New upstream version 18.11-rc1
[deb_dpdk.git] / drivers / compress / qat / qat_comp_pmd.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2018 Intel Corporation
3  */
4
5 #include "qat_comp.h"
6 #include "qat_comp_pmd.h"
7
8 static const struct rte_compressdev_capabilities qat_comp_gen_capabilities[] = {
9         {/* COMPRESSION - deflate */
10          .algo = RTE_COMP_ALGO_DEFLATE,
11          .comp_feature_flags = RTE_COMP_FF_MULTI_PKT_CHECKSUM |
12                                 RTE_COMP_FF_CRC32_CHECKSUM |
13                                 RTE_COMP_FF_ADLER32_CHECKSUM |
14                                 RTE_COMP_FF_CRC32_ADLER32_CHECKSUM |
15                                 RTE_COMP_FF_SHAREABLE_PRIV_XFORM |
16                                 RTE_COMP_FF_HUFFMAN_FIXED |
17                                 RTE_COMP_FF_HUFFMAN_DYNAMIC |
18                                 RTE_COMP_FF_OOP_SGL_IN_SGL_OUT |
19                                 RTE_COMP_FF_OOP_SGL_IN_LB_OUT |
20                                 RTE_COMP_FF_OOP_LB_IN_SGL_OUT,
21          .window_size = {.min = 15, .max = 15, .increment = 0} },
22         {RTE_COMP_ALGO_LIST_END, 0, {0, 0, 0} } };
23
24 static void
25 qat_comp_stats_get(struct rte_compressdev *dev,
26                 struct rte_compressdev_stats *stats)
27 {
28         struct qat_common_stats qat_stats = {0};
29         struct qat_comp_dev_private *qat_priv;
30
31         if (stats == NULL || dev == NULL) {
32                 QAT_LOG(ERR, "invalid ptr: stats %p, dev %p", stats, dev);
33                 return;
34         }
35         qat_priv = dev->data->dev_private;
36
37         qat_stats_get(qat_priv->qat_dev, &qat_stats, QAT_SERVICE_COMPRESSION);
38         stats->enqueued_count = qat_stats.enqueued_count;
39         stats->dequeued_count = qat_stats.dequeued_count;
40         stats->enqueue_err_count = qat_stats.enqueue_err_count;
41         stats->dequeue_err_count = qat_stats.dequeue_err_count;
42 }
43
44 static void
45 qat_comp_stats_reset(struct rte_compressdev *dev)
46 {
47         struct qat_comp_dev_private *qat_priv;
48
49         if (dev == NULL) {
50                 QAT_LOG(ERR, "invalid compressdev ptr %p", dev);
51                 return;
52         }
53         qat_priv = dev->data->dev_private;
54
55         qat_stats_reset(qat_priv->qat_dev, QAT_SERVICE_COMPRESSION);
56
57 }
58
59 static int
60 qat_comp_qp_release(struct rte_compressdev *dev, uint16_t queue_pair_id)
61 {
62         struct qat_comp_dev_private *qat_private = dev->data->dev_private;
63
64         QAT_LOG(DEBUG, "Release comp qp %u on device %d",
65                                 queue_pair_id, dev->data->dev_id);
66
67         qat_private->qat_dev->qps_in_use[QAT_SERVICE_COMPRESSION][queue_pair_id]
68                                                 = NULL;
69
70         return qat_qp_release((struct qat_qp **)
71                         &(dev->data->queue_pairs[queue_pair_id]));
72 }
73
74 static int
75 qat_comp_qp_setup(struct rte_compressdev *dev, uint16_t qp_id,
76                   uint32_t max_inflight_ops, int socket_id)
77 {
78         struct qat_qp *qp;
79         int ret = 0;
80         uint32_t i;
81         struct qat_qp_config qat_qp_conf;
82
83         struct qat_qp **qp_addr =
84                         (struct qat_qp **)&(dev->data->queue_pairs[qp_id]);
85         struct qat_comp_dev_private *qat_private = dev->data->dev_private;
86         const struct qat_qp_hw_data *comp_hw_qps =
87                         qat_gen_config[qat_private->qat_dev->qat_dev_gen]
88                                       .qp_hw_data[QAT_SERVICE_COMPRESSION];
89         const struct qat_qp_hw_data *qp_hw_data = comp_hw_qps + qp_id;
90
91         /* If qp is already in use free ring memory and qp metadata. */
92         if (*qp_addr != NULL) {
93                 ret = qat_comp_qp_release(dev, qp_id);
94                 if (ret < 0)
95                         return ret;
96         }
97         if (qp_id >= qat_qps_per_service(comp_hw_qps,
98                                          QAT_SERVICE_COMPRESSION)) {
99                 QAT_LOG(ERR, "qp_id %u invalid for this device", qp_id);
100                 return -EINVAL;
101         }
102
103         qat_qp_conf.hw = qp_hw_data;
104         qat_qp_conf.build_request = qat_comp_build_request;
105         qat_qp_conf.cookie_size = sizeof(struct qat_comp_op_cookie);
106         qat_qp_conf.nb_descriptors = max_inflight_ops;
107         qat_qp_conf.socket_id = socket_id;
108         qat_qp_conf.service_str = "comp";
109
110         ret = qat_qp_setup(qat_private->qat_dev, qp_addr, qp_id, &qat_qp_conf);
111         if (ret != 0)
112                 return ret;
113
114         /* store a link to the qp in the qat_pci_device */
115         qat_private->qat_dev->qps_in_use[QAT_SERVICE_COMPRESSION][qp_id]
116                                                                 = *qp_addr;
117
118         qp = (struct qat_qp *)*qp_addr;
119
120         for (i = 0; i < qp->nb_descriptors; i++) {
121
122                 struct qat_comp_op_cookie *cookie =
123                                 qp->op_cookies[i];
124
125                 cookie->qat_sgl_src_phys_addr =
126                                 rte_mempool_virt2iova(cookie) +
127                                 offsetof(struct qat_comp_op_cookie,
128                                 qat_sgl_src);
129
130                 cookie->qat_sgl_dst_phys_addr =
131                                 rte_mempool_virt2iova(cookie) +
132                                 offsetof(struct qat_comp_op_cookie,
133                                 qat_sgl_dst);
134         }
135
136         return ret;
137 }
138
139
140 #define QAT_IM_BUFFER_DEBUG 0
141 static const struct rte_memzone *
142 qat_comp_setup_inter_buffers(struct qat_comp_dev_private *comp_dev,
143                               uint32_t buff_size)
144 {
145         char inter_buff_mz_name[RTE_MEMZONE_NAMESIZE];
146         const struct rte_memzone *memzone;
147         uint8_t *mz_start = NULL;
148         rte_iova_t mz_start_phys = 0;
149         struct array_of_ptrs *array_of_pointers;
150         int size_of_ptr_array;
151         uint32_t full_size;
152         uint32_t offset_of_sgls, offset_of_flat_buffs = 0;
153         int i;
154         int num_im_sgls = qat_gen_config[
155                 comp_dev->qat_dev->qat_dev_gen].comp_num_im_bufs_required;
156
157         QAT_LOG(DEBUG, "QAT COMP device %s needs %d sgls",
158                                 comp_dev->qat_dev->name, num_im_sgls);
159         snprintf(inter_buff_mz_name, RTE_MEMZONE_NAMESIZE,
160                                 "%s_inter_buff", comp_dev->qat_dev->name);
161         memzone = rte_memzone_lookup(inter_buff_mz_name);
162         if (memzone != NULL) {
163                 QAT_LOG(DEBUG, "QAT COMP im buffer memzone created already");
164                 return memzone;
165         }
166
167         /* Create a memzone to hold intermediate buffers and associated
168          * meta-data needed by the firmware. The memzone contains:
169          *  - a list of num_im_sgls physical pointers to sgls
170          *  - the num_im_sgl sgl structures, each pointing to 2 flat buffers
171          *  - the flat buffers: num_im_sgl * 2
172          * where num_im_sgls depends on the hardware generation of the device
173          */
174
175         size_of_ptr_array = num_im_sgls * sizeof(phys_addr_t);
176         offset_of_sgls = (size_of_ptr_array + (~QAT_64_BYTE_ALIGN_MASK))
177                         & QAT_64_BYTE_ALIGN_MASK;
178         offset_of_flat_buffs =
179             offset_of_sgls + num_im_sgls * sizeof(struct qat_inter_sgl);
180         full_size = offset_of_flat_buffs +
181                         num_im_sgls * buff_size * QAT_NUM_BUFS_IN_IM_SGL;
182
183         memzone = rte_memzone_reserve_aligned(inter_buff_mz_name, full_size,
184                         comp_dev->compressdev->data->socket_id,
185                         RTE_MEMZONE_2MB, QAT_64_BYTE_ALIGN);
186         if (memzone == NULL) {
187                 QAT_LOG(ERR, "Can't allocate intermediate buffers"
188                                 " for device %s", comp_dev->qat_dev->name);
189                 return NULL;
190         }
191
192         mz_start = (uint8_t *)memzone->addr;
193         mz_start_phys = memzone->phys_addr;
194         QAT_LOG(DEBUG, "Memzone %s: addr = %p, phys = 0x%"PRIx64
195                         ", size required %d, size created %zu",
196                         inter_buff_mz_name, mz_start, mz_start_phys,
197                         full_size, memzone->len);
198
199         array_of_pointers = (struct array_of_ptrs *)mz_start;
200         for (i = 0; i < num_im_sgls; i++) {
201                 uint32_t curr_sgl_offset =
202                     offset_of_sgls + i * sizeof(struct qat_inter_sgl);
203                 struct qat_inter_sgl *sgl =
204                     (struct qat_inter_sgl *)(mz_start + curr_sgl_offset);
205                 array_of_pointers->pointer[i] = mz_start_phys + curr_sgl_offset;
206
207                 sgl->num_bufs = QAT_NUM_BUFS_IN_IM_SGL;
208                 sgl->num_mapped_bufs = 0;
209                 sgl->resrvd = 0;
210                 sgl->buffers[0].addr = mz_start_phys + offset_of_flat_buffs +
211                         ((i * QAT_NUM_BUFS_IN_IM_SGL) * buff_size);
212                 sgl->buffers[0].len = buff_size;
213                 sgl->buffers[0].resrvd = 0;
214                 sgl->buffers[1].addr = mz_start_phys + offset_of_flat_buffs +
215                         (((i * QAT_NUM_BUFS_IN_IM_SGL) + 1) * buff_size);
216                 sgl->buffers[1].len = buff_size;
217                 sgl->buffers[1].resrvd = 0;
218
219 #if QAT_IM_BUFFER_DEBUG
220                 QAT_LOG(DEBUG, "  : phys addr of sgl[%i] in array_of_pointers"
221                             "= 0x%"PRIx64, i, array_of_pointers->pointer[i]);
222                 QAT_LOG(DEBUG, "  : virt address of sgl[%i] = %p", i, sgl);
223                 QAT_LOG(DEBUG, "  : sgl->buffers[0].addr = 0x%"PRIx64", len=%d",
224                         sgl->buffers[0].addr, sgl->buffers[0].len);
225                 QAT_LOG(DEBUG, "  : sgl->buffers[1].addr = 0x%"PRIx64", len=%d",
226                         sgl->buffers[1].addr, sgl->buffers[1].len);
227 #endif
228                 }
229 #if QAT_IM_BUFFER_DEBUG
230         QAT_DP_HEXDUMP_LOG(DEBUG,  "IM buffer memzone start:",
231                         mz_start, offset_of_flat_buffs + 32);
232 #endif
233         return memzone;
234 }
235
236 static struct rte_mempool *
237 qat_comp_create_xform_pool(struct qat_comp_dev_private *comp_dev,
238                               uint32_t num_elements)
239 {
240         char xform_pool_name[RTE_MEMPOOL_NAMESIZE];
241         struct rte_mempool *mp;
242
243         snprintf(xform_pool_name, RTE_MEMPOOL_NAMESIZE,
244                         "%s_xforms", comp_dev->qat_dev->name);
245
246         QAT_LOG(DEBUG, "xformpool: %s", xform_pool_name);
247         mp = rte_mempool_lookup(xform_pool_name);
248
249         if (mp != NULL) {
250                 QAT_LOG(DEBUG, "xformpool already created");
251                 if (mp->size != num_elements) {
252                         QAT_LOG(DEBUG, "xformpool wrong size - delete it");
253                         rte_mempool_free(mp);
254                         mp = NULL;
255                         comp_dev->xformpool = NULL;
256                 }
257         }
258
259         if (mp == NULL)
260                 mp = rte_mempool_create(xform_pool_name,
261                                 num_elements,
262                                 qat_comp_xform_size(), 0, 0,
263                                 NULL, NULL, NULL, NULL, rte_socket_id(),
264                                 0);
265         if (mp == NULL) {
266                 QAT_LOG(ERR, "Err creating mempool %s w %d elements of size %d",
267                         xform_pool_name, num_elements, qat_comp_xform_size());
268                 return NULL;
269         }
270
271         return mp;
272 }
273
274 static void
275 _qat_comp_dev_config_clear(struct qat_comp_dev_private *comp_dev)
276 {
277         /* Free intermediate buffers */
278         if (comp_dev->interm_buff_mz) {
279                 rte_memzone_free(comp_dev->interm_buff_mz);
280                 comp_dev->interm_buff_mz = NULL;
281         }
282
283         /* Free private_xform pool */
284         if (comp_dev->xformpool) {
285                 /* Free internal mempool for private xforms */
286                 rte_mempool_free(comp_dev->xformpool);
287                 comp_dev->xformpool = NULL;
288         }
289 }
290
291 static int
292 qat_comp_dev_config(struct rte_compressdev *dev,
293                 struct rte_compressdev_config *config)
294 {
295         struct qat_comp_dev_private *comp_dev = dev->data->dev_private;
296         int ret = 0;
297
298         if (config->max_nb_streams != 0) {
299                 QAT_LOG(ERR,
300         "QAT device does not support STATEFUL so max_nb_streams must be 0");
301                 return -EINVAL;
302         }
303
304         if (RTE_PMD_QAT_COMP_IM_BUFFER_SIZE == 0) {
305                 QAT_LOG(WARNING,
306                         "RTE_PMD_QAT_COMP_IM_BUFFER_SIZE = 0 in config file, so"
307                         " QAT device can't be used for Dynamic Deflate. "
308                         "Did you really intend to do this?");
309         } else {
310                 comp_dev->interm_buff_mz =
311                                 qat_comp_setup_inter_buffers(comp_dev,
312                                         RTE_PMD_QAT_COMP_IM_BUFFER_SIZE);
313                 if (comp_dev->interm_buff_mz == NULL) {
314                         ret = -ENOMEM;
315                         goto error_out;
316                 }
317         }
318
319         comp_dev->xformpool = qat_comp_create_xform_pool(comp_dev,
320                                         config->max_nb_priv_xforms);
321         if (comp_dev->xformpool == NULL) {
322
323                 ret = -ENOMEM;
324                 goto error_out;
325         }
326         return 0;
327
328 error_out:
329         _qat_comp_dev_config_clear(comp_dev);
330         return ret;
331 }
332
333 static int
334 qat_comp_dev_start(struct rte_compressdev *dev __rte_unused)
335 {
336         return 0;
337 }
338
339 static void
340 qat_comp_dev_stop(struct rte_compressdev *dev __rte_unused)
341 {
342
343 }
344
345 static int
346 qat_comp_dev_close(struct rte_compressdev *dev)
347 {
348         int i;
349         int ret = 0;
350         struct qat_comp_dev_private *comp_dev = dev->data->dev_private;
351
352         for (i = 0; i < dev->data->nb_queue_pairs; i++) {
353                 ret = qat_comp_qp_release(dev, i);
354                 if (ret < 0)
355                         return ret;
356         }
357
358         _qat_comp_dev_config_clear(comp_dev);
359
360         return ret;
361 }
362
363
364 static void
365 qat_comp_dev_info_get(struct rte_compressdev *dev,
366                         struct rte_compressdev_info *info)
367 {
368         struct qat_comp_dev_private *comp_dev = dev->data->dev_private;
369         const struct qat_qp_hw_data *comp_hw_qps =
370                 qat_gen_config[comp_dev->qat_dev->qat_dev_gen]
371                               .qp_hw_data[QAT_SERVICE_COMPRESSION];
372
373         if (info != NULL) {
374                 info->max_nb_queue_pairs =
375                         qat_qps_per_service(comp_hw_qps,
376                                             QAT_SERVICE_COMPRESSION);
377                 info->feature_flags = dev->feature_flags;
378                 info->capabilities = comp_dev->qat_dev_capabilities;
379         }
380 }
381
382 static uint16_t
383 qat_comp_pmd_enqueue_op_burst(void *qp, struct rte_comp_op **ops,
384                 uint16_t nb_ops)
385 {
386         return qat_enqueue_op_burst(qp, (void **)ops, nb_ops);
387 }
388
389 static uint16_t
390 qat_comp_pmd_dequeue_op_burst(void *qp, struct rte_comp_op **ops,
391                               uint16_t nb_ops)
392 {
393         return qat_dequeue_op_burst(qp, (void **)ops, nb_ops);
394 }
395
396 static uint16_t
397 qat_comp_pmd_enq_deq_dummy_op_burst(void *qp __rte_unused,
398                                     struct rte_comp_op **ops __rte_unused,
399                                     uint16_t nb_ops __rte_unused)
400 {
401         QAT_DP_LOG(ERR, "QAT PMD detected wrong FW version !");
402         return 0;
403 }
404
405 static struct rte_compressdev_ops compress_qat_dummy_ops = {
406
407         /* Device related operations */
408         .dev_configure          = NULL,
409         .dev_start              = NULL,
410         .dev_stop               = qat_comp_dev_stop,
411         .dev_close              = qat_comp_dev_close,
412         .dev_infos_get          = NULL,
413
414         .stats_get              = NULL,
415         .stats_reset            = qat_comp_stats_reset,
416         .queue_pair_setup       = NULL,
417         .queue_pair_release     = qat_comp_qp_release,
418
419         /* Compression related operations */
420         .private_xform_create   = NULL,
421         .private_xform_free     = qat_comp_private_xform_free
422 };
423
424 static uint16_t
425 qat_comp_pmd_dequeue_frst_op_burst(void *qp, struct rte_comp_op **ops,
426                                    uint16_t nb_ops)
427 {
428         uint16_t ret = qat_dequeue_op_burst(qp, (void **)ops, nb_ops);
429         struct qat_qp *tmp_qp = (struct qat_qp *)qp;
430
431         if (ret) {
432                 if ((*ops)->debug_status ==
433                                 (uint64_t)ERR_CODE_QAT_COMP_WRONG_FW) {
434                         tmp_qp->qat_dev->comp_dev->compressdev->enqueue_burst =
435                                         qat_comp_pmd_enq_deq_dummy_op_burst;
436                         tmp_qp->qat_dev->comp_dev->compressdev->dequeue_burst =
437                                         qat_comp_pmd_enq_deq_dummy_op_burst;
438
439                         tmp_qp->qat_dev->comp_dev->compressdev->dev_ops =
440                                         &compress_qat_dummy_ops;
441                         QAT_LOG(ERR, "QAT PMD detected wrong FW version !");
442
443                 } else {
444                         tmp_qp->qat_dev->comp_dev->compressdev->dequeue_burst =
445                                         qat_comp_pmd_dequeue_op_burst;
446                 }
447         }
448         return ret;
449 }
450
451 static struct rte_compressdev_ops compress_qat_ops = {
452
453         /* Device related operations */
454         .dev_configure          = qat_comp_dev_config,
455         .dev_start              = qat_comp_dev_start,
456         .dev_stop               = qat_comp_dev_stop,
457         .dev_close              = qat_comp_dev_close,
458         .dev_infos_get          = qat_comp_dev_info_get,
459
460         .stats_get              = qat_comp_stats_get,
461         .stats_reset            = qat_comp_stats_reset,
462         .queue_pair_setup       = qat_comp_qp_setup,
463         .queue_pair_release     = qat_comp_qp_release,
464
465         /* Compression related operations */
466         .private_xform_create   = qat_comp_private_xform_create,
467         .private_xform_free     = qat_comp_private_xform_free
468 };
469
470 /* An rte_driver is needed in the registration of the device with compressdev.
471  * The actual qat pci's rte_driver can't be used as its name represents
472  * the whole pci device with all services. Think of this as a holder for a name
473  * for the compression part of the pci device.
474  */
475 static const char qat_comp_drv_name[] = RTE_STR(COMPRESSDEV_NAME_QAT_PMD);
476 static const struct rte_driver compdev_qat_driver = {
477         .name = qat_comp_drv_name,
478         .alias = qat_comp_drv_name
479 };
480 int
481 qat_comp_dev_create(struct qat_pci_device *qat_pci_dev)
482 {
483         if (qat_pci_dev->qat_dev_gen == QAT_GEN1) {
484                 QAT_LOG(ERR, "Compression PMD not supported on QAT dh895xcc");
485                 return 0;
486         }
487         if (qat_pci_dev->qat_dev_gen == QAT_GEN3) {
488                 QAT_LOG(ERR, "Compression PMD not supported on QAT c4xxx");
489                 return 0;
490         }
491
492         struct rte_compressdev_pmd_init_params init_params = {
493                 .name = "",
494                 .socket_id = qat_pci_dev->pci_dev->device.numa_node,
495         };
496         char name[RTE_COMPRESSDEV_NAME_MAX_LEN];
497         struct rte_compressdev *compressdev;
498         struct qat_comp_dev_private *comp_dev;
499
500         snprintf(name, RTE_COMPRESSDEV_NAME_MAX_LEN, "%s_%s",
501                         qat_pci_dev->name, "comp");
502         QAT_LOG(DEBUG, "Creating QAT COMP device %s", name);
503
504         /* Populate subset device to use in compressdev device creation */
505         qat_pci_dev->comp_rte_dev.driver = &compdev_qat_driver;
506         qat_pci_dev->comp_rte_dev.numa_node =
507                                         qat_pci_dev->pci_dev->device.numa_node;
508         qat_pci_dev->comp_rte_dev.devargs = NULL;
509
510         compressdev = rte_compressdev_pmd_create(name,
511                         &(qat_pci_dev->comp_rte_dev),
512                         sizeof(struct qat_comp_dev_private),
513                         &init_params);
514
515         if (compressdev == NULL)
516                 return -ENODEV;
517
518         compressdev->dev_ops = &compress_qat_ops;
519
520         compressdev->enqueue_burst = qat_comp_pmd_enqueue_op_burst;
521         compressdev->dequeue_burst = qat_comp_pmd_dequeue_frst_op_burst;
522
523         compressdev->feature_flags = RTE_COMPDEV_FF_HW_ACCELERATED;
524
525         comp_dev = compressdev->data->dev_private;
526         comp_dev->qat_dev = qat_pci_dev;
527         comp_dev->compressdev = compressdev;
528         qat_pci_dev->comp_dev = comp_dev;
529
530         switch (qat_pci_dev->qat_dev_gen) {
531         case QAT_GEN1:
532         case QAT_GEN2:
533         case QAT_GEN3:
534                 comp_dev->qat_dev_capabilities = qat_comp_gen_capabilities;
535                 break;
536         default:
537                 comp_dev->qat_dev_capabilities = qat_comp_gen_capabilities;
538                 QAT_LOG(DEBUG,
539                         "QAT gen %d capabilities unknown, default to GEN1",
540                                         qat_pci_dev->qat_dev_gen);
541                 break;
542         }
543
544         QAT_LOG(DEBUG,
545                     "Created QAT COMP device %s as compressdev instance %d",
546                         name, compressdev->data->dev_id);
547         return 0;
548 }
549
550 int
551 qat_comp_dev_destroy(struct qat_pci_device *qat_pci_dev)
552 {
553         struct qat_comp_dev_private *comp_dev;
554
555         if (qat_pci_dev == NULL)
556                 return -ENODEV;
557
558         comp_dev = qat_pci_dev->comp_dev;
559         if (comp_dev == NULL)
560                 return 0;
561
562         /* clean up any resources used by the device */
563         qat_comp_dev_close(comp_dev->compressdev);
564
565         rte_compressdev_pmd_destroy(comp_dev->compressdev);
566         qat_pci_dev->comp_dev = NULL;
567
568         return 0;
569 }