net/qede/base: add UDP ports in bulletin board message
[dpdk.git] / drivers / net / qede / base / ecore_chain.h
1 /*
2  * Copyright (c) 2016 QLogic Corporation.
3  * All rights reserved.
4  * www.qlogic.com
5  *
6  * See LICENSE.qede_pmd for copyright and licensing details.
7  */
8
9 #ifndef __ECORE_CHAIN_H__
10 #define __ECORE_CHAIN_H__
11
12 #include <assert.h>             /* @DPDK */
13
14 #include "common_hsi.h"
15 #include "ecore_utils.h"
16
17 enum ecore_chain_mode {
18         /* Each Page contains a next pointer at its end */
19         ECORE_CHAIN_MODE_NEXT_PTR,
20
21         /* Chain is a single page (next ptr) is unrequired */
22         ECORE_CHAIN_MODE_SINGLE,
23
24         /* Page pointers are located in a side list */
25         ECORE_CHAIN_MODE_PBL,
26 };
27
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 */
32 };
33
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,
37
38         /* The chain's size/prod/cons are kept in 32-bit variables  */
39         ECORE_CHAIN_CNT_TYPE_U32,
40 };
41
42 struct ecore_chain_next {
43         struct regpair next_phys;
44         void *next_virt;
45 };
46
47 struct ecore_chain_pbl_u16 {
48         u16 prod_page_idx;
49         u16 cons_page_idx;
50 };
51
52 struct ecore_chain_pbl_u32 {
53         u32 prod_page_idx;
54         u32 cons_page_idx;
55 };
56
57 struct ecore_chain_ext_pbl {
58         dma_addr_t p_pbl_phys;
59         void *p_pbl_virt;
60 };
61
62 struct ecore_chain_u16 {
63         /* Cyclic index of next element to produce/consme */
64         u16 prod_idx;
65         u16 cons_idx;
66 };
67
68 struct ecore_chain_u32 {
69         /* Cyclic index of next element to produce/consme */
70         u32 prod_idx;
71         u32 cons_idx;
72 };
73
74 struct ecore_chain {
75         /* fastpath portion of the chain - required for commands such
76          * as produce / consume.
77          */
78         /* Point to next element to produce/consume */
79         void *p_prod_elem;
80         void *p_cons_elem;
81
82         /* Fastpath portions of the PBL [if exists] */
83
84         struct {
85                 /* Table for keeping the virtual addresses of the chain pages,
86                  * respectively to the physical addresses in the pbl table.
87                  */
88                 void            **pp_virt_addr_tbl;
89
90                 union {
91                         struct ecore_chain_pbl_u16      u16;
92                         struct ecore_chain_pbl_u32      u32;
93                 } c;
94         } pbl;
95
96         union {
97                 struct ecore_chain_u16 chain16;
98                 struct ecore_chain_u32 chain32;
99         } u;
100
101         /* Capacity counts only usable elements */
102         u32                             capacity;
103         u32                             page_cnt;
104
105         /* A u8 would suffice for mode, but it would save as a lot of headaches
106          * on castings & defaults.
107          */
108         enum ecore_chain_mode           mode;
109
110         /* Elements information for fast calculations */
111         u16 elem_per_page;
112         u16 elem_per_page_mask;
113         u16 elem_size;
114         u16 next_page_mask;
115         u16 usable_per_page;
116         u8 elem_unusable;
117
118         u8                              cnt_type;
119
120         /* Slowpath of the chain - required for initialization and destruction,
121          * but isn't involved in regular functionality.
122          */
123
124         /* Base address of a pre-allocated buffer for pbl */
125         struct {
126                 dma_addr_t              p_phys_table;
127                 void                    *p_virt_table;
128         } pbl_sp;
129
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].
133          */
134         void                            *p_virt_addr;
135         dma_addr_t                      p_phys_addr;
136
137         /* Total number of elements [for entire chain] */
138         u32                             size;
139
140         u8                              intended_use;
141
142         /* TBD - do we really need this? Couldn't find usage for it */
143         bool                            b_external_pbl;
144
145         void *dp_ctx;
146 };
147
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))
151
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) /    \
155                      (elem_size))) : 0)
156
157 #define USABLE_ELEMS_PER_PAGE(elem_size, mode)          \
158         ((u32)(ELEMS_PER_PAGE(elem_size) -                      \
159         UNUSABLE_ELEMS_PER_PAGE(elem_size, mode)))
160
161 #define ECORE_CHAIN_PAGE_CNT(elem_cnt, elem_size, mode)         \
162         DIV_ROUND_UP(elem_cnt, USABLE_ELEMS_PER_PAGE(elem_size, mode))
163
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)
166
167 /* Accessors */
168 static OSAL_INLINE u16 ecore_chain_get_prod_idx(struct ecore_chain *p_chain)
169 {
170         OSAL_ASSERT(is_chain_u16(p_chain));
171         return p_chain->u.chain16.prod_idx;
172 }
173
174 static OSAL_INLINE u32 ecore_chain_get_prod_idx_u32(struct ecore_chain *p_chain)
175 {
176         OSAL_ASSERT(is_chain_u32(p_chain));
177         return p_chain->u.chain32.prod_idx;
178 }
179
180 static OSAL_INLINE u16 ecore_chain_get_cons_idx(struct ecore_chain *p_chain)
181 {
182         OSAL_ASSERT(is_chain_u16(p_chain));
183         return p_chain->u.chain16.cons_idx;
184 }
185
186 static OSAL_INLINE u32 ecore_chain_get_cons_idx_u32(struct ecore_chain *p_chain)
187 {
188         OSAL_ASSERT(is_chain_u32(p_chain));
189         return p_chain->u.chain32.cons_idx;
190 }
191
192 /* FIXME:
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.
196  */
197 #define ECORE_U16_MAX   ((u16)~0U)
198 #define ECORE_U32_MAX   ((u32)~0U)
199
200 static OSAL_INLINE u16 ecore_chain_get_elem_left(struct ecore_chain *p_chain)
201 {
202         u16 used;
203
204         OSAL_ASSERT(is_chain_u16(p_chain));
205
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;
212
213         return (u16)(p_chain->capacity - used);
214 }
215
216 static OSAL_INLINE u32
217 ecore_chain_get_elem_left_u32(struct ecore_chain *p_chain)
218 {
219         u32 used;
220
221         OSAL_ASSERT(is_chain_u32(p_chain));
222
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;
229
230         return p_chain->capacity - used;
231 }
232
233 static OSAL_INLINE u8 ecore_chain_is_full(struct ecore_chain *p_chain)
234 {
235         if (is_chain_u16(p_chain))
236                 return (ecore_chain_get_elem_left(p_chain) ==
237                         p_chain->capacity);
238         else
239                 return (ecore_chain_get_elem_left_u32(p_chain) ==
240                         p_chain->capacity);
241 }
242
243 static OSAL_INLINE u8 ecore_chain_is_empty(struct ecore_chain *p_chain)
244 {
245         if (is_chain_u16(p_chain))
246                 return (ecore_chain_get_elem_left(p_chain) == 0);
247         else
248                 return (ecore_chain_get_elem_left_u32(p_chain) == 0);
249 }
250
251 static OSAL_INLINE
252 u16 ecore_chain_get_elem_per_page(struct ecore_chain *p_chain)
253 {
254         return p_chain->elem_per_page;
255 }
256
257 static OSAL_INLINE
258 u16 ecore_chain_get_usable_per_page(struct ecore_chain *p_chain)
259 {
260         return p_chain->usable_per_page;
261 }
262
263 static OSAL_INLINE
264 u8 ecore_chain_get_unusable_per_page(struct ecore_chain *p_chain)
265 {
266         return p_chain->elem_unusable;
267 }
268
269 static OSAL_INLINE u32 ecore_chain_get_size(struct ecore_chain *p_chain)
270 {
271         return p_chain->size;
272 }
273
274 static OSAL_INLINE u32 ecore_chain_get_page_cnt(struct ecore_chain *p_chain)
275 {
276         return p_chain->page_cnt;
277 }
278
279 static OSAL_INLINE
280 dma_addr_t ecore_chain_get_pbl_phys(struct ecore_chain *p_chain)
281 {
282         return p_chain->pbl_sp.p_phys_table;
283 }
284
285 /**
286  * @brief ecore_chain_advance_page -
287  *
288  * Advance the next element accros pages for a linked chain
289  *
290  * @param p_chain
291  * @param p_next_elem
292  * @param idx_to_inc
293  * @param page_to_inc
294  */
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)
298 {
299         struct ecore_chain_next *p_next = OSAL_NULL;
300         u32 page_index = 0;
301
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;
308                 else
309                         *(u32 *)idx_to_inc += (u16)p_chain->elem_unusable;
310                 break;
311         case ECORE_CHAIN_MODE_SINGLE:
312                 *p_next_elem = p_chain->p_virt_addr;
313                 break;
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;
319                 } else {
320                         if (++(*(u32 *)page_to_inc) == p_chain->page_cnt)
321                                 *(u32 *)page_to_inc = 0;
322                         page_index = *(u32 *)page_to_inc;
323                 }
324                 *p_next_elem = p_chain->pbl.pp_virt_addr_tbl[page_index];
325         }
326 }
327
328 #define is_unusable_idx(p, idx)                 \
329         (((p)->u.chain16.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
330
331 #define is_unusable_idx_u32(p, idx)             \
332         (((p)->u.chain32.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
333
334 #define is_unusable_next_idx(p, idx)            \
335         ((((p)->u.chain16.idx + 1) &            \
336         (p)->elem_per_page_mask) == (p)->usable_per_page)
337
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)
341
342 #define test_and_skip(p, idx)                                           \
343         do {                                                            \
344                 if (is_chain_u16(p)) {                                  \
345                         if (is_unusable_idx(p, idx))                    \
346                                 (p)->u.chain16.idx +=                   \
347                                         (p)->elem_unusable;             \
348                 } else {                                                \
349                         if (is_unusable_idx_u32(p, idx))                \
350                                 (p)->u.chain32.idx +=                   \
351                                         (p)->elem_unusable;             \
352                 }                                                       \
353         } while (0)
354
355 /**
356  * @brief ecore_chain_return_multi_produced -
357  *
358  * A chain in which the driver "Produces" elements should use this API
359  * to indicate previous produced elements are now consumed.
360  *
361  * @param p_chain
362  * @param num
363  */
364 static OSAL_INLINE
365 void ecore_chain_return_multi_produced(struct ecore_chain *p_chain, u32 num)
366 {
367         if (is_chain_u16(p_chain))
368                 p_chain->u.chain16.cons_idx += (u16)num;
369         else
370                 p_chain->u.chain32.cons_idx += num;
371         test_and_skip(p_chain, cons_idx);
372 }
373
374 /**
375  * @brief ecore_chain_return_produced -
376  *
377  * A chain in which the driver "Produces" elements should use this API
378  * to indicate previous produced elements are now consumed.
379  *
380  * @param p_chain
381  */
382 static OSAL_INLINE void ecore_chain_return_produced(struct ecore_chain *p_chain)
383 {
384         if (is_chain_u16(p_chain))
385                 p_chain->u.chain16.cons_idx++;
386         else
387                 p_chain->u.chain32.cons_idx++;
388         test_and_skip(p_chain, cons_idx);
389 }
390
391 /**
392  * @brief ecore_chain_produce -
393  *
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.
397  *
398  * @param p_chain
399  *
400  * @return void*, a pointer to next element
401  */
402 static OSAL_INLINE void *ecore_chain_produce(struct ecore_chain *p_chain)
403 {
404         void *p_ret = OSAL_NULL, *p_prod_idx, *p_prod_page_idx;
405
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);
413                 }
414                 p_chain->u.chain16.prod_idx++;
415         } else {
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);
422                 }
423                 p_chain->u.chain32.prod_idx++;
424         }
425
426         p_ret = p_chain->p_prod_elem;
427         p_chain->p_prod_elem = (void *)(((u8 *)p_chain->p_prod_elem) +
428                                         p_chain->elem_size);
429
430         return p_ret;
431 }
432
433 /**
434  * @brief ecore_chain_get_capacity -
435  *
436  * Get the maximum number of BDs in chain
437  *
438  * @param p_chain
439  * @param num
440  *
441  * @return number of unusable BDs
442  */
443 static OSAL_INLINE u32 ecore_chain_get_capacity(struct ecore_chain *p_chain)
444 {
445         return p_chain->capacity;
446 }
447
448 /**
449  * @brief ecore_chain_recycle_consumed -
450  *
451  * Returns an element which was previously consumed;
452  * Increments producers so they could be written to FW.
453  *
454  * @param p_chain
455  */
456 static OSAL_INLINE
457 void ecore_chain_recycle_consumed(struct ecore_chain *p_chain)
458 {
459         test_and_skip(p_chain, prod_idx);
460         if (is_chain_u16(p_chain))
461                 p_chain->u.chain16.prod_idx++;
462         else
463                 p_chain->u.chain32.prod_idx++;
464 }
465
466 /**
467  * @brief ecore_chain_consume -
468  *
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.
471  *
472  * @param p_chain
473  *
474  * @return void*, a pointer to the next buffer written
475  */
476 static OSAL_INLINE void *ecore_chain_consume(struct ecore_chain *p_chain)
477 {
478         void *p_ret = OSAL_NULL, *p_cons_idx, *p_cons_page_idx;
479
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);
487                 }
488                 p_chain->u.chain16.cons_idx++;
489         } else {
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);
496                 }
497                 p_chain->u.chain32.cons_idx++;
498         }
499
500         p_ret = p_chain->p_cons_elem;
501         p_chain->p_cons_elem = (void *)(((u8 *)p_chain->p_cons_elem) +
502                                         p_chain->elem_size);
503
504         return p_ret;
505 }
506
507 /**
508  * @brief ecore_chain_reset -
509  *
510  * Resets the chain to its start state
511  *
512  * @param p_chain pointer to a previously allocted chain
513  */
514 static OSAL_INLINE void ecore_chain_reset(struct ecore_chain *p_chain)
515 {
516         u32 i;
517
518         if (is_chain_u16(p_chain)) {
519                 p_chain->u.chain16.prod_idx = 0;
520                 p_chain->u.chain16.cons_idx = 0;
521         } else {
522                 p_chain->u.chain32.prod_idx = 0;
523                 p_chain->u.chain32.cons_idx = 0;
524         }
525         p_chain->p_cons_elem = p_chain->p_virt_addr;
526         p_chain->p_prod_elem = p_chain->p_virt_addr;
527
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.
533                  */
534                 u32 reset_val = p_chain->page_cnt - 1;
535
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;
539                 } else {
540                         p_chain->pbl.c.u32.prod_page_idx = reset_val;
541                         p_chain->pbl.c.u32.cons_page_idx = reset_val;
542                 }
543         }
544
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);
550                 break;
551
552         case ECORE_CHAIN_USE_TO_CONSUME_PRODUCE:
553         case ECORE_CHAIN_USE_TO_PRODUCE:
554         default:
555                 /* Do nothing */
556                 break;
557         }
558 }
559
560 /**
561  * @brief ecore_chain_init_params -
562  *
563  * Initalizes a basic chain struct
564  *
565  * @param p_chain
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
569  * @param mode
570  * @param cnt_type
571  * @param dp_ctx
572  */
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)
578 {
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;
586
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);
593
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;
601
602         p_chain->dp_ctx = dp_ctx;
603 }
604
605 /**
606  * @brief ecore_chain_init_mem -
607  *
608  * Initalizes a basic chain struct with its chain buffers
609  *
610  * @param p_chain
611  * @param p_virt_addr   virtual address of allocated buffer's beginning
612  * @param p_phys_addr   physical address of allocated buffer's beginning
613  *
614  */
615 static OSAL_INLINE void ecore_chain_init_mem(struct ecore_chain *p_chain,
616                                              void *p_virt_addr,
617                                              dma_addr_t p_phys_addr)
618 {
619         p_chain->p_virt_addr = p_virt_addr;
620         p_chain->p_phys_addr = p_phys_addr;
621 }
622
623 /**
624  * @brief ecore_chain_init_pbl_mem -
625  *
626  * Initalizes a basic chain struct with its pbl buffers
627  *
628  * @param p_chain
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.
636  *
637  */
638 static OSAL_INLINE void ecore_chain_init_pbl_mem(struct ecore_chain *p_chain,
639                                                  void *p_virt_pbl,
640                                                  dma_addr_t p_phys_pbl,
641                                                  void **pp_virt_addr_tbl)
642 {
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;
646 }
647
648 /**
649  * @brief ecore_chain_init_next_ptr_elem -
650  *
651  * Initalizes a next pointer element
652  *
653  * @param p_chain
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
658  *
659  */
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)
663 {
664         struct ecore_chain_next *p_next;
665         u32 size;
666
667         size = p_chain->elem_size * p_chain->usable_per_page;
668         p_next = (struct ecore_chain_next *)((u8 *)p_virt_curr + size);
669
670         DMA_REGPAIR_LE(p_next->next_phys, p_phys_next);
671
672         p_next->next_virt = p_virt_next;
673 }
674
675 /**
676  * @brief ecore_chain_get_last_elem -
677  *
678  * Returns a pointer to the last element of the chain
679  *
680  * @param p_chain
681  *
682  * @return void*
683  */
684 static OSAL_INLINE void *ecore_chain_get_last_elem(struct ecore_chain *p_chain)
685 {
686         struct ecore_chain_next *p_next = OSAL_NULL;
687         void *p_virt_addr = OSAL_NULL;
688         u32 size, last_page_idx;
689
690         if (!p_chain->p_virt_addr)
691                 goto out;
692
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;
700                         p_next =
701                             (struct ecore_chain_next *)((u8 *)p_virt_addr +
702                                                         size);
703                 }
704                 break;
705         case ECORE_CHAIN_MODE_SINGLE:
706                 p_virt_addr = p_chain->p_virt_addr;
707                 break;
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];
711                 break;
712         }
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);
716 out:
717         return p_virt_addr;
718 }
719
720 /**
721  * @brief ecore_chain_set_prod - sets the prod to the given value
722  *
723  * @param prod_idx
724  * @param p_prod_elem
725  */
726 static OSAL_INLINE void ecore_chain_set_prod(struct ecore_chain *p_chain,
727                                              u32 prod_idx, void *p_prod_elem)
728 {
729         if (is_chain_u16(p_chain))
730                 p_chain->u.chain16.prod_idx = (u16)prod_idx;
731         else
732                 p_chain->u.chain32.prod_idx = prod_idx;
733         p_chain->p_prod_elem = p_prod_elem;
734 }
735
736 /**
737  * @brief ecore_chain_pbl_zero_mem - set chain memory to 0
738  *
739  * @param p_chain
740  */
741 static OSAL_INLINE void ecore_chain_pbl_zero_mem(struct ecore_chain *p_chain)
742 {
743         u32 i, page_cnt;
744
745         if (p_chain->mode != ECORE_CHAIN_MODE_PBL)
746                 return;
747
748         page_cnt = ecore_chain_get_page_cnt(p_chain);
749
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);
753 }
754
755 int ecore_chain_print(struct ecore_chain *p_chain, char *buffer,
756                       u32 buffer_size, u32 *element_indx, u32 stop_indx,
757                       bool print_metadata,
758                       int (*func_ptr_print_element)(struct ecore_chain *p_chain,
759                                                     void *p_element,
760                                                     char *buffer),
761                       int (*func_ptr_print_metadata)(struct ecore_chain
762                                                      *p_chain,
763                                                      char *buffer));
764
765 #endif /* __ECORE_CHAIN_H__ */