1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
7 #include <rte_common.h>
8 #include <rte_bus_vdev.h>
9 #include <rte_malloc.h>
11 #include <rte_kvargs.h>
13 #include <rte_bbdev.h>
14 #include <rte_bbdev_pmd.h>
16 #include <phy_turbo.h>
18 #include <phy_rate_match.h>
21 #define DRIVER_NAME turbo_sw
23 /* Turbo SW PMD logging ID */
24 static int bbdev_turbo_sw_logtype;
26 /* Helper macro for logging */
27 #define rte_bbdev_log(level, fmt, ...) \
28 rte_log(RTE_LOG_ ## level, bbdev_turbo_sw_logtype, fmt "\n", \
31 #define rte_bbdev_log_debug(fmt, ...) \
32 rte_bbdev_log(DEBUG, RTE_STR(__LINE__) ":%s() " fmt, __func__, \
35 /* Number of columns in sub-block interleaver (36.212, section 5.1.4.1.1) */
36 #define C_SUBBLOCK (32)
37 #define MAX_TB_SIZE (391656)
38 #define MAX_CB_SIZE (6144)
39 #define MAX_KW (18528)
41 /* private data structure */
42 struct bbdev_private {
43 unsigned int max_nb_queues; /**< Max number of queues */
46 /* Initialisation params structure that can be used by Turbo SW driver */
47 struct turbo_sw_params {
48 int socket_id; /*< Turbo SW device socket */
49 uint16_t queues_num; /*< Turbo SW device queues number */
52 /* Accecptable params for Turbo SW devices */
53 #define TURBO_SW_MAX_NB_QUEUES_ARG "max_nb_queues"
54 #define TURBO_SW_SOCKET_ID_ARG "socket_id"
56 static const char * const turbo_sw_valid_params[] = {
57 TURBO_SW_MAX_NB_QUEUES_ARG,
58 TURBO_SW_SOCKET_ID_ARG
62 struct turbo_sw_queue {
63 /* Ring for processed (encoded/decoded) operations which are ready to
66 struct rte_ring *processed_pkts;
67 /* Stores input for turbo encoder (used when CRC attachment is
71 /* Stores output from turbo encoder */
73 /* Alpha gamma buf for bblib_turbo_decoder() function */
75 /* Temp buf for bblib_turbo_decoder() function */
77 /* Input buf for bblib_rate_dematching_lte() function */
79 /* Output buf for bblib_rate_dematching_lte() function */
80 uint8_t *deint_output;
81 /* Output buf for bblib_turbodec_adapter_lte() function */
82 uint8_t *adapter_output;
83 /* Operation type of this queue */
84 enum rte_bbdev_op_type type;
85 } __rte_cache_aligned;
87 /* Calculate index based on Table 5.1.3-3 from TS34.212 */
89 compute_idx(uint16_t k)
93 if (k < 40 || k > MAX_CB_SIZE)
97 if ((k - 2048) % 64 != 0)
100 result = 124 + (k - 2048) / 64;
101 } else if (k <= 512) {
102 if ((k - 40) % 8 != 0)
105 result = (k - 40) / 8 + 1;
106 } else if (k <= 1024) {
107 if ((k - 512) % 16 != 0)
110 result = 60 + (k - 512) / 16;
111 } else { /* 1024 < k <= 2048 */
112 if ((k - 1024) % 32 != 0)
115 result = 92 + (k - 1024) / 32;
121 /* Read flag value 0/1 from bitmap */
123 check_bit(uint32_t bitmap, uint32_t bitmask)
125 return bitmap & bitmask;
128 /* Get device info */
130 info_get(struct rte_bbdev *dev, struct rte_bbdev_driver_info *dev_info)
132 struct bbdev_private *internals = dev->data->dev_private;
134 static const struct rte_bbdev_op_cap bbdev_capabilities[] = {
136 .type = RTE_BBDEV_OP_TURBO_DEC,
139 RTE_BBDEV_TURBO_SUBBLOCK_DEINTERLEAVE |
140 RTE_BBDEV_TURBO_POS_LLR_1_BIT_IN |
141 RTE_BBDEV_TURBO_NEG_LLR_1_BIT_IN |
142 RTE_BBDEV_TURBO_CRC_TYPE_24B |
143 RTE_BBDEV_TURBO_EARLY_TERMINATION,
144 .num_buffers_src = RTE_BBDEV_MAX_CODE_BLOCKS,
145 .num_buffers_hard_out =
146 RTE_BBDEV_MAX_CODE_BLOCKS,
147 .num_buffers_soft_out = 0,
151 .type = RTE_BBDEV_OP_TURBO_ENC,
154 RTE_BBDEV_TURBO_CRC_24B_ATTACH |
155 RTE_BBDEV_TURBO_CRC_24A_ATTACH |
156 RTE_BBDEV_TURBO_RATE_MATCH |
157 RTE_BBDEV_TURBO_RV_INDEX_BYPASS,
158 .num_buffers_src = RTE_BBDEV_MAX_CODE_BLOCKS,
159 .num_buffers_dst = RTE_BBDEV_MAX_CODE_BLOCKS,
162 RTE_BBDEV_END_OF_CAPABILITIES_LIST()
165 static struct rte_bbdev_queue_conf default_queue_conf = {
166 .queue_size = RTE_BBDEV_QUEUE_SIZE_LIMIT,
169 static const enum rte_cpu_flag_t cpu_flag = RTE_CPUFLAG_SSE4_2;
171 default_queue_conf.socket = dev->data->socket_id;
173 dev_info->driver_name = RTE_STR(DRIVER_NAME);
174 dev_info->max_num_queues = internals->max_nb_queues;
175 dev_info->queue_size_lim = RTE_BBDEV_QUEUE_SIZE_LIMIT;
176 dev_info->hardware_accelerated = false;
177 dev_info->max_queue_priority = 0;
178 dev_info->default_queue_conf = default_queue_conf;
179 dev_info->capabilities = bbdev_capabilities;
180 dev_info->cpu_flag_reqs = &cpu_flag;
181 dev_info->min_alignment = 64;
183 rte_bbdev_log_debug("got device info from %u\n", dev->data->dev_id);
188 q_release(struct rte_bbdev *dev, uint16_t q_id)
190 struct turbo_sw_queue *q = dev->data->queues[q_id].queue_private;
193 rte_ring_free(q->processed_pkts);
194 rte_free(q->enc_out);
197 rte_free(q->code_block);
198 rte_free(q->deint_input);
199 rte_free(q->deint_output);
200 rte_free(q->adapter_output);
202 dev->data->queues[q_id].queue_private = NULL;
205 rte_bbdev_log_debug("released device queue %u:%u",
206 dev->data->dev_id, q_id);
212 q_setup(struct rte_bbdev *dev, uint16_t q_id,
213 const struct rte_bbdev_queue_conf *queue_conf)
216 struct turbo_sw_queue *q;
217 char name[RTE_RING_NAMESIZE];
219 /* Allocate the queue data structure. */
220 q = rte_zmalloc_socket(RTE_STR(DRIVER_NAME), sizeof(*q),
221 RTE_CACHE_LINE_SIZE, queue_conf->socket);
223 rte_bbdev_log(ERR, "Failed to allocate queue memory");
227 /* Allocate memory for encoder output. */
228 ret = snprintf(name, RTE_RING_NAMESIZE, RTE_STR(DRIVER_NAME)"_enc_out%u:%u",
229 dev->data->dev_id, q_id);
230 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
232 "Creating queue name for device %u queue %u failed",
233 dev->data->dev_id, q_id);
234 return -ENAMETOOLONG;
236 q->enc_out = rte_zmalloc_socket(name,
237 ((MAX_TB_SIZE >> 3) + 3) * sizeof(*q->enc_out) * 3,
238 RTE_CACHE_LINE_SIZE, queue_conf->socket);
239 if (q->enc_out == NULL) {
241 "Failed to allocate queue memory for %s", name);
245 /* Allocate memory for rate matching output. */
246 ret = snprintf(name, RTE_RING_NAMESIZE,
247 RTE_STR(DRIVER_NAME)"_enc_in%u:%u", dev->data->dev_id,
249 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
251 "Creating queue name for device %u queue %u failed",
252 dev->data->dev_id, q_id);
253 return -ENAMETOOLONG;
255 q->enc_in = rte_zmalloc_socket(name,
256 (MAX_CB_SIZE >> 3) * sizeof(*q->enc_in),
257 RTE_CACHE_LINE_SIZE, queue_conf->socket);
258 if (q->enc_in == NULL) {
260 "Failed to allocate queue memory for %s", name);
264 /* Allocate memory for Aplha Gamma temp buffer. */
265 ret = snprintf(name, RTE_RING_NAMESIZE, RTE_STR(DRIVER_NAME)"_ag%u:%u",
266 dev->data->dev_id, q_id);
267 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
269 "Creating queue name for device %u queue %u failed",
270 dev->data->dev_id, q_id);
271 return -ENAMETOOLONG;
273 q->ag = rte_zmalloc_socket(name,
274 MAX_CB_SIZE * 10 * sizeof(*q->ag),
275 RTE_CACHE_LINE_SIZE, queue_conf->socket);
278 "Failed to allocate queue memory for %s", name);
282 /* Allocate memory for code block temp buffer. */
283 ret = snprintf(name, RTE_RING_NAMESIZE, RTE_STR(DRIVER_NAME)"_cb%u:%u",
284 dev->data->dev_id, q_id);
285 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
287 "Creating queue name for device %u queue %u failed",
288 dev->data->dev_id, q_id);
289 return -ENAMETOOLONG;
291 q->code_block = rte_zmalloc_socket(name,
292 (6144 >> 3) * sizeof(*q->code_block),
293 RTE_CACHE_LINE_SIZE, queue_conf->socket);
294 if (q->code_block == NULL) {
296 "Failed to allocate queue memory for %s", name);
300 /* Allocate memory for Deinterleaver input. */
301 ret = snprintf(name, RTE_RING_NAMESIZE,
302 RTE_STR(DRIVER_NAME)"_deint_input%u:%u",
303 dev->data->dev_id, q_id);
304 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
306 "Creating queue name for device %u queue %u failed",
307 dev->data->dev_id, q_id);
308 return -ENAMETOOLONG;
310 q->deint_input = rte_zmalloc_socket(name,
311 MAX_KW * sizeof(*q->deint_input),
312 RTE_CACHE_LINE_SIZE, queue_conf->socket);
313 if (q->deint_input == NULL) {
315 "Failed to allocate queue memory for %s", name);
319 /* Allocate memory for Deinterleaver output. */
320 ret = snprintf(name, RTE_RING_NAMESIZE,
321 RTE_STR(DRIVER_NAME)"_deint_output%u:%u",
322 dev->data->dev_id, q_id);
323 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
325 "Creating queue name for device %u queue %u failed",
326 dev->data->dev_id, q_id);
327 return -ENAMETOOLONG;
329 q->deint_output = rte_zmalloc_socket(NULL,
330 MAX_KW * sizeof(*q->deint_output),
331 RTE_CACHE_LINE_SIZE, queue_conf->socket);
332 if (q->deint_output == NULL) {
334 "Failed to allocate queue memory for %s", name);
338 /* Allocate memory for Adapter output. */
339 ret = snprintf(name, RTE_RING_NAMESIZE,
340 RTE_STR(DRIVER_NAME)"_adapter_output%u:%u",
341 dev->data->dev_id, q_id);
342 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
344 "Creating queue name for device %u queue %u failed",
345 dev->data->dev_id, q_id);
346 return -ENAMETOOLONG;
348 q->adapter_output = rte_zmalloc_socket(NULL,
349 MAX_CB_SIZE * 6 * sizeof(*q->adapter_output),
350 RTE_CACHE_LINE_SIZE, queue_conf->socket);
351 if (q->adapter_output == NULL) {
353 "Failed to allocate queue memory for %s", name);
357 /* Create ring for packets awaiting to be dequeued. */
358 ret = snprintf(name, RTE_RING_NAMESIZE, RTE_STR(DRIVER_NAME)"%u:%u",
359 dev->data->dev_id, q_id);
360 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
362 "Creating queue name for device %u queue %u failed",
363 dev->data->dev_id, q_id);
364 return -ENAMETOOLONG;
366 q->processed_pkts = rte_ring_create(name, queue_conf->queue_size,
367 queue_conf->socket, RING_F_SP_ENQ | RING_F_SC_DEQ);
368 if (q->processed_pkts == NULL) {
369 rte_bbdev_log(ERR, "Failed to create ring for %s", name);
373 q->type = queue_conf->op_type;
375 dev->data->queues[q_id].queue_private = q;
376 rte_bbdev_log_debug("setup device queue %s", name);
380 rte_ring_free(q->processed_pkts);
381 rte_free(q->enc_out);
384 rte_free(q->code_block);
385 rte_free(q->deint_input);
386 rte_free(q->deint_output);
387 rte_free(q->adapter_output);
392 static const struct rte_bbdev_ops pmd_ops = {
393 .info_get = info_get,
394 .queue_setup = q_setup,
395 .queue_release = q_release
398 /* Checks if the encoder input buffer is correct.
399 * Returns 0 if it's valid, -1 otherwise.
402 is_enc_input_valid(const uint16_t k, const int32_t k_idx,
403 const uint16_t in_length)
406 rte_bbdev_log(ERR, "K Index is invalid");
410 if (in_length - (k >> 3) < 0) {
412 "Mismatch between input length (%u bytes) and K (%u bits)",
417 if (k > MAX_CB_SIZE) {
418 rte_bbdev_log(ERR, "CB size (%u) is too big, max: %d",
426 /* Checks if the decoder input buffer is correct.
427 * Returns 0 if it's valid, -1 otherwise.
430 is_dec_input_valid(int32_t k_idx, int16_t kw, int16_t in_length)
433 rte_bbdev_log(ERR, "K index is invalid");
437 if (in_length - kw < 0) {
439 "Mismatch between input length (%u) and kw (%u)",
445 rte_bbdev_log(ERR, "Input length (%u) is too big, max: %d",
454 process_enc_cb(struct turbo_sw_queue *q, struct rte_bbdev_enc_op *op,
455 uint8_t cb_idx, uint8_t c, uint16_t k, uint16_t ncb,
456 uint32_t e, struct rte_mbuf *m_in, struct rte_mbuf *m_out,
457 uint16_t in_offset, uint16_t out_offset, uint16_t total_left)
462 uint8_t *in, *out0, *out1, *out2, *tmp_out, *rm_out;
463 struct rte_bbdev_op_turbo_enc *enc = &op->turbo_enc;
464 struct bblib_crc_request crc_req;
465 struct bblib_crc_response crc_resp;
466 struct bblib_turbo_encoder_request turbo_req;
467 struct bblib_turbo_encoder_response turbo_resp;
468 struct bblib_rate_match_dl_request rm_req;
469 struct bblib_rate_match_dl_response rm_resp;
471 k_idx = compute_idx(k);
472 in = rte_pktmbuf_mtod_offset(m_in, uint8_t *, in_offset);
474 /* CRC24A (for TB) */
475 if ((enc->op_flags & RTE_BBDEV_TURBO_CRC_24A_ATTACH) &&
476 (enc->code_block_mode == 1)) {
477 ret = is_enc_input_valid(k - 24, k_idx, total_left);
479 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
482 /* copy the input to the temporary buffer to be able to extend
485 rte_memcpy(q->enc_in, in, (k - 24) >> 3);
487 crc_req.len = (k - 24) >> 3;
488 crc_resp.data = q->enc_in;
489 bblib_lte_crc24a_gen(&crc_req, &crc_resp);
492 } else if (enc->op_flags & RTE_BBDEV_TURBO_CRC_24B_ATTACH) {
494 ret = is_enc_input_valid(k - 24, k_idx, total_left);
496 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
499 /* copy the input to the temporary buffer to be able to extend
502 rte_memcpy(q->enc_in, in, (k - 24) >> 3);
504 crc_req.len = (k - 24) >> 3;
505 crc_resp.data = q->enc_in;
506 bblib_lte_crc24b_gen(&crc_req, &crc_resp);
510 ret = is_enc_input_valid(k, k_idx, total_left);
512 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
519 /* Each bit layer output from turbo encoder is (k+4) bits long, i.e.
520 * input length + 4 tail bits. That's (k/8) + 1 bytes after rounding up.
521 * So dst_data's length should be 3*(k/8) + 3 bytes.
524 out1 = RTE_PTR_ADD(out0, (k >> 3) + 1);
525 out2 = RTE_PTR_ADD(out1, (k >> 3) + 1);
527 turbo_req.case_id = k_idx;
528 turbo_req.input_win = in;
529 turbo_req.length = k >> 3;
530 turbo_resp.output_win_0 = out0;
531 turbo_resp.output_win_1 = out1;
532 turbo_resp.output_win_2 = out2;
533 if (bblib_turbo_encoder(&turbo_req, &turbo_resp) != 0) {
534 op->status |= 1 << RTE_BBDEV_DRV_ERROR;
535 rte_bbdev_log(ERR, "Turbo Encoder failed");
540 if (enc->op_flags & RTE_BBDEV_TURBO_RATE_MATCH) {
541 /* get output data starting address */
542 rm_out = (uint8_t *)rte_pktmbuf_append(m_out, (e >> 3));
543 if (rm_out == NULL) {
544 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
546 "Too little space in output mbuf");
549 /* rte_bbdev_op_data.offset can be different than the offset
550 * of the appended bytes
552 rm_out = rte_pktmbuf_mtod_offset(m_out, uint8_t *, out_offset);
554 /* index of current code block */
556 /* total number of code block */
558 /* For DL - 1, UL - 0 */
559 rm_req.direction = 1;
560 /* According to 3ggp 36.212 Spec 5.1.4.1.2 section Nsoft, KMIMO
561 * and MDL_HARQ are used for Ncb calculation. As Ncb is already
562 * known we can adjust those parameters
564 rm_req.Nsoft = ncb * rm_req.C;
567 /* According to 3ggp 36.212 Spec 5.1.4.1.2 section Nl, Qm and G
568 * are used for E calculation. As E is already known we can
569 * adjust those parameters
573 rm_req.G = rm_req.NL * rm_req.Qm * rm_req.C;
575 rm_req.rvidx = enc->rv_index;
576 rm_req.Kidx = k_idx - 1;
581 rm_resp.output = rm_out;
582 rm_resp.OutputLen = (e >> 3);
583 if (enc->op_flags & RTE_BBDEV_TURBO_RV_INDEX_BYPASS)
584 rm_req.bypass_rvidx = 1;
586 rm_req.bypass_rvidx = 0;
588 if (bblib_rate_match_dl(&rm_req, &rm_resp) != 0) {
589 op->status |= 1 << RTE_BBDEV_DRV_ERROR;
590 rte_bbdev_log(ERR, "Rate matching failed");
593 enc->output.length += rm_resp.OutputLen;
595 /* Rate matching is bypassed */
597 /* Completing last byte of out0 (where 4 tail bits are stored)
598 * by moving first 4 bits from out1
600 tmp_out = (uint8_t *) --out1;
601 *tmp_out = *tmp_out | ((*(tmp_out + 1) & 0xF0) >> 4);
603 /* Shifting out1 data by 4 bits to the left */
604 for (m = 0; m < k >> 3; ++m) {
605 uint8_t *first = tmp_out;
606 uint8_t second = *(tmp_out + 1);
607 *first = (*first << 4) | ((second & 0xF0) >> 4);
610 /* Shifting out2 data by 8 bits to the left */
611 for (m = 0; m < (k >> 3) + 1; ++m) {
612 *tmp_out = *(tmp_out + 1);
617 /* copy shifted output to turbo_enc entity */
618 out0 = (uint8_t *)rte_pktmbuf_append(m_out,
621 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
623 "Too little space in output mbuf");
626 enc->output.length += (k >> 3) * 3 + 2;
627 /* rte_bbdev_op_data.offset can be different than the
628 * offset of the appended bytes
630 out0 = rte_pktmbuf_mtod_offset(m_out, uint8_t *,
632 rte_memcpy(out0, q->enc_out, (k >> 3) * 3 + 2);
637 enqueue_enc_one_op(struct turbo_sw_queue *q, struct rte_bbdev_enc_op *op)
639 uint8_t c, r, crc24_bits = 0;
642 struct rte_bbdev_op_turbo_enc *enc = &op->turbo_enc;
643 uint16_t in_offset = enc->input.offset;
644 uint16_t out_offset = enc->output.offset;
645 struct rte_mbuf *m_in = enc->input.data;
646 struct rte_mbuf *m_out = enc->output.data;
647 uint16_t total_left = enc->input.length;
649 /* Clear op status */
652 if (total_left > MAX_TB_SIZE >> 3) {
653 rte_bbdev_log(ERR, "TB size (%u) is too big, max: %d",
654 total_left, MAX_TB_SIZE);
655 op->status = 1 << RTE_BBDEV_DATA_ERROR;
659 if (m_in == NULL || m_out == NULL) {
660 rte_bbdev_log(ERR, "Invalid mbuf pointer");
661 op->status = 1 << RTE_BBDEV_DATA_ERROR;
665 if ((enc->op_flags & RTE_BBDEV_TURBO_CRC_24B_ATTACH) ||
666 (enc->op_flags & RTE_BBDEV_TURBO_CRC_24A_ATTACH))
669 if (enc->code_block_mode == 0) { /* For Transport Block mode */
670 c = enc->tb_params.c;
671 r = enc->tb_params.r;
672 } else {/* For Code Block mode */
677 while (total_left > 0 && r < c) {
678 if (enc->code_block_mode == 0) {
679 k = (r < enc->tb_params.c_neg) ?
680 enc->tb_params.k_neg : enc->tb_params.k_pos;
681 ncb = (r < enc->tb_params.c_neg) ?
682 enc->tb_params.ncb_neg : enc->tb_params.ncb_pos;
683 e = (r < enc->tb_params.cab) ?
684 enc->tb_params.ea : enc->tb_params.eb;
686 k = enc->cb_params.k;
687 ncb = enc->cb_params.ncb;
688 e = enc->cb_params.e;
691 process_enc_cb(q, op, r, c, k, ncb, e, m_in,
692 m_out, in_offset, out_offset, total_left);
693 /* Update total_left */
694 total_left -= (k - crc24_bits) >> 3;
695 /* Update offsets for next CBs (if exist) */
696 in_offset += (k - crc24_bits) >> 3;
697 if (enc->op_flags & RTE_BBDEV_TURBO_RATE_MATCH)
698 out_offset += e >> 3;
700 out_offset += (k >> 3) * 3 + 2;
704 /* check if all input data was processed */
705 if (total_left != 0) {
706 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
708 "Mismatch between mbuf length and included CBs sizes");
712 static inline uint16_t
713 enqueue_enc_all_ops(struct turbo_sw_queue *q, struct rte_bbdev_enc_op **ops,
718 for (i = 0; i < nb_ops; ++i)
719 enqueue_enc_one_op(q, ops[i]);
721 return rte_ring_enqueue_burst(q->processed_pkts, (void **)ops, nb_ops,
725 /* Remove the padding bytes from a cyclic buffer.
726 * The input buffer is a data stream wk as described in 3GPP TS 36.212 section
727 * 5.1.4.1.2 starting from w0 and with length Ncb bytes.
728 * The output buffer is a data stream wk with pruned padding bytes. It's length
729 * is 3*D bytes and the order of non-padding bytes is preserved.
732 remove_nulls_from_circular_buf(const uint8_t *in, uint8_t *out, uint16_t k,
735 uint32_t in_idx, out_idx, c_idx;
736 const uint32_t d = k + 4;
737 const uint32_t kw = (ncb / 3);
738 const uint32_t nd = kw - d;
739 const uint32_t r_subblock = kw / C_SUBBLOCK;
740 /* Inter-column permutation pattern */
741 const uint32_t P[C_SUBBLOCK] = {0, 16, 8, 24, 4, 20, 12, 28, 2, 18, 10,
742 26, 6, 22, 14, 30, 1, 17, 9, 25, 5, 21, 13, 29, 3, 19,
743 11, 27, 7, 23, 15, 31};
747 /* The padding bytes are at the first Nd positions in the first row. */
748 for (c_idx = 0; in_idx < kw; in_idx += r_subblock, ++c_idx) {
750 rte_memcpy(&out[out_idx], &in[in_idx + 1],
752 out_idx += r_subblock - 1;
754 rte_memcpy(&out[out_idx], &in[in_idx], r_subblock);
755 out_idx += r_subblock;
759 /* First and second parity bits sub-blocks are interlaced. */
760 for (c_idx = 0; in_idx < ncb - 2 * r_subblock;
761 in_idx += 2 * r_subblock, ++c_idx) {
762 uint32_t second_block_c_idx = P[c_idx];
763 uint32_t third_block_c_idx = P[c_idx] + 1;
765 if (second_block_c_idx < nd && third_block_c_idx < nd) {
766 rte_memcpy(&out[out_idx], &in[in_idx + 2],
768 out_idx += 2 * r_subblock - 2;
769 } else if (second_block_c_idx >= nd &&
770 third_block_c_idx >= nd) {
771 rte_memcpy(&out[out_idx], &in[in_idx], 2 * r_subblock);
772 out_idx += 2 * r_subblock;
773 } else if (second_block_c_idx < nd) {
774 out[out_idx++] = in[in_idx];
775 rte_memcpy(&out[out_idx], &in[in_idx + 2],
777 out_idx += 2 * r_subblock - 2;
779 rte_memcpy(&out[out_idx], &in[in_idx + 1],
781 out_idx += 2 * r_subblock - 1;
785 /* Last interlaced row is different - its last byte is the only padding
786 * byte. We can have from 2 up to 26 padding bytes (Nd) per sub-block.
787 * After interlacing the 1st and 2nd parity sub-blocks we can have 0, 1
788 * or 2 padding bytes each time we make a step of 2 * R_SUBBLOCK bytes
789 * (moving to another column). 2nd parity sub-block uses the same
790 * inter-column permutation pattern as the systematic and 1st parity
791 * sub-blocks but it adds '1' to the resulting index and calculates the
792 * modulus of the result and Kw. Last column is mapped to itself (id 31)
793 * so the first byte taken from the 2nd parity sub-block will be the
794 * 32nd (31+1) byte, then 64th etc. (step is C_SUBBLOCK == 32) and the
795 * last byte will be the first byte from the sub-block:
796 * (32 + 32 * (R_SUBBLOCK-1)) % Kw == Kw % Kw == 0. Nd can't be smaller
797 * than 2 so we know that bytes with ids 0 and 1 must be the padding
798 * bytes. The bytes from the 1st parity sub-block are the bytes from the
799 * 31st column - Nd can't be greater than 26 so we are sure that there
800 * are no padding bytes in 31st column.
802 rte_memcpy(&out[out_idx], &in[in_idx], 2 * r_subblock - 1);
806 move_padding_bytes(const uint8_t *in, uint8_t *out, uint16_t k,
810 uint16_t kpi = ncb / 3;
811 uint16_t nd = kpi - d;
813 rte_memcpy(&out[nd], in, d);
814 rte_memcpy(&out[nd + kpi + 64], &in[kpi], d);
815 rte_memcpy(&out[nd + 2 * (kpi + 64)], &in[2 * kpi], d);
819 process_dec_cb(struct turbo_sw_queue *q, struct rte_bbdev_dec_op *op,
820 uint8_t c, uint16_t k, uint16_t kw, struct rte_mbuf *m_in,
821 struct rte_mbuf *m_out, uint16_t in_offset, uint16_t out_offset,
822 bool check_crc_24b, uint16_t total_left)
827 uint8_t *in, *out, *adapter_input;
828 int32_t ncb, ncb_without_null;
829 struct bblib_turbo_adapter_ul_response adapter_resp;
830 struct bblib_turbo_adapter_ul_request adapter_req;
831 struct bblib_turbo_decoder_request turbo_req;
832 struct bblib_turbo_decoder_response turbo_resp;
833 struct rte_bbdev_op_turbo_dec *dec = &op->turbo_dec;
835 k_idx = compute_idx(k);
837 ret = is_dec_input_valid(k_idx, kw, total_left);
839 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
843 in = rte_pktmbuf_mtod_offset(m_in, uint8_t *, in_offset);
845 ncb_without_null = (k + 4) * 3;
847 if (check_bit(dec->op_flags, RTE_BBDEV_TURBO_SUBBLOCK_DEINTERLEAVE)) {
848 struct bblib_deinterleave_ul_request deint_req;
849 struct bblib_deinterleave_ul_response deint_resp;
851 /* SW decoder accepts only a circular buffer without NULL bytes
852 * so the input needs to be converted.
854 remove_nulls_from_circular_buf(in, q->deint_input, k, ncb);
856 deint_req.pharqbuffer = q->deint_input;
857 deint_req.ncb = ncb_without_null;
858 deint_resp.pinteleavebuffer = q->deint_output;
859 bblib_deinterleave_ul(&deint_req, &deint_resp);
861 move_padding_bytes(in, q->deint_output, k, ncb);
863 adapter_input = q->deint_output;
865 if (dec->op_flags & RTE_BBDEV_TURBO_POS_LLR_1_BIT_IN)
866 adapter_req.isinverted = 1;
867 else if (dec->op_flags & RTE_BBDEV_TURBO_NEG_LLR_1_BIT_IN)
868 adapter_req.isinverted = 0;
870 op->status |= 1 << RTE_BBDEV_DRV_ERROR;
871 rte_bbdev_log(ERR, "LLR format wasn't specified");
875 adapter_req.ncb = ncb_without_null;
876 adapter_req.pinteleavebuffer = adapter_input;
877 adapter_resp.pharqout = q->adapter_output;
878 bblib_turbo_adapter_ul(&adapter_req, &adapter_resp);
880 out = (uint8_t *)rte_pktmbuf_append(m_out, (k >> 3));
882 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
883 rte_bbdev_log(ERR, "Too little space in output mbuf");
886 /* rte_bbdev_op_data.offset can be different than the offset of the
889 out = rte_pktmbuf_mtod_offset(m_out, uint8_t *, out_offset);
894 turbo_req.input = (int8_t *)q->adapter_output;
896 turbo_req.k_idx = k_idx;
897 turbo_req.max_iter_num = dec->iter_max;
898 turbo_resp.ag_buf = q->ag;
899 turbo_resp.cb_buf = q->code_block;
900 turbo_resp.output = out;
901 iter_cnt = bblib_turbo_decoder(&turbo_req, &turbo_resp);
902 dec->hard_output.length += (k >> 3);
905 /* Temporary solution for returned iter_count from SDK */
906 iter_cnt = (iter_cnt - 1) / 2;
907 dec->iter_count = RTE_MAX(iter_cnt, dec->iter_count);
909 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
910 rte_bbdev_log(ERR, "Turbo Decoder failed");
916 enqueue_dec_one_op(struct turbo_sw_queue *q, struct rte_bbdev_dec_op *op)
920 struct rte_bbdev_op_turbo_dec *dec = &op->turbo_dec;
921 struct rte_mbuf *m_in = dec->input.data;
922 struct rte_mbuf *m_out = dec->hard_output.data;
923 uint16_t in_offset = dec->input.offset;
924 uint16_t total_left = dec->input.length;
925 uint16_t out_offset = dec->hard_output.offset;
927 /* Clear op status */
930 if (m_in == NULL || m_out == NULL) {
931 rte_bbdev_log(ERR, "Invalid mbuf pointer");
932 op->status = 1 << RTE_BBDEV_DATA_ERROR;
936 if (dec->code_block_mode == 0) { /* For Transport Block mode */
937 c = dec->tb_params.c;
938 } else { /* For Code Block mode */
939 k = dec->cb_params.k;
943 while (total_left > 0) {
944 if (dec->code_block_mode == 0)
945 k = (r < dec->tb_params.c_neg) ?
946 dec->tb_params.k_neg : dec->tb_params.k_pos;
948 /* Calculates circular buffer size (Kw).
949 * According to 3gpp 36.212 section 5.1.4.2
953 * where nCol is 32 and nRow can be calculated from:
955 * where D is the size of each output from turbo encoder block
958 kw = RTE_ALIGN_CEIL(k + 4, C_SUBBLOCK) * 3;
960 process_dec_cb(q, op, c, k, kw, m_in, m_out, in_offset,
961 out_offset, check_bit(dec->op_flags,
962 RTE_BBDEV_TURBO_CRC_TYPE_24B), total_left);
963 /* As a result of decoding we get Code Block with included
964 * decoded CRC24 at the end of Code Block. Type of CRC24 is
968 /* Update total_left */
970 /* Update offsets for next CBs (if exist) */
972 out_offset += (k >> 3);
975 if (total_left != 0) {
976 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
978 "Mismatch between mbuf length and included Circular buffer sizes");
982 static inline uint16_t
983 enqueue_dec_all_ops(struct turbo_sw_queue *q, struct rte_bbdev_dec_op **ops,
988 for (i = 0; i < nb_ops; ++i)
989 enqueue_dec_one_op(q, ops[i]);
991 return rte_ring_enqueue_burst(q->processed_pkts, (void **)ops, nb_ops,
997 enqueue_enc_ops(struct rte_bbdev_queue_data *q_data,
998 struct rte_bbdev_enc_op **ops, uint16_t nb_ops)
1000 void *queue = q_data->queue_private;
1001 struct turbo_sw_queue *q = queue;
1002 uint16_t nb_enqueued = 0;
1004 nb_enqueued = enqueue_enc_all_ops(q, ops, nb_ops);
1006 q_data->queue_stats.enqueue_err_count += nb_ops - nb_enqueued;
1007 q_data->queue_stats.enqueued_count += nb_enqueued;
1014 enqueue_dec_ops(struct rte_bbdev_queue_data *q_data,
1015 struct rte_bbdev_dec_op **ops, uint16_t nb_ops)
1017 void *queue = q_data->queue_private;
1018 struct turbo_sw_queue *q = queue;
1019 uint16_t nb_enqueued = 0;
1021 nb_enqueued = enqueue_dec_all_ops(q, ops, nb_ops);
1023 q_data->queue_stats.enqueue_err_count += nb_ops - nb_enqueued;
1024 q_data->queue_stats.enqueued_count += nb_enqueued;
1029 /* Dequeue decode burst */
1031 dequeue_dec_ops(struct rte_bbdev_queue_data *q_data,
1032 struct rte_bbdev_dec_op **ops, uint16_t nb_ops)
1034 struct turbo_sw_queue *q = q_data->queue_private;
1035 uint16_t nb_dequeued = rte_ring_dequeue_burst(q->processed_pkts,
1036 (void **)ops, nb_ops, NULL);
1037 q_data->queue_stats.dequeued_count += nb_dequeued;
1042 /* Dequeue encode burst */
1044 dequeue_enc_ops(struct rte_bbdev_queue_data *q_data,
1045 struct rte_bbdev_enc_op **ops, uint16_t nb_ops)
1047 struct turbo_sw_queue *q = q_data->queue_private;
1048 uint16_t nb_dequeued = rte_ring_dequeue_burst(q->processed_pkts,
1049 (void **)ops, nb_ops, NULL);
1050 q_data->queue_stats.dequeued_count += nb_dequeued;
1055 /* Parse 16bit integer from string argument */
1057 parse_u16_arg(const char *key, const char *value, void *extra_args)
1059 uint16_t *u16 = extra_args;
1060 unsigned int long result;
1062 if ((value == NULL) || (extra_args == NULL))
1065 result = strtoul(value, NULL, 0);
1066 if ((result >= (1 << 16)) || (errno != 0)) {
1067 rte_bbdev_log(ERR, "Invalid value %lu for %s", result, key);
1070 *u16 = (uint16_t)result;
1074 /* Parse parameters used to create device */
1076 parse_turbo_sw_params(struct turbo_sw_params *params, const char *input_args)
1078 struct rte_kvargs *kvlist = NULL;
1084 kvlist = rte_kvargs_parse(input_args, turbo_sw_valid_params);
1088 ret = rte_kvargs_process(kvlist, turbo_sw_valid_params[0],
1089 &parse_u16_arg, ¶ms->queues_num);
1093 ret = rte_kvargs_process(kvlist, turbo_sw_valid_params[1],
1094 &parse_u16_arg, ¶ms->socket_id);
1098 if (params->socket_id >= RTE_MAX_NUMA_NODES) {
1099 rte_bbdev_log(ERR, "Invalid socket, must be < %u",
1100 RTE_MAX_NUMA_NODES);
1107 rte_kvargs_free(kvlist);
1113 turbo_sw_bbdev_create(struct rte_vdev_device *vdev,
1114 struct turbo_sw_params *init_params)
1116 struct rte_bbdev *bbdev;
1117 const char *name = rte_vdev_device_name(vdev);
1119 bbdev = rte_bbdev_allocate(name);
1123 bbdev->data->dev_private = rte_zmalloc_socket(name,
1124 sizeof(struct bbdev_private), RTE_CACHE_LINE_SIZE,
1125 init_params->socket_id);
1126 if (bbdev->data->dev_private == NULL) {
1127 rte_bbdev_release(bbdev);
1131 bbdev->dev_ops = &pmd_ops;
1132 bbdev->device = &vdev->device;
1133 bbdev->data->socket_id = init_params->socket_id;
1134 bbdev->intr_handle = NULL;
1136 /* register rx/tx burst functions for data path */
1137 bbdev->dequeue_enc_ops = dequeue_enc_ops;
1138 bbdev->dequeue_dec_ops = dequeue_dec_ops;
1139 bbdev->enqueue_enc_ops = enqueue_enc_ops;
1140 bbdev->enqueue_dec_ops = enqueue_dec_ops;
1141 ((struct bbdev_private *) bbdev->data->dev_private)->max_nb_queues =
1142 init_params->queues_num;
1147 /* Initialise device */
1149 turbo_sw_bbdev_probe(struct rte_vdev_device *vdev)
1151 struct turbo_sw_params init_params = {
1153 RTE_BBDEV_DEFAULT_MAX_NB_QUEUES
1156 const char *input_args;
1161 name = rte_vdev_device_name(vdev);
1164 input_args = rte_vdev_device_args(vdev);
1165 parse_turbo_sw_params(&init_params, input_args);
1167 rte_bbdev_log_debug(
1168 "Initialising %s on NUMA node %d with max queues: %d\n",
1169 name, init_params.socket_id, init_params.queues_num);
1171 return turbo_sw_bbdev_create(vdev, &init_params);
1174 /* Uninitialise device */
1176 turbo_sw_bbdev_remove(struct rte_vdev_device *vdev)
1178 struct rte_bbdev *bbdev;
1184 name = rte_vdev_device_name(vdev);
1188 bbdev = rte_bbdev_get_named_dev(name);
1192 rte_free(bbdev->data->dev_private);
1194 return rte_bbdev_release(bbdev);
1197 static struct rte_vdev_driver bbdev_turbo_sw_pmd_drv = {
1198 .probe = turbo_sw_bbdev_probe,
1199 .remove = turbo_sw_bbdev_remove
1202 RTE_PMD_REGISTER_VDEV(DRIVER_NAME, bbdev_turbo_sw_pmd_drv);
1203 RTE_PMD_REGISTER_PARAM_STRING(DRIVER_NAME,
1204 TURBO_SW_MAX_NB_QUEUES_ARG"=<int> "
1205 TURBO_SW_SOCKET_ID_ARG"=<int>");
1207 RTE_INIT(null_bbdev_init_log);
1209 null_bbdev_init_log(void)
1211 bbdev_turbo_sw_logtype = rte_log_register("pmd.bb.turbo_sw");
1212 if (bbdev_turbo_sw_logtype >= 0)
1213 rte_log_set_level(bbdev_turbo_sw_logtype, RTE_LOG_NOTICE);