blob: e30c64fcce67c5304f496f8587ecc2f67ba14af9 [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;
228 int migrated;
229 u32 bth0, bth1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400230
Don Hiatt90397462017-05-12 09:20:20 -0700231 bth0 = be32_to_cpu(packet->ohdr->bth[0]);
232 bth1 = be32_to_cpu(packet->ohdr->bth[1]);
233 migrated = bth0 & IB_BTH_MIG_REQ;
234
235 if (qp->s_mig_state == IB_MIG_ARMED && migrated) {
236 if (!packet->grh) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400237 if (rdma_ah_get_ah_flags(&qp->alt_ah_attr) &
238 IB_AH_GRH)
Don Hiatt90397462017-05-12 09:20:20 -0700239 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400240 } else {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400241 const struct ib_global_route *grh;
242
243 if (!(rdma_ah_get_ah_flags(&qp->alt_ah_attr) &
244 IB_AH_GRH))
Don Hiatt90397462017-05-12 09:20:20 -0700245 return 1;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400246 grh = rdma_ah_read_grh(&qp->alt_ah_attr);
247 guid = get_sguid(ibp, grh->sgid_index);
Don Hiatt90397462017-05-12 09:20:20 -0700248 if (!gid_ok(&packet->grh->dgid, ibp->rvp.gid_prefix,
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800249 guid))
Don Hiatt90397462017-05-12 09:20:20 -0700250 return 1;
Jubin John17fb4f22016-02-14 20:21:52 -0800251 if (!gid_ok(
Don Hiatt90397462017-05-12 09:20:20 -0700252 &packet->grh->sgid,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400253 grh->dgid.global.subnet_prefix,
254 grh->dgid.global.interface_id))
Don Hiatt90397462017-05-12 09:20:20 -0700255 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400256 }
Don Hiatt90397462017-05-12 09:20:20 -0700257 if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), (u16)bth0,
258 sc5, slid))) {
Dennis Dalessandro13d84912017-05-29 17:22:01 -0700259 hfi1_bad_pkey(ibp, (u16)bth0, sl,
260 0, qp->ibqp.qp_num, slid, dlid);
Don Hiatt90397462017-05-12 09:20:20 -0700261 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400262 }
263 /* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */
Don Hiatt90397462017-05-12 09:20:20 -0700264 if (slid != rdma_ah_get_dlid(&qp->alt_ah_attr) ||
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400265 ppd_from_ibp(ibp)->port !=
266 rdma_ah_get_port_num(&qp->alt_ah_attr))
Don Hiatt90397462017-05-12 09:20:20 -0700267 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400268 spin_lock_irqsave(&qp->s_lock, flags);
269 hfi1_migrate_qp(qp);
270 spin_unlock_irqrestore(&qp->s_lock, flags);
271 } else {
Don Hiatt90397462017-05-12 09:20:20 -0700272 if (!packet->grh) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400273 if (rdma_ah_get_ah_flags(&qp->remote_ah_attr) &
274 IB_AH_GRH)
Don Hiatt90397462017-05-12 09:20:20 -0700275 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400276 } else {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400277 const struct ib_global_route *grh;
278
279 if (!(rdma_ah_get_ah_flags(&qp->remote_ah_attr) &
280 IB_AH_GRH))
Don Hiatt90397462017-05-12 09:20:20 -0700281 return 1;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400282 grh = rdma_ah_read_grh(&qp->remote_ah_attr);
283 guid = get_sguid(ibp, grh->sgid_index);
Don Hiatt90397462017-05-12 09:20:20 -0700284 if (!gid_ok(&packet->grh->dgid, ibp->rvp.gid_prefix,
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800285 guid))
Don Hiatt90397462017-05-12 09:20:20 -0700286 return 1;
Jubin John17fb4f22016-02-14 20:21:52 -0800287 if (!gid_ok(
Don Hiatt90397462017-05-12 09:20:20 -0700288 &packet->grh->sgid,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400289 grh->dgid.global.subnet_prefix,
290 grh->dgid.global.interface_id))
Don Hiatt90397462017-05-12 09:20:20 -0700291 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400292 }
Don Hiatt90397462017-05-12 09:20:20 -0700293 if (unlikely(rcv_pkey_check(ppd_from_ibp(ibp), (u16)bth0,
294 sc5, slid))) {
Dennis Dalessandro13d84912017-05-29 17:22:01 -0700295 hfi1_bad_pkey(ibp, (u16)bth0, sl,
296 0, qp->ibqp.qp_num, slid, dlid);
Don Hiatt90397462017-05-12 09:20:20 -0700297 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400298 }
299 /* Validate the SLID. See Ch. 9.6.1.5 */
Don Hiatt90397462017-05-12 09:20:20 -0700300 if ((slid != rdma_ah_get_dlid(&qp->remote_ah_attr)) ||
Mike Marciniszyn77241052015-07-30 15:17:43 -0400301 ppd_from_ibp(ibp)->port != qp->port_num)
Don Hiatt90397462017-05-12 09:20:20 -0700302 return 1;
303 if (qp->s_mig_state == IB_MIG_REARM && !migrated)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400304 qp->s_mig_state = IB_MIG_ARMED;
305 }
306
307 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400308}
309
310/**
311 * ruc_loopback - handle UC and RC loopback requests
312 * @sqp: the sending QP
313 *
314 * This is called from hfi1_do_send() to
315 * forward a WQE addressed to the same HFI.
Dennis Dalessandroca00c622016-09-25 07:42:08 -0700316 * Note that although we are single threaded due to the send engine, we still
Mike Marciniszyn77241052015-07-30 15:17:43 -0400317 * have to protect against post_send(). We don't have to worry about
318 * receive interrupts since this is a connected protocol and all packets
319 * will pass through here.
320 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800321static void ruc_loopback(struct rvt_qp *sqp)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400322{
323 struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800324 struct rvt_qp *qp;
325 struct rvt_swqe *wqe;
326 struct rvt_sge *sge;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400327 unsigned long flags;
328 struct ib_wc wc;
329 u64 sdata;
330 atomic64_t *maddr;
331 enum ib_wc_status send_status;
Brian Welty0128fce2017-02-08 05:27:31 -0800332 bool release;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400333 int ret;
Brian Welty0128fce2017-02-08 05:27:31 -0800334 bool copy_last = false;
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700335 int local_ops = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400336
337 rcu_read_lock();
338
339 /*
340 * Note that we check the responder QP state after
341 * checking the requester's state.
342 */
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800343 qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
344 sqp->remote_qpn);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400345
346 spin_lock_irqsave(&sqp->s_lock, flags);
347
348 /* Return if we are already busy processing a work request. */
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800349 if ((sqp->s_flags & (RVT_S_BUSY | RVT_S_ANY_WAIT)) ||
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800350 !(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_OR_FLUSH_SEND))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400351 goto unlock;
352
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800353 sqp->s_flags |= RVT_S_BUSY;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400354
355again:
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800356 smp_read_barrier_depends(); /* see post_one_send() */
357 if (sqp->s_last == ACCESS_ONCE(sqp->s_head))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400358 goto clr_busy;
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800359 wqe = rvt_get_swqe_ptr(sqp, sqp->s_last);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400360
361 /* Return if it is not OK to start a new work request. */
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800362 if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
363 if (!(ib_rvt_state_ops[sqp->state] & RVT_FLUSH_SEND))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400364 goto clr_busy;
365 /* We are in the error state, flush the work request. */
366 send_status = IB_WC_WR_FLUSH_ERR;
367 goto flush_send;
368 }
369
370 /*
371 * We can rely on the entry not changing without the s_lock
372 * being held until we update s_last.
373 * We increment s_cur to indicate s_last is in progress.
374 */
375 if (sqp->s_last == sqp->s_cur) {
376 if (++sqp->s_cur >= sqp->s_size)
377 sqp->s_cur = 0;
378 }
379 spin_unlock_irqrestore(&sqp->s_lock, flags);
380
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800381 if (!qp || !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) ||
Mike Marciniszyn77241052015-07-30 15:17:43 -0400382 qp->ibqp.qp_type != sqp->ibqp.qp_type) {
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800383 ibp->rvp.n_pkt_drops++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400384 /*
385 * For RC, the requester would timeout and retry so
386 * shortcut the timeouts and just signal too many retries.
387 */
388 if (sqp->ibqp.qp_type == IB_QPT_RC)
389 send_status = IB_WC_RETRY_EXC_ERR;
390 else
391 send_status = IB_WC_SUCCESS;
392 goto serr;
393 }
394
395 memset(&wc, 0, sizeof(wc));
396 send_status = IB_WC_SUCCESS;
397
Brian Welty0128fce2017-02-08 05:27:31 -0800398 release = true;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400399 sqp->s_sge.sge = wqe->sg_list[0];
400 sqp->s_sge.sg_list = wqe->sg_list + 1;
401 sqp->s_sge.num_sge = wqe->wr.num_sge;
402 sqp->s_len = wqe->length;
403 switch (wqe->wr.opcode) {
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700404 case IB_WR_REG_MR:
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700405 goto send_comp;
406
407 case IB_WR_LOCAL_INV:
Jianxin Xiongd9b13c22016-07-25 13:39:45 -0700408 if (!(wqe->wr.send_flags & RVT_SEND_COMPLETION_ONLY)) {
409 if (rvt_invalidate_rkey(sqp,
410 wqe->wr.ex.invalidate_rkey))
411 send_status = IB_WC_LOC_PROT_ERR;
412 local_ops = 1;
413 }
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700414 goto send_comp;
415
416 case IB_WR_SEND_WITH_INV:
417 if (!rvt_invalidate_rkey(qp, wqe->wr.ex.invalidate_rkey)) {
418 wc.wc_flags = IB_WC_WITH_INVALIDATE;
419 wc.ex.invalidate_rkey = wqe->wr.ex.invalidate_rkey;
420 }
421 goto send;
422
Mike Marciniszyn77241052015-07-30 15:17:43 -0400423 case IB_WR_SEND_WITH_IMM:
424 wc.wc_flags = IB_WC_WITH_IMM;
425 wc.ex.imm_data = wqe->wr.ex.imm_data;
426 /* FALLTHROUGH */
427 case IB_WR_SEND:
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700428send:
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800429 ret = hfi1_rvt_get_rwqe(qp, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400430 if (ret < 0)
431 goto op_err;
432 if (!ret)
433 goto rnr_nak;
434 break;
435
436 case IB_WR_RDMA_WRITE_WITH_IMM:
437 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
438 goto inv_err;
439 wc.wc_flags = IB_WC_WITH_IMM;
440 wc.ex.imm_data = wqe->wr.ex.imm_data;
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800441 ret = hfi1_rvt_get_rwqe(qp, 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400442 if (ret < 0)
443 goto op_err;
444 if (!ret)
445 goto rnr_nak;
Dean Luick7b0b01a2016-02-03 14:35:49 -0800446 /* skip copy_last set and qp_access_flags recheck */
447 goto do_write;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400448 case IB_WR_RDMA_WRITE:
Brian Welty0128fce2017-02-08 05:27:31 -0800449 copy_last = rvt_is_user_qp(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400450 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))
451 goto inv_err;
Dean Luick7b0b01a2016-02-03 14:35:49 -0800452do_write:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400453 if (wqe->length == 0)
Harish Chegondi42d6ec12016-03-05 08:49:24 -0800454 break;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800455 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, wqe->length,
456 wqe->rdma_wr.remote_addr,
457 wqe->rdma_wr.rkey,
458 IB_ACCESS_REMOTE_WRITE)))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400459 goto acc_err;
460 qp->r_sge.sg_list = NULL;
461 qp->r_sge.num_sge = 1;
462 qp->r_sge.total_len = wqe->length;
463 break;
464
465 case IB_WR_RDMA_READ:
466 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))
467 goto inv_err;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800468 if (unlikely(!rvt_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,
469 wqe->rdma_wr.remote_addr,
470 wqe->rdma_wr.rkey,
471 IB_ACCESS_REMOTE_READ)))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400472 goto acc_err;
Brian Welty0128fce2017-02-08 05:27:31 -0800473 release = false;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400474 sqp->s_sge.sg_list = NULL;
475 sqp->s_sge.num_sge = 1;
476 qp->r_sge.sge = wqe->sg_list[0];
477 qp->r_sge.sg_list = wqe->sg_list + 1;
478 qp->r_sge.num_sge = wqe->wr.num_sge;
479 qp->r_sge.total_len = wqe->length;
480 break;
481
482 case IB_WR_ATOMIC_CMP_AND_SWP:
483 case IB_WR_ATOMIC_FETCH_AND_ADD:
484 if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))
485 goto inv_err;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800486 if (unlikely(!rvt_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),
487 wqe->atomic_wr.remote_addr,
488 wqe->atomic_wr.rkey,
489 IB_ACCESS_REMOTE_ATOMIC)))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400490 goto acc_err;
491 /* Perform atomic OP and save result. */
Jubin John50e5dcb2016-02-14 20:19:41 -0800492 maddr = (atomic64_t *)qp->r_sge.sge.vaddr;
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100493 sdata = wqe->atomic_wr.compare_add;
Jubin John50e5dcb2016-02-14 20:19:41 -0800494 *(u64 *)sqp->s_sge.sge.vaddr =
Mike Marciniszyn77241052015-07-30 15:17:43 -0400495 (wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?
Jubin John50e5dcb2016-02-14 20:19:41 -0800496 (u64)atomic64_add_return(sdata, maddr) - sdata :
497 (u64)cmpxchg((u64 *)qp->r_sge.sge.vaddr,
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100498 sdata, wqe->atomic_wr.swap);
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800499 rvt_put_mr(qp->r_sge.sge.mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400500 qp->r_sge.num_sge = 0;
501 goto send_comp;
502
503 default:
504 send_status = IB_WC_LOC_QP_OP_ERR;
505 goto serr;
506 }
507
508 sge = &sqp->s_sge.sge;
509 while (sqp->s_len) {
510 u32 len = sqp->s_len;
511
512 if (len > sge->length)
513 len = sge->length;
514 if (len > sge->sge_length)
515 len = sge->sge_length;
516 WARN_ON_ONCE(len == 0);
Dean Luick7b0b01a2016-02-03 14:35:49 -0800517 hfi1_copy_sge(&qp->r_sge, sge->vaddr, len, release, copy_last);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400518 sge->vaddr += len;
519 sge->length -= len;
520 sge->sge_length -= len;
521 if (sge->sge_length == 0) {
522 if (!release)
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800523 rvt_put_mr(sge->mr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400524 if (--sqp->s_sge.num_sge)
525 *sge = *sqp->s_sge.sg_list++;
526 } else if (sge->length == 0 && sge->mr->lkey) {
Dennis Dalessandrocd4ceee2016-01-19 14:41:55 -0800527 if (++sge->n >= RVT_SEGSZ) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400528 if (++sge->m >= sge->mr->mapsz)
529 break;
530 sge->n = 0;
531 }
532 sge->vaddr =
533 sge->mr->map[sge->m]->segs[sge->n].vaddr;
534 sge->length =
535 sge->mr->map[sge->m]->segs[sge->n].length;
536 }
537 sqp->s_len -= len;
538 }
539 if (release)
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800540 rvt_put_ss(&qp->r_sge);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400541
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800542 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400543 goto send_comp;
544
545 if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)
546 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
547 else
548 wc.opcode = IB_WC_RECV;
549 wc.wr_id = qp->r_wr_id;
550 wc.status = IB_WC_SUCCESS;
551 wc.byte_len = wqe->length;
552 wc.qp = &qp->ibqp;
553 wc.src_qp = qp->remote_qpn;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400554 wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr);
555 wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400556 wc.port_num = 1;
557 /* Signal completion event if the solicited bit is set. */
Dennis Dalessandroabd712d2016-01-19 14:43:22 -0800558 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
559 wqe->wr.send_flags & IB_SEND_SOLICITED);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400560
561send_comp:
562 spin_lock_irqsave(&sqp->s_lock, flags);
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800563 ibp->rvp.n_loop_pkts++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400564flush_send:
565 sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;
566 hfi1_send_complete(sqp, wqe, send_status);
Jianxin Xiong0db3dfa2016-07-25 13:38:37 -0700567 if (local_ops) {
568 atomic_dec(&sqp->local_ops_pending);
569 local_ops = 0;
570 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400571 goto again;
572
573rnr_nak:
574 /* Handle RNR NAK */
575 if (qp->ibqp.qp_type == IB_QPT_UC)
576 goto send_comp;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800577 ibp->rvp.n_rnr_naks++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400578 /*
579 * Note: we don't need the s_lock held since the BUSY flag
580 * makes this single threaded.
581 */
582 if (sqp->s_rnr_retry == 0) {
583 send_status = IB_WC_RNR_RETRY_EXC_ERR;
584 goto serr;
585 }
586 if (sqp->s_rnr_retry_cnt < 7)
587 sqp->s_rnr_retry--;
588 spin_lock_irqsave(&sqp->s_lock, flags);
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800589 if (!(ib_rvt_state_ops[sqp->state] & RVT_PROCESS_RECV_OK))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400590 goto clr_busy;
Venkata Sandeep Dhanalakota56acbbf2017-02-08 05:27:19 -0800591 rvt_add_rnr_timer(sqp, qp->r_min_rnr_timer <<
Don Hiatt832666c2017-02-08 05:28:25 -0800592 IB_AETH_CREDIT_SHIFT);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400593 goto clr_busy;
594
595op_err:
596 send_status = IB_WC_REM_OP_ERR;
597 wc.status = IB_WC_LOC_QP_OP_ERR;
598 goto err;
599
600inv_err:
601 send_status = IB_WC_REM_INV_REQ_ERR;
602 wc.status = IB_WC_LOC_QP_OP_ERR;
603 goto err;
604
605acc_err:
606 send_status = IB_WC_REM_ACCESS_ERR;
607 wc.status = IB_WC_LOC_PROT_ERR;
608err:
609 /* responder goes to error state */
Brian Weltybeb5a042017-02-08 05:27:01 -0800610 rvt_rc_error(qp, wc.status);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400611
612serr:
613 spin_lock_irqsave(&sqp->s_lock, flags);
614 hfi1_send_complete(sqp, wqe, send_status);
615 if (sqp->ibqp.qp_type == IB_QPT_RC) {
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800616 int lastwqe = rvt_error_qp(sqp, IB_WC_WR_FLUSH_ERR);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400617
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800618 sqp->s_flags &= ~RVT_S_BUSY;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400619 spin_unlock_irqrestore(&sqp->s_lock, flags);
620 if (lastwqe) {
621 struct ib_event ev;
622
623 ev.device = sqp->ibqp.device;
624 ev.element.qp = &sqp->ibqp;
625 ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
626 sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);
627 }
628 goto done;
629 }
630clr_busy:
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800631 sqp->s_flags &= ~RVT_S_BUSY;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400632unlock:
633 spin_unlock_irqrestore(&sqp->s_lock, flags);
634done:
635 rcu_read_unlock();
636}
637
638/**
639 * hfi1_make_grh - construct a GRH header
640 * @ibp: a pointer to the IB port
641 * @hdr: a pointer to the GRH header being constructed
642 * @grh: the global route address to send to
643 * @hwords: the number of 32 bit words of header being sent
644 * @nwords: the number of 32 bit words of data being sent
645 *
646 * Return the size of the header in 32 bit words.
647 */
648u32 hfi1_make_grh(struct hfi1_ibport *ibp, struct ib_grh *hdr,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400649 const struct ib_global_route *grh, u32 hwords, u32 nwords)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400650{
651 hdr->version_tclass_flow =
652 cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
653 (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
654 (grh->flow_label << IB_GRH_FLOW_SHIFT));
655 hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
656 /* next_hdr is defined by C8-7 in ch. 8.4.1 */
657 hdr->next_hdr = IB_GRH_NEXT_HDR;
658 hdr->hop_limit = grh->hop_limit;
659 /* The SGID is 32-bit aligned. */
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800660 hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400661 hdr->sgid.global.interface_id =
Jakub Pawlaka6cd5f02016-10-17 04:19:30 -0700662 grh->sgid_index < HFI1_GUIDS_PER_PORT ?
663 get_sguid(ibp, grh->sgid_index) :
664 get_sguid(ibp, HFI1_PORT_GUID_INDEX);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400665 hdr->dgid = grh->dgid;
666
667 /* GRH header size in 32-bit words. */
668 return sizeof(struct ib_grh) / sizeof(u32);
669}
670
Don Hiatt30e07412017-08-04 13:54:04 -0700671#define BTH2_OFFSET (offsetof(struct hfi1_sdma_header, \
672 hdr.ibh.u.oth.bth[2]) / 4)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400673
674/**
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700675 * build_ahg - create ahg in s_ahg
Mike Marciniszyn77241052015-07-30 15:17:43 -0400676 * @qp: a pointer to QP
677 * @npsn: the next PSN for the request/response
678 *
679 * This routine handles the AHG by allocating an ahg entry and causing the
680 * copy of the first middle.
681 *
682 * Subsequent middles use the copied entry, editing the
683 * PSN with 1 or 2 edits.
684 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800685static inline void build_ahg(struct rvt_qp *qp, u32 npsn)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400686{
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800687 struct hfi1_qp_priv *priv = qp->priv;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800688
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800689 if (unlikely(qp->s_flags & RVT_S_AHG_CLEAR))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400690 clear_ahg(qp);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800691 if (!(qp->s_flags & RVT_S_AHG_VALID)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400692 /* first middle that needs copy */
Mike Marciniszynd7b8ba52015-11-09 19:13:59 -0500693 if (qp->s_ahgidx < 0)
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800694 qp->s_ahgidx = sdma_ahg_alloc(priv->s_sde);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400695 if (qp->s_ahgidx >= 0) {
696 qp->s_ahgpsn = npsn;
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700697 priv->s_ahg->tx_flags |= SDMA_TXREQ_F_AHG_COPY;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400698 /* save to protect a change in another thread */
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700699 priv->s_ahg->ahgidx = qp->s_ahgidx;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800700 qp->s_flags |= RVT_S_AHG_VALID;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400701 }
702 } else {
703 /* subsequent middle after valid */
704 if (qp->s_ahgidx >= 0) {
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700705 priv->s_ahg->tx_flags |= SDMA_TXREQ_F_USE_AHG;
706 priv->s_ahg->ahgidx = qp->s_ahgidx;
707 priv->s_ahg->ahgcount++;
708 priv->s_ahg->ahgdesc[0] =
Mike Marciniszyn77241052015-07-30 15:17:43 -0400709 sdma_build_ahg_descriptor(
710 (__force u16)cpu_to_be16((u16)npsn),
711 BTH2_OFFSET,
712 16,
713 16);
714 if ((npsn & 0xffff0000) !=
715 (qp->s_ahgpsn & 0xffff0000)) {
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700716 priv->s_ahg->ahgcount++;
717 priv->s_ahg->ahgdesc[1] =
Mike Marciniszyn77241052015-07-30 15:17:43 -0400718 sdma_build_ahg_descriptor(
719 (__force u16)cpu_to_be16(
720 (u16)(npsn >> 16)),
721 BTH2_OFFSET,
722 0,
723 16);
724 }
725 }
726 }
727}
728
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700729void hfi1_make_ruc_header(struct rvt_qp *qp, struct ib_other_headers *ohdr,
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800730 u32 bth0, u32 bth2, int middle,
731 struct hfi1_pkt_state *ps)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400732{
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800733 struct hfi1_qp_priv *priv = qp->priv;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800734 struct hfi1_ibport *ibp = ps->ibp;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400735 u16 lrh0;
736 u32 nwords;
737 u32 extra_bytes;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400738 u32 bth1;
739
740 /* Construct the header. */
Don Hiatte922ae02016-12-07 19:33:00 -0800741 extra_bytes = -ps->s_txreq->s_cur_size & 3;
742 nwords = (ps->s_txreq->s_cur_size + extra_bytes) >> 2;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400743 lrh0 = HFI1_LRH_BTH;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400744 if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)) {
745 qp->s_hdrwords +=
746 hfi1_make_grh(ibp,
Don Hiatt30e07412017-08-04 13:54:04 -0700747 &ps->s_txreq->phdr.hdr.ibh.u.l.grh,
748 &qp->remote_ah_attr.grh,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400749 qp->s_hdrwords, nwords);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400750 lrh0 = HFI1_LRH_GRH;
751 middle = 0;
752 }
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400753 lrh0 |= (priv->s_sc & 0xf) << 12 |
754 (rdma_ah_get_sl(&qp->remote_ah_attr) & 0xf) << 4;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400755 /*
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700756 * reset s_ahg/AHG fields
Mike Marciniszyn77241052015-07-30 15:17:43 -0400757 *
758 * This insures that the ahgentry/ahgcount
759 * are at a non-AHG default to protect
760 * build_verbs_tx_desc() from using
761 * an include ahgidx.
762 *
763 * build_ahg() will modify as appropriate
764 * to use the AHG feature.
765 */
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700766 priv->s_ahg->tx_flags = 0;
767 priv->s_ahg->ahgcount = 0;
768 priv->s_ahg->ahgidx = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400769 if (qp->s_mig_state == IB_MIG_MIGRATED)
770 bth0 |= IB_BTH_MIG_REQ;
771 else
772 middle = 0;
773 if (middle)
774 build_ahg(qp, bth2);
775 else
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800776 qp->s_flags &= ~RVT_S_AHG_VALID;
Don Hiatt30e07412017-08-04 13:54:04 -0700777 ps->s_txreq->phdr.hdr.ibh.lrh[0] = cpu_to_be16(lrh0);
778 ps->s_txreq->phdr.hdr.ibh.lrh[1] =
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400779 cpu_to_be16(rdma_ah_get_dlid(&qp->remote_ah_attr));
Don Hiatt30e07412017-08-04 13:54:04 -0700780 ps->s_txreq->phdr.hdr.ibh.lrh[2] =
Mike Marciniszyn77241052015-07-30 15:17:43 -0400781 cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
Don Hiatt30e07412017-08-04 13:54:04 -0700782 ps->s_txreq->phdr.hdr.ibh.lrh[3] =
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400783 cpu_to_be16(ppd_from_ibp(ibp)->lid |
Don Hiatt30e07412017-08-04 13:54:04 -0700784 rdma_ah_get_path_bits(&qp->remote_ah_attr));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400785 bth0 |= hfi1_get_pkey(ibp, qp->s_pkey_index);
786 bth0 |= extra_bytes << 20;
787 ohdr->bth[0] = cpu_to_be32(bth0);
788 bth1 = qp->remote_qpn;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800789 if (qp->s_flags & RVT_S_ECN) {
790 qp->s_flags &= ~RVT_S_ECN;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400791 /* we recently received a FECN, so return a BECN */
Don Hiatt3d591092017-04-09 10:16:28 -0700792 bth1 |= (IB_BECN_MASK << IB_BECN_SHIFT);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400793 }
794 ohdr->bth[1] = cpu_to_be32(bth1);
795 ohdr->bth[2] = cpu_to_be32(bth2);
796}
797
Dean Luickb4219222015-10-26 10:28:35 -0400798/* when sending, force a reschedule every one of these periods */
799#define SEND_RESCHED_TIMEOUT (5 * HZ) /* 5s in jiffies */
800
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700801/**
802 * schedule_send_yield - test for a yield required for QP send engine
803 * @timeout: Final time for timeout slice for jiffies
804 * @qp: a pointer to QP
805 * @ps: a pointer to a structure with commonly lookup values for
806 * the the send engine progress
807 *
808 * This routine checks if the time slice for the QP has expired
809 * for RC QPs, if so an additional work entry is queued. At this
810 * point, other QPs have an opportunity to be scheduled. It
811 * returns true if a yield is required, otherwise, false
812 * is returned.
813 */
814static bool schedule_send_yield(struct rvt_qp *qp,
815 struct hfi1_pkt_state *ps)
816{
Kaike Wanbcad2912017-07-24 07:45:37 -0700817 ps->pkts_sent = true;
818
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700819 if (unlikely(time_after(jiffies, ps->timeout))) {
820 if (!ps->in_thread ||
821 workqueue_congested(ps->cpu, ps->ppd->hfi1_wq)) {
822 spin_lock_irqsave(&qp->s_lock, ps->flags);
823 qp->s_flags &= ~RVT_S_BUSY;
824 hfi1_schedule_send(qp);
825 spin_unlock_irqrestore(&qp->s_lock, ps->flags);
826 this_cpu_inc(*ps->ppd->dd->send_schedule);
827 trace_hfi1_rc_expired_time_slice(qp, true);
828 return true;
829 }
830
831 cond_resched();
832 this_cpu_inc(*ps->ppd->dd->send_schedule);
833 ps->timeout = jiffies + ps->timeout_int;
834 }
835
836 trace_hfi1_rc_expired_time_slice(qp, false);
837 return false;
838}
839
Mike Marciniszynb6eac932017-04-09 10:16:35 -0700840void hfi1_do_send_from_rvt(struct rvt_qp *qp)
841{
842 hfi1_do_send(qp, false);
843}
844
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800845void _hfi1_do_send(struct work_struct *work)
846{
847 struct iowait *wait = container_of(work, struct iowait, iowork);
848 struct rvt_qp *qp = iowait_to_qp(wait);
849
Mike Marciniszynb6eac932017-04-09 10:16:35 -0700850 hfi1_do_send(qp, true);
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800851}
852
Mike Marciniszyn77241052015-07-30 15:17:43 -0400853/**
854 * hfi1_do_send - perform a send on a QP
855 * @work: contains a pointer to the QP
Mike Marciniszynb6eac932017-04-09 10:16:35 -0700856 * @in_thread: true if in a workqueue thread
Mike Marciniszyn77241052015-07-30 15:17:43 -0400857 *
858 * Process entries in the send work queue until credit or queue is
Dennis Dalessandroca00c622016-09-25 07:42:08 -0700859 * exhausted. Only allow one CPU to send a packet per QP.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400860 * Otherwise, two threads could send packets out of order.
861 */
Mike Marciniszynb6eac932017-04-09 10:16:35 -0700862void hfi1_do_send(struct rvt_qp *qp, bool in_thread)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400863{
Dennis Dalessandrod46e5142015-11-11 00:34:37 -0500864 struct hfi1_pkt_state ps;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800865 struct hfi1_qp_priv *priv = qp->priv;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800866 int (*make_req)(struct rvt_qp *qp, struct hfi1_pkt_state *ps);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400867
Dennis Dalessandrod46e5142015-11-11 00:34:37 -0500868 ps.dev = to_idev(qp->ibqp.device);
869 ps.ibp = to_iport(qp->ibqp.device, qp->port_num);
870 ps.ppd = ppd_from_ibp(ps.ibp);
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700871 ps.in_thread = in_thread;
872
873 trace_hfi1_rc_do_send(qp, in_thread);
Dennis Dalessandrod46e5142015-11-11 00:34:37 -0500874
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800875 switch (qp->ibqp.qp_type) {
876 case IB_QPT_RC:
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400877 if (!loopback && ((rdma_ah_get_dlid(&qp->remote_ah_attr) &
878 ~((1 << ps.ppd->lmc) - 1)) ==
879 ps.ppd->lid)) {
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800880 ruc_loopback(qp);
881 return;
882 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400883 make_req = hfi1_make_rc_req;
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700884 ps.timeout_int = qp->timeout_jiffies;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800885 break;
886 case IB_QPT_UC:
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400887 if (!loopback && ((rdma_ah_get_dlid(&qp->remote_ah_attr) &
888 ~((1 << ps.ppd->lmc) - 1)) ==
889 ps.ppd->lid)) {
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800890 ruc_loopback(qp);
891 return;
892 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400893 make_req = hfi1_make_uc_req;
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700894 ps.timeout_int = SEND_RESCHED_TIMEOUT;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800895 break;
896 default:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400897 make_req = hfi1_make_ud_req;
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700898 ps.timeout_int = SEND_RESCHED_TIMEOUT;
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800899 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400900
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700901 spin_lock_irqsave(&qp->s_lock, ps.flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400902
903 /* Return if we are already busy processing a work request. */
904 if (!hfi1_send_ok(qp)) {
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700905 spin_unlock_irqrestore(&qp->s_lock, ps.flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400906 return;
907 }
908
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800909 qp->s_flags |= RVT_S_BUSY;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400910
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700911 ps.timeout_int = ps.timeout_int / 8;
912 ps.timeout = jiffies + ps.timeout_int;
913 ps.cpu = priv->s_sde ? priv->s_sde->cpu :
Vennila Megavannan23cd4712016-02-03 14:34:23 -0800914 cpumask_first(cpumask_of_node(ps.ppd->dd->node));
Kaike Wanbcad2912017-07-24 07:45:37 -0700915 ps.pkts_sent = false;
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700916
Mike Marciniszyn711e1042016-02-14 12:45:18 -0800917 /* insure a pre-built packet is handled */
918 ps.s_txreq = get_waiting_verbs_txreq(qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400919 do {
920 /* Check for a constructed packet to be sent. */
921 if (qp->s_hdrwords != 0) {
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700922 spin_unlock_irqrestore(&qp->s_lock, ps.flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400923 /*
924 * If the packet cannot be sent now, return and
Dennis Dalessandroca00c622016-09-25 07:42:08 -0700925 * the send engine will be woken up later.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400926 */
Dennis Dalessandrod46e5142015-11-11 00:34:37 -0500927 if (hfi1_verbs_send(qp, &ps))
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800928 return;
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700929 /* Record that s_ahg is empty. */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400930 qp->s_hdrwords = 0;
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800931 /* allow other tasks to run */
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700932 if (schedule_send_yield(qp, &ps))
933 return;
934
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700935 spin_lock_irqsave(&qp->s_lock, ps.flags);
Dean Luickb4219222015-10-26 10:28:35 -0400936 }
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800937 } while (make_req(qp, &ps));
Kaike Wanbcad2912017-07-24 07:45:37 -0700938 iowait_starve_clear(ps.pkts_sent, &priv->s_iowait);
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700939 spin_unlock_irqrestore(&qp->s_lock, ps.flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400940}
941
942/*
943 * This should be called with s_lock held.
944 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800945void hfi1_send_complete(struct rvt_qp *qp, struct rvt_swqe *wqe,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400946 enum ib_wc_status status)
947{
948 u32 old_last, last;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400949
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800950 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_OR_FLUSH_SEND))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400951 return;
952
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -0800953 last = qp->s_last;
954 old_last = last;
Mike Marciniszyn9260b352017-03-20 17:25:23 -0700955 trace_hfi1_qp_send_completion(qp, wqe, last);
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -0800956 if (++last >= qp->s_size)
957 last = 0;
Mike Marciniszyn9260b352017-03-20 17:25:23 -0700958 trace_hfi1_qp_send_completion(qp, wqe, last);
Mike Marciniszyn6c2ab0b2016-02-04 11:03:19 -0800959 qp->s_last = last;
960 /* See post_send() */
961 barrier();
Mike Marciniszync64607a2016-12-07 19:34:31 -0800962 rvt_put_swqe(wqe);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400963 if (qp->ibqp.qp_type == IB_QPT_UD ||
964 qp->ibqp.qp_type == IB_QPT_SMI ||
965 qp->ibqp.qp_type == IB_QPT_GSI)
Dennis Dalessandro15723f02016-01-19 14:42:17 -0800966 atomic_dec(&ibah_to_rvtah(wqe->ud_wr.ah)->refcount);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400967
Mike Marciniszyn43a474a2017-03-20 17:25:04 -0700968 rvt_qp_swqe_complete(qp,
969 wqe,
970 ib_hfi1_wc_opcode[wqe->wr.opcode],
971 status);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400972
Mike Marciniszyn77241052015-07-30 15:17:43 -0400973 if (qp->s_acked == old_last)
974 qp->s_acked = last;
975 if (qp->s_cur == old_last)
976 qp->s_cur = last;
977 if (qp->s_tail == old_last)
978 qp->s_tail = last;
979 if (qp->state == IB_QPS_SQD && last == qp->s_cur)
980 qp->s_draining = 0;
981}