]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/qlnx/qlnxe/ecore_chain.h
Pull down pjdfstest 0.1
[FreeBSD/FreeBSD.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 /* FIXME:
218  * Should create OSALs for the below definitions.
219  * For Linux, replace them with the existing U16_MAX and U32_MAX, and handle
220  * kernel versions that lack them.
221  */
222 #define ECORE_U16_MAX   ((u16)~0U)
223 #define ECORE_U32_MAX   ((u32)~0U)
224
225 static OSAL_INLINE u16 ecore_chain_get_elem_left(struct ecore_chain *p_chain)
226 {
227         u16 used;
228
229         OSAL_ASSERT(is_chain_u16(p_chain));
230
231         used = (u16)(((u32)ECORE_U16_MAX + 1 +
232                       (u32)(p_chain->u.chain16.prod_idx)) -
233                      (u32)p_chain->u.chain16.cons_idx);
234         if (p_chain->mode == ECORE_CHAIN_MODE_NEXT_PTR)
235                 used -= p_chain->u.chain16.prod_idx / p_chain->elem_per_page -
236                         p_chain->u.chain16.cons_idx / p_chain->elem_per_page;
237
238         return (u16)(p_chain->capacity - used);
239 }
240
241 static OSAL_INLINE u32
242 ecore_chain_get_elem_left_u32(struct ecore_chain *p_chain)
243 {
244         u32 used;
245
246         OSAL_ASSERT(is_chain_u32(p_chain));
247
248         used = (u32)(((u64)ECORE_U32_MAX + 1 +
249                       (u64)(p_chain->u.chain32.prod_idx)) -
250                      (u64)p_chain->u.chain32.cons_idx);
251         if (p_chain->mode == ECORE_CHAIN_MODE_NEXT_PTR)
252                 used -= p_chain->u.chain32.prod_idx / p_chain->elem_per_page -
253                         p_chain->u.chain32.cons_idx / p_chain->elem_per_page;
254
255         return p_chain->capacity - used;
256 }
257
258 static OSAL_INLINE u8 ecore_chain_is_full(struct ecore_chain *p_chain)
259 {
260         if (is_chain_u16(p_chain))
261                 return (ecore_chain_get_elem_left(p_chain) ==
262                         p_chain->capacity);
263         else
264                 return (ecore_chain_get_elem_left_u32(p_chain) ==
265                         p_chain->capacity);
266 }
267
268 static OSAL_INLINE u8 ecore_chain_is_empty(struct ecore_chain *p_chain)
269 {
270         if (is_chain_u16(p_chain))
271                 return (ecore_chain_get_elem_left(p_chain) == 0);
272         else
273                 return (ecore_chain_get_elem_left_u32(p_chain) == 0);
274 }
275
276 static OSAL_INLINE
277 u16 ecore_chain_get_elem_per_page(struct ecore_chain *p_chain)
278 {
279         return p_chain->elem_per_page;
280 }
281
282 static OSAL_INLINE
283 u16 ecore_chain_get_usable_per_page(struct ecore_chain *p_chain)
284 {
285         return p_chain->usable_per_page;
286 }
287
288 static OSAL_INLINE
289 u8 ecore_chain_get_unusable_per_page(struct ecore_chain *p_chain)
290 {
291         return p_chain->elem_unusable;
292 }
293
294 static OSAL_INLINE u32 ecore_chain_get_size(struct ecore_chain *p_chain)
295 {
296         return p_chain->size;
297 }
298
299 static OSAL_INLINE u32 ecore_chain_get_page_cnt(struct ecore_chain *p_chain)
300 {
301         return p_chain->page_cnt;
302 }
303
304 static OSAL_INLINE
305 dma_addr_t ecore_chain_get_pbl_phys(struct ecore_chain *p_chain)
306 {
307         return p_chain->pbl_sp.p_phys_table;
308 }
309
310 /**
311  * @brief ecore_chain_advance_page -
312  *
313  * Advance the next element accros pages for a linked chain
314  *
315  * @param p_chain
316  * @param p_next_elem
317  * @param idx_to_inc
318  * @param page_to_inc
319  */
320 static OSAL_INLINE void
321 ecore_chain_advance_page(struct ecore_chain *p_chain, void **p_next_elem,
322                          void *idx_to_inc, void *page_to_inc)
323 {
324         struct ecore_chain_next *p_next = OSAL_NULL;
325         u32 page_index = 0;
326
327         switch(p_chain->mode) {
328         case ECORE_CHAIN_MODE_NEXT_PTR:
329                 p_next = (struct ecore_chain_next *)(*p_next_elem);
330                 *p_next_elem = p_next->next_virt;
331                 if (is_chain_u16(p_chain))
332                         *(u16 *)idx_to_inc += (u16)p_chain->elem_unusable;
333                 else
334                         *(u32 *)idx_to_inc += (u16)p_chain->elem_unusable;
335                 break;
336         case ECORE_CHAIN_MODE_SINGLE:
337                 *p_next_elem = p_chain->p_virt_addr;
338                 break;
339         case ECORE_CHAIN_MODE_PBL:
340                 if (is_chain_u16(p_chain)) {
341                         if (++(*(u16 *)page_to_inc) == p_chain->page_cnt)
342                                 *(u16 *)page_to_inc = 0;
343                         page_index = *(u16 *)page_to_inc;
344                 } else {
345                         if (++(*(u32 *)page_to_inc) == p_chain->page_cnt)
346                                 *(u32 *)page_to_inc = 0;
347                         page_index = *(u32 *)page_to_inc;
348                 }
349                 *p_next_elem = p_chain->pbl.pp_virt_addr_tbl[page_index];
350         }
351 }
352
353 #define is_unusable_idx(p, idx)                 \
354         (((p)->u.chain16.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
355
356 #define is_unusable_idx_u32(p, idx)             \
357         (((p)->u.chain32.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
358
359 #define is_unusable_next_idx(p, idx)            \
360         ((((p)->u.chain16.idx + 1) & (p)->elem_per_page_mask) == (p)->usable_per_page)
361
362 #define is_unusable_next_idx_u32(p, idx)        \
363         ((((p)->u.chain32.idx + 1) & (p)->elem_per_page_mask) == (p)->usable_per_page)
364
365 #define test_and_skip(p, idx)                                                   \
366         do {                                                                    \
367                 if (is_chain_u16(p)) {                                          \
368                         if (is_unusable_idx(p, idx))                            \
369                                 (p)->u.chain16.idx += (p)->elem_unusable;       \
370                 } else {                                                        \
371                         if (is_unusable_idx_u32(p, idx))                        \
372                                 (p)->u.chain32.idx += (p)->elem_unusable;       \
373                 }                                                               \
374         } while (0)
375
376 /**
377  * @brief ecore_chain_return_multi_produced -
378  *
379  * A chain in which the driver "Produces" elements should use this API
380  * to indicate previous produced elements are now consumed.
381  *
382  * @param p_chain
383  * @param num
384  */
385 static OSAL_INLINE
386 void ecore_chain_return_multi_produced(struct ecore_chain *p_chain, u32 num)
387 {
388         if (is_chain_u16(p_chain))
389                 p_chain->u.chain16.cons_idx += (u16)num;
390         else
391                 p_chain->u.chain32.cons_idx += num;
392         test_and_skip(p_chain, cons_idx);
393 }
394
395 /**
396  * @brief ecore_chain_return_produced -
397  *
398  * A chain in which the driver "Produces" elements should use this API
399  * to indicate previous produced elements are now consumed.
400  *
401  * @param p_chain
402  */
403 static OSAL_INLINE void ecore_chain_return_produced(struct ecore_chain *p_chain)
404 {
405         if (is_chain_u16(p_chain))
406                 p_chain->u.chain16.cons_idx++;
407         else
408                 p_chain->u.chain32.cons_idx++;
409         test_and_skip(p_chain, cons_idx);
410 }
411
412 /**
413  * @brief ecore_chain_produce -
414  *
415  * A chain in which the driver "Produces" elements should use this to get
416  * a pointer to the next element which can be "Produced". It's driver
417  * responsibility to validate that the chain has room for new element.
418  *
419  * @param p_chain
420  *
421  * @return void*, a pointer to next element
422  */
423 static OSAL_INLINE void *ecore_chain_produce(struct ecore_chain *p_chain)
424 {
425         void *p_ret = OSAL_NULL, *p_prod_idx, *p_prod_page_idx;
426
427         if (is_chain_u16(p_chain)) {
428                 if ((p_chain->u.chain16.prod_idx &
429                      p_chain->elem_per_page_mask) ==
430                     p_chain->next_page_mask) {
431                         p_prod_idx = &p_chain->u.chain16.prod_idx;
432                         p_prod_page_idx = &p_chain->pbl.c.pbl_u16.prod_page_idx;
433                         ecore_chain_advance_page(p_chain, &p_chain->p_prod_elem,
434                                                  p_prod_idx, p_prod_page_idx);
435                 }
436                 p_chain->u.chain16.prod_idx++;
437         } else {
438                 if ((p_chain->u.chain32.prod_idx &
439                      p_chain->elem_per_page_mask) ==
440                     p_chain->next_page_mask) {
441                         p_prod_idx = &p_chain->u.chain32.prod_idx;
442                         p_prod_page_idx = &p_chain->pbl.c.pbl_u32.prod_page_idx;
443                         ecore_chain_advance_page(p_chain, &p_chain->p_prod_elem,
444                                                  p_prod_idx, p_prod_page_idx);
445                 }
446                 p_chain->u.chain32.prod_idx++;
447         }
448
449         p_ret = p_chain->p_prod_elem;
450         p_chain->p_prod_elem = (void*)(((u8*)p_chain->p_prod_elem) +
451                                        p_chain->elem_size);
452
453         return p_ret;
454 }
455
456 /**
457  * @brief ecore_chain_get_capacity -
458  *
459  * Get the maximum number of BDs in chain
460  *
461  * @param p_chain
462  * @param num
463  *
464  * @return number of unusable BDs
465  */
466 static OSAL_INLINE u32 ecore_chain_get_capacity(struct ecore_chain *p_chain)
467 {
468         return p_chain->capacity;
469 }
470
471 /**
472  * @brief ecore_chain_recycle_consumed -
473  *
474  * Returns an element which was previously consumed;
475  * Increments producers so they could be written to FW.
476  *
477  * @param p_chain
478  */
479 static OSAL_INLINE
480 void ecore_chain_recycle_consumed(struct ecore_chain *p_chain)
481 {
482         test_and_skip(p_chain, prod_idx);
483         if (is_chain_u16(p_chain))
484                 p_chain->u.chain16.prod_idx++;
485         else
486                 p_chain->u.chain32.prod_idx++;
487 }
488
489 /**
490  * @brief ecore_chain_consume -
491  *
492  * A Chain in which the driver utilizes data written by a different source
493  * (i.e., FW) should use this to access passed buffers.
494  *
495  * @param p_chain
496  *
497  * @return void*, a pointer to the next buffer written
498  */
499 static OSAL_INLINE void *ecore_chain_consume(struct ecore_chain *p_chain)
500 {
501         void *p_ret = OSAL_NULL, *p_cons_idx, *p_cons_page_idx;
502
503         if (is_chain_u16(p_chain)) {
504                 if ((p_chain->u.chain16.cons_idx &
505                      p_chain->elem_per_page_mask) ==
506                     p_chain->next_page_mask) {
507                         p_cons_idx = &p_chain->u.chain16.cons_idx;
508                         p_cons_page_idx = &p_chain->pbl.c.pbl_u16.cons_page_idx;
509                         ecore_chain_advance_page(p_chain, &p_chain->p_cons_elem,
510                                                  p_cons_idx, p_cons_page_idx);
511                 }
512                 p_chain->u.chain16.cons_idx++;
513         } else {
514                 if ((p_chain->u.chain32.cons_idx &
515                      p_chain->elem_per_page_mask) ==
516                     p_chain->next_page_mask) {
517                         p_cons_idx = &p_chain->u.chain32.cons_idx;
518                         p_cons_page_idx = &p_chain->pbl.c.pbl_u32.cons_page_idx;
519                         ecore_chain_advance_page(p_chain, &p_chain->p_cons_elem,
520                                                  p_cons_idx, p_cons_page_idx);
521                 }
522                 p_chain->u.chain32.cons_idx++;
523         }
524
525         p_ret = p_chain->p_cons_elem;
526         p_chain->p_cons_elem = (void*)(((u8*)p_chain->p_cons_elem) +
527                                        p_chain->elem_size);
528
529         return p_ret;
530 }
531
532 /**
533  * @brief ecore_chain_reset -
534  *
535  * Resets the chain to its start state
536  *
537  * @param p_chain pointer to a previously allocted chain
538  */
539 static OSAL_INLINE void ecore_chain_reset(struct ecore_chain *p_chain)
540 {
541         u32 i;
542
543         if (is_chain_u16(p_chain)) {
544                 p_chain->u.chain16.prod_idx = 0;
545                 p_chain->u.chain16.cons_idx = 0;
546         } else {
547                 p_chain->u.chain32.prod_idx = 0;
548                 p_chain->u.chain32.cons_idx = 0;
549         }
550         p_chain->p_cons_elem = p_chain->p_virt_addr;
551         p_chain->p_prod_elem = p_chain->p_virt_addr;
552
553         if (p_chain->mode == ECORE_CHAIN_MODE_PBL) {
554                 /* Use (page_cnt - 1) as a reset value for the prod/cons page's
555                  * indices, to avoid unnecessary page advancing on the first
556                  * call to ecore_chain_produce/consume. Instead, the indices
557                  * will be advanced to page_cnt and then will be wrapped to 0.
558                  */
559                 u32 reset_val = p_chain->page_cnt - 1;
560
561                 if (is_chain_u16(p_chain)) {
562                         p_chain->pbl.c.pbl_u16.prod_page_idx = (u16)reset_val;
563                         p_chain->pbl.c.pbl_u16.cons_page_idx = (u16)reset_val;
564                 } else {
565                         p_chain->pbl.c.pbl_u32.prod_page_idx = reset_val;
566                         p_chain->pbl.c.pbl_u32.cons_page_idx = reset_val;
567                 }
568         }
569
570         switch (p_chain->intended_use) {
571         case ECORE_CHAIN_USE_TO_CONSUME:
572                 /* produce empty elements */
573                 for (i = 0; i < p_chain->capacity; i++)
574                         ecore_chain_recycle_consumed(p_chain);
575                 break;
576
577         case ECORE_CHAIN_USE_TO_CONSUME_PRODUCE:
578         case ECORE_CHAIN_USE_TO_PRODUCE:
579         default:
580                 /* Do nothing */
581                 break;
582         }
583 }
584
585 /**
586  * @brief ecore_chain_init_params -
587  *
588  * Initalizes a basic chain struct
589  *
590  * @param p_chain
591  * @param page_cnt      number of pages in the allocated buffer
592  * @param elem_size     size of each element in the chain
593  * @param intended_use
594  * @param mode
595  * @param cnt_type
596  * @param dp_ctx
597  */
598 static OSAL_INLINE void
599 ecore_chain_init_params(struct ecore_chain *p_chain, u32 page_cnt, u8 elem_size,
600                         enum ecore_chain_use_mode intended_use,
601                         enum ecore_chain_mode mode,
602                         enum ecore_chain_cnt_type cnt_type, void *dp_ctx)
603 {
604         /* chain fixed parameters */
605         p_chain->p_virt_addr = OSAL_NULL;
606         p_chain->p_phys_addr = 0;
607         p_chain->elem_size = elem_size;
608         p_chain->intended_use = (u8)intended_use;
609         p_chain->mode = mode;
610         p_chain->cnt_type = (u8)cnt_type;
611
612         p_chain->elem_per_page = ELEMS_PER_PAGE(elem_size);
613         p_chain->usable_per_page = USABLE_ELEMS_PER_PAGE(elem_size, mode);
614         p_chain->elem_per_page_mask = p_chain->elem_per_page - 1;
615         p_chain->elem_unusable = UNUSABLE_ELEMS_PER_PAGE(elem_size, mode);
616         p_chain->next_page_mask = (p_chain->usable_per_page &
617                                    p_chain->elem_per_page_mask);
618
619         p_chain->page_cnt = page_cnt;
620         p_chain->capacity = p_chain->usable_per_page * page_cnt;
621         p_chain->size = p_chain->elem_per_page * page_cnt;
622         p_chain->b_external_pbl = false;
623         p_chain->pbl_sp.p_phys_table = 0;
624         p_chain->pbl_sp.p_virt_table = OSAL_NULL;
625         p_chain->pbl.pp_virt_addr_tbl = OSAL_NULL;
626
627         p_chain->dp_ctx = dp_ctx;
628 }
629
630 /**
631  * @brief ecore_chain_init_mem -
632  *
633  * Initalizes a basic chain struct with its chain buffers
634  *
635  * @param p_chain
636  * @param p_virt_addr   virtual address of allocated buffer's beginning
637  * @param p_phys_addr   physical address of allocated buffer's beginning
638  *
639  */
640 static OSAL_INLINE void ecore_chain_init_mem(struct ecore_chain *p_chain,
641                                              void *p_virt_addr,
642                                              dma_addr_t p_phys_addr)
643 {
644         p_chain->p_virt_addr = p_virt_addr;
645         p_chain->p_phys_addr = p_phys_addr;
646 }
647
648 /**
649  * @brief ecore_chain_init_pbl_mem -
650  *
651  * Initalizes a basic chain struct with its pbl buffers
652  *
653  * @param p_chain
654  * @param p_virt_pbl    pointer to a pre allocated side table which will hold
655  *                      virtual page addresses.
656  * @param p_phys_pbl    pointer to a pre-allocated side table which will hold
657  *                      physical page addresses.
658  * @param pp_virt_addr_tbl
659  *                      pointer to a pre-allocated side table which will hold
660  *                      the virtual addresses of the chain pages.
661  *
662  */
663 static OSAL_INLINE void ecore_chain_init_pbl_mem(struct ecore_chain *p_chain,
664                                                  void *p_virt_pbl,
665                                                  dma_addr_t p_phys_pbl,
666                                                  void **pp_virt_addr_tbl)
667 {
668         p_chain->pbl_sp.p_phys_table = p_phys_pbl;
669         p_chain->pbl_sp.p_virt_table = p_virt_pbl;
670         p_chain->pbl.pp_virt_addr_tbl = pp_virt_addr_tbl;
671 }
672
673 /**
674  * @brief ecore_chain_init_next_ptr_elem -
675  *
676  * Initalizes a next pointer element
677  *
678  * @param p_chain
679  * @param p_virt_curr   virtual address of a chain page of which the next
680  *                      pointer element is initialized
681  * @param p_virt_next   virtual address of the next chain page
682  * @param p_phys_next   physical address of the next chain page
683  *
684  */
685 static OSAL_INLINE void
686 ecore_chain_init_next_ptr_elem(struct ecore_chain *p_chain, void *p_virt_curr,
687                                void *p_virt_next, dma_addr_t p_phys_next)
688 {
689         struct ecore_chain_next *p_next;
690         u32 size;
691
692         size = p_chain->elem_size * p_chain->usable_per_page;
693         p_next = (struct ecore_chain_next *)((u8 *)p_virt_curr + size);
694
695         DMA_REGPAIR_LE(p_next->next_phys, p_phys_next);
696
697         p_next->next_virt = p_virt_next;
698 }
699
700 /**
701  * @brief ecore_chain_get_last_elem -
702  *
703  * Returns a pointer to the last element of the chain
704  *
705  * @param p_chain
706  *
707  * @return void*
708  */
709 static OSAL_INLINE void *ecore_chain_get_last_elem(struct ecore_chain *p_chain)
710 {
711         struct ecore_chain_next *p_next = OSAL_NULL;
712         void *p_virt_addr = OSAL_NULL;
713         u32 size, last_page_idx;
714
715         if (!p_chain->p_virt_addr)
716                 goto out;
717
718         switch (p_chain->mode) {
719         case ECORE_CHAIN_MODE_NEXT_PTR:
720                 size = p_chain->elem_size * p_chain->usable_per_page;
721                 p_virt_addr = p_chain->p_virt_addr;
722                 p_next = (struct ecore_chain_next *)((u8 *)p_virt_addr + size);
723                 while (p_next->next_virt != p_chain->p_virt_addr) {
724                         p_virt_addr = p_next->next_virt;
725                         p_next = (struct ecore_chain_next *)((u8 *)p_virt_addr +
726                                                              size);
727                 }
728                 break;
729         case ECORE_CHAIN_MODE_SINGLE:
730                 p_virt_addr = p_chain->p_virt_addr;
731                 break;
732         case ECORE_CHAIN_MODE_PBL:
733                 last_page_idx = p_chain->page_cnt - 1;
734                 p_virt_addr = p_chain->pbl.pp_virt_addr_tbl[last_page_idx];
735                 break;
736         }
737         /* p_virt_addr points at this stage to the last page of the chain */
738         size = p_chain->elem_size * (p_chain->usable_per_page - 1);
739         p_virt_addr = (u8 *)p_virt_addr + size;
740 out:
741         return p_virt_addr;
742 }
743
744 /**
745  * @brief ecore_chain_set_prod - sets the prod to the given value
746  *
747  * @param prod_idx
748  * @param p_prod_elem
749  */
750 static OSAL_INLINE void ecore_chain_set_prod(struct ecore_chain *p_chain,
751                                              u32 prod_idx, void *p_prod_elem)
752 {
753         if (is_chain_u16(p_chain))
754                 p_chain->u.chain16.prod_idx = (u16)prod_idx;
755         else
756                 p_chain->u.chain32.prod_idx = prod_idx;
757         p_chain->p_prod_elem = p_prod_elem;
758 }
759
760 /**
761  * @brief ecore_chain_pbl_zero_mem - set chain memory to 0
762  *
763  * @param p_chain
764  */
765 static OSAL_INLINE void ecore_chain_pbl_zero_mem(struct ecore_chain *p_chain)
766 {
767         u32 i, page_cnt;
768
769         if (p_chain->mode != ECORE_CHAIN_MODE_PBL)
770                 return;
771
772         page_cnt = ecore_chain_get_page_cnt(p_chain);
773
774         for (i = 0; i < page_cnt; i++)
775                 OSAL_MEM_ZERO(p_chain->pbl.pp_virt_addr_tbl[i],
776                               ECORE_CHAIN_PAGE_SIZE);
777 }
778
779 int ecore_chain_print(struct ecore_chain *p_chain, char *buffer,
780                       u32 buffer_size, u32 *element_indx, u32 stop_indx,
781                       bool print_metadata,
782                       int (*func_ptr_print_element)(struct ecore_chain *p_chain,
783                                                     void *p_element,
784                                                     char *buffer),
785                       int (*func_ptr_print_metadata)(struct ecore_chain *p_chain,
786                                                      char *buffer));
787
788 #endif /* __ECORE_CHAIN_H__ */