2 * Copyright (c) 2016 QLogic Corporation.
6 * See LICENSE.qede_pmd for copyright and licensing details.
9 #ifndef __ECORE_CHAIN_H__
10 #define __ECORE_CHAIN_H__
12 #include <assert.h> /* @DPDK */
14 #include "common_hsi.h"
15 #include "ecore_utils.h"
17 enum ecore_chain_mode {
18 /* Each Page contains a next pointer at its end */
19 ECORE_CHAIN_MODE_NEXT_PTR,
21 /* Chain is a single page (next ptr) is unrequired */
22 ECORE_CHAIN_MODE_SINGLE,
24 /* Page pointers are located in a side list */
28 enum ecore_chain_use_mode {
29 ECORE_CHAIN_USE_TO_PRODUCE, /* Chain starts empty */
30 ECORE_CHAIN_USE_TO_CONSUME, /* Chain starts full */
31 ECORE_CHAIN_USE_TO_CONSUME_PRODUCE, /* Chain starts empty */
34 enum ecore_chain_cnt_type {
35 /* The chain's size/prod/cons are kept in 16-bit variables */
36 ECORE_CHAIN_CNT_TYPE_U16,
38 /* The chain's size/prod/cons are kept in 32-bit variables */
39 ECORE_CHAIN_CNT_TYPE_U32,
42 struct ecore_chain_next {
43 struct regpair next_phys;
47 struct ecore_chain_pbl_u16 {
52 struct ecore_chain_pbl_u32 {
57 struct ecore_chain_ext_pbl {
58 dma_addr_t p_pbl_phys;
62 struct ecore_chain_pbl {
63 /* Base address of a pre-allocated buffer for pbl */
64 dma_addr_t p_phys_table;
67 /* Table for keeping the virtual addresses of the chain pages,
68 * respectively to the physical addresses in the pbl table.
70 void **pp_virt_addr_tbl;
72 /* Index to current used page by producer/consumer */
74 struct ecore_chain_pbl_u16 pbl16;
75 struct ecore_chain_pbl_u32 pbl32;
81 struct ecore_chain_u16 {
82 /* Cyclic index of next element to produce/consme */
87 struct ecore_chain_u32 {
88 /* Cyclic index of next element to produce/consme */
94 /* Address of first page of the chain */
96 dma_addr_t p_phys_addr;
98 /* Point to next element to produce/consume */
102 enum ecore_chain_mode mode;
103 enum ecore_chain_use_mode intended_use;
105 enum ecore_chain_cnt_type cnt_type;
107 struct ecore_chain_u16 chain16;
108 struct ecore_chain_u32 chain32;
113 /* Number of elements - capacity is for usable elements only,
114 * while size will contain total number of elements [for entire chain].
119 /* Elements information for fast calculations */
121 u16 elem_per_page_mask;
127 struct ecore_chain_pbl pbl;
132 #define ECORE_CHAIN_PBL_ENTRY_SIZE (8)
133 #define ECORE_CHAIN_PAGE_SIZE (0x1000)
134 #define ELEMS_PER_PAGE(elem_size) (ECORE_CHAIN_PAGE_SIZE / (elem_size))
136 #define UNUSABLE_ELEMS_PER_PAGE(elem_size, mode) \
137 ((mode == ECORE_CHAIN_MODE_NEXT_PTR) ? \
138 (1 + ((sizeof(struct ecore_chain_next) - 1) / \
141 #define USABLE_ELEMS_PER_PAGE(elem_size, mode) \
142 ((u32)(ELEMS_PER_PAGE(elem_size) - \
143 UNUSABLE_ELEMS_PER_PAGE(elem_size, mode)))
145 #define ECORE_CHAIN_PAGE_CNT(elem_cnt, elem_size, mode) \
146 DIV_ROUND_UP(elem_cnt, USABLE_ELEMS_PER_PAGE(elem_size, mode))
148 #define is_chain_u16(p) ((p)->cnt_type == ECORE_CHAIN_CNT_TYPE_U16)
149 #define is_chain_u32(p) ((p)->cnt_type == ECORE_CHAIN_CNT_TYPE_U32)
152 static OSAL_INLINE u16 ecore_chain_get_prod_idx(struct ecore_chain *p_chain)
154 OSAL_ASSERT(is_chain_u16(p_chain));
155 return p_chain->u.chain16.prod_idx;
158 static OSAL_INLINE u32 ecore_chain_get_prod_idx_u32(struct ecore_chain *p_chain)
160 OSAL_ASSERT(is_chain_u32(p_chain));
161 return p_chain->u.chain32.prod_idx;
164 static OSAL_INLINE u16 ecore_chain_get_cons_idx(struct ecore_chain *p_chain)
166 OSAL_ASSERT(is_chain_u16(p_chain));
167 return p_chain->u.chain16.cons_idx;
170 static OSAL_INLINE u32 ecore_chain_get_cons_idx_u32(struct ecore_chain *p_chain)
172 OSAL_ASSERT(is_chain_u32(p_chain));
173 return p_chain->u.chain32.cons_idx;
177 * Should create OSALs for the below definitions.
178 * For Linux, replace them with the existing U16_MAX and U32_MAX, and handle
179 * kernel versions that lack them.
181 #define ECORE_U16_MAX ((u16)~0U)
182 #define ECORE_U32_MAX ((u32)~0U)
184 static OSAL_INLINE u16 ecore_chain_get_elem_left(struct ecore_chain *p_chain)
188 OSAL_ASSERT(is_chain_u16(p_chain));
190 used = (u16)(((u32)ECORE_U16_MAX + 1 +
191 (u32)(p_chain->u.chain16.prod_idx)) -
192 (u32)p_chain->u.chain16.cons_idx);
193 if (p_chain->mode == ECORE_CHAIN_MODE_NEXT_PTR)
194 used -= p_chain->u.chain16.prod_idx / p_chain->elem_per_page -
195 p_chain->u.chain16.cons_idx / p_chain->elem_per_page;
197 return (u16)(p_chain->capacity - used);
200 static OSAL_INLINE u32
201 ecore_chain_get_elem_left_u32(struct ecore_chain *p_chain)
205 OSAL_ASSERT(is_chain_u32(p_chain));
207 used = (u32)(((u64)ECORE_U32_MAX + 1 +
208 (u64)(p_chain->u.chain32.prod_idx)) -
209 (u64)p_chain->u.chain32.cons_idx);
210 if (p_chain->mode == ECORE_CHAIN_MODE_NEXT_PTR)
211 used -= p_chain->u.chain32.prod_idx / p_chain->elem_per_page -
212 p_chain->u.chain32.cons_idx / p_chain->elem_per_page;
214 return p_chain->capacity - used;
217 static OSAL_INLINE u8 ecore_chain_is_full(struct ecore_chain *p_chain)
219 if (is_chain_u16(p_chain))
220 return (ecore_chain_get_elem_left(p_chain) ==
223 return (ecore_chain_get_elem_left_u32(p_chain) ==
227 static OSAL_INLINE u8 ecore_chain_is_empty(struct ecore_chain *p_chain)
229 if (is_chain_u16(p_chain))
230 return (ecore_chain_get_elem_left(p_chain) == 0);
232 return (ecore_chain_get_elem_left_u32(p_chain) == 0);
236 u16 ecore_chain_get_elem_per_page(struct ecore_chain *p_chain)
238 return p_chain->elem_per_page;
242 u16 ecore_chain_get_usable_per_page(struct ecore_chain *p_chain)
244 return p_chain->usable_per_page;
248 u16 ecore_chain_get_unusable_per_page(struct ecore_chain *p_chain)
250 return p_chain->elem_unusable;
253 static OSAL_INLINE u32 ecore_chain_get_size(struct ecore_chain *p_chain)
255 return p_chain->size;
258 static OSAL_INLINE u32 ecore_chain_get_page_cnt(struct ecore_chain *p_chain)
260 return p_chain->page_cnt;
264 dma_addr_t ecore_chain_get_pbl_phys(struct ecore_chain *p_chain)
266 return p_chain->pbl.p_phys_table;
270 * @brief ecore_chain_advance_page -
272 * Advance the next element accros pages for a linked chain
279 static OSAL_INLINE void
280 ecore_chain_advance_page(struct ecore_chain *p_chain, void **p_next_elem,
281 void *idx_to_inc, void *page_to_inc)
283 struct ecore_chain_next *p_next = OSAL_NULL;
286 switch (p_chain->mode) {
287 case ECORE_CHAIN_MODE_NEXT_PTR:
288 p_next = (struct ecore_chain_next *)(*p_next_elem);
289 *p_next_elem = p_next->next_virt;
290 if (is_chain_u16(p_chain))
291 *(u16 *)idx_to_inc += p_chain->elem_unusable;
293 *(u32 *)idx_to_inc += p_chain->elem_unusable;
295 case ECORE_CHAIN_MODE_SINGLE:
296 *p_next_elem = p_chain->p_virt_addr;
298 case ECORE_CHAIN_MODE_PBL:
299 if (is_chain_u16(p_chain)) {
300 if (++(*(u16 *)page_to_inc) == p_chain->page_cnt)
301 *(u16 *)page_to_inc = 0;
302 page_index = *(u16 *)page_to_inc;
304 if (++(*(u32 *)page_to_inc) == p_chain->page_cnt)
305 *(u32 *)page_to_inc = 0;
306 page_index = *(u32 *)page_to_inc;
308 *p_next_elem = p_chain->pbl.pp_virt_addr_tbl[page_index];
312 #define is_unusable_idx(p, idx) \
313 (((p)->u.chain16.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
315 #define is_unusable_idx_u32(p, idx) \
316 (((p)->u.chain32.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
318 #define is_unusable_next_idx(p, idx) \
319 ((((p)->u.chain16.idx + 1) & \
320 (p)->elem_per_page_mask) == (p)->usable_per_page)
322 #define is_unusable_next_idx_u32(p, idx) \
323 ((((p)->u.chain32.idx + 1) & \
324 (p)->elem_per_page_mask) == (p)->usable_per_page)
326 #define test_and_skip(p, idx) \
328 if (is_chain_u16(p)) { \
329 if (is_unusable_idx(p, idx)) \
330 (p)->u.chain16.idx += \
331 (p)->elem_unusable; \
333 if (is_unusable_idx_u32(p, idx)) \
334 (p)->u.chain32.idx += \
335 (p)->elem_unusable; \
340 * @brief ecore_chain_return_multi_produced -
342 * A chain in which the driver "Produces" elements should use this API
343 * to indicate previous produced elements are now consumed.
349 void ecore_chain_return_multi_produced(struct ecore_chain *p_chain, u32 num)
351 if (is_chain_u16(p_chain))
352 p_chain->u.chain16.cons_idx += (u16)num;
354 p_chain->u.chain32.cons_idx += num;
355 test_and_skip(p_chain, cons_idx);
359 * @brief ecore_chain_return_produced -
361 * A chain in which the driver "Produces" elements should use this API
362 * to indicate previous produced elements are now consumed.
366 static OSAL_INLINE void ecore_chain_return_produced(struct ecore_chain *p_chain)
368 if (is_chain_u16(p_chain))
369 p_chain->u.chain16.cons_idx++;
371 p_chain->u.chain32.cons_idx++;
372 test_and_skip(p_chain, cons_idx);
376 * @brief ecore_chain_produce -
378 * A chain in which the driver "Produces" elements should use this to get
379 * a pointer to the next element which can be "Produced". It's driver
380 * responsibility to validate that the chain has room for new element.
384 * @return void*, a pointer to next element
386 static OSAL_INLINE void *ecore_chain_produce(struct ecore_chain *p_chain)
388 void *p_ret = OSAL_NULL, *p_prod_idx, *p_prod_page_idx;
390 if (is_chain_u16(p_chain)) {
391 if ((p_chain->u.chain16.prod_idx &
392 p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
393 p_prod_idx = &p_chain->u.chain16.prod_idx;
394 p_prod_page_idx = &p_chain->pbl.u.pbl16.prod_page_idx;
395 ecore_chain_advance_page(p_chain, &p_chain->p_prod_elem,
396 p_prod_idx, p_prod_page_idx);
398 p_chain->u.chain16.prod_idx++;
400 if ((p_chain->u.chain32.prod_idx &
401 p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
402 p_prod_idx = &p_chain->u.chain32.prod_idx;
403 p_prod_page_idx = &p_chain->pbl.u.pbl32.prod_page_idx;
404 ecore_chain_advance_page(p_chain, &p_chain->p_prod_elem,
405 p_prod_idx, p_prod_page_idx);
407 p_chain->u.chain32.prod_idx++;
410 p_ret = p_chain->p_prod_elem;
411 p_chain->p_prod_elem = (void *)(((u8 *)p_chain->p_prod_elem) +
418 * @brief ecore_chain_get_capacity -
420 * Get the maximum number of BDs in chain
425 * @return number of unusable BDs
427 static OSAL_INLINE u32 ecore_chain_get_capacity(struct ecore_chain *p_chain)
429 return p_chain->capacity;
433 * @brief ecore_chain_recycle_consumed -
435 * Returns an element which was previously consumed;
436 * Increments producers so they could be written to FW.
441 void ecore_chain_recycle_consumed(struct ecore_chain *p_chain)
443 test_and_skip(p_chain, prod_idx);
444 if (is_chain_u16(p_chain))
445 p_chain->u.chain16.prod_idx++;
447 p_chain->u.chain32.prod_idx++;
451 * @brief ecore_chain_consume -
453 * A Chain in which the driver utilizes data written by a different source
454 * (i.e., FW) should use this to access passed buffers.
458 * @return void*, a pointer to the next buffer written
460 static OSAL_INLINE void *ecore_chain_consume(struct ecore_chain *p_chain)
462 void *p_ret = OSAL_NULL, *p_cons_idx, *p_cons_page_idx;
464 if (is_chain_u16(p_chain)) {
465 if ((p_chain->u.chain16.cons_idx &
466 p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
467 p_cons_idx = &p_chain->u.chain16.cons_idx;
468 p_cons_page_idx = &p_chain->pbl.u.pbl16.cons_page_idx;
469 ecore_chain_advance_page(p_chain, &p_chain->p_cons_elem,
470 p_cons_idx, p_cons_page_idx);
472 p_chain->u.chain16.cons_idx++;
474 if ((p_chain->u.chain32.cons_idx &
475 p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
476 p_cons_idx = &p_chain->u.chain32.cons_idx;
477 p_cons_page_idx = &p_chain->pbl.u.pbl32.cons_page_idx;
478 ecore_chain_advance_page(p_chain, &p_chain->p_cons_elem,
479 p_cons_idx, p_cons_page_idx);
481 p_chain->u.chain32.cons_idx++;
484 p_ret = p_chain->p_cons_elem;
485 p_chain->p_cons_elem = (void *)(((u8 *)p_chain->p_cons_elem) +
492 * @brief ecore_chain_reset -
494 * Resets the chain to its start state
496 * @param p_chain pointer to a previously allocted chain
498 static OSAL_INLINE void ecore_chain_reset(struct ecore_chain *p_chain)
502 if (is_chain_u16(p_chain)) {
503 p_chain->u.chain16.prod_idx = 0;
504 p_chain->u.chain16.cons_idx = 0;
506 p_chain->u.chain32.prod_idx = 0;
507 p_chain->u.chain32.cons_idx = 0;
509 p_chain->p_cons_elem = p_chain->p_virt_addr;
510 p_chain->p_prod_elem = p_chain->p_virt_addr;
512 if (p_chain->mode == ECORE_CHAIN_MODE_PBL) {
513 /* Use (page_cnt - 1) as a reset value for the prod/cons page's
514 * indices, to avoid unnecessary page advancing on the first
515 * call to ecore_chain_produce/consume. Instead, the indices
516 * will be advanced to page_cnt and then will be wrapped to 0.
518 u32 reset_val = p_chain->page_cnt - 1;
520 if (is_chain_u16(p_chain)) {
521 p_chain->pbl.u.pbl16.prod_page_idx = (u16)reset_val;
522 p_chain->pbl.u.pbl16.cons_page_idx = (u16)reset_val;
524 p_chain->pbl.u.pbl32.prod_page_idx = reset_val;
525 p_chain->pbl.u.pbl32.cons_page_idx = reset_val;
529 switch (p_chain->intended_use) {
530 case ECORE_CHAIN_USE_TO_CONSUME_PRODUCE:
531 case ECORE_CHAIN_USE_TO_PRODUCE:
535 case ECORE_CHAIN_USE_TO_CONSUME:
536 /* produce empty elements */
537 for (i = 0; i < p_chain->capacity; i++)
538 ecore_chain_recycle_consumed(p_chain);
544 * @brief ecore_chain_init_params -
546 * Initalizes a basic chain struct
549 * @param page_cnt number of pages in the allocated buffer
550 * @param elem_size size of each element in the chain
551 * @param intended_use
556 static OSAL_INLINE void
557 ecore_chain_init_params(struct ecore_chain *p_chain, u32 page_cnt, u8 elem_size,
558 enum ecore_chain_use_mode intended_use,
559 enum ecore_chain_mode mode,
560 enum ecore_chain_cnt_type cnt_type, void *dp_ctx)
562 /* chain fixed parameters */
563 p_chain->p_virt_addr = OSAL_NULL;
564 p_chain->p_phys_addr = 0;
565 p_chain->elem_size = elem_size;
566 p_chain->intended_use = intended_use;
567 p_chain->mode = mode;
568 p_chain->cnt_type = cnt_type;
570 p_chain->elem_per_page = ELEMS_PER_PAGE(elem_size);
571 p_chain->usable_per_page = USABLE_ELEMS_PER_PAGE(elem_size, mode);
572 p_chain->elem_per_page_mask = p_chain->elem_per_page - 1;
573 p_chain->elem_unusable = UNUSABLE_ELEMS_PER_PAGE(elem_size, mode);
574 p_chain->next_page_mask = (p_chain->usable_per_page &
575 p_chain->elem_per_page_mask);
577 p_chain->page_cnt = page_cnt;
578 p_chain->capacity = p_chain->usable_per_page * page_cnt;
579 p_chain->size = p_chain->elem_per_page * page_cnt;
580 p_chain->pbl.external = false;
581 p_chain->pbl.p_phys_table = 0;
582 p_chain->pbl.p_virt_table = OSAL_NULL;
583 p_chain->pbl.pp_virt_addr_tbl = OSAL_NULL;
585 p_chain->dp_ctx = dp_ctx;
589 * @brief ecore_chain_init_mem -
591 * Initalizes a basic chain struct with its chain buffers
594 * @param p_virt_addr virtual address of allocated buffer's beginning
595 * @param p_phys_addr physical address of allocated buffer's beginning
598 static OSAL_INLINE void ecore_chain_init_mem(struct ecore_chain *p_chain,
600 dma_addr_t p_phys_addr)
602 p_chain->p_virt_addr = p_virt_addr;
603 p_chain->p_phys_addr = p_phys_addr;
607 * @brief ecore_chain_init_pbl_mem -
609 * Initalizes a basic chain struct with its pbl buffers
612 * @param p_virt_pbl pointer to a pre allocated side table which will hold
613 * virtual page addresses.
614 * @param p_phys_pbl pointer to a pre-allocated side table which will hold
615 * physical page addresses.
616 * @param pp_virt_addr_tbl
617 * pointer to a pre-allocated side table which will hold
618 * the virtual addresses of the chain pages.
621 static OSAL_INLINE void ecore_chain_init_pbl_mem(struct ecore_chain *p_chain,
623 dma_addr_t p_phys_pbl,
624 void **pp_virt_addr_tbl)
626 p_chain->pbl.p_phys_table = p_phys_pbl;
627 p_chain->pbl.p_virt_table = p_virt_pbl;
628 p_chain->pbl.pp_virt_addr_tbl = pp_virt_addr_tbl;
632 * @brief ecore_chain_init_next_ptr_elem -
634 * Initalizes a next pointer element
637 * @param p_virt_curr virtual address of a chain page of which the next
638 * pointer element is initialized
639 * @param p_virt_next virtual address of the next chain page
640 * @param p_phys_next physical address of the next chain page
643 static OSAL_INLINE void
644 ecore_chain_init_next_ptr_elem(struct ecore_chain *p_chain, void *p_virt_curr,
645 void *p_virt_next, dma_addr_t p_phys_next)
647 struct ecore_chain_next *p_next;
650 size = p_chain->elem_size * p_chain->usable_per_page;
651 p_next = (struct ecore_chain_next *)((u8 *)p_virt_curr + size);
653 DMA_REGPAIR_LE(p_next->next_phys, p_phys_next);
655 p_next->next_virt = p_virt_next;
659 * @brief ecore_chain_get_last_elem -
661 * Returns a pointer to the last element of the chain
667 static OSAL_INLINE void *ecore_chain_get_last_elem(struct ecore_chain *p_chain)
669 struct ecore_chain_next *p_next = OSAL_NULL;
670 void *p_virt_addr = OSAL_NULL;
671 u32 size, last_page_idx;
673 if (!p_chain->p_virt_addr)
676 switch (p_chain->mode) {
677 case ECORE_CHAIN_MODE_NEXT_PTR:
678 size = p_chain->elem_size * p_chain->usable_per_page;
679 p_virt_addr = p_chain->p_virt_addr;
680 p_next = (struct ecore_chain_next *)((u8 *)p_virt_addr + size);
681 while (p_next->next_virt != p_chain->p_virt_addr) {
682 p_virt_addr = p_next->next_virt;
684 (struct ecore_chain_next *)((u8 *)p_virt_addr +
688 case ECORE_CHAIN_MODE_SINGLE:
689 p_virt_addr = p_chain->p_virt_addr;
691 case ECORE_CHAIN_MODE_PBL:
692 last_page_idx = p_chain->page_cnt - 1;
693 p_virt_addr = p_chain->pbl.pp_virt_addr_tbl[last_page_idx];
696 /* p_virt_addr points at this stage to the last page of the chain */
697 size = p_chain->elem_size * (p_chain->usable_per_page - 1);
698 p_virt_addr = ((u8 *)p_virt_addr + size);
704 * @brief ecore_chain_set_prod - sets the prod to the given value
709 static OSAL_INLINE void ecore_chain_set_prod(struct ecore_chain *p_chain,
710 u32 prod_idx, void *p_prod_elem)
712 if (is_chain_u16(p_chain))
713 p_chain->u.chain16.prod_idx = (u16)prod_idx;
715 p_chain->u.chain32.prod_idx = prod_idx;
716 p_chain->p_prod_elem = p_prod_elem;
720 * @brief ecore_chain_pbl_zero_mem - set chain memory to 0
724 static OSAL_INLINE void ecore_chain_pbl_zero_mem(struct ecore_chain *p_chain)
728 if (p_chain->mode != ECORE_CHAIN_MODE_PBL)
731 page_cnt = ecore_chain_get_page_cnt(p_chain);
733 for (i = 0; i < page_cnt; i++)
734 OSAL_MEM_ZERO(p_chain->pbl.pp_virt_addr_tbl[i],
735 ECORE_CHAIN_PAGE_SIZE);
738 int ecore_chain_print(struct ecore_chain *p_chain, char *buffer,
739 u32 buffer_size, u32 *element_indx, u32 stop_indx,
741 int (*func_ptr_print_element)(struct ecore_chain *p_chain,
744 int (*func_ptr_print_metadata)(struct ecore_chain
748 #endif /* __ECORE_CHAIN_H__ */