blob: b708376b67dacf11daba51eba8dca9dc56be978f [file] [log] [blame]
Mike Marciniszyn77241052015-07-30 15:17:43 -04001/*
Jubin John05d6ac12016-02-14 20:22:17 -08002 * Copyright(c) 2015, 2016 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/net.h>
49#include <rdma/ib_smi.h>
50
51#include "hfi.h"
52#include "mad.h"
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -080053#include "verbs_txreq.h"
Mike Marciniszyn711e1042016-02-14 12:45:18 -080054#include "qp.h"
Mike Marciniszyn77241052015-07-30 15:17:43 -040055
56/**
57 * ud_loopback - handle send on loopback QPs
58 * @sqp: the sending QP
59 * @swqe: the send work request
60 *
61 * This is called from hfi1_make_ud_req() to forward a WQE addressed
62 * to the same HFI.
63 * Note that the receive interrupt handler may be calling hfi1_ud_rcv()
64 * while this is being called.
65 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -080066static void ud_loopback(struct rvt_qp *sqp, struct rvt_swqe *swqe)
Mike Marciniszyn77241052015-07-30 15:17:43 -040067{
68 struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
69 struct hfi1_pportdata *ppd;
Dennis Dalessandro895420d2016-01-19 14:42:28 -080070 struct rvt_qp *qp;
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -040071 struct rdma_ah_attr *ah_attr;
Mike Marciniszyn77241052015-07-30 15:17:43 -040072 unsigned long flags;
Dennis Dalessandro895420d2016-01-19 14:42:28 -080073 struct rvt_sge_state ssge;
74 struct rvt_sge *sge;
Mike Marciniszyn77241052015-07-30 15:17:43 -040075 struct ib_wc wc;
76 u32 length;
77 enum ib_qp_type sqptype, dqptype;
78
79 rcu_read_lock();
80
Dennis Dalessandroec4274f2016-01-19 14:43:44 -080081 qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
82 swqe->ud_wr.remote_qpn);
Mike Marciniszyn77241052015-07-30 15:17:43 -040083 if (!qp) {
Dennis Dalessandro4eb06882016-01-19 14:42:39 -080084 ibp->rvp.n_pkt_drops++;
Mike Marciniszyn77241052015-07-30 15:17:43 -040085 rcu_read_unlock();
86 return;
87 }
88
89 sqptype = sqp->ibqp.qp_type == IB_QPT_GSI ?
90 IB_QPT_UD : sqp->ibqp.qp_type;
91 dqptype = qp->ibqp.qp_type == IB_QPT_GSI ?
92 IB_QPT_UD : qp->ibqp.qp_type;
93
94 if (dqptype != sqptype ||
Dennis Dalessandro83693bd2016-01-19 14:43:33 -080095 !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
Dennis Dalessandro4eb06882016-01-19 14:42:39 -080096 ibp->rvp.n_pkt_drops++;
Mike Marciniszyn77241052015-07-30 15:17:43 -040097 goto drop;
98 }
99
Dennis Dalessandro15723f02016-01-19 14:42:17 -0800100 ah_attr = &ibah_to_rvtah(swqe->ud_wr.ah)->attr;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400101 ppd = ppd_from_ibp(ibp);
102
103 if (qp->ibqp.qp_num > 1) {
104 u16 pkey;
105 u16 slid;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400106 u8 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
Mike Marciniszyn77241052015-07-30 15:17:43 -0400107
108 pkey = hfi1_get_pkey(ibp, sqp->s_pkey_index);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400109 slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
Mike Marciniszyn77241052015-07-30 15:17:43 -0400110 ((1 << ppd->lmc) - 1));
111 if (unlikely(ingress_pkey_check(ppd, pkey, sc5,
Don Hiatt5786adf32017-08-04 13:54:10 -0700112 qp->s_pkey_index,
113 slid, false))) {
Dennis Dalessandro13d84912017-05-29 17:22:01 -0700114 hfi1_bad_pkey(ibp, pkey,
115 rdma_ah_get_sl(ah_attr),
116 sqp->ibqp.qp_num, qp->ibqp.qp_num,
117 slid, rdma_ah_get_dlid(ah_attr));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400118 goto drop;
119 }
120 }
121
122 /*
123 * Check that the qkey matches (except for QP0, see 9.6.1.4.1).
124 * Qkeys with the high order bit set mean use the
125 * qkey from the QP context instead of the WR (see 10.2.5).
126 */
127 if (qp->ibqp.qp_num) {
128 u32 qkey;
129
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100130 qkey = (int)swqe->ud_wr.remote_qkey < 0 ?
131 sqp->qkey : swqe->ud_wr.remote_qkey;
Dennis Dalessandro13d84912017-05-29 17:22:01 -0700132 if (unlikely(qkey != qp->qkey))
133 goto drop; /* silently drop per IBTA spec */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400134 }
135
136 /*
137 * A GRH is expected to precede the data even if not
138 * present on the wire.
139 */
140 length = swqe->length;
141 memset(&wc, 0, sizeof(wc));
142 wc.byte_len = length + sizeof(struct ib_grh);
143
144 if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
145 wc.wc_flags = IB_WC_WITH_IMM;
146 wc.ex.imm_data = swqe->wr.ex.imm_data;
147 }
148
149 spin_lock_irqsave(&qp->r_lock, flags);
150
151 /*
152 * Get the next work request entry to find where to put the data.
153 */
Jubin Johne4909742016-02-14 20:22:00 -0800154 if (qp->r_flags & RVT_R_REUSE_SGE) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800155 qp->r_flags &= ~RVT_R_REUSE_SGE;
Jubin Johne4909742016-02-14 20:22:00 -0800156 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400157 int ret;
158
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800159 ret = hfi1_rvt_get_rwqe(qp, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400160 if (ret < 0) {
Brian Weltybeb5a042017-02-08 05:27:01 -0800161 rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400162 goto bail_unlock;
163 }
164 if (!ret) {
165 if (qp->ibqp.qp_num == 0)
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800166 ibp->rvp.n_vl15_dropped++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400167 goto bail_unlock;
168 }
169 }
170 /* Silently drop packets which are too big. */
171 if (unlikely(wc.byte_len > qp->r_len)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800172 qp->r_flags |= RVT_R_REUSE_SGE;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800173 ibp->rvp.n_pkt_drops++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400174 goto bail_unlock;
175 }
176
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400177 if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
Dasaratharaman Chandramouli527dbf12016-07-25 13:40:40 -0700178 struct ib_grh grh;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400179 const struct ib_global_route *grd = rdma_ah_read_grh(ah_attr);
Dasaratharaman Chandramouli527dbf12016-07-25 13:40:40 -0700180
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400181 hfi1_make_grh(ibp, &grh, grd, 0, 0);
Dasaratharaman Chandramouli527dbf12016-07-25 13:40:40 -0700182 hfi1_copy_sge(&qp->r_sge, &grh,
Brian Welty0128fce2017-02-08 05:27:31 -0800183 sizeof(grh), true, false);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400184 wc.wc_flags |= IB_WC_GRH;
Jubin Johne4909742016-02-14 20:22:00 -0800185 } else {
Brian Welty1198fce2017-02-08 05:27:37 -0800186 rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
Jubin Johne4909742016-02-14 20:22:00 -0800187 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400188 ssge.sg_list = swqe->sg_list + 1;
189 ssge.sge = *swqe->sg_list;
190 ssge.num_sge = swqe->wr.num_sge;
191 sge = &ssge.sge;
192 while (length) {
193 u32 len = sge->length;
194
195 if (len > length)
196 len = length;
197 if (len > sge->sge_length)
198 len = sge->sge_length;
199 WARN_ON_ONCE(len == 0);
Brian Welty0128fce2017-02-08 05:27:31 -0800200 hfi1_copy_sge(&qp->r_sge, sge->vaddr, len, true, false);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400201 sge->vaddr += len;
202 sge->length -= len;
203 sge->sge_length -= len;
204 if (sge->sge_length == 0) {
205 if (--ssge.num_sge)
206 *sge = *ssge.sg_list++;
207 } else if (sge->length == 0 && sge->mr->lkey) {
Dennis Dalessandrocd4ceee2016-01-19 14:41:55 -0800208 if (++sge->n >= RVT_SEGSZ) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400209 if (++sge->m >= sge->mr->mapsz)
210 break;
211 sge->n = 0;
212 }
213 sge->vaddr =
214 sge->mr->map[sge->m]->segs[sge->n].vaddr;
215 sge->length =
216 sge->mr->map[sge->m]->segs[sge->n].length;
217 }
218 length -= len;
219 }
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800220 rvt_put_ss(&qp->r_sge);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800221 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400222 goto bail_unlock;
223 wc.wr_id = qp->r_wr_id;
224 wc.status = IB_WC_SUCCESS;
225 wc.opcode = IB_WC_RECV;
226 wc.qp = &qp->ibqp;
227 wc.src_qp = sqp->ibqp.qp_num;
228 if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI) {
229 if (sqp->ibqp.qp_type == IB_QPT_GSI ||
230 sqp->ibqp.qp_type == IB_QPT_SMI)
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100231 wc.pkey_index = swqe->ud_wr.pkey_index;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400232 else
233 wc.pkey_index = sqp->s_pkey_index;
234 } else {
235 wc.pkey_index = 0;
236 }
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400237 wc.slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
238 ((1 << ppd->lmc) - 1));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400239 /* Check for loopback when the port lid is not set */
240 if (wc.slid == 0 && sqp->ibqp.qp_type == IB_QPT_GSI)
Dennis Dalessandro8859b4a2016-01-19 14:42:11 -0800241 wc.slid = be16_to_cpu(IB_LID_PERMISSIVE);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400242 wc.sl = rdma_ah_get_sl(ah_attr);
243 wc.dlid_path_bits = rdma_ah_get_dlid(ah_attr) & ((1 << ppd->lmc) - 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400244 wc.port_num = qp->port_num;
245 /* Signal completion event if the solicited bit is set. */
Dennis Dalessandroabd712d2016-01-19 14:43:22 -0800246 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
247 swqe->wr.send_flags & IB_SEND_SOLICITED);
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800248 ibp->rvp.n_loop_pkts++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400249bail_unlock:
250 spin_unlock_irqrestore(&qp->r_lock, flags);
251drop:
252 rcu_read_unlock();
253}
254
255/**
256 * hfi1_make_ud_req - construct a UD request packet
257 * @qp: the QP
258 *
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800259 * Assume s_lock is held.
260 *
Mike Marciniszyn77241052015-07-30 15:17:43 -0400261 * Return 1 if constructed; otherwise, return 0.
262 */
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800263int hfi1_make_ud_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400264{
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800265 struct hfi1_qp_priv *priv = qp->priv;
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700266 struct ib_other_headers *ohdr;
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -0400267 struct rdma_ah_attr *ah_attr;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400268 struct hfi1_pportdata *ppd;
269 struct hfi1_ibport *ibp;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800270 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400271 u32 nwords;
272 u32 extra_bytes;
273 u32 bth0;
274 u16 lrh0;
275 u16 lid;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400276 int next_cur;
277 u8 sc5;
278
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800279 ps->s_txreq = get_txreq(ps->dev, qp);
280 if (IS_ERR(ps->s_txreq))
281 goto bail_no_tx;
282
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800283 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
284 if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400285 goto bail;
286 /* We are in the error state, flush the work request. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800287 smp_read_barrier_depends(); /* see post_one_send */
288 if (qp->s_last == ACCESS_ONCE(qp->s_head))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400289 goto bail;
290 /* If DMAs are in progress, we can't flush immediately. */
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800291 if (iowait_sdma_pending(&priv->s_iowait)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800292 qp->s_flags |= RVT_S_WAIT_DMA;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400293 goto bail;
294 }
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800295 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400296 hfi1_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800297 goto done_free_tx;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400298 }
299
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800300 /* see post_one_send() */
301 smp_read_barrier_depends();
302 if (qp->s_cur == ACCESS_ONCE(qp->s_head))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400303 goto bail;
304
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800305 wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400306 next_cur = qp->s_cur + 1;
307 if (next_cur >= qp->s_size)
308 next_cur = 0;
309
310 /* Construct the header. */
311 ibp = to_iport(qp->ibqp.device, qp->port_num);
312 ppd = ppd_from_ibp(ibp);
Dennis Dalessandro15723f02016-01-19 14:42:17 -0800313 ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400314 if (rdma_ah_get_dlid(ah_attr) < be16_to_cpu(IB_MULTICAST_LID_BASE) ||
315 rdma_ah_get_dlid(ah_attr) == be16_to_cpu(IB_LID_PERMISSIVE)) {
316 lid = rdma_ah_get_dlid(ah_attr) & ~((1 << ppd->lmc) - 1);
Jubin John17fb4f22016-02-14 20:21:52 -0800317 if (unlikely(!loopback &&
318 (lid == ppd->lid ||
319 (lid == be16_to_cpu(IB_LID_PERMISSIVE) &&
320 qp->ibqp.qp_type == IB_QPT_GSI)))) {
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700321 unsigned long tflags = ps->flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400322 /*
323 * If DMAs are in progress, we can't generate
324 * a completion for the loopback packet since
325 * it would be out of order.
326 * Instead of waiting, we could queue a
327 * zero length descriptor so we get a callback.
328 */
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800329 if (iowait_sdma_pending(&priv->s_iowait)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800330 qp->s_flags |= RVT_S_WAIT_DMA;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400331 goto bail;
332 }
333 qp->s_cur = next_cur;
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700334 spin_unlock_irqrestore(&qp->s_lock, tflags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400335 ud_loopback(qp, wqe);
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700336 spin_lock_irqsave(&qp->s_lock, tflags);
337 ps->flags = tflags;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400338 hfi1_send_complete(qp, wqe, IB_WC_SUCCESS);
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800339 goto done_free_tx;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400340 }
341 }
342
343 qp->s_cur = next_cur;
344 extra_bytes = -wqe->length & 3;
345 nwords = (wqe->length + extra_bytes) >> 2;
346
347 /* header size in 32-bit words LRH+BTH+DETH = (8+12+8)/4. */
348 qp->s_hdrwords = 7;
Don Hiatte922ae02016-12-07 19:33:00 -0800349 ps->s_txreq->s_cur_size = wqe->length;
Mitko Haralanovb777f152016-12-07 19:33:27 -0800350 ps->s_txreq->ss = &qp->s_sge;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400351 qp->s_srate = rdma_ah_get_static_rate(ah_attr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400352 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
353 qp->s_wqe = wqe;
354 qp->s_sge.sge = wqe->sg_list[0];
355 qp->s_sge.sg_list = wqe->sg_list + 1;
356 qp->s_sge.num_sge = wqe->wr.num_sge;
357 qp->s_sge.total_len = wqe->length;
358
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400359 if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400360 /* Header size in 32-bit words. */
Don Hiatt30e07412017-08-04 13:54:04 -0700361 qp->s_hdrwords +=
362 hfi1_make_grh(ibp,
363 &ps->s_txreq->phdr.hdr.ibh.u.l.grh,
364 rdma_ah_read_grh(ah_attr),
365 qp->s_hdrwords, nwords);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400366 lrh0 = HFI1_LRH_GRH;
Don Hiatt30e07412017-08-04 13:54:04 -0700367 ohdr = &ps->s_txreq->phdr.hdr.ibh.u.l.oth;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400368 /*
369 * Don't worry about sending to locally attached multicast
370 * QPs. It is unspecified by the spec. what happens.
371 */
372 } else {
373 /* Header size in 32-bit words. */
374 lrh0 = HFI1_LRH_BTH;
Don Hiatt30e07412017-08-04 13:54:04 -0700375 ohdr = &ps->s_txreq->phdr.hdr.ibh.u.oth;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400376 }
377 if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
378 qp->s_hdrwords++;
379 ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;
380 bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
Jubin Johne4909742016-02-14 20:22:00 -0800381 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400382 bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
Jubin Johne4909742016-02-14 20:22:00 -0800383 }
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400384 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
385 lrh0 |= (rdma_ah_get_sl(ah_attr) & 0xf) << 4;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400386 if (qp->ibqp.qp_type == IB_QPT_SMI) {
387 lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800388 priv->s_sc = 0xf;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400389 } else {
390 lrh0 |= (sc5 & 0xf) << 12;
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800391 priv->s_sc = sc5;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400392 }
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800393 priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
Mike Marciniszyn711e1042016-02-14 12:45:18 -0800394 ps->s_txreq->sde = priv->s_sde;
Jubin John721d0422016-02-14 12:45:00 -0800395 priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
Mike Marciniszyn711e1042016-02-14 12:45:18 -0800396 ps->s_txreq->psc = priv->s_sendcontext;
Don Hiatt30e07412017-08-04 13:54:04 -0700397 ps->s_txreq->phdr.hdr.ibh.lrh[0] = cpu_to_be16(lrh0);
398 ps->s_txreq->phdr.hdr.ibh.lrh[1] =
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400399 cpu_to_be16(rdma_ah_get_dlid(ah_attr));
Don Hiatt30e07412017-08-04 13:54:04 -0700400 ps->s_txreq->phdr.hdr.ibh.lrh[2] =
Mike Marciniszyn77241052015-07-30 15:17:43 -0400401 cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400402 if (rdma_ah_get_dlid(ah_attr) == be16_to_cpu(IB_LID_PERMISSIVE)) {
Don Hiatt30e07412017-08-04 13:54:04 -0700403 ps->s_txreq->phdr.hdr.ibh.lrh[3] = IB_LID_PERMISSIVE;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800404 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400405 lid = ppd->lid;
406 if (lid) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400407 lid |= rdma_ah_get_path_bits(ah_attr) &
408 ((1 << ppd->lmc) - 1);
Don Hiatt30e07412017-08-04 13:54:04 -0700409 ps->s_txreq->phdr.hdr.ibh.lrh[3] = cpu_to_be16(lid);
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800410 } else {
Don Hiatt30e07412017-08-04 13:54:04 -0700411 ps->s_txreq->phdr.hdr.ibh.lrh[3] = IB_LID_PERMISSIVE;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800412 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400413 }
414 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
415 bth0 |= IB_BTH_SOLICITED;
416 bth0 |= extra_bytes << 20;
417 if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI)
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100418 bth0 |= hfi1_get_pkey(ibp, wqe->ud_wr.pkey_index);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400419 else
420 bth0 |= hfi1_get_pkey(ibp, qp->s_pkey_index);
421 ohdr->bth[0] = cpu_to_be32(bth0);
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100422 ohdr->bth[1] = cpu_to_be32(wqe->ud_wr.remote_qpn);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800423 ohdr->bth[2] = cpu_to_be32(mask_psn(wqe->psn));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400424 /*
425 * Qkeys with the high order bit set mean use the
426 * qkey from the QP context instead of the WR (see 10.2.5).
427 */
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100428 ohdr->u.ud.deth[0] = cpu_to_be32((int)wqe->ud_wr.remote_qkey < 0 ?
429 qp->qkey : wqe->ud_wr.remote_qkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400430 ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
431 /* disarm any ahg */
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700432 priv->s_ahg->ahgcount = 0;
433 priv->s_ahg->ahgidx = 0;
434 priv->s_ahg->tx_flags = 0;
Mike Marciniszyn711e1042016-02-14 12:45:18 -0800435 /* pbc */
436 ps->s_txreq->hdr_dwords = qp->s_hdrwords + 2;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400437
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800438 return 1;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800439
440done_free_tx:
441 hfi1_put_txreq(ps->s_txreq);
442 ps->s_txreq = NULL;
443 return 1;
444
Mike Marciniszyn77241052015-07-30 15:17:43 -0400445bail:
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800446 hfi1_put_txreq(ps->s_txreq);
447
448bail_no_tx:
449 ps->s_txreq = NULL;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800450 qp->s_flags &= ~RVT_S_BUSY;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800451 qp->s_hdrwords = 0;
452 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400453}
454
455/*
456 * Hardware can't check this so we do it here.
457 *
458 * This is a slightly different algorithm than the standard pkey check. It
459 * special cases the management keys and allows for 0x7fff and 0xffff to be in
460 * the table at the same time.
461 *
462 * @returns the index found or -1 if not found
463 */
464int hfi1_lookup_pkey_idx(struct hfi1_ibport *ibp, u16 pkey)
465{
466 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
467 unsigned i;
468
469 if (pkey == FULL_MGMT_P_KEY || pkey == LIM_MGMT_P_KEY) {
470 unsigned lim_idx = -1;
471
472 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i) {
473 /* here we look for an exact match */
474 if (ppd->pkeys[i] == pkey)
475 return i;
476 if (ppd->pkeys[i] == LIM_MGMT_P_KEY)
477 lim_idx = i;
478 }
479
480 /* did not find 0xffff return 0x7fff idx if found */
481 if (pkey == FULL_MGMT_P_KEY)
482 return lim_idx;
483
484 /* no match... */
485 return -1;
486 }
487
488 pkey &= 0x7fff; /* remove limited/full membership bit */
489
490 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i)
491 if ((ppd->pkeys[i] & 0x7fff) == pkey)
492 return i;
493
494 /*
495 * Should not get here, this means hardware failed to validate pkeys.
496 */
497 return -1;
498}
499
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800500void return_cnp(struct hfi1_ibport *ibp, struct rvt_qp *qp, u32 remote_qpn,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400501 u32 pkey, u32 slid, u32 dlid, u8 sc5,
502 const struct ib_grh *old_grh)
503{
504 u64 pbc, pbc_flags = 0;
505 u32 bth0, plen, vl, hwords = 5;
506 u16 lrh0;
507 u8 sl = ibp->sc_to_sl[sc5];
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700508 struct ib_header hdr;
509 struct ib_other_headers *ohdr;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400510 struct pio_buf *pbuf;
511 struct send_context *ctxt = qp_to_send_context(qp, sc5);
512 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
513
514 if (old_grh) {
515 struct ib_grh *grh = &hdr.u.l.grh;
516
517 grh->version_tclass_flow = old_grh->version_tclass_flow;
518 grh->paylen = cpu_to_be16((hwords - 2 + SIZE_OF_CRC) << 2);
519 grh->hop_limit = 0xff;
520 grh->sgid = old_grh->dgid;
521 grh->dgid = old_grh->sgid;
522 ohdr = &hdr.u.l.oth;
523 lrh0 = HFI1_LRH_GRH;
524 hwords += sizeof(struct ib_grh) / sizeof(u32);
525 } else {
526 ohdr = &hdr.u.oth;
527 lrh0 = HFI1_LRH_BTH;
528 }
529
530 lrh0 |= (sc5 & 0xf) << 12 | sl << 4;
531
532 bth0 = pkey | (IB_OPCODE_CNP << 24);
533 ohdr->bth[0] = cpu_to_be32(bth0);
534
Don Hiatt3d591092017-04-09 10:16:28 -0700535 ohdr->bth[1] = cpu_to_be32(remote_qpn | (1 << IB_BECN_SHIFT));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400536 ohdr->bth[2] = 0; /* PSN 0 */
537
538 hdr.lrh[0] = cpu_to_be16(lrh0);
539 hdr.lrh[1] = cpu_to_be16(dlid);
540 hdr.lrh[2] = cpu_to_be16(hwords + SIZE_OF_CRC);
541 hdr.lrh[3] = cpu_to_be16(slid);
542
543 plen = 2 /* PBC */ + hwords;
Don Hiatt7dafbab2017-05-12 09:19:55 -0700544 pbc_flags |= (ib_is_sc5(sc5) << PBC_DC_INFO_SHIFT);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400545 vl = sc_to_vlt(ppd->dd, sc5);
546 pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
547 if (ctxt) {
548 pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
549 if (pbuf)
550 ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
551 &hdr, hwords);
552 }
553}
554
555/*
556 * opa_smp_check() - Do the regular pkey checking, and the additional
Jianxin Xiongf3809202016-09-30 20:11:09 -0700557 * checks for SMPs specified in OPAv1 rev 1.0, 9/19/2016 update, section
558 * 9.10.25 ("SMA Packet Checks").
Mike Marciniszyn77241052015-07-30 15:17:43 -0400559 *
560 * Note that:
561 * - Checks are done using the pkey directly from the packet's BTH,
562 * and specifically _not_ the pkey that we attach to the completion,
563 * which may be different.
564 * - These checks are specifically for "non-local" SMPs (i.e., SMPs
565 * which originated on another node). SMPs which are sent from, and
566 * destined to this node are checked in opa_local_smp_check().
567 *
568 * At the point where opa_smp_check() is called, we know:
569 * - destination QP is QP0
570 *
571 * opa_smp_check() returns 0 if all checks succeed, 1 otherwise.
572 */
573static int opa_smp_check(struct hfi1_ibport *ibp, u16 pkey, u8 sc5,
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800574 struct rvt_qp *qp, u16 slid, struct opa_smp *smp)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400575{
576 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
577
578 /*
579 * I don't think it's possible for us to get here with sc != 0xf,
580 * but check it to be certain.
581 */
582 if (sc5 != 0xf)
583 return 1;
584
585 if (rcv_pkey_check(ppd, pkey, sc5, slid))
586 return 1;
587
588 /*
589 * At this point we know (and so don't need to check again) that
590 * the pkey is either LIM_MGMT_P_KEY, or FULL_MGMT_P_KEY
591 * (see ingress_pkey_check).
592 */
593 if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE &&
594 smp->mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED) {
595 ingress_pkey_table_fail(ppd, pkey, slid);
596 return 1;
597 }
598
599 /*
600 * SMPs fall into one of four (disjoint) categories:
Jianxin Xiongf3809202016-09-30 20:11:09 -0700601 * SMA request, SMA response, SMA trap, or SMA trap repress.
602 * Our response depends, in part, on which type of SMP we're
603 * processing.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400604 *
Jianxin Xiongf3809202016-09-30 20:11:09 -0700605 * If this is an SMA response, skip the check here.
606 *
607 * If this is an SMA request or SMA trap repress:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400608 * - pkey != FULL_MGMT_P_KEY =>
609 * increment port recv constraint errors, drop MAD
Jianxin Xiongf3809202016-09-30 20:11:09 -0700610 *
611 * Otherwise:
612 * - accept if the port is running an SM
613 * - drop MAD if it's an SMA trap
614 * - pkey == FULL_MGMT_P_KEY =>
615 * reply with unsupported method
616 * - pkey != FULL_MGMT_P_KEY =>
617 * increment port recv constraint errors, drop MAD
Mike Marciniszyn77241052015-07-30 15:17:43 -0400618 */
619 switch (smp->method) {
Jianxin Xiongf3809202016-09-30 20:11:09 -0700620 case IB_MGMT_METHOD_GET_RESP:
621 case IB_MGMT_METHOD_REPORT_RESP:
622 break;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400623 case IB_MGMT_METHOD_GET:
624 case IB_MGMT_METHOD_SET:
625 case IB_MGMT_METHOD_REPORT:
626 case IB_MGMT_METHOD_TRAP_REPRESS:
627 if (pkey != FULL_MGMT_P_KEY) {
628 ingress_pkey_table_fail(ppd, pkey, slid);
629 return 1;
630 }
631 break;
Jianxin Xiongf3809202016-09-30 20:11:09 -0700632 default:
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800633 if (ibp->rvp.port_cap_flags & IB_PORT_SM)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400634 return 0;
Jianxin Xiongf3809202016-09-30 20:11:09 -0700635 if (smp->method == IB_MGMT_METHOD_TRAP)
636 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400637 if (pkey == FULL_MGMT_P_KEY) {
638 smp->status |= IB_SMP_UNSUP_METHOD;
639 return 0;
640 }
Jianxin Xiongf3809202016-09-30 20:11:09 -0700641 ingress_pkey_table_fail(ppd, pkey, slid);
642 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400643 }
644 return 0;
645}
646
Mike Marciniszyn77241052015-07-30 15:17:43 -0400647/**
648 * hfi1_ud_rcv - receive an incoming UD packet
649 * @ibp: the port the packet came in on
650 * @hdr: the packet header
651 * @rcv_flags: flags relevant to rcv processing
652 * @data: the packet data
653 * @tlen: the packet length
654 * @qp: the QP the packet came on
655 *
656 * This is called from qp_rcv() to process an incoming UD packet
657 * for the given QP.
658 * Called at interrupt level.
659 */
660void hfi1_ud_rcv(struct hfi1_packet *packet)
661{
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700662 struct ib_other_headers *ohdr = packet->ohdr;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400663 u32 hdrsize = packet->hlen;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400664 struct ib_wc wc;
665 u32 qkey;
666 u32 src_qp;
Don Hiatt90397462017-05-12 09:20:20 -0700667 u16 pkey;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400668 int mgmt_pkey_idx = -1;
Sebastian Sanchezf3e862c2017-02-08 05:26:25 -0800669 struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
Dasaratharaman Chandramouli89c057c2016-07-25 13:40:28 -0700670 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700671 struct ib_header *hdr = packet->hdr;
Don Hiatt72c07e22017-08-04 13:53:58 -0700672 void *data = packet->payload;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400673 u32 tlen = packet->tlen;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800674 struct rvt_qp *qp = packet->qp;
Dasaratharaman Chandramouliaad559c2017-04-09 10:16:15 -0700675 u8 sc5 = hfi1_9B_get_sc5(hdr, packet->rhf);
Don Hiatt90397462017-05-12 09:20:20 -0700676 u8 sl_from_sc;
677 u8 extra_bytes = packet->pad;
678 u8 opcode = packet->opcode;
679 u8 sl = packet->sl;
680 u32 dlid = packet->dlid;
681 u32 slid = packet->slid;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400682
Don Hiatt7dafbab2017-05-12 09:19:55 -0700683 qkey = ib_get_qkey(ohdr);
684 src_qp = ib_get_sqpn(ohdr);
Don Hiattcb4270572017-04-09 10:16:22 -0700685 pkey = ib_bth_get_pkey(ohdr);
Dasaratharaman Chandramouli89c057c2016-07-25 13:40:28 -0700686 extra_bytes += (SIZE_OF_CRC << 2);
687 sl_from_sc = ibp->sc_to_sl[sc5];
Mike Marciniszyn77241052015-07-30 15:17:43 -0400688
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700689 process_ecn(qp, packet, (opcode != IB_OPCODE_CNP));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400690 /*
691 * Get the number of bytes the message was padded by
692 * and drop incomplete packets.
693 */
Dasaratharaman Chandramouli89c057c2016-07-25 13:40:28 -0700694 if (unlikely(tlen < (hdrsize + extra_bytes)))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400695 goto drop;
696
Dasaratharaman Chandramouli89c057c2016-07-25 13:40:28 -0700697 tlen -= hdrsize + extra_bytes;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400698
699 /*
700 * Check that the permissive LID is only used on QP0
701 * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
702 */
703 if (qp->ibqp.qp_num) {
704 if (unlikely(hdr->lrh[1] == IB_LID_PERMISSIVE ||
705 hdr->lrh[3] == IB_LID_PERMISSIVE))
706 goto drop;
707 if (qp->ibqp.qp_num > 1) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400708 if (unlikely(rcv_pkey_check(ppd, pkey, sc5, slid))) {
709 /*
710 * Traps will not be sent for packets dropped
711 * by the HW. This is fine, as sending trap
712 * for invalid pkeys is optional according to
713 * IB spec (release 1.3, section 10.9.4)
714 */
Dennis Dalessandro13d84912017-05-29 17:22:01 -0700715 hfi1_bad_pkey(ibp,
716 pkey, sl,
717 src_qp, qp->ibqp.qp_num,
718 slid, dlid);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400719 return;
720 }
721 } else {
722 /* GSI packet */
723 mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
724 if (mgmt_pkey_idx < 0)
725 goto drop;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400726 }
Dennis Dalessandro13d84912017-05-29 17:22:01 -0700727 if (unlikely(qkey != qp->qkey)) /* Silent drop */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400728 return;
Dennis Dalessandro13d84912017-05-29 17:22:01 -0700729
Mike Marciniszyn77241052015-07-30 15:17:43 -0400730 /* Drop invalid MAD packets (see 13.5.3.1). */
731 if (unlikely(qp->ibqp.qp_num == 1 &&
Dasaratharaman Chandramouli89c057c2016-07-25 13:40:28 -0700732 (tlen > 2048 || (sc5 == 0xF))))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400733 goto drop;
734 } else {
735 /* Received on QP0, and so by definition, this is an SMP */
736 struct opa_smp *smp = (struct opa_smp *)data;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400737
738 if (opa_smp_check(ibp, pkey, sc5, qp, slid, smp))
739 goto drop;
740
741 if (tlen > 2048)
742 goto drop;
743 if ((hdr->lrh[1] == IB_LID_PERMISSIVE ||
744 hdr->lrh[3] == IB_LID_PERMISSIVE) &&
745 smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
746 goto drop;
747
748 /* look up SMI pkey */
749 mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
750 if (mgmt_pkey_idx < 0)
751 goto drop;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400752 }
753
754 if (qp->ibqp.qp_num > 1 &&
755 opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
756 wc.ex.imm_data = ohdr->u.ud.imm_data;
757 wc.wc_flags = IB_WC_WITH_IMM;
758 tlen -= sizeof(u32);
759 } else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
760 wc.ex.imm_data = 0;
761 wc.wc_flags = 0;
Jubin Johne4909742016-02-14 20:22:00 -0800762 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400763 goto drop;
Jubin Johne4909742016-02-14 20:22:00 -0800764 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400765
766 /*
767 * A GRH is expected to precede the data even if not
768 * present on the wire.
769 */
770 wc.byte_len = tlen + sizeof(struct ib_grh);
771
772 /*
773 * Get the next work request entry to find where to put the data.
774 */
Jubin Johne4909742016-02-14 20:22:00 -0800775 if (qp->r_flags & RVT_R_REUSE_SGE) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800776 qp->r_flags &= ~RVT_R_REUSE_SGE;
Jubin Johne4909742016-02-14 20:22:00 -0800777 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400778 int ret;
779
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800780 ret = hfi1_rvt_get_rwqe(qp, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400781 if (ret < 0) {
Brian Weltybeb5a042017-02-08 05:27:01 -0800782 rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400783 return;
784 }
785 if (!ret) {
786 if (qp->ibqp.qp_num == 0)
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800787 ibp->rvp.n_vl15_dropped++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400788 return;
789 }
790 }
791 /* Silently drop packets which are too big. */
792 if (unlikely(wc.byte_len > qp->r_len)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800793 qp->r_flags |= RVT_R_REUSE_SGE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400794 goto drop;
795 }
Don Hiatt90397462017-05-12 09:20:20 -0700796 if (packet->grh) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400797 hfi1_copy_sge(&qp->r_sge, &hdr->u.l.grh,
Brian Welty0128fce2017-02-08 05:27:31 -0800798 sizeof(struct ib_grh), true, false);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400799 wc.wc_flags |= IB_WC_GRH;
Jubin Johne4909742016-02-14 20:22:00 -0800800 } else {
Brian Welty1198fce2017-02-08 05:27:37 -0800801 rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
Jubin Johne4909742016-02-14 20:22:00 -0800802 }
Dean Luick7b0b01a2016-02-03 14:35:49 -0800803 hfi1_copy_sge(&qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh),
Brian Welty0128fce2017-02-08 05:27:31 -0800804 true, false);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800805 rvt_put_ss(&qp->r_sge);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800806 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400807 return;
808 wc.wr_id = qp->r_wr_id;
809 wc.status = IB_WC_SUCCESS;
810 wc.opcode = IB_WC_RECV;
811 wc.vendor_err = 0;
812 wc.qp = &qp->ibqp;
813 wc.src_qp = src_qp;
814
815 if (qp->ibqp.qp_type == IB_QPT_GSI ||
816 qp->ibqp.qp_type == IB_QPT_SMI) {
817 if (mgmt_pkey_idx < 0) {
818 if (net_ratelimit()) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400819 struct hfi1_devdata *dd = ppd->dd;
820
821 dd_dev_err(dd, "QP type %d mgmt_pkey_idx < 0 and packet not dropped???\n",
822 qp->ibqp.qp_type);
823 mgmt_pkey_idx = 0;
824 }
825 }
826 wc.pkey_index = (unsigned)mgmt_pkey_idx;
Jubin Johne4909742016-02-14 20:22:00 -0800827 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400828 wc.pkey_index = 0;
Jubin Johne4909742016-02-14 20:22:00 -0800829 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400830
Dasaratharaman Chandramouli89c057c2016-07-25 13:40:28 -0700831 wc.slid = slid;
832 wc.sl = sl_from_sc;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400833
834 /*
835 * Save the LMC lower bits if the destination LID is a unicast LID.
836 */
Dennis Dalessandro8859b4a2016-01-19 14:42:11 -0800837 wc.dlid_path_bits = dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE) ? 0 :
Mike Marciniszyn77241052015-07-30 15:17:43 -0400838 dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1);
839 wc.port_num = qp->port_num;
840 /* Signal completion event if the solicited bit is set. */
Dennis Dalessandroabd712d2016-01-19 14:43:22 -0800841 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
842 (ohdr->bth[0] &
843 cpu_to_be32(IB_BTH_SOLICITED)) != 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400844 return;
845
846drop:
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800847 ibp->rvp.n_pkt_drops++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400848}