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