2 * Copyright (c) 2013-2015, Mellanox Technologies, Ltd. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <linux/compiler.h>
29 #include <linux/kref.h>
30 #include <rdma/ib_umem.h>
31 #include <rdma/ib_user_verbs.h>
35 static void mlx5_ib_cq_comp(struct mlx5_core_cq *cq)
37 struct ib_cq *ibcq = &to_mibcq(cq)->ibcq;
39 ibcq->comp_handler(ibcq, ibcq->cq_context);
42 static void mlx5_ib_cq_event(struct mlx5_core_cq *mcq, int type)
44 struct mlx5_ib_cq *cq = container_of(mcq, struct mlx5_ib_cq, mcq);
45 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
46 struct ib_cq *ibcq = &cq->ibcq;
47 struct ib_event event;
49 if (type != MLX5_EVENT_TYPE_CQ_ERROR) {
50 mlx5_ib_warn(dev, "Unexpected event type %d on CQ %06x\n",
55 if (ibcq->event_handler) {
56 event.device = &dev->ib_dev;
57 event.event = IB_EVENT_CQ_ERR;
58 event.element.cq = ibcq;
59 ibcq->event_handler(&event, ibcq->cq_context);
63 static void *get_cqe_from_buf(struct mlx5_ib_cq_buf *buf, int n, int size)
65 return mlx5_buf_offset(&buf->buf, n * size);
68 static void *get_cqe(struct mlx5_ib_cq *cq, int n)
70 return get_cqe_from_buf(&cq->buf, n, cq->mcq.cqe_sz);
73 static u8 sw_ownership_bit(int n, int nent)
75 return (n & nent) ? 1 : 0;
78 static void *get_sw_cqe(struct mlx5_ib_cq *cq, int n)
80 void *cqe = get_cqe(cq, n & cq->ibcq.cqe);
81 struct mlx5_cqe64 *cqe64;
83 cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
85 if (likely((cqe64->op_own) >> 4 != MLX5_CQE_INVALID) &&
86 !((cqe64->op_own & MLX5_CQE_OWNER_MASK) ^ !!(n & (cq->ibcq.cqe + 1)))) {
93 static void *next_cqe_sw(struct mlx5_ib_cq *cq)
95 return get_sw_cqe(cq, cq->mcq.cons_index);
98 static enum ib_wc_opcode get_umr_comp(struct mlx5_ib_wq *wq, int idx)
100 switch (wq->swr_ctx[idx].wr_data) {
101 case IB_WR_LOCAL_INV:
102 return IB_WC_LOCAL_INV;
104 case IB_WR_FAST_REG_MR:
105 return IB_WC_FAST_REG_MR;
108 printf("mlx5_ib: WARN: ""unknown completion status\n");
113 static void handle_good_req(struct ib_wc *wc, struct mlx5_cqe64 *cqe,
114 struct mlx5_ib_wq *wq, int idx)
117 switch (be32_to_cpu(cqe->sop_drop_qpn) >> 24) {
118 case MLX5_OPCODE_RDMA_WRITE_IMM:
119 wc->wc_flags |= IB_WC_WITH_IMM;
120 case MLX5_OPCODE_RDMA_WRITE:
121 wc->opcode = IB_WC_RDMA_WRITE;
123 case MLX5_OPCODE_SEND_IMM:
124 wc->wc_flags |= IB_WC_WITH_IMM;
125 case MLX5_OPCODE_NOP:
126 case MLX5_OPCODE_SEND:
127 case MLX5_OPCODE_SEND_INVAL:
128 wc->opcode = IB_WC_SEND;
130 case MLX5_OPCODE_RDMA_READ:
131 wc->opcode = IB_WC_RDMA_READ;
132 wc->byte_len = be32_to_cpu(cqe->byte_cnt);
134 case MLX5_OPCODE_ATOMIC_CS:
135 wc->opcode = IB_WC_COMP_SWAP;
138 case MLX5_OPCODE_ATOMIC_FA:
139 wc->opcode = IB_WC_FETCH_ADD;
142 case MLX5_OPCODE_ATOMIC_MASKED_CS:
143 wc->opcode = IB_WC_MASKED_COMP_SWAP;
146 case MLX5_OPCODE_ATOMIC_MASKED_FA:
147 wc->opcode = IB_WC_MASKED_FETCH_ADD;
150 case MLX5_OPCODE_BIND_MW:
151 wc->opcode = IB_WC_BIND_MW;
153 case MLX5_OPCODE_UMR:
154 wc->opcode = get_umr_comp(wq, idx);
160 MLX5_GRH_IN_BUFFER = 1,
164 static void handle_responder(struct ib_wc *wc, struct mlx5_cqe64 *cqe,
165 struct mlx5_ib_qp *qp)
167 struct mlx5_ib_dev *dev = to_mdev(qp->ibqp.device);
168 struct mlx5_ib_srq *srq;
169 struct mlx5_ib_wq *wq;
172 #if defined(DX_ROCE_V1_5) || defined(DX_WINDOWS)
176 if (qp->ibqp.srq || qp->ibqp.xrcd) {
177 struct mlx5_core_srq *msrq = NULL;
180 msrq = mlx5_core_get_srq(dev->mdev,
181 be32_to_cpu(cqe->srqn));
182 srq = to_mibsrq(msrq);
184 srq = to_msrq(qp->ibqp.srq);
187 wqe_ctr = be16_to_cpu(cqe->wqe_counter);
188 wc->wr_id = srq->wrid[wqe_ctr];
189 mlx5_ib_free_srq_wqe(srq, wqe_ctr);
190 if (msrq && atomic_dec_and_test(&msrq->refcount))
191 complete(&msrq->free);
195 wc->wr_id = wq->rwr_ctx[wq->tail & (wq->wqe_cnt - 1)].wrid;
198 wc->byte_len = be32_to_cpu(cqe->byte_cnt);
200 switch (cqe->op_own >> 4) {
201 case MLX5_CQE_RESP_WR_IMM:
202 wc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
203 wc->wc_flags = IB_WC_WITH_IMM;
204 wc->ex.imm_data = cqe->imm_inval_pkey;
206 case MLX5_CQE_RESP_SEND:
207 wc->opcode = IB_WC_RECV;
210 case MLX5_CQE_RESP_SEND_IMM:
211 wc->opcode = IB_WC_RECV;
212 wc->wc_flags = IB_WC_WITH_IMM;
213 wc->ex.imm_data = cqe->imm_inval_pkey;
215 case MLX5_CQE_RESP_SEND_INV:
216 wc->opcode = IB_WC_RECV;
217 wc->wc_flags = IB_WC_WITH_INVALIDATE;
218 wc->ex.invalidate_rkey = be32_to_cpu(cqe->imm_inval_pkey);
221 wc->slid = be16_to_cpu(cqe->slid);
222 wc->sl = (be32_to_cpu(cqe->flags_rqpn) >> 24) & 0xf;
223 wc->src_qp = be32_to_cpu(cqe->flags_rqpn) & 0xffffff;
224 wc->dlid_path_bits = cqe->ml_path;
225 g = (be32_to_cpu(cqe->flags_rqpn) >> 28) & 3;
226 wc->wc_flags |= g ? IB_WC_GRH : 0;
227 wc->pkey_index = be32_to_cpu(cqe->imm_inval_pkey) & 0xffff;
229 #if defined(DX_ROCE_V1_5) || defined(DX_WINDOWS)
230 udp_header_valid = wc->sl & 0x8;
231 if (udp_header_valid)
232 wc->wc_flags |= IB_WC_WITH_UDP_HDR;
237 static void dump_cqe(struct mlx5_ib_dev *dev, struct mlx5_err_cqe *cqe)
239 __be32 *p = (__be32 *)cqe;
242 mlx5_ib_warn(dev, "dump error cqe\n");
243 for (i = 0; i < sizeof(*cqe) / 16; i++, p += 4)
244 printf("mlx5_ib: INFO: ""%08x %08x %08x %08x\n", be32_to_cpu(p[0]), be32_to_cpu(p[1]), be32_to_cpu(p[2]), be32_to_cpu(p[3]));
247 static void mlx5_handle_error_cqe(struct mlx5_ib_dev *dev,
248 struct mlx5_err_cqe *cqe,
253 switch (cqe->syndrome) {
254 case MLX5_CQE_SYNDROME_LOCAL_LENGTH_ERR:
255 wc->status = IB_WC_LOC_LEN_ERR;
257 case MLX5_CQE_SYNDROME_LOCAL_QP_OP_ERR:
258 wc->status = IB_WC_LOC_QP_OP_ERR;
260 case MLX5_CQE_SYNDROME_LOCAL_PROT_ERR:
261 wc->status = IB_WC_LOC_PROT_ERR;
263 case MLX5_CQE_SYNDROME_WR_FLUSH_ERR:
265 wc->status = IB_WC_WR_FLUSH_ERR;
267 case MLX5_CQE_SYNDROME_MW_BIND_ERR:
268 wc->status = IB_WC_MW_BIND_ERR;
270 case MLX5_CQE_SYNDROME_BAD_RESP_ERR:
271 wc->status = IB_WC_BAD_RESP_ERR;
273 case MLX5_CQE_SYNDROME_LOCAL_ACCESS_ERR:
274 wc->status = IB_WC_LOC_ACCESS_ERR;
276 case MLX5_CQE_SYNDROME_REMOTE_INVAL_REQ_ERR:
277 wc->status = IB_WC_REM_INV_REQ_ERR;
279 case MLX5_CQE_SYNDROME_REMOTE_ACCESS_ERR:
280 wc->status = IB_WC_REM_ACCESS_ERR;
282 case MLX5_CQE_SYNDROME_REMOTE_OP_ERR:
283 wc->status = IB_WC_REM_OP_ERR;
285 case MLX5_CQE_SYNDROME_TRANSPORT_RETRY_EXC_ERR:
286 wc->status = IB_WC_RETRY_EXC_ERR;
289 case MLX5_CQE_SYNDROME_RNR_RETRY_EXC_ERR:
290 wc->status = IB_WC_RNR_RETRY_EXC_ERR;
293 case MLX5_CQE_SYNDROME_REMOTE_ABORTED_ERR:
294 wc->status = IB_WC_REM_ABORT_ERR;
297 wc->status = IB_WC_GENERAL_ERR;
301 wc->vendor_err = cqe->vendor_err_synd;
306 static int is_atomic_response(struct mlx5_ib_qp *qp, u16 idx)
308 /* TBD: waiting decision
313 static void *mlx5_get_atomic_laddr(struct mlx5_ib_qp *qp, u16 idx)
315 struct mlx5_wqe_data_seg *dpseg;
318 dpseg = mlx5_get_send_wqe(qp, idx) + sizeof(struct mlx5_wqe_ctrl_seg) +
319 sizeof(struct mlx5_wqe_raddr_seg) +
320 sizeof(struct mlx5_wqe_atomic_seg);
321 addr = (void *)(uintptr_t)be64_to_cpu(dpseg->addr);
325 static void handle_atomic(struct mlx5_ib_qp *qp, struct mlx5_cqe64 *cqe64,
332 if (!is_atomic_response(qp, idx))
335 byte_count = be32_to_cpu(cqe64->byte_cnt);
336 addr = mlx5_get_atomic_laddr(qp, idx);
338 if (byte_count == 4) {
339 *(u32 *)addr = be32_to_cpu(*((__be32 *)addr));
341 for (i = 0; i < byte_count; i += 8) {
342 *(u64 *)addr = be64_to_cpu(*((__be64 *)addr));
350 static void handle_atomics(struct mlx5_ib_qp *qp, struct mlx5_cqe64 *cqe64,
356 idx = tail & (qp->sq.wqe_cnt - 1);
357 handle_atomic(qp, cqe64, idx);
361 tail = qp->sq.swr_ctx[idx].w_list.next;
363 tail = qp->sq.swr_ctx[idx].w_list.next;
364 qp->sq.last_poll = tail;
367 static void free_cq_buf(struct mlx5_ib_dev *dev, struct mlx5_ib_cq_buf *buf)
369 mlx5_buf_free(dev->mdev, &buf->buf);
372 static void sw_send_comp(struct mlx5_ib_qp *qp, int num_entries,
373 struct ib_wc *wc, int *npolled)
375 struct mlx5_ib_wq *wq;
382 cur = wq->head - wq->tail;
388 for (i = 0; i < cur && np < num_entries; i++) {
389 idx = wq->last_poll & (wq->wqe_cnt - 1);
390 wc->wr_id = wq->swr_ctx[idx].wrid;
391 wc->status = IB_WC_WR_FLUSH_ERR;
392 wc->vendor_err = MLX5_CQE_SYNDROME_WR_FLUSH_ERR;
397 wq->last_poll = wq->swr_ctx[idx].w_list.next;
402 static void sw_recv_comp(struct mlx5_ib_qp *qp, int num_entries,
403 struct ib_wc *wc, int *npolled)
405 struct mlx5_ib_wq *wq;
411 cur = wq->head - wq->tail;
417 for (i = 0; i < cur && np < num_entries; i++) {
418 wc->wr_id = wq->rwr_ctx[wq->tail & (wq->wqe_cnt - 1)].wrid;
419 wc->status = IB_WC_WR_FLUSH_ERR;
420 wc->vendor_err = MLX5_CQE_SYNDROME_WR_FLUSH_ERR;
429 static void mlx5_ib_poll_sw_comp(struct mlx5_ib_cq *cq, int num_entries,
430 struct ib_wc *wc, int *npolled)
432 struct mlx5_ib_qp *qp;
435 /* Find uncompleted WQEs belonging to that cq and retrun mmics ones */
436 list_for_each_entry(qp, &cq->list_send_qp, cq_send_list) {
437 sw_send_comp(qp, num_entries, wc + *npolled, npolled);
438 if (*npolled >= num_entries)
442 list_for_each_entry(qp, &cq->list_recv_qp, cq_recv_list) {
443 sw_recv_comp(qp, num_entries, wc + *npolled, npolled);
444 if (*npolled >= num_entries)
449 static inline u32 mlx5_ib_base_mkey(const u32 key)
451 return key & 0xffffff00u;
454 static int mlx5_poll_one(struct mlx5_ib_cq *cq,
455 struct mlx5_ib_qp **cur_qp,
458 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
459 struct mlx5_err_cqe *err_cqe;
460 struct mlx5_cqe64 *cqe64;
461 struct mlx5_core_qp *mqp;
462 struct mlx5_ib_wq *wq;
463 struct mlx5_sig_err_cqe *sig_err_cqe;
464 struct mlx5_core_mr *mmr;
465 struct mlx5_ib_mr *mr;
474 cqe = next_cqe_sw(cq);
478 cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
480 ++cq->mcq.cons_index;
482 /* Make sure we read CQ entry contents after we've checked the
487 opcode = cqe64->op_own >> 4;
488 if (unlikely(opcode == MLX5_CQE_RESIZE_CQ)) {
489 if (likely(cq->resize_buf)) {
490 free_cq_buf(dev, &cq->buf);
491 cq->buf = *cq->resize_buf;
492 kfree(cq->resize_buf);
493 cq->resize_buf = NULL;
496 mlx5_ib_warn(dev, "unexpected resize cqe\n");
500 qpn = ntohl(cqe64->sop_drop_qpn) & 0xffffff;
501 if (!*cur_qp || (qpn != (*cur_qp)->ibqp.qp_num)) {
502 /* We do not have to take the QP table lock here,
503 * because CQs will be locked while QPs are removed
506 mqp = __mlx5_qp_lookup(dev->mdev, qpn);
507 if (unlikely(!mqp)) {
508 mlx5_ib_warn(dev, "CQE@CQ %06x for unknown QPN %6x\n",
513 *cur_qp = to_mibqp(mqp);
516 wc->qp = &(*cur_qp)->ibqp;
520 wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
521 idx = wqe_ctr & (wq->wqe_cnt - 1);
522 handle_good_req(wc, cqe64, wq, idx);
523 handle_atomics(*cur_qp, cqe64, wq->last_poll, idx);
524 wc->wr_id = wq->swr_ctx[idx].wrid;
525 wq->tail = wq->swr_ctx[idx].wqe_head + 1;
526 if (unlikely(wq->swr_ctx[idx].w_list.opcode &
527 MLX5_OPCODE_SIGNATURE_CANCELED))
528 wc->status = IB_WC_GENERAL_ERR;
530 wc->status = IB_WC_SUCCESS;
532 case MLX5_CQE_RESP_WR_IMM:
533 case MLX5_CQE_RESP_SEND:
534 case MLX5_CQE_RESP_SEND_IMM:
535 case MLX5_CQE_RESP_SEND_INV:
536 handle_responder(wc, cqe64, *cur_qp);
537 wc->status = IB_WC_SUCCESS;
539 case MLX5_CQE_RESIZE_CQ:
541 case MLX5_CQE_REQ_ERR:
542 case MLX5_CQE_RESP_ERR:
543 err_cqe = (struct mlx5_err_cqe *)cqe64;
544 mlx5_handle_error_cqe(dev, err_cqe, wc);
545 mlx5_ib_dbg(dev, "%s error cqe on cqn 0x%x:\n",
546 opcode == MLX5_CQE_REQ_ERR ?
547 "Requestor" : "Responder", cq->mcq.cqn);
548 mlx5_ib_dbg(dev, "syndrome 0x%x, vendor syndrome 0x%x\n",
549 err_cqe->syndrome, err_cqe->vendor_err_synd);
550 if (opcode == MLX5_CQE_REQ_ERR) {
552 wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
553 idx = wqe_ctr & (wq->wqe_cnt - 1);
554 wc->wr_id = wq->swr_ctx[idx].wrid;
555 wq->tail = wq->swr_ctx[idx].wqe_head + 1;
557 struct mlx5_ib_srq *srq;
559 if ((*cur_qp)->ibqp.srq) {
560 srq = to_msrq((*cur_qp)->ibqp.srq);
561 wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
562 wc->wr_id = srq->wrid[wqe_ctr];
563 mlx5_ib_free_srq_wqe(srq, wqe_ctr);
566 wc->wr_id = wq->rwr_ctx[wq->tail & (wq->wqe_cnt - 1)].wrid;
571 case MLX5_CQE_SIG_ERR:
572 sig_err_cqe = (struct mlx5_sig_err_cqe *)cqe64;
574 spin_lock_irqsave(&dev->mdev->priv.mr_table.lock, flags);
575 mmr = __mlx5_mr_lookup(dev->mdev,
576 mlx5_ib_base_mkey(be32_to_cpu(sig_err_cqe->mkey)));
577 if (unlikely(!mmr)) {
578 spin_unlock_irqrestore(&dev->mdev->priv.mr_table.lock, flags);
579 mlx5_ib_warn(dev, "CQE@CQ %06x for unknown MR %6x\n",
580 cq->mcq.cqn, be32_to_cpu(sig_err_cqe->mkey));
585 mr->sig->sig_err_exists = true;
586 mr->sig->sigerr_count++;
588 mlx5_ib_warn(dev, "CQN: 0x%x Got SIGERR\n", cq->mcq.cqn);
590 spin_unlock_irqrestore(&dev->mdev->priv.mr_table.lock, flags);
597 int mlx5_ib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
599 struct mlx5_ib_cq *cq = to_mcq(ibcq);
600 struct mlx5_ib_qp *cur_qp = NULL;
601 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
602 struct mlx5_core_dev *mdev = dev->mdev;
607 spin_lock_irqsave(&cq->lock, flags);
608 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
609 mlx5_ib_poll_sw_comp(cq, num_entries, wc, &npolled);
613 for (npolled = 0; npolled < num_entries; npolled++) {
614 err = mlx5_poll_one(cq, &cur_qp, wc + npolled);
620 mlx5_cq_set_ci(&cq->mcq);
622 spin_unlock_irqrestore(&cq->lock, flags);
624 if (err == 0 || err == -EAGAIN)
630 int mlx5_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
632 struct mlx5_core_dev *mdev = to_mdev(ibcq->device)->mdev;
633 void __iomem *uar_page = mdev->priv.uuari.uars[0].map;
636 mlx5_cq_arm(&to_mcq(ibcq)->mcq,
637 (flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?
638 MLX5_CQ_DB_REQ_NOT_SOL : MLX5_CQ_DB_REQ_NOT,
640 MLX5_GET_DOORBELL_LOCK(&mdev->priv.cq_uar_lock),
641 to_mcq(ibcq)->mcq.cons_index);
646 static int alloc_cq_buf(struct mlx5_ib_dev *dev, struct mlx5_ib_cq_buf *buf,
647 int nent, int cqe_size)
651 err = mlx5_buf_alloc(dev->mdev, nent * cqe_size,
652 PAGE_SIZE * 2, &buf->buf);
654 mlx5_ib_err(dev, "alloc failed\n");
658 buf->cqe_size = cqe_size;
664 static int create_cq_user(struct mlx5_ib_dev *dev, struct ib_udata *udata,
665 struct ib_ucontext *context, struct mlx5_ib_cq *cq,
666 int entries, struct mlx5_create_cq_mbox_in **cqb,
667 int *cqe_size, int *index, int *inlen)
669 struct mlx5_exp_ib_create_cq ucmd;
676 memset(&ucmd, 0, sizeof(ucmd));
679 (udata->inlen - sizeof(struct ib_uverbs_cmd_hdr) <
680 sizeof(struct mlx5_ib_create_cq)) ?
681 (sizeof(struct mlx5_ib_create_cq) - sizeof(ucmd.reserved)) :
682 sizeof(struct mlx5_ib_create_cq);
684 if (ib_copy_from_udata(&ucmd, udata, ucmdlen)) {
685 mlx5_ib_err(dev, "copy failed\n");
689 if (ucmdlen == sizeof(ucmd) && ucmd.reserved != 0) {
690 mlx5_ib_err(dev, "command corrupted\n");
694 if (ucmd.cqe_size != 64 && ucmd.cqe_size != 128) {
695 mlx5_ib_warn(dev, "wrong CQE size %d\n", ucmd.cqe_size);
699 *cqe_size = ucmd.cqe_size;
701 cq->buf.umem = ib_umem_get(context, ucmd.buf_addr,
702 entries * ucmd.cqe_size,
703 IB_ACCESS_LOCAL_WRITE, 1);
704 if (IS_ERR(cq->buf.umem)) {
705 err = PTR_ERR(cq->buf.umem);
709 err = mlx5_ib_db_map_user(to_mucontext(context), ucmd.db_addr,
712 mlx5_ib_warn(dev, "map failed\n");
716 mlx5_ib_cont_pages(cq->buf.umem, ucmd.buf_addr, &npages, &page_shift,
718 mlx5_ib_dbg(dev, "addr 0x%llx, size %u, npages %d, page_shift %d, ncont %d\n",
719 (unsigned long long)ucmd.buf_addr, entries * ucmd.cqe_size,
720 npages, page_shift, ncont);
722 *inlen = sizeof(**cqb) + sizeof(*(*cqb)->pas) * ncont;
723 *cqb = mlx5_vzalloc(*inlen);
729 mlx5_ib_populate_pas(dev, cq->buf.umem, page_shift, (*cqb)->pas, 0);
730 (*cqb)->ctx.log_pg_sz = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
732 *index = to_mucontext(context)->uuari.uars[0].index;
734 if (*cqe_size == 64 && MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
735 if (ucmd.exp_data.cqe_comp_en == 1 &&
736 (ucmd.exp_data.comp_mask & MLX5_EXP_CREATE_CQ_MASK_CQE_COMP_EN)) {
737 MLX5_SET(cqc, &(*cqb)->ctx, cqe_compression_en, 1);
738 if (ucmd.exp_data.cqe_comp_recv_type ==
739 MLX5_IB_CQE_FORMAT_CSUM &&
740 (ucmd.exp_data.comp_mask &
741 MLX5_EXP_CREATE_CQ_MASK_CQE_COMP_RECV_TYPE))
742 MLX5_SET(cqc, &(*cqb)->ctx, mini_cqe_res_format,
743 MLX5_IB_CQE_FORMAT_CSUM);
750 mlx5_ib_db_unmap_user(to_mucontext(context), &cq->db);
753 ib_umem_release(cq->buf.umem);
757 static void destroy_cq_user(struct mlx5_ib_cq *cq, struct ib_ucontext *context)
760 mlx5_ib_db_unmap_user(to_mucontext(context), &cq->db);
761 ib_umem_release(cq->buf.umem);
764 static void init_cq_buf(struct mlx5_ib_cq *cq, struct mlx5_ib_cq_buf *buf)
768 struct mlx5_cqe64 *cqe64;
770 for (i = 0; i < buf->nent; i++) {
771 cqe = get_cqe_from_buf(buf, i, buf->cqe_size);
772 cqe64 = buf->cqe_size == 64 ? cqe : cqe + 64;
773 cqe64->op_own = MLX5_CQE_INVALID << 4;
777 static int create_cq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
778 int entries, int cqe_size,
779 struct mlx5_create_cq_mbox_in **cqb,
780 int *index, int *inlen)
784 err = mlx5_db_alloc(dev->mdev, &cq->db);
788 cq->mcq.set_ci_db = cq->db.db;
789 cq->mcq.arm_db = cq->db.db + 1;
790 cq->mcq.cqe_sz = cqe_size;
792 err = alloc_cq_buf(dev, &cq->buf, entries, cqe_size);
796 init_cq_buf(cq, &cq->buf);
798 *inlen = sizeof(**cqb) + sizeof(*(*cqb)->pas) * cq->buf.buf.npages;
799 *cqb = mlx5_vzalloc(*inlen);
804 mlx5_fill_page_array(&cq->buf.buf, (*cqb)->pas);
806 (*cqb)->ctx.log_pg_sz = cq->buf.buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT;
807 *index = dev->mdev->priv.uuari.uars[0].index;
812 free_cq_buf(dev, &cq->buf);
815 mlx5_db_free(dev->mdev, &cq->db);
819 static void destroy_cq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq)
821 free_cq_buf(dev, &cq->buf);
822 mlx5_db_free(dev->mdev, &cq->db);
825 struct ib_cq *mlx5_ib_create_cq(struct ib_device *ibdev,
826 int entries, int vector,
827 struct ib_ucontext *context,
828 struct ib_udata *udata)
830 struct mlx5_create_cq_mbox_in *cqb = NULL;
831 struct mlx5_ib_dev *dev = to_mdev(ibdev);
832 struct mlx5_ib_cq *cq;
833 int uninitialized_var(index);
834 int uninitialized_var(inlen);
840 if (entries < 0 || roundup_pow_of_two(entries + 1) >
841 (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz))) {
842 mlx5_ib_warn(dev, "wrong entries number %d(%ld), max %d\n",
843 entries, roundup_pow_of_two(entries + 1),
844 1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz));
845 return ERR_PTR(-EINVAL);
848 entries = roundup_pow_of_two(entries + 1);
850 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
852 return ERR_PTR(-ENOMEM);
854 cq->ibcq.cqe = entries - 1;
855 mutex_init(&cq->resize_mutex);
856 spin_lock_init(&cq->lock);
857 cq->resize_buf = NULL;
858 cq->resize_umem = NULL;
860 INIT_LIST_HEAD(&cq->list_send_qp);
861 INIT_LIST_HEAD(&cq->list_recv_qp);
864 err = create_cq_user(dev, udata, context, cq, entries,
865 &cqb, &cqe_size, &index, &inlen);
869 cqe_size = (cache_line_size() >= 128 ? 128 : 64);
870 err = create_cq_kernel(dev, cq, entries, cqe_size, &cqb,
876 cq->cqe_size = cqe_size;
877 cqb->ctx.cqe_sz_flags = cqe_sz_to_mlx_sz(cqe_size) << 5;
878 cqb->ctx.log_sz_usr_page = cpu_to_be32((ilog2(entries) << 24) | index);
879 err = mlx5_vector2eqn(dev->mdev, vector, &eqn, &irqn);
883 cqb->ctx.c_eqn = cpu_to_be16(eqn);
884 cqb->ctx.db_record_addr = cpu_to_be64(cq->db.dma);
886 err = mlx5_core_create_cq(dev->mdev, &cq->mcq, cqb, inlen);
890 mlx5_ib_dbg(dev, "cqn 0x%x\n", cq->mcq.cqn);
892 cq->mcq.comp = mlx5_ib_cq_comp;
893 cq->mcq.event = mlx5_ib_cq_event;
896 if (ib_copy_to_udata(udata, &cq->mcq.cqn, sizeof(__u32))) {
906 mlx5_core_destroy_cq(dev->mdev, &cq->mcq);
911 destroy_cq_user(cq, context);
913 destroy_cq_kernel(dev, cq);
922 int mlx5_ib_destroy_cq(struct ib_cq *cq)
924 struct mlx5_ib_dev *dev = to_mdev(cq->device);
925 struct mlx5_ib_cq *mcq = to_mcq(cq);
926 struct ib_ucontext *context = NULL;
929 context = cq->uobject->context;
931 mlx5_core_destroy_cq(dev->mdev, &mcq->mcq);
933 destroy_cq_user(mcq, context);
935 destroy_cq_kernel(dev, mcq);
942 static int is_equal_rsn(struct mlx5_cqe64 *cqe64, u32 rsn)
944 return rsn == (ntohl(cqe64->sop_drop_qpn) & 0xffffff);
947 void __mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 rsn, struct mlx5_ib_srq *srq)
949 struct mlx5_cqe64 *cqe64, *dest64;
958 /* First we need to find the current producer index, so we
959 * know where to start cleaning from. It doesn't matter if HW
960 * adds new entries after this loop -- the QP we're worried
961 * about is already in RESET, so the new entries won't come
962 * from our QP and therefore don't need to be checked.
964 for (prod_index = cq->mcq.cons_index; get_sw_cqe(cq, prod_index); prod_index++)
965 if (prod_index == cq->mcq.cons_index + cq->ibcq.cqe)
968 /* Now sweep backwards through the CQ, removing CQ entries
969 * that match our QP by copying older entries on top of them.
971 while ((int) --prod_index - (int) cq->mcq.cons_index >= 0) {
972 cqe = get_cqe(cq, prod_index & cq->ibcq.cqe);
973 cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
974 if (is_equal_rsn(cqe64, rsn)) {
975 if (srq && (ntohl(cqe64->srqn) & 0xffffff))
976 mlx5_ib_free_srq_wqe(srq, be16_to_cpu(cqe64->wqe_counter));
979 dest = get_cqe(cq, (prod_index + nfreed) & cq->ibcq.cqe);
980 dest64 = (cq->mcq.cqe_sz == 64) ? dest : dest + 64;
981 owner_bit = dest64->op_own & MLX5_CQE_OWNER_MASK;
982 memcpy(dest, cqe, cq->mcq.cqe_sz);
983 dest64->op_own = owner_bit |
984 (dest64->op_own & ~MLX5_CQE_OWNER_MASK);
989 cq->mcq.cons_index += nfreed;
990 /* Make sure update of buffer contents is done before
991 * updating consumer index.
994 mlx5_cq_set_ci(&cq->mcq);
998 void mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 qpn, struct mlx5_ib_srq *srq)
1003 spin_lock_irq(&cq->lock);
1004 __mlx5_ib_cq_clean(cq, qpn, srq);
1005 spin_unlock_irq(&cq->lock);
1008 int mlx5_ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
1010 struct mlx5_modify_cq_mbox_in *in;
1011 struct mlx5_ib_dev *dev = to_mdev(cq->device);
1012 struct mlx5_ib_cq *mcq = to_mcq(cq);
1017 in = kzalloc(sizeof(*in), GFP_KERNEL);
1021 in->cqn = cpu_to_be32(mcq->mcq.cqn);
1023 if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
1024 fsel |= (MLX5_CQ_MODIFY_PERIOD | MLX5_CQ_MODIFY_COUNT);
1025 if (cq_period & 0xf000) {
1026 /* A value higher than 0xfff is required, better
1027 * use the largest value possible. */
1029 printf("mlx5_ib: INFO: ""period supported is limited to 12 bits\n");
1032 in->ctx.cq_period = cpu_to_be16(cq_period);
1033 in->ctx.cq_max_count = cpu_to_be16(cq_count);
1039 in->field_select = cpu_to_be32(fsel);
1040 err = mlx5_core_modify_cq(dev->mdev, &mcq->mcq, in, sizeof(*in));
1045 mlx5_ib_warn(dev, "modify cq 0x%x failed\n", mcq->mcq.cqn);
1050 static int resize_user(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
1051 int entries, struct ib_udata *udata, int *npas,
1052 int *page_shift, int *cqe_size)
1054 struct mlx5_ib_resize_cq ucmd;
1055 struct ib_umem *umem;
1058 struct ib_ucontext *context = cq->buf.umem->context;
1060 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
1064 if (ucmd.reserved0 || ucmd.reserved1)
1067 umem = ib_umem_get(context, ucmd.buf_addr, entries * ucmd.cqe_size,
1068 IB_ACCESS_LOCAL_WRITE, 1);
1070 err = PTR_ERR(umem);
1074 mlx5_ib_cont_pages(umem, ucmd.buf_addr, &npages, page_shift,
1077 cq->resize_umem = umem;
1078 *cqe_size = ucmd.cqe_size;
1083 static void un_resize_user(struct mlx5_ib_cq *cq)
1085 ib_umem_release(cq->resize_umem);
1088 static int resize_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
1089 int entries, int cqe_size)
1093 cq->resize_buf = kzalloc(sizeof(*cq->resize_buf), GFP_KERNEL);
1094 if (!cq->resize_buf)
1097 err = alloc_cq_buf(dev, cq->resize_buf, entries, cqe_size);
1101 init_cq_buf(cq, cq->resize_buf);
1106 kfree(cq->resize_buf);
1110 static void un_resize_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq)
1112 free_cq_buf(dev, cq->resize_buf);
1113 cq->resize_buf = NULL;
1116 static int copy_resize_cqes(struct mlx5_ib_cq *cq)
1118 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
1119 struct mlx5_cqe64 *scqe64;
1120 struct mlx5_cqe64 *dcqe64;
1129 ssize = cq->buf.cqe_size;
1130 dsize = cq->resize_buf->cqe_size;
1131 if (ssize != dsize) {
1132 mlx5_ib_warn(dev, "resize from different cqe size is not supported\n");
1136 i = cq->mcq.cons_index;
1137 scqe = get_sw_cqe(cq, i);
1138 scqe64 = ssize == 64 ? scqe : scqe + 64;
1141 mlx5_ib_warn(dev, "expected cqe in sw ownership\n");
1145 while ((scqe64->op_own >> 4) != MLX5_CQE_RESIZE_CQ) {
1146 dcqe = get_cqe_from_buf(cq->resize_buf,
1147 (i + 1) & (cq->resize_buf->nent),
1149 dcqe64 = dsize == 64 ? dcqe : dcqe + 64;
1150 sw_own = sw_ownership_bit(i + 1, cq->resize_buf->nent);
1151 memcpy(dcqe, scqe, dsize);
1152 dcqe64->op_own = (dcqe64->op_own & ~MLX5_CQE_OWNER_MASK) | sw_own;
1155 scqe = get_sw_cqe(cq, i);
1156 scqe64 = ssize == 64 ? scqe : scqe + 64;
1158 mlx5_ib_warn(dev, "expected cqe in sw ownership\n");
1162 if (scqe == start_cqe) {
1163 printf("mlx5_ib: WARN: ""resize CQ failed to get resize CQE, CQN 0x%x\n", cq->mcq.cqn);
1167 ++cq->mcq.cons_index;
1171 int mlx5_ib_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
1173 struct mlx5_ib_dev *dev = to_mdev(ibcq->device);
1174 struct mlx5_ib_cq *cq = to_mcq(ibcq);
1175 struct mlx5_modify_cq_mbox_in *in;
1180 int uninitialized_var(cqe_size);
1181 unsigned long flags;
1183 if (!MLX5_CAP_GEN(dev->mdev, cq_resize)) {
1184 mlx5_ib_warn(dev, "Firmware does not support resize CQ\n");
1188 if (entries < 1 || roundup_pow_of_two(entries + 1) >
1189 (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz))) {
1190 mlx5_ib_warn(dev, "wrong entries number %d(%ld), max %d\n",
1191 entries, roundup_pow_of_two(entries + 1),
1192 1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz));
1196 entries = roundup_pow_of_two(entries + 1);
1198 if (entries == ibcq->cqe + 1)
1201 mutex_lock(&cq->resize_mutex);
1203 err = resize_user(dev, cq, entries, udata, &npas, &page_shift,
1207 err = resize_kernel(dev, cq, entries, cqe_size);
1209 npas = cq->resize_buf->buf.npages;
1210 page_shift = cq->resize_buf->buf.page_shift;
1215 mlx5_ib_warn(dev, "resize failed: %d\n", err);
1219 inlen = sizeof(*in) + npas * sizeof(in->pas[0]);
1220 in = mlx5_vzalloc(inlen);
1227 mlx5_ib_populate_pas(dev, cq->resize_umem, page_shift,
1230 mlx5_fill_page_array(&cq->resize_buf->buf, in->pas);
1232 in->field_select = cpu_to_be32(MLX5_MODIFY_CQ_MASK_LOG_SIZE |
1233 MLX5_MODIFY_CQ_MASK_PG_OFFSET |
1234 MLX5_MODIFY_CQ_MASK_PG_SIZE);
1235 in->ctx.log_pg_sz = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
1236 in->ctx.cqe_sz_flags = cqe_sz_to_mlx_sz(cqe_size) << 5;
1237 in->ctx.page_offset = 0;
1238 in->ctx.log_sz_usr_page = cpu_to_be32(ilog2(entries) << 24);
1239 in->hdr.opmod = cpu_to_be16(MLX5_CQ_OPMOD_RESIZE);
1240 in->cqn = cpu_to_be32(cq->mcq.cqn);
1242 err = mlx5_core_modify_cq(dev->mdev, &cq->mcq, in, inlen);
1244 mlx5_ib_warn(dev, "modify cq failed: %d\n", err);
1249 cq->ibcq.cqe = entries - 1;
1250 ib_umem_release(cq->buf.umem);
1251 cq->buf.umem = cq->resize_umem;
1252 cq->resize_umem = NULL;
1254 struct mlx5_ib_cq_buf tbuf;
1257 spin_lock_irqsave(&cq->lock, flags);
1258 if (cq->resize_buf) {
1259 err = copy_resize_cqes(cq);
1262 cq->buf = *cq->resize_buf;
1263 kfree(cq->resize_buf);
1264 cq->resize_buf = NULL;
1268 cq->ibcq.cqe = entries - 1;
1269 spin_unlock_irqrestore(&cq->lock, flags);
1271 free_cq_buf(dev, &tbuf);
1273 mutex_unlock(&cq->resize_mutex);
1285 un_resize_kernel(dev, cq);
1287 mutex_unlock(&cq->resize_mutex);
1291 int mlx5_ib_get_cqe_size(struct mlx5_ib_dev *dev, struct ib_cq *ibcq)
1293 struct mlx5_ib_cq *cq;
1299 return cq->cqe_size;