xref: /freebsd/sys/dev/mthca/mthca_cq.c (revision 33ec1ccbae880855a4aa9e221ba8512da70e541e)
1*33ec1ccbSHans Petter Selasky /*
2*33ec1ccbSHans Petter Selasky  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3*33ec1ccbSHans Petter Selasky  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4*33ec1ccbSHans Petter Selasky  * Copyright (c) 2005, 2006 Cisco Systems, Inc. All rights reserved.
5*33ec1ccbSHans Petter Selasky  * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
6*33ec1ccbSHans Petter Selasky  * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
7*33ec1ccbSHans Petter Selasky  *
8*33ec1ccbSHans Petter Selasky  * This software is available to you under a choice of one of two
9*33ec1ccbSHans Petter Selasky  * licenses.  You may choose to be licensed under the terms of the GNU
10*33ec1ccbSHans Petter Selasky  * General Public License (GPL) Version 2, available from the file
11*33ec1ccbSHans Petter Selasky  * COPYING in the main directory of this source tree, or the
12*33ec1ccbSHans Petter Selasky  * OpenIB.org BSD license below:
13*33ec1ccbSHans Petter Selasky  *
14*33ec1ccbSHans Petter Selasky  *     Redistribution and use in source and binary forms, with or
15*33ec1ccbSHans Petter Selasky  *     without modification, are permitted provided that the following
16*33ec1ccbSHans Petter Selasky  *     conditions are met:
17*33ec1ccbSHans Petter Selasky  *
18*33ec1ccbSHans Petter Selasky  *      - Redistributions of source code must retain the above
19*33ec1ccbSHans Petter Selasky  *        copyright notice, this list of conditions and the following
20*33ec1ccbSHans Petter Selasky  *        disclaimer.
21*33ec1ccbSHans Petter Selasky  *
22*33ec1ccbSHans Petter Selasky  *      - Redistributions in binary form must reproduce the above
23*33ec1ccbSHans Petter Selasky  *        copyright notice, this list of conditions and the following
24*33ec1ccbSHans Petter Selasky  *        disclaimer in the documentation and/or other materials
25*33ec1ccbSHans Petter Selasky  *        provided with the distribution.
26*33ec1ccbSHans Petter Selasky  *
27*33ec1ccbSHans Petter Selasky  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28*33ec1ccbSHans Petter Selasky  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29*33ec1ccbSHans Petter Selasky  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30*33ec1ccbSHans Petter Selasky  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31*33ec1ccbSHans Petter Selasky  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32*33ec1ccbSHans Petter Selasky  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33*33ec1ccbSHans Petter Selasky  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34*33ec1ccbSHans Petter Selasky  * SOFTWARE.
35*33ec1ccbSHans Petter Selasky  */
36*33ec1ccbSHans Petter Selasky 
37*33ec1ccbSHans Petter Selasky #include <linux/gfp.h>
38*33ec1ccbSHans Petter Selasky #include <linux/hardirq.h>
39*33ec1ccbSHans Petter Selasky #include <linux/sched.h>
40*33ec1ccbSHans Petter Selasky 
41*33ec1ccbSHans Petter Selasky #include <asm/io.h>
42*33ec1ccbSHans Petter Selasky 
43*33ec1ccbSHans Petter Selasky #include <rdma/ib_pack.h>
44*33ec1ccbSHans Petter Selasky 
45*33ec1ccbSHans Petter Selasky #include "mthca_dev.h"
46*33ec1ccbSHans Petter Selasky #include "mthca_cmd.h"
47*33ec1ccbSHans Petter Selasky #include "mthca_memfree.h"
48*33ec1ccbSHans Petter Selasky 
49*33ec1ccbSHans Petter Selasky enum {
50*33ec1ccbSHans Petter Selasky 	MTHCA_MAX_DIRECT_CQ_SIZE = 4 * PAGE_SIZE
51*33ec1ccbSHans Petter Selasky };
52*33ec1ccbSHans Petter Selasky 
53*33ec1ccbSHans Petter Selasky enum {
54*33ec1ccbSHans Petter Selasky 	MTHCA_CQ_ENTRY_SIZE = 0x20
55*33ec1ccbSHans Petter Selasky };
56*33ec1ccbSHans Petter Selasky 
57*33ec1ccbSHans Petter Selasky enum {
58*33ec1ccbSHans Petter Selasky 	MTHCA_ATOMIC_BYTE_LEN = 8
59*33ec1ccbSHans Petter Selasky };
60*33ec1ccbSHans Petter Selasky 
61*33ec1ccbSHans Petter Selasky /*
62*33ec1ccbSHans Petter Selasky  * Must be packed because start is 64 bits but only aligned to 32 bits.
63*33ec1ccbSHans Petter Selasky  */
64*33ec1ccbSHans Petter Selasky struct mthca_cq_context {
65*33ec1ccbSHans Petter Selasky 	__be32 flags;
66*33ec1ccbSHans Petter Selasky 	__be64 start;
67*33ec1ccbSHans Petter Selasky 	__be32 logsize_usrpage;
68*33ec1ccbSHans Petter Selasky 	__be32 error_eqn;	/* Tavor only */
69*33ec1ccbSHans Petter Selasky 	__be32 comp_eqn;
70*33ec1ccbSHans Petter Selasky 	__be32 pd;
71*33ec1ccbSHans Petter Selasky 	__be32 lkey;
72*33ec1ccbSHans Petter Selasky 	__be32 last_notified_index;
73*33ec1ccbSHans Petter Selasky 	__be32 solicit_producer_index;
74*33ec1ccbSHans Petter Selasky 	__be32 consumer_index;
75*33ec1ccbSHans Petter Selasky 	__be32 producer_index;
76*33ec1ccbSHans Petter Selasky 	__be32 cqn;
77*33ec1ccbSHans Petter Selasky 	__be32 ci_db;		/* Arbel only */
78*33ec1ccbSHans Petter Selasky 	__be32 state_db;	/* Arbel only */
79*33ec1ccbSHans Petter Selasky 	u32    reserved;
80*33ec1ccbSHans Petter Selasky } __attribute__((packed));
81*33ec1ccbSHans Petter Selasky 
82*33ec1ccbSHans Petter Selasky #define MTHCA_CQ_STATUS_OK          ( 0 << 28)
83*33ec1ccbSHans Petter Selasky #define MTHCA_CQ_STATUS_OVERFLOW    ( 9 << 28)
84*33ec1ccbSHans Petter Selasky #define MTHCA_CQ_STATUS_WRITE_FAIL  (10 << 28)
85*33ec1ccbSHans Petter Selasky #define MTHCA_CQ_FLAG_TR            ( 1 << 18)
86*33ec1ccbSHans Petter Selasky #define MTHCA_CQ_FLAG_OI            ( 1 << 17)
87*33ec1ccbSHans Petter Selasky #define MTHCA_CQ_STATE_DISARMED     ( 0 <<  8)
88*33ec1ccbSHans Petter Selasky #define MTHCA_CQ_STATE_ARMED        ( 1 <<  8)
89*33ec1ccbSHans Petter Selasky #define MTHCA_CQ_STATE_ARMED_SOL    ( 4 <<  8)
90*33ec1ccbSHans Petter Selasky #define MTHCA_EQ_STATE_FIRED        (10 <<  8)
91*33ec1ccbSHans Petter Selasky 
92*33ec1ccbSHans Petter Selasky enum {
93*33ec1ccbSHans Petter Selasky 	MTHCA_ERROR_CQE_OPCODE_MASK = 0xfe
94*33ec1ccbSHans Petter Selasky };
95*33ec1ccbSHans Petter Selasky 
96*33ec1ccbSHans Petter Selasky enum {
97*33ec1ccbSHans Petter Selasky 	SYNDROME_LOCAL_LENGTH_ERR 	 = 0x01,
98*33ec1ccbSHans Petter Selasky 	SYNDROME_LOCAL_QP_OP_ERR  	 = 0x02,
99*33ec1ccbSHans Petter Selasky 	SYNDROME_LOCAL_EEC_OP_ERR 	 = 0x03,
100*33ec1ccbSHans Petter Selasky 	SYNDROME_LOCAL_PROT_ERR   	 = 0x04,
101*33ec1ccbSHans Petter Selasky 	SYNDROME_WR_FLUSH_ERR     	 = 0x05,
102*33ec1ccbSHans Petter Selasky 	SYNDROME_MW_BIND_ERR      	 = 0x06,
103*33ec1ccbSHans Petter Selasky 	SYNDROME_BAD_RESP_ERR     	 = 0x10,
104*33ec1ccbSHans Petter Selasky 	SYNDROME_LOCAL_ACCESS_ERR 	 = 0x11,
105*33ec1ccbSHans Petter Selasky 	SYNDROME_REMOTE_INVAL_REQ_ERR 	 = 0x12,
106*33ec1ccbSHans Petter Selasky 	SYNDROME_REMOTE_ACCESS_ERR 	 = 0x13,
107*33ec1ccbSHans Petter Selasky 	SYNDROME_REMOTE_OP_ERR     	 = 0x14,
108*33ec1ccbSHans Petter Selasky 	SYNDROME_RETRY_EXC_ERR 		 = 0x15,
109*33ec1ccbSHans Petter Selasky 	SYNDROME_RNR_RETRY_EXC_ERR 	 = 0x16,
110*33ec1ccbSHans Petter Selasky 	SYNDROME_LOCAL_RDD_VIOL_ERR 	 = 0x20,
111*33ec1ccbSHans Petter Selasky 	SYNDROME_REMOTE_INVAL_RD_REQ_ERR = 0x21,
112*33ec1ccbSHans Petter Selasky 	SYNDROME_REMOTE_ABORTED_ERR 	 = 0x22,
113*33ec1ccbSHans Petter Selasky 	SYNDROME_INVAL_EECN_ERR 	 = 0x23,
114*33ec1ccbSHans Petter Selasky 	SYNDROME_INVAL_EEC_STATE_ERR 	 = 0x24
115*33ec1ccbSHans Petter Selasky };
116*33ec1ccbSHans Petter Selasky 
117*33ec1ccbSHans Petter Selasky struct mthca_cqe {
118*33ec1ccbSHans Petter Selasky 	__be32 my_qpn;
119*33ec1ccbSHans Petter Selasky 	__be32 my_ee;
120*33ec1ccbSHans Petter Selasky 	__be32 rqpn;
121*33ec1ccbSHans Petter Selasky 	u8     sl_ipok;
122*33ec1ccbSHans Petter Selasky 	u8     g_mlpath;
123*33ec1ccbSHans Petter Selasky 	__be16 rlid;
124*33ec1ccbSHans Petter Selasky 	__be32 imm_etype_pkey_eec;
125*33ec1ccbSHans Petter Selasky 	__be32 byte_cnt;
126*33ec1ccbSHans Petter Selasky 	__be32 wqe;
127*33ec1ccbSHans Petter Selasky 	u8     opcode;
128*33ec1ccbSHans Petter Selasky 	u8     is_send;
129*33ec1ccbSHans Petter Selasky 	u8     reserved;
130*33ec1ccbSHans Petter Selasky 	u8     owner;
131*33ec1ccbSHans Petter Selasky };
132*33ec1ccbSHans Petter Selasky 
133*33ec1ccbSHans Petter Selasky struct mthca_err_cqe {
134*33ec1ccbSHans Petter Selasky 	__be32 my_qpn;
135*33ec1ccbSHans Petter Selasky 	u32    reserved1[3];
136*33ec1ccbSHans Petter Selasky 	u8     syndrome;
137*33ec1ccbSHans Petter Selasky 	u8     vendor_err;
138*33ec1ccbSHans Petter Selasky 	__be16 db_cnt;
139*33ec1ccbSHans Petter Selasky 	u32    reserved2;
140*33ec1ccbSHans Petter Selasky 	__be32 wqe;
141*33ec1ccbSHans Petter Selasky 	u8     opcode;
142*33ec1ccbSHans Petter Selasky 	u8     reserved3[2];
143*33ec1ccbSHans Petter Selasky 	u8     owner;
144*33ec1ccbSHans Petter Selasky };
145*33ec1ccbSHans Petter Selasky 
146*33ec1ccbSHans Petter Selasky #define MTHCA_CQ_ENTRY_OWNER_SW      (0 << 7)
147*33ec1ccbSHans Petter Selasky #define MTHCA_CQ_ENTRY_OWNER_HW      (1 << 7)
148*33ec1ccbSHans Petter Selasky 
149*33ec1ccbSHans Petter Selasky #define MTHCA_TAVOR_CQ_DB_INC_CI       (1 << 24)
150*33ec1ccbSHans Petter Selasky #define MTHCA_TAVOR_CQ_DB_REQ_NOT      (2 << 24)
151*33ec1ccbSHans Petter Selasky #define MTHCA_TAVOR_CQ_DB_REQ_NOT_SOL  (3 << 24)
152*33ec1ccbSHans Petter Selasky #define MTHCA_TAVOR_CQ_DB_SET_CI       (4 << 24)
153*33ec1ccbSHans Petter Selasky #define MTHCA_TAVOR_CQ_DB_REQ_NOT_MULT (5 << 24)
154*33ec1ccbSHans Petter Selasky 
155*33ec1ccbSHans Petter Selasky #define MTHCA_ARBEL_CQ_DB_REQ_NOT_SOL  (1 << 24)
156*33ec1ccbSHans Petter Selasky #define MTHCA_ARBEL_CQ_DB_REQ_NOT      (2 << 24)
157*33ec1ccbSHans Petter Selasky #define MTHCA_ARBEL_CQ_DB_REQ_NOT_MULT (3 << 24)
158*33ec1ccbSHans Petter Selasky 
get_cqe_from_buf(struct mthca_cq_buf * buf,int entry)159*33ec1ccbSHans Petter Selasky static inline struct mthca_cqe *get_cqe_from_buf(struct mthca_cq_buf *buf,
160*33ec1ccbSHans Petter Selasky 						 int entry)
161*33ec1ccbSHans Petter Selasky {
162*33ec1ccbSHans Petter Selasky 	if (buf->is_direct)
163*33ec1ccbSHans Petter Selasky 		return buf->queue.direct.buf + (entry * MTHCA_CQ_ENTRY_SIZE);
164*33ec1ccbSHans Petter Selasky 	else
165*33ec1ccbSHans Petter Selasky 		return buf->queue.page_list[entry * MTHCA_CQ_ENTRY_SIZE / PAGE_SIZE].buf
166*33ec1ccbSHans Petter Selasky 			+ (entry * MTHCA_CQ_ENTRY_SIZE) % PAGE_SIZE;
167*33ec1ccbSHans Petter Selasky }
168*33ec1ccbSHans Petter Selasky 
get_cqe(struct mthca_cq * cq,int entry)169*33ec1ccbSHans Petter Selasky static inline struct mthca_cqe *get_cqe(struct mthca_cq *cq, int entry)
170*33ec1ccbSHans Petter Selasky {
171*33ec1ccbSHans Petter Selasky 	return get_cqe_from_buf(&cq->buf, entry);
172*33ec1ccbSHans Petter Selasky }
173*33ec1ccbSHans Petter Selasky 
cqe_sw(struct mthca_cqe * cqe)174*33ec1ccbSHans Petter Selasky static inline struct mthca_cqe *cqe_sw(struct mthca_cqe *cqe)
175*33ec1ccbSHans Petter Selasky {
176*33ec1ccbSHans Petter Selasky 	return MTHCA_CQ_ENTRY_OWNER_HW & cqe->owner ? NULL : cqe;
177*33ec1ccbSHans Petter Selasky }
178*33ec1ccbSHans Petter Selasky 
next_cqe_sw(struct mthca_cq * cq)179*33ec1ccbSHans Petter Selasky static inline struct mthca_cqe *next_cqe_sw(struct mthca_cq *cq)
180*33ec1ccbSHans Petter Selasky {
181*33ec1ccbSHans Petter Selasky 	return cqe_sw(get_cqe(cq, cq->cons_index & cq->ibcq.cqe));
182*33ec1ccbSHans Petter Selasky }
183*33ec1ccbSHans Petter Selasky 
set_cqe_hw(struct mthca_cqe * cqe)184*33ec1ccbSHans Petter Selasky static inline void set_cqe_hw(struct mthca_cqe *cqe)
185*33ec1ccbSHans Petter Selasky {
186*33ec1ccbSHans Petter Selasky 	cqe->owner = MTHCA_CQ_ENTRY_OWNER_HW;
187*33ec1ccbSHans Petter Selasky }
188*33ec1ccbSHans Petter Selasky 
dump_cqe(struct mthca_dev * dev,void * cqe_ptr)189*33ec1ccbSHans Petter Selasky static void dump_cqe(struct mthca_dev *dev, void *cqe_ptr)
190*33ec1ccbSHans Petter Selasky {
191*33ec1ccbSHans Petter Selasky 	__be32 *cqe = cqe_ptr;
192*33ec1ccbSHans Petter Selasky 
193*33ec1ccbSHans Petter Selasky 	(void) cqe;	/* avoid warning if mthca_dbg compiled away... */
194*33ec1ccbSHans Petter Selasky 	mthca_dbg(dev, "CQE contents %08x %08x %08x %08x %08x %08x %08x %08x\n",
195*33ec1ccbSHans Petter Selasky 		  be32_to_cpu(cqe[0]), be32_to_cpu(cqe[1]), be32_to_cpu(cqe[2]),
196*33ec1ccbSHans Petter Selasky 		  be32_to_cpu(cqe[3]), be32_to_cpu(cqe[4]), be32_to_cpu(cqe[5]),
197*33ec1ccbSHans Petter Selasky 		  be32_to_cpu(cqe[6]), be32_to_cpu(cqe[7]));
198*33ec1ccbSHans Petter Selasky }
199*33ec1ccbSHans Petter Selasky 
200*33ec1ccbSHans Petter Selasky /*
201*33ec1ccbSHans Petter Selasky  * incr is ignored in native Arbel (mem-free) mode, so cq->cons_index
202*33ec1ccbSHans Petter Selasky  * should be correct before calling update_cons_index().
203*33ec1ccbSHans Petter Selasky  */
update_cons_index(struct mthca_dev * dev,struct mthca_cq * cq,int incr)204*33ec1ccbSHans Petter Selasky static inline void update_cons_index(struct mthca_dev *dev, struct mthca_cq *cq,
205*33ec1ccbSHans Petter Selasky 				     int incr)
206*33ec1ccbSHans Petter Selasky {
207*33ec1ccbSHans Petter Selasky 	if (mthca_is_memfree(dev)) {
208*33ec1ccbSHans Petter Selasky 		*cq->set_ci_db = cpu_to_be32(cq->cons_index);
209*33ec1ccbSHans Petter Selasky 		wmb();
210*33ec1ccbSHans Petter Selasky 	} else {
211*33ec1ccbSHans Petter Selasky 		mthca_write64(MTHCA_TAVOR_CQ_DB_INC_CI | cq->cqn, incr - 1,
212*33ec1ccbSHans Petter Selasky 			      dev->kar + MTHCA_CQ_DOORBELL,
213*33ec1ccbSHans Petter Selasky 			      MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
214*33ec1ccbSHans Petter Selasky 		/*
215*33ec1ccbSHans Petter Selasky 		 * Make sure doorbells don't leak out of CQ spinlock
216*33ec1ccbSHans Petter Selasky 		 * and reach the HCA out of order:
217*33ec1ccbSHans Petter Selasky 		 */
218*33ec1ccbSHans Petter Selasky 		mmiowb();
219*33ec1ccbSHans Petter Selasky 	}
220*33ec1ccbSHans Petter Selasky }
221*33ec1ccbSHans Petter Selasky 
mthca_cq_completion(struct mthca_dev * dev,u32 cqn)222*33ec1ccbSHans Petter Selasky void mthca_cq_completion(struct mthca_dev *dev, u32 cqn)
223*33ec1ccbSHans Petter Selasky {
224*33ec1ccbSHans Petter Selasky 	struct mthca_cq *cq;
225*33ec1ccbSHans Petter Selasky 
226*33ec1ccbSHans Petter Selasky 	cq = mthca_array_get(&dev->cq_table.cq, cqn & (dev->limits.num_cqs - 1));
227*33ec1ccbSHans Petter Selasky 
228*33ec1ccbSHans Petter Selasky 	if (!cq) {
229*33ec1ccbSHans Petter Selasky 		mthca_warn(dev, "Completion event for bogus CQ %08x\n", cqn);
230*33ec1ccbSHans Petter Selasky 		return;
231*33ec1ccbSHans Petter Selasky 	}
232*33ec1ccbSHans Petter Selasky 
233*33ec1ccbSHans Petter Selasky 	++cq->arm_sn;
234*33ec1ccbSHans Petter Selasky 
235*33ec1ccbSHans Petter Selasky 	cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
236*33ec1ccbSHans Petter Selasky }
237*33ec1ccbSHans Petter Selasky 
mthca_cq_event(struct mthca_dev * dev,u32 cqn,enum ib_event_type event_type)238*33ec1ccbSHans Petter Selasky void mthca_cq_event(struct mthca_dev *dev, u32 cqn,
239*33ec1ccbSHans Petter Selasky 		    enum ib_event_type event_type)
240*33ec1ccbSHans Petter Selasky {
241*33ec1ccbSHans Petter Selasky 	struct mthca_cq *cq;
242*33ec1ccbSHans Petter Selasky 	struct ib_event event;
243*33ec1ccbSHans Petter Selasky 
244*33ec1ccbSHans Petter Selasky 	spin_lock(&dev->cq_table.lock);
245*33ec1ccbSHans Petter Selasky 
246*33ec1ccbSHans Petter Selasky 	cq = mthca_array_get(&dev->cq_table.cq, cqn & (dev->limits.num_cqs - 1));
247*33ec1ccbSHans Petter Selasky 	if (cq)
248*33ec1ccbSHans Petter Selasky 		++cq->refcount;
249*33ec1ccbSHans Petter Selasky 
250*33ec1ccbSHans Petter Selasky 	spin_unlock(&dev->cq_table.lock);
251*33ec1ccbSHans Petter Selasky 
252*33ec1ccbSHans Petter Selasky 	if (!cq) {
253*33ec1ccbSHans Petter Selasky 		mthca_warn(dev, "Async event for bogus CQ %08x\n", cqn);
254*33ec1ccbSHans Petter Selasky 		return;
255*33ec1ccbSHans Petter Selasky 	}
256*33ec1ccbSHans Petter Selasky 
257*33ec1ccbSHans Petter Selasky 	event.device      = &dev->ib_dev;
258*33ec1ccbSHans Petter Selasky 	event.event       = event_type;
259*33ec1ccbSHans Petter Selasky 	event.element.cq  = &cq->ibcq;
260*33ec1ccbSHans Petter Selasky 	if (cq->ibcq.event_handler)
261*33ec1ccbSHans Petter Selasky 		cq->ibcq.event_handler(&event, cq->ibcq.cq_context);
262*33ec1ccbSHans Petter Selasky 
263*33ec1ccbSHans Petter Selasky 	spin_lock(&dev->cq_table.lock);
264*33ec1ccbSHans Petter Selasky 	if (!--cq->refcount)
265*33ec1ccbSHans Petter Selasky 		wake_up(&cq->wait);
266*33ec1ccbSHans Petter Selasky 	spin_unlock(&dev->cq_table.lock);
267*33ec1ccbSHans Petter Selasky }
268*33ec1ccbSHans Petter Selasky 
is_recv_cqe(struct mthca_cqe * cqe)269*33ec1ccbSHans Petter Selasky static inline int is_recv_cqe(struct mthca_cqe *cqe)
270*33ec1ccbSHans Petter Selasky {
271*33ec1ccbSHans Petter Selasky 	if ((cqe->opcode & MTHCA_ERROR_CQE_OPCODE_MASK) ==
272*33ec1ccbSHans Petter Selasky 	    MTHCA_ERROR_CQE_OPCODE_MASK)
273*33ec1ccbSHans Petter Selasky 		return !(cqe->opcode & 0x01);
274*33ec1ccbSHans Petter Selasky 	else
275*33ec1ccbSHans Petter Selasky 		return !(cqe->is_send & 0x80);
276*33ec1ccbSHans Petter Selasky }
277*33ec1ccbSHans Petter Selasky 
mthca_cq_clean(struct mthca_dev * dev,struct mthca_cq * cq,u32 qpn,struct mthca_srq * srq)278*33ec1ccbSHans Petter Selasky void mthca_cq_clean(struct mthca_dev *dev, struct mthca_cq *cq, u32 qpn,
279*33ec1ccbSHans Petter Selasky 		    struct mthca_srq *srq)
280*33ec1ccbSHans Petter Selasky {
281*33ec1ccbSHans Petter Selasky 	struct mthca_cqe *cqe;
282*33ec1ccbSHans Petter Selasky 	u32 prod_index;
283*33ec1ccbSHans Petter Selasky 	int i, nfreed = 0;
284*33ec1ccbSHans Petter Selasky 
285*33ec1ccbSHans Petter Selasky 	spin_lock_irq(&cq->lock);
286*33ec1ccbSHans Petter Selasky 
287*33ec1ccbSHans Petter Selasky 	/*
288*33ec1ccbSHans Petter Selasky 	 * First we need to find the current producer index, so we
289*33ec1ccbSHans Petter Selasky 	 * know where to start cleaning from.  It doesn't matter if HW
290*33ec1ccbSHans Petter Selasky 	 * adds new entries after this loop -- the QP we're worried
291*33ec1ccbSHans Petter Selasky 	 * about is already in RESET, so the new entries won't come
292*33ec1ccbSHans Petter Selasky 	 * from our QP and therefore don't need to be checked.
293*33ec1ccbSHans Petter Selasky 	 */
294*33ec1ccbSHans Petter Selasky 	for (prod_index = cq->cons_index;
295*33ec1ccbSHans Petter Selasky 	     cqe_sw(get_cqe(cq, prod_index & cq->ibcq.cqe));
296*33ec1ccbSHans Petter Selasky 	     ++prod_index)
297*33ec1ccbSHans Petter Selasky 		if (prod_index == cq->cons_index + cq->ibcq.cqe)
298*33ec1ccbSHans Petter Selasky 			break;
299*33ec1ccbSHans Petter Selasky 
300*33ec1ccbSHans Petter Selasky 	if (0)
301*33ec1ccbSHans Petter Selasky 		mthca_dbg(dev, "Cleaning QPN %06x from CQN %06x; ci %d, pi %d\n",
302*33ec1ccbSHans Petter Selasky 			  qpn, cq->cqn, cq->cons_index, prod_index);
303*33ec1ccbSHans Petter Selasky 
304*33ec1ccbSHans Petter Selasky 	/*
305*33ec1ccbSHans Petter Selasky 	 * Now sweep backwards through the CQ, removing CQ entries
306*33ec1ccbSHans Petter Selasky 	 * that match our QP by copying older entries on top of them.
307*33ec1ccbSHans Petter Selasky 	 */
308*33ec1ccbSHans Petter Selasky 	while ((int) --prod_index - (int) cq->cons_index >= 0) {
309*33ec1ccbSHans Petter Selasky 		cqe = get_cqe(cq, prod_index & cq->ibcq.cqe);
310*33ec1ccbSHans Petter Selasky 		if (cqe->my_qpn == cpu_to_be32(qpn)) {
311*33ec1ccbSHans Petter Selasky 			if (srq && is_recv_cqe(cqe))
312*33ec1ccbSHans Petter Selasky 				mthca_free_srq_wqe(srq, be32_to_cpu(cqe->wqe));
313*33ec1ccbSHans Petter Selasky 			++nfreed;
314*33ec1ccbSHans Petter Selasky 		} else if (nfreed)
315*33ec1ccbSHans Petter Selasky 			memcpy(get_cqe(cq, (prod_index + nfreed) & cq->ibcq.cqe),
316*33ec1ccbSHans Petter Selasky 			       cqe, MTHCA_CQ_ENTRY_SIZE);
317*33ec1ccbSHans Petter Selasky 	}
318*33ec1ccbSHans Petter Selasky 
319*33ec1ccbSHans Petter Selasky 	if (nfreed) {
320*33ec1ccbSHans Petter Selasky 		for (i = 0; i < nfreed; ++i)
321*33ec1ccbSHans Petter Selasky 			set_cqe_hw(get_cqe(cq, (cq->cons_index + i) & cq->ibcq.cqe));
322*33ec1ccbSHans Petter Selasky 		wmb();
323*33ec1ccbSHans Petter Selasky 		cq->cons_index += nfreed;
324*33ec1ccbSHans Petter Selasky 		update_cons_index(dev, cq, nfreed);
325*33ec1ccbSHans Petter Selasky 	}
326*33ec1ccbSHans Petter Selasky 
327*33ec1ccbSHans Petter Selasky 	spin_unlock_irq(&cq->lock);
328*33ec1ccbSHans Petter Selasky }
329*33ec1ccbSHans Petter Selasky 
mthca_cq_resize_copy_cqes(struct mthca_cq * cq)330*33ec1ccbSHans Petter Selasky void mthca_cq_resize_copy_cqes(struct mthca_cq *cq)
331*33ec1ccbSHans Petter Selasky {
332*33ec1ccbSHans Petter Selasky 	int i;
333*33ec1ccbSHans Petter Selasky 
334*33ec1ccbSHans Petter Selasky 	/*
335*33ec1ccbSHans Petter Selasky 	 * In Tavor mode, the hardware keeps the consumer and producer
336*33ec1ccbSHans Petter Selasky 	 * indices mod the CQ size.  Since we might be making the CQ
337*33ec1ccbSHans Petter Selasky 	 * bigger, we need to deal with the case where the producer
338*33ec1ccbSHans Petter Selasky 	 * index wrapped around before the CQ was resized.
339*33ec1ccbSHans Petter Selasky 	 */
340*33ec1ccbSHans Petter Selasky 	if (!mthca_is_memfree(to_mdev(cq->ibcq.device)) &&
341*33ec1ccbSHans Petter Selasky 	    cq->ibcq.cqe < cq->resize_buf->cqe) {
342*33ec1ccbSHans Petter Selasky 		cq->cons_index &= cq->ibcq.cqe;
343*33ec1ccbSHans Petter Selasky 		if (cqe_sw(get_cqe(cq, cq->ibcq.cqe)))
344*33ec1ccbSHans Petter Selasky 			cq->cons_index -= cq->ibcq.cqe + 1;
345*33ec1ccbSHans Petter Selasky 	}
346*33ec1ccbSHans Petter Selasky 
347*33ec1ccbSHans Petter Selasky 	for (i = cq->cons_index; cqe_sw(get_cqe(cq, i & cq->ibcq.cqe)); ++i)
348*33ec1ccbSHans Petter Selasky 		memcpy(get_cqe_from_buf(&cq->resize_buf->buf,
349*33ec1ccbSHans Petter Selasky 					i & cq->resize_buf->cqe),
350*33ec1ccbSHans Petter Selasky 		       get_cqe(cq, i & cq->ibcq.cqe), MTHCA_CQ_ENTRY_SIZE);
351*33ec1ccbSHans Petter Selasky }
352*33ec1ccbSHans Petter Selasky 
mthca_alloc_cq_buf(struct mthca_dev * dev,struct mthca_cq_buf * buf,int nent)353*33ec1ccbSHans Petter Selasky int mthca_alloc_cq_buf(struct mthca_dev *dev, struct mthca_cq_buf *buf, int nent)
354*33ec1ccbSHans Petter Selasky {
355*33ec1ccbSHans Petter Selasky 	int ret;
356*33ec1ccbSHans Petter Selasky 	int i;
357*33ec1ccbSHans Petter Selasky 
358*33ec1ccbSHans Petter Selasky 	ret = mthca_buf_alloc(dev, nent * MTHCA_CQ_ENTRY_SIZE,
359*33ec1ccbSHans Petter Selasky 			      MTHCA_MAX_DIRECT_CQ_SIZE,
360*33ec1ccbSHans Petter Selasky 			      &buf->queue, &buf->is_direct,
361*33ec1ccbSHans Petter Selasky 			      &dev->driver_pd, 1, &buf->mr);
362*33ec1ccbSHans Petter Selasky 	if (ret)
363*33ec1ccbSHans Petter Selasky 		return ret;
364*33ec1ccbSHans Petter Selasky 
365*33ec1ccbSHans Petter Selasky 	for (i = 0; i < nent; ++i)
366*33ec1ccbSHans Petter Selasky 		set_cqe_hw(get_cqe_from_buf(buf, i));
367*33ec1ccbSHans Petter Selasky 
368*33ec1ccbSHans Petter Selasky 	return 0;
369*33ec1ccbSHans Petter Selasky }
370*33ec1ccbSHans Petter Selasky 
mthca_free_cq_buf(struct mthca_dev * dev,struct mthca_cq_buf * buf,int cqe)371*33ec1ccbSHans Petter Selasky void mthca_free_cq_buf(struct mthca_dev *dev, struct mthca_cq_buf *buf, int cqe)
372*33ec1ccbSHans Petter Selasky {
373*33ec1ccbSHans Petter Selasky 	mthca_buf_free(dev, (cqe + 1) * MTHCA_CQ_ENTRY_SIZE, &buf->queue,
374*33ec1ccbSHans Petter Selasky 		       buf->is_direct, &buf->mr);
375*33ec1ccbSHans Petter Selasky }
376*33ec1ccbSHans Petter Selasky 
handle_error_cqe(struct mthca_dev * dev,struct mthca_cq * cq,struct mthca_qp * qp,int wqe_index,int is_send,struct mthca_err_cqe * cqe,struct ib_wc * entry,int * free_cqe)377*33ec1ccbSHans Petter Selasky static void handle_error_cqe(struct mthca_dev *dev, struct mthca_cq *cq,
378*33ec1ccbSHans Petter Selasky 			     struct mthca_qp *qp, int wqe_index, int is_send,
379*33ec1ccbSHans Petter Selasky 			     struct mthca_err_cqe *cqe,
380*33ec1ccbSHans Petter Selasky 			     struct ib_wc *entry, int *free_cqe)
381*33ec1ccbSHans Petter Selasky {
382*33ec1ccbSHans Petter Selasky 	int dbd;
383*33ec1ccbSHans Petter Selasky 	__be32 new_wqe;
384*33ec1ccbSHans Petter Selasky 
385*33ec1ccbSHans Petter Selasky 	if (cqe->syndrome == SYNDROME_LOCAL_QP_OP_ERR) {
386*33ec1ccbSHans Petter Selasky 		mthca_dbg(dev, "local QP operation err "
387*33ec1ccbSHans Petter Selasky 			  "(QPN %06x, WQE @ %08x, CQN %06x, index %d)\n",
388*33ec1ccbSHans Petter Selasky 			  be32_to_cpu(cqe->my_qpn), be32_to_cpu(cqe->wqe),
389*33ec1ccbSHans Petter Selasky 			  cq->cqn, cq->cons_index);
390*33ec1ccbSHans Petter Selasky 		dump_cqe(dev, cqe);
391*33ec1ccbSHans Petter Selasky 	}
392*33ec1ccbSHans Petter Selasky 
393*33ec1ccbSHans Petter Selasky 	/*
394*33ec1ccbSHans Petter Selasky 	 * For completions in error, only work request ID, status, vendor error
395*33ec1ccbSHans Petter Selasky 	 * (and freed resource count for RD) have to be set.
396*33ec1ccbSHans Petter Selasky 	 */
397*33ec1ccbSHans Petter Selasky 	switch (cqe->syndrome) {
398*33ec1ccbSHans Petter Selasky 	case SYNDROME_LOCAL_LENGTH_ERR:
399*33ec1ccbSHans Petter Selasky 		entry->status = IB_WC_LOC_LEN_ERR;
400*33ec1ccbSHans Petter Selasky 		break;
401*33ec1ccbSHans Petter Selasky 	case SYNDROME_LOCAL_QP_OP_ERR:
402*33ec1ccbSHans Petter Selasky 		entry->status = IB_WC_LOC_QP_OP_ERR;
403*33ec1ccbSHans Petter Selasky 		break;
404*33ec1ccbSHans Petter Selasky 	case SYNDROME_LOCAL_EEC_OP_ERR:
405*33ec1ccbSHans Petter Selasky 		entry->status = IB_WC_LOC_EEC_OP_ERR;
406*33ec1ccbSHans Petter Selasky 		break;
407*33ec1ccbSHans Petter Selasky 	case SYNDROME_LOCAL_PROT_ERR:
408*33ec1ccbSHans Petter Selasky 		entry->status = IB_WC_LOC_PROT_ERR;
409*33ec1ccbSHans Petter Selasky 		break;
410*33ec1ccbSHans Petter Selasky 	case SYNDROME_WR_FLUSH_ERR:
411*33ec1ccbSHans Petter Selasky 		entry->status = IB_WC_WR_FLUSH_ERR;
412*33ec1ccbSHans Petter Selasky 		break;
413*33ec1ccbSHans Petter Selasky 	case SYNDROME_MW_BIND_ERR:
414*33ec1ccbSHans Petter Selasky 		entry->status = IB_WC_MW_BIND_ERR;
415*33ec1ccbSHans Petter Selasky 		break;
416*33ec1ccbSHans Petter Selasky 	case SYNDROME_BAD_RESP_ERR:
417*33ec1ccbSHans Petter Selasky 		entry->status = IB_WC_BAD_RESP_ERR;
418*33ec1ccbSHans Petter Selasky 		break;
419*33ec1ccbSHans Petter Selasky 	case SYNDROME_LOCAL_ACCESS_ERR:
420*33ec1ccbSHans Petter Selasky 		entry->status = IB_WC_LOC_ACCESS_ERR;
421*33ec1ccbSHans Petter Selasky 		break;
422*33ec1ccbSHans Petter Selasky 	case SYNDROME_REMOTE_INVAL_REQ_ERR:
423*33ec1ccbSHans Petter Selasky 		entry->status = IB_WC_REM_INV_REQ_ERR;
424*33ec1ccbSHans Petter Selasky 		break;
425*33ec1ccbSHans Petter Selasky 	case SYNDROME_REMOTE_ACCESS_ERR:
426*33ec1ccbSHans Petter Selasky 		entry->status = IB_WC_REM_ACCESS_ERR;
427*33ec1ccbSHans Petter Selasky 		break;
428*33ec1ccbSHans Petter Selasky 	case SYNDROME_REMOTE_OP_ERR:
429*33ec1ccbSHans Petter Selasky 		entry->status = IB_WC_REM_OP_ERR;
430*33ec1ccbSHans Petter Selasky 		break;
431*33ec1ccbSHans Petter Selasky 	case SYNDROME_RETRY_EXC_ERR:
432*33ec1ccbSHans Petter Selasky 		entry->status = IB_WC_RETRY_EXC_ERR;
433*33ec1ccbSHans Petter Selasky 		break;
434*33ec1ccbSHans Petter Selasky 	case SYNDROME_RNR_RETRY_EXC_ERR:
435*33ec1ccbSHans Petter Selasky 		entry->status = IB_WC_RNR_RETRY_EXC_ERR;
436*33ec1ccbSHans Petter Selasky 		break;
437*33ec1ccbSHans Petter Selasky 	case SYNDROME_LOCAL_RDD_VIOL_ERR:
438*33ec1ccbSHans Petter Selasky 		entry->status = IB_WC_LOC_RDD_VIOL_ERR;
439*33ec1ccbSHans Petter Selasky 		break;
440*33ec1ccbSHans Petter Selasky 	case SYNDROME_REMOTE_INVAL_RD_REQ_ERR:
441*33ec1ccbSHans Petter Selasky 		entry->status = IB_WC_REM_INV_RD_REQ_ERR;
442*33ec1ccbSHans Petter Selasky 		break;
443*33ec1ccbSHans Petter Selasky 	case SYNDROME_REMOTE_ABORTED_ERR:
444*33ec1ccbSHans Petter Selasky 		entry->status = IB_WC_REM_ABORT_ERR;
445*33ec1ccbSHans Petter Selasky 		break;
446*33ec1ccbSHans Petter Selasky 	case SYNDROME_INVAL_EECN_ERR:
447*33ec1ccbSHans Petter Selasky 		entry->status = IB_WC_INV_EECN_ERR;
448*33ec1ccbSHans Petter Selasky 		break;
449*33ec1ccbSHans Petter Selasky 	case SYNDROME_INVAL_EEC_STATE_ERR:
450*33ec1ccbSHans Petter Selasky 		entry->status = IB_WC_INV_EEC_STATE_ERR;
451*33ec1ccbSHans Petter Selasky 		break;
452*33ec1ccbSHans Petter Selasky 	default:
453*33ec1ccbSHans Petter Selasky 		entry->status = IB_WC_GENERAL_ERR;
454*33ec1ccbSHans Petter Selasky 		break;
455*33ec1ccbSHans Petter Selasky 	}
456*33ec1ccbSHans Petter Selasky 
457*33ec1ccbSHans Petter Selasky 	entry->vendor_err = cqe->vendor_err;
458*33ec1ccbSHans Petter Selasky 
459*33ec1ccbSHans Petter Selasky 	/*
460*33ec1ccbSHans Petter Selasky 	 * Mem-free HCAs always generate one CQE per WQE, even in the
461*33ec1ccbSHans Petter Selasky 	 * error case, so we don't have to check the doorbell count, etc.
462*33ec1ccbSHans Petter Selasky 	 */
463*33ec1ccbSHans Petter Selasky 	if (mthca_is_memfree(dev))
464*33ec1ccbSHans Petter Selasky 		return;
465*33ec1ccbSHans Petter Selasky 
466*33ec1ccbSHans Petter Selasky 	mthca_free_err_wqe(dev, qp, is_send, wqe_index, &dbd, &new_wqe);
467*33ec1ccbSHans Petter Selasky 
468*33ec1ccbSHans Petter Selasky 	/*
469*33ec1ccbSHans Petter Selasky 	 * If we're at the end of the WQE chain, or we've used up our
470*33ec1ccbSHans Petter Selasky 	 * doorbell count, free the CQE.  Otherwise just update it for
471*33ec1ccbSHans Petter Selasky 	 * the next poll operation.
472*33ec1ccbSHans Petter Selasky 	 */
473*33ec1ccbSHans Petter Selasky 	if (!(new_wqe & cpu_to_be32(0x3f)) || (!cqe->db_cnt && dbd))
474*33ec1ccbSHans Petter Selasky 		return;
475*33ec1ccbSHans Petter Selasky 
476*33ec1ccbSHans Petter Selasky 	be16_add_cpu(&cqe->db_cnt, -dbd);
477*33ec1ccbSHans Petter Selasky 	cqe->wqe      = new_wqe;
478*33ec1ccbSHans Petter Selasky 	cqe->syndrome = SYNDROME_WR_FLUSH_ERR;
479*33ec1ccbSHans Petter Selasky 
480*33ec1ccbSHans Petter Selasky 	*free_cqe = 0;
481*33ec1ccbSHans Petter Selasky }
482*33ec1ccbSHans Petter Selasky 
mthca_poll_one(struct mthca_dev * dev,struct mthca_cq * cq,struct mthca_qp ** cur_qp,int * freed,struct ib_wc * entry)483*33ec1ccbSHans Petter Selasky static inline int mthca_poll_one(struct mthca_dev *dev,
484*33ec1ccbSHans Petter Selasky 				 struct mthca_cq *cq,
485*33ec1ccbSHans Petter Selasky 				 struct mthca_qp **cur_qp,
486*33ec1ccbSHans Petter Selasky 				 int *freed,
487*33ec1ccbSHans Petter Selasky 				 struct ib_wc *entry)
488*33ec1ccbSHans Petter Selasky {
489*33ec1ccbSHans Petter Selasky 	struct mthca_wq *wq;
490*33ec1ccbSHans Petter Selasky 	struct mthca_cqe *cqe;
491*33ec1ccbSHans Petter Selasky 	int wqe_index;
492*33ec1ccbSHans Petter Selasky 	int is_error;
493*33ec1ccbSHans Petter Selasky 	int is_send;
494*33ec1ccbSHans Petter Selasky 	int free_cqe = 1;
495*33ec1ccbSHans Petter Selasky 	int err = 0;
496*33ec1ccbSHans Petter Selasky 	u16 checksum;
497*33ec1ccbSHans Petter Selasky 
498*33ec1ccbSHans Petter Selasky 	cqe = next_cqe_sw(cq);
499*33ec1ccbSHans Petter Selasky 	if (!cqe)
500*33ec1ccbSHans Petter Selasky 		return -EAGAIN;
501*33ec1ccbSHans Petter Selasky 
502*33ec1ccbSHans Petter Selasky 	/*
503*33ec1ccbSHans Petter Selasky 	 * Make sure we read CQ entry contents after we've checked the
504*33ec1ccbSHans Petter Selasky 	 * ownership bit.
505*33ec1ccbSHans Petter Selasky 	 */
506*33ec1ccbSHans Petter Selasky 	rmb();
507*33ec1ccbSHans Petter Selasky 
508*33ec1ccbSHans Petter Selasky 	if (0) {
509*33ec1ccbSHans Petter Selasky 		mthca_dbg(dev, "%x/%d: CQE -> QPN %06x, WQE @ %08x\n",
510*33ec1ccbSHans Petter Selasky 			  cq->cqn, cq->cons_index, be32_to_cpu(cqe->my_qpn),
511*33ec1ccbSHans Petter Selasky 			  be32_to_cpu(cqe->wqe));
512*33ec1ccbSHans Petter Selasky 		dump_cqe(dev, cqe);
513*33ec1ccbSHans Petter Selasky 	}
514*33ec1ccbSHans Petter Selasky 
515*33ec1ccbSHans Petter Selasky 	is_error = (cqe->opcode & MTHCA_ERROR_CQE_OPCODE_MASK) ==
516*33ec1ccbSHans Petter Selasky 		MTHCA_ERROR_CQE_OPCODE_MASK;
517*33ec1ccbSHans Petter Selasky 	is_send  = is_error ? cqe->opcode & 0x01 : cqe->is_send & 0x80;
518*33ec1ccbSHans Petter Selasky 
519*33ec1ccbSHans Petter Selasky 	if (!*cur_qp || be32_to_cpu(cqe->my_qpn) != (*cur_qp)->qpn) {
520*33ec1ccbSHans Petter Selasky 		/*
521*33ec1ccbSHans Petter Selasky 		 * We do not have to take the QP table lock here,
522*33ec1ccbSHans Petter Selasky 		 * because CQs will be locked while QPs are removed
523*33ec1ccbSHans Petter Selasky 		 * from the table.
524*33ec1ccbSHans Petter Selasky 		 */
525*33ec1ccbSHans Petter Selasky 		*cur_qp = mthca_array_get(&dev->qp_table.qp,
526*33ec1ccbSHans Petter Selasky 					  be32_to_cpu(cqe->my_qpn) &
527*33ec1ccbSHans Petter Selasky 					  (dev->limits.num_qps - 1));
528*33ec1ccbSHans Petter Selasky 		if (!*cur_qp) {
529*33ec1ccbSHans Petter Selasky 			mthca_warn(dev, "CQ entry for unknown QP %06x\n",
530*33ec1ccbSHans Petter Selasky 				   be32_to_cpu(cqe->my_qpn) & 0xffffff);
531*33ec1ccbSHans Petter Selasky 			err = -EINVAL;
532*33ec1ccbSHans Petter Selasky 			goto out;
533*33ec1ccbSHans Petter Selasky 		}
534*33ec1ccbSHans Petter Selasky 	}
535*33ec1ccbSHans Petter Selasky 
536*33ec1ccbSHans Petter Selasky 	entry->qp = &(*cur_qp)->ibqp;
537*33ec1ccbSHans Petter Selasky 
538*33ec1ccbSHans Petter Selasky 	if (is_send) {
539*33ec1ccbSHans Petter Selasky 		wq = &(*cur_qp)->sq;
540*33ec1ccbSHans Petter Selasky 		wqe_index = ((be32_to_cpu(cqe->wqe) - (*cur_qp)->send_wqe_offset)
541*33ec1ccbSHans Petter Selasky 			     >> wq->wqe_shift);
542*33ec1ccbSHans Petter Selasky 		entry->wr_id = (*cur_qp)->wrid[wqe_index +
543*33ec1ccbSHans Petter Selasky 					       (*cur_qp)->rq.max];
544*33ec1ccbSHans Petter Selasky 	} else if ((*cur_qp)->ibqp.srq) {
545*33ec1ccbSHans Petter Selasky 		struct mthca_srq *srq = to_msrq((*cur_qp)->ibqp.srq);
546*33ec1ccbSHans Petter Selasky 		u32 wqe = be32_to_cpu(cqe->wqe);
547*33ec1ccbSHans Petter Selasky 		wq = NULL;
548*33ec1ccbSHans Petter Selasky 		wqe_index = wqe >> srq->wqe_shift;
549*33ec1ccbSHans Petter Selasky 		entry->wr_id = srq->wrid[wqe_index];
550*33ec1ccbSHans Petter Selasky 		mthca_free_srq_wqe(srq, wqe);
551*33ec1ccbSHans Petter Selasky 	} else {
552*33ec1ccbSHans Petter Selasky 		s32 wqe;
553*33ec1ccbSHans Petter Selasky 		wq = &(*cur_qp)->rq;
554*33ec1ccbSHans Petter Selasky 		wqe = be32_to_cpu(cqe->wqe);
555*33ec1ccbSHans Petter Selasky 		wqe_index = wqe >> wq->wqe_shift;
556*33ec1ccbSHans Petter Selasky 		/*
557*33ec1ccbSHans Petter Selasky 		 * WQE addr == base - 1 might be reported in receive completion
558*33ec1ccbSHans Petter Selasky 		 * with error instead of (rq size - 1) by Sinai FW 1.0.800 and
559*33ec1ccbSHans Petter Selasky 		 * Arbel FW 5.1.400.  This bug should be fixed in later FW revs.
560*33ec1ccbSHans Petter Selasky 		 */
561*33ec1ccbSHans Petter Selasky 		if (unlikely(wqe_index < 0))
562*33ec1ccbSHans Petter Selasky 			wqe_index = wq->max - 1;
563*33ec1ccbSHans Petter Selasky 		entry->wr_id = (*cur_qp)->wrid[wqe_index];
564*33ec1ccbSHans Petter Selasky 	}
565*33ec1ccbSHans Petter Selasky 
566*33ec1ccbSHans Petter Selasky 	if (wq) {
567*33ec1ccbSHans Petter Selasky 		if (wq->last_comp < wqe_index)
568*33ec1ccbSHans Petter Selasky 			wq->tail += wqe_index - wq->last_comp;
569*33ec1ccbSHans Petter Selasky 		else
570*33ec1ccbSHans Petter Selasky 			wq->tail += wqe_index + wq->max - wq->last_comp;
571*33ec1ccbSHans Petter Selasky 
572*33ec1ccbSHans Petter Selasky 		wq->last_comp = wqe_index;
573*33ec1ccbSHans Petter Selasky 	}
574*33ec1ccbSHans Petter Selasky 
575*33ec1ccbSHans Petter Selasky 	if (is_error) {
576*33ec1ccbSHans Petter Selasky 		handle_error_cqe(dev, cq, *cur_qp, wqe_index, is_send,
577*33ec1ccbSHans Petter Selasky 				 (struct mthca_err_cqe *) cqe,
578*33ec1ccbSHans Petter Selasky 				 entry, &free_cqe);
579*33ec1ccbSHans Petter Selasky 		goto out;
580*33ec1ccbSHans Petter Selasky 	}
581*33ec1ccbSHans Petter Selasky 
582*33ec1ccbSHans Petter Selasky 	if (is_send) {
583*33ec1ccbSHans Petter Selasky 		entry->wc_flags = 0;
584*33ec1ccbSHans Petter Selasky 		switch (cqe->opcode) {
585*33ec1ccbSHans Petter Selasky 		case MTHCA_OPCODE_RDMA_WRITE:
586*33ec1ccbSHans Petter Selasky 			entry->opcode    = IB_WC_RDMA_WRITE;
587*33ec1ccbSHans Petter Selasky 			break;
588*33ec1ccbSHans Petter Selasky 		case MTHCA_OPCODE_RDMA_WRITE_IMM:
589*33ec1ccbSHans Petter Selasky 			entry->opcode    = IB_WC_RDMA_WRITE;
590*33ec1ccbSHans Petter Selasky 			entry->wc_flags |= IB_WC_WITH_IMM;
591*33ec1ccbSHans Petter Selasky 			break;
592*33ec1ccbSHans Petter Selasky 		case MTHCA_OPCODE_SEND:
593*33ec1ccbSHans Petter Selasky 			entry->opcode    = IB_WC_SEND;
594*33ec1ccbSHans Petter Selasky 			break;
595*33ec1ccbSHans Petter Selasky 		case MTHCA_OPCODE_SEND_IMM:
596*33ec1ccbSHans Petter Selasky 			entry->opcode    = IB_WC_SEND;
597*33ec1ccbSHans Petter Selasky 			entry->wc_flags |= IB_WC_WITH_IMM;
598*33ec1ccbSHans Petter Selasky 			break;
599*33ec1ccbSHans Petter Selasky 		case MTHCA_OPCODE_RDMA_READ:
600*33ec1ccbSHans Petter Selasky 			entry->opcode    = IB_WC_RDMA_READ;
601*33ec1ccbSHans Petter Selasky 			entry->byte_len  = be32_to_cpu(cqe->byte_cnt);
602*33ec1ccbSHans Petter Selasky 			break;
603*33ec1ccbSHans Petter Selasky 		case MTHCA_OPCODE_ATOMIC_CS:
604*33ec1ccbSHans Petter Selasky 			entry->opcode    = IB_WC_COMP_SWAP;
605*33ec1ccbSHans Petter Selasky 			entry->byte_len  = MTHCA_ATOMIC_BYTE_LEN;
606*33ec1ccbSHans Petter Selasky 			break;
607*33ec1ccbSHans Petter Selasky 		case MTHCA_OPCODE_ATOMIC_FA:
608*33ec1ccbSHans Petter Selasky 			entry->opcode    = IB_WC_FETCH_ADD;
609*33ec1ccbSHans Petter Selasky 			entry->byte_len  = MTHCA_ATOMIC_BYTE_LEN;
610*33ec1ccbSHans Petter Selasky 			break;
611*33ec1ccbSHans Petter Selasky 		default:
612*33ec1ccbSHans Petter Selasky 			entry->opcode    = MTHCA_OPCODE_INVALID;
613*33ec1ccbSHans Petter Selasky 			break;
614*33ec1ccbSHans Petter Selasky 		}
615*33ec1ccbSHans Petter Selasky 	} else {
616*33ec1ccbSHans Petter Selasky 		entry->byte_len = be32_to_cpu(cqe->byte_cnt);
617*33ec1ccbSHans Petter Selasky 		switch (cqe->opcode & 0x1f) {
618*33ec1ccbSHans Petter Selasky 		case IB_OPCODE_SEND_LAST_WITH_IMMEDIATE:
619*33ec1ccbSHans Petter Selasky 		case IB_OPCODE_SEND_ONLY_WITH_IMMEDIATE:
620*33ec1ccbSHans Petter Selasky 			entry->wc_flags = IB_WC_WITH_IMM;
621*33ec1ccbSHans Petter Selasky 			entry->ex.imm_data = cqe->imm_etype_pkey_eec;
622*33ec1ccbSHans Petter Selasky 			entry->opcode = IB_WC_RECV;
623*33ec1ccbSHans Petter Selasky 			break;
624*33ec1ccbSHans Petter Selasky 		case IB_OPCODE_RDMA_WRITE_LAST_WITH_IMMEDIATE:
625*33ec1ccbSHans Petter Selasky 		case IB_OPCODE_RDMA_WRITE_ONLY_WITH_IMMEDIATE:
626*33ec1ccbSHans Petter Selasky 			entry->wc_flags = IB_WC_WITH_IMM;
627*33ec1ccbSHans Petter Selasky 			entry->ex.imm_data = cqe->imm_etype_pkey_eec;
628*33ec1ccbSHans Petter Selasky 			entry->opcode = IB_WC_RECV_RDMA_WITH_IMM;
629*33ec1ccbSHans Petter Selasky 			break;
630*33ec1ccbSHans Petter Selasky 		default:
631*33ec1ccbSHans Petter Selasky 			entry->wc_flags = 0;
632*33ec1ccbSHans Petter Selasky 			entry->opcode = IB_WC_RECV;
633*33ec1ccbSHans Petter Selasky 			break;
634*33ec1ccbSHans Petter Selasky 		}
635*33ec1ccbSHans Petter Selasky 		entry->slid 	   = be16_to_cpu(cqe->rlid);
636*33ec1ccbSHans Petter Selasky 		entry->sl   	   = cqe->sl_ipok >> 4;
637*33ec1ccbSHans Petter Selasky 		entry->src_qp 	   = be32_to_cpu(cqe->rqpn) & 0xffffff;
638*33ec1ccbSHans Petter Selasky 		entry->dlid_path_bits = cqe->g_mlpath & 0x7f;
639*33ec1ccbSHans Petter Selasky 		entry->pkey_index  = be32_to_cpu(cqe->imm_etype_pkey_eec) >> 16;
640*33ec1ccbSHans Petter Selasky 		entry->wc_flags   |= cqe->g_mlpath & 0x80 ? IB_WC_GRH : 0;
641*33ec1ccbSHans Petter Selasky 		checksum = (be32_to_cpu(cqe->rqpn) >> 24) |
642*33ec1ccbSHans Petter Selasky 				((be32_to_cpu(cqe->my_ee) >> 16) & 0xff00);
643*33ec1ccbSHans Petter Selasky 		entry->wc_flags	  |=  (cqe->sl_ipok & 1 && checksum == 0xffff) ?
644*33ec1ccbSHans Petter Selasky 							IB_WC_IP_CSUM_OK : 0;
645*33ec1ccbSHans Petter Selasky 	}
646*33ec1ccbSHans Petter Selasky 
647*33ec1ccbSHans Petter Selasky 	entry->status = IB_WC_SUCCESS;
648*33ec1ccbSHans Petter Selasky 
649*33ec1ccbSHans Petter Selasky  out:
650*33ec1ccbSHans Petter Selasky 	if (likely(free_cqe)) {
651*33ec1ccbSHans Petter Selasky 		set_cqe_hw(cqe);
652*33ec1ccbSHans Petter Selasky 		++(*freed);
653*33ec1ccbSHans Petter Selasky 		++cq->cons_index;
654*33ec1ccbSHans Petter Selasky 	}
655*33ec1ccbSHans Petter Selasky 
656*33ec1ccbSHans Petter Selasky 	return err;
657*33ec1ccbSHans Petter Selasky }
658*33ec1ccbSHans Petter Selasky 
mthca_poll_cq(struct ib_cq * ibcq,int num_entries,struct ib_wc * entry)659*33ec1ccbSHans Petter Selasky int mthca_poll_cq(struct ib_cq *ibcq, int num_entries,
660*33ec1ccbSHans Petter Selasky 		  struct ib_wc *entry)
661*33ec1ccbSHans Petter Selasky {
662*33ec1ccbSHans Petter Selasky 	struct mthca_dev *dev = to_mdev(ibcq->device);
663*33ec1ccbSHans Petter Selasky 	struct mthca_cq *cq = to_mcq(ibcq);
664*33ec1ccbSHans Petter Selasky 	struct mthca_qp *qp = NULL;
665*33ec1ccbSHans Petter Selasky 	unsigned long flags;
666*33ec1ccbSHans Petter Selasky 	int err = 0;
667*33ec1ccbSHans Petter Selasky 	int freed = 0;
668*33ec1ccbSHans Petter Selasky 	int npolled;
669*33ec1ccbSHans Petter Selasky 
670*33ec1ccbSHans Petter Selasky 	spin_lock_irqsave(&cq->lock, flags);
671*33ec1ccbSHans Petter Selasky 
672*33ec1ccbSHans Petter Selasky 	npolled = 0;
673*33ec1ccbSHans Petter Selasky repoll:
674*33ec1ccbSHans Petter Selasky 	while (npolled < num_entries) {
675*33ec1ccbSHans Petter Selasky 		err = mthca_poll_one(dev, cq, &qp,
676*33ec1ccbSHans Petter Selasky 				     &freed, entry + npolled);
677*33ec1ccbSHans Petter Selasky 		if (err)
678*33ec1ccbSHans Petter Selasky 			break;
679*33ec1ccbSHans Petter Selasky 		++npolled;
680*33ec1ccbSHans Petter Selasky 	}
681*33ec1ccbSHans Petter Selasky 
682*33ec1ccbSHans Petter Selasky 	if (freed) {
683*33ec1ccbSHans Petter Selasky 		wmb();
684*33ec1ccbSHans Petter Selasky 		update_cons_index(dev, cq, freed);
685*33ec1ccbSHans Petter Selasky 	}
686*33ec1ccbSHans Petter Selasky 
687*33ec1ccbSHans Petter Selasky 	/*
688*33ec1ccbSHans Petter Selasky 	 * If a CQ resize is in progress and we discovered that the
689*33ec1ccbSHans Petter Selasky 	 * old buffer is empty, then peek in the new buffer, and if
690*33ec1ccbSHans Petter Selasky 	 * it's not empty, switch to the new buffer and continue
691*33ec1ccbSHans Petter Selasky 	 * polling there.
692*33ec1ccbSHans Petter Selasky 	 */
693*33ec1ccbSHans Petter Selasky 	if (unlikely(err == -EAGAIN && cq->resize_buf &&
694*33ec1ccbSHans Petter Selasky 		     cq->resize_buf->state == CQ_RESIZE_READY)) {
695*33ec1ccbSHans Petter Selasky 		/*
696*33ec1ccbSHans Petter Selasky 		 * In Tavor mode, the hardware keeps the producer
697*33ec1ccbSHans Petter Selasky 		 * index modulo the CQ size.  Since we might be making
698*33ec1ccbSHans Petter Selasky 		 * the CQ bigger, we need to mask our consumer index
699*33ec1ccbSHans Petter Selasky 		 * using the size of the old CQ buffer before looking
700*33ec1ccbSHans Petter Selasky 		 * in the new CQ buffer.
701*33ec1ccbSHans Petter Selasky 		 */
702*33ec1ccbSHans Petter Selasky 		if (!mthca_is_memfree(dev))
703*33ec1ccbSHans Petter Selasky 			cq->cons_index &= cq->ibcq.cqe;
704*33ec1ccbSHans Petter Selasky 
705*33ec1ccbSHans Petter Selasky 		if (cqe_sw(get_cqe_from_buf(&cq->resize_buf->buf,
706*33ec1ccbSHans Petter Selasky 					    cq->cons_index & cq->resize_buf->cqe))) {
707*33ec1ccbSHans Petter Selasky 			struct mthca_cq_buf tbuf;
708*33ec1ccbSHans Petter Selasky 			int tcqe;
709*33ec1ccbSHans Petter Selasky 
710*33ec1ccbSHans Petter Selasky 			tbuf         = cq->buf;
711*33ec1ccbSHans Petter Selasky 			tcqe         = cq->ibcq.cqe;
712*33ec1ccbSHans Petter Selasky 			cq->buf      = cq->resize_buf->buf;
713*33ec1ccbSHans Petter Selasky 			cq->ibcq.cqe = cq->resize_buf->cqe;
714*33ec1ccbSHans Petter Selasky 
715*33ec1ccbSHans Petter Selasky 			cq->resize_buf->buf   = tbuf;
716*33ec1ccbSHans Petter Selasky 			cq->resize_buf->cqe   = tcqe;
717*33ec1ccbSHans Petter Selasky 			cq->resize_buf->state = CQ_RESIZE_SWAPPED;
718*33ec1ccbSHans Petter Selasky 
719*33ec1ccbSHans Petter Selasky 			goto repoll;
720*33ec1ccbSHans Petter Selasky 		}
721*33ec1ccbSHans Petter Selasky 	}
722*33ec1ccbSHans Petter Selasky 
723*33ec1ccbSHans Petter Selasky 	spin_unlock_irqrestore(&cq->lock, flags);
724*33ec1ccbSHans Petter Selasky 
725*33ec1ccbSHans Petter Selasky 	return err == 0 || err == -EAGAIN ? npolled : err;
726*33ec1ccbSHans Petter Selasky }
727*33ec1ccbSHans Petter Selasky 
mthca_tavor_arm_cq(struct ib_cq * cq,enum ib_cq_notify_flags flags)728*33ec1ccbSHans Petter Selasky int mthca_tavor_arm_cq(struct ib_cq *cq, enum ib_cq_notify_flags flags)
729*33ec1ccbSHans Petter Selasky {
730*33ec1ccbSHans Petter Selasky 	u32 dbhi = ((flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?
731*33ec1ccbSHans Petter Selasky 		    MTHCA_TAVOR_CQ_DB_REQ_NOT_SOL :
732*33ec1ccbSHans Petter Selasky 		    MTHCA_TAVOR_CQ_DB_REQ_NOT) |
733*33ec1ccbSHans Petter Selasky 		to_mcq(cq)->cqn;
734*33ec1ccbSHans Petter Selasky 
735*33ec1ccbSHans Petter Selasky 	mthca_write64(dbhi, 0xffffffff, to_mdev(cq->device)->kar + MTHCA_CQ_DOORBELL,
736*33ec1ccbSHans Petter Selasky 		      MTHCA_GET_DOORBELL_LOCK(&to_mdev(cq->device)->doorbell_lock));
737*33ec1ccbSHans Petter Selasky 
738*33ec1ccbSHans Petter Selasky 	return 0;
739*33ec1ccbSHans Petter Selasky }
740*33ec1ccbSHans Petter Selasky 
mthca_arbel_arm_cq(struct ib_cq * ibcq,enum ib_cq_notify_flags flags)741*33ec1ccbSHans Petter Selasky int mthca_arbel_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
742*33ec1ccbSHans Petter Selasky {
743*33ec1ccbSHans Petter Selasky 	struct mthca_cq *cq = to_mcq(ibcq);
744*33ec1ccbSHans Petter Selasky 	__be32 db_rec[2];
745*33ec1ccbSHans Petter Selasky 	u32 dbhi;
746*33ec1ccbSHans Petter Selasky 	u32 sn = cq->arm_sn & 3;
747*33ec1ccbSHans Petter Selasky 
748*33ec1ccbSHans Petter Selasky 	db_rec[0] = cpu_to_be32(cq->cons_index);
749*33ec1ccbSHans Petter Selasky 	db_rec[1] = cpu_to_be32((cq->cqn << 8) | (2 << 5) | (sn << 3) |
750*33ec1ccbSHans Petter Selasky 				((flags & IB_CQ_SOLICITED_MASK) ==
751*33ec1ccbSHans Petter Selasky 				 IB_CQ_SOLICITED ? 1 : 2));
752*33ec1ccbSHans Petter Selasky 
753*33ec1ccbSHans Petter Selasky 	mthca_write_db_rec(db_rec, cq->arm_db);
754*33ec1ccbSHans Petter Selasky 
755*33ec1ccbSHans Petter Selasky 	/*
756*33ec1ccbSHans Petter Selasky 	 * Make sure that the doorbell record in host memory is
757*33ec1ccbSHans Petter Selasky 	 * written before ringing the doorbell via PCI MMIO.
758*33ec1ccbSHans Petter Selasky 	 */
759*33ec1ccbSHans Petter Selasky 	wmb();
760*33ec1ccbSHans Petter Selasky 
761*33ec1ccbSHans Petter Selasky 	dbhi = (sn << 28) |
762*33ec1ccbSHans Petter Selasky 		((flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?
763*33ec1ccbSHans Petter Selasky 		 MTHCA_ARBEL_CQ_DB_REQ_NOT_SOL :
764*33ec1ccbSHans Petter Selasky 		 MTHCA_ARBEL_CQ_DB_REQ_NOT) | cq->cqn;
765*33ec1ccbSHans Petter Selasky 
766*33ec1ccbSHans Petter Selasky 	mthca_write64(dbhi, cq->cons_index,
767*33ec1ccbSHans Petter Selasky 		      to_mdev(ibcq->device)->kar + MTHCA_CQ_DOORBELL,
768*33ec1ccbSHans Petter Selasky 		      MTHCA_GET_DOORBELL_LOCK(&to_mdev(ibcq->device)->doorbell_lock));
769*33ec1ccbSHans Petter Selasky 
770*33ec1ccbSHans Petter Selasky 	return 0;
771*33ec1ccbSHans Petter Selasky }
772*33ec1ccbSHans Petter Selasky 
mthca_init_cq(struct mthca_dev * dev,int nent,struct mthca_ucontext * ctx,u32 pdn,struct mthca_cq * cq)773*33ec1ccbSHans Petter Selasky int mthca_init_cq(struct mthca_dev *dev, int nent,
774*33ec1ccbSHans Petter Selasky 		  struct mthca_ucontext *ctx, u32 pdn,
775*33ec1ccbSHans Petter Selasky 		  struct mthca_cq *cq)
776*33ec1ccbSHans Petter Selasky {
777*33ec1ccbSHans Petter Selasky 	struct mthca_mailbox *mailbox;
778*33ec1ccbSHans Petter Selasky 	struct mthca_cq_context *cq_context;
779*33ec1ccbSHans Petter Selasky 	int err = -ENOMEM;
780*33ec1ccbSHans Petter Selasky 
781*33ec1ccbSHans Petter Selasky 	cq->ibcq.cqe  = nent - 1;
782*33ec1ccbSHans Petter Selasky 	cq->is_kernel = !ctx;
783*33ec1ccbSHans Petter Selasky 
784*33ec1ccbSHans Petter Selasky 	cq->cqn = mthca_alloc(&dev->cq_table.alloc);
785*33ec1ccbSHans Petter Selasky 	if (cq->cqn == -1)
786*33ec1ccbSHans Petter Selasky 		return -ENOMEM;
787*33ec1ccbSHans Petter Selasky 
788*33ec1ccbSHans Petter Selasky 	if (mthca_is_memfree(dev)) {
789*33ec1ccbSHans Petter Selasky 		err = mthca_table_get(dev, dev->cq_table.table, cq->cqn);
790*33ec1ccbSHans Petter Selasky 		if (err)
791*33ec1ccbSHans Petter Selasky 			goto err_out;
792*33ec1ccbSHans Petter Selasky 
793*33ec1ccbSHans Petter Selasky 		if (cq->is_kernel) {
794*33ec1ccbSHans Petter Selasky 			cq->arm_sn = 1;
795*33ec1ccbSHans Petter Selasky 
796*33ec1ccbSHans Petter Selasky 			err = -ENOMEM;
797*33ec1ccbSHans Petter Selasky 
798*33ec1ccbSHans Petter Selasky 			cq->set_ci_db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_CQ_SET_CI,
799*33ec1ccbSHans Petter Selasky 							     cq->cqn, &cq->set_ci_db);
800*33ec1ccbSHans Petter Selasky 			if (cq->set_ci_db_index < 0)
801*33ec1ccbSHans Petter Selasky 				goto err_out_icm;
802*33ec1ccbSHans Petter Selasky 
803*33ec1ccbSHans Petter Selasky 			cq->arm_db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_CQ_ARM,
804*33ec1ccbSHans Petter Selasky 							  cq->cqn, &cq->arm_db);
805*33ec1ccbSHans Petter Selasky 			if (cq->arm_db_index < 0)
806*33ec1ccbSHans Petter Selasky 				goto err_out_ci;
807*33ec1ccbSHans Petter Selasky 		}
808*33ec1ccbSHans Petter Selasky 	}
809*33ec1ccbSHans Petter Selasky 
810*33ec1ccbSHans Petter Selasky 	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
811*33ec1ccbSHans Petter Selasky 	if (IS_ERR(mailbox))
812*33ec1ccbSHans Petter Selasky 		goto err_out_arm;
813*33ec1ccbSHans Petter Selasky 
814*33ec1ccbSHans Petter Selasky 	cq_context = mailbox->buf;
815*33ec1ccbSHans Petter Selasky 
816*33ec1ccbSHans Petter Selasky 	if (cq->is_kernel) {
817*33ec1ccbSHans Petter Selasky 		err = mthca_alloc_cq_buf(dev, &cq->buf, nent);
818*33ec1ccbSHans Petter Selasky 		if (err)
819*33ec1ccbSHans Petter Selasky 			goto err_out_mailbox;
820*33ec1ccbSHans Petter Selasky 	}
821*33ec1ccbSHans Petter Selasky 
822*33ec1ccbSHans Petter Selasky 	spin_lock_init(&cq->lock);
823*33ec1ccbSHans Petter Selasky 	cq->refcount = 1;
824*33ec1ccbSHans Petter Selasky 	init_waitqueue_head(&cq->wait);
825*33ec1ccbSHans Petter Selasky 	mutex_init(&cq->mutex);
826*33ec1ccbSHans Petter Selasky 
827*33ec1ccbSHans Petter Selasky 	memset(cq_context, 0, sizeof *cq_context);
828*33ec1ccbSHans Petter Selasky 	cq_context->flags           = cpu_to_be32(MTHCA_CQ_STATUS_OK      |
829*33ec1ccbSHans Petter Selasky 						  MTHCA_CQ_STATE_DISARMED |
830*33ec1ccbSHans Petter Selasky 						  MTHCA_CQ_FLAG_TR);
831*33ec1ccbSHans Petter Selasky 	cq_context->logsize_usrpage = cpu_to_be32((ffs(nent) - 1) << 24);
832*33ec1ccbSHans Petter Selasky 	if (ctx)
833*33ec1ccbSHans Petter Selasky 		cq_context->logsize_usrpage |= cpu_to_be32(ctx->uar.index);
834*33ec1ccbSHans Petter Selasky 	else
835*33ec1ccbSHans Petter Selasky 		cq_context->logsize_usrpage |= cpu_to_be32(dev->driver_uar.index);
836*33ec1ccbSHans Petter Selasky 	cq_context->error_eqn       = cpu_to_be32(dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn);
837*33ec1ccbSHans Petter Selasky 	cq_context->comp_eqn        = cpu_to_be32(dev->eq_table.eq[MTHCA_EQ_COMP].eqn);
838*33ec1ccbSHans Petter Selasky 	cq_context->pd              = cpu_to_be32(pdn);
839*33ec1ccbSHans Petter Selasky 	cq_context->lkey            = cpu_to_be32(cq->buf.mr.ibmr.lkey);
840*33ec1ccbSHans Petter Selasky 	cq_context->cqn             = cpu_to_be32(cq->cqn);
841*33ec1ccbSHans Petter Selasky 
842*33ec1ccbSHans Petter Selasky 	if (mthca_is_memfree(dev)) {
843*33ec1ccbSHans Petter Selasky 		cq_context->ci_db    = cpu_to_be32(cq->set_ci_db_index);
844*33ec1ccbSHans Petter Selasky 		cq_context->state_db = cpu_to_be32(cq->arm_db_index);
845*33ec1ccbSHans Petter Selasky 	}
846*33ec1ccbSHans Petter Selasky 
847*33ec1ccbSHans Petter Selasky 	err = mthca_SW2HW_CQ(dev, mailbox, cq->cqn);
848*33ec1ccbSHans Petter Selasky 	if (err) {
849*33ec1ccbSHans Petter Selasky 		mthca_warn(dev, "SW2HW_CQ failed (%d)\n", err);
850*33ec1ccbSHans Petter Selasky 		goto err_out_free_mr;
851*33ec1ccbSHans Petter Selasky 	}
852*33ec1ccbSHans Petter Selasky 
853*33ec1ccbSHans Petter Selasky 	spin_lock_irq(&dev->cq_table.lock);
854*33ec1ccbSHans Petter Selasky 	if (mthca_array_set(&dev->cq_table.cq,
855*33ec1ccbSHans Petter Selasky 			    cq->cqn & (dev->limits.num_cqs - 1),
856*33ec1ccbSHans Petter Selasky 			    cq)) {
857*33ec1ccbSHans Petter Selasky 		spin_unlock_irq(&dev->cq_table.lock);
858*33ec1ccbSHans Petter Selasky 		goto err_out_free_mr;
859*33ec1ccbSHans Petter Selasky 	}
860*33ec1ccbSHans Petter Selasky 	spin_unlock_irq(&dev->cq_table.lock);
861*33ec1ccbSHans Petter Selasky 
862*33ec1ccbSHans Petter Selasky 	cq->cons_index = 0;
863*33ec1ccbSHans Petter Selasky 
864*33ec1ccbSHans Petter Selasky 	mthca_free_mailbox(dev, mailbox);
865*33ec1ccbSHans Petter Selasky 
866*33ec1ccbSHans Petter Selasky 	return 0;
867*33ec1ccbSHans Petter Selasky 
868*33ec1ccbSHans Petter Selasky err_out_free_mr:
869*33ec1ccbSHans Petter Selasky 	if (cq->is_kernel)
870*33ec1ccbSHans Petter Selasky 		mthca_free_cq_buf(dev, &cq->buf, cq->ibcq.cqe);
871*33ec1ccbSHans Petter Selasky 
872*33ec1ccbSHans Petter Selasky err_out_mailbox:
873*33ec1ccbSHans Petter Selasky 	mthca_free_mailbox(dev, mailbox);
874*33ec1ccbSHans Petter Selasky 
875*33ec1ccbSHans Petter Selasky err_out_arm:
876*33ec1ccbSHans Petter Selasky 	if (cq->is_kernel && mthca_is_memfree(dev))
877*33ec1ccbSHans Petter Selasky 		mthca_free_db(dev, MTHCA_DB_TYPE_CQ_ARM, cq->arm_db_index);
878*33ec1ccbSHans Petter Selasky 
879*33ec1ccbSHans Petter Selasky err_out_ci:
880*33ec1ccbSHans Petter Selasky 	if (cq->is_kernel && mthca_is_memfree(dev))
881*33ec1ccbSHans Petter Selasky 		mthca_free_db(dev, MTHCA_DB_TYPE_CQ_SET_CI, cq->set_ci_db_index);
882*33ec1ccbSHans Petter Selasky 
883*33ec1ccbSHans Petter Selasky err_out_icm:
884*33ec1ccbSHans Petter Selasky 	mthca_table_put(dev, dev->cq_table.table, cq->cqn);
885*33ec1ccbSHans Petter Selasky 
886*33ec1ccbSHans Petter Selasky err_out:
887*33ec1ccbSHans Petter Selasky 	mthca_free(&dev->cq_table.alloc, cq->cqn);
888*33ec1ccbSHans Petter Selasky 
889*33ec1ccbSHans Petter Selasky 	return err;
890*33ec1ccbSHans Petter Selasky }
891*33ec1ccbSHans Petter Selasky 
get_cq_refcount(struct mthca_dev * dev,struct mthca_cq * cq)892*33ec1ccbSHans Petter Selasky static inline int get_cq_refcount(struct mthca_dev *dev, struct mthca_cq *cq)
893*33ec1ccbSHans Petter Selasky {
894*33ec1ccbSHans Petter Selasky 	int c;
895*33ec1ccbSHans Petter Selasky 
896*33ec1ccbSHans Petter Selasky 	spin_lock_irq(&dev->cq_table.lock);
897*33ec1ccbSHans Petter Selasky 	c = cq->refcount;
898*33ec1ccbSHans Petter Selasky 	spin_unlock_irq(&dev->cq_table.lock);
899*33ec1ccbSHans Petter Selasky 
900*33ec1ccbSHans Petter Selasky 	return c;
901*33ec1ccbSHans Petter Selasky }
902*33ec1ccbSHans Petter Selasky 
mthca_free_cq(struct mthca_dev * dev,struct mthca_cq * cq)903*33ec1ccbSHans Petter Selasky void mthca_free_cq(struct mthca_dev *dev,
904*33ec1ccbSHans Petter Selasky 		   struct mthca_cq *cq)
905*33ec1ccbSHans Petter Selasky {
906*33ec1ccbSHans Petter Selasky 	struct mthca_mailbox *mailbox;
907*33ec1ccbSHans Petter Selasky 	int err;
908*33ec1ccbSHans Petter Selasky 
909*33ec1ccbSHans Petter Selasky 	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
910*33ec1ccbSHans Petter Selasky 	if (IS_ERR(mailbox)) {
911*33ec1ccbSHans Petter Selasky 		mthca_warn(dev, "No memory for mailbox to free CQ.\n");
912*33ec1ccbSHans Petter Selasky 		return;
913*33ec1ccbSHans Petter Selasky 	}
914*33ec1ccbSHans Petter Selasky 
915*33ec1ccbSHans Petter Selasky 	err = mthca_HW2SW_CQ(dev, mailbox, cq->cqn);
916*33ec1ccbSHans Petter Selasky 	if (err)
917*33ec1ccbSHans Petter Selasky 		mthca_warn(dev, "HW2SW_CQ failed (%d)\n", err);
918*33ec1ccbSHans Petter Selasky 
919*33ec1ccbSHans Petter Selasky 	if (0) {
920*33ec1ccbSHans Petter Selasky 		__be32 *ctx = mailbox->buf;
921*33ec1ccbSHans Petter Selasky 		int j;
922*33ec1ccbSHans Petter Selasky 
923*33ec1ccbSHans Petter Selasky 		printk(KERN_ERR "context for CQN %x (cons index %x, next sw %d)\n",
924*33ec1ccbSHans Petter Selasky 		       cq->cqn, cq->cons_index,
925*33ec1ccbSHans Petter Selasky 		       cq->is_kernel ? !!next_cqe_sw(cq) : 0);
926*33ec1ccbSHans Petter Selasky 		for (j = 0; j < 16; ++j)
927*33ec1ccbSHans Petter Selasky 			printk(KERN_ERR "[%2x] %08x\n", j * 4, be32_to_cpu(ctx[j]));
928*33ec1ccbSHans Petter Selasky 	}
929*33ec1ccbSHans Petter Selasky 
930*33ec1ccbSHans Petter Selasky 	spin_lock_irq(&dev->cq_table.lock);
931*33ec1ccbSHans Petter Selasky 	mthca_array_clear(&dev->cq_table.cq,
932*33ec1ccbSHans Petter Selasky 			  cq->cqn & (dev->limits.num_cqs - 1));
933*33ec1ccbSHans Petter Selasky 	--cq->refcount;
934*33ec1ccbSHans Petter Selasky 	spin_unlock_irq(&dev->cq_table.lock);
935*33ec1ccbSHans Petter Selasky 
936*33ec1ccbSHans Petter Selasky 	if (dev->mthca_flags & MTHCA_FLAG_MSI_X)
937*33ec1ccbSHans Petter Selasky 		synchronize_irq(dev->eq_table.eq[MTHCA_EQ_COMP].msi_x_vector);
938*33ec1ccbSHans Petter Selasky 	else
939*33ec1ccbSHans Petter Selasky 		synchronize_irq(dev->pdev->irq);
940*33ec1ccbSHans Petter Selasky 
941*33ec1ccbSHans Petter Selasky 	wait_event(cq->wait, !get_cq_refcount(dev, cq));
942*33ec1ccbSHans Petter Selasky 
943*33ec1ccbSHans Petter Selasky 	if (cq->is_kernel) {
944*33ec1ccbSHans Petter Selasky 		mthca_free_cq_buf(dev, &cq->buf, cq->ibcq.cqe);
945*33ec1ccbSHans Petter Selasky 		if (mthca_is_memfree(dev)) {
946*33ec1ccbSHans Petter Selasky 			mthca_free_db(dev, MTHCA_DB_TYPE_CQ_ARM,    cq->arm_db_index);
947*33ec1ccbSHans Petter Selasky 			mthca_free_db(dev, MTHCA_DB_TYPE_CQ_SET_CI, cq->set_ci_db_index);
948*33ec1ccbSHans Petter Selasky 		}
949*33ec1ccbSHans Petter Selasky 	}
950*33ec1ccbSHans Petter Selasky 
951*33ec1ccbSHans Petter Selasky 	mthca_table_put(dev, dev->cq_table.table, cq->cqn);
952*33ec1ccbSHans Petter Selasky 	mthca_free(&dev->cq_table.alloc, cq->cqn);
953*33ec1ccbSHans Petter Selasky 	mthca_free_mailbox(dev, mailbox);
954*33ec1ccbSHans Petter Selasky }
955*33ec1ccbSHans Petter Selasky 
mthca_init_cq_table(struct mthca_dev * dev)956*33ec1ccbSHans Petter Selasky int mthca_init_cq_table(struct mthca_dev *dev)
957*33ec1ccbSHans Petter Selasky {
958*33ec1ccbSHans Petter Selasky 	int err;
959*33ec1ccbSHans Petter Selasky 
960*33ec1ccbSHans Petter Selasky 	spin_lock_init(&dev->cq_table.lock);
961*33ec1ccbSHans Petter Selasky 
962*33ec1ccbSHans Petter Selasky 	err = mthca_alloc_init(&dev->cq_table.alloc,
963*33ec1ccbSHans Petter Selasky 			       dev->limits.num_cqs,
964*33ec1ccbSHans Petter Selasky 			       (1 << 24) - 1,
965*33ec1ccbSHans Petter Selasky 			       dev->limits.reserved_cqs);
966*33ec1ccbSHans Petter Selasky 	if (err)
967*33ec1ccbSHans Petter Selasky 		return err;
968*33ec1ccbSHans Petter Selasky 
969*33ec1ccbSHans Petter Selasky 	err = mthca_array_init(&dev->cq_table.cq,
970*33ec1ccbSHans Petter Selasky 			       dev->limits.num_cqs);
971*33ec1ccbSHans Petter Selasky 	if (err)
972*33ec1ccbSHans Petter Selasky 		mthca_alloc_cleanup(&dev->cq_table.alloc);
973*33ec1ccbSHans Petter Selasky 
974*33ec1ccbSHans Petter Selasky 	return err;
975*33ec1ccbSHans Petter Selasky }
976*33ec1ccbSHans Petter Selasky 
mthca_cleanup_cq_table(struct mthca_dev * dev)977*33ec1ccbSHans Petter Selasky void mthca_cleanup_cq_table(struct mthca_dev *dev)
978*33ec1ccbSHans Petter Selasky {
979*33ec1ccbSHans Petter Selasky 	mthca_array_cleanup(&dev->cq_table.cq, dev->limits.num_cqs);
980*33ec1ccbSHans Petter Selasky 	mthca_alloc_cleanup(&dev->cq_table.alloc);
981*33ec1ccbSHans Petter Selasky }
982