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"
13 #include "adf_pf2vf_msg.h"
14 #include "qat_pf2vf.h"
17 struct qat_pf2vf_dev qat_pf2vf_gen4 = {
18 .pf2vf_offset = ADF_4XXXIOV_PF2VM_OFFSET,
19 .vf2pf_offset = ADF_4XXXIOV_VM2PF_OFFSET,
20 .pf2vf_type_shift = ADF_PFVF_2X_MSGTYPE_SHIFT,
21 .pf2vf_type_mask = ADF_PFVF_2X_MSGTYPE_MASK,
22 .pf2vf_data_shift = ADF_PFVF_2X_MSGDATA_SHIFT,
23 .pf2vf_data_mask = ADF_PFVF_2X_MSGDATA_MASK,
26 /* Hardware device information per generation */
28 struct qat_gen_hw_data qat_gen_config[] = {
31 .qp_hw_data = qat_gen1_qps,
32 .comp_num_im_bufs_required = QAT_NUM_INTERM_BUFS_GEN1
36 .qp_hw_data = qat_gen1_qps,
37 /* gen2 has same ring layout as gen1 */
38 .comp_num_im_bufs_required = QAT_NUM_INTERM_BUFS_GEN2
42 .qp_hw_data = qat_gen3_qps,
43 .comp_num_im_bufs_required = QAT_NUM_INTERM_BUFS_GEN3
48 .comp_num_im_bufs_required = QAT_NUM_INTERM_BUFS_GEN3,
49 .pf2vf_dev = &qat_pf2vf_gen4
53 /* per-process array of device data */
54 struct qat_device_info qat_pci_devs[RTE_PMD_QAT_MAX_PCI_DEVICES];
55 static int qat_nb_pci_devices;
58 * The set of PCI devices this driver supports
61 static const struct rte_pci_id pci_id_qat_map[] = {
63 RTE_PCI_DEVICE(0x8086, 0x0443),
66 RTE_PCI_DEVICE(0x8086, 0x37c9),
69 RTE_PCI_DEVICE(0x8086, 0x19e3),
72 RTE_PCI_DEVICE(0x8086, 0x6f55),
75 RTE_PCI_DEVICE(0x8086, 0x18ef),
78 RTE_PCI_DEVICE(0x8086, 0x18a1),
81 RTE_PCI_DEVICE(0x8086, 0x4941),
86 static struct qat_pci_device *
87 qat_pci_get_named_dev(const char *name)
94 for (i = 0; i < RTE_PMD_QAT_MAX_PCI_DEVICES; i++) {
95 if (qat_pci_devs[i].mz &&
96 (strcmp(((struct qat_pci_device *)
97 qat_pci_devs[i].mz->addr)->name, name)
99 return (struct qat_pci_device *)
100 qat_pci_devs[i].mz->addr;
107 qat_pci_find_free_device_index(void)
111 for (dev_id = 0; dev_id < RTE_PMD_QAT_MAX_PCI_DEVICES;
113 if (qat_pci_devs[dev_id].mz == NULL)
119 struct qat_pci_device *
120 qat_get_qat_dev_from_pci_dev(struct rte_pci_device *pci_dev)
122 char name[QAT_DEV_NAME_MAX_LEN];
124 rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
126 return qat_pci_get_named_dev(name);
130 qat_gen4_reset_ring_pair(struct qat_pci_device *qat_pci_dev)
134 struct qat_pf2vf_msg pf2vf_msg;
136 pf2vf_msg.msg_type = ADF_VF2PF_MSGTYPE_RP_RESET;
137 pf2vf_msg.block_hdr = -1;
138 for (i = 0; i < QAT_GEN4_BUNDLE_NUM; i++) {
139 pf2vf_msg.msg_data = i;
140 ret = qat_pf2vf_exch_msg(qat_pci_dev, pf2vf_msg, 1, data);
142 QAT_LOG(ERR, "QAT error when reset bundle no %d",
151 static void qat_dev_parse_cmd(const char *str, struct qat_dev_cmd_param
158 char value_str[4] = { };
160 param = qat_dev_cmd_param[i].name;
164 const char *arg = strstr(str, param);
165 const char *arg2 = NULL;
168 arg2 = arg + strlen(param);
170 QAT_LOG(DEBUG, "parsing error '=' sign"
171 " should immediately follow %s",
177 QAT_LOG(DEBUG, "%s not provided", param);
182 if (!isdigit(*(arg2 + iter)))
187 QAT_LOG(DEBUG, "parsing error %s"
188 " no number provided",
191 memcpy(value_str, arg2, iter);
192 value = strtol(value_str, NULL, 10);
193 if (value > MAX_QP_THRESHOLD_SIZE) {
194 QAT_LOG(DEBUG, "Exceeded max size of"
195 " threshold, setting to %d",
196 MAX_QP_THRESHOLD_SIZE);
197 value = MAX_QP_THRESHOLD_SIZE;
199 QAT_LOG(DEBUG, "parsing %s = %ld",
203 qat_dev_cmd_param[i].val = value;
208 struct qat_pci_device *
209 qat_pci_device_allocate(struct rte_pci_device *pci_dev,
210 struct qat_dev_cmd_param *qat_dev_cmd_param)
212 struct qat_pci_device *qat_dev;
213 uint8_t qat_dev_id = 0;
214 char name[QAT_DEV_NAME_MAX_LEN];
215 struct rte_devargs *devargs = pci_dev->device.devargs;
217 rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
218 snprintf(name+strlen(name), QAT_DEV_NAME_MAX_LEN-strlen(name), "_qat");
220 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
221 const struct rte_memzone *mz = rte_memzone_lookup(name);
225 "Secondary can't find %s mz, did primary create device?",
230 qat_pci_devs[qat_dev->qat_dev_id].mz = mz;
231 qat_pci_devs[qat_dev->qat_dev_id].pci_dev = pci_dev;
232 qat_nb_pci_devices++;
233 QAT_LOG(DEBUG, "QAT device %d found, name %s, total QATs %d",
234 qat_dev->qat_dev_id, qat_dev->name, qat_nb_pci_devices);
238 if (qat_pci_get_named_dev(name) != NULL) {
239 QAT_LOG(ERR, "QAT device with name %s already allocated!",
244 qat_dev_id = qat_pci_find_free_device_index();
245 if (qat_dev_id == RTE_PMD_QAT_MAX_PCI_DEVICES) {
246 QAT_LOG(ERR, "Reached maximum number of QAT devices");
250 qat_pci_devs[qat_dev_id].mz = rte_memzone_reserve(name,
251 sizeof(struct qat_pci_device),
254 if (qat_pci_devs[qat_dev_id].mz == NULL) {
255 QAT_LOG(ERR, "Error when allocating memzone for QAT_%d",
260 qat_dev = qat_pci_devs[qat_dev_id].mz->addr;
261 memset(qat_dev, 0, sizeof(*qat_dev));
262 strlcpy(qat_dev->name, name, QAT_DEV_NAME_MAX_LEN);
263 qat_dev->qat_dev_id = qat_dev_id;
264 qat_pci_devs[qat_dev_id].pci_dev = pci_dev;
265 switch (pci_dev->id.device_id) {
267 qat_dev->qat_dev_gen = QAT_GEN1;
273 qat_dev->qat_dev_gen = QAT_GEN2;
276 qat_dev->qat_dev_gen = QAT_GEN3;
279 qat_dev->qat_dev_gen = QAT_GEN4;
282 QAT_LOG(ERR, "Invalid dev_id, can't determine generation");
283 rte_memzone_free(qat_pci_devs[qat_dev->qat_dev_id].mz);
287 if (qat_dev->qat_dev_gen == QAT_GEN4) {
288 qat_dev->misc_bar_io_addr = pci_dev->mem_resource[2].addr;
289 if (qat_dev->misc_bar_io_addr == NULL) {
290 QAT_LOG(ERR, "QAT cannot get access to VF misc bar");
295 if (devargs && devargs->drv_str)
296 qat_dev_parse_cmd(devargs->drv_str, qat_dev_cmd_param);
298 if (qat_dev->qat_dev_gen >= QAT_GEN4) {
299 int ret = qat_read_qp_config(qat_dev, qat_dev->qat_dev_gen);
303 "Cannot acquire ring configuration for QAT_%d",
309 rte_spinlock_init(&qat_dev->arb_csr_lock);
310 qat_nb_pci_devices++;
312 QAT_LOG(DEBUG, "QAT device %d found, name %s, total QATs %d",
313 qat_dev->qat_dev_id, qat_dev->name, qat_nb_pci_devices);
319 qat_pci_device_release(struct rte_pci_device *pci_dev)
321 struct qat_pci_device *qat_dev;
322 char name[QAT_DEV_NAME_MAX_LEN];
328 rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
329 snprintf(name+strlen(name), QAT_DEV_NAME_MAX_LEN-strlen(name), "_qat");
330 qat_dev = qat_pci_get_named_dev(name);
331 if (qat_dev != NULL) {
333 struct qat_device_info *inst =
334 &qat_pci_devs[qat_dev->qat_dev_id];
335 /* Check that there are no service devs still on pci device */
337 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
338 if (qat_dev->sym_dev != NULL) {
339 QAT_LOG(DEBUG, "QAT sym device %s is busy",
343 if (qat_dev->asym_dev != NULL) {
344 QAT_LOG(DEBUG, "QAT asym device %s is busy",
348 if (qat_dev->comp_dev != NULL) {
349 QAT_LOG(DEBUG, "QAT comp device %s is busy",
355 rte_memzone_free(inst->mz);
357 memset(inst, 0, sizeof(struct qat_device_info));
358 qat_nb_pci_devices--;
359 QAT_LOG(DEBUG, "QAT device %s released, total QATs %d",
360 name, qat_nb_pci_devices);
366 qat_pci_dev_destroy(struct qat_pci_device *qat_pci_dev,
367 struct rte_pci_device *pci_dev)
369 qat_sym_dev_destroy(qat_pci_dev);
370 qat_comp_dev_destroy(qat_pci_dev);
371 qat_asym_dev_destroy(qat_pci_dev);
372 return qat_pci_device_release(pci_dev);
375 static int qat_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
376 struct rte_pci_device *pci_dev)
378 int sym_ret = 0, asym_ret = 0, comp_ret = 0;
379 int num_pmds_created = 0;
380 struct qat_pci_device *qat_pci_dev;
381 struct qat_dev_cmd_param qat_dev_cmd_param[] = {
382 { SYM_ENQ_THRESHOLD_NAME, 0 },
383 { ASYM_ENQ_THRESHOLD_NAME, 0 },
384 { COMP_ENQ_THRESHOLD_NAME, 0 },
388 QAT_LOG(DEBUG, "Found QAT device at %02x:%02x.%x",
391 pci_dev->addr.function);
393 qat_pci_dev = qat_pci_device_allocate(pci_dev, qat_dev_cmd_param);
394 if (qat_pci_dev == NULL)
397 if (qat_pci_dev->qat_dev_gen == QAT_GEN4) {
398 if (qat_gen4_reset_ring_pair(qat_pci_dev)) {
400 "Cannot reset ring pairs, does pf driver supports pf2vf comms?"
406 sym_ret = qat_sym_dev_create(qat_pci_dev, qat_dev_cmd_param);
413 "Failed to create QAT SYM PMD on device %s",
416 comp_ret = qat_comp_dev_create(qat_pci_dev, qat_dev_cmd_param);
421 "Failed to create QAT COMP PMD on device %s",
424 asym_ret = qat_asym_dev_create(qat_pci_dev, qat_dev_cmd_param);
429 "Failed to create QAT ASYM PMD on device %s",
432 if (num_pmds_created == 0)
433 qat_pci_dev_destroy(qat_pci_dev, pci_dev);
438 static int qat_pci_remove(struct rte_pci_device *pci_dev)
440 struct qat_pci_device *qat_pci_dev;
445 qat_pci_dev = qat_get_qat_dev_from_pci_dev(pci_dev);
446 if (qat_pci_dev == NULL)
449 return qat_pci_dev_destroy(qat_pci_dev, pci_dev);
452 static struct rte_pci_driver rte_qat_pmd = {
453 .id_table = pci_id_qat_map,
454 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
455 .probe = qat_pci_probe,
456 .remove = qat_pci_remove
460 qat_sym_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused,
461 struct qat_dev_cmd_param *qat_dev_cmd_param __rte_unused)
467 qat_asym_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused,
468 struct qat_dev_cmd_param *qat_dev_cmd_param __rte_unused)
474 qat_sym_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
480 qat_asym_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
486 qat_comp_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused,
487 struct qat_dev_cmd_param *qat_dev_cmd_param __rte_unused)
493 qat_comp_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
498 RTE_PMD_REGISTER_PCI(QAT_PCI_NAME, rte_qat_pmd);
499 RTE_PMD_REGISTER_PCI_TABLE(QAT_PCI_NAME, pci_id_qat_map);
500 RTE_PMD_REGISTER_KMOD_DEP(QAT_PCI_NAME, "* igb_uio | uio_pci_generic | vfio-pci");