Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
0002 /*
0003  * Copyright(c) 2015 - 2019 Intel Corporation.
0004  */
0005 
0006 #include <linux/net.h>
0007 #include <rdma/ib_smi.h>
0008 
0009 #include "hfi.h"
0010 #include "mad.h"
0011 #include "verbs_txreq.h"
0012 #include "trace_ibhdrs.h"
0013 #include "qp.h"
0014 
0015 /* We support only two types - 9B and 16B for now */
0016 static const hfi1_make_req hfi1_make_ud_req_tbl[2] = {
0017     [HFI1_PKT_TYPE_9B] = &hfi1_make_ud_req_9B,
0018     [HFI1_PKT_TYPE_16B] = &hfi1_make_ud_req_16B
0019 };
0020 
0021 /**
0022  * ud_loopback - handle send on loopback QPs
0023  * @sqp: the sending QP
0024  * @swqe: the send work request
0025  *
0026  * This is called from hfi1_make_ud_req() to forward a WQE addressed
0027  * to the same HFI.
0028  * Note that the receive interrupt handler may be calling hfi1_ud_rcv()
0029  * while this is being called.
0030  */
0031 static void ud_loopback(struct rvt_qp *sqp, struct rvt_swqe *swqe)
0032 {
0033     struct hfi1_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
0034     struct hfi1_pportdata *ppd;
0035     struct hfi1_qp_priv *priv = sqp->priv;
0036     struct rvt_qp *qp;
0037     struct rdma_ah_attr *ah_attr;
0038     unsigned long flags;
0039     struct rvt_sge_state ssge;
0040     struct rvt_sge *sge;
0041     struct ib_wc wc;
0042     u32 length;
0043     enum ib_qp_type sqptype, dqptype;
0044 
0045     rcu_read_lock();
0046 
0047     qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
0048                 rvt_get_swqe_remote_qpn(swqe));
0049     if (!qp) {
0050         ibp->rvp.n_pkt_drops++;
0051         rcu_read_unlock();
0052         return;
0053     }
0054 
0055     sqptype = sqp->ibqp.qp_type == IB_QPT_GSI ?
0056             IB_QPT_UD : sqp->ibqp.qp_type;
0057     dqptype = qp->ibqp.qp_type == IB_QPT_GSI ?
0058             IB_QPT_UD : qp->ibqp.qp_type;
0059 
0060     if (dqptype != sqptype ||
0061         !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
0062         ibp->rvp.n_pkt_drops++;
0063         goto drop;
0064     }
0065 
0066     ah_attr = rvt_get_swqe_ah_attr(swqe);
0067     ppd = ppd_from_ibp(ibp);
0068 
0069     if (qp->ibqp.qp_num > 1) {
0070         u16 pkey;
0071         u32 slid;
0072         u8 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
0073 
0074         pkey = hfi1_get_pkey(ibp, sqp->s_pkey_index);
0075         slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
0076                    ((1 << ppd->lmc) - 1));
0077         if (unlikely(ingress_pkey_check(ppd, pkey, sc5,
0078                         qp->s_pkey_index,
0079                         slid, false))) {
0080             hfi1_bad_pkey(ibp, pkey,
0081                       rdma_ah_get_sl(ah_attr),
0082                       sqp->ibqp.qp_num, qp->ibqp.qp_num,
0083                       slid, rdma_ah_get_dlid(ah_attr));
0084             goto drop;
0085         }
0086     }
0087 
0088     /*
0089      * Check that the qkey matches (except for QP0, see 9.6.1.4.1).
0090      * Qkeys with the high order bit set mean use the
0091      * qkey from the QP context instead of the WR (see 10.2.5).
0092      */
0093     if (qp->ibqp.qp_num) {
0094         u32 qkey;
0095 
0096         qkey = (int)rvt_get_swqe_remote_qkey(swqe) < 0 ?
0097             sqp->qkey : rvt_get_swqe_remote_qkey(swqe);
0098         if (unlikely(qkey != qp->qkey))
0099             goto drop; /* silently drop per IBTA spec */
0100     }
0101 
0102     /*
0103      * A GRH is expected to precede the data even if not
0104      * present on the wire.
0105      */
0106     length = swqe->length;
0107     memset(&wc, 0, sizeof(wc));
0108     wc.byte_len = length + sizeof(struct ib_grh);
0109 
0110     if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
0111         wc.wc_flags = IB_WC_WITH_IMM;
0112         wc.ex.imm_data = swqe->wr.ex.imm_data;
0113     }
0114 
0115     spin_lock_irqsave(&qp->r_lock, flags);
0116 
0117     /*
0118      * Get the next work request entry to find where to put the data.
0119      */
0120     if (qp->r_flags & RVT_R_REUSE_SGE) {
0121         qp->r_flags &= ~RVT_R_REUSE_SGE;
0122     } else {
0123         int ret;
0124 
0125         ret = rvt_get_rwqe(qp, false);
0126         if (ret < 0) {
0127             rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
0128             goto bail_unlock;
0129         }
0130         if (!ret) {
0131             if (qp->ibqp.qp_num == 0)
0132                 ibp->rvp.n_vl15_dropped++;
0133             goto bail_unlock;
0134         }
0135     }
0136     /* Silently drop packets which are too big. */
0137     if (unlikely(wc.byte_len > qp->r_len)) {
0138         qp->r_flags |= RVT_R_REUSE_SGE;
0139         ibp->rvp.n_pkt_drops++;
0140         goto bail_unlock;
0141     }
0142 
0143     if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
0144         struct ib_grh grh;
0145         struct ib_global_route grd = *(rdma_ah_read_grh(ah_attr));
0146 
0147         /*
0148          * For loopback packets with extended LIDs, the
0149          * sgid_index in the GRH is 0 and the dgid is
0150          * OPA GID of the sender. While creating a response
0151          * to the loopback packet, IB core creates the new
0152          * sgid_index from the DGID and that will be the
0153          * OPA_GID_INDEX. The new dgid is from the sgid
0154          * index and that will be in the IB GID format.
0155          *
0156          * We now have a case where the sent packet had a
0157          * different sgid_index and dgid compared to the
0158          * one that was received in response.
0159          *
0160          * Fix this inconsistency.
0161          */
0162         if (priv->hdr_type == HFI1_PKT_TYPE_16B) {
0163             if (grd.sgid_index == 0)
0164                 grd.sgid_index = OPA_GID_INDEX;
0165 
0166             if (ib_is_opa_gid(&grd.dgid))
0167                 grd.dgid.global.interface_id =
0168                 cpu_to_be64(ppd->guids[HFI1_PORT_GUID_INDEX]);
0169         }
0170 
0171         hfi1_make_grh(ibp, &grh, &grd, 0, 0);
0172         rvt_copy_sge(qp, &qp->r_sge, &grh,
0173                  sizeof(grh), true, false);
0174         wc.wc_flags |= IB_WC_GRH;
0175     } else {
0176         rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
0177     }
0178     ssge.sg_list = swqe->sg_list + 1;
0179     ssge.sge = *swqe->sg_list;
0180     ssge.num_sge = swqe->wr.num_sge;
0181     sge = &ssge.sge;
0182     while (length) {
0183         u32 len = rvt_get_sge_length(sge, length);
0184 
0185         WARN_ON_ONCE(len == 0);
0186         rvt_copy_sge(qp, &qp->r_sge, sge->vaddr, len, true, false);
0187         rvt_update_sge(&ssge, len, false);
0188         length -= len;
0189     }
0190     rvt_put_ss(&qp->r_sge);
0191     if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
0192         goto bail_unlock;
0193     wc.wr_id = qp->r_wr_id;
0194     wc.status = IB_WC_SUCCESS;
0195     wc.opcode = IB_WC_RECV;
0196     wc.qp = &qp->ibqp;
0197     wc.src_qp = sqp->ibqp.qp_num;
0198     if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI) {
0199         if (sqp->ibqp.qp_type == IB_QPT_GSI ||
0200             sqp->ibqp.qp_type == IB_QPT_SMI)
0201             wc.pkey_index = rvt_get_swqe_pkey_index(swqe);
0202         else
0203             wc.pkey_index = sqp->s_pkey_index;
0204     } else {
0205         wc.pkey_index = 0;
0206     }
0207     wc.slid = (ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
0208                    ((1 << ppd->lmc) - 1))) & U16_MAX;
0209     /* Check for loopback when the port lid is not set */
0210     if (wc.slid == 0 && sqp->ibqp.qp_type == IB_QPT_GSI)
0211         wc.slid = be16_to_cpu(IB_LID_PERMISSIVE);
0212     wc.sl = rdma_ah_get_sl(ah_attr);
0213     wc.dlid_path_bits = rdma_ah_get_dlid(ah_attr) & ((1 << ppd->lmc) - 1);
0214     wc.port_num = qp->port_num;
0215     /* Signal completion event if the solicited bit is set. */
0216     rvt_recv_cq(qp, &wc, swqe->wr.send_flags & IB_SEND_SOLICITED);
0217     ibp->rvp.n_loop_pkts++;
0218 bail_unlock:
0219     spin_unlock_irqrestore(&qp->r_lock, flags);
0220 drop:
0221     rcu_read_unlock();
0222 }
0223 
0224 static void hfi1_make_bth_deth(struct rvt_qp *qp, struct rvt_swqe *wqe,
0225                    struct ib_other_headers *ohdr,
0226                    u16 *pkey, u32 extra_bytes, bool bypass)
0227 {
0228     u32 bth0;
0229     struct hfi1_ibport *ibp;
0230 
0231     ibp = to_iport(qp->ibqp.device, qp->port_num);
0232     if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
0233         ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;
0234         bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
0235     } else {
0236         bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
0237     }
0238 
0239     if (wqe->wr.send_flags & IB_SEND_SOLICITED)
0240         bth0 |= IB_BTH_SOLICITED;
0241     bth0 |= extra_bytes << 20;
0242     if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI)
0243         *pkey = hfi1_get_pkey(ibp, rvt_get_swqe_pkey_index(wqe));
0244     else
0245         *pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
0246     if (!bypass)
0247         bth0 |= *pkey;
0248     ohdr->bth[0] = cpu_to_be32(bth0);
0249     ohdr->bth[1] = cpu_to_be32(rvt_get_swqe_remote_qpn(wqe));
0250     ohdr->bth[2] = cpu_to_be32(mask_psn(wqe->psn));
0251     /*
0252      * Qkeys with the high order bit set mean use the
0253      * qkey from the QP context instead of the WR (see 10.2.5).
0254      */
0255     ohdr->u.ud.deth[0] =
0256         cpu_to_be32((int)rvt_get_swqe_remote_qkey(wqe) < 0 ? qp->qkey :
0257                 rvt_get_swqe_remote_qkey(wqe));
0258     ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
0259 }
0260 
0261 void hfi1_make_ud_req_9B(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
0262              struct rvt_swqe *wqe)
0263 {
0264     u32 nwords, extra_bytes;
0265     u16 len, slid, dlid, pkey;
0266     u16 lrh0 = 0;
0267     u8 sc5;
0268     struct hfi1_qp_priv *priv = qp->priv;
0269     struct ib_other_headers *ohdr;
0270     struct rdma_ah_attr *ah_attr;
0271     struct hfi1_pportdata *ppd;
0272     struct hfi1_ibport *ibp;
0273     struct ib_grh *grh;
0274 
0275     ibp = to_iport(qp->ibqp.device, qp->port_num);
0276     ppd = ppd_from_ibp(ibp);
0277     ah_attr = rvt_get_swqe_ah_attr(wqe);
0278 
0279     extra_bytes = -wqe->length & 3;
0280     nwords = ((wqe->length + extra_bytes) >> 2) + SIZE_OF_CRC;
0281     /* header size in dwords LRH+BTH+DETH = (8+12+8)/4. */
0282     ps->s_txreq->hdr_dwords = 7;
0283     if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
0284         ps->s_txreq->hdr_dwords++;
0285 
0286     if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
0287         grh = &ps->s_txreq->phdr.hdr.ibh.u.l.grh;
0288         ps->s_txreq->hdr_dwords +=
0289             hfi1_make_grh(ibp, grh, rdma_ah_read_grh(ah_attr),
0290                       ps->s_txreq->hdr_dwords - LRH_9B_DWORDS,
0291                       nwords);
0292         lrh0 = HFI1_LRH_GRH;
0293         ohdr = &ps->s_txreq->phdr.hdr.ibh.u.l.oth;
0294     } else {
0295         lrh0 = HFI1_LRH_BTH;
0296         ohdr = &ps->s_txreq->phdr.hdr.ibh.u.oth;
0297     }
0298 
0299     sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
0300     lrh0 |= (rdma_ah_get_sl(ah_attr) & 0xf) << 4;
0301     if (qp->ibqp.qp_type == IB_QPT_SMI) {
0302         lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
0303         priv->s_sc = 0xf;
0304     } else {
0305         lrh0 |= (sc5 & 0xf) << 12;
0306         priv->s_sc = sc5;
0307     }
0308 
0309     dlid = opa_get_lid(rdma_ah_get_dlid(ah_attr), 9B);
0310     if (dlid == be16_to_cpu(IB_LID_PERMISSIVE)) {
0311         slid = be16_to_cpu(IB_LID_PERMISSIVE);
0312     } else {
0313         u16 lid = (u16)ppd->lid;
0314 
0315         if (lid) {
0316             lid |= rdma_ah_get_path_bits(ah_attr) &
0317                 ((1 << ppd->lmc) - 1);
0318             slid = lid;
0319         } else {
0320             slid = be16_to_cpu(IB_LID_PERMISSIVE);
0321         }
0322     }
0323     hfi1_make_bth_deth(qp, wqe, ohdr, &pkey, extra_bytes, false);
0324     len = ps->s_txreq->hdr_dwords + nwords;
0325 
0326     /* Setup the packet */
0327     ps->s_txreq->phdr.hdr.hdr_type = HFI1_PKT_TYPE_9B;
0328     hfi1_make_ib_hdr(&ps->s_txreq->phdr.hdr.ibh,
0329              lrh0, len, dlid, slid);
0330 }
0331 
0332 void hfi1_make_ud_req_16B(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
0333               struct rvt_swqe *wqe)
0334 {
0335     struct hfi1_qp_priv *priv = qp->priv;
0336     struct ib_other_headers *ohdr;
0337     struct rdma_ah_attr *ah_attr;
0338     struct hfi1_pportdata *ppd;
0339     struct hfi1_ibport *ibp;
0340     u32 dlid, slid, nwords, extra_bytes;
0341     u32 dest_qp = rvt_get_swqe_remote_qpn(wqe);
0342     u32 src_qp = qp->ibqp.qp_num;
0343     u16 len, pkey;
0344     u8 l4, sc5;
0345     bool is_mgmt = false;
0346 
0347     ibp = to_iport(qp->ibqp.device, qp->port_num);
0348     ppd = ppd_from_ibp(ibp);
0349     ah_attr = rvt_get_swqe_ah_attr(wqe);
0350 
0351     /*
0352      * Build 16B Management Packet if either the destination
0353      * or source queue pair number is 0 or 1.
0354      */
0355     if (dest_qp == 0 || src_qp == 0 || dest_qp == 1 || src_qp == 1) {
0356         /* header size in dwords 16B LRH+L4_FM = (16+8)/4. */
0357         ps->s_txreq->hdr_dwords = 6;
0358         is_mgmt = true;
0359     } else {
0360         /* header size in dwords 16B LRH+BTH+DETH = (16+12+8)/4. */
0361         ps->s_txreq->hdr_dwords = 9;
0362         if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
0363             ps->s_txreq->hdr_dwords++;
0364     }
0365 
0366     /* SW provides space for CRC and LT for bypass packets. */
0367     extra_bytes = hfi1_get_16b_padding((ps->s_txreq->hdr_dwords << 2),
0368                        wqe->length);
0369     nwords = ((wqe->length + extra_bytes + SIZE_OF_LT) >> 2) + SIZE_OF_CRC;
0370 
0371     if ((rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) &&
0372         hfi1_check_mcast(rdma_ah_get_dlid(ah_attr))) {
0373         struct ib_grh *grh;
0374         struct ib_global_route *grd = rdma_ah_retrieve_grh(ah_attr);
0375         /*
0376          * Ensure OPA GIDs are transformed to IB gids
0377          * before creating the GRH.
0378          */
0379         if (grd->sgid_index == OPA_GID_INDEX) {
0380             dd_dev_warn(ppd->dd, "Bad sgid_index. sgid_index: %d\n",
0381                     grd->sgid_index);
0382             grd->sgid_index = 0;
0383         }
0384         grh = &ps->s_txreq->phdr.hdr.opah.u.l.grh;
0385         ps->s_txreq->hdr_dwords += hfi1_make_grh(
0386             ibp, grh, grd,
0387             ps->s_txreq->hdr_dwords - LRH_16B_DWORDS,
0388             nwords);
0389         ohdr = &ps->s_txreq->phdr.hdr.opah.u.l.oth;
0390         l4 = OPA_16B_L4_IB_GLOBAL;
0391     } else {
0392         ohdr = &ps->s_txreq->phdr.hdr.opah.u.oth;
0393         l4 = OPA_16B_L4_IB_LOCAL;
0394     }
0395 
0396     sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
0397     if (qp->ibqp.qp_type == IB_QPT_SMI)
0398         priv->s_sc = 0xf;
0399     else
0400         priv->s_sc = sc5;
0401 
0402     dlid = opa_get_lid(rdma_ah_get_dlid(ah_attr), 16B);
0403     if (!ppd->lid)
0404         slid = be32_to_cpu(OPA_LID_PERMISSIVE);
0405     else
0406         slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
0407                ((1 << ppd->lmc) - 1));
0408 
0409     if (is_mgmt) {
0410         l4 = OPA_16B_L4_FM;
0411         pkey = hfi1_get_pkey(ibp, rvt_get_swqe_pkey_index(wqe));
0412         hfi1_16B_set_qpn(&ps->s_txreq->phdr.hdr.opah.u.mgmt,
0413                  dest_qp, src_qp);
0414     } else {
0415         hfi1_make_bth_deth(qp, wqe, ohdr, &pkey, extra_bytes, true);
0416     }
0417     /* Convert dwords to flits */
0418     len = (ps->s_txreq->hdr_dwords + nwords) >> 1;
0419 
0420     /* Setup the packet */
0421     ps->s_txreq->phdr.hdr.hdr_type = HFI1_PKT_TYPE_16B;
0422     hfi1_make_16b_hdr(&ps->s_txreq->phdr.hdr.opah,
0423               slid, dlid, len, pkey, 0, 0, l4, priv->s_sc);
0424 }
0425 
0426 /**
0427  * hfi1_make_ud_req - construct a UD request packet
0428  * @qp: the QP
0429  * @ps: the current packet state
0430  *
0431  * Assume s_lock is held.
0432  *
0433  * Return 1 if constructed; otherwise, return 0.
0434  */
0435 int hfi1_make_ud_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
0436 {
0437     struct hfi1_qp_priv *priv = qp->priv;
0438     struct rdma_ah_attr *ah_attr;
0439     struct hfi1_pportdata *ppd;
0440     struct hfi1_ibport *ibp;
0441     struct rvt_swqe *wqe;
0442     int next_cur;
0443     u32 lid;
0444 
0445     ps->s_txreq = get_txreq(ps->dev, qp);
0446     if (!ps->s_txreq)
0447         goto bail_no_tx;
0448 
0449     if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
0450         if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
0451             goto bail;
0452         /* We are in the error state, flush the work request. */
0453         if (qp->s_last == READ_ONCE(qp->s_head))
0454             goto bail;
0455         /* If DMAs are in progress, we can't flush immediately. */
0456         if (iowait_sdma_pending(&priv->s_iowait)) {
0457             qp->s_flags |= RVT_S_WAIT_DMA;
0458             goto bail;
0459         }
0460         wqe = rvt_get_swqe_ptr(qp, qp->s_last);
0461         rvt_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
0462         goto done_free_tx;
0463     }
0464 
0465     /* see post_one_send() */
0466     if (qp->s_cur == READ_ONCE(qp->s_head))
0467         goto bail;
0468 
0469     wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
0470     next_cur = qp->s_cur + 1;
0471     if (next_cur >= qp->s_size)
0472         next_cur = 0;
0473 
0474     /* Construct the header. */
0475     ibp = to_iport(qp->ibqp.device, qp->port_num);
0476     ppd = ppd_from_ibp(ibp);
0477     ah_attr = rvt_get_swqe_ah_attr(wqe);
0478     priv->hdr_type = hfi1_get_hdr_type(ppd->lid, ah_attr);
0479     if ((!hfi1_check_mcast(rdma_ah_get_dlid(ah_attr))) ||
0480         (rdma_ah_get_dlid(ah_attr) == be32_to_cpu(OPA_LID_PERMISSIVE))) {
0481         lid = rdma_ah_get_dlid(ah_attr) & ~((1 << ppd->lmc) - 1);
0482         if (unlikely(!loopback &&
0483                  ((lid == ppd->lid) ||
0484                   ((lid == be32_to_cpu(OPA_LID_PERMISSIVE)) &&
0485                    (qp->ibqp.qp_type == IB_QPT_GSI))))) {
0486             unsigned long tflags = ps->flags;
0487             /*
0488              * If DMAs are in progress, we can't generate
0489              * a completion for the loopback packet since
0490              * it would be out of order.
0491              * Instead of waiting, we could queue a
0492              * zero length descriptor so we get a callback.
0493              */
0494             if (iowait_sdma_pending(&priv->s_iowait)) {
0495                 qp->s_flags |= RVT_S_WAIT_DMA;
0496                 goto bail;
0497             }
0498             qp->s_cur = next_cur;
0499             spin_unlock_irqrestore(&qp->s_lock, tflags);
0500             ud_loopback(qp, wqe);
0501             spin_lock_irqsave(&qp->s_lock, tflags);
0502             ps->flags = tflags;
0503             rvt_send_complete(qp, wqe, IB_WC_SUCCESS);
0504             goto done_free_tx;
0505         }
0506     }
0507 
0508     qp->s_cur = next_cur;
0509     ps->s_txreq->s_cur_size = wqe->length;
0510     ps->s_txreq->ss = &qp->s_sge;
0511     qp->s_srate = rdma_ah_get_static_rate(ah_attr);
0512     qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
0513     qp->s_wqe = wqe;
0514     qp->s_sge.sge = wqe->sg_list[0];
0515     qp->s_sge.sg_list = wqe->sg_list + 1;
0516     qp->s_sge.num_sge = wqe->wr.num_sge;
0517     qp->s_sge.total_len = wqe->length;
0518 
0519     /* Make the appropriate header */
0520     hfi1_make_ud_req_tbl[priv->hdr_type](qp, ps, qp->s_wqe);
0521     priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
0522     ps->s_txreq->sde = priv->s_sde;
0523     priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
0524     ps->s_txreq->psc = priv->s_sendcontext;
0525     /* disarm any ahg */
0526     priv->s_ahg->ahgcount = 0;
0527     priv->s_ahg->ahgidx = 0;
0528     priv->s_ahg->tx_flags = 0;
0529 
0530     return 1;
0531 
0532 done_free_tx:
0533     hfi1_put_txreq(ps->s_txreq);
0534     ps->s_txreq = NULL;
0535     return 1;
0536 
0537 bail:
0538     hfi1_put_txreq(ps->s_txreq);
0539 
0540 bail_no_tx:
0541     ps->s_txreq = NULL;
0542     qp->s_flags &= ~RVT_S_BUSY;
0543     return 0;
0544 }
0545 
0546 /*
0547  * Hardware can't check this so we do it here.
0548  *
0549  * This is a slightly different algorithm than the standard pkey check.  It
0550  * special cases the management keys and allows for 0x7fff and 0xffff to be in
0551  * the table at the same time.
0552  *
0553  * @returns the index found or -1 if not found
0554  */
0555 int hfi1_lookup_pkey_idx(struct hfi1_ibport *ibp, u16 pkey)
0556 {
0557     struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
0558     unsigned i;
0559 
0560     if (pkey == FULL_MGMT_P_KEY || pkey == LIM_MGMT_P_KEY) {
0561         unsigned lim_idx = -1;
0562 
0563         for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i) {
0564             /* here we look for an exact match */
0565             if (ppd->pkeys[i] == pkey)
0566                 return i;
0567             if (ppd->pkeys[i] == LIM_MGMT_P_KEY)
0568                 lim_idx = i;
0569         }
0570 
0571         /* did not find 0xffff return 0x7fff idx if found */
0572         if (pkey == FULL_MGMT_P_KEY)
0573             return lim_idx;
0574 
0575         /* no match...  */
0576         return -1;
0577     }
0578 
0579     pkey &= 0x7fff; /* remove limited/full membership bit */
0580 
0581     for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i)
0582         if ((ppd->pkeys[i] & 0x7fff) == pkey)
0583             return i;
0584 
0585     /*
0586      * Should not get here, this means hardware failed to validate pkeys.
0587      */
0588     return -1;
0589 }
0590 
0591 void return_cnp_16B(struct hfi1_ibport *ibp, struct rvt_qp *qp,
0592             u32 remote_qpn, u16 pkey, u32 slid, u32 dlid,
0593             u8 sc5, const struct ib_grh *old_grh)
0594 {
0595     u64 pbc, pbc_flags = 0;
0596     u32 bth0, plen, vl, hwords = 7;
0597     u16 len;
0598     u8 l4;
0599     struct hfi1_opa_header hdr;
0600     struct ib_other_headers *ohdr;
0601     struct pio_buf *pbuf;
0602     struct send_context *ctxt = qp_to_send_context(qp, sc5);
0603     struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
0604     u32 nwords;
0605 
0606     hdr.hdr_type = HFI1_PKT_TYPE_16B;
0607     /* Populate length */
0608     nwords = ((hfi1_get_16b_padding(hwords << 2, 0) +
0609            SIZE_OF_LT) >> 2) + SIZE_OF_CRC;
0610     if (old_grh) {
0611         struct ib_grh *grh = &hdr.opah.u.l.grh;
0612 
0613         grh->version_tclass_flow = old_grh->version_tclass_flow;
0614         grh->paylen = cpu_to_be16(
0615             (hwords - LRH_16B_DWORDS + nwords) << 2);
0616         grh->hop_limit = 0xff;
0617         grh->sgid = old_grh->dgid;
0618         grh->dgid = old_grh->sgid;
0619         ohdr = &hdr.opah.u.l.oth;
0620         l4 = OPA_16B_L4_IB_GLOBAL;
0621         hwords += sizeof(struct ib_grh) / sizeof(u32);
0622     } else {
0623         ohdr = &hdr.opah.u.oth;
0624         l4 = OPA_16B_L4_IB_LOCAL;
0625     }
0626 
0627     /* BIT 16 to 19 is TVER. Bit 20 to 22 is pad cnt */
0628     bth0 = (IB_OPCODE_CNP << 24) | (1 << 16) |
0629            (hfi1_get_16b_padding(hwords << 2, 0) << 20);
0630     ohdr->bth[0] = cpu_to_be32(bth0);
0631 
0632     ohdr->bth[1] = cpu_to_be32(remote_qpn);
0633     ohdr->bth[2] = 0; /* PSN 0 */
0634 
0635     /* Convert dwords to flits */
0636     len = (hwords + nwords) >> 1;
0637     hfi1_make_16b_hdr(&hdr.opah, slid, dlid, len, pkey, 1, 0, l4, sc5);
0638 
0639     plen = 2 /* PBC */ + hwords + nwords;
0640     pbc_flags |= PBC_PACKET_BYPASS | PBC_INSERT_BYPASS_ICRC;
0641     vl = sc_to_vlt(ppd->dd, sc5);
0642     pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
0643     if (ctxt) {
0644         pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
0645         if (!IS_ERR_OR_NULL(pbuf)) {
0646             trace_pio_output_ibhdr(ppd->dd, &hdr, sc5);
0647             ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
0648                          &hdr, hwords);
0649         }
0650     }
0651 }
0652 
0653 void return_cnp(struct hfi1_ibport *ibp, struct rvt_qp *qp, u32 remote_qpn,
0654         u16 pkey, u32 slid, u32 dlid, u8 sc5,
0655         const struct ib_grh *old_grh)
0656 {
0657     u64 pbc, pbc_flags = 0;
0658     u32 bth0, plen, vl, hwords = 5;
0659     u16 lrh0;
0660     u8 sl = ibp->sc_to_sl[sc5];
0661     struct hfi1_opa_header hdr;
0662     struct ib_other_headers *ohdr;
0663     struct pio_buf *pbuf;
0664     struct send_context *ctxt = qp_to_send_context(qp, sc5);
0665     struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
0666 
0667     hdr.hdr_type = HFI1_PKT_TYPE_9B;
0668     if (old_grh) {
0669         struct ib_grh *grh = &hdr.ibh.u.l.grh;
0670 
0671         grh->version_tclass_flow = old_grh->version_tclass_flow;
0672         grh->paylen = cpu_to_be16(
0673             (hwords - LRH_9B_DWORDS + SIZE_OF_CRC) << 2);
0674         grh->hop_limit = 0xff;
0675         grh->sgid = old_grh->dgid;
0676         grh->dgid = old_grh->sgid;
0677         ohdr = &hdr.ibh.u.l.oth;
0678         lrh0 = HFI1_LRH_GRH;
0679         hwords += sizeof(struct ib_grh) / sizeof(u32);
0680     } else {
0681         ohdr = &hdr.ibh.u.oth;
0682         lrh0 = HFI1_LRH_BTH;
0683     }
0684 
0685     lrh0 |= (sc5 & 0xf) << 12 | sl << 4;
0686 
0687     bth0 = pkey | (IB_OPCODE_CNP << 24);
0688     ohdr->bth[0] = cpu_to_be32(bth0);
0689 
0690     ohdr->bth[1] = cpu_to_be32(remote_qpn | (1 << IB_BECN_SHIFT));
0691     ohdr->bth[2] = 0; /* PSN 0 */
0692 
0693     hfi1_make_ib_hdr(&hdr.ibh, lrh0, hwords + SIZE_OF_CRC, dlid, slid);
0694     plen = 2 /* PBC */ + hwords;
0695     pbc_flags |= (ib_is_sc5(sc5) << PBC_DC_INFO_SHIFT);
0696     vl = sc_to_vlt(ppd->dd, sc5);
0697     pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
0698     if (ctxt) {
0699         pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
0700         if (!IS_ERR_OR_NULL(pbuf)) {
0701             trace_pio_output_ibhdr(ppd->dd, &hdr, sc5);
0702             ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
0703                          &hdr, hwords);
0704         }
0705     }
0706 }
0707 
0708 /*
0709  * opa_smp_check() - Do the regular pkey checking, and the additional
0710  * checks for SMPs specified in OPAv1 rev 1.0, 9/19/2016 update, section
0711  * 9.10.25 ("SMA Packet Checks").
0712  *
0713  * Note that:
0714  *   - Checks are done using the pkey directly from the packet's BTH,
0715  *     and specifically _not_ the pkey that we attach to the completion,
0716  *     which may be different.
0717  *   - These checks are specifically for "non-local" SMPs (i.e., SMPs
0718  *     which originated on another node). SMPs which are sent from, and
0719  *     destined to this node are checked in opa_local_smp_check().
0720  *
0721  * At the point where opa_smp_check() is called, we know:
0722  *   - destination QP is QP0
0723  *
0724  * opa_smp_check() returns 0 if all checks succeed, 1 otherwise.
0725  */
0726 static int opa_smp_check(struct hfi1_ibport *ibp, u16 pkey, u8 sc5,
0727              struct rvt_qp *qp, u16 slid, struct opa_smp *smp)
0728 {
0729     struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
0730 
0731     /*
0732      * I don't think it's possible for us to get here with sc != 0xf,
0733      * but check it to be certain.
0734      */
0735     if (sc5 != 0xf)
0736         return 1;
0737 
0738     if (rcv_pkey_check(ppd, pkey, sc5, slid))
0739         return 1;
0740 
0741     /*
0742      * At this point we know (and so don't need to check again) that
0743      * the pkey is either LIM_MGMT_P_KEY, or FULL_MGMT_P_KEY
0744      * (see ingress_pkey_check).
0745      */
0746     if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE &&
0747         smp->mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED) {
0748         ingress_pkey_table_fail(ppd, pkey, slid);
0749         return 1;
0750     }
0751 
0752     /*
0753      * SMPs fall into one of four (disjoint) categories:
0754      * SMA request, SMA response, SMA trap, or SMA trap repress.
0755      * Our response depends, in part, on which type of SMP we're
0756      * processing.
0757      *
0758      * If this is an SMA response, skip the check here.
0759      *
0760      * If this is an SMA request or SMA trap repress:
0761      *   - pkey != FULL_MGMT_P_KEY =>
0762      *       increment port recv constraint errors, drop MAD
0763      *
0764      * Otherwise:
0765      *    - accept if the port is running an SM
0766      *    - drop MAD if it's an SMA trap
0767      *    - pkey == FULL_MGMT_P_KEY =>
0768      *        reply with unsupported method
0769      *    - pkey != FULL_MGMT_P_KEY =>
0770      *    increment port recv constraint errors, drop MAD
0771      */
0772     switch (smp->method) {
0773     case IB_MGMT_METHOD_GET_RESP:
0774     case IB_MGMT_METHOD_REPORT_RESP:
0775         break;
0776     case IB_MGMT_METHOD_GET:
0777     case IB_MGMT_METHOD_SET:
0778     case IB_MGMT_METHOD_REPORT:
0779     case IB_MGMT_METHOD_TRAP_REPRESS:
0780         if (pkey != FULL_MGMT_P_KEY) {
0781             ingress_pkey_table_fail(ppd, pkey, slid);
0782             return 1;
0783         }
0784         break;
0785     default:
0786         if (ibp->rvp.port_cap_flags & IB_PORT_SM)
0787             return 0;
0788         if (smp->method == IB_MGMT_METHOD_TRAP)
0789             return 1;
0790         if (pkey == FULL_MGMT_P_KEY) {
0791             smp->status |= IB_SMP_UNSUP_METHOD;
0792             return 0;
0793         }
0794         ingress_pkey_table_fail(ppd, pkey, slid);
0795         return 1;
0796     }
0797     return 0;
0798 }
0799 
0800 /**
0801  * hfi1_ud_rcv - receive an incoming UD packet
0802  * @packet: the packet structure
0803  *
0804  * This is called from qp_rcv() to process an incoming UD packet
0805  * for the given QP.
0806  * Called at interrupt level.
0807  */
0808 void hfi1_ud_rcv(struct hfi1_packet *packet)
0809 {
0810     u32 hdrsize = packet->hlen;
0811     struct ib_wc wc;
0812     u32 src_qp;
0813     u16 pkey;
0814     int mgmt_pkey_idx = -1;
0815     struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
0816     struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
0817     void *data = packet->payload;
0818     u32 tlen = packet->tlen;
0819     struct rvt_qp *qp = packet->qp;
0820     u8 sc5 = packet->sc;
0821     u8 sl_from_sc;
0822     u8 opcode = packet->opcode;
0823     u8 sl = packet->sl;
0824     u32 dlid = packet->dlid;
0825     u32 slid = packet->slid;
0826     u8 extra_bytes;
0827     u8 l4 = 0;
0828     bool dlid_is_permissive;
0829     bool slid_is_permissive;
0830     bool solicited = false;
0831 
0832     extra_bytes = packet->pad + packet->extra_byte + (SIZE_OF_CRC << 2);
0833 
0834     if (packet->etype == RHF_RCV_TYPE_BYPASS) {
0835         u32 permissive_lid =
0836             opa_get_lid(be32_to_cpu(OPA_LID_PERMISSIVE), 16B);
0837 
0838         l4 = hfi1_16B_get_l4(packet->hdr);
0839         pkey = hfi1_16B_get_pkey(packet->hdr);
0840         dlid_is_permissive = (dlid == permissive_lid);
0841         slid_is_permissive = (slid == permissive_lid);
0842     } else {
0843         pkey = ib_bth_get_pkey(packet->ohdr);
0844         dlid_is_permissive = (dlid == be16_to_cpu(IB_LID_PERMISSIVE));
0845         slid_is_permissive = (slid == be16_to_cpu(IB_LID_PERMISSIVE));
0846     }
0847     sl_from_sc = ibp->sc_to_sl[sc5];
0848 
0849     if (likely(l4 != OPA_16B_L4_FM)) {
0850         src_qp = ib_get_sqpn(packet->ohdr);
0851         solicited = ib_bth_is_solicited(packet->ohdr);
0852     } else {
0853         src_qp = hfi1_16B_get_src_qpn(packet->mgmt);
0854     }
0855 
0856     process_ecn(qp, packet);
0857     /*
0858      * Get the number of bytes the message was padded by
0859      * and drop incomplete packets.
0860      */
0861     if (unlikely(tlen < (hdrsize + extra_bytes)))
0862         goto drop;
0863 
0864     tlen -= hdrsize + extra_bytes;
0865 
0866     /*
0867      * Check that the permissive LID is only used on QP0
0868      * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
0869      */
0870     if (qp->ibqp.qp_num) {
0871         if (unlikely(dlid_is_permissive || slid_is_permissive))
0872             goto drop;
0873         if (qp->ibqp.qp_num > 1) {
0874             if (unlikely(rcv_pkey_check(ppd, pkey, sc5, slid))) {
0875                 /*
0876                  * Traps will not be sent for packets dropped
0877                  * by the HW. This is fine, as sending trap
0878                  * for invalid pkeys is optional according to
0879                  * IB spec (release 1.3, section 10.9.4)
0880                  */
0881                 hfi1_bad_pkey(ibp,
0882                           pkey, sl,
0883                           src_qp, qp->ibqp.qp_num,
0884                           slid, dlid);
0885                 return;
0886             }
0887         } else {
0888             /* GSI packet */
0889             mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
0890             if (mgmt_pkey_idx < 0)
0891                 goto drop;
0892         }
0893         if (unlikely(l4 != OPA_16B_L4_FM &&
0894                  ib_get_qkey(packet->ohdr) != qp->qkey))
0895             return; /* Silent drop */
0896 
0897         /* Drop invalid MAD packets (see 13.5.3.1). */
0898         if (unlikely(qp->ibqp.qp_num == 1 &&
0899                  (tlen > 2048 || (sc5 == 0xF))))
0900             goto drop;
0901     } else {
0902         /* Received on QP0, and so by definition, this is an SMP */
0903         struct opa_smp *smp = (struct opa_smp *)data;
0904 
0905         if (opa_smp_check(ibp, pkey, sc5, qp, slid, smp))
0906             goto drop;
0907 
0908         if (tlen > 2048)
0909             goto drop;
0910         if ((dlid_is_permissive || slid_is_permissive) &&
0911             smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
0912             goto drop;
0913 
0914         /* look up SMI pkey */
0915         mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
0916         if (mgmt_pkey_idx < 0)
0917             goto drop;
0918     }
0919 
0920     if (qp->ibqp.qp_num > 1 &&
0921         opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
0922         wc.ex.imm_data = packet->ohdr->u.ud.imm_data;
0923         wc.wc_flags = IB_WC_WITH_IMM;
0924     } else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
0925         wc.ex.imm_data = 0;
0926         wc.wc_flags = 0;
0927     } else {
0928         goto drop;
0929     }
0930 
0931     /*
0932      * A GRH is expected to precede the data even if not
0933      * present on the wire.
0934      */
0935     wc.byte_len = tlen + sizeof(struct ib_grh);
0936 
0937     /*
0938      * Get the next work request entry to find where to put the data.
0939      */
0940     if (qp->r_flags & RVT_R_REUSE_SGE) {
0941         qp->r_flags &= ~RVT_R_REUSE_SGE;
0942     } else {
0943         int ret;
0944 
0945         ret = rvt_get_rwqe(qp, false);
0946         if (ret < 0) {
0947             rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
0948             return;
0949         }
0950         if (!ret) {
0951             if (qp->ibqp.qp_num == 0)
0952                 ibp->rvp.n_vl15_dropped++;
0953             return;
0954         }
0955     }
0956     /* Silently drop packets which are too big. */
0957     if (unlikely(wc.byte_len > qp->r_len)) {
0958         qp->r_flags |= RVT_R_REUSE_SGE;
0959         goto drop;
0960     }
0961     if (packet->grh) {
0962         rvt_copy_sge(qp, &qp->r_sge, packet->grh,
0963                  sizeof(struct ib_grh), true, false);
0964         wc.wc_flags |= IB_WC_GRH;
0965     } else if (packet->etype == RHF_RCV_TYPE_BYPASS) {
0966         struct ib_grh grh;
0967         /*
0968          * Assuming we only created 16B on the send side
0969          * if we want to use large LIDs, since GRH was stripped
0970          * out when creating 16B, add back the GRH here.
0971          */
0972         hfi1_make_ext_grh(packet, &grh, slid, dlid);
0973         rvt_copy_sge(qp, &qp->r_sge, &grh,
0974                  sizeof(struct ib_grh), true, false);
0975         wc.wc_flags |= IB_WC_GRH;
0976     } else {
0977         rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
0978     }
0979     rvt_copy_sge(qp, &qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh),
0980              true, false);
0981     rvt_put_ss(&qp->r_sge);
0982     if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
0983         return;
0984     wc.wr_id = qp->r_wr_id;
0985     wc.status = IB_WC_SUCCESS;
0986     wc.opcode = IB_WC_RECV;
0987     wc.vendor_err = 0;
0988     wc.qp = &qp->ibqp;
0989     wc.src_qp = src_qp;
0990 
0991     if (qp->ibqp.qp_type == IB_QPT_GSI ||
0992         qp->ibqp.qp_type == IB_QPT_SMI) {
0993         if (mgmt_pkey_idx < 0) {
0994             if (net_ratelimit()) {
0995                 struct hfi1_devdata *dd = ppd->dd;
0996 
0997                 dd_dev_err(dd, "QP type %d mgmt_pkey_idx < 0 and packet not dropped???\n",
0998                        qp->ibqp.qp_type);
0999                 mgmt_pkey_idx = 0;
1000             }
1001         }
1002         wc.pkey_index = (unsigned)mgmt_pkey_idx;
1003     } else {
1004         wc.pkey_index = 0;
1005     }
1006     if (slid_is_permissive)
1007         slid = be32_to_cpu(OPA_LID_PERMISSIVE);
1008     wc.slid = slid & U16_MAX;
1009     wc.sl = sl_from_sc;
1010 
1011     /*
1012      * Save the LMC lower bits if the destination LID is a unicast LID.
1013      */
1014     wc.dlid_path_bits = hfi1_check_mcast(dlid) ? 0 :
1015         dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1);
1016     wc.port_num = qp->port_num;
1017     /* Signal completion event if the solicited bit is set. */
1018     rvt_recv_cq(qp, &wc, solicited);
1019     return;
1020 
1021 drop:
1022     ibp->rvp.n_pkt_drops++;
1023 }