blob: a19ebb19952eac7f09760a62f91a6f37e18c965f [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;
Moni Shoua4b829c02017-04-20 13:26:54 +0300175 u8 roce_packet_type;
176 bool vlan_present;
Eli Cohene126ba92013-07-07 17:25:49 +0300177 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;
Erez Shitritc7ce8332016-02-21 16:27:18 +0200211 wc->wc_flags = IB_WC_IP_CSUM_OK;
212 if (unlikely(!((cqe->hds_ip_ext & CQE_L3_OK) &&
213 (cqe->hds_ip_ext & CQE_L4_OK))))
214 wc->wc_flags = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300215 break;
216 case MLX5_CQE_RESP_SEND_IMM:
217 wc->opcode = IB_WC_RECV;
218 wc->wc_flags = IB_WC_WITH_IMM;
219 wc->ex.imm_data = cqe->imm_inval_pkey;
220 break;
221 case MLX5_CQE_RESP_SEND_INV:
222 wc->opcode = IB_WC_RECV;
223 wc->wc_flags = IB_WC_WITH_INVALIDATE;
224 wc->ex.invalidate_rkey = be32_to_cpu(cqe->imm_inval_pkey);
225 break;
226 }
227 wc->slid = be16_to_cpu(cqe->slid);
Eli Cohene126ba92013-07-07 17:25:49 +0300228 wc->src_qp = be32_to_cpu(cqe->flags_rqpn) & 0xffffff;
229 wc->dlid_path_bits = cqe->ml_path;
230 g = (be32_to_cpu(cqe->flags_rqpn) >> 28) & 3;
231 wc->wc_flags |= g ? IB_WC_GRH : 0;
Sagi Grimbergb6364012015-09-02 22:23:04 +0300232 if (unlikely(is_qp1(qp->ibqp.qp_type))) {
233 u16 pkey = be32_to_cpu(cqe->imm_inval_pkey) & 0xffff;
234
235 ib_find_cached_pkey(&dev->ib_dev, qp->port, pkey,
236 &wc->pkey_index);
237 } else {
238 wc->pkey_index = 0;
239 }
Achiad Shochatcb34be62015-12-23 18:47:22 +0200240
Moni Shoua4b829c02017-04-20 13:26:54 +0300241 if (ll != IB_LINK_LAYER_ETHERNET) {
242 wc->sl = (be32_to_cpu(cqe->flags_rqpn) >> 24) & 0xf;
Achiad Shochatcb34be62015-12-23 18:47:22 +0200243 return;
Moni Shoua4b829c02017-04-20 13:26:54 +0300244 }
Achiad Shochatcb34be62015-12-23 18:47:22 +0200245
Moni Shoua4b829c02017-04-20 13:26:54 +0300246 vlan_present = cqe->l4_l3_hdr_type & 0x1;
247 roce_packet_type = (be32_to_cpu(cqe->flags_rqpn) >> 24) & 0x3;
248 if (vlan_present) {
249 wc->vlan_id = (be16_to_cpu(cqe->vlan_info)) & 0xfff;
250 wc->sl = (be16_to_cpu(cqe->vlan_info) >> 13) & 0x7;
251 wc->wc_flags |= IB_WC_WITH_VLAN;
252 } else {
253 wc->sl = 0;
254 }
255
256 switch (roce_packet_type) {
Achiad Shochatcb34be62015-12-23 18:47:22 +0200257 case MLX5_CQE_ROCE_L3_HEADER_TYPE_GRH:
258 wc->network_hdr_type = RDMA_NETWORK_IB;
259 break;
260 case MLX5_CQE_ROCE_L3_HEADER_TYPE_IPV6:
261 wc->network_hdr_type = RDMA_NETWORK_IPV6;
262 break;
263 case MLX5_CQE_ROCE_L3_HEADER_TYPE_IPV4:
264 wc->network_hdr_type = RDMA_NETWORK_IPV4;
265 break;
266 }
267 wc->wc_flags |= IB_WC_WITH_NETWORK_HDR_TYPE;
Eli Cohene126ba92013-07-07 17:25:49 +0300268}
269
270static void dump_cqe(struct mlx5_ib_dev *dev, struct mlx5_err_cqe *cqe)
271{
272 __be32 *p = (__be32 *)cqe;
273 int i;
274
275 mlx5_ib_warn(dev, "dump error cqe\n");
276 for (i = 0; i < sizeof(*cqe) / 16; i++, p += 4)
277 pr_info("%08x %08x %08x %08x\n", be32_to_cpu(p[0]),
278 be32_to_cpu(p[1]), be32_to_cpu(p[2]),
279 be32_to_cpu(p[3]));
280}
281
282static void mlx5_handle_error_cqe(struct mlx5_ib_dev *dev,
283 struct mlx5_err_cqe *cqe,
284 struct ib_wc *wc)
285{
286 int dump = 1;
287
288 switch (cqe->syndrome) {
289 case MLX5_CQE_SYNDROME_LOCAL_LENGTH_ERR:
290 wc->status = IB_WC_LOC_LEN_ERR;
291 break;
292 case MLX5_CQE_SYNDROME_LOCAL_QP_OP_ERR:
293 wc->status = IB_WC_LOC_QP_OP_ERR;
294 break;
295 case MLX5_CQE_SYNDROME_LOCAL_PROT_ERR:
296 wc->status = IB_WC_LOC_PROT_ERR;
297 break;
298 case MLX5_CQE_SYNDROME_WR_FLUSH_ERR:
299 dump = 0;
300 wc->status = IB_WC_WR_FLUSH_ERR;
301 break;
302 case MLX5_CQE_SYNDROME_MW_BIND_ERR:
303 wc->status = IB_WC_MW_BIND_ERR;
304 break;
305 case MLX5_CQE_SYNDROME_BAD_RESP_ERR:
306 wc->status = IB_WC_BAD_RESP_ERR;
307 break;
308 case MLX5_CQE_SYNDROME_LOCAL_ACCESS_ERR:
309 wc->status = IB_WC_LOC_ACCESS_ERR;
310 break;
311 case MLX5_CQE_SYNDROME_REMOTE_INVAL_REQ_ERR:
312 wc->status = IB_WC_REM_INV_REQ_ERR;
313 break;
314 case MLX5_CQE_SYNDROME_REMOTE_ACCESS_ERR:
315 wc->status = IB_WC_REM_ACCESS_ERR;
316 break;
317 case MLX5_CQE_SYNDROME_REMOTE_OP_ERR:
318 wc->status = IB_WC_REM_OP_ERR;
319 break;
320 case MLX5_CQE_SYNDROME_TRANSPORT_RETRY_EXC_ERR:
321 wc->status = IB_WC_RETRY_EXC_ERR;
322 dump = 0;
323 break;
324 case MLX5_CQE_SYNDROME_RNR_RETRY_EXC_ERR:
325 wc->status = IB_WC_RNR_RETRY_EXC_ERR;
326 dump = 0;
327 break;
328 case MLX5_CQE_SYNDROME_REMOTE_ABORTED_ERR:
329 wc->status = IB_WC_REM_ABORT_ERR;
330 break;
331 default:
332 wc->status = IB_WC_GENERAL_ERR;
333 break;
334 }
335
336 wc->vendor_err = cqe->vendor_err_synd;
337 if (dump)
338 dump_cqe(dev, cqe);
339}
340
341static int is_atomic_response(struct mlx5_ib_qp *qp, uint16_t idx)
342{
343 /* TBD: waiting decision
344 */
345 return 0;
346}
347
348static void *mlx5_get_atomic_laddr(struct mlx5_ib_qp *qp, uint16_t idx)
349{
350 struct mlx5_wqe_data_seg *dpseg;
351 void *addr;
352
353 dpseg = mlx5_get_send_wqe(qp, idx) + sizeof(struct mlx5_wqe_ctrl_seg) +
354 sizeof(struct mlx5_wqe_raddr_seg) +
355 sizeof(struct mlx5_wqe_atomic_seg);
356 addr = (void *)(unsigned long)be64_to_cpu(dpseg->addr);
357 return addr;
358}
359
360static void handle_atomic(struct mlx5_ib_qp *qp, struct mlx5_cqe64 *cqe64,
361 uint16_t idx)
362{
363 void *addr;
364 int byte_count;
365 int i;
366
367 if (!is_atomic_response(qp, idx))
368 return;
369
370 byte_count = be32_to_cpu(cqe64->byte_cnt);
371 addr = mlx5_get_atomic_laddr(qp, idx);
372
373 if (byte_count == 4) {
374 *(uint32_t *)addr = be32_to_cpu(*((__be32 *)addr));
375 } else {
376 for (i = 0; i < byte_count; i += 8) {
377 *(uint64_t *)addr = be64_to_cpu(*((__be64 *)addr));
378 addr += 8;
379 }
380 }
381
382 return;
383}
384
385static void handle_atomics(struct mlx5_ib_qp *qp, struct mlx5_cqe64 *cqe64,
386 u16 tail, u16 head)
387{
Jack Morgensteinf241e742014-07-28 23:30:23 +0300388 u16 idx;
Eli Cohene126ba92013-07-07 17:25:49 +0300389
390 do {
391 idx = tail & (qp->sq.wqe_cnt - 1);
392 handle_atomic(qp, cqe64, idx);
393 if (idx == head)
394 break;
395
396 tail = qp->sq.w_list[idx].next;
397 } while (1);
398 tail = qp->sq.w_list[idx].next;
399 qp->sq.last_poll = tail;
400}
401
Eli Cohenbde51582014-01-14 17:45:18 +0200402static void free_cq_buf(struct mlx5_ib_dev *dev, struct mlx5_ib_cq_buf *buf)
403{
Jack Morgenstein9603b612014-07-28 23:30:22 +0300404 mlx5_buf_free(dev->mdev, &buf->buf);
Eli Cohenbde51582014-01-14 17:45:18 +0200405}
406
Sagi Grimbergd5436ba2014-02-23 14:19:12 +0200407static void get_sig_err_item(struct mlx5_sig_err_cqe *cqe,
408 struct ib_sig_err *item)
409{
410 u16 syndrome = be16_to_cpu(cqe->syndrome);
411
412#define GUARD_ERR (1 << 13)
413#define APPTAG_ERR (1 << 12)
414#define REFTAG_ERR (1 << 11)
415
416 if (syndrome & GUARD_ERR) {
417 item->err_type = IB_SIG_BAD_GUARD;
418 item->expected = be32_to_cpu(cqe->expected_trans_sig) >> 16;
419 item->actual = be32_to_cpu(cqe->actual_trans_sig) >> 16;
420 } else
421 if (syndrome & REFTAG_ERR) {
422 item->err_type = IB_SIG_BAD_REFTAG;
423 item->expected = be32_to_cpu(cqe->expected_reftag);
424 item->actual = be32_to_cpu(cqe->actual_reftag);
425 } else
426 if (syndrome & APPTAG_ERR) {
427 item->err_type = IB_SIG_BAD_APPTAG;
428 item->expected = be32_to_cpu(cqe->expected_trans_sig) & 0xffff;
429 item->actual = be32_to_cpu(cqe->actual_trans_sig) & 0xffff;
430 } else {
431 pr_err("Got signature completion error with bad syndrome %04x\n",
432 syndrome);
433 }
434
435 item->sig_err_offset = be64_to_cpu(cqe->err_offset);
436 item->key = be32_to_cpu(cqe->mkey);
437}
438
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300439static void sw_send_comp(struct mlx5_ib_qp *qp, int num_entries,
440 struct ib_wc *wc, int *npolled)
441{
442 struct mlx5_ib_wq *wq;
443 unsigned int cur;
444 unsigned int idx;
445 int np;
446 int i;
447
448 wq = &qp->sq;
449 cur = wq->head - wq->tail;
450 np = *npolled;
451
452 if (cur == 0)
453 return;
454
455 for (i = 0; i < cur && np < num_entries; i++) {
456 idx = wq->last_poll & (wq->wqe_cnt - 1);
457 wc->wr_id = wq->wrid[idx];
458 wc->status = IB_WC_WR_FLUSH_ERR;
459 wc->vendor_err = MLX5_CQE_SYNDROME_WR_FLUSH_ERR;
460 wq->tail++;
461 np++;
462 wc->qp = &qp->ibqp;
463 wc++;
464 wq->last_poll = wq->w_list[idx].next;
465 }
466 *npolled = np;
467}
468
469static void sw_recv_comp(struct mlx5_ib_qp *qp, int num_entries,
470 struct ib_wc *wc, int *npolled)
471{
472 struct mlx5_ib_wq *wq;
473 unsigned int cur;
474 int np;
475 int i;
476
477 wq = &qp->rq;
478 cur = wq->head - wq->tail;
479 np = *npolled;
480
481 if (cur == 0)
482 return;
483
484 for (i = 0; i < cur && np < num_entries; i++) {
485 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
486 wc->status = IB_WC_WR_FLUSH_ERR;
487 wc->vendor_err = MLX5_CQE_SYNDROME_WR_FLUSH_ERR;
488 wq->tail++;
489 np++;
490 wc->qp = &qp->ibqp;
491 wc++;
492 }
493 *npolled = np;
494}
495
496static void mlx5_ib_poll_sw_comp(struct mlx5_ib_cq *cq, int num_entries,
497 struct ib_wc *wc, int *npolled)
498{
499 struct mlx5_ib_qp *qp;
500
501 *npolled = 0;
502 /* Find uncompleted WQEs belonging to that cq and retrun mmics ones */
503 list_for_each_entry(qp, &cq->list_send_qp, cq_send_list) {
504 sw_send_comp(qp, num_entries, wc + *npolled, npolled);
505 if (*npolled >= num_entries)
506 return;
507 }
508
509 list_for_each_entry(qp, &cq->list_recv_qp, cq_recv_list) {
510 sw_recv_comp(qp, num_entries, wc + *npolled, npolled);
511 if (*npolled >= num_entries)
512 return;
513 }
514}
515
Eli Cohene126ba92013-07-07 17:25:49 +0300516static int mlx5_poll_one(struct mlx5_ib_cq *cq,
517 struct mlx5_ib_qp **cur_qp,
518 struct ib_wc *wc)
519{
520 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
521 struct mlx5_err_cqe *err_cqe;
522 struct mlx5_cqe64 *cqe64;
523 struct mlx5_core_qp *mqp;
524 struct mlx5_ib_wq *wq;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +0200525 struct mlx5_sig_err_cqe *sig_err_cqe;
Matan Baraka606b0f2016-02-29 18:05:28 +0200526 struct mlx5_core_mkey *mmkey;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +0200527 struct mlx5_ib_mr *mr;
Eli Cohene126ba92013-07-07 17:25:49 +0300528 uint8_t opcode;
529 uint32_t qpn;
530 u16 wqe_ctr;
531 void *cqe;
532 int idx;
533
Eli Cohenbde51582014-01-14 17:45:18 +0200534repoll:
Eli Cohene126ba92013-07-07 17:25:49 +0300535 cqe = next_cqe_sw(cq);
536 if (!cqe)
537 return -EAGAIN;
538
539 cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
540
541 ++cq->mcq.cons_index;
542
543 /* Make sure we read CQ entry contents after we've checked the
544 * ownership bit.
545 */
546 rmb();
547
Eli Cohenbde51582014-01-14 17:45:18 +0200548 opcode = cqe64->op_own >> 4;
549 if (unlikely(opcode == MLX5_CQE_RESIZE_CQ)) {
550 if (likely(cq->resize_buf)) {
551 free_cq_buf(dev, &cq->buf);
552 cq->buf = *cq->resize_buf;
553 kfree(cq->resize_buf);
554 cq->resize_buf = NULL;
555 goto repoll;
556 } else {
557 mlx5_ib_warn(dev, "unexpected resize cqe\n");
558 }
559 }
Eli Cohene126ba92013-07-07 17:25:49 +0300560
561 qpn = ntohl(cqe64->sop_drop_qpn) & 0xffffff;
562 if (!*cur_qp || (qpn != (*cur_qp)->ibqp.qp_num)) {
563 /* We do not have to take the QP table lock here,
564 * because CQs will be locked while QPs are removed
565 * from the table.
566 */
Jack Morgenstein9603b612014-07-28 23:30:22 +0300567 mqp = __mlx5_qp_lookup(dev->mdev, qpn);
Eli Cohene126ba92013-07-07 17:25:49 +0300568 *cur_qp = to_mibqp(mqp);
569 }
570
571 wc->qp = &(*cur_qp)->ibqp;
Eli Cohene126ba92013-07-07 17:25:49 +0300572 switch (opcode) {
573 case MLX5_CQE_REQ:
574 wq = &(*cur_qp)->sq;
575 wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
576 idx = wqe_ctr & (wq->wqe_cnt - 1);
577 handle_good_req(wc, cqe64, wq, idx);
578 handle_atomics(*cur_qp, cqe64, wq->last_poll, idx);
579 wc->wr_id = wq->wrid[idx];
580 wq->tail = wq->wqe_head[idx] + 1;
581 wc->status = IB_WC_SUCCESS;
582 break;
583 case MLX5_CQE_RESP_WR_IMM:
584 case MLX5_CQE_RESP_SEND:
585 case MLX5_CQE_RESP_SEND_IMM:
586 case MLX5_CQE_RESP_SEND_INV:
587 handle_responder(wc, cqe64, *cur_qp);
588 wc->status = IB_WC_SUCCESS;
589 break;
590 case MLX5_CQE_RESIZE_CQ:
591 break;
592 case MLX5_CQE_REQ_ERR:
593 case MLX5_CQE_RESP_ERR:
594 err_cqe = (struct mlx5_err_cqe *)cqe64;
595 mlx5_handle_error_cqe(dev, err_cqe, wc);
596 mlx5_ib_dbg(dev, "%s error cqe on cqn 0x%x:\n",
597 opcode == MLX5_CQE_REQ_ERR ?
598 "Requestor" : "Responder", cq->mcq.cqn);
599 mlx5_ib_dbg(dev, "syndrome 0x%x, vendor syndrome 0x%x\n",
600 err_cqe->syndrome, err_cqe->vendor_err_synd);
601 if (opcode == MLX5_CQE_REQ_ERR) {
602 wq = &(*cur_qp)->sq;
603 wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
604 idx = wqe_ctr & (wq->wqe_cnt - 1);
605 wc->wr_id = wq->wrid[idx];
606 wq->tail = wq->wqe_head[idx] + 1;
607 } else {
608 struct mlx5_ib_srq *srq;
609
610 if ((*cur_qp)->ibqp.srq) {
611 srq = to_msrq((*cur_qp)->ibqp.srq);
612 wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
613 wc->wr_id = srq->wrid[wqe_ctr];
614 mlx5_ib_free_srq_wqe(srq, wqe_ctr);
615 } else {
616 wq = &(*cur_qp)->rq;
617 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
618 ++wq->tail;
619 }
620 }
621 break;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +0200622 case MLX5_CQE_SIG_ERR:
623 sig_err_cqe = (struct mlx5_sig_err_cqe *)cqe64;
624
Matan Baraka606b0f2016-02-29 18:05:28 +0200625 read_lock(&dev->mdev->priv.mkey_table.lock);
626 mmkey = __mlx5_mr_lookup(dev->mdev,
627 mlx5_base_mkey(be32_to_cpu(sig_err_cqe->mkey)));
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,
Erez Shitrite3554022018-05-21 11:41:01 +0300648 struct ib_wc *wc, bool is_fatal_err)
Haggai Eran25361e02016-02-29 15:45:08 +0200649{
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
Erez Shitrite3554022018-05-21 11:41:01 +0300661 if (unlikely(is_fatal_err)) {
662 soft_wc->wc.status = IB_WC_WR_FLUSH_ERR;
663 soft_wc->wc.vendor_err = MLX5_CQE_SYNDROME_WR_FLUSH_ERR;
664 }
Haggai Eran25361e02016-02-29 15:45:08 +0200665 wc[npolled++] = soft_wc->wc;
666 list_del(&soft_wc->list);
667 kfree(soft_wc);
668 }
669
670 return npolled;
671}
672
Eli Cohene126ba92013-07-07 17:25:49 +0300673int mlx5_ib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
674{
675 struct mlx5_ib_cq *cq = to_mcq(ibcq);
676 struct mlx5_ib_qp *cur_qp = NULL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300677 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
678 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300679 unsigned long flags;
Haggai Eran25361e02016-02-29 15:45:08 +0200680 int soft_polled = 0;
Eli Cohene126ba92013-07-07 17:25:49 +0300681 int npolled;
Eli Cohene126ba92013-07-07 17:25:49 +0300682
683 spin_lock_irqsave(&cq->lock, flags);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300684 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
Erez Shitrite3554022018-05-21 11:41:01 +0300685 /* make sure no soft wqe's are waiting */
686 if (unlikely(!list_empty(&cq->wc_list)))
687 soft_polled = poll_soft_wc(cq, num_entries, wc, true);
688
689 mlx5_ib_poll_sw_comp(cq, num_entries - soft_polled,
690 wc + soft_polled, &npolled);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300691 goto out;
692 }
Eli Cohene126ba92013-07-07 17:25:49 +0300693
Haggai Eran25361e02016-02-29 15:45:08 +0200694 if (unlikely(!list_empty(&cq->wc_list)))
Erez Shitrite3554022018-05-21 11:41:01 +0300695 soft_polled = poll_soft_wc(cq, num_entries, wc, false);
Haggai Eran25361e02016-02-29 15:45:08 +0200696
697 for (npolled = 0; npolled < num_entries - soft_polled; npolled++) {
Leon Romanovskydbdf7d42016-08-28 10:58:38 +0300698 if (mlx5_poll_one(cq, &cur_qp, wc + soft_polled + npolled))
Eli Cohene126ba92013-07-07 17:25:49 +0300699 break;
700 }
701
702 if (npolled)
703 mlx5_cq_set_ci(&cq->mcq);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300704out:
Eli Cohene126ba92013-07-07 17:25:49 +0300705 spin_unlock_irqrestore(&cq->lock, flags);
706
Leon Romanovskydbdf7d42016-08-28 10:58:38 +0300707 return soft_polled + npolled;
Eli Cohene126ba92013-07-07 17:25:49 +0300708}
709
710int mlx5_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
711{
Saeed Mahameedce0f7502015-04-02 17:07:33 +0300712 struct mlx5_core_dev *mdev = to_mdev(ibcq->device)->mdev;
Haggai Eran25361e02016-02-29 15:45:08 +0200713 struct mlx5_ib_cq *cq = to_mcq(ibcq);
Saeed Mahameedce0f7502015-04-02 17:07:33 +0300714 void __iomem *uar_page = mdev->priv.uuari.uars[0].map;
Haggai Eran25361e02016-02-29 15:45:08 +0200715 unsigned long irq_flags;
716 int ret = 0;
Saeed Mahameedce0f7502015-04-02 17:07:33 +0300717
Haggai Eran25361e02016-02-29 15:45:08 +0200718 spin_lock_irqsave(&cq->lock, irq_flags);
719 if (cq->notify_flags != IB_CQ_NEXT_COMP)
720 cq->notify_flags = flags & IB_CQ_SOLICITED_MASK;
721
722 if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !list_empty(&cq->wc_list))
723 ret = 1;
724 spin_unlock_irqrestore(&cq->lock, irq_flags);
725
726 mlx5_cq_arm(&cq->mcq,
Eli Cohene126ba92013-07-07 17:25:49 +0300727 (flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?
728 MLX5_CQ_DB_REQ_NOT_SOL : MLX5_CQ_DB_REQ_NOT,
Saeed Mahameedce0f7502015-04-02 17:07:33 +0300729 uar_page,
730 MLX5_GET_DOORBELL_LOCK(&mdev->priv.cq_uar_lock),
731 to_mcq(ibcq)->mcq.cons_index);
Eli Cohene126ba92013-07-07 17:25:49 +0300732
Haggai Eran25361e02016-02-29 15:45:08 +0200733 return ret;
Eli Cohene126ba92013-07-07 17:25:49 +0300734}
735
736static int alloc_cq_buf(struct mlx5_ib_dev *dev, struct mlx5_ib_cq_buf *buf,
737 int nent, int cqe_size)
738{
739 int err;
740
Amir Vadai64ffaa22015-05-28 22:28:38 +0300741 err = mlx5_buf_alloc(dev->mdev, nent * cqe_size, &buf->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300742 if (err)
743 return err;
744
745 buf->cqe_size = cqe_size;
Eli Cohenbde51582014-01-14 17:45:18 +0200746 buf->nent = nent;
Eli Cohene126ba92013-07-07 17:25:49 +0300747
748 return 0;
749}
750
Eli Cohene126ba92013-07-07 17:25:49 +0300751static int create_cq_user(struct mlx5_ib_dev *dev, struct ib_udata *udata,
752 struct ib_ucontext *context, struct mlx5_ib_cq *cq,
Saeed Mahameed27827782016-07-16 02:33:22 +0300753 int entries, u32 **cqb,
Eli Cohene126ba92013-07-07 17:25:49 +0300754 int *cqe_size, int *index, int *inlen)
755{
756 struct mlx5_ib_create_cq ucmd;
Yann Droneauda8237b32014-05-05 19:33:21 +0200757 size_t ucmdlen;
Eli Cohene126ba92013-07-07 17:25:49 +0300758 int page_shift;
Saeed Mahameed27827782016-07-16 02:33:22 +0300759 __be64 *pas;
Eli Cohene126ba92013-07-07 17:25:49 +0300760 int npages;
761 int ncont;
Saeed Mahameed27827782016-07-16 02:33:22 +0300762 void *cqc;
Eli Cohene126ba92013-07-07 17:25:49 +0300763 int err;
764
Yann Droneauda8237b32014-05-05 19:33:21 +0200765 ucmdlen =
766 (udata->inlen - sizeof(struct ib_uverbs_cmd_hdr) <
767 sizeof(ucmd)) ? (sizeof(ucmd) -
768 sizeof(ucmd.reserved)) : sizeof(ucmd);
769
770 if (ib_copy_from_udata(&ucmd, udata, ucmdlen))
Eli Cohene126ba92013-07-07 17:25:49 +0300771 return -EFAULT;
772
Yann Droneauda8237b32014-05-05 19:33:21 +0200773 if (ucmdlen == sizeof(ucmd) &&
774 ucmd.reserved != 0)
775 return -EINVAL;
776
Eli Cohene126ba92013-07-07 17:25:49 +0300777 if (ucmd.cqe_size != 64 && ucmd.cqe_size != 128)
778 return -EINVAL;
779
780 *cqe_size = ucmd.cqe_size;
781
782 cq->buf.umem = ib_umem_get(context, ucmd.buf_addr,
783 entries * ucmd.cqe_size,
784 IB_ACCESS_LOCAL_WRITE, 1);
785 if (IS_ERR(cq->buf.umem)) {
786 err = PTR_ERR(cq->buf.umem);
787 return err;
788 }
789
790 err = mlx5_ib_db_map_user(to_mucontext(context), ucmd.db_addr,
791 &cq->db);
792 if (err)
793 goto err_umem;
794
795 mlx5_ib_cont_pages(cq->buf.umem, ucmd.buf_addr, &npages, &page_shift,
796 &ncont, NULL);
797 mlx5_ib_dbg(dev, "addr 0x%llx, size %u, npages %d, page_shift %d, ncont %d\n",
798 ucmd.buf_addr, entries * ucmd.cqe_size, npages, page_shift, ncont);
799
Saeed Mahameed27827782016-07-16 02:33:22 +0300800 *inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
801 MLX5_FLD_SZ_BYTES(create_cq_in, pas[0]) * ncont;
Eli Cohene126ba92013-07-07 17:25:49 +0300802 *cqb = mlx5_vzalloc(*inlen);
803 if (!*cqb) {
804 err = -ENOMEM;
805 goto err_db;
806 }
Saeed Mahameed27827782016-07-16 02:33:22 +0300807
808 pas = (__be64 *)MLX5_ADDR_OF(create_cq_in, *cqb, pas);
809 mlx5_ib_populate_pas(dev, cq->buf.umem, page_shift, pas, 0);
810
811 cqc = MLX5_ADDR_OF(create_cq_in, *cqb, cq_context);
812 MLX5_SET(cqc, cqc, log_page_size,
813 page_shift - MLX5_ADAPTER_PAGE_SHIFT);
Eli Cohene126ba92013-07-07 17:25:49 +0300814
815 *index = to_mucontext(context)->uuari.uars[0].index;
816
817 return 0;
818
819err_db:
820 mlx5_ib_db_unmap_user(to_mucontext(context), &cq->db);
821
822err_umem:
823 ib_umem_release(cq->buf.umem);
824 return err;
825}
826
827static void destroy_cq_user(struct mlx5_ib_cq *cq, struct ib_ucontext *context)
828{
829 mlx5_ib_db_unmap_user(to_mucontext(context), &cq->db);
830 ib_umem_release(cq->buf.umem);
831}
832
Eli Cohenbde51582014-01-14 17:45:18 +0200833static void init_cq_buf(struct mlx5_ib_cq *cq, struct mlx5_ib_cq_buf *buf)
Eli Cohene126ba92013-07-07 17:25:49 +0300834{
835 int i;
836 void *cqe;
837 struct mlx5_cqe64 *cqe64;
838
Eli Cohenbde51582014-01-14 17:45:18 +0200839 for (i = 0; i < buf->nent; i++) {
840 cqe = get_cqe_from_buf(buf, i, buf->cqe_size);
841 cqe64 = buf->cqe_size == 64 ? cqe : cqe + 64;
842 cqe64->op_own = MLX5_CQE_INVALID << 4;
Eli Cohene126ba92013-07-07 17:25:49 +0300843 }
844}
845
846static int create_cq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
847 int entries, int cqe_size,
Saeed Mahameed27827782016-07-16 02:33:22 +0300848 u32 **cqb, int *index, int *inlen)
Eli Cohene126ba92013-07-07 17:25:49 +0300849{
Saeed Mahameed27827782016-07-16 02:33:22 +0300850 __be64 *pas;
851 void *cqc;
Eli Cohene126ba92013-07-07 17:25:49 +0300852 int err;
853
Jack Morgenstein9603b612014-07-28 23:30:22 +0300854 err = mlx5_db_alloc(dev->mdev, &cq->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300855 if (err)
856 return err;
857
858 cq->mcq.set_ci_db = cq->db.db;
859 cq->mcq.arm_db = cq->db.db + 1;
Eli Cohene126ba92013-07-07 17:25:49 +0300860 cq->mcq.cqe_sz = cqe_size;
861
862 err = alloc_cq_buf(dev, &cq->buf, entries, cqe_size);
863 if (err)
864 goto err_db;
865
Eli Cohenbde51582014-01-14 17:45:18 +0200866 init_cq_buf(cq, &cq->buf);
Eli Cohene126ba92013-07-07 17:25:49 +0300867
Saeed Mahameed27827782016-07-16 02:33:22 +0300868 *inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
869 MLX5_FLD_SZ_BYTES(create_cq_in, pas[0]) * cq->buf.buf.npages;
Eli Cohene126ba92013-07-07 17:25:49 +0300870 *cqb = mlx5_vzalloc(*inlen);
871 if (!*cqb) {
872 err = -ENOMEM;
873 goto err_buf;
874 }
Eli Cohene126ba92013-07-07 17:25:49 +0300875
Saeed Mahameed27827782016-07-16 02:33:22 +0300876 pas = (__be64 *)MLX5_ADDR_OF(create_cq_in, *cqb, pas);
877 mlx5_fill_page_array(&cq->buf.buf, pas);
878
879 cqc = MLX5_ADDR_OF(create_cq_in, *cqb, cq_context);
880 MLX5_SET(cqc, cqc, log_page_size,
881 cq->buf.buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
882
Jack Morgenstein9603b612014-07-28 23:30:22 +0300883 *index = dev->mdev->priv.uuari.uars[0].index;
Eli Cohene126ba92013-07-07 17:25:49 +0300884
885 return 0;
886
887err_buf:
888 free_cq_buf(dev, &cq->buf);
889
890err_db:
Jack Morgenstein9603b612014-07-28 23:30:22 +0300891 mlx5_db_free(dev->mdev, &cq->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300892 return err;
893}
894
895static void destroy_cq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq)
896{
897 free_cq_buf(dev, &cq->buf);
Jack Morgenstein9603b612014-07-28 23:30:22 +0300898 mlx5_db_free(dev->mdev, &cq->db);
Eli Cohene126ba92013-07-07 17:25:49 +0300899}
900
Haggai Eran25361e02016-02-29 15:45:08 +0200901static void notify_soft_wc_handler(struct work_struct *work)
902{
903 struct mlx5_ib_cq *cq = container_of(work, struct mlx5_ib_cq,
904 notify_work);
905
906 cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
907}
908
Matan Barakbcf4c1e2015-06-11 16:35:20 +0300909struct ib_cq *mlx5_ib_create_cq(struct ib_device *ibdev,
910 const struct ib_cq_init_attr *attr,
911 struct ib_ucontext *context,
Eli Cohene126ba92013-07-07 17:25:49 +0300912 struct ib_udata *udata)
913{
Matan Barakbcf4c1e2015-06-11 16:35:20 +0300914 int entries = attr->cqe;
915 int vector = attr->comp_vector;
Eli Cohene126ba92013-07-07 17:25:49 +0300916 struct mlx5_ib_dev *dev = to_mdev(ibdev);
917 struct mlx5_ib_cq *cq;
918 int uninitialized_var(index);
919 int uninitialized_var(inlen);
Saeed Mahameed27827782016-07-16 02:33:22 +0300920 u32 *cqb = NULL;
921 void *cqc;
Eli Cohene126ba92013-07-07 17:25:49 +0300922 int cqe_size;
Doron Tsur0b6e26c2016-01-17 11:25:47 +0200923 unsigned int irqn;
Eli Cohene126ba92013-07-07 17:25:49 +0300924 int eqn;
925 int err;
926
Noa Osherovich9ea57852016-06-04 15:15:34 +0300927 if (entries < 0 ||
928 (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz))))
Eli Cohen51ee86a2013-10-23 09:53:13 +0300929 return ERR_PTR(-EINVAL);
930
Leon Romanovsky34356f62015-12-29 17:01:30 +0200931 if (check_cq_create_flags(attr->flags))
Matan Barak972ecb82015-12-15 20:30:09 +0200932 return ERR_PTR(-EOPNOTSUPP);
933
Eli Cohene126ba92013-07-07 17:25:49 +0300934 entries = roundup_pow_of_two(entries + 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300935 if (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz)))
Eli Cohene126ba92013-07-07 17:25:49 +0300936 return ERR_PTR(-EINVAL);
937
938 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
939 if (!cq)
940 return ERR_PTR(-ENOMEM);
941
942 cq->ibcq.cqe = entries - 1;
943 mutex_init(&cq->resize_mutex);
944 spin_lock_init(&cq->lock);
945 cq->resize_buf = NULL;
946 cq->resize_umem = NULL;
Leon Romanovsky051f2632015-12-20 12:16:11 +0200947 cq->create_flags = attr->flags;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +0300948 INIT_LIST_HEAD(&cq->list_send_qp);
949 INIT_LIST_HEAD(&cq->list_recv_qp);
Eli Cohene126ba92013-07-07 17:25:49 +0300950
951 if (context) {
952 err = create_cq_user(dev, udata, context, cq, entries,
953 &cqb, &cqe_size, &index, &inlen);
954 if (err)
955 goto err_create;
956 } else {
Daniel Jurgens16b0e062016-10-27 16:36:41 +0300957 cqe_size = cache_line_size() == 128 ? 128 : 64;
Eli Cohene126ba92013-07-07 17:25:49 +0300958 err = create_cq_kernel(dev, cq, entries, cqe_size, &cqb,
959 &index, &inlen);
960 if (err)
961 goto err_create;
Haggai Eran25361e02016-02-29 15:45:08 +0200962
963 INIT_WORK(&cq->notify_work, notify_soft_wc_handler);
Eli Cohene126ba92013-07-07 17:25:49 +0300964 }
965
Saeed Mahameed233d05d2015-04-02 17:07:32 +0300966 err = mlx5_vector2eqn(dev->mdev, vector, &eqn, &irqn);
Eli Cohene126ba92013-07-07 17:25:49 +0300967 if (err)
968 goto err_cqb;
969
Saeed Mahameed27827782016-07-16 02:33:22 +0300970 cq->cqe_size = cqe_size;
971
972 cqc = MLX5_ADDR_OF(create_cq_in, cqb, cq_context);
973 MLX5_SET(cqc, cqc, cqe_sz, cqe_sz_to_mlx_sz(cqe_size));
974 MLX5_SET(cqc, cqc, log_cq_size, ilog2(entries));
975 MLX5_SET(cqc, cqc, uar_page, index);
976 MLX5_SET(cqc, cqc, c_eqn, eqn);
977 MLX5_SET64(cqc, cqc, dbr_addr, cq->db.dma);
978 if (cq->create_flags & IB_CQ_FLAGS_IGNORE_OVERRUN)
979 MLX5_SET(cqc, cqc, oi, 1);
Eli Cohene126ba92013-07-07 17:25:49 +0300980
Jack Morgenstein9603b612014-07-28 23:30:22 +0300981 err = mlx5_core_create_cq(dev->mdev, &cq->mcq, cqb, inlen);
Eli Cohene126ba92013-07-07 17:25:49 +0300982 if (err)
983 goto err_cqb;
984
985 mlx5_ib_dbg(dev, "cqn 0x%x\n", cq->mcq.cqn);
986 cq->mcq.irqn = irqn;
Matan Barakc16d2752016-04-17 17:08:41 +0300987 if (context)
988 cq->mcq.tasklet_ctx.comp = mlx5_ib_cq_comp;
989 else
990 cq->mcq.comp = mlx5_ib_cq_comp;
Eli Cohene126ba92013-07-07 17:25:49 +0300991 cq->mcq.event = mlx5_ib_cq_event;
992
Haggai Eran25361e02016-02-29 15:45:08 +0200993 INIT_LIST_HEAD(&cq->wc_list);
994
Eli Cohene126ba92013-07-07 17:25:49 +0300995 if (context)
996 if (ib_copy_to_udata(udata, &cq->mcq.cqn, sizeof(__u32))) {
997 err = -EFAULT;
998 goto err_cmd;
999 }
1000
1001
Al Viro479163f2014-11-20 08:13:57 +00001002 kvfree(cqb);
Eli Cohene126ba92013-07-07 17:25:49 +03001003 return &cq->ibcq;
1004
1005err_cmd:
Jack Morgenstein9603b612014-07-28 23:30:22 +03001006 mlx5_core_destroy_cq(dev->mdev, &cq->mcq);
Eli Cohene126ba92013-07-07 17:25:49 +03001007
1008err_cqb:
Al Viro479163f2014-11-20 08:13:57 +00001009 kvfree(cqb);
Eli Cohene126ba92013-07-07 17:25:49 +03001010 if (context)
1011 destroy_cq_user(cq, context);
1012 else
1013 destroy_cq_kernel(dev, cq);
1014
1015err_create:
1016 kfree(cq);
1017
1018 return ERR_PTR(err);
1019}
1020
1021
1022int mlx5_ib_destroy_cq(struct ib_cq *cq)
1023{
1024 struct mlx5_ib_dev *dev = to_mdev(cq->device);
1025 struct mlx5_ib_cq *mcq = to_mcq(cq);
1026 struct ib_ucontext *context = NULL;
1027
1028 if (cq->uobject)
1029 context = cq->uobject->context;
1030
Jack Morgenstein9603b612014-07-28 23:30:22 +03001031 mlx5_core_destroy_cq(dev->mdev, &mcq->mcq);
Eli Cohene126ba92013-07-07 17:25:49 +03001032 if (context)
1033 destroy_cq_user(mcq, context);
1034 else
1035 destroy_cq_kernel(dev, mcq);
1036
1037 kfree(mcq);
1038
1039 return 0;
1040}
1041
Moshe Lazercfd8f1d2013-10-23 09:53:17 +03001042static int is_equal_rsn(struct mlx5_cqe64 *cqe64, u32 rsn)
Eli Cohene126ba92013-07-07 17:25:49 +03001043{
Moshe Lazercfd8f1d2013-10-23 09:53:17 +03001044 return rsn == (ntohl(cqe64->sop_drop_qpn) & 0xffffff);
Eli Cohene126ba92013-07-07 17:25:49 +03001045}
1046
1047void __mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 rsn, struct mlx5_ib_srq *srq)
1048{
1049 struct mlx5_cqe64 *cqe64, *dest64;
1050 void *cqe, *dest;
1051 u32 prod_index;
1052 int nfreed = 0;
1053 u8 owner_bit;
1054
1055 if (!cq)
1056 return;
1057
1058 /* First we need to find the current producer index, so we
1059 * know where to start cleaning from. It doesn't matter if HW
1060 * adds new entries after this loop -- the QP we're worried
1061 * about is already in RESET, so the new entries won't come
1062 * from our QP and therefore don't need to be checked.
1063 */
1064 for (prod_index = cq->mcq.cons_index; get_sw_cqe(cq, prod_index); prod_index++)
1065 if (prod_index == cq->mcq.cons_index + cq->ibcq.cqe)
1066 break;
1067
1068 /* Now sweep backwards through the CQ, removing CQ entries
1069 * that match our QP by copying older entries on top of them.
1070 */
1071 while ((int) --prod_index - (int) cq->mcq.cons_index >= 0) {
1072 cqe = get_cqe(cq, prod_index & cq->ibcq.cqe);
1073 cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
Moshe Lazercfd8f1d2013-10-23 09:53:17 +03001074 if (is_equal_rsn(cqe64, rsn)) {
1075 if (srq && (ntohl(cqe64->srqn) & 0xffffff))
Eli Cohene126ba92013-07-07 17:25:49 +03001076 mlx5_ib_free_srq_wqe(srq, be16_to_cpu(cqe64->wqe_counter));
1077 ++nfreed;
1078 } else if (nfreed) {
1079 dest = get_cqe(cq, (prod_index + nfreed) & cq->ibcq.cqe);
1080 dest64 = (cq->mcq.cqe_sz == 64) ? dest : dest + 64;
1081 owner_bit = dest64->op_own & MLX5_CQE_OWNER_MASK;
1082 memcpy(dest, cqe, cq->mcq.cqe_sz);
1083 dest64->op_own = owner_bit |
1084 (dest64->op_own & ~MLX5_CQE_OWNER_MASK);
1085 }
1086 }
1087
1088 if (nfreed) {
1089 cq->mcq.cons_index += nfreed;
1090 /* Make sure update of buffer contents is done before
1091 * updating consumer index.
1092 */
1093 wmb();
1094 mlx5_cq_set_ci(&cq->mcq);
1095 }
1096}
1097
1098void mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 qpn, struct mlx5_ib_srq *srq)
1099{
1100 if (!cq)
1101 return;
1102
1103 spin_lock_irq(&cq->lock);
1104 __mlx5_ib_cq_clean(cq, qpn, srq);
1105 spin_unlock_irq(&cq->lock);
1106}
1107
1108int mlx5_ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
1109{
Eli Cohen3bdb31f2014-01-14 17:45:17 +02001110 struct mlx5_ib_dev *dev = to_mdev(cq->device);
1111 struct mlx5_ib_cq *mcq = to_mcq(cq);
1112 int err;
Eli Cohen3bdb31f2014-01-14 17:45:17 +02001113
Saeed Mahameed938fe832015-05-28 22:28:41 +03001114 if (!MLX5_CAP_GEN(dev->mdev, cq_moderation))
Eli Cohen3bdb31f2014-01-14 17:45:17 +02001115 return -ENOSYS;
1116
Saeed Mahameed27827782016-07-16 02:33:22 +03001117 err = mlx5_core_modify_cq_moderation(dev->mdev, &mcq->mcq,
1118 cq_period, cq_count);
Eli Cohen3bdb31f2014-01-14 17:45:17 +02001119 if (err)
1120 mlx5_ib_warn(dev, "modify cq 0x%x failed\n", mcq->mcq.cqn);
1121
1122 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001123}
1124
Eli Cohenbde51582014-01-14 17:45:18 +02001125static int resize_user(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
1126 int entries, struct ib_udata *udata, int *npas,
1127 int *page_shift, int *cqe_size)
1128{
1129 struct mlx5_ib_resize_cq ucmd;
1130 struct ib_umem *umem;
1131 int err;
1132 int npages;
1133 struct ib_ucontext *context = cq->buf.umem->context;
1134
Eli Cohen57761d82014-01-15 14:56:44 +02001135 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
1136 if (err)
1137 return err;
1138
1139 if (ucmd.reserved0 || ucmd.reserved1)
1140 return -EINVAL;
Eli Cohenbde51582014-01-14 17:45:18 +02001141
Leon Romanovsky1bb45452018-03-07 15:29:09 +02001142 /* check multiplication overflow */
1143 if (ucmd.cqe_size && SIZE_MAX / ucmd.cqe_size <= entries - 1)
1144 return -EINVAL;
1145
1146 umem = ib_umem_get(context, ucmd.buf_addr,
1147 (size_t)ucmd.cqe_size * entries,
Eli Cohenbde51582014-01-14 17:45:18 +02001148 IB_ACCESS_LOCAL_WRITE, 1);
1149 if (IS_ERR(umem)) {
1150 err = PTR_ERR(umem);
1151 return err;
1152 }
1153
1154 mlx5_ib_cont_pages(umem, ucmd.buf_addr, &npages, page_shift,
1155 npas, NULL);
1156
1157 cq->resize_umem = umem;
1158 *cqe_size = ucmd.cqe_size;
1159
1160 return 0;
1161}
1162
1163static void un_resize_user(struct mlx5_ib_cq *cq)
1164{
1165 ib_umem_release(cq->resize_umem);
1166}
1167
1168static int resize_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
1169 int entries, int cqe_size)
1170{
1171 int err;
1172
1173 cq->resize_buf = kzalloc(sizeof(*cq->resize_buf), GFP_KERNEL);
1174 if (!cq->resize_buf)
1175 return -ENOMEM;
1176
1177 err = alloc_cq_buf(dev, cq->resize_buf, entries, cqe_size);
1178 if (err)
1179 goto ex;
1180
1181 init_cq_buf(cq, cq->resize_buf);
1182
1183 return 0;
1184
1185ex:
1186 kfree(cq->resize_buf);
1187 return err;
1188}
1189
1190static void un_resize_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq)
1191{
1192 free_cq_buf(dev, cq->resize_buf);
1193 cq->resize_buf = NULL;
1194}
1195
1196static int copy_resize_cqes(struct mlx5_ib_cq *cq)
1197{
1198 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
1199 struct mlx5_cqe64 *scqe64;
1200 struct mlx5_cqe64 *dcqe64;
1201 void *start_cqe;
1202 void *scqe;
1203 void *dcqe;
1204 int ssize;
1205 int dsize;
1206 int i;
1207 u8 sw_own;
1208
1209 ssize = cq->buf.cqe_size;
1210 dsize = cq->resize_buf->cqe_size;
1211 if (ssize != dsize) {
1212 mlx5_ib_warn(dev, "resize from different cqe size is not supported\n");
1213 return -EINVAL;
1214 }
1215
1216 i = cq->mcq.cons_index;
1217 scqe = get_sw_cqe(cq, i);
1218 scqe64 = ssize == 64 ? scqe : scqe + 64;
1219 start_cqe = scqe;
1220 if (!scqe) {
1221 mlx5_ib_warn(dev, "expected cqe in sw ownership\n");
1222 return -EINVAL;
1223 }
1224
1225 while ((scqe64->op_own >> 4) != MLX5_CQE_RESIZE_CQ) {
1226 dcqe = get_cqe_from_buf(cq->resize_buf,
1227 (i + 1) & (cq->resize_buf->nent),
1228 dsize);
1229 dcqe64 = dsize == 64 ? dcqe : dcqe + 64;
1230 sw_own = sw_ownership_bit(i + 1, cq->resize_buf->nent);
1231 memcpy(dcqe, scqe, dsize);
1232 dcqe64->op_own = (dcqe64->op_own & ~MLX5_CQE_OWNER_MASK) | sw_own;
1233
1234 ++i;
1235 scqe = get_sw_cqe(cq, i);
1236 scqe64 = ssize == 64 ? scqe : scqe + 64;
1237 if (!scqe) {
1238 mlx5_ib_warn(dev, "expected cqe in sw ownership\n");
1239 return -EINVAL;
1240 }
1241
1242 if (scqe == start_cqe) {
1243 pr_warn("resize CQ failed to get resize CQE, CQN 0x%x\n",
1244 cq->mcq.cqn);
1245 return -ENOMEM;
1246 }
1247 }
1248 ++cq->mcq.cons_index;
1249 return 0;
1250}
1251
Eli Cohene126ba92013-07-07 17:25:49 +03001252int mlx5_ib_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
1253{
Eli Cohenbde51582014-01-14 17:45:18 +02001254 struct mlx5_ib_dev *dev = to_mdev(ibcq->device);
1255 struct mlx5_ib_cq *cq = to_mcq(ibcq);
Saeed Mahameed27827782016-07-16 02:33:22 +03001256 void *cqc;
1257 u32 *in;
Eli Cohenbde51582014-01-14 17:45:18 +02001258 int err;
1259 int npas;
Saeed Mahameed27827782016-07-16 02:33:22 +03001260 __be64 *pas;
Eli Cohenbde51582014-01-14 17:45:18 +02001261 int page_shift;
1262 int inlen;
1263 int uninitialized_var(cqe_size);
1264 unsigned long flags;
1265
Saeed Mahameed938fe832015-05-28 22:28:41 +03001266 if (!MLX5_CAP_GEN(dev->mdev, cq_resize)) {
Eli Cohenbde51582014-01-14 17:45:18 +02001267 pr_info("Firmware does not support resize CQ\n");
1268 return -ENOSYS;
1269 }
1270
Noa Osherovich3c4c3772016-06-04 15:15:35 +03001271 if (entries < 1 ||
1272 entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz))) {
1273 mlx5_ib_warn(dev, "wrong entries number %d, max %d\n",
1274 entries,
1275 1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz));
Eli Cohenbde51582014-01-14 17:45:18 +02001276 return -EINVAL;
Noa Osherovich3c4c3772016-06-04 15:15:35 +03001277 }
Eli Cohenbde51582014-01-14 17:45:18 +02001278
1279 entries = roundup_pow_of_two(entries + 1);
Noa Osherovich3c4c3772016-06-04 15:15:35 +03001280 if (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz)) + 1)
Eli Cohenbde51582014-01-14 17:45:18 +02001281 return -EINVAL;
1282
1283 if (entries == ibcq->cqe + 1)
1284 return 0;
1285
1286 mutex_lock(&cq->resize_mutex);
1287 if (udata) {
1288 err = resize_user(dev, cq, entries, udata, &npas, &page_shift,
1289 &cqe_size);
1290 } else {
1291 cqe_size = 64;
1292 err = resize_kernel(dev, cq, entries, cqe_size);
1293 if (!err) {
1294 npas = cq->resize_buf->buf.npages;
1295 page_shift = cq->resize_buf->buf.page_shift;
1296 }
1297 }
1298
1299 if (err)
1300 goto ex;
1301
Saeed Mahameed27827782016-07-16 02:33:22 +03001302 inlen = MLX5_ST_SZ_BYTES(modify_cq_in) +
1303 MLX5_FLD_SZ_BYTES(modify_cq_in, pas[0]) * npas;
1304
Eli Cohenbde51582014-01-14 17:45:18 +02001305 in = mlx5_vzalloc(inlen);
1306 if (!in) {
1307 err = -ENOMEM;
1308 goto ex_resize;
1309 }
1310
Saeed Mahameed27827782016-07-16 02:33:22 +03001311 pas = (__be64 *)MLX5_ADDR_OF(modify_cq_in, in, pas);
Eli Cohenbde51582014-01-14 17:45:18 +02001312 if (udata)
1313 mlx5_ib_populate_pas(dev, cq->resize_umem, page_shift,
Saeed Mahameed27827782016-07-16 02:33:22 +03001314 pas, 0);
Eli Cohenbde51582014-01-14 17:45:18 +02001315 else
Saeed Mahameed27827782016-07-16 02:33:22 +03001316 mlx5_fill_page_array(&cq->resize_buf->buf, pas);
Eli Cohenbde51582014-01-14 17:45:18 +02001317
Saeed Mahameed27827782016-07-16 02:33:22 +03001318 MLX5_SET(modify_cq_in, in,
1319 modify_field_select_resize_field_select.resize_field_select.resize_field_select,
1320 MLX5_MODIFY_CQ_MASK_LOG_SIZE |
1321 MLX5_MODIFY_CQ_MASK_PG_OFFSET |
1322 MLX5_MODIFY_CQ_MASK_PG_SIZE);
1323
1324 cqc = MLX5_ADDR_OF(modify_cq_in, in, cq_context);
1325
1326 MLX5_SET(cqc, cqc, log_page_size,
1327 page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1328 MLX5_SET(cqc, cqc, cqe_sz, cqe_sz_to_mlx_sz(cqe_size));
1329 MLX5_SET(cqc, cqc, log_cq_size, ilog2(entries));
1330
1331 MLX5_SET(modify_cq_in, in, op_mod, MLX5_CQ_OPMOD_RESIZE);
1332 MLX5_SET(modify_cq_in, in, cqn, cq->mcq.cqn);
Eli Cohenbde51582014-01-14 17:45:18 +02001333
Jack Morgenstein9603b612014-07-28 23:30:22 +03001334 err = mlx5_core_modify_cq(dev->mdev, &cq->mcq, in, inlen);
Eli Cohenbde51582014-01-14 17:45:18 +02001335 if (err)
1336 goto ex_alloc;
1337
1338 if (udata) {
1339 cq->ibcq.cqe = entries - 1;
1340 ib_umem_release(cq->buf.umem);
1341 cq->buf.umem = cq->resize_umem;
1342 cq->resize_umem = NULL;
1343 } else {
1344 struct mlx5_ib_cq_buf tbuf;
1345 int resized = 0;
1346
1347 spin_lock_irqsave(&cq->lock, flags);
1348 if (cq->resize_buf) {
1349 err = copy_resize_cqes(cq);
1350 if (!err) {
1351 tbuf = cq->buf;
1352 cq->buf = *cq->resize_buf;
1353 kfree(cq->resize_buf);
1354 cq->resize_buf = NULL;
1355 resized = 1;
1356 }
1357 }
1358 cq->ibcq.cqe = entries - 1;
1359 spin_unlock_irqrestore(&cq->lock, flags);
1360 if (resized)
1361 free_cq_buf(dev, &tbuf);
1362 }
1363 mutex_unlock(&cq->resize_mutex);
1364
Al Viro479163f2014-11-20 08:13:57 +00001365 kvfree(in);
Eli Cohenbde51582014-01-14 17:45:18 +02001366 return 0;
1367
1368ex_alloc:
Al Viro479163f2014-11-20 08:13:57 +00001369 kvfree(in);
Eli Cohenbde51582014-01-14 17:45:18 +02001370
1371ex_resize:
1372 if (udata)
1373 un_resize_user(cq);
1374 else
1375 un_resize_kernel(dev, cq);
1376ex:
1377 mutex_unlock(&cq->resize_mutex);
1378 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001379}
1380
1381int mlx5_ib_get_cqe_size(struct mlx5_ib_dev *dev, struct ib_cq *ibcq)
1382{
1383 struct mlx5_ib_cq *cq;
1384
1385 if (!ibcq)
1386 return 128;
1387
1388 cq = to_mcq(ibcq);
1389 return cq->cqe_size;
1390}
Haggai Eran25361e02016-02-29 15:45:08 +02001391
1392/* Called from atomic context */
1393int mlx5_ib_generate_wc(struct ib_cq *ibcq, struct ib_wc *wc)
1394{
1395 struct mlx5_ib_wc *soft_wc;
1396 struct mlx5_ib_cq *cq = to_mcq(ibcq);
1397 unsigned long flags;
1398
1399 soft_wc = kmalloc(sizeof(*soft_wc), GFP_ATOMIC);
1400 if (!soft_wc)
1401 return -ENOMEM;
1402
1403 soft_wc->wc = *wc;
1404 spin_lock_irqsave(&cq->lock, flags);
1405 list_add_tail(&soft_wc->list, &cq->wc_list);
1406 if (cq->notify_flags == IB_CQ_NEXT_COMP ||
1407 wc->status != IB_WC_SUCCESS) {
1408 cq->notify_flags = 0;
1409 schedule_work(&cq->notify_work);
1410 }
1411 spin_unlock_irqrestore(&cq->lock, flags);
1412
1413 return 0;
1414}