net/qede: add new host ring type option
[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_pbl {
63         /* Base address of a pre-allocated buffer for pbl */
64         dma_addr_t p_phys_table;
65         void *p_virt_table;
66
67         /* Table for keeping the virtual addresses of the chain pages,
68          * respectively to the physical addresses in the pbl table.
69          */
70         void **pp_virt_addr_tbl;
71
72         /* Index to current used page by producer/consumer */
73         union {
74                 struct ecore_chain_pbl_u16 pbl16;
75                 struct ecore_chain_pbl_u32 pbl32;
76         } u;
77
78         bool external;
79 };
80
81 struct ecore_chain_u16 {
82         /* Cyclic index of next element to produce/consme */
83         u16 prod_idx;
84         u16 cons_idx;
85 };
86
87 struct ecore_chain_u32 {
88         /* Cyclic index of next element to produce/consme */
89         u32 prod_idx;
90         u32 cons_idx;
91 };
92
93 struct ecore_chain {
94         /* Address of first page of the chain */
95         void *p_virt_addr;
96         dma_addr_t p_phys_addr;
97
98         /* Point to next element to produce/consume */
99         void *p_prod_elem;
100         void *p_cons_elem;
101
102         enum ecore_chain_mode mode;
103         enum ecore_chain_use_mode intended_use;
104
105         enum ecore_chain_cnt_type cnt_type;
106         union {
107                 struct ecore_chain_u16 chain16;
108                 struct ecore_chain_u32 chain32;
109         } u;
110
111         u32 page_cnt;
112
113         /* Number of elements - capacity is for usable elements only,
114          * while size will contain total number of elements [for entire chain].
115          */
116         u32 capacity;
117         u32 size;
118
119         /* Elements information for fast calculations */
120         u16 elem_per_page;
121         u16 elem_per_page_mask;
122         u16 elem_unusable;
123         u16 usable_per_page;
124         u16 elem_size;
125         u16 next_page_mask;
126
127         struct ecore_chain_pbl pbl;
128
129         void *dp_ctx;
130 };
131
132 #define ECORE_CHAIN_PBL_ENTRY_SIZE      (8)
133 #define ECORE_CHAIN_PAGE_SIZE           (0x1000)
134 #define ELEMS_PER_PAGE(elem_size)       (ECORE_CHAIN_PAGE_SIZE / (elem_size))
135
136 #define UNUSABLE_ELEMS_PER_PAGE(elem_size, mode)                \
137           ((mode == ECORE_CHAIN_MODE_NEXT_PTR) ?                \
138            (1 + ((sizeof(struct ecore_chain_next) - 1) /                \
139            (elem_size))) : 0)
140
141 #define USABLE_ELEMS_PER_PAGE(elem_size, mode)          \
142         ((u32)(ELEMS_PER_PAGE(elem_size) -                      \
143         UNUSABLE_ELEMS_PER_PAGE(elem_size, mode)))
144
145 #define ECORE_CHAIN_PAGE_CNT(elem_cnt, elem_size, mode)         \
146         DIV_ROUND_UP(elem_cnt, USABLE_ELEMS_PER_PAGE(elem_size, mode))
147
148 #define is_chain_u16(p) ((p)->cnt_type == ECORE_CHAIN_CNT_TYPE_U16)
149 #define is_chain_u32(p) ((p)->cnt_type == ECORE_CHAIN_CNT_TYPE_U32)
150
151 /* Accessors */
152 static OSAL_INLINE u16 ecore_chain_get_prod_idx(struct ecore_chain *p_chain)
153 {
154         OSAL_ASSERT(is_chain_u16(p_chain));
155         return p_chain->u.chain16.prod_idx;
156 }
157
158 static OSAL_INLINE u32 ecore_chain_get_prod_idx_u32(struct ecore_chain *p_chain)
159 {
160         OSAL_ASSERT(is_chain_u32(p_chain));
161         return p_chain->u.chain32.prod_idx;
162 }
163
164 static OSAL_INLINE u16 ecore_chain_get_cons_idx(struct ecore_chain *p_chain)
165 {
166         OSAL_ASSERT(is_chain_u16(p_chain));
167         return p_chain->u.chain16.cons_idx;
168 }
169
170 static OSAL_INLINE u32 ecore_chain_get_cons_idx_u32(struct ecore_chain *p_chain)
171 {
172         OSAL_ASSERT(is_chain_u32(p_chain));
173         return p_chain->u.chain32.cons_idx;
174 }
175
176 /* FIXME:
177  * Should create OSALs for the below definitions.
178  * For Linux, replace them with the existing U16_MAX and U32_MAX, and handle
179  * kernel versions that lack them.
180  */
181 #define ECORE_U16_MAX   ((u16)~0U)
182 #define ECORE_U32_MAX   ((u32)~0U)
183
184 static OSAL_INLINE u16 ecore_chain_get_elem_left(struct ecore_chain *p_chain)
185 {
186         u16 used;
187
188         OSAL_ASSERT(is_chain_u16(p_chain));
189
190         used = (u16)(((u32)ECORE_U16_MAX + 1 +
191                       (u32)(p_chain->u.chain16.prod_idx)) -
192                      (u32)p_chain->u.chain16.cons_idx);
193         if (p_chain->mode == ECORE_CHAIN_MODE_NEXT_PTR)
194                 used -= p_chain->u.chain16.prod_idx / p_chain->elem_per_page -
195                         p_chain->u.chain16.cons_idx / p_chain->elem_per_page;
196
197         return (u16)(p_chain->capacity - used);
198 }
199
200 static OSAL_INLINE u32
201 ecore_chain_get_elem_left_u32(struct ecore_chain *p_chain)
202 {
203         u32 used;
204
205         OSAL_ASSERT(is_chain_u32(p_chain));
206
207         used = (u32)(((u64)ECORE_U32_MAX + 1 +
208                       (u64)(p_chain->u.chain32.prod_idx)) -
209                      (u64)p_chain->u.chain32.cons_idx);
210         if (p_chain->mode == ECORE_CHAIN_MODE_NEXT_PTR)
211                 used -= p_chain->u.chain32.prod_idx / p_chain->elem_per_page -
212                         p_chain->u.chain32.cons_idx / p_chain->elem_per_page;
213
214         return p_chain->capacity - used;
215 }
216
217 static OSAL_INLINE u8 ecore_chain_is_full(struct ecore_chain *p_chain)
218 {
219         if (is_chain_u16(p_chain))
220                 return (ecore_chain_get_elem_left(p_chain) ==
221                         p_chain->capacity);
222         else
223                 return (ecore_chain_get_elem_left_u32(p_chain) ==
224                         p_chain->capacity);
225 }
226
227 static OSAL_INLINE u8 ecore_chain_is_empty(struct ecore_chain *p_chain)
228 {
229         if (is_chain_u16(p_chain))
230                 return (ecore_chain_get_elem_left(p_chain) == 0);
231         else
232                 return (ecore_chain_get_elem_left_u32(p_chain) == 0);
233 }
234
235 static OSAL_INLINE
236 u16 ecore_chain_get_elem_per_page(struct ecore_chain *p_chain)
237 {
238         return p_chain->elem_per_page;
239 }
240
241 static OSAL_INLINE
242 u16 ecore_chain_get_usable_per_page(struct ecore_chain *p_chain)
243 {
244         return p_chain->usable_per_page;
245 }
246
247 static OSAL_INLINE
248 u16 ecore_chain_get_unusable_per_page(struct ecore_chain *p_chain)
249 {
250         return p_chain->elem_unusable;
251 }
252
253 static OSAL_INLINE u32 ecore_chain_get_size(struct ecore_chain *p_chain)
254 {
255         return p_chain->size;
256 }
257
258 static OSAL_INLINE u32 ecore_chain_get_page_cnt(struct ecore_chain *p_chain)
259 {
260         return p_chain->page_cnt;
261 }
262
263 static OSAL_INLINE
264 dma_addr_t ecore_chain_get_pbl_phys(struct ecore_chain *p_chain)
265 {
266         return p_chain->pbl.p_phys_table;
267 }
268
269 /**
270  * @brief ecore_chain_advance_page -
271  *
272  * Advance the next element accros pages for a linked chain
273  *
274  * @param p_chain
275  * @param p_next_elem
276  * @param idx_to_inc
277  * @param page_to_inc
278  */
279 static OSAL_INLINE void
280 ecore_chain_advance_page(struct ecore_chain *p_chain, void **p_next_elem,
281                          void *idx_to_inc, void *page_to_inc)
282 {
283         struct ecore_chain_next *p_next = OSAL_NULL;
284         u32 page_index = 0;
285
286         switch (p_chain->mode) {
287         case ECORE_CHAIN_MODE_NEXT_PTR:
288                 p_next = (struct ecore_chain_next *)(*p_next_elem);
289                 *p_next_elem = p_next->next_virt;
290                 if (is_chain_u16(p_chain))
291                         *(u16 *)idx_to_inc += p_chain->elem_unusable;
292                 else
293                         *(u32 *)idx_to_inc += p_chain->elem_unusable;
294                 break;
295         case ECORE_CHAIN_MODE_SINGLE:
296                 *p_next_elem = p_chain->p_virt_addr;
297                 break;
298         case ECORE_CHAIN_MODE_PBL:
299                 if (is_chain_u16(p_chain)) {
300                         if (++(*(u16 *)page_to_inc) == p_chain->page_cnt)
301                                 *(u16 *)page_to_inc = 0;
302                         page_index = *(u16 *)page_to_inc;
303                 } else {
304                         if (++(*(u32 *)page_to_inc) == p_chain->page_cnt)
305                                 *(u32 *)page_to_inc = 0;
306                         page_index = *(u32 *)page_to_inc;
307                 }
308                 *p_next_elem = p_chain->pbl.pp_virt_addr_tbl[page_index];
309         }
310 }
311
312 #define is_unusable_idx(p, idx)                 \
313         (((p)->u.chain16.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
314
315 #define is_unusable_idx_u32(p, idx)             \
316         (((p)->u.chain32.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
317
318 #define is_unusable_next_idx(p, idx)            \
319         ((((p)->u.chain16.idx + 1) &            \
320         (p)->elem_per_page_mask) == (p)->usable_per_page)
321
322 #define is_unusable_next_idx_u32(p, idx)        \
323         ((((p)->u.chain32.idx + 1) &            \
324         (p)->elem_per_page_mask) == (p)->usable_per_page)
325
326 #define test_and_skip(p, idx)                                           \
327         do {                                                            \
328                 if (is_chain_u16(p)) {                                  \
329                         if (is_unusable_idx(p, idx))                    \
330                                 (p)->u.chain16.idx +=                   \
331                                         (p)->elem_unusable;             \
332                 } else {                                                \
333                         if (is_unusable_idx_u32(p, idx))                \
334                                 (p)->u.chain32.idx +=                   \
335                                         (p)->elem_unusable;             \
336                 }                                                       \
337         } while (0)
338
339 /**
340  * @brief ecore_chain_return_multi_produced -
341  *
342  * A chain in which the driver "Produces" elements should use this API
343  * to indicate previous produced elements are now consumed.
344  *
345  * @param p_chain
346  * @param num
347  */
348 static OSAL_INLINE
349 void ecore_chain_return_multi_produced(struct ecore_chain *p_chain, u32 num)
350 {
351         if (is_chain_u16(p_chain))
352                 p_chain->u.chain16.cons_idx += (u16)num;
353         else
354                 p_chain->u.chain32.cons_idx += num;
355         test_and_skip(p_chain, cons_idx);
356 }
357
358 /**
359  * @brief ecore_chain_return_produced -
360  *
361  * A chain in which the driver "Produces" elements should use this API
362  * to indicate previous produced elements are now consumed.
363  *
364  * @param p_chain
365  */
366 static OSAL_INLINE void ecore_chain_return_produced(struct ecore_chain *p_chain)
367 {
368         if (is_chain_u16(p_chain))
369                 p_chain->u.chain16.cons_idx++;
370         else
371                 p_chain->u.chain32.cons_idx++;
372         test_and_skip(p_chain, cons_idx);
373 }
374
375 /**
376  * @brief ecore_chain_produce -
377  *
378  * A chain in which the driver "Produces" elements should use this to get
379  * a pointer to the next element which can be "Produced". It's driver
380  * responsibility to validate that the chain has room for new element.
381  *
382  * @param p_chain
383  *
384  * @return void*, a pointer to next element
385  */
386 static OSAL_INLINE void *ecore_chain_produce(struct ecore_chain *p_chain)
387 {
388         void *p_ret = OSAL_NULL, *p_prod_idx, *p_prod_page_idx;
389
390         if (is_chain_u16(p_chain)) {
391                 if ((p_chain->u.chain16.prod_idx &
392                      p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
393                         p_prod_idx = &p_chain->u.chain16.prod_idx;
394                         p_prod_page_idx = &p_chain->pbl.u.pbl16.prod_page_idx;
395                         ecore_chain_advance_page(p_chain, &p_chain->p_prod_elem,
396                                                  p_prod_idx, p_prod_page_idx);
397                 }
398                 p_chain->u.chain16.prod_idx++;
399         } else {
400                 if ((p_chain->u.chain32.prod_idx &
401                      p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
402                         p_prod_idx = &p_chain->u.chain32.prod_idx;
403                         p_prod_page_idx = &p_chain->pbl.u.pbl32.prod_page_idx;
404                         ecore_chain_advance_page(p_chain, &p_chain->p_prod_elem,
405                                                  p_prod_idx, p_prod_page_idx);
406                 }
407                 p_chain->u.chain32.prod_idx++;
408         }
409
410         p_ret = p_chain->p_prod_elem;
411         p_chain->p_prod_elem = (void *)(((u8 *)p_chain->p_prod_elem) +
412                                         p_chain->elem_size);
413
414         return p_ret;
415 }
416
417 /**
418  * @brief ecore_chain_get_capacity -
419  *
420  * Get the maximum number of BDs in chain
421  *
422  * @param p_chain
423  * @param num
424  *
425  * @return number of unusable BDs
426  */
427 static OSAL_INLINE u32 ecore_chain_get_capacity(struct ecore_chain *p_chain)
428 {
429         return p_chain->capacity;
430 }
431
432 /**
433  * @brief ecore_chain_recycle_consumed -
434  *
435  * Returns an element which was previously consumed;
436  * Increments producers so they could be written to FW.
437  *
438  * @param p_chain
439  */
440 static OSAL_INLINE
441 void ecore_chain_recycle_consumed(struct ecore_chain *p_chain)
442 {
443         test_and_skip(p_chain, prod_idx);
444         if (is_chain_u16(p_chain))
445                 p_chain->u.chain16.prod_idx++;
446         else
447                 p_chain->u.chain32.prod_idx++;
448 }
449
450 /**
451  * @brief ecore_chain_consume -
452  *
453  * A Chain in which the driver utilizes data written by a different source
454  * (i.e., FW) should use this to access passed buffers.
455  *
456  * @param p_chain
457  *
458  * @return void*, a pointer to the next buffer written
459  */
460 static OSAL_INLINE void *ecore_chain_consume(struct ecore_chain *p_chain)
461 {
462         void *p_ret = OSAL_NULL, *p_cons_idx, *p_cons_page_idx;
463
464         if (is_chain_u16(p_chain)) {
465                 if ((p_chain->u.chain16.cons_idx &
466                      p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
467                         p_cons_idx = &p_chain->u.chain16.cons_idx;
468                         p_cons_page_idx = &p_chain->pbl.u.pbl16.cons_page_idx;
469                         ecore_chain_advance_page(p_chain, &p_chain->p_cons_elem,
470                                                  p_cons_idx, p_cons_page_idx);
471                 }
472                 p_chain->u.chain16.cons_idx++;
473         } else {
474                 if ((p_chain->u.chain32.cons_idx &
475                      p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
476                         p_cons_idx = &p_chain->u.chain32.cons_idx;
477                         p_cons_page_idx = &p_chain->pbl.u.pbl32.cons_page_idx;
478                         ecore_chain_advance_page(p_chain, &p_chain->p_cons_elem,
479                                                  p_cons_idx, p_cons_page_idx);
480                 }
481                 p_chain->u.chain32.cons_idx++;
482         }
483
484         p_ret = p_chain->p_cons_elem;
485         p_chain->p_cons_elem = (void *)(((u8 *)p_chain->p_cons_elem) +
486                                         p_chain->elem_size);
487
488         return p_ret;
489 }
490
491 /**
492  * @brief ecore_chain_reset -
493  *
494  * Resets the chain to its start state
495  *
496  * @param p_chain pointer to a previously allocted chain
497  */
498 static OSAL_INLINE void ecore_chain_reset(struct ecore_chain *p_chain)
499 {
500         u32 i;
501
502         if (is_chain_u16(p_chain)) {
503                 p_chain->u.chain16.prod_idx = 0;
504                 p_chain->u.chain16.cons_idx = 0;
505         } else {
506                 p_chain->u.chain32.prod_idx = 0;
507                 p_chain->u.chain32.cons_idx = 0;
508         }
509         p_chain->p_cons_elem = p_chain->p_virt_addr;
510         p_chain->p_prod_elem = p_chain->p_virt_addr;
511
512         if (p_chain->mode == ECORE_CHAIN_MODE_PBL) {
513                 /* Use (page_cnt - 1) as a reset value for the prod/cons page's
514                  * indices, to avoid unnecessary page advancing on the first
515                  * call to ecore_chain_produce/consume. Instead, the indices
516                  * will be advanced to page_cnt and then will be wrapped to 0.
517                  */
518                 u32 reset_val = p_chain->page_cnt - 1;
519
520                 if (is_chain_u16(p_chain)) {
521                         p_chain->pbl.u.pbl16.prod_page_idx = (u16)reset_val;
522                         p_chain->pbl.u.pbl16.cons_page_idx = (u16)reset_val;
523                 } else {
524                         p_chain->pbl.u.pbl32.prod_page_idx = reset_val;
525                         p_chain->pbl.u.pbl32.cons_page_idx = reset_val;
526                 }
527         }
528
529         switch (p_chain->intended_use) {
530         case ECORE_CHAIN_USE_TO_CONSUME_PRODUCE:
531         case ECORE_CHAIN_USE_TO_PRODUCE:
532                         /* Do nothing */
533                         break;
534
535         case ECORE_CHAIN_USE_TO_CONSUME:
536                         /* produce empty elements */
537                         for (i = 0; i < p_chain->capacity; i++)
538                         ecore_chain_recycle_consumed(p_chain);
539                         break;
540         }
541 }
542
543 /**
544  * @brief ecore_chain_init_params -
545  *
546  * Initalizes a basic chain struct
547  *
548  * @param p_chain
549  * @param page_cnt      number of pages in the allocated buffer
550  * @param elem_size     size of each element in the chain
551  * @param intended_use
552  * @param mode
553  * @param cnt_type
554  * @param dp_ctx
555  */
556 static OSAL_INLINE void
557 ecore_chain_init_params(struct ecore_chain *p_chain, u32 page_cnt, u8 elem_size,
558                         enum ecore_chain_use_mode intended_use,
559                         enum ecore_chain_mode mode,
560                         enum ecore_chain_cnt_type cnt_type, void *dp_ctx)
561 {
562         /* chain fixed parameters */
563         p_chain->p_virt_addr = OSAL_NULL;
564         p_chain->p_phys_addr = 0;
565         p_chain->elem_size = elem_size;
566         p_chain->intended_use = intended_use;
567         p_chain->mode = mode;
568         p_chain->cnt_type = cnt_type;
569
570         p_chain->elem_per_page = ELEMS_PER_PAGE(elem_size);
571         p_chain->usable_per_page = USABLE_ELEMS_PER_PAGE(elem_size, mode);
572         p_chain->elem_per_page_mask = p_chain->elem_per_page - 1;
573         p_chain->elem_unusable = UNUSABLE_ELEMS_PER_PAGE(elem_size, mode);
574         p_chain->next_page_mask = (p_chain->usable_per_page &
575                                    p_chain->elem_per_page_mask);
576
577         p_chain->page_cnt = page_cnt;
578         p_chain->capacity = p_chain->usable_per_page * page_cnt;
579         p_chain->size = p_chain->elem_per_page * page_cnt;
580         p_chain->pbl.external = false;
581         p_chain->pbl.p_phys_table = 0;
582         p_chain->pbl.p_virt_table = OSAL_NULL;
583         p_chain->pbl.pp_virt_addr_tbl = OSAL_NULL;
584
585         p_chain->dp_ctx = dp_ctx;
586 }
587
588 /**
589  * @brief ecore_chain_init_mem -
590  *
591  * Initalizes a basic chain struct with its chain buffers
592  *
593  * @param p_chain
594  * @param p_virt_addr   virtual address of allocated buffer's beginning
595  * @param p_phys_addr   physical address of allocated buffer's beginning
596  *
597  */
598 static OSAL_INLINE void ecore_chain_init_mem(struct ecore_chain *p_chain,
599                                              void *p_virt_addr,
600                                              dma_addr_t p_phys_addr)
601 {
602         p_chain->p_virt_addr = p_virt_addr;
603         p_chain->p_phys_addr = p_phys_addr;
604 }
605
606 /**
607  * @brief ecore_chain_init_pbl_mem -
608  *
609  * Initalizes a basic chain struct with its pbl buffers
610  *
611  * @param p_chain
612  * @param p_virt_pbl    pointer to a pre allocated side table which will hold
613  *                      virtual page addresses.
614  * @param p_phys_pbl    pointer to a pre-allocated side table which will hold
615  *                      physical page addresses.
616  * @param pp_virt_addr_tbl
617  *                      pointer to a pre-allocated side table which will hold
618  *                      the virtual addresses of the chain pages.
619  *
620  */
621 static OSAL_INLINE void ecore_chain_init_pbl_mem(struct ecore_chain *p_chain,
622                                                  void *p_virt_pbl,
623                                                  dma_addr_t p_phys_pbl,
624                                                  void **pp_virt_addr_tbl)
625 {
626         p_chain->pbl.p_phys_table = p_phys_pbl;
627         p_chain->pbl.p_virt_table = p_virt_pbl;
628         p_chain->pbl.pp_virt_addr_tbl = pp_virt_addr_tbl;
629 }
630
631 /**
632  * @brief ecore_chain_init_next_ptr_elem -
633  *
634  * Initalizes a next pointer element
635  *
636  * @param p_chain
637  * @param p_virt_curr   virtual address of a chain page of which the next
638  *                      pointer element is initialized
639  * @param p_virt_next   virtual address of the next chain page
640  * @param p_phys_next   physical address of the next chain page
641  *
642  */
643 static OSAL_INLINE void
644 ecore_chain_init_next_ptr_elem(struct ecore_chain *p_chain, void *p_virt_curr,
645                                void *p_virt_next, dma_addr_t p_phys_next)
646 {
647         struct ecore_chain_next *p_next;
648         u32 size;
649
650         size = p_chain->elem_size * p_chain->usable_per_page;
651         p_next = (struct ecore_chain_next *)((u8 *)p_virt_curr + size);
652
653         DMA_REGPAIR_LE(p_next->next_phys, p_phys_next);
654
655         p_next->next_virt = p_virt_next;
656 }
657
658 /**
659  * @brief ecore_chain_get_last_elem -
660  *
661  * Returns a pointer to the last element of the chain
662  *
663  * @param p_chain
664  *
665  * @return void*
666  */
667 static OSAL_INLINE void *ecore_chain_get_last_elem(struct ecore_chain *p_chain)
668 {
669         struct ecore_chain_next *p_next = OSAL_NULL;
670         void *p_virt_addr = OSAL_NULL;
671         u32 size, last_page_idx;
672
673         if (!p_chain->p_virt_addr)
674                 goto out;
675
676         switch (p_chain->mode) {
677         case ECORE_CHAIN_MODE_NEXT_PTR:
678                 size = p_chain->elem_size * p_chain->usable_per_page;
679                 p_virt_addr = p_chain->p_virt_addr;
680                 p_next = (struct ecore_chain_next *)((u8 *)p_virt_addr + size);
681                 while (p_next->next_virt != p_chain->p_virt_addr) {
682                         p_virt_addr = p_next->next_virt;
683                         p_next =
684                             (struct ecore_chain_next *)((u8 *)p_virt_addr +
685                                                         size);
686                 }
687                 break;
688         case ECORE_CHAIN_MODE_SINGLE:
689                 p_virt_addr = p_chain->p_virt_addr;
690                 break;
691         case ECORE_CHAIN_MODE_PBL:
692                 last_page_idx = p_chain->page_cnt - 1;
693                 p_virt_addr = p_chain->pbl.pp_virt_addr_tbl[last_page_idx];
694                 break;
695         }
696         /* p_virt_addr points at this stage to the last page of the chain */
697         size = p_chain->elem_size * (p_chain->usable_per_page - 1);
698         p_virt_addr = ((u8 *)p_virt_addr + size);
699 out:
700         return p_virt_addr;
701 }
702
703 /**
704  * @brief ecore_chain_set_prod - sets the prod to the given value
705  *
706  * @param prod_idx
707  * @param p_prod_elem
708  */
709 static OSAL_INLINE void ecore_chain_set_prod(struct ecore_chain *p_chain,
710                                              u32 prod_idx, void *p_prod_elem)
711 {
712         if (is_chain_u16(p_chain))
713                 p_chain->u.chain16.prod_idx = (u16)prod_idx;
714         else
715                 p_chain->u.chain32.prod_idx = prod_idx;
716         p_chain->p_prod_elem = p_prod_elem;
717 }
718
719 /**
720  * @brief ecore_chain_pbl_zero_mem - set chain memory to 0
721  *
722  * @param p_chain
723  */
724 static OSAL_INLINE void ecore_chain_pbl_zero_mem(struct ecore_chain *p_chain)
725 {
726         u32 i, page_cnt;
727
728         if (p_chain->mode != ECORE_CHAIN_MODE_PBL)
729                 return;
730
731         page_cnt = ecore_chain_get_page_cnt(p_chain);
732
733         for (i = 0; i < page_cnt; i++)
734                 OSAL_MEM_ZERO(p_chain->pbl.pp_virt_addr_tbl[i],
735                               ECORE_CHAIN_PAGE_SIZE);
736 }
737
738 int ecore_chain_print(struct ecore_chain *p_chain, char *buffer,
739                       u32 buffer_size, u32 *element_indx, u32 stop_indx,
740                       bool print_metadata,
741                       int (*func_ptr_print_element)(struct ecore_chain *p_chain,
742                                                     void *p_element,
743                                                     char *buffer),
744                       int (*func_ptr_print_metadata)(struct ecore_chain
745                                                      *p_chain,
746                                                      char *buffer));
747
748 #endif /* __ECORE_CHAIN_H__ */