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_u16 {
63 /* Cyclic index of next element to produce/consme */
68 struct ecore_chain_u32 {
69 /* Cyclic index of next element to produce/consme */
75 /* fastpath portion of the chain - required for commands such
76 * as produce / consume.
78 /* Point to next element to produce/consume */
82 /* Fastpath portions of the PBL [if exists] */
85 /* Table for keeping the virtual addresses of the chain pages,
86 * respectively to the physical addresses in the pbl table.
88 void **pp_virt_addr_tbl;
91 struct ecore_chain_pbl_u16 u16;
92 struct ecore_chain_pbl_u32 u32;
97 struct ecore_chain_u16 chain16;
98 struct ecore_chain_u32 chain32;
101 /* Capacity counts only usable elements */
105 /* A u8 would suffice for mode, but it would save as a lot of headaches
106 * on castings & defaults.
108 enum ecore_chain_mode mode;
110 /* Elements information for fast calculations */
112 u16 elem_per_page_mask;
120 /* Slowpath of the chain - required for initialization and destruction,
121 * but isn't involved in regular functionality.
124 /* Base address of a pre-allocated buffer for pbl */
126 dma_addr_t p_phys_table;
130 /* Address of first page of the chain - the address is required
131 * for fastpath operation [consume/produce] but only for the the SINGLE
132 * flavour which isn't considered fastpath [== SPQ].
135 dma_addr_t p_phys_addr;
137 /* Total number of elements [for entire chain] */
142 /* TBD - do we really need this? Couldn't find usage for it */
148 #define ECORE_CHAIN_PBL_ENTRY_SIZE (8)
149 #define ECORE_CHAIN_PAGE_SIZE (0x1000)
150 #define ELEMS_PER_PAGE(elem_size) (ECORE_CHAIN_PAGE_SIZE / (elem_size))
152 #define UNUSABLE_ELEMS_PER_PAGE(elem_size, mode) \
153 ((mode == ECORE_CHAIN_MODE_NEXT_PTR) ? \
154 (u8)(1 + ((sizeof(struct ecore_chain_next) - 1) / \
157 #define USABLE_ELEMS_PER_PAGE(elem_size, mode) \
158 ((u32)(ELEMS_PER_PAGE(elem_size) - \
159 UNUSABLE_ELEMS_PER_PAGE(elem_size, mode)))
161 #define ECORE_CHAIN_PAGE_CNT(elem_cnt, elem_size, mode) \
162 DIV_ROUND_UP(elem_cnt, USABLE_ELEMS_PER_PAGE(elem_size, mode))
164 #define is_chain_u16(p) ((p)->cnt_type == ECORE_CHAIN_CNT_TYPE_U16)
165 #define is_chain_u32(p) ((p)->cnt_type == ECORE_CHAIN_CNT_TYPE_U32)
168 static OSAL_INLINE u16 ecore_chain_get_prod_idx(struct ecore_chain *p_chain)
170 OSAL_ASSERT(is_chain_u16(p_chain));
171 return p_chain->u.chain16.prod_idx;
174 static OSAL_INLINE u32 ecore_chain_get_prod_idx_u32(struct ecore_chain *p_chain)
176 OSAL_ASSERT(is_chain_u32(p_chain));
177 return p_chain->u.chain32.prod_idx;
180 static OSAL_INLINE u16 ecore_chain_get_cons_idx(struct ecore_chain *p_chain)
182 OSAL_ASSERT(is_chain_u16(p_chain));
183 return p_chain->u.chain16.cons_idx;
186 static OSAL_INLINE u32 ecore_chain_get_cons_idx_u32(struct ecore_chain *p_chain)
188 OSAL_ASSERT(is_chain_u32(p_chain));
189 return p_chain->u.chain32.cons_idx;
193 * Should create OSALs for the below definitions.
194 * For Linux, replace them with the existing U16_MAX and U32_MAX, and handle
195 * kernel versions that lack them.
197 #define ECORE_U16_MAX ((u16)~0U)
198 #define ECORE_U32_MAX ((u32)~0U)
200 static OSAL_INLINE u16 ecore_chain_get_elem_left(struct ecore_chain *p_chain)
204 OSAL_ASSERT(is_chain_u16(p_chain));
206 used = (u16)(((u32)ECORE_U16_MAX + 1 +
207 (u32)(p_chain->u.chain16.prod_idx)) -
208 (u32)p_chain->u.chain16.cons_idx);
209 if (p_chain->mode == ECORE_CHAIN_MODE_NEXT_PTR)
210 used -= p_chain->u.chain16.prod_idx / p_chain->elem_per_page -
211 p_chain->u.chain16.cons_idx / p_chain->elem_per_page;
213 return (u16)(p_chain->capacity - used);
216 static OSAL_INLINE u32
217 ecore_chain_get_elem_left_u32(struct ecore_chain *p_chain)
221 OSAL_ASSERT(is_chain_u32(p_chain));
223 used = (u32)(((u64)ECORE_U32_MAX + 1 +
224 (u64)(p_chain->u.chain32.prod_idx)) -
225 (u64)p_chain->u.chain32.cons_idx);
226 if (p_chain->mode == ECORE_CHAIN_MODE_NEXT_PTR)
227 used -= p_chain->u.chain32.prod_idx / p_chain->elem_per_page -
228 p_chain->u.chain32.cons_idx / p_chain->elem_per_page;
230 return p_chain->capacity - used;
233 static OSAL_INLINE u8 ecore_chain_is_full(struct ecore_chain *p_chain)
235 if (is_chain_u16(p_chain))
236 return (ecore_chain_get_elem_left(p_chain) ==
239 return (ecore_chain_get_elem_left_u32(p_chain) ==
243 static OSAL_INLINE u8 ecore_chain_is_empty(struct ecore_chain *p_chain)
245 if (is_chain_u16(p_chain))
246 return (ecore_chain_get_elem_left(p_chain) == 0);
248 return (ecore_chain_get_elem_left_u32(p_chain) == 0);
252 u16 ecore_chain_get_elem_per_page(struct ecore_chain *p_chain)
254 return p_chain->elem_per_page;
258 u16 ecore_chain_get_usable_per_page(struct ecore_chain *p_chain)
260 return p_chain->usable_per_page;
264 u8 ecore_chain_get_unusable_per_page(struct ecore_chain *p_chain)
266 return p_chain->elem_unusable;
269 static OSAL_INLINE u32 ecore_chain_get_size(struct ecore_chain *p_chain)
271 return p_chain->size;
274 static OSAL_INLINE u32 ecore_chain_get_page_cnt(struct ecore_chain *p_chain)
276 return p_chain->page_cnt;
280 dma_addr_t ecore_chain_get_pbl_phys(struct ecore_chain *p_chain)
282 return p_chain->pbl_sp.p_phys_table;
286 * @brief ecore_chain_advance_page -
288 * Advance the next element accros pages for a linked chain
295 static OSAL_INLINE void
296 ecore_chain_advance_page(struct ecore_chain *p_chain, void **p_next_elem,
297 void *idx_to_inc, void *page_to_inc)
299 struct ecore_chain_next *p_next = OSAL_NULL;
302 switch (p_chain->mode) {
303 case ECORE_CHAIN_MODE_NEXT_PTR:
304 p_next = (struct ecore_chain_next *)(*p_next_elem);
305 *p_next_elem = p_next->next_virt;
306 if (is_chain_u16(p_chain))
307 *(u16 *)idx_to_inc += (u16)p_chain->elem_unusable;
309 *(u32 *)idx_to_inc += (u16)p_chain->elem_unusable;
311 case ECORE_CHAIN_MODE_SINGLE:
312 *p_next_elem = p_chain->p_virt_addr;
314 case ECORE_CHAIN_MODE_PBL:
315 if (is_chain_u16(p_chain)) {
316 if (++(*(u16 *)page_to_inc) == p_chain->page_cnt)
317 *(u16 *)page_to_inc = 0;
318 page_index = *(u16 *)page_to_inc;
320 if (++(*(u32 *)page_to_inc) == p_chain->page_cnt)
321 *(u32 *)page_to_inc = 0;
322 page_index = *(u32 *)page_to_inc;
324 *p_next_elem = p_chain->pbl.pp_virt_addr_tbl[page_index];
328 #define is_unusable_idx(p, idx) \
329 (((p)->u.chain16.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
331 #define is_unusable_idx_u32(p, idx) \
332 (((p)->u.chain32.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
334 #define is_unusable_next_idx(p, idx) \
335 ((((p)->u.chain16.idx + 1) & \
336 (p)->elem_per_page_mask) == (p)->usable_per_page)
338 #define is_unusable_next_idx_u32(p, idx) \
339 ((((p)->u.chain32.idx + 1) & \
340 (p)->elem_per_page_mask) == (p)->usable_per_page)
342 #define test_and_skip(p, idx) \
344 if (is_chain_u16(p)) { \
345 if (is_unusable_idx(p, idx)) \
346 (p)->u.chain16.idx += \
347 (p)->elem_unusable; \
349 if (is_unusable_idx_u32(p, idx)) \
350 (p)->u.chain32.idx += \
351 (p)->elem_unusable; \
356 * @brief ecore_chain_return_multi_produced -
358 * A chain in which the driver "Produces" elements should use this API
359 * to indicate previous produced elements are now consumed.
365 void ecore_chain_return_multi_produced(struct ecore_chain *p_chain, u32 num)
367 if (is_chain_u16(p_chain))
368 p_chain->u.chain16.cons_idx += (u16)num;
370 p_chain->u.chain32.cons_idx += num;
371 test_and_skip(p_chain, cons_idx);
375 * @brief ecore_chain_return_produced -
377 * A chain in which the driver "Produces" elements should use this API
378 * to indicate previous produced elements are now consumed.
382 static OSAL_INLINE void ecore_chain_return_produced(struct ecore_chain *p_chain)
384 if (is_chain_u16(p_chain))
385 p_chain->u.chain16.cons_idx++;
387 p_chain->u.chain32.cons_idx++;
388 test_and_skip(p_chain, cons_idx);
392 * @brief ecore_chain_produce -
394 * A chain in which the driver "Produces" elements should use this to get
395 * a pointer to the next element which can be "Produced". It's driver
396 * responsibility to validate that the chain has room for new element.
400 * @return void*, a pointer to next element
402 static OSAL_INLINE void *ecore_chain_produce(struct ecore_chain *p_chain)
404 void *p_ret = OSAL_NULL, *p_prod_idx, *p_prod_page_idx;
406 if (is_chain_u16(p_chain)) {
407 if ((p_chain->u.chain16.prod_idx &
408 p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
409 p_prod_idx = &p_chain->u.chain16.prod_idx;
410 p_prod_page_idx = &p_chain->pbl.c.u16.prod_page_idx;
411 ecore_chain_advance_page(p_chain, &p_chain->p_prod_elem,
412 p_prod_idx, p_prod_page_idx);
414 p_chain->u.chain16.prod_idx++;
416 if ((p_chain->u.chain32.prod_idx &
417 p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
418 p_prod_idx = &p_chain->u.chain32.prod_idx;
419 p_prod_page_idx = &p_chain->pbl.c.u32.prod_page_idx;
420 ecore_chain_advance_page(p_chain, &p_chain->p_prod_elem,
421 p_prod_idx, p_prod_page_idx);
423 p_chain->u.chain32.prod_idx++;
426 p_ret = p_chain->p_prod_elem;
427 p_chain->p_prod_elem = (void *)(((u8 *)p_chain->p_prod_elem) +
434 * @brief ecore_chain_get_capacity -
436 * Get the maximum number of BDs in chain
441 * @return number of unusable BDs
443 static OSAL_INLINE u32 ecore_chain_get_capacity(struct ecore_chain *p_chain)
445 return p_chain->capacity;
449 * @brief ecore_chain_recycle_consumed -
451 * Returns an element which was previously consumed;
452 * Increments producers so they could be written to FW.
457 void ecore_chain_recycle_consumed(struct ecore_chain *p_chain)
459 test_and_skip(p_chain, prod_idx);
460 if (is_chain_u16(p_chain))
461 p_chain->u.chain16.prod_idx++;
463 p_chain->u.chain32.prod_idx++;
467 * @brief ecore_chain_consume -
469 * A Chain in which the driver utilizes data written by a different source
470 * (i.e., FW) should use this to access passed buffers.
474 * @return void*, a pointer to the next buffer written
476 static OSAL_INLINE void *ecore_chain_consume(struct ecore_chain *p_chain)
478 void *p_ret = OSAL_NULL, *p_cons_idx, *p_cons_page_idx;
480 if (is_chain_u16(p_chain)) {
481 if ((p_chain->u.chain16.cons_idx &
482 p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
483 p_cons_idx = &p_chain->u.chain16.cons_idx;
484 p_cons_page_idx = &p_chain->pbl.c.u16.cons_page_idx;
485 ecore_chain_advance_page(p_chain, &p_chain->p_cons_elem,
486 p_cons_idx, p_cons_page_idx);
488 p_chain->u.chain16.cons_idx++;
490 if ((p_chain->u.chain32.cons_idx &
491 p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
492 p_cons_idx = &p_chain->u.chain32.cons_idx;
493 p_cons_page_idx = &p_chain->pbl.c.u32.cons_page_idx;
494 ecore_chain_advance_page(p_chain, &p_chain->p_cons_elem,
495 p_cons_idx, p_cons_page_idx);
497 p_chain->u.chain32.cons_idx++;
500 p_ret = p_chain->p_cons_elem;
501 p_chain->p_cons_elem = (void *)(((u8 *)p_chain->p_cons_elem) +
508 * @brief ecore_chain_reset -
510 * Resets the chain to its start state
512 * @param p_chain pointer to a previously allocted chain
514 static OSAL_INLINE void ecore_chain_reset(struct ecore_chain *p_chain)
518 if (is_chain_u16(p_chain)) {
519 p_chain->u.chain16.prod_idx = 0;
520 p_chain->u.chain16.cons_idx = 0;
522 p_chain->u.chain32.prod_idx = 0;
523 p_chain->u.chain32.cons_idx = 0;
525 p_chain->p_cons_elem = p_chain->p_virt_addr;
526 p_chain->p_prod_elem = p_chain->p_virt_addr;
528 if (p_chain->mode == ECORE_CHAIN_MODE_PBL) {
529 /* Use (page_cnt - 1) as a reset value for the prod/cons page's
530 * indices, to avoid unnecessary page advancing on the first
531 * call to ecore_chain_produce/consume. Instead, the indices
532 * will be advanced to page_cnt and then will be wrapped to 0.
534 u32 reset_val = p_chain->page_cnt - 1;
536 if (is_chain_u16(p_chain)) {
537 p_chain->pbl.c.u16.prod_page_idx = (u16)reset_val;
538 p_chain->pbl.c.u16.cons_page_idx = (u16)reset_val;
540 p_chain->pbl.c.u32.prod_page_idx = reset_val;
541 p_chain->pbl.c.u32.cons_page_idx = reset_val;
545 switch (p_chain->intended_use) {
546 case ECORE_CHAIN_USE_TO_CONSUME:
547 /* produce empty elements */
548 for (i = 0; i < p_chain->capacity; i++)
549 ecore_chain_recycle_consumed(p_chain);
552 case ECORE_CHAIN_USE_TO_CONSUME_PRODUCE:
553 case ECORE_CHAIN_USE_TO_PRODUCE:
561 * @brief ecore_chain_init_params -
563 * Initalizes a basic chain struct
566 * @param page_cnt number of pages in the allocated buffer
567 * @param elem_size size of each element in the chain
568 * @param intended_use
573 static OSAL_INLINE void
574 ecore_chain_init_params(struct ecore_chain *p_chain, u32 page_cnt, u8 elem_size,
575 enum ecore_chain_use_mode intended_use,
576 enum ecore_chain_mode mode,
577 enum ecore_chain_cnt_type cnt_type, void *dp_ctx)
579 /* chain fixed parameters */
580 p_chain->p_virt_addr = OSAL_NULL;
581 p_chain->p_phys_addr = 0;
582 p_chain->elem_size = elem_size;
583 p_chain->intended_use = (u8)intended_use;
584 p_chain->mode = mode;
585 p_chain->cnt_type = (u8)cnt_type;
587 p_chain->elem_per_page = ELEMS_PER_PAGE(elem_size);
588 p_chain->usable_per_page = USABLE_ELEMS_PER_PAGE(elem_size, mode);
589 p_chain->elem_per_page_mask = p_chain->elem_per_page - 1;
590 p_chain->elem_unusable = UNUSABLE_ELEMS_PER_PAGE(elem_size, mode);
591 p_chain->next_page_mask = (p_chain->usable_per_page &
592 p_chain->elem_per_page_mask);
594 p_chain->page_cnt = page_cnt;
595 p_chain->capacity = p_chain->usable_per_page * page_cnt;
596 p_chain->size = p_chain->elem_per_page * page_cnt;
597 p_chain->b_external_pbl = false;
598 p_chain->pbl_sp.p_phys_table = 0;
599 p_chain->pbl_sp.p_virt_table = OSAL_NULL;
600 p_chain->pbl.pp_virt_addr_tbl = OSAL_NULL;
602 p_chain->dp_ctx = dp_ctx;
606 * @brief ecore_chain_init_mem -
608 * Initalizes a basic chain struct with its chain buffers
611 * @param p_virt_addr virtual address of allocated buffer's beginning
612 * @param p_phys_addr physical address of allocated buffer's beginning
615 static OSAL_INLINE void ecore_chain_init_mem(struct ecore_chain *p_chain,
617 dma_addr_t p_phys_addr)
619 p_chain->p_virt_addr = p_virt_addr;
620 p_chain->p_phys_addr = p_phys_addr;
624 * @brief ecore_chain_init_pbl_mem -
626 * Initalizes a basic chain struct with its pbl buffers
629 * @param p_virt_pbl pointer to a pre allocated side table which will hold
630 * virtual page addresses.
631 * @param p_phys_pbl pointer to a pre-allocated side table which will hold
632 * physical page addresses.
633 * @param pp_virt_addr_tbl
634 * pointer to a pre-allocated side table which will hold
635 * the virtual addresses of the chain pages.
638 static OSAL_INLINE void ecore_chain_init_pbl_mem(struct ecore_chain *p_chain,
640 dma_addr_t p_phys_pbl,
641 void **pp_virt_addr_tbl)
643 p_chain->pbl_sp.p_phys_table = p_phys_pbl;
644 p_chain->pbl_sp.p_virt_table = p_virt_pbl;
645 p_chain->pbl.pp_virt_addr_tbl = pp_virt_addr_tbl;
649 * @brief ecore_chain_init_next_ptr_elem -
651 * Initalizes a next pointer element
654 * @param p_virt_curr virtual address of a chain page of which the next
655 * pointer element is initialized
656 * @param p_virt_next virtual address of the next chain page
657 * @param p_phys_next physical address of the next chain page
660 static OSAL_INLINE void
661 ecore_chain_init_next_ptr_elem(struct ecore_chain *p_chain, void *p_virt_curr,
662 void *p_virt_next, dma_addr_t p_phys_next)
664 struct ecore_chain_next *p_next;
667 size = p_chain->elem_size * p_chain->usable_per_page;
668 p_next = (struct ecore_chain_next *)((u8 *)p_virt_curr + size);
670 DMA_REGPAIR_LE(p_next->next_phys, p_phys_next);
672 p_next->next_virt = p_virt_next;
676 * @brief ecore_chain_get_last_elem -
678 * Returns a pointer to the last element of the chain
684 static OSAL_INLINE void *ecore_chain_get_last_elem(struct ecore_chain *p_chain)
686 struct ecore_chain_next *p_next = OSAL_NULL;
687 void *p_virt_addr = OSAL_NULL;
688 u32 size, last_page_idx;
690 if (!p_chain->p_virt_addr)
693 switch (p_chain->mode) {
694 case ECORE_CHAIN_MODE_NEXT_PTR:
695 size = p_chain->elem_size * p_chain->usable_per_page;
696 p_virt_addr = p_chain->p_virt_addr;
697 p_next = (struct ecore_chain_next *)((u8 *)p_virt_addr + size);
698 while (p_next->next_virt != p_chain->p_virt_addr) {
699 p_virt_addr = p_next->next_virt;
701 (struct ecore_chain_next *)((u8 *)p_virt_addr +
705 case ECORE_CHAIN_MODE_SINGLE:
706 p_virt_addr = p_chain->p_virt_addr;
708 case ECORE_CHAIN_MODE_PBL:
709 last_page_idx = p_chain->page_cnt - 1;
710 p_virt_addr = p_chain->pbl.pp_virt_addr_tbl[last_page_idx];
713 /* p_virt_addr points at this stage to the last page of the chain */
714 size = p_chain->elem_size * (p_chain->usable_per_page - 1);
715 p_virt_addr = ((u8 *)p_virt_addr + size);
721 * @brief ecore_chain_set_prod - sets the prod to the given value
726 static OSAL_INLINE void ecore_chain_set_prod(struct ecore_chain *p_chain,
727 u32 prod_idx, void *p_prod_elem)
729 if (is_chain_u16(p_chain))
730 p_chain->u.chain16.prod_idx = (u16)prod_idx;
732 p_chain->u.chain32.prod_idx = prod_idx;
733 p_chain->p_prod_elem = p_prod_elem;
737 * @brief ecore_chain_pbl_zero_mem - set chain memory to 0
741 static OSAL_INLINE void ecore_chain_pbl_zero_mem(struct ecore_chain *p_chain)
745 if (p_chain->mode != ECORE_CHAIN_MODE_PBL)
748 page_cnt = ecore_chain_get_page_cnt(p_chain);
750 for (i = 0; i < page_cnt; i++)
751 OSAL_MEM_ZERO(p_chain->pbl.pp_virt_addr_tbl[i],
752 ECORE_CHAIN_PAGE_SIZE);
755 int ecore_chain_print(struct ecore_chain *p_chain, char *buffer,
756 u32 buffer_size, u32 *element_indx, u32 stop_indx,
758 int (*func_ptr_print_element)(struct ecore_chain *p_chain,
761 int (*func_ptr_print_metadata)(struct ecore_chain
765 #endif /* __ECORE_CHAIN_H__ */