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