xref: /openbmc/linux/drivers/infiniband/hw/qib/qib_verbs.c (revision 70696ea75b0b9d2cb220a09ea19d72a49f501d8e)
1f931551bSRalph Campbell /*
2e2eed58bSVinit Agnihotri  * Copyright (c) 2012, 2013 Intel Corporation.  All rights reserved.
31fb9fed6SMike Marciniszyn  * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
4f931551bSRalph Campbell  * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
5f931551bSRalph Campbell  *
6f931551bSRalph Campbell  * This software is available to you under a choice of one of two
7f931551bSRalph Campbell  * licenses.  You may choose to be licensed under the terms of the GNU
8f931551bSRalph Campbell  * General Public License (GPL) Version 2, available from the file
9f931551bSRalph Campbell  * COPYING in the main directory of this source tree, or the
10f931551bSRalph Campbell  * OpenIB.org BSD license below:
11f931551bSRalph Campbell  *
12f931551bSRalph Campbell  *     Redistribution and use in source and binary forms, with or
13f931551bSRalph Campbell  *     without modification, are permitted provided that the following
14f931551bSRalph Campbell  *     conditions are met:
15f931551bSRalph Campbell  *
16f931551bSRalph Campbell  *      - Redistributions of source code must retain the above
17f931551bSRalph Campbell  *        copyright notice, this list of conditions and the following
18f931551bSRalph Campbell  *        disclaimer.
19f931551bSRalph Campbell  *
20f931551bSRalph Campbell  *      - Redistributions in binary form must reproduce the above
21f931551bSRalph Campbell  *        copyright notice, this list of conditions and the following
22f931551bSRalph Campbell  *        disclaimer in the documentation and/or other materials
23f931551bSRalph Campbell  *        provided with the distribution.
24f931551bSRalph Campbell  *
25f931551bSRalph Campbell  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26f931551bSRalph Campbell  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27f931551bSRalph Campbell  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28f931551bSRalph Campbell  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29f931551bSRalph Campbell  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30f931551bSRalph Campbell  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31f931551bSRalph Campbell  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32f931551bSRalph Campbell  * SOFTWARE.
33f931551bSRalph Campbell  */
34f931551bSRalph Campbell 
35f931551bSRalph Campbell #include <rdma/ib_mad.h>
36f931551bSRalph Campbell #include <rdma/ib_user_verbs.h>
37f931551bSRalph Campbell #include <linux/io.h>
38e4dd23d7SPaul Gortmaker #include <linux/module.h>
39f931551bSRalph Campbell #include <linux/utsname.h>
40f931551bSRalph Campbell #include <linux/rculist.h>
41f931551bSRalph Campbell #include <linux/mm.h>
42af061a64SMike Marciniszyn #include <linux/random.h>
43d6f1c17eSMike Marciniszyn #include <linux/vmalloc.h>
44eb636ac0SDennis Dalessandro #include <rdma/rdma_vt.h>
45f931551bSRalph Campbell 
46f931551bSRalph Campbell #include "qib.h"
47f931551bSRalph Campbell #include "qib_common.h"
48f931551bSRalph Campbell 
49af061a64SMike Marciniszyn static unsigned int ib_qib_qp_table_size = 256;
50f931551bSRalph Campbell module_param_named(qp_table_size, ib_qib_qp_table_size, uint, S_IRUGO);
51f931551bSRalph Campbell MODULE_PARM_DESC(qp_table_size, "QP table size");
52f931551bSRalph Campbell 
537c2e11feSDennis Dalessandro static unsigned int qib_lkey_table_size = 16;
547c2e11feSDennis Dalessandro module_param_named(lkey_table_size, qib_lkey_table_size, uint,
55f931551bSRalph Campbell 		   S_IRUGO);
56f931551bSRalph Campbell MODULE_PARM_DESC(lkey_table_size,
57f931551bSRalph Campbell 		 "LKEY table size in bits (2^n, 1 <= n <= 23)");
58f931551bSRalph Campbell 
59f931551bSRalph Campbell static unsigned int ib_qib_max_pds = 0xFFFF;
60f931551bSRalph Campbell module_param_named(max_pds, ib_qib_max_pds, uint, S_IRUGO);
61f931551bSRalph Campbell MODULE_PARM_DESC(max_pds,
62f931551bSRalph Campbell 		 "Maximum number of protection domains to support");
63f931551bSRalph Campbell 
64f931551bSRalph Campbell static unsigned int ib_qib_max_ahs = 0xFFFF;
65f931551bSRalph Campbell module_param_named(max_ahs, ib_qib_max_ahs, uint, S_IRUGO);
66f931551bSRalph Campbell MODULE_PARM_DESC(max_ahs, "Maximum number of address handles to support");
67f931551bSRalph Campbell 
68f931551bSRalph Campbell unsigned int ib_qib_max_cqes = 0x2FFFF;
69f931551bSRalph Campbell module_param_named(max_cqes, ib_qib_max_cqes, uint, S_IRUGO);
70f931551bSRalph Campbell MODULE_PARM_DESC(max_cqes,
71f931551bSRalph Campbell 		 "Maximum number of completion queue entries to support");
72f931551bSRalph Campbell 
73f931551bSRalph Campbell unsigned int ib_qib_max_cqs = 0x1FFFF;
74f931551bSRalph Campbell module_param_named(max_cqs, ib_qib_max_cqs, uint, S_IRUGO);
75f931551bSRalph Campbell MODULE_PARM_DESC(max_cqs, "Maximum number of completion queues to support");
76f931551bSRalph Campbell 
77f931551bSRalph Campbell unsigned int ib_qib_max_qp_wrs = 0x3FFF;
78f931551bSRalph Campbell module_param_named(max_qp_wrs, ib_qib_max_qp_wrs, uint, S_IRUGO);
79f931551bSRalph Campbell MODULE_PARM_DESC(max_qp_wrs, "Maximum number of QP WRs to support");
80f931551bSRalph Campbell 
81f931551bSRalph Campbell unsigned int ib_qib_max_qps = 16384;
82f931551bSRalph Campbell module_param_named(max_qps, ib_qib_max_qps, uint, S_IRUGO);
83f931551bSRalph Campbell MODULE_PARM_DESC(max_qps, "Maximum number of QPs to support");
84f931551bSRalph Campbell 
85f931551bSRalph Campbell unsigned int ib_qib_max_sges = 0x60;
86f931551bSRalph Campbell module_param_named(max_sges, ib_qib_max_sges, uint, S_IRUGO);
87f931551bSRalph Campbell MODULE_PARM_DESC(max_sges, "Maximum number of SGEs to support");
88f931551bSRalph Campbell 
89f931551bSRalph Campbell unsigned int ib_qib_max_mcast_grps = 16384;
90f931551bSRalph Campbell module_param_named(max_mcast_grps, ib_qib_max_mcast_grps, uint, S_IRUGO);
91f931551bSRalph Campbell MODULE_PARM_DESC(max_mcast_grps,
92f931551bSRalph Campbell 		 "Maximum number of multicast groups to support");
93f931551bSRalph Campbell 
94f931551bSRalph Campbell unsigned int ib_qib_max_mcast_qp_attached = 16;
95f931551bSRalph Campbell module_param_named(max_mcast_qp_attached, ib_qib_max_mcast_qp_attached,
96f931551bSRalph Campbell 		   uint, S_IRUGO);
97f931551bSRalph Campbell MODULE_PARM_DESC(max_mcast_qp_attached,
98f931551bSRalph Campbell 		 "Maximum number of attached QPs to support");
99f931551bSRalph Campbell 
100f931551bSRalph Campbell unsigned int ib_qib_max_srqs = 1024;
101f931551bSRalph Campbell module_param_named(max_srqs, ib_qib_max_srqs, uint, S_IRUGO);
102f931551bSRalph Campbell MODULE_PARM_DESC(max_srqs, "Maximum number of SRQs to support");
103f931551bSRalph Campbell 
104f931551bSRalph Campbell unsigned int ib_qib_max_srq_sges = 128;
105f931551bSRalph Campbell module_param_named(max_srq_sges, ib_qib_max_srq_sges, uint, S_IRUGO);
106f931551bSRalph Campbell MODULE_PARM_DESC(max_srq_sges, "Maximum number of SRQ SGEs to support");
107f931551bSRalph Campbell 
108f931551bSRalph Campbell unsigned int ib_qib_max_srq_wrs = 0x1FFFF;
109f931551bSRalph Campbell module_param_named(max_srq_wrs, ib_qib_max_srq_wrs, uint, S_IRUGO);
110f931551bSRalph Campbell MODULE_PARM_DESC(max_srq_wrs, "Maximum number of SRQ WRs support");
111f931551bSRalph Campbell 
112f931551bSRalph Campbell static unsigned int ib_qib_disable_sma;
113f931551bSRalph Campbell module_param_named(disable_sma, ib_qib_disable_sma, uint, S_IWUSR | S_IRUGO);
114f931551bSRalph Campbell MODULE_PARM_DESC(disable_sma, "Disable the SMA");
115f931551bSRalph Campbell 
116f931551bSRalph Campbell /*
117f931551bSRalph Campbell  * Translate ib_wr_opcode into ib_wc_opcode.
118f931551bSRalph Campbell  */
119f931551bSRalph Campbell const enum ib_wc_opcode ib_qib_wc_opcode[] = {
120f931551bSRalph Campbell 	[IB_WR_RDMA_WRITE] = IB_WC_RDMA_WRITE,
121f931551bSRalph Campbell 	[IB_WR_RDMA_WRITE_WITH_IMM] = IB_WC_RDMA_WRITE,
122f931551bSRalph Campbell 	[IB_WR_SEND] = IB_WC_SEND,
123f931551bSRalph Campbell 	[IB_WR_SEND_WITH_IMM] = IB_WC_SEND,
124f931551bSRalph Campbell 	[IB_WR_RDMA_READ] = IB_WC_RDMA_READ,
125f931551bSRalph Campbell 	[IB_WR_ATOMIC_CMP_AND_SWP] = IB_WC_COMP_SWAP,
126f931551bSRalph Campbell 	[IB_WR_ATOMIC_FETCH_AND_ADD] = IB_WC_FETCH_ADD
127f931551bSRalph Campbell };
128f931551bSRalph Campbell 
129f931551bSRalph Campbell /*
130f931551bSRalph Campbell  * System image GUID.
131f931551bSRalph Campbell  */
132f931551bSRalph Campbell __be64 ib_qib_sys_image_guid;
133f931551bSRalph Campbell 
134f931551bSRalph Campbell /**
135f931551bSRalph Campbell  * qib_copy_sge - copy data to SGE memory
136f931551bSRalph Campbell  * @ss: the SGE state
137f931551bSRalph Campbell  * @data: the data to copy
138f931551bSRalph Campbell  * @length: the length of the data
139f931551bSRalph Campbell  */
1407c2e11feSDennis Dalessandro void qib_copy_sge(struct rvt_sge_state *ss, void *data, u32 length, int release)
141f931551bSRalph Campbell {
1427c2e11feSDennis Dalessandro 	struct rvt_sge *sge = &ss->sge;
143f931551bSRalph Campbell 
144f931551bSRalph Campbell 	while (length) {
145f931551bSRalph Campbell 		u32 len = sge->length;
146f931551bSRalph Campbell 
147f931551bSRalph Campbell 		if (len > length)
148f931551bSRalph Campbell 			len = length;
149f931551bSRalph Campbell 		if (len > sge->sge_length)
150f931551bSRalph Campbell 			len = sge->sge_length;
151f931551bSRalph Campbell 		BUG_ON(len == 0);
152f931551bSRalph Campbell 		memcpy(sge->vaddr, data, len);
153f931551bSRalph Campbell 		sge->vaddr += len;
154f931551bSRalph Campbell 		sge->length -= len;
155f931551bSRalph Campbell 		sge->sge_length -= len;
156f931551bSRalph Campbell 		if (sge->sge_length == 0) {
157f931551bSRalph Campbell 			if (release)
1587c2e11feSDennis Dalessandro 				rvt_put_mr(sge->mr);
159f931551bSRalph Campbell 			if (--ss->num_sge)
160f931551bSRalph Campbell 				*sge = *ss->sg_list++;
161f931551bSRalph Campbell 		} else if (sge->length == 0 && sge->mr->lkey) {
1627c2e11feSDennis Dalessandro 			if (++sge->n >= RVT_SEGSZ) {
163f931551bSRalph Campbell 				if (++sge->m >= sge->mr->mapsz)
164f931551bSRalph Campbell 					break;
165f931551bSRalph Campbell 				sge->n = 0;
166f931551bSRalph Campbell 			}
167f931551bSRalph Campbell 			sge->vaddr =
168f931551bSRalph Campbell 				sge->mr->map[sge->m]->segs[sge->n].vaddr;
169f931551bSRalph Campbell 			sge->length =
170f931551bSRalph Campbell 				sge->mr->map[sge->m]->segs[sge->n].length;
171f931551bSRalph Campbell 		}
172f931551bSRalph Campbell 		data += len;
173f931551bSRalph Campbell 		length -= len;
174f931551bSRalph Campbell 	}
175f931551bSRalph Campbell }
176f931551bSRalph Campbell 
177f931551bSRalph Campbell /**
178f931551bSRalph Campbell  * qib_skip_sge - skip over SGE memory - XXX almost dup of prev func
179f931551bSRalph Campbell  * @ss: the SGE state
180f931551bSRalph Campbell  * @length: the number of bytes to skip
181f931551bSRalph Campbell  */
1827c2e11feSDennis Dalessandro void qib_skip_sge(struct rvt_sge_state *ss, u32 length, int release)
183f931551bSRalph Campbell {
1847c2e11feSDennis Dalessandro 	struct rvt_sge *sge = &ss->sge;
185f931551bSRalph Campbell 
186f931551bSRalph Campbell 	while (length) {
187f931551bSRalph Campbell 		u32 len = sge->length;
188f931551bSRalph Campbell 
189f931551bSRalph Campbell 		if (len > length)
190f931551bSRalph Campbell 			len = length;
191f931551bSRalph Campbell 		if (len > sge->sge_length)
192f931551bSRalph Campbell 			len = sge->sge_length;
193f931551bSRalph Campbell 		BUG_ON(len == 0);
194f931551bSRalph Campbell 		sge->vaddr += len;
195f931551bSRalph Campbell 		sge->length -= len;
196f931551bSRalph Campbell 		sge->sge_length -= len;
197f931551bSRalph Campbell 		if (sge->sge_length == 0) {
198f931551bSRalph Campbell 			if (release)
1997c2e11feSDennis Dalessandro 				rvt_put_mr(sge->mr);
200f931551bSRalph Campbell 			if (--ss->num_sge)
201f931551bSRalph Campbell 				*sge = *ss->sg_list++;
202f931551bSRalph Campbell 		} else if (sge->length == 0 && sge->mr->lkey) {
2037c2e11feSDennis Dalessandro 			if (++sge->n >= RVT_SEGSZ) {
204f931551bSRalph Campbell 				if (++sge->m >= sge->mr->mapsz)
205f931551bSRalph Campbell 					break;
206f931551bSRalph Campbell 				sge->n = 0;
207f931551bSRalph Campbell 			}
208f931551bSRalph Campbell 			sge->vaddr =
209f931551bSRalph Campbell 				sge->mr->map[sge->m]->segs[sge->n].vaddr;
210f931551bSRalph Campbell 			sge->length =
211f931551bSRalph Campbell 				sge->mr->map[sge->m]->segs[sge->n].length;
212f931551bSRalph Campbell 		}
213f931551bSRalph Campbell 		length -= len;
214f931551bSRalph Campbell 	}
215f931551bSRalph Campbell }
216f931551bSRalph Campbell 
217f931551bSRalph Campbell /*
218f931551bSRalph Campbell  * Count the number of DMA descriptors needed to send length bytes of data.
219f931551bSRalph Campbell  * Don't modify the qib_sge_state to get the count.
220f931551bSRalph Campbell  * Return zero if any of the segments is not aligned.
221f931551bSRalph Campbell  */
2227c2e11feSDennis Dalessandro static u32 qib_count_sge(struct rvt_sge_state *ss, u32 length)
223f931551bSRalph Campbell {
2247c2e11feSDennis Dalessandro 	struct rvt_sge *sg_list = ss->sg_list;
2257c2e11feSDennis Dalessandro 	struct rvt_sge sge = ss->sge;
226f931551bSRalph Campbell 	u8 num_sge = ss->num_sge;
227f931551bSRalph Campbell 	u32 ndesc = 1;  /* count the header */
228f931551bSRalph Campbell 
229f931551bSRalph Campbell 	while (length) {
230f931551bSRalph Campbell 		u32 len = sge.length;
231f931551bSRalph Campbell 
232f931551bSRalph Campbell 		if (len > length)
233f931551bSRalph Campbell 			len = length;
234f931551bSRalph Campbell 		if (len > sge.sge_length)
235f931551bSRalph Campbell 			len = sge.sge_length;
236f931551bSRalph Campbell 		BUG_ON(len == 0);
237f931551bSRalph Campbell 		if (((long) sge.vaddr & (sizeof(u32) - 1)) ||
238f931551bSRalph Campbell 		    (len != length && (len & (sizeof(u32) - 1)))) {
239f931551bSRalph Campbell 			ndesc = 0;
240f931551bSRalph Campbell 			break;
241f931551bSRalph Campbell 		}
242f931551bSRalph Campbell 		ndesc++;
243f931551bSRalph Campbell 		sge.vaddr += len;
244f931551bSRalph Campbell 		sge.length -= len;
245f931551bSRalph Campbell 		sge.sge_length -= len;
246f931551bSRalph Campbell 		if (sge.sge_length == 0) {
247f931551bSRalph Campbell 			if (--num_sge)
248f931551bSRalph Campbell 				sge = *sg_list++;
249f931551bSRalph Campbell 		} else if (sge.length == 0 && sge.mr->lkey) {
2507c2e11feSDennis Dalessandro 			if (++sge.n >= RVT_SEGSZ) {
251f931551bSRalph Campbell 				if (++sge.m >= sge.mr->mapsz)
252f931551bSRalph Campbell 					break;
253f931551bSRalph Campbell 				sge.n = 0;
254f931551bSRalph Campbell 			}
255f931551bSRalph Campbell 			sge.vaddr =
256f931551bSRalph Campbell 				sge.mr->map[sge.m]->segs[sge.n].vaddr;
257f931551bSRalph Campbell 			sge.length =
258f931551bSRalph Campbell 				sge.mr->map[sge.m]->segs[sge.n].length;
259f931551bSRalph Campbell 		}
260f931551bSRalph Campbell 		length -= len;
261f931551bSRalph Campbell 	}
262f931551bSRalph Campbell 	return ndesc;
263f931551bSRalph Campbell }
264f931551bSRalph Campbell 
265f931551bSRalph Campbell /*
266f931551bSRalph Campbell  * Copy from the SGEs to the data buffer.
267f931551bSRalph Campbell  */
2687c2e11feSDennis Dalessandro static void qib_copy_from_sge(void *data, struct rvt_sge_state *ss, u32 length)
269f931551bSRalph Campbell {
2707c2e11feSDennis Dalessandro 	struct rvt_sge *sge = &ss->sge;
271f931551bSRalph Campbell 
272f931551bSRalph Campbell 	while (length) {
273f931551bSRalph Campbell 		u32 len = sge->length;
274f931551bSRalph Campbell 
275f931551bSRalph Campbell 		if (len > length)
276f931551bSRalph Campbell 			len = length;
277f931551bSRalph Campbell 		if (len > sge->sge_length)
278f931551bSRalph Campbell 			len = sge->sge_length;
279f931551bSRalph Campbell 		BUG_ON(len == 0);
280f931551bSRalph Campbell 		memcpy(data, sge->vaddr, len);
281f931551bSRalph Campbell 		sge->vaddr += len;
282f931551bSRalph Campbell 		sge->length -= len;
283f931551bSRalph Campbell 		sge->sge_length -= len;
284f931551bSRalph Campbell 		if (sge->sge_length == 0) {
285f931551bSRalph Campbell 			if (--ss->num_sge)
286f931551bSRalph Campbell 				*sge = *ss->sg_list++;
287f931551bSRalph Campbell 		} else if (sge->length == 0 && sge->mr->lkey) {
2887c2e11feSDennis Dalessandro 			if (++sge->n >= RVT_SEGSZ) {
289f931551bSRalph Campbell 				if (++sge->m >= sge->mr->mapsz)
290f931551bSRalph Campbell 					break;
291f931551bSRalph Campbell 				sge->n = 0;
292f931551bSRalph Campbell 			}
293f931551bSRalph Campbell 			sge->vaddr =
294f931551bSRalph Campbell 				sge->mr->map[sge->m]->segs[sge->n].vaddr;
295f931551bSRalph Campbell 			sge->length =
296f931551bSRalph Campbell 				sge->mr->map[sge->m]->segs[sge->n].length;
297f931551bSRalph Campbell 		}
298f931551bSRalph Campbell 		data += len;
299f931551bSRalph Campbell 		length -= len;
300f931551bSRalph Campbell 	}
301f931551bSRalph Campbell }
302f931551bSRalph Campbell 
303f931551bSRalph Campbell /**
304f931551bSRalph Campbell  * qib_qp_rcv - processing an incoming packet on a QP
305f931551bSRalph Campbell  * @rcd: the context pointer
306f931551bSRalph Campbell  * @hdr: the packet header
307f931551bSRalph Campbell  * @has_grh: true if the packet has a GRH
308f931551bSRalph Campbell  * @data: the packet data
309f931551bSRalph Campbell  * @tlen: the packet length
310f931551bSRalph Campbell  * @qp: the QP the packet came on
311f931551bSRalph Campbell  *
312f931551bSRalph Campbell  * This is called from qib_ib_rcv() to process an incoming packet
313f931551bSRalph Campbell  * for the given QP.
314f931551bSRalph Campbell  * Called at interrupt level.
315f931551bSRalph Campbell  */
316f931551bSRalph Campbell static void qib_qp_rcv(struct qib_ctxtdata *rcd, struct qib_ib_header *hdr,
3177c2e11feSDennis Dalessandro 		       int has_grh, void *data, u32 tlen, struct rvt_qp *qp)
318f931551bSRalph Campbell {
319f931551bSRalph Campbell 	struct qib_ibport *ibp = &rcd->ppd->ibport_data;
320f931551bSRalph Campbell 
321a5210c12SRalph Campbell 	spin_lock(&qp->r_lock);
322a5210c12SRalph Campbell 
323f931551bSRalph Campbell 	/* Check for valid receive state. */
324db3ef0ebSHarish Chegondi 	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
325f24a6d48SHarish Chegondi 		ibp->rvp.n_pkt_drops++;
326a5210c12SRalph Campbell 		goto unlock;
327f931551bSRalph Campbell 	}
328f931551bSRalph Campbell 
329f931551bSRalph Campbell 	switch (qp->ibqp.qp_type) {
330f931551bSRalph Campbell 	case IB_QPT_SMI:
331f931551bSRalph Campbell 	case IB_QPT_GSI:
332f931551bSRalph Campbell 		if (ib_qib_disable_sma)
333f931551bSRalph Campbell 			break;
334f931551bSRalph Campbell 		/* FALLTHROUGH */
335f931551bSRalph Campbell 	case IB_QPT_UD:
336f931551bSRalph Campbell 		qib_ud_rcv(ibp, hdr, has_grh, data, tlen, qp);
337f931551bSRalph Campbell 		break;
338f931551bSRalph Campbell 
339f931551bSRalph Campbell 	case IB_QPT_RC:
340f931551bSRalph Campbell 		qib_rc_rcv(rcd, hdr, has_grh, data, tlen, qp);
341f931551bSRalph Campbell 		break;
342f931551bSRalph Campbell 
343f931551bSRalph Campbell 	case IB_QPT_UC:
344f931551bSRalph Campbell 		qib_uc_rcv(ibp, hdr, has_grh, data, tlen, qp);
345f931551bSRalph Campbell 		break;
346f931551bSRalph Campbell 
347f931551bSRalph Campbell 	default:
348f931551bSRalph Campbell 		break;
349f931551bSRalph Campbell 	}
350a5210c12SRalph Campbell 
351a5210c12SRalph Campbell unlock:
352a5210c12SRalph Campbell 	spin_unlock(&qp->r_lock);
353f931551bSRalph Campbell }
354f931551bSRalph Campbell 
355f931551bSRalph Campbell /**
356f931551bSRalph Campbell  * qib_ib_rcv - process an incoming packet
357f931551bSRalph Campbell  * @rcd: the context pointer
358f931551bSRalph Campbell  * @rhdr: the header of the packet
359f931551bSRalph Campbell  * @data: the packet payload
360f931551bSRalph Campbell  * @tlen: the packet length
361f931551bSRalph Campbell  *
362f931551bSRalph Campbell  * This is called from qib_kreceive() to process an incoming packet at
363f931551bSRalph Campbell  * interrupt level. Tlen is the length of the header + data + CRC in bytes.
364f931551bSRalph Campbell  */
365f931551bSRalph Campbell void qib_ib_rcv(struct qib_ctxtdata *rcd, void *rhdr, void *data, u32 tlen)
366f931551bSRalph Campbell {
367f931551bSRalph Campbell 	struct qib_pportdata *ppd = rcd->ppd;
368f931551bSRalph Campbell 	struct qib_ibport *ibp = &ppd->ibport_data;
369f931551bSRalph Campbell 	struct qib_ib_header *hdr = rhdr;
3701cefc2cdSHarish Chegondi 	struct qib_devdata *dd = ppd->dd;
3711cefc2cdSHarish Chegondi 	struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
372f931551bSRalph Campbell 	struct qib_other_headers *ohdr;
3737c2e11feSDennis Dalessandro 	struct rvt_qp *qp;
374f931551bSRalph Campbell 	u32 qp_num;
375f931551bSRalph Campbell 	int lnh;
376f931551bSRalph Campbell 	u8 opcode;
377f931551bSRalph Campbell 	u16 lid;
378f931551bSRalph Campbell 
379f931551bSRalph Campbell 	/* 24 == LRH+BTH+CRC */
380f931551bSRalph Campbell 	if (unlikely(tlen < 24))
381f931551bSRalph Campbell 		goto drop;
382f931551bSRalph Campbell 
383f931551bSRalph Campbell 	/* Check for a valid destination LID (see ch. 7.11.1). */
384f931551bSRalph Campbell 	lid = be16_to_cpu(hdr->lrh[1]);
3859ff198f5SDennis Dalessandro 	if (lid < be16_to_cpu(IB_MULTICAST_LID_BASE)) {
386f931551bSRalph Campbell 		lid &= ~((1 << ppd->lmc) - 1);
387f931551bSRalph Campbell 		if (unlikely(lid != ppd->lid))
388f931551bSRalph Campbell 			goto drop;
389f931551bSRalph Campbell 	}
390f931551bSRalph Campbell 
391f931551bSRalph Campbell 	/* Check for GRH */
392f931551bSRalph Campbell 	lnh = be16_to_cpu(hdr->lrh[0]) & 3;
393f931551bSRalph Campbell 	if (lnh == QIB_LRH_BTH)
394f931551bSRalph Campbell 		ohdr = &hdr->u.oth;
395f931551bSRalph Campbell 	else if (lnh == QIB_LRH_GRH) {
396f931551bSRalph Campbell 		u32 vtf;
397f931551bSRalph Campbell 
398f931551bSRalph Campbell 		ohdr = &hdr->u.l.oth;
399f931551bSRalph Campbell 		if (hdr->u.l.grh.next_hdr != IB_GRH_NEXT_HDR)
400f931551bSRalph Campbell 			goto drop;
401f931551bSRalph Campbell 		vtf = be32_to_cpu(hdr->u.l.grh.version_tclass_flow);
402f931551bSRalph Campbell 		if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
403f931551bSRalph Campbell 			goto drop;
404f931551bSRalph Campbell 	} else
405f931551bSRalph Campbell 		goto drop;
406f931551bSRalph Campbell 
407ddb88765SMike Marciniszyn 	opcode = (be32_to_cpu(ohdr->bth[0]) >> 24) & 0x7f;
408ddb88765SMike Marciniszyn #ifdef CONFIG_DEBUG_FS
409ddb88765SMike Marciniszyn 	rcd->opstats->stats[opcode].n_bytes += tlen;
410ddb88765SMike Marciniszyn 	rcd->opstats->stats[opcode].n_packets++;
411ddb88765SMike Marciniszyn #endif
412f931551bSRalph Campbell 
413f931551bSRalph Campbell 	/* Get the destination QP number. */
414*70696ea7SHarish Chegondi 	qp_num = be32_to_cpu(ohdr->bth[1]) & RVT_QPN_MASK;
415f931551bSRalph Campbell 	if (qp_num == QIB_MULTICAST_QPN) {
41618f6c582SHarish Chegondi 		struct rvt_mcast *mcast;
41718f6c582SHarish Chegondi 		struct rvt_mcast_qp *p;
418f931551bSRalph Campbell 
419f931551bSRalph Campbell 		if (lnh != QIB_LRH_GRH)
420f931551bSRalph Campbell 			goto drop;
42118f6c582SHarish Chegondi 		mcast = rvt_mcast_find(&ibp->rvp, &hdr->u.l.grh.dgid);
422f931551bSRalph Campbell 		if (mcast == NULL)
423f931551bSRalph Campbell 			goto drop;
4247d7632adSMike Marciniszyn 		this_cpu_inc(ibp->pmastats->n_multicast_rcv);
425f931551bSRalph Campbell 		list_for_each_entry_rcu(p, &mcast->qp_list, list)
426f931551bSRalph Campbell 			qib_qp_rcv(rcd, hdr, 1, data, tlen, p->qp);
427f931551bSRalph Campbell 		/*
42818f6c582SHarish Chegondi 		 * Notify rvt_multicast_detach() if it is waiting for us
429f931551bSRalph Campbell 		 * to finish.
430f931551bSRalph Campbell 		 */
431f931551bSRalph Campbell 		if (atomic_dec_return(&mcast->refcount) <= 1)
432f931551bSRalph Campbell 			wake_up(&mcast->wait);
433f931551bSRalph Campbell 	} else {
4341cefc2cdSHarish Chegondi 		rcu_read_lock();
4351cefc2cdSHarish Chegondi 		qp = rvt_lookup_qpn(rdi, &ibp->rvp, qp_num);
4361cefc2cdSHarish Chegondi 		if (!qp) {
4371cefc2cdSHarish Chegondi 			rcu_read_unlock();
438f931551bSRalph Campbell 			goto drop;
4391cefc2cdSHarish Chegondi 		}
4407d7632adSMike Marciniszyn 		this_cpu_inc(ibp->pmastats->n_unicast_rcv);
441f931551bSRalph Campbell 		qib_qp_rcv(rcd, hdr, lnh == QIB_LRH_GRH, data, tlen, qp);
4421cefc2cdSHarish Chegondi 		rcu_read_unlock();
443f931551bSRalph Campbell 	}
444f931551bSRalph Campbell 	return;
445f931551bSRalph Campbell 
446f931551bSRalph Campbell drop:
447f24a6d48SHarish Chegondi 	ibp->rvp.n_pkt_drops++;
448f931551bSRalph Campbell }
449f931551bSRalph Campbell 
450f931551bSRalph Campbell /*
451f931551bSRalph Campbell  * This is called from a timer to check for QPs
452f931551bSRalph Campbell  * which need kernel memory in order to send a packet.
453f931551bSRalph Campbell  */
454f931551bSRalph Campbell static void mem_timer(unsigned long data)
455f931551bSRalph Campbell {
456f931551bSRalph Campbell 	struct qib_ibdev *dev = (struct qib_ibdev *) data;
457f931551bSRalph Campbell 	struct list_head *list = &dev->memwait;
4587c2e11feSDennis Dalessandro 	struct rvt_qp *qp = NULL;
459ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv = NULL;
460f931551bSRalph Campbell 	unsigned long flags;
461f931551bSRalph Campbell 
462cd18201fSHarish Chegondi 	spin_lock_irqsave(&dev->rdi.pending_lock, flags);
463f931551bSRalph Campbell 	if (!list_empty(list)) {
464ffc26907SDennis Dalessandro 		priv = list_entry(list->next, struct qib_qp_priv, iowait);
465ffc26907SDennis Dalessandro 		qp = priv->owner;
466ffc26907SDennis Dalessandro 		list_del_init(&priv->iowait);
467f931551bSRalph Campbell 		atomic_inc(&qp->refcount);
468f931551bSRalph Campbell 		if (!list_empty(list))
469f931551bSRalph Campbell 			mod_timer(&dev->mem_timer, jiffies + 1);
470f931551bSRalph Campbell 	}
471cd18201fSHarish Chegondi 	spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
472f931551bSRalph Campbell 
473f931551bSRalph Campbell 	if (qp) {
474f931551bSRalph Campbell 		spin_lock_irqsave(&qp->s_lock, flags);
47501ba79d4SHarish Chegondi 		if (qp->s_flags & RVT_S_WAIT_KMEM) {
47601ba79d4SHarish Chegondi 			qp->s_flags &= ~RVT_S_WAIT_KMEM;
477f931551bSRalph Campbell 			qib_schedule_send(qp);
478f931551bSRalph Campbell 		}
479f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
480f931551bSRalph Campbell 		if (atomic_dec_and_test(&qp->refcount))
481f931551bSRalph Campbell 			wake_up(&qp->wait);
482f931551bSRalph Campbell 	}
483f931551bSRalph Campbell }
484f931551bSRalph Campbell 
4857c2e11feSDennis Dalessandro static void update_sge(struct rvt_sge_state *ss, u32 length)
486f931551bSRalph Campbell {
4877c2e11feSDennis Dalessandro 	struct rvt_sge *sge = &ss->sge;
488f931551bSRalph Campbell 
489f931551bSRalph Campbell 	sge->vaddr += length;
490f931551bSRalph Campbell 	sge->length -= length;
491f931551bSRalph Campbell 	sge->sge_length -= length;
492f931551bSRalph Campbell 	if (sge->sge_length == 0) {
493f931551bSRalph Campbell 		if (--ss->num_sge)
494f931551bSRalph Campbell 			*sge = *ss->sg_list++;
495f931551bSRalph Campbell 	} else if (sge->length == 0 && sge->mr->lkey) {
4967c2e11feSDennis Dalessandro 		if (++sge->n >= RVT_SEGSZ) {
497f931551bSRalph Campbell 			if (++sge->m >= sge->mr->mapsz)
498f931551bSRalph Campbell 				return;
499f931551bSRalph Campbell 			sge->n = 0;
500f931551bSRalph Campbell 		}
501f931551bSRalph Campbell 		sge->vaddr = sge->mr->map[sge->m]->segs[sge->n].vaddr;
502f931551bSRalph Campbell 		sge->length = sge->mr->map[sge->m]->segs[sge->n].length;
503f931551bSRalph Campbell 	}
504f931551bSRalph Campbell }
505f931551bSRalph Campbell 
506f931551bSRalph Campbell #ifdef __LITTLE_ENDIAN
507f931551bSRalph Campbell static inline u32 get_upper_bits(u32 data, u32 shift)
508f931551bSRalph Campbell {
509f931551bSRalph Campbell 	return data >> shift;
510f931551bSRalph Campbell }
511f931551bSRalph Campbell 
512f931551bSRalph Campbell static inline u32 set_upper_bits(u32 data, u32 shift)
513f931551bSRalph Campbell {
514f931551bSRalph Campbell 	return data << shift;
515f931551bSRalph Campbell }
516f931551bSRalph Campbell 
517f931551bSRalph Campbell static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
518f931551bSRalph Campbell {
519f931551bSRalph Campbell 	data <<= ((sizeof(u32) - n) * BITS_PER_BYTE);
520f931551bSRalph Campbell 	data >>= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
521f931551bSRalph Campbell 	return data;
522f931551bSRalph Campbell }
523f931551bSRalph Campbell #else
524f931551bSRalph Campbell static inline u32 get_upper_bits(u32 data, u32 shift)
525f931551bSRalph Campbell {
526f931551bSRalph Campbell 	return data << shift;
527f931551bSRalph Campbell }
528f931551bSRalph Campbell 
529f931551bSRalph Campbell static inline u32 set_upper_bits(u32 data, u32 shift)
530f931551bSRalph Campbell {
531f931551bSRalph Campbell 	return data >> shift;
532f931551bSRalph Campbell }
533f931551bSRalph Campbell 
534f931551bSRalph Campbell static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
535f931551bSRalph Campbell {
536f931551bSRalph Campbell 	data >>= ((sizeof(u32) - n) * BITS_PER_BYTE);
537f931551bSRalph Campbell 	data <<= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
538f931551bSRalph Campbell 	return data;
539f931551bSRalph Campbell }
540f931551bSRalph Campbell #endif
541f931551bSRalph Campbell 
5427c2e11feSDennis Dalessandro static void copy_io(u32 __iomem *piobuf, struct rvt_sge_state *ss,
543f931551bSRalph Campbell 		    u32 length, unsigned flush_wc)
544f931551bSRalph Campbell {
545f931551bSRalph Campbell 	u32 extra = 0;
546f931551bSRalph Campbell 	u32 data = 0;
547f931551bSRalph Campbell 	u32 last;
548f931551bSRalph Campbell 
549f931551bSRalph Campbell 	while (1) {
550f931551bSRalph Campbell 		u32 len = ss->sge.length;
551f931551bSRalph Campbell 		u32 off;
552f931551bSRalph Campbell 
553f931551bSRalph Campbell 		if (len > length)
554f931551bSRalph Campbell 			len = length;
555f931551bSRalph Campbell 		if (len > ss->sge.sge_length)
556f931551bSRalph Campbell 			len = ss->sge.sge_length;
557f931551bSRalph Campbell 		BUG_ON(len == 0);
558f931551bSRalph Campbell 		/* If the source address is not aligned, try to align it. */
559f931551bSRalph Campbell 		off = (unsigned long)ss->sge.vaddr & (sizeof(u32) - 1);
560f931551bSRalph Campbell 		if (off) {
561f931551bSRalph Campbell 			u32 *addr = (u32 *)((unsigned long)ss->sge.vaddr &
562f931551bSRalph Campbell 					    ~(sizeof(u32) - 1));
563f931551bSRalph Campbell 			u32 v = get_upper_bits(*addr, off * BITS_PER_BYTE);
564f931551bSRalph Campbell 			u32 y;
565f931551bSRalph Campbell 
566f931551bSRalph Campbell 			y = sizeof(u32) - off;
567f931551bSRalph Campbell 			if (len > y)
568f931551bSRalph Campbell 				len = y;
569f931551bSRalph Campbell 			if (len + extra >= sizeof(u32)) {
570f931551bSRalph Campbell 				data |= set_upper_bits(v, extra *
571f931551bSRalph Campbell 						       BITS_PER_BYTE);
572f931551bSRalph Campbell 				len = sizeof(u32) - extra;
573f931551bSRalph Campbell 				if (len == length) {
574f931551bSRalph Campbell 					last = data;
575f931551bSRalph Campbell 					break;
576f931551bSRalph Campbell 				}
577f931551bSRalph Campbell 				__raw_writel(data, piobuf);
578f931551bSRalph Campbell 				piobuf++;
579f931551bSRalph Campbell 				extra = 0;
580f931551bSRalph Campbell 				data = 0;
581f931551bSRalph Campbell 			} else {
582f931551bSRalph Campbell 				/* Clear unused upper bytes */
583f931551bSRalph Campbell 				data |= clear_upper_bytes(v, len, extra);
584f931551bSRalph Campbell 				if (len == length) {
585f931551bSRalph Campbell 					last = data;
586f931551bSRalph Campbell 					break;
587f931551bSRalph Campbell 				}
588f931551bSRalph Campbell 				extra += len;
589f931551bSRalph Campbell 			}
590f931551bSRalph Campbell 		} else if (extra) {
591f931551bSRalph Campbell 			/* Source address is aligned. */
592f931551bSRalph Campbell 			u32 *addr = (u32 *) ss->sge.vaddr;
593f931551bSRalph Campbell 			int shift = extra * BITS_PER_BYTE;
594f931551bSRalph Campbell 			int ushift = 32 - shift;
595f931551bSRalph Campbell 			u32 l = len;
596f931551bSRalph Campbell 
597f931551bSRalph Campbell 			while (l >= sizeof(u32)) {
598f931551bSRalph Campbell 				u32 v = *addr;
599f931551bSRalph Campbell 
600f931551bSRalph Campbell 				data |= set_upper_bits(v, shift);
601f931551bSRalph Campbell 				__raw_writel(data, piobuf);
602f931551bSRalph Campbell 				data = get_upper_bits(v, ushift);
603f931551bSRalph Campbell 				piobuf++;
604f931551bSRalph Campbell 				addr++;
605f931551bSRalph Campbell 				l -= sizeof(u32);
606f931551bSRalph Campbell 			}
607f931551bSRalph Campbell 			/*
608f931551bSRalph Campbell 			 * We still have 'extra' number of bytes leftover.
609f931551bSRalph Campbell 			 */
610f931551bSRalph Campbell 			if (l) {
611f931551bSRalph Campbell 				u32 v = *addr;
612f931551bSRalph Campbell 
613f931551bSRalph Campbell 				if (l + extra >= sizeof(u32)) {
614f931551bSRalph Campbell 					data |= set_upper_bits(v, shift);
615f931551bSRalph Campbell 					len -= l + extra - sizeof(u32);
616f931551bSRalph Campbell 					if (len == length) {
617f931551bSRalph Campbell 						last = data;
618f931551bSRalph Campbell 						break;
619f931551bSRalph Campbell 					}
620f931551bSRalph Campbell 					__raw_writel(data, piobuf);
621f931551bSRalph Campbell 					piobuf++;
622f931551bSRalph Campbell 					extra = 0;
623f931551bSRalph Campbell 					data = 0;
624f931551bSRalph Campbell 				} else {
625f931551bSRalph Campbell 					/* Clear unused upper bytes */
626f931551bSRalph Campbell 					data |= clear_upper_bytes(v, l, extra);
627f931551bSRalph Campbell 					if (len == length) {
628f931551bSRalph Campbell 						last = data;
629f931551bSRalph Campbell 						break;
630f931551bSRalph Campbell 					}
631f931551bSRalph Campbell 					extra += l;
632f931551bSRalph Campbell 				}
633f931551bSRalph Campbell 			} else if (len == length) {
634f931551bSRalph Campbell 				last = data;
635f931551bSRalph Campbell 				break;
636f931551bSRalph Campbell 			}
637f931551bSRalph Campbell 		} else if (len == length) {
638f931551bSRalph Campbell 			u32 w;
639f931551bSRalph Campbell 
640f931551bSRalph Campbell 			/*
641f931551bSRalph Campbell 			 * Need to round up for the last dword in the
642f931551bSRalph Campbell 			 * packet.
643f931551bSRalph Campbell 			 */
644f931551bSRalph Campbell 			w = (len + 3) >> 2;
645f931551bSRalph Campbell 			qib_pio_copy(piobuf, ss->sge.vaddr, w - 1);
646f931551bSRalph Campbell 			piobuf += w - 1;
647f931551bSRalph Campbell 			last = ((u32 *) ss->sge.vaddr)[w - 1];
648f931551bSRalph Campbell 			break;
649f931551bSRalph Campbell 		} else {
650f931551bSRalph Campbell 			u32 w = len >> 2;
651f931551bSRalph Campbell 
652f931551bSRalph Campbell 			qib_pio_copy(piobuf, ss->sge.vaddr, w);
653f931551bSRalph Campbell 			piobuf += w;
654f931551bSRalph Campbell 
655f931551bSRalph Campbell 			extra = len & (sizeof(u32) - 1);
656f931551bSRalph Campbell 			if (extra) {
657f931551bSRalph Campbell 				u32 v = ((u32 *) ss->sge.vaddr)[w];
658f931551bSRalph Campbell 
659f931551bSRalph Campbell 				/* Clear unused upper bytes */
660f931551bSRalph Campbell 				data = clear_upper_bytes(v, extra, 0);
661f931551bSRalph Campbell 			}
662f931551bSRalph Campbell 		}
663f931551bSRalph Campbell 		update_sge(ss, len);
664f931551bSRalph Campbell 		length -= len;
665f931551bSRalph Campbell 	}
666f931551bSRalph Campbell 	/* Update address before sending packet. */
667f931551bSRalph Campbell 	update_sge(ss, length);
668f931551bSRalph Campbell 	if (flush_wc) {
669f931551bSRalph Campbell 		/* must flush early everything before trigger word */
670f931551bSRalph Campbell 		qib_flush_wc();
671f931551bSRalph Campbell 		__raw_writel(last, piobuf);
672f931551bSRalph Campbell 		/* be sure trigger word is written */
673f931551bSRalph Campbell 		qib_flush_wc();
674f931551bSRalph Campbell 	} else
675f931551bSRalph Campbell 		__raw_writel(last, piobuf);
676f931551bSRalph Campbell }
677f931551bSRalph Campbell 
67848947109SMike Marciniszyn static noinline struct qib_verbs_txreq *__get_txreq(struct qib_ibdev *dev,
6797c2e11feSDennis Dalessandro 					   struct rvt_qp *qp)
680f931551bSRalph Campbell {
681ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv = qp->priv;
682f931551bSRalph Campbell 	struct qib_verbs_txreq *tx;
683f931551bSRalph Campbell 	unsigned long flags;
684f931551bSRalph Campbell 
685f931551bSRalph Campbell 	spin_lock_irqsave(&qp->s_lock, flags);
686cd18201fSHarish Chegondi 	spin_lock(&dev->rdi.pending_lock);
687f931551bSRalph Campbell 
688f931551bSRalph Campbell 	if (!list_empty(&dev->txreq_free)) {
689f931551bSRalph Campbell 		struct list_head *l = dev->txreq_free.next;
690f931551bSRalph Campbell 
691f931551bSRalph Campbell 		list_del(l);
692cd18201fSHarish Chegondi 		spin_unlock(&dev->rdi.pending_lock);
69348947109SMike Marciniszyn 		spin_unlock_irqrestore(&qp->s_lock, flags);
694f931551bSRalph Campbell 		tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
695f931551bSRalph Campbell 	} else {
696db3ef0ebSHarish Chegondi 		if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK &&
697ffc26907SDennis Dalessandro 		    list_empty(&priv->iowait)) {
698f931551bSRalph Campbell 			dev->n_txwait++;
69901ba79d4SHarish Chegondi 			qp->s_flags |= RVT_S_WAIT_TX;
700ffc26907SDennis Dalessandro 			list_add_tail(&priv->iowait, &dev->txwait);
701f931551bSRalph Campbell 		}
70201ba79d4SHarish Chegondi 		qp->s_flags &= ~RVT_S_BUSY;
703cd18201fSHarish Chegondi 		spin_unlock(&dev->rdi.pending_lock);
704f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
70548947109SMike Marciniszyn 		tx = ERR_PTR(-EBUSY);
70648947109SMike Marciniszyn 	}
70748947109SMike Marciniszyn 	return tx;
70848947109SMike Marciniszyn }
709f931551bSRalph Campbell 
71048947109SMike Marciniszyn static inline struct qib_verbs_txreq *get_txreq(struct qib_ibdev *dev,
7117c2e11feSDennis Dalessandro 					 struct rvt_qp *qp)
71248947109SMike Marciniszyn {
71348947109SMike Marciniszyn 	struct qib_verbs_txreq *tx;
71448947109SMike Marciniszyn 	unsigned long flags;
71548947109SMike Marciniszyn 
716cd18201fSHarish Chegondi 	spin_lock_irqsave(&dev->rdi.pending_lock, flags);
71748947109SMike Marciniszyn 	/* assume the list non empty */
71848947109SMike Marciniszyn 	if (likely(!list_empty(&dev->txreq_free))) {
71948947109SMike Marciniszyn 		struct list_head *l = dev->txreq_free.next;
72048947109SMike Marciniszyn 
72148947109SMike Marciniszyn 		list_del(l);
722cd18201fSHarish Chegondi 		spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
72348947109SMike Marciniszyn 		tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
72448947109SMike Marciniszyn 	} else {
72548947109SMike Marciniszyn 		/* call slow path to get the extra lock */
726cd18201fSHarish Chegondi 		spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
72748947109SMike Marciniszyn 		tx =  __get_txreq(dev, qp);
72848947109SMike Marciniszyn 	}
729f931551bSRalph Campbell 	return tx;
730f931551bSRalph Campbell }
731f931551bSRalph Campbell 
732f931551bSRalph Campbell void qib_put_txreq(struct qib_verbs_txreq *tx)
733f931551bSRalph Campbell {
734f931551bSRalph Campbell 	struct qib_ibdev *dev;
7357c2e11feSDennis Dalessandro 	struct rvt_qp *qp;
736ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv;
737f931551bSRalph Campbell 	unsigned long flags;
738f931551bSRalph Campbell 
739f931551bSRalph Campbell 	qp = tx->qp;
740f931551bSRalph Campbell 	dev = to_idev(qp->ibqp.device);
741f931551bSRalph Campbell 
742f931551bSRalph Campbell 	if (tx->mr) {
7437c2e11feSDennis Dalessandro 		rvt_put_mr(tx->mr);
744f931551bSRalph Campbell 		tx->mr = NULL;
745f931551bSRalph Campbell 	}
746f931551bSRalph Campbell 	if (tx->txreq.flags & QIB_SDMA_TXREQ_F_FREEBUF) {
747f931551bSRalph Campbell 		tx->txreq.flags &= ~QIB_SDMA_TXREQ_F_FREEBUF;
748f931551bSRalph Campbell 		dma_unmap_single(&dd_from_dev(dev)->pcidev->dev,
749f931551bSRalph Campbell 				 tx->txreq.addr, tx->hdr_dwords << 2,
750f931551bSRalph Campbell 				 DMA_TO_DEVICE);
751f931551bSRalph Campbell 		kfree(tx->align_buf);
752f931551bSRalph Campbell 	}
753f931551bSRalph Campbell 
754cd18201fSHarish Chegondi 	spin_lock_irqsave(&dev->rdi.pending_lock, flags);
755f931551bSRalph Campbell 
756f931551bSRalph Campbell 	/* Put struct back on free list */
757f931551bSRalph Campbell 	list_add(&tx->txreq.list, &dev->txreq_free);
758f931551bSRalph Campbell 
759f931551bSRalph Campbell 	if (!list_empty(&dev->txwait)) {
760f931551bSRalph Campbell 		/* Wake up first QP wanting a free struct */
761ffc26907SDennis Dalessandro 		priv = list_entry(dev->txwait.next, struct qib_qp_priv,
762ffc26907SDennis Dalessandro 				  iowait);
763ffc26907SDennis Dalessandro 		qp = priv->owner;
764ffc26907SDennis Dalessandro 		list_del_init(&priv->iowait);
765f931551bSRalph Campbell 		atomic_inc(&qp->refcount);
766cd18201fSHarish Chegondi 		spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
767f931551bSRalph Campbell 
768f931551bSRalph Campbell 		spin_lock_irqsave(&qp->s_lock, flags);
76901ba79d4SHarish Chegondi 		if (qp->s_flags & RVT_S_WAIT_TX) {
77001ba79d4SHarish Chegondi 			qp->s_flags &= ~RVT_S_WAIT_TX;
771f931551bSRalph Campbell 			qib_schedule_send(qp);
772f931551bSRalph Campbell 		}
773f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
774f931551bSRalph Campbell 
775f931551bSRalph Campbell 		if (atomic_dec_and_test(&qp->refcount))
776f931551bSRalph Campbell 			wake_up(&qp->wait);
777f931551bSRalph Campbell 	} else
778cd18201fSHarish Chegondi 		spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
779f931551bSRalph Campbell }
780f931551bSRalph Campbell 
781f931551bSRalph Campbell /*
782f931551bSRalph Campbell  * This is called when there are send DMA descriptors that might be
783f931551bSRalph Campbell  * available.
784f931551bSRalph Campbell  *
785f931551bSRalph Campbell  * This is called with ppd->sdma_lock held.
786f931551bSRalph Campbell  */
787f931551bSRalph Campbell void qib_verbs_sdma_desc_avail(struct qib_pportdata *ppd, unsigned avail)
788f931551bSRalph Campbell {
7897c2e11feSDennis Dalessandro 	struct rvt_qp *qp, *nqp;
790ffc26907SDennis Dalessandro 	struct qib_qp_priv *qpp, *nqpp;
7917c2e11feSDennis Dalessandro 	struct rvt_qp *qps[20];
792f931551bSRalph Campbell 	struct qib_ibdev *dev;
793f931551bSRalph Campbell 	unsigned i, n;
794f931551bSRalph Campbell 
795f931551bSRalph Campbell 	n = 0;
796f931551bSRalph Campbell 	dev = &ppd->dd->verbs_dev;
797cd18201fSHarish Chegondi 	spin_lock(&dev->rdi.pending_lock);
798f931551bSRalph Campbell 
799f931551bSRalph Campbell 	/* Search wait list for first QP wanting DMA descriptors. */
800ffc26907SDennis Dalessandro 	list_for_each_entry_safe(qpp, nqpp, &dev->dmawait, iowait) {
801ffc26907SDennis Dalessandro 		qp = qpp->owner;
802ffc26907SDennis Dalessandro 		nqp = nqpp->owner;
803f931551bSRalph Campbell 		if (qp->port_num != ppd->port)
804f931551bSRalph Campbell 			continue;
805f931551bSRalph Campbell 		if (n == ARRAY_SIZE(qps))
806f931551bSRalph Campbell 			break;
807ffc26907SDennis Dalessandro 		if (qpp->s_tx->txreq.sg_count > avail)
808f931551bSRalph Campbell 			break;
809ffc26907SDennis Dalessandro 		avail -= qpp->s_tx->txreq.sg_count;
810ffc26907SDennis Dalessandro 		list_del_init(&qpp->iowait);
811f931551bSRalph Campbell 		atomic_inc(&qp->refcount);
812f931551bSRalph Campbell 		qps[n++] = qp;
813f931551bSRalph Campbell 	}
814f931551bSRalph Campbell 
815cd18201fSHarish Chegondi 	spin_unlock(&dev->rdi.pending_lock);
816f931551bSRalph Campbell 
817f931551bSRalph Campbell 	for (i = 0; i < n; i++) {
818f931551bSRalph Campbell 		qp = qps[i];
819f931551bSRalph Campbell 		spin_lock(&qp->s_lock);
82001ba79d4SHarish Chegondi 		if (qp->s_flags & RVT_S_WAIT_DMA_DESC) {
82101ba79d4SHarish Chegondi 			qp->s_flags &= ~RVT_S_WAIT_DMA_DESC;
822f931551bSRalph Campbell 			qib_schedule_send(qp);
823f931551bSRalph Campbell 		}
824f931551bSRalph Campbell 		spin_unlock(&qp->s_lock);
825f931551bSRalph Campbell 		if (atomic_dec_and_test(&qp->refcount))
826f931551bSRalph Campbell 			wake_up(&qp->wait);
827f931551bSRalph Campbell 	}
828f931551bSRalph Campbell }
829f931551bSRalph Campbell 
830f931551bSRalph Campbell /*
831f931551bSRalph Campbell  * This is called with ppd->sdma_lock held.
832f931551bSRalph Campbell  */
833f931551bSRalph Campbell static void sdma_complete(struct qib_sdma_txreq *cookie, int status)
834f931551bSRalph Campbell {
835f931551bSRalph Campbell 	struct qib_verbs_txreq *tx =
836f931551bSRalph Campbell 		container_of(cookie, struct qib_verbs_txreq, txreq);
8377c2e11feSDennis Dalessandro 	struct rvt_qp *qp = tx->qp;
838ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv = qp->priv;
839f931551bSRalph Campbell 
840f931551bSRalph Campbell 	spin_lock(&qp->s_lock);
841f931551bSRalph Campbell 	if (tx->wqe)
842f931551bSRalph Campbell 		qib_send_complete(qp, tx->wqe, IB_WC_SUCCESS);
843f931551bSRalph Campbell 	else if (qp->ibqp.qp_type == IB_QPT_RC) {
844f931551bSRalph Campbell 		struct qib_ib_header *hdr;
845f931551bSRalph Campbell 
846f931551bSRalph Campbell 		if (tx->txreq.flags & QIB_SDMA_TXREQ_F_FREEBUF)
847f931551bSRalph Campbell 			hdr = &tx->align_buf->hdr;
848f931551bSRalph Campbell 		else {
849f931551bSRalph Campbell 			struct qib_ibdev *dev = to_idev(qp->ibqp.device);
850f931551bSRalph Campbell 
851f931551bSRalph Campbell 			hdr = &dev->pio_hdrs[tx->hdr_inx].hdr;
852f931551bSRalph Campbell 		}
853f931551bSRalph Campbell 		qib_rc_send_complete(qp, hdr);
854f931551bSRalph Campbell 	}
855ffc26907SDennis Dalessandro 	if (atomic_dec_and_test(&priv->s_dma_busy)) {
856f931551bSRalph Campbell 		if (qp->state == IB_QPS_RESET)
857ffc26907SDennis Dalessandro 			wake_up(&priv->wait_dma);
85801ba79d4SHarish Chegondi 		else if (qp->s_flags & RVT_S_WAIT_DMA) {
85901ba79d4SHarish Chegondi 			qp->s_flags &= ~RVT_S_WAIT_DMA;
860f931551bSRalph Campbell 			qib_schedule_send(qp);
861f931551bSRalph Campbell 		}
862f931551bSRalph Campbell 	}
863f931551bSRalph Campbell 	spin_unlock(&qp->s_lock);
864f931551bSRalph Campbell 
865f931551bSRalph Campbell 	qib_put_txreq(tx);
866f931551bSRalph Campbell }
867f931551bSRalph Campbell 
8687c2e11feSDennis Dalessandro static int wait_kmem(struct qib_ibdev *dev, struct rvt_qp *qp)
869f931551bSRalph Campbell {
870ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv = qp->priv;
871f931551bSRalph Campbell 	unsigned long flags;
872f931551bSRalph Campbell 	int ret = 0;
873f931551bSRalph Campbell 
874f931551bSRalph Campbell 	spin_lock_irqsave(&qp->s_lock, flags);
875db3ef0ebSHarish Chegondi 	if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
876cd18201fSHarish Chegondi 		spin_lock(&dev->rdi.pending_lock);
877ffc26907SDennis Dalessandro 		if (list_empty(&priv->iowait)) {
878f931551bSRalph Campbell 			if (list_empty(&dev->memwait))
879f931551bSRalph Campbell 				mod_timer(&dev->mem_timer, jiffies + 1);
88001ba79d4SHarish Chegondi 			qp->s_flags |= RVT_S_WAIT_KMEM;
881ffc26907SDennis Dalessandro 			list_add_tail(&priv->iowait, &dev->memwait);
882f931551bSRalph Campbell 		}
883cd18201fSHarish Chegondi 		spin_unlock(&dev->rdi.pending_lock);
88401ba79d4SHarish Chegondi 		qp->s_flags &= ~RVT_S_BUSY;
885f931551bSRalph Campbell 		ret = -EBUSY;
886f931551bSRalph Campbell 	}
887f931551bSRalph Campbell 	spin_unlock_irqrestore(&qp->s_lock, flags);
888f931551bSRalph Campbell 
889f931551bSRalph Campbell 	return ret;
890f931551bSRalph Campbell }
891f931551bSRalph Campbell 
8927c2e11feSDennis Dalessandro static int qib_verbs_send_dma(struct rvt_qp *qp, struct qib_ib_header *hdr,
8937c2e11feSDennis Dalessandro 			      u32 hdrwords, struct rvt_sge_state *ss, u32 len,
894f931551bSRalph Campbell 			      u32 plen, u32 dwords)
895f931551bSRalph Campbell {
896ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv = qp->priv;
897f931551bSRalph Campbell 	struct qib_ibdev *dev = to_idev(qp->ibqp.device);
898f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_dev(dev);
899f931551bSRalph Campbell 	struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
900f931551bSRalph Campbell 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
901f931551bSRalph Campbell 	struct qib_verbs_txreq *tx;
902f931551bSRalph Campbell 	struct qib_pio_header *phdr;
903f931551bSRalph Campbell 	u32 control;
904f931551bSRalph Campbell 	u32 ndesc;
905f931551bSRalph Campbell 	int ret;
906f931551bSRalph Campbell 
907ffc26907SDennis Dalessandro 	tx = priv->s_tx;
908f931551bSRalph Campbell 	if (tx) {
909ffc26907SDennis Dalessandro 		priv->s_tx = NULL;
910f931551bSRalph Campbell 		/* resend previously constructed packet */
911f931551bSRalph Campbell 		ret = qib_sdma_verbs_send(ppd, tx->ss, tx->dwords, tx);
912f931551bSRalph Campbell 		goto bail;
913f931551bSRalph Campbell 	}
914f931551bSRalph Campbell 
91548947109SMike Marciniszyn 	tx = get_txreq(dev, qp);
91648947109SMike Marciniszyn 	if (IS_ERR(tx))
91748947109SMike Marciniszyn 		goto bail_tx;
918f931551bSRalph Campbell 
919f931551bSRalph Campbell 	control = dd->f_setpbc_control(ppd, plen, qp->s_srate,
920f931551bSRalph Campbell 				       be16_to_cpu(hdr->lrh[0]) >> 12);
921f931551bSRalph Campbell 	tx->qp = qp;
922f931551bSRalph Campbell 	tx->wqe = qp->s_wqe;
923f931551bSRalph Campbell 	tx->mr = qp->s_rdma_mr;
924f931551bSRalph Campbell 	if (qp->s_rdma_mr)
925f931551bSRalph Campbell 		qp->s_rdma_mr = NULL;
926f931551bSRalph Campbell 	tx->txreq.callback = sdma_complete;
927f931551bSRalph Campbell 	if (dd->flags & QIB_HAS_SDMA_TIMEOUT)
928f931551bSRalph Campbell 		tx->txreq.flags = QIB_SDMA_TXREQ_F_HEADTOHOST;
929f931551bSRalph Campbell 	else
930f931551bSRalph Campbell 		tx->txreq.flags = QIB_SDMA_TXREQ_F_INTREQ;
931f931551bSRalph Campbell 	if (plen + 1 > dd->piosize2kmax_dwords)
932f931551bSRalph Campbell 		tx->txreq.flags |= QIB_SDMA_TXREQ_F_USELARGEBUF;
933f931551bSRalph Campbell 
934f931551bSRalph Campbell 	if (len) {
935f931551bSRalph Campbell 		/*
936f931551bSRalph Campbell 		 * Don't try to DMA if it takes more descriptors than
937f931551bSRalph Campbell 		 * the queue holds.
938f931551bSRalph Campbell 		 */
939f931551bSRalph Campbell 		ndesc = qib_count_sge(ss, len);
940f931551bSRalph Campbell 		if (ndesc >= ppd->sdma_descq_cnt)
941f931551bSRalph Campbell 			ndesc = 0;
942f931551bSRalph Campbell 	} else
943f931551bSRalph Campbell 		ndesc = 1;
944f931551bSRalph Campbell 	if (ndesc) {
945f931551bSRalph Campbell 		phdr = &dev->pio_hdrs[tx->hdr_inx];
946f931551bSRalph Campbell 		phdr->pbc[0] = cpu_to_le32(plen);
947f931551bSRalph Campbell 		phdr->pbc[1] = cpu_to_le32(control);
948f931551bSRalph Campbell 		memcpy(&phdr->hdr, hdr, hdrwords << 2);
949f931551bSRalph Campbell 		tx->txreq.flags |= QIB_SDMA_TXREQ_F_FREEDESC;
950f931551bSRalph Campbell 		tx->txreq.sg_count = ndesc;
951f931551bSRalph Campbell 		tx->txreq.addr = dev->pio_hdrs_phys +
952f931551bSRalph Campbell 			tx->hdr_inx * sizeof(struct qib_pio_header);
953f931551bSRalph Campbell 		tx->hdr_dwords = hdrwords + 2; /* add PBC length */
954f931551bSRalph Campbell 		ret = qib_sdma_verbs_send(ppd, ss, dwords, tx);
955f931551bSRalph Campbell 		goto bail;
956f931551bSRalph Campbell 	}
957f931551bSRalph Campbell 
958f931551bSRalph Campbell 	/* Allocate a buffer and copy the header and payload to it. */
959f931551bSRalph Campbell 	tx->hdr_dwords = plen + 1;
960f931551bSRalph Campbell 	phdr = kmalloc(tx->hdr_dwords << 2, GFP_ATOMIC);
961f931551bSRalph Campbell 	if (!phdr)
962f931551bSRalph Campbell 		goto err_tx;
963f931551bSRalph Campbell 	phdr->pbc[0] = cpu_to_le32(plen);
964f931551bSRalph Campbell 	phdr->pbc[1] = cpu_to_le32(control);
965f931551bSRalph Campbell 	memcpy(&phdr->hdr, hdr, hdrwords << 2);
966f931551bSRalph Campbell 	qib_copy_from_sge((u32 *) &phdr->hdr + hdrwords, ss, len);
967f931551bSRalph Campbell 
968f931551bSRalph Campbell 	tx->txreq.addr = dma_map_single(&dd->pcidev->dev, phdr,
969f931551bSRalph Campbell 					tx->hdr_dwords << 2, DMA_TO_DEVICE);
970f931551bSRalph Campbell 	if (dma_mapping_error(&dd->pcidev->dev, tx->txreq.addr))
971f931551bSRalph Campbell 		goto map_err;
972f931551bSRalph Campbell 	tx->align_buf = phdr;
973f931551bSRalph Campbell 	tx->txreq.flags |= QIB_SDMA_TXREQ_F_FREEBUF;
974f931551bSRalph Campbell 	tx->txreq.sg_count = 1;
975f931551bSRalph Campbell 	ret = qib_sdma_verbs_send(ppd, NULL, 0, tx);
976f931551bSRalph Campbell 	goto unaligned;
977f931551bSRalph Campbell 
978f931551bSRalph Campbell map_err:
979f931551bSRalph Campbell 	kfree(phdr);
980f931551bSRalph Campbell err_tx:
981f931551bSRalph Campbell 	qib_put_txreq(tx);
982f931551bSRalph Campbell 	ret = wait_kmem(dev, qp);
983f931551bSRalph Campbell unaligned:
984f24a6d48SHarish Chegondi 	ibp->rvp.n_unaligned++;
985f931551bSRalph Campbell bail:
986f931551bSRalph Campbell 	return ret;
98748947109SMike Marciniszyn bail_tx:
98848947109SMike Marciniszyn 	ret = PTR_ERR(tx);
98948947109SMike Marciniszyn 	goto bail;
990f931551bSRalph Campbell }
991f931551bSRalph Campbell 
992f931551bSRalph Campbell /*
993f931551bSRalph Campbell  * If we are now in the error state, return zero to flush the
994f931551bSRalph Campbell  * send work request.
995f931551bSRalph Campbell  */
9967c2e11feSDennis Dalessandro static int no_bufs_available(struct rvt_qp *qp)
997f931551bSRalph Campbell {
998ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv = qp->priv;
999f931551bSRalph Campbell 	struct qib_ibdev *dev = to_idev(qp->ibqp.device);
1000f931551bSRalph Campbell 	struct qib_devdata *dd;
1001f931551bSRalph Campbell 	unsigned long flags;
1002f931551bSRalph Campbell 	int ret = 0;
1003f931551bSRalph Campbell 
1004f931551bSRalph Campbell 	/*
1005f931551bSRalph Campbell 	 * Note that as soon as want_buffer() is called and
1006f931551bSRalph Campbell 	 * possibly before it returns, qib_ib_piobufavail()
1007f931551bSRalph Campbell 	 * could be called. Therefore, put QP on the I/O wait list before
1008f931551bSRalph Campbell 	 * enabling the PIO avail interrupt.
1009f931551bSRalph Campbell 	 */
1010f931551bSRalph Campbell 	spin_lock_irqsave(&qp->s_lock, flags);
1011db3ef0ebSHarish Chegondi 	if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
1012cd18201fSHarish Chegondi 		spin_lock(&dev->rdi.pending_lock);
1013ffc26907SDennis Dalessandro 		if (list_empty(&priv->iowait)) {
1014f931551bSRalph Campbell 			dev->n_piowait++;
101501ba79d4SHarish Chegondi 			qp->s_flags |= RVT_S_WAIT_PIO;
1016ffc26907SDennis Dalessandro 			list_add_tail(&priv->iowait, &dev->piowait);
1017f931551bSRalph Campbell 			dd = dd_from_dev(dev);
1018f931551bSRalph Campbell 			dd->f_wantpiobuf_intr(dd, 1);
1019f931551bSRalph Campbell 		}
1020cd18201fSHarish Chegondi 		spin_unlock(&dev->rdi.pending_lock);
102101ba79d4SHarish Chegondi 		qp->s_flags &= ~RVT_S_BUSY;
1022f931551bSRalph Campbell 		ret = -EBUSY;
1023f931551bSRalph Campbell 	}
1024f931551bSRalph Campbell 	spin_unlock_irqrestore(&qp->s_lock, flags);
1025f931551bSRalph Campbell 	return ret;
1026f931551bSRalph Campbell }
1027f931551bSRalph Campbell 
10287c2e11feSDennis Dalessandro static int qib_verbs_send_pio(struct rvt_qp *qp, struct qib_ib_header *ibhdr,
10297c2e11feSDennis Dalessandro 			      u32 hdrwords, struct rvt_sge_state *ss, u32 len,
1030f931551bSRalph Campbell 			      u32 plen, u32 dwords)
1031f931551bSRalph Campbell {
1032f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
1033f931551bSRalph Campbell 	struct qib_pportdata *ppd = dd->pport + qp->port_num - 1;
1034f931551bSRalph Campbell 	u32 *hdr = (u32 *) ibhdr;
1035f931551bSRalph Campbell 	u32 __iomem *piobuf_orig;
1036f931551bSRalph Campbell 	u32 __iomem *piobuf;
1037f931551bSRalph Campbell 	u64 pbc;
1038f931551bSRalph Campbell 	unsigned long flags;
1039f931551bSRalph Campbell 	unsigned flush_wc;
1040f931551bSRalph Campbell 	u32 control;
1041f931551bSRalph Campbell 	u32 pbufn;
1042f931551bSRalph Campbell 
1043f931551bSRalph Campbell 	control = dd->f_setpbc_control(ppd, plen, qp->s_srate,
1044f931551bSRalph Campbell 		be16_to_cpu(ibhdr->lrh[0]) >> 12);
1045f931551bSRalph Campbell 	pbc = ((u64) control << 32) | plen;
1046f931551bSRalph Campbell 	piobuf = dd->f_getsendbuf(ppd, pbc, &pbufn);
1047f931551bSRalph Campbell 	if (unlikely(piobuf == NULL))
1048f931551bSRalph Campbell 		return no_bufs_available(qp);
1049f931551bSRalph Campbell 
1050f931551bSRalph Campbell 	/*
1051f931551bSRalph Campbell 	 * Write the pbc.
1052f931551bSRalph Campbell 	 * We have to flush after the PBC for correctness on some cpus
1053f931551bSRalph Campbell 	 * or WC buffer can be written out of order.
1054f931551bSRalph Campbell 	 */
1055f931551bSRalph Campbell 	writeq(pbc, piobuf);
1056f931551bSRalph Campbell 	piobuf_orig = piobuf;
1057f931551bSRalph Campbell 	piobuf += 2;
1058f931551bSRalph Campbell 
1059f931551bSRalph Campbell 	flush_wc = dd->flags & QIB_PIO_FLUSH_WC;
1060f931551bSRalph Campbell 	if (len == 0) {
1061f931551bSRalph Campbell 		/*
1062f931551bSRalph Campbell 		 * If there is just the header portion, must flush before
1063f931551bSRalph Campbell 		 * writing last word of header for correctness, and after
1064f931551bSRalph Campbell 		 * the last header word (trigger word).
1065f931551bSRalph Campbell 		 */
1066f931551bSRalph Campbell 		if (flush_wc) {
1067f931551bSRalph Campbell 			qib_flush_wc();
1068f931551bSRalph Campbell 			qib_pio_copy(piobuf, hdr, hdrwords - 1);
1069f931551bSRalph Campbell 			qib_flush_wc();
1070f931551bSRalph Campbell 			__raw_writel(hdr[hdrwords - 1], piobuf + hdrwords - 1);
1071f931551bSRalph Campbell 			qib_flush_wc();
1072f931551bSRalph Campbell 		} else
1073f931551bSRalph Campbell 			qib_pio_copy(piobuf, hdr, hdrwords);
1074f931551bSRalph Campbell 		goto done;
1075f931551bSRalph Campbell 	}
1076f931551bSRalph Campbell 
1077f931551bSRalph Campbell 	if (flush_wc)
1078f931551bSRalph Campbell 		qib_flush_wc();
1079f931551bSRalph Campbell 	qib_pio_copy(piobuf, hdr, hdrwords);
1080f931551bSRalph Campbell 	piobuf += hdrwords;
1081f931551bSRalph Campbell 
1082f931551bSRalph Campbell 	/* The common case is aligned and contained in one segment. */
1083f931551bSRalph Campbell 	if (likely(ss->num_sge == 1 && len <= ss->sge.length &&
1084f931551bSRalph Campbell 		   !((unsigned long)ss->sge.vaddr & (sizeof(u32) - 1)))) {
1085f931551bSRalph Campbell 		u32 *addr = (u32 *) ss->sge.vaddr;
1086f931551bSRalph Campbell 
1087f931551bSRalph Campbell 		/* Update address before sending packet. */
1088f931551bSRalph Campbell 		update_sge(ss, len);
1089f931551bSRalph Campbell 		if (flush_wc) {
1090f931551bSRalph Campbell 			qib_pio_copy(piobuf, addr, dwords - 1);
1091f931551bSRalph Campbell 			/* must flush early everything before trigger word */
1092f931551bSRalph Campbell 			qib_flush_wc();
1093f931551bSRalph Campbell 			__raw_writel(addr[dwords - 1], piobuf + dwords - 1);
1094f931551bSRalph Campbell 			/* be sure trigger word is written */
1095f931551bSRalph Campbell 			qib_flush_wc();
1096f931551bSRalph Campbell 		} else
1097f931551bSRalph Campbell 			qib_pio_copy(piobuf, addr, dwords);
1098f931551bSRalph Campbell 		goto done;
1099f931551bSRalph Campbell 	}
1100f931551bSRalph Campbell 	copy_io(piobuf, ss, len, flush_wc);
1101f931551bSRalph Campbell done:
1102f931551bSRalph Campbell 	if (dd->flags & QIB_USE_SPCL_TRIG) {
1103f931551bSRalph Campbell 		u32 spcl_off = (pbufn >= dd->piobcnt2k) ? 2047 : 1023;
1104da12c1f6SMike Marciniszyn 
1105f931551bSRalph Campbell 		qib_flush_wc();
1106f931551bSRalph Campbell 		__raw_writel(0xaebecede, piobuf_orig + spcl_off);
1107f931551bSRalph Campbell 	}
1108f931551bSRalph Campbell 	qib_sendbuf_done(dd, pbufn);
1109f931551bSRalph Campbell 	if (qp->s_rdma_mr) {
11107c2e11feSDennis Dalessandro 		rvt_put_mr(qp->s_rdma_mr);
1111f931551bSRalph Campbell 		qp->s_rdma_mr = NULL;
1112f931551bSRalph Campbell 	}
1113f931551bSRalph Campbell 	if (qp->s_wqe) {
1114f931551bSRalph Campbell 		spin_lock_irqsave(&qp->s_lock, flags);
1115f931551bSRalph Campbell 		qib_send_complete(qp, qp->s_wqe, IB_WC_SUCCESS);
1116f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
1117f931551bSRalph Campbell 	} else if (qp->ibqp.qp_type == IB_QPT_RC) {
1118f931551bSRalph Campbell 		spin_lock_irqsave(&qp->s_lock, flags);
1119f931551bSRalph Campbell 		qib_rc_send_complete(qp, ibhdr);
1120f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
1121f931551bSRalph Campbell 	}
1122f931551bSRalph Campbell 	return 0;
1123f931551bSRalph Campbell }
1124f931551bSRalph Campbell 
1125f931551bSRalph Campbell /**
1126f931551bSRalph Campbell  * qib_verbs_send - send a packet
1127f931551bSRalph Campbell  * @qp: the QP to send on
1128f931551bSRalph Campbell  * @hdr: the packet header
1129f931551bSRalph Campbell  * @hdrwords: the number of 32-bit words in the header
1130f931551bSRalph Campbell  * @ss: the SGE to send
1131f931551bSRalph Campbell  * @len: the length of the packet in bytes
1132f931551bSRalph Campbell  *
1133f931551bSRalph Campbell  * Return zero if packet is sent or queued OK.
113401ba79d4SHarish Chegondi  * Return non-zero and clear qp->s_flags RVT_S_BUSY otherwise.
1135f931551bSRalph Campbell  */
11367c2e11feSDennis Dalessandro int qib_verbs_send(struct rvt_qp *qp, struct qib_ib_header *hdr,
11377c2e11feSDennis Dalessandro 		   u32 hdrwords, struct rvt_sge_state *ss, u32 len)
1138f931551bSRalph Campbell {
1139f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
1140f931551bSRalph Campbell 	u32 plen;
1141f931551bSRalph Campbell 	int ret;
1142f931551bSRalph Campbell 	u32 dwords = (len + 3) >> 2;
1143f931551bSRalph Campbell 
1144f931551bSRalph Campbell 	/*
1145f931551bSRalph Campbell 	 * Calculate the send buffer trigger address.
1146f931551bSRalph Campbell 	 * The +1 counts for the pbc control dword following the pbc length.
1147f931551bSRalph Campbell 	 */
1148f931551bSRalph Campbell 	plen = hdrwords + dwords + 1;
1149f931551bSRalph Campbell 
1150f931551bSRalph Campbell 	/*
1151f931551bSRalph Campbell 	 * VL15 packets (IB_QPT_SMI) will always use PIO, so we
1152f931551bSRalph Campbell 	 * can defer SDMA restart until link goes ACTIVE without
1153f931551bSRalph Campbell 	 * worrying about just how we got there.
1154f931551bSRalph Campbell 	 */
1155f931551bSRalph Campbell 	if (qp->ibqp.qp_type == IB_QPT_SMI ||
1156f931551bSRalph Campbell 	    !(dd->flags & QIB_HAS_SEND_DMA))
1157f931551bSRalph Campbell 		ret = qib_verbs_send_pio(qp, hdr, hdrwords, ss, len,
1158f931551bSRalph Campbell 					 plen, dwords);
1159f931551bSRalph Campbell 	else
1160f931551bSRalph Campbell 		ret = qib_verbs_send_dma(qp, hdr, hdrwords, ss, len,
1161f931551bSRalph Campbell 					 plen, dwords);
1162f931551bSRalph Campbell 
1163f931551bSRalph Campbell 	return ret;
1164f931551bSRalph Campbell }
1165f931551bSRalph Campbell 
1166f931551bSRalph Campbell int qib_snapshot_counters(struct qib_pportdata *ppd, u64 *swords,
1167f931551bSRalph Campbell 			  u64 *rwords, u64 *spkts, u64 *rpkts,
1168f931551bSRalph Campbell 			  u64 *xmit_wait)
1169f931551bSRalph Campbell {
1170f931551bSRalph Campbell 	int ret;
1171f931551bSRalph Campbell 	struct qib_devdata *dd = ppd->dd;
1172f931551bSRalph Campbell 
1173f931551bSRalph Campbell 	if (!(dd->flags & QIB_PRESENT)) {
1174f931551bSRalph Campbell 		/* no hardware, freeze, etc. */
1175f931551bSRalph Campbell 		ret = -EINVAL;
1176f931551bSRalph Campbell 		goto bail;
1177f931551bSRalph Campbell 	}
1178f931551bSRalph Campbell 	*swords = dd->f_portcntr(ppd, QIBPORTCNTR_WORDSEND);
1179f931551bSRalph Campbell 	*rwords = dd->f_portcntr(ppd, QIBPORTCNTR_WORDRCV);
1180f931551bSRalph Campbell 	*spkts = dd->f_portcntr(ppd, QIBPORTCNTR_PKTSEND);
1181f931551bSRalph Campbell 	*rpkts = dd->f_portcntr(ppd, QIBPORTCNTR_PKTRCV);
1182f931551bSRalph Campbell 	*xmit_wait = dd->f_portcntr(ppd, QIBPORTCNTR_SENDSTALL);
1183f931551bSRalph Campbell 
1184f931551bSRalph Campbell 	ret = 0;
1185f931551bSRalph Campbell 
1186f931551bSRalph Campbell bail:
1187f931551bSRalph Campbell 	return ret;
1188f931551bSRalph Campbell }
1189f931551bSRalph Campbell 
1190f931551bSRalph Campbell /**
1191f931551bSRalph Campbell  * qib_get_counters - get various chip counters
1192f931551bSRalph Campbell  * @dd: the qlogic_ib device
1193f931551bSRalph Campbell  * @cntrs: counters are placed here
1194f931551bSRalph Campbell  *
1195f931551bSRalph Campbell  * Return the counters needed by recv_pma_get_portcounters().
1196f931551bSRalph Campbell  */
1197f931551bSRalph Campbell int qib_get_counters(struct qib_pportdata *ppd,
1198f931551bSRalph Campbell 		     struct qib_verbs_counters *cntrs)
1199f931551bSRalph Campbell {
1200f931551bSRalph Campbell 	int ret;
1201f931551bSRalph Campbell 
1202f931551bSRalph Campbell 	if (!(ppd->dd->flags & QIB_PRESENT)) {
1203f931551bSRalph Campbell 		/* no hardware, freeze, etc. */
1204f931551bSRalph Campbell 		ret = -EINVAL;
1205f931551bSRalph Campbell 		goto bail;
1206f931551bSRalph Campbell 	}
1207f931551bSRalph Campbell 	cntrs->symbol_error_counter =
1208f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBSYMBOLERR);
1209f931551bSRalph Campbell 	cntrs->link_error_recovery_counter =
1210f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBLINKERRRECOV);
1211f931551bSRalph Campbell 	/*
1212f931551bSRalph Campbell 	 * The link downed counter counts when the other side downs the
1213f931551bSRalph Campbell 	 * connection.  We add in the number of times we downed the link
1214f931551bSRalph Campbell 	 * due to local link integrity errors to compensate.
1215f931551bSRalph Campbell 	 */
1216f931551bSRalph Campbell 	cntrs->link_downed_counter =
1217f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBLINKDOWN);
1218f931551bSRalph Campbell 	cntrs->port_rcv_errors =
1219f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXDROPPKT) +
1220f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RCVOVFL) +
1221f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERR_RLEN) +
1222f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_INVALIDRLEN) +
1223f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRLINK) +
1224f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRICRC) +
1225f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRVCRC) +
1226f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRLPCRC) +
1227f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_BADFORMAT);
1228f931551bSRalph Campbell 	cntrs->port_rcv_errors +=
1229f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXLOCALPHYERR);
1230f931551bSRalph Campbell 	cntrs->port_rcv_errors +=
1231f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXVLERR);
1232f931551bSRalph Campbell 	cntrs->port_rcv_remphys_errors =
1233f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RCVEBP);
1234f931551bSRalph Campbell 	cntrs->port_xmit_discards =
1235f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_UNSUPVL);
1236f931551bSRalph Campbell 	cntrs->port_xmit_data = ppd->dd->f_portcntr(ppd,
1237f931551bSRalph Campbell 			QIBPORTCNTR_WORDSEND);
1238f931551bSRalph Campbell 	cntrs->port_rcv_data = ppd->dd->f_portcntr(ppd,
1239f931551bSRalph Campbell 			QIBPORTCNTR_WORDRCV);
1240f931551bSRalph Campbell 	cntrs->port_xmit_packets = ppd->dd->f_portcntr(ppd,
1241f931551bSRalph Campbell 			QIBPORTCNTR_PKTSEND);
1242f931551bSRalph Campbell 	cntrs->port_rcv_packets = ppd->dd->f_portcntr(ppd,
1243f931551bSRalph Campbell 			QIBPORTCNTR_PKTRCV);
1244f931551bSRalph Campbell 	cntrs->local_link_integrity_errors =
1245f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_LLI);
1246f931551bSRalph Campbell 	cntrs->excessive_buffer_overrun_errors =
1247f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_EXCESSBUFOVFL);
1248f931551bSRalph Campbell 	cntrs->vl15_dropped =
1249f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_VL15PKTDROP);
1250f931551bSRalph Campbell 
1251f931551bSRalph Campbell 	ret = 0;
1252f931551bSRalph Campbell 
1253f931551bSRalph Campbell bail:
1254f931551bSRalph Campbell 	return ret;
1255f931551bSRalph Campbell }
1256f931551bSRalph Campbell 
1257f931551bSRalph Campbell /**
1258f931551bSRalph Campbell  * qib_ib_piobufavail - callback when a PIO buffer is available
1259f931551bSRalph Campbell  * @dd: the device pointer
1260f931551bSRalph Campbell  *
1261f931551bSRalph Campbell  * This is called from qib_intr() at interrupt level when a PIO buffer is
1262f931551bSRalph Campbell  * available after qib_verbs_send() returned an error that no buffers were
1263f931551bSRalph Campbell  * available. Disable the interrupt if there are no more QPs waiting.
1264f931551bSRalph Campbell  */
1265f931551bSRalph Campbell void qib_ib_piobufavail(struct qib_devdata *dd)
1266f931551bSRalph Campbell {
1267f931551bSRalph Campbell 	struct qib_ibdev *dev = &dd->verbs_dev;
1268f931551bSRalph Campbell 	struct list_head *list;
12697c2e11feSDennis Dalessandro 	struct rvt_qp *qps[5];
12707c2e11feSDennis Dalessandro 	struct rvt_qp *qp;
1271f931551bSRalph Campbell 	unsigned long flags;
1272f931551bSRalph Campbell 	unsigned i, n;
1273ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv;
1274f931551bSRalph Campbell 
1275f931551bSRalph Campbell 	list = &dev->piowait;
1276f931551bSRalph Campbell 	n = 0;
1277f931551bSRalph Campbell 
1278f931551bSRalph Campbell 	/*
1279f931551bSRalph Campbell 	 * Note: checking that the piowait list is empty and clearing
1280f931551bSRalph Campbell 	 * the buffer available interrupt needs to be atomic or we
1281f931551bSRalph Campbell 	 * could end up with QPs on the wait list with the interrupt
1282f931551bSRalph Campbell 	 * disabled.
1283f931551bSRalph Campbell 	 */
1284cd18201fSHarish Chegondi 	spin_lock_irqsave(&dev->rdi.pending_lock, flags);
1285f931551bSRalph Campbell 	while (!list_empty(list)) {
1286f931551bSRalph Campbell 		if (n == ARRAY_SIZE(qps))
1287f931551bSRalph Campbell 			goto full;
1288ffc26907SDennis Dalessandro 		priv = list_entry(list->next, struct qib_qp_priv, iowait);
1289ffc26907SDennis Dalessandro 		qp = priv->owner;
1290ffc26907SDennis Dalessandro 		list_del_init(&priv->iowait);
1291f931551bSRalph Campbell 		atomic_inc(&qp->refcount);
1292f931551bSRalph Campbell 		qps[n++] = qp;
1293f931551bSRalph Campbell 	}
1294f931551bSRalph Campbell 	dd->f_wantpiobuf_intr(dd, 0);
1295f931551bSRalph Campbell full:
1296cd18201fSHarish Chegondi 	spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
1297f931551bSRalph Campbell 
1298f931551bSRalph Campbell 	for (i = 0; i < n; i++) {
1299f931551bSRalph Campbell 		qp = qps[i];
1300f931551bSRalph Campbell 
1301f931551bSRalph Campbell 		spin_lock_irqsave(&qp->s_lock, flags);
130201ba79d4SHarish Chegondi 		if (qp->s_flags & RVT_S_WAIT_PIO) {
130301ba79d4SHarish Chegondi 			qp->s_flags &= ~RVT_S_WAIT_PIO;
1304f931551bSRalph Campbell 			qib_schedule_send(qp);
1305f931551bSRalph Campbell 		}
1306f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
1307f931551bSRalph Campbell 
1308f931551bSRalph Campbell 		/* Notify qib_destroy_qp() if it is waiting. */
1309f931551bSRalph Campbell 		if (atomic_dec_and_test(&qp->refcount))
1310f931551bSRalph Campbell 			wake_up(&qp->wait);
1311f931551bSRalph Campbell 	}
1312f931551bSRalph Campbell }
1313f931551bSRalph Campbell 
1314f931551bSRalph Campbell static int qib_query_port(struct ib_device *ibdev, u8 port,
1315f931551bSRalph Campbell 			  struct ib_port_attr *props)
1316f931551bSRalph Campbell {
1317f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_ibdev(ibdev);
1318f931551bSRalph Campbell 	struct qib_ibport *ibp = to_iport(ibdev, port);
1319f931551bSRalph Campbell 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1320f931551bSRalph Campbell 	enum ib_mtu mtu;
1321f931551bSRalph Campbell 	u16 lid = ppd->lid;
1322f931551bSRalph Campbell 
1323f931551bSRalph Campbell 	memset(props, 0, sizeof(*props));
1324f931551bSRalph Campbell 	props->lid = lid ? lid : be16_to_cpu(IB_LID_PERMISSIVE);
1325f931551bSRalph Campbell 	props->lmc = ppd->lmc;
1326f24a6d48SHarish Chegondi 	props->sm_lid = ibp->rvp.sm_lid;
1327f24a6d48SHarish Chegondi 	props->sm_sl = ibp->rvp.sm_sl;
1328f931551bSRalph Campbell 	props->state = dd->f_iblink_state(ppd->lastibcstat);
1329f931551bSRalph Campbell 	props->phys_state = dd->f_ibphys_portstate(ppd->lastibcstat);
1330f24a6d48SHarish Chegondi 	props->port_cap_flags = ibp->rvp.port_cap_flags;
1331f931551bSRalph Campbell 	props->gid_tbl_len = QIB_GUIDS_PER_PORT;
1332f931551bSRalph Campbell 	props->max_msg_sz = 0x80000000;
1333f931551bSRalph Campbell 	props->pkey_tbl_len = qib_get_npkeys(dd);
1334f24a6d48SHarish Chegondi 	props->bad_pkey_cntr = ibp->rvp.pkey_violations;
1335f24a6d48SHarish Chegondi 	props->qkey_viol_cntr = ibp->rvp.qkey_violations;
1336f931551bSRalph Campbell 	props->active_width = ppd->link_width_active;
1337f931551bSRalph Campbell 	/* See rate_show() */
1338f931551bSRalph Campbell 	props->active_speed = ppd->link_speed_active;
1339f931551bSRalph Campbell 	props->max_vl_num = qib_num_vls(ppd->vls_supported);
1340f931551bSRalph Campbell 	props->init_type_reply = 0;
1341f931551bSRalph Campbell 
1342f931551bSRalph Campbell 	props->max_mtu = qib_ibmtu ? qib_ibmtu : IB_MTU_4096;
1343f931551bSRalph Campbell 	switch (ppd->ibmtu) {
1344f931551bSRalph Campbell 	case 4096:
1345f931551bSRalph Campbell 		mtu = IB_MTU_4096;
1346f931551bSRalph Campbell 		break;
1347f931551bSRalph Campbell 	case 2048:
1348f931551bSRalph Campbell 		mtu = IB_MTU_2048;
1349f931551bSRalph Campbell 		break;
1350f931551bSRalph Campbell 	case 1024:
1351f931551bSRalph Campbell 		mtu = IB_MTU_1024;
1352f931551bSRalph Campbell 		break;
1353f931551bSRalph Campbell 	case 512:
1354f931551bSRalph Campbell 		mtu = IB_MTU_512;
1355f931551bSRalph Campbell 		break;
1356f931551bSRalph Campbell 	case 256:
1357f931551bSRalph Campbell 		mtu = IB_MTU_256;
1358f931551bSRalph Campbell 		break;
1359f931551bSRalph Campbell 	default:
1360f931551bSRalph Campbell 		mtu = IB_MTU_2048;
1361f931551bSRalph Campbell 	}
1362f931551bSRalph Campbell 	props->active_mtu = mtu;
1363f24a6d48SHarish Chegondi 	props->subnet_timeout = ibp->rvp.subnet_timeout;
1364f931551bSRalph Campbell 
1365f931551bSRalph Campbell 	return 0;
1366f931551bSRalph Campbell }
1367f931551bSRalph Campbell 
1368f931551bSRalph Campbell static int qib_modify_device(struct ib_device *device,
1369f931551bSRalph Campbell 			     int device_modify_mask,
1370f931551bSRalph Campbell 			     struct ib_device_modify *device_modify)
1371f931551bSRalph Campbell {
1372f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_ibdev(device);
1373f931551bSRalph Campbell 	unsigned i;
1374f931551bSRalph Campbell 	int ret;
1375f931551bSRalph Campbell 
1376f931551bSRalph Campbell 	if (device_modify_mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
1377f931551bSRalph Campbell 				   IB_DEVICE_MODIFY_NODE_DESC)) {
1378f931551bSRalph Campbell 		ret = -EOPNOTSUPP;
1379f931551bSRalph Campbell 		goto bail;
1380f931551bSRalph Campbell 	}
1381f931551bSRalph Campbell 
1382f931551bSRalph Campbell 	if (device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC) {
1383f931551bSRalph Campbell 		memcpy(device->node_desc, device_modify->node_desc, 64);
1384f931551bSRalph Campbell 		for (i = 0; i < dd->num_pports; i++) {
1385f931551bSRalph Campbell 			struct qib_ibport *ibp = &dd->pport[i].ibport_data;
1386f931551bSRalph Campbell 
1387f931551bSRalph Campbell 			qib_node_desc_chg(ibp);
1388f931551bSRalph Campbell 		}
1389f931551bSRalph Campbell 	}
1390f931551bSRalph Campbell 
1391f931551bSRalph Campbell 	if (device_modify_mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID) {
1392f931551bSRalph Campbell 		ib_qib_sys_image_guid =
1393f931551bSRalph Campbell 			cpu_to_be64(device_modify->sys_image_guid);
1394f931551bSRalph Campbell 		for (i = 0; i < dd->num_pports; i++) {
1395f931551bSRalph Campbell 			struct qib_ibport *ibp = &dd->pport[i].ibport_data;
1396f931551bSRalph Campbell 
1397f931551bSRalph Campbell 			qib_sys_guid_chg(ibp);
1398f931551bSRalph Campbell 		}
1399f931551bSRalph Campbell 	}
1400f931551bSRalph Campbell 
1401f931551bSRalph Campbell 	ret = 0;
1402f931551bSRalph Campbell 
1403f931551bSRalph Campbell bail:
1404f931551bSRalph Campbell 	return ret;
1405f931551bSRalph Campbell }
1406f931551bSRalph Campbell 
1407f931551bSRalph Campbell static int qib_modify_port(struct ib_device *ibdev, u8 port,
1408f931551bSRalph Campbell 			   int port_modify_mask, struct ib_port_modify *props)
1409f931551bSRalph Campbell {
1410f931551bSRalph Campbell 	struct qib_ibport *ibp = to_iport(ibdev, port);
1411f931551bSRalph Campbell 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1412f931551bSRalph Campbell 
1413f24a6d48SHarish Chegondi 	ibp->rvp.port_cap_flags |= props->set_port_cap_mask;
1414f24a6d48SHarish Chegondi 	ibp->rvp.port_cap_flags &= ~props->clr_port_cap_mask;
1415f931551bSRalph Campbell 	if (props->set_port_cap_mask || props->clr_port_cap_mask)
1416f931551bSRalph Campbell 		qib_cap_mask_chg(ibp);
1417f931551bSRalph Campbell 	if (port_modify_mask & IB_PORT_SHUTDOWN)
1418f931551bSRalph Campbell 		qib_set_linkstate(ppd, QIB_IB_LINKDOWN);
1419f931551bSRalph Campbell 	if (port_modify_mask & IB_PORT_RESET_QKEY_CNTR)
1420f24a6d48SHarish Chegondi 		ibp->rvp.qkey_violations = 0;
1421f931551bSRalph Campbell 	return 0;
1422f931551bSRalph Campbell }
1423f931551bSRalph Campbell 
1424f931551bSRalph Campbell static int qib_query_gid(struct ib_device *ibdev, u8 port,
1425f931551bSRalph Campbell 			 int index, union ib_gid *gid)
1426f931551bSRalph Campbell {
1427f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_ibdev(ibdev);
1428f931551bSRalph Campbell 	int ret = 0;
1429f931551bSRalph Campbell 
1430f931551bSRalph Campbell 	if (!port || port > dd->num_pports)
1431f931551bSRalph Campbell 		ret = -EINVAL;
1432f931551bSRalph Campbell 	else {
1433f931551bSRalph Campbell 		struct qib_ibport *ibp = to_iport(ibdev, port);
1434f931551bSRalph Campbell 		struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1435f931551bSRalph Campbell 
1436f24a6d48SHarish Chegondi 		gid->global.subnet_prefix = ibp->rvp.gid_prefix;
1437f931551bSRalph Campbell 		if (index == 0)
1438f931551bSRalph Campbell 			gid->global.interface_id = ppd->guid;
1439f931551bSRalph Campbell 		else if (index < QIB_GUIDS_PER_PORT)
1440f931551bSRalph Campbell 			gid->global.interface_id = ibp->guids[index - 1];
1441f931551bSRalph Campbell 		else
1442f931551bSRalph Campbell 			ret = -EINVAL;
1443f931551bSRalph Campbell 	}
1444f931551bSRalph Campbell 
1445f931551bSRalph Campbell 	return ret;
1446f931551bSRalph Campbell }
1447f931551bSRalph Campbell 
1448f931551bSRalph Campbell int qib_check_ah(struct ib_device *ibdev, struct ib_ah_attr *ah_attr)
1449f931551bSRalph Campbell {
1450f931551bSRalph Campbell 	if (ah_attr->sl > 15)
1451f931551bSRalph Campbell 		return -EINVAL;
1452f931551bSRalph Campbell 
145396ab1ac1SDennis Dalessandro 	return 0;
1454f931551bSRalph Campbell }
1455f931551bSRalph Campbell 
14565418a5abSHarish Chegondi static void qib_notify_new_ah(struct ib_device *ibdev,
14575418a5abSHarish Chegondi 			      struct ib_ah_attr *ah_attr,
14585418a5abSHarish Chegondi 			      struct rvt_ah *ah)
14595418a5abSHarish Chegondi {
14605418a5abSHarish Chegondi 	struct qib_ibport *ibp;
14615418a5abSHarish Chegondi 	struct qib_pportdata *ppd;
14625418a5abSHarish Chegondi 
14635418a5abSHarish Chegondi 	/*
14645418a5abSHarish Chegondi 	 * Do not trust reading anything from rvt_ah at this point as it is not
14655418a5abSHarish Chegondi 	 * done being setup. We can however modify things which we need to set.
14665418a5abSHarish Chegondi 	 */
14675418a5abSHarish Chegondi 
14685418a5abSHarish Chegondi 	ibp = to_iport(ibdev, ah_attr->port_num);
14695418a5abSHarish Chegondi 	ppd = ppd_from_ibp(ibp);
14705418a5abSHarish Chegondi 	ah->vl = ibp->sl_to_vl[ah->attr.sl];
14715418a5abSHarish Chegondi 	ah->log_pmtu = ilog2(ppd->ibmtu);
14725418a5abSHarish Chegondi }
14735418a5abSHarish Chegondi 
14741fb9fed6SMike Marciniszyn struct ib_ah *qib_create_qp0_ah(struct qib_ibport *ibp, u16 dlid)
14751fb9fed6SMike Marciniszyn {
14761fb9fed6SMike Marciniszyn 	struct ib_ah_attr attr;
14771fb9fed6SMike Marciniszyn 	struct ib_ah *ah = ERR_PTR(-EINVAL);
14787c2e11feSDennis Dalessandro 	struct rvt_qp *qp0;
14791fb9fed6SMike Marciniszyn 
1480041af0bbSMike Marciniszyn 	memset(&attr, 0, sizeof(attr));
14811fb9fed6SMike Marciniszyn 	attr.dlid = dlid;
14821fb9fed6SMike Marciniszyn 	attr.port_num = ppd_from_ibp(ibp)->port;
14831fb9fed6SMike Marciniszyn 	rcu_read_lock();
1484f24a6d48SHarish Chegondi 	qp0 = rcu_dereference(ibp->rvp.qp[0]);
14851fb9fed6SMike Marciniszyn 	if (qp0)
14861fb9fed6SMike Marciniszyn 		ah = ib_create_ah(qp0->ibqp.pd, &attr);
14871fb9fed6SMike Marciniszyn 	rcu_read_unlock();
14881fb9fed6SMike Marciniszyn 	return ah;
14891fb9fed6SMike Marciniszyn }
14901fb9fed6SMike Marciniszyn 
1491f931551bSRalph Campbell /**
1492f931551bSRalph Campbell  * qib_get_npkeys - return the size of the PKEY table for context 0
1493f931551bSRalph Campbell  * @dd: the qlogic_ib device
1494f931551bSRalph Campbell  */
1495f931551bSRalph Campbell unsigned qib_get_npkeys(struct qib_devdata *dd)
1496f931551bSRalph Campbell {
1497f931551bSRalph Campbell 	return ARRAY_SIZE(dd->rcd[0]->pkeys);
1498f931551bSRalph Campbell }
1499f931551bSRalph Campbell 
1500f931551bSRalph Campbell /*
1501f931551bSRalph Campbell  * Return the indexed PKEY from the port PKEY table.
1502f931551bSRalph Campbell  * No need to validate rcd[ctxt]; the port is setup if we are here.
1503f931551bSRalph Campbell  */
1504f931551bSRalph Campbell unsigned qib_get_pkey(struct qib_ibport *ibp, unsigned index)
1505f931551bSRalph Campbell {
1506f931551bSRalph Campbell 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1507f931551bSRalph Campbell 	struct qib_devdata *dd = ppd->dd;
1508f931551bSRalph Campbell 	unsigned ctxt = ppd->hw_pidx;
1509f931551bSRalph Campbell 	unsigned ret;
1510f931551bSRalph Campbell 
1511f931551bSRalph Campbell 	/* dd->rcd null if mini_init or some init failures */
1512f931551bSRalph Campbell 	if (!dd->rcd || index >= ARRAY_SIZE(dd->rcd[ctxt]->pkeys))
1513f931551bSRalph Campbell 		ret = 0;
1514f931551bSRalph Campbell 	else
1515f931551bSRalph Campbell 		ret = dd->rcd[ctxt]->pkeys[index];
1516f931551bSRalph Campbell 
1517f931551bSRalph Campbell 	return ret;
1518f931551bSRalph Campbell }
1519f931551bSRalph Campbell 
1520f931551bSRalph Campbell static void init_ibport(struct qib_pportdata *ppd)
1521f931551bSRalph Campbell {
1522f931551bSRalph Campbell 	struct qib_verbs_counters cntrs;
1523f931551bSRalph Campbell 	struct qib_ibport *ibp = &ppd->ibport_data;
1524f931551bSRalph Campbell 
1525f24a6d48SHarish Chegondi 	spin_lock_init(&ibp->rvp.lock);
1526f931551bSRalph Campbell 	/* Set the prefix to the default value (see ch. 4.1.1) */
1527f24a6d48SHarish Chegondi 	ibp->rvp.gid_prefix = IB_DEFAULT_GID_PREFIX;
1528f24a6d48SHarish Chegondi 	ibp->rvp.sm_lid = be16_to_cpu(IB_LID_PERMISSIVE);
1529f24a6d48SHarish Chegondi 	ibp->rvp.port_cap_flags = IB_PORT_SYS_IMAGE_GUID_SUP |
1530f931551bSRalph Campbell 		IB_PORT_CLIENT_REG_SUP | IB_PORT_SL_MAP_SUP |
1531f931551bSRalph Campbell 		IB_PORT_TRAP_SUP | IB_PORT_AUTO_MIGR_SUP |
1532f931551bSRalph Campbell 		IB_PORT_DR_NOTICE_SUP | IB_PORT_CAP_MASK_NOTICE_SUP |
1533f931551bSRalph Campbell 		IB_PORT_OTHER_LOCAL_CHANGES_SUP;
1534f931551bSRalph Campbell 	if (ppd->dd->flags & QIB_HAS_LINK_LATENCY)
1535f24a6d48SHarish Chegondi 		ibp->rvp.port_cap_flags |= IB_PORT_LINK_LATENCY_SUP;
1536f24a6d48SHarish Chegondi 	ibp->rvp.pma_counter_select[0] = IB_PMA_PORT_XMIT_DATA;
1537f24a6d48SHarish Chegondi 	ibp->rvp.pma_counter_select[1] = IB_PMA_PORT_RCV_DATA;
1538f24a6d48SHarish Chegondi 	ibp->rvp.pma_counter_select[2] = IB_PMA_PORT_XMIT_PKTS;
1539f24a6d48SHarish Chegondi 	ibp->rvp.pma_counter_select[3] = IB_PMA_PORT_RCV_PKTS;
1540f24a6d48SHarish Chegondi 	ibp->rvp.pma_counter_select[4] = IB_PMA_PORT_XMIT_WAIT;
1541f931551bSRalph Campbell 
1542f931551bSRalph Campbell 	/* Snapshot current HW counters to "clear" them. */
1543f931551bSRalph Campbell 	qib_get_counters(ppd, &cntrs);
1544f931551bSRalph Campbell 	ibp->z_symbol_error_counter = cntrs.symbol_error_counter;
1545f931551bSRalph Campbell 	ibp->z_link_error_recovery_counter =
1546f931551bSRalph Campbell 		cntrs.link_error_recovery_counter;
1547f931551bSRalph Campbell 	ibp->z_link_downed_counter = cntrs.link_downed_counter;
1548f931551bSRalph Campbell 	ibp->z_port_rcv_errors = cntrs.port_rcv_errors;
1549f931551bSRalph Campbell 	ibp->z_port_rcv_remphys_errors = cntrs.port_rcv_remphys_errors;
1550f931551bSRalph Campbell 	ibp->z_port_xmit_discards = cntrs.port_xmit_discards;
1551f931551bSRalph Campbell 	ibp->z_port_xmit_data = cntrs.port_xmit_data;
1552f931551bSRalph Campbell 	ibp->z_port_rcv_data = cntrs.port_rcv_data;
1553f931551bSRalph Campbell 	ibp->z_port_xmit_packets = cntrs.port_xmit_packets;
1554f931551bSRalph Campbell 	ibp->z_port_rcv_packets = cntrs.port_rcv_packets;
1555f931551bSRalph Campbell 	ibp->z_local_link_integrity_errors =
1556f931551bSRalph Campbell 		cntrs.local_link_integrity_errors;
1557f931551bSRalph Campbell 	ibp->z_excessive_buffer_overrun_errors =
1558f931551bSRalph Campbell 		cntrs.excessive_buffer_overrun_errors;
1559f931551bSRalph Campbell 	ibp->z_vl15_dropped = cntrs.vl15_dropped;
1560f24a6d48SHarish Chegondi 	RCU_INIT_POINTER(ibp->rvp.qp[0], NULL);
1561f24a6d48SHarish Chegondi 	RCU_INIT_POINTER(ibp->rvp.qp[1], NULL);
1562f931551bSRalph Campbell }
1563f931551bSRalph Campbell 
15647738613eSIra Weiny static int qib_port_immutable(struct ib_device *ibdev, u8 port_num,
15657738613eSIra Weiny 			      struct ib_port_immutable *immutable)
15667738613eSIra Weiny {
15677738613eSIra Weiny 	struct ib_port_attr attr;
15687738613eSIra Weiny 	int err;
15697738613eSIra Weiny 
15707738613eSIra Weiny 	err = qib_query_port(ibdev, port_num, &attr);
15717738613eSIra Weiny 	if (err)
15727738613eSIra Weiny 		return err;
15737738613eSIra Weiny 
15747738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
15757738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
1576f9b22e35SIra Weiny 	immutable->core_cap_flags = RDMA_CORE_PORT_IBA_IB;
1577337877a4SIra Weiny 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
15787738613eSIra Weiny 
15797738613eSIra Weiny 	return 0;
15807738613eSIra Weiny }
15817738613eSIra Weiny 
1582f931551bSRalph Campbell /**
15830aeddea2SHarish Chegondi  * qib_fill_device_attr - Fill in rvt dev info device attributes.
15840aeddea2SHarish Chegondi  * @dd: the device data structure
15850aeddea2SHarish Chegondi  */
15860aeddea2SHarish Chegondi static void qib_fill_device_attr(struct qib_devdata *dd)
15870aeddea2SHarish Chegondi {
15880aeddea2SHarish Chegondi 	struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
15890aeddea2SHarish Chegondi 
15900aeddea2SHarish Chegondi 	memset(&rdi->dparms.props, 0, sizeof(rdi->dparms.props));
15910aeddea2SHarish Chegondi 
15920aeddea2SHarish Chegondi 	rdi->dparms.props.max_pd = ib_qib_max_pds;
15930aeddea2SHarish Chegondi 	rdi->dparms.props.max_ah = ib_qib_max_ahs;
15940aeddea2SHarish Chegondi 	rdi->dparms.props.device_cap_flags = IB_DEVICE_BAD_PKEY_CNTR |
15950aeddea2SHarish Chegondi 		IB_DEVICE_BAD_QKEY_CNTR | IB_DEVICE_SHUTDOWN_PORT |
15960aeddea2SHarish Chegondi 		IB_DEVICE_SYS_IMAGE_GUID | IB_DEVICE_RC_RNR_NAK_GEN |
15970aeddea2SHarish Chegondi 		IB_DEVICE_PORT_ACTIVE_EVENT | IB_DEVICE_SRQ_RESIZE;
15980aeddea2SHarish Chegondi 	rdi->dparms.props.page_size_cap = PAGE_SIZE;
15990aeddea2SHarish Chegondi 	rdi->dparms.props.vendor_id =
16000aeddea2SHarish Chegondi 		QIB_SRC_OUI_1 << 16 | QIB_SRC_OUI_2 << 8 | QIB_SRC_OUI_3;
16010aeddea2SHarish Chegondi 	rdi->dparms.props.vendor_part_id = dd->deviceid;
16020aeddea2SHarish Chegondi 	rdi->dparms.props.hw_ver = dd->minrev;
16030aeddea2SHarish Chegondi 	rdi->dparms.props.sys_image_guid = ib_qib_sys_image_guid;
16040aeddea2SHarish Chegondi 	rdi->dparms.props.max_mr_size = ~0ULL;
16050aeddea2SHarish Chegondi 	rdi->dparms.props.max_qp = ib_qib_max_qps;
16060aeddea2SHarish Chegondi 	rdi->dparms.props.max_qp_wr = ib_qib_max_qp_wrs;
16070aeddea2SHarish Chegondi 	rdi->dparms.props.max_sge = ib_qib_max_sges;
16080aeddea2SHarish Chegondi 	rdi->dparms.props.max_sge_rd = ib_qib_max_sges;
16090aeddea2SHarish Chegondi 	rdi->dparms.props.max_cq = ib_qib_max_cqs;
16100aeddea2SHarish Chegondi 	rdi->dparms.props.max_cqe = ib_qib_max_cqes;
16110aeddea2SHarish Chegondi 	rdi->dparms.props.max_ah = ib_qib_max_ahs;
16120aeddea2SHarish Chegondi 	rdi->dparms.props.max_mr = rdi->lkey_table.max;
16130aeddea2SHarish Chegondi 	rdi->dparms.props.max_fmr = rdi->lkey_table.max;
16140aeddea2SHarish Chegondi 	rdi->dparms.props.max_map_per_fmr = 32767;
16150aeddea2SHarish Chegondi 	rdi->dparms.props.max_qp_rd_atom = QIB_MAX_RDMA_ATOMIC;
16160aeddea2SHarish Chegondi 	rdi->dparms.props.max_qp_init_rd_atom = 255;
16170aeddea2SHarish Chegondi 	rdi->dparms.props.max_srq = ib_qib_max_srqs;
16180aeddea2SHarish Chegondi 	rdi->dparms.props.max_srq_wr = ib_qib_max_srq_wrs;
16190aeddea2SHarish Chegondi 	rdi->dparms.props.max_srq_sge = ib_qib_max_srq_sges;
16200aeddea2SHarish Chegondi 	rdi->dparms.props.atomic_cap = IB_ATOMIC_GLOB;
16210aeddea2SHarish Chegondi 	rdi->dparms.props.max_pkeys = qib_get_npkeys(dd);
16220aeddea2SHarish Chegondi 	rdi->dparms.props.max_mcast_grp = ib_qib_max_mcast_grps;
16230aeddea2SHarish Chegondi 	rdi->dparms.props.max_mcast_qp_attach = ib_qib_max_mcast_qp_attached;
16240aeddea2SHarish Chegondi 	rdi->dparms.props.max_total_mcast_qp_attach =
16250aeddea2SHarish Chegondi 					rdi->dparms.props.max_mcast_qp_attach *
16260aeddea2SHarish Chegondi 					rdi->dparms.props.max_mcast_grp;
16270aeddea2SHarish Chegondi }
16280aeddea2SHarish Chegondi 
16290aeddea2SHarish Chegondi /**
1630f931551bSRalph Campbell  * qib_register_ib_device - register our device with the infiniband core
1631f931551bSRalph Campbell  * @dd: the device data structure
1632f931551bSRalph Campbell  * Return the allocated qib_ibdev pointer or NULL on error.
1633f931551bSRalph Campbell  */
1634f931551bSRalph Campbell int qib_register_ib_device(struct qib_devdata *dd)
1635f931551bSRalph Campbell {
1636f931551bSRalph Campbell 	struct qib_ibdev *dev = &dd->verbs_dev;
16372dc05ab5SDennis Dalessandro 	struct ib_device *ibdev = &dev->rdi.ibdev;
1638f931551bSRalph Campbell 	struct qib_pportdata *ppd = dd->pport;
163976fec3e0SHarish Chegondi 	unsigned i, ctxt;
1640f931551bSRalph Campbell 	int ret;
1641f931551bSRalph Campbell 
1642af061a64SMike Marciniszyn 	get_random_bytes(&dev->qp_rnd, sizeof(dev->qp_rnd));
1643f931551bSRalph Campbell 	for (i = 0; i < dd->num_pports; i++)
1644f931551bSRalph Campbell 		init_ibport(ppd + i);
1645f931551bSRalph Campbell 
1646f931551bSRalph Campbell 	/* Only need to initialize non-zero fields. */
1647f931551bSRalph Campbell 	init_timer(&dev->mem_timer);
1648f931551bSRalph Campbell 	dev->mem_timer.function = mem_timer;
1649f931551bSRalph Campbell 	dev->mem_timer.data = (unsigned long) dev;
1650f931551bSRalph Campbell 
165147c7ea6dSHarish Chegondi 	qpt_mask = dd->qpn_mask;
1652f931551bSRalph Campbell 
1653f931551bSRalph Campbell 	INIT_LIST_HEAD(&dev->piowait);
1654f931551bSRalph Campbell 	INIT_LIST_HEAD(&dev->dmawait);
1655f931551bSRalph Campbell 	INIT_LIST_HEAD(&dev->txwait);
1656f931551bSRalph Campbell 	INIT_LIST_HEAD(&dev->memwait);
1657f931551bSRalph Campbell 	INIT_LIST_HEAD(&dev->txreq_free);
1658f931551bSRalph Campbell 
1659f931551bSRalph Campbell 	if (ppd->sdma_descq_cnt) {
1660f931551bSRalph Campbell 		dev->pio_hdrs = dma_alloc_coherent(&dd->pcidev->dev,
1661f931551bSRalph Campbell 						ppd->sdma_descq_cnt *
1662f931551bSRalph Campbell 						sizeof(struct qib_pio_header),
1663f931551bSRalph Campbell 						&dev->pio_hdrs_phys,
1664f931551bSRalph Campbell 						GFP_KERNEL);
1665f931551bSRalph Campbell 		if (!dev->pio_hdrs) {
1666f931551bSRalph Campbell 			ret = -ENOMEM;
1667f931551bSRalph Campbell 			goto err_hdrs;
1668f931551bSRalph Campbell 		}
1669f931551bSRalph Campbell 	}
1670f931551bSRalph Campbell 
1671f931551bSRalph Campbell 	for (i = 0; i < ppd->sdma_descq_cnt; i++) {
1672f931551bSRalph Campbell 		struct qib_verbs_txreq *tx;
1673f931551bSRalph Campbell 
1674041af0bbSMike Marciniszyn 		tx = kzalloc(sizeof(*tx), GFP_KERNEL);
1675f931551bSRalph Campbell 		if (!tx) {
1676f931551bSRalph Campbell 			ret = -ENOMEM;
1677f931551bSRalph Campbell 			goto err_tx;
1678f931551bSRalph Campbell 		}
1679f931551bSRalph Campbell 		tx->hdr_inx = i;
1680f931551bSRalph Campbell 		list_add(&tx->txreq.list, &dev->txreq_free);
1681f931551bSRalph Campbell 	}
1682f931551bSRalph Campbell 
1683f931551bSRalph Campbell 	/*
1684f931551bSRalph Campbell 	 * The system image GUID is supposed to be the same for all
1685f931551bSRalph Campbell 	 * IB HCAs in a single system but since there can be other
1686f931551bSRalph Campbell 	 * device types in the system, we can't be sure this is unique.
1687f931551bSRalph Campbell 	 */
1688f931551bSRalph Campbell 	if (!ib_qib_sys_image_guid)
1689f931551bSRalph Campbell 		ib_qib_sys_image_guid = ppd->guid;
1690f931551bSRalph Campbell 
1691f931551bSRalph Campbell 	strlcpy(ibdev->name, "qib%d", IB_DEVICE_NAME_MAX);
1692f931551bSRalph Campbell 	ibdev->owner = THIS_MODULE;
1693f931551bSRalph Campbell 	ibdev->node_guid = ppd->guid;
1694f931551bSRalph Campbell 	ibdev->phys_port_cnt = dd->num_pports;
1695f931551bSRalph Campbell 	ibdev->dma_device = &dd->pcidev->dev;
1696f931551bSRalph Campbell 	ibdev->modify_device = qib_modify_device;
1697f931551bSRalph Campbell 	ibdev->query_port = qib_query_port;
1698f931551bSRalph Campbell 	ibdev->modify_port = qib_modify_port;
1699f931551bSRalph Campbell 	ibdev->query_gid = qib_query_gid;
1700f931551bSRalph Campbell 	ibdev->destroy_qp = qib_destroy_qp;
1701f931551bSRalph Campbell 	ibdev->process_mad = qib_process_mad;
17027738613eSIra Weiny 	ibdev->get_port_immutable = qib_port_immutable;
1703f931551bSRalph Campbell 
1704f931551bSRalph Campbell 	snprintf(ibdev->node_desc, sizeof(ibdev->node_desc),
1705e2eed58bSVinit Agnihotri 		 "Intel Infiniband HCA %s", init_utsname()->nodename);
1706f931551bSRalph Campbell 
17072dc05ab5SDennis Dalessandro 	/*
17082dc05ab5SDennis Dalessandro 	 * Fill in rvt info object.
17092dc05ab5SDennis Dalessandro 	 */
17102dc05ab5SDennis Dalessandro 	dd->verbs_dev.rdi.driver_f.port_callback = qib_create_port_files;
17116a9df403SDennis Dalessandro 	dd->verbs_dev.rdi.driver_f.get_card_name = qib_get_card_name;
17126a9df403SDennis Dalessandro 	dd->verbs_dev.rdi.driver_f.get_pci_dev = qib_get_pci_dev;
171396ab1ac1SDennis Dalessandro 	dd->verbs_dev.rdi.driver_f.check_ah = qib_check_ah;
17145418a5abSHarish Chegondi 	dd->verbs_dev.rdi.driver_f.notify_new_ah = qib_notify_new_ah;
171547c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.driver_f.alloc_qpn = alloc_qpn;
171647c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.driver_f.qp_priv_alloc = qp_priv_alloc;
171747c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.driver_f.qp_priv_free = qp_priv_free;
171847c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.driver_f.free_all_qps = qib_free_all_qps;
171947c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.driver_f.notify_qp_reset = notify_qp_reset;
1720db3ef0ebSHarish Chegondi 	dd->verbs_dev.rdi.driver_f.do_send = qib_do_send;
1721db3ef0ebSHarish Chegondi 	dd->verbs_dev.rdi.driver_f.schedule_send = qib_schedule_send;
1722*70696ea7SHarish Chegondi 	dd->verbs_dev.rdi.driver_f.quiesce_qp = quiesce_qp;
1723*70696ea7SHarish Chegondi 	dd->verbs_dev.rdi.driver_f.stop_send_queue = stop_send_queue;
1724*70696ea7SHarish Chegondi 	dd->verbs_dev.rdi.driver_f.flush_qp_waiters = flush_qp_waiters;
1725*70696ea7SHarish Chegondi 	dd->verbs_dev.rdi.driver_f.notify_error_qp = notify_error_qp;
1726*70696ea7SHarish Chegondi 	dd->verbs_dev.rdi.driver_f.mtu_to_path_mtu = mtu_to_path_mtu;
1727*70696ea7SHarish Chegondi 	dd->verbs_dev.rdi.driver_f.mtu_from_qp = mtu_from_qp;
1728*70696ea7SHarish Chegondi 	dd->verbs_dev.rdi.driver_f.get_pmtu_from_attr = get_pmtu_from_attr;
172947c7ea6dSHarish Chegondi 
1730*70696ea7SHarish Chegondi 	dd->verbs_dev.rdi.dparms.max_rdma_atomic = QIB_MAX_RDMA_ATOMIC;
17317c2e11feSDennis Dalessandro 	dd->verbs_dev.rdi.dparms.lkey_table_size = qib_lkey_table_size;
173247c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.dparms.qp_table_size = ib_qib_qp_table_size;
173347c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.dparms.qpn_start = 1;
173447c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.dparms.qpn_res_start = QIB_KD_QP;
173547c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.dparms.qpn_res_end = QIB_KD_QP; /* Reserve one QP */
173647c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.dparms.qpn_inc = 1;
173747c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.dparms.qos_shift = 1;
1738034a3e70SHarish Chegondi 	dd->verbs_dev.rdi.dparms.psn_mask = QIB_PSN_MASK;
1739*70696ea7SHarish Chegondi 	dd->verbs_dev.rdi.dparms.psn_shift = QIB_PSN_SHIFT;
1740*70696ea7SHarish Chegondi 	dd->verbs_dev.rdi.dparms.psn_modify_mask = QIB_PSN_MASK;
174176fec3e0SHarish Chegondi 	dd->verbs_dev.rdi.dparms.nports = dd->num_pports;
174276fec3e0SHarish Chegondi 	dd->verbs_dev.rdi.dparms.npkeys = qib_get_npkeys(dd);
17434bb88e5fSHarish Chegondi 	dd->verbs_dev.rdi.dparms.node = dd->assigned_node_id;
17444bb88e5fSHarish Chegondi 	snprintf(dd->verbs_dev.rdi.dparms.cq_name,
17454bb88e5fSHarish Chegondi 		 sizeof(dd->verbs_dev.rdi.dparms.cq_name),
17464bb88e5fSHarish Chegondi 		 "qib_cq%d", dd->unit);
174776fec3e0SHarish Chegondi 
17480aeddea2SHarish Chegondi 	qib_fill_device_attr(dd);
17490aeddea2SHarish Chegondi 
175076fec3e0SHarish Chegondi 	ppd = dd->pport;
175176fec3e0SHarish Chegondi 	for (i = 0; i < dd->num_pports; i++, ppd++) {
175276fec3e0SHarish Chegondi 		ctxt = ppd->hw_pidx;
175376fec3e0SHarish Chegondi 		rvt_init_port(&dd->verbs_dev.rdi,
175476fec3e0SHarish Chegondi 			      &ppd->ibport_data.rvp,
175576fec3e0SHarish Chegondi 			      i,
175676fec3e0SHarish Chegondi 			      dd->rcd[ctxt]->pkeys);
175776fec3e0SHarish Chegondi 	}
17582dc05ab5SDennis Dalessandro 
17592dc05ab5SDennis Dalessandro 	ret = rvt_register_device(&dd->verbs_dev.rdi);
1760f931551bSRalph Campbell 	if (ret)
17615196aa96SDennis Dalessandro 		goto err_tx;
1762f931551bSRalph Campbell 
1763c9bdad3cSMike Marciniszyn 	ret = qib_verbs_register_sysfs(dd);
1764c9bdad3cSMike Marciniszyn 	if (ret)
1765f931551bSRalph Campbell 		goto err_class;
1766f931551bSRalph Campbell 
17675196aa96SDennis Dalessandro 	return ret;
1768f931551bSRalph Campbell 
1769f931551bSRalph Campbell err_class:
17702dc05ab5SDennis Dalessandro 	rvt_unregister_device(&dd->verbs_dev.rdi);
1771f931551bSRalph Campbell err_tx:
1772f931551bSRalph Campbell 	while (!list_empty(&dev->txreq_free)) {
1773f931551bSRalph Campbell 		struct list_head *l = dev->txreq_free.next;
1774f931551bSRalph Campbell 		struct qib_verbs_txreq *tx;
1775f931551bSRalph Campbell 
1776f931551bSRalph Campbell 		list_del(l);
1777f931551bSRalph Campbell 		tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
1778f931551bSRalph Campbell 		kfree(tx);
1779f931551bSRalph Campbell 	}
1780f931551bSRalph Campbell 	if (ppd->sdma_descq_cnt)
1781f931551bSRalph Campbell 		dma_free_coherent(&dd->pcidev->dev,
1782f931551bSRalph Campbell 				  ppd->sdma_descq_cnt *
1783f931551bSRalph Campbell 					sizeof(struct qib_pio_header),
1784f931551bSRalph Campbell 				  dev->pio_hdrs, dev->pio_hdrs_phys);
1785f931551bSRalph Campbell err_hdrs:
1786f931551bSRalph Campbell 	qib_dev_err(dd, "cannot register verbs: %d!\n", -ret);
1787f931551bSRalph Campbell 	return ret;
1788f931551bSRalph Campbell }
1789f931551bSRalph Campbell 
1790f931551bSRalph Campbell void qib_unregister_ib_device(struct qib_devdata *dd)
1791f931551bSRalph Campbell {
1792f931551bSRalph Campbell 	struct qib_ibdev *dev = &dd->verbs_dev;
1793f931551bSRalph Campbell 
1794f931551bSRalph Campbell 	qib_verbs_unregister_sysfs(dd);
1795f931551bSRalph Campbell 
17962dc05ab5SDennis Dalessandro 	rvt_unregister_device(&dd->verbs_dev.rdi);
1797f931551bSRalph Campbell 
1798f931551bSRalph Campbell 	if (!list_empty(&dev->piowait))
1799f931551bSRalph Campbell 		qib_dev_err(dd, "piowait list not empty!\n");
1800f931551bSRalph Campbell 	if (!list_empty(&dev->dmawait))
1801f931551bSRalph Campbell 		qib_dev_err(dd, "dmawait list not empty!\n");
1802f931551bSRalph Campbell 	if (!list_empty(&dev->txwait))
1803f931551bSRalph Campbell 		qib_dev_err(dd, "txwait list not empty!\n");
1804f931551bSRalph Campbell 	if (!list_empty(&dev->memwait))
1805f931551bSRalph Campbell 		qib_dev_err(dd, "memwait list not empty!\n");
1806f931551bSRalph Campbell 
1807f931551bSRalph Campbell 	del_timer_sync(&dev->mem_timer);
1808f931551bSRalph Campbell 	while (!list_empty(&dev->txreq_free)) {
1809f931551bSRalph Campbell 		struct list_head *l = dev->txreq_free.next;
1810f931551bSRalph Campbell 		struct qib_verbs_txreq *tx;
1811f931551bSRalph Campbell 
1812f931551bSRalph Campbell 		list_del(l);
1813f931551bSRalph Campbell 		tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
1814f931551bSRalph Campbell 		kfree(tx);
1815f931551bSRalph Campbell 	}
1816f931551bSRalph Campbell 	if (dd->pport->sdma_descq_cnt)
1817f931551bSRalph Campbell 		dma_free_coherent(&dd->pcidev->dev,
1818f931551bSRalph Campbell 				  dd->pport->sdma_descq_cnt *
1819f931551bSRalph Campbell 					sizeof(struct qib_pio_header),
1820f931551bSRalph Campbell 				  dev->pio_hdrs, dev->pio_hdrs_phys);
1821f931551bSRalph Campbell }
1822551ace12SMike Marciniszyn 
1823551ace12SMike Marciniszyn /*
1824551ace12SMike Marciniszyn  * This must be called with s_lock held.
1825551ace12SMike Marciniszyn  */
18267c2e11feSDennis Dalessandro void qib_schedule_send(struct rvt_qp *qp)
1827551ace12SMike Marciniszyn {
1828ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv = qp->priv;
1829551ace12SMike Marciniszyn 	if (qib_send_ok(qp)) {
1830551ace12SMike Marciniszyn 		struct qib_ibport *ibp =
1831551ace12SMike Marciniszyn 			to_iport(qp->ibqp.device, qp->port_num);
1832551ace12SMike Marciniszyn 		struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1833551ace12SMike Marciniszyn 
1834ffc26907SDennis Dalessandro 		queue_work(ppd->qib_wq, &priv->s_work);
1835551ace12SMike Marciniszyn 	}
1836551ace12SMike Marciniszyn }
1837