xref: /dpdk/drivers/baseband/fpga_lte_fec/fpga_lte_fec.c (revision 1be86f2e94e4fc6e975d260ec50029e3966f1bdb)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Intel Corporation
3  */
4 
5 #include <unistd.h>
6 
7 #include <rte_common.h>
8 #include <rte_log.h>
9 #include <dev_driver.h>
10 #include <rte_malloc.h>
11 #include <rte_mempool.h>
12 #include <rte_errno.h>
13 #include <rte_pci.h>
14 #include <bus_pci_driver.h>
15 #include <rte_byteorder.h>
16 #ifdef RTE_BBDEV_OFFLOAD_COST
17 #include <rte_cycles.h>
18 #endif
19 
20 #include <rte_bbdev.h>
21 #include <rte_bbdev_pmd.h>
22 
23 #include "fpga_lte_fec.h"
24 
25 #ifdef RTE_LIBRTE_BBDEV_DEBUG
26 RTE_LOG_REGISTER_DEFAULT(fpga_lte_fec_logtype, DEBUG);
27 #else
28 RTE_LOG_REGISTER_DEFAULT(fpga_lte_fec_logtype, NOTICE);
29 #endif
30 
31 /* Helper macro for logging */
32 #define rte_bbdev_log(level, fmt, ...) \
33 	rte_log(RTE_LOG_ ## level, fpga_lte_fec_logtype, fmt "\n", \
34 		##__VA_ARGS__)
35 
36 #ifdef RTE_LIBRTE_BBDEV_DEBUG
37 #define rte_bbdev_log_debug(fmt, ...) \
38 		rte_bbdev_log(DEBUG, "fpga_lte_fec: " fmt, \
39 		##__VA_ARGS__)
40 #else
41 #define rte_bbdev_log_debug(fmt, ...)
42 #endif
43 
44 /* FPGA LTE FEC driver names */
45 #define FPGA_LTE_FEC_PF_DRIVER_NAME intel_fpga_lte_fec_pf
46 #define FPGA_LTE_FEC_VF_DRIVER_NAME intel_fpga_lte_fec_vf
47 
48 /* FPGA LTE FEC PCI vendor & device IDs */
49 #define FPGA_LTE_FEC_VENDOR_ID (0x1172)
50 #define FPGA_LTE_FEC_PF_DEVICE_ID (0x5052)
51 #define FPGA_LTE_FEC_VF_DEVICE_ID (0x5050)
52 
53 /* Align DMA descriptors to 256 bytes - cache-aligned */
54 #define FPGA_RING_DESC_ENTRY_LENGTH (8)
55 /* Ring size is in 256 bits (32 bytes) units */
56 #define FPGA_RING_DESC_LEN_UNIT_BYTES (32)
57 /* Maximum size of queue */
58 #define FPGA_RING_MAX_SIZE (1024)
59 #define FPGA_FLR_TIMEOUT_UNIT (16.384)
60 
61 #define FPGA_NUM_UL_QUEUES (32)
62 #define FPGA_NUM_DL_QUEUES (32)
63 #define FPGA_TOTAL_NUM_QUEUES (FPGA_NUM_UL_QUEUES + FPGA_NUM_DL_QUEUES)
64 #define FPGA_NUM_INTR_VEC (FPGA_TOTAL_NUM_QUEUES - RTE_INTR_VEC_RXTX_OFFSET)
65 
66 #define FPGA_INVALID_HW_QUEUE_ID (0xFFFFFFFF)
67 
68 #define FPGA_QUEUE_FLUSH_TIMEOUT_US (1000)
69 #define FPGA_TIMEOUT_CHECK_INTERVAL (5)
70 
71 /* FPGA LTE FEC Register mapping on BAR0 */
72 enum {
73 	FPGA_LTE_FEC_VERSION_ID = 0x00000000, /* len: 4B */
74 	FPGA_LTE_FEC_CONFIGURATION = 0x00000004, /* len: 2B */
75 	FPGA_LTE_FEC_QUEUE_PF_VF_MAP_DONE = 0x00000008, /* len: 1B */
76 	FPGA_LTE_FEC_LOAD_BALANCE_FACTOR = 0x0000000a, /* len: 2B */
77 	FPGA_LTE_FEC_RING_DESC_LEN = 0x0000000c, /* len: 2B */
78 	FPGA_LTE_FEC_FLR_TIME_OUT = 0x0000000e, /* len: 2B */
79 	FPGA_LTE_FEC_VFQ_FLUSH_STATUS_LW = 0x00000018, /* len: 4B */
80 	FPGA_LTE_FEC_VFQ_FLUSH_STATUS_HI = 0x0000001c, /* len: 4B */
81 	FPGA_LTE_FEC_VF0_DEBUG = 0x00000020, /* len: 4B */
82 	FPGA_LTE_FEC_VF1_DEBUG = 0x00000024, /* len: 4B */
83 	FPGA_LTE_FEC_VF2_DEBUG = 0x00000028, /* len: 4B */
84 	FPGA_LTE_FEC_VF3_DEBUG = 0x0000002c, /* len: 4B */
85 	FPGA_LTE_FEC_VF4_DEBUG = 0x00000030, /* len: 4B */
86 	FPGA_LTE_FEC_VF5_DEBUG = 0x00000034, /* len: 4B */
87 	FPGA_LTE_FEC_VF6_DEBUG = 0x00000038, /* len: 4B */
88 	FPGA_LTE_FEC_VF7_DEBUG = 0x0000003c, /* len: 4B */
89 	FPGA_LTE_FEC_QUEUE_MAP = 0x00000040, /* len: 256B */
90 	FPGA_LTE_FEC_RING_CTRL_REGS = 0x00000200  /* len: 2048B */
91 };
92 
93 /* FPGA LTE FEC Ring Control Registers */
94 enum {
95 	FPGA_LTE_FEC_RING_HEAD_ADDR = 0x00000008,
96 	FPGA_LTE_FEC_RING_SIZE = 0x00000010,
97 	FPGA_LTE_FEC_RING_MISC = 0x00000014,
98 	FPGA_LTE_FEC_RING_ENABLE = 0x00000015,
99 	FPGA_LTE_FEC_RING_FLUSH_QUEUE_EN = 0x00000016,
100 	FPGA_LTE_FEC_RING_SHADOW_TAIL = 0x00000018,
101 	FPGA_LTE_FEC_RING_HEAD_POINT = 0x0000001C
102 };
103 
104 /* FPGA LTE FEC DESCRIPTOR ERROR */
105 enum {
106 	DESC_ERR_NO_ERR = 0x0,
107 	DESC_ERR_K_OUT_OF_RANGE = 0x1,
108 	DESC_ERR_K_NOT_NORMAL = 0x2,
109 	DESC_ERR_KPAI_NOT_NORMAL = 0x3,
110 	DESC_ERR_DESC_OFFSET_ERR = 0x4,
111 	DESC_ERR_DESC_READ_FAIL = 0x8,
112 	DESC_ERR_DESC_READ_TIMEOUT = 0x9,
113 	DESC_ERR_DESC_READ_TLP_POISONED = 0xA,
114 	DESC_ERR_CB_READ_FAIL = 0xC,
115 	DESC_ERR_CB_READ_TIMEOUT = 0xD,
116 	DESC_ERR_CB_READ_TLP_POISONED = 0xE
117 };
118 
119 /* FPGA LTE FEC DMA Encoding Request Descriptor */
120 struct __rte_packed fpga_dma_enc_desc {
121 	uint32_t done:1,
122 		rsrvd0:11,
123 		error:4,
124 		rsrvd1:16;
125 	uint32_t ncb:16,
126 		rsrvd2:14,
127 		rv:2;
128 	uint32_t bypass_rm:1,
129 		irq_en:1,
130 		crc_en:1,
131 		rsrvd3:13,
132 		offset:10,
133 		rsrvd4:6;
134 	uint16_t e;
135 	uint16_t k;
136 	uint32_t out_addr_lw;
137 	uint32_t out_addr_hi;
138 	uint32_t in_addr_lw;
139 	uint32_t in_addr_hi;
140 
141 	union {
142 		struct {
143 			/* Virtual addresses used to retrieve SW context info */
144 			void *op_addr;
145 			/* Stores information about total number of Code Blocks
146 			 * in currently processed Transport Block
147 			 */
148 			uint64_t cbs_in_op;
149 		};
150 
151 		uint8_t sw_ctxt[FPGA_RING_DESC_LEN_UNIT_BYTES *
152 					(FPGA_RING_DESC_ENTRY_LENGTH - 1)];
153 	};
154 };
155 
156 /* FPGA LTE FEC DMA Decoding Request Descriptor */
157 struct __rte_packed fpga_dma_dec_desc {
158 	uint32_t done:1,
159 		iter:5,
160 		rsrvd0:2,
161 		crc_pass:1,
162 		rsrvd1:3,
163 		error:4,
164 		crc_type:1,
165 		rsrvd2:7,
166 		max_iter:5,
167 		rsrvd3:3;
168 	uint32_t rsrvd4;
169 	uint32_t bypass_rm:1,
170 		irq_en:1,
171 		drop_crc:1,
172 		rsrvd5:13,
173 		offset:10,
174 		rsrvd6:6;
175 	uint16_t k;
176 	uint16_t in_len;
177 	uint32_t out_addr_lw;
178 	uint32_t out_addr_hi;
179 	uint32_t in_addr_lw;
180 	uint32_t in_addr_hi;
181 
182 	union {
183 		struct {
184 			/* Virtual addresses used to retrieve SW context info */
185 			void *op_addr;
186 			/* Stores information about total number of Code Blocks
187 			 * in currently processed Transport Block
188 			 */
189 			uint8_t cbs_in_op;
190 		};
191 
192 		uint32_t sw_ctxt[8 * (FPGA_RING_DESC_ENTRY_LENGTH - 1)];
193 	};
194 };
195 
196 /* FPGA LTE DMA Descriptor */
197 union fpga_dma_desc {
198 	struct fpga_dma_enc_desc enc_req;
199 	struct fpga_dma_dec_desc dec_req;
200 };
201 
202 /* FPGA LTE FEC Ring Control Register */
203 struct __rte_packed fpga_ring_ctrl_reg {
204 	uint64_t ring_base_addr;
205 	uint64_t ring_head_addr;
206 	uint16_t ring_size:11;
207 	uint16_t rsrvd0;
208 	union { /* Miscellaneous register */
209 		uint8_t misc;
210 		uint8_t max_ul_dec:5,
211 			max_ul_dec_en:1,
212 			rsrvd1:2;
213 	};
214 	uint8_t enable;
215 	uint8_t flush_queue_en;
216 	uint8_t rsrvd2;
217 	uint16_t shadow_tail;
218 	uint16_t rsrvd3;
219 	uint16_t head_point;
220 	uint16_t rsrvd4;
221 
222 };
223 
224 /* Private data structure for each FPGA FEC device */
225 struct fpga_lte_fec_device {
226 	/** Base address of MMIO registers (BAR0) */
227 	void *mmio_base;
228 	/** Base address of memory for sw rings */
229 	void *sw_rings;
230 	/** Physical address of sw_rings */
231 	rte_iova_t sw_rings_phys;
232 	/** Number of bytes available for each queue in device. */
233 	uint32_t sw_ring_size;
234 	/** Max number of entries available for each queue in device */
235 	uint32_t sw_ring_max_depth;
236 	/** Base address of response tail pointer buffer */
237 	uint32_t *tail_ptrs;
238 	/** Physical address of tail pointers */
239 	rte_iova_t tail_ptr_phys;
240 	/** Queues flush completion flag */
241 	uint64_t *flush_queue_status;
242 	/* Bitmap capturing which Queues are bound to the PF/VF */
243 	uint64_t q_bound_bit_map;
244 	/* Bitmap capturing which Queues have already been assigned */
245 	uint64_t q_assigned_bit_map;
246 	/** True if this is a PF FPGA FEC device */
247 	bool pf_device;
248 };
249 
250 /* Structure associated with each queue. */
251 struct __rte_cache_aligned fpga_queue {
252 	struct fpga_ring_ctrl_reg ring_ctrl_reg;  /* Ring Control Register */
253 	union fpga_dma_desc *ring_addr;  /* Virtual address of software ring */
254 	uint64_t *ring_head_addr;  /* Virtual address of completion_head */
255 	uint64_t shadow_completion_head; /* Shadow completion head value */
256 	uint16_t head_free_desc;  /* Ring head */
257 	uint16_t tail;  /* Ring tail */
258 	/* Mask used to wrap enqueued descriptors on the sw ring */
259 	uint32_t sw_ring_wrap_mask;
260 	uint32_t irq_enable;  /* Enable ops dequeue interrupts if set to 1 */
261 	uint8_t q_idx;  /* Queue index */
262 	struct fpga_lte_fec_device *d;
263 	/* MMIO register of shadow_tail used to enqueue descriptors */
264 	void *shadow_tail_addr;
265 };
266 
267 /* Write to 16 bit MMIO register address */
268 static inline void
269 mmio_write_16(void *addr, uint16_t value)
270 {
271 	*((volatile uint16_t *)(addr)) = rte_cpu_to_le_16(value);
272 }
273 
274 /* Write to 32 bit MMIO register address */
275 static inline void
276 mmio_write_32(void *addr, uint32_t value)
277 {
278 	*((volatile uint32_t *)(addr)) = rte_cpu_to_le_32(value);
279 }
280 
281 /* Write to 64 bit MMIO register address */
282 static inline void
283 mmio_write_64(void *addr, uint64_t value)
284 {
285 	*((volatile uint64_t *)(addr)) = rte_cpu_to_le_64(value);
286 }
287 
288 /* Write a 8 bit register of a FPGA LTE FEC device */
289 static inline void
290 fpga_reg_write_8(void *mmio_base, uint32_t offset, uint8_t payload)
291 {
292 	void *reg_addr = RTE_PTR_ADD(mmio_base, offset);
293 	*((volatile uint8_t *)(reg_addr)) = payload;
294 }
295 
296 /* Write a 16 bit register of a FPGA LTE FEC device */
297 static inline void
298 fpga_reg_write_16(void *mmio_base, uint32_t offset, uint16_t payload)
299 {
300 	void *reg_addr = RTE_PTR_ADD(mmio_base, offset);
301 	mmio_write_16(reg_addr, payload);
302 }
303 
304 /* Write a 32 bit register of a FPGA LTE FEC device */
305 static inline void
306 fpga_reg_write_32(void *mmio_base, uint32_t offset, uint32_t payload)
307 {
308 	void *reg_addr = RTE_PTR_ADD(mmio_base, offset);
309 	mmio_write_32(reg_addr, payload);
310 }
311 
312 /* Write a 64 bit register of a FPGA LTE FEC device */
313 static inline void
314 fpga_reg_write_64(void *mmio_base, uint32_t offset, uint64_t payload)
315 {
316 	void *reg_addr = RTE_PTR_ADD(mmio_base, offset);
317 	mmio_write_64(reg_addr, payload);
318 }
319 
320 /* Write a ring control register of a FPGA LTE FEC device */
321 static inline void
322 fpga_ring_reg_write(void *mmio_base, uint32_t offset,
323 		struct fpga_ring_ctrl_reg payload)
324 {
325 	fpga_reg_write_64(mmio_base, offset, payload.ring_base_addr);
326 	fpga_reg_write_64(mmio_base, offset + FPGA_LTE_FEC_RING_HEAD_ADDR,
327 			payload.ring_head_addr);
328 	fpga_reg_write_16(mmio_base, offset + FPGA_LTE_FEC_RING_SIZE,
329 			payload.ring_size);
330 	fpga_reg_write_16(mmio_base, offset + FPGA_LTE_FEC_RING_HEAD_POINT,
331 			payload.head_point);
332 	fpga_reg_write_8(mmio_base, offset + FPGA_LTE_FEC_RING_FLUSH_QUEUE_EN,
333 			payload.flush_queue_en);
334 	fpga_reg_write_16(mmio_base, offset + FPGA_LTE_FEC_RING_SHADOW_TAIL,
335 			payload.shadow_tail);
336 	fpga_reg_write_8(mmio_base, offset + FPGA_LTE_FEC_RING_MISC,
337 			payload.misc);
338 	fpga_reg_write_8(mmio_base, offset + FPGA_LTE_FEC_RING_ENABLE,
339 			payload.enable);
340 }
341 
342 /* Read a register of FPGA LTE FEC device */
343 static uint32_t
344 fpga_reg_read_32(void *mmio_base, uint32_t offset)
345 {
346 	void *reg_addr = RTE_PTR_ADD(mmio_base, offset);
347 	uint32_t ret = *((volatile uint32_t *)(reg_addr));
348 	return rte_le_to_cpu_32(ret);
349 }
350 
351 #ifdef RTE_LIBRTE_BBDEV_DEBUG
352 /* Read a register of FPGA LTE FEC device */
353 static uint8_t
354 fpga_reg_read_8(void *mmio_base, uint32_t offset)
355 {
356 	void *reg_addr = RTE_PTR_ADD(mmio_base, offset);
357 	return *((volatile uint8_t *)(reg_addr));
358 }
359 
360 /* Read a register of FPGA LTE FEC device */
361 static uint16_t
362 fpga_reg_read_16(void *mmio_base, uint32_t offset)
363 {
364 	void *reg_addr = RTE_PTR_ADD(mmio_base, offset);
365 	uint16_t ret = *((volatile uint16_t *)(reg_addr));
366 	return rte_le_to_cpu_16(ret);
367 }
368 
369 /* Read a register of FPGA LTE FEC device */
370 static uint64_t
371 fpga_reg_read_64(void *mmio_base, uint32_t offset)
372 {
373 	void *reg_addr = RTE_PTR_ADD(mmio_base, offset);
374 	uint64_t ret = *((volatile uint64_t *)(reg_addr));
375 	return rte_le_to_cpu_64(ret);
376 }
377 
378 /* Read Ring Control Register of FPGA LTE FEC device */
379 static inline void
380 print_ring_reg_debug_info(void *mmio_base, uint32_t offset)
381 {
382 	rte_bbdev_log_debug(
383 		"FPGA MMIO base address @ %p | Ring Control Register @ offset = 0x%08"
384 		PRIx32, mmio_base, offset);
385 	rte_bbdev_log_debug(
386 		"RING_BASE_ADDR = 0x%016"PRIx64,
387 		fpga_reg_read_64(mmio_base, offset));
388 	rte_bbdev_log_debug(
389 		"RING_HEAD_ADDR = 0x%016"PRIx64,
390 		fpga_reg_read_64(mmio_base, offset +
391 				FPGA_LTE_FEC_RING_HEAD_ADDR));
392 	rte_bbdev_log_debug(
393 		"RING_SIZE = 0x%04"PRIx16,
394 		fpga_reg_read_16(mmio_base, offset +
395 				FPGA_LTE_FEC_RING_SIZE));
396 	rte_bbdev_log_debug(
397 		"RING_MISC = 0x%02"PRIx8,
398 		fpga_reg_read_8(mmio_base, offset +
399 				FPGA_LTE_FEC_RING_MISC));
400 	rte_bbdev_log_debug(
401 		"RING_ENABLE = 0x%02"PRIx8,
402 		fpga_reg_read_8(mmio_base, offset +
403 				FPGA_LTE_FEC_RING_ENABLE));
404 	rte_bbdev_log_debug(
405 		"RING_FLUSH_QUEUE_EN = 0x%02"PRIx8,
406 		fpga_reg_read_8(mmio_base, offset +
407 				FPGA_LTE_FEC_RING_FLUSH_QUEUE_EN));
408 	rte_bbdev_log_debug(
409 		"RING_SHADOW_TAIL = 0x%04"PRIx16,
410 		fpga_reg_read_16(mmio_base, offset +
411 				FPGA_LTE_FEC_RING_SHADOW_TAIL));
412 	rte_bbdev_log_debug(
413 		"RING_HEAD_POINT = 0x%04"PRIx16,
414 		fpga_reg_read_16(mmio_base, offset +
415 				FPGA_LTE_FEC_RING_HEAD_POINT));
416 }
417 
418 /* Read Static Register of FPGA LTE FEC device */
419 static inline void
420 print_static_reg_debug_info(void *mmio_base)
421 {
422 	uint16_t config = fpga_reg_read_16(mmio_base,
423 			FPGA_LTE_FEC_CONFIGURATION);
424 	uint8_t qmap_done = fpga_reg_read_8(mmio_base,
425 			FPGA_LTE_FEC_QUEUE_PF_VF_MAP_DONE);
426 	uint16_t lb_factor = fpga_reg_read_16(mmio_base,
427 			FPGA_LTE_FEC_LOAD_BALANCE_FACTOR);
428 	uint16_t ring_desc_len = fpga_reg_read_16(mmio_base,
429 			FPGA_LTE_FEC_RING_DESC_LEN);
430 	uint16_t flr_time_out = fpga_reg_read_16(mmio_base,
431 			FPGA_LTE_FEC_FLR_TIME_OUT);
432 
433 	rte_bbdev_log_debug("UL.DL Weights = %u.%u",
434 			((uint8_t)config), ((uint8_t)(config >> 8)));
435 	rte_bbdev_log_debug("UL.DL Load Balance = %u.%u",
436 			((uint8_t)lb_factor), ((uint8_t)(lb_factor >> 8)));
437 	rte_bbdev_log_debug("Queue-PF/VF Mapping Table = %s",
438 			(qmap_done > 0) ? "READY" : "NOT-READY");
439 	rte_bbdev_log_debug("Ring Descriptor Size = %u bytes",
440 			ring_desc_len*FPGA_RING_DESC_LEN_UNIT_BYTES);
441 	rte_bbdev_log_debug("FLR Timeout = %f usec",
442 			(float)flr_time_out*FPGA_FLR_TIMEOUT_UNIT);
443 }
444 
445 /* Print decode DMA Descriptor of FPGA LTE FEC device */
446 static void
447 print_dma_dec_desc_debug_info(union fpga_dma_desc *desc)
448 {
449 	rte_bbdev_log_debug("DMA response desc %p\n"
450 		"\t-- done(%"PRIu32") | iter(%"PRIu32") | crc_pass(%"PRIu32")"
451 		" | error (%"PRIu32") | crc_type(%"PRIu32")\n"
452 		"\t-- max_iter(%"PRIu32") | bypass_rm(%"PRIu32") | "
453 		"irq_en (%"PRIu32") | drop_crc(%"PRIu32") | offset(%"PRIu32")\n"
454 		"\t-- k(%"PRIu32") | in_len (%"PRIu16") | op_add(%p)\n"
455 		"\t-- cbs_in_op(%"PRIu32") | in_add (0x%08"PRIx32"%08"PRIx32") | "
456 		"out_add (0x%08"PRIx32"%08"PRIx32")",
457 		desc,
458 		(uint32_t)desc->dec_req.done,
459 		(uint32_t)desc->dec_req.iter,
460 		(uint32_t)desc->dec_req.crc_pass,
461 		(uint32_t)desc->dec_req.error,
462 		(uint32_t)desc->dec_req.crc_type,
463 		(uint32_t)desc->dec_req.max_iter,
464 		(uint32_t)desc->dec_req.bypass_rm,
465 		(uint32_t)desc->dec_req.irq_en,
466 		(uint32_t)desc->dec_req.drop_crc,
467 		(uint32_t)desc->dec_req.offset,
468 		(uint32_t)desc->dec_req.k,
469 		(uint16_t)desc->dec_req.in_len,
470 		desc->dec_req.op_addr,
471 		(uint32_t)desc->dec_req.cbs_in_op,
472 		(uint32_t)desc->dec_req.in_addr_hi,
473 		(uint32_t)desc->dec_req.in_addr_lw,
474 		(uint32_t)desc->dec_req.out_addr_hi,
475 		(uint32_t)desc->dec_req.out_addr_lw);
476 }
477 #endif
478 
479 static int
480 fpga_setup_queues(struct rte_bbdev *dev, uint16_t num_queues, int socket_id)
481 {
482 	/* Number of queues bound to a PF/VF */
483 	uint32_t hw_q_num = 0;
484 	uint32_t ring_size, payload, address, q_id, offset;
485 	rte_iova_t phys_addr;
486 	struct fpga_ring_ctrl_reg ring_reg;
487 	struct fpga_lte_fec_device *fpga_dev = dev->data->dev_private;
488 
489 	address = FPGA_LTE_FEC_QUEUE_PF_VF_MAP_DONE;
490 	if (!(fpga_reg_read_32(fpga_dev->mmio_base, address) & 0x1)) {
491 		rte_bbdev_log(ERR,
492 				"Queue-PF/VF mapping is not set! Was PF configured for device (%s) ?",
493 				dev->data->name);
494 		return -EPERM;
495 	}
496 
497 	/* Clear queue registers structure */
498 	memset(&ring_reg, 0, sizeof(struct fpga_ring_ctrl_reg));
499 
500 	/* Scan queue map.
501 	 * If a queue is valid and mapped to a calling PF/VF the read value is
502 	 * replaced with a queue ID and if it's not then
503 	 * FPGA_INVALID_HW_QUEUE_ID is returned.
504 	 */
505 	for (q_id = 0; q_id < FPGA_TOTAL_NUM_QUEUES; ++q_id) {
506 		uint32_t hw_q_id = fpga_reg_read_32(fpga_dev->mmio_base,
507 				FPGA_LTE_FEC_QUEUE_MAP + (q_id << 2));
508 
509 		rte_bbdev_log_debug("%s: queue ID: %u, registry queue ID: %u",
510 				dev->device->name, q_id, hw_q_id);
511 
512 		if (hw_q_id != FPGA_INVALID_HW_QUEUE_ID) {
513 			fpga_dev->q_bound_bit_map |= (1ULL << q_id);
514 			/* Clear queue register of found queue */
515 			offset = FPGA_LTE_FEC_RING_CTRL_REGS +
516 				(sizeof(struct fpga_ring_ctrl_reg) * q_id);
517 			fpga_ring_reg_write(fpga_dev->mmio_base,
518 					offset, ring_reg);
519 			++hw_q_num;
520 		}
521 	}
522 	if (hw_q_num == 0) {
523 		rte_bbdev_log(ERR,
524 			"No HW queues assigned to this device. Probably this is a VF configured for PF mode. Check device configuration!");
525 		return -ENODEV;
526 	}
527 
528 	if (num_queues > hw_q_num) {
529 		rte_bbdev_log(ERR,
530 			"Not enough queues for device %s! Requested: %u, available: %u",
531 			dev->device->name, num_queues, hw_q_num);
532 		return -EINVAL;
533 	}
534 
535 	ring_size = FPGA_RING_MAX_SIZE * sizeof(struct fpga_dma_dec_desc);
536 
537 	/* Enforce 32 byte alignment */
538 	RTE_BUILD_BUG_ON((RTE_CACHE_LINE_SIZE % 32) != 0);
539 
540 	/* Allocate memory for SW descriptor rings */
541 	fpga_dev->sw_rings = rte_zmalloc_socket(dev->device->driver->name,
542 			num_queues * ring_size, RTE_CACHE_LINE_SIZE,
543 			socket_id);
544 	if (fpga_dev->sw_rings == NULL) {
545 		rte_bbdev_log(ERR,
546 				"Failed to allocate memory for %s:%u sw_rings",
547 				dev->device->driver->name, dev->data->dev_id);
548 		return -ENOMEM;
549 	}
550 
551 	fpga_dev->sw_rings_phys = rte_malloc_virt2iova(fpga_dev->sw_rings);
552 	fpga_dev->sw_ring_size = ring_size;
553 	fpga_dev->sw_ring_max_depth = FPGA_RING_MAX_SIZE;
554 
555 	/* Allocate memory for ring flush status */
556 	fpga_dev->flush_queue_status = rte_zmalloc_socket(NULL,
557 			sizeof(uint64_t), RTE_CACHE_LINE_SIZE, socket_id);
558 	if (fpga_dev->flush_queue_status == NULL) {
559 		rte_bbdev_log(ERR,
560 				"Failed to allocate memory for %s:%u flush_queue_status",
561 				dev->device->driver->name, dev->data->dev_id);
562 		return -ENOMEM;
563 	}
564 
565 	/* Set the flush status address registers */
566 	phys_addr = rte_malloc_virt2iova(fpga_dev->flush_queue_status);
567 
568 	address = FPGA_LTE_FEC_VFQ_FLUSH_STATUS_LW;
569 	payload = (uint32_t)(phys_addr);
570 	fpga_reg_write_32(fpga_dev->mmio_base, address, payload);
571 
572 	address = FPGA_LTE_FEC_VFQ_FLUSH_STATUS_HI;
573 	payload = (uint32_t)(phys_addr >> 32);
574 	fpga_reg_write_32(fpga_dev->mmio_base, address, payload);
575 
576 	return 0;
577 }
578 
579 static int
580 fpga_dev_close(struct rte_bbdev *dev)
581 {
582 	struct fpga_lte_fec_device *fpga_dev = dev->data->dev_private;
583 
584 	rte_free(fpga_dev->sw_rings);
585 	rte_free(fpga_dev->flush_queue_status);
586 
587 	return 0;
588 }
589 
590 static void
591 fpga_dev_info_get(struct rte_bbdev *dev,
592 		struct rte_bbdev_driver_info *dev_info)
593 {
594 	struct fpga_lte_fec_device *d = dev->data->dev_private;
595 	uint32_t q_id = 0;
596 
597 	/* TODO RTE_BBDEV_TURBO_NEG_LLR_1_BIT_IN and numbers of buffers are set
598 	 * to temporary values as they are required by test application while
599 	 * validation phase.
600 	 */
601 	static const struct rte_bbdev_op_cap bbdev_capabilities[] = {
602 		{
603 			.type = RTE_BBDEV_OP_TURBO_DEC,
604 			.cap.turbo_dec = {
605 				.capability_flags =
606 					RTE_BBDEV_TURBO_CRC_TYPE_24B |
607 					RTE_BBDEV_TURBO_SUBBLOCK_DEINTERLEAVE |
608 					RTE_BBDEV_TURBO_DEC_INTERRUPTS |
609 					RTE_BBDEV_TURBO_NEG_LLR_1_BIT_IN |
610 					RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP,
611 				.max_llr_modulus = INT8_MAX,
612 				.num_buffers_src =
613 						RTE_BBDEV_TURBO_MAX_CODE_BLOCKS,
614 				.num_buffers_hard_out =
615 					RTE_BBDEV_TURBO_MAX_CODE_BLOCKS,
616 				.num_buffers_soft_out = 0
617 			}
618 		},
619 		{
620 			.type = RTE_BBDEV_OP_TURBO_ENC,
621 			.cap.turbo_enc = {
622 				.capability_flags =
623 					RTE_BBDEV_TURBO_CRC_24B_ATTACH |
624 					RTE_BBDEV_TURBO_RATE_MATCH |
625 					RTE_BBDEV_TURBO_ENC_INTERRUPTS,
626 				.num_buffers_src =
627 						RTE_BBDEV_TURBO_MAX_CODE_BLOCKS,
628 				.num_buffers_dst =
629 						RTE_BBDEV_TURBO_MAX_CODE_BLOCKS
630 			}
631 		},
632 		RTE_BBDEV_END_OF_CAPABILITIES_LIST()
633 	};
634 
635 	static struct rte_bbdev_queue_conf default_queue_conf;
636 	default_queue_conf.socket = dev->data->socket_id;
637 	default_queue_conf.queue_size = FPGA_RING_MAX_SIZE;
638 
639 
640 	dev_info->driver_name = dev->device->driver->name;
641 	dev_info->queue_size_lim = FPGA_RING_MAX_SIZE;
642 	dev_info->hardware_accelerated = true;
643 	dev_info->min_alignment = 64;
644 	dev_info->default_queue_conf = default_queue_conf;
645 	dev_info->capabilities = bbdev_capabilities;
646 	dev_info->cpu_flag_reqs = NULL;
647 	dev_info->data_endianness = RTE_LITTLE_ENDIAN;
648 	dev_info->device_status = RTE_BBDEV_DEV_NOT_SUPPORTED;
649 
650 	/* Calculates number of queues assigned to device */
651 	dev_info->max_num_queues = 0;
652 	for (q_id = 0; q_id < FPGA_TOTAL_NUM_QUEUES; ++q_id) {
653 		uint32_t hw_q_id = fpga_reg_read_32(d->mmio_base,
654 				FPGA_LTE_FEC_QUEUE_MAP + (q_id << 2));
655 		if (hw_q_id != FPGA_INVALID_HW_QUEUE_ID)
656 			dev_info->max_num_queues++;
657 	}
658 }
659 
660 /**
661  * Find index of queue bound to current PF/VF which is unassigned. Return -1
662  * when there is no available queue
663  */
664 static int
665 fpga_find_free_queue_idx(struct rte_bbdev *dev,
666 		const struct rte_bbdev_queue_conf *conf)
667 {
668 	struct fpga_lte_fec_device *d = dev->data->dev_private;
669 	uint64_t q_idx;
670 	uint8_t i = 0;
671 	uint8_t range = FPGA_TOTAL_NUM_QUEUES >> 1;
672 
673 	if (conf->op_type == RTE_BBDEV_OP_TURBO_ENC) {
674 		i = FPGA_NUM_DL_QUEUES;
675 		range = FPGA_TOTAL_NUM_QUEUES;
676 	}
677 
678 	for (; i < range; ++i) {
679 		q_idx = 1ULL << i;
680 		/* Check if index of queue is bound to current PF/VF */
681 		if (d->q_bound_bit_map & q_idx)
682 			/* Check if found queue was not already assigned */
683 			if (!(d->q_assigned_bit_map & q_idx)) {
684 				d->q_assigned_bit_map |= q_idx;
685 				return i;
686 			}
687 	}
688 
689 	rte_bbdev_log(INFO, "Failed to find free queue on %s", dev->data->name);
690 
691 	return -1;
692 }
693 
694 static int
695 fpga_queue_setup(struct rte_bbdev *dev, uint16_t queue_id,
696 		const struct rte_bbdev_queue_conf *conf)
697 {
698 	uint32_t address, ring_offset;
699 	struct fpga_lte_fec_device *d = dev->data->dev_private;
700 	struct fpga_queue *q;
701 	int8_t q_idx;
702 
703 	/* Check if there is a free queue to assign */
704 	q_idx = fpga_find_free_queue_idx(dev, conf);
705 	if (q_idx == -1)
706 		return -1;
707 
708 	/* Allocate the queue data structure. */
709 	q = rte_zmalloc_socket(dev->device->driver->name, sizeof(*q),
710 			RTE_CACHE_LINE_SIZE, conf->socket);
711 	if (q == NULL) {
712 		/* Mark queue as un-assigned */
713 		d->q_assigned_bit_map &= (0xFFFFFFFF - (1ULL << q_idx));
714 		rte_bbdev_log(ERR, "Failed to allocate queue memory");
715 		return -ENOMEM;
716 	}
717 
718 	q->d = d;
719 	q->q_idx = q_idx;
720 
721 	/* Set ring_base_addr */
722 	q->ring_addr = RTE_PTR_ADD(d->sw_rings, (d->sw_ring_size * queue_id));
723 	q->ring_ctrl_reg.ring_base_addr = d->sw_rings_phys +
724 			(d->sw_ring_size * queue_id);
725 
726 	/* Allocate memory for Completion Head variable*/
727 	q->ring_head_addr = rte_zmalloc_socket(dev->device->driver->name,
728 			sizeof(uint64_t), RTE_CACHE_LINE_SIZE, conf->socket);
729 	if (q->ring_head_addr == NULL) {
730 		/* Mark queue as un-assigned */
731 		d->q_assigned_bit_map &= (0xFFFFFFFF - (1ULL << q_idx));
732 		rte_free(q);
733 		rte_bbdev_log(ERR,
734 				"Failed to allocate memory for %s:%u completion_head",
735 				dev->device->driver->name, dev->data->dev_id);
736 		return -ENOMEM;
737 	}
738 	/* Set ring_head_addr */
739 	q->ring_ctrl_reg.ring_head_addr =
740 			rte_malloc_virt2iova(q->ring_head_addr);
741 
742 	/* Clear shadow_completion_head */
743 	q->shadow_completion_head = 0;
744 
745 	/* Set ring_size */
746 	if (conf->queue_size > FPGA_RING_MAX_SIZE) {
747 		/* Mark queue as un-assigned */
748 		d->q_assigned_bit_map &= (0xFFFFFFFF - (1ULL << q_idx));
749 		rte_free(q->ring_head_addr);
750 		rte_free(q);
751 		rte_bbdev_log(ERR,
752 				"Size of queue is too big %d (MAX: %d ) for %s:%u",
753 				conf->queue_size, FPGA_RING_MAX_SIZE,
754 				dev->device->driver->name, dev->data->dev_id);
755 		return -EINVAL;
756 	}
757 	q->ring_ctrl_reg.ring_size = conf->queue_size;
758 
759 	/* Set Miscellaneous FPGA register*/
760 	/* Max iteration number for TTI mitigation - todo */
761 	q->ring_ctrl_reg.max_ul_dec = 0;
762 	/* Enable max iteration number for TTI - todo */
763 	q->ring_ctrl_reg.max_ul_dec_en = 0;
764 
765 	/* Enable the ring */
766 	q->ring_ctrl_reg.enable = 1;
767 
768 	/* Set FPGA head_point and tail registers */
769 	q->ring_ctrl_reg.head_point = q->tail = 0;
770 
771 	/* Set FPGA shadow_tail register */
772 	q->ring_ctrl_reg.shadow_tail = q->tail;
773 
774 	/* Calculates the ring offset for found queue */
775 	ring_offset = FPGA_LTE_FEC_RING_CTRL_REGS +
776 			(sizeof(struct fpga_ring_ctrl_reg) * q_idx);
777 
778 	/* Set FPGA Ring Control Registers */
779 	fpga_ring_reg_write(d->mmio_base, ring_offset, q->ring_ctrl_reg);
780 
781 	/* Store MMIO register of shadow_tail */
782 	address = ring_offset + FPGA_LTE_FEC_RING_SHADOW_TAIL;
783 	q->shadow_tail_addr = RTE_PTR_ADD(d->mmio_base, address);
784 
785 	q->head_free_desc = q->tail;
786 
787 	/* Set wrap mask */
788 	q->sw_ring_wrap_mask = conf->queue_size - 1;
789 
790 	rte_bbdev_log_debug("Setup dev%u q%u: queue_idx=%u",
791 			dev->data->dev_id, queue_id, q->q_idx);
792 
793 	dev->data->queues[queue_id].queue_private = q;
794 
795 	rte_bbdev_log_debug("BBDEV queue[%d] set up for FPGA queue[%d]",
796 			queue_id, q_idx);
797 
798 #ifdef RTE_LIBRTE_BBDEV_DEBUG
799 	/* Read FPGA Ring Control Registers after configuration*/
800 	print_ring_reg_debug_info(d->mmio_base, ring_offset);
801 #endif
802 	return 0;
803 }
804 
805 static int
806 fpga_queue_release(struct rte_bbdev *dev, uint16_t queue_id)
807 {
808 	struct fpga_lte_fec_device *d = dev->data->dev_private;
809 	struct fpga_queue *q = dev->data->queues[queue_id].queue_private;
810 	struct fpga_ring_ctrl_reg ring_reg;
811 	uint32_t offset;
812 
813 	rte_bbdev_log_debug("FPGA Queue[%d] released", queue_id);
814 
815 	if (q != NULL) {
816 		memset(&ring_reg, 0, sizeof(struct fpga_ring_ctrl_reg));
817 		offset = FPGA_LTE_FEC_RING_CTRL_REGS +
818 			(sizeof(struct fpga_ring_ctrl_reg) * q->q_idx);
819 		/* Disable queue */
820 		fpga_reg_write_8(d->mmio_base,
821 				offset + FPGA_LTE_FEC_RING_ENABLE, 0x00);
822 		/* Clear queue registers */
823 		fpga_ring_reg_write(d->mmio_base, offset, ring_reg);
824 
825 		/* Mark the Queue as un-assigned */
826 		d->q_assigned_bit_map &= (0xFFFFFFFF - (1ULL << q->q_idx));
827 		rte_free(q->ring_head_addr);
828 		rte_free(q);
829 		dev->data->queues[queue_id].queue_private = NULL;
830 	}
831 
832 	return 0;
833 }
834 
835 /* Function starts a device queue. */
836 static int
837 fpga_queue_start(struct rte_bbdev *dev, uint16_t queue_id)
838 {
839 	struct fpga_lte_fec_device *d = dev->data->dev_private;
840 #ifdef RTE_LIBRTE_BBDEV_DEBUG
841 	if (d == NULL) {
842 		rte_bbdev_log(ERR, "Invalid device pointer");
843 		return -1;
844 	}
845 #endif
846 	struct fpga_queue *q = dev->data->queues[queue_id].queue_private;
847 	uint32_t offset = FPGA_LTE_FEC_RING_CTRL_REGS +
848 			(sizeof(struct fpga_ring_ctrl_reg) * q->q_idx);
849 	uint8_t enable = 0x01;
850 	uint16_t zero = 0x0000;
851 
852 	/* Clear queue head and tail variables */
853 	q->tail = q->head_free_desc = 0;
854 
855 	/* Clear FPGA head_point and tail registers */
856 	fpga_reg_write_16(d->mmio_base, offset + FPGA_LTE_FEC_RING_HEAD_POINT,
857 			zero);
858 	fpga_reg_write_16(d->mmio_base, offset + FPGA_LTE_FEC_RING_SHADOW_TAIL,
859 			zero);
860 
861 	/* Enable queue */
862 	fpga_reg_write_8(d->mmio_base, offset + FPGA_LTE_FEC_RING_ENABLE,
863 			enable);
864 
865 	rte_bbdev_log_debug("FPGA Queue[%d] started", queue_id);
866 	return 0;
867 }
868 
869 /* Function stops a device queue. */
870 static int
871 fpga_queue_stop(struct rte_bbdev *dev, uint16_t queue_id)
872 {
873 	struct fpga_lte_fec_device *d = dev->data->dev_private;
874 #ifdef RTE_LIBRTE_BBDEV_DEBUG
875 	if (d == NULL) {
876 		rte_bbdev_log(ERR, "Invalid device pointer");
877 		return -1;
878 	}
879 #endif
880 	struct fpga_queue *q = dev->data->queues[queue_id].queue_private;
881 	uint32_t offset = FPGA_LTE_FEC_RING_CTRL_REGS +
882 			(sizeof(struct fpga_ring_ctrl_reg) * q->q_idx);
883 	uint8_t payload = 0x01;
884 	uint8_t counter = 0;
885 	uint8_t timeout = FPGA_QUEUE_FLUSH_TIMEOUT_US /
886 			FPGA_TIMEOUT_CHECK_INTERVAL;
887 
888 	/* Set flush_queue_en bit to trigger queue flushing */
889 	fpga_reg_write_8(d->mmio_base,
890 			offset + FPGA_LTE_FEC_RING_FLUSH_QUEUE_EN, payload);
891 
892 	/** Check if queue flush is completed.
893 	 * FPGA will update the completion flag after queue flushing is
894 	 * completed. If completion flag is not updated within 1ms it is
895 	 * considered as a failure.
896 	 */
897 	while (!(*((volatile uint8_t *)d->flush_queue_status + q->q_idx) & payload)) {
898 		if (counter > timeout) {
899 			rte_bbdev_log(ERR, "FPGA Queue Flush failed for queue %d",
900 					queue_id);
901 			return -1;
902 		}
903 		usleep(FPGA_TIMEOUT_CHECK_INTERVAL);
904 		counter++;
905 	}
906 
907 	/* Disable queue */
908 	payload = 0x00;
909 	fpga_reg_write_8(d->mmio_base, offset + FPGA_LTE_FEC_RING_ENABLE,
910 			payload);
911 
912 	rte_bbdev_log_debug("FPGA Queue[%d] stopped", queue_id);
913 	return 0;
914 }
915 
916 static inline uint16_t
917 get_queue_id(struct rte_bbdev_data *data, uint8_t q_idx)
918 {
919 	uint16_t queue_id;
920 
921 	for (queue_id = 0; queue_id < data->num_queues; ++queue_id) {
922 		struct fpga_queue *q = data->queues[queue_id].queue_private;
923 		if (q != NULL && q->q_idx == q_idx)
924 			return queue_id;
925 	}
926 
927 	return -1;
928 }
929 
930 /* Interrupt handler triggered by FPGA dev for handling specific interrupt */
931 static void
932 fpga_dev_interrupt_handler(void *cb_arg)
933 {
934 	struct rte_bbdev *dev = cb_arg;
935 	struct fpga_lte_fec_device *fpga_dev = dev->data->dev_private;
936 	struct fpga_queue *q;
937 	uint64_t ring_head;
938 	uint64_t q_idx;
939 	uint16_t queue_id;
940 	uint8_t i;
941 
942 	/* Scan queue assigned to this device */
943 	for (i = 0; i < FPGA_TOTAL_NUM_QUEUES; ++i) {
944 		q_idx = 1ULL << i;
945 		if (fpga_dev->q_bound_bit_map & q_idx) {
946 			queue_id = get_queue_id(dev->data, i);
947 			if (queue_id == (uint16_t) -1)
948 				continue;
949 
950 			/* Check if completion head was changed */
951 			q = dev->data->queues[queue_id].queue_private;
952 			ring_head = *q->ring_head_addr;
953 			if (q->shadow_completion_head != ring_head &&
954 				q->irq_enable == 1) {
955 				q->shadow_completion_head = ring_head;
956 				rte_bbdev_pmd_callback_process(
957 						dev,
958 						RTE_BBDEV_EVENT_DEQUEUE,
959 						&queue_id);
960 			}
961 		}
962 	}
963 }
964 
965 static int
966 fpga_queue_intr_enable(struct rte_bbdev *dev, uint16_t queue_id)
967 {
968 	struct fpga_queue *q = dev->data->queues[queue_id].queue_private;
969 
970 	if (!rte_intr_cap_multiple(dev->intr_handle))
971 		return -ENOTSUP;
972 
973 	q->irq_enable = 1;
974 
975 	return 0;
976 }
977 
978 static int
979 fpga_queue_intr_disable(struct rte_bbdev *dev, uint16_t queue_id)
980 {
981 	struct fpga_queue *q = dev->data->queues[queue_id].queue_private;
982 	q->irq_enable = 0;
983 
984 	return 0;
985 }
986 
987 static int
988 fpga_intr_enable(struct rte_bbdev *dev)
989 {
990 	int ret;
991 	uint8_t i;
992 
993 	if (!rte_intr_cap_multiple(dev->intr_handle)) {
994 		rte_bbdev_log(ERR, "Multiple intr vector is not supported by FPGA (%s)",
995 				dev->data->name);
996 		return -ENOTSUP;
997 	}
998 
999 	/* Create event file descriptors for each of 64 queue. Event fds will be
1000 	 * mapped to FPGA IRQs in rte_intr_enable(). This is a 1:1 mapping where
1001 	 * the IRQ number is a direct translation to the queue number.
1002 	 *
1003 	 * 63 (FPGA_NUM_INTR_VEC) event fds are created as rte_intr_enable()
1004 	 * mapped the first IRQ to already created interrupt event file
1005 	 * descriptor (intr_handle->fd).
1006 	 */
1007 	if (rte_intr_efd_enable(dev->intr_handle, FPGA_NUM_INTR_VEC)) {
1008 		rte_bbdev_log(ERR, "Failed to create fds for %u queues",
1009 				dev->data->num_queues);
1010 		return -1;
1011 	}
1012 
1013 	/* TODO Each event file descriptor is overwritten by interrupt event
1014 	 * file descriptor. That descriptor is added to epoll observed list.
1015 	 * It ensures that callback function assigned to that descriptor will
1016 	 * invoked when any FPGA queue issues interrupt.
1017 	 */
1018 	for (i = 0; i < FPGA_NUM_INTR_VEC; ++i) {
1019 		if (rte_intr_efds_index_set(dev->intr_handle, i,
1020 				rte_intr_fd_get(dev->intr_handle)))
1021 			return -rte_errno;
1022 	}
1023 
1024 	if (rte_intr_vec_list_alloc(dev->intr_handle, "intr_vec",
1025 			dev->data->num_queues)) {
1026 		rte_bbdev_log(ERR, "Failed to allocate %u vectors",
1027 				dev->data->num_queues);
1028 		return -ENOMEM;
1029 	}
1030 
1031 	ret = rte_intr_enable(dev->intr_handle);
1032 	if (ret < 0) {
1033 		rte_bbdev_log(ERR,
1034 				"Couldn't enable interrupts for device: %s",
1035 				dev->data->name);
1036 		return ret;
1037 	}
1038 
1039 	ret = rte_intr_callback_register(dev->intr_handle,
1040 			fpga_dev_interrupt_handler, dev);
1041 	if (ret < 0) {
1042 		rte_bbdev_log(ERR,
1043 				"Couldn't register interrupt callback for device: %s",
1044 				dev->data->name);
1045 		return ret;
1046 	}
1047 
1048 	return 0;
1049 }
1050 
1051 static const struct rte_bbdev_ops fpga_ops = {
1052 	.setup_queues = fpga_setup_queues,
1053 	.intr_enable = fpga_intr_enable,
1054 	.close = fpga_dev_close,
1055 	.info_get = fpga_dev_info_get,
1056 	.queue_setup = fpga_queue_setup,
1057 	.queue_stop = fpga_queue_stop,
1058 	.queue_start = fpga_queue_start,
1059 	.queue_release = fpga_queue_release,
1060 	.queue_intr_enable = fpga_queue_intr_enable,
1061 	.queue_intr_disable = fpga_queue_intr_disable
1062 };
1063 
1064 static inline void
1065 fpga_dma_enqueue(struct fpga_queue *q, uint16_t num_desc,
1066 		struct rte_bbdev_stats *queue_stats)
1067 {
1068 #ifdef RTE_BBDEV_OFFLOAD_COST
1069 	uint64_t start_time = 0;
1070 	queue_stats->acc_offload_cycles = 0;
1071 #else
1072 	RTE_SET_USED(queue_stats);
1073 #endif
1074 
1075 	/* Update tail and shadow_tail register */
1076 	q->tail = (q->tail + num_desc) & q->sw_ring_wrap_mask;
1077 
1078 	rte_wmb();
1079 
1080 #ifdef RTE_BBDEV_OFFLOAD_COST
1081 	/* Start time measurement for enqueue function offload. */
1082 	start_time = rte_rdtsc_precise();
1083 #endif
1084 	mmio_write_16(q->shadow_tail_addr, q->tail);
1085 
1086 #ifdef RTE_BBDEV_OFFLOAD_COST
1087 	rte_wmb();
1088 	queue_stats->acc_offload_cycles += rte_rdtsc_precise() - start_time;
1089 #endif
1090 }
1091 
1092 /* Calculates number of CBs in processed encoder TB based on 'r' and input
1093  * length.
1094  */
1095 static inline uint8_t
1096 get_num_cbs_in_op_enc(struct rte_bbdev_op_turbo_enc *turbo_enc)
1097 {
1098 	uint8_t c, c_neg, r, crc24_bits = 0;
1099 	uint16_t k, k_neg, k_pos;
1100 	uint8_t cbs_in_op = 0;
1101 	int32_t length;
1102 
1103 	length = turbo_enc->input.length;
1104 	r = turbo_enc->tb_params.r;
1105 	c = turbo_enc->tb_params.c;
1106 	c_neg = turbo_enc->tb_params.c_neg;
1107 	k_neg = turbo_enc->tb_params.k_neg;
1108 	k_pos = turbo_enc->tb_params.k_pos;
1109 	crc24_bits = 24;
1110 	while (length > 0 && r < c) {
1111 		k = (r < c_neg) ? k_neg : k_pos;
1112 		length -= (k - crc24_bits) >> 3;
1113 		r++;
1114 		cbs_in_op++;
1115 	}
1116 
1117 	return cbs_in_op;
1118 }
1119 
1120 /* Calculates number of CBs in processed decoder TB based on 'r' and input
1121  * length.
1122  */
1123 static inline uint16_t
1124 get_num_cbs_in_op_dec(struct rte_bbdev_op_turbo_dec *turbo_dec)
1125 {
1126 	uint8_t c, c_neg, r = 0;
1127 	uint16_t kw, k, k_neg, k_pos, cbs_in_op = 0;
1128 	int32_t length;
1129 
1130 	length = turbo_dec->input.length;
1131 	r = turbo_dec->tb_params.r;
1132 	c = turbo_dec->tb_params.c;
1133 	c_neg = turbo_dec->tb_params.c_neg;
1134 	k_neg = turbo_dec->tb_params.k_neg;
1135 	k_pos = turbo_dec->tb_params.k_pos;
1136 	while (length > 0 && r < c) {
1137 		k = (r < c_neg) ? k_neg : k_pos;
1138 		kw = RTE_ALIGN_CEIL(k + 4, 32) * 3;
1139 		length -= kw;
1140 		r++;
1141 		cbs_in_op++;
1142 	}
1143 
1144 	return cbs_in_op;
1145 }
1146 
1147 /* Read flag value 0/1/ from bitmap */
1148 static inline bool
1149 check_bit(uint32_t bitmap, uint32_t bitmask)
1150 {
1151 	return bitmap & bitmask;
1152 }
1153 
1154 /* Print an error if a descriptor error has occurred.
1155  *  Return 0 on success, 1 on failure
1156  */
1157 static inline int
1158 check_desc_error(uint32_t error_code) {
1159 	switch (error_code) {
1160 	case DESC_ERR_NO_ERR:
1161 		return 0;
1162 	case DESC_ERR_K_OUT_OF_RANGE:
1163 		rte_bbdev_log(ERR, "Block_size_k is out of range (k<40 or k>6144)");
1164 		break;
1165 	case DESC_ERR_K_NOT_NORMAL:
1166 		rte_bbdev_log(ERR, "Block_size_k is not a normal value within normal range");
1167 		break;
1168 	case DESC_ERR_KPAI_NOT_NORMAL:
1169 		rte_bbdev_log(ERR, "Three_kpai is not a normal value for UL only");
1170 		break;
1171 	case DESC_ERR_DESC_OFFSET_ERR:
1172 		rte_bbdev_log(ERR, "Queue offset does not meet the expectation in the FPGA");
1173 		break;
1174 	case (DESC_ERR_K_OUT_OF_RANGE | DESC_ERR_DESC_OFFSET_ERR):
1175 		rte_bbdev_log(ERR, "Block_size_k is out of range (k<40 or k>6144) and queue offset error");
1176 		break;
1177 	case (DESC_ERR_K_NOT_NORMAL | DESC_ERR_DESC_OFFSET_ERR):
1178 		rte_bbdev_log(ERR, "Block_size_k is not a normal value within normal range and queue offset error");
1179 		break;
1180 	case (DESC_ERR_KPAI_NOT_NORMAL | DESC_ERR_DESC_OFFSET_ERR):
1181 		rte_bbdev_log(ERR, "Three_kpai is not a normal value for UL only and queue offset error");
1182 		break;
1183 	case DESC_ERR_DESC_READ_FAIL:
1184 		rte_bbdev_log(ERR, "Unsuccessful completion for descriptor read");
1185 		break;
1186 	case DESC_ERR_DESC_READ_TIMEOUT:
1187 		rte_bbdev_log(ERR, "Descriptor read time-out");
1188 		break;
1189 	case DESC_ERR_DESC_READ_TLP_POISONED:
1190 		rte_bbdev_log(ERR, "Descriptor read TLP poisoned");
1191 		break;
1192 	case DESC_ERR_CB_READ_FAIL:
1193 		rte_bbdev_log(ERR, "Unsuccessful completion for code block");
1194 		break;
1195 	case DESC_ERR_CB_READ_TIMEOUT:
1196 		rte_bbdev_log(ERR, "Code block read time-out");
1197 		break;
1198 	case DESC_ERR_CB_READ_TLP_POISONED:
1199 		rte_bbdev_log(ERR, "Code block read TLP poisoned");
1200 		break;
1201 	default:
1202 		rte_bbdev_log(ERR, "Descriptor error unknown error code %u",
1203 				error_code);
1204 		break;
1205 	}
1206 	return 1;
1207 }
1208 
1209 /**
1210  * Set DMA descriptor for encode operation (1 Code Block)
1211  *
1212  * @param op
1213  *   Pointer to a single encode operation.
1214  * @param desc
1215  *   Pointer to DMA descriptor.
1216  * @param input
1217  *   Pointer to pointer to input data which will be decoded.
1218  * @param k
1219  *   K value (length of input in bits).
1220  * @param e
1221  *   E value (length of output in bits).
1222  * @param ncb
1223  *   Ncb value (size of the soft buffer).
1224  * @param out_length
1225  *   Length of output buffer
1226  * @param in_offset
1227  *   Input offset in rte_mbuf structure. It is used for calculating the point
1228  *   where data is starting.
1229  * @param out_offset
1230  *   Output offset in rte_mbuf structure. It is used for calculating the point
1231  *   where hard output data will be stored.
1232  * @param cbs_in_op
1233  *   Number of CBs contained in one operation.
1234  */
1235 static inline int
1236 fpga_dma_desc_te_fill(struct rte_bbdev_enc_op *op,
1237 		struct fpga_dma_enc_desc *desc, struct rte_mbuf *input,
1238 		struct rte_mbuf *output, uint16_t k, uint16_t e, uint16_t ncb,
1239 		uint32_t in_offset, uint32_t out_offset, uint16_t desc_offset,
1240 		uint8_t cbs_in_op)
1241 
1242 {
1243 	/* reset */
1244 	desc->done = 0;
1245 	desc->crc_en = check_bit(op->turbo_enc.op_flags,
1246 		RTE_BBDEV_TURBO_CRC_24B_ATTACH);
1247 	desc->bypass_rm = !check_bit(op->turbo_enc.op_flags,
1248 		RTE_BBDEV_TURBO_RATE_MATCH);
1249 	desc->k = k;
1250 	desc->e = e;
1251 	desc->ncb = ncb;
1252 	desc->rv = op->turbo_enc.rv_index;
1253 	desc->offset = desc_offset;
1254 	/* Set inbound data buffer address */
1255 	desc->in_addr_hi = (uint32_t)(
1256 			rte_pktmbuf_iova_offset(input, in_offset) >> 32);
1257 	desc->in_addr_lw = (uint32_t)(
1258 			rte_pktmbuf_iova_offset(input, in_offset));
1259 
1260 	desc->out_addr_hi = (uint32_t)(
1261 			rte_pktmbuf_iova_offset(output, out_offset) >> 32);
1262 	desc->out_addr_lw = (uint32_t)(
1263 			rte_pktmbuf_iova_offset(output, out_offset));
1264 
1265 	/* Save software context needed for dequeue */
1266 	desc->op_addr = op;
1267 
1268 	/* Set total number of CBs in an op */
1269 	desc->cbs_in_op = cbs_in_op;
1270 
1271 	return 0;
1272 }
1273 
1274 /**
1275  * Set DMA descriptor for encode operation (1 Code Block)
1276  *
1277  * @param op
1278  *   Pointer to a single encode operation.
1279  * @param desc
1280  *   Pointer to DMA descriptor.
1281  * @param input
1282  *   Pointer to pointer to input data which will be decoded.
1283  * @param in_length
1284  *   Length of an input.
1285  * @param k
1286  *   K value (length of an output in bits).
1287  * @param in_offset
1288  *   Input offset in rte_mbuf structure. It is used for calculating the point
1289  *   where data is starting.
1290  * @param out_offset
1291  *   Output offset in rte_mbuf structure. It is used for calculating the point
1292  *   where hard output data will be stored.
1293  * @param cbs_in_op
1294  *   Number of CBs contained in one operation.
1295  */
1296 static inline int
1297 fpga_dma_desc_td_fill(struct rte_bbdev_dec_op *op,
1298 		struct fpga_dma_dec_desc *desc, struct rte_mbuf *input,
1299 		struct rte_mbuf *output, uint16_t in_length, uint16_t k,
1300 		uint32_t in_offset, uint32_t out_offset, uint16_t desc_offset,
1301 		uint8_t cbs_in_op)
1302 {
1303 	/* reset */
1304 	desc->done = 0;
1305 	/* Set inbound data buffer address */
1306 	desc->in_addr_hi = (uint32_t)(
1307 			rte_pktmbuf_iova_offset(input, in_offset) >> 32);
1308 	desc->in_addr_lw = (uint32_t)(
1309 			rte_pktmbuf_iova_offset(input, in_offset));
1310 	desc->in_len = in_length;
1311 	desc->k = k;
1312 	desc->crc_type = !check_bit(op->turbo_dec.op_flags,
1313 			RTE_BBDEV_TURBO_CRC_TYPE_24B);
1314 	if ((op->turbo_dec.code_block_mode == RTE_BBDEV_TRANSPORT_BLOCK)
1315 		&& !check_bit(op->turbo_dec.op_flags,
1316 		RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP))
1317 		desc->drop_crc = 1;
1318 	desc->max_iter = op->turbo_dec.iter_max * 2;
1319 	desc->offset = desc_offset;
1320 	desc->out_addr_hi = (uint32_t)(
1321 			rte_pktmbuf_iova_offset(output, out_offset) >> 32);
1322 	desc->out_addr_lw = (uint32_t)(
1323 			rte_pktmbuf_iova_offset(output, out_offset));
1324 
1325 	/* Save software context needed for dequeue */
1326 	desc->op_addr = op;
1327 
1328 	/* Set total number of CBs in an op */
1329 	desc->cbs_in_op = cbs_in_op;
1330 
1331 	return 0;
1332 }
1333 
1334 #ifdef RTE_LIBRTE_BBDEV_DEBUG
1335 /* Validates turbo encoder parameters */
1336 static int
1337 validate_enc_op(struct rte_bbdev_enc_op *op)
1338 {
1339 	struct rte_bbdev_op_turbo_enc *turbo_enc = &op->turbo_enc;
1340 	struct rte_bbdev_op_enc_turbo_cb_params *cb = NULL;
1341 	struct rte_bbdev_op_enc_turbo_tb_params *tb = NULL;
1342 	uint16_t kw, kw_neg, kw_pos;
1343 
1344 	if (turbo_enc->input.length >
1345 			RTE_BBDEV_TURBO_MAX_TB_SIZE >> 3) {
1346 		rte_bbdev_log(ERR, "TB size (%u) is too big, max: %d",
1347 				turbo_enc->input.length,
1348 				RTE_BBDEV_TURBO_MAX_TB_SIZE);
1349 		op->status = 1 << RTE_BBDEV_DATA_ERROR;
1350 		return -1;
1351 	}
1352 
1353 	if (op->mempool == NULL) {
1354 		rte_bbdev_log(ERR, "Invalid mempool pointer");
1355 		return -1;
1356 	}
1357 	if (turbo_enc->input.data == NULL) {
1358 		rte_bbdev_log(ERR, "Invalid input pointer");
1359 		return -1;
1360 	}
1361 	if (turbo_enc->output.data == NULL) {
1362 		rte_bbdev_log(ERR, "Invalid output pointer");
1363 		return -1;
1364 	}
1365 	if (turbo_enc->rv_index > 3) {
1366 		rte_bbdev_log(ERR,
1367 				"rv_index (%u) is out of range 0 <= value <= 3",
1368 				turbo_enc->rv_index);
1369 		return -1;
1370 	}
1371 	if (turbo_enc->code_block_mode != RTE_BBDEV_TRANSPORT_BLOCK &&
1372 			turbo_enc->code_block_mode != RTE_BBDEV_CODE_BLOCK) {
1373 		rte_bbdev_log(ERR,
1374 				"code_block_mode (%u) is out of range 0 <= value <= 1",
1375 				turbo_enc->code_block_mode);
1376 		return -1;
1377 	}
1378 
1379 	if (turbo_enc->code_block_mode == RTE_BBDEV_TRANSPORT_BLOCK) {
1380 		tb = &turbo_enc->tb_params;
1381 		if ((tb->k_neg < RTE_BBDEV_TURBO_MIN_CB_SIZE
1382 				|| tb->k_neg > RTE_BBDEV_TURBO_MAX_CB_SIZE)
1383 				&& tb->c_neg > 0) {
1384 			rte_bbdev_log(ERR,
1385 					"k_neg (%u) is out of range %u <= value <= %u",
1386 					tb->k_neg, RTE_BBDEV_TURBO_MIN_CB_SIZE,
1387 					RTE_BBDEV_TURBO_MAX_CB_SIZE);
1388 			return -1;
1389 		}
1390 		if (tb->k_pos < RTE_BBDEV_TURBO_MIN_CB_SIZE
1391 				|| tb->k_pos > RTE_BBDEV_TURBO_MAX_CB_SIZE) {
1392 			rte_bbdev_log(ERR,
1393 					"k_pos (%u) is out of range %u <= value <= %u",
1394 					tb->k_pos, RTE_BBDEV_TURBO_MIN_CB_SIZE,
1395 					RTE_BBDEV_TURBO_MAX_CB_SIZE);
1396 			return -1;
1397 		}
1398 		if (tb->c_neg > (RTE_BBDEV_TURBO_MAX_CODE_BLOCKS - 1))
1399 			rte_bbdev_log(ERR,
1400 					"c_neg (%u) is out of range 0 <= value <= %u",
1401 					tb->c_neg,
1402 					RTE_BBDEV_TURBO_MAX_CODE_BLOCKS - 1);
1403 		if (tb->c < 1 || tb->c > RTE_BBDEV_TURBO_MAX_CODE_BLOCKS) {
1404 			rte_bbdev_log(ERR,
1405 					"c (%u) is out of range 1 <= value <= %u",
1406 					tb->c, RTE_BBDEV_TURBO_MAX_CODE_BLOCKS);
1407 			return -1;
1408 		}
1409 		if (tb->cab > tb->c) {
1410 			rte_bbdev_log(ERR,
1411 					"cab (%u) is greater than c (%u)",
1412 					tb->cab, tb->c);
1413 			return -1;
1414 		}
1415 		if ((tb->ea < RTE_BBDEV_TURBO_MIN_CB_SIZE || (tb->ea % 2))
1416 				&& tb->r < tb->cab) {
1417 			rte_bbdev_log(ERR,
1418 					"ea (%u) is less than %u or it is not even",
1419 					tb->ea, RTE_BBDEV_TURBO_MIN_CB_SIZE);
1420 			return -1;
1421 		}
1422 		if ((tb->eb < RTE_BBDEV_TURBO_MIN_CB_SIZE || (tb->eb % 2))
1423 				&& tb->c > tb->cab) {
1424 			rte_bbdev_log(ERR,
1425 					"eb (%u) is less than %u or it is not even",
1426 					tb->eb, RTE_BBDEV_TURBO_MIN_CB_SIZE);
1427 			return -1;
1428 		}
1429 
1430 		kw_neg = 3 * RTE_ALIGN_CEIL(tb->k_neg + 4,
1431 					RTE_BBDEV_TURBO_C_SUBBLOCK);
1432 		if (tb->ncb_neg < tb->k_neg || tb->ncb_neg > kw_neg) {
1433 			rte_bbdev_log(ERR,
1434 					"ncb_neg (%u) is out of range (%u) k_neg <= value <= (%u) kw_neg",
1435 					tb->ncb_neg, tb->k_neg, kw_neg);
1436 			return -1;
1437 		}
1438 
1439 		kw_pos = 3 * RTE_ALIGN_CEIL(tb->k_pos + 4,
1440 					RTE_BBDEV_TURBO_C_SUBBLOCK);
1441 		if (tb->ncb_pos < tb->k_pos || tb->ncb_pos > kw_pos) {
1442 			rte_bbdev_log(ERR,
1443 					"ncb_pos (%u) is out of range (%u) k_pos <= value <= (%u) kw_pos",
1444 					tb->ncb_pos, tb->k_pos, kw_pos);
1445 			return -1;
1446 		}
1447 		if (tb->r > (tb->c - 1)) {
1448 			rte_bbdev_log(ERR,
1449 					"r (%u) is greater than c - 1 (%u)",
1450 					tb->r, tb->c - 1);
1451 			return -1;
1452 		}
1453 	} else {
1454 		cb = &turbo_enc->cb_params;
1455 		if (cb->k < RTE_BBDEV_TURBO_MIN_CB_SIZE
1456 				|| cb->k > RTE_BBDEV_TURBO_MAX_CB_SIZE) {
1457 			rte_bbdev_log(ERR,
1458 					"k (%u) is out of range %u <= value <= %u",
1459 					cb->k, RTE_BBDEV_TURBO_MIN_CB_SIZE,
1460 					RTE_BBDEV_TURBO_MAX_CB_SIZE);
1461 			return -1;
1462 		}
1463 
1464 		if (cb->e < RTE_BBDEV_TURBO_MIN_CB_SIZE || (cb->e % 2)) {
1465 			rte_bbdev_log(ERR,
1466 					"e (%u) is less than %u or it is not even",
1467 					cb->e, RTE_BBDEV_TURBO_MIN_CB_SIZE);
1468 			return -1;
1469 		}
1470 
1471 		kw = RTE_ALIGN_CEIL(cb->k + 4, RTE_BBDEV_TURBO_C_SUBBLOCK) * 3;
1472 		if (cb->ncb < cb->k || cb->ncb > kw) {
1473 			rte_bbdev_log(ERR,
1474 					"ncb (%u) is out of range (%u) k <= value <= (%u) kw",
1475 					cb->ncb, cb->k, kw);
1476 			return -1;
1477 		}
1478 	}
1479 
1480 	return 0;
1481 }
1482 #endif
1483 
1484 static inline char *
1485 mbuf_append(struct rte_mbuf *m_head, struct rte_mbuf *m, uint16_t len)
1486 {
1487 	if (unlikely(len > rte_pktmbuf_tailroom(m)))
1488 		return NULL;
1489 
1490 	char *tail = (char *)m->buf_addr + m->data_off + m->data_len;
1491 	m->data_len = (uint16_t)(m->data_len + len);
1492 	m_head->pkt_len  = (m_head->pkt_len + len);
1493 	return tail;
1494 }
1495 
1496 static inline int
1497 enqueue_enc_one_op_cb(struct fpga_queue *q, struct rte_bbdev_enc_op *op,
1498 		uint16_t desc_offset)
1499 {
1500 	union fpga_dma_desc *desc;
1501 	struct rte_mbuf *input;
1502 	struct rte_mbuf *output;
1503 	int ret;
1504 	uint16_t k, e, ncb, ring_offset;
1505 	uint32_t total_left, in_length, out_length, in_offset, out_offset;
1506 
1507 #ifdef RTE_LIBRTE_BBDEV_DEBUG
1508 	/* Validate op structure */
1509 	if (validate_enc_op(op) == -1) {
1510 		rte_bbdev_log(ERR, "Turbo encoder validation failed");
1511 		return -EINVAL;
1512 	}
1513 #endif
1514 
1515 	input = op->turbo_enc.input.data;
1516 	output = op->turbo_enc.output.data;
1517 	in_offset = op->turbo_enc.input.offset;
1518 	out_offset = op->turbo_enc.output.offset;
1519 	total_left = op->turbo_enc.input.length;
1520 	k = op->turbo_enc.cb_params.k;
1521 	e = op->turbo_enc.cb_params.e;
1522 	ncb = op->turbo_enc.cb_params.ncb;
1523 
1524 	if (check_bit(op->turbo_enc.op_flags, RTE_BBDEV_TURBO_CRC_24B_ATTACH))
1525 		in_length = ((k - 24) >> 3);
1526 	else
1527 		in_length = k >> 3;
1528 
1529 	if (check_bit(op->turbo_enc.op_flags, RTE_BBDEV_TURBO_RATE_MATCH))
1530 		out_length = (e + 7) >> 3;
1531 	else
1532 		out_length = (k >> 3) * 3 + 2;
1533 
1534 	mbuf_append(output, output, out_length);
1535 
1536 	/* Offset into the ring */
1537 	ring_offset = ((q->tail + desc_offset) & q->sw_ring_wrap_mask);
1538 	/* Setup DMA Descriptor */
1539 	desc = q->ring_addr + ring_offset;
1540 
1541 	ret = fpga_dma_desc_te_fill(op, &desc->enc_req, input, output, k, e,
1542 			ncb, in_offset, out_offset, ring_offset, 1);
1543 	if (unlikely(ret < 0))
1544 		return ret;
1545 
1546 	/* Update lengths */
1547 	total_left -= in_length;
1548 	op->turbo_enc.output.length += out_length;
1549 
1550 	if (total_left > 0) {
1551 		rte_bbdev_log(ERR,
1552 			"Mismatch between mbuf length and included CB sizes: mbuf len %u, cb len %u",
1553 				total_left, in_length);
1554 		return -1;
1555 	}
1556 
1557 	return 1;
1558 }
1559 
1560 static inline int
1561 enqueue_enc_one_op_tb(struct fpga_queue *q, struct rte_bbdev_enc_op *op,
1562 		uint16_t desc_offset, uint8_t cbs_in_op)
1563 {
1564 	union fpga_dma_desc *desc;
1565 	struct rte_mbuf *input, *output_head, *output;
1566 	int ret;
1567 	uint8_t r, c, crc24_bits = 0;
1568 	uint16_t k, e, ncb, ring_offset;
1569 	uint32_t mbuf_total_left, in_length, out_length, in_offset, out_offset;
1570 	uint32_t seg_total_left;
1571 	uint16_t current_enqueued_cbs = 0;
1572 
1573 #ifdef RTE_LIBRTE_BBDEV_DEBUG
1574 	/* Validate op structure */
1575 	if (validate_enc_op(op) == -1) {
1576 		rte_bbdev_log(ERR, "Turbo encoder validation failed");
1577 		return -EINVAL;
1578 	}
1579 #endif
1580 
1581 	input = op->turbo_enc.input.data;
1582 	output_head = output = op->turbo_enc.output.data;
1583 	in_offset = op->turbo_enc.input.offset;
1584 	out_offset = op->turbo_enc.output.offset;
1585 	mbuf_total_left = op->turbo_enc.input.length;
1586 
1587 	c = op->turbo_enc.tb_params.c;
1588 	r = op->turbo_enc.tb_params.r;
1589 
1590 	if (check_bit(op->turbo_enc.op_flags, RTE_BBDEV_TURBO_CRC_24B_ATTACH))
1591 		crc24_bits = 24;
1592 
1593 	while (mbuf_total_left > 0 && r < c && input != NULL) {
1594 		seg_total_left = rte_pktmbuf_data_len(input) - in_offset;
1595 
1596 		e = (r < op->turbo_enc.tb_params.cab) ?
1597 				op->turbo_enc.tb_params.ea :
1598 				op->turbo_enc.tb_params.eb;
1599 		k = (r < op->turbo_enc.tb_params.c_neg) ?
1600 				op->turbo_enc.tb_params.k_neg :
1601 				op->turbo_enc.tb_params.k_pos;
1602 		ncb = (r < op->turbo_enc.tb_params.c_neg) ?
1603 				op->turbo_enc.tb_params.ncb_neg :
1604 				op->turbo_enc.tb_params.ncb_pos;
1605 
1606 		in_length = ((k - crc24_bits) >> 3);
1607 
1608 		if (check_bit(op->turbo_enc.op_flags,
1609 			RTE_BBDEV_TURBO_RATE_MATCH))
1610 			out_length = (e + 7) >> 3;
1611 		else
1612 			out_length = (k >> 3) * 3 + 2;
1613 
1614 		mbuf_append(output_head, output, out_length);
1615 
1616 		/* Setup DMA Descriptor */
1617 		ring_offset = ((q->tail + desc_offset) & q->sw_ring_wrap_mask);
1618 		desc = q->ring_addr + ring_offset;
1619 		ret = fpga_dma_desc_te_fill(op, &desc->enc_req, input, output,
1620 				k, e, ncb, in_offset, out_offset, ring_offset,
1621 				cbs_in_op);
1622 		if (unlikely(ret < 0))
1623 			return ret;
1624 
1625 		rte_bbdev_log_debug("DMA request desc %p", desc);
1626 
1627 		/* Update lengths */
1628 		op->turbo_enc.output.length += out_length;
1629 		mbuf_total_left -= in_length;
1630 
1631 		/* Update offsets */
1632 		if (seg_total_left == in_length) {
1633 			/* Go to the next mbuf */
1634 			input = input->next;
1635 			output = output->next;
1636 			in_offset = 0;
1637 			out_offset = 0;
1638 		} else {
1639 			in_offset += in_length;
1640 			out_offset += out_length;
1641 		}
1642 
1643 		r++;
1644 		desc_offset++;
1645 		current_enqueued_cbs++;
1646 	}
1647 
1648 	if (mbuf_total_left > 0) {
1649 		rte_bbdev_log(ERR,
1650 				"Some date still left for processing: mbuf_total_left = %u",
1651 				mbuf_total_left);
1652 		return -1;
1653 	}
1654 
1655 	return current_enqueued_cbs;
1656 }
1657 
1658 #ifdef RTE_LIBRTE_BBDEV_DEBUG
1659 /* Validates turbo decoder parameters */
1660 static int
1661 validate_dec_op(struct rte_bbdev_dec_op *op)
1662 {
1663 	struct rte_bbdev_op_turbo_dec *turbo_dec = &op->turbo_dec;
1664 	struct rte_bbdev_op_dec_turbo_cb_params *cb = NULL;
1665 	struct rte_bbdev_op_dec_turbo_tb_params *tb = NULL;
1666 
1667 	if (op->mempool == NULL) {
1668 		rte_bbdev_log(ERR, "Invalid mempool pointer");
1669 		return -1;
1670 	}
1671 	if (turbo_dec->input.data == NULL) {
1672 		rte_bbdev_log(ERR, "Invalid input pointer");
1673 		return -1;
1674 	}
1675 	if (turbo_dec->hard_output.data == NULL) {
1676 		rte_bbdev_log(ERR, "Invalid hard_output pointer");
1677 		return -1;
1678 	}
1679 	if (turbo_dec->rv_index > 3) {
1680 		rte_bbdev_log(ERR,
1681 				"rv_index (%u) is out of range 0 <= value <= 3",
1682 				turbo_dec->rv_index);
1683 		return -1;
1684 	}
1685 	if (turbo_dec->iter_min < 1) {
1686 		rte_bbdev_log(ERR,
1687 				"iter_min (%u) is less than 1",
1688 				turbo_dec->iter_min);
1689 		return -1;
1690 	}
1691 	if (turbo_dec->iter_max <= 2) {
1692 		rte_bbdev_log(ERR,
1693 				"iter_max (%u) is less than or equal to 2",
1694 				turbo_dec->iter_max);
1695 		return -1;
1696 	}
1697 	if (turbo_dec->iter_min > turbo_dec->iter_max) {
1698 		rte_bbdev_log(ERR,
1699 				"iter_min (%u) is greater than iter_max (%u)",
1700 				turbo_dec->iter_min, turbo_dec->iter_max);
1701 		return -1;
1702 	}
1703 	if (turbo_dec->code_block_mode != RTE_BBDEV_TRANSPORT_BLOCK &&
1704 			turbo_dec->code_block_mode != RTE_BBDEV_CODE_BLOCK) {
1705 		rte_bbdev_log(ERR,
1706 				"code_block_mode (%u) is out of range 0 <= value <= 1",
1707 				turbo_dec->code_block_mode);
1708 		return -1;
1709 	}
1710 
1711 	if (turbo_dec->code_block_mode == RTE_BBDEV_TRANSPORT_BLOCK) {
1712 
1713 		if ((turbo_dec->op_flags &
1714 			RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP) &&
1715 			!(turbo_dec->op_flags & RTE_BBDEV_TURBO_CRC_TYPE_24B)) {
1716 			rte_bbdev_log(ERR,
1717 				"RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP should accompany RTE_BBDEV_TURBO_CRC_TYPE_24B");
1718 			return -1;
1719 		}
1720 
1721 		tb = &turbo_dec->tb_params;
1722 		if ((tb->k_neg < RTE_BBDEV_TURBO_MIN_CB_SIZE
1723 				|| tb->k_neg > RTE_BBDEV_TURBO_MAX_CB_SIZE)
1724 				&& tb->c_neg > 0) {
1725 			rte_bbdev_log(ERR,
1726 					"k_neg (%u) is out of range %u <= value <= %u",
1727 					tb->k_neg, RTE_BBDEV_TURBO_MIN_CB_SIZE,
1728 					RTE_BBDEV_TURBO_MAX_CB_SIZE);
1729 			return -1;
1730 		}
1731 		if ((tb->k_pos < RTE_BBDEV_TURBO_MIN_CB_SIZE
1732 				|| tb->k_pos > RTE_BBDEV_TURBO_MAX_CB_SIZE)
1733 				&& tb->c > tb->c_neg) {
1734 			rte_bbdev_log(ERR,
1735 					"k_pos (%u) is out of range %u <= value <= %u",
1736 					tb->k_pos, RTE_BBDEV_TURBO_MIN_CB_SIZE,
1737 					RTE_BBDEV_TURBO_MAX_CB_SIZE);
1738 			return -1;
1739 		}
1740 		if (tb->c_neg > (RTE_BBDEV_TURBO_MAX_CODE_BLOCKS - 1))
1741 			rte_bbdev_log(ERR,
1742 					"c_neg (%u) is out of range 0 <= value <= %u",
1743 					tb->c_neg,
1744 					RTE_BBDEV_TURBO_MAX_CODE_BLOCKS - 1);
1745 		if (tb->c < 1 || tb->c > RTE_BBDEV_TURBO_MAX_CODE_BLOCKS) {
1746 			rte_bbdev_log(ERR,
1747 					"c (%u) is out of range 1 <= value <= %u",
1748 					tb->c, RTE_BBDEV_TURBO_MAX_CODE_BLOCKS);
1749 			return -1;
1750 		}
1751 		if (tb->cab > tb->c) {
1752 			rte_bbdev_log(ERR,
1753 					"cab (%u) is greater than c (%u)",
1754 					tb->cab, tb->c);
1755 			return -1;
1756 		}
1757 	} else {
1758 
1759 		if (turbo_dec->op_flags & RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP) {
1760 			rte_bbdev_log(ERR,
1761 					"RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP is invalid in CB-mode");
1762 			return -1;
1763 		}
1764 
1765 		cb = &turbo_dec->cb_params;
1766 		if (cb->k < RTE_BBDEV_TURBO_MIN_CB_SIZE
1767 				|| cb->k > RTE_BBDEV_TURBO_MAX_CB_SIZE) {
1768 			rte_bbdev_log(ERR,
1769 					"k (%u) is out of range %u <= value <= %u",
1770 					cb->k, RTE_BBDEV_TURBO_MIN_CB_SIZE,
1771 					RTE_BBDEV_TURBO_MAX_CB_SIZE);
1772 			return -1;
1773 		}
1774 	}
1775 
1776 	return 0;
1777 }
1778 #endif
1779 
1780 static inline int
1781 enqueue_dec_one_op_cb(struct fpga_queue *q, struct rte_bbdev_dec_op *op,
1782 		uint16_t desc_offset)
1783 {
1784 	union fpga_dma_desc *desc;
1785 	struct rte_mbuf *input;
1786 	struct rte_mbuf *output;
1787 	int ret;
1788 	uint16_t k, kw, ring_offset;
1789 	uint32_t total_left, in_length, out_length, in_offset, out_offset;
1790 
1791 #ifdef RTE_LIBRTE_BBDEV_DEBUG
1792 	/* Validate op structure */
1793 	if (validate_dec_op(op) == -1) {
1794 		rte_bbdev_log(ERR, "Turbo decoder validation failed");
1795 		return -EINVAL;
1796 	}
1797 #endif
1798 
1799 	input = op->turbo_dec.input.data;
1800 	output = op->turbo_dec.hard_output.data;
1801 	total_left = op->turbo_dec.input.length;
1802 	in_offset = op->turbo_dec.input.offset;
1803 	out_offset = op->turbo_dec.hard_output.offset;
1804 
1805 	k = op->turbo_dec.cb_params.k;
1806 	kw = RTE_ALIGN_CEIL(k + 4, 32) * 3;
1807 	in_length = kw;
1808 	out_length = k >> 3;
1809 
1810 	mbuf_append(output, output, out_length);
1811 
1812 	/* Setup DMA Descriptor */
1813 	ring_offset = ((q->tail + desc_offset) & q->sw_ring_wrap_mask);
1814 	desc = q->ring_addr + ring_offset;
1815 	ret = fpga_dma_desc_td_fill(op, &desc->dec_req, input, output,
1816 			in_length, k, in_offset, out_offset, ring_offset, 1);
1817 	if (unlikely(ret < 0))
1818 		return ret;
1819 
1820 #ifdef RTE_LIBRTE_BBDEV_DEBUG
1821 	print_dma_dec_desc_debug_info(desc);
1822 #endif
1823 
1824 	/* Update lengths */
1825 	total_left -= in_length;
1826 	op->turbo_dec.hard_output.length += out_length;
1827 
1828 	if (total_left > 0) {
1829 		rte_bbdev_log(ERR,
1830 				"Mismatch between mbuf length and included CB sizes: mbuf len %u, cb len %u",
1831 				total_left, in_length);
1832 		return -1;
1833 	}
1834 
1835 	return 1;
1836 }
1837 
1838 
1839 static inline int
1840 enqueue_dec_one_op_tb(struct fpga_queue *q, struct rte_bbdev_dec_op *op,
1841 		uint16_t desc_offset, uint8_t cbs_in_op)
1842 {
1843 	union fpga_dma_desc *desc;
1844 	struct rte_mbuf *input, *output_head, *output;
1845 	int ret;
1846 	uint8_t r, c;
1847 	uint16_t k, kw, in_length, out_length, ring_offset;
1848 	uint32_t mbuf_total_left, seg_total_left, in_offset, out_offset;
1849 	uint16_t current_enqueued_cbs = 0;
1850 	uint16_t crc24_overlap = 0;
1851 
1852 #ifdef RTE_LIBRTE_BBDEV_DEBUG
1853 	/* Validate op structure */
1854 	if (validate_dec_op(op) == -1) {
1855 		rte_bbdev_log(ERR, "Turbo decoder validation failed");
1856 		return -EINVAL;
1857 	}
1858 #endif
1859 
1860 	input = op->turbo_dec.input.data;
1861 	output_head = output = op->turbo_dec.hard_output.data;
1862 	mbuf_total_left = op->turbo_dec.input.length;
1863 	in_offset = op->turbo_dec.input.offset;
1864 	out_offset = op->turbo_dec.hard_output.offset;
1865 
1866 	if (!check_bit(op->turbo_dec.op_flags,
1867 		RTE_BBDEV_TURBO_DEC_TB_CRC_24B_KEEP))
1868 		crc24_overlap = 24;
1869 
1870 	c = op->turbo_dec.tb_params.c;
1871 	r = op->turbo_dec.tb_params.r;
1872 
1873 	while (mbuf_total_left > 0 && r < c && input != NULL) {
1874 		seg_total_left = rte_pktmbuf_data_len(input) - in_offset;
1875 		k = (r < op->turbo_dec.tb_params.c_neg) ?
1876 				op->turbo_dec.tb_params.k_neg :
1877 				op->turbo_dec.tb_params.k_pos;
1878 		kw = RTE_ALIGN_CEIL(k + 4, 32) * 3;
1879 
1880 		in_length = kw;
1881 		out_length = (k - crc24_overlap) >> 3;
1882 
1883 		mbuf_append(output_head, output, out_length);
1884 
1885 		if (seg_total_left < in_length) {
1886 			rte_bbdev_log(ERR,
1887 					"Partial CB found in a TB. FPGA Driver doesn't support scatter-gather operations!");
1888 			return -1;
1889 		}
1890 
1891 		/* Setup DMA Descriptor */
1892 		ring_offset = ((q->tail + desc_offset) & q->sw_ring_wrap_mask);
1893 		desc = q->ring_addr + ring_offset;
1894 		ret = fpga_dma_desc_td_fill(op, &desc->dec_req, input, output,
1895 				in_length, k, in_offset, out_offset,
1896 				ring_offset, cbs_in_op);
1897 		if (unlikely(ret < 0))
1898 			return ret;
1899 
1900 		/* Update lengths */
1901 		ret = rte_pktmbuf_trim(op->turbo_dec.hard_output.data,
1902 				(crc24_overlap >> 3));
1903 #ifdef RTE_LIBRTE_BBDEV_DEBUG
1904 		if (ret < 0) {
1905 			rte_bbdev_log(ERR,
1906 					"The length to remove is greater than the length of the last segment");
1907 			return -EINVAL;
1908 		}
1909 #endif
1910 		op->turbo_dec.hard_output.length += out_length;
1911 		mbuf_total_left -= in_length;
1912 
1913 		/* Update offsets */
1914 		if (seg_total_left == in_length) {
1915 			/* Go to the next mbuf */
1916 			input = input->next;
1917 			output = output->next;
1918 			in_offset = 0;
1919 			out_offset = 0;
1920 		} else {
1921 			in_offset += in_length;
1922 			out_offset += out_length;
1923 		}
1924 
1925 		r++;
1926 		desc_offset++;
1927 		current_enqueued_cbs++;
1928 	}
1929 
1930 	if (mbuf_total_left > 0) {
1931 		rte_bbdev_log(ERR,
1932 				"Some date still left for processing: mbuf_total_left = %u",
1933 				mbuf_total_left);
1934 		return -1;
1935 	}
1936 
1937 	return current_enqueued_cbs;
1938 }
1939 
1940 static uint16_t
1941 fpga_enqueue_enc(struct rte_bbdev_queue_data *q_data,
1942 		struct rte_bbdev_enc_op **ops, uint16_t num)
1943 {
1944 	uint8_t cbs_in_op;
1945 	uint16_t i, total_enqueued_cbs = 0;
1946 	int32_t avail;
1947 	int enqueued_cbs;
1948 	struct fpga_queue *q = q_data->queue_private;
1949 	union fpga_dma_desc *desc;
1950 
1951 	/* Check if queue is not full */
1952 	if (unlikely(((q->tail + 1) & q->sw_ring_wrap_mask) ==
1953 			q->head_free_desc))
1954 		return 0;
1955 
1956 	/* Calculates available space */
1957 	avail = (q->head_free_desc > q->tail) ?
1958 		q->head_free_desc - q->tail - 1 :
1959 		q->ring_ctrl_reg.ring_size + q->head_free_desc - q->tail - 1;
1960 
1961 	for (i = 0; i < num; ++i) {
1962 		if (ops[i]->turbo_enc.code_block_mode ==
1963 				RTE_BBDEV_TRANSPORT_BLOCK) {
1964 			cbs_in_op = get_num_cbs_in_op_enc(&ops[i]->turbo_enc);
1965 			/* Check if there is available space for further
1966 			 * processing
1967 			 */
1968 			if (unlikely(avail - cbs_in_op < 0))
1969 				break;
1970 			avail -= cbs_in_op;
1971 			enqueued_cbs = enqueue_enc_one_op_tb(q, ops[i],
1972 					total_enqueued_cbs, cbs_in_op);
1973 		} else {
1974 			/* Check if there is available space for further
1975 			 * processing
1976 			 */
1977 			if (unlikely(avail - 1 < 0))
1978 				break;
1979 			avail -= 1;
1980 			enqueued_cbs = enqueue_enc_one_op_cb(q, ops[i],
1981 					total_enqueued_cbs);
1982 		}
1983 
1984 		if (enqueued_cbs < 0)
1985 			break;
1986 
1987 		total_enqueued_cbs += enqueued_cbs;
1988 
1989 		rte_bbdev_log_debug("enqueuing enc ops [%d/%d] | head %d | tail %d",
1990 				total_enqueued_cbs, num,
1991 				q->head_free_desc, q->tail);
1992 	}
1993 
1994 	/* Set interrupt bit for last CB in enqueued ops. FPGA issues interrupt
1995 	 * only when all previous CBs were already processed.
1996 	 */
1997 	desc = q->ring_addr + ((q->tail + total_enqueued_cbs - 1)
1998 			& q->sw_ring_wrap_mask);
1999 	desc->enc_req.irq_en = q->irq_enable;
2000 
2001 	fpga_dma_enqueue(q, total_enqueued_cbs, &q_data->queue_stats);
2002 
2003 	/* Update stats */
2004 	q_data->queue_stats.enqueued_count += i;
2005 	q_data->queue_stats.enqueue_err_count += num - i;
2006 
2007 	return i;
2008 }
2009 
2010 static uint16_t
2011 fpga_enqueue_dec(struct rte_bbdev_queue_data *q_data,
2012 		struct rte_bbdev_dec_op **ops, uint16_t num)
2013 {
2014 	uint8_t cbs_in_op;
2015 	uint16_t i, total_enqueued_cbs = 0;
2016 	int32_t avail;
2017 	int enqueued_cbs;
2018 	struct fpga_queue *q = q_data->queue_private;
2019 	union fpga_dma_desc *desc;
2020 
2021 	/* Check if queue is not full */
2022 	if (unlikely(((q->tail + 1) & q->sw_ring_wrap_mask) ==
2023 			q->head_free_desc))
2024 		return 0;
2025 
2026 	/* Calculates available space */
2027 	avail = (q->head_free_desc > q->tail) ?
2028 		q->head_free_desc - q->tail - 1 :
2029 		q->ring_ctrl_reg.ring_size + q->head_free_desc - q->tail - 1;
2030 
2031 	for (i = 0; i < num; ++i) {
2032 		if (ops[i]->turbo_dec.code_block_mode ==
2033 				RTE_BBDEV_TRANSPORT_BLOCK) {
2034 			cbs_in_op = get_num_cbs_in_op_dec(&ops[i]->turbo_dec);
2035 			/* Check if there is available space for further
2036 			 * processing
2037 			 */
2038 			if (unlikely(avail - cbs_in_op < 0))
2039 				break;
2040 			avail -= cbs_in_op;
2041 			enqueued_cbs = enqueue_dec_one_op_tb(q, ops[i],
2042 					total_enqueued_cbs, cbs_in_op);
2043 		} else {
2044 			/* Check if there is available space for further
2045 			 * processing
2046 			 */
2047 			if (unlikely(avail - 1 < 0))
2048 				break;
2049 			avail -= 1;
2050 			enqueued_cbs = enqueue_dec_one_op_cb(q, ops[i],
2051 					total_enqueued_cbs);
2052 		}
2053 
2054 		if (enqueued_cbs < 0)
2055 			break;
2056 
2057 		total_enqueued_cbs += enqueued_cbs;
2058 
2059 		rte_bbdev_log_debug("enqueuing dec ops [%d/%d] | head %d | tail %d",
2060 				total_enqueued_cbs, num,
2061 				q->head_free_desc, q->tail);
2062 	}
2063 
2064 	/* Set interrupt bit for last CB in enqueued ops. FPGA issues interrupt
2065 	 * only when all previous CBs were already processed.
2066 	 */
2067 	desc = q->ring_addr + ((q->tail + total_enqueued_cbs - 1)
2068 			& q->sw_ring_wrap_mask);
2069 	desc->dec_req.irq_en = q->irq_enable;
2070 
2071 	fpga_dma_enqueue(q, total_enqueued_cbs, &q_data->queue_stats);
2072 
2073 	/* Update stats */
2074 	q_data->queue_stats.enqueued_count += i;
2075 	q_data->queue_stats.enqueue_err_count += num - i;
2076 
2077 	return i;
2078 }
2079 
2080 static inline int
2081 dequeue_enc_one_op_cb(struct fpga_queue *q, struct rte_bbdev_enc_op **op,
2082 		uint16_t desc_offset)
2083 {
2084 	union fpga_dma_desc *desc;
2085 	int desc_error = 0;
2086 
2087 	/* Set current desc */
2088 	desc = q->ring_addr + ((q->head_free_desc + desc_offset)
2089 			& q->sw_ring_wrap_mask);
2090 
2091 	/*check if done */
2092 	if (desc->enc_req.done == 0)
2093 		return -1;
2094 
2095 	/* make sure the response is read atomically */
2096 	rte_smp_rmb();
2097 
2098 	rte_bbdev_log_debug("DMA response desc %p", desc);
2099 
2100 	*op = desc->enc_req.op_addr;
2101 	/* Check the descriptor error field, return 1 on error */
2102 	desc_error = check_desc_error(desc->enc_req.error);
2103 	(*op)->status = desc_error << RTE_BBDEV_DATA_ERROR;
2104 
2105 	return 1;
2106 }
2107 
2108 static inline int
2109 dequeue_enc_one_op_tb(struct fpga_queue *q, struct rte_bbdev_enc_op **op,
2110 		uint16_t desc_offset)
2111 {
2112 	union fpga_dma_desc *desc;
2113 	uint8_t cbs_in_op, cb_idx;
2114 	int desc_error = 0;
2115 	int status = 0;
2116 
2117 	/* Set descriptor */
2118 	desc = q->ring_addr + ((q->head_free_desc + desc_offset)
2119 			& q->sw_ring_wrap_mask);
2120 
2121 	/* Verify if done bit is set */
2122 	if (desc->enc_req.done == 0)
2123 		return -1;
2124 
2125 	/* Make sure the response is read atomically */
2126 	rte_smp_rmb();
2127 
2128 	/* Verify if done bit in all CBs is set */
2129 	cbs_in_op = desc->enc_req.cbs_in_op;
2130 	for (cb_idx = 1; cb_idx < cbs_in_op; ++cb_idx) {
2131 		desc = q->ring_addr + ((q->head_free_desc + desc_offset +
2132 				cb_idx) & q->sw_ring_wrap_mask);
2133 		if (desc->enc_req.done == 0)
2134 			return -1;
2135 	}
2136 
2137 	/* Make sure the response is read atomically */
2138 	rte_smp_rmb();
2139 
2140 	for (cb_idx = 0; cb_idx < cbs_in_op; ++cb_idx) {
2141 		desc = q->ring_addr + ((q->head_free_desc + desc_offset +
2142 				cb_idx) & q->sw_ring_wrap_mask);
2143 		/* Check the descriptor error field, return 1 on error */
2144 		desc_error = check_desc_error(desc->enc_req.error);
2145 		status |=  desc_error << RTE_BBDEV_DATA_ERROR;
2146 		rte_bbdev_log_debug("DMA response desc %p", desc);
2147 	}
2148 
2149 	*op = desc->enc_req.op_addr;
2150 	(*op)->status = status;
2151 	return cbs_in_op;
2152 }
2153 
2154 static inline int
2155 dequeue_dec_one_op_cb(struct fpga_queue *q, struct rte_bbdev_dec_op **op,
2156 		uint16_t desc_offset)
2157 {
2158 	union fpga_dma_desc *desc;
2159 	int desc_error = 0;
2160 	/* Set descriptor */
2161 	desc = q->ring_addr + ((q->head_free_desc + desc_offset)
2162 			& q->sw_ring_wrap_mask);
2163 
2164 	/* Verify done bit is set */
2165 	if (desc->dec_req.done == 0)
2166 		return -1;
2167 
2168 	/* make sure the response is read atomically */
2169 	rte_smp_rmb();
2170 
2171 #ifdef RTE_LIBRTE_BBDEV_DEBUG
2172 	print_dma_dec_desc_debug_info(desc);
2173 
2174 #endif
2175 
2176 	*op = desc->dec_req.op_addr;
2177 	/* FPGA reports in half-iterations, from 0 to 31. get ceiling */
2178 	(*op)->turbo_dec.iter_count = (desc->dec_req.iter + 2) >> 1;
2179 	/* crc_pass = 0 when decoder fails */
2180 	(*op)->status = !(desc->dec_req.crc_pass) << RTE_BBDEV_CRC_ERROR;
2181 	/* Check the descriptor error field, return 1 on error */
2182 	desc_error = check_desc_error(desc->enc_req.error);
2183 	(*op)->status |= desc_error << RTE_BBDEV_DATA_ERROR;
2184 	return 1;
2185 }
2186 
2187 static inline int
2188 dequeue_dec_one_op_tb(struct fpga_queue *q, struct rte_bbdev_dec_op **op,
2189 		uint16_t desc_offset)
2190 {
2191 	union fpga_dma_desc *desc;
2192 	uint8_t cbs_in_op, cb_idx, iter_count = 0;
2193 	int status = 0;
2194 	int  desc_error = 0;
2195 	/* Set descriptor */
2196 	desc = q->ring_addr + ((q->head_free_desc + desc_offset)
2197 			& q->sw_ring_wrap_mask);
2198 
2199 	/* Verify if done bit is set */
2200 	if (desc->dec_req.done == 0)
2201 		return -1;
2202 
2203 	/* Make sure the response is read atomically */
2204 	rte_smp_rmb();
2205 
2206 	/* Verify if done bit in all CBs is set */
2207 	cbs_in_op = desc->dec_req.cbs_in_op;
2208 	for (cb_idx = 1; cb_idx < cbs_in_op; ++cb_idx) {
2209 		desc = q->ring_addr + ((q->head_free_desc + desc_offset +
2210 				cb_idx) & q->sw_ring_wrap_mask);
2211 		if (desc->dec_req.done == 0)
2212 			return -1;
2213 	}
2214 
2215 	/* Make sure the response is read atomically */
2216 	rte_smp_rmb();
2217 
2218 	for (cb_idx = 0; cb_idx < cbs_in_op; ++cb_idx) {
2219 		desc = q->ring_addr + ((q->head_free_desc + desc_offset +
2220 				cb_idx) & q->sw_ring_wrap_mask);
2221 		/* get max iter_count for all CBs in op */
2222 		iter_count = RTE_MAX(iter_count, (uint8_t) desc->dec_req.iter);
2223 		/* crc_pass = 0 when decoder fails, one fails all */
2224 		status |= !(desc->dec_req.crc_pass) << RTE_BBDEV_CRC_ERROR;
2225 		/* Check the descriptor error field, return 1 on error */
2226 		desc_error = check_desc_error(desc->enc_req.error);
2227 		status |= desc_error << RTE_BBDEV_DATA_ERROR;
2228 		rte_bbdev_log_debug("DMA response desc %p", desc);
2229 	}
2230 
2231 	*op = desc->dec_req.op_addr;
2232 
2233 	/* FPGA reports in half-iterations, get ceiling */
2234 	(*op)->turbo_dec.iter_count = (iter_count + 2) >> 1;
2235 	(*op)->status = status;
2236 	return cbs_in_op;
2237 }
2238 
2239 static uint16_t
2240 fpga_dequeue_enc(struct rte_bbdev_queue_data *q_data,
2241 		struct rte_bbdev_enc_op **ops, uint16_t num)
2242 {
2243 	struct fpga_queue *q = q_data->queue_private;
2244 	uint32_t avail = (q->tail - q->head_free_desc) & q->sw_ring_wrap_mask;
2245 	uint16_t i;
2246 	uint16_t dequeued_cbs = 0;
2247 	struct rte_bbdev_enc_op *op;
2248 	int ret;
2249 
2250 	for (i = 0; (i < num) && (dequeued_cbs < avail); ++i) {
2251 		op = (q->ring_addr + ((q->head_free_desc + dequeued_cbs)
2252 			& q->sw_ring_wrap_mask))->enc_req.op_addr;
2253 		if (op->turbo_enc.code_block_mode == RTE_BBDEV_TRANSPORT_BLOCK)
2254 			ret = dequeue_enc_one_op_tb(q, &ops[i], dequeued_cbs);
2255 		else
2256 			ret = dequeue_enc_one_op_cb(q, &ops[i], dequeued_cbs);
2257 
2258 		if (ret < 0)
2259 			break;
2260 
2261 		dequeued_cbs += ret;
2262 
2263 		rte_bbdev_log_debug("dequeuing enc ops [%d/%d] | head %d | tail %d",
2264 				dequeued_cbs, num, q->head_free_desc, q->tail);
2265 	}
2266 
2267 	/* Update head */
2268 	q->head_free_desc = (q->head_free_desc + dequeued_cbs) &
2269 			q->sw_ring_wrap_mask;
2270 
2271 	/* Update stats */
2272 	q_data->queue_stats.dequeued_count += i;
2273 
2274 	return i;
2275 }
2276 
2277 static uint16_t
2278 fpga_dequeue_dec(struct rte_bbdev_queue_data *q_data,
2279 		struct rte_bbdev_dec_op **ops, uint16_t num)
2280 {
2281 	struct fpga_queue *q = q_data->queue_private;
2282 	uint32_t avail = (q->tail - q->head_free_desc) & q->sw_ring_wrap_mask;
2283 	uint16_t i;
2284 	uint16_t dequeued_cbs = 0;
2285 	struct rte_bbdev_dec_op *op;
2286 	int ret;
2287 
2288 	for (i = 0; (i < num) && (dequeued_cbs < avail); ++i) {
2289 		op = (q->ring_addr + ((q->head_free_desc + dequeued_cbs)
2290 			& q->sw_ring_wrap_mask))->dec_req.op_addr;
2291 		if (op->turbo_dec.code_block_mode == RTE_BBDEV_TRANSPORT_BLOCK)
2292 			ret = dequeue_dec_one_op_tb(q, &ops[i], dequeued_cbs);
2293 		else
2294 			ret = dequeue_dec_one_op_cb(q, &ops[i], dequeued_cbs);
2295 
2296 		if (ret < 0)
2297 			break;
2298 
2299 		dequeued_cbs += ret;
2300 
2301 		rte_bbdev_log_debug("dequeuing dec ops [%d/%d] | head %d | tail %d",
2302 				dequeued_cbs, num, q->head_free_desc, q->tail);
2303 	}
2304 
2305 	/* Update head */
2306 	q->head_free_desc = (q->head_free_desc + dequeued_cbs) &
2307 			q->sw_ring_wrap_mask;
2308 
2309 	/* Update stats */
2310 	q_data->queue_stats.dequeued_count += i;
2311 
2312 	return i;
2313 }
2314 
2315 /* Initialization Function */
2316 static void
2317 fpga_lte_fec_init(struct rte_bbdev *dev, struct rte_pci_driver *drv)
2318 {
2319 	struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev->device);
2320 
2321 	dev->dev_ops = &fpga_ops;
2322 	dev->enqueue_enc_ops = fpga_enqueue_enc;
2323 	dev->enqueue_dec_ops = fpga_enqueue_dec;
2324 	dev->dequeue_enc_ops = fpga_dequeue_enc;
2325 	dev->dequeue_dec_ops = fpga_dequeue_dec;
2326 
2327 	((struct fpga_lte_fec_device *) dev->data->dev_private)->pf_device =
2328 			!strcmp(drv->driver.name,
2329 					RTE_STR(FPGA_LTE_FEC_PF_DRIVER_NAME));
2330 	((struct fpga_lte_fec_device *) dev->data->dev_private)->mmio_base =
2331 			pci_dev->mem_resource[0].addr;
2332 
2333 	rte_bbdev_log_debug(
2334 			"Init device %s [%s] @ virtaddr %p phyaddr %#"PRIx64,
2335 			drv->driver.name, dev->data->name,
2336 			(void *)pci_dev->mem_resource[0].addr,
2337 			pci_dev->mem_resource[0].phys_addr);
2338 }
2339 
2340 static int
2341 fpga_lte_fec_probe(struct rte_pci_driver *pci_drv,
2342 	struct rte_pci_device *pci_dev)
2343 {
2344 	struct rte_bbdev *bbdev = NULL;
2345 	char dev_name[RTE_BBDEV_NAME_MAX_LEN];
2346 
2347 	if (pci_dev == NULL) {
2348 		rte_bbdev_log(ERR, "NULL PCI device");
2349 		return -EINVAL;
2350 	}
2351 
2352 	rte_pci_device_name(&pci_dev->addr, dev_name, sizeof(dev_name));
2353 
2354 	/* Allocate memory to be used privately by drivers */
2355 	bbdev = rte_bbdev_allocate(pci_dev->device.name);
2356 	if (bbdev == NULL)
2357 		return -ENODEV;
2358 
2359 	/* allocate device private memory */
2360 	bbdev->data->dev_private = rte_zmalloc_socket(dev_name,
2361 			sizeof(struct fpga_lte_fec_device), RTE_CACHE_LINE_SIZE,
2362 			pci_dev->device.numa_node);
2363 
2364 	if (bbdev->data->dev_private == NULL) {
2365 		rte_bbdev_log(CRIT,
2366 				"Allocate of %zu bytes for device \"%s\" failed",
2367 				sizeof(struct fpga_lte_fec_device), dev_name);
2368 				rte_bbdev_release(bbdev);
2369 			return -ENOMEM;
2370 	}
2371 
2372 	/* Fill HW specific part of device structure */
2373 	bbdev->device = &pci_dev->device;
2374 	bbdev->intr_handle = pci_dev->intr_handle;
2375 	bbdev->data->socket_id = pci_dev->device.numa_node;
2376 
2377 	/* Invoke FEC FPGA device initialization function */
2378 	fpga_lte_fec_init(bbdev, pci_drv);
2379 
2380 	rte_bbdev_log_debug("bbdev id = %u [%s]",
2381 			bbdev->data->dev_id, dev_name);
2382 
2383 	struct fpga_lte_fec_device *d = bbdev->data->dev_private;
2384 	uint32_t version_id = fpga_reg_read_32(d->mmio_base,
2385 			FPGA_LTE_FEC_VERSION_ID);
2386 	rte_bbdev_log(INFO, "FEC FPGA RTL v%u.%u",
2387 		((uint16_t)(version_id >> 16)), ((uint16_t)version_id));
2388 
2389 #ifdef RTE_LIBRTE_BBDEV_DEBUG
2390 	if (!strcmp(pci_drv->driver.name,
2391 			RTE_STR(FPGA_LTE_FEC_PF_DRIVER_NAME)))
2392 		print_static_reg_debug_info(d->mmio_base);
2393 #endif
2394 	return 0;
2395 }
2396 
2397 static int
2398 fpga_lte_fec_remove(struct rte_pci_device *pci_dev)
2399 {
2400 	struct rte_bbdev *bbdev;
2401 	int ret;
2402 	uint8_t dev_id;
2403 
2404 	if (pci_dev == NULL)
2405 		return -EINVAL;
2406 
2407 	/* Find device */
2408 	bbdev = rte_bbdev_get_named_dev(pci_dev->device.name);
2409 	if (bbdev == NULL) {
2410 		rte_bbdev_log(CRIT,
2411 				"Couldn't find HW dev \"%s\" to uninitialise it",
2412 				pci_dev->device.name);
2413 		return -ENODEV;
2414 	}
2415 	dev_id = bbdev->data->dev_id;
2416 
2417 	/* free device private memory before close */
2418 	rte_free(bbdev->data->dev_private);
2419 
2420 	/* Close device */
2421 	ret = rte_bbdev_close(dev_id);
2422 	if (ret < 0)
2423 		rte_bbdev_log(ERR,
2424 				"Device %i failed to close during uninit: %i",
2425 				dev_id, ret);
2426 
2427 	/* release bbdev from library */
2428 	ret = rte_bbdev_release(bbdev);
2429 	if (ret)
2430 		rte_bbdev_log(ERR, "Device %i failed to uninit: %i", dev_id,
2431 				ret);
2432 
2433 	rte_bbdev_log_debug("Destroyed bbdev = %u", dev_id);
2434 
2435 	return 0;
2436 }
2437 
2438 static inline void
2439 set_default_fpga_conf(struct rte_fpga_lte_fec_conf *def_conf)
2440 {
2441 	/* clear default configuration before initialization */
2442 	memset(def_conf, 0, sizeof(struct rte_fpga_lte_fec_conf));
2443 	/* Set pf mode to true */
2444 	def_conf->pf_mode_en = true;
2445 
2446 	/* Set ratio between UL and DL to 1:1 (unit of weight is 3 CBs) */
2447 	def_conf->ul_bandwidth = 3;
2448 	def_conf->dl_bandwidth = 3;
2449 
2450 	/* Set Load Balance Factor to 64 */
2451 	def_conf->dl_load_balance = 64;
2452 	def_conf->ul_load_balance = 64;
2453 }
2454 
2455 /* Initial configuration of FPGA LTE FEC device */
2456 int
2457 rte_fpga_lte_fec_configure(const char *dev_name,
2458 		const struct rte_fpga_lte_fec_conf *conf)
2459 {
2460 	uint32_t payload_32, address;
2461 	uint16_t payload_16;
2462 	uint8_t payload_8;
2463 	uint16_t q_id, vf_id, total_q_id, total_ul_q_id, total_dl_q_id;
2464 	struct rte_bbdev *bbdev = rte_bbdev_get_named_dev(dev_name);
2465 	struct rte_fpga_lte_fec_conf def_conf;
2466 
2467 	if (bbdev == NULL) {
2468 		rte_bbdev_log(ERR,
2469 				"Invalid dev_name (%s), or device is not yet initialised",
2470 				dev_name);
2471 		return -ENODEV;
2472 	}
2473 
2474 	struct fpga_lte_fec_device *d = bbdev->data->dev_private;
2475 
2476 	if (conf == NULL) {
2477 		rte_bbdev_log(ERR,
2478 				"FPGA Configuration was not provided. Default configuration will be loaded.");
2479 		set_default_fpga_conf(&def_conf);
2480 		conf = &def_conf;
2481 	}
2482 
2483 	/*
2484 	 * Configure UL:DL ratio.
2485 	 * [7:0]: UL weight
2486 	 * [15:8]: DL weight
2487 	 */
2488 	payload_16 = (conf->dl_bandwidth << 8) | conf->ul_bandwidth;
2489 	address = FPGA_LTE_FEC_CONFIGURATION;
2490 	fpga_reg_write_16(d->mmio_base, address, payload_16);
2491 
2492 	/* Clear all queues registers */
2493 	payload_32 = FPGA_INVALID_HW_QUEUE_ID;
2494 	for (q_id = 0; q_id < FPGA_TOTAL_NUM_QUEUES; ++q_id) {
2495 		address = (q_id << 2) + FPGA_LTE_FEC_QUEUE_MAP;
2496 		fpga_reg_write_32(d->mmio_base, address, payload_32);
2497 	}
2498 
2499 	/*
2500 	 * If PF mode is enabled allocate all queues for PF only.
2501 	 *
2502 	 * For VF mode each VF can have different number of UL and DL queues.
2503 	 * Total number of queues to configure cannot exceed FPGA
2504 	 * capabilities - 64 queues - 32 queues for UL and 32 queues for DL.
2505 	 * Queues mapping is done according to configuration:
2506 	 *
2507 	 * UL queues:
2508 	 * |                Q_ID              | VF_ID |
2509 	 * |                 0                |   0   |
2510 	 * |                ...               |   0   |
2511 	 * | conf->vf_dl_queues_number[0] - 1 |   0   |
2512 	 * | conf->vf_dl_queues_number[0]     |   1   |
2513 	 * |                ...               |   1   |
2514 	 * | conf->vf_dl_queues_number[1] - 1 |   1   |
2515 	 * |                ...               |  ...  |
2516 	 * | conf->vf_dl_queues_number[7] - 1 |   7   |
2517 	 *
2518 	 * DL queues:
2519 	 * |                Q_ID              | VF_ID |
2520 	 * |                 32               |   0   |
2521 	 * |                ...               |   0   |
2522 	 * | conf->vf_ul_queues_number[0] - 1 |   0   |
2523 	 * | conf->vf_ul_queues_number[0]     |   1   |
2524 	 * |                ...               |   1   |
2525 	 * | conf->vf_ul_queues_number[1] - 1 |   1   |
2526 	 * |                ...               |  ...  |
2527 	 * | conf->vf_ul_queues_number[7] - 1 |   7   |
2528 	 *
2529 	 * Example of configuration:
2530 	 * conf->vf_ul_queues_number[0] = 4;  -> 4 UL queues for VF0
2531 	 * conf->vf_dl_queues_number[0] = 4;  -> 4 DL queues for VF0
2532 	 * conf->vf_ul_queues_number[1] = 2;  -> 2 UL queues for VF1
2533 	 * conf->vf_dl_queues_number[1] = 2;  -> 2 DL queues for VF1
2534 	 *
2535 	 * UL:
2536 	 * | Q_ID | VF_ID |
2537 	 * |   0  |   0   |
2538 	 * |   1  |   0   |
2539 	 * |   2  |   0   |
2540 	 * |   3  |   0   |
2541 	 * |   4  |   1   |
2542 	 * |   5  |   1   |
2543 	 *
2544 	 * DL:
2545 	 * | Q_ID | VF_ID |
2546 	 * |  32  |   0   |
2547 	 * |  33  |   0   |
2548 	 * |  34  |   0   |
2549 	 * |  35  |   0   |
2550 	 * |  36  |   1   |
2551 	 * |  37  |   1   |
2552 	 */
2553 	if (conf->pf_mode_en) {
2554 		payload_32 = 0x1;
2555 		for (q_id = 0; q_id < FPGA_TOTAL_NUM_QUEUES; ++q_id) {
2556 			address = (q_id << 2) + FPGA_LTE_FEC_QUEUE_MAP;
2557 			fpga_reg_write_32(d->mmio_base, address, payload_32);
2558 		}
2559 	} else {
2560 		/* Calculate total number of UL and DL queues to configure */
2561 		total_ul_q_id = total_dl_q_id = 0;
2562 		for (vf_id = 0; vf_id < FPGA_LTE_FEC_NUM_VFS; ++vf_id) {
2563 			total_ul_q_id += conf->vf_ul_queues_number[vf_id];
2564 			total_dl_q_id += conf->vf_dl_queues_number[vf_id];
2565 		}
2566 		total_q_id = total_dl_q_id + total_ul_q_id;
2567 		/*
2568 		 * Check if total number of queues to configure does not exceed
2569 		 * FPGA capabilities (64 queues - 32 UL and 32 DL queues)
2570 		 */
2571 		if ((total_ul_q_id > FPGA_NUM_UL_QUEUES) ||
2572 			(total_dl_q_id > FPGA_NUM_DL_QUEUES) ||
2573 			(total_q_id > FPGA_TOTAL_NUM_QUEUES)) {
2574 			rte_bbdev_log(ERR,
2575 					"FPGA Configuration failed. Too many queues to configure: UL_Q %u, DL_Q %u, FPGA_Q %u",
2576 					total_ul_q_id, total_dl_q_id,
2577 					FPGA_TOTAL_NUM_QUEUES);
2578 			return -EINVAL;
2579 		}
2580 		total_ul_q_id = 0;
2581 		for (vf_id = 0; vf_id < FPGA_LTE_FEC_NUM_VFS; ++vf_id) {
2582 			for (q_id = 0; q_id < conf->vf_ul_queues_number[vf_id];
2583 					++q_id, ++total_ul_q_id) {
2584 				address = (total_ul_q_id << 2) +
2585 						FPGA_LTE_FEC_QUEUE_MAP;
2586 				payload_32 = ((0x80 + vf_id) << 16) | 0x1;
2587 				fpga_reg_write_32(d->mmio_base, address,
2588 						payload_32);
2589 			}
2590 		}
2591 		total_dl_q_id = 0;
2592 		for (vf_id = 0; vf_id < FPGA_LTE_FEC_NUM_VFS; ++vf_id) {
2593 			for (q_id = 0; q_id < conf->vf_dl_queues_number[vf_id];
2594 					++q_id, ++total_dl_q_id) {
2595 				address = ((total_dl_q_id + FPGA_NUM_UL_QUEUES)
2596 						<< 2) + FPGA_LTE_FEC_QUEUE_MAP;
2597 				payload_32 = ((0x80 + vf_id) << 16) | 0x1;
2598 				fpga_reg_write_32(d->mmio_base, address,
2599 						payload_32);
2600 			}
2601 		}
2602 	}
2603 
2604 	/* Setting Load Balance Factor */
2605 	payload_16 = (conf->dl_load_balance << 8) | (conf->ul_load_balance);
2606 	address = FPGA_LTE_FEC_LOAD_BALANCE_FACTOR;
2607 	fpga_reg_write_16(d->mmio_base, address, payload_16);
2608 
2609 	/* Setting length of ring descriptor entry */
2610 	payload_16 = FPGA_RING_DESC_ENTRY_LENGTH;
2611 	address = FPGA_LTE_FEC_RING_DESC_LEN;
2612 	fpga_reg_write_16(d->mmio_base, address, payload_16);
2613 
2614 	/* Setting FLR timeout value */
2615 	payload_16 = conf->flr_time_out;
2616 	address = FPGA_LTE_FEC_FLR_TIME_OUT;
2617 	fpga_reg_write_16(d->mmio_base, address, payload_16);
2618 
2619 	/* Queue PF/VF mapping table is ready */
2620 	payload_8 = 0x1;
2621 	address = FPGA_LTE_FEC_QUEUE_PF_VF_MAP_DONE;
2622 	fpga_reg_write_8(d->mmio_base, address, payload_8);
2623 
2624 	rte_bbdev_log_debug("PF FPGA LTE FEC configuration complete for %s",
2625 			dev_name);
2626 
2627 #ifdef RTE_LIBRTE_BBDEV_DEBUG
2628 	print_static_reg_debug_info(d->mmio_base);
2629 #endif
2630 	return 0;
2631 }
2632 
2633 /* FPGA LTE FEC PCI PF address map */
2634 static struct rte_pci_id pci_id_fpga_lte_fec_pf_map[] = {
2635 	{
2636 		RTE_PCI_DEVICE(FPGA_LTE_FEC_VENDOR_ID,
2637 				FPGA_LTE_FEC_PF_DEVICE_ID)
2638 	},
2639 	{.device_id = 0},
2640 };
2641 
2642 static struct rte_pci_driver fpga_lte_fec_pci_pf_driver = {
2643 	.probe = fpga_lte_fec_probe,
2644 	.remove = fpga_lte_fec_remove,
2645 	.id_table = pci_id_fpga_lte_fec_pf_map,
2646 	.drv_flags = RTE_PCI_DRV_NEED_MAPPING
2647 };
2648 
2649 /* FPGA LTE FEC PCI VF address map */
2650 static struct rte_pci_id pci_id_fpga_lte_fec_vf_map[] = {
2651 	{
2652 		RTE_PCI_DEVICE(FPGA_LTE_FEC_VENDOR_ID,
2653 				FPGA_LTE_FEC_VF_DEVICE_ID)
2654 	},
2655 	{.device_id = 0},
2656 };
2657 
2658 static struct rte_pci_driver fpga_lte_fec_pci_vf_driver = {
2659 	.probe = fpga_lte_fec_probe,
2660 	.remove = fpga_lte_fec_remove,
2661 	.id_table = pci_id_fpga_lte_fec_vf_map,
2662 	.drv_flags = RTE_PCI_DRV_NEED_MAPPING
2663 };
2664 
2665 
2666 RTE_PMD_REGISTER_PCI(FPGA_LTE_FEC_PF_DRIVER_NAME, fpga_lte_fec_pci_pf_driver);
2667 RTE_PMD_REGISTER_PCI_TABLE(FPGA_LTE_FEC_PF_DRIVER_NAME,
2668 		pci_id_fpga_lte_fec_pf_map);
2669 RTE_PMD_REGISTER_PCI(FPGA_LTE_FEC_VF_DRIVER_NAME, fpga_lte_fec_pci_vf_driver);
2670 RTE_PMD_REGISTER_PCI_TABLE(FPGA_LTE_FEC_VF_DRIVER_NAME,
2671 		pci_id_fpga_lte_fec_vf_map);
2672