xref: /dpdk/drivers/baseband/turbo_sw/bbdev_turbo_software.c (revision 9585f8b159e2cfebc64f1d02aa051329b208c6e9)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4 
5 #include <string.h>
6 
7 #include <rte_common.h>
8 #include <rte_bus_vdev.h>
9 #include <rte_malloc.h>
10 #include <rte_ring.h>
11 #include <rte_kvargs.h>
12 #include <rte_cycles.h>
13 
14 #include <rte_bbdev.h>
15 #include <rte_bbdev_pmd.h>
16 
17 #include <phy_turbo.h>
18 #include <phy_crc.h>
19 #include <phy_rate_match.h>
20 #include <divide.h>
21 
22 #define DRIVER_NAME baseband_turbo_sw
23 
24 /* Turbo SW PMD logging ID */
25 static int bbdev_turbo_sw_logtype;
26 
27 /* Helper macro for logging */
28 #define rte_bbdev_log(level, fmt, ...) \
29 	rte_log(RTE_LOG_ ## level, bbdev_turbo_sw_logtype, fmt "\n", \
30 		##__VA_ARGS__)
31 
32 #define rte_bbdev_log_debug(fmt, ...) \
33 	rte_bbdev_log(DEBUG, RTE_STR(__LINE__) ":%s() " fmt, __func__, \
34 		##__VA_ARGS__)
35 
36 #define DEINT_INPUT_BUF_SIZE (((RTE_BBDEV_MAX_CB_SIZE >> 3) + 1) * 48)
37 #define DEINT_OUTPUT_BUF_SIZE (DEINT_INPUT_BUF_SIZE * 6)
38 #define ADAPTER_OUTPUT_BUF_SIZE ((RTE_BBDEV_MAX_CB_SIZE + 4) * 48)
39 
40 /* private data structure */
41 struct bbdev_private {
42 	unsigned int max_nb_queues;  /**< Max number of queues */
43 };
44 
45 /*  Initialisation params structure that can be used by Turbo SW driver */
46 struct turbo_sw_params {
47 	int socket_id;  /*< Turbo SW device socket */
48 	uint16_t queues_num;  /*< Turbo SW device queues number */
49 };
50 
51 /* Accecptable params for Turbo SW devices */
52 #define TURBO_SW_MAX_NB_QUEUES_ARG  "max_nb_queues"
53 #define TURBO_SW_SOCKET_ID_ARG      "socket_id"
54 
55 static const char * const turbo_sw_valid_params[] = {
56 	TURBO_SW_MAX_NB_QUEUES_ARG,
57 	TURBO_SW_SOCKET_ID_ARG
58 };
59 
60 /* queue */
61 struct turbo_sw_queue {
62 	/* Ring for processed (encoded/decoded) operations which are ready to
63 	 * be dequeued.
64 	 */
65 	struct rte_ring *processed_pkts;
66 	/* Stores input for turbo encoder (used when CRC attachment is
67 	 * performed
68 	 */
69 	uint8_t *enc_in;
70 	/* Stores output from turbo encoder */
71 	uint8_t *enc_out;
72 	/* Alpha gamma buf for bblib_turbo_decoder() function */
73 	int8_t *ag;
74 	/* Temp buf for bblib_turbo_decoder() function */
75 	uint16_t *code_block;
76 	/* Input buf for bblib_rate_dematching_lte() function */
77 	uint8_t *deint_input;
78 	/* Output buf for bblib_rate_dematching_lte() function */
79 	uint8_t *deint_output;
80 	/* Output buf for bblib_turbodec_adapter_lte() function */
81 	uint8_t *adapter_output;
82 	/* Operation type of this queue */
83 	enum rte_bbdev_op_type type;
84 } __rte_cache_aligned;
85 
86 static inline char *
87 mbuf_append(struct rte_mbuf *m_head, struct rte_mbuf *m, uint16_t len)
88 {
89 	if (unlikely(len > rte_pktmbuf_tailroom(m)))
90 		return NULL;
91 
92 	char *tail = (char *)m->buf_addr + m->data_off + m->data_len;
93 	m->data_len = (uint16_t)(m->data_len + len);
94 	m_head->pkt_len  = (m_head->pkt_len + len);
95 	return tail;
96 }
97 
98 /* Calculate index based on Table 5.1.3-3 from TS34.212 */
99 static inline int32_t
100 compute_idx(uint16_t k)
101 {
102 	int32_t result = 0;
103 
104 	if (k < RTE_BBDEV_MIN_CB_SIZE || k > RTE_BBDEV_MAX_CB_SIZE)
105 		return -1;
106 
107 	if (k > 2048) {
108 		if ((k - 2048) % 64 != 0)
109 			result = -1;
110 
111 		result = 124 + (k - 2048) / 64;
112 	} else if (k <= 512) {
113 		if ((k - 40) % 8 != 0)
114 			result = -1;
115 
116 		result = (k - 40) / 8 + 1;
117 	} else if (k <= 1024) {
118 		if ((k - 512) % 16 != 0)
119 			result = -1;
120 
121 		result = 60 + (k - 512) / 16;
122 	} else { /* 1024 < k <= 2048 */
123 		if ((k - 1024) % 32 != 0)
124 			result = -1;
125 
126 		result = 92 + (k - 1024) / 32;
127 	}
128 
129 	return result;
130 }
131 
132 /* Read flag value 0/1 from bitmap */
133 static inline bool
134 check_bit(uint32_t bitmap, uint32_t bitmask)
135 {
136 	return bitmap & bitmask;
137 }
138 
139 /* Get device info */
140 static void
141 info_get(struct rte_bbdev *dev, struct rte_bbdev_driver_info *dev_info)
142 {
143 	struct bbdev_private *internals = dev->data->dev_private;
144 
145 	static const struct rte_bbdev_op_cap bbdev_capabilities[] = {
146 		{
147 			.type = RTE_BBDEV_OP_TURBO_DEC,
148 			.cap.turbo_dec = {
149 				.capability_flags =
150 					RTE_BBDEV_TURBO_SUBBLOCK_DEINTERLEAVE |
151 					RTE_BBDEV_TURBO_POS_LLR_1_BIT_IN |
152 					RTE_BBDEV_TURBO_NEG_LLR_1_BIT_IN |
153 					RTE_BBDEV_TURBO_CRC_TYPE_24B |
154 					RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP |
155 					RTE_BBDEV_TURBO_EARLY_TERMINATION,
156 				.max_llr_modulus = 16,
157 				.num_buffers_src = RTE_BBDEV_MAX_CODE_BLOCKS,
158 				.num_buffers_hard_out =
159 						RTE_BBDEV_MAX_CODE_BLOCKS,
160 				.num_buffers_soft_out = 0,
161 			}
162 		},
163 		{
164 			.type   = RTE_BBDEV_OP_TURBO_ENC,
165 			.cap.turbo_enc = {
166 				.capability_flags =
167 						RTE_BBDEV_TURBO_CRC_24B_ATTACH |
168 						RTE_BBDEV_TURBO_CRC_24A_ATTACH |
169 						RTE_BBDEV_TURBO_RATE_MATCH |
170 						RTE_BBDEV_TURBO_RV_INDEX_BYPASS,
171 				.num_buffers_src = RTE_BBDEV_MAX_CODE_BLOCKS,
172 				.num_buffers_dst = RTE_BBDEV_MAX_CODE_BLOCKS,
173 			}
174 		},
175 		RTE_BBDEV_END_OF_CAPABILITIES_LIST()
176 	};
177 
178 	static struct rte_bbdev_queue_conf default_queue_conf = {
179 		.queue_size = RTE_BBDEV_QUEUE_SIZE_LIMIT,
180 	};
181 
182 	static const enum rte_cpu_flag_t cpu_flag = RTE_CPUFLAG_SSE4_2;
183 
184 	default_queue_conf.socket = dev->data->socket_id;
185 
186 	dev_info->driver_name = RTE_STR(DRIVER_NAME);
187 	dev_info->max_num_queues = internals->max_nb_queues;
188 	dev_info->queue_size_lim = RTE_BBDEV_QUEUE_SIZE_LIMIT;
189 	dev_info->hardware_accelerated = false;
190 	dev_info->max_dl_queue_priority = 0;
191 	dev_info->max_ul_queue_priority = 0;
192 	dev_info->default_queue_conf = default_queue_conf;
193 	dev_info->capabilities = bbdev_capabilities;
194 	dev_info->cpu_flag_reqs = &cpu_flag;
195 	dev_info->min_alignment = 64;
196 
197 	rte_bbdev_log_debug("got device info from %u\n", dev->data->dev_id);
198 }
199 
200 /* Release queue */
201 static int
202 q_release(struct rte_bbdev *dev, uint16_t q_id)
203 {
204 	struct turbo_sw_queue *q = dev->data->queues[q_id].queue_private;
205 
206 	if (q != NULL) {
207 		rte_ring_free(q->processed_pkts);
208 		rte_free(q->enc_out);
209 		rte_free(q->enc_in);
210 		rte_free(q->ag);
211 		rte_free(q->code_block);
212 		rte_free(q->deint_input);
213 		rte_free(q->deint_output);
214 		rte_free(q->adapter_output);
215 		rte_free(q);
216 		dev->data->queues[q_id].queue_private = NULL;
217 	}
218 
219 	rte_bbdev_log_debug("released device queue %u:%u",
220 			dev->data->dev_id, q_id);
221 	return 0;
222 }
223 
224 /* Setup a queue */
225 static int
226 q_setup(struct rte_bbdev *dev, uint16_t q_id,
227 		const struct rte_bbdev_queue_conf *queue_conf)
228 {
229 	int ret;
230 	struct turbo_sw_queue *q;
231 	char name[RTE_RING_NAMESIZE];
232 
233 	/* Allocate the queue data structure. */
234 	q = rte_zmalloc_socket(RTE_STR(DRIVER_NAME), sizeof(*q),
235 			RTE_CACHE_LINE_SIZE, queue_conf->socket);
236 	if (q == NULL) {
237 		rte_bbdev_log(ERR, "Failed to allocate queue memory");
238 		return -ENOMEM;
239 	}
240 
241 	/* Allocate memory for encoder output. */
242 	ret = snprintf(name, RTE_RING_NAMESIZE, RTE_STR(DRIVER_NAME)"_enc_o%u:%u",
243 			dev->data->dev_id, q_id);
244 	if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
245 		rte_bbdev_log(ERR,
246 				"Creating queue name for device %u queue %u failed",
247 				dev->data->dev_id, q_id);
248 		return -ENAMETOOLONG;
249 	}
250 	q->enc_out = rte_zmalloc_socket(name,
251 			((RTE_BBDEV_MAX_TB_SIZE >> 3) + 3) *
252 			sizeof(*q->enc_out) * 3,
253 			RTE_CACHE_LINE_SIZE, queue_conf->socket);
254 	if (q->enc_out == NULL) {
255 		rte_bbdev_log(ERR,
256 			"Failed to allocate queue memory for %s", name);
257 		goto free_q;
258 	}
259 
260 	/* Allocate memory for rate matching output. */
261 	ret = snprintf(name, RTE_RING_NAMESIZE,
262 			RTE_STR(DRIVER_NAME)"_enc_i%u:%u", dev->data->dev_id,
263 			q_id);
264 	if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
265 		rte_bbdev_log(ERR,
266 				"Creating queue name for device %u queue %u failed",
267 				dev->data->dev_id, q_id);
268 		return -ENAMETOOLONG;
269 	}
270 	q->enc_in = rte_zmalloc_socket(name,
271 			(RTE_BBDEV_MAX_CB_SIZE >> 3) * sizeof(*q->enc_in),
272 			RTE_CACHE_LINE_SIZE, queue_conf->socket);
273 	if (q->enc_in == NULL) {
274 		rte_bbdev_log(ERR,
275 			"Failed to allocate queue memory for %s", name);
276 		goto free_q;
277 	}
278 
279 	/* Allocate memory for Aplha Gamma temp buffer. */
280 	ret = snprintf(name, RTE_RING_NAMESIZE, RTE_STR(DRIVER_NAME)"_ag%u:%u",
281 			dev->data->dev_id, q_id);
282 	if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
283 		rte_bbdev_log(ERR,
284 				"Creating queue name for device %u queue %u failed",
285 				dev->data->dev_id, q_id);
286 		return -ENAMETOOLONG;
287 	}
288 	q->ag = rte_zmalloc_socket(name,
289 			RTE_BBDEV_MAX_CB_SIZE * 10 * sizeof(*q->ag),
290 			RTE_CACHE_LINE_SIZE, queue_conf->socket);
291 	if (q->ag == NULL) {
292 		rte_bbdev_log(ERR,
293 			"Failed to allocate queue memory for %s", name);
294 		goto free_q;
295 	}
296 
297 	/* Allocate memory for code block temp buffer. */
298 	ret = snprintf(name, RTE_RING_NAMESIZE, RTE_STR(DRIVER_NAME)"_cb%u:%u",
299 			dev->data->dev_id, q_id);
300 	if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
301 		rte_bbdev_log(ERR,
302 				"Creating queue name for device %u queue %u failed",
303 				dev->data->dev_id, q_id);
304 		return -ENAMETOOLONG;
305 	}
306 	q->code_block = rte_zmalloc_socket(name,
307 			RTE_BBDEV_MAX_CB_SIZE * sizeof(*q->code_block),
308 			RTE_CACHE_LINE_SIZE, queue_conf->socket);
309 	if (q->code_block == NULL) {
310 		rte_bbdev_log(ERR,
311 			"Failed to allocate queue memory for %s", name);
312 		goto free_q;
313 	}
314 
315 	/* Allocate memory for Deinterleaver input. */
316 	ret = snprintf(name, RTE_RING_NAMESIZE,
317 			RTE_STR(DRIVER_NAME)"_de_i%u:%u",
318 			dev->data->dev_id, q_id);
319 	if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
320 		rte_bbdev_log(ERR,
321 				"Creating queue name for device %u queue %u failed",
322 				dev->data->dev_id, q_id);
323 		return -ENAMETOOLONG;
324 	}
325 	q->deint_input = rte_zmalloc_socket(name,
326 			DEINT_INPUT_BUF_SIZE * sizeof(*q->deint_input),
327 			RTE_CACHE_LINE_SIZE, queue_conf->socket);
328 	if (q->deint_input == NULL) {
329 		rte_bbdev_log(ERR,
330 			"Failed to allocate queue memory for %s", name);
331 		goto free_q;
332 	}
333 
334 	/* Allocate memory for Deinterleaver output. */
335 	ret = snprintf(name, RTE_RING_NAMESIZE,
336 			RTE_STR(DRIVER_NAME)"_de_o%u:%u",
337 			dev->data->dev_id, q_id);
338 	if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
339 		rte_bbdev_log(ERR,
340 				"Creating queue name for device %u queue %u failed",
341 				dev->data->dev_id, q_id);
342 		return -ENAMETOOLONG;
343 	}
344 	q->deint_output = rte_zmalloc_socket(NULL,
345 			DEINT_OUTPUT_BUF_SIZE * sizeof(*q->deint_output),
346 			RTE_CACHE_LINE_SIZE, queue_conf->socket);
347 	if (q->deint_output == NULL) {
348 		rte_bbdev_log(ERR,
349 			"Failed to allocate queue memory for %s", name);
350 		goto free_q;
351 	}
352 
353 	/* Allocate memory for Adapter output. */
354 	ret = snprintf(name, RTE_RING_NAMESIZE,
355 			RTE_STR(DRIVER_NAME)"_ada_o%u:%u",
356 			dev->data->dev_id, q_id);
357 	if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
358 		rte_bbdev_log(ERR,
359 				"Creating queue name for device %u queue %u failed",
360 				dev->data->dev_id, q_id);
361 		return -ENAMETOOLONG;
362 	}
363 	q->adapter_output = rte_zmalloc_socket(NULL,
364 			ADAPTER_OUTPUT_BUF_SIZE * sizeof(*q->adapter_output),
365 			RTE_CACHE_LINE_SIZE, queue_conf->socket);
366 	if (q->adapter_output == NULL) {
367 		rte_bbdev_log(ERR,
368 			"Failed to allocate queue memory for %s", name);
369 		goto free_q;
370 	}
371 
372 	/* Create ring for packets awaiting to be dequeued. */
373 	ret = snprintf(name, RTE_RING_NAMESIZE, RTE_STR(DRIVER_NAME)"%u:%u",
374 			dev->data->dev_id, q_id);
375 	if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
376 		rte_bbdev_log(ERR,
377 				"Creating queue name for device %u queue %u failed",
378 				dev->data->dev_id, q_id);
379 		return -ENAMETOOLONG;
380 	}
381 	q->processed_pkts = rte_ring_create(name, queue_conf->queue_size,
382 			queue_conf->socket, RING_F_SP_ENQ | RING_F_SC_DEQ);
383 	if (q->processed_pkts == NULL) {
384 		rte_bbdev_log(ERR, "Failed to create ring for %s", name);
385 		goto free_q;
386 	}
387 
388 	q->type = queue_conf->op_type;
389 
390 	dev->data->queues[q_id].queue_private = q;
391 	rte_bbdev_log_debug("setup device queue %s", name);
392 	return 0;
393 
394 free_q:
395 	rte_ring_free(q->processed_pkts);
396 	rte_free(q->enc_out);
397 	rte_free(q->enc_in);
398 	rte_free(q->ag);
399 	rte_free(q->code_block);
400 	rte_free(q->deint_input);
401 	rte_free(q->deint_output);
402 	rte_free(q->adapter_output);
403 	rte_free(q);
404 	return -EFAULT;
405 }
406 
407 static const struct rte_bbdev_ops pmd_ops = {
408 	.info_get = info_get,
409 	.queue_setup = q_setup,
410 	.queue_release = q_release
411 };
412 
413 /* Checks if the encoder input buffer is correct.
414  * Returns 0 if it's valid, -1 otherwise.
415  */
416 static inline int
417 is_enc_input_valid(const uint16_t k, const int32_t k_idx,
418 		const uint16_t in_length)
419 {
420 	if (k_idx < 0) {
421 		rte_bbdev_log(ERR, "K Index is invalid");
422 		return -1;
423 	}
424 
425 	if (in_length - (k >> 3) < 0) {
426 		rte_bbdev_log(ERR,
427 				"Mismatch between input length (%u bytes) and K (%u bits)",
428 				in_length, k);
429 		return -1;
430 	}
431 
432 	if (k > RTE_BBDEV_MAX_CB_SIZE) {
433 		rte_bbdev_log(ERR, "CB size (%u) is too big, max: %d",
434 				k, RTE_BBDEV_MAX_CB_SIZE);
435 		return -1;
436 	}
437 
438 	return 0;
439 }
440 
441 /* Checks if the decoder input buffer is correct.
442  * Returns 0 if it's valid, -1 otherwise.
443  */
444 static inline int
445 is_dec_input_valid(int32_t k_idx, int16_t kw, int16_t in_length)
446 {
447 	if (k_idx < 0) {
448 		rte_bbdev_log(ERR, "K index is invalid");
449 		return -1;
450 	}
451 
452 	if (in_length < kw) {
453 		rte_bbdev_log(ERR,
454 				"Mismatch between input length (%u) and kw (%u)",
455 				in_length, kw);
456 		return -1;
457 	}
458 
459 	if (kw > RTE_BBDEV_MAX_KW) {
460 		rte_bbdev_log(ERR, "Input length (%u) is too big, max: %d",
461 				kw, RTE_BBDEV_MAX_KW);
462 		return -1;
463 	}
464 
465 	return 0;
466 }
467 
468 static inline void
469 process_enc_cb(struct turbo_sw_queue *q, struct rte_bbdev_enc_op *op,
470 		uint8_t r, uint8_t c, uint16_t k, uint16_t ncb,
471 		uint32_t e, struct rte_mbuf *m_in, struct rte_mbuf *m_out_head,
472 		struct rte_mbuf *m_out, uint16_t in_offset, uint16_t out_offset,
473 		uint16_t in_length, struct rte_bbdev_stats *q_stats)
474 {
475 	int ret;
476 	int16_t k_idx;
477 	uint16_t m;
478 	uint8_t *in, *out0, *out1, *out2, *tmp_out, *rm_out;
479 	uint64_t first_3_bytes = 0;
480 	struct rte_bbdev_op_turbo_enc *enc = &op->turbo_enc;
481 	struct bblib_crc_request crc_req;
482 	struct bblib_crc_response crc_resp;
483 	struct bblib_turbo_encoder_request turbo_req;
484 	struct bblib_turbo_encoder_response turbo_resp;
485 	struct bblib_rate_match_dl_request rm_req;
486 	struct bblib_rate_match_dl_response rm_resp;
487 #ifdef RTE_BBDEV_OFFLOAD_COST
488 	uint64_t start_time;
489 #else
490 	RTE_SET_USED(q_stats);
491 #endif
492 
493 	k_idx = compute_idx(k);
494 	in = rte_pktmbuf_mtod_offset(m_in, uint8_t *, in_offset);
495 
496 	/* CRC24A (for TB) */
497 	if ((enc->op_flags & RTE_BBDEV_TURBO_CRC_24A_ATTACH) &&
498 		(enc->code_block_mode == 1)) {
499 		ret = is_enc_input_valid(k - 24, k_idx, in_length);
500 		if (ret != 0) {
501 			op->status |= 1 << RTE_BBDEV_DATA_ERROR;
502 			return;
503 		}
504 		crc_req.data = in;
505 		crc_req.len = k - 24;
506 		/* Check if there is a room for CRC bits if not use
507 		 * the temporary buffer.
508 		 */
509 		if (mbuf_append(m_in, m_in, 3) == NULL) {
510 			rte_memcpy(q->enc_in, in, (k - 24) >> 3);
511 			in = q->enc_in;
512 		} else {
513 			/* Store 3 first bytes of next CB as they will be
514 			 * overwritten by CRC bytes. If it is the last CB then
515 			 * there is no point to store 3 next bytes and this
516 			 * if..else branch will be omitted.
517 			 */
518 			first_3_bytes = *((uint64_t *)&in[(k - 32) >> 3]);
519 		}
520 
521 		crc_resp.data = in;
522 #ifdef RTE_BBDEV_OFFLOAD_COST
523 		start_time = rte_rdtsc_precise();
524 #endif
525 		/* CRC24A generation */
526 		bblib_lte_crc24a_gen(&crc_req, &crc_resp);
527 #ifdef RTE_BBDEV_OFFLOAD_COST
528 		q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
529 #endif
530 	} else if (enc->op_flags & RTE_BBDEV_TURBO_CRC_24B_ATTACH) {
531 		/* CRC24B */
532 		ret = is_enc_input_valid(k - 24, k_idx, in_length);
533 		if (ret != 0) {
534 			op->status |= 1 << RTE_BBDEV_DATA_ERROR;
535 			return;
536 		}
537 		crc_req.data = in;
538 		crc_req.len = k - 24;
539 		/* Check if there is a room for CRC bits if this is the last
540 		 * CB in TB. If not use temporary buffer.
541 		 */
542 		if ((c - r == 1) && (mbuf_append(m_in, m_in, 3) == NULL)) {
543 			rte_memcpy(q->enc_in, in, (k - 24) >> 3);
544 			in = q->enc_in;
545 		} else if (c - r > 1) {
546 			/* Store 3 first bytes of next CB as they will be
547 			 * overwritten by CRC bytes. If it is the last CB then
548 			 * there is no point to store 3 next bytes and this
549 			 * if..else branch will be omitted.
550 			 */
551 			first_3_bytes = *((uint64_t *)&in[(k - 32) >> 3]);
552 		}
553 
554 		crc_resp.data = in;
555 #ifdef RTE_BBDEV_OFFLOAD_COST
556 		start_time = rte_rdtsc_precise();
557 #endif
558 		/* CRC24B generation */
559 		bblib_lte_crc24b_gen(&crc_req, &crc_resp);
560 #ifdef RTE_BBDEV_OFFLOAD_COST
561 		q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
562 #endif
563 	} else {
564 		ret = is_enc_input_valid(k, k_idx, in_length);
565 		if (ret != 0) {
566 			op->status |= 1 << RTE_BBDEV_DATA_ERROR;
567 			return;
568 		}
569 	}
570 
571 	/* Turbo encoder */
572 
573 	/* Each bit layer output from turbo encoder is (k+4) bits long, i.e.
574 	 * input length + 4 tail bits. That's (k/8) + 1 bytes after rounding up.
575 	 * So dst_data's length should be 3*(k/8) + 3 bytes.
576 	 * In Rate-matching bypass case outputs pointers passed to encoder
577 	 * (out0, out1 and out2) can directly point to addresses of output from
578 	 * turbo_enc entity.
579 	 */
580 	if (enc->op_flags & RTE_BBDEV_TURBO_RATE_MATCH) {
581 		out0 = q->enc_out;
582 		out1 = RTE_PTR_ADD(out0, (k >> 3) + 1);
583 		out2 = RTE_PTR_ADD(out1, (k >> 3) + 1);
584 	} else {
585 		out0 = (uint8_t *)mbuf_append(m_out_head, m_out,
586 				(k >> 3) * 3 + 2);
587 		if (out0 == NULL) {
588 			op->status |= 1 << RTE_BBDEV_DATA_ERROR;
589 			rte_bbdev_log(ERR,
590 					"Too little space in output mbuf");
591 			return;
592 		}
593 		enc->output.length += (k >> 3) * 3 + 2;
594 		/* rte_bbdev_op_data.offset can be different than the
595 		 * offset of the appended bytes
596 		 */
597 		out0 = rte_pktmbuf_mtod_offset(m_out, uint8_t *, out_offset);
598 		out1 = rte_pktmbuf_mtod_offset(m_out, uint8_t *,
599 				out_offset + (k >> 3) + 1);
600 		out2 = rte_pktmbuf_mtod_offset(m_out, uint8_t *,
601 				out_offset + 2 * ((k >> 3) + 1));
602 	}
603 
604 	turbo_req.case_id = k_idx;
605 	turbo_req.input_win = in;
606 	turbo_req.length = k >> 3;
607 	turbo_resp.output_win_0 = out0;
608 	turbo_resp.output_win_1 = out1;
609 	turbo_resp.output_win_2 = out2;
610 
611 #ifdef RTE_BBDEV_OFFLOAD_COST
612 	start_time = rte_rdtsc_precise();
613 #endif
614 	/* Turbo encoding */
615 	if (bblib_turbo_encoder(&turbo_req, &turbo_resp) != 0) {
616 		op->status |= 1 << RTE_BBDEV_DRV_ERROR;
617 		rte_bbdev_log(ERR, "Turbo Encoder failed");
618 		return;
619 	}
620 #ifdef RTE_BBDEV_OFFLOAD_COST
621 	q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
622 #endif
623 
624 	/* Restore 3 first bytes of next CB if they were overwritten by CRC*/
625 	if (first_3_bytes != 0)
626 		*((uint64_t *)&in[(k - 32) >> 3]) = first_3_bytes;
627 
628 	/* Rate-matching */
629 	if (enc->op_flags & RTE_BBDEV_TURBO_RATE_MATCH) {
630 		uint8_t mask_id;
631 		/* Integer round up division by 8 */
632 		uint16_t out_len = (e + 7) >> 3;
633 		/* The mask array is indexed using E%8. E is an even number so
634 		 * there are only 4 possible values.
635 		 */
636 		const uint8_t mask_out[] = {0xFF, 0xC0, 0xF0, 0xFC};
637 
638 		/* get output data starting address */
639 		rm_out = (uint8_t *)mbuf_append(m_out_head, m_out, out_len);
640 		if (rm_out == NULL) {
641 			op->status |= 1 << RTE_BBDEV_DATA_ERROR;
642 			rte_bbdev_log(ERR,
643 					"Too little space in output mbuf");
644 			return;
645 		}
646 		/* rte_bbdev_op_data.offset can be different than the offset
647 		 * of the appended bytes
648 		 */
649 		rm_out = rte_pktmbuf_mtod_offset(m_out, uint8_t *, out_offset);
650 
651 		/* index of current code block */
652 		rm_req.r = r;
653 		/* total number of code block */
654 		rm_req.C = c;
655 		/* For DL - 1, UL - 0 */
656 		rm_req.direction = 1;
657 		/* According to 3ggp 36.212 Spec 5.1.4.1.2 section Nsoft, KMIMO
658 		 * and MDL_HARQ are used for Ncb calculation. As Ncb is already
659 		 * known we can adjust those parameters
660 		 */
661 		rm_req.Nsoft = ncb * rm_req.C;
662 		rm_req.KMIMO = 1;
663 		rm_req.MDL_HARQ = 1;
664 		/* According to 3ggp 36.212 Spec 5.1.4.1.2 section Nl, Qm and G
665 		 * are used for E calculation. As E is already known we can
666 		 * adjust those parameters
667 		 */
668 		rm_req.NL = e;
669 		rm_req.Qm = 1;
670 		rm_req.G = rm_req.NL * rm_req.Qm * rm_req.C;
671 
672 		rm_req.rvidx = enc->rv_index;
673 		rm_req.Kidx = k_idx - 1;
674 		rm_req.nLen = k + 4;
675 		rm_req.tin0 = out0;
676 		rm_req.tin1 = out1;
677 		rm_req.tin2 = out2;
678 		rm_resp.output = rm_out;
679 		rm_resp.OutputLen = out_len;
680 		if (enc->op_flags & RTE_BBDEV_TURBO_RV_INDEX_BYPASS)
681 			rm_req.bypass_rvidx = 1;
682 		else
683 			rm_req.bypass_rvidx = 0;
684 
685 #ifdef RTE_BBDEV_OFFLOAD_COST
686 		start_time = rte_rdtsc_precise();
687 #endif
688 		/* Rate-Matching */
689 		if (bblib_rate_match_dl(&rm_req, &rm_resp) != 0) {
690 			op->status |= 1 << RTE_BBDEV_DRV_ERROR;
691 			rte_bbdev_log(ERR, "Rate matching failed");
692 			return;
693 		}
694 #ifdef RTE_BBDEV_OFFLOAD_COST
695 		q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
696 #endif
697 
698 		/* SW fills an entire last byte even if E%8 != 0. Clear the
699 		 * superfluous data bits for consistency with HW device.
700 		 */
701 		mask_id = (e & 7) >> 1;
702 		rm_out[out_len - 1] &= mask_out[mask_id];
703 		enc->output.length += rm_resp.OutputLen;
704 	} else {
705 		/* Rate matching is bypassed */
706 
707 		/* Completing last byte of out0 (where 4 tail bits are stored)
708 		 * by moving first 4 bits from out1
709 		 */
710 		tmp_out = (uint8_t *) --out1;
711 		*tmp_out = *tmp_out | ((*(tmp_out + 1) & 0xF0) >> 4);
712 		tmp_out++;
713 		/* Shifting out1 data by 4 bits to the left */
714 		for (m = 0; m < k >> 3; ++m) {
715 			uint8_t *first = tmp_out;
716 			uint8_t second = *(tmp_out + 1);
717 			*first = (*first << 4) | ((second & 0xF0) >> 4);
718 			tmp_out++;
719 		}
720 		/* Shifting out2 data by 8 bits to the left */
721 		for (m = 0; m < (k >> 3) + 1; ++m) {
722 			*tmp_out = *(tmp_out + 1);
723 			tmp_out++;
724 		}
725 		*tmp_out = 0;
726 	}
727 }
728 
729 static inline void
730 enqueue_enc_one_op(struct turbo_sw_queue *q, struct rte_bbdev_enc_op *op,
731 		struct rte_bbdev_stats *queue_stats)
732 {
733 	uint8_t c, r, crc24_bits = 0;
734 	uint16_t k, ncb;
735 	uint32_t e;
736 	struct rte_bbdev_op_turbo_enc *enc = &op->turbo_enc;
737 	uint16_t in_offset = enc->input.offset;
738 	uint16_t out_offset = enc->output.offset;
739 	struct rte_mbuf *m_in = enc->input.data;
740 	struct rte_mbuf *m_out = enc->output.data;
741 	struct rte_mbuf *m_out_head = enc->output.data;
742 	uint32_t in_length, mbuf_total_left = enc->input.length;
743 	uint16_t seg_total_left;
744 
745 	/* Clear op status */
746 	op->status = 0;
747 
748 	if (mbuf_total_left > RTE_BBDEV_MAX_TB_SIZE >> 3) {
749 		rte_bbdev_log(ERR, "TB size (%u) is too big, max: %d",
750 				mbuf_total_left, RTE_BBDEV_MAX_TB_SIZE);
751 		op->status = 1 << RTE_BBDEV_DATA_ERROR;
752 		return;
753 	}
754 
755 	if (m_in == NULL || m_out == NULL) {
756 		rte_bbdev_log(ERR, "Invalid mbuf pointer");
757 		op->status = 1 << RTE_BBDEV_DATA_ERROR;
758 		return;
759 	}
760 
761 	if ((enc->op_flags & RTE_BBDEV_TURBO_CRC_24B_ATTACH) ||
762 		(enc->op_flags & RTE_BBDEV_TURBO_CRC_24A_ATTACH))
763 		crc24_bits = 24;
764 
765 	if (enc->code_block_mode == 0) { /* For Transport Block mode */
766 		c = enc->tb_params.c;
767 		r = enc->tb_params.r;
768 	} else {/* For Code Block mode */
769 		c = 1;
770 		r = 0;
771 	}
772 
773 	while (mbuf_total_left > 0 && r < c) {
774 
775 		seg_total_left = rte_pktmbuf_data_len(m_in) - in_offset;
776 
777 		if (enc->code_block_mode == 0) {
778 			k = (r < enc->tb_params.c_neg) ?
779 				enc->tb_params.k_neg : enc->tb_params.k_pos;
780 			ncb = (r < enc->tb_params.c_neg) ?
781 				enc->tb_params.ncb_neg : enc->tb_params.ncb_pos;
782 			e = (r < enc->tb_params.cab) ?
783 				enc->tb_params.ea : enc->tb_params.eb;
784 		} else {
785 			k = enc->cb_params.k;
786 			ncb = enc->cb_params.ncb;
787 			e = enc->cb_params.e;
788 		}
789 
790 		process_enc_cb(q, op, r, c, k, ncb, e, m_in, m_out_head,
791 				m_out, in_offset, out_offset, seg_total_left,
792 				queue_stats);
793 		/* Update total_left */
794 		in_length = ((k - crc24_bits) >> 3);
795 		mbuf_total_left -= in_length;
796 		/* Update offsets for next CBs (if exist) */
797 		in_offset += (k - crc24_bits) >> 3;
798 		if (enc->op_flags & RTE_BBDEV_TURBO_RATE_MATCH)
799 			out_offset += e >> 3;
800 		else
801 			out_offset += (k >> 3) * 3 + 2;
802 
803 		/* Update offsets */
804 		if (seg_total_left == in_length) {
805 			/* Go to the next mbuf */
806 			m_in = m_in->next;
807 			m_out = m_out->next;
808 			in_offset = 0;
809 			out_offset = 0;
810 		}
811 		r++;
812 	}
813 
814 	/* check if all input data was processed */
815 	if (mbuf_total_left != 0) {
816 		op->status |= 1 << RTE_BBDEV_DATA_ERROR;
817 		rte_bbdev_log(ERR,
818 				"Mismatch between mbuf length and included CBs sizes");
819 	}
820 }
821 
822 static inline uint16_t
823 enqueue_enc_all_ops(struct turbo_sw_queue *q, struct rte_bbdev_enc_op **ops,
824 		uint16_t nb_ops, struct rte_bbdev_stats *queue_stats)
825 {
826 	uint16_t i;
827 #ifdef RTE_BBDEV_OFFLOAD_COST
828 	queue_stats->acc_offload_cycles = 0;
829 #endif
830 
831 	for (i = 0; i < nb_ops; ++i)
832 		enqueue_enc_one_op(q, ops[i], queue_stats);
833 
834 	return rte_ring_enqueue_burst(q->processed_pkts, (void **)ops, nb_ops,
835 			NULL);
836 }
837 
838 /* Remove the padding bytes from a cyclic buffer.
839  * The input buffer is a data stream wk as described in 3GPP TS 36.212 section
840  * 5.1.4.1.2 starting from w0 and with length Ncb bytes.
841  * The output buffer is a data stream wk with pruned padding bytes. It's length
842  * is 3*D bytes and the order of non-padding bytes is preserved.
843  */
844 static inline void
845 remove_nulls_from_circular_buf(const uint8_t *in, uint8_t *out, uint16_t k,
846 		uint16_t ncb)
847 {
848 	uint32_t in_idx, out_idx, c_idx;
849 	const uint32_t d = k + 4;
850 	const uint32_t kw = (ncb / 3);
851 	const uint32_t nd = kw - d;
852 	const uint32_t r_subblock = kw / RTE_BBDEV_C_SUBBLOCK;
853 	/* Inter-column permutation pattern */
854 	const uint32_t P[RTE_BBDEV_C_SUBBLOCK] = {0, 16, 8, 24, 4, 20, 12, 28,
855 			2, 18, 10, 26, 6, 22, 14, 30, 1, 17, 9, 25, 5, 21, 13,
856 			29, 3, 19, 11, 27, 7, 23, 15, 31};
857 	in_idx = 0;
858 	out_idx = 0;
859 
860 	/* The padding bytes are at the first Nd positions in the first row. */
861 	for (c_idx = 0; in_idx < kw; in_idx += r_subblock, ++c_idx) {
862 		if (P[c_idx] < nd) {
863 			rte_memcpy(&out[out_idx], &in[in_idx + 1],
864 					r_subblock - 1);
865 			out_idx += r_subblock - 1;
866 		} else {
867 			rte_memcpy(&out[out_idx], &in[in_idx], r_subblock);
868 			out_idx += r_subblock;
869 		}
870 	}
871 
872 	/* First and second parity bits sub-blocks are interlaced. */
873 	for (c_idx = 0; in_idx < ncb - 2 * r_subblock;
874 			in_idx += 2 * r_subblock, ++c_idx) {
875 		uint32_t second_block_c_idx = P[c_idx];
876 		uint32_t third_block_c_idx = P[c_idx] + 1;
877 
878 		if (second_block_c_idx < nd && third_block_c_idx < nd) {
879 			rte_memcpy(&out[out_idx], &in[in_idx + 2],
880 					2 * r_subblock - 2);
881 			out_idx += 2 * r_subblock - 2;
882 		} else if (second_block_c_idx >= nd &&
883 				third_block_c_idx >= nd) {
884 			rte_memcpy(&out[out_idx], &in[in_idx], 2 * r_subblock);
885 			out_idx += 2 * r_subblock;
886 		} else if (second_block_c_idx < nd) {
887 			out[out_idx++] = in[in_idx];
888 			rte_memcpy(&out[out_idx], &in[in_idx + 2],
889 					2 * r_subblock - 2);
890 			out_idx += 2 * r_subblock - 2;
891 		} else {
892 			rte_memcpy(&out[out_idx], &in[in_idx + 1],
893 					2 * r_subblock - 1);
894 			out_idx += 2 * r_subblock - 1;
895 		}
896 	}
897 
898 	/* Last interlaced row is different - its last byte is the only padding
899 	 * byte. We can have from 4 up to 28 padding bytes (Nd) per sub-block.
900 	 * After interlacing the 1st and 2nd parity sub-blocks we can have 0, 1
901 	 * or 2 padding bytes each time we make a step of 2 * R_SUBBLOCK bytes
902 	 * (moving to another column). 2nd parity sub-block uses the same
903 	 * inter-column permutation pattern as the systematic and 1st parity
904 	 * sub-blocks but it adds '1' to the resulting index and calculates the
905 	 * modulus of the result and Kw. Last column is mapped to itself (id 31)
906 	 * so the first byte taken from the 2nd parity sub-block will be the
907 	 * 32nd (31+1) byte, then 64th etc. (step is C_SUBBLOCK == 32) and the
908 	 * last byte will be the first byte from the sub-block:
909 	 * (32 + 32 * (R_SUBBLOCK-1)) % Kw == Kw % Kw == 0. Nd can't  be smaller
910 	 * than 4 so we know that bytes with ids 0, 1, 2 and 3 must be the
911 	 * padding bytes. The bytes from the 1st parity sub-block are the bytes
912 	 * from the 31st column - Nd can't be greater than 28 so we are sure
913 	 * that there are no padding bytes in 31st column.
914 	 */
915 	rte_memcpy(&out[out_idx], &in[in_idx], 2 * r_subblock - 1);
916 }
917 
918 static inline void
919 move_padding_bytes(const uint8_t *in, uint8_t *out, uint16_t k,
920 		uint16_t ncb)
921 {
922 	uint16_t d = k + 4;
923 	uint16_t kpi = ncb / 3;
924 	uint16_t nd = kpi - d;
925 
926 	rte_memcpy(&out[nd], in, d);
927 	rte_memcpy(&out[nd + kpi + 64], &in[kpi], d);
928 	rte_memcpy(&out[(nd - 1) + 2 * (kpi + 64)], &in[2 * kpi], d);
929 }
930 
931 static inline void
932 process_dec_cb(struct turbo_sw_queue *q, struct rte_bbdev_dec_op *op,
933 		uint8_t c, uint16_t k, uint16_t kw, struct rte_mbuf *m_in,
934 		struct rte_mbuf *m_out_head, struct rte_mbuf *m_out,
935 		uint16_t in_offset, uint16_t out_offset, bool check_crc_24b,
936 		uint16_t crc24_overlap, uint16_t in_length,
937 		struct rte_bbdev_stats *q_stats)
938 {
939 	int ret;
940 	int32_t k_idx;
941 	int32_t iter_cnt;
942 	uint8_t *in, *out, *adapter_input;
943 	int32_t ncb, ncb_without_null;
944 	struct bblib_turbo_adapter_ul_response adapter_resp;
945 	struct bblib_turbo_adapter_ul_request adapter_req;
946 	struct bblib_turbo_decoder_request turbo_req;
947 	struct bblib_turbo_decoder_response turbo_resp;
948 	struct rte_bbdev_op_turbo_dec *dec = &op->turbo_dec;
949 #ifdef RTE_BBDEV_OFFLOAD_COST
950 	uint64_t start_time;
951 #else
952 	RTE_SET_USED(q_stats);
953 #endif
954 
955 	k_idx = compute_idx(k);
956 
957 	ret = is_dec_input_valid(k_idx, kw, in_length);
958 	if (ret != 0) {
959 		op->status |= 1 << RTE_BBDEV_DATA_ERROR;
960 		return;
961 	}
962 
963 	in = rte_pktmbuf_mtod_offset(m_in, uint8_t *, in_offset);
964 	ncb = kw;
965 	ncb_without_null = (k + 4) * 3;
966 
967 	if (check_bit(dec->op_flags, RTE_BBDEV_TURBO_SUBBLOCK_DEINTERLEAVE)) {
968 		struct bblib_deinterleave_ul_request deint_req;
969 		struct bblib_deinterleave_ul_response deint_resp;
970 
971 		/* SW decoder accepts only a circular buffer without NULL bytes
972 		 * so the input needs to be converted.
973 		 */
974 		remove_nulls_from_circular_buf(in, q->deint_input, k, ncb);
975 
976 		deint_req.pharqbuffer = q->deint_input;
977 		deint_req.ncb = ncb_without_null;
978 		deint_resp.pinteleavebuffer = q->deint_output;
979 
980 #ifdef RTE_BBDEV_OFFLOAD_COST
981 		start_time = rte_rdtsc_precise();
982 #endif
983 		bblib_deinterleave_ul(&deint_req, &deint_resp);
984 #ifdef RTE_BBDEV_OFFLOAD_COST
985 		q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
986 #endif
987 	} else
988 		move_padding_bytes(in, q->deint_output, k, ncb);
989 
990 	adapter_input = q->deint_output;
991 
992 	if (dec->op_flags & RTE_BBDEV_TURBO_POS_LLR_1_BIT_IN)
993 		adapter_req.isinverted = 1;
994 	else if (dec->op_flags & RTE_BBDEV_TURBO_NEG_LLR_1_BIT_IN)
995 		adapter_req.isinverted = 0;
996 	else {
997 		op->status |= 1 << RTE_BBDEV_DRV_ERROR;
998 		rte_bbdev_log(ERR, "LLR format wasn't specified");
999 		return;
1000 	}
1001 
1002 	adapter_req.ncb = ncb_without_null;
1003 	adapter_req.pinteleavebuffer = adapter_input;
1004 	adapter_resp.pharqout = q->adapter_output;
1005 
1006 #ifdef RTE_BBDEV_OFFLOAD_COST
1007 	start_time = rte_rdtsc_precise();
1008 #endif
1009 	/* Turbo decode adaptation */
1010 	bblib_turbo_adapter_ul(&adapter_req, &adapter_resp);
1011 #ifdef RTE_BBDEV_OFFLOAD_COST
1012 	q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
1013 #endif
1014 
1015 	out = (uint8_t *)mbuf_append(m_out_head, m_out,
1016 			((k - crc24_overlap) >> 3));
1017 	if (out == NULL) {
1018 		op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1019 		rte_bbdev_log(ERR, "Too little space in output mbuf");
1020 		return;
1021 	}
1022 	/* rte_bbdev_op_data.offset can be different than the offset of the
1023 	 * appended bytes
1024 	 */
1025 	out = rte_pktmbuf_mtod_offset(m_out, uint8_t *, out_offset);
1026 	if (check_crc_24b)
1027 		turbo_req.c = c + 1;
1028 	else
1029 		turbo_req.c = c;
1030 	turbo_req.input = (int8_t *)q->adapter_output;
1031 	turbo_req.k = k;
1032 	turbo_req.k_idx = k_idx;
1033 	turbo_req.max_iter_num = dec->iter_max;
1034 	turbo_req.early_term_disable = !check_bit(dec->op_flags,
1035 			RTE_BBDEV_TURBO_EARLY_TERMINATION);
1036 	turbo_resp.ag_buf = q->ag;
1037 	turbo_resp.cb_buf = q->code_block;
1038 	turbo_resp.output = out;
1039 
1040 #ifdef RTE_BBDEV_OFFLOAD_COST
1041 	start_time = rte_rdtsc_precise();
1042 #endif
1043 	/* Turbo decode */
1044 	iter_cnt = bblib_turbo_decoder(&turbo_req, &turbo_resp);
1045 #ifdef RTE_BBDEV_OFFLOAD_COST
1046 	q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
1047 #endif
1048 	dec->hard_output.length += (k >> 3);
1049 
1050 	if (iter_cnt > 0) {
1051 		/* Temporary solution for returned iter_count from SDK */
1052 		iter_cnt = (iter_cnt - 1) >> 1;
1053 		dec->iter_count = RTE_MAX(iter_cnt, dec->iter_count);
1054 	} else {
1055 		op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1056 		rte_bbdev_log(ERR, "Turbo Decoder failed");
1057 		return;
1058 	}
1059 }
1060 
1061 static inline void
1062 enqueue_dec_one_op(struct turbo_sw_queue *q, struct rte_bbdev_dec_op *op,
1063 		struct rte_bbdev_stats *queue_stats)
1064 {
1065 	uint8_t c, r = 0;
1066 	uint16_t kw, k = 0;
1067 	uint16_t crc24_overlap = 0;
1068 	struct rte_bbdev_op_turbo_dec *dec = &op->turbo_dec;
1069 	struct rte_mbuf *m_in = dec->input.data;
1070 	struct rte_mbuf *m_out = dec->hard_output.data;
1071 	struct rte_mbuf *m_out_head = dec->hard_output.data;
1072 	uint16_t in_offset = dec->input.offset;
1073 	uint16_t out_offset = dec->hard_output.offset;
1074 	uint32_t mbuf_total_left = dec->input.length;
1075 	uint16_t seg_total_left;
1076 
1077 	/* Clear op status */
1078 	op->status = 0;
1079 
1080 	if (m_in == NULL || m_out == NULL) {
1081 		rte_bbdev_log(ERR, "Invalid mbuf pointer");
1082 		op->status = 1 << RTE_BBDEV_DATA_ERROR;
1083 		return;
1084 	}
1085 
1086 	if (dec->code_block_mode == 0) { /* For Transport Block mode */
1087 		c = dec->tb_params.c;
1088 	} else { /* For Code Block mode */
1089 		k = dec->cb_params.k;
1090 		c = 1;
1091 	}
1092 
1093 	if ((c > 1) && !check_bit(dec->op_flags,
1094 		RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP))
1095 		crc24_overlap = 24;
1096 
1097 	while (mbuf_total_left > 0) {
1098 		if (dec->code_block_mode == 0)
1099 			k = (r < dec->tb_params.c_neg) ?
1100 				dec->tb_params.k_neg : dec->tb_params.k_pos;
1101 
1102 		seg_total_left = rte_pktmbuf_data_len(m_in) - in_offset;
1103 
1104 		/* Calculates circular buffer size (Kw).
1105 		 * According to 3gpp 36.212 section 5.1.4.2
1106 		 *   Kw = 3 * Kpi,
1107 		 * where:
1108 		 *   Kpi = nCol * nRow
1109 		 * where nCol is 32 and nRow can be calculated from:
1110 		 *   D =< nCol * nRow
1111 		 * where D is the size of each output from turbo encoder block
1112 		 * (k + 4).
1113 		 */
1114 		kw = RTE_ALIGN_CEIL(k + 4, RTE_BBDEV_C_SUBBLOCK) * 3;
1115 
1116 		process_dec_cb(q, op, c, k, kw, m_in, m_out_head, m_out,
1117 				in_offset, out_offset, check_bit(dec->op_flags,
1118 				RTE_BBDEV_TURBO_CRC_TYPE_24B), crc24_overlap,
1119 				seg_total_left, queue_stats);
1120 		/* To keep CRC24 attached to end of Code block, use
1121 		 * RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP flag as it
1122 		 * removed by default once verified.
1123 		 */
1124 
1125 		mbuf_total_left -= kw;
1126 
1127 		/* Update offsets */
1128 		if (seg_total_left == kw) {
1129 			/* Go to the next mbuf */
1130 			m_in = m_in->next;
1131 			m_out = m_out->next;
1132 			in_offset = 0;
1133 			out_offset = 0;
1134 		} else {
1135 			/* Update offsets for next CBs (if exist) */
1136 			in_offset += kw;
1137 			out_offset += ((k - crc24_overlap) >> 3);
1138 		}
1139 		r++;
1140 	}
1141 	if (mbuf_total_left != 0) {
1142 		op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1143 		rte_bbdev_log(ERR,
1144 				"Mismatch between mbuf length and included Circular buffer sizes");
1145 	}
1146 }
1147 
1148 static inline uint16_t
1149 enqueue_dec_all_ops(struct turbo_sw_queue *q, struct rte_bbdev_dec_op **ops,
1150 		uint16_t nb_ops, struct rte_bbdev_stats *queue_stats)
1151 {
1152 	uint16_t i;
1153 #ifdef RTE_BBDEV_OFFLOAD_COST
1154 	queue_stats->acc_offload_cycles = 0;
1155 #endif
1156 
1157 	for (i = 0; i < nb_ops; ++i)
1158 		enqueue_dec_one_op(q, ops[i], queue_stats);
1159 
1160 	return rte_ring_enqueue_burst(q->processed_pkts, (void **)ops, nb_ops,
1161 			NULL);
1162 }
1163 
1164 /* Enqueue burst */
1165 static uint16_t
1166 enqueue_enc_ops(struct rte_bbdev_queue_data *q_data,
1167 		struct rte_bbdev_enc_op **ops, uint16_t nb_ops)
1168 {
1169 	void *queue = q_data->queue_private;
1170 	struct turbo_sw_queue *q = queue;
1171 	uint16_t nb_enqueued = 0;
1172 
1173 	nb_enqueued = enqueue_enc_all_ops(q, ops, nb_ops, &q_data->queue_stats);
1174 
1175 	q_data->queue_stats.enqueue_err_count += nb_ops - nb_enqueued;
1176 	q_data->queue_stats.enqueued_count += nb_enqueued;
1177 
1178 	return nb_enqueued;
1179 }
1180 
1181 /* Enqueue burst */
1182 static uint16_t
1183 enqueue_dec_ops(struct rte_bbdev_queue_data *q_data,
1184 		 struct rte_bbdev_dec_op **ops, uint16_t nb_ops)
1185 {
1186 	void *queue = q_data->queue_private;
1187 	struct turbo_sw_queue *q = queue;
1188 	uint16_t nb_enqueued = 0;
1189 
1190 	nb_enqueued = enqueue_dec_all_ops(q, ops, nb_ops, &q_data->queue_stats);
1191 
1192 	q_data->queue_stats.enqueue_err_count += nb_ops - nb_enqueued;
1193 	q_data->queue_stats.enqueued_count += nb_enqueued;
1194 
1195 	return nb_enqueued;
1196 }
1197 
1198 /* Dequeue decode burst */
1199 static uint16_t
1200 dequeue_dec_ops(struct rte_bbdev_queue_data *q_data,
1201 		struct rte_bbdev_dec_op **ops, uint16_t nb_ops)
1202 {
1203 	struct turbo_sw_queue *q = q_data->queue_private;
1204 	uint16_t nb_dequeued = rte_ring_dequeue_burst(q->processed_pkts,
1205 			(void **)ops, nb_ops, NULL);
1206 	q_data->queue_stats.dequeued_count += nb_dequeued;
1207 
1208 	return nb_dequeued;
1209 }
1210 
1211 /* Dequeue encode burst */
1212 static uint16_t
1213 dequeue_enc_ops(struct rte_bbdev_queue_data *q_data,
1214 		struct rte_bbdev_enc_op **ops, uint16_t nb_ops)
1215 {
1216 	struct turbo_sw_queue *q = q_data->queue_private;
1217 	uint16_t nb_dequeued = rte_ring_dequeue_burst(q->processed_pkts,
1218 			(void **)ops, nb_ops, NULL);
1219 	q_data->queue_stats.dequeued_count += nb_dequeued;
1220 
1221 	return nb_dequeued;
1222 }
1223 
1224 /* Parse 16bit integer from string argument */
1225 static inline int
1226 parse_u16_arg(const char *key, const char *value, void *extra_args)
1227 {
1228 	uint16_t *u16 = extra_args;
1229 	unsigned int long result;
1230 
1231 	if ((value == NULL) || (extra_args == NULL))
1232 		return -EINVAL;
1233 	errno = 0;
1234 	result = strtoul(value, NULL, 0);
1235 	if ((result >= (1 << 16)) || (errno != 0)) {
1236 		rte_bbdev_log(ERR, "Invalid value %lu for %s", result, key);
1237 		return -ERANGE;
1238 	}
1239 	*u16 = (uint16_t)result;
1240 	return 0;
1241 }
1242 
1243 /* Parse parameters used to create device */
1244 static int
1245 parse_turbo_sw_params(struct turbo_sw_params *params, const char *input_args)
1246 {
1247 	struct rte_kvargs *kvlist = NULL;
1248 	int ret = 0;
1249 
1250 	if (params == NULL)
1251 		return -EINVAL;
1252 	if (input_args) {
1253 		kvlist = rte_kvargs_parse(input_args, turbo_sw_valid_params);
1254 		if (kvlist == NULL)
1255 			return -EFAULT;
1256 
1257 		ret = rte_kvargs_process(kvlist, turbo_sw_valid_params[0],
1258 					&parse_u16_arg, &params->queues_num);
1259 		if (ret < 0)
1260 			goto exit;
1261 
1262 		ret = rte_kvargs_process(kvlist, turbo_sw_valid_params[1],
1263 					&parse_u16_arg, &params->socket_id);
1264 		if (ret < 0)
1265 			goto exit;
1266 
1267 		if (params->socket_id >= RTE_MAX_NUMA_NODES) {
1268 			rte_bbdev_log(ERR, "Invalid socket, must be < %u",
1269 					RTE_MAX_NUMA_NODES);
1270 			goto exit;
1271 		}
1272 	}
1273 
1274 exit:
1275 	if (kvlist)
1276 		rte_kvargs_free(kvlist);
1277 	return ret;
1278 }
1279 
1280 /* Create device */
1281 static int
1282 turbo_sw_bbdev_create(struct rte_vdev_device *vdev,
1283 		struct turbo_sw_params *init_params)
1284 {
1285 	struct rte_bbdev *bbdev;
1286 	const char *name = rte_vdev_device_name(vdev);
1287 
1288 	bbdev = rte_bbdev_allocate(name);
1289 	if (bbdev == NULL)
1290 		return -ENODEV;
1291 
1292 	bbdev->data->dev_private = rte_zmalloc_socket(name,
1293 			sizeof(struct bbdev_private), RTE_CACHE_LINE_SIZE,
1294 			init_params->socket_id);
1295 	if (bbdev->data->dev_private == NULL) {
1296 		rte_bbdev_release(bbdev);
1297 		return -ENOMEM;
1298 	}
1299 
1300 	bbdev->dev_ops = &pmd_ops;
1301 	bbdev->device = &vdev->device;
1302 	bbdev->data->socket_id = init_params->socket_id;
1303 	bbdev->intr_handle = NULL;
1304 
1305 	/* register rx/tx burst functions for data path */
1306 	bbdev->dequeue_enc_ops = dequeue_enc_ops;
1307 	bbdev->dequeue_dec_ops = dequeue_dec_ops;
1308 	bbdev->enqueue_enc_ops = enqueue_enc_ops;
1309 	bbdev->enqueue_dec_ops = enqueue_dec_ops;
1310 	((struct bbdev_private *) bbdev->data->dev_private)->max_nb_queues =
1311 			init_params->queues_num;
1312 
1313 	return 0;
1314 }
1315 
1316 /* Initialise device */
1317 static int
1318 turbo_sw_bbdev_probe(struct rte_vdev_device *vdev)
1319 {
1320 	struct turbo_sw_params init_params = {
1321 		rte_socket_id(),
1322 		RTE_BBDEV_DEFAULT_MAX_NB_QUEUES
1323 	};
1324 	const char *name;
1325 	const char *input_args;
1326 
1327 	if (vdev == NULL)
1328 		return -EINVAL;
1329 
1330 	name = rte_vdev_device_name(vdev);
1331 	if (name == NULL)
1332 		return -EINVAL;
1333 	input_args = rte_vdev_device_args(vdev);
1334 	parse_turbo_sw_params(&init_params, input_args);
1335 
1336 	rte_bbdev_log_debug(
1337 			"Initialising %s on NUMA node %d with max queues: %d\n",
1338 			name, init_params.socket_id, init_params.queues_num);
1339 
1340 	return turbo_sw_bbdev_create(vdev, &init_params);
1341 }
1342 
1343 /* Uninitialise device */
1344 static int
1345 turbo_sw_bbdev_remove(struct rte_vdev_device *vdev)
1346 {
1347 	struct rte_bbdev *bbdev;
1348 	const char *name;
1349 
1350 	if (vdev == NULL)
1351 		return -EINVAL;
1352 
1353 	name = rte_vdev_device_name(vdev);
1354 	if (name == NULL)
1355 		return -EINVAL;
1356 
1357 	bbdev = rte_bbdev_get_named_dev(name);
1358 	if (bbdev == NULL)
1359 		return -EINVAL;
1360 
1361 	rte_free(bbdev->data->dev_private);
1362 
1363 	return rte_bbdev_release(bbdev);
1364 }
1365 
1366 static struct rte_vdev_driver bbdev_turbo_sw_pmd_drv = {
1367 	.probe = turbo_sw_bbdev_probe,
1368 	.remove = turbo_sw_bbdev_remove
1369 };
1370 
1371 RTE_PMD_REGISTER_VDEV(DRIVER_NAME, bbdev_turbo_sw_pmd_drv);
1372 RTE_PMD_REGISTER_PARAM_STRING(DRIVER_NAME,
1373 	TURBO_SW_MAX_NB_QUEUES_ARG"=<int> "
1374 	TURBO_SW_SOCKET_ID_ARG"=<int>");
1375 RTE_PMD_REGISTER_ALIAS(DRIVER_NAME, turbo_sw);
1376 
1377 RTE_INIT(turbo_sw_bbdev_init_log)
1378 {
1379 	bbdev_turbo_sw_logtype = rte_log_register("pmd.bb.turbo_sw");
1380 	if (bbdev_turbo_sw_logtype >= 0)
1381 		rte_log_set_level(bbdev_turbo_sw_logtype, RTE_LOG_NOTICE);
1382 }
1383