blob: 6a4e95cefae5f64c337b69f75f9187c48a6e40f6 [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,
112 qp->s_pkey_index, slid))) {
Erik E. Kahn5cd24112015-12-10 09:59:40 -0500113 hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_P_KEY, pkey,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400114 rdma_ah_get_sl(ah_attr),
Mike Marciniszyn77241052015-07-30 15:17:43 -0400115 sqp->ibqp.qp_num, qp->ibqp.qp_num,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400116 slid, rdma_ah_get_dlid(ah_attr));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400117 goto drop;
118 }
119 }
120
121 /*
122 * Check that the qkey matches (except for QP0, see 9.6.1.4.1).
123 * Qkeys with the high order bit set mean use the
124 * qkey from the QP context instead of the WR (see 10.2.5).
125 */
126 if (qp->ibqp.qp_num) {
127 u32 qkey;
128
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100129 qkey = (int)swqe->ud_wr.remote_qkey < 0 ?
130 sqp->qkey : swqe->ud_wr.remote_qkey;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400131 if (unlikely(qkey != qp->qkey)) {
132 u16 lid;
133
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400134 lid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
Mike Marciniszyn77241052015-07-30 15:17:43 -0400135 ((1 << ppd->lmc) - 1));
Erik E. Kahn5cd24112015-12-10 09:59:40 -0500136 hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_Q_KEY, qkey,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400137 rdma_ah_get_sl(ah_attr),
Mike Marciniszyn77241052015-07-30 15:17:43 -0400138 sqp->ibqp.qp_num, qp->ibqp.qp_num,
Erik E. Kahn5cd24112015-12-10 09:59:40 -0500139 lid,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400140 rdma_ah_get_dlid(ah_attr));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400141 goto drop;
142 }
143 }
144
145 /*
146 * A GRH is expected to precede the data even if not
147 * present on the wire.
148 */
149 length = swqe->length;
150 memset(&wc, 0, sizeof(wc));
151 wc.byte_len = length + sizeof(struct ib_grh);
152
153 if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
154 wc.wc_flags = IB_WC_WITH_IMM;
155 wc.ex.imm_data = swqe->wr.ex.imm_data;
156 }
157
158 spin_lock_irqsave(&qp->r_lock, flags);
159
160 /*
161 * Get the next work request entry to find where to put the data.
162 */
Jubin Johne4909742016-02-14 20:22:00 -0800163 if (qp->r_flags & RVT_R_REUSE_SGE) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800164 qp->r_flags &= ~RVT_R_REUSE_SGE;
Jubin Johne4909742016-02-14 20:22:00 -0800165 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400166 int ret;
167
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800168 ret = hfi1_rvt_get_rwqe(qp, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400169 if (ret < 0) {
Brian Weltybeb5a042017-02-08 05:27:01 -0800170 rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400171 goto bail_unlock;
172 }
173 if (!ret) {
174 if (qp->ibqp.qp_num == 0)
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800175 ibp->rvp.n_vl15_dropped++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400176 goto bail_unlock;
177 }
178 }
179 /* Silently drop packets which are too big. */
180 if (unlikely(wc.byte_len > qp->r_len)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800181 qp->r_flags |= RVT_R_REUSE_SGE;
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800182 ibp->rvp.n_pkt_drops++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400183 goto bail_unlock;
184 }
185
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400186 if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
Dasaratharaman Chandramouli527dbf12016-07-25 13:40:40 -0700187 struct ib_grh grh;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400188 const struct ib_global_route *grd = rdma_ah_read_grh(ah_attr);
Dasaratharaman Chandramouli527dbf12016-07-25 13:40:40 -0700189
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400190 hfi1_make_grh(ibp, &grh, grd, 0, 0);
Dasaratharaman Chandramouli527dbf12016-07-25 13:40:40 -0700191 hfi1_copy_sge(&qp->r_sge, &grh,
Brian Welty0128fce2017-02-08 05:27:31 -0800192 sizeof(grh), true, false);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400193 wc.wc_flags |= IB_WC_GRH;
Jubin Johne4909742016-02-14 20:22:00 -0800194 } else {
Brian Welty1198fce2017-02-08 05:27:37 -0800195 rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
Jubin Johne4909742016-02-14 20:22:00 -0800196 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400197 ssge.sg_list = swqe->sg_list + 1;
198 ssge.sge = *swqe->sg_list;
199 ssge.num_sge = swqe->wr.num_sge;
200 sge = &ssge.sge;
201 while (length) {
202 u32 len = sge->length;
203
204 if (len > length)
205 len = length;
206 if (len > sge->sge_length)
207 len = sge->sge_length;
208 WARN_ON_ONCE(len == 0);
Brian Welty0128fce2017-02-08 05:27:31 -0800209 hfi1_copy_sge(&qp->r_sge, sge->vaddr, len, true, false);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400210 sge->vaddr += len;
211 sge->length -= len;
212 sge->sge_length -= len;
213 if (sge->sge_length == 0) {
214 if (--ssge.num_sge)
215 *sge = *ssge.sg_list++;
216 } else if (sge->length == 0 && sge->mr->lkey) {
Dennis Dalessandrocd4ceee2016-01-19 14:41:55 -0800217 if (++sge->n >= RVT_SEGSZ) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400218 if (++sge->m >= sge->mr->mapsz)
219 break;
220 sge->n = 0;
221 }
222 sge->vaddr =
223 sge->mr->map[sge->m]->segs[sge->n].vaddr;
224 sge->length =
225 sge->mr->map[sge->m]->segs[sge->n].length;
226 }
227 length -= len;
228 }
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800229 rvt_put_ss(&qp->r_sge);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800230 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400231 goto bail_unlock;
232 wc.wr_id = qp->r_wr_id;
233 wc.status = IB_WC_SUCCESS;
234 wc.opcode = IB_WC_RECV;
235 wc.qp = &qp->ibqp;
236 wc.src_qp = sqp->ibqp.qp_num;
237 if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI) {
238 if (sqp->ibqp.qp_type == IB_QPT_GSI ||
239 sqp->ibqp.qp_type == IB_QPT_SMI)
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100240 wc.pkey_index = swqe->ud_wr.pkey_index;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400241 else
242 wc.pkey_index = sqp->s_pkey_index;
243 } else {
244 wc.pkey_index = 0;
245 }
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400246 wc.slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
247 ((1 << ppd->lmc) - 1));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400248 /* Check for loopback when the port lid is not set */
249 if (wc.slid == 0 && sqp->ibqp.qp_type == IB_QPT_GSI)
Dennis Dalessandro8859b4a2016-01-19 14:42:11 -0800250 wc.slid = be16_to_cpu(IB_LID_PERMISSIVE);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400251 wc.sl = rdma_ah_get_sl(ah_attr);
252 wc.dlid_path_bits = rdma_ah_get_dlid(ah_attr) & ((1 << ppd->lmc) - 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400253 wc.port_num = qp->port_num;
254 /* Signal completion event if the solicited bit is set. */
Dennis Dalessandroabd712d2016-01-19 14:43:22 -0800255 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
256 swqe->wr.send_flags & IB_SEND_SOLICITED);
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800257 ibp->rvp.n_loop_pkts++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400258bail_unlock:
259 spin_unlock_irqrestore(&qp->r_lock, flags);
260drop:
261 rcu_read_unlock();
262}
263
264/**
265 * hfi1_make_ud_req - construct a UD request packet
266 * @qp: the QP
267 *
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800268 * Assume s_lock is held.
269 *
Mike Marciniszyn77241052015-07-30 15:17:43 -0400270 * Return 1 if constructed; otherwise, return 0.
271 */
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800272int hfi1_make_ud_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400273{
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800274 struct hfi1_qp_priv *priv = qp->priv;
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700275 struct ib_other_headers *ohdr;
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -0400276 struct rdma_ah_attr *ah_attr;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400277 struct hfi1_pportdata *ppd;
278 struct hfi1_ibport *ibp;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800279 struct rvt_swqe *wqe;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400280 u32 nwords;
281 u32 extra_bytes;
282 u32 bth0;
283 u16 lrh0;
284 u16 lid;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400285 int next_cur;
286 u8 sc5;
287
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800288 ps->s_txreq = get_txreq(ps->dev, qp);
289 if (IS_ERR(ps->s_txreq))
290 goto bail_no_tx;
291
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800292 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
293 if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400294 goto bail;
295 /* We are in the error state, flush the work request. */
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800296 smp_read_barrier_depends(); /* see post_one_send */
297 if (qp->s_last == ACCESS_ONCE(qp->s_head))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400298 goto bail;
299 /* If DMAs are in progress, we can't flush immediately. */
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800300 if (iowait_sdma_pending(&priv->s_iowait)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800301 qp->s_flags |= RVT_S_WAIT_DMA;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400302 goto bail;
303 }
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800304 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400305 hfi1_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800306 goto done_free_tx;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400307 }
308
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800309 /* see post_one_send() */
310 smp_read_barrier_depends();
311 if (qp->s_cur == ACCESS_ONCE(qp->s_head))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400312 goto bail;
313
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800314 wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400315 next_cur = qp->s_cur + 1;
316 if (next_cur >= qp->s_size)
317 next_cur = 0;
318
319 /* Construct the header. */
320 ibp = to_iport(qp->ibqp.device, qp->port_num);
321 ppd = ppd_from_ibp(ibp);
Dennis Dalessandro15723f02016-01-19 14:42:17 -0800322 ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400323 if (rdma_ah_get_dlid(ah_attr) < be16_to_cpu(IB_MULTICAST_LID_BASE) ||
324 rdma_ah_get_dlid(ah_attr) == be16_to_cpu(IB_LID_PERMISSIVE)) {
325 lid = rdma_ah_get_dlid(ah_attr) & ~((1 << ppd->lmc) - 1);
Jubin John17fb4f22016-02-14 20:21:52 -0800326 if (unlikely(!loopback &&
327 (lid == ppd->lid ||
328 (lid == be16_to_cpu(IB_LID_PERMISSIVE) &&
329 qp->ibqp.qp_type == IB_QPT_GSI)))) {
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700330 unsigned long tflags = ps->flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400331 /*
332 * If DMAs are in progress, we can't generate
333 * a completion for the loopback packet since
334 * it would be out of order.
335 * Instead of waiting, we could queue a
336 * zero length descriptor so we get a callback.
337 */
Mike Marciniszyn14553ca2016-02-14 12:45:36 -0800338 if (iowait_sdma_pending(&priv->s_iowait)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800339 qp->s_flags |= RVT_S_WAIT_DMA;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400340 goto bail;
341 }
342 qp->s_cur = next_cur;
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700343 spin_unlock_irqrestore(&qp->s_lock, tflags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400344 ud_loopback(qp, wqe);
Mike Marciniszyn747f4d72016-04-12 10:46:10 -0700345 spin_lock_irqsave(&qp->s_lock, tflags);
346 ps->flags = tflags;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400347 hfi1_send_complete(qp, wqe, IB_WC_SUCCESS);
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800348 goto done_free_tx;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400349 }
350 }
351
352 qp->s_cur = next_cur;
353 extra_bytes = -wqe->length & 3;
354 nwords = (wqe->length + extra_bytes) >> 2;
355
356 /* header size in 32-bit words LRH+BTH+DETH = (8+12+8)/4. */
357 qp->s_hdrwords = 7;
Don Hiatte922ae02016-12-07 19:33:00 -0800358 ps->s_txreq->s_cur_size = wqe->length;
Mitko Haralanovb777f152016-12-07 19:33:27 -0800359 ps->s_txreq->ss = &qp->s_sge;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400360 qp->s_srate = rdma_ah_get_static_rate(ah_attr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400361 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
362 qp->s_wqe = wqe;
363 qp->s_sge.sge = wqe->sg_list[0];
364 qp->s_sge.sg_list = wqe->sg_list + 1;
365 qp->s_sge.num_sge = wqe->wr.num_sge;
366 qp->s_sge.total_len = wqe->length;
367
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400368 if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400369 /* Header size in 32-bit words. */
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800370 qp->s_hdrwords += hfi1_make_grh(ibp,
371 &ps->s_txreq->phdr.hdr.u.l.grh,
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400372 rdma_ah_read_grh(ah_attr),
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800373 qp->s_hdrwords, nwords);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400374 lrh0 = HFI1_LRH_GRH;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800375 ohdr = &ps->s_txreq->phdr.hdr.u.l.oth;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400376 /*
377 * Don't worry about sending to locally attached multicast
378 * QPs. It is unspecified by the spec. what happens.
379 */
380 } else {
381 /* Header size in 32-bit words. */
382 lrh0 = HFI1_LRH_BTH;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800383 ohdr = &ps->s_txreq->phdr.hdr.u.oth;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400384 }
385 if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
386 qp->s_hdrwords++;
387 ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;
388 bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
Jubin Johne4909742016-02-14 20:22:00 -0800389 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400390 bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
Jubin Johne4909742016-02-14 20:22:00 -0800391 }
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400392 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
393 lrh0 |= (rdma_ah_get_sl(ah_attr) & 0xf) << 4;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400394 if (qp->ibqp.qp_type == IB_QPT_SMI) {
395 lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800396 priv->s_sc = 0xf;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400397 } else {
398 lrh0 |= (sc5 & 0xf) << 12;
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800399 priv->s_sc = sc5;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400400 }
Dennis Dalessandro4c6829c2016-01-19 14:42:00 -0800401 priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
Mike Marciniszyn711e1042016-02-14 12:45:18 -0800402 ps->s_txreq->sde = priv->s_sde;
Jubin John721d0422016-02-14 12:45:00 -0800403 priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
Mike Marciniszyn711e1042016-02-14 12:45:18 -0800404 ps->s_txreq->psc = priv->s_sendcontext;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800405 ps->s_txreq->phdr.hdr.lrh[0] = cpu_to_be16(lrh0);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400406 ps->s_txreq->phdr.hdr.lrh[1] =
407 cpu_to_be16(rdma_ah_get_dlid(ah_attr));
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800408 ps->s_txreq->phdr.hdr.lrh[2] =
Mike Marciniszyn77241052015-07-30 15:17:43 -0400409 cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400410 if (rdma_ah_get_dlid(ah_attr) == be16_to_cpu(IB_LID_PERMISSIVE)) {
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800411 ps->s_txreq->phdr.hdr.lrh[3] = IB_LID_PERMISSIVE;
412 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400413 lid = ppd->lid;
414 if (lid) {
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400415 lid |= rdma_ah_get_path_bits(ah_attr) &
416 ((1 << ppd->lmc) - 1);
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800417 ps->s_txreq->phdr.hdr.lrh[3] = cpu_to_be16(lid);
418 } else {
419 ps->s_txreq->phdr.hdr.lrh[3] = IB_LID_PERMISSIVE;
420 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400421 }
422 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
423 bth0 |= IB_BTH_SOLICITED;
424 bth0 |= extra_bytes << 20;
425 if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI)
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100426 bth0 |= hfi1_get_pkey(ibp, wqe->ud_wr.pkey_index);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400427 else
428 bth0 |= hfi1_get_pkey(ibp, qp->s_pkey_index);
429 ohdr->bth[0] = cpu_to_be32(bth0);
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100430 ohdr->bth[1] = cpu_to_be32(wqe->ud_wr.remote_qpn);
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800431 ohdr->bth[2] = cpu_to_be32(mask_psn(wqe->psn));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400432 /*
433 * Qkeys with the high order bit set mean use the
434 * qkey from the QP context instead of the WR (see 10.2.5).
435 */
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100436 ohdr->u.ud.deth[0] = cpu_to_be32((int)wqe->ud_wr.remote_qkey < 0 ?
437 qp->qkey : wqe->ud_wr.remote_qkey);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400438 ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
439 /* disarm any ahg */
Dasaratharaman Chandramoulia9b6b3b2016-07-25 13:40:16 -0700440 priv->s_ahg->ahgcount = 0;
441 priv->s_ahg->ahgidx = 0;
442 priv->s_ahg->tx_flags = 0;
Mike Marciniszyn711e1042016-02-14 12:45:18 -0800443 /* pbc */
444 ps->s_txreq->hdr_dwords = qp->s_hdrwords + 2;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400445
Mike Marciniszyn46a80d62016-02-14 12:10:04 -0800446 return 1;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800447
448done_free_tx:
449 hfi1_put_txreq(ps->s_txreq);
450 ps->s_txreq = NULL;
451 return 1;
452
Mike Marciniszyn77241052015-07-30 15:17:43 -0400453bail:
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800454 hfi1_put_txreq(ps->s_txreq);
455
456bail_no_tx:
457 ps->s_txreq = NULL;
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800458 qp->s_flags &= ~RVT_S_BUSY;
Dennis Dalessandrobb5df5f2016-02-14 12:44:43 -0800459 qp->s_hdrwords = 0;
460 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400461}
462
463/*
464 * Hardware can't check this so we do it here.
465 *
466 * This is a slightly different algorithm than the standard pkey check. It
467 * special cases the management keys and allows for 0x7fff and 0xffff to be in
468 * the table at the same time.
469 *
470 * @returns the index found or -1 if not found
471 */
472int hfi1_lookup_pkey_idx(struct hfi1_ibport *ibp, u16 pkey)
473{
474 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
475 unsigned i;
476
477 if (pkey == FULL_MGMT_P_KEY || pkey == LIM_MGMT_P_KEY) {
478 unsigned lim_idx = -1;
479
480 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i) {
481 /* here we look for an exact match */
482 if (ppd->pkeys[i] == pkey)
483 return i;
484 if (ppd->pkeys[i] == LIM_MGMT_P_KEY)
485 lim_idx = i;
486 }
487
488 /* did not find 0xffff return 0x7fff idx if found */
489 if (pkey == FULL_MGMT_P_KEY)
490 return lim_idx;
491
492 /* no match... */
493 return -1;
494 }
495
496 pkey &= 0x7fff; /* remove limited/full membership bit */
497
498 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i)
499 if ((ppd->pkeys[i] & 0x7fff) == pkey)
500 return i;
501
502 /*
503 * Should not get here, this means hardware failed to validate pkeys.
504 */
505 return -1;
506}
507
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800508void return_cnp(struct hfi1_ibport *ibp, struct rvt_qp *qp, u32 remote_qpn,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400509 u32 pkey, u32 slid, u32 dlid, u8 sc5,
510 const struct ib_grh *old_grh)
511{
512 u64 pbc, pbc_flags = 0;
513 u32 bth0, plen, vl, hwords = 5;
514 u16 lrh0;
515 u8 sl = ibp->sc_to_sl[sc5];
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700516 struct ib_header hdr;
517 struct ib_other_headers *ohdr;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400518 struct pio_buf *pbuf;
519 struct send_context *ctxt = qp_to_send_context(qp, sc5);
520 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
521
522 if (old_grh) {
523 struct ib_grh *grh = &hdr.u.l.grh;
524
525 grh->version_tclass_flow = old_grh->version_tclass_flow;
526 grh->paylen = cpu_to_be16((hwords - 2 + SIZE_OF_CRC) << 2);
527 grh->hop_limit = 0xff;
528 grh->sgid = old_grh->dgid;
529 grh->dgid = old_grh->sgid;
530 ohdr = &hdr.u.l.oth;
531 lrh0 = HFI1_LRH_GRH;
532 hwords += sizeof(struct ib_grh) / sizeof(u32);
533 } else {
534 ohdr = &hdr.u.oth;
535 lrh0 = HFI1_LRH_BTH;
536 }
537
538 lrh0 |= (sc5 & 0xf) << 12 | sl << 4;
539
540 bth0 = pkey | (IB_OPCODE_CNP << 24);
541 ohdr->bth[0] = cpu_to_be32(bth0);
542
Don Hiatt3d591092017-04-09 10:16:28 -0700543 ohdr->bth[1] = cpu_to_be32(remote_qpn | (1 << IB_BECN_SHIFT));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400544 ohdr->bth[2] = 0; /* PSN 0 */
545
546 hdr.lrh[0] = cpu_to_be16(lrh0);
547 hdr.lrh[1] = cpu_to_be16(dlid);
548 hdr.lrh[2] = cpu_to_be16(hwords + SIZE_OF_CRC);
549 hdr.lrh[3] = cpu_to_be16(slid);
550
551 plen = 2 /* PBC */ + hwords;
552 pbc_flags |= (!!(sc5 & 0x10)) << PBC_DC_INFO_SHIFT;
553 vl = sc_to_vlt(ppd->dd, sc5);
554 pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
555 if (ctxt) {
556 pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
557 if (pbuf)
558 ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
559 &hdr, hwords);
560 }
561}
562
563/*
564 * opa_smp_check() - Do the regular pkey checking, and the additional
Jianxin Xiongf3809202016-09-30 20:11:09 -0700565 * checks for SMPs specified in OPAv1 rev 1.0, 9/19/2016 update, section
566 * 9.10.25 ("SMA Packet Checks").
Mike Marciniszyn77241052015-07-30 15:17:43 -0400567 *
568 * Note that:
569 * - Checks are done using the pkey directly from the packet's BTH,
570 * and specifically _not_ the pkey that we attach to the completion,
571 * which may be different.
572 * - These checks are specifically for "non-local" SMPs (i.e., SMPs
573 * which originated on another node). SMPs which are sent from, and
574 * destined to this node are checked in opa_local_smp_check().
575 *
576 * At the point where opa_smp_check() is called, we know:
577 * - destination QP is QP0
578 *
579 * opa_smp_check() returns 0 if all checks succeed, 1 otherwise.
580 */
581static int opa_smp_check(struct hfi1_ibport *ibp, u16 pkey, u8 sc5,
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800582 struct rvt_qp *qp, u16 slid, struct opa_smp *smp)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400583{
584 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
585
586 /*
587 * I don't think it's possible for us to get here with sc != 0xf,
588 * but check it to be certain.
589 */
590 if (sc5 != 0xf)
591 return 1;
592
593 if (rcv_pkey_check(ppd, pkey, sc5, slid))
594 return 1;
595
596 /*
597 * At this point we know (and so don't need to check again) that
598 * the pkey is either LIM_MGMT_P_KEY, or FULL_MGMT_P_KEY
599 * (see ingress_pkey_check).
600 */
601 if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE &&
602 smp->mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED) {
603 ingress_pkey_table_fail(ppd, pkey, slid);
604 return 1;
605 }
606
607 /*
608 * SMPs fall into one of four (disjoint) categories:
Jianxin Xiongf3809202016-09-30 20:11:09 -0700609 * SMA request, SMA response, SMA trap, or SMA trap repress.
610 * Our response depends, in part, on which type of SMP we're
611 * processing.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400612 *
Jianxin Xiongf3809202016-09-30 20:11:09 -0700613 * If this is an SMA response, skip the check here.
614 *
615 * If this is an SMA request or SMA trap repress:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400616 * - pkey != FULL_MGMT_P_KEY =>
617 * increment port recv constraint errors, drop MAD
Jianxin Xiongf3809202016-09-30 20:11:09 -0700618 *
619 * Otherwise:
620 * - accept if the port is running an SM
621 * - drop MAD if it's an SMA trap
622 * - pkey == FULL_MGMT_P_KEY =>
623 * reply with unsupported method
624 * - pkey != FULL_MGMT_P_KEY =>
625 * increment port recv constraint errors, drop MAD
Mike Marciniszyn77241052015-07-30 15:17:43 -0400626 */
627 switch (smp->method) {
Jianxin Xiongf3809202016-09-30 20:11:09 -0700628 case IB_MGMT_METHOD_GET_RESP:
629 case IB_MGMT_METHOD_REPORT_RESP:
630 break;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400631 case IB_MGMT_METHOD_GET:
632 case IB_MGMT_METHOD_SET:
633 case IB_MGMT_METHOD_REPORT:
634 case IB_MGMT_METHOD_TRAP_REPRESS:
635 if (pkey != FULL_MGMT_P_KEY) {
636 ingress_pkey_table_fail(ppd, pkey, slid);
637 return 1;
638 }
639 break;
Jianxin Xiongf3809202016-09-30 20:11:09 -0700640 default:
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800641 if (ibp->rvp.port_cap_flags & IB_PORT_SM)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400642 return 0;
Jianxin Xiongf3809202016-09-30 20:11:09 -0700643 if (smp->method == IB_MGMT_METHOD_TRAP)
644 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400645 if (pkey == FULL_MGMT_P_KEY) {
646 smp->status |= IB_SMP_UNSUP_METHOD;
647 return 0;
648 }
Jianxin Xiongf3809202016-09-30 20:11:09 -0700649 ingress_pkey_table_fail(ppd, pkey, slid);
650 return 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400651 }
652 return 0;
653}
654
Mike Marciniszyn77241052015-07-30 15:17:43 -0400655/**
656 * hfi1_ud_rcv - receive an incoming UD packet
657 * @ibp: the port the packet came in on
658 * @hdr: the packet header
659 * @rcv_flags: flags relevant to rcv processing
660 * @data: the packet data
661 * @tlen: the packet length
662 * @qp: the QP the packet came on
663 *
664 * This is called from qp_rcv() to process an incoming UD packet
665 * for the given QP.
666 * Called at interrupt level.
667 */
668void hfi1_ud_rcv(struct hfi1_packet *packet)
669{
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700670 struct ib_other_headers *ohdr = packet->ohdr;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400671 int opcode;
672 u32 hdrsize = packet->hlen;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400673 struct ib_wc wc;
674 u32 qkey;
675 u32 src_qp;
676 u16 dlid, pkey;
677 int mgmt_pkey_idx = -1;
Sebastian Sanchezf3e862c2017-02-08 05:26:25 -0800678 struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
Dasaratharaman Chandramouli89c057c2016-07-25 13:40:28 -0700679 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700680 struct ib_header *hdr = packet->hdr;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400681 u32 rcv_flags = packet->rcv_flags;
682 void *data = packet->ebuf;
683 u32 tlen = packet->tlen;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800684 struct rvt_qp *qp = packet->qp;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400685 bool has_grh = rcv_flags & HFI1_HAS_GRH;
Dasaratharaman Chandramouliaad559c2017-04-09 10:16:15 -0700686 u8 sc5 = hfi1_9B_get_sc5(hdr, packet->rhf);
Dasaratharaman Chandramouli89c057c2016-07-25 13:40:28 -0700687 u32 bth1;
688 u8 sl_from_sc, sl;
689 u16 slid;
690 u8 extra_bytes;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400691
692 qkey = be32_to_cpu(ohdr->u.ud.deth[0]);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800693 src_qp = be32_to_cpu(ohdr->u.ud.deth[1]) & RVT_QPN_MASK;
Don Hiattcb4270572017-04-09 10:16:22 -0700694 dlid = ib_get_dlid(hdr);
Dasaratharaman Chandramouli89c057c2016-07-25 13:40:28 -0700695 bth1 = be32_to_cpu(ohdr->bth[1]);
Don Hiattcb4270572017-04-09 10:16:22 -0700696 slid = ib_get_slid(hdr);
697 pkey = ib_bth_get_pkey(ohdr);
698 opcode = ib_bth_get_opcode(ohdr);
699 sl = ib_get_sl(hdr);
700 extra_bytes = ib_bth_get_pad(ohdr);
Dasaratharaman Chandramouli89c057c2016-07-25 13:40:28 -0700701 extra_bytes += (SIZE_OF_CRC << 2);
702 sl_from_sc = ibp->sc_to_sl[sc5];
Mike Marciniszyn77241052015-07-30 15:17:43 -0400703
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700704 process_ecn(qp, packet, (opcode != IB_OPCODE_CNP));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400705 /*
706 * Get the number of bytes the message was padded by
707 * and drop incomplete packets.
708 */
Dasaratharaman Chandramouli89c057c2016-07-25 13:40:28 -0700709 if (unlikely(tlen < (hdrsize + extra_bytes)))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400710 goto drop;
711
Dasaratharaman Chandramouli89c057c2016-07-25 13:40:28 -0700712 tlen -= hdrsize + extra_bytes;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400713
714 /*
715 * Check that the permissive LID is only used on QP0
716 * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
717 */
718 if (qp->ibqp.qp_num) {
719 if (unlikely(hdr->lrh[1] == IB_LID_PERMISSIVE ||
720 hdr->lrh[3] == IB_LID_PERMISSIVE))
721 goto drop;
722 if (qp->ibqp.qp_num > 1) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400723 if (unlikely(rcv_pkey_check(ppd, pkey, sc5, slid))) {
724 /*
725 * Traps will not be sent for packets dropped
726 * by the HW. This is fine, as sending trap
727 * for invalid pkeys is optional according to
728 * IB spec (release 1.3, section 10.9.4)
729 */
Erik E. Kahn5cd24112015-12-10 09:59:40 -0500730 hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_P_KEY,
Dasaratharaman Chandramouli89c057c2016-07-25 13:40:28 -0700731 pkey, sl,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400732 src_qp, qp->ibqp.qp_num,
Dasaratharaman Chandramouli89c057c2016-07-25 13:40:28 -0700733 slid, dlid);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400734 return;
735 }
736 } else {
737 /* GSI packet */
738 mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
739 if (mgmt_pkey_idx < 0)
740 goto drop;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400741 }
742 if (unlikely(qkey != qp->qkey)) {
Dasaratharaman Chandramouli89c057c2016-07-25 13:40:28 -0700743 hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_Q_KEY, qkey, sl,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400744 src_qp, qp->ibqp.qp_num,
Dasaratharaman Chandramouli89c057c2016-07-25 13:40:28 -0700745 slid, dlid);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400746 return;
747 }
748 /* Drop invalid MAD packets (see 13.5.3.1). */
749 if (unlikely(qp->ibqp.qp_num == 1 &&
Dasaratharaman Chandramouli89c057c2016-07-25 13:40:28 -0700750 (tlen > 2048 || (sc5 == 0xF))))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400751 goto drop;
752 } else {
753 /* Received on QP0, and so by definition, this is an SMP */
754 struct opa_smp *smp = (struct opa_smp *)data;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400755
756 if (opa_smp_check(ibp, pkey, sc5, qp, slid, smp))
757 goto drop;
758
759 if (tlen > 2048)
760 goto drop;
761 if ((hdr->lrh[1] == IB_LID_PERMISSIVE ||
762 hdr->lrh[3] == IB_LID_PERMISSIVE) &&
763 smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
764 goto drop;
765
766 /* look up SMI pkey */
767 mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
768 if (mgmt_pkey_idx < 0)
769 goto drop;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400770 }
771
772 if (qp->ibqp.qp_num > 1 &&
773 opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
774 wc.ex.imm_data = ohdr->u.ud.imm_data;
775 wc.wc_flags = IB_WC_WITH_IMM;
776 tlen -= sizeof(u32);
777 } else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
778 wc.ex.imm_data = 0;
779 wc.wc_flags = 0;
Jubin Johne4909742016-02-14 20:22:00 -0800780 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400781 goto drop;
Jubin Johne4909742016-02-14 20:22:00 -0800782 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400783
784 /*
785 * A GRH is expected to precede the data even if not
786 * present on the wire.
787 */
788 wc.byte_len = tlen + sizeof(struct ib_grh);
789
790 /*
791 * Get the next work request entry to find where to put the data.
792 */
Jubin Johne4909742016-02-14 20:22:00 -0800793 if (qp->r_flags & RVT_R_REUSE_SGE) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800794 qp->r_flags &= ~RVT_R_REUSE_SGE;
Jubin Johne4909742016-02-14 20:22:00 -0800795 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400796 int ret;
797
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800798 ret = hfi1_rvt_get_rwqe(qp, 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400799 if (ret < 0) {
Brian Weltybeb5a042017-02-08 05:27:01 -0800800 rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400801 return;
802 }
803 if (!ret) {
804 if (qp->ibqp.qp_num == 0)
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800805 ibp->rvp.n_vl15_dropped++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400806 return;
807 }
808 }
809 /* Silently drop packets which are too big. */
810 if (unlikely(wc.byte_len > qp->r_len)) {
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800811 qp->r_flags |= RVT_R_REUSE_SGE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400812 goto drop;
813 }
814 if (has_grh) {
815 hfi1_copy_sge(&qp->r_sge, &hdr->u.l.grh,
Brian Welty0128fce2017-02-08 05:27:31 -0800816 sizeof(struct ib_grh), true, false);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400817 wc.wc_flags |= IB_WC_GRH;
Jubin Johne4909742016-02-14 20:22:00 -0800818 } else {
Brian Welty1198fce2017-02-08 05:27:37 -0800819 rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
Jubin Johne4909742016-02-14 20:22:00 -0800820 }
Dean Luick7b0b01a2016-02-03 14:35:49 -0800821 hfi1_copy_sge(&qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh),
Brian Welty0128fce2017-02-08 05:27:31 -0800822 true, false);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800823 rvt_put_ss(&qp->r_sge);
Dennis Dalessandro54d10c12016-01-19 14:43:01 -0800824 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400825 return;
826 wc.wr_id = qp->r_wr_id;
827 wc.status = IB_WC_SUCCESS;
828 wc.opcode = IB_WC_RECV;
829 wc.vendor_err = 0;
830 wc.qp = &qp->ibqp;
831 wc.src_qp = src_qp;
832
833 if (qp->ibqp.qp_type == IB_QPT_GSI ||
834 qp->ibqp.qp_type == IB_QPT_SMI) {
835 if (mgmt_pkey_idx < 0) {
836 if (net_ratelimit()) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400837 struct hfi1_devdata *dd = ppd->dd;
838
839 dd_dev_err(dd, "QP type %d mgmt_pkey_idx < 0 and packet not dropped???\n",
840 qp->ibqp.qp_type);
841 mgmt_pkey_idx = 0;
842 }
843 }
844 wc.pkey_index = (unsigned)mgmt_pkey_idx;
Jubin Johne4909742016-02-14 20:22:00 -0800845 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400846 wc.pkey_index = 0;
Jubin Johne4909742016-02-14 20:22:00 -0800847 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400848
Dasaratharaman Chandramouli89c057c2016-07-25 13:40:28 -0700849 wc.slid = slid;
850 wc.sl = sl_from_sc;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400851
852 /*
853 * Save the LMC lower bits if the destination LID is a unicast LID.
854 */
Dennis Dalessandro8859b4a2016-01-19 14:42:11 -0800855 wc.dlid_path_bits = dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE) ? 0 :
Mike Marciniszyn77241052015-07-30 15:17:43 -0400856 dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1);
857 wc.port_num = qp->port_num;
858 /* Signal completion event if the solicited bit is set. */
Dennis Dalessandroabd712d2016-01-19 14:43:22 -0800859 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
860 (ohdr->bth[0] &
861 cpu_to_be32(IB_BTH_SOLICITED)) != 0);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400862 return;
863
864drop:
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800865 ibp->rvp.n_pkt_drops++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400866}