xref: /openbmc/linux/drivers/infiniband/hw/qib/qib_verbs.c (revision 05cf4fe738242183f1237f1b3a28b4479348c0a1)
1 /*
2  * Copyright (c) 2012 - 2018 Intel Corporation.  All rights reserved.
3  * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
4  * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 
35 #include <rdma/ib_mad.h>
36 #include <rdma/ib_user_verbs.h>
37 #include <linux/io.h>
38 #include <linux/module.h>
39 #include <linux/utsname.h>
40 #include <linux/rculist.h>
41 #include <linux/mm.h>
42 #include <linux/random.h>
43 #include <linux/vmalloc.h>
44 #include <rdma/rdma_vt.h>
45 
46 #include "qib.h"
47 #include "qib_common.h"
48 
49 static unsigned int ib_qib_qp_table_size = 256;
50 module_param_named(qp_table_size, ib_qib_qp_table_size, uint, S_IRUGO);
51 MODULE_PARM_DESC(qp_table_size, "QP table size");
52 
53 static unsigned int qib_lkey_table_size = 16;
54 module_param_named(lkey_table_size, qib_lkey_table_size, uint,
55 		   S_IRUGO);
56 MODULE_PARM_DESC(lkey_table_size,
57 		 "LKEY table size in bits (2^n, 1 <= n <= 23)");
58 
59 static unsigned int ib_qib_max_pds = 0xFFFF;
60 module_param_named(max_pds, ib_qib_max_pds, uint, S_IRUGO);
61 MODULE_PARM_DESC(max_pds,
62 		 "Maximum number of protection domains to support");
63 
64 static unsigned int ib_qib_max_ahs = 0xFFFF;
65 module_param_named(max_ahs, ib_qib_max_ahs, uint, S_IRUGO);
66 MODULE_PARM_DESC(max_ahs, "Maximum number of address handles to support");
67 
68 unsigned int ib_qib_max_cqes = 0x2FFFF;
69 module_param_named(max_cqes, ib_qib_max_cqes, uint, S_IRUGO);
70 MODULE_PARM_DESC(max_cqes,
71 		 "Maximum number of completion queue entries to support");
72 
73 unsigned int ib_qib_max_cqs = 0x1FFFF;
74 module_param_named(max_cqs, ib_qib_max_cqs, uint, S_IRUGO);
75 MODULE_PARM_DESC(max_cqs, "Maximum number of completion queues to support");
76 
77 unsigned int ib_qib_max_qp_wrs = 0x3FFF;
78 module_param_named(max_qp_wrs, ib_qib_max_qp_wrs, uint, S_IRUGO);
79 MODULE_PARM_DESC(max_qp_wrs, "Maximum number of QP WRs to support");
80 
81 unsigned int ib_qib_max_qps = 16384;
82 module_param_named(max_qps, ib_qib_max_qps, uint, S_IRUGO);
83 MODULE_PARM_DESC(max_qps, "Maximum number of QPs to support");
84 
85 unsigned int ib_qib_max_sges = 0x60;
86 module_param_named(max_sges, ib_qib_max_sges, uint, S_IRUGO);
87 MODULE_PARM_DESC(max_sges, "Maximum number of SGEs to support");
88 
89 unsigned int ib_qib_max_mcast_grps = 16384;
90 module_param_named(max_mcast_grps, ib_qib_max_mcast_grps, uint, S_IRUGO);
91 MODULE_PARM_DESC(max_mcast_grps,
92 		 "Maximum number of multicast groups to support");
93 
94 unsigned int ib_qib_max_mcast_qp_attached = 16;
95 module_param_named(max_mcast_qp_attached, ib_qib_max_mcast_qp_attached,
96 		   uint, S_IRUGO);
97 MODULE_PARM_DESC(max_mcast_qp_attached,
98 		 "Maximum number of attached QPs to support");
99 
100 unsigned int ib_qib_max_srqs = 1024;
101 module_param_named(max_srqs, ib_qib_max_srqs, uint, S_IRUGO);
102 MODULE_PARM_DESC(max_srqs, "Maximum number of SRQs to support");
103 
104 unsigned int ib_qib_max_srq_sges = 128;
105 module_param_named(max_srq_sges, ib_qib_max_srq_sges, uint, S_IRUGO);
106 MODULE_PARM_DESC(max_srq_sges, "Maximum number of SRQ SGEs to support");
107 
108 unsigned int ib_qib_max_srq_wrs = 0x1FFFF;
109 module_param_named(max_srq_wrs, ib_qib_max_srq_wrs, uint, S_IRUGO);
110 MODULE_PARM_DESC(max_srq_wrs, "Maximum number of SRQ WRs support");
111 
112 static unsigned int ib_qib_disable_sma;
113 module_param_named(disable_sma, ib_qib_disable_sma, uint, S_IWUSR | S_IRUGO);
114 MODULE_PARM_DESC(disable_sma, "Disable the SMA");
115 
116 /*
117  * Translate ib_wr_opcode into ib_wc_opcode.
118  */
119 const enum ib_wc_opcode ib_qib_wc_opcode[] = {
120 	[IB_WR_RDMA_WRITE] = IB_WC_RDMA_WRITE,
121 	[IB_WR_RDMA_WRITE_WITH_IMM] = IB_WC_RDMA_WRITE,
122 	[IB_WR_SEND] = IB_WC_SEND,
123 	[IB_WR_SEND_WITH_IMM] = IB_WC_SEND,
124 	[IB_WR_RDMA_READ] = IB_WC_RDMA_READ,
125 	[IB_WR_ATOMIC_CMP_AND_SWP] = IB_WC_COMP_SWAP,
126 	[IB_WR_ATOMIC_FETCH_AND_ADD] = IB_WC_FETCH_ADD
127 };
128 
129 /*
130  * System image GUID.
131  */
132 __be64 ib_qib_sys_image_guid;
133 
134 /*
135  * Count the number of DMA descriptors needed to send length bytes of data.
136  * Don't modify the qib_sge_state to get the count.
137  * Return zero if any of the segments is not aligned.
138  */
139 static u32 qib_count_sge(struct rvt_sge_state *ss, u32 length)
140 {
141 	struct rvt_sge *sg_list = ss->sg_list;
142 	struct rvt_sge sge = ss->sge;
143 	u8 num_sge = ss->num_sge;
144 	u32 ndesc = 1;  /* count the header */
145 
146 	while (length) {
147 		u32 len = sge.length;
148 
149 		if (len > length)
150 			len = length;
151 		if (len > sge.sge_length)
152 			len = sge.sge_length;
153 		BUG_ON(len == 0);
154 		if (((long) sge.vaddr & (sizeof(u32) - 1)) ||
155 		    (len != length && (len & (sizeof(u32) - 1)))) {
156 			ndesc = 0;
157 			break;
158 		}
159 		ndesc++;
160 		sge.vaddr += len;
161 		sge.length -= len;
162 		sge.sge_length -= len;
163 		if (sge.sge_length == 0) {
164 			if (--num_sge)
165 				sge = *sg_list++;
166 		} else if (sge.length == 0 && sge.mr->lkey) {
167 			if (++sge.n >= RVT_SEGSZ) {
168 				if (++sge.m >= sge.mr->mapsz)
169 					break;
170 				sge.n = 0;
171 			}
172 			sge.vaddr =
173 				sge.mr->map[sge.m]->segs[sge.n].vaddr;
174 			sge.length =
175 				sge.mr->map[sge.m]->segs[sge.n].length;
176 		}
177 		length -= len;
178 	}
179 	return ndesc;
180 }
181 
182 /*
183  * Copy from the SGEs to the data buffer.
184  */
185 static void qib_copy_from_sge(void *data, struct rvt_sge_state *ss, u32 length)
186 {
187 	struct rvt_sge *sge = &ss->sge;
188 
189 	while (length) {
190 		u32 len = sge->length;
191 
192 		if (len > length)
193 			len = length;
194 		if (len > sge->sge_length)
195 			len = sge->sge_length;
196 		BUG_ON(len == 0);
197 		memcpy(data, sge->vaddr, len);
198 		sge->vaddr += len;
199 		sge->length -= len;
200 		sge->sge_length -= len;
201 		if (sge->sge_length == 0) {
202 			if (--ss->num_sge)
203 				*sge = *ss->sg_list++;
204 		} else if (sge->length == 0 && sge->mr->lkey) {
205 			if (++sge->n >= RVT_SEGSZ) {
206 				if (++sge->m >= sge->mr->mapsz)
207 					break;
208 				sge->n = 0;
209 			}
210 			sge->vaddr =
211 				sge->mr->map[sge->m]->segs[sge->n].vaddr;
212 			sge->length =
213 				sge->mr->map[sge->m]->segs[sge->n].length;
214 		}
215 		data += len;
216 		length -= len;
217 	}
218 }
219 
220 /**
221  * qib_qp_rcv - processing an incoming packet on a QP
222  * @rcd: the context pointer
223  * @hdr: the packet header
224  * @has_grh: true if the packet has a GRH
225  * @data: the packet data
226  * @tlen: the packet length
227  * @qp: the QP the packet came on
228  *
229  * This is called from qib_ib_rcv() to process an incoming packet
230  * for the given QP.
231  * Called at interrupt level.
232  */
233 static void qib_qp_rcv(struct qib_ctxtdata *rcd, struct ib_header *hdr,
234 		       int has_grh, void *data, u32 tlen, struct rvt_qp *qp)
235 {
236 	struct qib_ibport *ibp = &rcd->ppd->ibport_data;
237 
238 	spin_lock(&qp->r_lock);
239 
240 	/* Check for valid receive state. */
241 	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
242 		ibp->rvp.n_pkt_drops++;
243 		goto unlock;
244 	}
245 
246 	switch (qp->ibqp.qp_type) {
247 	case IB_QPT_SMI:
248 	case IB_QPT_GSI:
249 		if (ib_qib_disable_sma)
250 			break;
251 		/* FALLTHROUGH */
252 	case IB_QPT_UD:
253 		qib_ud_rcv(ibp, hdr, has_grh, data, tlen, qp);
254 		break;
255 
256 	case IB_QPT_RC:
257 		qib_rc_rcv(rcd, hdr, has_grh, data, tlen, qp);
258 		break;
259 
260 	case IB_QPT_UC:
261 		qib_uc_rcv(ibp, hdr, has_grh, data, tlen, qp);
262 		break;
263 
264 	default:
265 		break;
266 	}
267 
268 unlock:
269 	spin_unlock(&qp->r_lock);
270 }
271 
272 /**
273  * qib_ib_rcv - process an incoming packet
274  * @rcd: the context pointer
275  * @rhdr: the header of the packet
276  * @data: the packet payload
277  * @tlen: the packet length
278  *
279  * This is called from qib_kreceive() to process an incoming packet at
280  * interrupt level. Tlen is the length of the header + data + CRC in bytes.
281  */
282 void qib_ib_rcv(struct qib_ctxtdata *rcd, void *rhdr, void *data, u32 tlen)
283 {
284 	struct qib_pportdata *ppd = rcd->ppd;
285 	struct qib_ibport *ibp = &ppd->ibport_data;
286 	struct ib_header *hdr = rhdr;
287 	struct qib_devdata *dd = ppd->dd;
288 	struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
289 	struct ib_other_headers *ohdr;
290 	struct rvt_qp *qp;
291 	u32 qp_num;
292 	int lnh;
293 	u8 opcode;
294 	u16 lid;
295 
296 	/* 24 == LRH+BTH+CRC */
297 	if (unlikely(tlen < 24))
298 		goto drop;
299 
300 	/* Check for a valid destination LID (see ch. 7.11.1). */
301 	lid = be16_to_cpu(hdr->lrh[1]);
302 	if (lid < be16_to_cpu(IB_MULTICAST_LID_BASE)) {
303 		lid &= ~((1 << ppd->lmc) - 1);
304 		if (unlikely(lid != ppd->lid))
305 			goto drop;
306 	}
307 
308 	/* Check for GRH */
309 	lnh = be16_to_cpu(hdr->lrh[0]) & 3;
310 	if (lnh == QIB_LRH_BTH)
311 		ohdr = &hdr->u.oth;
312 	else if (lnh == QIB_LRH_GRH) {
313 		u32 vtf;
314 
315 		ohdr = &hdr->u.l.oth;
316 		if (hdr->u.l.grh.next_hdr != IB_GRH_NEXT_HDR)
317 			goto drop;
318 		vtf = be32_to_cpu(hdr->u.l.grh.version_tclass_flow);
319 		if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
320 			goto drop;
321 	} else
322 		goto drop;
323 
324 	opcode = (be32_to_cpu(ohdr->bth[0]) >> 24) & 0x7f;
325 #ifdef CONFIG_DEBUG_FS
326 	rcd->opstats->stats[opcode].n_bytes += tlen;
327 	rcd->opstats->stats[opcode].n_packets++;
328 #endif
329 
330 	/* Get the destination QP number. */
331 	qp_num = be32_to_cpu(ohdr->bth[1]) & RVT_QPN_MASK;
332 	if (qp_num == QIB_MULTICAST_QPN) {
333 		struct rvt_mcast *mcast;
334 		struct rvt_mcast_qp *p;
335 
336 		if (lnh != QIB_LRH_GRH)
337 			goto drop;
338 		mcast = rvt_mcast_find(&ibp->rvp, &hdr->u.l.grh.dgid, lid);
339 		if (mcast == NULL)
340 			goto drop;
341 		this_cpu_inc(ibp->pmastats->n_multicast_rcv);
342 		list_for_each_entry_rcu(p, &mcast->qp_list, list)
343 			qib_qp_rcv(rcd, hdr, 1, data, tlen, p->qp);
344 		/*
345 		 * Notify rvt_multicast_detach() if it is waiting for us
346 		 * to finish.
347 		 */
348 		if (atomic_dec_return(&mcast->refcount) <= 1)
349 			wake_up(&mcast->wait);
350 	} else {
351 		rcu_read_lock();
352 		qp = rvt_lookup_qpn(rdi, &ibp->rvp, qp_num);
353 		if (!qp) {
354 			rcu_read_unlock();
355 			goto drop;
356 		}
357 		this_cpu_inc(ibp->pmastats->n_unicast_rcv);
358 		qib_qp_rcv(rcd, hdr, lnh == QIB_LRH_GRH, data, tlen, qp);
359 		rcu_read_unlock();
360 	}
361 	return;
362 
363 drop:
364 	ibp->rvp.n_pkt_drops++;
365 }
366 
367 /*
368  * This is called from a timer to check for QPs
369  * which need kernel memory in order to send a packet.
370  */
371 static void mem_timer(struct timer_list *t)
372 {
373 	struct qib_ibdev *dev = from_timer(dev, t, mem_timer);
374 	struct list_head *list = &dev->memwait;
375 	struct rvt_qp *qp = NULL;
376 	struct qib_qp_priv *priv = NULL;
377 	unsigned long flags;
378 
379 	spin_lock_irqsave(&dev->rdi.pending_lock, flags);
380 	if (!list_empty(list)) {
381 		priv = list_entry(list->next, struct qib_qp_priv, iowait);
382 		qp = priv->owner;
383 		list_del_init(&priv->iowait);
384 		rvt_get_qp(qp);
385 		if (!list_empty(list))
386 			mod_timer(&dev->mem_timer, jiffies + 1);
387 	}
388 	spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
389 
390 	if (qp) {
391 		spin_lock_irqsave(&qp->s_lock, flags);
392 		if (qp->s_flags & RVT_S_WAIT_KMEM) {
393 			qp->s_flags &= ~RVT_S_WAIT_KMEM;
394 			qib_schedule_send(qp);
395 		}
396 		spin_unlock_irqrestore(&qp->s_lock, flags);
397 		rvt_put_qp(qp);
398 	}
399 }
400 
401 #ifdef __LITTLE_ENDIAN
402 static inline u32 get_upper_bits(u32 data, u32 shift)
403 {
404 	return data >> shift;
405 }
406 
407 static inline u32 set_upper_bits(u32 data, u32 shift)
408 {
409 	return data << shift;
410 }
411 
412 static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
413 {
414 	data <<= ((sizeof(u32) - n) * BITS_PER_BYTE);
415 	data >>= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
416 	return data;
417 }
418 #else
419 static inline u32 get_upper_bits(u32 data, u32 shift)
420 {
421 	return data << shift;
422 }
423 
424 static inline u32 set_upper_bits(u32 data, u32 shift)
425 {
426 	return data >> shift;
427 }
428 
429 static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
430 {
431 	data >>= ((sizeof(u32) - n) * BITS_PER_BYTE);
432 	data <<= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
433 	return data;
434 }
435 #endif
436 
437 static void copy_io(u32 __iomem *piobuf, struct rvt_sge_state *ss,
438 		    u32 length, unsigned flush_wc)
439 {
440 	u32 extra = 0;
441 	u32 data = 0;
442 	u32 last;
443 
444 	while (1) {
445 		u32 len = ss->sge.length;
446 		u32 off;
447 
448 		if (len > length)
449 			len = length;
450 		if (len > ss->sge.sge_length)
451 			len = ss->sge.sge_length;
452 		BUG_ON(len == 0);
453 		/* If the source address is not aligned, try to align it. */
454 		off = (unsigned long)ss->sge.vaddr & (sizeof(u32) - 1);
455 		if (off) {
456 			u32 *addr = (u32 *)((unsigned long)ss->sge.vaddr &
457 					    ~(sizeof(u32) - 1));
458 			u32 v = get_upper_bits(*addr, off * BITS_PER_BYTE);
459 			u32 y;
460 
461 			y = sizeof(u32) - off;
462 			if (len > y)
463 				len = y;
464 			if (len + extra >= sizeof(u32)) {
465 				data |= set_upper_bits(v, extra *
466 						       BITS_PER_BYTE);
467 				len = sizeof(u32) - extra;
468 				if (len == length) {
469 					last = data;
470 					break;
471 				}
472 				__raw_writel(data, piobuf);
473 				piobuf++;
474 				extra = 0;
475 				data = 0;
476 			} else {
477 				/* Clear unused upper bytes */
478 				data |= clear_upper_bytes(v, len, extra);
479 				if (len == length) {
480 					last = data;
481 					break;
482 				}
483 				extra += len;
484 			}
485 		} else if (extra) {
486 			/* Source address is aligned. */
487 			u32 *addr = (u32 *) ss->sge.vaddr;
488 			int shift = extra * BITS_PER_BYTE;
489 			int ushift = 32 - shift;
490 			u32 l = len;
491 
492 			while (l >= sizeof(u32)) {
493 				u32 v = *addr;
494 
495 				data |= set_upper_bits(v, shift);
496 				__raw_writel(data, piobuf);
497 				data = get_upper_bits(v, ushift);
498 				piobuf++;
499 				addr++;
500 				l -= sizeof(u32);
501 			}
502 			/*
503 			 * We still have 'extra' number of bytes leftover.
504 			 */
505 			if (l) {
506 				u32 v = *addr;
507 
508 				if (l + extra >= sizeof(u32)) {
509 					data |= set_upper_bits(v, shift);
510 					len -= l + extra - sizeof(u32);
511 					if (len == length) {
512 						last = data;
513 						break;
514 					}
515 					__raw_writel(data, piobuf);
516 					piobuf++;
517 					extra = 0;
518 					data = 0;
519 				} else {
520 					/* Clear unused upper bytes */
521 					data |= clear_upper_bytes(v, l, extra);
522 					if (len == length) {
523 						last = data;
524 						break;
525 					}
526 					extra += l;
527 				}
528 			} else if (len == length) {
529 				last = data;
530 				break;
531 			}
532 		} else if (len == length) {
533 			u32 w;
534 
535 			/*
536 			 * Need to round up for the last dword in the
537 			 * packet.
538 			 */
539 			w = (len + 3) >> 2;
540 			qib_pio_copy(piobuf, ss->sge.vaddr, w - 1);
541 			piobuf += w - 1;
542 			last = ((u32 *) ss->sge.vaddr)[w - 1];
543 			break;
544 		} else {
545 			u32 w = len >> 2;
546 
547 			qib_pio_copy(piobuf, ss->sge.vaddr, w);
548 			piobuf += w;
549 
550 			extra = len & (sizeof(u32) - 1);
551 			if (extra) {
552 				u32 v = ((u32 *) ss->sge.vaddr)[w];
553 
554 				/* Clear unused upper bytes */
555 				data = clear_upper_bytes(v, extra, 0);
556 			}
557 		}
558 		rvt_update_sge(ss, len, false);
559 		length -= len;
560 	}
561 	/* Update address before sending packet. */
562 	rvt_update_sge(ss, length, false);
563 	if (flush_wc) {
564 		/* must flush early everything before trigger word */
565 		qib_flush_wc();
566 		__raw_writel(last, piobuf);
567 		/* be sure trigger word is written */
568 		qib_flush_wc();
569 	} else
570 		__raw_writel(last, piobuf);
571 }
572 
573 static noinline struct qib_verbs_txreq *__get_txreq(struct qib_ibdev *dev,
574 					   struct rvt_qp *qp)
575 {
576 	struct qib_qp_priv *priv = qp->priv;
577 	struct qib_verbs_txreq *tx;
578 	unsigned long flags;
579 
580 	spin_lock_irqsave(&qp->s_lock, flags);
581 	spin_lock(&dev->rdi.pending_lock);
582 
583 	if (!list_empty(&dev->txreq_free)) {
584 		struct list_head *l = dev->txreq_free.next;
585 
586 		list_del(l);
587 		spin_unlock(&dev->rdi.pending_lock);
588 		spin_unlock_irqrestore(&qp->s_lock, flags);
589 		tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
590 	} else {
591 		if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK &&
592 		    list_empty(&priv->iowait)) {
593 			dev->n_txwait++;
594 			qp->s_flags |= RVT_S_WAIT_TX;
595 			list_add_tail(&priv->iowait, &dev->txwait);
596 		}
597 		qp->s_flags &= ~RVT_S_BUSY;
598 		spin_unlock(&dev->rdi.pending_lock);
599 		spin_unlock_irqrestore(&qp->s_lock, flags);
600 		tx = ERR_PTR(-EBUSY);
601 	}
602 	return tx;
603 }
604 
605 static inline struct qib_verbs_txreq *get_txreq(struct qib_ibdev *dev,
606 					 struct rvt_qp *qp)
607 {
608 	struct qib_verbs_txreq *tx;
609 	unsigned long flags;
610 
611 	spin_lock_irqsave(&dev->rdi.pending_lock, flags);
612 	/* assume the list non empty */
613 	if (likely(!list_empty(&dev->txreq_free))) {
614 		struct list_head *l = dev->txreq_free.next;
615 
616 		list_del(l);
617 		spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
618 		tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
619 	} else {
620 		/* call slow path to get the extra lock */
621 		spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
622 		tx =  __get_txreq(dev, qp);
623 	}
624 	return tx;
625 }
626 
627 void qib_put_txreq(struct qib_verbs_txreq *tx)
628 {
629 	struct qib_ibdev *dev;
630 	struct rvt_qp *qp;
631 	struct qib_qp_priv *priv;
632 	unsigned long flags;
633 
634 	qp = tx->qp;
635 	dev = to_idev(qp->ibqp.device);
636 
637 	if (tx->mr) {
638 		rvt_put_mr(tx->mr);
639 		tx->mr = NULL;
640 	}
641 	if (tx->txreq.flags & QIB_SDMA_TXREQ_F_FREEBUF) {
642 		tx->txreq.flags &= ~QIB_SDMA_TXREQ_F_FREEBUF;
643 		dma_unmap_single(&dd_from_dev(dev)->pcidev->dev,
644 				 tx->txreq.addr, tx->hdr_dwords << 2,
645 				 DMA_TO_DEVICE);
646 		kfree(tx->align_buf);
647 	}
648 
649 	spin_lock_irqsave(&dev->rdi.pending_lock, flags);
650 
651 	/* Put struct back on free list */
652 	list_add(&tx->txreq.list, &dev->txreq_free);
653 
654 	if (!list_empty(&dev->txwait)) {
655 		/* Wake up first QP wanting a free struct */
656 		priv = list_entry(dev->txwait.next, struct qib_qp_priv,
657 				  iowait);
658 		qp = priv->owner;
659 		list_del_init(&priv->iowait);
660 		rvt_get_qp(qp);
661 		spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
662 
663 		spin_lock_irqsave(&qp->s_lock, flags);
664 		if (qp->s_flags & RVT_S_WAIT_TX) {
665 			qp->s_flags &= ~RVT_S_WAIT_TX;
666 			qib_schedule_send(qp);
667 		}
668 		spin_unlock_irqrestore(&qp->s_lock, flags);
669 
670 		rvt_put_qp(qp);
671 	} else
672 		spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
673 }
674 
675 /*
676  * This is called when there are send DMA descriptors that might be
677  * available.
678  *
679  * This is called with ppd->sdma_lock held.
680  */
681 void qib_verbs_sdma_desc_avail(struct qib_pportdata *ppd, unsigned avail)
682 {
683 	struct rvt_qp *qp;
684 	struct qib_qp_priv *qpp, *nqpp;
685 	struct rvt_qp *qps[20];
686 	struct qib_ibdev *dev;
687 	unsigned i, n;
688 
689 	n = 0;
690 	dev = &ppd->dd->verbs_dev;
691 	spin_lock(&dev->rdi.pending_lock);
692 
693 	/* Search wait list for first QP wanting DMA descriptors. */
694 	list_for_each_entry_safe(qpp, nqpp, &dev->dmawait, iowait) {
695 		qp = qpp->owner;
696 		if (qp->port_num != ppd->port)
697 			continue;
698 		if (n == ARRAY_SIZE(qps))
699 			break;
700 		if (qpp->s_tx->txreq.sg_count > avail)
701 			break;
702 		avail -= qpp->s_tx->txreq.sg_count;
703 		list_del_init(&qpp->iowait);
704 		rvt_get_qp(qp);
705 		qps[n++] = qp;
706 	}
707 
708 	spin_unlock(&dev->rdi.pending_lock);
709 
710 	for (i = 0; i < n; i++) {
711 		qp = qps[i];
712 		spin_lock(&qp->s_lock);
713 		if (qp->s_flags & RVT_S_WAIT_DMA_DESC) {
714 			qp->s_flags &= ~RVT_S_WAIT_DMA_DESC;
715 			qib_schedule_send(qp);
716 		}
717 		spin_unlock(&qp->s_lock);
718 		rvt_put_qp(qp);
719 	}
720 }
721 
722 /*
723  * This is called with ppd->sdma_lock held.
724  */
725 static void sdma_complete(struct qib_sdma_txreq *cookie, int status)
726 {
727 	struct qib_verbs_txreq *tx =
728 		container_of(cookie, struct qib_verbs_txreq, txreq);
729 	struct rvt_qp *qp = tx->qp;
730 	struct qib_qp_priv *priv = qp->priv;
731 
732 	spin_lock(&qp->s_lock);
733 	if (tx->wqe)
734 		rvt_send_complete(qp, tx->wqe, IB_WC_SUCCESS);
735 	else if (qp->ibqp.qp_type == IB_QPT_RC) {
736 		struct ib_header *hdr;
737 
738 		if (tx->txreq.flags & QIB_SDMA_TXREQ_F_FREEBUF)
739 			hdr = &tx->align_buf->hdr;
740 		else {
741 			struct qib_ibdev *dev = to_idev(qp->ibqp.device);
742 
743 			hdr = &dev->pio_hdrs[tx->hdr_inx].hdr;
744 		}
745 		qib_rc_send_complete(qp, hdr);
746 	}
747 	if (atomic_dec_and_test(&priv->s_dma_busy)) {
748 		if (qp->state == IB_QPS_RESET)
749 			wake_up(&priv->wait_dma);
750 		else if (qp->s_flags & RVT_S_WAIT_DMA) {
751 			qp->s_flags &= ~RVT_S_WAIT_DMA;
752 			qib_schedule_send(qp);
753 		}
754 	}
755 	spin_unlock(&qp->s_lock);
756 
757 	qib_put_txreq(tx);
758 }
759 
760 static int wait_kmem(struct qib_ibdev *dev, struct rvt_qp *qp)
761 {
762 	struct qib_qp_priv *priv = qp->priv;
763 	unsigned long flags;
764 	int ret = 0;
765 
766 	spin_lock_irqsave(&qp->s_lock, flags);
767 	if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
768 		spin_lock(&dev->rdi.pending_lock);
769 		if (list_empty(&priv->iowait)) {
770 			if (list_empty(&dev->memwait))
771 				mod_timer(&dev->mem_timer, jiffies + 1);
772 			qp->s_flags |= RVT_S_WAIT_KMEM;
773 			list_add_tail(&priv->iowait, &dev->memwait);
774 		}
775 		spin_unlock(&dev->rdi.pending_lock);
776 		qp->s_flags &= ~RVT_S_BUSY;
777 		ret = -EBUSY;
778 	}
779 	spin_unlock_irqrestore(&qp->s_lock, flags);
780 
781 	return ret;
782 }
783 
784 static int qib_verbs_send_dma(struct rvt_qp *qp, struct ib_header *hdr,
785 			      u32 hdrwords, struct rvt_sge_state *ss, u32 len,
786 			      u32 plen, u32 dwords)
787 {
788 	struct qib_qp_priv *priv = qp->priv;
789 	struct qib_ibdev *dev = to_idev(qp->ibqp.device);
790 	struct qib_devdata *dd = dd_from_dev(dev);
791 	struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
792 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
793 	struct qib_verbs_txreq *tx;
794 	struct qib_pio_header *phdr;
795 	u32 control;
796 	u32 ndesc;
797 	int ret;
798 
799 	tx = priv->s_tx;
800 	if (tx) {
801 		priv->s_tx = NULL;
802 		/* resend previously constructed packet */
803 		ret = qib_sdma_verbs_send(ppd, tx->ss, tx->dwords, tx);
804 		goto bail;
805 	}
806 
807 	tx = get_txreq(dev, qp);
808 	if (IS_ERR(tx))
809 		goto bail_tx;
810 
811 	control = dd->f_setpbc_control(ppd, plen, qp->s_srate,
812 				       be16_to_cpu(hdr->lrh[0]) >> 12);
813 	tx->qp = qp;
814 	tx->wqe = qp->s_wqe;
815 	tx->mr = qp->s_rdma_mr;
816 	if (qp->s_rdma_mr)
817 		qp->s_rdma_mr = NULL;
818 	tx->txreq.callback = sdma_complete;
819 	if (dd->flags & QIB_HAS_SDMA_TIMEOUT)
820 		tx->txreq.flags = QIB_SDMA_TXREQ_F_HEADTOHOST;
821 	else
822 		tx->txreq.flags = QIB_SDMA_TXREQ_F_INTREQ;
823 	if (plen + 1 > dd->piosize2kmax_dwords)
824 		tx->txreq.flags |= QIB_SDMA_TXREQ_F_USELARGEBUF;
825 
826 	if (len) {
827 		/*
828 		 * Don't try to DMA if it takes more descriptors than
829 		 * the queue holds.
830 		 */
831 		ndesc = qib_count_sge(ss, len);
832 		if (ndesc >= ppd->sdma_descq_cnt)
833 			ndesc = 0;
834 	} else
835 		ndesc = 1;
836 	if (ndesc) {
837 		phdr = &dev->pio_hdrs[tx->hdr_inx];
838 		phdr->pbc[0] = cpu_to_le32(plen);
839 		phdr->pbc[1] = cpu_to_le32(control);
840 		memcpy(&phdr->hdr, hdr, hdrwords << 2);
841 		tx->txreq.flags |= QIB_SDMA_TXREQ_F_FREEDESC;
842 		tx->txreq.sg_count = ndesc;
843 		tx->txreq.addr = dev->pio_hdrs_phys +
844 			tx->hdr_inx * sizeof(struct qib_pio_header);
845 		tx->hdr_dwords = hdrwords + 2; /* add PBC length */
846 		ret = qib_sdma_verbs_send(ppd, ss, dwords, tx);
847 		goto bail;
848 	}
849 
850 	/* Allocate a buffer and copy the header and payload to it. */
851 	tx->hdr_dwords = plen + 1;
852 	phdr = kmalloc(tx->hdr_dwords << 2, GFP_ATOMIC);
853 	if (!phdr)
854 		goto err_tx;
855 	phdr->pbc[0] = cpu_to_le32(plen);
856 	phdr->pbc[1] = cpu_to_le32(control);
857 	memcpy(&phdr->hdr, hdr, hdrwords << 2);
858 	qib_copy_from_sge((u32 *) &phdr->hdr + hdrwords, ss, len);
859 
860 	tx->txreq.addr = dma_map_single(&dd->pcidev->dev, phdr,
861 					tx->hdr_dwords << 2, DMA_TO_DEVICE);
862 	if (dma_mapping_error(&dd->pcidev->dev, tx->txreq.addr))
863 		goto map_err;
864 	tx->align_buf = phdr;
865 	tx->txreq.flags |= QIB_SDMA_TXREQ_F_FREEBUF;
866 	tx->txreq.sg_count = 1;
867 	ret = qib_sdma_verbs_send(ppd, NULL, 0, tx);
868 	goto unaligned;
869 
870 map_err:
871 	kfree(phdr);
872 err_tx:
873 	qib_put_txreq(tx);
874 	ret = wait_kmem(dev, qp);
875 unaligned:
876 	ibp->rvp.n_unaligned++;
877 bail:
878 	return ret;
879 bail_tx:
880 	ret = PTR_ERR(tx);
881 	goto bail;
882 }
883 
884 /*
885  * If we are now in the error state, return zero to flush the
886  * send work request.
887  */
888 static int no_bufs_available(struct rvt_qp *qp)
889 {
890 	struct qib_qp_priv *priv = qp->priv;
891 	struct qib_ibdev *dev = to_idev(qp->ibqp.device);
892 	struct qib_devdata *dd;
893 	unsigned long flags;
894 	int ret = 0;
895 
896 	/*
897 	 * Note that as soon as want_buffer() is called and
898 	 * possibly before it returns, qib_ib_piobufavail()
899 	 * could be called. Therefore, put QP on the I/O wait list before
900 	 * enabling the PIO avail interrupt.
901 	 */
902 	spin_lock_irqsave(&qp->s_lock, flags);
903 	if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
904 		spin_lock(&dev->rdi.pending_lock);
905 		if (list_empty(&priv->iowait)) {
906 			dev->n_piowait++;
907 			qp->s_flags |= RVT_S_WAIT_PIO;
908 			list_add_tail(&priv->iowait, &dev->piowait);
909 			dd = dd_from_dev(dev);
910 			dd->f_wantpiobuf_intr(dd, 1);
911 		}
912 		spin_unlock(&dev->rdi.pending_lock);
913 		qp->s_flags &= ~RVT_S_BUSY;
914 		ret = -EBUSY;
915 	}
916 	spin_unlock_irqrestore(&qp->s_lock, flags);
917 	return ret;
918 }
919 
920 static int qib_verbs_send_pio(struct rvt_qp *qp, struct ib_header *ibhdr,
921 			      u32 hdrwords, struct rvt_sge_state *ss, u32 len,
922 			      u32 plen, u32 dwords)
923 {
924 	struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
925 	struct qib_pportdata *ppd = dd->pport + qp->port_num - 1;
926 	u32 *hdr = (u32 *) ibhdr;
927 	u32 __iomem *piobuf_orig;
928 	u32 __iomem *piobuf;
929 	u64 pbc;
930 	unsigned long flags;
931 	unsigned flush_wc;
932 	u32 control;
933 	u32 pbufn;
934 
935 	control = dd->f_setpbc_control(ppd, plen, qp->s_srate,
936 		be16_to_cpu(ibhdr->lrh[0]) >> 12);
937 	pbc = ((u64) control << 32) | plen;
938 	piobuf = dd->f_getsendbuf(ppd, pbc, &pbufn);
939 	if (unlikely(piobuf == NULL))
940 		return no_bufs_available(qp);
941 
942 	/*
943 	 * Write the pbc.
944 	 * We have to flush after the PBC for correctness on some cpus
945 	 * or WC buffer can be written out of order.
946 	 */
947 	writeq(pbc, piobuf);
948 	piobuf_orig = piobuf;
949 	piobuf += 2;
950 
951 	flush_wc = dd->flags & QIB_PIO_FLUSH_WC;
952 	if (len == 0) {
953 		/*
954 		 * If there is just the header portion, must flush before
955 		 * writing last word of header for correctness, and after
956 		 * the last header word (trigger word).
957 		 */
958 		if (flush_wc) {
959 			qib_flush_wc();
960 			qib_pio_copy(piobuf, hdr, hdrwords - 1);
961 			qib_flush_wc();
962 			__raw_writel(hdr[hdrwords - 1], piobuf + hdrwords - 1);
963 			qib_flush_wc();
964 		} else
965 			qib_pio_copy(piobuf, hdr, hdrwords);
966 		goto done;
967 	}
968 
969 	if (flush_wc)
970 		qib_flush_wc();
971 	qib_pio_copy(piobuf, hdr, hdrwords);
972 	piobuf += hdrwords;
973 
974 	/* The common case is aligned and contained in one segment. */
975 	if (likely(ss->num_sge == 1 && len <= ss->sge.length &&
976 		   !((unsigned long)ss->sge.vaddr & (sizeof(u32) - 1)))) {
977 		u32 *addr = (u32 *) ss->sge.vaddr;
978 
979 		/* Update address before sending packet. */
980 		rvt_update_sge(ss, len, false);
981 		if (flush_wc) {
982 			qib_pio_copy(piobuf, addr, dwords - 1);
983 			/* must flush early everything before trigger word */
984 			qib_flush_wc();
985 			__raw_writel(addr[dwords - 1], piobuf + dwords - 1);
986 			/* be sure trigger word is written */
987 			qib_flush_wc();
988 		} else
989 			qib_pio_copy(piobuf, addr, dwords);
990 		goto done;
991 	}
992 	copy_io(piobuf, ss, len, flush_wc);
993 done:
994 	if (dd->flags & QIB_USE_SPCL_TRIG) {
995 		u32 spcl_off = (pbufn >= dd->piobcnt2k) ? 2047 : 1023;
996 
997 		qib_flush_wc();
998 		__raw_writel(0xaebecede, piobuf_orig + spcl_off);
999 	}
1000 	qib_sendbuf_done(dd, pbufn);
1001 	if (qp->s_rdma_mr) {
1002 		rvt_put_mr(qp->s_rdma_mr);
1003 		qp->s_rdma_mr = NULL;
1004 	}
1005 	if (qp->s_wqe) {
1006 		spin_lock_irqsave(&qp->s_lock, flags);
1007 		rvt_send_complete(qp, qp->s_wqe, IB_WC_SUCCESS);
1008 		spin_unlock_irqrestore(&qp->s_lock, flags);
1009 	} else if (qp->ibqp.qp_type == IB_QPT_RC) {
1010 		spin_lock_irqsave(&qp->s_lock, flags);
1011 		qib_rc_send_complete(qp, ibhdr);
1012 		spin_unlock_irqrestore(&qp->s_lock, flags);
1013 	}
1014 	return 0;
1015 }
1016 
1017 /**
1018  * qib_verbs_send - send a packet
1019  * @qp: the QP to send on
1020  * @hdr: the packet header
1021  * @hdrwords: the number of 32-bit words in the header
1022  * @ss: the SGE to send
1023  * @len: the length of the packet in bytes
1024  *
1025  * Return zero if packet is sent or queued OK.
1026  * Return non-zero and clear qp->s_flags RVT_S_BUSY otherwise.
1027  */
1028 int qib_verbs_send(struct rvt_qp *qp, struct ib_header *hdr,
1029 		   u32 hdrwords, struct rvt_sge_state *ss, u32 len)
1030 {
1031 	struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
1032 	u32 plen;
1033 	int ret;
1034 	u32 dwords = (len + 3) >> 2;
1035 
1036 	/*
1037 	 * Calculate the send buffer trigger address.
1038 	 * The +1 counts for the pbc control dword following the pbc length.
1039 	 */
1040 	plen = hdrwords + dwords + 1;
1041 
1042 	/*
1043 	 * VL15 packets (IB_QPT_SMI) will always use PIO, so we
1044 	 * can defer SDMA restart until link goes ACTIVE without
1045 	 * worrying about just how we got there.
1046 	 */
1047 	if (qp->ibqp.qp_type == IB_QPT_SMI ||
1048 	    !(dd->flags & QIB_HAS_SEND_DMA))
1049 		ret = qib_verbs_send_pio(qp, hdr, hdrwords, ss, len,
1050 					 plen, dwords);
1051 	else
1052 		ret = qib_verbs_send_dma(qp, hdr, hdrwords, ss, len,
1053 					 plen, dwords);
1054 
1055 	return ret;
1056 }
1057 
1058 int qib_snapshot_counters(struct qib_pportdata *ppd, u64 *swords,
1059 			  u64 *rwords, u64 *spkts, u64 *rpkts,
1060 			  u64 *xmit_wait)
1061 {
1062 	int ret;
1063 	struct qib_devdata *dd = ppd->dd;
1064 
1065 	if (!(dd->flags & QIB_PRESENT)) {
1066 		/* no hardware, freeze, etc. */
1067 		ret = -EINVAL;
1068 		goto bail;
1069 	}
1070 	*swords = dd->f_portcntr(ppd, QIBPORTCNTR_WORDSEND);
1071 	*rwords = dd->f_portcntr(ppd, QIBPORTCNTR_WORDRCV);
1072 	*spkts = dd->f_portcntr(ppd, QIBPORTCNTR_PKTSEND);
1073 	*rpkts = dd->f_portcntr(ppd, QIBPORTCNTR_PKTRCV);
1074 	*xmit_wait = dd->f_portcntr(ppd, QIBPORTCNTR_SENDSTALL);
1075 
1076 	ret = 0;
1077 
1078 bail:
1079 	return ret;
1080 }
1081 
1082 /**
1083  * qib_get_counters - get various chip counters
1084  * @dd: the qlogic_ib device
1085  * @cntrs: counters are placed here
1086  *
1087  * Return the counters needed by recv_pma_get_portcounters().
1088  */
1089 int qib_get_counters(struct qib_pportdata *ppd,
1090 		     struct qib_verbs_counters *cntrs)
1091 {
1092 	int ret;
1093 
1094 	if (!(ppd->dd->flags & QIB_PRESENT)) {
1095 		/* no hardware, freeze, etc. */
1096 		ret = -EINVAL;
1097 		goto bail;
1098 	}
1099 	cntrs->symbol_error_counter =
1100 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBSYMBOLERR);
1101 	cntrs->link_error_recovery_counter =
1102 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBLINKERRRECOV);
1103 	/*
1104 	 * The link downed counter counts when the other side downs the
1105 	 * connection.  We add in the number of times we downed the link
1106 	 * due to local link integrity errors to compensate.
1107 	 */
1108 	cntrs->link_downed_counter =
1109 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBLINKDOWN);
1110 	cntrs->port_rcv_errors =
1111 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXDROPPKT) +
1112 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RCVOVFL) +
1113 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERR_RLEN) +
1114 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_INVALIDRLEN) +
1115 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRLINK) +
1116 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRICRC) +
1117 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRVCRC) +
1118 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRLPCRC) +
1119 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_BADFORMAT);
1120 	cntrs->port_rcv_errors +=
1121 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXLOCALPHYERR);
1122 	cntrs->port_rcv_errors +=
1123 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXVLERR);
1124 	cntrs->port_rcv_remphys_errors =
1125 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RCVEBP);
1126 	cntrs->port_xmit_discards =
1127 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_UNSUPVL);
1128 	cntrs->port_xmit_data = ppd->dd->f_portcntr(ppd,
1129 			QIBPORTCNTR_WORDSEND);
1130 	cntrs->port_rcv_data = ppd->dd->f_portcntr(ppd,
1131 			QIBPORTCNTR_WORDRCV);
1132 	cntrs->port_xmit_packets = ppd->dd->f_portcntr(ppd,
1133 			QIBPORTCNTR_PKTSEND);
1134 	cntrs->port_rcv_packets = ppd->dd->f_portcntr(ppd,
1135 			QIBPORTCNTR_PKTRCV);
1136 	cntrs->local_link_integrity_errors =
1137 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_LLI);
1138 	cntrs->excessive_buffer_overrun_errors =
1139 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_EXCESSBUFOVFL);
1140 	cntrs->vl15_dropped =
1141 		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_VL15PKTDROP);
1142 
1143 	ret = 0;
1144 
1145 bail:
1146 	return ret;
1147 }
1148 
1149 /**
1150  * qib_ib_piobufavail - callback when a PIO buffer is available
1151  * @dd: the device pointer
1152  *
1153  * This is called from qib_intr() at interrupt level when a PIO buffer is
1154  * available after qib_verbs_send() returned an error that no buffers were
1155  * available. Disable the interrupt if there are no more QPs waiting.
1156  */
1157 void qib_ib_piobufavail(struct qib_devdata *dd)
1158 {
1159 	struct qib_ibdev *dev = &dd->verbs_dev;
1160 	struct list_head *list;
1161 	struct rvt_qp *qps[5];
1162 	struct rvt_qp *qp;
1163 	unsigned long flags;
1164 	unsigned i, n;
1165 	struct qib_qp_priv *priv;
1166 
1167 	list = &dev->piowait;
1168 	n = 0;
1169 
1170 	/*
1171 	 * Note: checking that the piowait list is empty and clearing
1172 	 * the buffer available interrupt needs to be atomic or we
1173 	 * could end up with QPs on the wait list with the interrupt
1174 	 * disabled.
1175 	 */
1176 	spin_lock_irqsave(&dev->rdi.pending_lock, flags);
1177 	while (!list_empty(list)) {
1178 		if (n == ARRAY_SIZE(qps))
1179 			goto full;
1180 		priv = list_entry(list->next, struct qib_qp_priv, iowait);
1181 		qp = priv->owner;
1182 		list_del_init(&priv->iowait);
1183 		rvt_get_qp(qp);
1184 		qps[n++] = qp;
1185 	}
1186 	dd->f_wantpiobuf_intr(dd, 0);
1187 full:
1188 	spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
1189 
1190 	for (i = 0; i < n; i++) {
1191 		qp = qps[i];
1192 
1193 		spin_lock_irqsave(&qp->s_lock, flags);
1194 		if (qp->s_flags & RVT_S_WAIT_PIO) {
1195 			qp->s_flags &= ~RVT_S_WAIT_PIO;
1196 			qib_schedule_send(qp);
1197 		}
1198 		spin_unlock_irqrestore(&qp->s_lock, flags);
1199 
1200 		/* Notify qib_destroy_qp() if it is waiting. */
1201 		rvt_put_qp(qp);
1202 	}
1203 }
1204 
1205 static int qib_query_port(struct rvt_dev_info *rdi, u8 port_num,
1206 			  struct ib_port_attr *props)
1207 {
1208 	struct qib_ibdev *ibdev = container_of(rdi, struct qib_ibdev, rdi);
1209 	struct qib_devdata *dd = dd_from_dev(ibdev);
1210 	struct qib_pportdata *ppd = &dd->pport[port_num - 1];
1211 	enum ib_mtu mtu;
1212 	u16 lid = ppd->lid;
1213 
1214 	/* props being zeroed by the caller, avoid zeroing it here */
1215 	props->lid = lid ? lid : be16_to_cpu(IB_LID_PERMISSIVE);
1216 	props->lmc = ppd->lmc;
1217 	props->state = dd->f_iblink_state(ppd->lastibcstat);
1218 	props->phys_state = dd->f_ibphys_portstate(ppd->lastibcstat);
1219 	props->gid_tbl_len = QIB_GUIDS_PER_PORT;
1220 	props->active_width = ppd->link_width_active;
1221 	/* See rate_show() */
1222 	props->active_speed = ppd->link_speed_active;
1223 	props->max_vl_num = qib_num_vls(ppd->vls_supported);
1224 
1225 	props->max_mtu = qib_ibmtu ? qib_ibmtu : IB_MTU_4096;
1226 	switch (ppd->ibmtu) {
1227 	case 4096:
1228 		mtu = IB_MTU_4096;
1229 		break;
1230 	case 2048:
1231 		mtu = IB_MTU_2048;
1232 		break;
1233 	case 1024:
1234 		mtu = IB_MTU_1024;
1235 		break;
1236 	case 512:
1237 		mtu = IB_MTU_512;
1238 		break;
1239 	case 256:
1240 		mtu = IB_MTU_256;
1241 		break;
1242 	default:
1243 		mtu = IB_MTU_2048;
1244 	}
1245 	props->active_mtu = mtu;
1246 
1247 	return 0;
1248 }
1249 
1250 static int qib_modify_device(struct ib_device *device,
1251 			     int device_modify_mask,
1252 			     struct ib_device_modify *device_modify)
1253 {
1254 	struct qib_devdata *dd = dd_from_ibdev(device);
1255 	unsigned i;
1256 	int ret;
1257 
1258 	if (device_modify_mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
1259 				   IB_DEVICE_MODIFY_NODE_DESC)) {
1260 		ret = -EOPNOTSUPP;
1261 		goto bail;
1262 	}
1263 
1264 	if (device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC) {
1265 		memcpy(device->node_desc, device_modify->node_desc,
1266 		       IB_DEVICE_NODE_DESC_MAX);
1267 		for (i = 0; i < dd->num_pports; i++) {
1268 			struct qib_ibport *ibp = &dd->pport[i].ibport_data;
1269 
1270 			qib_node_desc_chg(ibp);
1271 		}
1272 	}
1273 
1274 	if (device_modify_mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID) {
1275 		ib_qib_sys_image_guid =
1276 			cpu_to_be64(device_modify->sys_image_guid);
1277 		for (i = 0; i < dd->num_pports; i++) {
1278 			struct qib_ibport *ibp = &dd->pport[i].ibport_data;
1279 
1280 			qib_sys_guid_chg(ibp);
1281 		}
1282 	}
1283 
1284 	ret = 0;
1285 
1286 bail:
1287 	return ret;
1288 }
1289 
1290 static int qib_shut_down_port(struct rvt_dev_info *rdi, u8 port_num)
1291 {
1292 	struct qib_ibdev *ibdev = container_of(rdi, struct qib_ibdev, rdi);
1293 	struct qib_devdata *dd = dd_from_dev(ibdev);
1294 	struct qib_pportdata *ppd = &dd->pport[port_num - 1];
1295 
1296 	qib_set_linkstate(ppd, QIB_IB_LINKDOWN);
1297 
1298 	return 0;
1299 }
1300 
1301 static int qib_get_guid_be(struct rvt_dev_info *rdi, struct rvt_ibport *rvp,
1302 			   int guid_index, __be64 *guid)
1303 {
1304 	struct qib_ibport *ibp = container_of(rvp, struct qib_ibport, rvp);
1305 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1306 
1307 	if (guid_index == 0)
1308 		*guid = ppd->guid;
1309 	else if (guid_index < QIB_GUIDS_PER_PORT)
1310 		*guid = ibp->guids[guid_index - 1];
1311 	else
1312 		return -EINVAL;
1313 
1314 	return 0;
1315 }
1316 
1317 int qib_check_ah(struct ib_device *ibdev, struct rdma_ah_attr *ah_attr)
1318 {
1319 	if (rdma_ah_get_sl(ah_attr) > 15)
1320 		return -EINVAL;
1321 
1322 	if (rdma_ah_get_dlid(ah_attr) == 0)
1323 		return -EINVAL;
1324 	if (rdma_ah_get_dlid(ah_attr) >=
1325 		be16_to_cpu(IB_MULTICAST_LID_BASE) &&
1326 	    rdma_ah_get_dlid(ah_attr) !=
1327 		be16_to_cpu(IB_LID_PERMISSIVE) &&
1328 	    !(rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH))
1329 		return -EINVAL;
1330 
1331 	return 0;
1332 }
1333 
1334 static void qib_notify_new_ah(struct ib_device *ibdev,
1335 			      struct rdma_ah_attr *ah_attr,
1336 			      struct rvt_ah *ah)
1337 {
1338 	struct qib_ibport *ibp;
1339 	struct qib_pportdata *ppd;
1340 
1341 	/*
1342 	 * Do not trust reading anything from rvt_ah at this point as it is not
1343 	 * done being setup. We can however modify things which we need to set.
1344 	 */
1345 
1346 	ibp = to_iport(ibdev, rdma_ah_get_port_num(ah_attr));
1347 	ppd = ppd_from_ibp(ibp);
1348 	ah->vl = ibp->sl_to_vl[rdma_ah_get_sl(&ah->attr)];
1349 	ah->log_pmtu = ilog2(ppd->ibmtu);
1350 }
1351 
1352 struct ib_ah *qib_create_qp0_ah(struct qib_ibport *ibp, u16 dlid)
1353 {
1354 	struct rdma_ah_attr attr;
1355 	struct ib_ah *ah = ERR_PTR(-EINVAL);
1356 	struct rvt_qp *qp0;
1357 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1358 	struct qib_devdata *dd = dd_from_ppd(ppd);
1359 	u8 port_num = ppd->port;
1360 
1361 	memset(&attr, 0, sizeof(attr));
1362 	attr.type = rdma_ah_find_type(&dd->verbs_dev.rdi.ibdev, port_num);
1363 	rdma_ah_set_dlid(&attr, dlid);
1364 	rdma_ah_set_port_num(&attr, port_num);
1365 	rcu_read_lock();
1366 	qp0 = rcu_dereference(ibp->rvp.qp[0]);
1367 	if (qp0)
1368 		ah = rdma_create_ah(qp0->ibqp.pd, &attr);
1369 	rcu_read_unlock();
1370 	return ah;
1371 }
1372 
1373 /**
1374  * qib_get_npkeys - return the size of the PKEY table for context 0
1375  * @dd: the qlogic_ib device
1376  */
1377 unsigned qib_get_npkeys(struct qib_devdata *dd)
1378 {
1379 	return ARRAY_SIZE(dd->rcd[0]->pkeys);
1380 }
1381 
1382 /*
1383  * Return the indexed PKEY from the port PKEY table.
1384  * No need to validate rcd[ctxt]; the port is setup if we are here.
1385  */
1386 unsigned qib_get_pkey(struct qib_ibport *ibp, unsigned index)
1387 {
1388 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1389 	struct qib_devdata *dd = ppd->dd;
1390 	unsigned ctxt = ppd->hw_pidx;
1391 	unsigned ret;
1392 
1393 	/* dd->rcd null if mini_init or some init failures */
1394 	if (!dd->rcd || index >= ARRAY_SIZE(dd->rcd[ctxt]->pkeys))
1395 		ret = 0;
1396 	else
1397 		ret = dd->rcd[ctxt]->pkeys[index];
1398 
1399 	return ret;
1400 }
1401 
1402 static void init_ibport(struct qib_pportdata *ppd)
1403 {
1404 	struct qib_verbs_counters cntrs;
1405 	struct qib_ibport *ibp = &ppd->ibport_data;
1406 
1407 	spin_lock_init(&ibp->rvp.lock);
1408 	/* Set the prefix to the default value (see ch. 4.1.1) */
1409 	ibp->rvp.gid_prefix = IB_DEFAULT_GID_PREFIX;
1410 	ibp->rvp.sm_lid = be16_to_cpu(IB_LID_PERMISSIVE);
1411 	ibp->rvp.port_cap_flags = IB_PORT_SYS_IMAGE_GUID_SUP |
1412 		IB_PORT_CLIENT_REG_SUP | IB_PORT_SL_MAP_SUP |
1413 		IB_PORT_TRAP_SUP | IB_PORT_AUTO_MIGR_SUP |
1414 		IB_PORT_DR_NOTICE_SUP | IB_PORT_CAP_MASK_NOTICE_SUP |
1415 		IB_PORT_OTHER_LOCAL_CHANGES_SUP;
1416 	if (ppd->dd->flags & QIB_HAS_LINK_LATENCY)
1417 		ibp->rvp.port_cap_flags |= IB_PORT_LINK_LATENCY_SUP;
1418 	ibp->rvp.pma_counter_select[0] = IB_PMA_PORT_XMIT_DATA;
1419 	ibp->rvp.pma_counter_select[1] = IB_PMA_PORT_RCV_DATA;
1420 	ibp->rvp.pma_counter_select[2] = IB_PMA_PORT_XMIT_PKTS;
1421 	ibp->rvp.pma_counter_select[3] = IB_PMA_PORT_RCV_PKTS;
1422 	ibp->rvp.pma_counter_select[4] = IB_PMA_PORT_XMIT_WAIT;
1423 
1424 	/* Snapshot current HW counters to "clear" them. */
1425 	qib_get_counters(ppd, &cntrs);
1426 	ibp->z_symbol_error_counter = cntrs.symbol_error_counter;
1427 	ibp->z_link_error_recovery_counter =
1428 		cntrs.link_error_recovery_counter;
1429 	ibp->z_link_downed_counter = cntrs.link_downed_counter;
1430 	ibp->z_port_rcv_errors = cntrs.port_rcv_errors;
1431 	ibp->z_port_rcv_remphys_errors = cntrs.port_rcv_remphys_errors;
1432 	ibp->z_port_xmit_discards = cntrs.port_xmit_discards;
1433 	ibp->z_port_xmit_data = cntrs.port_xmit_data;
1434 	ibp->z_port_rcv_data = cntrs.port_rcv_data;
1435 	ibp->z_port_xmit_packets = cntrs.port_xmit_packets;
1436 	ibp->z_port_rcv_packets = cntrs.port_rcv_packets;
1437 	ibp->z_local_link_integrity_errors =
1438 		cntrs.local_link_integrity_errors;
1439 	ibp->z_excessive_buffer_overrun_errors =
1440 		cntrs.excessive_buffer_overrun_errors;
1441 	ibp->z_vl15_dropped = cntrs.vl15_dropped;
1442 	RCU_INIT_POINTER(ibp->rvp.qp[0], NULL);
1443 	RCU_INIT_POINTER(ibp->rvp.qp[1], NULL);
1444 }
1445 
1446 /**
1447  * qib_fill_device_attr - Fill in rvt dev info device attributes.
1448  * @dd: the device data structure
1449  */
1450 static void qib_fill_device_attr(struct qib_devdata *dd)
1451 {
1452 	struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
1453 
1454 	memset(&rdi->dparms.props, 0, sizeof(rdi->dparms.props));
1455 
1456 	rdi->dparms.props.max_pd = ib_qib_max_pds;
1457 	rdi->dparms.props.max_ah = ib_qib_max_ahs;
1458 	rdi->dparms.props.device_cap_flags = IB_DEVICE_BAD_PKEY_CNTR |
1459 		IB_DEVICE_BAD_QKEY_CNTR | IB_DEVICE_SHUTDOWN_PORT |
1460 		IB_DEVICE_SYS_IMAGE_GUID | IB_DEVICE_RC_RNR_NAK_GEN |
1461 		IB_DEVICE_PORT_ACTIVE_EVENT | IB_DEVICE_SRQ_RESIZE;
1462 	rdi->dparms.props.page_size_cap = PAGE_SIZE;
1463 	rdi->dparms.props.vendor_id =
1464 		QIB_SRC_OUI_1 << 16 | QIB_SRC_OUI_2 << 8 | QIB_SRC_OUI_3;
1465 	rdi->dparms.props.vendor_part_id = dd->deviceid;
1466 	rdi->dparms.props.hw_ver = dd->minrev;
1467 	rdi->dparms.props.sys_image_guid = ib_qib_sys_image_guid;
1468 	rdi->dparms.props.max_mr_size = ~0ULL;
1469 	rdi->dparms.props.max_qp = ib_qib_max_qps;
1470 	rdi->dparms.props.max_qp_wr = ib_qib_max_qp_wrs;
1471 	rdi->dparms.props.max_send_sge = ib_qib_max_sges;
1472 	rdi->dparms.props.max_recv_sge = ib_qib_max_sges;
1473 	rdi->dparms.props.max_sge_rd = ib_qib_max_sges;
1474 	rdi->dparms.props.max_cq = ib_qib_max_cqs;
1475 	rdi->dparms.props.max_cqe = ib_qib_max_cqes;
1476 	rdi->dparms.props.max_ah = ib_qib_max_ahs;
1477 	rdi->dparms.props.max_mr = rdi->lkey_table.max;
1478 	rdi->dparms.props.max_fmr = rdi->lkey_table.max;
1479 	rdi->dparms.props.max_map_per_fmr = 32767;
1480 	rdi->dparms.props.max_qp_rd_atom = QIB_MAX_RDMA_ATOMIC;
1481 	rdi->dparms.props.max_qp_init_rd_atom = 255;
1482 	rdi->dparms.props.max_srq = ib_qib_max_srqs;
1483 	rdi->dparms.props.max_srq_wr = ib_qib_max_srq_wrs;
1484 	rdi->dparms.props.max_srq_sge = ib_qib_max_srq_sges;
1485 	rdi->dparms.props.atomic_cap = IB_ATOMIC_GLOB;
1486 	rdi->dparms.props.max_pkeys = qib_get_npkeys(dd);
1487 	rdi->dparms.props.max_mcast_grp = ib_qib_max_mcast_grps;
1488 	rdi->dparms.props.max_mcast_qp_attach = ib_qib_max_mcast_qp_attached;
1489 	rdi->dparms.props.max_total_mcast_qp_attach =
1490 					rdi->dparms.props.max_mcast_qp_attach *
1491 					rdi->dparms.props.max_mcast_grp;
1492 	/* post send table */
1493 	dd->verbs_dev.rdi.post_parms = qib_post_parms;
1494 
1495 	/* opcode translation table */
1496 	dd->verbs_dev.rdi.wc_opcode = ib_qib_wc_opcode;
1497 }
1498 
1499 /**
1500  * qib_register_ib_device - register our device with the infiniband core
1501  * @dd: the device data structure
1502  * Return the allocated qib_ibdev pointer or NULL on error.
1503  */
1504 int qib_register_ib_device(struct qib_devdata *dd)
1505 {
1506 	struct qib_ibdev *dev = &dd->verbs_dev;
1507 	struct ib_device *ibdev = &dev->rdi.ibdev;
1508 	struct qib_pportdata *ppd = dd->pport;
1509 	unsigned i, ctxt;
1510 	int ret;
1511 
1512 	get_random_bytes(&dev->qp_rnd, sizeof(dev->qp_rnd));
1513 	for (i = 0; i < dd->num_pports; i++)
1514 		init_ibport(ppd + i);
1515 
1516 	/* Only need to initialize non-zero fields. */
1517 	timer_setup(&dev->mem_timer, mem_timer, 0);
1518 
1519 	INIT_LIST_HEAD(&dev->piowait);
1520 	INIT_LIST_HEAD(&dev->dmawait);
1521 	INIT_LIST_HEAD(&dev->txwait);
1522 	INIT_LIST_HEAD(&dev->memwait);
1523 	INIT_LIST_HEAD(&dev->txreq_free);
1524 
1525 	if (ppd->sdma_descq_cnt) {
1526 		dev->pio_hdrs = dma_alloc_coherent(&dd->pcidev->dev,
1527 						ppd->sdma_descq_cnt *
1528 						sizeof(struct qib_pio_header),
1529 						&dev->pio_hdrs_phys,
1530 						GFP_KERNEL);
1531 		if (!dev->pio_hdrs) {
1532 			ret = -ENOMEM;
1533 			goto err_hdrs;
1534 		}
1535 	}
1536 
1537 	for (i = 0; i < ppd->sdma_descq_cnt; i++) {
1538 		struct qib_verbs_txreq *tx;
1539 
1540 		tx = kzalloc(sizeof(*tx), GFP_KERNEL);
1541 		if (!tx) {
1542 			ret = -ENOMEM;
1543 			goto err_tx;
1544 		}
1545 		tx->hdr_inx = i;
1546 		list_add(&tx->txreq.list, &dev->txreq_free);
1547 	}
1548 
1549 	/*
1550 	 * The system image GUID is supposed to be the same for all
1551 	 * IB HCAs in a single system but since there can be other
1552 	 * device types in the system, we can't be sure this is unique.
1553 	 */
1554 	if (!ib_qib_sys_image_guid)
1555 		ib_qib_sys_image_guid = ppd->guid;
1556 
1557 	ibdev->owner = THIS_MODULE;
1558 	ibdev->node_guid = ppd->guid;
1559 	ibdev->phys_port_cnt = dd->num_pports;
1560 	ibdev->dev.parent = &dd->pcidev->dev;
1561 	ibdev->modify_device = qib_modify_device;
1562 	ibdev->process_mad = qib_process_mad;
1563 
1564 	snprintf(ibdev->node_desc, sizeof(ibdev->node_desc),
1565 		 "Intel Infiniband HCA %s", init_utsname()->nodename);
1566 
1567 	/*
1568 	 * Fill in rvt info object.
1569 	 */
1570 	dd->verbs_dev.rdi.driver_f.port_callback = qib_create_port_files;
1571 	dd->verbs_dev.rdi.driver_f.get_pci_dev = qib_get_pci_dev;
1572 	dd->verbs_dev.rdi.driver_f.check_ah = qib_check_ah;
1573 	dd->verbs_dev.rdi.driver_f.setup_wqe = qib_check_send_wqe;
1574 	dd->verbs_dev.rdi.driver_f.notify_new_ah = qib_notify_new_ah;
1575 	dd->verbs_dev.rdi.driver_f.alloc_qpn = qib_alloc_qpn;
1576 	dd->verbs_dev.rdi.driver_f.qp_priv_alloc = qib_qp_priv_alloc;
1577 	dd->verbs_dev.rdi.driver_f.qp_priv_free = qib_qp_priv_free;
1578 	dd->verbs_dev.rdi.driver_f.free_all_qps = qib_free_all_qps;
1579 	dd->verbs_dev.rdi.driver_f.notify_qp_reset = qib_notify_qp_reset;
1580 	dd->verbs_dev.rdi.driver_f.do_send = qib_do_send;
1581 	dd->verbs_dev.rdi.driver_f.schedule_send = qib_schedule_send;
1582 	dd->verbs_dev.rdi.driver_f.quiesce_qp = qib_quiesce_qp;
1583 	dd->verbs_dev.rdi.driver_f.stop_send_queue = qib_stop_send_queue;
1584 	dd->verbs_dev.rdi.driver_f.flush_qp_waiters = qib_flush_qp_waiters;
1585 	dd->verbs_dev.rdi.driver_f.notify_error_qp = qib_notify_error_qp;
1586 	dd->verbs_dev.rdi.driver_f.notify_restart_rc = qib_restart_rc;
1587 	dd->verbs_dev.rdi.driver_f.mtu_to_path_mtu = qib_mtu_to_path_mtu;
1588 	dd->verbs_dev.rdi.driver_f.mtu_from_qp = qib_mtu_from_qp;
1589 	dd->verbs_dev.rdi.driver_f.get_pmtu_from_attr = qib_get_pmtu_from_attr;
1590 	dd->verbs_dev.rdi.driver_f.schedule_send_no_lock = _qib_schedule_send;
1591 	dd->verbs_dev.rdi.driver_f.query_port_state = qib_query_port;
1592 	dd->verbs_dev.rdi.driver_f.shut_down_port = qib_shut_down_port;
1593 	dd->verbs_dev.rdi.driver_f.cap_mask_chg = qib_cap_mask_chg;
1594 	dd->verbs_dev.rdi.driver_f.notify_create_mad_agent =
1595 						qib_notify_create_mad_agent;
1596 	dd->verbs_dev.rdi.driver_f.notify_free_mad_agent =
1597 						qib_notify_free_mad_agent;
1598 
1599 	dd->verbs_dev.rdi.dparms.max_rdma_atomic = QIB_MAX_RDMA_ATOMIC;
1600 	dd->verbs_dev.rdi.driver_f.get_guid_be = qib_get_guid_be;
1601 	dd->verbs_dev.rdi.dparms.lkey_table_size = qib_lkey_table_size;
1602 	dd->verbs_dev.rdi.dparms.qp_table_size = ib_qib_qp_table_size;
1603 	dd->verbs_dev.rdi.dparms.qpn_start = 1;
1604 	dd->verbs_dev.rdi.dparms.qpn_res_start = QIB_KD_QP;
1605 	dd->verbs_dev.rdi.dparms.qpn_res_end = QIB_KD_QP; /* Reserve one QP */
1606 	dd->verbs_dev.rdi.dparms.qpn_inc = 1;
1607 	dd->verbs_dev.rdi.dparms.qos_shift = 1;
1608 	dd->verbs_dev.rdi.dparms.psn_mask = QIB_PSN_MASK;
1609 	dd->verbs_dev.rdi.dparms.psn_shift = QIB_PSN_SHIFT;
1610 	dd->verbs_dev.rdi.dparms.psn_modify_mask = QIB_PSN_MASK;
1611 	dd->verbs_dev.rdi.dparms.nports = dd->num_pports;
1612 	dd->verbs_dev.rdi.dparms.npkeys = qib_get_npkeys(dd);
1613 	dd->verbs_dev.rdi.dparms.node = dd->assigned_node_id;
1614 	dd->verbs_dev.rdi.dparms.core_cap_flags = RDMA_CORE_PORT_IBA_IB;
1615 	dd->verbs_dev.rdi.dparms.max_mad_size = IB_MGMT_MAD_SIZE;
1616 	dd->verbs_dev.rdi.dparms.sge_copy_mode = RVT_SGE_COPY_MEMCPY;
1617 
1618 	qib_fill_device_attr(dd);
1619 
1620 	ppd = dd->pport;
1621 	for (i = 0; i < dd->num_pports; i++, ppd++) {
1622 		ctxt = ppd->hw_pidx;
1623 		rvt_init_port(&dd->verbs_dev.rdi,
1624 			      &ppd->ibport_data.rvp,
1625 			      i,
1626 			      dd->rcd[ctxt]->pkeys);
1627 	}
1628 	rdma_set_device_sysfs_group(&dd->verbs_dev.rdi.ibdev, &qib_attr_group);
1629 
1630 	ret = rvt_register_device(&dd->verbs_dev.rdi, RDMA_DRIVER_QIB);
1631 	if (ret)
1632 		goto err_tx;
1633 
1634 	return ret;
1635 
1636 err_tx:
1637 	while (!list_empty(&dev->txreq_free)) {
1638 		struct list_head *l = dev->txreq_free.next;
1639 		struct qib_verbs_txreq *tx;
1640 
1641 		list_del(l);
1642 		tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
1643 		kfree(tx);
1644 	}
1645 	if (ppd->sdma_descq_cnt)
1646 		dma_free_coherent(&dd->pcidev->dev,
1647 				  ppd->sdma_descq_cnt *
1648 					sizeof(struct qib_pio_header),
1649 				  dev->pio_hdrs, dev->pio_hdrs_phys);
1650 err_hdrs:
1651 	qib_dev_err(dd, "cannot register verbs: %d!\n", -ret);
1652 	return ret;
1653 }
1654 
1655 void qib_unregister_ib_device(struct qib_devdata *dd)
1656 {
1657 	struct qib_ibdev *dev = &dd->verbs_dev;
1658 
1659 	qib_verbs_unregister_sysfs(dd);
1660 
1661 	rvt_unregister_device(&dd->verbs_dev.rdi);
1662 
1663 	if (!list_empty(&dev->piowait))
1664 		qib_dev_err(dd, "piowait list not empty!\n");
1665 	if (!list_empty(&dev->dmawait))
1666 		qib_dev_err(dd, "dmawait list not empty!\n");
1667 	if (!list_empty(&dev->txwait))
1668 		qib_dev_err(dd, "txwait list not empty!\n");
1669 	if (!list_empty(&dev->memwait))
1670 		qib_dev_err(dd, "memwait list not empty!\n");
1671 
1672 	del_timer_sync(&dev->mem_timer);
1673 	while (!list_empty(&dev->txreq_free)) {
1674 		struct list_head *l = dev->txreq_free.next;
1675 		struct qib_verbs_txreq *tx;
1676 
1677 		list_del(l);
1678 		tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
1679 		kfree(tx);
1680 	}
1681 	if (dd->pport->sdma_descq_cnt)
1682 		dma_free_coherent(&dd->pcidev->dev,
1683 				  dd->pport->sdma_descq_cnt *
1684 					sizeof(struct qib_pio_header),
1685 				  dev->pio_hdrs, dev->pio_hdrs_phys);
1686 }
1687 
1688 /**
1689  * _qib_schedule_send - schedule progress
1690  * @qp - the qp
1691  *
1692  * This schedules progress w/o regard to the s_flags.
1693  *
1694  * It is only used in post send, which doesn't hold
1695  * the s_lock.
1696  */
1697 bool _qib_schedule_send(struct rvt_qp *qp)
1698 {
1699 	struct qib_ibport *ibp =
1700 		to_iport(qp->ibqp.device, qp->port_num);
1701 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
1702 	struct qib_qp_priv *priv = qp->priv;
1703 
1704 	return queue_work(ppd->qib_wq, &priv->s_work);
1705 }
1706 
1707 /**
1708  * qib_schedule_send - schedule progress
1709  * @qp - the qp
1710  *
1711  * This schedules qp progress.  The s_lock
1712  * should be held.
1713  */
1714 bool qib_schedule_send(struct rvt_qp *qp)
1715 {
1716 	if (qib_send_ok(qp))
1717 		return _qib_schedule_send(qp);
1718 	return false;
1719 }
1720