1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
5 #include <rte_string_fns.h>
6 #include <rte_devargs.h>
9 #include "qat_device.h"
10 #include "adf_transport_access_macros.h"
11 #include "qat_sym_pmd.h"
12 #include "qat_comp_pmd.h"
14 /* Hardware device information per generation */
16 struct qat_gen_hw_data qat_gen_config[] = {
19 .qp_hw_data = qat_gen1_qps,
20 .comp_num_im_bufs_required = QAT_NUM_INTERM_BUFS_GEN1
24 .qp_hw_data = qat_gen1_qps,
25 /* gen2 has same ring layout as gen1 */
26 .comp_num_im_bufs_required = QAT_NUM_INTERM_BUFS_GEN2
30 .qp_hw_data = qat_gen3_qps,
31 .comp_num_im_bufs_required = QAT_NUM_INTERM_BUFS_GEN3
36 static struct qat_pci_device qat_pci_devices[RTE_PMD_QAT_MAX_PCI_DEVICES];
37 static int qat_nb_pci_devices;
40 * The set of PCI devices this driver supports
43 static const struct rte_pci_id pci_id_qat_map[] = {
45 RTE_PCI_DEVICE(0x8086, 0x0443),
48 RTE_PCI_DEVICE(0x8086, 0x37c9),
51 RTE_PCI_DEVICE(0x8086, 0x19e3),
54 RTE_PCI_DEVICE(0x8086, 0x6f55),
57 RTE_PCI_DEVICE(0x8086, 0x18a1),
62 static struct qat_pci_device *
63 qat_pci_get_dev(uint8_t dev_id)
65 return &qat_pci_devices[dev_id];
68 static struct qat_pci_device *
69 qat_pci_get_named_dev(const char *name)
71 struct qat_pci_device *dev;
77 for (i = 0; i < RTE_PMD_QAT_MAX_PCI_DEVICES; i++) {
78 dev = &qat_pci_devices[i];
80 if ((dev->attached == QAT_ATTACHED) &&
81 (strcmp(dev->name, name) == 0))
89 qat_pci_find_free_device_index(void)
93 for (dev_id = 0; dev_id < RTE_PMD_QAT_MAX_PCI_DEVICES; dev_id++) {
94 if (qat_pci_devices[dev_id].attached == QAT_DETACHED)
100 struct qat_pci_device *
101 qat_get_qat_dev_from_pci_dev(struct rte_pci_device *pci_dev)
103 char name[QAT_DEV_NAME_MAX_LEN];
105 rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
107 return qat_pci_get_named_dev(name);
110 static void qat_dev_parse_cmd(const char *str, struct qat_dev_cmd_param
117 char value_str[4] = { };
119 param = qat_dev_cmd_param[i].name;
123 const char *arg = strstr(str, param);
124 const char *arg2 = NULL;
127 arg2 = arg + strlen(param);
129 QAT_LOG(DEBUG, "parsing error '=' sign"
130 " should immediately follow %s",
136 QAT_LOG(DEBUG, "%s not provided", param);
141 if (!isdigit(*(arg2 + iter)))
146 QAT_LOG(DEBUG, "parsing error %s"
147 " no number provided",
150 memcpy(value_str, arg2, iter);
151 value = strtol(value_str, NULL, 10);
152 if (value > MAX_QP_THRESHOLD_SIZE) {
153 QAT_LOG(DEBUG, "Exceeded max size of"
154 " threshold, setting to %d",
155 MAX_QP_THRESHOLD_SIZE);
156 value = MAX_QP_THRESHOLD_SIZE;
158 QAT_LOG(DEBUG, "parsing %s = %ld",
162 qat_dev_cmd_param[i].val = value;
167 struct qat_pci_device *
168 qat_pci_device_allocate(struct rte_pci_device *pci_dev,
169 struct qat_dev_cmd_param *qat_dev_cmd_param)
171 struct qat_pci_device *qat_dev;
173 char name[QAT_DEV_NAME_MAX_LEN];
174 struct rte_devargs *devargs = pci_dev->device.devargs;
176 rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
177 snprintf(name+strlen(name), QAT_DEV_NAME_MAX_LEN-strlen(name), "_qat");
178 if (qat_pci_get_named_dev(name) != NULL) {
179 QAT_LOG(ERR, "QAT device with name %s already allocated!",
184 qat_dev_id = qat_pci_find_free_device_index();
185 if (qat_dev_id == RTE_PMD_QAT_MAX_PCI_DEVICES) {
186 QAT_LOG(ERR, "Reached maximum number of QAT devices");
190 qat_dev = qat_pci_get_dev(qat_dev_id);
191 memset(qat_dev, 0, sizeof(*qat_dev));
192 strlcpy(qat_dev->name, name, QAT_DEV_NAME_MAX_LEN);
193 qat_dev->qat_dev_id = qat_dev_id;
194 qat_dev->pci_dev = pci_dev;
195 switch (qat_dev->pci_dev->id.device_id) {
197 qat_dev->qat_dev_gen = QAT_GEN1;
202 qat_dev->qat_dev_gen = QAT_GEN2;
205 qat_dev->qat_dev_gen = QAT_GEN3;
208 QAT_LOG(ERR, "Invalid dev_id, can't determine generation");
212 if (devargs && devargs->drv_str)
213 qat_dev_parse_cmd(devargs->drv_str, qat_dev_cmd_param);
215 rte_spinlock_init(&qat_dev->arb_csr_lock);
217 qat_dev->attached = QAT_ATTACHED;
219 qat_nb_pci_devices++;
221 QAT_LOG(DEBUG, "QAT device %d allocated, name %s, total QATs %d",
222 qat_dev->qat_dev_id, qat_dev->name, qat_nb_pci_devices);
228 qat_pci_device_release(struct rte_pci_device *pci_dev)
230 struct qat_pci_device *qat_dev;
231 char name[QAT_DEV_NAME_MAX_LEN];
236 rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
237 snprintf(name+strlen(name), QAT_DEV_NAME_MAX_LEN-strlen(name), "_qat");
238 qat_dev = qat_pci_get_named_dev(name);
239 if (qat_dev != NULL) {
241 /* Check that there are no service devs still on pci device */
242 if (qat_dev->sym_dev != NULL)
245 qat_dev->attached = QAT_DETACHED;
246 qat_nb_pci_devices--;
248 QAT_LOG(DEBUG, "QAT device %s released, total QATs %d",
249 name, qat_nb_pci_devices);
254 qat_pci_dev_destroy(struct qat_pci_device *qat_pci_dev,
255 struct rte_pci_device *pci_dev)
257 qat_sym_dev_destroy(qat_pci_dev);
258 qat_comp_dev_destroy(qat_pci_dev);
259 qat_asym_dev_destroy(qat_pci_dev);
260 return qat_pci_device_release(pci_dev);
263 static int qat_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
264 struct rte_pci_device *pci_dev)
266 int sym_ret = 0, asym_ret = 0, comp_ret = 0;
267 int num_pmds_created = 0;
268 struct qat_pci_device *qat_pci_dev;
269 struct qat_dev_cmd_param qat_dev_cmd_param[] = {
270 { SYM_ENQ_THRESHOLD_NAME, 0 },
271 { ASYM_ENQ_THRESHOLD_NAME, 0 },
272 { COMP_ENQ_THRESHOLD_NAME, 0 },
276 QAT_LOG(DEBUG, "Found QAT device at %02x:%02x.%x",
279 pci_dev->addr.function);
281 qat_pci_dev = qat_pci_device_allocate(pci_dev, qat_dev_cmd_param);
282 if (qat_pci_dev == NULL)
285 sym_ret = qat_sym_dev_create(qat_pci_dev, qat_dev_cmd_param);
292 "Failed to create QAT SYM PMD on device %s",
295 comp_ret = qat_comp_dev_create(qat_pci_dev, qat_dev_cmd_param);
300 "Failed to create QAT COMP PMD on device %s",
303 asym_ret = qat_asym_dev_create(qat_pci_dev, qat_dev_cmd_param);
308 "Failed to create QAT ASYM PMD on device %s",
311 if (num_pmds_created == 0)
312 qat_pci_dev_destroy(qat_pci_dev, pci_dev);
317 static int qat_pci_remove(struct rte_pci_device *pci_dev)
319 struct qat_pci_device *qat_pci_dev;
324 qat_pci_dev = qat_get_qat_dev_from_pci_dev(pci_dev);
325 if (qat_pci_dev == NULL)
328 return qat_pci_dev_destroy(qat_pci_dev, pci_dev);
331 static struct rte_pci_driver rte_qat_pmd = {
332 .id_table = pci_id_qat_map,
333 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
334 .probe = qat_pci_probe,
335 .remove = qat_pci_remove
339 qat_sym_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused,
340 struct qat_dev_cmd_param *qat_dev_cmd_param __rte_unused)
346 qat_asym_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused,
347 struct qat_dev_cmd_param *qat_dev_cmd_param __rte_unused)
353 qat_sym_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
359 qat_asym_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
365 qat_comp_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused,
366 struct qat_dev_cmd_param *qat_dev_cmd_param __rte_unused)
372 qat_comp_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
377 RTE_PMD_REGISTER_PCI(QAT_PCI_NAME, rte_qat_pmd);
378 RTE_PMD_REGISTER_PCI_TABLE(QAT_PCI_NAME, pci_id_qat_map);