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 dma_addr_t ecore_chain_get_pbl_phys(struct ecore_chain *p_chain)
257 return p_chain->pbl.p_phys_table;
261 * @brief ecore_chain_advance_page -
263 * Advance the next element accros pages for a linked chain
270 static OSAL_INLINE void
271 ecore_chain_advance_page(struct ecore_chain *p_chain, void **p_next_elem,
272 void *idx_to_inc, void *page_to_inc)
274 struct ecore_chain_next *p_next = OSAL_NULL;
277 switch (p_chain->mode) {
278 case ECORE_CHAIN_MODE_NEXT_PTR:
279 p_next = (struct ecore_chain_next *)(*p_next_elem);
280 *p_next_elem = p_next->next_virt;
281 if (is_chain_u16(p_chain))
282 *(u16 *)idx_to_inc += p_chain->elem_unusable;
284 *(u32 *)idx_to_inc += p_chain->elem_unusable;
286 case ECORE_CHAIN_MODE_SINGLE:
287 *p_next_elem = p_chain->p_virt_addr;
289 case ECORE_CHAIN_MODE_PBL:
290 if (is_chain_u16(p_chain)) {
291 if (++(*(u16 *)page_to_inc) == p_chain->page_cnt)
292 *(u16 *)page_to_inc = 0;
293 page_index = *(u16 *)page_to_inc;
295 if (++(*(u32 *)page_to_inc) == p_chain->page_cnt)
296 *(u32 *)page_to_inc = 0;
297 page_index = *(u32 *)page_to_inc;
299 *p_next_elem = p_chain->pbl.pp_virt_addr_tbl[page_index];
303 #define is_unusable_idx(p, idx) \
304 (((p)->u.chain16.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
306 #define is_unusable_idx_u32(p, idx) \
307 (((p)->u.chain32.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
309 #define is_unusable_next_idx(p, idx) \
310 ((((p)->u.chain16.idx + 1) & (p)->elem_per_page_mask) == \
311 (p)->usable_per_page)
313 #define is_unusable_next_idx_u32(p, idx) \
314 ((((p)->u.chain32.idx + 1) & (p)->elem_per_page_mask) \
315 == (p)->usable_per_page)
317 #define test_and_skip(p, idx) \
319 if (is_chain_u16(p)) { \
320 if (is_unusable_idx(p, idx)) \
321 (p)->u.chain16.idx += (p)->elem_unusable; \
323 if (is_unusable_idx_u32(p, idx)) \
324 (p)->u.chain32.idx += (p)->elem_unusable; \
329 * @brief ecore_chain_return_multi_produced -
331 * A chain in which the driver "Produces" elements should use this API
332 * to indicate previous produced elements are now consumed.
338 void ecore_chain_return_multi_produced(struct ecore_chain *p_chain, u32 num)
340 if (is_chain_u16(p_chain))
341 p_chain->u.chain16.cons_idx += (u16)num;
343 p_chain->u.chain32.cons_idx += num;
344 test_and_skip(p_chain, cons_idx);
348 * @brief ecore_chain_return_produced -
350 * A chain in which the driver "Produces" elements should use this API
351 * to indicate previous produced elements are now consumed.
355 static OSAL_INLINE void ecore_chain_return_produced(struct ecore_chain *p_chain)
357 if (is_chain_u16(p_chain))
358 p_chain->u.chain16.cons_idx++;
360 p_chain->u.chain32.cons_idx++;
361 test_and_skip(p_chain, cons_idx);
365 * @brief ecore_chain_produce -
367 * A chain in which the driver "Produces" elements should use this to get
368 * a pointer to the next element which can be "Produced". It's driver
369 * responsibility to validate that the chain has room for new element.
373 * @return void*, a pointer to next element
375 static OSAL_INLINE void *ecore_chain_produce(struct ecore_chain *p_chain)
377 void *p_ret = OSAL_NULL, *p_prod_idx, *p_prod_page_idx;
379 if (is_chain_u16(p_chain)) {
380 if ((p_chain->u.chain16.prod_idx &
381 p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
382 p_prod_idx = &p_chain->u.chain16.prod_idx;
383 p_prod_page_idx = &p_chain->pbl.u.pbl16.prod_page_idx;
384 ecore_chain_advance_page(p_chain, &p_chain->p_prod_elem,
385 p_prod_idx, p_prod_page_idx);
387 p_chain->u.chain16.prod_idx++;
389 if ((p_chain->u.chain32.prod_idx &
390 p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
391 p_prod_idx = &p_chain->u.chain32.prod_idx;
392 p_prod_page_idx = &p_chain->pbl.u.pbl32.prod_page_idx;
393 ecore_chain_advance_page(p_chain, &p_chain->p_prod_elem,
394 p_prod_idx, p_prod_page_idx);
396 p_chain->u.chain32.prod_idx++;
399 p_ret = p_chain->p_prod_elem;
400 p_chain->p_prod_elem = (void *)(((u8 *)p_chain->p_prod_elem) +
407 * @brief ecore_chain_get_capacity -
409 * Get the maximum number of BDs in chain
414 * @return number of unusable BDs
416 static OSAL_INLINE u32 ecore_chain_get_capacity(struct ecore_chain *p_chain)
418 return p_chain->capacity;
422 * @brief ecore_chain_recycle_consumed -
424 * Returns an element which was previously consumed;
425 * Increments producers so they could be written to FW.
430 void ecore_chain_recycle_consumed(struct ecore_chain *p_chain)
432 test_and_skip(p_chain, prod_idx);
433 if (is_chain_u16(p_chain))
434 p_chain->u.chain16.prod_idx++;
436 p_chain->u.chain32.prod_idx++;
440 * @brief ecore_chain_consume -
442 * A Chain in which the driver utilizes data written by a different source
443 * (i.e., FW) should use this to access passed buffers.
447 * @return void*, a pointer to the next buffer written
449 static OSAL_INLINE void *ecore_chain_consume(struct ecore_chain *p_chain)
451 void *p_ret = OSAL_NULL, *p_cons_idx, *p_cons_page_idx;
453 if (is_chain_u16(p_chain)) {
454 if ((p_chain->u.chain16.cons_idx &
455 p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
456 p_cons_idx = &p_chain->u.chain16.cons_idx;
457 p_cons_page_idx = &p_chain->pbl.u.pbl16.cons_page_idx;
458 ecore_chain_advance_page(p_chain, &p_chain->p_cons_elem,
459 p_cons_idx, p_cons_page_idx);
461 p_chain->u.chain16.cons_idx++;
463 if ((p_chain->u.chain32.cons_idx &
464 p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
465 p_cons_idx = &p_chain->u.chain32.cons_idx;
466 p_cons_page_idx = &p_chain->pbl.u.pbl32.cons_page_idx;
467 ecore_chain_advance_page(p_chain, &p_chain->p_cons_elem,
468 p_cons_idx, p_cons_page_idx);
470 p_chain->u.chain32.cons_idx++;
473 p_ret = p_chain->p_cons_elem;
474 p_chain->p_cons_elem = (void *)(((u8 *)p_chain->p_cons_elem) +
481 * @brief ecore_chain_reset -
483 * Resets the chain to its start state
485 * @param p_chain pointer to a previously allocted chain
487 static OSAL_INLINE void ecore_chain_reset(struct ecore_chain *p_chain)
491 if (is_chain_u16(p_chain)) {
492 p_chain->u.chain16.prod_idx = 0;
493 p_chain->u.chain16.cons_idx = 0;
495 p_chain->u.chain32.prod_idx = 0;
496 p_chain->u.chain32.cons_idx = 0;
498 p_chain->p_cons_elem = p_chain->p_virt_addr;
499 p_chain->p_prod_elem = p_chain->p_virt_addr;
501 if (p_chain->mode == ECORE_CHAIN_MODE_PBL) {
502 /* Use (page_cnt - 1) as a reset value for the prod/cons page's
503 * indices, to avoid unnecessary page advancing on the first
504 * call to ecore_chain_produce/consume. Instead, the indices
505 * will be advanced to page_cnt and then will be wrapped to 0.
507 u32 reset_val = p_chain->page_cnt - 1;
509 if (is_chain_u16(p_chain)) {
510 p_chain->pbl.u.pbl16.prod_page_idx = (u16)reset_val;
511 p_chain->pbl.u.pbl16.cons_page_idx = (u16)reset_val;
513 p_chain->pbl.u.pbl32.prod_page_idx = reset_val;
514 p_chain->pbl.u.pbl32.cons_page_idx = reset_val;
518 switch (p_chain->intended_use) {
519 case ECORE_CHAIN_USE_TO_CONSUME_PRODUCE:
520 case ECORE_CHAIN_USE_TO_PRODUCE:
524 case ECORE_CHAIN_USE_TO_CONSUME:
525 /* produce empty elements */
526 for (i = 0; i < p_chain->capacity; i++)
527 ecore_chain_recycle_consumed(p_chain);
533 * @brief ecore_chain_init_params -
535 * Initalizes a basic chain struct
538 * @param page_cnt number of pages in the allocated buffer
539 * @param elem_size size of each element in the chain
540 * @param intended_use
544 static OSAL_INLINE void
545 ecore_chain_init_params(struct ecore_chain *p_chain, u32 page_cnt, u8 elem_size,
546 enum ecore_chain_use_mode intended_use,
547 enum ecore_chain_mode mode,
548 enum ecore_chain_cnt_type cnt_type)
550 /* chain fixed parameters */
551 p_chain->p_virt_addr = OSAL_NULL;
552 p_chain->p_phys_addr = 0;
553 p_chain->elem_size = elem_size;
554 p_chain->intended_use = intended_use;
555 p_chain->mode = mode;
556 p_chain->cnt_type = cnt_type;
558 p_chain->elem_per_page = ELEMS_PER_PAGE(elem_size);
559 p_chain->usable_per_page = USABLE_ELEMS_PER_PAGE(elem_size, mode);
560 p_chain->elem_per_page_mask = p_chain->elem_per_page - 1;
561 p_chain->elem_unusable = UNUSABLE_ELEMS_PER_PAGE(elem_size, mode);
562 p_chain->next_page_mask = (p_chain->usable_per_page &
563 p_chain->elem_per_page_mask);
565 p_chain->page_cnt = page_cnt;
566 p_chain->capacity = p_chain->usable_per_page * page_cnt;
567 p_chain->size = p_chain->elem_per_page * page_cnt;
569 p_chain->pbl.p_phys_table = 0;
570 p_chain->pbl.p_virt_table = OSAL_NULL;
571 p_chain->pbl.pp_virt_addr_tbl = OSAL_NULL;
575 * @brief ecore_chain_init_mem -
577 * Initalizes a basic chain struct with its chain buffers
580 * @param p_virt_addr virtual address of allocated buffer's beginning
581 * @param p_phys_addr physical address of allocated buffer's beginning
584 static OSAL_INLINE void ecore_chain_init_mem(struct ecore_chain *p_chain,
586 dma_addr_t p_phys_addr)
588 p_chain->p_virt_addr = p_virt_addr;
589 p_chain->p_phys_addr = p_phys_addr;
593 * @brief ecore_chain_init_pbl_mem -
595 * Initalizes a basic chain struct with its pbl buffers
598 * @param p_virt_pbl pointer to a pre allocated side table which will hold
599 * virtual page addresses.
600 * @param p_phys_pbl pointer to a pre-allocated side table which will hold
601 * physical page addresses.
602 * @param pp_virt_addr_tbl
603 * pointer to a pre-allocated side table which will hold
604 * the virtual addresses of the chain pages.
607 static OSAL_INLINE void ecore_chain_init_pbl_mem(struct ecore_chain *p_chain,
609 dma_addr_t p_phys_pbl,
610 void **pp_virt_addr_tbl)
612 p_chain->pbl.p_phys_table = p_phys_pbl;
613 p_chain->pbl.p_virt_table = p_virt_pbl;
614 p_chain->pbl.pp_virt_addr_tbl = pp_virt_addr_tbl;
618 * @brief ecore_chain_init_next_ptr_elem -
620 * Initalizes a next pointer element
623 * @param p_virt_curr virtual address of a chain page of which the next
624 * pointer element is initialized
625 * @param p_virt_next virtual address of the next chain page
626 * @param p_phys_next physical address of the next chain page
629 static OSAL_INLINE void
630 ecore_chain_init_next_ptr_elem(struct ecore_chain *p_chain, void *p_virt_curr,
631 void *p_virt_next, dma_addr_t p_phys_next)
633 struct ecore_chain_next *p_next;
636 size = p_chain->elem_size * p_chain->usable_per_page;
637 p_next = (struct ecore_chain_next *)((u8 *)p_virt_curr + size);
639 DMA_REGPAIR_LE(p_next->next_phys, p_phys_next);
641 p_next->next_virt = p_virt_next;
645 * @brief ecore_chain_get_last_elem -
647 * Returns a pointer to the last element of the chain
653 static OSAL_INLINE void *ecore_chain_get_last_elem(struct ecore_chain *p_chain)
655 struct ecore_chain_next *p_next = OSAL_NULL;
656 void *p_virt_addr = OSAL_NULL;
657 u32 size, last_page_idx;
659 if (!p_chain->p_virt_addr)
662 switch (p_chain->mode) {
663 case ECORE_CHAIN_MODE_NEXT_PTR:
664 size = p_chain->elem_size * p_chain->usable_per_page;
665 p_virt_addr = p_chain->p_virt_addr;
666 p_next = (struct ecore_chain_next *)((u8 *)p_virt_addr + size);
667 while (p_next->next_virt != p_chain->p_virt_addr) {
668 p_virt_addr = p_next->next_virt;
670 (struct ecore_chain_next *)((u8 *)p_virt_addr +
674 case ECORE_CHAIN_MODE_SINGLE:
675 p_virt_addr = p_chain->p_virt_addr;
677 case ECORE_CHAIN_MODE_PBL:
678 last_page_idx = p_chain->page_cnt - 1;
679 p_virt_addr = p_chain->pbl.pp_virt_addr_tbl[last_page_idx];
682 /* p_virt_addr points at this stage to the last page of the chain */
683 size = p_chain->elem_size * (p_chain->usable_per_page - 1);
684 p_virt_addr = ((u8 *)p_virt_addr + size);
690 * @brief ecore_chain_set_prod - sets the prod to the given value
695 static OSAL_INLINE void ecore_chain_set_prod(struct ecore_chain *p_chain,
696 u32 prod_idx, void *p_prod_elem)
698 if (is_chain_u16(p_chain))
699 p_chain->u.chain16.prod_idx = (u16)prod_idx;
701 p_chain->u.chain32.prod_idx = prod_idx;
702 p_chain->p_prod_elem = p_prod_elem;
706 * @brief ecore_chain_pbl_zero_mem - set chain memory to 0
710 static OSAL_INLINE void ecore_chain_pbl_zero_mem(struct ecore_chain *p_chain)
714 if (p_chain->mode != ECORE_CHAIN_MODE_PBL)
717 page_cnt = ecore_chain_get_page_cnt(p_chain);
719 for (i = 0; i < page_cnt; i++)
720 OSAL_MEM_ZERO(p_chain->pbl.pp_virt_addr_tbl[i],
721 ECORE_CHAIN_PAGE_SIZE);
724 #endif /* __ECORE_CHAIN_H__ */