compress/isal: fix offset usage
[dpdk.git] / drivers / compress / isal / isal_compress_pmd.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4 #include <isa-l.h>
5
6 #include <rte_bus_vdev.h>
7 #include <rte_common.h>
8 #include <rte_malloc.h>
9 #include <rte_mbuf.h>
10 #include <rte_compressdev_pmd.h>
11
12 #include "isal_compress_pmd_private.h"
13
14 #define RTE_COMP_ISAL_WINDOW_SIZE 15
15 #define RTE_COMP_ISAL_LEVEL_ZERO 0 /* ISA-L Level 0 used for fixed Huffman */
16 #define RTE_COMP_ISAL_LEVEL_ONE 1
17 #define RTE_COMP_ISAL_LEVEL_TWO 2
18 #define RTE_COMP_ISAL_LEVEL_THREE 3 /* Optimised for AVX512 & AVX2 only */
19
20 int isal_logtype_driver;
21
22 /* Verify and set private xform parameters */
23 int
24 isal_comp_set_priv_xform_parameters(struct isal_priv_xform *priv_xform,
25                 const struct rte_comp_xform *xform)
26 {
27         if (xform == NULL)
28                 return -EINVAL;
29
30         /* Set compression private xform variables */
31         if (xform->type == RTE_COMP_COMPRESS) {
32                 /* Set private xform type - COMPRESS/DECOMPRESS */
33                 priv_xform->type = RTE_COMP_COMPRESS;
34
35                 /* Set private xform algorithm */
36                 if (xform->compress.algo != RTE_COMP_ALGO_DEFLATE) {
37                         if (xform->compress.algo == RTE_COMP_ALGO_NULL) {
38                                 ISAL_PMD_LOG(ERR, "By-pass not supported\n");
39                                 return -ENOTSUP;
40                         }
41                         ISAL_PMD_LOG(ERR, "Algorithm not supported\n");
42                         return -ENOTSUP;
43                 }
44                 priv_xform->compress.algo = RTE_COMP_ALGO_DEFLATE;
45
46                 /* Set private xform checksum - raw deflate by default */
47                 if (xform->compress.chksum != RTE_COMP_CHECKSUM_NONE) {
48                         ISAL_PMD_LOG(ERR, "Checksum not supported\n");
49                         return -ENOTSUP;
50                 }
51
52                 /* Set private xform window size, 32K supported */
53                 if (xform->compress.window_size == RTE_COMP_ISAL_WINDOW_SIZE)
54                         priv_xform->compress.window_size =
55                                         RTE_COMP_ISAL_WINDOW_SIZE;
56                 else {
57                         ISAL_PMD_LOG(ERR, "Window size not supported\n");
58                         return -ENOTSUP;
59                 }
60
61                 /* Set private xform huffman type */
62                 switch (xform->compress.deflate.huffman) {
63                 case(RTE_COMP_HUFFMAN_DEFAULT):
64                         priv_xform->compress.deflate.huffman =
65                                         RTE_COMP_HUFFMAN_DEFAULT;
66                         break;
67                 case(RTE_COMP_HUFFMAN_FIXED):
68                         priv_xform->compress.deflate.huffman =
69                                         RTE_COMP_HUFFMAN_FIXED;
70                         break;
71                 case(RTE_COMP_HUFFMAN_DYNAMIC):
72                         priv_xform->compress.deflate.huffman =
73                                         RTE_COMP_HUFFMAN_DYNAMIC;
74                         break;
75                 default:
76                         ISAL_PMD_LOG(ERR, "Huffman code not supported\n");
77                         return -ENOTSUP;
78                 }
79
80                 /* Set private xform level.
81                  * Checking compliance with compressdev API, -1 <= level => 9
82                  */
83                 if (xform->compress.level < RTE_COMP_LEVEL_PMD_DEFAULT ||
84                                 xform->compress.level > RTE_COMP_LEVEL_MAX) {
85                         ISAL_PMD_LOG(ERR, "Compression level out of range\n");
86                         return -EINVAL;
87                 }
88                 /* Check for Compressdev API level 0, No compression
89                  * not supported in ISA-L
90                  */
91                 else if (xform->compress.level == RTE_COMP_LEVEL_NONE) {
92                         ISAL_PMD_LOG(ERR, "No Compression not supported\n");
93                         return -ENOTSUP;
94                 }
95                 /* If using fixed huffman code, level must be 0 */
96                 else if (priv_xform->compress.deflate.huffman ==
97                                 RTE_COMP_HUFFMAN_FIXED) {
98                         ISAL_PMD_LOG(DEBUG, "ISA-L level 0 used due to a"
99                                         " fixed huffman code\n");
100                         priv_xform->compress.level = RTE_COMP_ISAL_LEVEL_ZERO;
101                         priv_xform->level_buffer_size =
102                                         ISAL_DEF_LVL0_DEFAULT;
103                 } else {
104                         /* Mapping API levels to ISA-L levels 1,2 & 3 */
105                         switch (xform->compress.level) {
106                         case RTE_COMP_LEVEL_PMD_DEFAULT:
107                                 /* Default is 1 if not using fixed huffman */
108                                 priv_xform->compress.level =
109                                                 RTE_COMP_ISAL_LEVEL_ONE;
110                                 priv_xform->level_buffer_size =
111                                                 ISAL_DEF_LVL1_DEFAULT;
112                                 break;
113                         case RTE_COMP_LEVEL_MIN:
114                                 priv_xform->compress.level =
115                                                 RTE_COMP_ISAL_LEVEL_ONE;
116                                 priv_xform->level_buffer_size =
117                                                 ISAL_DEF_LVL1_DEFAULT;
118                                 break;
119                         case RTE_COMP_ISAL_LEVEL_TWO:
120                                 priv_xform->compress.level =
121                                                 RTE_COMP_ISAL_LEVEL_TWO;
122                                 priv_xform->level_buffer_size =
123                                                 ISAL_DEF_LVL2_DEFAULT;
124                                 break;
125                         /* Level 3 or higher requested */
126                         default:
127                                 /* Check for AVX512, to use ISA-L level 3 */
128                                 if (rte_cpu_get_flag_enabled(
129                                                 RTE_CPUFLAG_AVX512F)) {
130                                         priv_xform->compress.level =
131                                                 RTE_COMP_ISAL_LEVEL_THREE;
132                                         priv_xform->level_buffer_size =
133                                                 ISAL_DEF_LVL3_DEFAULT;
134                                 }
135                                 /* Check for AVX2, to use ISA-L level 3 */
136                                 else if (rte_cpu_get_flag_enabled(
137                                                 RTE_CPUFLAG_AVX2)) {
138                                         priv_xform->compress.level =
139                                                 RTE_COMP_ISAL_LEVEL_THREE;
140                                         priv_xform->level_buffer_size =
141                                                 ISAL_DEF_LVL3_DEFAULT;
142                                 } else {
143                                         ISAL_PMD_LOG(DEBUG, "Requested ISA-L level"
144                                                 " 3 or above; Level 3 optimized"
145                                                 " for AVX512 & AVX2 only."
146                                                 " level changed to 2.\n");
147                                         priv_xform->compress.level =
148                                                 RTE_COMP_ISAL_LEVEL_TWO;
149                                         priv_xform->level_buffer_size =
150                                                 ISAL_DEF_LVL2_DEFAULT;
151                                 }
152                         }
153                 }
154         }
155
156         /* Set decompression private xform variables */
157         else if (xform->type == RTE_COMP_DECOMPRESS) {
158
159                 /* Set private xform type - COMPRESS/DECOMPRESS */
160                 priv_xform->type = RTE_COMP_DECOMPRESS;
161
162                 /* Set private xform algorithm */
163                 if (xform->decompress.algo != RTE_COMP_ALGO_DEFLATE) {
164                         if (xform->decompress.algo == RTE_COMP_ALGO_NULL) {
165                                 ISAL_PMD_LOG(ERR, "By pass not supported\n");
166                                 return -ENOTSUP;
167                         }
168                         ISAL_PMD_LOG(ERR, "Algorithm not supported\n");
169                         return -ENOTSUP;
170                 }
171                 priv_xform->decompress.algo = RTE_COMP_ALGO_DEFLATE;
172
173                 /* Set private xform checksum - raw deflate by default */
174                 if (xform->compress.chksum != RTE_COMP_CHECKSUM_NONE) {
175                         ISAL_PMD_LOG(ERR, "Checksum not supported\n");
176                         return -ENOTSUP;
177                 }
178
179                 /* Set private xform window size, 32K supported */
180                 if (xform->decompress.window_size == RTE_COMP_ISAL_WINDOW_SIZE)
181                         priv_xform->decompress.window_size =
182                                         RTE_COMP_ISAL_WINDOW_SIZE;
183                 else {
184                         ISAL_PMD_LOG(ERR, "Window size not supported\n");
185                         return -ENOTSUP;
186                 }
187         }
188         return 0;
189 }
190
191 /* Stateless Compression Function */
192 static int
193 process_isal_deflate(struct rte_comp_op *op, struct isal_comp_qp *qp,
194                 struct isal_priv_xform *priv_xform)
195 {
196         int ret = 0;
197         op->status = RTE_COMP_OP_STATUS_SUCCESS;
198
199         /* Required due to init clearing level_buf */
200         uint8_t *temp_level_buf = qp->stream->level_buf;
201
202         /* Initialize compression stream */
203         isal_deflate_stateless_init(qp->stream);
204
205         qp->stream->level_buf = temp_level_buf;
206
207         /* Stateless operation, input will be consumed in one go */
208         qp->stream->flush = NO_FLUSH;
209
210         /* set op level & intermediate level buffer */
211         qp->stream->level = priv_xform->compress.level;
212         qp->stream->level_buf_size = priv_xform->level_buffer_size;
213
214         /* Set op huffman code */
215         if (priv_xform->compress.deflate.huffman == RTE_COMP_HUFFMAN_FIXED)
216                 isal_deflate_set_hufftables(qp->stream, NULL,
217                                 IGZIP_HUFFTABLE_STATIC);
218         else if (priv_xform->compress.deflate.huffman ==
219                         RTE_COMP_HUFFMAN_DEFAULT)
220                 isal_deflate_set_hufftables(qp->stream, NULL,
221                         IGZIP_HUFFTABLE_DEFAULT);
222         /* Dynamically change the huffman code to suit the input data */
223         else if (priv_xform->compress.deflate.huffman ==
224                         RTE_COMP_HUFFMAN_DYNAMIC)
225                 isal_deflate_set_hufftables(qp->stream, NULL,
226                                 IGZIP_HUFFTABLE_DEFAULT);
227
228         qp->stream->end_of_stream = 1; /* All input consumed in one go */
229         if ((op->src.length + op->src.offset) > op->m_src->data_len) {
230                 ISAL_PMD_LOG(ERR, "Input mbuf not big enough for the length and"
231                                 " offset provided.\n");
232                 op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
233                 return -1;
234         }
235         /* Point compression stream to input buffer */
236         qp->stream->avail_in = op->src.length;
237         qp->stream->next_in = rte_pktmbuf_mtod_offset(op->m_src, uint8_t *,
238                         op->src.offset);
239
240         if (op->dst.offset > op->m_dst->data_len) {
241                 ISAL_PMD_LOG(ERR, "Output mbuf not big enough for the length"
242                                 " and offset provided.\n");
243                 op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
244                 return -1;
245         }
246         /*  Point compression stream to output buffer */
247         qp->stream->avail_out = op->m_dst->data_len - op->dst.offset;
248         qp->stream->next_out  = rte_pktmbuf_mtod_offset(op->m_dst, uint8_t *,
249                 op->dst.offset);
250
251         if (unlikely(!qp->stream->next_in || !qp->stream->next_out)) {
252                 ISAL_PMD_LOG(ERR, "Invalid source or destination buffers\n");
253                 op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
254                 return -1;
255         }
256
257         /* Execute compression operation */
258         ret =  isal_deflate_stateless(qp->stream);
259
260         /* Check that output buffer did not run out of space */
261         if (ret == STATELESS_OVERFLOW) {
262                 ISAL_PMD_LOG(ERR, "Output buffer not big enough\n");
263                 op->status = RTE_COMP_OP_STATUS_OUT_OF_SPACE_TERMINATED;
264                 return ret;
265         }
266
267         /* Check that input buffer has been fully consumed */
268         if (qp->stream->avail_in != (uint32_t)0) {
269                 ISAL_PMD_LOG(ERR, "Input buffer could not be read entirely\n");
270                 op->status = RTE_COMP_OP_STATUS_ERROR;
271                 return -1;
272         }
273
274         if (ret != COMP_OK) {
275                 op->status = RTE_COMP_OP_STATUS_ERROR;
276                 return ret;
277         }
278
279         op->consumed = qp->stream->total_in;
280         op->produced = qp->stream->total_out;
281
282         return ret;
283 }
284
285 /* Stateless Decompression Function */
286 static int
287 process_isal_inflate(struct rte_comp_op *op, struct isal_comp_qp *qp)
288 {
289         int ret = 0;
290
291         op->status = RTE_COMP_OP_STATUS_SUCCESS;
292
293         /* Initialize decompression state */
294         isal_inflate_init(qp->state);
295
296         if ((op->src.length + op->src.offset) > op->m_src->data_len) {
297                 ISAL_PMD_LOG(ERR, "Input mbuf not big enough for the length and"
298                                 " offset provided.\n");
299                 op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
300                 return -1;
301         }
302         /* Point decompression state to input buffer */
303         qp->state->avail_in = op->src.length;
304         qp->state->next_in = rte_pktmbuf_mtod_offset(op->m_src, uint8_t *,
305                         op->src.offset);
306
307         if (op->dst.offset > op->m_dst->data_len) {
308                 ISAL_PMD_LOG(ERR, "Output mbuf not big enough for the length "
309                                 "and offset provided.\n");
310                 op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
311                 return -1;
312         }
313         /* Point decompression state to output buffer */
314         qp->state->avail_out = op->m_dst->data_len - op->dst.offset;
315         qp->state->next_out  = rte_pktmbuf_mtod_offset(op->m_dst, uint8_t *,
316                         op->dst.offset);
317
318         if (unlikely(!qp->state->next_in || !qp->state->next_out)) {
319                 ISAL_PMD_LOG(ERR, "Invalid source or destination buffers\n");
320                 op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
321                 return -1;
322         }
323
324         /* Execute decompression operation */
325         ret = isal_inflate_stateless(qp->state);
326
327         if (ret == ISAL_OUT_OVERFLOW) {
328                 ISAL_PMD_LOG(ERR, "Output buffer not big enough\n");
329                 op->status = RTE_COMP_OP_STATUS_OUT_OF_SPACE_TERMINATED;
330                 return ret;
331         }
332
333         /* Check that input buffer has been fully consumed */
334         if (qp->state->avail_in != (uint32_t)0) {
335                 ISAL_PMD_LOG(ERR, "Input buffer could not be read entirely\n");
336                 op->status = RTE_COMP_OP_STATUS_ERROR;
337                 return -1;
338         }
339
340         if (ret != ISAL_DECOMP_OK) {
341                 op->status = RTE_COMP_OP_STATUS_ERROR;
342                 return ret;
343         }
344
345         op->consumed = op->src.length - qp->state->avail_in;
346         op->produced = qp->state->total_out;
347
348 return ret;
349 }
350
351 /* Process compression/decompression operation */
352 static int
353 process_op(struct isal_comp_qp *qp, struct rte_comp_op *op,
354                 struct isal_priv_xform *priv_xform)
355 {
356         switch (priv_xform->type) {
357         case RTE_COMP_COMPRESS:
358                 process_isal_deflate(op, qp, priv_xform);
359                 break;
360         case RTE_COMP_DECOMPRESS:
361                 process_isal_inflate(op, qp);
362                 break;
363         default:
364                 ISAL_PMD_LOG(ERR, "Operation Not Supported\n");
365                 return -ENOTSUP;
366         }
367         return 0;
368 }
369
370 /* Enqueue burst */
371 static uint16_t
372 isal_comp_pmd_enqueue_burst(void *queue_pair, struct rte_comp_op **ops,
373                         uint16_t nb_ops)
374 {
375         struct isal_comp_qp *qp = queue_pair;
376         uint16_t i;
377         int retval;
378         int16_t num_enq = RTE_MIN(qp->num_free_elements, nb_ops);
379
380         for (i = 0; i < num_enq; i++) {
381                 if (unlikely(ops[i]->op_type != RTE_COMP_OP_STATELESS)) {
382                         ops[i]->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
383                         ISAL_PMD_LOG(ERR, "Stateful operation not Supported\n");
384                         qp->qp_stats.enqueue_err_count++;
385                         continue;
386                 }
387                 retval = process_op(qp, ops[i], ops[i]->private_xform);
388                 if (unlikely(retval < 0) ||
389                                 ops[i]->status != RTE_COMP_OP_STATUS_SUCCESS) {
390                         qp->qp_stats.enqueue_err_count++;
391                 }
392         }
393
394         retval = rte_ring_enqueue_burst(qp->processed_pkts, (void *)ops,
395                         num_enq, NULL);
396         qp->num_free_elements -= retval;
397         qp->qp_stats.enqueued_count += retval;
398
399         return retval;
400 }
401
402 /* Dequeue burst */
403 static uint16_t
404 isal_comp_pmd_dequeue_burst(void *queue_pair, struct rte_comp_op **ops,
405                 uint16_t nb_ops)
406 {
407         struct isal_comp_qp *qp = queue_pair;
408         uint16_t nb_dequeued;
409
410         nb_dequeued = rte_ring_dequeue_burst(qp->processed_pkts, (void **)ops,
411                         nb_ops, NULL);
412         qp->num_free_elements += nb_dequeued;
413         qp->qp_stats.dequeued_count += nb_dequeued;
414
415         return nb_dequeued;
416 }
417
418 /* Create ISA-L compression device */
419 static int
420 compdev_isal_create(const char *name, struct rte_vdev_device *vdev,
421                 struct rte_compressdev_pmd_init_params *init_params)
422 {
423         struct rte_compressdev *dev;
424
425         dev = rte_compressdev_pmd_create(name, &vdev->device,
426                         sizeof(struct isal_comp_private), init_params);
427         if (dev == NULL) {
428                 ISAL_PMD_LOG(ERR, "failed to create compressdev vdev");
429                 return -EFAULT;
430         }
431
432         dev->dev_ops = isal_compress_pmd_ops;
433
434         /* register rx/tx burst functions for data path */
435         dev->dequeue_burst = isal_comp_pmd_dequeue_burst;
436         dev->enqueue_burst = isal_comp_pmd_enqueue_burst;
437
438         return 0;
439 }
440
441 /** Remove compression device */
442 static int
443 compdev_isal_remove_dev(struct rte_vdev_device *vdev)
444 {
445         struct rte_compressdev *compdev;
446         const char *name;
447
448         name = rte_vdev_device_name(vdev);
449         if (name == NULL)
450                 return -EINVAL;
451
452         compdev = rte_compressdev_pmd_get_named_dev(name);
453         if (compdev == NULL)
454                 return -ENODEV;
455
456         return rte_compressdev_pmd_destroy(compdev);
457 }
458
459 /** Initialise ISA-L compression device */
460 static int
461 compdev_isal_probe(struct rte_vdev_device *dev)
462 {
463         struct rte_compressdev_pmd_init_params init_params = {
464                 "",
465                 rte_socket_id(),
466         };
467         const char *name, *args;
468         int retval;
469
470         name = rte_vdev_device_name(dev);
471         if (name == NULL)
472                 return -EINVAL;
473
474         args = rte_vdev_device_args(dev);
475
476         retval = rte_compressdev_pmd_parse_input_args(&init_params, args);
477         if (retval) {
478                 ISAL_PMD_LOG(ERR,
479                         "Failed to parse initialisation arguments[%s]\n", args);
480                 return -EINVAL;
481         }
482
483         return compdev_isal_create(name, dev, &init_params);
484 }
485
486 static struct rte_vdev_driver compdev_isal_pmd_drv = {
487         .probe = compdev_isal_probe,
488         .remove = compdev_isal_remove_dev,
489 };
490
491 RTE_PMD_REGISTER_VDEV(COMPDEV_NAME_ISAL_PMD, compdev_isal_pmd_drv);
492 RTE_PMD_REGISTER_PARAM_STRING(COMPDEV_NAME_ISAL_PMD,
493         "socket_id=<int>");
494
495 RTE_INIT(isal_init_log)
496 {
497         isal_logtype_driver = rte_log_register("comp_isal");
498         if (isal_logtype_driver >= 0)
499                 rte_log_set_level(isal_logtype_driver, RTE_LOG_INFO);
500 }