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