Path: blob/master/drivers/infiniband/hw/ipath/ipath_ud.c
15112 views
/*1* Copyright (c) 2006, 2007, 2008 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 <linux/sched.h>34#include <rdma/ib_smi.h>3536#include "ipath_verbs.h"37#include "ipath_kernel.h"3839/**40* ipath_ud_loopback - handle send on loopback QPs41* @sqp: the sending QP42* @swqe: the send work request43*44* This is called from ipath_make_ud_req() to forward a WQE addressed45* to the same HCA.46* Note that the receive interrupt handler may be calling ipath_ud_rcv()47* while this is being called.48*/49static void ipath_ud_loopback(struct ipath_qp *sqp, struct ipath_swqe *swqe)50{51struct ipath_ibdev *dev = to_idev(sqp->ibqp.device);52struct ipath_qp *qp;53struct ib_ah_attr *ah_attr;54unsigned long flags;55struct ipath_rq *rq;56struct ipath_srq *srq;57struct ipath_sge_state rsge;58struct ipath_sge *sge;59struct ipath_rwq *wq;60struct ipath_rwqe *wqe;61void (*handler)(struct ib_event *, void *);62struct ib_wc wc;63u32 tail;64u32 rlen;65u32 length;6667qp = ipath_lookup_qpn(&dev->qp_table, swqe->wr.wr.ud.remote_qpn);68if (!qp || !(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_RECV_OK)) {69dev->n_pkt_drops++;70goto done;71}7273/*74* Check that the qkey matches (except for QP0, see 9.6.1.4.1).75* Qkeys with the high order bit set mean use the76* qkey from the QP context instead of the WR (see 10.2.5).77*/78if (unlikely(qp->ibqp.qp_num &&79((int) swqe->wr.wr.ud.remote_qkey < 0 ?80sqp->qkey : swqe->wr.wr.ud.remote_qkey) != qp->qkey)) {81/* XXX OK to lose a count once in a while. */82dev->qkey_violations++;83dev->n_pkt_drops++;84goto drop;85}8687/*88* A GRH is expected to precede the data even if not89* present on the wire.90*/91length = swqe->length;92memset(&wc, 0, sizeof wc);93wc.byte_len = length + sizeof(struct ib_grh);9495if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) {96wc.wc_flags = IB_WC_WITH_IMM;97wc.ex.imm_data = swqe->wr.ex.imm_data;98}99100/*101* This would be a lot simpler if we could call ipath_get_rwqe()102* but that uses state that the receive interrupt handler uses103* so we would need to lock out receive interrupts while doing104* local loopback.105*/106if (qp->ibqp.srq) {107srq = to_isrq(qp->ibqp.srq);108handler = srq->ibsrq.event_handler;109rq = &srq->rq;110} else {111srq = NULL;112handler = NULL;113rq = &qp->r_rq;114}115116/*117* Get the next work request entry to find where to put the data.118* Note that it is safe to drop the lock after changing rq->tail119* since ipath_post_receive() won't fill the empty slot.120*/121spin_lock_irqsave(&rq->lock, flags);122wq = rq->wq;123tail = wq->tail;124/* Validate tail before using it since it is user writable. */125if (tail >= rq->size)126tail = 0;127if (unlikely(tail == wq->head)) {128spin_unlock_irqrestore(&rq->lock, flags);129dev->n_pkt_drops++;130goto drop;131}132wqe = get_rwqe_ptr(rq, tail);133rsge.sg_list = qp->r_ud_sg_list;134if (!ipath_init_sge(qp, wqe, &rlen, &rsge)) {135spin_unlock_irqrestore(&rq->lock, flags);136dev->n_pkt_drops++;137goto drop;138}139/* Silently drop packets which are too big. */140if (wc.byte_len > rlen) {141spin_unlock_irqrestore(&rq->lock, flags);142dev->n_pkt_drops++;143goto drop;144}145if (++tail >= rq->size)146tail = 0;147wq->tail = tail;148wc.wr_id = wqe->wr_id;149if (handler) {150u32 n;151152/*153* validate head pointer value and compute154* the number of remaining WQEs.155*/156n = wq->head;157if (n >= rq->size)158n = 0;159if (n < tail)160n += rq->size - tail;161else162n -= tail;163if (n < srq->limit) {164struct ib_event ev;165166srq->limit = 0;167spin_unlock_irqrestore(&rq->lock, flags);168ev.device = qp->ibqp.device;169ev.element.srq = qp->ibqp.srq;170ev.event = IB_EVENT_SRQ_LIMIT_REACHED;171handler(&ev, srq->ibsrq.srq_context);172} else173spin_unlock_irqrestore(&rq->lock, flags);174} else175spin_unlock_irqrestore(&rq->lock, flags);176177ah_attr = &to_iah(swqe->wr.wr.ud.ah)->attr;178if (ah_attr->ah_flags & IB_AH_GRH) {179ipath_copy_sge(&rsge, &ah_attr->grh, sizeof(struct ib_grh));180wc.wc_flags |= IB_WC_GRH;181} else182ipath_skip_sge(&rsge, sizeof(struct ib_grh));183sge = swqe->sg_list;184while (length) {185u32 len = sge->length;186187if (len > length)188len = length;189if (len > sge->sge_length)190len = sge->sge_length;191BUG_ON(len == 0);192ipath_copy_sge(&rsge, sge->vaddr, len);193sge->vaddr += len;194sge->length -= len;195sge->sge_length -= len;196if (sge->sge_length == 0) {197if (--swqe->wr.num_sge)198sge++;199} else if (sge->length == 0 && sge->mr != NULL) {200if (++sge->n >= IPATH_SEGSZ) {201if (++sge->m >= sge->mr->mapsz)202break;203sge->n = 0;204}205sge->vaddr =206sge->mr->map[sge->m]->segs[sge->n].vaddr;207sge->length =208sge->mr->map[sge->m]->segs[sge->n].length;209}210length -= len;211}212wc.status = IB_WC_SUCCESS;213wc.opcode = IB_WC_RECV;214wc.qp = &qp->ibqp;215wc.src_qp = sqp->ibqp.qp_num;216/* XXX do we know which pkey matched? Only needed for GSI. */217wc.pkey_index = 0;218wc.slid = dev->dd->ipath_lid |219(ah_attr->src_path_bits &220((1 << dev->dd->ipath_lmc) - 1));221wc.sl = ah_attr->sl;222wc.dlid_path_bits =223ah_attr->dlid & ((1 << dev->dd->ipath_lmc) - 1);224wc.port_num = 1;225/* Signal completion event if the solicited bit is set. */226ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,227swqe->wr.send_flags & IB_SEND_SOLICITED);228drop:229if (atomic_dec_and_test(&qp->refcount))230wake_up(&qp->wait);231done:;232}233234/**235* ipath_make_ud_req - construct a UD request packet236* @qp: the QP237*238* Return 1 if constructed; otherwise, return 0.239*/240int ipath_make_ud_req(struct ipath_qp *qp)241{242struct ipath_ibdev *dev = to_idev(qp->ibqp.device);243struct ipath_other_headers *ohdr;244struct ib_ah_attr *ah_attr;245struct ipath_swqe *wqe;246unsigned long flags;247u32 nwords;248u32 extra_bytes;249u32 bth0;250u16 lrh0;251u16 lid;252int ret = 0;253int next_cur;254255spin_lock_irqsave(&qp->s_lock, flags);256257if (!(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_NEXT_SEND_OK)) {258if (!(ib_ipath_state_ops[qp->state] & IPATH_FLUSH_SEND))259goto bail;260/* We are in the error state, flush the work request. */261if (qp->s_last == qp->s_head)262goto bail;263/* If DMAs are in progress, we can't flush immediately. */264if (atomic_read(&qp->s_dma_busy)) {265qp->s_flags |= IPATH_S_WAIT_DMA;266goto bail;267}268wqe = get_swqe_ptr(qp, qp->s_last);269ipath_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);270goto done;271}272273if (qp->s_cur == qp->s_head)274goto bail;275276wqe = get_swqe_ptr(qp, qp->s_cur);277next_cur = qp->s_cur + 1;278if (next_cur >= qp->s_size)279next_cur = 0;280281/* Construct the header. */282ah_attr = &to_iah(wqe->wr.wr.ud.ah)->attr;283if (ah_attr->dlid >= IPATH_MULTICAST_LID_BASE) {284if (ah_attr->dlid != IPATH_PERMISSIVE_LID)285dev->n_multicast_xmit++;286else287dev->n_unicast_xmit++;288} else {289dev->n_unicast_xmit++;290lid = ah_attr->dlid & ~((1 << dev->dd->ipath_lmc) - 1);291if (unlikely(lid == dev->dd->ipath_lid)) {292/*293* If DMAs are in progress, we can't generate294* a completion for the loopback packet since295* it would be out of order.296* XXX Instead of waiting, we could queue a297* zero length descriptor so we get a callback.298*/299if (atomic_read(&qp->s_dma_busy)) {300qp->s_flags |= IPATH_S_WAIT_DMA;301goto bail;302}303qp->s_cur = next_cur;304spin_unlock_irqrestore(&qp->s_lock, flags);305ipath_ud_loopback(qp, wqe);306spin_lock_irqsave(&qp->s_lock, flags);307ipath_send_complete(qp, wqe, IB_WC_SUCCESS);308goto done;309}310}311312qp->s_cur = next_cur;313extra_bytes = -wqe->length & 3;314nwords = (wqe->length + extra_bytes) >> 2;315316/* header size in 32-bit words LRH+BTH+DETH = (8+12+8)/4. */317qp->s_hdrwords = 7;318qp->s_cur_size = wqe->length;319qp->s_cur_sge = &qp->s_sge;320qp->s_dmult = ah_attr->static_rate;321qp->s_wqe = wqe;322qp->s_sge.sge = wqe->sg_list[0];323qp->s_sge.sg_list = wqe->sg_list + 1;324qp->s_sge.num_sge = wqe->wr.num_sge;325326if (ah_attr->ah_flags & IB_AH_GRH) {327/* Header size in 32-bit words. */328qp->s_hdrwords += ipath_make_grh(dev, &qp->s_hdr.u.l.grh,329&ah_attr->grh,330qp->s_hdrwords, nwords);331lrh0 = IPATH_LRH_GRH;332ohdr = &qp->s_hdr.u.l.oth;333/*334* Don't worry about sending to locally attached multicast335* QPs. It is unspecified by the spec. what happens.336*/337} else {338/* Header size in 32-bit words. */339lrh0 = IPATH_LRH_BTH;340ohdr = &qp->s_hdr.u.oth;341}342if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {343qp->s_hdrwords++;344ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;345bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;346} else347bth0 = IB_OPCODE_UD_SEND_ONLY << 24;348lrh0 |= ah_attr->sl << 4;349if (qp->ibqp.qp_type == IB_QPT_SMI)350lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */351qp->s_hdr.lrh[0] = cpu_to_be16(lrh0);352qp->s_hdr.lrh[1] = cpu_to_be16(ah_attr->dlid); /* DEST LID */353qp->s_hdr.lrh[2] = cpu_to_be16(qp->s_hdrwords + nwords +354SIZE_OF_CRC);355lid = dev->dd->ipath_lid;356if (lid) {357lid |= ah_attr->src_path_bits &358((1 << dev->dd->ipath_lmc) - 1);359qp->s_hdr.lrh[3] = cpu_to_be16(lid);360} else361qp->s_hdr.lrh[3] = IB_LID_PERMISSIVE;362if (wqe->wr.send_flags & IB_SEND_SOLICITED)363bth0 |= 1 << 23;364bth0 |= extra_bytes << 20;365bth0 |= qp->ibqp.qp_type == IB_QPT_SMI ? IPATH_DEFAULT_P_KEY :366ipath_get_pkey(dev->dd, qp->s_pkey_index);367ohdr->bth[0] = cpu_to_be32(bth0);368/*369* Use the multicast QP if the destination LID is a multicast LID.370*/371ohdr->bth[1] = ah_attr->dlid >= IPATH_MULTICAST_LID_BASE &&372ah_attr->dlid != IPATH_PERMISSIVE_LID ?373cpu_to_be32(IPATH_MULTICAST_QPN) :374cpu_to_be32(wqe->wr.wr.ud.remote_qpn);375ohdr->bth[2] = cpu_to_be32(qp->s_next_psn++ & IPATH_PSN_MASK);376/*377* Qkeys with the high order bit set mean use the378* qkey from the QP context instead of the WR (see 10.2.5).379*/380ohdr->u.ud.deth[0] = cpu_to_be32((int)wqe->wr.wr.ud.remote_qkey < 0 ?381qp->qkey : wqe->wr.wr.ud.remote_qkey);382ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);383384done:385ret = 1;386goto unlock;387388bail:389qp->s_flags &= ~IPATH_S_BUSY;390unlock:391spin_unlock_irqrestore(&qp->s_lock, flags);392return ret;393}394395/**396* ipath_ud_rcv - receive an incoming UD packet397* @dev: the device the packet came in on398* @hdr: the packet header399* @has_grh: true if the packet has a GRH400* @data: the packet data401* @tlen: the packet length402* @qp: the QP the packet came on403*404* This is called from ipath_qp_rcv() to process an incoming UD packet405* for the given QP.406* Called at interrupt level.407*/408void ipath_ud_rcv(struct ipath_ibdev *dev, struct ipath_ib_header *hdr,409int has_grh, void *data, u32 tlen, struct ipath_qp *qp)410{411struct ipath_other_headers *ohdr;412int opcode;413u32 hdrsize;414u32 pad;415struct ib_wc wc;416u32 qkey;417u32 src_qp;418u16 dlid;419int header_in_data;420421/* Check for GRH */422if (!has_grh) {423ohdr = &hdr->u.oth;424hdrsize = 8 + 12 + 8; /* LRH + BTH + DETH */425qkey = be32_to_cpu(ohdr->u.ud.deth[0]);426src_qp = be32_to_cpu(ohdr->u.ud.deth[1]);427header_in_data = 0;428} else {429ohdr = &hdr->u.l.oth;430hdrsize = 8 + 40 + 12 + 8; /* LRH + GRH + BTH + DETH */431/*432* The header with GRH is 68 bytes and the core driver sets433* the eager header buffer size to 56 bytes so the last 12434* bytes of the IB header is in the data buffer.435*/436header_in_data = dev->dd->ipath_rcvhdrentsize == 16;437if (header_in_data) {438qkey = be32_to_cpu(((__be32 *) data)[1]);439src_qp = be32_to_cpu(((__be32 *) data)[2]);440data += 12;441} else {442qkey = be32_to_cpu(ohdr->u.ud.deth[0]);443src_qp = be32_to_cpu(ohdr->u.ud.deth[1]);444}445}446src_qp &= IPATH_QPN_MASK;447448/*449* Check that the permissive LID is only used on QP0450* and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).451*/452if (qp->ibqp.qp_num) {453if (unlikely(hdr->lrh[1] == IB_LID_PERMISSIVE ||454hdr->lrh[3] == IB_LID_PERMISSIVE)) {455dev->n_pkt_drops++;456goto bail;457}458if (unlikely(qkey != qp->qkey)) {459/* XXX OK to lose a count once in a while. */460dev->qkey_violations++;461dev->n_pkt_drops++;462goto bail;463}464} else if (hdr->lrh[1] == IB_LID_PERMISSIVE ||465hdr->lrh[3] == IB_LID_PERMISSIVE) {466struct ib_smp *smp = (struct ib_smp *) data;467468if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {469dev->n_pkt_drops++;470goto bail;471}472}473474/*475* The opcode is in the low byte when its in network order476* (top byte when in host order).477*/478opcode = be32_to_cpu(ohdr->bth[0]) >> 24;479if (qp->ibqp.qp_num > 1 &&480opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {481if (header_in_data) {482wc.ex.imm_data = *(__be32 *) data;483data += sizeof(__be32);484} else485wc.ex.imm_data = ohdr->u.ud.imm_data;486wc.wc_flags = IB_WC_WITH_IMM;487hdrsize += sizeof(u32);488} else if (opcode == IB_OPCODE_UD_SEND_ONLY) {489wc.ex.imm_data = 0;490wc.wc_flags = 0;491} else {492dev->n_pkt_drops++;493goto bail;494}495496/* Get the number of bytes the message was padded by. */497pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;498if (unlikely(tlen < (hdrsize + pad + 4))) {499/* Drop incomplete packets. */500dev->n_pkt_drops++;501goto bail;502}503tlen -= hdrsize + pad + 4;504505/* Drop invalid MAD packets (see 13.5.3.1). */506if (unlikely((qp->ibqp.qp_num == 0 &&507(tlen != 256 ||508(be16_to_cpu(hdr->lrh[0]) >> 12) != 15)) ||509(qp->ibqp.qp_num == 1 &&510(tlen != 256 ||511(be16_to_cpu(hdr->lrh[0]) >> 12) == 15)))) {512dev->n_pkt_drops++;513goto bail;514}515516/*517* A GRH is expected to precede the data even if not518* present on the wire.519*/520wc.byte_len = tlen + sizeof(struct ib_grh);521522/*523* Get the next work request entry to find where to put the data.524*/525if (qp->r_flags & IPATH_R_REUSE_SGE)526qp->r_flags &= ~IPATH_R_REUSE_SGE;527else if (!ipath_get_rwqe(qp, 0)) {528/*529* Count VL15 packets dropped due to no receive buffer.530* Otherwise, count them as buffer overruns since usually,531* the HW will be able to receive packets even if there are532* no QPs with posted receive buffers.533*/534if (qp->ibqp.qp_num == 0)535dev->n_vl15_dropped++;536else537dev->rcv_errors++;538goto bail;539}540/* Silently drop packets which are too big. */541if (wc.byte_len > qp->r_len) {542qp->r_flags |= IPATH_R_REUSE_SGE;543dev->n_pkt_drops++;544goto bail;545}546if (has_grh) {547ipath_copy_sge(&qp->r_sge, &hdr->u.l.grh,548sizeof(struct ib_grh));549wc.wc_flags |= IB_WC_GRH;550} else551ipath_skip_sge(&qp->r_sge, sizeof(struct ib_grh));552ipath_copy_sge(&qp->r_sge, data,553wc.byte_len - sizeof(struct ib_grh));554if (!test_and_clear_bit(IPATH_R_WRID_VALID, &qp->r_aflags))555goto bail;556wc.wr_id = qp->r_wr_id;557wc.status = IB_WC_SUCCESS;558wc.opcode = IB_WC_RECV;559wc.vendor_err = 0;560wc.qp = &qp->ibqp;561wc.src_qp = src_qp;562/* XXX do we know which pkey matched? Only needed for GSI. */563wc.pkey_index = 0;564wc.slid = be16_to_cpu(hdr->lrh[3]);565wc.sl = (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF;566dlid = be16_to_cpu(hdr->lrh[1]);567/*568* Save the LMC lower bits if the destination LID is a unicast LID.569*/570wc.dlid_path_bits = dlid >= IPATH_MULTICAST_LID_BASE ? 0 :571dlid & ((1 << dev->dd->ipath_lmc) - 1);572wc.port_num = 1;573/* Signal completion event if the solicited bit is set. */574ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,575(ohdr->bth[0] &576cpu_to_be32(1 << 23)) != 0);577578bail:;579}580581582