IB/qib, staging/rdma/hfi1: add s_hlock for use in post send
[linux-block.git] / drivers / staging / rdma / hfi1 / ud.c
CommitLineData
77241056
MM
1/*
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2015 Intel Corporation.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * BSD LICENSE
20 *
21 * Copyright(c) 2015 Intel Corporation.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 *
27 * - Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * - Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in
31 * the documentation and/or other materials provided with the
32 * distribution.
33 * - Neither the name of Intel Corporation nor the names of its
34 * contributors may be used to endorse or promote products derived
35 * from this software without specific prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 *
49 */
50
51#include <linux/net.h>
52#include <rdma/ib_smi.h>
53
54#include "hfi.h"
55#include "mad.h"
56#include "qp.h"
57
58/**
59 * ud_loopback - handle send on loopback QPs
60 * @sqp: the sending QP
61 * @swqe: the send work request
62 *
63 * This is called from hfi1_make_ud_req() to forward a WQE addressed
64 * to the same HFI.
65 * Note that the receive interrupt handler may be calling hfi1_ud_rcv()
66 * while this is being called.
67 */
895420dd 68static void ud_loopback(struct rvt_qp *sqp, struct rvt_swqe *swqe)
77241056
MM
69{
70 struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
71 struct hfi1_pportdata *ppd;
895420dd 72 struct rvt_qp *qp;
77241056
MM
73 struct ib_ah_attr *ah_attr;
74 unsigned long flags;
895420dd
DD
75 struct rvt_sge_state ssge;
76 struct rvt_sge *sge;
77241056
MM
77 struct ib_wc wc;
78 u32 length;
79 enum ib_qp_type sqptype, dqptype;
80
81 rcu_read_lock();
82
ec4274f1
DD
83 qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
84 swqe->ud_wr.remote_qpn);
77241056 85 if (!qp) {
4eb06882 86 ibp->rvp.n_pkt_drops++;
77241056
MM
87 rcu_read_unlock();
88 return;
89 }
90
91 sqptype = sqp->ibqp.qp_type == IB_QPT_GSI ?
92 IB_QPT_UD : sqp->ibqp.qp_type;
93 dqptype = qp->ibqp.qp_type == IB_QPT_GSI ?
94 IB_QPT_UD : qp->ibqp.qp_type;
95
96 if (dqptype != sqptype ||
83693bd1 97 !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
4eb06882 98 ibp->rvp.n_pkt_drops++;
77241056
MM
99 goto drop;
100 }
101
15723f06 102 ah_attr = &ibah_to_rvtah(swqe->ud_wr.ah)->attr;
77241056
MM
103 ppd = ppd_from_ibp(ibp);
104
105 if (qp->ibqp.qp_num > 1) {
106 u16 pkey;
107 u16 slid;
108 u8 sc5 = ibp->sl_to_sc[ah_attr->sl];
109
110 pkey = hfi1_get_pkey(ibp, sqp->s_pkey_index);
111 slid = ppd->lid | (ah_attr->src_path_bits &
112 ((1 << ppd->lmc) - 1));
113 if (unlikely(ingress_pkey_check(ppd, pkey, sc5,
114 qp->s_pkey_index, slid))) {
5cd24119 115 hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_P_KEY, pkey,
77241056
MM
116 ah_attr->sl,
117 sqp->ibqp.qp_num, qp->ibqp.qp_num,
5cd24119 118 slid, ah_attr->dlid);
77241056
MM
119 goto drop;
120 }
121 }
122
123 /*
124 * Check that the qkey matches (except for QP0, see 9.6.1.4.1).
125 * Qkeys with the high order bit set mean use the
126 * qkey from the QP context instead of the WR (see 10.2.5).
127 */
128 if (qp->ibqp.qp_num) {
129 u32 qkey;
130
e622f2f4
CH
131 qkey = (int)swqe->ud_wr.remote_qkey < 0 ?
132 sqp->qkey : swqe->ud_wr.remote_qkey;
77241056
MM
133 if (unlikely(qkey != qp->qkey)) {
134 u16 lid;
135
136 lid = ppd->lid | (ah_attr->src_path_bits &
137 ((1 << ppd->lmc) - 1));
5cd24119 138 hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_Q_KEY, qkey,
77241056
MM
139 ah_attr->sl,
140 sqp->ibqp.qp_num, qp->ibqp.qp_num,
5cd24119
EK
141 lid,
142 ah_attr->dlid);
77241056
MM
143 goto drop;
144 }
145 }
146
147 /*
148 * A GRH is expected to precede the data even if not
149 * present on the wire.
150 */
151 length = swqe->length;
152 memset(&wc, 0, sizeof(wc));
153 wc.byte_len = length + sizeof(struct ib_grh);
154
155 if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
156 wc.wc_flags = IB_WC_WITH_IMM;
157 wc.ex.imm_data = swqe->wr.ex.imm_data;
158 }
159
160 spin_lock_irqsave(&qp->r_lock, flags);
161
162 /*
163 * Get the next work request entry to find where to put the data.
164 */
54d10c1e
DD
165 if (qp->r_flags & RVT_R_REUSE_SGE)
166 qp->r_flags &= ~RVT_R_REUSE_SGE;
77241056
MM
167 else {
168 int ret;
169
ec4274f1 170 ret = hfi1_rvt_get_rwqe(qp, 0);
77241056
MM
171 if (ret < 0) {
172 hfi1_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
173 goto bail_unlock;
174 }
175 if (!ret) {
176 if (qp->ibqp.qp_num == 0)
4eb06882 177 ibp->rvp.n_vl15_dropped++;
77241056
MM
178 goto bail_unlock;
179 }
180 }
181 /* Silently drop packets which are too big. */
182 if (unlikely(wc.byte_len > qp->r_len)) {
54d10c1e 183 qp->r_flags |= RVT_R_REUSE_SGE;
4eb06882 184 ibp->rvp.n_pkt_drops++;
77241056
MM
185 goto bail_unlock;
186 }
187
188 if (ah_attr->ah_flags & IB_AH_GRH) {
189 hfi1_copy_sge(&qp->r_sge, &ah_attr->grh,
7b0b01aa 190 sizeof(struct ib_grh), 1, 0);
77241056
MM
191 wc.wc_flags |= IB_WC_GRH;
192 } else
193 hfi1_skip_sge(&qp->r_sge, sizeof(struct ib_grh), 1);
194 ssge.sg_list = swqe->sg_list + 1;
195 ssge.sge = *swqe->sg_list;
196 ssge.num_sge = swqe->wr.num_sge;
197 sge = &ssge.sge;
198 while (length) {
199 u32 len = sge->length;
200
201 if (len > length)
202 len = length;
203 if (len > sge->sge_length)
204 len = sge->sge_length;
205 WARN_ON_ONCE(len == 0);
7b0b01aa 206 hfi1_copy_sge(&qp->r_sge, sge->vaddr, len, 1, 0);
77241056
MM
207 sge->vaddr += len;
208 sge->length -= len;
209 sge->sge_length -= len;
210 if (sge->sge_length == 0) {
211 if (--ssge.num_sge)
212 *sge = *ssge.sg_list++;
213 } else if (sge->length == 0 && sge->mr->lkey) {
cd4ceee3 214 if (++sge->n >= RVT_SEGSZ) {
77241056
MM
215 if (++sge->m >= sge->mr->mapsz)
216 break;
217 sge->n = 0;
218 }
219 sge->vaddr =
220 sge->mr->map[sge->m]->segs[sge->n].vaddr;
221 sge->length =
222 sge->mr->map[sge->m]->segs[sge->n].length;
223 }
224 length -= len;
225 }
ec4274f1 226 rvt_put_ss(&qp->r_sge);
54d10c1e 227 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
77241056
MM
228 goto bail_unlock;
229 wc.wr_id = qp->r_wr_id;
230 wc.status = IB_WC_SUCCESS;
231 wc.opcode = IB_WC_RECV;
232 wc.qp = &qp->ibqp;
233 wc.src_qp = sqp->ibqp.qp_num;
234 if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI) {
235 if (sqp->ibqp.qp_type == IB_QPT_GSI ||
236 sqp->ibqp.qp_type == IB_QPT_SMI)
e622f2f4 237 wc.pkey_index = swqe->ud_wr.pkey_index;
77241056
MM
238 else
239 wc.pkey_index = sqp->s_pkey_index;
240 } else {
241 wc.pkey_index = 0;
242 }
243 wc.slid = ppd->lid | (ah_attr->src_path_bits & ((1 << ppd->lmc) - 1));
244 /* Check for loopback when the port lid is not set */
245 if (wc.slid == 0 && sqp->ibqp.qp_type == IB_QPT_GSI)
8859b4a6 246 wc.slid = be16_to_cpu(IB_LID_PERMISSIVE);
77241056
MM
247 wc.sl = ah_attr->sl;
248 wc.dlid_path_bits = ah_attr->dlid & ((1 << ppd->lmc) - 1);
249 wc.port_num = qp->port_num;
250 /* Signal completion event if the solicited bit is set. */
abd712da
DD
251 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
252 swqe->wr.send_flags & IB_SEND_SOLICITED);
4eb06882 253 ibp->rvp.n_loop_pkts++;
77241056
MM
254bail_unlock:
255 spin_unlock_irqrestore(&qp->r_lock, flags);
256drop:
257 rcu_read_unlock();
258}
259
260/**
261 * hfi1_make_ud_req - construct a UD request packet
262 * @qp: the QP
263 *
46a80d62
MM
264 * Assume s_lock is held.
265 *
77241056
MM
266 * Return 1 if constructed; otherwise, return 0.
267 */
895420dd 268int hfi1_make_ud_req(struct rvt_qp *qp)
77241056 269{
4c6829c5 270 struct hfi1_qp_priv *priv = qp->priv;
77241056
MM
271 struct hfi1_other_headers *ohdr;
272 struct ib_ah_attr *ah_attr;
273 struct hfi1_pportdata *ppd;
274 struct hfi1_ibport *ibp;
895420dd 275 struct rvt_swqe *wqe;
77241056
MM
276 u32 nwords;
277 u32 extra_bytes;
278 u32 bth0;
279 u16 lrh0;
280 u16 lid;
281 int ret = 0;
282 int next_cur;
283 u8 sc5;
284
83693bd1
DD
285 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
286 if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
77241056
MM
287 goto bail;
288 /* We are in the error state, flush the work request. */
46a80d62
MM
289 smp_read_barrier_depends(); /* see post_one_send */
290 if (qp->s_last == ACCESS_ONCE(qp->s_head))
77241056
MM
291 goto bail;
292 /* If DMAs are in progress, we can't flush immediately. */
4c6829c5 293 if (atomic_read(&priv->s_iowait.sdma_busy)) {
54d10c1e 294 qp->s_flags |= RVT_S_WAIT_DMA;
77241056
MM
295 goto bail;
296 }
83693bd1 297 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
77241056
MM
298 hfi1_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
299 goto done;
300 }
301
46a80d62
MM
302 /* see post_one_send() */
303 smp_read_barrier_depends();
304 if (qp->s_cur == ACCESS_ONCE(qp->s_head))
77241056
MM
305 goto bail;
306
83693bd1 307 wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
77241056
MM
308 next_cur = qp->s_cur + 1;
309 if (next_cur >= qp->s_size)
310 next_cur = 0;
311
312 /* Construct the header. */
313 ibp = to_iport(qp->ibqp.device, qp->port_num);
314 ppd = ppd_from_ibp(ibp);
15723f06 315 ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;
8859b4a6
DD
316 if (ah_attr->dlid < be16_to_cpu(IB_MULTICAST_LID_BASE) ||
317 ah_attr->dlid == be16_to_cpu(IB_LID_PERMISSIVE)) {
77241056
MM
318 lid = ah_attr->dlid & ~((1 << ppd->lmc) - 1);
319 if (unlikely(!loopback && (lid == ppd->lid ||
8859b4a6 320 (lid == be16_to_cpu(IB_LID_PERMISSIVE) &&
77241056 321 qp->ibqp.qp_type == IB_QPT_GSI)))) {
46a80d62 322 unsigned long flags;
77241056
MM
323 /*
324 * If DMAs are in progress, we can't generate
325 * a completion for the loopback packet since
326 * it would be out of order.
327 * Instead of waiting, we could queue a
328 * zero length descriptor so we get a callback.
329 */
4c6829c5 330 if (atomic_read(&priv->s_iowait.sdma_busy)) {
54d10c1e 331 qp->s_flags |= RVT_S_WAIT_DMA;
77241056
MM
332 goto bail;
333 }
334 qp->s_cur = next_cur;
46a80d62 335 local_irq_save(flags);
77241056
MM
336 spin_unlock_irqrestore(&qp->s_lock, flags);
337 ud_loopback(qp, wqe);
338 spin_lock_irqsave(&qp->s_lock, flags);
339 hfi1_send_complete(qp, wqe, IB_WC_SUCCESS);
340 goto done;
341 }
342 }
343
344 qp->s_cur = next_cur;
345 extra_bytes = -wqe->length & 3;
346 nwords = (wqe->length + extra_bytes) >> 2;
347
348 /* header size in 32-bit words LRH+BTH+DETH = (8+12+8)/4. */
349 qp->s_hdrwords = 7;
350 qp->s_cur_size = wqe->length;
351 qp->s_cur_sge = &qp->s_sge;
352 qp->s_srate = ah_attr->static_rate;
353 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
354 qp->s_wqe = wqe;
355 qp->s_sge.sge = wqe->sg_list[0];
356 qp->s_sge.sg_list = wqe->sg_list + 1;
357 qp->s_sge.num_sge = wqe->wr.num_sge;
358 qp->s_sge.total_len = wqe->length;
359
360 if (ah_attr->ah_flags & IB_AH_GRH) {
361 /* Header size in 32-bit words. */
4c6829c5 362 qp->s_hdrwords += hfi1_make_grh(ibp, &priv->s_hdr->ibh.u.l.grh,
77241056
MM
363 &ah_attr->grh,
364 qp->s_hdrwords, nwords);
365 lrh0 = HFI1_LRH_GRH;
4c6829c5 366 ohdr = &priv->s_hdr->ibh.u.l.oth;
77241056
MM
367 /*
368 * Don't worry about sending to locally attached multicast
369 * QPs. It is unspecified by the spec. what happens.
370 */
371 } else {
372 /* Header size in 32-bit words. */
373 lrh0 = HFI1_LRH_BTH;
4c6829c5 374 ohdr = &priv->s_hdr->ibh.u.oth;
77241056
MM
375 }
376 if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
377 qp->s_hdrwords++;
378 ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;
379 bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
380 } else
381 bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
382 sc5 = ibp->sl_to_sc[ah_attr->sl];
383 lrh0 |= (ah_attr->sl & 0xf) << 4;
384 if (qp->ibqp.qp_type == IB_QPT_SMI) {
385 lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
4c6829c5 386 priv->s_sc = 0xf;
77241056
MM
387 } else {
388 lrh0 |= (sc5 & 0xf) << 12;
4c6829c5 389 priv->s_sc = sc5;
77241056 390 }
4c6829c5
DD
391 priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
392 priv->s_hdr->ibh.lrh[0] = cpu_to_be16(lrh0);
393 priv->s_hdr->ibh.lrh[1] = cpu_to_be16(ah_attr->dlid); /* DEST LID */
394 priv->s_hdr->ibh.lrh[2] =
77241056
MM
395 cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
396 if (ah_attr->dlid == be16_to_cpu(IB_LID_PERMISSIVE))
4c6829c5 397 priv->s_hdr->ibh.lrh[3] = IB_LID_PERMISSIVE;
77241056
MM
398 else {
399 lid = ppd->lid;
400 if (lid) {
401 lid |= ah_attr->src_path_bits & ((1 << ppd->lmc) - 1);
4c6829c5 402 priv->s_hdr->ibh.lrh[3] = cpu_to_be16(lid);
77241056 403 } else
4c6829c5 404 priv->s_hdr->ibh.lrh[3] = IB_LID_PERMISSIVE;
77241056
MM
405 }
406 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
407 bth0 |= IB_BTH_SOLICITED;
408 bth0 |= extra_bytes << 20;
409 if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI)
e622f2f4 410 bth0 |= hfi1_get_pkey(ibp, wqe->ud_wr.pkey_index);
77241056
MM
411 else
412 bth0 |= hfi1_get_pkey(ibp, qp->s_pkey_index);
413 ohdr->bth[0] = cpu_to_be32(bth0);
e622f2f4 414 ohdr->bth[1] = cpu_to_be32(wqe->ud_wr.remote_qpn);
46a80d62 415 ohdr->bth[2] = cpu_to_be32(mask_psn(wqe->psn));
77241056
MM
416 /*
417 * Qkeys with the high order bit set mean use the
418 * qkey from the QP context instead of the WR (see 10.2.5).
419 */
e622f2f4
CH
420 ohdr->u.ud.deth[0] = cpu_to_be32((int)wqe->ud_wr.remote_qkey < 0 ?
421 qp->qkey : wqe->ud_wr.remote_qkey);
77241056
MM
422 ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
423 /* disarm any ahg */
4c6829c5
DD
424 priv->s_hdr->ahgcount = 0;
425 priv->s_hdr->ahgidx = 0;
426 priv->s_hdr->tx_flags = 0;
427 priv->s_hdr->sde = NULL;
77241056
MM
428
429done:
46a80d62 430 return 1;
77241056 431bail:
54d10c1e 432 qp->s_flags &= ~RVT_S_BUSY;
77241056
MM
433 return ret;
434}
435
436/*
437 * Hardware can't check this so we do it here.
438 *
439 * This is a slightly different algorithm than the standard pkey check. It
440 * special cases the management keys and allows for 0x7fff and 0xffff to be in
441 * the table at the same time.
442 *
443 * @returns the index found or -1 if not found
444 */
445int hfi1_lookup_pkey_idx(struct hfi1_ibport *ibp, u16 pkey)
446{
447 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
448 unsigned i;
449
450 if (pkey == FULL_MGMT_P_KEY || pkey == LIM_MGMT_P_KEY) {
451 unsigned lim_idx = -1;
452
453 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i) {
454 /* here we look for an exact match */
455 if (ppd->pkeys[i] == pkey)
456 return i;
457 if (ppd->pkeys[i] == LIM_MGMT_P_KEY)
458 lim_idx = i;
459 }
460
461 /* did not find 0xffff return 0x7fff idx if found */
462 if (pkey == FULL_MGMT_P_KEY)
463 return lim_idx;
464
465 /* no match... */
466 return -1;
467 }
468
469 pkey &= 0x7fff; /* remove limited/full membership bit */
470
471 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i)
472 if ((ppd->pkeys[i] & 0x7fff) == pkey)
473 return i;
474
475 /*
476 * Should not get here, this means hardware failed to validate pkeys.
477 */
478 return -1;
479}
480
895420dd 481void return_cnp(struct hfi1_ibport *ibp, struct rvt_qp *qp, u32 remote_qpn,
77241056
MM
482 u32 pkey, u32 slid, u32 dlid, u8 sc5,
483 const struct ib_grh *old_grh)
484{
485 u64 pbc, pbc_flags = 0;
486 u32 bth0, plen, vl, hwords = 5;
487 u16 lrh0;
488 u8 sl = ibp->sc_to_sl[sc5];
489 struct hfi1_ib_header hdr;
490 struct hfi1_other_headers *ohdr;
491 struct pio_buf *pbuf;
492 struct send_context *ctxt = qp_to_send_context(qp, sc5);
493 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
494
495 if (old_grh) {
496 struct ib_grh *grh = &hdr.u.l.grh;
497
498 grh->version_tclass_flow = old_grh->version_tclass_flow;
499 grh->paylen = cpu_to_be16((hwords - 2 + SIZE_OF_CRC) << 2);
500 grh->hop_limit = 0xff;
501 grh->sgid = old_grh->dgid;
502 grh->dgid = old_grh->sgid;
503 ohdr = &hdr.u.l.oth;
504 lrh0 = HFI1_LRH_GRH;
505 hwords += sizeof(struct ib_grh) / sizeof(u32);
506 } else {
507 ohdr = &hdr.u.oth;
508 lrh0 = HFI1_LRH_BTH;
509 }
510
511 lrh0 |= (sc5 & 0xf) << 12 | sl << 4;
512
513 bth0 = pkey | (IB_OPCODE_CNP << 24);
514 ohdr->bth[0] = cpu_to_be32(bth0);
515
516 ohdr->bth[1] = cpu_to_be32(remote_qpn | (1 << HFI1_BECN_SHIFT));
517 ohdr->bth[2] = 0; /* PSN 0 */
518
519 hdr.lrh[0] = cpu_to_be16(lrh0);
520 hdr.lrh[1] = cpu_to_be16(dlid);
521 hdr.lrh[2] = cpu_to_be16(hwords + SIZE_OF_CRC);
522 hdr.lrh[3] = cpu_to_be16(slid);
523
524 plen = 2 /* PBC */ + hwords;
525 pbc_flags |= (!!(sc5 & 0x10)) << PBC_DC_INFO_SHIFT;
526 vl = sc_to_vlt(ppd->dd, sc5);
527 pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
528 if (ctxt) {
529 pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
530 if (pbuf)
531 ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
532 &hdr, hwords);
533 }
534}
535
536/*
537 * opa_smp_check() - Do the regular pkey checking, and the additional
538 * checks for SMPs specified in OPAv1 rev 0.90, section 9.10.26
539 * ("SMA Packet Checks").
540 *
541 * Note that:
542 * - Checks are done using the pkey directly from the packet's BTH,
543 * and specifically _not_ the pkey that we attach to the completion,
544 * which may be different.
545 * - These checks are specifically for "non-local" SMPs (i.e., SMPs
546 * which originated on another node). SMPs which are sent from, and
547 * destined to this node are checked in opa_local_smp_check().
548 *
549 * At the point where opa_smp_check() is called, we know:
550 * - destination QP is QP0
551 *
552 * opa_smp_check() returns 0 if all checks succeed, 1 otherwise.
553 */
554static int opa_smp_check(struct hfi1_ibport *ibp, u16 pkey, u8 sc5,
895420dd 555 struct rvt_qp *qp, u16 slid, struct opa_smp *smp)
77241056
MM
556{
557 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
558
559 /*
560 * I don't think it's possible for us to get here with sc != 0xf,
561 * but check it to be certain.
562 */
563 if (sc5 != 0xf)
564 return 1;
565
566 if (rcv_pkey_check(ppd, pkey, sc5, slid))
567 return 1;
568
569 /*
570 * At this point we know (and so don't need to check again) that
571 * the pkey is either LIM_MGMT_P_KEY, or FULL_MGMT_P_KEY
572 * (see ingress_pkey_check).
573 */
574 if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE &&
575 smp->mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED) {
576 ingress_pkey_table_fail(ppd, pkey, slid);
577 return 1;
578 }
579
580 /*
581 * SMPs fall into one of four (disjoint) categories:
582 * SMA request, SMA response, trap, or trap repress.
583 * Our response depends, in part, on which type of
584 * SMP we're processing.
585 *
586 * If this is not an SMA request, or trap repress:
587 * - accept MAD if the port is running an SM
588 * - pkey == FULL_MGMT_P_KEY =>
589 * reply with unsupported method (i.e., just mark
590 * the smp's status field here, and let it be
591 * processed normally)
592 * - pkey != LIM_MGMT_P_KEY =>
593 * increment port recv constraint errors, drop MAD
594 * If this is an SMA request or trap repress:
595 * - pkey != FULL_MGMT_P_KEY =>
596 * increment port recv constraint errors, drop MAD
597 */
598 switch (smp->method) {
599 case IB_MGMT_METHOD_GET:
600 case IB_MGMT_METHOD_SET:
601 case IB_MGMT_METHOD_REPORT:
602 case IB_MGMT_METHOD_TRAP_REPRESS:
603 if (pkey != FULL_MGMT_P_KEY) {
604 ingress_pkey_table_fail(ppd, pkey, slid);
605 return 1;
606 }
607 break;
608 case IB_MGMT_METHOD_SEND:
609 case IB_MGMT_METHOD_TRAP:
610 case IB_MGMT_METHOD_GET_RESP:
611 case IB_MGMT_METHOD_REPORT_RESP:
4eb06882 612 if (ibp->rvp.port_cap_flags & IB_PORT_SM)
77241056
MM
613 return 0;
614 if (pkey == FULL_MGMT_P_KEY) {
615 smp->status |= IB_SMP_UNSUP_METHOD;
616 return 0;
617 }
618 if (pkey != LIM_MGMT_P_KEY) {
619 ingress_pkey_table_fail(ppd, pkey, slid);
620 return 1;
621 }
622 break;
623 default:
624 break;
625 }
626 return 0;
627}
628
629
630/**
631 * hfi1_ud_rcv - receive an incoming UD packet
632 * @ibp: the port the packet came in on
633 * @hdr: the packet header
634 * @rcv_flags: flags relevant to rcv processing
635 * @data: the packet data
636 * @tlen: the packet length
637 * @qp: the QP the packet came on
638 *
639 * This is called from qp_rcv() to process an incoming UD packet
640 * for the given QP.
641 * Called at interrupt level.
642 */
643void hfi1_ud_rcv(struct hfi1_packet *packet)
644{
645 struct hfi1_other_headers *ohdr = packet->ohdr;
646 int opcode;
647 u32 hdrsize = packet->hlen;
648 u32 pad;
649 struct ib_wc wc;
650 u32 qkey;
651 u32 src_qp;
652 u16 dlid, pkey;
653 int mgmt_pkey_idx = -1;
654 struct hfi1_ibport *ibp = &packet->rcd->ppd->ibport_data;
655 struct hfi1_ib_header *hdr = packet->hdr;
656 u32 rcv_flags = packet->rcv_flags;
657 void *data = packet->ebuf;
658 u32 tlen = packet->tlen;
895420dd 659 struct rvt_qp *qp = packet->qp;
77241056
MM
660 bool has_grh = rcv_flags & HFI1_HAS_GRH;
661 bool sc4_bit = has_sc4_bit(packet);
662 u8 sc;
663 u32 bth1;
664 int is_mcast;
665 struct ib_grh *grh = NULL;
666
667 qkey = be32_to_cpu(ohdr->u.ud.deth[0]);
ec4274f1 668 src_qp = be32_to_cpu(ohdr->u.ud.deth[1]) & RVT_QPN_MASK;
77241056 669 dlid = be16_to_cpu(hdr->lrh[1]);
8859b4a6
DD
670 is_mcast = (dlid > be16_to_cpu(IB_MULTICAST_LID_BASE)) &&
671 (dlid != be16_to_cpu(IB_LID_PERMISSIVE));
77241056
MM
672 bth1 = be32_to_cpu(ohdr->bth[1]);
673 if (unlikely(bth1 & HFI1_BECN_SMASK)) {
674 /*
675 * In pre-B0 h/w the CNP_OPCODE is handled via an
624be1db 676 * error path.
77241056
MM
677 */
678 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
ec4274f1 679 u32 lqpn = be32_to_cpu(ohdr->bth[1]) & RVT_QPN_MASK;
77241056
MM
680 u8 sl, sc5;
681
682 sc5 = (be16_to_cpu(hdr->lrh[0]) >> 12) & 0xf;
683 sc5 |= sc4_bit;
684 sl = ibp->sc_to_sl[sc5];
685
686 process_becn(ppd, sl, 0, lqpn, 0, IB_CC_SVCTYPE_UD);
687 }
688
689 /*
690 * The opcode is in the low byte when its in network order
691 * (top byte when in host order).
692 */
693 opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
694 opcode &= 0xff;
695
696 pkey = (u16)be32_to_cpu(ohdr->bth[0]);
697
698 if (!is_mcast && (opcode != IB_OPCODE_CNP) && bth1 & HFI1_FECN_SMASK) {
699 u16 slid = be16_to_cpu(hdr->lrh[3]);
700 u8 sc5;
701
702 sc5 = (be16_to_cpu(hdr->lrh[0]) >> 12) & 0xf;
703 sc5 |= sc4_bit;
704
705 return_cnp(ibp, qp, src_qp, pkey, dlid, slid, sc5, grh);
706 }
707 /*
708 * Get the number of bytes the message was padded by
709 * and drop incomplete packets.
710 */
711 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
712 if (unlikely(tlen < (hdrsize + pad + 4)))
713 goto drop;
714
715 tlen -= hdrsize + pad + 4;
716
717 /*
718 * Check that the permissive LID is only used on QP0
719 * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
720 */
721 if (qp->ibqp.qp_num) {
722 if (unlikely(hdr->lrh[1] == IB_LID_PERMISSIVE ||
723 hdr->lrh[3] == IB_LID_PERMISSIVE))
724 goto drop;
725 if (qp->ibqp.qp_num > 1) {
726 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
727 u16 slid;
728 u8 sc5;
729
730 sc5 = (be16_to_cpu(hdr->lrh[0]) >> 12) & 0xf;
731 sc5 |= sc4_bit;
732
733 slid = be16_to_cpu(hdr->lrh[3]);
734 if (unlikely(rcv_pkey_check(ppd, pkey, sc5, slid))) {
735 /*
736 * Traps will not be sent for packets dropped
737 * by the HW. This is fine, as sending trap
738 * for invalid pkeys is optional according to
739 * IB spec (release 1.3, section 10.9.4)
740 */
5cd24119 741 hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_P_KEY,
77241056
MM
742 pkey,
743 (be16_to_cpu(hdr->lrh[0]) >> 4) &
744 0xF,
745 src_qp, qp->ibqp.qp_num,
5cd24119
EK
746 be16_to_cpu(hdr->lrh[3]),
747 be16_to_cpu(hdr->lrh[1]));
77241056
MM
748 return;
749 }
750 } else {
751 /* GSI packet */
752 mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
753 if (mgmt_pkey_idx < 0)
754 goto drop;
755
756 }
757 if (unlikely(qkey != qp->qkey)) {
5cd24119 758 hfi1_bad_pqkey(ibp, OPA_TRAP_BAD_Q_KEY, qkey,
77241056
MM
759 (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
760 src_qp, qp->ibqp.qp_num,
5cd24119
EK
761 be16_to_cpu(hdr->lrh[3]),
762 be16_to_cpu(hdr->lrh[1]));
77241056
MM
763 return;
764 }
765 /* Drop invalid MAD packets (see 13.5.3.1). */
766 if (unlikely(qp->ibqp.qp_num == 1 &&
767 (tlen > 2048 ||
768 (be16_to_cpu(hdr->lrh[0]) >> 12) == 15)))
769 goto drop;
770 } else {
771 /* Received on QP0, and so by definition, this is an SMP */
772 struct opa_smp *smp = (struct opa_smp *)data;
773 u16 slid = be16_to_cpu(hdr->lrh[3]);
774 u8 sc5;
775
776 sc5 = (be16_to_cpu(hdr->lrh[0]) >> 12) & 0xf;
777 sc5 |= sc4_bit;
778
779 if (opa_smp_check(ibp, pkey, sc5, qp, slid, smp))
780 goto drop;
781
782 if (tlen > 2048)
783 goto drop;
784 if ((hdr->lrh[1] == IB_LID_PERMISSIVE ||
785 hdr->lrh[3] == IB_LID_PERMISSIVE) &&
786 smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
787 goto drop;
788
789 /* look up SMI pkey */
790 mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
791 if (mgmt_pkey_idx < 0)
792 goto drop;
793
794 }
795
796 if (qp->ibqp.qp_num > 1 &&
797 opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
798 wc.ex.imm_data = ohdr->u.ud.imm_data;
799 wc.wc_flags = IB_WC_WITH_IMM;
800 tlen -= sizeof(u32);
801 } else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
802 wc.ex.imm_data = 0;
803 wc.wc_flags = 0;
804 } else
805 goto drop;
806
807 /*
808 * A GRH is expected to precede the data even if not
809 * present on the wire.
810 */
811 wc.byte_len = tlen + sizeof(struct ib_grh);
812
813 /*
814 * Get the next work request entry to find where to put the data.
815 */
54d10c1e
DD
816 if (qp->r_flags & RVT_R_REUSE_SGE)
817 qp->r_flags &= ~RVT_R_REUSE_SGE;
77241056
MM
818 else {
819 int ret;
820
ec4274f1 821 ret = hfi1_rvt_get_rwqe(qp, 0);
77241056
MM
822 if (ret < 0) {
823 hfi1_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
824 return;
825 }
826 if (!ret) {
827 if (qp->ibqp.qp_num == 0)
4eb06882 828 ibp->rvp.n_vl15_dropped++;
77241056
MM
829 return;
830 }
831 }
832 /* Silently drop packets which are too big. */
833 if (unlikely(wc.byte_len > qp->r_len)) {
54d10c1e 834 qp->r_flags |= RVT_R_REUSE_SGE;
77241056
MM
835 goto drop;
836 }
837 if (has_grh) {
838 hfi1_copy_sge(&qp->r_sge, &hdr->u.l.grh,
7b0b01aa 839 sizeof(struct ib_grh), 1, 0);
77241056
MM
840 wc.wc_flags |= IB_WC_GRH;
841 } else
842 hfi1_skip_sge(&qp->r_sge, sizeof(struct ib_grh), 1);
7b0b01aa
DL
843 hfi1_copy_sge(&qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh),
844 1, 0);
ec4274f1 845 rvt_put_ss(&qp->r_sge);
54d10c1e 846 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
77241056
MM
847 return;
848 wc.wr_id = qp->r_wr_id;
849 wc.status = IB_WC_SUCCESS;
850 wc.opcode = IB_WC_RECV;
851 wc.vendor_err = 0;
852 wc.qp = &qp->ibqp;
853 wc.src_qp = src_qp;
854
855 if (qp->ibqp.qp_type == IB_QPT_GSI ||
856 qp->ibqp.qp_type == IB_QPT_SMI) {
857 if (mgmt_pkey_idx < 0) {
858 if (net_ratelimit()) {
859 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
860 struct hfi1_devdata *dd = ppd->dd;
861
862 dd_dev_err(dd, "QP type %d mgmt_pkey_idx < 0 and packet not dropped???\n",
863 qp->ibqp.qp_type);
864 mgmt_pkey_idx = 0;
865 }
866 }
867 wc.pkey_index = (unsigned)mgmt_pkey_idx;
868 } else
869 wc.pkey_index = 0;
870
871 wc.slid = be16_to_cpu(hdr->lrh[3]);
872 sc = (be16_to_cpu(hdr->lrh[0]) >> 12) & 0xf;
873 sc |= sc4_bit;
874 wc.sl = ibp->sc_to_sl[sc];
875
876 /*
877 * Save the LMC lower bits if the destination LID is a unicast LID.
878 */
8859b4a6 879 wc.dlid_path_bits = dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE) ? 0 :
77241056
MM
880 dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1);
881 wc.port_num = qp->port_num;
882 /* Signal completion event if the solicited bit is set. */
abd712da
DD
883 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
884 (ohdr->bth[0] &
885 cpu_to_be32(IB_BTH_SOLICITED)) != 0);
77241056
MM
886 return;
887
888drop:
4eb06882 889 ibp->rvp.n_pkt_drops++;
77241056 890}