blob: ccf8d8037355f914ce1aea3c19c4fb7b7e8a6963 [file] [log] [blame]
Mike Marciniszyn77241052015-07-30 15:17:43 -04001/*
Mike Marciniszynb6eac932017-04-09 10:16:35 -07002 * Copyright(c) 2015 - 2017 Intel Corporation.
Mike Marciniszyn77241052015-07-30 15:17:43 -04003 *
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
6 *
7 * GPL LICENSE SUMMARY
8 *
Mike Marciniszyn77241052015-07-30 15:17:43 -04009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * BSD LICENSE
19 *
Mike Marciniszyn77241052015-07-30 15:17:43 -040020 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
29 * distribution.
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 *
46 */
47
48#include <linux/spinlock.h>
49
50#include "hfi.h"
51#include "mad.h"
52#include "qp.h"
Mike Marciniszyn45842ab2016-02-14 12:44:34 -080053#include "verbs_txreq.h"
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -080054#include "trace.h"
Mike Marciniszyn77241052015-07-30 15:17:43 -040055
56/*
Mike Marciniszyn77241052015-07-30 15:17:43 -040057 * Validate a RWQE and fill in the SGE state.
58 * Return 1 if OK.
59 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -080060static int init_sge(struct rvt_qp *qp, struct rvt_rwqe *wqe)
Mike Marciniszyn77241052015-07-30 15:17:43 -040061{
62 int i, j, ret;
63 struct ib_wc wc;
Dennis Dalessandrocd4ceee2016-01-19 14:41:55 -080064 struct rvt_lkey_table *rkt;
Dennis Dalessandro4f87ccf2016-01-19 14:41:50 -080065 struct rvt_pd *pd;
Dennis Dalessandro895420d2016-01-19 14:42:28 -080066 struct rvt_sge_state *ss;
Mike Marciniszyn77241052015-07-30 15:17:43 -040067
Dennis Dalessandro895420d2016-01-19 14:42:28 -080068 rkt = &to_idev(qp->ibqp.device)->rdi.lkey_table;
Dennis Dalessandro4f87ccf2016-01-19 14:41:50 -080069 pd = ibpd_to_rvtpd(qp->ibqp.srq ? qp->ibqp.srq->pd : qp->ibqp.pd);
Mike Marciniszyn77241052015-07-30 15:17:43 -040070 ss = &qp->r_sge;
71 ss->sg_list = qp->r_sg_list;
72 qp->r_len = 0;
73 for (i = j = 0; i < wqe->num_sge; i++) {
74 if (wqe->sg_list[i].length == 0)
75 continue;
76 /* Check LKEY */
Dennis Dalessandro895420d2016-01-19 14:42:28 -080077 if (!rvt_lkey_ok(rkt, pd, j ? &ss->sg_list[j - 1] : &ss->sge,
78 &wqe->sg_list[i], IB_ACCESS_LOCAL_WRITE))
Mike Marciniszyn77241052015-07-30 15:17:43 -040079 goto bad_lkey;
80 qp->r_len += wqe->sg_list[i].length;
81 j++;
82 }
83 ss->num_sge = j;
84 ss->total_len = qp->r_len;
85 ret = 1;
86 goto bail;
87
88bad_lkey:
89 while (j) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -080090 struct rvt_sge *sge = --j ? &ss->sg_list[j - 1] : &ss->sge;
Mike Marciniszyn77241052015-07-30 15:17:43 -040091
Dennis Dalessandro895420d2016-01-19 14:42:28 -080092 rvt_put_mr(sge->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -040093 }
94 ss->num_sge = 0;
95 memset(&wc, 0, sizeof(wc));
96 wc.wr_id = wqe->wr_id;
97 wc.status = IB_WC_LOC_PROT_ERR;
98 wc.opcode = IB_WC_RECV;
99 wc.qp = &qp->ibqp;
100 /* Signal solicited completion event. */
Dennis Dalessandroabd712d2016-01-19 14:43:22 -0800101 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400102 ret = 0;
103bail:
104 return ret;
105}
106
107/**
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800108 * hfi1_rvt_get_rwqe - copy the next RWQE into the QP's RWQE
Mike Marciniszyn77241052015-07-30 15:17:43 -0400109 * @qp: the QP
110 * @wr_id_only: update qp->r_wr_id only, not qp->r_sge
111 *
112 * Return -1 if there is a local error, 0 if no RWQE is available,
113 * otherwise return 1.
114 *
115 * Can be called from interrupt level.
116 */
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800117int hfi1_rvt_get_rwqe(struct rvt_qp *qp, int wr_id_only)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400118{
119 unsigned long flags;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800120 struct rvt_rq *rq;
121 struct rvt_rwq *wq;
Dennis Dalessandro39db3e62016-01-19 14:42:33 -0800122 struct rvt_srq *srq;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800123 struct rvt_rwqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400124 void (*handler)(struct ib_event *, void *);
125 u32 tail;
126 int ret;
127
128 if (qp->ibqp.srq) {
Dennis Dalessandro39db3e62016-01-19 14:42:33 -0800129 srq = ibsrq_to_rvtsrq(qp->ibqp.srq);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400130 handler = srq->ibsrq.event_handler;
131 rq = &srq->rq;
132 } else {
133 srq = NULL;
134 handler = NULL;
135 rq = &qp->r_rq;
136 }
137
138 spin_lock_irqsave(&rq->lock, flags);
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800139 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400140 ret = 0;
141 goto unlock;
142 }
143
144 wq = rq->wq;
145 tail = wq->tail;
146 /* Validate tail before using it since it is user writable. */
147 if (tail >= rq->size)
148 tail = 0;
149 if (unlikely(tail == wq->head)) {
150 ret = 0;
151 goto unlock;
152 }
153 /* Make sure entry is read after head index is read. */
154 smp_rmb();
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800155 wqe = rvt_get_rwqe_ptr(rq, tail);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400156 /*
157 * Even though we update the tail index in memory, the verbs
158 * consumer is not supposed to post more entries until a
159 * completion is generated.
160 */
161 if (++tail >= rq->size)
162 tail = 0;
163 wq->tail = tail;
164 if (!wr_id_only && !init_sge(qp, wqe)) {
165 ret = -1;
166 goto unlock;
167 }
168 qp->r_wr_id = wqe->wr_id;
169
170 ret = 1;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800171 set_bit(RVT_R_WRID_VALID, &qp->r_aflags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400172 if (handler) {
173 u32 n;
174
175 /*
176 * Validate head pointer value and compute
177 * the number of remaining WQEs.
178 */
179 n = wq->head;
180 if (n >= rq->size)
181 n = 0;
182 if (n < tail)
183 n += rq->size - tail;
184 else
185 n -= tail;
186 if (n < srq->limit) {
187 struct ib_event ev;
188
189 srq->limit = 0;
190 spin_unlock_irqrestore(&rq->lock, flags);
191 ev.device = qp->ibqp.device;
192 ev.element.srq = qp->ibqp.srq;
193 ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
194 handler(&ev, srq->ibsrq.srq_context);
195 goto bail;
196 }
197 }
198unlock:
199 spin_unlock_irqrestore(&rq->lock, flags);
200bail:
201 return ret;
202}
203
Mike Marciniszyn77241052015-07-30 15:17:43 -0400204static int gid_ok(union ib_gid *gid, __be64 gid_prefix, __be64 id)
205{
206 return (gid->global.interface_id == id &&
207 (gid->global.subnet_prefix == gid_prefix ||
208 gid->global.subnet_prefix == IB_DEFAULT_GID_PREFIX));
209}
210
211/*
212 *
213 * This should be called with the QP r_lock held.
214 *
215 * The s_lock will be acquired around the hfi1_migrate_qp() call.
216 */
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700217int hfi1_ruc_check_hdr(struct hfi1_ibport *ibp, struct ib_header *hdr,
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800218 int has_grh, struct rvt_qp *qp, u32 bth0)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400219{
220 __be64 guid;
221 unsigned long flags;
222 u8 sc5 = ibp->sl_to_sc[qp->remote_ah_attr.sl];
223
224 if (qp->s_mig_state == IB_MIG_ARMED && (bth0 & IB_BTH_MIG_REQ)) {
225 if (!has_grh) {
226 if (qp->alt_ah_attr.ah_flags & IB_AH_GRH)
227 goto err;
228 } else {
229 if (!(qp->alt_ah_attr.ah_flags & IB_AH_GRH))
230 goto err;
231 guid = get_sguid(ibp, qp->alt_ah_attr.grh.sgid_index);
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800232 if (!gid_ok(&hdr->u.l.grh.dgid, ibp->rvp.gid_prefix,
233 guid))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400234 goto err;
Jubin John17fb4f22016-02-14 20:21:52 -0800235 if (!gid_ok(
236 &hdr->u.l.grh.sgid,
237 qp->alt_ah_attr.grh.dgid.global.subnet_prefix,
238 qp->alt_ah_attr.grh.dgid.global.interface_id))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400239 goto err;
240 }
Don Hiattcb4270572017-04-09 10:16:22 -0700241 if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), (u16)bth0, sc5,
242 ib_get_slid(hdr)))) {
Erik E. Kahn5cd24112015-12-10 09:59:40 -0500243 hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_P_KEY,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400244 (u16)bth0,
Don Hiattcb4270572017-04-09 10:16:22 -0700245 ib_get_sl(hdr),
Mike Marciniszyn77241052015-07-30 15:17:43 -0400246 0, qp->ibqp.qp_num,
Don Hiattcb4270572017-04-09 10:16:22 -0700247 ib_get_slid(hdr),
248 ib_get_dlid(hdr));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400249 goto err;
250 }
251 /* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */
Don Hiattcb4270572017-04-09 10:16:22 -0700252 if (ib_get_slid(hdr) != qp->alt_ah_attr.dlid ||
Mike Marciniszyn77241052015-07-30 15:17:43 -0400253 ppd_from_ibp(ibp)->port != qp->alt_ah_attr.port_num)
254 goto err;
255 spin_lock_irqsave(&qp->s_lock, flags);
256 hfi1_migrate_qp(qp);
257 spin_unlock_irqrestore(&qp->s_lock, flags);
258 } else {
259 if (!has_grh) {
260 if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)
261 goto err;
262 } else {
263 if (!(qp->remote_ah_attr.ah_flags & IB_AH_GRH))
264 goto err;
265 guid = get_sguid(ibp,
266 qp->remote_ah_attr.grh.sgid_index);
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800267 if (!gid_ok(&hdr->u.l.grh.dgid, ibp->rvp.gid_prefix,
268 guid))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400269 goto err;
Jubin John17fb4f22016-02-14 20:21:52 -0800270 if (!gid_ok(
271 &hdr->u.l.grh.sgid,
272 qp->remote_ah_attr.grh.dgid.global.subnet_prefix,
273 qp->remote_ah_attr.grh.dgid.global.interface_id))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400274 goto err;
275 }
Don Hiattcb4270572017-04-09 10:16:22 -0700276 if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), (u16)bth0, sc5,
277 ib_get_slid(hdr)))) {
Erik E. Kahn5cd24112015-12-10 09:59:40 -0500278 hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_P_KEY,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400279 (u16)bth0,
Don Hiattcb4270572017-04-09 10:16:22 -0700280 ib_get_sl(hdr),
Mike Marciniszyn77241052015-07-30 15:17:43 -0400281 0, qp->ibqp.qp_num,
Don Hiattcb4270572017-04-09 10:16:22 -0700282 ib_get_slid(hdr),
283 ib_get_dlid(hdr));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400284 goto err;
285 }
286 /* Validate the SLID. See Ch. 9.6.1.5 */
Don Hiattcb4270572017-04-09 10:16:22 -0700287 if (ib_get_slid(hdr) != qp->remote_ah_attr.dlid ||
Mike Marciniszyn77241052015-07-30 15:17:43 -0400288 ppd_from_ibp(ibp)->port != qp->port_num)
289 goto err;
290 if (qp->s_mig_state == IB_MIG_REARM &&
291 !(bth0 & IB_BTH_MIG_REQ))
292 qp->s_mig_state = IB_MIG_ARMED;
293 }
294
295 return 0;
296
297err:
298 return 1;
299}
300
301/**
302 * ruc_loopback - handle UC and RC loopback requests
303 * @sqp: the sending QP
304 *
305 * This is called from hfi1_do_send() to
306 * forward a WQE addressed to the same HFI.
Dennis Dalessandroca00c622016-09-25 07:42:08 -0700307 * Note that although we are single threaded due to the send engine, we still
Mike Marciniszyn77241052015-07-30 15:17:43 -0400308 * have to protect against post_send(). We don't have to worry about
309 * receive interrupts since this is a connected protocol and all packets
310 * will pass through here.
311 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800312static void ruc_loopback(struct rvt_qp *sqp)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400313{
314 struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800315 struct rvt_qp *qp;
316 struct rvt_swqe *wqe;
317 struct rvt_sge *sge;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400318 unsigned long flags;
319 struct ib_wc wc;
320 u64 sdata;
321 atomic64_t *maddr;
322 enum ib_wc_status send_status;
Brian Welty0128fce2017-02-08 05:27:31 -0800323 bool release;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400324 int ret;
Brian Welty0128fce2017-02-08 05:27:31 -0800325 bool copy_last = false;
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700326 int local_ops = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400327
328 rcu_read_lock();
329
330 /*
331 * Note that we check the responder QP state after
332 * checking the requester's state.
333 */
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800334 qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
335 sqp->remote_qpn);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400336
337 spin_lock_irqsave(&sqp->s_lock, flags);
338
339 /* Return if we are already busy processing a work request. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800340 if ((sqp->s_flags & (RVT_S_BUSY | RVT_S_ANY_WAIT)) ||
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800341 !(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_OR_FLUSH_SEND))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400342 goto unlock;
343
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800344 sqp->s_flags |= RVT_S_BUSY;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400345
346again:
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800347 smp_read_barrier_depends(); /* see post_one_send() */
348 if (sqp->s_last == ACCESS_ONCE(sqp->s_head))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400349 goto clr_busy;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800350 wqe = rvt_get_swqe_ptr(sqp, sqp->s_last);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400351
352 /* Return if it is not OK to start a new work request. */
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800353 if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
354 if (!(ib_rvt_state_ops[sqp->state] & RVT_FLUSH_SEND))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400355 goto clr_busy;
356 /* We are in the error state, flush the work request. */
357 send_status = IB_WC_WR_FLUSH_ERR;
358 goto flush_send;
359 }
360
361 /*
362 * We can rely on the entry not changing without the s_lock
363 * being held until we update s_last.
364 * We increment s_cur to indicate s_last is in progress.
365 */
366 if (sqp->s_last == sqp->s_cur) {
367 if (++sqp->s_cur >= sqp->s_size)
368 sqp->s_cur = 0;
369 }
370 spin_unlock_irqrestore(&sqp->s_lock, flags);
371
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800372 if (!qp || !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) ||
Mike Marciniszyn77241052015-07-30 15:17:43 -0400373 qp->ibqp.qp_type != sqp->ibqp.qp_type) {
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800374 ibp->rvp.n_pkt_drops++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400375 /*
376 * For RC, the requester would timeout and retry so
377 * shortcut the timeouts and just signal too many retries.
378 */
379 if (sqp->ibqp.qp_type == IB_QPT_RC)
380 send_status = IB_WC_RETRY_EXC_ERR;
381 else
382 send_status = IB_WC_SUCCESS;
383 goto serr;
384 }
385
386 memset(&wc, 0, sizeof(wc));
387 send_status = IB_WC_SUCCESS;
388
Brian Welty0128fce2017-02-08 05:27:31 -0800389 release = true;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400390 sqp->s_sge.sge = wqe->sg_list[0];
391 sqp->s_sge.sg_list = wqe->sg_list + 1;
392 sqp->s_sge.num_sge = wqe->wr.num_sge;
393 sqp->s_len = wqe->length;
394 switch (wqe->wr.opcode) {
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700395 case IB_WR_REG_MR:
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700396 goto send_comp;
397
398 case IB_WR_LOCAL_INV:
Jianxin Xiongd9b13c22016-07-25 13:39:45 -0700399 if (!(wqe->wr.send_flags & RVT_SEND_COMPLETION_ONLY)) {
400 if (rvt_invalidate_rkey(sqp,
401 wqe->wr.ex.invalidate_rkey))
402 send_status = IB_WC_LOC_PROT_ERR;
403 local_ops = 1;
404 }
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700405 goto send_comp;
406
407 case IB_WR_SEND_WITH_INV:
408 if (!rvt_invalidate_rkey(qp, wqe->wr.ex.invalidate_rkey)) {
409 wc.wc_flags = IB_WC_WITH_INVALIDATE;
410 wc.ex.invalidate_rkey = wqe->wr.ex.invalidate_rkey;
411 }
412 goto send;
413
Mike Marciniszyn77241052015-07-30 15:17:43 -0400414 case IB_WR_SEND_WITH_IMM:
415 wc.wc_flags = IB_WC_WITH_IMM;
416 wc.ex.imm_data = wqe->wr.ex.imm_data;
417 /* FALLTHROUGH */
418 case IB_WR_SEND:
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700419send:
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800420 ret = hfi1_rvt_get_rwqe(qp, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400421 if (ret < 0)
422 goto op_err;
423 if (!ret)
424 goto rnr_nak;
425 break;
426
427 case IB_WR_RDMA_WRITE_WITH_IMM:
428 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
429 goto inv_err;
430 wc.wc_flags = IB_WC_WITH_IMM;
431 wc.ex.imm_data = wqe->wr.ex.imm_data;
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800432 ret = hfi1_rvt_get_rwqe(qp, 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400433 if (ret < 0)
434 goto op_err;
435 if (!ret)
436 goto rnr_nak;
Dean Luick7b0b01a2016-02-03 14:35:49 -0800437 /* skip copy_last set and qp_access_flags recheck */
438 goto do_write;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400439 case IB_WR_RDMA_WRITE:
Brian Welty0128fce2017-02-08 05:27:31 -0800440 copy_last = rvt_is_user_qp(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400441 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
442 goto inv_err;
Dean Luick7b0b01a2016-02-03 14:35:49 -0800443do_write:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400444 if (wqe->length == 0)
Harish Chegondi42d6ec12016-03-05 08:49:24 -0800445 break;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800446 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
447 wqe->rdma_wr.remote_addr,
448 wqe->rdma_wr.rkey,
449 IB_ACCESS_REMOTE_WRITE)))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400450 goto acc_err;
451 qp->r_sge.sg_list = NULL;
452 qp->r_sge.num_sge = 1;
453 qp->r_sge.total_len = wqe->length;
454 break;
455
456 case IB_WR_RDMA_READ:
457 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
458 goto inv_err;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800459 if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
460 wqe->rdma_wr.remote_addr,
461 wqe->rdma_wr.rkey,
462 IB_ACCESS_REMOTE_READ)))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400463 goto acc_err;
Brian Welty0128fce2017-02-08 05:27:31 -0800464 release = false;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400465 sqp->s_sge.sg_list = NULL;
466 sqp->s_sge.num_sge = 1;
467 qp->r_sge.sge = wqe->sg_list[0];
468 qp->r_sge.sg_list = wqe->sg_list + 1;
469 qp->r_sge.num_sge = wqe->wr.num_sge;
470 qp->r_sge.total_len = wqe->length;
471 break;
472
473 case IB_WR_ATOMIC_CMP_AND_SWP:
474 case IB_WR_ATOMIC_FETCH_AND_ADD:
475 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
476 goto inv_err;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800477 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
478 wqe->atomic_wr.remote_addr,
479 wqe->atomic_wr.rkey,
480 IB_ACCESS_REMOTE_ATOMIC)))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400481 goto acc_err;
482 /* Perform atomic OP and save result. */
Jubin John50e5dcb2016-02-14 20:19:41 -0800483 maddr = (atomic64_t *)qp->r_sge.sge.vaddr;
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100484 sdata = wqe->atomic_wr.compare_add;
Jubin John50e5dcb2016-02-14 20:19:41 -0800485 *(u64 *)sqp->s_sge.sge.vaddr =
Mike Marciniszyn77241052015-07-30 15:17:43 -0400486 (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
Jubin John50e5dcb2016-02-14 20:19:41 -0800487 (u64)atomic64_add_return(sdata, maddr) - sdata :
488 (u64)cmpxchg((u64 *)qp->r_sge.sge.vaddr,
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100489 sdata, wqe->atomic_wr.swap);
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800490 rvt_put_mr(qp->r_sge.sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400491 qp->r_sge.num_sge = 0;
492 goto send_comp;
493
494 default:
495 send_status = IB_WC_LOC_QP_OP_ERR;
496 goto serr;
497 }
498
499 sge = &sqp->s_sge.sge;
500 while (sqp->s_len) {
501 u32 len = sqp->s_len;
502
503 if (len > sge->length)
504 len = sge->length;
505 if (len > sge->sge_length)
506 len = sge->sge_length;
507 WARN_ON_ONCE(len == 0);
Dean Luick7b0b01a2016-02-03 14:35:49 -0800508 hfi1_copy_sge(&qp->r_sge, sge->vaddr, len, release, copy_last);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400509 sge->vaddr += len;
510 sge->length -= len;
511 sge->sge_length -= len;
512 if (sge->sge_length == 0) {
513 if (!release)
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800514 rvt_put_mr(sge->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400515 if (--sqp->s_sge.num_sge)
516 *sge = *sqp->s_sge.sg_list++;
517 } else if (sge->length == 0 && sge->mr->lkey) {
Dennis Dalessandrocd4ceee2016-01-19 14:41:55 -0800518 if (++sge->n >= RVT_SEGSZ) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400519 if (++sge->m >= sge->mr->mapsz)
520 break;
521 sge->n = 0;
522 }
523 sge->vaddr =
524 sge->mr->map[sge->m]->segs[sge->n].vaddr;
525 sge->length =
526 sge->mr->map[sge->m]->segs[sge->n].length;
527 }
528 sqp->s_len -= len;
529 }
530 if (release)
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800531 rvt_put_ss(&qp->r_sge);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400532
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800533 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400534 goto send_comp;
535
536 if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
537 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
538 else
539 wc.opcode = IB_WC_RECV;
540 wc.wr_id = qp->r_wr_id;
541 wc.status = IB_WC_SUCCESS;
542 wc.byte_len = wqe->length;
543 wc.qp = &qp->ibqp;
544 wc.src_qp = qp->remote_qpn;
545 wc.slid = qp->remote_ah_attr.dlid;
546 wc.sl = qp->remote_ah_attr.sl;
547 wc.port_num = 1;
548 /* Signal completion event if the solicited bit is set. */
Dennis Dalessandroabd712d2016-01-19 14:43:22 -0800549 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
550 wqe->wr.send_flags & IB_SEND_SOLICITED);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400551
552send_comp:
553 spin_lock_irqsave(&sqp->s_lock, flags);
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800554 ibp->rvp.n_loop_pkts++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400555flush_send:
556 sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
557 hfi1_send_complete(sqp, wqe, send_status);
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700558 if (local_ops) {
559 atomic_dec(&sqp->local_ops_pending);
560 local_ops = 0;
561 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400562 goto again;
563
564rnr_nak:
565 /* Handle RNR NAK */
566 if (qp->ibqp.qp_type == IB_QPT_UC)
567 goto send_comp;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800568 ibp->rvp.n_rnr_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400569 /*
570 * Note: we don't need the s_lock held since the BUSY flag
571 * makes this single threaded.
572 */
573 if (sqp->s_rnr_retry == 0) {
574 send_status = IB_WC_RNR_RETRY_EXC_ERR;
575 goto serr;
576 }
577 if (sqp->s_rnr_retry_cnt < 7)
578 sqp->s_rnr_retry--;
579 spin_lock_irqsave(&sqp->s_lock, flags);
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800580 if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400581 goto clr_busy;
Venkata Sandeep Dhanalakota56acbbf2017-02-08 05:27:19 -0800582 rvt_add_rnr_timer(sqp, qp->r_min_rnr_timer <<
Don Hiatt832666c2017-02-08 05:28:25 -0800583 IB_AETH_CREDIT_SHIFT);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400584 goto clr_busy;
585
586op_err:
587 send_status = IB_WC_REM_OP_ERR;
588 wc.status = IB_WC_LOC_QP_OP_ERR;
589 goto err;
590
591inv_err:
592 send_status = IB_WC_REM_INV_REQ_ERR;
593 wc.status = IB_WC_LOC_QP_OP_ERR;
594 goto err;
595
596acc_err:
597 send_status = IB_WC_REM_ACCESS_ERR;
598 wc.status = IB_WC_LOC_PROT_ERR;
599err:
600 /* responder goes to error state */
Brian Weltybeb5a042017-02-08 05:27:01 -0800601 rvt_rc_error(qp, wc.status);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400602
603serr:
604 spin_lock_irqsave(&sqp->s_lock, flags);
605 hfi1_send_complete(sqp, wqe, send_status);
606 if (sqp->ibqp.qp_type == IB_QPT_RC) {
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800607 int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400608
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800609 sqp->s_flags &= ~RVT_S_BUSY;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400610 spin_unlock_irqrestore(&sqp->s_lock, flags);
611 if (lastwqe) {
612 struct ib_event ev;
613
614 ev.device = sqp->ibqp.device;
615 ev.element.qp = &sqp->ibqp;
616 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
617 sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
618 }
619 goto done;
620 }
621clr_busy:
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800622 sqp->s_flags &= ~RVT_S_BUSY;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400623unlock:
624 spin_unlock_irqrestore(&sqp->s_lock, flags);
625done:
626 rcu_read_unlock();
627}
628
629/**
630 * hfi1_make_grh - construct a GRH header
631 * @ibp: a pointer to the IB port
632 * @hdr: a pointer to the GRH header being constructed
633 * @grh: the global route address to send to
634 * @hwords: the number of 32 bit words of header being sent
635 * @nwords: the number of 32 bit words of data being sent
636 *
637 * Return the size of the header in 32 bit words.
638 */
639u32 hfi1_make_grh(struct hfi1_ibport *ibp, struct ib_grh *hdr,
640 struct ib_global_route *grh, u32 hwords, u32 nwords)
641{
642 hdr->version_tclass_flow =
643 cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
644 (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
645 (grh->flow_label << IB_GRH_FLOW_SHIFT));
646 hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
647 /* next_hdr is defined by C8-7 in ch. 8.4.1 */
648 hdr->next_hdr = IB_GRH_NEXT_HDR;
649 hdr->hop_limit = grh->hop_limit;
650 /* The SGID is 32-bit aligned. */
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800651 hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400652 hdr->sgid.global.interface_id =
Jakub Pawlaka6cd5f02016-10-17 04:19:30 -0700653 grh->sgid_index < HFI1_GUIDS_PER_PORT ?
654 get_sguid(ibp, grh->sgid_index) :
655 get_sguid(ibp, HFI1_PORT_GUID_INDEX);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400656 hdr->dgid = grh->dgid;
657
658 /* GRH header size in 32-bit words. */
659 return sizeof(struct ib_grh) / sizeof(u32);
660}
661
Don Hiattd4d602e2016-07-25 13:40:22 -0700662#define BTH2_OFFSET (offsetof(struct hfi1_sdma_header, hdr.u.oth.bth[2]) / 4)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400663
664/**
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700665 * build_ahg - create ahg in s_ahg
Mike Marciniszyn77241052015-07-30 15:17:43 -0400666 * @qp: a pointer to QP
667 * @npsn: the next PSN for the request/response
668 *
669 * This routine handles the AHG by allocating an ahg entry and causing the
670 * copy of the first middle.
671 *
672 * Subsequent middles use the copied entry, editing the
673 * PSN with 1 or 2 edits.
674 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800675static inline void build_ahg(struct rvt_qp *qp, u32 npsn)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400676{
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800677 struct hfi1_qp_priv *priv = qp->priv;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800678
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800679 if (unlikely(qp->s_flags & RVT_S_AHG_CLEAR))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400680 clear_ahg(qp);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800681 if (!(qp->s_flags & RVT_S_AHG_VALID)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400682 /* first middle that needs copy */
Mike Marciniszynd7b8ba52015-11-09 19:13:59 -0500683 if (qp->s_ahgidx < 0)
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800684 qp->s_ahgidx = sdma_ahg_alloc(priv->s_sde);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400685 if (qp->s_ahgidx >= 0) {
686 qp->s_ahgpsn = npsn;
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700687 priv->s_ahg->tx_flags |= SDMA_TXREQ_F_AHG_COPY;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400688 /* save to protect a change in another thread */
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700689 priv->s_ahg->ahgidx = qp->s_ahgidx;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800690 qp->s_flags |= RVT_S_AHG_VALID;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400691 }
692 } else {
693 /* subsequent middle after valid */
694 if (qp->s_ahgidx >= 0) {
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700695 priv->s_ahg->tx_flags |= SDMA_TXREQ_F_USE_AHG;
696 priv->s_ahg->ahgidx = qp->s_ahgidx;
697 priv->s_ahg->ahgcount++;
698 priv->s_ahg->ahgdesc[0] =
Mike Marciniszyn77241052015-07-30 15:17:43 -0400699 sdma_build_ahg_descriptor(
700 (__force u16)cpu_to_be16((u16)npsn),
701 BTH2_OFFSET,
702 16,
703 16);
704 if ((npsn & 0xffff0000) !=
705 (qp->s_ahgpsn & 0xffff0000)) {
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700706 priv->s_ahg->ahgcount++;
707 priv->s_ahg->ahgdesc[1] =
Mike Marciniszyn77241052015-07-30 15:17:43 -0400708 sdma_build_ahg_descriptor(
709 (__force u16)cpu_to_be16(
710 (u16)(npsn >> 16)),
711 BTH2_OFFSET,
712 0,
713 16);
714 }
715 }
716 }
717}
718
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700719void hfi1_make_ruc_header(struct rvt_qp *qp, struct ib_other_headers *ohdr,
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800720 u32 bth0, u32 bth2, int middle,
721 struct hfi1_pkt_state *ps)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400722{
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800723 struct hfi1_qp_priv *priv = qp->priv;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800724 struct hfi1_ibport *ibp = ps->ibp;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400725 u16 lrh0;
726 u32 nwords;
727 u32 extra_bytes;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400728 u32 bth1;
729
730 /* Construct the header. */
Don Hiatte922ae02016-12-07 19:33:00 -0800731 extra_bytes = -ps->s_txreq->s_cur_size & 3;
732 nwords = (ps->s_txreq->s_cur_size + extra_bytes) >> 2;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400733 lrh0 = HFI1_LRH_BTH;
734 if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800735 qp->s_hdrwords += hfi1_make_grh(ibp,
736 &ps->s_txreq->phdr.hdr.u.l.grh,
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800737 &qp->remote_ah_attr.grh,
738 qp->s_hdrwords, nwords);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400739 lrh0 = HFI1_LRH_GRH;
740 middle = 0;
741 }
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800742 lrh0 |= (priv->s_sc & 0xf) << 12 | (qp->remote_ah_attr.sl & 0xf) << 4;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400743 /*
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700744 * reset s_ahg/AHG fields
Mike Marciniszyn77241052015-07-30 15:17:43 -0400745 *
746 * This insures that the ahgentry/ahgcount
747 * are at a non-AHG default to protect
748 * build_verbs_tx_desc() from using
749 * an include ahgidx.
750 *
751 * build_ahg() will modify as appropriate
752 * to use the AHG feature.
753 */
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700754 priv->s_ahg->tx_flags = 0;
755 priv->s_ahg->ahgcount = 0;
756 priv->s_ahg->ahgidx = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400757 if (qp->s_mig_state == IB_MIG_MIGRATED)
758 bth0 |= IB_BTH_MIG_REQ;
759 else
760 middle = 0;
761 if (middle)
762 build_ahg(qp, bth2);
763 else
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800764 qp->s_flags &= ~RVT_S_AHG_VALID;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800765 ps->s_txreq->phdr.hdr.lrh[0] = cpu_to_be16(lrh0);
766 ps->s_txreq->phdr.hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);
767 ps->s_txreq->phdr.hdr.lrh[2] =
Mike Marciniszyn77241052015-07-30 15:17:43 -0400768 cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800769 ps->s_txreq->phdr.hdr.lrh[3] = cpu_to_be16(ppd_from_ibp(ibp)->lid |
Mike Marciniszyn77241052015-07-30 15:17:43 -0400770 qp->remote_ah_attr.src_path_bits);
771 bth0 |= hfi1_get_pkey(ibp, qp->s_pkey_index);
772 bth0 |= extra_bytes << 20;
773 ohdr->bth[0] = cpu_to_be32(bth0);
774 bth1 = qp->remote_qpn;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800775 if (qp->s_flags & RVT_S_ECN) {
776 qp->s_flags &= ~RVT_S_ECN;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400777 /* we recently received a FECN, so return a BECN */
Don Hiatt3d591092017-04-09 10:16:28 -0700778 bth1 |= (IB_BECN_MASK << IB_BECN_SHIFT);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400779 }
780 ohdr->bth[1] = cpu_to_be32(bth1);
781 ohdr->bth[2] = cpu_to_be32(bth2);
782}
783
Dean Luickb4219222015-10-26 10:28:35 -0400784/* when sending, force a reschedule every one of these periods */
785#define SEND_RESCHED_TIMEOUT (5 * HZ) /* 5s in jiffies */
786
Mike Marciniszynb6eac932017-04-09 10:16:35 -0700787void hfi1_do_send_from_rvt(struct rvt_qp *qp)
788{
789 hfi1_do_send(qp, false);
790}
791
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800792void _hfi1_do_send(struct work_struct *work)
793{
794 struct iowait *wait = container_of(work, struct iowait, iowork);
795 struct rvt_qp *qp = iowait_to_qp(wait);
796
Mike Marciniszynb6eac932017-04-09 10:16:35 -0700797 hfi1_do_send(qp, true);
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800798}
799
Mike Marciniszyn77241052015-07-30 15:17:43 -0400800/**
801 * hfi1_do_send - perform a send on a QP
802 * @work: contains a pointer to the QP
Mike Marciniszynb6eac932017-04-09 10:16:35 -0700803 * @in_thread: true if in a workqueue thread
Mike Marciniszyn77241052015-07-30 15:17:43 -0400804 *
805 * Process entries in the send work queue until credit or queue is
Dennis Dalessandroca00c622016-09-25 07:42:08 -0700806 * exhausted. Only allow one CPU to send a packet per QP.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400807 * Otherwise, two threads could send packets out of order.
808 */
Mike Marciniszynb6eac932017-04-09 10:16:35 -0700809void hfi1_do_send(struct rvt_qp *qp, bool in_thread)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400810{
Dennis Dalessandrod46e5142015-11-11 00:34:37 -0500811 struct hfi1_pkt_state ps;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800812 struct hfi1_qp_priv *priv = qp->priv;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800813 int (*make_req)(struct rvt_qp *qp, struct hfi1_pkt_state *ps);
Dean Luickb4219222015-10-26 10:28:35 -0400814 unsigned long timeout;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800815 unsigned long timeout_int;
816 int cpu;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400817
Dennis Dalessandrod46e5142015-11-11 00:34:37 -0500818 ps.dev = to_idev(qp->ibqp.device);
819 ps.ibp = to_iport(qp->ibqp.device, qp->port_num);
820 ps.ppd = ppd_from_ibp(ps.ibp);
821
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800822 switch (qp->ibqp.qp_type) {
823 case IB_QPT_RC:
824 if (!loopback && ((qp->remote_ah_attr.dlid & ~((1 << ps.ppd->lmc
825 ) - 1)) ==
826 ps.ppd->lid)) {
827 ruc_loopback(qp);
828 return;
829 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400830 make_req = hfi1_make_rc_req;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800831 timeout_int = (qp->timeout_jiffies);
832 break;
833 case IB_QPT_UC:
834 if (!loopback && ((qp->remote_ah_attr.dlid & ~((1 << ps.ppd->lmc
835 ) - 1)) ==
836 ps.ppd->lid)) {
837 ruc_loopback(qp);
838 return;
839 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400840 make_req = hfi1_make_uc_req;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800841 timeout_int = SEND_RESCHED_TIMEOUT;
842 break;
843 default:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400844 make_req = hfi1_make_ud_req;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800845 timeout_int = SEND_RESCHED_TIMEOUT;
846 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400847
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700848 spin_lock_irqsave(&qp->s_lock, ps.flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400849
850 /* Return if we are already busy processing a work request. */
851 if (!hfi1_send_ok(qp)) {
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700852 spin_unlock_irqrestore(&qp->s_lock, ps.flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400853 return;
854 }
855
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800856 qp->s_flags |= RVT_S_BUSY;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400857
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800858 timeout = jiffies + (timeout_int) / 8;
859 cpu = priv->s_sde ? priv->s_sde->cpu :
860 cpumask_first(cpumask_of_node(ps.ppd->dd->node));
Mike Marciniszyn711e1042016-02-14 12:45:18 -0800861 /* insure a pre-built packet is handled */
862 ps.s_txreq = get_waiting_verbs_txreq(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400863 do {
864 /* Check for a constructed packet to be sent. */
865 if (qp->s_hdrwords != 0) {
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700866 spin_unlock_irqrestore(&qp->s_lock, ps.flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400867 /*
868 * If the packet cannot be sent now, return and
Dennis Dalessandroca00c622016-09-25 07:42:08 -0700869 * the send engine will be woken up later.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400870 */
Dennis Dalessandrod46e5142015-11-11 00:34:37 -0500871 if (hfi1_verbs_send(qp, &ps))
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800872 return;
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700873 /* Record that s_ahg is empty. */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400874 qp->s_hdrwords = 0;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800875 /* allow other tasks to run */
876 if (unlikely(time_after(jiffies, timeout))) {
Mike Marciniszynb6eac932017-04-09 10:16:35 -0700877 if (!in_thread ||
878 workqueue_congested(
879 cpu,
880 ps.ppd->hfi1_wq)) {
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700881 spin_lock_irqsave(
882 &qp->s_lock,
883 ps.flags);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800884 qp->s_flags &= ~RVT_S_BUSY;
885 hfi1_schedule_send(qp);
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700886 spin_unlock_irqrestore(
887 &qp->s_lock,
888 ps.flags);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800889 this_cpu_inc(
890 *ps.ppd->dd->send_schedule);
891 return;
892 }
Mike Marciniszynb6eac932017-04-09 10:16:35 -0700893 cond_resched();
894 this_cpu_inc(
895 *ps.ppd->dd->send_schedule);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800896 timeout = jiffies + (timeout_int) / 8;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800897 }
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700898 spin_lock_irqsave(&qp->s_lock, ps.flags);
Dean Luickb4219222015-10-26 10:28:35 -0400899 }
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800900 } while (make_req(qp, &ps));
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800901
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700902 spin_unlock_irqrestore(&qp->s_lock, ps.flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400903}
904
905/*
906 * This should be called with s_lock held.
907 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800908void hfi1_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400909 enum ib_wc_status status)
910{
911 u32 old_last, last;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400912
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800913 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400914 return;
915
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -0800916 last = qp->s_last;
917 old_last = last;
Mike Marciniszyn9260b352017-03-20 17:25:23 -0700918 trace_hfi1_qp_send_completion(qp, wqe, last);
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -0800919 if (++last >= qp->s_size)
920 last = 0;
Mike Marciniszyn9260b352017-03-20 17:25:23 -0700921 trace_hfi1_qp_send_completion(qp, wqe, last);
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -0800922 qp->s_last = last;
923 /* See post_send() */
924 barrier();
Mike Marciniszync64607a2016-12-07 19:34:31 -0800925 rvt_put_swqe(wqe);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400926 if (qp->ibqp.qp_type == IB_QPT_UD ||
927 qp->ibqp.qp_type == IB_QPT_SMI ||
928 qp->ibqp.qp_type == IB_QPT_GSI)
Dennis Dalessandro15723f02016-01-19 14:42:17 -0800929 atomic_dec(&ibah_to_rvtah(wqe->ud_wr.ah)->refcount);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400930
Mike Marciniszyn43a474a2017-03-20 17:25:04 -0700931 rvt_qp_swqe_complete(qp,
932 wqe,
933 ib_hfi1_wc_opcode[wqe->wr.opcode],
934 status);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400935
Mike Marciniszyn77241052015-07-30 15:17:43 -0400936 if (qp->s_acked == old_last)
937 qp->s_acked = last;
938 if (qp->s_cur == old_last)
939 qp->s_cur = last;
940 if (qp->s_tail == old_last)
941 qp->s_tail = last;
942 if (qp->state == IB_QPS_SQD && last == qp->s_cur)
943 qp->s_draining = 0;
944}