Imported Upstream version 16.07-rc1
[deb_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_pbl {
58         /* Base address of a pre-allocated buffer for pbl */
59         dma_addr_t p_phys_table;
60         void *p_virt_table;
61
62         /* Table for keeping the virtual addresses of the chain pages,
63          * respectively to the physical addresses in the pbl table.
64          */
65         void **pp_virt_addr_tbl;
66
67         /* Index to current used page by producer/consumer */
68         union {
69                 struct ecore_chain_pbl_u16 pbl16;
70                 struct ecore_chain_pbl_u32 pbl32;
71         } u;
72 };
73
74 struct ecore_chain_u16 {
75         /* Cyclic index of next element to produce/consme */
76         u16 prod_idx;
77         u16 cons_idx;
78 };
79
80 struct ecore_chain_u32 {
81         /* Cyclic index of next element to produce/consme */
82         u32 prod_idx;
83         u32 cons_idx;
84 };
85
86 struct ecore_chain {
87         /* Address of first page of the chain */
88         void *p_virt_addr;
89         dma_addr_t p_phys_addr;
90
91         /* Point to next element to produce/consume */
92         void *p_prod_elem;
93         void *p_cons_elem;
94
95         enum ecore_chain_mode mode;
96         enum ecore_chain_use_mode intended_use;
97
98         enum ecore_chain_cnt_type cnt_type;
99         union {
100                 struct ecore_chain_u16 chain16;
101                 struct ecore_chain_u32 chain32;
102         } u;
103
104         u32 page_cnt;
105
106         /* Number of elements - capacity is for usable elements only,
107          * while size will contain total number of elements [for entire chain].
108          */
109         u32 capacity;
110         u32 size;
111
112         /* Elements information for fast calculations */
113         u16 elem_per_page;
114         u16 elem_per_page_mask;
115         u16 elem_unusable;
116         u16 usable_per_page;
117         u16 elem_size;
118         u16 next_page_mask;
119
120         struct ecore_chain_pbl pbl;
121 };
122
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))
126
127 #define UNUSABLE_ELEMS_PER_PAGE(elem_size, mode)                \
128           ((mode == ECORE_CHAIN_MODE_NEXT_PTR) ?                \
129            (1 + ((sizeof(struct ecore_chain_next) - 1) /                \
130            (elem_size))) : 0)
131
132 #define USABLE_ELEMS_PER_PAGE(elem_size, mode)                  \
133         ((u32)(ELEMS_PER_PAGE(elem_size) -                      \
134         UNUSABLE_ELEMS_PER_PAGE(elem_size, mode)))
135
136 #define ECORE_CHAIN_PAGE_CNT(elem_cnt, elem_size, mode)         \
137         DIV_ROUND_UP(elem_cnt, USABLE_ELEMS_PER_PAGE(elem_size, mode))
138
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)
141
142 /* Accessors */
143 static OSAL_INLINE u16 ecore_chain_get_prod_idx(struct ecore_chain *p_chain)
144 {
145         OSAL_ASSERT(is_chain_u16(p_chain));
146         return p_chain->u.chain16.prod_idx;
147 }
148
149 static OSAL_INLINE u32 ecore_chain_get_prod_idx_u32(struct ecore_chain *p_chain)
150 {
151         OSAL_ASSERT(is_chain_u32(p_chain));
152         return p_chain->u.chain32.prod_idx;
153 }
154
155 static OSAL_INLINE u16 ecore_chain_get_cons_idx(struct ecore_chain *p_chain)
156 {
157         OSAL_ASSERT(is_chain_u16(p_chain));
158         return p_chain->u.chain16.cons_idx;
159 }
160
161 static OSAL_INLINE u32 ecore_chain_get_cons_idx_u32(struct ecore_chain *p_chain)
162 {
163         OSAL_ASSERT(is_chain_u32(p_chain));
164         return p_chain->u.chain32.cons_idx;
165 }
166
167 /* FIXME:
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.
171  */
172 #define ECORE_U16_MAX   ((u16)~0U)
173 #define ECORE_U32_MAX   ((u32)~0U)
174
175 static OSAL_INLINE u16 ecore_chain_get_elem_left(struct ecore_chain *p_chain)
176 {
177         u16 used;
178
179         OSAL_ASSERT(is_chain_u16(p_chain));
180
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;
187
188         return (u16)(p_chain->capacity - used);
189 }
190
191 static OSAL_INLINE u32
192 ecore_chain_get_elem_left_u32(struct ecore_chain *p_chain)
193 {
194         u32 used;
195
196         OSAL_ASSERT(is_chain_u32(p_chain));
197
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;
204
205         return p_chain->capacity - used;
206 }
207
208 static OSAL_INLINE u8 ecore_chain_is_full(struct ecore_chain *p_chain)
209 {
210         if (is_chain_u16(p_chain))
211                 return (ecore_chain_get_elem_left(p_chain) ==
212                         p_chain->capacity);
213         else
214                 return (ecore_chain_get_elem_left_u32(p_chain) ==
215                         p_chain->capacity);
216 }
217
218 static OSAL_INLINE u8 ecore_chain_is_empty(struct ecore_chain *p_chain)
219 {
220         if (is_chain_u16(p_chain))
221                 return (ecore_chain_get_elem_left(p_chain) == 0);
222         else
223                 return (ecore_chain_get_elem_left_u32(p_chain) == 0);
224 }
225
226 static OSAL_INLINE
227 u16 ecore_chain_get_elem_per_page(struct ecore_chain *p_chain)
228 {
229         return p_chain->elem_per_page;
230 }
231
232 static OSAL_INLINE
233 u16 ecore_chain_get_usable_per_page(struct ecore_chain *p_chain)
234 {
235         return p_chain->usable_per_page;
236 }
237
238 static OSAL_INLINE
239 u16 ecore_chain_get_unusable_per_page(struct ecore_chain *p_chain)
240 {
241         return p_chain->elem_unusable;
242 }
243
244 static OSAL_INLINE u32 ecore_chain_get_size(struct ecore_chain *p_chain)
245 {
246         return p_chain->size;
247 }
248
249 static OSAL_INLINE u32 ecore_chain_get_page_cnt(struct ecore_chain *p_chain)
250 {
251         return p_chain->page_cnt;
252 }
253
254 static OSAL_INLINE
255 dma_addr_t ecore_chain_get_pbl_phys(struct ecore_chain *p_chain)
256 {
257         return p_chain->pbl.p_phys_table;
258 }
259
260 /**
261  * @brief ecore_chain_advance_page -
262  *
263  * Advance the next element accros pages for a linked chain
264  *
265  * @param p_chain
266  * @param p_next_elem
267  * @param idx_to_inc
268  * @param page_to_inc
269  */
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)
273 {
274         struct ecore_chain_next *p_next = OSAL_NULL;
275         u32 page_index = 0;
276
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;
283                 else
284                         *(u32 *)idx_to_inc += p_chain->elem_unusable;
285                 break;
286         case ECORE_CHAIN_MODE_SINGLE:
287                 *p_next_elem = p_chain->p_virt_addr;
288                 break;
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;
294                 } else {
295                         if (++(*(u32 *)page_to_inc) == p_chain->page_cnt)
296                                 *(u32 *)page_to_inc = 0;
297                         page_index = *(u32 *)page_to_inc;
298                 }
299                 *p_next_elem = p_chain->pbl.pp_virt_addr_tbl[page_index];
300         }
301 }
302
303 #define is_unusable_idx(p, idx)                 \
304         (((p)->u.chain16.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
305
306 #define is_unusable_idx_u32(p, idx)             \
307         (((p)->u.chain32.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
308
309 #define is_unusable_next_idx(p, idx)            \
310         ((((p)->u.chain16.idx + 1) & (p)->elem_per_page_mask) == \
311         (p)->usable_per_page)
312
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)
316
317 #define test_and_skip(p, idx)                                           \
318         do {                                                            \
319                 if (is_chain_u16(p)) {                                  \
320                         if (is_unusable_idx(p, idx))                    \
321                                 (p)->u.chain16.idx += (p)->elem_unusable; \
322                 } else {                                                \
323                         if (is_unusable_idx_u32(p, idx))                \
324                                 (p)->u.chain32.idx += (p)->elem_unusable; \
325                 }                                                       \
326         } while (0)
327
328 /**
329  * @brief ecore_chain_return_multi_produced -
330  *
331  * A chain in which the driver "Produces" elements should use this API
332  * to indicate previous produced elements are now consumed.
333  *
334  * @param p_chain
335  * @param num
336  */
337 static OSAL_INLINE
338 void ecore_chain_return_multi_produced(struct ecore_chain *p_chain, u32 num)
339 {
340         if (is_chain_u16(p_chain))
341                 p_chain->u.chain16.cons_idx += (u16)num;
342         else
343                 p_chain->u.chain32.cons_idx += num;
344         test_and_skip(p_chain, cons_idx);
345 }
346
347 /**
348  * @brief ecore_chain_return_produced -
349  *
350  * A chain in which the driver "Produces" elements should use this API
351  * to indicate previous produced elements are now consumed.
352  *
353  * @param p_chain
354  */
355 static OSAL_INLINE void ecore_chain_return_produced(struct ecore_chain *p_chain)
356 {
357         if (is_chain_u16(p_chain))
358                 p_chain->u.chain16.cons_idx++;
359         else
360                 p_chain->u.chain32.cons_idx++;
361         test_and_skip(p_chain, cons_idx);
362 }
363
364 /**
365  * @brief ecore_chain_produce -
366  *
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.
370  *
371  * @param p_chain
372  *
373  * @return void*, a pointer to next element
374  */
375 static OSAL_INLINE void *ecore_chain_produce(struct ecore_chain *p_chain)
376 {
377         void *p_ret = OSAL_NULL, *p_prod_idx, *p_prod_page_idx;
378
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);
386                 }
387                 p_chain->u.chain16.prod_idx++;
388         } else {
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);
395                 }
396                 p_chain->u.chain32.prod_idx++;
397         }
398
399         p_ret = p_chain->p_prod_elem;
400         p_chain->p_prod_elem = (void *)(((u8 *)p_chain->p_prod_elem) +
401                                         p_chain->elem_size);
402
403         return p_ret;
404 }
405
406 /**
407  * @brief ecore_chain_get_capacity -
408  *
409  * Get the maximum number of BDs in chain
410  *
411  * @param p_chain
412  * @param num
413  *
414  * @return number of unusable BDs
415  */
416 static OSAL_INLINE u32 ecore_chain_get_capacity(struct ecore_chain *p_chain)
417 {
418         return p_chain->capacity;
419 }
420
421 /**
422  * @brief ecore_chain_recycle_consumed -
423  *
424  * Returns an element which was previously consumed;
425  * Increments producers so they could be written to FW.
426  *
427  * @param p_chain
428  */
429 static OSAL_INLINE
430 void ecore_chain_recycle_consumed(struct ecore_chain *p_chain)
431 {
432         test_and_skip(p_chain, prod_idx);
433         if (is_chain_u16(p_chain))
434                 p_chain->u.chain16.prod_idx++;
435         else
436                 p_chain->u.chain32.prod_idx++;
437 }
438
439 /**
440  * @brief ecore_chain_consume -
441  *
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.
444  *
445  * @param p_chain
446  *
447  * @return void*, a pointer to the next buffer written
448  */
449 static OSAL_INLINE void *ecore_chain_consume(struct ecore_chain *p_chain)
450 {
451         void *p_ret = OSAL_NULL, *p_cons_idx, *p_cons_page_idx;
452
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);
460                 }
461                 p_chain->u.chain16.cons_idx++;
462         } else {
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);
469                 }
470                 p_chain->u.chain32.cons_idx++;
471         }
472
473         p_ret = p_chain->p_cons_elem;
474         p_chain->p_cons_elem = (void *)(((u8 *)p_chain->p_cons_elem) +
475                                         p_chain->elem_size);
476
477         return p_ret;
478 }
479
480 /**
481  * @brief ecore_chain_reset -
482  *
483  * Resets the chain to its start state
484  *
485  * @param p_chain pointer to a previously allocted chain
486  */
487 static OSAL_INLINE void ecore_chain_reset(struct ecore_chain *p_chain)
488 {
489         u32 i;
490
491         if (is_chain_u16(p_chain)) {
492                 p_chain->u.chain16.prod_idx = 0;
493                 p_chain->u.chain16.cons_idx = 0;
494         } else {
495                 p_chain->u.chain32.prod_idx = 0;
496                 p_chain->u.chain32.cons_idx = 0;
497         }
498         p_chain->p_cons_elem = p_chain->p_virt_addr;
499         p_chain->p_prod_elem = p_chain->p_virt_addr;
500
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.
506                  */
507                 u32 reset_val = p_chain->page_cnt - 1;
508
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;
512                 } else {
513                         p_chain->pbl.u.pbl32.prod_page_idx = reset_val;
514                         p_chain->pbl.u.pbl32.cons_page_idx = reset_val;
515                 }
516         }
517
518         switch (p_chain->intended_use) {
519         case ECORE_CHAIN_USE_TO_CONSUME_PRODUCE:
520         case ECORE_CHAIN_USE_TO_PRODUCE:
521                 /* Do nothing */
522                 break;
523
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);
528                 break;
529         }
530 }
531
532 /**
533  * @brief ecore_chain_init_params -
534  *
535  * Initalizes a basic chain struct
536  *
537  * @param p_chain
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
541  * @param mode
542  * @param cnt_type
543  */
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)
549 {
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;
557
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);
564
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;
568
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;
572 }
573
574 /**
575  * @brief ecore_chain_init_mem -
576  *
577  * Initalizes a basic chain struct with its chain buffers
578  *
579  * @param p_chain
580  * @param p_virt_addr   virtual address of allocated buffer's beginning
581  * @param p_phys_addr   physical address of allocated buffer's beginning
582  *
583  */
584 static OSAL_INLINE void ecore_chain_init_mem(struct ecore_chain *p_chain,
585                                              void *p_virt_addr,
586                                              dma_addr_t p_phys_addr)
587 {
588         p_chain->p_virt_addr = p_virt_addr;
589         p_chain->p_phys_addr = p_phys_addr;
590 }
591
592 /**
593  * @brief ecore_chain_init_pbl_mem -
594  *
595  * Initalizes a basic chain struct with its pbl buffers
596  *
597  * @param p_chain
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.
605  *
606  */
607 static OSAL_INLINE void ecore_chain_init_pbl_mem(struct ecore_chain *p_chain,
608                                                  void *p_virt_pbl,
609                                                  dma_addr_t p_phys_pbl,
610                                                  void **pp_virt_addr_tbl)
611 {
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;
615 }
616
617 /**
618  * @brief ecore_chain_init_next_ptr_elem -
619  *
620  * Initalizes a next pointer element
621  *
622  * @param p_chain
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
627  *
628  */
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)
632 {
633         struct ecore_chain_next *p_next;
634         u32 size;
635
636         size = p_chain->elem_size * p_chain->usable_per_page;
637         p_next = (struct ecore_chain_next *)((u8 *)p_virt_curr + size);
638
639         DMA_REGPAIR_LE(p_next->next_phys, p_phys_next);
640
641         p_next->next_virt = p_virt_next;
642 }
643
644 /**
645  * @brief ecore_chain_get_last_elem -
646  *
647  * Returns a pointer to the last element of the chain
648  *
649  * @param p_chain
650  *
651  * @return void*
652  */
653 static OSAL_INLINE void *ecore_chain_get_last_elem(struct ecore_chain *p_chain)
654 {
655         struct ecore_chain_next *p_next = OSAL_NULL;
656         void *p_virt_addr = OSAL_NULL;
657         u32 size, last_page_idx;
658
659         if (!p_chain->p_virt_addr)
660                 goto out;
661
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;
669                         p_next =
670                             (struct ecore_chain_next *)((u8 *)p_virt_addr +
671                                                         size);
672                 }
673                 break;
674         case ECORE_CHAIN_MODE_SINGLE:
675                 p_virt_addr = p_chain->p_virt_addr;
676                 break;
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];
680                 break;
681         }
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);
685 out:
686         return p_virt_addr;
687 }
688
689 /**
690  * @brief ecore_chain_set_prod - sets the prod to the given value
691  *
692  * @param prod_idx
693  * @param p_prod_elem
694  */
695 static OSAL_INLINE void ecore_chain_set_prod(struct ecore_chain *p_chain,
696                                              u32 prod_idx, void *p_prod_elem)
697 {
698         if (is_chain_u16(p_chain))
699                 p_chain->u.chain16.prod_idx = (u16)prod_idx;
700         else
701                 p_chain->u.chain32.prod_idx = prod_idx;
702         p_chain->p_prod_elem = p_prod_elem;
703 }
704
705 /**
706  * @brief ecore_chain_pbl_zero_mem - set chain memory to 0
707  *
708  * @param p_chain
709  */
710 static OSAL_INLINE void ecore_chain_pbl_zero_mem(struct ecore_chain *p_chain)
711 {
712         u32 i, page_cnt;
713
714         if (p_chain->mode != ECORE_CHAIN_MODE_PBL)
715                 return;
716
717         page_cnt = ecore_chain_get_page_cnt(p_chain);
718
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);
722 }
723
724 #endif /* __ECORE_CHAIN_H__ */