common/qat: reset ring pairs before setting GEN4
[dpdk.git] / drivers / common / qat / qat_device.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018-2020 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_pmd.h"
12 #include "qat_comp_pmd.h"
13 #include "adf_pf2vf_msg.h"
14 #include "qat_pf2vf.h"
15
16 /* pv2vf data Gen 4*/
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,
24 };
25
26 /* Hardware device information per generation */
27 __extension__
28 struct qat_gen_hw_data qat_gen_config[] =  {
29         [QAT_GEN1] = {
30                 .dev_gen = QAT_GEN1,
31                 .qp_hw_data = qat_gen1_qps,
32                 .comp_num_im_bufs_required = QAT_NUM_INTERM_BUFS_GEN1
33         },
34         [QAT_GEN2] = {
35                 .dev_gen = QAT_GEN2,
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
39         },
40         [QAT_GEN3] = {
41                 .dev_gen = QAT_GEN3,
42                 .qp_hw_data = qat_gen3_qps,
43                 .comp_num_im_bufs_required = QAT_NUM_INTERM_BUFS_GEN3
44         },
45         [QAT_GEN4] = {
46                 .dev_gen = QAT_GEN4,
47                 .qp_hw_data = NULL,
48                 .comp_num_im_bufs_required = QAT_NUM_INTERM_BUFS_GEN3,
49                 .pf2vf_dev = &qat_pf2vf_gen4
50         },
51 };
52
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;
56
57 /*
58  * The set of PCI devices this driver supports
59  */
60
61 static const struct rte_pci_id pci_id_qat_map[] = {
62                 {
63                         RTE_PCI_DEVICE(0x8086, 0x0443),
64                 },
65                 {
66                         RTE_PCI_DEVICE(0x8086, 0x37c9),
67                 },
68                 {
69                         RTE_PCI_DEVICE(0x8086, 0x19e3),
70                 },
71                 {
72                         RTE_PCI_DEVICE(0x8086, 0x6f55),
73                 },
74                 {
75                         RTE_PCI_DEVICE(0x8086, 0x18ef),
76                 },
77                 {
78                         RTE_PCI_DEVICE(0x8086, 0x18a1),
79                 },
80                 {
81                         RTE_PCI_DEVICE(0x8086, 0x4941),
82                 },
83                 {.device_id = 0},
84 };
85
86 static struct qat_pci_device *
87 qat_pci_get_named_dev(const char *name)
88 {
89         unsigned int i;
90
91         if (name == NULL)
92                 return NULL;
93
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)
98                                 == 0))
99                         return (struct qat_pci_device *)
100                                 qat_pci_devs[i].mz->addr;
101         }
102
103         return NULL;
104 }
105
106 static uint8_t
107 qat_pci_find_free_device_index(void)
108 {
109                 uint8_t dev_id;
110
111                 for (dev_id = 0; dev_id < RTE_PMD_QAT_MAX_PCI_DEVICES;
112                                 dev_id++) {
113                         if (qat_pci_devs[dev_id].mz == NULL)
114                                 break;
115                 }
116                 return dev_id;
117 }
118
119 struct qat_pci_device *
120 qat_get_qat_dev_from_pci_dev(struct rte_pci_device *pci_dev)
121 {
122         char name[QAT_DEV_NAME_MAX_LEN];
123
124         rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
125
126         return qat_pci_get_named_dev(name);
127 }
128
129 static int
130 qat_gen4_reset_ring_pair(struct qat_pci_device *qat_pci_dev)
131 {
132         int ret = 0, i;
133         uint8_t data[4];
134         struct qat_pf2vf_msg pf2vf_msg;
135
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);
141                 if (ret) {
142                         QAT_LOG(ERR, "QAT error when reset bundle no %d",
143                                 i);
144                         return ret;
145                 }
146         }
147
148         return 0;
149 }
150
151 static void qat_dev_parse_cmd(const char *str, struct qat_dev_cmd_param
152                 *qat_dev_cmd_param)
153 {
154         int i = 0;
155         const char *param;
156
157         while (1) {
158                 char value_str[4] = { };
159
160                 param = qat_dev_cmd_param[i].name;
161                 if (param == NULL)
162                         return;
163                 long value = 0;
164                 const char *arg = strstr(str, param);
165                 const char *arg2 = NULL;
166
167                 if (arg) {
168                         arg2 = arg + strlen(param);
169                         if (*arg2 != '=') {
170                                 QAT_LOG(DEBUG, "parsing error '=' sign"
171                                                 " should immediately follow %s",
172                                                 param);
173                                 arg2 = NULL;
174                         } else
175                                 arg2++;
176                 } else {
177                         QAT_LOG(DEBUG, "%s not provided", param);
178                 }
179                 if (arg2) {
180                         int iter = 0;
181                         while (iter < 2) {
182                                 if (!isdigit(*(arg2 + iter)))
183                                         break;
184                                 iter++;
185                         }
186                         if (!iter) {
187                                 QAT_LOG(DEBUG, "parsing error %s"
188                                                " no number provided",
189                                                param);
190                         } else {
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;
198                                 }
199                                 QAT_LOG(DEBUG, "parsing %s = %ld",
200                                                 param, value);
201                         }
202                 }
203                 qat_dev_cmd_param[i].val = value;
204                 i++;
205         }
206 }
207
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)
211 {
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;
216
217         rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
218         snprintf(name+strlen(name), QAT_DEV_NAME_MAX_LEN-strlen(name), "_qat");
219
220         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
221                 const struct rte_memzone *mz = rte_memzone_lookup(name);
222
223                 if (mz == NULL) {
224                         QAT_LOG(ERR,
225                                 "Secondary can't find %s mz, did primary create device?",
226                                 name);
227                         return NULL;
228                 }
229                 qat_dev = mz->addr;
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);
235                 return qat_dev;
236         }
237
238         if (qat_pci_get_named_dev(name) != NULL) {
239                 QAT_LOG(ERR, "QAT device with name %s already allocated!",
240                                 name);
241                 return NULL;
242         }
243
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");
247                 return NULL;
248         }
249
250         qat_pci_devs[qat_dev_id].mz = rte_memzone_reserve(name,
251                 sizeof(struct qat_pci_device),
252                 rte_socket_id(), 0);
253
254         if (qat_pci_devs[qat_dev_id].mz == NULL) {
255                 QAT_LOG(ERR, "Error when allocating memzone for QAT_%d",
256                         qat_dev_id);
257                 return NULL;
258         }
259
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) {
266         case 0x0443:
267                 qat_dev->qat_dev_gen = QAT_GEN1;
268                 break;
269         case 0x37c9:
270         case 0x19e3:
271         case 0x6f55:
272         case 0x18ef:
273                 qat_dev->qat_dev_gen = QAT_GEN2;
274                 break;
275         case 0x18a1:
276                 qat_dev->qat_dev_gen = QAT_GEN3;
277                 break;
278         case 0x4941:
279                 qat_dev->qat_dev_gen = QAT_GEN4;
280                 break;
281         default:
282                 QAT_LOG(ERR, "Invalid dev_id, can't determine generation");
283                 rte_memzone_free(qat_pci_devs[qat_dev->qat_dev_id].mz);
284                 return NULL;
285         }
286
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");
291                         return NULL;
292                 }
293         }
294
295         if (devargs && devargs->drv_str)
296                 qat_dev_parse_cmd(devargs->drv_str, qat_dev_cmd_param);
297
298         if (qat_dev->qat_dev_gen >= QAT_GEN4) {
299                 int ret = qat_read_qp_config(qat_dev, qat_dev->qat_dev_gen);
300
301                 if (ret) {
302                         QAT_LOG(ERR,
303                                 "Cannot acquire ring configuration for QAT_%d",
304                                 qat_dev_id);
305                         return NULL;
306                 }
307         }
308
309         rte_spinlock_init(&qat_dev->arb_csr_lock);
310         qat_nb_pci_devices++;
311
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);
314
315         return qat_dev;
316 }
317
318 static int
319 qat_pci_device_release(struct rte_pci_device *pci_dev)
320 {
321         struct qat_pci_device *qat_dev;
322         char name[QAT_DEV_NAME_MAX_LEN];
323         int busy = 0;
324
325         if (pci_dev == NULL)
326                 return -EINVAL;
327
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) {
332
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 */
336
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",
340                                         name);
341                                 busy = 1;
342                         }
343                         if (qat_dev->asym_dev != NULL) {
344                                 QAT_LOG(DEBUG, "QAT asym device %s is busy",
345                                         name);
346                                 busy = 1;
347                         }
348                         if (qat_dev->comp_dev != NULL) {
349                                 QAT_LOG(DEBUG, "QAT comp device %s is busy",
350                                         name);
351                                 busy = 1;
352                         }
353                         if (busy)
354                                 return -EBUSY;
355                         rte_memzone_free(inst->mz);
356                 }
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);
361         }
362         return 0;
363 }
364
365 static int
366 qat_pci_dev_destroy(struct qat_pci_device *qat_pci_dev,
367                 struct rte_pci_device *pci_dev)
368 {
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);
373 }
374
375 static int qat_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
376                 struct rte_pci_device *pci_dev)
377 {
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 },
385                         { NULL, 0 },
386         };
387
388         QAT_LOG(DEBUG, "Found QAT device at %02x:%02x.%x",
389                         pci_dev->addr.bus,
390                         pci_dev->addr.devid,
391                         pci_dev->addr.function);
392
393         qat_pci_dev = qat_pci_device_allocate(pci_dev, qat_dev_cmd_param);
394         if (qat_pci_dev == NULL)
395                 return -ENODEV;
396
397         if (qat_pci_dev->qat_dev_gen == QAT_GEN4) {
398                 if (qat_gen4_reset_ring_pair(qat_pci_dev)) {
399                         QAT_LOG(ERR,
400                                 "Cannot reset ring pairs, does pf driver supports pf2vf comms?"
401                                 );
402                         return -ENODEV;
403                 }
404         }
405
406         sym_ret = qat_sym_dev_create(qat_pci_dev, qat_dev_cmd_param);
407         if (sym_ret == 0) {
408                 num_pmds_created++;
409
410         }
411         else
412                 QAT_LOG(WARNING,
413                                 "Failed to create QAT SYM PMD on device %s",
414                                 qat_pci_dev->name);
415
416         comp_ret = qat_comp_dev_create(qat_pci_dev, qat_dev_cmd_param);
417         if (comp_ret == 0)
418                 num_pmds_created++;
419         else
420                 QAT_LOG(WARNING,
421                                 "Failed to create QAT COMP PMD on device %s",
422                                 qat_pci_dev->name);
423
424         asym_ret = qat_asym_dev_create(qat_pci_dev, qat_dev_cmd_param);
425         if (asym_ret == 0)
426                 num_pmds_created++;
427         else
428                 QAT_LOG(WARNING,
429                                 "Failed to create QAT ASYM PMD on device %s",
430                                 qat_pci_dev->name);
431
432         if (num_pmds_created == 0)
433                 qat_pci_dev_destroy(qat_pci_dev, pci_dev);
434
435         return 0;
436 }
437
438 static int qat_pci_remove(struct rte_pci_device *pci_dev)
439 {
440         struct qat_pci_device *qat_pci_dev;
441
442         if (pci_dev == NULL)
443                 return -EINVAL;
444
445         qat_pci_dev = qat_get_qat_dev_from_pci_dev(pci_dev);
446         if (qat_pci_dev == NULL)
447                 return 0;
448
449         return qat_pci_dev_destroy(qat_pci_dev, pci_dev);
450 }
451
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
457 };
458
459 __rte_weak int
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)
462 {
463         return 0;
464 }
465
466 __rte_weak int
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)
469 {
470         return 0;
471 }
472
473 __rte_weak int
474 qat_sym_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
475 {
476         return 0;
477 }
478
479 __rte_weak int
480 qat_asym_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
481 {
482         return 0;
483 }
484
485 __rte_weak int
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)
488 {
489         return 0;
490 }
491
492 __rte_weak int
493 qat_comp_dev_destroy(struct qat_pci_device *qat_pci_dev __rte_unused)
494 {
495         return 0;
496 }
497
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");