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"
10 #include "qat_comp_pmd.h"
12 /* Hardware device information per generation */
14 struct qat_gen_hw_data qat_gen_config[] = {
17 .qp_hw_data = qat_gen1_qps,
18 .comp_num_im_bufs_required = QAT_NUM_INTERM_BUFS_GEN1
22 .qp_hw_data = qat_gen1_qps,
23 /* gen2 has same ring layout as gen1 */
24 .comp_num_im_bufs_required = QAT_NUM_INTERM_BUFS_GEN2
28 .qp_hw_data = qat_gen3_qps,
29 .comp_num_im_bufs_required = QAT_NUM_INTERM_BUFS_GEN3
34 static struct qat_pci_device qat_pci_devices[RTE_PMD_QAT_MAX_PCI_DEVICES];
35 static int qat_nb_pci_devices;
38 * The set of PCI devices this driver supports
41 static const struct rte_pci_id pci_id_qat_map[] = {
43 RTE_PCI_DEVICE(0x8086, 0x0443),
46 RTE_PCI_DEVICE(0x8086, 0x37c9),
49 RTE_PCI_DEVICE(0x8086, 0x19e3),
52 RTE_PCI_DEVICE(0x8086, 0x6f55),
55 RTE_PCI_DEVICE(0x8086, 0x18a1),
60 static struct qat_pci_device *
61 qat_pci_get_dev(uint8_t dev_id)
63 return &qat_pci_devices[dev_id];
66 static struct qat_pci_device *
67 qat_pci_get_named_dev(const char *name)
69 struct qat_pci_device *dev;
75 for (i = 0; i < RTE_PMD_QAT_MAX_PCI_DEVICES; i++) {
76 dev = &qat_pci_devices[i];
78 if ((dev->attached == QAT_ATTACHED) &&
79 (strcmp(dev->name, name) == 0))
87 qat_pci_find_free_device_index(void)
91 for (dev_id = 0; dev_id < RTE_PMD_QAT_MAX_PCI_DEVICES; dev_id++) {
92 if (qat_pci_devices[dev_id].attached == QAT_DETACHED)
98 struct qat_pci_device *
99 qat_get_qat_dev_from_pci_dev(struct rte_pci_device *pci_dev)
101 char name[QAT_DEV_NAME_MAX_LEN];
103 rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
105 return qat_pci_get_named_dev(name);
108 struct qat_pci_device *
109 qat_pci_device_allocate(struct rte_pci_device *pci_dev)
111 struct qat_pci_device *qat_dev;
113 char name[QAT_DEV_NAME_MAX_LEN];
115 rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
116 snprintf(name+strlen(name), QAT_DEV_NAME_MAX_LEN-strlen(name), "_qat");
117 if (qat_pci_get_named_dev(name) != NULL) {
118 QAT_LOG(ERR, "QAT device with name %s already allocated!",
123 qat_dev_id = qat_pci_find_free_device_index();
124 if (qat_dev_id == RTE_PMD_QAT_MAX_PCI_DEVICES) {
125 QAT_LOG(ERR, "Reached maximum number of QAT devices");
129 qat_dev = qat_pci_get_dev(qat_dev_id);
130 memset(qat_dev, 0, sizeof(*qat_dev));
131 strlcpy(qat_dev->name, name, QAT_DEV_NAME_MAX_LEN);
132 qat_dev->qat_dev_id = qat_dev_id;
133 qat_dev->pci_dev = pci_dev;
134 switch (qat_dev->pci_dev->id.device_id) {
136 qat_dev->qat_dev_gen = QAT_GEN1;
141 qat_dev->qat_dev_gen = QAT_GEN2;
144 qat_dev->qat_dev_gen = QAT_GEN3;
147 QAT_LOG(ERR, "Invalid dev_id, can't determine generation");
151 rte_spinlock_init(&qat_dev->arb_csr_lock);
153 qat_dev->attached = QAT_ATTACHED;
155 qat_nb_pci_devices++;
157 QAT_LOG(DEBUG, "QAT device %d allocated, name %s, total QATs %d",
158 qat_dev->qat_dev_id, qat_dev->name, qat_nb_pci_devices);
164 qat_pci_device_release(struct rte_pci_device *pci_dev)
166 struct qat_pci_device *qat_dev;
167 char name[QAT_DEV_NAME_MAX_LEN];
172 rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
173 snprintf(name+strlen(name), QAT_DEV_NAME_MAX_LEN-strlen(name), "_qat");
174 qat_dev = qat_pci_get_named_dev(name);
175 if (qat_dev != NULL) {
177 /* Check that there are no service devs still on pci device */
178 if (qat_dev->sym_dev != NULL)
181 qat_dev->attached = QAT_DETACHED;
182 qat_nb_pci_devices--;
184 QAT_LOG(DEBUG, "QAT device %s released, total QATs %d",
185 name, qat_nb_pci_devices);
190 qat_pci_dev_destroy(struct qat_pci_device *qat_pci_dev,
191 struct rte_pci_device *pci_dev)
193 qat_sym_dev_destroy(qat_pci_dev);
194 qat_comp_dev_destroy(qat_pci_dev);
195 qat_asym_dev_destroy(qat_pci_dev);
196 return qat_pci_device_release(pci_dev);
199 static int qat_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
200 struct rte_pci_device *pci_dev)
203 int num_pmds_created = 0;
204 struct qat_pci_device *qat_pci_dev;
206 QAT_LOG(DEBUG, "Found QAT device at %02x:%02x.%x",
209 pci_dev->addr.function);
211 qat_pci_dev = qat_pci_device_allocate(pci_dev);
212 if (qat_pci_dev == NULL)
215 ret = qat_sym_dev_create(qat_pci_dev);
220 "Failed to create QAT SYM PMD on device %s",
223 ret = qat_comp_dev_create(qat_pci_dev);
228 "Failed to create QAT COMP PMD on device %s",
231 ret = qat_asym_dev_create(qat_pci_dev);
236 "Failed to create QAT ASYM PMD on device %s",
239 if (num_pmds_created == 0)
240 qat_pci_dev_destroy(qat_pci_dev, pci_dev);
245 static int qat_pci_remove(struct rte_pci_device *pci_dev)
247 struct qat_pci_device *qat_pci_dev;
252 qat_pci_dev = qat_get_qat_dev_from_pci_dev(pci_dev);
253 if (qat_pci_dev == NULL)
256 return qat_pci_dev_destroy(qat_pci_dev, pci_dev);
259 static struct rte_pci_driver rte_qat_pmd = {
260 .id_table = pci_id_qat_map,
261 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
262 .probe = qat_pci_probe,
263 .remove = qat_pci_remove
267 qat_sym_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused)
273 qat_asym_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused)
279 qat_sym_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
285 qat_asym_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
291 qat_comp_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused)
297 qat_comp_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
302 RTE_PMD_REGISTER_PCI(QAT_PCI_NAME, rte_qat_pmd);
303 RTE_PMD_REGISTER_PCI_TABLE(QAT_PCI_NAME, pci_id_qat_map);