New upstream version 18.11.2
[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 3 parts:
169          *  - a list of num_im_sgls physical pointers to sgls
170          *  - the num_im_sgl sgl structures, each pointing to
171          *    QAT_NUM_BUFS_IN_IM_SGL flat buffers
172          *  - the flat buffers: num_im_sgl * QAT_NUM_BUFS_IN_IM_SGL
173          *    buffers, each of buff_size
174          * num_im_sgls depends on the hardware generation of the device
175          * buff_size comes from the user via the config file
176          */
177
178         size_of_ptr_array = num_im_sgls * sizeof(phys_addr_t);
179         offset_of_sgls = (size_of_ptr_array + (~QAT_64_BYTE_ALIGN_MASK))
180                         & QAT_64_BYTE_ALIGN_MASK;
181         offset_of_flat_buffs =
182             offset_of_sgls + num_im_sgls * sizeof(struct qat_inter_sgl);
183         full_size = offset_of_flat_buffs +
184                         num_im_sgls * buff_size * QAT_NUM_BUFS_IN_IM_SGL;
185
186         memzone = rte_memzone_reserve_aligned(inter_buff_mz_name, full_size,
187                         comp_dev->compressdev->data->socket_id,
188                         RTE_MEMZONE_IOVA_CONTIG, QAT_64_BYTE_ALIGN);
189         if (memzone == NULL) {
190                 QAT_LOG(ERR, "Can't allocate intermediate buffers"
191                                 " for device %s", comp_dev->qat_dev->name);
192                 return NULL;
193         }
194
195         mz_start = (uint8_t *)memzone->addr;
196         mz_start_phys = memzone->phys_addr;
197         QAT_LOG(DEBUG, "Memzone %s: addr = %p, phys = 0x%"PRIx64
198                         ", size required %d, size created %zu",
199                         inter_buff_mz_name, mz_start, mz_start_phys,
200                         full_size, memzone->len);
201
202         array_of_pointers = (struct array_of_ptrs *)mz_start;
203         for (i = 0; i < num_im_sgls; i++) {
204                 uint32_t curr_sgl_offset =
205                     offset_of_sgls + i * sizeof(struct qat_inter_sgl);
206                 struct qat_inter_sgl *sgl =
207                     (struct qat_inter_sgl *)(mz_start + curr_sgl_offset);
208                 int lb;
209                 array_of_pointers->pointer[i] = mz_start_phys + curr_sgl_offset;
210
211                 sgl->num_bufs = QAT_NUM_BUFS_IN_IM_SGL;
212                 sgl->num_mapped_bufs = 0;
213                 sgl->resrvd = 0;
214
215 #if QAT_IM_BUFFER_DEBUG
216                 QAT_LOG(DEBUG, "  : phys addr of sgl[%i] in array_of_pointers"
217                         " = 0x%"PRIx64, i, array_of_pointers->pointer[i]);
218                 QAT_LOG(DEBUG, "  : virt address of sgl[%i] = %p", i, sgl);
219 #endif
220                 for (lb = 0; lb < QAT_NUM_BUFS_IN_IM_SGL; lb++) {
221                         sgl->buffers[lb].addr =
222                           mz_start_phys + offset_of_flat_buffs +
223                           (((i * QAT_NUM_BUFS_IN_IM_SGL) + lb) * buff_size);
224                         sgl->buffers[lb].len = buff_size;
225                         sgl->buffers[lb].resrvd = 0;
226 #if QAT_IM_BUFFER_DEBUG
227                         QAT_LOG(DEBUG,
228                           "  : sgl->buffers[%d].addr = 0x%"PRIx64", len=%d",
229                           lb, sgl->buffers[lb].addr, sgl->buffers[lb].len);
230 #endif
231                 }
232         }
233 #if QAT_IM_BUFFER_DEBUG
234         QAT_DP_HEXDUMP_LOG(DEBUG,  "IM buffer memzone start:",
235                         mz_start, offset_of_flat_buffs + 32);
236 #endif
237         return memzone;
238 }
239
240 static struct rte_mempool *
241 qat_comp_create_xform_pool(struct qat_comp_dev_private *comp_dev,
242                            struct rte_compressdev_config *config,
243                            uint32_t num_elements)
244 {
245         char xform_pool_name[RTE_MEMPOOL_NAMESIZE];
246         struct rte_mempool *mp;
247
248         snprintf(xform_pool_name, RTE_MEMPOOL_NAMESIZE,
249                         "%s_xforms", comp_dev->qat_dev->name);
250
251         QAT_LOG(DEBUG, "xformpool: %s", xform_pool_name);
252         mp = rte_mempool_lookup(xform_pool_name);
253
254         if (mp != NULL) {
255                 QAT_LOG(DEBUG, "xformpool already created");
256                 if (mp->size != num_elements) {
257                         QAT_LOG(DEBUG, "xformpool wrong size - delete it");
258                         rte_mempool_free(mp);
259                         mp = NULL;
260                         comp_dev->xformpool = NULL;
261                 }
262         }
263
264         if (mp == NULL)
265                 mp = rte_mempool_create(xform_pool_name,
266                                 num_elements,
267                                 qat_comp_xform_size(), 0, 0,
268                                 NULL, NULL, NULL, NULL, config->socket_id,
269                                 0);
270         if (mp == NULL) {
271                 QAT_LOG(ERR, "Err creating mempool %s w %d elements of size %d",
272                         xform_pool_name, num_elements, qat_comp_xform_size());
273                 return NULL;
274         }
275
276         return mp;
277 }
278
279 static void
280 _qat_comp_dev_config_clear(struct qat_comp_dev_private *comp_dev)
281 {
282         /* Free intermediate buffers */
283         if (comp_dev->interm_buff_mz) {
284                 rte_memzone_free(comp_dev->interm_buff_mz);
285                 comp_dev->interm_buff_mz = NULL;
286         }
287
288         /* Free private_xform pool */
289         if (comp_dev->xformpool) {
290                 /* Free internal mempool for private xforms */
291                 rte_mempool_free(comp_dev->xformpool);
292                 comp_dev->xformpool = NULL;
293         }
294 }
295
296 static int
297 qat_comp_dev_config(struct rte_compressdev *dev,
298                 struct rte_compressdev_config *config)
299 {
300         struct qat_comp_dev_private *comp_dev = dev->data->dev_private;
301         int ret = 0;
302
303         if (config->max_nb_streams != 0) {
304                 QAT_LOG(ERR,
305         "QAT device does not support STATEFUL so max_nb_streams must be 0");
306                 return -EINVAL;
307         }
308
309         if (RTE_PMD_QAT_COMP_IM_BUFFER_SIZE == 0) {
310                 QAT_LOG(WARNING,
311                         "RTE_PMD_QAT_COMP_IM_BUFFER_SIZE = 0 in config file, so"
312                         " QAT device can't be used for Dynamic Deflate. "
313                         "Did you really intend to do this?");
314         } else {
315                 comp_dev->interm_buff_mz =
316                                 qat_comp_setup_inter_buffers(comp_dev,
317                                         RTE_PMD_QAT_COMP_IM_BUFFER_SIZE);
318                 if (comp_dev->interm_buff_mz == NULL) {
319                         ret = -ENOMEM;
320                         goto error_out;
321                 }
322         }
323
324         comp_dev->xformpool = qat_comp_create_xform_pool(comp_dev, config,
325                                         config->max_nb_priv_xforms);
326         if (comp_dev->xformpool == NULL) {
327
328                 ret = -ENOMEM;
329                 goto error_out;
330         }
331         return 0;
332
333 error_out:
334         _qat_comp_dev_config_clear(comp_dev);
335         return ret;
336 }
337
338 static int
339 qat_comp_dev_start(struct rte_compressdev *dev __rte_unused)
340 {
341         return 0;
342 }
343
344 static void
345 qat_comp_dev_stop(struct rte_compressdev *dev __rte_unused)
346 {
347
348 }
349
350 static int
351 qat_comp_dev_close(struct rte_compressdev *dev)
352 {
353         int i;
354         int ret = 0;
355         struct qat_comp_dev_private *comp_dev = dev->data->dev_private;
356
357         for (i = 0; i < dev->data->nb_queue_pairs; i++) {
358                 ret = qat_comp_qp_release(dev, i);
359                 if (ret < 0)
360                         return ret;
361         }
362
363         _qat_comp_dev_config_clear(comp_dev);
364
365         return ret;
366 }
367
368
369 static void
370 qat_comp_dev_info_get(struct rte_compressdev *dev,
371                         struct rte_compressdev_info *info)
372 {
373         struct qat_comp_dev_private *comp_dev = dev->data->dev_private;
374         const struct qat_qp_hw_data *comp_hw_qps =
375                 qat_gen_config[comp_dev->qat_dev->qat_dev_gen]
376                               .qp_hw_data[QAT_SERVICE_COMPRESSION];
377
378         if (info != NULL) {
379                 info->max_nb_queue_pairs =
380                         qat_qps_per_service(comp_hw_qps,
381                                             QAT_SERVICE_COMPRESSION);
382                 info->feature_flags = dev->feature_flags;
383                 info->capabilities = comp_dev->qat_dev_capabilities;
384         }
385 }
386
387 static uint16_t
388 qat_comp_pmd_enqueue_op_burst(void *qp, struct rte_comp_op **ops,
389                 uint16_t nb_ops)
390 {
391         return qat_enqueue_op_burst(qp, (void **)ops, nb_ops);
392 }
393
394 static uint16_t
395 qat_comp_pmd_dequeue_op_burst(void *qp, struct rte_comp_op **ops,
396                               uint16_t nb_ops)
397 {
398         return qat_dequeue_op_burst(qp, (void **)ops, nb_ops);
399 }
400
401 static uint16_t
402 qat_comp_pmd_enq_deq_dummy_op_burst(void *qp __rte_unused,
403                                     struct rte_comp_op **ops __rte_unused,
404                                     uint16_t nb_ops __rte_unused)
405 {
406         QAT_DP_LOG(ERR, "QAT PMD detected wrong FW version !");
407         return 0;
408 }
409
410 static struct rte_compressdev_ops compress_qat_dummy_ops = {
411
412         /* Device related operations */
413         .dev_configure          = NULL,
414         .dev_start              = NULL,
415         .dev_stop               = qat_comp_dev_stop,
416         .dev_close              = qat_comp_dev_close,
417         .dev_infos_get          = NULL,
418
419         .stats_get              = NULL,
420         .stats_reset            = qat_comp_stats_reset,
421         .queue_pair_setup       = NULL,
422         .queue_pair_release     = qat_comp_qp_release,
423
424         /* Compression related operations */
425         .private_xform_create   = NULL,
426         .private_xform_free     = qat_comp_private_xform_free
427 };
428
429 static uint16_t
430 qat_comp_pmd_dequeue_frst_op_burst(void *qp, struct rte_comp_op **ops,
431                                    uint16_t nb_ops)
432 {
433         uint16_t ret = qat_dequeue_op_burst(qp, (void **)ops, nb_ops);
434         struct qat_qp *tmp_qp = (struct qat_qp *)qp;
435
436         if (ret) {
437                 if ((*ops)->debug_status ==
438                                 (uint64_t)ERR_CODE_QAT_COMP_WRONG_FW) {
439                         tmp_qp->qat_dev->comp_dev->compressdev->enqueue_burst =
440                                         qat_comp_pmd_enq_deq_dummy_op_burst;
441                         tmp_qp->qat_dev->comp_dev->compressdev->dequeue_burst =
442                                         qat_comp_pmd_enq_deq_dummy_op_burst;
443
444                         tmp_qp->qat_dev->comp_dev->compressdev->dev_ops =
445                                         &compress_qat_dummy_ops;
446                         QAT_LOG(ERR, "QAT PMD detected wrong FW version !");
447
448                 } else {
449                         tmp_qp->qat_dev->comp_dev->compressdev->dequeue_burst =
450                                         qat_comp_pmd_dequeue_op_burst;
451                 }
452         }
453         return ret;
454 }
455
456 static struct rte_compressdev_ops compress_qat_ops = {
457
458         /* Device related operations */
459         .dev_configure          = qat_comp_dev_config,
460         .dev_start              = qat_comp_dev_start,
461         .dev_stop               = qat_comp_dev_stop,
462         .dev_close              = qat_comp_dev_close,
463         .dev_infos_get          = qat_comp_dev_info_get,
464
465         .stats_get              = qat_comp_stats_get,
466         .stats_reset            = qat_comp_stats_reset,
467         .queue_pair_setup       = qat_comp_qp_setup,
468         .queue_pair_release     = qat_comp_qp_release,
469
470         /* Compression related operations */
471         .private_xform_create   = qat_comp_private_xform_create,
472         .private_xform_free     = qat_comp_private_xform_free
473 };
474
475 /* An rte_driver is needed in the registration of the device with compressdev.
476  * The actual qat pci's rte_driver can't be used as its name represents
477  * the whole pci device with all services. Think of this as a holder for a name
478  * for the compression part of the pci device.
479  */
480 static const char qat_comp_drv_name[] = RTE_STR(COMPRESSDEV_NAME_QAT_PMD);
481 static const struct rte_driver compdev_qat_driver = {
482         .name = qat_comp_drv_name,
483         .alias = qat_comp_drv_name
484 };
485 int
486 qat_comp_dev_create(struct qat_pci_device *qat_pci_dev)
487 {
488         if (qat_pci_dev->qat_dev_gen == QAT_GEN1) {
489                 QAT_LOG(ERR, "Compression PMD not supported on QAT dh895xcc");
490                 return 0;
491         }
492         if (qat_pci_dev->qat_dev_gen == QAT_GEN3) {
493                 QAT_LOG(ERR, "Compression PMD not supported on QAT c4xxx");
494                 return 0;
495         }
496
497         struct rte_compressdev_pmd_init_params init_params = {
498                 .name = "",
499                 .socket_id = qat_pci_dev->pci_dev->device.numa_node,
500         };
501         char name[RTE_COMPRESSDEV_NAME_MAX_LEN];
502         struct rte_compressdev *compressdev;
503         struct qat_comp_dev_private *comp_dev;
504
505         snprintf(name, RTE_COMPRESSDEV_NAME_MAX_LEN, "%s_%s",
506                         qat_pci_dev->name, "comp");
507         QAT_LOG(DEBUG, "Creating QAT COMP device %s", name);
508
509         /* Populate subset device to use in compressdev device creation */
510         qat_pci_dev->comp_rte_dev.driver = &compdev_qat_driver;
511         qat_pci_dev->comp_rte_dev.numa_node =
512                                         qat_pci_dev->pci_dev->device.numa_node;
513         qat_pci_dev->comp_rte_dev.devargs = NULL;
514
515         compressdev = rte_compressdev_pmd_create(name,
516                         &(qat_pci_dev->comp_rte_dev),
517                         sizeof(struct qat_comp_dev_private),
518                         &init_params);
519
520         if (compressdev == NULL)
521                 return -ENODEV;
522
523         compressdev->dev_ops = &compress_qat_ops;
524
525         compressdev->enqueue_burst = qat_comp_pmd_enqueue_op_burst;
526         compressdev->dequeue_burst = qat_comp_pmd_dequeue_frst_op_burst;
527
528         compressdev->feature_flags = RTE_COMPDEV_FF_HW_ACCELERATED;
529
530         comp_dev = compressdev->data->dev_private;
531         comp_dev->qat_dev = qat_pci_dev;
532         comp_dev->compressdev = compressdev;
533         qat_pci_dev->comp_dev = comp_dev;
534
535         switch (qat_pci_dev->qat_dev_gen) {
536         case QAT_GEN1:
537         case QAT_GEN2:
538         case QAT_GEN3:
539                 comp_dev->qat_dev_capabilities = qat_comp_gen_capabilities;
540                 break;
541         default:
542                 comp_dev->qat_dev_capabilities = qat_comp_gen_capabilities;
543                 QAT_LOG(DEBUG,
544                         "QAT gen %d capabilities unknown, default to GEN1",
545                                         qat_pci_dev->qat_dev_gen);
546                 break;
547         }
548
549         QAT_LOG(DEBUG,
550                     "Created QAT COMP device %s as compressdev instance %d",
551                         name, compressdev->data->dev_id);
552         return 0;
553 }
554
555 int
556 qat_comp_dev_destroy(struct qat_pci_device *qat_pci_dev)
557 {
558         struct qat_comp_dev_private *comp_dev;
559
560         if (qat_pci_dev == NULL)
561                 return -ENODEV;
562
563         comp_dev = qat_pci_dev->comp_dev;
564         if (comp_dev == NULL)
565                 return 0;
566
567         /* clean up any resources used by the device */
568         qat_comp_dev_close(comp_dev->compressdev);
569
570         rte_compressdev_pmd_destroy(comp_dev->compressdev);
571         qat_pci_dev->comp_dev = NULL;
572
573         return 0;
574 }