• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Huawei HiNIC PCI Express Linux driver
4  * Copyright(c) 2017 Huawei Technologies Co., Ltd
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/pci.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>
19 
20 #include "hinic_hw_if.h"
21 #include "hinic_hw_wqe.h"
22 #include "hinic_hw_wq.h"
23 #include "hinic_hw_cmdq.h"
24 
25 #define WQS_BLOCKS_PER_PAGE             4
26 
27 #define WQ_BLOCK_SIZE                   4096
28 #define WQS_PAGE_SIZE                   (WQS_BLOCKS_PER_PAGE * WQ_BLOCK_SIZE)
29 
30 #define WQS_MAX_NUM_BLOCKS              128
31 #define WQS_FREE_BLOCKS_SIZE(wqs)       (WQS_MAX_NUM_BLOCKS * \
32 					 sizeof((wqs)->free_blocks[0]))
33 
34 #define WQ_SIZE(wq)                     ((wq)->q_depth * (wq)->wqebb_size)
35 
36 #define WQ_PAGE_ADDR_SIZE               sizeof(u64)
37 #define WQ_MAX_PAGES                    (WQ_BLOCK_SIZE / WQ_PAGE_ADDR_SIZE)
38 
39 #define CMDQ_BLOCK_SIZE                 512
40 #define CMDQ_PAGE_SIZE                  4096
41 
42 #define CMDQ_WQ_MAX_PAGES               (CMDQ_BLOCK_SIZE / WQ_PAGE_ADDR_SIZE)
43 
44 #define WQ_BASE_VADDR(wqs, wq)          \
45 			((void *)((wqs)->page_vaddr[(wq)->page_idx]) \
46 				+ (wq)->block_idx * WQ_BLOCK_SIZE)
47 
48 #define WQ_BASE_PADDR(wqs, wq)          \
49 			((wqs)->page_paddr[(wq)->page_idx] \
50 				+ (wq)->block_idx * WQ_BLOCK_SIZE)
51 
52 #define WQ_BASE_ADDR(wqs, wq)           \
53 			((void *)((wqs)->shadow_page_vaddr[(wq)->page_idx]) \
54 				+ (wq)->block_idx * WQ_BLOCK_SIZE)
55 
56 #define CMDQ_BASE_VADDR(cmdq_pages, wq) \
57 			((void *)((cmdq_pages)->page_vaddr) \
58 				+ (wq)->block_idx * CMDQ_BLOCK_SIZE)
59 
60 #define CMDQ_BASE_PADDR(cmdq_pages, wq) \
61 			((cmdq_pages)->page_paddr \
62 				+ (wq)->block_idx * CMDQ_BLOCK_SIZE)
63 
64 #define CMDQ_BASE_ADDR(cmdq_pages, wq)  \
65 			((void *)((cmdq_pages)->shadow_page_vaddr) \
66 				+ (wq)->block_idx * CMDQ_BLOCK_SIZE)
67 
68 #define WQ_PAGE_ADDR(wq, idx)           \
69 			((wq)->shadow_block_vaddr[WQE_PAGE_NUM(wq, idx)])
70 
71 #define MASKED_WQE_IDX(wq, idx)         ((idx) & (wq)->mask)
72 
73 #define WQE_IN_RANGE(wqe, start, end)   \
74 		(((unsigned long)(wqe) >= (unsigned long)(start)) && \
75 		 ((unsigned long)(wqe) < (unsigned long)(end)))
76 
77 #define WQE_SHADOW_PAGE(wq, wqe)        \
78 		(((unsigned long)(wqe) - (unsigned long)(wq)->shadow_wqe) \
79 			/ (wq)->max_wqe_size)
80 
WQE_PAGE_OFF(struct hinic_wq * wq,u16 idx)81 static inline int WQE_PAGE_OFF(struct hinic_wq *wq, u16 idx)
82 {
83 	return (((idx) & ((wq)->num_wqebbs_per_page - 1))
84 		<< (wq)->wqebb_size_shift);
85 }
86 
WQE_PAGE_NUM(struct hinic_wq * wq,u16 idx)87 static inline int WQE_PAGE_NUM(struct hinic_wq *wq, u16 idx)
88 {
89 	return (((idx) >> ((wq)->wqebbs_per_page_shift))
90 		& ((wq)->num_q_pages - 1));
91 }
92 /**
93  * queue_alloc_page - allocate page for Queue
94  * @hwif: HW interface for allocating DMA
95  * @vaddr: virtual address will be returned in this address
96  * @paddr: physical address will be returned in this address
97  * @shadow_vaddr: VM area will be return here for holding WQ page addresses
98  * @page_sz: page size of each WQ page
99  *
100  * Return 0 - Success, negative - Failure
101  **/
queue_alloc_page(struct hinic_hwif * hwif,u64 ** vaddr,u64 * paddr,void *** shadow_vaddr,size_t page_sz)102 static int queue_alloc_page(struct hinic_hwif *hwif, u64 **vaddr, u64 *paddr,
103 			    void ***shadow_vaddr, size_t page_sz)
104 {
105 	struct pci_dev *pdev = hwif->pdev;
106 	dma_addr_t dma_addr;
107 
108 	*vaddr = dma_alloc_coherent(&pdev->dev, page_sz, &dma_addr,
109 				    GFP_KERNEL);
110 	if (!*vaddr) {
111 		dev_err(&pdev->dev, "Failed to allocate dma for wqs page\n");
112 		return -ENOMEM;
113 	}
114 
115 	*paddr = (u64)dma_addr;
116 
117 	/* use vzalloc for big mem */
118 	*shadow_vaddr = vzalloc(page_sz);
119 	if (!*shadow_vaddr)
120 		goto err_shadow_vaddr;
121 
122 	return 0;
123 
124 err_shadow_vaddr:
125 	dma_free_coherent(&pdev->dev, page_sz, *vaddr, dma_addr);
126 	return -ENOMEM;
127 }
128 
129 /**
130  * wqs_allocate_page - allocate page for WQ set
131  * @wqs: Work Queue Set
132  * @page_idx: the page index of the page will be allocated
133  *
134  * Return 0 - Success, negative - Failure
135  **/
wqs_allocate_page(struct hinic_wqs * wqs,int page_idx)136 static int wqs_allocate_page(struct hinic_wqs *wqs, int page_idx)
137 {
138 	return queue_alloc_page(wqs->hwif, &wqs->page_vaddr[page_idx],
139 				&wqs->page_paddr[page_idx],
140 				&wqs->shadow_page_vaddr[page_idx],
141 				WQS_PAGE_SIZE);
142 }
143 
144 /**
145  * wqs_free_page - free page of WQ set
146  * @wqs: Work Queue Set
147  * @page_idx: the page index of the page will be freed
148  **/
wqs_free_page(struct hinic_wqs * wqs,int page_idx)149 static void wqs_free_page(struct hinic_wqs *wqs, int page_idx)
150 {
151 	struct hinic_hwif *hwif = wqs->hwif;
152 	struct pci_dev *pdev = hwif->pdev;
153 
154 	dma_free_coherent(&pdev->dev, WQS_PAGE_SIZE,
155 			  wqs->page_vaddr[page_idx],
156 			  (dma_addr_t)wqs->page_paddr[page_idx]);
157 	vfree(wqs->shadow_page_vaddr[page_idx]);
158 }
159 
160 /**
161  * cmdq_allocate_page - allocate page for cmdq
162  * @cmdq_pages: the pages of the cmdq queue struct to hold the page
163  *
164  * Return 0 - Success, negative - Failure
165  **/
cmdq_allocate_page(struct hinic_cmdq_pages * cmdq_pages)166 static int cmdq_allocate_page(struct hinic_cmdq_pages *cmdq_pages)
167 {
168 	return queue_alloc_page(cmdq_pages->hwif, &cmdq_pages->page_vaddr,
169 				&cmdq_pages->page_paddr,
170 				&cmdq_pages->shadow_page_vaddr,
171 				CMDQ_PAGE_SIZE);
172 }
173 
174 /**
175  * cmdq_free_page - free page from cmdq
176  * @cmdq_pages: the pages of the cmdq queue struct that hold the page
177  *
178  * Return 0 - Success, negative - Failure
179  **/
cmdq_free_page(struct hinic_cmdq_pages * cmdq_pages)180 static void cmdq_free_page(struct hinic_cmdq_pages *cmdq_pages)
181 {
182 	struct hinic_hwif *hwif = cmdq_pages->hwif;
183 	struct pci_dev *pdev = hwif->pdev;
184 
185 	dma_free_coherent(&pdev->dev, CMDQ_PAGE_SIZE,
186 			  cmdq_pages->page_vaddr,
187 			  (dma_addr_t)cmdq_pages->page_paddr);
188 	vfree(cmdq_pages->shadow_page_vaddr);
189 }
190 
alloc_page_arrays(struct hinic_wqs * wqs)191 static int alloc_page_arrays(struct hinic_wqs *wqs)
192 {
193 	struct hinic_hwif *hwif = wqs->hwif;
194 	struct pci_dev *pdev = hwif->pdev;
195 
196 	wqs->page_paddr = devm_kcalloc(&pdev->dev, wqs->num_pages,
197 				       sizeof(*wqs->page_paddr), GFP_KERNEL);
198 	if (!wqs->page_paddr)
199 		return -ENOMEM;
200 
201 	wqs->page_vaddr = devm_kcalloc(&pdev->dev, wqs->num_pages,
202 				       sizeof(*wqs->page_vaddr), GFP_KERNEL);
203 	if (!wqs->page_vaddr)
204 		goto err_page_vaddr;
205 
206 	wqs->shadow_page_vaddr = devm_kcalloc(&pdev->dev, wqs->num_pages,
207 					      sizeof(*wqs->shadow_page_vaddr),
208 					      GFP_KERNEL);
209 	if (!wqs->shadow_page_vaddr)
210 		goto err_page_shadow_vaddr;
211 
212 	return 0;
213 
214 err_page_shadow_vaddr:
215 	devm_kfree(&pdev->dev, wqs->page_vaddr);
216 
217 err_page_vaddr:
218 	devm_kfree(&pdev->dev, wqs->page_paddr);
219 	return -ENOMEM;
220 }
221 
free_page_arrays(struct hinic_wqs * wqs)222 static void free_page_arrays(struct hinic_wqs *wqs)
223 {
224 	struct hinic_hwif *hwif = wqs->hwif;
225 	struct pci_dev *pdev = hwif->pdev;
226 
227 	devm_kfree(&pdev->dev, wqs->shadow_page_vaddr);
228 	devm_kfree(&pdev->dev, wqs->page_vaddr);
229 	devm_kfree(&pdev->dev, wqs->page_paddr);
230 }
231 
wqs_next_block(struct hinic_wqs * wqs,int * page_idx,int * block_idx)232 static int wqs_next_block(struct hinic_wqs *wqs, int *page_idx,
233 			  int *block_idx)
234 {
235 	int pos;
236 
237 	down(&wqs->alloc_blocks_lock);
238 
239 	wqs->num_free_blks--;
240 
241 	if (wqs->num_free_blks < 0) {
242 		wqs->num_free_blks++;
243 		up(&wqs->alloc_blocks_lock);
244 		return -ENOMEM;
245 	}
246 
247 	pos = wqs->alloc_blk_pos++;
248 	pos &= WQS_MAX_NUM_BLOCKS - 1;
249 
250 	*page_idx = wqs->free_blocks[pos].page_idx;
251 	*block_idx = wqs->free_blocks[pos].block_idx;
252 
253 	wqs->free_blocks[pos].page_idx = -1;
254 	wqs->free_blocks[pos].block_idx = -1;
255 
256 	up(&wqs->alloc_blocks_lock);
257 	return 0;
258 }
259 
wqs_return_block(struct hinic_wqs * wqs,int page_idx,int block_idx)260 static void wqs_return_block(struct hinic_wqs *wqs, int page_idx,
261 			     int block_idx)
262 {
263 	int pos;
264 
265 	down(&wqs->alloc_blocks_lock);
266 
267 	pos = wqs->return_blk_pos++;
268 	pos &= WQS_MAX_NUM_BLOCKS - 1;
269 
270 	wqs->free_blocks[pos].page_idx = page_idx;
271 	wqs->free_blocks[pos].block_idx = block_idx;
272 
273 	wqs->num_free_blks++;
274 
275 	up(&wqs->alloc_blocks_lock);
276 }
277 
init_wqs_blocks_arr(struct hinic_wqs * wqs)278 static void init_wqs_blocks_arr(struct hinic_wqs *wqs)
279 {
280 	int page_idx, blk_idx, pos = 0;
281 
282 	for (page_idx = 0; page_idx < wqs->num_pages; page_idx++) {
283 		for (blk_idx = 0; blk_idx < WQS_BLOCKS_PER_PAGE; blk_idx++) {
284 			wqs->free_blocks[pos].page_idx = page_idx;
285 			wqs->free_blocks[pos].block_idx = blk_idx;
286 			pos++;
287 		}
288 	}
289 
290 	wqs->alloc_blk_pos = 0;
291 	wqs->return_blk_pos = pos;
292 	wqs->num_free_blks = pos;
293 
294 	sema_init(&wqs->alloc_blocks_lock, 1);
295 }
296 
297 /**
298  * hinic_wqs_alloc - allocate Work Queues set
299  * @wqs: Work Queue Set
300  * @max_wqs: maximum wqs to allocate
301  * @hwif: HW interface for use for the allocation
302  *
303  * Return 0 - Success, negative - Failure
304  **/
hinic_wqs_alloc(struct hinic_wqs * wqs,int max_wqs,struct hinic_hwif * hwif)305 int hinic_wqs_alloc(struct hinic_wqs *wqs, int max_wqs,
306 		    struct hinic_hwif *hwif)
307 {
308 	struct pci_dev *pdev = hwif->pdev;
309 	int err, i, page_idx;
310 
311 	max_wqs = ALIGN(max_wqs, WQS_BLOCKS_PER_PAGE);
312 	if (max_wqs > WQS_MAX_NUM_BLOCKS)  {
313 		dev_err(&pdev->dev, "Invalid max_wqs = %d\n", max_wqs);
314 		return -EINVAL;
315 	}
316 
317 	wqs->hwif = hwif;
318 	wqs->num_pages = max_wqs / WQS_BLOCKS_PER_PAGE;
319 
320 	if (alloc_page_arrays(wqs)) {
321 		dev_err(&pdev->dev,
322 			"Failed to allocate mem for page addresses\n");
323 		return -ENOMEM;
324 	}
325 
326 	for (page_idx = 0; page_idx < wqs->num_pages; page_idx++) {
327 		err = wqs_allocate_page(wqs, page_idx);
328 		if (err) {
329 			dev_err(&pdev->dev, "Failed wq page allocation\n");
330 			goto err_wq_allocate_page;
331 		}
332 	}
333 
334 	wqs->free_blocks = devm_kzalloc(&pdev->dev, WQS_FREE_BLOCKS_SIZE(wqs),
335 					GFP_KERNEL);
336 	if (!wqs->free_blocks) {
337 		err = -ENOMEM;
338 		goto err_alloc_blocks;
339 	}
340 
341 	init_wqs_blocks_arr(wqs);
342 	return 0;
343 
344 err_alloc_blocks:
345 err_wq_allocate_page:
346 	for (i = 0; i < page_idx; i++)
347 		wqs_free_page(wqs, i);
348 
349 	free_page_arrays(wqs);
350 	return err;
351 }
352 
353 /**
354  * hinic_wqs_free - free Work Queues set
355  * @wqs: Work Queue Set
356  **/
hinic_wqs_free(struct hinic_wqs * wqs)357 void hinic_wqs_free(struct hinic_wqs *wqs)
358 {
359 	struct hinic_hwif *hwif = wqs->hwif;
360 	struct pci_dev *pdev = hwif->pdev;
361 	int page_idx;
362 
363 	devm_kfree(&pdev->dev, wqs->free_blocks);
364 
365 	for (page_idx = 0; page_idx < wqs->num_pages; page_idx++)
366 		wqs_free_page(wqs, page_idx);
367 
368 	free_page_arrays(wqs);
369 }
370 
371 /**
372  * alloc_wqes_shadow - allocate WQE shadows for WQ
373  * @wq: WQ to allocate shadows for
374  *
375  * Return 0 - Success, negative - Failure
376  **/
alloc_wqes_shadow(struct hinic_wq * wq)377 static int alloc_wqes_shadow(struct hinic_wq *wq)
378 {
379 	struct hinic_hwif *hwif = wq->hwif;
380 	struct pci_dev *pdev = hwif->pdev;
381 
382 	wq->shadow_wqe = devm_kcalloc(&pdev->dev, wq->num_q_pages,
383 				      wq->max_wqe_size, GFP_KERNEL);
384 	if (!wq->shadow_wqe)
385 		return -ENOMEM;
386 
387 	wq->shadow_idx = devm_kcalloc(&pdev->dev, wq->num_q_pages,
388 				      sizeof(*wq->shadow_idx), GFP_KERNEL);
389 	if (!wq->shadow_idx)
390 		goto err_shadow_idx;
391 
392 	return 0;
393 
394 err_shadow_idx:
395 	devm_kfree(&pdev->dev, wq->shadow_wqe);
396 	return -ENOMEM;
397 }
398 
399 /**
400  * free_wqes_shadow - free WQE shadows of WQ
401  * @wq: WQ to free shadows from
402  **/
free_wqes_shadow(struct hinic_wq * wq)403 static void free_wqes_shadow(struct hinic_wq *wq)
404 {
405 	struct hinic_hwif *hwif = wq->hwif;
406 	struct pci_dev *pdev = hwif->pdev;
407 
408 	devm_kfree(&pdev->dev, wq->shadow_idx);
409 	devm_kfree(&pdev->dev, wq->shadow_wqe);
410 }
411 
412 /**
413  * free_wq_pages - free pages of WQ
414  * @hwif: HW interface for releasing dma addresses
415  * @wq: WQ to free pages from
416  * @num_q_pages: number pages to free
417  **/
free_wq_pages(struct hinic_wq * wq,struct hinic_hwif * hwif,int num_q_pages)418 static void free_wq_pages(struct hinic_wq *wq, struct hinic_hwif *hwif,
419 			  int num_q_pages)
420 {
421 	struct pci_dev *pdev = hwif->pdev;
422 	int i;
423 
424 	for (i = 0; i < num_q_pages; i++) {
425 		void **vaddr = &wq->shadow_block_vaddr[i];
426 		u64 *paddr = &wq->block_vaddr[i];
427 		dma_addr_t dma_addr;
428 
429 		dma_addr = (dma_addr_t)be64_to_cpu(*paddr);
430 		dma_free_coherent(&pdev->dev, wq->wq_page_size, *vaddr,
431 				  dma_addr);
432 	}
433 
434 	free_wqes_shadow(wq);
435 }
436 
437 /**
438  * alloc_wq_pages - alloc pages for WQ
439  * @hwif: HW interface for allocating dma addresses
440  * @wq: WQ to allocate pages for
441  * @max_pages: maximum pages allowed
442  *
443  * Return 0 - Success, negative - Failure
444  **/
alloc_wq_pages(struct hinic_wq * wq,struct hinic_hwif * hwif,int max_pages)445 static int alloc_wq_pages(struct hinic_wq *wq, struct hinic_hwif *hwif,
446 			  int max_pages)
447 {
448 	struct pci_dev *pdev = hwif->pdev;
449 	int i, err, num_q_pages;
450 
451 	num_q_pages = ALIGN(WQ_SIZE(wq), wq->wq_page_size) / wq->wq_page_size;
452 	if (num_q_pages > max_pages) {
453 		dev_err(&pdev->dev, "Number wq pages exceeds the limit\n");
454 		return -EINVAL;
455 	}
456 
457 	if (num_q_pages & (num_q_pages - 1)) {
458 		dev_err(&pdev->dev, "Number wq pages must be power of 2\n");
459 		return -EINVAL;
460 	}
461 
462 	wq->num_q_pages = num_q_pages;
463 
464 	err = alloc_wqes_shadow(wq);
465 	if (err) {
466 		dev_err(&pdev->dev, "Failed to allocate wqe shadow\n");
467 		return err;
468 	}
469 
470 	for (i = 0; i < num_q_pages; i++) {
471 		void **vaddr = &wq->shadow_block_vaddr[i];
472 		u64 *paddr = &wq->block_vaddr[i];
473 		dma_addr_t dma_addr;
474 
475 		*vaddr = dma_alloc_coherent(&pdev->dev, wq->wq_page_size,
476 					    &dma_addr, GFP_KERNEL);
477 		if (!*vaddr) {
478 			dev_err(&pdev->dev, "Failed to allocate wq page\n");
479 			goto err_alloc_wq_pages;
480 		}
481 
482 		/* HW uses Big Endian Format */
483 		*paddr = cpu_to_be64(dma_addr);
484 	}
485 
486 	return 0;
487 
488 err_alloc_wq_pages:
489 	free_wq_pages(wq, hwif, i);
490 	return -ENOMEM;
491 }
492 
493 /**
494  * hinic_wq_allocate - Allocate the WQ resources from the WQS
495  * @wqs: WQ set from which to allocate the WQ resources
496  * @wq: WQ to allocate resources for it from the WQ set
497  * @wqebb_size: Work Queue Block Byte Size
498  * @wq_page_size: the page size in the Work Queue
499  * @q_depth: number of wqebbs in WQ
500  * @max_wqe_size: maximum WQE size that will be used in the WQ
501  *
502  * Return 0 - Success, negative - Failure
503  **/
hinic_wq_allocate(struct hinic_wqs * wqs,struct hinic_wq * wq,u16 wqebb_size,u32 wq_page_size,u16 q_depth,u16 max_wqe_size)504 int hinic_wq_allocate(struct hinic_wqs *wqs, struct hinic_wq *wq,
505 		      u16 wqebb_size, u32 wq_page_size, u16 q_depth,
506 		      u16 max_wqe_size)
507 {
508 	struct hinic_hwif *hwif = wqs->hwif;
509 	struct pci_dev *pdev = hwif->pdev;
510 	u16 num_wqebbs_per_page;
511 	u16 wqebb_size_shift;
512 	int err;
513 
514 	if (!is_power_of_2(wqebb_size)) {
515 		dev_err(&pdev->dev, "wqebb_size must be power of 2\n");
516 		return -EINVAL;
517 	}
518 
519 	if (wq_page_size == 0) {
520 		dev_err(&pdev->dev, "wq_page_size must be > 0\n");
521 		return -EINVAL;
522 	}
523 
524 	if (q_depth & (q_depth - 1)) {
525 		dev_err(&pdev->dev, "WQ q_depth must be power of 2\n");
526 		return -EINVAL;
527 	}
528 
529 	wqebb_size_shift = ilog2(wqebb_size);
530 	num_wqebbs_per_page = ALIGN(wq_page_size, wqebb_size)
531 				>> wqebb_size_shift;
532 
533 	if (!is_power_of_2(num_wqebbs_per_page)) {
534 		dev_err(&pdev->dev, "num wqebbs per page must be power of 2\n");
535 		return -EINVAL;
536 	}
537 
538 	wq->hwif = hwif;
539 
540 	err = wqs_next_block(wqs, &wq->page_idx, &wq->block_idx);
541 	if (err) {
542 		dev_err(&pdev->dev, "Failed to get free wqs next block\n");
543 		return err;
544 	}
545 
546 	wq->wqebb_size = wqebb_size;
547 	wq->wq_page_size = wq_page_size;
548 	wq->q_depth = q_depth;
549 	wq->max_wqe_size = max_wqe_size;
550 	wq->num_wqebbs_per_page = num_wqebbs_per_page;
551 	wq->wqebbs_per_page_shift = ilog2(num_wqebbs_per_page);
552 	wq->wqebb_size_shift = wqebb_size_shift;
553 	wq->block_vaddr = WQ_BASE_VADDR(wqs, wq);
554 	wq->shadow_block_vaddr = WQ_BASE_ADDR(wqs, wq);
555 	wq->block_paddr = WQ_BASE_PADDR(wqs, wq);
556 
557 	err = alloc_wq_pages(wq, wqs->hwif, WQ_MAX_PAGES);
558 	if (err) {
559 		dev_err(&pdev->dev, "Failed to allocate wq pages\n");
560 		goto err_alloc_wq_pages;
561 	}
562 
563 	atomic_set(&wq->cons_idx, 0);
564 	atomic_set(&wq->prod_idx, 0);
565 	atomic_set(&wq->delta, q_depth);
566 	wq->mask = q_depth - 1;
567 
568 	return 0;
569 
570 err_alloc_wq_pages:
571 	wqs_return_block(wqs, wq->page_idx, wq->block_idx);
572 	return err;
573 }
574 
575 /**
576  * hinic_wq_free - Free the WQ resources to the WQS
577  * @wqs: WQ set to free the WQ resources to it
578  * @wq: WQ to free its resources to the WQ set resources
579  **/
hinic_wq_free(struct hinic_wqs * wqs,struct hinic_wq * wq)580 void hinic_wq_free(struct hinic_wqs *wqs, struct hinic_wq *wq)
581 {
582 	free_wq_pages(wq, wqs->hwif, wq->num_q_pages);
583 
584 	wqs_return_block(wqs, wq->page_idx, wq->block_idx);
585 }
586 
587 /**
588  * hinic_wqs_cmdq_alloc - Allocate wqs for cmdqs
589  * @cmdq_pages: will hold the pages of the cmdq
590  * @wq: returned wqs
591  * @hwif: HW interface
592  * @cmdq_blocks: number of cmdq blocks/wq to allocate
593  * @wqebb_size: Work Queue Block Byte Size
594  * @wq_page_size: the page size in the Work Queue
595  * @q_depth: number of wqebbs in WQ
596  * @max_wqe_size: maximum WQE size that will be used in the WQ
597  *
598  * Return 0 - Success, negative - Failure
599  **/
hinic_wqs_cmdq_alloc(struct hinic_cmdq_pages * cmdq_pages,struct hinic_wq * wq,struct hinic_hwif * hwif,int cmdq_blocks,u16 wqebb_size,u32 wq_page_size,u16 q_depth,u16 max_wqe_size)600 int hinic_wqs_cmdq_alloc(struct hinic_cmdq_pages *cmdq_pages,
601 			 struct hinic_wq *wq, struct hinic_hwif *hwif,
602 			 int cmdq_blocks, u16 wqebb_size, u32 wq_page_size,
603 			 u16 q_depth, u16 max_wqe_size)
604 {
605 	struct pci_dev *pdev = hwif->pdev;
606 	u16 num_wqebbs_per_page_shift;
607 	u16 num_wqebbs_per_page;
608 	u16 wqebb_size_shift;
609 	int i, j, err = -ENOMEM;
610 
611 	if (!is_power_of_2(wqebb_size)) {
612 		dev_err(&pdev->dev, "wqebb_size must be power of 2\n");
613 		return -EINVAL;
614 	}
615 
616 	if (wq_page_size == 0) {
617 		dev_err(&pdev->dev, "wq_page_size must be > 0\n");
618 		return -EINVAL;
619 	}
620 
621 	if (q_depth & (q_depth - 1)) {
622 		dev_err(&pdev->dev, "WQ q_depth must be power of 2\n");
623 		return -EINVAL;
624 	}
625 
626 	wqebb_size_shift = ilog2(wqebb_size);
627 	num_wqebbs_per_page = ALIGN(wq_page_size, wqebb_size)
628 				>> wqebb_size_shift;
629 
630 	if (!is_power_of_2(num_wqebbs_per_page)) {
631 		dev_err(&pdev->dev, "num wqebbs per page must be power of 2\n");
632 		return -EINVAL;
633 	}
634 
635 	cmdq_pages->hwif = hwif;
636 
637 	err = cmdq_allocate_page(cmdq_pages);
638 	if (err) {
639 		dev_err(&pdev->dev, "Failed to allocate CMDQ page\n");
640 		return err;
641 	}
642 	num_wqebbs_per_page_shift = ilog2(num_wqebbs_per_page);
643 
644 	for (i = 0; i < cmdq_blocks; i++) {
645 		wq[i].hwif = hwif;
646 		wq[i].page_idx = 0;
647 		wq[i].block_idx = i;
648 
649 		wq[i].wqebb_size = wqebb_size;
650 		wq[i].wq_page_size = wq_page_size;
651 		wq[i].q_depth = q_depth;
652 		wq[i].max_wqe_size = max_wqe_size;
653 		wq[i].num_wqebbs_per_page = num_wqebbs_per_page;
654 		wq[i].wqebbs_per_page_shift = num_wqebbs_per_page_shift;
655 		wq[i].wqebb_size_shift = wqebb_size_shift;
656 		wq[i].block_vaddr = CMDQ_BASE_VADDR(cmdq_pages, &wq[i]);
657 		wq[i].shadow_block_vaddr = CMDQ_BASE_ADDR(cmdq_pages, &wq[i]);
658 		wq[i].block_paddr = CMDQ_BASE_PADDR(cmdq_pages, &wq[i]);
659 
660 		err = alloc_wq_pages(&wq[i], cmdq_pages->hwif,
661 				     CMDQ_WQ_MAX_PAGES);
662 		if (err) {
663 			dev_err(&pdev->dev, "Failed to alloc CMDQ blocks\n");
664 			goto err_cmdq_block;
665 		}
666 
667 		atomic_set(&wq[i].cons_idx, 0);
668 		atomic_set(&wq[i].prod_idx, 0);
669 		atomic_set(&wq[i].delta, q_depth);
670 		wq[i].mask = q_depth - 1;
671 	}
672 
673 	return 0;
674 
675 err_cmdq_block:
676 	for (j = 0; j < i; j++)
677 		free_wq_pages(&wq[j], cmdq_pages->hwif, wq[j].num_q_pages);
678 
679 	cmdq_free_page(cmdq_pages);
680 	return err;
681 }
682 
683 /**
684  * hinic_wqs_cmdq_free - Free wqs from cmdqs
685  * @cmdq_pages: hold the pages of the cmdq
686  * @wq: wqs to free
687  * @cmdq_blocks: number of wqs to free
688  **/
hinic_wqs_cmdq_free(struct hinic_cmdq_pages * cmdq_pages,struct hinic_wq * wq,int cmdq_blocks)689 void hinic_wqs_cmdq_free(struct hinic_cmdq_pages *cmdq_pages,
690 			 struct hinic_wq *wq, int cmdq_blocks)
691 {
692 	int i;
693 
694 	for (i = 0; i < cmdq_blocks; i++)
695 		free_wq_pages(&wq[i], cmdq_pages->hwif, wq[i].num_q_pages);
696 
697 	cmdq_free_page(cmdq_pages);
698 }
699 
copy_wqe_to_shadow(struct hinic_wq * wq,void * shadow_addr,int num_wqebbs,u16 idx)700 static void copy_wqe_to_shadow(struct hinic_wq *wq, void *shadow_addr,
701 			       int num_wqebbs, u16 idx)
702 {
703 	void *wqebb_addr;
704 	int i;
705 
706 	for (i = 0; i < num_wqebbs; i++, idx++) {
707 		idx = MASKED_WQE_IDX(wq, idx);
708 		wqebb_addr = WQ_PAGE_ADDR(wq, idx) +
709 			     WQE_PAGE_OFF(wq, idx);
710 
711 		memcpy(shadow_addr, wqebb_addr, wq->wqebb_size);
712 
713 		shadow_addr += wq->wqebb_size;
714 	}
715 }
716 
copy_wqe_from_shadow(struct hinic_wq * wq,void * shadow_addr,int num_wqebbs,u16 idx)717 static void copy_wqe_from_shadow(struct hinic_wq *wq, void *shadow_addr,
718 				 int num_wqebbs, u16 idx)
719 {
720 	void *wqebb_addr;
721 	int i;
722 
723 	for (i = 0; i < num_wqebbs; i++, idx++) {
724 		idx = MASKED_WQE_IDX(wq, idx);
725 		wqebb_addr = WQ_PAGE_ADDR(wq, idx) +
726 			     WQE_PAGE_OFF(wq, idx);
727 
728 		memcpy(wqebb_addr, shadow_addr, wq->wqebb_size);
729 		shadow_addr += wq->wqebb_size;
730 	}
731 }
732 
733 /**
734  * hinic_get_wqe - get wqe ptr in the current pi and update the pi
735  * @wq: wq to get wqe from
736  * @wqe_size: wqe size
737  * @prod_idx: returned pi
738  *
739  * Return wqe pointer
740  **/
hinic_get_wqe(struct hinic_wq * wq,unsigned int wqe_size,u16 * prod_idx)741 struct hinic_hw_wqe *hinic_get_wqe(struct hinic_wq *wq, unsigned int wqe_size,
742 				   u16 *prod_idx)
743 {
744 	int curr_pg, end_pg, num_wqebbs;
745 	u16 curr_prod_idx, end_prod_idx;
746 
747 	*prod_idx = MASKED_WQE_IDX(wq, atomic_read(&wq->prod_idx));
748 
749 	num_wqebbs = ALIGN(wqe_size, wq->wqebb_size) >> wq->wqebb_size_shift;
750 
751 	if (atomic_sub_return(num_wqebbs, &wq->delta) <= 0) {
752 		atomic_add(num_wqebbs, &wq->delta);
753 		return ERR_PTR(-EBUSY);
754 	}
755 
756 	end_prod_idx = atomic_add_return(num_wqebbs, &wq->prod_idx);
757 
758 	end_prod_idx = MASKED_WQE_IDX(wq, end_prod_idx);
759 	curr_prod_idx = end_prod_idx - num_wqebbs;
760 	curr_prod_idx = MASKED_WQE_IDX(wq, curr_prod_idx);
761 
762 	/* end prod index points to the next wqebb, therefore minus 1 */
763 	end_prod_idx = MASKED_WQE_IDX(wq, end_prod_idx - 1);
764 
765 	curr_pg = WQE_PAGE_NUM(wq, curr_prod_idx);
766 	end_pg = WQE_PAGE_NUM(wq, end_prod_idx);
767 
768 	*prod_idx = curr_prod_idx;
769 
770 	/* If we only have one page, still need to get shadown wqe when
771 	 * wqe rolling-over page
772 	 */
773 	if (curr_pg != end_pg || end_prod_idx < *prod_idx) {
774 		void *shadow_addr = &wq->shadow_wqe[curr_pg * wq->max_wqe_size];
775 
776 		copy_wqe_to_shadow(wq, shadow_addr, num_wqebbs, *prod_idx);
777 
778 		wq->shadow_idx[curr_pg] = *prod_idx;
779 		return shadow_addr;
780 	}
781 
782 	return WQ_PAGE_ADDR(wq, *prod_idx) + WQE_PAGE_OFF(wq, *prod_idx);
783 }
784 
785 /**
786  * hinic_return_wqe - return the wqe when transmit failed
787  * @wq: wq to return wqe
788  * @wqe_size: wqe size
789  **/
hinic_return_wqe(struct hinic_wq * wq,unsigned int wqe_size)790 void hinic_return_wqe(struct hinic_wq *wq, unsigned int wqe_size)
791 {
792 	int num_wqebbs = ALIGN(wqe_size, wq->wqebb_size) / wq->wqebb_size;
793 
794 	atomic_sub(num_wqebbs, &wq->prod_idx);
795 
796 	atomic_add(num_wqebbs, &wq->delta);
797 }
798 
799 /**
800  * hinic_put_wqe - return the wqe place to use for a new wqe
801  * @wq: wq to return wqe
802  * @wqe_size: wqe size
803  **/
hinic_put_wqe(struct hinic_wq * wq,unsigned int wqe_size)804 void hinic_put_wqe(struct hinic_wq *wq, unsigned int wqe_size)
805 {
806 	int num_wqebbs = ALIGN(wqe_size, wq->wqebb_size)
807 			>> wq->wqebb_size_shift;
808 
809 	atomic_add(num_wqebbs, &wq->cons_idx);
810 
811 	atomic_add(num_wqebbs, &wq->delta);
812 }
813 
814 /**
815  * hinic_read_wqe - read wqe ptr in the current ci
816  * @wq: wq to get read from
817  * @wqe_size: wqe size
818  * @cons_idx: returned ci
819  *
820  * Return wqe pointer
821  **/
hinic_read_wqe(struct hinic_wq * wq,unsigned int wqe_size,u16 * cons_idx)822 struct hinic_hw_wqe *hinic_read_wqe(struct hinic_wq *wq, unsigned int wqe_size,
823 				    u16 *cons_idx)
824 {
825 	int num_wqebbs = ALIGN(wqe_size, wq->wqebb_size)
826 			>> wq->wqebb_size_shift;
827 	u16 curr_cons_idx, end_cons_idx;
828 	int curr_pg, end_pg;
829 
830 	if ((atomic_read(&wq->delta) + num_wqebbs) > wq->q_depth)
831 		return ERR_PTR(-EBUSY);
832 
833 	curr_cons_idx = atomic_read(&wq->cons_idx);
834 
835 	curr_cons_idx = MASKED_WQE_IDX(wq, curr_cons_idx);
836 	end_cons_idx = MASKED_WQE_IDX(wq, curr_cons_idx + num_wqebbs - 1);
837 
838 	curr_pg = WQE_PAGE_NUM(wq, curr_cons_idx);
839 	end_pg = WQE_PAGE_NUM(wq, end_cons_idx);
840 
841 	*cons_idx = curr_cons_idx;
842 
843 	/* If we only have one page, still need to get shadown wqe when
844 	 * wqe rolling-over page
845 	 */
846 	if (curr_pg != end_pg || end_cons_idx < curr_cons_idx) {
847 		void *shadow_addr = &wq->shadow_wqe[curr_pg * wq->max_wqe_size];
848 
849 		copy_wqe_to_shadow(wq, shadow_addr, num_wqebbs, *cons_idx);
850 		return shadow_addr;
851 	}
852 
853 	return WQ_PAGE_ADDR(wq, *cons_idx) + WQE_PAGE_OFF(wq, *cons_idx);
854 }
855 
856 /**
857  * hinic_read_wqe_direct - read wqe directly from ci position
858  * @wq: wq
859  * @cons_idx: ci position
860  *
861  * Return wqe
862  **/
hinic_read_wqe_direct(struct hinic_wq * wq,u16 cons_idx)863 struct hinic_hw_wqe *hinic_read_wqe_direct(struct hinic_wq *wq, u16 cons_idx)
864 {
865 	return WQ_PAGE_ADDR(wq, cons_idx) + WQE_PAGE_OFF(wq, cons_idx);
866 }
867 
868 /**
869  * wqe_shadow - check if a wqe is shadow
870  * @wq: wq of the wqe
871  * @wqe: the wqe for shadow checking
872  *
873  * Return true - shadow, false - Not shadow
874  **/
wqe_shadow(struct hinic_wq * wq,struct hinic_hw_wqe * wqe)875 static inline bool wqe_shadow(struct hinic_wq *wq, struct hinic_hw_wqe *wqe)
876 {
877 	size_t wqe_shadow_size = wq->num_q_pages * wq->max_wqe_size;
878 
879 	return WQE_IN_RANGE(wqe, wq->shadow_wqe,
880 			    &wq->shadow_wqe[wqe_shadow_size]);
881 }
882 
883 /**
884  * hinic_write_wqe - write the wqe to the wq
885  * @wq: wq to write wqe to
886  * @wqe: wqe to write
887  * @wqe_size: wqe size
888  **/
hinic_write_wqe(struct hinic_wq * wq,struct hinic_hw_wqe * wqe,unsigned int wqe_size)889 void hinic_write_wqe(struct hinic_wq *wq, struct hinic_hw_wqe *wqe,
890 		     unsigned int wqe_size)
891 {
892 	int curr_pg, num_wqebbs;
893 	void *shadow_addr;
894 	u16 prod_idx;
895 
896 	if (wqe_shadow(wq, wqe)) {
897 		curr_pg = WQE_SHADOW_PAGE(wq, wqe);
898 
899 		prod_idx = wq->shadow_idx[curr_pg];
900 		num_wqebbs = ALIGN(wqe_size, wq->wqebb_size) / wq->wqebb_size;
901 		shadow_addr = &wq->shadow_wqe[curr_pg * wq->max_wqe_size];
902 
903 		copy_wqe_from_shadow(wq, shadow_addr, num_wqebbs, prod_idx);
904 	}
905 }
906