2 * Copyright(c) 2015 - 2018 Intel Corporation.
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.
9 * 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.
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.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
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
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.
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.
48 #include <linux/net.h>
49 #include <rdma/ib_smi.h>
53 #include "verbs_txreq.h"
54 #include "trace_ibhdrs.h"
57 /* We support only two types - 9B and 16B for now */
58 static const hfi1_make_req hfi1_make_ud_req_tbl[2] = {
59 [HFI1_PKT_TYPE_9B] = &hfi1_make_ud_req_9B,
60 [HFI1_PKT_TYPE_16B] = &hfi1_make_ud_req_16B
64 * ud_loopback - handle send on loopback QPs
65 * @sqp: the sending QP
66 * @swqe: the send work request
68 * This is called from hfi1_make_ud_req() to forward a WQE addressed
70 * Note that the receive interrupt handler may be calling hfi1_ud_rcv()
71 * while this is being called.
73 static void ud_loopback(struct rvt_qp *sqp, struct rvt_swqe *swqe)
75 struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
76 struct hfi1_pportdata *ppd;
77 struct hfi1_qp_priv *priv = sqp->priv;
79 struct rdma_ah_attr *ah_attr;
81 struct rvt_sge_state ssge;
85 enum ib_qp_type sqptype, dqptype;
89 qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
90 swqe->ud_wr.remote_qpn);
92 ibp->rvp.n_pkt_drops++;
97 sqptype = sqp->ibqp.qp_type == IB_QPT_GSI ?
98 IB_QPT_UD : sqp->ibqp.qp_type;
99 dqptype = qp->ibqp.qp_type == IB_QPT_GSI ?
100 IB_QPT_UD : qp->ibqp.qp_type;
102 if (dqptype != sqptype ||
103 !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
104 ibp->rvp.n_pkt_drops++;
108 ah_attr = &ibah_to_rvtah(swqe->ud_wr.ah)->attr;
109 ppd = ppd_from_ibp(ibp);
111 if (qp->ibqp.qp_num > 1) {
114 u8 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
116 pkey = hfi1_get_pkey(ibp, sqp->s_pkey_index);
117 slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
118 ((1 << ppd->lmc) - 1));
119 if (unlikely(ingress_pkey_check(ppd, pkey, sc5,
122 hfi1_bad_pkey(ibp, pkey,
123 rdma_ah_get_sl(ah_attr),
124 sqp->ibqp.qp_num, qp->ibqp.qp_num,
125 slid, rdma_ah_get_dlid(ah_attr));
131 * Check that the qkey matches (except for QP0, see 9.6.1.4.1).
132 * Qkeys with the high order bit set mean use the
133 * qkey from the QP context instead of the WR (see 10.2.5).
135 if (qp->ibqp.qp_num) {
138 qkey = (int)swqe->ud_wr.remote_qkey < 0 ?
139 sqp->qkey : swqe->ud_wr.remote_qkey;
140 if (unlikely(qkey != qp->qkey))
141 goto drop; /* silently drop per IBTA spec */
145 * A GRH is expected to precede the data even if not
146 * present on the wire.
148 length = swqe->length;
149 memset(&wc, 0, sizeof(wc));
150 wc.byte_len = length + sizeof(struct ib_grh);
152 if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
153 wc.wc_flags = IB_WC_WITH_IMM;
154 wc.ex.imm_data = swqe->wr.ex.imm_data;
157 spin_lock_irqsave(&qp->r_lock, flags);
160 * Get the next work request entry to find where to put the data.
162 if (qp->r_flags & RVT_R_REUSE_SGE) {
163 qp->r_flags &= ~RVT_R_REUSE_SGE;
167 ret = rvt_get_rwqe(qp, false);
169 rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
173 if (qp->ibqp.qp_num == 0)
174 ibp->rvp.n_vl15_dropped++;
178 /* Silently drop packets which are too big. */
179 if (unlikely(wc.byte_len > qp->r_len)) {
180 qp->r_flags |= RVT_R_REUSE_SGE;
181 ibp->rvp.n_pkt_drops++;
185 if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
187 struct ib_global_route grd = *(rdma_ah_read_grh(ah_attr));
190 * For loopback packets with extended LIDs, the
191 * sgid_index in the GRH is 0 and the dgid is
192 * OPA GID of the sender. While creating a response
193 * to the loopback packet, IB core creates the new
194 * sgid_index from the DGID and that will be the
195 * OPA_GID_INDEX. The new dgid is from the sgid
196 * index and that will be in the IB GID format.
198 * We now have a case where the sent packet had a
199 * different sgid_index and dgid compared to the
200 * one that was received in response.
202 * Fix this inconsistency.
204 if (priv->hdr_type == HFI1_PKT_TYPE_16B) {
205 if (grd.sgid_index == 0)
206 grd.sgid_index = OPA_GID_INDEX;
208 if (ib_is_opa_gid(&grd.dgid))
209 grd.dgid.global.interface_id =
210 cpu_to_be64(ppd->guids[HFI1_PORT_GUID_INDEX]);
213 hfi1_make_grh(ibp, &grh, &grd, 0, 0);
214 rvt_copy_sge(qp, &qp->r_sge, &grh,
215 sizeof(grh), true, false);
216 wc.wc_flags |= IB_WC_GRH;
218 rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
220 ssge.sg_list = swqe->sg_list + 1;
221 ssge.sge = *swqe->sg_list;
222 ssge.num_sge = swqe->wr.num_sge;
225 u32 len = sge->length;
229 if (len > sge->sge_length)
230 len = sge->sge_length;
231 WARN_ON_ONCE(len == 0);
232 rvt_copy_sge(qp, &qp->r_sge, sge->vaddr, len, true, false);
235 sge->sge_length -= len;
236 if (sge->sge_length == 0) {
238 *sge = *ssge.sg_list++;
239 } else if (sge->length == 0 && sge->mr->lkey) {
240 if (++sge->n >= RVT_SEGSZ) {
241 if (++sge->m >= sge->mr->mapsz)
246 sge->mr->map[sge->m]->segs[sge->n].vaddr;
248 sge->mr->map[sge->m]->segs[sge->n].length;
252 rvt_put_ss(&qp->r_sge);
253 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
255 wc.wr_id = qp->r_wr_id;
256 wc.status = IB_WC_SUCCESS;
257 wc.opcode = IB_WC_RECV;
259 wc.src_qp = sqp->ibqp.qp_num;
260 if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI) {
261 if (sqp->ibqp.qp_type == IB_QPT_GSI ||
262 sqp->ibqp.qp_type == IB_QPT_SMI)
263 wc.pkey_index = swqe->ud_wr.pkey_index;
265 wc.pkey_index = sqp->s_pkey_index;
269 wc.slid = (ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
270 ((1 << ppd->lmc) - 1))) & U16_MAX;
271 /* Check for loopback when the port lid is not set */
272 if (wc.slid == 0 && sqp->ibqp.qp_type == IB_QPT_GSI)
273 wc.slid = be16_to_cpu(IB_LID_PERMISSIVE);
274 wc.sl = rdma_ah_get_sl(ah_attr);
275 wc.dlid_path_bits = rdma_ah_get_dlid(ah_attr) & ((1 << ppd->lmc) - 1);
276 wc.port_num = qp->port_num;
277 /* Signal completion event if the solicited bit is set. */
278 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc,
279 swqe->wr.send_flags & IB_SEND_SOLICITED);
280 ibp->rvp.n_loop_pkts++;
282 spin_unlock_irqrestore(&qp->r_lock, flags);
287 static void hfi1_make_bth_deth(struct rvt_qp *qp, struct rvt_swqe *wqe,
288 struct ib_other_headers *ohdr,
289 u16 *pkey, u32 extra_bytes, bool bypass)
292 struct hfi1_ibport *ibp;
294 ibp = to_iport(qp->ibqp.device, qp->port_num);
295 if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
296 ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;
297 bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
299 bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
302 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
303 bth0 |= IB_BTH_SOLICITED;
304 bth0 |= extra_bytes << 20;
305 if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI)
306 *pkey = hfi1_get_pkey(ibp, wqe->ud_wr.pkey_index);
308 *pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
311 ohdr->bth[0] = cpu_to_be32(bth0);
312 ohdr->bth[1] = cpu_to_be32(wqe->ud_wr.remote_qpn);
313 ohdr->bth[2] = cpu_to_be32(mask_psn(wqe->psn));
315 * Qkeys with the high order bit set mean use the
316 * qkey from the QP context instead of the WR (see 10.2.5).
318 ohdr->u.ud.deth[0] = cpu_to_be32((int)wqe->ud_wr.remote_qkey < 0 ?
319 qp->qkey : wqe->ud_wr.remote_qkey);
320 ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
323 void hfi1_make_ud_req_9B(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
324 struct rvt_swqe *wqe)
326 u32 nwords, extra_bytes;
327 u16 len, slid, dlid, pkey;
330 struct hfi1_qp_priv *priv = qp->priv;
331 struct ib_other_headers *ohdr;
332 struct rdma_ah_attr *ah_attr;
333 struct hfi1_pportdata *ppd;
334 struct hfi1_ibport *ibp;
337 ibp = to_iport(qp->ibqp.device, qp->port_num);
338 ppd = ppd_from_ibp(ibp);
339 ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;
341 extra_bytes = -wqe->length & 3;
342 nwords = ((wqe->length + extra_bytes) >> 2) + SIZE_OF_CRC;
343 /* header size in dwords LRH+BTH+DETH = (8+12+8)/4. */
344 ps->s_txreq->hdr_dwords = 7;
345 if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
346 ps->s_txreq->hdr_dwords++;
348 if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
349 grh = &ps->s_txreq->phdr.hdr.ibh.u.l.grh;
350 ps->s_txreq->hdr_dwords +=
351 hfi1_make_grh(ibp, grh, rdma_ah_read_grh(ah_attr),
352 ps->s_txreq->hdr_dwords - LRH_9B_DWORDS,
355 ohdr = &ps->s_txreq->phdr.hdr.ibh.u.l.oth;
358 ohdr = &ps->s_txreq->phdr.hdr.ibh.u.oth;
361 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
362 lrh0 |= (rdma_ah_get_sl(ah_attr) & 0xf) << 4;
363 if (qp->ibqp.qp_type == IB_QPT_SMI) {
364 lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
367 lrh0 |= (sc5 & 0xf) << 12;
371 dlid = opa_get_lid(rdma_ah_get_dlid(ah_attr), 9B);
372 if (dlid == be16_to_cpu(IB_LID_PERMISSIVE)) {
373 slid = be16_to_cpu(IB_LID_PERMISSIVE);
375 u16 lid = (u16)ppd->lid;
378 lid |= rdma_ah_get_path_bits(ah_attr) &
379 ((1 << ppd->lmc) - 1);
382 slid = be16_to_cpu(IB_LID_PERMISSIVE);
385 hfi1_make_bth_deth(qp, wqe, ohdr, &pkey, extra_bytes, false);
386 len = ps->s_txreq->hdr_dwords + nwords;
388 /* Setup the packet */
389 ps->s_txreq->phdr.hdr.hdr_type = HFI1_PKT_TYPE_9B;
390 hfi1_make_ib_hdr(&ps->s_txreq->phdr.hdr.ibh,
391 lrh0, len, dlid, slid);
394 void hfi1_make_ud_req_16B(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
395 struct rvt_swqe *wqe)
397 struct hfi1_qp_priv *priv = qp->priv;
398 struct ib_other_headers *ohdr;
399 struct rdma_ah_attr *ah_attr;
400 struct hfi1_pportdata *ppd;
401 struct hfi1_ibport *ibp;
402 u32 dlid, slid, nwords, extra_bytes;
403 u32 dest_qp = wqe->ud_wr.remote_qpn;
404 u32 src_qp = qp->ibqp.qp_num;
407 bool is_mgmt = false;
409 ibp = to_iport(qp->ibqp.device, qp->port_num);
410 ppd = ppd_from_ibp(ibp);
411 ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;
414 * Build 16B Management Packet if either the destination
415 * or source queue pair number is 0 or 1.
417 if (dest_qp == 0 || src_qp == 0 || dest_qp == 1 || src_qp == 1) {
418 /* header size in dwords 16B LRH+L4_FM = (16+8)/4. */
419 ps->s_txreq->hdr_dwords = 6;
422 /* header size in dwords 16B LRH+BTH+DETH = (16+12+8)/4. */
423 ps->s_txreq->hdr_dwords = 9;
424 if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
425 ps->s_txreq->hdr_dwords++;
428 /* SW provides space for CRC and LT for bypass packets. */
429 extra_bytes = hfi1_get_16b_padding((ps->s_txreq->hdr_dwords << 2),
431 nwords = ((wqe->length + extra_bytes + SIZE_OF_LT) >> 2) + SIZE_OF_CRC;
433 if ((rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) &&
434 hfi1_check_mcast(rdma_ah_get_dlid(ah_attr))) {
436 struct ib_global_route *grd = rdma_ah_retrieve_grh(ah_attr);
438 * Ensure OPA GIDs are transformed to IB gids
439 * before creating the GRH.
441 if (grd->sgid_index == OPA_GID_INDEX) {
442 dd_dev_warn(ppd->dd, "Bad sgid_index. sgid_index: %d\n",
446 grh = &ps->s_txreq->phdr.hdr.opah.u.l.grh;
447 ps->s_txreq->hdr_dwords += hfi1_make_grh(
449 ps->s_txreq->hdr_dwords - LRH_16B_DWORDS,
451 ohdr = &ps->s_txreq->phdr.hdr.opah.u.l.oth;
452 l4 = OPA_16B_L4_IB_GLOBAL;
454 ohdr = &ps->s_txreq->phdr.hdr.opah.u.oth;
455 l4 = OPA_16B_L4_IB_LOCAL;
458 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
459 if (qp->ibqp.qp_type == IB_QPT_SMI)
464 dlid = opa_get_lid(rdma_ah_get_dlid(ah_attr), 16B);
466 slid = be32_to_cpu(OPA_LID_PERMISSIVE);
468 slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
469 ((1 << ppd->lmc) - 1));
473 pkey = hfi1_get_pkey(ibp, wqe->ud_wr.pkey_index);
474 hfi1_16B_set_qpn(&ps->s_txreq->phdr.hdr.opah.u.mgmt,
477 hfi1_make_bth_deth(qp, wqe, ohdr, &pkey, extra_bytes, true);
479 /* Convert dwords to flits */
480 len = (ps->s_txreq->hdr_dwords + nwords) >> 1;
482 /* Setup the packet */
483 ps->s_txreq->phdr.hdr.hdr_type = HFI1_PKT_TYPE_16B;
484 hfi1_make_16b_hdr(&ps->s_txreq->phdr.hdr.opah,
485 slid, dlid, len, pkey, 0, 0, l4, priv->s_sc);
489 * hfi1_make_ud_req - construct a UD request packet
492 * Assume s_lock is held.
494 * Return 1 if constructed; otherwise, return 0.
496 int hfi1_make_ud_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
498 struct hfi1_qp_priv *priv = qp->priv;
499 struct rdma_ah_attr *ah_attr;
500 struct hfi1_pportdata *ppd;
501 struct hfi1_ibport *ibp;
502 struct rvt_swqe *wqe;
506 ps->s_txreq = get_txreq(ps->dev, qp);
510 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
511 if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
513 /* We are in the error state, flush the work request. */
514 if (qp->s_last == READ_ONCE(qp->s_head))
516 /* If DMAs are in progress, we can't flush immediately. */
517 if (iowait_sdma_pending(&priv->s_iowait)) {
518 qp->s_flags |= RVT_S_WAIT_DMA;
521 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
522 rvt_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
526 /* see post_one_send() */
527 if (qp->s_cur == READ_ONCE(qp->s_head))
530 wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
531 next_cur = qp->s_cur + 1;
532 if (next_cur >= qp->s_size)
535 /* Construct the header. */
536 ibp = to_iport(qp->ibqp.device, qp->port_num);
537 ppd = ppd_from_ibp(ibp);
538 ah_attr = &ibah_to_rvtah(wqe->ud_wr.ah)->attr;
539 priv->hdr_type = hfi1_get_hdr_type(ppd->lid, ah_attr);
540 if ((!hfi1_check_mcast(rdma_ah_get_dlid(ah_attr))) ||
541 (rdma_ah_get_dlid(ah_attr) == be32_to_cpu(OPA_LID_PERMISSIVE))) {
542 lid = rdma_ah_get_dlid(ah_attr) & ~((1 << ppd->lmc) - 1);
543 if (unlikely(!loopback &&
544 ((lid == ppd->lid) ||
545 ((lid == be32_to_cpu(OPA_LID_PERMISSIVE)) &&
546 (qp->ibqp.qp_type == IB_QPT_GSI))))) {
547 unsigned long tflags = ps->flags;
549 * If DMAs are in progress, we can't generate
550 * a completion for the loopback packet since
551 * it would be out of order.
552 * Instead of waiting, we could queue a
553 * zero length descriptor so we get a callback.
555 if (iowait_sdma_pending(&priv->s_iowait)) {
556 qp->s_flags |= RVT_S_WAIT_DMA;
559 qp->s_cur = next_cur;
560 spin_unlock_irqrestore(&qp->s_lock, tflags);
561 ud_loopback(qp, wqe);
562 spin_lock_irqsave(&qp->s_lock, tflags);
564 rvt_send_complete(qp, wqe, IB_WC_SUCCESS);
569 qp->s_cur = next_cur;
570 ps->s_txreq->s_cur_size = wqe->length;
571 ps->s_txreq->ss = &qp->s_sge;
572 qp->s_srate = rdma_ah_get_static_rate(ah_attr);
573 qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
575 qp->s_sge.sge = wqe->sg_list[0];
576 qp->s_sge.sg_list = wqe->sg_list + 1;
577 qp->s_sge.num_sge = wqe->wr.num_sge;
578 qp->s_sge.total_len = wqe->length;
580 /* Make the appropriate header */
581 hfi1_make_ud_req_tbl[priv->hdr_type](qp, ps, qp->s_wqe);
582 priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
583 ps->s_txreq->sde = priv->s_sde;
584 priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
585 ps->s_txreq->psc = priv->s_sendcontext;
587 priv->s_ahg->ahgcount = 0;
588 priv->s_ahg->ahgidx = 0;
589 priv->s_ahg->tx_flags = 0;
594 hfi1_put_txreq(ps->s_txreq);
599 hfi1_put_txreq(ps->s_txreq);
603 qp->s_flags &= ~RVT_S_BUSY;
608 * Hardware can't check this so we do it here.
610 * This is a slightly different algorithm than the standard pkey check. It
611 * special cases the management keys and allows for 0x7fff and 0xffff to be in
612 * the table at the same time.
614 * @returns the index found or -1 if not found
616 int hfi1_lookup_pkey_idx(struct hfi1_ibport *ibp, u16 pkey)
618 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
621 if (pkey == FULL_MGMT_P_KEY || pkey == LIM_MGMT_P_KEY) {
622 unsigned lim_idx = -1;
624 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i) {
625 /* here we look for an exact match */
626 if (ppd->pkeys[i] == pkey)
628 if (ppd->pkeys[i] == LIM_MGMT_P_KEY)
632 /* did not find 0xffff return 0x7fff idx if found */
633 if (pkey == FULL_MGMT_P_KEY)
640 pkey &= 0x7fff; /* remove limited/full membership bit */
642 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i)
643 if ((ppd->pkeys[i] & 0x7fff) == pkey)
647 * Should not get here, this means hardware failed to validate pkeys.
652 void return_cnp_16B(struct hfi1_ibport *ibp, struct rvt_qp *qp,
653 u32 remote_qpn, u16 pkey, u32 slid, u32 dlid,
654 u8 sc5, const struct ib_grh *old_grh)
656 u64 pbc, pbc_flags = 0;
657 u32 bth0, plen, vl, hwords = 7;
660 struct hfi1_opa_header hdr;
661 struct ib_other_headers *ohdr;
662 struct pio_buf *pbuf;
663 struct send_context *ctxt = qp_to_send_context(qp, sc5);
664 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
667 hdr.hdr_type = HFI1_PKT_TYPE_16B;
668 /* Populate length */
669 nwords = ((hfi1_get_16b_padding(hwords << 2, 0) +
670 SIZE_OF_LT) >> 2) + SIZE_OF_CRC;
672 struct ib_grh *grh = &hdr.opah.u.l.grh;
674 grh->version_tclass_flow = old_grh->version_tclass_flow;
675 grh->paylen = cpu_to_be16(
676 (hwords - LRH_16B_DWORDS + nwords) << 2);
677 grh->hop_limit = 0xff;
678 grh->sgid = old_grh->dgid;
679 grh->dgid = old_grh->sgid;
680 ohdr = &hdr.opah.u.l.oth;
681 l4 = OPA_16B_L4_IB_GLOBAL;
682 hwords += sizeof(struct ib_grh) / sizeof(u32);
684 ohdr = &hdr.opah.u.oth;
685 l4 = OPA_16B_L4_IB_LOCAL;
688 /* BIT 16 to 19 is TVER. Bit 20 to 22 is pad cnt */
689 bth0 = (IB_OPCODE_CNP << 24) | (1 << 16) |
690 (hfi1_get_16b_padding(hwords << 2, 0) << 20);
691 ohdr->bth[0] = cpu_to_be32(bth0);
693 ohdr->bth[1] = cpu_to_be32(remote_qpn);
694 ohdr->bth[2] = 0; /* PSN 0 */
696 /* Convert dwords to flits */
697 len = (hwords + nwords) >> 1;
698 hfi1_make_16b_hdr(&hdr.opah, slid, dlid, len, pkey, 1, 0, l4, sc5);
700 plen = 2 /* PBC */ + hwords + nwords;
701 pbc_flags |= PBC_PACKET_BYPASS | PBC_INSERT_BYPASS_ICRC;
702 vl = sc_to_vlt(ppd->dd, sc5);
703 pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
705 pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
707 trace_pio_output_ibhdr(ppd->dd, &hdr, sc5);
708 ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
714 void return_cnp(struct hfi1_ibport *ibp, struct rvt_qp *qp, u32 remote_qpn,
715 u16 pkey, u32 slid, u32 dlid, u8 sc5,
716 const struct ib_grh *old_grh)
718 u64 pbc, pbc_flags = 0;
719 u32 bth0, plen, vl, hwords = 5;
721 u8 sl = ibp->sc_to_sl[sc5];
722 struct hfi1_opa_header hdr;
723 struct ib_other_headers *ohdr;
724 struct pio_buf *pbuf;
725 struct send_context *ctxt = qp_to_send_context(qp, sc5);
726 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
728 hdr.hdr_type = HFI1_PKT_TYPE_9B;
730 struct ib_grh *grh = &hdr.ibh.u.l.grh;
732 grh->version_tclass_flow = old_grh->version_tclass_flow;
733 grh->paylen = cpu_to_be16(
734 (hwords - LRH_9B_DWORDS + SIZE_OF_CRC) << 2);
735 grh->hop_limit = 0xff;
736 grh->sgid = old_grh->dgid;
737 grh->dgid = old_grh->sgid;
738 ohdr = &hdr.ibh.u.l.oth;
740 hwords += sizeof(struct ib_grh) / sizeof(u32);
742 ohdr = &hdr.ibh.u.oth;
746 lrh0 |= (sc5 & 0xf) << 12 | sl << 4;
748 bth0 = pkey | (IB_OPCODE_CNP << 24);
749 ohdr->bth[0] = cpu_to_be32(bth0);
751 ohdr->bth[1] = cpu_to_be32(remote_qpn | (1 << IB_BECN_SHIFT));
752 ohdr->bth[2] = 0; /* PSN 0 */
754 hfi1_make_ib_hdr(&hdr.ibh, lrh0, hwords + SIZE_OF_CRC, dlid, slid);
755 plen = 2 /* PBC */ + hwords;
756 pbc_flags |= (ib_is_sc5(sc5) << PBC_DC_INFO_SHIFT);
757 vl = sc_to_vlt(ppd->dd, sc5);
758 pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
760 pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
762 trace_pio_output_ibhdr(ppd->dd, &hdr, sc5);
763 ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
770 * opa_smp_check() - Do the regular pkey checking, and the additional
771 * checks for SMPs specified in OPAv1 rev 1.0, 9/19/2016 update, section
772 * 9.10.25 ("SMA Packet Checks").
775 * - Checks are done using the pkey directly from the packet's BTH,
776 * and specifically _not_ the pkey that we attach to the completion,
777 * which may be different.
778 * - These checks are specifically for "non-local" SMPs (i.e., SMPs
779 * which originated on another node). SMPs which are sent from, and
780 * destined to this node are checked in opa_local_smp_check().
782 * At the point where opa_smp_check() is called, we know:
783 * - destination QP is QP0
785 * opa_smp_check() returns 0 if all checks succeed, 1 otherwise.
787 static int opa_smp_check(struct hfi1_ibport *ibp, u16 pkey, u8 sc5,
788 struct rvt_qp *qp, u16 slid, struct opa_smp *smp)
790 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
793 * I don't think it's possible for us to get here with sc != 0xf,
794 * but check it to be certain.
799 if (rcv_pkey_check(ppd, pkey, sc5, slid))
803 * At this point we know (and so don't need to check again) that
804 * the pkey is either LIM_MGMT_P_KEY, or FULL_MGMT_P_KEY
805 * (see ingress_pkey_check).
807 if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE &&
808 smp->mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED) {
809 ingress_pkey_table_fail(ppd, pkey, slid);
814 * SMPs fall into one of four (disjoint) categories:
815 * SMA request, SMA response, SMA trap, or SMA trap repress.
816 * Our response depends, in part, on which type of SMP we're
819 * If this is an SMA response, skip the check here.
821 * If this is an SMA request or SMA trap repress:
822 * - pkey != FULL_MGMT_P_KEY =>
823 * increment port recv constraint errors, drop MAD
826 * - accept if the port is running an SM
827 * - drop MAD if it's an SMA trap
828 * - pkey == FULL_MGMT_P_KEY =>
829 * reply with unsupported method
830 * - pkey != FULL_MGMT_P_KEY =>
831 * increment port recv constraint errors, drop MAD
833 switch (smp->method) {
834 case IB_MGMT_METHOD_GET_RESP:
835 case IB_MGMT_METHOD_REPORT_RESP:
837 case IB_MGMT_METHOD_GET:
838 case IB_MGMT_METHOD_SET:
839 case IB_MGMT_METHOD_REPORT:
840 case IB_MGMT_METHOD_TRAP_REPRESS:
841 if (pkey != FULL_MGMT_P_KEY) {
842 ingress_pkey_table_fail(ppd, pkey, slid);
847 if (ibp->rvp.port_cap_flags & IB_PORT_SM)
849 if (smp->method == IB_MGMT_METHOD_TRAP)
851 if (pkey == FULL_MGMT_P_KEY) {
852 smp->status |= IB_SMP_UNSUP_METHOD;
855 ingress_pkey_table_fail(ppd, pkey, slid);
862 * hfi1_ud_rcv - receive an incoming UD packet
863 * @ibp: the port the packet came in on
864 * @hdr: the packet header
865 * @rcv_flags: flags relevant to rcv processing
866 * @data: the packet data
867 * @tlen: the packet length
868 * @qp: the QP the packet came on
870 * This is called from qp_rcv() to process an incoming UD packet
872 * Called at interrupt level.
874 void hfi1_ud_rcv(struct hfi1_packet *packet)
876 u32 hdrsize = packet->hlen;
880 int mgmt_pkey_idx = -1;
881 struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
882 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
883 void *data = packet->payload;
884 u32 tlen = packet->tlen;
885 struct rvt_qp *qp = packet->qp;
888 u8 opcode = packet->opcode;
890 u32 dlid = packet->dlid;
891 u32 slid = packet->slid;
894 bool dlid_is_permissive;
895 bool slid_is_permissive;
896 bool solicited = false;
898 extra_bytes = packet->pad + packet->extra_byte + (SIZE_OF_CRC << 2);
900 if (packet->etype == RHF_RCV_TYPE_BYPASS) {
902 opa_get_lid(be32_to_cpu(OPA_LID_PERMISSIVE), 16B);
904 l4 = hfi1_16B_get_l4(packet->hdr);
905 pkey = hfi1_16B_get_pkey(packet->hdr);
906 dlid_is_permissive = (dlid == permissive_lid);
907 slid_is_permissive = (slid == permissive_lid);
909 pkey = ib_bth_get_pkey(packet->ohdr);
910 dlid_is_permissive = (dlid == be16_to_cpu(IB_LID_PERMISSIVE));
911 slid_is_permissive = (slid == be16_to_cpu(IB_LID_PERMISSIVE));
913 sl_from_sc = ibp->sc_to_sl[sc5];
915 if (likely(l4 != OPA_16B_L4_FM)) {
916 src_qp = ib_get_sqpn(packet->ohdr);
917 solicited = ib_bth_is_solicited(packet->ohdr);
919 src_qp = hfi1_16B_get_src_qpn(packet->mgmt);
922 process_ecn(qp, packet);
924 * Get the number of bytes the message was padded by
925 * and drop incomplete packets.
927 if (unlikely(tlen < (hdrsize + extra_bytes)))
930 tlen -= hdrsize + extra_bytes;
933 * Check that the permissive LID is only used on QP0
934 * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
936 if (qp->ibqp.qp_num) {
937 if (unlikely(dlid_is_permissive || slid_is_permissive))
939 if (qp->ibqp.qp_num > 1) {
940 if (unlikely(rcv_pkey_check(ppd, pkey, sc5, slid))) {
942 * Traps will not be sent for packets dropped
943 * by the HW. This is fine, as sending trap
944 * for invalid pkeys is optional according to
945 * IB spec (release 1.3, section 10.9.4)
949 src_qp, qp->ibqp.qp_num,
955 mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
956 if (mgmt_pkey_idx < 0)
959 if (unlikely(l4 != OPA_16B_L4_FM &&
960 ib_get_qkey(packet->ohdr) != qp->qkey))
961 return; /* Silent drop */
963 /* Drop invalid MAD packets (see 13.5.3.1). */
964 if (unlikely(qp->ibqp.qp_num == 1 &&
965 (tlen > 2048 || (sc5 == 0xF))))
968 /* Received on QP0, and so by definition, this is an SMP */
969 struct opa_smp *smp = (struct opa_smp *)data;
971 if (opa_smp_check(ibp, pkey, sc5, qp, slid, smp))
976 if ((dlid_is_permissive || slid_is_permissive) &&
977 smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
980 /* look up SMI pkey */
981 mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
982 if (mgmt_pkey_idx < 0)
986 if (qp->ibqp.qp_num > 1 &&
987 opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
988 wc.ex.imm_data = packet->ohdr->u.ud.imm_data;
989 wc.wc_flags = IB_WC_WITH_IMM;
990 } else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
998 * A GRH is expected to precede the data even if not
999 * present on the wire.
1001 wc.byte_len = tlen + sizeof(struct ib_grh);
1004 * Get the next work request entry to find where to put the data.
1006 if (qp->r_flags & RVT_R_REUSE_SGE) {
1007 qp->r_flags &= ~RVT_R_REUSE_SGE;
1011 ret = rvt_get_rwqe(qp, false);
1013 rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
1017 if (qp->ibqp.qp_num == 0)
1018 ibp->rvp.n_vl15_dropped++;
1022 /* Silently drop packets which are too big. */
1023 if (unlikely(wc.byte_len > qp->r_len)) {
1024 qp->r_flags |= RVT_R_REUSE_SGE;
1028 rvt_copy_sge(qp, &qp->r_sge, packet->grh,
1029 sizeof(struct ib_grh), true, false);
1030 wc.wc_flags |= IB_WC_GRH;
1031 } else if (packet->etype == RHF_RCV_TYPE_BYPASS) {
1034 * Assuming we only created 16B on the send side
1035 * if we want to use large LIDs, since GRH was stripped
1036 * out when creating 16B, add back the GRH here.
1038 hfi1_make_ext_grh(packet, &grh, slid, dlid);
1039 rvt_copy_sge(qp, &qp->r_sge, &grh,
1040 sizeof(struct ib_grh), true, false);
1041 wc.wc_flags |= IB_WC_GRH;
1043 rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
1045 rvt_copy_sge(qp, &qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh),
1047 rvt_put_ss(&qp->r_sge);
1048 if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
1050 wc.wr_id = qp->r_wr_id;
1051 wc.status = IB_WC_SUCCESS;
1052 wc.opcode = IB_WC_RECV;
1057 if (qp->ibqp.qp_type == IB_QPT_GSI ||
1058 qp->ibqp.qp_type == IB_QPT_SMI) {
1059 if (mgmt_pkey_idx < 0) {
1060 if (net_ratelimit()) {
1061 struct hfi1_devdata *dd = ppd->dd;
1063 dd_dev_err(dd, "QP type %d mgmt_pkey_idx < 0 and packet not dropped???\n",
1068 wc.pkey_index = (unsigned)mgmt_pkey_idx;
1072 if (slid_is_permissive)
1073 slid = be32_to_cpu(OPA_LID_PERMISSIVE);
1074 wc.slid = slid & U16_MAX;
1078 * Save the LMC lower bits if the destination LID is a unicast LID.
1080 wc.dlid_path_bits = hfi1_check_mcast(dlid) ? 0 :
1081 dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1);
1082 wc.port_num = qp->port_num;
1083 /* Signal completion event if the solicited bit is set. */
1084 rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, solicited);
1088 ibp->rvp.n_pkt_drops++;