net/ice: fix GTPU RSS
[dpdk.git] / drivers / baseband / turbo_sw / bbdev_turbo_software.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 #include <string.h>
6
7 #include <rte_common.h>
8 #include <rte_bus_vdev.h>
9 #include <rte_malloc.h>
10 #include <rte_ring.h>
11 #include <rte_kvargs.h>
12 #include <rte_cycles.h>
13
14 #include <rte_bbdev.h>
15 #include <rte_bbdev_pmd.h>
16
17 #include <rte_hexdump.h>
18 #include <rte_log.h>
19
20 #ifdef RTE_BBDEV_SDK_AVX2
21 #include <ipp.h>
22 #include <ipps.h>
23 #include <phy_turbo.h>
24 #include <phy_crc.h>
25 #include <phy_rate_match.h>
26 #endif
27 #ifdef RTE_BBDEV_SDK_AVX512
28 #include <bit_reverse.h>
29 #include <phy_ldpc_encoder_5gnr.h>
30 #include <phy_ldpc_decoder_5gnr.h>
31 #include <phy_LDPC_ratematch_5gnr.h>
32 #include <phy_rate_dematching_5gnr.h>
33 #endif
34
35 #define DRIVER_NAME baseband_turbo_sw
36
37 RTE_LOG_REGISTER(bbdev_turbo_sw_logtype, pmd.bb.turbo_sw, NOTICE);
38
39 /* Helper macro for logging */
40 #define rte_bbdev_log(level, fmt, ...) \
41         rte_log(RTE_LOG_ ## level, bbdev_turbo_sw_logtype, fmt "\n", \
42                 ##__VA_ARGS__)
43
44 #define rte_bbdev_log_debug(fmt, ...) \
45         rte_bbdev_log(DEBUG, RTE_STR(__LINE__) ":%s() " fmt, __func__, \
46                 ##__VA_ARGS__)
47
48 #define DEINT_INPUT_BUF_SIZE (((RTE_BBDEV_TURBO_MAX_CB_SIZE >> 3) + 1) * 48)
49 #define DEINT_OUTPUT_BUF_SIZE (DEINT_INPUT_BUF_SIZE * 6)
50 #define ADAPTER_OUTPUT_BUF_SIZE ((RTE_BBDEV_TURBO_MAX_CB_SIZE + 4) * 48)
51
52 /* private data structure */
53 struct bbdev_private {
54         unsigned int max_nb_queues;  /**< Max number of queues */
55 };
56
57 /*  Initialisation params structure that can be used by Turbo SW driver */
58 struct turbo_sw_params {
59         int socket_id;  /*< Turbo SW device socket */
60         uint16_t queues_num;  /*< Turbo SW device queues number */
61 };
62
63 /* Accecptable params for Turbo SW devices */
64 #define TURBO_SW_MAX_NB_QUEUES_ARG  "max_nb_queues"
65 #define TURBO_SW_SOCKET_ID_ARG      "socket_id"
66
67 static const char * const turbo_sw_valid_params[] = {
68         TURBO_SW_MAX_NB_QUEUES_ARG,
69         TURBO_SW_SOCKET_ID_ARG
70 };
71
72 /* queue */
73 struct turbo_sw_queue {
74         /* Ring for processed (encoded/decoded) operations which are ready to
75          * be dequeued.
76          */
77         struct rte_ring *processed_pkts;
78         /* Stores input for turbo encoder (used when CRC attachment is
79          * performed
80          */
81         uint8_t *enc_in;
82         /* Stores output from turbo encoder */
83         uint8_t *enc_out;
84         /* Alpha gamma buf for bblib_turbo_decoder() function */
85         int8_t *ag;
86         /* Temp buf for bblib_turbo_decoder() function */
87         uint16_t *code_block;
88         /* Input buf for bblib_rate_dematching_lte() function */
89         uint8_t *deint_input;
90         /* Output buf for bblib_rate_dematching_lte() function */
91         uint8_t *deint_output;
92         /* Output buf for bblib_turbodec_adapter_lte() function */
93         uint8_t *adapter_output;
94         /* Operation type of this queue */
95         enum rte_bbdev_op_type type;
96 } __rte_cache_aligned;
97
98
99 #ifdef RTE_BBDEV_SDK_AVX2
100 static inline char *
101 mbuf_append(struct rte_mbuf *m_head, struct rte_mbuf *m, uint16_t len)
102 {
103         if (unlikely(len > rte_pktmbuf_tailroom(m)))
104                 return NULL;
105
106         char *tail = (char *)m->buf_addr + m->data_off + m->data_len;
107         m->data_len = (uint16_t)(m->data_len + len);
108         m_head->pkt_len  = (m_head->pkt_len + len);
109         return tail;
110 }
111
112 /* Calculate index based on Table 5.1.3-3 from TS34.212 */
113 static inline int32_t
114 compute_idx(uint16_t k)
115 {
116         int32_t result = 0;
117
118         if (k < RTE_BBDEV_TURBO_MIN_CB_SIZE || k > RTE_BBDEV_TURBO_MAX_CB_SIZE)
119                 return -1;
120
121         if (k > 2048) {
122                 if ((k - 2048) % 64 != 0)
123                         result = -1;
124
125                 result = 124 + (k - 2048) / 64;
126         } else if (k <= 512) {
127                 if ((k - 40) % 8 != 0)
128                         result = -1;
129
130                 result = (k - 40) / 8 + 1;
131         } else if (k <= 1024) {
132                 if ((k - 512) % 16 != 0)
133                         result = -1;
134
135                 result = 60 + (k - 512) / 16;
136         } else { /* 1024 < k <= 2048 */
137                 if ((k - 1024) % 32 != 0)
138                         result = -1;
139
140                 result = 92 + (k - 1024) / 32;
141         }
142
143         return result;
144 }
145 #endif
146
147 /* Read flag value 0/1 from bitmap */
148 static inline bool
149 check_bit(uint32_t bitmap, uint32_t bitmask)
150 {
151         return bitmap & bitmask;
152 }
153
154 /* Get device info */
155 static void
156 info_get(struct rte_bbdev *dev, struct rte_bbdev_driver_info *dev_info)
157 {
158         struct bbdev_private *internals = dev->data->dev_private;
159
160         static const struct rte_bbdev_op_cap bbdev_capabilities[] = {
161 #ifdef RTE_BBDEV_SDK_AVX2
162                 {
163                         .type = RTE_BBDEV_OP_TURBO_DEC,
164                         .cap.turbo_dec = {
165                                 .capability_flags =
166                                         RTE_BBDEV_TURBO_SUBBLOCK_DEINTERLEAVE |
167                                         RTE_BBDEV_TURBO_POS_LLR_1_BIT_IN |
168                                         RTE_BBDEV_TURBO_NEG_LLR_1_BIT_IN |
169                                         RTE_BBDEV_TURBO_CRC_TYPE_24B |
170                                         RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP |
171                                         RTE_BBDEV_TURBO_EARLY_TERMINATION,
172                                 .max_llr_modulus = 16,
173                                 .num_buffers_src =
174                                                 RTE_BBDEV_TURBO_MAX_CODE_BLOCKS,
175                                 .num_buffers_hard_out =
176                                                 RTE_BBDEV_TURBO_MAX_CODE_BLOCKS,
177                                 .num_buffers_soft_out = 0,
178                         }
179                 },
180                 {
181                         .type   = RTE_BBDEV_OP_TURBO_ENC,
182                         .cap.turbo_enc = {
183                                 .capability_flags =
184                                                 RTE_BBDEV_TURBO_CRC_24B_ATTACH |
185                                                 RTE_BBDEV_TURBO_CRC_24A_ATTACH |
186                                                 RTE_BBDEV_TURBO_RATE_MATCH |
187                                                 RTE_BBDEV_TURBO_RV_INDEX_BYPASS,
188                                 .num_buffers_src =
189                                                 RTE_BBDEV_TURBO_MAX_CODE_BLOCKS,
190                                 .num_buffers_dst =
191                                                 RTE_BBDEV_TURBO_MAX_CODE_BLOCKS,
192                         }
193                 },
194 #endif
195 #ifdef RTE_BBDEV_SDK_AVX512
196                 {
197                         .type   = RTE_BBDEV_OP_LDPC_ENC,
198                         .cap.ldpc_enc = {
199                                 .capability_flags =
200                                                 RTE_BBDEV_LDPC_RATE_MATCH |
201                                                 RTE_BBDEV_LDPC_CRC_24A_ATTACH |
202                                                 RTE_BBDEV_LDPC_CRC_24B_ATTACH,
203                                 .num_buffers_src =
204                                                 RTE_BBDEV_LDPC_MAX_CODE_BLOCKS,
205                                 .num_buffers_dst =
206                                                 RTE_BBDEV_LDPC_MAX_CODE_BLOCKS,
207                         }
208                 },
209                 {
210                 .type   = RTE_BBDEV_OP_LDPC_DEC,
211                 .cap.ldpc_dec = {
212                         .capability_flags =
213                                         RTE_BBDEV_LDPC_CRC_TYPE_24B_CHECK |
214                                         RTE_BBDEV_LDPC_CRC_TYPE_24A_CHECK |
215                                         RTE_BBDEV_LDPC_CRC_TYPE_24B_DROP |
216                                         RTE_BBDEV_LDPC_HQ_COMBINE_IN_ENABLE |
217                                         RTE_BBDEV_LDPC_HQ_COMBINE_OUT_ENABLE |
218                                         RTE_BBDEV_LDPC_ITERATION_STOP_ENABLE,
219                         .llr_size = 8,
220                         .llr_decimals = 4,
221                         .num_buffers_src =
222                                         RTE_BBDEV_LDPC_MAX_CODE_BLOCKS,
223                         .num_buffers_hard_out =
224                                         RTE_BBDEV_LDPC_MAX_CODE_BLOCKS,
225                         .num_buffers_soft_out = 0,
226                 }
227                 },
228 #endif
229                 RTE_BBDEV_END_OF_CAPABILITIES_LIST()
230         };
231
232         static struct rte_bbdev_queue_conf default_queue_conf = {
233                 .queue_size = RTE_BBDEV_QUEUE_SIZE_LIMIT,
234         };
235 #ifdef RTE_BBDEV_SDK_AVX2
236         static const enum rte_cpu_flag_t cpu_flag = RTE_CPUFLAG_SSE4_2;
237         dev_info->cpu_flag_reqs = &cpu_flag;
238 #else
239         dev_info->cpu_flag_reqs = NULL;
240 #endif
241         default_queue_conf.socket = dev->data->socket_id;
242
243         dev_info->driver_name = RTE_STR(DRIVER_NAME);
244         dev_info->max_num_queues = internals->max_nb_queues;
245         dev_info->queue_size_lim = RTE_BBDEV_QUEUE_SIZE_LIMIT;
246         dev_info->hardware_accelerated = false;
247         dev_info->max_dl_queue_priority = 0;
248         dev_info->max_ul_queue_priority = 0;
249         dev_info->default_queue_conf = default_queue_conf;
250         dev_info->capabilities = bbdev_capabilities;
251         dev_info->min_alignment = 64;
252         dev_info->harq_buffer_size = 0;
253
254         rte_bbdev_log_debug("got device info from %u\n", dev->data->dev_id);
255 }
256
257 /* Release queue */
258 static int
259 q_release(struct rte_bbdev *dev, uint16_t q_id)
260 {
261         struct turbo_sw_queue *q = dev->data->queues[q_id].queue_private;
262
263         if (q != NULL) {
264                 rte_ring_free(q->processed_pkts);
265                 rte_free(q->enc_out);
266                 rte_free(q->enc_in);
267                 rte_free(q->ag);
268                 rte_free(q->code_block);
269                 rte_free(q->deint_input);
270                 rte_free(q->deint_output);
271                 rte_free(q->adapter_output);
272                 rte_free(q);
273                 dev->data->queues[q_id].queue_private = NULL;
274         }
275
276         rte_bbdev_log_debug("released device queue %u:%u",
277                         dev->data->dev_id, q_id);
278         return 0;
279 }
280
281 /* Setup a queue */
282 static int
283 q_setup(struct rte_bbdev *dev, uint16_t q_id,
284                 const struct rte_bbdev_queue_conf *queue_conf)
285 {
286         int ret;
287         struct turbo_sw_queue *q;
288         char name[RTE_RING_NAMESIZE];
289
290         /* Allocate the queue data structure. */
291         q = rte_zmalloc_socket(RTE_STR(DRIVER_NAME), sizeof(*q),
292                         RTE_CACHE_LINE_SIZE, queue_conf->socket);
293         if (q == NULL) {
294                 rte_bbdev_log(ERR, "Failed to allocate queue memory");
295                 return -ENOMEM;
296         }
297
298         /* Allocate memory for encoder output. */
299         ret = snprintf(name, RTE_RING_NAMESIZE, RTE_STR(DRIVER_NAME)"_enc_o%u:%u",
300                         dev->data->dev_id, q_id);
301         if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
302                 rte_bbdev_log(ERR,
303                                 "Creating queue name for device %u queue %u failed",
304                                 dev->data->dev_id, q_id);
305                 return -ENAMETOOLONG;
306         }
307         q->enc_out = rte_zmalloc_socket(name,
308                         ((RTE_BBDEV_TURBO_MAX_TB_SIZE >> 3) + 3) *
309                         sizeof(*q->enc_out) * 3,
310                         RTE_CACHE_LINE_SIZE, queue_conf->socket);
311         if (q->enc_out == NULL) {
312                 rte_bbdev_log(ERR,
313                         "Failed to allocate queue memory for %s", name);
314                 goto free_q;
315         }
316
317         /* Allocate memory for rate matching output. */
318         ret = snprintf(name, RTE_RING_NAMESIZE,
319                         RTE_STR(DRIVER_NAME)"_enc_i%u:%u", dev->data->dev_id,
320                         q_id);
321         if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
322                 rte_bbdev_log(ERR,
323                                 "Creating queue name for device %u queue %u failed",
324                                 dev->data->dev_id, q_id);
325                 return -ENAMETOOLONG;
326         }
327         q->enc_in = rte_zmalloc_socket(name,
328                         (RTE_BBDEV_LDPC_MAX_CB_SIZE >> 3) * sizeof(*q->enc_in),
329                         RTE_CACHE_LINE_SIZE, queue_conf->socket);
330         if (q->enc_in == NULL) {
331                 rte_bbdev_log(ERR,
332                         "Failed to allocate queue memory for %s", name);
333                 goto free_q;
334         }
335
336         /* Allocate memory for Alpha Gamma temp buffer. */
337         ret = snprintf(name, RTE_RING_NAMESIZE, RTE_STR(DRIVER_NAME)"_ag%u:%u",
338                         dev->data->dev_id, q_id);
339         if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
340                 rte_bbdev_log(ERR,
341                                 "Creating queue name for device %u queue %u failed",
342                                 dev->data->dev_id, q_id);
343                 return -ENAMETOOLONG;
344         }
345         q->ag = rte_zmalloc_socket(name,
346                         RTE_BBDEV_TURBO_MAX_CB_SIZE * 10 * sizeof(*q->ag),
347                         RTE_CACHE_LINE_SIZE, queue_conf->socket);
348         if (q->ag == NULL) {
349                 rte_bbdev_log(ERR,
350                         "Failed to allocate queue memory for %s", name);
351                 goto free_q;
352         }
353
354         /* Allocate memory for code block temp buffer. */
355         ret = snprintf(name, RTE_RING_NAMESIZE, RTE_STR(DRIVER_NAME)"_cb%u:%u",
356                         dev->data->dev_id, q_id);
357         if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
358                 rte_bbdev_log(ERR,
359                                 "Creating queue name for device %u queue %u failed",
360                                 dev->data->dev_id, q_id);
361                 return -ENAMETOOLONG;
362         }
363         q->code_block = rte_zmalloc_socket(name,
364                         RTE_BBDEV_TURBO_MAX_CB_SIZE * sizeof(*q->code_block),
365                         RTE_CACHE_LINE_SIZE, queue_conf->socket);
366         if (q->code_block == NULL) {
367                 rte_bbdev_log(ERR,
368                         "Failed to allocate queue memory for %s", name);
369                 goto free_q;
370         }
371
372         /* Allocate memory for Deinterleaver input. */
373         ret = snprintf(name, RTE_RING_NAMESIZE,
374                         RTE_STR(DRIVER_NAME)"_de_i%u:%u",
375                         dev->data->dev_id, q_id);
376         if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
377                 rte_bbdev_log(ERR,
378                                 "Creating queue name for device %u queue %u failed",
379                                 dev->data->dev_id, q_id);
380                 return -ENAMETOOLONG;
381         }
382         q->deint_input = rte_zmalloc_socket(name,
383                         DEINT_INPUT_BUF_SIZE * sizeof(*q->deint_input),
384                         RTE_CACHE_LINE_SIZE, queue_conf->socket);
385         if (q->deint_input == NULL) {
386                 rte_bbdev_log(ERR,
387                         "Failed to allocate queue memory for %s", name);
388                 goto free_q;
389         }
390
391         /* Allocate memory for Deinterleaver output. */
392         ret = snprintf(name, RTE_RING_NAMESIZE,
393                         RTE_STR(DRIVER_NAME)"_de_o%u:%u",
394                         dev->data->dev_id, q_id);
395         if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
396                 rte_bbdev_log(ERR,
397                                 "Creating queue name for device %u queue %u failed",
398                                 dev->data->dev_id, q_id);
399                 return -ENAMETOOLONG;
400         }
401         q->deint_output = rte_zmalloc_socket(NULL,
402                         DEINT_OUTPUT_BUF_SIZE * sizeof(*q->deint_output),
403                         RTE_CACHE_LINE_SIZE, queue_conf->socket);
404         if (q->deint_output == NULL) {
405                 rte_bbdev_log(ERR,
406                         "Failed to allocate queue memory for %s", name);
407                 goto free_q;
408         }
409
410         /* Allocate memory for Adapter output. */
411         ret = snprintf(name, RTE_RING_NAMESIZE,
412                         RTE_STR(DRIVER_NAME)"_ada_o%u:%u",
413                         dev->data->dev_id, q_id);
414         if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
415                 rte_bbdev_log(ERR,
416                                 "Creating queue name for device %u queue %u failed",
417                                 dev->data->dev_id, q_id);
418                 return -ENAMETOOLONG;
419         }
420         q->adapter_output = rte_zmalloc_socket(NULL,
421                         ADAPTER_OUTPUT_BUF_SIZE * sizeof(*q->adapter_output),
422                         RTE_CACHE_LINE_SIZE, queue_conf->socket);
423         if (q->adapter_output == NULL) {
424                 rte_bbdev_log(ERR,
425                         "Failed to allocate queue memory for %s", name);
426                 goto free_q;
427         }
428
429         /* Create ring for packets awaiting to be dequeued. */
430         ret = snprintf(name, RTE_RING_NAMESIZE, RTE_STR(DRIVER_NAME)"%u:%u",
431                         dev->data->dev_id, q_id);
432         if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
433                 rte_bbdev_log(ERR,
434                                 "Creating queue name for device %u queue %u failed",
435                                 dev->data->dev_id, q_id);
436                 return -ENAMETOOLONG;
437         }
438         q->processed_pkts = rte_ring_create(name, queue_conf->queue_size,
439                         queue_conf->socket, RING_F_SP_ENQ | RING_F_SC_DEQ);
440         if (q->processed_pkts == NULL) {
441                 rte_bbdev_log(ERR, "Failed to create ring for %s", name);
442                 goto free_q;
443         }
444
445         q->type = queue_conf->op_type;
446
447         dev->data->queues[q_id].queue_private = q;
448         rte_bbdev_log_debug("setup device queue %s", name);
449         return 0;
450
451 free_q:
452         rte_ring_free(q->processed_pkts);
453         rte_free(q->enc_out);
454         rte_free(q->enc_in);
455         rte_free(q->ag);
456         rte_free(q->code_block);
457         rte_free(q->deint_input);
458         rte_free(q->deint_output);
459         rte_free(q->adapter_output);
460         rte_free(q);
461         return -EFAULT;
462 }
463
464 static const struct rte_bbdev_ops pmd_ops = {
465         .info_get = info_get,
466         .queue_setup = q_setup,
467         .queue_release = q_release
468 };
469
470 #ifdef RTE_BBDEV_SDK_AVX2
471 #ifdef RTE_LIBRTE_BBDEV_DEBUG
472 /* Checks if the encoder input buffer is correct.
473  * Returns 0 if it's valid, -1 otherwise.
474  */
475 static inline int
476 is_enc_input_valid(const uint16_t k, const int32_t k_idx,
477                 const uint16_t in_length)
478 {
479         if (k_idx < 0) {
480                 rte_bbdev_log(ERR, "K Index is invalid");
481                 return -1;
482         }
483
484         if (in_length - (k >> 3) < 0) {
485                 rte_bbdev_log(ERR,
486                                 "Mismatch between input length (%u bytes) and K (%u bits)",
487                                 in_length, k);
488                 return -1;
489         }
490
491         if (k > RTE_BBDEV_TURBO_MAX_CB_SIZE) {
492                 rte_bbdev_log(ERR, "CB size (%u) is too big, max: %d",
493                                 k, RTE_BBDEV_TURBO_MAX_CB_SIZE);
494                 return -1;
495         }
496
497         return 0;
498 }
499
500 /* Checks if the decoder input buffer is correct.
501  * Returns 0 if it's valid, -1 otherwise.
502  */
503 static inline int
504 is_dec_input_valid(int32_t k_idx, int16_t kw, int16_t in_length)
505 {
506         if (k_idx < 0) {
507                 rte_bbdev_log(ERR, "K index is invalid");
508                 return -1;
509         }
510
511         if (in_length < kw) {
512                 rte_bbdev_log(ERR,
513                                 "Mismatch between input length (%u) and kw (%u)",
514                                 in_length, kw);
515                 return -1;
516         }
517
518         if (kw > RTE_BBDEV_TURBO_MAX_KW) {
519                 rte_bbdev_log(ERR, "Input length (%u) is too big, max: %d",
520                                 kw, RTE_BBDEV_TURBO_MAX_KW);
521                 return -1;
522         }
523
524         return 0;
525 }
526 #endif
527 #endif
528
529 static inline void
530 process_enc_cb(struct turbo_sw_queue *q, struct rte_bbdev_enc_op *op,
531                 uint8_t r, uint8_t c, uint16_t k, uint16_t ncb,
532                 uint32_t e, struct rte_mbuf *m_in, struct rte_mbuf *m_out_head,
533                 struct rte_mbuf *m_out, uint16_t in_offset, uint16_t out_offset,
534                 uint16_t in_length, struct rte_bbdev_stats *q_stats)
535 {
536 #ifdef RTE_BBDEV_SDK_AVX2
537 #ifdef RTE_LIBRTE_BBDEV_DEBUG
538         int ret;
539 #else
540         RTE_SET_USED(in_length);
541 #endif
542         int16_t k_idx;
543         uint16_t m;
544         uint8_t *in, *out0, *out1, *out2, *tmp_out, *rm_out;
545         uint64_t first_3_bytes = 0;
546         struct rte_bbdev_op_turbo_enc *enc = &op->turbo_enc;
547         struct bblib_crc_request crc_req;
548         struct bblib_crc_response crc_resp;
549         struct bblib_turbo_encoder_request turbo_req;
550         struct bblib_turbo_encoder_response turbo_resp;
551         struct bblib_rate_match_dl_request rm_req;
552         struct bblib_rate_match_dl_response rm_resp;
553 #ifdef RTE_BBDEV_OFFLOAD_COST
554         uint64_t start_time;
555 #else
556         RTE_SET_USED(q_stats);
557 #endif
558
559         k_idx = compute_idx(k);
560         in = rte_pktmbuf_mtod_offset(m_in, uint8_t *, in_offset);
561
562         /* CRC24A (for TB) */
563         if ((enc->op_flags & RTE_BBDEV_TURBO_CRC_24A_ATTACH) &&
564                 (enc->code_block_mode == 1)) {
565 #ifdef RTE_LIBRTE_BBDEV_DEBUG
566                 ret = is_enc_input_valid(k - 24, k_idx, in_length);
567                 if (ret != 0) {
568                         op->status |= 1 << RTE_BBDEV_DATA_ERROR;
569                         return;
570                 }
571 #endif
572
573                 crc_req.data = in;
574                 crc_req.len = k - 24;
575                 /* Check if there is a room for CRC bits if not use
576                  * the temporary buffer.
577                  */
578                 if (mbuf_append(m_in, m_in, 3) == NULL) {
579                         rte_memcpy(q->enc_in, in, (k - 24) >> 3);
580                         in = q->enc_in;
581                 } else {
582                         /* Store 3 first bytes of next CB as they will be
583                          * overwritten by CRC bytes. If it is the last CB then
584                          * there is no point to store 3 next bytes and this
585                          * if..else branch will be omitted.
586                          */
587                         first_3_bytes = *((uint64_t *)&in[(k - 32) >> 3]);
588                 }
589
590                 crc_resp.data = in;
591 #ifdef RTE_BBDEV_OFFLOAD_COST
592                 start_time = rte_rdtsc_precise();
593 #endif
594                 /* CRC24A generation */
595                 bblib_lte_crc24a_gen(&crc_req, &crc_resp);
596 #ifdef RTE_BBDEV_OFFLOAD_COST
597                 q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
598 #endif
599         } else if (enc->op_flags & RTE_BBDEV_TURBO_CRC_24B_ATTACH) {
600                 /* CRC24B */
601 #ifdef RTE_LIBRTE_BBDEV_DEBUG
602                 ret = is_enc_input_valid(k - 24, k_idx, in_length);
603                 if (ret != 0) {
604                         op->status |= 1 << RTE_BBDEV_DATA_ERROR;
605                         return;
606                 }
607 #endif
608
609                 crc_req.data = in;
610                 crc_req.len = k - 24;
611                 /* Check if there is a room for CRC bits if this is the last
612                  * CB in TB. If not use temporary buffer.
613                  */
614                 if ((c - r == 1) && (mbuf_append(m_in, m_in, 3) == NULL)) {
615                         rte_memcpy(q->enc_in, in, (k - 24) >> 3);
616                         in = q->enc_in;
617                 } else if (c - r > 1) {
618                         /* Store 3 first bytes of next CB as they will be
619                          * overwritten by CRC bytes. If it is the last CB then
620                          * there is no point to store 3 next bytes and this
621                          * if..else branch will be omitted.
622                          */
623                         first_3_bytes = *((uint64_t *)&in[(k - 32) >> 3]);
624                 }
625
626                 crc_resp.data = in;
627 #ifdef RTE_BBDEV_OFFLOAD_COST
628                 start_time = rte_rdtsc_precise();
629 #endif
630                 /* CRC24B generation */
631                 bblib_lte_crc24b_gen(&crc_req, &crc_resp);
632 #ifdef RTE_BBDEV_OFFLOAD_COST
633                 q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
634 #endif
635         }
636 #ifdef RTE_LIBRTE_BBDEV_DEBUG
637         else {
638                 ret = is_enc_input_valid(k, k_idx, in_length);
639                 if (ret != 0) {
640                         op->status |= 1 << RTE_BBDEV_DATA_ERROR;
641                         return;
642                 }
643         }
644 #endif
645
646         /* Turbo encoder */
647
648         /* Each bit layer output from turbo encoder is (k+4) bits long, i.e.
649          * input length + 4 tail bits. That's (k/8) + 1 bytes after rounding up.
650          * So dst_data's length should be 3*(k/8) + 3 bytes.
651          * In Rate-matching bypass case outputs pointers passed to encoder
652          * (out0, out1 and out2) can directly point to addresses of output from
653          * turbo_enc entity.
654          */
655         if (enc->op_flags & RTE_BBDEV_TURBO_RATE_MATCH) {
656                 out0 = q->enc_out;
657                 out1 = RTE_PTR_ADD(out0, (k >> 3) + 1);
658                 out2 = RTE_PTR_ADD(out1, (k >> 3) + 1);
659         } else {
660                 out0 = (uint8_t *)mbuf_append(m_out_head, m_out,
661                                 (k >> 3) * 3 + 2);
662                 if (out0 == NULL) {
663                         op->status |= 1 << RTE_BBDEV_DATA_ERROR;
664                         rte_bbdev_log(ERR,
665                                         "Too little space in output mbuf");
666                         return;
667                 }
668                 enc->output.length += (k >> 3) * 3 + 2;
669                 /* rte_bbdev_op_data.offset can be different than the
670                  * offset of the appended bytes
671                  */
672                 out0 = rte_pktmbuf_mtod_offset(m_out, uint8_t *, out_offset);
673                 out1 = rte_pktmbuf_mtod_offset(m_out, uint8_t *,
674                                 out_offset + (k >> 3) + 1);
675                 out2 = rte_pktmbuf_mtod_offset(m_out, uint8_t *,
676                                 out_offset + 2 * ((k >> 3) + 1));
677         }
678
679         turbo_req.case_id = k_idx;
680         turbo_req.input_win = in;
681         turbo_req.length = k >> 3;
682         turbo_resp.output_win_0 = out0;
683         turbo_resp.output_win_1 = out1;
684         turbo_resp.output_win_2 = out2;
685
686 #ifdef RTE_BBDEV_OFFLOAD_COST
687         start_time = rte_rdtsc_precise();
688 #endif
689         /* Turbo encoding */
690         if (bblib_turbo_encoder(&turbo_req, &turbo_resp) != 0) {
691                 op->status |= 1 << RTE_BBDEV_DRV_ERROR;
692                 rte_bbdev_log(ERR, "Turbo Encoder failed");
693                 return;
694         }
695 #ifdef RTE_BBDEV_OFFLOAD_COST
696         q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
697 #endif
698
699         /* Restore 3 first bytes of next CB if they were overwritten by CRC*/
700         if (first_3_bytes != 0)
701                 *((uint64_t *)&in[(k - 32) >> 3]) = first_3_bytes;
702
703         /* Rate-matching */
704         if (enc->op_flags & RTE_BBDEV_TURBO_RATE_MATCH) {
705                 uint8_t mask_id;
706                 /* Integer round up division by 8 */
707                 uint16_t out_len = (e + 7) >> 3;
708                 /* The mask array is indexed using E%8. E is an even number so
709                  * there are only 4 possible values.
710                  */
711                 const uint8_t mask_out[] = {0xFF, 0xC0, 0xF0, 0xFC};
712
713                 /* get output data starting address */
714                 rm_out = (uint8_t *)mbuf_append(m_out_head, m_out, out_len);
715                 if (rm_out == NULL) {
716                         op->status |= 1 << RTE_BBDEV_DATA_ERROR;
717                         rte_bbdev_log(ERR,
718                                         "Too little space in output mbuf");
719                         return;
720                 }
721                 /* rte_bbdev_op_data.offset can be different than the offset
722                  * of the appended bytes
723                  */
724                 rm_out = rte_pktmbuf_mtod_offset(m_out, uint8_t *, out_offset);
725
726                 /* index of current code block */
727                 rm_req.r = r;
728                 /* total number of code block */
729                 rm_req.C = c;
730                 /* For DL - 1, UL - 0 */
731                 rm_req.direction = 1;
732                 /* According to 3ggp 36.212 Spec 5.1.4.1.2 section Nsoft, KMIMO
733                  * and MDL_HARQ are used for Ncb calculation. As Ncb is already
734                  * known we can adjust those parameters
735                  */
736                 rm_req.Nsoft = ncb * rm_req.C;
737                 rm_req.KMIMO = 1;
738                 rm_req.MDL_HARQ = 1;
739                 /* According to 3ggp 36.212 Spec 5.1.4.1.2 section Nl, Qm and G
740                  * are used for E calculation. As E is already known we can
741                  * adjust those parameters
742                  */
743                 rm_req.NL = e;
744                 rm_req.Qm = 1;
745                 rm_req.G = rm_req.NL * rm_req.Qm * rm_req.C;
746
747                 rm_req.rvidx = enc->rv_index;
748                 rm_req.Kidx = k_idx - 1;
749                 rm_req.nLen = k + 4;
750                 rm_req.tin0 = out0;
751                 rm_req.tin1 = out1;
752                 rm_req.tin2 = out2;
753                 rm_resp.output = rm_out;
754                 rm_resp.OutputLen = out_len;
755                 if (enc->op_flags & RTE_BBDEV_TURBO_RV_INDEX_BYPASS)
756                         rm_req.bypass_rvidx = 1;
757                 else
758                         rm_req.bypass_rvidx = 0;
759
760 #ifdef RTE_BBDEV_OFFLOAD_COST
761                 start_time = rte_rdtsc_precise();
762 #endif
763                 /* Rate-Matching */
764                 if (bblib_rate_match_dl(&rm_req, &rm_resp) != 0) {
765                         op->status |= 1 << RTE_BBDEV_DRV_ERROR;
766                         rte_bbdev_log(ERR, "Rate matching failed");
767                         return;
768                 }
769 #ifdef RTE_BBDEV_OFFLOAD_COST
770                 q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
771 #endif
772
773                 /* SW fills an entire last byte even if E%8 != 0. Clear the
774                  * superfluous data bits for consistency with HW device.
775                  */
776                 mask_id = (e & 7) >> 1;
777                 rm_out[out_len - 1] &= mask_out[mask_id];
778                 enc->output.length += rm_resp.OutputLen;
779         } else {
780                 /* Rate matching is bypassed */
781
782                 /* Completing last byte of out0 (where 4 tail bits are stored)
783                  * by moving first 4 bits from out1
784                  */
785                 tmp_out = (uint8_t *) --out1;
786                 *tmp_out = *tmp_out | ((*(tmp_out + 1) & 0xF0) >> 4);
787                 tmp_out++;
788                 /* Shifting out1 data by 4 bits to the left */
789                 for (m = 0; m < k >> 3; ++m) {
790                         uint8_t *first = tmp_out;
791                         uint8_t second = *(tmp_out + 1);
792                         *first = (*first << 4) | ((second & 0xF0) >> 4);
793                         tmp_out++;
794                 }
795                 /* Shifting out2 data by 8 bits to the left */
796                 for (m = 0; m < (k >> 3) + 1; ++m) {
797                         *tmp_out = *(tmp_out + 1);
798                         tmp_out++;
799                 }
800                 *tmp_out = 0;
801         }
802 #else
803         RTE_SET_USED(q);
804         RTE_SET_USED(op);
805         RTE_SET_USED(r);
806         RTE_SET_USED(c);
807         RTE_SET_USED(k);
808         RTE_SET_USED(ncb);
809         RTE_SET_USED(e);
810         RTE_SET_USED(m_in);
811         RTE_SET_USED(m_out_head);
812         RTE_SET_USED(m_out);
813         RTE_SET_USED(in_offset);
814         RTE_SET_USED(out_offset);
815         RTE_SET_USED(in_length);
816         RTE_SET_USED(q_stats);
817 #endif
818 }
819
820
821 static inline void
822 process_ldpc_enc_cb(struct turbo_sw_queue *q, struct rte_bbdev_enc_op *op,
823                 uint32_t e, struct rte_mbuf *m_in, struct rte_mbuf *m_out_head,
824                 struct rte_mbuf *m_out, uint16_t in_offset, uint16_t out_offset,
825                 uint16_t seg_total_left, struct rte_bbdev_stats *q_stats)
826 {
827 #ifdef RTE_BBDEV_SDK_AVX512
828         RTE_SET_USED(seg_total_left);
829         uint8_t *in, *rm_out;
830         struct rte_bbdev_op_ldpc_enc *enc = &op->ldpc_enc;
831         struct bblib_ldpc_encoder_5gnr_request ldpc_req;
832         struct bblib_ldpc_encoder_5gnr_response ldpc_resp;
833         struct bblib_LDPC_ratematch_5gnr_request rm_req;
834         struct bblib_LDPC_ratematch_5gnr_response rm_resp;
835         struct bblib_crc_request crc_req;
836         struct bblib_crc_response crc_resp;
837         uint16_t msgLen, puntBits, parity_offset, out_len;
838         uint16_t K = (enc->basegraph == 1 ? 22 : 10) * enc->z_c;
839         uint16_t in_length_in_bits = K - enc->n_filler;
840         uint16_t in_length_in_bytes = (in_length_in_bits + 7) >> 3;
841
842 #ifdef RTE_BBDEV_OFFLOAD_COST
843         uint64_t start_time = rte_rdtsc_precise();
844 #else
845         RTE_SET_USED(q_stats);
846 #endif
847
848         in = rte_pktmbuf_mtod_offset(m_in, uint8_t *, in_offset);
849
850         /* Masking the Filler bits explicitly */
851         memset(q->enc_in  + (in_length_in_bytes - 3), 0,
852                         ((K + 7) >> 3) - (in_length_in_bytes - 3));
853         /* CRC Generation */
854         if (enc->op_flags & RTE_BBDEV_LDPC_CRC_24A_ATTACH) {
855                 rte_memcpy(q->enc_in, in, in_length_in_bytes - 3);
856                 crc_req.data = in;
857                 crc_req.len = in_length_in_bits - 24;
858                 crc_resp.data = q->enc_in;
859                 bblib_lte_crc24a_gen(&crc_req, &crc_resp);
860         } else if (enc->op_flags & RTE_BBDEV_LDPC_CRC_24B_ATTACH) {
861                 rte_memcpy(q->enc_in, in, in_length_in_bytes - 3);
862                 crc_req.data = in;
863                 crc_req.len = in_length_in_bits - 24;
864                 crc_resp.data = q->enc_in;
865                 bblib_lte_crc24b_gen(&crc_req, &crc_resp);
866         } else
867                 rte_memcpy(q->enc_in, in, in_length_in_bytes);
868
869         /* LDPC Encoding */
870         ldpc_req.Zc = enc->z_c;
871         ldpc_req.baseGraph = enc->basegraph;
872         /* Number of rows set to maximum */
873         ldpc_req.nRows = ldpc_req.baseGraph == 1 ? 46 : 42;
874         ldpc_req.numberCodeblocks = 1;
875         ldpc_req.input[0] = (int8_t *) q->enc_in;
876         ldpc_resp.output[0] = (int8_t *) q->enc_out;
877
878         bblib_bit_reverse(ldpc_req.input[0], in_length_in_bytes << 3);
879
880         if (bblib_ldpc_encoder_5gnr(&ldpc_req, &ldpc_resp) != 0) {
881                 op->status |= 1 << RTE_BBDEV_DRV_ERROR;
882                 rte_bbdev_log(ERR, "LDPC Encoder failed");
883                 return;
884         }
885
886         /*
887          * Systematic + Parity : Recreating stream with filler bits, ideally
888          * the bit select could handle this in the RM SDK
889          */
890         msgLen = (ldpc_req.baseGraph == 1 ? 22 : 10) * ldpc_req.Zc;
891         puntBits = 2 * ldpc_req.Zc;
892         parity_offset = msgLen - puntBits;
893         ippsCopyBE_1u(((uint8_t *) ldpc_req.input[0]) + (puntBits / 8),
894                         puntBits%8, q->adapter_output, 0, parity_offset);
895         ippsCopyBE_1u(q->enc_out, 0, q->adapter_output + (parity_offset / 8),
896                         parity_offset % 8, ldpc_req.nRows * ldpc_req.Zc);
897
898         out_len = (e + 7) >> 3;
899         /* get output data starting address */
900         rm_out = (uint8_t *)mbuf_append(m_out_head, m_out, out_len);
901         if (rm_out == NULL) {
902                 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
903                 rte_bbdev_log(ERR,
904                                 "Too little space in output mbuf");
905                 return;
906         }
907         /*
908          * rte_bbdev_op_data.offset can be different than the offset
909          * of the appended bytes
910          */
911         rm_out = rte_pktmbuf_mtod_offset(m_out, uint8_t *, out_offset);
912
913         /* Rate-Matching */
914         rm_req.E = e;
915         rm_req.Ncb = enc->n_cb;
916         rm_req.Qm = enc->q_m;
917         rm_req.Zc = enc->z_c;
918         rm_req.baseGraph = enc->basegraph;
919         rm_req.input = q->adapter_output;
920         rm_req.nLen = enc->n_filler;
921         rm_req.nullIndex = parity_offset - enc->n_filler;
922         rm_req.rvidx = enc->rv_index;
923         rm_resp.output = q->deint_output;
924
925         if (bblib_LDPC_ratematch_5gnr(&rm_req, &rm_resp) != 0) {
926                 op->status |= 1 << RTE_BBDEV_DRV_ERROR;
927                 rte_bbdev_log(ERR, "Rate matching failed");
928                 return;
929         }
930
931         /* RM SDK may provide non zero bits on last byte */
932         if ((e % 8) != 0)
933                 q->deint_output[out_len-1] &= (1 << (e % 8)) - 1;
934
935         bblib_bit_reverse((int8_t *) q->deint_output, out_len << 3);
936
937         rte_memcpy(rm_out, q->deint_output, out_len);
938         enc->output.length += out_len;
939
940 #ifdef RTE_BBDEV_OFFLOAD_COST
941         q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
942 #endif
943 #else
944         RTE_SET_USED(q);
945         RTE_SET_USED(op);
946         RTE_SET_USED(e);
947         RTE_SET_USED(m_in);
948         RTE_SET_USED(m_out_head);
949         RTE_SET_USED(m_out);
950         RTE_SET_USED(in_offset);
951         RTE_SET_USED(out_offset);
952         RTE_SET_USED(seg_total_left);
953         RTE_SET_USED(q_stats);
954 #endif
955 }
956
957 static inline void
958 enqueue_enc_one_op(struct turbo_sw_queue *q, struct rte_bbdev_enc_op *op,
959                 struct rte_bbdev_stats *queue_stats)
960 {
961         uint8_t c, r, crc24_bits = 0;
962         uint16_t k, ncb;
963         uint32_t e;
964         struct rte_bbdev_op_turbo_enc *enc = &op->turbo_enc;
965         uint16_t in_offset = enc->input.offset;
966         uint16_t out_offset = enc->output.offset;
967         struct rte_mbuf *m_in = enc->input.data;
968         struct rte_mbuf *m_out = enc->output.data;
969         struct rte_mbuf *m_out_head = enc->output.data;
970         uint32_t in_length, mbuf_total_left = enc->input.length;
971         uint16_t seg_total_left;
972
973         /* Clear op status */
974         op->status = 0;
975
976         if (mbuf_total_left > RTE_BBDEV_TURBO_MAX_TB_SIZE >> 3) {
977                 rte_bbdev_log(ERR, "TB size (%u) is too big, max: %d",
978                                 mbuf_total_left, RTE_BBDEV_TURBO_MAX_TB_SIZE);
979                 op->status = 1 << RTE_BBDEV_DATA_ERROR;
980                 return;
981         }
982
983         if (m_in == NULL || m_out == NULL) {
984                 rte_bbdev_log(ERR, "Invalid mbuf pointer");
985                 op->status = 1 << RTE_BBDEV_DATA_ERROR;
986                 return;
987         }
988
989         if ((enc->op_flags & RTE_BBDEV_TURBO_CRC_24B_ATTACH) ||
990                 (enc->op_flags & RTE_BBDEV_TURBO_CRC_24A_ATTACH))
991                 crc24_bits = 24;
992
993         if (enc->code_block_mode == 0) { /* For Transport Block mode */
994                 c = enc->tb_params.c;
995                 r = enc->tb_params.r;
996         } else {/* For Code Block mode */
997                 c = 1;
998                 r = 0;
999         }
1000
1001         while (mbuf_total_left > 0 && r < c) {
1002
1003                 seg_total_left = rte_pktmbuf_data_len(m_in) - in_offset;
1004
1005                 if (enc->code_block_mode == 0) {
1006                         k = (r < enc->tb_params.c_neg) ?
1007                                 enc->tb_params.k_neg : enc->tb_params.k_pos;
1008                         ncb = (r < enc->tb_params.c_neg) ?
1009                                 enc->tb_params.ncb_neg : enc->tb_params.ncb_pos;
1010                         e = (r < enc->tb_params.cab) ?
1011                                 enc->tb_params.ea : enc->tb_params.eb;
1012                 } else {
1013                         k = enc->cb_params.k;
1014                         ncb = enc->cb_params.ncb;
1015                         e = enc->cb_params.e;
1016                 }
1017
1018                 process_enc_cb(q, op, r, c, k, ncb, e, m_in, m_out_head,
1019                                 m_out, in_offset, out_offset, seg_total_left,
1020                                 queue_stats);
1021                 /* Update total_left */
1022                 in_length = ((k - crc24_bits) >> 3);
1023                 mbuf_total_left -= in_length;
1024                 /* Update offsets for next CBs (if exist) */
1025                 in_offset += (k - crc24_bits) >> 3;
1026                 if (enc->op_flags & RTE_BBDEV_TURBO_RATE_MATCH)
1027                         out_offset += e >> 3;
1028                 else
1029                         out_offset += (k >> 3) * 3 + 2;
1030
1031                 /* Update offsets */
1032                 if (seg_total_left == in_length) {
1033                         /* Go to the next mbuf */
1034                         m_in = m_in->next;
1035                         m_out = m_out->next;
1036                         in_offset = 0;
1037                         out_offset = 0;
1038                 }
1039                 r++;
1040         }
1041
1042         /* check if all input data was processed */
1043         if (mbuf_total_left != 0) {
1044                 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1045                 rte_bbdev_log(ERR,
1046                                 "Mismatch between mbuf length and included CBs sizes");
1047         }
1048 }
1049
1050
1051 static inline void
1052 enqueue_ldpc_enc_one_op(struct turbo_sw_queue *q, struct rte_bbdev_enc_op *op,
1053                 struct rte_bbdev_stats *queue_stats)
1054 {
1055         uint8_t c, r, crc24_bits = 0;
1056         uint32_t e;
1057         struct rte_bbdev_op_ldpc_enc *enc = &op->ldpc_enc;
1058         uint16_t in_offset = enc->input.offset;
1059         uint16_t out_offset = enc->output.offset;
1060         struct rte_mbuf *m_in = enc->input.data;
1061         struct rte_mbuf *m_out = enc->output.data;
1062         struct rte_mbuf *m_out_head = enc->output.data;
1063         uint32_t in_length, mbuf_total_left = enc->input.length;
1064
1065         uint16_t seg_total_left;
1066
1067         /* Clear op status */
1068         op->status = 0;
1069
1070         if (mbuf_total_left > RTE_BBDEV_TURBO_MAX_TB_SIZE >> 3) {
1071                 rte_bbdev_log(ERR, "TB size (%u) is too big, max: %d",
1072                                 mbuf_total_left, RTE_BBDEV_TURBO_MAX_TB_SIZE);
1073                 op->status = 1 << RTE_BBDEV_DATA_ERROR;
1074                 return;
1075         }
1076
1077         if (m_in == NULL || m_out == NULL) {
1078                 rte_bbdev_log(ERR, "Invalid mbuf pointer");
1079                 op->status = 1 << RTE_BBDEV_DATA_ERROR;
1080                 return;
1081         }
1082
1083         if ((enc->op_flags & RTE_BBDEV_TURBO_CRC_24B_ATTACH) ||
1084                 (enc->op_flags & RTE_BBDEV_TURBO_CRC_24A_ATTACH))
1085                 crc24_bits = 24;
1086
1087         if (enc->code_block_mode == 0) { /* For Transport Block mode */
1088                 c = enc->tb_params.c;
1089                 r = enc->tb_params.r;
1090         } else { /* For Code Block mode */
1091                 c = 1;
1092                 r = 0;
1093         }
1094
1095         while (mbuf_total_left > 0 && r < c) {
1096
1097                 seg_total_left = rte_pktmbuf_data_len(m_in) - in_offset;
1098
1099                 if (enc->code_block_mode == 0) {
1100                         e = (r < enc->tb_params.cab) ?
1101                                 enc->tb_params.ea : enc->tb_params.eb;
1102                 } else {
1103                         e = enc->cb_params.e;
1104                 }
1105
1106                 process_ldpc_enc_cb(q, op, e, m_in, m_out_head,
1107                                 m_out, in_offset, out_offset, seg_total_left,
1108                                 queue_stats);
1109                 /* Update total_left */
1110                 in_length = (enc->basegraph == 1 ? 22 : 10) * enc->z_c;
1111                 in_length = ((in_length - crc24_bits - enc->n_filler) >> 3);
1112                 mbuf_total_left -= in_length;
1113                 /* Update offsets for next CBs (if exist) */
1114                 in_offset += in_length;
1115                 out_offset += (e + 7) >> 3;
1116
1117                 /* Update offsets */
1118                 if (seg_total_left == in_length) {
1119                         /* Go to the next mbuf */
1120                         m_in = m_in->next;
1121                         m_out = m_out->next;
1122                         in_offset = 0;
1123                         out_offset = 0;
1124                 }
1125                 r++;
1126         }
1127
1128         /* check if all input data was processed */
1129         if (mbuf_total_left != 0) {
1130                 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1131                 rte_bbdev_log(ERR,
1132                                 "Mismatch between mbuf length and included CBs sizes %d",
1133                                 mbuf_total_left);
1134         }
1135 }
1136
1137 static inline uint16_t
1138 enqueue_enc_all_ops(struct turbo_sw_queue *q, struct rte_bbdev_enc_op **ops,
1139                 uint16_t nb_ops, struct rte_bbdev_stats *queue_stats)
1140 {
1141         uint16_t i;
1142 #ifdef RTE_BBDEV_OFFLOAD_COST
1143         queue_stats->acc_offload_cycles = 0;
1144 #endif
1145
1146         for (i = 0; i < nb_ops; ++i)
1147                 enqueue_enc_one_op(q, ops[i], queue_stats);
1148
1149         return rte_ring_enqueue_burst(q->processed_pkts, (void **)ops, nb_ops,
1150                         NULL);
1151 }
1152
1153 static inline uint16_t
1154 enqueue_ldpc_enc_all_ops(struct turbo_sw_queue *q,
1155                 struct rte_bbdev_enc_op **ops,
1156                 uint16_t nb_ops, struct rte_bbdev_stats *queue_stats)
1157 {
1158         uint16_t i;
1159 #ifdef RTE_BBDEV_OFFLOAD_COST
1160         queue_stats->acc_offload_cycles = 0;
1161 #endif
1162
1163         for (i = 0; i < nb_ops; ++i)
1164                 enqueue_ldpc_enc_one_op(q, ops[i], queue_stats);
1165
1166         return rte_ring_enqueue_burst(q->processed_pkts, (void **)ops, nb_ops,
1167                         NULL);
1168 }
1169
1170 #ifdef RTE_BBDEV_SDK_AVX2
1171 static inline void
1172 move_padding_bytes(const uint8_t *in, uint8_t *out, uint16_t k,
1173                 uint16_t ncb)
1174 {
1175         uint16_t d = k + 4;
1176         uint16_t kpi = ncb / 3;
1177         uint16_t nd = kpi - d;
1178
1179         rte_memcpy(&out[nd], in, d);
1180         rte_memcpy(&out[nd + kpi + 64], &in[kpi], d);
1181         rte_memcpy(&out[(nd - 1) + 2 * (kpi + 64)], &in[2 * kpi], d);
1182 }
1183 #endif
1184
1185 static inline void
1186 process_dec_cb(struct turbo_sw_queue *q, struct rte_bbdev_dec_op *op,
1187                 uint8_t c, uint16_t k, uint16_t kw, struct rte_mbuf *m_in,
1188                 struct rte_mbuf *m_out_head, struct rte_mbuf *m_out,
1189                 uint16_t in_offset, uint16_t out_offset, bool check_crc_24b,
1190                 uint16_t crc24_overlap, uint16_t in_length,
1191                 struct rte_bbdev_stats *q_stats)
1192 {
1193 #ifdef RTE_BBDEV_SDK_AVX2
1194 #ifdef RTE_LIBRTE_BBDEV_DEBUG
1195         int ret;
1196 #else
1197         RTE_SET_USED(in_length);
1198 #endif
1199         int32_t k_idx;
1200         int32_t iter_cnt;
1201         uint8_t *in, *out, *adapter_input;
1202         int32_t ncb, ncb_without_null;
1203         struct bblib_turbo_adapter_ul_response adapter_resp;
1204         struct bblib_turbo_adapter_ul_request adapter_req;
1205         struct bblib_turbo_decoder_request turbo_req;
1206         struct bblib_turbo_decoder_response turbo_resp;
1207         struct rte_bbdev_op_turbo_dec *dec = &op->turbo_dec;
1208 #ifdef RTE_BBDEV_OFFLOAD_COST
1209         uint64_t start_time;
1210 #else
1211         RTE_SET_USED(q_stats);
1212 #endif
1213
1214         k_idx = compute_idx(k);
1215
1216 #ifdef RTE_LIBRTE_BBDEV_DEBUG
1217         ret = is_dec_input_valid(k_idx, kw, in_length);
1218         if (ret != 0) {
1219                 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1220                 return;
1221         }
1222 #endif
1223
1224         in = rte_pktmbuf_mtod_offset(m_in, uint8_t *, in_offset);
1225         ncb = kw;
1226         ncb_without_null = (k + 4) * 3;
1227
1228         if (check_bit(dec->op_flags, RTE_BBDEV_TURBO_SUBBLOCK_DEINTERLEAVE)) {
1229                 struct bblib_deinterleave_ul_request deint_req;
1230                 struct bblib_deinterleave_ul_response deint_resp;
1231
1232                 deint_req.circ_buffer = BBLIB_FULL_CIRCULAR_BUFFER;
1233                 deint_req.pharqbuffer = in;
1234                 deint_req.ncb = ncb;
1235                 deint_resp.pinteleavebuffer = q->deint_output;
1236
1237 #ifdef RTE_BBDEV_OFFLOAD_COST
1238         start_time = rte_rdtsc_precise();
1239 #endif
1240                 /* Sub-block De-Interleaving */
1241                 bblib_deinterleave_ul(&deint_req, &deint_resp);
1242 #ifdef RTE_BBDEV_OFFLOAD_COST
1243         q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
1244 #endif
1245         } else
1246                 move_padding_bytes(in, q->deint_output, k, ncb);
1247
1248         adapter_input = q->deint_output;
1249
1250         if (dec->op_flags & RTE_BBDEV_TURBO_POS_LLR_1_BIT_IN)
1251                 adapter_req.isinverted = 1;
1252         else if (dec->op_flags & RTE_BBDEV_TURBO_NEG_LLR_1_BIT_IN)
1253                 adapter_req.isinverted = 0;
1254         else {
1255                 op->status |= 1 << RTE_BBDEV_DRV_ERROR;
1256                 rte_bbdev_log(ERR, "LLR format wasn't specified");
1257                 return;
1258         }
1259
1260         adapter_req.ncb = ncb_without_null;
1261         adapter_req.pinteleavebuffer = adapter_input;
1262         adapter_resp.pharqout = q->adapter_output;
1263
1264 #ifdef RTE_BBDEV_OFFLOAD_COST
1265         start_time = rte_rdtsc_precise();
1266 #endif
1267         /* Turbo decode adaptation */
1268         bblib_turbo_adapter_ul(&adapter_req, &adapter_resp);
1269 #ifdef RTE_BBDEV_OFFLOAD_COST
1270         q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
1271 #endif
1272
1273         out = (uint8_t *)mbuf_append(m_out_head, m_out,
1274                         ((k - crc24_overlap) >> 3));
1275         if (out == NULL) {
1276                 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1277                 rte_bbdev_log(ERR, "Too little space in output mbuf");
1278                 return;
1279         }
1280         /* rte_bbdev_op_data.offset can be different than the offset of the
1281          * appended bytes
1282          */
1283         out = rte_pktmbuf_mtod_offset(m_out, uint8_t *, out_offset);
1284         if (check_crc_24b)
1285                 turbo_req.c = c + 1;
1286         else
1287                 turbo_req.c = c;
1288         turbo_req.input = (int8_t *)q->adapter_output;
1289         turbo_req.k = k;
1290         turbo_req.k_idx = k_idx;
1291         turbo_req.max_iter_num = dec->iter_max;
1292         turbo_req.early_term_disable = !check_bit(dec->op_flags,
1293                         RTE_BBDEV_TURBO_EARLY_TERMINATION);
1294         turbo_resp.ag_buf = q->ag;
1295         turbo_resp.cb_buf = q->code_block;
1296         turbo_resp.output = out;
1297
1298 #ifdef RTE_BBDEV_OFFLOAD_COST
1299         start_time = rte_rdtsc_precise();
1300 #endif
1301         /* Turbo decode */
1302         iter_cnt = bblib_turbo_decoder(&turbo_req, &turbo_resp);
1303 #ifdef RTE_BBDEV_OFFLOAD_COST
1304         q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
1305 #endif
1306         dec->hard_output.length += (k >> 3);
1307
1308         if (iter_cnt > 0) {
1309                 /* Temporary solution for returned iter_count from SDK */
1310                 iter_cnt = (iter_cnt - 1) >> 1;
1311                 dec->iter_count = RTE_MAX(iter_cnt, dec->iter_count);
1312         } else {
1313                 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1314                 rte_bbdev_log(ERR, "Turbo Decoder failed");
1315                 return;
1316         }
1317 #else
1318         RTE_SET_USED(q);
1319         RTE_SET_USED(op);
1320         RTE_SET_USED(c);
1321         RTE_SET_USED(k);
1322         RTE_SET_USED(kw);
1323         RTE_SET_USED(m_in);
1324         RTE_SET_USED(m_out_head);
1325         RTE_SET_USED(m_out);
1326         RTE_SET_USED(in_offset);
1327         RTE_SET_USED(out_offset);
1328         RTE_SET_USED(check_crc_24b);
1329         RTE_SET_USED(crc24_overlap);
1330         RTE_SET_USED(in_length);
1331         RTE_SET_USED(q_stats);
1332 #endif
1333 }
1334
1335 static inline void
1336 process_ldpc_dec_cb(struct turbo_sw_queue *q, struct rte_bbdev_dec_op *op,
1337                 uint8_t c, uint16_t out_length, uint32_t e,
1338                 struct rte_mbuf *m_in,
1339                 struct rte_mbuf *m_out_head, struct rte_mbuf *m_out,
1340                 struct rte_mbuf *m_harq_in,
1341                 struct rte_mbuf *m_harq_out_head, struct rte_mbuf *m_harq_out,
1342                 uint16_t in_offset, uint16_t out_offset,
1343                 uint16_t harq_in_offset, uint16_t harq_out_offset,
1344                 bool check_crc_24b,
1345                 uint16_t crc24_overlap, uint16_t in_length,
1346                 struct rte_bbdev_stats *q_stats)
1347 {
1348 #ifdef RTE_BBDEV_SDK_AVX512
1349         RTE_SET_USED(in_length);
1350         RTE_SET_USED(c);
1351         uint8_t *in, *out, *harq_in, *harq_out, *adapter_input;
1352         struct bblib_rate_dematching_5gnr_request derm_req;
1353         struct bblib_rate_dematching_5gnr_response derm_resp;
1354         struct bblib_ldpc_decoder_5gnr_request dec_req;
1355         struct bblib_ldpc_decoder_5gnr_response dec_resp;
1356         struct bblib_crc_request crc_req;
1357         struct bblib_crc_response crc_resp;
1358         struct rte_bbdev_op_ldpc_dec *dec = &op->ldpc_dec;
1359         uint16_t K, parity_offset, sys_cols, outLenWithCrc;
1360         int16_t deRmOutSize, numRows;
1361
1362         /* Compute some LDPC BG lengths */
1363         outLenWithCrc = out_length + (crc24_overlap >> 3);
1364         sys_cols = (dec->basegraph == 1) ? 22 : 10;
1365         K = sys_cols * dec->z_c;
1366         parity_offset = K - 2 * dec->z_c;
1367
1368 #ifdef RTE_BBDEV_OFFLOAD_COST
1369         uint64_t start_time = rte_rdtsc_precise();
1370 #else
1371         RTE_SET_USED(q_stats);
1372 #endif
1373
1374         in = rte_pktmbuf_mtod_offset(m_in, uint8_t *, in_offset);
1375
1376         if (check_bit(dec->op_flags, RTE_BBDEV_LDPC_HQ_COMBINE_IN_ENABLE)) {
1377                 /**
1378                  *  Single contiguous block from the first LLR of the
1379                  *  circular buffer.
1380                  */
1381                 harq_in = NULL;
1382                 if (m_harq_in != NULL)
1383                         harq_in = rte_pktmbuf_mtod_offset(m_harq_in,
1384                                 uint8_t *, harq_in_offset);
1385                 if (harq_in == NULL) {
1386                         op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1387                         rte_bbdev_log(ERR, "No space in harq input mbuf");
1388                         return;
1389                 }
1390                 uint16_t harq_in_length = RTE_MIN(
1391                                 dec->harq_combined_input.length,
1392                                 (uint32_t) dec->n_cb);
1393                 memset(q->ag + harq_in_length, 0,
1394                                 dec->n_cb - harq_in_length);
1395                 rte_memcpy(q->ag, harq_in, harq_in_length);
1396         }
1397
1398         derm_req.p_in = (int8_t *) in;
1399         derm_req.p_harq = q->ag; /* This doesn't include the filler bits */
1400         derm_req.base_graph = dec->basegraph;
1401         derm_req.zc = dec->z_c;
1402         derm_req.ncb = dec->n_cb;
1403         derm_req.e = e;
1404         derm_req.k0 = 0; /* Actual output from SDK */
1405         derm_req.isretx = check_bit(dec->op_flags,
1406                         RTE_BBDEV_LDPC_HQ_COMBINE_IN_ENABLE);
1407         derm_req.rvid = dec->rv_index;
1408         derm_req.modulation_order = dec->q_m;
1409         derm_req.start_null_index = parity_offset - dec->n_filler;
1410         derm_req.num_of_null = dec->n_filler;
1411
1412         bblib_rate_dematching_5gnr(&derm_req, &derm_resp);
1413
1414         /* Compute RM out size and number of rows */
1415         deRmOutSize = RTE_MIN(
1416                         derm_req.k0 + derm_req.e -
1417                         ((derm_req.k0 < derm_req.start_null_index) ?
1418                                         0 : dec->n_filler),
1419                         dec->n_cb - dec->n_filler);
1420         if (m_harq_in != NULL)
1421                 deRmOutSize = RTE_MAX(deRmOutSize,
1422                                 RTE_MIN(dec->n_cb - dec->n_filler,
1423                                                 m_harq_in->data_len));
1424         numRows = ((deRmOutSize + dec->n_filler + dec->z_c - 1) / dec->z_c)
1425                         - sys_cols + 2;
1426         numRows = RTE_MAX(4, numRows);
1427
1428         /* get output data starting address */
1429         out = (uint8_t *)mbuf_append(m_out_head, m_out, out_length);
1430         if (out == NULL) {
1431                 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1432                 rte_bbdev_log(ERR,
1433                                 "Too little space in LDPC decoder output mbuf");
1434                 return;
1435         }
1436
1437         /* rte_bbdev_op_data.offset can be different than the offset
1438          * of the appended bytes
1439          */
1440         out = rte_pktmbuf_mtod_offset(m_out, uint8_t *, out_offset);
1441         adapter_input = q->enc_out;
1442
1443         dec_req.Zc = dec->z_c;
1444         dec_req.baseGraph = dec->basegraph;
1445         dec_req.nRows = numRows;
1446         dec_req.numChannelLlrs = deRmOutSize;
1447         dec_req.varNodes = derm_req.p_harq;
1448         dec_req.numFillerBits = dec->n_filler;
1449         dec_req.maxIterations = dec->iter_max;
1450         dec_req.enableEarlyTermination = check_bit(dec->op_flags,
1451                         RTE_BBDEV_LDPC_ITERATION_STOP_ENABLE);
1452         dec_resp.varNodes = (int16_t *) q->adapter_output;
1453         dec_resp.compactedMessageBytes = q->enc_out;
1454
1455         bblib_ldpc_decoder_5gnr(&dec_req, &dec_resp);
1456
1457         dec->iter_count = RTE_MAX(dec_resp.iterationAtTermination,
1458                         dec->iter_count);
1459         if (!dec_resp.parityPassedAtTermination)
1460                 op->status |= 1 << RTE_BBDEV_SYNDROME_ERROR;
1461
1462         bblib_bit_reverse((int8_t *) q->enc_out, outLenWithCrc << 3);
1463
1464         if (check_bit(dec->op_flags, RTE_BBDEV_LDPC_CRC_TYPE_24A_CHECK) ||
1465                         check_bit(dec->op_flags,
1466                                         RTE_BBDEV_LDPC_CRC_TYPE_24B_CHECK)) {
1467                 crc_req.data = adapter_input;
1468                 crc_req.len  = K - dec->n_filler - 24;
1469                 crc_resp.check_passed = false;
1470                 crc_resp.data = adapter_input;
1471                 if (check_crc_24b)
1472                         bblib_lte_crc24b_check(&crc_req, &crc_resp);
1473                 else
1474                         bblib_lte_crc24a_check(&crc_req, &crc_resp);
1475                 if (!crc_resp.check_passed)
1476                         op->status |= 1 << RTE_BBDEV_CRC_ERROR;
1477         }
1478
1479 #ifdef RTE_BBDEV_OFFLOAD_COST
1480         q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
1481 #endif
1482         if (check_bit(dec->op_flags, RTE_BBDEV_LDPC_HQ_COMBINE_OUT_ENABLE)) {
1483                 harq_out = NULL;
1484                 if (m_harq_out != NULL) {
1485                         /* Initialize HARQ data length since we overwrite */
1486                         m_harq_out->data_len = 0;
1487                         /* Check there is enough space
1488                          * in the HARQ outbound buffer
1489                          */
1490                         harq_out = (uint8_t *)mbuf_append(m_harq_out_head,
1491                                         m_harq_out, deRmOutSize);
1492                 }
1493                 if (harq_out == NULL) {
1494                         op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1495                         rte_bbdev_log(ERR, "No space in HARQ output mbuf");
1496                         return;
1497                 }
1498                 /* get output data starting address and overwrite the data */
1499                 harq_out = rte_pktmbuf_mtod_offset(m_harq_out, uint8_t *,
1500                                 harq_out_offset);
1501                 rte_memcpy(harq_out, derm_req.p_harq, deRmOutSize);
1502                 dec->harq_combined_output.length += deRmOutSize;
1503         }
1504
1505         rte_memcpy(out, adapter_input, out_length);
1506         dec->hard_output.length += out_length;
1507 #else
1508         RTE_SET_USED(q);
1509         RTE_SET_USED(op);
1510         RTE_SET_USED(c);
1511         RTE_SET_USED(out_length);
1512         RTE_SET_USED(e);
1513         RTE_SET_USED(m_in);
1514         RTE_SET_USED(m_out_head);
1515         RTE_SET_USED(m_out);
1516         RTE_SET_USED(m_harq_in);
1517         RTE_SET_USED(m_harq_out_head);
1518         RTE_SET_USED(m_harq_out);
1519         RTE_SET_USED(harq_in_offset);
1520         RTE_SET_USED(harq_out_offset);
1521         RTE_SET_USED(in_offset);
1522         RTE_SET_USED(out_offset);
1523         RTE_SET_USED(check_crc_24b);
1524         RTE_SET_USED(crc24_overlap);
1525         RTE_SET_USED(in_length);
1526         RTE_SET_USED(q_stats);
1527 #endif
1528 }
1529
1530
1531 static inline void
1532 enqueue_dec_one_op(struct turbo_sw_queue *q, struct rte_bbdev_dec_op *op,
1533                 struct rte_bbdev_stats *queue_stats)
1534 {
1535         uint8_t c, r = 0;
1536         uint16_t kw, k = 0;
1537         uint16_t crc24_overlap = 0;
1538         struct rte_bbdev_op_turbo_dec *dec = &op->turbo_dec;
1539         struct rte_mbuf *m_in = dec->input.data;
1540         struct rte_mbuf *m_out = dec->hard_output.data;
1541         struct rte_mbuf *m_out_head = dec->hard_output.data;
1542         uint16_t in_offset = dec->input.offset;
1543         uint16_t out_offset = dec->hard_output.offset;
1544         uint32_t mbuf_total_left = dec->input.length;
1545         uint16_t seg_total_left;
1546
1547         /* Clear op status */
1548         op->status = 0;
1549
1550         if (m_in == NULL || m_out == NULL) {
1551                 rte_bbdev_log(ERR, "Invalid mbuf pointer");
1552                 op->status = 1 << RTE_BBDEV_DATA_ERROR;
1553                 return;
1554         }
1555
1556         if (dec->code_block_mode == 0) { /* For Transport Block mode */
1557                 c = dec->tb_params.c;
1558         } else { /* For Code Block mode */
1559                 k = dec->cb_params.k;
1560                 c = 1;
1561         }
1562
1563         if ((c > 1) && !check_bit(dec->op_flags,
1564                 RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP))
1565                 crc24_overlap = 24;
1566
1567         while (mbuf_total_left > 0) {
1568                 if (dec->code_block_mode == 0)
1569                         k = (r < dec->tb_params.c_neg) ?
1570                                 dec->tb_params.k_neg : dec->tb_params.k_pos;
1571
1572                 seg_total_left = rte_pktmbuf_data_len(m_in) - in_offset;
1573
1574                 /* Calculates circular buffer size (Kw).
1575                  * According to 3gpp 36.212 section 5.1.4.2
1576                  *   Kw = 3 * Kpi,
1577                  * where:
1578                  *   Kpi = nCol * nRow
1579                  * where nCol is 32 and nRow can be calculated from:
1580                  *   D =< nCol * nRow
1581                  * where D is the size of each output from turbo encoder block
1582                  * (k + 4).
1583                  */
1584                 kw = RTE_ALIGN_CEIL(k + 4, RTE_BBDEV_TURBO_C_SUBBLOCK) * 3;
1585
1586                 process_dec_cb(q, op, c, k, kw, m_in, m_out_head, m_out,
1587                                 in_offset, out_offset, check_bit(dec->op_flags,
1588                                 RTE_BBDEV_TURBO_CRC_TYPE_24B), crc24_overlap,
1589                                 seg_total_left, queue_stats);
1590
1591                 /* To keep CRC24 attached to end of Code block, use
1592                  * RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP flag as it
1593                  * removed by default once verified.
1594                  */
1595
1596                 mbuf_total_left -= kw;
1597
1598                 /* Update offsets */
1599                 if (seg_total_left == kw) {
1600                         /* Go to the next mbuf */
1601                         m_in = m_in->next;
1602                         m_out = m_out->next;
1603                         in_offset = 0;
1604                         out_offset = 0;
1605                 } else {
1606                         /* Update offsets for next CBs (if exist) */
1607                         in_offset += kw;
1608                         out_offset += ((k - crc24_overlap) >> 3);
1609                 }
1610                 r++;
1611         }
1612 }
1613
1614 static inline void
1615 enqueue_ldpc_dec_one_op(struct turbo_sw_queue *q, struct rte_bbdev_dec_op *op,
1616                 struct rte_bbdev_stats *queue_stats)
1617 {
1618         uint8_t c, r = 0;
1619         uint32_t e;
1620         uint16_t out_length, crc24_overlap = 0;
1621         struct rte_bbdev_op_ldpc_dec *dec = &op->ldpc_dec;
1622         struct rte_mbuf *m_in = dec->input.data;
1623         struct rte_mbuf *m_harq_in = dec->harq_combined_input.data;
1624         struct rte_mbuf *m_harq_out = dec->harq_combined_output.data;
1625         struct rte_mbuf *m_harq_out_head = dec->harq_combined_output.data;
1626         struct rte_mbuf *m_out = dec->hard_output.data;
1627         struct rte_mbuf *m_out_head = dec->hard_output.data;
1628         uint16_t in_offset = dec->input.offset;
1629         uint16_t harq_in_offset = dec->harq_combined_input.offset;
1630         uint16_t harq_out_offset = dec->harq_combined_output.offset;
1631         uint16_t out_offset = dec->hard_output.offset;
1632         uint32_t mbuf_total_left = dec->input.length;
1633         uint16_t seg_total_left;
1634
1635         /* Clear op status */
1636         op->status = 0;
1637
1638         if (m_in == NULL || m_out == NULL) {
1639                 rte_bbdev_log(ERR, "Invalid mbuf pointer");
1640                 op->status = 1 << RTE_BBDEV_DATA_ERROR;
1641                 return;
1642         }
1643
1644         if (dec->code_block_mode == 0) { /* For Transport Block mode */
1645                 c = dec->tb_params.c;
1646                 e = dec->tb_params.ea;
1647         } else { /* For Code Block mode */
1648                 c = 1;
1649                 e = dec->cb_params.e;
1650         }
1651
1652         if (check_bit(dec->op_flags, RTE_BBDEV_LDPC_CRC_TYPE_24B_DROP))
1653                 crc24_overlap = 24;
1654
1655         out_length = (dec->basegraph == 1 ? 22 : 10) * dec->z_c; /* K */
1656         out_length = ((out_length - crc24_overlap - dec->n_filler) >> 3);
1657
1658         while (mbuf_total_left > 0) {
1659                 if (dec->code_block_mode == 0)
1660                         e = (r < dec->tb_params.cab) ?
1661                                 dec->tb_params.ea : dec->tb_params.eb;
1662                 /* Special case handling when overusing mbuf */
1663                 if (e < RTE_BBDEV_LDPC_E_MAX_MBUF)
1664                         seg_total_left = rte_pktmbuf_data_len(m_in) - in_offset;
1665                 else
1666                         seg_total_left = e;
1667
1668                 process_ldpc_dec_cb(q, op, c, out_length, e,
1669                                 m_in, m_out_head, m_out,
1670                                 m_harq_in, m_harq_out_head, m_harq_out,
1671                                 in_offset, out_offset, harq_in_offset,
1672                                 harq_out_offset,
1673                                 check_bit(dec->op_flags,
1674                                 RTE_BBDEV_LDPC_CRC_TYPE_24B_CHECK),
1675                                 crc24_overlap,
1676                                 seg_total_left, queue_stats);
1677
1678                 /* To keep CRC24 attached to end of Code block, use
1679                  * RTE_BBDEV_LDPC_DEC_TB_CRC_24B_KEEP flag as it
1680                  * removed by default once verified.
1681                  */
1682
1683                 mbuf_total_left -= e;
1684
1685                 /* Update offsets */
1686                 if (seg_total_left == e) {
1687                         /* Go to the next mbuf */
1688                         m_in = m_in->next;
1689                         m_out = m_out->next;
1690                         if (m_harq_in != NULL)
1691                                 m_harq_in = m_harq_in->next;
1692                         if (m_harq_out != NULL)
1693                                 m_harq_out = m_harq_out->next;
1694                         in_offset = 0;
1695                         out_offset = 0;
1696                         harq_in_offset = 0;
1697                         harq_out_offset = 0;
1698                 } else {
1699                         /* Update offsets for next CBs (if exist) */
1700                         in_offset += e;
1701                         out_offset += out_length;
1702                 }
1703                 r++;
1704         }
1705 }
1706
1707 static inline uint16_t
1708 enqueue_dec_all_ops(struct turbo_sw_queue *q, struct rte_bbdev_dec_op **ops,
1709                 uint16_t nb_ops, struct rte_bbdev_stats *queue_stats)
1710 {
1711         uint16_t i;
1712 #ifdef RTE_BBDEV_OFFLOAD_COST
1713         queue_stats->acc_offload_cycles = 0;
1714 #endif
1715
1716         for (i = 0; i < nb_ops; ++i)
1717                 enqueue_dec_one_op(q, ops[i], queue_stats);
1718
1719         return rte_ring_enqueue_burst(q->processed_pkts, (void **)ops, nb_ops,
1720                         NULL);
1721 }
1722
1723 static inline uint16_t
1724 enqueue_ldpc_dec_all_ops(struct turbo_sw_queue *q,
1725                 struct rte_bbdev_dec_op **ops,
1726                 uint16_t nb_ops, struct rte_bbdev_stats *queue_stats)
1727 {
1728         uint16_t i;
1729 #ifdef RTE_BBDEV_OFFLOAD_COST
1730         queue_stats->acc_offload_cycles = 0;
1731 #endif
1732
1733         for (i = 0; i < nb_ops; ++i)
1734                 enqueue_ldpc_dec_one_op(q, ops[i], queue_stats);
1735
1736         return rte_ring_enqueue_burst(q->processed_pkts, (void **)ops, nb_ops,
1737                         NULL);
1738 }
1739
1740 /* Enqueue burst */
1741 static uint16_t
1742 enqueue_enc_ops(struct rte_bbdev_queue_data *q_data,
1743                 struct rte_bbdev_enc_op **ops, uint16_t nb_ops)
1744 {
1745         void *queue = q_data->queue_private;
1746         struct turbo_sw_queue *q = queue;
1747         uint16_t nb_enqueued = 0;
1748
1749         nb_enqueued = enqueue_enc_all_ops(q, ops, nb_ops, &q_data->queue_stats);
1750
1751         q_data->queue_stats.enqueue_err_count += nb_ops - nb_enqueued;
1752         q_data->queue_stats.enqueued_count += nb_enqueued;
1753
1754         return nb_enqueued;
1755 }
1756
1757 /* Enqueue burst */
1758 static uint16_t
1759 enqueue_ldpc_enc_ops(struct rte_bbdev_queue_data *q_data,
1760                 struct rte_bbdev_enc_op **ops, uint16_t nb_ops)
1761 {
1762         void *queue = q_data->queue_private;
1763         struct turbo_sw_queue *q = queue;
1764         uint16_t nb_enqueued = 0;
1765
1766         nb_enqueued = enqueue_ldpc_enc_all_ops(
1767                         q, ops, nb_ops, &q_data->queue_stats);
1768
1769         q_data->queue_stats.enqueue_err_count += nb_ops - nb_enqueued;
1770         q_data->queue_stats.enqueued_count += nb_enqueued;
1771
1772         return nb_enqueued;
1773 }
1774
1775 /* Enqueue burst */
1776 static uint16_t
1777 enqueue_dec_ops(struct rte_bbdev_queue_data *q_data,
1778                  struct rte_bbdev_dec_op **ops, uint16_t nb_ops)
1779 {
1780         void *queue = q_data->queue_private;
1781         struct turbo_sw_queue *q = queue;
1782         uint16_t nb_enqueued = 0;
1783
1784         nb_enqueued = enqueue_dec_all_ops(q, ops, nb_ops, &q_data->queue_stats);
1785
1786         q_data->queue_stats.enqueue_err_count += nb_ops - nb_enqueued;
1787         q_data->queue_stats.enqueued_count += nb_enqueued;
1788
1789         return nb_enqueued;
1790 }
1791
1792 /* Enqueue burst */
1793 static uint16_t
1794 enqueue_ldpc_dec_ops(struct rte_bbdev_queue_data *q_data,
1795                  struct rte_bbdev_dec_op **ops, uint16_t nb_ops)
1796 {
1797         void *queue = q_data->queue_private;
1798         struct turbo_sw_queue *q = queue;
1799         uint16_t nb_enqueued = 0;
1800
1801         nb_enqueued = enqueue_ldpc_dec_all_ops(q, ops, nb_ops,
1802                         &q_data->queue_stats);
1803
1804         q_data->queue_stats.enqueue_err_count += nb_ops - nb_enqueued;
1805         q_data->queue_stats.enqueued_count += nb_enqueued;
1806
1807         return nb_enqueued;
1808 }
1809
1810 /* Dequeue decode burst */
1811 static uint16_t
1812 dequeue_dec_ops(struct rte_bbdev_queue_data *q_data,
1813                 struct rte_bbdev_dec_op **ops, uint16_t nb_ops)
1814 {
1815         struct turbo_sw_queue *q = q_data->queue_private;
1816         uint16_t nb_dequeued = rte_ring_dequeue_burst(q->processed_pkts,
1817                         (void **)ops, nb_ops, NULL);
1818         q_data->queue_stats.dequeued_count += nb_dequeued;
1819
1820         return nb_dequeued;
1821 }
1822
1823 /* Dequeue encode burst */
1824 static uint16_t
1825 dequeue_enc_ops(struct rte_bbdev_queue_data *q_data,
1826                 struct rte_bbdev_enc_op **ops, uint16_t nb_ops)
1827 {
1828         struct turbo_sw_queue *q = q_data->queue_private;
1829         uint16_t nb_dequeued = rte_ring_dequeue_burst(q->processed_pkts,
1830                         (void **)ops, nb_ops, NULL);
1831         q_data->queue_stats.dequeued_count += nb_dequeued;
1832
1833         return nb_dequeued;
1834 }
1835
1836 /* Parse 16bit integer from string argument */
1837 static inline int
1838 parse_u16_arg(const char *key, const char *value, void *extra_args)
1839 {
1840         uint16_t *u16 = extra_args;
1841         unsigned int long result;
1842
1843         if ((value == NULL) || (extra_args == NULL))
1844                 return -EINVAL;
1845         errno = 0;
1846         result = strtoul(value, NULL, 0);
1847         if ((result >= (1 << 16)) || (errno != 0)) {
1848                 rte_bbdev_log(ERR, "Invalid value %lu for %s", result, key);
1849                 return -ERANGE;
1850         }
1851         *u16 = (uint16_t)result;
1852         return 0;
1853 }
1854
1855 /* Parse parameters used to create device */
1856 static int
1857 parse_turbo_sw_params(struct turbo_sw_params *params, const char *input_args)
1858 {
1859         struct rte_kvargs *kvlist = NULL;
1860         int ret = 0;
1861
1862         if (params == NULL)
1863                 return -EINVAL;
1864         if (input_args) {
1865                 kvlist = rte_kvargs_parse(input_args, turbo_sw_valid_params);
1866                 if (kvlist == NULL)
1867                         return -EFAULT;
1868
1869                 ret = rte_kvargs_process(kvlist, turbo_sw_valid_params[0],
1870                                         &parse_u16_arg, &params->queues_num);
1871                 if (ret < 0)
1872                         goto exit;
1873
1874                 ret = rte_kvargs_process(kvlist, turbo_sw_valid_params[1],
1875                                         &parse_u16_arg, &params->socket_id);
1876                 if (ret < 0)
1877                         goto exit;
1878
1879                 if (params->socket_id >= RTE_MAX_NUMA_NODES) {
1880                         rte_bbdev_log(ERR, "Invalid socket, must be < %u",
1881                                         RTE_MAX_NUMA_NODES);
1882                         goto exit;
1883                 }
1884         }
1885
1886 exit:
1887         if (kvlist)
1888                 rte_kvargs_free(kvlist);
1889         return ret;
1890 }
1891
1892 /* Create device */
1893 static int
1894 turbo_sw_bbdev_create(struct rte_vdev_device *vdev,
1895                 struct turbo_sw_params *init_params)
1896 {
1897         struct rte_bbdev *bbdev;
1898         const char *name = rte_vdev_device_name(vdev);
1899
1900         bbdev = rte_bbdev_allocate(name);
1901         if (bbdev == NULL)
1902                 return -ENODEV;
1903
1904         bbdev->data->dev_private = rte_zmalloc_socket(name,
1905                         sizeof(struct bbdev_private), RTE_CACHE_LINE_SIZE,
1906                         init_params->socket_id);
1907         if (bbdev->data->dev_private == NULL) {
1908                 rte_bbdev_release(bbdev);
1909                 return -ENOMEM;
1910         }
1911
1912         bbdev->dev_ops = &pmd_ops;
1913         bbdev->device = &vdev->device;
1914         bbdev->data->socket_id = init_params->socket_id;
1915         bbdev->intr_handle = NULL;
1916
1917         /* register rx/tx burst functions for data path */
1918         bbdev->dequeue_enc_ops = dequeue_enc_ops;
1919         bbdev->dequeue_dec_ops = dequeue_dec_ops;
1920         bbdev->enqueue_enc_ops = enqueue_enc_ops;
1921         bbdev->enqueue_dec_ops = enqueue_dec_ops;
1922         bbdev->dequeue_ldpc_enc_ops = dequeue_enc_ops;
1923         bbdev->dequeue_ldpc_dec_ops = dequeue_dec_ops;
1924         bbdev->enqueue_ldpc_enc_ops = enqueue_ldpc_enc_ops;
1925         bbdev->enqueue_ldpc_dec_ops = enqueue_ldpc_dec_ops;
1926         ((struct bbdev_private *) bbdev->data->dev_private)->max_nb_queues =
1927                         init_params->queues_num;
1928
1929         return 0;
1930 }
1931
1932 /* Initialise device */
1933 static int
1934 turbo_sw_bbdev_probe(struct rte_vdev_device *vdev)
1935 {
1936         struct turbo_sw_params init_params = {
1937                 rte_socket_id(),
1938                 RTE_BBDEV_DEFAULT_MAX_NB_QUEUES
1939         };
1940         const char *name;
1941         const char *input_args;
1942
1943         if (vdev == NULL)
1944                 return -EINVAL;
1945
1946         name = rte_vdev_device_name(vdev);
1947         if (name == NULL)
1948                 return -EINVAL;
1949         input_args = rte_vdev_device_args(vdev);
1950         parse_turbo_sw_params(&init_params, input_args);
1951
1952         rte_bbdev_log_debug(
1953                         "Initialising %s on NUMA node %d with max queues: %d\n",
1954                         name, init_params.socket_id, init_params.queues_num);
1955
1956         return turbo_sw_bbdev_create(vdev, &init_params);
1957 }
1958
1959 /* Uninitialise device */
1960 static int
1961 turbo_sw_bbdev_remove(struct rte_vdev_device *vdev)
1962 {
1963         struct rte_bbdev *bbdev;
1964         const char *name;
1965
1966         if (vdev == NULL)
1967                 return -EINVAL;
1968
1969         name = rte_vdev_device_name(vdev);
1970         if (name == NULL)
1971                 return -EINVAL;
1972
1973         bbdev = rte_bbdev_get_named_dev(name);
1974         if (bbdev == NULL)
1975                 return -EINVAL;
1976
1977         rte_free(bbdev->data->dev_private);
1978
1979         return rte_bbdev_release(bbdev);
1980 }
1981
1982 static struct rte_vdev_driver bbdev_turbo_sw_pmd_drv = {
1983         .probe = turbo_sw_bbdev_probe,
1984         .remove = turbo_sw_bbdev_remove
1985 };
1986
1987 RTE_PMD_REGISTER_VDEV(DRIVER_NAME, bbdev_turbo_sw_pmd_drv);
1988 RTE_PMD_REGISTER_PARAM_STRING(DRIVER_NAME,
1989         TURBO_SW_MAX_NB_QUEUES_ARG"=<int> "
1990         TURBO_SW_SOCKET_ID_ARG"=<int>");
1991 RTE_PMD_REGISTER_ALIAS(DRIVER_NAME, turbo_sw);