1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
5 #include "qat_device.h"
6 #include "adf_transport_access_macros.h"
7 #include "qat_sym_pmd.h"
9 /* Hardware device information per generation */
11 struct qat_gen_hw_data qat_gen_config[] = {
14 .qp_hw_data = qat_gen1_qps,
18 .qp_hw_data = qat_gen1_qps,
19 /* gen2 has same ring layout as gen1 */
24 static struct qat_pci_device qat_pci_devices[RTE_PMD_QAT_MAX_PCI_DEVICES];
25 static int qat_nb_pci_devices;
28 * The set of PCI devices this driver supports
31 static const struct rte_pci_id pci_id_qat_map[] = {
33 RTE_PCI_DEVICE(0x8086, 0x0443),
36 RTE_PCI_DEVICE(0x8086, 0x37c9),
39 RTE_PCI_DEVICE(0x8086, 0x19e3),
42 RTE_PCI_DEVICE(0x8086, 0x6f55),
48 static struct qat_pci_device *
49 qat_pci_get_dev(uint8_t dev_id)
51 return &qat_pci_devices[dev_id];
54 static struct qat_pci_device *
55 qat_pci_get_named_dev(const char *name)
57 struct qat_pci_device *dev;
63 for (i = 0; i < RTE_PMD_QAT_MAX_PCI_DEVICES; i++) {
64 dev = &qat_pci_devices[i];
66 if ((dev->attached == QAT_ATTACHED) &&
67 (strcmp(dev->name, name) == 0))
75 qat_pci_find_free_device_index(void)
79 for (dev_id = 0; dev_id < RTE_PMD_QAT_MAX_PCI_DEVICES; dev_id++) {
80 if (qat_pci_devices[dev_id].attached == QAT_DETACHED)
86 struct qat_pci_device *
87 qat_get_qat_dev_from_pci_dev(struct rte_pci_device *pci_dev)
89 char name[QAT_DEV_NAME_MAX_LEN];
91 rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
93 return qat_pci_get_named_dev(name);
96 struct qat_pci_device *
97 qat_pci_device_allocate(struct rte_pci_device *pci_dev)
99 struct qat_pci_device *qat_dev;
101 char name[QAT_DEV_NAME_MAX_LEN];
103 rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
104 snprintf(name+strlen(name), QAT_DEV_NAME_MAX_LEN-strlen(name), "_qat");
105 if (qat_pci_get_named_dev(name) != NULL) {
106 QAT_LOG(ERR, "QAT device with name %s already allocated!",
111 qat_dev_id = qat_pci_find_free_device_index();
112 if (qat_dev_id == RTE_PMD_QAT_MAX_PCI_DEVICES) {
113 QAT_LOG(ERR, "Reached maximum number of QAT devices");
117 qat_dev = qat_pci_get_dev(qat_dev_id);
118 memset(qat_dev, 0, sizeof(*qat_dev));
119 snprintf(qat_dev->name, QAT_DEV_NAME_MAX_LEN, "%s", name);
120 qat_dev->qat_dev_id = qat_dev_id;
121 qat_dev->pci_dev = pci_dev;
122 switch (qat_dev->pci_dev->id.device_id) {
124 qat_dev->qat_dev_gen = QAT_GEN1;
129 qat_dev->qat_dev_gen = QAT_GEN2;
132 QAT_LOG(ERR, "Invalid dev_id, can't determine generation");
136 rte_spinlock_init(&qat_dev->arb_csr_lock);
138 qat_dev->attached = QAT_ATTACHED;
140 qat_nb_pci_devices++;
142 QAT_LOG(DEBUG, "QAT device %d allocated, name %s, total QATs %d",
143 qat_dev->qat_dev_id, qat_dev->name, qat_nb_pci_devices);
149 qat_pci_device_release(struct rte_pci_device *pci_dev)
151 struct qat_pci_device *qat_dev;
152 char name[QAT_DEV_NAME_MAX_LEN];
157 rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
158 snprintf(name+strlen(name), QAT_DEV_NAME_MAX_LEN-strlen(name), "_qat");
159 qat_dev = qat_pci_get_named_dev(name);
160 if (qat_dev != NULL) {
162 /* Check that there are no service devs still on pci device */
163 if (qat_dev->sym_dev != NULL)
166 qat_dev->attached = QAT_DETACHED;
167 qat_nb_pci_devices--;
169 QAT_LOG(DEBUG, "QAT device %s released, total QATs %d",
170 name, qat_nb_pci_devices);
175 qat_pci_dev_destroy(struct qat_pci_device *qat_pci_dev,
176 struct rte_pci_device *pci_dev)
178 qat_sym_dev_destroy(qat_pci_dev);
179 qat_comp_dev_destroy(qat_pci_dev);
180 qat_asym_dev_destroy(qat_pci_dev);
181 return qat_pci_device_release(pci_dev);
184 static int qat_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
185 struct rte_pci_device *pci_dev)
188 struct qat_pci_device *qat_pci_dev;
190 QAT_LOG(DEBUG, "Found QAT device at %02x:%02x.%x",
193 pci_dev->addr.function);
195 qat_pci_dev = qat_pci_device_allocate(pci_dev);
196 if (qat_pci_dev == NULL)
199 ret = qat_sym_dev_create(qat_pci_dev);
203 ret = qat_comp_dev_create(qat_pci_dev);
207 ret = qat_asym_dev_create(qat_pci_dev);
214 qat_pci_dev_destroy(qat_pci_dev, pci_dev);
219 static int qat_pci_remove(struct rte_pci_device *pci_dev)
221 struct qat_pci_device *qat_pci_dev;
226 qat_pci_dev = qat_get_qat_dev_from_pci_dev(pci_dev);
227 if (qat_pci_dev == NULL)
230 return qat_pci_dev_destroy(qat_pci_dev, pci_dev);
233 static struct rte_pci_driver rte_qat_pmd = {
234 .id_table = pci_id_qat_map,
235 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
236 .probe = qat_pci_probe,
237 .remove = qat_pci_remove
240 __attribute__((weak)) int
241 qat_sym_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused)
246 __attribute__((weak)) int
247 qat_asym_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused)
252 __attribute__((weak)) int
253 qat_sym_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
258 __attribute__((weak)) int
259 qat_asym_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
264 __attribute__((weak)) int
265 qat_comp_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused)
270 __attribute__((weak)) int
271 qat_comp_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
276 RTE_PMD_REGISTER_PCI(QAT_PCI_NAME, rte_qat_pmd);
277 RTE_PMD_REGISTER_PCI_TABLE(QAT_PCI_NAME, pci_id_qat_map);