Path: blob/master/drivers/infiniband/hw/mthca/mthca_cq.c
15112 views
/*1* Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.2* Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.3* Copyright (c) 2005, 2006 Cisco Systems, Inc. All rights reserved.4* Copyright (c) 2005 Mellanox Technologies. All rights reserved.5* Copyright (c) 2004 Voltaire, Inc. All rights reserved.6*7* This software is available to you under a choice of one of two8* licenses. You may choose to be licensed under the terms of the GNU9* General Public License (GPL) Version 2, available from the file10* COPYING in the main directory of this source tree, or the11* OpenIB.org BSD license below:12*13* Redistribution and use in source and binary forms, with or14* without modification, are permitted provided that the following15* conditions are met:16*17* - Redistributions of source code must retain the above18* copyright notice, this list of conditions and the following19* disclaimer.20*21* - Redistributions in binary form must reproduce the above22* copyright notice, this list of conditions and the following23* disclaimer in the documentation and/or other materials24* provided with the distribution.25*26* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,27* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF28* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND29* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS30* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN31* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN32* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE33* SOFTWARE.34*/3536#include <linux/gfp.h>37#include <linux/hardirq.h>38#include <linux/sched.h>3940#include <asm/io.h>4142#include <rdma/ib_pack.h>4344#include "mthca_dev.h"45#include "mthca_cmd.h"46#include "mthca_memfree.h"4748enum {49MTHCA_MAX_DIRECT_CQ_SIZE = 4 * PAGE_SIZE50};5152enum {53MTHCA_CQ_ENTRY_SIZE = 0x2054};5556enum {57MTHCA_ATOMIC_BYTE_LEN = 858};5960/*61* Must be packed because start is 64 bits but only aligned to 32 bits.62*/63struct mthca_cq_context {64__be32 flags;65__be64 start;66__be32 logsize_usrpage;67__be32 error_eqn; /* Tavor only */68__be32 comp_eqn;69__be32 pd;70__be32 lkey;71__be32 last_notified_index;72__be32 solicit_producer_index;73__be32 consumer_index;74__be32 producer_index;75__be32 cqn;76__be32 ci_db; /* Arbel only */77__be32 state_db; /* Arbel only */78u32 reserved;79} __attribute__((packed));8081#define MTHCA_CQ_STATUS_OK ( 0 << 28)82#define MTHCA_CQ_STATUS_OVERFLOW ( 9 << 28)83#define MTHCA_CQ_STATUS_WRITE_FAIL (10 << 28)84#define MTHCA_CQ_FLAG_TR ( 1 << 18)85#define MTHCA_CQ_FLAG_OI ( 1 << 17)86#define MTHCA_CQ_STATE_DISARMED ( 0 << 8)87#define MTHCA_CQ_STATE_ARMED ( 1 << 8)88#define MTHCA_CQ_STATE_ARMED_SOL ( 4 << 8)89#define MTHCA_EQ_STATE_FIRED (10 << 8)9091enum {92MTHCA_ERROR_CQE_OPCODE_MASK = 0xfe93};9495enum {96SYNDROME_LOCAL_LENGTH_ERR = 0x01,97SYNDROME_LOCAL_QP_OP_ERR = 0x02,98SYNDROME_LOCAL_EEC_OP_ERR = 0x03,99SYNDROME_LOCAL_PROT_ERR = 0x04,100SYNDROME_WR_FLUSH_ERR = 0x05,101SYNDROME_MW_BIND_ERR = 0x06,102SYNDROME_BAD_RESP_ERR = 0x10,103SYNDROME_LOCAL_ACCESS_ERR = 0x11,104SYNDROME_REMOTE_INVAL_REQ_ERR = 0x12,105SYNDROME_REMOTE_ACCESS_ERR = 0x13,106SYNDROME_REMOTE_OP_ERR = 0x14,107SYNDROME_RETRY_EXC_ERR = 0x15,108SYNDROME_RNR_RETRY_EXC_ERR = 0x16,109SYNDROME_LOCAL_RDD_VIOL_ERR = 0x20,110SYNDROME_REMOTE_INVAL_RD_REQ_ERR = 0x21,111SYNDROME_REMOTE_ABORTED_ERR = 0x22,112SYNDROME_INVAL_EECN_ERR = 0x23,113SYNDROME_INVAL_EEC_STATE_ERR = 0x24114};115116struct mthca_cqe {117__be32 my_qpn;118__be32 my_ee;119__be32 rqpn;120u8 sl_ipok;121u8 g_mlpath;122__be16 rlid;123__be32 imm_etype_pkey_eec;124__be32 byte_cnt;125__be32 wqe;126u8 opcode;127u8 is_send;128u8 reserved;129u8 owner;130};131132struct mthca_err_cqe {133__be32 my_qpn;134u32 reserved1[3];135u8 syndrome;136u8 vendor_err;137__be16 db_cnt;138u32 reserved2;139__be32 wqe;140u8 opcode;141u8 reserved3[2];142u8 owner;143};144145#define MTHCA_CQ_ENTRY_OWNER_SW (0 << 7)146#define MTHCA_CQ_ENTRY_OWNER_HW (1 << 7)147148#define MTHCA_TAVOR_CQ_DB_INC_CI (1 << 24)149#define MTHCA_TAVOR_CQ_DB_REQ_NOT (2 << 24)150#define MTHCA_TAVOR_CQ_DB_REQ_NOT_SOL (3 << 24)151#define MTHCA_TAVOR_CQ_DB_SET_CI (4 << 24)152#define MTHCA_TAVOR_CQ_DB_REQ_NOT_MULT (5 << 24)153154#define MTHCA_ARBEL_CQ_DB_REQ_NOT_SOL (1 << 24)155#define MTHCA_ARBEL_CQ_DB_REQ_NOT (2 << 24)156#define MTHCA_ARBEL_CQ_DB_REQ_NOT_MULT (3 << 24)157158static inline struct mthca_cqe *get_cqe_from_buf(struct mthca_cq_buf *buf,159int entry)160{161if (buf->is_direct)162return buf->queue.direct.buf + (entry * MTHCA_CQ_ENTRY_SIZE);163else164return buf->queue.page_list[entry * MTHCA_CQ_ENTRY_SIZE / PAGE_SIZE].buf165+ (entry * MTHCA_CQ_ENTRY_SIZE) % PAGE_SIZE;166}167168static inline struct mthca_cqe *get_cqe(struct mthca_cq *cq, int entry)169{170return get_cqe_from_buf(&cq->buf, entry);171}172173static inline struct mthca_cqe *cqe_sw(struct mthca_cqe *cqe)174{175return MTHCA_CQ_ENTRY_OWNER_HW & cqe->owner ? NULL : cqe;176}177178static inline struct mthca_cqe *next_cqe_sw(struct mthca_cq *cq)179{180return cqe_sw(get_cqe(cq, cq->cons_index & cq->ibcq.cqe));181}182183static inline void set_cqe_hw(struct mthca_cqe *cqe)184{185cqe->owner = MTHCA_CQ_ENTRY_OWNER_HW;186}187188static void dump_cqe(struct mthca_dev *dev, void *cqe_ptr)189{190__be32 *cqe = cqe_ptr;191192(void) cqe; /* avoid warning if mthca_dbg compiled away... */193mthca_dbg(dev, "CQE contents %08x %08x %08x %08x %08x %08x %08x %08x\n",194be32_to_cpu(cqe[0]), be32_to_cpu(cqe[1]), be32_to_cpu(cqe[2]),195be32_to_cpu(cqe[3]), be32_to_cpu(cqe[4]), be32_to_cpu(cqe[5]),196be32_to_cpu(cqe[6]), be32_to_cpu(cqe[7]));197}198199/*200* incr is ignored in native Arbel (mem-free) mode, so cq->cons_index201* should be correct before calling update_cons_index().202*/203static inline void update_cons_index(struct mthca_dev *dev, struct mthca_cq *cq,204int incr)205{206if (mthca_is_memfree(dev)) {207*cq->set_ci_db = cpu_to_be32(cq->cons_index);208wmb();209} else {210mthca_write64(MTHCA_TAVOR_CQ_DB_INC_CI | cq->cqn, incr - 1,211dev->kar + MTHCA_CQ_DOORBELL,212MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));213/*214* Make sure doorbells don't leak out of CQ spinlock215* and reach the HCA out of order:216*/217mmiowb();218}219}220221void mthca_cq_completion(struct mthca_dev *dev, u32 cqn)222{223struct mthca_cq *cq;224225cq = mthca_array_get(&dev->cq_table.cq, cqn & (dev->limits.num_cqs - 1));226227if (!cq) {228mthca_warn(dev, "Completion event for bogus CQ %08x\n", cqn);229return;230}231232++cq->arm_sn;233234cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);235}236237void mthca_cq_event(struct mthca_dev *dev, u32 cqn,238enum ib_event_type event_type)239{240struct mthca_cq *cq;241struct ib_event event;242243spin_lock(&dev->cq_table.lock);244245cq = mthca_array_get(&dev->cq_table.cq, cqn & (dev->limits.num_cqs - 1));246if (cq)247++cq->refcount;248249spin_unlock(&dev->cq_table.lock);250251if (!cq) {252mthca_warn(dev, "Async event for bogus CQ %08x\n", cqn);253return;254}255256event.device = &dev->ib_dev;257event.event = event_type;258event.element.cq = &cq->ibcq;259if (cq->ibcq.event_handler)260cq->ibcq.event_handler(&event, cq->ibcq.cq_context);261262spin_lock(&dev->cq_table.lock);263if (!--cq->refcount)264wake_up(&cq->wait);265spin_unlock(&dev->cq_table.lock);266}267268static inline int is_recv_cqe(struct mthca_cqe *cqe)269{270if ((cqe->opcode & MTHCA_ERROR_CQE_OPCODE_MASK) ==271MTHCA_ERROR_CQE_OPCODE_MASK)272return !(cqe->opcode & 0x01);273else274return !(cqe->is_send & 0x80);275}276277void mthca_cq_clean(struct mthca_dev *dev, struct mthca_cq *cq, u32 qpn,278struct mthca_srq *srq)279{280struct mthca_cqe *cqe;281u32 prod_index;282int i, nfreed = 0;283284spin_lock_irq(&cq->lock);285286/*287* First we need to find the current producer index, so we288* know where to start cleaning from. It doesn't matter if HW289* adds new entries after this loop -- the QP we're worried290* about is already in RESET, so the new entries won't come291* from our QP and therefore don't need to be checked.292*/293for (prod_index = cq->cons_index;294cqe_sw(get_cqe(cq, prod_index & cq->ibcq.cqe));295++prod_index)296if (prod_index == cq->cons_index + cq->ibcq.cqe)297break;298299if (0)300mthca_dbg(dev, "Cleaning QPN %06x from CQN %06x; ci %d, pi %d\n",301qpn, cq->cqn, cq->cons_index, prod_index);302303/*304* Now sweep backwards through the CQ, removing CQ entries305* that match our QP by copying older entries on top of them.306*/307while ((int) --prod_index - (int) cq->cons_index >= 0) {308cqe = get_cqe(cq, prod_index & cq->ibcq.cqe);309if (cqe->my_qpn == cpu_to_be32(qpn)) {310if (srq && is_recv_cqe(cqe))311mthca_free_srq_wqe(srq, be32_to_cpu(cqe->wqe));312++nfreed;313} else if (nfreed)314memcpy(get_cqe(cq, (prod_index + nfreed) & cq->ibcq.cqe),315cqe, MTHCA_CQ_ENTRY_SIZE);316}317318if (nfreed) {319for (i = 0; i < nfreed; ++i)320set_cqe_hw(get_cqe(cq, (cq->cons_index + i) & cq->ibcq.cqe));321wmb();322cq->cons_index += nfreed;323update_cons_index(dev, cq, nfreed);324}325326spin_unlock_irq(&cq->lock);327}328329void mthca_cq_resize_copy_cqes(struct mthca_cq *cq)330{331int i;332333/*334* In Tavor mode, the hardware keeps the consumer and producer335* indices mod the CQ size. Since we might be making the CQ336* bigger, we need to deal with the case where the producer337* index wrapped around before the CQ was resized.338*/339if (!mthca_is_memfree(to_mdev(cq->ibcq.device)) &&340cq->ibcq.cqe < cq->resize_buf->cqe) {341cq->cons_index &= cq->ibcq.cqe;342if (cqe_sw(get_cqe(cq, cq->ibcq.cqe)))343cq->cons_index -= cq->ibcq.cqe + 1;344}345346for (i = cq->cons_index; cqe_sw(get_cqe(cq, i & cq->ibcq.cqe)); ++i)347memcpy(get_cqe_from_buf(&cq->resize_buf->buf,348i & cq->resize_buf->cqe),349get_cqe(cq, i & cq->ibcq.cqe), MTHCA_CQ_ENTRY_SIZE);350}351352int mthca_alloc_cq_buf(struct mthca_dev *dev, struct mthca_cq_buf *buf, int nent)353{354int ret;355int i;356357ret = mthca_buf_alloc(dev, nent * MTHCA_CQ_ENTRY_SIZE,358MTHCA_MAX_DIRECT_CQ_SIZE,359&buf->queue, &buf->is_direct,360&dev->driver_pd, 1, &buf->mr);361if (ret)362return ret;363364for (i = 0; i < nent; ++i)365set_cqe_hw(get_cqe_from_buf(buf, i));366367return 0;368}369370void mthca_free_cq_buf(struct mthca_dev *dev, struct mthca_cq_buf *buf, int cqe)371{372mthca_buf_free(dev, (cqe + 1) * MTHCA_CQ_ENTRY_SIZE, &buf->queue,373buf->is_direct, &buf->mr);374}375376static void handle_error_cqe(struct mthca_dev *dev, struct mthca_cq *cq,377struct mthca_qp *qp, int wqe_index, int is_send,378struct mthca_err_cqe *cqe,379struct ib_wc *entry, int *free_cqe)380{381int dbd;382__be32 new_wqe;383384if (cqe->syndrome == SYNDROME_LOCAL_QP_OP_ERR) {385mthca_dbg(dev, "local QP operation err "386"(QPN %06x, WQE @ %08x, CQN %06x, index %d)\n",387be32_to_cpu(cqe->my_qpn), be32_to_cpu(cqe->wqe),388cq->cqn, cq->cons_index);389dump_cqe(dev, cqe);390}391392/*393* For completions in error, only work request ID, status, vendor error394* (and freed resource count for RD) have to be set.395*/396switch (cqe->syndrome) {397case SYNDROME_LOCAL_LENGTH_ERR:398entry->status = IB_WC_LOC_LEN_ERR;399break;400case SYNDROME_LOCAL_QP_OP_ERR:401entry->status = IB_WC_LOC_QP_OP_ERR;402break;403case SYNDROME_LOCAL_EEC_OP_ERR:404entry->status = IB_WC_LOC_EEC_OP_ERR;405break;406case SYNDROME_LOCAL_PROT_ERR:407entry->status = IB_WC_LOC_PROT_ERR;408break;409case SYNDROME_WR_FLUSH_ERR:410entry->status = IB_WC_WR_FLUSH_ERR;411break;412case SYNDROME_MW_BIND_ERR:413entry->status = IB_WC_MW_BIND_ERR;414break;415case SYNDROME_BAD_RESP_ERR:416entry->status = IB_WC_BAD_RESP_ERR;417break;418case SYNDROME_LOCAL_ACCESS_ERR:419entry->status = IB_WC_LOC_ACCESS_ERR;420break;421case SYNDROME_REMOTE_INVAL_REQ_ERR:422entry->status = IB_WC_REM_INV_REQ_ERR;423break;424case SYNDROME_REMOTE_ACCESS_ERR:425entry->status = IB_WC_REM_ACCESS_ERR;426break;427case SYNDROME_REMOTE_OP_ERR:428entry->status = IB_WC_REM_OP_ERR;429break;430case SYNDROME_RETRY_EXC_ERR:431entry->status = IB_WC_RETRY_EXC_ERR;432break;433case SYNDROME_RNR_RETRY_EXC_ERR:434entry->status = IB_WC_RNR_RETRY_EXC_ERR;435break;436case SYNDROME_LOCAL_RDD_VIOL_ERR:437entry->status = IB_WC_LOC_RDD_VIOL_ERR;438break;439case SYNDROME_REMOTE_INVAL_RD_REQ_ERR:440entry->status = IB_WC_REM_INV_RD_REQ_ERR;441break;442case SYNDROME_REMOTE_ABORTED_ERR:443entry->status = IB_WC_REM_ABORT_ERR;444break;445case SYNDROME_INVAL_EECN_ERR:446entry->status = IB_WC_INV_EECN_ERR;447break;448case SYNDROME_INVAL_EEC_STATE_ERR:449entry->status = IB_WC_INV_EEC_STATE_ERR;450break;451default:452entry->status = IB_WC_GENERAL_ERR;453break;454}455456entry->vendor_err = cqe->vendor_err;457458/*459* Mem-free HCAs always generate one CQE per WQE, even in the460* error case, so we don't have to check the doorbell count, etc.461*/462if (mthca_is_memfree(dev))463return;464465mthca_free_err_wqe(dev, qp, is_send, wqe_index, &dbd, &new_wqe);466467/*468* If we're at the end of the WQE chain, or we've used up our469* doorbell count, free the CQE. Otherwise just update it for470* the next poll operation.471*/472if (!(new_wqe & cpu_to_be32(0x3f)) || (!cqe->db_cnt && dbd))473return;474475be16_add_cpu(&cqe->db_cnt, -dbd);476cqe->wqe = new_wqe;477cqe->syndrome = SYNDROME_WR_FLUSH_ERR;478479*free_cqe = 0;480}481482static inline int mthca_poll_one(struct mthca_dev *dev,483struct mthca_cq *cq,484struct mthca_qp **cur_qp,485int *freed,486struct ib_wc *entry)487{488struct mthca_wq *wq;489struct mthca_cqe *cqe;490int wqe_index;491int is_error;492int is_send;493int free_cqe = 1;494int err = 0;495u16 checksum;496497cqe = next_cqe_sw(cq);498if (!cqe)499return -EAGAIN;500501/*502* Make sure we read CQ entry contents after we've checked the503* ownership bit.504*/505rmb();506507if (0) {508mthca_dbg(dev, "%x/%d: CQE -> QPN %06x, WQE @ %08x\n",509cq->cqn, cq->cons_index, be32_to_cpu(cqe->my_qpn),510be32_to_cpu(cqe->wqe));511dump_cqe(dev, cqe);512}513514is_error = (cqe->opcode & MTHCA_ERROR_CQE_OPCODE_MASK) ==515MTHCA_ERROR_CQE_OPCODE_MASK;516is_send = is_error ? cqe->opcode & 0x01 : cqe->is_send & 0x80;517518if (!*cur_qp || be32_to_cpu(cqe->my_qpn) != (*cur_qp)->qpn) {519/*520* We do not have to take the QP table lock here,521* because CQs will be locked while QPs are removed522* from the table.523*/524*cur_qp = mthca_array_get(&dev->qp_table.qp,525be32_to_cpu(cqe->my_qpn) &526(dev->limits.num_qps - 1));527if (!*cur_qp) {528mthca_warn(dev, "CQ entry for unknown QP %06x\n",529be32_to_cpu(cqe->my_qpn) & 0xffffff);530err = -EINVAL;531goto out;532}533}534535entry->qp = &(*cur_qp)->ibqp;536537if (is_send) {538wq = &(*cur_qp)->sq;539wqe_index = ((be32_to_cpu(cqe->wqe) - (*cur_qp)->send_wqe_offset)540>> wq->wqe_shift);541entry->wr_id = (*cur_qp)->wrid[wqe_index +542(*cur_qp)->rq.max];543} else if ((*cur_qp)->ibqp.srq) {544struct mthca_srq *srq = to_msrq((*cur_qp)->ibqp.srq);545u32 wqe = be32_to_cpu(cqe->wqe);546wq = NULL;547wqe_index = wqe >> srq->wqe_shift;548entry->wr_id = srq->wrid[wqe_index];549mthca_free_srq_wqe(srq, wqe);550} else {551s32 wqe;552wq = &(*cur_qp)->rq;553wqe = be32_to_cpu(cqe->wqe);554wqe_index = wqe >> wq->wqe_shift;555/*556* WQE addr == base - 1 might be reported in receive completion557* with error instead of (rq size - 1) by Sinai FW 1.0.800 and558* Arbel FW 5.1.400. This bug should be fixed in later FW revs.559*/560if (unlikely(wqe_index < 0))561wqe_index = wq->max - 1;562entry->wr_id = (*cur_qp)->wrid[wqe_index];563}564565if (wq) {566if (wq->last_comp < wqe_index)567wq->tail += wqe_index - wq->last_comp;568else569wq->tail += wqe_index + wq->max - wq->last_comp;570571wq->last_comp = wqe_index;572}573574if (is_error) {575handle_error_cqe(dev, cq, *cur_qp, wqe_index, is_send,576(struct mthca_err_cqe *) cqe,577entry, &free_cqe);578goto out;579}580581if (is_send) {582entry->wc_flags = 0;583switch (cqe->opcode) {584case MTHCA_OPCODE_RDMA_WRITE:585entry->opcode = IB_WC_RDMA_WRITE;586break;587case MTHCA_OPCODE_RDMA_WRITE_IMM:588entry->opcode = IB_WC_RDMA_WRITE;589entry->wc_flags |= IB_WC_WITH_IMM;590break;591case MTHCA_OPCODE_SEND:592entry->opcode = IB_WC_SEND;593break;594case MTHCA_OPCODE_SEND_IMM:595entry->opcode = IB_WC_SEND;596entry->wc_flags |= IB_WC_WITH_IMM;597break;598case MTHCA_OPCODE_RDMA_READ:599entry->opcode = IB_WC_RDMA_READ;600entry->byte_len = be32_to_cpu(cqe->byte_cnt);601break;602case MTHCA_OPCODE_ATOMIC_CS:603entry->opcode = IB_WC_COMP_SWAP;604entry->byte_len = MTHCA_ATOMIC_BYTE_LEN;605break;606case MTHCA_OPCODE_ATOMIC_FA:607entry->opcode = IB_WC_FETCH_ADD;608entry->byte_len = MTHCA_ATOMIC_BYTE_LEN;609break;610case MTHCA_OPCODE_BIND_MW:611entry->opcode = IB_WC_BIND_MW;612break;613default:614entry->opcode = MTHCA_OPCODE_INVALID;615break;616}617} else {618entry->byte_len = be32_to_cpu(cqe->byte_cnt);619switch (cqe->opcode & 0x1f) {620case IB_OPCODE_SEND_LAST_WITH_IMMEDIATE:621case IB_OPCODE_SEND_ONLY_WITH_IMMEDIATE:622entry->wc_flags = IB_WC_WITH_IMM;623entry->ex.imm_data = cqe->imm_etype_pkey_eec;624entry->opcode = IB_WC_RECV;625break;626case IB_OPCODE_RDMA_WRITE_LAST_WITH_IMMEDIATE:627case IB_OPCODE_RDMA_WRITE_ONLY_WITH_IMMEDIATE:628entry->wc_flags = IB_WC_WITH_IMM;629entry->ex.imm_data = cqe->imm_etype_pkey_eec;630entry->opcode = IB_WC_RECV_RDMA_WITH_IMM;631break;632default:633entry->wc_flags = 0;634entry->opcode = IB_WC_RECV;635break;636}637entry->slid = be16_to_cpu(cqe->rlid);638entry->sl = cqe->sl_ipok >> 4;639entry->src_qp = be32_to_cpu(cqe->rqpn) & 0xffffff;640entry->dlid_path_bits = cqe->g_mlpath & 0x7f;641entry->pkey_index = be32_to_cpu(cqe->imm_etype_pkey_eec) >> 16;642entry->wc_flags |= cqe->g_mlpath & 0x80 ? IB_WC_GRH : 0;643checksum = (be32_to_cpu(cqe->rqpn) >> 24) |644((be32_to_cpu(cqe->my_ee) >> 16) & 0xff00);645entry->csum_ok = (cqe->sl_ipok & 1 && checksum == 0xffff);646}647648entry->status = IB_WC_SUCCESS;649650out:651if (likely(free_cqe)) {652set_cqe_hw(cqe);653++(*freed);654++cq->cons_index;655}656657return err;658}659660int mthca_poll_cq(struct ib_cq *ibcq, int num_entries,661struct ib_wc *entry)662{663struct mthca_dev *dev = to_mdev(ibcq->device);664struct mthca_cq *cq = to_mcq(ibcq);665struct mthca_qp *qp = NULL;666unsigned long flags;667int err = 0;668int freed = 0;669int npolled;670671spin_lock_irqsave(&cq->lock, flags);672673npolled = 0;674repoll:675while (npolled < num_entries) {676err = mthca_poll_one(dev, cq, &qp,677&freed, entry + npolled);678if (err)679break;680++npolled;681}682683if (freed) {684wmb();685update_cons_index(dev, cq, freed);686}687688/*689* If a CQ resize is in progress and we discovered that the690* old buffer is empty, then peek in the new buffer, and if691* it's not empty, switch to the new buffer and continue692* polling there.693*/694if (unlikely(err == -EAGAIN && cq->resize_buf &&695cq->resize_buf->state == CQ_RESIZE_READY)) {696/*697* In Tavor mode, the hardware keeps the producer698* index modulo the CQ size. Since we might be making699* the CQ bigger, we need to mask our consumer index700* using the size of the old CQ buffer before looking701* in the new CQ buffer.702*/703if (!mthca_is_memfree(dev))704cq->cons_index &= cq->ibcq.cqe;705706if (cqe_sw(get_cqe_from_buf(&cq->resize_buf->buf,707cq->cons_index & cq->resize_buf->cqe))) {708struct mthca_cq_buf tbuf;709int tcqe;710711tbuf = cq->buf;712tcqe = cq->ibcq.cqe;713cq->buf = cq->resize_buf->buf;714cq->ibcq.cqe = cq->resize_buf->cqe;715716cq->resize_buf->buf = tbuf;717cq->resize_buf->cqe = tcqe;718cq->resize_buf->state = CQ_RESIZE_SWAPPED;719720goto repoll;721}722}723724spin_unlock_irqrestore(&cq->lock, flags);725726return err == 0 || err == -EAGAIN ? npolled : err;727}728729int mthca_tavor_arm_cq(struct ib_cq *cq, enum ib_cq_notify_flags flags)730{731u32 dbhi = ((flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?732MTHCA_TAVOR_CQ_DB_REQ_NOT_SOL :733MTHCA_TAVOR_CQ_DB_REQ_NOT) |734to_mcq(cq)->cqn;735736mthca_write64(dbhi, 0xffffffff, to_mdev(cq->device)->kar + MTHCA_CQ_DOORBELL,737MTHCA_GET_DOORBELL_LOCK(&to_mdev(cq->device)->doorbell_lock));738739return 0;740}741742int mthca_arbel_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)743{744struct mthca_cq *cq = to_mcq(ibcq);745__be32 db_rec[2];746u32 dbhi;747u32 sn = cq->arm_sn & 3;748749db_rec[0] = cpu_to_be32(cq->cons_index);750db_rec[1] = cpu_to_be32((cq->cqn << 8) | (2 << 5) | (sn << 3) |751((flags & IB_CQ_SOLICITED_MASK) ==752IB_CQ_SOLICITED ? 1 : 2));753754mthca_write_db_rec(db_rec, cq->arm_db);755756/*757* Make sure that the doorbell record in host memory is758* written before ringing the doorbell via PCI MMIO.759*/760wmb();761762dbhi = (sn << 28) |763((flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?764MTHCA_ARBEL_CQ_DB_REQ_NOT_SOL :765MTHCA_ARBEL_CQ_DB_REQ_NOT) | cq->cqn;766767mthca_write64(dbhi, cq->cons_index,768to_mdev(ibcq->device)->kar + MTHCA_CQ_DOORBELL,769MTHCA_GET_DOORBELL_LOCK(&to_mdev(ibcq->device)->doorbell_lock));770771return 0;772}773774int mthca_init_cq(struct mthca_dev *dev, int nent,775struct mthca_ucontext *ctx, u32 pdn,776struct mthca_cq *cq)777{778struct mthca_mailbox *mailbox;779struct mthca_cq_context *cq_context;780int err = -ENOMEM;781u8 status;782783cq->ibcq.cqe = nent - 1;784cq->is_kernel = !ctx;785786cq->cqn = mthca_alloc(&dev->cq_table.alloc);787if (cq->cqn == -1)788return -ENOMEM;789790if (mthca_is_memfree(dev)) {791err = mthca_table_get(dev, dev->cq_table.table, cq->cqn);792if (err)793goto err_out;794795if (cq->is_kernel) {796cq->arm_sn = 1;797798err = -ENOMEM;799800cq->set_ci_db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_CQ_SET_CI,801cq->cqn, &cq->set_ci_db);802if (cq->set_ci_db_index < 0)803goto err_out_icm;804805cq->arm_db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_CQ_ARM,806cq->cqn, &cq->arm_db);807if (cq->arm_db_index < 0)808goto err_out_ci;809}810}811812mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);813if (IS_ERR(mailbox))814goto err_out_arm;815816cq_context = mailbox->buf;817818if (cq->is_kernel) {819err = mthca_alloc_cq_buf(dev, &cq->buf, nent);820if (err)821goto err_out_mailbox;822}823824spin_lock_init(&cq->lock);825cq->refcount = 1;826init_waitqueue_head(&cq->wait);827mutex_init(&cq->mutex);828829memset(cq_context, 0, sizeof *cq_context);830cq_context->flags = cpu_to_be32(MTHCA_CQ_STATUS_OK |831MTHCA_CQ_STATE_DISARMED |832MTHCA_CQ_FLAG_TR);833cq_context->logsize_usrpage = cpu_to_be32((ffs(nent) - 1) << 24);834if (ctx)835cq_context->logsize_usrpage |= cpu_to_be32(ctx->uar.index);836else837cq_context->logsize_usrpage |= cpu_to_be32(dev->driver_uar.index);838cq_context->error_eqn = cpu_to_be32(dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn);839cq_context->comp_eqn = cpu_to_be32(dev->eq_table.eq[MTHCA_EQ_COMP].eqn);840cq_context->pd = cpu_to_be32(pdn);841cq_context->lkey = cpu_to_be32(cq->buf.mr.ibmr.lkey);842cq_context->cqn = cpu_to_be32(cq->cqn);843844if (mthca_is_memfree(dev)) {845cq_context->ci_db = cpu_to_be32(cq->set_ci_db_index);846cq_context->state_db = cpu_to_be32(cq->arm_db_index);847}848849err = mthca_SW2HW_CQ(dev, mailbox, cq->cqn, &status);850if (err) {851mthca_warn(dev, "SW2HW_CQ failed (%d)\n", err);852goto err_out_free_mr;853}854855if (status) {856mthca_warn(dev, "SW2HW_CQ returned status 0x%02x\n",857status);858err = -EINVAL;859goto err_out_free_mr;860}861862spin_lock_irq(&dev->cq_table.lock);863if (mthca_array_set(&dev->cq_table.cq,864cq->cqn & (dev->limits.num_cqs - 1),865cq)) {866spin_unlock_irq(&dev->cq_table.lock);867goto err_out_free_mr;868}869spin_unlock_irq(&dev->cq_table.lock);870871cq->cons_index = 0;872873mthca_free_mailbox(dev, mailbox);874875return 0;876877err_out_free_mr:878if (cq->is_kernel)879mthca_free_cq_buf(dev, &cq->buf, cq->ibcq.cqe);880881err_out_mailbox:882mthca_free_mailbox(dev, mailbox);883884err_out_arm:885if (cq->is_kernel && mthca_is_memfree(dev))886mthca_free_db(dev, MTHCA_DB_TYPE_CQ_ARM, cq->arm_db_index);887888err_out_ci:889if (cq->is_kernel && mthca_is_memfree(dev))890mthca_free_db(dev, MTHCA_DB_TYPE_CQ_SET_CI, cq->set_ci_db_index);891892err_out_icm:893mthca_table_put(dev, dev->cq_table.table, cq->cqn);894895err_out:896mthca_free(&dev->cq_table.alloc, cq->cqn);897898return err;899}900901static inline int get_cq_refcount(struct mthca_dev *dev, struct mthca_cq *cq)902{903int c;904905spin_lock_irq(&dev->cq_table.lock);906c = cq->refcount;907spin_unlock_irq(&dev->cq_table.lock);908909return c;910}911912void mthca_free_cq(struct mthca_dev *dev,913struct mthca_cq *cq)914{915struct mthca_mailbox *mailbox;916int err;917u8 status;918919mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);920if (IS_ERR(mailbox)) {921mthca_warn(dev, "No memory for mailbox to free CQ.\n");922return;923}924925err = mthca_HW2SW_CQ(dev, mailbox, cq->cqn, &status);926if (err)927mthca_warn(dev, "HW2SW_CQ failed (%d)\n", err);928else if (status)929mthca_warn(dev, "HW2SW_CQ returned status 0x%02x\n", status);930931if (0) {932__be32 *ctx = mailbox->buf;933int j;934935printk(KERN_ERR "context for CQN %x (cons index %x, next sw %d)\n",936cq->cqn, cq->cons_index,937cq->is_kernel ? !!next_cqe_sw(cq) : 0);938for (j = 0; j < 16; ++j)939printk(KERN_ERR "[%2x] %08x\n", j * 4, be32_to_cpu(ctx[j]));940}941942spin_lock_irq(&dev->cq_table.lock);943mthca_array_clear(&dev->cq_table.cq,944cq->cqn & (dev->limits.num_cqs - 1));945--cq->refcount;946spin_unlock_irq(&dev->cq_table.lock);947948if (dev->mthca_flags & MTHCA_FLAG_MSI_X)949synchronize_irq(dev->eq_table.eq[MTHCA_EQ_COMP].msi_x_vector);950else951synchronize_irq(dev->pdev->irq);952953wait_event(cq->wait, !get_cq_refcount(dev, cq));954955if (cq->is_kernel) {956mthca_free_cq_buf(dev, &cq->buf, cq->ibcq.cqe);957if (mthca_is_memfree(dev)) {958mthca_free_db(dev, MTHCA_DB_TYPE_CQ_ARM, cq->arm_db_index);959mthca_free_db(dev, MTHCA_DB_TYPE_CQ_SET_CI, cq->set_ci_db_index);960}961}962963mthca_table_put(dev, dev->cq_table.table, cq->cqn);964mthca_free(&dev->cq_table.alloc, cq->cqn);965mthca_free_mailbox(dev, mailbox);966}967968int mthca_init_cq_table(struct mthca_dev *dev)969{970int err;971972spin_lock_init(&dev->cq_table.lock);973974err = mthca_alloc_init(&dev->cq_table.alloc,975dev->limits.num_cqs,976(1 << 24) - 1,977dev->limits.reserved_cqs);978if (err)979return err;980981err = mthca_array_init(&dev->cq_table.cq,982dev->limits.num_cqs);983if (err)984mthca_alloc_cleanup(&dev->cq_table.alloc);985986return err;987}988989void mthca_cleanup_cq_table(struct mthca_dev *dev)990{991mthca_array_cleanup(&dev->cq_table.cq, dev->limits.num_cqs);992mthca_alloc_cleanup(&dev->cq_table.alloc);993}994995996