xref: /dpdk/drivers/baseband/turbo_sw/bbdev_turbo_software.c (revision 25d11a86c56d50947af33d0b79ede622809bd8b9)
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 static inline void
839 move_padding_bytes(const uint8_t *in, uint8_t *out, uint16_t k,
840 		uint16_t ncb)
841 {
842 	uint16_t d = k + 4;
843 	uint16_t kpi = ncb / 3;
844 	uint16_t nd = kpi - d;
845 
846 	rte_memcpy(&out[nd], in, d);
847 	rte_memcpy(&out[nd + kpi + 64], &in[kpi], d);
848 	rte_memcpy(&out[(nd - 1) + 2 * (kpi + 64)], &in[2 * kpi], d);
849 }
850 
851 static inline void
852 process_dec_cb(struct turbo_sw_queue *q, struct rte_bbdev_dec_op *op,
853 		uint8_t c, uint16_t k, uint16_t kw, struct rte_mbuf *m_in,
854 		struct rte_mbuf *m_out_head, struct rte_mbuf *m_out,
855 		uint16_t in_offset, uint16_t out_offset, bool check_crc_24b,
856 		uint16_t crc24_overlap, uint16_t in_length,
857 		struct rte_bbdev_stats *q_stats)
858 {
859 	int ret;
860 	int32_t k_idx;
861 	int32_t iter_cnt;
862 	uint8_t *in, *out, *adapter_input;
863 	int32_t ncb, ncb_without_null;
864 	struct bblib_turbo_adapter_ul_response adapter_resp;
865 	struct bblib_turbo_adapter_ul_request adapter_req;
866 	struct bblib_turbo_decoder_request turbo_req;
867 	struct bblib_turbo_decoder_response turbo_resp;
868 	struct rte_bbdev_op_turbo_dec *dec = &op->turbo_dec;
869 #ifdef RTE_BBDEV_OFFLOAD_COST
870 	uint64_t start_time;
871 #else
872 	RTE_SET_USED(q_stats);
873 #endif
874 
875 	k_idx = compute_idx(k);
876 
877 	ret = is_dec_input_valid(k_idx, kw, in_length);
878 	if (ret != 0) {
879 		op->status |= 1 << RTE_BBDEV_DATA_ERROR;
880 		return;
881 	}
882 
883 	in = rte_pktmbuf_mtod_offset(m_in, uint8_t *, in_offset);
884 	ncb = kw;
885 	ncb_without_null = (k + 4) * 3;
886 
887 	if (check_bit(dec->op_flags, RTE_BBDEV_TURBO_SUBBLOCK_DEINTERLEAVE)) {
888 		struct bblib_deinterleave_ul_request deint_req;
889 		struct bblib_deinterleave_ul_response deint_resp;
890 
891 		deint_req.circ_buffer = BBLIB_FULL_CIRCULAR_BUFFER;
892 		deint_req.pharqbuffer = in;
893 		deint_req.ncb = ncb;
894 		deint_resp.pinteleavebuffer = q->deint_output;
895 
896 #ifdef RTE_BBDEV_OFFLOAD_COST
897 		start_time = rte_rdtsc_precise();
898 #endif
899 		bblib_deinterleave_ul(&deint_req, &deint_resp);
900 #ifdef RTE_BBDEV_OFFLOAD_COST
901 		q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
902 #endif
903 	} else
904 		move_padding_bytes(in, q->deint_output, k, ncb);
905 
906 	adapter_input = q->deint_output;
907 
908 	if (dec->op_flags & RTE_BBDEV_TURBO_POS_LLR_1_BIT_IN)
909 		adapter_req.isinverted = 1;
910 	else if (dec->op_flags & RTE_BBDEV_TURBO_NEG_LLR_1_BIT_IN)
911 		adapter_req.isinverted = 0;
912 	else {
913 		op->status |= 1 << RTE_BBDEV_DRV_ERROR;
914 		rte_bbdev_log(ERR, "LLR format wasn't specified");
915 		return;
916 	}
917 
918 	adapter_req.ncb = ncb_without_null;
919 	adapter_req.pinteleavebuffer = adapter_input;
920 	adapter_resp.pharqout = q->adapter_output;
921 
922 #ifdef RTE_BBDEV_OFFLOAD_COST
923 	start_time = rte_rdtsc_precise();
924 #endif
925 	/* Turbo decode adaptation */
926 	bblib_turbo_adapter_ul(&adapter_req, &adapter_resp);
927 #ifdef RTE_BBDEV_OFFLOAD_COST
928 	q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
929 #endif
930 
931 	out = (uint8_t *)mbuf_append(m_out_head, m_out,
932 			((k - crc24_overlap) >> 3));
933 	if (out == NULL) {
934 		op->status |= 1 << RTE_BBDEV_DATA_ERROR;
935 		rte_bbdev_log(ERR, "Too little space in output mbuf");
936 		return;
937 	}
938 	/* rte_bbdev_op_data.offset can be different than the offset of the
939 	 * appended bytes
940 	 */
941 	out = rte_pktmbuf_mtod_offset(m_out, uint8_t *, out_offset);
942 	if (check_crc_24b)
943 		turbo_req.c = c + 1;
944 	else
945 		turbo_req.c = c;
946 	turbo_req.input = (int8_t *)q->adapter_output;
947 	turbo_req.k = k;
948 	turbo_req.k_idx = k_idx;
949 	turbo_req.max_iter_num = dec->iter_max;
950 	turbo_req.early_term_disable = !check_bit(dec->op_flags,
951 			RTE_BBDEV_TURBO_EARLY_TERMINATION);
952 	turbo_resp.ag_buf = q->ag;
953 	turbo_resp.cb_buf = q->code_block;
954 	turbo_resp.output = out;
955 
956 #ifdef RTE_BBDEV_OFFLOAD_COST
957 	start_time = rte_rdtsc_precise();
958 #endif
959 	/* Turbo decode */
960 	iter_cnt = bblib_turbo_decoder(&turbo_req, &turbo_resp);
961 #ifdef RTE_BBDEV_OFFLOAD_COST
962 	q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
963 #endif
964 	dec->hard_output.length += (k >> 3);
965 
966 	if (iter_cnt > 0) {
967 		/* Temporary solution for returned iter_count from SDK */
968 		iter_cnt = (iter_cnt - 1) >> 1;
969 		dec->iter_count = RTE_MAX(iter_cnt, dec->iter_count);
970 	} else {
971 		op->status |= 1 << RTE_BBDEV_DATA_ERROR;
972 		rte_bbdev_log(ERR, "Turbo Decoder failed");
973 		return;
974 	}
975 }
976 
977 static inline void
978 enqueue_dec_one_op(struct turbo_sw_queue *q, struct rte_bbdev_dec_op *op,
979 		struct rte_bbdev_stats *queue_stats)
980 {
981 	uint8_t c, r = 0;
982 	uint16_t kw, k = 0;
983 	uint16_t crc24_overlap = 0;
984 	struct rte_bbdev_op_turbo_dec *dec = &op->turbo_dec;
985 	struct rte_mbuf *m_in = dec->input.data;
986 	struct rte_mbuf *m_out = dec->hard_output.data;
987 	struct rte_mbuf *m_out_head = dec->hard_output.data;
988 	uint16_t in_offset = dec->input.offset;
989 	uint16_t out_offset = dec->hard_output.offset;
990 	uint32_t mbuf_total_left = dec->input.length;
991 	uint16_t seg_total_left;
992 
993 	/* Clear op status */
994 	op->status = 0;
995 
996 	if (m_in == NULL || m_out == NULL) {
997 		rte_bbdev_log(ERR, "Invalid mbuf pointer");
998 		op->status = 1 << RTE_BBDEV_DATA_ERROR;
999 		return;
1000 	}
1001 
1002 	if (dec->code_block_mode == 0) { /* For Transport Block mode */
1003 		c = dec->tb_params.c;
1004 	} else { /* For Code Block mode */
1005 		k = dec->cb_params.k;
1006 		c = 1;
1007 	}
1008 
1009 	if ((c > 1) && !check_bit(dec->op_flags,
1010 		RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP))
1011 		crc24_overlap = 24;
1012 
1013 	while (mbuf_total_left > 0) {
1014 		if (dec->code_block_mode == 0)
1015 			k = (r < dec->tb_params.c_neg) ?
1016 				dec->tb_params.k_neg : dec->tb_params.k_pos;
1017 
1018 		seg_total_left = rte_pktmbuf_data_len(m_in) - in_offset;
1019 
1020 		/* Calculates circular buffer size (Kw).
1021 		 * According to 3gpp 36.212 section 5.1.4.2
1022 		 *   Kw = 3 * Kpi,
1023 		 * where:
1024 		 *   Kpi = nCol * nRow
1025 		 * where nCol is 32 and nRow can be calculated from:
1026 		 *   D =< nCol * nRow
1027 		 * where D is the size of each output from turbo encoder block
1028 		 * (k + 4).
1029 		 */
1030 		kw = RTE_ALIGN_CEIL(k + 4, RTE_BBDEV_C_SUBBLOCK) * 3;
1031 
1032 		process_dec_cb(q, op, c, k, kw, m_in, m_out_head, m_out,
1033 				in_offset, out_offset, check_bit(dec->op_flags,
1034 				RTE_BBDEV_TURBO_CRC_TYPE_24B), crc24_overlap,
1035 				seg_total_left, queue_stats);
1036 		/* To keep CRC24 attached to end of Code block, use
1037 		 * RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP flag as it
1038 		 * removed by default once verified.
1039 		 */
1040 
1041 		mbuf_total_left -= kw;
1042 
1043 		/* Update offsets */
1044 		if (seg_total_left == kw) {
1045 			/* Go to the next mbuf */
1046 			m_in = m_in->next;
1047 			m_out = m_out->next;
1048 			in_offset = 0;
1049 			out_offset = 0;
1050 		} else {
1051 			/* Update offsets for next CBs (if exist) */
1052 			in_offset += kw;
1053 			out_offset += ((k - crc24_overlap) >> 3);
1054 		}
1055 		r++;
1056 	}
1057 	if (mbuf_total_left != 0) {
1058 		op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1059 		rte_bbdev_log(ERR,
1060 				"Mismatch between mbuf length and included Circular buffer sizes");
1061 	}
1062 }
1063 
1064 static inline uint16_t
1065 enqueue_dec_all_ops(struct turbo_sw_queue *q, struct rte_bbdev_dec_op **ops,
1066 		uint16_t nb_ops, struct rte_bbdev_stats *queue_stats)
1067 {
1068 	uint16_t i;
1069 #ifdef RTE_BBDEV_OFFLOAD_COST
1070 	queue_stats->acc_offload_cycles = 0;
1071 #endif
1072 
1073 	for (i = 0; i < nb_ops; ++i)
1074 		enqueue_dec_one_op(q, ops[i], queue_stats);
1075 
1076 	return rte_ring_enqueue_burst(q->processed_pkts, (void **)ops, nb_ops,
1077 			NULL);
1078 }
1079 
1080 /* Enqueue burst */
1081 static uint16_t
1082 enqueue_enc_ops(struct rte_bbdev_queue_data *q_data,
1083 		struct rte_bbdev_enc_op **ops, uint16_t nb_ops)
1084 {
1085 	void *queue = q_data->queue_private;
1086 	struct turbo_sw_queue *q = queue;
1087 	uint16_t nb_enqueued = 0;
1088 
1089 	nb_enqueued = enqueue_enc_all_ops(q, ops, nb_ops, &q_data->queue_stats);
1090 
1091 	q_data->queue_stats.enqueue_err_count += nb_ops - nb_enqueued;
1092 	q_data->queue_stats.enqueued_count += nb_enqueued;
1093 
1094 	return nb_enqueued;
1095 }
1096 
1097 /* Enqueue burst */
1098 static uint16_t
1099 enqueue_dec_ops(struct rte_bbdev_queue_data *q_data,
1100 		 struct rte_bbdev_dec_op **ops, uint16_t nb_ops)
1101 {
1102 	void *queue = q_data->queue_private;
1103 	struct turbo_sw_queue *q = queue;
1104 	uint16_t nb_enqueued = 0;
1105 
1106 	nb_enqueued = enqueue_dec_all_ops(q, ops, nb_ops, &q_data->queue_stats);
1107 
1108 	q_data->queue_stats.enqueue_err_count += nb_ops - nb_enqueued;
1109 	q_data->queue_stats.enqueued_count += nb_enqueued;
1110 
1111 	return nb_enqueued;
1112 }
1113 
1114 /* Dequeue decode burst */
1115 static uint16_t
1116 dequeue_dec_ops(struct rte_bbdev_queue_data *q_data,
1117 		struct rte_bbdev_dec_op **ops, uint16_t nb_ops)
1118 {
1119 	struct turbo_sw_queue *q = q_data->queue_private;
1120 	uint16_t nb_dequeued = rte_ring_dequeue_burst(q->processed_pkts,
1121 			(void **)ops, nb_ops, NULL);
1122 	q_data->queue_stats.dequeued_count += nb_dequeued;
1123 
1124 	return nb_dequeued;
1125 }
1126 
1127 /* Dequeue encode burst */
1128 static uint16_t
1129 dequeue_enc_ops(struct rte_bbdev_queue_data *q_data,
1130 		struct rte_bbdev_enc_op **ops, uint16_t nb_ops)
1131 {
1132 	struct turbo_sw_queue *q = q_data->queue_private;
1133 	uint16_t nb_dequeued = rte_ring_dequeue_burst(q->processed_pkts,
1134 			(void **)ops, nb_ops, NULL);
1135 	q_data->queue_stats.dequeued_count += nb_dequeued;
1136 
1137 	return nb_dequeued;
1138 }
1139 
1140 /* Parse 16bit integer from string argument */
1141 static inline int
1142 parse_u16_arg(const char *key, const char *value, void *extra_args)
1143 {
1144 	uint16_t *u16 = extra_args;
1145 	unsigned int long result;
1146 
1147 	if ((value == NULL) || (extra_args == NULL))
1148 		return -EINVAL;
1149 	errno = 0;
1150 	result = strtoul(value, NULL, 0);
1151 	if ((result >= (1 << 16)) || (errno != 0)) {
1152 		rte_bbdev_log(ERR, "Invalid value %lu for %s", result, key);
1153 		return -ERANGE;
1154 	}
1155 	*u16 = (uint16_t)result;
1156 	return 0;
1157 }
1158 
1159 /* Parse parameters used to create device */
1160 static int
1161 parse_turbo_sw_params(struct turbo_sw_params *params, const char *input_args)
1162 {
1163 	struct rte_kvargs *kvlist = NULL;
1164 	int ret = 0;
1165 
1166 	if (params == NULL)
1167 		return -EINVAL;
1168 	if (input_args) {
1169 		kvlist = rte_kvargs_parse(input_args, turbo_sw_valid_params);
1170 		if (kvlist == NULL)
1171 			return -EFAULT;
1172 
1173 		ret = rte_kvargs_process(kvlist, turbo_sw_valid_params[0],
1174 					&parse_u16_arg, &params->queues_num);
1175 		if (ret < 0)
1176 			goto exit;
1177 
1178 		ret = rte_kvargs_process(kvlist, turbo_sw_valid_params[1],
1179 					&parse_u16_arg, &params->socket_id);
1180 		if (ret < 0)
1181 			goto exit;
1182 
1183 		if (params->socket_id >= RTE_MAX_NUMA_NODES) {
1184 			rte_bbdev_log(ERR, "Invalid socket, must be < %u",
1185 					RTE_MAX_NUMA_NODES);
1186 			goto exit;
1187 		}
1188 	}
1189 
1190 exit:
1191 	if (kvlist)
1192 		rte_kvargs_free(kvlist);
1193 	return ret;
1194 }
1195 
1196 /* Create device */
1197 static int
1198 turbo_sw_bbdev_create(struct rte_vdev_device *vdev,
1199 		struct turbo_sw_params *init_params)
1200 {
1201 	struct rte_bbdev *bbdev;
1202 	const char *name = rte_vdev_device_name(vdev);
1203 
1204 	bbdev = rte_bbdev_allocate(name);
1205 	if (bbdev == NULL)
1206 		return -ENODEV;
1207 
1208 	bbdev->data->dev_private = rte_zmalloc_socket(name,
1209 			sizeof(struct bbdev_private), RTE_CACHE_LINE_SIZE,
1210 			init_params->socket_id);
1211 	if (bbdev->data->dev_private == NULL) {
1212 		rte_bbdev_release(bbdev);
1213 		return -ENOMEM;
1214 	}
1215 
1216 	bbdev->dev_ops = &pmd_ops;
1217 	bbdev->device = &vdev->device;
1218 	bbdev->data->socket_id = init_params->socket_id;
1219 	bbdev->intr_handle = NULL;
1220 
1221 	/* register rx/tx burst functions for data path */
1222 	bbdev->dequeue_enc_ops = dequeue_enc_ops;
1223 	bbdev->dequeue_dec_ops = dequeue_dec_ops;
1224 	bbdev->enqueue_enc_ops = enqueue_enc_ops;
1225 	bbdev->enqueue_dec_ops = enqueue_dec_ops;
1226 	((struct bbdev_private *) bbdev->data->dev_private)->max_nb_queues =
1227 			init_params->queues_num;
1228 
1229 	return 0;
1230 }
1231 
1232 /* Initialise device */
1233 static int
1234 turbo_sw_bbdev_probe(struct rte_vdev_device *vdev)
1235 {
1236 	struct turbo_sw_params init_params = {
1237 		rte_socket_id(),
1238 		RTE_BBDEV_DEFAULT_MAX_NB_QUEUES
1239 	};
1240 	const char *name;
1241 	const char *input_args;
1242 
1243 	if (vdev == NULL)
1244 		return -EINVAL;
1245 
1246 	name = rte_vdev_device_name(vdev);
1247 	if (name == NULL)
1248 		return -EINVAL;
1249 	input_args = rte_vdev_device_args(vdev);
1250 	parse_turbo_sw_params(&init_params, input_args);
1251 
1252 	rte_bbdev_log_debug(
1253 			"Initialising %s on NUMA node %d with max queues: %d\n",
1254 			name, init_params.socket_id, init_params.queues_num);
1255 
1256 	return turbo_sw_bbdev_create(vdev, &init_params);
1257 }
1258 
1259 /* Uninitialise device */
1260 static int
1261 turbo_sw_bbdev_remove(struct rte_vdev_device *vdev)
1262 {
1263 	struct rte_bbdev *bbdev;
1264 	const char *name;
1265 
1266 	if (vdev == NULL)
1267 		return -EINVAL;
1268 
1269 	name = rte_vdev_device_name(vdev);
1270 	if (name == NULL)
1271 		return -EINVAL;
1272 
1273 	bbdev = rte_bbdev_get_named_dev(name);
1274 	if (bbdev == NULL)
1275 		return -EINVAL;
1276 
1277 	rte_free(bbdev->data->dev_private);
1278 
1279 	return rte_bbdev_release(bbdev);
1280 }
1281 
1282 static struct rte_vdev_driver bbdev_turbo_sw_pmd_drv = {
1283 	.probe = turbo_sw_bbdev_probe,
1284 	.remove = turbo_sw_bbdev_remove
1285 };
1286 
1287 RTE_PMD_REGISTER_VDEV(DRIVER_NAME, bbdev_turbo_sw_pmd_drv);
1288 RTE_PMD_REGISTER_PARAM_STRING(DRIVER_NAME,
1289 	TURBO_SW_MAX_NB_QUEUES_ARG"=<int> "
1290 	TURBO_SW_SOCKET_ID_ARG"=<int>");
1291 RTE_PMD_REGISTER_ALIAS(DRIVER_NAME, turbo_sw);
1292 
1293 RTE_INIT(turbo_sw_bbdev_init_log)
1294 {
1295 	bbdev_turbo_sw_logtype = rte_log_register("pmd.bb.turbo_sw");
1296 	if (bbdev_turbo_sw_logtype >= 0)
1297 		rte_log_set_level(bbdev_turbo_sw_logtype, RTE_LOG_NOTICE);
1298 }
1299