xref: /linux/drivers/infiniband/hw/qib/qib_verbs.c (revision 33023fb85a42b53bf778bc025f9667b582282be4)
1f931551bSRalph Campbell /*
25d18ee67SSebastian Sanchez  * Copyright (c) 2012 - 2018 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 /*
11743a474aaSMike Marciniszyn  * Translate ib_wr_opcode into ib_wc_opcode.
11843a474aaSMike Marciniszyn  */
11943a474aaSMike Marciniszyn const enum ib_wc_opcode ib_qib_wc_opcode[] = {
12043a474aaSMike Marciniszyn 	[IB_WR_RDMA_WRITE] = IB_WC_RDMA_WRITE,
12143a474aaSMike Marciniszyn 	[IB_WR_RDMA_WRITE_WITH_IMM] = IB_WC_RDMA_WRITE,
12243a474aaSMike Marciniszyn 	[IB_WR_SEND] = IB_WC_SEND,
12343a474aaSMike Marciniszyn 	[IB_WR_SEND_WITH_IMM] = IB_WC_SEND,
12443a474aaSMike Marciniszyn 	[IB_WR_RDMA_READ] = IB_WC_RDMA_READ,
12543a474aaSMike Marciniszyn 	[IB_WR_ATOMIC_CMP_AND_SWP] = IB_WC_COMP_SWAP,
12643a474aaSMike Marciniszyn 	[IB_WR_ATOMIC_FETCH_AND_ADD] = IB_WC_FETCH_ADD
12743a474aaSMike Marciniszyn };
12843a474aaSMike Marciniszyn 
12943a474aaSMike Marciniszyn /*
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) {
1453fc4a090SBrian Welty 		u32 len = rvt_get_sge_length(sge, length);
146f931551bSRalph Campbell 
1473fc4a090SBrian Welty 		WARN_ON_ONCE(len == 0);
148f931551bSRalph Campbell 		memcpy(sge->vaddr, data, len);
1493fc4a090SBrian Welty 		rvt_update_sge(ss, len, release);
150f931551bSRalph Campbell 		data += len;
151f931551bSRalph Campbell 		length -= len;
152f931551bSRalph Campbell 	}
153f931551bSRalph Campbell }
154f931551bSRalph Campbell 
155f931551bSRalph Campbell /*
156f931551bSRalph Campbell  * Count the number of DMA descriptors needed to send length bytes of data.
157f931551bSRalph Campbell  * Don't modify the qib_sge_state to get the count.
158f931551bSRalph Campbell  * Return zero if any of the segments is not aligned.
159f931551bSRalph Campbell  */
1607c2e11feSDennis Dalessandro static u32 qib_count_sge(struct rvt_sge_state *ss, u32 length)
161f931551bSRalph Campbell {
1627c2e11feSDennis Dalessandro 	struct rvt_sge *sg_list = ss->sg_list;
1637c2e11feSDennis Dalessandro 	struct rvt_sge sge = ss->sge;
164f931551bSRalph Campbell 	u8 num_sge = ss->num_sge;
165f931551bSRalph Campbell 	u32 ndesc = 1;  /* count the header */
166f931551bSRalph Campbell 
167f931551bSRalph Campbell 	while (length) {
168f931551bSRalph Campbell 		u32 len = sge.length;
169f931551bSRalph Campbell 
170f931551bSRalph Campbell 		if (len > length)
171f931551bSRalph Campbell 			len = length;
172f931551bSRalph Campbell 		if (len > sge.sge_length)
173f931551bSRalph Campbell 			len = sge.sge_length;
174f931551bSRalph Campbell 		BUG_ON(len == 0);
175f931551bSRalph Campbell 		if (((long) sge.vaddr & (sizeof(u32) - 1)) ||
176f931551bSRalph Campbell 		    (len != length && (len & (sizeof(u32) - 1)))) {
177f931551bSRalph Campbell 			ndesc = 0;
178f931551bSRalph Campbell 			break;
179f931551bSRalph Campbell 		}
180f931551bSRalph Campbell 		ndesc++;
181f931551bSRalph Campbell 		sge.vaddr += len;
182f931551bSRalph Campbell 		sge.length -= len;
183f931551bSRalph Campbell 		sge.sge_length -= len;
184f931551bSRalph Campbell 		if (sge.sge_length == 0) {
185f931551bSRalph Campbell 			if (--num_sge)
186f931551bSRalph Campbell 				sge = *sg_list++;
187f931551bSRalph Campbell 		} else if (sge.length == 0 && sge.mr->lkey) {
1887c2e11feSDennis Dalessandro 			if (++sge.n >= RVT_SEGSZ) {
189f931551bSRalph Campbell 				if (++sge.m >= sge.mr->mapsz)
190f931551bSRalph Campbell 					break;
191f931551bSRalph Campbell 				sge.n = 0;
192f931551bSRalph Campbell 			}
193f931551bSRalph Campbell 			sge.vaddr =
194f931551bSRalph Campbell 				sge.mr->map[sge.m]->segs[sge.n].vaddr;
195f931551bSRalph Campbell 			sge.length =
196f931551bSRalph Campbell 				sge.mr->map[sge.m]->segs[sge.n].length;
197f931551bSRalph Campbell 		}
198f931551bSRalph Campbell 		length -= len;
199f931551bSRalph Campbell 	}
200f931551bSRalph Campbell 	return ndesc;
201f931551bSRalph Campbell }
202f931551bSRalph Campbell 
203f931551bSRalph Campbell /*
204f931551bSRalph Campbell  * Copy from the SGEs to the data buffer.
205f931551bSRalph Campbell  */
2067c2e11feSDennis Dalessandro static void qib_copy_from_sge(void *data, struct rvt_sge_state *ss, u32 length)
207f931551bSRalph Campbell {
2087c2e11feSDennis Dalessandro 	struct rvt_sge *sge = &ss->sge;
209f931551bSRalph Campbell 
210f931551bSRalph Campbell 	while (length) {
211f931551bSRalph Campbell 		u32 len = sge->length;
212f931551bSRalph Campbell 
213f931551bSRalph Campbell 		if (len > length)
214f931551bSRalph Campbell 			len = length;
215f931551bSRalph Campbell 		if (len > sge->sge_length)
216f931551bSRalph Campbell 			len = sge->sge_length;
217f931551bSRalph Campbell 		BUG_ON(len == 0);
218f931551bSRalph Campbell 		memcpy(data, sge->vaddr, len);
219f931551bSRalph Campbell 		sge->vaddr += len;
220f931551bSRalph Campbell 		sge->length -= len;
221f931551bSRalph Campbell 		sge->sge_length -= len;
222f931551bSRalph Campbell 		if (sge->sge_length == 0) {
223f931551bSRalph Campbell 			if (--ss->num_sge)
224f931551bSRalph Campbell 				*sge = *ss->sg_list++;
225f931551bSRalph Campbell 		} else if (sge->length == 0 && sge->mr->lkey) {
2267c2e11feSDennis Dalessandro 			if (++sge->n >= RVT_SEGSZ) {
227f931551bSRalph Campbell 				if (++sge->m >= sge->mr->mapsz)
228f931551bSRalph Campbell 					break;
229f931551bSRalph Campbell 				sge->n = 0;
230f931551bSRalph Campbell 			}
231f931551bSRalph Campbell 			sge->vaddr =
232f931551bSRalph Campbell 				sge->mr->map[sge->m]->segs[sge->n].vaddr;
233f931551bSRalph Campbell 			sge->length =
234f931551bSRalph Campbell 				sge->mr->map[sge->m]->segs[sge->n].length;
235f931551bSRalph Campbell 		}
236f931551bSRalph Campbell 		data += len;
237f931551bSRalph Campbell 		length -= len;
238f931551bSRalph Campbell 	}
239f931551bSRalph Campbell }
240f931551bSRalph Campbell 
241f931551bSRalph Campbell /**
242f931551bSRalph Campbell  * qib_qp_rcv - processing an incoming packet on a QP
243f931551bSRalph Campbell  * @rcd: the context pointer
244f931551bSRalph Campbell  * @hdr: the packet header
245f931551bSRalph Campbell  * @has_grh: true if the packet has a GRH
246f931551bSRalph Campbell  * @data: the packet data
247f931551bSRalph Campbell  * @tlen: the packet length
248f931551bSRalph Campbell  * @qp: the QP the packet came on
249f931551bSRalph Campbell  *
250f931551bSRalph Campbell  * This is called from qib_ib_rcv() to process an incoming packet
251f931551bSRalph Campbell  * for the given QP.
252f931551bSRalph Campbell  * Called at interrupt level.
253f931551bSRalph Campbell  */
254261a4351SMike Marciniszyn static void qib_qp_rcv(struct qib_ctxtdata *rcd, struct ib_header *hdr,
2557c2e11feSDennis Dalessandro 		       int has_grh, void *data, u32 tlen, struct rvt_qp *qp)
256f931551bSRalph Campbell {
257f931551bSRalph Campbell 	struct qib_ibport *ibp = &rcd->ppd->ibport_data;
258f931551bSRalph Campbell 
259a5210c12SRalph Campbell 	spin_lock(&qp->r_lock);
260a5210c12SRalph Campbell 
261f931551bSRalph Campbell 	/* Check for valid receive state. */
262db3ef0ebSHarish Chegondi 	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
263f24a6d48SHarish Chegondi 		ibp->rvp.n_pkt_drops++;
264a5210c12SRalph Campbell 		goto unlock;
265f931551bSRalph Campbell 	}
266f931551bSRalph Campbell 
267f931551bSRalph Campbell 	switch (qp->ibqp.qp_type) {
268f931551bSRalph Campbell 	case IB_QPT_SMI:
269f931551bSRalph Campbell 	case IB_QPT_GSI:
270f931551bSRalph Campbell 		if (ib_qib_disable_sma)
271f931551bSRalph Campbell 			break;
272f931551bSRalph Campbell 		/* FALLTHROUGH */
273f931551bSRalph Campbell 	case IB_QPT_UD:
274f931551bSRalph Campbell 		qib_ud_rcv(ibp, hdr, has_grh, data, tlen, qp);
275f931551bSRalph Campbell 		break;
276f931551bSRalph Campbell 
277f931551bSRalph Campbell 	case IB_QPT_RC:
278f931551bSRalph Campbell 		qib_rc_rcv(rcd, hdr, has_grh, data, tlen, qp);
279f931551bSRalph Campbell 		break;
280f931551bSRalph Campbell 
281f931551bSRalph Campbell 	case IB_QPT_UC:
282f931551bSRalph Campbell 		qib_uc_rcv(ibp, hdr, has_grh, data, tlen, qp);
283f931551bSRalph Campbell 		break;
284f931551bSRalph Campbell 
285f931551bSRalph Campbell 	default:
286f931551bSRalph Campbell 		break;
287f931551bSRalph Campbell 	}
288a5210c12SRalph Campbell 
289a5210c12SRalph Campbell unlock:
290a5210c12SRalph Campbell 	spin_unlock(&qp->r_lock);
291f931551bSRalph Campbell }
292f931551bSRalph Campbell 
293f931551bSRalph Campbell /**
294f931551bSRalph Campbell  * qib_ib_rcv - process an incoming packet
295f931551bSRalph Campbell  * @rcd: the context pointer
296f931551bSRalph Campbell  * @rhdr: the header of the packet
297f931551bSRalph Campbell  * @data: the packet payload
298f931551bSRalph Campbell  * @tlen: the packet length
299f931551bSRalph Campbell  *
300f931551bSRalph Campbell  * This is called from qib_kreceive() to process an incoming packet at
301f931551bSRalph Campbell  * interrupt level. Tlen is the length of the header + data + CRC in bytes.
302f931551bSRalph Campbell  */
303f931551bSRalph Campbell void qib_ib_rcv(struct qib_ctxtdata *rcd, void *rhdr, void *data, u32 tlen)
304f931551bSRalph Campbell {
305f931551bSRalph Campbell 	struct qib_pportdata *ppd = rcd->ppd;
306f931551bSRalph Campbell 	struct qib_ibport *ibp = &ppd->ibport_data;
307261a4351SMike Marciniszyn 	struct ib_header *hdr = rhdr;
3081cefc2cdSHarish Chegondi 	struct qib_devdata *dd = ppd->dd;
3091cefc2cdSHarish Chegondi 	struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
310261a4351SMike Marciniszyn 	struct ib_other_headers *ohdr;
3117c2e11feSDennis Dalessandro 	struct rvt_qp *qp;
312f931551bSRalph Campbell 	u32 qp_num;
313f931551bSRalph Campbell 	int lnh;
314f931551bSRalph Campbell 	u8 opcode;
315f931551bSRalph Campbell 	u16 lid;
316f931551bSRalph Campbell 
317f931551bSRalph Campbell 	/* 24 == LRH+BTH+CRC */
318f931551bSRalph Campbell 	if (unlikely(tlen < 24))
319f931551bSRalph Campbell 		goto drop;
320f931551bSRalph Campbell 
321f931551bSRalph Campbell 	/* Check for a valid destination LID (see ch. 7.11.1). */
322f931551bSRalph Campbell 	lid = be16_to_cpu(hdr->lrh[1]);
3239ff198f5SDennis Dalessandro 	if (lid < be16_to_cpu(IB_MULTICAST_LID_BASE)) {
324f931551bSRalph Campbell 		lid &= ~((1 << ppd->lmc) - 1);
325f931551bSRalph Campbell 		if (unlikely(lid != ppd->lid))
326f931551bSRalph Campbell 			goto drop;
327f931551bSRalph Campbell 	}
328f931551bSRalph Campbell 
329f931551bSRalph Campbell 	/* Check for GRH */
330f931551bSRalph Campbell 	lnh = be16_to_cpu(hdr->lrh[0]) & 3;
331f931551bSRalph Campbell 	if (lnh == QIB_LRH_BTH)
332f931551bSRalph Campbell 		ohdr = &hdr->u.oth;
333f931551bSRalph Campbell 	else if (lnh == QIB_LRH_GRH) {
334f931551bSRalph Campbell 		u32 vtf;
335f931551bSRalph Campbell 
336f931551bSRalph Campbell 		ohdr = &hdr->u.l.oth;
337f931551bSRalph Campbell 		if (hdr->u.l.grh.next_hdr != IB_GRH_NEXT_HDR)
338f931551bSRalph Campbell 			goto drop;
339f931551bSRalph Campbell 		vtf = be32_to_cpu(hdr->u.l.grh.version_tclass_flow);
340f931551bSRalph Campbell 		if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
341f931551bSRalph Campbell 			goto drop;
342f931551bSRalph Campbell 	} else
343f931551bSRalph Campbell 		goto drop;
344f931551bSRalph Campbell 
345ddb88765SMike Marciniszyn 	opcode = (be32_to_cpu(ohdr->bth[0]) >> 24) & 0x7f;
346ddb88765SMike Marciniszyn #ifdef CONFIG_DEBUG_FS
347ddb88765SMike Marciniszyn 	rcd->opstats->stats[opcode].n_bytes += tlen;
348ddb88765SMike Marciniszyn 	rcd->opstats->stats[opcode].n_packets++;
349ddb88765SMike Marciniszyn #endif
350f931551bSRalph Campbell 
351f931551bSRalph Campbell 	/* Get the destination QP number. */
35270696ea7SHarish Chegondi 	qp_num = be32_to_cpu(ohdr->bth[1]) & RVT_QPN_MASK;
353f931551bSRalph Campbell 	if (qp_num == QIB_MULTICAST_QPN) {
35418f6c582SHarish Chegondi 		struct rvt_mcast *mcast;
35518f6c582SHarish Chegondi 		struct rvt_mcast_qp *p;
356f931551bSRalph Campbell 
357f931551bSRalph Campbell 		if (lnh != QIB_LRH_GRH)
358f931551bSRalph Campbell 			goto drop;
359aad9ff97SMichael J. Ruhl 		mcast = rvt_mcast_find(&ibp->rvp, &hdr->u.l.grh.dgid, lid);
360f931551bSRalph Campbell 		if (mcast == NULL)
361f931551bSRalph Campbell 			goto drop;
3627d7632adSMike Marciniszyn 		this_cpu_inc(ibp->pmastats->n_multicast_rcv);
363f931551bSRalph Campbell 		list_for_each_entry_rcu(p, &mcast->qp_list, list)
364f931551bSRalph Campbell 			qib_qp_rcv(rcd, hdr, 1, data, tlen, p->qp);
365f931551bSRalph Campbell 		/*
36618f6c582SHarish Chegondi 		 * Notify rvt_multicast_detach() if it is waiting for us
367f931551bSRalph Campbell 		 * to finish.
368f931551bSRalph Campbell 		 */
369f931551bSRalph Campbell 		if (atomic_dec_return(&mcast->refcount) <= 1)
370f931551bSRalph Campbell 			wake_up(&mcast->wait);
371f931551bSRalph Campbell 	} else {
3721cefc2cdSHarish Chegondi 		rcu_read_lock();
3731cefc2cdSHarish Chegondi 		qp = rvt_lookup_qpn(rdi, &ibp->rvp, qp_num);
3741cefc2cdSHarish Chegondi 		if (!qp) {
3751cefc2cdSHarish Chegondi 			rcu_read_unlock();
376f931551bSRalph Campbell 			goto drop;
3771cefc2cdSHarish Chegondi 		}
3787d7632adSMike Marciniszyn 		this_cpu_inc(ibp->pmastats->n_unicast_rcv);
379f931551bSRalph Campbell 		qib_qp_rcv(rcd, hdr, lnh == QIB_LRH_GRH, data, tlen, qp);
3801cefc2cdSHarish Chegondi 		rcu_read_unlock();
381f931551bSRalph Campbell 	}
382f931551bSRalph Campbell 	return;
383f931551bSRalph Campbell 
384f931551bSRalph Campbell drop:
385f24a6d48SHarish Chegondi 	ibp->rvp.n_pkt_drops++;
386f931551bSRalph Campbell }
387f931551bSRalph Campbell 
388f931551bSRalph Campbell /*
389f931551bSRalph Campbell  * This is called from a timer to check for QPs
390f931551bSRalph Campbell  * which need kernel memory in order to send a packet.
391f931551bSRalph Campbell  */
3924037c92fSKees Cook static void mem_timer(struct timer_list *t)
393f931551bSRalph Campbell {
3944037c92fSKees Cook 	struct qib_ibdev *dev = from_timer(dev, t, mem_timer);
395f931551bSRalph Campbell 	struct list_head *list = &dev->memwait;
3967c2e11feSDennis Dalessandro 	struct rvt_qp *qp = NULL;
397ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv = NULL;
398f931551bSRalph Campbell 	unsigned long flags;
399f931551bSRalph Campbell 
400cd18201fSHarish Chegondi 	spin_lock_irqsave(&dev->rdi.pending_lock, flags);
401f931551bSRalph Campbell 	if (!list_empty(list)) {
402ffc26907SDennis Dalessandro 		priv = list_entry(list->next, struct qib_qp_priv, iowait);
403ffc26907SDennis Dalessandro 		qp = priv->owner;
404ffc26907SDennis Dalessandro 		list_del_init(&priv->iowait);
405238b1862SSebastian Sanchez 		rvt_get_qp(qp);
406f931551bSRalph Campbell 		if (!list_empty(list))
407f931551bSRalph Campbell 			mod_timer(&dev->mem_timer, jiffies + 1);
408f931551bSRalph Campbell 	}
409cd18201fSHarish Chegondi 	spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
410f931551bSRalph Campbell 
411f931551bSRalph Campbell 	if (qp) {
412f931551bSRalph Campbell 		spin_lock_irqsave(&qp->s_lock, flags);
41301ba79d4SHarish Chegondi 		if (qp->s_flags & RVT_S_WAIT_KMEM) {
41401ba79d4SHarish Chegondi 			qp->s_flags &= ~RVT_S_WAIT_KMEM;
415f931551bSRalph Campbell 			qib_schedule_send(qp);
416f931551bSRalph Campbell 		}
417f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
418238b1862SSebastian Sanchez 		rvt_put_qp(qp);
419f931551bSRalph Campbell 	}
420f931551bSRalph Campbell }
421f931551bSRalph Campbell 
422f931551bSRalph Campbell #ifdef __LITTLE_ENDIAN
423f931551bSRalph Campbell static inline u32 get_upper_bits(u32 data, u32 shift)
424f931551bSRalph Campbell {
425f931551bSRalph Campbell 	return data >> shift;
426f931551bSRalph Campbell }
427f931551bSRalph Campbell 
428f931551bSRalph Campbell static inline u32 set_upper_bits(u32 data, u32 shift)
429f931551bSRalph Campbell {
430f931551bSRalph Campbell 	return data << shift;
431f931551bSRalph Campbell }
432f931551bSRalph Campbell 
433f931551bSRalph Campbell static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
434f931551bSRalph Campbell {
435f931551bSRalph Campbell 	data <<= ((sizeof(u32) - n) * BITS_PER_BYTE);
436f931551bSRalph Campbell 	data >>= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
437f931551bSRalph Campbell 	return data;
438f931551bSRalph Campbell }
439f931551bSRalph Campbell #else
440f931551bSRalph Campbell static inline u32 get_upper_bits(u32 data, u32 shift)
441f931551bSRalph Campbell {
442f931551bSRalph Campbell 	return data << shift;
443f931551bSRalph Campbell }
444f931551bSRalph Campbell 
445f931551bSRalph Campbell static inline u32 set_upper_bits(u32 data, u32 shift)
446f931551bSRalph Campbell {
447f931551bSRalph Campbell 	return data >> shift;
448f931551bSRalph Campbell }
449f931551bSRalph Campbell 
450f931551bSRalph Campbell static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
451f931551bSRalph Campbell {
452f931551bSRalph Campbell 	data >>= ((sizeof(u32) - n) * BITS_PER_BYTE);
453f931551bSRalph Campbell 	data <<= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
454f931551bSRalph Campbell 	return data;
455f931551bSRalph Campbell }
456f931551bSRalph Campbell #endif
457f931551bSRalph Campbell 
4587c2e11feSDennis Dalessandro static void copy_io(u32 __iomem *piobuf, struct rvt_sge_state *ss,
459f931551bSRalph Campbell 		    u32 length, unsigned flush_wc)
460f931551bSRalph Campbell {
461f931551bSRalph Campbell 	u32 extra = 0;
462f931551bSRalph Campbell 	u32 data = 0;
463f931551bSRalph Campbell 	u32 last;
464f931551bSRalph Campbell 
465f931551bSRalph Campbell 	while (1) {
466f931551bSRalph Campbell 		u32 len = ss->sge.length;
467f931551bSRalph Campbell 		u32 off;
468f931551bSRalph Campbell 
469f931551bSRalph Campbell 		if (len > length)
470f931551bSRalph Campbell 			len = length;
471f931551bSRalph Campbell 		if (len > ss->sge.sge_length)
472f931551bSRalph Campbell 			len = ss->sge.sge_length;
473f931551bSRalph Campbell 		BUG_ON(len == 0);
474f931551bSRalph Campbell 		/* If the source address is not aligned, try to align it. */
475f931551bSRalph Campbell 		off = (unsigned long)ss->sge.vaddr & (sizeof(u32) - 1);
476f931551bSRalph Campbell 		if (off) {
477f931551bSRalph Campbell 			u32 *addr = (u32 *)((unsigned long)ss->sge.vaddr &
478f931551bSRalph Campbell 					    ~(sizeof(u32) - 1));
479f931551bSRalph Campbell 			u32 v = get_upper_bits(*addr, off * BITS_PER_BYTE);
480f931551bSRalph Campbell 			u32 y;
481f931551bSRalph Campbell 
482f931551bSRalph Campbell 			y = sizeof(u32) - off;
483f931551bSRalph Campbell 			if (len > y)
484f931551bSRalph Campbell 				len = y;
485f931551bSRalph Campbell 			if (len + extra >= sizeof(u32)) {
486f931551bSRalph Campbell 				data |= set_upper_bits(v, extra *
487f931551bSRalph Campbell 						       BITS_PER_BYTE);
488f931551bSRalph Campbell 				len = sizeof(u32) - extra;
489f931551bSRalph Campbell 				if (len == length) {
490f931551bSRalph Campbell 					last = data;
491f931551bSRalph Campbell 					break;
492f931551bSRalph Campbell 				}
493f931551bSRalph Campbell 				__raw_writel(data, piobuf);
494f931551bSRalph Campbell 				piobuf++;
495f931551bSRalph Campbell 				extra = 0;
496f931551bSRalph Campbell 				data = 0;
497f931551bSRalph Campbell 			} else {
498f931551bSRalph Campbell 				/* Clear unused upper bytes */
499f931551bSRalph Campbell 				data |= clear_upper_bytes(v, len, extra);
500f931551bSRalph Campbell 				if (len == length) {
501f931551bSRalph Campbell 					last = data;
502f931551bSRalph Campbell 					break;
503f931551bSRalph Campbell 				}
504f931551bSRalph Campbell 				extra += len;
505f931551bSRalph Campbell 			}
506f931551bSRalph Campbell 		} else if (extra) {
507f931551bSRalph Campbell 			/* Source address is aligned. */
508f931551bSRalph Campbell 			u32 *addr = (u32 *) ss->sge.vaddr;
509f931551bSRalph Campbell 			int shift = extra * BITS_PER_BYTE;
510f931551bSRalph Campbell 			int ushift = 32 - shift;
511f931551bSRalph Campbell 			u32 l = len;
512f931551bSRalph Campbell 
513f931551bSRalph Campbell 			while (l >= sizeof(u32)) {
514f931551bSRalph Campbell 				u32 v = *addr;
515f931551bSRalph Campbell 
516f931551bSRalph Campbell 				data |= set_upper_bits(v, shift);
517f931551bSRalph Campbell 				__raw_writel(data, piobuf);
518f931551bSRalph Campbell 				data = get_upper_bits(v, ushift);
519f931551bSRalph Campbell 				piobuf++;
520f931551bSRalph Campbell 				addr++;
521f931551bSRalph Campbell 				l -= sizeof(u32);
522f931551bSRalph Campbell 			}
523f931551bSRalph Campbell 			/*
524f931551bSRalph Campbell 			 * We still have 'extra' number of bytes leftover.
525f931551bSRalph Campbell 			 */
526f931551bSRalph Campbell 			if (l) {
527f931551bSRalph Campbell 				u32 v = *addr;
528f931551bSRalph Campbell 
529f931551bSRalph Campbell 				if (l + extra >= sizeof(u32)) {
530f931551bSRalph Campbell 					data |= set_upper_bits(v, shift);
531f931551bSRalph Campbell 					len -= l + extra - sizeof(u32);
532f931551bSRalph Campbell 					if (len == length) {
533f931551bSRalph Campbell 						last = data;
534f931551bSRalph Campbell 						break;
535f931551bSRalph Campbell 					}
536f931551bSRalph Campbell 					__raw_writel(data, piobuf);
537f931551bSRalph Campbell 					piobuf++;
538f931551bSRalph Campbell 					extra = 0;
539f931551bSRalph Campbell 					data = 0;
540f931551bSRalph Campbell 				} else {
541f931551bSRalph Campbell 					/* Clear unused upper bytes */
542f931551bSRalph Campbell 					data |= clear_upper_bytes(v, l, extra);
543f931551bSRalph Campbell 					if (len == length) {
544f931551bSRalph Campbell 						last = data;
545f931551bSRalph Campbell 						break;
546f931551bSRalph Campbell 					}
547f931551bSRalph Campbell 					extra += l;
548f931551bSRalph Campbell 				}
549f931551bSRalph Campbell 			} else if (len == length) {
550f931551bSRalph Campbell 				last = data;
551f931551bSRalph Campbell 				break;
552f931551bSRalph Campbell 			}
553f931551bSRalph Campbell 		} else if (len == length) {
554f931551bSRalph Campbell 			u32 w;
555f931551bSRalph Campbell 
556f931551bSRalph Campbell 			/*
557f931551bSRalph Campbell 			 * Need to round up for the last dword in the
558f931551bSRalph Campbell 			 * packet.
559f931551bSRalph Campbell 			 */
560f931551bSRalph Campbell 			w = (len + 3) >> 2;
561f931551bSRalph Campbell 			qib_pio_copy(piobuf, ss->sge.vaddr, w - 1);
562f931551bSRalph Campbell 			piobuf += w - 1;
563f931551bSRalph Campbell 			last = ((u32 *) ss->sge.vaddr)[w - 1];
564f931551bSRalph Campbell 			break;
565f931551bSRalph Campbell 		} else {
566f931551bSRalph Campbell 			u32 w = len >> 2;
567f931551bSRalph Campbell 
568f931551bSRalph Campbell 			qib_pio_copy(piobuf, ss->sge.vaddr, w);
569f931551bSRalph Campbell 			piobuf += w;
570f931551bSRalph Campbell 
571f931551bSRalph Campbell 			extra = len & (sizeof(u32) - 1);
572f931551bSRalph Campbell 			if (extra) {
573f931551bSRalph Campbell 				u32 v = ((u32 *) ss->sge.vaddr)[w];
574f931551bSRalph Campbell 
575f931551bSRalph Campbell 				/* Clear unused upper bytes */
576f931551bSRalph Campbell 				data = clear_upper_bytes(v, extra, 0);
577f931551bSRalph Campbell 			}
578f931551bSRalph Campbell 		}
5793fc4a090SBrian Welty 		rvt_update_sge(ss, len, false);
580f931551bSRalph Campbell 		length -= len;
581f931551bSRalph Campbell 	}
582f931551bSRalph Campbell 	/* Update address before sending packet. */
5833fc4a090SBrian Welty 	rvt_update_sge(ss, length, false);
584f931551bSRalph Campbell 	if (flush_wc) {
585f931551bSRalph Campbell 		/* must flush early everything before trigger word */
586f931551bSRalph Campbell 		qib_flush_wc();
587f931551bSRalph Campbell 		__raw_writel(last, piobuf);
588f931551bSRalph Campbell 		/* be sure trigger word is written */
589f931551bSRalph Campbell 		qib_flush_wc();
590f931551bSRalph Campbell 	} else
591f931551bSRalph Campbell 		__raw_writel(last, piobuf);
592f931551bSRalph Campbell }
593f931551bSRalph Campbell 
59448947109SMike Marciniszyn static noinline struct qib_verbs_txreq *__get_txreq(struct qib_ibdev *dev,
5957c2e11feSDennis Dalessandro 					   struct rvt_qp *qp)
596f931551bSRalph Campbell {
597ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv = qp->priv;
598f931551bSRalph Campbell 	struct qib_verbs_txreq *tx;
599f931551bSRalph Campbell 	unsigned long flags;
600f931551bSRalph Campbell 
601f931551bSRalph Campbell 	spin_lock_irqsave(&qp->s_lock, flags);
602cd18201fSHarish Chegondi 	spin_lock(&dev->rdi.pending_lock);
603f931551bSRalph Campbell 
604f931551bSRalph Campbell 	if (!list_empty(&dev->txreq_free)) {
605f931551bSRalph Campbell 		struct list_head *l = dev->txreq_free.next;
606f931551bSRalph Campbell 
607f931551bSRalph Campbell 		list_del(l);
608cd18201fSHarish Chegondi 		spin_unlock(&dev->rdi.pending_lock);
60948947109SMike Marciniszyn 		spin_unlock_irqrestore(&qp->s_lock, flags);
610f931551bSRalph Campbell 		tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
611f931551bSRalph Campbell 	} else {
612db3ef0ebSHarish Chegondi 		if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK &&
613ffc26907SDennis Dalessandro 		    list_empty(&priv->iowait)) {
614f931551bSRalph Campbell 			dev->n_txwait++;
61501ba79d4SHarish Chegondi 			qp->s_flags |= RVT_S_WAIT_TX;
616ffc26907SDennis Dalessandro 			list_add_tail(&priv->iowait, &dev->txwait);
617f931551bSRalph Campbell 		}
61801ba79d4SHarish Chegondi 		qp->s_flags &= ~RVT_S_BUSY;
619cd18201fSHarish Chegondi 		spin_unlock(&dev->rdi.pending_lock);
620f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
62148947109SMike Marciniszyn 		tx = ERR_PTR(-EBUSY);
62248947109SMike Marciniszyn 	}
62348947109SMike Marciniszyn 	return tx;
62448947109SMike Marciniszyn }
625f931551bSRalph Campbell 
62648947109SMike Marciniszyn static inline struct qib_verbs_txreq *get_txreq(struct qib_ibdev *dev,
6277c2e11feSDennis Dalessandro 					 struct rvt_qp *qp)
62848947109SMike Marciniszyn {
62948947109SMike Marciniszyn 	struct qib_verbs_txreq *tx;
63048947109SMike Marciniszyn 	unsigned long flags;
63148947109SMike Marciniszyn 
632cd18201fSHarish Chegondi 	spin_lock_irqsave(&dev->rdi.pending_lock, flags);
63348947109SMike Marciniszyn 	/* assume the list non empty */
63448947109SMike Marciniszyn 	if (likely(!list_empty(&dev->txreq_free))) {
63548947109SMike Marciniszyn 		struct list_head *l = dev->txreq_free.next;
63648947109SMike Marciniszyn 
63748947109SMike Marciniszyn 		list_del(l);
638cd18201fSHarish Chegondi 		spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
63948947109SMike Marciniszyn 		tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
64048947109SMike Marciniszyn 	} else {
64148947109SMike Marciniszyn 		/* call slow path to get the extra lock */
642cd18201fSHarish Chegondi 		spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
64348947109SMike Marciniszyn 		tx =  __get_txreq(dev, qp);
64448947109SMike Marciniszyn 	}
645f931551bSRalph Campbell 	return tx;
646f931551bSRalph Campbell }
647f931551bSRalph Campbell 
648f931551bSRalph Campbell void qib_put_txreq(struct qib_verbs_txreq *tx)
649f931551bSRalph Campbell {
650f931551bSRalph Campbell 	struct qib_ibdev *dev;
6517c2e11feSDennis Dalessandro 	struct rvt_qp *qp;
652ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv;
653f931551bSRalph Campbell 	unsigned long flags;
654f931551bSRalph Campbell 
655f931551bSRalph Campbell 	qp = tx->qp;
656f931551bSRalph Campbell 	dev = to_idev(qp->ibqp.device);
657f931551bSRalph Campbell 
658f931551bSRalph Campbell 	if (tx->mr) {
6597c2e11feSDennis Dalessandro 		rvt_put_mr(tx->mr);
660f931551bSRalph Campbell 		tx->mr = NULL;
661f931551bSRalph Campbell 	}
662f931551bSRalph Campbell 	if (tx->txreq.flags & QIB_SDMA_TXREQ_F_FREEBUF) {
663f931551bSRalph Campbell 		tx->txreq.flags &= ~QIB_SDMA_TXREQ_F_FREEBUF;
664f931551bSRalph Campbell 		dma_unmap_single(&dd_from_dev(dev)->pcidev->dev,
665f931551bSRalph Campbell 				 tx->txreq.addr, tx->hdr_dwords << 2,
666f931551bSRalph Campbell 				 DMA_TO_DEVICE);
667f931551bSRalph Campbell 		kfree(tx->align_buf);
668f931551bSRalph Campbell 	}
669f931551bSRalph Campbell 
670cd18201fSHarish Chegondi 	spin_lock_irqsave(&dev->rdi.pending_lock, flags);
671f931551bSRalph Campbell 
672f931551bSRalph Campbell 	/* Put struct back on free list */
673f931551bSRalph Campbell 	list_add(&tx->txreq.list, &dev->txreq_free);
674f931551bSRalph Campbell 
675f931551bSRalph Campbell 	if (!list_empty(&dev->txwait)) {
676f931551bSRalph Campbell 		/* Wake up first QP wanting a free struct */
677ffc26907SDennis Dalessandro 		priv = list_entry(dev->txwait.next, struct qib_qp_priv,
678ffc26907SDennis Dalessandro 				  iowait);
679ffc26907SDennis Dalessandro 		qp = priv->owner;
680ffc26907SDennis Dalessandro 		list_del_init(&priv->iowait);
681238b1862SSebastian Sanchez 		rvt_get_qp(qp);
682cd18201fSHarish Chegondi 		spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
683f931551bSRalph Campbell 
684f931551bSRalph Campbell 		spin_lock_irqsave(&qp->s_lock, flags);
68501ba79d4SHarish Chegondi 		if (qp->s_flags & RVT_S_WAIT_TX) {
68601ba79d4SHarish Chegondi 			qp->s_flags &= ~RVT_S_WAIT_TX;
687f931551bSRalph Campbell 			qib_schedule_send(qp);
688f931551bSRalph Campbell 		}
689f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
690f931551bSRalph Campbell 
691238b1862SSebastian Sanchez 		rvt_put_qp(qp);
692f931551bSRalph Campbell 	} else
693cd18201fSHarish Chegondi 		spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
694f931551bSRalph Campbell }
695f931551bSRalph Campbell 
696f931551bSRalph Campbell /*
697f931551bSRalph Campbell  * This is called when there are send DMA descriptors that might be
698f931551bSRalph Campbell  * available.
699f931551bSRalph Campbell  *
700f931551bSRalph Campbell  * This is called with ppd->sdma_lock held.
701f931551bSRalph Campbell  */
702f931551bSRalph Campbell void qib_verbs_sdma_desc_avail(struct qib_pportdata *ppd, unsigned avail)
703f931551bSRalph Campbell {
7042055d1f0SBart Van Assche 	struct rvt_qp *qp;
705ffc26907SDennis Dalessandro 	struct qib_qp_priv *qpp, *nqpp;
7067c2e11feSDennis Dalessandro 	struct rvt_qp *qps[20];
707f931551bSRalph Campbell 	struct qib_ibdev *dev;
708f931551bSRalph Campbell 	unsigned i, n;
709f931551bSRalph Campbell 
710f931551bSRalph Campbell 	n = 0;
711f931551bSRalph Campbell 	dev = &ppd->dd->verbs_dev;
712cd18201fSHarish Chegondi 	spin_lock(&dev->rdi.pending_lock);
713f931551bSRalph Campbell 
714f931551bSRalph Campbell 	/* Search wait list for first QP wanting DMA descriptors. */
715ffc26907SDennis Dalessandro 	list_for_each_entry_safe(qpp, nqpp, &dev->dmawait, iowait) {
716ffc26907SDennis Dalessandro 		qp = qpp->owner;
717f931551bSRalph Campbell 		if (qp->port_num != ppd->port)
718f931551bSRalph Campbell 			continue;
719f931551bSRalph Campbell 		if (n == ARRAY_SIZE(qps))
720f931551bSRalph Campbell 			break;
721ffc26907SDennis Dalessandro 		if (qpp->s_tx->txreq.sg_count > avail)
722f931551bSRalph Campbell 			break;
723ffc26907SDennis Dalessandro 		avail -= qpp->s_tx->txreq.sg_count;
724ffc26907SDennis Dalessandro 		list_del_init(&qpp->iowait);
725238b1862SSebastian Sanchez 		rvt_get_qp(qp);
726f931551bSRalph Campbell 		qps[n++] = qp;
727f931551bSRalph Campbell 	}
728f931551bSRalph Campbell 
729cd18201fSHarish Chegondi 	spin_unlock(&dev->rdi.pending_lock);
730f931551bSRalph Campbell 
731f931551bSRalph Campbell 	for (i = 0; i < n; i++) {
732f931551bSRalph Campbell 		qp = qps[i];
733f931551bSRalph Campbell 		spin_lock(&qp->s_lock);
73401ba79d4SHarish Chegondi 		if (qp->s_flags & RVT_S_WAIT_DMA_DESC) {
73501ba79d4SHarish Chegondi 			qp->s_flags &= ~RVT_S_WAIT_DMA_DESC;
736f931551bSRalph Campbell 			qib_schedule_send(qp);
737f931551bSRalph Campbell 		}
738f931551bSRalph Campbell 		spin_unlock(&qp->s_lock);
739238b1862SSebastian Sanchez 		rvt_put_qp(qp);
740f931551bSRalph Campbell 	}
741f931551bSRalph Campbell }
742f931551bSRalph Campbell 
743f931551bSRalph Campbell /*
744f931551bSRalph Campbell  * This is called with ppd->sdma_lock held.
745f931551bSRalph Campbell  */
746f931551bSRalph Campbell static void sdma_complete(struct qib_sdma_txreq *cookie, int status)
747f931551bSRalph Campbell {
748f931551bSRalph Campbell 	struct qib_verbs_txreq *tx =
749f931551bSRalph Campbell 		container_of(cookie, struct qib_verbs_txreq, txreq);
7507c2e11feSDennis Dalessandro 	struct rvt_qp *qp = tx->qp;
751ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv = qp->priv;
752f931551bSRalph Campbell 
753f931551bSRalph Campbell 	spin_lock(&qp->s_lock);
754f931551bSRalph Campbell 	if (tx->wqe)
755f931551bSRalph Campbell 		qib_send_complete(qp, tx->wqe, IB_WC_SUCCESS);
756f931551bSRalph Campbell 	else if (qp->ibqp.qp_type == IB_QPT_RC) {
757261a4351SMike Marciniszyn 		struct ib_header *hdr;
758f931551bSRalph Campbell 
759f931551bSRalph Campbell 		if (tx->txreq.flags & QIB_SDMA_TXREQ_F_FREEBUF)
760f931551bSRalph Campbell 			hdr = &tx->align_buf->hdr;
761f931551bSRalph Campbell 		else {
762f931551bSRalph Campbell 			struct qib_ibdev *dev = to_idev(qp->ibqp.device);
763f931551bSRalph Campbell 
764f931551bSRalph Campbell 			hdr = &dev->pio_hdrs[tx->hdr_inx].hdr;
765f931551bSRalph Campbell 		}
766f931551bSRalph Campbell 		qib_rc_send_complete(qp, hdr);
767f931551bSRalph Campbell 	}
768ffc26907SDennis Dalessandro 	if (atomic_dec_and_test(&priv->s_dma_busy)) {
769f931551bSRalph Campbell 		if (qp->state == IB_QPS_RESET)
770ffc26907SDennis Dalessandro 			wake_up(&priv->wait_dma);
77101ba79d4SHarish Chegondi 		else if (qp->s_flags & RVT_S_WAIT_DMA) {
77201ba79d4SHarish Chegondi 			qp->s_flags &= ~RVT_S_WAIT_DMA;
773f931551bSRalph Campbell 			qib_schedule_send(qp);
774f931551bSRalph Campbell 		}
775f931551bSRalph Campbell 	}
776f931551bSRalph Campbell 	spin_unlock(&qp->s_lock);
777f931551bSRalph Campbell 
778f931551bSRalph Campbell 	qib_put_txreq(tx);
779f931551bSRalph Campbell }
780f931551bSRalph Campbell 
7817c2e11feSDennis Dalessandro static int wait_kmem(struct qib_ibdev *dev, struct rvt_qp *qp)
782f931551bSRalph Campbell {
783ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv = qp->priv;
784f931551bSRalph Campbell 	unsigned long flags;
785f931551bSRalph Campbell 	int ret = 0;
786f931551bSRalph Campbell 
787f931551bSRalph Campbell 	spin_lock_irqsave(&qp->s_lock, flags);
788db3ef0ebSHarish Chegondi 	if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
789cd18201fSHarish Chegondi 		spin_lock(&dev->rdi.pending_lock);
790ffc26907SDennis Dalessandro 		if (list_empty(&priv->iowait)) {
791f931551bSRalph Campbell 			if (list_empty(&dev->memwait))
792f931551bSRalph Campbell 				mod_timer(&dev->mem_timer, jiffies + 1);
79301ba79d4SHarish Chegondi 			qp->s_flags |= RVT_S_WAIT_KMEM;
794ffc26907SDennis Dalessandro 			list_add_tail(&priv->iowait, &dev->memwait);
795f931551bSRalph Campbell 		}
796cd18201fSHarish Chegondi 		spin_unlock(&dev->rdi.pending_lock);
79701ba79d4SHarish Chegondi 		qp->s_flags &= ~RVT_S_BUSY;
798f931551bSRalph Campbell 		ret = -EBUSY;
799f931551bSRalph Campbell 	}
800f931551bSRalph Campbell 	spin_unlock_irqrestore(&qp->s_lock, flags);
801f931551bSRalph Campbell 
802f931551bSRalph Campbell 	return ret;
803f931551bSRalph Campbell }
804f931551bSRalph Campbell 
805261a4351SMike Marciniszyn static int qib_verbs_send_dma(struct rvt_qp *qp, struct ib_header *hdr,
8067c2e11feSDennis Dalessandro 			      u32 hdrwords, struct rvt_sge_state *ss, u32 len,
807f931551bSRalph Campbell 			      u32 plen, u32 dwords)
808f931551bSRalph Campbell {
809ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv = qp->priv;
810f931551bSRalph Campbell 	struct qib_ibdev *dev = to_idev(qp->ibqp.device);
811f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_dev(dev);
812f931551bSRalph Campbell 	struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
813f931551bSRalph Campbell 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
814f931551bSRalph Campbell 	struct qib_verbs_txreq *tx;
815f931551bSRalph Campbell 	struct qib_pio_header *phdr;
816f931551bSRalph Campbell 	u32 control;
817f931551bSRalph Campbell 	u32 ndesc;
818f931551bSRalph Campbell 	int ret;
819f931551bSRalph Campbell 
820ffc26907SDennis Dalessandro 	tx = priv->s_tx;
821f931551bSRalph Campbell 	if (tx) {
822ffc26907SDennis Dalessandro 		priv->s_tx = NULL;
823f931551bSRalph Campbell 		/* resend previously constructed packet */
824f931551bSRalph Campbell 		ret = qib_sdma_verbs_send(ppd, tx->ss, tx->dwords, tx);
825f931551bSRalph Campbell 		goto bail;
826f931551bSRalph Campbell 	}
827f931551bSRalph Campbell 
82848947109SMike Marciniszyn 	tx = get_txreq(dev, qp);
82948947109SMike Marciniszyn 	if (IS_ERR(tx))
83048947109SMike Marciniszyn 		goto bail_tx;
831f931551bSRalph Campbell 
832f931551bSRalph Campbell 	control = dd->f_setpbc_control(ppd, plen, qp->s_srate,
833f931551bSRalph Campbell 				       be16_to_cpu(hdr->lrh[0]) >> 12);
834f931551bSRalph Campbell 	tx->qp = qp;
835f931551bSRalph Campbell 	tx->wqe = qp->s_wqe;
836f931551bSRalph Campbell 	tx->mr = qp->s_rdma_mr;
837f931551bSRalph Campbell 	if (qp->s_rdma_mr)
838f931551bSRalph Campbell 		qp->s_rdma_mr = NULL;
839f931551bSRalph Campbell 	tx->txreq.callback = sdma_complete;
840f931551bSRalph Campbell 	if (dd->flags & QIB_HAS_SDMA_TIMEOUT)
841f931551bSRalph Campbell 		tx->txreq.flags = QIB_SDMA_TXREQ_F_HEADTOHOST;
842f931551bSRalph Campbell 	else
843f931551bSRalph Campbell 		tx->txreq.flags = QIB_SDMA_TXREQ_F_INTREQ;
844f931551bSRalph Campbell 	if (plen + 1 > dd->piosize2kmax_dwords)
845f931551bSRalph Campbell 		tx->txreq.flags |= QIB_SDMA_TXREQ_F_USELARGEBUF;
846f931551bSRalph Campbell 
847f931551bSRalph Campbell 	if (len) {
848f931551bSRalph Campbell 		/*
849f931551bSRalph Campbell 		 * Don't try to DMA if it takes more descriptors than
850f931551bSRalph Campbell 		 * the queue holds.
851f931551bSRalph Campbell 		 */
852f931551bSRalph Campbell 		ndesc = qib_count_sge(ss, len);
853f931551bSRalph Campbell 		if (ndesc >= ppd->sdma_descq_cnt)
854f931551bSRalph Campbell 			ndesc = 0;
855f931551bSRalph Campbell 	} else
856f931551bSRalph Campbell 		ndesc = 1;
857f931551bSRalph Campbell 	if (ndesc) {
858f931551bSRalph Campbell 		phdr = &dev->pio_hdrs[tx->hdr_inx];
859f931551bSRalph Campbell 		phdr->pbc[0] = cpu_to_le32(plen);
860f931551bSRalph Campbell 		phdr->pbc[1] = cpu_to_le32(control);
861f931551bSRalph Campbell 		memcpy(&phdr->hdr, hdr, hdrwords << 2);
862f931551bSRalph Campbell 		tx->txreq.flags |= QIB_SDMA_TXREQ_F_FREEDESC;
863f931551bSRalph Campbell 		tx->txreq.sg_count = ndesc;
864f931551bSRalph Campbell 		tx->txreq.addr = dev->pio_hdrs_phys +
865f931551bSRalph Campbell 			tx->hdr_inx * sizeof(struct qib_pio_header);
866f931551bSRalph Campbell 		tx->hdr_dwords = hdrwords + 2; /* add PBC length */
867f931551bSRalph Campbell 		ret = qib_sdma_verbs_send(ppd, ss, dwords, tx);
868f931551bSRalph Campbell 		goto bail;
869f931551bSRalph Campbell 	}
870f931551bSRalph Campbell 
871f931551bSRalph Campbell 	/* Allocate a buffer and copy the header and payload to it. */
872f931551bSRalph Campbell 	tx->hdr_dwords = plen + 1;
873f931551bSRalph Campbell 	phdr = kmalloc(tx->hdr_dwords << 2, GFP_ATOMIC);
874f931551bSRalph Campbell 	if (!phdr)
875f931551bSRalph Campbell 		goto err_tx;
876f931551bSRalph Campbell 	phdr->pbc[0] = cpu_to_le32(plen);
877f931551bSRalph Campbell 	phdr->pbc[1] = cpu_to_le32(control);
878f931551bSRalph Campbell 	memcpy(&phdr->hdr, hdr, hdrwords << 2);
879f931551bSRalph Campbell 	qib_copy_from_sge((u32 *) &phdr->hdr + hdrwords, ss, len);
880f931551bSRalph Campbell 
881f931551bSRalph Campbell 	tx->txreq.addr = dma_map_single(&dd->pcidev->dev, phdr,
882f931551bSRalph Campbell 					tx->hdr_dwords << 2, DMA_TO_DEVICE);
883f931551bSRalph Campbell 	if (dma_mapping_error(&dd->pcidev->dev, tx->txreq.addr))
884f931551bSRalph Campbell 		goto map_err;
885f931551bSRalph Campbell 	tx->align_buf = phdr;
886f931551bSRalph Campbell 	tx->txreq.flags |= QIB_SDMA_TXREQ_F_FREEBUF;
887f931551bSRalph Campbell 	tx->txreq.sg_count = 1;
888f931551bSRalph Campbell 	ret = qib_sdma_verbs_send(ppd, NULL, 0, tx);
889f931551bSRalph Campbell 	goto unaligned;
890f931551bSRalph Campbell 
891f931551bSRalph Campbell map_err:
892f931551bSRalph Campbell 	kfree(phdr);
893f931551bSRalph Campbell err_tx:
894f931551bSRalph Campbell 	qib_put_txreq(tx);
895f931551bSRalph Campbell 	ret = wait_kmem(dev, qp);
896f931551bSRalph Campbell unaligned:
897f24a6d48SHarish Chegondi 	ibp->rvp.n_unaligned++;
898f931551bSRalph Campbell bail:
899f931551bSRalph Campbell 	return ret;
90048947109SMike Marciniszyn bail_tx:
90148947109SMike Marciniszyn 	ret = PTR_ERR(tx);
90248947109SMike Marciniszyn 	goto bail;
903f931551bSRalph Campbell }
904f931551bSRalph Campbell 
905f931551bSRalph Campbell /*
906f931551bSRalph Campbell  * If we are now in the error state, return zero to flush the
907f931551bSRalph Campbell  * send work request.
908f931551bSRalph Campbell  */
9097c2e11feSDennis Dalessandro static int no_bufs_available(struct rvt_qp *qp)
910f931551bSRalph Campbell {
911ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv = qp->priv;
912f931551bSRalph Campbell 	struct qib_ibdev *dev = to_idev(qp->ibqp.device);
913f931551bSRalph Campbell 	struct qib_devdata *dd;
914f931551bSRalph Campbell 	unsigned long flags;
915f931551bSRalph Campbell 	int ret = 0;
916f931551bSRalph Campbell 
917f931551bSRalph Campbell 	/*
918f931551bSRalph Campbell 	 * Note that as soon as want_buffer() is called and
919f931551bSRalph Campbell 	 * possibly before it returns, qib_ib_piobufavail()
920f931551bSRalph Campbell 	 * could be called. Therefore, put QP on the I/O wait list before
921f931551bSRalph Campbell 	 * enabling the PIO avail interrupt.
922f931551bSRalph Campbell 	 */
923f931551bSRalph Campbell 	spin_lock_irqsave(&qp->s_lock, flags);
924db3ef0ebSHarish Chegondi 	if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
925cd18201fSHarish Chegondi 		spin_lock(&dev->rdi.pending_lock);
926ffc26907SDennis Dalessandro 		if (list_empty(&priv->iowait)) {
927f931551bSRalph Campbell 			dev->n_piowait++;
92801ba79d4SHarish Chegondi 			qp->s_flags |= RVT_S_WAIT_PIO;
929ffc26907SDennis Dalessandro 			list_add_tail(&priv->iowait, &dev->piowait);
930f931551bSRalph Campbell 			dd = dd_from_dev(dev);
931f931551bSRalph Campbell 			dd->f_wantpiobuf_intr(dd, 1);
932f931551bSRalph Campbell 		}
933cd18201fSHarish Chegondi 		spin_unlock(&dev->rdi.pending_lock);
93401ba79d4SHarish Chegondi 		qp->s_flags &= ~RVT_S_BUSY;
935f931551bSRalph Campbell 		ret = -EBUSY;
936f931551bSRalph Campbell 	}
937f931551bSRalph Campbell 	spin_unlock_irqrestore(&qp->s_lock, flags);
938f931551bSRalph Campbell 	return ret;
939f931551bSRalph Campbell }
940f931551bSRalph Campbell 
941261a4351SMike Marciniszyn static int qib_verbs_send_pio(struct rvt_qp *qp, struct ib_header *ibhdr,
9427c2e11feSDennis Dalessandro 			      u32 hdrwords, struct rvt_sge_state *ss, u32 len,
943f931551bSRalph Campbell 			      u32 plen, u32 dwords)
944f931551bSRalph Campbell {
945f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
946f931551bSRalph Campbell 	struct qib_pportdata *ppd = dd->pport + qp->port_num - 1;
947f931551bSRalph Campbell 	u32 *hdr = (u32 *) ibhdr;
948f931551bSRalph Campbell 	u32 __iomem *piobuf_orig;
949f931551bSRalph Campbell 	u32 __iomem *piobuf;
950f931551bSRalph Campbell 	u64 pbc;
951f931551bSRalph Campbell 	unsigned long flags;
952f931551bSRalph Campbell 	unsigned flush_wc;
953f931551bSRalph Campbell 	u32 control;
954f931551bSRalph Campbell 	u32 pbufn;
955f931551bSRalph Campbell 
956f931551bSRalph Campbell 	control = dd->f_setpbc_control(ppd, plen, qp->s_srate,
957f931551bSRalph Campbell 		be16_to_cpu(ibhdr->lrh[0]) >> 12);
958f931551bSRalph Campbell 	pbc = ((u64) control << 32) | plen;
959f931551bSRalph Campbell 	piobuf = dd->f_getsendbuf(ppd, pbc, &pbufn);
960f931551bSRalph Campbell 	if (unlikely(piobuf == NULL))
961f931551bSRalph Campbell 		return no_bufs_available(qp);
962f931551bSRalph Campbell 
963f931551bSRalph Campbell 	/*
964f931551bSRalph Campbell 	 * Write the pbc.
965f931551bSRalph Campbell 	 * We have to flush after the PBC for correctness on some cpus
966f931551bSRalph Campbell 	 * or WC buffer can be written out of order.
967f931551bSRalph Campbell 	 */
968f931551bSRalph Campbell 	writeq(pbc, piobuf);
969f931551bSRalph Campbell 	piobuf_orig = piobuf;
970f931551bSRalph Campbell 	piobuf += 2;
971f931551bSRalph Campbell 
972f931551bSRalph Campbell 	flush_wc = dd->flags & QIB_PIO_FLUSH_WC;
973f931551bSRalph Campbell 	if (len == 0) {
974f931551bSRalph Campbell 		/*
975f931551bSRalph Campbell 		 * If there is just the header portion, must flush before
976f931551bSRalph Campbell 		 * writing last word of header for correctness, and after
977f931551bSRalph Campbell 		 * the last header word (trigger word).
978f931551bSRalph Campbell 		 */
979f931551bSRalph Campbell 		if (flush_wc) {
980f931551bSRalph Campbell 			qib_flush_wc();
981f931551bSRalph Campbell 			qib_pio_copy(piobuf, hdr, hdrwords - 1);
982f931551bSRalph Campbell 			qib_flush_wc();
983f931551bSRalph Campbell 			__raw_writel(hdr[hdrwords - 1], piobuf + hdrwords - 1);
984f931551bSRalph Campbell 			qib_flush_wc();
985f931551bSRalph Campbell 		} else
986f931551bSRalph Campbell 			qib_pio_copy(piobuf, hdr, hdrwords);
987f931551bSRalph Campbell 		goto done;
988f931551bSRalph Campbell 	}
989f931551bSRalph Campbell 
990f931551bSRalph Campbell 	if (flush_wc)
991f931551bSRalph Campbell 		qib_flush_wc();
992f931551bSRalph Campbell 	qib_pio_copy(piobuf, hdr, hdrwords);
993f931551bSRalph Campbell 	piobuf += hdrwords;
994f931551bSRalph Campbell 
995f931551bSRalph Campbell 	/* The common case is aligned and contained in one segment. */
996f931551bSRalph Campbell 	if (likely(ss->num_sge == 1 && len <= ss->sge.length &&
997f931551bSRalph Campbell 		   !((unsigned long)ss->sge.vaddr & (sizeof(u32) - 1)))) {
998f931551bSRalph Campbell 		u32 *addr = (u32 *) ss->sge.vaddr;
999f931551bSRalph Campbell 
1000f931551bSRalph Campbell 		/* Update address before sending packet. */
10013fc4a090SBrian Welty 		rvt_update_sge(ss, len, false);
1002f931551bSRalph Campbell 		if (flush_wc) {
1003f931551bSRalph Campbell 			qib_pio_copy(piobuf, addr, dwords - 1);
1004f931551bSRalph Campbell 			/* must flush early everything before trigger word */
1005f931551bSRalph Campbell 			qib_flush_wc();
1006f931551bSRalph Campbell 			__raw_writel(addr[dwords - 1], piobuf + dwords - 1);
1007f931551bSRalph Campbell 			/* be sure trigger word is written */
1008f931551bSRalph Campbell 			qib_flush_wc();
1009f931551bSRalph Campbell 		} else
1010f931551bSRalph Campbell 			qib_pio_copy(piobuf, addr, dwords);
1011f931551bSRalph Campbell 		goto done;
1012f931551bSRalph Campbell 	}
1013f931551bSRalph Campbell 	copy_io(piobuf, ss, len, flush_wc);
1014f931551bSRalph Campbell done:
1015f931551bSRalph Campbell 	if (dd->flags & QIB_USE_SPCL_TRIG) {
1016f931551bSRalph Campbell 		u32 spcl_off = (pbufn >= dd->piobcnt2k) ? 2047 : 1023;
1017da12c1f6SMike Marciniszyn 
1018f931551bSRalph Campbell 		qib_flush_wc();
1019f931551bSRalph Campbell 		__raw_writel(0xaebecede, piobuf_orig + spcl_off);
1020f931551bSRalph Campbell 	}
1021f931551bSRalph Campbell 	qib_sendbuf_done(dd, pbufn);
1022f931551bSRalph Campbell 	if (qp->s_rdma_mr) {
10237c2e11feSDennis Dalessandro 		rvt_put_mr(qp->s_rdma_mr);
1024f931551bSRalph Campbell 		qp->s_rdma_mr = NULL;
1025f931551bSRalph Campbell 	}
1026f931551bSRalph Campbell 	if (qp->s_wqe) {
1027f931551bSRalph Campbell 		spin_lock_irqsave(&qp->s_lock, flags);
1028f931551bSRalph Campbell 		qib_send_complete(qp, qp->s_wqe, IB_WC_SUCCESS);
1029f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
1030f931551bSRalph Campbell 	} else if (qp->ibqp.qp_type == IB_QPT_RC) {
1031f931551bSRalph Campbell 		spin_lock_irqsave(&qp->s_lock, flags);
1032f931551bSRalph Campbell 		qib_rc_send_complete(qp, ibhdr);
1033f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
1034f931551bSRalph Campbell 	}
1035f931551bSRalph Campbell 	return 0;
1036f931551bSRalph Campbell }
1037f931551bSRalph Campbell 
1038f931551bSRalph Campbell /**
1039f931551bSRalph Campbell  * qib_verbs_send - send a packet
1040f931551bSRalph Campbell  * @qp: the QP to send on
1041f931551bSRalph Campbell  * @hdr: the packet header
1042f931551bSRalph Campbell  * @hdrwords: the number of 32-bit words in the header
1043f931551bSRalph Campbell  * @ss: the SGE to send
1044f931551bSRalph Campbell  * @len: the length of the packet in bytes
1045f931551bSRalph Campbell  *
1046f931551bSRalph Campbell  * Return zero if packet is sent or queued OK.
104701ba79d4SHarish Chegondi  * Return non-zero and clear qp->s_flags RVT_S_BUSY otherwise.
1048f931551bSRalph Campbell  */
1049261a4351SMike Marciniszyn int qib_verbs_send(struct rvt_qp *qp, struct ib_header *hdr,
10507c2e11feSDennis Dalessandro 		   u32 hdrwords, struct rvt_sge_state *ss, u32 len)
1051f931551bSRalph Campbell {
1052f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
1053f931551bSRalph Campbell 	u32 plen;
1054f931551bSRalph Campbell 	int ret;
1055f931551bSRalph Campbell 	u32 dwords = (len + 3) >> 2;
1056f931551bSRalph Campbell 
1057f931551bSRalph Campbell 	/*
1058f931551bSRalph Campbell 	 * Calculate the send buffer trigger address.
1059f931551bSRalph Campbell 	 * The +1 counts for the pbc control dword following the pbc length.
1060f931551bSRalph Campbell 	 */
1061f931551bSRalph Campbell 	plen = hdrwords + dwords + 1;
1062f931551bSRalph Campbell 
1063f931551bSRalph Campbell 	/*
1064f931551bSRalph Campbell 	 * VL15 packets (IB_QPT_SMI) will always use PIO, so we
1065f931551bSRalph Campbell 	 * can defer SDMA restart until link goes ACTIVE without
1066f931551bSRalph Campbell 	 * worrying about just how we got there.
1067f931551bSRalph Campbell 	 */
1068f931551bSRalph Campbell 	if (qp->ibqp.qp_type == IB_QPT_SMI ||
1069f931551bSRalph Campbell 	    !(dd->flags & QIB_HAS_SEND_DMA))
1070f931551bSRalph Campbell 		ret = qib_verbs_send_pio(qp, hdr, hdrwords, ss, len,
1071f931551bSRalph Campbell 					 plen, dwords);
1072f931551bSRalph Campbell 	else
1073f931551bSRalph Campbell 		ret = qib_verbs_send_dma(qp, hdr, hdrwords, ss, len,
1074f931551bSRalph Campbell 					 plen, dwords);
1075f931551bSRalph Campbell 
1076f931551bSRalph Campbell 	return ret;
1077f931551bSRalph Campbell }
1078f931551bSRalph Campbell 
1079f931551bSRalph Campbell int qib_snapshot_counters(struct qib_pportdata *ppd, u64 *swords,
1080f931551bSRalph Campbell 			  u64 *rwords, u64 *spkts, u64 *rpkts,
1081f931551bSRalph Campbell 			  u64 *xmit_wait)
1082f931551bSRalph Campbell {
1083f931551bSRalph Campbell 	int ret;
1084f931551bSRalph Campbell 	struct qib_devdata *dd = ppd->dd;
1085f931551bSRalph Campbell 
1086f931551bSRalph Campbell 	if (!(dd->flags & QIB_PRESENT)) {
1087f931551bSRalph Campbell 		/* no hardware, freeze, etc. */
1088f931551bSRalph Campbell 		ret = -EINVAL;
1089f931551bSRalph Campbell 		goto bail;
1090f931551bSRalph Campbell 	}
1091f931551bSRalph Campbell 	*swords = dd->f_portcntr(ppd, QIBPORTCNTR_WORDSEND);
1092f931551bSRalph Campbell 	*rwords = dd->f_portcntr(ppd, QIBPORTCNTR_WORDRCV);
1093f931551bSRalph Campbell 	*spkts = dd->f_portcntr(ppd, QIBPORTCNTR_PKTSEND);
1094f931551bSRalph Campbell 	*rpkts = dd->f_portcntr(ppd, QIBPORTCNTR_PKTRCV);
1095f931551bSRalph Campbell 	*xmit_wait = dd->f_portcntr(ppd, QIBPORTCNTR_SENDSTALL);
1096f931551bSRalph Campbell 
1097f931551bSRalph Campbell 	ret = 0;
1098f931551bSRalph Campbell 
1099f931551bSRalph Campbell bail:
1100f931551bSRalph Campbell 	return ret;
1101f931551bSRalph Campbell }
1102f931551bSRalph Campbell 
1103f931551bSRalph Campbell /**
1104f931551bSRalph Campbell  * qib_get_counters - get various chip counters
1105f931551bSRalph Campbell  * @dd: the qlogic_ib device
1106f931551bSRalph Campbell  * @cntrs: counters are placed here
1107f931551bSRalph Campbell  *
1108f931551bSRalph Campbell  * Return the counters needed by recv_pma_get_portcounters().
1109f931551bSRalph Campbell  */
1110f931551bSRalph Campbell int qib_get_counters(struct qib_pportdata *ppd,
1111f931551bSRalph Campbell 		     struct qib_verbs_counters *cntrs)
1112f931551bSRalph Campbell {
1113f931551bSRalph Campbell 	int ret;
1114f931551bSRalph Campbell 
1115f931551bSRalph Campbell 	if (!(ppd->dd->flags & QIB_PRESENT)) {
1116f931551bSRalph Campbell 		/* no hardware, freeze, etc. */
1117f931551bSRalph Campbell 		ret = -EINVAL;
1118f931551bSRalph Campbell 		goto bail;
1119f931551bSRalph Campbell 	}
1120f931551bSRalph Campbell 	cntrs->symbol_error_counter =
1121f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBSYMBOLERR);
1122f931551bSRalph Campbell 	cntrs->link_error_recovery_counter =
1123f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBLINKERRRECOV);
1124f931551bSRalph Campbell 	/*
1125f931551bSRalph Campbell 	 * The link downed counter counts when the other side downs the
1126f931551bSRalph Campbell 	 * connection.  We add in the number of times we downed the link
1127f931551bSRalph Campbell 	 * due to local link integrity errors to compensate.
1128f931551bSRalph Campbell 	 */
1129f931551bSRalph Campbell 	cntrs->link_downed_counter =
1130f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBLINKDOWN);
1131f931551bSRalph Campbell 	cntrs->port_rcv_errors =
1132f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXDROPPKT) +
1133f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RCVOVFL) +
1134f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERR_RLEN) +
1135f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_INVALIDRLEN) +
1136f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRLINK) +
1137f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRICRC) +
1138f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRVCRC) +
1139f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRLPCRC) +
1140f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_BADFORMAT);
1141f931551bSRalph Campbell 	cntrs->port_rcv_errors +=
1142f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXLOCALPHYERR);
1143f931551bSRalph Campbell 	cntrs->port_rcv_errors +=
1144f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXVLERR);
1145f931551bSRalph Campbell 	cntrs->port_rcv_remphys_errors =
1146f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RCVEBP);
1147f931551bSRalph Campbell 	cntrs->port_xmit_discards =
1148f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_UNSUPVL);
1149f931551bSRalph Campbell 	cntrs->port_xmit_data = ppd->dd->f_portcntr(ppd,
1150f931551bSRalph Campbell 			QIBPORTCNTR_WORDSEND);
1151f931551bSRalph Campbell 	cntrs->port_rcv_data = ppd->dd->f_portcntr(ppd,
1152f931551bSRalph Campbell 			QIBPORTCNTR_WORDRCV);
1153f931551bSRalph Campbell 	cntrs->port_xmit_packets = ppd->dd->f_portcntr(ppd,
1154f931551bSRalph Campbell 			QIBPORTCNTR_PKTSEND);
1155f931551bSRalph Campbell 	cntrs->port_rcv_packets = ppd->dd->f_portcntr(ppd,
1156f931551bSRalph Campbell 			QIBPORTCNTR_PKTRCV);
1157f931551bSRalph Campbell 	cntrs->local_link_integrity_errors =
1158f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_LLI);
1159f931551bSRalph Campbell 	cntrs->excessive_buffer_overrun_errors =
1160f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_EXCESSBUFOVFL);
1161f931551bSRalph Campbell 	cntrs->vl15_dropped =
1162f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_VL15PKTDROP);
1163f931551bSRalph Campbell 
1164f931551bSRalph Campbell 	ret = 0;
1165f931551bSRalph Campbell 
1166f931551bSRalph Campbell bail:
1167f931551bSRalph Campbell 	return ret;
1168f931551bSRalph Campbell }
1169f931551bSRalph Campbell 
1170f931551bSRalph Campbell /**
1171f931551bSRalph Campbell  * qib_ib_piobufavail - callback when a PIO buffer is available
1172f931551bSRalph Campbell  * @dd: the device pointer
1173f931551bSRalph Campbell  *
1174f931551bSRalph Campbell  * This is called from qib_intr() at interrupt level when a PIO buffer is
1175f931551bSRalph Campbell  * available after qib_verbs_send() returned an error that no buffers were
1176f931551bSRalph Campbell  * available. Disable the interrupt if there are no more QPs waiting.
1177f931551bSRalph Campbell  */
1178f931551bSRalph Campbell void qib_ib_piobufavail(struct qib_devdata *dd)
1179f931551bSRalph Campbell {
1180f931551bSRalph Campbell 	struct qib_ibdev *dev = &dd->verbs_dev;
1181f931551bSRalph Campbell 	struct list_head *list;
11827c2e11feSDennis Dalessandro 	struct rvt_qp *qps[5];
11837c2e11feSDennis Dalessandro 	struct rvt_qp *qp;
1184f931551bSRalph Campbell 	unsigned long flags;
1185f931551bSRalph Campbell 	unsigned i, n;
1186ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv;
1187f931551bSRalph Campbell 
1188f931551bSRalph Campbell 	list = &dev->piowait;
1189f931551bSRalph Campbell 	n = 0;
1190f931551bSRalph Campbell 
1191f931551bSRalph Campbell 	/*
1192f931551bSRalph Campbell 	 * Note: checking that the piowait list is empty and clearing
1193f931551bSRalph Campbell 	 * the buffer available interrupt needs to be atomic or we
1194f931551bSRalph Campbell 	 * could end up with QPs on the wait list with the interrupt
1195f931551bSRalph Campbell 	 * disabled.
1196f931551bSRalph Campbell 	 */
1197cd18201fSHarish Chegondi 	spin_lock_irqsave(&dev->rdi.pending_lock, flags);
1198f931551bSRalph Campbell 	while (!list_empty(list)) {
1199f931551bSRalph Campbell 		if (n == ARRAY_SIZE(qps))
1200f931551bSRalph Campbell 			goto full;
1201ffc26907SDennis Dalessandro 		priv = list_entry(list->next, struct qib_qp_priv, iowait);
1202ffc26907SDennis Dalessandro 		qp = priv->owner;
1203ffc26907SDennis Dalessandro 		list_del_init(&priv->iowait);
1204238b1862SSebastian Sanchez 		rvt_get_qp(qp);
1205f931551bSRalph Campbell 		qps[n++] = qp;
1206f931551bSRalph Campbell 	}
1207f931551bSRalph Campbell 	dd->f_wantpiobuf_intr(dd, 0);
1208f931551bSRalph Campbell full:
1209cd18201fSHarish Chegondi 	spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
1210f931551bSRalph Campbell 
1211f931551bSRalph Campbell 	for (i = 0; i < n; i++) {
1212f931551bSRalph Campbell 		qp = qps[i];
1213f931551bSRalph Campbell 
1214f931551bSRalph Campbell 		spin_lock_irqsave(&qp->s_lock, flags);
121501ba79d4SHarish Chegondi 		if (qp->s_flags & RVT_S_WAIT_PIO) {
121601ba79d4SHarish Chegondi 			qp->s_flags &= ~RVT_S_WAIT_PIO;
1217f931551bSRalph Campbell 			qib_schedule_send(qp);
1218f931551bSRalph Campbell 		}
1219f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
1220f931551bSRalph Campbell 
1221f931551bSRalph Campbell 		/* Notify qib_destroy_qp() if it is waiting. */
1222238b1862SSebastian Sanchez 		rvt_put_qp(qp);
1223f931551bSRalph Campbell 	}
1224f931551bSRalph Campbell }
1225f931551bSRalph Campbell 
1226530a5d8eSHarish Chegondi static int qib_query_port(struct rvt_dev_info *rdi, u8 port_num,
1227f931551bSRalph Campbell 			  struct ib_port_attr *props)
1228f931551bSRalph Campbell {
1229530a5d8eSHarish Chegondi 	struct qib_ibdev *ibdev = container_of(rdi, struct qib_ibdev, rdi);
1230530a5d8eSHarish Chegondi 	struct qib_devdata *dd = dd_from_dev(ibdev);
1231530a5d8eSHarish Chegondi 	struct qib_pportdata *ppd = &dd->pport[port_num - 1];
1232f931551bSRalph Campbell 	enum ib_mtu mtu;
1233f931551bSRalph Campbell 	u16 lid = ppd->lid;
1234f931551bSRalph Campbell 
1235c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
1236f931551bSRalph Campbell 	props->lid = lid ? lid : be16_to_cpu(IB_LID_PERMISSIVE);
1237f931551bSRalph Campbell 	props->lmc = ppd->lmc;
1238f931551bSRalph Campbell 	props->state = dd->f_iblink_state(ppd->lastibcstat);
1239f931551bSRalph Campbell 	props->phys_state = dd->f_ibphys_portstate(ppd->lastibcstat);
1240f931551bSRalph Campbell 	props->gid_tbl_len = QIB_GUIDS_PER_PORT;
1241f931551bSRalph Campbell 	props->active_width = ppd->link_width_active;
1242f931551bSRalph Campbell 	/* See rate_show() */
1243f931551bSRalph Campbell 	props->active_speed = ppd->link_speed_active;
1244f931551bSRalph Campbell 	props->max_vl_num = qib_num_vls(ppd->vls_supported);
1245f931551bSRalph Campbell 
1246f931551bSRalph Campbell 	props->max_mtu = qib_ibmtu ? qib_ibmtu : IB_MTU_4096;
1247f931551bSRalph Campbell 	switch (ppd->ibmtu) {
1248f931551bSRalph Campbell 	case 4096:
1249f931551bSRalph Campbell 		mtu = IB_MTU_4096;
1250f931551bSRalph Campbell 		break;
1251f931551bSRalph Campbell 	case 2048:
1252f931551bSRalph Campbell 		mtu = IB_MTU_2048;
1253f931551bSRalph Campbell 		break;
1254f931551bSRalph Campbell 	case 1024:
1255f931551bSRalph Campbell 		mtu = IB_MTU_1024;
1256f931551bSRalph Campbell 		break;
1257f931551bSRalph Campbell 	case 512:
1258f931551bSRalph Campbell 		mtu = IB_MTU_512;
1259f931551bSRalph Campbell 		break;
1260f931551bSRalph Campbell 	case 256:
1261f931551bSRalph Campbell 		mtu = IB_MTU_256;
1262f931551bSRalph Campbell 		break;
1263f931551bSRalph Campbell 	default:
1264f931551bSRalph Campbell 		mtu = IB_MTU_2048;
1265f931551bSRalph Campbell 	}
1266f931551bSRalph Campbell 	props->active_mtu = mtu;
1267f931551bSRalph Campbell 
1268f931551bSRalph Campbell 	return 0;
1269f931551bSRalph Campbell }
1270f931551bSRalph Campbell 
1271f931551bSRalph Campbell static int qib_modify_device(struct ib_device *device,
1272f931551bSRalph Campbell 			     int device_modify_mask,
1273f931551bSRalph Campbell 			     struct ib_device_modify *device_modify)
1274f931551bSRalph Campbell {
1275f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_ibdev(device);
1276f931551bSRalph Campbell 	unsigned i;
1277f931551bSRalph Campbell 	int ret;
1278f931551bSRalph Campbell 
1279f931551bSRalph Campbell 	if (device_modify_mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
1280f931551bSRalph Campbell 				   IB_DEVICE_MODIFY_NODE_DESC)) {
1281f931551bSRalph Campbell 		ret = -EOPNOTSUPP;
1282f931551bSRalph Campbell 		goto bail;
1283f931551bSRalph Campbell 	}
1284f931551bSRalph Campbell 
1285f931551bSRalph Campbell 	if (device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC) {
1286bd99fdeaSYuval Shaia 		memcpy(device->node_desc, device_modify->node_desc,
1287bd99fdeaSYuval Shaia 		       IB_DEVICE_NODE_DESC_MAX);
1288f931551bSRalph Campbell 		for (i = 0; i < dd->num_pports; i++) {
1289f931551bSRalph Campbell 			struct qib_ibport *ibp = &dd->pport[i].ibport_data;
1290f931551bSRalph Campbell 
1291f931551bSRalph Campbell 			qib_node_desc_chg(ibp);
1292f931551bSRalph Campbell 		}
1293f931551bSRalph Campbell 	}
1294f931551bSRalph Campbell 
1295f931551bSRalph Campbell 	if (device_modify_mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID) {
1296f931551bSRalph Campbell 		ib_qib_sys_image_guid =
1297f931551bSRalph Campbell 			cpu_to_be64(device_modify->sys_image_guid);
1298f931551bSRalph Campbell 		for (i = 0; i < dd->num_pports; i++) {
1299f931551bSRalph Campbell 			struct qib_ibport *ibp = &dd->pport[i].ibport_data;
1300f931551bSRalph Campbell 
1301f931551bSRalph Campbell 			qib_sys_guid_chg(ibp);
1302f931551bSRalph Campbell 		}
1303f931551bSRalph Campbell 	}
1304f931551bSRalph Campbell 
1305f931551bSRalph Campbell 	ret = 0;
1306f931551bSRalph Campbell 
1307f931551bSRalph Campbell bail:
1308f931551bSRalph Campbell 	return ret;
1309f931551bSRalph Campbell }
1310f931551bSRalph Campbell 
131120f333b6SHarish Chegondi static int qib_shut_down_port(struct rvt_dev_info *rdi, u8 port_num)
1312f931551bSRalph Campbell {
1313530a5d8eSHarish Chegondi 	struct qib_ibdev *ibdev = container_of(rdi, struct qib_ibdev, rdi);
1314530a5d8eSHarish Chegondi 	struct qib_devdata *dd = dd_from_dev(ibdev);
1315530a5d8eSHarish Chegondi 	struct qib_pportdata *ppd = &dd->pport[port_num - 1];
1316f931551bSRalph Campbell 
1317f931551bSRalph Campbell 	qib_set_linkstate(ppd, QIB_IB_LINKDOWN);
1318530a5d8eSHarish Chegondi 
1319f931551bSRalph Campbell 	return 0;
1320f931551bSRalph Campbell }
1321f931551bSRalph Campbell 
132223667546SDennis Dalessandro static int qib_get_guid_be(struct rvt_dev_info *rdi, struct rvt_ibport *rvp,
132323667546SDennis Dalessandro 			   int guid_index, __be64 *guid)
1324f931551bSRalph Campbell {
132523667546SDennis Dalessandro 	struct qib_ibport *ibp = container_of(rvp, struct qib_ibport, rvp);
1326f931551bSRalph Campbell 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1327f931551bSRalph Campbell 
132823667546SDennis Dalessandro 	if (guid_index == 0)
132923667546SDennis Dalessandro 		*guid = ppd->guid;
133023667546SDennis Dalessandro 	else if (guid_index < QIB_GUIDS_PER_PORT)
133123667546SDennis Dalessandro 		*guid = ibp->guids[guid_index - 1];
1332f931551bSRalph Campbell 	else
133323667546SDennis Dalessandro 		return -EINVAL;
1334f931551bSRalph Campbell 
133523667546SDennis Dalessandro 	return 0;
1336f931551bSRalph Campbell }
1337f931551bSRalph Campbell 
133890898850SDasaratharaman Chandramouli int qib_check_ah(struct ib_device *ibdev, struct rdma_ah_attr *ah_attr)
1339f931551bSRalph Campbell {
1340d8966fcdSDasaratharaman Chandramouli 	if (rdma_ah_get_sl(ah_attr) > 15)
1341f931551bSRalph Campbell 		return -EINVAL;
1342f931551bSRalph Campbell 
134313c19222SDon Hiatt 	if (rdma_ah_get_dlid(ah_attr) == 0)
134413c19222SDon Hiatt 		return -EINVAL;
134513c19222SDon Hiatt 	if (rdma_ah_get_dlid(ah_attr) >=
134613c19222SDon Hiatt 		be16_to_cpu(IB_MULTICAST_LID_BASE) &&
134713c19222SDon Hiatt 	    rdma_ah_get_dlid(ah_attr) !=
134813c19222SDon Hiatt 		be16_to_cpu(IB_LID_PERMISSIVE) &&
134913c19222SDon Hiatt 	    !(rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH))
135013c19222SDon Hiatt 		return -EINVAL;
135113c19222SDon Hiatt 
135296ab1ac1SDennis Dalessandro 	return 0;
1353f931551bSRalph Campbell }
1354f931551bSRalph Campbell 
13555418a5abSHarish Chegondi static void qib_notify_new_ah(struct ib_device *ibdev,
135690898850SDasaratharaman Chandramouli 			      struct rdma_ah_attr *ah_attr,
13575418a5abSHarish Chegondi 			      struct rvt_ah *ah)
13585418a5abSHarish Chegondi {
13595418a5abSHarish Chegondi 	struct qib_ibport *ibp;
13605418a5abSHarish Chegondi 	struct qib_pportdata *ppd;
13615418a5abSHarish Chegondi 
13625418a5abSHarish Chegondi 	/*
13635418a5abSHarish Chegondi 	 * Do not trust reading anything from rvt_ah at this point as it is not
13645418a5abSHarish Chegondi 	 * done being setup. We can however modify things which we need to set.
13655418a5abSHarish Chegondi 	 */
13665418a5abSHarish Chegondi 
1367d8966fcdSDasaratharaman Chandramouli 	ibp = to_iport(ibdev, rdma_ah_get_port_num(ah_attr));
13685418a5abSHarish Chegondi 	ppd = ppd_from_ibp(ibp);
1369d8966fcdSDasaratharaman Chandramouli 	ah->vl = ibp->sl_to_vl[rdma_ah_get_sl(&ah->attr)];
13705418a5abSHarish Chegondi 	ah->log_pmtu = ilog2(ppd->ibmtu);
13715418a5abSHarish Chegondi }
13725418a5abSHarish Chegondi 
13731fb9fed6SMike Marciniszyn struct ib_ah *qib_create_qp0_ah(struct qib_ibport *ibp, u16 dlid)
13741fb9fed6SMike Marciniszyn {
137590898850SDasaratharaman Chandramouli 	struct rdma_ah_attr attr;
13761fb9fed6SMike Marciniszyn 	struct ib_ah *ah = ERR_PTR(-EINVAL);
13777c2e11feSDennis Dalessandro 	struct rvt_qp *qp0;
1378d8966fcdSDasaratharaman Chandramouli 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
137944c58487SDasaratharaman Chandramouli 	struct qib_devdata *dd = dd_from_ppd(ppd);
1380d8966fcdSDasaratharaman Chandramouli 	u8 port_num = ppd->port;
13811fb9fed6SMike Marciniszyn 
1382041af0bbSMike Marciniszyn 	memset(&attr, 0, sizeof(attr));
138344c58487SDasaratharaman Chandramouli 	attr.type = rdma_ah_find_type(&dd->verbs_dev.rdi.ibdev, port_num);
1384d8966fcdSDasaratharaman Chandramouli 	rdma_ah_set_dlid(&attr, dlid);
1385d8966fcdSDasaratharaman Chandramouli 	rdma_ah_set_port_num(&attr, port_num);
13861fb9fed6SMike Marciniszyn 	rcu_read_lock();
1387f24a6d48SHarish Chegondi 	qp0 = rcu_dereference(ibp->rvp.qp[0]);
13881fb9fed6SMike Marciniszyn 	if (qp0)
13890a18cfe4SDasaratharaman Chandramouli 		ah = rdma_create_ah(qp0->ibqp.pd, &attr);
13901fb9fed6SMike Marciniszyn 	rcu_read_unlock();
13911fb9fed6SMike Marciniszyn 	return ah;
13921fb9fed6SMike Marciniszyn }
13931fb9fed6SMike Marciniszyn 
1394f931551bSRalph Campbell /**
1395f931551bSRalph Campbell  * qib_get_npkeys - return the size of the PKEY table for context 0
1396f931551bSRalph Campbell  * @dd: the qlogic_ib device
1397f931551bSRalph Campbell  */
1398f931551bSRalph Campbell unsigned qib_get_npkeys(struct qib_devdata *dd)
1399f931551bSRalph Campbell {
1400f931551bSRalph Campbell 	return ARRAY_SIZE(dd->rcd[0]->pkeys);
1401f931551bSRalph Campbell }
1402f931551bSRalph Campbell 
1403f931551bSRalph Campbell /*
1404f931551bSRalph Campbell  * Return the indexed PKEY from the port PKEY table.
1405f931551bSRalph Campbell  * No need to validate rcd[ctxt]; the port is setup if we are here.
1406f931551bSRalph Campbell  */
1407f931551bSRalph Campbell unsigned qib_get_pkey(struct qib_ibport *ibp, unsigned index)
1408f931551bSRalph Campbell {
1409f931551bSRalph Campbell 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1410f931551bSRalph Campbell 	struct qib_devdata *dd = ppd->dd;
1411f931551bSRalph Campbell 	unsigned ctxt = ppd->hw_pidx;
1412f931551bSRalph Campbell 	unsigned ret;
1413f931551bSRalph Campbell 
1414f931551bSRalph Campbell 	/* dd->rcd null if mini_init or some init failures */
1415f931551bSRalph Campbell 	if (!dd->rcd || index >= ARRAY_SIZE(dd->rcd[ctxt]->pkeys))
1416f931551bSRalph Campbell 		ret = 0;
1417f931551bSRalph Campbell 	else
1418f931551bSRalph Campbell 		ret = dd->rcd[ctxt]->pkeys[index];
1419f931551bSRalph Campbell 
1420f931551bSRalph Campbell 	return ret;
1421f931551bSRalph Campbell }
1422f931551bSRalph Campbell 
1423f931551bSRalph Campbell static void init_ibport(struct qib_pportdata *ppd)
1424f931551bSRalph Campbell {
1425f931551bSRalph Campbell 	struct qib_verbs_counters cntrs;
1426f931551bSRalph Campbell 	struct qib_ibport *ibp = &ppd->ibport_data;
1427f931551bSRalph Campbell 
1428f24a6d48SHarish Chegondi 	spin_lock_init(&ibp->rvp.lock);
1429f931551bSRalph Campbell 	/* Set the prefix to the default value (see ch. 4.1.1) */
1430f24a6d48SHarish Chegondi 	ibp->rvp.gid_prefix = IB_DEFAULT_GID_PREFIX;
1431f24a6d48SHarish Chegondi 	ibp->rvp.sm_lid = be16_to_cpu(IB_LID_PERMISSIVE);
1432f24a6d48SHarish Chegondi 	ibp->rvp.port_cap_flags = IB_PORT_SYS_IMAGE_GUID_SUP |
1433f931551bSRalph Campbell 		IB_PORT_CLIENT_REG_SUP | IB_PORT_SL_MAP_SUP |
1434f931551bSRalph Campbell 		IB_PORT_TRAP_SUP | IB_PORT_AUTO_MIGR_SUP |
1435f931551bSRalph Campbell 		IB_PORT_DR_NOTICE_SUP | IB_PORT_CAP_MASK_NOTICE_SUP |
1436f931551bSRalph Campbell 		IB_PORT_OTHER_LOCAL_CHANGES_SUP;
1437f931551bSRalph Campbell 	if (ppd->dd->flags & QIB_HAS_LINK_LATENCY)
1438f24a6d48SHarish Chegondi 		ibp->rvp.port_cap_flags |= IB_PORT_LINK_LATENCY_SUP;
1439f24a6d48SHarish Chegondi 	ibp->rvp.pma_counter_select[0] = IB_PMA_PORT_XMIT_DATA;
1440f24a6d48SHarish Chegondi 	ibp->rvp.pma_counter_select[1] = IB_PMA_PORT_RCV_DATA;
1441f24a6d48SHarish Chegondi 	ibp->rvp.pma_counter_select[2] = IB_PMA_PORT_XMIT_PKTS;
1442f24a6d48SHarish Chegondi 	ibp->rvp.pma_counter_select[3] = IB_PMA_PORT_RCV_PKTS;
1443f24a6d48SHarish Chegondi 	ibp->rvp.pma_counter_select[4] = IB_PMA_PORT_XMIT_WAIT;
1444f931551bSRalph Campbell 
1445f931551bSRalph Campbell 	/* Snapshot current HW counters to "clear" them. */
1446f931551bSRalph Campbell 	qib_get_counters(ppd, &cntrs);
1447f931551bSRalph Campbell 	ibp->z_symbol_error_counter = cntrs.symbol_error_counter;
1448f931551bSRalph Campbell 	ibp->z_link_error_recovery_counter =
1449f931551bSRalph Campbell 		cntrs.link_error_recovery_counter;
1450f931551bSRalph Campbell 	ibp->z_link_downed_counter = cntrs.link_downed_counter;
1451f931551bSRalph Campbell 	ibp->z_port_rcv_errors = cntrs.port_rcv_errors;
1452f931551bSRalph Campbell 	ibp->z_port_rcv_remphys_errors = cntrs.port_rcv_remphys_errors;
1453f931551bSRalph Campbell 	ibp->z_port_xmit_discards = cntrs.port_xmit_discards;
1454f931551bSRalph Campbell 	ibp->z_port_xmit_data = cntrs.port_xmit_data;
1455f931551bSRalph Campbell 	ibp->z_port_rcv_data = cntrs.port_rcv_data;
1456f931551bSRalph Campbell 	ibp->z_port_xmit_packets = cntrs.port_xmit_packets;
1457f931551bSRalph Campbell 	ibp->z_port_rcv_packets = cntrs.port_rcv_packets;
1458f931551bSRalph Campbell 	ibp->z_local_link_integrity_errors =
1459f931551bSRalph Campbell 		cntrs.local_link_integrity_errors;
1460f931551bSRalph Campbell 	ibp->z_excessive_buffer_overrun_errors =
1461f931551bSRalph Campbell 		cntrs.excessive_buffer_overrun_errors;
1462f931551bSRalph Campbell 	ibp->z_vl15_dropped = cntrs.vl15_dropped;
1463f24a6d48SHarish Chegondi 	RCU_INIT_POINTER(ibp->rvp.qp[0], NULL);
1464f24a6d48SHarish Chegondi 	RCU_INIT_POINTER(ibp->rvp.qp[1], NULL);
1465f931551bSRalph Campbell }
1466f931551bSRalph Campbell 
1467f931551bSRalph Campbell /**
14680aeddea2SHarish Chegondi  * qib_fill_device_attr - Fill in rvt dev info device attributes.
14690aeddea2SHarish Chegondi  * @dd: the device data structure
14700aeddea2SHarish Chegondi  */
14710aeddea2SHarish Chegondi static void qib_fill_device_attr(struct qib_devdata *dd)
14720aeddea2SHarish Chegondi {
14730aeddea2SHarish Chegondi 	struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
14740aeddea2SHarish Chegondi 
14750aeddea2SHarish Chegondi 	memset(&rdi->dparms.props, 0, sizeof(rdi->dparms.props));
14760aeddea2SHarish Chegondi 
14770aeddea2SHarish Chegondi 	rdi->dparms.props.max_pd = ib_qib_max_pds;
14780aeddea2SHarish Chegondi 	rdi->dparms.props.max_ah = ib_qib_max_ahs;
14790aeddea2SHarish Chegondi 	rdi->dparms.props.device_cap_flags = IB_DEVICE_BAD_PKEY_CNTR |
14800aeddea2SHarish Chegondi 		IB_DEVICE_BAD_QKEY_CNTR | IB_DEVICE_SHUTDOWN_PORT |
14810aeddea2SHarish Chegondi 		IB_DEVICE_SYS_IMAGE_GUID | IB_DEVICE_RC_RNR_NAK_GEN |
14820aeddea2SHarish Chegondi 		IB_DEVICE_PORT_ACTIVE_EVENT | IB_DEVICE_SRQ_RESIZE;
14830aeddea2SHarish Chegondi 	rdi->dparms.props.page_size_cap = PAGE_SIZE;
14840aeddea2SHarish Chegondi 	rdi->dparms.props.vendor_id =
14850aeddea2SHarish Chegondi 		QIB_SRC_OUI_1 << 16 | QIB_SRC_OUI_2 << 8 | QIB_SRC_OUI_3;
14860aeddea2SHarish Chegondi 	rdi->dparms.props.vendor_part_id = dd->deviceid;
14870aeddea2SHarish Chegondi 	rdi->dparms.props.hw_ver = dd->minrev;
14880aeddea2SHarish Chegondi 	rdi->dparms.props.sys_image_guid = ib_qib_sys_image_guid;
14890aeddea2SHarish Chegondi 	rdi->dparms.props.max_mr_size = ~0ULL;
14900aeddea2SHarish Chegondi 	rdi->dparms.props.max_qp = ib_qib_max_qps;
14910aeddea2SHarish Chegondi 	rdi->dparms.props.max_qp_wr = ib_qib_max_qp_wrs;
1492*33023fb8SSteve Wise 	rdi->dparms.props.max_send_sge = ib_qib_max_sges;
1493*33023fb8SSteve Wise 	rdi->dparms.props.max_recv_sge = ib_qib_max_sges;
14940aeddea2SHarish Chegondi 	rdi->dparms.props.max_sge_rd = ib_qib_max_sges;
14950aeddea2SHarish Chegondi 	rdi->dparms.props.max_cq = ib_qib_max_cqs;
14960aeddea2SHarish Chegondi 	rdi->dparms.props.max_cqe = ib_qib_max_cqes;
14970aeddea2SHarish Chegondi 	rdi->dparms.props.max_ah = ib_qib_max_ahs;
14980aeddea2SHarish Chegondi 	rdi->dparms.props.max_mr = rdi->lkey_table.max;
14990aeddea2SHarish Chegondi 	rdi->dparms.props.max_fmr = rdi->lkey_table.max;
15000aeddea2SHarish Chegondi 	rdi->dparms.props.max_map_per_fmr = 32767;
15010aeddea2SHarish Chegondi 	rdi->dparms.props.max_qp_rd_atom = QIB_MAX_RDMA_ATOMIC;
15020aeddea2SHarish Chegondi 	rdi->dparms.props.max_qp_init_rd_atom = 255;
15030aeddea2SHarish Chegondi 	rdi->dparms.props.max_srq = ib_qib_max_srqs;
15040aeddea2SHarish Chegondi 	rdi->dparms.props.max_srq_wr = ib_qib_max_srq_wrs;
15050aeddea2SHarish Chegondi 	rdi->dparms.props.max_srq_sge = ib_qib_max_srq_sges;
15060aeddea2SHarish Chegondi 	rdi->dparms.props.atomic_cap = IB_ATOMIC_GLOB;
15070aeddea2SHarish Chegondi 	rdi->dparms.props.max_pkeys = qib_get_npkeys(dd);
15080aeddea2SHarish Chegondi 	rdi->dparms.props.max_mcast_grp = ib_qib_max_mcast_grps;
15090aeddea2SHarish Chegondi 	rdi->dparms.props.max_mcast_qp_attach = ib_qib_max_mcast_qp_attached;
15100aeddea2SHarish Chegondi 	rdi->dparms.props.max_total_mcast_qp_attach =
15110aeddea2SHarish Chegondi 					rdi->dparms.props.max_mcast_qp_attach *
15120aeddea2SHarish Chegondi 					rdi->dparms.props.max_mcast_grp;
15139ec4faa3SMike Marciniszyn 	/* post send table */
15149ec4faa3SMike Marciniszyn 	dd->verbs_dev.rdi.post_parms = qib_post_parms;
15150aeddea2SHarish Chegondi }
15160aeddea2SHarish Chegondi 
15170aeddea2SHarish Chegondi /**
1518f931551bSRalph Campbell  * qib_register_ib_device - register our device with the infiniband core
1519f931551bSRalph Campbell  * @dd: the device data structure
1520f931551bSRalph Campbell  * Return the allocated qib_ibdev pointer or NULL on error.
1521f931551bSRalph Campbell  */
1522f931551bSRalph Campbell int qib_register_ib_device(struct qib_devdata *dd)
1523f931551bSRalph Campbell {
1524f931551bSRalph Campbell 	struct qib_ibdev *dev = &dd->verbs_dev;
15252dc05ab5SDennis Dalessandro 	struct ib_device *ibdev = &dev->rdi.ibdev;
1526f931551bSRalph Campbell 	struct qib_pportdata *ppd = dd->pport;
152776fec3e0SHarish Chegondi 	unsigned i, ctxt;
1528f931551bSRalph Campbell 	int ret;
1529f931551bSRalph Campbell 
1530af061a64SMike Marciniszyn 	get_random_bytes(&dev->qp_rnd, sizeof(dev->qp_rnd));
1531f931551bSRalph Campbell 	for (i = 0; i < dd->num_pports; i++)
1532f931551bSRalph Campbell 		init_ibport(ppd + i);
1533f931551bSRalph Campbell 
1534f931551bSRalph Campbell 	/* Only need to initialize non-zero fields. */
15354037c92fSKees Cook 	timer_setup(&dev->mem_timer, mem_timer, 0);
1536f931551bSRalph Campbell 
1537f931551bSRalph Campbell 	INIT_LIST_HEAD(&dev->piowait);
1538f931551bSRalph Campbell 	INIT_LIST_HEAD(&dev->dmawait);
1539f931551bSRalph Campbell 	INIT_LIST_HEAD(&dev->txwait);
1540f931551bSRalph Campbell 	INIT_LIST_HEAD(&dev->memwait);
1541f931551bSRalph Campbell 	INIT_LIST_HEAD(&dev->txreq_free);
1542f931551bSRalph Campbell 
1543f931551bSRalph Campbell 	if (ppd->sdma_descq_cnt) {
1544f931551bSRalph Campbell 		dev->pio_hdrs = dma_alloc_coherent(&dd->pcidev->dev,
1545f931551bSRalph Campbell 						ppd->sdma_descq_cnt *
1546f931551bSRalph Campbell 						sizeof(struct qib_pio_header),
1547f931551bSRalph Campbell 						&dev->pio_hdrs_phys,
1548f931551bSRalph Campbell 						GFP_KERNEL);
1549f931551bSRalph Campbell 		if (!dev->pio_hdrs) {
1550f931551bSRalph Campbell 			ret = -ENOMEM;
1551f931551bSRalph Campbell 			goto err_hdrs;
1552f931551bSRalph Campbell 		}
1553f931551bSRalph Campbell 	}
1554f931551bSRalph Campbell 
1555f931551bSRalph Campbell 	for (i = 0; i < ppd->sdma_descq_cnt; i++) {
1556f931551bSRalph Campbell 		struct qib_verbs_txreq *tx;
1557f931551bSRalph Campbell 
1558041af0bbSMike Marciniszyn 		tx = kzalloc(sizeof(*tx), GFP_KERNEL);
1559f931551bSRalph Campbell 		if (!tx) {
1560f931551bSRalph Campbell 			ret = -ENOMEM;
1561f931551bSRalph Campbell 			goto err_tx;
1562f931551bSRalph Campbell 		}
1563f931551bSRalph Campbell 		tx->hdr_inx = i;
1564f931551bSRalph Campbell 		list_add(&tx->txreq.list, &dev->txreq_free);
1565f931551bSRalph Campbell 	}
1566f931551bSRalph Campbell 
1567f931551bSRalph Campbell 	/*
1568f931551bSRalph Campbell 	 * The system image GUID is supposed to be the same for all
1569f931551bSRalph Campbell 	 * IB HCAs in a single system but since there can be other
1570f931551bSRalph Campbell 	 * device types in the system, we can't be sure this is unique.
1571f931551bSRalph Campbell 	 */
1572f931551bSRalph Campbell 	if (!ib_qib_sys_image_guid)
1573f931551bSRalph Campbell 		ib_qib_sys_image_guid = ppd->guid;
1574f931551bSRalph Campbell 
1575f931551bSRalph Campbell 	ibdev->owner = THIS_MODULE;
1576f931551bSRalph Campbell 	ibdev->node_guid = ppd->guid;
1577f931551bSRalph Campbell 	ibdev->phys_port_cnt = dd->num_pports;
1578989ab358SBart Van Assche 	ibdev->dev.parent = &dd->pcidev->dev;
1579f931551bSRalph Campbell 	ibdev->modify_device = qib_modify_device;
1580f931551bSRalph Campbell 	ibdev->process_mad = qib_process_mad;
1581f931551bSRalph Campbell 
1582f931551bSRalph Campbell 	snprintf(ibdev->node_desc, sizeof(ibdev->node_desc),
1583e2eed58bSVinit Agnihotri 		 "Intel Infiniband HCA %s", init_utsname()->nodename);
1584f931551bSRalph Campbell 
15852dc05ab5SDennis Dalessandro 	/*
15862dc05ab5SDennis Dalessandro 	 * Fill in rvt info object.
15872dc05ab5SDennis Dalessandro 	 */
15882dc05ab5SDennis Dalessandro 	dd->verbs_dev.rdi.driver_f.port_callback = qib_create_port_files;
15896a9df403SDennis Dalessandro 	dd->verbs_dev.rdi.driver_f.get_pci_dev = qib_get_pci_dev;
159096ab1ac1SDennis Dalessandro 	dd->verbs_dev.rdi.driver_f.check_ah = qib_check_ah;
159146a80d62SMike Marciniszyn 	dd->verbs_dev.rdi.driver_f.check_send_wqe = qib_check_send_wqe;
15925418a5abSHarish Chegondi 	dd->verbs_dev.rdi.driver_f.notify_new_ah = qib_notify_new_ah;
159320f333b6SHarish Chegondi 	dd->verbs_dev.rdi.driver_f.alloc_qpn = qib_alloc_qpn;
159420f333b6SHarish Chegondi 	dd->verbs_dev.rdi.driver_f.qp_priv_alloc = qib_qp_priv_alloc;
159520f333b6SHarish Chegondi 	dd->verbs_dev.rdi.driver_f.qp_priv_free = qib_qp_priv_free;
159647c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.driver_f.free_all_qps = qib_free_all_qps;
159720f333b6SHarish Chegondi 	dd->verbs_dev.rdi.driver_f.notify_qp_reset = qib_notify_qp_reset;
1598db3ef0ebSHarish Chegondi 	dd->verbs_dev.rdi.driver_f.do_send = qib_do_send;
1599db3ef0ebSHarish Chegondi 	dd->verbs_dev.rdi.driver_f.schedule_send = qib_schedule_send;
160020f333b6SHarish Chegondi 	dd->verbs_dev.rdi.driver_f.quiesce_qp = qib_quiesce_qp;
160120f333b6SHarish Chegondi 	dd->verbs_dev.rdi.driver_f.stop_send_queue = qib_stop_send_queue;
160220f333b6SHarish Chegondi 	dd->verbs_dev.rdi.driver_f.flush_qp_waiters = qib_flush_qp_waiters;
160320f333b6SHarish Chegondi 	dd->verbs_dev.rdi.driver_f.notify_error_qp = qib_notify_error_qp;
1604b4238e70SVenkata Sandeep Dhanalakota 	dd->verbs_dev.rdi.driver_f.notify_restart_rc = qib_restart_rc;
160520f333b6SHarish Chegondi 	dd->verbs_dev.rdi.driver_f.mtu_to_path_mtu = qib_mtu_to_path_mtu;
160620f333b6SHarish Chegondi 	dd->verbs_dev.rdi.driver_f.mtu_from_qp = qib_mtu_from_qp;
160720f333b6SHarish Chegondi 	dd->verbs_dev.rdi.driver_f.get_pmtu_from_attr = qib_get_pmtu_from_attr;
160846a80d62SMike Marciniszyn 	dd->verbs_dev.rdi.driver_f.schedule_send_no_lock = _qib_schedule_send;
1609530a5d8eSHarish Chegondi 	dd->verbs_dev.rdi.driver_f.query_port_state = qib_query_port;
161020f333b6SHarish Chegondi 	dd->verbs_dev.rdi.driver_f.shut_down_port = qib_shut_down_port;
1611530a5d8eSHarish Chegondi 	dd->verbs_dev.rdi.driver_f.cap_mask_chg = qib_cap_mask_chg;
1612611ac099SDennis Dalessandro 	dd->verbs_dev.rdi.driver_f.notify_create_mad_agent =
1613611ac099SDennis Dalessandro 						qib_notify_create_mad_agent;
1614611ac099SDennis Dalessandro 	dd->verbs_dev.rdi.driver_f.notify_free_mad_agent =
1615611ac099SDennis Dalessandro 						qib_notify_free_mad_agent;
161647c7ea6dSHarish Chegondi 
161770696ea7SHarish Chegondi 	dd->verbs_dev.rdi.dparms.max_rdma_atomic = QIB_MAX_RDMA_ATOMIC;
161823667546SDennis Dalessandro 	dd->verbs_dev.rdi.driver_f.get_guid_be = qib_get_guid_be;
16197c2e11feSDennis Dalessandro 	dd->verbs_dev.rdi.dparms.lkey_table_size = qib_lkey_table_size;
162047c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.dparms.qp_table_size = ib_qib_qp_table_size;
162147c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.dparms.qpn_start = 1;
162247c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.dparms.qpn_res_start = QIB_KD_QP;
162347c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.dparms.qpn_res_end = QIB_KD_QP; /* Reserve one QP */
162447c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.dparms.qpn_inc = 1;
162547c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.dparms.qos_shift = 1;
1626034a3e70SHarish Chegondi 	dd->verbs_dev.rdi.dparms.psn_mask = QIB_PSN_MASK;
162770696ea7SHarish Chegondi 	dd->verbs_dev.rdi.dparms.psn_shift = QIB_PSN_SHIFT;
162870696ea7SHarish Chegondi 	dd->verbs_dev.rdi.dparms.psn_modify_mask = QIB_PSN_MASK;
162976fec3e0SHarish Chegondi 	dd->verbs_dev.rdi.dparms.nports = dd->num_pports;
163076fec3e0SHarish Chegondi 	dd->verbs_dev.rdi.dparms.npkeys = qib_get_npkeys(dd);
16314bb88e5fSHarish Chegondi 	dd->verbs_dev.rdi.dparms.node = dd->assigned_node_id;
1632530a5d8eSHarish Chegondi 	dd->verbs_dev.rdi.dparms.core_cap_flags = RDMA_CORE_PORT_IBA_IB;
1633530a5d8eSHarish Chegondi 	dd->verbs_dev.rdi.dparms.max_mad_size = IB_MGMT_MAD_SIZE;
1634530a5d8eSHarish Chegondi 
16350aeddea2SHarish Chegondi 	qib_fill_device_attr(dd);
16360aeddea2SHarish Chegondi 
163776fec3e0SHarish Chegondi 	ppd = dd->pport;
163876fec3e0SHarish Chegondi 	for (i = 0; i < dd->num_pports; i++, ppd++) {
163976fec3e0SHarish Chegondi 		ctxt = ppd->hw_pidx;
164076fec3e0SHarish Chegondi 		rvt_init_port(&dd->verbs_dev.rdi,
164176fec3e0SHarish Chegondi 			      &ppd->ibport_data.rvp,
164276fec3e0SHarish Chegondi 			      i,
164376fec3e0SHarish Chegondi 			      dd->rcd[ctxt]->pkeys);
164476fec3e0SHarish Chegondi 	}
16452dc05ab5SDennis Dalessandro 
16460ede73bcSMatan Barak 	ret = rvt_register_device(&dd->verbs_dev.rdi, RDMA_DRIVER_QIB);
1647f931551bSRalph Campbell 	if (ret)
16485196aa96SDennis Dalessandro 		goto err_tx;
1649f931551bSRalph Campbell 
1650c9bdad3cSMike Marciniszyn 	ret = qib_verbs_register_sysfs(dd);
1651c9bdad3cSMike Marciniszyn 	if (ret)
1652f931551bSRalph Campbell 		goto err_class;
1653f931551bSRalph Campbell 
16545196aa96SDennis Dalessandro 	return ret;
1655f931551bSRalph Campbell 
1656f931551bSRalph Campbell err_class:
16572dc05ab5SDennis Dalessandro 	rvt_unregister_device(&dd->verbs_dev.rdi);
1658f931551bSRalph Campbell err_tx:
1659f931551bSRalph Campbell 	while (!list_empty(&dev->txreq_free)) {
1660f931551bSRalph Campbell 		struct list_head *l = dev->txreq_free.next;
1661f931551bSRalph Campbell 		struct qib_verbs_txreq *tx;
1662f931551bSRalph Campbell 
1663f931551bSRalph Campbell 		list_del(l);
1664f931551bSRalph Campbell 		tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
1665f931551bSRalph Campbell 		kfree(tx);
1666f931551bSRalph Campbell 	}
1667f931551bSRalph Campbell 	if (ppd->sdma_descq_cnt)
1668f931551bSRalph Campbell 		dma_free_coherent(&dd->pcidev->dev,
1669f931551bSRalph Campbell 				  ppd->sdma_descq_cnt *
1670f931551bSRalph Campbell 					sizeof(struct qib_pio_header),
1671f931551bSRalph Campbell 				  dev->pio_hdrs, dev->pio_hdrs_phys);
1672f931551bSRalph Campbell err_hdrs:
1673f931551bSRalph Campbell 	qib_dev_err(dd, "cannot register verbs: %d!\n", -ret);
1674f931551bSRalph Campbell 	return ret;
1675f931551bSRalph Campbell }
1676f931551bSRalph Campbell 
1677f931551bSRalph Campbell void qib_unregister_ib_device(struct qib_devdata *dd)
1678f931551bSRalph Campbell {
1679f931551bSRalph Campbell 	struct qib_ibdev *dev = &dd->verbs_dev;
1680f931551bSRalph Campbell 
1681f931551bSRalph Campbell 	qib_verbs_unregister_sysfs(dd);
1682f931551bSRalph Campbell 
16832dc05ab5SDennis Dalessandro 	rvt_unregister_device(&dd->verbs_dev.rdi);
1684f931551bSRalph Campbell 
1685f931551bSRalph Campbell 	if (!list_empty(&dev->piowait))
1686f931551bSRalph Campbell 		qib_dev_err(dd, "piowait list not empty!\n");
1687f931551bSRalph Campbell 	if (!list_empty(&dev->dmawait))
1688f931551bSRalph Campbell 		qib_dev_err(dd, "dmawait list not empty!\n");
1689f931551bSRalph Campbell 	if (!list_empty(&dev->txwait))
1690f931551bSRalph Campbell 		qib_dev_err(dd, "txwait list not empty!\n");
1691f931551bSRalph Campbell 	if (!list_empty(&dev->memwait))
1692f931551bSRalph Campbell 		qib_dev_err(dd, "memwait list not empty!\n");
1693f931551bSRalph Campbell 
1694f931551bSRalph Campbell 	del_timer_sync(&dev->mem_timer);
1695f931551bSRalph Campbell 	while (!list_empty(&dev->txreq_free)) {
1696f931551bSRalph Campbell 		struct list_head *l = dev->txreq_free.next;
1697f931551bSRalph Campbell 		struct qib_verbs_txreq *tx;
1698f931551bSRalph Campbell 
1699f931551bSRalph Campbell 		list_del(l);
1700f931551bSRalph Campbell 		tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
1701f931551bSRalph Campbell 		kfree(tx);
1702f931551bSRalph Campbell 	}
1703f931551bSRalph Campbell 	if (dd->pport->sdma_descq_cnt)
1704f931551bSRalph Campbell 		dma_free_coherent(&dd->pcidev->dev,
1705f931551bSRalph Campbell 				  dd->pport->sdma_descq_cnt *
1706f931551bSRalph Campbell 					sizeof(struct qib_pio_header),
1707f931551bSRalph Campbell 				  dev->pio_hdrs, dev->pio_hdrs_phys);
1708f931551bSRalph Campbell }
1709551ace12SMike Marciniszyn 
171046a80d62SMike Marciniszyn /**
171146a80d62SMike Marciniszyn  * _qib_schedule_send - schedule progress
171246a80d62SMike Marciniszyn  * @qp - the qp
171346a80d62SMike Marciniszyn  *
171446a80d62SMike Marciniszyn  * This schedules progress w/o regard to the s_flags.
171546a80d62SMike Marciniszyn  *
171646a80d62SMike Marciniszyn  * It is only used in post send, which doesn't hold
171746a80d62SMike Marciniszyn  * the s_lock.
1718551ace12SMike Marciniszyn  */
171946a80d62SMike Marciniszyn void _qib_schedule_send(struct rvt_qp *qp)
1720551ace12SMike Marciniszyn {
1721551ace12SMike Marciniszyn 	struct qib_ibport *ibp =
1722551ace12SMike Marciniszyn 		to_iport(qp->ibqp.device, qp->port_num);
1723551ace12SMike Marciniszyn 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
172446a80d62SMike Marciniszyn 	struct qib_qp_priv *priv = qp->priv;
1725551ace12SMike Marciniszyn 
1726ffc26907SDennis Dalessandro 	queue_work(ppd->qib_wq, &priv->s_work);
1727551ace12SMike Marciniszyn }
172846a80d62SMike Marciniszyn 
172946a80d62SMike Marciniszyn /**
173046a80d62SMike Marciniszyn  * qib_schedule_send - schedule progress
173146a80d62SMike Marciniszyn  * @qp - the qp
173246a80d62SMike Marciniszyn  *
173346a80d62SMike Marciniszyn  * This schedules qp progress.  The s_lock
173446a80d62SMike Marciniszyn  * should be held.
173546a80d62SMike Marciniszyn  */
173646a80d62SMike Marciniszyn void qib_schedule_send(struct rvt_qp *qp)
173746a80d62SMike Marciniszyn {
173846a80d62SMike Marciniszyn 	if (qib_send_ok(qp))
173946a80d62SMike Marciniszyn 		_qib_schedule_send(qp);
1740551ace12SMike Marciniszyn }
1741