1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
5 #include <rte_string_fns.h>
7 #include "qat_device.h"
8 #include "adf_transport_access_macros.h"
9 #include "qat_sym_pmd.h"
11 /* Hardware device information per generation */
13 struct qat_gen_hw_data qat_gen_config[] = {
16 .qp_hw_data = qat_gen1_qps,
20 .qp_hw_data = qat_gen1_qps,
21 /* gen2 has same ring layout as gen1 */
25 .qp_hw_data = qat_gen3_qps,
30 static struct qat_pci_device qat_pci_devices[RTE_PMD_QAT_MAX_PCI_DEVICES];
31 static int qat_nb_pci_devices;
34 * The set of PCI devices this driver supports
37 static const struct rte_pci_id pci_id_qat_map[] = {
39 RTE_PCI_DEVICE(0x8086, 0x0443),
42 RTE_PCI_DEVICE(0x8086, 0x37c9),
45 RTE_PCI_DEVICE(0x8086, 0x19e3),
48 RTE_PCI_DEVICE(0x8086, 0x6f55),
51 RTE_PCI_DEVICE(0x8086, 0x18a1),
56 static struct qat_pci_device *
57 qat_pci_get_dev(uint8_t dev_id)
59 return &qat_pci_devices[dev_id];
62 static struct qat_pci_device *
63 qat_pci_get_named_dev(const char *name)
65 struct qat_pci_device *dev;
71 for (i = 0; i < RTE_PMD_QAT_MAX_PCI_DEVICES; i++) {
72 dev = &qat_pci_devices[i];
74 if ((dev->attached == QAT_ATTACHED) &&
75 (strcmp(dev->name, name) == 0))
83 qat_pci_find_free_device_index(void)
87 for (dev_id = 0; dev_id < RTE_PMD_QAT_MAX_PCI_DEVICES; dev_id++) {
88 if (qat_pci_devices[dev_id].attached == QAT_DETACHED)
94 struct qat_pci_device *
95 qat_get_qat_dev_from_pci_dev(struct rte_pci_device *pci_dev)
97 char name[QAT_DEV_NAME_MAX_LEN];
99 rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
101 return qat_pci_get_named_dev(name);
104 struct qat_pci_device *
105 qat_pci_device_allocate(struct rte_pci_device *pci_dev)
107 struct qat_pci_device *qat_dev;
109 char name[QAT_DEV_NAME_MAX_LEN];
111 rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
112 snprintf(name+strlen(name), QAT_DEV_NAME_MAX_LEN-strlen(name), "_qat");
113 if (qat_pci_get_named_dev(name) != NULL) {
114 QAT_LOG(ERR, "QAT device with name %s already allocated!",
119 qat_dev_id = qat_pci_find_free_device_index();
120 if (qat_dev_id == RTE_PMD_QAT_MAX_PCI_DEVICES) {
121 QAT_LOG(ERR, "Reached maximum number of QAT devices");
125 qat_dev = qat_pci_get_dev(qat_dev_id);
126 memset(qat_dev, 0, sizeof(*qat_dev));
127 strlcpy(qat_dev->name, name, QAT_DEV_NAME_MAX_LEN);
128 qat_dev->qat_dev_id = qat_dev_id;
129 qat_dev->pci_dev = pci_dev;
130 switch (qat_dev->pci_dev->id.device_id) {
132 qat_dev->qat_dev_gen = QAT_GEN1;
137 qat_dev->qat_dev_gen = QAT_GEN2;
140 qat_dev->qat_dev_gen = QAT_GEN3;
143 QAT_LOG(ERR, "Invalid dev_id, can't determine generation");
147 rte_spinlock_init(&qat_dev->arb_csr_lock);
149 qat_dev->attached = QAT_ATTACHED;
151 qat_nb_pci_devices++;
153 QAT_LOG(DEBUG, "QAT device %d allocated, name %s, total QATs %d",
154 qat_dev->qat_dev_id, qat_dev->name, qat_nb_pci_devices);
160 qat_pci_device_release(struct rte_pci_device *pci_dev)
162 struct qat_pci_device *qat_dev;
163 char name[QAT_DEV_NAME_MAX_LEN];
168 rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
169 snprintf(name+strlen(name), QAT_DEV_NAME_MAX_LEN-strlen(name), "_qat");
170 qat_dev = qat_pci_get_named_dev(name);
171 if (qat_dev != NULL) {
173 /* Check that there are no service devs still on pci device */
174 if (qat_dev->sym_dev != NULL)
177 qat_dev->attached = QAT_DETACHED;
178 qat_nb_pci_devices--;
180 QAT_LOG(DEBUG, "QAT device %s released, total QATs %d",
181 name, qat_nb_pci_devices);
186 qat_pci_dev_destroy(struct qat_pci_device *qat_pci_dev,
187 struct rte_pci_device *pci_dev)
189 qat_sym_dev_destroy(qat_pci_dev);
190 qat_comp_dev_destroy(qat_pci_dev);
191 qat_asym_dev_destroy(qat_pci_dev);
192 return qat_pci_device_release(pci_dev);
195 static int qat_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
196 struct rte_pci_device *pci_dev)
199 int num_pmds_created = 0;
200 struct qat_pci_device *qat_pci_dev;
202 QAT_LOG(DEBUG, "Found QAT device at %02x:%02x.%x",
205 pci_dev->addr.function);
207 qat_pci_dev = qat_pci_device_allocate(pci_dev);
208 if (qat_pci_dev == NULL)
211 ret = qat_sym_dev_create(qat_pci_dev);
216 "Failed to create QAT SYM PMD on device %s",
219 ret = qat_comp_dev_create(qat_pci_dev);
224 "Failed to create QAT COMP PMD on device %s",
227 ret = qat_asym_dev_create(qat_pci_dev);
232 "Failed to create QAT ASYM PMD on device %s",
235 if (num_pmds_created == 0)
236 qat_pci_dev_destroy(qat_pci_dev, pci_dev);
241 static int qat_pci_remove(struct rte_pci_device *pci_dev)
243 struct qat_pci_device *qat_pci_dev;
248 qat_pci_dev = qat_get_qat_dev_from_pci_dev(pci_dev);
249 if (qat_pci_dev == NULL)
252 return qat_pci_dev_destroy(qat_pci_dev, pci_dev);
255 static struct rte_pci_driver rte_qat_pmd = {
256 .id_table = pci_id_qat_map,
257 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
258 .probe = qat_pci_probe,
259 .remove = qat_pci_remove
262 __attribute__((weak)) int
263 qat_sym_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused)
268 __attribute__((weak)) int
269 qat_asym_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused)
274 __attribute__((weak)) int
275 qat_sym_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
280 __attribute__((weak)) int
281 qat_asym_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
286 __attribute__((weak)) int
287 qat_comp_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused)
292 __attribute__((weak)) int
293 qat_comp_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
298 RTE_PMD_REGISTER_PCI(QAT_PCI_NAME, rte_qat_pmd);
299 RTE_PMD_REGISTER_PCI_TABLE(QAT_PCI_NAME, pci_id_qat_map);