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