1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) */
2 /* QLogic qed NIC Driver
3 * Copyright (c) 2015-2017 QLogic Corporation
4 * Copyright (c) 2019-2020 Marvell International Ltd.
10 #include <linux/types.h>
11 #include <asm/byteorder.h>
12 #include <linux/kernel.h>
13 #include <linux/list.h>
14 #include <linux/sizes.h>
15 #include <linux/slab.h>
16 #include <linux/qed/common_hsi.h>
19 /* Each Page contains a next pointer at its end */
20 QED_CHAIN_MODE_NEXT_PTR,
22 /* Chain is a single page (next ptr) is unrequired */
23 QED_CHAIN_MODE_SINGLE,
25 /* Page pointers are located in a side list */
29 enum qed_chain_use_mode {
30 QED_CHAIN_USE_TO_PRODUCE, /* Chain starts empty */
31 QED_CHAIN_USE_TO_CONSUME, /* Chain starts full */
32 QED_CHAIN_USE_TO_CONSUME_PRODUCE, /* Chain starts empty */
35 enum qed_chain_cnt_type {
36 /* The chain's size/prod/cons are kept in 16-bit variables */
37 QED_CHAIN_CNT_TYPE_U16,
39 /* The chain's size/prod/cons are kept in 32-bit variables */
40 QED_CHAIN_CNT_TYPE_U32,
43 struct qed_chain_next {
44 struct regpair next_phys;
48 struct qed_chain_pbl_u16 {
53 struct qed_chain_pbl_u32 {
58 struct qed_chain_u16 {
59 /* Cyclic index of next element to produce/consme */
64 struct qed_chain_u32 {
65 /* Cyclic index of next element to produce/consme */
70 struct addr_tbl_entry {
76 /* Fastpath portion of the chain - required for commands such
77 * as produce / consume.
80 /* Point to next element to produce/consume */
84 /* Fastpath portions of the PBL [if exists] */
87 /* Table for keeping the virtual and physical addresses of the
88 * chain pages, respectively to the physical addresses
91 struct addr_tbl_entry *pp_addr_tbl;
94 struct qed_chain_pbl_u16 u16;
95 struct qed_chain_pbl_u32 u32;
100 struct qed_chain_u16 chain16;
101 struct qed_chain_u32 chain32;
104 /* Capacity counts only usable elements */
108 enum qed_chain_mode mode;
110 /* Elements information for fast calculations */
112 u16 elem_per_page_mask;
118 enum qed_chain_cnt_type cnt_type;
120 /* Slowpath of the chain - required for initialization and destruction,
121 * but isn't involved in regular functionality.
126 /* Base address of a pre-allocated buffer for pbl */
129 dma_addr_t table_phys;
133 /* Address of first page of the chain - the address is required
134 * for fastpath operation [consume/produce] but only for the SINGLE
135 * flavour which isn't considered fastpath [== SPQ].
138 dma_addr_t p_phys_addr;
140 /* Total number of elements [for entire chain] */
143 enum qed_chain_use_mode intended_use;
148 struct qed_chain_init_params {
149 enum qed_chain_mode mode;
150 enum qed_chain_use_mode intended_use;
151 enum qed_chain_cnt_type cnt_type;
158 dma_addr_t ext_pbl_phys;
161 #define QED_CHAIN_PAGE_SIZE SZ_4K
163 #define ELEMS_PER_PAGE(elem_size, page_size) \
164 ((page_size) / (elem_size))
166 #define UNUSABLE_ELEMS_PER_PAGE(elem_size, mode) \
167 (((mode) == QED_CHAIN_MODE_NEXT_PTR) ? \
168 (u8)(1 + ((sizeof(struct qed_chain_next) - 1) / (elem_size))) : \
171 #define USABLE_ELEMS_PER_PAGE(elem_size, page_size, mode) \
172 ((u32)(ELEMS_PER_PAGE((elem_size), (page_size)) - \
173 UNUSABLE_ELEMS_PER_PAGE((elem_size), (mode))))
175 #define QED_CHAIN_PAGE_CNT(elem_cnt, elem_size, page_size, mode) \
176 DIV_ROUND_UP((elem_cnt), \
177 USABLE_ELEMS_PER_PAGE((elem_size), (page_size), (mode)))
179 #define is_chain_u16(p) \
180 ((p)->cnt_type == QED_CHAIN_CNT_TYPE_U16)
181 #define is_chain_u32(p) \
182 ((p)->cnt_type == QED_CHAIN_CNT_TYPE_U32)
186 static inline u16 qed_chain_get_prod_idx(const struct qed_chain *chain)
188 return chain->u.chain16.prod_idx;
191 static inline u16 qed_chain_get_cons_idx(const struct qed_chain *chain)
193 return chain->u.chain16.cons_idx;
196 static inline u32 qed_chain_get_prod_idx_u32(const struct qed_chain *chain)
198 return chain->u.chain32.prod_idx;
201 static inline u32 qed_chain_get_cons_idx_u32(const struct qed_chain *chain)
203 return chain->u.chain32.cons_idx;
206 static inline u16 qed_chain_get_elem_used(const struct qed_chain *chain)
208 u32 prod = qed_chain_get_prod_idx(chain);
209 u32 cons = qed_chain_get_cons_idx(chain);
210 u16 elem_per_page = chain->elem_per_page;
214 prod += (u32)U16_MAX + 1;
216 used = (u16)(prod - cons);
217 if (chain->mode == QED_CHAIN_MODE_NEXT_PTR)
218 used -= (u16)(prod / elem_per_page - cons / elem_per_page);
223 static inline u16 qed_chain_get_elem_left(const struct qed_chain *chain)
225 return (u16)(chain->capacity - qed_chain_get_elem_used(chain));
228 static inline u32 qed_chain_get_elem_used_u32(const struct qed_chain *chain)
230 u64 prod = qed_chain_get_prod_idx_u32(chain);
231 u64 cons = qed_chain_get_cons_idx_u32(chain);
232 u16 elem_per_page = chain->elem_per_page;
236 prod += (u64)U32_MAX + 1;
238 used = (u32)(prod - cons);
239 if (chain->mode == QED_CHAIN_MODE_NEXT_PTR)
240 used -= (u32)(prod / elem_per_page - cons / elem_per_page);
245 static inline u32 qed_chain_get_elem_left_u32(const struct qed_chain *chain)
247 return chain->capacity - qed_chain_get_elem_used_u32(chain);
250 static inline u16 qed_chain_get_usable_per_page(const struct qed_chain *chain)
252 return chain->usable_per_page;
255 static inline u8 qed_chain_get_unusable_per_page(const struct qed_chain *chain)
257 return chain->elem_unusable;
260 static inline u32 qed_chain_get_page_cnt(const struct qed_chain *chain)
262 return chain->page_cnt;
265 static inline dma_addr_t qed_chain_get_pbl_phys(const struct qed_chain *chain)
267 return chain->pbl_sp.table_phys;
271 * @brief qed_chain_advance_page -
273 * Advance the next element accros pages for a linked chain
281 qed_chain_advance_page(struct qed_chain *p_chain,
282 void **p_next_elem, void *idx_to_inc, void *page_to_inc)
284 struct qed_chain_next *p_next = NULL;
287 switch (p_chain->mode) {
288 case QED_CHAIN_MODE_NEXT_PTR:
289 p_next = *p_next_elem;
290 *p_next_elem = p_next->next_virt;
291 if (is_chain_u16(p_chain))
292 *(u16 *)idx_to_inc += p_chain->elem_unusable;
294 *(u32 *)idx_to_inc += p_chain->elem_unusable;
296 case QED_CHAIN_MODE_SINGLE:
297 *p_next_elem = p_chain->p_virt_addr;
300 case QED_CHAIN_MODE_PBL:
301 if (is_chain_u16(p_chain)) {
302 if (++(*(u16 *)page_to_inc) == p_chain->page_cnt)
303 *(u16 *)page_to_inc = 0;
304 page_index = *(u16 *)page_to_inc;
306 if (++(*(u32 *)page_to_inc) == p_chain->page_cnt)
307 *(u32 *)page_to_inc = 0;
308 page_index = *(u32 *)page_to_inc;
310 *p_next_elem = p_chain->pbl.pp_addr_tbl[page_index].virt_addr;
314 #define is_unusable_idx(p, idx) \
315 (((p)->u.chain16.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
317 #define is_unusable_idx_u32(p, idx) \
318 (((p)->u.chain32.idx & (p)->elem_per_page_mask) == (p)->usable_per_page)
319 #define is_unusable_next_idx(p, idx) \
320 ((((p)->u.chain16.idx + 1) & (p)->elem_per_page_mask) == \
321 (p)->usable_per_page)
323 #define is_unusable_next_idx_u32(p, idx) \
324 ((((p)->u.chain32.idx + 1) & (p)->elem_per_page_mask) == \
325 (p)->usable_per_page)
327 #define test_and_skip(p, idx) \
329 if (is_chain_u16(p)) { \
330 if (is_unusable_idx(p, idx)) \
331 (p)->u.chain16.idx += (p)->elem_unusable; \
333 if (is_unusable_idx_u32(p, idx)) \
334 (p)->u.chain32.idx += (p)->elem_unusable; \
339 * @brief qed_chain_return_produced -
341 * A chain in which the driver "Produces" elements should use this API
342 * to indicate previous produced elements are now consumed.
346 static inline void qed_chain_return_produced(struct qed_chain *p_chain)
348 if (is_chain_u16(p_chain))
349 p_chain->u.chain16.cons_idx++;
351 p_chain->u.chain32.cons_idx++;
352 test_and_skip(p_chain, cons_idx);
356 * @brief qed_chain_produce -
358 * A chain in which the driver "Produces" elements should use this to get
359 * a pointer to the next element which can be "Produced". It's driver
360 * responsibility to validate that the chain has room for new element.
364 * @return void*, a pointer to next element
366 static inline void *qed_chain_produce(struct qed_chain *p_chain)
368 void *p_ret = NULL, *p_prod_idx, *p_prod_page_idx;
370 if (is_chain_u16(p_chain)) {
371 if ((p_chain->u.chain16.prod_idx &
372 p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
373 p_prod_idx = &p_chain->u.chain16.prod_idx;
374 p_prod_page_idx = &p_chain->pbl.c.u16.prod_page_idx;
375 qed_chain_advance_page(p_chain, &p_chain->p_prod_elem,
376 p_prod_idx, p_prod_page_idx);
378 p_chain->u.chain16.prod_idx++;
380 if ((p_chain->u.chain32.prod_idx &
381 p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
382 p_prod_idx = &p_chain->u.chain32.prod_idx;
383 p_prod_page_idx = &p_chain->pbl.c.u32.prod_page_idx;
384 qed_chain_advance_page(p_chain, &p_chain->p_prod_elem,
385 p_prod_idx, p_prod_page_idx);
387 p_chain->u.chain32.prod_idx++;
390 p_ret = p_chain->p_prod_elem;
391 p_chain->p_prod_elem = (void *)(((u8 *)p_chain->p_prod_elem) +
398 * @brief qed_chain_get_capacity -
400 * Get the maximum number of BDs in chain
405 * @return number of unusable BDs
407 static inline u32 qed_chain_get_capacity(struct qed_chain *p_chain)
409 return p_chain->capacity;
413 * @brief qed_chain_recycle_consumed -
415 * Returns an element which was previously consumed;
416 * Increments producers so they could be written to FW.
420 static inline void qed_chain_recycle_consumed(struct qed_chain *p_chain)
422 test_and_skip(p_chain, prod_idx);
423 if (is_chain_u16(p_chain))
424 p_chain->u.chain16.prod_idx++;
426 p_chain->u.chain32.prod_idx++;
430 * @brief qed_chain_consume -
432 * A Chain in which the driver utilizes data written by a different source
433 * (i.e., FW) should use this to access passed buffers.
437 * @return void*, a pointer to the next buffer written
439 static inline void *qed_chain_consume(struct qed_chain *p_chain)
441 void *p_ret = NULL, *p_cons_idx, *p_cons_page_idx;
443 if (is_chain_u16(p_chain)) {
444 if ((p_chain->u.chain16.cons_idx &
445 p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
446 p_cons_idx = &p_chain->u.chain16.cons_idx;
447 p_cons_page_idx = &p_chain->pbl.c.u16.cons_page_idx;
448 qed_chain_advance_page(p_chain, &p_chain->p_cons_elem,
449 p_cons_idx, p_cons_page_idx);
451 p_chain->u.chain16.cons_idx++;
453 if ((p_chain->u.chain32.cons_idx &
454 p_chain->elem_per_page_mask) == p_chain->next_page_mask) {
455 p_cons_idx = &p_chain->u.chain32.cons_idx;
456 p_cons_page_idx = &p_chain->pbl.c.u32.cons_page_idx;
457 qed_chain_advance_page(p_chain, &p_chain->p_cons_elem,
458 p_cons_idx, p_cons_page_idx);
460 p_chain->u.chain32.cons_idx++;
463 p_ret = p_chain->p_cons_elem;
464 p_chain->p_cons_elem = (void *)(((u8 *)p_chain->p_cons_elem) +
471 * @brief qed_chain_reset - Resets the chain to its start state
473 * @param p_chain pointer to a previously allocted chain
475 static inline void qed_chain_reset(struct qed_chain *p_chain)
479 if (is_chain_u16(p_chain)) {
480 p_chain->u.chain16.prod_idx = 0;
481 p_chain->u.chain16.cons_idx = 0;
483 p_chain->u.chain32.prod_idx = 0;
484 p_chain->u.chain32.cons_idx = 0;
486 p_chain->p_cons_elem = p_chain->p_virt_addr;
487 p_chain->p_prod_elem = p_chain->p_virt_addr;
489 if (p_chain->mode == QED_CHAIN_MODE_PBL) {
490 /* Use (page_cnt - 1) as a reset value for the prod/cons page's
491 * indices, to avoid unnecessary page advancing on the first
492 * call to qed_chain_produce/consume. Instead, the indices
493 * will be advanced to page_cnt and then will be wrapped to 0.
495 u32 reset_val = p_chain->page_cnt - 1;
497 if (is_chain_u16(p_chain)) {
498 p_chain->pbl.c.u16.prod_page_idx = (u16)reset_val;
499 p_chain->pbl.c.u16.cons_page_idx = (u16)reset_val;
501 p_chain->pbl.c.u32.prod_page_idx = reset_val;
502 p_chain->pbl.c.u32.cons_page_idx = reset_val;
506 switch (p_chain->intended_use) {
507 case QED_CHAIN_USE_TO_CONSUME:
508 /* produce empty elements */
509 for (i = 0; i < p_chain->capacity; i++)
510 qed_chain_recycle_consumed(p_chain);
513 case QED_CHAIN_USE_TO_CONSUME_PRODUCE:
514 case QED_CHAIN_USE_TO_PRODUCE:
522 * @brief qed_chain_get_last_elem -
524 * Returns a pointer to the last element of the chain
530 static inline void *qed_chain_get_last_elem(struct qed_chain *p_chain)
532 struct qed_chain_next *p_next = NULL;
533 void *p_virt_addr = NULL;
534 u32 size, last_page_idx;
536 if (!p_chain->p_virt_addr)
539 switch (p_chain->mode) {
540 case QED_CHAIN_MODE_NEXT_PTR:
541 size = p_chain->elem_size * p_chain->usable_per_page;
542 p_virt_addr = p_chain->p_virt_addr;
543 p_next = (struct qed_chain_next *)((u8 *)p_virt_addr + size);
544 while (p_next->next_virt != p_chain->p_virt_addr) {
545 p_virt_addr = p_next->next_virt;
546 p_next = (struct qed_chain_next *)((u8 *)p_virt_addr +
550 case QED_CHAIN_MODE_SINGLE:
551 p_virt_addr = p_chain->p_virt_addr;
553 case QED_CHAIN_MODE_PBL:
554 last_page_idx = p_chain->page_cnt - 1;
555 p_virt_addr = p_chain->pbl.pp_addr_tbl[last_page_idx].virt_addr;
558 /* p_virt_addr points at this stage to the last page of the chain */
559 size = p_chain->elem_size * (p_chain->usable_per_page - 1);
560 p_virt_addr = (u8 *)p_virt_addr + size;
566 * @brief qed_chain_set_prod - sets the prod to the given value
571 static inline void qed_chain_set_prod(struct qed_chain *p_chain,
572 u32 prod_idx, void *p_prod_elem)
574 if (p_chain->mode == QED_CHAIN_MODE_PBL) {
575 u32 cur_prod, page_mask, page_cnt, page_diff;
577 cur_prod = is_chain_u16(p_chain) ? p_chain->u.chain16.prod_idx :
578 p_chain->u.chain32.prod_idx;
580 /* Assume that number of elements in a page is power of 2 */
581 page_mask = ~p_chain->elem_per_page_mask;
583 /* Use "cur_prod - 1" and "prod_idx - 1" since producer index
584 * reaches the first element of next page before the page index
585 * is incremented. See qed_chain_produce().
586 * Index wrap around is not a problem because the difference
587 * between current and given producer indices is always
588 * positive and lower than the chain's capacity.
590 page_diff = (((cur_prod - 1) & page_mask) -
591 ((prod_idx - 1) & page_mask)) /
592 p_chain->elem_per_page;
594 page_cnt = qed_chain_get_page_cnt(p_chain);
595 if (is_chain_u16(p_chain))
596 p_chain->pbl.c.u16.prod_page_idx =
597 (p_chain->pbl.c.u16.prod_page_idx -
598 page_diff + page_cnt) % page_cnt;
600 p_chain->pbl.c.u32.prod_page_idx =
601 (p_chain->pbl.c.u32.prod_page_idx -
602 page_diff + page_cnt) % page_cnt;
605 if (is_chain_u16(p_chain))
606 p_chain->u.chain16.prod_idx = (u16) prod_idx;
608 p_chain->u.chain32.prod_idx = prod_idx;
609 p_chain->p_prod_elem = p_prod_elem;
613 * @brief qed_chain_pbl_zero_mem - set chain memory to 0
617 static inline void qed_chain_pbl_zero_mem(struct qed_chain *p_chain)
621 if (p_chain->mode != QED_CHAIN_MODE_PBL)
624 page_cnt = qed_chain_get_page_cnt(p_chain);
626 for (i = 0; i < page_cnt; i++)
627 memset(p_chain->pbl.pp_addr_tbl[i].virt_addr, 0,