drivers/net: fix typo in vector Rx comment
[dpdk.git] / drivers / compress / qat / qat_comp_pmd.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2019 Intel Corporation
3  */
4
5 #include <rte_malloc.h>
6
7 #include "qat_comp.h"
8 #include "qat_comp_pmd.h"
9
10 #define QAT_PMD_COMP_SGL_DEF_SEGMENTS 16
11
12 struct stream_create_info {
13         struct qat_comp_dev_private *comp_dev;
14         int socket_id;
15         int error;
16 };
17
18 static const struct rte_compressdev_capabilities qat_comp_gen_capabilities[] = {
19         {/* COMPRESSION - deflate */
20          .algo = RTE_COMP_ALGO_DEFLATE,
21          .comp_feature_flags = RTE_COMP_FF_MULTI_PKT_CHECKSUM |
22                                 RTE_COMP_FF_CRC32_CHECKSUM |
23                                 RTE_COMP_FF_ADLER32_CHECKSUM |
24                                 RTE_COMP_FF_CRC32_ADLER32_CHECKSUM |
25                                 RTE_COMP_FF_SHAREABLE_PRIV_XFORM |
26                                 RTE_COMP_FF_HUFFMAN_FIXED |
27                                 RTE_COMP_FF_HUFFMAN_DYNAMIC |
28                                 RTE_COMP_FF_OOP_SGL_IN_SGL_OUT |
29                                 RTE_COMP_FF_OOP_SGL_IN_LB_OUT |
30                                 RTE_COMP_FF_OOP_LB_IN_SGL_OUT |
31                                 RTE_COMP_FF_STATEFUL_DECOMPRESSION,
32          .window_size = {.min = 15, .max = 15, .increment = 0} },
33         {RTE_COMP_ALGO_LIST_END, 0, {0, 0, 0} } };
34
35 static void
36 qat_comp_stats_get(struct rte_compressdev *dev,
37                 struct rte_compressdev_stats *stats)
38 {
39         struct qat_common_stats qat_stats = {0};
40         struct qat_comp_dev_private *qat_priv;
41
42         if (stats == NULL || dev == NULL) {
43                 QAT_LOG(ERR, "invalid ptr: stats %p, dev %p", stats, dev);
44                 return;
45         }
46         qat_priv = dev->data->dev_private;
47
48         qat_stats_get(qat_priv->qat_dev, &qat_stats, QAT_SERVICE_COMPRESSION);
49         stats->enqueued_count = qat_stats.enqueued_count;
50         stats->dequeued_count = qat_stats.dequeued_count;
51         stats->enqueue_err_count = qat_stats.enqueue_err_count;
52         stats->dequeue_err_count = qat_stats.dequeue_err_count;
53 }
54
55 static void
56 qat_comp_stats_reset(struct rte_compressdev *dev)
57 {
58         struct qat_comp_dev_private *qat_priv;
59
60         if (dev == NULL) {
61                 QAT_LOG(ERR, "invalid compressdev ptr %p", dev);
62                 return;
63         }
64         qat_priv = dev->data->dev_private;
65
66         qat_stats_reset(qat_priv->qat_dev, QAT_SERVICE_COMPRESSION);
67
68 }
69
70 static int
71 qat_comp_qp_release(struct rte_compressdev *dev, uint16_t queue_pair_id)
72 {
73         struct qat_comp_dev_private *qat_private = dev->data->dev_private;
74         struct qat_qp **qp_addr =
75                 (struct qat_qp **)&(dev->data->queue_pairs[queue_pair_id]);
76         struct qat_qp *qp = (struct qat_qp *)*qp_addr;
77         enum qat_device_gen qat_dev_gen = qat_private->qat_dev->qat_dev_gen;
78         uint32_t i;
79
80         QAT_LOG(DEBUG, "Release comp qp %u on device %d",
81                                 queue_pair_id, dev->data->dev_id);
82
83         qat_private->qat_dev->qps_in_use[QAT_SERVICE_COMPRESSION][queue_pair_id]
84                                                 = NULL;
85
86         if (qp != NULL)
87                 for (i = 0; i < qp->nb_descriptors; i++) {
88                         struct qat_comp_op_cookie *cookie = qp->op_cookies[i];
89
90                         rte_free(cookie->qat_sgl_src_d);
91                         rte_free(cookie->qat_sgl_dst_d);
92                 }
93
94         return qat_qp_release(qat_dev_gen, (struct qat_qp **)
95                         &(dev->data->queue_pairs[queue_pair_id]));
96 }
97
98 static int
99 qat_comp_qp_setup(struct rte_compressdev *dev, uint16_t qp_id,
100                   uint32_t max_inflight_ops, int socket_id)
101 {
102         struct qat_qp *qp;
103         int ret = 0;
104         uint32_t i;
105         struct qat_qp_config qat_qp_conf;
106
107         struct qat_qp **qp_addr =
108                         (struct qat_qp **)&(dev->data->queue_pairs[qp_id]);
109         struct qat_comp_dev_private *qat_private = dev->data->dev_private;
110         struct qat_pci_device *qat_dev = qat_private->qat_dev;
111         const struct qat_qp_hw_data *comp_hw_qps =
112                         qat_gen_config[qat_private->qat_dev->qat_dev_gen]
113                                       .qp_hw_data[QAT_SERVICE_COMPRESSION];
114         const struct qat_qp_hw_data *qp_hw_data = comp_hw_qps + qp_id;
115
116         /* If qp is already in use free ring memory and qp metadata. */
117         if (*qp_addr != NULL) {
118                 ret = qat_comp_qp_release(dev, qp_id);
119                 if (ret < 0)
120                         return ret;
121         }
122         if (qp_id >= qat_qps_per_service(qat_dev,
123                                          QAT_SERVICE_COMPRESSION)) {
124                 QAT_LOG(ERR, "qp_id %u invalid for this device", qp_id);
125                 return -EINVAL;
126         }
127
128         qat_qp_conf.hw = qp_hw_data;
129         qat_qp_conf.cookie_size = sizeof(struct qat_comp_op_cookie);
130         qat_qp_conf.nb_descriptors = max_inflight_ops;
131         qat_qp_conf.socket_id = socket_id;
132         qat_qp_conf.service_str = "comp";
133
134         ret = qat_qp_setup(qat_private->qat_dev, qp_addr, qp_id, &qat_qp_conf);
135         if (ret != 0)
136                 return ret;
137
138         /* store a link to the qp in the qat_pci_device */
139         qat_private->qat_dev->qps_in_use[QAT_SERVICE_COMPRESSION][qp_id]
140                                                                 = *qp_addr;
141
142         qp = (struct qat_qp *)*qp_addr;
143         qp->min_enq_burst_threshold = qat_private->min_enq_burst_threshold;
144
145         for (i = 0; i < qp->nb_descriptors; i++) {
146
147                 struct qat_comp_op_cookie *cookie =
148                                 qp->op_cookies[i];
149
150                 cookie->qp = qp;
151                 cookie->cookie_index = i;
152
153                 cookie->qat_sgl_src_d = rte_zmalloc_socket(NULL,
154                                         sizeof(struct qat_sgl) +
155                                         sizeof(struct qat_flat_buf) *
156                                         QAT_PMD_COMP_SGL_DEF_SEGMENTS,
157                                         64, dev->data->socket_id);
158
159                 cookie->qat_sgl_dst_d = rte_zmalloc_socket(NULL,
160                                         sizeof(struct qat_sgl) +
161                                         sizeof(struct qat_flat_buf) *
162                                         QAT_PMD_COMP_SGL_DEF_SEGMENTS,
163                                         64, dev->data->socket_id);
164
165                 if (cookie->qat_sgl_src_d == NULL ||
166                                 cookie->qat_sgl_dst_d == NULL) {
167                         QAT_LOG(ERR, "Can't allocate SGL"
168                                      " for device %s",
169                                      qat_private->qat_dev->name);
170                         return -ENOMEM;
171                 }
172
173                 cookie->qat_sgl_src_phys_addr =
174                                 rte_malloc_virt2iova(cookie->qat_sgl_src_d);
175
176                 cookie->qat_sgl_dst_phys_addr =
177                                 rte_malloc_virt2iova(cookie->qat_sgl_dst_d);
178
179                 cookie->dst_nb_elems = cookie->src_nb_elems =
180                                 QAT_PMD_COMP_SGL_DEF_SEGMENTS;
181
182                 cookie->socket_id = dev->data->socket_id;
183
184                 cookie->error = 0;
185         }
186
187         return ret;
188 }
189
190
191 #define QAT_IM_BUFFER_DEBUG 0
192 static const struct rte_memzone *
193 qat_comp_setup_inter_buffers(struct qat_comp_dev_private *comp_dev,
194                               uint32_t buff_size)
195 {
196         char inter_buff_mz_name[RTE_MEMZONE_NAMESIZE];
197         const struct rte_memzone *memzone;
198         uint8_t *mz_start = NULL;
199         rte_iova_t mz_start_phys = 0;
200         struct array_of_ptrs *array_of_pointers;
201         int size_of_ptr_array;
202         uint32_t full_size;
203         uint32_t offset_of_flat_buffs;
204         int i;
205         int num_im_sgls = qat_gen_config[
206                 comp_dev->qat_dev->qat_dev_gen].comp_num_im_bufs_required;
207
208         QAT_LOG(DEBUG, "QAT COMP device %s needs %d sgls",
209                                 comp_dev->qat_dev->name, num_im_sgls);
210         snprintf(inter_buff_mz_name, RTE_MEMZONE_NAMESIZE,
211                                 "%s_inter_buff", comp_dev->qat_dev->name);
212         memzone = rte_memzone_lookup(inter_buff_mz_name);
213         if (memzone != NULL) {
214                 QAT_LOG(DEBUG, "QAT COMP im buffer memzone created already");
215                 return memzone;
216         }
217
218         /* Create multiple memzones to hold intermediate buffers and associated
219          * meta-data needed by the firmware.
220          * The first memzone contains:
221          *  - a list of num_im_sgls physical pointers to sgls
222          * All other memzones contain:
223          *  - the sgl structure, pointing to QAT_NUM_BUFS_IN_IM_SGL flat buffers
224          *  - the flat buffers: QAT_NUM_BUFS_IN_IM_SGL buffers,
225          *    each of buff_size
226          * num_im_sgls depends on the hardware generation of the device
227          * buff_size comes from the user via the config file
228          */
229
230         size_of_ptr_array = num_im_sgls * sizeof(phys_addr_t);
231         offset_of_flat_buffs = sizeof(struct qat_inter_sgl);
232         full_size = offset_of_flat_buffs +
233                         buff_size * QAT_NUM_BUFS_IN_IM_SGL;
234
235         memzone = rte_memzone_reserve_aligned(inter_buff_mz_name,
236                         size_of_ptr_array,
237                         comp_dev->compressdev->data->socket_id,
238                         RTE_MEMZONE_IOVA_CONTIG, QAT_64_BYTE_ALIGN);
239         if (memzone == NULL) {
240                 QAT_LOG(ERR,
241                                 "Can't allocate intermediate buffers for device %s",
242                                 comp_dev->qat_dev->name);
243                 return NULL;
244         }
245
246         mz_start = (uint8_t *)memzone->addr;
247         mz_start_phys = memzone->iova;
248         QAT_LOG(DEBUG, "Memzone %s: addr = %p, phys = 0x%"PRIx64
249                         ", size required %d, size created %zu",
250                         inter_buff_mz_name, mz_start, mz_start_phys,
251                         size_of_ptr_array, memzone->len);
252
253         array_of_pointers = (struct array_of_ptrs *)mz_start;
254         for (i = 0; i < num_im_sgls; i++) {
255                 const struct rte_memzone *mz;
256                 struct qat_inter_sgl *sgl;
257                 int lb;
258
259                 snprintf(inter_buff_mz_name, RTE_MEMZONE_NAMESIZE,
260                                 "%s_inter_buff_%d", comp_dev->qat_dev->name, i);
261                 mz = rte_memzone_lookup(inter_buff_mz_name);
262                 if (mz == NULL) {
263                         mz = rte_memzone_reserve_aligned(inter_buff_mz_name,
264                                         full_size,
265                                         comp_dev->compressdev->data->socket_id,
266                                         RTE_MEMZONE_IOVA_CONTIG,
267                                         QAT_64_BYTE_ALIGN);
268                         if (mz == NULL) {
269                                 QAT_LOG(ERR,
270                                                 "Can't allocate intermediate buffers for device %s",
271                                                 comp_dev->qat_dev->name);
272                                 while (--i >= 0) {
273                                         snprintf(inter_buff_mz_name,
274                                                         RTE_MEMZONE_NAMESIZE,
275                                                         "%s_inter_buff_%d",
276                                                         comp_dev->qat_dev->name,
277                                                         i);
278                                         rte_memzone_free(
279                                                         rte_memzone_lookup(
280                                                         inter_buff_mz_name));
281                                 }
282                                 rte_memzone_free(memzone);
283                                 return NULL;
284                         }
285                 }
286
287                 QAT_LOG(DEBUG, "Memzone %s: addr = %p, phys = 0x%"PRIx64
288                                 ", size required %d, size created %zu",
289                                 inter_buff_mz_name, mz->addr, mz->iova,
290                                 full_size, mz->len);
291
292                 array_of_pointers->pointer[i] = mz->iova;
293
294                 sgl = (struct qat_inter_sgl *) mz->addr;
295                 sgl->num_bufs = QAT_NUM_BUFS_IN_IM_SGL;
296                 sgl->num_mapped_bufs = 0;
297                 sgl->resrvd = 0;
298
299 #if QAT_IM_BUFFER_DEBUG
300                 QAT_LOG(DEBUG, "  : phys addr of sgl[%i] in array_of_pointers"
301                         " = 0x%"PRIx64, i, array_of_pointers->pointer[i]);
302                 QAT_LOG(DEBUG, "  : virt address of sgl[%i] = %p", i, sgl);
303 #endif
304                 for (lb = 0; lb < QAT_NUM_BUFS_IN_IM_SGL; lb++) {
305                         sgl->buffers[lb].addr =
306                                         mz->iova + offset_of_flat_buffs +
307                                         lb * buff_size;
308                         sgl->buffers[lb].len = buff_size;
309                         sgl->buffers[lb].resrvd = 0;
310 #if QAT_IM_BUFFER_DEBUG
311                         QAT_LOG(DEBUG,
312                           "  : sgl->buffers[%d].addr = 0x%"PRIx64", len=%d",
313                           lb, sgl->buffers[lb].addr, sgl->buffers[lb].len);
314 #endif
315                 }
316         }
317 #if QAT_IM_BUFFER_DEBUG
318         QAT_DP_HEXDUMP_LOG(DEBUG,  "IM buffer memzone start:",
319                         memzone->addr, size_of_ptr_array);
320 #endif
321         return memzone;
322 }
323
324 static struct rte_mempool *
325 qat_comp_create_xform_pool(struct qat_comp_dev_private *comp_dev,
326                            struct rte_compressdev_config *config,
327                            uint32_t num_elements)
328 {
329         char xform_pool_name[RTE_MEMPOOL_NAMESIZE];
330         struct rte_mempool *mp;
331
332         snprintf(xform_pool_name, RTE_MEMPOOL_NAMESIZE,
333                         "%s_xforms", comp_dev->qat_dev->name);
334
335         QAT_LOG(DEBUG, "xformpool: %s", xform_pool_name);
336         mp = rte_mempool_lookup(xform_pool_name);
337
338         if (mp != NULL) {
339                 QAT_LOG(DEBUG, "xformpool already created");
340                 if (mp->size != num_elements) {
341                         QAT_LOG(DEBUG, "xformpool wrong size - delete it");
342                         rte_mempool_free(mp);
343                         mp = NULL;
344                         comp_dev->xformpool = NULL;
345                 }
346         }
347
348         if (mp == NULL)
349                 mp = rte_mempool_create(xform_pool_name,
350                                 num_elements,
351                                 qat_comp_xform_size(), 0, 0,
352                                 NULL, NULL, NULL, NULL, config->socket_id,
353                                 0);
354         if (mp == NULL) {
355                 QAT_LOG(ERR, "Err creating mempool %s w %d elements of size %d",
356                         xform_pool_name, num_elements, qat_comp_xform_size());
357                 return NULL;
358         }
359
360         return mp;
361 }
362
363 static void
364 qat_comp_stream_init(struct rte_mempool *mp __rte_unused, void *opaque,
365                      void *obj, unsigned int obj_idx)
366 {
367         struct stream_create_info *info = opaque;
368         struct qat_comp_stream *stream = obj;
369         char mz_name[RTE_MEMZONE_NAMESIZE];
370         const struct rte_memzone *memzone;
371         struct qat_inter_sgl *ram_banks_desc;
372
373         /* find a memzone for RAM banks */
374         snprintf(mz_name, RTE_MEMZONE_NAMESIZE, "%s_%u_rambanks",
375                  info->comp_dev->qat_dev->name, obj_idx);
376         memzone = rte_memzone_lookup(mz_name);
377         if (memzone == NULL) {
378                 /* allocate a memzone for compression state and RAM banks */
379                 memzone = rte_memzone_reserve_aligned(mz_name,
380                         QAT_STATE_REGISTERS_MAX_SIZE
381                                 + sizeof(struct qat_inter_sgl)
382                                 + QAT_INFLATE_CONTEXT_SIZE,
383                         info->socket_id,
384                         RTE_MEMZONE_IOVA_CONTIG, QAT_64_BYTE_ALIGN);
385                 if (memzone == NULL) {
386                         QAT_LOG(ERR,
387                             "Can't allocate RAM banks for device %s, object %u",
388                                 info->comp_dev->qat_dev->name, obj_idx);
389                         info->error = -ENOMEM;
390                         return;
391                 }
392         }
393
394         /* prepare the buffer list descriptor for RAM banks */
395         ram_banks_desc = (struct qat_inter_sgl *)
396                 (((uint8_t *) memzone->addr) + QAT_STATE_REGISTERS_MAX_SIZE);
397         ram_banks_desc->num_bufs = 1;
398         ram_banks_desc->buffers[0].len = QAT_INFLATE_CONTEXT_SIZE;
399         ram_banks_desc->buffers[0].addr = memzone->iova
400                         + QAT_STATE_REGISTERS_MAX_SIZE
401                         + sizeof(struct qat_inter_sgl);
402
403         memset(stream, 0, qat_comp_stream_size());
404         stream->memzone = memzone;
405         stream->state_registers_decomp = memzone->addr;
406         stream->state_registers_decomp_phys = memzone->iova;
407         stream->inflate_context = ((uint8_t *) memzone->addr)
408                         + QAT_STATE_REGISTERS_MAX_SIZE;
409         stream->inflate_context_phys = memzone->iova
410                         + QAT_STATE_REGISTERS_MAX_SIZE;
411 }
412
413 static void
414 qat_comp_stream_destroy(struct rte_mempool *mp __rte_unused,
415                         void *opaque __rte_unused, void *obj,
416                         unsigned obj_idx __rte_unused)
417 {
418         struct qat_comp_stream *stream = obj;
419
420         rte_memzone_free(stream->memzone);
421 }
422
423 static struct rte_mempool *
424 qat_comp_create_stream_pool(struct qat_comp_dev_private *comp_dev,
425                             int socket_id,
426                             uint32_t num_elements)
427 {
428         char stream_pool_name[RTE_MEMPOOL_NAMESIZE];
429         struct rte_mempool *mp;
430
431         snprintf(stream_pool_name, RTE_MEMPOOL_NAMESIZE,
432                  "%s_streams", comp_dev->qat_dev->name);
433
434         QAT_LOG(DEBUG, "streampool: %s", stream_pool_name);
435         mp = rte_mempool_lookup(stream_pool_name);
436
437         if (mp != NULL) {
438                 QAT_LOG(DEBUG, "streampool already created");
439                 if (mp->size != num_elements) {
440                         QAT_LOG(DEBUG, "streampool wrong size - delete it");
441                         rte_mempool_obj_iter(mp, qat_comp_stream_destroy, NULL);
442                         rte_mempool_free(mp);
443                         mp = NULL;
444                         comp_dev->streampool = NULL;
445                 }
446         }
447
448         if (mp == NULL) {
449                 struct stream_create_info info = {
450                         .comp_dev = comp_dev,
451                         .socket_id = socket_id,
452                         .error = 0
453                 };
454                 mp = rte_mempool_create(stream_pool_name,
455                                 num_elements,
456                                 qat_comp_stream_size(), 0, 0,
457                                 NULL, NULL, qat_comp_stream_init, &info,
458                                 socket_id, 0);
459                 if (mp == NULL) {
460                         QAT_LOG(ERR,
461                              "Err creating mempool %s w %d elements of size %d",
462                              stream_pool_name, num_elements,
463                              qat_comp_stream_size());
464                 } else if (info.error) {
465                         rte_mempool_obj_iter(mp, qat_comp_stream_destroy, NULL);
466                         QAT_LOG(ERR,
467                              "Destoying mempool %s as at least one element failed initialisation",
468                              stream_pool_name);
469                         rte_mempool_free(mp);
470                         mp = NULL;
471                 }
472         }
473
474         return mp;
475 }
476
477 static void
478 _qat_comp_dev_config_clear(struct qat_comp_dev_private *comp_dev)
479 {
480         /* Free intermediate buffers */
481         if (comp_dev->interm_buff_mz) {
482                 char mz_name[RTE_MEMZONE_NAMESIZE];
483                 int i = qat_gen_config[
484                       comp_dev->qat_dev->qat_dev_gen].comp_num_im_bufs_required;
485
486                 while (--i >= 0) {
487                         snprintf(mz_name, RTE_MEMZONE_NAMESIZE,
488                                         "%s_inter_buff_%d",
489                                         comp_dev->qat_dev->name, i);
490                         rte_memzone_free(rte_memzone_lookup(mz_name));
491                 }
492                 rte_memzone_free(comp_dev->interm_buff_mz);
493                 comp_dev->interm_buff_mz = NULL;
494         }
495
496         /* Free private_xform pool */
497         if (comp_dev->xformpool) {
498                 /* Free internal mempool for private xforms */
499                 rte_mempool_free(comp_dev->xformpool);
500                 comp_dev->xformpool = NULL;
501         }
502
503         /* Free stream pool */
504         if (comp_dev->streampool) {
505                 rte_mempool_obj_iter(comp_dev->streampool,
506                                      qat_comp_stream_destroy, NULL);
507                 rte_mempool_free(comp_dev->streampool);
508                 comp_dev->streampool = NULL;
509         }
510 }
511
512 static int
513 qat_comp_dev_config(struct rte_compressdev *dev,
514                 struct rte_compressdev_config *config)
515 {
516         struct qat_comp_dev_private *comp_dev = dev->data->dev_private;
517         int ret = 0;
518
519         if (RTE_PMD_QAT_COMP_IM_BUFFER_SIZE == 0) {
520                 QAT_LOG(WARNING,
521                         "RTE_PMD_QAT_COMP_IM_BUFFER_SIZE = 0 in config file, so"
522                         " QAT device can't be used for Dynamic Deflate. "
523                         "Did you really intend to do this?");
524         } else {
525                 comp_dev->interm_buff_mz =
526                                 qat_comp_setup_inter_buffers(comp_dev,
527                                         RTE_PMD_QAT_COMP_IM_BUFFER_SIZE);
528                 if (comp_dev->interm_buff_mz == NULL) {
529                         ret = -ENOMEM;
530                         goto error_out;
531                 }
532         }
533
534         if (config->max_nb_priv_xforms) {
535                 comp_dev->xformpool = qat_comp_create_xform_pool(comp_dev,
536                                             config, config->max_nb_priv_xforms);
537                 if (comp_dev->xformpool == NULL) {
538                         ret = -ENOMEM;
539                         goto error_out;
540                 }
541         } else
542                 comp_dev->xformpool = NULL;
543
544         if (config->max_nb_streams) {
545                 comp_dev->streampool = qat_comp_create_stream_pool(comp_dev,
546                                      config->socket_id, config->max_nb_streams);
547                 if (comp_dev->streampool == NULL) {
548                         ret = -ENOMEM;
549                         goto error_out;
550                 }
551         } else
552                 comp_dev->streampool = NULL;
553
554         return 0;
555
556 error_out:
557         _qat_comp_dev_config_clear(comp_dev);
558         return ret;
559 }
560
561 static int
562 qat_comp_dev_start(struct rte_compressdev *dev __rte_unused)
563 {
564         return 0;
565 }
566
567 static void
568 qat_comp_dev_stop(struct rte_compressdev *dev __rte_unused)
569 {
570
571 }
572
573 static int
574 qat_comp_dev_close(struct rte_compressdev *dev)
575 {
576         int i;
577         int ret = 0;
578         struct qat_comp_dev_private *comp_dev = dev->data->dev_private;
579
580         for (i = 0; i < dev->data->nb_queue_pairs; i++) {
581                 ret = qat_comp_qp_release(dev, i);
582                 if (ret < 0)
583                         return ret;
584         }
585
586         _qat_comp_dev_config_clear(comp_dev);
587
588         return ret;
589 }
590
591
592 static void
593 qat_comp_dev_info_get(struct rte_compressdev *dev,
594                         struct rte_compressdev_info *info)
595 {
596         struct qat_comp_dev_private *comp_dev = dev->data->dev_private;
597         struct qat_pci_device *qat_dev = comp_dev->qat_dev;
598
599         if (info != NULL) {
600                 info->max_nb_queue_pairs =
601                         qat_qps_per_service(qat_dev,
602                                             QAT_SERVICE_COMPRESSION);
603                 info->feature_flags = dev->feature_flags;
604                 info->capabilities = comp_dev->qat_dev_capabilities;
605         }
606 }
607
608 static uint16_t
609 qat_comp_pmd_enq_deq_dummy_op_burst(void *qp __rte_unused,
610                                     struct rte_comp_op **ops __rte_unused,
611                                     uint16_t nb_ops __rte_unused)
612 {
613         QAT_DP_LOG(ERR, "QAT PMD detected wrong FW version !");
614         return 0;
615 }
616
617 static struct rte_compressdev_ops compress_qat_dummy_ops = {
618
619         /* Device related operations */
620         .dev_configure          = NULL,
621         .dev_start              = NULL,
622         .dev_stop               = qat_comp_dev_stop,
623         .dev_close              = qat_comp_dev_close,
624         .dev_infos_get          = NULL,
625
626         .stats_get              = NULL,
627         .stats_reset            = qat_comp_stats_reset,
628         .queue_pair_setup       = NULL,
629         .queue_pair_release     = qat_comp_qp_release,
630
631         /* Compression related operations */
632         .private_xform_create   = NULL,
633         .private_xform_free     = qat_comp_private_xform_free
634 };
635
636 static uint16_t
637 qat_comp_pmd_dequeue_first_op_burst(void *qp, struct rte_comp_op **ops,
638                                    uint16_t nb_ops)
639 {
640         uint16_t ret = qat_dequeue_op_burst(qp, (void **)ops, nb_ops);
641         struct qat_qp *tmp_qp = (struct qat_qp *)qp;
642
643         if (ret) {
644                 if ((*ops)->debug_status ==
645                                 (uint64_t)ERR_CODE_QAT_COMP_WRONG_FW) {
646                         tmp_qp->qat_dev->comp_dev->compressdev->enqueue_burst =
647                                         qat_comp_pmd_enq_deq_dummy_op_burst;
648                         tmp_qp->qat_dev->comp_dev->compressdev->dequeue_burst =
649                                         qat_comp_pmd_enq_deq_dummy_op_burst;
650
651                         tmp_qp->qat_dev->comp_dev->compressdev->dev_ops =
652                                         &compress_qat_dummy_ops;
653                         QAT_LOG(ERR,
654                                         "This QAT hardware doesn't support compression operation");
655
656                 } else {
657                         tmp_qp->qat_dev->comp_dev->compressdev->dequeue_burst =
658                                         (compressdev_dequeue_pkt_burst_t)
659                                         qat_dequeue_op_burst;
660                 }
661         }
662         return ret;
663 }
664
665 static struct rte_compressdev_ops compress_qat_ops = {
666
667         /* Device related operations */
668         .dev_configure          = qat_comp_dev_config,
669         .dev_start              = qat_comp_dev_start,
670         .dev_stop               = qat_comp_dev_stop,
671         .dev_close              = qat_comp_dev_close,
672         .dev_infos_get          = qat_comp_dev_info_get,
673
674         .stats_get              = qat_comp_stats_get,
675         .stats_reset            = qat_comp_stats_reset,
676         .queue_pair_setup       = qat_comp_qp_setup,
677         .queue_pair_release     = qat_comp_qp_release,
678
679         /* Compression related operations */
680         .private_xform_create   = qat_comp_private_xform_create,
681         .private_xform_free     = qat_comp_private_xform_free,
682         .stream_create          = qat_comp_stream_create,
683         .stream_free            = qat_comp_stream_free
684 };
685
686 /* An rte_driver is needed in the registration of the device with compressdev.
687  * The actual qat pci's rte_driver can't be used as its name represents
688  * the whole pci device with all services. Think of this as a holder for a name
689  * for the compression part of the pci device.
690  */
691 static const char qat_comp_drv_name[] = RTE_STR(COMPRESSDEV_NAME_QAT_PMD);
692 static const struct rte_driver compdev_qat_driver = {
693         .name = qat_comp_drv_name,
694         .alias = qat_comp_drv_name
695 };
696 int
697 qat_comp_dev_create(struct qat_pci_device *qat_pci_dev,
698                 struct qat_dev_cmd_param *qat_dev_cmd_param)
699 {
700         int i = 0;
701         struct qat_device_info *qat_dev_instance =
702                         &qat_pci_devs[qat_pci_dev->qat_dev_id];
703         struct rte_compressdev_pmd_init_params init_params = {
704                 .name = "",
705                 .socket_id = qat_dev_instance->pci_dev->device.numa_node,
706         };
707         char name[RTE_COMPRESSDEV_NAME_MAX_LEN];
708         char capa_memz_name[RTE_COMPRESSDEV_NAME_MAX_LEN];
709         struct rte_compressdev *compressdev;
710         struct qat_comp_dev_private *comp_dev;
711         const struct rte_compressdev_capabilities *capabilities;
712         uint64_t capa_size;
713
714         if (qat_pci_dev->qat_dev_gen == QAT_GEN4) {
715                 QAT_LOG(ERR, "Compression PMD not supported on QAT 4xxx");
716                 return -EFAULT;
717         }
718         snprintf(name, RTE_COMPRESSDEV_NAME_MAX_LEN, "%s_%s",
719                         qat_pci_dev->name, "comp");
720         QAT_LOG(DEBUG, "Creating QAT COMP device %s", name);
721
722         /* Populate subset device to use in compressdev device creation */
723         qat_dev_instance->comp_rte_dev.driver = &compdev_qat_driver;
724         qat_dev_instance->comp_rte_dev.numa_node =
725                         qat_dev_instance->pci_dev->device.numa_node;
726         qat_dev_instance->comp_rte_dev.devargs = NULL;
727
728         compressdev = rte_compressdev_pmd_create(name,
729                         &(qat_dev_instance->comp_rte_dev),
730                         sizeof(struct qat_comp_dev_private),
731                         &init_params);
732
733         if (compressdev == NULL)
734                 return -ENODEV;
735
736         compressdev->dev_ops = &compress_qat_ops;
737
738         compressdev->enqueue_burst = (compressdev_enqueue_pkt_burst_t)
739                         qat_enqueue_comp_op_burst;
740         compressdev->dequeue_burst = qat_comp_pmd_dequeue_first_op_burst;
741
742         compressdev->feature_flags = RTE_COMPDEV_FF_HW_ACCELERATED;
743
744         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
745                 return 0;
746
747         snprintf(capa_memz_name, RTE_COMPRESSDEV_NAME_MAX_LEN,
748                         "QAT_COMP_CAPA_GEN_%d",
749                         qat_pci_dev->qat_dev_gen);
750
751         comp_dev = compressdev->data->dev_private;
752         comp_dev->qat_dev = qat_pci_dev;
753         comp_dev->compressdev = compressdev;
754
755         switch (qat_pci_dev->qat_dev_gen) {
756         case QAT_GEN1:
757         case QAT_GEN2:
758         case QAT_GEN3:
759                 capabilities = qat_comp_gen_capabilities;
760                 capa_size = sizeof(qat_comp_gen_capabilities);
761                 break;
762         default:
763                 capabilities = qat_comp_gen_capabilities;
764                 capa_size = sizeof(qat_comp_gen_capabilities);
765                 QAT_LOG(DEBUG,
766                         "QAT gen %d capabilities unknown, default to GEN1",
767                                         qat_pci_dev->qat_dev_gen);
768                 break;
769         }
770
771         comp_dev->capa_mz = rte_memzone_lookup(capa_memz_name);
772         if (comp_dev->capa_mz == NULL) {
773                 comp_dev->capa_mz = rte_memzone_reserve(capa_memz_name,
774                         capa_size,
775                         rte_socket_id(), 0);
776         }
777         if (comp_dev->capa_mz == NULL) {
778                 QAT_LOG(DEBUG,
779                         "Error allocating memzone for capabilities, destroying PMD for %s",
780                         name);
781                 memset(&qat_dev_instance->comp_rte_dev, 0,
782                         sizeof(qat_dev_instance->comp_rte_dev));
783                 rte_compressdev_pmd_destroy(compressdev);
784                 return -EFAULT;
785         }
786
787         memcpy(comp_dev->capa_mz->addr, capabilities, capa_size);
788         comp_dev->qat_dev_capabilities = comp_dev->capa_mz->addr;
789
790         while (1) {
791                 if (qat_dev_cmd_param[i].name == NULL)
792                         break;
793                 if (!strcmp(qat_dev_cmd_param[i].name, COMP_ENQ_THRESHOLD_NAME))
794                         comp_dev->min_enq_burst_threshold =
795                                         qat_dev_cmd_param[i].val;
796                 i++;
797         }
798         qat_pci_dev->comp_dev = comp_dev;
799
800         QAT_LOG(DEBUG,
801                     "Created QAT COMP device %s as compressdev instance %d",
802                         name, compressdev->data->dev_id);
803         return 0;
804 }
805
806 int
807 qat_comp_dev_destroy(struct qat_pci_device *qat_pci_dev)
808 {
809         struct qat_comp_dev_private *comp_dev;
810
811         if (qat_pci_dev == NULL)
812                 return -ENODEV;
813
814         comp_dev = qat_pci_dev->comp_dev;
815         if (comp_dev == NULL)
816                 return 0;
817
818         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
819                 rte_memzone_free(qat_pci_dev->comp_dev->capa_mz);
820
821         /* clean up any resources used by the device */
822         qat_comp_dev_close(comp_dev->compressdev);
823
824         rte_compressdev_pmd_destroy(comp_dev->compressdev);
825         qat_pci_dev->comp_dev = NULL;
826
827         return 0;
828 }