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>
12 #include <rte_cycles.h>
14 #include <rte_bbdev.h>
15 #include <rte_bbdev_pmd.h>
17 #include <rte_hexdump.h>
20 #ifdef RTE_BBDEV_SDK_AVX2
23 #include <phy_turbo.h>
25 #include <phy_rate_match.h>
27 #ifdef RTE_BBDEV_SDK_AVX512
28 #include <bit_reverse.h>
29 #include <phy_ldpc_encoder_5gnr.h>
30 #include <phy_ldpc_decoder_5gnr.h>
31 #include <phy_LDPC_ratematch_5gnr.h>
32 #include <phy_rate_dematching_5gnr.h>
35 #define DRIVER_NAME baseband_turbo_sw
37 /* Turbo SW PMD logging ID */
38 static int bbdev_turbo_sw_logtype;
40 /* Helper macro for logging */
41 #define rte_bbdev_log(level, fmt, ...) \
42 rte_log(RTE_LOG_ ## level, bbdev_turbo_sw_logtype, fmt "\n", \
45 #define rte_bbdev_log_debug(fmt, ...) \
46 rte_bbdev_log(DEBUG, RTE_STR(__LINE__) ":%s() " fmt, __func__, \
49 #define DEINT_INPUT_BUF_SIZE (((RTE_BBDEV_TURBO_MAX_CB_SIZE >> 3) + 1) * 48)
50 #define DEINT_OUTPUT_BUF_SIZE (DEINT_INPUT_BUF_SIZE * 6)
51 #define ADAPTER_OUTPUT_BUF_SIZE ((RTE_BBDEV_TURBO_MAX_CB_SIZE + 4) * 48)
53 /* private data structure */
54 struct bbdev_private {
55 unsigned int max_nb_queues; /**< Max number of queues */
58 /* Initialisation params structure that can be used by Turbo SW driver */
59 struct turbo_sw_params {
60 int socket_id; /*< Turbo SW device socket */
61 uint16_t queues_num; /*< Turbo SW device queues number */
64 /* Accecptable params for Turbo SW devices */
65 #define TURBO_SW_MAX_NB_QUEUES_ARG "max_nb_queues"
66 #define TURBO_SW_SOCKET_ID_ARG "socket_id"
68 static const char * const turbo_sw_valid_params[] = {
69 TURBO_SW_MAX_NB_QUEUES_ARG,
70 TURBO_SW_SOCKET_ID_ARG
74 struct turbo_sw_queue {
75 /* Ring for processed (encoded/decoded) operations which are ready to
78 struct rte_ring *processed_pkts;
79 /* Stores input for turbo encoder (used when CRC attachment is
83 /* Stores output from turbo encoder */
85 /* Alpha gamma buf for bblib_turbo_decoder() function */
87 /* Temp buf for bblib_turbo_decoder() function */
89 /* Input buf for bblib_rate_dematching_lte() function */
91 /* Output buf for bblib_rate_dematching_lte() function */
92 uint8_t *deint_output;
93 /* Output buf for bblib_turbodec_adapter_lte() function */
94 uint8_t *adapter_output;
95 /* Operation type of this queue */
96 enum rte_bbdev_op_type type;
97 } __rte_cache_aligned;
100 #ifdef RTE_BBDEV_SDK_AVX2
102 mbuf_append(struct rte_mbuf *m_head, struct rte_mbuf *m, uint16_t len)
104 if (unlikely(len > rte_pktmbuf_tailroom(m)))
107 char *tail = (char *)m->buf_addr + m->data_off + m->data_len;
108 m->data_len = (uint16_t)(m->data_len + len);
109 m_head->pkt_len = (m_head->pkt_len + len);
113 /* Calculate index based on Table 5.1.3-3 from TS34.212 */
114 static inline int32_t
115 compute_idx(uint16_t k)
119 if (k < RTE_BBDEV_TURBO_MIN_CB_SIZE || k > RTE_BBDEV_TURBO_MAX_CB_SIZE)
123 if ((k - 2048) % 64 != 0)
126 result = 124 + (k - 2048) / 64;
127 } else if (k <= 512) {
128 if ((k - 40) % 8 != 0)
131 result = (k - 40) / 8 + 1;
132 } else if (k <= 1024) {
133 if ((k - 512) % 16 != 0)
136 result = 60 + (k - 512) / 16;
137 } else { /* 1024 < k <= 2048 */
138 if ((k - 1024) % 32 != 0)
141 result = 92 + (k - 1024) / 32;
148 /* Read flag value 0/1 from bitmap */
150 check_bit(uint32_t bitmap, uint32_t bitmask)
152 return bitmap & bitmask;
155 /* Get device info */
157 info_get(struct rte_bbdev *dev, struct rte_bbdev_driver_info *dev_info)
159 struct bbdev_private *internals = dev->data->dev_private;
161 static const struct rte_bbdev_op_cap bbdev_capabilities[] = {
162 #ifdef RTE_BBDEV_SDK_AVX2
164 .type = RTE_BBDEV_OP_TURBO_DEC,
167 RTE_BBDEV_TURBO_SUBBLOCK_DEINTERLEAVE |
168 RTE_BBDEV_TURBO_POS_LLR_1_BIT_IN |
169 RTE_BBDEV_TURBO_NEG_LLR_1_BIT_IN |
170 RTE_BBDEV_TURBO_CRC_TYPE_24B |
171 RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP |
172 RTE_BBDEV_TURBO_EARLY_TERMINATION,
173 .max_llr_modulus = 16,
175 RTE_BBDEV_TURBO_MAX_CODE_BLOCKS,
176 .num_buffers_hard_out =
177 RTE_BBDEV_TURBO_MAX_CODE_BLOCKS,
178 .num_buffers_soft_out = 0,
182 .type = RTE_BBDEV_OP_TURBO_ENC,
185 RTE_BBDEV_TURBO_CRC_24B_ATTACH |
186 RTE_BBDEV_TURBO_CRC_24A_ATTACH |
187 RTE_BBDEV_TURBO_RATE_MATCH |
188 RTE_BBDEV_TURBO_RV_INDEX_BYPASS,
190 RTE_BBDEV_TURBO_MAX_CODE_BLOCKS,
192 RTE_BBDEV_TURBO_MAX_CODE_BLOCKS,
196 #ifdef RTE_BBDEV_SDK_AVX512
198 .type = RTE_BBDEV_OP_LDPC_ENC,
201 RTE_BBDEV_LDPC_RATE_MATCH |
202 RTE_BBDEV_LDPC_CRC_24A_ATTACH |
203 RTE_BBDEV_LDPC_CRC_24B_ATTACH,
205 RTE_BBDEV_LDPC_MAX_CODE_BLOCKS,
207 RTE_BBDEV_LDPC_MAX_CODE_BLOCKS,
211 .type = RTE_BBDEV_OP_LDPC_DEC,
214 RTE_BBDEV_LDPC_CRC_TYPE_24B_CHECK |
215 RTE_BBDEV_LDPC_CRC_TYPE_24A_CHECK |
216 RTE_BBDEV_LDPC_CRC_TYPE_24B_DROP |
217 RTE_BBDEV_LDPC_HQ_COMBINE_IN_ENABLE |
218 RTE_BBDEV_LDPC_HQ_COMBINE_OUT_ENABLE |
219 RTE_BBDEV_LDPC_ITERATION_STOP_ENABLE,
222 .harq_memory_size = 0,
224 RTE_BBDEV_LDPC_MAX_CODE_BLOCKS,
225 .num_buffers_hard_out =
226 RTE_BBDEV_LDPC_MAX_CODE_BLOCKS,
227 .num_buffers_soft_out = 0,
231 RTE_BBDEV_END_OF_CAPABILITIES_LIST()
234 static struct rte_bbdev_queue_conf default_queue_conf = {
235 .queue_size = RTE_BBDEV_QUEUE_SIZE_LIMIT,
237 #ifdef RTE_BBDEV_SDK_AVX2
238 static const enum rte_cpu_flag_t cpu_flag = RTE_CPUFLAG_SSE4_2;
239 dev_info->cpu_flag_reqs = &cpu_flag;
241 dev_info->cpu_flag_reqs = NULL;
243 default_queue_conf.socket = dev->data->socket_id;
245 dev_info->driver_name = RTE_STR(DRIVER_NAME);
246 dev_info->max_num_queues = internals->max_nb_queues;
247 dev_info->queue_size_lim = RTE_BBDEV_QUEUE_SIZE_LIMIT;
248 dev_info->hardware_accelerated = false;
249 dev_info->max_dl_queue_priority = 0;
250 dev_info->max_ul_queue_priority = 0;
251 dev_info->default_queue_conf = default_queue_conf;
252 dev_info->capabilities = bbdev_capabilities;
253 dev_info->min_alignment = 64;
255 rte_bbdev_log_debug("got device info from %u\n", dev->data->dev_id);
260 q_release(struct rte_bbdev *dev, uint16_t q_id)
262 struct turbo_sw_queue *q = dev->data->queues[q_id].queue_private;
265 rte_ring_free(q->processed_pkts);
266 rte_free(q->enc_out);
269 rte_free(q->code_block);
270 rte_free(q->deint_input);
271 rte_free(q->deint_output);
272 rte_free(q->adapter_output);
274 dev->data->queues[q_id].queue_private = NULL;
277 rte_bbdev_log_debug("released device queue %u:%u",
278 dev->data->dev_id, q_id);
284 q_setup(struct rte_bbdev *dev, uint16_t q_id,
285 const struct rte_bbdev_queue_conf *queue_conf)
288 struct turbo_sw_queue *q;
289 char name[RTE_RING_NAMESIZE];
291 /* Allocate the queue data structure. */
292 q = rte_zmalloc_socket(RTE_STR(DRIVER_NAME), sizeof(*q),
293 RTE_CACHE_LINE_SIZE, queue_conf->socket);
295 rte_bbdev_log(ERR, "Failed to allocate queue memory");
299 /* Allocate memory for encoder output. */
300 ret = snprintf(name, RTE_RING_NAMESIZE, RTE_STR(DRIVER_NAME)"_enc_o%u:%u",
301 dev->data->dev_id, q_id);
302 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
304 "Creating queue name for device %u queue %u failed",
305 dev->data->dev_id, q_id);
306 return -ENAMETOOLONG;
308 q->enc_out = rte_zmalloc_socket(name,
309 ((RTE_BBDEV_TURBO_MAX_TB_SIZE >> 3) + 3) *
310 sizeof(*q->enc_out) * 3,
311 RTE_CACHE_LINE_SIZE, queue_conf->socket);
312 if (q->enc_out == NULL) {
314 "Failed to allocate queue memory for %s", name);
318 /* Allocate memory for rate matching output. */
319 ret = snprintf(name, RTE_RING_NAMESIZE,
320 RTE_STR(DRIVER_NAME)"_enc_i%u:%u", dev->data->dev_id,
322 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
324 "Creating queue name for device %u queue %u failed",
325 dev->data->dev_id, q_id);
326 return -ENAMETOOLONG;
328 q->enc_in = rte_zmalloc_socket(name,
329 (RTE_BBDEV_LDPC_MAX_CB_SIZE >> 3) * sizeof(*q->enc_in),
330 RTE_CACHE_LINE_SIZE, queue_conf->socket);
331 if (q->enc_in == NULL) {
333 "Failed to allocate queue memory for %s", name);
337 /* Allocate memory for Alpha Gamma temp buffer. */
338 ret = snprintf(name, RTE_RING_NAMESIZE, RTE_STR(DRIVER_NAME)"_ag%u:%u",
339 dev->data->dev_id, q_id);
340 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
342 "Creating queue name for device %u queue %u failed",
343 dev->data->dev_id, q_id);
344 return -ENAMETOOLONG;
346 q->ag = rte_zmalloc_socket(name,
347 RTE_BBDEV_TURBO_MAX_CB_SIZE * 10 * sizeof(*q->ag),
348 RTE_CACHE_LINE_SIZE, queue_conf->socket);
351 "Failed to allocate queue memory for %s", name);
355 /* Allocate memory for code block temp buffer. */
356 ret = snprintf(name, RTE_RING_NAMESIZE, RTE_STR(DRIVER_NAME)"_cb%u:%u",
357 dev->data->dev_id, q_id);
358 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
360 "Creating queue name for device %u queue %u failed",
361 dev->data->dev_id, q_id);
362 return -ENAMETOOLONG;
364 q->code_block = rte_zmalloc_socket(name,
365 RTE_BBDEV_TURBO_MAX_CB_SIZE * sizeof(*q->code_block),
366 RTE_CACHE_LINE_SIZE, queue_conf->socket);
367 if (q->code_block == NULL) {
369 "Failed to allocate queue memory for %s", name);
373 /* Allocate memory for Deinterleaver input. */
374 ret = snprintf(name, RTE_RING_NAMESIZE,
375 RTE_STR(DRIVER_NAME)"_de_i%u:%u",
376 dev->data->dev_id, q_id);
377 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
379 "Creating queue name for device %u queue %u failed",
380 dev->data->dev_id, q_id);
381 return -ENAMETOOLONG;
383 q->deint_input = rte_zmalloc_socket(name,
384 DEINT_INPUT_BUF_SIZE * sizeof(*q->deint_input),
385 RTE_CACHE_LINE_SIZE, queue_conf->socket);
386 if (q->deint_input == NULL) {
388 "Failed to allocate queue memory for %s", name);
392 /* Allocate memory for Deinterleaver output. */
393 ret = snprintf(name, RTE_RING_NAMESIZE,
394 RTE_STR(DRIVER_NAME)"_de_o%u:%u",
395 dev->data->dev_id, q_id);
396 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
398 "Creating queue name for device %u queue %u failed",
399 dev->data->dev_id, q_id);
400 return -ENAMETOOLONG;
402 q->deint_output = rte_zmalloc_socket(NULL,
403 DEINT_OUTPUT_BUF_SIZE * sizeof(*q->deint_output),
404 RTE_CACHE_LINE_SIZE, queue_conf->socket);
405 if (q->deint_output == NULL) {
407 "Failed to allocate queue memory for %s", name);
411 /* Allocate memory for Adapter output. */
412 ret = snprintf(name, RTE_RING_NAMESIZE,
413 RTE_STR(DRIVER_NAME)"_ada_o%u:%u",
414 dev->data->dev_id, q_id);
415 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
417 "Creating queue name for device %u queue %u failed",
418 dev->data->dev_id, q_id);
419 return -ENAMETOOLONG;
421 q->adapter_output = rte_zmalloc_socket(NULL,
422 ADAPTER_OUTPUT_BUF_SIZE * sizeof(*q->adapter_output),
423 RTE_CACHE_LINE_SIZE, queue_conf->socket);
424 if (q->adapter_output == NULL) {
426 "Failed to allocate queue memory for %s", name);
430 /* Create ring for packets awaiting to be dequeued. */
431 ret = snprintf(name, RTE_RING_NAMESIZE, RTE_STR(DRIVER_NAME)"%u:%u",
432 dev->data->dev_id, q_id);
433 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
435 "Creating queue name for device %u queue %u failed",
436 dev->data->dev_id, q_id);
437 return -ENAMETOOLONG;
439 q->processed_pkts = rte_ring_create(name, queue_conf->queue_size,
440 queue_conf->socket, RING_F_SP_ENQ | RING_F_SC_DEQ);
441 if (q->processed_pkts == NULL) {
442 rte_bbdev_log(ERR, "Failed to create ring for %s", name);
446 q->type = queue_conf->op_type;
448 dev->data->queues[q_id].queue_private = q;
449 rte_bbdev_log_debug("setup device queue %s", name);
453 rte_ring_free(q->processed_pkts);
454 rte_free(q->enc_out);
457 rte_free(q->code_block);
458 rte_free(q->deint_input);
459 rte_free(q->deint_output);
460 rte_free(q->adapter_output);
465 static const struct rte_bbdev_ops pmd_ops = {
466 .info_get = info_get,
467 .queue_setup = q_setup,
468 .queue_release = q_release
471 #ifdef RTE_BBDEV_SDK_AVX2
472 #ifdef RTE_LIBRTE_BBDEV_DEBUG
473 /* Checks if the encoder input buffer is correct.
474 * Returns 0 if it's valid, -1 otherwise.
477 is_enc_input_valid(const uint16_t k, const int32_t k_idx,
478 const uint16_t in_length)
481 rte_bbdev_log(ERR, "K Index is invalid");
485 if (in_length - (k >> 3) < 0) {
487 "Mismatch between input length (%u bytes) and K (%u bits)",
492 if (k > RTE_BBDEV_TURBO_MAX_CB_SIZE) {
493 rte_bbdev_log(ERR, "CB size (%u) is too big, max: %d",
494 k, RTE_BBDEV_TURBO_MAX_CB_SIZE);
501 /* Checks if the decoder input buffer is correct.
502 * Returns 0 if it's valid, -1 otherwise.
505 is_dec_input_valid(int32_t k_idx, int16_t kw, int16_t in_length)
508 rte_bbdev_log(ERR, "K index is invalid");
512 if (in_length < kw) {
514 "Mismatch between input length (%u) and kw (%u)",
519 if (kw > RTE_BBDEV_TURBO_MAX_KW) {
520 rte_bbdev_log(ERR, "Input length (%u) is too big, max: %d",
521 kw, RTE_BBDEV_TURBO_MAX_KW);
531 process_enc_cb(struct turbo_sw_queue *q, struct rte_bbdev_enc_op *op,
532 uint8_t r, uint8_t c, uint16_t k, uint16_t ncb,
533 uint32_t e, struct rte_mbuf *m_in, struct rte_mbuf *m_out_head,
534 struct rte_mbuf *m_out, uint16_t in_offset, uint16_t out_offset,
535 uint16_t in_length, struct rte_bbdev_stats *q_stats)
537 #ifdef RTE_BBDEV_SDK_AVX2
538 #ifdef RTE_LIBRTE_BBDEV_DEBUG
541 RTE_SET_USED(in_length);
545 uint8_t *in, *out0, *out1, *out2, *tmp_out, *rm_out;
546 uint64_t first_3_bytes = 0;
547 struct rte_bbdev_op_turbo_enc *enc = &op->turbo_enc;
548 struct bblib_crc_request crc_req;
549 struct bblib_crc_response crc_resp;
550 struct bblib_turbo_encoder_request turbo_req;
551 struct bblib_turbo_encoder_response turbo_resp;
552 struct bblib_rate_match_dl_request rm_req;
553 struct bblib_rate_match_dl_response rm_resp;
554 #ifdef RTE_BBDEV_OFFLOAD_COST
557 RTE_SET_USED(q_stats);
560 k_idx = compute_idx(k);
561 in = rte_pktmbuf_mtod_offset(m_in, uint8_t *, in_offset);
563 /* CRC24A (for TB) */
564 if ((enc->op_flags & RTE_BBDEV_TURBO_CRC_24A_ATTACH) &&
565 (enc->code_block_mode == 1)) {
566 #ifdef RTE_LIBRTE_BBDEV_DEBUG
567 ret = is_enc_input_valid(k - 24, k_idx, in_length);
569 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
575 crc_req.len = k - 24;
576 /* Check if there is a room for CRC bits if not use
577 * the temporary buffer.
579 if (mbuf_append(m_in, m_in, 3) == NULL) {
580 rte_memcpy(q->enc_in, in, (k - 24) >> 3);
583 /* Store 3 first bytes of next CB as they will be
584 * overwritten by CRC bytes. If it is the last CB then
585 * there is no point to store 3 next bytes and this
586 * if..else branch will be omitted.
588 first_3_bytes = *((uint64_t *)&in[(k - 32) >> 3]);
592 #ifdef RTE_BBDEV_OFFLOAD_COST
593 start_time = rte_rdtsc_precise();
595 /* CRC24A generation */
596 bblib_lte_crc24a_gen(&crc_req, &crc_resp);
597 #ifdef RTE_BBDEV_OFFLOAD_COST
598 q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
600 } else if (enc->op_flags & RTE_BBDEV_TURBO_CRC_24B_ATTACH) {
602 #ifdef RTE_LIBRTE_BBDEV_DEBUG
603 ret = is_enc_input_valid(k - 24, k_idx, in_length);
605 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
611 crc_req.len = k - 24;
612 /* Check if there is a room for CRC bits if this is the last
613 * CB in TB. If not use temporary buffer.
615 if ((c - r == 1) && (mbuf_append(m_in, m_in, 3) == NULL)) {
616 rte_memcpy(q->enc_in, in, (k - 24) >> 3);
618 } else if (c - r > 1) {
619 /* Store 3 first bytes of next CB as they will be
620 * overwritten by CRC bytes. If it is the last CB then
621 * there is no point to store 3 next bytes and this
622 * if..else branch will be omitted.
624 first_3_bytes = *((uint64_t *)&in[(k - 32) >> 3]);
628 #ifdef RTE_BBDEV_OFFLOAD_COST
629 start_time = rte_rdtsc_precise();
631 /* CRC24B generation */
632 bblib_lte_crc24b_gen(&crc_req, &crc_resp);
633 #ifdef RTE_BBDEV_OFFLOAD_COST
634 q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
637 #ifdef RTE_LIBRTE_BBDEV_DEBUG
639 ret = is_enc_input_valid(k, k_idx, in_length);
641 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
649 /* Each bit layer output from turbo encoder is (k+4) bits long, i.e.
650 * input length + 4 tail bits. That's (k/8) + 1 bytes after rounding up.
651 * So dst_data's length should be 3*(k/8) + 3 bytes.
652 * In Rate-matching bypass case outputs pointers passed to encoder
653 * (out0, out1 and out2) can directly point to addresses of output from
656 if (enc->op_flags & RTE_BBDEV_TURBO_RATE_MATCH) {
658 out1 = RTE_PTR_ADD(out0, (k >> 3) + 1);
659 out2 = RTE_PTR_ADD(out1, (k >> 3) + 1);
661 out0 = (uint8_t *)mbuf_append(m_out_head, m_out,
664 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
666 "Too little space in output mbuf");
669 enc->output.length += (k >> 3) * 3 + 2;
670 /* rte_bbdev_op_data.offset can be different than the
671 * offset of the appended bytes
673 out0 = rte_pktmbuf_mtod_offset(m_out, uint8_t *, out_offset);
674 out1 = rte_pktmbuf_mtod_offset(m_out, uint8_t *,
675 out_offset + (k >> 3) + 1);
676 out2 = rte_pktmbuf_mtod_offset(m_out, uint8_t *,
677 out_offset + 2 * ((k >> 3) + 1));
680 turbo_req.case_id = k_idx;
681 turbo_req.input_win = in;
682 turbo_req.length = k >> 3;
683 turbo_resp.output_win_0 = out0;
684 turbo_resp.output_win_1 = out1;
685 turbo_resp.output_win_2 = out2;
687 #ifdef RTE_BBDEV_OFFLOAD_COST
688 start_time = rte_rdtsc_precise();
691 if (bblib_turbo_encoder(&turbo_req, &turbo_resp) != 0) {
692 op->status |= 1 << RTE_BBDEV_DRV_ERROR;
693 rte_bbdev_log(ERR, "Turbo Encoder failed");
696 #ifdef RTE_BBDEV_OFFLOAD_COST
697 q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
700 /* Restore 3 first bytes of next CB if they were overwritten by CRC*/
701 if (first_3_bytes != 0)
702 *((uint64_t *)&in[(k - 32) >> 3]) = first_3_bytes;
705 if (enc->op_flags & RTE_BBDEV_TURBO_RATE_MATCH) {
707 /* Integer round up division by 8 */
708 uint16_t out_len = (e + 7) >> 3;
709 /* The mask array is indexed using E%8. E is an even number so
710 * there are only 4 possible values.
712 const uint8_t mask_out[] = {0xFF, 0xC0, 0xF0, 0xFC};
714 /* get output data starting address */
715 rm_out = (uint8_t *)mbuf_append(m_out_head, m_out, out_len);
716 if (rm_out == NULL) {
717 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
719 "Too little space in output mbuf");
722 /* rte_bbdev_op_data.offset can be different than the offset
723 * of the appended bytes
725 rm_out = rte_pktmbuf_mtod_offset(m_out, uint8_t *, out_offset);
727 /* index of current code block */
729 /* total number of code block */
731 /* For DL - 1, UL - 0 */
732 rm_req.direction = 1;
733 /* According to 3ggp 36.212 Spec 5.1.4.1.2 section Nsoft, KMIMO
734 * and MDL_HARQ are used for Ncb calculation. As Ncb is already
735 * known we can adjust those parameters
737 rm_req.Nsoft = ncb * rm_req.C;
740 /* According to 3ggp 36.212 Spec 5.1.4.1.2 section Nl, Qm and G
741 * are used for E calculation. As E is already known we can
742 * adjust those parameters
746 rm_req.G = rm_req.NL * rm_req.Qm * rm_req.C;
748 rm_req.rvidx = enc->rv_index;
749 rm_req.Kidx = k_idx - 1;
754 rm_resp.output = rm_out;
755 rm_resp.OutputLen = out_len;
756 if (enc->op_flags & RTE_BBDEV_TURBO_RV_INDEX_BYPASS)
757 rm_req.bypass_rvidx = 1;
759 rm_req.bypass_rvidx = 0;
761 #ifdef RTE_BBDEV_OFFLOAD_COST
762 start_time = rte_rdtsc_precise();
765 if (bblib_rate_match_dl(&rm_req, &rm_resp) != 0) {
766 op->status |= 1 << RTE_BBDEV_DRV_ERROR;
767 rte_bbdev_log(ERR, "Rate matching failed");
770 #ifdef RTE_BBDEV_OFFLOAD_COST
771 q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
774 /* SW fills an entire last byte even if E%8 != 0. Clear the
775 * superfluous data bits for consistency with HW device.
777 mask_id = (e & 7) >> 1;
778 rm_out[out_len - 1] &= mask_out[mask_id];
779 enc->output.length += rm_resp.OutputLen;
781 /* Rate matching is bypassed */
783 /* Completing last byte of out0 (where 4 tail bits are stored)
784 * by moving first 4 bits from out1
786 tmp_out = (uint8_t *) --out1;
787 *tmp_out = *tmp_out | ((*(tmp_out + 1) & 0xF0) >> 4);
789 /* Shifting out1 data by 4 bits to the left */
790 for (m = 0; m < k >> 3; ++m) {
791 uint8_t *first = tmp_out;
792 uint8_t second = *(tmp_out + 1);
793 *first = (*first << 4) | ((second & 0xF0) >> 4);
796 /* Shifting out2 data by 8 bits to the left */
797 for (m = 0; m < (k >> 3) + 1; ++m) {
798 *tmp_out = *(tmp_out + 1);
812 RTE_SET_USED(m_out_head);
814 RTE_SET_USED(in_offset);
815 RTE_SET_USED(out_offset);
816 RTE_SET_USED(in_length);
817 RTE_SET_USED(q_stats);
823 process_ldpc_enc_cb(struct turbo_sw_queue *q, struct rte_bbdev_enc_op *op,
824 uint32_t e, struct rte_mbuf *m_in, struct rte_mbuf *m_out_head,
825 struct rte_mbuf *m_out, uint16_t in_offset, uint16_t out_offset,
826 uint16_t seg_total_left, struct rte_bbdev_stats *q_stats)
828 #ifdef RTE_BBDEV_SDK_AVX512
829 RTE_SET_USED(seg_total_left);
830 uint8_t *in, *rm_out;
831 struct rte_bbdev_op_ldpc_enc *enc = &op->ldpc_enc;
832 struct bblib_ldpc_encoder_5gnr_request ldpc_req;
833 struct bblib_ldpc_encoder_5gnr_response ldpc_resp;
834 struct bblib_LDPC_ratematch_5gnr_request rm_req;
835 struct bblib_LDPC_ratematch_5gnr_response rm_resp;
836 struct bblib_crc_request crc_req;
837 struct bblib_crc_response crc_resp;
838 uint16_t msgLen, puntBits, parity_offset, out_len;
839 uint16_t K = (enc->basegraph == 1 ? 22 : 10) * enc->z_c;
840 uint16_t in_length_in_bits = K - enc->n_filler;
841 uint16_t in_length_in_bytes = (in_length_in_bits + 7) >> 3;
843 #ifdef RTE_BBDEV_OFFLOAD_COST
844 uint64_t start_time = rte_rdtsc_precise();
846 RTE_SET_USED(q_stats);
849 in = rte_pktmbuf_mtod_offset(m_in, uint8_t *, in_offset);
851 /* Masking the Filler bits explicitly */
852 memset(q->enc_in + (in_length_in_bytes - 3), 0,
853 ((K + 7) >> 3) - (in_length_in_bytes - 3));
855 if (enc->op_flags & RTE_BBDEV_LDPC_CRC_24A_ATTACH) {
856 rte_memcpy(q->enc_in, in, in_length_in_bytes - 3);
858 crc_req.len = in_length_in_bits - 24;
859 crc_resp.data = q->enc_in;
860 bblib_lte_crc24a_gen(&crc_req, &crc_resp);
861 } else if (enc->op_flags & RTE_BBDEV_LDPC_CRC_24B_ATTACH) {
862 rte_memcpy(q->enc_in, in, in_length_in_bytes - 3);
864 crc_req.len = in_length_in_bits - 24;
865 crc_resp.data = q->enc_in;
866 bblib_lte_crc24b_gen(&crc_req, &crc_resp);
868 rte_memcpy(q->enc_in, in, in_length_in_bytes);
871 ldpc_req.Zc = enc->z_c;
872 ldpc_req.baseGraph = enc->basegraph;
873 /* Number of rows set to maximum */
874 ldpc_req.nRows = ldpc_req.baseGraph == 1 ? 46 : 42;
875 ldpc_req.numberCodeblocks = 1;
876 ldpc_req.input[0] = (int8_t *) q->enc_in;
877 ldpc_resp.output[0] = (int8_t *) q->enc_out;
879 bblib_bit_reverse(ldpc_req.input[0], in_length_in_bytes << 3);
881 if (bblib_ldpc_encoder_5gnr(&ldpc_req, &ldpc_resp) != 0) {
882 op->status |= 1 << RTE_BBDEV_DRV_ERROR;
883 rte_bbdev_log(ERR, "LDPC Encoder failed");
888 * Systematic + Parity : Recreating stream with filler bits, ideally
889 * the bit select could handle this in the RM SDK
891 msgLen = (ldpc_req.baseGraph == 1 ? 22 : 10) * ldpc_req.Zc;
892 puntBits = 2 * ldpc_req.Zc;
893 parity_offset = msgLen - puntBits;
894 ippsCopyBE_1u(((uint8_t *) ldpc_req.input[0]) + (puntBits / 8),
895 puntBits%8, q->adapter_output, 0, parity_offset);
896 ippsCopyBE_1u(q->enc_out, 0, q->adapter_output + (parity_offset / 8),
897 parity_offset % 8, ldpc_req.nRows * ldpc_req.Zc);
899 out_len = (e + 7) >> 3;
900 /* get output data starting address */
901 rm_out = (uint8_t *)mbuf_append(m_out_head, m_out, out_len);
902 if (rm_out == NULL) {
903 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
905 "Too little space in output mbuf");
909 * rte_bbdev_op_data.offset can be different than the offset
910 * of the appended bytes
912 rm_out = rte_pktmbuf_mtod_offset(m_out, uint8_t *, out_offset);
916 rm_req.Ncb = enc->n_cb;
917 rm_req.Qm = enc->q_m;
918 rm_req.Zc = enc->z_c;
919 rm_req.baseGraph = enc->basegraph;
920 rm_req.input = q->adapter_output;
921 rm_req.nLen = enc->n_filler;
922 rm_req.nullIndex = parity_offset - enc->n_filler;
923 rm_req.rvidx = enc->rv_index;
924 rm_resp.output = q->deint_output;
926 if (bblib_LDPC_ratematch_5gnr(&rm_req, &rm_resp) != 0) {
927 op->status |= 1 << RTE_BBDEV_DRV_ERROR;
928 rte_bbdev_log(ERR, "Rate matching failed");
932 /* RM SDK may provide non zero bits on last byte */
934 q->deint_output[out_len-1] &= (1 << (e % 8)) - 1;
936 bblib_bit_reverse((int8_t *) q->deint_output, out_len << 3);
938 rte_memcpy(rm_out, q->deint_output, out_len);
939 enc->output.length += out_len;
941 #ifdef RTE_BBDEV_OFFLOAD_COST
942 q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
949 RTE_SET_USED(m_out_head);
951 RTE_SET_USED(in_offset);
952 RTE_SET_USED(out_offset);
953 RTE_SET_USED(seg_total_left);
954 RTE_SET_USED(q_stats);
959 enqueue_enc_one_op(struct turbo_sw_queue *q, struct rte_bbdev_enc_op *op,
960 struct rte_bbdev_stats *queue_stats)
962 uint8_t c, r, crc24_bits = 0;
965 struct rte_bbdev_op_turbo_enc *enc = &op->turbo_enc;
966 uint16_t in_offset = enc->input.offset;
967 uint16_t out_offset = enc->output.offset;
968 struct rte_mbuf *m_in = enc->input.data;
969 struct rte_mbuf *m_out = enc->output.data;
970 struct rte_mbuf *m_out_head = enc->output.data;
971 uint32_t in_length, mbuf_total_left = enc->input.length;
972 uint16_t seg_total_left;
974 /* Clear op status */
977 if (mbuf_total_left > RTE_BBDEV_TURBO_MAX_TB_SIZE >> 3) {
978 rte_bbdev_log(ERR, "TB size (%u) is too big, max: %d",
979 mbuf_total_left, RTE_BBDEV_TURBO_MAX_TB_SIZE);
980 op->status = 1 << RTE_BBDEV_DATA_ERROR;
984 if (m_in == NULL || m_out == NULL) {
985 rte_bbdev_log(ERR, "Invalid mbuf pointer");
986 op->status = 1 << RTE_BBDEV_DATA_ERROR;
990 if ((enc->op_flags & RTE_BBDEV_TURBO_CRC_24B_ATTACH) ||
991 (enc->op_flags & RTE_BBDEV_TURBO_CRC_24A_ATTACH))
994 if (enc->code_block_mode == 0) { /* For Transport Block mode */
995 c = enc->tb_params.c;
996 r = enc->tb_params.r;
997 } else {/* For Code Block mode */
1002 while (mbuf_total_left > 0 && r < c) {
1004 seg_total_left = rte_pktmbuf_data_len(m_in) - in_offset;
1006 if (enc->code_block_mode == 0) {
1007 k = (r < enc->tb_params.c_neg) ?
1008 enc->tb_params.k_neg : enc->tb_params.k_pos;
1009 ncb = (r < enc->tb_params.c_neg) ?
1010 enc->tb_params.ncb_neg : enc->tb_params.ncb_pos;
1011 e = (r < enc->tb_params.cab) ?
1012 enc->tb_params.ea : enc->tb_params.eb;
1014 k = enc->cb_params.k;
1015 ncb = enc->cb_params.ncb;
1016 e = enc->cb_params.e;
1019 process_enc_cb(q, op, r, c, k, ncb, e, m_in, m_out_head,
1020 m_out, in_offset, out_offset, seg_total_left,
1022 /* Update total_left */
1023 in_length = ((k - crc24_bits) >> 3);
1024 mbuf_total_left -= in_length;
1025 /* Update offsets for next CBs (if exist) */
1026 in_offset += (k - crc24_bits) >> 3;
1027 if (enc->op_flags & RTE_BBDEV_TURBO_RATE_MATCH)
1028 out_offset += e >> 3;
1030 out_offset += (k >> 3) * 3 + 2;
1032 /* Update offsets */
1033 if (seg_total_left == in_length) {
1034 /* Go to the next mbuf */
1036 m_out = m_out->next;
1043 /* check if all input data was processed */
1044 if (mbuf_total_left != 0) {
1045 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1047 "Mismatch between mbuf length and included CBs sizes");
1053 enqueue_ldpc_enc_one_op(struct turbo_sw_queue *q, struct rte_bbdev_enc_op *op,
1054 struct rte_bbdev_stats *queue_stats)
1056 uint8_t c, r, crc24_bits = 0;
1058 struct rte_bbdev_op_ldpc_enc *enc = &op->ldpc_enc;
1059 uint16_t in_offset = enc->input.offset;
1060 uint16_t out_offset = enc->output.offset;
1061 struct rte_mbuf *m_in = enc->input.data;
1062 struct rte_mbuf *m_out = enc->output.data;
1063 struct rte_mbuf *m_out_head = enc->output.data;
1064 uint32_t in_length, mbuf_total_left = enc->input.length;
1066 uint16_t seg_total_left;
1068 /* Clear op status */
1071 if (mbuf_total_left > RTE_BBDEV_TURBO_MAX_TB_SIZE >> 3) {
1072 rte_bbdev_log(ERR, "TB size (%u) is too big, max: %d",
1073 mbuf_total_left, RTE_BBDEV_TURBO_MAX_TB_SIZE);
1074 op->status = 1 << RTE_BBDEV_DATA_ERROR;
1078 if (m_in == NULL || m_out == NULL) {
1079 rte_bbdev_log(ERR, "Invalid mbuf pointer");
1080 op->status = 1 << RTE_BBDEV_DATA_ERROR;
1084 if ((enc->op_flags & RTE_BBDEV_TURBO_CRC_24B_ATTACH) ||
1085 (enc->op_flags & RTE_BBDEV_TURBO_CRC_24A_ATTACH))
1088 if (enc->code_block_mode == 0) { /* For Transport Block mode */
1089 c = enc->tb_params.c;
1090 r = enc->tb_params.r;
1091 } else { /* For Code Block mode */
1096 while (mbuf_total_left > 0 && r < c) {
1098 seg_total_left = rte_pktmbuf_data_len(m_in) - in_offset;
1100 if (enc->code_block_mode == 0) {
1101 e = (r < enc->tb_params.cab) ?
1102 enc->tb_params.ea : enc->tb_params.eb;
1104 e = enc->cb_params.e;
1107 process_ldpc_enc_cb(q, op, e, m_in, m_out_head,
1108 m_out, in_offset, out_offset, seg_total_left,
1110 /* Update total_left */
1111 in_length = (enc->basegraph == 1 ? 22 : 10) * enc->z_c;
1112 in_length = ((in_length - crc24_bits - enc->n_filler) >> 3);
1113 mbuf_total_left -= in_length;
1114 /* Update offsets for next CBs (if exist) */
1115 in_offset += in_length;
1116 out_offset += (e + 7) >> 3;
1118 /* Update offsets */
1119 if (seg_total_left == in_length) {
1120 /* Go to the next mbuf */
1122 m_out = m_out->next;
1129 /* check if all input data was processed */
1130 if (mbuf_total_left != 0) {
1131 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1133 "Mismatch between mbuf length and included CBs sizes %d",
1138 static inline uint16_t
1139 enqueue_enc_all_ops(struct turbo_sw_queue *q, struct rte_bbdev_enc_op **ops,
1140 uint16_t nb_ops, struct rte_bbdev_stats *queue_stats)
1143 #ifdef RTE_BBDEV_OFFLOAD_COST
1144 queue_stats->acc_offload_cycles = 0;
1147 for (i = 0; i < nb_ops; ++i)
1148 enqueue_enc_one_op(q, ops[i], queue_stats);
1150 return rte_ring_enqueue_burst(q->processed_pkts, (void **)ops, nb_ops,
1154 static inline uint16_t
1155 enqueue_ldpc_enc_all_ops(struct turbo_sw_queue *q,
1156 struct rte_bbdev_enc_op **ops,
1157 uint16_t nb_ops, struct rte_bbdev_stats *queue_stats)
1160 #ifdef RTE_BBDEV_OFFLOAD_COST
1161 queue_stats->acc_offload_cycles = 0;
1164 for (i = 0; i < nb_ops; ++i)
1165 enqueue_ldpc_enc_one_op(q, ops[i], queue_stats);
1167 return rte_ring_enqueue_burst(q->processed_pkts, (void **)ops, nb_ops,
1171 #ifdef RTE_BBDEV_SDK_AVX2
1173 move_padding_bytes(const uint8_t *in, uint8_t *out, uint16_t k,
1177 uint16_t kpi = ncb / 3;
1178 uint16_t nd = kpi - d;
1180 rte_memcpy(&out[nd], in, d);
1181 rte_memcpy(&out[nd + kpi + 64], &in[kpi], d);
1182 rte_memcpy(&out[(nd - 1) + 2 * (kpi + 64)], &in[2 * kpi], d);
1187 process_dec_cb(struct turbo_sw_queue *q, struct rte_bbdev_dec_op *op,
1188 uint8_t c, uint16_t k, uint16_t kw, struct rte_mbuf *m_in,
1189 struct rte_mbuf *m_out_head, struct rte_mbuf *m_out,
1190 uint16_t in_offset, uint16_t out_offset, bool check_crc_24b,
1191 uint16_t crc24_overlap, uint16_t in_length,
1192 struct rte_bbdev_stats *q_stats)
1194 #ifdef RTE_BBDEV_SDK_AVX2
1195 #ifdef RTE_LIBRTE_BBDEV_DEBUG
1198 RTE_SET_USED(in_length);
1202 uint8_t *in, *out, *adapter_input;
1203 int32_t ncb, ncb_without_null;
1204 struct bblib_turbo_adapter_ul_response adapter_resp;
1205 struct bblib_turbo_adapter_ul_request adapter_req;
1206 struct bblib_turbo_decoder_request turbo_req;
1207 struct bblib_turbo_decoder_response turbo_resp;
1208 struct rte_bbdev_op_turbo_dec *dec = &op->turbo_dec;
1209 #ifdef RTE_BBDEV_OFFLOAD_COST
1210 uint64_t start_time;
1212 RTE_SET_USED(q_stats);
1215 k_idx = compute_idx(k);
1217 #ifdef RTE_LIBRTE_BBDEV_DEBUG
1218 ret = is_dec_input_valid(k_idx, kw, in_length);
1220 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1225 in = rte_pktmbuf_mtod_offset(m_in, uint8_t *, in_offset);
1227 ncb_without_null = (k + 4) * 3;
1229 if (check_bit(dec->op_flags, RTE_BBDEV_TURBO_SUBBLOCK_DEINTERLEAVE)) {
1230 struct bblib_deinterleave_ul_request deint_req;
1231 struct bblib_deinterleave_ul_response deint_resp;
1233 deint_req.circ_buffer = BBLIB_FULL_CIRCULAR_BUFFER;
1234 deint_req.pharqbuffer = in;
1235 deint_req.ncb = ncb;
1236 deint_resp.pinteleavebuffer = q->deint_output;
1238 #ifdef RTE_BBDEV_OFFLOAD_COST
1239 start_time = rte_rdtsc_precise();
1241 /* Sub-block De-Interleaving */
1242 bblib_deinterleave_ul(&deint_req, &deint_resp);
1243 #ifdef RTE_BBDEV_OFFLOAD_COST
1244 q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
1247 move_padding_bytes(in, q->deint_output, k, ncb);
1249 adapter_input = q->deint_output;
1251 if (dec->op_flags & RTE_BBDEV_TURBO_POS_LLR_1_BIT_IN)
1252 adapter_req.isinverted = 1;
1253 else if (dec->op_flags & RTE_BBDEV_TURBO_NEG_LLR_1_BIT_IN)
1254 adapter_req.isinverted = 0;
1256 op->status |= 1 << RTE_BBDEV_DRV_ERROR;
1257 rte_bbdev_log(ERR, "LLR format wasn't specified");
1261 adapter_req.ncb = ncb_without_null;
1262 adapter_req.pinteleavebuffer = adapter_input;
1263 adapter_resp.pharqout = q->adapter_output;
1265 #ifdef RTE_BBDEV_OFFLOAD_COST
1266 start_time = rte_rdtsc_precise();
1268 /* Turbo decode adaptation */
1269 bblib_turbo_adapter_ul(&adapter_req, &adapter_resp);
1270 #ifdef RTE_BBDEV_OFFLOAD_COST
1271 q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
1274 out = (uint8_t *)mbuf_append(m_out_head, m_out,
1275 ((k - crc24_overlap) >> 3));
1277 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1278 rte_bbdev_log(ERR, "Too little space in output mbuf");
1281 /* rte_bbdev_op_data.offset can be different than the offset of the
1284 out = rte_pktmbuf_mtod_offset(m_out, uint8_t *, out_offset);
1286 turbo_req.c = c + 1;
1289 turbo_req.input = (int8_t *)q->adapter_output;
1291 turbo_req.k_idx = k_idx;
1292 turbo_req.max_iter_num = dec->iter_max;
1293 turbo_req.early_term_disable = !check_bit(dec->op_flags,
1294 RTE_BBDEV_TURBO_EARLY_TERMINATION);
1295 turbo_resp.ag_buf = q->ag;
1296 turbo_resp.cb_buf = q->code_block;
1297 turbo_resp.output = out;
1299 #ifdef RTE_BBDEV_OFFLOAD_COST
1300 start_time = rte_rdtsc_precise();
1303 iter_cnt = bblib_turbo_decoder(&turbo_req, &turbo_resp);
1304 #ifdef RTE_BBDEV_OFFLOAD_COST
1305 q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
1307 dec->hard_output.length += (k >> 3);
1310 /* Temporary solution for returned iter_count from SDK */
1311 iter_cnt = (iter_cnt - 1) >> 1;
1312 dec->iter_count = RTE_MAX(iter_cnt, dec->iter_count);
1314 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1315 rte_bbdev_log(ERR, "Turbo Decoder failed");
1325 RTE_SET_USED(m_out_head);
1326 RTE_SET_USED(m_out);
1327 RTE_SET_USED(in_offset);
1328 RTE_SET_USED(out_offset);
1329 RTE_SET_USED(check_crc_24b);
1330 RTE_SET_USED(crc24_overlap);
1331 RTE_SET_USED(in_length);
1332 RTE_SET_USED(q_stats);
1337 process_ldpc_dec_cb(struct turbo_sw_queue *q, struct rte_bbdev_dec_op *op,
1338 uint8_t c, uint16_t out_length, uint16_t e,
1339 struct rte_mbuf *m_in,
1340 struct rte_mbuf *m_out_head, struct rte_mbuf *m_out,
1341 struct rte_mbuf *m_harq_in,
1342 struct rte_mbuf *m_harq_out_head, struct rte_mbuf *m_harq_out,
1343 uint16_t in_offset, uint16_t out_offset,
1344 uint16_t harq_in_offset, uint16_t harq_out_offset,
1346 uint16_t crc24_overlap, uint16_t in_length,
1347 struct rte_bbdev_stats *q_stats)
1349 #ifdef RTE_BBDEV_SDK_AVX512
1350 RTE_SET_USED(in_length);
1352 uint8_t *in, *out, *harq_in, *harq_out, *adapter_input;
1353 struct bblib_rate_dematching_5gnr_request derm_req;
1354 struct bblib_rate_dematching_5gnr_response derm_resp;
1355 struct bblib_ldpc_decoder_5gnr_request dec_req;
1356 struct bblib_ldpc_decoder_5gnr_response dec_resp;
1357 struct bblib_crc_request crc_req;
1358 struct bblib_crc_response crc_resp;
1359 struct rte_bbdev_op_ldpc_dec *dec = &op->ldpc_dec;
1360 uint16_t K, parity_offset, sys_cols, outLenWithCrc;
1361 int16_t deRmOutSize, numRows;
1363 /* Compute some LDPC BG lengths */
1364 outLenWithCrc = out_length + (crc24_overlap >> 3);
1365 sys_cols = (dec->basegraph == 1) ? 22 : 10;
1366 K = sys_cols * dec->z_c;
1367 parity_offset = K - 2 * dec->z_c;
1369 #ifdef RTE_BBDEV_OFFLOAD_COST
1370 uint64_t start_time = rte_rdtsc_precise();
1372 RTE_SET_USED(q_stats);
1375 in = rte_pktmbuf_mtod_offset(m_in, uint8_t *, in_offset);
1377 if (check_bit(dec->op_flags, RTE_BBDEV_LDPC_HQ_COMBINE_IN_ENABLE)) {
1379 * Single contiguous block from the first LLR of the
1383 if (m_harq_in != NULL)
1384 harq_in = rte_pktmbuf_mtod_offset(m_harq_in,
1385 uint8_t *, harq_in_offset);
1386 if (harq_in == NULL) {
1387 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1388 rte_bbdev_log(ERR, "No space in harq input mbuf");
1391 uint16_t harq_in_length = RTE_MIN(
1392 dec->harq_combined_input.length,
1393 (uint32_t) dec->n_cb);
1394 memset(q->ag + harq_in_length, 0,
1395 dec->n_cb - harq_in_length);
1396 rte_memcpy(q->ag, harq_in, harq_in_length);
1399 derm_req.p_in = (int8_t *) in;
1400 derm_req.p_harq = q->ag; /* This doesn't include the filler bits */
1401 derm_req.base_graph = dec->basegraph;
1402 derm_req.zc = dec->z_c;
1403 derm_req.ncb = dec->n_cb;
1405 derm_req.k0 = 0; /* Actual output from SDK */
1406 derm_req.isretx = check_bit(dec->op_flags,
1407 RTE_BBDEV_LDPC_HQ_COMBINE_IN_ENABLE);
1408 derm_req.rvid = dec->rv_index;
1409 derm_req.modulation_order = dec->q_m;
1410 derm_req.start_null_index = parity_offset - dec->n_filler;
1411 derm_req.num_of_null = dec->n_filler;
1413 bblib_rate_dematching_5gnr(&derm_req, &derm_resp);
1415 /* Compute RM out size and number of rows */
1416 deRmOutSize = RTE_MIN(
1417 derm_req.k0 + derm_req.e -
1418 ((derm_req.k0 < derm_req.start_null_index) ?
1420 dec->n_cb - dec->n_filler);
1421 if (m_harq_in != NULL)
1422 deRmOutSize = RTE_MAX(deRmOutSize,
1423 RTE_MIN(dec->n_cb - dec->n_filler,
1424 m_harq_in->data_len));
1425 numRows = ((deRmOutSize + dec->n_filler + dec->z_c - 1) / dec->z_c)
1427 numRows = RTE_MAX(4, numRows);
1429 /* get output data starting address */
1430 out = (uint8_t *)mbuf_append(m_out_head, m_out, out_length);
1432 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1434 "Too little space in LDPC decoder output mbuf");
1438 /* rte_bbdev_op_data.offset can be different than the offset
1439 * of the appended bytes
1441 out = rte_pktmbuf_mtod_offset(m_out, uint8_t *, out_offset);
1442 adapter_input = q->enc_out;
1444 dec_req.Zc = dec->z_c;
1445 dec_req.baseGraph = dec->basegraph;
1446 dec_req.nRows = numRows;
1447 dec_req.numChannelLlrs = deRmOutSize;
1448 dec_req.varNodes = derm_req.p_harq;
1449 dec_req.numFillerBits = dec->n_filler;
1450 dec_req.maxIterations = dec->iter_max;
1451 dec_req.enableEarlyTermination = check_bit(dec->op_flags,
1452 RTE_BBDEV_LDPC_ITERATION_STOP_ENABLE);
1453 dec_resp.varNodes = (int16_t *) q->adapter_output;
1454 dec_resp.compactedMessageBytes = q->enc_out;
1456 bblib_ldpc_decoder_5gnr(&dec_req, &dec_resp);
1458 dec->iter_count = RTE_MAX(dec_resp.iterationAtTermination,
1460 if (!dec_resp.parityPassedAtTermination)
1461 op->status |= 1 << RTE_BBDEV_SYNDROME_ERROR;
1463 bblib_bit_reverse((int8_t *) q->enc_out, outLenWithCrc << 3);
1465 if (check_bit(dec->op_flags, RTE_BBDEV_LDPC_CRC_TYPE_24A_CHECK) ||
1466 check_bit(dec->op_flags,
1467 RTE_BBDEV_LDPC_CRC_TYPE_24B_CHECK)) {
1468 crc_req.data = adapter_input;
1469 crc_req.len = K - dec->n_filler - 24;
1470 crc_resp.check_passed = false;
1471 crc_resp.data = adapter_input;
1473 bblib_lte_crc24b_check(&crc_req, &crc_resp);
1475 bblib_lte_crc24a_check(&crc_req, &crc_resp);
1476 if (!crc_resp.check_passed)
1477 op->status |= 1 << RTE_BBDEV_CRC_ERROR;
1480 #ifdef RTE_BBDEV_OFFLOAD_COST
1481 q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
1483 if (check_bit(dec->op_flags, RTE_BBDEV_LDPC_HQ_COMBINE_OUT_ENABLE)) {
1485 if (m_harq_out != NULL) {
1486 /* Initialize HARQ data length since we overwrite */
1487 m_harq_out->data_len = 0;
1488 /* Check there is enough space
1489 * in the HARQ outbound buffer
1491 harq_out = (uint8_t *)mbuf_append(m_harq_out_head,
1492 m_harq_out, deRmOutSize);
1494 if (harq_out == NULL) {
1495 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1496 rte_bbdev_log(ERR, "No space in HARQ output mbuf");
1499 /* get output data starting address and overwrite the data */
1500 harq_out = rte_pktmbuf_mtod_offset(m_harq_out, uint8_t *,
1502 rte_memcpy(harq_out, derm_req.p_harq, deRmOutSize);
1503 dec->harq_combined_output.length += deRmOutSize;
1506 rte_memcpy(out, adapter_input, out_length);
1507 dec->hard_output.length += out_length;
1512 RTE_SET_USED(out_length);
1515 RTE_SET_USED(m_out_head);
1516 RTE_SET_USED(m_out);
1517 RTE_SET_USED(m_harq_in);
1518 RTE_SET_USED(m_harq_out_head);
1519 RTE_SET_USED(m_harq_out);
1520 RTE_SET_USED(harq_in_offset);
1521 RTE_SET_USED(harq_out_offset);
1522 RTE_SET_USED(in_offset);
1523 RTE_SET_USED(out_offset);
1524 RTE_SET_USED(check_crc_24b);
1525 RTE_SET_USED(crc24_overlap);
1526 RTE_SET_USED(in_length);
1527 RTE_SET_USED(q_stats);
1533 enqueue_dec_one_op(struct turbo_sw_queue *q, struct rte_bbdev_dec_op *op,
1534 struct rte_bbdev_stats *queue_stats)
1538 uint16_t crc24_overlap = 0;
1539 struct rte_bbdev_op_turbo_dec *dec = &op->turbo_dec;
1540 struct rte_mbuf *m_in = dec->input.data;
1541 struct rte_mbuf *m_out = dec->hard_output.data;
1542 struct rte_mbuf *m_out_head = dec->hard_output.data;
1543 uint16_t in_offset = dec->input.offset;
1544 uint16_t out_offset = dec->hard_output.offset;
1545 uint32_t mbuf_total_left = dec->input.length;
1546 uint16_t seg_total_left;
1548 /* Clear op status */
1551 if (m_in == NULL || m_out == NULL) {
1552 rte_bbdev_log(ERR, "Invalid mbuf pointer");
1553 op->status = 1 << RTE_BBDEV_DATA_ERROR;
1557 if (dec->code_block_mode == 0) { /* For Transport Block mode */
1558 c = dec->tb_params.c;
1559 } else { /* For Code Block mode */
1560 k = dec->cb_params.k;
1564 if ((c > 1) && !check_bit(dec->op_flags,
1565 RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP))
1568 while (mbuf_total_left > 0) {
1569 if (dec->code_block_mode == 0)
1570 k = (r < dec->tb_params.c_neg) ?
1571 dec->tb_params.k_neg : dec->tb_params.k_pos;
1573 seg_total_left = rte_pktmbuf_data_len(m_in) - in_offset;
1575 /* Calculates circular buffer size (Kw).
1576 * According to 3gpp 36.212 section 5.1.4.2
1580 * where nCol is 32 and nRow can be calculated from:
1582 * where D is the size of each output from turbo encoder block
1585 kw = RTE_ALIGN_CEIL(k + 4, RTE_BBDEV_TURBO_C_SUBBLOCK) * 3;
1587 process_dec_cb(q, op, c, k, kw, m_in, m_out_head, m_out,
1588 in_offset, out_offset, check_bit(dec->op_flags,
1589 RTE_BBDEV_TURBO_CRC_TYPE_24B), crc24_overlap,
1590 seg_total_left, queue_stats);
1592 /* To keep CRC24 attached to end of Code block, use
1593 * RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP flag as it
1594 * removed by default once verified.
1597 mbuf_total_left -= kw;
1599 /* Update offsets */
1600 if (seg_total_left == kw) {
1601 /* Go to the next mbuf */
1603 m_out = m_out->next;
1607 /* Update offsets for next CBs (if exist) */
1609 out_offset += ((k - crc24_overlap) >> 3);
1614 if (mbuf_total_left != 0) {
1615 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1617 "Mismatch between mbuf length and included Circular buffer sizes");
1622 enqueue_ldpc_dec_one_op(struct turbo_sw_queue *q, struct rte_bbdev_dec_op *op,
1623 struct rte_bbdev_stats *queue_stats)
1626 uint16_t e, out_length;
1627 uint16_t crc24_overlap = 0;
1628 struct rte_bbdev_op_ldpc_dec *dec = &op->ldpc_dec;
1629 struct rte_mbuf *m_in = dec->input.data;
1630 struct rte_mbuf *m_harq_in = dec->harq_combined_input.data;
1631 struct rte_mbuf *m_harq_out = dec->harq_combined_output.data;
1632 struct rte_mbuf *m_harq_out_head = dec->harq_combined_output.data;
1633 struct rte_mbuf *m_out = dec->hard_output.data;
1634 struct rte_mbuf *m_out_head = dec->hard_output.data;
1635 uint16_t in_offset = dec->input.offset;
1636 uint16_t harq_in_offset = dec->harq_combined_input.offset;
1637 uint16_t harq_out_offset = dec->harq_combined_output.offset;
1638 uint16_t out_offset = dec->hard_output.offset;
1639 uint32_t mbuf_total_left = dec->input.length;
1640 uint16_t seg_total_left;
1642 /* Clear op status */
1645 if (m_in == NULL || m_out == NULL) {
1646 rte_bbdev_log(ERR, "Invalid mbuf pointer");
1647 op->status = 1 << RTE_BBDEV_DATA_ERROR;
1651 if (dec->code_block_mode == 0) { /* For Transport Block mode */
1652 c = dec->tb_params.c;
1653 e = dec->tb_params.ea;
1654 } else { /* For Code Block mode */
1656 e = dec->cb_params.e;
1659 if (check_bit(dec->op_flags, RTE_BBDEV_LDPC_CRC_TYPE_24B_DROP))
1662 out_length = (dec->basegraph == 1 ? 22 : 10) * dec->z_c; /* K */
1663 out_length = ((out_length - crc24_overlap - dec->n_filler) >> 3);
1665 while (mbuf_total_left > 0) {
1666 if (dec->code_block_mode == 0)
1667 e = (r < dec->tb_params.cab) ?
1668 dec->tb_params.ea : dec->tb_params.eb;
1670 seg_total_left = rte_pktmbuf_data_len(m_in) - in_offset;
1672 process_ldpc_dec_cb(q, op, c, out_length, e,
1673 m_in, m_out_head, m_out,
1674 m_harq_in, m_harq_out_head, m_harq_out,
1675 in_offset, out_offset, harq_in_offset,
1677 check_bit(dec->op_flags,
1678 RTE_BBDEV_LDPC_CRC_TYPE_24B_CHECK),
1680 seg_total_left, queue_stats);
1682 /* To keep CRC24 attached to end of Code block, use
1683 * RTE_BBDEV_LDPC_DEC_TB_CRC_24B_KEEP flag as it
1684 * removed by default once verified.
1687 mbuf_total_left -= e;
1689 /* Update offsets */
1690 if (seg_total_left == e) {
1691 /* Go to the next mbuf */
1693 m_out = m_out->next;
1694 if (m_harq_in != NULL)
1695 m_harq_in = m_harq_in->next;
1696 if (m_harq_out != NULL)
1697 m_harq_out = m_harq_out->next;
1701 harq_out_offset = 0;
1703 /* Update offsets for next CBs (if exist) */
1705 out_offset += out_length;
1710 if (mbuf_total_left != 0) {
1711 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1713 "Mismatch between mbuf length and included Circular buffer sizes");
1717 static inline uint16_t
1718 enqueue_dec_all_ops(struct turbo_sw_queue *q, struct rte_bbdev_dec_op **ops,
1719 uint16_t nb_ops, struct rte_bbdev_stats *queue_stats)
1722 #ifdef RTE_BBDEV_OFFLOAD_COST
1723 queue_stats->acc_offload_cycles = 0;
1726 for (i = 0; i < nb_ops; ++i)
1727 enqueue_dec_one_op(q, ops[i], queue_stats);
1729 return rte_ring_enqueue_burst(q->processed_pkts, (void **)ops, nb_ops,
1733 static inline uint16_t
1734 enqueue_ldpc_dec_all_ops(struct turbo_sw_queue *q,
1735 struct rte_bbdev_dec_op **ops,
1736 uint16_t nb_ops, struct rte_bbdev_stats *queue_stats)
1739 #ifdef RTE_BBDEV_OFFLOAD_COST
1740 queue_stats->acc_offload_cycles = 0;
1743 for (i = 0; i < nb_ops; ++i)
1744 enqueue_ldpc_dec_one_op(q, ops[i], queue_stats);
1746 return rte_ring_enqueue_burst(q->processed_pkts, (void **)ops, nb_ops,
1752 enqueue_enc_ops(struct rte_bbdev_queue_data *q_data,
1753 struct rte_bbdev_enc_op **ops, uint16_t nb_ops)
1755 void *queue = q_data->queue_private;
1756 struct turbo_sw_queue *q = queue;
1757 uint16_t nb_enqueued = 0;
1759 nb_enqueued = enqueue_enc_all_ops(q, ops, nb_ops, &q_data->queue_stats);
1761 q_data->queue_stats.enqueue_err_count += nb_ops - nb_enqueued;
1762 q_data->queue_stats.enqueued_count += nb_enqueued;
1769 enqueue_ldpc_enc_ops(struct rte_bbdev_queue_data *q_data,
1770 struct rte_bbdev_enc_op **ops, uint16_t nb_ops)
1772 void *queue = q_data->queue_private;
1773 struct turbo_sw_queue *q = queue;
1774 uint16_t nb_enqueued = 0;
1776 nb_enqueued = enqueue_ldpc_enc_all_ops(
1777 q, ops, nb_ops, &q_data->queue_stats);
1779 q_data->queue_stats.enqueue_err_count += nb_ops - nb_enqueued;
1780 q_data->queue_stats.enqueued_count += nb_enqueued;
1787 enqueue_dec_ops(struct rte_bbdev_queue_data *q_data,
1788 struct rte_bbdev_dec_op **ops, uint16_t nb_ops)
1790 void *queue = q_data->queue_private;
1791 struct turbo_sw_queue *q = queue;
1792 uint16_t nb_enqueued = 0;
1794 nb_enqueued = enqueue_dec_all_ops(q, ops, nb_ops, &q_data->queue_stats);
1796 q_data->queue_stats.enqueue_err_count += nb_ops - nb_enqueued;
1797 q_data->queue_stats.enqueued_count += nb_enqueued;
1804 enqueue_ldpc_dec_ops(struct rte_bbdev_queue_data *q_data,
1805 struct rte_bbdev_dec_op **ops, uint16_t nb_ops)
1807 void *queue = q_data->queue_private;
1808 struct turbo_sw_queue *q = queue;
1809 uint16_t nb_enqueued = 0;
1811 nb_enqueued = enqueue_ldpc_dec_all_ops(q, ops, nb_ops,
1812 &q_data->queue_stats);
1814 q_data->queue_stats.enqueue_err_count += nb_ops - nb_enqueued;
1815 q_data->queue_stats.enqueued_count += nb_enqueued;
1820 /* Dequeue decode burst */
1822 dequeue_dec_ops(struct rte_bbdev_queue_data *q_data,
1823 struct rte_bbdev_dec_op **ops, uint16_t nb_ops)
1825 struct turbo_sw_queue *q = q_data->queue_private;
1826 uint16_t nb_dequeued = rte_ring_dequeue_burst(q->processed_pkts,
1827 (void **)ops, nb_ops, NULL);
1828 q_data->queue_stats.dequeued_count += nb_dequeued;
1833 /* Dequeue encode burst */
1835 dequeue_enc_ops(struct rte_bbdev_queue_data *q_data,
1836 struct rte_bbdev_enc_op **ops, uint16_t nb_ops)
1838 struct turbo_sw_queue *q = q_data->queue_private;
1839 uint16_t nb_dequeued = rte_ring_dequeue_burst(q->processed_pkts,
1840 (void **)ops, nb_ops, NULL);
1841 q_data->queue_stats.dequeued_count += nb_dequeued;
1846 /* Parse 16bit integer from string argument */
1848 parse_u16_arg(const char *key, const char *value, void *extra_args)
1850 uint16_t *u16 = extra_args;
1851 unsigned int long result;
1853 if ((value == NULL) || (extra_args == NULL))
1856 result = strtoul(value, NULL, 0);
1857 if ((result >= (1 << 16)) || (errno != 0)) {
1858 rte_bbdev_log(ERR, "Invalid value %lu for %s", result, key);
1861 *u16 = (uint16_t)result;
1865 /* Parse parameters used to create device */
1867 parse_turbo_sw_params(struct turbo_sw_params *params, const char *input_args)
1869 struct rte_kvargs *kvlist = NULL;
1875 kvlist = rte_kvargs_parse(input_args, turbo_sw_valid_params);
1879 ret = rte_kvargs_process(kvlist, turbo_sw_valid_params[0],
1880 &parse_u16_arg, ¶ms->queues_num);
1884 ret = rte_kvargs_process(kvlist, turbo_sw_valid_params[1],
1885 &parse_u16_arg, ¶ms->socket_id);
1889 if (params->socket_id >= RTE_MAX_NUMA_NODES) {
1890 rte_bbdev_log(ERR, "Invalid socket, must be < %u",
1891 RTE_MAX_NUMA_NODES);
1898 rte_kvargs_free(kvlist);
1904 turbo_sw_bbdev_create(struct rte_vdev_device *vdev,
1905 struct turbo_sw_params *init_params)
1907 struct rte_bbdev *bbdev;
1908 const char *name = rte_vdev_device_name(vdev);
1910 bbdev = rte_bbdev_allocate(name);
1914 bbdev->data->dev_private = rte_zmalloc_socket(name,
1915 sizeof(struct bbdev_private), RTE_CACHE_LINE_SIZE,
1916 init_params->socket_id);
1917 if (bbdev->data->dev_private == NULL) {
1918 rte_bbdev_release(bbdev);
1922 bbdev->dev_ops = &pmd_ops;
1923 bbdev->device = &vdev->device;
1924 bbdev->data->socket_id = init_params->socket_id;
1925 bbdev->intr_handle = NULL;
1927 /* register rx/tx burst functions for data path */
1928 bbdev->dequeue_enc_ops = dequeue_enc_ops;
1929 bbdev->dequeue_dec_ops = dequeue_dec_ops;
1930 bbdev->enqueue_enc_ops = enqueue_enc_ops;
1931 bbdev->enqueue_dec_ops = enqueue_dec_ops;
1932 bbdev->dequeue_ldpc_enc_ops = dequeue_enc_ops;
1933 bbdev->dequeue_ldpc_dec_ops = dequeue_dec_ops;
1934 bbdev->enqueue_ldpc_enc_ops = enqueue_ldpc_enc_ops;
1935 bbdev->enqueue_ldpc_dec_ops = enqueue_ldpc_dec_ops;
1936 ((struct bbdev_private *) bbdev->data->dev_private)->max_nb_queues =
1937 init_params->queues_num;
1942 /* Initialise device */
1944 turbo_sw_bbdev_probe(struct rte_vdev_device *vdev)
1946 struct turbo_sw_params init_params = {
1948 RTE_BBDEV_DEFAULT_MAX_NB_QUEUES
1951 const char *input_args;
1956 name = rte_vdev_device_name(vdev);
1959 input_args = rte_vdev_device_args(vdev);
1960 parse_turbo_sw_params(&init_params, input_args);
1962 rte_bbdev_log_debug(
1963 "Initialising %s on NUMA node %d with max queues: %d\n",
1964 name, init_params.socket_id, init_params.queues_num);
1966 return turbo_sw_bbdev_create(vdev, &init_params);
1969 /* Uninitialise device */
1971 turbo_sw_bbdev_remove(struct rte_vdev_device *vdev)
1973 struct rte_bbdev *bbdev;
1979 name = rte_vdev_device_name(vdev);
1983 bbdev = rte_bbdev_get_named_dev(name);
1987 rte_free(bbdev->data->dev_private);
1989 return rte_bbdev_release(bbdev);
1992 static struct rte_vdev_driver bbdev_turbo_sw_pmd_drv = {
1993 .probe = turbo_sw_bbdev_probe,
1994 .remove = turbo_sw_bbdev_remove
1997 RTE_PMD_REGISTER_VDEV(DRIVER_NAME, bbdev_turbo_sw_pmd_drv);
1998 RTE_PMD_REGISTER_PARAM_STRING(DRIVER_NAME,
1999 TURBO_SW_MAX_NB_QUEUES_ARG"=<int> "
2000 TURBO_SW_SOCKET_ID_ARG"=<int>");
2001 RTE_PMD_REGISTER_ALIAS(DRIVER_NAME, turbo_sw);
2003 RTE_INIT(turbo_sw_bbdev_init_log)
2005 bbdev_turbo_sw_logtype = rte_log_register("pmd.bb.turbo_sw");
2006 if (bbdev_turbo_sw_logtype >= 0)
2007 rte_log_set_level(bbdev_turbo_sw_logtype, RTE_LOG_NOTICE);