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