blob: 425272210da05e4c0f7057cba2afd01f2c891929 [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 */
Mike Marciniszyn3ffea7d2017-07-29 08:43:43 -070077 ret = rvt_lkey_ok(rkt, pd, j ? &ss->sg_list[j - 1] : &ss->sge,
78 NULL, &wqe->sg_list[i],
79 IB_ACCESS_LOCAL_WRITE);
80 if (unlikely(ret <= 0))
Mike Marciniszyn77241052015-07-30 15:17:43 -040081 goto bad_lkey;
82 qp->r_len += wqe->sg_list[i].length;
83 j++;
84 }
85 ss->num_sge = j;
86 ss->total_len = qp->r_len;
87 ret = 1;
88 goto bail;
89
90bad_lkey:
91 while (j) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -080092 struct rvt_sge *sge = --j ? &ss->sg_list[j - 1] : &ss->sge;
Mike Marciniszyn77241052015-07-30 15:17:43 -040093
Dennis Dalessandro895420d2016-01-19 14:42:28 -080094 rvt_put_mr(sge->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -040095 }
96 ss->num_sge = 0;
97 memset(&wc, 0, sizeof(wc));
98 wc.wr_id = wqe->wr_id;
99 wc.status = IB_WC_LOC_PROT_ERR;
100 wc.opcode = IB_WC_RECV;
101 wc.qp = &qp->ibqp;
102 /* Signal solicited completion event. */
Dennis Dalessandroabd712d2016-01-19 14:43:22 -0800103 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400104 ret = 0;
105bail:
106 return ret;
107}
108
109/**
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800110 * hfi1_rvt_get_rwqe - copy the next RWQE into the QP's RWQE
Mike Marciniszyn77241052015-07-30 15:17:43 -0400111 * @qp: the QP
112 * @wr_id_only: update qp->r_wr_id only, not qp->r_sge
113 *
114 * Return -1 if there is a local error, 0 if no RWQE is available,
115 * otherwise return 1.
116 *
117 * Can be called from interrupt level.
118 */
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800119int hfi1_rvt_get_rwqe(struct rvt_qp *qp, int wr_id_only)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400120{
121 unsigned long flags;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800122 struct rvt_rq *rq;
123 struct rvt_rwq *wq;
Dennis Dalessandro39db3e62016-01-19 14:42:33 -0800124 struct rvt_srq *srq;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800125 struct rvt_rwqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400126 void (*handler)(struct ib_event *, void *);
127 u32 tail;
128 int ret;
129
130 if (qp->ibqp.srq) {
Dennis Dalessandro39db3e62016-01-19 14:42:33 -0800131 srq = ibsrq_to_rvtsrq(qp->ibqp.srq);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400132 handler = srq->ibsrq.event_handler;
133 rq = &srq->rq;
134 } else {
135 srq = NULL;
136 handler = NULL;
137 rq = &qp->r_rq;
138 }
139
140 spin_lock_irqsave(&rq->lock, flags);
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800141 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400142 ret = 0;
143 goto unlock;
144 }
145
146 wq = rq->wq;
147 tail = wq->tail;
148 /* Validate tail before using it since it is user writable. */
149 if (tail >= rq->size)
150 tail = 0;
151 if (unlikely(tail == wq->head)) {
152 ret = 0;
153 goto unlock;
154 }
155 /* Make sure entry is read after head index is read. */
156 smp_rmb();
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800157 wqe = rvt_get_rwqe_ptr(rq, tail);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400158 /*
159 * Even though we update the tail index in memory, the verbs
160 * consumer is not supposed to post more entries until a
161 * completion is generated.
162 */
163 if (++tail >= rq->size)
164 tail = 0;
165 wq->tail = tail;
166 if (!wr_id_only && !init_sge(qp, wqe)) {
167 ret = -1;
168 goto unlock;
169 }
170 qp->r_wr_id = wqe->wr_id;
171
172 ret = 1;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800173 set_bit(RVT_R_WRID_VALID, &qp->r_aflags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400174 if (handler) {
175 u32 n;
176
177 /*
178 * Validate head pointer value and compute
179 * the number of remaining WQEs.
180 */
181 n = wq->head;
182 if (n >= rq->size)
183 n = 0;
184 if (n < tail)
185 n += rq->size - tail;
186 else
187 n -= tail;
188 if (n < srq->limit) {
189 struct ib_event ev;
190
191 srq->limit = 0;
192 spin_unlock_irqrestore(&rq->lock, flags);
193 ev.device = qp->ibqp.device;
194 ev.element.srq = qp->ibqp.srq;
195 ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
196 handler(&ev, srq->ibsrq.srq_context);
197 goto bail;
198 }
199 }
200unlock:
201 spin_unlock_irqrestore(&rq->lock, flags);
202bail:
203 return ret;
204}
205
Mike Marciniszyn77241052015-07-30 15:17:43 -0400206static int gid_ok(union ib_gid *gid, __be64 gid_prefix, __be64 id)
207{
208 return (gid->global.interface_id == id &&
209 (gid->global.subnet_prefix == gid_prefix ||
210 gid->global.subnet_prefix == IB_DEFAULT_GID_PREFIX));
211}
212
213/*
214 *
215 * This should be called with the QP r_lock held.
216 *
217 * The s_lock will be acquired around the hfi1_migrate_qp() call.
218 */
Don Hiatt90397462017-05-12 09:20:20 -0700219int hfi1_ruc_check_hdr(struct hfi1_ibport *ibp, struct hfi1_packet *packet)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400220{
221 __be64 guid;
222 unsigned long flags;
Don Hiatt90397462017-05-12 09:20:20 -0700223 struct rvt_qp *qp = packet->qp;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400224 u8 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(&qp->remote_ah_attr)];
Don Hiatt90397462017-05-12 09:20:20 -0700225 u32 dlid = packet->dlid;
226 u32 slid = packet->slid;
227 u32 sl = packet->sl;
Sebastian Sanchez6d6b8842018-02-01 10:46:23 -0800228 bool migrated = packet->migrated;
229 u16 pkey = packet->pkey;
Don Hiatt90397462017-05-12 09:20:20 -0700230
231 if (qp->s_mig_state == IB_MIG_ARMED && migrated) {
232 if (!packet->grh) {
Don Hiatt5786adf32017-08-04 13:54:10 -0700233 if ((rdma_ah_get_ah_flags(&qp->alt_ah_attr) &
234 IB_AH_GRH) &&
235 (packet->etype != RHF_RCV_TYPE_BYPASS))
Don Hiatt90397462017-05-12 09:20:20 -0700236 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400237 } else {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400238 const struct ib_global_route *grh;
239
240 if (!(rdma_ah_get_ah_flags(&qp->alt_ah_attr) &
241 IB_AH_GRH))
Don Hiatt90397462017-05-12 09:20:20 -0700242 return 1;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400243 grh = rdma_ah_read_grh(&qp->alt_ah_attr);
244 guid = get_sguid(ibp, grh->sgid_index);
Don Hiatt90397462017-05-12 09:20:20 -0700245 if (!gid_ok(&packet->grh->dgid, ibp->rvp.gid_prefix,
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800246 guid))
Don Hiatt90397462017-05-12 09:20:20 -0700247 return 1;
Jubin John17fb4f22016-02-14 20:21:52 -0800248 if (!gid_ok(
Don Hiatt90397462017-05-12 09:20:20 -0700249 &packet->grh->sgid,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400250 grh->dgid.global.subnet_prefix,
251 grh->dgid.global.interface_id))
Don Hiatt90397462017-05-12 09:20:20 -0700252 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400253 }
Don Hiatt5786adf32017-08-04 13:54:10 -0700254 if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), pkey,
Don Hiatt90397462017-05-12 09:20:20 -0700255 sc5, slid))) {
Don Hiatt5786adf32017-08-04 13:54:10 -0700256 hfi1_bad_pkey(ibp, pkey, sl, 0, qp->ibqp.qp_num,
257 slid, dlid);
Don Hiatt90397462017-05-12 09:20:20 -0700258 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400259 }
260 /* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */
Don Hiatt90397462017-05-12 09:20:20 -0700261 if (slid != rdma_ah_get_dlid(&qp->alt_ah_attr) ||
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400262 ppd_from_ibp(ibp)->port !=
263 rdma_ah_get_port_num(&qp->alt_ah_attr))
Don Hiatt90397462017-05-12 09:20:20 -0700264 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400265 spin_lock_irqsave(&qp->s_lock, flags);
266 hfi1_migrate_qp(qp);
267 spin_unlock_irqrestore(&qp->s_lock, flags);
268 } else {
Don Hiatt90397462017-05-12 09:20:20 -0700269 if (!packet->grh) {
Don Hiatt5786adf32017-08-04 13:54:10 -0700270 if ((rdma_ah_get_ah_flags(&qp->remote_ah_attr) &
271 IB_AH_GRH) &&
272 (packet->etype != RHF_RCV_TYPE_BYPASS))
Don Hiatt90397462017-05-12 09:20:20 -0700273 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400274 } else {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400275 const struct ib_global_route *grh;
276
277 if (!(rdma_ah_get_ah_flags(&qp->remote_ah_attr) &
278 IB_AH_GRH))
Don Hiatt90397462017-05-12 09:20:20 -0700279 return 1;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400280 grh = rdma_ah_read_grh(&qp->remote_ah_attr);
281 guid = get_sguid(ibp, grh->sgid_index);
Don Hiatt90397462017-05-12 09:20:20 -0700282 if (!gid_ok(&packet->grh->dgid, ibp->rvp.gid_prefix,
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800283 guid))
Don Hiatt90397462017-05-12 09:20:20 -0700284 return 1;
Jubin John17fb4f22016-02-14 20:21:52 -0800285 if (!gid_ok(
Don Hiatt90397462017-05-12 09:20:20 -0700286 &packet->grh->sgid,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400287 grh->dgid.global.subnet_prefix,
288 grh->dgid.global.interface_id))
Don Hiatt90397462017-05-12 09:20:20 -0700289 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400290 }
Don Hiatt5786adf32017-08-04 13:54:10 -0700291 if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), pkey,
Don Hiatt90397462017-05-12 09:20:20 -0700292 sc5, slid))) {
Don Hiatt5786adf32017-08-04 13:54:10 -0700293 hfi1_bad_pkey(ibp, pkey, sl, 0, qp->ibqp.qp_num,
294 slid, dlid);
Don Hiatt90397462017-05-12 09:20:20 -0700295 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400296 }
297 /* Validate the SLID. See Ch. 9.6.1.5 */
Don Hiatt90397462017-05-12 09:20:20 -0700298 if ((slid != rdma_ah_get_dlid(&qp->remote_ah_attr)) ||
Mike Marciniszyn77241052015-07-30 15:17:43 -0400299 ppd_from_ibp(ibp)->port != qp->port_num)
Don Hiatt90397462017-05-12 09:20:20 -0700300 return 1;
301 if (qp->s_mig_state == IB_MIG_REARM && !migrated)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400302 qp->s_mig_state = IB_MIG_ARMED;
303 }
304
305 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400306}
307
308/**
309 * ruc_loopback - handle UC and RC loopback requests
310 * @sqp: the sending QP
311 *
312 * This is called from hfi1_do_send() to
313 * forward a WQE addressed to the same HFI.
Dennis Dalessandroca00c622016-09-25 07:42:08 -0700314 * Note that although we are single threaded due to the send engine, we still
Mike Marciniszyn77241052015-07-30 15:17:43 -0400315 * have to protect against post_send(). We don't have to worry about
316 * receive interrupts since this is a connected protocol and all packets
317 * will pass through here.
318 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800319static void ruc_loopback(struct rvt_qp *sqp)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400320{
321 struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800322 struct rvt_qp *qp;
323 struct rvt_swqe *wqe;
324 struct rvt_sge *sge;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400325 unsigned long flags;
326 struct ib_wc wc;
327 u64 sdata;
328 atomic64_t *maddr;
329 enum ib_wc_status send_status;
Brian Welty0128fce2017-02-08 05:27:31 -0800330 bool release;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400331 int ret;
Brian Welty0128fce2017-02-08 05:27:31 -0800332 bool copy_last = false;
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700333 int local_ops = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400334
335 rcu_read_lock();
336
337 /*
338 * Note that we check the responder QP state after
339 * checking the requester's state.
340 */
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800341 qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
342 sqp->remote_qpn);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400343
344 spin_lock_irqsave(&sqp->s_lock, flags);
345
346 /* Return if we are already busy processing a work request. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800347 if ((sqp->s_flags & (RVT_S_BUSY | RVT_S_ANY_WAIT)) ||
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800348 !(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_OR_FLUSH_SEND))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400349 goto unlock;
350
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800351 sqp->s_flags |= RVT_S_BUSY;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400352
353again:
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800354 smp_read_barrier_depends(); /* see post_one_send() */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700355 if (sqp->s_last == READ_ONCE(sqp->s_head))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400356 goto clr_busy;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800357 wqe = rvt_get_swqe_ptr(sqp, sqp->s_last);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400358
359 /* Return if it is not OK to start a new work request. */
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800360 if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
361 if (!(ib_rvt_state_ops[sqp->state] & RVT_FLUSH_SEND))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400362 goto clr_busy;
363 /* We are in the error state, flush the work request. */
364 send_status = IB_WC_WR_FLUSH_ERR;
365 goto flush_send;
366 }
367
368 /*
369 * We can rely on the entry not changing without the s_lock
370 * being held until we update s_last.
371 * We increment s_cur to indicate s_last is in progress.
372 */
373 if (sqp->s_last == sqp->s_cur) {
374 if (++sqp->s_cur >= sqp->s_size)
375 sqp->s_cur = 0;
376 }
377 spin_unlock_irqrestore(&sqp->s_lock, flags);
378
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800379 if (!qp || !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) ||
Mike Marciniszyn77241052015-07-30 15:17:43 -0400380 qp->ibqp.qp_type != sqp->ibqp.qp_type) {
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800381 ibp->rvp.n_pkt_drops++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400382 /*
383 * For RC, the requester would timeout and retry so
384 * shortcut the timeouts and just signal too many retries.
385 */
386 if (sqp->ibqp.qp_type == IB_QPT_RC)
387 send_status = IB_WC_RETRY_EXC_ERR;
388 else
389 send_status = IB_WC_SUCCESS;
390 goto serr;
391 }
392
393 memset(&wc, 0, sizeof(wc));
394 send_status = IB_WC_SUCCESS;
395
Brian Welty0128fce2017-02-08 05:27:31 -0800396 release = true;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400397 sqp->s_sge.sge = wqe->sg_list[0];
398 sqp->s_sge.sg_list = wqe->sg_list + 1;
399 sqp->s_sge.num_sge = wqe->wr.num_sge;
400 sqp->s_len = wqe->length;
401 switch (wqe->wr.opcode) {
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700402 case IB_WR_REG_MR:
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700403 goto send_comp;
404
405 case IB_WR_LOCAL_INV:
Jianxin Xiongd9b13c22016-07-25 13:39:45 -0700406 if (!(wqe->wr.send_flags & RVT_SEND_COMPLETION_ONLY)) {
407 if (rvt_invalidate_rkey(sqp,
408 wqe->wr.ex.invalidate_rkey))
409 send_status = IB_WC_LOC_PROT_ERR;
410 local_ops = 1;
411 }
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700412 goto send_comp;
413
414 case IB_WR_SEND_WITH_INV:
415 if (!rvt_invalidate_rkey(qp, wqe->wr.ex.invalidate_rkey)) {
416 wc.wc_flags = IB_WC_WITH_INVALIDATE;
417 wc.ex.invalidate_rkey = wqe->wr.ex.invalidate_rkey;
418 }
419 goto send;
420
Mike Marciniszyn77241052015-07-30 15:17:43 -0400421 case IB_WR_SEND_WITH_IMM:
422 wc.wc_flags = IB_WC_WITH_IMM;
423 wc.ex.imm_data = wqe->wr.ex.imm_data;
424 /* FALLTHROUGH */
425 case IB_WR_SEND:
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700426send:
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800427 ret = hfi1_rvt_get_rwqe(qp, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400428 if (ret < 0)
429 goto op_err;
430 if (!ret)
431 goto rnr_nak;
432 break;
433
434 case IB_WR_RDMA_WRITE_WITH_IMM:
435 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
436 goto inv_err;
437 wc.wc_flags = IB_WC_WITH_IMM;
438 wc.ex.imm_data = wqe->wr.ex.imm_data;
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800439 ret = hfi1_rvt_get_rwqe(qp, 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400440 if (ret < 0)
441 goto op_err;
442 if (!ret)
443 goto rnr_nak;
Dean Luick7b0b01a2016-02-03 14:35:49 -0800444 /* skip copy_last set and qp_access_flags recheck */
445 goto do_write;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400446 case IB_WR_RDMA_WRITE:
Brian Welty0128fce2017-02-08 05:27:31 -0800447 copy_last = rvt_is_user_qp(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400448 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
449 goto inv_err;
Dean Luick7b0b01a2016-02-03 14:35:49 -0800450do_write:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400451 if (wqe->length == 0)
Harish Chegondi42d6ec12016-03-05 08:49:24 -0800452 break;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800453 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
454 wqe->rdma_wr.remote_addr,
455 wqe->rdma_wr.rkey,
456 IB_ACCESS_REMOTE_WRITE)))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400457 goto acc_err;
458 qp->r_sge.sg_list = NULL;
459 qp->r_sge.num_sge = 1;
460 qp->r_sge.total_len = wqe->length;
461 break;
462
463 case IB_WR_RDMA_READ:
464 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
465 goto inv_err;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800466 if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
467 wqe->rdma_wr.remote_addr,
468 wqe->rdma_wr.rkey,
469 IB_ACCESS_REMOTE_READ)))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400470 goto acc_err;
Brian Welty0128fce2017-02-08 05:27:31 -0800471 release = false;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400472 sqp->s_sge.sg_list = NULL;
473 sqp->s_sge.num_sge = 1;
474 qp->r_sge.sge = wqe->sg_list[0];
475 qp->r_sge.sg_list = wqe->sg_list + 1;
476 qp->r_sge.num_sge = wqe->wr.num_sge;
477 qp->r_sge.total_len = wqe->length;
478 break;
479
480 case IB_WR_ATOMIC_CMP_AND_SWP:
481 case IB_WR_ATOMIC_FETCH_AND_ADD:
482 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
483 goto inv_err;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800484 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
485 wqe->atomic_wr.remote_addr,
486 wqe->atomic_wr.rkey,
487 IB_ACCESS_REMOTE_ATOMIC)))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400488 goto acc_err;
489 /* Perform atomic OP and save result. */
Jubin John50e5dcb2016-02-14 20:19:41 -0800490 maddr = (atomic64_t *)qp->r_sge.sge.vaddr;
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100491 sdata = wqe->atomic_wr.compare_add;
Jubin John50e5dcb2016-02-14 20:19:41 -0800492 *(u64 *)sqp->s_sge.sge.vaddr =
Mike Marciniszyn77241052015-07-30 15:17:43 -0400493 (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
Jubin John50e5dcb2016-02-14 20:19:41 -0800494 (u64)atomic64_add_return(sdata, maddr) - sdata :
495 (u64)cmpxchg((u64 *)qp->r_sge.sge.vaddr,
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100496 sdata, wqe->atomic_wr.swap);
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800497 rvt_put_mr(qp->r_sge.sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400498 qp->r_sge.num_sge = 0;
499 goto send_comp;
500
501 default:
502 send_status = IB_WC_LOC_QP_OP_ERR;
503 goto serr;
504 }
505
506 sge = &sqp->s_sge.sge;
507 while (sqp->s_len) {
508 u32 len = sqp->s_len;
509
510 if (len > sge->length)
511 len = sge->length;
512 if (len > sge->sge_length)
513 len = sge->sge_length;
514 WARN_ON_ONCE(len == 0);
Dean Luick7b0b01a2016-02-03 14:35:49 -0800515 hfi1_copy_sge(&qp->r_sge, sge->vaddr, len, release, copy_last);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400516 sge->vaddr += len;
517 sge->length -= len;
518 sge->sge_length -= len;
519 if (sge->sge_length == 0) {
520 if (!release)
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800521 rvt_put_mr(sge->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400522 if (--sqp->s_sge.num_sge)
523 *sge = *sqp->s_sge.sg_list++;
524 } else if (sge->length == 0 && sge->mr->lkey) {
Dennis Dalessandrocd4ceee2016-01-19 14:41:55 -0800525 if (++sge->n >= RVT_SEGSZ) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400526 if (++sge->m >= sge->mr->mapsz)
527 break;
528 sge->n = 0;
529 }
530 sge->vaddr =
531 sge->mr->map[sge->m]->segs[sge->n].vaddr;
532 sge->length =
533 sge->mr->map[sge->m]->segs[sge->n].length;
534 }
535 sqp->s_len -= len;
536 }
537 if (release)
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800538 rvt_put_ss(&qp->r_sge);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400539
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800540 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400541 goto send_comp;
542
543 if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
544 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
545 else
546 wc.opcode = IB_WC_RECV;
547 wc.wr_id = qp->r_wr_id;
548 wc.status = IB_WC_SUCCESS;
549 wc.byte_len = wqe->length;
550 wc.qp = &qp->ibqp;
551 wc.src_qp = qp->remote_qpn;
Don Hiattb64581a2017-11-06 06:39:22 -0800552 wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr) & U16_MAX;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400553 wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400554 wc.port_num = 1;
555 /* Signal completion event if the solicited bit is set. */
Dennis Dalessandroabd712d2016-01-19 14:43:22 -0800556 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
557 wqe->wr.send_flags & IB_SEND_SOLICITED);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400558
559send_comp:
560 spin_lock_irqsave(&sqp->s_lock, flags);
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800561 ibp->rvp.n_loop_pkts++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400562flush_send:
563 sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
564 hfi1_send_complete(sqp, wqe, send_status);
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700565 if (local_ops) {
566 atomic_dec(&sqp->local_ops_pending);
567 local_ops = 0;
568 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400569 goto again;
570
571rnr_nak:
572 /* Handle RNR NAK */
573 if (qp->ibqp.qp_type == IB_QPT_UC)
574 goto send_comp;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800575 ibp->rvp.n_rnr_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400576 /*
577 * Note: we don't need the s_lock held since the BUSY flag
578 * makes this single threaded.
579 */
580 if (sqp->s_rnr_retry == 0) {
581 send_status = IB_WC_RNR_RETRY_EXC_ERR;
582 goto serr;
583 }
584 if (sqp->s_rnr_retry_cnt < 7)
585 sqp->s_rnr_retry--;
586 spin_lock_irqsave(&sqp->s_lock, flags);
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800587 if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400588 goto clr_busy;
Venkata Sandeep Dhanalakota56acbbf2017-02-08 05:27:19 -0800589 rvt_add_rnr_timer(sqp, qp->r_min_rnr_timer <<
Don Hiatt832666c2017-02-08 05:28:25 -0800590 IB_AETH_CREDIT_SHIFT);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400591 goto clr_busy;
592
593op_err:
594 send_status = IB_WC_REM_OP_ERR;
595 wc.status = IB_WC_LOC_QP_OP_ERR;
596 goto err;
597
598inv_err:
599 send_status = IB_WC_REM_INV_REQ_ERR;
600 wc.status = IB_WC_LOC_QP_OP_ERR;
601 goto err;
602
603acc_err:
604 send_status = IB_WC_REM_ACCESS_ERR;
605 wc.status = IB_WC_LOC_PROT_ERR;
606err:
607 /* responder goes to error state */
Brian Weltybeb5a042017-02-08 05:27:01 -0800608 rvt_rc_error(qp, wc.status);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400609
610serr:
611 spin_lock_irqsave(&sqp->s_lock, flags);
612 hfi1_send_complete(sqp, wqe, send_status);
613 if (sqp->ibqp.qp_type == IB_QPT_RC) {
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800614 int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400615
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800616 sqp->s_flags &= ~RVT_S_BUSY;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400617 spin_unlock_irqrestore(&sqp->s_lock, flags);
618 if (lastwqe) {
619 struct ib_event ev;
620
621 ev.device = sqp->ibqp.device;
622 ev.element.qp = &sqp->ibqp;
623 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
624 sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
625 }
626 goto done;
627 }
628clr_busy:
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800629 sqp->s_flags &= ~RVT_S_BUSY;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400630unlock:
631 spin_unlock_irqrestore(&sqp->s_lock, flags);
632done:
633 rcu_read_unlock();
634}
635
636/**
637 * hfi1_make_grh - construct a GRH header
638 * @ibp: a pointer to the IB port
639 * @hdr: a pointer to the GRH header being constructed
640 * @grh: the global route address to send to
Don Hiatt88733e32017-08-04 13:54:23 -0700641 * @hwords: size of header after grh being sent in dwords
Mike Marciniszyn77241052015-07-30 15:17:43 -0400642 * @nwords: the number of 32 bit words of data being sent
643 *
644 * Return the size of the header in 32 bit words.
645 */
646u32 hfi1_make_grh(struct hfi1_ibport *ibp, struct ib_grh *hdr,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400647 const struct ib_global_route *grh, u32 hwords, u32 nwords)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400648{
649 hdr->version_tclass_flow =
650 cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
651 (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
652 (grh->flow_label << IB_GRH_FLOW_SHIFT));
Don Hiatt88733e32017-08-04 13:54:23 -0700653 hdr->paylen = cpu_to_be16((hwords + nwords) << 2);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400654 /* next_hdr is defined by C8-7 in ch. 8.4.1 */
655 hdr->next_hdr = IB_GRH_NEXT_HDR;
656 hdr->hop_limit = grh->hop_limit;
657 /* The SGID is 32-bit aligned. */
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800658 hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400659 hdr->sgid.global.interface_id =
Jakub Pawlaka6cd5f02016-10-17 04:19:30 -0700660 grh->sgid_index < HFI1_GUIDS_PER_PORT ?
661 get_sguid(ibp, grh->sgid_index) :
662 get_sguid(ibp, HFI1_PORT_GUID_INDEX);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400663 hdr->dgid = grh->dgid;
664
665 /* GRH header size in 32-bit words. */
666 return sizeof(struct ib_grh) / sizeof(u32);
667}
668
Don Hiatt30e07412017-08-04 13:54:04 -0700669#define BTH2_OFFSET (offsetof(struct hfi1_sdma_header, \
670 hdr.ibh.u.oth.bth[2]) / 4)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400671
672/**
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700673 * build_ahg - create ahg in s_ahg
Mike Marciniszyn77241052015-07-30 15:17:43 -0400674 * @qp: a pointer to QP
675 * @npsn: the next PSN for the request/response
676 *
677 * This routine handles the AHG by allocating an ahg entry and causing the
678 * copy of the first middle.
679 *
680 * Subsequent middles use the copied entry, editing the
681 * PSN with 1 or 2 edits.
682 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800683static inline void build_ahg(struct rvt_qp *qp, u32 npsn)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400684{
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800685 struct hfi1_qp_priv *priv = qp->priv;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800686
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800687 if (unlikely(qp->s_flags & RVT_S_AHG_CLEAR))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400688 clear_ahg(qp);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800689 if (!(qp->s_flags & RVT_S_AHG_VALID)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400690 /* first middle that needs copy */
Mike Marciniszynd7b8ba52015-11-09 19:13:59 -0500691 if (qp->s_ahgidx < 0)
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800692 qp->s_ahgidx = sdma_ahg_alloc(priv->s_sde);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400693 if (qp->s_ahgidx >= 0) {
694 qp->s_ahgpsn = npsn;
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700695 priv->s_ahg->tx_flags |= SDMA_TXREQ_F_AHG_COPY;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400696 /* save to protect a change in another thread */
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700697 priv->s_ahg->ahgidx = qp->s_ahgidx;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800698 qp->s_flags |= RVT_S_AHG_VALID;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400699 }
700 } else {
701 /* subsequent middle after valid */
702 if (qp->s_ahgidx >= 0) {
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700703 priv->s_ahg->tx_flags |= SDMA_TXREQ_F_USE_AHG;
704 priv->s_ahg->ahgidx = qp->s_ahgidx;
705 priv->s_ahg->ahgcount++;
706 priv->s_ahg->ahgdesc[0] =
Mike Marciniszyn77241052015-07-30 15:17:43 -0400707 sdma_build_ahg_descriptor(
708 (__force u16)cpu_to_be16((u16)npsn),
709 BTH2_OFFSET,
710 16,
711 16);
712 if ((npsn & 0xffff0000) !=
713 (qp->s_ahgpsn & 0xffff0000)) {
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700714 priv->s_ahg->ahgcount++;
715 priv->s_ahg->ahgdesc[1] =
Mike Marciniszyn77241052015-07-30 15:17:43 -0400716 sdma_build_ahg_descriptor(
717 (__force u16)cpu_to_be16(
718 (u16)(npsn >> 16)),
719 BTH2_OFFSET,
720 0,
721 16);
722 }
723 }
724 }
725}
726
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700727static inline void hfi1_make_ruc_bth(struct rvt_qp *qp,
728 struct ib_other_headers *ohdr,
729 u32 bth0, u32 bth1, u32 bth2)
730{
731 bth1 |= qp->remote_qpn;
732 ohdr->bth[0] = cpu_to_be32(bth0);
733 ohdr->bth[1] = cpu_to_be32(bth1);
734 ohdr->bth[2] = cpu_to_be32(bth2);
735}
736
737static inline void hfi1_make_ruc_header_16B(struct rvt_qp *qp,
738 struct ib_other_headers *ohdr,
739 u32 bth0, u32 bth2, int middle,
740 struct hfi1_pkt_state *ps)
741{
742 struct hfi1_qp_priv *priv = qp->priv;
743 struct hfi1_ibport *ibp = ps->ibp;
744 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
745 u32 bth1 = 0;
746 u32 slid;
747 u16 pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
748 u8 l4 = OPA_16B_L4_IB_LOCAL;
Mitko Haralanov96362582018-02-01 10:46:07 -0800749 u8 extra_bytes = hfi1_get_16b_padding(
750 (ps->s_txreq->hdr_dwords << 2),
751 ps->s_txreq->s_cur_size);
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700752 u32 nwords = SIZE_OF_CRC + ((ps->s_txreq->s_cur_size +
753 extra_bytes + SIZE_OF_LT) >> 2);
Sebastian Sanchezca85bb12018-02-01 10:46:38 -0800754 bool becn = false;
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700755
756 if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH) &&
757 hfi1_check_mcast(rdma_ah_get_dlid(&qp->remote_ah_attr))) {
758 struct ib_grh *grh;
759 struct ib_global_route *grd =
760 rdma_ah_retrieve_grh(&qp->remote_ah_attr);
761 int hdrwords;
762
763 /*
764 * Ensure OPA GIDs are transformed to IB gids
765 * before creating the GRH.
766 */
767 if (grd->sgid_index == OPA_GID_INDEX)
768 grd->sgid_index = 0;
769 grh = &ps->s_txreq->phdr.hdr.opah.u.l.grh;
770 l4 = OPA_16B_L4_IB_GLOBAL;
Mitko Haralanov96362582018-02-01 10:46:07 -0800771 hdrwords = ps->s_txreq->hdr_dwords - 4;
772 ps->s_txreq->hdr_dwords += hfi1_make_grh(ibp, grh, grd,
773 hdrwords, nwords);
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700774 middle = 0;
775 }
776
777 if (qp->s_mig_state == IB_MIG_MIGRATED)
778 bth1 |= OPA_BTH_MIG_REQ;
779 else
780 middle = 0;
781
782 if (middle)
783 build_ahg(qp, bth2);
784 else
785 qp->s_flags &= ~RVT_S_AHG_VALID;
786
787 bth0 |= pkey;
788 bth0 |= extra_bytes << 20;
789 if (qp->s_flags & RVT_S_ECN) {
790 qp->s_flags &= ~RVT_S_ECN;
791 /* we recently received a FECN, so return a BECN */
Sebastian Sanchezca85bb12018-02-01 10:46:38 -0800792 becn = true;
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700793 }
794 hfi1_make_ruc_bth(qp, ohdr, bth0, bth1, bth2);
795
796 if (!ppd->lid)
797 slid = be32_to_cpu(OPA_LID_PERMISSIVE);
798 else
799 slid = ppd->lid |
800 (rdma_ah_get_path_bits(&qp->remote_ah_attr) &
801 ((1 << ppd->lmc) - 1));
802
803 hfi1_make_16b_hdr(&ps->s_txreq->phdr.hdr.opah,
804 slid,
805 opa_get_lid(rdma_ah_get_dlid(&qp->remote_ah_attr),
806 16B),
Mitko Haralanov96362582018-02-01 10:46:07 -0800807 (ps->s_txreq->hdr_dwords + nwords) >> 1,
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700808 pkey, becn, 0, l4, priv->s_sc);
809}
810
811static inline void hfi1_make_ruc_header_9B(struct rvt_qp *qp,
812 struct ib_other_headers *ohdr,
813 u32 bth0, u32 bth2, int middle,
814 struct hfi1_pkt_state *ps)
815{
816 struct hfi1_qp_priv *priv = qp->priv;
817 struct hfi1_ibport *ibp = ps->ibp;
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700818 u32 bth1 = 0;
819 u16 pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
820 u16 lrh0 = HFI1_LRH_BTH;
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700821 u8 extra_bytes = -ps->s_txreq->s_cur_size & 3;
822 u32 nwords = SIZE_OF_CRC + ((ps->s_txreq->s_cur_size +
823 extra_bytes) >> 2);
824
825 if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)) {
826 struct ib_grh *grh = &ps->s_txreq->phdr.hdr.ibh.u.l.grh;
Mitko Haralanov96362582018-02-01 10:46:07 -0800827 int hdrwords = ps->s_txreq->hdr_dwords - 2;
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700828
829 lrh0 = HFI1_LRH_GRH;
Mitko Haralanov96362582018-02-01 10:46:07 -0800830 ps->s_txreq->hdr_dwords +=
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700831 hfi1_make_grh(ibp, grh,
832 rdma_ah_read_grh(&qp->remote_ah_attr),
833 hdrwords, nwords);
834 middle = 0;
835 }
836 lrh0 |= (priv->s_sc & 0xf) << 12 |
837 (rdma_ah_get_sl(&qp->remote_ah_attr) & 0xf) << 4;
838
839 if (qp->s_mig_state == IB_MIG_MIGRATED)
840 bth0 |= IB_BTH_MIG_REQ;
841 else
842 middle = 0;
843
844 if (middle)
845 build_ahg(qp, bth2);
846 else
847 qp->s_flags &= ~RVT_S_AHG_VALID;
848
849 bth0 |= pkey;
850 bth0 |= extra_bytes << 20;
851 if (qp->s_flags & RVT_S_ECN) {
852 qp->s_flags &= ~RVT_S_ECN;
853 /* we recently received a FECN, so return a BECN */
854 bth1 |= (IB_BECN_MASK << IB_BECN_SHIFT);
855 }
856 hfi1_make_ruc_bth(qp, ohdr, bth0, bth1, bth2);
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700857 hfi1_make_ib_hdr(&ps->s_txreq->phdr.hdr.ibh,
858 lrh0,
Mitko Haralanov96362582018-02-01 10:46:07 -0800859 ps->s_txreq->hdr_dwords + nwords,
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700860 opa_get_lid(rdma_ah_get_dlid(&qp->remote_ah_attr), 9B),
861 ppd_from_ibp(ibp)->lid |
862 rdma_ah_get_path_bits(&qp->remote_ah_attr));
863}
864
865typedef void (*hfi1_make_ruc_hdr)(struct rvt_qp *qp,
866 struct ib_other_headers *ohdr,
867 u32 bth0, u32 bth2, int middle,
868 struct hfi1_pkt_state *ps);
869
870/* We support only two types - 9B and 16B for now */
871static const hfi1_make_ruc_hdr hfi1_ruc_header_tbl[2] = {
872 [HFI1_PKT_TYPE_9B] = &hfi1_make_ruc_header_9B,
873 [HFI1_PKT_TYPE_16B] = &hfi1_make_ruc_header_16B
874};
875
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700876void hfi1_make_ruc_header(struct rvt_qp *qp, struct ib_other_headers *ohdr,
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800877 u32 bth0, u32 bth2, int middle,
878 struct hfi1_pkt_state *ps)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400879{
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800880 struct hfi1_qp_priv *priv = qp->priv;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400881
Mike Marciniszyn77241052015-07-30 15:17:43 -0400882 /*
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700883 * reset s_ahg/AHG fields
Mike Marciniszyn77241052015-07-30 15:17:43 -0400884 *
885 * This insures that the ahgentry/ahgcount
886 * are at a non-AHG default to protect
887 * build_verbs_tx_desc() from using
888 * an include ahgidx.
889 *
890 * build_ahg() will modify as appropriate
891 * to use the AHG feature.
892 */
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700893 priv->s_ahg->tx_flags = 0;
894 priv->s_ahg->ahgcount = 0;
895 priv->s_ahg->ahgidx = 0;
Don Hiatt5b6cabb2017-08-04 13:54:41 -0700896
897 /* Make the appropriate header */
898 hfi1_ruc_header_tbl[priv->hdr_type](qp, ohdr, bth0, bth2, middle, ps);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400899}
900
Dean Luickb4219222015-10-26 10:28:35 -0400901/* when sending, force a reschedule every one of these periods */
902#define SEND_RESCHED_TIMEOUT (5 * HZ) /* 5s in jiffies */
903
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700904/**
905 * schedule_send_yield - test for a yield required for QP send engine
906 * @timeout: Final time for timeout slice for jiffies
907 * @qp: a pointer to QP
908 * @ps: a pointer to a structure with commonly lookup values for
909 * the the send engine progress
910 *
911 * This routine checks if the time slice for the QP has expired
912 * for RC QPs, if so an additional work entry is queued. At this
913 * point, other QPs have an opportunity to be scheduled. It
914 * returns true if a yield is required, otherwise, false
915 * is returned.
916 */
917static bool schedule_send_yield(struct rvt_qp *qp,
918 struct hfi1_pkt_state *ps)
919{
Kaike Wanbcad2912017-07-24 07:45:37 -0700920 ps->pkts_sent = true;
921
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700922 if (unlikely(time_after(jiffies, ps->timeout))) {
923 if (!ps->in_thread ||
924 workqueue_congested(ps->cpu, ps->ppd->hfi1_wq)) {
925 spin_lock_irqsave(&qp->s_lock, ps->flags);
926 qp->s_flags &= ~RVT_S_BUSY;
927 hfi1_schedule_send(qp);
928 spin_unlock_irqrestore(&qp->s_lock, ps->flags);
929 this_cpu_inc(*ps->ppd->dd->send_schedule);
930 trace_hfi1_rc_expired_time_slice(qp, true);
931 return true;
932 }
933
934 cond_resched();
935 this_cpu_inc(*ps->ppd->dd->send_schedule);
936 ps->timeout = jiffies + ps->timeout_int;
937 }
938
939 trace_hfi1_rc_expired_time_slice(qp, false);
940 return false;
941}
942
Mike Marciniszynb6eac932017-04-09 10:16:35 -0700943void hfi1_do_send_from_rvt(struct rvt_qp *qp)
944{
945 hfi1_do_send(qp, false);
946}
947
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800948void _hfi1_do_send(struct work_struct *work)
949{
950 struct iowait *wait = container_of(work, struct iowait, iowork);
951 struct rvt_qp *qp = iowait_to_qp(wait);
952
Mike Marciniszynb6eac932017-04-09 10:16:35 -0700953 hfi1_do_send(qp, true);
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800954}
955
Mike Marciniszyn77241052015-07-30 15:17:43 -0400956/**
957 * hfi1_do_send - perform a send on a QP
958 * @work: contains a pointer to the QP
Mike Marciniszynb6eac932017-04-09 10:16:35 -0700959 * @in_thread: true if in a workqueue thread
Mike Marciniszyn77241052015-07-30 15:17:43 -0400960 *
961 * Process entries in the send work queue until credit or queue is
Dennis Dalessandroca00c622016-09-25 07:42:08 -0700962 * exhausted. Only allow one CPU to send a packet per QP.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400963 * Otherwise, two threads could send packets out of order.
964 */
Mike Marciniszynb6eac932017-04-09 10:16:35 -0700965void hfi1_do_send(struct rvt_qp *qp, bool in_thread)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400966{
Dennis Dalessandrod46e5142015-11-11 00:34:37 -0500967 struct hfi1_pkt_state ps;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800968 struct hfi1_qp_priv *priv = qp->priv;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800969 int (*make_req)(struct rvt_qp *qp, struct hfi1_pkt_state *ps);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400970
Dennis Dalessandrod46e5142015-11-11 00:34:37 -0500971 ps.dev = to_idev(qp->ibqp.device);
972 ps.ibp = to_iport(qp->ibqp.device, qp->port_num);
973 ps.ppd = ppd_from_ibp(ps.ibp);
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700974 ps.in_thread = in_thread;
975
976 trace_hfi1_rc_do_send(qp, in_thread);
Dennis Dalessandrod46e5142015-11-11 00:34:37 -0500977
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800978 switch (qp->ibqp.qp_type) {
979 case IB_QPT_RC:
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400980 if (!loopback && ((rdma_ah_get_dlid(&qp->remote_ah_attr) &
981 ~((1 << ps.ppd->lmc) - 1)) ==
982 ps.ppd->lid)) {
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800983 ruc_loopback(qp);
984 return;
985 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400986 make_req = hfi1_make_rc_req;
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700987 ps.timeout_int = qp->timeout_jiffies;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800988 break;
989 case IB_QPT_UC:
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400990 if (!loopback && ((rdma_ah_get_dlid(&qp->remote_ah_attr) &
991 ~((1 << ps.ppd->lmc) - 1)) ==
992 ps.ppd->lid)) {
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800993 ruc_loopback(qp);
994 return;
995 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400996 make_req = hfi1_make_uc_req;
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700997 ps.timeout_int = SEND_RESCHED_TIMEOUT;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800998 break;
999 default:
Mike Marciniszyn77241052015-07-30 15:17:43 -04001000 make_req = hfi1_make_ud_req;
Mike Marciniszyndd1ed102017-05-04 05:14:10 -07001001 ps.timeout_int = SEND_RESCHED_TIMEOUT;
Vennila Megavannan23cd4712016-02-03 14:34:23 -08001002 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001003
Mike Marciniszyn747f4d72016-04-12 10:46:10 -07001004 spin_lock_irqsave(&qp->s_lock, ps.flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001005
1006 /* Return if we are already busy processing a work request. */
1007 if (!hfi1_send_ok(qp)) {
Mike Marciniszyn747f4d72016-04-12 10:46:10 -07001008 spin_unlock_irqrestore(&qp->s_lock, ps.flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001009 return;
1010 }
1011
Dennis Dalessandro54d10c12016-01-19 14:43:01 -08001012 qp->s_flags |= RVT_S_BUSY;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001013
Mike Marciniszyndd1ed102017-05-04 05:14:10 -07001014 ps.timeout_int = ps.timeout_int / 8;
1015 ps.timeout = jiffies + ps.timeout_int;
1016 ps.cpu = priv->s_sde ? priv->s_sde->cpu :
Vennila Megavannan23cd4712016-02-03 14:34:23 -08001017 cpumask_first(cpumask_of_node(ps.ppd->dd->node));
Kaike Wanbcad2912017-07-24 07:45:37 -07001018 ps.pkts_sent = false;
Mike Marciniszyndd1ed102017-05-04 05:14:10 -07001019
Mike Marciniszyn711e1042016-02-14 12:45:18 -08001020 /* insure a pre-built packet is handled */
1021 ps.s_txreq = get_waiting_verbs_txreq(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001022 do {
1023 /* Check for a constructed packet to be sent. */
Mitko Haralanov96362582018-02-01 10:46:07 -08001024 if (ps.s_txreq) {
Mike Marciniszyn747f4d72016-04-12 10:46:10 -07001025 spin_unlock_irqrestore(&qp->s_lock, ps.flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001026 /*
1027 * If the packet cannot be sent now, return and
Dennis Dalessandroca00c622016-09-25 07:42:08 -07001028 * the send engine will be woken up later.
Mike Marciniszyn77241052015-07-30 15:17:43 -04001029 */
Dennis Dalessandrod46e5142015-11-11 00:34:37 -05001030 if (hfi1_verbs_send(qp, &ps))
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001031 return;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -08001032 /* allow other tasks to run */
Mike Marciniszyndd1ed102017-05-04 05:14:10 -07001033 if (schedule_send_yield(qp, &ps))
1034 return;
1035
Mike Marciniszyn747f4d72016-04-12 10:46:10 -07001036 spin_lock_irqsave(&qp->s_lock, ps.flags);
Dean Luickb4219222015-10-26 10:28:35 -04001037 }
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -08001038 } while (make_req(qp, &ps));
Kaike Wanbcad2912017-07-24 07:45:37 -07001039 iowait_starve_clear(ps.pkts_sent, &priv->s_iowait);
Mike Marciniszyn747f4d72016-04-12 10:46:10 -07001040 spin_unlock_irqrestore(&qp->s_lock, ps.flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001041}
1042
1043/*
1044 * This should be called with s_lock held.
1045 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -08001046void hfi1_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001047 enum ib_wc_status status)
1048{
1049 u32 old_last, last;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001050
Dennis Dalessandro83693bd2016-01-19 14:43:33 -08001051 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001052 return;
1053
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -08001054 last = qp->s_last;
1055 old_last = last;
Mike Marciniszyn9260b352017-03-20 17:25:23 -07001056 trace_hfi1_qp_send_completion(qp, wqe, last);
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -08001057 if (++last >= qp->s_size)
1058 last = 0;
Mike Marciniszyn9260b352017-03-20 17:25:23 -07001059 trace_hfi1_qp_send_completion(qp, wqe, last);
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -08001060 qp->s_last = last;
1061 /* See post_send() */
1062 barrier();
Mike Marciniszync64607a2016-12-07 19:34:31 -08001063 rvt_put_swqe(wqe);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001064 if (qp->ibqp.qp_type == IB_QPT_UD ||
1065 qp->ibqp.qp_type == IB_QPT_SMI ||
1066 qp->ibqp.qp_type == IB_QPT_GSI)
Dennis Dalessandro15723f02016-01-19 14:42:17 -08001067 atomic_dec(&ibah_to_rvtah(wqe->ud_wr.ah)->refcount);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001068
Mike Marciniszyn43a474a2017-03-20 17:25:04 -07001069 rvt_qp_swqe_complete(qp,
1070 wqe,
1071 ib_hfi1_wc_opcode[wqe->wr.opcode],
1072 status);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001073
Mike Marciniszyn77241052015-07-30 15:17:43 -04001074 if (qp->s_acked == old_last)
1075 qp->s_acked = last;
1076 if (qp->s_cur == old_last)
1077 qp->s_cur = last;
1078 if (qp->s_tail == old_last)
1079 qp->s_tail = last;
1080 if (qp->state == IB_QPS_SQD && last == qp->s_cur)
1081 qp->s_draining = 0;
1082}