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