xref: /OK3568_Linux_fs/kernel/drivers/infiniband/hw/hfi1/pio.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright(c) 2015-2018 Intel Corporation.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * This file is provided under a dual BSD/GPLv2 license.  When using or
5*4882a593Smuzhiyun  * redistributing this file, you may do so under either license.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * GPL LICENSE SUMMARY
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
10*4882a593Smuzhiyun  * it under the terms of version 2 of the GNU General Public License as
11*4882a593Smuzhiyun  * published by the Free Software Foundation.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * This program is distributed in the hope that it will be useful, but
14*4882a593Smuzhiyun  * WITHOUT ANY WARRANTY; without even the implied warranty of
15*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16*4882a593Smuzhiyun  * General Public License for more details.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * BSD LICENSE
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
21*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions
22*4882a593Smuzhiyun  * are met:
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  *  - Redistributions of source code must retain the above copyright
25*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer.
26*4882a593Smuzhiyun  *  - Redistributions in binary form must reproduce the above copyright
27*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer in
28*4882a593Smuzhiyun  *    the documentation and/or other materials provided with the
29*4882a593Smuzhiyun  *    distribution.
30*4882a593Smuzhiyun  *  - Neither the name of Intel Corporation nor the names of its
31*4882a593Smuzhiyun  *    contributors may be used to endorse or promote products derived
32*4882a593Smuzhiyun  *    from this software without specific prior written permission.
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35*4882a593Smuzhiyun  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36*4882a593Smuzhiyun  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37*4882a593Smuzhiyun  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38*4882a593Smuzhiyun  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39*4882a593Smuzhiyun  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40*4882a593Smuzhiyun  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41*4882a593Smuzhiyun  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42*4882a593Smuzhiyun  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44*4882a593Smuzhiyun  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  */
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #include <linux/delay.h>
49*4882a593Smuzhiyun #include "hfi.h"
50*4882a593Smuzhiyun #include "qp.h"
51*4882a593Smuzhiyun #include "trace.h"
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun #define SC(name) SEND_CTXT_##name
54*4882a593Smuzhiyun /*
55*4882a593Smuzhiyun  * Send Context functions
56*4882a593Smuzhiyun  */
57*4882a593Smuzhiyun static void sc_wait_for_packet_egress(struct send_context *sc, int pause);
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun /*
60*4882a593Smuzhiyun  * Set the CM reset bit and wait for it to clear.  Use the provided
61*4882a593Smuzhiyun  * sendctrl register.  This routine has no locking.
62*4882a593Smuzhiyun  */
__cm_reset(struct hfi1_devdata * dd,u64 sendctrl)63*4882a593Smuzhiyun void __cm_reset(struct hfi1_devdata *dd, u64 sendctrl)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	write_csr(dd, SEND_CTRL, sendctrl | SEND_CTRL_CM_RESET_SMASK);
66*4882a593Smuzhiyun 	while (1) {
67*4882a593Smuzhiyun 		udelay(1);
68*4882a593Smuzhiyun 		sendctrl = read_csr(dd, SEND_CTRL);
69*4882a593Smuzhiyun 		if ((sendctrl & SEND_CTRL_CM_RESET_SMASK) == 0)
70*4882a593Smuzhiyun 			break;
71*4882a593Smuzhiyun 	}
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun /* global control of PIO send */
pio_send_control(struct hfi1_devdata * dd,int op)75*4882a593Smuzhiyun void pio_send_control(struct hfi1_devdata *dd, int op)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	u64 reg, mask;
78*4882a593Smuzhiyun 	unsigned long flags;
79*4882a593Smuzhiyun 	int write = 1;	/* write sendctrl back */
80*4882a593Smuzhiyun 	int flush = 0;	/* re-read sendctrl to make sure it is flushed */
81*4882a593Smuzhiyun 	int i;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	spin_lock_irqsave(&dd->sendctrl_lock, flags);
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	reg = read_csr(dd, SEND_CTRL);
86*4882a593Smuzhiyun 	switch (op) {
87*4882a593Smuzhiyun 	case PSC_GLOBAL_ENABLE:
88*4882a593Smuzhiyun 		reg |= SEND_CTRL_SEND_ENABLE_SMASK;
89*4882a593Smuzhiyun 		fallthrough;
90*4882a593Smuzhiyun 	case PSC_DATA_VL_ENABLE:
91*4882a593Smuzhiyun 		mask = 0;
92*4882a593Smuzhiyun 		for (i = 0; i < ARRAY_SIZE(dd->vld); i++)
93*4882a593Smuzhiyun 			if (!dd->vld[i].mtu)
94*4882a593Smuzhiyun 				mask |= BIT_ULL(i);
95*4882a593Smuzhiyun 		/* Disallow sending on VLs not enabled */
96*4882a593Smuzhiyun 		mask = (mask & SEND_CTRL_UNSUPPORTED_VL_MASK) <<
97*4882a593Smuzhiyun 			SEND_CTRL_UNSUPPORTED_VL_SHIFT;
98*4882a593Smuzhiyun 		reg = (reg & ~SEND_CTRL_UNSUPPORTED_VL_SMASK) | mask;
99*4882a593Smuzhiyun 		break;
100*4882a593Smuzhiyun 	case PSC_GLOBAL_DISABLE:
101*4882a593Smuzhiyun 		reg &= ~SEND_CTRL_SEND_ENABLE_SMASK;
102*4882a593Smuzhiyun 		break;
103*4882a593Smuzhiyun 	case PSC_GLOBAL_VLARB_ENABLE:
104*4882a593Smuzhiyun 		reg |= SEND_CTRL_VL_ARBITER_ENABLE_SMASK;
105*4882a593Smuzhiyun 		break;
106*4882a593Smuzhiyun 	case PSC_GLOBAL_VLARB_DISABLE:
107*4882a593Smuzhiyun 		reg &= ~SEND_CTRL_VL_ARBITER_ENABLE_SMASK;
108*4882a593Smuzhiyun 		break;
109*4882a593Smuzhiyun 	case PSC_CM_RESET:
110*4882a593Smuzhiyun 		__cm_reset(dd, reg);
111*4882a593Smuzhiyun 		write = 0; /* CSR already written (and flushed) */
112*4882a593Smuzhiyun 		break;
113*4882a593Smuzhiyun 	case PSC_DATA_VL_DISABLE:
114*4882a593Smuzhiyun 		reg |= SEND_CTRL_UNSUPPORTED_VL_SMASK;
115*4882a593Smuzhiyun 		flush = 1;
116*4882a593Smuzhiyun 		break;
117*4882a593Smuzhiyun 	default:
118*4882a593Smuzhiyun 		dd_dev_err(dd, "%s: invalid control %d\n", __func__, op);
119*4882a593Smuzhiyun 		break;
120*4882a593Smuzhiyun 	}
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	if (write) {
123*4882a593Smuzhiyun 		write_csr(dd, SEND_CTRL, reg);
124*4882a593Smuzhiyun 		if (flush)
125*4882a593Smuzhiyun 			(void)read_csr(dd, SEND_CTRL); /* flush write */
126*4882a593Smuzhiyun 	}
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dd->sendctrl_lock, flags);
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun /* number of send context memory pools */
132*4882a593Smuzhiyun #define NUM_SC_POOLS 2
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun /* Send Context Size (SCS) wildcards */
135*4882a593Smuzhiyun #define SCS_POOL_0 -1
136*4882a593Smuzhiyun #define SCS_POOL_1 -2
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun /* Send Context Count (SCC) wildcards */
139*4882a593Smuzhiyun #define SCC_PER_VL -1
140*4882a593Smuzhiyun #define SCC_PER_CPU  -2
141*4882a593Smuzhiyun #define SCC_PER_KRCVQ  -3
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun /* Send Context Size (SCS) constants */
144*4882a593Smuzhiyun #define SCS_ACK_CREDITS  32
145*4882a593Smuzhiyun #define SCS_VL15_CREDITS 102	/* 3 pkts of 2048B data + 128B header */
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun #define PIO_THRESHOLD_CEILING 4096
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun #define PIO_WAIT_BATCH_SIZE 5
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun /* default send context sizes */
152*4882a593Smuzhiyun static struct sc_config_sizes sc_config_sizes[SC_MAX] = {
153*4882a593Smuzhiyun 	[SC_KERNEL] = { .size  = SCS_POOL_0,	/* even divide, pool 0 */
154*4882a593Smuzhiyun 			.count = SCC_PER_VL },	/* one per NUMA */
155*4882a593Smuzhiyun 	[SC_ACK]    = { .size  = SCS_ACK_CREDITS,
156*4882a593Smuzhiyun 			.count = SCC_PER_KRCVQ },
157*4882a593Smuzhiyun 	[SC_USER]   = { .size  = SCS_POOL_0,	/* even divide, pool 0 */
158*4882a593Smuzhiyun 			.count = SCC_PER_CPU },	/* one per CPU */
159*4882a593Smuzhiyun 	[SC_VL15]   = { .size  = SCS_VL15_CREDITS,
160*4882a593Smuzhiyun 			.count = 1 },
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun };
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun /* send context memory pool configuration */
165*4882a593Smuzhiyun struct mem_pool_config {
166*4882a593Smuzhiyun 	int centipercent;	/* % of memory, in 100ths of 1% */
167*4882a593Smuzhiyun 	int absolute_blocks;	/* absolute block count */
168*4882a593Smuzhiyun };
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun /* default memory pool configuration: 100% in pool 0 */
171*4882a593Smuzhiyun static struct mem_pool_config sc_mem_pool_config[NUM_SC_POOLS] = {
172*4882a593Smuzhiyun 	/* centi%, abs blocks */
173*4882a593Smuzhiyun 	{  10000,     -1 },		/* pool 0 */
174*4882a593Smuzhiyun 	{      0,     -1 },		/* pool 1 */
175*4882a593Smuzhiyun };
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun /* memory pool information, used when calculating final sizes */
178*4882a593Smuzhiyun struct mem_pool_info {
179*4882a593Smuzhiyun 	int centipercent;	/*
180*4882a593Smuzhiyun 				 * 100th of 1% of memory to use, -1 if blocks
181*4882a593Smuzhiyun 				 * already set
182*4882a593Smuzhiyun 				 */
183*4882a593Smuzhiyun 	int count;		/* count of contexts in the pool */
184*4882a593Smuzhiyun 	int blocks;		/* block size of the pool */
185*4882a593Smuzhiyun 	int size;		/* context size, in blocks */
186*4882a593Smuzhiyun };
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun /*
189*4882a593Smuzhiyun  * Convert a pool wildcard to a valid pool index.  The wildcards
190*4882a593Smuzhiyun  * start at -1 and increase negatively.  Map them as:
191*4882a593Smuzhiyun  *	-1 => 0
192*4882a593Smuzhiyun  *	-2 => 1
193*4882a593Smuzhiyun  *	etc.
194*4882a593Smuzhiyun  *
195*4882a593Smuzhiyun  * Return -1 on non-wildcard input, otherwise convert to a pool number.
196*4882a593Smuzhiyun  */
wildcard_to_pool(int wc)197*4882a593Smuzhiyun static int wildcard_to_pool(int wc)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun 	if (wc >= 0)
200*4882a593Smuzhiyun 		return -1;	/* non-wildcard */
201*4882a593Smuzhiyun 	return -wc - 1;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun static const char *sc_type_names[SC_MAX] = {
205*4882a593Smuzhiyun 	"kernel",
206*4882a593Smuzhiyun 	"ack",
207*4882a593Smuzhiyun 	"user",
208*4882a593Smuzhiyun 	"vl15"
209*4882a593Smuzhiyun };
210*4882a593Smuzhiyun 
sc_type_name(int index)211*4882a593Smuzhiyun static const char *sc_type_name(int index)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun 	if (index < 0 || index >= SC_MAX)
214*4882a593Smuzhiyun 		return "unknown";
215*4882a593Smuzhiyun 	return sc_type_names[index];
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun /*
219*4882a593Smuzhiyun  * Read the send context memory pool configuration and send context
220*4882a593Smuzhiyun  * size configuration.  Replace any wildcards and come up with final
221*4882a593Smuzhiyun  * counts and sizes for the send context types.
222*4882a593Smuzhiyun  */
init_sc_pools_and_sizes(struct hfi1_devdata * dd)223*4882a593Smuzhiyun int init_sc_pools_and_sizes(struct hfi1_devdata *dd)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun 	struct mem_pool_info mem_pool_info[NUM_SC_POOLS] = { { 0 } };
226*4882a593Smuzhiyun 	int total_blocks = (chip_pio_mem_size(dd) / PIO_BLOCK_SIZE) - 1;
227*4882a593Smuzhiyun 	int total_contexts = 0;
228*4882a593Smuzhiyun 	int fixed_blocks;
229*4882a593Smuzhiyun 	int pool_blocks;
230*4882a593Smuzhiyun 	int used_blocks;
231*4882a593Smuzhiyun 	int cp_total;		/* centipercent total */
232*4882a593Smuzhiyun 	int ab_total;		/* absolute block total */
233*4882a593Smuzhiyun 	int extra;
234*4882a593Smuzhiyun 	int i;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	/*
237*4882a593Smuzhiyun 	 * When SDMA is enabled, kernel context pio packet size is capped by
238*4882a593Smuzhiyun 	 * "piothreshold". Reduce pio buffer allocation for kernel context by
239*4882a593Smuzhiyun 	 * setting it to a fixed size. The allocation allows 3-deep buffering
240*4882a593Smuzhiyun 	 * of the largest pio packets plus up to 128 bytes header, sufficient
241*4882a593Smuzhiyun 	 * to maintain verbs performance.
242*4882a593Smuzhiyun 	 *
243*4882a593Smuzhiyun 	 * When SDMA is disabled, keep the default pooling allocation.
244*4882a593Smuzhiyun 	 */
245*4882a593Smuzhiyun 	if (HFI1_CAP_IS_KSET(SDMA)) {
246*4882a593Smuzhiyun 		u16 max_pkt_size = (piothreshold < PIO_THRESHOLD_CEILING) ?
247*4882a593Smuzhiyun 					 piothreshold : PIO_THRESHOLD_CEILING;
248*4882a593Smuzhiyun 		sc_config_sizes[SC_KERNEL].size =
249*4882a593Smuzhiyun 			3 * (max_pkt_size + 128) / PIO_BLOCK_SIZE;
250*4882a593Smuzhiyun 	}
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	/*
253*4882a593Smuzhiyun 	 * Step 0:
254*4882a593Smuzhiyun 	 *	- copy the centipercents/absolute sizes from the pool config
255*4882a593Smuzhiyun 	 *	- sanity check these values
256*4882a593Smuzhiyun 	 *	- add up centipercents, then later check for full value
257*4882a593Smuzhiyun 	 *	- add up absolute blocks, then later check for over-commit
258*4882a593Smuzhiyun 	 */
259*4882a593Smuzhiyun 	cp_total = 0;
260*4882a593Smuzhiyun 	ab_total = 0;
261*4882a593Smuzhiyun 	for (i = 0; i < NUM_SC_POOLS; i++) {
262*4882a593Smuzhiyun 		int cp = sc_mem_pool_config[i].centipercent;
263*4882a593Smuzhiyun 		int ab = sc_mem_pool_config[i].absolute_blocks;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 		/*
266*4882a593Smuzhiyun 		 * A negative value is "unused" or "invalid".  Both *can*
267*4882a593Smuzhiyun 		 * be valid, but centipercent wins, so check that first
268*4882a593Smuzhiyun 		 */
269*4882a593Smuzhiyun 		if (cp >= 0) {			/* centipercent valid */
270*4882a593Smuzhiyun 			cp_total += cp;
271*4882a593Smuzhiyun 		} else if (ab >= 0) {		/* absolute blocks valid */
272*4882a593Smuzhiyun 			ab_total += ab;
273*4882a593Smuzhiyun 		} else {			/* neither valid */
274*4882a593Smuzhiyun 			dd_dev_err(
275*4882a593Smuzhiyun 				dd,
276*4882a593Smuzhiyun 				"Send context memory pool %d: both the block count and centipercent are invalid\n",
277*4882a593Smuzhiyun 				i);
278*4882a593Smuzhiyun 			return -EINVAL;
279*4882a593Smuzhiyun 		}
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 		mem_pool_info[i].centipercent = cp;
282*4882a593Smuzhiyun 		mem_pool_info[i].blocks = ab;
283*4882a593Smuzhiyun 	}
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	/* do not use both % and absolute blocks for different pools */
286*4882a593Smuzhiyun 	if (cp_total != 0 && ab_total != 0) {
287*4882a593Smuzhiyun 		dd_dev_err(
288*4882a593Smuzhiyun 			dd,
289*4882a593Smuzhiyun 			"All send context memory pools must be described as either centipercent or blocks, no mixing between pools\n");
290*4882a593Smuzhiyun 		return -EINVAL;
291*4882a593Smuzhiyun 	}
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	/* if any percentages are present, they must add up to 100% x 100 */
294*4882a593Smuzhiyun 	if (cp_total != 0 && cp_total != 10000) {
295*4882a593Smuzhiyun 		dd_dev_err(
296*4882a593Smuzhiyun 			dd,
297*4882a593Smuzhiyun 			"Send context memory pool centipercent is %d, expecting 10000\n",
298*4882a593Smuzhiyun 			cp_total);
299*4882a593Smuzhiyun 		return -EINVAL;
300*4882a593Smuzhiyun 	}
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	/* the absolute pool total cannot be more than the mem total */
303*4882a593Smuzhiyun 	if (ab_total > total_blocks) {
304*4882a593Smuzhiyun 		dd_dev_err(
305*4882a593Smuzhiyun 			dd,
306*4882a593Smuzhiyun 			"Send context memory pool absolute block count %d is larger than the memory size %d\n",
307*4882a593Smuzhiyun 			ab_total, total_blocks);
308*4882a593Smuzhiyun 		return -EINVAL;
309*4882a593Smuzhiyun 	}
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	/*
312*4882a593Smuzhiyun 	 * Step 2:
313*4882a593Smuzhiyun 	 *	- copy from the context size config
314*4882a593Smuzhiyun 	 *	- replace context type wildcard counts with real values
315*4882a593Smuzhiyun 	 *	- add up non-memory pool block sizes
316*4882a593Smuzhiyun 	 *	- add up memory pool user counts
317*4882a593Smuzhiyun 	 */
318*4882a593Smuzhiyun 	fixed_blocks = 0;
319*4882a593Smuzhiyun 	for (i = 0; i < SC_MAX; i++) {
320*4882a593Smuzhiyun 		int count = sc_config_sizes[i].count;
321*4882a593Smuzhiyun 		int size = sc_config_sizes[i].size;
322*4882a593Smuzhiyun 		int pool;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 		/*
325*4882a593Smuzhiyun 		 * Sanity check count: Either a positive value or
326*4882a593Smuzhiyun 		 * one of the expected wildcards is valid.  The positive
327*4882a593Smuzhiyun 		 * value is checked later when we compare against total
328*4882a593Smuzhiyun 		 * memory available.
329*4882a593Smuzhiyun 		 */
330*4882a593Smuzhiyun 		if (i == SC_ACK) {
331*4882a593Smuzhiyun 			count = dd->n_krcv_queues;
332*4882a593Smuzhiyun 		} else if (i == SC_KERNEL) {
333*4882a593Smuzhiyun 			count = INIT_SC_PER_VL * num_vls;
334*4882a593Smuzhiyun 		} else if (count == SCC_PER_CPU) {
335*4882a593Smuzhiyun 			count = dd->num_rcv_contexts - dd->n_krcv_queues;
336*4882a593Smuzhiyun 		} else if (count < 0) {
337*4882a593Smuzhiyun 			dd_dev_err(
338*4882a593Smuzhiyun 				dd,
339*4882a593Smuzhiyun 				"%s send context invalid count wildcard %d\n",
340*4882a593Smuzhiyun 				sc_type_name(i), count);
341*4882a593Smuzhiyun 			return -EINVAL;
342*4882a593Smuzhiyun 		}
343*4882a593Smuzhiyun 		if (total_contexts + count > chip_send_contexts(dd))
344*4882a593Smuzhiyun 			count = chip_send_contexts(dd) - total_contexts;
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 		total_contexts += count;
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 		/*
349*4882a593Smuzhiyun 		 * Sanity check pool: The conversion will return a pool
350*4882a593Smuzhiyun 		 * number or -1 if a fixed (non-negative) value.  The fixed
351*4882a593Smuzhiyun 		 * value is checked later when we compare against
352*4882a593Smuzhiyun 		 * total memory available.
353*4882a593Smuzhiyun 		 */
354*4882a593Smuzhiyun 		pool = wildcard_to_pool(size);
355*4882a593Smuzhiyun 		if (pool == -1) {			/* non-wildcard */
356*4882a593Smuzhiyun 			fixed_blocks += size * count;
357*4882a593Smuzhiyun 		} else if (pool < NUM_SC_POOLS) {	/* valid wildcard */
358*4882a593Smuzhiyun 			mem_pool_info[pool].count += count;
359*4882a593Smuzhiyun 		} else {				/* invalid wildcard */
360*4882a593Smuzhiyun 			dd_dev_err(
361*4882a593Smuzhiyun 				dd,
362*4882a593Smuzhiyun 				"%s send context invalid pool wildcard %d\n",
363*4882a593Smuzhiyun 				sc_type_name(i), size);
364*4882a593Smuzhiyun 			return -EINVAL;
365*4882a593Smuzhiyun 		}
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 		dd->sc_sizes[i].count = count;
368*4882a593Smuzhiyun 		dd->sc_sizes[i].size = size;
369*4882a593Smuzhiyun 	}
370*4882a593Smuzhiyun 	if (fixed_blocks > total_blocks) {
371*4882a593Smuzhiyun 		dd_dev_err(
372*4882a593Smuzhiyun 			dd,
373*4882a593Smuzhiyun 			"Send context fixed block count, %u, larger than total block count %u\n",
374*4882a593Smuzhiyun 			fixed_blocks, total_blocks);
375*4882a593Smuzhiyun 		return -EINVAL;
376*4882a593Smuzhiyun 	}
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	/* step 3: calculate the blocks in the pools, and pool context sizes */
379*4882a593Smuzhiyun 	pool_blocks = total_blocks - fixed_blocks;
380*4882a593Smuzhiyun 	if (ab_total > pool_blocks) {
381*4882a593Smuzhiyun 		dd_dev_err(
382*4882a593Smuzhiyun 			dd,
383*4882a593Smuzhiyun 			"Send context fixed pool sizes, %u, larger than pool block count %u\n",
384*4882a593Smuzhiyun 			ab_total, pool_blocks);
385*4882a593Smuzhiyun 		return -EINVAL;
386*4882a593Smuzhiyun 	}
387*4882a593Smuzhiyun 	/* subtract off the fixed pool blocks */
388*4882a593Smuzhiyun 	pool_blocks -= ab_total;
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	for (i = 0; i < NUM_SC_POOLS; i++) {
391*4882a593Smuzhiyun 		struct mem_pool_info *pi = &mem_pool_info[i];
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 		/* % beats absolute blocks */
394*4882a593Smuzhiyun 		if (pi->centipercent >= 0)
395*4882a593Smuzhiyun 			pi->blocks = (pool_blocks * pi->centipercent) / 10000;
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 		if (pi->blocks == 0 && pi->count != 0) {
398*4882a593Smuzhiyun 			dd_dev_err(
399*4882a593Smuzhiyun 				dd,
400*4882a593Smuzhiyun 				"Send context memory pool %d has %u contexts, but no blocks\n",
401*4882a593Smuzhiyun 				i, pi->count);
402*4882a593Smuzhiyun 			return -EINVAL;
403*4882a593Smuzhiyun 		}
404*4882a593Smuzhiyun 		if (pi->count == 0) {
405*4882a593Smuzhiyun 			/* warn about wasted blocks */
406*4882a593Smuzhiyun 			if (pi->blocks != 0)
407*4882a593Smuzhiyun 				dd_dev_err(
408*4882a593Smuzhiyun 					dd,
409*4882a593Smuzhiyun 					"Send context memory pool %d has %u blocks, but zero contexts\n",
410*4882a593Smuzhiyun 					i, pi->blocks);
411*4882a593Smuzhiyun 			pi->size = 0;
412*4882a593Smuzhiyun 		} else {
413*4882a593Smuzhiyun 			pi->size = pi->blocks / pi->count;
414*4882a593Smuzhiyun 		}
415*4882a593Smuzhiyun 	}
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	/* step 4: fill in the context type sizes from the pool sizes */
418*4882a593Smuzhiyun 	used_blocks = 0;
419*4882a593Smuzhiyun 	for (i = 0; i < SC_MAX; i++) {
420*4882a593Smuzhiyun 		if (dd->sc_sizes[i].size < 0) {
421*4882a593Smuzhiyun 			unsigned pool = wildcard_to_pool(dd->sc_sizes[i].size);
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 			WARN_ON_ONCE(pool >= NUM_SC_POOLS);
424*4882a593Smuzhiyun 			dd->sc_sizes[i].size = mem_pool_info[pool].size;
425*4882a593Smuzhiyun 		}
426*4882a593Smuzhiyun 		/* make sure we are not larger than what is allowed by the HW */
427*4882a593Smuzhiyun #define PIO_MAX_BLOCKS 1024
428*4882a593Smuzhiyun 		if (dd->sc_sizes[i].size > PIO_MAX_BLOCKS)
429*4882a593Smuzhiyun 			dd->sc_sizes[i].size = PIO_MAX_BLOCKS;
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 		/* calculate our total usage */
432*4882a593Smuzhiyun 		used_blocks += dd->sc_sizes[i].size * dd->sc_sizes[i].count;
433*4882a593Smuzhiyun 	}
434*4882a593Smuzhiyun 	extra = total_blocks - used_blocks;
435*4882a593Smuzhiyun 	if (extra != 0)
436*4882a593Smuzhiyun 		dd_dev_info(dd, "unused send context blocks: %d\n", extra);
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	return total_contexts;
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun 
init_send_contexts(struct hfi1_devdata * dd)441*4882a593Smuzhiyun int init_send_contexts(struct hfi1_devdata *dd)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun 	u16 base;
444*4882a593Smuzhiyun 	int ret, i, j, context;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	ret = init_credit_return(dd);
447*4882a593Smuzhiyun 	if (ret)
448*4882a593Smuzhiyun 		return ret;
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	dd->hw_to_sw = kmalloc_array(TXE_NUM_CONTEXTS, sizeof(u8),
451*4882a593Smuzhiyun 					GFP_KERNEL);
452*4882a593Smuzhiyun 	dd->send_contexts = kcalloc(dd->num_send_contexts,
453*4882a593Smuzhiyun 				    sizeof(struct send_context_info),
454*4882a593Smuzhiyun 				    GFP_KERNEL);
455*4882a593Smuzhiyun 	if (!dd->send_contexts || !dd->hw_to_sw) {
456*4882a593Smuzhiyun 		kfree(dd->hw_to_sw);
457*4882a593Smuzhiyun 		kfree(dd->send_contexts);
458*4882a593Smuzhiyun 		free_credit_return(dd);
459*4882a593Smuzhiyun 		return -ENOMEM;
460*4882a593Smuzhiyun 	}
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	/* hardware context map starts with invalid send context indices */
463*4882a593Smuzhiyun 	for (i = 0; i < TXE_NUM_CONTEXTS; i++)
464*4882a593Smuzhiyun 		dd->hw_to_sw[i] = INVALID_SCI;
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	/*
467*4882a593Smuzhiyun 	 * All send contexts have their credit sizes.  Allocate credits
468*4882a593Smuzhiyun 	 * for each context one after another from the global space.
469*4882a593Smuzhiyun 	 */
470*4882a593Smuzhiyun 	context = 0;
471*4882a593Smuzhiyun 	base = 1;
472*4882a593Smuzhiyun 	for (i = 0; i < SC_MAX; i++) {
473*4882a593Smuzhiyun 		struct sc_config_sizes *scs = &dd->sc_sizes[i];
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 		for (j = 0; j < scs->count; j++) {
476*4882a593Smuzhiyun 			struct send_context_info *sci =
477*4882a593Smuzhiyun 						&dd->send_contexts[context];
478*4882a593Smuzhiyun 			sci->type = i;
479*4882a593Smuzhiyun 			sci->base = base;
480*4882a593Smuzhiyun 			sci->credits = scs->size;
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 			context++;
483*4882a593Smuzhiyun 			base += scs->size;
484*4882a593Smuzhiyun 		}
485*4882a593Smuzhiyun 	}
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 	return 0;
488*4882a593Smuzhiyun }
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun /*
491*4882a593Smuzhiyun  * Allocate a software index and hardware context of the given type.
492*4882a593Smuzhiyun  *
493*4882a593Smuzhiyun  * Must be called with dd->sc_lock held.
494*4882a593Smuzhiyun  */
sc_hw_alloc(struct hfi1_devdata * dd,int type,u32 * sw_index,u32 * hw_context)495*4882a593Smuzhiyun static int sc_hw_alloc(struct hfi1_devdata *dd, int type, u32 *sw_index,
496*4882a593Smuzhiyun 		       u32 *hw_context)
497*4882a593Smuzhiyun {
498*4882a593Smuzhiyun 	struct send_context_info *sci;
499*4882a593Smuzhiyun 	u32 index;
500*4882a593Smuzhiyun 	u32 context;
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 	for (index = 0, sci = &dd->send_contexts[0];
503*4882a593Smuzhiyun 			index < dd->num_send_contexts; index++, sci++) {
504*4882a593Smuzhiyun 		if (sci->type == type && sci->allocated == 0) {
505*4882a593Smuzhiyun 			sci->allocated = 1;
506*4882a593Smuzhiyun 			/* use a 1:1 mapping, but make them non-equal */
507*4882a593Smuzhiyun 			context = chip_send_contexts(dd) - index - 1;
508*4882a593Smuzhiyun 			dd->hw_to_sw[context] = index;
509*4882a593Smuzhiyun 			*sw_index = index;
510*4882a593Smuzhiyun 			*hw_context = context;
511*4882a593Smuzhiyun 			return 0; /* success */
512*4882a593Smuzhiyun 		}
513*4882a593Smuzhiyun 	}
514*4882a593Smuzhiyun 	dd_dev_err(dd, "Unable to locate a free type %d send context\n", type);
515*4882a593Smuzhiyun 	return -ENOSPC;
516*4882a593Smuzhiyun }
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun /*
519*4882a593Smuzhiyun  * Free the send context given by its software index.
520*4882a593Smuzhiyun  *
521*4882a593Smuzhiyun  * Must be called with dd->sc_lock held.
522*4882a593Smuzhiyun  */
sc_hw_free(struct hfi1_devdata * dd,u32 sw_index,u32 hw_context)523*4882a593Smuzhiyun static void sc_hw_free(struct hfi1_devdata *dd, u32 sw_index, u32 hw_context)
524*4882a593Smuzhiyun {
525*4882a593Smuzhiyun 	struct send_context_info *sci;
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	sci = &dd->send_contexts[sw_index];
528*4882a593Smuzhiyun 	if (!sci->allocated) {
529*4882a593Smuzhiyun 		dd_dev_err(dd, "%s: sw_index %u not allocated? hw_context %u\n",
530*4882a593Smuzhiyun 			   __func__, sw_index, hw_context);
531*4882a593Smuzhiyun 	}
532*4882a593Smuzhiyun 	sci->allocated = 0;
533*4882a593Smuzhiyun 	dd->hw_to_sw[hw_context] = INVALID_SCI;
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun /* return the base context of a context in a group */
group_context(u32 context,u32 group)537*4882a593Smuzhiyun static inline u32 group_context(u32 context, u32 group)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun 	return (context >> group) << group;
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun /* return the size of a group */
group_size(u32 group)543*4882a593Smuzhiyun static inline u32 group_size(u32 group)
544*4882a593Smuzhiyun {
545*4882a593Smuzhiyun 	return 1 << group;
546*4882a593Smuzhiyun }
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun /*
549*4882a593Smuzhiyun  * Obtain the credit return addresses, kernel virtual and bus, for the
550*4882a593Smuzhiyun  * given sc.
551*4882a593Smuzhiyun  *
552*4882a593Smuzhiyun  * To understand this routine:
553*4882a593Smuzhiyun  * o va and dma are arrays of struct credit_return.  One for each physical
554*4882a593Smuzhiyun  *   send context, per NUMA.
555*4882a593Smuzhiyun  * o Each send context always looks in its relative location in a struct
556*4882a593Smuzhiyun  *   credit_return for its credit return.
557*4882a593Smuzhiyun  * o Each send context in a group must have its return address CSR programmed
558*4882a593Smuzhiyun  *   with the same value.  Use the address of the first send context in the
559*4882a593Smuzhiyun  *   group.
560*4882a593Smuzhiyun  */
cr_group_addresses(struct send_context * sc,dma_addr_t * dma)561*4882a593Smuzhiyun static void cr_group_addresses(struct send_context *sc, dma_addr_t *dma)
562*4882a593Smuzhiyun {
563*4882a593Smuzhiyun 	u32 gc = group_context(sc->hw_context, sc->group);
564*4882a593Smuzhiyun 	u32 index = sc->hw_context & 0x7;
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 	sc->hw_free = &sc->dd->cr_base[sc->node].va[gc].cr[index];
567*4882a593Smuzhiyun 	*dma = (unsigned long)
568*4882a593Smuzhiyun 	       &((struct credit_return *)sc->dd->cr_base[sc->node].dma)[gc];
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun /*
572*4882a593Smuzhiyun  * Work queue function triggered in error interrupt routine for
573*4882a593Smuzhiyun  * kernel contexts.
574*4882a593Smuzhiyun  */
sc_halted(struct work_struct * work)575*4882a593Smuzhiyun static void sc_halted(struct work_struct *work)
576*4882a593Smuzhiyun {
577*4882a593Smuzhiyun 	struct send_context *sc;
578*4882a593Smuzhiyun 
579*4882a593Smuzhiyun 	sc = container_of(work, struct send_context, halt_work);
580*4882a593Smuzhiyun 	sc_restart(sc);
581*4882a593Smuzhiyun }
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun /*
584*4882a593Smuzhiyun  * Calculate PIO block threshold for this send context using the given MTU.
585*4882a593Smuzhiyun  * Trigger a return when one MTU plus optional header of credits remain.
586*4882a593Smuzhiyun  *
587*4882a593Smuzhiyun  * Parameter mtu is in bytes.
588*4882a593Smuzhiyun  * Parameter hdrqentsize is in DWORDs.
589*4882a593Smuzhiyun  *
590*4882a593Smuzhiyun  * Return value is what to write into the CSR: trigger return when
591*4882a593Smuzhiyun  * unreturned credits pass this count.
592*4882a593Smuzhiyun  */
sc_mtu_to_threshold(struct send_context * sc,u32 mtu,u32 hdrqentsize)593*4882a593Smuzhiyun u32 sc_mtu_to_threshold(struct send_context *sc, u32 mtu, u32 hdrqentsize)
594*4882a593Smuzhiyun {
595*4882a593Smuzhiyun 	u32 release_credits;
596*4882a593Smuzhiyun 	u32 threshold;
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun 	/* add in the header size, then divide by the PIO block size */
599*4882a593Smuzhiyun 	mtu += hdrqentsize << 2;
600*4882a593Smuzhiyun 	release_credits = DIV_ROUND_UP(mtu, PIO_BLOCK_SIZE);
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	/* check against this context's credits */
603*4882a593Smuzhiyun 	if (sc->credits <= release_credits)
604*4882a593Smuzhiyun 		threshold = 1;
605*4882a593Smuzhiyun 	else
606*4882a593Smuzhiyun 		threshold = sc->credits - release_credits;
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	return threshold;
609*4882a593Smuzhiyun }
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun /*
612*4882a593Smuzhiyun  * Calculate credit threshold in terms of percent of the allocated credits.
613*4882a593Smuzhiyun  * Trigger when unreturned credits equal or exceed the percentage of the whole.
614*4882a593Smuzhiyun  *
615*4882a593Smuzhiyun  * Return value is what to write into the CSR: trigger return when
616*4882a593Smuzhiyun  * unreturned credits pass this count.
617*4882a593Smuzhiyun  */
sc_percent_to_threshold(struct send_context * sc,u32 percent)618*4882a593Smuzhiyun u32 sc_percent_to_threshold(struct send_context *sc, u32 percent)
619*4882a593Smuzhiyun {
620*4882a593Smuzhiyun 	return (sc->credits * percent) / 100;
621*4882a593Smuzhiyun }
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun /*
624*4882a593Smuzhiyun  * Set the credit return threshold.
625*4882a593Smuzhiyun  */
sc_set_cr_threshold(struct send_context * sc,u32 new_threshold)626*4882a593Smuzhiyun void sc_set_cr_threshold(struct send_context *sc, u32 new_threshold)
627*4882a593Smuzhiyun {
628*4882a593Smuzhiyun 	unsigned long flags;
629*4882a593Smuzhiyun 	u32 old_threshold;
630*4882a593Smuzhiyun 	int force_return = 0;
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	spin_lock_irqsave(&sc->credit_ctrl_lock, flags);
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun 	old_threshold = (sc->credit_ctrl >>
635*4882a593Smuzhiyun 				SC(CREDIT_CTRL_THRESHOLD_SHIFT))
636*4882a593Smuzhiyun 			 & SC(CREDIT_CTRL_THRESHOLD_MASK);
637*4882a593Smuzhiyun 
638*4882a593Smuzhiyun 	if (new_threshold != old_threshold) {
639*4882a593Smuzhiyun 		sc->credit_ctrl =
640*4882a593Smuzhiyun 			(sc->credit_ctrl
641*4882a593Smuzhiyun 				& ~SC(CREDIT_CTRL_THRESHOLD_SMASK))
642*4882a593Smuzhiyun 			| ((new_threshold
643*4882a593Smuzhiyun 				& SC(CREDIT_CTRL_THRESHOLD_MASK))
644*4882a593Smuzhiyun 			   << SC(CREDIT_CTRL_THRESHOLD_SHIFT));
645*4882a593Smuzhiyun 		write_kctxt_csr(sc->dd, sc->hw_context,
646*4882a593Smuzhiyun 				SC(CREDIT_CTRL), sc->credit_ctrl);
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 		/* force a credit return on change to avoid a possible stall */
649*4882a593Smuzhiyun 		force_return = 1;
650*4882a593Smuzhiyun 	}
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun 	spin_unlock_irqrestore(&sc->credit_ctrl_lock, flags);
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 	if (force_return)
655*4882a593Smuzhiyun 		sc_return_credits(sc);
656*4882a593Smuzhiyun }
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun /*
659*4882a593Smuzhiyun  * set_pio_integrity
660*4882a593Smuzhiyun  *
661*4882a593Smuzhiyun  * Set the CHECK_ENABLE register for the send context 'sc'.
662*4882a593Smuzhiyun  */
set_pio_integrity(struct send_context * sc)663*4882a593Smuzhiyun void set_pio_integrity(struct send_context *sc)
664*4882a593Smuzhiyun {
665*4882a593Smuzhiyun 	struct hfi1_devdata *dd = sc->dd;
666*4882a593Smuzhiyun 	u32 hw_context = sc->hw_context;
667*4882a593Smuzhiyun 	int type = sc->type;
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	write_kctxt_csr(dd, hw_context,
670*4882a593Smuzhiyun 			SC(CHECK_ENABLE),
671*4882a593Smuzhiyun 			hfi1_pkt_default_send_ctxt_mask(dd, type));
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun 
get_buffers_allocated(struct send_context * sc)674*4882a593Smuzhiyun static u32 get_buffers_allocated(struct send_context *sc)
675*4882a593Smuzhiyun {
676*4882a593Smuzhiyun 	int cpu;
677*4882a593Smuzhiyun 	u32 ret = 0;
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 	for_each_possible_cpu(cpu)
680*4882a593Smuzhiyun 		ret += *per_cpu_ptr(sc->buffers_allocated, cpu);
681*4882a593Smuzhiyun 	return ret;
682*4882a593Smuzhiyun }
683*4882a593Smuzhiyun 
reset_buffers_allocated(struct send_context * sc)684*4882a593Smuzhiyun static void reset_buffers_allocated(struct send_context *sc)
685*4882a593Smuzhiyun {
686*4882a593Smuzhiyun 	int cpu;
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	for_each_possible_cpu(cpu)
689*4882a593Smuzhiyun 		(*per_cpu_ptr(sc->buffers_allocated, cpu)) = 0;
690*4882a593Smuzhiyun }
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun /*
693*4882a593Smuzhiyun  * Allocate a NUMA relative send context structure of the given type along
694*4882a593Smuzhiyun  * with a HW context.
695*4882a593Smuzhiyun  */
sc_alloc(struct hfi1_devdata * dd,int type,uint hdrqentsize,int numa)696*4882a593Smuzhiyun struct send_context *sc_alloc(struct hfi1_devdata *dd, int type,
697*4882a593Smuzhiyun 			      uint hdrqentsize, int numa)
698*4882a593Smuzhiyun {
699*4882a593Smuzhiyun 	struct send_context_info *sci;
700*4882a593Smuzhiyun 	struct send_context *sc = NULL;
701*4882a593Smuzhiyun 	dma_addr_t dma;
702*4882a593Smuzhiyun 	unsigned long flags;
703*4882a593Smuzhiyun 	u64 reg;
704*4882a593Smuzhiyun 	u32 thresh;
705*4882a593Smuzhiyun 	u32 sw_index;
706*4882a593Smuzhiyun 	u32 hw_context;
707*4882a593Smuzhiyun 	int ret;
708*4882a593Smuzhiyun 	u8 opval, opmask;
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 	/* do not allocate while frozen */
711*4882a593Smuzhiyun 	if (dd->flags & HFI1_FROZEN)
712*4882a593Smuzhiyun 		return NULL;
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 	sc = kzalloc_node(sizeof(*sc), GFP_KERNEL, numa);
715*4882a593Smuzhiyun 	if (!sc)
716*4882a593Smuzhiyun 		return NULL;
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun 	sc->buffers_allocated = alloc_percpu(u32);
719*4882a593Smuzhiyun 	if (!sc->buffers_allocated) {
720*4882a593Smuzhiyun 		kfree(sc);
721*4882a593Smuzhiyun 		dd_dev_err(dd,
722*4882a593Smuzhiyun 			   "Cannot allocate buffers_allocated per cpu counters\n"
723*4882a593Smuzhiyun 			  );
724*4882a593Smuzhiyun 		return NULL;
725*4882a593Smuzhiyun 	}
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	spin_lock_irqsave(&dd->sc_lock, flags);
728*4882a593Smuzhiyun 	ret = sc_hw_alloc(dd, type, &sw_index, &hw_context);
729*4882a593Smuzhiyun 	if (ret) {
730*4882a593Smuzhiyun 		spin_unlock_irqrestore(&dd->sc_lock, flags);
731*4882a593Smuzhiyun 		free_percpu(sc->buffers_allocated);
732*4882a593Smuzhiyun 		kfree(sc);
733*4882a593Smuzhiyun 		return NULL;
734*4882a593Smuzhiyun 	}
735*4882a593Smuzhiyun 
736*4882a593Smuzhiyun 	sci = &dd->send_contexts[sw_index];
737*4882a593Smuzhiyun 	sci->sc = sc;
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	sc->dd = dd;
740*4882a593Smuzhiyun 	sc->node = numa;
741*4882a593Smuzhiyun 	sc->type = type;
742*4882a593Smuzhiyun 	spin_lock_init(&sc->alloc_lock);
743*4882a593Smuzhiyun 	spin_lock_init(&sc->release_lock);
744*4882a593Smuzhiyun 	spin_lock_init(&sc->credit_ctrl_lock);
745*4882a593Smuzhiyun 	seqlock_init(&sc->waitlock);
746*4882a593Smuzhiyun 	INIT_LIST_HEAD(&sc->piowait);
747*4882a593Smuzhiyun 	INIT_WORK(&sc->halt_work, sc_halted);
748*4882a593Smuzhiyun 	init_waitqueue_head(&sc->halt_wait);
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 	/* grouping is always single context for now */
751*4882a593Smuzhiyun 	sc->group = 0;
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 	sc->sw_index = sw_index;
754*4882a593Smuzhiyun 	sc->hw_context = hw_context;
755*4882a593Smuzhiyun 	cr_group_addresses(sc, &dma);
756*4882a593Smuzhiyun 	sc->credits = sci->credits;
757*4882a593Smuzhiyun 	sc->size = sc->credits * PIO_BLOCK_SIZE;
758*4882a593Smuzhiyun 
759*4882a593Smuzhiyun /* PIO Send Memory Address details */
760*4882a593Smuzhiyun #define PIO_ADDR_CONTEXT_MASK 0xfful
761*4882a593Smuzhiyun #define PIO_ADDR_CONTEXT_SHIFT 16
762*4882a593Smuzhiyun 	sc->base_addr = dd->piobase + ((hw_context & PIO_ADDR_CONTEXT_MASK)
763*4882a593Smuzhiyun 					<< PIO_ADDR_CONTEXT_SHIFT);
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	/* set base and credits */
766*4882a593Smuzhiyun 	reg = ((sci->credits & SC(CTRL_CTXT_DEPTH_MASK))
767*4882a593Smuzhiyun 					<< SC(CTRL_CTXT_DEPTH_SHIFT))
768*4882a593Smuzhiyun 		| ((sci->base & SC(CTRL_CTXT_BASE_MASK))
769*4882a593Smuzhiyun 					<< SC(CTRL_CTXT_BASE_SHIFT));
770*4882a593Smuzhiyun 	write_kctxt_csr(dd, hw_context, SC(CTRL), reg);
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 	set_pio_integrity(sc);
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 	/* unmask all errors */
775*4882a593Smuzhiyun 	write_kctxt_csr(dd, hw_context, SC(ERR_MASK), (u64)-1);
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	/* set the default partition key */
778*4882a593Smuzhiyun 	write_kctxt_csr(dd, hw_context, SC(CHECK_PARTITION_KEY),
779*4882a593Smuzhiyun 			(SC(CHECK_PARTITION_KEY_VALUE_MASK) &
780*4882a593Smuzhiyun 			 DEFAULT_PKEY) <<
781*4882a593Smuzhiyun 			SC(CHECK_PARTITION_KEY_VALUE_SHIFT));
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	/* per context type checks */
784*4882a593Smuzhiyun 	if (type == SC_USER) {
785*4882a593Smuzhiyun 		opval = USER_OPCODE_CHECK_VAL;
786*4882a593Smuzhiyun 		opmask = USER_OPCODE_CHECK_MASK;
787*4882a593Smuzhiyun 	} else {
788*4882a593Smuzhiyun 		opval = OPCODE_CHECK_VAL_DISABLED;
789*4882a593Smuzhiyun 		opmask = OPCODE_CHECK_MASK_DISABLED;
790*4882a593Smuzhiyun 	}
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	/* set the send context check opcode mask and value */
793*4882a593Smuzhiyun 	write_kctxt_csr(dd, hw_context, SC(CHECK_OPCODE),
794*4882a593Smuzhiyun 			((u64)opmask << SC(CHECK_OPCODE_MASK_SHIFT)) |
795*4882a593Smuzhiyun 			((u64)opval << SC(CHECK_OPCODE_VALUE_SHIFT)));
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 	/* set up credit return */
798*4882a593Smuzhiyun 	reg = dma & SC(CREDIT_RETURN_ADDR_ADDRESS_SMASK);
799*4882a593Smuzhiyun 	write_kctxt_csr(dd, hw_context, SC(CREDIT_RETURN_ADDR), reg);
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun 	/*
802*4882a593Smuzhiyun 	 * Calculate the initial credit return threshold.
803*4882a593Smuzhiyun 	 *
804*4882a593Smuzhiyun 	 * For Ack contexts, set a threshold for half the credits.
805*4882a593Smuzhiyun 	 * For User contexts use the given percentage.  This has been
806*4882a593Smuzhiyun 	 * sanitized on driver start-up.
807*4882a593Smuzhiyun 	 * For Kernel contexts, use the default MTU plus a header
808*4882a593Smuzhiyun 	 * or half the credits, whichever is smaller. This should
809*4882a593Smuzhiyun 	 * work for both the 3-deep buffering allocation and the
810*4882a593Smuzhiyun 	 * pooling allocation.
811*4882a593Smuzhiyun 	 */
812*4882a593Smuzhiyun 	if (type == SC_ACK) {
813*4882a593Smuzhiyun 		thresh = sc_percent_to_threshold(sc, 50);
814*4882a593Smuzhiyun 	} else if (type == SC_USER) {
815*4882a593Smuzhiyun 		thresh = sc_percent_to_threshold(sc,
816*4882a593Smuzhiyun 						 user_credit_return_threshold);
817*4882a593Smuzhiyun 	} else { /* kernel */
818*4882a593Smuzhiyun 		thresh = min(sc_percent_to_threshold(sc, 50),
819*4882a593Smuzhiyun 			     sc_mtu_to_threshold(sc, hfi1_max_mtu,
820*4882a593Smuzhiyun 						 hdrqentsize));
821*4882a593Smuzhiyun 	}
822*4882a593Smuzhiyun 	reg = thresh << SC(CREDIT_CTRL_THRESHOLD_SHIFT);
823*4882a593Smuzhiyun 	/* add in early return */
824*4882a593Smuzhiyun 	if (type == SC_USER && HFI1_CAP_IS_USET(EARLY_CREDIT_RETURN))
825*4882a593Smuzhiyun 		reg |= SC(CREDIT_CTRL_EARLY_RETURN_SMASK);
826*4882a593Smuzhiyun 	else if (HFI1_CAP_IS_KSET(EARLY_CREDIT_RETURN)) /* kernel, ack */
827*4882a593Smuzhiyun 		reg |= SC(CREDIT_CTRL_EARLY_RETURN_SMASK);
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun 	/* set up write-through credit_ctrl */
830*4882a593Smuzhiyun 	sc->credit_ctrl = reg;
831*4882a593Smuzhiyun 	write_kctxt_csr(dd, hw_context, SC(CREDIT_CTRL), reg);
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun 	/* User send contexts should not allow sending on VL15 */
834*4882a593Smuzhiyun 	if (type == SC_USER) {
835*4882a593Smuzhiyun 		reg = 1ULL << 15;
836*4882a593Smuzhiyun 		write_kctxt_csr(dd, hw_context, SC(CHECK_VL), reg);
837*4882a593Smuzhiyun 	}
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dd->sc_lock, flags);
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	/*
842*4882a593Smuzhiyun 	 * Allocate shadow ring to track outstanding PIO buffers _after_
843*4882a593Smuzhiyun 	 * unlocking.  We don't know the size until the lock is held and
844*4882a593Smuzhiyun 	 * we can't allocate while the lock is held.  No one is using
845*4882a593Smuzhiyun 	 * the context yet, so allocate it now.
846*4882a593Smuzhiyun 	 *
847*4882a593Smuzhiyun 	 * User contexts do not get a shadow ring.
848*4882a593Smuzhiyun 	 */
849*4882a593Smuzhiyun 	if (type != SC_USER) {
850*4882a593Smuzhiyun 		/*
851*4882a593Smuzhiyun 		 * Size the shadow ring 1 larger than the number of credits
852*4882a593Smuzhiyun 		 * so head == tail can mean empty.
853*4882a593Smuzhiyun 		 */
854*4882a593Smuzhiyun 		sc->sr_size = sci->credits + 1;
855*4882a593Smuzhiyun 		sc->sr = kcalloc_node(sc->sr_size,
856*4882a593Smuzhiyun 				      sizeof(union pio_shadow_ring),
857*4882a593Smuzhiyun 				      GFP_KERNEL, numa);
858*4882a593Smuzhiyun 		if (!sc->sr) {
859*4882a593Smuzhiyun 			sc_free(sc);
860*4882a593Smuzhiyun 			return NULL;
861*4882a593Smuzhiyun 		}
862*4882a593Smuzhiyun 	}
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 	hfi1_cdbg(PIO,
865*4882a593Smuzhiyun 		  "Send context %u(%u) %s group %u credits %u credit_ctrl 0x%llx threshold %u\n",
866*4882a593Smuzhiyun 		  sw_index,
867*4882a593Smuzhiyun 		  hw_context,
868*4882a593Smuzhiyun 		  sc_type_name(type),
869*4882a593Smuzhiyun 		  sc->group,
870*4882a593Smuzhiyun 		  sc->credits,
871*4882a593Smuzhiyun 		  sc->credit_ctrl,
872*4882a593Smuzhiyun 		  thresh);
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun 	return sc;
875*4882a593Smuzhiyun }
876*4882a593Smuzhiyun 
877*4882a593Smuzhiyun /* free a per-NUMA send context structure */
sc_free(struct send_context * sc)878*4882a593Smuzhiyun void sc_free(struct send_context *sc)
879*4882a593Smuzhiyun {
880*4882a593Smuzhiyun 	struct hfi1_devdata *dd;
881*4882a593Smuzhiyun 	unsigned long flags;
882*4882a593Smuzhiyun 	u32 sw_index;
883*4882a593Smuzhiyun 	u32 hw_context;
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun 	if (!sc)
886*4882a593Smuzhiyun 		return;
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 	sc->flags |= SCF_IN_FREE;	/* ensure no restarts */
889*4882a593Smuzhiyun 	dd = sc->dd;
890*4882a593Smuzhiyun 	if (!list_empty(&sc->piowait))
891*4882a593Smuzhiyun 		dd_dev_err(dd, "piowait list not empty!\n");
892*4882a593Smuzhiyun 	sw_index = sc->sw_index;
893*4882a593Smuzhiyun 	hw_context = sc->hw_context;
894*4882a593Smuzhiyun 	sc_disable(sc);	/* make sure the HW is disabled */
895*4882a593Smuzhiyun 	flush_work(&sc->halt_work);
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 	spin_lock_irqsave(&dd->sc_lock, flags);
898*4882a593Smuzhiyun 	dd->send_contexts[sw_index].sc = NULL;
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun 	/* clear/disable all registers set in sc_alloc */
901*4882a593Smuzhiyun 	write_kctxt_csr(dd, hw_context, SC(CTRL), 0);
902*4882a593Smuzhiyun 	write_kctxt_csr(dd, hw_context, SC(CHECK_ENABLE), 0);
903*4882a593Smuzhiyun 	write_kctxt_csr(dd, hw_context, SC(ERR_MASK), 0);
904*4882a593Smuzhiyun 	write_kctxt_csr(dd, hw_context, SC(CHECK_PARTITION_KEY), 0);
905*4882a593Smuzhiyun 	write_kctxt_csr(dd, hw_context, SC(CHECK_OPCODE), 0);
906*4882a593Smuzhiyun 	write_kctxt_csr(dd, hw_context, SC(CREDIT_RETURN_ADDR), 0);
907*4882a593Smuzhiyun 	write_kctxt_csr(dd, hw_context, SC(CREDIT_CTRL), 0);
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 	/* release the index and context for re-use */
910*4882a593Smuzhiyun 	sc_hw_free(dd, sw_index, hw_context);
911*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dd->sc_lock, flags);
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun 	kfree(sc->sr);
914*4882a593Smuzhiyun 	free_percpu(sc->buffers_allocated);
915*4882a593Smuzhiyun 	kfree(sc);
916*4882a593Smuzhiyun }
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun /* disable the context */
sc_disable(struct send_context * sc)919*4882a593Smuzhiyun void sc_disable(struct send_context *sc)
920*4882a593Smuzhiyun {
921*4882a593Smuzhiyun 	u64 reg;
922*4882a593Smuzhiyun 	struct pio_buf *pbuf;
923*4882a593Smuzhiyun 	LIST_HEAD(wake_list);
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun 	if (!sc)
926*4882a593Smuzhiyun 		return;
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun 	/* do all steps, even if already disabled */
929*4882a593Smuzhiyun 	spin_lock_irq(&sc->alloc_lock);
930*4882a593Smuzhiyun 	reg = read_kctxt_csr(sc->dd, sc->hw_context, SC(CTRL));
931*4882a593Smuzhiyun 	reg &= ~SC(CTRL_CTXT_ENABLE_SMASK);
932*4882a593Smuzhiyun 	sc->flags &= ~SCF_ENABLED;
933*4882a593Smuzhiyun 	sc_wait_for_packet_egress(sc, 1);
934*4882a593Smuzhiyun 	write_kctxt_csr(sc->dd, sc->hw_context, SC(CTRL), reg);
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	/*
937*4882a593Smuzhiyun 	 * Flush any waiters.  Once the context is disabled,
938*4882a593Smuzhiyun 	 * credit return interrupts are stopped (although there
939*4882a593Smuzhiyun 	 * could be one in-process when the context is disabled).
940*4882a593Smuzhiyun 	 * Wait one microsecond for any lingering interrupts, then
941*4882a593Smuzhiyun 	 * proceed with the flush.
942*4882a593Smuzhiyun 	 */
943*4882a593Smuzhiyun 	udelay(1);
944*4882a593Smuzhiyun 	spin_lock(&sc->release_lock);
945*4882a593Smuzhiyun 	if (sc->sr) {	/* this context has a shadow ring */
946*4882a593Smuzhiyun 		while (sc->sr_tail != sc->sr_head) {
947*4882a593Smuzhiyun 			pbuf = &sc->sr[sc->sr_tail].pbuf;
948*4882a593Smuzhiyun 			if (pbuf->cb)
949*4882a593Smuzhiyun 				(*pbuf->cb)(pbuf->arg, PRC_SC_DISABLE);
950*4882a593Smuzhiyun 			sc->sr_tail++;
951*4882a593Smuzhiyun 			if (sc->sr_tail >= sc->sr_size)
952*4882a593Smuzhiyun 				sc->sr_tail = 0;
953*4882a593Smuzhiyun 		}
954*4882a593Smuzhiyun 	}
955*4882a593Smuzhiyun 	spin_unlock(&sc->release_lock);
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun 	write_seqlock(&sc->waitlock);
958*4882a593Smuzhiyun 	list_splice_init(&sc->piowait, &wake_list);
959*4882a593Smuzhiyun 	write_sequnlock(&sc->waitlock);
960*4882a593Smuzhiyun 	while (!list_empty(&wake_list)) {
961*4882a593Smuzhiyun 		struct iowait *wait;
962*4882a593Smuzhiyun 		struct rvt_qp *qp;
963*4882a593Smuzhiyun 		struct hfi1_qp_priv *priv;
964*4882a593Smuzhiyun 
965*4882a593Smuzhiyun 		wait = list_first_entry(&wake_list, struct iowait, list);
966*4882a593Smuzhiyun 		qp = iowait_to_qp(wait);
967*4882a593Smuzhiyun 		priv = qp->priv;
968*4882a593Smuzhiyun 		list_del_init(&priv->s_iowait.list);
969*4882a593Smuzhiyun 		priv->s_iowait.lock = NULL;
970*4882a593Smuzhiyun 		hfi1_qp_wakeup(qp, RVT_S_WAIT_PIO | HFI1_S_WAIT_PIO_DRAIN);
971*4882a593Smuzhiyun 	}
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 	spin_unlock_irq(&sc->alloc_lock);
974*4882a593Smuzhiyun }
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun /* return SendEgressCtxtStatus.PacketOccupancy */
packet_occupancy(u64 reg)977*4882a593Smuzhiyun static u64 packet_occupancy(u64 reg)
978*4882a593Smuzhiyun {
979*4882a593Smuzhiyun 	return (reg &
980*4882a593Smuzhiyun 		SEND_EGRESS_CTXT_STATUS_CTXT_EGRESS_PACKET_OCCUPANCY_SMASK)
981*4882a593Smuzhiyun 		>> SEND_EGRESS_CTXT_STATUS_CTXT_EGRESS_PACKET_OCCUPANCY_SHIFT;
982*4882a593Smuzhiyun }
983*4882a593Smuzhiyun 
984*4882a593Smuzhiyun /* is egress halted on the context? */
egress_halted(u64 reg)985*4882a593Smuzhiyun static bool egress_halted(u64 reg)
986*4882a593Smuzhiyun {
987*4882a593Smuzhiyun 	return !!(reg & SEND_EGRESS_CTXT_STATUS_CTXT_EGRESS_HALT_STATUS_SMASK);
988*4882a593Smuzhiyun }
989*4882a593Smuzhiyun 
990*4882a593Smuzhiyun /* is the send context halted? */
is_sc_halted(struct hfi1_devdata * dd,u32 hw_context)991*4882a593Smuzhiyun static bool is_sc_halted(struct hfi1_devdata *dd, u32 hw_context)
992*4882a593Smuzhiyun {
993*4882a593Smuzhiyun 	return !!(read_kctxt_csr(dd, hw_context, SC(STATUS)) &
994*4882a593Smuzhiyun 		  SC(STATUS_CTXT_HALTED_SMASK));
995*4882a593Smuzhiyun }
996*4882a593Smuzhiyun 
997*4882a593Smuzhiyun /**
998*4882a593Smuzhiyun  * sc_wait_for_packet_egress
999*4882a593Smuzhiyun  * @sc: valid send context
1000*4882a593Smuzhiyun  * @pause: wait for credit return
1001*4882a593Smuzhiyun  *
1002*4882a593Smuzhiyun  * Wait for packet egress, optionally pause for credit return
1003*4882a593Smuzhiyun  *
1004*4882a593Smuzhiyun  * Egress halt and Context halt are not necessarily the same thing, so
1005*4882a593Smuzhiyun  * check for both.
1006*4882a593Smuzhiyun  *
1007*4882a593Smuzhiyun  * NOTE: The context halt bit may not be set immediately.  Because of this,
1008*4882a593Smuzhiyun  * it is necessary to check the SW SFC_HALTED bit (set in the IRQ) and the HW
1009*4882a593Smuzhiyun  * context bit to determine if the context is halted.
1010*4882a593Smuzhiyun  */
sc_wait_for_packet_egress(struct send_context * sc,int pause)1011*4882a593Smuzhiyun static void sc_wait_for_packet_egress(struct send_context *sc, int pause)
1012*4882a593Smuzhiyun {
1013*4882a593Smuzhiyun 	struct hfi1_devdata *dd = sc->dd;
1014*4882a593Smuzhiyun 	u64 reg = 0;
1015*4882a593Smuzhiyun 	u64 reg_prev;
1016*4882a593Smuzhiyun 	u32 loop = 0;
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun 	while (1) {
1019*4882a593Smuzhiyun 		reg_prev = reg;
1020*4882a593Smuzhiyun 		reg = read_csr(dd, sc->hw_context * 8 +
1021*4882a593Smuzhiyun 			       SEND_EGRESS_CTXT_STATUS);
1022*4882a593Smuzhiyun 		/* done if any halt bits, SW or HW are set */
1023*4882a593Smuzhiyun 		if (sc->flags & SCF_HALTED ||
1024*4882a593Smuzhiyun 		    is_sc_halted(dd, sc->hw_context) || egress_halted(reg))
1025*4882a593Smuzhiyun 			break;
1026*4882a593Smuzhiyun 		reg = packet_occupancy(reg);
1027*4882a593Smuzhiyun 		if (reg == 0)
1028*4882a593Smuzhiyun 			break;
1029*4882a593Smuzhiyun 		/* counter is reset if occupancy count changes */
1030*4882a593Smuzhiyun 		if (reg != reg_prev)
1031*4882a593Smuzhiyun 			loop = 0;
1032*4882a593Smuzhiyun 		if (loop > 50000) {
1033*4882a593Smuzhiyun 			/* timed out - bounce the link */
1034*4882a593Smuzhiyun 			dd_dev_err(dd,
1035*4882a593Smuzhiyun 				   "%s: context %u(%u) timeout waiting for packets to egress, remaining count %u, bouncing link\n",
1036*4882a593Smuzhiyun 				   __func__, sc->sw_index,
1037*4882a593Smuzhiyun 				   sc->hw_context, (u32)reg);
1038*4882a593Smuzhiyun 			queue_work(dd->pport->link_wq,
1039*4882a593Smuzhiyun 				   &dd->pport->link_bounce_work);
1040*4882a593Smuzhiyun 			break;
1041*4882a593Smuzhiyun 		}
1042*4882a593Smuzhiyun 		loop++;
1043*4882a593Smuzhiyun 		udelay(1);
1044*4882a593Smuzhiyun 	}
1045*4882a593Smuzhiyun 
1046*4882a593Smuzhiyun 	if (pause)
1047*4882a593Smuzhiyun 		/* Add additional delay to ensure chip returns all credits */
1048*4882a593Smuzhiyun 		pause_for_credit_return(dd);
1049*4882a593Smuzhiyun }
1050*4882a593Smuzhiyun 
sc_wait(struct hfi1_devdata * dd)1051*4882a593Smuzhiyun void sc_wait(struct hfi1_devdata *dd)
1052*4882a593Smuzhiyun {
1053*4882a593Smuzhiyun 	int i;
1054*4882a593Smuzhiyun 
1055*4882a593Smuzhiyun 	for (i = 0; i < dd->num_send_contexts; i++) {
1056*4882a593Smuzhiyun 		struct send_context *sc = dd->send_contexts[i].sc;
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun 		if (!sc)
1059*4882a593Smuzhiyun 			continue;
1060*4882a593Smuzhiyun 		sc_wait_for_packet_egress(sc, 0);
1061*4882a593Smuzhiyun 	}
1062*4882a593Smuzhiyun }
1063*4882a593Smuzhiyun 
1064*4882a593Smuzhiyun /*
1065*4882a593Smuzhiyun  * Restart a context after it has been halted due to error.
1066*4882a593Smuzhiyun  *
1067*4882a593Smuzhiyun  * If the first step fails - wait for the halt to be asserted, return early.
1068*4882a593Smuzhiyun  * Otherwise complain about timeouts but keep going.
1069*4882a593Smuzhiyun  *
1070*4882a593Smuzhiyun  * It is expected that allocations (enabled flag bit) have been shut off
1071*4882a593Smuzhiyun  * already (only applies to kernel contexts).
1072*4882a593Smuzhiyun  */
sc_restart(struct send_context * sc)1073*4882a593Smuzhiyun int sc_restart(struct send_context *sc)
1074*4882a593Smuzhiyun {
1075*4882a593Smuzhiyun 	struct hfi1_devdata *dd = sc->dd;
1076*4882a593Smuzhiyun 	u64 reg;
1077*4882a593Smuzhiyun 	u32 loop;
1078*4882a593Smuzhiyun 	int count;
1079*4882a593Smuzhiyun 
1080*4882a593Smuzhiyun 	/* bounce off if not halted, or being free'd */
1081*4882a593Smuzhiyun 	if (!(sc->flags & SCF_HALTED) || (sc->flags & SCF_IN_FREE))
1082*4882a593Smuzhiyun 		return -EINVAL;
1083*4882a593Smuzhiyun 
1084*4882a593Smuzhiyun 	dd_dev_info(dd, "restarting send context %u(%u)\n", sc->sw_index,
1085*4882a593Smuzhiyun 		    sc->hw_context);
1086*4882a593Smuzhiyun 
1087*4882a593Smuzhiyun 	/*
1088*4882a593Smuzhiyun 	 * Step 1: Wait for the context to actually halt.
1089*4882a593Smuzhiyun 	 *
1090*4882a593Smuzhiyun 	 * The error interrupt is asynchronous to actually setting halt
1091*4882a593Smuzhiyun 	 * on the context.
1092*4882a593Smuzhiyun 	 */
1093*4882a593Smuzhiyun 	loop = 0;
1094*4882a593Smuzhiyun 	while (1) {
1095*4882a593Smuzhiyun 		reg = read_kctxt_csr(dd, sc->hw_context, SC(STATUS));
1096*4882a593Smuzhiyun 		if (reg & SC(STATUS_CTXT_HALTED_SMASK))
1097*4882a593Smuzhiyun 			break;
1098*4882a593Smuzhiyun 		if (loop > 100) {
1099*4882a593Smuzhiyun 			dd_dev_err(dd, "%s: context %u(%u) not halting, skipping\n",
1100*4882a593Smuzhiyun 				   __func__, sc->sw_index, sc->hw_context);
1101*4882a593Smuzhiyun 			return -ETIME;
1102*4882a593Smuzhiyun 		}
1103*4882a593Smuzhiyun 		loop++;
1104*4882a593Smuzhiyun 		udelay(1);
1105*4882a593Smuzhiyun 	}
1106*4882a593Smuzhiyun 
1107*4882a593Smuzhiyun 	/*
1108*4882a593Smuzhiyun 	 * Step 2: Ensure no users are still trying to write to PIO.
1109*4882a593Smuzhiyun 	 *
1110*4882a593Smuzhiyun 	 * For kernel contexts, we have already turned off buffer allocation.
1111*4882a593Smuzhiyun 	 * Now wait for the buffer count to go to zero.
1112*4882a593Smuzhiyun 	 *
1113*4882a593Smuzhiyun 	 * For user contexts, the user handling code has cut off write access
1114*4882a593Smuzhiyun 	 * to the context's PIO pages before calling this routine and will
1115*4882a593Smuzhiyun 	 * restore write access after this routine returns.
1116*4882a593Smuzhiyun 	 */
1117*4882a593Smuzhiyun 	if (sc->type != SC_USER) {
1118*4882a593Smuzhiyun 		/* kernel context */
1119*4882a593Smuzhiyun 		loop = 0;
1120*4882a593Smuzhiyun 		while (1) {
1121*4882a593Smuzhiyun 			count = get_buffers_allocated(sc);
1122*4882a593Smuzhiyun 			if (count == 0)
1123*4882a593Smuzhiyun 				break;
1124*4882a593Smuzhiyun 			if (loop > 100) {
1125*4882a593Smuzhiyun 				dd_dev_err(dd,
1126*4882a593Smuzhiyun 					   "%s: context %u(%u) timeout waiting for PIO buffers to zero, remaining %d\n",
1127*4882a593Smuzhiyun 					   __func__, sc->sw_index,
1128*4882a593Smuzhiyun 					   sc->hw_context, count);
1129*4882a593Smuzhiyun 			}
1130*4882a593Smuzhiyun 			loop++;
1131*4882a593Smuzhiyun 			udelay(1);
1132*4882a593Smuzhiyun 		}
1133*4882a593Smuzhiyun 	}
1134*4882a593Smuzhiyun 
1135*4882a593Smuzhiyun 	/*
1136*4882a593Smuzhiyun 	 * Step 3: Wait for all packets to egress.
1137*4882a593Smuzhiyun 	 * This is done while disabling the send context
1138*4882a593Smuzhiyun 	 *
1139*4882a593Smuzhiyun 	 * Step 4: Disable the context
1140*4882a593Smuzhiyun 	 *
1141*4882a593Smuzhiyun 	 * This is a superset of the halt.  After the disable, the
1142*4882a593Smuzhiyun 	 * errors can be cleared.
1143*4882a593Smuzhiyun 	 */
1144*4882a593Smuzhiyun 	sc_disable(sc);
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 	/*
1147*4882a593Smuzhiyun 	 * Step 5: Enable the context
1148*4882a593Smuzhiyun 	 *
1149*4882a593Smuzhiyun 	 * This enable will clear the halted flag and per-send context
1150*4882a593Smuzhiyun 	 * error flags.
1151*4882a593Smuzhiyun 	 */
1152*4882a593Smuzhiyun 	return sc_enable(sc);
1153*4882a593Smuzhiyun }
1154*4882a593Smuzhiyun 
1155*4882a593Smuzhiyun /*
1156*4882a593Smuzhiyun  * PIO freeze processing.  To be called after the TXE block is fully frozen.
1157*4882a593Smuzhiyun  * Go through all frozen send contexts and disable them.  The contexts are
1158*4882a593Smuzhiyun  * already stopped by the freeze.
1159*4882a593Smuzhiyun  */
pio_freeze(struct hfi1_devdata * dd)1160*4882a593Smuzhiyun void pio_freeze(struct hfi1_devdata *dd)
1161*4882a593Smuzhiyun {
1162*4882a593Smuzhiyun 	struct send_context *sc;
1163*4882a593Smuzhiyun 	int i;
1164*4882a593Smuzhiyun 
1165*4882a593Smuzhiyun 	for (i = 0; i < dd->num_send_contexts; i++) {
1166*4882a593Smuzhiyun 		sc = dd->send_contexts[i].sc;
1167*4882a593Smuzhiyun 		/*
1168*4882a593Smuzhiyun 		 * Don't disable unallocated, unfrozen, or user send contexts.
1169*4882a593Smuzhiyun 		 * User send contexts will be disabled when the process
1170*4882a593Smuzhiyun 		 * calls into the driver to reset its context.
1171*4882a593Smuzhiyun 		 */
1172*4882a593Smuzhiyun 		if (!sc || !(sc->flags & SCF_FROZEN) || sc->type == SC_USER)
1173*4882a593Smuzhiyun 			continue;
1174*4882a593Smuzhiyun 
1175*4882a593Smuzhiyun 		/* only need to disable, the context is already stopped */
1176*4882a593Smuzhiyun 		sc_disable(sc);
1177*4882a593Smuzhiyun 	}
1178*4882a593Smuzhiyun }
1179*4882a593Smuzhiyun 
1180*4882a593Smuzhiyun /*
1181*4882a593Smuzhiyun  * Unfreeze PIO for kernel send contexts.  The precondition for calling this
1182*4882a593Smuzhiyun  * is that all PIO send contexts have been disabled and the SPC freeze has
1183*4882a593Smuzhiyun  * been cleared.  Now perform the last step and re-enable each kernel context.
1184*4882a593Smuzhiyun  * User (PSM) processing will occur when PSM calls into the kernel to
1185*4882a593Smuzhiyun  * acknowledge the freeze.
1186*4882a593Smuzhiyun  */
pio_kernel_unfreeze(struct hfi1_devdata * dd)1187*4882a593Smuzhiyun void pio_kernel_unfreeze(struct hfi1_devdata *dd)
1188*4882a593Smuzhiyun {
1189*4882a593Smuzhiyun 	struct send_context *sc;
1190*4882a593Smuzhiyun 	int i;
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun 	for (i = 0; i < dd->num_send_contexts; i++) {
1193*4882a593Smuzhiyun 		sc = dd->send_contexts[i].sc;
1194*4882a593Smuzhiyun 		if (!sc || !(sc->flags & SCF_FROZEN) || sc->type == SC_USER)
1195*4882a593Smuzhiyun 			continue;
1196*4882a593Smuzhiyun 		if (sc->flags & SCF_LINK_DOWN)
1197*4882a593Smuzhiyun 			continue;
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun 		sc_enable(sc);	/* will clear the sc frozen flag */
1200*4882a593Smuzhiyun 	}
1201*4882a593Smuzhiyun }
1202*4882a593Smuzhiyun 
1203*4882a593Smuzhiyun /**
1204*4882a593Smuzhiyun  * pio_kernel_linkup() - Re-enable send contexts after linkup event
1205*4882a593Smuzhiyun  * @dd: valid devive data
1206*4882a593Smuzhiyun  *
1207*4882a593Smuzhiyun  * When the link goes down, the freeze path is taken.  However, a link down
1208*4882a593Smuzhiyun  * event is different from a freeze because if the send context is re-enabled
1209*4882a593Smuzhiyun  * whowever is sending data will start sending data again, which will hang
1210*4882a593Smuzhiyun  * any QP that is sending data.
1211*4882a593Smuzhiyun  *
1212*4882a593Smuzhiyun  * The freeze path now looks at the type of event that occurs and takes this
1213*4882a593Smuzhiyun  * path for link down event.
1214*4882a593Smuzhiyun  */
pio_kernel_linkup(struct hfi1_devdata * dd)1215*4882a593Smuzhiyun void pio_kernel_linkup(struct hfi1_devdata *dd)
1216*4882a593Smuzhiyun {
1217*4882a593Smuzhiyun 	struct send_context *sc;
1218*4882a593Smuzhiyun 	int i;
1219*4882a593Smuzhiyun 
1220*4882a593Smuzhiyun 	for (i = 0; i < dd->num_send_contexts; i++) {
1221*4882a593Smuzhiyun 		sc = dd->send_contexts[i].sc;
1222*4882a593Smuzhiyun 		if (!sc || !(sc->flags & SCF_LINK_DOWN) || sc->type == SC_USER)
1223*4882a593Smuzhiyun 			continue;
1224*4882a593Smuzhiyun 
1225*4882a593Smuzhiyun 		sc_enable(sc);	/* will clear the sc link down flag */
1226*4882a593Smuzhiyun 	}
1227*4882a593Smuzhiyun }
1228*4882a593Smuzhiyun 
1229*4882a593Smuzhiyun /*
1230*4882a593Smuzhiyun  * Wait for the SendPioInitCtxt.PioInitInProgress bit to clear.
1231*4882a593Smuzhiyun  * Returns:
1232*4882a593Smuzhiyun  *	-ETIMEDOUT - if we wait too long
1233*4882a593Smuzhiyun  *	-EIO	   - if there was an error
1234*4882a593Smuzhiyun  */
pio_init_wait_progress(struct hfi1_devdata * dd)1235*4882a593Smuzhiyun static int pio_init_wait_progress(struct hfi1_devdata *dd)
1236*4882a593Smuzhiyun {
1237*4882a593Smuzhiyun 	u64 reg;
1238*4882a593Smuzhiyun 	int max, count = 0;
1239*4882a593Smuzhiyun 
1240*4882a593Smuzhiyun 	/* max is the longest possible HW init time / delay */
1241*4882a593Smuzhiyun 	max = (dd->icode == ICODE_FPGA_EMULATION) ? 120 : 5;
1242*4882a593Smuzhiyun 	while (1) {
1243*4882a593Smuzhiyun 		reg = read_csr(dd, SEND_PIO_INIT_CTXT);
1244*4882a593Smuzhiyun 		if (!(reg & SEND_PIO_INIT_CTXT_PIO_INIT_IN_PROGRESS_SMASK))
1245*4882a593Smuzhiyun 			break;
1246*4882a593Smuzhiyun 		if (count >= max)
1247*4882a593Smuzhiyun 			return -ETIMEDOUT;
1248*4882a593Smuzhiyun 		udelay(5);
1249*4882a593Smuzhiyun 		count++;
1250*4882a593Smuzhiyun 	}
1251*4882a593Smuzhiyun 
1252*4882a593Smuzhiyun 	return reg & SEND_PIO_INIT_CTXT_PIO_INIT_ERR_SMASK ? -EIO : 0;
1253*4882a593Smuzhiyun }
1254*4882a593Smuzhiyun 
1255*4882a593Smuzhiyun /*
1256*4882a593Smuzhiyun  * Reset all of the send contexts to their power-on state.  Used
1257*4882a593Smuzhiyun  * only during manual init - no lock against sc_enable needed.
1258*4882a593Smuzhiyun  */
pio_reset_all(struct hfi1_devdata * dd)1259*4882a593Smuzhiyun void pio_reset_all(struct hfi1_devdata *dd)
1260*4882a593Smuzhiyun {
1261*4882a593Smuzhiyun 	int ret;
1262*4882a593Smuzhiyun 
1263*4882a593Smuzhiyun 	/* make sure the init engine is not busy */
1264*4882a593Smuzhiyun 	ret = pio_init_wait_progress(dd);
1265*4882a593Smuzhiyun 	/* ignore any timeout */
1266*4882a593Smuzhiyun 	if (ret == -EIO) {
1267*4882a593Smuzhiyun 		/* clear the error */
1268*4882a593Smuzhiyun 		write_csr(dd, SEND_PIO_ERR_CLEAR,
1269*4882a593Smuzhiyun 			  SEND_PIO_ERR_CLEAR_PIO_INIT_SM_IN_ERR_SMASK);
1270*4882a593Smuzhiyun 	}
1271*4882a593Smuzhiyun 
1272*4882a593Smuzhiyun 	/* reset init all */
1273*4882a593Smuzhiyun 	write_csr(dd, SEND_PIO_INIT_CTXT,
1274*4882a593Smuzhiyun 		  SEND_PIO_INIT_CTXT_PIO_ALL_CTXT_INIT_SMASK);
1275*4882a593Smuzhiyun 	udelay(2);
1276*4882a593Smuzhiyun 	ret = pio_init_wait_progress(dd);
1277*4882a593Smuzhiyun 	if (ret < 0) {
1278*4882a593Smuzhiyun 		dd_dev_err(dd,
1279*4882a593Smuzhiyun 			   "PIO send context init %s while initializing all PIO blocks\n",
1280*4882a593Smuzhiyun 			   ret == -ETIMEDOUT ? "is stuck" : "had an error");
1281*4882a593Smuzhiyun 	}
1282*4882a593Smuzhiyun }
1283*4882a593Smuzhiyun 
1284*4882a593Smuzhiyun /* enable the context */
sc_enable(struct send_context * sc)1285*4882a593Smuzhiyun int sc_enable(struct send_context *sc)
1286*4882a593Smuzhiyun {
1287*4882a593Smuzhiyun 	u64 sc_ctrl, reg, pio;
1288*4882a593Smuzhiyun 	struct hfi1_devdata *dd;
1289*4882a593Smuzhiyun 	unsigned long flags;
1290*4882a593Smuzhiyun 	int ret = 0;
1291*4882a593Smuzhiyun 
1292*4882a593Smuzhiyun 	if (!sc)
1293*4882a593Smuzhiyun 		return -EINVAL;
1294*4882a593Smuzhiyun 	dd = sc->dd;
1295*4882a593Smuzhiyun 
1296*4882a593Smuzhiyun 	/*
1297*4882a593Smuzhiyun 	 * Obtain the allocator lock to guard against any allocation
1298*4882a593Smuzhiyun 	 * attempts (which should not happen prior to context being
1299*4882a593Smuzhiyun 	 * enabled). On the release/disable side we don't need to
1300*4882a593Smuzhiyun 	 * worry about locking since the releaser will not do anything
1301*4882a593Smuzhiyun 	 * if the context accounting values have not changed.
1302*4882a593Smuzhiyun 	 */
1303*4882a593Smuzhiyun 	spin_lock_irqsave(&sc->alloc_lock, flags);
1304*4882a593Smuzhiyun 	sc_ctrl = read_kctxt_csr(dd, sc->hw_context, SC(CTRL));
1305*4882a593Smuzhiyun 	if ((sc_ctrl & SC(CTRL_CTXT_ENABLE_SMASK)))
1306*4882a593Smuzhiyun 		goto unlock; /* already enabled */
1307*4882a593Smuzhiyun 
1308*4882a593Smuzhiyun 	/* IMPORTANT: only clear free and fill if transitioning 0 -> 1 */
1309*4882a593Smuzhiyun 
1310*4882a593Smuzhiyun 	*sc->hw_free = 0;
1311*4882a593Smuzhiyun 	sc->free = 0;
1312*4882a593Smuzhiyun 	sc->alloc_free = 0;
1313*4882a593Smuzhiyun 	sc->fill = 0;
1314*4882a593Smuzhiyun 	sc->fill_wrap = 0;
1315*4882a593Smuzhiyun 	sc->sr_head = 0;
1316*4882a593Smuzhiyun 	sc->sr_tail = 0;
1317*4882a593Smuzhiyun 	sc->flags = 0;
1318*4882a593Smuzhiyun 	/* the alloc lock insures no fast path allocation */
1319*4882a593Smuzhiyun 	reset_buffers_allocated(sc);
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun 	/*
1322*4882a593Smuzhiyun 	 * Clear all per-context errors.  Some of these will be set when
1323*4882a593Smuzhiyun 	 * we are re-enabling after a context halt.  Now that the context
1324*4882a593Smuzhiyun 	 * is disabled, the halt will not clear until after the PIO init
1325*4882a593Smuzhiyun 	 * engine runs below.
1326*4882a593Smuzhiyun 	 */
1327*4882a593Smuzhiyun 	reg = read_kctxt_csr(dd, sc->hw_context, SC(ERR_STATUS));
1328*4882a593Smuzhiyun 	if (reg)
1329*4882a593Smuzhiyun 		write_kctxt_csr(dd, sc->hw_context, SC(ERR_CLEAR), reg);
1330*4882a593Smuzhiyun 
1331*4882a593Smuzhiyun 	/*
1332*4882a593Smuzhiyun 	 * The HW PIO initialization engine can handle only one init
1333*4882a593Smuzhiyun 	 * request at a time. Serialize access to each device's engine.
1334*4882a593Smuzhiyun 	 */
1335*4882a593Smuzhiyun 	spin_lock(&dd->sc_init_lock);
1336*4882a593Smuzhiyun 	/*
1337*4882a593Smuzhiyun 	 * Since access to this code block is serialized and
1338*4882a593Smuzhiyun 	 * each access waits for the initialization to complete
1339*4882a593Smuzhiyun 	 * before releasing the lock, the PIO initialization engine
1340*4882a593Smuzhiyun 	 * should not be in use, so we don't have to wait for the
1341*4882a593Smuzhiyun 	 * InProgress bit to go down.
1342*4882a593Smuzhiyun 	 */
1343*4882a593Smuzhiyun 	pio = ((sc->hw_context & SEND_PIO_INIT_CTXT_PIO_CTXT_NUM_MASK) <<
1344*4882a593Smuzhiyun 	       SEND_PIO_INIT_CTXT_PIO_CTXT_NUM_SHIFT) |
1345*4882a593Smuzhiyun 		SEND_PIO_INIT_CTXT_PIO_SINGLE_CTXT_INIT_SMASK;
1346*4882a593Smuzhiyun 	write_csr(dd, SEND_PIO_INIT_CTXT, pio);
1347*4882a593Smuzhiyun 	/*
1348*4882a593Smuzhiyun 	 * Wait until the engine is done.  Give the chip the required time
1349*4882a593Smuzhiyun 	 * so, hopefully, we read the register just once.
1350*4882a593Smuzhiyun 	 */
1351*4882a593Smuzhiyun 	udelay(2);
1352*4882a593Smuzhiyun 	ret = pio_init_wait_progress(dd);
1353*4882a593Smuzhiyun 	spin_unlock(&dd->sc_init_lock);
1354*4882a593Smuzhiyun 	if (ret) {
1355*4882a593Smuzhiyun 		dd_dev_err(dd,
1356*4882a593Smuzhiyun 			   "sctxt%u(%u): Context not enabled due to init failure %d\n",
1357*4882a593Smuzhiyun 			   sc->sw_index, sc->hw_context, ret);
1358*4882a593Smuzhiyun 		goto unlock;
1359*4882a593Smuzhiyun 	}
1360*4882a593Smuzhiyun 
1361*4882a593Smuzhiyun 	/*
1362*4882a593Smuzhiyun 	 * All is well. Enable the context.
1363*4882a593Smuzhiyun 	 */
1364*4882a593Smuzhiyun 	sc_ctrl |= SC(CTRL_CTXT_ENABLE_SMASK);
1365*4882a593Smuzhiyun 	write_kctxt_csr(dd, sc->hw_context, SC(CTRL), sc_ctrl);
1366*4882a593Smuzhiyun 	/*
1367*4882a593Smuzhiyun 	 * Read SendCtxtCtrl to force the write out and prevent a timing
1368*4882a593Smuzhiyun 	 * hazard where a PIO write may reach the context before the enable.
1369*4882a593Smuzhiyun 	 */
1370*4882a593Smuzhiyun 	read_kctxt_csr(dd, sc->hw_context, SC(CTRL));
1371*4882a593Smuzhiyun 	sc->flags |= SCF_ENABLED;
1372*4882a593Smuzhiyun 
1373*4882a593Smuzhiyun unlock:
1374*4882a593Smuzhiyun 	spin_unlock_irqrestore(&sc->alloc_lock, flags);
1375*4882a593Smuzhiyun 
1376*4882a593Smuzhiyun 	return ret;
1377*4882a593Smuzhiyun }
1378*4882a593Smuzhiyun 
1379*4882a593Smuzhiyun /* force a credit return on the context */
sc_return_credits(struct send_context * sc)1380*4882a593Smuzhiyun void sc_return_credits(struct send_context *sc)
1381*4882a593Smuzhiyun {
1382*4882a593Smuzhiyun 	if (!sc)
1383*4882a593Smuzhiyun 		return;
1384*4882a593Smuzhiyun 
1385*4882a593Smuzhiyun 	/* a 0->1 transition schedules a credit return */
1386*4882a593Smuzhiyun 	write_kctxt_csr(sc->dd, sc->hw_context, SC(CREDIT_FORCE),
1387*4882a593Smuzhiyun 			SC(CREDIT_FORCE_FORCE_RETURN_SMASK));
1388*4882a593Smuzhiyun 	/*
1389*4882a593Smuzhiyun 	 * Ensure that the write is flushed and the credit return is
1390*4882a593Smuzhiyun 	 * scheduled. We care more about the 0 -> 1 transition.
1391*4882a593Smuzhiyun 	 */
1392*4882a593Smuzhiyun 	read_kctxt_csr(sc->dd, sc->hw_context, SC(CREDIT_FORCE));
1393*4882a593Smuzhiyun 	/* set back to 0 for next time */
1394*4882a593Smuzhiyun 	write_kctxt_csr(sc->dd, sc->hw_context, SC(CREDIT_FORCE), 0);
1395*4882a593Smuzhiyun }
1396*4882a593Smuzhiyun 
1397*4882a593Smuzhiyun /* allow all in-flight packets to drain on the context */
sc_flush(struct send_context * sc)1398*4882a593Smuzhiyun void sc_flush(struct send_context *sc)
1399*4882a593Smuzhiyun {
1400*4882a593Smuzhiyun 	if (!sc)
1401*4882a593Smuzhiyun 		return;
1402*4882a593Smuzhiyun 
1403*4882a593Smuzhiyun 	sc_wait_for_packet_egress(sc, 1);
1404*4882a593Smuzhiyun }
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun /* drop all packets on the context, no waiting until they are sent */
sc_drop(struct send_context * sc)1407*4882a593Smuzhiyun void sc_drop(struct send_context *sc)
1408*4882a593Smuzhiyun {
1409*4882a593Smuzhiyun 	if (!sc)
1410*4882a593Smuzhiyun 		return;
1411*4882a593Smuzhiyun 
1412*4882a593Smuzhiyun 	dd_dev_info(sc->dd, "%s: context %u(%u) - not implemented\n",
1413*4882a593Smuzhiyun 		    __func__, sc->sw_index, sc->hw_context);
1414*4882a593Smuzhiyun }
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun /*
1417*4882a593Smuzhiyun  * Start the software reaction to a context halt or SPC freeze:
1418*4882a593Smuzhiyun  *	- mark the context as halted or frozen
1419*4882a593Smuzhiyun  *	- stop buffer allocations
1420*4882a593Smuzhiyun  *
1421*4882a593Smuzhiyun  * Called from the error interrupt.  Other work is deferred until
1422*4882a593Smuzhiyun  * out of the interrupt.
1423*4882a593Smuzhiyun  */
sc_stop(struct send_context * sc,int flag)1424*4882a593Smuzhiyun void sc_stop(struct send_context *sc, int flag)
1425*4882a593Smuzhiyun {
1426*4882a593Smuzhiyun 	unsigned long flags;
1427*4882a593Smuzhiyun 
1428*4882a593Smuzhiyun 	/* stop buffer allocations */
1429*4882a593Smuzhiyun 	spin_lock_irqsave(&sc->alloc_lock, flags);
1430*4882a593Smuzhiyun 	/* mark the context */
1431*4882a593Smuzhiyun 	sc->flags |= flag;
1432*4882a593Smuzhiyun 	sc->flags &= ~SCF_ENABLED;
1433*4882a593Smuzhiyun 	spin_unlock_irqrestore(&sc->alloc_lock, flags);
1434*4882a593Smuzhiyun 	wake_up(&sc->halt_wait);
1435*4882a593Smuzhiyun }
1436*4882a593Smuzhiyun 
1437*4882a593Smuzhiyun #define BLOCK_DWORDS (PIO_BLOCK_SIZE / sizeof(u32))
1438*4882a593Smuzhiyun #define dwords_to_blocks(x) DIV_ROUND_UP(x, BLOCK_DWORDS)
1439*4882a593Smuzhiyun 
1440*4882a593Smuzhiyun /*
1441*4882a593Smuzhiyun  * The send context buffer "allocator".
1442*4882a593Smuzhiyun  *
1443*4882a593Smuzhiyun  * @sc: the PIO send context we are allocating from
1444*4882a593Smuzhiyun  * @len: length of whole packet - including PBC - in dwords
1445*4882a593Smuzhiyun  * @cb: optional callback to call when the buffer is finished sending
1446*4882a593Smuzhiyun  * @arg: argument for cb
1447*4882a593Smuzhiyun  *
1448*4882a593Smuzhiyun  * Return a pointer to a PIO buffer, NULL if not enough room, -ECOMM
1449*4882a593Smuzhiyun  * when link is down.
1450*4882a593Smuzhiyun  */
sc_buffer_alloc(struct send_context * sc,u32 dw_len,pio_release_cb cb,void * arg)1451*4882a593Smuzhiyun struct pio_buf *sc_buffer_alloc(struct send_context *sc, u32 dw_len,
1452*4882a593Smuzhiyun 				pio_release_cb cb, void *arg)
1453*4882a593Smuzhiyun {
1454*4882a593Smuzhiyun 	struct pio_buf *pbuf = NULL;
1455*4882a593Smuzhiyun 	unsigned long flags;
1456*4882a593Smuzhiyun 	unsigned long avail;
1457*4882a593Smuzhiyun 	unsigned long blocks = dwords_to_blocks(dw_len);
1458*4882a593Smuzhiyun 	u32 fill_wrap;
1459*4882a593Smuzhiyun 	int trycount = 0;
1460*4882a593Smuzhiyun 	u32 head, next;
1461*4882a593Smuzhiyun 
1462*4882a593Smuzhiyun 	spin_lock_irqsave(&sc->alloc_lock, flags);
1463*4882a593Smuzhiyun 	if (!(sc->flags & SCF_ENABLED)) {
1464*4882a593Smuzhiyun 		spin_unlock_irqrestore(&sc->alloc_lock, flags);
1465*4882a593Smuzhiyun 		return ERR_PTR(-ECOMM);
1466*4882a593Smuzhiyun 	}
1467*4882a593Smuzhiyun 
1468*4882a593Smuzhiyun retry:
1469*4882a593Smuzhiyun 	avail = (unsigned long)sc->credits - (sc->fill - sc->alloc_free);
1470*4882a593Smuzhiyun 	if (blocks > avail) {
1471*4882a593Smuzhiyun 		/* not enough room */
1472*4882a593Smuzhiyun 		if (unlikely(trycount))	{ /* already tried to get more room */
1473*4882a593Smuzhiyun 			spin_unlock_irqrestore(&sc->alloc_lock, flags);
1474*4882a593Smuzhiyun 			goto done;
1475*4882a593Smuzhiyun 		}
1476*4882a593Smuzhiyun 		/* copy from receiver cache line and recalculate */
1477*4882a593Smuzhiyun 		sc->alloc_free = READ_ONCE(sc->free);
1478*4882a593Smuzhiyun 		avail =
1479*4882a593Smuzhiyun 			(unsigned long)sc->credits -
1480*4882a593Smuzhiyun 			(sc->fill - sc->alloc_free);
1481*4882a593Smuzhiyun 		if (blocks > avail) {
1482*4882a593Smuzhiyun 			/* still no room, actively update */
1483*4882a593Smuzhiyun 			sc_release_update(sc);
1484*4882a593Smuzhiyun 			sc->alloc_free = READ_ONCE(sc->free);
1485*4882a593Smuzhiyun 			trycount++;
1486*4882a593Smuzhiyun 			goto retry;
1487*4882a593Smuzhiyun 		}
1488*4882a593Smuzhiyun 	}
1489*4882a593Smuzhiyun 
1490*4882a593Smuzhiyun 	/* there is enough room */
1491*4882a593Smuzhiyun 
1492*4882a593Smuzhiyun 	preempt_disable();
1493*4882a593Smuzhiyun 	this_cpu_inc(*sc->buffers_allocated);
1494*4882a593Smuzhiyun 
1495*4882a593Smuzhiyun 	/* read this once */
1496*4882a593Smuzhiyun 	head = sc->sr_head;
1497*4882a593Smuzhiyun 
1498*4882a593Smuzhiyun 	/* "allocate" the buffer */
1499*4882a593Smuzhiyun 	sc->fill += blocks;
1500*4882a593Smuzhiyun 	fill_wrap = sc->fill_wrap;
1501*4882a593Smuzhiyun 	sc->fill_wrap += blocks;
1502*4882a593Smuzhiyun 	if (sc->fill_wrap >= sc->credits)
1503*4882a593Smuzhiyun 		sc->fill_wrap = sc->fill_wrap - sc->credits;
1504*4882a593Smuzhiyun 
1505*4882a593Smuzhiyun 	/*
1506*4882a593Smuzhiyun 	 * Fill the parts that the releaser looks at before moving the head.
1507*4882a593Smuzhiyun 	 * The only necessary piece is the sent_at field.  The credits
1508*4882a593Smuzhiyun 	 * we have just allocated cannot have been returned yet, so the
1509*4882a593Smuzhiyun 	 * cb and arg will not be looked at for a "while".  Put them
1510*4882a593Smuzhiyun 	 * on this side of the memory barrier anyway.
1511*4882a593Smuzhiyun 	 */
1512*4882a593Smuzhiyun 	pbuf = &sc->sr[head].pbuf;
1513*4882a593Smuzhiyun 	pbuf->sent_at = sc->fill;
1514*4882a593Smuzhiyun 	pbuf->cb = cb;
1515*4882a593Smuzhiyun 	pbuf->arg = arg;
1516*4882a593Smuzhiyun 	pbuf->sc = sc;	/* could be filled in at sc->sr init time */
1517*4882a593Smuzhiyun 	/* make sure this is in memory before updating the head */
1518*4882a593Smuzhiyun 
1519*4882a593Smuzhiyun 	/* calculate next head index, do not store */
1520*4882a593Smuzhiyun 	next = head + 1;
1521*4882a593Smuzhiyun 	if (next >= sc->sr_size)
1522*4882a593Smuzhiyun 		next = 0;
1523*4882a593Smuzhiyun 	/*
1524*4882a593Smuzhiyun 	 * update the head - must be last! - the releaser can look at fields
1525*4882a593Smuzhiyun 	 * in pbuf once we move the head
1526*4882a593Smuzhiyun 	 */
1527*4882a593Smuzhiyun 	smp_wmb();
1528*4882a593Smuzhiyun 	sc->sr_head = next;
1529*4882a593Smuzhiyun 	spin_unlock_irqrestore(&sc->alloc_lock, flags);
1530*4882a593Smuzhiyun 
1531*4882a593Smuzhiyun 	/* finish filling in the buffer outside the lock */
1532*4882a593Smuzhiyun 	pbuf->start = sc->base_addr + fill_wrap * PIO_BLOCK_SIZE;
1533*4882a593Smuzhiyun 	pbuf->end = sc->base_addr + sc->size;
1534*4882a593Smuzhiyun 	pbuf->qw_written = 0;
1535*4882a593Smuzhiyun 	pbuf->carry_bytes = 0;
1536*4882a593Smuzhiyun 	pbuf->carry.val64 = 0;
1537*4882a593Smuzhiyun done:
1538*4882a593Smuzhiyun 	return pbuf;
1539*4882a593Smuzhiyun }
1540*4882a593Smuzhiyun 
1541*4882a593Smuzhiyun /*
1542*4882a593Smuzhiyun  * There are at least two entities that can turn on credit return
1543*4882a593Smuzhiyun  * interrupts and they can overlap.  Avoid problems by implementing
1544*4882a593Smuzhiyun  * a count scheme that is enforced by a lock.  The lock is needed because
1545*4882a593Smuzhiyun  * the count and CSR write must be paired.
1546*4882a593Smuzhiyun  */
1547*4882a593Smuzhiyun 
1548*4882a593Smuzhiyun /*
1549*4882a593Smuzhiyun  * Start credit return interrupts.  This is managed by a count.  If already
1550*4882a593Smuzhiyun  * on, just increment the count.
1551*4882a593Smuzhiyun  */
sc_add_credit_return_intr(struct send_context * sc)1552*4882a593Smuzhiyun void sc_add_credit_return_intr(struct send_context *sc)
1553*4882a593Smuzhiyun {
1554*4882a593Smuzhiyun 	unsigned long flags;
1555*4882a593Smuzhiyun 
1556*4882a593Smuzhiyun 	/* lock must surround both the count change and the CSR update */
1557*4882a593Smuzhiyun 	spin_lock_irqsave(&sc->credit_ctrl_lock, flags);
1558*4882a593Smuzhiyun 	if (sc->credit_intr_count == 0) {
1559*4882a593Smuzhiyun 		sc->credit_ctrl |= SC(CREDIT_CTRL_CREDIT_INTR_SMASK);
1560*4882a593Smuzhiyun 		write_kctxt_csr(sc->dd, sc->hw_context,
1561*4882a593Smuzhiyun 				SC(CREDIT_CTRL), sc->credit_ctrl);
1562*4882a593Smuzhiyun 	}
1563*4882a593Smuzhiyun 	sc->credit_intr_count++;
1564*4882a593Smuzhiyun 	spin_unlock_irqrestore(&sc->credit_ctrl_lock, flags);
1565*4882a593Smuzhiyun }
1566*4882a593Smuzhiyun 
1567*4882a593Smuzhiyun /*
1568*4882a593Smuzhiyun  * Stop credit return interrupts.  This is managed by a count.  Decrement the
1569*4882a593Smuzhiyun  * count, if the last user, then turn the credit interrupts off.
1570*4882a593Smuzhiyun  */
sc_del_credit_return_intr(struct send_context * sc)1571*4882a593Smuzhiyun void sc_del_credit_return_intr(struct send_context *sc)
1572*4882a593Smuzhiyun {
1573*4882a593Smuzhiyun 	unsigned long flags;
1574*4882a593Smuzhiyun 
1575*4882a593Smuzhiyun 	WARN_ON(sc->credit_intr_count == 0);
1576*4882a593Smuzhiyun 
1577*4882a593Smuzhiyun 	/* lock must surround both the count change and the CSR update */
1578*4882a593Smuzhiyun 	spin_lock_irqsave(&sc->credit_ctrl_lock, flags);
1579*4882a593Smuzhiyun 	sc->credit_intr_count--;
1580*4882a593Smuzhiyun 	if (sc->credit_intr_count == 0) {
1581*4882a593Smuzhiyun 		sc->credit_ctrl &= ~SC(CREDIT_CTRL_CREDIT_INTR_SMASK);
1582*4882a593Smuzhiyun 		write_kctxt_csr(sc->dd, sc->hw_context,
1583*4882a593Smuzhiyun 				SC(CREDIT_CTRL), sc->credit_ctrl);
1584*4882a593Smuzhiyun 	}
1585*4882a593Smuzhiyun 	spin_unlock_irqrestore(&sc->credit_ctrl_lock, flags);
1586*4882a593Smuzhiyun }
1587*4882a593Smuzhiyun 
1588*4882a593Smuzhiyun /*
1589*4882a593Smuzhiyun  * The caller must be careful when calling this.  All needint calls
1590*4882a593Smuzhiyun  * must be paired with !needint.
1591*4882a593Smuzhiyun  */
hfi1_sc_wantpiobuf_intr(struct send_context * sc,u32 needint)1592*4882a593Smuzhiyun void hfi1_sc_wantpiobuf_intr(struct send_context *sc, u32 needint)
1593*4882a593Smuzhiyun {
1594*4882a593Smuzhiyun 	if (needint)
1595*4882a593Smuzhiyun 		sc_add_credit_return_intr(sc);
1596*4882a593Smuzhiyun 	else
1597*4882a593Smuzhiyun 		sc_del_credit_return_intr(sc);
1598*4882a593Smuzhiyun 	trace_hfi1_wantpiointr(sc, needint, sc->credit_ctrl);
1599*4882a593Smuzhiyun 	if (needint)
1600*4882a593Smuzhiyun 		sc_return_credits(sc);
1601*4882a593Smuzhiyun }
1602*4882a593Smuzhiyun 
1603*4882a593Smuzhiyun /**
1604*4882a593Smuzhiyun  * sc_piobufavail - callback when a PIO buffer is available
1605*4882a593Smuzhiyun  * @sc: the send context
1606*4882a593Smuzhiyun  *
1607*4882a593Smuzhiyun  * This is called from the interrupt handler when a PIO buffer is
1608*4882a593Smuzhiyun  * available after hfi1_verbs_send() returned an error that no buffers were
1609*4882a593Smuzhiyun  * available. Disable the interrupt if there are no more QPs waiting.
1610*4882a593Smuzhiyun  */
sc_piobufavail(struct send_context * sc)1611*4882a593Smuzhiyun static void sc_piobufavail(struct send_context *sc)
1612*4882a593Smuzhiyun {
1613*4882a593Smuzhiyun 	struct hfi1_devdata *dd = sc->dd;
1614*4882a593Smuzhiyun 	struct list_head *list;
1615*4882a593Smuzhiyun 	struct rvt_qp *qps[PIO_WAIT_BATCH_SIZE];
1616*4882a593Smuzhiyun 	struct rvt_qp *qp;
1617*4882a593Smuzhiyun 	struct hfi1_qp_priv *priv;
1618*4882a593Smuzhiyun 	unsigned long flags;
1619*4882a593Smuzhiyun 	uint i, n = 0, top_idx = 0;
1620*4882a593Smuzhiyun 
1621*4882a593Smuzhiyun 	if (dd->send_contexts[sc->sw_index].type != SC_KERNEL &&
1622*4882a593Smuzhiyun 	    dd->send_contexts[sc->sw_index].type != SC_VL15)
1623*4882a593Smuzhiyun 		return;
1624*4882a593Smuzhiyun 	list = &sc->piowait;
1625*4882a593Smuzhiyun 	/*
1626*4882a593Smuzhiyun 	 * Note: checking that the piowait list is empty and clearing
1627*4882a593Smuzhiyun 	 * the buffer available interrupt needs to be atomic or we
1628*4882a593Smuzhiyun 	 * could end up with QPs on the wait list with the interrupt
1629*4882a593Smuzhiyun 	 * disabled.
1630*4882a593Smuzhiyun 	 */
1631*4882a593Smuzhiyun 	write_seqlock_irqsave(&sc->waitlock, flags);
1632*4882a593Smuzhiyun 	while (!list_empty(list)) {
1633*4882a593Smuzhiyun 		struct iowait *wait;
1634*4882a593Smuzhiyun 
1635*4882a593Smuzhiyun 		if (n == ARRAY_SIZE(qps))
1636*4882a593Smuzhiyun 			break;
1637*4882a593Smuzhiyun 		wait = list_first_entry(list, struct iowait, list);
1638*4882a593Smuzhiyun 		iowait_get_priority(wait);
1639*4882a593Smuzhiyun 		qp = iowait_to_qp(wait);
1640*4882a593Smuzhiyun 		priv = qp->priv;
1641*4882a593Smuzhiyun 		list_del_init(&priv->s_iowait.list);
1642*4882a593Smuzhiyun 		priv->s_iowait.lock = NULL;
1643*4882a593Smuzhiyun 		if (n) {
1644*4882a593Smuzhiyun 			priv = qps[top_idx]->priv;
1645*4882a593Smuzhiyun 			top_idx = iowait_priority_update_top(wait,
1646*4882a593Smuzhiyun 							     &priv->s_iowait,
1647*4882a593Smuzhiyun 							     n, top_idx);
1648*4882a593Smuzhiyun 		}
1649*4882a593Smuzhiyun 
1650*4882a593Smuzhiyun 		/* refcount held until actual wake up */
1651*4882a593Smuzhiyun 		qps[n++] = qp;
1652*4882a593Smuzhiyun 	}
1653*4882a593Smuzhiyun 	/*
1654*4882a593Smuzhiyun 	 * If there had been waiters and there are more
1655*4882a593Smuzhiyun 	 * insure that we redo the force to avoid a potential hang.
1656*4882a593Smuzhiyun 	 */
1657*4882a593Smuzhiyun 	if (n) {
1658*4882a593Smuzhiyun 		hfi1_sc_wantpiobuf_intr(sc, 0);
1659*4882a593Smuzhiyun 		if (!list_empty(list))
1660*4882a593Smuzhiyun 			hfi1_sc_wantpiobuf_intr(sc, 1);
1661*4882a593Smuzhiyun 	}
1662*4882a593Smuzhiyun 	write_sequnlock_irqrestore(&sc->waitlock, flags);
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun 	/* Wake up the top-priority one first */
1665*4882a593Smuzhiyun 	if (n)
1666*4882a593Smuzhiyun 		hfi1_qp_wakeup(qps[top_idx],
1667*4882a593Smuzhiyun 			       RVT_S_WAIT_PIO | HFI1_S_WAIT_PIO_DRAIN);
1668*4882a593Smuzhiyun 	for (i = 0; i < n; i++)
1669*4882a593Smuzhiyun 		if (i != top_idx)
1670*4882a593Smuzhiyun 			hfi1_qp_wakeup(qps[i],
1671*4882a593Smuzhiyun 				       RVT_S_WAIT_PIO | HFI1_S_WAIT_PIO_DRAIN);
1672*4882a593Smuzhiyun }
1673*4882a593Smuzhiyun 
1674*4882a593Smuzhiyun /* translate a send credit update to a bit code of reasons */
fill_code(u64 hw_free)1675*4882a593Smuzhiyun static inline int fill_code(u64 hw_free)
1676*4882a593Smuzhiyun {
1677*4882a593Smuzhiyun 	int code = 0;
1678*4882a593Smuzhiyun 
1679*4882a593Smuzhiyun 	if (hw_free & CR_STATUS_SMASK)
1680*4882a593Smuzhiyun 		code |= PRC_STATUS_ERR;
1681*4882a593Smuzhiyun 	if (hw_free & CR_CREDIT_RETURN_DUE_TO_PBC_SMASK)
1682*4882a593Smuzhiyun 		code |= PRC_PBC;
1683*4882a593Smuzhiyun 	if (hw_free & CR_CREDIT_RETURN_DUE_TO_THRESHOLD_SMASK)
1684*4882a593Smuzhiyun 		code |= PRC_THRESHOLD;
1685*4882a593Smuzhiyun 	if (hw_free & CR_CREDIT_RETURN_DUE_TO_ERR_SMASK)
1686*4882a593Smuzhiyun 		code |= PRC_FILL_ERR;
1687*4882a593Smuzhiyun 	if (hw_free & CR_CREDIT_RETURN_DUE_TO_FORCE_SMASK)
1688*4882a593Smuzhiyun 		code |= PRC_SC_DISABLE;
1689*4882a593Smuzhiyun 	return code;
1690*4882a593Smuzhiyun }
1691*4882a593Smuzhiyun 
1692*4882a593Smuzhiyun /* use the jiffies compare to get the wrap right */
1693*4882a593Smuzhiyun #define sent_before(a, b) time_before(a, b)	/* a < b */
1694*4882a593Smuzhiyun 
1695*4882a593Smuzhiyun /*
1696*4882a593Smuzhiyun  * The send context buffer "releaser".
1697*4882a593Smuzhiyun  */
sc_release_update(struct send_context * sc)1698*4882a593Smuzhiyun void sc_release_update(struct send_context *sc)
1699*4882a593Smuzhiyun {
1700*4882a593Smuzhiyun 	struct pio_buf *pbuf;
1701*4882a593Smuzhiyun 	u64 hw_free;
1702*4882a593Smuzhiyun 	u32 head, tail;
1703*4882a593Smuzhiyun 	unsigned long old_free;
1704*4882a593Smuzhiyun 	unsigned long free;
1705*4882a593Smuzhiyun 	unsigned long extra;
1706*4882a593Smuzhiyun 	unsigned long flags;
1707*4882a593Smuzhiyun 	int code;
1708*4882a593Smuzhiyun 
1709*4882a593Smuzhiyun 	if (!sc)
1710*4882a593Smuzhiyun 		return;
1711*4882a593Smuzhiyun 
1712*4882a593Smuzhiyun 	spin_lock_irqsave(&sc->release_lock, flags);
1713*4882a593Smuzhiyun 	/* update free */
1714*4882a593Smuzhiyun 	hw_free = le64_to_cpu(*sc->hw_free);		/* volatile read */
1715*4882a593Smuzhiyun 	old_free = sc->free;
1716*4882a593Smuzhiyun 	extra = (((hw_free & CR_COUNTER_SMASK) >> CR_COUNTER_SHIFT)
1717*4882a593Smuzhiyun 			- (old_free & CR_COUNTER_MASK))
1718*4882a593Smuzhiyun 				& CR_COUNTER_MASK;
1719*4882a593Smuzhiyun 	free = old_free + extra;
1720*4882a593Smuzhiyun 	trace_hfi1_piofree(sc, extra);
1721*4882a593Smuzhiyun 
1722*4882a593Smuzhiyun 	/* call sent buffer callbacks */
1723*4882a593Smuzhiyun 	code = -1;				/* code not yet set */
1724*4882a593Smuzhiyun 	head = READ_ONCE(sc->sr_head);	/* snapshot the head */
1725*4882a593Smuzhiyun 	tail = sc->sr_tail;
1726*4882a593Smuzhiyun 	while (head != tail) {
1727*4882a593Smuzhiyun 		pbuf = &sc->sr[tail].pbuf;
1728*4882a593Smuzhiyun 
1729*4882a593Smuzhiyun 		if (sent_before(free, pbuf->sent_at)) {
1730*4882a593Smuzhiyun 			/* not sent yet */
1731*4882a593Smuzhiyun 			break;
1732*4882a593Smuzhiyun 		}
1733*4882a593Smuzhiyun 		if (pbuf->cb) {
1734*4882a593Smuzhiyun 			if (code < 0) /* fill in code on first user */
1735*4882a593Smuzhiyun 				code = fill_code(hw_free);
1736*4882a593Smuzhiyun 			(*pbuf->cb)(pbuf->arg, code);
1737*4882a593Smuzhiyun 		}
1738*4882a593Smuzhiyun 
1739*4882a593Smuzhiyun 		tail++;
1740*4882a593Smuzhiyun 		if (tail >= sc->sr_size)
1741*4882a593Smuzhiyun 			tail = 0;
1742*4882a593Smuzhiyun 	}
1743*4882a593Smuzhiyun 	sc->sr_tail = tail;
1744*4882a593Smuzhiyun 	/* make sure tail is updated before free */
1745*4882a593Smuzhiyun 	smp_wmb();
1746*4882a593Smuzhiyun 	sc->free = free;
1747*4882a593Smuzhiyun 	spin_unlock_irqrestore(&sc->release_lock, flags);
1748*4882a593Smuzhiyun 	sc_piobufavail(sc);
1749*4882a593Smuzhiyun }
1750*4882a593Smuzhiyun 
1751*4882a593Smuzhiyun /*
1752*4882a593Smuzhiyun  * Send context group releaser.  Argument is the send context that caused
1753*4882a593Smuzhiyun  * the interrupt.  Called from the send context interrupt handler.
1754*4882a593Smuzhiyun  *
1755*4882a593Smuzhiyun  * Call release on all contexts in the group.
1756*4882a593Smuzhiyun  *
1757*4882a593Smuzhiyun  * This routine takes the sc_lock without an irqsave because it is only
1758*4882a593Smuzhiyun  * called from an interrupt handler.  Adjust if that changes.
1759*4882a593Smuzhiyun  */
sc_group_release_update(struct hfi1_devdata * dd,u32 hw_context)1760*4882a593Smuzhiyun void sc_group_release_update(struct hfi1_devdata *dd, u32 hw_context)
1761*4882a593Smuzhiyun {
1762*4882a593Smuzhiyun 	struct send_context *sc;
1763*4882a593Smuzhiyun 	u32 sw_index;
1764*4882a593Smuzhiyun 	u32 gc, gc_end;
1765*4882a593Smuzhiyun 
1766*4882a593Smuzhiyun 	spin_lock(&dd->sc_lock);
1767*4882a593Smuzhiyun 	sw_index = dd->hw_to_sw[hw_context];
1768*4882a593Smuzhiyun 	if (unlikely(sw_index >= dd->num_send_contexts)) {
1769*4882a593Smuzhiyun 		dd_dev_err(dd, "%s: invalid hw (%u) to sw (%u) mapping\n",
1770*4882a593Smuzhiyun 			   __func__, hw_context, sw_index);
1771*4882a593Smuzhiyun 		goto done;
1772*4882a593Smuzhiyun 	}
1773*4882a593Smuzhiyun 	sc = dd->send_contexts[sw_index].sc;
1774*4882a593Smuzhiyun 	if (unlikely(!sc))
1775*4882a593Smuzhiyun 		goto done;
1776*4882a593Smuzhiyun 
1777*4882a593Smuzhiyun 	gc = group_context(hw_context, sc->group);
1778*4882a593Smuzhiyun 	gc_end = gc + group_size(sc->group);
1779*4882a593Smuzhiyun 	for (; gc < gc_end; gc++) {
1780*4882a593Smuzhiyun 		sw_index = dd->hw_to_sw[gc];
1781*4882a593Smuzhiyun 		if (unlikely(sw_index >= dd->num_send_contexts)) {
1782*4882a593Smuzhiyun 			dd_dev_err(dd,
1783*4882a593Smuzhiyun 				   "%s: invalid hw (%u) to sw (%u) mapping\n",
1784*4882a593Smuzhiyun 				   __func__, hw_context, sw_index);
1785*4882a593Smuzhiyun 			continue;
1786*4882a593Smuzhiyun 		}
1787*4882a593Smuzhiyun 		sc_release_update(dd->send_contexts[sw_index].sc);
1788*4882a593Smuzhiyun 	}
1789*4882a593Smuzhiyun done:
1790*4882a593Smuzhiyun 	spin_unlock(&dd->sc_lock);
1791*4882a593Smuzhiyun }
1792*4882a593Smuzhiyun 
1793*4882a593Smuzhiyun /*
1794*4882a593Smuzhiyun  * pio_select_send_context_vl() - select send context
1795*4882a593Smuzhiyun  * @dd: devdata
1796*4882a593Smuzhiyun  * @selector: a spreading factor
1797*4882a593Smuzhiyun  * @vl: this vl
1798*4882a593Smuzhiyun  *
1799*4882a593Smuzhiyun  * This function returns a send context based on the selector and a vl.
1800*4882a593Smuzhiyun  * The mapping fields are protected by RCU
1801*4882a593Smuzhiyun  */
pio_select_send_context_vl(struct hfi1_devdata * dd,u32 selector,u8 vl)1802*4882a593Smuzhiyun struct send_context *pio_select_send_context_vl(struct hfi1_devdata *dd,
1803*4882a593Smuzhiyun 						u32 selector, u8 vl)
1804*4882a593Smuzhiyun {
1805*4882a593Smuzhiyun 	struct pio_vl_map *m;
1806*4882a593Smuzhiyun 	struct pio_map_elem *e;
1807*4882a593Smuzhiyun 	struct send_context *rval;
1808*4882a593Smuzhiyun 
1809*4882a593Smuzhiyun 	/*
1810*4882a593Smuzhiyun 	 * NOTE This should only happen if SC->VL changed after the initial
1811*4882a593Smuzhiyun 	 * checks on the QP/AH
1812*4882a593Smuzhiyun 	 * Default will return VL0's send context below
1813*4882a593Smuzhiyun 	 */
1814*4882a593Smuzhiyun 	if (unlikely(vl >= num_vls)) {
1815*4882a593Smuzhiyun 		rval = NULL;
1816*4882a593Smuzhiyun 		goto done;
1817*4882a593Smuzhiyun 	}
1818*4882a593Smuzhiyun 
1819*4882a593Smuzhiyun 	rcu_read_lock();
1820*4882a593Smuzhiyun 	m = rcu_dereference(dd->pio_map);
1821*4882a593Smuzhiyun 	if (unlikely(!m)) {
1822*4882a593Smuzhiyun 		rcu_read_unlock();
1823*4882a593Smuzhiyun 		return dd->vld[0].sc;
1824*4882a593Smuzhiyun 	}
1825*4882a593Smuzhiyun 	e = m->map[vl & m->mask];
1826*4882a593Smuzhiyun 	rval = e->ksc[selector & e->mask];
1827*4882a593Smuzhiyun 	rcu_read_unlock();
1828*4882a593Smuzhiyun 
1829*4882a593Smuzhiyun done:
1830*4882a593Smuzhiyun 	rval = !rval ? dd->vld[0].sc : rval;
1831*4882a593Smuzhiyun 	return rval;
1832*4882a593Smuzhiyun }
1833*4882a593Smuzhiyun 
1834*4882a593Smuzhiyun /*
1835*4882a593Smuzhiyun  * pio_select_send_context_sc() - select send context
1836*4882a593Smuzhiyun  * @dd: devdata
1837*4882a593Smuzhiyun  * @selector: a spreading factor
1838*4882a593Smuzhiyun  * @sc5: the 5 bit sc
1839*4882a593Smuzhiyun  *
1840*4882a593Smuzhiyun  * This function returns an send context based on the selector and an sc
1841*4882a593Smuzhiyun  */
pio_select_send_context_sc(struct hfi1_devdata * dd,u32 selector,u8 sc5)1842*4882a593Smuzhiyun struct send_context *pio_select_send_context_sc(struct hfi1_devdata *dd,
1843*4882a593Smuzhiyun 						u32 selector, u8 sc5)
1844*4882a593Smuzhiyun {
1845*4882a593Smuzhiyun 	u8 vl = sc_to_vlt(dd, sc5);
1846*4882a593Smuzhiyun 
1847*4882a593Smuzhiyun 	return pio_select_send_context_vl(dd, selector, vl);
1848*4882a593Smuzhiyun }
1849*4882a593Smuzhiyun 
1850*4882a593Smuzhiyun /*
1851*4882a593Smuzhiyun  * Free the indicated map struct
1852*4882a593Smuzhiyun  */
pio_map_free(struct pio_vl_map * m)1853*4882a593Smuzhiyun static void pio_map_free(struct pio_vl_map *m)
1854*4882a593Smuzhiyun {
1855*4882a593Smuzhiyun 	int i;
1856*4882a593Smuzhiyun 
1857*4882a593Smuzhiyun 	for (i = 0; m && i < m->actual_vls; i++)
1858*4882a593Smuzhiyun 		kfree(m->map[i]);
1859*4882a593Smuzhiyun 	kfree(m);
1860*4882a593Smuzhiyun }
1861*4882a593Smuzhiyun 
1862*4882a593Smuzhiyun /*
1863*4882a593Smuzhiyun  * Handle RCU callback
1864*4882a593Smuzhiyun  */
pio_map_rcu_callback(struct rcu_head * list)1865*4882a593Smuzhiyun static void pio_map_rcu_callback(struct rcu_head *list)
1866*4882a593Smuzhiyun {
1867*4882a593Smuzhiyun 	struct pio_vl_map *m = container_of(list, struct pio_vl_map, list);
1868*4882a593Smuzhiyun 
1869*4882a593Smuzhiyun 	pio_map_free(m);
1870*4882a593Smuzhiyun }
1871*4882a593Smuzhiyun 
1872*4882a593Smuzhiyun /*
1873*4882a593Smuzhiyun  * Set credit return threshold for the kernel send context
1874*4882a593Smuzhiyun  */
set_threshold(struct hfi1_devdata * dd,int scontext,int i)1875*4882a593Smuzhiyun static void set_threshold(struct hfi1_devdata *dd, int scontext, int i)
1876*4882a593Smuzhiyun {
1877*4882a593Smuzhiyun 	u32 thres;
1878*4882a593Smuzhiyun 
1879*4882a593Smuzhiyun 	thres = min(sc_percent_to_threshold(dd->kernel_send_context[scontext],
1880*4882a593Smuzhiyun 					    50),
1881*4882a593Smuzhiyun 		    sc_mtu_to_threshold(dd->kernel_send_context[scontext],
1882*4882a593Smuzhiyun 					dd->vld[i].mtu,
1883*4882a593Smuzhiyun 					dd->rcd[0]->rcvhdrqentsize));
1884*4882a593Smuzhiyun 	sc_set_cr_threshold(dd->kernel_send_context[scontext], thres);
1885*4882a593Smuzhiyun }
1886*4882a593Smuzhiyun 
1887*4882a593Smuzhiyun /*
1888*4882a593Smuzhiyun  * pio_map_init - called when #vls change
1889*4882a593Smuzhiyun  * @dd: hfi1_devdata
1890*4882a593Smuzhiyun  * @port: port number
1891*4882a593Smuzhiyun  * @num_vls: number of vls
1892*4882a593Smuzhiyun  * @vl_scontexts: per vl send context mapping (optional)
1893*4882a593Smuzhiyun  *
1894*4882a593Smuzhiyun  * This routine changes the mapping based on the number of vls.
1895*4882a593Smuzhiyun  *
1896*4882a593Smuzhiyun  * vl_scontexts is used to specify a non-uniform vl/send context
1897*4882a593Smuzhiyun  * loading. NULL implies auto computing the loading and giving each
1898*4882a593Smuzhiyun  * VL an uniform distribution of send contexts per VL.
1899*4882a593Smuzhiyun  *
1900*4882a593Smuzhiyun  * The auto algorithm computers the sc_per_vl and the number of extra
1901*4882a593Smuzhiyun  * send contexts. Any extra send contexts are added from the last VL
1902*4882a593Smuzhiyun  * on down
1903*4882a593Smuzhiyun  *
1904*4882a593Smuzhiyun  * rcu locking is used here to control access to the mapping fields.
1905*4882a593Smuzhiyun  *
1906*4882a593Smuzhiyun  * If either the num_vls or num_send_contexts are non-power of 2, the
1907*4882a593Smuzhiyun  * array sizes in the struct pio_vl_map and the struct pio_map_elem are
1908*4882a593Smuzhiyun  * rounded up to the next highest power of 2 and the first entry is
1909*4882a593Smuzhiyun  * reused in a round robin fashion.
1910*4882a593Smuzhiyun  *
1911*4882a593Smuzhiyun  * If an error occurs the map change is not done and the mapping is not
1912*4882a593Smuzhiyun  * chaged.
1913*4882a593Smuzhiyun  *
1914*4882a593Smuzhiyun  */
pio_map_init(struct hfi1_devdata * dd,u8 port,u8 num_vls,u8 * vl_scontexts)1915*4882a593Smuzhiyun int pio_map_init(struct hfi1_devdata *dd, u8 port, u8 num_vls, u8 *vl_scontexts)
1916*4882a593Smuzhiyun {
1917*4882a593Smuzhiyun 	int i, j;
1918*4882a593Smuzhiyun 	int extra, sc_per_vl;
1919*4882a593Smuzhiyun 	int scontext = 1;
1920*4882a593Smuzhiyun 	int num_kernel_send_contexts = 0;
1921*4882a593Smuzhiyun 	u8 lvl_scontexts[OPA_MAX_VLS];
1922*4882a593Smuzhiyun 	struct pio_vl_map *oldmap, *newmap;
1923*4882a593Smuzhiyun 
1924*4882a593Smuzhiyun 	if (!vl_scontexts) {
1925*4882a593Smuzhiyun 		for (i = 0; i < dd->num_send_contexts; i++)
1926*4882a593Smuzhiyun 			if (dd->send_contexts[i].type == SC_KERNEL)
1927*4882a593Smuzhiyun 				num_kernel_send_contexts++;
1928*4882a593Smuzhiyun 		/* truncate divide */
1929*4882a593Smuzhiyun 		sc_per_vl = num_kernel_send_contexts / num_vls;
1930*4882a593Smuzhiyun 		/* extras */
1931*4882a593Smuzhiyun 		extra = num_kernel_send_contexts % num_vls;
1932*4882a593Smuzhiyun 		vl_scontexts = lvl_scontexts;
1933*4882a593Smuzhiyun 		/* add extras from last vl down */
1934*4882a593Smuzhiyun 		for (i = num_vls - 1; i >= 0; i--, extra--)
1935*4882a593Smuzhiyun 			vl_scontexts[i] = sc_per_vl + (extra > 0 ? 1 : 0);
1936*4882a593Smuzhiyun 	}
1937*4882a593Smuzhiyun 	/* build new map */
1938*4882a593Smuzhiyun 	newmap = kzalloc(sizeof(*newmap) +
1939*4882a593Smuzhiyun 			 roundup_pow_of_two(num_vls) *
1940*4882a593Smuzhiyun 			 sizeof(struct pio_map_elem *),
1941*4882a593Smuzhiyun 			 GFP_KERNEL);
1942*4882a593Smuzhiyun 	if (!newmap)
1943*4882a593Smuzhiyun 		goto bail;
1944*4882a593Smuzhiyun 	newmap->actual_vls = num_vls;
1945*4882a593Smuzhiyun 	newmap->vls = roundup_pow_of_two(num_vls);
1946*4882a593Smuzhiyun 	newmap->mask = (1 << ilog2(newmap->vls)) - 1;
1947*4882a593Smuzhiyun 	for (i = 0; i < newmap->vls; i++) {
1948*4882a593Smuzhiyun 		/* save for wrap around */
1949*4882a593Smuzhiyun 		int first_scontext = scontext;
1950*4882a593Smuzhiyun 
1951*4882a593Smuzhiyun 		if (i < newmap->actual_vls) {
1952*4882a593Smuzhiyun 			int sz = roundup_pow_of_two(vl_scontexts[i]);
1953*4882a593Smuzhiyun 
1954*4882a593Smuzhiyun 			/* only allocate once */
1955*4882a593Smuzhiyun 			newmap->map[i] = kzalloc(sizeof(*newmap->map[i]) +
1956*4882a593Smuzhiyun 						 sz * sizeof(struct
1957*4882a593Smuzhiyun 							     send_context *),
1958*4882a593Smuzhiyun 						 GFP_KERNEL);
1959*4882a593Smuzhiyun 			if (!newmap->map[i])
1960*4882a593Smuzhiyun 				goto bail;
1961*4882a593Smuzhiyun 			newmap->map[i]->mask = (1 << ilog2(sz)) - 1;
1962*4882a593Smuzhiyun 			/*
1963*4882a593Smuzhiyun 			 * assign send contexts and
1964*4882a593Smuzhiyun 			 * adjust credit return threshold
1965*4882a593Smuzhiyun 			 */
1966*4882a593Smuzhiyun 			for (j = 0; j < sz; j++) {
1967*4882a593Smuzhiyun 				if (dd->kernel_send_context[scontext]) {
1968*4882a593Smuzhiyun 					newmap->map[i]->ksc[j] =
1969*4882a593Smuzhiyun 					dd->kernel_send_context[scontext];
1970*4882a593Smuzhiyun 					set_threshold(dd, scontext, i);
1971*4882a593Smuzhiyun 				}
1972*4882a593Smuzhiyun 				if (++scontext >= first_scontext +
1973*4882a593Smuzhiyun 						  vl_scontexts[i])
1974*4882a593Smuzhiyun 					/* wrap back to first send context */
1975*4882a593Smuzhiyun 					scontext = first_scontext;
1976*4882a593Smuzhiyun 			}
1977*4882a593Smuzhiyun 		} else {
1978*4882a593Smuzhiyun 			/* just re-use entry without allocating */
1979*4882a593Smuzhiyun 			newmap->map[i] = newmap->map[i % num_vls];
1980*4882a593Smuzhiyun 		}
1981*4882a593Smuzhiyun 		scontext = first_scontext + vl_scontexts[i];
1982*4882a593Smuzhiyun 	}
1983*4882a593Smuzhiyun 	/* newmap in hand, save old map */
1984*4882a593Smuzhiyun 	spin_lock_irq(&dd->pio_map_lock);
1985*4882a593Smuzhiyun 	oldmap = rcu_dereference_protected(dd->pio_map,
1986*4882a593Smuzhiyun 					   lockdep_is_held(&dd->pio_map_lock));
1987*4882a593Smuzhiyun 
1988*4882a593Smuzhiyun 	/* publish newmap */
1989*4882a593Smuzhiyun 	rcu_assign_pointer(dd->pio_map, newmap);
1990*4882a593Smuzhiyun 
1991*4882a593Smuzhiyun 	spin_unlock_irq(&dd->pio_map_lock);
1992*4882a593Smuzhiyun 	/* success, free any old map after grace period */
1993*4882a593Smuzhiyun 	if (oldmap)
1994*4882a593Smuzhiyun 		call_rcu(&oldmap->list, pio_map_rcu_callback);
1995*4882a593Smuzhiyun 	return 0;
1996*4882a593Smuzhiyun bail:
1997*4882a593Smuzhiyun 	/* free any partial allocation */
1998*4882a593Smuzhiyun 	pio_map_free(newmap);
1999*4882a593Smuzhiyun 	return -ENOMEM;
2000*4882a593Smuzhiyun }
2001*4882a593Smuzhiyun 
free_pio_map(struct hfi1_devdata * dd)2002*4882a593Smuzhiyun void free_pio_map(struct hfi1_devdata *dd)
2003*4882a593Smuzhiyun {
2004*4882a593Smuzhiyun 	/* Free PIO map if allocated */
2005*4882a593Smuzhiyun 	if (rcu_access_pointer(dd->pio_map)) {
2006*4882a593Smuzhiyun 		spin_lock_irq(&dd->pio_map_lock);
2007*4882a593Smuzhiyun 		pio_map_free(rcu_access_pointer(dd->pio_map));
2008*4882a593Smuzhiyun 		RCU_INIT_POINTER(dd->pio_map, NULL);
2009*4882a593Smuzhiyun 		spin_unlock_irq(&dd->pio_map_lock);
2010*4882a593Smuzhiyun 		synchronize_rcu();
2011*4882a593Smuzhiyun 	}
2012*4882a593Smuzhiyun 	kfree(dd->kernel_send_context);
2013*4882a593Smuzhiyun 	dd->kernel_send_context = NULL;
2014*4882a593Smuzhiyun }
2015*4882a593Smuzhiyun 
init_pervl_scs(struct hfi1_devdata * dd)2016*4882a593Smuzhiyun int init_pervl_scs(struct hfi1_devdata *dd)
2017*4882a593Smuzhiyun {
2018*4882a593Smuzhiyun 	int i;
2019*4882a593Smuzhiyun 	u64 mask, all_vl_mask = (u64)0x80ff; /* VLs 0-7, 15 */
2020*4882a593Smuzhiyun 	u64 data_vls_mask = (u64)0x00ff; /* VLs 0-7 */
2021*4882a593Smuzhiyun 	u32 ctxt;
2022*4882a593Smuzhiyun 	struct hfi1_pportdata *ppd = dd->pport;
2023*4882a593Smuzhiyun 
2024*4882a593Smuzhiyun 	dd->vld[15].sc = sc_alloc(dd, SC_VL15,
2025*4882a593Smuzhiyun 				  dd->rcd[0]->rcvhdrqentsize, dd->node);
2026*4882a593Smuzhiyun 	if (!dd->vld[15].sc)
2027*4882a593Smuzhiyun 		return -ENOMEM;
2028*4882a593Smuzhiyun 
2029*4882a593Smuzhiyun 	hfi1_init_ctxt(dd->vld[15].sc);
2030*4882a593Smuzhiyun 	dd->vld[15].mtu = enum_to_mtu(OPA_MTU_2048);
2031*4882a593Smuzhiyun 
2032*4882a593Smuzhiyun 	dd->kernel_send_context = kcalloc_node(dd->num_send_contexts,
2033*4882a593Smuzhiyun 					       sizeof(struct send_context *),
2034*4882a593Smuzhiyun 					       GFP_KERNEL, dd->node);
2035*4882a593Smuzhiyun 	if (!dd->kernel_send_context)
2036*4882a593Smuzhiyun 		goto freesc15;
2037*4882a593Smuzhiyun 
2038*4882a593Smuzhiyun 	dd->kernel_send_context[0] = dd->vld[15].sc;
2039*4882a593Smuzhiyun 
2040*4882a593Smuzhiyun 	for (i = 0; i < num_vls; i++) {
2041*4882a593Smuzhiyun 		/*
2042*4882a593Smuzhiyun 		 * Since this function does not deal with a specific
2043*4882a593Smuzhiyun 		 * receive context but we need the RcvHdrQ entry size,
2044*4882a593Smuzhiyun 		 * use the size from rcd[0]. It is guaranteed to be
2045*4882a593Smuzhiyun 		 * valid at this point and will remain the same for all
2046*4882a593Smuzhiyun 		 * receive contexts.
2047*4882a593Smuzhiyun 		 */
2048*4882a593Smuzhiyun 		dd->vld[i].sc = sc_alloc(dd, SC_KERNEL,
2049*4882a593Smuzhiyun 					 dd->rcd[0]->rcvhdrqentsize, dd->node);
2050*4882a593Smuzhiyun 		if (!dd->vld[i].sc)
2051*4882a593Smuzhiyun 			goto nomem;
2052*4882a593Smuzhiyun 		dd->kernel_send_context[i + 1] = dd->vld[i].sc;
2053*4882a593Smuzhiyun 		hfi1_init_ctxt(dd->vld[i].sc);
2054*4882a593Smuzhiyun 		/* non VL15 start with the max MTU */
2055*4882a593Smuzhiyun 		dd->vld[i].mtu = hfi1_max_mtu;
2056*4882a593Smuzhiyun 	}
2057*4882a593Smuzhiyun 	for (i = num_vls; i < INIT_SC_PER_VL * num_vls; i++) {
2058*4882a593Smuzhiyun 		dd->kernel_send_context[i + 1] =
2059*4882a593Smuzhiyun 		sc_alloc(dd, SC_KERNEL, dd->rcd[0]->rcvhdrqentsize, dd->node);
2060*4882a593Smuzhiyun 		if (!dd->kernel_send_context[i + 1])
2061*4882a593Smuzhiyun 			goto nomem;
2062*4882a593Smuzhiyun 		hfi1_init_ctxt(dd->kernel_send_context[i + 1]);
2063*4882a593Smuzhiyun 	}
2064*4882a593Smuzhiyun 
2065*4882a593Smuzhiyun 	sc_enable(dd->vld[15].sc);
2066*4882a593Smuzhiyun 	ctxt = dd->vld[15].sc->hw_context;
2067*4882a593Smuzhiyun 	mask = all_vl_mask & ~(1LL << 15);
2068*4882a593Smuzhiyun 	write_kctxt_csr(dd, ctxt, SC(CHECK_VL), mask);
2069*4882a593Smuzhiyun 	dd_dev_info(dd,
2070*4882a593Smuzhiyun 		    "Using send context %u(%u) for VL15\n",
2071*4882a593Smuzhiyun 		    dd->vld[15].sc->sw_index, ctxt);
2072*4882a593Smuzhiyun 
2073*4882a593Smuzhiyun 	for (i = 0; i < num_vls; i++) {
2074*4882a593Smuzhiyun 		sc_enable(dd->vld[i].sc);
2075*4882a593Smuzhiyun 		ctxt = dd->vld[i].sc->hw_context;
2076*4882a593Smuzhiyun 		mask = all_vl_mask & ~(data_vls_mask);
2077*4882a593Smuzhiyun 		write_kctxt_csr(dd, ctxt, SC(CHECK_VL), mask);
2078*4882a593Smuzhiyun 	}
2079*4882a593Smuzhiyun 	for (i = num_vls; i < INIT_SC_PER_VL * num_vls; i++) {
2080*4882a593Smuzhiyun 		sc_enable(dd->kernel_send_context[i + 1]);
2081*4882a593Smuzhiyun 		ctxt = dd->kernel_send_context[i + 1]->hw_context;
2082*4882a593Smuzhiyun 		mask = all_vl_mask & ~(data_vls_mask);
2083*4882a593Smuzhiyun 		write_kctxt_csr(dd, ctxt, SC(CHECK_VL), mask);
2084*4882a593Smuzhiyun 	}
2085*4882a593Smuzhiyun 
2086*4882a593Smuzhiyun 	if (pio_map_init(dd, ppd->port - 1, num_vls, NULL))
2087*4882a593Smuzhiyun 		goto nomem;
2088*4882a593Smuzhiyun 	return 0;
2089*4882a593Smuzhiyun 
2090*4882a593Smuzhiyun nomem:
2091*4882a593Smuzhiyun 	for (i = 0; i < num_vls; i++) {
2092*4882a593Smuzhiyun 		sc_free(dd->vld[i].sc);
2093*4882a593Smuzhiyun 		dd->vld[i].sc = NULL;
2094*4882a593Smuzhiyun 	}
2095*4882a593Smuzhiyun 
2096*4882a593Smuzhiyun 	for (i = num_vls; i < INIT_SC_PER_VL * num_vls; i++)
2097*4882a593Smuzhiyun 		sc_free(dd->kernel_send_context[i + 1]);
2098*4882a593Smuzhiyun 
2099*4882a593Smuzhiyun 	kfree(dd->kernel_send_context);
2100*4882a593Smuzhiyun 	dd->kernel_send_context = NULL;
2101*4882a593Smuzhiyun 
2102*4882a593Smuzhiyun freesc15:
2103*4882a593Smuzhiyun 	sc_free(dd->vld[15].sc);
2104*4882a593Smuzhiyun 	return -ENOMEM;
2105*4882a593Smuzhiyun }
2106*4882a593Smuzhiyun 
init_credit_return(struct hfi1_devdata * dd)2107*4882a593Smuzhiyun int init_credit_return(struct hfi1_devdata *dd)
2108*4882a593Smuzhiyun {
2109*4882a593Smuzhiyun 	int ret;
2110*4882a593Smuzhiyun 	int i;
2111*4882a593Smuzhiyun 
2112*4882a593Smuzhiyun 	dd->cr_base = kcalloc(
2113*4882a593Smuzhiyun 		node_affinity.num_possible_nodes,
2114*4882a593Smuzhiyun 		sizeof(struct credit_return_base),
2115*4882a593Smuzhiyun 		GFP_KERNEL);
2116*4882a593Smuzhiyun 	if (!dd->cr_base) {
2117*4882a593Smuzhiyun 		ret = -ENOMEM;
2118*4882a593Smuzhiyun 		goto done;
2119*4882a593Smuzhiyun 	}
2120*4882a593Smuzhiyun 	for_each_node_with_cpus(i) {
2121*4882a593Smuzhiyun 		int bytes = TXE_NUM_CONTEXTS * sizeof(struct credit_return);
2122*4882a593Smuzhiyun 
2123*4882a593Smuzhiyun 		set_dev_node(&dd->pcidev->dev, i);
2124*4882a593Smuzhiyun 		dd->cr_base[i].va = dma_alloc_coherent(&dd->pcidev->dev,
2125*4882a593Smuzhiyun 						       bytes,
2126*4882a593Smuzhiyun 						       &dd->cr_base[i].dma,
2127*4882a593Smuzhiyun 						       GFP_KERNEL);
2128*4882a593Smuzhiyun 		if (!dd->cr_base[i].va) {
2129*4882a593Smuzhiyun 			set_dev_node(&dd->pcidev->dev, dd->node);
2130*4882a593Smuzhiyun 			dd_dev_err(dd,
2131*4882a593Smuzhiyun 				   "Unable to allocate credit return DMA range for NUMA %d\n",
2132*4882a593Smuzhiyun 				   i);
2133*4882a593Smuzhiyun 			ret = -ENOMEM;
2134*4882a593Smuzhiyun 			goto done;
2135*4882a593Smuzhiyun 		}
2136*4882a593Smuzhiyun 	}
2137*4882a593Smuzhiyun 	set_dev_node(&dd->pcidev->dev, dd->node);
2138*4882a593Smuzhiyun 
2139*4882a593Smuzhiyun 	ret = 0;
2140*4882a593Smuzhiyun done:
2141*4882a593Smuzhiyun 	return ret;
2142*4882a593Smuzhiyun }
2143*4882a593Smuzhiyun 
free_credit_return(struct hfi1_devdata * dd)2144*4882a593Smuzhiyun void free_credit_return(struct hfi1_devdata *dd)
2145*4882a593Smuzhiyun {
2146*4882a593Smuzhiyun 	int i;
2147*4882a593Smuzhiyun 
2148*4882a593Smuzhiyun 	if (!dd->cr_base)
2149*4882a593Smuzhiyun 		return;
2150*4882a593Smuzhiyun 	for (i = 0; i < node_affinity.num_possible_nodes; i++) {
2151*4882a593Smuzhiyun 		if (dd->cr_base[i].va) {
2152*4882a593Smuzhiyun 			dma_free_coherent(&dd->pcidev->dev,
2153*4882a593Smuzhiyun 					  TXE_NUM_CONTEXTS *
2154*4882a593Smuzhiyun 					  sizeof(struct credit_return),
2155*4882a593Smuzhiyun 					  dd->cr_base[i].va,
2156*4882a593Smuzhiyun 					  dd->cr_base[i].dma);
2157*4882a593Smuzhiyun 		}
2158*4882a593Smuzhiyun 	}
2159*4882a593Smuzhiyun 	kfree(dd->cr_base);
2160*4882a593Smuzhiyun 	dd->cr_base = NULL;
2161*4882a593Smuzhiyun }
2162*4882a593Smuzhiyun 
seqfile_dump_sci(struct seq_file * s,u32 i,struct send_context_info * sci)2163*4882a593Smuzhiyun void seqfile_dump_sci(struct seq_file *s, u32 i,
2164*4882a593Smuzhiyun 		      struct send_context_info *sci)
2165*4882a593Smuzhiyun {
2166*4882a593Smuzhiyun 	struct send_context *sc = sci->sc;
2167*4882a593Smuzhiyun 	u64 reg;
2168*4882a593Smuzhiyun 
2169*4882a593Smuzhiyun 	seq_printf(s, "SCI %u: type %u base %u credits %u\n",
2170*4882a593Smuzhiyun 		   i, sci->type, sci->base, sci->credits);
2171*4882a593Smuzhiyun 	seq_printf(s, "  flags 0x%x sw_inx %u hw_ctxt %u grp %u\n",
2172*4882a593Smuzhiyun 		   sc->flags,  sc->sw_index, sc->hw_context, sc->group);
2173*4882a593Smuzhiyun 	seq_printf(s, "  sr_size %u credits %u sr_head %u sr_tail %u\n",
2174*4882a593Smuzhiyun 		   sc->sr_size, sc->credits, sc->sr_head, sc->sr_tail);
2175*4882a593Smuzhiyun 	seq_printf(s, "  fill %lu free %lu fill_wrap %u alloc_free %lu\n",
2176*4882a593Smuzhiyun 		   sc->fill, sc->free, sc->fill_wrap, sc->alloc_free);
2177*4882a593Smuzhiyun 	seq_printf(s, "  credit_intr_count %u credit_ctrl 0x%llx\n",
2178*4882a593Smuzhiyun 		   sc->credit_intr_count, sc->credit_ctrl);
2179*4882a593Smuzhiyun 	reg = read_kctxt_csr(sc->dd, sc->hw_context, SC(CREDIT_STATUS));
2180*4882a593Smuzhiyun 	seq_printf(s, "  *hw_free %llu CurrentFree %llu LastReturned %llu\n",
2181*4882a593Smuzhiyun 		   (le64_to_cpu(*sc->hw_free) & CR_COUNTER_SMASK) >>
2182*4882a593Smuzhiyun 		    CR_COUNTER_SHIFT,
2183*4882a593Smuzhiyun 		   (reg >> SC(CREDIT_STATUS_CURRENT_FREE_COUNTER_SHIFT)) &
2184*4882a593Smuzhiyun 		    SC(CREDIT_STATUS_CURRENT_FREE_COUNTER_MASK),
2185*4882a593Smuzhiyun 		   reg & SC(CREDIT_STATUS_LAST_RETURNED_COUNTER_SMASK));
2186*4882a593Smuzhiyun }
2187