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_pbl {
58 /* Base address of a pre-allocated buffer for pbl */
59 dma_addr_t p_phys_table;
62 /* Table for keeping the virtual addresses of the chain pages,
63 * respectively to the physical addresses in the pbl table.
65 void **pp_virt_addr_tbl;
67 /* Index to current used page by producer/consumer */
69 struct ecore_chain_pbl_u16 pbl16;
70 struct ecore_chain_pbl_u32 pbl32;
74 struct ecore_chain_u16 {
75 /* Cyclic index of next element to produce/consme */
80 struct ecore_chain_u32 {
81 /* Cyclic index of next element to produce/consme */
87 /* Address of first page of the chain */
89 dma_addr_t p_phys_addr;
91 /* Point to next element to produce/consume */
95 enum ecore_chain_mode mode;
96 enum ecore_chain_use_mode intended_use;
98 enum ecore_chain_cnt_type cnt_type;
100 struct ecore_chain_u16 chain16;
101 struct ecore_chain_u32 chain32;
106 /* Number of elements - capacity is for usable elements only,
107 * while size will contain total number of elements [for entire chain].
112 /* Elements information for fast calculations */
114 u16 elem_per_page_mask;
120 struct ecore_chain_pbl pbl;
123 #define ECORE_CHAIN_PBL_ENTRY_SIZE (8)
124 #define ECORE_CHAIN_PAGE_SIZE (0x1000)
125 #define ELEMS_PER_PAGE(elem_size) (ECORE_CHAIN_PAGE_SIZE / (elem_size))
127 #define UNUSABLE_ELEMS_PER_PAGE(elem_size, mode) \
128 ((mode == ECORE_CHAIN_MODE_NEXT_PTR) ? \
129 (1 + ((sizeof(struct ecore_chain_next) - 1) / \
132 #define USABLE_ELEMS_PER_PAGE(elem_size, mode) \
133 ((u32)(ELEMS_PER_PAGE(elem_size) - \
134 UNUSABLE_ELEMS_PER_PAGE(elem_size, mode)))
136 #define ECORE_CHAIN_PAGE_CNT(elem_cnt, elem_size, mode) \
137 DIV_ROUND_UP(elem_cnt, USABLE_ELEMS_PER_PAGE(elem_size, mode))
139 #define is_chain_u16(p) ((p)->cnt_type == ECORE_CHAIN_CNT_TYPE_U16)
140 #define is_chain_u32(p) ((p)->cnt_type == ECORE_CHAIN_CNT_TYPE_U32)
143 static OSAL_INLINE u16 ecore_chain_get_prod_idx(struct ecore_chain *p_chain)
145 OSAL_ASSERT(is_chain_u16(p_chain));
146 return p_chain->u.chain16.prod_idx;
149 static OSAL_INLINE u32 ecore_chain_get_prod_idx_u32(struct ecore_chain *p_chain)
151 OSAL_ASSERT(is_chain_u32(p_chain));
152 return p_chain->u.chain32.prod_idx;
155 static OSAL_INLINE u16 ecore_chain_get_cons_idx(struct ecore_chain *p_chain)
157 OSAL_ASSERT(is_chain_u16(p_chain));
158 return p_chain->u.chain16.cons_idx;
161 static OSAL_INLINE u32 ecore_chain_get_cons_idx_u32(struct ecore_chain *p_chain)
163 OSAL_ASSERT(is_chain_u32(p_chain));
164 return p_chain->u.chain32.cons_idx;
168 * Should create OSALs for the below definitions.
169 * For Linux, replace them with the existing U16_MAX and U32_MAX, and handle
170 * kernel versions that lack them.
172 #define ECORE_U16_MAX ((u16)~0U)
173 #define ECORE_U32_MAX ((u32)~0U)
175 static OSAL_INLINE u16 ecore_chain_get_elem_left(struct ecore_chain *p_chain)
179 OSAL_ASSERT(is_chain_u16(p_chain));
181 used = (u16)(((u32)ECORE_U16_MAX + 1 +
182 (u32)(p_chain->u.chain16.prod_idx)) -
183 (u32)p_chain->u.chain16.cons_idx);
184 if (p_chain->mode == ECORE_CHAIN_MODE_NEXT_PTR)
185 used -= p_chain->u.chain16.prod_idx / p_chain->elem_per_page -
186 p_chain->u.chain16.cons_idx / p_chain->elem_per_page;
188 return (u16)(p_chain->capacity - used);
191 static OSAL_INLINE u32
192 ecore_chain_get_elem_left_u32(struct ecore_chain *p_chain)
196 OSAL_ASSERT(is_chain_u32(p_chain));
198 used = (u32)(((u64)ECORE_U32_MAX + 1 +
199 (u64)(p_chain->u.chain32.prod_idx)) -
200 (u64)p_chain->u.chain32.cons_idx);
201 if (p_chain->mode == ECORE_CHAIN_MODE_NEXT_PTR)
202 used -= p_chain->u.chain32.prod_idx / p_chain->elem_per_page -
203 p_chain->u.chain32.cons_idx / p_chain->elem_per_page;
205 return p_chain->capacity - used;
208 static OSAL_INLINE u8 ecore_chain_is_full(struct ecore_chain *p_chain)
210 if (is_chain_u16(p_chain))
211 return (ecore_chain_get_elem_left(p_chain) ==
214 return (ecore_chain_get_elem_left_u32(p_chain) ==
218 static OSAL_INLINE u8 ecore_chain_is_empty(struct ecore_chain *p_chain)
220 if (is_chain_u16(p_chain))
221 return (ecore_chain_get_elem_left(p_chain) == 0);
223 return (ecore_chain_get_elem_left_u32(p_chain) == 0);
227 u16 ecore_chain_get_elem_per_page(struct ecore_chain *p_chain)
229 return p_chain->elem_per_page;
233 u16 ecore_chain_get_usable_per_page(struct ecore_chain *p_chain)
235 return p_chain->usable_per_page;
239 u16 ecore_chain_get_unusable_per_page(struct ecore_chain *p_chain)
241 return p_chain->elem_unusable;
244 static OSAL_INLINE u32 ecore_chain_get_size(struct ecore_chain *p_chain)
246 return p_chain->size;
249 static OSAL_INLINE u32 ecore_chain_get_page_cnt(struct ecore_chain *p_chain)
251 return p_chain->page_cnt;
255 * @brief ecore_chain_advance_page -
257 * Advance the next element accros pages for a linked chain
264 static OSAL_INLINE void
265 ecore_chain_advance_page(struct ecore_chain *p_chain, void **p_next_elem,
266 void *idx_to_inc, void *page_to_inc)
268 struct ecore_chain_next *p_next = OSAL_NULL;
271 switch (p_chain->mode) {
272 case ECORE_CHAIN_MODE_NEXT_PTR:
273 p_next = (struct ecore_chain_next *)(*p_next_elem);
274 *p_next_elem = p_next->next_virt;
275 if (is_chain_u16(p_chain))
276 *(u16 *)idx_to_inc += p_chain->elem_unusable;
278 *(u32 *)idx_to_inc += p_chain->elem_unusable;
280 case ECORE_CHAIN_MODE_SINGLE:
281 *p_next_elem = p_chain->p_virt_addr;
283 case ECORE_CHAIN_MODE_PBL:
284 if (is_chain_u16(p_chain)) {
285 if (++(*(u16 *)page_to_inc) == p_chain->page_cnt)
286 *(u16 *)page_to_inc = 0;
287 page_index = *(u16 *)page_to_inc;
289 if (++(*(u32 *)page_to_inc) == p_chain->page_cnt)
290 *(u32 *)page_to_inc = 0;
291 page_index = *(u32 *)page_to_inc;
293 *p_next_elem = p_chain->pbl.pp_virt_addr_tbl[page_index];
297 #define is_unusable_idx(p, idx) \
298 (((p)->u.chain16.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
300 #define is_unusable_idx_u32(p, idx) \
301 (((p)->u.chain32.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
303 #define is_unusable_next_idx(p, idx) \
304 ((((p)->u.chain16.idx + 1) & (p)->elem_per_page_mask) == \
305 (p)->usable_per_page)
307 #define is_unusable_next_idx_u32(p, idx) \
308 ((((p)->u.chain32.idx + 1) & (p)->elem_per_page_mask) \
309 == (p)->usable_per_page)
311 #define test_and_skip(p, idx) \
313 if (is_chain_u16(p)) { \
314 if (is_unusable_idx(p, idx)) \
315 (p)->u.chain16.idx += (p)->elem_unusable; \
317 if (is_unusable_idx_u32(p, idx)) \
318 (p)->u.chain32.idx += (p)->elem_unusable; \
323 * @brief ecore_chain_return_multi_produced -
325 * A chain in which the driver "Produces" elements should use this API
326 * to indicate previous produced elements are now consumed.
332 void ecore_chain_return_multi_produced(struct ecore_chain *p_chain, u32 num)
334 if (is_chain_u16(p_chain))
335 p_chain->u.chain16.cons_idx += (u16)num;
337 p_chain->u.chain32.cons_idx += num;
338 test_and_skip(p_chain, cons_idx);
342 * @brief ecore_chain_return_produced -
344 * A chain in which the driver "Produces" elements should use this API
345 * to indicate previous produced elements are now consumed.
349 static OSAL_INLINE void ecore_chain_return_produced(struct ecore_chain *p_chain)
351 if (is_chain_u16(p_chain))
352 p_chain->u.chain16.cons_idx++;
354 p_chain->u.chain32.cons_idx++;
355 test_and_skip(p_chain, cons_idx);
359 * @brief ecore_chain_produce -
361 * A chain in which the driver "Produces" elements should use this to get
362 * a pointer to the next element which can be "Produced". It's driver
363 * responsibility to validate that the chain has room for new element.
367 * @return void*, a pointer to next element
369 static OSAL_INLINE void *ecore_chain_produce(struct ecore_chain *p_chain)
371 void *p_ret = OSAL_NULL, *p_prod_idx, *p_prod_page_idx;
373 if (is_chain_u16(p_chain)) {
374 if ((p_chain->u.chain16.prod_idx &
375 p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
376 p_prod_idx = &p_chain->u.chain16.prod_idx;
377 p_prod_page_idx = &p_chain->pbl.u.pbl16.prod_page_idx;
378 ecore_chain_advance_page(p_chain, &p_chain->p_prod_elem,
379 p_prod_idx, p_prod_page_idx);
381 p_chain->u.chain16.prod_idx++;
383 if ((p_chain->u.chain32.prod_idx &
384 p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
385 p_prod_idx = &p_chain->u.chain32.prod_idx;
386 p_prod_page_idx = &p_chain->pbl.u.pbl32.prod_page_idx;
387 ecore_chain_advance_page(p_chain, &p_chain->p_prod_elem,
388 p_prod_idx, p_prod_page_idx);
390 p_chain->u.chain32.prod_idx++;
393 p_ret = p_chain->p_prod_elem;
394 p_chain->p_prod_elem = (void *)(((u8 *)p_chain->p_prod_elem) +
401 * @brief ecore_chain_get_capacity -
403 * Get the maximum number of BDs in chain
408 * @return number of unusable BDs
410 static OSAL_INLINE u32 ecore_chain_get_capacity(struct ecore_chain *p_chain)
412 return p_chain->capacity;
416 * @brief ecore_chain_recycle_consumed -
418 * Returns an element which was previously consumed;
419 * Increments producers so they could be written to FW.
424 void ecore_chain_recycle_consumed(struct ecore_chain *p_chain)
426 test_and_skip(p_chain, prod_idx);
427 if (is_chain_u16(p_chain))
428 p_chain->u.chain16.prod_idx++;
430 p_chain->u.chain32.prod_idx++;
434 * @brief ecore_chain_consume -
436 * A Chain in which the driver utilizes data written by a different source
437 * (i.e., FW) should use this to access passed buffers.
441 * @return void*, a pointer to the next buffer written
443 static OSAL_INLINE void *ecore_chain_consume(struct ecore_chain *p_chain)
445 void *p_ret = OSAL_NULL, *p_cons_idx, *p_cons_page_idx;
447 if (is_chain_u16(p_chain)) {
448 if ((p_chain->u.chain16.cons_idx &
449 p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
450 p_cons_idx = &p_chain->u.chain16.cons_idx;
451 p_cons_page_idx = &p_chain->pbl.u.pbl16.cons_page_idx;
452 ecore_chain_advance_page(p_chain, &p_chain->p_cons_elem,
453 p_cons_idx, p_cons_page_idx);
455 p_chain->u.chain16.cons_idx++;
457 if ((p_chain->u.chain32.cons_idx &
458 p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
459 p_cons_idx = &p_chain->u.chain32.cons_idx;
460 p_cons_page_idx = &p_chain->pbl.u.pbl32.cons_page_idx;
461 ecore_chain_advance_page(p_chain, &p_chain->p_cons_elem,
462 p_cons_idx, p_cons_page_idx);
464 p_chain->u.chain32.cons_idx++;
467 p_ret = p_chain->p_cons_elem;
468 p_chain->p_cons_elem = (void *)(((u8 *)p_chain->p_cons_elem) +
475 * @brief ecore_chain_reset -
477 * Resets the chain to its start state
479 * @param p_chain pointer to a previously allocted chain
481 static OSAL_INLINE void ecore_chain_reset(struct ecore_chain *p_chain)
485 if (is_chain_u16(p_chain)) {
486 p_chain->u.chain16.prod_idx = 0;
487 p_chain->u.chain16.cons_idx = 0;
489 p_chain->u.chain32.prod_idx = 0;
490 p_chain->u.chain32.cons_idx = 0;
492 p_chain->p_cons_elem = p_chain->p_virt_addr;
493 p_chain->p_prod_elem = p_chain->p_virt_addr;
495 if (p_chain->mode == ECORE_CHAIN_MODE_PBL) {
496 /* Use (page_cnt - 1) as a reset value for the prod/cons page's
497 * indices, to avoid unnecessary page advancing on the first
498 * call to ecore_chain_produce/consume. Instead, the indices
499 * will be advanced to page_cnt and then will be wrapped to 0.
501 u32 reset_val = p_chain->page_cnt - 1;
503 if (is_chain_u16(p_chain)) {
504 p_chain->pbl.u.pbl16.prod_page_idx = (u16)reset_val;
505 p_chain->pbl.u.pbl16.cons_page_idx = (u16)reset_val;
507 p_chain->pbl.u.pbl32.prod_page_idx = reset_val;
508 p_chain->pbl.u.pbl32.cons_page_idx = reset_val;
512 switch (p_chain->intended_use) {
513 case ECORE_CHAIN_USE_TO_CONSUME_PRODUCE:
514 case ECORE_CHAIN_USE_TO_PRODUCE:
518 case ECORE_CHAIN_USE_TO_CONSUME:
519 /* produce empty elements */
520 for (i = 0; i < p_chain->capacity; i++)
521 ecore_chain_recycle_consumed(p_chain);
527 * @brief ecore_chain_init_params -
529 * Initalizes a basic chain struct
532 * @param page_cnt number of pages in the allocated buffer
533 * @param elem_size size of each element in the chain
534 * @param intended_use
538 static OSAL_INLINE void
539 ecore_chain_init_params(struct ecore_chain *p_chain, u32 page_cnt, u8 elem_size,
540 enum ecore_chain_use_mode intended_use,
541 enum ecore_chain_mode mode,
542 enum ecore_chain_cnt_type cnt_type)
544 /* chain fixed parameters */
545 p_chain->p_virt_addr = OSAL_NULL;
546 p_chain->p_phys_addr = 0;
547 p_chain->elem_size = elem_size;
548 p_chain->intended_use = intended_use;
549 p_chain->mode = mode;
550 p_chain->cnt_type = cnt_type;
552 p_chain->elem_per_page = ELEMS_PER_PAGE(elem_size);
553 p_chain->usable_per_page = USABLE_ELEMS_PER_PAGE(elem_size, mode);
554 p_chain->elem_per_page_mask = p_chain->elem_per_page - 1;
555 p_chain->elem_unusable = UNUSABLE_ELEMS_PER_PAGE(elem_size, mode);
556 p_chain->next_page_mask = (p_chain->usable_per_page &
557 p_chain->elem_per_page_mask);
559 p_chain->page_cnt = page_cnt;
560 p_chain->capacity = p_chain->usable_per_page * page_cnt;
561 p_chain->size = p_chain->elem_per_page * page_cnt;
563 p_chain->pbl.p_phys_table = 0;
564 p_chain->pbl.p_virt_table = OSAL_NULL;
565 p_chain->pbl.pp_virt_addr_tbl = OSAL_NULL;
569 * @brief ecore_chain_init_mem -
571 * Initalizes a basic chain struct with its chain buffers
574 * @param p_virt_addr virtual address of allocated buffer's beginning
575 * @param p_phys_addr physical address of allocated buffer's beginning
578 static OSAL_INLINE void ecore_chain_init_mem(struct ecore_chain *p_chain,
580 dma_addr_t p_phys_addr)
582 p_chain->p_virt_addr = p_virt_addr;
583 p_chain->p_phys_addr = p_phys_addr;
587 * @brief ecore_chain_init_pbl_mem -
589 * Initalizes a basic chain struct with its pbl buffers
592 * @param p_virt_pbl pointer to a pre allocated side table which will hold
593 * virtual page addresses.
594 * @param p_phys_pbl pointer to a pre-allocated side table which will hold
595 * physical page addresses.
596 * @param pp_virt_addr_tbl
597 * pointer to a pre-allocated side table which will hold
598 * the virtual addresses of the chain pages.
601 static OSAL_INLINE void ecore_chain_init_pbl_mem(struct ecore_chain *p_chain,
603 dma_addr_t p_phys_pbl,
604 void **pp_virt_addr_tbl)
606 p_chain->pbl.p_phys_table = p_phys_pbl;
607 p_chain->pbl.p_virt_table = p_virt_pbl;
608 p_chain->pbl.pp_virt_addr_tbl = pp_virt_addr_tbl;
612 * @brief ecore_chain_init_next_ptr_elem -
614 * Initalizes a next pointer element
617 * @param p_virt_curr virtual address of a chain page of which the next
618 * pointer element is initialized
619 * @param p_virt_next virtual address of the next chain page
620 * @param p_phys_next physical address of the next chain page
623 static OSAL_INLINE void
624 ecore_chain_init_next_ptr_elem(struct ecore_chain *p_chain, void *p_virt_curr,
625 void *p_virt_next, dma_addr_t p_phys_next)
627 struct ecore_chain_next *p_next;
630 size = p_chain->elem_size * p_chain->usable_per_page;
631 p_next = (struct ecore_chain_next *)((u8 *)p_virt_curr + size);
633 DMA_REGPAIR_LE(p_next->next_phys, p_phys_next);
635 p_next->next_virt = p_virt_next;
639 * @brief ecore_chain_get_last_elem -
641 * Returns a pointer to the last element of the chain
647 static OSAL_INLINE void *ecore_chain_get_last_elem(struct ecore_chain *p_chain)
649 struct ecore_chain_next *p_next = OSAL_NULL;
650 void *p_virt_addr = OSAL_NULL;
651 u32 size, last_page_idx;
653 if (!p_chain->p_virt_addr)
656 switch (p_chain->mode) {
657 case ECORE_CHAIN_MODE_NEXT_PTR:
658 size = p_chain->elem_size * p_chain->usable_per_page;
659 p_virt_addr = p_chain->p_virt_addr;
660 p_next = (struct ecore_chain_next *)((u8 *)p_virt_addr + size);
661 while (p_next->next_virt != p_chain->p_virt_addr) {
662 p_virt_addr = p_next->next_virt;
664 (struct ecore_chain_next *)((u8 *)p_virt_addr +
668 case ECORE_CHAIN_MODE_SINGLE:
669 p_virt_addr = p_chain->p_virt_addr;
671 case ECORE_CHAIN_MODE_PBL:
672 last_page_idx = p_chain->page_cnt - 1;
673 p_virt_addr = p_chain->pbl.pp_virt_addr_tbl[last_page_idx];
676 /* p_virt_addr points at this stage to the last page of the chain */
677 size = p_chain->elem_size * (p_chain->usable_per_page - 1);
678 p_virt_addr = ((u8 *)p_virt_addr + size);
684 * @brief ecore_chain_set_prod - sets the prod to the given value
689 static OSAL_INLINE void ecore_chain_set_prod(struct ecore_chain *p_chain,
690 u32 prod_idx, void *p_prod_elem)
692 if (is_chain_u16(p_chain))
693 p_chain->u.chain16.prod_idx = (u16)prod_idx;
695 p_chain->u.chain32.prod_idx = prod_idx;
696 p_chain->p_prod_elem = p_prod_elem;
700 * @brief ecore_chain_pbl_zero_mem - set chain memory to 0
704 static OSAL_INLINE void ecore_chain_pbl_zero_mem(struct ecore_chain *p_chain)
708 if (p_chain->mode != ECORE_CHAIN_MODE_PBL)
711 page_cnt = ecore_chain_get_page_cnt(p_chain);
713 for (i = 0; i < page_cnt; i++)
714 OSAL_MEM_ZERO(p_chain->pbl.pp_virt_addr_tbl[i],
715 ECORE_CHAIN_PAGE_SIZE);
718 #endif /* __ECORE_CHAIN_H__ */