1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
3*4882a593Smuzhiyun * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * This software is available to you under a choice of one of two
6*4882a593Smuzhiyun * licenses. You may choose to be licensed under the terms of the GNU
7*4882a593Smuzhiyun * General Public License (GPL) Version 2, available from the file
8*4882a593Smuzhiyun * COPYING in the main directory of this source tree, or the
9*4882a593Smuzhiyun * OpenIB.org BSD license below:
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or
12*4882a593Smuzhiyun * without modification, are permitted provided that the following
13*4882a593Smuzhiyun * conditions are met:
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * - Redistributions of source code must retain the above
16*4882a593Smuzhiyun * copyright notice, this list of conditions and the following
17*4882a593Smuzhiyun * disclaimer.
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * - Redistributions in binary form must reproduce the above
20*4882a593Smuzhiyun * copyright notice, this list of conditions and the following
21*4882a593Smuzhiyun * disclaimer in the documentation and/or other materials
22*4882a593Smuzhiyun * provided with the distribution.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25*4882a593Smuzhiyun * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26*4882a593Smuzhiyun * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27*4882a593Smuzhiyun * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28*4882a593Smuzhiyun * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29*4882a593Smuzhiyun * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30*4882a593Smuzhiyun * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31*4882a593Smuzhiyun * SOFTWARE.
32*4882a593Smuzhiyun */
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun #include <linux/mlx4/cq.h>
35*4882a593Smuzhiyun #include <linux/mlx4/qp.h>
36*4882a593Smuzhiyun #include <linux/mlx4/srq.h>
37*4882a593Smuzhiyun #include <linux/slab.h>
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun #include "mlx4_ib.h"
40*4882a593Smuzhiyun #include <rdma/mlx4-abi.h>
41*4882a593Smuzhiyun #include <rdma/uverbs_ioctl.h>
42*4882a593Smuzhiyun
mlx4_ib_cq_comp(struct mlx4_cq * cq)43*4882a593Smuzhiyun static void mlx4_ib_cq_comp(struct mlx4_cq *cq)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun struct ib_cq *ibcq = &to_mibcq(cq)->ibcq;
46*4882a593Smuzhiyun ibcq->comp_handler(ibcq, ibcq->cq_context);
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun
mlx4_ib_cq_event(struct mlx4_cq * cq,enum mlx4_event type)49*4882a593Smuzhiyun static void mlx4_ib_cq_event(struct mlx4_cq *cq, enum mlx4_event type)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun struct ib_event event;
52*4882a593Smuzhiyun struct ib_cq *ibcq;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun if (type != MLX4_EVENT_TYPE_CQ_ERROR) {
55*4882a593Smuzhiyun pr_warn("Unexpected event type %d "
56*4882a593Smuzhiyun "on CQ %06x\n", type, cq->cqn);
57*4882a593Smuzhiyun return;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun ibcq = &to_mibcq(cq)->ibcq;
61*4882a593Smuzhiyun if (ibcq->event_handler) {
62*4882a593Smuzhiyun event.device = ibcq->device;
63*4882a593Smuzhiyun event.event = IB_EVENT_CQ_ERR;
64*4882a593Smuzhiyun event.element.cq = ibcq;
65*4882a593Smuzhiyun ibcq->event_handler(&event, ibcq->cq_context);
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun
get_cqe_from_buf(struct mlx4_ib_cq_buf * buf,int n)69*4882a593Smuzhiyun static void *get_cqe_from_buf(struct mlx4_ib_cq_buf *buf, int n)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun return mlx4_buf_offset(&buf->buf, n * buf->entry_size);
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
get_cqe(struct mlx4_ib_cq * cq,int n)74*4882a593Smuzhiyun static void *get_cqe(struct mlx4_ib_cq *cq, int n)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun return get_cqe_from_buf(&cq->buf, n);
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun
get_sw_cqe(struct mlx4_ib_cq * cq,int n)79*4882a593Smuzhiyun static void *get_sw_cqe(struct mlx4_ib_cq *cq, int n)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun struct mlx4_cqe *cqe = get_cqe(cq, n & cq->ibcq.cqe);
82*4882a593Smuzhiyun struct mlx4_cqe *tcqe = ((cq->buf.entry_size == 64) ? (cqe + 1) : cqe);
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun return (!!(tcqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK) ^
85*4882a593Smuzhiyun !!(n & (cq->ibcq.cqe + 1))) ? NULL : cqe;
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun
next_cqe_sw(struct mlx4_ib_cq * cq)88*4882a593Smuzhiyun static struct mlx4_cqe *next_cqe_sw(struct mlx4_ib_cq *cq)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun return get_sw_cqe(cq, cq->mcq.cons_index);
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
mlx4_ib_modify_cq(struct ib_cq * cq,u16 cq_count,u16 cq_period)93*4882a593Smuzhiyun int mlx4_ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun struct mlx4_ib_cq *mcq = to_mcq(cq);
96*4882a593Smuzhiyun struct mlx4_ib_dev *dev = to_mdev(cq->device);
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun return mlx4_cq_modify(dev->dev, &mcq->mcq, cq_count, cq_period);
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
mlx4_ib_alloc_cq_buf(struct mlx4_ib_dev * dev,struct mlx4_ib_cq_buf * buf,int nent)101*4882a593Smuzhiyun static int mlx4_ib_alloc_cq_buf(struct mlx4_ib_dev *dev, struct mlx4_ib_cq_buf *buf, int nent)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun int err;
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun err = mlx4_buf_alloc(dev->dev, nent * dev->dev->caps.cqe_size,
106*4882a593Smuzhiyun PAGE_SIZE * 2, &buf->buf);
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun if (err)
109*4882a593Smuzhiyun goto out;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun buf->entry_size = dev->dev->caps.cqe_size;
112*4882a593Smuzhiyun err = mlx4_mtt_init(dev->dev, buf->buf.npages, buf->buf.page_shift,
113*4882a593Smuzhiyun &buf->mtt);
114*4882a593Smuzhiyun if (err)
115*4882a593Smuzhiyun goto err_buf;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun err = mlx4_buf_write_mtt(dev->dev, &buf->mtt, &buf->buf);
118*4882a593Smuzhiyun if (err)
119*4882a593Smuzhiyun goto err_mtt;
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun return 0;
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun err_mtt:
124*4882a593Smuzhiyun mlx4_mtt_cleanup(dev->dev, &buf->mtt);
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun err_buf:
127*4882a593Smuzhiyun mlx4_buf_free(dev->dev, nent * buf->entry_size, &buf->buf);
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun out:
130*4882a593Smuzhiyun return err;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
mlx4_ib_free_cq_buf(struct mlx4_ib_dev * dev,struct mlx4_ib_cq_buf * buf,int cqe)133*4882a593Smuzhiyun static void mlx4_ib_free_cq_buf(struct mlx4_ib_dev *dev, struct mlx4_ib_cq_buf *buf, int cqe)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun mlx4_buf_free(dev->dev, (cqe + 1) * buf->entry_size, &buf->buf);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
mlx4_ib_get_cq_umem(struct mlx4_ib_dev * dev,struct ib_udata * udata,struct mlx4_ib_cq_buf * buf,struct ib_umem ** umem,u64 buf_addr,int cqe)138*4882a593Smuzhiyun static int mlx4_ib_get_cq_umem(struct mlx4_ib_dev *dev, struct ib_udata *udata,
139*4882a593Smuzhiyun struct mlx4_ib_cq_buf *buf,
140*4882a593Smuzhiyun struct ib_umem **umem, u64 buf_addr, int cqe)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun int err;
143*4882a593Smuzhiyun int cqe_size = dev->dev->caps.cqe_size;
144*4882a593Smuzhiyun int shift;
145*4882a593Smuzhiyun int n;
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun *umem = ib_umem_get(&dev->ib_dev, buf_addr, cqe * cqe_size,
148*4882a593Smuzhiyun IB_ACCESS_LOCAL_WRITE);
149*4882a593Smuzhiyun if (IS_ERR(*umem))
150*4882a593Smuzhiyun return PTR_ERR(*umem);
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun shift = mlx4_ib_umem_calc_optimal_mtt_size(*umem, 0, &n);
153*4882a593Smuzhiyun err = mlx4_mtt_init(dev->dev, n, shift, &buf->mtt);
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun if (err)
156*4882a593Smuzhiyun goto err_buf;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun err = mlx4_ib_umem_write_mtt(dev, &buf->mtt, *umem);
159*4882a593Smuzhiyun if (err)
160*4882a593Smuzhiyun goto err_mtt;
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun return 0;
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun err_mtt:
165*4882a593Smuzhiyun mlx4_mtt_cleanup(dev->dev, &buf->mtt);
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun err_buf:
168*4882a593Smuzhiyun ib_umem_release(*umem);
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun return err;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun #define CQ_CREATE_FLAGS_SUPPORTED IB_UVERBS_CQ_FLAGS_TIMESTAMP_COMPLETION
mlx4_ib_create_cq(struct ib_cq * ibcq,const struct ib_cq_init_attr * attr,struct ib_udata * udata)174*4882a593Smuzhiyun int mlx4_ib_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
175*4882a593Smuzhiyun struct ib_udata *udata)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun struct ib_device *ibdev = ibcq->device;
178*4882a593Smuzhiyun int entries = attr->cqe;
179*4882a593Smuzhiyun int vector = attr->comp_vector;
180*4882a593Smuzhiyun struct mlx4_ib_dev *dev = to_mdev(ibdev);
181*4882a593Smuzhiyun struct mlx4_ib_cq *cq = to_mcq(ibcq);
182*4882a593Smuzhiyun struct mlx4_uar *uar;
183*4882a593Smuzhiyun void *buf_addr;
184*4882a593Smuzhiyun int err;
185*4882a593Smuzhiyun struct mlx4_ib_ucontext *context = rdma_udata_to_drv_context(
186*4882a593Smuzhiyun udata, struct mlx4_ib_ucontext, ibucontext);
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun if (entries < 1 || entries > dev->dev->caps.max_cqes)
189*4882a593Smuzhiyun return -EINVAL;
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun if (attr->flags & ~CQ_CREATE_FLAGS_SUPPORTED)
192*4882a593Smuzhiyun return -EINVAL;
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun entries = roundup_pow_of_two(entries + 1);
195*4882a593Smuzhiyun cq->ibcq.cqe = entries - 1;
196*4882a593Smuzhiyun mutex_init(&cq->resize_mutex);
197*4882a593Smuzhiyun spin_lock_init(&cq->lock);
198*4882a593Smuzhiyun cq->resize_buf = NULL;
199*4882a593Smuzhiyun cq->resize_umem = NULL;
200*4882a593Smuzhiyun cq->create_flags = attr->flags;
201*4882a593Smuzhiyun INIT_LIST_HEAD(&cq->send_qp_list);
202*4882a593Smuzhiyun INIT_LIST_HEAD(&cq->recv_qp_list);
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun if (udata) {
205*4882a593Smuzhiyun struct mlx4_ib_create_cq ucmd;
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
208*4882a593Smuzhiyun err = -EFAULT;
209*4882a593Smuzhiyun goto err_cq;
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun buf_addr = (void *)(unsigned long)ucmd.buf_addr;
213*4882a593Smuzhiyun err = mlx4_ib_get_cq_umem(dev, udata, &cq->buf, &cq->umem,
214*4882a593Smuzhiyun ucmd.buf_addr, entries);
215*4882a593Smuzhiyun if (err)
216*4882a593Smuzhiyun goto err_cq;
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun err = mlx4_ib_db_map_user(udata, ucmd.db_addr, &cq->db);
219*4882a593Smuzhiyun if (err)
220*4882a593Smuzhiyun goto err_mtt;
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun uar = &context->uar;
223*4882a593Smuzhiyun cq->mcq.usage = MLX4_RES_USAGE_USER_VERBS;
224*4882a593Smuzhiyun } else {
225*4882a593Smuzhiyun err = mlx4_db_alloc(dev->dev, &cq->db, 1);
226*4882a593Smuzhiyun if (err)
227*4882a593Smuzhiyun goto err_cq;
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun cq->mcq.set_ci_db = cq->db.db;
230*4882a593Smuzhiyun cq->mcq.arm_db = cq->db.db + 1;
231*4882a593Smuzhiyun *cq->mcq.set_ci_db = 0;
232*4882a593Smuzhiyun *cq->mcq.arm_db = 0;
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun err = mlx4_ib_alloc_cq_buf(dev, &cq->buf, entries);
235*4882a593Smuzhiyun if (err)
236*4882a593Smuzhiyun goto err_db;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun buf_addr = &cq->buf.buf;
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun uar = &dev->priv_uar;
241*4882a593Smuzhiyun cq->mcq.usage = MLX4_RES_USAGE_DRIVER;
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun if (dev->eq_table)
245*4882a593Smuzhiyun vector = dev->eq_table[vector % ibdev->num_comp_vectors];
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun err = mlx4_cq_alloc(dev->dev, entries, &cq->buf.mtt, uar, cq->db.dma,
248*4882a593Smuzhiyun &cq->mcq, vector, 0,
249*4882a593Smuzhiyun !!(cq->create_flags &
250*4882a593Smuzhiyun IB_UVERBS_CQ_FLAGS_TIMESTAMP_COMPLETION),
251*4882a593Smuzhiyun buf_addr, !!udata);
252*4882a593Smuzhiyun if (err)
253*4882a593Smuzhiyun goto err_dbmap;
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun if (udata)
256*4882a593Smuzhiyun cq->mcq.tasklet_ctx.comp = mlx4_ib_cq_comp;
257*4882a593Smuzhiyun else
258*4882a593Smuzhiyun cq->mcq.comp = mlx4_ib_cq_comp;
259*4882a593Smuzhiyun cq->mcq.event = mlx4_ib_cq_event;
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun if (udata)
262*4882a593Smuzhiyun if (ib_copy_to_udata(udata, &cq->mcq.cqn, sizeof (__u32))) {
263*4882a593Smuzhiyun err = -EFAULT;
264*4882a593Smuzhiyun goto err_cq_free;
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun return 0;
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun err_cq_free:
270*4882a593Smuzhiyun mlx4_cq_free(dev->dev, &cq->mcq);
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun err_dbmap:
273*4882a593Smuzhiyun if (udata)
274*4882a593Smuzhiyun mlx4_ib_db_unmap_user(context, &cq->db);
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun err_mtt:
277*4882a593Smuzhiyun mlx4_mtt_cleanup(dev->dev, &cq->buf.mtt);
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun ib_umem_release(cq->umem);
280*4882a593Smuzhiyun if (!udata)
281*4882a593Smuzhiyun mlx4_ib_free_cq_buf(dev, &cq->buf, cq->ibcq.cqe);
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun err_db:
284*4882a593Smuzhiyun if (!udata)
285*4882a593Smuzhiyun mlx4_db_free(dev->dev, &cq->db);
286*4882a593Smuzhiyun err_cq:
287*4882a593Smuzhiyun return err;
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun
mlx4_alloc_resize_buf(struct mlx4_ib_dev * dev,struct mlx4_ib_cq * cq,int entries)290*4882a593Smuzhiyun static int mlx4_alloc_resize_buf(struct mlx4_ib_dev *dev, struct mlx4_ib_cq *cq,
291*4882a593Smuzhiyun int entries)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun int err;
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun if (cq->resize_buf)
296*4882a593Smuzhiyun return -EBUSY;
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun cq->resize_buf = kmalloc(sizeof *cq->resize_buf, GFP_KERNEL);
299*4882a593Smuzhiyun if (!cq->resize_buf)
300*4882a593Smuzhiyun return -ENOMEM;
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun err = mlx4_ib_alloc_cq_buf(dev, &cq->resize_buf->buf, entries);
303*4882a593Smuzhiyun if (err) {
304*4882a593Smuzhiyun kfree(cq->resize_buf);
305*4882a593Smuzhiyun cq->resize_buf = NULL;
306*4882a593Smuzhiyun return err;
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun cq->resize_buf->cqe = entries - 1;
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun return 0;
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun
mlx4_alloc_resize_umem(struct mlx4_ib_dev * dev,struct mlx4_ib_cq * cq,int entries,struct ib_udata * udata)314*4882a593Smuzhiyun static int mlx4_alloc_resize_umem(struct mlx4_ib_dev *dev, struct mlx4_ib_cq *cq,
315*4882a593Smuzhiyun int entries, struct ib_udata *udata)
316*4882a593Smuzhiyun {
317*4882a593Smuzhiyun struct mlx4_ib_resize_cq ucmd;
318*4882a593Smuzhiyun int err;
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun if (cq->resize_umem)
321*4882a593Smuzhiyun return -EBUSY;
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd))
324*4882a593Smuzhiyun return -EFAULT;
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun cq->resize_buf = kmalloc(sizeof *cq->resize_buf, GFP_KERNEL);
327*4882a593Smuzhiyun if (!cq->resize_buf)
328*4882a593Smuzhiyun return -ENOMEM;
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun err = mlx4_ib_get_cq_umem(dev, udata, &cq->resize_buf->buf,
331*4882a593Smuzhiyun &cq->resize_umem, ucmd.buf_addr, entries);
332*4882a593Smuzhiyun if (err) {
333*4882a593Smuzhiyun kfree(cq->resize_buf);
334*4882a593Smuzhiyun cq->resize_buf = NULL;
335*4882a593Smuzhiyun return err;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun cq->resize_buf->cqe = entries - 1;
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun return 0;
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun
mlx4_ib_get_outstanding_cqes(struct mlx4_ib_cq * cq)343*4882a593Smuzhiyun static int mlx4_ib_get_outstanding_cqes(struct mlx4_ib_cq *cq)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun u32 i;
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun i = cq->mcq.cons_index;
348*4882a593Smuzhiyun while (get_sw_cqe(cq, i))
349*4882a593Smuzhiyun ++i;
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun return i - cq->mcq.cons_index;
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun
mlx4_ib_cq_resize_copy_cqes(struct mlx4_ib_cq * cq)354*4882a593Smuzhiyun static void mlx4_ib_cq_resize_copy_cqes(struct mlx4_ib_cq *cq)
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun struct mlx4_cqe *cqe, *new_cqe;
357*4882a593Smuzhiyun int i;
358*4882a593Smuzhiyun int cqe_size = cq->buf.entry_size;
359*4882a593Smuzhiyun int cqe_inc = cqe_size == 64 ? 1 : 0;
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun i = cq->mcq.cons_index;
362*4882a593Smuzhiyun cqe = get_cqe(cq, i & cq->ibcq.cqe);
363*4882a593Smuzhiyun cqe += cqe_inc;
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun while ((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) != MLX4_CQE_OPCODE_RESIZE) {
366*4882a593Smuzhiyun new_cqe = get_cqe_from_buf(&cq->resize_buf->buf,
367*4882a593Smuzhiyun (i + 1) & cq->resize_buf->cqe);
368*4882a593Smuzhiyun memcpy(new_cqe, get_cqe(cq, i & cq->ibcq.cqe), cqe_size);
369*4882a593Smuzhiyun new_cqe += cqe_inc;
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun new_cqe->owner_sr_opcode = (cqe->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK) |
372*4882a593Smuzhiyun (((i + 1) & (cq->resize_buf->cqe + 1)) ? MLX4_CQE_OWNER_MASK : 0);
373*4882a593Smuzhiyun cqe = get_cqe(cq, ++i & cq->ibcq.cqe);
374*4882a593Smuzhiyun cqe += cqe_inc;
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun ++cq->mcq.cons_index;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun
mlx4_ib_resize_cq(struct ib_cq * ibcq,int entries,struct ib_udata * udata)379*4882a593Smuzhiyun int mlx4_ib_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
380*4882a593Smuzhiyun {
381*4882a593Smuzhiyun struct mlx4_ib_dev *dev = to_mdev(ibcq->device);
382*4882a593Smuzhiyun struct mlx4_ib_cq *cq = to_mcq(ibcq);
383*4882a593Smuzhiyun struct mlx4_mtt mtt;
384*4882a593Smuzhiyun int outst_cqe;
385*4882a593Smuzhiyun int err;
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun mutex_lock(&cq->resize_mutex);
388*4882a593Smuzhiyun if (entries < 1 || entries > dev->dev->caps.max_cqes) {
389*4882a593Smuzhiyun err = -EINVAL;
390*4882a593Smuzhiyun goto out;
391*4882a593Smuzhiyun }
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun entries = roundup_pow_of_two(entries + 1);
394*4882a593Smuzhiyun if (entries == ibcq->cqe + 1) {
395*4882a593Smuzhiyun err = 0;
396*4882a593Smuzhiyun goto out;
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun if (entries > dev->dev->caps.max_cqes + 1) {
400*4882a593Smuzhiyun err = -EINVAL;
401*4882a593Smuzhiyun goto out;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun if (ibcq->uobject) {
405*4882a593Smuzhiyun err = mlx4_alloc_resize_umem(dev, cq, entries, udata);
406*4882a593Smuzhiyun if (err)
407*4882a593Smuzhiyun goto out;
408*4882a593Smuzhiyun } else {
409*4882a593Smuzhiyun /* Can't be smaller than the number of outstanding CQEs */
410*4882a593Smuzhiyun outst_cqe = mlx4_ib_get_outstanding_cqes(cq);
411*4882a593Smuzhiyun if (entries < outst_cqe + 1) {
412*4882a593Smuzhiyun err = -EINVAL;
413*4882a593Smuzhiyun goto out;
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun err = mlx4_alloc_resize_buf(dev, cq, entries);
417*4882a593Smuzhiyun if (err)
418*4882a593Smuzhiyun goto out;
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun mtt = cq->buf.mtt;
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun err = mlx4_cq_resize(dev->dev, &cq->mcq, entries, &cq->resize_buf->buf.mtt);
424*4882a593Smuzhiyun if (err)
425*4882a593Smuzhiyun goto err_buf;
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun mlx4_mtt_cleanup(dev->dev, &mtt);
428*4882a593Smuzhiyun if (ibcq->uobject) {
429*4882a593Smuzhiyun cq->buf = cq->resize_buf->buf;
430*4882a593Smuzhiyun cq->ibcq.cqe = cq->resize_buf->cqe;
431*4882a593Smuzhiyun ib_umem_release(cq->umem);
432*4882a593Smuzhiyun cq->umem = cq->resize_umem;
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun kfree(cq->resize_buf);
435*4882a593Smuzhiyun cq->resize_buf = NULL;
436*4882a593Smuzhiyun cq->resize_umem = NULL;
437*4882a593Smuzhiyun } else {
438*4882a593Smuzhiyun struct mlx4_ib_cq_buf tmp_buf;
439*4882a593Smuzhiyun int tmp_cqe = 0;
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun spin_lock_irq(&cq->lock);
442*4882a593Smuzhiyun if (cq->resize_buf) {
443*4882a593Smuzhiyun mlx4_ib_cq_resize_copy_cqes(cq);
444*4882a593Smuzhiyun tmp_buf = cq->buf;
445*4882a593Smuzhiyun tmp_cqe = cq->ibcq.cqe;
446*4882a593Smuzhiyun cq->buf = cq->resize_buf->buf;
447*4882a593Smuzhiyun cq->ibcq.cqe = cq->resize_buf->cqe;
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun kfree(cq->resize_buf);
450*4882a593Smuzhiyun cq->resize_buf = NULL;
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun spin_unlock_irq(&cq->lock);
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun if (tmp_cqe)
455*4882a593Smuzhiyun mlx4_ib_free_cq_buf(dev, &tmp_buf, tmp_cqe);
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun goto out;
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun err_buf:
461*4882a593Smuzhiyun mlx4_mtt_cleanup(dev->dev, &cq->resize_buf->buf.mtt);
462*4882a593Smuzhiyun if (!ibcq->uobject)
463*4882a593Smuzhiyun mlx4_ib_free_cq_buf(dev, &cq->resize_buf->buf,
464*4882a593Smuzhiyun cq->resize_buf->cqe);
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun kfree(cq->resize_buf);
467*4882a593Smuzhiyun cq->resize_buf = NULL;
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun ib_umem_release(cq->resize_umem);
470*4882a593Smuzhiyun cq->resize_umem = NULL;
471*4882a593Smuzhiyun out:
472*4882a593Smuzhiyun mutex_unlock(&cq->resize_mutex);
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun return err;
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun
mlx4_ib_destroy_cq(struct ib_cq * cq,struct ib_udata * udata)477*4882a593Smuzhiyun int mlx4_ib_destroy_cq(struct ib_cq *cq, struct ib_udata *udata)
478*4882a593Smuzhiyun {
479*4882a593Smuzhiyun struct mlx4_ib_dev *dev = to_mdev(cq->device);
480*4882a593Smuzhiyun struct mlx4_ib_cq *mcq = to_mcq(cq);
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun mlx4_cq_free(dev->dev, &mcq->mcq);
483*4882a593Smuzhiyun mlx4_mtt_cleanup(dev->dev, &mcq->buf.mtt);
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun if (udata) {
486*4882a593Smuzhiyun mlx4_ib_db_unmap_user(
487*4882a593Smuzhiyun rdma_udata_to_drv_context(
488*4882a593Smuzhiyun udata,
489*4882a593Smuzhiyun struct mlx4_ib_ucontext,
490*4882a593Smuzhiyun ibucontext),
491*4882a593Smuzhiyun &mcq->db);
492*4882a593Smuzhiyun } else {
493*4882a593Smuzhiyun mlx4_ib_free_cq_buf(dev, &mcq->buf, cq->cqe);
494*4882a593Smuzhiyun mlx4_db_free(dev->dev, &mcq->db);
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun ib_umem_release(mcq->umem);
497*4882a593Smuzhiyun return 0;
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun
dump_cqe(void * cqe)500*4882a593Smuzhiyun static void dump_cqe(void *cqe)
501*4882a593Smuzhiyun {
502*4882a593Smuzhiyun __be32 *buf = cqe;
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun pr_debug("CQE contents %08x %08x %08x %08x %08x %08x %08x %08x\n",
505*4882a593Smuzhiyun be32_to_cpu(buf[0]), be32_to_cpu(buf[1]), be32_to_cpu(buf[2]),
506*4882a593Smuzhiyun be32_to_cpu(buf[3]), be32_to_cpu(buf[4]), be32_to_cpu(buf[5]),
507*4882a593Smuzhiyun be32_to_cpu(buf[6]), be32_to_cpu(buf[7]));
508*4882a593Smuzhiyun }
509*4882a593Smuzhiyun
mlx4_ib_handle_error_cqe(struct mlx4_err_cqe * cqe,struct ib_wc * wc)510*4882a593Smuzhiyun static void mlx4_ib_handle_error_cqe(struct mlx4_err_cqe *cqe,
511*4882a593Smuzhiyun struct ib_wc *wc)
512*4882a593Smuzhiyun {
513*4882a593Smuzhiyun if (cqe->syndrome == MLX4_CQE_SYNDROME_LOCAL_QP_OP_ERR) {
514*4882a593Smuzhiyun pr_debug("local QP operation err "
515*4882a593Smuzhiyun "(QPN %06x, WQE index %x, vendor syndrome %02x, "
516*4882a593Smuzhiyun "opcode = %02x)\n",
517*4882a593Smuzhiyun be32_to_cpu(cqe->my_qpn), be16_to_cpu(cqe->wqe_index),
518*4882a593Smuzhiyun cqe->vendor_err_syndrome,
519*4882a593Smuzhiyun cqe->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK);
520*4882a593Smuzhiyun dump_cqe(cqe);
521*4882a593Smuzhiyun }
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun switch (cqe->syndrome) {
524*4882a593Smuzhiyun case MLX4_CQE_SYNDROME_LOCAL_LENGTH_ERR:
525*4882a593Smuzhiyun wc->status = IB_WC_LOC_LEN_ERR;
526*4882a593Smuzhiyun break;
527*4882a593Smuzhiyun case MLX4_CQE_SYNDROME_LOCAL_QP_OP_ERR:
528*4882a593Smuzhiyun wc->status = IB_WC_LOC_QP_OP_ERR;
529*4882a593Smuzhiyun break;
530*4882a593Smuzhiyun case MLX4_CQE_SYNDROME_LOCAL_PROT_ERR:
531*4882a593Smuzhiyun wc->status = IB_WC_LOC_PROT_ERR;
532*4882a593Smuzhiyun break;
533*4882a593Smuzhiyun case MLX4_CQE_SYNDROME_WR_FLUSH_ERR:
534*4882a593Smuzhiyun wc->status = IB_WC_WR_FLUSH_ERR;
535*4882a593Smuzhiyun break;
536*4882a593Smuzhiyun case MLX4_CQE_SYNDROME_MW_BIND_ERR:
537*4882a593Smuzhiyun wc->status = IB_WC_MW_BIND_ERR;
538*4882a593Smuzhiyun break;
539*4882a593Smuzhiyun case MLX4_CQE_SYNDROME_BAD_RESP_ERR:
540*4882a593Smuzhiyun wc->status = IB_WC_BAD_RESP_ERR;
541*4882a593Smuzhiyun break;
542*4882a593Smuzhiyun case MLX4_CQE_SYNDROME_LOCAL_ACCESS_ERR:
543*4882a593Smuzhiyun wc->status = IB_WC_LOC_ACCESS_ERR;
544*4882a593Smuzhiyun break;
545*4882a593Smuzhiyun case MLX4_CQE_SYNDROME_REMOTE_INVAL_REQ_ERR:
546*4882a593Smuzhiyun wc->status = IB_WC_REM_INV_REQ_ERR;
547*4882a593Smuzhiyun break;
548*4882a593Smuzhiyun case MLX4_CQE_SYNDROME_REMOTE_ACCESS_ERR:
549*4882a593Smuzhiyun wc->status = IB_WC_REM_ACCESS_ERR;
550*4882a593Smuzhiyun break;
551*4882a593Smuzhiyun case MLX4_CQE_SYNDROME_REMOTE_OP_ERR:
552*4882a593Smuzhiyun wc->status = IB_WC_REM_OP_ERR;
553*4882a593Smuzhiyun break;
554*4882a593Smuzhiyun case MLX4_CQE_SYNDROME_TRANSPORT_RETRY_EXC_ERR:
555*4882a593Smuzhiyun wc->status = IB_WC_RETRY_EXC_ERR;
556*4882a593Smuzhiyun break;
557*4882a593Smuzhiyun case MLX4_CQE_SYNDROME_RNR_RETRY_EXC_ERR:
558*4882a593Smuzhiyun wc->status = IB_WC_RNR_RETRY_EXC_ERR;
559*4882a593Smuzhiyun break;
560*4882a593Smuzhiyun case MLX4_CQE_SYNDROME_REMOTE_ABORTED_ERR:
561*4882a593Smuzhiyun wc->status = IB_WC_REM_ABORT_ERR;
562*4882a593Smuzhiyun break;
563*4882a593Smuzhiyun default:
564*4882a593Smuzhiyun wc->status = IB_WC_GENERAL_ERR;
565*4882a593Smuzhiyun break;
566*4882a593Smuzhiyun }
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun wc->vendor_err = cqe->vendor_err_syndrome;
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun
mlx4_ib_ipoib_csum_ok(__be16 status,u8 badfcs_enc,__be16 checksum)571*4882a593Smuzhiyun static int mlx4_ib_ipoib_csum_ok(__be16 status, u8 badfcs_enc, __be16 checksum)
572*4882a593Smuzhiyun {
573*4882a593Smuzhiyun return ((badfcs_enc & MLX4_CQE_STATUS_L4_CSUM) ||
574*4882a593Smuzhiyun ((status & cpu_to_be16(MLX4_CQE_STATUS_IPOK)) &&
575*4882a593Smuzhiyun (status & cpu_to_be16(MLX4_CQE_STATUS_TCP |
576*4882a593Smuzhiyun MLX4_CQE_STATUS_UDP)) &&
577*4882a593Smuzhiyun (checksum == cpu_to_be16(0xffff))));
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun
use_tunnel_data(struct mlx4_ib_qp * qp,struct mlx4_ib_cq * cq,struct ib_wc * wc,unsigned tail,struct mlx4_cqe * cqe,int is_eth)580*4882a593Smuzhiyun static void use_tunnel_data(struct mlx4_ib_qp *qp, struct mlx4_ib_cq *cq, struct ib_wc *wc,
581*4882a593Smuzhiyun unsigned tail, struct mlx4_cqe *cqe, int is_eth)
582*4882a593Smuzhiyun {
583*4882a593Smuzhiyun struct mlx4_ib_proxy_sqp_hdr *hdr;
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun ib_dma_sync_single_for_cpu(qp->ibqp.device,
586*4882a593Smuzhiyun qp->sqp_proxy_rcv[tail].map,
587*4882a593Smuzhiyun sizeof (struct mlx4_ib_proxy_sqp_hdr),
588*4882a593Smuzhiyun DMA_FROM_DEVICE);
589*4882a593Smuzhiyun hdr = (struct mlx4_ib_proxy_sqp_hdr *) (qp->sqp_proxy_rcv[tail].addr);
590*4882a593Smuzhiyun wc->pkey_index = be16_to_cpu(hdr->tun.pkey_index);
591*4882a593Smuzhiyun wc->src_qp = be32_to_cpu(hdr->tun.flags_src_qp) & 0xFFFFFF;
592*4882a593Smuzhiyun wc->wc_flags |= (hdr->tun.g_ml_path & 0x80) ? (IB_WC_GRH) : 0;
593*4882a593Smuzhiyun wc->dlid_path_bits = 0;
594*4882a593Smuzhiyun
595*4882a593Smuzhiyun if (is_eth) {
596*4882a593Smuzhiyun wc->slid = 0;
597*4882a593Smuzhiyun wc->vlan_id = be16_to_cpu(hdr->tun.sl_vid);
598*4882a593Smuzhiyun memcpy(&(wc->smac[0]), (char *)&hdr->tun.mac_31_0, 4);
599*4882a593Smuzhiyun memcpy(&(wc->smac[4]), (char *)&hdr->tun.slid_mac_47_32, 2);
600*4882a593Smuzhiyun wc->wc_flags |= (IB_WC_WITH_VLAN | IB_WC_WITH_SMAC);
601*4882a593Smuzhiyun } else {
602*4882a593Smuzhiyun wc->slid = be16_to_cpu(hdr->tun.slid_mac_47_32);
603*4882a593Smuzhiyun wc->sl = (u8) (be16_to_cpu(hdr->tun.sl_vid) >> 12);
604*4882a593Smuzhiyun }
605*4882a593Smuzhiyun }
606*4882a593Smuzhiyun
mlx4_ib_qp_sw_comp(struct mlx4_ib_qp * qp,int num_entries,struct ib_wc * wc,int * npolled,int is_send)607*4882a593Smuzhiyun static void mlx4_ib_qp_sw_comp(struct mlx4_ib_qp *qp, int num_entries,
608*4882a593Smuzhiyun struct ib_wc *wc, int *npolled, int is_send)
609*4882a593Smuzhiyun {
610*4882a593Smuzhiyun struct mlx4_ib_wq *wq;
611*4882a593Smuzhiyun unsigned cur;
612*4882a593Smuzhiyun int i;
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun wq = is_send ? &qp->sq : &qp->rq;
615*4882a593Smuzhiyun cur = wq->head - wq->tail;
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun if (cur == 0)
618*4882a593Smuzhiyun return;
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun for (i = 0; i < cur && *npolled < num_entries; i++) {
621*4882a593Smuzhiyun wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
622*4882a593Smuzhiyun wc->status = IB_WC_WR_FLUSH_ERR;
623*4882a593Smuzhiyun wc->vendor_err = MLX4_CQE_SYNDROME_WR_FLUSH_ERR;
624*4882a593Smuzhiyun wq->tail++;
625*4882a593Smuzhiyun (*npolled)++;
626*4882a593Smuzhiyun wc->qp = &qp->ibqp;
627*4882a593Smuzhiyun wc++;
628*4882a593Smuzhiyun }
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun
mlx4_ib_poll_sw_comp(struct mlx4_ib_cq * cq,int num_entries,struct ib_wc * wc,int * npolled)631*4882a593Smuzhiyun static void mlx4_ib_poll_sw_comp(struct mlx4_ib_cq *cq, int num_entries,
632*4882a593Smuzhiyun struct ib_wc *wc, int *npolled)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun struct mlx4_ib_qp *qp;
635*4882a593Smuzhiyun
636*4882a593Smuzhiyun *npolled = 0;
637*4882a593Smuzhiyun /* Find uncompleted WQEs belonging to that cq and return
638*4882a593Smuzhiyun * simulated FLUSH_ERR completions
639*4882a593Smuzhiyun */
640*4882a593Smuzhiyun list_for_each_entry(qp, &cq->send_qp_list, cq_send_list) {
641*4882a593Smuzhiyun mlx4_ib_qp_sw_comp(qp, num_entries, wc + *npolled, npolled, 1);
642*4882a593Smuzhiyun if (*npolled >= num_entries)
643*4882a593Smuzhiyun goto out;
644*4882a593Smuzhiyun }
645*4882a593Smuzhiyun
646*4882a593Smuzhiyun list_for_each_entry(qp, &cq->recv_qp_list, cq_recv_list) {
647*4882a593Smuzhiyun mlx4_ib_qp_sw_comp(qp, num_entries, wc + *npolled, npolled, 0);
648*4882a593Smuzhiyun if (*npolled >= num_entries)
649*4882a593Smuzhiyun goto out;
650*4882a593Smuzhiyun }
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun out:
653*4882a593Smuzhiyun return;
654*4882a593Smuzhiyun }
655*4882a593Smuzhiyun
mlx4_ib_poll_one(struct mlx4_ib_cq * cq,struct mlx4_ib_qp ** cur_qp,struct ib_wc * wc)656*4882a593Smuzhiyun static int mlx4_ib_poll_one(struct mlx4_ib_cq *cq,
657*4882a593Smuzhiyun struct mlx4_ib_qp **cur_qp,
658*4882a593Smuzhiyun struct ib_wc *wc)
659*4882a593Smuzhiyun {
660*4882a593Smuzhiyun struct mlx4_cqe *cqe;
661*4882a593Smuzhiyun struct mlx4_qp *mqp;
662*4882a593Smuzhiyun struct mlx4_ib_wq *wq;
663*4882a593Smuzhiyun struct mlx4_ib_srq *srq;
664*4882a593Smuzhiyun struct mlx4_srq *msrq = NULL;
665*4882a593Smuzhiyun int is_send;
666*4882a593Smuzhiyun int is_error;
667*4882a593Smuzhiyun int is_eth;
668*4882a593Smuzhiyun u32 g_mlpath_rqpn;
669*4882a593Smuzhiyun u16 wqe_ctr;
670*4882a593Smuzhiyun unsigned tail = 0;
671*4882a593Smuzhiyun
672*4882a593Smuzhiyun repoll:
673*4882a593Smuzhiyun cqe = next_cqe_sw(cq);
674*4882a593Smuzhiyun if (!cqe)
675*4882a593Smuzhiyun return -EAGAIN;
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun if (cq->buf.entry_size == 64)
678*4882a593Smuzhiyun cqe++;
679*4882a593Smuzhiyun
680*4882a593Smuzhiyun ++cq->mcq.cons_index;
681*4882a593Smuzhiyun
682*4882a593Smuzhiyun /*
683*4882a593Smuzhiyun * Make sure we read CQ entry contents after we've checked the
684*4882a593Smuzhiyun * ownership bit.
685*4882a593Smuzhiyun */
686*4882a593Smuzhiyun rmb();
687*4882a593Smuzhiyun
688*4882a593Smuzhiyun is_send = cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK;
689*4882a593Smuzhiyun is_error = (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
690*4882a593Smuzhiyun MLX4_CQE_OPCODE_ERROR;
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun /* Resize CQ in progress */
693*4882a593Smuzhiyun if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) == MLX4_CQE_OPCODE_RESIZE)) {
694*4882a593Smuzhiyun if (cq->resize_buf) {
695*4882a593Smuzhiyun struct mlx4_ib_dev *dev = to_mdev(cq->ibcq.device);
696*4882a593Smuzhiyun
697*4882a593Smuzhiyun mlx4_ib_free_cq_buf(dev, &cq->buf, cq->ibcq.cqe);
698*4882a593Smuzhiyun cq->buf = cq->resize_buf->buf;
699*4882a593Smuzhiyun cq->ibcq.cqe = cq->resize_buf->cqe;
700*4882a593Smuzhiyun
701*4882a593Smuzhiyun kfree(cq->resize_buf);
702*4882a593Smuzhiyun cq->resize_buf = NULL;
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun
705*4882a593Smuzhiyun goto repoll;
706*4882a593Smuzhiyun }
707*4882a593Smuzhiyun
708*4882a593Smuzhiyun if (!*cur_qp ||
709*4882a593Smuzhiyun (be32_to_cpu(cqe->vlan_my_qpn) & MLX4_CQE_QPN_MASK) != (*cur_qp)->mqp.qpn) {
710*4882a593Smuzhiyun /*
711*4882a593Smuzhiyun * We do not have to take the QP table lock here,
712*4882a593Smuzhiyun * because CQs will be locked while QPs are removed
713*4882a593Smuzhiyun * from the table.
714*4882a593Smuzhiyun */
715*4882a593Smuzhiyun mqp = __mlx4_qp_lookup(to_mdev(cq->ibcq.device)->dev,
716*4882a593Smuzhiyun be32_to_cpu(cqe->vlan_my_qpn));
717*4882a593Smuzhiyun *cur_qp = to_mibqp(mqp);
718*4882a593Smuzhiyun }
719*4882a593Smuzhiyun
720*4882a593Smuzhiyun wc->qp = &(*cur_qp)->ibqp;
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun if (wc->qp->qp_type == IB_QPT_XRC_TGT) {
723*4882a593Smuzhiyun u32 srq_num;
724*4882a593Smuzhiyun g_mlpath_rqpn = be32_to_cpu(cqe->g_mlpath_rqpn);
725*4882a593Smuzhiyun srq_num = g_mlpath_rqpn & 0xffffff;
726*4882a593Smuzhiyun /* SRQ is also in the radix tree */
727*4882a593Smuzhiyun msrq = mlx4_srq_lookup(to_mdev(cq->ibcq.device)->dev,
728*4882a593Smuzhiyun srq_num);
729*4882a593Smuzhiyun }
730*4882a593Smuzhiyun
731*4882a593Smuzhiyun if (is_send) {
732*4882a593Smuzhiyun wq = &(*cur_qp)->sq;
733*4882a593Smuzhiyun if (!(*cur_qp)->sq_signal_bits) {
734*4882a593Smuzhiyun wqe_ctr = be16_to_cpu(cqe->wqe_index);
735*4882a593Smuzhiyun wq->tail += (u16) (wqe_ctr - (u16) wq->tail);
736*4882a593Smuzhiyun }
737*4882a593Smuzhiyun wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
738*4882a593Smuzhiyun ++wq->tail;
739*4882a593Smuzhiyun } else if ((*cur_qp)->ibqp.srq) {
740*4882a593Smuzhiyun srq = to_msrq((*cur_qp)->ibqp.srq);
741*4882a593Smuzhiyun wqe_ctr = be16_to_cpu(cqe->wqe_index);
742*4882a593Smuzhiyun wc->wr_id = srq->wrid[wqe_ctr];
743*4882a593Smuzhiyun mlx4_ib_free_srq_wqe(srq, wqe_ctr);
744*4882a593Smuzhiyun } else if (msrq) {
745*4882a593Smuzhiyun srq = to_mibsrq(msrq);
746*4882a593Smuzhiyun wqe_ctr = be16_to_cpu(cqe->wqe_index);
747*4882a593Smuzhiyun wc->wr_id = srq->wrid[wqe_ctr];
748*4882a593Smuzhiyun mlx4_ib_free_srq_wqe(srq, wqe_ctr);
749*4882a593Smuzhiyun } else {
750*4882a593Smuzhiyun wq = &(*cur_qp)->rq;
751*4882a593Smuzhiyun tail = wq->tail & (wq->wqe_cnt - 1);
752*4882a593Smuzhiyun wc->wr_id = wq->wrid[tail];
753*4882a593Smuzhiyun ++wq->tail;
754*4882a593Smuzhiyun }
755*4882a593Smuzhiyun
756*4882a593Smuzhiyun if (unlikely(is_error)) {
757*4882a593Smuzhiyun mlx4_ib_handle_error_cqe((struct mlx4_err_cqe *) cqe, wc);
758*4882a593Smuzhiyun return 0;
759*4882a593Smuzhiyun }
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun wc->status = IB_WC_SUCCESS;
762*4882a593Smuzhiyun
763*4882a593Smuzhiyun if (is_send) {
764*4882a593Smuzhiyun wc->wc_flags = 0;
765*4882a593Smuzhiyun switch (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) {
766*4882a593Smuzhiyun case MLX4_OPCODE_RDMA_WRITE_IMM:
767*4882a593Smuzhiyun wc->wc_flags |= IB_WC_WITH_IMM;
768*4882a593Smuzhiyun fallthrough;
769*4882a593Smuzhiyun case MLX4_OPCODE_RDMA_WRITE:
770*4882a593Smuzhiyun wc->opcode = IB_WC_RDMA_WRITE;
771*4882a593Smuzhiyun break;
772*4882a593Smuzhiyun case MLX4_OPCODE_SEND_IMM:
773*4882a593Smuzhiyun wc->wc_flags |= IB_WC_WITH_IMM;
774*4882a593Smuzhiyun fallthrough;
775*4882a593Smuzhiyun case MLX4_OPCODE_SEND:
776*4882a593Smuzhiyun case MLX4_OPCODE_SEND_INVAL:
777*4882a593Smuzhiyun wc->opcode = IB_WC_SEND;
778*4882a593Smuzhiyun break;
779*4882a593Smuzhiyun case MLX4_OPCODE_RDMA_READ:
780*4882a593Smuzhiyun wc->opcode = IB_WC_RDMA_READ;
781*4882a593Smuzhiyun wc->byte_len = be32_to_cpu(cqe->byte_cnt);
782*4882a593Smuzhiyun break;
783*4882a593Smuzhiyun case MLX4_OPCODE_ATOMIC_CS:
784*4882a593Smuzhiyun wc->opcode = IB_WC_COMP_SWAP;
785*4882a593Smuzhiyun wc->byte_len = 8;
786*4882a593Smuzhiyun break;
787*4882a593Smuzhiyun case MLX4_OPCODE_ATOMIC_FA:
788*4882a593Smuzhiyun wc->opcode = IB_WC_FETCH_ADD;
789*4882a593Smuzhiyun wc->byte_len = 8;
790*4882a593Smuzhiyun break;
791*4882a593Smuzhiyun case MLX4_OPCODE_MASKED_ATOMIC_CS:
792*4882a593Smuzhiyun wc->opcode = IB_WC_MASKED_COMP_SWAP;
793*4882a593Smuzhiyun wc->byte_len = 8;
794*4882a593Smuzhiyun break;
795*4882a593Smuzhiyun case MLX4_OPCODE_MASKED_ATOMIC_FA:
796*4882a593Smuzhiyun wc->opcode = IB_WC_MASKED_FETCH_ADD;
797*4882a593Smuzhiyun wc->byte_len = 8;
798*4882a593Smuzhiyun break;
799*4882a593Smuzhiyun case MLX4_OPCODE_LSO:
800*4882a593Smuzhiyun wc->opcode = IB_WC_LSO;
801*4882a593Smuzhiyun break;
802*4882a593Smuzhiyun case MLX4_OPCODE_FMR:
803*4882a593Smuzhiyun wc->opcode = IB_WC_REG_MR;
804*4882a593Smuzhiyun break;
805*4882a593Smuzhiyun case MLX4_OPCODE_LOCAL_INVAL:
806*4882a593Smuzhiyun wc->opcode = IB_WC_LOCAL_INV;
807*4882a593Smuzhiyun break;
808*4882a593Smuzhiyun }
809*4882a593Smuzhiyun } else {
810*4882a593Smuzhiyun wc->byte_len = be32_to_cpu(cqe->byte_cnt);
811*4882a593Smuzhiyun
812*4882a593Smuzhiyun switch (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) {
813*4882a593Smuzhiyun case MLX4_RECV_OPCODE_RDMA_WRITE_IMM:
814*4882a593Smuzhiyun wc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
815*4882a593Smuzhiyun wc->wc_flags = IB_WC_WITH_IMM;
816*4882a593Smuzhiyun wc->ex.imm_data = cqe->immed_rss_invalid;
817*4882a593Smuzhiyun break;
818*4882a593Smuzhiyun case MLX4_RECV_OPCODE_SEND_INVAL:
819*4882a593Smuzhiyun wc->opcode = IB_WC_RECV;
820*4882a593Smuzhiyun wc->wc_flags = IB_WC_WITH_INVALIDATE;
821*4882a593Smuzhiyun wc->ex.invalidate_rkey = be32_to_cpu(cqe->immed_rss_invalid);
822*4882a593Smuzhiyun break;
823*4882a593Smuzhiyun case MLX4_RECV_OPCODE_SEND:
824*4882a593Smuzhiyun wc->opcode = IB_WC_RECV;
825*4882a593Smuzhiyun wc->wc_flags = 0;
826*4882a593Smuzhiyun break;
827*4882a593Smuzhiyun case MLX4_RECV_OPCODE_SEND_IMM:
828*4882a593Smuzhiyun wc->opcode = IB_WC_RECV;
829*4882a593Smuzhiyun wc->wc_flags = IB_WC_WITH_IMM;
830*4882a593Smuzhiyun wc->ex.imm_data = cqe->immed_rss_invalid;
831*4882a593Smuzhiyun break;
832*4882a593Smuzhiyun }
833*4882a593Smuzhiyun
834*4882a593Smuzhiyun is_eth = (rdma_port_get_link_layer(wc->qp->device,
835*4882a593Smuzhiyun (*cur_qp)->port) ==
836*4882a593Smuzhiyun IB_LINK_LAYER_ETHERNET);
837*4882a593Smuzhiyun if (mlx4_is_mfunc(to_mdev(cq->ibcq.device)->dev)) {
838*4882a593Smuzhiyun if ((*cur_qp)->mlx4_ib_qp_type &
839*4882a593Smuzhiyun (MLX4_IB_QPT_PROXY_SMI_OWNER |
840*4882a593Smuzhiyun MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI)) {
841*4882a593Smuzhiyun use_tunnel_data(*cur_qp, cq, wc, tail, cqe,
842*4882a593Smuzhiyun is_eth);
843*4882a593Smuzhiyun return 0;
844*4882a593Smuzhiyun }
845*4882a593Smuzhiyun }
846*4882a593Smuzhiyun
847*4882a593Smuzhiyun g_mlpath_rqpn = be32_to_cpu(cqe->g_mlpath_rqpn);
848*4882a593Smuzhiyun wc->src_qp = g_mlpath_rqpn & 0xffffff;
849*4882a593Smuzhiyun wc->dlid_path_bits = (g_mlpath_rqpn >> 24) & 0x7f;
850*4882a593Smuzhiyun wc->wc_flags |= g_mlpath_rqpn & 0x80000000 ? IB_WC_GRH : 0;
851*4882a593Smuzhiyun wc->pkey_index = be32_to_cpu(cqe->immed_rss_invalid) & 0x7f;
852*4882a593Smuzhiyun wc->wc_flags |= mlx4_ib_ipoib_csum_ok(cqe->status,
853*4882a593Smuzhiyun cqe->badfcs_enc,
854*4882a593Smuzhiyun cqe->checksum) ? IB_WC_IP_CSUM_OK : 0;
855*4882a593Smuzhiyun if (is_eth) {
856*4882a593Smuzhiyun wc->slid = 0;
857*4882a593Smuzhiyun wc->sl = be16_to_cpu(cqe->sl_vid) >> 13;
858*4882a593Smuzhiyun if (be32_to_cpu(cqe->vlan_my_qpn) &
859*4882a593Smuzhiyun MLX4_CQE_CVLAN_PRESENT_MASK) {
860*4882a593Smuzhiyun wc->vlan_id = be16_to_cpu(cqe->sl_vid) &
861*4882a593Smuzhiyun MLX4_CQE_VID_MASK;
862*4882a593Smuzhiyun } else {
863*4882a593Smuzhiyun wc->vlan_id = 0xffff;
864*4882a593Smuzhiyun }
865*4882a593Smuzhiyun memcpy(wc->smac, cqe->smac, ETH_ALEN);
866*4882a593Smuzhiyun wc->wc_flags |= (IB_WC_WITH_VLAN | IB_WC_WITH_SMAC);
867*4882a593Smuzhiyun } else {
868*4882a593Smuzhiyun wc->slid = be16_to_cpu(cqe->rlid);
869*4882a593Smuzhiyun wc->sl = be16_to_cpu(cqe->sl_vid) >> 12;
870*4882a593Smuzhiyun wc->vlan_id = 0xffff;
871*4882a593Smuzhiyun }
872*4882a593Smuzhiyun }
873*4882a593Smuzhiyun
874*4882a593Smuzhiyun return 0;
875*4882a593Smuzhiyun }
876*4882a593Smuzhiyun
mlx4_ib_poll_cq(struct ib_cq * ibcq,int num_entries,struct ib_wc * wc)877*4882a593Smuzhiyun int mlx4_ib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
878*4882a593Smuzhiyun {
879*4882a593Smuzhiyun struct mlx4_ib_cq *cq = to_mcq(ibcq);
880*4882a593Smuzhiyun struct mlx4_ib_qp *cur_qp = NULL;
881*4882a593Smuzhiyun unsigned long flags;
882*4882a593Smuzhiyun int npolled;
883*4882a593Smuzhiyun struct mlx4_ib_dev *mdev = to_mdev(cq->ibcq.device);
884*4882a593Smuzhiyun
885*4882a593Smuzhiyun spin_lock_irqsave(&cq->lock, flags);
886*4882a593Smuzhiyun if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
887*4882a593Smuzhiyun mlx4_ib_poll_sw_comp(cq, num_entries, wc, &npolled);
888*4882a593Smuzhiyun goto out;
889*4882a593Smuzhiyun }
890*4882a593Smuzhiyun
891*4882a593Smuzhiyun for (npolled = 0; npolled < num_entries; ++npolled) {
892*4882a593Smuzhiyun if (mlx4_ib_poll_one(cq, &cur_qp, wc + npolled))
893*4882a593Smuzhiyun break;
894*4882a593Smuzhiyun }
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun mlx4_cq_set_ci(&cq->mcq);
897*4882a593Smuzhiyun
898*4882a593Smuzhiyun out:
899*4882a593Smuzhiyun spin_unlock_irqrestore(&cq->lock, flags);
900*4882a593Smuzhiyun
901*4882a593Smuzhiyun return npolled;
902*4882a593Smuzhiyun }
903*4882a593Smuzhiyun
mlx4_ib_arm_cq(struct ib_cq * ibcq,enum ib_cq_notify_flags flags)904*4882a593Smuzhiyun int mlx4_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
905*4882a593Smuzhiyun {
906*4882a593Smuzhiyun mlx4_cq_arm(&to_mcq(ibcq)->mcq,
907*4882a593Smuzhiyun (flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?
908*4882a593Smuzhiyun MLX4_CQ_DB_REQ_NOT_SOL : MLX4_CQ_DB_REQ_NOT,
909*4882a593Smuzhiyun to_mdev(ibcq->device)->uar_map,
910*4882a593Smuzhiyun MLX4_GET_DOORBELL_LOCK(&to_mdev(ibcq->device)->uar_lock));
911*4882a593Smuzhiyun
912*4882a593Smuzhiyun return 0;
913*4882a593Smuzhiyun }
914*4882a593Smuzhiyun
__mlx4_ib_cq_clean(struct mlx4_ib_cq * cq,u32 qpn,struct mlx4_ib_srq * srq)915*4882a593Smuzhiyun void __mlx4_ib_cq_clean(struct mlx4_ib_cq *cq, u32 qpn, struct mlx4_ib_srq *srq)
916*4882a593Smuzhiyun {
917*4882a593Smuzhiyun u32 prod_index;
918*4882a593Smuzhiyun int nfreed = 0;
919*4882a593Smuzhiyun struct mlx4_cqe *cqe, *dest;
920*4882a593Smuzhiyun u8 owner_bit;
921*4882a593Smuzhiyun int cqe_inc = cq->buf.entry_size == 64 ? 1 : 0;
922*4882a593Smuzhiyun
923*4882a593Smuzhiyun /*
924*4882a593Smuzhiyun * First we need to find the current producer index, so we
925*4882a593Smuzhiyun * know where to start cleaning from. It doesn't matter if HW
926*4882a593Smuzhiyun * adds new entries after this loop -- the QP we're worried
927*4882a593Smuzhiyun * about is already in RESET, so the new entries won't come
928*4882a593Smuzhiyun * from our QP and therefore don't need to be checked.
929*4882a593Smuzhiyun */
930*4882a593Smuzhiyun for (prod_index = cq->mcq.cons_index; get_sw_cqe(cq, prod_index); ++prod_index)
931*4882a593Smuzhiyun if (prod_index == cq->mcq.cons_index + cq->ibcq.cqe)
932*4882a593Smuzhiyun break;
933*4882a593Smuzhiyun
934*4882a593Smuzhiyun /*
935*4882a593Smuzhiyun * Now sweep backwards through the CQ, removing CQ entries
936*4882a593Smuzhiyun * that match our QP by copying older entries on top of them.
937*4882a593Smuzhiyun */
938*4882a593Smuzhiyun while ((int) --prod_index - (int) cq->mcq.cons_index >= 0) {
939*4882a593Smuzhiyun cqe = get_cqe(cq, prod_index & cq->ibcq.cqe);
940*4882a593Smuzhiyun cqe += cqe_inc;
941*4882a593Smuzhiyun
942*4882a593Smuzhiyun if ((be32_to_cpu(cqe->vlan_my_qpn) & MLX4_CQE_QPN_MASK) == qpn) {
943*4882a593Smuzhiyun if (srq && !(cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK))
944*4882a593Smuzhiyun mlx4_ib_free_srq_wqe(srq, be16_to_cpu(cqe->wqe_index));
945*4882a593Smuzhiyun ++nfreed;
946*4882a593Smuzhiyun } else if (nfreed) {
947*4882a593Smuzhiyun dest = get_cqe(cq, (prod_index + nfreed) & cq->ibcq.cqe);
948*4882a593Smuzhiyun dest += cqe_inc;
949*4882a593Smuzhiyun
950*4882a593Smuzhiyun owner_bit = dest->owner_sr_opcode & MLX4_CQE_OWNER_MASK;
951*4882a593Smuzhiyun memcpy(dest, cqe, sizeof *cqe);
952*4882a593Smuzhiyun dest->owner_sr_opcode = owner_bit |
953*4882a593Smuzhiyun (dest->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK);
954*4882a593Smuzhiyun }
955*4882a593Smuzhiyun }
956*4882a593Smuzhiyun
957*4882a593Smuzhiyun if (nfreed) {
958*4882a593Smuzhiyun cq->mcq.cons_index += nfreed;
959*4882a593Smuzhiyun /*
960*4882a593Smuzhiyun * Make sure update of buffer contents is done before
961*4882a593Smuzhiyun * updating consumer index.
962*4882a593Smuzhiyun */
963*4882a593Smuzhiyun wmb();
964*4882a593Smuzhiyun mlx4_cq_set_ci(&cq->mcq);
965*4882a593Smuzhiyun }
966*4882a593Smuzhiyun }
967*4882a593Smuzhiyun
mlx4_ib_cq_clean(struct mlx4_ib_cq * cq,u32 qpn,struct mlx4_ib_srq * srq)968*4882a593Smuzhiyun void mlx4_ib_cq_clean(struct mlx4_ib_cq *cq, u32 qpn, struct mlx4_ib_srq *srq)
969*4882a593Smuzhiyun {
970*4882a593Smuzhiyun spin_lock_irq(&cq->lock);
971*4882a593Smuzhiyun __mlx4_ib_cq_clean(cq, qpn, srq);
972*4882a593Smuzhiyun spin_unlock_irq(&cq->lock);
973*4882a593Smuzhiyun }
974