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  * Validate a RWQE and fill in the SGE state.
42  * Return 1 if OK.
43  */
44 static int qib_init_sge(struct rvt_qp *qp, struct rvt_rwqe *wqe)
45 {
46 	int i, j, ret;
47 	struct ib_wc wc;
48 	struct rvt_lkey_table *rkt;
49 	struct rvt_pd *pd;
50 	struct rvt_sge_state *ss;
51 
52 	rkt = &to_idev(qp->ibqp.device)->rdi.lkey_table;
53 	pd = ibpd_to_rvtpd(qp->ibqp.srq ? qp->ibqp.srq->pd : qp->ibqp.pd);
54 	ss = &qp->r_sge;
55 	ss->sg_list = qp->r_sg_list;
56 	qp->r_len = 0;
57 	for (i = j = 0; i < wqe->num_sge; i++) {
58 		if (wqe->sg_list[i].length == 0)
59 			continue;
60 		/* Check LKEY */
61 		if (!rvt_lkey_ok(rkt, pd, j ? &ss->sg_list[j - 1] : &ss->sge,
62 				 &wqe->sg_list[i], IB_ACCESS_LOCAL_WRITE))
63 			goto bad_lkey;
64 		qp->r_len += wqe->sg_list[i].length;
65 		j++;
66 	}
67 	ss->num_sge = j;
68 	ss->total_len = qp->r_len;
69 	ret = 1;
70 	goto bail;
71 
72 bad_lkey:
73 	while (j) {
74 		struct rvt_sge *sge = --j ? &ss->sg_list[j - 1] : &ss->sge;
75 
76 		rvt_put_mr(sge->mr);
77 	}
78 	ss->num_sge = 0;
79 	memset(&wc, 0, sizeof(wc));
80 	wc.wr_id = wqe->wr_id;
81 	wc.status = IB_WC_LOC_PROT_ERR;
82 	wc.opcode = IB_WC_RECV;
83 	wc.qp = &qp->ibqp;
84 	/* Signal solicited completion event. */
85 	rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
86 	ret = 0;
87 bail:
88 	return ret;
89 }
90 
91 /**
92  * qib_get_rwqe - copy the next RWQE into the QP's RWQE
93  * @qp: the QP
94  * @wr_id_only: update qp->r_wr_id only, not qp->r_sge
95  *
96  * Return -1 if there is a local error, 0 if no RWQE is available,
97  * otherwise return 1.
98  *
99  * Can be called from interrupt level.
100  */
101 int qib_get_rwqe(struct rvt_qp *qp, int wr_id_only)
102 {
103 	unsigned long flags;
104 	struct rvt_rq *rq;
105 	struct rvt_rwq *wq;
106 	struct rvt_srq *srq;
107 	struct rvt_rwqe *wqe;
108 	void (*handler)(struct ib_event *, void *);
109 	u32 tail;
110 	int ret;
111 
112 	if (qp->ibqp.srq) {
113 		srq = ibsrq_to_rvtsrq(qp->ibqp.srq);
114 		handler = srq->ibsrq.event_handler;
115 		rq = &srq->rq;
116 	} else {
117 		srq = NULL;
118 		handler = NULL;
119 		rq = &qp->r_rq;
120 	}
121 
122 	spin_lock_irqsave(&rq->lock, flags);
123 	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
124 		ret = 0;
125 		goto unlock;
126 	}
127 
128 	wq = rq->wq;
129 	tail = wq->tail;
130 	/* Validate tail before using it since it is user writable. */
131 	if (tail >= rq->size)
132 		tail = 0;
133 	if (unlikely(tail == wq->head)) {
134 		ret = 0;
135 		goto unlock;
136 	}
137 	/* Make sure entry is read after head index is read. */
138 	smp_rmb();
139 	wqe = rvt_get_rwqe_ptr(rq, tail);
140 	/*
141 	 * Even though we update the tail index in memory, the verbs
142 	 * consumer is not supposed to post more entries until a
143 	 * completion is generated.
144 	 */
145 	if (++tail >= rq->size)
146 		tail = 0;
147 	wq->tail = tail;
148 	if (!wr_id_only && !qib_init_sge(qp, wqe)) {
149 		ret = -1;
150 		goto unlock;
151 	}
152 	qp->r_wr_id = wqe->wr_id;
153 
154 	ret = 1;
155 	set_bit(RVT_R_WRID_VALID, &qp->r_aflags);
156 	if (handler) {
157 		u32 n;
158 
159 		/*
160 		 * Validate head pointer value and compute
161 		 * the number of remaining WQEs.
162 		 */
163 		n = wq->head;
164 		if (n >= rq->size)
165 			n = 0;
166 		if (n < tail)
167 			n += rq->size - tail;
168 		else
169 			n -= tail;
170 		if (n < srq->limit) {
171 			struct ib_event ev;
172 
173 			srq->limit = 0;
174 			spin_unlock_irqrestore(&rq->lock, flags);
175 			ev.device = qp->ibqp.device;
176 			ev.element.srq = qp->ibqp.srq;
177 			ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
178 			handler(&ev, srq->ibsrq.srq_context);
179 			goto bail;
180 		}
181 	}
182 unlock:
183 	spin_unlock_irqrestore(&rq->lock, flags);
184 bail:
185 	return ret;
186 }
187 
188 /*
189  * Switch to alternate path.
190  * The QP s_lock should be held and interrupts disabled.
191  */
192 void qib_migrate_qp(struct rvt_qp *qp)
193 {
194 	struct ib_event ev;
195 
196 	qp->s_mig_state = IB_MIG_MIGRATED;
197 	qp->remote_ah_attr = qp->alt_ah_attr;
198 	qp->port_num = qp->alt_ah_attr.port_num;
199 	qp->s_pkey_index = qp->s_alt_pkey_index;
200 
201 	ev.device = qp->ibqp.device;
202 	ev.element.qp = &qp->ibqp;
203 	ev.event = IB_EVENT_PATH_MIG;
204 	qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
205 }
206 
207 static __be64 get_sguid(struct qib_ibport *ibp, unsigned index)
208 {
209 	if (!index) {
210 		struct qib_pportdata *ppd = ppd_from_ibp(ibp);
211 
212 		return ppd->guid;
213 	}
214 	return ibp->guids[index - 1];
215 }
216 
217 static int gid_ok(union ib_gid *gid, __be64 gid_prefix, __be64 id)
218 {
219 	return (gid->global.interface_id == id &&
220 		(gid->global.subnet_prefix == gid_prefix ||
221 		 gid->global.subnet_prefix == IB_DEFAULT_GID_PREFIX));
222 }
223 
224 /*
225  *
226  * This should be called with the QP r_lock held.
227  *
228  * The s_lock will be acquired around the qib_migrate_qp() call.
229  */
230 int qib_ruc_check_hdr(struct qib_ibport *ibp, struct ib_header *hdr,
231 		      int has_grh, struct rvt_qp *qp, u32 bth0)
232 {
233 	__be64 guid;
234 	unsigned long flags;
235 
236 	if (qp->s_mig_state == IB_MIG_ARMED && (bth0 & IB_BTH_MIG_REQ)) {
237 		if (!has_grh) {
238 			if (qp->alt_ah_attr.ah_flags & IB_AH_GRH)
239 				goto err;
240 		} else {
241 			if (!(qp->alt_ah_attr.ah_flags & IB_AH_GRH))
242 				goto err;
243 			guid = get_sguid(ibp, qp->alt_ah_attr.grh.sgid_index);
244 			if (!gid_ok(&hdr->u.l.grh.dgid,
245 				    ibp->rvp.gid_prefix, guid))
246 				goto err;
247 			if (!gid_ok(&hdr->u.l.grh.sgid,
248 			    qp->alt_ah_attr.grh.dgid.global.subnet_prefix,
249 			    qp->alt_ah_attr.grh.dgid.global.interface_id))
250 				goto err;
251 		}
252 		if (!qib_pkey_ok((u16)bth0,
253 				 qib_get_pkey(ibp, qp->s_alt_pkey_index))) {
254 			qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_PKEY,
255 				      (u16)bth0,
256 				      (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
257 				      0, qp->ibqp.qp_num,
258 				      hdr->lrh[3], hdr->lrh[1]);
259 			goto err;
260 		}
261 		/* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */
262 		if (be16_to_cpu(hdr->lrh[3]) != qp->alt_ah_attr.dlid ||
263 		    ppd_from_ibp(ibp)->port != qp->alt_ah_attr.port_num)
264 			goto err;
265 		spin_lock_irqsave(&qp->s_lock, flags);
266 		qib_migrate_qp(qp);
267 		spin_unlock_irqrestore(&qp->s_lock, flags);
268 	} else {
269 		if (!has_grh) {
270 			if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
271 				goto err;
272 		} else {
273 			if (!(qp->remote_ah_attr.ah_flags & IB_AH_GRH))
274 				goto err;
275 			guid = get_sguid(ibp,
276 					 qp->remote_ah_attr.grh.sgid_index);
277 			if (!gid_ok(&hdr->u.l.grh.dgid,
278 				    ibp->rvp.gid_prefix, guid))
279 				goto err;
280 			if (!gid_ok(&hdr->u.l.grh.sgid,
281 			    qp->remote_ah_attr.grh.dgid.global.subnet_prefix,
282 			    qp->remote_ah_attr.grh.dgid.global.interface_id))
283 				goto err;
284 		}
285 		if (!qib_pkey_ok((u16)bth0,
286 				 qib_get_pkey(ibp, qp->s_pkey_index))) {
287 			qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_PKEY,
288 				      (u16)bth0,
289 				      (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
290 				      0, qp->ibqp.qp_num,
291 				      hdr->lrh[3], hdr->lrh[1]);
292 			goto err;
293 		}
294 		/* Validate the SLID. See Ch. 9.6.1.5 */
295 		if (be16_to_cpu(hdr->lrh[3]) != qp->remote_ah_attr.dlid ||
296 		    ppd_from_ibp(ibp)->port != qp->port_num)
297 			goto err;
298 		if (qp->s_mig_state == IB_MIG_REARM &&
299 		    !(bth0 & IB_BTH_MIG_REQ))
300 			qp->s_mig_state = IB_MIG_ARMED;
301 	}
302 
303 	return 0;
304 
305 err:
306 	return 1;
307 }
308 
309 /**
310  * qib_ruc_loopback - handle UC and RC lookback requests
311  * @sqp: the sending QP
312  *
313  * This is called from qib_do_send() to
314  * forward a WQE addressed to the same HCA.
315  * Note that although we are single threaded due to the tasklet, we still
316  * have to protect against post_send().  We don't have to worry about
317  * receive interrupts since this is a connected protocol and all packets
318  * will pass through here.
319  */
320 static void qib_ruc_loopback(struct rvt_qp *sqp)
321 {
322 	struct qib_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
323 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
324 	struct qib_devdata *dd = ppd->dd;
325 	struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
326 	struct rvt_qp *qp;
327 	struct rvt_swqe *wqe;
328 	struct rvt_sge *sge;
329 	unsigned long flags;
330 	struct ib_wc wc;
331 	u64 sdata;
332 	atomic64_t *maddr;
333 	enum ib_wc_status send_status;
334 	int release;
335 	int ret;
336 
337 	rcu_read_lock();
338 	/*
339 	 * Note that we check the responder QP state after
340 	 * checking the requester's state.
341 	 */
342 	qp = rvt_lookup_qpn(rdi, &ibp->rvp, sqp->remote_qpn);
343 	if (!qp)
344 		goto done;
345 
346 	spin_lock_irqsave(&sqp->s_lock, flags);
347 
348 	/* Return if we are already busy processing a work request. */
349 	if ((sqp->s_flags & (RVT_S_BUSY | RVT_S_ANY_WAIT)) ||
350 	    !(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_OR_FLUSH_SEND))
351 		goto unlock;
352 
353 	sqp->s_flags |= RVT_S_BUSY;
354 
355 again:
356 	smp_read_barrier_depends(); /* see post_one_send() */
357 	if (sqp->s_last == ACCESS_ONCE(sqp->s_head))
358 		goto clr_busy;
359 	wqe = rvt_get_swqe_ptr(sqp, sqp->s_last);
360 
361 	/* Return if it is not OK to start a new work reqeust. */
362 	if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
363 		if (!(ib_rvt_state_ops[sqp->state] & RVT_FLUSH_SEND))
364 			goto clr_busy;
365 		/* We are in the error state, flush the work request. */
366 		send_status = IB_WC_WR_FLUSH_ERR;
367 		goto flush_send;
368 	}
369 
370 	/*
371 	 * We can rely on the entry not changing without the s_lock
372 	 * being held until we update s_last.
373 	 * We increment s_cur to indicate s_last is in progress.
374 	 */
375 	if (sqp->s_last == sqp->s_cur) {
376 		if (++sqp->s_cur >= sqp->s_size)
377 			sqp->s_cur = 0;
378 	}
379 	spin_unlock_irqrestore(&sqp->s_lock, flags);
380 
381 	if (!qp || !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) ||
382 	    qp->ibqp.qp_type != sqp->ibqp.qp_type) {
383 		ibp->rvp.n_pkt_drops++;
384 		/*
385 		 * For RC, the requester would timeout and retry so
386 		 * shortcut the timeouts and just signal too many retries.
387 		 */
388 		if (sqp->ibqp.qp_type == IB_QPT_RC)
389 			send_status = IB_WC_RETRY_EXC_ERR;
390 		else
391 			send_status = IB_WC_SUCCESS;
392 		goto serr;
393 	}
394 
395 	memset(&wc, 0, sizeof(wc));
396 	send_status = IB_WC_SUCCESS;
397 
398 	release = 1;
399 	sqp->s_sge.sge = wqe->sg_list[0];
400 	sqp->s_sge.sg_list = wqe->sg_list + 1;
401 	sqp->s_sge.num_sge = wqe->wr.num_sge;
402 	sqp->s_len = wqe->length;
403 	switch (wqe->wr.opcode) {
404 	case IB_WR_SEND_WITH_IMM:
405 		wc.wc_flags = IB_WC_WITH_IMM;
406 		wc.ex.imm_data = wqe->wr.ex.imm_data;
407 		/* FALLTHROUGH */
408 	case IB_WR_SEND:
409 		ret = qib_get_rwqe(qp, 0);
410 		if (ret < 0)
411 			goto op_err;
412 		if (!ret)
413 			goto rnr_nak;
414 		break;
415 
416 	case IB_WR_RDMA_WRITE_WITH_IMM:
417 		if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
418 			goto inv_err;
419 		wc.wc_flags = IB_WC_WITH_IMM;
420 		wc.ex.imm_data = wqe->wr.ex.imm_data;
421 		ret = qib_get_rwqe(qp, 1);
422 		if (ret < 0)
423 			goto op_err;
424 		if (!ret)
425 			goto rnr_nak;
426 		/* FALLTHROUGH */
427 	case IB_WR_RDMA_WRITE:
428 		if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
429 			goto inv_err;
430 		if (wqe->length == 0)
431 			break;
432 		if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
433 					  wqe->rdma_wr.remote_addr,
434 					  wqe->rdma_wr.rkey,
435 					  IB_ACCESS_REMOTE_WRITE)))
436 			goto acc_err;
437 		qp->r_sge.sg_list = NULL;
438 		qp->r_sge.num_sge = 1;
439 		qp->r_sge.total_len = wqe->length;
440 		break;
441 
442 	case IB_WR_RDMA_READ:
443 		if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
444 			goto inv_err;
445 		if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
446 					  wqe->rdma_wr.remote_addr,
447 					  wqe->rdma_wr.rkey,
448 					  IB_ACCESS_REMOTE_READ)))
449 			goto acc_err;
450 		release = 0;
451 		sqp->s_sge.sg_list = NULL;
452 		sqp->s_sge.num_sge = 1;
453 		qp->r_sge.sge = wqe->sg_list[0];
454 		qp->r_sge.sg_list = wqe->sg_list + 1;
455 		qp->r_sge.num_sge = wqe->wr.num_sge;
456 		qp->r_sge.total_len = wqe->length;
457 		break;
458 
459 	case IB_WR_ATOMIC_CMP_AND_SWP:
460 	case IB_WR_ATOMIC_FETCH_AND_ADD:
461 		if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
462 			goto inv_err;
463 		if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
464 					  wqe->atomic_wr.remote_addr,
465 					  wqe->atomic_wr.rkey,
466 					  IB_ACCESS_REMOTE_ATOMIC)))
467 			goto acc_err;
468 		/* Perform atomic OP and save result. */
469 		maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
470 		sdata = wqe->atomic_wr.compare_add;
471 		*(u64 *) sqp->s_sge.sge.vaddr =
472 			(wqe->atomic_wr.wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
473 			(u64) atomic64_add_return(sdata, maddr) - sdata :
474 			(u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
475 				      sdata, wqe->atomic_wr.swap);
476 		rvt_put_mr(qp->r_sge.sge.mr);
477 		qp->r_sge.num_sge = 0;
478 		goto send_comp;
479 
480 	default:
481 		send_status = IB_WC_LOC_QP_OP_ERR;
482 		goto serr;
483 	}
484 
485 	sge = &sqp->s_sge.sge;
486 	while (sqp->s_len) {
487 		u32 len = sqp->s_len;
488 
489 		if (len > sge->length)
490 			len = sge->length;
491 		if (len > sge->sge_length)
492 			len = sge->sge_length;
493 		BUG_ON(len == 0);
494 		qib_copy_sge(&qp->r_sge, sge->vaddr, len, release);
495 		sge->vaddr += len;
496 		sge->length -= len;
497 		sge->sge_length -= len;
498 		if (sge->sge_length == 0) {
499 			if (!release)
500 				rvt_put_mr(sge->mr);
501 			if (--sqp->s_sge.num_sge)
502 				*sge = *sqp->s_sge.sg_list++;
503 		} else if (sge->length == 0 && sge->mr->lkey) {
504 			if (++sge->n >= RVT_SEGSZ) {
505 				if (++sge->m >= sge->mr->mapsz)
506 					break;
507 				sge->n = 0;
508 			}
509 			sge->vaddr =
510 				sge->mr->map[sge->m]->segs[sge->n].vaddr;
511 			sge->length =
512 				sge->mr->map[sge->m]->segs[sge->n].length;
513 		}
514 		sqp->s_len -= len;
515 	}
516 	if (release)
517 		rvt_put_ss(&qp->r_sge);
518 
519 	if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
520 		goto send_comp;
521 
522 	if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
523 		wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
524 	else
525 		wc.opcode = IB_WC_RECV;
526 	wc.wr_id = qp->r_wr_id;
527 	wc.status = IB_WC_SUCCESS;
528 	wc.byte_len = wqe->length;
529 	wc.qp = &qp->ibqp;
530 	wc.src_qp = qp->remote_qpn;
531 	wc.slid = qp->remote_ah_attr.dlid;
532 	wc.sl = qp->remote_ah_attr.sl;
533 	wc.port_num = 1;
534 	/* Signal completion event if the solicited bit is set. */
535 	rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
536 		     wqe->wr.send_flags & IB_SEND_SOLICITED);
537 
538 send_comp:
539 	spin_lock_irqsave(&sqp->s_lock, flags);
540 	ibp->rvp.n_loop_pkts++;
541 flush_send:
542 	sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
543 	qib_send_complete(sqp, wqe, send_status);
544 	goto again;
545 
546 rnr_nak:
547 	/* Handle RNR NAK */
548 	if (qp->ibqp.qp_type == IB_QPT_UC)
549 		goto send_comp;
550 	ibp->rvp.n_rnr_naks++;
551 	/*
552 	 * Note: we don't need the s_lock held since the BUSY flag
553 	 * makes this single threaded.
554 	 */
555 	if (sqp->s_rnr_retry == 0) {
556 		send_status = IB_WC_RNR_RETRY_EXC_ERR;
557 		goto serr;
558 	}
559 	if (sqp->s_rnr_retry_cnt < 7)
560 		sqp->s_rnr_retry--;
561 	spin_lock_irqsave(&sqp->s_lock, flags);
562 	if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK))
563 		goto clr_busy;
564 	rvt_add_rnr_timer(sqp, qp->r_min_rnr_timer <<
565 				IB_AETH_CREDIT_SHIFT);
566 	goto clr_busy;
567 
568 op_err:
569 	send_status = IB_WC_REM_OP_ERR;
570 	wc.status = IB_WC_LOC_QP_OP_ERR;
571 	goto err;
572 
573 inv_err:
574 	send_status = IB_WC_REM_INV_REQ_ERR;
575 	wc.status = IB_WC_LOC_QP_OP_ERR;
576 	goto err;
577 
578 acc_err:
579 	send_status = IB_WC_REM_ACCESS_ERR;
580 	wc.status = IB_WC_LOC_PROT_ERR;
581 err:
582 	/* responder goes to error state */
583 	rvt_rc_error(qp, wc.status);
584 
585 serr:
586 	spin_lock_irqsave(&sqp->s_lock, flags);
587 	qib_send_complete(sqp, wqe, send_status);
588 	if (sqp->ibqp.qp_type == IB_QPT_RC) {
589 		int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
590 
591 		sqp->s_flags &= ~RVT_S_BUSY;
592 		spin_unlock_irqrestore(&sqp->s_lock, flags);
593 		if (lastwqe) {
594 			struct ib_event ev;
595 
596 			ev.device = sqp->ibqp.device;
597 			ev.element.qp = &sqp->ibqp;
598 			ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
599 			sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
600 		}
601 		goto done;
602 	}
603 clr_busy:
604 	sqp->s_flags &= ~RVT_S_BUSY;
605 unlock:
606 	spin_unlock_irqrestore(&sqp->s_lock, flags);
607 done:
608 	rcu_read_unlock();
609 }
610 
611 /**
612  * qib_make_grh - construct a GRH header
613  * @ibp: a pointer to the IB port
614  * @hdr: a pointer to the GRH header being constructed
615  * @grh: the global route address to send to
616  * @hwords: the number of 32 bit words of header being sent
617  * @nwords: the number of 32 bit words of data being sent
618  *
619  * Return the size of the header in 32 bit words.
620  */
621 u32 qib_make_grh(struct qib_ibport *ibp, struct ib_grh *hdr,
622 		 struct ib_global_route *grh, u32 hwords, u32 nwords)
623 {
624 	hdr->version_tclass_flow =
625 		cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
626 			    (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
627 			    (grh->flow_label << IB_GRH_FLOW_SHIFT));
628 	hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
629 	/* next_hdr is defined by C8-7 in ch. 8.4.1 */
630 	hdr->next_hdr = IB_GRH_NEXT_HDR;
631 	hdr->hop_limit = grh->hop_limit;
632 	/* The SGID is 32-bit aligned. */
633 	hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
634 	hdr->sgid.global.interface_id = grh->sgid_index ?
635 		ibp->guids[grh->sgid_index - 1] : ppd_from_ibp(ibp)->guid;
636 	hdr->dgid = grh->dgid;
637 
638 	/* GRH header size in 32-bit words. */
639 	return sizeof(struct ib_grh) / sizeof(u32);
640 }
641 
642 void qib_make_ruc_header(struct rvt_qp *qp, struct ib_other_headers *ohdr,
643 			 u32 bth0, u32 bth2)
644 {
645 	struct qib_qp_priv *priv = qp->priv;
646 	struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
647 	u16 lrh0;
648 	u32 nwords;
649 	u32 extra_bytes;
650 
651 	/* Construct the header. */
652 	extra_bytes = -qp->s_cur_size & 3;
653 	nwords = (qp->s_cur_size + extra_bytes) >> 2;
654 	lrh0 = QIB_LRH_BTH;
655 	if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
656 		qp->s_hdrwords += qib_make_grh(ibp, &priv->s_hdr->u.l.grh,
657 					       &qp->remote_ah_attr.grh,
658 					       qp->s_hdrwords, nwords);
659 		lrh0 = QIB_LRH_GRH;
660 	}
661 	lrh0 |= ibp->sl_to_vl[qp->remote_ah_attr.sl] << 12 |
662 		qp->remote_ah_attr.sl << 4;
663 	priv->s_hdr->lrh[0] = cpu_to_be16(lrh0);
664 	priv->s_hdr->lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
665 	priv->s_hdr->lrh[2] =
666 			cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
667 	priv->s_hdr->lrh[3] = cpu_to_be16(ppd_from_ibp(ibp)->lid |
668 				       qp->remote_ah_attr.src_path_bits);
669 	bth0 |= qib_get_pkey(ibp, qp->s_pkey_index);
670 	bth0 |= extra_bytes << 20;
671 	if (qp->s_mig_state == IB_MIG_MIGRATED)
672 		bth0 |= IB_BTH_MIG_REQ;
673 	ohdr->bth[0] = cpu_to_be32(bth0);
674 	ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
675 	ohdr->bth[2] = cpu_to_be32(bth2);
676 	this_cpu_inc(ibp->pmastats->n_unicast_xmit);
677 }
678 
679 void _qib_do_send(struct work_struct *work)
680 {
681 	struct qib_qp_priv *priv = container_of(work, struct qib_qp_priv,
682 						s_work);
683 	struct rvt_qp *qp = priv->owner;
684 
685 	qib_do_send(qp);
686 }
687 
688 /**
689  * qib_do_send - perform a send on a QP
690  * @qp: pointer to the QP
691  *
692  * Process entries in the send work queue until credit or queue is
693  * exhausted.  Only allow one CPU to send a packet per QP (tasklet).
694  * Otherwise, two threads could send packets out of order.
695  */
696 void qib_do_send(struct rvt_qp *qp)
697 {
698 	struct qib_qp_priv *priv = qp->priv;
699 	struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
700 	struct qib_pportdata *ppd = ppd_from_ibp(ibp);
701 	int (*make_req)(struct rvt_qp *qp, unsigned long *flags);
702 	unsigned long flags;
703 
704 	if ((qp->ibqp.qp_type == IB_QPT_RC ||
705 	     qp->ibqp.qp_type == IB_QPT_UC) &&
706 	    (qp->remote_ah_attr.dlid & ~((1 << ppd->lmc) - 1)) == ppd->lid) {
707 		qib_ruc_loopback(qp);
708 		return;
709 	}
710 
711 	if (qp->ibqp.qp_type == IB_QPT_RC)
712 		make_req = qib_make_rc_req;
713 	else if (qp->ibqp.qp_type == IB_QPT_UC)
714 		make_req = qib_make_uc_req;
715 	else
716 		make_req = qib_make_ud_req;
717 
718 	spin_lock_irqsave(&qp->s_lock, flags);
719 
720 	/* Return if we are already busy processing a work request. */
721 	if (!qib_send_ok(qp)) {
722 		spin_unlock_irqrestore(&qp->s_lock, flags);
723 		return;
724 	}
725 
726 	qp->s_flags |= RVT_S_BUSY;
727 
728 	do {
729 		/* Check for a constructed packet to be sent. */
730 		if (qp->s_hdrwords != 0) {
731 			spin_unlock_irqrestore(&qp->s_lock, flags);
732 			/*
733 			 * If the packet cannot be sent now, return and
734 			 * the send tasklet will be woken up later.
735 			 */
736 			if (qib_verbs_send(qp, priv->s_hdr, qp->s_hdrwords,
737 					   qp->s_cur_sge, qp->s_cur_size))
738 				return;
739 			/* Record that s_hdr is empty. */
740 			qp->s_hdrwords = 0;
741 			spin_lock_irqsave(&qp->s_lock, flags);
742 		}
743 	} while (make_req(qp, &flags));
744 
745 	spin_unlock_irqrestore(&qp->s_lock, flags);
746 }
747 
748 /*
749  * This should be called with s_lock held.
750  */
751 void qib_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
752 		       enum ib_wc_status status)
753 {
754 	u32 old_last, last;
755 
756 	if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
757 		return;
758 
759 	last = qp->s_last;
760 	old_last = last;
761 	if (++last >= qp->s_size)
762 		last = 0;
763 	qp->s_last = last;
764 	/* See post_send() */
765 	barrier();
766 	rvt_put_swqe(wqe);
767 	if (qp->ibqp.qp_type == IB_QPT_UD ||
768 	    qp->ibqp.qp_type == IB_QPT_SMI ||
769 	    qp->ibqp.qp_type == IB_QPT_GSI)
770 		atomic_dec(&ibah_to_rvtah(wqe->ud_wr.ah)->refcount);
771 
772 	rvt_qp_swqe_complete(qp, wqe, status);
773 
774 	if (qp->s_acked == old_last)
775 		qp->s_acked = last;
776 	if (qp->s_cur == old_last)
777 		qp->s_cur = last;
778 	if (qp->s_tail == old_last)
779 		qp->s_tail = last;
780 	if (qp->state == IB_QPS_SQD && last == qp->s_cur)
781 		qp->s_draining = 0;
782 }
783