1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018-2020 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 .comp_num_im_bufs_required = QAT_NUM_INTERM_BUFS_GEN3
40 /* per-process array of device data */
41 struct qat_device_info qat_pci_devs[RTE_PMD_QAT_MAX_PCI_DEVICES];
42 static int qat_nb_pci_devices;
45 * The set of PCI devices this driver supports
48 static const struct rte_pci_id pci_id_qat_map[] = {
50 RTE_PCI_DEVICE(0x8086, 0x0443),
53 RTE_PCI_DEVICE(0x8086, 0x37c9),
56 RTE_PCI_DEVICE(0x8086, 0x19e3),
59 RTE_PCI_DEVICE(0x8086, 0x6f55),
62 RTE_PCI_DEVICE(0x8086, 0x18ef),
65 RTE_PCI_DEVICE(0x8086, 0x18a1),
68 RTE_PCI_DEVICE(0x8086, 0x4941),
73 static struct qat_pci_device *
74 qat_pci_get_named_dev(const char *name)
81 for (i = 0; i < RTE_PMD_QAT_MAX_PCI_DEVICES; i++) {
82 if (qat_pci_devs[i].mz &&
83 (strcmp(((struct qat_pci_device *)
84 qat_pci_devs[i].mz->addr)->name, name)
86 return (struct qat_pci_device *)
87 qat_pci_devs[i].mz->addr;
94 qat_pci_find_free_device_index(void)
98 for (dev_id = 0; dev_id < RTE_PMD_QAT_MAX_PCI_DEVICES;
100 if (qat_pci_devs[dev_id].mz == NULL)
106 struct qat_pci_device *
107 qat_get_qat_dev_from_pci_dev(struct rte_pci_device *pci_dev)
109 char name[QAT_DEV_NAME_MAX_LEN];
111 rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
113 return qat_pci_get_named_dev(name);
116 static void qat_dev_parse_cmd(const char *str, struct qat_dev_cmd_param
123 char value_str[4] = { };
125 param = qat_dev_cmd_param[i].name;
129 const char *arg = strstr(str, param);
130 const char *arg2 = NULL;
133 arg2 = arg + strlen(param);
135 QAT_LOG(DEBUG, "parsing error '=' sign"
136 " should immediately follow %s",
142 QAT_LOG(DEBUG, "%s not provided", param);
147 if (!isdigit(*(arg2 + iter)))
152 QAT_LOG(DEBUG, "parsing error %s"
153 " no number provided",
156 memcpy(value_str, arg2, iter);
157 value = strtol(value_str, NULL, 10);
158 if (value > MAX_QP_THRESHOLD_SIZE) {
159 QAT_LOG(DEBUG, "Exceeded max size of"
160 " threshold, setting to %d",
161 MAX_QP_THRESHOLD_SIZE);
162 value = MAX_QP_THRESHOLD_SIZE;
164 QAT_LOG(DEBUG, "parsing %s = %ld",
168 qat_dev_cmd_param[i].val = value;
173 struct qat_pci_device *
174 qat_pci_device_allocate(struct rte_pci_device *pci_dev,
175 struct qat_dev_cmd_param *qat_dev_cmd_param)
177 struct qat_pci_device *qat_dev;
178 uint8_t qat_dev_id = 0;
179 char name[QAT_DEV_NAME_MAX_LEN];
180 struct rte_devargs *devargs = pci_dev->device.devargs;
182 rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
183 snprintf(name+strlen(name), QAT_DEV_NAME_MAX_LEN-strlen(name), "_qat");
185 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
186 const struct rte_memzone *mz = rte_memzone_lookup(name);
190 "Secondary can't find %s mz, did primary create device?",
195 qat_pci_devs[qat_dev->qat_dev_id].mz = mz;
196 qat_pci_devs[qat_dev->qat_dev_id].pci_dev = pci_dev;
197 qat_nb_pci_devices++;
198 QAT_LOG(DEBUG, "QAT device %d found, name %s, total QATs %d",
199 qat_dev->qat_dev_id, qat_dev->name, qat_nb_pci_devices);
203 if (qat_pci_get_named_dev(name) != NULL) {
204 QAT_LOG(ERR, "QAT device with name %s already allocated!",
209 qat_dev_id = qat_pci_find_free_device_index();
210 if (qat_dev_id == RTE_PMD_QAT_MAX_PCI_DEVICES) {
211 QAT_LOG(ERR, "Reached maximum number of QAT devices");
215 qat_pci_devs[qat_dev_id].mz = rte_memzone_reserve(name,
216 sizeof(struct qat_pci_device),
219 if (qat_pci_devs[qat_dev_id].mz == NULL) {
220 QAT_LOG(ERR, "Error when allocating memzone for QAT_%d",
225 qat_dev = qat_pci_devs[qat_dev_id].mz->addr;
226 memset(qat_dev, 0, sizeof(*qat_dev));
227 strlcpy(qat_dev->name, name, QAT_DEV_NAME_MAX_LEN);
228 qat_dev->qat_dev_id = qat_dev_id;
229 qat_pci_devs[qat_dev_id].pci_dev = pci_dev;
230 switch (pci_dev->id.device_id) {
232 qat_dev->qat_dev_gen = QAT_GEN1;
238 qat_dev->qat_dev_gen = QAT_GEN2;
241 qat_dev->qat_dev_gen = QAT_GEN3;
244 qat_dev->qat_dev_gen = QAT_GEN4;
247 QAT_LOG(ERR, "Invalid dev_id, can't determine generation");
248 rte_memzone_free(qat_pci_devs[qat_dev->qat_dev_id].mz);
252 if (devargs && devargs->drv_str)
253 qat_dev_parse_cmd(devargs->drv_str, qat_dev_cmd_param);
255 if (qat_dev->qat_dev_gen >= QAT_GEN4) {
256 int ret = qat_read_qp_config(qat_dev, qat_dev->qat_dev_gen);
260 "Cannot acquire ring configuration for QAT_%d",
266 rte_spinlock_init(&qat_dev->arb_csr_lock);
267 qat_nb_pci_devices++;
269 QAT_LOG(DEBUG, "QAT device %d found, name %s, total QATs %d",
270 qat_dev->qat_dev_id, qat_dev->name, qat_nb_pci_devices);
276 qat_pci_device_release(struct rte_pci_device *pci_dev)
278 struct qat_pci_device *qat_dev;
279 char name[QAT_DEV_NAME_MAX_LEN];
285 rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
286 snprintf(name+strlen(name), QAT_DEV_NAME_MAX_LEN-strlen(name), "_qat");
287 qat_dev = qat_pci_get_named_dev(name);
288 if (qat_dev != NULL) {
290 struct qat_device_info *inst =
291 &qat_pci_devs[qat_dev->qat_dev_id];
292 /* Check that there are no service devs still on pci device */
294 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
295 if (qat_dev->sym_dev != NULL) {
296 QAT_LOG(DEBUG, "QAT sym device %s is busy",
300 if (qat_dev->asym_dev != NULL) {
301 QAT_LOG(DEBUG, "QAT asym device %s is busy",
305 if (qat_dev->comp_dev != NULL) {
306 QAT_LOG(DEBUG, "QAT comp device %s is busy",
312 rte_memzone_free(inst->mz);
314 memset(inst, 0, sizeof(struct qat_device_info));
315 qat_nb_pci_devices--;
316 QAT_LOG(DEBUG, "QAT device %s released, total QATs %d",
317 name, qat_nb_pci_devices);
323 qat_pci_dev_destroy(struct qat_pci_device *qat_pci_dev,
324 struct rte_pci_device *pci_dev)
326 qat_sym_dev_destroy(qat_pci_dev);
327 qat_comp_dev_destroy(qat_pci_dev);
328 qat_asym_dev_destroy(qat_pci_dev);
329 return qat_pci_device_release(pci_dev);
332 static int qat_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
333 struct rte_pci_device *pci_dev)
335 int sym_ret = 0, asym_ret = 0, comp_ret = 0;
336 int num_pmds_created = 0;
337 struct qat_pci_device *qat_pci_dev;
338 struct qat_dev_cmd_param qat_dev_cmd_param[] = {
339 { SYM_ENQ_THRESHOLD_NAME, 0 },
340 { ASYM_ENQ_THRESHOLD_NAME, 0 },
341 { COMP_ENQ_THRESHOLD_NAME, 0 },
345 QAT_LOG(DEBUG, "Found QAT device at %02x:%02x.%x",
348 pci_dev->addr.function);
350 qat_pci_dev = qat_pci_device_allocate(pci_dev, qat_dev_cmd_param);
351 if (qat_pci_dev == NULL)
354 sym_ret = qat_sym_dev_create(qat_pci_dev, qat_dev_cmd_param);
361 "Failed to create QAT SYM PMD on device %s",
364 comp_ret = qat_comp_dev_create(qat_pci_dev, qat_dev_cmd_param);
369 "Failed to create QAT COMP PMD on device %s",
372 asym_ret = qat_asym_dev_create(qat_pci_dev, qat_dev_cmd_param);
377 "Failed to create QAT ASYM PMD on device %s",
380 if (num_pmds_created == 0)
381 qat_pci_dev_destroy(qat_pci_dev, pci_dev);
386 static int qat_pci_remove(struct rte_pci_device *pci_dev)
388 struct qat_pci_device *qat_pci_dev;
393 qat_pci_dev = qat_get_qat_dev_from_pci_dev(pci_dev);
394 if (qat_pci_dev == NULL)
397 return qat_pci_dev_destroy(qat_pci_dev, pci_dev);
400 static struct rte_pci_driver rte_qat_pmd = {
401 .id_table = pci_id_qat_map,
402 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
403 .probe = qat_pci_probe,
404 .remove = qat_pci_remove
408 qat_sym_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused,
409 struct qat_dev_cmd_param *qat_dev_cmd_param __rte_unused)
415 qat_asym_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused,
416 struct qat_dev_cmd_param *qat_dev_cmd_param __rte_unused)
422 qat_sym_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
428 qat_asym_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
434 qat_comp_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused,
435 struct qat_dev_cmd_param *qat_dev_cmd_param __rte_unused)
441 qat_comp_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
446 RTE_PMD_REGISTER_PCI(QAT_PCI_NAME, rte_qat_pmd);
447 RTE_PMD_REGISTER_PCI_TABLE(QAT_PCI_NAME, pci_id_qat_map);
448 RTE_PMD_REGISTER_KMOD_DEP(QAT_PCI_NAME, "* igb_uio | uio_pci_generic | vfio-pci");