1 /*
2  * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
3  * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #include <linux/spinlock.h>
35 #include <rdma/ib_smi.h>
36 
37 #include "qib.h"
38 #include "qib_mad.h"
39 
40 /*
41  * Switch to alternate path.
42  * The QP s_lock should be held and interrupts disabled.
43  */
44 void qib_migrate_qp(struct rvt_qp *qp)
45 {
46 	struct ib_event ev;
47 
48 	qp->s_mig_state = IB_MIG_MIGRATED;
49 	qp->remote_ah_attr = qp->alt_ah_attr;
50 	qp->port_num = rdma_ah_get_port_num(&qp->alt_ah_attr);
51 	qp->s_pkey_index = qp->s_alt_pkey_index;
52 
53 	ev.device = qp->ibqp.device;
54 	ev.element.qp = &qp->ibqp;
55 	ev.event = IB_EVENT_PATH_MIG;
56 	qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
57 }
58 
59 static __be64 get_sguid(struct qib_ibport *ibp, unsigned index)
60 {
61 	if (!index) {
62 		struct qib_pportdata *ppd = ppd_from_ibp(ibp);
63 
64 		return ppd->guid;
65 	}
66 	return ibp->guids[index - 1];
67 }
68 
69 static int gid_ok(union ib_gid *gid, __be64 gid_prefix, __be64 id)
70 {
71 	return (gid->global.interface_id == id &&
72 		(gid->global.subnet_prefix == gid_prefix ||
73 		 gid->global.subnet_prefix == IB_DEFAULT_GID_PREFIX));
74 }
75 
76 /*
77  *
78  * This should be called with the QP r_lock held.
79  *
80  * The s_lock will be acquired around the qib_migrate_qp() call.
81  */
82 int qib_ruc_check_hdr(struct qib_ibport *ibp, struct ib_header *hdr,
83 		      int has_grh, struct rvt_qp *qp, u32 bth0)
84 {
85 	__be64 guid;
86 	unsigned long flags;
87 
88 	if (qp->s_mig_state == IB_MIG_ARMED && (bth0 & IB_BTH_MIG_REQ)) {
89 		if (!has_grh) {
90 			if (rdma_ah_get_ah_flags(&qp->alt_ah_attr) &
91 			    IB_AH_GRH)
92 				goto err;
93 		} else {
94 			const struct ib_global_route *grh;
95 
96 			if (!(rdma_ah_get_ah_flags(&qp->alt_ah_attr) &
97 			      IB_AH_GRH))
98 				goto err;
99 			grh = rdma_ah_read_grh(&qp->alt_ah_attr);
100 			guid = get_sguid(ibp, grh->sgid_index);
101 			if (!gid_ok(&hdr->u.l.grh.dgid,
102 				    ibp->rvp.gid_prefix, guid))
103 				goto err;
104 			if (!gid_ok(&hdr->u.l.grh.sgid,
105 			    grh->dgid.global.subnet_prefix,
106 			    grh->dgid.global.interface_id))
107 				goto err;
108 		}
109 		if (!qib_pkey_ok((u16)bth0,
110 				 qib_get_pkey(ibp, qp->s_alt_pkey_index))) {
111 			qib_bad_pkey(ibp,
112 				     (u16)bth0,
113 				     (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
114 				     0, qp->ibqp.qp_num,
115 				     hdr->lrh[3], hdr->lrh[1]);
116 			goto err;
117 		}
118 		/* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */
119 		if ((be16_to_cpu(hdr->lrh[3]) !=
120 		     rdma_ah_get_dlid(&qp->alt_ah_attr)) ||
121 		    ppd_from_ibp(ibp)->port !=
122 			    rdma_ah_get_port_num(&qp->alt_ah_attr))
123 			goto err;
124 		spin_lock_irqsave(&qp->s_lock, flags);
125 		qib_migrate_qp(qp);
126 		spin_unlock_irqrestore(&qp->s_lock, flags);
127 	} else {
128 		if (!has_grh) {
129 			if (rdma_ah_get_ah_flags(&qp->remote_ah_attr) &
130 			    IB_AH_GRH)
131 				goto err;
132 		} else {
133 			const struct ib_global_route *grh;
134 
135 			if (!(rdma_ah_get_ah_flags(&qp->remote_ah_attr) &
136 			      IB_AH_GRH))
137 				goto err;
138 			grh = rdma_ah_read_grh(&qp->remote_ah_attr);
139 			guid = get_sguid(ibp, grh->sgid_index);
140 			if (!gid_ok(&hdr->u.l.grh.dgid,
141 				    ibp->rvp.gid_prefix, guid))
142 				goto err;
143 			if (!gid_ok(&hdr->u.l.grh.sgid,
144 			    grh->dgid.global.subnet_prefix,
145 			    grh->dgid.global.interface_id))
146 				goto err;
147 		}
148 		if (!qib_pkey_ok((u16)bth0,
149 				 qib_get_pkey(ibp, qp->s_pkey_index))) {
150 			qib_bad_pkey(ibp,
151 				     (u16)bth0,
152 				     (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
153 				     0, qp->ibqp.qp_num,
154 				     hdr->lrh[3], hdr->lrh[1]);
155 			goto err;
156 		}
157 		/* Validate the SLID. See Ch. 9.6.1.5 */
158 		if (be16_to_cpu(hdr->lrh[3]) !=
159 		    rdma_ah_get_dlid(&qp->remote_ah_attr) ||
160 		    ppd_from_ibp(ibp)->port != qp->port_num)
161 			goto err;
162 		if (qp->s_mig_state == IB_MIG_REARM &&
163 		    !(bth0 & IB_BTH_MIG_REQ))
164 			qp->s_mig_state = IB_MIG_ARMED;
165 	}
166 
167 	return 0;
168 
169 err:
170 	return 1;
171 }
172 
173 /**
174  * qib_ruc_loopback - handle UC and RC lookback requests
175  * @sqp: the sending QP
176  *
177  * This is called from qib_do_send() to
178  * forward a WQE addressed to the same HCA.
179  * Note that although we are single threaded due to the tasklet, we still
180  * have to protect against post_send().  We don't have to worry about
181  * receive interrupts since this is a connected protocol and all packets
182  * will pass through here.
183  */
184 static void qib_ruc_loopback(struct rvt_qp *sqp)
185 {
186 	struct qib_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
187 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
188 	struct qib_devdata *dd = ppd->dd;
189 	struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
190 	struct rvt_qp *qp;
191 	struct rvt_swqe *wqe;
192 	struct rvt_sge *sge;
193 	unsigned long flags;
194 	struct ib_wc wc;
195 	u64 sdata;
196 	atomic64_t *maddr;
197 	enum ib_wc_status send_status;
198 	int release;
199 	int ret;
200 
201 	rcu_read_lock();
202 	/*
203 	 * Note that we check the responder QP state after
204 	 * checking the requester's state.
205 	 */
206 	qp = rvt_lookup_qpn(rdi, &ibp->rvp, sqp->remote_qpn);
207 	if (!qp)
208 		goto done;
209 
210 	spin_lock_irqsave(&sqp->s_lock, flags);
211 
212 	/* Return if we are already busy processing a work request. */
213 	if ((sqp->s_flags & (RVT_S_BUSY | RVT_S_ANY_WAIT)) ||
214 	    !(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_OR_FLUSH_SEND))
215 		goto unlock;
216 
217 	sqp->s_flags |= RVT_S_BUSY;
218 
219 again:
220 	if (sqp->s_last == READ_ONCE(sqp->s_head))
221 		goto clr_busy;
222 	wqe = rvt_get_swqe_ptr(sqp, sqp->s_last);
223 
224 	/* Return if it is not OK to start a new work reqeust. */
225 	if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
226 		if (!(ib_rvt_state_ops[sqp->state] & RVT_FLUSH_SEND))
227 			goto clr_busy;
228 		/* We are in the error state, flush the work request. */
229 		send_status = IB_WC_WR_FLUSH_ERR;
230 		goto flush_send;
231 	}
232 
233 	/*
234 	 * We can rely on the entry not changing without the s_lock
235 	 * being held until we update s_last.
236 	 * We increment s_cur to indicate s_last is in progress.
237 	 */
238 	if (sqp->s_last == sqp->s_cur) {
239 		if (++sqp->s_cur >= sqp->s_size)
240 			sqp->s_cur = 0;
241 	}
242 	spin_unlock_irqrestore(&sqp->s_lock, flags);
243 
244 	if (!qp || !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) ||
245 	    qp->ibqp.qp_type != sqp->ibqp.qp_type) {
246 		ibp->rvp.n_pkt_drops++;
247 		/*
248 		 * For RC, the requester would timeout and retry so
249 		 * shortcut the timeouts and just signal too many retries.
250 		 */
251 		if (sqp->ibqp.qp_type == IB_QPT_RC)
252 			send_status = IB_WC_RETRY_EXC_ERR;
253 		else
254 			send_status = IB_WC_SUCCESS;
255 		goto serr;
256 	}
257 
258 	memset(&wc, 0, sizeof(wc));
259 	send_status = IB_WC_SUCCESS;
260 
261 	release = 1;
262 	sqp->s_sge.sge = wqe->sg_list[0];
263 	sqp->s_sge.sg_list = wqe->sg_list + 1;
264 	sqp->s_sge.num_sge = wqe->wr.num_sge;
265 	sqp->s_len = wqe->length;
266 	switch (wqe->wr.opcode) {
267 	case IB_WR_SEND_WITH_IMM:
268 		wc.wc_flags = IB_WC_WITH_IMM;
269 		wc.ex.imm_data = wqe->wr.ex.imm_data;
270 		/* FALLTHROUGH */
271 	case IB_WR_SEND:
272 		ret = rvt_get_rwqe(qp, false);
273 		if (ret < 0)
274 			goto op_err;
275 		if (!ret)
276 			goto rnr_nak;
277 		break;
278 
279 	case IB_WR_RDMA_WRITE_WITH_IMM:
280 		if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
281 			goto inv_err;
282 		wc.wc_flags = IB_WC_WITH_IMM;
283 		wc.ex.imm_data = wqe->wr.ex.imm_data;
284 		ret = rvt_get_rwqe(qp, true);
285 		if (ret < 0)
286 			goto op_err;
287 		if (!ret)
288 			goto rnr_nak;
289 		/* FALLTHROUGH */
290 	case IB_WR_RDMA_WRITE:
291 		if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
292 			goto inv_err;
293 		if (wqe->length == 0)
294 			break;
295 		if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
296 					  wqe->rdma_wr.remote_addr,
297 					  wqe->rdma_wr.rkey,
298 					  IB_ACCESS_REMOTE_WRITE)))
299 			goto acc_err;
300 		qp->r_sge.sg_list = NULL;
301 		qp->r_sge.num_sge = 1;
302 		qp->r_sge.total_len = wqe->length;
303 		break;
304 
305 	case IB_WR_RDMA_READ:
306 		if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
307 			goto inv_err;
308 		if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
309 					  wqe->rdma_wr.remote_addr,
310 					  wqe->rdma_wr.rkey,
311 					  IB_ACCESS_REMOTE_READ)))
312 			goto acc_err;
313 		release = 0;
314 		sqp->s_sge.sg_list = NULL;
315 		sqp->s_sge.num_sge = 1;
316 		qp->r_sge.sge = wqe->sg_list[0];
317 		qp->r_sge.sg_list = wqe->sg_list + 1;
318 		qp->r_sge.num_sge = wqe->wr.num_sge;
319 		qp->r_sge.total_len = wqe->length;
320 		break;
321 
322 	case IB_WR_ATOMIC_CMP_AND_SWP:
323 	case IB_WR_ATOMIC_FETCH_AND_ADD:
324 		if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
325 			goto inv_err;
326 		if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
327 					  wqe->atomic_wr.remote_addr,
328 					  wqe->atomic_wr.rkey,
329 					  IB_ACCESS_REMOTE_ATOMIC)))
330 			goto acc_err;
331 		/* Perform atomic OP and save result. */
332 		maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
333 		sdata = wqe->atomic_wr.compare_add;
334 		*(u64 *) sqp->s_sge.sge.vaddr =
335 			(wqe->atomic_wr.wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
336 			(u64) atomic64_add_return(sdata, maddr) - sdata :
337 			(u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
338 				      sdata, wqe->atomic_wr.swap);
339 		rvt_put_mr(qp->r_sge.sge.mr);
340 		qp->r_sge.num_sge = 0;
341 		goto send_comp;
342 
343 	default:
344 		send_status = IB_WC_LOC_QP_OP_ERR;
345 		goto serr;
346 	}
347 
348 	sge = &sqp->s_sge.sge;
349 	while (sqp->s_len) {
350 		u32 len = sqp->s_len;
351 
352 		if (len > sge->length)
353 			len = sge->length;
354 		if (len > sge->sge_length)
355 			len = sge->sge_length;
356 		BUG_ON(len == 0);
357 		qib_copy_sge(&qp->r_sge, sge->vaddr, len, release);
358 		sge->vaddr += len;
359 		sge->length -= len;
360 		sge->sge_length -= len;
361 		if (sge->sge_length == 0) {
362 			if (!release)
363 				rvt_put_mr(sge->mr);
364 			if (--sqp->s_sge.num_sge)
365 				*sge = *sqp->s_sge.sg_list++;
366 		} else if (sge->length == 0 && sge->mr->lkey) {
367 			if (++sge->n >= RVT_SEGSZ) {
368 				if (++sge->m >= sge->mr->mapsz)
369 					break;
370 				sge->n = 0;
371 			}
372 			sge->vaddr =
373 				sge->mr->map[sge->m]->segs[sge->n].vaddr;
374 			sge->length =
375 				sge->mr->map[sge->m]->segs[sge->n].length;
376 		}
377 		sqp->s_len -= len;
378 	}
379 	if (release)
380 		rvt_put_ss(&qp->r_sge);
381 
382 	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
383 		goto send_comp;
384 
385 	if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
386 		wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
387 	else
388 		wc.opcode = IB_WC_RECV;
389 	wc.wr_id = qp->r_wr_id;
390 	wc.status = IB_WC_SUCCESS;
391 	wc.byte_len = wqe->length;
392 	wc.qp = &qp->ibqp;
393 	wc.src_qp = qp->remote_qpn;
394 	wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr);
395 	wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
396 	wc.port_num = 1;
397 	/* Signal completion event if the solicited bit is set. */
398 	rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
399 		     wqe->wr.send_flags & IB_SEND_SOLICITED);
400 
401 send_comp:
402 	spin_lock_irqsave(&sqp->s_lock, flags);
403 	ibp->rvp.n_loop_pkts++;
404 flush_send:
405 	sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
406 	qib_send_complete(sqp, wqe, send_status);
407 	goto again;
408 
409 rnr_nak:
410 	/* Handle RNR NAK */
411 	if (qp->ibqp.qp_type == IB_QPT_UC)
412 		goto send_comp;
413 	ibp->rvp.n_rnr_naks++;
414 	/*
415 	 * Note: we don't need the s_lock held since the BUSY flag
416 	 * makes this single threaded.
417 	 */
418 	if (sqp->s_rnr_retry == 0) {
419 		send_status = IB_WC_RNR_RETRY_EXC_ERR;
420 		goto serr;
421 	}
422 	if (sqp->s_rnr_retry_cnt < 7)
423 		sqp->s_rnr_retry--;
424 	spin_lock_irqsave(&sqp->s_lock, flags);
425 	if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK))
426 		goto clr_busy;
427 	rvt_add_rnr_timer(sqp, qp->r_min_rnr_timer <<
428 				IB_AETH_CREDIT_SHIFT);
429 	goto clr_busy;
430 
431 op_err:
432 	send_status = IB_WC_REM_OP_ERR;
433 	wc.status = IB_WC_LOC_QP_OP_ERR;
434 	goto err;
435 
436 inv_err:
437 	send_status = IB_WC_REM_INV_REQ_ERR;
438 	wc.status = IB_WC_LOC_QP_OP_ERR;
439 	goto err;
440 
441 acc_err:
442 	send_status = IB_WC_REM_ACCESS_ERR;
443 	wc.status = IB_WC_LOC_PROT_ERR;
444 err:
445 	/* responder goes to error state */
446 	rvt_rc_error(qp, wc.status);
447 
448 serr:
449 	spin_lock_irqsave(&sqp->s_lock, flags);
450 	qib_send_complete(sqp, wqe, send_status);
451 	if (sqp->ibqp.qp_type == IB_QPT_RC) {
452 		int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
453 
454 		sqp->s_flags &= ~RVT_S_BUSY;
455 		spin_unlock_irqrestore(&sqp->s_lock, flags);
456 		if (lastwqe) {
457 			struct ib_event ev;
458 
459 			ev.device = sqp->ibqp.device;
460 			ev.element.qp = &sqp->ibqp;
461 			ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
462 			sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
463 		}
464 		goto done;
465 	}
466 clr_busy:
467 	sqp->s_flags &= ~RVT_S_BUSY;
468 unlock:
469 	spin_unlock_irqrestore(&sqp->s_lock, flags);
470 done:
471 	rcu_read_unlock();
472 }
473 
474 /**
475  * qib_make_grh - construct a GRH header
476  * @ibp: a pointer to the IB port
477  * @hdr: a pointer to the GRH header being constructed
478  * @grh: the global route address to send to
479  * @hwords: the number of 32 bit words of header being sent
480  * @nwords: the number of 32 bit words of data being sent
481  *
482  * Return the size of the header in 32 bit words.
483  */
484 u32 qib_make_grh(struct qib_ibport *ibp, struct ib_grh *hdr,
485 		 const struct ib_global_route *grh, u32 hwords, u32 nwords)
486 {
487 	hdr->version_tclass_flow =
488 		cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
489 			    (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
490 			    (grh->flow_label << IB_GRH_FLOW_SHIFT));
491 	hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
492 	/* next_hdr is defined by C8-7 in ch. 8.4.1 */
493 	hdr->next_hdr = IB_GRH_NEXT_HDR;
494 	hdr->hop_limit = grh->hop_limit;
495 	/* The SGID is 32-bit aligned. */
496 	hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
497 	if (!grh->sgid_index)
498 		hdr->sgid.global.interface_id = ppd_from_ibp(ibp)->guid;
499 	else if (grh->sgid_index < QIB_GUIDS_PER_PORT)
500 		hdr->sgid.global.interface_id = ibp->guids[grh->sgid_index - 1];
501 	hdr->dgid = grh->dgid;
502 
503 	/* GRH header size in 32-bit words. */
504 	return sizeof(struct ib_grh) / sizeof(u32);
505 }
506 
507 void qib_make_ruc_header(struct rvt_qp *qp, struct ib_other_headers *ohdr,
508 			 u32 bth0, u32 bth2)
509 {
510 	struct qib_qp_priv *priv = qp->priv;
511 	struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
512 	u16 lrh0;
513 	u32 nwords;
514 	u32 extra_bytes;
515 
516 	/* Construct the header. */
517 	extra_bytes = -qp->s_cur_size & 3;
518 	nwords = (qp->s_cur_size + extra_bytes) >> 2;
519 	lrh0 = QIB_LRH_BTH;
520 	if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)) {
521 		qp->s_hdrwords +=
522 			qib_make_grh(ibp, &priv->s_hdr->u.l.grh,
523 				     rdma_ah_read_grh(&qp->remote_ah_attr),
524 				     qp->s_hdrwords, nwords);
525 		lrh0 = QIB_LRH_GRH;
526 	}
527 	lrh0 |= ibp->sl_to_vl[rdma_ah_get_sl(&qp->remote_ah_attr)] << 12 |
528 		rdma_ah_get_sl(&qp->remote_ah_attr) << 4;
529 	priv->s_hdr->lrh[0] = cpu_to_be16(lrh0);
530 	priv->s_hdr->lrh[1] =
531 			cpu_to_be16(rdma_ah_get_dlid(&qp->remote_ah_attr));
532 	priv->s_hdr->lrh[2] =
533 			cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
534 	priv->s_hdr->lrh[3] =
535 		cpu_to_be16(ppd_from_ibp(ibp)->lid |
536 			    rdma_ah_get_path_bits(&qp->remote_ah_attr));
537 	bth0 |= qib_get_pkey(ibp, qp->s_pkey_index);
538 	bth0 |= extra_bytes << 20;
539 	if (qp->s_mig_state == IB_MIG_MIGRATED)
540 		bth0 |= IB_BTH_MIG_REQ;
541 	ohdr->bth[0] = cpu_to_be32(bth0);
542 	ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
543 	ohdr->bth[2] = cpu_to_be32(bth2);
544 	this_cpu_inc(ibp->pmastats->n_unicast_xmit);
545 }
546 
547 void _qib_do_send(struct work_struct *work)
548 {
549 	struct qib_qp_priv *priv = container_of(work, struct qib_qp_priv,
550 						s_work);
551 	struct rvt_qp *qp = priv->owner;
552 
553 	qib_do_send(qp);
554 }
555 
556 /**
557  * qib_do_send - perform a send on a QP
558  * @qp: pointer to the QP
559  *
560  * Process entries in the send work queue until credit or queue is
561  * exhausted.  Only allow one CPU to send a packet per QP (tasklet).
562  * Otherwise, two threads could send packets out of order.
563  */
564 void qib_do_send(struct rvt_qp *qp)
565 {
566 	struct qib_qp_priv *priv = qp->priv;
567 	struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
568 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
569 	int (*make_req)(struct rvt_qp *qp, unsigned long *flags);
570 	unsigned long flags;
571 
572 	if ((qp->ibqp.qp_type == IB_QPT_RC ||
573 	     qp->ibqp.qp_type == IB_QPT_UC) &&
574 	    (rdma_ah_get_dlid(&qp->remote_ah_attr) &
575 	     ~((1 << ppd->lmc) - 1)) == ppd->lid) {
576 		qib_ruc_loopback(qp);
577 		return;
578 	}
579 
580 	if (qp->ibqp.qp_type == IB_QPT_RC)
581 		make_req = qib_make_rc_req;
582 	else if (qp->ibqp.qp_type == IB_QPT_UC)
583 		make_req = qib_make_uc_req;
584 	else
585 		make_req = qib_make_ud_req;
586 
587 	spin_lock_irqsave(&qp->s_lock, flags);
588 
589 	/* Return if we are already busy processing a work request. */
590 	if (!qib_send_ok(qp)) {
591 		spin_unlock_irqrestore(&qp->s_lock, flags);
592 		return;
593 	}
594 
595 	qp->s_flags |= RVT_S_BUSY;
596 
597 	do {
598 		/* Check for a constructed packet to be sent. */
599 		if (qp->s_hdrwords != 0) {
600 			spin_unlock_irqrestore(&qp->s_lock, flags);
601 			/*
602 			 * If the packet cannot be sent now, return and
603 			 * the send tasklet will be woken up later.
604 			 */
605 			if (qib_verbs_send(qp, priv->s_hdr, qp->s_hdrwords,
606 					   qp->s_cur_sge, qp->s_cur_size))
607 				return;
608 			/* Record that s_hdr is empty. */
609 			qp->s_hdrwords = 0;
610 			spin_lock_irqsave(&qp->s_lock, flags);
611 		}
612 	} while (make_req(qp, &flags));
613 
614 	spin_unlock_irqrestore(&qp->s_lock, flags);
615 }
616 
617 /*
618  * This should be called with s_lock held.
619  */
620 void qib_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
621 		       enum ib_wc_status status)
622 {
623 	u32 old_last, last;
624 
625 	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
626 		return;
627 
628 	last = qp->s_last;
629 	old_last = last;
630 	if (++last >= qp->s_size)
631 		last = 0;
632 	qp->s_last = last;
633 	/* See post_send() */
634 	barrier();
635 	rvt_put_swqe(wqe);
636 	if (qp->ibqp.qp_type == IB_QPT_UD ||
637 	    qp->ibqp.qp_type == IB_QPT_SMI ||
638 	    qp->ibqp.qp_type == IB_QPT_GSI)
639 		atomic_dec(&ibah_to_rvtah(wqe->ud_wr.ah)->refcount);
640 
641 	rvt_qp_swqe_complete(qp,
642 			     wqe,
643 			     ib_qib_wc_opcode[wqe->wr.opcode],
644 			     status);
645 
646 	if (qp->s_acked == old_last)
647 		qp->s_acked = last;
648 	if (qp->s_cur == old_last)
649 		qp->s_cur = last;
650 	if (qp->s_tail == old_last)
651 		qp->s_tail = last;
652 	if (qp->state == IB_QPS_SQD && last == qp->s_cur)
653 		qp->s_draining = 0;
654 }
655