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 */
26 static struct qat_pci_device qat_pci_devices[RTE_PMD_QAT_MAX_PCI_DEVICES];
27 static int qat_nb_pci_devices;
30 * The set of PCI devices this driver supports
33 static const struct rte_pci_id pci_id_qat_map[] = {
35 RTE_PCI_DEVICE(0x8086, 0x0443),
38 RTE_PCI_DEVICE(0x8086, 0x37c9),
41 RTE_PCI_DEVICE(0x8086, 0x19e3),
44 RTE_PCI_DEVICE(0x8086, 0x6f55),
50 static struct qat_pci_device *
51 qat_pci_get_dev(uint8_t dev_id)
53 return &qat_pci_devices[dev_id];
56 static struct qat_pci_device *
57 qat_pci_get_named_dev(const char *name)
59 struct qat_pci_device *dev;
65 for (i = 0; i < RTE_PMD_QAT_MAX_PCI_DEVICES; i++) {
66 dev = &qat_pci_devices[i];
68 if ((dev->attached == QAT_ATTACHED) &&
69 (strcmp(dev->name, name) == 0))
77 qat_pci_find_free_device_index(void)
81 for (dev_id = 0; dev_id < RTE_PMD_QAT_MAX_PCI_DEVICES; dev_id++) {
82 if (qat_pci_devices[dev_id].attached == QAT_DETACHED)
88 struct qat_pci_device *
89 qat_get_qat_dev_from_pci_dev(struct rte_pci_device *pci_dev)
91 char name[QAT_DEV_NAME_MAX_LEN];
93 rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
95 return qat_pci_get_named_dev(name);
98 struct qat_pci_device *
99 qat_pci_device_allocate(struct rte_pci_device *pci_dev)
101 struct qat_pci_device *qat_dev;
103 char name[QAT_DEV_NAME_MAX_LEN];
105 rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
106 snprintf(name+strlen(name), QAT_DEV_NAME_MAX_LEN-strlen(name), "_qat");
107 if (qat_pci_get_named_dev(name) != NULL) {
108 QAT_LOG(ERR, "QAT device with name %s already allocated!",
113 qat_dev_id = qat_pci_find_free_device_index();
114 if (qat_dev_id == RTE_PMD_QAT_MAX_PCI_DEVICES) {
115 QAT_LOG(ERR, "Reached maximum number of QAT devices");
119 qat_dev = qat_pci_get_dev(qat_dev_id);
120 memset(qat_dev, 0, sizeof(*qat_dev));
121 strlcpy(qat_dev->name, name, QAT_DEV_NAME_MAX_LEN);
122 qat_dev->qat_dev_id = qat_dev_id;
123 qat_dev->pci_dev = pci_dev;
124 switch (qat_dev->pci_dev->id.device_id) {
126 qat_dev->qat_dev_gen = QAT_GEN1;
131 qat_dev->qat_dev_gen = QAT_GEN2;
134 QAT_LOG(ERR, "Invalid dev_id, can't determine generation");
138 rte_spinlock_init(&qat_dev->arb_csr_lock);
140 qat_dev->attached = QAT_ATTACHED;
142 qat_nb_pci_devices++;
144 QAT_LOG(DEBUG, "QAT device %d allocated, name %s, total QATs %d",
145 qat_dev->qat_dev_id, qat_dev->name, qat_nb_pci_devices);
151 qat_pci_device_release(struct rte_pci_device *pci_dev)
153 struct qat_pci_device *qat_dev;
154 char name[QAT_DEV_NAME_MAX_LEN];
159 rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
160 snprintf(name+strlen(name), QAT_DEV_NAME_MAX_LEN-strlen(name), "_qat");
161 qat_dev = qat_pci_get_named_dev(name);
162 if (qat_dev != NULL) {
164 /* Check that there are no service devs still on pci device */
165 if (qat_dev->sym_dev != NULL)
168 qat_dev->attached = QAT_DETACHED;
169 qat_nb_pci_devices--;
171 QAT_LOG(DEBUG, "QAT device %s released, total QATs %d",
172 name, qat_nb_pci_devices);
177 qat_pci_dev_destroy(struct qat_pci_device *qat_pci_dev,
178 struct rte_pci_device *pci_dev)
180 qat_sym_dev_destroy(qat_pci_dev);
181 qat_comp_dev_destroy(qat_pci_dev);
182 qat_asym_dev_destroy(qat_pci_dev);
183 return qat_pci_device_release(pci_dev);
186 static int qat_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
187 struct rte_pci_device *pci_dev)
190 struct qat_pci_device *qat_pci_dev;
192 QAT_LOG(DEBUG, "Found QAT device at %02x:%02x.%x",
195 pci_dev->addr.function);
197 qat_pci_dev = qat_pci_device_allocate(pci_dev);
198 if (qat_pci_dev == NULL)
201 ret = qat_sym_dev_create(qat_pci_dev);
205 ret = qat_comp_dev_create(qat_pci_dev);
209 ret = qat_asym_dev_create(qat_pci_dev);
216 qat_pci_dev_destroy(qat_pci_dev, pci_dev);
221 static int qat_pci_remove(struct rte_pci_device *pci_dev)
223 struct qat_pci_device *qat_pci_dev;
228 qat_pci_dev = qat_get_qat_dev_from_pci_dev(pci_dev);
229 if (qat_pci_dev == NULL)
232 return qat_pci_dev_destroy(qat_pci_dev, pci_dev);
235 static struct rte_pci_driver rte_qat_pmd = {
236 .id_table = pci_id_qat_map,
237 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
238 .probe = qat_pci_probe,
239 .remove = qat_pci_remove
242 __attribute__((weak)) int
243 qat_sym_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused)
248 __attribute__((weak)) int
249 qat_asym_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused)
254 __attribute__((weak)) int
255 qat_sym_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
260 __attribute__((weak)) int
261 qat_asym_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
266 __attribute__((weak)) int
267 qat_comp_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused)
272 __attribute__((weak)) int
273 qat_comp_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
278 RTE_PMD_REGISTER_PCI(QAT_PCI_NAME, rte_qat_pmd);
279 RTE_PMD_REGISTER_PCI_TABLE(QAT_PCI_NAME, pci_id_qat_map);