Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2018-2022 Intel Corporation
3 : : */
4 : :
5 : : #include <rte_string_fns.h>
6 : : #include <rte_devargs.h>
7 : : #include <ctype.h>
8 : :
9 : : #include "qat_device.h"
10 : : #include "adf_transport_access_macros.h"
11 : : #include "qat_sym.h"
12 : : #include "qat_comp_pmd.h"
13 : : #include "adf_pf2vf_msg.h"
14 : : #include "qat_pf2vf.h"
15 : :
16 : : #define NOT_NULL(arg, func, msg, ...) \
17 : : do { \
18 : : if (arg == NULL) { \
19 : : QAT_LOG(ERR, \
20 : : msg, ##__VA_ARGS__); \
21 : : func; \
22 : : } \
23 : : } while (0)
24 : :
25 : : /* Hardware device information per generation */
26 : : struct qat_gen_hw_data qat_gen_config[QAT_N_GENS];
27 : : struct qat_dev_hw_spec_funcs *qat_dev_hw_spec[QAT_N_GENS];
28 : :
29 : : /* per-process array of device data */
30 : : struct qat_device_info qat_pci_devs[RTE_PMD_QAT_MAX_PCI_DEVICES];
31 : : static int qat_nb_pci_devices;
32 : :
33 : : /*
34 : : * The set of PCI devices this driver supports
35 : : */
36 : :
37 : : static const struct rte_pci_id pci_id_qat_map[] = {
38 : : {
39 : : RTE_PCI_DEVICE(0x8086, 0x0443),
40 : : },
41 : : {
42 : : RTE_PCI_DEVICE(0x8086, 0x37c9),
43 : : },
44 : : {
45 : : RTE_PCI_DEVICE(0x8086, 0x19e3),
46 : : },
47 : : {
48 : : RTE_PCI_DEVICE(0x8086, 0x6f55),
49 : : },
50 : : {
51 : : RTE_PCI_DEVICE(0x8086, 0x18ef),
52 : : },
53 : : {
54 : : RTE_PCI_DEVICE(0x8086, 0x18a1),
55 : : },
56 : : {
57 : : RTE_PCI_DEVICE(0x8086, 0x4941),
58 : : },
59 : : {
60 : : RTE_PCI_DEVICE(0x8086, 0x4943),
61 : : },
62 : : {
63 : : RTE_PCI_DEVICE(0x8086, 0x4945),
64 : : },
65 : : {.device_id = 0},
66 : : };
67 : :
68 : : static int
69 : : qat_pci_get_extra_size(enum qat_device_gen qat_dev_gen)
70 : : {
71 : 0 : struct qat_dev_hw_spec_funcs *ops_hw =
72 : : qat_dev_hw_spec[qat_dev_gen];
73 [ # # ]: 0 : if (ops_hw->qat_dev_get_extra_size == NULL)
74 : : return -ENOTSUP;
75 : 0 : return ops_hw->qat_dev_get_extra_size();
76 : : }
77 : :
78 : : static struct qat_pci_device *
79 : 0 : qat_pci_get_named_dev(const char *name)
80 : : {
81 : : unsigned int i;
82 : :
83 [ # # ]: 0 : if (name == NULL)
84 : : return NULL;
85 : :
86 [ # # ]: 0 : for (i = 0; i < RTE_PMD_QAT_MAX_PCI_DEVICES; i++) {
87 [ # # ]: 0 : if (qat_pci_devs[i].mz &&
88 : 0 : (strcmp(((struct qat_pci_device *)
89 [ # # ]: 0 : qat_pci_devs[i].mz->addr)->name, name)
90 : : == 0))
91 : 0 : return (struct qat_pci_device *)
92 : : qat_pci_devs[i].mz->addr;
93 : : }
94 : :
95 : : return NULL;
96 : : }
97 : :
98 : : static uint8_t
99 : : qat_pci_find_free_device_index(void)
100 : : {
101 : : uint8_t dev_id;
102 : :
103 [ # # ]: 0 : for (dev_id = 0; dev_id < RTE_PMD_QAT_MAX_PCI_DEVICES;
104 : 0 : dev_id++) {
105 [ # # ]: 0 : if (qat_pci_devs[dev_id].mz == NULL)
106 : : break;
107 : : }
108 : : return dev_id;
109 : : }
110 : :
111 : : struct qat_pci_device *
112 : 0 : qat_get_qat_dev_from_pci_dev(struct rte_pci_device *pci_dev)
113 : : {
114 : : char name[QAT_DEV_NAME_MAX_LEN];
115 : :
116 : 0 : rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
117 : :
118 : 0 : return qat_pci_get_named_dev(name);
119 : : }
120 : :
121 : : static void
122 : 0 : qat_dev_parse_cmd(const char *str, struct qat_dev_cmd_param
123 : : *qat_dev_cmd_param)
124 : : {
125 : : int i = 0;
126 : : const char *param;
127 : :
128 : 0 : while (1) {
129 : 0 : char value_str[4] = { };
130 : :
131 : 0 : param = qat_dev_cmd_param[i].name;
132 [ # # ]: 0 : if (param == NULL)
133 : 0 : return;
134 : : long value = 0;
135 : 0 : const char *arg = strstr(str, param);
136 : : const char *arg2 = NULL;
137 : :
138 [ # # ]: 0 : if (arg) {
139 : 0 : arg2 = arg + strlen(param);
140 [ # # ]: 0 : if (*arg2 != '=') {
141 : 0 : QAT_LOG(DEBUG, "parsing error '=' sign"
142 : : " should immediately follow %s",
143 : : param);
144 : : arg2 = NULL;
145 : : } else
146 : 0 : arg2++;
147 : : } else {
148 : 0 : QAT_LOG(DEBUG, "%s not provided", param);
149 : : }
150 : : if (arg2) {
151 : : int iter = 0;
152 [ # # ]: 0 : while (iter < 2) {
153 [ # # ]: 0 : if (!isdigit(*(arg2 + iter)))
154 : : break;
155 : 0 : iter++;
156 : : }
157 [ # # ]: 0 : if (!iter) {
158 : 0 : QAT_LOG(DEBUG, "parsing error %s"
159 : : " no number provided",
160 : : param);
161 : : } else {
162 : 0 : memcpy(value_str, arg2, iter);
163 : 0 : value = strtol(value_str, NULL, 10);
164 [ # # ]: 0 : if (strcmp(param,
165 : : SYM_CIPHER_CRC_ENABLE_NAME) == 0) {
166 [ # # ]: 0 : if (value < 0 || value > 1) {
167 : 0 : QAT_LOG(DEBUG, "The value for qat_sym_cipher_crc_enable should be set to 0 or 1, setting to 0");
168 : : value = 0;
169 : : }
170 [ # # ]: 0 : } else if (value > MAX_QP_THRESHOLD_SIZE) {
171 : 0 : QAT_LOG(DEBUG, "Exceeded max size of"
172 : : " threshold, setting to %d",
173 : : MAX_QP_THRESHOLD_SIZE);
174 : : value = MAX_QP_THRESHOLD_SIZE;
175 : : }
176 : 0 : QAT_LOG(DEBUG, "parsing %s = %ld",
177 : : param, value);
178 : : }
179 : : }
180 : 0 : qat_dev_cmd_param[i].val = value;
181 : 0 : i++;
182 : : }
183 : : }
184 : :
185 : : static enum qat_device_gen
186 : 0 : pick_gen(const struct rte_pci_device *pci_dev)
187 : : {
188 [ # # # # : 0 : switch (pci_dev->id.device_id) {
# ]
189 : : case 0x0443:
190 : : return QAT_GEN1;
191 : 0 : case 0x37c9:
192 : : case 0x19e3:
193 : : case 0x6f55:
194 : : case 0x18ef:
195 : 0 : return QAT_GEN2;
196 : 0 : case 0x18a1:
197 : 0 : return QAT_GEN3;
198 : 0 : case 0x4941:
199 : : case 0x4943:
200 : : case 0x4945:
201 : 0 : return QAT_GEN4;
202 : 0 : default:
203 : 0 : QAT_LOG(ERR, "Invalid dev_id, can't determine generation");
204 : 0 : return QAT_N_GENS;
205 : : }
206 : : }
207 : :
208 : : struct qat_pci_device *
209 : 0 : qat_pci_device_allocate(struct rte_pci_device *pci_dev,
210 : : struct qat_dev_cmd_param *qat_dev_cmd_param)
211 : : {
212 : : struct qat_pci_device *qat_dev;
213 : : enum qat_device_gen qat_dev_gen;
214 : : uint8_t qat_dev_id = 0;
215 : : char name[QAT_DEV_NAME_MAX_LEN];
216 : 0 : struct rte_devargs *devargs = pci_dev->device.devargs;
217 : : struct qat_dev_hw_spec_funcs *ops_hw;
218 : : struct rte_mem_resource *mem_resource;
219 : : const struct rte_memzone *qat_dev_mz;
220 : : int qat_dev_size, extra_size;
221 : :
222 : 0 : rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
223 : 0 : snprintf(name+strlen(name), QAT_DEV_NAME_MAX_LEN-strlen(name), "_qat");
224 : :
225 : 0 : qat_dev_gen = pick_gen(pci_dev);
226 [ # # ]: 0 : if (qat_dev_gen == QAT_N_GENS) {
227 : 0 : QAT_LOG(ERR, "Invalid dev_id, can't determine generation");
228 : 0 : return NULL;
229 : : }
230 : :
231 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
232 : 0 : const struct rte_memzone *mz = rte_memzone_lookup(name);
233 : :
234 [ # # ]: 0 : if (mz == NULL) {
235 : 0 : QAT_LOG(ERR,
236 : : "Secondary can't find %s mz, did primary create device?",
237 : : name);
238 : 0 : return NULL;
239 : : }
240 : 0 : qat_dev = mz->addr;
241 : 0 : qat_pci_devs[qat_dev->qat_dev_id].mz = mz;
242 : 0 : qat_pci_devs[qat_dev->qat_dev_id].pci_dev = pci_dev;
243 : 0 : qat_nb_pci_devices++;
244 : 0 : QAT_LOG(DEBUG, "QAT device %d found, name %s, total QATs %d",
245 : : qat_dev->qat_dev_id, qat_dev->name, qat_nb_pci_devices);
246 : 0 : return qat_dev;
247 : : }
248 : :
249 [ # # ]: 0 : if (qat_pci_get_named_dev(name) != NULL) {
250 : 0 : QAT_LOG(ERR, "QAT device with name %s already allocated!",
251 : : name);
252 : 0 : return NULL;
253 : : }
254 : :
255 : : qat_dev_id = qat_pci_find_free_device_index();
256 [ # # ]: 0 : if (qat_dev_id == RTE_PMD_QAT_MAX_PCI_DEVICES) {
257 : 0 : QAT_LOG(ERR, "Reached maximum number of QAT devices");
258 : 0 : return NULL;
259 : : }
260 : :
261 : : extra_size = qat_pci_get_extra_size(qat_dev_gen);
262 [ # # ]: 0 : if (extra_size < 0) {
263 : 0 : QAT_LOG(ERR, "QAT internal error: no pci pointer for gen %d",
264 : : qat_dev_gen);
265 : 0 : return NULL;
266 : : }
267 : :
268 : 0 : qat_dev_size = sizeof(struct qat_pci_device) + extra_size;
269 : 0 : qat_dev_mz = rte_memzone_reserve(name, qat_dev_size,
270 : 0 : rte_socket_id(), 0);
271 : :
272 [ # # ]: 0 : if (qat_dev_mz == NULL) {
273 : 0 : QAT_LOG(ERR, "Error when allocating memzone for QAT_%d",
274 : : qat_dev_id);
275 : 0 : return NULL;
276 : : }
277 : :
278 [ # # ]: 0 : qat_dev = qat_dev_mz->addr;
279 : : memset(qat_dev, 0, qat_dev_size);
280 : 0 : qat_dev->dev_private = qat_dev + 1;
281 [ # # ]: 0 : strlcpy(qat_dev->name, name, QAT_DEV_NAME_MAX_LEN);
282 : 0 : qat_dev->qat_dev_id = qat_dev_id;
283 : 0 : qat_dev->qat_dev_gen = qat_dev_gen;
284 : :
285 : 0 : ops_hw = qat_dev_hw_spec[qat_dev->qat_dev_gen];
286 [ # # ]: 0 : NOT_NULL(ops_hw->qat_dev_get_misc_bar, goto error,
287 : : "QAT internal error! qat_dev_get_misc_bar function not set");
288 [ # # ]: 0 : if (ops_hw->qat_dev_get_misc_bar(&mem_resource, pci_dev) == 0) {
289 [ # # ]: 0 : if (mem_resource->addr == NULL) {
290 : 0 : QAT_LOG(ERR, "QAT cannot get access to VF misc bar");
291 : 0 : goto error;
292 : : }
293 : 0 : qat_dev->misc_bar_io_addr = mem_resource->addr;
294 : : } else
295 : 0 : qat_dev->misc_bar_io_addr = NULL;
296 : :
297 [ # # # # ]: 0 : if (devargs && devargs->drv_str)
298 : 0 : qat_dev_parse_cmd(devargs->drv_str, qat_dev_cmd_param);
299 : :
300 [ # # ]: 0 : if (qat_read_qp_config(qat_dev)) {
301 : 0 : QAT_LOG(ERR,
302 : : "Cannot acquire ring configuration for QAT_%d",
303 : : qat_dev_id);
304 : 0 : goto error;
305 : : }
306 [ # # ]: 0 : NOT_NULL(ops_hw->qat_dev_reset_ring_pairs, goto error,
307 : : "QAT internal error! Reset ring pairs function not set, gen : %d",
308 : : qat_dev_gen);
309 [ # # ]: 0 : if (ops_hw->qat_dev_reset_ring_pairs(qat_dev)) {
310 : 0 : QAT_LOG(ERR,
311 : : "Cannot reset ring pairs, does pf driver supports pf2vf comms?"
312 : : );
313 : 0 : goto error;
314 : : }
315 [ # # ]: 0 : NOT_NULL(ops_hw->qat_dev_get_slice_map, goto error,
316 : : "QAT internal error! Read slice function not set, gen : %d",
317 : : qat_dev_gen);
318 [ # # ]: 0 : if (ops_hw->qat_dev_get_slice_map(&qat_dev->slice_map, pci_dev) < 0) {
319 : 0 : RTE_LOG(ERR, EAL,
320 : : "Cannot read slice configuration\n");
321 : 0 : goto error;
322 : : }
323 : : rte_spinlock_init(&qat_dev->arb_csr_lock);
324 : :
325 : : /* No errors when allocating, attach memzone with
326 : : * qat_dev to list of devices
327 : : */
328 : 0 : qat_pci_devs[qat_dev_id].mz = qat_dev_mz;
329 : 0 : qat_pci_devs[qat_dev_id].pci_dev = pci_dev;
330 : 0 : qat_nb_pci_devices++;
331 : :
332 : 0 : QAT_LOG(DEBUG, "QAT device %d found, name %s, total QATs %d",
333 : : qat_dev->qat_dev_id, qat_dev->name, qat_nb_pci_devices);
334 : :
335 : 0 : return qat_dev;
336 : 0 : error:
337 [ # # ]: 0 : if (rte_memzone_free(qat_dev_mz)) {
338 : 0 : QAT_LOG(DEBUG,
339 : : "QAT internal error! Trying to free already allocated memzone: %s",
340 : : qat_dev_mz->name);
341 : : }
342 : : return NULL;
343 : : }
344 : :
345 : : static int
346 : 0 : qat_pci_device_release(struct rte_pci_device *pci_dev)
347 : : {
348 : : struct qat_pci_device *qat_dev;
349 : : char name[QAT_DEV_NAME_MAX_LEN];
350 : : int busy = 0;
351 : :
352 [ # # ]: 0 : if (pci_dev == NULL)
353 : : return -EINVAL;
354 : :
355 : 0 : rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
356 [ # # ]: 0 : snprintf(name+strlen(name), QAT_DEV_NAME_MAX_LEN-strlen(name), "_qat");
357 : 0 : qat_dev = qat_pci_get_named_dev(name);
358 [ # # ]: 0 : if (qat_dev != NULL) {
359 : :
360 : 0 : struct qat_device_info *inst =
361 : 0 : &qat_pci_devs[qat_dev->qat_dev_id];
362 : : /* Check that there are no service devs still on pci device */
363 : :
364 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
365 [ # # ]: 0 : if (qat_dev->sym_dev != NULL) {
366 : 0 : QAT_LOG(DEBUG, "QAT sym device %s is busy",
367 : : name);
368 : : busy = 1;
369 : : }
370 [ # # ]: 0 : if (qat_dev->asym_dev != NULL) {
371 : 0 : QAT_LOG(DEBUG, "QAT asym device %s is busy",
372 : : name);
373 : : busy = 1;
374 : : }
375 [ # # ]: 0 : if (qat_dev->comp_dev != NULL) {
376 : 0 : QAT_LOG(DEBUG, "QAT comp device %s is busy",
377 : : name);
378 : : busy = 1;
379 : : }
380 [ # # ]: 0 : if (busy)
381 : 0 : return -EBUSY;
382 : 0 : rte_memzone_free(inst->mz);
383 : : }
384 : : memset(inst, 0, sizeof(struct qat_device_info));
385 : 0 : qat_nb_pci_devices--;
386 : 0 : QAT_LOG(DEBUG, "QAT device %s released, total QATs %d",
387 : : name, qat_nb_pci_devices);
388 : : }
389 : : return 0;
390 : : }
391 : :
392 : : static int
393 : 0 : qat_pci_dev_destroy(struct qat_pci_device *qat_pci_dev,
394 : : struct rte_pci_device *pci_dev)
395 : : {
396 : 0 : qat_sym_dev_destroy(qat_pci_dev);
397 : 0 : qat_comp_dev_destroy(qat_pci_dev);
398 : 0 : qat_asym_dev_destroy(qat_pci_dev);
399 : 0 : return qat_pci_device_release(pci_dev);
400 : : }
401 : :
402 : 0 : static int qat_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
403 : : struct rte_pci_device *pci_dev)
404 : : {
405 : : int sym_ret = 0, asym_ret = 0, comp_ret = 0;
406 : : int num_pmds_created = 0;
407 : : struct qat_pci_device *qat_pci_dev;
408 : 0 : struct qat_dev_cmd_param qat_dev_cmd_param[] = {
409 : : { QAT_LEGACY_CAPA, 0 },
410 : : { SYM_ENQ_THRESHOLD_NAME, 0 },
411 : : { ASYM_ENQ_THRESHOLD_NAME, 0 },
412 : : { COMP_ENQ_THRESHOLD_NAME, 0 },
413 : : { SYM_CIPHER_CRC_ENABLE_NAME, 0 },
414 : : [QAT_CMD_SLICE_MAP_POS] = { QAT_CMD_SLICE_MAP, 0},
415 : : { NULL, 0 },
416 : : };
417 : :
418 : 0 : QAT_LOG(DEBUG, "Found QAT device at %02x:%02x.%x",
419 : : pci_dev->addr.bus,
420 : : pci_dev->addr.devid,
421 : : pci_dev->addr.function);
422 : :
423 : 0 : qat_pci_dev = qat_pci_device_allocate(pci_dev, qat_dev_cmd_param);
424 [ # # ]: 0 : if (qat_pci_dev == NULL)
425 : : return -ENODEV;
426 : :
427 : 0 : qat_dev_cmd_param[QAT_CMD_SLICE_MAP_POS].val = qat_pci_dev->slice_map;
428 : 0 : sym_ret = qat_sym_dev_create(qat_pci_dev, qat_dev_cmd_param);
429 [ # # ]: 0 : if (sym_ret == 0) {
430 : : num_pmds_created++;
431 : : }
432 : : else
433 : 0 : QAT_LOG(WARNING,
434 : : "Failed to create QAT SYM PMD on device %s",
435 : : qat_pci_dev->name);
436 : :
437 : 0 : comp_ret = qat_comp_dev_create(qat_pci_dev, qat_dev_cmd_param);
438 [ # # ]: 0 : if (comp_ret == 0)
439 : 0 : num_pmds_created++;
440 : : else
441 : 0 : QAT_LOG(WARNING,
442 : : "Failed to create QAT COMP PMD on device %s",
443 : : qat_pci_dev->name);
444 : :
445 : 0 : asym_ret = qat_asym_dev_create(qat_pci_dev, qat_dev_cmd_param);
446 [ # # ]: 0 : if (asym_ret == 0)
447 : 0 : num_pmds_created++;
448 : : else
449 : 0 : QAT_LOG(WARNING,
450 : : "Failed to create QAT ASYM PMD on device %s",
451 : : qat_pci_dev->name);
452 : :
453 [ # # ]: 0 : if (num_pmds_created == 0)
454 : 0 : qat_pci_dev_destroy(qat_pci_dev, pci_dev);
455 : :
456 : : return 0;
457 : : }
458 : :
459 : : static int
460 : 0 : qat_pci_remove(struct rte_pci_device *pci_dev)
461 : : {
462 : : struct qat_pci_device *qat_pci_dev;
463 : :
464 [ # # ]: 0 : if (pci_dev == NULL)
465 : : return -EINVAL;
466 : :
467 : 0 : qat_pci_dev = qat_get_qat_dev_from_pci_dev(pci_dev);
468 [ # # ]: 0 : if (qat_pci_dev == NULL)
469 : : return 0;
470 : :
471 : 0 : return qat_pci_dev_destroy(qat_pci_dev, pci_dev);
472 : : }
473 : :
474 : : static struct rte_pci_driver rte_qat_pmd = {
475 : : .id_table = pci_id_qat_map,
476 : : .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
477 : : .probe = qat_pci_probe,
478 : : .remove = qat_pci_remove
479 : : };
480 : :
481 : : __rte_weak int
482 : 0 : qat_sym_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused,
483 : : struct qat_dev_cmd_param *qat_dev_cmd_param __rte_unused)
484 : : {
485 : 0 : return 0;
486 : : }
487 : :
488 : : __rte_weak int
489 : 0 : qat_asym_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused,
490 : : const struct qat_dev_cmd_param *qat_dev_cmd_param __rte_unused)
491 : : {
492 : 0 : return 0;
493 : : }
494 : :
495 : : __rte_weak int
496 : 0 : qat_sym_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
497 : : {
498 : 0 : return 0;
499 : : }
500 : :
501 : : __rte_weak int
502 : 0 : qat_asym_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
503 : : {
504 : 0 : return 0;
505 : : }
506 : :
507 : : __rte_weak int
508 : 0 : qat_comp_dev_create(struct qat_pci_device *qat_pci_dev __rte_unused,
509 : : struct qat_dev_cmd_param *qat_dev_cmd_param __rte_unused)
510 : : {
511 : 0 : return 0;
512 : : }
513 : :
514 : : __rte_weak int
515 : 0 : qat_comp_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
516 : : {
517 : 0 : return 0;
518 : : }
519 : :
520 : 235 : RTE_PMD_REGISTER_PCI(QAT_PCI_NAME, rte_qat_pmd);
521 : : RTE_PMD_REGISTER_PCI_TABLE(QAT_PCI_NAME, pci_id_qat_map);
522 : : RTE_PMD_REGISTER_KMOD_DEP(QAT_PCI_NAME, "* igb_uio | uio_pci_generic | vfio-pci");
|