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