]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/qlnx/qlnxe/ecore_chain.h
MFC r316485
[FreeBSD/stable/10.git] / sys / dev / qlnx / qlnxe / ecore_chain.h
1 /*
2  * Copyright (c) 2017-2018 Cavium, Inc. 
3  * All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions
7  *  are met:
8  *
9  *  1. Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  *  2. Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *
15  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  *  POSSIBILITY OF SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  *
29  */
30
31 #ifndef __ECORE_CHAIN_H__
32 #define __ECORE_CHAIN_H__
33
34 #include "common_hsi.h"
35 #include "ecore_utils.h"
36
37 enum ecore_chain_mode
38 {
39         /* Each Page contains a next pointer at its end */
40         ECORE_CHAIN_MODE_NEXT_PTR,
41
42         /* Chain is a single page (next ptr) is unrequired */
43         ECORE_CHAIN_MODE_SINGLE,
44
45         /* Page pointers are located in a side list */
46         ECORE_CHAIN_MODE_PBL,
47 };
48
49 enum ecore_chain_use_mode
50 {
51         ECORE_CHAIN_USE_TO_PRODUCE,             /* Chain starts empty */
52         ECORE_CHAIN_USE_TO_CONSUME,             /* Chain starts full */
53         ECORE_CHAIN_USE_TO_CONSUME_PRODUCE,     /* Chain starts empty */
54 };
55
56 enum ecore_chain_cnt_type {
57         /* The chain's size/prod/cons are kept in 16-bit variables */
58         ECORE_CHAIN_CNT_TYPE_U16,
59
60         /* The chain's size/prod/cons are kept in 32-bit variables  */
61         ECORE_CHAIN_CNT_TYPE_U32,
62 };
63
64 struct ecore_chain_next
65 {
66         struct regpair  next_phys;
67         void            *next_virt;
68 };
69
70 struct ecore_chain_pbl_u16 {
71         u16     prod_page_idx;
72         u16     cons_page_idx;
73 };
74
75 struct ecore_chain_pbl_u32 {
76         u32     prod_page_idx;
77         u32     cons_page_idx;
78 };
79
80 struct ecore_chain_ext_pbl
81 {
82         dma_addr_t      p_pbl_phys;
83         void            *p_pbl_virt;
84 };
85
86 struct ecore_chain_u16 {
87         /* Cyclic index of next element to produce/consme */
88         u16     prod_idx;
89         u16     cons_idx;
90 };
91
92 struct ecore_chain_u32 {
93         /* Cyclic index of next element to produce/consme */
94         u32     prod_idx;
95         u32     cons_idx;
96 };
97
98 struct ecore_chain
99 {
100         /* fastpath portion of the chain - required for commands such
101          * as produce / consume.
102          */
103         /* Point to next element to produce/consume */
104         void                            *p_prod_elem;
105         void                            *p_cons_elem;
106
107         /* Fastpath portions of the PBL [if exists] */
108
109         struct {
110                 /* Table for keeping the virtual addresses of the chain pages,
111                  * respectively to the physical addresses in the pbl table.
112                  */
113                 void            **pp_virt_addr_tbl;
114
115                 union {
116                         struct ecore_chain_pbl_u16      pbl_u16;
117                         struct ecore_chain_pbl_u32      pbl_u32;
118                 } c;
119         } pbl;
120
121         union {
122                 struct ecore_chain_u16  chain16;
123                 struct ecore_chain_u32  chain32;
124         } u;
125
126         /* Capacity counts only usable elements */
127         u32                             capacity;
128         u32                             page_cnt;
129
130         /* A u8 would suffice for mode, but it would save as a lot of headaches
131          * on castings & defaults.
132          */
133         enum ecore_chain_mode           mode;
134
135         /* Elements information for fast calculations */
136         u16                             elem_per_page;
137         u16                             elem_per_page_mask;
138         u16                             elem_size;
139         u16                             next_page_mask;
140         u16                             usable_per_page;
141         u8                              elem_unusable;
142
143         u8                              cnt_type;
144
145         /* Slowpath of the chain - required for initialization and destruction,
146          * but isn't involved in regular functionality.
147          */
148
149         /* Base address of a pre-allocated buffer for pbl */
150         struct {
151                 dma_addr_t              p_phys_table;
152                 void                    *p_virt_table;
153         } pbl_sp;
154
155         /* Address of first page of the chain  - the address is required
156          * for fastpath operation [consume/produce] but only for the the SINGLE
157          * flavour which isn't considered fastpath [== SPQ].
158          */
159         void                            *p_virt_addr;
160         dma_addr_t                      p_phys_addr;
161
162         /* Total number of elements [for entire chain] */
163         u32                             size;
164
165         u8                              intended_use;
166
167         /* TBD - do we really need this? Couldn't find usage for it */
168         bool                            b_external_pbl;
169
170         void                            *dp_ctx;
171 };
172
173 #define ECORE_CHAIN_PBL_ENTRY_SIZE      (8)
174 #define ECORE_CHAIN_PAGE_SIZE           (0x1000)
175 #define ELEMS_PER_PAGE(elem_size)       (ECORE_CHAIN_PAGE_SIZE/(elem_size))
176
177 #define UNUSABLE_ELEMS_PER_PAGE(elem_size, mode)                \
178           ((mode == ECORE_CHAIN_MODE_NEXT_PTR) ?                \
179            (u8)(1 + ((sizeof(struct ecore_chain_next)-1) /      \
180                      (elem_size))) : 0)
181
182 #define USABLE_ELEMS_PER_PAGE(elem_size, mode)                  \
183           ((u32) (ELEMS_PER_PAGE(elem_size) -                   \
184                   UNUSABLE_ELEMS_PER_PAGE(elem_size, mode)))
185
186 #define ECORE_CHAIN_PAGE_CNT(elem_cnt, elem_size, mode)         \
187         DIV_ROUND_UP(elem_cnt, USABLE_ELEMS_PER_PAGE(elem_size, mode))
188
189 #define is_chain_u16(p) ((p)->cnt_type == ECORE_CHAIN_CNT_TYPE_U16)
190 #define is_chain_u32(p) ((p)->cnt_type == ECORE_CHAIN_CNT_TYPE_U32)
191
192 /* Accessors */
193 static OSAL_INLINE u16 ecore_chain_get_prod_idx(struct ecore_chain *p_chain)
194 {
195         OSAL_ASSERT(is_chain_u16(p_chain));
196         return p_chain->u.chain16.prod_idx;
197 }
198
199 static OSAL_INLINE u32 ecore_chain_get_prod_idx_u32(struct ecore_chain *p_chain)
200 {
201         OSAL_ASSERT(is_chain_u32(p_chain));
202         return p_chain->u.chain32.prod_idx;
203 }
204
205 static OSAL_INLINE u16 ecore_chain_get_cons_idx(struct ecore_chain *p_chain)
206 {
207         OSAL_ASSERT(is_chain_u16(p_chain));
208         return p_chain->u.chain16.cons_idx;
209 }
210
211 static OSAL_INLINE u32 ecore_chain_get_cons_idx_u32(struct ecore_chain *p_chain)
212 {
213         OSAL_ASSERT(is_chain_u32(p_chain));
214         return p_chain->u.chain32.cons_idx;
215 }
216
217 #define ECORE_U16_MAX   ((u16)~0U)
218 #define ECORE_U32_MAX   ((u32)~0U)
219
220 static OSAL_INLINE u16 ecore_chain_get_elem_left(struct ecore_chain *p_chain)
221 {
222         u16 used;
223
224         OSAL_ASSERT(is_chain_u16(p_chain));
225
226         used = (u16)(((u32)ECORE_U16_MAX + 1 +
227                       (u32)(p_chain->u.chain16.prod_idx)) -
228                      (u32)p_chain->u.chain16.cons_idx);
229         if (p_chain->mode == ECORE_CHAIN_MODE_NEXT_PTR)
230                 used -= p_chain->u.chain16.prod_idx / p_chain->elem_per_page -
231                         p_chain->u.chain16.cons_idx / p_chain->elem_per_page;
232
233         return (u16)(p_chain->capacity - used);
234 }
235
236 static OSAL_INLINE u32
237 ecore_chain_get_elem_left_u32(struct ecore_chain *p_chain)
238 {
239         u32 used;
240
241         OSAL_ASSERT(is_chain_u32(p_chain));
242
243         used = (u32)(((u64)ECORE_U32_MAX + 1 +
244                       (u64)(p_chain->u.chain32.prod_idx)) -
245                      (u64)p_chain->u.chain32.cons_idx);
246         if (p_chain->mode == ECORE_CHAIN_MODE_NEXT_PTR)
247                 used -= p_chain->u.chain32.prod_idx / p_chain->elem_per_page -
248                         p_chain->u.chain32.cons_idx / p_chain->elem_per_page;
249
250         return p_chain->capacity - used;
251 }
252
253 static OSAL_INLINE u8 ecore_chain_is_full(struct ecore_chain *p_chain)
254 {
255         if (is_chain_u16(p_chain))
256                 return (ecore_chain_get_elem_left(p_chain) ==
257                         p_chain->capacity);
258         else
259                 return (ecore_chain_get_elem_left_u32(p_chain) ==
260                         p_chain->capacity);
261 }
262
263 static OSAL_INLINE u8 ecore_chain_is_empty(struct ecore_chain *p_chain)
264 {
265         if (is_chain_u16(p_chain))
266                 return (ecore_chain_get_elem_left(p_chain) == 0);
267         else
268                 return (ecore_chain_get_elem_left_u32(p_chain) == 0);
269 }
270
271 static OSAL_INLINE
272 u16 ecore_chain_get_elem_per_page(struct ecore_chain *p_chain)
273 {
274         return p_chain->elem_per_page;
275 }
276
277 static OSAL_INLINE
278 u16 ecore_chain_get_usable_per_page(struct ecore_chain *p_chain)
279 {
280         return p_chain->usable_per_page;
281 }
282
283 static OSAL_INLINE
284 u8 ecore_chain_get_unusable_per_page(struct ecore_chain *p_chain)
285 {
286         return p_chain->elem_unusable;
287 }
288
289 static OSAL_INLINE u32 ecore_chain_get_size(struct ecore_chain *p_chain)
290 {
291         return p_chain->size;
292 }
293
294 static OSAL_INLINE u32 ecore_chain_get_page_cnt(struct ecore_chain *p_chain)
295 {
296         return p_chain->page_cnt;
297 }
298
299 static OSAL_INLINE
300 dma_addr_t ecore_chain_get_pbl_phys(struct ecore_chain *p_chain)
301 {
302         return p_chain->pbl_sp.p_phys_table;
303 }
304
305 /**
306  * @brief ecore_chain_advance_page -
307  *
308  * Advance the next element accros pages for a linked chain
309  *
310  * @param p_chain
311  * @param p_next_elem
312  * @param idx_to_inc
313  * @param page_to_inc
314  */
315 static OSAL_INLINE void
316 ecore_chain_advance_page(struct ecore_chain *p_chain, void **p_next_elem,
317                          void *idx_to_inc, void *page_to_inc)
318 {
319         struct ecore_chain_next *p_next = OSAL_NULL;
320         u32 page_index = 0;
321
322         switch(p_chain->mode) {
323         case ECORE_CHAIN_MODE_NEXT_PTR:
324                 p_next = (struct ecore_chain_next *)(*p_next_elem);
325                 *p_next_elem = p_next->next_virt;
326                 if (is_chain_u16(p_chain))
327                         *(u16 *)idx_to_inc += (u16)p_chain->elem_unusable;
328                 else
329                         *(u32 *)idx_to_inc += (u16)p_chain->elem_unusable;
330                 break;
331         case ECORE_CHAIN_MODE_SINGLE:
332                 *p_next_elem = p_chain->p_virt_addr;
333                 break;
334         case ECORE_CHAIN_MODE_PBL:
335                 if (is_chain_u16(p_chain)) {
336                         if (++(*(u16 *)page_to_inc) == p_chain->page_cnt)
337                                 *(u16 *)page_to_inc = 0;
338                         page_index = *(u16 *)page_to_inc;
339                 } else {
340                         if (++(*(u32 *)page_to_inc) == p_chain->page_cnt)
341                                 *(u32 *)page_to_inc = 0;
342                         page_index = *(u32 *)page_to_inc;
343                 }
344                 *p_next_elem = p_chain->pbl.pp_virt_addr_tbl[page_index];
345         }
346 }
347
348 #define is_unusable_idx(p, idx)                 \
349         (((p)->u.chain16.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
350
351 #define is_unusable_idx_u32(p, idx)             \
352         (((p)->u.chain32.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
353
354 #define is_unusable_next_idx(p, idx)            \
355         ((((p)->u.chain16.idx + 1) & (p)->elem_per_page_mask) == (p)->usable_per_page)
356
357 #define is_unusable_next_idx_u32(p, idx)        \
358         ((((p)->u.chain32.idx + 1) & (p)->elem_per_page_mask) == (p)->usable_per_page)
359
360 #define test_and_skip(p, idx)                                                   \
361         do {                                                                    \
362                 if (is_chain_u16(p)) {                                          \
363                         if (is_unusable_idx(p, idx))                            \
364                                 (p)->u.chain16.idx += (p)->elem_unusable;       \
365                 } else {                                                        \
366                         if (is_unusable_idx_u32(p, idx))                        \
367                                 (p)->u.chain32.idx += (p)->elem_unusable;       \
368                 }                                                               \
369         } while (0)
370
371 /**
372  * @brief ecore_chain_return_multi_produced -
373  *
374  * A chain in which the driver "Produces" elements should use this API
375  * to indicate previous produced elements are now consumed.
376  *
377  * @param p_chain
378  * @param num
379  */
380 static OSAL_INLINE
381 void ecore_chain_return_multi_produced(struct ecore_chain *p_chain, u32 num)
382 {
383         if (is_chain_u16(p_chain))
384                 p_chain->u.chain16.cons_idx += (u16)num;
385         else
386                 p_chain->u.chain32.cons_idx += num;
387         test_and_skip(p_chain, cons_idx);
388 }
389
390 /**
391  * @brief ecore_chain_return_produced -
392  *
393  * A chain in which the driver "Produces" elements should use this API
394  * to indicate previous produced elements are now consumed.
395  *
396  * @param p_chain
397  */
398 static OSAL_INLINE void ecore_chain_return_produced(struct ecore_chain *p_chain)
399 {
400         if (is_chain_u16(p_chain))
401                 p_chain->u.chain16.cons_idx++;
402         else
403                 p_chain->u.chain32.cons_idx++;
404         test_and_skip(p_chain, cons_idx);
405 }
406
407 /**
408  * @brief ecore_chain_produce -
409  *
410  * A chain in which the driver "Produces" elements should use this to get
411  * a pointer to the next element which can be "Produced". It's driver
412  * responsibility to validate that the chain has room for new element.
413  *
414  * @param p_chain
415  *
416  * @return void*, a pointer to next element
417  */
418 static OSAL_INLINE void *ecore_chain_produce(struct ecore_chain *p_chain)
419 {
420         void *p_ret = OSAL_NULL, *p_prod_idx, *p_prod_page_idx;
421
422         if (is_chain_u16(p_chain)) {
423                 if ((p_chain->u.chain16.prod_idx &
424                      p_chain->elem_per_page_mask) ==
425                     p_chain->next_page_mask) {
426                         p_prod_idx = &p_chain->u.chain16.prod_idx;
427                         p_prod_page_idx = &p_chain->pbl.c.pbl_u16.prod_page_idx;
428                         ecore_chain_advance_page(p_chain, &p_chain->p_prod_elem,
429                                                  p_prod_idx, p_prod_page_idx);
430                 }
431                 p_chain->u.chain16.prod_idx++;
432         } else {
433                 if ((p_chain->u.chain32.prod_idx &
434                      p_chain->elem_per_page_mask) ==
435                     p_chain->next_page_mask) {
436                         p_prod_idx = &p_chain->u.chain32.prod_idx;
437                         p_prod_page_idx = &p_chain->pbl.c.pbl_u32.prod_page_idx;
438                         ecore_chain_advance_page(p_chain, &p_chain->p_prod_elem,
439                                                  p_prod_idx, p_prod_page_idx);
440                 }
441                 p_chain->u.chain32.prod_idx++;
442         }
443
444         p_ret = p_chain->p_prod_elem;
445         p_chain->p_prod_elem = (void*)(((u8*)p_chain->p_prod_elem) +
446                                        p_chain->elem_size);
447
448         return p_ret;
449 }
450
451 /**
452  * @brief ecore_chain_get_capacity -
453  *
454  * Get the maximum number of BDs in chain
455  *
456  * @param p_chain
457  * @param num
458  *
459  * @return number of unusable BDs
460  */
461 static OSAL_INLINE u32 ecore_chain_get_capacity(struct ecore_chain *p_chain)
462 {
463         return p_chain->capacity;
464 }
465
466 /**
467  * @brief ecore_chain_recycle_consumed -
468  *
469  * Returns an element which was previously consumed;
470  * Increments producers so they could be written to FW.
471  *
472  * @param p_chain
473  */
474 static OSAL_INLINE
475 void ecore_chain_recycle_consumed(struct ecore_chain *p_chain)
476 {
477         test_and_skip(p_chain, prod_idx);
478         if (is_chain_u16(p_chain))
479                 p_chain->u.chain16.prod_idx++;
480         else
481                 p_chain->u.chain32.prod_idx++;
482 }
483
484 /**
485  * @brief ecore_chain_consume -
486  *
487  * A Chain in which the driver utilizes data written by a different source
488  * (i.e., FW) should use this to access passed buffers.
489  *
490  * @param p_chain
491  *
492  * @return void*, a pointer to the next buffer written
493  */
494 static OSAL_INLINE void *ecore_chain_consume(struct ecore_chain *p_chain)
495 {
496         void *p_ret = OSAL_NULL, *p_cons_idx, *p_cons_page_idx;
497
498         if (is_chain_u16(p_chain)) {
499                 if ((p_chain->u.chain16.cons_idx &
500                      p_chain->elem_per_page_mask) ==
501                     p_chain->next_page_mask) {
502                         p_cons_idx = &p_chain->u.chain16.cons_idx;
503                         p_cons_page_idx = &p_chain->pbl.c.pbl_u16.cons_page_idx;
504                         ecore_chain_advance_page(p_chain, &p_chain->p_cons_elem,
505                                                  p_cons_idx, p_cons_page_idx);
506                 }
507                 p_chain->u.chain16.cons_idx++;
508         } else {
509                 if ((p_chain->u.chain32.cons_idx &
510                      p_chain->elem_per_page_mask) ==
511                     p_chain->next_page_mask) {
512                         p_cons_idx = &p_chain->u.chain32.cons_idx;
513                         p_cons_page_idx = &p_chain->pbl.c.pbl_u32.cons_page_idx;
514                         ecore_chain_advance_page(p_chain, &p_chain->p_cons_elem,
515                                                  p_cons_idx, p_cons_page_idx);
516                 }
517                 p_chain->u.chain32.cons_idx++;
518         }
519
520         p_ret = p_chain->p_cons_elem;
521         p_chain->p_cons_elem = (void*)(((u8*)p_chain->p_cons_elem) +
522                                        p_chain->elem_size);
523
524         return p_ret;
525 }
526
527 /**
528  * @brief ecore_chain_reset -
529  *
530  * Resets the chain to its start state
531  *
532  * @param p_chain pointer to a previously allocted chain
533  */
534 static OSAL_INLINE void ecore_chain_reset(struct ecore_chain *p_chain)
535 {
536         u32 i;
537
538         if (is_chain_u16(p_chain)) {
539                 p_chain->u.chain16.prod_idx = 0;
540                 p_chain->u.chain16.cons_idx = 0;
541         } else {
542                 p_chain->u.chain32.prod_idx = 0;
543                 p_chain->u.chain32.cons_idx = 0;
544         }
545         p_chain->p_cons_elem = p_chain->p_virt_addr;
546         p_chain->p_prod_elem = p_chain->p_virt_addr;
547
548         if (p_chain->mode == ECORE_CHAIN_MODE_PBL) {
549                 /* Use (page_cnt - 1) as a reset value for the prod/cons page's
550                  * indices, to avoid unnecessary page advancing on the first
551                  * call to ecore_chain_produce/consume. Instead, the indices
552                  * will be advanced to page_cnt and then will be wrapped to 0.
553                  */
554                 u32 reset_val = p_chain->page_cnt - 1;
555
556                 if (is_chain_u16(p_chain)) {
557                         p_chain->pbl.c.pbl_u16.prod_page_idx = (u16)reset_val;
558                         p_chain->pbl.c.pbl_u16.cons_page_idx = (u16)reset_val;
559                 } else {
560                         p_chain->pbl.c.pbl_u32.prod_page_idx = reset_val;
561                         p_chain->pbl.c.pbl_u32.cons_page_idx = reset_val;
562                 }
563         }
564
565         switch (p_chain->intended_use) {
566         case ECORE_CHAIN_USE_TO_CONSUME:
567                 /* produce empty elements */
568                 for (i = 0; i < p_chain->capacity; i++)
569                         ecore_chain_recycle_consumed(p_chain);
570                 break;
571
572         case ECORE_CHAIN_USE_TO_CONSUME_PRODUCE:
573         case ECORE_CHAIN_USE_TO_PRODUCE:
574         default:
575                 /* Do nothing */
576                 break;
577         }
578 }
579
580 /**
581  * @brief ecore_chain_init_params -
582  *
583  * Initalizes a basic chain struct
584  *
585  * @param p_chain
586  * @param page_cnt      number of pages in the allocated buffer
587  * @param elem_size     size of each element in the chain
588  * @param intended_use
589  * @param mode
590  * @param cnt_type
591  * @param dp_ctx
592  */
593 static OSAL_INLINE void
594 ecore_chain_init_params(struct ecore_chain *p_chain, u32 page_cnt, u8 elem_size,
595                         enum ecore_chain_use_mode intended_use,
596                         enum ecore_chain_mode mode,
597                         enum ecore_chain_cnt_type cnt_type, void *dp_ctx)
598 {
599         /* chain fixed parameters */
600         p_chain->p_virt_addr = OSAL_NULL;
601         p_chain->p_phys_addr = 0;
602         p_chain->elem_size = elem_size;
603         p_chain->intended_use = (u8)intended_use;
604         p_chain->mode = mode;
605         p_chain->cnt_type = (u8)cnt_type;
606
607         p_chain->elem_per_page = ELEMS_PER_PAGE(elem_size);
608         p_chain->usable_per_page = USABLE_ELEMS_PER_PAGE(elem_size, mode);
609         p_chain->elem_per_page_mask = p_chain->elem_per_page - 1;
610         p_chain->elem_unusable = UNUSABLE_ELEMS_PER_PAGE(elem_size, mode);
611         p_chain->next_page_mask = (p_chain->usable_per_page &
612                                    p_chain->elem_per_page_mask);
613
614         p_chain->page_cnt = page_cnt;
615         p_chain->capacity = p_chain->usable_per_page * page_cnt;
616         p_chain->size = p_chain->elem_per_page * page_cnt;
617         p_chain->b_external_pbl = false;
618         p_chain->pbl_sp.p_phys_table = 0;
619         p_chain->pbl_sp.p_virt_table = OSAL_NULL;
620         p_chain->pbl.pp_virt_addr_tbl = OSAL_NULL;
621
622         p_chain->dp_ctx = dp_ctx;
623 }
624
625 /**
626  * @brief ecore_chain_init_mem -
627  *
628  * Initalizes a basic chain struct with its chain buffers
629  *
630  * @param p_chain
631  * @param p_virt_addr   virtual address of allocated buffer's beginning
632  * @param p_phys_addr   physical address of allocated buffer's beginning
633  *
634  */
635 static OSAL_INLINE void ecore_chain_init_mem(struct ecore_chain *p_chain,
636                                              void *p_virt_addr,
637                                              dma_addr_t p_phys_addr)
638 {
639         p_chain->p_virt_addr = p_virt_addr;
640         p_chain->p_phys_addr = p_phys_addr;
641 }
642
643 /**
644  * @brief ecore_chain_init_pbl_mem -
645  *
646  * Initalizes a basic chain struct with its pbl buffers
647  *
648  * @param p_chain
649  * @param p_virt_pbl    pointer to a pre allocated side table which will hold
650  *                      virtual page addresses.
651  * @param p_phys_pbl    pointer to a pre-allocated side table which will hold
652  *                      physical page addresses.
653  * @param pp_virt_addr_tbl
654  *                      pointer to a pre-allocated side table which will hold
655  *                      the virtual addresses of the chain pages.
656  *
657  */
658 static OSAL_INLINE void ecore_chain_init_pbl_mem(struct ecore_chain *p_chain,
659                                                  void *p_virt_pbl,
660                                                  dma_addr_t p_phys_pbl,
661                                                  void **pp_virt_addr_tbl)
662 {
663         p_chain->pbl_sp.p_phys_table = p_phys_pbl;
664         p_chain->pbl_sp.p_virt_table = p_virt_pbl;
665         p_chain->pbl.pp_virt_addr_tbl = pp_virt_addr_tbl;
666 }
667
668 /**
669  * @brief ecore_chain_init_next_ptr_elem -
670  *
671  * Initalizes a next pointer element
672  *
673  * @param p_chain
674  * @param p_virt_curr   virtual address of a chain page of which the next
675  *                      pointer element is initialized
676  * @param p_virt_next   virtual address of the next chain page
677  * @param p_phys_next   physical address of the next chain page
678  *
679  */
680 static OSAL_INLINE void
681 ecore_chain_init_next_ptr_elem(struct ecore_chain *p_chain, void *p_virt_curr,
682                                void *p_virt_next, dma_addr_t p_phys_next)
683 {
684         struct ecore_chain_next *p_next;
685         u32 size;
686
687         size = p_chain->elem_size * p_chain->usable_per_page;
688         p_next = (struct ecore_chain_next *)((u8 *)p_virt_curr + size);
689
690         DMA_REGPAIR_LE(p_next->next_phys, p_phys_next);
691
692         p_next->next_virt = p_virt_next;
693 }
694
695 /**
696  * @brief ecore_chain_get_last_elem -
697  *
698  * Returns a pointer to the last element of the chain
699  *
700  * @param p_chain
701  *
702  * @return void*
703  */
704 static OSAL_INLINE void *ecore_chain_get_last_elem(struct ecore_chain *p_chain)
705 {
706         struct ecore_chain_next *p_next = OSAL_NULL;
707         void *p_virt_addr = OSAL_NULL;
708         u32 size, last_page_idx;
709
710         if (!p_chain->p_virt_addr)
711                 goto out;
712
713         switch (p_chain->mode) {
714         case ECORE_CHAIN_MODE_NEXT_PTR:
715                 size = p_chain->elem_size * p_chain->usable_per_page;
716                 p_virt_addr = p_chain->p_virt_addr;
717                 p_next = (struct ecore_chain_next *)((u8 *)p_virt_addr + size);
718                 while (p_next->next_virt != p_chain->p_virt_addr) {
719                         p_virt_addr = p_next->next_virt;
720                         p_next = (struct ecore_chain_next *)((u8 *)p_virt_addr +
721                                                              size);
722                 }
723                 break;
724         case ECORE_CHAIN_MODE_SINGLE:
725                 p_virt_addr = p_chain->p_virt_addr;
726                 break;
727         case ECORE_CHAIN_MODE_PBL:
728                 last_page_idx = p_chain->page_cnt - 1;
729                 p_virt_addr = p_chain->pbl.pp_virt_addr_tbl[last_page_idx];
730                 break;
731         }
732         /* p_virt_addr points at this stage to the last page of the chain */
733         size = p_chain->elem_size * (p_chain->usable_per_page - 1);
734         p_virt_addr = (u8 *)p_virt_addr + size;
735 out:
736         return p_virt_addr;
737 }
738
739 /**
740  * @brief ecore_chain_set_prod - sets the prod to the given value
741  *
742  * @param prod_idx
743  * @param p_prod_elem
744  */
745 static OSAL_INLINE void ecore_chain_set_prod(struct ecore_chain *p_chain,
746                                              u32 prod_idx, void *p_prod_elem)
747 {
748         if (is_chain_u16(p_chain))
749                 p_chain->u.chain16.prod_idx = (u16)prod_idx;
750         else
751                 p_chain->u.chain32.prod_idx = prod_idx;
752         p_chain->p_prod_elem = p_prod_elem;
753 }
754
755 /**
756  * @brief ecore_chain_pbl_zero_mem - set chain memory to 0
757  *
758  * @param p_chain
759  */
760 static OSAL_INLINE void ecore_chain_pbl_zero_mem(struct ecore_chain *p_chain)
761 {
762         u32 i, page_cnt;
763
764         if (p_chain->mode != ECORE_CHAIN_MODE_PBL)
765                 return;
766
767         page_cnt = ecore_chain_get_page_cnt(p_chain);
768
769         for (i = 0; i < page_cnt; i++)
770                 OSAL_MEM_ZERO(p_chain->pbl.pp_virt_addr_tbl[i],
771                               ECORE_CHAIN_PAGE_SIZE);
772 }
773
774 int ecore_chain_print(struct ecore_chain *p_chain, char *buffer,
775                       u32 buffer_size, u32 *element_indx, u32 stop_indx,
776                       bool print_metadata,
777                       int (*func_ptr_print_element)(struct ecore_chain *p_chain,
778                                                     void *p_element,
779                                                     char *buffer),
780                       int (*func_ptr_print_metadata)(struct ecore_chain *p_chain,
781                                                      char *buffer));
782
783 #endif /* __ECORE_CHAIN_H__ */