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 RTE_LOG_REGISTER(bbdev_turbo_sw_logtype, pmd.bb.turbo_sw, NOTICE);
39 /* Helper macro for logging */
40 #define rte_bbdev_log(level, fmt, ...) \
41 rte_log(RTE_LOG_ ## level, bbdev_turbo_sw_logtype, fmt "\n", \
44 #define rte_bbdev_log_debug(fmt, ...) \
45 rte_bbdev_log(DEBUG, RTE_STR(__LINE__) ":%s() " fmt, __func__, \
48 #define DEINT_INPUT_BUF_SIZE (((RTE_BBDEV_TURBO_MAX_CB_SIZE >> 3) + 1) * 48)
49 #define DEINT_OUTPUT_BUF_SIZE (DEINT_INPUT_BUF_SIZE * 6)
50 #define ADAPTER_OUTPUT_BUF_SIZE ((RTE_BBDEV_TURBO_MAX_CB_SIZE + 4) * 48)
52 /* private data structure */
53 struct bbdev_private {
54 unsigned int max_nb_queues; /**< Max number of queues */
57 /* Initialisation params structure that can be used by Turbo SW driver */
58 struct turbo_sw_params {
59 int socket_id; /*< Turbo SW device socket */
60 uint16_t queues_num; /*< Turbo SW device queues number */
63 /* Accecptable params for Turbo SW devices */
64 #define TURBO_SW_MAX_NB_QUEUES_ARG "max_nb_queues"
65 #define TURBO_SW_SOCKET_ID_ARG "socket_id"
67 static const char * const turbo_sw_valid_params[] = {
68 TURBO_SW_MAX_NB_QUEUES_ARG,
69 TURBO_SW_SOCKET_ID_ARG
73 struct turbo_sw_queue {
74 /* Ring for processed (encoded/decoded) operations which are ready to
77 struct rte_ring *processed_pkts;
78 /* Stores input for turbo encoder (used when CRC attachment is
82 /* Stores output from turbo encoder */
84 /* Alpha gamma buf for bblib_turbo_decoder() function */
86 /* Temp buf for bblib_turbo_decoder() function */
88 /* Input buf for bblib_rate_dematching_lte() function */
90 /* Output buf for bblib_rate_dematching_lte() function */
91 uint8_t *deint_output;
92 /* Output buf for bblib_turbodec_adapter_lte() function */
93 uint8_t *adapter_output;
94 /* Operation type of this queue */
95 enum rte_bbdev_op_type type;
96 } __rte_cache_aligned;
99 #ifdef RTE_BBDEV_SDK_AVX2
101 mbuf_append(struct rte_mbuf *m_head, struct rte_mbuf *m, uint16_t len)
103 if (unlikely(len > rte_pktmbuf_tailroom(m)))
106 char *tail = (char *)m->buf_addr + m->data_off + m->data_len;
107 m->data_len = (uint16_t)(m->data_len + len);
108 m_head->pkt_len = (m_head->pkt_len + len);
112 /* Calculate index based on Table 5.1.3-3 from TS34.212 */
113 static inline int32_t
114 compute_idx(uint16_t k)
118 if (k < RTE_BBDEV_TURBO_MIN_CB_SIZE || k > RTE_BBDEV_TURBO_MAX_CB_SIZE)
122 if ((k - 2048) % 64 != 0)
125 result = 124 + (k - 2048) / 64;
126 } else if (k <= 512) {
127 if ((k - 40) % 8 != 0)
130 result = (k - 40) / 8 + 1;
131 } else if (k <= 1024) {
132 if ((k - 512) % 16 != 0)
135 result = 60 + (k - 512) / 16;
136 } else { /* 1024 < k <= 2048 */
137 if ((k - 1024) % 32 != 0)
140 result = 92 + (k - 1024) / 32;
147 /* Read flag value 0/1 from bitmap */
149 check_bit(uint32_t bitmap, uint32_t bitmask)
151 return bitmap & bitmask;
154 /* Get device info */
156 info_get(struct rte_bbdev *dev, struct rte_bbdev_driver_info *dev_info)
158 struct bbdev_private *internals = dev->data->dev_private;
160 static const struct rte_bbdev_op_cap bbdev_capabilities[] = {
161 #ifdef RTE_BBDEV_SDK_AVX2
163 .type = RTE_BBDEV_OP_TURBO_DEC,
166 RTE_BBDEV_TURBO_SUBBLOCK_DEINTERLEAVE |
167 RTE_BBDEV_TURBO_POS_LLR_1_BIT_IN |
168 RTE_BBDEV_TURBO_NEG_LLR_1_BIT_IN |
169 RTE_BBDEV_TURBO_CRC_TYPE_24B |
170 RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP |
171 RTE_BBDEV_TURBO_EARLY_TERMINATION,
172 .max_llr_modulus = 16,
174 RTE_BBDEV_TURBO_MAX_CODE_BLOCKS,
175 .num_buffers_hard_out =
176 RTE_BBDEV_TURBO_MAX_CODE_BLOCKS,
177 .num_buffers_soft_out = 0,
181 .type = RTE_BBDEV_OP_TURBO_ENC,
184 RTE_BBDEV_TURBO_CRC_24B_ATTACH |
185 RTE_BBDEV_TURBO_CRC_24A_ATTACH |
186 RTE_BBDEV_TURBO_RATE_MATCH |
187 RTE_BBDEV_TURBO_RV_INDEX_BYPASS,
189 RTE_BBDEV_TURBO_MAX_CODE_BLOCKS,
191 RTE_BBDEV_TURBO_MAX_CODE_BLOCKS,
195 #ifdef RTE_BBDEV_SDK_AVX512
197 .type = RTE_BBDEV_OP_LDPC_ENC,
200 RTE_BBDEV_LDPC_RATE_MATCH |
201 RTE_BBDEV_LDPC_CRC_24A_ATTACH |
202 RTE_BBDEV_LDPC_CRC_24B_ATTACH,
204 RTE_BBDEV_LDPC_MAX_CODE_BLOCKS,
206 RTE_BBDEV_LDPC_MAX_CODE_BLOCKS,
210 .type = RTE_BBDEV_OP_LDPC_DEC,
213 RTE_BBDEV_LDPC_CRC_TYPE_24B_CHECK |
214 RTE_BBDEV_LDPC_CRC_TYPE_24A_CHECK |
215 RTE_BBDEV_LDPC_CRC_TYPE_24B_DROP |
216 RTE_BBDEV_LDPC_HQ_COMBINE_IN_ENABLE |
217 RTE_BBDEV_LDPC_HQ_COMBINE_OUT_ENABLE |
218 RTE_BBDEV_LDPC_ITERATION_STOP_ENABLE,
222 RTE_BBDEV_LDPC_MAX_CODE_BLOCKS,
223 .num_buffers_hard_out =
224 RTE_BBDEV_LDPC_MAX_CODE_BLOCKS,
225 .num_buffers_soft_out = 0,
229 RTE_BBDEV_END_OF_CAPABILITIES_LIST()
232 static struct rte_bbdev_queue_conf default_queue_conf = {
233 .queue_size = RTE_BBDEV_QUEUE_SIZE_LIMIT,
235 #ifdef RTE_BBDEV_SDK_AVX2
236 static const enum rte_cpu_flag_t cpu_flag = RTE_CPUFLAG_SSE4_2;
237 dev_info->cpu_flag_reqs = &cpu_flag;
239 dev_info->cpu_flag_reqs = NULL;
241 default_queue_conf.socket = dev->data->socket_id;
243 dev_info->driver_name = RTE_STR(DRIVER_NAME);
244 dev_info->max_num_queues = internals->max_nb_queues;
245 dev_info->queue_size_lim = RTE_BBDEV_QUEUE_SIZE_LIMIT;
246 dev_info->hardware_accelerated = false;
247 dev_info->max_dl_queue_priority = 0;
248 dev_info->max_ul_queue_priority = 0;
249 dev_info->default_queue_conf = default_queue_conf;
250 dev_info->capabilities = bbdev_capabilities;
251 dev_info->min_alignment = 64;
252 dev_info->harq_buffer_size = 0;
254 rte_bbdev_log_debug("got device info from %u\n", dev->data->dev_id);
259 q_release(struct rte_bbdev *dev, uint16_t q_id)
261 struct turbo_sw_queue *q = dev->data->queues[q_id].queue_private;
264 rte_ring_free(q->processed_pkts);
265 rte_free(q->enc_out);
268 rte_free(q->code_block);
269 rte_free(q->deint_input);
270 rte_free(q->deint_output);
271 rte_free(q->adapter_output);
273 dev->data->queues[q_id].queue_private = NULL;
276 rte_bbdev_log_debug("released device queue %u:%u",
277 dev->data->dev_id, q_id);
283 q_setup(struct rte_bbdev *dev, uint16_t q_id,
284 const struct rte_bbdev_queue_conf *queue_conf)
287 struct turbo_sw_queue *q;
288 char name[RTE_RING_NAMESIZE];
290 /* Allocate the queue data structure. */
291 q = rte_zmalloc_socket(RTE_STR(DRIVER_NAME), sizeof(*q),
292 RTE_CACHE_LINE_SIZE, queue_conf->socket);
294 rte_bbdev_log(ERR, "Failed to allocate queue memory");
298 /* Allocate memory for encoder output. */
299 ret = snprintf(name, RTE_RING_NAMESIZE, RTE_STR(DRIVER_NAME)"_enc_o%u:%u",
300 dev->data->dev_id, q_id);
301 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
303 "Creating queue name for device %u queue %u failed",
304 dev->data->dev_id, q_id);
305 return -ENAMETOOLONG;
307 q->enc_out = rte_zmalloc_socket(name,
308 ((RTE_BBDEV_TURBO_MAX_TB_SIZE >> 3) + 3) *
309 sizeof(*q->enc_out) * 3,
310 RTE_CACHE_LINE_SIZE, queue_conf->socket);
311 if (q->enc_out == NULL) {
313 "Failed to allocate queue memory for %s", name);
317 /* Allocate memory for rate matching output. */
318 ret = snprintf(name, RTE_RING_NAMESIZE,
319 RTE_STR(DRIVER_NAME)"_enc_i%u:%u", dev->data->dev_id,
321 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
323 "Creating queue name for device %u queue %u failed",
324 dev->data->dev_id, q_id);
325 return -ENAMETOOLONG;
327 q->enc_in = rte_zmalloc_socket(name,
328 (RTE_BBDEV_LDPC_MAX_CB_SIZE >> 3) * sizeof(*q->enc_in),
329 RTE_CACHE_LINE_SIZE, queue_conf->socket);
330 if (q->enc_in == NULL) {
332 "Failed to allocate queue memory for %s", name);
336 /* Allocate memory for Alpha Gamma temp buffer. */
337 ret = snprintf(name, RTE_RING_NAMESIZE, RTE_STR(DRIVER_NAME)"_ag%u:%u",
338 dev->data->dev_id, q_id);
339 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
341 "Creating queue name for device %u queue %u failed",
342 dev->data->dev_id, q_id);
343 return -ENAMETOOLONG;
345 q->ag = rte_zmalloc_socket(name,
346 RTE_BBDEV_TURBO_MAX_CB_SIZE * 10 * sizeof(*q->ag),
347 RTE_CACHE_LINE_SIZE, queue_conf->socket);
350 "Failed to allocate queue memory for %s", name);
354 /* Allocate memory for code block temp buffer. */
355 ret = snprintf(name, RTE_RING_NAMESIZE, RTE_STR(DRIVER_NAME)"_cb%u:%u",
356 dev->data->dev_id, q_id);
357 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
359 "Creating queue name for device %u queue %u failed",
360 dev->data->dev_id, q_id);
361 return -ENAMETOOLONG;
363 q->code_block = rte_zmalloc_socket(name,
364 RTE_BBDEV_TURBO_MAX_CB_SIZE * sizeof(*q->code_block),
365 RTE_CACHE_LINE_SIZE, queue_conf->socket);
366 if (q->code_block == NULL) {
368 "Failed to allocate queue memory for %s", name);
372 /* Allocate memory for Deinterleaver input. */
373 ret = snprintf(name, RTE_RING_NAMESIZE,
374 RTE_STR(DRIVER_NAME)"_de_i%u:%u",
375 dev->data->dev_id, q_id);
376 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
378 "Creating queue name for device %u queue %u failed",
379 dev->data->dev_id, q_id);
380 return -ENAMETOOLONG;
382 q->deint_input = rte_zmalloc_socket(name,
383 DEINT_INPUT_BUF_SIZE * sizeof(*q->deint_input),
384 RTE_CACHE_LINE_SIZE, queue_conf->socket);
385 if (q->deint_input == NULL) {
387 "Failed to allocate queue memory for %s", name);
391 /* Allocate memory for Deinterleaver output. */
392 ret = snprintf(name, RTE_RING_NAMESIZE,
393 RTE_STR(DRIVER_NAME)"_de_o%u:%u",
394 dev->data->dev_id, q_id);
395 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
397 "Creating queue name for device %u queue %u failed",
398 dev->data->dev_id, q_id);
399 return -ENAMETOOLONG;
401 q->deint_output = rte_zmalloc_socket(NULL,
402 DEINT_OUTPUT_BUF_SIZE * sizeof(*q->deint_output),
403 RTE_CACHE_LINE_SIZE, queue_conf->socket);
404 if (q->deint_output == NULL) {
406 "Failed to allocate queue memory for %s", name);
410 /* Allocate memory for Adapter output. */
411 ret = snprintf(name, RTE_RING_NAMESIZE,
412 RTE_STR(DRIVER_NAME)"_ada_o%u:%u",
413 dev->data->dev_id, q_id);
414 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
416 "Creating queue name for device %u queue %u failed",
417 dev->data->dev_id, q_id);
418 return -ENAMETOOLONG;
420 q->adapter_output = rte_zmalloc_socket(NULL,
421 ADAPTER_OUTPUT_BUF_SIZE * sizeof(*q->adapter_output),
422 RTE_CACHE_LINE_SIZE, queue_conf->socket);
423 if (q->adapter_output == NULL) {
425 "Failed to allocate queue memory for %s", name);
429 /* Create ring for packets awaiting to be dequeued. */
430 ret = snprintf(name, RTE_RING_NAMESIZE, RTE_STR(DRIVER_NAME)"%u:%u",
431 dev->data->dev_id, q_id);
432 if ((ret < 0) || (ret >= (int)RTE_RING_NAMESIZE)) {
434 "Creating queue name for device %u queue %u failed",
435 dev->data->dev_id, q_id);
436 return -ENAMETOOLONG;
438 q->processed_pkts = rte_ring_create(name, queue_conf->queue_size,
439 queue_conf->socket, RING_F_SP_ENQ | RING_F_SC_DEQ);
440 if (q->processed_pkts == NULL) {
441 rte_bbdev_log(ERR, "Failed to create ring for %s", name);
445 q->type = queue_conf->op_type;
447 dev->data->queues[q_id].queue_private = q;
448 rte_bbdev_log_debug("setup device queue %s", name);
452 rte_ring_free(q->processed_pkts);
453 rte_free(q->enc_out);
456 rte_free(q->code_block);
457 rte_free(q->deint_input);
458 rte_free(q->deint_output);
459 rte_free(q->adapter_output);
464 static const struct rte_bbdev_ops pmd_ops = {
465 .info_get = info_get,
466 .queue_setup = q_setup,
467 .queue_release = q_release
470 #ifdef RTE_BBDEV_SDK_AVX2
471 #ifdef RTE_LIBRTE_BBDEV_DEBUG
472 /* Checks if the encoder input buffer is correct.
473 * Returns 0 if it's valid, -1 otherwise.
476 is_enc_input_valid(const uint16_t k, const int32_t k_idx,
477 const uint16_t in_length)
480 rte_bbdev_log(ERR, "K Index is invalid");
484 if (in_length - (k >> 3) < 0) {
486 "Mismatch between input length (%u bytes) and K (%u bits)",
491 if (k > RTE_BBDEV_TURBO_MAX_CB_SIZE) {
492 rte_bbdev_log(ERR, "CB size (%u) is too big, max: %d",
493 k, RTE_BBDEV_TURBO_MAX_CB_SIZE);
500 /* Checks if the decoder input buffer is correct.
501 * Returns 0 if it's valid, -1 otherwise.
504 is_dec_input_valid(int32_t k_idx, int16_t kw, int16_t in_length)
507 rte_bbdev_log(ERR, "K index is invalid");
511 if (in_length < kw) {
513 "Mismatch between input length (%u) and kw (%u)",
518 if (kw > RTE_BBDEV_TURBO_MAX_KW) {
519 rte_bbdev_log(ERR, "Input length (%u) is too big, max: %d",
520 kw, RTE_BBDEV_TURBO_MAX_KW);
530 process_enc_cb(struct turbo_sw_queue *q, struct rte_bbdev_enc_op *op,
531 uint8_t r, uint8_t c, uint16_t k, uint16_t ncb,
532 uint32_t e, struct rte_mbuf *m_in, struct rte_mbuf *m_out_head,
533 struct rte_mbuf *m_out, uint16_t in_offset, uint16_t out_offset,
534 uint16_t in_length, struct rte_bbdev_stats *q_stats)
536 #ifdef RTE_BBDEV_SDK_AVX2
537 #ifdef RTE_LIBRTE_BBDEV_DEBUG
540 RTE_SET_USED(in_length);
544 uint8_t *in, *out0, *out1, *out2, *tmp_out, *rm_out;
545 uint64_t first_3_bytes = 0;
546 struct rte_bbdev_op_turbo_enc *enc = &op->turbo_enc;
547 struct bblib_crc_request crc_req;
548 struct bblib_crc_response crc_resp;
549 struct bblib_turbo_encoder_request turbo_req;
550 struct bblib_turbo_encoder_response turbo_resp;
551 struct bblib_rate_match_dl_request rm_req;
552 struct bblib_rate_match_dl_response rm_resp;
553 #ifdef RTE_BBDEV_OFFLOAD_COST
556 RTE_SET_USED(q_stats);
559 k_idx = compute_idx(k);
560 in = rte_pktmbuf_mtod_offset(m_in, uint8_t *, in_offset);
562 /* CRC24A (for TB) */
563 if ((enc->op_flags & RTE_BBDEV_TURBO_CRC_24A_ATTACH) &&
564 (enc->code_block_mode == 1)) {
565 #ifdef RTE_LIBRTE_BBDEV_DEBUG
566 ret = is_enc_input_valid(k - 24, k_idx, in_length);
568 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
574 crc_req.len = k - 24;
575 /* Check if there is a room for CRC bits if not use
576 * the temporary buffer.
578 if (mbuf_append(m_in, m_in, 3) == NULL) {
579 rte_memcpy(q->enc_in, in, (k - 24) >> 3);
582 /* Store 3 first bytes of next CB as they will be
583 * overwritten by CRC bytes. If it is the last CB then
584 * there is no point to store 3 next bytes and this
585 * if..else branch will be omitted.
587 first_3_bytes = *((uint64_t *)&in[(k - 32) >> 3]);
591 #ifdef RTE_BBDEV_OFFLOAD_COST
592 start_time = rte_rdtsc_precise();
594 /* CRC24A generation */
595 bblib_lte_crc24a_gen(&crc_req, &crc_resp);
596 #ifdef RTE_BBDEV_OFFLOAD_COST
597 q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
599 } else if (enc->op_flags & RTE_BBDEV_TURBO_CRC_24B_ATTACH) {
601 #ifdef RTE_LIBRTE_BBDEV_DEBUG
602 ret = is_enc_input_valid(k - 24, k_idx, in_length);
604 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
610 crc_req.len = k - 24;
611 /* Check if there is a room for CRC bits if this is the last
612 * CB in TB. If not use temporary buffer.
614 if ((c - r == 1) && (mbuf_append(m_in, m_in, 3) == NULL)) {
615 rte_memcpy(q->enc_in, in, (k - 24) >> 3);
617 } else if (c - r > 1) {
618 /* Store 3 first bytes of next CB as they will be
619 * overwritten by CRC bytes. If it is the last CB then
620 * there is no point to store 3 next bytes and this
621 * if..else branch will be omitted.
623 first_3_bytes = *((uint64_t *)&in[(k - 32) >> 3]);
627 #ifdef RTE_BBDEV_OFFLOAD_COST
628 start_time = rte_rdtsc_precise();
630 /* CRC24B generation */
631 bblib_lte_crc24b_gen(&crc_req, &crc_resp);
632 #ifdef RTE_BBDEV_OFFLOAD_COST
633 q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
636 #ifdef RTE_LIBRTE_BBDEV_DEBUG
638 ret = is_enc_input_valid(k, k_idx, in_length);
640 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
648 /* Each bit layer output from turbo encoder is (k+4) bits long, i.e.
649 * input length + 4 tail bits. That's (k/8) + 1 bytes after rounding up.
650 * So dst_data's length should be 3*(k/8) + 3 bytes.
651 * In Rate-matching bypass case outputs pointers passed to encoder
652 * (out0, out1 and out2) can directly point to addresses of output from
655 if (enc->op_flags & RTE_BBDEV_TURBO_RATE_MATCH) {
657 out1 = RTE_PTR_ADD(out0, (k >> 3) + 1);
658 out2 = RTE_PTR_ADD(out1, (k >> 3) + 1);
660 out0 = (uint8_t *)mbuf_append(m_out_head, m_out,
663 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
665 "Too little space in output mbuf");
668 enc->output.length += (k >> 3) * 3 + 2;
669 /* rte_bbdev_op_data.offset can be different than the
670 * offset of the appended bytes
672 out0 = rte_pktmbuf_mtod_offset(m_out, uint8_t *, out_offset);
673 out1 = rte_pktmbuf_mtod_offset(m_out, uint8_t *,
674 out_offset + (k >> 3) + 1);
675 out2 = rte_pktmbuf_mtod_offset(m_out, uint8_t *,
676 out_offset + 2 * ((k >> 3) + 1));
679 turbo_req.case_id = k_idx;
680 turbo_req.input_win = in;
681 turbo_req.length = k >> 3;
682 turbo_resp.output_win_0 = out0;
683 turbo_resp.output_win_1 = out1;
684 turbo_resp.output_win_2 = out2;
686 #ifdef RTE_BBDEV_OFFLOAD_COST
687 start_time = rte_rdtsc_precise();
690 if (bblib_turbo_encoder(&turbo_req, &turbo_resp) != 0) {
691 op->status |= 1 << RTE_BBDEV_DRV_ERROR;
692 rte_bbdev_log(ERR, "Turbo Encoder failed");
695 #ifdef RTE_BBDEV_OFFLOAD_COST
696 q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
699 /* Restore 3 first bytes of next CB if they were overwritten by CRC*/
700 if (first_3_bytes != 0)
701 *((uint64_t *)&in[(k - 32) >> 3]) = first_3_bytes;
704 if (enc->op_flags & RTE_BBDEV_TURBO_RATE_MATCH) {
706 /* Integer round up division by 8 */
707 uint16_t out_len = (e + 7) >> 3;
708 /* The mask array is indexed using E%8. E is an even number so
709 * there are only 4 possible values.
711 const uint8_t mask_out[] = {0xFF, 0xC0, 0xF0, 0xFC};
713 /* get output data starting address */
714 rm_out = (uint8_t *)mbuf_append(m_out_head, m_out, out_len);
715 if (rm_out == NULL) {
716 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
718 "Too little space in output mbuf");
721 /* rte_bbdev_op_data.offset can be different than the offset
722 * of the appended bytes
724 rm_out = rte_pktmbuf_mtod_offset(m_out, uint8_t *, out_offset);
726 /* index of current code block */
728 /* total number of code block */
730 /* For DL - 1, UL - 0 */
731 rm_req.direction = 1;
732 /* According to 3ggp 36.212 Spec 5.1.4.1.2 section Nsoft, KMIMO
733 * and MDL_HARQ are used for Ncb calculation. As Ncb is already
734 * known we can adjust those parameters
736 rm_req.Nsoft = ncb * rm_req.C;
739 /* According to 3ggp 36.212 Spec 5.1.4.1.2 section Nl, Qm and G
740 * are used for E calculation. As E is already known we can
741 * adjust those parameters
745 rm_req.G = rm_req.NL * rm_req.Qm * rm_req.C;
747 rm_req.rvidx = enc->rv_index;
748 rm_req.Kidx = k_idx - 1;
753 rm_resp.output = rm_out;
754 rm_resp.OutputLen = out_len;
755 if (enc->op_flags & RTE_BBDEV_TURBO_RV_INDEX_BYPASS)
756 rm_req.bypass_rvidx = 1;
758 rm_req.bypass_rvidx = 0;
760 #ifdef RTE_BBDEV_OFFLOAD_COST
761 start_time = rte_rdtsc_precise();
764 if (bblib_rate_match_dl(&rm_req, &rm_resp) != 0) {
765 op->status |= 1 << RTE_BBDEV_DRV_ERROR;
766 rte_bbdev_log(ERR, "Rate matching failed");
769 #ifdef RTE_BBDEV_OFFLOAD_COST
770 q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
773 /* SW fills an entire last byte even if E%8 != 0. Clear the
774 * superfluous data bits for consistency with HW device.
776 mask_id = (e & 7) >> 1;
777 rm_out[out_len - 1] &= mask_out[mask_id];
778 enc->output.length += rm_resp.OutputLen;
780 /* Rate matching is bypassed */
782 /* Completing last byte of out0 (where 4 tail bits are stored)
783 * by moving first 4 bits from out1
785 tmp_out = (uint8_t *) --out1;
786 *tmp_out = *tmp_out | ((*(tmp_out + 1) & 0xF0) >> 4);
788 /* Shifting out1 data by 4 bits to the left */
789 for (m = 0; m < k >> 3; ++m) {
790 uint8_t *first = tmp_out;
791 uint8_t second = *(tmp_out + 1);
792 *first = (*first << 4) | ((second & 0xF0) >> 4);
795 /* Shifting out2 data by 8 bits to the left */
796 for (m = 0; m < (k >> 3) + 1; ++m) {
797 *tmp_out = *(tmp_out + 1);
811 RTE_SET_USED(m_out_head);
813 RTE_SET_USED(in_offset);
814 RTE_SET_USED(out_offset);
815 RTE_SET_USED(in_length);
816 RTE_SET_USED(q_stats);
822 process_ldpc_enc_cb(struct turbo_sw_queue *q, struct rte_bbdev_enc_op *op,
823 uint32_t e, struct rte_mbuf *m_in, struct rte_mbuf *m_out_head,
824 struct rte_mbuf *m_out, uint16_t in_offset, uint16_t out_offset,
825 uint16_t seg_total_left, struct rte_bbdev_stats *q_stats)
827 #ifdef RTE_BBDEV_SDK_AVX512
828 RTE_SET_USED(seg_total_left);
829 uint8_t *in, *rm_out;
830 struct rte_bbdev_op_ldpc_enc *enc = &op->ldpc_enc;
831 struct bblib_ldpc_encoder_5gnr_request ldpc_req;
832 struct bblib_ldpc_encoder_5gnr_response ldpc_resp;
833 struct bblib_LDPC_ratematch_5gnr_request rm_req;
834 struct bblib_LDPC_ratematch_5gnr_response rm_resp;
835 struct bblib_crc_request crc_req;
836 struct bblib_crc_response crc_resp;
837 uint16_t msgLen, puntBits, parity_offset, out_len;
838 uint16_t K = (enc->basegraph == 1 ? 22 : 10) * enc->z_c;
839 uint16_t in_length_in_bits = K - enc->n_filler;
840 uint16_t in_length_in_bytes = (in_length_in_bits + 7) >> 3;
842 #ifdef RTE_BBDEV_OFFLOAD_COST
843 uint64_t start_time = rte_rdtsc_precise();
845 RTE_SET_USED(q_stats);
848 in = rte_pktmbuf_mtod_offset(m_in, uint8_t *, in_offset);
850 /* Masking the Filler bits explicitly */
851 memset(q->enc_in + (in_length_in_bytes - 3), 0,
852 ((K + 7) >> 3) - (in_length_in_bytes - 3));
854 if (enc->op_flags & RTE_BBDEV_LDPC_CRC_24A_ATTACH) {
855 rte_memcpy(q->enc_in, in, in_length_in_bytes - 3);
857 crc_req.len = in_length_in_bits - 24;
858 crc_resp.data = q->enc_in;
859 bblib_lte_crc24a_gen(&crc_req, &crc_resp);
860 } else if (enc->op_flags & RTE_BBDEV_LDPC_CRC_24B_ATTACH) {
861 rte_memcpy(q->enc_in, in, in_length_in_bytes - 3);
863 crc_req.len = in_length_in_bits - 24;
864 crc_resp.data = q->enc_in;
865 bblib_lte_crc24b_gen(&crc_req, &crc_resp);
867 rte_memcpy(q->enc_in, in, in_length_in_bytes);
870 ldpc_req.Zc = enc->z_c;
871 ldpc_req.baseGraph = enc->basegraph;
872 /* Number of rows set to maximum */
873 ldpc_req.nRows = ldpc_req.baseGraph == 1 ? 46 : 42;
874 ldpc_req.numberCodeblocks = 1;
875 ldpc_req.input[0] = (int8_t *) q->enc_in;
876 ldpc_resp.output[0] = (int8_t *) q->enc_out;
878 bblib_bit_reverse(ldpc_req.input[0], in_length_in_bytes << 3);
880 if (bblib_ldpc_encoder_5gnr(&ldpc_req, &ldpc_resp) != 0) {
881 op->status |= 1 << RTE_BBDEV_DRV_ERROR;
882 rte_bbdev_log(ERR, "LDPC Encoder failed");
887 * Systematic + Parity : Recreating stream with filler bits, ideally
888 * the bit select could handle this in the RM SDK
890 msgLen = (ldpc_req.baseGraph == 1 ? 22 : 10) * ldpc_req.Zc;
891 puntBits = 2 * ldpc_req.Zc;
892 parity_offset = msgLen - puntBits;
893 ippsCopyBE_1u(((uint8_t *) ldpc_req.input[0]) + (puntBits / 8),
894 puntBits%8, q->adapter_output, 0, parity_offset);
895 ippsCopyBE_1u(q->enc_out, 0, q->adapter_output + (parity_offset / 8),
896 parity_offset % 8, ldpc_req.nRows * ldpc_req.Zc);
898 out_len = (e + 7) >> 3;
899 /* get output data starting address */
900 rm_out = (uint8_t *)mbuf_append(m_out_head, m_out, out_len);
901 if (rm_out == NULL) {
902 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
904 "Too little space in output mbuf");
908 * rte_bbdev_op_data.offset can be different than the offset
909 * of the appended bytes
911 rm_out = rte_pktmbuf_mtod_offset(m_out, uint8_t *, out_offset);
915 rm_req.Ncb = enc->n_cb;
916 rm_req.Qm = enc->q_m;
917 rm_req.Zc = enc->z_c;
918 rm_req.baseGraph = enc->basegraph;
919 rm_req.input = q->adapter_output;
920 rm_req.nLen = enc->n_filler;
921 rm_req.nullIndex = parity_offset - enc->n_filler;
922 rm_req.rvidx = enc->rv_index;
923 rm_resp.output = q->deint_output;
925 if (bblib_LDPC_ratematch_5gnr(&rm_req, &rm_resp) != 0) {
926 op->status |= 1 << RTE_BBDEV_DRV_ERROR;
927 rte_bbdev_log(ERR, "Rate matching failed");
931 /* RM SDK may provide non zero bits on last byte */
933 q->deint_output[out_len-1] &= (1 << (e % 8)) - 1;
935 bblib_bit_reverse((int8_t *) q->deint_output, out_len << 3);
937 rte_memcpy(rm_out, q->deint_output, out_len);
938 enc->output.length += out_len;
940 #ifdef RTE_BBDEV_OFFLOAD_COST
941 q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
948 RTE_SET_USED(m_out_head);
950 RTE_SET_USED(in_offset);
951 RTE_SET_USED(out_offset);
952 RTE_SET_USED(seg_total_left);
953 RTE_SET_USED(q_stats);
958 enqueue_enc_one_op(struct turbo_sw_queue *q, struct rte_bbdev_enc_op *op,
959 struct rte_bbdev_stats *queue_stats)
961 uint8_t c, r, crc24_bits = 0;
964 struct rte_bbdev_op_turbo_enc *enc = &op->turbo_enc;
965 uint16_t in_offset = enc->input.offset;
966 uint16_t out_offset = enc->output.offset;
967 struct rte_mbuf *m_in = enc->input.data;
968 struct rte_mbuf *m_out = enc->output.data;
969 struct rte_mbuf *m_out_head = enc->output.data;
970 uint32_t in_length, mbuf_total_left = enc->input.length;
971 uint16_t seg_total_left;
973 /* Clear op status */
976 if (mbuf_total_left > RTE_BBDEV_TURBO_MAX_TB_SIZE >> 3) {
977 rte_bbdev_log(ERR, "TB size (%u) is too big, max: %d",
978 mbuf_total_left, RTE_BBDEV_TURBO_MAX_TB_SIZE);
979 op->status = 1 << RTE_BBDEV_DATA_ERROR;
983 if (m_in == NULL || m_out == NULL) {
984 rte_bbdev_log(ERR, "Invalid mbuf pointer");
985 op->status = 1 << RTE_BBDEV_DATA_ERROR;
989 if ((enc->op_flags & RTE_BBDEV_TURBO_CRC_24B_ATTACH) ||
990 (enc->op_flags & RTE_BBDEV_TURBO_CRC_24A_ATTACH))
993 if (enc->code_block_mode == 0) { /* For Transport Block mode */
994 c = enc->tb_params.c;
995 r = enc->tb_params.r;
996 } else {/* For Code Block mode */
1001 while (mbuf_total_left > 0 && r < c) {
1003 seg_total_left = rte_pktmbuf_data_len(m_in) - in_offset;
1005 if (enc->code_block_mode == 0) {
1006 k = (r < enc->tb_params.c_neg) ?
1007 enc->tb_params.k_neg : enc->tb_params.k_pos;
1008 ncb = (r < enc->tb_params.c_neg) ?
1009 enc->tb_params.ncb_neg : enc->tb_params.ncb_pos;
1010 e = (r < enc->tb_params.cab) ?
1011 enc->tb_params.ea : enc->tb_params.eb;
1013 k = enc->cb_params.k;
1014 ncb = enc->cb_params.ncb;
1015 e = enc->cb_params.e;
1018 process_enc_cb(q, op, r, c, k, ncb, e, m_in, m_out_head,
1019 m_out, in_offset, out_offset, seg_total_left,
1021 /* Update total_left */
1022 in_length = ((k - crc24_bits) >> 3);
1023 mbuf_total_left -= in_length;
1024 /* Update offsets for next CBs (if exist) */
1025 in_offset += (k - crc24_bits) >> 3;
1026 if (enc->op_flags & RTE_BBDEV_TURBO_RATE_MATCH)
1027 out_offset += e >> 3;
1029 out_offset += (k >> 3) * 3 + 2;
1031 /* Update offsets */
1032 if (seg_total_left == in_length) {
1033 /* Go to the next mbuf */
1035 m_out = m_out->next;
1042 /* check if all input data was processed */
1043 if (mbuf_total_left != 0) {
1044 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1046 "Mismatch between mbuf length and included CBs sizes");
1052 enqueue_ldpc_enc_one_op(struct turbo_sw_queue *q, struct rte_bbdev_enc_op *op,
1053 struct rte_bbdev_stats *queue_stats)
1055 uint8_t c, r, crc24_bits = 0;
1057 struct rte_bbdev_op_ldpc_enc *enc = &op->ldpc_enc;
1058 uint16_t in_offset = enc->input.offset;
1059 uint16_t out_offset = enc->output.offset;
1060 struct rte_mbuf *m_in = enc->input.data;
1061 struct rte_mbuf *m_out = enc->output.data;
1062 struct rte_mbuf *m_out_head = enc->output.data;
1063 uint32_t in_length, mbuf_total_left = enc->input.length;
1065 uint16_t seg_total_left;
1067 /* Clear op status */
1070 if (mbuf_total_left > RTE_BBDEV_TURBO_MAX_TB_SIZE >> 3) {
1071 rte_bbdev_log(ERR, "TB size (%u) is too big, max: %d",
1072 mbuf_total_left, RTE_BBDEV_TURBO_MAX_TB_SIZE);
1073 op->status = 1 << RTE_BBDEV_DATA_ERROR;
1077 if (m_in == NULL || m_out == NULL) {
1078 rte_bbdev_log(ERR, "Invalid mbuf pointer");
1079 op->status = 1 << RTE_BBDEV_DATA_ERROR;
1083 if ((enc->op_flags & RTE_BBDEV_TURBO_CRC_24B_ATTACH) ||
1084 (enc->op_flags & RTE_BBDEV_TURBO_CRC_24A_ATTACH))
1087 if (enc->code_block_mode == 0) { /* For Transport Block mode */
1088 c = enc->tb_params.c;
1089 r = enc->tb_params.r;
1090 } else { /* For Code Block mode */
1095 while (mbuf_total_left > 0 && r < c) {
1097 seg_total_left = rte_pktmbuf_data_len(m_in) - in_offset;
1099 if (enc->code_block_mode == 0) {
1100 e = (r < enc->tb_params.cab) ?
1101 enc->tb_params.ea : enc->tb_params.eb;
1103 e = enc->cb_params.e;
1106 process_ldpc_enc_cb(q, op, e, m_in, m_out_head,
1107 m_out, in_offset, out_offset, seg_total_left,
1109 /* Update total_left */
1110 in_length = (enc->basegraph == 1 ? 22 : 10) * enc->z_c;
1111 in_length = ((in_length - crc24_bits - enc->n_filler) >> 3);
1112 mbuf_total_left -= in_length;
1113 /* Update offsets for next CBs (if exist) */
1114 in_offset += in_length;
1115 out_offset += (e + 7) >> 3;
1117 /* Update offsets */
1118 if (seg_total_left == in_length) {
1119 /* Go to the next mbuf */
1121 m_out = m_out->next;
1128 /* check if all input data was processed */
1129 if (mbuf_total_left != 0) {
1130 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1132 "Mismatch between mbuf length and included CBs sizes %d",
1137 static inline uint16_t
1138 enqueue_enc_all_ops(struct turbo_sw_queue *q, struct rte_bbdev_enc_op **ops,
1139 uint16_t nb_ops, struct rte_bbdev_stats *queue_stats)
1142 #ifdef RTE_BBDEV_OFFLOAD_COST
1143 queue_stats->acc_offload_cycles = 0;
1146 for (i = 0; i < nb_ops; ++i)
1147 enqueue_enc_one_op(q, ops[i], queue_stats);
1149 return rte_ring_enqueue_burst(q->processed_pkts, (void **)ops, nb_ops,
1153 static inline uint16_t
1154 enqueue_ldpc_enc_all_ops(struct turbo_sw_queue *q,
1155 struct rte_bbdev_enc_op **ops,
1156 uint16_t nb_ops, struct rte_bbdev_stats *queue_stats)
1159 #ifdef RTE_BBDEV_OFFLOAD_COST
1160 queue_stats->acc_offload_cycles = 0;
1163 for (i = 0; i < nb_ops; ++i)
1164 enqueue_ldpc_enc_one_op(q, ops[i], queue_stats);
1166 return rte_ring_enqueue_burst(q->processed_pkts, (void **)ops, nb_ops,
1170 #ifdef RTE_BBDEV_SDK_AVX2
1172 move_padding_bytes(const uint8_t *in, uint8_t *out, uint16_t k,
1176 uint16_t kpi = ncb / 3;
1177 uint16_t nd = kpi - d;
1179 rte_memcpy(&out[nd], in, d);
1180 rte_memcpy(&out[nd + kpi + 64], &in[kpi], d);
1181 rte_memcpy(&out[(nd - 1) + 2 * (kpi + 64)], &in[2 * kpi], d);
1186 process_dec_cb(struct turbo_sw_queue *q, struct rte_bbdev_dec_op *op,
1187 uint8_t c, uint16_t k, uint16_t kw, struct rte_mbuf *m_in,
1188 struct rte_mbuf *m_out_head, struct rte_mbuf *m_out,
1189 uint16_t in_offset, uint16_t out_offset, bool check_crc_24b,
1190 uint16_t crc24_overlap, uint16_t in_length,
1191 struct rte_bbdev_stats *q_stats)
1193 #ifdef RTE_BBDEV_SDK_AVX2
1194 #ifdef RTE_LIBRTE_BBDEV_DEBUG
1197 RTE_SET_USED(in_length);
1201 uint8_t *in, *out, *adapter_input;
1202 int32_t ncb, ncb_without_null;
1203 struct bblib_turbo_adapter_ul_response adapter_resp;
1204 struct bblib_turbo_adapter_ul_request adapter_req;
1205 struct bblib_turbo_decoder_request turbo_req;
1206 struct bblib_turbo_decoder_response turbo_resp;
1207 struct rte_bbdev_op_turbo_dec *dec = &op->turbo_dec;
1208 #ifdef RTE_BBDEV_OFFLOAD_COST
1209 uint64_t start_time;
1211 RTE_SET_USED(q_stats);
1214 k_idx = compute_idx(k);
1216 #ifdef RTE_LIBRTE_BBDEV_DEBUG
1217 ret = is_dec_input_valid(k_idx, kw, in_length);
1219 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1224 in = rte_pktmbuf_mtod_offset(m_in, uint8_t *, in_offset);
1226 ncb_without_null = (k + 4) * 3;
1228 if (check_bit(dec->op_flags, RTE_BBDEV_TURBO_SUBBLOCK_DEINTERLEAVE)) {
1229 struct bblib_deinterleave_ul_request deint_req;
1230 struct bblib_deinterleave_ul_response deint_resp;
1232 deint_req.circ_buffer = BBLIB_FULL_CIRCULAR_BUFFER;
1233 deint_req.pharqbuffer = in;
1234 deint_req.ncb = ncb;
1235 deint_resp.pinteleavebuffer = q->deint_output;
1237 #ifdef RTE_BBDEV_OFFLOAD_COST
1238 start_time = rte_rdtsc_precise();
1240 /* Sub-block De-Interleaving */
1241 bblib_deinterleave_ul(&deint_req, &deint_resp);
1242 #ifdef RTE_BBDEV_OFFLOAD_COST
1243 q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
1246 move_padding_bytes(in, q->deint_output, k, ncb);
1248 adapter_input = q->deint_output;
1250 if (dec->op_flags & RTE_BBDEV_TURBO_POS_LLR_1_BIT_IN)
1251 adapter_req.isinverted = 1;
1252 else if (dec->op_flags & RTE_BBDEV_TURBO_NEG_LLR_1_BIT_IN)
1253 adapter_req.isinverted = 0;
1255 op->status |= 1 << RTE_BBDEV_DRV_ERROR;
1256 rte_bbdev_log(ERR, "LLR format wasn't specified");
1260 adapter_req.ncb = ncb_without_null;
1261 adapter_req.pinteleavebuffer = adapter_input;
1262 adapter_resp.pharqout = q->adapter_output;
1264 #ifdef RTE_BBDEV_OFFLOAD_COST
1265 start_time = rte_rdtsc_precise();
1267 /* Turbo decode adaptation */
1268 bblib_turbo_adapter_ul(&adapter_req, &adapter_resp);
1269 #ifdef RTE_BBDEV_OFFLOAD_COST
1270 q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
1273 out = (uint8_t *)mbuf_append(m_out_head, m_out,
1274 ((k - crc24_overlap) >> 3));
1276 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1277 rte_bbdev_log(ERR, "Too little space in output mbuf");
1280 /* rte_bbdev_op_data.offset can be different than the offset of the
1283 out = rte_pktmbuf_mtod_offset(m_out, uint8_t *, out_offset);
1285 turbo_req.c = c + 1;
1288 turbo_req.input = (int8_t *)q->adapter_output;
1290 turbo_req.k_idx = k_idx;
1291 turbo_req.max_iter_num = dec->iter_max;
1292 turbo_req.early_term_disable = !check_bit(dec->op_flags,
1293 RTE_BBDEV_TURBO_EARLY_TERMINATION);
1294 turbo_resp.ag_buf = q->ag;
1295 turbo_resp.cb_buf = q->code_block;
1296 turbo_resp.output = out;
1298 #ifdef RTE_BBDEV_OFFLOAD_COST
1299 start_time = rte_rdtsc_precise();
1302 iter_cnt = bblib_turbo_decoder(&turbo_req, &turbo_resp);
1303 #ifdef RTE_BBDEV_OFFLOAD_COST
1304 q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
1306 dec->hard_output.length += (k >> 3);
1309 /* Temporary solution for returned iter_count from SDK */
1310 iter_cnt = (iter_cnt - 1) >> 1;
1311 dec->iter_count = RTE_MAX(iter_cnt, dec->iter_count);
1313 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1314 rte_bbdev_log(ERR, "Turbo Decoder failed");
1324 RTE_SET_USED(m_out_head);
1325 RTE_SET_USED(m_out);
1326 RTE_SET_USED(in_offset);
1327 RTE_SET_USED(out_offset);
1328 RTE_SET_USED(check_crc_24b);
1329 RTE_SET_USED(crc24_overlap);
1330 RTE_SET_USED(in_length);
1331 RTE_SET_USED(q_stats);
1336 process_ldpc_dec_cb(struct turbo_sw_queue *q, struct rte_bbdev_dec_op *op,
1337 uint8_t c, uint16_t out_length, uint32_t e,
1338 struct rte_mbuf *m_in,
1339 struct rte_mbuf *m_out_head, struct rte_mbuf *m_out,
1340 struct rte_mbuf *m_harq_in,
1341 struct rte_mbuf *m_harq_out_head, struct rte_mbuf *m_harq_out,
1342 uint16_t in_offset, uint16_t out_offset,
1343 uint16_t harq_in_offset, uint16_t harq_out_offset,
1345 uint16_t crc24_overlap, uint16_t in_length,
1346 struct rte_bbdev_stats *q_stats)
1348 #ifdef RTE_BBDEV_SDK_AVX512
1349 RTE_SET_USED(in_length);
1351 uint8_t *in, *out, *harq_in, *harq_out, *adapter_input;
1352 struct bblib_rate_dematching_5gnr_request derm_req;
1353 struct bblib_rate_dematching_5gnr_response derm_resp;
1354 struct bblib_ldpc_decoder_5gnr_request dec_req;
1355 struct bblib_ldpc_decoder_5gnr_response dec_resp;
1356 struct bblib_crc_request crc_req;
1357 struct bblib_crc_response crc_resp;
1358 struct rte_bbdev_op_ldpc_dec *dec = &op->ldpc_dec;
1359 uint16_t K, parity_offset, sys_cols, outLenWithCrc;
1360 int16_t deRmOutSize, numRows;
1362 /* Compute some LDPC BG lengths */
1363 outLenWithCrc = out_length + (crc24_overlap >> 3);
1364 sys_cols = (dec->basegraph == 1) ? 22 : 10;
1365 K = sys_cols * dec->z_c;
1366 parity_offset = K - 2 * dec->z_c;
1368 #ifdef RTE_BBDEV_OFFLOAD_COST
1369 uint64_t start_time = rte_rdtsc_precise();
1371 RTE_SET_USED(q_stats);
1374 in = rte_pktmbuf_mtod_offset(m_in, uint8_t *, in_offset);
1376 if (check_bit(dec->op_flags, RTE_BBDEV_LDPC_HQ_COMBINE_IN_ENABLE)) {
1378 * Single contiguous block from the first LLR of the
1382 if (m_harq_in != NULL)
1383 harq_in = rte_pktmbuf_mtod_offset(m_harq_in,
1384 uint8_t *, harq_in_offset);
1385 if (harq_in == NULL) {
1386 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1387 rte_bbdev_log(ERR, "No space in harq input mbuf");
1390 uint16_t harq_in_length = RTE_MIN(
1391 dec->harq_combined_input.length,
1392 (uint32_t) dec->n_cb);
1393 memset(q->ag + harq_in_length, 0,
1394 dec->n_cb - harq_in_length);
1395 rte_memcpy(q->ag, harq_in, harq_in_length);
1398 derm_req.p_in = (int8_t *) in;
1399 derm_req.p_harq = q->ag; /* This doesn't include the filler bits */
1400 derm_req.base_graph = dec->basegraph;
1401 derm_req.zc = dec->z_c;
1402 derm_req.ncb = dec->n_cb;
1404 derm_req.k0 = 0; /* Actual output from SDK */
1405 derm_req.isretx = check_bit(dec->op_flags,
1406 RTE_BBDEV_LDPC_HQ_COMBINE_IN_ENABLE);
1407 derm_req.rvid = dec->rv_index;
1408 derm_req.modulation_order = dec->q_m;
1409 derm_req.start_null_index = parity_offset - dec->n_filler;
1410 derm_req.num_of_null = dec->n_filler;
1412 bblib_rate_dematching_5gnr(&derm_req, &derm_resp);
1414 /* Compute RM out size and number of rows */
1415 deRmOutSize = RTE_MIN(
1416 derm_req.k0 + derm_req.e -
1417 ((derm_req.k0 < derm_req.start_null_index) ?
1419 dec->n_cb - dec->n_filler);
1420 if (m_harq_in != NULL)
1421 deRmOutSize = RTE_MAX(deRmOutSize,
1422 RTE_MIN(dec->n_cb - dec->n_filler,
1423 m_harq_in->data_len));
1424 numRows = ((deRmOutSize + dec->n_filler + dec->z_c - 1) / dec->z_c)
1426 numRows = RTE_MAX(4, numRows);
1428 /* get output data starting address */
1429 out = (uint8_t *)mbuf_append(m_out_head, m_out, out_length);
1431 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1433 "Too little space in LDPC decoder output mbuf");
1437 /* rte_bbdev_op_data.offset can be different than the offset
1438 * of the appended bytes
1440 out = rte_pktmbuf_mtod_offset(m_out, uint8_t *, out_offset);
1441 adapter_input = q->enc_out;
1443 dec_req.Zc = dec->z_c;
1444 dec_req.baseGraph = dec->basegraph;
1445 dec_req.nRows = numRows;
1446 dec_req.numChannelLlrs = deRmOutSize;
1447 dec_req.varNodes = derm_req.p_harq;
1448 dec_req.numFillerBits = dec->n_filler;
1449 dec_req.maxIterations = dec->iter_max;
1450 dec_req.enableEarlyTermination = check_bit(dec->op_flags,
1451 RTE_BBDEV_LDPC_ITERATION_STOP_ENABLE);
1452 dec_resp.varNodes = (int16_t *) q->adapter_output;
1453 dec_resp.compactedMessageBytes = q->enc_out;
1455 bblib_ldpc_decoder_5gnr(&dec_req, &dec_resp);
1457 dec->iter_count = RTE_MAX(dec_resp.iterationAtTermination,
1459 if (!dec_resp.parityPassedAtTermination)
1460 op->status |= 1 << RTE_BBDEV_SYNDROME_ERROR;
1462 bblib_bit_reverse((int8_t *) q->enc_out, outLenWithCrc << 3);
1464 if (check_bit(dec->op_flags, RTE_BBDEV_LDPC_CRC_TYPE_24A_CHECK) ||
1465 check_bit(dec->op_flags,
1466 RTE_BBDEV_LDPC_CRC_TYPE_24B_CHECK)) {
1467 crc_req.data = adapter_input;
1468 crc_req.len = K - dec->n_filler - 24;
1469 crc_resp.check_passed = false;
1470 crc_resp.data = adapter_input;
1472 bblib_lte_crc24b_check(&crc_req, &crc_resp);
1474 bblib_lte_crc24a_check(&crc_req, &crc_resp);
1475 if (!crc_resp.check_passed)
1476 op->status |= 1 << RTE_BBDEV_CRC_ERROR;
1479 #ifdef RTE_BBDEV_OFFLOAD_COST
1480 q_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
1482 if (check_bit(dec->op_flags, RTE_BBDEV_LDPC_HQ_COMBINE_OUT_ENABLE)) {
1484 if (m_harq_out != NULL) {
1485 /* Initialize HARQ data length since we overwrite */
1486 m_harq_out->data_len = 0;
1487 /* Check there is enough space
1488 * in the HARQ outbound buffer
1490 harq_out = (uint8_t *)mbuf_append(m_harq_out_head,
1491 m_harq_out, deRmOutSize);
1493 if (harq_out == NULL) {
1494 op->status |= 1 << RTE_BBDEV_DATA_ERROR;
1495 rte_bbdev_log(ERR, "No space in HARQ output mbuf");
1498 /* get output data starting address and overwrite the data */
1499 harq_out = rte_pktmbuf_mtod_offset(m_harq_out, uint8_t *,
1501 rte_memcpy(harq_out, derm_req.p_harq, deRmOutSize);
1502 dec->harq_combined_output.length += deRmOutSize;
1505 rte_memcpy(out, adapter_input, out_length);
1506 dec->hard_output.length += out_length;
1511 RTE_SET_USED(out_length);
1514 RTE_SET_USED(m_out_head);
1515 RTE_SET_USED(m_out);
1516 RTE_SET_USED(m_harq_in);
1517 RTE_SET_USED(m_harq_out_head);
1518 RTE_SET_USED(m_harq_out);
1519 RTE_SET_USED(harq_in_offset);
1520 RTE_SET_USED(harq_out_offset);
1521 RTE_SET_USED(in_offset);
1522 RTE_SET_USED(out_offset);
1523 RTE_SET_USED(check_crc_24b);
1524 RTE_SET_USED(crc24_overlap);
1525 RTE_SET_USED(in_length);
1526 RTE_SET_USED(q_stats);
1532 enqueue_dec_one_op(struct turbo_sw_queue *q, struct rte_bbdev_dec_op *op,
1533 struct rte_bbdev_stats *queue_stats)
1537 uint16_t crc24_overlap = 0;
1538 struct rte_bbdev_op_turbo_dec *dec = &op->turbo_dec;
1539 struct rte_mbuf *m_in = dec->input.data;
1540 struct rte_mbuf *m_out = dec->hard_output.data;
1541 struct rte_mbuf *m_out_head = dec->hard_output.data;
1542 uint16_t in_offset = dec->input.offset;
1543 uint16_t out_offset = dec->hard_output.offset;
1544 uint32_t mbuf_total_left = dec->input.length;
1545 uint16_t seg_total_left;
1547 /* Clear op status */
1550 if (m_in == NULL || m_out == NULL) {
1551 rte_bbdev_log(ERR, "Invalid mbuf pointer");
1552 op->status = 1 << RTE_BBDEV_DATA_ERROR;
1556 if (dec->code_block_mode == 0) { /* For Transport Block mode */
1557 c = dec->tb_params.c;
1558 } else { /* For Code Block mode */
1559 k = dec->cb_params.k;
1563 if ((c > 1) && !check_bit(dec->op_flags,
1564 RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP))
1567 while (mbuf_total_left > 0) {
1568 if (dec->code_block_mode == 0)
1569 k = (r < dec->tb_params.c_neg) ?
1570 dec->tb_params.k_neg : dec->tb_params.k_pos;
1572 seg_total_left = rte_pktmbuf_data_len(m_in) - in_offset;
1574 /* Calculates circular buffer size (Kw).
1575 * According to 3gpp 36.212 section 5.1.4.2
1579 * where nCol is 32 and nRow can be calculated from:
1581 * where D is the size of each output from turbo encoder block
1584 kw = RTE_ALIGN_CEIL(k + 4, RTE_BBDEV_TURBO_C_SUBBLOCK) * 3;
1586 process_dec_cb(q, op, c, k, kw, m_in, m_out_head, m_out,
1587 in_offset, out_offset, check_bit(dec->op_flags,
1588 RTE_BBDEV_TURBO_CRC_TYPE_24B), crc24_overlap,
1589 seg_total_left, queue_stats);
1591 /* To keep CRC24 attached to end of Code block, use
1592 * RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP flag as it
1593 * removed by default once verified.
1596 mbuf_total_left -= kw;
1598 /* Update offsets */
1599 if (seg_total_left == kw) {
1600 /* Go to the next mbuf */
1602 m_out = m_out->next;
1606 /* Update offsets for next CBs (if exist) */
1608 out_offset += ((k - crc24_overlap) >> 3);
1615 enqueue_ldpc_dec_one_op(struct turbo_sw_queue *q, struct rte_bbdev_dec_op *op,
1616 struct rte_bbdev_stats *queue_stats)
1620 uint16_t out_length, crc24_overlap = 0;
1621 struct rte_bbdev_op_ldpc_dec *dec = &op->ldpc_dec;
1622 struct rte_mbuf *m_in = dec->input.data;
1623 struct rte_mbuf *m_harq_in = dec->harq_combined_input.data;
1624 struct rte_mbuf *m_harq_out = dec->harq_combined_output.data;
1625 struct rte_mbuf *m_harq_out_head = dec->harq_combined_output.data;
1626 struct rte_mbuf *m_out = dec->hard_output.data;
1627 struct rte_mbuf *m_out_head = dec->hard_output.data;
1628 uint16_t in_offset = dec->input.offset;
1629 uint16_t harq_in_offset = dec->harq_combined_input.offset;
1630 uint16_t harq_out_offset = dec->harq_combined_output.offset;
1631 uint16_t out_offset = dec->hard_output.offset;
1632 uint32_t mbuf_total_left = dec->input.length;
1633 uint16_t seg_total_left;
1635 /* Clear op status */
1638 if (m_in == NULL || m_out == NULL) {
1639 rte_bbdev_log(ERR, "Invalid mbuf pointer");
1640 op->status = 1 << RTE_BBDEV_DATA_ERROR;
1644 if (dec->code_block_mode == 0) { /* For Transport Block mode */
1645 c = dec->tb_params.c;
1646 e = dec->tb_params.ea;
1647 } else { /* For Code Block mode */
1649 e = dec->cb_params.e;
1652 if (check_bit(dec->op_flags, RTE_BBDEV_LDPC_CRC_TYPE_24B_DROP))
1655 out_length = (dec->basegraph == 1 ? 22 : 10) * dec->z_c; /* K */
1656 out_length = ((out_length - crc24_overlap - dec->n_filler) >> 3);
1658 while (mbuf_total_left > 0) {
1659 if (dec->code_block_mode == 0)
1660 e = (r < dec->tb_params.cab) ?
1661 dec->tb_params.ea : dec->tb_params.eb;
1662 /* Special case handling when overusing mbuf */
1663 if (e < RTE_BBDEV_LDPC_E_MAX_MBUF)
1664 seg_total_left = rte_pktmbuf_data_len(m_in) - in_offset;
1668 process_ldpc_dec_cb(q, op, c, out_length, e,
1669 m_in, m_out_head, m_out,
1670 m_harq_in, m_harq_out_head, m_harq_out,
1671 in_offset, out_offset, harq_in_offset,
1673 check_bit(dec->op_flags,
1674 RTE_BBDEV_LDPC_CRC_TYPE_24B_CHECK),
1676 seg_total_left, queue_stats);
1678 /* To keep CRC24 attached to end of Code block, use
1679 * RTE_BBDEV_LDPC_DEC_TB_CRC_24B_KEEP flag as it
1680 * removed by default once verified.
1683 mbuf_total_left -= e;
1685 /* Update offsets */
1686 if (seg_total_left == e) {
1687 /* Go to the next mbuf */
1689 m_out = m_out->next;
1690 if (m_harq_in != NULL)
1691 m_harq_in = m_harq_in->next;
1692 if (m_harq_out != NULL)
1693 m_harq_out = m_harq_out->next;
1697 harq_out_offset = 0;
1699 /* Update offsets for next CBs (if exist) */
1701 out_offset += out_length;
1707 static inline uint16_t
1708 enqueue_dec_all_ops(struct turbo_sw_queue *q, struct rte_bbdev_dec_op **ops,
1709 uint16_t nb_ops, struct rte_bbdev_stats *queue_stats)
1712 #ifdef RTE_BBDEV_OFFLOAD_COST
1713 queue_stats->acc_offload_cycles = 0;
1716 for (i = 0; i < nb_ops; ++i)
1717 enqueue_dec_one_op(q, ops[i], queue_stats);
1719 return rte_ring_enqueue_burst(q->processed_pkts, (void **)ops, nb_ops,
1723 static inline uint16_t
1724 enqueue_ldpc_dec_all_ops(struct turbo_sw_queue *q,
1725 struct rte_bbdev_dec_op **ops,
1726 uint16_t nb_ops, struct rte_bbdev_stats *queue_stats)
1729 #ifdef RTE_BBDEV_OFFLOAD_COST
1730 queue_stats->acc_offload_cycles = 0;
1733 for (i = 0; i < nb_ops; ++i)
1734 enqueue_ldpc_dec_one_op(q, ops[i], queue_stats);
1736 return rte_ring_enqueue_burst(q->processed_pkts, (void **)ops, nb_ops,
1742 enqueue_enc_ops(struct rte_bbdev_queue_data *q_data,
1743 struct rte_bbdev_enc_op **ops, uint16_t nb_ops)
1745 void *queue = q_data->queue_private;
1746 struct turbo_sw_queue *q = queue;
1747 uint16_t nb_enqueued = 0;
1749 nb_enqueued = enqueue_enc_all_ops(q, ops, nb_ops, &q_data->queue_stats);
1751 q_data->queue_stats.enqueue_err_count += nb_ops - nb_enqueued;
1752 q_data->queue_stats.enqueued_count += nb_enqueued;
1759 enqueue_ldpc_enc_ops(struct rte_bbdev_queue_data *q_data,
1760 struct rte_bbdev_enc_op **ops, uint16_t nb_ops)
1762 void *queue = q_data->queue_private;
1763 struct turbo_sw_queue *q = queue;
1764 uint16_t nb_enqueued = 0;
1766 nb_enqueued = enqueue_ldpc_enc_all_ops(
1767 q, ops, nb_ops, &q_data->queue_stats);
1769 q_data->queue_stats.enqueue_err_count += nb_ops - nb_enqueued;
1770 q_data->queue_stats.enqueued_count += nb_enqueued;
1777 enqueue_dec_ops(struct rte_bbdev_queue_data *q_data,
1778 struct rte_bbdev_dec_op **ops, uint16_t nb_ops)
1780 void *queue = q_data->queue_private;
1781 struct turbo_sw_queue *q = queue;
1782 uint16_t nb_enqueued = 0;
1784 nb_enqueued = enqueue_dec_all_ops(q, ops, nb_ops, &q_data->queue_stats);
1786 q_data->queue_stats.enqueue_err_count += nb_ops - nb_enqueued;
1787 q_data->queue_stats.enqueued_count += nb_enqueued;
1794 enqueue_ldpc_dec_ops(struct rte_bbdev_queue_data *q_data,
1795 struct rte_bbdev_dec_op **ops, uint16_t nb_ops)
1797 void *queue = q_data->queue_private;
1798 struct turbo_sw_queue *q = queue;
1799 uint16_t nb_enqueued = 0;
1801 nb_enqueued = enqueue_ldpc_dec_all_ops(q, ops, nb_ops,
1802 &q_data->queue_stats);
1804 q_data->queue_stats.enqueue_err_count += nb_ops - nb_enqueued;
1805 q_data->queue_stats.enqueued_count += nb_enqueued;
1810 /* Dequeue decode burst */
1812 dequeue_dec_ops(struct rte_bbdev_queue_data *q_data,
1813 struct rte_bbdev_dec_op **ops, uint16_t nb_ops)
1815 struct turbo_sw_queue *q = q_data->queue_private;
1816 uint16_t nb_dequeued = rte_ring_dequeue_burst(q->processed_pkts,
1817 (void **)ops, nb_ops, NULL);
1818 q_data->queue_stats.dequeued_count += nb_dequeued;
1823 /* Dequeue encode burst */
1825 dequeue_enc_ops(struct rte_bbdev_queue_data *q_data,
1826 struct rte_bbdev_enc_op **ops, uint16_t nb_ops)
1828 struct turbo_sw_queue *q = q_data->queue_private;
1829 uint16_t nb_dequeued = rte_ring_dequeue_burst(q->processed_pkts,
1830 (void **)ops, nb_ops, NULL);
1831 q_data->queue_stats.dequeued_count += nb_dequeued;
1836 /* Parse 16bit integer from string argument */
1838 parse_u16_arg(const char *key, const char *value, void *extra_args)
1840 uint16_t *u16 = extra_args;
1841 unsigned int long result;
1843 if ((value == NULL) || (extra_args == NULL))
1846 result = strtoul(value, NULL, 0);
1847 if ((result >= (1 << 16)) || (errno != 0)) {
1848 rte_bbdev_log(ERR, "Invalid value %lu for %s", result, key);
1851 *u16 = (uint16_t)result;
1855 /* Parse parameters used to create device */
1857 parse_turbo_sw_params(struct turbo_sw_params *params, const char *input_args)
1859 struct rte_kvargs *kvlist = NULL;
1865 kvlist = rte_kvargs_parse(input_args, turbo_sw_valid_params);
1869 ret = rte_kvargs_process(kvlist, turbo_sw_valid_params[0],
1870 &parse_u16_arg, ¶ms->queues_num);
1874 ret = rte_kvargs_process(kvlist, turbo_sw_valid_params[1],
1875 &parse_u16_arg, ¶ms->socket_id);
1879 if (params->socket_id >= RTE_MAX_NUMA_NODES) {
1880 rte_bbdev_log(ERR, "Invalid socket, must be < %u",
1881 RTE_MAX_NUMA_NODES);
1888 rte_kvargs_free(kvlist);
1894 turbo_sw_bbdev_create(struct rte_vdev_device *vdev,
1895 struct turbo_sw_params *init_params)
1897 struct rte_bbdev *bbdev;
1898 const char *name = rte_vdev_device_name(vdev);
1900 bbdev = rte_bbdev_allocate(name);
1904 bbdev->data->dev_private = rte_zmalloc_socket(name,
1905 sizeof(struct bbdev_private), RTE_CACHE_LINE_SIZE,
1906 init_params->socket_id);
1907 if (bbdev->data->dev_private == NULL) {
1908 rte_bbdev_release(bbdev);
1912 bbdev->dev_ops = &pmd_ops;
1913 bbdev->device = &vdev->device;
1914 bbdev->data->socket_id = init_params->socket_id;
1915 bbdev->intr_handle = NULL;
1917 /* register rx/tx burst functions for data path */
1918 bbdev->dequeue_enc_ops = dequeue_enc_ops;
1919 bbdev->dequeue_dec_ops = dequeue_dec_ops;
1920 bbdev->enqueue_enc_ops = enqueue_enc_ops;
1921 bbdev->enqueue_dec_ops = enqueue_dec_ops;
1922 bbdev->dequeue_ldpc_enc_ops = dequeue_enc_ops;
1923 bbdev->dequeue_ldpc_dec_ops = dequeue_dec_ops;
1924 bbdev->enqueue_ldpc_enc_ops = enqueue_ldpc_enc_ops;
1925 bbdev->enqueue_ldpc_dec_ops = enqueue_ldpc_dec_ops;
1926 ((struct bbdev_private *) bbdev->data->dev_private)->max_nb_queues =
1927 init_params->queues_num;
1932 /* Initialise device */
1934 turbo_sw_bbdev_probe(struct rte_vdev_device *vdev)
1936 struct turbo_sw_params init_params = {
1938 RTE_BBDEV_DEFAULT_MAX_NB_QUEUES
1941 const char *input_args;
1946 name = rte_vdev_device_name(vdev);
1949 input_args = rte_vdev_device_args(vdev);
1950 parse_turbo_sw_params(&init_params, input_args);
1952 rte_bbdev_log_debug(
1953 "Initialising %s on NUMA node %d with max queues: %d\n",
1954 name, init_params.socket_id, init_params.queues_num);
1956 return turbo_sw_bbdev_create(vdev, &init_params);
1959 /* Uninitialise device */
1961 turbo_sw_bbdev_remove(struct rte_vdev_device *vdev)
1963 struct rte_bbdev *bbdev;
1969 name = rte_vdev_device_name(vdev);
1973 bbdev = rte_bbdev_get_named_dev(name);
1977 rte_free(bbdev->data->dev_private);
1979 return rte_bbdev_release(bbdev);
1982 static struct rte_vdev_driver bbdev_turbo_sw_pmd_drv = {
1983 .probe = turbo_sw_bbdev_probe,
1984 .remove = turbo_sw_bbdev_remove
1987 RTE_PMD_REGISTER_VDEV(DRIVER_NAME, bbdev_turbo_sw_pmd_drv);
1988 RTE_PMD_REGISTER_PARAM_STRING(DRIVER_NAME,
1989 TURBO_SW_MAX_NB_QUEUES_ARG"=<int> "
1990 TURBO_SW_SOCKET_ID_ARG"=<int>");
1991 RTE_PMD_REGISTER_ALIAS(DRIVER_NAME, turbo_sw);