qede: add base driver
[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 /**
255  * @brief ecore_chain_advance_page -
256  *
257  * Advance the next element accros pages for a linked chain
258  *
259  * @param p_chain
260  * @param p_next_elem
261  * @param idx_to_inc
262  * @param page_to_inc
263  */
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)
267 {
268         struct ecore_chain_next *p_next = OSAL_NULL;
269         u32 page_index = 0;
270
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;
277                 else
278                         *(u32 *)idx_to_inc += p_chain->elem_unusable;
279                 break;
280         case ECORE_CHAIN_MODE_SINGLE:
281                 *p_next_elem = p_chain->p_virt_addr;
282                 break;
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;
288                 } else {
289                         if (++(*(u32 *)page_to_inc) == p_chain->page_cnt)
290                                 *(u32 *)page_to_inc = 0;
291                         page_index = *(u32 *)page_to_inc;
292                 }
293                 *p_next_elem = p_chain->pbl.pp_virt_addr_tbl[page_index];
294         }
295 }
296
297 #define is_unusable_idx(p, idx)                 \
298         (((p)->u.chain16.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
299
300 #define is_unusable_idx_u32(p, idx)             \
301         (((p)->u.chain32.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
302
303 #define is_unusable_next_idx(p, idx)            \
304         ((((p)->u.chain16.idx + 1) & (p)->elem_per_page_mask) == \
305         (p)->usable_per_page)
306
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)
310
311 #define test_and_skip(p, idx)                                           \
312         do {                                                            \
313                 if (is_chain_u16(p)) {                                  \
314                         if (is_unusable_idx(p, idx))                    \
315                                 (p)->u.chain16.idx += (p)->elem_unusable; \
316                 } else {                                                \
317                         if (is_unusable_idx_u32(p, idx))                \
318                                 (p)->u.chain32.idx += (p)->elem_unusable; \
319                 }                                                       \
320         } while (0)
321
322 /**
323  * @brief ecore_chain_return_multi_produced -
324  *
325  * A chain in which the driver "Produces" elements should use this API
326  * to indicate previous produced elements are now consumed.
327  *
328  * @param p_chain
329  * @param num
330  */
331 static OSAL_INLINE
332 void ecore_chain_return_multi_produced(struct ecore_chain *p_chain, u32 num)
333 {
334         if (is_chain_u16(p_chain))
335                 p_chain->u.chain16.cons_idx += (u16)num;
336         else
337                 p_chain->u.chain32.cons_idx += num;
338         test_and_skip(p_chain, cons_idx);
339 }
340
341 /**
342  * @brief ecore_chain_return_produced -
343  *
344  * A chain in which the driver "Produces" elements should use this API
345  * to indicate previous produced elements are now consumed.
346  *
347  * @param p_chain
348  */
349 static OSAL_INLINE void ecore_chain_return_produced(struct ecore_chain *p_chain)
350 {
351         if (is_chain_u16(p_chain))
352                 p_chain->u.chain16.cons_idx++;
353         else
354                 p_chain->u.chain32.cons_idx++;
355         test_and_skip(p_chain, cons_idx);
356 }
357
358 /**
359  * @brief ecore_chain_produce -
360  *
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.
364  *
365  * @param p_chain
366  *
367  * @return void*, a pointer to next element
368  */
369 static OSAL_INLINE void *ecore_chain_produce(struct ecore_chain *p_chain)
370 {
371         void *p_ret = OSAL_NULL, *p_prod_idx, *p_prod_page_idx;
372
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);
380                 }
381                 p_chain->u.chain16.prod_idx++;
382         } else {
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);
389                 }
390                 p_chain->u.chain32.prod_idx++;
391         }
392
393         p_ret = p_chain->p_prod_elem;
394         p_chain->p_prod_elem = (void *)(((u8 *)p_chain->p_prod_elem) +
395                                         p_chain->elem_size);
396
397         return p_ret;
398 }
399
400 /**
401  * @brief ecore_chain_get_capacity -
402  *
403  * Get the maximum number of BDs in chain
404  *
405  * @param p_chain
406  * @param num
407  *
408  * @return number of unusable BDs
409  */
410 static OSAL_INLINE u32 ecore_chain_get_capacity(struct ecore_chain *p_chain)
411 {
412         return p_chain->capacity;
413 }
414
415 /**
416  * @brief ecore_chain_recycle_consumed -
417  *
418  * Returns an element which was previously consumed;
419  * Increments producers so they could be written to FW.
420  *
421  * @param p_chain
422  */
423 static OSAL_INLINE
424 void ecore_chain_recycle_consumed(struct ecore_chain *p_chain)
425 {
426         test_and_skip(p_chain, prod_idx);
427         if (is_chain_u16(p_chain))
428                 p_chain->u.chain16.prod_idx++;
429         else
430                 p_chain->u.chain32.prod_idx++;
431 }
432
433 /**
434  * @brief ecore_chain_consume -
435  *
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.
438  *
439  * @param p_chain
440  *
441  * @return void*, a pointer to the next buffer written
442  */
443 static OSAL_INLINE void *ecore_chain_consume(struct ecore_chain *p_chain)
444 {
445         void *p_ret = OSAL_NULL, *p_cons_idx, *p_cons_page_idx;
446
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);
454                 }
455                 p_chain->u.chain16.cons_idx++;
456         } else {
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);
463                 }
464                 p_chain->u.chain32.cons_idx++;
465         }
466
467         p_ret = p_chain->p_cons_elem;
468         p_chain->p_cons_elem = (void *)(((u8 *)p_chain->p_cons_elem) +
469                                         p_chain->elem_size);
470
471         return p_ret;
472 }
473
474 /**
475  * @brief ecore_chain_reset -
476  *
477  * Resets the chain to its start state
478  *
479  * @param p_chain pointer to a previously allocted chain
480  */
481 static OSAL_INLINE void ecore_chain_reset(struct ecore_chain *p_chain)
482 {
483         u32 i;
484
485         if (is_chain_u16(p_chain)) {
486                 p_chain->u.chain16.prod_idx = 0;
487                 p_chain->u.chain16.cons_idx = 0;
488         } else {
489                 p_chain->u.chain32.prod_idx = 0;
490                 p_chain->u.chain32.cons_idx = 0;
491         }
492         p_chain->p_cons_elem = p_chain->p_virt_addr;
493         p_chain->p_prod_elem = p_chain->p_virt_addr;
494
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.
500                  */
501                 u32 reset_val = p_chain->page_cnt - 1;
502
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;
506                 } else {
507                         p_chain->pbl.u.pbl32.prod_page_idx = reset_val;
508                         p_chain->pbl.u.pbl32.cons_page_idx = reset_val;
509                 }
510         }
511
512         switch (p_chain->intended_use) {
513         case ECORE_CHAIN_USE_TO_CONSUME_PRODUCE:
514         case ECORE_CHAIN_USE_TO_PRODUCE:
515                 /* Do nothing */
516                 break;
517
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);
522                 break;
523         }
524 }
525
526 /**
527  * @brief ecore_chain_init_params -
528  *
529  * Initalizes a basic chain struct
530  *
531  * @param p_chain
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
535  * @param mode
536  * @param cnt_type
537  */
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)
543 {
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;
551
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);
558
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;
562
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;
566 }
567
568 /**
569  * @brief ecore_chain_init_mem -
570  *
571  * Initalizes a basic chain struct with its chain buffers
572  *
573  * @param p_chain
574  * @param p_virt_addr   virtual address of allocated buffer's beginning
575  * @param p_phys_addr   physical address of allocated buffer's beginning
576  *
577  */
578 static OSAL_INLINE void ecore_chain_init_mem(struct ecore_chain *p_chain,
579                                              void *p_virt_addr,
580                                              dma_addr_t p_phys_addr)
581 {
582         p_chain->p_virt_addr = p_virt_addr;
583         p_chain->p_phys_addr = p_phys_addr;
584 }
585
586 /**
587  * @brief ecore_chain_init_pbl_mem -
588  *
589  * Initalizes a basic chain struct with its pbl buffers
590  *
591  * @param p_chain
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.
599  *
600  */
601 static OSAL_INLINE void ecore_chain_init_pbl_mem(struct ecore_chain *p_chain,
602                                                  void *p_virt_pbl,
603                                                  dma_addr_t p_phys_pbl,
604                                                  void **pp_virt_addr_tbl)
605 {
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;
609 }
610
611 /**
612  * @brief ecore_chain_init_next_ptr_elem -
613  *
614  * Initalizes a next pointer element
615  *
616  * @param p_chain
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
621  *
622  */
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)
626 {
627         struct ecore_chain_next *p_next;
628         u32 size;
629
630         size = p_chain->elem_size * p_chain->usable_per_page;
631         p_next = (struct ecore_chain_next *)((u8 *)p_virt_curr + size);
632
633         DMA_REGPAIR_LE(p_next->next_phys, p_phys_next);
634
635         p_next->next_virt = p_virt_next;
636 }
637
638 /**
639  * @brief ecore_chain_get_last_elem -
640  *
641  * Returns a pointer to the last element of the chain
642  *
643  * @param p_chain
644  *
645  * @return void*
646  */
647 static OSAL_INLINE void *ecore_chain_get_last_elem(struct ecore_chain *p_chain)
648 {
649         struct ecore_chain_next *p_next = OSAL_NULL;
650         void *p_virt_addr = OSAL_NULL;
651         u32 size, last_page_idx;
652
653         if (!p_chain->p_virt_addr)
654                 goto out;
655
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;
663                         p_next =
664                             (struct ecore_chain_next *)((u8 *)p_virt_addr +
665                                                         size);
666                 }
667                 break;
668         case ECORE_CHAIN_MODE_SINGLE:
669                 p_virt_addr = p_chain->p_virt_addr;
670                 break;
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];
674                 break;
675         }
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);
679 out:
680         return p_virt_addr;
681 }
682
683 /**
684  * @brief ecore_chain_set_prod - sets the prod to the given value
685  *
686  * @param prod_idx
687  * @param p_prod_elem
688  */
689 static OSAL_INLINE void ecore_chain_set_prod(struct ecore_chain *p_chain,
690                                              u32 prod_idx, void *p_prod_elem)
691 {
692         if (is_chain_u16(p_chain))
693                 p_chain->u.chain16.prod_idx = (u16)prod_idx;
694         else
695                 p_chain->u.chain32.prod_idx = prod_idx;
696         p_chain->p_prod_elem = p_prod_elem;
697 }
698
699 /**
700  * @brief ecore_chain_pbl_zero_mem - set chain memory to 0
701  *
702  * @param p_chain
703  */
704 static OSAL_INLINE void ecore_chain_pbl_zero_mem(struct ecore_chain *p_chain)
705 {
706         u32 i, page_cnt;
707
708         if (p_chain->mode != ECORE_CHAIN_MODE_PBL)
709                 return;
710
711         page_cnt = ecore_chain_get_page_cnt(p_chain);
712
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);
716 }
717
718 #endif /* __ECORE_CHAIN_H__ */