xref: /openbmc/linux/drivers/infiniband/hw/qib/qib_verbs.c (revision 47c7ea6d8e70510c3b3e311cfc20943cd3fe786a)
1f931551bSRalph Campbell /*
2e2eed58bSVinit Agnihotri  * Copyright (c) 2012, 2013 Intel Corporation.  All rights reserved.
31fb9fed6SMike Marciniszyn  * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
4f931551bSRalph Campbell  * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
5f931551bSRalph Campbell  *
6f931551bSRalph Campbell  * This software is available to you under a choice of one of two
7f931551bSRalph Campbell  * licenses.  You may choose to be licensed under the terms of the GNU
8f931551bSRalph Campbell  * General Public License (GPL) Version 2, available from the file
9f931551bSRalph Campbell  * COPYING in the main directory of this source tree, or the
10f931551bSRalph Campbell  * OpenIB.org BSD license below:
11f931551bSRalph Campbell  *
12f931551bSRalph Campbell  *     Redistribution and use in source and binary forms, with or
13f931551bSRalph Campbell  *     without modification, are permitted provided that the following
14f931551bSRalph Campbell  *     conditions are met:
15f931551bSRalph Campbell  *
16f931551bSRalph Campbell  *      - Redistributions of source code must retain the above
17f931551bSRalph Campbell  *        copyright notice, this list of conditions and the following
18f931551bSRalph Campbell  *        disclaimer.
19f931551bSRalph Campbell  *
20f931551bSRalph Campbell  *      - Redistributions in binary form must reproduce the above
21f931551bSRalph Campbell  *        copyright notice, this list of conditions and the following
22f931551bSRalph Campbell  *        disclaimer in the documentation and/or other materials
23f931551bSRalph Campbell  *        provided with the distribution.
24f931551bSRalph Campbell  *
25f931551bSRalph Campbell  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26f931551bSRalph Campbell  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27f931551bSRalph Campbell  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28f931551bSRalph Campbell  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29f931551bSRalph Campbell  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30f931551bSRalph Campbell  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31f931551bSRalph Campbell  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32f931551bSRalph Campbell  * SOFTWARE.
33f931551bSRalph Campbell  */
34f931551bSRalph Campbell 
35f931551bSRalph Campbell #include <rdma/ib_mad.h>
36f931551bSRalph Campbell #include <rdma/ib_user_verbs.h>
37f931551bSRalph Campbell #include <linux/io.h>
38e4dd23d7SPaul Gortmaker #include <linux/module.h>
39f931551bSRalph Campbell #include <linux/utsname.h>
40f931551bSRalph Campbell #include <linux/rculist.h>
41f931551bSRalph Campbell #include <linux/mm.h>
42af061a64SMike Marciniszyn #include <linux/random.h>
43d6f1c17eSMike Marciniszyn #include <linux/vmalloc.h>
44eb636ac0SDennis Dalessandro #include <rdma/rdma_vt.h>
45f931551bSRalph Campbell 
46f931551bSRalph Campbell #include "qib.h"
47f931551bSRalph Campbell #include "qib_common.h"
48f931551bSRalph Campbell 
49af061a64SMike Marciniszyn static unsigned int ib_qib_qp_table_size = 256;
50f931551bSRalph Campbell module_param_named(qp_table_size, ib_qib_qp_table_size, uint, S_IRUGO);
51f931551bSRalph Campbell MODULE_PARM_DESC(qp_table_size, "QP table size");
52f931551bSRalph Campbell 
537c2e11feSDennis Dalessandro static unsigned int qib_lkey_table_size = 16;
547c2e11feSDennis Dalessandro module_param_named(lkey_table_size, qib_lkey_table_size, uint,
55f931551bSRalph Campbell 		   S_IRUGO);
56f931551bSRalph Campbell MODULE_PARM_DESC(lkey_table_size,
57f931551bSRalph Campbell 		 "LKEY table size in bits (2^n, 1 <= n <= 23)");
58f931551bSRalph Campbell 
59f931551bSRalph Campbell static unsigned int ib_qib_max_pds = 0xFFFF;
60f931551bSRalph Campbell module_param_named(max_pds, ib_qib_max_pds, uint, S_IRUGO);
61f931551bSRalph Campbell MODULE_PARM_DESC(max_pds,
62f931551bSRalph Campbell 		 "Maximum number of protection domains to support");
63f931551bSRalph Campbell 
64f931551bSRalph Campbell static unsigned int ib_qib_max_ahs = 0xFFFF;
65f931551bSRalph Campbell module_param_named(max_ahs, ib_qib_max_ahs, uint, S_IRUGO);
66f931551bSRalph Campbell MODULE_PARM_DESC(max_ahs, "Maximum number of address handles to support");
67f931551bSRalph Campbell 
68f931551bSRalph Campbell unsigned int ib_qib_max_cqes = 0x2FFFF;
69f931551bSRalph Campbell module_param_named(max_cqes, ib_qib_max_cqes, uint, S_IRUGO);
70f931551bSRalph Campbell MODULE_PARM_DESC(max_cqes,
71f931551bSRalph Campbell 		 "Maximum number of completion queue entries to support");
72f931551bSRalph Campbell 
73f931551bSRalph Campbell unsigned int ib_qib_max_cqs = 0x1FFFF;
74f931551bSRalph Campbell module_param_named(max_cqs, ib_qib_max_cqs, uint, S_IRUGO);
75f931551bSRalph Campbell MODULE_PARM_DESC(max_cqs, "Maximum number of completion queues to support");
76f931551bSRalph Campbell 
77f931551bSRalph Campbell unsigned int ib_qib_max_qp_wrs = 0x3FFF;
78f931551bSRalph Campbell module_param_named(max_qp_wrs, ib_qib_max_qp_wrs, uint, S_IRUGO);
79f931551bSRalph Campbell MODULE_PARM_DESC(max_qp_wrs, "Maximum number of QP WRs to support");
80f931551bSRalph Campbell 
81f931551bSRalph Campbell unsigned int ib_qib_max_qps = 16384;
82f931551bSRalph Campbell module_param_named(max_qps, ib_qib_max_qps, uint, S_IRUGO);
83f931551bSRalph Campbell MODULE_PARM_DESC(max_qps, "Maximum number of QPs to support");
84f931551bSRalph Campbell 
85f931551bSRalph Campbell unsigned int ib_qib_max_sges = 0x60;
86f931551bSRalph Campbell module_param_named(max_sges, ib_qib_max_sges, uint, S_IRUGO);
87f931551bSRalph Campbell MODULE_PARM_DESC(max_sges, "Maximum number of SGEs to support");
88f931551bSRalph Campbell 
89f931551bSRalph Campbell unsigned int ib_qib_max_mcast_grps = 16384;
90f931551bSRalph Campbell module_param_named(max_mcast_grps, ib_qib_max_mcast_grps, uint, S_IRUGO);
91f931551bSRalph Campbell MODULE_PARM_DESC(max_mcast_grps,
92f931551bSRalph Campbell 		 "Maximum number of multicast groups to support");
93f931551bSRalph Campbell 
94f931551bSRalph Campbell unsigned int ib_qib_max_mcast_qp_attached = 16;
95f931551bSRalph Campbell module_param_named(max_mcast_qp_attached, ib_qib_max_mcast_qp_attached,
96f931551bSRalph Campbell 		   uint, S_IRUGO);
97f931551bSRalph Campbell MODULE_PARM_DESC(max_mcast_qp_attached,
98f931551bSRalph Campbell 		 "Maximum number of attached QPs to support");
99f931551bSRalph Campbell 
100f931551bSRalph Campbell unsigned int ib_qib_max_srqs = 1024;
101f931551bSRalph Campbell module_param_named(max_srqs, ib_qib_max_srqs, uint, S_IRUGO);
102f931551bSRalph Campbell MODULE_PARM_DESC(max_srqs, "Maximum number of SRQs to support");
103f931551bSRalph Campbell 
104f931551bSRalph Campbell unsigned int ib_qib_max_srq_sges = 128;
105f931551bSRalph Campbell module_param_named(max_srq_sges, ib_qib_max_srq_sges, uint, S_IRUGO);
106f931551bSRalph Campbell MODULE_PARM_DESC(max_srq_sges, "Maximum number of SRQ SGEs to support");
107f931551bSRalph Campbell 
108f931551bSRalph Campbell unsigned int ib_qib_max_srq_wrs = 0x1FFFF;
109f931551bSRalph Campbell module_param_named(max_srq_wrs, ib_qib_max_srq_wrs, uint, S_IRUGO);
110f931551bSRalph Campbell MODULE_PARM_DESC(max_srq_wrs, "Maximum number of SRQ WRs support");
111f931551bSRalph Campbell 
112f931551bSRalph Campbell static unsigned int ib_qib_disable_sma;
113f931551bSRalph Campbell module_param_named(disable_sma, ib_qib_disable_sma, uint, S_IWUSR | S_IRUGO);
114f931551bSRalph Campbell MODULE_PARM_DESC(disable_sma, "Disable the SMA");
115f931551bSRalph Campbell 
116f931551bSRalph Campbell /*
117f931551bSRalph Campbell  * Note that it is OK to post send work requests in the SQE and ERR
118f931551bSRalph Campbell  * states; qib_do_send() will process them and generate error
119f931551bSRalph Campbell  * completions as per IB 1.2 C10-96.
120f931551bSRalph Campbell  */
121f931551bSRalph Campbell const int ib_qib_state_ops[IB_QPS_ERR + 1] = {
122f931551bSRalph Campbell 	[IB_QPS_RESET] = 0,
123f931551bSRalph Campbell 	[IB_QPS_INIT] = QIB_POST_RECV_OK,
124f931551bSRalph Campbell 	[IB_QPS_RTR] = QIB_POST_RECV_OK | QIB_PROCESS_RECV_OK,
125f931551bSRalph Campbell 	[IB_QPS_RTS] = QIB_POST_RECV_OK | QIB_PROCESS_RECV_OK |
126f931551bSRalph Campbell 	    QIB_POST_SEND_OK | QIB_PROCESS_SEND_OK |
127f931551bSRalph Campbell 	    QIB_PROCESS_NEXT_SEND_OK,
128f931551bSRalph Campbell 	[IB_QPS_SQD] = QIB_POST_RECV_OK | QIB_PROCESS_RECV_OK |
129f931551bSRalph Campbell 	    QIB_POST_SEND_OK | QIB_PROCESS_SEND_OK,
130f931551bSRalph Campbell 	[IB_QPS_SQE] = QIB_POST_RECV_OK | QIB_PROCESS_RECV_OK |
131f931551bSRalph Campbell 	    QIB_POST_SEND_OK | QIB_FLUSH_SEND,
132f931551bSRalph Campbell 	[IB_QPS_ERR] = QIB_POST_RECV_OK | QIB_FLUSH_RECV |
133f931551bSRalph Campbell 	    QIB_POST_SEND_OK | QIB_FLUSH_SEND,
134f931551bSRalph Campbell };
135f931551bSRalph Campbell 
136f931551bSRalph Campbell /*
137f931551bSRalph Campbell  * Translate ib_wr_opcode into ib_wc_opcode.
138f931551bSRalph Campbell  */
139f931551bSRalph Campbell const enum ib_wc_opcode ib_qib_wc_opcode[] = {
140f931551bSRalph Campbell 	[IB_WR_RDMA_WRITE] = IB_WC_RDMA_WRITE,
141f931551bSRalph Campbell 	[IB_WR_RDMA_WRITE_WITH_IMM] = IB_WC_RDMA_WRITE,
142f931551bSRalph Campbell 	[IB_WR_SEND] = IB_WC_SEND,
143f931551bSRalph Campbell 	[IB_WR_SEND_WITH_IMM] = IB_WC_SEND,
144f931551bSRalph Campbell 	[IB_WR_RDMA_READ] = IB_WC_RDMA_READ,
145f931551bSRalph Campbell 	[IB_WR_ATOMIC_CMP_AND_SWP] = IB_WC_COMP_SWAP,
146f931551bSRalph Campbell 	[IB_WR_ATOMIC_FETCH_AND_ADD] = IB_WC_FETCH_ADD
147f931551bSRalph Campbell };
148f931551bSRalph Campbell 
149f931551bSRalph Campbell /*
150f931551bSRalph Campbell  * System image GUID.
151f931551bSRalph Campbell  */
152f931551bSRalph Campbell __be64 ib_qib_sys_image_guid;
153f931551bSRalph Campbell 
154f931551bSRalph Campbell /**
155f931551bSRalph Campbell  * qib_copy_sge - copy data to SGE memory
156f931551bSRalph Campbell  * @ss: the SGE state
157f931551bSRalph Campbell  * @data: the data to copy
158f931551bSRalph Campbell  * @length: the length of the data
159f931551bSRalph Campbell  */
1607c2e11feSDennis Dalessandro void qib_copy_sge(struct rvt_sge_state *ss, void *data, u32 length, int release)
161f931551bSRalph Campbell {
1627c2e11feSDennis Dalessandro 	struct rvt_sge *sge = &ss->sge;
163f931551bSRalph Campbell 
164f931551bSRalph Campbell 	while (length) {
165f931551bSRalph Campbell 		u32 len = sge->length;
166f931551bSRalph Campbell 
167f931551bSRalph Campbell 		if (len > length)
168f931551bSRalph Campbell 			len = length;
169f931551bSRalph Campbell 		if (len > sge->sge_length)
170f931551bSRalph Campbell 			len = sge->sge_length;
171f931551bSRalph Campbell 		BUG_ON(len == 0);
172f931551bSRalph Campbell 		memcpy(sge->vaddr, data, len);
173f931551bSRalph Campbell 		sge->vaddr += len;
174f931551bSRalph Campbell 		sge->length -= len;
175f931551bSRalph Campbell 		sge->sge_length -= len;
176f931551bSRalph Campbell 		if (sge->sge_length == 0) {
177f931551bSRalph Campbell 			if (release)
1787c2e11feSDennis Dalessandro 				rvt_put_mr(sge->mr);
179f931551bSRalph Campbell 			if (--ss->num_sge)
180f931551bSRalph Campbell 				*sge = *ss->sg_list++;
181f931551bSRalph Campbell 		} else if (sge->length == 0 && sge->mr->lkey) {
1827c2e11feSDennis Dalessandro 			if (++sge->n >= RVT_SEGSZ) {
183f931551bSRalph Campbell 				if (++sge->m >= sge->mr->mapsz)
184f931551bSRalph Campbell 					break;
185f931551bSRalph Campbell 				sge->n = 0;
186f931551bSRalph Campbell 			}
187f931551bSRalph Campbell 			sge->vaddr =
188f931551bSRalph Campbell 				sge->mr->map[sge->m]->segs[sge->n].vaddr;
189f931551bSRalph Campbell 			sge->length =
190f931551bSRalph Campbell 				sge->mr->map[sge->m]->segs[sge->n].length;
191f931551bSRalph Campbell 		}
192f931551bSRalph Campbell 		data += len;
193f931551bSRalph Campbell 		length -= len;
194f931551bSRalph Campbell 	}
195f931551bSRalph Campbell }
196f931551bSRalph Campbell 
197f931551bSRalph Campbell /**
198f931551bSRalph Campbell  * qib_skip_sge - skip over SGE memory - XXX almost dup of prev func
199f931551bSRalph Campbell  * @ss: the SGE state
200f931551bSRalph Campbell  * @length: the number of bytes to skip
201f931551bSRalph Campbell  */
2027c2e11feSDennis Dalessandro void qib_skip_sge(struct rvt_sge_state *ss, u32 length, int release)
203f931551bSRalph Campbell {
2047c2e11feSDennis Dalessandro 	struct rvt_sge *sge = &ss->sge;
205f931551bSRalph Campbell 
206f931551bSRalph Campbell 	while (length) {
207f931551bSRalph Campbell 		u32 len = sge->length;
208f931551bSRalph Campbell 
209f931551bSRalph Campbell 		if (len > length)
210f931551bSRalph Campbell 			len = length;
211f931551bSRalph Campbell 		if (len > sge->sge_length)
212f931551bSRalph Campbell 			len = sge->sge_length;
213f931551bSRalph Campbell 		BUG_ON(len == 0);
214f931551bSRalph Campbell 		sge->vaddr += len;
215f931551bSRalph Campbell 		sge->length -= len;
216f931551bSRalph Campbell 		sge->sge_length -= len;
217f931551bSRalph Campbell 		if (sge->sge_length == 0) {
218f931551bSRalph Campbell 			if (release)
2197c2e11feSDennis Dalessandro 				rvt_put_mr(sge->mr);
220f931551bSRalph Campbell 			if (--ss->num_sge)
221f931551bSRalph Campbell 				*sge = *ss->sg_list++;
222f931551bSRalph Campbell 		} else if (sge->length == 0 && sge->mr->lkey) {
2237c2e11feSDennis Dalessandro 			if (++sge->n >= RVT_SEGSZ) {
224f931551bSRalph Campbell 				if (++sge->m >= sge->mr->mapsz)
225f931551bSRalph Campbell 					break;
226f931551bSRalph Campbell 				sge->n = 0;
227f931551bSRalph Campbell 			}
228f931551bSRalph Campbell 			sge->vaddr =
229f931551bSRalph Campbell 				sge->mr->map[sge->m]->segs[sge->n].vaddr;
230f931551bSRalph Campbell 			sge->length =
231f931551bSRalph Campbell 				sge->mr->map[sge->m]->segs[sge->n].length;
232f931551bSRalph Campbell 		}
233f931551bSRalph Campbell 		length -= len;
234f931551bSRalph Campbell 	}
235f931551bSRalph Campbell }
236f931551bSRalph Campbell 
237f931551bSRalph Campbell /*
238f931551bSRalph Campbell  * Count the number of DMA descriptors needed to send length bytes of data.
239f931551bSRalph Campbell  * Don't modify the qib_sge_state to get the count.
240f931551bSRalph Campbell  * Return zero if any of the segments is not aligned.
241f931551bSRalph Campbell  */
2427c2e11feSDennis Dalessandro static u32 qib_count_sge(struct rvt_sge_state *ss, u32 length)
243f931551bSRalph Campbell {
2447c2e11feSDennis Dalessandro 	struct rvt_sge *sg_list = ss->sg_list;
2457c2e11feSDennis Dalessandro 	struct rvt_sge sge = ss->sge;
246f931551bSRalph Campbell 	u8 num_sge = ss->num_sge;
247f931551bSRalph Campbell 	u32 ndesc = 1;  /* count the header */
248f931551bSRalph Campbell 
249f931551bSRalph Campbell 	while (length) {
250f931551bSRalph Campbell 		u32 len = sge.length;
251f931551bSRalph Campbell 
252f931551bSRalph Campbell 		if (len > length)
253f931551bSRalph Campbell 			len = length;
254f931551bSRalph Campbell 		if (len > sge.sge_length)
255f931551bSRalph Campbell 			len = sge.sge_length;
256f931551bSRalph Campbell 		BUG_ON(len == 0);
257f931551bSRalph Campbell 		if (((long) sge.vaddr & (sizeof(u32) - 1)) ||
258f931551bSRalph Campbell 		    (len != length && (len & (sizeof(u32) - 1)))) {
259f931551bSRalph Campbell 			ndesc = 0;
260f931551bSRalph Campbell 			break;
261f931551bSRalph Campbell 		}
262f931551bSRalph Campbell 		ndesc++;
263f931551bSRalph Campbell 		sge.vaddr += len;
264f931551bSRalph Campbell 		sge.length -= len;
265f931551bSRalph Campbell 		sge.sge_length -= len;
266f931551bSRalph Campbell 		if (sge.sge_length == 0) {
267f931551bSRalph Campbell 			if (--num_sge)
268f931551bSRalph Campbell 				sge = *sg_list++;
269f931551bSRalph Campbell 		} else if (sge.length == 0 && sge.mr->lkey) {
2707c2e11feSDennis Dalessandro 			if (++sge.n >= RVT_SEGSZ) {
271f931551bSRalph Campbell 				if (++sge.m >= sge.mr->mapsz)
272f931551bSRalph Campbell 					break;
273f931551bSRalph Campbell 				sge.n = 0;
274f931551bSRalph Campbell 			}
275f931551bSRalph Campbell 			sge.vaddr =
276f931551bSRalph Campbell 				sge.mr->map[sge.m]->segs[sge.n].vaddr;
277f931551bSRalph Campbell 			sge.length =
278f931551bSRalph Campbell 				sge.mr->map[sge.m]->segs[sge.n].length;
279f931551bSRalph Campbell 		}
280f931551bSRalph Campbell 		length -= len;
281f931551bSRalph Campbell 	}
282f931551bSRalph Campbell 	return ndesc;
283f931551bSRalph Campbell }
284f931551bSRalph Campbell 
285f931551bSRalph Campbell /*
286f931551bSRalph Campbell  * Copy from the SGEs to the data buffer.
287f931551bSRalph Campbell  */
2887c2e11feSDennis Dalessandro static void qib_copy_from_sge(void *data, struct rvt_sge_state *ss, u32 length)
289f931551bSRalph Campbell {
2907c2e11feSDennis Dalessandro 	struct rvt_sge *sge = &ss->sge;
291f931551bSRalph Campbell 
292f931551bSRalph Campbell 	while (length) {
293f931551bSRalph Campbell 		u32 len = sge->length;
294f931551bSRalph Campbell 
295f931551bSRalph Campbell 		if (len > length)
296f931551bSRalph Campbell 			len = length;
297f931551bSRalph Campbell 		if (len > sge->sge_length)
298f931551bSRalph Campbell 			len = sge->sge_length;
299f931551bSRalph Campbell 		BUG_ON(len == 0);
300f931551bSRalph Campbell 		memcpy(data, sge->vaddr, len);
301f931551bSRalph Campbell 		sge->vaddr += len;
302f931551bSRalph Campbell 		sge->length -= len;
303f931551bSRalph Campbell 		sge->sge_length -= len;
304f931551bSRalph Campbell 		if (sge->sge_length == 0) {
305f931551bSRalph Campbell 			if (--ss->num_sge)
306f931551bSRalph Campbell 				*sge = *ss->sg_list++;
307f931551bSRalph Campbell 		} else if (sge->length == 0 && sge->mr->lkey) {
3087c2e11feSDennis Dalessandro 			if (++sge->n >= RVT_SEGSZ) {
309f931551bSRalph Campbell 				if (++sge->m >= sge->mr->mapsz)
310f931551bSRalph Campbell 					break;
311f931551bSRalph Campbell 				sge->n = 0;
312f931551bSRalph Campbell 			}
313f931551bSRalph Campbell 			sge->vaddr =
314f931551bSRalph Campbell 				sge->mr->map[sge->m]->segs[sge->n].vaddr;
315f931551bSRalph Campbell 			sge->length =
316f931551bSRalph Campbell 				sge->mr->map[sge->m]->segs[sge->n].length;
317f931551bSRalph Campbell 		}
318f931551bSRalph Campbell 		data += len;
319f931551bSRalph Campbell 		length -= len;
320f931551bSRalph Campbell 	}
321f931551bSRalph Campbell }
322f931551bSRalph Campbell 
323f931551bSRalph Campbell /**
324f931551bSRalph Campbell  * qib_post_one_send - post one RC, UC, or UD send work request
325f931551bSRalph Campbell  * @qp: the QP to post on
326f931551bSRalph Campbell  * @wr: the work request to send
327f931551bSRalph Campbell  */
3287c2e11feSDennis Dalessandro static int qib_post_one_send(struct rvt_qp *qp, struct ib_send_wr *wr,
329551ace12SMike Marciniszyn 			     int *scheduled)
330f931551bSRalph Campbell {
3317c2e11feSDennis Dalessandro 	struct rvt_swqe *wqe;
332f931551bSRalph Campbell 	u32 next;
333f931551bSRalph Campbell 	int i;
334f931551bSRalph Campbell 	int j;
335f931551bSRalph Campbell 	int acc;
336f931551bSRalph Campbell 	int ret;
337f931551bSRalph Campbell 	unsigned long flags;
3387c2e11feSDennis Dalessandro 	struct rvt_lkey_table *rkt;
339f44728d6SDennis Dalessandro 	struct rvt_pd *pd;
340967bcfc0SMike Marciniszyn 	int avoid_schedule = 0;
341f931551bSRalph Campbell 
342f931551bSRalph Campbell 	spin_lock_irqsave(&qp->s_lock, flags);
343f931551bSRalph Campbell 
344f931551bSRalph Campbell 	/* Check that state is OK to post send. */
345f931551bSRalph Campbell 	if (unlikely(!(ib_qib_state_ops[qp->state] & QIB_POST_SEND_OK)))
346f931551bSRalph Campbell 		goto bail_inval;
347f931551bSRalph Campbell 
348f931551bSRalph Campbell 	/* IB spec says that num_sge == 0 is OK. */
349f931551bSRalph Campbell 	if (wr->num_sge > qp->s_max_sge)
350f931551bSRalph Campbell 		goto bail_inval;
351f931551bSRalph Campbell 
352f931551bSRalph Campbell 	/*
353f931551bSRalph Campbell 	 * Don't allow RDMA reads or atomic operations on UC or
354f931551bSRalph Campbell 	 * undefined operations.
355f931551bSRalph Campbell 	 * Make sure buffer is large enough to hold the result for atomics.
356f931551bSRalph Campbell 	 */
3577c2e11feSDennis Dalessandro 	if (qp->ibqp.qp_type == IB_QPT_UC) {
358f931551bSRalph Campbell 		if ((unsigned) wr->opcode >= IB_WR_RDMA_READ)
359f931551bSRalph Campbell 			goto bail_inval;
360f931551bSRalph Campbell 	} else if (qp->ibqp.qp_type != IB_QPT_RC) {
361f931551bSRalph Campbell 		/* Check IB_QPT_SMI, IB_QPT_GSI, IB_QPT_UD opcode */
362f931551bSRalph Campbell 		if (wr->opcode != IB_WR_SEND &&
363f931551bSRalph Campbell 		    wr->opcode != IB_WR_SEND_WITH_IMM)
364f931551bSRalph Campbell 			goto bail_inval;
365f931551bSRalph Campbell 		/* Check UD destination address PD */
366e622f2f4SChristoph Hellwig 		if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
367f931551bSRalph Campbell 			goto bail_inval;
368f931551bSRalph Campbell 	} else if ((unsigned) wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD)
369f931551bSRalph Campbell 		goto bail_inval;
370f931551bSRalph Campbell 	else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
371f931551bSRalph Campbell 		   (wr->num_sge == 0 ||
372f931551bSRalph Campbell 		    wr->sg_list[0].length < sizeof(u64) ||
373f931551bSRalph Campbell 		    wr->sg_list[0].addr & (sizeof(u64) - 1)))
374f931551bSRalph Campbell 		goto bail_inval;
375f931551bSRalph Campbell 	else if (wr->opcode >= IB_WR_RDMA_READ && !qp->s_max_rd_atomic)
376f931551bSRalph Campbell 		goto bail_inval;
377f931551bSRalph Campbell 
378f931551bSRalph Campbell 	next = qp->s_head + 1;
379f931551bSRalph Campbell 	if (next >= qp->s_size)
380f931551bSRalph Campbell 		next = 0;
381f931551bSRalph Campbell 	if (next == qp->s_last) {
382f931551bSRalph Campbell 		ret = -ENOMEM;
383f931551bSRalph Campbell 		goto bail;
384f931551bSRalph Campbell 	}
385f931551bSRalph Campbell 
3867c2e11feSDennis Dalessandro 	rkt = &to_idev(qp->ibqp.device)->rdi.lkey_table;
387f44728d6SDennis Dalessandro 	pd = ibpd_to_rvtpd(qp->ibqp.pd);
388f931551bSRalph Campbell 	wqe = get_swqe_ptr(qp, qp->s_head);
389e622f2f4SChristoph Hellwig 
390e622f2f4SChristoph Hellwig 	if (qp->ibqp.qp_type != IB_QPT_UC &&
391e622f2f4SChristoph Hellwig 	    qp->ibqp.qp_type != IB_QPT_RC)
392e622f2f4SChristoph Hellwig 		memcpy(&wqe->ud_wr, ud_wr(wr), sizeof(wqe->ud_wr));
39338071a46SSagi Grimberg 	else if (wr->opcode == IB_WR_REG_MR)
39438071a46SSagi Grimberg 		memcpy(&wqe->reg_wr, reg_wr(wr),
39538071a46SSagi Grimberg 			sizeof(wqe->reg_wr));
396e622f2f4SChristoph Hellwig 	else if (wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
397e622f2f4SChristoph Hellwig 		 wr->opcode == IB_WR_RDMA_WRITE ||
398e622f2f4SChristoph Hellwig 		 wr->opcode == IB_WR_RDMA_READ)
399e622f2f4SChristoph Hellwig 		memcpy(&wqe->rdma_wr, rdma_wr(wr), sizeof(wqe->rdma_wr));
400e622f2f4SChristoph Hellwig 	else if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
401e622f2f4SChristoph Hellwig 		 wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
402e622f2f4SChristoph Hellwig 		memcpy(&wqe->atomic_wr, atomic_wr(wr), sizeof(wqe->atomic_wr));
403e622f2f4SChristoph Hellwig 	else
404e622f2f4SChristoph Hellwig 		memcpy(&wqe->wr, wr, sizeof(wqe->wr));
405e622f2f4SChristoph Hellwig 
406f931551bSRalph Campbell 	wqe->length = 0;
407f931551bSRalph Campbell 	j = 0;
408f931551bSRalph Campbell 	if (wr->num_sge) {
409f931551bSRalph Campbell 		acc = wr->opcode >= IB_WR_RDMA_READ ?
410f931551bSRalph Campbell 			IB_ACCESS_LOCAL_WRITE : 0;
411f931551bSRalph Campbell 		for (i = 0; i < wr->num_sge; i++) {
412f931551bSRalph Campbell 			u32 length = wr->sg_list[i].length;
413f931551bSRalph Campbell 			int ok;
414f931551bSRalph Campbell 
415f931551bSRalph Campbell 			if (length == 0)
416f931551bSRalph Campbell 				continue;
4177c2e11feSDennis Dalessandro 			ok = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j],
418f931551bSRalph Campbell 					 &wr->sg_list[i], acc);
419f931551bSRalph Campbell 			if (!ok)
420f931551bSRalph Campbell 				goto bail_inval_free;
421f931551bSRalph Campbell 			wqe->length += length;
422f931551bSRalph Campbell 			j++;
423f931551bSRalph Campbell 		}
424f931551bSRalph Campbell 		wqe->wr.num_sge = j;
425f931551bSRalph Campbell 	}
426f931551bSRalph Campbell 	if (qp->ibqp.qp_type == IB_QPT_UC ||
427f931551bSRalph Campbell 	    qp->ibqp.qp_type == IB_QPT_RC) {
428f931551bSRalph Campbell 		if (wqe->length > 0x80000000U)
429f931551bSRalph Campbell 			goto bail_inval_free;
430967bcfc0SMike Marciniszyn 		if (wqe->length <= qp->pmtu)
431967bcfc0SMike Marciniszyn 			avoid_schedule = 1;
432f931551bSRalph Campbell 	} else if (wqe->length > (dd_from_ibdev(qp->ibqp.device)->pport +
433967bcfc0SMike Marciniszyn 				  qp->port_num - 1)->ibmtu) {
434f931551bSRalph Campbell 		goto bail_inval_free;
435967bcfc0SMike Marciniszyn 	} else {
43696ab1ac1SDennis Dalessandro 		atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
437967bcfc0SMike Marciniszyn 		avoid_schedule = 1;
438967bcfc0SMike Marciniszyn 	}
439f931551bSRalph Campbell 	wqe->ssn = qp->s_ssn++;
440f931551bSRalph Campbell 	qp->s_head = next;
441f931551bSRalph Campbell 
442f931551bSRalph Campbell 	ret = 0;
443f931551bSRalph Campbell 	goto bail;
444f931551bSRalph Campbell 
445f931551bSRalph Campbell bail_inval_free:
446f931551bSRalph Campbell 	while (j) {
4477c2e11feSDennis Dalessandro 		struct rvt_sge *sge = &wqe->sg_list[--j];
448f931551bSRalph Campbell 
4497c2e11feSDennis Dalessandro 		rvt_put_mr(sge->mr);
450f931551bSRalph Campbell 	}
451f931551bSRalph Campbell bail_inval:
452f931551bSRalph Campbell 	ret = -EINVAL;
453f931551bSRalph Campbell bail:
454967bcfc0SMike Marciniszyn 	if (!ret && !wr->next && !avoid_schedule &&
455551ace12SMike Marciniszyn 	 !qib_sdma_empty(
456551ace12SMike Marciniszyn 	   dd_from_ibdev(qp->ibqp.device)->pport + qp->port_num - 1)) {
457551ace12SMike Marciniszyn 		qib_schedule_send(qp);
458551ace12SMike Marciniszyn 		*scheduled = 1;
459551ace12SMike Marciniszyn 	}
460f931551bSRalph Campbell 	spin_unlock_irqrestore(&qp->s_lock, flags);
461f931551bSRalph Campbell 	return ret;
462f931551bSRalph Campbell }
463f931551bSRalph Campbell 
464f931551bSRalph Campbell /**
465f931551bSRalph Campbell  * qib_post_send - post a send on a QP
466f931551bSRalph Campbell  * @ibqp: the QP to post the send on
467f931551bSRalph Campbell  * @wr: the list of work requests to post
468f931551bSRalph Campbell  * @bad_wr: the first bad WR is put here
469f931551bSRalph Campbell  *
470f931551bSRalph Campbell  * This may be called from interrupt context.
471f931551bSRalph Campbell  */
472f931551bSRalph Campbell static int qib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
473f931551bSRalph Campbell 			 struct ib_send_wr **bad_wr)
474f931551bSRalph Campbell {
4757c2e11feSDennis Dalessandro 	struct rvt_qp *qp = to_iqp(ibqp);
476ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv = qp->priv;
477f931551bSRalph Campbell 	int err = 0;
478551ace12SMike Marciniszyn 	int scheduled = 0;
479f931551bSRalph Campbell 
480f931551bSRalph Campbell 	for (; wr; wr = wr->next) {
481551ace12SMike Marciniszyn 		err = qib_post_one_send(qp, wr, &scheduled);
482f931551bSRalph Campbell 		if (err) {
483f931551bSRalph Campbell 			*bad_wr = wr;
484f931551bSRalph Campbell 			goto bail;
485f931551bSRalph Campbell 		}
486f931551bSRalph Campbell 	}
487f931551bSRalph Campbell 
488f931551bSRalph Campbell 	/* Try to do the send work in the caller's context. */
489551ace12SMike Marciniszyn 	if (!scheduled)
490ffc26907SDennis Dalessandro 		qib_do_send(&priv->s_work);
491f931551bSRalph Campbell 
492f931551bSRalph Campbell bail:
493f931551bSRalph Campbell 	return err;
494f931551bSRalph Campbell }
495f931551bSRalph Campbell 
496f931551bSRalph Campbell /**
497f931551bSRalph Campbell  * qib_post_receive - post a receive on a QP
498f931551bSRalph Campbell  * @ibqp: the QP to post the receive on
499f931551bSRalph Campbell  * @wr: the WR to post
500f931551bSRalph Campbell  * @bad_wr: the first bad WR is put here
501f931551bSRalph Campbell  *
502f931551bSRalph Campbell  * This may be called from interrupt context.
503f931551bSRalph Campbell  */
504f931551bSRalph Campbell static int qib_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
505f931551bSRalph Campbell 			    struct ib_recv_wr **bad_wr)
506f931551bSRalph Campbell {
5077c2e11feSDennis Dalessandro 	struct rvt_qp *qp = to_iqp(ibqp);
5087c2e11feSDennis Dalessandro 	struct rvt_rwq *wq = qp->r_rq.wq;
509f931551bSRalph Campbell 	unsigned long flags;
510f931551bSRalph Campbell 	int ret;
511f931551bSRalph Campbell 
512f931551bSRalph Campbell 	/* Check that state is OK to post receive. */
513f931551bSRalph Campbell 	if (!(ib_qib_state_ops[qp->state] & QIB_POST_RECV_OK) || !wq) {
514f931551bSRalph Campbell 		*bad_wr = wr;
515f931551bSRalph Campbell 		ret = -EINVAL;
516f931551bSRalph Campbell 		goto bail;
517f931551bSRalph Campbell 	}
518f931551bSRalph Campbell 
519f931551bSRalph Campbell 	for (; wr; wr = wr->next) {
5207c2e11feSDennis Dalessandro 		struct rvt_rwqe *wqe;
521f931551bSRalph Campbell 		u32 next;
522f931551bSRalph Campbell 		int i;
523f931551bSRalph Campbell 
524f931551bSRalph Campbell 		if ((unsigned) wr->num_sge > qp->r_rq.max_sge) {
525f931551bSRalph Campbell 			*bad_wr = wr;
526f931551bSRalph Campbell 			ret = -EINVAL;
527f931551bSRalph Campbell 			goto bail;
528f931551bSRalph Campbell 		}
529f931551bSRalph Campbell 
530f931551bSRalph Campbell 		spin_lock_irqsave(&qp->r_rq.lock, flags);
531f931551bSRalph Campbell 		next = wq->head + 1;
532f931551bSRalph Campbell 		if (next >= qp->r_rq.size)
533f931551bSRalph Campbell 			next = 0;
534f931551bSRalph Campbell 		if (next == wq->tail) {
535f931551bSRalph Campbell 			spin_unlock_irqrestore(&qp->r_rq.lock, flags);
536f931551bSRalph Campbell 			*bad_wr = wr;
537f931551bSRalph Campbell 			ret = -ENOMEM;
538f931551bSRalph Campbell 			goto bail;
539f931551bSRalph Campbell 		}
540f931551bSRalph Campbell 
541f931551bSRalph Campbell 		wqe = get_rwqe_ptr(&qp->r_rq, wq->head);
542f931551bSRalph Campbell 		wqe->wr_id = wr->wr_id;
543f931551bSRalph Campbell 		wqe->num_sge = wr->num_sge;
544f931551bSRalph Campbell 		for (i = 0; i < wr->num_sge; i++)
545f931551bSRalph Campbell 			wqe->sg_list[i] = wr->sg_list[i];
546f931551bSRalph Campbell 		/* Make sure queue entry is written before the head index. */
547f931551bSRalph Campbell 		smp_wmb();
548f931551bSRalph Campbell 		wq->head = next;
549f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->r_rq.lock, flags);
550f931551bSRalph Campbell 	}
551f931551bSRalph Campbell 	ret = 0;
552f931551bSRalph Campbell 
553f931551bSRalph Campbell bail:
554f931551bSRalph Campbell 	return ret;
555f931551bSRalph Campbell }
556f931551bSRalph Campbell 
557f931551bSRalph Campbell /**
558f931551bSRalph Campbell  * qib_qp_rcv - processing an incoming packet on a QP
559f931551bSRalph Campbell  * @rcd: the context pointer
560f931551bSRalph Campbell  * @hdr: the packet header
561f931551bSRalph Campbell  * @has_grh: true if the packet has a GRH
562f931551bSRalph Campbell  * @data: the packet data
563f931551bSRalph Campbell  * @tlen: the packet length
564f931551bSRalph Campbell  * @qp: the QP the packet came on
565f931551bSRalph Campbell  *
566f931551bSRalph Campbell  * This is called from qib_ib_rcv() to process an incoming packet
567f931551bSRalph Campbell  * for the given QP.
568f931551bSRalph Campbell  * Called at interrupt level.
569f931551bSRalph Campbell  */
570f931551bSRalph Campbell static void qib_qp_rcv(struct qib_ctxtdata *rcd, struct qib_ib_header *hdr,
5717c2e11feSDennis Dalessandro 		       int has_grh, void *data, u32 tlen, struct rvt_qp *qp)
572f931551bSRalph Campbell {
573f931551bSRalph Campbell 	struct qib_ibport *ibp = &rcd->ppd->ibport_data;
574f931551bSRalph Campbell 
575a5210c12SRalph Campbell 	spin_lock(&qp->r_lock);
576a5210c12SRalph Campbell 
577f931551bSRalph Campbell 	/* Check for valid receive state. */
578f931551bSRalph Campbell 	if (!(ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK)) {
579f24a6d48SHarish Chegondi 		ibp->rvp.n_pkt_drops++;
580a5210c12SRalph Campbell 		goto unlock;
581f931551bSRalph Campbell 	}
582f931551bSRalph Campbell 
583f931551bSRalph Campbell 	switch (qp->ibqp.qp_type) {
584f931551bSRalph Campbell 	case IB_QPT_SMI:
585f931551bSRalph Campbell 	case IB_QPT_GSI:
586f931551bSRalph Campbell 		if (ib_qib_disable_sma)
587f931551bSRalph Campbell 			break;
588f931551bSRalph Campbell 		/* FALLTHROUGH */
589f931551bSRalph Campbell 	case IB_QPT_UD:
590f931551bSRalph Campbell 		qib_ud_rcv(ibp, hdr, has_grh, data, tlen, qp);
591f931551bSRalph Campbell 		break;
592f931551bSRalph Campbell 
593f931551bSRalph Campbell 	case IB_QPT_RC:
594f931551bSRalph Campbell 		qib_rc_rcv(rcd, hdr, has_grh, data, tlen, qp);
595f931551bSRalph Campbell 		break;
596f931551bSRalph Campbell 
597f931551bSRalph Campbell 	case IB_QPT_UC:
598f931551bSRalph Campbell 		qib_uc_rcv(ibp, hdr, has_grh, data, tlen, qp);
599f931551bSRalph Campbell 		break;
600f931551bSRalph Campbell 
601f931551bSRalph Campbell 	default:
602f931551bSRalph Campbell 		break;
603f931551bSRalph Campbell 	}
604a5210c12SRalph Campbell 
605a5210c12SRalph Campbell unlock:
606a5210c12SRalph Campbell 	spin_unlock(&qp->r_lock);
607f931551bSRalph Campbell }
608f931551bSRalph Campbell 
609f931551bSRalph Campbell /**
610f931551bSRalph Campbell  * qib_ib_rcv - process an incoming packet
611f931551bSRalph Campbell  * @rcd: the context pointer
612f931551bSRalph Campbell  * @rhdr: the header of the packet
613f931551bSRalph Campbell  * @data: the packet payload
614f931551bSRalph Campbell  * @tlen: the packet length
615f931551bSRalph Campbell  *
616f931551bSRalph Campbell  * This is called from qib_kreceive() to process an incoming packet at
617f931551bSRalph Campbell  * interrupt level. Tlen is the length of the header + data + CRC in bytes.
618f931551bSRalph Campbell  */
619f931551bSRalph Campbell void qib_ib_rcv(struct qib_ctxtdata *rcd, void *rhdr, void *data, u32 tlen)
620f931551bSRalph Campbell {
621f931551bSRalph Campbell 	struct qib_pportdata *ppd = rcd->ppd;
622f931551bSRalph Campbell 	struct qib_ibport *ibp = &ppd->ibport_data;
623f931551bSRalph Campbell 	struct qib_ib_header *hdr = rhdr;
624f931551bSRalph Campbell 	struct qib_other_headers *ohdr;
6257c2e11feSDennis Dalessandro 	struct rvt_qp *qp;
626f931551bSRalph Campbell 	u32 qp_num;
627f931551bSRalph Campbell 	int lnh;
628f931551bSRalph Campbell 	u8 opcode;
629f931551bSRalph Campbell 	u16 lid;
630f931551bSRalph Campbell 
631f931551bSRalph Campbell 	/* 24 == LRH+BTH+CRC */
632f931551bSRalph Campbell 	if (unlikely(tlen < 24))
633f931551bSRalph Campbell 		goto drop;
634f931551bSRalph Campbell 
635f931551bSRalph Campbell 	/* Check for a valid destination LID (see ch. 7.11.1). */
636f931551bSRalph Campbell 	lid = be16_to_cpu(hdr->lrh[1]);
6379ff198f5SDennis Dalessandro 	if (lid < be16_to_cpu(IB_MULTICAST_LID_BASE)) {
638f931551bSRalph Campbell 		lid &= ~((1 << ppd->lmc) - 1);
639f931551bSRalph Campbell 		if (unlikely(lid != ppd->lid))
640f931551bSRalph Campbell 			goto drop;
641f931551bSRalph Campbell 	}
642f931551bSRalph Campbell 
643f931551bSRalph Campbell 	/* Check for GRH */
644f931551bSRalph Campbell 	lnh = be16_to_cpu(hdr->lrh[0]) & 3;
645f931551bSRalph Campbell 	if (lnh == QIB_LRH_BTH)
646f931551bSRalph Campbell 		ohdr = &hdr->u.oth;
647f931551bSRalph Campbell 	else if (lnh == QIB_LRH_GRH) {
648f931551bSRalph Campbell 		u32 vtf;
649f931551bSRalph Campbell 
650f931551bSRalph Campbell 		ohdr = &hdr->u.l.oth;
651f931551bSRalph Campbell 		if (hdr->u.l.grh.next_hdr != IB_GRH_NEXT_HDR)
652f931551bSRalph Campbell 			goto drop;
653f931551bSRalph Campbell 		vtf = be32_to_cpu(hdr->u.l.grh.version_tclass_flow);
654f931551bSRalph Campbell 		if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
655f931551bSRalph Campbell 			goto drop;
656f931551bSRalph Campbell 	} else
657f931551bSRalph Campbell 		goto drop;
658f931551bSRalph Campbell 
659ddb88765SMike Marciniszyn 	opcode = (be32_to_cpu(ohdr->bth[0]) >> 24) & 0x7f;
660ddb88765SMike Marciniszyn #ifdef CONFIG_DEBUG_FS
661ddb88765SMike Marciniszyn 	rcd->opstats->stats[opcode].n_bytes += tlen;
662ddb88765SMike Marciniszyn 	rcd->opstats->stats[opcode].n_packets++;
663ddb88765SMike Marciniszyn #endif
664f931551bSRalph Campbell 
665f931551bSRalph Campbell 	/* Get the destination QP number. */
666f931551bSRalph Campbell 	qp_num = be32_to_cpu(ohdr->bth[1]) & QIB_QPN_MASK;
667f931551bSRalph Campbell 	if (qp_num == QIB_MULTICAST_QPN) {
668f931551bSRalph Campbell 		struct qib_mcast *mcast;
669f931551bSRalph Campbell 		struct qib_mcast_qp *p;
670f931551bSRalph Campbell 
671f931551bSRalph Campbell 		if (lnh != QIB_LRH_GRH)
672f931551bSRalph Campbell 			goto drop;
673f931551bSRalph Campbell 		mcast = qib_mcast_find(ibp, &hdr->u.l.grh.dgid);
674f931551bSRalph Campbell 		if (mcast == NULL)
675f931551bSRalph Campbell 			goto drop;
6767d7632adSMike Marciniszyn 		this_cpu_inc(ibp->pmastats->n_multicast_rcv);
677f931551bSRalph Campbell 		list_for_each_entry_rcu(p, &mcast->qp_list, list)
678f931551bSRalph Campbell 			qib_qp_rcv(rcd, hdr, 1, data, tlen, p->qp);
679f931551bSRalph Campbell 		/*
680f931551bSRalph Campbell 		 * Notify qib_multicast_detach() if it is waiting for us
681f931551bSRalph Campbell 		 * to finish.
682f931551bSRalph Campbell 		 */
683f931551bSRalph Campbell 		if (atomic_dec_return(&mcast->refcount) <= 1)
684f931551bSRalph Campbell 			wake_up(&mcast->wait);
685f931551bSRalph Campbell 	} else {
686af061a64SMike Marciniszyn 		if (rcd->lookaside_qp) {
687af061a64SMike Marciniszyn 			if (rcd->lookaside_qpn != qp_num) {
688af061a64SMike Marciniszyn 				if (atomic_dec_and_test(
689af061a64SMike Marciniszyn 					&rcd->lookaside_qp->refcount))
690af061a64SMike Marciniszyn 					wake_up(
691af061a64SMike Marciniszyn 					 &rcd->lookaside_qp->wait);
692af061a64SMike Marciniszyn 				rcd->lookaside_qp = NULL;
693af061a64SMike Marciniszyn 			}
694af061a64SMike Marciniszyn 		}
695af061a64SMike Marciniszyn 		if (!rcd->lookaside_qp) {
696f931551bSRalph Campbell 			qp = qib_lookup_qpn(ibp, qp_num);
697f931551bSRalph Campbell 			if (!qp)
698f931551bSRalph Campbell 				goto drop;
699af061a64SMike Marciniszyn 			rcd->lookaside_qp = qp;
700af061a64SMike Marciniszyn 			rcd->lookaside_qpn = qp_num;
701af061a64SMike Marciniszyn 		} else
702af061a64SMike Marciniszyn 			qp = rcd->lookaside_qp;
7037d7632adSMike Marciniszyn 		this_cpu_inc(ibp->pmastats->n_unicast_rcv);
704f931551bSRalph Campbell 		qib_qp_rcv(rcd, hdr, lnh == QIB_LRH_GRH, data, tlen, qp);
705f931551bSRalph Campbell 	}
706f931551bSRalph Campbell 	return;
707f931551bSRalph Campbell 
708f931551bSRalph Campbell drop:
709f24a6d48SHarish Chegondi 	ibp->rvp.n_pkt_drops++;
710f931551bSRalph Campbell }
711f931551bSRalph Campbell 
712f931551bSRalph Campbell /*
713f931551bSRalph Campbell  * This is called from a timer to check for QPs
714f931551bSRalph Campbell  * which need kernel memory in order to send a packet.
715f931551bSRalph Campbell  */
716f931551bSRalph Campbell static void mem_timer(unsigned long data)
717f931551bSRalph Campbell {
718f931551bSRalph Campbell 	struct qib_ibdev *dev = (struct qib_ibdev *) data;
719f931551bSRalph Campbell 	struct list_head *list = &dev->memwait;
7207c2e11feSDennis Dalessandro 	struct rvt_qp *qp = NULL;
721ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv = NULL;
722f931551bSRalph Campbell 	unsigned long flags;
723f931551bSRalph Campbell 
724cd18201fSHarish Chegondi 	spin_lock_irqsave(&dev->rdi.pending_lock, flags);
725f931551bSRalph Campbell 	if (!list_empty(list)) {
726ffc26907SDennis Dalessandro 		priv = list_entry(list->next, struct qib_qp_priv, iowait);
727ffc26907SDennis Dalessandro 		qp = priv->owner;
728ffc26907SDennis Dalessandro 		list_del_init(&priv->iowait);
729f931551bSRalph Campbell 		atomic_inc(&qp->refcount);
730f931551bSRalph Campbell 		if (!list_empty(list))
731f931551bSRalph Campbell 			mod_timer(&dev->mem_timer, jiffies + 1);
732f931551bSRalph Campbell 	}
733cd18201fSHarish Chegondi 	spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
734f931551bSRalph Campbell 
735f931551bSRalph Campbell 	if (qp) {
736f931551bSRalph Campbell 		spin_lock_irqsave(&qp->s_lock, flags);
73701ba79d4SHarish Chegondi 		if (qp->s_flags & RVT_S_WAIT_KMEM) {
73801ba79d4SHarish Chegondi 			qp->s_flags &= ~RVT_S_WAIT_KMEM;
739f931551bSRalph Campbell 			qib_schedule_send(qp);
740f931551bSRalph Campbell 		}
741f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
742f931551bSRalph Campbell 		if (atomic_dec_and_test(&qp->refcount))
743f931551bSRalph Campbell 			wake_up(&qp->wait);
744f931551bSRalph Campbell 	}
745f931551bSRalph Campbell }
746f931551bSRalph Campbell 
7477c2e11feSDennis Dalessandro static void update_sge(struct rvt_sge_state *ss, u32 length)
748f931551bSRalph Campbell {
7497c2e11feSDennis Dalessandro 	struct rvt_sge *sge = &ss->sge;
750f931551bSRalph Campbell 
751f931551bSRalph Campbell 	sge->vaddr += length;
752f931551bSRalph Campbell 	sge->length -= length;
753f931551bSRalph Campbell 	sge->sge_length -= length;
754f931551bSRalph Campbell 	if (sge->sge_length == 0) {
755f931551bSRalph Campbell 		if (--ss->num_sge)
756f931551bSRalph Campbell 			*sge = *ss->sg_list++;
757f931551bSRalph Campbell 	} else if (sge->length == 0 && sge->mr->lkey) {
7587c2e11feSDennis Dalessandro 		if (++sge->n >= RVT_SEGSZ) {
759f931551bSRalph Campbell 			if (++sge->m >= sge->mr->mapsz)
760f931551bSRalph Campbell 				return;
761f931551bSRalph Campbell 			sge->n = 0;
762f931551bSRalph Campbell 		}
763f931551bSRalph Campbell 		sge->vaddr = sge->mr->map[sge->m]->segs[sge->n].vaddr;
764f931551bSRalph Campbell 		sge->length = sge->mr->map[sge->m]->segs[sge->n].length;
765f931551bSRalph Campbell 	}
766f931551bSRalph Campbell }
767f931551bSRalph Campbell 
768f931551bSRalph Campbell #ifdef __LITTLE_ENDIAN
769f931551bSRalph Campbell static inline u32 get_upper_bits(u32 data, u32 shift)
770f931551bSRalph Campbell {
771f931551bSRalph Campbell 	return data >> shift;
772f931551bSRalph Campbell }
773f931551bSRalph Campbell 
774f931551bSRalph Campbell static inline u32 set_upper_bits(u32 data, u32 shift)
775f931551bSRalph Campbell {
776f931551bSRalph Campbell 	return data << shift;
777f931551bSRalph Campbell }
778f931551bSRalph Campbell 
779f931551bSRalph Campbell static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
780f931551bSRalph Campbell {
781f931551bSRalph Campbell 	data <<= ((sizeof(u32) - n) * BITS_PER_BYTE);
782f931551bSRalph Campbell 	data >>= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
783f931551bSRalph Campbell 	return data;
784f931551bSRalph Campbell }
785f931551bSRalph Campbell #else
786f931551bSRalph Campbell static inline u32 get_upper_bits(u32 data, u32 shift)
787f931551bSRalph Campbell {
788f931551bSRalph Campbell 	return data << shift;
789f931551bSRalph Campbell }
790f931551bSRalph Campbell 
791f931551bSRalph Campbell static inline u32 set_upper_bits(u32 data, u32 shift)
792f931551bSRalph Campbell {
793f931551bSRalph Campbell 	return data >> shift;
794f931551bSRalph Campbell }
795f931551bSRalph Campbell 
796f931551bSRalph Campbell static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
797f931551bSRalph Campbell {
798f931551bSRalph Campbell 	data >>= ((sizeof(u32) - n) * BITS_PER_BYTE);
799f931551bSRalph Campbell 	data <<= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
800f931551bSRalph Campbell 	return data;
801f931551bSRalph Campbell }
802f931551bSRalph Campbell #endif
803f931551bSRalph Campbell 
8047c2e11feSDennis Dalessandro static void copy_io(u32 __iomem *piobuf, struct rvt_sge_state *ss,
805f931551bSRalph Campbell 		    u32 length, unsigned flush_wc)
806f931551bSRalph Campbell {
807f931551bSRalph Campbell 	u32 extra = 0;
808f931551bSRalph Campbell 	u32 data = 0;
809f931551bSRalph Campbell 	u32 last;
810f931551bSRalph Campbell 
811f931551bSRalph Campbell 	while (1) {
812f931551bSRalph Campbell 		u32 len = ss->sge.length;
813f931551bSRalph Campbell 		u32 off;
814f931551bSRalph Campbell 
815f931551bSRalph Campbell 		if (len > length)
816f931551bSRalph Campbell 			len = length;
817f931551bSRalph Campbell 		if (len > ss->sge.sge_length)
818f931551bSRalph Campbell 			len = ss->sge.sge_length;
819f931551bSRalph Campbell 		BUG_ON(len == 0);
820f931551bSRalph Campbell 		/* If the source address is not aligned, try to align it. */
821f931551bSRalph Campbell 		off = (unsigned long)ss->sge.vaddr & (sizeof(u32) - 1);
822f931551bSRalph Campbell 		if (off) {
823f931551bSRalph Campbell 			u32 *addr = (u32 *)((unsigned long)ss->sge.vaddr &
824f931551bSRalph Campbell 					    ~(sizeof(u32) - 1));
825f931551bSRalph Campbell 			u32 v = get_upper_bits(*addr, off * BITS_PER_BYTE);
826f931551bSRalph Campbell 			u32 y;
827f931551bSRalph Campbell 
828f931551bSRalph Campbell 			y = sizeof(u32) - off;
829f931551bSRalph Campbell 			if (len > y)
830f931551bSRalph Campbell 				len = y;
831f931551bSRalph Campbell 			if (len + extra >= sizeof(u32)) {
832f931551bSRalph Campbell 				data |= set_upper_bits(v, extra *
833f931551bSRalph Campbell 						       BITS_PER_BYTE);
834f931551bSRalph Campbell 				len = sizeof(u32) - extra;
835f931551bSRalph Campbell 				if (len == length) {
836f931551bSRalph Campbell 					last = data;
837f931551bSRalph Campbell 					break;
838f931551bSRalph Campbell 				}
839f931551bSRalph Campbell 				__raw_writel(data, piobuf);
840f931551bSRalph Campbell 				piobuf++;
841f931551bSRalph Campbell 				extra = 0;
842f931551bSRalph Campbell 				data = 0;
843f931551bSRalph Campbell 			} else {
844f931551bSRalph Campbell 				/* Clear unused upper bytes */
845f931551bSRalph Campbell 				data |= clear_upper_bytes(v, len, extra);
846f931551bSRalph Campbell 				if (len == length) {
847f931551bSRalph Campbell 					last = data;
848f931551bSRalph Campbell 					break;
849f931551bSRalph Campbell 				}
850f931551bSRalph Campbell 				extra += len;
851f931551bSRalph Campbell 			}
852f931551bSRalph Campbell 		} else if (extra) {
853f931551bSRalph Campbell 			/* Source address is aligned. */
854f931551bSRalph Campbell 			u32 *addr = (u32 *) ss->sge.vaddr;
855f931551bSRalph Campbell 			int shift = extra * BITS_PER_BYTE;
856f931551bSRalph Campbell 			int ushift = 32 - shift;
857f931551bSRalph Campbell 			u32 l = len;
858f931551bSRalph Campbell 
859f931551bSRalph Campbell 			while (l >= sizeof(u32)) {
860f931551bSRalph Campbell 				u32 v = *addr;
861f931551bSRalph Campbell 
862f931551bSRalph Campbell 				data |= set_upper_bits(v, shift);
863f931551bSRalph Campbell 				__raw_writel(data, piobuf);
864f931551bSRalph Campbell 				data = get_upper_bits(v, ushift);
865f931551bSRalph Campbell 				piobuf++;
866f931551bSRalph Campbell 				addr++;
867f931551bSRalph Campbell 				l -= sizeof(u32);
868f931551bSRalph Campbell 			}
869f931551bSRalph Campbell 			/*
870f931551bSRalph Campbell 			 * We still have 'extra' number of bytes leftover.
871f931551bSRalph Campbell 			 */
872f931551bSRalph Campbell 			if (l) {
873f931551bSRalph Campbell 				u32 v = *addr;
874f931551bSRalph Campbell 
875f931551bSRalph Campbell 				if (l + extra >= sizeof(u32)) {
876f931551bSRalph Campbell 					data |= set_upper_bits(v, shift);
877f931551bSRalph Campbell 					len -= l + extra - sizeof(u32);
878f931551bSRalph Campbell 					if (len == length) {
879f931551bSRalph Campbell 						last = data;
880f931551bSRalph Campbell 						break;
881f931551bSRalph Campbell 					}
882f931551bSRalph Campbell 					__raw_writel(data, piobuf);
883f931551bSRalph Campbell 					piobuf++;
884f931551bSRalph Campbell 					extra = 0;
885f931551bSRalph Campbell 					data = 0;
886f931551bSRalph Campbell 				} else {
887f931551bSRalph Campbell 					/* Clear unused upper bytes */
888f931551bSRalph Campbell 					data |= clear_upper_bytes(v, l, extra);
889f931551bSRalph Campbell 					if (len == length) {
890f931551bSRalph Campbell 						last = data;
891f931551bSRalph Campbell 						break;
892f931551bSRalph Campbell 					}
893f931551bSRalph Campbell 					extra += l;
894f931551bSRalph Campbell 				}
895f931551bSRalph Campbell 			} else if (len == length) {
896f931551bSRalph Campbell 				last = data;
897f931551bSRalph Campbell 				break;
898f931551bSRalph Campbell 			}
899f931551bSRalph Campbell 		} else if (len == length) {
900f931551bSRalph Campbell 			u32 w;
901f931551bSRalph Campbell 
902f931551bSRalph Campbell 			/*
903f931551bSRalph Campbell 			 * Need to round up for the last dword in the
904f931551bSRalph Campbell 			 * packet.
905f931551bSRalph Campbell 			 */
906f931551bSRalph Campbell 			w = (len + 3) >> 2;
907f931551bSRalph Campbell 			qib_pio_copy(piobuf, ss->sge.vaddr, w - 1);
908f931551bSRalph Campbell 			piobuf += w - 1;
909f931551bSRalph Campbell 			last = ((u32 *) ss->sge.vaddr)[w - 1];
910f931551bSRalph Campbell 			break;
911f931551bSRalph Campbell 		} else {
912f931551bSRalph Campbell 			u32 w = len >> 2;
913f931551bSRalph Campbell 
914f931551bSRalph Campbell 			qib_pio_copy(piobuf, ss->sge.vaddr, w);
915f931551bSRalph Campbell 			piobuf += w;
916f931551bSRalph Campbell 
917f931551bSRalph Campbell 			extra = len & (sizeof(u32) - 1);
918f931551bSRalph Campbell 			if (extra) {
919f931551bSRalph Campbell 				u32 v = ((u32 *) ss->sge.vaddr)[w];
920f931551bSRalph Campbell 
921f931551bSRalph Campbell 				/* Clear unused upper bytes */
922f931551bSRalph Campbell 				data = clear_upper_bytes(v, extra, 0);
923f931551bSRalph Campbell 			}
924f931551bSRalph Campbell 		}
925f931551bSRalph Campbell 		update_sge(ss, len);
926f931551bSRalph Campbell 		length -= len;
927f931551bSRalph Campbell 	}
928f931551bSRalph Campbell 	/* Update address before sending packet. */
929f931551bSRalph Campbell 	update_sge(ss, length);
930f931551bSRalph Campbell 	if (flush_wc) {
931f931551bSRalph Campbell 		/* must flush early everything before trigger word */
932f931551bSRalph Campbell 		qib_flush_wc();
933f931551bSRalph Campbell 		__raw_writel(last, piobuf);
934f931551bSRalph Campbell 		/* be sure trigger word is written */
935f931551bSRalph Campbell 		qib_flush_wc();
936f931551bSRalph Campbell 	} else
937f931551bSRalph Campbell 		__raw_writel(last, piobuf);
938f931551bSRalph Campbell }
939f931551bSRalph Campbell 
94048947109SMike Marciniszyn static noinline struct qib_verbs_txreq *__get_txreq(struct qib_ibdev *dev,
9417c2e11feSDennis Dalessandro 					   struct rvt_qp *qp)
942f931551bSRalph Campbell {
943ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv = qp->priv;
944f931551bSRalph Campbell 	struct qib_verbs_txreq *tx;
945f931551bSRalph Campbell 	unsigned long flags;
946f931551bSRalph Campbell 
947f931551bSRalph Campbell 	spin_lock_irqsave(&qp->s_lock, flags);
948cd18201fSHarish Chegondi 	spin_lock(&dev->rdi.pending_lock);
949f931551bSRalph Campbell 
950f931551bSRalph Campbell 	if (!list_empty(&dev->txreq_free)) {
951f931551bSRalph Campbell 		struct list_head *l = dev->txreq_free.next;
952f931551bSRalph Campbell 
953f931551bSRalph Campbell 		list_del(l);
954cd18201fSHarish Chegondi 		spin_unlock(&dev->rdi.pending_lock);
95548947109SMike Marciniszyn 		spin_unlock_irqrestore(&qp->s_lock, flags);
956f931551bSRalph Campbell 		tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
957f931551bSRalph Campbell 	} else {
958f931551bSRalph Campbell 		if (ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK &&
959ffc26907SDennis Dalessandro 		    list_empty(&priv->iowait)) {
960f931551bSRalph Campbell 			dev->n_txwait++;
96101ba79d4SHarish Chegondi 			qp->s_flags |= RVT_S_WAIT_TX;
962ffc26907SDennis Dalessandro 			list_add_tail(&priv->iowait, &dev->txwait);
963f931551bSRalph Campbell 		}
96401ba79d4SHarish Chegondi 		qp->s_flags &= ~RVT_S_BUSY;
965cd18201fSHarish Chegondi 		spin_unlock(&dev->rdi.pending_lock);
966f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
96748947109SMike Marciniszyn 		tx = ERR_PTR(-EBUSY);
96848947109SMike Marciniszyn 	}
96948947109SMike Marciniszyn 	return tx;
97048947109SMike Marciniszyn }
971f931551bSRalph Campbell 
97248947109SMike Marciniszyn static inline struct qib_verbs_txreq *get_txreq(struct qib_ibdev *dev,
9737c2e11feSDennis Dalessandro 					 struct rvt_qp *qp)
97448947109SMike Marciniszyn {
97548947109SMike Marciniszyn 	struct qib_verbs_txreq *tx;
97648947109SMike Marciniszyn 	unsigned long flags;
97748947109SMike Marciniszyn 
978cd18201fSHarish Chegondi 	spin_lock_irqsave(&dev->rdi.pending_lock, flags);
97948947109SMike Marciniszyn 	/* assume the list non empty */
98048947109SMike Marciniszyn 	if (likely(!list_empty(&dev->txreq_free))) {
98148947109SMike Marciniszyn 		struct list_head *l = dev->txreq_free.next;
98248947109SMike Marciniszyn 
98348947109SMike Marciniszyn 		list_del(l);
984cd18201fSHarish Chegondi 		spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
98548947109SMike Marciniszyn 		tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
98648947109SMike Marciniszyn 	} else {
98748947109SMike Marciniszyn 		/* call slow path to get the extra lock */
988cd18201fSHarish Chegondi 		spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
98948947109SMike Marciniszyn 		tx =  __get_txreq(dev, qp);
99048947109SMike Marciniszyn 	}
991f931551bSRalph Campbell 	return tx;
992f931551bSRalph Campbell }
993f931551bSRalph Campbell 
994f931551bSRalph Campbell void qib_put_txreq(struct qib_verbs_txreq *tx)
995f931551bSRalph Campbell {
996f931551bSRalph Campbell 	struct qib_ibdev *dev;
9977c2e11feSDennis Dalessandro 	struct rvt_qp *qp;
998ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv;
999f931551bSRalph Campbell 	unsigned long flags;
1000f931551bSRalph Campbell 
1001f931551bSRalph Campbell 	qp = tx->qp;
1002f931551bSRalph Campbell 	dev = to_idev(qp->ibqp.device);
1003f931551bSRalph Campbell 
1004f931551bSRalph Campbell 	if (atomic_dec_and_test(&qp->refcount))
1005f931551bSRalph Campbell 		wake_up(&qp->wait);
1006f931551bSRalph Campbell 	if (tx->mr) {
10077c2e11feSDennis Dalessandro 		rvt_put_mr(tx->mr);
1008f931551bSRalph Campbell 		tx->mr = NULL;
1009f931551bSRalph Campbell 	}
1010f931551bSRalph Campbell 	if (tx->txreq.flags & QIB_SDMA_TXREQ_F_FREEBUF) {
1011f931551bSRalph Campbell 		tx->txreq.flags &= ~QIB_SDMA_TXREQ_F_FREEBUF;
1012f931551bSRalph Campbell 		dma_unmap_single(&dd_from_dev(dev)->pcidev->dev,
1013f931551bSRalph Campbell 				 tx->txreq.addr, tx->hdr_dwords << 2,
1014f931551bSRalph Campbell 				 DMA_TO_DEVICE);
1015f931551bSRalph Campbell 		kfree(tx->align_buf);
1016f931551bSRalph Campbell 	}
1017f931551bSRalph Campbell 
1018cd18201fSHarish Chegondi 	spin_lock_irqsave(&dev->rdi.pending_lock, flags);
1019f931551bSRalph Campbell 
1020f931551bSRalph Campbell 	/* Put struct back on free list */
1021f931551bSRalph Campbell 	list_add(&tx->txreq.list, &dev->txreq_free);
1022f931551bSRalph Campbell 
1023f931551bSRalph Campbell 	if (!list_empty(&dev->txwait)) {
1024f931551bSRalph Campbell 		/* Wake up first QP wanting a free struct */
1025ffc26907SDennis Dalessandro 		priv = list_entry(dev->txwait.next, struct qib_qp_priv,
1026ffc26907SDennis Dalessandro 				  iowait);
1027ffc26907SDennis Dalessandro 		qp = priv->owner;
1028ffc26907SDennis Dalessandro 		list_del_init(&priv->iowait);
1029f931551bSRalph Campbell 		atomic_inc(&qp->refcount);
1030cd18201fSHarish Chegondi 		spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
1031f931551bSRalph Campbell 
1032f931551bSRalph Campbell 		spin_lock_irqsave(&qp->s_lock, flags);
103301ba79d4SHarish Chegondi 		if (qp->s_flags & RVT_S_WAIT_TX) {
103401ba79d4SHarish Chegondi 			qp->s_flags &= ~RVT_S_WAIT_TX;
1035f931551bSRalph Campbell 			qib_schedule_send(qp);
1036f931551bSRalph Campbell 		}
1037f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
1038f931551bSRalph Campbell 
1039f931551bSRalph Campbell 		if (atomic_dec_and_test(&qp->refcount))
1040f931551bSRalph Campbell 			wake_up(&qp->wait);
1041f931551bSRalph Campbell 	} else
1042cd18201fSHarish Chegondi 		spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
1043f931551bSRalph Campbell }
1044f931551bSRalph Campbell 
1045f931551bSRalph Campbell /*
1046f931551bSRalph Campbell  * This is called when there are send DMA descriptors that might be
1047f931551bSRalph Campbell  * available.
1048f931551bSRalph Campbell  *
1049f931551bSRalph Campbell  * This is called with ppd->sdma_lock held.
1050f931551bSRalph Campbell  */
1051f931551bSRalph Campbell void qib_verbs_sdma_desc_avail(struct qib_pportdata *ppd, unsigned avail)
1052f931551bSRalph Campbell {
10537c2e11feSDennis Dalessandro 	struct rvt_qp *qp, *nqp;
1054ffc26907SDennis Dalessandro 	struct qib_qp_priv *qpp, *nqpp;
10557c2e11feSDennis Dalessandro 	struct rvt_qp *qps[20];
1056f931551bSRalph Campbell 	struct qib_ibdev *dev;
1057f931551bSRalph Campbell 	unsigned i, n;
1058f931551bSRalph Campbell 
1059f931551bSRalph Campbell 	n = 0;
1060f931551bSRalph Campbell 	dev = &ppd->dd->verbs_dev;
1061cd18201fSHarish Chegondi 	spin_lock(&dev->rdi.pending_lock);
1062f931551bSRalph Campbell 
1063f931551bSRalph Campbell 	/* Search wait list for first QP wanting DMA descriptors. */
1064ffc26907SDennis Dalessandro 	list_for_each_entry_safe(qpp, nqpp, &dev->dmawait, iowait) {
1065ffc26907SDennis Dalessandro 		qp = qpp->owner;
1066ffc26907SDennis Dalessandro 		nqp = nqpp->owner;
1067f931551bSRalph Campbell 		if (qp->port_num != ppd->port)
1068f931551bSRalph Campbell 			continue;
1069f931551bSRalph Campbell 		if (n == ARRAY_SIZE(qps))
1070f931551bSRalph Campbell 			break;
1071ffc26907SDennis Dalessandro 		if (qpp->s_tx->txreq.sg_count > avail)
1072f931551bSRalph Campbell 			break;
1073ffc26907SDennis Dalessandro 		avail -= qpp->s_tx->txreq.sg_count;
1074ffc26907SDennis Dalessandro 		list_del_init(&qpp->iowait);
1075f931551bSRalph Campbell 		atomic_inc(&qp->refcount);
1076f931551bSRalph Campbell 		qps[n++] = qp;
1077f931551bSRalph Campbell 	}
1078f931551bSRalph Campbell 
1079cd18201fSHarish Chegondi 	spin_unlock(&dev->rdi.pending_lock);
1080f931551bSRalph Campbell 
1081f931551bSRalph Campbell 	for (i = 0; i < n; i++) {
1082f931551bSRalph Campbell 		qp = qps[i];
1083f931551bSRalph Campbell 		spin_lock(&qp->s_lock);
108401ba79d4SHarish Chegondi 		if (qp->s_flags & RVT_S_WAIT_DMA_DESC) {
108501ba79d4SHarish Chegondi 			qp->s_flags &= ~RVT_S_WAIT_DMA_DESC;
1086f931551bSRalph Campbell 			qib_schedule_send(qp);
1087f931551bSRalph Campbell 		}
1088f931551bSRalph Campbell 		spin_unlock(&qp->s_lock);
1089f931551bSRalph Campbell 		if (atomic_dec_and_test(&qp->refcount))
1090f931551bSRalph Campbell 			wake_up(&qp->wait);
1091f931551bSRalph Campbell 	}
1092f931551bSRalph Campbell }
1093f931551bSRalph Campbell 
1094f931551bSRalph Campbell /*
1095f931551bSRalph Campbell  * This is called with ppd->sdma_lock held.
1096f931551bSRalph Campbell  */
1097f931551bSRalph Campbell static void sdma_complete(struct qib_sdma_txreq *cookie, int status)
1098f931551bSRalph Campbell {
1099f931551bSRalph Campbell 	struct qib_verbs_txreq *tx =
1100f931551bSRalph Campbell 		container_of(cookie, struct qib_verbs_txreq, txreq);
11017c2e11feSDennis Dalessandro 	struct rvt_qp *qp = tx->qp;
1102ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv = qp->priv;
1103f931551bSRalph Campbell 
1104f931551bSRalph Campbell 	spin_lock(&qp->s_lock);
1105f931551bSRalph Campbell 	if (tx->wqe)
1106f931551bSRalph Campbell 		qib_send_complete(qp, tx->wqe, IB_WC_SUCCESS);
1107f931551bSRalph Campbell 	else if (qp->ibqp.qp_type == IB_QPT_RC) {
1108f931551bSRalph Campbell 		struct qib_ib_header *hdr;
1109f931551bSRalph Campbell 
1110f931551bSRalph Campbell 		if (tx->txreq.flags & QIB_SDMA_TXREQ_F_FREEBUF)
1111f931551bSRalph Campbell 			hdr = &tx->align_buf->hdr;
1112f931551bSRalph Campbell 		else {
1113f931551bSRalph Campbell 			struct qib_ibdev *dev = to_idev(qp->ibqp.device);
1114f931551bSRalph Campbell 
1115f931551bSRalph Campbell 			hdr = &dev->pio_hdrs[tx->hdr_inx].hdr;
1116f931551bSRalph Campbell 		}
1117f931551bSRalph Campbell 		qib_rc_send_complete(qp, hdr);
1118f931551bSRalph Campbell 	}
1119ffc26907SDennis Dalessandro 	if (atomic_dec_and_test(&priv->s_dma_busy)) {
1120f931551bSRalph Campbell 		if (qp->state == IB_QPS_RESET)
1121ffc26907SDennis Dalessandro 			wake_up(&priv->wait_dma);
112201ba79d4SHarish Chegondi 		else if (qp->s_flags & RVT_S_WAIT_DMA) {
112301ba79d4SHarish Chegondi 			qp->s_flags &= ~RVT_S_WAIT_DMA;
1124f931551bSRalph Campbell 			qib_schedule_send(qp);
1125f931551bSRalph Campbell 		}
1126f931551bSRalph Campbell 	}
1127f931551bSRalph Campbell 	spin_unlock(&qp->s_lock);
1128f931551bSRalph Campbell 
1129f931551bSRalph Campbell 	qib_put_txreq(tx);
1130f931551bSRalph Campbell }
1131f931551bSRalph Campbell 
11327c2e11feSDennis Dalessandro static int wait_kmem(struct qib_ibdev *dev, struct rvt_qp *qp)
1133f931551bSRalph Campbell {
1134ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv = qp->priv;
1135f931551bSRalph Campbell 	unsigned long flags;
1136f931551bSRalph Campbell 	int ret = 0;
1137f931551bSRalph Campbell 
1138f931551bSRalph Campbell 	spin_lock_irqsave(&qp->s_lock, flags);
1139f931551bSRalph Campbell 	if (ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK) {
1140cd18201fSHarish Chegondi 		spin_lock(&dev->rdi.pending_lock);
1141ffc26907SDennis Dalessandro 		if (list_empty(&priv->iowait)) {
1142f931551bSRalph Campbell 			if (list_empty(&dev->memwait))
1143f931551bSRalph Campbell 				mod_timer(&dev->mem_timer, jiffies + 1);
114401ba79d4SHarish Chegondi 			qp->s_flags |= RVT_S_WAIT_KMEM;
1145ffc26907SDennis Dalessandro 			list_add_tail(&priv->iowait, &dev->memwait);
1146f931551bSRalph Campbell 		}
1147cd18201fSHarish Chegondi 		spin_unlock(&dev->rdi.pending_lock);
114801ba79d4SHarish Chegondi 		qp->s_flags &= ~RVT_S_BUSY;
1149f931551bSRalph Campbell 		ret = -EBUSY;
1150f931551bSRalph Campbell 	}
1151f931551bSRalph Campbell 	spin_unlock_irqrestore(&qp->s_lock, flags);
1152f931551bSRalph Campbell 
1153f931551bSRalph Campbell 	return ret;
1154f931551bSRalph Campbell }
1155f931551bSRalph Campbell 
11567c2e11feSDennis Dalessandro static int qib_verbs_send_dma(struct rvt_qp *qp, struct qib_ib_header *hdr,
11577c2e11feSDennis Dalessandro 			      u32 hdrwords, struct rvt_sge_state *ss, u32 len,
1158f931551bSRalph Campbell 			      u32 plen, u32 dwords)
1159f931551bSRalph Campbell {
1160ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv = qp->priv;
1161f931551bSRalph Campbell 	struct qib_ibdev *dev = to_idev(qp->ibqp.device);
1162f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_dev(dev);
1163f931551bSRalph Campbell 	struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
1164f931551bSRalph Campbell 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1165f931551bSRalph Campbell 	struct qib_verbs_txreq *tx;
1166f931551bSRalph Campbell 	struct qib_pio_header *phdr;
1167f931551bSRalph Campbell 	u32 control;
1168f931551bSRalph Campbell 	u32 ndesc;
1169f931551bSRalph Campbell 	int ret;
1170f931551bSRalph Campbell 
1171ffc26907SDennis Dalessandro 	tx = priv->s_tx;
1172f931551bSRalph Campbell 	if (tx) {
1173ffc26907SDennis Dalessandro 		priv->s_tx = NULL;
1174f931551bSRalph Campbell 		/* resend previously constructed packet */
1175f931551bSRalph Campbell 		ret = qib_sdma_verbs_send(ppd, tx->ss, tx->dwords, tx);
1176f931551bSRalph Campbell 		goto bail;
1177f931551bSRalph Campbell 	}
1178f931551bSRalph Campbell 
117948947109SMike Marciniszyn 	tx = get_txreq(dev, qp);
118048947109SMike Marciniszyn 	if (IS_ERR(tx))
118148947109SMike Marciniszyn 		goto bail_tx;
1182f931551bSRalph Campbell 
1183f931551bSRalph Campbell 	control = dd->f_setpbc_control(ppd, plen, qp->s_srate,
1184f931551bSRalph Campbell 				       be16_to_cpu(hdr->lrh[0]) >> 12);
1185f931551bSRalph Campbell 	tx->qp = qp;
1186f931551bSRalph Campbell 	atomic_inc(&qp->refcount);
1187f931551bSRalph Campbell 	tx->wqe = qp->s_wqe;
1188f931551bSRalph Campbell 	tx->mr = qp->s_rdma_mr;
1189f931551bSRalph Campbell 	if (qp->s_rdma_mr)
1190f931551bSRalph Campbell 		qp->s_rdma_mr = NULL;
1191f931551bSRalph Campbell 	tx->txreq.callback = sdma_complete;
1192f931551bSRalph Campbell 	if (dd->flags & QIB_HAS_SDMA_TIMEOUT)
1193f931551bSRalph Campbell 		tx->txreq.flags = QIB_SDMA_TXREQ_F_HEADTOHOST;
1194f931551bSRalph Campbell 	else
1195f931551bSRalph Campbell 		tx->txreq.flags = QIB_SDMA_TXREQ_F_INTREQ;
1196f931551bSRalph Campbell 	if (plen + 1 > dd->piosize2kmax_dwords)
1197f931551bSRalph Campbell 		tx->txreq.flags |= QIB_SDMA_TXREQ_F_USELARGEBUF;
1198f931551bSRalph Campbell 
1199f931551bSRalph Campbell 	if (len) {
1200f931551bSRalph Campbell 		/*
1201f931551bSRalph Campbell 		 * Don't try to DMA if it takes more descriptors than
1202f931551bSRalph Campbell 		 * the queue holds.
1203f931551bSRalph Campbell 		 */
1204f931551bSRalph Campbell 		ndesc = qib_count_sge(ss, len);
1205f931551bSRalph Campbell 		if (ndesc >= ppd->sdma_descq_cnt)
1206f931551bSRalph Campbell 			ndesc = 0;
1207f931551bSRalph Campbell 	} else
1208f931551bSRalph Campbell 		ndesc = 1;
1209f931551bSRalph Campbell 	if (ndesc) {
1210f931551bSRalph Campbell 		phdr = &dev->pio_hdrs[tx->hdr_inx];
1211f931551bSRalph Campbell 		phdr->pbc[0] = cpu_to_le32(plen);
1212f931551bSRalph Campbell 		phdr->pbc[1] = cpu_to_le32(control);
1213f931551bSRalph Campbell 		memcpy(&phdr->hdr, hdr, hdrwords << 2);
1214f931551bSRalph Campbell 		tx->txreq.flags |= QIB_SDMA_TXREQ_F_FREEDESC;
1215f931551bSRalph Campbell 		tx->txreq.sg_count = ndesc;
1216f931551bSRalph Campbell 		tx->txreq.addr = dev->pio_hdrs_phys +
1217f931551bSRalph Campbell 			tx->hdr_inx * sizeof(struct qib_pio_header);
1218f931551bSRalph Campbell 		tx->hdr_dwords = hdrwords + 2; /* add PBC length */
1219f931551bSRalph Campbell 		ret = qib_sdma_verbs_send(ppd, ss, dwords, tx);
1220f931551bSRalph Campbell 		goto bail;
1221f931551bSRalph Campbell 	}
1222f931551bSRalph Campbell 
1223f931551bSRalph Campbell 	/* Allocate a buffer and copy the header and payload to it. */
1224f931551bSRalph Campbell 	tx->hdr_dwords = plen + 1;
1225f931551bSRalph Campbell 	phdr = kmalloc(tx->hdr_dwords << 2, GFP_ATOMIC);
1226f931551bSRalph Campbell 	if (!phdr)
1227f931551bSRalph Campbell 		goto err_tx;
1228f931551bSRalph Campbell 	phdr->pbc[0] = cpu_to_le32(plen);
1229f931551bSRalph Campbell 	phdr->pbc[1] = cpu_to_le32(control);
1230f931551bSRalph Campbell 	memcpy(&phdr->hdr, hdr, hdrwords << 2);
1231f931551bSRalph Campbell 	qib_copy_from_sge((u32 *) &phdr->hdr + hdrwords, ss, len);
1232f931551bSRalph Campbell 
1233f931551bSRalph Campbell 	tx->txreq.addr = dma_map_single(&dd->pcidev->dev, phdr,
1234f931551bSRalph Campbell 					tx->hdr_dwords << 2, DMA_TO_DEVICE);
1235f931551bSRalph Campbell 	if (dma_mapping_error(&dd->pcidev->dev, tx->txreq.addr))
1236f931551bSRalph Campbell 		goto map_err;
1237f931551bSRalph Campbell 	tx->align_buf = phdr;
1238f931551bSRalph Campbell 	tx->txreq.flags |= QIB_SDMA_TXREQ_F_FREEBUF;
1239f931551bSRalph Campbell 	tx->txreq.sg_count = 1;
1240f931551bSRalph Campbell 	ret = qib_sdma_verbs_send(ppd, NULL, 0, tx);
1241f931551bSRalph Campbell 	goto unaligned;
1242f931551bSRalph Campbell 
1243f931551bSRalph Campbell map_err:
1244f931551bSRalph Campbell 	kfree(phdr);
1245f931551bSRalph Campbell err_tx:
1246f931551bSRalph Campbell 	qib_put_txreq(tx);
1247f931551bSRalph Campbell 	ret = wait_kmem(dev, qp);
1248f931551bSRalph Campbell unaligned:
1249f24a6d48SHarish Chegondi 	ibp->rvp.n_unaligned++;
1250f931551bSRalph Campbell bail:
1251f931551bSRalph Campbell 	return ret;
125248947109SMike Marciniszyn bail_tx:
125348947109SMike Marciniszyn 	ret = PTR_ERR(tx);
125448947109SMike Marciniszyn 	goto bail;
1255f931551bSRalph Campbell }
1256f931551bSRalph Campbell 
1257f931551bSRalph Campbell /*
1258f931551bSRalph Campbell  * If we are now in the error state, return zero to flush the
1259f931551bSRalph Campbell  * send work request.
1260f931551bSRalph Campbell  */
12617c2e11feSDennis Dalessandro static int no_bufs_available(struct rvt_qp *qp)
1262f931551bSRalph Campbell {
1263ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv = qp->priv;
1264f931551bSRalph Campbell 	struct qib_ibdev *dev = to_idev(qp->ibqp.device);
1265f931551bSRalph Campbell 	struct qib_devdata *dd;
1266f931551bSRalph Campbell 	unsigned long flags;
1267f931551bSRalph Campbell 	int ret = 0;
1268f931551bSRalph Campbell 
1269f931551bSRalph Campbell 	/*
1270f931551bSRalph Campbell 	 * Note that as soon as want_buffer() is called and
1271f931551bSRalph Campbell 	 * possibly before it returns, qib_ib_piobufavail()
1272f931551bSRalph Campbell 	 * could be called. Therefore, put QP on the I/O wait list before
1273f931551bSRalph Campbell 	 * enabling the PIO avail interrupt.
1274f931551bSRalph Campbell 	 */
1275f931551bSRalph Campbell 	spin_lock_irqsave(&qp->s_lock, flags);
1276f931551bSRalph Campbell 	if (ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK) {
1277cd18201fSHarish Chegondi 		spin_lock(&dev->rdi.pending_lock);
1278ffc26907SDennis Dalessandro 		if (list_empty(&priv->iowait)) {
1279f931551bSRalph Campbell 			dev->n_piowait++;
128001ba79d4SHarish Chegondi 			qp->s_flags |= RVT_S_WAIT_PIO;
1281ffc26907SDennis Dalessandro 			list_add_tail(&priv->iowait, &dev->piowait);
1282f931551bSRalph Campbell 			dd = dd_from_dev(dev);
1283f931551bSRalph Campbell 			dd->f_wantpiobuf_intr(dd, 1);
1284f931551bSRalph Campbell 		}
1285cd18201fSHarish Chegondi 		spin_unlock(&dev->rdi.pending_lock);
128601ba79d4SHarish Chegondi 		qp->s_flags &= ~RVT_S_BUSY;
1287f931551bSRalph Campbell 		ret = -EBUSY;
1288f931551bSRalph Campbell 	}
1289f931551bSRalph Campbell 	spin_unlock_irqrestore(&qp->s_lock, flags);
1290f931551bSRalph Campbell 	return ret;
1291f931551bSRalph Campbell }
1292f931551bSRalph Campbell 
12937c2e11feSDennis Dalessandro static int qib_verbs_send_pio(struct rvt_qp *qp, struct qib_ib_header *ibhdr,
12947c2e11feSDennis Dalessandro 			      u32 hdrwords, struct rvt_sge_state *ss, u32 len,
1295f931551bSRalph Campbell 			      u32 plen, u32 dwords)
1296f931551bSRalph Campbell {
1297f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
1298f931551bSRalph Campbell 	struct qib_pportdata *ppd = dd->pport + qp->port_num - 1;
1299f931551bSRalph Campbell 	u32 *hdr = (u32 *) ibhdr;
1300f931551bSRalph Campbell 	u32 __iomem *piobuf_orig;
1301f931551bSRalph Campbell 	u32 __iomem *piobuf;
1302f931551bSRalph Campbell 	u64 pbc;
1303f931551bSRalph Campbell 	unsigned long flags;
1304f931551bSRalph Campbell 	unsigned flush_wc;
1305f931551bSRalph Campbell 	u32 control;
1306f931551bSRalph Campbell 	u32 pbufn;
1307f931551bSRalph Campbell 
1308f931551bSRalph Campbell 	control = dd->f_setpbc_control(ppd, plen, qp->s_srate,
1309f931551bSRalph Campbell 		be16_to_cpu(ibhdr->lrh[0]) >> 12);
1310f931551bSRalph Campbell 	pbc = ((u64) control << 32) | plen;
1311f931551bSRalph Campbell 	piobuf = dd->f_getsendbuf(ppd, pbc, &pbufn);
1312f931551bSRalph Campbell 	if (unlikely(piobuf == NULL))
1313f931551bSRalph Campbell 		return no_bufs_available(qp);
1314f931551bSRalph Campbell 
1315f931551bSRalph Campbell 	/*
1316f931551bSRalph Campbell 	 * Write the pbc.
1317f931551bSRalph Campbell 	 * We have to flush after the PBC for correctness on some cpus
1318f931551bSRalph Campbell 	 * or WC buffer can be written out of order.
1319f931551bSRalph Campbell 	 */
1320f931551bSRalph Campbell 	writeq(pbc, piobuf);
1321f931551bSRalph Campbell 	piobuf_orig = piobuf;
1322f931551bSRalph Campbell 	piobuf += 2;
1323f931551bSRalph Campbell 
1324f931551bSRalph Campbell 	flush_wc = dd->flags & QIB_PIO_FLUSH_WC;
1325f931551bSRalph Campbell 	if (len == 0) {
1326f931551bSRalph Campbell 		/*
1327f931551bSRalph Campbell 		 * If there is just the header portion, must flush before
1328f931551bSRalph Campbell 		 * writing last word of header for correctness, and after
1329f931551bSRalph Campbell 		 * the last header word (trigger word).
1330f931551bSRalph Campbell 		 */
1331f931551bSRalph Campbell 		if (flush_wc) {
1332f931551bSRalph Campbell 			qib_flush_wc();
1333f931551bSRalph Campbell 			qib_pio_copy(piobuf, hdr, hdrwords - 1);
1334f931551bSRalph Campbell 			qib_flush_wc();
1335f931551bSRalph Campbell 			__raw_writel(hdr[hdrwords - 1], piobuf + hdrwords - 1);
1336f931551bSRalph Campbell 			qib_flush_wc();
1337f931551bSRalph Campbell 		} else
1338f931551bSRalph Campbell 			qib_pio_copy(piobuf, hdr, hdrwords);
1339f931551bSRalph Campbell 		goto done;
1340f931551bSRalph Campbell 	}
1341f931551bSRalph Campbell 
1342f931551bSRalph Campbell 	if (flush_wc)
1343f931551bSRalph Campbell 		qib_flush_wc();
1344f931551bSRalph Campbell 	qib_pio_copy(piobuf, hdr, hdrwords);
1345f931551bSRalph Campbell 	piobuf += hdrwords;
1346f931551bSRalph Campbell 
1347f931551bSRalph Campbell 	/* The common case is aligned and contained in one segment. */
1348f931551bSRalph Campbell 	if (likely(ss->num_sge == 1 && len <= ss->sge.length &&
1349f931551bSRalph Campbell 		   !((unsigned long)ss->sge.vaddr & (sizeof(u32) - 1)))) {
1350f931551bSRalph Campbell 		u32 *addr = (u32 *) ss->sge.vaddr;
1351f931551bSRalph Campbell 
1352f931551bSRalph Campbell 		/* Update address before sending packet. */
1353f931551bSRalph Campbell 		update_sge(ss, len);
1354f931551bSRalph Campbell 		if (flush_wc) {
1355f931551bSRalph Campbell 			qib_pio_copy(piobuf, addr, dwords - 1);
1356f931551bSRalph Campbell 			/* must flush early everything before trigger word */
1357f931551bSRalph Campbell 			qib_flush_wc();
1358f931551bSRalph Campbell 			__raw_writel(addr[dwords - 1], piobuf + dwords - 1);
1359f931551bSRalph Campbell 			/* be sure trigger word is written */
1360f931551bSRalph Campbell 			qib_flush_wc();
1361f931551bSRalph Campbell 		} else
1362f931551bSRalph Campbell 			qib_pio_copy(piobuf, addr, dwords);
1363f931551bSRalph Campbell 		goto done;
1364f931551bSRalph Campbell 	}
1365f931551bSRalph Campbell 	copy_io(piobuf, ss, len, flush_wc);
1366f931551bSRalph Campbell done:
1367f931551bSRalph Campbell 	if (dd->flags & QIB_USE_SPCL_TRIG) {
1368f931551bSRalph Campbell 		u32 spcl_off = (pbufn >= dd->piobcnt2k) ? 2047 : 1023;
1369da12c1f6SMike Marciniszyn 
1370f931551bSRalph Campbell 		qib_flush_wc();
1371f931551bSRalph Campbell 		__raw_writel(0xaebecede, piobuf_orig + spcl_off);
1372f931551bSRalph Campbell 	}
1373f931551bSRalph Campbell 	qib_sendbuf_done(dd, pbufn);
1374f931551bSRalph Campbell 	if (qp->s_rdma_mr) {
13757c2e11feSDennis Dalessandro 		rvt_put_mr(qp->s_rdma_mr);
1376f931551bSRalph Campbell 		qp->s_rdma_mr = NULL;
1377f931551bSRalph Campbell 	}
1378f931551bSRalph Campbell 	if (qp->s_wqe) {
1379f931551bSRalph Campbell 		spin_lock_irqsave(&qp->s_lock, flags);
1380f931551bSRalph Campbell 		qib_send_complete(qp, qp->s_wqe, IB_WC_SUCCESS);
1381f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
1382f931551bSRalph Campbell 	} else if (qp->ibqp.qp_type == IB_QPT_RC) {
1383f931551bSRalph Campbell 		spin_lock_irqsave(&qp->s_lock, flags);
1384f931551bSRalph Campbell 		qib_rc_send_complete(qp, ibhdr);
1385f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
1386f931551bSRalph Campbell 	}
1387f931551bSRalph Campbell 	return 0;
1388f931551bSRalph Campbell }
1389f931551bSRalph Campbell 
1390f931551bSRalph Campbell /**
1391f931551bSRalph Campbell  * qib_verbs_send - send a packet
1392f931551bSRalph Campbell  * @qp: the QP to send on
1393f931551bSRalph Campbell  * @hdr: the packet header
1394f931551bSRalph Campbell  * @hdrwords: the number of 32-bit words in the header
1395f931551bSRalph Campbell  * @ss: the SGE to send
1396f931551bSRalph Campbell  * @len: the length of the packet in bytes
1397f931551bSRalph Campbell  *
1398f931551bSRalph Campbell  * Return zero if packet is sent or queued OK.
139901ba79d4SHarish Chegondi  * Return non-zero and clear qp->s_flags RVT_S_BUSY otherwise.
1400f931551bSRalph Campbell  */
14017c2e11feSDennis Dalessandro int qib_verbs_send(struct rvt_qp *qp, struct qib_ib_header *hdr,
14027c2e11feSDennis Dalessandro 		   u32 hdrwords, struct rvt_sge_state *ss, u32 len)
1403f931551bSRalph Campbell {
1404f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
1405f931551bSRalph Campbell 	u32 plen;
1406f931551bSRalph Campbell 	int ret;
1407f931551bSRalph Campbell 	u32 dwords = (len + 3) >> 2;
1408f931551bSRalph Campbell 
1409f931551bSRalph Campbell 	/*
1410f931551bSRalph Campbell 	 * Calculate the send buffer trigger address.
1411f931551bSRalph Campbell 	 * The +1 counts for the pbc control dword following the pbc length.
1412f931551bSRalph Campbell 	 */
1413f931551bSRalph Campbell 	plen = hdrwords + dwords + 1;
1414f931551bSRalph Campbell 
1415f931551bSRalph Campbell 	/*
1416f931551bSRalph Campbell 	 * VL15 packets (IB_QPT_SMI) will always use PIO, so we
1417f931551bSRalph Campbell 	 * can defer SDMA restart until link goes ACTIVE without
1418f931551bSRalph Campbell 	 * worrying about just how we got there.
1419f931551bSRalph Campbell 	 */
1420f931551bSRalph Campbell 	if (qp->ibqp.qp_type == IB_QPT_SMI ||
1421f931551bSRalph Campbell 	    !(dd->flags & QIB_HAS_SEND_DMA))
1422f931551bSRalph Campbell 		ret = qib_verbs_send_pio(qp, hdr, hdrwords, ss, len,
1423f931551bSRalph Campbell 					 plen, dwords);
1424f931551bSRalph Campbell 	else
1425f931551bSRalph Campbell 		ret = qib_verbs_send_dma(qp, hdr, hdrwords, ss, len,
1426f931551bSRalph Campbell 					 plen, dwords);
1427f931551bSRalph Campbell 
1428f931551bSRalph Campbell 	return ret;
1429f931551bSRalph Campbell }
1430f931551bSRalph Campbell 
1431f931551bSRalph Campbell int qib_snapshot_counters(struct qib_pportdata *ppd, u64 *swords,
1432f931551bSRalph Campbell 			  u64 *rwords, u64 *spkts, u64 *rpkts,
1433f931551bSRalph Campbell 			  u64 *xmit_wait)
1434f931551bSRalph Campbell {
1435f931551bSRalph Campbell 	int ret;
1436f931551bSRalph Campbell 	struct qib_devdata *dd = ppd->dd;
1437f931551bSRalph Campbell 
1438f931551bSRalph Campbell 	if (!(dd->flags & QIB_PRESENT)) {
1439f931551bSRalph Campbell 		/* no hardware, freeze, etc. */
1440f931551bSRalph Campbell 		ret = -EINVAL;
1441f931551bSRalph Campbell 		goto bail;
1442f931551bSRalph Campbell 	}
1443f931551bSRalph Campbell 	*swords = dd->f_portcntr(ppd, QIBPORTCNTR_WORDSEND);
1444f931551bSRalph Campbell 	*rwords = dd->f_portcntr(ppd, QIBPORTCNTR_WORDRCV);
1445f931551bSRalph Campbell 	*spkts = dd->f_portcntr(ppd, QIBPORTCNTR_PKTSEND);
1446f931551bSRalph Campbell 	*rpkts = dd->f_portcntr(ppd, QIBPORTCNTR_PKTRCV);
1447f931551bSRalph Campbell 	*xmit_wait = dd->f_portcntr(ppd, QIBPORTCNTR_SENDSTALL);
1448f931551bSRalph Campbell 
1449f931551bSRalph Campbell 	ret = 0;
1450f931551bSRalph Campbell 
1451f931551bSRalph Campbell bail:
1452f931551bSRalph Campbell 	return ret;
1453f931551bSRalph Campbell }
1454f931551bSRalph Campbell 
1455f931551bSRalph Campbell /**
1456f931551bSRalph Campbell  * qib_get_counters - get various chip counters
1457f931551bSRalph Campbell  * @dd: the qlogic_ib device
1458f931551bSRalph Campbell  * @cntrs: counters are placed here
1459f931551bSRalph Campbell  *
1460f931551bSRalph Campbell  * Return the counters needed by recv_pma_get_portcounters().
1461f931551bSRalph Campbell  */
1462f931551bSRalph Campbell int qib_get_counters(struct qib_pportdata *ppd,
1463f931551bSRalph Campbell 		     struct qib_verbs_counters *cntrs)
1464f931551bSRalph Campbell {
1465f931551bSRalph Campbell 	int ret;
1466f931551bSRalph Campbell 
1467f931551bSRalph Campbell 	if (!(ppd->dd->flags & QIB_PRESENT)) {
1468f931551bSRalph Campbell 		/* no hardware, freeze, etc. */
1469f931551bSRalph Campbell 		ret = -EINVAL;
1470f931551bSRalph Campbell 		goto bail;
1471f931551bSRalph Campbell 	}
1472f931551bSRalph Campbell 	cntrs->symbol_error_counter =
1473f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBSYMBOLERR);
1474f931551bSRalph Campbell 	cntrs->link_error_recovery_counter =
1475f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBLINKERRRECOV);
1476f931551bSRalph Campbell 	/*
1477f931551bSRalph Campbell 	 * The link downed counter counts when the other side downs the
1478f931551bSRalph Campbell 	 * connection.  We add in the number of times we downed the link
1479f931551bSRalph Campbell 	 * due to local link integrity errors to compensate.
1480f931551bSRalph Campbell 	 */
1481f931551bSRalph Campbell 	cntrs->link_downed_counter =
1482f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBLINKDOWN);
1483f931551bSRalph Campbell 	cntrs->port_rcv_errors =
1484f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXDROPPKT) +
1485f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RCVOVFL) +
1486f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERR_RLEN) +
1487f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_INVALIDRLEN) +
1488f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRLINK) +
1489f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRICRC) +
1490f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRVCRC) +
1491f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRLPCRC) +
1492f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_BADFORMAT);
1493f931551bSRalph Campbell 	cntrs->port_rcv_errors +=
1494f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXLOCALPHYERR);
1495f931551bSRalph Campbell 	cntrs->port_rcv_errors +=
1496f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXVLERR);
1497f931551bSRalph Campbell 	cntrs->port_rcv_remphys_errors =
1498f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RCVEBP);
1499f931551bSRalph Campbell 	cntrs->port_xmit_discards =
1500f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_UNSUPVL);
1501f931551bSRalph Campbell 	cntrs->port_xmit_data = ppd->dd->f_portcntr(ppd,
1502f931551bSRalph Campbell 			QIBPORTCNTR_WORDSEND);
1503f931551bSRalph Campbell 	cntrs->port_rcv_data = ppd->dd->f_portcntr(ppd,
1504f931551bSRalph Campbell 			QIBPORTCNTR_WORDRCV);
1505f931551bSRalph Campbell 	cntrs->port_xmit_packets = ppd->dd->f_portcntr(ppd,
1506f931551bSRalph Campbell 			QIBPORTCNTR_PKTSEND);
1507f931551bSRalph Campbell 	cntrs->port_rcv_packets = ppd->dd->f_portcntr(ppd,
1508f931551bSRalph Campbell 			QIBPORTCNTR_PKTRCV);
1509f931551bSRalph Campbell 	cntrs->local_link_integrity_errors =
1510f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_LLI);
1511f931551bSRalph Campbell 	cntrs->excessive_buffer_overrun_errors =
1512f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_EXCESSBUFOVFL);
1513f931551bSRalph Campbell 	cntrs->vl15_dropped =
1514f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_VL15PKTDROP);
1515f931551bSRalph Campbell 
1516f931551bSRalph Campbell 	ret = 0;
1517f931551bSRalph Campbell 
1518f931551bSRalph Campbell bail:
1519f931551bSRalph Campbell 	return ret;
1520f931551bSRalph Campbell }
1521f931551bSRalph Campbell 
1522f931551bSRalph Campbell /**
1523f931551bSRalph Campbell  * qib_ib_piobufavail - callback when a PIO buffer is available
1524f931551bSRalph Campbell  * @dd: the device pointer
1525f931551bSRalph Campbell  *
1526f931551bSRalph Campbell  * This is called from qib_intr() at interrupt level when a PIO buffer is
1527f931551bSRalph Campbell  * available after qib_verbs_send() returned an error that no buffers were
1528f931551bSRalph Campbell  * available. Disable the interrupt if there are no more QPs waiting.
1529f931551bSRalph Campbell  */
1530f931551bSRalph Campbell void qib_ib_piobufavail(struct qib_devdata *dd)
1531f931551bSRalph Campbell {
1532f931551bSRalph Campbell 	struct qib_ibdev *dev = &dd->verbs_dev;
1533f931551bSRalph Campbell 	struct list_head *list;
15347c2e11feSDennis Dalessandro 	struct rvt_qp *qps[5];
15357c2e11feSDennis Dalessandro 	struct rvt_qp *qp;
1536f931551bSRalph Campbell 	unsigned long flags;
1537f931551bSRalph Campbell 	unsigned i, n;
1538ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv;
1539f931551bSRalph Campbell 
1540f931551bSRalph Campbell 	list = &dev->piowait;
1541f931551bSRalph Campbell 	n = 0;
1542f931551bSRalph Campbell 
1543f931551bSRalph Campbell 	/*
1544f931551bSRalph Campbell 	 * Note: checking that the piowait list is empty and clearing
1545f931551bSRalph Campbell 	 * the buffer available interrupt needs to be atomic or we
1546f931551bSRalph Campbell 	 * could end up with QPs on the wait list with the interrupt
1547f931551bSRalph Campbell 	 * disabled.
1548f931551bSRalph Campbell 	 */
1549cd18201fSHarish Chegondi 	spin_lock_irqsave(&dev->rdi.pending_lock, flags);
1550f931551bSRalph Campbell 	while (!list_empty(list)) {
1551f931551bSRalph Campbell 		if (n == ARRAY_SIZE(qps))
1552f931551bSRalph Campbell 			goto full;
1553ffc26907SDennis Dalessandro 		priv = list_entry(list->next, struct qib_qp_priv, iowait);
1554ffc26907SDennis Dalessandro 		qp = priv->owner;
1555ffc26907SDennis Dalessandro 		list_del_init(&priv->iowait);
1556f931551bSRalph Campbell 		atomic_inc(&qp->refcount);
1557f931551bSRalph Campbell 		qps[n++] = qp;
1558f931551bSRalph Campbell 	}
1559f931551bSRalph Campbell 	dd->f_wantpiobuf_intr(dd, 0);
1560f931551bSRalph Campbell full:
1561cd18201fSHarish Chegondi 	spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
1562f931551bSRalph Campbell 
1563f931551bSRalph Campbell 	for (i = 0; i < n; i++) {
1564f931551bSRalph Campbell 		qp = qps[i];
1565f931551bSRalph Campbell 
1566f931551bSRalph Campbell 		spin_lock_irqsave(&qp->s_lock, flags);
156701ba79d4SHarish Chegondi 		if (qp->s_flags & RVT_S_WAIT_PIO) {
156801ba79d4SHarish Chegondi 			qp->s_flags &= ~RVT_S_WAIT_PIO;
1569f931551bSRalph Campbell 			qib_schedule_send(qp);
1570f931551bSRalph Campbell 		}
1571f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
1572f931551bSRalph Campbell 
1573f931551bSRalph Campbell 		/* Notify qib_destroy_qp() if it is waiting. */
1574f931551bSRalph Campbell 		if (atomic_dec_and_test(&qp->refcount))
1575f931551bSRalph Campbell 			wake_up(&qp->wait);
1576f931551bSRalph Campbell 	}
1577f931551bSRalph Campbell }
1578f931551bSRalph Campbell 
1579f931551bSRalph Campbell static int qib_query_port(struct ib_device *ibdev, u8 port,
1580f931551bSRalph Campbell 			  struct ib_port_attr *props)
1581f931551bSRalph Campbell {
1582f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_ibdev(ibdev);
1583f931551bSRalph Campbell 	struct qib_ibport *ibp = to_iport(ibdev, port);
1584f931551bSRalph Campbell 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1585f931551bSRalph Campbell 	enum ib_mtu mtu;
1586f931551bSRalph Campbell 	u16 lid = ppd->lid;
1587f931551bSRalph Campbell 
1588f931551bSRalph Campbell 	memset(props, 0, sizeof(*props));
1589f931551bSRalph Campbell 	props->lid = lid ? lid : be16_to_cpu(IB_LID_PERMISSIVE);
1590f931551bSRalph Campbell 	props->lmc = ppd->lmc;
1591f24a6d48SHarish Chegondi 	props->sm_lid = ibp->rvp.sm_lid;
1592f24a6d48SHarish Chegondi 	props->sm_sl = ibp->rvp.sm_sl;
1593f931551bSRalph Campbell 	props->state = dd->f_iblink_state(ppd->lastibcstat);
1594f931551bSRalph Campbell 	props->phys_state = dd->f_ibphys_portstate(ppd->lastibcstat);
1595f24a6d48SHarish Chegondi 	props->port_cap_flags = ibp->rvp.port_cap_flags;
1596f931551bSRalph Campbell 	props->gid_tbl_len = QIB_GUIDS_PER_PORT;
1597f931551bSRalph Campbell 	props->max_msg_sz = 0x80000000;
1598f931551bSRalph Campbell 	props->pkey_tbl_len = qib_get_npkeys(dd);
1599f24a6d48SHarish Chegondi 	props->bad_pkey_cntr = ibp->rvp.pkey_violations;
1600f24a6d48SHarish Chegondi 	props->qkey_viol_cntr = ibp->rvp.qkey_violations;
1601f931551bSRalph Campbell 	props->active_width = ppd->link_width_active;
1602f931551bSRalph Campbell 	/* See rate_show() */
1603f931551bSRalph Campbell 	props->active_speed = ppd->link_speed_active;
1604f931551bSRalph Campbell 	props->max_vl_num = qib_num_vls(ppd->vls_supported);
1605f931551bSRalph Campbell 	props->init_type_reply = 0;
1606f931551bSRalph Campbell 
1607f931551bSRalph Campbell 	props->max_mtu = qib_ibmtu ? qib_ibmtu : IB_MTU_4096;
1608f931551bSRalph Campbell 	switch (ppd->ibmtu) {
1609f931551bSRalph Campbell 	case 4096:
1610f931551bSRalph Campbell 		mtu = IB_MTU_4096;
1611f931551bSRalph Campbell 		break;
1612f931551bSRalph Campbell 	case 2048:
1613f931551bSRalph Campbell 		mtu = IB_MTU_2048;
1614f931551bSRalph Campbell 		break;
1615f931551bSRalph Campbell 	case 1024:
1616f931551bSRalph Campbell 		mtu = IB_MTU_1024;
1617f931551bSRalph Campbell 		break;
1618f931551bSRalph Campbell 	case 512:
1619f931551bSRalph Campbell 		mtu = IB_MTU_512;
1620f931551bSRalph Campbell 		break;
1621f931551bSRalph Campbell 	case 256:
1622f931551bSRalph Campbell 		mtu = IB_MTU_256;
1623f931551bSRalph Campbell 		break;
1624f931551bSRalph Campbell 	default:
1625f931551bSRalph Campbell 		mtu = IB_MTU_2048;
1626f931551bSRalph Campbell 	}
1627f931551bSRalph Campbell 	props->active_mtu = mtu;
1628f24a6d48SHarish Chegondi 	props->subnet_timeout = ibp->rvp.subnet_timeout;
1629f931551bSRalph Campbell 
1630f931551bSRalph Campbell 	return 0;
1631f931551bSRalph Campbell }
1632f931551bSRalph Campbell 
1633f931551bSRalph Campbell static int qib_modify_device(struct ib_device *device,
1634f931551bSRalph Campbell 			     int device_modify_mask,
1635f931551bSRalph Campbell 			     struct ib_device_modify *device_modify)
1636f931551bSRalph Campbell {
1637f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_ibdev(device);
1638f931551bSRalph Campbell 	unsigned i;
1639f931551bSRalph Campbell 	int ret;
1640f931551bSRalph Campbell 
1641f931551bSRalph Campbell 	if (device_modify_mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
1642f931551bSRalph Campbell 				   IB_DEVICE_MODIFY_NODE_DESC)) {
1643f931551bSRalph Campbell 		ret = -EOPNOTSUPP;
1644f931551bSRalph Campbell 		goto bail;
1645f931551bSRalph Campbell 	}
1646f931551bSRalph Campbell 
1647f931551bSRalph Campbell 	if (device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC) {
1648f931551bSRalph Campbell 		memcpy(device->node_desc, device_modify->node_desc, 64);
1649f931551bSRalph Campbell 		for (i = 0; i < dd->num_pports; i++) {
1650f931551bSRalph Campbell 			struct qib_ibport *ibp = &dd->pport[i].ibport_data;
1651f931551bSRalph Campbell 
1652f931551bSRalph Campbell 			qib_node_desc_chg(ibp);
1653f931551bSRalph Campbell 		}
1654f931551bSRalph Campbell 	}
1655f931551bSRalph Campbell 
1656f931551bSRalph Campbell 	if (device_modify_mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID) {
1657f931551bSRalph Campbell 		ib_qib_sys_image_guid =
1658f931551bSRalph Campbell 			cpu_to_be64(device_modify->sys_image_guid);
1659f931551bSRalph Campbell 		for (i = 0; i < dd->num_pports; i++) {
1660f931551bSRalph Campbell 			struct qib_ibport *ibp = &dd->pport[i].ibport_data;
1661f931551bSRalph Campbell 
1662f931551bSRalph Campbell 			qib_sys_guid_chg(ibp);
1663f931551bSRalph Campbell 		}
1664f931551bSRalph Campbell 	}
1665f931551bSRalph Campbell 
1666f931551bSRalph Campbell 	ret = 0;
1667f931551bSRalph Campbell 
1668f931551bSRalph Campbell bail:
1669f931551bSRalph Campbell 	return ret;
1670f931551bSRalph Campbell }
1671f931551bSRalph Campbell 
1672f931551bSRalph Campbell static int qib_modify_port(struct ib_device *ibdev, u8 port,
1673f931551bSRalph Campbell 			   int port_modify_mask, struct ib_port_modify *props)
1674f931551bSRalph Campbell {
1675f931551bSRalph Campbell 	struct qib_ibport *ibp = to_iport(ibdev, port);
1676f931551bSRalph Campbell 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1677f931551bSRalph Campbell 
1678f24a6d48SHarish Chegondi 	ibp->rvp.port_cap_flags |= props->set_port_cap_mask;
1679f24a6d48SHarish Chegondi 	ibp->rvp.port_cap_flags &= ~props->clr_port_cap_mask;
1680f931551bSRalph Campbell 	if (props->set_port_cap_mask || props->clr_port_cap_mask)
1681f931551bSRalph Campbell 		qib_cap_mask_chg(ibp);
1682f931551bSRalph Campbell 	if (port_modify_mask & IB_PORT_SHUTDOWN)
1683f931551bSRalph Campbell 		qib_set_linkstate(ppd, QIB_IB_LINKDOWN);
1684f931551bSRalph Campbell 	if (port_modify_mask & IB_PORT_RESET_QKEY_CNTR)
1685f24a6d48SHarish Chegondi 		ibp->rvp.qkey_violations = 0;
1686f931551bSRalph Campbell 	return 0;
1687f931551bSRalph Campbell }
1688f931551bSRalph Campbell 
1689f931551bSRalph Campbell static int qib_query_gid(struct ib_device *ibdev, u8 port,
1690f931551bSRalph Campbell 			 int index, union ib_gid *gid)
1691f931551bSRalph Campbell {
1692f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_ibdev(ibdev);
1693f931551bSRalph Campbell 	int ret = 0;
1694f931551bSRalph Campbell 
1695f931551bSRalph Campbell 	if (!port || port > dd->num_pports)
1696f931551bSRalph Campbell 		ret = -EINVAL;
1697f931551bSRalph Campbell 	else {
1698f931551bSRalph Campbell 		struct qib_ibport *ibp = to_iport(ibdev, port);
1699f931551bSRalph Campbell 		struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1700f931551bSRalph Campbell 
1701f24a6d48SHarish Chegondi 		gid->global.subnet_prefix = ibp->rvp.gid_prefix;
1702f931551bSRalph Campbell 		if (index == 0)
1703f931551bSRalph Campbell 			gid->global.interface_id = ppd->guid;
1704f931551bSRalph Campbell 		else if (index < QIB_GUIDS_PER_PORT)
1705f931551bSRalph Campbell 			gid->global.interface_id = ibp->guids[index - 1];
1706f931551bSRalph Campbell 		else
1707f931551bSRalph Campbell 			ret = -EINVAL;
1708f931551bSRalph Campbell 	}
1709f931551bSRalph Campbell 
1710f931551bSRalph Campbell 	return ret;
1711f931551bSRalph Campbell }
1712f931551bSRalph Campbell 
1713f931551bSRalph Campbell int qib_check_ah(struct ib_device *ibdev, struct ib_ah_attr *ah_attr)
1714f931551bSRalph Campbell {
1715f931551bSRalph Campbell 	if (ah_attr->sl > 15)
1716f931551bSRalph Campbell 		return -EINVAL;
1717f931551bSRalph Campbell 
171896ab1ac1SDennis Dalessandro 	return 0;
1719f931551bSRalph Campbell }
1720f931551bSRalph Campbell 
17215418a5abSHarish Chegondi static void qib_notify_new_ah(struct ib_device *ibdev,
17225418a5abSHarish Chegondi 			      struct ib_ah_attr *ah_attr,
17235418a5abSHarish Chegondi 			      struct rvt_ah *ah)
17245418a5abSHarish Chegondi {
17255418a5abSHarish Chegondi 	struct qib_ibport *ibp;
17265418a5abSHarish Chegondi 	struct qib_pportdata *ppd;
17275418a5abSHarish Chegondi 
17285418a5abSHarish Chegondi 	/*
17295418a5abSHarish Chegondi 	 * Do not trust reading anything from rvt_ah at this point as it is not
17305418a5abSHarish Chegondi 	 * done being setup. We can however modify things which we need to set.
17315418a5abSHarish Chegondi 	 */
17325418a5abSHarish Chegondi 
17335418a5abSHarish Chegondi 	ibp = to_iport(ibdev, ah_attr->port_num);
17345418a5abSHarish Chegondi 	ppd = ppd_from_ibp(ibp);
17355418a5abSHarish Chegondi 	ah->vl = ibp->sl_to_vl[ah->attr.sl];
17365418a5abSHarish Chegondi 	ah->log_pmtu = ilog2(ppd->ibmtu);
17375418a5abSHarish Chegondi }
17385418a5abSHarish Chegondi 
17391fb9fed6SMike Marciniszyn struct ib_ah *qib_create_qp0_ah(struct qib_ibport *ibp, u16 dlid)
17401fb9fed6SMike Marciniszyn {
17411fb9fed6SMike Marciniszyn 	struct ib_ah_attr attr;
17421fb9fed6SMike Marciniszyn 	struct ib_ah *ah = ERR_PTR(-EINVAL);
17437c2e11feSDennis Dalessandro 	struct rvt_qp *qp0;
17441fb9fed6SMike Marciniszyn 
1745041af0bbSMike Marciniszyn 	memset(&attr, 0, sizeof(attr));
17461fb9fed6SMike Marciniszyn 	attr.dlid = dlid;
17471fb9fed6SMike Marciniszyn 	attr.port_num = ppd_from_ibp(ibp)->port;
17481fb9fed6SMike Marciniszyn 	rcu_read_lock();
1749f24a6d48SHarish Chegondi 	qp0 = rcu_dereference(ibp->rvp.qp[0]);
17501fb9fed6SMike Marciniszyn 	if (qp0)
17511fb9fed6SMike Marciniszyn 		ah = ib_create_ah(qp0->ibqp.pd, &attr);
17521fb9fed6SMike Marciniszyn 	rcu_read_unlock();
17531fb9fed6SMike Marciniszyn 	return ah;
17541fb9fed6SMike Marciniszyn }
17551fb9fed6SMike Marciniszyn 
1756f931551bSRalph Campbell /**
1757f931551bSRalph Campbell  * qib_get_npkeys - return the size of the PKEY table for context 0
1758f931551bSRalph Campbell  * @dd: the qlogic_ib device
1759f931551bSRalph Campbell  */
1760f931551bSRalph Campbell unsigned qib_get_npkeys(struct qib_devdata *dd)
1761f931551bSRalph Campbell {
1762f931551bSRalph Campbell 	return ARRAY_SIZE(dd->rcd[0]->pkeys);
1763f931551bSRalph Campbell }
1764f931551bSRalph Campbell 
1765f931551bSRalph Campbell /*
1766f931551bSRalph Campbell  * Return the indexed PKEY from the port PKEY table.
1767f931551bSRalph Campbell  * No need to validate rcd[ctxt]; the port is setup if we are here.
1768f931551bSRalph Campbell  */
1769f931551bSRalph Campbell unsigned qib_get_pkey(struct qib_ibport *ibp, unsigned index)
1770f931551bSRalph Campbell {
1771f931551bSRalph Campbell 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1772f931551bSRalph Campbell 	struct qib_devdata *dd = ppd->dd;
1773f931551bSRalph Campbell 	unsigned ctxt = ppd->hw_pidx;
1774f931551bSRalph Campbell 	unsigned ret;
1775f931551bSRalph Campbell 
1776f931551bSRalph Campbell 	/* dd->rcd null if mini_init or some init failures */
1777f931551bSRalph Campbell 	if (!dd->rcd || index >= ARRAY_SIZE(dd->rcd[ctxt]->pkeys))
1778f931551bSRalph Campbell 		ret = 0;
1779f931551bSRalph Campbell 	else
1780f931551bSRalph Campbell 		ret = dd->rcd[ctxt]->pkeys[index];
1781f931551bSRalph Campbell 
1782f931551bSRalph Campbell 	return ret;
1783f931551bSRalph Campbell }
1784f931551bSRalph Campbell 
1785f931551bSRalph Campbell static void init_ibport(struct qib_pportdata *ppd)
1786f931551bSRalph Campbell {
1787f931551bSRalph Campbell 	struct qib_verbs_counters cntrs;
1788f931551bSRalph Campbell 	struct qib_ibport *ibp = &ppd->ibport_data;
1789f931551bSRalph Campbell 
1790f24a6d48SHarish Chegondi 	spin_lock_init(&ibp->rvp.lock);
1791f931551bSRalph Campbell 	/* Set the prefix to the default value (see ch. 4.1.1) */
1792f24a6d48SHarish Chegondi 	ibp->rvp.gid_prefix = IB_DEFAULT_GID_PREFIX;
1793f24a6d48SHarish Chegondi 	ibp->rvp.sm_lid = be16_to_cpu(IB_LID_PERMISSIVE);
1794f24a6d48SHarish Chegondi 	ibp->rvp.port_cap_flags = IB_PORT_SYS_IMAGE_GUID_SUP |
1795f931551bSRalph Campbell 		IB_PORT_CLIENT_REG_SUP | IB_PORT_SL_MAP_SUP |
1796f931551bSRalph Campbell 		IB_PORT_TRAP_SUP | IB_PORT_AUTO_MIGR_SUP |
1797f931551bSRalph Campbell 		IB_PORT_DR_NOTICE_SUP | IB_PORT_CAP_MASK_NOTICE_SUP |
1798f931551bSRalph Campbell 		IB_PORT_OTHER_LOCAL_CHANGES_SUP;
1799f931551bSRalph Campbell 	if (ppd->dd->flags & QIB_HAS_LINK_LATENCY)
1800f24a6d48SHarish Chegondi 		ibp->rvp.port_cap_flags |= IB_PORT_LINK_LATENCY_SUP;
1801f24a6d48SHarish Chegondi 	ibp->rvp.pma_counter_select[0] = IB_PMA_PORT_XMIT_DATA;
1802f24a6d48SHarish Chegondi 	ibp->rvp.pma_counter_select[1] = IB_PMA_PORT_RCV_DATA;
1803f24a6d48SHarish Chegondi 	ibp->rvp.pma_counter_select[2] = IB_PMA_PORT_XMIT_PKTS;
1804f24a6d48SHarish Chegondi 	ibp->rvp.pma_counter_select[3] = IB_PMA_PORT_RCV_PKTS;
1805f24a6d48SHarish Chegondi 	ibp->rvp.pma_counter_select[4] = IB_PMA_PORT_XMIT_WAIT;
1806f931551bSRalph Campbell 
1807f931551bSRalph Campbell 	/* Snapshot current HW counters to "clear" them. */
1808f931551bSRalph Campbell 	qib_get_counters(ppd, &cntrs);
1809f931551bSRalph Campbell 	ibp->z_symbol_error_counter = cntrs.symbol_error_counter;
1810f931551bSRalph Campbell 	ibp->z_link_error_recovery_counter =
1811f931551bSRalph Campbell 		cntrs.link_error_recovery_counter;
1812f931551bSRalph Campbell 	ibp->z_link_downed_counter = cntrs.link_downed_counter;
1813f931551bSRalph Campbell 	ibp->z_port_rcv_errors = cntrs.port_rcv_errors;
1814f931551bSRalph Campbell 	ibp->z_port_rcv_remphys_errors = cntrs.port_rcv_remphys_errors;
1815f931551bSRalph Campbell 	ibp->z_port_xmit_discards = cntrs.port_xmit_discards;
1816f931551bSRalph Campbell 	ibp->z_port_xmit_data = cntrs.port_xmit_data;
1817f931551bSRalph Campbell 	ibp->z_port_rcv_data = cntrs.port_rcv_data;
1818f931551bSRalph Campbell 	ibp->z_port_xmit_packets = cntrs.port_xmit_packets;
1819f931551bSRalph Campbell 	ibp->z_port_rcv_packets = cntrs.port_rcv_packets;
1820f931551bSRalph Campbell 	ibp->z_local_link_integrity_errors =
1821f931551bSRalph Campbell 		cntrs.local_link_integrity_errors;
1822f931551bSRalph Campbell 	ibp->z_excessive_buffer_overrun_errors =
1823f931551bSRalph Campbell 		cntrs.excessive_buffer_overrun_errors;
1824f931551bSRalph Campbell 	ibp->z_vl15_dropped = cntrs.vl15_dropped;
1825f24a6d48SHarish Chegondi 	RCU_INIT_POINTER(ibp->rvp.qp[0], NULL);
1826f24a6d48SHarish Chegondi 	RCU_INIT_POINTER(ibp->rvp.qp[1], NULL);
1827f931551bSRalph Campbell }
1828f931551bSRalph Campbell 
18297738613eSIra Weiny static int qib_port_immutable(struct ib_device *ibdev, u8 port_num,
18307738613eSIra Weiny 			      struct ib_port_immutable *immutable)
18317738613eSIra Weiny {
18327738613eSIra Weiny 	struct ib_port_attr attr;
18337738613eSIra Weiny 	int err;
18347738613eSIra Weiny 
18357738613eSIra Weiny 	err = qib_query_port(ibdev, port_num, &attr);
18367738613eSIra Weiny 	if (err)
18377738613eSIra Weiny 		return err;
18387738613eSIra Weiny 
18397738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
18407738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
1841f9b22e35SIra Weiny 	immutable->core_cap_flags = RDMA_CORE_PORT_IBA_IB;
1842337877a4SIra Weiny 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
18437738613eSIra Weiny 
18447738613eSIra Weiny 	return 0;
18457738613eSIra Weiny }
18467738613eSIra Weiny 
1847f931551bSRalph Campbell /**
18480aeddea2SHarish Chegondi  * qib_fill_device_attr - Fill in rvt dev info device attributes.
18490aeddea2SHarish Chegondi  * @dd: the device data structure
18500aeddea2SHarish Chegondi  */
18510aeddea2SHarish Chegondi static void qib_fill_device_attr(struct qib_devdata *dd)
18520aeddea2SHarish Chegondi {
18530aeddea2SHarish Chegondi 	struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
18540aeddea2SHarish Chegondi 
18550aeddea2SHarish Chegondi 	memset(&rdi->dparms.props, 0, sizeof(rdi->dparms.props));
18560aeddea2SHarish Chegondi 
18570aeddea2SHarish Chegondi 	rdi->dparms.props.max_pd = ib_qib_max_pds;
18580aeddea2SHarish Chegondi 	rdi->dparms.props.max_ah = ib_qib_max_ahs;
18590aeddea2SHarish Chegondi 	rdi->dparms.props.device_cap_flags = IB_DEVICE_BAD_PKEY_CNTR |
18600aeddea2SHarish Chegondi 		IB_DEVICE_BAD_QKEY_CNTR | IB_DEVICE_SHUTDOWN_PORT |
18610aeddea2SHarish Chegondi 		IB_DEVICE_SYS_IMAGE_GUID | IB_DEVICE_RC_RNR_NAK_GEN |
18620aeddea2SHarish Chegondi 		IB_DEVICE_PORT_ACTIVE_EVENT | IB_DEVICE_SRQ_RESIZE;
18630aeddea2SHarish Chegondi 	rdi->dparms.props.page_size_cap = PAGE_SIZE;
18640aeddea2SHarish Chegondi 	rdi->dparms.props.vendor_id =
18650aeddea2SHarish Chegondi 		QIB_SRC_OUI_1 << 16 | QIB_SRC_OUI_2 << 8 | QIB_SRC_OUI_3;
18660aeddea2SHarish Chegondi 	rdi->dparms.props.vendor_part_id = dd->deviceid;
18670aeddea2SHarish Chegondi 	rdi->dparms.props.hw_ver = dd->minrev;
18680aeddea2SHarish Chegondi 	rdi->dparms.props.sys_image_guid = ib_qib_sys_image_guid;
18690aeddea2SHarish Chegondi 	rdi->dparms.props.max_mr_size = ~0ULL;
18700aeddea2SHarish Chegondi 	rdi->dparms.props.max_qp = ib_qib_max_qps;
18710aeddea2SHarish Chegondi 	rdi->dparms.props.max_qp_wr = ib_qib_max_qp_wrs;
18720aeddea2SHarish Chegondi 	rdi->dparms.props.max_sge = ib_qib_max_sges;
18730aeddea2SHarish Chegondi 	rdi->dparms.props.max_sge_rd = ib_qib_max_sges;
18740aeddea2SHarish Chegondi 	rdi->dparms.props.max_cq = ib_qib_max_cqs;
18750aeddea2SHarish Chegondi 	rdi->dparms.props.max_cqe = ib_qib_max_cqes;
18760aeddea2SHarish Chegondi 	rdi->dparms.props.max_ah = ib_qib_max_ahs;
18770aeddea2SHarish Chegondi 	rdi->dparms.props.max_mr = rdi->lkey_table.max;
18780aeddea2SHarish Chegondi 	rdi->dparms.props.max_fmr = rdi->lkey_table.max;
18790aeddea2SHarish Chegondi 	rdi->dparms.props.max_map_per_fmr = 32767;
18800aeddea2SHarish Chegondi 	rdi->dparms.props.max_qp_rd_atom = QIB_MAX_RDMA_ATOMIC;
18810aeddea2SHarish Chegondi 	rdi->dparms.props.max_qp_init_rd_atom = 255;
18820aeddea2SHarish Chegondi 	rdi->dparms.props.max_srq = ib_qib_max_srqs;
18830aeddea2SHarish Chegondi 	rdi->dparms.props.max_srq_wr = ib_qib_max_srq_wrs;
18840aeddea2SHarish Chegondi 	rdi->dparms.props.max_srq_sge = ib_qib_max_srq_sges;
18850aeddea2SHarish Chegondi 	rdi->dparms.props.atomic_cap = IB_ATOMIC_GLOB;
18860aeddea2SHarish Chegondi 	rdi->dparms.props.max_pkeys = qib_get_npkeys(dd);
18870aeddea2SHarish Chegondi 	rdi->dparms.props.max_mcast_grp = ib_qib_max_mcast_grps;
18880aeddea2SHarish Chegondi 	rdi->dparms.props.max_mcast_qp_attach = ib_qib_max_mcast_qp_attached;
18890aeddea2SHarish Chegondi 	rdi->dparms.props.max_total_mcast_qp_attach =
18900aeddea2SHarish Chegondi 					rdi->dparms.props.max_mcast_qp_attach *
18910aeddea2SHarish Chegondi 					rdi->dparms.props.max_mcast_grp;
18920aeddea2SHarish Chegondi }
18930aeddea2SHarish Chegondi 
18940aeddea2SHarish Chegondi /**
1895f931551bSRalph Campbell  * qib_register_ib_device - register our device with the infiniband core
1896f931551bSRalph Campbell  * @dd: the device data structure
1897f931551bSRalph Campbell  * Return the allocated qib_ibdev pointer or NULL on error.
1898f931551bSRalph Campbell  */
1899f931551bSRalph Campbell int qib_register_ib_device(struct qib_devdata *dd)
1900f931551bSRalph Campbell {
1901f931551bSRalph Campbell 	struct qib_ibdev *dev = &dd->verbs_dev;
19022dc05ab5SDennis Dalessandro 	struct ib_device *ibdev = &dev->rdi.ibdev;
1903f931551bSRalph Campbell 	struct qib_pportdata *ppd = dd->pport;
190476fec3e0SHarish Chegondi 	unsigned i, ctxt;
1905f931551bSRalph Campbell 	int ret;
1906f931551bSRalph Campbell 
1907af061a64SMike Marciniszyn 	get_random_bytes(&dev->qp_rnd, sizeof(dev->qp_rnd));
1908f931551bSRalph Campbell 	for (i = 0; i < dd->num_pports; i++)
1909f931551bSRalph Campbell 		init_ibport(ppd + i);
1910f931551bSRalph Campbell 
1911f931551bSRalph Campbell 	/* Only need to initialize non-zero fields. */
1912f931551bSRalph Campbell 	spin_lock_init(&dev->n_cqs_lock);
1913f931551bSRalph Campbell 	spin_lock_init(&dev->n_qps_lock);
1914f931551bSRalph Campbell 	spin_lock_init(&dev->n_srqs_lock);
1915f931551bSRalph Campbell 	spin_lock_init(&dev->n_mcast_grps_lock);
1916f931551bSRalph Campbell 	init_timer(&dev->mem_timer);
1917f931551bSRalph Campbell 	dev->mem_timer.function = mem_timer;
1918f931551bSRalph Campbell 	dev->mem_timer.data = (unsigned long) dev;
1919f931551bSRalph Campbell 
1920*47c7ea6dSHarish Chegondi 	qpt_mask = dd->qpn_mask;
1921f931551bSRalph Campbell 
1922f931551bSRalph Campbell 	INIT_LIST_HEAD(&dev->piowait);
1923f931551bSRalph Campbell 	INIT_LIST_HEAD(&dev->dmawait);
1924f931551bSRalph Campbell 	INIT_LIST_HEAD(&dev->txwait);
1925f931551bSRalph Campbell 	INIT_LIST_HEAD(&dev->memwait);
1926f931551bSRalph Campbell 	INIT_LIST_HEAD(&dev->txreq_free);
1927f931551bSRalph Campbell 
1928f931551bSRalph Campbell 	if (ppd->sdma_descq_cnt) {
1929f931551bSRalph Campbell 		dev->pio_hdrs = dma_alloc_coherent(&dd->pcidev->dev,
1930f931551bSRalph Campbell 						ppd->sdma_descq_cnt *
1931f931551bSRalph Campbell 						sizeof(struct qib_pio_header),
1932f931551bSRalph Campbell 						&dev->pio_hdrs_phys,
1933f931551bSRalph Campbell 						GFP_KERNEL);
1934f931551bSRalph Campbell 		if (!dev->pio_hdrs) {
1935f931551bSRalph Campbell 			ret = -ENOMEM;
1936f931551bSRalph Campbell 			goto err_hdrs;
1937f931551bSRalph Campbell 		}
1938f931551bSRalph Campbell 	}
1939f931551bSRalph Campbell 
1940f931551bSRalph Campbell 	for (i = 0; i < ppd->sdma_descq_cnt; i++) {
1941f931551bSRalph Campbell 		struct qib_verbs_txreq *tx;
1942f931551bSRalph Campbell 
1943041af0bbSMike Marciniszyn 		tx = kzalloc(sizeof(*tx), GFP_KERNEL);
1944f931551bSRalph Campbell 		if (!tx) {
1945f931551bSRalph Campbell 			ret = -ENOMEM;
1946f931551bSRalph Campbell 			goto err_tx;
1947f931551bSRalph Campbell 		}
1948f931551bSRalph Campbell 		tx->hdr_inx = i;
1949f931551bSRalph Campbell 		list_add(&tx->txreq.list, &dev->txreq_free);
1950f931551bSRalph Campbell 	}
1951f931551bSRalph Campbell 
1952f931551bSRalph Campbell 	/*
1953f931551bSRalph Campbell 	 * The system image GUID is supposed to be the same for all
1954f931551bSRalph Campbell 	 * IB HCAs in a single system but since there can be other
1955f931551bSRalph Campbell 	 * device types in the system, we can't be sure this is unique.
1956f931551bSRalph Campbell 	 */
1957f931551bSRalph Campbell 	if (!ib_qib_sys_image_guid)
1958f931551bSRalph Campbell 		ib_qib_sys_image_guid = ppd->guid;
1959f931551bSRalph Campbell 
1960f931551bSRalph Campbell 	strlcpy(ibdev->name, "qib%d", IB_DEVICE_NAME_MAX);
1961f931551bSRalph Campbell 	ibdev->owner = THIS_MODULE;
1962f931551bSRalph Campbell 	ibdev->node_guid = ppd->guid;
1963f931551bSRalph Campbell 	ibdev->uverbs_abi_ver = QIB_UVERBS_ABI_VERSION;
1964f931551bSRalph Campbell 	ibdev->uverbs_cmd_mask =
1965f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)         |
1966f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)        |
1967f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)          |
1968f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)            |
1969f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)          |
1970f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)           |
1971f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_MODIFY_AH)           |
1972f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_QUERY_AH)            |
1973f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)          |
1974f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_REG_MR)              |
1975f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)            |
1976f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
1977f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)           |
1978f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)           |
1979f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)          |
1980f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_POLL_CQ)             |
1981f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ)       |
1982f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)           |
1983f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)            |
1984f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)           |
1985f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)          |
1986f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_POST_SEND)           |
1987f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_POST_RECV)           |
1988f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)        |
1989f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)        |
1990f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)          |
1991f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)          |
1992f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)           |
1993f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)         |
1994f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
1995f931551bSRalph Campbell 	ibdev->node_type = RDMA_NODE_IB_CA;
1996f931551bSRalph Campbell 	ibdev->phys_port_cnt = dd->num_pports;
1997f931551bSRalph Campbell 	ibdev->num_comp_vectors = 1;
1998f931551bSRalph Campbell 	ibdev->dma_device = &dd->pcidev->dev;
19990aeddea2SHarish Chegondi 	ibdev->query_device = NULL;
2000f931551bSRalph Campbell 	ibdev->modify_device = qib_modify_device;
2001f931551bSRalph Campbell 	ibdev->query_port = qib_query_port;
2002f931551bSRalph Campbell 	ibdev->modify_port = qib_modify_port;
200376fec3e0SHarish Chegondi 	ibdev->query_pkey = NULL;
2004f931551bSRalph Campbell 	ibdev->query_gid = qib_query_gid;
20051da0f7e2SHarish Chegondi 	ibdev->alloc_ucontext = NULL;
20061da0f7e2SHarish Chegondi 	ibdev->dealloc_ucontext = NULL;
2007f44728d6SDennis Dalessandro 	ibdev->alloc_pd = NULL;
2008f44728d6SDennis Dalessandro 	ibdev->dealloc_pd = NULL;
200996ab1ac1SDennis Dalessandro 	ibdev->create_ah = NULL;
201096ab1ac1SDennis Dalessandro 	ibdev->destroy_ah = NULL;
201196ab1ac1SDennis Dalessandro 	ibdev->modify_ah = NULL;
201296ab1ac1SDennis Dalessandro 	ibdev->query_ah = NULL;
2013f931551bSRalph Campbell 	ibdev->create_srq = qib_create_srq;
2014f931551bSRalph Campbell 	ibdev->modify_srq = qib_modify_srq;
2015f931551bSRalph Campbell 	ibdev->query_srq = qib_query_srq;
2016f931551bSRalph Campbell 	ibdev->destroy_srq = qib_destroy_srq;
2017*47c7ea6dSHarish Chegondi 	ibdev->create_qp = NULL;
2018f931551bSRalph Campbell 	ibdev->modify_qp = qib_modify_qp;
2019f931551bSRalph Campbell 	ibdev->query_qp = qib_query_qp;
2020f931551bSRalph Campbell 	ibdev->destroy_qp = qib_destroy_qp;
2021f931551bSRalph Campbell 	ibdev->post_send = qib_post_send;
2022f931551bSRalph Campbell 	ibdev->post_recv = qib_post_receive;
2023f931551bSRalph Campbell 	ibdev->post_srq_recv = qib_post_srq_receive;
2024f931551bSRalph Campbell 	ibdev->create_cq = qib_create_cq;
2025f931551bSRalph Campbell 	ibdev->destroy_cq = qib_destroy_cq;
2026f931551bSRalph Campbell 	ibdev->resize_cq = qib_resize_cq;
2027f931551bSRalph Campbell 	ibdev->poll_cq = qib_poll_cq;
2028f931551bSRalph Campbell 	ibdev->req_notify_cq = qib_req_notify_cq;
20297c2e11feSDennis Dalessandro 	ibdev->get_dma_mr = NULL;
20307c2e11feSDennis Dalessandro 	ibdev->reg_user_mr = NULL;
20317c2e11feSDennis Dalessandro 	ibdev->dereg_mr = NULL;
20327c2e11feSDennis Dalessandro 	ibdev->alloc_mr = NULL;
20337c2e11feSDennis Dalessandro 	ibdev->map_mr_sg = NULL;
20347c2e11feSDennis Dalessandro 	ibdev->alloc_fmr = NULL;
20357c2e11feSDennis Dalessandro 	ibdev->map_phys_fmr = NULL;
20367c2e11feSDennis Dalessandro 	ibdev->unmap_fmr = NULL;
20377c2e11feSDennis Dalessandro 	ibdev->dealloc_fmr = NULL;
2038f931551bSRalph Campbell 	ibdev->attach_mcast = qib_multicast_attach;
2039f931551bSRalph Campbell 	ibdev->detach_mcast = qib_multicast_detach;
2040f931551bSRalph Campbell 	ibdev->process_mad = qib_process_mad;
2041cd18201fSHarish Chegondi 	ibdev->mmap = NULL;
2042eb636ac0SDennis Dalessandro 	ibdev->dma_ops = NULL;
20437738613eSIra Weiny 	ibdev->get_port_immutable = qib_port_immutable;
2044f931551bSRalph Campbell 
2045f931551bSRalph Campbell 	snprintf(ibdev->node_desc, sizeof(ibdev->node_desc),
2046e2eed58bSVinit Agnihotri 		 "Intel Infiniband HCA %s", init_utsname()->nodename);
2047f931551bSRalph Campbell 
20482dc05ab5SDennis Dalessandro 	/*
20492dc05ab5SDennis Dalessandro 	 * Fill in rvt info object.
20502dc05ab5SDennis Dalessandro 	 */
20512dc05ab5SDennis Dalessandro 	dd->verbs_dev.rdi.driver_f.port_callback = qib_create_port_files;
20526a9df403SDennis Dalessandro 	dd->verbs_dev.rdi.driver_f.get_card_name = qib_get_card_name;
20536a9df403SDennis Dalessandro 	dd->verbs_dev.rdi.driver_f.get_pci_dev = qib_get_pci_dev;
205496ab1ac1SDennis Dalessandro 	dd->verbs_dev.rdi.driver_f.check_ah = qib_check_ah;
20555418a5abSHarish Chegondi 	dd->verbs_dev.rdi.driver_f.notify_new_ah = qib_notify_new_ah;
2056*47c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.driver_f.alloc_qpn = alloc_qpn;
2057*47c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.driver_f.qp_priv_alloc = qp_priv_alloc;
2058*47c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.driver_f.qp_priv_free = qp_priv_free;
2059*47c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.driver_f.free_all_qps = qib_free_all_qps;
2060*47c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.driver_f.notify_qp_reset = notify_qp_reset;
2061*47c7ea6dSHarish Chegondi 
2062*47c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.flags = RVT_FLAG_CQ_INIT_DRIVER;
2063*47c7ea6dSHarish Chegondi 
20647c2e11feSDennis Dalessandro 	dd->verbs_dev.rdi.dparms.lkey_table_size = qib_lkey_table_size;
2065*47c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.dparms.qp_table_size = ib_qib_qp_table_size;
2066*47c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.dparms.qpn_start = 1;
2067*47c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.dparms.qpn_res_start = QIB_KD_QP;
2068*47c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.dparms.qpn_res_end = QIB_KD_QP; /* Reserve one QP */
2069*47c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.dparms.qpn_inc = 1;
2070*47c7ea6dSHarish Chegondi 	dd->verbs_dev.rdi.dparms.qos_shift = 1;
207176fec3e0SHarish Chegondi 	dd->verbs_dev.rdi.dparms.nports = dd->num_pports;
207276fec3e0SHarish Chegondi 	dd->verbs_dev.rdi.dparms.npkeys = qib_get_npkeys(dd);
207376fec3e0SHarish Chegondi 
20740aeddea2SHarish Chegondi 	qib_fill_device_attr(dd);
20750aeddea2SHarish Chegondi 
207676fec3e0SHarish Chegondi 	ppd = dd->pport;
207776fec3e0SHarish Chegondi 	for (i = 0; i < dd->num_pports; i++, ppd++) {
207876fec3e0SHarish Chegondi 		ctxt = ppd->hw_pidx;
207976fec3e0SHarish Chegondi 		rvt_init_port(&dd->verbs_dev.rdi,
208076fec3e0SHarish Chegondi 			      &ppd->ibport_data.rvp,
208176fec3e0SHarish Chegondi 			      i,
208276fec3e0SHarish Chegondi 			      dd->rcd[ctxt]->pkeys);
208376fec3e0SHarish Chegondi 	}
20842dc05ab5SDennis Dalessandro 
20852dc05ab5SDennis Dalessandro 	ret = rvt_register_device(&dd->verbs_dev.rdi);
2086f931551bSRalph Campbell 	if (ret)
2087f931551bSRalph Campbell 		goto err_reg;
2088f931551bSRalph Campbell 
2089f931551bSRalph Campbell 	ret = qib_create_agents(dev);
2090f931551bSRalph Campbell 	if (ret)
2091f931551bSRalph Campbell 		goto err_agents;
2092f931551bSRalph Campbell 
2093c9bdad3cSMike Marciniszyn 	ret = qib_verbs_register_sysfs(dd);
2094c9bdad3cSMike Marciniszyn 	if (ret)
2095f931551bSRalph Campbell 		goto err_class;
2096f931551bSRalph Campbell 
2097f931551bSRalph Campbell 	goto bail;
2098f931551bSRalph Campbell 
2099f931551bSRalph Campbell err_class:
2100f931551bSRalph Campbell 	qib_free_agents(dev);
2101f931551bSRalph Campbell err_agents:
21022dc05ab5SDennis Dalessandro 	rvt_unregister_device(&dd->verbs_dev.rdi);
2103f931551bSRalph Campbell err_reg:
2104f931551bSRalph Campbell err_tx:
2105f931551bSRalph Campbell 	while (!list_empty(&dev->txreq_free)) {
2106f931551bSRalph Campbell 		struct list_head *l = dev->txreq_free.next;
2107f931551bSRalph Campbell 		struct qib_verbs_txreq *tx;
2108f931551bSRalph Campbell 
2109f931551bSRalph Campbell 		list_del(l);
2110f931551bSRalph Campbell 		tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
2111f931551bSRalph Campbell 		kfree(tx);
2112f931551bSRalph Campbell 	}
2113f931551bSRalph Campbell 	if (ppd->sdma_descq_cnt)
2114f931551bSRalph Campbell 		dma_free_coherent(&dd->pcidev->dev,
2115f931551bSRalph Campbell 				  ppd->sdma_descq_cnt *
2116f931551bSRalph Campbell 					sizeof(struct qib_pio_header),
2117f931551bSRalph Campbell 				  dev->pio_hdrs, dev->pio_hdrs_phys);
2118f931551bSRalph Campbell err_hdrs:
2119f931551bSRalph Campbell 	qib_dev_err(dd, "cannot register verbs: %d!\n", -ret);
2120f931551bSRalph Campbell bail:
2121f931551bSRalph Campbell 	return ret;
2122f931551bSRalph Campbell }
2123f931551bSRalph Campbell 
2124f931551bSRalph Campbell void qib_unregister_ib_device(struct qib_devdata *dd)
2125f931551bSRalph Campbell {
2126f931551bSRalph Campbell 	struct qib_ibdev *dev = &dd->verbs_dev;
2127f931551bSRalph Campbell 
2128f931551bSRalph Campbell 	qib_verbs_unregister_sysfs(dd);
2129f931551bSRalph Campbell 
2130f931551bSRalph Campbell 	qib_free_agents(dev);
2131f931551bSRalph Campbell 
21322dc05ab5SDennis Dalessandro 	rvt_unregister_device(&dd->verbs_dev.rdi);
2133f931551bSRalph Campbell 
2134f931551bSRalph Campbell 	if (!list_empty(&dev->piowait))
2135f931551bSRalph Campbell 		qib_dev_err(dd, "piowait list not empty!\n");
2136f931551bSRalph Campbell 	if (!list_empty(&dev->dmawait))
2137f931551bSRalph Campbell 		qib_dev_err(dd, "dmawait list not empty!\n");
2138f931551bSRalph Campbell 	if (!list_empty(&dev->txwait))
2139f931551bSRalph Campbell 		qib_dev_err(dd, "txwait list not empty!\n");
2140f931551bSRalph Campbell 	if (!list_empty(&dev->memwait))
2141f931551bSRalph Campbell 		qib_dev_err(dd, "memwait list not empty!\n");
2142f931551bSRalph Campbell 
2143f931551bSRalph Campbell 	del_timer_sync(&dev->mem_timer);
2144f931551bSRalph Campbell 	while (!list_empty(&dev->txreq_free)) {
2145f931551bSRalph Campbell 		struct list_head *l = dev->txreq_free.next;
2146f931551bSRalph Campbell 		struct qib_verbs_txreq *tx;
2147f931551bSRalph Campbell 
2148f931551bSRalph Campbell 		list_del(l);
2149f931551bSRalph Campbell 		tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
2150f931551bSRalph Campbell 		kfree(tx);
2151f931551bSRalph Campbell 	}
2152f931551bSRalph Campbell 	if (dd->pport->sdma_descq_cnt)
2153f931551bSRalph Campbell 		dma_free_coherent(&dd->pcidev->dev,
2154f931551bSRalph Campbell 				  dd->pport->sdma_descq_cnt *
2155f931551bSRalph Campbell 					sizeof(struct qib_pio_header),
2156f931551bSRalph Campbell 				  dev->pio_hdrs, dev->pio_hdrs_phys);
2157f931551bSRalph Campbell }
2158551ace12SMike Marciniszyn 
2159551ace12SMike Marciniszyn /*
2160551ace12SMike Marciniszyn  * This must be called with s_lock held.
2161551ace12SMike Marciniszyn  */
21627c2e11feSDennis Dalessandro void qib_schedule_send(struct rvt_qp *qp)
2163551ace12SMike Marciniszyn {
2164ffc26907SDennis Dalessandro 	struct qib_qp_priv *priv = qp->priv;
2165551ace12SMike Marciniszyn 	if (qib_send_ok(qp)) {
2166551ace12SMike Marciniszyn 		struct qib_ibport *ibp =
2167551ace12SMike Marciniszyn 			to_iport(qp->ibqp.device, qp->port_num);
2168551ace12SMike Marciniszyn 		struct qib_pportdata *ppd = ppd_from_ibp(ibp);
2169551ace12SMike Marciniszyn 
2170ffc26907SDennis Dalessandro 		queue_work(ppd->qib_wq, &priv->s_work);
2171551ace12SMike Marciniszyn 	}
2172551ace12SMike Marciniszyn }
2173