blob: 1188fef08450dd5e4696956139965bfe83c9ab7b [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>
Sagi Grimbergb6364012015-09-02 22:23:04 +030036#include <rdma/ib_cache.h>
Eli Cohene126ba92013-07-07 17:25:49 +030037#include "mlx5_ib.h"
Eli Cohene126ba92013-07-07 17:25:49 +030038
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
Sagi Grimberg8a187ee2015-10-13 19:11:26 +0300111 case IB_WR_REG_MR:
112 return IB_WC_REG_MR;
113
Eli Cohene126ba92013-07-07 17:25:49 +0300114 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;
Eli Cohene126ba92013-07-07 17:25:49 +0300156 case MLX5_OPCODE_UMR:
157 wc->opcode = get_umr_comp(wq, idx);
158 break;
159 }
160}
161
162enum {
163 MLX5_GRH_IN_BUFFER = 1,
164 MLX5_GRH_IN_CQE = 2,
165};
166
167static void handle_responder(struct ib_wc *wc, struct mlx5_cqe64 *cqe,
168 struct mlx5_ib_qp *qp)
169{
Achiad Shochatcb34be62015-12-23 18:47:22 +0200170 enum rdma_link_layer ll = rdma_port_get_link_layer(qp->ibqp.device, 1);
Eli Cohene126ba92013-07-07 17:25:49 +0300171 struct mlx5_ib_dev *dev = to_mdev(qp->ibqp.device);
172 struct mlx5_ib_srq *srq;
173 struct mlx5_ib_wq *wq;
174 u16 wqe_ctr;
175 u8 g;
176
177 if (qp->ibqp.srq || qp->ibqp.xrcd) {
178 struct mlx5_core_srq *msrq = NULL;
179
180 if (qp->ibqp.xrcd) {
Jack Morgenstein9603b612014-07-28 23:30:22 +0300181 msrq = mlx5_core_get_srq(dev->mdev,
Eli Cohene126ba92013-07-07 17:25:49 +0300182 be32_to_cpu(cqe->srqn));
183 srq = to_mibsrq(msrq);
184 } else {
185 srq = to_msrq(qp->ibqp.srq);
186 }
187 if (srq) {
188 wqe_ctr = be16_to_cpu(cqe->wqe_counter);
189 wc->wr_id = srq->wrid[wqe_ctr];
190 mlx5_ib_free_srq_wqe(srq, wqe_ctr);
191 if (msrq && atomic_dec_and_test(&msrq->refcount))
192 complete(&msrq->free);
193 }
194 } else {
195 wq = &qp->rq;
196 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
197 ++wq->tail;
198 }
199 wc->byte_len = be32_to_cpu(cqe->byte_cnt);
200
201 switch (cqe->op_own >> 4) {
202 case MLX5_CQE_RESP_WR_IMM:
203 wc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
204 wc->wc_flags = IB_WC_WITH_IMM;
205 wc->ex.imm_data = cqe->imm_inval_pkey;
206 break;
207 case MLX5_CQE_RESP_SEND:
208 wc->opcode = IB_WC_RECV;
Erez Shitritc7ce8332016-02-21 16:27:18 +0200209 wc->wc_flags = IB_WC_IP_CSUM_OK;
210 if (unlikely(!((cqe->hds_ip_ext & CQE_L3_OK) &&
211 (cqe->hds_ip_ext & CQE_L4_OK))))
212 wc->wc_flags = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300213 break;
214 case MLX5_CQE_RESP_SEND_IMM:
215 wc->opcode = IB_WC_RECV;
216 wc->wc_flags = IB_WC_WITH_IMM;
217 wc->ex.imm_data = cqe->imm_inval_pkey;
218 break;
219 case MLX5_CQE_RESP_SEND_INV:
220 wc->opcode = IB_WC_RECV;
221 wc->wc_flags = IB_WC_WITH_INVALIDATE;
222 wc->ex.invalidate_rkey = be32_to_cpu(cqe->imm_inval_pkey);
223 break;
224 }
225 wc->slid = be16_to_cpu(cqe->slid);
226 wc->sl = (be32_to_cpu(cqe->flags_rqpn) >> 24) & 0xf;
227 wc->src_qp = be32_to_cpu(cqe->flags_rqpn) & 0xffffff;
228 wc->dlid_path_bits = cqe->ml_path;
229 g = (be32_to_cpu(cqe->flags_rqpn) >> 28) & 3;
230 wc->wc_flags |= g ? IB_WC_GRH : 0;
Sagi Grimbergb6364012015-09-02 22:23:04 +0300231 if (unlikely(is_qp1(qp->ibqp.qp_type))) {
232 u16 pkey = be32_to_cpu(cqe->imm_inval_pkey) & 0xffff;
233
234 ib_find_cached_pkey(&dev->ib_dev, qp->port, pkey,
235 &wc->pkey_index);
236 } else {
237 wc->pkey_index = 0;
238 }
Achiad Shochatcb34be62015-12-23 18:47:22 +0200239
240 if (ll != IB_LINK_LAYER_ETHERNET)
241 return;
242
243 switch (wc->sl & 0x3) {
244 case MLX5_CQE_ROCE_L3_HEADER_TYPE_GRH:
245 wc->network_hdr_type = RDMA_NETWORK_IB;
246 break;
247 case MLX5_CQE_ROCE_L3_HEADER_TYPE_IPV6:
248 wc->network_hdr_type = RDMA_NETWORK_IPV6;
249 break;
250 case MLX5_CQE_ROCE_L3_HEADER_TYPE_IPV4:
251 wc->network_hdr_type = RDMA_NETWORK_IPV4;
252 break;
253 }
254 wc->wc_flags |= IB_WC_WITH_NETWORK_HDR_TYPE;
Eli Cohene126ba92013-07-07 17:25:49 +0300255}
256
257static void dump_cqe(struct mlx5_ib_dev *dev, struct mlx5_err_cqe *cqe)
258{
259 __be32 *p = (__be32 *)cqe;
260 int i;
261
262 mlx5_ib_warn(dev, "dump error cqe\n");
263 for (i = 0; i < sizeof(*cqe) / 16; i++, p += 4)
264 pr_info("%08x %08x %08x %08x\n", be32_to_cpu(p[0]),
265 be32_to_cpu(p[1]), be32_to_cpu(p[2]),
266 be32_to_cpu(p[3]));
267}
268
269static void mlx5_handle_error_cqe(struct mlx5_ib_dev *dev,
270 struct mlx5_err_cqe *cqe,
271 struct ib_wc *wc)
272{
273 int dump = 1;
274
275 switch (cqe->syndrome) {
276 case MLX5_CQE_SYNDROME_LOCAL_LENGTH_ERR:
277 wc->status = IB_WC_LOC_LEN_ERR;
278 break;
279 case MLX5_CQE_SYNDROME_LOCAL_QP_OP_ERR:
280 wc->status = IB_WC_LOC_QP_OP_ERR;
281 break;
282 case MLX5_CQE_SYNDROME_LOCAL_PROT_ERR:
283 wc->status = IB_WC_LOC_PROT_ERR;
284 break;
285 case MLX5_CQE_SYNDROME_WR_FLUSH_ERR:
286 dump = 0;
287 wc->status = IB_WC_WR_FLUSH_ERR;
288 break;
289 case MLX5_CQE_SYNDROME_MW_BIND_ERR:
290 wc->status = IB_WC_MW_BIND_ERR;
291 break;
292 case MLX5_CQE_SYNDROME_BAD_RESP_ERR:
293 wc->status = IB_WC_BAD_RESP_ERR;
294 break;
295 case MLX5_CQE_SYNDROME_LOCAL_ACCESS_ERR:
296 wc->status = IB_WC_LOC_ACCESS_ERR;
297 break;
298 case MLX5_CQE_SYNDROME_REMOTE_INVAL_REQ_ERR:
299 wc->status = IB_WC_REM_INV_REQ_ERR;
300 break;
301 case MLX5_CQE_SYNDROME_REMOTE_ACCESS_ERR:
302 wc->status = IB_WC_REM_ACCESS_ERR;
303 break;
304 case MLX5_CQE_SYNDROME_REMOTE_OP_ERR:
305 wc->status = IB_WC_REM_OP_ERR;
306 break;
307 case MLX5_CQE_SYNDROME_TRANSPORT_RETRY_EXC_ERR:
308 wc->status = IB_WC_RETRY_EXC_ERR;
309 dump = 0;
310 break;
311 case MLX5_CQE_SYNDROME_RNR_RETRY_EXC_ERR:
312 wc->status = IB_WC_RNR_RETRY_EXC_ERR;
313 dump = 0;
314 break;
315 case MLX5_CQE_SYNDROME_REMOTE_ABORTED_ERR:
316 wc->status = IB_WC_REM_ABORT_ERR;
317 break;
318 default:
319 wc->status = IB_WC_GENERAL_ERR;
320 break;
321 }
322
323 wc->vendor_err = cqe->vendor_err_synd;
324 if (dump)
325 dump_cqe(dev, cqe);
326}
327
328static int is_atomic_response(struct mlx5_ib_qp *qp, uint16_t idx)
329{
330 /* TBD: waiting decision
331 */
332 return 0;
333}
334
335static void *mlx5_get_atomic_laddr(struct mlx5_ib_qp *qp, uint16_t idx)
336{
337 struct mlx5_wqe_data_seg *dpseg;
338 void *addr;
339
340 dpseg = mlx5_get_send_wqe(qp, idx) + sizeof(struct mlx5_wqe_ctrl_seg) +
341 sizeof(struct mlx5_wqe_raddr_seg) +
342 sizeof(struct mlx5_wqe_atomic_seg);
343 addr = (void *)(unsigned long)be64_to_cpu(dpseg->addr);
344 return addr;
345}
346
347static void handle_atomic(struct mlx5_ib_qp *qp, struct mlx5_cqe64 *cqe64,
348 uint16_t idx)
349{
350 void *addr;
351 int byte_count;
352 int i;
353
354 if (!is_atomic_response(qp, idx))
355 return;
356
357 byte_count = be32_to_cpu(cqe64->byte_cnt);
358 addr = mlx5_get_atomic_laddr(qp, idx);
359
360 if (byte_count == 4) {
361 *(uint32_t *)addr = be32_to_cpu(*((__be32 *)addr));
362 } else {
363 for (i = 0; i < byte_count; i += 8) {
364 *(uint64_t *)addr = be64_to_cpu(*((__be64 *)addr));
365 addr += 8;
366 }
367 }
368
369 return;
370}
371
372static void handle_atomics(struct mlx5_ib_qp *qp, struct mlx5_cqe64 *cqe64,
373 u16 tail, u16 head)
374{
Jack Morgensteinf241e742014-07-28 23:30:23 +0300375 u16 idx;
Eli Cohene126ba92013-07-07 17:25:49 +0300376
377 do {
378 idx = tail & (qp->sq.wqe_cnt - 1);
379 handle_atomic(qp, cqe64, idx);
380 if (idx == head)
381 break;
382
383 tail = qp->sq.w_list[idx].next;
384 } while (1);
385 tail = qp->sq.w_list[idx].next;
386 qp->sq.last_poll = tail;
387}
388
Eli Cohenbde51582014-01-14 17:45:18 +0200389static void free_cq_buf(struct mlx5_ib_dev *dev, struct mlx5_ib_cq_buf *buf)
390{
Jack Morgenstein9603b612014-07-28 23:30:22 +0300391 mlx5_buf_free(dev->mdev, &buf->buf);
Eli Cohenbde51582014-01-14 17:45:18 +0200392}
393
Sagi Grimbergd5436ba2014-02-23 14:19:12 +0200394static void get_sig_err_item(struct mlx5_sig_err_cqe *cqe,
395 struct ib_sig_err *item)
396{
397 u16 syndrome = be16_to_cpu(cqe->syndrome);
398
399#define GUARD_ERR (1 << 13)
400#define APPTAG_ERR (1 << 12)
401#define REFTAG_ERR (1 << 11)
402
403 if (syndrome & GUARD_ERR) {
404 item->err_type = IB_SIG_BAD_GUARD;
405 item->expected = be32_to_cpu(cqe->expected_trans_sig) >> 16;
406 item->actual = be32_to_cpu(cqe->actual_trans_sig) >> 16;
407 } else
408 if (syndrome & REFTAG_ERR) {
409 item->err_type = IB_SIG_BAD_REFTAG;
410 item->expected = be32_to_cpu(cqe->expected_reftag);
411 item->actual = be32_to_cpu(cqe->actual_reftag);
412 } else
413 if (syndrome & APPTAG_ERR) {
414 item->err_type = IB_SIG_BAD_APPTAG;
415 item->expected = be32_to_cpu(cqe->expected_trans_sig) & 0xffff;
416 item->actual = be32_to_cpu(cqe->actual_trans_sig) & 0xffff;
417 } else {
418 pr_err("Got signature completion error with bad syndrome %04x\n",
419 syndrome);
420 }
421
422 item->sig_err_offset = be64_to_cpu(cqe->err_offset);
423 item->key = be32_to_cpu(cqe->mkey);
424}
425
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300426static void sw_send_comp(struct mlx5_ib_qp *qp, int num_entries,
427 struct ib_wc *wc, int *npolled)
428{
429 struct mlx5_ib_wq *wq;
430 unsigned int cur;
431 unsigned int idx;
432 int np;
433 int i;
434
435 wq = &qp->sq;
436 cur = wq->head - wq->tail;
437 np = *npolled;
438
439 if (cur == 0)
440 return;
441
442 for (i = 0; i < cur && np < num_entries; i++) {
443 idx = wq->last_poll & (wq->wqe_cnt - 1);
444 wc->wr_id = wq->wrid[idx];
445 wc->status = IB_WC_WR_FLUSH_ERR;
446 wc->vendor_err = MLX5_CQE_SYNDROME_WR_FLUSH_ERR;
447 wq->tail++;
448 np++;
449 wc->qp = &qp->ibqp;
450 wc++;
451 wq->last_poll = wq->w_list[idx].next;
452 }
453 *npolled = np;
454}
455
456static void sw_recv_comp(struct mlx5_ib_qp *qp, int num_entries,
457 struct ib_wc *wc, int *npolled)
458{
459 struct mlx5_ib_wq *wq;
460 unsigned int cur;
461 int np;
462 int i;
463
464 wq = &qp->rq;
465 cur = wq->head - wq->tail;
466 np = *npolled;
467
468 if (cur == 0)
469 return;
470
471 for (i = 0; i < cur && np < num_entries; i++) {
472 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
473 wc->status = IB_WC_WR_FLUSH_ERR;
474 wc->vendor_err = MLX5_CQE_SYNDROME_WR_FLUSH_ERR;
475 wq->tail++;
476 np++;
477 wc->qp = &qp->ibqp;
478 wc++;
479 }
480 *npolled = np;
481}
482
483static void mlx5_ib_poll_sw_comp(struct mlx5_ib_cq *cq, int num_entries,
484 struct ib_wc *wc, int *npolled)
485{
486 struct mlx5_ib_qp *qp;
487
488 *npolled = 0;
489 /* Find uncompleted WQEs belonging to that cq and retrun mmics ones */
490 list_for_each_entry(qp, &cq->list_send_qp, cq_send_list) {
491 sw_send_comp(qp, num_entries, wc + *npolled, npolled);
492 if (*npolled >= num_entries)
493 return;
494 }
495
496 list_for_each_entry(qp, &cq->list_recv_qp, cq_recv_list) {
497 sw_recv_comp(qp, num_entries, wc + *npolled, npolled);
498 if (*npolled >= num_entries)
499 return;
500 }
501}
502
Eli Cohene126ba92013-07-07 17:25:49 +0300503static int mlx5_poll_one(struct mlx5_ib_cq *cq,
504 struct mlx5_ib_qp **cur_qp,
505 struct ib_wc *wc)
506{
507 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
508 struct mlx5_err_cqe *err_cqe;
509 struct mlx5_cqe64 *cqe64;
510 struct mlx5_core_qp *mqp;
511 struct mlx5_ib_wq *wq;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +0200512 struct mlx5_sig_err_cqe *sig_err_cqe;
Matan Baraka606b0f2016-02-29 18:05:28 +0200513 struct mlx5_core_mkey *mmkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +0200514 struct mlx5_ib_mr *mr;
Eli Cohene126ba92013-07-07 17:25:49 +0300515 uint8_t opcode;
516 uint32_t qpn;
517 u16 wqe_ctr;
518 void *cqe;
519 int idx;
520
Eli Cohenbde51582014-01-14 17:45:18 +0200521repoll:
Eli Cohene126ba92013-07-07 17:25:49 +0300522 cqe = next_cqe_sw(cq);
523 if (!cqe)
524 return -EAGAIN;
525
526 cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
527
528 ++cq->mcq.cons_index;
529
530 /* Make sure we read CQ entry contents after we've checked the
531 * ownership bit.
532 */
533 rmb();
534
Eli Cohenbde51582014-01-14 17:45:18 +0200535 opcode = cqe64->op_own >> 4;
536 if (unlikely(opcode == MLX5_CQE_RESIZE_CQ)) {
537 if (likely(cq->resize_buf)) {
538 free_cq_buf(dev, &cq->buf);
539 cq->buf = *cq->resize_buf;
540 kfree(cq->resize_buf);
541 cq->resize_buf = NULL;
542 goto repoll;
543 } else {
544 mlx5_ib_warn(dev, "unexpected resize cqe\n");
545 }
546 }
Eli Cohene126ba92013-07-07 17:25:49 +0300547
548 qpn = ntohl(cqe64->sop_drop_qpn) & 0xffffff;
549 if (!*cur_qp || (qpn != (*cur_qp)->ibqp.qp_num)) {
550 /* We do not have to take the QP table lock here,
551 * because CQs will be locked while QPs are removed
552 * from the table.
553 */
Jack Morgenstein9603b612014-07-28 23:30:22 +0300554 mqp = __mlx5_qp_lookup(dev->mdev, qpn);
Eli Cohene126ba92013-07-07 17:25:49 +0300555 if (unlikely(!mqp)) {
556 mlx5_ib_warn(dev, "CQE@CQ %06x for unknown QPN %6x\n",
557 cq->mcq.cqn, qpn);
558 return -EINVAL;
559 }
560
561 *cur_qp = to_mibqp(mqp);
562 }
563
564 wc->qp = &(*cur_qp)->ibqp;
Eli Cohene126ba92013-07-07 17:25:49 +0300565 switch (opcode) {
566 case MLX5_CQE_REQ:
567 wq = &(*cur_qp)->sq;
568 wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
569 idx = wqe_ctr & (wq->wqe_cnt - 1);
570 handle_good_req(wc, cqe64, wq, idx);
571 handle_atomics(*cur_qp, cqe64, wq->last_poll, idx);
572 wc->wr_id = wq->wrid[idx];
573 wq->tail = wq->wqe_head[idx] + 1;
574 wc->status = IB_WC_SUCCESS;
575 break;
576 case MLX5_CQE_RESP_WR_IMM:
577 case MLX5_CQE_RESP_SEND:
578 case MLX5_CQE_RESP_SEND_IMM:
579 case MLX5_CQE_RESP_SEND_INV:
580 handle_responder(wc, cqe64, *cur_qp);
581 wc->status = IB_WC_SUCCESS;
582 break;
583 case MLX5_CQE_RESIZE_CQ:
584 break;
585 case MLX5_CQE_REQ_ERR:
586 case MLX5_CQE_RESP_ERR:
587 err_cqe = (struct mlx5_err_cqe *)cqe64;
588 mlx5_handle_error_cqe(dev, err_cqe, wc);
589 mlx5_ib_dbg(dev, "%s error cqe on cqn 0x%x:\n",
590 opcode == MLX5_CQE_REQ_ERR ?
591 "Requestor" : "Responder", cq->mcq.cqn);
592 mlx5_ib_dbg(dev, "syndrome 0x%x, vendor syndrome 0x%x\n",
593 err_cqe->syndrome, err_cqe->vendor_err_synd);
594 if (opcode == MLX5_CQE_REQ_ERR) {
595 wq = &(*cur_qp)->sq;
596 wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
597 idx = wqe_ctr & (wq->wqe_cnt - 1);
598 wc->wr_id = wq->wrid[idx];
599 wq->tail = wq->wqe_head[idx] + 1;
600 } else {
601 struct mlx5_ib_srq *srq;
602
603 if ((*cur_qp)->ibqp.srq) {
604 srq = to_msrq((*cur_qp)->ibqp.srq);
605 wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
606 wc->wr_id = srq->wrid[wqe_ctr];
607 mlx5_ib_free_srq_wqe(srq, wqe_ctr);
608 } else {
609 wq = &(*cur_qp)->rq;
610 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
611 ++wq->tail;
612 }
613 }
614 break;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +0200615 case MLX5_CQE_SIG_ERR:
616 sig_err_cqe = (struct mlx5_sig_err_cqe *)cqe64;
617
Matan Baraka606b0f2016-02-29 18:05:28 +0200618 read_lock(&dev->mdev->priv.mkey_table.lock);
619 mmkey = __mlx5_mr_lookup(dev->mdev,
620 mlx5_base_mkey(be32_to_cpu(sig_err_cqe->mkey)));
621 if (unlikely(!mmkey)) {
622 read_unlock(&dev->mdev->priv.mkey_table.lock);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +0200623 mlx5_ib_warn(dev, "CQE@CQ %06x for unknown MR %6x\n",
624 cq->mcq.cqn, be32_to_cpu(sig_err_cqe->mkey));
625 return -EINVAL;
626 }
627
Matan Baraka606b0f2016-02-29 18:05:28 +0200628 mr = to_mibmr(mmkey);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +0200629 get_sig_err_item(sig_err_cqe, &mr->sig->err_item);
630 mr->sig->sig_err_exists = true;
631 mr->sig->sigerr_count++;
632
633 mlx5_ib_warn(dev, "CQN: 0x%x Got SIGERR on key: 0x%x err_type %x err_offset %llx expected %x actual %x\n",
634 cq->mcq.cqn, mr->sig->err_item.key,
635 mr->sig->err_item.err_type,
636 mr->sig->err_item.sig_err_offset,
637 mr->sig->err_item.expected,
638 mr->sig->err_item.actual);
639
Matan Baraka606b0f2016-02-29 18:05:28 +0200640 read_unlock(&dev->mdev->priv.mkey_table.lock);
Sagi Grimbergd5436ba2014-02-23 14:19:12 +0200641 goto repoll;
Eli Cohene126ba92013-07-07 17:25:49 +0300642 }
643
644 return 0;
645}
646
Haggai Eran25361e02016-02-29 15:45:08 +0200647static int poll_soft_wc(struct mlx5_ib_cq *cq, int num_entries,
648 struct ib_wc *wc)
649{
650 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
651 struct mlx5_ib_wc *soft_wc, *next;
652 int npolled = 0;
653
654 list_for_each_entry_safe(soft_wc, next, &cq->wc_list, list) {
655 if (npolled >= num_entries)
656 break;
657
658 mlx5_ib_dbg(dev, "polled software generated completion on CQ 0x%x\n",
659 cq->mcq.cqn);
660
661 wc[npolled++] = soft_wc->wc;
662 list_del(&soft_wc->list);
663 kfree(soft_wc);
664 }
665
666 return npolled;
667}
668
Eli Cohene126ba92013-07-07 17:25:49 +0300669int mlx5_ib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
670{
671 struct mlx5_ib_cq *cq = to_mcq(ibcq);
672 struct mlx5_ib_qp *cur_qp = NULL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300673 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
674 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300675 unsigned long flags;
Haggai Eran25361e02016-02-29 15:45:08 +0200676 int soft_polled = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300677 int npolled;
678 int err = 0;
679
680 spin_lock_irqsave(&cq->lock, flags);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300681 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
682 mlx5_ib_poll_sw_comp(cq, num_entries, wc, &npolled);
683 goto out;
684 }
Eli Cohene126ba92013-07-07 17:25:49 +0300685
Haggai Eran25361e02016-02-29 15:45:08 +0200686 if (unlikely(!list_empty(&cq->wc_list)))
687 soft_polled = poll_soft_wc(cq, num_entries, wc);
688
689 for (npolled = 0; npolled < num_entries - soft_polled; npolled++) {
690 err = mlx5_poll_one(cq, &cur_qp, wc + soft_polled + npolled);
Eli Cohene126ba92013-07-07 17:25:49 +0300691 if (err)
692 break;
693 }
694
695 if (npolled)
696 mlx5_cq_set_ci(&cq->mcq);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300697out:
Eli Cohene126ba92013-07-07 17:25:49 +0300698 spin_unlock_irqrestore(&cq->lock, flags);
699
700 if (err == 0 || err == -EAGAIN)
Haggai Eran25361e02016-02-29 15:45:08 +0200701 return soft_polled + npolled;
Eli Cohene126ba92013-07-07 17:25:49 +0300702 else
703 return err;
704}
705
706int mlx5_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
707{
Saeed Mahameedce0f7502015-04-02 17:07:33 +0300708 struct mlx5_core_dev *mdev = to_mdev(ibcq->device)->mdev;
Haggai Eran25361e02016-02-29 15:45:08 +0200709 struct mlx5_ib_cq *cq = to_mcq(ibcq);
Saeed Mahameedce0f7502015-04-02 17:07:33 +0300710 void __iomem *uar_page = mdev->priv.uuari.uars[0].map;
Haggai Eran25361e02016-02-29 15:45:08 +0200711 unsigned long irq_flags;
712 int ret = 0;
Saeed Mahameedce0f7502015-04-02 17:07:33 +0300713
Haggai Eran25361e02016-02-29 15:45:08 +0200714 spin_lock_irqsave(&cq->lock, irq_flags);
715 if (cq->notify_flags != IB_CQ_NEXT_COMP)
716 cq->notify_flags = flags & IB_CQ_SOLICITED_MASK;
717
718 if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !list_empty(&cq->wc_list))
719 ret = 1;
720 spin_unlock_irqrestore(&cq->lock, irq_flags);
721
722 mlx5_cq_arm(&cq->mcq,
Eli Cohene126ba92013-07-07 17:25:49 +0300723 (flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?
724 MLX5_CQ_DB_REQ_NOT_SOL : MLX5_CQ_DB_REQ_NOT,
Saeed Mahameedce0f7502015-04-02 17:07:33 +0300725 uar_page,
726 MLX5_GET_DOORBELL_LOCK(&mdev->priv.cq_uar_lock),
727 to_mcq(ibcq)->mcq.cons_index);
Eli Cohene126ba92013-07-07 17:25:49 +0300728
Haggai Eran25361e02016-02-29 15:45:08 +0200729 return ret;
Eli Cohene126ba92013-07-07 17:25:49 +0300730}
731
732static int alloc_cq_buf(struct mlx5_ib_dev *dev, struct mlx5_ib_cq_buf *buf,
733 int nent, int cqe_size)
734{
735 int err;
736
Amir Vadai64ffaa22015-05-28 22:28:38 +0300737 err = mlx5_buf_alloc(dev->mdev, nent * cqe_size, &buf->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300738 if (err)
739 return err;
740
741 buf->cqe_size = cqe_size;
Eli Cohenbde51582014-01-14 17:45:18 +0200742 buf->nent = nent;
Eli Cohene126ba92013-07-07 17:25:49 +0300743
744 return 0;
745}
746
Eli Cohene126ba92013-07-07 17:25:49 +0300747static int create_cq_user(struct mlx5_ib_dev *dev, struct ib_udata *udata,
748 struct ib_ucontext *context, struct mlx5_ib_cq *cq,
Saeed Mahameed27827782016-07-16 02:33:22 +0300749 int entries, u32 **cqb,
Eli Cohene126ba92013-07-07 17:25:49 +0300750 int *cqe_size, int *index, int *inlen)
751{
752 struct mlx5_ib_create_cq ucmd;
Yann Droneauda8237b32014-05-05 19:33:21 +0200753 size_t ucmdlen;
Eli Cohene126ba92013-07-07 17:25:49 +0300754 int page_shift;
Saeed Mahameed27827782016-07-16 02:33:22 +0300755 __be64 *pas;
Eli Cohene126ba92013-07-07 17:25:49 +0300756 int npages;
757 int ncont;
Saeed Mahameed27827782016-07-16 02:33:22 +0300758 void *cqc;
Eli Cohene126ba92013-07-07 17:25:49 +0300759 int err;
760
Yann Droneauda8237b32014-05-05 19:33:21 +0200761 ucmdlen =
762 (udata->inlen - sizeof(struct ib_uverbs_cmd_hdr) <
763 sizeof(ucmd)) ? (sizeof(ucmd) -
764 sizeof(ucmd.reserved)) : sizeof(ucmd);
765
766 if (ib_copy_from_udata(&ucmd, udata, ucmdlen))
Eli Cohene126ba92013-07-07 17:25:49 +0300767 return -EFAULT;
768
Yann Droneauda8237b32014-05-05 19:33:21 +0200769 if (ucmdlen == sizeof(ucmd) &&
770 ucmd.reserved != 0)
771 return -EINVAL;
772
Eli Cohene126ba92013-07-07 17:25:49 +0300773 if (ucmd.cqe_size != 64 && ucmd.cqe_size != 128)
774 return -EINVAL;
775
776 *cqe_size = ucmd.cqe_size;
777
778 cq->buf.umem = ib_umem_get(context, ucmd.buf_addr,
779 entries * ucmd.cqe_size,
780 IB_ACCESS_LOCAL_WRITE, 1);
781 if (IS_ERR(cq->buf.umem)) {
782 err = PTR_ERR(cq->buf.umem);
783 return err;
784 }
785
786 err = mlx5_ib_db_map_user(to_mucontext(context), ucmd.db_addr,
787 &cq->db);
788 if (err)
789 goto err_umem;
790
791 mlx5_ib_cont_pages(cq->buf.umem, ucmd.buf_addr, &npages, &page_shift,
792 &ncont, NULL);
793 mlx5_ib_dbg(dev, "addr 0x%llx, size %u, npages %d, page_shift %d, ncont %d\n",
794 ucmd.buf_addr, entries * ucmd.cqe_size, npages, page_shift, ncont);
795
Saeed Mahameed27827782016-07-16 02:33:22 +0300796 *inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
797 MLX5_FLD_SZ_BYTES(create_cq_in, pas[0]) * ncont;
Eli Cohene126ba92013-07-07 17:25:49 +0300798 *cqb = mlx5_vzalloc(*inlen);
799 if (!*cqb) {
800 err = -ENOMEM;
801 goto err_db;
802 }
Saeed Mahameed27827782016-07-16 02:33:22 +0300803
804 pas = (__be64 *)MLX5_ADDR_OF(create_cq_in, *cqb, pas);
805 mlx5_ib_populate_pas(dev, cq->buf.umem, page_shift, pas, 0);
806
807 cqc = MLX5_ADDR_OF(create_cq_in, *cqb, cq_context);
808 MLX5_SET(cqc, cqc, log_page_size,
809 page_shift - MLX5_ADAPTER_PAGE_SHIFT);
Eli Cohene126ba92013-07-07 17:25:49 +0300810
811 *index = to_mucontext(context)->uuari.uars[0].index;
812
813 return 0;
814
815err_db:
816 mlx5_ib_db_unmap_user(to_mucontext(context), &cq->db);
817
818err_umem:
819 ib_umem_release(cq->buf.umem);
820 return err;
821}
822
823static void destroy_cq_user(struct mlx5_ib_cq *cq, struct ib_ucontext *context)
824{
825 mlx5_ib_db_unmap_user(to_mucontext(context), &cq->db);
826 ib_umem_release(cq->buf.umem);
827}
828
Eli Cohenbde51582014-01-14 17:45:18 +0200829static void init_cq_buf(struct mlx5_ib_cq *cq, struct mlx5_ib_cq_buf *buf)
Eli Cohene126ba92013-07-07 17:25:49 +0300830{
831 int i;
832 void *cqe;
833 struct mlx5_cqe64 *cqe64;
834
Eli Cohenbde51582014-01-14 17:45:18 +0200835 for (i = 0; i < buf->nent; i++) {
836 cqe = get_cqe_from_buf(buf, i, buf->cqe_size);
837 cqe64 = buf->cqe_size == 64 ? cqe : cqe + 64;
838 cqe64->op_own = MLX5_CQE_INVALID << 4;
Eli Cohene126ba92013-07-07 17:25:49 +0300839 }
840}
841
842static int create_cq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
843 int entries, int cqe_size,
Saeed Mahameed27827782016-07-16 02:33:22 +0300844 u32 **cqb, int *index, int *inlen)
Eli Cohene126ba92013-07-07 17:25:49 +0300845{
Saeed Mahameed27827782016-07-16 02:33:22 +0300846 __be64 *pas;
847 void *cqc;
Eli Cohene126ba92013-07-07 17:25:49 +0300848 int err;
849
Jack Morgenstein9603b612014-07-28 23:30:22 +0300850 err = mlx5_db_alloc(dev->mdev, &cq->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300851 if (err)
852 return err;
853
854 cq->mcq.set_ci_db = cq->db.db;
855 cq->mcq.arm_db = cq->db.db + 1;
Eli Cohene126ba92013-07-07 17:25:49 +0300856 cq->mcq.cqe_sz = cqe_size;
857
858 err = alloc_cq_buf(dev, &cq->buf, entries, cqe_size);
859 if (err)
860 goto err_db;
861
Eli Cohenbde51582014-01-14 17:45:18 +0200862 init_cq_buf(cq, &cq->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300863
Saeed Mahameed27827782016-07-16 02:33:22 +0300864 *inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
865 MLX5_FLD_SZ_BYTES(create_cq_in, pas[0]) * cq->buf.buf.npages;
Eli Cohene126ba92013-07-07 17:25:49 +0300866 *cqb = mlx5_vzalloc(*inlen);
867 if (!*cqb) {
868 err = -ENOMEM;
869 goto err_buf;
870 }
Eli Cohene126ba92013-07-07 17:25:49 +0300871
Saeed Mahameed27827782016-07-16 02:33:22 +0300872 pas = (__be64 *)MLX5_ADDR_OF(create_cq_in, *cqb, pas);
873 mlx5_fill_page_array(&cq->buf.buf, pas);
874
875 cqc = MLX5_ADDR_OF(create_cq_in, *cqb, cq_context);
876 MLX5_SET(cqc, cqc, log_page_size,
877 cq->buf.buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
878
Jack Morgenstein9603b612014-07-28 23:30:22 +0300879 *index = dev->mdev->priv.uuari.uars[0].index;
Eli Cohene126ba92013-07-07 17:25:49 +0300880
881 return 0;
882
883err_buf:
884 free_cq_buf(dev, &cq->buf);
885
886err_db:
Jack Morgenstein9603b612014-07-28 23:30:22 +0300887 mlx5_db_free(dev->mdev, &cq->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300888 return err;
889}
890
891static void destroy_cq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq)
892{
893 free_cq_buf(dev, &cq->buf);
Jack Morgenstein9603b612014-07-28 23:30:22 +0300894 mlx5_db_free(dev->mdev, &cq->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300895}
896
Haggai Eran25361e02016-02-29 15:45:08 +0200897static void notify_soft_wc_handler(struct work_struct *work)
898{
899 struct mlx5_ib_cq *cq = container_of(work, struct mlx5_ib_cq,
900 notify_work);
901
902 cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
903}
904
Matan Barakbcf4c1e2015-06-11 16:35:20 +0300905struct ib_cq *mlx5_ib_create_cq(struct ib_device *ibdev,
906 const struct ib_cq_init_attr *attr,
907 struct ib_ucontext *context,
Eli Cohene126ba92013-07-07 17:25:49 +0300908 struct ib_udata *udata)
909{
Matan Barakbcf4c1e2015-06-11 16:35:20 +0300910 int entries = attr->cqe;
911 int vector = attr->comp_vector;
Eli Cohene126ba92013-07-07 17:25:49 +0300912 struct mlx5_ib_dev *dev = to_mdev(ibdev);
913 struct mlx5_ib_cq *cq;
914 int uninitialized_var(index);
915 int uninitialized_var(inlen);
Saeed Mahameed27827782016-07-16 02:33:22 +0300916 u32 *cqb = NULL;
917 void *cqc;
Eli Cohene126ba92013-07-07 17:25:49 +0300918 int cqe_size;
Doron Tsur0b6e26c2016-01-17 11:25:47 +0200919 unsigned int irqn;
Eli Cohene126ba92013-07-07 17:25:49 +0300920 int eqn;
921 int err;
922
Noa Osherovich9ea57852016-06-04 15:15:34 +0300923 if (entries < 0 ||
924 (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz))))
Eli Cohen51ee86a2013-10-23 09:53:13 +0300925 return ERR_PTR(-EINVAL);
926
Leon Romanovsky34356f62015-12-29 17:01:30 +0200927 if (check_cq_create_flags(attr->flags))
Matan Barak972ecb82015-12-15 20:30:09 +0200928 return ERR_PTR(-EOPNOTSUPP);
929
Eli Cohene126ba92013-07-07 17:25:49 +0300930 entries = roundup_pow_of_two(entries + 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300931 if (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz)))
Eli Cohene126ba92013-07-07 17:25:49 +0300932 return ERR_PTR(-EINVAL);
933
934 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
935 if (!cq)
936 return ERR_PTR(-ENOMEM);
937
938 cq->ibcq.cqe = entries - 1;
939 mutex_init(&cq->resize_mutex);
940 spin_lock_init(&cq->lock);
941 cq->resize_buf = NULL;
942 cq->resize_umem = NULL;
Leon Romanovsky051f2632015-12-20 12:16:11 +0200943 cq->create_flags = attr->flags;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300944 INIT_LIST_HEAD(&cq->list_send_qp);
945 INIT_LIST_HEAD(&cq->list_recv_qp);
Eli Cohene126ba92013-07-07 17:25:49 +0300946
947 if (context) {
948 err = create_cq_user(dev, udata, context, cq, entries,
949 &cqb, &cqe_size, &index, &inlen);
950 if (err)
951 goto err_create;
952 } else {
953 /* for now choose 64 bytes till we have a proper interface */
954 cqe_size = 64;
955 err = create_cq_kernel(dev, cq, entries, cqe_size, &cqb,
956 &index, &inlen);
957 if (err)
958 goto err_create;
Haggai Eran25361e02016-02-29 15:45:08 +0200959
960 INIT_WORK(&cq->notify_work, notify_soft_wc_handler);
Eli Cohene126ba92013-07-07 17:25:49 +0300961 }
962
Saeed Mahameed233d05d2015-04-02 17:07:32 +0300963 err = mlx5_vector2eqn(dev->mdev, vector, &eqn, &irqn);
Eli Cohene126ba92013-07-07 17:25:49 +0300964 if (err)
965 goto err_cqb;
966
Saeed Mahameed27827782016-07-16 02:33:22 +0300967 cq->cqe_size = cqe_size;
968
969 cqc = MLX5_ADDR_OF(create_cq_in, cqb, cq_context);
970 MLX5_SET(cqc, cqc, cqe_sz, cqe_sz_to_mlx_sz(cqe_size));
971 MLX5_SET(cqc, cqc, log_cq_size, ilog2(entries));
972 MLX5_SET(cqc, cqc, uar_page, index);
973 MLX5_SET(cqc, cqc, c_eqn, eqn);
974 MLX5_SET64(cqc, cqc, dbr_addr, cq->db.dma);
975 if (cq->create_flags & IB_CQ_FLAGS_IGNORE_OVERRUN)
976 MLX5_SET(cqc, cqc, oi, 1);
Eli Cohene126ba92013-07-07 17:25:49 +0300977
Jack Morgenstein9603b612014-07-28 23:30:22 +0300978 err = mlx5_core_create_cq(dev->mdev, &cq->mcq, cqb, inlen);
Eli Cohene126ba92013-07-07 17:25:49 +0300979 if (err)
980 goto err_cqb;
981
982 mlx5_ib_dbg(dev, "cqn 0x%x\n", cq->mcq.cqn);
983 cq->mcq.irqn = irqn;
Matan Barakc16d2752016-04-17 17:08:41 +0300984 if (context)
985 cq->mcq.tasklet_ctx.comp = mlx5_ib_cq_comp;
986 else
987 cq->mcq.comp = mlx5_ib_cq_comp;
Eli Cohene126ba92013-07-07 17:25:49 +0300988 cq->mcq.event = mlx5_ib_cq_event;
989
Haggai Eran25361e02016-02-29 15:45:08 +0200990 INIT_LIST_HEAD(&cq->wc_list);
991
Eli Cohene126ba92013-07-07 17:25:49 +0300992 if (context)
993 if (ib_copy_to_udata(udata, &cq->mcq.cqn, sizeof(__u32))) {
994 err = -EFAULT;
995 goto err_cmd;
996 }
997
998
Al Viro479163f2014-11-20 08:13:57 +0000999 kvfree(cqb);
Eli Cohene126ba92013-07-07 17:25:49 +03001000 return &cq->ibcq;
1001
1002err_cmd:
Jack Morgenstein9603b612014-07-28 23:30:22 +03001003 mlx5_core_destroy_cq(dev->mdev, &cq->mcq);
Eli Cohene126ba92013-07-07 17:25:49 +03001004
1005err_cqb:
Al Viro479163f2014-11-20 08:13:57 +00001006 kvfree(cqb);
Eli Cohene126ba92013-07-07 17:25:49 +03001007 if (context)
1008 destroy_cq_user(cq, context);
1009 else
1010 destroy_cq_kernel(dev, cq);
1011
1012err_create:
1013 kfree(cq);
1014
1015 return ERR_PTR(err);
1016}
1017
1018
1019int mlx5_ib_destroy_cq(struct ib_cq *cq)
1020{
1021 struct mlx5_ib_dev *dev = to_mdev(cq->device);
1022 struct mlx5_ib_cq *mcq = to_mcq(cq);
1023 struct ib_ucontext *context = NULL;
1024
1025 if (cq->uobject)
1026 context = cq->uobject->context;
1027
Jack Morgenstein9603b612014-07-28 23:30:22 +03001028 mlx5_core_destroy_cq(dev->mdev, &mcq->mcq);
Eli Cohene126ba92013-07-07 17:25:49 +03001029 if (context)
1030 destroy_cq_user(mcq, context);
1031 else
1032 destroy_cq_kernel(dev, mcq);
1033
1034 kfree(mcq);
1035
1036 return 0;
1037}
1038
Moshe Lazercfd8f1d2013-10-23 09:53:17 +03001039static int is_equal_rsn(struct mlx5_cqe64 *cqe64, u32 rsn)
Eli Cohene126ba92013-07-07 17:25:49 +03001040{
Moshe Lazercfd8f1d2013-10-23 09:53:17 +03001041 return rsn == (ntohl(cqe64->sop_drop_qpn) & 0xffffff);
Eli Cohene126ba92013-07-07 17:25:49 +03001042}
1043
1044void __mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 rsn, struct mlx5_ib_srq *srq)
1045{
1046 struct mlx5_cqe64 *cqe64, *dest64;
1047 void *cqe, *dest;
1048 u32 prod_index;
1049 int nfreed = 0;
1050 u8 owner_bit;
1051
1052 if (!cq)
1053 return;
1054
1055 /* First we need to find the current producer index, so we
1056 * know where to start cleaning from. It doesn't matter if HW
1057 * adds new entries after this loop -- the QP we're worried
1058 * about is already in RESET, so the new entries won't come
1059 * from our QP and therefore don't need to be checked.
1060 */
1061 for (prod_index = cq->mcq.cons_index; get_sw_cqe(cq, prod_index); prod_index++)
1062 if (prod_index == cq->mcq.cons_index + cq->ibcq.cqe)
1063 break;
1064
1065 /* Now sweep backwards through the CQ, removing CQ entries
1066 * that match our QP by copying older entries on top of them.
1067 */
1068 while ((int) --prod_index - (int) cq->mcq.cons_index >= 0) {
1069 cqe = get_cqe(cq, prod_index & cq->ibcq.cqe);
1070 cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
Moshe Lazercfd8f1d2013-10-23 09:53:17 +03001071 if (is_equal_rsn(cqe64, rsn)) {
1072 if (srq && (ntohl(cqe64->srqn) & 0xffffff))
Eli Cohene126ba92013-07-07 17:25:49 +03001073 mlx5_ib_free_srq_wqe(srq, be16_to_cpu(cqe64->wqe_counter));
1074 ++nfreed;
1075 } else if (nfreed) {
1076 dest = get_cqe(cq, (prod_index + nfreed) & cq->ibcq.cqe);
1077 dest64 = (cq->mcq.cqe_sz == 64) ? dest : dest + 64;
1078 owner_bit = dest64->op_own & MLX5_CQE_OWNER_MASK;
1079 memcpy(dest, cqe, cq->mcq.cqe_sz);
1080 dest64->op_own = owner_bit |
1081 (dest64->op_own & ~MLX5_CQE_OWNER_MASK);
1082 }
1083 }
1084
1085 if (nfreed) {
1086 cq->mcq.cons_index += nfreed;
1087 /* Make sure update of buffer contents is done before
1088 * updating consumer index.
1089 */
1090 wmb();
1091 mlx5_cq_set_ci(&cq->mcq);
1092 }
1093}
1094
1095void mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 qpn, struct mlx5_ib_srq *srq)
1096{
1097 if (!cq)
1098 return;
1099
1100 spin_lock_irq(&cq->lock);
1101 __mlx5_ib_cq_clean(cq, qpn, srq);
1102 spin_unlock_irq(&cq->lock);
1103}
1104
1105int mlx5_ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
1106{
Eli Cohen3bdb31f2014-01-14 17:45:17 +02001107 struct mlx5_ib_dev *dev = to_mdev(cq->device);
1108 struct mlx5_ib_cq *mcq = to_mcq(cq);
1109 int err;
Eli Cohen3bdb31f2014-01-14 17:45:17 +02001110
Saeed Mahameed938fe832015-05-28 22:28:41 +03001111 if (!MLX5_CAP_GEN(dev->mdev, cq_moderation))
Eli Cohen3bdb31f2014-01-14 17:45:17 +02001112 return -ENOSYS;
1113
Saeed Mahameed27827782016-07-16 02:33:22 +03001114 err = mlx5_core_modify_cq_moderation(dev->mdev, &mcq->mcq,
1115 cq_period, cq_count);
Eli Cohen3bdb31f2014-01-14 17:45:17 +02001116 if (err)
1117 mlx5_ib_warn(dev, "modify cq 0x%x failed\n", mcq->mcq.cqn);
1118
1119 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001120}
1121
Eli Cohenbde51582014-01-14 17:45:18 +02001122static int resize_user(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
1123 int entries, struct ib_udata *udata, int *npas,
1124 int *page_shift, int *cqe_size)
1125{
1126 struct mlx5_ib_resize_cq ucmd;
1127 struct ib_umem *umem;
1128 int err;
1129 int npages;
1130 struct ib_ucontext *context = cq->buf.umem->context;
1131
Eli Cohen57761d82014-01-15 14:56:44 +02001132 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
1133 if (err)
1134 return err;
1135
1136 if (ucmd.reserved0 || ucmd.reserved1)
1137 return -EINVAL;
Eli Cohenbde51582014-01-14 17:45:18 +02001138
1139 umem = ib_umem_get(context, ucmd.buf_addr, entries * ucmd.cqe_size,
1140 IB_ACCESS_LOCAL_WRITE, 1);
1141 if (IS_ERR(umem)) {
1142 err = PTR_ERR(umem);
1143 return err;
1144 }
1145
1146 mlx5_ib_cont_pages(umem, ucmd.buf_addr, &npages, page_shift,
1147 npas, NULL);
1148
1149 cq->resize_umem = umem;
1150 *cqe_size = ucmd.cqe_size;
1151
1152 return 0;
1153}
1154
1155static void un_resize_user(struct mlx5_ib_cq *cq)
1156{
1157 ib_umem_release(cq->resize_umem);
1158}
1159
1160static int resize_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
1161 int entries, int cqe_size)
1162{
1163 int err;
1164
1165 cq->resize_buf = kzalloc(sizeof(*cq->resize_buf), GFP_KERNEL);
1166 if (!cq->resize_buf)
1167 return -ENOMEM;
1168
1169 err = alloc_cq_buf(dev, cq->resize_buf, entries, cqe_size);
1170 if (err)
1171 goto ex;
1172
1173 init_cq_buf(cq, cq->resize_buf);
1174
1175 return 0;
1176
1177ex:
1178 kfree(cq->resize_buf);
1179 return err;
1180}
1181
1182static void un_resize_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq)
1183{
1184 free_cq_buf(dev, cq->resize_buf);
1185 cq->resize_buf = NULL;
1186}
1187
1188static int copy_resize_cqes(struct mlx5_ib_cq *cq)
1189{
1190 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
1191 struct mlx5_cqe64 *scqe64;
1192 struct mlx5_cqe64 *dcqe64;
1193 void *start_cqe;
1194 void *scqe;
1195 void *dcqe;
1196 int ssize;
1197 int dsize;
1198 int i;
1199 u8 sw_own;
1200
1201 ssize = cq->buf.cqe_size;
1202 dsize = cq->resize_buf->cqe_size;
1203 if (ssize != dsize) {
1204 mlx5_ib_warn(dev, "resize from different cqe size is not supported\n");
1205 return -EINVAL;
1206 }
1207
1208 i = cq->mcq.cons_index;
1209 scqe = get_sw_cqe(cq, i);
1210 scqe64 = ssize == 64 ? scqe : scqe + 64;
1211 start_cqe = scqe;
1212 if (!scqe) {
1213 mlx5_ib_warn(dev, "expected cqe in sw ownership\n");
1214 return -EINVAL;
1215 }
1216
1217 while ((scqe64->op_own >> 4) != MLX5_CQE_RESIZE_CQ) {
1218 dcqe = get_cqe_from_buf(cq->resize_buf,
1219 (i + 1) & (cq->resize_buf->nent),
1220 dsize);
1221 dcqe64 = dsize == 64 ? dcqe : dcqe + 64;
1222 sw_own = sw_ownership_bit(i + 1, cq->resize_buf->nent);
1223 memcpy(dcqe, scqe, dsize);
1224 dcqe64->op_own = (dcqe64->op_own & ~MLX5_CQE_OWNER_MASK) | sw_own;
1225
1226 ++i;
1227 scqe = get_sw_cqe(cq, i);
1228 scqe64 = ssize == 64 ? scqe : scqe + 64;
1229 if (!scqe) {
1230 mlx5_ib_warn(dev, "expected cqe in sw ownership\n");
1231 return -EINVAL;
1232 }
1233
1234 if (scqe == start_cqe) {
1235 pr_warn("resize CQ failed to get resize CQE, CQN 0x%x\n",
1236 cq->mcq.cqn);
1237 return -ENOMEM;
1238 }
1239 }
1240 ++cq->mcq.cons_index;
1241 return 0;
1242}
1243
Eli Cohene126ba92013-07-07 17:25:49 +03001244int mlx5_ib_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
1245{
Eli Cohenbde51582014-01-14 17:45:18 +02001246 struct mlx5_ib_dev *dev = to_mdev(ibcq->device);
1247 struct mlx5_ib_cq *cq = to_mcq(ibcq);
Saeed Mahameed27827782016-07-16 02:33:22 +03001248 void *cqc;
1249 u32 *in;
Eli Cohenbde51582014-01-14 17:45:18 +02001250 int err;
1251 int npas;
Saeed Mahameed27827782016-07-16 02:33:22 +03001252 __be64 *pas;
Eli Cohenbde51582014-01-14 17:45:18 +02001253 int page_shift;
1254 int inlen;
1255 int uninitialized_var(cqe_size);
1256 unsigned long flags;
1257
Saeed Mahameed938fe832015-05-28 22:28:41 +03001258 if (!MLX5_CAP_GEN(dev->mdev, cq_resize)) {
Eli Cohenbde51582014-01-14 17:45:18 +02001259 pr_info("Firmware does not support resize CQ\n");
1260 return -ENOSYS;
1261 }
1262
Noa Osherovich3c4c3772016-06-04 15:15:35 +03001263 if (entries < 1 ||
1264 entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz))) {
1265 mlx5_ib_warn(dev, "wrong entries number %d, max %d\n",
1266 entries,
1267 1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz));
Eli Cohenbde51582014-01-14 17:45:18 +02001268 return -EINVAL;
Noa Osherovich3c4c3772016-06-04 15:15:35 +03001269 }
Eli Cohenbde51582014-01-14 17:45:18 +02001270
1271 entries = roundup_pow_of_two(entries + 1);
Noa Osherovich3c4c3772016-06-04 15:15:35 +03001272 if (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz)) + 1)
Eli Cohenbde51582014-01-14 17:45:18 +02001273 return -EINVAL;
1274
1275 if (entries == ibcq->cqe + 1)
1276 return 0;
1277
1278 mutex_lock(&cq->resize_mutex);
1279 if (udata) {
1280 err = resize_user(dev, cq, entries, udata, &npas, &page_shift,
1281 &cqe_size);
1282 } else {
1283 cqe_size = 64;
1284 err = resize_kernel(dev, cq, entries, cqe_size);
1285 if (!err) {
1286 npas = cq->resize_buf->buf.npages;
1287 page_shift = cq->resize_buf->buf.page_shift;
1288 }
1289 }
1290
1291 if (err)
1292 goto ex;
1293
Saeed Mahameed27827782016-07-16 02:33:22 +03001294 inlen = MLX5_ST_SZ_BYTES(modify_cq_in) +
1295 MLX5_FLD_SZ_BYTES(modify_cq_in, pas[0]) * npas;
1296
Eli Cohenbde51582014-01-14 17:45:18 +02001297 in = mlx5_vzalloc(inlen);
1298 if (!in) {
1299 err = -ENOMEM;
1300 goto ex_resize;
1301 }
1302
Saeed Mahameed27827782016-07-16 02:33:22 +03001303 pas = (__be64 *)MLX5_ADDR_OF(modify_cq_in, in, pas);
Eli Cohenbde51582014-01-14 17:45:18 +02001304 if (udata)
1305 mlx5_ib_populate_pas(dev, cq->resize_umem, page_shift,
Saeed Mahameed27827782016-07-16 02:33:22 +03001306 pas, 0);
Eli Cohenbde51582014-01-14 17:45:18 +02001307 else
Saeed Mahameed27827782016-07-16 02:33:22 +03001308 mlx5_fill_page_array(&cq->resize_buf->buf, pas);
Eli Cohenbde51582014-01-14 17:45:18 +02001309
Saeed Mahameed27827782016-07-16 02:33:22 +03001310 MLX5_SET(modify_cq_in, in,
1311 modify_field_select_resize_field_select.resize_field_select.resize_field_select,
1312 MLX5_MODIFY_CQ_MASK_LOG_SIZE |
1313 MLX5_MODIFY_CQ_MASK_PG_OFFSET |
1314 MLX5_MODIFY_CQ_MASK_PG_SIZE);
1315
1316 cqc = MLX5_ADDR_OF(modify_cq_in, in, cq_context);
1317
1318 MLX5_SET(cqc, cqc, log_page_size,
1319 page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1320 MLX5_SET(cqc, cqc, cqe_sz, cqe_sz_to_mlx_sz(cqe_size));
1321 MLX5_SET(cqc, cqc, log_cq_size, ilog2(entries));
1322
1323 MLX5_SET(modify_cq_in, in, op_mod, MLX5_CQ_OPMOD_RESIZE);
1324 MLX5_SET(modify_cq_in, in, cqn, cq->mcq.cqn);
Eli Cohenbde51582014-01-14 17:45:18 +02001325
Jack Morgenstein9603b612014-07-28 23:30:22 +03001326 err = mlx5_core_modify_cq(dev->mdev, &cq->mcq, in, inlen);
Eli Cohenbde51582014-01-14 17:45:18 +02001327 if (err)
1328 goto ex_alloc;
1329
1330 if (udata) {
1331 cq->ibcq.cqe = entries - 1;
1332 ib_umem_release(cq->buf.umem);
1333 cq->buf.umem = cq->resize_umem;
1334 cq->resize_umem = NULL;
1335 } else {
1336 struct mlx5_ib_cq_buf tbuf;
1337 int resized = 0;
1338
1339 spin_lock_irqsave(&cq->lock, flags);
1340 if (cq->resize_buf) {
1341 err = copy_resize_cqes(cq);
1342 if (!err) {
1343 tbuf = cq->buf;
1344 cq->buf = *cq->resize_buf;
1345 kfree(cq->resize_buf);
1346 cq->resize_buf = NULL;
1347 resized = 1;
1348 }
1349 }
1350 cq->ibcq.cqe = entries - 1;
1351 spin_unlock_irqrestore(&cq->lock, flags);
1352 if (resized)
1353 free_cq_buf(dev, &tbuf);
1354 }
1355 mutex_unlock(&cq->resize_mutex);
1356
Al Viro479163f2014-11-20 08:13:57 +00001357 kvfree(in);
Eli Cohenbde51582014-01-14 17:45:18 +02001358 return 0;
1359
1360ex_alloc:
Al Viro479163f2014-11-20 08:13:57 +00001361 kvfree(in);
Eli Cohenbde51582014-01-14 17:45:18 +02001362
1363ex_resize:
1364 if (udata)
1365 un_resize_user(cq);
1366 else
1367 un_resize_kernel(dev, cq);
1368ex:
1369 mutex_unlock(&cq->resize_mutex);
1370 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001371}
1372
1373int mlx5_ib_get_cqe_size(struct mlx5_ib_dev *dev, struct ib_cq *ibcq)
1374{
1375 struct mlx5_ib_cq *cq;
1376
1377 if (!ibcq)
1378 return 128;
1379
1380 cq = to_mcq(ibcq);
1381 return cq->cqe_size;
1382}
Haggai Eran25361e02016-02-29 15:45:08 +02001383
1384/* Called from atomic context */
1385int mlx5_ib_generate_wc(struct ib_cq *ibcq, struct ib_wc *wc)
1386{
1387 struct mlx5_ib_wc *soft_wc;
1388 struct mlx5_ib_cq *cq = to_mcq(ibcq);
1389 unsigned long flags;
1390
1391 soft_wc = kmalloc(sizeof(*soft_wc), GFP_ATOMIC);
1392 if (!soft_wc)
1393 return -ENOMEM;
1394
1395 soft_wc->wc = *wc;
1396 spin_lock_irqsave(&cq->lock, flags);
1397 list_add_tail(&soft_wc->list, &cq->wc_list);
1398 if (cq->notify_flags == IB_CQ_NEXT_COMP ||
1399 wc->status != IB_WC_SUCCESS) {
1400 cq->notify_flags = 0;
1401 schedule_work(&cq->notify_work);
1402 }
1403 spin_unlock_irqrestore(&cq->lock, flags);
1404
1405 return 0;
1406}