Path: blob/master/drivers/infiniband/hw/qib/qib_ud.c
15112 views
/*1* Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.2* Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.3*4* This software is available to you under a choice of one of two5* licenses. You may choose to be licensed under the terms of the GNU6* General Public License (GPL) Version 2, available from the file7* COPYING in the main directory of this source tree, or the8* OpenIB.org BSD license below:9*10* Redistribution and use in source and binary forms, with or11* without modification, are permitted provided that the following12* conditions are met:13*14* - Redistributions of source code must retain the above15* copyright notice, this list of conditions and the following16* disclaimer.17*18* - Redistributions in binary form must reproduce the above19* copyright notice, this list of conditions and the following20* disclaimer in the documentation and/or other materials21* provided with the distribution.22*23* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,24* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF25* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND26* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS27* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN28* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN29* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE30* SOFTWARE.31*/3233#include <rdma/ib_smi.h>3435#include "qib.h"36#include "qib_mad.h"3738/**39* qib_ud_loopback - handle send on loopback QPs40* @sqp: the sending QP41* @swqe: the send work request42*43* This is called from qib_make_ud_req() to forward a WQE addressed44* to the same HCA.45* Note that the receive interrupt handler may be calling qib_ud_rcv()46* while this is being called.47*/48static void qib_ud_loopback(struct qib_qp *sqp, struct qib_swqe *swqe)49{50struct qib_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);51struct qib_pportdata *ppd;52struct qib_qp *qp;53struct ib_ah_attr *ah_attr;54unsigned long flags;55struct qib_sge_state ssge;56struct qib_sge *sge;57struct ib_wc wc;58u32 length;5960qp = qib_lookup_qpn(ibp, swqe->wr.wr.ud.remote_qpn);61if (!qp) {62ibp->n_pkt_drops++;63return;64}65if (qp->ibqp.qp_type != sqp->ibqp.qp_type ||66!(ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK)) {67ibp->n_pkt_drops++;68goto drop;69}7071ah_attr = &to_iah(swqe->wr.wr.ud.ah)->attr;72ppd = ppd_from_ibp(ibp);7374if (qp->ibqp.qp_num > 1) {75u16 pkey1;76u16 pkey2;77u16 lid;7879pkey1 = qib_get_pkey(ibp, sqp->s_pkey_index);80pkey2 = qib_get_pkey(ibp, qp->s_pkey_index);81if (unlikely(!qib_pkey_ok(pkey1, pkey2))) {82lid = ppd->lid | (ah_attr->src_path_bits &83((1 << ppd->lmc) - 1));84qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_PKEY, pkey1,85ah_attr->sl,86sqp->ibqp.qp_num, qp->ibqp.qp_num,87cpu_to_be16(lid),88cpu_to_be16(ah_attr->dlid));89goto drop;90}91}9293/*94* Check that the qkey matches (except for QP0, see 9.6.1.4.1).95* Qkeys with the high order bit set mean use the96* qkey from the QP context instead of the WR (see 10.2.5).97*/98if (qp->ibqp.qp_num) {99u32 qkey;100101qkey = (int)swqe->wr.wr.ud.remote_qkey < 0 ?102sqp->qkey : swqe->wr.wr.ud.remote_qkey;103if (unlikely(qkey != qp->qkey)) {104u16 lid;105106lid = ppd->lid | (ah_attr->src_path_bits &107((1 << ppd->lmc) - 1));108qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_QKEY, qkey,109ah_attr->sl,110sqp->ibqp.qp_num, qp->ibqp.qp_num,111cpu_to_be16(lid),112cpu_to_be16(ah_attr->dlid));113goto drop;114}115}116117/*118* A GRH is expected to precede the data even if not119* present on the wire.120*/121length = swqe->length;122memset(&wc, 0, sizeof wc);123wc.byte_len = length + sizeof(struct ib_grh);124125if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) {126wc.wc_flags = IB_WC_WITH_IMM;127wc.ex.imm_data = swqe->wr.ex.imm_data;128}129130spin_lock_irqsave(&qp->r_lock, flags);131132/*133* Get the next work request entry to find where to put the data.134*/135if (qp->r_flags & QIB_R_REUSE_SGE)136qp->r_flags &= ~QIB_R_REUSE_SGE;137else {138int ret;139140ret = qib_get_rwqe(qp, 0);141if (ret < 0) {142qib_rc_error(qp, IB_WC_LOC_QP_OP_ERR);143goto bail_unlock;144}145if (!ret) {146if (qp->ibqp.qp_num == 0)147ibp->n_vl15_dropped++;148goto bail_unlock;149}150}151/* Silently drop packets which are too big. */152if (unlikely(wc.byte_len > qp->r_len)) {153qp->r_flags |= QIB_R_REUSE_SGE;154ibp->n_pkt_drops++;155goto bail_unlock;156}157158if (ah_attr->ah_flags & IB_AH_GRH) {159qib_copy_sge(&qp->r_sge, &ah_attr->grh,160sizeof(struct ib_grh), 1);161wc.wc_flags |= IB_WC_GRH;162} else163qib_skip_sge(&qp->r_sge, sizeof(struct ib_grh), 1);164ssge.sg_list = swqe->sg_list + 1;165ssge.sge = *swqe->sg_list;166ssge.num_sge = swqe->wr.num_sge;167sge = &ssge.sge;168while (length) {169u32 len = sge->length;170171if (len > length)172len = length;173if (len > sge->sge_length)174len = sge->sge_length;175BUG_ON(len == 0);176qib_copy_sge(&qp->r_sge, sge->vaddr, len, 1);177sge->vaddr += len;178sge->length -= len;179sge->sge_length -= len;180if (sge->sge_length == 0) {181if (--ssge.num_sge)182*sge = *ssge.sg_list++;183} else if (sge->length == 0 && sge->mr->lkey) {184if (++sge->n >= QIB_SEGSZ) {185if (++sge->m >= sge->mr->mapsz)186break;187sge->n = 0;188}189sge->vaddr =190sge->mr->map[sge->m]->segs[sge->n].vaddr;191sge->length =192sge->mr->map[sge->m]->segs[sge->n].length;193}194length -= len;195}196while (qp->r_sge.num_sge) {197atomic_dec(&qp->r_sge.sge.mr->refcount);198if (--qp->r_sge.num_sge)199qp->r_sge.sge = *qp->r_sge.sg_list++;200}201if (!test_and_clear_bit(QIB_R_WRID_VALID, &qp->r_aflags))202goto bail_unlock;203wc.wr_id = qp->r_wr_id;204wc.status = IB_WC_SUCCESS;205wc.opcode = IB_WC_RECV;206wc.qp = &qp->ibqp;207wc.src_qp = sqp->ibqp.qp_num;208wc.pkey_index = qp->ibqp.qp_type == IB_QPT_GSI ?209swqe->wr.wr.ud.pkey_index : 0;210wc.slid = ppd->lid | (ah_attr->src_path_bits & ((1 << ppd->lmc) - 1));211wc.sl = ah_attr->sl;212wc.dlid_path_bits = ah_attr->dlid & ((1 << ppd->lmc) - 1);213wc.port_num = qp->port_num;214/* Signal completion event if the solicited bit is set. */215qib_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,216swqe->wr.send_flags & IB_SEND_SOLICITED);217ibp->n_loop_pkts++;218bail_unlock:219spin_unlock_irqrestore(&qp->r_lock, flags);220drop:221if (atomic_dec_and_test(&qp->refcount))222wake_up(&qp->wait);223}224225/**226* qib_make_ud_req - construct a UD request packet227* @qp: the QP228*229* Return 1 if constructed; otherwise, return 0.230*/231int qib_make_ud_req(struct qib_qp *qp)232{233struct qib_other_headers *ohdr;234struct ib_ah_attr *ah_attr;235struct qib_pportdata *ppd;236struct qib_ibport *ibp;237struct qib_swqe *wqe;238unsigned long flags;239u32 nwords;240u32 extra_bytes;241u32 bth0;242u16 lrh0;243u16 lid;244int ret = 0;245int next_cur;246247spin_lock_irqsave(&qp->s_lock, flags);248249if (!(ib_qib_state_ops[qp->state] & QIB_PROCESS_NEXT_SEND_OK)) {250if (!(ib_qib_state_ops[qp->state] & QIB_FLUSH_SEND))251goto bail;252/* We are in the error state, flush the work request. */253if (qp->s_last == qp->s_head)254goto bail;255/* If DMAs are in progress, we can't flush immediately. */256if (atomic_read(&qp->s_dma_busy)) {257qp->s_flags |= QIB_S_WAIT_DMA;258goto bail;259}260wqe = get_swqe_ptr(qp, qp->s_last);261qib_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);262goto done;263}264265if (qp->s_cur == qp->s_head)266goto bail;267268wqe = get_swqe_ptr(qp, qp->s_cur);269next_cur = qp->s_cur + 1;270if (next_cur >= qp->s_size)271next_cur = 0;272273/* Construct the header. */274ibp = to_iport(qp->ibqp.device, qp->port_num);275ppd = ppd_from_ibp(ibp);276ah_attr = &to_iah(wqe->wr.wr.ud.ah)->attr;277if (ah_attr->dlid >= QIB_MULTICAST_LID_BASE) {278if (ah_attr->dlid != QIB_PERMISSIVE_LID)279ibp->n_multicast_xmit++;280else281ibp->n_unicast_xmit++;282} else {283ibp->n_unicast_xmit++;284lid = ah_attr->dlid & ~((1 << ppd->lmc) - 1);285if (unlikely(lid == ppd->lid)) {286/*287* If DMAs are in progress, we can't generate288* a completion for the loopback packet since289* it would be out of order.290* XXX Instead of waiting, we could queue a291* zero length descriptor so we get a callback.292*/293if (atomic_read(&qp->s_dma_busy)) {294qp->s_flags |= QIB_S_WAIT_DMA;295goto bail;296}297qp->s_cur = next_cur;298spin_unlock_irqrestore(&qp->s_lock, flags);299qib_ud_loopback(qp, wqe);300spin_lock_irqsave(&qp->s_lock, flags);301qib_send_complete(qp, wqe, IB_WC_SUCCESS);302goto done;303}304}305306qp->s_cur = next_cur;307extra_bytes = -wqe->length & 3;308nwords = (wqe->length + extra_bytes) >> 2;309310/* header size in 32-bit words LRH+BTH+DETH = (8+12+8)/4. */311qp->s_hdrwords = 7;312qp->s_cur_size = wqe->length;313qp->s_cur_sge = &qp->s_sge;314qp->s_srate = ah_attr->static_rate;315qp->s_wqe = wqe;316qp->s_sge.sge = wqe->sg_list[0];317qp->s_sge.sg_list = wqe->sg_list + 1;318qp->s_sge.num_sge = wqe->wr.num_sge;319qp->s_sge.total_len = wqe->length;320321if (ah_attr->ah_flags & IB_AH_GRH) {322/* Header size in 32-bit words. */323qp->s_hdrwords += qib_make_grh(ibp, &qp->s_hdr.u.l.grh,324&ah_attr->grh,325qp->s_hdrwords, nwords);326lrh0 = QIB_LRH_GRH;327ohdr = &qp->s_hdr.u.l.oth;328/*329* Don't worry about sending to locally attached multicast330* QPs. It is unspecified by the spec. what happens.331*/332} else {333/* Header size in 32-bit words. */334lrh0 = QIB_LRH_BTH;335ohdr = &qp->s_hdr.u.oth;336}337if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {338qp->s_hdrwords++;339ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;340bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;341} else342bth0 = IB_OPCODE_UD_SEND_ONLY << 24;343lrh0 |= ah_attr->sl << 4;344if (qp->ibqp.qp_type == IB_QPT_SMI)345lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */346else347lrh0 |= ibp->sl_to_vl[ah_attr->sl] << 12;348qp->s_hdr.lrh[0] = cpu_to_be16(lrh0);349qp->s_hdr.lrh[1] = cpu_to_be16(ah_attr->dlid); /* DEST LID */350qp->s_hdr.lrh[2] = cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);351lid = ppd->lid;352if (lid) {353lid |= ah_attr->src_path_bits & ((1 << ppd->lmc) - 1);354qp->s_hdr.lrh[3] = cpu_to_be16(lid);355} else356qp->s_hdr.lrh[3] = IB_LID_PERMISSIVE;357if (wqe->wr.send_flags & IB_SEND_SOLICITED)358bth0 |= IB_BTH_SOLICITED;359bth0 |= extra_bytes << 20;360bth0 |= qp->ibqp.qp_type == IB_QPT_SMI ? QIB_DEFAULT_P_KEY :361qib_get_pkey(ibp, qp->ibqp.qp_type == IB_QPT_GSI ?362wqe->wr.wr.ud.pkey_index : qp->s_pkey_index);363ohdr->bth[0] = cpu_to_be32(bth0);364/*365* Use the multicast QP if the destination LID is a multicast LID.366*/367ohdr->bth[1] = ah_attr->dlid >= QIB_MULTICAST_LID_BASE &&368ah_attr->dlid != QIB_PERMISSIVE_LID ?369cpu_to_be32(QIB_MULTICAST_QPN) :370cpu_to_be32(wqe->wr.wr.ud.remote_qpn);371ohdr->bth[2] = cpu_to_be32(qp->s_next_psn++ & QIB_PSN_MASK);372/*373* Qkeys with the high order bit set mean use the374* qkey from the QP context instead of the WR (see 10.2.5).375*/376ohdr->u.ud.deth[0] = cpu_to_be32((int)wqe->wr.wr.ud.remote_qkey < 0 ?377qp->qkey : wqe->wr.wr.ud.remote_qkey);378ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);379380done:381ret = 1;382goto unlock;383384bail:385qp->s_flags &= ~QIB_S_BUSY;386unlock:387spin_unlock_irqrestore(&qp->s_lock, flags);388return ret;389}390391static unsigned qib_lookup_pkey(struct qib_ibport *ibp, u16 pkey)392{393struct qib_pportdata *ppd = ppd_from_ibp(ibp);394struct qib_devdata *dd = ppd->dd;395unsigned ctxt = ppd->hw_pidx;396unsigned i;397398pkey &= 0x7fff; /* remove limited/full membership bit */399400for (i = 0; i < ARRAY_SIZE(dd->rcd[ctxt]->pkeys); ++i)401if ((dd->rcd[ctxt]->pkeys[i] & 0x7fff) == pkey)402return i;403404/*405* Should not get here, this means hardware failed to validate pkeys.406* Punt and return index 0.407*/408return 0;409}410411/**412* qib_ud_rcv - receive an incoming UD packet413* @ibp: the port the packet came in on414* @hdr: the packet header415* @has_grh: true if the packet has a GRH416* @data: the packet data417* @tlen: the packet length418* @qp: the QP the packet came on419*420* This is called from qib_qp_rcv() to process an incoming UD packet421* for the given QP.422* Called at interrupt level.423*/424void qib_ud_rcv(struct qib_ibport *ibp, struct qib_ib_header *hdr,425int has_grh, void *data, u32 tlen, struct qib_qp *qp)426{427struct qib_other_headers *ohdr;428int opcode;429u32 hdrsize;430u32 pad;431struct ib_wc wc;432u32 qkey;433u32 src_qp;434u16 dlid;435436/* Check for GRH */437if (!has_grh) {438ohdr = &hdr->u.oth;439hdrsize = 8 + 12 + 8; /* LRH + BTH + DETH */440} else {441ohdr = &hdr->u.l.oth;442hdrsize = 8 + 40 + 12 + 8; /* LRH + GRH + BTH + DETH */443}444qkey = be32_to_cpu(ohdr->u.ud.deth[0]);445src_qp = be32_to_cpu(ohdr->u.ud.deth[1]) & QIB_QPN_MASK;446447/*448* Get the number of bytes the message was padded by449* and drop incomplete packets.450*/451pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;452if (unlikely(tlen < (hdrsize + pad + 4)))453goto drop;454455tlen -= hdrsize + pad + 4;456457/*458* Check that the permissive LID is only used on QP0459* and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).460*/461if (qp->ibqp.qp_num) {462if (unlikely(hdr->lrh[1] == IB_LID_PERMISSIVE ||463hdr->lrh[3] == IB_LID_PERMISSIVE))464goto drop;465if (qp->ibqp.qp_num > 1) {466u16 pkey1, pkey2;467468pkey1 = be32_to_cpu(ohdr->bth[0]);469pkey2 = qib_get_pkey(ibp, qp->s_pkey_index);470if (unlikely(!qib_pkey_ok(pkey1, pkey2))) {471qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_PKEY,472pkey1,473(be16_to_cpu(hdr->lrh[0]) >> 4) &4740xF,475src_qp, qp->ibqp.qp_num,476hdr->lrh[3], hdr->lrh[1]);477return;478}479}480if (unlikely(qkey != qp->qkey)) {481qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_QKEY, qkey,482(be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,483src_qp, qp->ibqp.qp_num,484hdr->lrh[3], hdr->lrh[1]);485return;486}487/* Drop invalid MAD packets (see 13.5.3.1). */488if (unlikely(qp->ibqp.qp_num == 1 &&489(tlen != 256 ||490(be16_to_cpu(hdr->lrh[0]) >> 12) == 15)))491goto drop;492} else {493struct ib_smp *smp;494495/* Drop invalid MAD packets (see 13.5.3.1). */496if (tlen != 256 || (be16_to_cpu(hdr->lrh[0]) >> 12) != 15)497goto drop;498smp = (struct ib_smp *) data;499if ((hdr->lrh[1] == IB_LID_PERMISSIVE ||500hdr->lrh[3] == IB_LID_PERMISSIVE) &&501smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)502goto drop;503}504505/*506* The opcode is in the low byte when its in network order507* (top byte when in host order).508*/509opcode = be32_to_cpu(ohdr->bth[0]) >> 24;510if (qp->ibqp.qp_num > 1 &&511opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {512wc.ex.imm_data = ohdr->u.ud.imm_data;513wc.wc_flags = IB_WC_WITH_IMM;514tlen -= sizeof(u32);515} else if (opcode == IB_OPCODE_UD_SEND_ONLY) {516wc.ex.imm_data = 0;517wc.wc_flags = 0;518} else519goto drop;520521/*522* A GRH is expected to precede the data even if not523* present on the wire.524*/525wc.byte_len = tlen + sizeof(struct ib_grh);526527/*528* Get the next work request entry to find where to put the data.529*/530if (qp->r_flags & QIB_R_REUSE_SGE)531qp->r_flags &= ~QIB_R_REUSE_SGE;532else {533int ret;534535ret = qib_get_rwqe(qp, 0);536if (ret < 0) {537qib_rc_error(qp, IB_WC_LOC_QP_OP_ERR);538return;539}540if (!ret) {541if (qp->ibqp.qp_num == 0)542ibp->n_vl15_dropped++;543return;544}545}546/* Silently drop packets which are too big. */547if (unlikely(wc.byte_len > qp->r_len)) {548qp->r_flags |= QIB_R_REUSE_SGE;549goto drop;550}551if (has_grh) {552qib_copy_sge(&qp->r_sge, &hdr->u.l.grh,553sizeof(struct ib_grh), 1);554wc.wc_flags |= IB_WC_GRH;555} else556qib_skip_sge(&qp->r_sge, sizeof(struct ib_grh), 1);557qib_copy_sge(&qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh), 1);558while (qp->r_sge.num_sge) {559atomic_dec(&qp->r_sge.sge.mr->refcount);560if (--qp->r_sge.num_sge)561qp->r_sge.sge = *qp->r_sge.sg_list++;562}563if (!test_and_clear_bit(QIB_R_WRID_VALID, &qp->r_aflags))564return;565wc.wr_id = qp->r_wr_id;566wc.status = IB_WC_SUCCESS;567wc.opcode = IB_WC_RECV;568wc.vendor_err = 0;569wc.qp = &qp->ibqp;570wc.src_qp = src_qp;571wc.pkey_index = qp->ibqp.qp_type == IB_QPT_GSI ?572qib_lookup_pkey(ibp, be32_to_cpu(ohdr->bth[0])) : 0;573wc.slid = be16_to_cpu(hdr->lrh[3]);574wc.sl = (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF;575dlid = be16_to_cpu(hdr->lrh[1]);576/*577* Save the LMC lower bits if the destination LID is a unicast LID.578*/579wc.dlid_path_bits = dlid >= QIB_MULTICAST_LID_BASE ? 0 :580dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1);581wc.port_num = qp->port_num;582/* Signal completion event if the solicited bit is set. */583qib_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,584(ohdr->bth[0] &585cpu_to_be32(IB_BTH_SOLICITED)) != 0);586return;587588drop:589ibp->n_pkt_drops++;590}591592593