xref: /openbmc/linux/drivers/scsi/csiostor/csio_wr.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1a3667aaeSNaresh Kumar Inna /*
2a3667aaeSNaresh Kumar Inna  * This file is part of the Chelsio FCoE driver for Linux.
3a3667aaeSNaresh Kumar Inna  *
4a3667aaeSNaresh Kumar Inna  * Copyright (c) 2008-2012 Chelsio Communications, Inc. All rights reserved.
5a3667aaeSNaresh Kumar Inna  *
6a3667aaeSNaresh Kumar Inna  * This software is available to you under a choice of one of two
7a3667aaeSNaresh Kumar Inna  * licenses.  You may choose to be licensed under the terms of the GNU
8a3667aaeSNaresh Kumar Inna  * General Public License (GPL) Version 2, available from the file
9a3667aaeSNaresh Kumar Inna  * COPYING in the main directory of this source tree, or the
10a3667aaeSNaresh Kumar Inna  * OpenIB.org BSD license below:
11a3667aaeSNaresh Kumar Inna  *
12a3667aaeSNaresh Kumar Inna  *     Redistribution and use in source and binary forms, with or
13a3667aaeSNaresh Kumar Inna  *     without modification, are permitted provided that the following
14a3667aaeSNaresh Kumar Inna  *     conditions are met:
15a3667aaeSNaresh Kumar Inna  *
16a3667aaeSNaresh Kumar Inna  *      - Redistributions of source code must retain the above
17a3667aaeSNaresh Kumar Inna  *        copyright notice, this list of conditions and the following
18a3667aaeSNaresh Kumar Inna  *        disclaimer.
19a3667aaeSNaresh Kumar Inna  *
20a3667aaeSNaresh Kumar Inna  *      - Redistributions in binary form must reproduce the above
21a3667aaeSNaresh Kumar Inna  *        copyright notice, this list of conditions and the following
22a3667aaeSNaresh Kumar Inna  *        disclaimer in the documentation and/or other materials
23a3667aaeSNaresh Kumar Inna  *        provided with the distribution.
24a3667aaeSNaresh Kumar Inna  *
25a3667aaeSNaresh Kumar Inna  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26a3667aaeSNaresh Kumar Inna  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27a3667aaeSNaresh Kumar Inna  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28a3667aaeSNaresh Kumar Inna  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29a3667aaeSNaresh Kumar Inna  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30a3667aaeSNaresh Kumar Inna  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31a3667aaeSNaresh Kumar Inna  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32a3667aaeSNaresh Kumar Inna  * SOFTWARE.
33a3667aaeSNaresh Kumar Inna  */
34a3667aaeSNaresh Kumar Inna 
35a3667aaeSNaresh Kumar Inna #include <linux/kernel.h>
36a3667aaeSNaresh Kumar Inna #include <linux/string.h>
37a3667aaeSNaresh Kumar Inna #include <linux/compiler.h>
38a3667aaeSNaresh Kumar Inna #include <linux/slab.h>
39a3667aaeSNaresh Kumar Inna #include <asm/page.h>
40a3667aaeSNaresh Kumar Inna #include <linux/cache.h>
41a3667aaeSNaresh Kumar Inna 
42db67befaSVarun Prakash #include "t4_values.h"
43a3667aaeSNaresh Kumar Inna #include "csio_hw.h"
44a3667aaeSNaresh Kumar Inna #include "csio_wr.h"
45a3667aaeSNaresh Kumar Inna #include "csio_mb.h"
46a3667aaeSNaresh Kumar Inna #include "csio_defs.h"
47a3667aaeSNaresh Kumar Inna 
48a3667aaeSNaresh Kumar Inna int csio_intr_coalesce_cnt;		/* value:SGE_INGRESS_RX_THRESHOLD[0] */
49a3667aaeSNaresh Kumar Inna static int csio_sge_thresh_reg;		/* SGE_INGRESS_RX_THRESHOLD[0] */
50a3667aaeSNaresh Kumar Inna 
51a3667aaeSNaresh Kumar Inna int csio_intr_coalesce_time = 10;	/* value:SGE_TIMER_VALUE_1 */
52a3667aaeSNaresh Kumar Inna static int csio_sge_timer_reg = 1;
53a3667aaeSNaresh Kumar Inna 
54a3667aaeSNaresh Kumar Inna #define CSIO_SET_FLBUF_SIZE(_hw, _reg, _val)				\
55f612b815SHariprasad Shenai 	csio_wr_reg32((_hw), (_val), SGE_FL_BUFFER_SIZE##_reg##_A)
56a3667aaeSNaresh Kumar Inna 
57a3667aaeSNaresh Kumar Inna static void
csio_get_flbuf_size(struct csio_hw * hw,struct csio_sge * sge,uint32_t reg)58a3667aaeSNaresh Kumar Inna csio_get_flbuf_size(struct csio_hw *hw, struct csio_sge *sge, uint32_t reg)
59a3667aaeSNaresh Kumar Inna {
60f612b815SHariprasad Shenai 	sge->sge_fl_buf_size[reg] = csio_rd_reg32(hw, SGE_FL_BUFFER_SIZE0_A +
61a3667aaeSNaresh Kumar Inna 							reg * sizeof(uint32_t));
62a3667aaeSNaresh Kumar Inna }
63a3667aaeSNaresh Kumar Inna 
64a3667aaeSNaresh Kumar Inna /* Free list buffer size */
65a3667aaeSNaresh Kumar Inna static inline uint32_t
csio_wr_fl_bufsz(struct csio_sge * sge,struct csio_dma_buf * buf)66a3667aaeSNaresh Kumar Inna csio_wr_fl_bufsz(struct csio_sge *sge, struct csio_dma_buf *buf)
67a3667aaeSNaresh Kumar Inna {
68a3667aaeSNaresh Kumar Inna 	return sge->sge_fl_buf_size[buf->paddr & 0xF];
69a3667aaeSNaresh Kumar Inna }
70a3667aaeSNaresh Kumar Inna 
71a3667aaeSNaresh Kumar Inna /* Size of the egress queue status page */
72a3667aaeSNaresh Kumar Inna static inline uint32_t
csio_wr_qstat_pgsz(struct csio_hw * hw)73a3667aaeSNaresh Kumar Inna csio_wr_qstat_pgsz(struct csio_hw *hw)
74a3667aaeSNaresh Kumar Inna {
75f612b815SHariprasad Shenai 	return (hw->wrm.sge.sge_control & EGRSTATUSPAGESIZE_F) ?  128 : 64;
76a3667aaeSNaresh Kumar Inna }
77a3667aaeSNaresh Kumar Inna 
78a3667aaeSNaresh Kumar Inna /* Ring freelist doorbell */
79a3667aaeSNaresh Kumar Inna static inline void
csio_wr_ring_fldb(struct csio_hw * hw,struct csio_q * flq)80a3667aaeSNaresh Kumar Inna csio_wr_ring_fldb(struct csio_hw *hw, struct csio_q *flq)
81a3667aaeSNaresh Kumar Inna {
82a3667aaeSNaresh Kumar Inna 	/*
83a3667aaeSNaresh Kumar Inna 	 * Ring the doorbell only when we have atleast CSIO_QCREDIT_SZ
84a3667aaeSNaresh Kumar Inna 	 * number of bytes in the freelist queue. This translates to atleast
85a3667aaeSNaresh Kumar Inna 	 * 8 freelist buffer pointers (since each pointer is 8 bytes).
86a3667aaeSNaresh Kumar Inna 	 */
87a3667aaeSNaresh Kumar Inna 	if (flq->inc_idx >= 8) {
88f612b815SHariprasad Shenai 		csio_wr_reg32(hw, DBPRIO_F | QID_V(flq->un.fl.flid) |
893fb4c22eSPraveen Madhavan 				  PIDX_T5_V(flq->inc_idx / 8) | DBTYPE_F,
90f612b815SHariprasad Shenai 				  MYPF_REG(SGE_PF_KDOORBELL_A));
91a3667aaeSNaresh Kumar Inna 		flq->inc_idx &= 7;
92a3667aaeSNaresh Kumar Inna 	}
93a3667aaeSNaresh Kumar Inna }
94a3667aaeSNaresh Kumar Inna 
95a3667aaeSNaresh Kumar Inna /* Write a 0 cidx increment value to enable SGE interrupts for this queue */
96a3667aaeSNaresh Kumar Inna static void
csio_wr_sge_intr_enable(struct csio_hw * hw,uint16_t iqid)97a3667aaeSNaresh Kumar Inna csio_wr_sge_intr_enable(struct csio_hw *hw, uint16_t iqid)
98a3667aaeSNaresh Kumar Inna {
99f612b815SHariprasad Shenai 	csio_wr_reg32(hw, CIDXINC_V(0)		|
100f612b815SHariprasad Shenai 			  INGRESSQID_V(iqid)	|
101f612b815SHariprasad Shenai 			  TIMERREG_V(X_TIMERREG_RESTART_COUNTER),
102f612b815SHariprasad Shenai 			  MYPF_REG(SGE_PF_GTS_A));
103a3667aaeSNaresh Kumar Inna }
104a3667aaeSNaresh Kumar Inna 
105a3667aaeSNaresh Kumar Inna /*
106a3667aaeSNaresh Kumar Inna  * csio_wr_fill_fl - Populate the FL buffers of a FL queue.
107a3667aaeSNaresh Kumar Inna  * @hw: HW module.
108a3667aaeSNaresh Kumar Inna  * @flq: Freelist queue.
109a3667aaeSNaresh Kumar Inna  *
110a3667aaeSNaresh Kumar Inna  * Fill up freelist buffer entries with buffers of size specified
111a3667aaeSNaresh Kumar Inna  * in the size register.
112a3667aaeSNaresh Kumar Inna  *
113a3667aaeSNaresh Kumar Inna  */
114a3667aaeSNaresh Kumar Inna static int
csio_wr_fill_fl(struct csio_hw * hw,struct csio_q * flq)115a3667aaeSNaresh Kumar Inna csio_wr_fill_fl(struct csio_hw *hw, struct csio_q *flq)
116a3667aaeSNaresh Kumar Inna {
117a3667aaeSNaresh Kumar Inna 	struct csio_wrm *wrm = csio_hw_to_wrm(hw);
118a3667aaeSNaresh Kumar Inna 	struct csio_sge *sge = &wrm->sge;
119a3667aaeSNaresh Kumar Inna 	__be64 *d = (__be64 *)(flq->vstart);
120a3667aaeSNaresh Kumar Inna 	struct csio_dma_buf *buf = &flq->un.fl.bufs[0];
121a3667aaeSNaresh Kumar Inna 	uint64_t paddr;
122a3667aaeSNaresh Kumar Inna 	int sreg = flq->un.fl.sreg;
123a3667aaeSNaresh Kumar Inna 	int n = flq->credits;
124a3667aaeSNaresh Kumar Inna 
125a3667aaeSNaresh Kumar Inna 	while (n--) {
126a3667aaeSNaresh Kumar Inna 		buf->len = sge->sge_fl_buf_size[sreg];
127c22b332dSChristoph Hellwig 		buf->vaddr = dma_alloc_coherent(&hw->pdev->dev, buf->len,
128c22b332dSChristoph Hellwig 						&buf->paddr, GFP_KERNEL);
129a3667aaeSNaresh Kumar Inna 		if (!buf->vaddr) {
130a3667aaeSNaresh Kumar Inna 			csio_err(hw, "Could only fill %d buffers!\n", n + 1);
131a3667aaeSNaresh Kumar Inna 			return -ENOMEM;
132a3667aaeSNaresh Kumar Inna 		}
133a3667aaeSNaresh Kumar Inna 
134a3667aaeSNaresh Kumar Inna 		paddr = buf->paddr | (sreg & 0xF);
135a3667aaeSNaresh Kumar Inna 
136a3667aaeSNaresh Kumar Inna 		*d++ = cpu_to_be64(paddr);
137a3667aaeSNaresh Kumar Inna 		buf++;
138a3667aaeSNaresh Kumar Inna 	}
139a3667aaeSNaresh Kumar Inna 
140a3667aaeSNaresh Kumar Inna 	return 0;
141a3667aaeSNaresh Kumar Inna }
142a3667aaeSNaresh Kumar Inna 
143a3667aaeSNaresh Kumar Inna /*
144a3667aaeSNaresh Kumar Inna  * csio_wr_update_fl -
145a3667aaeSNaresh Kumar Inna  * @hw: HW module.
146a3667aaeSNaresh Kumar Inna  * @flq: Freelist queue.
147a3667aaeSNaresh Kumar Inna  *
148a3667aaeSNaresh Kumar Inna  *
149a3667aaeSNaresh Kumar Inna  */
150a3667aaeSNaresh Kumar Inna static inline void
csio_wr_update_fl(struct csio_hw * hw,struct csio_q * flq,uint16_t n)151a3667aaeSNaresh Kumar Inna csio_wr_update_fl(struct csio_hw *hw, struct csio_q *flq, uint16_t n)
152a3667aaeSNaresh Kumar Inna {
153a3667aaeSNaresh Kumar Inna 
154a3667aaeSNaresh Kumar Inna 	flq->inc_idx += n;
155a3667aaeSNaresh Kumar Inna 	flq->pidx += n;
156a3667aaeSNaresh Kumar Inna 	if (unlikely(flq->pidx >= flq->credits))
157a3667aaeSNaresh Kumar Inna 		flq->pidx -= (uint16_t)flq->credits;
158a3667aaeSNaresh Kumar Inna 
159a3667aaeSNaresh Kumar Inna 	CSIO_INC_STATS(flq, n_flq_refill);
160a3667aaeSNaresh Kumar Inna }
161a3667aaeSNaresh Kumar Inna 
162a3667aaeSNaresh Kumar Inna /*
163a3667aaeSNaresh Kumar Inna  * csio_wr_alloc_q - Allocate a WR queue and initialize it.
164a3667aaeSNaresh Kumar Inna  * @hw: HW module
165a3667aaeSNaresh Kumar Inna  * @qsize: Size of the queue in bytes
166a3667aaeSNaresh Kumar Inna  * @wrsize: Since of WR in this queue, if fixed.
167a3667aaeSNaresh Kumar Inna  * @type: Type of queue (Ingress/Egress/Freelist)
168a3667aaeSNaresh Kumar Inna  * @owner: Module that owns this queue.
169a3667aaeSNaresh Kumar Inna  * @nflb: Number of freelist buffers for FL.
170a3667aaeSNaresh Kumar Inna  * @sreg: What is the FL buffer size register?
171a3667aaeSNaresh Kumar Inna  * @iq_int_handler: Ingress queue handler in INTx mode.
172a3667aaeSNaresh Kumar Inna  *
173a3667aaeSNaresh Kumar Inna  * This function allocates and sets up a queue for the caller
174a3667aaeSNaresh Kumar Inna  * of size qsize, aligned at the required boundary. This is subject to
175a3667aaeSNaresh Kumar Inna  * be free entries being available in the queue array. If one is found,
176a3667aaeSNaresh Kumar Inna  * it is initialized with the allocated queue, marked as being used (owner),
177a3667aaeSNaresh Kumar Inna  * and a handle returned to the caller in form of the queue's index
178a3667aaeSNaresh Kumar Inna  * into the q_arr array.
179a3667aaeSNaresh Kumar Inna  * If user has indicated a freelist (by specifying nflb > 0), create
180a3667aaeSNaresh Kumar Inna  * another queue (with its own index into q_arr) for the freelist. Allocate
181a3667aaeSNaresh Kumar Inna  * memory for DMA buffer metadata (vaddr, len etc). Save off the freelist
182a3667aaeSNaresh Kumar Inna  * idx in the ingress queue's flq.idx. This is how a Freelist is associated
183a3667aaeSNaresh Kumar Inna  * with its owning ingress queue.
184a3667aaeSNaresh Kumar Inna  */
185a3667aaeSNaresh Kumar Inna int
csio_wr_alloc_q(struct csio_hw * hw,uint32_t qsize,uint32_t wrsize,uint16_t type,void * owner,uint32_t nflb,int sreg,iq_handler_t iq_intx_handler)186a3667aaeSNaresh Kumar Inna csio_wr_alloc_q(struct csio_hw *hw, uint32_t qsize, uint32_t wrsize,
187a3667aaeSNaresh Kumar Inna 		uint16_t type, void *owner, uint32_t nflb, int sreg,
188a3667aaeSNaresh Kumar Inna 		iq_handler_t iq_intx_handler)
189a3667aaeSNaresh Kumar Inna {
190a3667aaeSNaresh Kumar Inna 	struct csio_wrm *wrm = csio_hw_to_wrm(hw);
191a3667aaeSNaresh Kumar Inna 	struct csio_q	*q, *flq;
192a3667aaeSNaresh Kumar Inna 	int		free_idx = wrm->free_qidx;
193a3667aaeSNaresh Kumar Inna 	int		ret_idx = free_idx;
194a3667aaeSNaresh Kumar Inna 	uint32_t	qsz;
195a3667aaeSNaresh Kumar Inna 	int flq_idx;
196a3667aaeSNaresh Kumar Inna 
197a3667aaeSNaresh Kumar Inna 	if (free_idx >= wrm->num_q) {
198a3667aaeSNaresh Kumar Inna 		csio_err(hw, "No more free queues.\n");
199a3667aaeSNaresh Kumar Inna 		return -1;
200a3667aaeSNaresh Kumar Inna 	}
201a3667aaeSNaresh Kumar Inna 
202a3667aaeSNaresh Kumar Inna 	switch (type) {
203a3667aaeSNaresh Kumar Inna 	case CSIO_EGRESS:
204a3667aaeSNaresh Kumar Inna 		qsz = ALIGN(qsize, CSIO_QCREDIT_SZ) + csio_wr_qstat_pgsz(hw);
205a3667aaeSNaresh Kumar Inna 		break;
206a3667aaeSNaresh Kumar Inna 	case CSIO_INGRESS:
207a3667aaeSNaresh Kumar Inna 		switch (wrsize) {
208a3667aaeSNaresh Kumar Inna 		case 16:
209a3667aaeSNaresh Kumar Inna 		case 32:
210a3667aaeSNaresh Kumar Inna 		case 64:
211a3667aaeSNaresh Kumar Inna 		case 128:
212a3667aaeSNaresh Kumar Inna 			break;
213a3667aaeSNaresh Kumar Inna 		default:
214a3667aaeSNaresh Kumar Inna 			csio_err(hw, "Invalid Ingress queue WR size:%d\n",
215a3667aaeSNaresh Kumar Inna 				    wrsize);
216a3667aaeSNaresh Kumar Inna 			return -1;
217a3667aaeSNaresh Kumar Inna 		}
218a3667aaeSNaresh Kumar Inna 
219a3667aaeSNaresh Kumar Inna 		/*
220a3667aaeSNaresh Kumar Inna 		 * Number of elements must be a multiple of 16
221a3667aaeSNaresh Kumar Inna 		 * So this includes status page size
222a3667aaeSNaresh Kumar Inna 		 */
223a3667aaeSNaresh Kumar Inna 		qsz = ALIGN(qsize/wrsize, 16) * wrsize;
224a3667aaeSNaresh Kumar Inna 
225a3667aaeSNaresh Kumar Inna 		break;
226a3667aaeSNaresh Kumar Inna 	case CSIO_FREELIST:
227a3667aaeSNaresh Kumar Inna 		qsz = ALIGN(qsize/wrsize, 8) * wrsize + csio_wr_qstat_pgsz(hw);
228a3667aaeSNaresh Kumar Inna 		break;
229a3667aaeSNaresh Kumar Inna 	default:
230a3667aaeSNaresh Kumar Inna 		csio_err(hw, "Invalid queue type: 0x%x\n", type);
231a3667aaeSNaresh Kumar Inna 		return -1;
232a3667aaeSNaresh Kumar Inna 	}
233a3667aaeSNaresh Kumar Inna 
234a3667aaeSNaresh Kumar Inna 	q = wrm->q_arr[free_idx];
235a3667aaeSNaresh Kumar Inna 
236750afb08SLuis Chamberlain 	q->vstart = dma_alloc_coherent(&hw->pdev->dev, qsz, &q->pstart,
237c22b332dSChristoph Hellwig 				       GFP_KERNEL);
238a3667aaeSNaresh Kumar Inna 	if (!q->vstart) {
239a3667aaeSNaresh Kumar Inna 		csio_err(hw,
240a3667aaeSNaresh Kumar Inna 			 "Failed to allocate DMA memory for "
241a3667aaeSNaresh Kumar Inna 			 "queue at id: %d size: %d\n", free_idx, qsize);
242a3667aaeSNaresh Kumar Inna 		return -1;
243a3667aaeSNaresh Kumar Inna 	}
244a3667aaeSNaresh Kumar Inna 
245a3667aaeSNaresh Kumar Inna 	q->type		= type;
246a3667aaeSNaresh Kumar Inna 	q->owner	= owner;
247a3667aaeSNaresh Kumar Inna 	q->pidx		= q->cidx = q->inc_idx = 0;
248a3667aaeSNaresh Kumar Inna 	q->size		= qsz;
249a3667aaeSNaresh Kumar Inna 	q->wr_sz	= wrsize;	/* If using fixed size WRs */
250a3667aaeSNaresh Kumar Inna 
251a3667aaeSNaresh Kumar Inna 	wrm->free_qidx++;
252a3667aaeSNaresh Kumar Inna 
253a3667aaeSNaresh Kumar Inna 	if (type == CSIO_INGRESS) {
254a3667aaeSNaresh Kumar Inna 		/* Since queue area is set to zero */
255a3667aaeSNaresh Kumar Inna 		q->un.iq.genbit	= 1;
256a3667aaeSNaresh Kumar Inna 
257a3667aaeSNaresh Kumar Inna 		/*
258a3667aaeSNaresh Kumar Inna 		 * Ingress queue status page size is always the size of
259a3667aaeSNaresh Kumar Inna 		 * the ingress queue entry.
260a3667aaeSNaresh Kumar Inna 		 */
261a3667aaeSNaresh Kumar Inna 		q->credits	= (qsz - q->wr_sz) / q->wr_sz;
262a3667aaeSNaresh Kumar Inna 		q->vwrap	= (void *)((uintptr_t)(q->vstart) + qsz
263a3667aaeSNaresh Kumar Inna 							- q->wr_sz);
264a3667aaeSNaresh Kumar Inna 
265a3667aaeSNaresh Kumar Inna 		/* Allocate memory for FL if requested */
266a3667aaeSNaresh Kumar Inna 		if (nflb > 0) {
267a3667aaeSNaresh Kumar Inna 			flq_idx = csio_wr_alloc_q(hw, nflb * sizeof(__be64),
268a3667aaeSNaresh Kumar Inna 						  sizeof(__be64), CSIO_FREELIST,
269a3667aaeSNaresh Kumar Inna 						  owner, 0, sreg, NULL);
270a3667aaeSNaresh Kumar Inna 			if (flq_idx == -1) {
271a3667aaeSNaresh Kumar Inna 				csio_err(hw,
272a3667aaeSNaresh Kumar Inna 					 "Failed to allocate FL queue"
273a3667aaeSNaresh Kumar Inna 					 " for IQ idx:%d\n", free_idx);
274a3667aaeSNaresh Kumar Inna 				return -1;
275a3667aaeSNaresh Kumar Inna 			}
276a3667aaeSNaresh Kumar Inna 
277a3667aaeSNaresh Kumar Inna 			/* Associate the new FL with the Ingress quue */
278a3667aaeSNaresh Kumar Inna 			q->un.iq.flq_idx = flq_idx;
279a3667aaeSNaresh Kumar Inna 
280a3667aaeSNaresh Kumar Inna 			flq = wrm->q_arr[q->un.iq.flq_idx];
2816396bb22SKees Cook 			flq->un.fl.bufs = kcalloc(flq->credits,
282a3667aaeSNaresh Kumar Inna 						  sizeof(struct csio_dma_buf),
283a3667aaeSNaresh Kumar Inna 						  GFP_KERNEL);
284a3667aaeSNaresh Kumar Inna 			if (!flq->un.fl.bufs) {
285a3667aaeSNaresh Kumar Inna 				csio_err(hw,
286a3667aaeSNaresh Kumar Inna 					 "Failed to allocate FL queue bufs"
287a3667aaeSNaresh Kumar Inna 					 " for IQ idx:%d\n", free_idx);
288a3667aaeSNaresh Kumar Inna 				return -1;
289a3667aaeSNaresh Kumar Inna 			}
290a3667aaeSNaresh Kumar Inna 
291a3667aaeSNaresh Kumar Inna 			flq->un.fl.packen = 0;
292a3667aaeSNaresh Kumar Inna 			flq->un.fl.offset = 0;
293a3667aaeSNaresh Kumar Inna 			flq->un.fl.sreg = sreg;
294a3667aaeSNaresh Kumar Inna 
295a3667aaeSNaresh Kumar Inna 			/* Fill up the free list buffers */
296a3667aaeSNaresh Kumar Inna 			if (csio_wr_fill_fl(hw, flq))
297a3667aaeSNaresh Kumar Inna 				return -1;
298a3667aaeSNaresh Kumar Inna 
299a3667aaeSNaresh Kumar Inna 			/*
300a3667aaeSNaresh Kumar Inna 			 * Make sure in a FLQ, atleast 1 credit (8 FL buffers)
301a3667aaeSNaresh Kumar Inna 			 * remains unpopulated,otherwise HW thinks
302a3667aaeSNaresh Kumar Inna 			 * FLQ is empty.
303a3667aaeSNaresh Kumar Inna 			 */
304a3667aaeSNaresh Kumar Inna 			flq->pidx = flq->inc_idx = flq->credits - 8;
305a3667aaeSNaresh Kumar Inna 		} else {
306a3667aaeSNaresh Kumar Inna 			q->un.iq.flq_idx = -1;
307a3667aaeSNaresh Kumar Inna 		}
308a3667aaeSNaresh Kumar Inna 
309a3667aaeSNaresh Kumar Inna 		/* Associate the IQ INTx handler. */
310a3667aaeSNaresh Kumar Inna 		q->un.iq.iq_intx_handler = iq_intx_handler;
311a3667aaeSNaresh Kumar Inna 
312a3667aaeSNaresh Kumar Inna 		csio_q_iqid(hw, ret_idx) = CSIO_MAX_QID;
313a3667aaeSNaresh Kumar Inna 
314a3667aaeSNaresh Kumar Inna 	} else if (type == CSIO_EGRESS) {
315a3667aaeSNaresh Kumar Inna 		q->credits = (qsz - csio_wr_qstat_pgsz(hw)) / CSIO_QCREDIT_SZ;
316a3667aaeSNaresh Kumar Inna 		q->vwrap   = (void *)((uintptr_t)(q->vstart) + qsz
317a3667aaeSNaresh Kumar Inna 						- csio_wr_qstat_pgsz(hw));
318a3667aaeSNaresh Kumar Inna 		csio_q_eqid(hw, ret_idx) = CSIO_MAX_QID;
319a3667aaeSNaresh Kumar Inna 	} else { /* Freelist */
320a3667aaeSNaresh Kumar Inna 		q->credits = (qsz - csio_wr_qstat_pgsz(hw)) / sizeof(__be64);
321a3667aaeSNaresh Kumar Inna 		q->vwrap   = (void *)((uintptr_t)(q->vstart) + qsz
322a3667aaeSNaresh Kumar Inna 						- csio_wr_qstat_pgsz(hw));
323a3667aaeSNaresh Kumar Inna 		csio_q_flid(hw, ret_idx) = CSIO_MAX_QID;
324a3667aaeSNaresh Kumar Inna 	}
325a3667aaeSNaresh Kumar Inna 
326a3667aaeSNaresh Kumar Inna 	return ret_idx;
327a3667aaeSNaresh Kumar Inna }
328a3667aaeSNaresh Kumar Inna 
329a3667aaeSNaresh Kumar Inna /*
330a3667aaeSNaresh Kumar Inna  * csio_wr_iq_create_rsp - Response handler for IQ creation.
331a3667aaeSNaresh Kumar Inna  * @hw: The HW module.
332a3667aaeSNaresh Kumar Inna  * @mbp: Mailbox.
333a3667aaeSNaresh Kumar Inna  * @iq_idx: Ingress queue that got created.
334a3667aaeSNaresh Kumar Inna  *
335a3667aaeSNaresh Kumar Inna  * Handle FW_IQ_CMD mailbox completion. Save off the assigned IQ/FL ids.
336a3667aaeSNaresh Kumar Inna  */
337a3667aaeSNaresh Kumar Inna static int
csio_wr_iq_create_rsp(struct csio_hw * hw,struct csio_mb * mbp,int iq_idx)338a3667aaeSNaresh Kumar Inna csio_wr_iq_create_rsp(struct csio_hw *hw, struct csio_mb *mbp, int iq_idx)
339a3667aaeSNaresh Kumar Inna {
340a3667aaeSNaresh Kumar Inna 	struct csio_iq_params iqp;
341a3667aaeSNaresh Kumar Inna 	enum fw_retval retval;
342a3667aaeSNaresh Kumar Inna 	uint32_t iq_id;
343a3667aaeSNaresh Kumar Inna 	int flq_idx;
344a3667aaeSNaresh Kumar Inna 
345a3667aaeSNaresh Kumar Inna 	memset(&iqp, 0, sizeof(struct csio_iq_params));
346a3667aaeSNaresh Kumar Inna 
347a3667aaeSNaresh Kumar Inna 	csio_mb_iq_alloc_write_rsp(hw, mbp, &retval, &iqp);
348a3667aaeSNaresh Kumar Inna 
349a3667aaeSNaresh Kumar Inna 	if (retval != FW_SUCCESS) {
350a3667aaeSNaresh Kumar Inna 		csio_err(hw, "IQ cmd returned 0x%x!\n", retval);
351a3667aaeSNaresh Kumar Inna 		mempool_free(mbp, hw->mb_mempool);
352a3667aaeSNaresh Kumar Inna 		return -EINVAL;
353a3667aaeSNaresh Kumar Inna 	}
354a3667aaeSNaresh Kumar Inna 
355a3667aaeSNaresh Kumar Inna 	csio_q_iqid(hw, iq_idx)		= iqp.iqid;
356a3667aaeSNaresh Kumar Inna 	csio_q_physiqid(hw, iq_idx)	= iqp.physiqid;
357a3667aaeSNaresh Kumar Inna 	csio_q_pidx(hw, iq_idx)		= csio_q_cidx(hw, iq_idx) = 0;
358a3667aaeSNaresh Kumar Inna 	csio_q_inc_idx(hw, iq_idx)	= 0;
359a3667aaeSNaresh Kumar Inna 
360a3667aaeSNaresh Kumar Inna 	/* Actual iq-id. */
361a3667aaeSNaresh Kumar Inna 	iq_id = iqp.iqid - hw->wrm.fw_iq_start;
362a3667aaeSNaresh Kumar Inna 
363a3667aaeSNaresh Kumar Inna 	/* Set the iq-id to iq map table. */
364a3667aaeSNaresh Kumar Inna 	if (iq_id >= CSIO_MAX_IQ) {
365a3667aaeSNaresh Kumar Inna 		csio_err(hw,
366a3667aaeSNaresh Kumar Inna 			 "Exceeding MAX_IQ(%d) supported!"
367a3667aaeSNaresh Kumar Inna 			 " iqid:%d rel_iqid:%d FW iq_start:%d\n",
368a3667aaeSNaresh Kumar Inna 			 CSIO_MAX_IQ, iq_id, iqp.iqid, hw->wrm.fw_iq_start);
369a3667aaeSNaresh Kumar Inna 		mempool_free(mbp, hw->mb_mempool);
370a3667aaeSNaresh Kumar Inna 		return -EINVAL;
371a3667aaeSNaresh Kumar Inna 	}
372a3667aaeSNaresh Kumar Inna 	csio_q_set_intr_map(hw, iq_idx, iq_id);
373a3667aaeSNaresh Kumar Inna 
374a3667aaeSNaresh Kumar Inna 	/*
375a3667aaeSNaresh Kumar Inna 	 * During FW_IQ_CMD, FW sets interrupt_sent bit to 1 in the SGE
376a3667aaeSNaresh Kumar Inna 	 * ingress context of this queue. This will block interrupts to
377a3667aaeSNaresh Kumar Inna 	 * this queue until the next GTS write. Therefore, we do a
378a3667aaeSNaresh Kumar Inna 	 * 0-cidx increment GTS write for this queue just to clear the
379a3667aaeSNaresh Kumar Inna 	 * interrupt_sent bit. This will re-enable interrupts to this
380a3667aaeSNaresh Kumar Inna 	 * queue.
381a3667aaeSNaresh Kumar Inna 	 */
382a3667aaeSNaresh Kumar Inna 	csio_wr_sge_intr_enable(hw, iqp.physiqid);
383a3667aaeSNaresh Kumar Inna 
384a3667aaeSNaresh Kumar Inna 	flq_idx = csio_q_iq_flq_idx(hw, iq_idx);
385a3667aaeSNaresh Kumar Inna 	if (flq_idx != -1) {
386a3667aaeSNaresh Kumar Inna 		struct csio_q *flq = hw->wrm.q_arr[flq_idx];
387a3667aaeSNaresh Kumar Inna 
388a3667aaeSNaresh Kumar Inna 		csio_q_flid(hw, flq_idx) = iqp.fl0id;
389a3667aaeSNaresh Kumar Inna 		csio_q_cidx(hw, flq_idx) = 0;
390a3667aaeSNaresh Kumar Inna 		csio_q_pidx(hw, flq_idx)    = csio_q_credits(hw, flq_idx) - 8;
391a3667aaeSNaresh Kumar Inna 		csio_q_inc_idx(hw, flq_idx) = csio_q_credits(hw, flq_idx) - 8;
392a3667aaeSNaresh Kumar Inna 
393a3667aaeSNaresh Kumar Inna 		/* Now update SGE about the buffers allocated during init */
394a3667aaeSNaresh Kumar Inna 		csio_wr_ring_fldb(hw, flq);
395a3667aaeSNaresh Kumar Inna 	}
396a3667aaeSNaresh Kumar Inna 
397a3667aaeSNaresh Kumar Inna 	mempool_free(mbp, hw->mb_mempool);
398a3667aaeSNaresh Kumar Inna 
399a3667aaeSNaresh Kumar Inna 	return 0;
400a3667aaeSNaresh Kumar Inna }
401a3667aaeSNaresh Kumar Inna 
402a3667aaeSNaresh Kumar Inna /*
403a3667aaeSNaresh Kumar Inna  * csio_wr_iq_create - Configure an Ingress queue with FW.
404a3667aaeSNaresh Kumar Inna  * @hw: The HW module.
405a3667aaeSNaresh Kumar Inna  * @priv: Private data object.
406a3667aaeSNaresh Kumar Inna  * @iq_idx: Ingress queue index in the WR module.
407a3667aaeSNaresh Kumar Inna  * @vec: MSIX vector.
408a3667aaeSNaresh Kumar Inna  * @portid: PCIE Channel to be associated with this queue.
409a3667aaeSNaresh Kumar Inna  * @async: Is this a FW asynchronous message handling queue?
410a3667aaeSNaresh Kumar Inna  * @cbfn: Completion callback.
411a3667aaeSNaresh Kumar Inna  *
412a3667aaeSNaresh Kumar Inna  * This API configures an ingress queue with FW by issuing a FW_IQ_CMD mailbox
413a3667aaeSNaresh Kumar Inna  * with alloc/write bits set.
414a3667aaeSNaresh Kumar Inna  */
415a3667aaeSNaresh Kumar Inna int
csio_wr_iq_create(struct csio_hw * hw,void * priv,int iq_idx,uint32_t vec,uint8_t portid,bool async,void (* cbfn)(struct csio_hw *,struct csio_mb *))416a3667aaeSNaresh Kumar Inna csio_wr_iq_create(struct csio_hw *hw, void *priv, int iq_idx,
417a3667aaeSNaresh Kumar Inna 		  uint32_t vec, uint8_t portid, bool async,
418a3667aaeSNaresh Kumar Inna 		  void (*cbfn) (struct csio_hw *, struct csio_mb *))
419a3667aaeSNaresh Kumar Inna {
420a3667aaeSNaresh Kumar Inna 	struct csio_mb  *mbp;
421a3667aaeSNaresh Kumar Inna 	struct csio_iq_params iqp;
422a3667aaeSNaresh Kumar Inna 	int flq_idx;
423a3667aaeSNaresh Kumar Inna 
424a3667aaeSNaresh Kumar Inna 	memset(&iqp, 0, sizeof(struct csio_iq_params));
425a3667aaeSNaresh Kumar Inna 	csio_q_portid(hw, iq_idx) = portid;
426a3667aaeSNaresh Kumar Inna 
427a3667aaeSNaresh Kumar Inna 	mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
428a3667aaeSNaresh Kumar Inna 	if (!mbp) {
429a3667aaeSNaresh Kumar Inna 		csio_err(hw, "IQ command out of memory!\n");
430a3667aaeSNaresh Kumar Inna 		return -ENOMEM;
431a3667aaeSNaresh Kumar Inna 	}
432a3667aaeSNaresh Kumar Inna 
433a3667aaeSNaresh Kumar Inna 	switch (hw->intr_mode) {
434a3667aaeSNaresh Kumar Inna 	case CSIO_IM_INTX:
435a3667aaeSNaresh Kumar Inna 	case CSIO_IM_MSI:
436a3667aaeSNaresh Kumar Inna 		/* For interrupt forwarding queue only */
437a3667aaeSNaresh Kumar Inna 		if (hw->intr_iq_idx == iq_idx)
438a3667aaeSNaresh Kumar Inna 			iqp.iqandst	= X_INTERRUPTDESTINATION_PCIE;
439a3667aaeSNaresh Kumar Inna 		else
440a3667aaeSNaresh Kumar Inna 			iqp.iqandst	= X_INTERRUPTDESTINATION_IQ;
441a3667aaeSNaresh Kumar Inna 		iqp.iqandstindex	=
442a3667aaeSNaresh Kumar Inna 			csio_q_physiqid(hw, hw->intr_iq_idx);
443a3667aaeSNaresh Kumar Inna 		break;
444a3667aaeSNaresh Kumar Inna 	case CSIO_IM_MSIX:
445a3667aaeSNaresh Kumar Inna 		iqp.iqandst		= X_INTERRUPTDESTINATION_PCIE;
446a3667aaeSNaresh Kumar Inna 		iqp.iqandstindex	= (uint16_t)vec;
447a3667aaeSNaresh Kumar Inna 		break;
448a3667aaeSNaresh Kumar Inna 	case CSIO_IM_NONE:
449a3667aaeSNaresh Kumar Inna 		mempool_free(mbp, hw->mb_mempool);
450a3667aaeSNaresh Kumar Inna 		return -EINVAL;
451a3667aaeSNaresh Kumar Inna 	}
452a3667aaeSNaresh Kumar Inna 
453a3667aaeSNaresh Kumar Inna 	/* Pass in the ingress queue cmd parameters */
454a3667aaeSNaresh Kumar Inna 	iqp.pfn			= hw->pfn;
455a3667aaeSNaresh Kumar Inna 	iqp.vfn			= 0;
456a3667aaeSNaresh Kumar Inna 	iqp.iq_start		= 1;
457a3667aaeSNaresh Kumar Inna 	iqp.viid		= 0;
458a3667aaeSNaresh Kumar Inna 	iqp.type		= FW_IQ_TYPE_FL_INT_CAP;
459a3667aaeSNaresh Kumar Inna 	iqp.iqasynch		= async;
460a3667aaeSNaresh Kumar Inna 	if (csio_intr_coalesce_cnt)
461a3667aaeSNaresh Kumar Inna 		iqp.iqanus	= X_UPDATESCHEDULING_COUNTER_OPTTIMER;
462a3667aaeSNaresh Kumar Inna 	else
463a3667aaeSNaresh Kumar Inna 		iqp.iqanus	= X_UPDATESCHEDULING_TIMER;
464a3667aaeSNaresh Kumar Inna 	iqp.iqanud		= X_UPDATEDELIVERY_INTERRUPT;
465a3667aaeSNaresh Kumar Inna 	iqp.iqpciech		= portid;
466a3667aaeSNaresh Kumar Inna 	iqp.iqintcntthresh	= (uint8_t)csio_sge_thresh_reg;
467a3667aaeSNaresh Kumar Inna 
468a3667aaeSNaresh Kumar Inna 	switch (csio_q_wr_sz(hw, iq_idx)) {
469a3667aaeSNaresh Kumar Inna 	case 16:
470a3667aaeSNaresh Kumar Inna 		iqp.iqesize = 0; break;
471a3667aaeSNaresh Kumar Inna 	case 32:
472a3667aaeSNaresh Kumar Inna 		iqp.iqesize = 1; break;
473a3667aaeSNaresh Kumar Inna 	case 64:
474a3667aaeSNaresh Kumar Inna 		iqp.iqesize = 2; break;
475a3667aaeSNaresh Kumar Inna 	case 128:
476a3667aaeSNaresh Kumar Inna 		iqp.iqesize = 3; break;
477a3667aaeSNaresh Kumar Inna 	}
478a3667aaeSNaresh Kumar Inna 
479a3667aaeSNaresh Kumar Inna 	iqp.iqsize		= csio_q_size(hw, iq_idx) /
480a3667aaeSNaresh Kumar Inna 						csio_q_wr_sz(hw, iq_idx);
481a3667aaeSNaresh Kumar Inna 	iqp.iqaddr		= csio_q_pstart(hw, iq_idx);
482a3667aaeSNaresh Kumar Inna 
483a3667aaeSNaresh Kumar Inna 	flq_idx = csio_q_iq_flq_idx(hw, iq_idx);
484a3667aaeSNaresh Kumar Inna 	if (flq_idx != -1) {
4854bbd458eSVarun Prakash 		enum chip_type chip = CHELSIO_CHIP_VERSION(hw->chip_id);
486a3667aaeSNaresh Kumar Inna 		struct csio_q *flq = hw->wrm.q_arr[flq_idx];
487a3667aaeSNaresh Kumar Inna 
488a3667aaeSNaresh Kumar Inna 		iqp.fl0paden	= 1;
489a3667aaeSNaresh Kumar Inna 		iqp.fl0packen	= flq->un.fl.packen ? 1 : 0;
490a3667aaeSNaresh Kumar Inna 		iqp.fl0fbmin	= X_FETCHBURSTMIN_64B;
4914bbd458eSVarun Prakash 		iqp.fl0fbmax	= ((chip == CHELSIO_T5) ?
4924bbd458eSVarun Prakash 				  X_FETCHBURSTMAX_512B : X_FETCHBURSTMAX_256B);
493a3667aaeSNaresh Kumar Inna 		iqp.fl0size	= csio_q_size(hw, flq_idx) / CSIO_QCREDIT_SZ;
494a3667aaeSNaresh Kumar Inna 		iqp.fl0addr	= csio_q_pstart(hw, flq_idx);
495a3667aaeSNaresh Kumar Inna 	}
496a3667aaeSNaresh Kumar Inna 
497a3667aaeSNaresh Kumar Inna 	csio_mb_iq_alloc_write(hw, mbp, priv, CSIO_MB_DEFAULT_TMO, &iqp, cbfn);
498a3667aaeSNaresh Kumar Inna 
499a3667aaeSNaresh Kumar Inna 	if (csio_mb_issue(hw, mbp)) {
500a3667aaeSNaresh Kumar Inna 		csio_err(hw, "Issue of IQ cmd failed!\n");
501a3667aaeSNaresh Kumar Inna 		mempool_free(mbp, hw->mb_mempool);
502a3667aaeSNaresh Kumar Inna 		return -EINVAL;
503a3667aaeSNaresh Kumar Inna 	}
504a3667aaeSNaresh Kumar Inna 
505a3667aaeSNaresh Kumar Inna 	if (cbfn != NULL)
506a3667aaeSNaresh Kumar Inna 		return 0;
507a3667aaeSNaresh Kumar Inna 
508a3667aaeSNaresh Kumar Inna 	return csio_wr_iq_create_rsp(hw, mbp, iq_idx);
509a3667aaeSNaresh Kumar Inna }
510a3667aaeSNaresh Kumar Inna 
511a3667aaeSNaresh Kumar Inna /*
512a3667aaeSNaresh Kumar Inna  * csio_wr_eq_create_rsp - Response handler for EQ creation.
513a3667aaeSNaresh Kumar Inna  * @hw: The HW module.
514a3667aaeSNaresh Kumar Inna  * @mbp: Mailbox.
515a3667aaeSNaresh Kumar Inna  * @eq_idx: Egress queue that got created.
516a3667aaeSNaresh Kumar Inna  *
517a3667aaeSNaresh Kumar Inna  * Handle FW_EQ_OFLD_CMD mailbox completion. Save off the assigned EQ ids.
518a3667aaeSNaresh Kumar Inna  */
519a3667aaeSNaresh Kumar Inna static int
csio_wr_eq_cfg_rsp(struct csio_hw * hw,struct csio_mb * mbp,int eq_idx)520a3667aaeSNaresh Kumar Inna csio_wr_eq_cfg_rsp(struct csio_hw *hw, struct csio_mb *mbp, int eq_idx)
521a3667aaeSNaresh Kumar Inna {
522a3667aaeSNaresh Kumar Inna 	struct csio_eq_params eqp;
523a3667aaeSNaresh Kumar Inna 	enum fw_retval retval;
524a3667aaeSNaresh Kumar Inna 
525a3667aaeSNaresh Kumar Inna 	memset(&eqp, 0, sizeof(struct csio_eq_params));
526a3667aaeSNaresh Kumar Inna 
527a3667aaeSNaresh Kumar Inna 	csio_mb_eq_ofld_alloc_write_rsp(hw, mbp, &retval, &eqp);
528a3667aaeSNaresh Kumar Inna 
529a3667aaeSNaresh Kumar Inna 	if (retval != FW_SUCCESS) {
530a3667aaeSNaresh Kumar Inna 		csio_err(hw, "EQ OFLD cmd returned 0x%x!\n", retval);
531a3667aaeSNaresh Kumar Inna 		mempool_free(mbp, hw->mb_mempool);
532a3667aaeSNaresh Kumar Inna 		return -EINVAL;
533a3667aaeSNaresh Kumar Inna 	}
534a3667aaeSNaresh Kumar Inna 
535a3667aaeSNaresh Kumar Inna 	csio_q_eqid(hw, eq_idx)	= (uint16_t)eqp.eqid;
536a3667aaeSNaresh Kumar Inna 	csio_q_physeqid(hw, eq_idx) = (uint16_t)eqp.physeqid;
537a3667aaeSNaresh Kumar Inna 	csio_q_pidx(hw, eq_idx)	= csio_q_cidx(hw, eq_idx) = 0;
538a3667aaeSNaresh Kumar Inna 	csio_q_inc_idx(hw, eq_idx) = 0;
539a3667aaeSNaresh Kumar Inna 
540a3667aaeSNaresh Kumar Inna 	mempool_free(mbp, hw->mb_mempool);
541a3667aaeSNaresh Kumar Inna 
542a3667aaeSNaresh Kumar Inna 	return 0;
543a3667aaeSNaresh Kumar Inna }
544a3667aaeSNaresh Kumar Inna 
545a3667aaeSNaresh Kumar Inna /*
546a3667aaeSNaresh Kumar Inna  * csio_wr_eq_create - Configure an Egress queue with FW.
547a3667aaeSNaresh Kumar Inna  * @hw: HW module.
548a3667aaeSNaresh Kumar Inna  * @priv: Private data.
549a3667aaeSNaresh Kumar Inna  * @eq_idx: Egress queue index in the WR module.
550a3667aaeSNaresh Kumar Inna  * @iq_idx: Associated ingress queue index.
551a3667aaeSNaresh Kumar Inna  * @cbfn: Completion callback.
552a3667aaeSNaresh Kumar Inna  *
553a3667aaeSNaresh Kumar Inna  * This API configures a offload egress queue with FW by issuing a
554a3667aaeSNaresh Kumar Inna  * FW_EQ_OFLD_CMD  (with alloc + write ) mailbox.
555a3667aaeSNaresh Kumar Inna  */
556a3667aaeSNaresh Kumar Inna int
csio_wr_eq_create(struct csio_hw * hw,void * priv,int eq_idx,int iq_idx,uint8_t portid,void (* cbfn)(struct csio_hw *,struct csio_mb *))557a3667aaeSNaresh Kumar Inna csio_wr_eq_create(struct csio_hw *hw, void *priv, int eq_idx,
558a3667aaeSNaresh Kumar Inna 		  int iq_idx, uint8_t portid,
559a3667aaeSNaresh Kumar Inna 		  void (*cbfn) (struct csio_hw *, struct csio_mb *))
560a3667aaeSNaresh Kumar Inna {
561a3667aaeSNaresh Kumar Inna 	struct csio_mb  *mbp;
562a3667aaeSNaresh Kumar Inna 	struct csio_eq_params eqp;
563a3667aaeSNaresh Kumar Inna 
564a3667aaeSNaresh Kumar Inna 	memset(&eqp, 0, sizeof(struct csio_eq_params));
565a3667aaeSNaresh Kumar Inna 
566a3667aaeSNaresh Kumar Inna 	mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
567a3667aaeSNaresh Kumar Inna 	if (!mbp) {
568a3667aaeSNaresh Kumar Inna 		csio_err(hw, "EQ command out of memory!\n");
569a3667aaeSNaresh Kumar Inna 		return -ENOMEM;
570a3667aaeSNaresh Kumar Inna 	}
571a3667aaeSNaresh Kumar Inna 
572a3667aaeSNaresh Kumar Inna 	eqp.pfn			= hw->pfn;
573a3667aaeSNaresh Kumar Inna 	eqp.vfn			= 0;
574a3667aaeSNaresh Kumar Inna 	eqp.eqstart		= 1;
575a3667aaeSNaresh Kumar Inna 	eqp.hostfcmode		= X_HOSTFCMODE_STATUS_PAGE;
576a3667aaeSNaresh Kumar Inna 	eqp.iqid		= csio_q_iqid(hw, iq_idx);
577a3667aaeSNaresh Kumar Inna 	eqp.fbmin		= X_FETCHBURSTMIN_64B;
578a3667aaeSNaresh Kumar Inna 	eqp.fbmax		= X_FETCHBURSTMAX_512B;
579a3667aaeSNaresh Kumar Inna 	eqp.cidxfthresh		= 0;
580a3667aaeSNaresh Kumar Inna 	eqp.pciechn		= portid;
581a3667aaeSNaresh Kumar Inna 	eqp.eqsize		= csio_q_size(hw, eq_idx) / CSIO_QCREDIT_SZ;
582a3667aaeSNaresh Kumar Inna 	eqp.eqaddr		= csio_q_pstart(hw, eq_idx);
583a3667aaeSNaresh Kumar Inna 
584a3667aaeSNaresh Kumar Inna 	csio_mb_eq_ofld_alloc_write(hw, mbp, priv, CSIO_MB_DEFAULT_TMO,
585a3667aaeSNaresh Kumar Inna 				    &eqp, cbfn);
586a3667aaeSNaresh Kumar Inna 
587a3667aaeSNaresh Kumar Inna 	if (csio_mb_issue(hw, mbp)) {
588a3667aaeSNaresh Kumar Inna 		csio_err(hw, "Issue of EQ OFLD cmd failed!\n");
589a3667aaeSNaresh Kumar Inna 		mempool_free(mbp, hw->mb_mempool);
590a3667aaeSNaresh Kumar Inna 		return -EINVAL;
591a3667aaeSNaresh Kumar Inna 	}
592a3667aaeSNaresh Kumar Inna 
593a3667aaeSNaresh Kumar Inna 	if (cbfn != NULL)
594a3667aaeSNaresh Kumar Inna 		return 0;
595a3667aaeSNaresh Kumar Inna 
596a3667aaeSNaresh Kumar Inna 	return csio_wr_eq_cfg_rsp(hw, mbp, eq_idx);
597a3667aaeSNaresh Kumar Inna }
598a3667aaeSNaresh Kumar Inna 
599a3667aaeSNaresh Kumar Inna /*
600a3667aaeSNaresh Kumar Inna  * csio_wr_iq_destroy_rsp - Response handler for IQ removal.
601a3667aaeSNaresh Kumar Inna  * @hw: The HW module.
602a3667aaeSNaresh Kumar Inna  * @mbp: Mailbox.
603a3667aaeSNaresh Kumar Inna  * @iq_idx: Ingress queue that was freed.
604a3667aaeSNaresh Kumar Inna  *
605a3667aaeSNaresh Kumar Inna  * Handle FW_IQ_CMD (free) mailbox completion.
606a3667aaeSNaresh Kumar Inna  */
607a3667aaeSNaresh Kumar Inna static int
csio_wr_iq_destroy_rsp(struct csio_hw * hw,struct csio_mb * mbp,int iq_idx)608a3667aaeSNaresh Kumar Inna csio_wr_iq_destroy_rsp(struct csio_hw *hw, struct csio_mb *mbp, int iq_idx)
609a3667aaeSNaresh Kumar Inna {
610a3667aaeSNaresh Kumar Inna 	enum fw_retval retval = csio_mb_fw_retval(mbp);
611a3667aaeSNaresh Kumar Inna 	int rv = 0;
612a3667aaeSNaresh Kumar Inna 
613a3667aaeSNaresh Kumar Inna 	if (retval != FW_SUCCESS)
614a3667aaeSNaresh Kumar Inna 		rv = -EINVAL;
615a3667aaeSNaresh Kumar Inna 
616a3667aaeSNaresh Kumar Inna 	mempool_free(mbp, hw->mb_mempool);
617a3667aaeSNaresh Kumar Inna 
618a3667aaeSNaresh Kumar Inna 	return rv;
619a3667aaeSNaresh Kumar Inna }
620a3667aaeSNaresh Kumar Inna 
621a3667aaeSNaresh Kumar Inna /*
622a3667aaeSNaresh Kumar Inna  * csio_wr_iq_destroy - Free an ingress queue.
623a3667aaeSNaresh Kumar Inna  * @hw: The HW module.
624a3667aaeSNaresh Kumar Inna  * @priv: Private data object.
625a3667aaeSNaresh Kumar Inna  * @iq_idx: Ingress queue index to destroy
626a3667aaeSNaresh Kumar Inna  * @cbfn: Completion callback.
627a3667aaeSNaresh Kumar Inna  *
628a3667aaeSNaresh Kumar Inna  * This API frees an ingress queue by issuing the FW_IQ_CMD
629a3667aaeSNaresh Kumar Inna  * with the free bit set.
630a3667aaeSNaresh Kumar Inna  */
631a3667aaeSNaresh Kumar Inna static int
csio_wr_iq_destroy(struct csio_hw * hw,void * priv,int iq_idx,void (* cbfn)(struct csio_hw *,struct csio_mb *))632a3667aaeSNaresh Kumar Inna csio_wr_iq_destroy(struct csio_hw *hw, void *priv, int iq_idx,
633a3667aaeSNaresh Kumar Inna 		   void (*cbfn)(struct csio_hw *, struct csio_mb *))
634a3667aaeSNaresh Kumar Inna {
635a3667aaeSNaresh Kumar Inna 	int rv = 0;
636a3667aaeSNaresh Kumar Inna 	struct csio_mb  *mbp;
637a3667aaeSNaresh Kumar Inna 	struct csio_iq_params iqp;
638a3667aaeSNaresh Kumar Inna 	int flq_idx;
639a3667aaeSNaresh Kumar Inna 
640a3667aaeSNaresh Kumar Inna 	memset(&iqp, 0, sizeof(struct csio_iq_params));
641a3667aaeSNaresh Kumar Inna 
642a3667aaeSNaresh Kumar Inna 	mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
643a3667aaeSNaresh Kumar Inna 	if (!mbp)
644a3667aaeSNaresh Kumar Inna 		return -ENOMEM;
645a3667aaeSNaresh Kumar Inna 
646a3667aaeSNaresh Kumar Inna 	iqp.pfn		= hw->pfn;
647a3667aaeSNaresh Kumar Inna 	iqp.vfn		= 0;
648a3667aaeSNaresh Kumar Inna 	iqp.iqid	= csio_q_iqid(hw, iq_idx);
649a3667aaeSNaresh Kumar Inna 	iqp.type	= FW_IQ_TYPE_FL_INT_CAP;
650a3667aaeSNaresh Kumar Inna 
651a3667aaeSNaresh Kumar Inna 	flq_idx = csio_q_iq_flq_idx(hw, iq_idx);
652a3667aaeSNaresh Kumar Inna 	if (flq_idx != -1)
653a3667aaeSNaresh Kumar Inna 		iqp.fl0id = csio_q_flid(hw, flq_idx);
654a3667aaeSNaresh Kumar Inna 	else
655a3667aaeSNaresh Kumar Inna 		iqp.fl0id = 0xFFFF;
656a3667aaeSNaresh Kumar Inna 
657a3667aaeSNaresh Kumar Inna 	iqp.fl1id = 0xFFFF;
658a3667aaeSNaresh Kumar Inna 
659a3667aaeSNaresh Kumar Inna 	csio_mb_iq_free(hw, mbp, priv, CSIO_MB_DEFAULT_TMO, &iqp, cbfn);
660a3667aaeSNaresh Kumar Inna 
661a3667aaeSNaresh Kumar Inna 	rv = csio_mb_issue(hw, mbp);
662a3667aaeSNaresh Kumar Inna 	if (rv != 0) {
663a3667aaeSNaresh Kumar Inna 		mempool_free(mbp, hw->mb_mempool);
664a3667aaeSNaresh Kumar Inna 		return rv;
665a3667aaeSNaresh Kumar Inna 	}
666a3667aaeSNaresh Kumar Inna 
667a3667aaeSNaresh Kumar Inna 	if (cbfn != NULL)
668a3667aaeSNaresh Kumar Inna 		return 0;
669a3667aaeSNaresh Kumar Inna 
670a3667aaeSNaresh Kumar Inna 	return csio_wr_iq_destroy_rsp(hw, mbp, iq_idx);
671a3667aaeSNaresh Kumar Inna }
672a3667aaeSNaresh Kumar Inna 
673a3667aaeSNaresh Kumar Inna /*
674a3667aaeSNaresh Kumar Inna  * csio_wr_eq_destroy_rsp - Response handler for OFLD EQ creation.
675a3667aaeSNaresh Kumar Inna  * @hw: The HW module.
676a3667aaeSNaresh Kumar Inna  * @mbp: Mailbox.
677a3667aaeSNaresh Kumar Inna  * @eq_idx: Egress queue that was freed.
678a3667aaeSNaresh Kumar Inna  *
679a3667aaeSNaresh Kumar Inna  * Handle FW_OFLD_EQ_CMD (free) mailbox completion.
680a3667aaeSNaresh Kumar Inna  */
681a3667aaeSNaresh Kumar Inna static int
csio_wr_eq_destroy_rsp(struct csio_hw * hw,struct csio_mb * mbp,int eq_idx)682a3667aaeSNaresh Kumar Inna csio_wr_eq_destroy_rsp(struct csio_hw *hw, struct csio_mb *mbp, int eq_idx)
683a3667aaeSNaresh Kumar Inna {
684a3667aaeSNaresh Kumar Inna 	enum fw_retval retval = csio_mb_fw_retval(mbp);
685a3667aaeSNaresh Kumar Inna 	int rv = 0;
686a3667aaeSNaresh Kumar Inna 
687a3667aaeSNaresh Kumar Inna 	if (retval != FW_SUCCESS)
688a3667aaeSNaresh Kumar Inna 		rv = -EINVAL;
689a3667aaeSNaresh Kumar Inna 
690a3667aaeSNaresh Kumar Inna 	mempool_free(mbp, hw->mb_mempool);
691a3667aaeSNaresh Kumar Inna 
692a3667aaeSNaresh Kumar Inna 	return rv;
693a3667aaeSNaresh Kumar Inna }
694a3667aaeSNaresh Kumar Inna 
695a3667aaeSNaresh Kumar Inna /*
696a3667aaeSNaresh Kumar Inna  * csio_wr_eq_destroy - Free an Egress queue.
697a3667aaeSNaresh Kumar Inna  * @hw: The HW module.
698a3667aaeSNaresh Kumar Inna  * @priv: Private data object.
699a3667aaeSNaresh Kumar Inna  * @eq_idx: Egress queue index to destroy
700a3667aaeSNaresh Kumar Inna  * @cbfn: Completion callback.
701a3667aaeSNaresh Kumar Inna  *
702a3667aaeSNaresh Kumar Inna  * This API frees an Egress queue by issuing the FW_EQ_OFLD_CMD
703a3667aaeSNaresh Kumar Inna  * with the free bit set.
704a3667aaeSNaresh Kumar Inna  */
705a3667aaeSNaresh Kumar Inna static int
csio_wr_eq_destroy(struct csio_hw * hw,void * priv,int eq_idx,void (* cbfn)(struct csio_hw *,struct csio_mb *))706a3667aaeSNaresh Kumar Inna csio_wr_eq_destroy(struct csio_hw *hw, void *priv, int eq_idx,
707a3667aaeSNaresh Kumar Inna 		   void (*cbfn) (struct csio_hw *, struct csio_mb *))
708a3667aaeSNaresh Kumar Inna {
709a3667aaeSNaresh Kumar Inna 	int rv = 0;
710a3667aaeSNaresh Kumar Inna 	struct csio_mb  *mbp;
711a3667aaeSNaresh Kumar Inna 	struct csio_eq_params eqp;
712a3667aaeSNaresh Kumar Inna 
713a3667aaeSNaresh Kumar Inna 	memset(&eqp, 0, sizeof(struct csio_eq_params));
714a3667aaeSNaresh Kumar Inna 
715a3667aaeSNaresh Kumar Inna 	mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
716a3667aaeSNaresh Kumar Inna 	if (!mbp)
717a3667aaeSNaresh Kumar Inna 		return -ENOMEM;
718a3667aaeSNaresh Kumar Inna 
719a3667aaeSNaresh Kumar Inna 	eqp.pfn		= hw->pfn;
720a3667aaeSNaresh Kumar Inna 	eqp.vfn		= 0;
721a3667aaeSNaresh Kumar Inna 	eqp.eqid	= csio_q_eqid(hw, eq_idx);
722a3667aaeSNaresh Kumar Inna 
723a3667aaeSNaresh Kumar Inna 	csio_mb_eq_ofld_free(hw, mbp, priv, CSIO_MB_DEFAULT_TMO, &eqp, cbfn);
724a3667aaeSNaresh Kumar Inna 
725a3667aaeSNaresh Kumar Inna 	rv = csio_mb_issue(hw, mbp);
726a3667aaeSNaresh Kumar Inna 	if (rv != 0) {
727a3667aaeSNaresh Kumar Inna 		mempool_free(mbp, hw->mb_mempool);
728a3667aaeSNaresh Kumar Inna 		return rv;
729a3667aaeSNaresh Kumar Inna 	}
730a3667aaeSNaresh Kumar Inna 
731a3667aaeSNaresh Kumar Inna 	if (cbfn != NULL)
732a3667aaeSNaresh Kumar Inna 		return 0;
733a3667aaeSNaresh Kumar Inna 
734a3667aaeSNaresh Kumar Inna 	return csio_wr_eq_destroy_rsp(hw, mbp, eq_idx);
735a3667aaeSNaresh Kumar Inna }
736a3667aaeSNaresh Kumar Inna 
737a3667aaeSNaresh Kumar Inna /*
738a3667aaeSNaresh Kumar Inna  * csio_wr_cleanup_eq_stpg - Cleanup Egress queue status page
739a3667aaeSNaresh Kumar Inna  * @hw: HW module
740a3667aaeSNaresh Kumar Inna  * @qidx: Egress queue index
741a3667aaeSNaresh Kumar Inna  *
742a3667aaeSNaresh Kumar Inna  * Cleanup the Egress queue status page.
743a3667aaeSNaresh Kumar Inna  */
744a3667aaeSNaresh Kumar Inna static void
csio_wr_cleanup_eq_stpg(struct csio_hw * hw,int qidx)745a3667aaeSNaresh Kumar Inna csio_wr_cleanup_eq_stpg(struct csio_hw *hw, int qidx)
746a3667aaeSNaresh Kumar Inna {
747a3667aaeSNaresh Kumar Inna 	struct csio_q	*q = csio_hw_to_wrm(hw)->q_arr[qidx];
748a3667aaeSNaresh Kumar Inna 	struct csio_qstatus_page *stp = (struct csio_qstatus_page *)q->vwrap;
749a3667aaeSNaresh Kumar Inna 
750a3667aaeSNaresh Kumar Inna 	memset(stp, 0, sizeof(*stp));
751a3667aaeSNaresh Kumar Inna }
752a3667aaeSNaresh Kumar Inna 
753a3667aaeSNaresh Kumar Inna /*
754a3667aaeSNaresh Kumar Inna  * csio_wr_cleanup_iq_ftr - Cleanup Footer entries in IQ
755a3667aaeSNaresh Kumar Inna  * @hw: HW module
756a3667aaeSNaresh Kumar Inna  * @qidx: Ingress queue index
757a3667aaeSNaresh Kumar Inna  *
758a3667aaeSNaresh Kumar Inna  * Cleanup the footer entries in the given ingress queue,
759a3667aaeSNaresh Kumar Inna  * set to 1 the internal copy of genbit.
760a3667aaeSNaresh Kumar Inna  */
761a3667aaeSNaresh Kumar Inna static void
csio_wr_cleanup_iq_ftr(struct csio_hw * hw,int qidx)762a3667aaeSNaresh Kumar Inna csio_wr_cleanup_iq_ftr(struct csio_hw *hw, int qidx)
763a3667aaeSNaresh Kumar Inna {
764a3667aaeSNaresh Kumar Inna 	struct csio_wrm *wrm	= csio_hw_to_wrm(hw);
765a3667aaeSNaresh Kumar Inna 	struct csio_q	*q	= wrm->q_arr[qidx];
766a3667aaeSNaresh Kumar Inna 	void *wr;
767a3667aaeSNaresh Kumar Inna 	struct csio_iqwr_footer *ftr;
768a3667aaeSNaresh Kumar Inna 	uint32_t i = 0;
769a3667aaeSNaresh Kumar Inna 
770a3667aaeSNaresh Kumar Inna 	/* set to 1 since we are just about zero out genbit */
771a3667aaeSNaresh Kumar Inna 	q->un.iq.genbit = 1;
772a3667aaeSNaresh Kumar Inna 
773a3667aaeSNaresh Kumar Inna 	for (i = 0; i < q->credits; i++) {
774a3667aaeSNaresh Kumar Inna 		/* Get the WR */
775a3667aaeSNaresh Kumar Inna 		wr = (void *)((uintptr_t)q->vstart +
776a3667aaeSNaresh Kumar Inna 					   (i * q->wr_sz));
777a3667aaeSNaresh Kumar Inna 		/* Get the footer */
778a3667aaeSNaresh Kumar Inna 		ftr = (struct csio_iqwr_footer *)((uintptr_t)wr +
779a3667aaeSNaresh Kumar Inna 					  (q->wr_sz - sizeof(*ftr)));
780a3667aaeSNaresh Kumar Inna 		/* Zero out footer */
781a3667aaeSNaresh Kumar Inna 		memset(ftr, 0, sizeof(*ftr));
782a3667aaeSNaresh Kumar Inna 	}
783a3667aaeSNaresh Kumar Inna }
784a3667aaeSNaresh Kumar Inna 
785a3667aaeSNaresh Kumar Inna int
csio_wr_destroy_queues(struct csio_hw * hw,bool cmd)786a3667aaeSNaresh Kumar Inna csio_wr_destroy_queues(struct csio_hw *hw, bool cmd)
787a3667aaeSNaresh Kumar Inna {
788a3667aaeSNaresh Kumar Inna 	int i, flq_idx;
789a3667aaeSNaresh Kumar Inna 	struct csio_q *q;
790a3667aaeSNaresh Kumar Inna 	struct csio_wrm *wrm = csio_hw_to_wrm(hw);
791a3667aaeSNaresh Kumar Inna 	int rv;
792a3667aaeSNaresh Kumar Inna 
793a3667aaeSNaresh Kumar Inna 	for (i = 0; i < wrm->free_qidx; i++) {
794a3667aaeSNaresh Kumar Inna 		q = wrm->q_arr[i];
795a3667aaeSNaresh Kumar Inna 
796a3667aaeSNaresh Kumar Inna 		switch (q->type) {
797a3667aaeSNaresh Kumar Inna 		case CSIO_EGRESS:
798a3667aaeSNaresh Kumar Inna 			if (csio_q_eqid(hw, i) != CSIO_MAX_QID) {
799a3667aaeSNaresh Kumar Inna 				csio_wr_cleanup_eq_stpg(hw, i);
800a3667aaeSNaresh Kumar Inna 				if (!cmd) {
801a3667aaeSNaresh Kumar Inna 					csio_q_eqid(hw, i) = CSIO_MAX_QID;
802a3667aaeSNaresh Kumar Inna 					continue;
803a3667aaeSNaresh Kumar Inna 				}
804a3667aaeSNaresh Kumar Inna 
805a3667aaeSNaresh Kumar Inna 				rv = csio_wr_eq_destroy(hw, NULL, i, NULL);
806a3667aaeSNaresh Kumar Inna 				if ((rv == -EBUSY) || (rv == -ETIMEDOUT))
807a3667aaeSNaresh Kumar Inna 					cmd = false;
808a3667aaeSNaresh Kumar Inna 
809a3667aaeSNaresh Kumar Inna 				csio_q_eqid(hw, i) = CSIO_MAX_QID;
810a3667aaeSNaresh Kumar Inna 			}
811df561f66SGustavo A. R. Silva 			fallthrough;
812a3667aaeSNaresh Kumar Inna 		case CSIO_INGRESS:
813a3667aaeSNaresh Kumar Inna 			if (csio_q_iqid(hw, i) != CSIO_MAX_QID) {
814a3667aaeSNaresh Kumar Inna 				csio_wr_cleanup_iq_ftr(hw, i);
815a3667aaeSNaresh Kumar Inna 				if (!cmd) {
816a3667aaeSNaresh Kumar Inna 					csio_q_iqid(hw, i) = CSIO_MAX_QID;
817a3667aaeSNaresh Kumar Inna 					flq_idx = csio_q_iq_flq_idx(hw, i);
818a3667aaeSNaresh Kumar Inna 					if (flq_idx != -1)
819a3667aaeSNaresh Kumar Inna 						csio_q_flid(hw, flq_idx) =
820a3667aaeSNaresh Kumar Inna 								CSIO_MAX_QID;
821a3667aaeSNaresh Kumar Inna 					continue;
822a3667aaeSNaresh Kumar Inna 				}
823a3667aaeSNaresh Kumar Inna 
824a3667aaeSNaresh Kumar Inna 				rv = csio_wr_iq_destroy(hw, NULL, i, NULL);
825a3667aaeSNaresh Kumar Inna 				if ((rv == -EBUSY) || (rv == -ETIMEDOUT))
826a3667aaeSNaresh Kumar Inna 					cmd = false;
827a3667aaeSNaresh Kumar Inna 
828a3667aaeSNaresh Kumar Inna 				csio_q_iqid(hw, i) = CSIO_MAX_QID;
829a3667aaeSNaresh Kumar Inna 				flq_idx = csio_q_iq_flq_idx(hw, i);
830a3667aaeSNaresh Kumar Inna 				if (flq_idx != -1)
831a3667aaeSNaresh Kumar Inna 					csio_q_flid(hw, flq_idx) = CSIO_MAX_QID;
832a3667aaeSNaresh Kumar Inna 			}
83396507758SGustavo A. R. Silva 			break;
834a3667aaeSNaresh Kumar Inna 		default:
835a3667aaeSNaresh Kumar Inna 			break;
836a3667aaeSNaresh Kumar Inna 		}
837a3667aaeSNaresh Kumar Inna 	}
838a3667aaeSNaresh Kumar Inna 
839a3667aaeSNaresh Kumar Inna 	hw->flags &= ~CSIO_HWF_Q_FW_ALLOCED;
840a3667aaeSNaresh Kumar Inna 
841a3667aaeSNaresh Kumar Inna 	return 0;
842a3667aaeSNaresh Kumar Inna }
843a3667aaeSNaresh Kumar Inna 
844a3667aaeSNaresh Kumar Inna /*
845a3667aaeSNaresh Kumar Inna  * csio_wr_get - Get requested size of WR entry/entries from queue.
846a3667aaeSNaresh Kumar Inna  * @hw: HW module.
847a3667aaeSNaresh Kumar Inna  * @qidx: Index of queue.
848a3667aaeSNaresh Kumar Inna  * @size: Cumulative size of Work request(s).
849a3667aaeSNaresh Kumar Inna  * @wrp: Work request pair.
850a3667aaeSNaresh Kumar Inna  *
851a3667aaeSNaresh Kumar Inna  * If requested credits are available, return the start address of the
852a3667aaeSNaresh Kumar Inna  * work request in the work request pair. Set pidx accordingly and
853a3667aaeSNaresh Kumar Inna  * return.
854a3667aaeSNaresh Kumar Inna  *
855a3667aaeSNaresh Kumar Inna  * NOTE about WR pair:
856a3667aaeSNaresh Kumar Inna  * ==================
857a3667aaeSNaresh Kumar Inna  * A WR can start towards the end of a queue, and then continue at the
858a3667aaeSNaresh Kumar Inna  * beginning, since the queue is considered to be circular. This will
859a3667aaeSNaresh Kumar Inna  * require a pair of address/size to be passed back to the caller -
860a3667aaeSNaresh Kumar Inna  * hence Work request pair format.
861a3667aaeSNaresh Kumar Inna  */
862a3667aaeSNaresh Kumar Inna int
csio_wr_get(struct csio_hw * hw,int qidx,uint32_t size,struct csio_wr_pair * wrp)863a3667aaeSNaresh Kumar Inna csio_wr_get(struct csio_hw *hw, int qidx, uint32_t size,
864a3667aaeSNaresh Kumar Inna 	    struct csio_wr_pair *wrp)
865a3667aaeSNaresh Kumar Inna {
866a3667aaeSNaresh Kumar Inna 	struct csio_wrm *wrm = csio_hw_to_wrm(hw);
867a3667aaeSNaresh Kumar Inna 	struct csio_q *q = wrm->q_arr[qidx];
868a3667aaeSNaresh Kumar Inna 	void *cwr = (void *)((uintptr_t)(q->vstart) +
869a3667aaeSNaresh Kumar Inna 						(q->pidx * CSIO_QCREDIT_SZ));
870a3667aaeSNaresh Kumar Inna 	struct csio_qstatus_page *stp = (struct csio_qstatus_page *)q->vwrap;
871a3667aaeSNaresh Kumar Inna 	uint16_t cidx = q->cidx = ntohs(stp->cidx);
872a3667aaeSNaresh Kumar Inna 	uint16_t pidx = q->pidx;
873a3667aaeSNaresh Kumar Inna 	uint32_t req_sz	= ALIGN(size, CSIO_QCREDIT_SZ);
874a3667aaeSNaresh Kumar Inna 	int req_credits	= req_sz / CSIO_QCREDIT_SZ;
875a3667aaeSNaresh Kumar Inna 	int credits;
876a3667aaeSNaresh Kumar Inna 
877a3667aaeSNaresh Kumar Inna 	CSIO_DB_ASSERT(q->owner != NULL);
878a3667aaeSNaresh Kumar Inna 	CSIO_DB_ASSERT((qidx >= 0) && (qidx < wrm->free_qidx));
879a3667aaeSNaresh Kumar Inna 	CSIO_DB_ASSERT(cidx <= q->credits);
880a3667aaeSNaresh Kumar Inna 
881a3667aaeSNaresh Kumar Inna 	/* Calculate credits */
882a3667aaeSNaresh Kumar Inna 	if (pidx > cidx) {
883a3667aaeSNaresh Kumar Inna 		credits = q->credits - (pidx - cidx) - 1;
884a3667aaeSNaresh Kumar Inna 	} else if (cidx > pidx) {
885a3667aaeSNaresh Kumar Inna 		credits = cidx - pidx - 1;
886a3667aaeSNaresh Kumar Inna 	} else {
887a3667aaeSNaresh Kumar Inna 		/* cidx == pidx, empty queue */
888a3667aaeSNaresh Kumar Inna 		credits = q->credits;
889a3667aaeSNaresh Kumar Inna 		CSIO_INC_STATS(q, n_qempty);
890a3667aaeSNaresh Kumar Inna 	}
891a3667aaeSNaresh Kumar Inna 
892a3667aaeSNaresh Kumar Inna 	/*
893a3667aaeSNaresh Kumar Inna 	 * Check if we have enough credits.
894a3667aaeSNaresh Kumar Inna 	 * credits = 1 implies queue is full.
895a3667aaeSNaresh Kumar Inna 	 */
896a3667aaeSNaresh Kumar Inna 	if (!credits || (req_credits > credits)) {
897a3667aaeSNaresh Kumar Inna 		CSIO_INC_STATS(q, n_qfull);
898a3667aaeSNaresh Kumar Inna 		return -EBUSY;
899a3667aaeSNaresh Kumar Inna 	}
900a3667aaeSNaresh Kumar Inna 
901a3667aaeSNaresh Kumar Inna 	/*
902a3667aaeSNaresh Kumar Inna 	 * If we are here, we have enough credits to satisfy the
903a3667aaeSNaresh Kumar Inna 	 * request. Check if we are near the end of q, and if WR spills over.
904a3667aaeSNaresh Kumar Inna 	 * If it does, use the first addr/size to cover the queue until
905a3667aaeSNaresh Kumar Inna 	 * the end. Fit the remainder portion of the request at the top
906a3667aaeSNaresh Kumar Inna 	 * of queue and return it in the second addr/len. Set pidx
907a3667aaeSNaresh Kumar Inna 	 * accordingly.
908a3667aaeSNaresh Kumar Inna 	 */
909a3667aaeSNaresh Kumar Inna 	if (unlikely(((uintptr_t)cwr + req_sz) > (uintptr_t)(q->vwrap))) {
910a3667aaeSNaresh Kumar Inna 		wrp->addr1 = cwr;
911a3667aaeSNaresh Kumar Inna 		wrp->size1 = (uint32_t)((uintptr_t)q->vwrap - (uintptr_t)cwr);
912a3667aaeSNaresh Kumar Inna 		wrp->addr2 = q->vstart;
913a3667aaeSNaresh Kumar Inna 		wrp->size2 = req_sz - wrp->size1;
914a3667aaeSNaresh Kumar Inna 		q->pidx	= (uint16_t)(ALIGN(wrp->size2, CSIO_QCREDIT_SZ) /
915a3667aaeSNaresh Kumar Inna 							CSIO_QCREDIT_SZ);
916a3667aaeSNaresh Kumar Inna 		CSIO_INC_STATS(q, n_qwrap);
917a3667aaeSNaresh Kumar Inna 		CSIO_INC_STATS(q, n_eq_wr_split);
918a3667aaeSNaresh Kumar Inna 	} else {
919a3667aaeSNaresh Kumar Inna 		wrp->addr1 = cwr;
920a3667aaeSNaresh Kumar Inna 		wrp->size1 = req_sz;
921a3667aaeSNaresh Kumar Inna 		wrp->addr2 = NULL;
922a3667aaeSNaresh Kumar Inna 		wrp->size2 = 0;
923a3667aaeSNaresh Kumar Inna 		q->pidx	+= (uint16_t)req_credits;
924a3667aaeSNaresh Kumar Inna 
925a3667aaeSNaresh Kumar Inna 		/* We are the end of queue, roll back pidx to top of queue */
926a3667aaeSNaresh Kumar Inna 		if (unlikely(q->pidx == q->credits)) {
927a3667aaeSNaresh Kumar Inna 			q->pidx = 0;
928a3667aaeSNaresh Kumar Inna 			CSIO_INC_STATS(q, n_qwrap);
929a3667aaeSNaresh Kumar Inna 		}
930a3667aaeSNaresh Kumar Inna 	}
931a3667aaeSNaresh Kumar Inna 
932a3667aaeSNaresh Kumar Inna 	q->inc_idx = (uint16_t)req_credits;
933a3667aaeSNaresh Kumar Inna 
934a3667aaeSNaresh Kumar Inna 	CSIO_INC_STATS(q, n_tot_reqs);
935a3667aaeSNaresh Kumar Inna 
936a3667aaeSNaresh Kumar Inna 	return 0;
937a3667aaeSNaresh Kumar Inna }
938a3667aaeSNaresh Kumar Inna 
939a3667aaeSNaresh Kumar Inna /*
940a3667aaeSNaresh Kumar Inna  * csio_wr_copy_to_wrp - Copies given data into WR.
941a3667aaeSNaresh Kumar Inna  * @data_buf - Data buffer
942a3667aaeSNaresh Kumar Inna  * @wrp - Work request pair.
943a3667aaeSNaresh Kumar Inna  * @wr_off - Work request offset.
944a3667aaeSNaresh Kumar Inna  * @data_len - Data length.
945a3667aaeSNaresh Kumar Inna  *
946a3667aaeSNaresh Kumar Inna  * Copies the given data in Work Request. Work request pair(wrp) specifies
947a3667aaeSNaresh Kumar Inna  * address information of Work request.
948a3667aaeSNaresh Kumar Inna  * Returns: none
949a3667aaeSNaresh Kumar Inna  */
950a3667aaeSNaresh Kumar Inna void
csio_wr_copy_to_wrp(void * data_buf,struct csio_wr_pair * wrp,uint32_t wr_off,uint32_t data_len)951a3667aaeSNaresh Kumar Inna csio_wr_copy_to_wrp(void *data_buf, struct csio_wr_pair *wrp,
952a3667aaeSNaresh Kumar Inna 		   uint32_t wr_off, uint32_t data_len)
953a3667aaeSNaresh Kumar Inna {
954a3667aaeSNaresh Kumar Inna 	uint32_t nbytes;
955a3667aaeSNaresh Kumar Inna 
956a3667aaeSNaresh Kumar Inna 	/* Number of space available in buffer addr1 of WRP */
957a3667aaeSNaresh Kumar Inna 	nbytes = ((wrp->size1 - wr_off) >= data_len) ?
958a3667aaeSNaresh Kumar Inna 					data_len : (wrp->size1 - wr_off);
959a3667aaeSNaresh Kumar Inna 
960a3667aaeSNaresh Kumar Inna 	memcpy((uint8_t *) wrp->addr1 + wr_off, data_buf, nbytes);
961a3667aaeSNaresh Kumar Inna 	data_len -= nbytes;
962a3667aaeSNaresh Kumar Inna 
963a3667aaeSNaresh Kumar Inna 	/* Write the remaining data from the begining of circular buffer */
964a3667aaeSNaresh Kumar Inna 	if (data_len) {
965a3667aaeSNaresh Kumar Inna 		CSIO_DB_ASSERT(data_len <= wrp->size2);
966a3667aaeSNaresh Kumar Inna 		CSIO_DB_ASSERT(wrp->addr2 != NULL);
967a3667aaeSNaresh Kumar Inna 		memcpy(wrp->addr2, (uint8_t *) data_buf + nbytes, data_len);
968a3667aaeSNaresh Kumar Inna 	}
969a3667aaeSNaresh Kumar Inna }
970a3667aaeSNaresh Kumar Inna 
971a3667aaeSNaresh Kumar Inna /*
972a3667aaeSNaresh Kumar Inna  * csio_wr_issue - Notify chip of Work request.
973a3667aaeSNaresh Kumar Inna  * @hw: HW module.
974a3667aaeSNaresh Kumar Inna  * @qidx: Index of queue.
975a3667aaeSNaresh Kumar Inna  * @prio: 0: Low priority, 1: High priority
976a3667aaeSNaresh Kumar Inna  *
977a3667aaeSNaresh Kumar Inna  * Rings the SGE Doorbell by writing the current producer index of the passed
978a3667aaeSNaresh Kumar Inna  * in queue into the register.
979a3667aaeSNaresh Kumar Inna  *
980a3667aaeSNaresh Kumar Inna  */
981a3667aaeSNaresh Kumar Inna int
csio_wr_issue(struct csio_hw * hw,int qidx,bool prio)982a3667aaeSNaresh Kumar Inna csio_wr_issue(struct csio_hw *hw, int qidx, bool prio)
983a3667aaeSNaresh Kumar Inna {
984a3667aaeSNaresh Kumar Inna 	struct csio_wrm *wrm = csio_hw_to_wrm(hw);
985a3667aaeSNaresh Kumar Inna 	struct csio_q *q = wrm->q_arr[qidx];
986a3667aaeSNaresh Kumar Inna 
987a3667aaeSNaresh Kumar Inna 	CSIO_DB_ASSERT((qidx >= 0) && (qidx < wrm->free_qidx));
988a3667aaeSNaresh Kumar Inna 
989a3667aaeSNaresh Kumar Inna 	wmb();
990a3667aaeSNaresh Kumar Inna 	/* Ring SGE Doorbell writing q->pidx into it */
991f612b815SHariprasad Shenai 	csio_wr_reg32(hw, DBPRIO_V(prio) | QID_V(q->un.eq.physeqid) |
9923fb4c22eSPraveen Madhavan 			  PIDX_T5_V(q->inc_idx) | DBTYPE_F,
993f612b815SHariprasad Shenai 			  MYPF_REG(SGE_PF_KDOORBELL_A));
994a3667aaeSNaresh Kumar Inna 	q->inc_idx = 0;
995a3667aaeSNaresh Kumar Inna 
996a3667aaeSNaresh Kumar Inna 	return 0;
997a3667aaeSNaresh Kumar Inna }
998a3667aaeSNaresh Kumar Inna 
999a3667aaeSNaresh Kumar Inna static inline uint32_t
csio_wr_avail_qcredits(struct csio_q * q)1000a3667aaeSNaresh Kumar Inna csio_wr_avail_qcredits(struct csio_q *q)
1001a3667aaeSNaresh Kumar Inna {
1002a3667aaeSNaresh Kumar Inna 	if (q->pidx > q->cidx)
1003a3667aaeSNaresh Kumar Inna 		return q->pidx - q->cidx;
1004a3667aaeSNaresh Kumar Inna 	else if (q->cidx > q->pidx)
1005a3667aaeSNaresh Kumar Inna 		return q->credits - (q->cidx - q->pidx);
1006a3667aaeSNaresh Kumar Inna 	else
1007a3667aaeSNaresh Kumar Inna 		return 0;	/* cidx == pidx, empty queue */
1008a3667aaeSNaresh Kumar Inna }
1009a3667aaeSNaresh Kumar Inna 
1010a3667aaeSNaresh Kumar Inna /*
1011a3667aaeSNaresh Kumar Inna  * csio_wr_inval_flq_buf - Invalidate a free list buffer entry.
1012a3667aaeSNaresh Kumar Inna  * @hw: HW module.
1013a3667aaeSNaresh Kumar Inna  * @flq: The freelist queue.
1014a3667aaeSNaresh Kumar Inna  *
1015a3667aaeSNaresh Kumar Inna  * Invalidate the driver's version of a freelist buffer entry,
1016a3667aaeSNaresh Kumar Inna  * without freeing the associated the DMA memory. The entry
1017a3667aaeSNaresh Kumar Inna  * to be invalidated is picked up from the current Free list
1018a3667aaeSNaresh Kumar Inna  * queue cidx.
1019a3667aaeSNaresh Kumar Inna  *
1020a3667aaeSNaresh Kumar Inna  */
1021a3667aaeSNaresh Kumar Inna static inline void
csio_wr_inval_flq_buf(struct csio_hw * hw,struct csio_q * flq)1022a3667aaeSNaresh Kumar Inna csio_wr_inval_flq_buf(struct csio_hw *hw, struct csio_q *flq)
1023a3667aaeSNaresh Kumar Inna {
1024a3667aaeSNaresh Kumar Inna 	flq->cidx++;
1025a3667aaeSNaresh Kumar Inna 	if (flq->cidx == flq->credits) {
1026a3667aaeSNaresh Kumar Inna 		flq->cidx = 0;
1027a3667aaeSNaresh Kumar Inna 		CSIO_INC_STATS(flq, n_qwrap);
1028a3667aaeSNaresh Kumar Inna 	}
1029a3667aaeSNaresh Kumar Inna }
1030a3667aaeSNaresh Kumar Inna 
1031a3667aaeSNaresh Kumar Inna /*
1032a3667aaeSNaresh Kumar Inna  * csio_wr_process_fl - Process a freelist completion.
1033a3667aaeSNaresh Kumar Inna  * @hw: HW module.
1034a3667aaeSNaresh Kumar Inna  * @q: The ingress queue attached to the Freelist.
1035a3667aaeSNaresh Kumar Inna  * @wr: The freelist completion WR in the ingress queue.
1036a3667aaeSNaresh Kumar Inna  * @len_to_qid: The lower 32-bits of the first flit of the RSP footer
1037a3667aaeSNaresh Kumar Inna  * @iq_handler: Caller's handler for this completion.
1038a3667aaeSNaresh Kumar Inna  * @priv: Private pointer of caller
1039a3667aaeSNaresh Kumar Inna  *
1040a3667aaeSNaresh Kumar Inna  */
1041a3667aaeSNaresh Kumar Inna static inline void
csio_wr_process_fl(struct csio_hw * hw,struct csio_q * q,void * wr,uint32_t len_to_qid,void (* iq_handler)(struct csio_hw *,void *,uint32_t,struct csio_fl_dma_buf *,void *),void * priv)1042a3667aaeSNaresh Kumar Inna csio_wr_process_fl(struct csio_hw *hw, struct csio_q *q,
1043a3667aaeSNaresh Kumar Inna 		   void *wr, uint32_t len_to_qid,
1044a3667aaeSNaresh Kumar Inna 		   void (*iq_handler)(struct csio_hw *, void *,
1045a3667aaeSNaresh Kumar Inna 				      uint32_t, struct csio_fl_dma_buf *,
1046a3667aaeSNaresh Kumar Inna 				      void *),
1047a3667aaeSNaresh Kumar Inna 		   void *priv)
1048a3667aaeSNaresh Kumar Inna {
1049a3667aaeSNaresh Kumar Inna 	struct csio_wrm *wrm = csio_hw_to_wrm(hw);
1050a3667aaeSNaresh Kumar Inna 	struct csio_sge *sge = &wrm->sge;
1051a3667aaeSNaresh Kumar Inna 	struct csio_fl_dma_buf flb;
1052a3667aaeSNaresh Kumar Inna 	struct csio_dma_buf *buf, *fbuf;
1053a3667aaeSNaresh Kumar Inna 	uint32_t bufsz, len, lastlen = 0;
1054a3667aaeSNaresh Kumar Inna 	struct csio_q *flq = hw->wrm.q_arr[q->un.iq.flq_idx];
1055a3667aaeSNaresh Kumar Inna 
1056a3667aaeSNaresh Kumar Inna 	CSIO_DB_ASSERT(flq != NULL);
1057a3667aaeSNaresh Kumar Inna 
1058a3667aaeSNaresh Kumar Inna 	len = len_to_qid;
1059a3667aaeSNaresh Kumar Inna 
1060a3667aaeSNaresh Kumar Inna 	if (len & IQWRF_NEWBUF) {
1061a3667aaeSNaresh Kumar Inna 		if (flq->un.fl.offset > 0) {
1062a3667aaeSNaresh Kumar Inna 			csio_wr_inval_flq_buf(hw, flq);
1063a3667aaeSNaresh Kumar Inna 			flq->un.fl.offset = 0;
1064a3667aaeSNaresh Kumar Inna 		}
1065a3667aaeSNaresh Kumar Inna 		len = IQWRF_LEN_GET(len);
1066a3667aaeSNaresh Kumar Inna 	}
1067a3667aaeSNaresh Kumar Inna 
1068a3667aaeSNaresh Kumar Inna 	CSIO_DB_ASSERT(len != 0);
1069a3667aaeSNaresh Kumar Inna 
1070a3667aaeSNaresh Kumar Inna 	flb.totlen = len;
1071a3667aaeSNaresh Kumar Inna 
1072a3667aaeSNaresh Kumar Inna 	/* Consume all freelist buffers used for len bytes */
1073*0aa46ebaSColin Ian King 	for (fbuf = flb.flbufs; ; fbuf++) {
1074a3667aaeSNaresh Kumar Inna 		buf = &flq->un.fl.bufs[flq->cidx];
1075a3667aaeSNaresh Kumar Inna 		bufsz = csio_wr_fl_bufsz(sge, buf);
1076a3667aaeSNaresh Kumar Inna 
1077a3667aaeSNaresh Kumar Inna 		fbuf->paddr	= buf->paddr;
1078a3667aaeSNaresh Kumar Inna 		fbuf->vaddr	= buf->vaddr;
1079a3667aaeSNaresh Kumar Inna 
1080a3667aaeSNaresh Kumar Inna 		flb.offset	= flq->un.fl.offset;
1081a3667aaeSNaresh Kumar Inna 		lastlen		= min(bufsz, len);
1082a3667aaeSNaresh Kumar Inna 		fbuf->len	= lastlen;
1083a3667aaeSNaresh Kumar Inna 
1084a3667aaeSNaresh Kumar Inna 		len -= lastlen;
1085a3667aaeSNaresh Kumar Inna 		if (!len)
1086a3667aaeSNaresh Kumar Inna 			break;
1087a3667aaeSNaresh Kumar Inna 		csio_wr_inval_flq_buf(hw, flq);
1088a3667aaeSNaresh Kumar Inna 	}
1089a3667aaeSNaresh Kumar Inna 
1090a3667aaeSNaresh Kumar Inna 	flb.defer_free = flq->un.fl.packen ? 0 : 1;
1091a3667aaeSNaresh Kumar Inna 
1092a3667aaeSNaresh Kumar Inna 	iq_handler(hw, wr, q->wr_sz - sizeof(struct csio_iqwr_footer),
1093a3667aaeSNaresh Kumar Inna 		   &flb, priv);
1094a3667aaeSNaresh Kumar Inna 
1095a3667aaeSNaresh Kumar Inna 	if (flq->un.fl.packen)
1096a3667aaeSNaresh Kumar Inna 		flq->un.fl.offset += ALIGN(lastlen, sge->csio_fl_align);
1097a3667aaeSNaresh Kumar Inna 	else
1098a3667aaeSNaresh Kumar Inna 		csio_wr_inval_flq_buf(hw, flq);
1099a3667aaeSNaresh Kumar Inna 
1100a3667aaeSNaresh Kumar Inna }
1101a3667aaeSNaresh Kumar Inna 
1102a3667aaeSNaresh Kumar Inna /*
1103a3667aaeSNaresh Kumar Inna  * csio_is_new_iqwr - Is this a new Ingress queue entry ?
1104a3667aaeSNaresh Kumar Inna  * @q: Ingress quueue.
1105a3667aaeSNaresh Kumar Inna  * @ftr: Ingress queue WR SGE footer.
1106a3667aaeSNaresh Kumar Inna  *
1107a3667aaeSNaresh Kumar Inna  * The entry is new if our generation bit matches the corresponding
1108a3667aaeSNaresh Kumar Inna  * bit in the footer of the current WR.
1109a3667aaeSNaresh Kumar Inna  */
1110a3667aaeSNaresh Kumar Inna static inline bool
csio_is_new_iqwr(struct csio_q * q,struct csio_iqwr_footer * ftr)1111a3667aaeSNaresh Kumar Inna csio_is_new_iqwr(struct csio_q *q, struct csio_iqwr_footer *ftr)
1112a3667aaeSNaresh Kumar Inna {
1113a3667aaeSNaresh Kumar Inna 	return (q->un.iq.genbit == (ftr->u.type_gen >> IQWRF_GEN_SHIFT));
1114a3667aaeSNaresh Kumar Inna }
1115a3667aaeSNaresh Kumar Inna 
1116a3667aaeSNaresh Kumar Inna /*
1117a3667aaeSNaresh Kumar Inna  * csio_wr_process_iq - Process elements in Ingress queue.
1118a3667aaeSNaresh Kumar Inna  * @hw:  HW pointer
1119a3667aaeSNaresh Kumar Inna  * @qidx: Index of queue
1120a3667aaeSNaresh Kumar Inna  * @iq_handler: Handler for this queue
1121a3667aaeSNaresh Kumar Inna  * @priv: Caller's private pointer
1122a3667aaeSNaresh Kumar Inna  *
1123a3667aaeSNaresh Kumar Inna  * This routine walks through every entry of the ingress queue, calling
1124a3667aaeSNaresh Kumar Inna  * the provided iq_handler with the entry, until the generation bit
1125a3667aaeSNaresh Kumar Inna  * flips.
1126a3667aaeSNaresh Kumar Inna  */
1127a3667aaeSNaresh Kumar Inna int
csio_wr_process_iq(struct csio_hw * hw,struct csio_q * q,void (* iq_handler)(struct csio_hw *,void *,uint32_t,struct csio_fl_dma_buf *,void *),void * priv)1128a3667aaeSNaresh Kumar Inna csio_wr_process_iq(struct csio_hw *hw, struct csio_q *q,
1129a3667aaeSNaresh Kumar Inna 		   void (*iq_handler)(struct csio_hw *, void *,
1130a3667aaeSNaresh Kumar Inna 				      uint32_t, struct csio_fl_dma_buf *,
1131a3667aaeSNaresh Kumar Inna 				      void *),
1132a3667aaeSNaresh Kumar Inna 		   void *priv)
1133a3667aaeSNaresh Kumar Inna {
1134a3667aaeSNaresh Kumar Inna 	struct csio_wrm *wrm = csio_hw_to_wrm(hw);
1135a3667aaeSNaresh Kumar Inna 	void *wr = (void *)((uintptr_t)q->vstart + (q->cidx * q->wr_sz));
1136a3667aaeSNaresh Kumar Inna 	struct csio_iqwr_footer *ftr;
1137a3667aaeSNaresh Kumar Inna 	uint32_t wr_type, fw_qid, qid;
1138a3667aaeSNaresh Kumar Inna 	struct csio_q *q_completed;
1139a3667aaeSNaresh Kumar Inna 	struct csio_q *flq = csio_iq_has_fl(q) ?
1140a3667aaeSNaresh Kumar Inna 					wrm->q_arr[q->un.iq.flq_idx] : NULL;
1141a3667aaeSNaresh Kumar Inna 	int rv = 0;
1142a3667aaeSNaresh Kumar Inna 
1143a3667aaeSNaresh Kumar Inna 	/* Get the footer */
1144a3667aaeSNaresh Kumar Inna 	ftr = (struct csio_iqwr_footer *)((uintptr_t)wr +
1145a3667aaeSNaresh Kumar Inna 					  (q->wr_sz - sizeof(*ftr)));
1146a3667aaeSNaresh Kumar Inna 
1147a3667aaeSNaresh Kumar Inna 	/*
1148a3667aaeSNaresh Kumar Inna 	 * When q wrapped around last time, driver should have inverted
1149a3667aaeSNaresh Kumar Inna 	 * ic.genbit as well.
1150a3667aaeSNaresh Kumar Inna 	 */
1151a3667aaeSNaresh Kumar Inna 	while (csio_is_new_iqwr(q, ftr)) {
1152a3667aaeSNaresh Kumar Inna 
1153a3667aaeSNaresh Kumar Inna 		CSIO_DB_ASSERT(((uintptr_t)wr + q->wr_sz) <=
1154a3667aaeSNaresh Kumar Inna 						(uintptr_t)q->vwrap);
1155a3667aaeSNaresh Kumar Inna 		rmb();
1156a3667aaeSNaresh Kumar Inna 		wr_type = IQWRF_TYPE_GET(ftr->u.type_gen);
1157a3667aaeSNaresh Kumar Inna 
1158a3667aaeSNaresh Kumar Inna 		switch (wr_type) {
1159a3667aaeSNaresh Kumar Inna 		case X_RSPD_TYPE_CPL:
1160a3667aaeSNaresh Kumar Inna 			/* Subtract footer from WR len */
1161a3667aaeSNaresh Kumar Inna 			iq_handler(hw, wr, q->wr_sz - sizeof(*ftr), NULL, priv);
1162a3667aaeSNaresh Kumar Inna 			break;
1163a3667aaeSNaresh Kumar Inna 		case X_RSPD_TYPE_FLBUF:
1164a3667aaeSNaresh Kumar Inna 			csio_wr_process_fl(hw, q, wr,
1165a3667aaeSNaresh Kumar Inna 					   ntohl(ftr->pldbuflen_qid),
1166a3667aaeSNaresh Kumar Inna 					   iq_handler, priv);
1167a3667aaeSNaresh Kumar Inna 			break;
1168a3667aaeSNaresh Kumar Inna 		case X_RSPD_TYPE_INTR:
1169a3667aaeSNaresh Kumar Inna 			fw_qid = ntohl(ftr->pldbuflen_qid);
1170a3667aaeSNaresh Kumar Inna 			qid = fw_qid - wrm->fw_iq_start;
1171a3667aaeSNaresh Kumar Inna 			q_completed = hw->wrm.intr_map[qid];
1172a3667aaeSNaresh Kumar Inna 
1173a3667aaeSNaresh Kumar Inna 			if (unlikely(qid ==
1174a3667aaeSNaresh Kumar Inna 					csio_q_physiqid(hw, hw->intr_iq_idx))) {
1175a3667aaeSNaresh Kumar Inna 				/*
1176a3667aaeSNaresh Kumar Inna 				 * We are already in the Forward Interrupt
1177a3667aaeSNaresh Kumar Inna 				 * Interrupt Queue Service! Do-not service
1178a3667aaeSNaresh Kumar Inna 				 * again!
1179a3667aaeSNaresh Kumar Inna 				 *
1180a3667aaeSNaresh Kumar Inna 				 */
1181a3667aaeSNaresh Kumar Inna 			} else {
1182a3667aaeSNaresh Kumar Inna 				CSIO_DB_ASSERT(q_completed);
1183a3667aaeSNaresh Kumar Inna 				CSIO_DB_ASSERT(
1184a3667aaeSNaresh Kumar Inna 					q_completed->un.iq.iq_intx_handler);
1185a3667aaeSNaresh Kumar Inna 
1186a3667aaeSNaresh Kumar Inna 				/* Call the queue handler. */
1187a3667aaeSNaresh Kumar Inna 				q_completed->un.iq.iq_intx_handler(hw, NULL,
1188a3667aaeSNaresh Kumar Inna 						0, NULL, (void *)q_completed);
1189a3667aaeSNaresh Kumar Inna 			}
1190a3667aaeSNaresh Kumar Inna 			break;
1191a3667aaeSNaresh Kumar Inna 		default:
1192a3667aaeSNaresh Kumar Inna 			csio_warn(hw, "Unknown resp type 0x%x received\n",
1193a3667aaeSNaresh Kumar Inna 				 wr_type);
1194a3667aaeSNaresh Kumar Inna 			CSIO_INC_STATS(q, n_rsp_unknown);
1195a3667aaeSNaresh Kumar Inna 			break;
1196a3667aaeSNaresh Kumar Inna 		}
1197a3667aaeSNaresh Kumar Inna 
1198a3667aaeSNaresh Kumar Inna 		/*
1199a3667aaeSNaresh Kumar Inna 		 * Ingress *always* has fixed size WR entries. Therefore,
1200a3667aaeSNaresh Kumar Inna 		 * there should always be complete WRs towards the end of
1201a3667aaeSNaresh Kumar Inna 		 * queue.
1202a3667aaeSNaresh Kumar Inna 		 */
1203a3667aaeSNaresh Kumar Inna 		if (((uintptr_t)wr + q->wr_sz) == (uintptr_t)q->vwrap) {
1204a3667aaeSNaresh Kumar Inna 
1205a3667aaeSNaresh Kumar Inna 			/* Roll over to start of queue */
1206a3667aaeSNaresh Kumar Inna 			q->cidx = 0;
1207a3667aaeSNaresh Kumar Inna 			wr	= q->vstart;
1208a3667aaeSNaresh Kumar Inna 
1209a3667aaeSNaresh Kumar Inna 			/* Toggle genbit */
1210a3667aaeSNaresh Kumar Inna 			q->un.iq.genbit ^= 0x1;
1211a3667aaeSNaresh Kumar Inna 
1212a3667aaeSNaresh Kumar Inna 			CSIO_INC_STATS(q, n_qwrap);
1213a3667aaeSNaresh Kumar Inna 		} else {
1214a3667aaeSNaresh Kumar Inna 			q->cidx++;
1215a3667aaeSNaresh Kumar Inna 			wr	= (void *)((uintptr_t)(q->vstart) +
1216a3667aaeSNaresh Kumar Inna 					   (q->cidx * q->wr_sz));
1217a3667aaeSNaresh Kumar Inna 		}
1218a3667aaeSNaresh Kumar Inna 
1219a3667aaeSNaresh Kumar Inna 		ftr = (struct csio_iqwr_footer *)((uintptr_t)wr +
1220a3667aaeSNaresh Kumar Inna 						  (q->wr_sz - sizeof(*ftr)));
1221a3667aaeSNaresh Kumar Inna 		q->inc_idx++;
1222a3667aaeSNaresh Kumar Inna 
1223a3667aaeSNaresh Kumar Inna 	} /* while (q->un.iq.genbit == hdr->genbit) */
1224a3667aaeSNaresh Kumar Inna 
1225a3667aaeSNaresh Kumar Inna 	/*
1226a3667aaeSNaresh Kumar Inna 	 * We need to re-arm SGE interrupts in case we got a stray interrupt,
1227a3667aaeSNaresh Kumar Inna 	 * especially in msix mode. With INTx, this may be a common occurence.
1228a3667aaeSNaresh Kumar Inna 	 */
1229a3667aaeSNaresh Kumar Inna 	if (unlikely(!q->inc_idx)) {
1230a3667aaeSNaresh Kumar Inna 		CSIO_INC_STATS(q, n_stray_comp);
1231a3667aaeSNaresh Kumar Inna 		rv = -EINVAL;
1232a3667aaeSNaresh Kumar Inna 		goto restart;
1233a3667aaeSNaresh Kumar Inna 	}
1234a3667aaeSNaresh Kumar Inna 
1235a3667aaeSNaresh Kumar Inna 	/* Replenish free list buffers if pending falls below low water mark */
1236a3667aaeSNaresh Kumar Inna 	if (flq) {
1237a3667aaeSNaresh Kumar Inna 		uint32_t avail  = csio_wr_avail_qcredits(flq);
1238a3667aaeSNaresh Kumar Inna 		if (avail <= 16) {
1239a3667aaeSNaresh Kumar Inna 			/* Make sure in FLQ, atleast 1 credit (8 FL buffers)
1240a3667aaeSNaresh Kumar Inna 			 * remains unpopulated otherwise HW thinks
1241a3667aaeSNaresh Kumar Inna 			 * FLQ is empty.
1242a3667aaeSNaresh Kumar Inna 			 */
1243a3667aaeSNaresh Kumar Inna 			csio_wr_update_fl(hw, flq, (flq->credits - 8) - avail);
1244a3667aaeSNaresh Kumar Inna 			csio_wr_ring_fldb(hw, flq);
1245a3667aaeSNaresh Kumar Inna 		}
1246a3667aaeSNaresh Kumar Inna 	}
1247a3667aaeSNaresh Kumar Inna 
1248a3667aaeSNaresh Kumar Inna restart:
1249a3667aaeSNaresh Kumar Inna 	/* Now inform SGE about our incremental index value */
1250f612b815SHariprasad Shenai 	csio_wr_reg32(hw, CIDXINC_V(q->inc_idx)		|
1251f612b815SHariprasad Shenai 			  INGRESSQID_V(q->un.iq.physiqid)	|
1252f612b815SHariprasad Shenai 			  TIMERREG_V(csio_sge_timer_reg),
1253f612b815SHariprasad Shenai 			  MYPF_REG(SGE_PF_GTS_A));
1254a3667aaeSNaresh Kumar Inna 	q->stats.n_tot_rsps += q->inc_idx;
1255a3667aaeSNaresh Kumar Inna 
1256a3667aaeSNaresh Kumar Inna 	q->inc_idx = 0;
1257a3667aaeSNaresh Kumar Inna 
1258a3667aaeSNaresh Kumar Inna 	return rv;
1259a3667aaeSNaresh Kumar Inna }
1260a3667aaeSNaresh Kumar Inna 
1261a3667aaeSNaresh Kumar Inna int
csio_wr_process_iq_idx(struct csio_hw * hw,int qidx,void (* iq_handler)(struct csio_hw *,void *,uint32_t,struct csio_fl_dma_buf *,void *),void * priv)1262a3667aaeSNaresh Kumar Inna csio_wr_process_iq_idx(struct csio_hw *hw, int qidx,
1263a3667aaeSNaresh Kumar Inna 		   void (*iq_handler)(struct csio_hw *, void *,
1264a3667aaeSNaresh Kumar Inna 				      uint32_t, struct csio_fl_dma_buf *,
1265a3667aaeSNaresh Kumar Inna 				      void *),
1266a3667aaeSNaresh Kumar Inna 		   void *priv)
1267a3667aaeSNaresh Kumar Inna {
1268a3667aaeSNaresh Kumar Inna 	struct csio_wrm *wrm	= csio_hw_to_wrm(hw);
1269a3667aaeSNaresh Kumar Inna 	struct csio_q	*iq	= wrm->q_arr[qidx];
1270a3667aaeSNaresh Kumar Inna 
1271a3667aaeSNaresh Kumar Inna 	return csio_wr_process_iq(hw, iq, iq_handler, priv);
1272a3667aaeSNaresh Kumar Inna }
1273a3667aaeSNaresh Kumar Inna 
1274a3667aaeSNaresh Kumar Inna static int
csio_closest_timer(struct csio_sge * s,int time)1275a3667aaeSNaresh Kumar Inna csio_closest_timer(struct csio_sge *s, int time)
1276a3667aaeSNaresh Kumar Inna {
1277a3667aaeSNaresh Kumar Inna 	int i, delta, match = 0, min_delta = INT_MAX;
1278a3667aaeSNaresh Kumar Inna 
1279a3667aaeSNaresh Kumar Inna 	for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
1280a3667aaeSNaresh Kumar Inna 		delta = time - s->timer_val[i];
1281a3667aaeSNaresh Kumar Inna 		if (delta < 0)
1282a3667aaeSNaresh Kumar Inna 			delta = -delta;
1283a3667aaeSNaresh Kumar Inna 		if (delta < min_delta) {
1284a3667aaeSNaresh Kumar Inna 			min_delta = delta;
1285a3667aaeSNaresh Kumar Inna 			match = i;
1286a3667aaeSNaresh Kumar Inna 		}
1287a3667aaeSNaresh Kumar Inna 	}
1288a3667aaeSNaresh Kumar Inna 	return match;
1289a3667aaeSNaresh Kumar Inna }
1290a3667aaeSNaresh Kumar Inna 
1291a3667aaeSNaresh Kumar Inna static int
csio_closest_thresh(struct csio_sge * s,int cnt)1292a3667aaeSNaresh Kumar Inna csio_closest_thresh(struct csio_sge *s, int cnt)
1293a3667aaeSNaresh Kumar Inna {
1294a3667aaeSNaresh Kumar Inna 	int i, delta, match = 0, min_delta = INT_MAX;
1295a3667aaeSNaresh Kumar Inna 
1296a3667aaeSNaresh Kumar Inna 	for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
1297a3667aaeSNaresh Kumar Inna 		delta = cnt - s->counter_val[i];
1298a3667aaeSNaresh Kumar Inna 		if (delta < 0)
1299a3667aaeSNaresh Kumar Inna 			delta = -delta;
1300a3667aaeSNaresh Kumar Inna 		if (delta < min_delta) {
1301a3667aaeSNaresh Kumar Inna 			min_delta = delta;
1302a3667aaeSNaresh Kumar Inna 			match = i;
1303a3667aaeSNaresh Kumar Inna 		}
1304a3667aaeSNaresh Kumar Inna 	}
1305a3667aaeSNaresh Kumar Inna 	return match;
1306a3667aaeSNaresh Kumar Inna }
1307a3667aaeSNaresh Kumar Inna 
1308a3667aaeSNaresh Kumar Inna static void
csio_wr_fixup_host_params(struct csio_hw * hw)1309a3667aaeSNaresh Kumar Inna csio_wr_fixup_host_params(struct csio_hw *hw)
1310a3667aaeSNaresh Kumar Inna {
1311a3667aaeSNaresh Kumar Inna 	struct csio_wrm *wrm = csio_hw_to_wrm(hw);
1312a3667aaeSNaresh Kumar Inna 	struct csio_sge *sge = &wrm->sge;
1313a3667aaeSNaresh Kumar Inna 	uint32_t clsz = L1_CACHE_BYTES;
1314a3667aaeSNaresh Kumar Inna 	uint32_t s_hps = PAGE_SHIFT - 10;
1315a3667aaeSNaresh Kumar Inna 	uint32_t stat_len = clsz > 64 ? 128 : 64;
1316db67befaSVarun Prakash 	u32 fl_align = clsz < 32 ? 32 : clsz;
1317db67befaSVarun Prakash 	u32 pack_align;
1318db67befaSVarun Prakash 	u32 ingpad, ingpack;
1319a3667aaeSNaresh Kumar Inna 
1320f612b815SHariprasad Shenai 	csio_wr_reg32(hw, HOSTPAGESIZEPF0_V(s_hps) | HOSTPAGESIZEPF1_V(s_hps) |
1321f612b815SHariprasad Shenai 		      HOSTPAGESIZEPF2_V(s_hps) | HOSTPAGESIZEPF3_V(s_hps) |
1322f612b815SHariprasad Shenai 		      HOSTPAGESIZEPF4_V(s_hps) | HOSTPAGESIZEPF5_V(s_hps) |
1323f612b815SHariprasad Shenai 		      HOSTPAGESIZEPF6_V(s_hps) | HOSTPAGESIZEPF7_V(s_hps),
1324f612b815SHariprasad Shenai 		      SGE_HOST_PAGE_SIZE_A);
1325a3667aaeSNaresh Kumar Inna 
1326db67befaSVarun Prakash 	/* T5 introduced the separation of the Free List Padding and
1327db67befaSVarun Prakash 	 * Packing Boundaries.  Thus, we can select a smaller Padding
1328db67befaSVarun Prakash 	 * Boundary to avoid uselessly chewing up PCIe Link and Memory
1329db67befaSVarun Prakash 	 * Bandwidth, and use a Packing Boundary which is large enough
1330db67befaSVarun Prakash 	 * to avoid false sharing between CPUs, etc.
1331db67befaSVarun Prakash 	 *
1332db67befaSVarun Prakash 	 * For the PCI Link, the smaller the Padding Boundary the
1333db67befaSVarun Prakash 	 * better.  For the Memory Controller, a smaller Padding
1334db67befaSVarun Prakash 	 * Boundary is better until we cross under the Memory Line
1335db67befaSVarun Prakash 	 * Size (the minimum unit of transfer to/from Memory).  If we
1336db67befaSVarun Prakash 	 * have a Padding Boundary which is smaller than the Memory
1337db67befaSVarun Prakash 	 * Line Size, that'll involve a Read-Modify-Write cycle on the
1338db67befaSVarun Prakash 	 * Memory Controller which is never good.
1339db67befaSVarun Prakash 	 */
1340db67befaSVarun Prakash 
1341db67befaSVarun Prakash 	/* We want the Packing Boundary to be based on the Cache Line
1342db67befaSVarun Prakash 	 * Size in order to help avoid False Sharing performance
1343db67befaSVarun Prakash 	 * issues between CPUs, etc.  We also want the Packing
1344db67befaSVarun Prakash 	 * Boundary to incorporate the PCI-E Maximum Payload Size.  We
1345db67befaSVarun Prakash 	 * get best performance when the Packing Boundary is a
1346db67befaSVarun Prakash 	 * multiple of the Maximum Payload Size.
1347db67befaSVarun Prakash 	 */
1348db67befaSVarun Prakash 	pack_align = fl_align;
13493e76ca95SFrederick Lawler 	if (pci_is_pcie(hw->pdev)) {
1350db67befaSVarun Prakash 		u32 mps, mps_log;
1351db67befaSVarun Prakash 		u16 devctl;
1352db67befaSVarun Prakash 
1353db67befaSVarun Prakash 		/* The PCIe Device Control Maximum Payload Size field
1354db67befaSVarun Prakash 		 * [bits 7:5] encodes sizes as powers of 2 starting at
1355db67befaSVarun Prakash 		 * 128 bytes.
1356db67befaSVarun Prakash 		 */
13573e76ca95SFrederick Lawler 		pcie_capability_read_word(hw->pdev, PCI_EXP_DEVCTL, &devctl);
1358db67befaSVarun Prakash 		mps_log = ((devctl & PCI_EXP_DEVCTL_PAYLOAD) >> 5) + 7;
1359db67befaSVarun Prakash 		mps = 1 << mps_log;
1360db67befaSVarun Prakash 		if (mps > pack_align)
1361db67befaSVarun Prakash 			pack_align = mps;
1362db67befaSVarun Prakash 	}
1363db67befaSVarun Prakash 
1364db67befaSVarun Prakash 	/* T5/T6 have a special interpretation of the "0"
1365db67befaSVarun Prakash 	 * value for the Packing Boundary.  This corresponds to 16
1366db67befaSVarun Prakash 	 * bytes instead of the expected 32 bytes.
1367db67befaSVarun Prakash 	 */
1368db67befaSVarun Prakash 	if (pack_align <= 16) {
1369db67befaSVarun Prakash 		ingpack = INGPACKBOUNDARY_16B_X;
1370db67befaSVarun Prakash 		fl_align = 16;
1371db67befaSVarun Prakash 	} else if (pack_align == 32) {
1372db67befaSVarun Prakash 		ingpack = INGPACKBOUNDARY_64B_X;
1373db67befaSVarun Prakash 		fl_align = 64;
1374db67befaSVarun Prakash 	} else {
1375db67befaSVarun Prakash 		u32 pack_align_log = fls(pack_align) - 1;
1376db67befaSVarun Prakash 
1377db67befaSVarun Prakash 		ingpack = pack_align_log - INGPACKBOUNDARY_SHIFT_X;
1378db67befaSVarun Prakash 		fl_align = pack_align;
1379db67befaSVarun Prakash 	}
1380db67befaSVarun Prakash 
1381db67befaSVarun Prakash 	/* Use the smallest Ingress Padding which isn't smaller than
1382db67befaSVarun Prakash 	 * the Memory Controller Read/Write Size.  We'll take that as
1383db67befaSVarun Prakash 	 * being 8 bytes since we don't know of any system with a
1384db67befaSVarun Prakash 	 * wider Memory Controller Bus Width.
1385db67befaSVarun Prakash 	 */
1386db67befaSVarun Prakash 	if (csio_is_t5(hw->pdev->device & CSIO_HW_CHIP_MASK))
1387db67befaSVarun Prakash 		ingpad = INGPADBOUNDARY_32B_X;
1388db67befaSVarun Prakash 	else
1389db67befaSVarun Prakash 		ingpad = T6_INGPADBOUNDARY_8B_X;
1390a3667aaeSNaresh Kumar Inna 
1391f612b815SHariprasad Shenai 	csio_set_reg_field(hw, SGE_CONTROL_A,
1392f612b815SHariprasad Shenai 			   INGPADBOUNDARY_V(INGPADBOUNDARY_M) |
1393f612b815SHariprasad Shenai 			   EGRSTATUSPAGESIZE_F,
1394f612b815SHariprasad Shenai 			   INGPADBOUNDARY_V(ingpad) |
1395f612b815SHariprasad Shenai 			   EGRSTATUSPAGESIZE_V(stat_len != 64));
1396db67befaSVarun Prakash 	csio_set_reg_field(hw, SGE_CONTROL2_A,
1397db67befaSVarun Prakash 			   INGPACKBOUNDARY_V(INGPACKBOUNDARY_M),
1398db67befaSVarun Prakash 			   INGPACKBOUNDARY_V(ingpack));
1399a3667aaeSNaresh Kumar Inna 
1400a3667aaeSNaresh Kumar Inna 	/* FL BUFFER SIZE#0 is Page size i,e already aligned to cache line */
1401f612b815SHariprasad Shenai 	csio_wr_reg32(hw, PAGE_SIZE, SGE_FL_BUFFER_SIZE0_A);
1402d69630e8SArvind Bhushan 
1403d69630e8SArvind Bhushan 	/*
1404d69630e8SArvind Bhushan 	 * If using hard params, the following will get set correctly
1405d69630e8SArvind Bhushan 	 * in csio_wr_set_sge().
1406d69630e8SArvind Bhushan 	 */
1407d69630e8SArvind Bhushan 	if (hw->flags & CSIO_HWF_USING_SOFT_PARAMS) {
1408a3667aaeSNaresh Kumar Inna 		csio_wr_reg32(hw,
1409f612b815SHariprasad Shenai 			(csio_rd_reg32(hw, SGE_FL_BUFFER_SIZE2_A) +
1410db67befaSVarun Prakash 			fl_align - 1) & ~(fl_align - 1),
1411f612b815SHariprasad Shenai 			SGE_FL_BUFFER_SIZE2_A);
1412a3667aaeSNaresh Kumar Inna 		csio_wr_reg32(hw,
1413f612b815SHariprasad Shenai 			(csio_rd_reg32(hw, SGE_FL_BUFFER_SIZE3_A) +
1414db67befaSVarun Prakash 			fl_align - 1) & ~(fl_align - 1),
1415f612b815SHariprasad Shenai 			SGE_FL_BUFFER_SIZE3_A);
1416d69630e8SArvind Bhushan 	}
1417a3667aaeSNaresh Kumar Inna 
1418db67befaSVarun Prakash 	sge->csio_fl_align = fl_align;
1419db67befaSVarun Prakash 
14200d804338SHariprasad Shenai 	csio_wr_reg32(hw, HPZ0_V(PAGE_SHIFT - 12), ULP_RX_TDDP_PSZ_A);
1421a3667aaeSNaresh Kumar Inna 
1422a3667aaeSNaresh Kumar Inna 	/* default value of rx_dma_offset of the NIC driver */
1423f612b815SHariprasad Shenai 	csio_set_reg_field(hw, SGE_CONTROL_A,
1424f612b815SHariprasad Shenai 			   PKTSHIFT_V(PKTSHIFT_M),
1425f612b815SHariprasad Shenai 			   PKTSHIFT_V(CSIO_SGE_RX_DMA_OFFSET));
14267cc16380SArvind Bhushan 
1427837e4a42SHariprasad Shenai 	csio_hw_tp_wr_bits_indirect(hw, TP_INGRESS_CONFIG_A,
1428837e4a42SHariprasad Shenai 				    CSUM_HAS_PSEUDO_HDR_F, 0);
1429a3667aaeSNaresh Kumar Inna }
1430a3667aaeSNaresh Kumar Inna 
1431a3667aaeSNaresh Kumar Inna static void
csio_init_intr_coalesce_parms(struct csio_hw * hw)1432a3667aaeSNaresh Kumar Inna csio_init_intr_coalesce_parms(struct csio_hw *hw)
1433a3667aaeSNaresh Kumar Inna {
1434a3667aaeSNaresh Kumar Inna 	struct csio_wrm *wrm = csio_hw_to_wrm(hw);
1435a3667aaeSNaresh Kumar Inna 	struct csio_sge *sge = &wrm->sge;
1436a3667aaeSNaresh Kumar Inna 
1437a3667aaeSNaresh Kumar Inna 	csio_sge_thresh_reg = csio_closest_thresh(sge, csio_intr_coalesce_cnt);
1438a3667aaeSNaresh Kumar Inna 	if (csio_intr_coalesce_cnt) {
1439a3667aaeSNaresh Kumar Inna 		csio_sge_thresh_reg = 0;
1440a3667aaeSNaresh Kumar Inna 		csio_sge_timer_reg = X_TIMERREG_RESTART_COUNTER;
1441a3667aaeSNaresh Kumar Inna 		return;
1442a3667aaeSNaresh Kumar Inna 	}
1443a3667aaeSNaresh Kumar Inna 
1444a3667aaeSNaresh Kumar Inna 	csio_sge_timer_reg = csio_closest_timer(sge, csio_intr_coalesce_time);
1445a3667aaeSNaresh Kumar Inna }
1446a3667aaeSNaresh Kumar Inna 
1447a3667aaeSNaresh Kumar Inna /*
1448a3667aaeSNaresh Kumar Inna  * csio_wr_get_sge - Get SGE register values.
1449a3667aaeSNaresh Kumar Inna  * @hw: HW module.
1450a3667aaeSNaresh Kumar Inna  *
1451a3667aaeSNaresh Kumar Inna  * Used by non-master functions and by master-functions relying on config file.
1452a3667aaeSNaresh Kumar Inna  */
1453a3667aaeSNaresh Kumar Inna static void
csio_wr_get_sge(struct csio_hw * hw)1454a3667aaeSNaresh Kumar Inna csio_wr_get_sge(struct csio_hw *hw)
1455a3667aaeSNaresh Kumar Inna {
1456a3667aaeSNaresh Kumar Inna 	struct csio_wrm *wrm = csio_hw_to_wrm(hw);
1457a3667aaeSNaresh Kumar Inna 	struct csio_sge *sge = &wrm->sge;
1458a3667aaeSNaresh Kumar Inna 	uint32_t ingpad;
1459a3667aaeSNaresh Kumar Inna 	int i;
1460a3667aaeSNaresh Kumar Inna 	u32 timer_value_0_and_1, timer_value_2_and_3, timer_value_4_and_5;
1461a3667aaeSNaresh Kumar Inna 	u32 ingress_rx_threshold;
1462a3667aaeSNaresh Kumar Inna 
1463f612b815SHariprasad Shenai 	sge->sge_control = csio_rd_reg32(hw, SGE_CONTROL_A);
1464a3667aaeSNaresh Kumar Inna 
1465f612b815SHariprasad Shenai 	ingpad = INGPADBOUNDARY_G(sge->sge_control);
1466a3667aaeSNaresh Kumar Inna 
1467a3667aaeSNaresh Kumar Inna 	switch (ingpad) {
1468a3667aaeSNaresh Kumar Inna 	case X_INGPCIEBOUNDARY_32B:
1469a3667aaeSNaresh Kumar Inna 		sge->csio_fl_align = 32; break;
1470a3667aaeSNaresh Kumar Inna 	case X_INGPCIEBOUNDARY_64B:
1471a3667aaeSNaresh Kumar Inna 		sge->csio_fl_align = 64; break;
1472a3667aaeSNaresh Kumar Inna 	case X_INGPCIEBOUNDARY_128B:
1473a3667aaeSNaresh Kumar Inna 		sge->csio_fl_align = 128; break;
1474a3667aaeSNaresh Kumar Inna 	case X_INGPCIEBOUNDARY_256B:
1475a3667aaeSNaresh Kumar Inna 		sge->csio_fl_align = 256; break;
1476a3667aaeSNaresh Kumar Inna 	case X_INGPCIEBOUNDARY_512B:
1477a3667aaeSNaresh Kumar Inna 		sge->csio_fl_align = 512; break;
1478a3667aaeSNaresh Kumar Inna 	case X_INGPCIEBOUNDARY_1024B:
1479a3667aaeSNaresh Kumar Inna 		sge->csio_fl_align = 1024; break;
1480a3667aaeSNaresh Kumar Inna 	case X_INGPCIEBOUNDARY_2048B:
1481a3667aaeSNaresh Kumar Inna 		sge->csio_fl_align = 2048; break;
1482a3667aaeSNaresh Kumar Inna 	case X_INGPCIEBOUNDARY_4096B:
1483a3667aaeSNaresh Kumar Inna 		sge->csio_fl_align = 4096; break;
1484a3667aaeSNaresh Kumar Inna 	}
1485a3667aaeSNaresh Kumar Inna 
1486a3667aaeSNaresh Kumar Inna 	for (i = 0; i < CSIO_SGE_FL_SIZE_REGS; i++)
1487a3667aaeSNaresh Kumar Inna 		csio_get_flbuf_size(hw, sge, i);
1488a3667aaeSNaresh Kumar Inna 
1489f061de42SHariprasad Shenai 	timer_value_0_and_1 = csio_rd_reg32(hw, SGE_TIMER_VALUE_0_AND_1_A);
1490f061de42SHariprasad Shenai 	timer_value_2_and_3 = csio_rd_reg32(hw, SGE_TIMER_VALUE_2_AND_3_A);
1491f061de42SHariprasad Shenai 	timer_value_4_and_5 = csio_rd_reg32(hw, SGE_TIMER_VALUE_4_AND_5_A);
1492a3667aaeSNaresh Kumar Inna 
1493a3667aaeSNaresh Kumar Inna 	sge->timer_val[0] = (uint16_t)csio_core_ticks_to_us(hw,
1494f061de42SHariprasad Shenai 					TIMERVALUE0_G(timer_value_0_and_1));
1495a3667aaeSNaresh Kumar Inna 	sge->timer_val[1] = (uint16_t)csio_core_ticks_to_us(hw,
1496f061de42SHariprasad Shenai 					TIMERVALUE1_G(timer_value_0_and_1));
1497a3667aaeSNaresh Kumar Inna 	sge->timer_val[2] = (uint16_t)csio_core_ticks_to_us(hw,
1498f061de42SHariprasad Shenai 					TIMERVALUE2_G(timer_value_2_and_3));
1499a3667aaeSNaresh Kumar Inna 	sge->timer_val[3] = (uint16_t)csio_core_ticks_to_us(hw,
1500f061de42SHariprasad Shenai 					TIMERVALUE3_G(timer_value_2_and_3));
1501a3667aaeSNaresh Kumar Inna 	sge->timer_val[4] = (uint16_t)csio_core_ticks_to_us(hw,
1502f061de42SHariprasad Shenai 					TIMERVALUE4_G(timer_value_4_and_5));
1503a3667aaeSNaresh Kumar Inna 	sge->timer_val[5] = (uint16_t)csio_core_ticks_to_us(hw,
1504f061de42SHariprasad Shenai 					TIMERVALUE5_G(timer_value_4_and_5));
1505a3667aaeSNaresh Kumar Inna 
1506f612b815SHariprasad Shenai 	ingress_rx_threshold = csio_rd_reg32(hw, SGE_INGRESS_RX_THRESHOLD_A);
1507f612b815SHariprasad Shenai 	sge->counter_val[0] = THRESHOLD_0_G(ingress_rx_threshold);
1508f612b815SHariprasad Shenai 	sge->counter_val[1] = THRESHOLD_1_G(ingress_rx_threshold);
1509f612b815SHariprasad Shenai 	sge->counter_val[2] = THRESHOLD_2_G(ingress_rx_threshold);
1510f612b815SHariprasad Shenai 	sge->counter_val[3] = THRESHOLD_3_G(ingress_rx_threshold);
1511a3667aaeSNaresh Kumar Inna 
1512a3667aaeSNaresh Kumar Inna 	csio_init_intr_coalesce_parms(hw);
1513a3667aaeSNaresh Kumar Inna }
1514a3667aaeSNaresh Kumar Inna 
1515a3667aaeSNaresh Kumar Inna /*
1516a3667aaeSNaresh Kumar Inna  * csio_wr_set_sge - Initialize SGE registers
1517a3667aaeSNaresh Kumar Inna  * @hw: HW module.
1518a3667aaeSNaresh Kumar Inna  *
1519a3667aaeSNaresh Kumar Inna  * Used by Master function to initialize SGE registers in the absence
1520a3667aaeSNaresh Kumar Inna  * of a config file.
1521a3667aaeSNaresh Kumar Inna  */
1522a3667aaeSNaresh Kumar Inna static void
csio_wr_set_sge(struct csio_hw * hw)1523a3667aaeSNaresh Kumar Inna csio_wr_set_sge(struct csio_hw *hw)
1524a3667aaeSNaresh Kumar Inna {
1525a3667aaeSNaresh Kumar Inna 	struct csio_wrm *wrm = csio_hw_to_wrm(hw);
1526a3667aaeSNaresh Kumar Inna 	struct csio_sge *sge = &wrm->sge;
1527a3667aaeSNaresh Kumar Inna 	int i;
1528a3667aaeSNaresh Kumar Inna 
1529a3667aaeSNaresh Kumar Inna 	/*
1530a3667aaeSNaresh Kumar Inna 	 * Set up our basic SGE mode to deliver CPL messages to our Ingress
1531a3667aaeSNaresh Kumar Inna 	 * Queue and Packet Date to the Free List.
1532a3667aaeSNaresh Kumar Inna 	 */
1533f612b815SHariprasad Shenai 	csio_set_reg_field(hw, SGE_CONTROL_A, RXPKTCPLMODE_F, RXPKTCPLMODE_F);
1534a3667aaeSNaresh Kumar Inna 
1535f612b815SHariprasad Shenai 	sge->sge_control = csio_rd_reg32(hw, SGE_CONTROL_A);
1536a3667aaeSNaresh Kumar Inna 
1537a3667aaeSNaresh Kumar Inna 	/* sge->csio_fl_align is set up by csio_wr_fixup_host_params(). */
1538a3667aaeSNaresh Kumar Inna 
1539a3667aaeSNaresh Kumar Inna 	/*
1540a3667aaeSNaresh Kumar Inna 	 * Set up to drop DOORBELL writes when the DOORBELL FIFO overflows
1541a3667aaeSNaresh Kumar Inna 	 * and generate an interrupt when this occurs so we can recover.
1542a3667aaeSNaresh Kumar Inna 	 */
1543f612b815SHariprasad Shenai 	csio_set_reg_field(hw, SGE_DBFIFO_STATUS_A,
15443fb4c22eSPraveen Madhavan 			   LP_INT_THRESH_T5_V(LP_INT_THRESH_T5_M),
15453fb4c22eSPraveen Madhavan 			   LP_INT_THRESH_T5_V(CSIO_SGE_DBFIFO_INT_THRESH));
15463fb4c22eSPraveen Madhavan 	csio_set_reg_field(hw, SGE_DBFIFO_STATUS2_A,
15473fb4c22eSPraveen Madhavan 			   HP_INT_THRESH_T5_V(LP_INT_THRESH_T5_M),
15483fb4c22eSPraveen Madhavan 			   HP_INT_THRESH_T5_V(CSIO_SGE_DBFIFO_INT_THRESH));
15497cc16380SArvind Bhushan 
1550f612b815SHariprasad Shenai 	csio_set_reg_field(hw, SGE_DOORBELL_CONTROL_A, ENABLE_DROP_F,
1551f612b815SHariprasad Shenai 			   ENABLE_DROP_F);
1552a3667aaeSNaresh Kumar Inna 
1553a3667aaeSNaresh Kumar Inna 	/* SGE_FL_BUFFER_SIZE0 is set up by csio_wr_fixup_host_params(). */
1554a3667aaeSNaresh Kumar Inna 
1555a3667aaeSNaresh Kumar Inna 	CSIO_SET_FLBUF_SIZE(hw, 1, CSIO_SGE_FLBUF_SIZE1);
1556d69630e8SArvind Bhushan 	csio_wr_reg32(hw, (CSIO_SGE_FLBUF_SIZE2 + sge->csio_fl_align - 1)
1557f612b815SHariprasad Shenai 		      & ~(sge->csio_fl_align - 1), SGE_FL_BUFFER_SIZE2_A);
1558d69630e8SArvind Bhushan 	csio_wr_reg32(hw, (CSIO_SGE_FLBUF_SIZE3 + sge->csio_fl_align - 1)
1559f612b815SHariprasad Shenai 		      & ~(sge->csio_fl_align - 1), SGE_FL_BUFFER_SIZE3_A);
1560a3667aaeSNaresh Kumar Inna 	CSIO_SET_FLBUF_SIZE(hw, 4, CSIO_SGE_FLBUF_SIZE4);
1561a3667aaeSNaresh Kumar Inna 	CSIO_SET_FLBUF_SIZE(hw, 5, CSIO_SGE_FLBUF_SIZE5);
1562a3667aaeSNaresh Kumar Inna 	CSIO_SET_FLBUF_SIZE(hw, 6, CSIO_SGE_FLBUF_SIZE6);
1563a3667aaeSNaresh Kumar Inna 	CSIO_SET_FLBUF_SIZE(hw, 7, CSIO_SGE_FLBUF_SIZE7);
1564a3667aaeSNaresh Kumar Inna 	CSIO_SET_FLBUF_SIZE(hw, 8, CSIO_SGE_FLBUF_SIZE8);
1565a3667aaeSNaresh Kumar Inna 
1566a3667aaeSNaresh Kumar Inna 	for (i = 0; i < CSIO_SGE_FL_SIZE_REGS; i++)
1567a3667aaeSNaresh Kumar Inna 		csio_get_flbuf_size(hw, sge, i);
1568a3667aaeSNaresh Kumar Inna 
1569a3667aaeSNaresh Kumar Inna 	/* Initialize interrupt coalescing attributes */
1570a3667aaeSNaresh Kumar Inna 	sge->timer_val[0] = CSIO_SGE_TIMER_VAL_0;
1571a3667aaeSNaresh Kumar Inna 	sge->timer_val[1] = CSIO_SGE_TIMER_VAL_1;
1572a3667aaeSNaresh Kumar Inna 	sge->timer_val[2] = CSIO_SGE_TIMER_VAL_2;
1573a3667aaeSNaresh Kumar Inna 	sge->timer_val[3] = CSIO_SGE_TIMER_VAL_3;
1574a3667aaeSNaresh Kumar Inna 	sge->timer_val[4] = CSIO_SGE_TIMER_VAL_4;
1575a3667aaeSNaresh Kumar Inna 	sge->timer_val[5] = CSIO_SGE_TIMER_VAL_5;
1576a3667aaeSNaresh Kumar Inna 
1577a3667aaeSNaresh Kumar Inna 	sge->counter_val[0] = CSIO_SGE_INT_CNT_VAL_0;
1578a3667aaeSNaresh Kumar Inna 	sge->counter_val[1] = CSIO_SGE_INT_CNT_VAL_1;
1579a3667aaeSNaresh Kumar Inna 	sge->counter_val[2] = CSIO_SGE_INT_CNT_VAL_2;
1580a3667aaeSNaresh Kumar Inna 	sge->counter_val[3] = CSIO_SGE_INT_CNT_VAL_3;
1581a3667aaeSNaresh Kumar Inna 
1582f612b815SHariprasad Shenai 	csio_wr_reg32(hw, THRESHOLD_0_V(sge->counter_val[0]) |
1583f612b815SHariprasad Shenai 		      THRESHOLD_1_V(sge->counter_val[1]) |
1584f612b815SHariprasad Shenai 		      THRESHOLD_2_V(sge->counter_val[2]) |
1585f612b815SHariprasad Shenai 		      THRESHOLD_3_V(sge->counter_val[3]),
1586f612b815SHariprasad Shenai 		      SGE_INGRESS_RX_THRESHOLD_A);
1587a3667aaeSNaresh Kumar Inna 
1588a3667aaeSNaresh Kumar Inna 	csio_wr_reg32(hw,
1589f061de42SHariprasad Shenai 		   TIMERVALUE0_V(csio_us_to_core_ticks(hw, sge->timer_val[0])) |
1590f061de42SHariprasad Shenai 		   TIMERVALUE1_V(csio_us_to_core_ticks(hw, sge->timer_val[1])),
1591f061de42SHariprasad Shenai 		   SGE_TIMER_VALUE_0_AND_1_A);
1592a3667aaeSNaresh Kumar Inna 
1593a3667aaeSNaresh Kumar Inna 	csio_wr_reg32(hw,
1594f061de42SHariprasad Shenai 		   TIMERVALUE2_V(csio_us_to_core_ticks(hw, sge->timer_val[2])) |
1595f061de42SHariprasad Shenai 		   TIMERVALUE3_V(csio_us_to_core_ticks(hw, sge->timer_val[3])),
1596f061de42SHariprasad Shenai 		   SGE_TIMER_VALUE_2_AND_3_A);
1597a3667aaeSNaresh Kumar Inna 
1598a3667aaeSNaresh Kumar Inna 	csio_wr_reg32(hw,
1599f061de42SHariprasad Shenai 		   TIMERVALUE4_V(csio_us_to_core_ticks(hw, sge->timer_val[4])) |
1600f061de42SHariprasad Shenai 		   TIMERVALUE5_V(csio_us_to_core_ticks(hw, sge->timer_val[5])),
1601f061de42SHariprasad Shenai 		   SGE_TIMER_VALUE_4_AND_5_A);
1602a3667aaeSNaresh Kumar Inna 
1603a3667aaeSNaresh Kumar Inna 	csio_init_intr_coalesce_parms(hw);
1604a3667aaeSNaresh Kumar Inna }
1605a3667aaeSNaresh Kumar Inna 
1606a3667aaeSNaresh Kumar Inna void
csio_wr_sge_init(struct csio_hw * hw)1607a3667aaeSNaresh Kumar Inna csio_wr_sge_init(struct csio_hw *hw)
1608a3667aaeSNaresh Kumar Inna {
1609a3667aaeSNaresh Kumar Inna 	/*
1610d69630e8SArvind Bhushan 	 * If we are master and chip is not initialized:
1611a3667aaeSNaresh Kumar Inna 	 *    - If we plan to use the config file, we need to fixup some
1612a3667aaeSNaresh Kumar Inna 	 *      host specific registers, and read the rest of the SGE
1613a3667aaeSNaresh Kumar Inna 	 *      configuration.
1614a3667aaeSNaresh Kumar Inna 	 *    - If we dont plan to use the config file, we need to initialize
1615a3667aaeSNaresh Kumar Inna 	 *      SGE entirely, including fixing the host specific registers.
1616d69630e8SArvind Bhushan 	 * If we are master and chip is initialized, just read and work off of
1617d69630e8SArvind Bhushan 	 *	the already initialized SGE values.
1618a3667aaeSNaresh Kumar Inna 	 * If we arent the master, we are only allowed to read and work off of
1619a3667aaeSNaresh Kumar Inna 	 *      the already initialized SGE values.
1620a3667aaeSNaresh Kumar Inna 	 *
1621a3667aaeSNaresh Kumar Inna 	 * Therefore, before calling this function, we assume that the master-
1622d69630e8SArvind Bhushan 	 * ship of the card, state and whether to use config file or not, have
1623d69630e8SArvind Bhushan 	 * already been decided.
1624a3667aaeSNaresh Kumar Inna 	 */
1625a3667aaeSNaresh Kumar Inna 	if (csio_is_hw_master(hw)) {
1626d69630e8SArvind Bhushan 		if (hw->fw_state != CSIO_DEV_STATE_INIT)
1627a3667aaeSNaresh Kumar Inna 			csio_wr_fixup_host_params(hw);
1628a3667aaeSNaresh Kumar Inna 
1629a3667aaeSNaresh Kumar Inna 		if (hw->flags & CSIO_HWF_USING_SOFT_PARAMS)
1630a3667aaeSNaresh Kumar Inna 			csio_wr_get_sge(hw);
1631a3667aaeSNaresh Kumar Inna 		else
1632a3667aaeSNaresh Kumar Inna 			csio_wr_set_sge(hw);
1633a3667aaeSNaresh Kumar Inna 	} else
1634a3667aaeSNaresh Kumar Inna 		csio_wr_get_sge(hw);
1635a3667aaeSNaresh Kumar Inna }
1636a3667aaeSNaresh Kumar Inna 
1637a3667aaeSNaresh Kumar Inna /*
1638a3667aaeSNaresh Kumar Inna  * csio_wrm_init - Initialize Work request module.
1639a3667aaeSNaresh Kumar Inna  * @wrm: WR module
1640a3667aaeSNaresh Kumar Inna  * @hw: HW pointer
1641a3667aaeSNaresh Kumar Inna  *
1642a3667aaeSNaresh Kumar Inna  * Allocates memory for an array of queue pointers starting at q_arr.
1643a3667aaeSNaresh Kumar Inna  */
1644a3667aaeSNaresh Kumar Inna int
csio_wrm_init(struct csio_wrm * wrm,struct csio_hw * hw)1645a3667aaeSNaresh Kumar Inna csio_wrm_init(struct csio_wrm *wrm, struct csio_hw *hw)
1646a3667aaeSNaresh Kumar Inna {
1647a3667aaeSNaresh Kumar Inna 	int i;
1648a3667aaeSNaresh Kumar Inna 
1649a3667aaeSNaresh Kumar Inna 	if (!wrm->num_q) {
1650a3667aaeSNaresh Kumar Inna 		csio_err(hw, "Num queues is not set\n");
1651a3667aaeSNaresh Kumar Inna 		return -EINVAL;
1652a3667aaeSNaresh Kumar Inna 	}
1653a3667aaeSNaresh Kumar Inna 
16546396bb22SKees Cook 	wrm->q_arr = kcalloc(wrm->num_q, sizeof(struct csio_q *), GFP_KERNEL);
1655a3667aaeSNaresh Kumar Inna 	if (!wrm->q_arr)
1656a3667aaeSNaresh Kumar Inna 		goto err;
1657a3667aaeSNaresh Kumar Inna 
1658a3667aaeSNaresh Kumar Inna 	for (i = 0; i < wrm->num_q; i++) {
1659a3667aaeSNaresh Kumar Inna 		wrm->q_arr[i] = kzalloc(sizeof(struct csio_q), GFP_KERNEL);
1660a3667aaeSNaresh Kumar Inna 		if (!wrm->q_arr[i]) {
1661a3667aaeSNaresh Kumar Inna 			while (--i >= 0)
1662a3667aaeSNaresh Kumar Inna 				kfree(wrm->q_arr[i]);
1663a3667aaeSNaresh Kumar Inna 			goto err_free_arr;
1664a3667aaeSNaresh Kumar Inna 		}
1665a3667aaeSNaresh Kumar Inna 	}
1666a3667aaeSNaresh Kumar Inna 	wrm->free_qidx	= 0;
1667a3667aaeSNaresh Kumar Inna 
1668a3667aaeSNaresh Kumar Inna 	return 0;
1669a3667aaeSNaresh Kumar Inna 
1670a3667aaeSNaresh Kumar Inna err_free_arr:
1671a3667aaeSNaresh Kumar Inna 	kfree(wrm->q_arr);
1672a3667aaeSNaresh Kumar Inna err:
1673a3667aaeSNaresh Kumar Inna 	return -ENOMEM;
1674a3667aaeSNaresh Kumar Inna }
1675a3667aaeSNaresh Kumar Inna 
1676a3667aaeSNaresh Kumar Inna /*
1677a3667aaeSNaresh Kumar Inna  * csio_wrm_exit - Initialize Work request module.
1678a3667aaeSNaresh Kumar Inna  * @wrm: WR module
1679a3667aaeSNaresh Kumar Inna  * @hw: HW module
1680a3667aaeSNaresh Kumar Inna  *
1681a3667aaeSNaresh Kumar Inna  * Uninitialize WR module. Free q_arr and pointers in it.
1682a3667aaeSNaresh Kumar Inna  * We have the additional job of freeing the DMA memory associated
1683a3667aaeSNaresh Kumar Inna  * with the queues.
1684a3667aaeSNaresh Kumar Inna  */
1685a3667aaeSNaresh Kumar Inna void
csio_wrm_exit(struct csio_wrm * wrm,struct csio_hw * hw)1686a3667aaeSNaresh Kumar Inna csio_wrm_exit(struct csio_wrm *wrm, struct csio_hw *hw)
1687a3667aaeSNaresh Kumar Inna {
1688a3667aaeSNaresh Kumar Inna 	int i;
1689a3667aaeSNaresh Kumar Inna 	uint32_t j;
1690a3667aaeSNaresh Kumar Inna 	struct csio_q *q;
1691a3667aaeSNaresh Kumar Inna 	struct csio_dma_buf *buf;
1692a3667aaeSNaresh Kumar Inna 
1693a3667aaeSNaresh Kumar Inna 	for (i = 0; i < wrm->num_q; i++) {
1694a3667aaeSNaresh Kumar Inna 		q = wrm->q_arr[i];
1695a3667aaeSNaresh Kumar Inna 
1696a3667aaeSNaresh Kumar Inna 		if (wrm->free_qidx && (i < wrm->free_qidx)) {
1697a3667aaeSNaresh Kumar Inna 			if (q->type == CSIO_FREELIST) {
1698a3667aaeSNaresh Kumar Inna 				if (!q->un.fl.bufs)
1699a3667aaeSNaresh Kumar Inna 					continue;
1700a3667aaeSNaresh Kumar Inna 				for (j = 0; j < q->credits; j++) {
1701a3667aaeSNaresh Kumar Inna 					buf = &q->un.fl.bufs[j];
1702a3667aaeSNaresh Kumar Inna 					if (!buf->vaddr)
1703a3667aaeSNaresh Kumar Inna 						continue;
1704c22b332dSChristoph Hellwig 					dma_free_coherent(&hw->pdev->dev,
1705c22b332dSChristoph Hellwig 							buf->len, buf->vaddr,
1706a3667aaeSNaresh Kumar Inna 							buf->paddr);
1707a3667aaeSNaresh Kumar Inna 				}
1708a3667aaeSNaresh Kumar Inna 				kfree(q->un.fl.bufs);
1709a3667aaeSNaresh Kumar Inna 			}
1710c22b332dSChristoph Hellwig 			dma_free_coherent(&hw->pdev->dev, q->size,
1711a3667aaeSNaresh Kumar Inna 					q->vstart, q->pstart);
1712a3667aaeSNaresh Kumar Inna 		}
1713a3667aaeSNaresh Kumar Inna 		kfree(q);
1714a3667aaeSNaresh Kumar Inna 	}
1715a3667aaeSNaresh Kumar Inna 
1716a3667aaeSNaresh Kumar Inna 	hw->flags &= ~CSIO_HWF_Q_MEM_ALLOCED;
1717a3667aaeSNaresh Kumar Inna 
1718a3667aaeSNaresh Kumar Inna 	kfree(wrm->q_arr);
1719a3667aaeSNaresh Kumar Inna }
1720