1 // SPDX-License-Identifier: GPL-2.0-only
3 * Huawei HiNIC PCI Express Linux driver
4 * Copyright(c) 2017 Huawei Technologies Co., Ltd
7 #include <linux/kernel.h>
8 #include <linux/types.h>
10 #include <linux/device.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/slab.h>
13 #include <linux/atomic.h>
14 #include <linux/semaphore.h>
15 #include <linux/errno.h>
16 #include <linux/vmalloc.h>
17 #include <linux/err.h>
18 #include <asm/byteorder.h>
20 #include "hinic_hw_if.h"
21 #include "hinic_hw_wqe.h"
22 #include "hinic_hw_wq.h"
23 #include "hinic_hw_cmdq.h"
25 #define WQS_BLOCKS_PER_PAGE 4
27 #define WQ_BLOCK_SIZE 4096
28 #define WQS_PAGE_SIZE (WQS_BLOCKS_PER_PAGE * WQ_BLOCK_SIZE)
30 #define WQS_MAX_NUM_BLOCKS 128
31 #define WQS_FREE_BLOCKS_SIZE(wqs) (WQS_MAX_NUM_BLOCKS * \
32 sizeof((wqs)->free_blocks[0]))
34 #define WQ_SIZE(wq) ((wq)->q_depth * (wq)->wqebb_size)
36 #define WQ_PAGE_ADDR_SIZE sizeof(u64)
37 #define WQ_MAX_PAGES (WQ_BLOCK_SIZE / WQ_PAGE_ADDR_SIZE)
39 #define CMDQ_BLOCK_SIZE 512
40 #define CMDQ_PAGE_SIZE 4096
42 #define CMDQ_WQ_MAX_PAGES (CMDQ_BLOCK_SIZE / WQ_PAGE_ADDR_SIZE)
44 #define WQ_BASE_VADDR(wqs, wq) \
45 ((void *)((wqs)->page_vaddr[(wq)->page_idx]) \
46 + (wq)->block_idx * WQ_BLOCK_SIZE)
48 #define WQ_BASE_PADDR(wqs, wq) \
49 ((wqs)->page_paddr[(wq)->page_idx] \
50 + (wq)->block_idx * WQ_BLOCK_SIZE)
52 #define WQ_BASE_ADDR(wqs, wq) \
53 ((void *)((wqs)->shadow_page_vaddr[(wq)->page_idx]) \
54 + (wq)->block_idx * WQ_BLOCK_SIZE)
56 #define CMDQ_BASE_VADDR(cmdq_pages, wq) \
57 ((void *)((cmdq_pages)->page_vaddr) \
58 + (wq)->block_idx * CMDQ_BLOCK_SIZE)
60 #define CMDQ_BASE_PADDR(cmdq_pages, wq) \
61 ((cmdq_pages)->page_paddr \
62 + (wq)->block_idx * CMDQ_BLOCK_SIZE)
64 #define CMDQ_BASE_ADDR(cmdq_pages, wq) \
65 ((void *)((cmdq_pages)->shadow_page_vaddr) \
66 + (wq)->block_idx * CMDQ_BLOCK_SIZE)
68 #define WQ_PAGE_ADDR(wq, idx) \
69 ((wq)->shadow_block_vaddr[WQE_PAGE_NUM(wq, idx)])
71 #define MASKED_WQE_IDX(wq, idx) ((idx) & (wq)->mask)
73 #define WQE_IN_RANGE(wqe, start, end) \
74 (((unsigned long)(wqe) >= (unsigned long)(start)) && \
75 ((unsigned long)(wqe) < (unsigned long)(end)))
77 #define WQE_SHADOW_PAGE(wq, wqe) \
78 (((unsigned long)(wqe) - (unsigned long)(wq)->shadow_wqe) \
81 static inline int WQE_PAGE_OFF(struct hinic_wq *wq, u16 idx)
83 return (((idx) & ((wq)->num_wqebbs_per_page - 1))
84 << (wq)->wqebb_size_shift);
87 static inline int WQE_PAGE_NUM(struct hinic_wq *wq, u16 idx)
89 return (((idx) >> ((wq)->wqebbs_per_page_shift))
90 & ((wq)->num_q_pages - 1));
94 * queue_alloc_page - allocate page for Queue
95 * @hwif: HW interface for allocating DMA
96 * @vaddr: virtual address will be returned in this address
97 * @paddr: physical address will be returned in this address
98 * @shadow_vaddr: VM area will be return here for holding WQ page addresses
99 * @page_sz: page size of each WQ page
101 * Return 0 - Success, negative - Failure
103 static int queue_alloc_page(struct hinic_hwif *hwif, u64 **vaddr, u64 *paddr,
104 void ***shadow_vaddr, size_t page_sz)
106 struct pci_dev *pdev = hwif->pdev;
109 *vaddr = dma_alloc_coherent(&pdev->dev, page_sz, &dma_addr,
112 dev_err(&pdev->dev, "Failed to allocate dma for wqs page\n");
116 *paddr = (u64)dma_addr;
118 /* use vzalloc for big mem */
119 *shadow_vaddr = vzalloc(page_sz);
121 goto err_shadow_vaddr;
126 dma_free_coherent(&pdev->dev, page_sz, *vaddr, dma_addr);
131 * wqs_allocate_page - allocate page for WQ set
132 * @wqs: Work Queue Set
133 * @page_idx: the page index of the page will be allocated
135 * Return 0 - Success, negative - Failure
137 static int wqs_allocate_page(struct hinic_wqs *wqs, int page_idx)
139 return queue_alloc_page(wqs->hwif, &wqs->page_vaddr[page_idx],
140 &wqs->page_paddr[page_idx],
141 &wqs->shadow_page_vaddr[page_idx],
146 * wqs_free_page - free page of WQ set
147 * @wqs: Work Queue Set
148 * @page_idx: the page index of the page will be freed
150 static void wqs_free_page(struct hinic_wqs *wqs, int page_idx)
152 struct hinic_hwif *hwif = wqs->hwif;
153 struct pci_dev *pdev = hwif->pdev;
155 dma_free_coherent(&pdev->dev, WQS_PAGE_SIZE,
156 wqs->page_vaddr[page_idx],
157 (dma_addr_t)wqs->page_paddr[page_idx]);
158 vfree(wqs->shadow_page_vaddr[page_idx]);
162 * cmdq_allocate_page - allocate page for cmdq
163 * @cmdq_pages: the pages of the cmdq queue struct to hold the page
165 * Return 0 - Success, negative - Failure
167 static int cmdq_allocate_page(struct hinic_cmdq_pages *cmdq_pages)
169 return queue_alloc_page(cmdq_pages->hwif, &cmdq_pages->page_vaddr,
170 &cmdq_pages->page_paddr,
171 &cmdq_pages->shadow_page_vaddr,
176 * cmdq_free_page - free page from cmdq
177 * @cmdq_pages: the pages of the cmdq queue struct that hold the page
179 static void cmdq_free_page(struct hinic_cmdq_pages *cmdq_pages)
181 struct hinic_hwif *hwif = cmdq_pages->hwif;
182 struct pci_dev *pdev = hwif->pdev;
184 dma_free_coherent(&pdev->dev, CMDQ_PAGE_SIZE,
185 cmdq_pages->page_vaddr,
186 (dma_addr_t)cmdq_pages->page_paddr);
187 vfree(cmdq_pages->shadow_page_vaddr);
190 static int alloc_page_arrays(struct hinic_wqs *wqs)
192 struct hinic_hwif *hwif = wqs->hwif;
193 struct pci_dev *pdev = hwif->pdev;
195 wqs->page_paddr = devm_kcalloc(&pdev->dev, wqs->num_pages,
196 sizeof(*wqs->page_paddr), GFP_KERNEL);
197 if (!wqs->page_paddr)
200 wqs->page_vaddr = devm_kcalloc(&pdev->dev, wqs->num_pages,
201 sizeof(*wqs->page_vaddr), GFP_KERNEL);
202 if (!wqs->page_vaddr)
205 wqs->shadow_page_vaddr = devm_kcalloc(&pdev->dev, wqs->num_pages,
206 sizeof(*wqs->shadow_page_vaddr),
208 if (!wqs->shadow_page_vaddr)
209 goto err_page_shadow_vaddr;
213 err_page_shadow_vaddr:
214 devm_kfree(&pdev->dev, wqs->page_vaddr);
217 devm_kfree(&pdev->dev, wqs->page_paddr);
221 static void free_page_arrays(struct hinic_wqs *wqs)
223 struct hinic_hwif *hwif = wqs->hwif;
224 struct pci_dev *pdev = hwif->pdev;
226 devm_kfree(&pdev->dev, wqs->shadow_page_vaddr);
227 devm_kfree(&pdev->dev, wqs->page_vaddr);
228 devm_kfree(&pdev->dev, wqs->page_paddr);
231 static int wqs_next_block(struct hinic_wqs *wqs, int *page_idx,
236 down(&wqs->alloc_blocks_lock);
238 wqs->num_free_blks--;
240 if (wqs->num_free_blks < 0) {
241 wqs->num_free_blks++;
242 up(&wqs->alloc_blocks_lock);
246 pos = wqs->alloc_blk_pos++;
247 pos &= WQS_MAX_NUM_BLOCKS - 1;
249 *page_idx = wqs->free_blocks[pos].page_idx;
250 *block_idx = wqs->free_blocks[pos].block_idx;
252 wqs->free_blocks[pos].page_idx = -1;
253 wqs->free_blocks[pos].block_idx = -1;
255 up(&wqs->alloc_blocks_lock);
259 static void wqs_return_block(struct hinic_wqs *wqs, int page_idx,
264 down(&wqs->alloc_blocks_lock);
266 pos = wqs->return_blk_pos++;
267 pos &= WQS_MAX_NUM_BLOCKS - 1;
269 wqs->free_blocks[pos].page_idx = page_idx;
270 wqs->free_blocks[pos].block_idx = block_idx;
272 wqs->num_free_blks++;
274 up(&wqs->alloc_blocks_lock);
277 static void init_wqs_blocks_arr(struct hinic_wqs *wqs)
279 int page_idx, blk_idx, pos = 0;
281 for (page_idx = 0; page_idx < wqs->num_pages; page_idx++) {
282 for (blk_idx = 0; blk_idx < WQS_BLOCKS_PER_PAGE; blk_idx++) {
283 wqs->free_blocks[pos].page_idx = page_idx;
284 wqs->free_blocks[pos].block_idx = blk_idx;
289 wqs->alloc_blk_pos = 0;
290 wqs->return_blk_pos = pos;
291 wqs->num_free_blks = pos;
293 sema_init(&wqs->alloc_blocks_lock, 1);
297 * hinic_wqs_alloc - allocate Work Queues set
298 * @wqs: Work Queue Set
299 * @max_wqs: maximum wqs to allocate
300 * @hwif: HW interface for use for the allocation
302 * Return 0 - Success, negative - Failure
304 int hinic_wqs_alloc(struct hinic_wqs *wqs, int max_wqs,
305 struct hinic_hwif *hwif)
307 struct pci_dev *pdev = hwif->pdev;
308 int err, i, page_idx;
310 max_wqs = ALIGN(max_wqs, WQS_BLOCKS_PER_PAGE);
311 if (max_wqs > WQS_MAX_NUM_BLOCKS) {
312 dev_err(&pdev->dev, "Invalid max_wqs = %d\n", max_wqs);
317 wqs->num_pages = max_wqs / WQS_BLOCKS_PER_PAGE;
319 if (alloc_page_arrays(wqs)) {
321 "Failed to allocate mem for page addresses\n");
325 for (page_idx = 0; page_idx < wqs->num_pages; page_idx++) {
326 err = wqs_allocate_page(wqs, page_idx);
328 dev_err(&pdev->dev, "Failed wq page allocation\n");
329 goto err_wq_allocate_page;
333 wqs->free_blocks = devm_kzalloc(&pdev->dev, WQS_FREE_BLOCKS_SIZE(wqs),
335 if (!wqs->free_blocks) {
337 goto err_alloc_blocks;
340 init_wqs_blocks_arr(wqs);
344 err_wq_allocate_page:
345 for (i = 0; i < page_idx; i++)
346 wqs_free_page(wqs, i);
348 free_page_arrays(wqs);
353 * hinic_wqs_free - free Work Queues set
354 * @wqs: Work Queue Set
356 void hinic_wqs_free(struct hinic_wqs *wqs)
358 struct hinic_hwif *hwif = wqs->hwif;
359 struct pci_dev *pdev = hwif->pdev;
362 devm_kfree(&pdev->dev, wqs->free_blocks);
364 for (page_idx = 0; page_idx < wqs->num_pages; page_idx++)
365 wqs_free_page(wqs, page_idx);
367 free_page_arrays(wqs);
371 * alloc_wqes_shadow - allocate WQE shadows for WQ
372 * @wq: WQ to allocate shadows for
374 * Return 0 - Success, negative - Failure
376 static int alloc_wqes_shadow(struct hinic_wq *wq)
378 struct hinic_hwif *hwif = wq->hwif;
379 struct pci_dev *pdev = hwif->pdev;
381 wq->shadow_wqe = devm_kcalloc(&pdev->dev, wq->num_q_pages,
382 wq->max_wqe_size, GFP_KERNEL);
386 wq->shadow_idx = devm_kcalloc(&pdev->dev, wq->num_q_pages,
387 sizeof(*wq->shadow_idx), GFP_KERNEL);
394 devm_kfree(&pdev->dev, wq->shadow_wqe);
399 * free_wqes_shadow - free WQE shadows of WQ
400 * @wq: WQ to free shadows from
402 static void free_wqes_shadow(struct hinic_wq *wq)
404 struct hinic_hwif *hwif = wq->hwif;
405 struct pci_dev *pdev = hwif->pdev;
407 devm_kfree(&pdev->dev, wq->shadow_idx);
408 devm_kfree(&pdev->dev, wq->shadow_wqe);
412 * free_wq_pages - free pages of WQ
413 * @hwif: HW interface for releasing dma addresses
414 * @wq: WQ to free pages from
415 * @num_q_pages: number pages to free
417 static void free_wq_pages(struct hinic_wq *wq, struct hinic_hwif *hwif,
420 struct pci_dev *pdev = hwif->pdev;
423 for (i = 0; i < num_q_pages; i++) {
424 void **vaddr = &wq->shadow_block_vaddr[i];
425 u64 *paddr = &wq->block_vaddr[i];
428 dma_addr = (dma_addr_t)be64_to_cpu(*paddr);
429 dma_free_coherent(&pdev->dev, wq->wq_page_size, *vaddr,
433 free_wqes_shadow(wq);
437 * alloc_wq_pages - alloc pages for WQ
438 * @hwif: HW interface for allocating dma addresses
439 * @wq: WQ to allocate pages for
440 * @max_pages: maximum pages allowed
442 * Return 0 - Success, negative - Failure
444 static int alloc_wq_pages(struct hinic_wq *wq, struct hinic_hwif *hwif,
447 struct pci_dev *pdev = hwif->pdev;
448 int i, err, num_q_pages;
450 num_q_pages = ALIGN(WQ_SIZE(wq), wq->wq_page_size) / wq->wq_page_size;
451 if (num_q_pages > max_pages) {
452 dev_err(&pdev->dev, "Number wq pages exceeds the limit\n");
456 if (num_q_pages & (num_q_pages - 1)) {
457 dev_err(&pdev->dev, "Number wq pages must be power of 2\n");
461 wq->num_q_pages = num_q_pages;
463 err = alloc_wqes_shadow(wq);
465 dev_err(&pdev->dev, "Failed to allocate wqe shadow\n");
469 for (i = 0; i < num_q_pages; i++) {
470 void **vaddr = &wq->shadow_block_vaddr[i];
471 u64 *paddr = &wq->block_vaddr[i];
474 *vaddr = dma_alloc_coherent(&pdev->dev, wq->wq_page_size,
475 &dma_addr, GFP_KERNEL);
477 dev_err(&pdev->dev, "Failed to allocate wq page\n");
478 goto err_alloc_wq_pages;
481 /* HW uses Big Endian Format */
482 *paddr = cpu_to_be64(dma_addr);
488 free_wq_pages(wq, hwif, i);
493 * hinic_wq_allocate - Allocate the WQ resources from the WQS
494 * @wqs: WQ set from which to allocate the WQ resources
495 * @wq: WQ to allocate resources for it from the WQ set
496 * @wqebb_size: Work Queue Block Byte Size
497 * @wq_page_size: the page size in the Work Queue
498 * @q_depth: number of wqebbs in WQ
499 * @max_wqe_size: maximum WQE size that will be used in the WQ
501 * Return 0 - Success, negative - Failure
503 int hinic_wq_allocate(struct hinic_wqs *wqs, struct hinic_wq *wq,
504 u16 wqebb_size, u32 wq_page_size, u16 q_depth,
507 struct hinic_hwif *hwif = wqs->hwif;
508 struct pci_dev *pdev = hwif->pdev;
509 u16 num_wqebbs_per_page;
510 u16 wqebb_size_shift;
513 if (!is_power_of_2(wqebb_size)) {
514 dev_err(&pdev->dev, "wqebb_size must be power of 2\n");
518 if (wq_page_size == 0) {
519 dev_err(&pdev->dev, "wq_page_size must be > 0\n");
523 if (q_depth & (q_depth - 1)) {
524 dev_err(&pdev->dev, "WQ q_depth must be power of 2\n");
528 wqebb_size_shift = ilog2(wqebb_size);
529 num_wqebbs_per_page = ALIGN(wq_page_size, wqebb_size)
532 if (!is_power_of_2(num_wqebbs_per_page)) {
533 dev_err(&pdev->dev, "num wqebbs per page must be power of 2\n");
539 err = wqs_next_block(wqs, &wq->page_idx, &wq->block_idx);
541 dev_err(&pdev->dev, "Failed to get free wqs next block\n");
545 wq->wqebb_size = wqebb_size;
546 wq->wq_page_size = wq_page_size;
547 wq->q_depth = q_depth;
548 wq->max_wqe_size = max_wqe_size;
549 wq->num_wqebbs_per_page = num_wqebbs_per_page;
550 wq->wqebbs_per_page_shift = ilog2(num_wqebbs_per_page);
551 wq->wqebb_size_shift = wqebb_size_shift;
552 wq->block_vaddr = WQ_BASE_VADDR(wqs, wq);
553 wq->shadow_block_vaddr = WQ_BASE_ADDR(wqs, wq);
554 wq->block_paddr = WQ_BASE_PADDR(wqs, wq);
556 err = alloc_wq_pages(wq, wqs->hwif, WQ_MAX_PAGES);
558 dev_err(&pdev->dev, "Failed to allocate wq pages\n");
559 goto err_alloc_wq_pages;
562 atomic_set(&wq->cons_idx, 0);
563 atomic_set(&wq->prod_idx, 0);
564 atomic_set(&wq->delta, q_depth);
565 wq->mask = q_depth - 1;
570 wqs_return_block(wqs, wq->page_idx, wq->block_idx);
575 * hinic_wq_free - Free the WQ resources to the WQS
576 * @wqs: WQ set to free the WQ resources to it
577 * @wq: WQ to free its resources to the WQ set resources
579 void hinic_wq_free(struct hinic_wqs *wqs, struct hinic_wq *wq)
581 free_wq_pages(wq, wqs->hwif, wq->num_q_pages);
583 wqs_return_block(wqs, wq->page_idx, wq->block_idx);
587 * hinic_wqs_cmdq_alloc - Allocate wqs for cmdqs
588 * @cmdq_pages: will hold the pages of the cmdq
590 * @hwif: HW interface
591 * @cmdq_blocks: number of cmdq blocks/wq to allocate
592 * @wqebb_size: Work Queue Block Byte Size
593 * @wq_page_size: the page size in the Work Queue
594 * @q_depth: number of wqebbs in WQ
595 * @max_wqe_size: maximum WQE size that will be used in the WQ
597 * Return 0 - Success, negative - Failure
599 int hinic_wqs_cmdq_alloc(struct hinic_cmdq_pages *cmdq_pages,
600 struct hinic_wq *wq, struct hinic_hwif *hwif,
601 int cmdq_blocks, u16 wqebb_size, u32 wq_page_size,
602 u16 q_depth, u16 max_wqe_size)
604 struct pci_dev *pdev = hwif->pdev;
605 u16 num_wqebbs_per_page_shift;
606 u16 num_wqebbs_per_page;
607 u16 wqebb_size_shift;
608 int i, j, err = -ENOMEM;
610 if (!is_power_of_2(wqebb_size)) {
611 dev_err(&pdev->dev, "wqebb_size must be power of 2\n");
615 if (wq_page_size == 0) {
616 dev_err(&pdev->dev, "wq_page_size must be > 0\n");
620 if (q_depth & (q_depth - 1)) {
621 dev_err(&pdev->dev, "WQ q_depth must be power of 2\n");
625 wqebb_size_shift = ilog2(wqebb_size);
626 num_wqebbs_per_page = ALIGN(wq_page_size, wqebb_size)
629 if (!is_power_of_2(num_wqebbs_per_page)) {
630 dev_err(&pdev->dev, "num wqebbs per page must be power of 2\n");
634 cmdq_pages->hwif = hwif;
636 err = cmdq_allocate_page(cmdq_pages);
638 dev_err(&pdev->dev, "Failed to allocate CMDQ page\n");
641 num_wqebbs_per_page_shift = ilog2(num_wqebbs_per_page);
643 for (i = 0; i < cmdq_blocks; i++) {
648 wq[i].wqebb_size = wqebb_size;
649 wq[i].wq_page_size = wq_page_size;
650 wq[i].q_depth = q_depth;
651 wq[i].max_wqe_size = max_wqe_size;
652 wq[i].num_wqebbs_per_page = num_wqebbs_per_page;
653 wq[i].wqebbs_per_page_shift = num_wqebbs_per_page_shift;
654 wq[i].wqebb_size_shift = wqebb_size_shift;
655 wq[i].block_vaddr = CMDQ_BASE_VADDR(cmdq_pages, &wq[i]);
656 wq[i].shadow_block_vaddr = CMDQ_BASE_ADDR(cmdq_pages, &wq[i]);
657 wq[i].block_paddr = CMDQ_BASE_PADDR(cmdq_pages, &wq[i]);
659 err = alloc_wq_pages(&wq[i], cmdq_pages->hwif,
662 dev_err(&pdev->dev, "Failed to alloc CMDQ blocks\n");
666 atomic_set(&wq[i].cons_idx, 0);
667 atomic_set(&wq[i].prod_idx, 0);
668 atomic_set(&wq[i].delta, q_depth);
669 wq[i].mask = q_depth - 1;
675 for (j = 0; j < i; j++)
676 free_wq_pages(&wq[j], cmdq_pages->hwif, wq[j].num_q_pages);
678 cmdq_free_page(cmdq_pages);
683 * hinic_wqs_cmdq_free - Free wqs from cmdqs
684 * @cmdq_pages: hold the pages of the cmdq
686 * @cmdq_blocks: number of wqs to free
688 void hinic_wqs_cmdq_free(struct hinic_cmdq_pages *cmdq_pages,
689 struct hinic_wq *wq, int cmdq_blocks)
693 for (i = 0; i < cmdq_blocks; i++)
694 free_wq_pages(&wq[i], cmdq_pages->hwif, wq[i].num_q_pages);
696 cmdq_free_page(cmdq_pages);
699 static void copy_wqe_to_shadow(struct hinic_wq *wq, void *shadow_addr,
700 int num_wqebbs, u16 idx)
705 for (i = 0; i < num_wqebbs; i++, idx++) {
706 idx = MASKED_WQE_IDX(wq, idx);
707 wqebb_addr = WQ_PAGE_ADDR(wq, idx) +
708 WQE_PAGE_OFF(wq, idx);
710 memcpy(shadow_addr, wqebb_addr, wq->wqebb_size);
712 shadow_addr += wq->wqebb_size;
716 static void copy_wqe_from_shadow(struct hinic_wq *wq, void *shadow_addr,
717 int num_wqebbs, u16 idx)
722 for (i = 0; i < num_wqebbs; i++, idx++) {
723 idx = MASKED_WQE_IDX(wq, idx);
724 wqebb_addr = WQ_PAGE_ADDR(wq, idx) +
725 WQE_PAGE_OFF(wq, idx);
727 memcpy(wqebb_addr, shadow_addr, wq->wqebb_size);
728 shadow_addr += wq->wqebb_size;
733 * hinic_get_wqe - get wqe ptr in the current pi and update the pi
734 * @wq: wq to get wqe from
735 * @wqe_size: wqe size
736 * @prod_idx: returned pi
740 struct hinic_hw_wqe *hinic_get_wqe(struct hinic_wq *wq, unsigned int wqe_size,
743 int curr_pg, end_pg, num_wqebbs;
744 u16 curr_prod_idx, end_prod_idx;
746 *prod_idx = MASKED_WQE_IDX(wq, atomic_read(&wq->prod_idx));
748 num_wqebbs = ALIGN(wqe_size, wq->wqebb_size) >> wq->wqebb_size_shift;
750 if (atomic_sub_return(num_wqebbs, &wq->delta) <= 0) {
751 atomic_add(num_wqebbs, &wq->delta);
752 return ERR_PTR(-EBUSY);
755 end_prod_idx = atomic_add_return(num_wqebbs, &wq->prod_idx);
757 end_prod_idx = MASKED_WQE_IDX(wq, end_prod_idx);
758 curr_prod_idx = end_prod_idx - num_wqebbs;
759 curr_prod_idx = MASKED_WQE_IDX(wq, curr_prod_idx);
761 /* end prod index points to the next wqebb, therefore minus 1 */
762 end_prod_idx = MASKED_WQE_IDX(wq, end_prod_idx - 1);
764 curr_pg = WQE_PAGE_NUM(wq, curr_prod_idx);
765 end_pg = WQE_PAGE_NUM(wq, end_prod_idx);
767 *prod_idx = curr_prod_idx;
769 /* If we only have one page, still need to get shadown wqe when
770 * wqe rolling-over page
772 if (curr_pg != end_pg || end_prod_idx < *prod_idx) {
773 void *shadow_addr = &wq->shadow_wqe[curr_pg * wq->max_wqe_size];
775 copy_wqe_to_shadow(wq, shadow_addr, num_wqebbs, *prod_idx);
777 wq->shadow_idx[curr_pg] = *prod_idx;
781 return WQ_PAGE_ADDR(wq, *prod_idx) + WQE_PAGE_OFF(wq, *prod_idx);
785 * hinic_return_wqe - return the wqe when transmit failed
786 * @wq: wq to return wqe
787 * @wqe_size: wqe size
789 void hinic_return_wqe(struct hinic_wq *wq, unsigned int wqe_size)
791 int num_wqebbs = ALIGN(wqe_size, wq->wqebb_size) / wq->wqebb_size;
793 atomic_sub(num_wqebbs, &wq->prod_idx);
795 atomic_add(num_wqebbs, &wq->delta);
799 * hinic_put_wqe - return the wqe place to use for a new wqe
800 * @wq: wq to return wqe
801 * @wqe_size: wqe size
803 void hinic_put_wqe(struct hinic_wq *wq, unsigned int wqe_size)
805 int num_wqebbs = ALIGN(wqe_size, wq->wqebb_size)
806 >> wq->wqebb_size_shift;
808 atomic_add(num_wqebbs, &wq->cons_idx);
810 atomic_add(num_wqebbs, &wq->delta);
814 * hinic_read_wqe - read wqe ptr in the current ci
815 * @wq: wq to get read from
816 * @wqe_size: wqe size
817 * @cons_idx: returned ci
821 struct hinic_hw_wqe *hinic_read_wqe(struct hinic_wq *wq, unsigned int wqe_size,
824 int num_wqebbs = ALIGN(wqe_size, wq->wqebb_size)
825 >> wq->wqebb_size_shift;
826 u16 curr_cons_idx, end_cons_idx;
829 if ((atomic_read(&wq->delta) + num_wqebbs) > wq->q_depth)
830 return ERR_PTR(-EBUSY);
832 curr_cons_idx = atomic_read(&wq->cons_idx);
834 curr_cons_idx = MASKED_WQE_IDX(wq, curr_cons_idx);
835 end_cons_idx = MASKED_WQE_IDX(wq, curr_cons_idx + num_wqebbs - 1);
837 curr_pg = WQE_PAGE_NUM(wq, curr_cons_idx);
838 end_pg = WQE_PAGE_NUM(wq, end_cons_idx);
840 *cons_idx = curr_cons_idx;
842 /* If we only have one page, still need to get shadown wqe when
843 * wqe rolling-over page
845 if (curr_pg != end_pg || end_cons_idx < curr_cons_idx) {
846 void *shadow_addr = &wq->shadow_wqe[curr_pg * wq->max_wqe_size];
848 copy_wqe_to_shadow(wq, shadow_addr, num_wqebbs, *cons_idx);
852 return WQ_PAGE_ADDR(wq, *cons_idx) + WQE_PAGE_OFF(wq, *cons_idx);
856 * hinic_read_wqe_direct - read wqe directly from ci position
858 * @cons_idx: ci position
862 struct hinic_hw_wqe *hinic_read_wqe_direct(struct hinic_wq *wq, u16 cons_idx)
864 return WQ_PAGE_ADDR(wq, cons_idx) + WQE_PAGE_OFF(wq, cons_idx);
868 * wqe_shadow - check if a wqe is shadow
870 * @wqe: the wqe for shadow checking
872 * Return true - shadow, false - Not shadow
874 static inline bool wqe_shadow(struct hinic_wq *wq, struct hinic_hw_wqe *wqe)
876 size_t wqe_shadow_size = wq->num_q_pages * wq->max_wqe_size;
878 return WQE_IN_RANGE(wqe, wq->shadow_wqe,
879 &wq->shadow_wqe[wqe_shadow_size]);
883 * hinic_write_wqe - write the wqe to the wq
884 * @wq: wq to write wqe to
886 * @wqe_size: wqe size
888 void hinic_write_wqe(struct hinic_wq *wq, struct hinic_hw_wqe *wqe,
889 unsigned int wqe_size)
891 int curr_pg, num_wqebbs;
895 if (wqe_shadow(wq, wqe)) {
896 curr_pg = WQE_SHADOW_PAGE(wq, wqe);
898 prod_idx = wq->shadow_idx[curr_pg];
899 num_wqebbs = ALIGN(wqe_size, wq->wqebb_size) / wq->wqebb_size;
900 shadow_addr = &wq->shadow_wqe[curr_pg * wq->max_wqe_size];
902 copy_wqe_from_shadow(wq, shadow_addr, num_wqebbs, prod_idx);