blob: a7add3c5d0f2b0a1ccb9b09f460b5d61582459b5 [file] [log] [blame]
Mike Marciniszyn77241052015-07-30 15:17:43 -04001/*
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2015 Intel Corporation.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * BSD LICENSE
20 *
21 * Copyright(c) 2015 Intel Corporation.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 *
27 * - Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * - Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in
31 * the documentation and/or other materials provided with the
32 * distribution.
33 * - Neither the name of Intel Corporation nor the names of its
34 * contributors may be used to endorse or promote products derived
35 * from this software without specific prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 *
49 */
50
51#include <linux/spinlock.h>
52
53#include "hfi.h"
54#include "mad.h"
55#include "qp.h"
56#include "sdma.h"
57
58/*
59 * Convert the AETH RNR timeout code into the number of microseconds.
60 */
61const u32 ib_hfi1_rnr_table[32] = {
62 655360, /* 00: 655.36 */
63 10, /* 01: .01 */
64 20, /* 02 .02 */
65 30, /* 03: .03 */
66 40, /* 04: .04 */
67 60, /* 05: .06 */
68 80, /* 06: .08 */
69 120, /* 07: .12 */
70 160, /* 08: .16 */
71 240, /* 09: .24 */
72 320, /* 0A: .32 */
73 480, /* 0B: .48 */
74 640, /* 0C: .64 */
75 960, /* 0D: .96 */
76 1280, /* 0E: 1.28 */
77 1920, /* 0F: 1.92 */
78 2560, /* 10: 2.56 */
79 3840, /* 11: 3.84 */
80 5120, /* 12: 5.12 */
81 7680, /* 13: 7.68 */
82 10240, /* 14: 10.24 */
83 15360, /* 15: 15.36 */
84 20480, /* 16: 20.48 */
85 30720, /* 17: 30.72 */
86 40960, /* 18: 40.96 */
87 61440, /* 19: 61.44 */
88 81920, /* 1A: 81.92 */
89 122880, /* 1B: 122.88 */
90 163840, /* 1C: 163.84 */
91 245760, /* 1D: 245.76 */
92 327680, /* 1E: 327.68 */
93 491520 /* 1F: 491.52 */
94};
95
96/*
97 * Validate a RWQE and fill in the SGE state.
98 * Return 1 if OK.
99 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800100static int init_sge(struct rvt_qp *qp, struct rvt_rwqe *wqe)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400101{
102 int i, j, ret;
103 struct ib_wc wc;
Dennis Dalessandrocd4ceee2016-01-19 14:41:55 -0800104 struct rvt_lkey_table *rkt;
Dennis Dalessandro4f87ccf2016-01-19 14:41:50 -0800105 struct rvt_pd *pd;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800106 struct rvt_sge_state *ss;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400107
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800108 rkt = &to_idev(qp->ibqp.device)->rdi.lkey_table;
Dennis Dalessandro4f87ccf2016-01-19 14:41:50 -0800109 pd = ibpd_to_rvtpd(qp->ibqp.srq ? qp->ibqp.srq->pd : qp->ibqp.pd);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400110 ss = &qp->r_sge;
111 ss->sg_list = qp->r_sg_list;
112 qp->r_len = 0;
113 for (i = j = 0; i < wqe->num_sge; i++) {
114 if (wqe->sg_list[i].length == 0)
115 continue;
116 /* Check LKEY */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800117 if (!rvt_lkey_ok(rkt, pd, j ? &ss->sg_list[j - 1] : &ss->sge,
118 &wqe->sg_list[i], IB_ACCESS_LOCAL_WRITE))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400119 goto bad_lkey;
120 qp->r_len += wqe->sg_list[i].length;
121 j++;
122 }
123 ss->num_sge = j;
124 ss->total_len = qp->r_len;
125 ret = 1;
126 goto bail;
127
128bad_lkey:
129 while (j) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800130 struct rvt_sge *sge = --j ? &ss->sg_list[j - 1] : &ss->sge;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400131
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800132 rvt_put_mr(sge->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400133 }
134 ss->num_sge = 0;
135 memset(&wc, 0, sizeof(wc));
136 wc.wr_id = wqe->wr_id;
137 wc.status = IB_WC_LOC_PROT_ERR;
138 wc.opcode = IB_WC_RECV;
139 wc.qp = &qp->ibqp;
140 /* Signal solicited completion event. */
Dennis Dalessandroabd712d2016-01-19 14:43:22 -0800141 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400142 ret = 0;
143bail:
144 return ret;
145}
146
147/**
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800148 * hfi1_rvt_get_rwqe - copy the next RWQE into the QP's RWQE
Mike Marciniszyn77241052015-07-30 15:17:43 -0400149 * @qp: the QP
150 * @wr_id_only: update qp->r_wr_id only, not qp->r_sge
151 *
152 * Return -1 if there is a local error, 0 if no RWQE is available,
153 * otherwise return 1.
154 *
155 * Can be called from interrupt level.
156 */
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800157int hfi1_rvt_get_rwqe(struct rvt_qp *qp, int wr_id_only)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400158{
159 unsigned long flags;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800160 struct rvt_rq *rq;
161 struct rvt_rwq *wq;
Dennis Dalessandro39db3e62016-01-19 14:42:33 -0800162 struct rvt_srq *srq;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800163 struct rvt_rwqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400164 void (*handler)(struct ib_event *, void *);
165 u32 tail;
166 int ret;
167
168 if (qp->ibqp.srq) {
Dennis Dalessandro39db3e62016-01-19 14:42:33 -0800169 srq = ibsrq_to_rvtsrq(qp->ibqp.srq);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400170 handler = srq->ibsrq.event_handler;
171 rq = &srq->rq;
172 } else {
173 srq = NULL;
174 handler = NULL;
175 rq = &qp->r_rq;
176 }
177
178 spin_lock_irqsave(&rq->lock, flags);
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800179 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400180 ret = 0;
181 goto unlock;
182 }
183
184 wq = rq->wq;
185 tail = wq->tail;
186 /* Validate tail before using it since it is user writable. */
187 if (tail >= rq->size)
188 tail = 0;
189 if (unlikely(tail == wq->head)) {
190 ret = 0;
191 goto unlock;
192 }
193 /* Make sure entry is read after head index is read. */
194 smp_rmb();
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800195 wqe = rvt_get_rwqe_ptr(rq, tail);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400196 /*
197 * Even though we update the tail index in memory, the verbs
198 * consumer is not supposed to post more entries until a
199 * completion is generated.
200 */
201 if (++tail >= rq->size)
202 tail = 0;
203 wq->tail = tail;
204 if (!wr_id_only && !init_sge(qp, wqe)) {
205 ret = -1;
206 goto unlock;
207 }
208 qp->r_wr_id = wqe->wr_id;
209
210 ret = 1;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800211 set_bit(RVT_R_WRID_VALID, &qp->r_aflags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400212 if (handler) {
213 u32 n;
214
215 /*
216 * Validate head pointer value and compute
217 * the number of remaining WQEs.
218 */
219 n = wq->head;
220 if (n >= rq->size)
221 n = 0;
222 if (n < tail)
223 n += rq->size - tail;
224 else
225 n -= tail;
226 if (n < srq->limit) {
227 struct ib_event ev;
228
229 srq->limit = 0;
230 spin_unlock_irqrestore(&rq->lock, flags);
231 ev.device = qp->ibqp.device;
232 ev.element.srq = qp->ibqp.srq;
233 ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
234 handler(&ev, srq->ibsrq.srq_context);
235 goto bail;
236 }
237 }
238unlock:
239 spin_unlock_irqrestore(&rq->lock, flags);
240bail:
241 return ret;
242}
243
Mike Marciniszyn77241052015-07-30 15:17:43 -0400244static __be64 get_sguid(struct hfi1_ibport *ibp, unsigned index)
245{
246 if (!index) {
247 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
248
249 return cpu_to_be64(ppd->guid);
250 }
251 return ibp->guids[index - 1];
252}
253
254static int gid_ok(union ib_gid *gid, __be64 gid_prefix, __be64 id)
255{
256 return (gid->global.interface_id == id &&
257 (gid->global.subnet_prefix == gid_prefix ||
258 gid->global.subnet_prefix == IB_DEFAULT_GID_PREFIX));
259}
260
261/*
262 *
263 * This should be called with the QP r_lock held.
264 *
265 * The s_lock will be acquired around the hfi1_migrate_qp() call.
266 */
267int hfi1_ruc_check_hdr(struct hfi1_ibport *ibp, struct hfi1_ib_header *hdr,
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800268 int has_grh, struct rvt_qp *qp, u32 bth0)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400269{
270 __be64 guid;
271 unsigned long flags;
272 u8 sc5 = ibp->sl_to_sc[qp->remote_ah_attr.sl];
273
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);
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800282 if (!gid_ok(&hdr->u.l.grh.dgid, ibp->rvp.gid_prefix,
283 guid))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400284 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 (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), (u16)bth0,
291 sc5, be16_to_cpu(hdr->lrh[3])))) {
Erik E. Kahn5cd24112015-12-10 09:59:40 -0500292 hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_P_KEY,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400293 (u16)bth0,
294 (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
295 0, qp->ibqp.qp_num,
Erik E. Kahn5cd24112015-12-10 09:59:40 -0500296 be16_to_cpu(hdr->lrh[3]),
297 be16_to_cpu(hdr->lrh[1]));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400298 goto err;
299 }
300 /* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */
301 if (be16_to_cpu(hdr->lrh[3]) != qp->alt_ah_attr.dlid ||
302 ppd_from_ibp(ibp)->port != qp->alt_ah_attr.port_num)
303 goto err;
304 spin_lock_irqsave(&qp->s_lock, flags);
305 hfi1_migrate_qp(qp);
306 spin_unlock_irqrestore(&qp->s_lock, flags);
307 } else {
308 if (!has_grh) {
309 if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
310 goto err;
311 } else {
312 if (!(qp->remote_ah_attr.ah_flags & IB_AH_GRH))
313 goto err;
314 guid = get_sguid(ibp,
315 qp->remote_ah_attr.grh.sgid_index);
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800316 if (!gid_ok(&hdr->u.l.grh.dgid, ibp->rvp.gid_prefix,
317 guid))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400318 goto err;
319 if (!gid_ok(&hdr->u.l.grh.sgid,
320 qp->remote_ah_attr.grh.dgid.global.subnet_prefix,
321 qp->remote_ah_attr.grh.dgid.global.interface_id))
322 goto err;
323 }
324 if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), (u16)bth0,
325 sc5, be16_to_cpu(hdr->lrh[3])))) {
Erik E. Kahn5cd24112015-12-10 09:59:40 -0500326 hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_P_KEY,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400327 (u16)bth0,
328 (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
329 0, qp->ibqp.qp_num,
Erik E. Kahn5cd24112015-12-10 09:59:40 -0500330 be16_to_cpu(hdr->lrh[3]),
331 be16_to_cpu(hdr->lrh[1]));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400332 goto err;
333 }
334 /* Validate the SLID. See Ch. 9.6.1.5 */
335 if (be16_to_cpu(hdr->lrh[3]) != qp->remote_ah_attr.dlid ||
336 ppd_from_ibp(ibp)->port != qp->port_num)
337 goto err;
338 if (qp->s_mig_state == IB_MIG_REARM &&
339 !(bth0 & IB_BTH_MIG_REQ))
340 qp->s_mig_state = IB_MIG_ARMED;
341 }
342
343 return 0;
344
345err:
346 return 1;
347}
348
349/**
350 * ruc_loopback - handle UC and RC loopback requests
351 * @sqp: the sending QP
352 *
353 * This is called from hfi1_do_send() to
354 * forward a WQE addressed to the same HFI.
355 * Note that although we are single threaded due to the tasklet, we still
356 * have to protect against post_send(). We don't have to worry about
357 * receive interrupts since this is a connected protocol and all packets
358 * will pass through here.
359 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800360static void ruc_loopback(struct rvt_qp *sqp)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400361{
362 struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800363 struct rvt_qp *qp;
364 struct rvt_swqe *wqe;
365 struct rvt_sge *sge;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400366 unsigned long flags;
367 struct ib_wc wc;
368 u64 sdata;
369 atomic64_t *maddr;
370 enum ib_wc_status send_status;
371 int release;
372 int ret;
Dean Luick7b0b01a2016-02-03 14:35:49 -0800373 int copy_last = 0;
Mike Marciniszyn34cee282016-02-09 14:29:31 -0800374 u32 to;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400375
376 rcu_read_lock();
377
378 /*
379 * Note that we check the responder QP state after
380 * checking the requester's state.
381 */
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800382 qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
383 sqp->remote_qpn);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400384
385 spin_lock_irqsave(&sqp->s_lock, flags);
386
387 /* Return if we are already busy processing a work request. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800388 if ((sqp->s_flags & (RVT_S_BUSY | RVT_S_ANY_WAIT)) ||
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800389 !(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_OR_FLUSH_SEND))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400390 goto unlock;
391
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800392 sqp->s_flags |= RVT_S_BUSY;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400393
394again:
395 if (sqp->s_last == sqp->s_head)
396 goto clr_busy;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800397 wqe = rvt_get_swqe_ptr(sqp, sqp->s_last);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400398
399 /* Return if it is not OK to start a new work request. */
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800400 if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
401 if (!(ib_rvt_state_ops[sqp->state] & RVT_FLUSH_SEND))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400402 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
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800419 if (!qp || !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) ||
Mike Marciniszyn77241052015-07-30 15:17:43 -0400420 qp->ibqp.qp_type != sqp->ibqp.qp_type) {
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800421 ibp->rvp.n_pkt_drops++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400422 /*
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
433 memset(&wc, 0, sizeof(wc));
434 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:
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800447 ret = hfi1_rvt_get_rwqe(qp, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400448 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;
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800459 ret = hfi1_rvt_get_rwqe(qp, 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400460 if (ret < 0)
461 goto op_err;
462 if (!ret)
463 goto rnr_nak;
Dean Luick7b0b01a2016-02-03 14:35:49 -0800464 /* skip copy_last set and qp_access_flags recheck */
465 goto do_write;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400466 case IB_WR_RDMA_WRITE:
Dean Luick7b0b01a2016-02-03 14:35:49 -0800467 copy_last = ibpd_to_rvtpd(qp->ibqp.pd)->user;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400468 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
469 goto inv_err;
Dean Luick7b0b01a2016-02-03 14:35:49 -0800470do_write:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400471 if (wqe->length == 0)
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800472 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
473 wqe->rdma_wr.remote_addr,
474 wqe->rdma_wr.rkey,
475 IB_ACCESS_REMOTE_WRITE)))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400476 goto acc_err;
477 qp->r_sge.sg_list = NULL;
478 qp->r_sge.num_sge = 1;
479 qp->r_sge.total_len = wqe->length;
480 break;
481
482 case IB_WR_RDMA_READ:
483 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
484 goto inv_err;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800485 if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
486 wqe->rdma_wr.remote_addr,
487 wqe->rdma_wr.rkey,
488 IB_ACCESS_REMOTE_READ)))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400489 goto acc_err;
490 release = 0;
491 sqp->s_sge.sg_list = NULL;
492 sqp->s_sge.num_sge = 1;
493 qp->r_sge.sge = wqe->sg_list[0];
494 qp->r_sge.sg_list = wqe->sg_list + 1;
495 qp->r_sge.num_sge = wqe->wr.num_sge;
496 qp->r_sge.total_len = wqe->length;
497 break;
498
499 case IB_WR_ATOMIC_CMP_AND_SWP:
500 case IB_WR_ATOMIC_FETCH_AND_ADD:
501 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
502 goto inv_err;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800503 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
504 wqe->atomic_wr.remote_addr,
505 wqe->atomic_wr.rkey,
506 IB_ACCESS_REMOTE_ATOMIC)))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400507 goto acc_err;
508 /* Perform atomic OP and save result. */
509 maddr = (atomic64_t *) qp->r_sge.sge.vaddr;
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100510 sdata = wqe->atomic_wr.compare_add;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400511 *(u64 *) sqp->s_sge.sge.vaddr =
512 (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
513 (u64) atomic64_add_return(sdata, maddr) - sdata :
514 (u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100515 sdata, wqe->atomic_wr.swap);
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800516 rvt_put_mr(qp->r_sge.sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400517 qp->r_sge.num_sge = 0;
518 goto send_comp;
519
520 default:
521 send_status = IB_WC_LOC_QP_OP_ERR;
522 goto serr;
523 }
524
525 sge = &sqp->s_sge.sge;
526 while (sqp->s_len) {
527 u32 len = sqp->s_len;
528
529 if (len > sge->length)
530 len = sge->length;
531 if (len > sge->sge_length)
532 len = sge->sge_length;
533 WARN_ON_ONCE(len == 0);
Dean Luick7b0b01a2016-02-03 14:35:49 -0800534 hfi1_copy_sge(&qp->r_sge, sge->vaddr, len, release, copy_last);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400535 sge->vaddr += len;
536 sge->length -= len;
537 sge->sge_length -= len;
538 if (sge->sge_length == 0) {
539 if (!release)
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800540 rvt_put_mr(sge->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400541 if (--sqp->s_sge.num_sge)
542 *sge = *sqp->s_sge.sg_list++;
543 } else if (sge->length == 0 && sge->mr->lkey) {
Dennis Dalessandrocd4ceee2016-01-19 14:41:55 -0800544 if (++sge->n >= RVT_SEGSZ) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400545 if (++sge->m >= sge->mr->mapsz)
546 break;
547 sge->n = 0;
548 }
549 sge->vaddr =
550 sge->mr->map[sge->m]->segs[sge->n].vaddr;
551 sge->length =
552 sge->mr->map[sge->m]->segs[sge->n].length;
553 }
554 sqp->s_len -= len;
555 }
556 if (release)
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800557 rvt_put_ss(&qp->r_sge);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400558
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800559 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400560 goto send_comp;
561
562 if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
563 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
564 else
565 wc.opcode = IB_WC_RECV;
566 wc.wr_id = qp->r_wr_id;
567 wc.status = IB_WC_SUCCESS;
568 wc.byte_len = wqe->length;
569 wc.qp = &qp->ibqp;
570 wc.src_qp = qp->remote_qpn;
571 wc.slid = qp->remote_ah_attr.dlid;
572 wc.sl = qp->remote_ah_attr.sl;
573 wc.port_num = 1;
574 /* Signal completion event if the solicited bit is set. */
Dennis Dalessandroabd712d2016-01-19 14:43:22 -0800575 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
576 wqe->wr.send_flags & IB_SEND_SOLICITED);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400577
578send_comp:
579 spin_lock_irqsave(&sqp->s_lock, flags);
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800580 ibp->rvp.n_loop_pkts++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400581flush_send:
582 sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
583 hfi1_send_complete(sqp, wqe, send_status);
584 goto again;
585
586rnr_nak:
587 /* Handle RNR NAK */
588 if (qp->ibqp.qp_type == IB_QPT_UC)
589 goto send_comp;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800590 ibp->rvp.n_rnr_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400591 /*
592 * Note: we don't need the s_lock held since the BUSY flag
593 * makes this single threaded.
594 */
595 if (sqp->s_rnr_retry == 0) {
596 send_status = IB_WC_RNR_RETRY_EXC_ERR;
597 goto serr;
598 }
599 if (sqp->s_rnr_retry_cnt < 7)
600 sqp->s_rnr_retry--;
601 spin_lock_irqsave(&sqp->s_lock, flags);
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800602 if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400603 goto clr_busy;
Mike Marciniszyn34cee282016-02-09 14:29:31 -0800604 to = ib_hfi1_rnr_table[qp->r_min_rnr_timer];
605 hfi1_add_rnr_timer(sqp, to);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400606 goto clr_busy;
607
608op_err:
609 send_status = IB_WC_REM_OP_ERR;
610 wc.status = IB_WC_LOC_QP_OP_ERR;
611 goto err;
612
613inv_err:
614 send_status = IB_WC_REM_INV_REQ_ERR;
615 wc.status = IB_WC_LOC_QP_OP_ERR;
616 goto err;
617
618acc_err:
619 send_status = IB_WC_REM_ACCESS_ERR;
620 wc.status = IB_WC_LOC_PROT_ERR;
621err:
622 /* responder goes to error state */
623 hfi1_rc_error(qp, wc.status);
624
625serr:
626 spin_lock_irqsave(&sqp->s_lock, flags);
627 hfi1_send_complete(sqp, wqe, send_status);
628 if (sqp->ibqp.qp_type == IB_QPT_RC) {
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800629 int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400630
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800631 sqp->s_flags &= ~RVT_S_BUSY;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400632 spin_unlock_irqrestore(&sqp->s_lock, flags);
633 if (lastwqe) {
634 struct ib_event ev;
635
636 ev.device = sqp->ibqp.device;
637 ev.element.qp = &sqp->ibqp;
638 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
639 sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
640 }
641 goto done;
642 }
643clr_busy:
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800644 sqp->s_flags &= ~RVT_S_BUSY;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400645unlock:
646 spin_unlock_irqrestore(&sqp->s_lock, flags);
647done:
648 rcu_read_unlock();
649}
650
651/**
652 * hfi1_make_grh - construct a GRH header
653 * @ibp: a pointer to the IB port
654 * @hdr: a pointer to the GRH header being constructed
655 * @grh: the global route address to send to
656 * @hwords: the number of 32 bit words of header being sent
657 * @nwords: the number of 32 bit words of data being sent
658 *
659 * Return the size of the header in 32 bit words.
660 */
661u32 hfi1_make_grh(struct hfi1_ibport *ibp, struct ib_grh *hdr,
662 struct ib_global_route *grh, u32 hwords, u32 nwords)
663{
664 hdr->version_tclass_flow =
665 cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
666 (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
667 (grh->flow_label << IB_GRH_FLOW_SHIFT));
668 hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
669 /* next_hdr is defined by C8-7 in ch. 8.4.1 */
670 hdr->next_hdr = IB_GRH_NEXT_HDR;
671 hdr->hop_limit = grh->hop_limit;
672 /* The SGID is 32-bit aligned. */
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800673 hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400674 hdr->sgid.global.interface_id =
675 grh->sgid_index && grh->sgid_index < ARRAY_SIZE(ibp->guids) ?
676 ibp->guids[grh->sgid_index - 1] :
677 cpu_to_be64(ppd_from_ibp(ibp)->guid);
678 hdr->dgid = grh->dgid;
679
680 /* GRH header size in 32-bit words. */
681 return sizeof(struct ib_grh) / sizeof(u32);
682}
683
Mike Marciniszyn77241052015-07-30 15:17:43 -0400684#define BTH2_OFFSET (offsetof(struct hfi1_pio_header, hdr.u.oth.bth[2]) / 4)
685
686/**
687 * build_ahg - create ahg in s_hdr
688 * @qp: a pointer to QP
689 * @npsn: the next PSN for the request/response
690 *
691 * This routine handles the AHG by allocating an ahg entry and causing the
692 * copy of the first middle.
693 *
694 * Subsequent middles use the copied entry, editing the
695 * PSN with 1 or 2 edits.
696 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800697static inline void build_ahg(struct rvt_qp *qp, u32 npsn)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400698{
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800699 struct hfi1_qp_priv *priv = qp->priv;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800700 if (unlikely(qp->s_flags & RVT_S_AHG_CLEAR))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400701 clear_ahg(qp);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800702 if (!(qp->s_flags & RVT_S_AHG_VALID)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400703 /* first middle that needs copy */
Mike Marciniszynd7b8ba52015-11-09 19:13:59 -0500704 if (qp->s_ahgidx < 0)
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800705 qp->s_ahgidx = sdma_ahg_alloc(priv->s_sde);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400706 if (qp->s_ahgidx >= 0) {
707 qp->s_ahgpsn = npsn;
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800708 priv->s_hdr->tx_flags |= SDMA_TXREQ_F_AHG_COPY;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400709 /* save to protect a change in another thread */
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800710 priv->s_hdr->sde = priv->s_sde;
711 priv->s_hdr->ahgidx = qp->s_ahgidx;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800712 qp->s_flags |= RVT_S_AHG_VALID;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400713 }
714 } else {
715 /* subsequent middle after valid */
716 if (qp->s_ahgidx >= 0) {
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800717 priv->s_hdr->tx_flags |= SDMA_TXREQ_F_USE_AHG;
718 priv->s_hdr->ahgidx = qp->s_ahgidx;
719 priv->s_hdr->ahgcount++;
720 priv->s_hdr->ahgdesc[0] =
Mike Marciniszyn77241052015-07-30 15:17:43 -0400721 sdma_build_ahg_descriptor(
722 (__force u16)cpu_to_be16((u16)npsn),
723 BTH2_OFFSET,
724 16,
725 16);
726 if ((npsn & 0xffff0000) !=
727 (qp->s_ahgpsn & 0xffff0000)) {
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800728 priv->s_hdr->ahgcount++;
729 priv->s_hdr->ahgdesc[1] =
Mike Marciniszyn77241052015-07-30 15:17:43 -0400730 sdma_build_ahg_descriptor(
731 (__force u16)cpu_to_be16(
732 (u16)(npsn >> 16)),
733 BTH2_OFFSET,
734 0,
735 16);
736 }
737 }
738 }
739}
740
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800741void hfi1_make_ruc_header(struct rvt_qp *qp, struct hfi1_other_headers *ohdr,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400742 u32 bth0, u32 bth2, int middle)
743{
744 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800745 struct hfi1_qp_priv *priv = qp->priv;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400746 u16 lrh0;
747 u32 nwords;
748 u32 extra_bytes;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400749 u32 bth1;
750
751 /* Construct the header. */
752 extra_bytes = -qp->s_cur_size & 3;
753 nwords = (qp->s_cur_size + extra_bytes) >> 2;
754 lrh0 = HFI1_LRH_BTH;
755 if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800756 qp->s_hdrwords += hfi1_make_grh(ibp, &priv->s_hdr->ibh.u.l.grh,
757 &qp->remote_ah_attr.grh,
758 qp->s_hdrwords, nwords);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400759 lrh0 = HFI1_LRH_GRH;
760 middle = 0;
761 }
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800762 lrh0 |= (priv->s_sc & 0xf) << 12 | (qp->remote_ah_attr.sl & 0xf) << 4;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400763 /*
764 * reset s_hdr/AHG fields
765 *
766 * This insures that the ahgentry/ahgcount
767 * are at a non-AHG default to protect
768 * build_verbs_tx_desc() from using
769 * an include ahgidx.
770 *
771 * build_ahg() will modify as appropriate
772 * to use the AHG feature.
773 */
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800774 priv->s_hdr->tx_flags = 0;
775 priv->s_hdr->ahgcount = 0;
776 priv->s_hdr->ahgidx = 0;
777 priv->s_hdr->sde = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400778 if (qp->s_mig_state == IB_MIG_MIGRATED)
779 bth0 |= IB_BTH_MIG_REQ;
780 else
781 middle = 0;
782 if (middle)
783 build_ahg(qp, bth2);
784 else
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800785 qp->s_flags &= ~RVT_S_AHG_VALID;
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800786 priv->s_hdr->ibh.lrh[0] = cpu_to_be16(lrh0);
787 priv->s_hdr->ibh.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
788 priv->s_hdr->ibh.lrh[2] =
Mike Marciniszyn77241052015-07-30 15:17:43 -0400789 cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800790 priv->s_hdr->ibh.lrh[3] = cpu_to_be16(ppd_from_ibp(ibp)->lid |
Mike Marciniszyn77241052015-07-30 15:17:43 -0400791 qp->remote_ah_attr.src_path_bits);
792 bth0 |= hfi1_get_pkey(ibp, qp->s_pkey_index);
793 bth0 |= extra_bytes << 20;
794 ohdr->bth[0] = cpu_to_be32(bth0);
795 bth1 = qp->remote_qpn;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800796 if (qp->s_flags & RVT_S_ECN) {
797 qp->s_flags &= ~RVT_S_ECN;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400798 /* we recently received a FECN, so return a BECN */
799 bth1 |= (HFI1_BECN_MASK << HFI1_BECN_SHIFT);
800 }
801 ohdr->bth[1] = cpu_to_be32(bth1);
802 ohdr->bth[2] = cpu_to_be32(bth2);
803}
804
Dean Luickb4219222015-10-26 10:28:35 -0400805/* when sending, force a reschedule every one of these periods */
806#define SEND_RESCHED_TIMEOUT (5 * HZ) /* 5s in jiffies */
807
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800808void _hfi1_do_send(struct work_struct *work)
809{
810 struct iowait *wait = container_of(work, struct iowait, iowork);
811 struct rvt_qp *qp = iowait_to_qp(wait);
812
813 hfi1_do_send(qp);
814}
815
Mike Marciniszyn77241052015-07-30 15:17:43 -0400816/**
817 * hfi1_do_send - perform a send on a QP
818 * @work: contains a pointer to the QP
819 *
820 * Process entries in the send work queue until credit or queue is
821 * exhausted. Only allow one CPU to send a packet per QP (tasklet).
822 * Otherwise, two threads could send packets out of order.
823 */
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800824void hfi1_do_send(struct rvt_qp *qp)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400825{
Dennis Dalessandrod46e5142015-11-11 00:34:37 -0500826 struct hfi1_pkt_state ps;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800827 struct hfi1_qp_priv *priv = qp->priv;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800828 int (*make_req)(struct rvt_qp *qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400829 unsigned long flags;
Dean Luickb4219222015-10-26 10:28:35 -0400830 unsigned long timeout;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800831 unsigned long timeout_int;
832 int cpu;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400833
Dennis Dalessandrod46e5142015-11-11 00:34:37 -0500834 ps.dev = to_idev(qp->ibqp.device);
835 ps.ibp = to_iport(qp->ibqp.device, qp->port_num);
836 ps.ppd = ppd_from_ibp(ps.ibp);
837
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800838 switch (qp->ibqp.qp_type) {
839 case IB_QPT_RC:
840 if (!loopback && ((qp->remote_ah_attr.dlid & ~((1 << ps.ppd->lmc
841 ) - 1)) ==
842 ps.ppd->lid)) {
843 ruc_loopback(qp);
844 return;
845 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400846 make_req = hfi1_make_rc_req;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800847 timeout_int = (qp->timeout_jiffies);
848 break;
849 case IB_QPT_UC:
850 if (!loopback && ((qp->remote_ah_attr.dlid & ~((1 << ps.ppd->lmc
851 ) - 1)) ==
852 ps.ppd->lid)) {
853 ruc_loopback(qp);
854 return;
855 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400856 make_req = hfi1_make_uc_req;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800857 timeout_int = SEND_RESCHED_TIMEOUT;
858 break;
859 default:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400860 make_req = hfi1_make_ud_req;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800861 timeout_int = SEND_RESCHED_TIMEOUT;
862 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400863
864 spin_lock_irqsave(&qp->s_lock, flags);
865
866 /* Return if we are already busy processing a work request. */
867 if (!hfi1_send_ok(qp)) {
868 spin_unlock_irqrestore(&qp->s_lock, flags);
869 return;
870 }
871
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800872 qp->s_flags |= RVT_S_BUSY;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400873
874 spin_unlock_irqrestore(&qp->s_lock, flags);
875
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800876 timeout = jiffies + (timeout_int) / 8;
877 cpu = priv->s_sde ? priv->s_sde->cpu :
878 cpumask_first(cpumask_of_node(ps.ppd->dd->node));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400879 do {
880 /* Check for a constructed packet to be sent. */
881 if (qp->s_hdrwords != 0) {
882 /*
883 * If the packet cannot be sent now, return and
884 * the send tasklet will be woken up later.
885 */
Dennis Dalessandrod46e5142015-11-11 00:34:37 -0500886 if (hfi1_verbs_send(qp, &ps))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400887 break;
888 /* Record that s_hdr is empty. */
889 qp->s_hdrwords = 0;
890 }
Dean Luickb4219222015-10-26 10:28:35 -0400891
892 /* allow other tasks to run */
893 if (unlikely(time_after(jiffies, timeout))) {
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800894 if (workqueue_congested(cpu, ps.ppd->hfi1_wq)) {
895 spin_lock_irqsave(&qp->s_lock, flags);
896 qp->s_flags &= ~RVT_S_BUSY;
897 hfi1_schedule_send(qp);
898 spin_unlock_irqrestore(&qp->s_lock,
899 flags);
900 this_cpu_inc(*ps.ppd->dd->send_schedule);
901 return;
902 }
Dean Luickb4219222015-10-26 10:28:35 -0400903 cond_resched();
Vennila Megavannan89abfc82016-02-03 14:34:07 -0800904 this_cpu_inc(*ps.ppd->dd->send_schedule);
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800905 timeout = jiffies + (timeout_int) / 8;
Dean Luickb4219222015-10-26 10:28:35 -0400906 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400907 } while (make_req(qp));
908}
909
910/*
911 * This should be called with s_lock held.
912 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800913void hfi1_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400914 enum ib_wc_status status)
915{
916 u32 old_last, last;
917 unsigned i;
918
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800919 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400920 return;
921
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -0800922 last = qp->s_last;
923 old_last = last;
924 if (++last >= qp->s_size)
925 last = 0;
926 qp->s_last = last;
927 /* See post_send() */
928 barrier();
Mike Marciniszyn77241052015-07-30 15:17:43 -0400929 for (i = 0; i < wqe->wr.num_sge; i++) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800930 struct rvt_sge *sge = &wqe->sg_list[i];
Mike Marciniszyn77241052015-07-30 15:17:43 -0400931
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800932 rvt_put_mr(sge->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400933 }
934 if (qp->ibqp.qp_type == IB_QPT_UD ||
935 qp->ibqp.qp_type == IB_QPT_SMI ||
936 qp->ibqp.qp_type == IB_QPT_GSI)
Dennis Dalessandro15723f02016-01-19 14:42:17 -0800937 atomic_dec(&ibah_to_rvtah(wqe->ud_wr.ah)->refcount);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400938
939 /* See ch. 11.2.4.1 and 10.7.3.1 */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800940 if (!(qp->s_flags & RVT_S_SIGNAL_REQ_WR) ||
Mike Marciniszyn77241052015-07-30 15:17:43 -0400941 (wqe->wr.send_flags & IB_SEND_SIGNALED) ||
942 status != IB_WC_SUCCESS) {
943 struct ib_wc wc;
944
945 memset(&wc, 0, sizeof(wc));
946 wc.wr_id = wqe->wr.wr_id;
947 wc.status = status;
948 wc.opcode = ib_hfi1_wc_opcode[wqe->wr.opcode];
949 wc.qp = &qp->ibqp;
950 if (status == IB_WC_SUCCESS)
951 wc.byte_len = wqe->length;
Dennis Dalessandroabd712d2016-01-19 14:43:22 -0800952 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.send_cq), &wc,
953 status != IB_WC_SUCCESS);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400954 }
955
Mike Marciniszyn77241052015-07-30 15:17:43 -0400956 if (qp->s_acked == old_last)
957 qp->s_acked = last;
958 if (qp->s_cur == old_last)
959 qp->s_cur = last;
960 if (qp->s_tail == old_last)
961 qp->s_tail = last;
962 if (qp->state == IB_QPS_SQD && last == qp->s_cur)
963 qp->s_draining = 0;
964}