xref: /openbmc/linux/drivers/infiniband/hw/qib/qib_verbs.c (revision aaae91f4f05c39f02e36e89b00ad84465f8eb02b)
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>
44f931551bSRalph Campbell 
45f931551bSRalph Campbell #include "qib.h"
46f931551bSRalph Campbell #include "qib_common.h"
47f931551bSRalph Campbell 
48af061a64SMike Marciniszyn static unsigned int ib_qib_qp_table_size = 256;
49f931551bSRalph Campbell module_param_named(qp_table_size, ib_qib_qp_table_size, uint, S_IRUGO);
50f931551bSRalph Campbell MODULE_PARM_DESC(qp_table_size, "QP table size");
51f931551bSRalph Campbell 
52f931551bSRalph Campbell unsigned int ib_qib_lkey_table_size = 16;
53f931551bSRalph Campbell module_param_named(lkey_table_size, ib_qib_lkey_table_size, uint,
54f931551bSRalph Campbell 		   S_IRUGO);
55f931551bSRalph Campbell MODULE_PARM_DESC(lkey_table_size,
56f931551bSRalph Campbell 		 "LKEY table size in bits (2^n, 1 <= n <= 23)");
57f931551bSRalph Campbell 
58f931551bSRalph Campbell static unsigned int ib_qib_max_pds = 0xFFFF;
59f931551bSRalph Campbell module_param_named(max_pds, ib_qib_max_pds, uint, S_IRUGO);
60f931551bSRalph Campbell MODULE_PARM_DESC(max_pds,
61f931551bSRalph Campbell 		 "Maximum number of protection domains to support");
62f931551bSRalph Campbell 
63f931551bSRalph Campbell static unsigned int ib_qib_max_ahs = 0xFFFF;
64f931551bSRalph Campbell module_param_named(max_ahs, ib_qib_max_ahs, uint, S_IRUGO);
65f931551bSRalph Campbell MODULE_PARM_DESC(max_ahs, "Maximum number of address handles to support");
66f931551bSRalph Campbell 
67f931551bSRalph Campbell unsigned int ib_qib_max_cqes = 0x2FFFF;
68f931551bSRalph Campbell module_param_named(max_cqes, ib_qib_max_cqes, uint, S_IRUGO);
69f931551bSRalph Campbell MODULE_PARM_DESC(max_cqes,
70f931551bSRalph Campbell 		 "Maximum number of completion queue entries to support");
71f931551bSRalph Campbell 
72f931551bSRalph Campbell unsigned int ib_qib_max_cqs = 0x1FFFF;
73f931551bSRalph Campbell module_param_named(max_cqs, ib_qib_max_cqs, uint, S_IRUGO);
74f931551bSRalph Campbell MODULE_PARM_DESC(max_cqs, "Maximum number of completion queues to support");
75f931551bSRalph Campbell 
76f931551bSRalph Campbell unsigned int ib_qib_max_qp_wrs = 0x3FFF;
77f931551bSRalph Campbell module_param_named(max_qp_wrs, ib_qib_max_qp_wrs, uint, S_IRUGO);
78f931551bSRalph Campbell MODULE_PARM_DESC(max_qp_wrs, "Maximum number of QP WRs to support");
79f931551bSRalph Campbell 
80f931551bSRalph Campbell unsigned int ib_qib_max_qps = 16384;
81f931551bSRalph Campbell module_param_named(max_qps, ib_qib_max_qps, uint, S_IRUGO);
82f931551bSRalph Campbell MODULE_PARM_DESC(max_qps, "Maximum number of QPs to support");
83f931551bSRalph Campbell 
84f931551bSRalph Campbell unsigned int ib_qib_max_sges = 0x60;
85f931551bSRalph Campbell module_param_named(max_sges, ib_qib_max_sges, uint, S_IRUGO);
86f931551bSRalph Campbell MODULE_PARM_DESC(max_sges, "Maximum number of SGEs to support");
87f931551bSRalph Campbell 
88f931551bSRalph Campbell unsigned int ib_qib_max_mcast_grps = 16384;
89f931551bSRalph Campbell module_param_named(max_mcast_grps, ib_qib_max_mcast_grps, uint, S_IRUGO);
90f931551bSRalph Campbell MODULE_PARM_DESC(max_mcast_grps,
91f931551bSRalph Campbell 		 "Maximum number of multicast groups to support");
92f931551bSRalph Campbell 
93f931551bSRalph Campbell unsigned int ib_qib_max_mcast_qp_attached = 16;
94f931551bSRalph Campbell module_param_named(max_mcast_qp_attached, ib_qib_max_mcast_qp_attached,
95f931551bSRalph Campbell 		   uint, S_IRUGO);
96f931551bSRalph Campbell MODULE_PARM_DESC(max_mcast_qp_attached,
97f931551bSRalph Campbell 		 "Maximum number of attached QPs to support");
98f931551bSRalph Campbell 
99f931551bSRalph Campbell unsigned int ib_qib_max_srqs = 1024;
100f931551bSRalph Campbell module_param_named(max_srqs, ib_qib_max_srqs, uint, S_IRUGO);
101f931551bSRalph Campbell MODULE_PARM_DESC(max_srqs, "Maximum number of SRQs to support");
102f931551bSRalph Campbell 
103f931551bSRalph Campbell unsigned int ib_qib_max_srq_sges = 128;
104f931551bSRalph Campbell module_param_named(max_srq_sges, ib_qib_max_srq_sges, uint, S_IRUGO);
105f931551bSRalph Campbell MODULE_PARM_DESC(max_srq_sges, "Maximum number of SRQ SGEs to support");
106f931551bSRalph Campbell 
107f931551bSRalph Campbell unsigned int ib_qib_max_srq_wrs = 0x1FFFF;
108f931551bSRalph Campbell module_param_named(max_srq_wrs, ib_qib_max_srq_wrs, uint, S_IRUGO);
109f931551bSRalph Campbell MODULE_PARM_DESC(max_srq_wrs, "Maximum number of SRQ WRs support");
110f931551bSRalph Campbell 
111f931551bSRalph Campbell static unsigned int ib_qib_disable_sma;
112f931551bSRalph Campbell module_param_named(disable_sma, ib_qib_disable_sma, uint, S_IWUSR | S_IRUGO);
113f931551bSRalph Campbell MODULE_PARM_DESC(disable_sma, "Disable the SMA");
114f931551bSRalph Campbell 
115f931551bSRalph Campbell /*
116f931551bSRalph Campbell  * Note that it is OK to post send work requests in the SQE and ERR
117f931551bSRalph Campbell  * states; qib_do_send() will process them and generate error
118f931551bSRalph Campbell  * completions as per IB 1.2 C10-96.
119f931551bSRalph Campbell  */
120f931551bSRalph Campbell const int ib_qib_state_ops[IB_QPS_ERR + 1] = {
121f931551bSRalph Campbell 	[IB_QPS_RESET] = 0,
122f931551bSRalph Campbell 	[IB_QPS_INIT] = QIB_POST_RECV_OK,
123f931551bSRalph Campbell 	[IB_QPS_RTR] = QIB_POST_RECV_OK | QIB_PROCESS_RECV_OK,
124f931551bSRalph Campbell 	[IB_QPS_RTS] = QIB_POST_RECV_OK | QIB_PROCESS_RECV_OK |
125f931551bSRalph Campbell 	    QIB_POST_SEND_OK | QIB_PROCESS_SEND_OK |
126f931551bSRalph Campbell 	    QIB_PROCESS_NEXT_SEND_OK,
127f931551bSRalph Campbell 	[IB_QPS_SQD] = QIB_POST_RECV_OK | QIB_PROCESS_RECV_OK |
128f931551bSRalph Campbell 	    QIB_POST_SEND_OK | QIB_PROCESS_SEND_OK,
129f931551bSRalph Campbell 	[IB_QPS_SQE] = QIB_POST_RECV_OK | QIB_PROCESS_RECV_OK |
130f931551bSRalph Campbell 	    QIB_POST_SEND_OK | QIB_FLUSH_SEND,
131f931551bSRalph Campbell 	[IB_QPS_ERR] = QIB_POST_RECV_OK | QIB_FLUSH_RECV |
132f931551bSRalph Campbell 	    QIB_POST_SEND_OK | QIB_FLUSH_SEND,
133f931551bSRalph Campbell };
134f931551bSRalph Campbell 
135f931551bSRalph Campbell struct qib_ucontext {
136f931551bSRalph Campbell 	struct ib_ucontext ibucontext;
137f931551bSRalph Campbell };
138f931551bSRalph Campbell 
139f931551bSRalph Campbell static inline struct qib_ucontext *to_iucontext(struct ib_ucontext
140f931551bSRalph Campbell 						  *ibucontext)
141f931551bSRalph Campbell {
142f931551bSRalph Campbell 	return container_of(ibucontext, struct qib_ucontext, ibucontext);
143f931551bSRalph Campbell }
144f931551bSRalph Campbell 
145f931551bSRalph Campbell /*
146f931551bSRalph Campbell  * Translate ib_wr_opcode into ib_wc_opcode.
147f931551bSRalph Campbell  */
148f931551bSRalph Campbell const enum ib_wc_opcode ib_qib_wc_opcode[] = {
149f931551bSRalph Campbell 	[IB_WR_RDMA_WRITE] = IB_WC_RDMA_WRITE,
150f931551bSRalph Campbell 	[IB_WR_RDMA_WRITE_WITH_IMM] = IB_WC_RDMA_WRITE,
151f931551bSRalph Campbell 	[IB_WR_SEND] = IB_WC_SEND,
152f931551bSRalph Campbell 	[IB_WR_SEND_WITH_IMM] = IB_WC_SEND,
153f931551bSRalph Campbell 	[IB_WR_RDMA_READ] = IB_WC_RDMA_READ,
154f931551bSRalph Campbell 	[IB_WR_ATOMIC_CMP_AND_SWP] = IB_WC_COMP_SWAP,
155f931551bSRalph Campbell 	[IB_WR_ATOMIC_FETCH_AND_ADD] = IB_WC_FETCH_ADD
156f931551bSRalph Campbell };
157f931551bSRalph Campbell 
158f931551bSRalph Campbell /*
159f931551bSRalph Campbell  * System image GUID.
160f931551bSRalph Campbell  */
161f931551bSRalph Campbell __be64 ib_qib_sys_image_guid;
162f931551bSRalph Campbell 
163f931551bSRalph Campbell /**
164f931551bSRalph Campbell  * qib_copy_sge - copy data to SGE memory
165f931551bSRalph Campbell  * @ss: the SGE state
166f931551bSRalph Campbell  * @data: the data to copy
167f931551bSRalph Campbell  * @length: the length of the data
168f931551bSRalph Campbell  */
169f931551bSRalph Campbell void qib_copy_sge(struct qib_sge_state *ss, void *data, u32 length, int release)
170f931551bSRalph Campbell {
171f931551bSRalph Campbell 	struct qib_sge *sge = &ss->sge;
172f931551bSRalph Campbell 
173f931551bSRalph Campbell 	while (length) {
174f931551bSRalph Campbell 		u32 len = sge->length;
175f931551bSRalph Campbell 
176f931551bSRalph Campbell 		if (len > length)
177f931551bSRalph Campbell 			len = length;
178f931551bSRalph Campbell 		if (len > sge->sge_length)
179f931551bSRalph Campbell 			len = sge->sge_length;
180f931551bSRalph Campbell 		BUG_ON(len == 0);
181f931551bSRalph Campbell 		memcpy(sge->vaddr, data, len);
182f931551bSRalph Campbell 		sge->vaddr += len;
183f931551bSRalph Campbell 		sge->length -= len;
184f931551bSRalph Campbell 		sge->sge_length -= len;
185f931551bSRalph Campbell 		if (sge->sge_length == 0) {
186f931551bSRalph Campbell 			if (release)
1876a82649fSMike Marciniszyn 				qib_put_mr(sge->mr);
188f931551bSRalph Campbell 			if (--ss->num_sge)
189f931551bSRalph Campbell 				*sge = *ss->sg_list++;
190f931551bSRalph Campbell 		} else if (sge->length == 0 && sge->mr->lkey) {
191f931551bSRalph Campbell 			if (++sge->n >= QIB_SEGSZ) {
192f931551bSRalph Campbell 				if (++sge->m >= sge->mr->mapsz)
193f931551bSRalph Campbell 					break;
194f931551bSRalph Campbell 				sge->n = 0;
195f931551bSRalph Campbell 			}
196f931551bSRalph Campbell 			sge->vaddr =
197f931551bSRalph Campbell 				sge->mr->map[sge->m]->segs[sge->n].vaddr;
198f931551bSRalph Campbell 			sge->length =
199f931551bSRalph Campbell 				sge->mr->map[sge->m]->segs[sge->n].length;
200f931551bSRalph Campbell 		}
201f931551bSRalph Campbell 		data += len;
202f931551bSRalph Campbell 		length -= len;
203f931551bSRalph Campbell 	}
204f931551bSRalph Campbell }
205f931551bSRalph Campbell 
206f931551bSRalph Campbell /**
207f931551bSRalph Campbell  * qib_skip_sge - skip over SGE memory - XXX almost dup of prev func
208f931551bSRalph Campbell  * @ss: the SGE state
209f931551bSRalph Campbell  * @length: the number of bytes to skip
210f931551bSRalph Campbell  */
211f931551bSRalph Campbell void qib_skip_sge(struct qib_sge_state *ss, u32 length, int release)
212f931551bSRalph Campbell {
213f931551bSRalph Campbell 	struct qib_sge *sge = &ss->sge;
214f931551bSRalph Campbell 
215f931551bSRalph Campbell 	while (length) {
216f931551bSRalph Campbell 		u32 len = sge->length;
217f931551bSRalph Campbell 
218f931551bSRalph Campbell 		if (len > length)
219f931551bSRalph Campbell 			len = length;
220f931551bSRalph Campbell 		if (len > sge->sge_length)
221f931551bSRalph Campbell 			len = sge->sge_length;
222f931551bSRalph Campbell 		BUG_ON(len == 0);
223f931551bSRalph Campbell 		sge->vaddr += len;
224f931551bSRalph Campbell 		sge->length -= len;
225f931551bSRalph Campbell 		sge->sge_length -= len;
226f931551bSRalph Campbell 		if (sge->sge_length == 0) {
227f931551bSRalph Campbell 			if (release)
2286a82649fSMike Marciniszyn 				qib_put_mr(sge->mr);
229f931551bSRalph Campbell 			if (--ss->num_sge)
230f931551bSRalph Campbell 				*sge = *ss->sg_list++;
231f931551bSRalph Campbell 		} else if (sge->length == 0 && sge->mr->lkey) {
232f931551bSRalph Campbell 			if (++sge->n >= QIB_SEGSZ) {
233f931551bSRalph Campbell 				if (++sge->m >= sge->mr->mapsz)
234f931551bSRalph Campbell 					break;
235f931551bSRalph Campbell 				sge->n = 0;
236f931551bSRalph Campbell 			}
237f931551bSRalph Campbell 			sge->vaddr =
238f931551bSRalph Campbell 				sge->mr->map[sge->m]->segs[sge->n].vaddr;
239f931551bSRalph Campbell 			sge->length =
240f931551bSRalph Campbell 				sge->mr->map[sge->m]->segs[sge->n].length;
241f931551bSRalph Campbell 		}
242f931551bSRalph Campbell 		length -= len;
243f931551bSRalph Campbell 	}
244f931551bSRalph Campbell }
245f931551bSRalph Campbell 
246f931551bSRalph Campbell /*
247f931551bSRalph Campbell  * Count the number of DMA descriptors needed to send length bytes of data.
248f931551bSRalph Campbell  * Don't modify the qib_sge_state to get the count.
249f931551bSRalph Campbell  * Return zero if any of the segments is not aligned.
250f931551bSRalph Campbell  */
251f931551bSRalph Campbell static u32 qib_count_sge(struct qib_sge_state *ss, u32 length)
252f931551bSRalph Campbell {
253f931551bSRalph Campbell 	struct qib_sge *sg_list = ss->sg_list;
254f931551bSRalph Campbell 	struct qib_sge sge = ss->sge;
255f931551bSRalph Campbell 	u8 num_sge = ss->num_sge;
256f931551bSRalph Campbell 	u32 ndesc = 1;  /* count the header */
257f931551bSRalph Campbell 
258f931551bSRalph Campbell 	while (length) {
259f931551bSRalph Campbell 		u32 len = sge.length;
260f931551bSRalph Campbell 
261f931551bSRalph Campbell 		if (len > length)
262f931551bSRalph Campbell 			len = length;
263f931551bSRalph Campbell 		if (len > sge.sge_length)
264f931551bSRalph Campbell 			len = sge.sge_length;
265f931551bSRalph Campbell 		BUG_ON(len == 0);
266f931551bSRalph Campbell 		if (((long) sge.vaddr & (sizeof(u32) - 1)) ||
267f931551bSRalph Campbell 		    (len != length && (len & (sizeof(u32) - 1)))) {
268f931551bSRalph Campbell 			ndesc = 0;
269f931551bSRalph Campbell 			break;
270f931551bSRalph Campbell 		}
271f931551bSRalph Campbell 		ndesc++;
272f931551bSRalph Campbell 		sge.vaddr += len;
273f931551bSRalph Campbell 		sge.length -= len;
274f931551bSRalph Campbell 		sge.sge_length -= len;
275f931551bSRalph Campbell 		if (sge.sge_length == 0) {
276f931551bSRalph Campbell 			if (--num_sge)
277f931551bSRalph Campbell 				sge = *sg_list++;
278f931551bSRalph Campbell 		} else if (sge.length == 0 && sge.mr->lkey) {
279f931551bSRalph Campbell 			if (++sge.n >= QIB_SEGSZ) {
280f931551bSRalph Campbell 				if (++sge.m >= sge.mr->mapsz)
281f931551bSRalph Campbell 					break;
282f931551bSRalph Campbell 				sge.n = 0;
283f931551bSRalph Campbell 			}
284f931551bSRalph Campbell 			sge.vaddr =
285f931551bSRalph Campbell 				sge.mr->map[sge.m]->segs[sge.n].vaddr;
286f931551bSRalph Campbell 			sge.length =
287f931551bSRalph Campbell 				sge.mr->map[sge.m]->segs[sge.n].length;
288f931551bSRalph Campbell 		}
289f931551bSRalph Campbell 		length -= len;
290f931551bSRalph Campbell 	}
291f931551bSRalph Campbell 	return ndesc;
292f931551bSRalph Campbell }
293f931551bSRalph Campbell 
294f931551bSRalph Campbell /*
295f931551bSRalph Campbell  * Copy from the SGEs to the data buffer.
296f931551bSRalph Campbell  */
297f931551bSRalph Campbell static void qib_copy_from_sge(void *data, struct qib_sge_state *ss, u32 length)
298f931551bSRalph Campbell {
299f931551bSRalph Campbell 	struct qib_sge *sge = &ss->sge;
300f931551bSRalph Campbell 
301f931551bSRalph Campbell 	while (length) {
302f931551bSRalph Campbell 		u32 len = sge->length;
303f931551bSRalph Campbell 
304f931551bSRalph Campbell 		if (len > length)
305f931551bSRalph Campbell 			len = length;
306f931551bSRalph Campbell 		if (len > sge->sge_length)
307f931551bSRalph Campbell 			len = sge->sge_length;
308f931551bSRalph Campbell 		BUG_ON(len == 0);
309f931551bSRalph Campbell 		memcpy(data, sge->vaddr, len);
310f931551bSRalph Campbell 		sge->vaddr += len;
311f931551bSRalph Campbell 		sge->length -= len;
312f931551bSRalph Campbell 		sge->sge_length -= len;
313f931551bSRalph Campbell 		if (sge->sge_length == 0) {
314f931551bSRalph Campbell 			if (--ss->num_sge)
315f931551bSRalph Campbell 				*sge = *ss->sg_list++;
316f931551bSRalph Campbell 		} else if (sge->length == 0 && sge->mr->lkey) {
317f931551bSRalph Campbell 			if (++sge->n >= QIB_SEGSZ) {
318f931551bSRalph Campbell 				if (++sge->m >= sge->mr->mapsz)
319f931551bSRalph Campbell 					break;
320f931551bSRalph Campbell 				sge->n = 0;
321f931551bSRalph Campbell 			}
322f931551bSRalph Campbell 			sge->vaddr =
323f931551bSRalph Campbell 				sge->mr->map[sge->m]->segs[sge->n].vaddr;
324f931551bSRalph Campbell 			sge->length =
325f931551bSRalph Campbell 				sge->mr->map[sge->m]->segs[sge->n].length;
326f931551bSRalph Campbell 		}
327f931551bSRalph Campbell 		data += len;
328f931551bSRalph Campbell 		length -= len;
329f931551bSRalph Campbell 	}
330f931551bSRalph Campbell }
331f931551bSRalph Campbell 
332f931551bSRalph Campbell /**
333f931551bSRalph Campbell  * qib_post_one_send - post one RC, UC, or UD send work request
334f931551bSRalph Campbell  * @qp: the QP to post on
335f931551bSRalph Campbell  * @wr: the work request to send
336f931551bSRalph Campbell  */
337551ace12SMike Marciniszyn static int qib_post_one_send(struct qib_qp *qp, struct ib_send_wr *wr,
338551ace12SMike Marciniszyn 	int *scheduled)
339f931551bSRalph Campbell {
340f931551bSRalph Campbell 	struct qib_swqe *wqe;
341f931551bSRalph Campbell 	u32 next;
342f931551bSRalph Campbell 	int i;
343f931551bSRalph Campbell 	int j;
344f931551bSRalph Campbell 	int acc;
345f931551bSRalph Campbell 	int ret;
346f931551bSRalph Campbell 	unsigned long flags;
347f931551bSRalph Campbell 	struct qib_lkey_table *rkt;
348f931551bSRalph Campbell 	struct qib_pd *pd;
349f931551bSRalph Campbell 
350f931551bSRalph Campbell 	spin_lock_irqsave(&qp->s_lock, flags);
351f931551bSRalph Campbell 
352f931551bSRalph Campbell 	/* Check that state is OK to post send. */
353f931551bSRalph Campbell 	if (unlikely(!(ib_qib_state_ops[qp->state] & QIB_POST_SEND_OK)))
354f931551bSRalph Campbell 		goto bail_inval;
355f931551bSRalph Campbell 
356f931551bSRalph Campbell 	/* IB spec says that num_sge == 0 is OK. */
357f931551bSRalph Campbell 	if (wr->num_sge > qp->s_max_sge)
358f931551bSRalph Campbell 		goto bail_inval;
359f931551bSRalph Campbell 
360f931551bSRalph Campbell 	/*
361f931551bSRalph Campbell 	 * Don't allow RDMA reads or atomic operations on UC or
362f931551bSRalph Campbell 	 * undefined operations.
363f931551bSRalph Campbell 	 * Make sure buffer is large enough to hold the result for atomics.
364f931551bSRalph Campbell 	 */
365f931551bSRalph Campbell 	if (wr->opcode == IB_WR_FAST_REG_MR) {
366f931551bSRalph Campbell 		if (qib_fast_reg_mr(qp, wr))
367f931551bSRalph Campbell 			goto bail_inval;
368f931551bSRalph Campbell 	} else if (qp->ibqp.qp_type == IB_QPT_UC) {
369f931551bSRalph Campbell 		if ((unsigned) wr->opcode >= IB_WR_RDMA_READ)
370f931551bSRalph Campbell 			goto bail_inval;
371f931551bSRalph Campbell 	} else if (qp->ibqp.qp_type != IB_QPT_RC) {
372f931551bSRalph Campbell 		/* Check IB_QPT_SMI, IB_QPT_GSI, IB_QPT_UD opcode */
373f931551bSRalph Campbell 		if (wr->opcode != IB_WR_SEND &&
374f931551bSRalph Campbell 		    wr->opcode != IB_WR_SEND_WITH_IMM)
375f931551bSRalph Campbell 			goto bail_inval;
376f931551bSRalph Campbell 		/* Check UD destination address PD */
377f931551bSRalph Campbell 		if (qp->ibqp.pd != wr->wr.ud.ah->pd)
378f931551bSRalph Campbell 			goto bail_inval;
379f931551bSRalph Campbell 	} else if ((unsigned) wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD)
380f931551bSRalph Campbell 		goto bail_inval;
381f931551bSRalph Campbell 	else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
382f931551bSRalph Campbell 		   (wr->num_sge == 0 ||
383f931551bSRalph Campbell 		    wr->sg_list[0].length < sizeof(u64) ||
384f931551bSRalph Campbell 		    wr->sg_list[0].addr & (sizeof(u64) - 1)))
385f931551bSRalph Campbell 		goto bail_inval;
386f931551bSRalph Campbell 	else if (wr->opcode >= IB_WR_RDMA_READ && !qp->s_max_rd_atomic)
387f931551bSRalph Campbell 		goto bail_inval;
388f931551bSRalph Campbell 
389f931551bSRalph Campbell 	next = qp->s_head + 1;
390f931551bSRalph Campbell 	if (next >= qp->s_size)
391f931551bSRalph Campbell 		next = 0;
392f931551bSRalph Campbell 	if (next == qp->s_last) {
393f931551bSRalph Campbell 		ret = -ENOMEM;
394f931551bSRalph Campbell 		goto bail;
395f931551bSRalph Campbell 	}
396f931551bSRalph Campbell 
397f931551bSRalph Campbell 	rkt = &to_idev(qp->ibqp.device)->lk_table;
398f931551bSRalph Campbell 	pd = to_ipd(qp->ibqp.pd);
399f931551bSRalph Campbell 	wqe = get_swqe_ptr(qp, qp->s_head);
400f931551bSRalph Campbell 	wqe->wr = *wr;
401f931551bSRalph Campbell 	wqe->length = 0;
402f931551bSRalph Campbell 	j = 0;
403f931551bSRalph Campbell 	if (wr->num_sge) {
404f931551bSRalph Campbell 		acc = wr->opcode >= IB_WR_RDMA_READ ?
405f931551bSRalph Campbell 			IB_ACCESS_LOCAL_WRITE : 0;
406f931551bSRalph Campbell 		for (i = 0; i < wr->num_sge; i++) {
407f931551bSRalph Campbell 			u32 length = wr->sg_list[i].length;
408f931551bSRalph Campbell 			int ok;
409f931551bSRalph Campbell 
410f931551bSRalph Campbell 			if (length == 0)
411f931551bSRalph Campbell 				continue;
412f931551bSRalph Campbell 			ok = qib_lkey_ok(rkt, pd, &wqe->sg_list[j],
413f931551bSRalph Campbell 					 &wr->sg_list[i], acc);
414f931551bSRalph Campbell 			if (!ok)
415f931551bSRalph Campbell 				goto bail_inval_free;
416f931551bSRalph Campbell 			wqe->length += length;
417f931551bSRalph Campbell 			j++;
418f931551bSRalph Campbell 		}
419f931551bSRalph Campbell 		wqe->wr.num_sge = j;
420f931551bSRalph Campbell 	}
421f931551bSRalph Campbell 	if (qp->ibqp.qp_type == IB_QPT_UC ||
422f931551bSRalph Campbell 	    qp->ibqp.qp_type == IB_QPT_RC) {
423f931551bSRalph Campbell 		if (wqe->length > 0x80000000U)
424f931551bSRalph Campbell 			goto bail_inval_free;
425f931551bSRalph Campbell 	} else if (wqe->length > (dd_from_ibdev(qp->ibqp.device)->pport +
426f931551bSRalph Campbell 				  qp->port_num - 1)->ibmtu)
427f931551bSRalph Campbell 		goto bail_inval_free;
428f931551bSRalph Campbell 	else
429f931551bSRalph Campbell 		atomic_inc(&to_iah(wr->wr.ud.ah)->refcount);
430f931551bSRalph Campbell 	wqe->ssn = qp->s_ssn++;
431f931551bSRalph Campbell 	qp->s_head = next;
432f931551bSRalph Campbell 
433f931551bSRalph Campbell 	ret = 0;
434f931551bSRalph Campbell 	goto bail;
435f931551bSRalph Campbell 
436f931551bSRalph Campbell bail_inval_free:
437f931551bSRalph Campbell 	while (j) {
438f931551bSRalph Campbell 		struct qib_sge *sge = &wqe->sg_list[--j];
439f931551bSRalph Campbell 
4406a82649fSMike Marciniszyn 		qib_put_mr(sge->mr);
441f931551bSRalph Campbell 	}
442f931551bSRalph Campbell bail_inval:
443f931551bSRalph Campbell 	ret = -EINVAL;
444f931551bSRalph Campbell bail:
445551ace12SMike Marciniszyn 	if (!ret && !wr->next &&
446551ace12SMike Marciniszyn 	 !qib_sdma_empty(
447551ace12SMike Marciniszyn 	   dd_from_ibdev(qp->ibqp.device)->pport + qp->port_num - 1)) {
448551ace12SMike Marciniszyn 		qib_schedule_send(qp);
449551ace12SMike Marciniszyn 		*scheduled = 1;
450551ace12SMike Marciniszyn 	}
451f931551bSRalph Campbell 	spin_unlock_irqrestore(&qp->s_lock, flags);
452f931551bSRalph Campbell 	return ret;
453f931551bSRalph Campbell }
454f931551bSRalph Campbell 
455f931551bSRalph Campbell /**
456f931551bSRalph Campbell  * qib_post_send - post a send on a QP
457f931551bSRalph Campbell  * @ibqp: the QP to post the send on
458f931551bSRalph Campbell  * @wr: the list of work requests to post
459f931551bSRalph Campbell  * @bad_wr: the first bad WR is put here
460f931551bSRalph Campbell  *
461f931551bSRalph Campbell  * This may be called from interrupt context.
462f931551bSRalph Campbell  */
463f931551bSRalph Campbell static int qib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
464f931551bSRalph Campbell 			 struct ib_send_wr **bad_wr)
465f931551bSRalph Campbell {
466f931551bSRalph Campbell 	struct qib_qp *qp = to_iqp(ibqp);
467f931551bSRalph Campbell 	int err = 0;
468551ace12SMike Marciniszyn 	int scheduled = 0;
469f931551bSRalph Campbell 
470f931551bSRalph Campbell 	for (; wr; wr = wr->next) {
471551ace12SMike Marciniszyn 		err = qib_post_one_send(qp, wr, &scheduled);
472f931551bSRalph Campbell 		if (err) {
473f931551bSRalph Campbell 			*bad_wr = wr;
474f931551bSRalph Campbell 			goto bail;
475f931551bSRalph Campbell 		}
476f931551bSRalph Campbell 	}
477f931551bSRalph Campbell 
478f931551bSRalph Campbell 	/* Try to do the send work in the caller's context. */
479551ace12SMike Marciniszyn 	if (!scheduled)
480f931551bSRalph Campbell 		qib_do_send(&qp->s_work);
481f931551bSRalph Campbell 
482f931551bSRalph Campbell bail:
483f931551bSRalph Campbell 	return err;
484f931551bSRalph Campbell }
485f931551bSRalph Campbell 
486f931551bSRalph Campbell /**
487f931551bSRalph Campbell  * qib_post_receive - post a receive on a QP
488f931551bSRalph Campbell  * @ibqp: the QP to post the receive on
489f931551bSRalph Campbell  * @wr: the WR to post
490f931551bSRalph Campbell  * @bad_wr: the first bad WR is put here
491f931551bSRalph Campbell  *
492f931551bSRalph Campbell  * This may be called from interrupt context.
493f931551bSRalph Campbell  */
494f931551bSRalph Campbell static int qib_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
495f931551bSRalph Campbell 			    struct ib_recv_wr **bad_wr)
496f931551bSRalph Campbell {
497f931551bSRalph Campbell 	struct qib_qp *qp = to_iqp(ibqp);
498f931551bSRalph Campbell 	struct qib_rwq *wq = qp->r_rq.wq;
499f931551bSRalph Campbell 	unsigned long flags;
500f931551bSRalph Campbell 	int ret;
501f931551bSRalph Campbell 
502f931551bSRalph Campbell 	/* Check that state is OK to post receive. */
503f931551bSRalph Campbell 	if (!(ib_qib_state_ops[qp->state] & QIB_POST_RECV_OK) || !wq) {
504f931551bSRalph Campbell 		*bad_wr = wr;
505f931551bSRalph Campbell 		ret = -EINVAL;
506f931551bSRalph Campbell 		goto bail;
507f931551bSRalph Campbell 	}
508f931551bSRalph Campbell 
509f931551bSRalph Campbell 	for (; wr; wr = wr->next) {
510f931551bSRalph Campbell 		struct qib_rwqe *wqe;
511f931551bSRalph Campbell 		u32 next;
512f931551bSRalph Campbell 		int i;
513f931551bSRalph Campbell 
514f931551bSRalph Campbell 		if ((unsigned) wr->num_sge > qp->r_rq.max_sge) {
515f931551bSRalph Campbell 			*bad_wr = wr;
516f931551bSRalph Campbell 			ret = -EINVAL;
517f931551bSRalph Campbell 			goto bail;
518f931551bSRalph Campbell 		}
519f931551bSRalph Campbell 
520f931551bSRalph Campbell 		spin_lock_irqsave(&qp->r_rq.lock, flags);
521f931551bSRalph Campbell 		next = wq->head + 1;
522f931551bSRalph Campbell 		if (next >= qp->r_rq.size)
523f931551bSRalph Campbell 			next = 0;
524f931551bSRalph Campbell 		if (next == wq->tail) {
525f931551bSRalph Campbell 			spin_unlock_irqrestore(&qp->r_rq.lock, flags);
526f931551bSRalph Campbell 			*bad_wr = wr;
527f931551bSRalph Campbell 			ret = -ENOMEM;
528f931551bSRalph Campbell 			goto bail;
529f931551bSRalph Campbell 		}
530f931551bSRalph Campbell 
531f931551bSRalph Campbell 		wqe = get_rwqe_ptr(&qp->r_rq, wq->head);
532f931551bSRalph Campbell 		wqe->wr_id = wr->wr_id;
533f931551bSRalph Campbell 		wqe->num_sge = wr->num_sge;
534f931551bSRalph Campbell 		for (i = 0; i < wr->num_sge; i++)
535f931551bSRalph Campbell 			wqe->sg_list[i] = wr->sg_list[i];
536f931551bSRalph Campbell 		/* Make sure queue entry is written before the head index. */
537f931551bSRalph Campbell 		smp_wmb();
538f931551bSRalph Campbell 		wq->head = next;
539f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->r_rq.lock, flags);
540f931551bSRalph Campbell 	}
541f931551bSRalph Campbell 	ret = 0;
542f931551bSRalph Campbell 
543f931551bSRalph Campbell bail:
544f931551bSRalph Campbell 	return ret;
545f931551bSRalph Campbell }
546f931551bSRalph Campbell 
547f931551bSRalph Campbell /**
548f931551bSRalph Campbell  * qib_qp_rcv - processing an incoming packet on a QP
549f931551bSRalph Campbell  * @rcd: the context pointer
550f931551bSRalph Campbell  * @hdr: the packet header
551f931551bSRalph Campbell  * @has_grh: true if the packet has a GRH
552f931551bSRalph Campbell  * @data: the packet data
553f931551bSRalph Campbell  * @tlen: the packet length
554f931551bSRalph Campbell  * @qp: the QP the packet came on
555f931551bSRalph Campbell  *
556f931551bSRalph Campbell  * This is called from qib_ib_rcv() to process an incoming packet
557f931551bSRalph Campbell  * for the given QP.
558f931551bSRalph Campbell  * Called at interrupt level.
559f931551bSRalph Campbell  */
560f931551bSRalph Campbell static void qib_qp_rcv(struct qib_ctxtdata *rcd, struct qib_ib_header *hdr,
561f931551bSRalph Campbell 		       int has_grh, void *data, u32 tlen, struct qib_qp *qp)
562f931551bSRalph Campbell {
563f931551bSRalph Campbell 	struct qib_ibport *ibp = &rcd->ppd->ibport_data;
564f931551bSRalph Campbell 
565a5210c12SRalph Campbell 	spin_lock(&qp->r_lock);
566a5210c12SRalph Campbell 
567f931551bSRalph Campbell 	/* Check for valid receive state. */
568f931551bSRalph Campbell 	if (!(ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK)) {
569f931551bSRalph Campbell 		ibp->n_pkt_drops++;
570a5210c12SRalph Campbell 		goto unlock;
571f931551bSRalph Campbell 	}
572f931551bSRalph Campbell 
573f931551bSRalph Campbell 	switch (qp->ibqp.qp_type) {
574f931551bSRalph Campbell 	case IB_QPT_SMI:
575f931551bSRalph Campbell 	case IB_QPT_GSI:
576f931551bSRalph Campbell 		if (ib_qib_disable_sma)
577f931551bSRalph Campbell 			break;
578f931551bSRalph Campbell 		/* FALLTHROUGH */
579f931551bSRalph Campbell 	case IB_QPT_UD:
580f931551bSRalph Campbell 		qib_ud_rcv(ibp, hdr, has_grh, data, tlen, qp);
581f931551bSRalph Campbell 		break;
582f931551bSRalph Campbell 
583f931551bSRalph Campbell 	case IB_QPT_RC:
584f931551bSRalph Campbell 		qib_rc_rcv(rcd, hdr, has_grh, data, tlen, qp);
585f931551bSRalph Campbell 		break;
586f931551bSRalph Campbell 
587f931551bSRalph Campbell 	case IB_QPT_UC:
588f931551bSRalph Campbell 		qib_uc_rcv(ibp, hdr, has_grh, data, tlen, qp);
589f931551bSRalph Campbell 		break;
590f931551bSRalph Campbell 
591f931551bSRalph Campbell 	default:
592f931551bSRalph Campbell 		break;
593f931551bSRalph Campbell 	}
594a5210c12SRalph Campbell 
595a5210c12SRalph Campbell unlock:
596a5210c12SRalph Campbell 	spin_unlock(&qp->r_lock);
597f931551bSRalph Campbell }
598f931551bSRalph Campbell 
599f931551bSRalph Campbell /**
600f931551bSRalph Campbell  * qib_ib_rcv - process an incoming packet
601f931551bSRalph Campbell  * @rcd: the context pointer
602f931551bSRalph Campbell  * @rhdr: the header of the packet
603f931551bSRalph Campbell  * @data: the packet payload
604f931551bSRalph Campbell  * @tlen: the packet length
605f931551bSRalph Campbell  *
606f931551bSRalph Campbell  * This is called from qib_kreceive() to process an incoming packet at
607f931551bSRalph Campbell  * interrupt level. Tlen is the length of the header + data + CRC in bytes.
608f931551bSRalph Campbell  */
609f931551bSRalph Campbell void qib_ib_rcv(struct qib_ctxtdata *rcd, void *rhdr, void *data, u32 tlen)
610f931551bSRalph Campbell {
611f931551bSRalph Campbell 	struct qib_pportdata *ppd = rcd->ppd;
612f931551bSRalph Campbell 	struct qib_ibport *ibp = &ppd->ibport_data;
613f931551bSRalph Campbell 	struct qib_ib_header *hdr = rhdr;
614f931551bSRalph Campbell 	struct qib_other_headers *ohdr;
615f931551bSRalph Campbell 	struct qib_qp *qp;
616f931551bSRalph Campbell 	u32 qp_num;
617f931551bSRalph Campbell 	int lnh;
618f931551bSRalph Campbell 	u8 opcode;
619f931551bSRalph Campbell 	u16 lid;
620f931551bSRalph Campbell 
621f931551bSRalph Campbell 	/* 24 == LRH+BTH+CRC */
622f931551bSRalph Campbell 	if (unlikely(tlen < 24))
623f931551bSRalph Campbell 		goto drop;
624f931551bSRalph Campbell 
625f931551bSRalph Campbell 	/* Check for a valid destination LID (see ch. 7.11.1). */
626f931551bSRalph Campbell 	lid = be16_to_cpu(hdr->lrh[1]);
627f931551bSRalph Campbell 	if (lid < QIB_MULTICAST_LID_BASE) {
628f931551bSRalph Campbell 		lid &= ~((1 << ppd->lmc) - 1);
629f931551bSRalph Campbell 		if (unlikely(lid != ppd->lid))
630f931551bSRalph Campbell 			goto drop;
631f931551bSRalph Campbell 	}
632f931551bSRalph Campbell 
633f931551bSRalph Campbell 	/* Check for GRH */
634f931551bSRalph Campbell 	lnh = be16_to_cpu(hdr->lrh[0]) & 3;
635f931551bSRalph Campbell 	if (lnh == QIB_LRH_BTH)
636f931551bSRalph Campbell 		ohdr = &hdr->u.oth;
637f931551bSRalph Campbell 	else if (lnh == QIB_LRH_GRH) {
638f931551bSRalph Campbell 		u32 vtf;
639f931551bSRalph Campbell 
640f931551bSRalph Campbell 		ohdr = &hdr->u.l.oth;
641f931551bSRalph Campbell 		if (hdr->u.l.grh.next_hdr != IB_GRH_NEXT_HDR)
642f931551bSRalph Campbell 			goto drop;
643f931551bSRalph Campbell 		vtf = be32_to_cpu(hdr->u.l.grh.version_tclass_flow);
644f931551bSRalph Campbell 		if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
645f931551bSRalph Campbell 			goto drop;
646f931551bSRalph Campbell 	} else
647f931551bSRalph Campbell 		goto drop;
648f931551bSRalph Campbell 
649ddb88765SMike Marciniszyn 	opcode = (be32_to_cpu(ohdr->bth[0]) >> 24) & 0x7f;
650ddb88765SMike Marciniszyn #ifdef CONFIG_DEBUG_FS
651ddb88765SMike Marciniszyn 	rcd->opstats->stats[opcode].n_bytes += tlen;
652ddb88765SMike Marciniszyn 	rcd->opstats->stats[opcode].n_packets++;
653ddb88765SMike Marciniszyn #endif
654f931551bSRalph Campbell 
655f931551bSRalph Campbell 	/* Get the destination QP number. */
656f931551bSRalph Campbell 	qp_num = be32_to_cpu(ohdr->bth[1]) & QIB_QPN_MASK;
657f931551bSRalph Campbell 	if (qp_num == QIB_MULTICAST_QPN) {
658f931551bSRalph Campbell 		struct qib_mcast *mcast;
659f931551bSRalph Campbell 		struct qib_mcast_qp *p;
660f931551bSRalph Campbell 
661f931551bSRalph Campbell 		if (lnh != QIB_LRH_GRH)
662f931551bSRalph Campbell 			goto drop;
663f931551bSRalph Campbell 		mcast = qib_mcast_find(ibp, &hdr->u.l.grh.dgid);
664f931551bSRalph Campbell 		if (mcast == NULL)
665f931551bSRalph Campbell 			goto drop;
6667d7632adSMike Marciniszyn 		this_cpu_inc(ibp->pmastats->n_multicast_rcv);
667f931551bSRalph Campbell 		list_for_each_entry_rcu(p, &mcast->qp_list, list)
668f931551bSRalph Campbell 			qib_qp_rcv(rcd, hdr, 1, data, tlen, p->qp);
669f931551bSRalph Campbell 		/*
670f931551bSRalph Campbell 		 * Notify qib_multicast_detach() if it is waiting for us
671f931551bSRalph Campbell 		 * to finish.
672f931551bSRalph Campbell 		 */
673f931551bSRalph Campbell 		if (atomic_dec_return(&mcast->refcount) <= 1)
674f931551bSRalph Campbell 			wake_up(&mcast->wait);
675f931551bSRalph Campbell 	} else {
676af061a64SMike Marciniszyn 		if (rcd->lookaside_qp) {
677af061a64SMike Marciniszyn 			if (rcd->lookaside_qpn != qp_num) {
678af061a64SMike Marciniszyn 				if (atomic_dec_and_test(
679af061a64SMike Marciniszyn 					&rcd->lookaside_qp->refcount))
680af061a64SMike Marciniszyn 					wake_up(
681af061a64SMike Marciniszyn 					 &rcd->lookaside_qp->wait);
682af061a64SMike Marciniszyn 				rcd->lookaside_qp = NULL;
683af061a64SMike Marciniszyn 			}
684af061a64SMike Marciniszyn 		}
685af061a64SMike Marciniszyn 		if (!rcd->lookaside_qp) {
686f931551bSRalph Campbell 			qp = qib_lookup_qpn(ibp, qp_num);
687f931551bSRalph Campbell 			if (!qp)
688f931551bSRalph Campbell 				goto drop;
689af061a64SMike Marciniszyn 			rcd->lookaside_qp = qp;
690af061a64SMike Marciniszyn 			rcd->lookaside_qpn = qp_num;
691af061a64SMike Marciniszyn 		} else
692af061a64SMike Marciniszyn 			qp = rcd->lookaside_qp;
6937d7632adSMike Marciniszyn 		this_cpu_inc(ibp->pmastats->n_unicast_rcv);
694f931551bSRalph Campbell 		qib_qp_rcv(rcd, hdr, lnh == QIB_LRH_GRH, data, tlen, qp);
695f931551bSRalph Campbell 	}
696f931551bSRalph Campbell 	return;
697f931551bSRalph Campbell 
698f931551bSRalph Campbell drop:
699f931551bSRalph Campbell 	ibp->n_pkt_drops++;
700f931551bSRalph Campbell }
701f931551bSRalph Campbell 
702f931551bSRalph Campbell /*
703f931551bSRalph Campbell  * This is called from a timer to check for QPs
704f931551bSRalph Campbell  * which need kernel memory in order to send a packet.
705f931551bSRalph Campbell  */
706f931551bSRalph Campbell static void mem_timer(unsigned long data)
707f931551bSRalph Campbell {
708f931551bSRalph Campbell 	struct qib_ibdev *dev = (struct qib_ibdev *) data;
709f931551bSRalph Campbell 	struct list_head *list = &dev->memwait;
710f931551bSRalph Campbell 	struct qib_qp *qp = NULL;
711f931551bSRalph Campbell 	unsigned long flags;
712f931551bSRalph Campbell 
713f931551bSRalph Campbell 	spin_lock_irqsave(&dev->pending_lock, flags);
714f931551bSRalph Campbell 	if (!list_empty(list)) {
715f931551bSRalph Campbell 		qp = list_entry(list->next, struct qib_qp, iowait);
716f931551bSRalph Campbell 		list_del_init(&qp->iowait);
717f931551bSRalph Campbell 		atomic_inc(&qp->refcount);
718f931551bSRalph Campbell 		if (!list_empty(list))
719f931551bSRalph Campbell 			mod_timer(&dev->mem_timer, jiffies + 1);
720f931551bSRalph Campbell 	}
721f931551bSRalph Campbell 	spin_unlock_irqrestore(&dev->pending_lock, flags);
722f931551bSRalph Campbell 
723f931551bSRalph Campbell 	if (qp) {
724f931551bSRalph Campbell 		spin_lock_irqsave(&qp->s_lock, flags);
725f931551bSRalph Campbell 		if (qp->s_flags & QIB_S_WAIT_KMEM) {
726f931551bSRalph Campbell 			qp->s_flags &= ~QIB_S_WAIT_KMEM;
727f931551bSRalph Campbell 			qib_schedule_send(qp);
728f931551bSRalph Campbell 		}
729f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
730f931551bSRalph Campbell 		if (atomic_dec_and_test(&qp->refcount))
731f931551bSRalph Campbell 			wake_up(&qp->wait);
732f931551bSRalph Campbell 	}
733f931551bSRalph Campbell }
734f931551bSRalph Campbell 
735f931551bSRalph Campbell static void update_sge(struct qib_sge_state *ss, u32 length)
736f931551bSRalph Campbell {
737f931551bSRalph Campbell 	struct qib_sge *sge = &ss->sge;
738f931551bSRalph Campbell 
739f931551bSRalph Campbell 	sge->vaddr += length;
740f931551bSRalph Campbell 	sge->length -= length;
741f931551bSRalph Campbell 	sge->sge_length -= length;
742f931551bSRalph Campbell 	if (sge->sge_length == 0) {
743f931551bSRalph Campbell 		if (--ss->num_sge)
744f931551bSRalph Campbell 			*sge = *ss->sg_list++;
745f931551bSRalph Campbell 	} else if (sge->length == 0 && sge->mr->lkey) {
746f931551bSRalph Campbell 		if (++sge->n >= QIB_SEGSZ) {
747f931551bSRalph Campbell 			if (++sge->m >= sge->mr->mapsz)
748f931551bSRalph Campbell 				return;
749f931551bSRalph Campbell 			sge->n = 0;
750f931551bSRalph Campbell 		}
751f931551bSRalph Campbell 		sge->vaddr = sge->mr->map[sge->m]->segs[sge->n].vaddr;
752f931551bSRalph Campbell 		sge->length = sge->mr->map[sge->m]->segs[sge->n].length;
753f931551bSRalph Campbell 	}
754f931551bSRalph Campbell }
755f931551bSRalph Campbell 
756f931551bSRalph Campbell #ifdef __LITTLE_ENDIAN
757f931551bSRalph Campbell static inline u32 get_upper_bits(u32 data, u32 shift)
758f931551bSRalph Campbell {
759f931551bSRalph Campbell 	return data >> shift;
760f931551bSRalph Campbell }
761f931551bSRalph Campbell 
762f931551bSRalph Campbell static inline u32 set_upper_bits(u32 data, u32 shift)
763f931551bSRalph Campbell {
764f931551bSRalph Campbell 	return data << shift;
765f931551bSRalph Campbell }
766f931551bSRalph Campbell 
767f931551bSRalph Campbell static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
768f931551bSRalph Campbell {
769f931551bSRalph Campbell 	data <<= ((sizeof(u32) - n) * BITS_PER_BYTE);
770f931551bSRalph Campbell 	data >>= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
771f931551bSRalph Campbell 	return data;
772f931551bSRalph Campbell }
773f931551bSRalph Campbell #else
774f931551bSRalph Campbell static inline u32 get_upper_bits(u32 data, u32 shift)
775f931551bSRalph Campbell {
776f931551bSRalph Campbell 	return data << shift;
777f931551bSRalph Campbell }
778f931551bSRalph Campbell 
779f931551bSRalph Campbell static inline u32 set_upper_bits(u32 data, u32 shift)
780f931551bSRalph Campbell {
781f931551bSRalph Campbell 	return data >> shift;
782f931551bSRalph Campbell }
783f931551bSRalph Campbell 
784f931551bSRalph Campbell static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
785f931551bSRalph Campbell {
786f931551bSRalph Campbell 	data >>= ((sizeof(u32) - n) * BITS_PER_BYTE);
787f931551bSRalph Campbell 	data <<= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
788f931551bSRalph Campbell 	return data;
789f931551bSRalph Campbell }
790f931551bSRalph Campbell #endif
791f931551bSRalph Campbell 
792f931551bSRalph Campbell static void copy_io(u32 __iomem *piobuf, struct qib_sge_state *ss,
793f931551bSRalph Campbell 		    u32 length, unsigned flush_wc)
794f931551bSRalph Campbell {
795f931551bSRalph Campbell 	u32 extra = 0;
796f931551bSRalph Campbell 	u32 data = 0;
797f931551bSRalph Campbell 	u32 last;
798f931551bSRalph Campbell 
799f931551bSRalph Campbell 	while (1) {
800f931551bSRalph Campbell 		u32 len = ss->sge.length;
801f931551bSRalph Campbell 		u32 off;
802f931551bSRalph Campbell 
803f931551bSRalph Campbell 		if (len > length)
804f931551bSRalph Campbell 			len = length;
805f931551bSRalph Campbell 		if (len > ss->sge.sge_length)
806f931551bSRalph Campbell 			len = ss->sge.sge_length;
807f931551bSRalph Campbell 		BUG_ON(len == 0);
808f931551bSRalph Campbell 		/* If the source address is not aligned, try to align it. */
809f931551bSRalph Campbell 		off = (unsigned long)ss->sge.vaddr & (sizeof(u32) - 1);
810f931551bSRalph Campbell 		if (off) {
811f931551bSRalph Campbell 			u32 *addr = (u32 *)((unsigned long)ss->sge.vaddr &
812f931551bSRalph Campbell 					    ~(sizeof(u32) - 1));
813f931551bSRalph Campbell 			u32 v = get_upper_bits(*addr, off * BITS_PER_BYTE);
814f931551bSRalph Campbell 			u32 y;
815f931551bSRalph Campbell 
816f931551bSRalph Campbell 			y = sizeof(u32) - off;
817f931551bSRalph Campbell 			if (len > y)
818f931551bSRalph Campbell 				len = y;
819f931551bSRalph Campbell 			if (len + extra >= sizeof(u32)) {
820f931551bSRalph Campbell 				data |= set_upper_bits(v, extra *
821f931551bSRalph Campbell 						       BITS_PER_BYTE);
822f931551bSRalph Campbell 				len = sizeof(u32) - extra;
823f931551bSRalph Campbell 				if (len == length) {
824f931551bSRalph Campbell 					last = data;
825f931551bSRalph Campbell 					break;
826f931551bSRalph Campbell 				}
827f931551bSRalph Campbell 				__raw_writel(data, piobuf);
828f931551bSRalph Campbell 				piobuf++;
829f931551bSRalph Campbell 				extra = 0;
830f931551bSRalph Campbell 				data = 0;
831f931551bSRalph Campbell 			} else {
832f931551bSRalph Campbell 				/* Clear unused upper bytes */
833f931551bSRalph Campbell 				data |= clear_upper_bytes(v, len, extra);
834f931551bSRalph Campbell 				if (len == length) {
835f931551bSRalph Campbell 					last = data;
836f931551bSRalph Campbell 					break;
837f931551bSRalph Campbell 				}
838f931551bSRalph Campbell 				extra += len;
839f931551bSRalph Campbell 			}
840f931551bSRalph Campbell 		} else if (extra) {
841f931551bSRalph Campbell 			/* Source address is aligned. */
842f931551bSRalph Campbell 			u32 *addr = (u32 *) ss->sge.vaddr;
843f931551bSRalph Campbell 			int shift = extra * BITS_PER_BYTE;
844f931551bSRalph Campbell 			int ushift = 32 - shift;
845f931551bSRalph Campbell 			u32 l = len;
846f931551bSRalph Campbell 
847f931551bSRalph Campbell 			while (l >= sizeof(u32)) {
848f931551bSRalph Campbell 				u32 v = *addr;
849f931551bSRalph Campbell 
850f931551bSRalph Campbell 				data |= set_upper_bits(v, shift);
851f931551bSRalph Campbell 				__raw_writel(data, piobuf);
852f931551bSRalph Campbell 				data = get_upper_bits(v, ushift);
853f931551bSRalph Campbell 				piobuf++;
854f931551bSRalph Campbell 				addr++;
855f931551bSRalph Campbell 				l -= sizeof(u32);
856f931551bSRalph Campbell 			}
857f931551bSRalph Campbell 			/*
858f931551bSRalph Campbell 			 * We still have 'extra' number of bytes leftover.
859f931551bSRalph Campbell 			 */
860f931551bSRalph Campbell 			if (l) {
861f931551bSRalph Campbell 				u32 v = *addr;
862f931551bSRalph Campbell 
863f931551bSRalph Campbell 				if (l + extra >= sizeof(u32)) {
864f931551bSRalph Campbell 					data |= set_upper_bits(v, shift);
865f931551bSRalph Campbell 					len -= l + extra - sizeof(u32);
866f931551bSRalph Campbell 					if (len == length) {
867f931551bSRalph Campbell 						last = data;
868f931551bSRalph Campbell 						break;
869f931551bSRalph Campbell 					}
870f931551bSRalph Campbell 					__raw_writel(data, piobuf);
871f931551bSRalph Campbell 					piobuf++;
872f931551bSRalph Campbell 					extra = 0;
873f931551bSRalph Campbell 					data = 0;
874f931551bSRalph Campbell 				} else {
875f931551bSRalph Campbell 					/* Clear unused upper bytes */
876f931551bSRalph Campbell 					data |= clear_upper_bytes(v, l, extra);
877f931551bSRalph Campbell 					if (len == length) {
878f931551bSRalph Campbell 						last = data;
879f931551bSRalph Campbell 						break;
880f931551bSRalph Campbell 					}
881f931551bSRalph Campbell 					extra += l;
882f931551bSRalph Campbell 				}
883f931551bSRalph Campbell 			} else if (len == length) {
884f931551bSRalph Campbell 				last = data;
885f931551bSRalph Campbell 				break;
886f931551bSRalph Campbell 			}
887f931551bSRalph Campbell 		} else if (len == length) {
888f931551bSRalph Campbell 			u32 w;
889f931551bSRalph Campbell 
890f931551bSRalph Campbell 			/*
891f931551bSRalph Campbell 			 * Need to round up for the last dword in the
892f931551bSRalph Campbell 			 * packet.
893f931551bSRalph Campbell 			 */
894f931551bSRalph Campbell 			w = (len + 3) >> 2;
895f931551bSRalph Campbell 			qib_pio_copy(piobuf, ss->sge.vaddr, w - 1);
896f931551bSRalph Campbell 			piobuf += w - 1;
897f931551bSRalph Campbell 			last = ((u32 *) ss->sge.vaddr)[w - 1];
898f931551bSRalph Campbell 			break;
899f931551bSRalph Campbell 		} else {
900f931551bSRalph Campbell 			u32 w = len >> 2;
901f931551bSRalph Campbell 
902f931551bSRalph Campbell 			qib_pio_copy(piobuf, ss->sge.vaddr, w);
903f931551bSRalph Campbell 			piobuf += w;
904f931551bSRalph Campbell 
905f931551bSRalph Campbell 			extra = len & (sizeof(u32) - 1);
906f931551bSRalph Campbell 			if (extra) {
907f931551bSRalph Campbell 				u32 v = ((u32 *) ss->sge.vaddr)[w];
908f931551bSRalph Campbell 
909f931551bSRalph Campbell 				/* Clear unused upper bytes */
910f931551bSRalph Campbell 				data = clear_upper_bytes(v, extra, 0);
911f931551bSRalph Campbell 			}
912f931551bSRalph Campbell 		}
913f931551bSRalph Campbell 		update_sge(ss, len);
914f931551bSRalph Campbell 		length -= len;
915f931551bSRalph Campbell 	}
916f931551bSRalph Campbell 	/* Update address before sending packet. */
917f931551bSRalph Campbell 	update_sge(ss, length);
918f931551bSRalph Campbell 	if (flush_wc) {
919f931551bSRalph Campbell 		/* must flush early everything before trigger word */
920f931551bSRalph Campbell 		qib_flush_wc();
921f931551bSRalph Campbell 		__raw_writel(last, piobuf);
922f931551bSRalph Campbell 		/* be sure trigger word is written */
923f931551bSRalph Campbell 		qib_flush_wc();
924f931551bSRalph Campbell 	} else
925f931551bSRalph Campbell 		__raw_writel(last, piobuf);
926f931551bSRalph Campbell }
927f931551bSRalph Campbell 
92848947109SMike Marciniszyn static noinline struct qib_verbs_txreq *__get_txreq(struct qib_ibdev *dev,
92948947109SMike Marciniszyn 					   struct qib_qp *qp)
930f931551bSRalph Campbell {
931f931551bSRalph Campbell 	struct qib_verbs_txreq *tx;
932f931551bSRalph Campbell 	unsigned long flags;
933f931551bSRalph Campbell 
934f931551bSRalph Campbell 	spin_lock_irqsave(&qp->s_lock, flags);
935f931551bSRalph Campbell 	spin_lock(&dev->pending_lock);
936f931551bSRalph Campbell 
937f931551bSRalph Campbell 	if (!list_empty(&dev->txreq_free)) {
938f931551bSRalph Campbell 		struct list_head *l = dev->txreq_free.next;
939f931551bSRalph Campbell 
940f931551bSRalph Campbell 		list_del(l);
94148947109SMike Marciniszyn 		spin_unlock(&dev->pending_lock);
94248947109SMike Marciniszyn 		spin_unlock_irqrestore(&qp->s_lock, flags);
943f931551bSRalph Campbell 		tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
944f931551bSRalph Campbell 	} else {
945f931551bSRalph Campbell 		if (ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK &&
946f931551bSRalph Campbell 		    list_empty(&qp->iowait)) {
947f931551bSRalph Campbell 			dev->n_txwait++;
948f931551bSRalph Campbell 			qp->s_flags |= QIB_S_WAIT_TX;
949f931551bSRalph Campbell 			list_add_tail(&qp->iowait, &dev->txwait);
950f931551bSRalph Campbell 		}
951f931551bSRalph Campbell 		qp->s_flags &= ~QIB_S_BUSY;
952f931551bSRalph Campbell 		spin_unlock(&dev->pending_lock);
953f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
95448947109SMike Marciniszyn 		tx = ERR_PTR(-EBUSY);
95548947109SMike Marciniszyn 	}
95648947109SMike Marciniszyn 	return tx;
95748947109SMike Marciniszyn }
958f931551bSRalph Campbell 
95948947109SMike Marciniszyn static inline struct qib_verbs_txreq *get_txreq(struct qib_ibdev *dev,
96048947109SMike Marciniszyn 					 struct qib_qp *qp)
96148947109SMike Marciniszyn {
96248947109SMike Marciniszyn 	struct qib_verbs_txreq *tx;
96348947109SMike Marciniszyn 	unsigned long flags;
96448947109SMike Marciniszyn 
96548947109SMike Marciniszyn 	spin_lock_irqsave(&dev->pending_lock, flags);
96648947109SMike Marciniszyn 	/* assume the list non empty */
96748947109SMike Marciniszyn 	if (likely(!list_empty(&dev->txreq_free))) {
96848947109SMike Marciniszyn 		struct list_head *l = dev->txreq_free.next;
96948947109SMike Marciniszyn 
97048947109SMike Marciniszyn 		list_del(l);
97148947109SMike Marciniszyn 		spin_unlock_irqrestore(&dev->pending_lock, flags);
97248947109SMike Marciniszyn 		tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
97348947109SMike Marciniszyn 	} else {
97448947109SMike Marciniszyn 		/* call slow path to get the extra lock */
97548947109SMike Marciniszyn 		spin_unlock_irqrestore(&dev->pending_lock, flags);
97648947109SMike Marciniszyn 		tx =  __get_txreq(dev, qp);
97748947109SMike Marciniszyn 	}
978f931551bSRalph Campbell 	return tx;
979f931551bSRalph Campbell }
980f931551bSRalph Campbell 
981f931551bSRalph Campbell void qib_put_txreq(struct qib_verbs_txreq *tx)
982f931551bSRalph Campbell {
983f931551bSRalph Campbell 	struct qib_ibdev *dev;
984f931551bSRalph Campbell 	struct qib_qp *qp;
985f931551bSRalph Campbell 	unsigned long flags;
986f931551bSRalph Campbell 
987f931551bSRalph Campbell 	qp = tx->qp;
988f931551bSRalph Campbell 	dev = to_idev(qp->ibqp.device);
989f931551bSRalph Campbell 
990f931551bSRalph Campbell 	if (atomic_dec_and_test(&qp->refcount))
991f931551bSRalph Campbell 		wake_up(&qp->wait);
992f931551bSRalph Campbell 	if (tx->mr) {
9936a82649fSMike Marciniszyn 		qib_put_mr(tx->mr);
994f931551bSRalph Campbell 		tx->mr = NULL;
995f931551bSRalph Campbell 	}
996f931551bSRalph Campbell 	if (tx->txreq.flags & QIB_SDMA_TXREQ_F_FREEBUF) {
997f931551bSRalph Campbell 		tx->txreq.flags &= ~QIB_SDMA_TXREQ_F_FREEBUF;
998f931551bSRalph Campbell 		dma_unmap_single(&dd_from_dev(dev)->pcidev->dev,
999f931551bSRalph Campbell 				 tx->txreq.addr, tx->hdr_dwords << 2,
1000f931551bSRalph Campbell 				 DMA_TO_DEVICE);
1001f931551bSRalph Campbell 		kfree(tx->align_buf);
1002f931551bSRalph Campbell 	}
1003f931551bSRalph Campbell 
1004f931551bSRalph Campbell 	spin_lock_irqsave(&dev->pending_lock, flags);
1005f931551bSRalph Campbell 
1006f931551bSRalph Campbell 	/* Put struct back on free list */
1007f931551bSRalph Campbell 	list_add(&tx->txreq.list, &dev->txreq_free);
1008f931551bSRalph Campbell 
1009f931551bSRalph Campbell 	if (!list_empty(&dev->txwait)) {
1010f931551bSRalph Campbell 		/* Wake up first QP wanting a free struct */
1011f931551bSRalph Campbell 		qp = list_entry(dev->txwait.next, struct qib_qp, iowait);
1012f931551bSRalph Campbell 		list_del_init(&qp->iowait);
1013f931551bSRalph Campbell 		atomic_inc(&qp->refcount);
1014f931551bSRalph Campbell 		spin_unlock_irqrestore(&dev->pending_lock, flags);
1015f931551bSRalph Campbell 
1016f931551bSRalph Campbell 		spin_lock_irqsave(&qp->s_lock, flags);
1017f931551bSRalph Campbell 		if (qp->s_flags & QIB_S_WAIT_TX) {
1018f931551bSRalph Campbell 			qp->s_flags &= ~QIB_S_WAIT_TX;
1019f931551bSRalph Campbell 			qib_schedule_send(qp);
1020f931551bSRalph Campbell 		}
1021f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
1022f931551bSRalph Campbell 
1023f931551bSRalph Campbell 		if (atomic_dec_and_test(&qp->refcount))
1024f931551bSRalph Campbell 			wake_up(&qp->wait);
1025f931551bSRalph Campbell 	} else
1026f931551bSRalph Campbell 		spin_unlock_irqrestore(&dev->pending_lock, flags);
1027f931551bSRalph Campbell }
1028f931551bSRalph Campbell 
1029f931551bSRalph Campbell /*
1030f931551bSRalph Campbell  * This is called when there are send DMA descriptors that might be
1031f931551bSRalph Campbell  * available.
1032f931551bSRalph Campbell  *
1033f931551bSRalph Campbell  * This is called with ppd->sdma_lock held.
1034f931551bSRalph Campbell  */
1035f931551bSRalph Campbell void qib_verbs_sdma_desc_avail(struct qib_pportdata *ppd, unsigned avail)
1036f931551bSRalph Campbell {
1037f931551bSRalph Campbell 	struct qib_qp *qp, *nqp;
1038f931551bSRalph Campbell 	struct qib_qp *qps[20];
1039f931551bSRalph Campbell 	struct qib_ibdev *dev;
1040f931551bSRalph Campbell 	unsigned i, n;
1041f931551bSRalph Campbell 
1042f931551bSRalph Campbell 	n = 0;
1043f931551bSRalph Campbell 	dev = &ppd->dd->verbs_dev;
1044f931551bSRalph Campbell 	spin_lock(&dev->pending_lock);
1045f931551bSRalph Campbell 
1046f931551bSRalph Campbell 	/* Search wait list for first QP wanting DMA descriptors. */
1047f931551bSRalph Campbell 	list_for_each_entry_safe(qp, nqp, &dev->dmawait, iowait) {
1048f931551bSRalph Campbell 		if (qp->port_num != ppd->port)
1049f931551bSRalph Campbell 			continue;
1050f931551bSRalph Campbell 		if (n == ARRAY_SIZE(qps))
1051f931551bSRalph Campbell 			break;
1052f931551bSRalph Campbell 		if (qp->s_tx->txreq.sg_count > avail)
1053f931551bSRalph Campbell 			break;
1054f931551bSRalph Campbell 		avail -= qp->s_tx->txreq.sg_count;
1055f931551bSRalph Campbell 		list_del_init(&qp->iowait);
1056f931551bSRalph Campbell 		atomic_inc(&qp->refcount);
1057f931551bSRalph Campbell 		qps[n++] = qp;
1058f931551bSRalph Campbell 	}
1059f931551bSRalph Campbell 
1060f931551bSRalph Campbell 	spin_unlock(&dev->pending_lock);
1061f931551bSRalph Campbell 
1062f931551bSRalph Campbell 	for (i = 0; i < n; i++) {
1063f931551bSRalph Campbell 		qp = qps[i];
1064f931551bSRalph Campbell 		spin_lock(&qp->s_lock);
1065f931551bSRalph Campbell 		if (qp->s_flags & QIB_S_WAIT_DMA_DESC) {
1066f931551bSRalph Campbell 			qp->s_flags &= ~QIB_S_WAIT_DMA_DESC;
1067f931551bSRalph Campbell 			qib_schedule_send(qp);
1068f931551bSRalph Campbell 		}
1069f931551bSRalph Campbell 		spin_unlock(&qp->s_lock);
1070f931551bSRalph Campbell 		if (atomic_dec_and_test(&qp->refcount))
1071f931551bSRalph Campbell 			wake_up(&qp->wait);
1072f931551bSRalph Campbell 	}
1073f931551bSRalph Campbell }
1074f931551bSRalph Campbell 
1075f931551bSRalph Campbell /*
1076f931551bSRalph Campbell  * This is called with ppd->sdma_lock held.
1077f931551bSRalph Campbell  */
1078f931551bSRalph Campbell static void sdma_complete(struct qib_sdma_txreq *cookie, int status)
1079f931551bSRalph Campbell {
1080f931551bSRalph Campbell 	struct qib_verbs_txreq *tx =
1081f931551bSRalph Campbell 		container_of(cookie, struct qib_verbs_txreq, txreq);
1082f931551bSRalph Campbell 	struct qib_qp *qp = tx->qp;
1083f931551bSRalph Campbell 
1084f931551bSRalph Campbell 	spin_lock(&qp->s_lock);
1085f931551bSRalph Campbell 	if (tx->wqe)
1086f931551bSRalph Campbell 		qib_send_complete(qp, tx->wqe, IB_WC_SUCCESS);
1087f931551bSRalph Campbell 	else if (qp->ibqp.qp_type == IB_QPT_RC) {
1088f931551bSRalph Campbell 		struct qib_ib_header *hdr;
1089f931551bSRalph Campbell 
1090f931551bSRalph Campbell 		if (tx->txreq.flags & QIB_SDMA_TXREQ_F_FREEBUF)
1091f931551bSRalph Campbell 			hdr = &tx->align_buf->hdr;
1092f931551bSRalph Campbell 		else {
1093f931551bSRalph Campbell 			struct qib_ibdev *dev = to_idev(qp->ibqp.device);
1094f931551bSRalph Campbell 
1095f931551bSRalph Campbell 			hdr = &dev->pio_hdrs[tx->hdr_inx].hdr;
1096f931551bSRalph Campbell 		}
1097f931551bSRalph Campbell 		qib_rc_send_complete(qp, hdr);
1098f931551bSRalph Campbell 	}
1099f931551bSRalph Campbell 	if (atomic_dec_and_test(&qp->s_dma_busy)) {
1100f931551bSRalph Campbell 		if (qp->state == IB_QPS_RESET)
1101f931551bSRalph Campbell 			wake_up(&qp->wait_dma);
1102f931551bSRalph Campbell 		else if (qp->s_flags & QIB_S_WAIT_DMA) {
1103f931551bSRalph Campbell 			qp->s_flags &= ~QIB_S_WAIT_DMA;
1104f931551bSRalph Campbell 			qib_schedule_send(qp);
1105f931551bSRalph Campbell 		}
1106f931551bSRalph Campbell 	}
1107f931551bSRalph Campbell 	spin_unlock(&qp->s_lock);
1108f931551bSRalph Campbell 
1109f931551bSRalph Campbell 	qib_put_txreq(tx);
1110f931551bSRalph Campbell }
1111f931551bSRalph Campbell 
1112f931551bSRalph Campbell static int wait_kmem(struct qib_ibdev *dev, struct qib_qp *qp)
1113f931551bSRalph Campbell {
1114f931551bSRalph Campbell 	unsigned long flags;
1115f931551bSRalph Campbell 	int ret = 0;
1116f931551bSRalph Campbell 
1117f931551bSRalph Campbell 	spin_lock_irqsave(&qp->s_lock, flags);
1118f931551bSRalph Campbell 	if (ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK) {
1119f931551bSRalph Campbell 		spin_lock(&dev->pending_lock);
1120f931551bSRalph Campbell 		if (list_empty(&qp->iowait)) {
1121f931551bSRalph Campbell 			if (list_empty(&dev->memwait))
1122f931551bSRalph Campbell 				mod_timer(&dev->mem_timer, jiffies + 1);
1123f931551bSRalph Campbell 			qp->s_flags |= QIB_S_WAIT_KMEM;
1124f931551bSRalph Campbell 			list_add_tail(&qp->iowait, &dev->memwait);
1125f931551bSRalph Campbell 		}
1126f931551bSRalph Campbell 		spin_unlock(&dev->pending_lock);
1127f931551bSRalph Campbell 		qp->s_flags &= ~QIB_S_BUSY;
1128f931551bSRalph Campbell 		ret = -EBUSY;
1129f931551bSRalph Campbell 	}
1130f931551bSRalph Campbell 	spin_unlock_irqrestore(&qp->s_lock, flags);
1131f931551bSRalph Campbell 
1132f931551bSRalph Campbell 	return ret;
1133f931551bSRalph Campbell }
1134f931551bSRalph Campbell 
1135f931551bSRalph Campbell static int qib_verbs_send_dma(struct qib_qp *qp, struct qib_ib_header *hdr,
1136f931551bSRalph Campbell 			      u32 hdrwords, struct qib_sge_state *ss, u32 len,
1137f931551bSRalph Campbell 			      u32 plen, u32 dwords)
1138f931551bSRalph Campbell {
1139f931551bSRalph Campbell 	struct qib_ibdev *dev = to_idev(qp->ibqp.device);
1140f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_dev(dev);
1141f931551bSRalph Campbell 	struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
1142f931551bSRalph Campbell 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1143f931551bSRalph Campbell 	struct qib_verbs_txreq *tx;
1144f931551bSRalph Campbell 	struct qib_pio_header *phdr;
1145f931551bSRalph Campbell 	u32 control;
1146f931551bSRalph Campbell 	u32 ndesc;
1147f931551bSRalph Campbell 	int ret;
1148f931551bSRalph Campbell 
1149f931551bSRalph Campbell 	tx = qp->s_tx;
1150f931551bSRalph Campbell 	if (tx) {
1151f931551bSRalph Campbell 		qp->s_tx = NULL;
1152f931551bSRalph Campbell 		/* resend previously constructed packet */
1153f931551bSRalph Campbell 		ret = qib_sdma_verbs_send(ppd, tx->ss, tx->dwords, tx);
1154f931551bSRalph Campbell 		goto bail;
1155f931551bSRalph Campbell 	}
1156f931551bSRalph Campbell 
115748947109SMike Marciniszyn 	tx = get_txreq(dev, qp);
115848947109SMike Marciniszyn 	if (IS_ERR(tx))
115948947109SMike Marciniszyn 		goto bail_tx;
1160f931551bSRalph Campbell 
1161f931551bSRalph Campbell 	control = dd->f_setpbc_control(ppd, plen, qp->s_srate,
1162f931551bSRalph Campbell 				       be16_to_cpu(hdr->lrh[0]) >> 12);
1163f931551bSRalph Campbell 	tx->qp = qp;
1164f931551bSRalph Campbell 	atomic_inc(&qp->refcount);
1165f931551bSRalph Campbell 	tx->wqe = qp->s_wqe;
1166f931551bSRalph Campbell 	tx->mr = qp->s_rdma_mr;
1167f931551bSRalph Campbell 	if (qp->s_rdma_mr)
1168f931551bSRalph Campbell 		qp->s_rdma_mr = NULL;
1169f931551bSRalph Campbell 	tx->txreq.callback = sdma_complete;
1170f931551bSRalph Campbell 	if (dd->flags & QIB_HAS_SDMA_TIMEOUT)
1171f931551bSRalph Campbell 		tx->txreq.flags = QIB_SDMA_TXREQ_F_HEADTOHOST;
1172f931551bSRalph Campbell 	else
1173f931551bSRalph Campbell 		tx->txreq.flags = QIB_SDMA_TXREQ_F_INTREQ;
1174f931551bSRalph Campbell 	if (plen + 1 > dd->piosize2kmax_dwords)
1175f931551bSRalph Campbell 		tx->txreq.flags |= QIB_SDMA_TXREQ_F_USELARGEBUF;
1176f931551bSRalph Campbell 
1177f931551bSRalph Campbell 	if (len) {
1178f931551bSRalph Campbell 		/*
1179f931551bSRalph Campbell 		 * Don't try to DMA if it takes more descriptors than
1180f931551bSRalph Campbell 		 * the queue holds.
1181f931551bSRalph Campbell 		 */
1182f931551bSRalph Campbell 		ndesc = qib_count_sge(ss, len);
1183f931551bSRalph Campbell 		if (ndesc >= ppd->sdma_descq_cnt)
1184f931551bSRalph Campbell 			ndesc = 0;
1185f931551bSRalph Campbell 	} else
1186f931551bSRalph Campbell 		ndesc = 1;
1187f931551bSRalph Campbell 	if (ndesc) {
1188f931551bSRalph Campbell 		phdr = &dev->pio_hdrs[tx->hdr_inx];
1189f931551bSRalph Campbell 		phdr->pbc[0] = cpu_to_le32(plen);
1190f931551bSRalph Campbell 		phdr->pbc[1] = cpu_to_le32(control);
1191f931551bSRalph Campbell 		memcpy(&phdr->hdr, hdr, hdrwords << 2);
1192f931551bSRalph Campbell 		tx->txreq.flags |= QIB_SDMA_TXREQ_F_FREEDESC;
1193f931551bSRalph Campbell 		tx->txreq.sg_count = ndesc;
1194f931551bSRalph Campbell 		tx->txreq.addr = dev->pio_hdrs_phys +
1195f931551bSRalph Campbell 			tx->hdr_inx * sizeof(struct qib_pio_header);
1196f931551bSRalph Campbell 		tx->hdr_dwords = hdrwords + 2; /* add PBC length */
1197f931551bSRalph Campbell 		ret = qib_sdma_verbs_send(ppd, ss, dwords, tx);
1198f931551bSRalph Campbell 		goto bail;
1199f931551bSRalph Campbell 	}
1200f931551bSRalph Campbell 
1201f931551bSRalph Campbell 	/* Allocate a buffer and copy the header and payload to it. */
1202f931551bSRalph Campbell 	tx->hdr_dwords = plen + 1;
1203f931551bSRalph Campbell 	phdr = kmalloc(tx->hdr_dwords << 2, GFP_ATOMIC);
1204f931551bSRalph Campbell 	if (!phdr)
1205f931551bSRalph Campbell 		goto err_tx;
1206f931551bSRalph Campbell 	phdr->pbc[0] = cpu_to_le32(plen);
1207f931551bSRalph Campbell 	phdr->pbc[1] = cpu_to_le32(control);
1208f931551bSRalph Campbell 	memcpy(&phdr->hdr, hdr, hdrwords << 2);
1209f931551bSRalph Campbell 	qib_copy_from_sge((u32 *) &phdr->hdr + hdrwords, ss, len);
1210f931551bSRalph Campbell 
1211f931551bSRalph Campbell 	tx->txreq.addr = dma_map_single(&dd->pcidev->dev, phdr,
1212f931551bSRalph Campbell 					tx->hdr_dwords << 2, DMA_TO_DEVICE);
1213f931551bSRalph Campbell 	if (dma_mapping_error(&dd->pcidev->dev, tx->txreq.addr))
1214f931551bSRalph Campbell 		goto map_err;
1215f931551bSRalph Campbell 	tx->align_buf = phdr;
1216f931551bSRalph Campbell 	tx->txreq.flags |= QIB_SDMA_TXREQ_F_FREEBUF;
1217f931551bSRalph Campbell 	tx->txreq.sg_count = 1;
1218f931551bSRalph Campbell 	ret = qib_sdma_verbs_send(ppd, NULL, 0, tx);
1219f931551bSRalph Campbell 	goto unaligned;
1220f931551bSRalph Campbell 
1221f931551bSRalph Campbell map_err:
1222f931551bSRalph Campbell 	kfree(phdr);
1223f931551bSRalph Campbell err_tx:
1224f931551bSRalph Campbell 	qib_put_txreq(tx);
1225f931551bSRalph Campbell 	ret = wait_kmem(dev, qp);
1226f931551bSRalph Campbell unaligned:
1227f931551bSRalph Campbell 	ibp->n_unaligned++;
1228f931551bSRalph Campbell bail:
1229f931551bSRalph Campbell 	return ret;
123048947109SMike Marciniszyn bail_tx:
123148947109SMike Marciniszyn 	ret = PTR_ERR(tx);
123248947109SMike Marciniszyn 	goto bail;
1233f931551bSRalph Campbell }
1234f931551bSRalph Campbell 
1235f931551bSRalph Campbell /*
1236f931551bSRalph Campbell  * If we are now in the error state, return zero to flush the
1237f931551bSRalph Campbell  * send work request.
1238f931551bSRalph Campbell  */
1239f931551bSRalph Campbell static int no_bufs_available(struct qib_qp *qp)
1240f931551bSRalph Campbell {
1241f931551bSRalph Campbell 	struct qib_ibdev *dev = to_idev(qp->ibqp.device);
1242f931551bSRalph Campbell 	struct qib_devdata *dd;
1243f931551bSRalph Campbell 	unsigned long flags;
1244f931551bSRalph Campbell 	int ret = 0;
1245f931551bSRalph Campbell 
1246f931551bSRalph Campbell 	/*
1247f931551bSRalph Campbell 	 * Note that as soon as want_buffer() is called and
1248f931551bSRalph Campbell 	 * possibly before it returns, qib_ib_piobufavail()
1249f931551bSRalph Campbell 	 * could be called. Therefore, put QP on the I/O wait list before
1250f931551bSRalph Campbell 	 * enabling the PIO avail interrupt.
1251f931551bSRalph Campbell 	 */
1252f931551bSRalph Campbell 	spin_lock_irqsave(&qp->s_lock, flags);
1253f931551bSRalph Campbell 	if (ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK) {
1254f931551bSRalph Campbell 		spin_lock(&dev->pending_lock);
1255f931551bSRalph Campbell 		if (list_empty(&qp->iowait)) {
1256f931551bSRalph Campbell 			dev->n_piowait++;
1257f931551bSRalph Campbell 			qp->s_flags |= QIB_S_WAIT_PIO;
1258f931551bSRalph Campbell 			list_add_tail(&qp->iowait, &dev->piowait);
1259f931551bSRalph Campbell 			dd = dd_from_dev(dev);
1260f931551bSRalph Campbell 			dd->f_wantpiobuf_intr(dd, 1);
1261f931551bSRalph Campbell 		}
1262f931551bSRalph Campbell 		spin_unlock(&dev->pending_lock);
1263f931551bSRalph Campbell 		qp->s_flags &= ~QIB_S_BUSY;
1264f931551bSRalph Campbell 		ret = -EBUSY;
1265f931551bSRalph Campbell 	}
1266f931551bSRalph Campbell 	spin_unlock_irqrestore(&qp->s_lock, flags);
1267f931551bSRalph Campbell 	return ret;
1268f931551bSRalph Campbell }
1269f931551bSRalph Campbell 
1270f931551bSRalph Campbell static int qib_verbs_send_pio(struct qib_qp *qp, struct qib_ib_header *ibhdr,
1271f931551bSRalph Campbell 			      u32 hdrwords, struct qib_sge_state *ss, u32 len,
1272f931551bSRalph Campbell 			      u32 plen, u32 dwords)
1273f931551bSRalph Campbell {
1274f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
1275f931551bSRalph Campbell 	struct qib_pportdata *ppd = dd->pport + qp->port_num - 1;
1276f931551bSRalph Campbell 	u32 *hdr = (u32 *) ibhdr;
1277f931551bSRalph Campbell 	u32 __iomem *piobuf_orig;
1278f931551bSRalph Campbell 	u32 __iomem *piobuf;
1279f931551bSRalph Campbell 	u64 pbc;
1280f931551bSRalph Campbell 	unsigned long flags;
1281f931551bSRalph Campbell 	unsigned flush_wc;
1282f931551bSRalph Campbell 	u32 control;
1283f931551bSRalph Campbell 	u32 pbufn;
1284f931551bSRalph Campbell 
1285f931551bSRalph Campbell 	control = dd->f_setpbc_control(ppd, plen, qp->s_srate,
1286f931551bSRalph Campbell 		be16_to_cpu(ibhdr->lrh[0]) >> 12);
1287f931551bSRalph Campbell 	pbc = ((u64) control << 32) | plen;
1288f931551bSRalph Campbell 	piobuf = dd->f_getsendbuf(ppd, pbc, &pbufn);
1289f931551bSRalph Campbell 	if (unlikely(piobuf == NULL))
1290f931551bSRalph Campbell 		return no_bufs_available(qp);
1291f931551bSRalph Campbell 
1292f931551bSRalph Campbell 	/*
1293f931551bSRalph Campbell 	 * Write the pbc.
1294f931551bSRalph Campbell 	 * We have to flush after the PBC for correctness on some cpus
1295f931551bSRalph Campbell 	 * or WC buffer can be written out of order.
1296f931551bSRalph Campbell 	 */
1297f931551bSRalph Campbell 	writeq(pbc, piobuf);
1298f931551bSRalph Campbell 	piobuf_orig = piobuf;
1299f931551bSRalph Campbell 	piobuf += 2;
1300f931551bSRalph Campbell 
1301f931551bSRalph Campbell 	flush_wc = dd->flags & QIB_PIO_FLUSH_WC;
1302f931551bSRalph Campbell 	if (len == 0) {
1303f931551bSRalph Campbell 		/*
1304f931551bSRalph Campbell 		 * If there is just the header portion, must flush before
1305f931551bSRalph Campbell 		 * writing last word of header for correctness, and after
1306f931551bSRalph Campbell 		 * the last header word (trigger word).
1307f931551bSRalph Campbell 		 */
1308f931551bSRalph Campbell 		if (flush_wc) {
1309f931551bSRalph Campbell 			qib_flush_wc();
1310f931551bSRalph Campbell 			qib_pio_copy(piobuf, hdr, hdrwords - 1);
1311f931551bSRalph Campbell 			qib_flush_wc();
1312f931551bSRalph Campbell 			__raw_writel(hdr[hdrwords - 1], piobuf + hdrwords - 1);
1313f931551bSRalph Campbell 			qib_flush_wc();
1314f931551bSRalph Campbell 		} else
1315f931551bSRalph Campbell 			qib_pio_copy(piobuf, hdr, hdrwords);
1316f931551bSRalph Campbell 		goto done;
1317f931551bSRalph Campbell 	}
1318f931551bSRalph Campbell 
1319f931551bSRalph Campbell 	if (flush_wc)
1320f931551bSRalph Campbell 		qib_flush_wc();
1321f931551bSRalph Campbell 	qib_pio_copy(piobuf, hdr, hdrwords);
1322f931551bSRalph Campbell 	piobuf += hdrwords;
1323f931551bSRalph Campbell 
1324f931551bSRalph Campbell 	/* The common case is aligned and contained in one segment. */
1325f931551bSRalph Campbell 	if (likely(ss->num_sge == 1 && len <= ss->sge.length &&
1326f931551bSRalph Campbell 		   !((unsigned long)ss->sge.vaddr & (sizeof(u32) - 1)))) {
1327f931551bSRalph Campbell 		u32 *addr = (u32 *) ss->sge.vaddr;
1328f931551bSRalph Campbell 
1329f931551bSRalph Campbell 		/* Update address before sending packet. */
1330f931551bSRalph Campbell 		update_sge(ss, len);
1331f931551bSRalph Campbell 		if (flush_wc) {
1332f931551bSRalph Campbell 			qib_pio_copy(piobuf, addr, dwords - 1);
1333f931551bSRalph Campbell 			/* must flush early everything before trigger word */
1334f931551bSRalph Campbell 			qib_flush_wc();
1335f931551bSRalph Campbell 			__raw_writel(addr[dwords - 1], piobuf + dwords - 1);
1336f931551bSRalph Campbell 			/* be sure trigger word is written */
1337f931551bSRalph Campbell 			qib_flush_wc();
1338f931551bSRalph Campbell 		} else
1339f931551bSRalph Campbell 			qib_pio_copy(piobuf, addr, dwords);
1340f931551bSRalph Campbell 		goto done;
1341f931551bSRalph Campbell 	}
1342f931551bSRalph Campbell 	copy_io(piobuf, ss, len, flush_wc);
1343f931551bSRalph Campbell done:
1344f931551bSRalph Campbell 	if (dd->flags & QIB_USE_SPCL_TRIG) {
1345f931551bSRalph Campbell 		u32 spcl_off = (pbufn >= dd->piobcnt2k) ? 2047 : 1023;
1346da12c1f6SMike Marciniszyn 
1347f931551bSRalph Campbell 		qib_flush_wc();
1348f931551bSRalph Campbell 		__raw_writel(0xaebecede, piobuf_orig + spcl_off);
1349f931551bSRalph Campbell 	}
1350f931551bSRalph Campbell 	qib_sendbuf_done(dd, pbufn);
1351f931551bSRalph Campbell 	if (qp->s_rdma_mr) {
13526a82649fSMike Marciniszyn 		qib_put_mr(qp->s_rdma_mr);
1353f931551bSRalph Campbell 		qp->s_rdma_mr = NULL;
1354f931551bSRalph Campbell 	}
1355f931551bSRalph Campbell 	if (qp->s_wqe) {
1356f931551bSRalph Campbell 		spin_lock_irqsave(&qp->s_lock, flags);
1357f931551bSRalph Campbell 		qib_send_complete(qp, qp->s_wqe, IB_WC_SUCCESS);
1358f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
1359f931551bSRalph Campbell 	} else if (qp->ibqp.qp_type == IB_QPT_RC) {
1360f931551bSRalph Campbell 		spin_lock_irqsave(&qp->s_lock, flags);
1361f931551bSRalph Campbell 		qib_rc_send_complete(qp, ibhdr);
1362f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
1363f931551bSRalph Campbell 	}
1364f931551bSRalph Campbell 	return 0;
1365f931551bSRalph Campbell }
1366f931551bSRalph Campbell 
1367f931551bSRalph Campbell /**
1368f931551bSRalph Campbell  * qib_verbs_send - send a packet
1369f931551bSRalph Campbell  * @qp: the QP to send on
1370f931551bSRalph Campbell  * @hdr: the packet header
1371f931551bSRalph Campbell  * @hdrwords: the number of 32-bit words in the header
1372f931551bSRalph Campbell  * @ss: the SGE to send
1373f931551bSRalph Campbell  * @len: the length of the packet in bytes
1374f931551bSRalph Campbell  *
1375f931551bSRalph Campbell  * Return zero if packet is sent or queued OK.
1376f931551bSRalph Campbell  * Return non-zero and clear qp->s_flags QIB_S_BUSY otherwise.
1377f931551bSRalph Campbell  */
1378f931551bSRalph Campbell int qib_verbs_send(struct qib_qp *qp, struct qib_ib_header *hdr,
1379f931551bSRalph Campbell 		   u32 hdrwords, struct qib_sge_state *ss, u32 len)
1380f931551bSRalph Campbell {
1381f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
1382f931551bSRalph Campbell 	u32 plen;
1383f931551bSRalph Campbell 	int ret;
1384f931551bSRalph Campbell 	u32 dwords = (len + 3) >> 2;
1385f931551bSRalph Campbell 
1386f931551bSRalph Campbell 	/*
1387f931551bSRalph Campbell 	 * Calculate the send buffer trigger address.
1388f931551bSRalph Campbell 	 * The +1 counts for the pbc control dword following the pbc length.
1389f931551bSRalph Campbell 	 */
1390f931551bSRalph Campbell 	plen = hdrwords + dwords + 1;
1391f931551bSRalph Campbell 
1392f931551bSRalph Campbell 	/*
1393f931551bSRalph Campbell 	 * VL15 packets (IB_QPT_SMI) will always use PIO, so we
1394f931551bSRalph Campbell 	 * can defer SDMA restart until link goes ACTIVE without
1395f931551bSRalph Campbell 	 * worrying about just how we got there.
1396f931551bSRalph Campbell 	 */
1397f931551bSRalph Campbell 	if (qp->ibqp.qp_type == IB_QPT_SMI ||
1398f931551bSRalph Campbell 	    !(dd->flags & QIB_HAS_SEND_DMA))
1399f931551bSRalph Campbell 		ret = qib_verbs_send_pio(qp, hdr, hdrwords, ss, len,
1400f931551bSRalph Campbell 					 plen, dwords);
1401f931551bSRalph Campbell 	else
1402f931551bSRalph Campbell 		ret = qib_verbs_send_dma(qp, hdr, hdrwords, ss, len,
1403f931551bSRalph Campbell 					 plen, dwords);
1404f931551bSRalph Campbell 
1405f931551bSRalph Campbell 	return ret;
1406f931551bSRalph Campbell }
1407f931551bSRalph Campbell 
1408f931551bSRalph Campbell int qib_snapshot_counters(struct qib_pportdata *ppd, u64 *swords,
1409f931551bSRalph Campbell 			  u64 *rwords, u64 *spkts, u64 *rpkts,
1410f931551bSRalph Campbell 			  u64 *xmit_wait)
1411f931551bSRalph Campbell {
1412f931551bSRalph Campbell 	int ret;
1413f931551bSRalph Campbell 	struct qib_devdata *dd = ppd->dd;
1414f931551bSRalph Campbell 
1415f931551bSRalph Campbell 	if (!(dd->flags & QIB_PRESENT)) {
1416f931551bSRalph Campbell 		/* no hardware, freeze, etc. */
1417f931551bSRalph Campbell 		ret = -EINVAL;
1418f931551bSRalph Campbell 		goto bail;
1419f931551bSRalph Campbell 	}
1420f931551bSRalph Campbell 	*swords = dd->f_portcntr(ppd, QIBPORTCNTR_WORDSEND);
1421f931551bSRalph Campbell 	*rwords = dd->f_portcntr(ppd, QIBPORTCNTR_WORDRCV);
1422f931551bSRalph Campbell 	*spkts = dd->f_portcntr(ppd, QIBPORTCNTR_PKTSEND);
1423f931551bSRalph Campbell 	*rpkts = dd->f_portcntr(ppd, QIBPORTCNTR_PKTRCV);
1424f931551bSRalph Campbell 	*xmit_wait = dd->f_portcntr(ppd, QIBPORTCNTR_SENDSTALL);
1425f931551bSRalph Campbell 
1426f931551bSRalph Campbell 	ret = 0;
1427f931551bSRalph Campbell 
1428f931551bSRalph Campbell bail:
1429f931551bSRalph Campbell 	return ret;
1430f931551bSRalph Campbell }
1431f931551bSRalph Campbell 
1432f931551bSRalph Campbell /**
1433f931551bSRalph Campbell  * qib_get_counters - get various chip counters
1434f931551bSRalph Campbell  * @dd: the qlogic_ib device
1435f931551bSRalph Campbell  * @cntrs: counters are placed here
1436f931551bSRalph Campbell  *
1437f931551bSRalph Campbell  * Return the counters needed by recv_pma_get_portcounters().
1438f931551bSRalph Campbell  */
1439f931551bSRalph Campbell int qib_get_counters(struct qib_pportdata *ppd,
1440f931551bSRalph Campbell 		     struct qib_verbs_counters *cntrs)
1441f931551bSRalph Campbell {
1442f931551bSRalph Campbell 	int ret;
1443f931551bSRalph Campbell 
1444f931551bSRalph Campbell 	if (!(ppd->dd->flags & QIB_PRESENT)) {
1445f931551bSRalph Campbell 		/* no hardware, freeze, etc. */
1446f931551bSRalph Campbell 		ret = -EINVAL;
1447f931551bSRalph Campbell 		goto bail;
1448f931551bSRalph Campbell 	}
1449f931551bSRalph Campbell 	cntrs->symbol_error_counter =
1450f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBSYMBOLERR);
1451f931551bSRalph Campbell 	cntrs->link_error_recovery_counter =
1452f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBLINKERRRECOV);
1453f931551bSRalph Campbell 	/*
1454f931551bSRalph Campbell 	 * The link downed counter counts when the other side downs the
1455f931551bSRalph Campbell 	 * connection.  We add in the number of times we downed the link
1456f931551bSRalph Campbell 	 * due to local link integrity errors to compensate.
1457f931551bSRalph Campbell 	 */
1458f931551bSRalph Campbell 	cntrs->link_downed_counter =
1459f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBLINKDOWN);
1460f931551bSRalph Campbell 	cntrs->port_rcv_errors =
1461f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXDROPPKT) +
1462f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RCVOVFL) +
1463f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERR_RLEN) +
1464f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_INVALIDRLEN) +
1465f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRLINK) +
1466f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRICRC) +
1467f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRVCRC) +
1468f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRLPCRC) +
1469f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_BADFORMAT);
1470f931551bSRalph Campbell 	cntrs->port_rcv_errors +=
1471f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXLOCALPHYERR);
1472f931551bSRalph Campbell 	cntrs->port_rcv_errors +=
1473f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXVLERR);
1474f931551bSRalph Campbell 	cntrs->port_rcv_remphys_errors =
1475f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RCVEBP);
1476f931551bSRalph Campbell 	cntrs->port_xmit_discards =
1477f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_UNSUPVL);
1478f931551bSRalph Campbell 	cntrs->port_xmit_data = ppd->dd->f_portcntr(ppd,
1479f931551bSRalph Campbell 			QIBPORTCNTR_WORDSEND);
1480f931551bSRalph Campbell 	cntrs->port_rcv_data = ppd->dd->f_portcntr(ppd,
1481f931551bSRalph Campbell 			QIBPORTCNTR_WORDRCV);
1482f931551bSRalph Campbell 	cntrs->port_xmit_packets = ppd->dd->f_portcntr(ppd,
1483f931551bSRalph Campbell 			QIBPORTCNTR_PKTSEND);
1484f931551bSRalph Campbell 	cntrs->port_rcv_packets = ppd->dd->f_portcntr(ppd,
1485f931551bSRalph Campbell 			QIBPORTCNTR_PKTRCV);
1486f931551bSRalph Campbell 	cntrs->local_link_integrity_errors =
1487f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_LLI);
1488f931551bSRalph Campbell 	cntrs->excessive_buffer_overrun_errors =
1489f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_EXCESSBUFOVFL);
1490f931551bSRalph Campbell 	cntrs->vl15_dropped =
1491f931551bSRalph Campbell 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_VL15PKTDROP);
1492f931551bSRalph Campbell 
1493f931551bSRalph Campbell 	ret = 0;
1494f931551bSRalph Campbell 
1495f931551bSRalph Campbell bail:
1496f931551bSRalph Campbell 	return ret;
1497f931551bSRalph Campbell }
1498f931551bSRalph Campbell 
1499f931551bSRalph Campbell /**
1500f931551bSRalph Campbell  * qib_ib_piobufavail - callback when a PIO buffer is available
1501f931551bSRalph Campbell  * @dd: the device pointer
1502f931551bSRalph Campbell  *
1503f931551bSRalph Campbell  * This is called from qib_intr() at interrupt level when a PIO buffer is
1504f931551bSRalph Campbell  * available after qib_verbs_send() returned an error that no buffers were
1505f931551bSRalph Campbell  * available. Disable the interrupt if there are no more QPs waiting.
1506f931551bSRalph Campbell  */
1507f931551bSRalph Campbell void qib_ib_piobufavail(struct qib_devdata *dd)
1508f931551bSRalph Campbell {
1509f931551bSRalph Campbell 	struct qib_ibdev *dev = &dd->verbs_dev;
1510f931551bSRalph Campbell 	struct list_head *list;
1511f931551bSRalph Campbell 	struct qib_qp *qps[5];
1512f931551bSRalph Campbell 	struct qib_qp *qp;
1513f931551bSRalph Campbell 	unsigned long flags;
1514f931551bSRalph Campbell 	unsigned i, n;
1515f931551bSRalph Campbell 
1516f931551bSRalph Campbell 	list = &dev->piowait;
1517f931551bSRalph Campbell 	n = 0;
1518f931551bSRalph Campbell 
1519f931551bSRalph Campbell 	/*
1520f931551bSRalph Campbell 	 * Note: checking that the piowait list is empty and clearing
1521f931551bSRalph Campbell 	 * the buffer available interrupt needs to be atomic or we
1522f931551bSRalph Campbell 	 * could end up with QPs on the wait list with the interrupt
1523f931551bSRalph Campbell 	 * disabled.
1524f931551bSRalph Campbell 	 */
1525f931551bSRalph Campbell 	spin_lock_irqsave(&dev->pending_lock, flags);
1526f931551bSRalph Campbell 	while (!list_empty(list)) {
1527f931551bSRalph Campbell 		if (n == ARRAY_SIZE(qps))
1528f931551bSRalph Campbell 			goto full;
1529f931551bSRalph Campbell 		qp = list_entry(list->next, struct qib_qp, iowait);
1530f931551bSRalph Campbell 		list_del_init(&qp->iowait);
1531f931551bSRalph Campbell 		atomic_inc(&qp->refcount);
1532f931551bSRalph Campbell 		qps[n++] = qp;
1533f931551bSRalph Campbell 	}
1534f931551bSRalph Campbell 	dd->f_wantpiobuf_intr(dd, 0);
1535f931551bSRalph Campbell full:
1536f931551bSRalph Campbell 	spin_unlock_irqrestore(&dev->pending_lock, flags);
1537f931551bSRalph Campbell 
1538f931551bSRalph Campbell 	for (i = 0; i < n; i++) {
1539f931551bSRalph Campbell 		qp = qps[i];
1540f931551bSRalph Campbell 
1541f931551bSRalph Campbell 		spin_lock_irqsave(&qp->s_lock, flags);
1542f931551bSRalph Campbell 		if (qp->s_flags & QIB_S_WAIT_PIO) {
1543f931551bSRalph Campbell 			qp->s_flags &= ~QIB_S_WAIT_PIO;
1544f931551bSRalph Campbell 			qib_schedule_send(qp);
1545f931551bSRalph Campbell 		}
1546f931551bSRalph Campbell 		spin_unlock_irqrestore(&qp->s_lock, flags);
1547f931551bSRalph Campbell 
1548f931551bSRalph Campbell 		/* Notify qib_destroy_qp() if it is waiting. */
1549f931551bSRalph Campbell 		if (atomic_dec_and_test(&qp->refcount))
1550f931551bSRalph Campbell 			wake_up(&qp->wait);
1551f931551bSRalph Campbell 	}
1552f931551bSRalph Campbell }
1553f931551bSRalph Campbell 
15542528e33eSMatan Barak static int qib_query_device(struct ib_device *ibdev, struct ib_device_attr *props,
15552528e33eSMatan Barak 			    struct ib_udata *uhw)
1556f931551bSRalph Campbell {
1557f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_ibdev(ibdev);
1558f931551bSRalph Campbell 	struct qib_ibdev *dev = to_idev(ibdev);
1559f931551bSRalph Campbell 
15602528e33eSMatan Barak 	if (uhw->inlen || uhw->outlen)
15612528e33eSMatan Barak 		return -EINVAL;
1562f931551bSRalph Campbell 	memset(props, 0, sizeof(*props));
1563f931551bSRalph Campbell 
1564f931551bSRalph Campbell 	props->device_cap_flags = IB_DEVICE_BAD_PKEY_CNTR |
1565f931551bSRalph Campbell 		IB_DEVICE_BAD_QKEY_CNTR | IB_DEVICE_SHUTDOWN_PORT |
1566f931551bSRalph Campbell 		IB_DEVICE_SYS_IMAGE_GUID | IB_DEVICE_RC_RNR_NAK_GEN |
1567f931551bSRalph Campbell 		IB_DEVICE_PORT_ACTIVE_EVENT | IB_DEVICE_SRQ_RESIZE;
1568f931551bSRalph Campbell 	props->page_size_cap = PAGE_SIZE;
1569f931551bSRalph Campbell 	props->vendor_id =
1570f931551bSRalph Campbell 		QIB_SRC_OUI_1 << 16 | QIB_SRC_OUI_2 << 8 | QIB_SRC_OUI_3;
1571f931551bSRalph Campbell 	props->vendor_part_id = dd->deviceid;
1572f931551bSRalph Campbell 	props->hw_ver = dd->minrev;
1573f931551bSRalph Campbell 	props->sys_image_guid = ib_qib_sys_image_guid;
1574f931551bSRalph Campbell 	props->max_mr_size = ~0ULL;
1575f931551bSRalph Campbell 	props->max_qp = ib_qib_max_qps;
1576f931551bSRalph Campbell 	props->max_qp_wr = ib_qib_max_qp_wrs;
1577f931551bSRalph Campbell 	props->max_sge = ib_qib_max_sges;
1578*aaae91f4SSteve Wise 	props->max_sge_rd = ib_qib_max_sges;
1579f931551bSRalph Campbell 	props->max_cq = ib_qib_max_cqs;
1580f931551bSRalph Campbell 	props->max_ah = ib_qib_max_ahs;
1581f931551bSRalph Campbell 	props->max_cqe = ib_qib_max_cqes;
1582f931551bSRalph Campbell 	props->max_mr = dev->lk_table.max;
1583f931551bSRalph Campbell 	props->max_fmr = dev->lk_table.max;
1584f931551bSRalph Campbell 	props->max_map_per_fmr = 32767;
1585f931551bSRalph Campbell 	props->max_pd = ib_qib_max_pds;
1586f931551bSRalph Campbell 	props->max_qp_rd_atom = QIB_MAX_RDMA_ATOMIC;
1587f931551bSRalph Campbell 	props->max_qp_init_rd_atom = 255;
1588f931551bSRalph Campbell 	/* props->max_res_rd_atom */
1589f931551bSRalph Campbell 	props->max_srq = ib_qib_max_srqs;
1590f931551bSRalph Campbell 	props->max_srq_wr = ib_qib_max_srq_wrs;
1591f931551bSRalph Campbell 	props->max_srq_sge = ib_qib_max_srq_sges;
1592f931551bSRalph Campbell 	/* props->local_ca_ack_delay */
1593f931551bSRalph Campbell 	props->atomic_cap = IB_ATOMIC_GLOB;
1594f931551bSRalph Campbell 	props->max_pkeys = qib_get_npkeys(dd);
1595f931551bSRalph Campbell 	props->max_mcast_grp = ib_qib_max_mcast_grps;
1596f931551bSRalph Campbell 	props->max_mcast_qp_attach = ib_qib_max_mcast_qp_attached;
1597f931551bSRalph Campbell 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
1598f931551bSRalph Campbell 		props->max_mcast_grp;
1599f931551bSRalph Campbell 
1600f931551bSRalph Campbell 	return 0;
1601f931551bSRalph Campbell }
1602f931551bSRalph Campbell 
1603f931551bSRalph Campbell static int qib_query_port(struct ib_device *ibdev, u8 port,
1604f931551bSRalph Campbell 			  struct ib_port_attr *props)
1605f931551bSRalph Campbell {
1606f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_ibdev(ibdev);
1607f931551bSRalph Campbell 	struct qib_ibport *ibp = to_iport(ibdev, port);
1608f931551bSRalph Campbell 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1609f931551bSRalph Campbell 	enum ib_mtu mtu;
1610f931551bSRalph Campbell 	u16 lid = ppd->lid;
1611f931551bSRalph Campbell 
1612f931551bSRalph Campbell 	memset(props, 0, sizeof(*props));
1613f931551bSRalph Campbell 	props->lid = lid ? lid : be16_to_cpu(IB_LID_PERMISSIVE);
1614f931551bSRalph Campbell 	props->lmc = ppd->lmc;
1615f931551bSRalph Campbell 	props->sm_lid = ibp->sm_lid;
1616f931551bSRalph Campbell 	props->sm_sl = ibp->sm_sl;
1617f931551bSRalph Campbell 	props->state = dd->f_iblink_state(ppd->lastibcstat);
1618f931551bSRalph Campbell 	props->phys_state = dd->f_ibphys_portstate(ppd->lastibcstat);
1619f931551bSRalph Campbell 	props->port_cap_flags = ibp->port_cap_flags;
1620f931551bSRalph Campbell 	props->gid_tbl_len = QIB_GUIDS_PER_PORT;
1621f931551bSRalph Campbell 	props->max_msg_sz = 0x80000000;
1622f931551bSRalph Campbell 	props->pkey_tbl_len = qib_get_npkeys(dd);
1623f931551bSRalph Campbell 	props->bad_pkey_cntr = ibp->pkey_violations;
1624f931551bSRalph Campbell 	props->qkey_viol_cntr = ibp->qkey_violations;
1625f931551bSRalph Campbell 	props->active_width = ppd->link_width_active;
1626f931551bSRalph Campbell 	/* See rate_show() */
1627f931551bSRalph Campbell 	props->active_speed = ppd->link_speed_active;
1628f931551bSRalph Campbell 	props->max_vl_num = qib_num_vls(ppd->vls_supported);
1629f931551bSRalph Campbell 	props->init_type_reply = 0;
1630f931551bSRalph Campbell 
1631f931551bSRalph Campbell 	props->max_mtu = qib_ibmtu ? qib_ibmtu : IB_MTU_4096;
1632f931551bSRalph Campbell 	switch (ppd->ibmtu) {
1633f931551bSRalph Campbell 	case 4096:
1634f931551bSRalph Campbell 		mtu = IB_MTU_4096;
1635f931551bSRalph Campbell 		break;
1636f931551bSRalph Campbell 	case 2048:
1637f931551bSRalph Campbell 		mtu = IB_MTU_2048;
1638f931551bSRalph Campbell 		break;
1639f931551bSRalph Campbell 	case 1024:
1640f931551bSRalph Campbell 		mtu = IB_MTU_1024;
1641f931551bSRalph Campbell 		break;
1642f931551bSRalph Campbell 	case 512:
1643f931551bSRalph Campbell 		mtu = IB_MTU_512;
1644f931551bSRalph Campbell 		break;
1645f931551bSRalph Campbell 	case 256:
1646f931551bSRalph Campbell 		mtu = IB_MTU_256;
1647f931551bSRalph Campbell 		break;
1648f931551bSRalph Campbell 	default:
1649f931551bSRalph Campbell 		mtu = IB_MTU_2048;
1650f931551bSRalph Campbell 	}
1651f931551bSRalph Campbell 	props->active_mtu = mtu;
1652f931551bSRalph Campbell 	props->subnet_timeout = ibp->subnet_timeout;
1653f931551bSRalph Campbell 
1654f931551bSRalph Campbell 	return 0;
1655f931551bSRalph Campbell }
1656f931551bSRalph Campbell 
1657f931551bSRalph Campbell static int qib_modify_device(struct ib_device *device,
1658f931551bSRalph Campbell 			     int device_modify_mask,
1659f931551bSRalph Campbell 			     struct ib_device_modify *device_modify)
1660f931551bSRalph Campbell {
1661f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_ibdev(device);
1662f931551bSRalph Campbell 	unsigned i;
1663f931551bSRalph Campbell 	int ret;
1664f931551bSRalph Campbell 
1665f931551bSRalph Campbell 	if (device_modify_mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
1666f931551bSRalph Campbell 				   IB_DEVICE_MODIFY_NODE_DESC)) {
1667f931551bSRalph Campbell 		ret = -EOPNOTSUPP;
1668f931551bSRalph Campbell 		goto bail;
1669f931551bSRalph Campbell 	}
1670f931551bSRalph Campbell 
1671f931551bSRalph Campbell 	if (device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC) {
1672f931551bSRalph Campbell 		memcpy(device->node_desc, device_modify->node_desc, 64);
1673f931551bSRalph Campbell 		for (i = 0; i < dd->num_pports; i++) {
1674f931551bSRalph Campbell 			struct qib_ibport *ibp = &dd->pport[i].ibport_data;
1675f931551bSRalph Campbell 
1676f931551bSRalph Campbell 			qib_node_desc_chg(ibp);
1677f931551bSRalph Campbell 		}
1678f931551bSRalph Campbell 	}
1679f931551bSRalph Campbell 
1680f931551bSRalph Campbell 	if (device_modify_mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID) {
1681f931551bSRalph Campbell 		ib_qib_sys_image_guid =
1682f931551bSRalph Campbell 			cpu_to_be64(device_modify->sys_image_guid);
1683f931551bSRalph Campbell 		for (i = 0; i < dd->num_pports; i++) {
1684f931551bSRalph Campbell 			struct qib_ibport *ibp = &dd->pport[i].ibport_data;
1685f931551bSRalph Campbell 
1686f931551bSRalph Campbell 			qib_sys_guid_chg(ibp);
1687f931551bSRalph Campbell 		}
1688f931551bSRalph Campbell 	}
1689f931551bSRalph Campbell 
1690f931551bSRalph Campbell 	ret = 0;
1691f931551bSRalph Campbell 
1692f931551bSRalph Campbell bail:
1693f931551bSRalph Campbell 	return ret;
1694f931551bSRalph Campbell }
1695f931551bSRalph Campbell 
1696f931551bSRalph Campbell static int qib_modify_port(struct ib_device *ibdev, u8 port,
1697f931551bSRalph Campbell 			   int port_modify_mask, struct ib_port_modify *props)
1698f931551bSRalph Campbell {
1699f931551bSRalph Campbell 	struct qib_ibport *ibp = to_iport(ibdev, port);
1700f931551bSRalph Campbell 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1701f931551bSRalph Campbell 
1702f931551bSRalph Campbell 	ibp->port_cap_flags |= props->set_port_cap_mask;
1703f931551bSRalph Campbell 	ibp->port_cap_flags &= ~props->clr_port_cap_mask;
1704f931551bSRalph Campbell 	if (props->set_port_cap_mask || props->clr_port_cap_mask)
1705f931551bSRalph Campbell 		qib_cap_mask_chg(ibp);
1706f931551bSRalph Campbell 	if (port_modify_mask & IB_PORT_SHUTDOWN)
1707f931551bSRalph Campbell 		qib_set_linkstate(ppd, QIB_IB_LINKDOWN);
1708f931551bSRalph Campbell 	if (port_modify_mask & IB_PORT_RESET_QKEY_CNTR)
1709f931551bSRalph Campbell 		ibp->qkey_violations = 0;
1710f931551bSRalph Campbell 	return 0;
1711f931551bSRalph Campbell }
1712f931551bSRalph Campbell 
1713f931551bSRalph Campbell static int qib_query_gid(struct ib_device *ibdev, u8 port,
1714f931551bSRalph Campbell 			 int index, union ib_gid *gid)
1715f931551bSRalph Campbell {
1716f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_ibdev(ibdev);
1717f931551bSRalph Campbell 	int ret = 0;
1718f931551bSRalph Campbell 
1719f931551bSRalph Campbell 	if (!port || port > dd->num_pports)
1720f931551bSRalph Campbell 		ret = -EINVAL;
1721f931551bSRalph Campbell 	else {
1722f931551bSRalph Campbell 		struct qib_ibport *ibp = to_iport(ibdev, port);
1723f931551bSRalph Campbell 		struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1724f931551bSRalph Campbell 
1725f931551bSRalph Campbell 		gid->global.subnet_prefix = ibp->gid_prefix;
1726f931551bSRalph Campbell 		if (index == 0)
1727f931551bSRalph Campbell 			gid->global.interface_id = ppd->guid;
1728f931551bSRalph Campbell 		else if (index < QIB_GUIDS_PER_PORT)
1729f931551bSRalph Campbell 			gid->global.interface_id = ibp->guids[index - 1];
1730f931551bSRalph Campbell 		else
1731f931551bSRalph Campbell 			ret = -EINVAL;
1732f931551bSRalph Campbell 	}
1733f931551bSRalph Campbell 
1734f931551bSRalph Campbell 	return ret;
1735f931551bSRalph Campbell }
1736f931551bSRalph Campbell 
1737f931551bSRalph Campbell static struct ib_pd *qib_alloc_pd(struct ib_device *ibdev,
1738f931551bSRalph Campbell 				  struct ib_ucontext *context,
1739f931551bSRalph Campbell 				  struct ib_udata *udata)
1740f931551bSRalph Campbell {
1741f931551bSRalph Campbell 	struct qib_ibdev *dev = to_idev(ibdev);
1742f931551bSRalph Campbell 	struct qib_pd *pd;
1743f931551bSRalph Campbell 	struct ib_pd *ret;
1744f931551bSRalph Campbell 
1745f931551bSRalph Campbell 	/*
1746f931551bSRalph Campbell 	 * This is actually totally arbitrary.  Some correctness tests
1747f931551bSRalph Campbell 	 * assume there's a maximum number of PDs that can be allocated.
1748f931551bSRalph Campbell 	 * We don't actually have this limit, but we fail the test if
1749f931551bSRalph Campbell 	 * we allow allocations of more than we report for this value.
1750f931551bSRalph Campbell 	 */
1751f931551bSRalph Campbell 
1752041af0bbSMike Marciniszyn 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1753f931551bSRalph Campbell 	if (!pd) {
1754f931551bSRalph Campbell 		ret = ERR_PTR(-ENOMEM);
1755f931551bSRalph Campbell 		goto bail;
1756f931551bSRalph Campbell 	}
1757f931551bSRalph Campbell 
1758f931551bSRalph Campbell 	spin_lock(&dev->n_pds_lock);
1759f931551bSRalph Campbell 	if (dev->n_pds_allocated == ib_qib_max_pds) {
1760f931551bSRalph Campbell 		spin_unlock(&dev->n_pds_lock);
1761f931551bSRalph Campbell 		kfree(pd);
1762f931551bSRalph Campbell 		ret = ERR_PTR(-ENOMEM);
1763f931551bSRalph Campbell 		goto bail;
1764f931551bSRalph Campbell 	}
1765f931551bSRalph Campbell 
1766f931551bSRalph Campbell 	dev->n_pds_allocated++;
1767f931551bSRalph Campbell 	spin_unlock(&dev->n_pds_lock);
1768f931551bSRalph Campbell 
1769f931551bSRalph Campbell 	/* ib_alloc_pd() will initialize pd->ibpd. */
1770f931551bSRalph Campbell 	pd->user = udata != NULL;
1771f931551bSRalph Campbell 
1772f931551bSRalph Campbell 	ret = &pd->ibpd;
1773f931551bSRalph Campbell 
1774f931551bSRalph Campbell bail:
1775f931551bSRalph Campbell 	return ret;
1776f931551bSRalph Campbell }
1777f931551bSRalph Campbell 
1778f931551bSRalph Campbell static int qib_dealloc_pd(struct ib_pd *ibpd)
1779f931551bSRalph Campbell {
1780f931551bSRalph Campbell 	struct qib_pd *pd = to_ipd(ibpd);
1781f931551bSRalph Campbell 	struct qib_ibdev *dev = to_idev(ibpd->device);
1782f931551bSRalph Campbell 
1783f931551bSRalph Campbell 	spin_lock(&dev->n_pds_lock);
1784f931551bSRalph Campbell 	dev->n_pds_allocated--;
1785f931551bSRalph Campbell 	spin_unlock(&dev->n_pds_lock);
1786f931551bSRalph Campbell 
1787f931551bSRalph Campbell 	kfree(pd);
1788f931551bSRalph Campbell 
1789f931551bSRalph Campbell 	return 0;
1790f931551bSRalph Campbell }
1791f931551bSRalph Campbell 
1792f931551bSRalph Campbell int qib_check_ah(struct ib_device *ibdev, struct ib_ah_attr *ah_attr)
1793f931551bSRalph Campbell {
1794f931551bSRalph Campbell 	/* A multicast address requires a GRH (see ch. 8.4.1). */
1795f931551bSRalph Campbell 	if (ah_attr->dlid >= QIB_MULTICAST_LID_BASE &&
1796f931551bSRalph Campbell 	    ah_attr->dlid != QIB_PERMISSIVE_LID &&
1797f931551bSRalph Campbell 	    !(ah_attr->ah_flags & IB_AH_GRH))
1798f931551bSRalph Campbell 		goto bail;
1799f931551bSRalph Campbell 	if ((ah_attr->ah_flags & IB_AH_GRH) &&
1800f931551bSRalph Campbell 	    ah_attr->grh.sgid_index >= QIB_GUIDS_PER_PORT)
1801f931551bSRalph Campbell 		goto bail;
1802f931551bSRalph Campbell 	if (ah_attr->dlid == 0)
1803f931551bSRalph Campbell 		goto bail;
1804f931551bSRalph Campbell 	if (ah_attr->port_num < 1 ||
1805f931551bSRalph Campbell 	    ah_attr->port_num > ibdev->phys_port_cnt)
1806f931551bSRalph Campbell 		goto bail;
1807f931551bSRalph Campbell 	if (ah_attr->static_rate != IB_RATE_PORT_CURRENT &&
1808f931551bSRalph Campbell 	    ib_rate_to_mult(ah_attr->static_rate) < 0)
1809f931551bSRalph Campbell 		goto bail;
1810f931551bSRalph Campbell 	if (ah_attr->sl > 15)
1811f931551bSRalph Campbell 		goto bail;
1812f931551bSRalph Campbell 	return 0;
1813f931551bSRalph Campbell bail:
1814f931551bSRalph Campbell 	return -EINVAL;
1815f931551bSRalph Campbell }
1816f931551bSRalph Campbell 
1817f931551bSRalph Campbell /**
1818f931551bSRalph Campbell  * qib_create_ah - create an address handle
1819f931551bSRalph Campbell  * @pd: the protection domain
1820f931551bSRalph Campbell  * @ah_attr: the attributes of the AH
1821f931551bSRalph Campbell  *
1822f931551bSRalph Campbell  * This may be called from interrupt context.
1823f931551bSRalph Campbell  */
1824f931551bSRalph Campbell static struct ib_ah *qib_create_ah(struct ib_pd *pd,
1825f931551bSRalph Campbell 				   struct ib_ah_attr *ah_attr)
1826f931551bSRalph Campbell {
1827f931551bSRalph Campbell 	struct qib_ah *ah;
1828f931551bSRalph Campbell 	struct ib_ah *ret;
1829f931551bSRalph Campbell 	struct qib_ibdev *dev = to_idev(pd->device);
1830f931551bSRalph Campbell 	unsigned long flags;
1831f931551bSRalph Campbell 
1832f931551bSRalph Campbell 	if (qib_check_ah(pd->device, ah_attr)) {
1833f931551bSRalph Campbell 		ret = ERR_PTR(-EINVAL);
1834f931551bSRalph Campbell 		goto bail;
1835f931551bSRalph Campbell 	}
1836f931551bSRalph Campbell 
1837041af0bbSMike Marciniszyn 	ah = kmalloc(sizeof(*ah), GFP_ATOMIC);
1838f931551bSRalph Campbell 	if (!ah) {
1839f931551bSRalph Campbell 		ret = ERR_PTR(-ENOMEM);
1840f931551bSRalph Campbell 		goto bail;
1841f931551bSRalph Campbell 	}
1842f931551bSRalph Campbell 
1843f931551bSRalph Campbell 	spin_lock_irqsave(&dev->n_ahs_lock, flags);
1844f931551bSRalph Campbell 	if (dev->n_ahs_allocated == ib_qib_max_ahs) {
1845f931551bSRalph Campbell 		spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
1846f931551bSRalph Campbell 		kfree(ah);
1847f931551bSRalph Campbell 		ret = ERR_PTR(-ENOMEM);
1848f931551bSRalph Campbell 		goto bail;
1849f931551bSRalph Campbell 	}
1850f931551bSRalph Campbell 
1851f931551bSRalph Campbell 	dev->n_ahs_allocated++;
1852f931551bSRalph Campbell 	spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
1853f931551bSRalph Campbell 
1854f931551bSRalph Campbell 	/* ib_create_ah() will initialize ah->ibah. */
1855f931551bSRalph Campbell 	ah->attr = *ah_attr;
1856f931551bSRalph Campbell 	atomic_set(&ah->refcount, 0);
1857f931551bSRalph Campbell 
1858f931551bSRalph Campbell 	ret = &ah->ibah;
1859f931551bSRalph Campbell 
1860f931551bSRalph Campbell bail:
1861f931551bSRalph Campbell 	return ret;
1862f931551bSRalph Campbell }
1863f931551bSRalph Campbell 
18641fb9fed6SMike Marciniszyn struct ib_ah *qib_create_qp0_ah(struct qib_ibport *ibp, u16 dlid)
18651fb9fed6SMike Marciniszyn {
18661fb9fed6SMike Marciniszyn 	struct ib_ah_attr attr;
18671fb9fed6SMike Marciniszyn 	struct ib_ah *ah = ERR_PTR(-EINVAL);
18681fb9fed6SMike Marciniszyn 	struct qib_qp *qp0;
18691fb9fed6SMike Marciniszyn 
1870041af0bbSMike Marciniszyn 	memset(&attr, 0, sizeof(attr));
18711fb9fed6SMike Marciniszyn 	attr.dlid = dlid;
18721fb9fed6SMike Marciniszyn 	attr.port_num = ppd_from_ibp(ibp)->port;
18731fb9fed6SMike Marciniszyn 	rcu_read_lock();
18741fb9fed6SMike Marciniszyn 	qp0 = rcu_dereference(ibp->qp0);
18751fb9fed6SMike Marciniszyn 	if (qp0)
18761fb9fed6SMike Marciniszyn 		ah = ib_create_ah(qp0->ibqp.pd, &attr);
18771fb9fed6SMike Marciniszyn 	rcu_read_unlock();
18781fb9fed6SMike Marciniszyn 	return ah;
18791fb9fed6SMike Marciniszyn }
18801fb9fed6SMike Marciniszyn 
1881f931551bSRalph Campbell /**
1882f931551bSRalph Campbell  * qib_destroy_ah - destroy an address handle
1883f931551bSRalph Campbell  * @ibah: the AH to destroy
1884f931551bSRalph Campbell  *
1885f931551bSRalph Campbell  * This may be called from interrupt context.
1886f931551bSRalph Campbell  */
1887f931551bSRalph Campbell static int qib_destroy_ah(struct ib_ah *ibah)
1888f931551bSRalph Campbell {
1889f931551bSRalph Campbell 	struct qib_ibdev *dev = to_idev(ibah->device);
1890f931551bSRalph Campbell 	struct qib_ah *ah = to_iah(ibah);
1891f931551bSRalph Campbell 	unsigned long flags;
1892f931551bSRalph Campbell 
1893f931551bSRalph Campbell 	if (atomic_read(&ah->refcount) != 0)
1894f931551bSRalph Campbell 		return -EBUSY;
1895f931551bSRalph Campbell 
1896f931551bSRalph Campbell 	spin_lock_irqsave(&dev->n_ahs_lock, flags);
1897f931551bSRalph Campbell 	dev->n_ahs_allocated--;
1898f931551bSRalph Campbell 	spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
1899f931551bSRalph Campbell 
1900f931551bSRalph Campbell 	kfree(ah);
1901f931551bSRalph Campbell 
1902f931551bSRalph Campbell 	return 0;
1903f931551bSRalph Campbell }
1904f931551bSRalph Campbell 
1905f931551bSRalph Campbell static int qib_modify_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr)
1906f931551bSRalph Campbell {
1907f931551bSRalph Campbell 	struct qib_ah *ah = to_iah(ibah);
1908f931551bSRalph Campbell 
1909f931551bSRalph Campbell 	if (qib_check_ah(ibah->device, ah_attr))
1910f931551bSRalph Campbell 		return -EINVAL;
1911f931551bSRalph Campbell 
1912f931551bSRalph Campbell 	ah->attr = *ah_attr;
1913f931551bSRalph Campbell 
1914f931551bSRalph Campbell 	return 0;
1915f931551bSRalph Campbell }
1916f931551bSRalph Campbell 
1917f931551bSRalph Campbell static int qib_query_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr)
1918f931551bSRalph Campbell {
1919f931551bSRalph Campbell 	struct qib_ah *ah = to_iah(ibah);
1920f931551bSRalph Campbell 
1921f931551bSRalph Campbell 	*ah_attr = ah->attr;
1922f931551bSRalph Campbell 
1923f931551bSRalph Campbell 	return 0;
1924f931551bSRalph Campbell }
1925f931551bSRalph Campbell 
1926f931551bSRalph Campbell /**
1927f931551bSRalph Campbell  * qib_get_npkeys - return the size of the PKEY table for context 0
1928f931551bSRalph Campbell  * @dd: the qlogic_ib device
1929f931551bSRalph Campbell  */
1930f931551bSRalph Campbell unsigned qib_get_npkeys(struct qib_devdata *dd)
1931f931551bSRalph Campbell {
1932f931551bSRalph Campbell 	return ARRAY_SIZE(dd->rcd[0]->pkeys);
1933f931551bSRalph Campbell }
1934f931551bSRalph Campbell 
1935f931551bSRalph Campbell /*
1936f931551bSRalph Campbell  * Return the indexed PKEY from the port PKEY table.
1937f931551bSRalph Campbell  * No need to validate rcd[ctxt]; the port is setup if we are here.
1938f931551bSRalph Campbell  */
1939f931551bSRalph Campbell unsigned qib_get_pkey(struct qib_ibport *ibp, unsigned index)
1940f931551bSRalph Campbell {
1941f931551bSRalph Campbell 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1942f931551bSRalph Campbell 	struct qib_devdata *dd = ppd->dd;
1943f931551bSRalph Campbell 	unsigned ctxt = ppd->hw_pidx;
1944f931551bSRalph Campbell 	unsigned ret;
1945f931551bSRalph Campbell 
1946f931551bSRalph Campbell 	/* dd->rcd null if mini_init or some init failures */
1947f931551bSRalph Campbell 	if (!dd->rcd || index >= ARRAY_SIZE(dd->rcd[ctxt]->pkeys))
1948f931551bSRalph Campbell 		ret = 0;
1949f931551bSRalph Campbell 	else
1950f931551bSRalph Campbell 		ret = dd->rcd[ctxt]->pkeys[index];
1951f931551bSRalph Campbell 
1952f931551bSRalph Campbell 	return ret;
1953f931551bSRalph Campbell }
1954f931551bSRalph Campbell 
1955f931551bSRalph Campbell static int qib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1956f931551bSRalph Campbell 			  u16 *pkey)
1957f931551bSRalph Campbell {
1958f931551bSRalph Campbell 	struct qib_devdata *dd = dd_from_ibdev(ibdev);
1959f931551bSRalph Campbell 	int ret;
1960f931551bSRalph Campbell 
1961f931551bSRalph Campbell 	if (index >= qib_get_npkeys(dd)) {
1962f931551bSRalph Campbell 		ret = -EINVAL;
1963f931551bSRalph Campbell 		goto bail;
1964f931551bSRalph Campbell 	}
1965f931551bSRalph Campbell 
1966f931551bSRalph Campbell 	*pkey = qib_get_pkey(to_iport(ibdev, port), index);
1967f931551bSRalph Campbell 	ret = 0;
1968f931551bSRalph Campbell 
1969f931551bSRalph Campbell bail:
1970f931551bSRalph Campbell 	return ret;
1971f931551bSRalph Campbell }
1972f931551bSRalph Campbell 
1973f931551bSRalph Campbell /**
1974f931551bSRalph Campbell  * qib_alloc_ucontext - allocate a ucontest
1975f931551bSRalph Campbell  * @ibdev: the infiniband device
1976f931551bSRalph Campbell  * @udata: not used by the QLogic_IB driver
1977f931551bSRalph Campbell  */
1978f931551bSRalph Campbell 
1979f931551bSRalph Campbell static struct ib_ucontext *qib_alloc_ucontext(struct ib_device *ibdev,
1980f931551bSRalph Campbell 					      struct ib_udata *udata)
1981f931551bSRalph Campbell {
1982f931551bSRalph Campbell 	struct qib_ucontext *context;
1983f931551bSRalph Campbell 	struct ib_ucontext *ret;
1984f931551bSRalph Campbell 
1985041af0bbSMike Marciniszyn 	context = kmalloc(sizeof(*context), GFP_KERNEL);
1986f931551bSRalph Campbell 	if (!context) {
1987f931551bSRalph Campbell 		ret = ERR_PTR(-ENOMEM);
1988f931551bSRalph Campbell 		goto bail;
1989f931551bSRalph Campbell 	}
1990f931551bSRalph Campbell 
1991f931551bSRalph Campbell 	ret = &context->ibucontext;
1992f931551bSRalph Campbell 
1993f931551bSRalph Campbell bail:
1994f931551bSRalph Campbell 	return ret;
1995f931551bSRalph Campbell }
1996f931551bSRalph Campbell 
1997f931551bSRalph Campbell static int qib_dealloc_ucontext(struct ib_ucontext *context)
1998f931551bSRalph Campbell {
1999f931551bSRalph Campbell 	kfree(to_iucontext(context));
2000f931551bSRalph Campbell 	return 0;
2001f931551bSRalph Campbell }
2002f931551bSRalph Campbell 
2003f931551bSRalph Campbell static void init_ibport(struct qib_pportdata *ppd)
2004f931551bSRalph Campbell {
2005f931551bSRalph Campbell 	struct qib_verbs_counters cntrs;
2006f931551bSRalph Campbell 	struct qib_ibport *ibp = &ppd->ibport_data;
2007f931551bSRalph Campbell 
2008f931551bSRalph Campbell 	spin_lock_init(&ibp->lock);
2009f931551bSRalph Campbell 	/* Set the prefix to the default value (see ch. 4.1.1) */
2010f931551bSRalph Campbell 	ibp->gid_prefix = IB_DEFAULT_GID_PREFIX;
2011f931551bSRalph Campbell 	ibp->sm_lid = be16_to_cpu(IB_LID_PERMISSIVE);
2012f931551bSRalph Campbell 	ibp->port_cap_flags = IB_PORT_SYS_IMAGE_GUID_SUP |
2013f931551bSRalph Campbell 		IB_PORT_CLIENT_REG_SUP | IB_PORT_SL_MAP_SUP |
2014f931551bSRalph Campbell 		IB_PORT_TRAP_SUP | IB_PORT_AUTO_MIGR_SUP |
2015f931551bSRalph Campbell 		IB_PORT_DR_NOTICE_SUP | IB_PORT_CAP_MASK_NOTICE_SUP |
2016f931551bSRalph Campbell 		IB_PORT_OTHER_LOCAL_CHANGES_SUP;
2017f931551bSRalph Campbell 	if (ppd->dd->flags & QIB_HAS_LINK_LATENCY)
2018f931551bSRalph Campbell 		ibp->port_cap_flags |= IB_PORT_LINK_LATENCY_SUP;
2019f931551bSRalph Campbell 	ibp->pma_counter_select[0] = IB_PMA_PORT_XMIT_DATA;
2020f931551bSRalph Campbell 	ibp->pma_counter_select[1] = IB_PMA_PORT_RCV_DATA;
2021f931551bSRalph Campbell 	ibp->pma_counter_select[2] = IB_PMA_PORT_XMIT_PKTS;
2022f931551bSRalph Campbell 	ibp->pma_counter_select[3] = IB_PMA_PORT_RCV_PKTS;
2023f931551bSRalph Campbell 	ibp->pma_counter_select[4] = IB_PMA_PORT_XMIT_WAIT;
2024f931551bSRalph Campbell 
2025f931551bSRalph Campbell 	/* Snapshot current HW counters to "clear" them. */
2026f931551bSRalph Campbell 	qib_get_counters(ppd, &cntrs);
2027f931551bSRalph Campbell 	ibp->z_symbol_error_counter = cntrs.symbol_error_counter;
2028f931551bSRalph Campbell 	ibp->z_link_error_recovery_counter =
2029f931551bSRalph Campbell 		cntrs.link_error_recovery_counter;
2030f931551bSRalph Campbell 	ibp->z_link_downed_counter = cntrs.link_downed_counter;
2031f931551bSRalph Campbell 	ibp->z_port_rcv_errors = cntrs.port_rcv_errors;
2032f931551bSRalph Campbell 	ibp->z_port_rcv_remphys_errors = cntrs.port_rcv_remphys_errors;
2033f931551bSRalph Campbell 	ibp->z_port_xmit_discards = cntrs.port_xmit_discards;
2034f931551bSRalph Campbell 	ibp->z_port_xmit_data = cntrs.port_xmit_data;
2035f931551bSRalph Campbell 	ibp->z_port_rcv_data = cntrs.port_rcv_data;
2036f931551bSRalph Campbell 	ibp->z_port_xmit_packets = cntrs.port_xmit_packets;
2037f931551bSRalph Campbell 	ibp->z_port_rcv_packets = cntrs.port_rcv_packets;
2038f931551bSRalph Campbell 	ibp->z_local_link_integrity_errors =
2039f931551bSRalph Campbell 		cntrs.local_link_integrity_errors;
2040f931551bSRalph Campbell 	ibp->z_excessive_buffer_overrun_errors =
2041f931551bSRalph Campbell 		cntrs.excessive_buffer_overrun_errors;
2042f931551bSRalph Campbell 	ibp->z_vl15_dropped = cntrs.vl15_dropped;
2043af061a64SMike Marciniszyn 	RCU_INIT_POINTER(ibp->qp0, NULL);
2044af061a64SMike Marciniszyn 	RCU_INIT_POINTER(ibp->qp1, NULL);
2045f931551bSRalph Campbell }
2046f931551bSRalph Campbell 
20477738613eSIra Weiny static int qib_port_immutable(struct ib_device *ibdev, u8 port_num,
20487738613eSIra Weiny 			      struct ib_port_immutable *immutable)
20497738613eSIra Weiny {
20507738613eSIra Weiny 	struct ib_port_attr attr;
20517738613eSIra Weiny 	int err;
20527738613eSIra Weiny 
20537738613eSIra Weiny 	err = qib_query_port(ibdev, port_num, &attr);
20547738613eSIra Weiny 	if (err)
20557738613eSIra Weiny 		return err;
20567738613eSIra Weiny 
20577738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
20587738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
2059f9b22e35SIra Weiny 	immutable->core_cap_flags = RDMA_CORE_PORT_IBA_IB;
2060337877a4SIra Weiny 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
20617738613eSIra Weiny 
20627738613eSIra Weiny 	return 0;
20637738613eSIra Weiny }
20647738613eSIra Weiny 
2065f931551bSRalph Campbell /**
2066f931551bSRalph Campbell  * qib_register_ib_device - register our device with the infiniband core
2067f931551bSRalph Campbell  * @dd: the device data structure
2068f931551bSRalph Campbell  * Return the allocated qib_ibdev pointer or NULL on error.
2069f931551bSRalph Campbell  */
2070f931551bSRalph Campbell int qib_register_ib_device(struct qib_devdata *dd)
2071f931551bSRalph Campbell {
2072f931551bSRalph Campbell 	struct qib_ibdev *dev = &dd->verbs_dev;
2073f931551bSRalph Campbell 	struct ib_device *ibdev = &dev->ibdev;
2074f931551bSRalph Campbell 	struct qib_pportdata *ppd = dd->pport;
2075f931551bSRalph Campbell 	unsigned i, lk_tab_size;
2076f931551bSRalph Campbell 	int ret;
2077f931551bSRalph Campbell 
2078f931551bSRalph Campbell 	dev->qp_table_size = ib_qib_qp_table_size;
2079af061a64SMike Marciniszyn 	get_random_bytes(&dev->qp_rnd, sizeof(dev->qp_rnd));
2080a46a2802SMike Marciniszyn 	dev->qp_table = kmalloc_array(
2081a46a2802SMike Marciniszyn 				dev->qp_table_size,
2082a46a2802SMike Marciniszyn 				sizeof(*dev->qp_table),
2083f931551bSRalph Campbell 				GFP_KERNEL);
2084f931551bSRalph Campbell 	if (!dev->qp_table) {
2085f931551bSRalph Campbell 		ret = -ENOMEM;
2086f931551bSRalph Campbell 		goto err_qpt;
2087f931551bSRalph Campbell 	}
2088af061a64SMike Marciniszyn 	for (i = 0; i < dev->qp_table_size; i++)
2089af061a64SMike Marciniszyn 		RCU_INIT_POINTER(dev->qp_table[i], NULL);
2090f931551bSRalph Campbell 
2091f931551bSRalph Campbell 	for (i = 0; i < dd->num_pports; i++)
2092f931551bSRalph Campbell 		init_ibport(ppd + i);
2093f931551bSRalph Campbell 
2094f931551bSRalph Campbell 	/* Only need to initialize non-zero fields. */
2095f931551bSRalph Campbell 	spin_lock_init(&dev->qpt_lock);
2096f931551bSRalph Campbell 	spin_lock_init(&dev->n_pds_lock);
2097f931551bSRalph Campbell 	spin_lock_init(&dev->n_ahs_lock);
2098f931551bSRalph Campbell 	spin_lock_init(&dev->n_cqs_lock);
2099f931551bSRalph Campbell 	spin_lock_init(&dev->n_qps_lock);
2100f931551bSRalph Campbell 	spin_lock_init(&dev->n_srqs_lock);
2101f931551bSRalph Campbell 	spin_lock_init(&dev->n_mcast_grps_lock);
2102f931551bSRalph Campbell 	init_timer(&dev->mem_timer);
2103f931551bSRalph Campbell 	dev->mem_timer.function = mem_timer;
2104f931551bSRalph Campbell 	dev->mem_timer.data = (unsigned long) dev;
2105f931551bSRalph Campbell 
2106f931551bSRalph Campbell 	qib_init_qpn_table(dd, &dev->qpn_table);
2107f931551bSRalph Campbell 
2108f931551bSRalph Campbell 	/*
2109f931551bSRalph Campbell 	 * The top ib_qib_lkey_table_size bits are used to index the
2110f931551bSRalph Campbell 	 * table.  The lower 8 bits can be owned by the user (copied from
2111f931551bSRalph Campbell 	 * the LKEY).  The remaining bits act as a generation number or tag.
2112f931551bSRalph Campbell 	 */
2113f931551bSRalph Campbell 	spin_lock_init(&dev->lk_table.lock);
2114d6f1c17eSMike Marciniszyn 	/* insure generation is at least 4 bits see keys.c */
2115d6f1c17eSMike Marciniszyn 	if (ib_qib_lkey_table_size > MAX_LKEY_TABLE_BITS) {
2116d6f1c17eSMike Marciniszyn 		qib_dev_warn(dd, "lkey bits %u too large, reduced to %u\n",
2117d6f1c17eSMike Marciniszyn 			ib_qib_lkey_table_size, MAX_LKEY_TABLE_BITS);
2118d6f1c17eSMike Marciniszyn 		ib_qib_lkey_table_size = MAX_LKEY_TABLE_BITS;
2119d6f1c17eSMike Marciniszyn 	}
2120f931551bSRalph Campbell 	dev->lk_table.max = 1 << ib_qib_lkey_table_size;
2121f931551bSRalph Campbell 	lk_tab_size = dev->lk_table.max * sizeof(*dev->lk_table.table);
21221fb9fed6SMike Marciniszyn 	dev->lk_table.table = (struct qib_mregion __rcu **)
2123d6f1c17eSMike Marciniszyn 		vmalloc(lk_tab_size);
2124f931551bSRalph Campbell 	if (dev->lk_table.table == NULL) {
2125f931551bSRalph Campbell 		ret = -ENOMEM;
2126f931551bSRalph Campbell 		goto err_lk;
2127f931551bSRalph Campbell 	}
21288aac4cc3SMike Marciniszyn 	RCU_INIT_POINTER(dev->dma_mr, NULL);
21298aac4cc3SMike Marciniszyn 	for (i = 0; i < dev->lk_table.max; i++)
21308aac4cc3SMike Marciniszyn 		RCU_INIT_POINTER(dev->lk_table.table[i], NULL);
2131f931551bSRalph Campbell 	INIT_LIST_HEAD(&dev->pending_mmaps);
2132f931551bSRalph Campbell 	spin_lock_init(&dev->pending_lock);
2133f931551bSRalph Campbell 	dev->mmap_offset = PAGE_SIZE;
2134f931551bSRalph Campbell 	spin_lock_init(&dev->mmap_offset_lock);
2135f931551bSRalph Campbell 	INIT_LIST_HEAD(&dev->piowait);
2136f931551bSRalph Campbell 	INIT_LIST_HEAD(&dev->dmawait);
2137f931551bSRalph Campbell 	INIT_LIST_HEAD(&dev->txwait);
2138f931551bSRalph Campbell 	INIT_LIST_HEAD(&dev->memwait);
2139f931551bSRalph Campbell 	INIT_LIST_HEAD(&dev->txreq_free);
2140f931551bSRalph Campbell 
2141f931551bSRalph Campbell 	if (ppd->sdma_descq_cnt) {
2142f931551bSRalph Campbell 		dev->pio_hdrs = dma_alloc_coherent(&dd->pcidev->dev,
2143f931551bSRalph Campbell 						ppd->sdma_descq_cnt *
2144f931551bSRalph Campbell 						sizeof(struct qib_pio_header),
2145f931551bSRalph Campbell 						&dev->pio_hdrs_phys,
2146f931551bSRalph Campbell 						GFP_KERNEL);
2147f931551bSRalph Campbell 		if (!dev->pio_hdrs) {
2148f931551bSRalph Campbell 			ret = -ENOMEM;
2149f931551bSRalph Campbell 			goto err_hdrs;
2150f931551bSRalph Campbell 		}
2151f931551bSRalph Campbell 	}
2152f931551bSRalph Campbell 
2153f931551bSRalph Campbell 	for (i = 0; i < ppd->sdma_descq_cnt; i++) {
2154f931551bSRalph Campbell 		struct qib_verbs_txreq *tx;
2155f931551bSRalph Campbell 
2156041af0bbSMike Marciniszyn 		tx = kzalloc(sizeof(*tx), GFP_KERNEL);
2157f931551bSRalph Campbell 		if (!tx) {
2158f931551bSRalph Campbell 			ret = -ENOMEM;
2159f931551bSRalph Campbell 			goto err_tx;
2160f931551bSRalph Campbell 		}
2161f931551bSRalph Campbell 		tx->hdr_inx = i;
2162f931551bSRalph Campbell 		list_add(&tx->txreq.list, &dev->txreq_free);
2163f931551bSRalph Campbell 	}
2164f931551bSRalph Campbell 
2165f931551bSRalph Campbell 	/*
2166f931551bSRalph Campbell 	 * The system image GUID is supposed to be the same for all
2167f931551bSRalph Campbell 	 * IB HCAs in a single system but since there can be other
2168f931551bSRalph Campbell 	 * device types in the system, we can't be sure this is unique.
2169f931551bSRalph Campbell 	 */
2170f931551bSRalph Campbell 	if (!ib_qib_sys_image_guid)
2171f931551bSRalph Campbell 		ib_qib_sys_image_guid = ppd->guid;
2172f931551bSRalph Campbell 
2173f931551bSRalph Campbell 	strlcpy(ibdev->name, "qib%d", IB_DEVICE_NAME_MAX);
2174f931551bSRalph Campbell 	ibdev->owner = THIS_MODULE;
2175f931551bSRalph Campbell 	ibdev->node_guid = ppd->guid;
2176f931551bSRalph Campbell 	ibdev->uverbs_abi_ver = QIB_UVERBS_ABI_VERSION;
2177f931551bSRalph Campbell 	ibdev->uverbs_cmd_mask =
2178f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)         |
2179f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)        |
2180f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)          |
2181f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)            |
2182f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)          |
2183f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)           |
2184f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_MODIFY_AH)           |
2185f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_QUERY_AH)            |
2186f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)          |
2187f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_REG_MR)              |
2188f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)            |
2189f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
2190f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)           |
2191f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)           |
2192f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)          |
2193f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_POLL_CQ)             |
2194f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ)       |
2195f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)           |
2196f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)            |
2197f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)           |
2198f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)          |
2199f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_POST_SEND)           |
2200f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_POST_RECV)           |
2201f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)        |
2202f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)        |
2203f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)          |
2204f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)          |
2205f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)           |
2206f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)         |
2207f931551bSRalph Campbell 		(1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
2208f931551bSRalph Campbell 	ibdev->node_type = RDMA_NODE_IB_CA;
2209f931551bSRalph Campbell 	ibdev->phys_port_cnt = dd->num_pports;
2210f931551bSRalph Campbell 	ibdev->num_comp_vectors = 1;
2211f931551bSRalph Campbell 	ibdev->dma_device = &dd->pcidev->dev;
2212f931551bSRalph Campbell 	ibdev->query_device = qib_query_device;
2213f931551bSRalph Campbell 	ibdev->modify_device = qib_modify_device;
2214f931551bSRalph Campbell 	ibdev->query_port = qib_query_port;
2215f931551bSRalph Campbell 	ibdev->modify_port = qib_modify_port;
2216f931551bSRalph Campbell 	ibdev->query_pkey = qib_query_pkey;
2217f931551bSRalph Campbell 	ibdev->query_gid = qib_query_gid;
2218f931551bSRalph Campbell 	ibdev->alloc_ucontext = qib_alloc_ucontext;
2219f931551bSRalph Campbell 	ibdev->dealloc_ucontext = qib_dealloc_ucontext;
2220f931551bSRalph Campbell 	ibdev->alloc_pd = qib_alloc_pd;
2221f931551bSRalph Campbell 	ibdev->dealloc_pd = qib_dealloc_pd;
2222f931551bSRalph Campbell 	ibdev->create_ah = qib_create_ah;
2223f931551bSRalph Campbell 	ibdev->destroy_ah = qib_destroy_ah;
2224f931551bSRalph Campbell 	ibdev->modify_ah = qib_modify_ah;
2225f931551bSRalph Campbell 	ibdev->query_ah = qib_query_ah;
2226f931551bSRalph Campbell 	ibdev->create_srq = qib_create_srq;
2227f931551bSRalph Campbell 	ibdev->modify_srq = qib_modify_srq;
2228f931551bSRalph Campbell 	ibdev->query_srq = qib_query_srq;
2229f931551bSRalph Campbell 	ibdev->destroy_srq = qib_destroy_srq;
2230f931551bSRalph Campbell 	ibdev->create_qp = qib_create_qp;
2231f931551bSRalph Campbell 	ibdev->modify_qp = qib_modify_qp;
2232f931551bSRalph Campbell 	ibdev->query_qp = qib_query_qp;
2233f931551bSRalph Campbell 	ibdev->destroy_qp = qib_destroy_qp;
2234f931551bSRalph Campbell 	ibdev->post_send = qib_post_send;
2235f931551bSRalph Campbell 	ibdev->post_recv = qib_post_receive;
2236f931551bSRalph Campbell 	ibdev->post_srq_recv = qib_post_srq_receive;
2237f931551bSRalph Campbell 	ibdev->create_cq = qib_create_cq;
2238f931551bSRalph Campbell 	ibdev->destroy_cq = qib_destroy_cq;
2239f931551bSRalph Campbell 	ibdev->resize_cq = qib_resize_cq;
2240f931551bSRalph Campbell 	ibdev->poll_cq = qib_poll_cq;
2241f931551bSRalph Campbell 	ibdev->req_notify_cq = qib_req_notify_cq;
2242f931551bSRalph Campbell 	ibdev->get_dma_mr = qib_get_dma_mr;
2243f931551bSRalph Campbell 	ibdev->reg_phys_mr = qib_reg_phys_mr;
2244f931551bSRalph Campbell 	ibdev->reg_user_mr = qib_reg_user_mr;
2245f931551bSRalph Campbell 	ibdev->dereg_mr = qib_dereg_mr;
2246f931551bSRalph Campbell 	ibdev->alloc_fast_reg_mr = qib_alloc_fast_reg_mr;
2247f931551bSRalph Campbell 	ibdev->alloc_fast_reg_page_list = qib_alloc_fast_reg_page_list;
2248f931551bSRalph Campbell 	ibdev->free_fast_reg_page_list = qib_free_fast_reg_page_list;
2249f931551bSRalph Campbell 	ibdev->alloc_fmr = qib_alloc_fmr;
2250f931551bSRalph Campbell 	ibdev->map_phys_fmr = qib_map_phys_fmr;
2251f931551bSRalph Campbell 	ibdev->unmap_fmr = qib_unmap_fmr;
2252f931551bSRalph Campbell 	ibdev->dealloc_fmr = qib_dealloc_fmr;
2253f931551bSRalph Campbell 	ibdev->attach_mcast = qib_multicast_attach;
2254f931551bSRalph Campbell 	ibdev->detach_mcast = qib_multicast_detach;
2255f931551bSRalph Campbell 	ibdev->process_mad = qib_process_mad;
2256f931551bSRalph Campbell 	ibdev->mmap = qib_mmap;
2257f931551bSRalph Campbell 	ibdev->dma_ops = &qib_dma_mapping_ops;
22587738613eSIra Weiny 	ibdev->get_port_immutable = qib_port_immutable;
2259f931551bSRalph Campbell 
2260f931551bSRalph Campbell 	snprintf(ibdev->node_desc, sizeof(ibdev->node_desc),
2261e2eed58bSVinit Agnihotri 		 "Intel Infiniband HCA %s", init_utsname()->nodename);
2262f931551bSRalph Campbell 
2263f931551bSRalph Campbell 	ret = ib_register_device(ibdev, qib_create_port_files);
2264f931551bSRalph Campbell 	if (ret)
2265f931551bSRalph Campbell 		goto err_reg;
2266f931551bSRalph Campbell 
2267f931551bSRalph Campbell 	ret = qib_create_agents(dev);
2268f931551bSRalph Campbell 	if (ret)
2269f931551bSRalph Campbell 		goto err_agents;
2270f931551bSRalph Campbell 
2271c9bdad3cSMike Marciniszyn 	ret = qib_verbs_register_sysfs(dd);
2272c9bdad3cSMike Marciniszyn 	if (ret)
2273f931551bSRalph Campbell 		goto err_class;
2274f931551bSRalph Campbell 
2275f931551bSRalph Campbell 	goto bail;
2276f931551bSRalph Campbell 
2277f931551bSRalph Campbell err_class:
2278f931551bSRalph Campbell 	qib_free_agents(dev);
2279f931551bSRalph Campbell err_agents:
2280f931551bSRalph Campbell 	ib_unregister_device(ibdev);
2281f931551bSRalph Campbell err_reg:
2282f931551bSRalph Campbell err_tx:
2283f931551bSRalph Campbell 	while (!list_empty(&dev->txreq_free)) {
2284f931551bSRalph Campbell 		struct list_head *l = dev->txreq_free.next;
2285f931551bSRalph Campbell 		struct qib_verbs_txreq *tx;
2286f931551bSRalph Campbell 
2287f931551bSRalph Campbell 		list_del(l);
2288f931551bSRalph Campbell 		tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
2289f931551bSRalph Campbell 		kfree(tx);
2290f931551bSRalph Campbell 	}
2291f931551bSRalph Campbell 	if (ppd->sdma_descq_cnt)
2292f931551bSRalph Campbell 		dma_free_coherent(&dd->pcidev->dev,
2293f931551bSRalph Campbell 				  ppd->sdma_descq_cnt *
2294f931551bSRalph Campbell 					sizeof(struct qib_pio_header),
2295f931551bSRalph Campbell 				  dev->pio_hdrs, dev->pio_hdrs_phys);
2296f931551bSRalph Campbell err_hdrs:
2297d6f1c17eSMike Marciniszyn 	vfree(dev->lk_table.table);
2298f931551bSRalph Campbell err_lk:
2299f931551bSRalph Campbell 	kfree(dev->qp_table);
2300f931551bSRalph Campbell err_qpt:
2301f931551bSRalph Campbell 	qib_dev_err(dd, "cannot register verbs: %d!\n", -ret);
2302f931551bSRalph Campbell bail:
2303f931551bSRalph Campbell 	return ret;
2304f931551bSRalph Campbell }
2305f931551bSRalph Campbell 
2306f931551bSRalph Campbell void qib_unregister_ib_device(struct qib_devdata *dd)
2307f931551bSRalph Campbell {
2308f931551bSRalph Campbell 	struct qib_ibdev *dev = &dd->verbs_dev;
2309f931551bSRalph Campbell 	struct ib_device *ibdev = &dev->ibdev;
2310f931551bSRalph Campbell 	u32 qps_inuse;
2311f931551bSRalph Campbell 	unsigned lk_tab_size;
2312f931551bSRalph Campbell 
2313f931551bSRalph Campbell 	qib_verbs_unregister_sysfs(dd);
2314f931551bSRalph Campbell 
2315f931551bSRalph Campbell 	qib_free_agents(dev);
2316f931551bSRalph Campbell 
2317f931551bSRalph Campbell 	ib_unregister_device(ibdev);
2318f931551bSRalph Campbell 
2319f931551bSRalph Campbell 	if (!list_empty(&dev->piowait))
2320f931551bSRalph Campbell 		qib_dev_err(dd, "piowait list not empty!\n");
2321f931551bSRalph Campbell 	if (!list_empty(&dev->dmawait))
2322f931551bSRalph Campbell 		qib_dev_err(dd, "dmawait list not empty!\n");
2323f931551bSRalph Campbell 	if (!list_empty(&dev->txwait))
2324f931551bSRalph Campbell 		qib_dev_err(dd, "txwait list not empty!\n");
2325f931551bSRalph Campbell 	if (!list_empty(&dev->memwait))
2326f931551bSRalph Campbell 		qib_dev_err(dd, "memwait list not empty!\n");
2327f931551bSRalph Campbell 	if (dev->dma_mr)
2328f931551bSRalph Campbell 		qib_dev_err(dd, "DMA MR not NULL!\n");
2329f931551bSRalph Campbell 
2330f931551bSRalph Campbell 	qps_inuse = qib_free_all_qps(dd);
2331f931551bSRalph Campbell 	if (qps_inuse)
2332f931551bSRalph Campbell 		qib_dev_err(dd, "QP memory leak! %u still in use\n",
2333f931551bSRalph Campbell 			    qps_inuse);
2334f931551bSRalph Campbell 
2335f931551bSRalph Campbell 	del_timer_sync(&dev->mem_timer);
2336f931551bSRalph Campbell 	qib_free_qpn_table(&dev->qpn_table);
2337f931551bSRalph Campbell 	while (!list_empty(&dev->txreq_free)) {
2338f931551bSRalph Campbell 		struct list_head *l = dev->txreq_free.next;
2339f931551bSRalph Campbell 		struct qib_verbs_txreq *tx;
2340f931551bSRalph Campbell 
2341f931551bSRalph Campbell 		list_del(l);
2342f931551bSRalph Campbell 		tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
2343f931551bSRalph Campbell 		kfree(tx);
2344f931551bSRalph Campbell 	}
2345f931551bSRalph Campbell 	if (dd->pport->sdma_descq_cnt)
2346f931551bSRalph Campbell 		dma_free_coherent(&dd->pcidev->dev,
2347f931551bSRalph Campbell 				  dd->pport->sdma_descq_cnt *
2348f931551bSRalph Campbell 					sizeof(struct qib_pio_header),
2349f931551bSRalph Campbell 				  dev->pio_hdrs, dev->pio_hdrs_phys);
2350f931551bSRalph Campbell 	lk_tab_size = dev->lk_table.max * sizeof(*dev->lk_table.table);
2351d6f1c17eSMike Marciniszyn 	vfree(dev->lk_table.table);
2352f931551bSRalph Campbell 	kfree(dev->qp_table);
2353f931551bSRalph Campbell }
2354551ace12SMike Marciniszyn 
2355551ace12SMike Marciniszyn /*
2356551ace12SMike Marciniszyn  * This must be called with s_lock held.
2357551ace12SMike Marciniszyn  */
2358551ace12SMike Marciniszyn void qib_schedule_send(struct qib_qp *qp)
2359551ace12SMike Marciniszyn {
2360551ace12SMike Marciniszyn 	if (qib_send_ok(qp)) {
2361551ace12SMike Marciniszyn 		struct qib_ibport *ibp =
2362551ace12SMike Marciniszyn 			to_iport(qp->ibqp.device, qp->port_num);
2363551ace12SMike Marciniszyn 		struct qib_pportdata *ppd = ppd_from_ibp(ibp);
2364551ace12SMike Marciniszyn 
2365551ace12SMike Marciniszyn 		queue_work(ppd->qib_wq, &qp->s_work);
2366551ace12SMike Marciniszyn 	}
2367551ace12SMike Marciniszyn }
2368