blob: 5c9eeea628054db99df2c987478b6bd713dbcf16 [file] [log] [blame]
Eli Cohene126ba92013-07-07 17:25:49 +03001/*
Saeed Mahameed6cf0a152015-04-02 17:07:30 +03002 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
Eli Cohene126ba92013-07-07 17:25:49 +03003 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33#include <linux/kref.h>
34#include <rdma/ib_umem.h>
Yann Droneauda8237b32014-05-05 19:33:21 +020035#include <rdma/ib_user_verbs.h>
Eli Cohene126ba92013-07-07 17:25:49 +030036#include "mlx5_ib.h"
37#include "user.h"
38
39static void mlx5_ib_cq_comp(struct mlx5_core_cq *cq)
40{
41 struct ib_cq *ibcq = &to_mibcq(cq)->ibcq;
42
43 ibcq->comp_handler(ibcq, ibcq->cq_context);
44}
45
46static void mlx5_ib_cq_event(struct mlx5_core_cq *mcq, enum mlx5_event type)
47{
48 struct mlx5_ib_cq *cq = container_of(mcq, struct mlx5_ib_cq, mcq);
49 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
50 struct ib_cq *ibcq = &cq->ibcq;
51 struct ib_event event;
52
53 if (type != MLX5_EVENT_TYPE_CQ_ERROR) {
54 mlx5_ib_warn(dev, "Unexpected event type %d on CQ %06x\n",
55 type, mcq->cqn);
56 return;
57 }
58
59 if (ibcq->event_handler) {
60 event.device = &dev->ib_dev;
61 event.event = IB_EVENT_CQ_ERR;
62 event.element.cq = ibcq;
63 ibcq->event_handler(&event, ibcq->cq_context);
64 }
65}
66
67static void *get_cqe_from_buf(struct mlx5_ib_cq_buf *buf, int n, int size)
68{
69 return mlx5_buf_offset(&buf->buf, n * size);
70}
71
72static void *get_cqe(struct mlx5_ib_cq *cq, int n)
73{
74 return get_cqe_from_buf(&cq->buf, n, cq->mcq.cqe_sz);
75}
76
Eli Cohenbde51582014-01-14 17:45:18 +020077static u8 sw_ownership_bit(int n, int nent)
78{
79 return (n & nent) ? 1 : 0;
80}
81
Eli Cohene126ba92013-07-07 17:25:49 +030082static void *get_sw_cqe(struct mlx5_ib_cq *cq, int n)
83{
84 void *cqe = get_cqe(cq, n & cq->ibcq.cqe);
85 struct mlx5_cqe64 *cqe64;
86
87 cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
Eli Cohenbde51582014-01-14 17:45:18 +020088
89 if (likely((cqe64->op_own) >> 4 != MLX5_CQE_INVALID) &&
90 !((cqe64->op_own & MLX5_CQE_OWNER_MASK) ^ !!(n & (cq->ibcq.cqe + 1)))) {
91 return cqe;
92 } else {
93 return NULL;
94 }
Eli Cohene126ba92013-07-07 17:25:49 +030095}
96
97static void *next_cqe_sw(struct mlx5_ib_cq *cq)
98{
99 return get_sw_cqe(cq, cq->mcq.cons_index);
100}
101
102static enum ib_wc_opcode get_umr_comp(struct mlx5_ib_wq *wq, int idx)
103{
104 switch (wq->wr_data[idx]) {
105 case MLX5_IB_WR_UMR:
106 return 0;
107
108 case IB_WR_LOCAL_INV:
109 return IB_WC_LOCAL_INV;
110
111 case IB_WR_FAST_REG_MR:
112 return IB_WC_FAST_REG_MR;
113
114 default:
115 pr_warn("unknown completion status\n");
116 return 0;
117 }
118}
119
120static void handle_good_req(struct ib_wc *wc, struct mlx5_cqe64 *cqe,
121 struct mlx5_ib_wq *wq, int idx)
122{
123 wc->wc_flags = 0;
124 switch (be32_to_cpu(cqe->sop_drop_qpn) >> 24) {
125 case MLX5_OPCODE_RDMA_WRITE_IMM:
126 wc->wc_flags |= IB_WC_WITH_IMM;
127 case MLX5_OPCODE_RDMA_WRITE:
128 wc->opcode = IB_WC_RDMA_WRITE;
129 break;
130 case MLX5_OPCODE_SEND_IMM:
131 wc->wc_flags |= IB_WC_WITH_IMM;
132 case MLX5_OPCODE_SEND:
133 case MLX5_OPCODE_SEND_INVAL:
134 wc->opcode = IB_WC_SEND;
135 break;
136 case MLX5_OPCODE_RDMA_READ:
137 wc->opcode = IB_WC_RDMA_READ;
138 wc->byte_len = be32_to_cpu(cqe->byte_cnt);
139 break;
140 case MLX5_OPCODE_ATOMIC_CS:
141 wc->opcode = IB_WC_COMP_SWAP;
142 wc->byte_len = 8;
143 break;
144 case MLX5_OPCODE_ATOMIC_FA:
145 wc->opcode = IB_WC_FETCH_ADD;
146 wc->byte_len = 8;
147 break;
148 case MLX5_OPCODE_ATOMIC_MASKED_CS:
149 wc->opcode = IB_WC_MASKED_COMP_SWAP;
150 wc->byte_len = 8;
151 break;
152 case MLX5_OPCODE_ATOMIC_MASKED_FA:
153 wc->opcode = IB_WC_MASKED_FETCH_ADD;
154 wc->byte_len = 8;
155 break;
156 case MLX5_OPCODE_BIND_MW:
157 wc->opcode = IB_WC_BIND_MW;
158 break;
159 case MLX5_OPCODE_UMR:
160 wc->opcode = get_umr_comp(wq, idx);
161 break;
162 }
163}
164
165enum {
166 MLX5_GRH_IN_BUFFER = 1,
167 MLX5_GRH_IN_CQE = 2,
168};
169
170static void handle_responder(struct ib_wc *wc, struct mlx5_cqe64 *cqe,
171 struct mlx5_ib_qp *qp)
172{
173 struct mlx5_ib_dev *dev = to_mdev(qp->ibqp.device);
174 struct mlx5_ib_srq *srq;
175 struct mlx5_ib_wq *wq;
176 u16 wqe_ctr;
177 u8 g;
178
179 if (qp->ibqp.srq || qp->ibqp.xrcd) {
180 struct mlx5_core_srq *msrq = NULL;
181
182 if (qp->ibqp.xrcd) {
Jack Morgenstein9603b612014-07-28 23:30:22 +0300183 msrq = mlx5_core_get_srq(dev->mdev,
Eli Cohene126ba92013-07-07 17:25:49 +0300184 be32_to_cpu(cqe->srqn));
185 srq = to_mibsrq(msrq);
186 } else {
187 srq = to_msrq(qp->ibqp.srq);
188 }
189 if (srq) {
190 wqe_ctr = be16_to_cpu(cqe->wqe_counter);
191 wc->wr_id = srq->wrid[wqe_ctr];
192 mlx5_ib_free_srq_wqe(srq, wqe_ctr);
193 if (msrq && atomic_dec_and_test(&msrq->refcount))
194 complete(&msrq->free);
195 }
196 } else {
197 wq = &qp->rq;
198 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
199 ++wq->tail;
200 }
201 wc->byte_len = be32_to_cpu(cqe->byte_cnt);
202
203 switch (cqe->op_own >> 4) {
204 case MLX5_CQE_RESP_WR_IMM:
205 wc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
206 wc->wc_flags = IB_WC_WITH_IMM;
207 wc->ex.imm_data = cqe->imm_inval_pkey;
208 break;
209 case MLX5_CQE_RESP_SEND:
210 wc->opcode = IB_WC_RECV;
211 wc->wc_flags = 0;
212 break;
213 case MLX5_CQE_RESP_SEND_IMM:
214 wc->opcode = IB_WC_RECV;
215 wc->wc_flags = IB_WC_WITH_IMM;
216 wc->ex.imm_data = cqe->imm_inval_pkey;
217 break;
218 case MLX5_CQE_RESP_SEND_INV:
219 wc->opcode = IB_WC_RECV;
220 wc->wc_flags = IB_WC_WITH_INVALIDATE;
221 wc->ex.invalidate_rkey = be32_to_cpu(cqe->imm_inval_pkey);
222 break;
223 }
224 wc->slid = be16_to_cpu(cqe->slid);
225 wc->sl = (be32_to_cpu(cqe->flags_rqpn) >> 24) & 0xf;
226 wc->src_qp = be32_to_cpu(cqe->flags_rqpn) & 0xffffff;
227 wc->dlid_path_bits = cqe->ml_path;
228 g = (be32_to_cpu(cqe->flags_rqpn) >> 28) & 3;
229 wc->wc_flags |= g ? IB_WC_GRH : 0;
230 wc->pkey_index = be32_to_cpu(cqe->imm_inval_pkey) & 0xffff;
231}
232
233static void dump_cqe(struct mlx5_ib_dev *dev, struct mlx5_err_cqe *cqe)
234{
235 __be32 *p = (__be32 *)cqe;
236 int i;
237
238 mlx5_ib_warn(dev, "dump error cqe\n");
239 for (i = 0; i < sizeof(*cqe) / 16; i++, p += 4)
240 pr_info("%08x %08x %08x %08x\n", be32_to_cpu(p[0]),
241 be32_to_cpu(p[1]), be32_to_cpu(p[2]),
242 be32_to_cpu(p[3]));
243}
244
245static void mlx5_handle_error_cqe(struct mlx5_ib_dev *dev,
246 struct mlx5_err_cqe *cqe,
247 struct ib_wc *wc)
248{
249 int dump = 1;
250
251 switch (cqe->syndrome) {
252 case MLX5_CQE_SYNDROME_LOCAL_LENGTH_ERR:
253 wc->status = IB_WC_LOC_LEN_ERR;
254 break;
255 case MLX5_CQE_SYNDROME_LOCAL_QP_OP_ERR:
256 wc->status = IB_WC_LOC_QP_OP_ERR;
257 break;
258 case MLX5_CQE_SYNDROME_LOCAL_PROT_ERR:
259 wc->status = IB_WC_LOC_PROT_ERR;
260 break;
261 case MLX5_CQE_SYNDROME_WR_FLUSH_ERR:
262 dump = 0;
263 wc->status = IB_WC_WR_FLUSH_ERR;
264 break;
265 case MLX5_CQE_SYNDROME_MW_BIND_ERR:
266 wc->status = IB_WC_MW_BIND_ERR;
267 break;
268 case MLX5_CQE_SYNDROME_BAD_RESP_ERR:
269 wc->status = IB_WC_BAD_RESP_ERR;
270 break;
271 case MLX5_CQE_SYNDROME_LOCAL_ACCESS_ERR:
272 wc->status = IB_WC_LOC_ACCESS_ERR;
273 break;
274 case MLX5_CQE_SYNDROME_REMOTE_INVAL_REQ_ERR:
275 wc->status = IB_WC_REM_INV_REQ_ERR;
276 break;
277 case MLX5_CQE_SYNDROME_REMOTE_ACCESS_ERR:
278 wc->status = IB_WC_REM_ACCESS_ERR;
279 break;
280 case MLX5_CQE_SYNDROME_REMOTE_OP_ERR:
281 wc->status = IB_WC_REM_OP_ERR;
282 break;
283 case MLX5_CQE_SYNDROME_TRANSPORT_RETRY_EXC_ERR:
284 wc->status = IB_WC_RETRY_EXC_ERR;
285 dump = 0;
286 break;
287 case MLX5_CQE_SYNDROME_RNR_RETRY_EXC_ERR:
288 wc->status = IB_WC_RNR_RETRY_EXC_ERR;
289 dump = 0;
290 break;
291 case MLX5_CQE_SYNDROME_REMOTE_ABORTED_ERR:
292 wc->status = IB_WC_REM_ABORT_ERR;
293 break;
294 default:
295 wc->status = IB_WC_GENERAL_ERR;
296 break;
297 }
298
299 wc->vendor_err = cqe->vendor_err_synd;
300 if (dump)
301 dump_cqe(dev, cqe);
302}
303
304static int is_atomic_response(struct mlx5_ib_qp *qp, uint16_t idx)
305{
306 /* TBD: waiting decision
307 */
308 return 0;
309}
310
311static void *mlx5_get_atomic_laddr(struct mlx5_ib_qp *qp, uint16_t idx)
312{
313 struct mlx5_wqe_data_seg *dpseg;
314 void *addr;
315
316 dpseg = mlx5_get_send_wqe(qp, idx) + sizeof(struct mlx5_wqe_ctrl_seg) +
317 sizeof(struct mlx5_wqe_raddr_seg) +
318 sizeof(struct mlx5_wqe_atomic_seg);
319 addr = (void *)(unsigned long)be64_to_cpu(dpseg->addr);
320 return addr;
321}
322
323static void handle_atomic(struct mlx5_ib_qp *qp, struct mlx5_cqe64 *cqe64,
324 uint16_t idx)
325{
326 void *addr;
327 int byte_count;
328 int i;
329
330 if (!is_atomic_response(qp, idx))
331 return;
332
333 byte_count = be32_to_cpu(cqe64->byte_cnt);
334 addr = mlx5_get_atomic_laddr(qp, idx);
335
336 if (byte_count == 4) {
337 *(uint32_t *)addr = be32_to_cpu(*((__be32 *)addr));
338 } else {
339 for (i = 0; i < byte_count; i += 8) {
340 *(uint64_t *)addr = be64_to_cpu(*((__be64 *)addr));
341 addr += 8;
342 }
343 }
344
345 return;
346}
347
348static void handle_atomics(struct mlx5_ib_qp *qp, struct mlx5_cqe64 *cqe64,
349 u16 tail, u16 head)
350{
Jack Morgensteinf241e742014-07-28 23:30:23 +0300351 u16 idx;
Eli Cohene126ba92013-07-07 17:25:49 +0300352
353 do {
354 idx = tail & (qp->sq.wqe_cnt - 1);
355 handle_atomic(qp, cqe64, idx);
356 if (idx == head)
357 break;
358
359 tail = qp->sq.w_list[idx].next;
360 } while (1);
361 tail = qp->sq.w_list[idx].next;
362 qp->sq.last_poll = tail;
363}
364
Eli Cohenbde51582014-01-14 17:45:18 +0200365static void free_cq_buf(struct mlx5_ib_dev *dev, struct mlx5_ib_cq_buf *buf)
366{
Jack Morgenstein9603b612014-07-28 23:30:22 +0300367 mlx5_buf_free(dev->mdev, &buf->buf);
Eli Cohenbde51582014-01-14 17:45:18 +0200368}
369
Sagi Grimbergd5436ba2014-02-23 14:19:12 +0200370static void get_sig_err_item(struct mlx5_sig_err_cqe *cqe,
371 struct ib_sig_err *item)
372{
373 u16 syndrome = be16_to_cpu(cqe->syndrome);
374
375#define GUARD_ERR (1 << 13)
376#define APPTAG_ERR (1 << 12)
377#define REFTAG_ERR (1 << 11)
378
379 if (syndrome & GUARD_ERR) {
380 item->err_type = IB_SIG_BAD_GUARD;
381 item->expected = be32_to_cpu(cqe->expected_trans_sig) >> 16;
382 item->actual = be32_to_cpu(cqe->actual_trans_sig) >> 16;
383 } else
384 if (syndrome & REFTAG_ERR) {
385 item->err_type = IB_SIG_BAD_REFTAG;
386 item->expected = be32_to_cpu(cqe->expected_reftag);
387 item->actual = be32_to_cpu(cqe->actual_reftag);
388 } else
389 if (syndrome & APPTAG_ERR) {
390 item->err_type = IB_SIG_BAD_APPTAG;
391 item->expected = be32_to_cpu(cqe->expected_trans_sig) & 0xffff;
392 item->actual = be32_to_cpu(cqe->actual_trans_sig) & 0xffff;
393 } else {
394 pr_err("Got signature completion error with bad syndrome %04x\n",
395 syndrome);
396 }
397
398 item->sig_err_offset = be64_to_cpu(cqe->err_offset);
399 item->key = be32_to_cpu(cqe->mkey);
400}
401
Eli Cohene126ba92013-07-07 17:25:49 +0300402static int mlx5_poll_one(struct mlx5_ib_cq *cq,
403 struct mlx5_ib_qp **cur_qp,
404 struct ib_wc *wc)
405{
406 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
407 struct mlx5_err_cqe *err_cqe;
408 struct mlx5_cqe64 *cqe64;
409 struct mlx5_core_qp *mqp;
410 struct mlx5_ib_wq *wq;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +0200411 struct mlx5_sig_err_cqe *sig_err_cqe;
412 struct mlx5_core_mr *mmr;
413 struct mlx5_ib_mr *mr;
Eli Cohene126ba92013-07-07 17:25:49 +0300414 uint8_t opcode;
415 uint32_t qpn;
416 u16 wqe_ctr;
417 void *cqe;
418 int idx;
419
Eli Cohenbde51582014-01-14 17:45:18 +0200420repoll:
Eli Cohene126ba92013-07-07 17:25:49 +0300421 cqe = next_cqe_sw(cq);
422 if (!cqe)
423 return -EAGAIN;
424
425 cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
426
427 ++cq->mcq.cons_index;
428
429 /* Make sure we read CQ entry contents after we've checked the
430 * ownership bit.
431 */
432 rmb();
433
Eli Cohenbde51582014-01-14 17:45:18 +0200434 opcode = cqe64->op_own >> 4;
435 if (unlikely(opcode == MLX5_CQE_RESIZE_CQ)) {
436 if (likely(cq->resize_buf)) {
437 free_cq_buf(dev, &cq->buf);
438 cq->buf = *cq->resize_buf;
439 kfree(cq->resize_buf);
440 cq->resize_buf = NULL;
441 goto repoll;
442 } else {
443 mlx5_ib_warn(dev, "unexpected resize cqe\n");
444 }
445 }
Eli Cohene126ba92013-07-07 17:25:49 +0300446
447 qpn = ntohl(cqe64->sop_drop_qpn) & 0xffffff;
448 if (!*cur_qp || (qpn != (*cur_qp)->ibqp.qp_num)) {
449 /* We do not have to take the QP table lock here,
450 * because CQs will be locked while QPs are removed
451 * from the table.
452 */
Jack Morgenstein9603b612014-07-28 23:30:22 +0300453 mqp = __mlx5_qp_lookup(dev->mdev, qpn);
Eli Cohene126ba92013-07-07 17:25:49 +0300454 if (unlikely(!mqp)) {
455 mlx5_ib_warn(dev, "CQE@CQ %06x for unknown QPN %6x\n",
456 cq->mcq.cqn, qpn);
457 return -EINVAL;
458 }
459
460 *cur_qp = to_mibqp(mqp);
461 }
462
463 wc->qp = &(*cur_qp)->ibqp;
Eli Cohene126ba92013-07-07 17:25:49 +0300464 switch (opcode) {
465 case MLX5_CQE_REQ:
466 wq = &(*cur_qp)->sq;
467 wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
468 idx = wqe_ctr & (wq->wqe_cnt - 1);
469 handle_good_req(wc, cqe64, wq, idx);
470 handle_atomics(*cur_qp, cqe64, wq->last_poll, idx);
471 wc->wr_id = wq->wrid[idx];
472 wq->tail = wq->wqe_head[idx] + 1;
473 wc->status = IB_WC_SUCCESS;
474 break;
475 case MLX5_CQE_RESP_WR_IMM:
476 case MLX5_CQE_RESP_SEND:
477 case MLX5_CQE_RESP_SEND_IMM:
478 case MLX5_CQE_RESP_SEND_INV:
479 handle_responder(wc, cqe64, *cur_qp);
480 wc->status = IB_WC_SUCCESS;
481 break;
482 case MLX5_CQE_RESIZE_CQ:
483 break;
484 case MLX5_CQE_REQ_ERR:
485 case MLX5_CQE_RESP_ERR:
486 err_cqe = (struct mlx5_err_cqe *)cqe64;
487 mlx5_handle_error_cqe(dev, err_cqe, wc);
488 mlx5_ib_dbg(dev, "%s error cqe on cqn 0x%x:\n",
489 opcode == MLX5_CQE_REQ_ERR ?
490 "Requestor" : "Responder", cq->mcq.cqn);
491 mlx5_ib_dbg(dev, "syndrome 0x%x, vendor syndrome 0x%x\n",
492 err_cqe->syndrome, err_cqe->vendor_err_synd);
493 if (opcode == MLX5_CQE_REQ_ERR) {
494 wq = &(*cur_qp)->sq;
495 wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
496 idx = wqe_ctr & (wq->wqe_cnt - 1);
497 wc->wr_id = wq->wrid[idx];
498 wq->tail = wq->wqe_head[idx] + 1;
499 } else {
500 struct mlx5_ib_srq *srq;
501
502 if ((*cur_qp)->ibqp.srq) {
503 srq = to_msrq((*cur_qp)->ibqp.srq);
504 wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
505 wc->wr_id = srq->wrid[wqe_ctr];
506 mlx5_ib_free_srq_wqe(srq, wqe_ctr);
507 } else {
508 wq = &(*cur_qp)->rq;
509 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
510 ++wq->tail;
511 }
512 }
513 break;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +0200514 case MLX5_CQE_SIG_ERR:
515 sig_err_cqe = (struct mlx5_sig_err_cqe *)cqe64;
516
Jack Morgenstein9603b612014-07-28 23:30:22 +0300517 read_lock(&dev->mdev->priv.mr_table.lock);
518 mmr = __mlx5_mr_lookup(dev->mdev,
Sagi Grimbergd5436ba2014-02-23 14:19:12 +0200519 mlx5_base_mkey(be32_to_cpu(sig_err_cqe->mkey)));
520 if (unlikely(!mmr)) {
Jack Morgenstein9603b612014-07-28 23:30:22 +0300521 read_unlock(&dev->mdev->priv.mr_table.lock);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +0200522 mlx5_ib_warn(dev, "CQE@CQ %06x for unknown MR %6x\n",
523 cq->mcq.cqn, be32_to_cpu(sig_err_cqe->mkey));
524 return -EINVAL;
525 }
526
527 mr = to_mibmr(mmr);
528 get_sig_err_item(sig_err_cqe, &mr->sig->err_item);
529 mr->sig->sig_err_exists = true;
530 mr->sig->sigerr_count++;
531
532 mlx5_ib_warn(dev, "CQN: 0x%x Got SIGERR on key: 0x%x err_type %x err_offset %llx expected %x actual %x\n",
533 cq->mcq.cqn, mr->sig->err_item.key,
534 mr->sig->err_item.err_type,
535 mr->sig->err_item.sig_err_offset,
536 mr->sig->err_item.expected,
537 mr->sig->err_item.actual);
538
Jack Morgenstein9603b612014-07-28 23:30:22 +0300539 read_unlock(&dev->mdev->priv.mr_table.lock);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +0200540 goto repoll;
Eli Cohene126ba92013-07-07 17:25:49 +0300541 }
542
543 return 0;
544}
545
546int mlx5_ib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
547{
548 struct mlx5_ib_cq *cq = to_mcq(ibcq);
549 struct mlx5_ib_qp *cur_qp = NULL;
550 unsigned long flags;
551 int npolled;
552 int err = 0;
553
554 spin_lock_irqsave(&cq->lock, flags);
555
556 for (npolled = 0; npolled < num_entries; npolled++) {
557 err = mlx5_poll_one(cq, &cur_qp, wc + npolled);
558 if (err)
559 break;
560 }
561
562 if (npolled)
563 mlx5_cq_set_ci(&cq->mcq);
564
565 spin_unlock_irqrestore(&cq->lock, flags);
566
567 if (err == 0 || err == -EAGAIN)
568 return npolled;
569 else
570 return err;
571}
572
573int mlx5_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
574{
Saeed Mahameedce0f7502015-04-02 17:07:33 +0300575 struct mlx5_core_dev *mdev = to_mdev(ibcq->device)->mdev;
576 void __iomem *uar_page = mdev->priv.uuari.uars[0].map;
577
Eli Cohene126ba92013-07-07 17:25:49 +0300578 mlx5_cq_arm(&to_mcq(ibcq)->mcq,
579 (flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?
580 MLX5_CQ_DB_REQ_NOT_SOL : MLX5_CQ_DB_REQ_NOT,
Saeed Mahameedce0f7502015-04-02 17:07:33 +0300581 uar_page,
582 MLX5_GET_DOORBELL_LOCK(&mdev->priv.cq_uar_lock),
583 to_mcq(ibcq)->mcq.cons_index);
Eli Cohene126ba92013-07-07 17:25:49 +0300584
585 return 0;
586}
587
588static int alloc_cq_buf(struct mlx5_ib_dev *dev, struct mlx5_ib_cq_buf *buf,
589 int nent, int cqe_size)
590{
591 int err;
592
Amir Vadai64ffaa22015-05-28 22:28:38 +0300593 err = mlx5_buf_alloc(dev->mdev, nent * cqe_size, &buf->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300594 if (err)
595 return err;
596
597 buf->cqe_size = cqe_size;
Eli Cohenbde51582014-01-14 17:45:18 +0200598 buf->nent = nent;
Eli Cohene126ba92013-07-07 17:25:49 +0300599
600 return 0;
601}
602
Eli Cohene126ba92013-07-07 17:25:49 +0300603static int create_cq_user(struct mlx5_ib_dev *dev, struct ib_udata *udata,
604 struct ib_ucontext *context, struct mlx5_ib_cq *cq,
605 int entries, struct mlx5_create_cq_mbox_in **cqb,
606 int *cqe_size, int *index, int *inlen)
607{
608 struct mlx5_ib_create_cq ucmd;
Yann Droneauda8237b32014-05-05 19:33:21 +0200609 size_t ucmdlen;
Eli Cohene126ba92013-07-07 17:25:49 +0300610 int page_shift;
611 int npages;
612 int ncont;
613 int err;
614
Yann Droneauda8237b32014-05-05 19:33:21 +0200615 ucmdlen =
616 (udata->inlen - sizeof(struct ib_uverbs_cmd_hdr) <
617 sizeof(ucmd)) ? (sizeof(ucmd) -
618 sizeof(ucmd.reserved)) : sizeof(ucmd);
619
620 if (ib_copy_from_udata(&ucmd, udata, ucmdlen))
Eli Cohene126ba92013-07-07 17:25:49 +0300621 return -EFAULT;
622
Yann Droneauda8237b32014-05-05 19:33:21 +0200623 if (ucmdlen == sizeof(ucmd) &&
624 ucmd.reserved != 0)
625 return -EINVAL;
626
Eli Cohene126ba92013-07-07 17:25:49 +0300627 if (ucmd.cqe_size != 64 && ucmd.cqe_size != 128)
628 return -EINVAL;
629
630 *cqe_size = ucmd.cqe_size;
631
632 cq->buf.umem = ib_umem_get(context, ucmd.buf_addr,
633 entries * ucmd.cqe_size,
634 IB_ACCESS_LOCAL_WRITE, 1);
635 if (IS_ERR(cq->buf.umem)) {
636 err = PTR_ERR(cq->buf.umem);
637 return err;
638 }
639
640 err = mlx5_ib_db_map_user(to_mucontext(context), ucmd.db_addr,
641 &cq->db);
642 if (err)
643 goto err_umem;
644
645 mlx5_ib_cont_pages(cq->buf.umem, ucmd.buf_addr, &npages, &page_shift,
646 &ncont, NULL);
647 mlx5_ib_dbg(dev, "addr 0x%llx, size %u, npages %d, page_shift %d, ncont %d\n",
648 ucmd.buf_addr, entries * ucmd.cqe_size, npages, page_shift, ncont);
649
650 *inlen = sizeof(**cqb) + sizeof(*(*cqb)->pas) * ncont;
651 *cqb = mlx5_vzalloc(*inlen);
652 if (!*cqb) {
653 err = -ENOMEM;
654 goto err_db;
655 }
656 mlx5_ib_populate_pas(dev, cq->buf.umem, page_shift, (*cqb)->pas, 0);
Eli Cohencf1c5e12013-10-31 15:26:36 +0200657 (*cqb)->ctx.log_pg_sz = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
Eli Cohene126ba92013-07-07 17:25:49 +0300658
659 *index = to_mucontext(context)->uuari.uars[0].index;
660
661 return 0;
662
663err_db:
664 mlx5_ib_db_unmap_user(to_mucontext(context), &cq->db);
665
666err_umem:
667 ib_umem_release(cq->buf.umem);
668 return err;
669}
670
671static void destroy_cq_user(struct mlx5_ib_cq *cq, struct ib_ucontext *context)
672{
673 mlx5_ib_db_unmap_user(to_mucontext(context), &cq->db);
674 ib_umem_release(cq->buf.umem);
675}
676
Eli Cohenbde51582014-01-14 17:45:18 +0200677static void init_cq_buf(struct mlx5_ib_cq *cq, struct mlx5_ib_cq_buf *buf)
Eli Cohene126ba92013-07-07 17:25:49 +0300678{
679 int i;
680 void *cqe;
681 struct mlx5_cqe64 *cqe64;
682
Eli Cohenbde51582014-01-14 17:45:18 +0200683 for (i = 0; i < buf->nent; i++) {
684 cqe = get_cqe_from_buf(buf, i, buf->cqe_size);
685 cqe64 = buf->cqe_size == 64 ? cqe : cqe + 64;
686 cqe64->op_own = MLX5_CQE_INVALID << 4;
Eli Cohene126ba92013-07-07 17:25:49 +0300687 }
688}
689
690static int create_cq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
691 int entries, int cqe_size,
692 struct mlx5_create_cq_mbox_in **cqb,
693 int *index, int *inlen)
694{
695 int err;
696
Jack Morgenstein9603b612014-07-28 23:30:22 +0300697 err = mlx5_db_alloc(dev->mdev, &cq->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300698 if (err)
699 return err;
700
701 cq->mcq.set_ci_db = cq->db.db;
702 cq->mcq.arm_db = cq->db.db + 1;
Eli Cohene126ba92013-07-07 17:25:49 +0300703 cq->mcq.cqe_sz = cqe_size;
704
705 err = alloc_cq_buf(dev, &cq->buf, entries, cqe_size);
706 if (err)
707 goto err_db;
708
Eli Cohenbde51582014-01-14 17:45:18 +0200709 init_cq_buf(cq, &cq->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300710
711 *inlen = sizeof(**cqb) + sizeof(*(*cqb)->pas) * cq->buf.buf.npages;
712 *cqb = mlx5_vzalloc(*inlen);
713 if (!*cqb) {
714 err = -ENOMEM;
715 goto err_buf;
716 }
717 mlx5_fill_page_array(&cq->buf.buf, (*cqb)->pas);
718
Eli Cohen1b77d2b2013-10-24 12:01:03 +0300719 (*cqb)->ctx.log_pg_sz = cq->buf.buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT;
Jack Morgenstein9603b612014-07-28 23:30:22 +0300720 *index = dev->mdev->priv.uuari.uars[0].index;
Eli Cohene126ba92013-07-07 17:25:49 +0300721
722 return 0;
723
724err_buf:
725 free_cq_buf(dev, &cq->buf);
726
727err_db:
Jack Morgenstein9603b612014-07-28 23:30:22 +0300728 mlx5_db_free(dev->mdev, &cq->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300729 return err;
730}
731
732static void destroy_cq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq)
733{
734 free_cq_buf(dev, &cq->buf);
Jack Morgenstein9603b612014-07-28 23:30:22 +0300735 mlx5_db_free(dev->mdev, &cq->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300736}
737
Matan Barakbcf4c1e2015-06-11 16:35:20 +0300738struct ib_cq *mlx5_ib_create_cq(struct ib_device *ibdev,
739 const struct ib_cq_init_attr *attr,
740 struct ib_ucontext *context,
Eli Cohene126ba92013-07-07 17:25:49 +0300741 struct ib_udata *udata)
742{
Matan Barakbcf4c1e2015-06-11 16:35:20 +0300743 int entries = attr->cqe;
744 int vector = attr->comp_vector;
Eli Cohene126ba92013-07-07 17:25:49 +0300745 struct mlx5_create_cq_mbox_in *cqb = NULL;
746 struct mlx5_ib_dev *dev = to_mdev(ibdev);
747 struct mlx5_ib_cq *cq;
748 int uninitialized_var(index);
749 int uninitialized_var(inlen);
750 int cqe_size;
751 int irqn;
752 int eqn;
753 int err;
754
Matan Barakbcf4c1e2015-06-11 16:35:20 +0300755 if (attr->flags)
756 return ERR_PTR(-EINVAL);
757
Eli Cohen51ee86a2013-10-23 09:53:13 +0300758 if (entries < 0)
759 return ERR_PTR(-EINVAL);
760
Eli Cohene126ba92013-07-07 17:25:49 +0300761 entries = roundup_pow_of_two(entries + 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300762 if (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz)))
Eli Cohene126ba92013-07-07 17:25:49 +0300763 return ERR_PTR(-EINVAL);
764
765 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
766 if (!cq)
767 return ERR_PTR(-ENOMEM);
768
769 cq->ibcq.cqe = entries - 1;
770 mutex_init(&cq->resize_mutex);
771 spin_lock_init(&cq->lock);
772 cq->resize_buf = NULL;
773 cq->resize_umem = NULL;
774
775 if (context) {
776 err = create_cq_user(dev, udata, context, cq, entries,
777 &cqb, &cqe_size, &index, &inlen);
778 if (err)
779 goto err_create;
780 } else {
781 /* for now choose 64 bytes till we have a proper interface */
782 cqe_size = 64;
783 err = create_cq_kernel(dev, cq, entries, cqe_size, &cqb,
784 &index, &inlen);
785 if (err)
786 goto err_create;
787 }
788
789 cq->cqe_size = cqe_size;
790 cqb->ctx.cqe_sz_flags = cqe_sz_to_mlx_sz(cqe_size) << 5;
791 cqb->ctx.log_sz_usr_page = cpu_to_be32((ilog2(entries) << 24) | index);
Saeed Mahameed233d05d2015-04-02 17:07:32 +0300792 err = mlx5_vector2eqn(dev->mdev, vector, &eqn, &irqn);
Eli Cohene126ba92013-07-07 17:25:49 +0300793 if (err)
794 goto err_cqb;
795
796 cqb->ctx.c_eqn = cpu_to_be16(eqn);
797 cqb->ctx.db_record_addr = cpu_to_be64(cq->db.dma);
798
Jack Morgenstein9603b612014-07-28 23:30:22 +0300799 err = mlx5_core_create_cq(dev->mdev, &cq->mcq, cqb, inlen);
Eli Cohene126ba92013-07-07 17:25:49 +0300800 if (err)
801 goto err_cqb;
802
803 mlx5_ib_dbg(dev, "cqn 0x%x\n", cq->mcq.cqn);
804 cq->mcq.irqn = irqn;
805 cq->mcq.comp = mlx5_ib_cq_comp;
806 cq->mcq.event = mlx5_ib_cq_event;
807
808 if (context)
809 if (ib_copy_to_udata(udata, &cq->mcq.cqn, sizeof(__u32))) {
810 err = -EFAULT;
811 goto err_cmd;
812 }
813
814
Al Viro479163f2014-11-20 08:13:57 +0000815 kvfree(cqb);
Eli Cohene126ba92013-07-07 17:25:49 +0300816 return &cq->ibcq;
817
818err_cmd:
Jack Morgenstein9603b612014-07-28 23:30:22 +0300819 mlx5_core_destroy_cq(dev->mdev, &cq->mcq);
Eli Cohene126ba92013-07-07 17:25:49 +0300820
821err_cqb:
Al Viro479163f2014-11-20 08:13:57 +0000822 kvfree(cqb);
Eli Cohene126ba92013-07-07 17:25:49 +0300823 if (context)
824 destroy_cq_user(cq, context);
825 else
826 destroy_cq_kernel(dev, cq);
827
828err_create:
829 kfree(cq);
830
831 return ERR_PTR(err);
832}
833
834
835int mlx5_ib_destroy_cq(struct ib_cq *cq)
836{
837 struct mlx5_ib_dev *dev = to_mdev(cq->device);
838 struct mlx5_ib_cq *mcq = to_mcq(cq);
839 struct ib_ucontext *context = NULL;
840
841 if (cq->uobject)
842 context = cq->uobject->context;
843
Jack Morgenstein9603b612014-07-28 23:30:22 +0300844 mlx5_core_destroy_cq(dev->mdev, &mcq->mcq);
Eli Cohene126ba92013-07-07 17:25:49 +0300845 if (context)
846 destroy_cq_user(mcq, context);
847 else
848 destroy_cq_kernel(dev, mcq);
849
850 kfree(mcq);
851
852 return 0;
853}
854
Moshe Lazercfd8f1d2013-10-23 09:53:17 +0300855static int is_equal_rsn(struct mlx5_cqe64 *cqe64, u32 rsn)
Eli Cohene126ba92013-07-07 17:25:49 +0300856{
Moshe Lazercfd8f1d2013-10-23 09:53:17 +0300857 return rsn == (ntohl(cqe64->sop_drop_qpn) & 0xffffff);
Eli Cohene126ba92013-07-07 17:25:49 +0300858}
859
860void __mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 rsn, struct mlx5_ib_srq *srq)
861{
862 struct mlx5_cqe64 *cqe64, *dest64;
863 void *cqe, *dest;
864 u32 prod_index;
865 int nfreed = 0;
866 u8 owner_bit;
867
868 if (!cq)
869 return;
870
871 /* First we need to find the current producer index, so we
872 * know where to start cleaning from. It doesn't matter if HW
873 * adds new entries after this loop -- the QP we're worried
874 * about is already in RESET, so the new entries won't come
875 * from our QP and therefore don't need to be checked.
876 */
877 for (prod_index = cq->mcq.cons_index; get_sw_cqe(cq, prod_index); prod_index++)
878 if (prod_index == cq->mcq.cons_index + cq->ibcq.cqe)
879 break;
880
881 /* Now sweep backwards through the CQ, removing CQ entries
882 * that match our QP by copying older entries on top of them.
883 */
884 while ((int) --prod_index - (int) cq->mcq.cons_index >= 0) {
885 cqe = get_cqe(cq, prod_index & cq->ibcq.cqe);
886 cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
Moshe Lazercfd8f1d2013-10-23 09:53:17 +0300887 if (is_equal_rsn(cqe64, rsn)) {
888 if (srq && (ntohl(cqe64->srqn) & 0xffffff))
Eli Cohene126ba92013-07-07 17:25:49 +0300889 mlx5_ib_free_srq_wqe(srq, be16_to_cpu(cqe64->wqe_counter));
890 ++nfreed;
891 } else if (nfreed) {
892 dest = get_cqe(cq, (prod_index + nfreed) & cq->ibcq.cqe);
893 dest64 = (cq->mcq.cqe_sz == 64) ? dest : dest + 64;
894 owner_bit = dest64->op_own & MLX5_CQE_OWNER_MASK;
895 memcpy(dest, cqe, cq->mcq.cqe_sz);
896 dest64->op_own = owner_bit |
897 (dest64->op_own & ~MLX5_CQE_OWNER_MASK);
898 }
899 }
900
901 if (nfreed) {
902 cq->mcq.cons_index += nfreed;
903 /* Make sure update of buffer contents is done before
904 * updating consumer index.
905 */
906 wmb();
907 mlx5_cq_set_ci(&cq->mcq);
908 }
909}
910
911void mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 qpn, struct mlx5_ib_srq *srq)
912{
913 if (!cq)
914 return;
915
916 spin_lock_irq(&cq->lock);
917 __mlx5_ib_cq_clean(cq, qpn, srq);
918 spin_unlock_irq(&cq->lock);
919}
920
921int mlx5_ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
922{
Eli Cohen3bdb31f2014-01-14 17:45:17 +0200923 struct mlx5_modify_cq_mbox_in *in;
924 struct mlx5_ib_dev *dev = to_mdev(cq->device);
925 struct mlx5_ib_cq *mcq = to_mcq(cq);
926 int err;
927 u32 fsel;
928
Saeed Mahameed938fe832015-05-28 22:28:41 +0300929 if (!MLX5_CAP_GEN(dev->mdev, cq_moderation))
Eli Cohen3bdb31f2014-01-14 17:45:17 +0200930 return -ENOSYS;
931
932 in = kzalloc(sizeof(*in), GFP_KERNEL);
933 if (!in)
934 return -ENOMEM;
935
936 in->cqn = cpu_to_be32(mcq->mcq.cqn);
937 fsel = (MLX5_CQ_MODIFY_PERIOD | MLX5_CQ_MODIFY_COUNT);
938 in->ctx.cq_period = cpu_to_be16(cq_period);
939 in->ctx.cq_max_count = cpu_to_be16(cq_count);
940 in->field_select = cpu_to_be32(fsel);
Jack Morgenstein9603b612014-07-28 23:30:22 +0300941 err = mlx5_core_modify_cq(dev->mdev, &mcq->mcq, in, sizeof(*in));
Eli Cohen3bdb31f2014-01-14 17:45:17 +0200942 kfree(in);
943
944 if (err)
945 mlx5_ib_warn(dev, "modify cq 0x%x failed\n", mcq->mcq.cqn);
946
947 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300948}
949
Eli Cohenbde51582014-01-14 17:45:18 +0200950static int resize_user(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
951 int entries, struct ib_udata *udata, int *npas,
952 int *page_shift, int *cqe_size)
953{
954 struct mlx5_ib_resize_cq ucmd;
955 struct ib_umem *umem;
956 int err;
957 int npages;
958 struct ib_ucontext *context = cq->buf.umem->context;
959
Eli Cohen57761d82014-01-15 14:56:44 +0200960 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
961 if (err)
962 return err;
963
964 if (ucmd.reserved0 || ucmd.reserved1)
965 return -EINVAL;
Eli Cohenbde51582014-01-14 17:45:18 +0200966
967 umem = ib_umem_get(context, ucmd.buf_addr, entries * ucmd.cqe_size,
968 IB_ACCESS_LOCAL_WRITE, 1);
969 if (IS_ERR(umem)) {
970 err = PTR_ERR(umem);
971 return err;
972 }
973
974 mlx5_ib_cont_pages(umem, ucmd.buf_addr, &npages, page_shift,
975 npas, NULL);
976
977 cq->resize_umem = umem;
978 *cqe_size = ucmd.cqe_size;
979
980 return 0;
981}
982
983static void un_resize_user(struct mlx5_ib_cq *cq)
984{
985 ib_umem_release(cq->resize_umem);
986}
987
988static int resize_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
989 int entries, int cqe_size)
990{
991 int err;
992
993 cq->resize_buf = kzalloc(sizeof(*cq->resize_buf), GFP_KERNEL);
994 if (!cq->resize_buf)
995 return -ENOMEM;
996
997 err = alloc_cq_buf(dev, cq->resize_buf, entries, cqe_size);
998 if (err)
999 goto ex;
1000
1001 init_cq_buf(cq, cq->resize_buf);
1002
1003 return 0;
1004
1005ex:
1006 kfree(cq->resize_buf);
1007 return err;
1008}
1009
1010static void un_resize_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq)
1011{
1012 free_cq_buf(dev, cq->resize_buf);
1013 cq->resize_buf = NULL;
1014}
1015
1016static int copy_resize_cqes(struct mlx5_ib_cq *cq)
1017{
1018 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
1019 struct mlx5_cqe64 *scqe64;
1020 struct mlx5_cqe64 *dcqe64;
1021 void *start_cqe;
1022 void *scqe;
1023 void *dcqe;
1024 int ssize;
1025 int dsize;
1026 int i;
1027 u8 sw_own;
1028
1029 ssize = cq->buf.cqe_size;
1030 dsize = cq->resize_buf->cqe_size;
1031 if (ssize != dsize) {
1032 mlx5_ib_warn(dev, "resize from different cqe size is not supported\n");
1033 return -EINVAL;
1034 }
1035
1036 i = cq->mcq.cons_index;
1037 scqe = get_sw_cqe(cq, i);
1038 scqe64 = ssize == 64 ? scqe : scqe + 64;
1039 start_cqe = scqe;
1040 if (!scqe) {
1041 mlx5_ib_warn(dev, "expected cqe in sw ownership\n");
1042 return -EINVAL;
1043 }
1044
1045 while ((scqe64->op_own >> 4) != MLX5_CQE_RESIZE_CQ) {
1046 dcqe = get_cqe_from_buf(cq->resize_buf,
1047 (i + 1) & (cq->resize_buf->nent),
1048 dsize);
1049 dcqe64 = dsize == 64 ? dcqe : dcqe + 64;
1050 sw_own = sw_ownership_bit(i + 1, cq->resize_buf->nent);
1051 memcpy(dcqe, scqe, dsize);
1052 dcqe64->op_own = (dcqe64->op_own & ~MLX5_CQE_OWNER_MASK) | sw_own;
1053
1054 ++i;
1055 scqe = get_sw_cqe(cq, i);
1056 scqe64 = ssize == 64 ? scqe : scqe + 64;
1057 if (!scqe) {
1058 mlx5_ib_warn(dev, "expected cqe in sw ownership\n");
1059 return -EINVAL;
1060 }
1061
1062 if (scqe == start_cqe) {
1063 pr_warn("resize CQ failed to get resize CQE, CQN 0x%x\n",
1064 cq->mcq.cqn);
1065 return -ENOMEM;
1066 }
1067 }
1068 ++cq->mcq.cons_index;
1069 return 0;
1070}
1071
Eli Cohene126ba92013-07-07 17:25:49 +03001072int mlx5_ib_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
1073{
Eli Cohenbde51582014-01-14 17:45:18 +02001074 struct mlx5_ib_dev *dev = to_mdev(ibcq->device);
1075 struct mlx5_ib_cq *cq = to_mcq(ibcq);
1076 struct mlx5_modify_cq_mbox_in *in;
1077 int err;
1078 int npas;
1079 int page_shift;
1080 int inlen;
1081 int uninitialized_var(cqe_size);
1082 unsigned long flags;
1083
Saeed Mahameed938fe832015-05-28 22:28:41 +03001084 if (!MLX5_CAP_GEN(dev->mdev, cq_resize)) {
Eli Cohenbde51582014-01-14 17:45:18 +02001085 pr_info("Firmware does not support resize CQ\n");
1086 return -ENOSYS;
1087 }
1088
1089 if (entries < 1)
1090 return -EINVAL;
1091
1092 entries = roundup_pow_of_two(entries + 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001093 if (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz)) + 1)
Eli Cohenbde51582014-01-14 17:45:18 +02001094 return -EINVAL;
1095
1096 if (entries == ibcq->cqe + 1)
1097 return 0;
1098
1099 mutex_lock(&cq->resize_mutex);
1100 if (udata) {
1101 err = resize_user(dev, cq, entries, udata, &npas, &page_shift,
1102 &cqe_size);
1103 } else {
1104 cqe_size = 64;
1105 err = resize_kernel(dev, cq, entries, cqe_size);
1106 if (!err) {
1107 npas = cq->resize_buf->buf.npages;
1108 page_shift = cq->resize_buf->buf.page_shift;
1109 }
1110 }
1111
1112 if (err)
1113 goto ex;
1114
1115 inlen = sizeof(*in) + npas * sizeof(in->pas[0]);
1116 in = mlx5_vzalloc(inlen);
1117 if (!in) {
1118 err = -ENOMEM;
1119 goto ex_resize;
1120 }
1121
1122 if (udata)
1123 mlx5_ib_populate_pas(dev, cq->resize_umem, page_shift,
1124 in->pas, 0);
1125 else
1126 mlx5_fill_page_array(&cq->resize_buf->buf, in->pas);
1127
1128 in->field_select = cpu_to_be32(MLX5_MODIFY_CQ_MASK_LOG_SIZE |
1129 MLX5_MODIFY_CQ_MASK_PG_OFFSET |
1130 MLX5_MODIFY_CQ_MASK_PG_SIZE);
1131 in->ctx.log_pg_sz = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
1132 in->ctx.cqe_sz_flags = cqe_sz_to_mlx_sz(cqe_size) << 5;
1133 in->ctx.page_offset = 0;
1134 in->ctx.log_sz_usr_page = cpu_to_be32(ilog2(entries) << 24);
1135 in->hdr.opmod = cpu_to_be16(MLX5_CQ_OPMOD_RESIZE);
1136 in->cqn = cpu_to_be32(cq->mcq.cqn);
1137
Jack Morgenstein9603b612014-07-28 23:30:22 +03001138 err = mlx5_core_modify_cq(dev->mdev, &cq->mcq, in, inlen);
Eli Cohenbde51582014-01-14 17:45:18 +02001139 if (err)
1140 goto ex_alloc;
1141
1142 if (udata) {
1143 cq->ibcq.cqe = entries - 1;
1144 ib_umem_release(cq->buf.umem);
1145 cq->buf.umem = cq->resize_umem;
1146 cq->resize_umem = NULL;
1147 } else {
1148 struct mlx5_ib_cq_buf tbuf;
1149 int resized = 0;
1150
1151 spin_lock_irqsave(&cq->lock, flags);
1152 if (cq->resize_buf) {
1153 err = copy_resize_cqes(cq);
1154 if (!err) {
1155 tbuf = cq->buf;
1156 cq->buf = *cq->resize_buf;
1157 kfree(cq->resize_buf);
1158 cq->resize_buf = NULL;
1159 resized = 1;
1160 }
1161 }
1162 cq->ibcq.cqe = entries - 1;
1163 spin_unlock_irqrestore(&cq->lock, flags);
1164 if (resized)
1165 free_cq_buf(dev, &tbuf);
1166 }
1167 mutex_unlock(&cq->resize_mutex);
1168
Al Viro479163f2014-11-20 08:13:57 +00001169 kvfree(in);
Eli Cohenbde51582014-01-14 17:45:18 +02001170 return 0;
1171
1172ex_alloc:
Al Viro479163f2014-11-20 08:13:57 +00001173 kvfree(in);
Eli Cohenbde51582014-01-14 17:45:18 +02001174
1175ex_resize:
1176 if (udata)
1177 un_resize_user(cq);
1178 else
1179 un_resize_kernel(dev, cq);
1180ex:
1181 mutex_unlock(&cq->resize_mutex);
1182 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001183}
1184
1185int mlx5_ib_get_cqe_size(struct mlx5_ib_dev *dev, struct ib_cq *ibcq)
1186{
1187 struct mlx5_ib_cq *cq;
1188
1189 if (!ibcq)
1190 return 128;
1191
1192 cq = to_mcq(ibcq);
1193 return cq->cqe_size;
1194}