Path: blob/master/drivers/infiniband/hw/qib/qib_ruc.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 <linux/spinlock.h>3435#include "qib.h"36#include "qib_mad.h"3738/*39* Convert the AETH RNR timeout code into the number of microseconds.40*/41const u32 ib_qib_rnr_table[32] = {42655360, /* 00: 655.36 */4310, /* 01: .01 */4420, /* 02 .02 */4530, /* 03: .03 */4640, /* 04: .04 */4760, /* 05: .06 */4880, /* 06: .08 */49120, /* 07: .12 */50160, /* 08: .16 */51240, /* 09: .24 */52320, /* 0A: .32 */53480, /* 0B: .48 */54640, /* 0C: .64 */55960, /* 0D: .96 */561280, /* 0E: 1.28 */571920, /* 0F: 1.92 */582560, /* 10: 2.56 */593840, /* 11: 3.84 */605120, /* 12: 5.12 */617680, /* 13: 7.68 */6210240, /* 14: 10.24 */6315360, /* 15: 15.36 */6420480, /* 16: 20.48 */6530720, /* 17: 30.72 */6640960, /* 18: 40.96 */6761440, /* 19: 61.44 */6881920, /* 1A: 81.92 */69122880, /* 1B: 122.88 */70163840, /* 1C: 163.84 */71245760, /* 1D: 245.76 */72327680, /* 1E: 327.68 */73491520 /* 1F: 491.52 */74};7576/*77* Validate a RWQE and fill in the SGE state.78* Return 1 if OK.79*/80static int qib_init_sge(struct qib_qp *qp, struct qib_rwqe *wqe)81{82int i, j, ret;83struct ib_wc wc;84struct qib_lkey_table *rkt;85struct qib_pd *pd;86struct qib_sge_state *ss;8788rkt = &to_idev(qp->ibqp.device)->lk_table;89pd = to_ipd(qp->ibqp.srq ? qp->ibqp.srq->pd : qp->ibqp.pd);90ss = &qp->r_sge;91ss->sg_list = qp->r_sg_list;92qp->r_len = 0;93for (i = j = 0; i < wqe->num_sge; i++) {94if (wqe->sg_list[i].length == 0)95continue;96/* Check LKEY */97if (!qib_lkey_ok(rkt, pd, j ? &ss->sg_list[j - 1] : &ss->sge,98&wqe->sg_list[i], IB_ACCESS_LOCAL_WRITE))99goto bad_lkey;100qp->r_len += wqe->sg_list[i].length;101j++;102}103ss->num_sge = j;104ss->total_len = qp->r_len;105ret = 1;106goto bail;107108bad_lkey:109while (j) {110struct qib_sge *sge = --j ? &ss->sg_list[j - 1] : &ss->sge;111112atomic_dec(&sge->mr->refcount);113}114ss->num_sge = 0;115memset(&wc, 0, sizeof(wc));116wc.wr_id = wqe->wr_id;117wc.status = IB_WC_LOC_PROT_ERR;118wc.opcode = IB_WC_RECV;119wc.qp = &qp->ibqp;120/* Signal solicited completion event. */121qib_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);122ret = 0;123bail:124return ret;125}126127/**128* qib_get_rwqe - copy the next RWQE into the QP's RWQE129* @qp: the QP130* @wr_id_only: update qp->r_wr_id only, not qp->r_sge131*132* Return -1 if there is a local error, 0 if no RWQE is available,133* otherwise return 1.134*135* Can be called from interrupt level.136*/137int qib_get_rwqe(struct qib_qp *qp, int wr_id_only)138{139unsigned long flags;140struct qib_rq *rq;141struct qib_rwq *wq;142struct qib_srq *srq;143struct qib_rwqe *wqe;144void (*handler)(struct ib_event *, void *);145u32 tail;146int ret;147148if (qp->ibqp.srq) {149srq = to_isrq(qp->ibqp.srq);150handler = srq->ibsrq.event_handler;151rq = &srq->rq;152} else {153srq = NULL;154handler = NULL;155rq = &qp->r_rq;156}157158spin_lock_irqsave(&rq->lock, flags);159if (!(ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK)) {160ret = 0;161goto unlock;162}163164wq = rq->wq;165tail = wq->tail;166/* Validate tail before using it since it is user writable. */167if (tail >= rq->size)168tail = 0;169if (unlikely(tail == wq->head)) {170ret = 0;171goto unlock;172}173/* Make sure entry is read after head index is read. */174smp_rmb();175wqe = get_rwqe_ptr(rq, tail);176/*177* Even though we update the tail index in memory, the verbs178* consumer is not supposed to post more entries until a179* completion is generated.180*/181if (++tail >= rq->size)182tail = 0;183wq->tail = tail;184if (!wr_id_only && !qib_init_sge(qp, wqe)) {185ret = -1;186goto unlock;187}188qp->r_wr_id = wqe->wr_id;189190ret = 1;191set_bit(QIB_R_WRID_VALID, &qp->r_aflags);192if (handler) {193u32 n;194195/*196* Validate head pointer value and compute197* the number of remaining WQEs.198*/199n = wq->head;200if (n >= rq->size)201n = 0;202if (n < tail)203n += rq->size - tail;204else205n -= tail;206if (n < srq->limit) {207struct ib_event ev;208209srq->limit = 0;210spin_unlock_irqrestore(&rq->lock, flags);211ev.device = qp->ibqp.device;212ev.element.srq = qp->ibqp.srq;213ev.event = IB_EVENT_SRQ_LIMIT_REACHED;214handler(&ev, srq->ibsrq.srq_context);215goto bail;216}217}218unlock:219spin_unlock_irqrestore(&rq->lock, flags);220bail:221return ret;222}223224/*225* Switch to alternate path.226* The QP s_lock should be held and interrupts disabled.227*/228void qib_migrate_qp(struct qib_qp *qp)229{230struct ib_event ev;231232qp->s_mig_state = IB_MIG_MIGRATED;233qp->remote_ah_attr = qp->alt_ah_attr;234qp->port_num = qp->alt_ah_attr.port_num;235qp->s_pkey_index = qp->s_alt_pkey_index;236237ev.device = qp->ibqp.device;238ev.element.qp = &qp->ibqp;239ev.event = IB_EVENT_PATH_MIG;240qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);241}242243static __be64 get_sguid(struct qib_ibport *ibp, unsigned index)244{245if (!index) {246struct qib_pportdata *ppd = ppd_from_ibp(ibp);247248return ppd->guid;249} else250return ibp->guids[index - 1];251}252253static int gid_ok(union ib_gid *gid, __be64 gid_prefix, __be64 id)254{255return (gid->global.interface_id == id &&256(gid->global.subnet_prefix == gid_prefix ||257gid->global.subnet_prefix == IB_DEFAULT_GID_PREFIX));258}259260/*261*262* This should be called with the QP s_lock held.263*/264int qib_ruc_check_hdr(struct qib_ibport *ibp, struct qib_ib_header *hdr,265int has_grh, struct qib_qp *qp, u32 bth0)266{267__be64 guid;268269if (qp->s_mig_state == IB_MIG_ARMED && (bth0 & IB_BTH_MIG_REQ)) {270if (!has_grh) {271if (qp->alt_ah_attr.ah_flags & IB_AH_GRH)272goto err;273} else {274if (!(qp->alt_ah_attr.ah_flags & IB_AH_GRH))275goto err;276guid = get_sguid(ibp, qp->alt_ah_attr.grh.sgid_index);277if (!gid_ok(&hdr->u.l.grh.dgid, ibp->gid_prefix, guid))278goto err;279if (!gid_ok(&hdr->u.l.grh.sgid,280qp->alt_ah_attr.grh.dgid.global.subnet_prefix,281qp->alt_ah_attr.grh.dgid.global.interface_id))282goto err;283}284if (!qib_pkey_ok((u16)bth0,285qib_get_pkey(ibp, qp->s_alt_pkey_index))) {286qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_PKEY,287(u16)bth0,288(be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,2890, qp->ibqp.qp_num,290hdr->lrh[3], hdr->lrh[1]);291goto err;292}293/* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */294if (be16_to_cpu(hdr->lrh[3]) != qp->alt_ah_attr.dlid ||295ppd_from_ibp(ibp)->port != qp->alt_ah_attr.port_num)296goto err;297qib_migrate_qp(qp);298} else {299if (!has_grh) {300if (qp->remote_ah_attr.ah_flags & IB_AH_GRH)301goto err;302} else {303if (!(qp->remote_ah_attr.ah_flags & IB_AH_GRH))304goto err;305guid = get_sguid(ibp,306qp->remote_ah_attr.grh.sgid_index);307if (!gid_ok(&hdr->u.l.grh.dgid, ibp->gid_prefix, guid))308goto err;309if (!gid_ok(&hdr->u.l.grh.sgid,310qp->remote_ah_attr.grh.dgid.global.subnet_prefix,311qp->remote_ah_attr.grh.dgid.global.interface_id))312goto err;313}314if (!qib_pkey_ok((u16)bth0,315qib_get_pkey(ibp, qp->s_pkey_index))) {316qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_PKEY,317(u16)bth0,318(be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,3190, qp->ibqp.qp_num,320hdr->lrh[3], hdr->lrh[1]);321goto err;322}323/* Validate the SLID. See Ch. 9.6.1.5 */324if (be16_to_cpu(hdr->lrh[3]) != qp->remote_ah_attr.dlid ||325ppd_from_ibp(ibp)->port != qp->port_num)326goto err;327if (qp->s_mig_state == IB_MIG_REARM &&328!(bth0 & IB_BTH_MIG_REQ))329qp->s_mig_state = IB_MIG_ARMED;330}331332return 0;333334err:335return 1;336}337338/**339* qib_ruc_loopback - handle UC and RC lookback requests340* @sqp: the sending QP341*342* This is called from qib_do_send() to343* forward a WQE addressed to the same HCA.344* Note that although we are single threaded due to the tasklet, we still345* have to protect against post_send(). We don't have to worry about346* receive interrupts since this is a connected protocol and all packets347* will pass through here.348*/349static void qib_ruc_loopback(struct qib_qp *sqp)350{351struct qib_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);352struct qib_qp *qp;353struct qib_swqe *wqe;354struct qib_sge *sge;355unsigned long flags;356struct ib_wc wc;357u64 sdata;358atomic64_t *maddr;359enum ib_wc_status send_status;360int release;361int ret;362363/*364* Note that we check the responder QP state after365* checking the requester's state.366*/367qp = qib_lookup_qpn(ibp, sqp->remote_qpn);368369spin_lock_irqsave(&sqp->s_lock, flags);370371/* Return if we are already busy processing a work request. */372if ((sqp->s_flags & (QIB_S_BUSY | QIB_S_ANY_WAIT)) ||373!(ib_qib_state_ops[sqp->state] & QIB_PROCESS_OR_FLUSH_SEND))374goto unlock;375376sqp->s_flags |= QIB_S_BUSY;377378again:379if (sqp->s_last == sqp->s_head)380goto clr_busy;381wqe = get_swqe_ptr(sqp, sqp->s_last);382383/* Return if it is not OK to start a new work reqeust. */384if (!(ib_qib_state_ops[sqp->state] & QIB_PROCESS_NEXT_SEND_OK)) {385if (!(ib_qib_state_ops[sqp->state] & QIB_FLUSH_SEND))386goto clr_busy;387/* We are in the error state, flush the work request. */388send_status = IB_WC_WR_FLUSH_ERR;389goto flush_send;390}391392/*393* We can rely on the entry not changing without the s_lock394* being held until we update s_last.395* We increment s_cur to indicate s_last is in progress.396*/397if (sqp->s_last == sqp->s_cur) {398if (++sqp->s_cur >= sqp->s_size)399sqp->s_cur = 0;400}401spin_unlock_irqrestore(&sqp->s_lock, flags);402403if (!qp || !(ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK) ||404qp->ibqp.qp_type != sqp->ibqp.qp_type) {405ibp->n_pkt_drops++;406/*407* For RC, the requester would timeout and retry so408* shortcut the timeouts and just signal too many retries.409*/410if (sqp->ibqp.qp_type == IB_QPT_RC)411send_status = IB_WC_RETRY_EXC_ERR;412else413send_status = IB_WC_SUCCESS;414goto serr;415}416417memset(&wc, 0, sizeof wc);418send_status = IB_WC_SUCCESS;419420release = 1;421sqp->s_sge.sge = wqe->sg_list[0];422sqp->s_sge.sg_list = wqe->sg_list + 1;423sqp->s_sge.num_sge = wqe->wr.num_sge;424sqp->s_len = wqe->length;425switch (wqe->wr.opcode) {426case IB_WR_SEND_WITH_IMM:427wc.wc_flags = IB_WC_WITH_IMM;428wc.ex.imm_data = wqe->wr.ex.imm_data;429/* FALLTHROUGH */430case IB_WR_SEND:431ret = qib_get_rwqe(qp, 0);432if (ret < 0)433goto op_err;434if (!ret)435goto rnr_nak;436break;437438case IB_WR_RDMA_WRITE_WITH_IMM:439if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))440goto inv_err;441wc.wc_flags = IB_WC_WITH_IMM;442wc.ex.imm_data = wqe->wr.ex.imm_data;443ret = qib_get_rwqe(qp, 1);444if (ret < 0)445goto op_err;446if (!ret)447goto rnr_nak;448/* FALLTHROUGH */449case IB_WR_RDMA_WRITE:450if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_WRITE)))451goto inv_err;452if (wqe->length == 0)453break;454if (unlikely(!qib_rkey_ok(qp, &qp->r_sge.sge, wqe->length,455wqe->wr.wr.rdma.remote_addr,456wqe->wr.wr.rdma.rkey,457IB_ACCESS_REMOTE_WRITE)))458goto acc_err;459qp->r_sge.sg_list = NULL;460qp->r_sge.num_sge = 1;461qp->r_sge.total_len = wqe->length;462break;463464case IB_WR_RDMA_READ:465if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_READ)))466goto inv_err;467if (unlikely(!qib_rkey_ok(qp, &sqp->s_sge.sge, wqe->length,468wqe->wr.wr.rdma.remote_addr,469wqe->wr.wr.rdma.rkey,470IB_ACCESS_REMOTE_READ)))471goto acc_err;472release = 0;473sqp->s_sge.sg_list = NULL;474sqp->s_sge.num_sge = 1;475qp->r_sge.sge = wqe->sg_list[0];476qp->r_sge.sg_list = wqe->sg_list + 1;477qp->r_sge.num_sge = wqe->wr.num_sge;478qp->r_sge.total_len = wqe->length;479break;480481case IB_WR_ATOMIC_CMP_AND_SWP:482case IB_WR_ATOMIC_FETCH_AND_ADD:483if (unlikely(!(qp->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC)))484goto inv_err;485if (unlikely(!qib_rkey_ok(qp, &qp->r_sge.sge, sizeof(u64),486wqe->wr.wr.atomic.remote_addr,487wqe->wr.wr.atomic.rkey,488IB_ACCESS_REMOTE_ATOMIC)))489goto acc_err;490/* Perform atomic OP and save result. */491maddr = (atomic64_t *) qp->r_sge.sge.vaddr;492sdata = wqe->wr.wr.atomic.compare_add;493*(u64 *) sqp->s_sge.sge.vaddr =494(wqe->wr.opcode == IB_WR_ATOMIC_FETCH_AND_ADD) ?495(u64) atomic64_add_return(sdata, maddr) - sdata :496(u64) cmpxchg((u64 *) qp->r_sge.sge.vaddr,497sdata, wqe->wr.wr.atomic.swap);498atomic_dec(&qp->r_sge.sge.mr->refcount);499qp->r_sge.num_sge = 0;500goto send_comp;501502default:503send_status = IB_WC_LOC_QP_OP_ERR;504goto serr;505}506507sge = &sqp->s_sge.sge;508while (sqp->s_len) {509u32 len = sqp->s_len;510511if (len > sge->length)512len = sge->length;513if (len > sge->sge_length)514len = sge->sge_length;515BUG_ON(len == 0);516qib_copy_sge(&qp->r_sge, sge->vaddr, len, release);517sge->vaddr += len;518sge->length -= len;519sge->sge_length -= len;520if (sge->sge_length == 0) {521if (!release)522atomic_dec(&sge->mr->refcount);523if (--sqp->s_sge.num_sge)524*sge = *sqp->s_sge.sg_list++;525} else if (sge->length == 0 && sge->mr->lkey) {526if (++sge->n >= QIB_SEGSZ) {527if (++sge->m >= sge->mr->mapsz)528break;529sge->n = 0;530}531sge->vaddr =532sge->mr->map[sge->m]->segs[sge->n].vaddr;533sge->length =534sge->mr->map[sge->m]->segs[sge->n].length;535}536sqp->s_len -= len;537}538if (release)539while (qp->r_sge.num_sge) {540atomic_dec(&qp->r_sge.sge.mr->refcount);541if (--qp->r_sge.num_sge)542qp->r_sge.sge = *qp->r_sge.sg_list++;543}544545if (!test_and_clear_bit(QIB_R_WRID_VALID, &qp->r_aflags))546goto send_comp;547548if (wqe->wr.opcode == IB_WR_RDMA_WRITE_WITH_IMM)549wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;550else551wc.opcode = IB_WC_RECV;552wc.wr_id = qp->r_wr_id;553wc.status = IB_WC_SUCCESS;554wc.byte_len = wqe->length;555wc.qp = &qp->ibqp;556wc.src_qp = qp->remote_qpn;557wc.slid = qp->remote_ah_attr.dlid;558wc.sl = qp->remote_ah_attr.sl;559wc.port_num = 1;560/* Signal completion event if the solicited bit is set. */561qib_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,562wqe->wr.send_flags & IB_SEND_SOLICITED);563564send_comp:565spin_lock_irqsave(&sqp->s_lock, flags);566ibp->n_loop_pkts++;567flush_send:568sqp->s_rnr_retry = sqp->s_rnr_retry_cnt;569qib_send_complete(sqp, wqe, send_status);570goto again;571572rnr_nak:573/* Handle RNR NAK */574if (qp->ibqp.qp_type == IB_QPT_UC)575goto send_comp;576ibp->n_rnr_naks++;577/*578* Note: we don't need the s_lock held since the BUSY flag579* makes this single threaded.580*/581if (sqp->s_rnr_retry == 0) {582send_status = IB_WC_RNR_RETRY_EXC_ERR;583goto serr;584}585if (sqp->s_rnr_retry_cnt < 7)586sqp->s_rnr_retry--;587spin_lock_irqsave(&sqp->s_lock, flags);588if (!(ib_qib_state_ops[sqp->state] & QIB_PROCESS_RECV_OK))589goto clr_busy;590sqp->s_flags |= QIB_S_WAIT_RNR;591sqp->s_timer.function = qib_rc_rnr_retry;592sqp->s_timer.expires = jiffies +593usecs_to_jiffies(ib_qib_rnr_table[qp->r_min_rnr_timer]);594add_timer(&sqp->s_timer);595goto clr_busy;596597op_err:598send_status = IB_WC_REM_OP_ERR;599wc.status = IB_WC_LOC_QP_OP_ERR;600goto err;601602inv_err:603send_status = IB_WC_REM_INV_REQ_ERR;604wc.status = IB_WC_LOC_QP_OP_ERR;605goto err;606607acc_err:608send_status = IB_WC_REM_ACCESS_ERR;609wc.status = IB_WC_LOC_PROT_ERR;610err:611/* responder goes to error state */612qib_rc_error(qp, wc.status);613614serr:615spin_lock_irqsave(&sqp->s_lock, flags);616qib_send_complete(sqp, wqe, send_status);617if (sqp->ibqp.qp_type == IB_QPT_RC) {618int lastwqe = qib_error_qp(sqp, IB_WC_WR_FLUSH_ERR);619620sqp->s_flags &= ~QIB_S_BUSY;621spin_unlock_irqrestore(&sqp->s_lock, flags);622if (lastwqe) {623struct ib_event ev;624625ev.device = sqp->ibqp.device;626ev.element.qp = &sqp->ibqp;627ev.event = IB_EVENT_QP_LAST_WQE_REACHED;628sqp->ibqp.event_handler(&ev, sqp->ibqp.qp_context);629}630goto done;631}632clr_busy:633sqp->s_flags &= ~QIB_S_BUSY;634unlock:635spin_unlock_irqrestore(&sqp->s_lock, flags);636done:637if (qp && atomic_dec_and_test(&qp->refcount))638wake_up(&qp->wait);639}640641/**642* qib_make_grh - construct a GRH header643* @ibp: a pointer to the IB port644* @hdr: a pointer to the GRH header being constructed645* @grh: the global route address to send to646* @hwords: the number of 32 bit words of header being sent647* @nwords: the number of 32 bit words of data being sent648*649* Return the size of the header in 32 bit words.650*/651u32 qib_make_grh(struct qib_ibport *ibp, struct ib_grh *hdr,652struct ib_global_route *grh, u32 hwords, u32 nwords)653{654hdr->version_tclass_flow =655cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |656(grh->traffic_class << IB_GRH_TCLASS_SHIFT) |657(grh->flow_label << IB_GRH_FLOW_SHIFT));658hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);659/* next_hdr is defined by C8-7 in ch. 8.4.1 */660hdr->next_hdr = IB_GRH_NEXT_HDR;661hdr->hop_limit = grh->hop_limit;662/* The SGID is 32-bit aligned. */663hdr->sgid.global.subnet_prefix = ibp->gid_prefix;664hdr->sgid.global.interface_id = grh->sgid_index ?665ibp->guids[grh->sgid_index - 1] : ppd_from_ibp(ibp)->guid;666hdr->dgid = grh->dgid;667668/* GRH header size in 32-bit words. */669return sizeof(struct ib_grh) / sizeof(u32);670}671672void qib_make_ruc_header(struct qib_qp *qp, struct qib_other_headers *ohdr,673u32 bth0, u32 bth2)674{675struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);676u16 lrh0;677u32 nwords;678u32 extra_bytes;679680/* Construct the header. */681extra_bytes = -qp->s_cur_size & 3;682nwords = (qp->s_cur_size + extra_bytes) >> 2;683lrh0 = QIB_LRH_BTH;684if (unlikely(qp->remote_ah_attr.ah_flags & IB_AH_GRH)) {685qp->s_hdrwords += qib_make_grh(ibp, &qp->s_hdr.u.l.grh,686&qp->remote_ah_attr.grh,687qp->s_hdrwords, nwords);688lrh0 = QIB_LRH_GRH;689}690lrh0 |= ibp->sl_to_vl[qp->remote_ah_attr.sl] << 12 |691qp->remote_ah_attr.sl << 4;692qp->s_hdr.lrh[0] = cpu_to_be16(lrh0);693qp->s_hdr.lrh[1] = cpu_to_be16(qp->remote_ah_attr.dlid);694qp->s_hdr.lrh[2] = cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);695qp->s_hdr.lrh[3] = cpu_to_be16(ppd_from_ibp(ibp)->lid |696qp->remote_ah_attr.src_path_bits);697bth0 |= qib_get_pkey(ibp, qp->s_pkey_index);698bth0 |= extra_bytes << 20;699if (qp->s_mig_state == IB_MIG_MIGRATED)700bth0 |= IB_BTH_MIG_REQ;701ohdr->bth[0] = cpu_to_be32(bth0);702ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);703ohdr->bth[2] = cpu_to_be32(bth2);704}705706/**707* qib_do_send - perform a send on a QP708* @work: contains a pointer to the QP709*710* Process entries in the send work queue until credit or queue is711* exhausted. Only allow one CPU to send a packet per QP (tasklet).712* Otherwise, two threads could send packets out of order.713*/714void qib_do_send(struct work_struct *work)715{716struct qib_qp *qp = container_of(work, struct qib_qp, s_work);717struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);718struct qib_pportdata *ppd = ppd_from_ibp(ibp);719int (*make_req)(struct qib_qp *qp);720unsigned long flags;721722if ((qp->ibqp.qp_type == IB_QPT_RC ||723qp->ibqp.qp_type == IB_QPT_UC) &&724(qp->remote_ah_attr.dlid & ~((1 << ppd->lmc) - 1)) == ppd->lid) {725qib_ruc_loopback(qp);726return;727}728729if (qp->ibqp.qp_type == IB_QPT_RC)730make_req = qib_make_rc_req;731else if (qp->ibqp.qp_type == IB_QPT_UC)732make_req = qib_make_uc_req;733else734make_req = qib_make_ud_req;735736spin_lock_irqsave(&qp->s_lock, flags);737738/* Return if we are already busy processing a work request. */739if (!qib_send_ok(qp)) {740spin_unlock_irqrestore(&qp->s_lock, flags);741return;742}743744qp->s_flags |= QIB_S_BUSY;745746spin_unlock_irqrestore(&qp->s_lock, flags);747748do {749/* Check for a constructed packet to be sent. */750if (qp->s_hdrwords != 0) {751/*752* If the packet cannot be sent now, return and753* the send tasklet will be woken up later.754*/755if (qib_verbs_send(qp, &qp->s_hdr, qp->s_hdrwords,756qp->s_cur_sge, qp->s_cur_size))757break;758/* Record that s_hdr is empty. */759qp->s_hdrwords = 0;760}761} while (make_req(qp));762}763764/*765* This should be called with s_lock held.766*/767void qib_send_complete(struct qib_qp *qp, struct qib_swqe *wqe,768enum ib_wc_status status)769{770u32 old_last, last;771unsigned i;772773if (!(ib_qib_state_ops[qp->state] & QIB_PROCESS_OR_FLUSH_SEND))774return;775776for (i = 0; i < wqe->wr.num_sge; i++) {777struct qib_sge *sge = &wqe->sg_list[i];778779atomic_dec(&sge->mr->refcount);780}781if (qp->ibqp.qp_type == IB_QPT_UD ||782qp->ibqp.qp_type == IB_QPT_SMI ||783qp->ibqp.qp_type == IB_QPT_GSI)784atomic_dec(&to_iah(wqe->wr.wr.ud.ah)->refcount);785786/* See ch. 11.2.4.1 and 10.7.3.1 */787if (!(qp->s_flags & QIB_S_SIGNAL_REQ_WR) ||788(wqe->wr.send_flags & IB_SEND_SIGNALED) ||789status != IB_WC_SUCCESS) {790struct ib_wc wc;791792memset(&wc, 0, sizeof wc);793wc.wr_id = wqe->wr.wr_id;794wc.status = status;795wc.opcode = ib_qib_wc_opcode[wqe->wr.opcode];796wc.qp = &qp->ibqp;797if (status == IB_WC_SUCCESS)798wc.byte_len = wqe->length;799qib_cq_enter(to_icq(qp->ibqp.send_cq), &wc,800status != IB_WC_SUCCESS);801}802803last = qp->s_last;804old_last = last;805if (++last >= qp->s_size)806last = 0;807qp->s_last = last;808if (qp->s_acked == old_last)809qp->s_acked = last;810if (qp->s_cur == old_last)811qp->s_cur = last;812if (qp->s_tail == old_last)813qp->s_tail = last;814if (qp->state == IB_QPS_SQD && last == qp->s_cur)815qp->s_draining = 0;816}817818819