blob: b6777925629704d3a2c3c7bc51a5475a725ca2c3 [file] [log] [blame]
Ralph Campbellf9315512010-05-23 21:44:54 -07001/*
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>
Ira Weiny0629cb02015-09-03 14:16:30 -040035#include <rdma/ib_smi.h>
Ralph Campbellf9315512010-05-23 21:44:54 -070036
37#include "qib.h"
38#include "qib_mad.h"
39
40/*
41 * Convert the AETH RNR timeout code into the number of microseconds.
42 */
43const u32 ib_qib_rnr_table[32] = {
44 655360, /* 00: 655.36 */
45 10, /* 01: .01 */
46 20, /* 02 .02 */
47 30, /* 03: .03 */
48 40, /* 04: .04 */
49 60, /* 05: .06 */
50 80, /* 06: .08 */
51 120, /* 07: .12 */
52 160, /* 08: .16 */
53 240, /* 09: .24 */
54 320, /* 0A: .32 */
55 480, /* 0B: .48 */
56 640, /* 0C: .64 */
57 960, /* 0D: .96 */
58 1280, /* 0E: 1.28 */
59 1920, /* 0F: 1.92 */
60 2560, /* 10: 2.56 */
61 3840, /* 11: 3.84 */
62 5120, /* 12: 5.12 */
63 7680, /* 13: 7.68 */
64 10240, /* 14: 10.24 */
65 15360, /* 15: 15.36 */
66 20480, /* 16: 20.48 */
67 30720, /* 17: 30.72 */
68 40960, /* 18: 40.96 */
69 61440, /* 19: 61.44 */
70 81920, /* 1A: 81.92 */
71 122880, /* 1B: 122.88 */
72 163840, /* 1C: 163.84 */
73 245760, /* 1D: 245.76 */
74 327680, /* 1E: 327.68 */
75 491520 /* 1F: 491.52 */
76};
77
78/*
79 * Validate a RWQE and fill in the SGE state.
80 * Return 1 if OK.
81 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -080082static int qib_init_sge(struct rvt_qp *qp, struct rvt_rwqe *wqe)
Ralph Campbellf9315512010-05-23 21:44:54 -070083{
84 int i, j, ret;
85 struct ib_wc wc;
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -080086 struct rvt_lkey_table *rkt;
Dennis Dalessandrof44728d2016-01-22 12:44:44 -080087 struct rvt_pd *pd;
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -080088 struct rvt_sge_state *ss;
Ralph Campbellf9315512010-05-23 21:44:54 -070089
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -080090 rkt = &to_idev(qp->ibqp.device)->rdi.lkey_table;
Dennis Dalessandrof44728d2016-01-22 12:44:44 -080091 pd = ibpd_to_rvtpd(qp->ibqp.srq ? qp->ibqp.srq->pd : qp->ibqp.pd);
Ralph Campbellf9315512010-05-23 21:44:54 -070092 ss = &qp->r_sge;
93 ss->sg_list = qp->r_sg_list;
94 qp->r_len = 0;
95 for (i = j = 0; i < wqe->num_sge; i++) {
96 if (wqe->sg_list[i].length == 0)
97 continue;
98 /* Check LKEY */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -080099 if (!rvt_lkey_ok(rkt, pd, j ? &ss->sg_list[j - 1] : &ss->sge,
Ralph Campbellf9315512010-05-23 21:44:54 -0700100 &wqe->sg_list[i], IB_ACCESS_LOCAL_WRITE))
101 goto bad_lkey;
102 qp->r_len += wqe->sg_list[i].length;
103 j++;
104 }
105 ss->num_sge = j;
106 ss->total_len = qp->r_len;
107 ret = 1;
108 goto bail;
109
110bad_lkey:
111 while (j) {
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800112 struct rvt_sge *sge = --j ? &ss->sg_list[j - 1] : &ss->sge;
Ralph Campbellf9315512010-05-23 21:44:54 -0700113
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800114 rvt_put_mr(sge->mr);
Ralph Campbellf9315512010-05-23 21:44:54 -0700115 }
116 ss->num_sge = 0;
117 memset(&wc, 0, sizeof(wc));
118 wc.wr_id = wqe->wr_id;
119 wc.status = IB_WC_LOC_PROT_ERR;
120 wc.opcode = IB_WC_RECV;
121 wc.qp = &qp->ibqp;
122 /* Signal solicited completion event. */
Harish Chegondi4bb88e52016-01-22 13:07:36 -0800123 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
Ralph Campbellf9315512010-05-23 21:44:54 -0700124 ret = 0;
125bail:
126 return ret;
127}
128
129/**
130 * qib_get_rwqe - copy the next RWQE into the QP's RWQE
131 * @qp: the QP
132 * @wr_id_only: update qp->r_wr_id only, not qp->r_sge
133 *
134 * Return -1 if there is a local error, 0 if no RWQE is available,
135 * otherwise return 1.
136 *
137 * Can be called from interrupt level.
138 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800139int qib_get_rwqe(struct rvt_qp *qp, int wr_id_only)
Ralph Campbellf9315512010-05-23 21:44:54 -0700140{
141 unsigned long flags;
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800142 struct rvt_rq *rq;
143 struct rvt_rwq *wq;
Dennis Dalessandro894c7272016-01-22 12:46:17 -0800144 struct rvt_srq *srq;
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800145 struct rvt_rwqe *wqe;
Ralph Campbellf9315512010-05-23 21:44:54 -0700146 void (*handler)(struct ib_event *, void *);
147 u32 tail;
148 int ret;
149
150 if (qp->ibqp.srq) {
Dennis Dalessandro894c7272016-01-22 12:46:17 -0800151 srq = ibsrq_to_rvtsrq(qp->ibqp.srq);
Ralph Campbellf9315512010-05-23 21:44:54 -0700152 handler = srq->ibsrq.event_handler;
153 rq = &srq->rq;
154 } else {
155 srq = NULL;
156 handler = NULL;
157 rq = &qp->r_rq;
158 }
159
160 spin_lock_irqsave(&rq->lock, flags);
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800161 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
Ralph Campbellf9315512010-05-23 21:44:54 -0700162 ret = 0;
163 goto unlock;
164 }
165
166 wq = rq->wq;
167 tail = wq->tail;
168 /* Validate tail before using it since it is user writable. */
169 if (tail >= rq->size)
170 tail = 0;
171 if (unlikely(tail == wq->head)) {
172 ret = 0;
173 goto unlock;
174 }
175 /* Make sure entry is read after head index is read. */
176 smp_rmb();
Harish Chegondi70696ea2016-02-03 14:20:27 -0800177 wqe = rvt_get_rwqe_ptr(rq, tail);
Ralph Campbellf9315512010-05-23 21:44:54 -0700178 /*
179 * Even though we update the tail index in memory, the verbs
180 * consumer is not supposed to post more entries until a
181 * completion is generated.
182 */
183 if (++tail >= rq->size)
184 tail = 0;
185 wq->tail = tail;
186 if (!wr_id_only && !qib_init_sge(qp, wqe)) {
187 ret = -1;
188 goto unlock;
189 }
190 qp->r_wr_id = wqe->wr_id;
191
192 ret = 1;
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800193 set_bit(RVT_R_WRID_VALID, &qp->r_aflags);
Ralph Campbellf9315512010-05-23 21:44:54 -0700194 if (handler) {
195 u32 n;
196
197 /*
198 * Validate head pointer value and compute
199 * the number of remaining WQEs.
200 */
201 n = wq->head;
202 if (n >= rq->size)
203 n = 0;
204 if (n < tail)
205 n += rq->size - tail;
206 else
207 n -= tail;
208 if (n < srq->limit) {
209 struct ib_event ev;
210
211 srq->limit = 0;
212 spin_unlock_irqrestore(&rq->lock, flags);
213 ev.device = qp->ibqp.device;
214 ev.element.srq = qp->ibqp.srq;
215 ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
216 handler(&ev, srq->ibsrq.srq_context);
217 goto bail;
218 }
219 }
220unlock:
221 spin_unlock_irqrestore(&rq->lock, flags);
222bail:
223 return ret;
224}
225
226/*
227 * Switch to alternate path.
228 * The QP s_lock should be held and interrupts disabled.
229 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800230void qib_migrate_qp(struct rvt_qp *qp)
Ralph Campbellf9315512010-05-23 21:44:54 -0700231{
232 struct ib_event ev;
233
234 qp->s_mig_state = IB_MIG_MIGRATED;
235 qp->remote_ah_attr = qp->alt_ah_attr;
236 qp->port_num = qp->alt_ah_attr.port_num;
237 qp->s_pkey_index = qp->s_alt_pkey_index;
238
239 ev.device = qp->ibqp.device;
240 ev.element.qp = &qp->ibqp;
241 ev.event = IB_EVENT_PATH_MIG;
242 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
243}
244
245static __be64 get_sguid(struct qib_ibport *ibp, unsigned index)
246{
247 if (!index) {
248 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
249
250 return ppd->guid;
Mike Marciniszyna46a2802015-01-16 10:52:18 -0500251 }
252 return ibp->guids[index - 1];
Ralph Campbellf9315512010-05-23 21:44:54 -0700253}
254
255static int gid_ok(union ib_gid *gid, __be64 gid_prefix, __be64 id)
256{
257 return (gid->global.interface_id == id &&
258 (gid->global.subnet_prefix == gid_prefix ||
259 gid->global.subnet_prefix == IB_DEFAULT_GID_PREFIX));
260}
261
262/*
263 *
Mike Marciniszyn9fd54732011-09-23 13:17:00 -0400264 * This should be called with the QP r_lock held.
265 *
266 * The s_lock will be acquired around the qib_migrate_qp() call.
Ralph Campbellf9315512010-05-23 21:44:54 -0700267 */
268int qib_ruc_check_hdr(struct qib_ibport *ibp, struct qib_ib_header *hdr,
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800269 int has_grh, struct rvt_qp *qp, u32 bth0)
Ralph Campbellf9315512010-05-23 21:44:54 -0700270{
271 __be64 guid;
Mike Marciniszyn9fd54732011-09-23 13:17:00 -0400272 unsigned long flags;
Ralph Campbellf9315512010-05-23 21:44:54 -0700273
274 if (qp->s_mig_state == IB_MIG_ARMED && (bth0 & IB_BTH_MIG_REQ)) {
275 if (!has_grh) {
276 if (qp->alt_ah_attr.ah_flags & IB_AH_GRH)
277 goto err;
278 } else {
279 if (!(qp->alt_ah_attr.ah_flags & IB_AH_GRH))
280 goto err;
281 guid = get_sguid(ibp, qp->alt_ah_attr.grh.sgid_index);
Harish Chegondif24a6d42016-01-22 12:56:02 -0800282 if (!gid_ok(&hdr->u.l.grh.dgid,
283 ibp->rvp.gid_prefix, guid))
Ralph Campbellf9315512010-05-23 21:44:54 -0700284 goto err;
285 if (!gid_ok(&hdr->u.l.grh.sgid,
286 qp->alt_ah_attr.grh.dgid.global.subnet_prefix,
287 qp->alt_ah_attr.grh.dgid.global.interface_id))
288 goto err;
289 }
290 if (!qib_pkey_ok((u16)bth0,
291 qib_get_pkey(ibp, qp->s_alt_pkey_index))) {
292 qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_PKEY,
293 (u16)bth0,
294 (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
295 0, qp->ibqp.qp_num,
296 hdr->lrh[3], hdr->lrh[1]);
297 goto err;
298 }
299 /* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */
300 if (be16_to_cpu(hdr->lrh[3]) != qp->alt_ah_attr.dlid ||
301 ppd_from_ibp(ibp)->port != qp->alt_ah_attr.port_num)
302 goto err;
Mike Marciniszyn9fd54732011-09-23 13:17:00 -0400303 spin_lock_irqsave(&qp->s_lock, flags);
Ralph Campbellf9315512010-05-23 21:44:54 -0700304 qib_migrate_qp(qp);
Mike Marciniszyn9fd54732011-09-23 13:17:00 -0400305 spin_unlock_irqrestore(&qp->s_lock, flags);
Ralph Campbellf9315512010-05-23 21:44:54 -0700306 } else {
307 if (!has_grh) {
308 if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
309 goto err;
310 } else {
311 if (!(qp->remote_ah_attr.ah_flags & IB_AH_GRH))
312 goto err;
313 guid = get_sguid(ibp,
314 qp->remote_ah_attr.grh.sgid_index);
Harish Chegondif24a6d42016-01-22 12:56:02 -0800315 if (!gid_ok(&hdr->u.l.grh.dgid,
316 ibp->rvp.gid_prefix, guid))
Ralph Campbellf9315512010-05-23 21:44:54 -0700317 goto err;
318 if (!gid_ok(&hdr->u.l.grh.sgid,
319 qp->remote_ah_attr.grh.dgid.global.subnet_prefix,
320 qp->remote_ah_attr.grh.dgid.global.interface_id))
321 goto err;
322 }
323 if (!qib_pkey_ok((u16)bth0,
324 qib_get_pkey(ibp, qp->s_pkey_index))) {
325 qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_PKEY,
326 (u16)bth0,
327 (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
328 0, qp->ibqp.qp_num,
329 hdr->lrh[3], hdr->lrh[1]);
330 goto err;
331 }
332 /* Validate the SLID. See Ch. 9.6.1.5 */
333 if (be16_to_cpu(hdr->lrh[3]) != qp->remote_ah_attr.dlid ||
334 ppd_from_ibp(ibp)->port != qp->port_num)
335 goto err;
336 if (qp->s_mig_state == IB_MIG_REARM &&
337 !(bth0 & IB_BTH_MIG_REQ))
338 qp->s_mig_state = IB_MIG_ARMED;
339 }
340
341 return 0;
342
343err:
344 return 1;
345}
346
347/**
348 * qib_ruc_loopback - handle UC and RC lookback requests
349 * @sqp: the sending QP
350 *
351 * This is called from qib_do_send() to
352 * forward a WQE addressed to the same HCA.
353 * Note that although we are single threaded due to the tasklet, we still
354 * have to protect against post_send(). We don't have to worry about
355 * receive interrupts since this is a connected protocol and all packets
356 * will pass through here.
357 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800358static void qib_ruc_loopback(struct rvt_qp *sqp)
Ralph Campbellf9315512010-05-23 21:44:54 -0700359{
360 struct qib_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
Harish Chegondi1cefc2c2016-02-03 14:20:19 -0800361 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
362 struct qib_devdata *dd = ppd->dd;
363 struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800364 struct rvt_qp *qp;
365 struct rvt_swqe *wqe;
366 struct rvt_sge *sge;
Ralph Campbellf9315512010-05-23 21:44:54 -0700367 unsigned long flags;
368 struct ib_wc wc;
369 u64 sdata;
370 atomic64_t *maddr;
371 enum ib_wc_status send_status;
372 int release;
373 int ret;
374
Harish Chegondi1cefc2c2016-02-03 14:20:19 -0800375 rcu_read_lock();
Ralph Campbellf9315512010-05-23 21:44:54 -0700376 /*
377 * Note that we check the responder QP state after
378 * checking the requester's state.
379 */
Harish Chegondi1cefc2c2016-02-03 14:20:19 -0800380 qp = rvt_lookup_qpn(rdi, &ibp->rvp, sqp->remote_qpn);
381 if (!qp)
382 goto done;
Ralph Campbellf9315512010-05-23 21:44:54 -0700383
384 spin_lock_irqsave(&sqp->s_lock, flags);
385
386 /* Return if we are already busy processing a work request. */
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800387 if ((sqp->s_flags & (RVT_S_BUSY | RVT_S_ANY_WAIT)) ||
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800388 !(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_OR_FLUSH_SEND))
Ralph Campbellf9315512010-05-23 21:44:54 -0700389 goto unlock;
390
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800391 sqp->s_flags |= RVT_S_BUSY;
Ralph Campbellf9315512010-05-23 21:44:54 -0700392
393again:
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800394 smp_read_barrier_depends(); /* see post_one_send() */
395 if (sqp->s_last == ACCESS_ONCE(sqp->s_head))
Ralph Campbellf9315512010-05-23 21:44:54 -0700396 goto clr_busy;
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800397 wqe = rvt_get_swqe_ptr(sqp, sqp->s_last);
Ralph Campbellf9315512010-05-23 21:44:54 -0700398
399 /* Return if it is not OK to start a new work reqeust. */
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800400 if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
401 if (!(ib_rvt_state_ops[sqp->state] & RVT_FLUSH_SEND))
Ralph Campbellf9315512010-05-23 21:44:54 -0700402 goto clr_busy;
403 /* We are in the error state, flush the work request. */
404 send_status = IB_WC_WR_FLUSH_ERR;
405 goto flush_send;
406 }
407
408 /*
409 * We can rely on the entry not changing without the s_lock
410 * being held until we update s_last.
411 * We increment s_cur to indicate s_last is in progress.
412 */
413 if (sqp->s_last == sqp->s_cur) {
414 if (++sqp->s_cur >= sqp->s_size)
415 sqp->s_cur = 0;
416 }
417 spin_unlock_irqrestore(&sqp->s_lock, flags);
418
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800419 if (!qp || !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) ||
Ralph Campbellf9315512010-05-23 21:44:54 -0700420 qp->ibqp.qp_type != sqp->ibqp.qp_type) {
Harish Chegondif24a6d42016-01-22 12:56:02 -0800421 ibp->rvp.n_pkt_drops++;
Ralph Campbellf9315512010-05-23 21:44:54 -0700422 /*
423 * For RC, the requester would timeout and retry so
424 * shortcut the timeouts and just signal too many retries.
425 */
426 if (sqp->ibqp.qp_type == IB_QPT_RC)
427 send_status = IB_WC_RETRY_EXC_ERR;
428 else
429 send_status = IB_WC_SUCCESS;
430 goto serr;
431 }
432
Mike Marciniszyn041af0b2015-01-16 10:50:32 -0500433 memset(&wc, 0, sizeof(wc));
Ralph Campbellf9315512010-05-23 21:44:54 -0700434 send_status = IB_WC_SUCCESS;
435
436 release = 1;
437 sqp->s_sge.sge = wqe->sg_list[0];
438 sqp->s_sge.sg_list = wqe->sg_list + 1;
439 sqp->s_sge.num_sge = wqe->wr.num_sge;
440 sqp->s_len = wqe->length;
441 switch (wqe->wr.opcode) {
442 case IB_WR_SEND_WITH_IMM:
443 wc.wc_flags = IB_WC_WITH_IMM;
444 wc.ex.imm_data = wqe->wr.ex.imm_data;
445 /* FALLTHROUGH */
446 case IB_WR_SEND:
447 ret = qib_get_rwqe(qp, 0);
448 if (ret < 0)
449 goto op_err;
450 if (!ret)
451 goto rnr_nak;
452 break;
453
454 case IB_WR_RDMA_WRITE_WITH_IMM:
455 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
456 goto inv_err;
457 wc.wc_flags = IB_WC_WITH_IMM;
458 wc.ex.imm_data = wqe->wr.ex.imm_data;
459 ret = qib_get_rwqe(qp, 1);
460 if (ret < 0)
461 goto op_err;
462 if (!ret)
463 goto rnr_nak;
464 /* FALLTHROUGH */
465 case IB_WR_RDMA_WRITE:
466 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
467 goto inv_err;
468 if (wqe->length == 0)
469 break;
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800470 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100471 wqe->rdma_wr.remote_addr,
472 wqe->rdma_wr.rkey,
Ralph Campbellf9315512010-05-23 21:44:54 -0700473 IB_ACCESS_REMOTE_WRITE)))
474 goto acc_err;
475 qp->r_sge.sg_list = NULL;
476 qp->r_sge.num_sge = 1;
477 qp->r_sge.total_len = wqe->length;
478 break;
479
480 case IB_WR_RDMA_READ:
481 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
482 goto inv_err;
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800483 if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100484 wqe->rdma_wr.remote_addr,
485 wqe->rdma_wr.rkey,
Ralph Campbellf9315512010-05-23 21:44:54 -0700486 IB_ACCESS_REMOTE_READ)))
487 goto acc_err;
488 release = 0;
489 sqp->s_sge.sg_list = NULL;
490 sqp->s_sge.num_sge = 1;
491 qp->r_sge.sge = wqe->sg_list[0];
492 qp->r_sge.sg_list = wqe->sg_list + 1;
493 qp->r_sge.num_sge = wqe->wr.num_sge;
494 qp->r_sge.total_len = wqe->length;
495 break;
496
497 case IB_WR_ATOMIC_CMP_AND_SWP:
498 case IB_WR_ATOMIC_FETCH_AND_ADD:
499 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
500 goto inv_err;
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800501 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100502 wqe->atomic_wr.remote_addr,
503 wqe->atomic_wr.rkey,
Ralph Campbellf9315512010-05-23 21:44:54 -0700504 IB_ACCESS_REMOTE_ATOMIC)))
505 goto acc_err;
506 /* Perform atomic OP and save result. */
507 maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100508 sdata = wqe->atomic_wr.compare_add;
Ralph Campbellf9315512010-05-23 21:44:54 -0700509 *(u64 *) sqp->s_sge.sge.vaddr =
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100510 (wqe->atomic_wr.wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
Ralph Campbellf9315512010-05-23 21:44:54 -0700511 (u64) atomic64_add_return(sdata, maddr) - sdata :
512 (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100513 sdata, wqe->atomic_wr.swap);
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800514 rvt_put_mr(qp->r_sge.sge.mr);
Ralph Campbellf9315512010-05-23 21:44:54 -0700515 qp->r_sge.num_sge = 0;
516 goto send_comp;
517
518 default:
519 send_status = IB_WC_LOC_QP_OP_ERR;
520 goto serr;
521 }
522
523 sge = &sqp->s_sge.sge;
524 while (sqp->s_len) {
525 u32 len = sqp->s_len;
526
527 if (len > sge->length)
528 len = sge->length;
529 if (len > sge->sge_length)
530 len = sge->sge_length;
531 BUG_ON(len == 0);
532 qib_copy_sge(&qp->r_sge, sge->vaddr, len, release);
533 sge->vaddr += len;
534 sge->length -= len;
535 sge->sge_length -= len;
536 if (sge->sge_length == 0) {
537 if (!release)
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800538 rvt_put_mr(sge->mr);
Ralph Campbellf9315512010-05-23 21:44:54 -0700539 if (--sqp->s_sge.num_sge)
540 *sge = *sqp->s_sge.sg_list++;
541 } else if (sge->length == 0 && sge->mr->lkey) {
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800542 if (++sge->n >= RVT_SEGSZ) {
Ralph Campbellf9315512010-05-23 21:44:54 -0700543 if (++sge->m >= sge->mr->mapsz)
544 break;
545 sge->n = 0;
546 }
547 sge->vaddr =
548 sge->mr->map[sge->m]->segs[sge->n].vaddr;
549 sge->length =
550 sge->mr->map[sge->m]->segs[sge->n].length;
551 }
552 sqp->s_len -= len;
553 }
554 if (release)
Harish Chegondi70696ea2016-02-03 14:20:27 -0800555 rvt_put_ss(&qp->r_sge);
Ralph Campbellf9315512010-05-23 21:44:54 -0700556
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800557 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
Ralph Campbellf9315512010-05-23 21:44:54 -0700558 goto send_comp;
559
560 if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
561 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
562 else
563 wc.opcode = IB_WC_RECV;
564 wc.wr_id = qp->r_wr_id;
565 wc.status = IB_WC_SUCCESS;
566 wc.byte_len = wqe->length;
567 wc.qp = &qp->ibqp;
568 wc.src_qp = qp->remote_qpn;
569 wc.slid = qp->remote_ah_attr.dlid;
570 wc.sl = qp->remote_ah_attr.sl;
571 wc.port_num = 1;
572 /* Signal completion event if the solicited bit is set. */
Harish Chegondi4bb88e52016-01-22 13:07:36 -0800573 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
574 wqe->wr.send_flags & IB_SEND_SOLICITED);
Ralph Campbellf9315512010-05-23 21:44:54 -0700575
576send_comp:
577 spin_lock_irqsave(&sqp->s_lock, flags);
Harish Chegondif24a6d42016-01-22 12:56:02 -0800578 ibp->rvp.n_loop_pkts++;
Ralph Campbellf9315512010-05-23 21:44:54 -0700579flush_send:
580 sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
581 qib_send_complete(sqp, wqe, send_status);
582 goto again;
583
584rnr_nak:
585 /* Handle RNR NAK */
586 if (qp->ibqp.qp_type == IB_QPT_UC)
587 goto send_comp;
Harish Chegondif24a6d42016-01-22 12:56:02 -0800588 ibp->rvp.n_rnr_naks++;
Ralph Campbellf9315512010-05-23 21:44:54 -0700589 /*
590 * Note: we don't need the s_lock held since the BUSY flag
591 * makes this single threaded.
592 */
593 if (sqp->s_rnr_retry == 0) {
594 send_status = IB_WC_RNR_RETRY_EXC_ERR;
595 goto serr;
596 }
597 if (sqp->s_rnr_retry_cnt < 7)
598 sqp->s_rnr_retry--;
599 spin_lock_irqsave(&sqp->s_lock, flags);
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800600 if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK))
Ralph Campbellf9315512010-05-23 21:44:54 -0700601 goto clr_busy;
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800602 sqp->s_flags |= RVT_S_WAIT_RNR;
Ralph Campbellf9315512010-05-23 21:44:54 -0700603 sqp->s_timer.function = qib_rc_rnr_retry;
604 sqp->s_timer.expires = jiffies +
605 usecs_to_jiffies(ib_qib_rnr_table[qp->r_min_rnr_timer]);
606 add_timer(&sqp->s_timer);
607 goto clr_busy;
608
609op_err:
610 send_status = IB_WC_REM_OP_ERR;
611 wc.status = IB_WC_LOC_QP_OP_ERR;
612 goto err;
613
614inv_err:
615 send_status = IB_WC_REM_INV_REQ_ERR;
616 wc.status = IB_WC_LOC_QP_OP_ERR;
617 goto err;
618
619acc_err:
620 send_status = IB_WC_REM_ACCESS_ERR;
621 wc.status = IB_WC_LOC_PROT_ERR;
622err:
623 /* responder goes to error state */
624 qib_rc_error(qp, wc.status);
625
626serr:
627 spin_lock_irqsave(&sqp->s_lock, flags);
628 qib_send_complete(sqp, wqe, send_status);
629 if (sqp->ibqp.qp_type == IB_QPT_RC) {
Harish Chegondi70696ea2016-02-03 14:20:27 -0800630 int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
Ralph Campbellf9315512010-05-23 21:44:54 -0700631
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800632 sqp->s_flags &= ~RVT_S_BUSY;
Ralph Campbellf9315512010-05-23 21:44:54 -0700633 spin_unlock_irqrestore(&sqp->s_lock, flags);
634 if (lastwqe) {
635 struct ib_event ev;
636
637 ev.device = sqp->ibqp.device;
638 ev.element.qp = &sqp->ibqp;
639 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
640 sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
641 }
642 goto done;
643 }
644clr_busy:
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800645 sqp->s_flags &= ~RVT_S_BUSY;
Ralph Campbellf9315512010-05-23 21:44:54 -0700646unlock:
647 spin_unlock_irqrestore(&sqp->s_lock, flags);
648done:
Harish Chegondi1cefc2c2016-02-03 14:20:19 -0800649 rcu_read_unlock();
Ralph Campbellf9315512010-05-23 21:44:54 -0700650}
651
652/**
653 * qib_make_grh - construct a GRH header
654 * @ibp: a pointer to the IB port
655 * @hdr: a pointer to the GRH header being constructed
656 * @grh: the global route address to send to
657 * @hwords: the number of 32 bit words of header being sent
658 * @nwords: the number of 32 bit words of data being sent
659 *
660 * Return the size of the header in 32 bit words.
661 */
662u32 qib_make_grh(struct qib_ibport *ibp, struct ib_grh *hdr,
663 struct ib_global_route *grh, u32 hwords, u32 nwords)
664{
665 hdr->version_tclass_flow =
666 cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
667 (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
668 (grh->flow_label << IB_GRH_FLOW_SHIFT));
669 hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
670 /* next_hdr is defined by C8-7 in ch. 8.4.1 */
671 hdr->next_hdr = IB_GRH_NEXT_HDR;
672 hdr->hop_limit = grh->hop_limit;
673 /* The SGID is 32-bit aligned. */
Harish Chegondif24a6d42016-01-22 12:56:02 -0800674 hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
Ralph Campbellf9315512010-05-23 21:44:54 -0700675 hdr->sgid.global.interface_id = grh->sgid_index ?
676 ibp->guids[grh->sgid_index - 1] : ppd_from_ibp(ibp)->guid;
677 hdr->dgid = grh->dgid;
678
679 /* GRH header size in 32-bit words. */
680 return sizeof(struct ib_grh) / sizeof(u32);
681}
682
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800683void qib_make_ruc_header(struct rvt_qp *qp, struct qib_other_headers *ohdr,
Ralph Campbellf9315512010-05-23 21:44:54 -0700684 u32 bth0, u32 bth2)
685{
Dennis Dalessandroffc26902016-01-22 12:45:11 -0800686 struct qib_qp_priv *priv = qp->priv;
Ralph Campbellf9315512010-05-23 21:44:54 -0700687 struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
688 u16 lrh0;
689 u32 nwords;
690 u32 extra_bytes;
691
692 /* Construct the header. */
693 extra_bytes = -qp->s_cur_size & 3;
694 nwords = (qp->s_cur_size + extra_bytes) >> 2;
695 lrh0 = QIB_LRH_BTH;
696 if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
Dennis Dalessandroffc26902016-01-22 12:45:11 -0800697 qp->s_hdrwords += qib_make_grh(ibp, &priv->s_hdr->u.l.grh,
Ralph Campbellf9315512010-05-23 21:44:54 -0700698 &qp->remote_ah_attr.grh,
699 qp->s_hdrwords, nwords);
700 lrh0 = QIB_LRH_GRH;
701 }
702 lrh0 |= ibp->sl_to_vl[qp->remote_ah_attr.sl] << 12 |
703 qp->remote_ah_attr.sl << 4;
Dennis Dalessandroffc26902016-01-22 12:45:11 -0800704 priv->s_hdr->lrh[0] = cpu_to_be16(lrh0);
705 priv->s_hdr->lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
706 priv->s_hdr->lrh[2] =
707 cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
708 priv->s_hdr->lrh[3] = cpu_to_be16(ppd_from_ibp(ibp)->lid |
Ralph Campbellf9315512010-05-23 21:44:54 -0700709 qp->remote_ah_attr.src_path_bits);
710 bth0 |= qib_get_pkey(ibp, qp->s_pkey_index);
711 bth0 |= extra_bytes << 20;
712 if (qp->s_mig_state == IB_MIG_MIGRATED)
713 bth0 |= IB_BTH_MIG_REQ;
714 ohdr->bth[0] = cpu_to_be32(bth0);
715 ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
716 ohdr->bth[2] = cpu_to_be32(bth2);
Mike Marciniszyn7d7632a2014-03-07 08:40:55 -0500717 this_cpu_inc(ibp->pmastats->n_unicast_xmit);
Ralph Campbellf9315512010-05-23 21:44:54 -0700718}
719
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800720void _qib_do_send(struct work_struct *work)
721{
722 struct qib_qp_priv *priv = container_of(work, struct qib_qp_priv,
723 s_work);
724 struct rvt_qp *qp = priv->owner;
725
726 qib_do_send(qp);
727}
728
Ralph Campbellf9315512010-05-23 21:44:54 -0700729/**
730 * qib_do_send - perform a send on a QP
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800731 * @qp: pointer to the QP
Ralph Campbellf9315512010-05-23 21:44:54 -0700732 *
733 * Process entries in the send work queue until credit or queue is
734 * exhausted. Only allow one CPU to send a packet per QP (tasklet).
735 * Otherwise, two threads could send packets out of order.
736 */
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800737void qib_do_send(struct rvt_qp *qp)
Ralph Campbellf9315512010-05-23 21:44:54 -0700738{
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800739 struct qib_qp_priv *priv = qp->priv;
Ralph Campbellf9315512010-05-23 21:44:54 -0700740 struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
741 struct qib_pportdata *ppd = ppd_from_ibp(ibp);
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700742 int (*make_req)(struct rvt_qp *qp, unsigned long *flags);
Ralph Campbellf9315512010-05-23 21:44:54 -0700743 unsigned long flags;
744
745 if ((qp->ibqp.qp_type == IB_QPT_RC ||
746 qp->ibqp.qp_type == IB_QPT_UC) &&
747 (qp->remote_ah_attr.dlid & ~((1 << ppd->lmc) - 1)) == ppd->lid) {
748 qib_ruc_loopback(qp);
749 return;
750 }
751
752 if (qp->ibqp.qp_type == IB_QPT_RC)
753 make_req = qib_make_rc_req;
754 else if (qp->ibqp.qp_type == IB_QPT_UC)
755 make_req = qib_make_uc_req;
756 else
757 make_req = qib_make_ud_req;
758
759 spin_lock_irqsave(&qp->s_lock, flags);
760
761 /* Return if we are already busy processing a work request. */
762 if (!qib_send_ok(qp)) {
763 spin_unlock_irqrestore(&qp->s_lock, flags);
764 return;
765 }
766
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800767 qp->s_flags |= RVT_S_BUSY;
Ralph Campbellf9315512010-05-23 21:44:54 -0700768
Ralph Campbellf9315512010-05-23 21:44:54 -0700769 do {
770 /* Check for a constructed packet to be sent. */
771 if (qp->s_hdrwords != 0) {
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800772 spin_unlock_irqrestore(&qp->s_lock, flags);
Ralph Campbellf9315512010-05-23 21:44:54 -0700773 /*
774 * If the packet cannot be sent now, return and
775 * the send tasklet will be woken up later.
776 */
Dennis Dalessandroffc26902016-01-22 12:45:11 -0800777 if (qib_verbs_send(qp, priv->s_hdr, qp->s_hdrwords,
Ralph Campbellf9315512010-05-23 21:44:54 -0700778 qp->s_cur_sge, qp->s_cur_size))
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800779 return;
Ralph Campbellf9315512010-05-23 21:44:54 -0700780 /* Record that s_hdr is empty. */
781 qp->s_hdrwords = 0;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800782 spin_lock_irqsave(&qp->s_lock, flags);
Ralph Campbellf9315512010-05-23 21:44:54 -0700783 }
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700784 } while (make_req(qp, &flags));
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800785
786 spin_unlock_irqrestore(&qp->s_lock, flags);
Ralph Campbellf9315512010-05-23 21:44:54 -0700787}
788
789/*
790 * This should be called with s_lock held.
791 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800792void qib_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
Ralph Campbellf9315512010-05-23 21:44:54 -0700793 enum ib_wc_status status)
794{
795 u32 old_last, last;
796 unsigned i;
797
Harish Chegondidb3ef0e2016-01-22 13:07:42 -0800798 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
Ralph Campbellf9315512010-05-23 21:44:54 -0700799 return;
800
Mike Marciniszynee845412016-02-04 11:03:28 -0800801 last = qp->s_last;
802 old_last = last;
803 if (++last >= qp->s_size)
804 last = 0;
805 qp->s_last = last;
806 /* See post_send() */
807 barrier();
Ralph Campbellf9315512010-05-23 21:44:54 -0700808 for (i = 0; i < wqe->wr.num_sge; i++) {
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800809 struct rvt_sge *sge = &wqe->sg_list[i];
Ralph Campbellf9315512010-05-23 21:44:54 -0700810
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800811 rvt_put_mr(sge->mr);
Ralph Campbellf9315512010-05-23 21:44:54 -0700812 }
813 if (qp->ibqp.qp_type == IB_QPT_UD ||
814 qp->ibqp.qp_type == IB_QPT_SMI ||
815 qp->ibqp.qp_type == IB_QPT_GSI)
Dennis Dalessandro96ab1ac2016-01-22 12:46:07 -0800816 atomic_dec(&ibah_to_rvtah(wqe->ud_wr.ah)->refcount);
Ralph Campbellf9315512010-05-23 21:44:54 -0700817
818 /* See ch. 11.2.4.1 and 10.7.3.1 */
Harish Chegondi01ba79d2016-01-22 12:56:46 -0800819 if (!(qp->s_flags & RVT_S_SIGNAL_REQ_WR) ||
Ralph Campbellf9315512010-05-23 21:44:54 -0700820 (wqe->wr.send_flags & IB_SEND_SIGNALED) ||
821 status != IB_WC_SUCCESS) {
822 struct ib_wc wc;
823
Mike Marciniszyn041af0b2015-01-16 10:50:32 -0500824 memset(&wc, 0, sizeof(wc));
Ralph Campbellf9315512010-05-23 21:44:54 -0700825 wc.wr_id = wqe->wr.wr_id;
826 wc.status = status;
827 wc.opcode = ib_qib_wc_opcode[wqe->wr.opcode];
828 wc.qp = &qp->ibqp;
829 if (status == IB_WC_SUCCESS)
830 wc.byte_len = wqe->length;
Harish Chegondi4bb88e52016-01-22 13:07:36 -0800831 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.send_cq), &wc,
Ralph Campbellf9315512010-05-23 21:44:54 -0700832 status != IB_WC_SUCCESS);
833 }
834
Ralph Campbellf9315512010-05-23 21:44:54 -0700835 if (qp->s_acked == old_last)
836 qp->s_acked = last;
837 if (qp->s_cur == old_last)
838 qp->s_cur = last;
839 if (qp->s_tail == old_last)
840 qp->s_tail = last;
841 if (qp->state == IB_QPS_SQD && last == qp->s_cur)
842 qp->s_draining = 0;
843}