This source file includes following definitions.
- ud_loopback
- hfi1_make_bth_deth
- hfi1_make_ud_req_9B
- hfi1_make_ud_req_16B
- hfi1_make_ud_req
- hfi1_lookup_pkey_idx
- return_cnp_16B
- return_cnp
- opa_smp_check
- hfi1_ud_rcv
   1 
   2 
   3 
   4 
   5 
   6 
   7 
   8 
   9 
  10 
  11 
  12 
  13 
  14 
  15 
  16 
  17 
  18 
  19 
  20 
  21 
  22 
  23 
  24 
  25 
  26 
  27 
  28 
  29 
  30 
  31 
  32 
  33 
  34 
  35 
  36 
  37 
  38 
  39 
  40 
  41 
  42 
  43 
  44 
  45 
  46 
  47 
  48 #include <linux/net.h>
  49 #include <rdma/ib_smi.h>
  50 
  51 #include "hfi.h"
  52 #include "mad.h"
  53 #include "verbs_txreq.h"
  54 #include "trace_ibhdrs.h"
  55 #include "qp.h"
  56 
  57 
  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
  61 };
  62 
  63 
  64 
  65 
  66 
  67 
  68 
  69 
  70 
  71 
  72 
  73 static void ud_loopback(struct rvt_qp *sqp, struct rvt_swqe *swqe)
  74 {
  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;
  78         struct rvt_qp *qp;
  79         struct rdma_ah_attr *ah_attr;
  80         unsigned long flags;
  81         struct rvt_sge_state ssge;
  82         struct rvt_sge *sge;
  83         struct ib_wc wc;
  84         u32 length;
  85         enum ib_qp_type sqptype, dqptype;
  86 
  87         rcu_read_lock();
  88 
  89         qp = rvt_lookup_qpn(ib_to_rvt(sqp->ibqp.device), &ibp->rvp,
  90                             rvt_get_swqe_remote_qpn(swqe));
  91         if (!qp) {
  92                 ibp->rvp.n_pkt_drops++;
  93                 rcu_read_unlock();
  94                 return;
  95         }
  96 
  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;
 101 
 102         if (dqptype != sqptype ||
 103             !(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
 104                 ibp->rvp.n_pkt_drops++;
 105                 goto drop;
 106         }
 107 
 108         ah_attr = rvt_get_swqe_ah_attr(swqe);
 109         ppd = ppd_from_ibp(ibp);
 110 
 111         if (qp->ibqp.qp_num > 1) {
 112                 u16 pkey;
 113                 u32 slid;
 114                 u8 sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
 115 
 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,
 120                                                 qp->s_pkey_index,
 121                                                 slid, false))) {
 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));
 126                         goto drop;
 127                 }
 128         }
 129 
 130         
 131 
 132 
 133 
 134 
 135         if (qp->ibqp.qp_num) {
 136                 u32 qkey;
 137 
 138                 qkey = (int)rvt_get_swqe_remote_qkey(swqe) < 0 ?
 139                         sqp->qkey : rvt_get_swqe_remote_qkey(swqe);
 140                 if (unlikely(qkey != qp->qkey))
 141                         goto drop; 
 142         }
 143 
 144         
 145 
 146 
 147 
 148         length = swqe->length;
 149         memset(&wc, 0, sizeof(wc));
 150         wc.byte_len = length + sizeof(struct ib_grh);
 151 
 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;
 155         }
 156 
 157         spin_lock_irqsave(&qp->r_lock, flags);
 158 
 159         
 160 
 161 
 162         if (qp->r_flags & RVT_R_REUSE_SGE) {
 163                 qp->r_flags &= ~RVT_R_REUSE_SGE;
 164         } else {
 165                 int ret;
 166 
 167                 ret = rvt_get_rwqe(qp, false);
 168                 if (ret < 0) {
 169                         rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
 170                         goto bail_unlock;
 171                 }
 172                 if (!ret) {
 173                         if (qp->ibqp.qp_num == 0)
 174                                 ibp->rvp.n_vl15_dropped++;
 175                         goto bail_unlock;
 176                 }
 177         }
 178         
 179         if (unlikely(wc.byte_len > qp->r_len)) {
 180                 qp->r_flags |= RVT_R_REUSE_SGE;
 181                 ibp->rvp.n_pkt_drops++;
 182                 goto bail_unlock;
 183         }
 184 
 185         if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
 186                 struct ib_grh grh;
 187                 struct ib_global_route grd = *(rdma_ah_read_grh(ah_attr));
 188 
 189                 
 190 
 191 
 192 
 193 
 194 
 195 
 196 
 197 
 198 
 199 
 200 
 201 
 202 
 203 
 204                 if (priv->hdr_type == HFI1_PKT_TYPE_16B) {
 205                         if (grd.sgid_index == 0)
 206                                 grd.sgid_index = OPA_GID_INDEX;
 207 
 208                         if (ib_is_opa_gid(&grd.dgid))
 209                                 grd.dgid.global.interface_id =
 210                                 cpu_to_be64(ppd->guids[HFI1_PORT_GUID_INDEX]);
 211                 }
 212 
 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;
 217         } else {
 218                 rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
 219         }
 220         ssge.sg_list = swqe->sg_list + 1;
 221         ssge.sge = *swqe->sg_list;
 222         ssge.num_sge = swqe->wr.num_sge;
 223         sge = &ssge.sge;
 224         while (length) {
 225                 u32 len = rvt_get_sge_length(sge, length);
 226 
 227                 WARN_ON_ONCE(len == 0);
 228                 rvt_copy_sge(qp, &qp->r_sge, sge->vaddr, len, true, false);
 229                 rvt_update_sge(&ssge, len, false);
 230                 length -= len;
 231         }
 232         rvt_put_ss(&qp->r_sge);
 233         if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
 234                 goto bail_unlock;
 235         wc.wr_id = qp->r_wr_id;
 236         wc.status = IB_WC_SUCCESS;
 237         wc.opcode = IB_WC_RECV;
 238         wc.qp = &qp->ibqp;
 239         wc.src_qp = sqp->ibqp.qp_num;
 240         if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI) {
 241                 if (sqp->ibqp.qp_type == IB_QPT_GSI ||
 242                     sqp->ibqp.qp_type == IB_QPT_SMI)
 243                         wc.pkey_index = rvt_get_swqe_pkey_index(swqe);
 244                 else
 245                         wc.pkey_index = sqp->s_pkey_index;
 246         } else {
 247                 wc.pkey_index = 0;
 248         }
 249         wc.slid = (ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
 250                                    ((1 << ppd->lmc) - 1))) & U16_MAX;
 251         
 252         if (wc.slid == 0 && sqp->ibqp.qp_type == IB_QPT_GSI)
 253                 wc.slid = be16_to_cpu(IB_LID_PERMISSIVE);
 254         wc.sl = rdma_ah_get_sl(ah_attr);
 255         wc.dlid_path_bits = rdma_ah_get_dlid(ah_attr) & ((1 << ppd->lmc) - 1);
 256         wc.port_num = qp->port_num;
 257         
 258         rvt_recv_cq(qp, &wc, swqe->wr.send_flags & IB_SEND_SOLICITED);
 259         ibp->rvp.n_loop_pkts++;
 260 bail_unlock:
 261         spin_unlock_irqrestore(&qp->r_lock, flags);
 262 drop:
 263         rcu_read_unlock();
 264 }
 265 
 266 static void hfi1_make_bth_deth(struct rvt_qp *qp, struct rvt_swqe *wqe,
 267                                struct ib_other_headers *ohdr,
 268                                u16 *pkey, u32 extra_bytes, bool bypass)
 269 {
 270         u32 bth0;
 271         struct hfi1_ibport *ibp;
 272 
 273         ibp = to_iport(qp->ibqp.device, qp->port_num);
 274         if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
 275                 ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;
 276                 bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
 277         } else {
 278                 bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
 279         }
 280 
 281         if (wqe->wr.send_flags & IB_SEND_SOLICITED)
 282                 bth0 |= IB_BTH_SOLICITED;
 283         bth0 |= extra_bytes << 20;
 284         if (qp->ibqp.qp_type == IB_QPT_GSI || qp->ibqp.qp_type == IB_QPT_SMI)
 285                 *pkey = hfi1_get_pkey(ibp, rvt_get_swqe_pkey_index(wqe));
 286         else
 287                 *pkey = hfi1_get_pkey(ibp, qp->s_pkey_index);
 288         if (!bypass)
 289                 bth0 |= *pkey;
 290         ohdr->bth[0] = cpu_to_be32(bth0);
 291         ohdr->bth[1] = cpu_to_be32(rvt_get_swqe_remote_qpn(wqe));
 292         ohdr->bth[2] = cpu_to_be32(mask_psn(wqe->psn));
 293         
 294 
 295 
 296 
 297         ohdr->u.ud.deth[0] =
 298                 cpu_to_be32((int)rvt_get_swqe_remote_qkey(wqe) < 0 ? qp->qkey :
 299                             rvt_get_swqe_remote_qkey(wqe));
 300         ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
 301 }
 302 
 303 void hfi1_make_ud_req_9B(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
 304                          struct rvt_swqe *wqe)
 305 {
 306         u32 nwords, extra_bytes;
 307         u16 len, slid, dlid, pkey;
 308         u16 lrh0 = 0;
 309         u8 sc5;
 310         struct hfi1_qp_priv *priv = qp->priv;
 311         struct ib_other_headers *ohdr;
 312         struct rdma_ah_attr *ah_attr;
 313         struct hfi1_pportdata *ppd;
 314         struct hfi1_ibport *ibp;
 315         struct ib_grh *grh;
 316 
 317         ibp = to_iport(qp->ibqp.device, qp->port_num);
 318         ppd = ppd_from_ibp(ibp);
 319         ah_attr = rvt_get_swqe_ah_attr(wqe);
 320 
 321         extra_bytes = -wqe->length & 3;
 322         nwords = ((wqe->length + extra_bytes) >> 2) + SIZE_OF_CRC;
 323         
 324         ps->s_txreq->hdr_dwords = 7;
 325         if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
 326                 ps->s_txreq->hdr_dwords++;
 327 
 328         if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
 329                 grh = &ps->s_txreq->phdr.hdr.ibh.u.l.grh;
 330                 ps->s_txreq->hdr_dwords +=
 331                         hfi1_make_grh(ibp, grh, rdma_ah_read_grh(ah_attr),
 332                                       ps->s_txreq->hdr_dwords - LRH_9B_DWORDS,
 333                                       nwords);
 334                 lrh0 = HFI1_LRH_GRH;
 335                 ohdr = &ps->s_txreq->phdr.hdr.ibh.u.l.oth;
 336         } else {
 337                 lrh0 = HFI1_LRH_BTH;
 338                 ohdr = &ps->s_txreq->phdr.hdr.ibh.u.oth;
 339         }
 340 
 341         sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
 342         lrh0 |= (rdma_ah_get_sl(ah_attr) & 0xf) << 4;
 343         if (qp->ibqp.qp_type == IB_QPT_SMI) {
 344                 lrh0 |= 0xF000; 
 345                 priv->s_sc = 0xf;
 346         } else {
 347                 lrh0 |= (sc5 & 0xf) << 12;
 348                 priv->s_sc = sc5;
 349         }
 350 
 351         dlid = opa_get_lid(rdma_ah_get_dlid(ah_attr), 9B);
 352         if (dlid == be16_to_cpu(IB_LID_PERMISSIVE)) {
 353                 slid = be16_to_cpu(IB_LID_PERMISSIVE);
 354         } else {
 355                 u16 lid = (u16)ppd->lid;
 356 
 357                 if (lid) {
 358                         lid |= rdma_ah_get_path_bits(ah_attr) &
 359                                 ((1 << ppd->lmc) - 1);
 360                         slid = lid;
 361                 } else {
 362                         slid = be16_to_cpu(IB_LID_PERMISSIVE);
 363                 }
 364         }
 365         hfi1_make_bth_deth(qp, wqe, ohdr, &pkey, extra_bytes, false);
 366         len = ps->s_txreq->hdr_dwords + nwords;
 367 
 368         
 369         ps->s_txreq->phdr.hdr.hdr_type = HFI1_PKT_TYPE_9B;
 370         hfi1_make_ib_hdr(&ps->s_txreq->phdr.hdr.ibh,
 371                          lrh0, len, dlid, slid);
 372 }
 373 
 374 void hfi1_make_ud_req_16B(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
 375                           struct rvt_swqe *wqe)
 376 {
 377         struct hfi1_qp_priv *priv = qp->priv;
 378         struct ib_other_headers *ohdr;
 379         struct rdma_ah_attr *ah_attr;
 380         struct hfi1_pportdata *ppd;
 381         struct hfi1_ibport *ibp;
 382         u32 dlid, slid, nwords, extra_bytes;
 383         u32 dest_qp = rvt_get_swqe_remote_qpn(wqe);
 384         u32 src_qp = qp->ibqp.qp_num;
 385         u16 len, pkey;
 386         u8 l4, sc5;
 387         bool is_mgmt = false;
 388 
 389         ibp = to_iport(qp->ibqp.device, qp->port_num);
 390         ppd = ppd_from_ibp(ibp);
 391         ah_attr = rvt_get_swqe_ah_attr(wqe);
 392 
 393         
 394 
 395 
 396 
 397         if (dest_qp == 0 || src_qp == 0 || dest_qp == 1 || src_qp == 1) {
 398                 
 399                 ps->s_txreq->hdr_dwords = 6;
 400                 is_mgmt = true;
 401         } else {
 402                 
 403                 ps->s_txreq->hdr_dwords = 9;
 404                 if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM)
 405                         ps->s_txreq->hdr_dwords++;
 406         }
 407 
 408         
 409         extra_bytes = hfi1_get_16b_padding((ps->s_txreq->hdr_dwords << 2),
 410                                            wqe->length);
 411         nwords = ((wqe->length + extra_bytes + SIZE_OF_LT) >> 2) + SIZE_OF_CRC;
 412 
 413         if ((rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) &&
 414             hfi1_check_mcast(rdma_ah_get_dlid(ah_attr))) {
 415                 struct ib_grh *grh;
 416                 struct ib_global_route *grd = rdma_ah_retrieve_grh(ah_attr);
 417                 
 418 
 419 
 420 
 421                 if (grd->sgid_index == OPA_GID_INDEX) {
 422                         dd_dev_warn(ppd->dd, "Bad sgid_index. sgid_index: %d\n",
 423                                     grd->sgid_index);
 424                         grd->sgid_index = 0;
 425                 }
 426                 grh = &ps->s_txreq->phdr.hdr.opah.u.l.grh;
 427                 ps->s_txreq->hdr_dwords += hfi1_make_grh(
 428                         ibp, grh, grd,
 429                         ps->s_txreq->hdr_dwords - LRH_16B_DWORDS,
 430                         nwords);
 431                 ohdr = &ps->s_txreq->phdr.hdr.opah.u.l.oth;
 432                 l4 = OPA_16B_L4_IB_GLOBAL;
 433         } else {
 434                 ohdr = &ps->s_txreq->phdr.hdr.opah.u.oth;
 435                 l4 = OPA_16B_L4_IB_LOCAL;
 436         }
 437 
 438         sc5 = ibp->sl_to_sc[rdma_ah_get_sl(ah_attr)];
 439         if (qp->ibqp.qp_type == IB_QPT_SMI)
 440                 priv->s_sc = 0xf;
 441         else
 442                 priv->s_sc = sc5;
 443 
 444         dlid = opa_get_lid(rdma_ah_get_dlid(ah_attr), 16B);
 445         if (!ppd->lid)
 446                 slid = be32_to_cpu(OPA_LID_PERMISSIVE);
 447         else
 448                 slid = ppd->lid | (rdma_ah_get_path_bits(ah_attr) &
 449                            ((1 << ppd->lmc) - 1));
 450 
 451         if (is_mgmt) {
 452                 l4 = OPA_16B_L4_FM;
 453                 pkey = hfi1_get_pkey(ibp, rvt_get_swqe_pkey_index(wqe));
 454                 hfi1_16B_set_qpn(&ps->s_txreq->phdr.hdr.opah.u.mgmt,
 455                                  dest_qp, src_qp);
 456         } else {
 457                 hfi1_make_bth_deth(qp, wqe, ohdr, &pkey, extra_bytes, true);
 458         }
 459         
 460         len = (ps->s_txreq->hdr_dwords + nwords) >> 1;
 461 
 462         
 463         ps->s_txreq->phdr.hdr.hdr_type = HFI1_PKT_TYPE_16B;
 464         hfi1_make_16b_hdr(&ps->s_txreq->phdr.hdr.opah,
 465                           slid, dlid, len, pkey, 0, 0, l4, priv->s_sc);
 466 }
 467 
 468 
 469 
 470 
 471 
 472 
 473 
 474 
 475 
 476 int hfi1_make_ud_req(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
 477 {
 478         struct hfi1_qp_priv *priv = qp->priv;
 479         struct rdma_ah_attr *ah_attr;
 480         struct hfi1_pportdata *ppd;
 481         struct hfi1_ibport *ibp;
 482         struct rvt_swqe *wqe;
 483         int next_cur;
 484         u32 lid;
 485 
 486         ps->s_txreq = get_txreq(ps->dev, qp);
 487         if (!ps->s_txreq)
 488                 goto bail_no_tx;
 489 
 490         if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_NEXT_SEND_OK)) {
 491                 if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
 492                         goto bail;
 493                 
 494                 if (qp->s_last == READ_ONCE(qp->s_head))
 495                         goto bail;
 496                 
 497                 if (iowait_sdma_pending(&priv->s_iowait)) {
 498                         qp->s_flags |= RVT_S_WAIT_DMA;
 499                         goto bail;
 500                 }
 501                 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
 502                 rvt_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
 503                 goto done_free_tx;
 504         }
 505 
 506         
 507         if (qp->s_cur == READ_ONCE(qp->s_head))
 508                 goto bail;
 509 
 510         wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
 511         next_cur = qp->s_cur + 1;
 512         if (next_cur >= qp->s_size)
 513                 next_cur = 0;
 514 
 515         
 516         ibp = to_iport(qp->ibqp.device, qp->port_num);
 517         ppd = ppd_from_ibp(ibp);
 518         ah_attr = rvt_get_swqe_ah_attr(wqe);
 519         priv->hdr_type = hfi1_get_hdr_type(ppd->lid, ah_attr);
 520         if ((!hfi1_check_mcast(rdma_ah_get_dlid(ah_attr))) ||
 521             (rdma_ah_get_dlid(ah_attr) == be32_to_cpu(OPA_LID_PERMISSIVE))) {
 522                 lid = rdma_ah_get_dlid(ah_attr) & ~((1 << ppd->lmc) - 1);
 523                 if (unlikely(!loopback &&
 524                              ((lid == ppd->lid) ||
 525                               ((lid == be32_to_cpu(OPA_LID_PERMISSIVE)) &&
 526                                (qp->ibqp.qp_type == IB_QPT_GSI))))) {
 527                         unsigned long tflags = ps->flags;
 528                         
 529 
 530 
 531 
 532 
 533 
 534 
 535                         if (iowait_sdma_pending(&priv->s_iowait)) {
 536                                 qp->s_flags |= RVT_S_WAIT_DMA;
 537                                 goto bail;
 538                         }
 539                         qp->s_cur = next_cur;
 540                         spin_unlock_irqrestore(&qp->s_lock, tflags);
 541                         ud_loopback(qp, wqe);
 542                         spin_lock_irqsave(&qp->s_lock, tflags);
 543                         ps->flags = tflags;
 544                         rvt_send_complete(qp, wqe, IB_WC_SUCCESS);
 545                         goto done_free_tx;
 546                 }
 547         }
 548 
 549         qp->s_cur = next_cur;
 550         ps->s_txreq->s_cur_size = wqe->length;
 551         ps->s_txreq->ss = &qp->s_sge;
 552         qp->s_srate = rdma_ah_get_static_rate(ah_attr);
 553         qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
 554         qp->s_wqe = wqe;
 555         qp->s_sge.sge = wqe->sg_list[0];
 556         qp->s_sge.sg_list = wqe->sg_list + 1;
 557         qp->s_sge.num_sge = wqe->wr.num_sge;
 558         qp->s_sge.total_len = wqe->length;
 559 
 560         
 561         hfi1_make_ud_req_tbl[priv->hdr_type](qp, ps, qp->s_wqe);
 562         priv->s_sde = qp_to_sdma_engine(qp, priv->s_sc);
 563         ps->s_txreq->sde = priv->s_sde;
 564         priv->s_sendcontext = qp_to_send_context(qp, priv->s_sc);
 565         ps->s_txreq->psc = priv->s_sendcontext;
 566         
 567         priv->s_ahg->ahgcount = 0;
 568         priv->s_ahg->ahgidx = 0;
 569         priv->s_ahg->tx_flags = 0;
 570 
 571         return 1;
 572 
 573 done_free_tx:
 574         hfi1_put_txreq(ps->s_txreq);
 575         ps->s_txreq = NULL;
 576         return 1;
 577 
 578 bail:
 579         hfi1_put_txreq(ps->s_txreq);
 580 
 581 bail_no_tx:
 582         ps->s_txreq = NULL;
 583         qp->s_flags &= ~RVT_S_BUSY;
 584         return 0;
 585 }
 586 
 587 
 588 
 589 
 590 
 591 
 592 
 593 
 594 
 595 
 596 int hfi1_lookup_pkey_idx(struct hfi1_ibport *ibp, u16 pkey)
 597 {
 598         struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
 599         unsigned i;
 600 
 601         if (pkey == FULL_MGMT_P_KEY || pkey == LIM_MGMT_P_KEY) {
 602                 unsigned lim_idx = -1;
 603 
 604                 for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i) {
 605                         
 606                         if (ppd->pkeys[i] == pkey)
 607                                 return i;
 608                         if (ppd->pkeys[i] == LIM_MGMT_P_KEY)
 609                                 lim_idx = i;
 610                 }
 611 
 612                 
 613                 if (pkey == FULL_MGMT_P_KEY)
 614                         return lim_idx;
 615 
 616                 
 617                 return -1;
 618         }
 619 
 620         pkey &= 0x7fff; 
 621 
 622         for (i = 0; i < ARRAY_SIZE(ppd->pkeys); ++i)
 623                 if ((ppd->pkeys[i] & 0x7fff) == pkey)
 624                         return i;
 625 
 626         
 627 
 628 
 629         return -1;
 630 }
 631 
 632 void return_cnp_16B(struct hfi1_ibport *ibp, struct rvt_qp *qp,
 633                     u32 remote_qpn, u16 pkey, u32 slid, u32 dlid,
 634                     u8 sc5, const struct ib_grh *old_grh)
 635 {
 636         u64 pbc, pbc_flags = 0;
 637         u32 bth0, plen, vl, hwords = 7;
 638         u16 len;
 639         u8 l4;
 640         struct hfi1_opa_header hdr;
 641         struct ib_other_headers *ohdr;
 642         struct pio_buf *pbuf;
 643         struct send_context *ctxt = qp_to_send_context(qp, sc5);
 644         struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
 645         u32 nwords;
 646 
 647         hdr.hdr_type = HFI1_PKT_TYPE_16B;
 648         
 649         nwords = ((hfi1_get_16b_padding(hwords << 2, 0) +
 650                    SIZE_OF_LT) >> 2) + SIZE_OF_CRC;
 651         if (old_grh) {
 652                 struct ib_grh *grh = &hdr.opah.u.l.grh;
 653 
 654                 grh->version_tclass_flow = old_grh->version_tclass_flow;
 655                 grh->paylen = cpu_to_be16(
 656                         (hwords - LRH_16B_DWORDS + nwords) << 2);
 657                 grh->hop_limit = 0xff;
 658                 grh->sgid = old_grh->dgid;
 659                 grh->dgid = old_grh->sgid;
 660                 ohdr = &hdr.opah.u.l.oth;
 661                 l4 = OPA_16B_L4_IB_GLOBAL;
 662                 hwords += sizeof(struct ib_grh) / sizeof(u32);
 663         } else {
 664                 ohdr = &hdr.opah.u.oth;
 665                 l4 = OPA_16B_L4_IB_LOCAL;
 666         }
 667 
 668         
 669         bth0 = (IB_OPCODE_CNP << 24) | (1 << 16) |
 670                (hfi1_get_16b_padding(hwords << 2, 0) << 20);
 671         ohdr->bth[0] = cpu_to_be32(bth0);
 672 
 673         ohdr->bth[1] = cpu_to_be32(remote_qpn);
 674         ohdr->bth[2] = 0; 
 675 
 676         
 677         len = (hwords + nwords) >> 1;
 678         hfi1_make_16b_hdr(&hdr.opah, slid, dlid, len, pkey, 1, 0, l4, sc5);
 679 
 680         plen = 2  + hwords + nwords;
 681         pbc_flags |= PBC_PACKET_BYPASS | PBC_INSERT_BYPASS_ICRC;
 682         vl = sc_to_vlt(ppd->dd, sc5);
 683         pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
 684         if (ctxt) {
 685                 pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
 686                 if (!IS_ERR_OR_NULL(pbuf)) {
 687                         trace_pio_output_ibhdr(ppd->dd, &hdr, sc5);
 688                         ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
 689                                                  &hdr, hwords);
 690                 }
 691         }
 692 }
 693 
 694 void return_cnp(struct hfi1_ibport *ibp, struct rvt_qp *qp, u32 remote_qpn,
 695                 u16 pkey, u32 slid, u32 dlid, u8 sc5,
 696                 const struct ib_grh *old_grh)
 697 {
 698         u64 pbc, pbc_flags = 0;
 699         u32 bth0, plen, vl, hwords = 5;
 700         u16 lrh0;
 701         u8 sl = ibp->sc_to_sl[sc5];
 702         struct hfi1_opa_header hdr;
 703         struct ib_other_headers *ohdr;
 704         struct pio_buf *pbuf;
 705         struct send_context *ctxt = qp_to_send_context(qp, sc5);
 706         struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
 707 
 708         hdr.hdr_type = HFI1_PKT_TYPE_9B;
 709         if (old_grh) {
 710                 struct ib_grh *grh = &hdr.ibh.u.l.grh;
 711 
 712                 grh->version_tclass_flow = old_grh->version_tclass_flow;
 713                 grh->paylen = cpu_to_be16(
 714                         (hwords - LRH_9B_DWORDS + SIZE_OF_CRC) << 2);
 715                 grh->hop_limit = 0xff;
 716                 grh->sgid = old_grh->dgid;
 717                 grh->dgid = old_grh->sgid;
 718                 ohdr = &hdr.ibh.u.l.oth;
 719                 lrh0 = HFI1_LRH_GRH;
 720                 hwords += sizeof(struct ib_grh) / sizeof(u32);
 721         } else {
 722                 ohdr = &hdr.ibh.u.oth;
 723                 lrh0 = HFI1_LRH_BTH;
 724         }
 725 
 726         lrh0 |= (sc5 & 0xf) << 12 | sl << 4;
 727 
 728         bth0 = pkey | (IB_OPCODE_CNP << 24);
 729         ohdr->bth[0] = cpu_to_be32(bth0);
 730 
 731         ohdr->bth[1] = cpu_to_be32(remote_qpn | (1 << IB_BECN_SHIFT));
 732         ohdr->bth[2] = 0; 
 733 
 734         hfi1_make_ib_hdr(&hdr.ibh, lrh0, hwords + SIZE_OF_CRC, dlid, slid);
 735         plen = 2  + hwords;
 736         pbc_flags |= (ib_is_sc5(sc5) << PBC_DC_INFO_SHIFT);
 737         vl = sc_to_vlt(ppd->dd, sc5);
 738         pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
 739         if (ctxt) {
 740                 pbuf = sc_buffer_alloc(ctxt, plen, NULL, NULL);
 741                 if (!IS_ERR_OR_NULL(pbuf)) {
 742                         trace_pio_output_ibhdr(ppd->dd, &hdr, sc5);
 743                         ppd->dd->pio_inline_send(ppd->dd, pbuf, pbc,
 744                                                  &hdr, hwords);
 745                 }
 746         }
 747 }
 748 
 749 
 750 
 751 
 752 
 753 
 754 
 755 
 756 
 757 
 758 
 759 
 760 
 761 
 762 
 763 
 764 
 765 
 766 
 767 static int opa_smp_check(struct hfi1_ibport *ibp, u16 pkey, u8 sc5,
 768                          struct rvt_qp *qp, u16 slid, struct opa_smp *smp)
 769 {
 770         struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
 771 
 772         
 773 
 774 
 775 
 776         if (sc5 != 0xf)
 777                 return 1;
 778 
 779         if (rcv_pkey_check(ppd, pkey, sc5, slid))
 780                 return 1;
 781 
 782         
 783 
 784 
 785 
 786 
 787         if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE &&
 788             smp->mgmt_class != IB_MGMT_CLASS_SUBN_LID_ROUTED) {
 789                 ingress_pkey_table_fail(ppd, pkey, slid);
 790                 return 1;
 791         }
 792 
 793         
 794 
 795 
 796 
 797 
 798 
 799 
 800 
 801 
 802 
 803 
 804 
 805 
 806 
 807 
 808 
 809 
 810 
 811 
 812 
 813         switch (smp->method) {
 814         case IB_MGMT_METHOD_GET_RESP:
 815         case IB_MGMT_METHOD_REPORT_RESP:
 816                 break;
 817         case IB_MGMT_METHOD_GET:
 818         case IB_MGMT_METHOD_SET:
 819         case IB_MGMT_METHOD_REPORT:
 820         case IB_MGMT_METHOD_TRAP_REPRESS:
 821                 if (pkey != FULL_MGMT_P_KEY) {
 822                         ingress_pkey_table_fail(ppd, pkey, slid);
 823                         return 1;
 824                 }
 825                 break;
 826         default:
 827                 if (ibp->rvp.port_cap_flags & IB_PORT_SM)
 828                         return 0;
 829                 if (smp->method == IB_MGMT_METHOD_TRAP)
 830                         return 1;
 831                 if (pkey == FULL_MGMT_P_KEY) {
 832                         smp->status |= IB_SMP_UNSUP_METHOD;
 833                         return 0;
 834                 }
 835                 ingress_pkey_table_fail(ppd, pkey, slid);
 836                 return 1;
 837         }
 838         return 0;
 839 }
 840 
 841 
 842 
 843 
 844 
 845 
 846 
 847 
 848 
 849 
 850 
 851 
 852 
 853 
 854 void hfi1_ud_rcv(struct hfi1_packet *packet)
 855 {
 856         u32 hdrsize = packet->hlen;
 857         struct ib_wc wc;
 858         u32 src_qp;
 859         u16 pkey;
 860         int mgmt_pkey_idx = -1;
 861         struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
 862         struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
 863         void *data = packet->payload;
 864         u32 tlen = packet->tlen;
 865         struct rvt_qp *qp = packet->qp;
 866         u8 sc5 = packet->sc;
 867         u8 sl_from_sc;
 868         u8 opcode = packet->opcode;
 869         u8 sl = packet->sl;
 870         u32 dlid = packet->dlid;
 871         u32 slid = packet->slid;
 872         u8 extra_bytes;
 873         u8 l4 = 0;
 874         bool dlid_is_permissive;
 875         bool slid_is_permissive;
 876         bool solicited = false;
 877 
 878         extra_bytes = packet->pad + packet->extra_byte + (SIZE_OF_CRC << 2);
 879 
 880         if (packet->etype == RHF_RCV_TYPE_BYPASS) {
 881                 u32 permissive_lid =
 882                         opa_get_lid(be32_to_cpu(OPA_LID_PERMISSIVE), 16B);
 883 
 884                 l4 = hfi1_16B_get_l4(packet->hdr);
 885                 pkey = hfi1_16B_get_pkey(packet->hdr);
 886                 dlid_is_permissive = (dlid == permissive_lid);
 887                 slid_is_permissive = (slid == permissive_lid);
 888         } else {
 889                 pkey = ib_bth_get_pkey(packet->ohdr);
 890                 dlid_is_permissive = (dlid == be16_to_cpu(IB_LID_PERMISSIVE));
 891                 slid_is_permissive = (slid == be16_to_cpu(IB_LID_PERMISSIVE));
 892         }
 893         sl_from_sc = ibp->sc_to_sl[sc5];
 894 
 895         if (likely(l4 != OPA_16B_L4_FM)) {
 896                 src_qp = ib_get_sqpn(packet->ohdr);
 897                 solicited = ib_bth_is_solicited(packet->ohdr);
 898         } else {
 899                 src_qp = hfi1_16B_get_src_qpn(packet->mgmt);
 900         }
 901 
 902         process_ecn(qp, packet);
 903         
 904 
 905 
 906 
 907         if (unlikely(tlen < (hdrsize + extra_bytes)))
 908                 goto drop;
 909 
 910         tlen -= hdrsize + extra_bytes;
 911 
 912         
 913 
 914 
 915 
 916         if (qp->ibqp.qp_num) {
 917                 if (unlikely(dlid_is_permissive || slid_is_permissive))
 918                         goto drop;
 919                 if (qp->ibqp.qp_num > 1) {
 920                         if (unlikely(rcv_pkey_check(ppd, pkey, sc5, slid))) {
 921                                 
 922 
 923 
 924 
 925 
 926 
 927                                 hfi1_bad_pkey(ibp,
 928                                               pkey, sl,
 929                                               src_qp, qp->ibqp.qp_num,
 930                                               slid, dlid);
 931                                 return;
 932                         }
 933                 } else {
 934                         
 935                         mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
 936                         if (mgmt_pkey_idx < 0)
 937                                 goto drop;
 938                 }
 939                 if (unlikely(l4 != OPA_16B_L4_FM &&
 940                              ib_get_qkey(packet->ohdr) != qp->qkey))
 941                         return; 
 942 
 943                 
 944                 if (unlikely(qp->ibqp.qp_num == 1 &&
 945                              (tlen > 2048 || (sc5 == 0xF))))
 946                         goto drop;
 947         } else {
 948                 
 949                 struct opa_smp *smp = (struct opa_smp *)data;
 950 
 951                 if (opa_smp_check(ibp, pkey, sc5, qp, slid, smp))
 952                         goto drop;
 953 
 954                 if (tlen > 2048)
 955                         goto drop;
 956                 if ((dlid_is_permissive || slid_is_permissive) &&
 957                     smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
 958                         goto drop;
 959 
 960                 
 961                 mgmt_pkey_idx = hfi1_lookup_pkey_idx(ibp, pkey);
 962                 if (mgmt_pkey_idx < 0)
 963                         goto drop;
 964         }
 965 
 966         if (qp->ibqp.qp_num > 1 &&
 967             opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
 968                 wc.ex.imm_data = packet->ohdr->u.ud.imm_data;
 969                 wc.wc_flags = IB_WC_WITH_IMM;
 970         } else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
 971                 wc.ex.imm_data = 0;
 972                 wc.wc_flags = 0;
 973         } else {
 974                 goto drop;
 975         }
 976 
 977         
 978 
 979 
 980 
 981         wc.byte_len = tlen + sizeof(struct ib_grh);
 982 
 983         
 984 
 985 
 986         if (qp->r_flags & RVT_R_REUSE_SGE) {
 987                 qp->r_flags &= ~RVT_R_REUSE_SGE;
 988         } else {
 989                 int ret;
 990 
 991                 ret = rvt_get_rwqe(qp, false);
 992                 if (ret < 0) {
 993                         rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
 994                         return;
 995                 }
 996                 if (!ret) {
 997                         if (qp->ibqp.qp_num == 0)
 998                                 ibp->rvp.n_vl15_dropped++;
 999                         return;
1000                 }
1001         }
1002         
1003         if (unlikely(wc.byte_len > qp->r_len)) {
1004                 qp->r_flags |= RVT_R_REUSE_SGE;
1005                 goto drop;
1006         }
1007         if (packet->grh) {
1008                 rvt_copy_sge(qp, &qp->r_sge, packet->grh,
1009                              sizeof(struct ib_grh), true, false);
1010                 wc.wc_flags |= IB_WC_GRH;
1011         } else if (packet->etype == RHF_RCV_TYPE_BYPASS) {
1012                 struct ib_grh grh;
1013                 
1014 
1015 
1016 
1017 
1018                 hfi1_make_ext_grh(packet, &grh, slid, dlid);
1019                 rvt_copy_sge(qp, &qp->r_sge, &grh,
1020                              sizeof(struct ib_grh), true, false);
1021                 wc.wc_flags |= IB_WC_GRH;
1022         } else {
1023                 rvt_skip_sge(&qp->r_sge, sizeof(struct ib_grh), true);
1024         }
1025         rvt_copy_sge(qp, &qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh),
1026                      true, false);
1027         rvt_put_ss(&qp->r_sge);
1028         if (!test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags))
1029                 return;
1030         wc.wr_id = qp->r_wr_id;
1031         wc.status = IB_WC_SUCCESS;
1032         wc.opcode = IB_WC_RECV;
1033         wc.vendor_err = 0;
1034         wc.qp = &qp->ibqp;
1035         wc.src_qp = src_qp;
1036 
1037         if (qp->ibqp.qp_type == IB_QPT_GSI ||
1038             qp->ibqp.qp_type == IB_QPT_SMI) {
1039                 if (mgmt_pkey_idx < 0) {
1040                         if (net_ratelimit()) {
1041                                 struct hfi1_devdata *dd = ppd->dd;
1042 
1043                                 dd_dev_err(dd, "QP type %d mgmt_pkey_idx < 0 and packet not dropped???\n",
1044                                            qp->ibqp.qp_type);
1045                                 mgmt_pkey_idx = 0;
1046                         }
1047                 }
1048                 wc.pkey_index = (unsigned)mgmt_pkey_idx;
1049         } else {
1050                 wc.pkey_index = 0;
1051         }
1052         if (slid_is_permissive)
1053                 slid = be32_to_cpu(OPA_LID_PERMISSIVE);
1054         wc.slid = slid & U16_MAX;
1055         wc.sl = sl_from_sc;
1056 
1057         
1058 
1059 
1060         wc.dlid_path_bits = hfi1_check_mcast(dlid) ? 0 :
1061                 dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1);
1062         wc.port_num = qp->port_num;
1063         
1064         rvt_recv_cq(qp, &wc, solicited);
1065         return;
1066 
1067 drop:
1068         ibp->rvp.n_pkt_drops++;
1069 }