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/kref.h>
29 #include <rdma/ib_umem.h>
30 #include <rdma/ib_user_verbs.h>
31 #include <rdma/ib_cache.h>
34 static void mlx5_ib_cq_comp(struct mlx5_core_cq *cq)
36 struct ib_cq *ibcq = &to_mibcq(cq)->ibcq;
38 ibcq->comp_handler(ibcq, ibcq->cq_context);
41 static void mlx5_ib_cq_event(struct mlx5_core_cq *mcq, int type)
43 struct mlx5_ib_cq *cq = container_of(mcq, struct mlx5_ib_cq, mcq);
44 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
45 struct ib_cq *ibcq = &cq->ibcq;
46 struct ib_event event;
48 if (type != MLX5_EVENT_TYPE_CQ_ERROR) {
49 mlx5_ib_warn(dev, "Unexpected event type %d on CQ %06x\n",
54 if (ibcq->event_handler) {
55 event.device = &dev->ib_dev;
56 event.event = IB_EVENT_CQ_ERR;
57 event.element.cq = ibcq;
58 ibcq->event_handler(&event, ibcq->cq_context);
62 static void *get_cqe_from_buf(struct mlx5_ib_cq_buf *buf, int n, int size)
64 return mlx5_buf_offset(&buf->buf, n * size);
67 static void *get_cqe(struct mlx5_ib_cq *cq, int n)
69 return get_cqe_from_buf(&cq->buf, n, cq->mcq.cqe_sz);
72 static u8 sw_ownership_bit(int n, int nent)
74 return (n & nent) ? 1 : 0;
77 static void *get_sw_cqe(struct mlx5_ib_cq *cq, int n)
79 void *cqe = get_cqe(cq, n & cq->ibcq.cqe);
80 struct mlx5_cqe64 *cqe64;
82 cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
84 if (likely((cqe64->op_own) >> 4 != MLX5_CQE_INVALID) &&
85 !((cqe64->op_own & MLX5_CQE_OWNER_MASK) ^ !!(n & (cq->ibcq.cqe + 1)))) {
92 static void *next_cqe_sw(struct mlx5_ib_cq *cq)
94 return get_sw_cqe(cq, cq->mcq.cons_index);
97 static enum ib_wc_opcode get_umr_comp(struct mlx5_ib_wq *wq, int idx)
99 switch (wq->wr_data[idx]) {
103 case IB_WR_LOCAL_INV:
104 return IB_WC_LOCAL_INV;
110 pr_warn("unknown completion status\n");
115 static void handle_good_req(struct ib_wc *wc, struct mlx5_cqe64 *cqe,
116 struct mlx5_ib_wq *wq, int idx)
119 switch (be32_to_cpu(cqe->sop_drop_qpn) >> 24) {
120 case MLX5_OPCODE_RDMA_WRITE_IMM:
121 wc->wc_flags |= IB_WC_WITH_IMM;
122 case MLX5_OPCODE_RDMA_WRITE:
123 wc->opcode = IB_WC_RDMA_WRITE;
125 case MLX5_OPCODE_SEND_IMM:
126 wc->wc_flags |= IB_WC_WITH_IMM;
127 case MLX5_OPCODE_SEND:
128 case MLX5_OPCODE_SEND_INVAL:
129 wc->opcode = IB_WC_SEND;
131 case MLX5_OPCODE_RDMA_READ:
132 wc->opcode = IB_WC_RDMA_READ;
133 wc->byte_len = be32_to_cpu(cqe->byte_cnt);
135 case MLX5_OPCODE_ATOMIC_CS:
136 wc->opcode = IB_WC_COMP_SWAP;
139 case MLX5_OPCODE_ATOMIC_FA:
140 wc->opcode = IB_WC_FETCH_ADD;
143 case MLX5_OPCODE_ATOMIC_MASKED_CS:
144 wc->opcode = IB_WC_MASKED_COMP_SWAP;
147 case MLX5_OPCODE_ATOMIC_MASKED_FA:
148 wc->opcode = IB_WC_MASKED_FETCH_ADD;
151 case MLX5_OPCODE_UMR:
152 wc->opcode = get_umr_comp(wq, idx);
158 MLX5_GRH_IN_BUFFER = 1,
162 static void handle_responder(struct ib_wc *wc, struct mlx5_cqe64 *cqe,
163 struct mlx5_ib_qp *qp)
165 enum rdma_link_layer ll = rdma_port_get_link_layer(qp->ibqp.device, 1);
166 struct mlx5_ib_dev *dev = to_mdev(qp->ibqp.device);
167 struct mlx5_ib_srq *srq;
168 struct mlx5_ib_wq *wq;
174 if (qp->ibqp.srq || qp->ibqp.xrcd) {
175 struct mlx5_core_srq *msrq = NULL;
178 msrq = mlx5_core_get_srq(dev->mdev,
179 be32_to_cpu(cqe->srqn));
180 srq = to_mibsrq(msrq);
182 srq = to_msrq(qp->ibqp.srq);
185 wqe_ctr = be16_to_cpu(cqe->wqe_counter);
186 wc->wr_id = srq->wrid[wqe_ctr];
187 mlx5_ib_free_srq_wqe(srq, wqe_ctr);
188 if (msrq && atomic_dec_and_test(&msrq->refcount))
189 complete(&msrq->free);
193 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
196 wc->byte_len = be32_to_cpu(cqe->byte_cnt);
198 switch (cqe->op_own >> 4) {
199 case MLX5_CQE_RESP_WR_IMM:
200 wc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
201 wc->wc_flags = IB_WC_WITH_IMM;
202 wc->ex.imm_data = cqe->imm_inval_pkey;
204 case MLX5_CQE_RESP_SEND:
205 wc->opcode = IB_WC_RECV;
206 wc->wc_flags = IB_WC_IP_CSUM_OK;
207 if (unlikely(!((cqe->hds_ip_ext & CQE_L3_OK) &&
208 (cqe->hds_ip_ext & CQE_L4_OK))))
211 case MLX5_CQE_RESP_SEND_IMM:
212 wc->opcode = IB_WC_RECV;
213 wc->wc_flags = IB_WC_WITH_IMM;
214 wc->ex.imm_data = cqe->imm_inval_pkey;
216 case MLX5_CQE_RESP_SEND_INV:
217 wc->opcode = IB_WC_RECV;
218 wc->wc_flags = IB_WC_WITH_INVALIDATE;
219 wc->ex.invalidate_rkey = be32_to_cpu(cqe->imm_inval_pkey);
222 wc->slid = be16_to_cpu(cqe->slid);
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 if (unlikely(is_qp1(qp->ibqp.qp_type))) {
228 u16 pkey = be32_to_cpu(cqe->imm_inval_pkey) & 0xffff;
230 ib_find_cached_pkey(&dev->ib_dev, qp->port, pkey,
236 if (ll != IB_LINK_LAYER_ETHERNET) {
237 wc->sl = (be32_to_cpu(cqe->flags_rqpn) >> 24) & 0xf;
241 vlan_present = cqe_has_vlan(cqe);
242 roce_packet_type = (be32_to_cpu(cqe->flags_rqpn) >> 24) & 0x3;
244 wc->vlan_id = (be16_to_cpu(cqe->vlan_info)) & 0xfff;
245 wc->sl = (be16_to_cpu(cqe->vlan_info) >> 13) & 0x7;
246 wc->wc_flags |= IB_WC_WITH_VLAN;
251 switch (roce_packet_type) {
252 case MLX5_CQE_ROCE_L3_HEADER_TYPE_GRH:
253 wc->network_hdr_type = RDMA_NETWORK_IB;
255 case MLX5_CQE_ROCE_L3_HEADER_TYPE_IPV6:
256 wc->network_hdr_type = RDMA_NETWORK_IPV6;
258 case MLX5_CQE_ROCE_L3_HEADER_TYPE_IPV4:
259 wc->network_hdr_type = RDMA_NETWORK_IPV4;
262 wc->wc_flags |= IB_WC_WITH_NETWORK_HDR_TYPE;
265 static void dump_cqe(struct mlx5_ib_dev *dev, struct mlx5_err_cqe *cqe)
267 __be32 *p = (__be32 *)cqe;
270 mlx5_ib_warn(dev, "dump error cqe\n");
271 for (i = 0; i < sizeof(*cqe) / 16; i++, p += 4)
272 pr_info("%08x %08x %08x %08x\n", be32_to_cpu(p[0]),
273 be32_to_cpu(p[1]), be32_to_cpu(p[2]),
277 static void mlx5_handle_error_cqe(struct mlx5_ib_dev *dev,
278 struct mlx5_err_cqe *cqe,
283 switch (cqe->syndrome) {
284 case MLX5_CQE_SYNDROME_LOCAL_LENGTH_ERR:
285 wc->status = IB_WC_LOC_LEN_ERR;
287 case MLX5_CQE_SYNDROME_LOCAL_QP_OP_ERR:
288 wc->status = IB_WC_LOC_QP_OP_ERR;
290 case MLX5_CQE_SYNDROME_LOCAL_PROT_ERR:
291 wc->status = IB_WC_LOC_PROT_ERR;
293 case MLX5_CQE_SYNDROME_WR_FLUSH_ERR:
295 wc->status = IB_WC_WR_FLUSH_ERR;
297 case MLX5_CQE_SYNDROME_MW_BIND_ERR:
298 wc->status = IB_WC_MW_BIND_ERR;
300 case MLX5_CQE_SYNDROME_BAD_RESP_ERR:
301 wc->status = IB_WC_BAD_RESP_ERR;
303 case MLX5_CQE_SYNDROME_LOCAL_ACCESS_ERR:
304 wc->status = IB_WC_LOC_ACCESS_ERR;
306 case MLX5_CQE_SYNDROME_REMOTE_INVAL_REQ_ERR:
307 wc->status = IB_WC_REM_INV_REQ_ERR;
309 case MLX5_CQE_SYNDROME_REMOTE_ACCESS_ERR:
310 wc->status = IB_WC_REM_ACCESS_ERR;
312 case MLX5_CQE_SYNDROME_REMOTE_OP_ERR:
313 wc->status = IB_WC_REM_OP_ERR;
315 case MLX5_CQE_SYNDROME_TRANSPORT_RETRY_EXC_ERR:
316 wc->status = IB_WC_RETRY_EXC_ERR;
319 case MLX5_CQE_SYNDROME_RNR_RETRY_EXC_ERR:
320 wc->status = IB_WC_RNR_RETRY_EXC_ERR;
323 case MLX5_CQE_SYNDROME_REMOTE_ABORTED_ERR:
324 wc->status = IB_WC_REM_ABORT_ERR;
327 wc->status = IB_WC_GENERAL_ERR;
331 wc->vendor_err = cqe->vendor_err_synd;
336 static int is_atomic_response(struct mlx5_ib_qp *qp, uint16_t idx)
338 /* TBD: waiting decision
343 static void *mlx5_get_atomic_laddr(struct mlx5_ib_qp *qp, uint16_t idx)
345 struct mlx5_wqe_data_seg *dpseg;
348 dpseg = mlx5_get_send_wqe(qp, idx) + sizeof(struct mlx5_wqe_ctrl_seg) +
349 sizeof(struct mlx5_wqe_raddr_seg) +
350 sizeof(struct mlx5_wqe_atomic_seg);
351 addr = (void *)(unsigned long)be64_to_cpu(dpseg->addr);
355 static void handle_atomic(struct mlx5_ib_qp *qp, struct mlx5_cqe64 *cqe64,
362 if (!is_atomic_response(qp, idx))
365 byte_count = be32_to_cpu(cqe64->byte_cnt);
366 addr = mlx5_get_atomic_laddr(qp, idx);
368 if (byte_count == 4) {
369 *(uint32_t *)addr = be32_to_cpu(*((__be32 *)addr));
371 for (i = 0; i < byte_count; i += 8) {
372 *(uint64_t *)addr = be64_to_cpu(*((__be64 *)addr));
380 static void handle_atomics(struct mlx5_ib_qp *qp, struct mlx5_cqe64 *cqe64,
386 idx = tail & (qp->sq.wqe_cnt - 1);
387 handle_atomic(qp, cqe64, idx);
391 tail = qp->sq.w_list[idx].next;
393 tail = qp->sq.w_list[idx].next;
394 qp->sq.last_poll = tail;
397 static void free_cq_buf(struct mlx5_ib_dev *dev, struct mlx5_ib_cq_buf *buf)
399 mlx5_buf_free(dev->mdev, &buf->buf);
402 static void get_sig_err_item(struct mlx5_sig_err_cqe *cqe,
403 struct ib_sig_err *item)
405 u16 syndrome = be16_to_cpu(cqe->syndrome);
407 #define GUARD_ERR (1 << 13)
408 #define APPTAG_ERR (1 << 12)
409 #define REFTAG_ERR (1 << 11)
411 if (syndrome & GUARD_ERR) {
412 item->err_type = IB_SIG_BAD_GUARD;
413 item->expected = be32_to_cpu(cqe->expected_trans_sig) >> 16;
414 item->actual = be32_to_cpu(cqe->actual_trans_sig) >> 16;
416 if (syndrome & REFTAG_ERR) {
417 item->err_type = IB_SIG_BAD_REFTAG;
418 item->expected = be32_to_cpu(cqe->expected_reftag);
419 item->actual = be32_to_cpu(cqe->actual_reftag);
421 if (syndrome & APPTAG_ERR) {
422 item->err_type = IB_SIG_BAD_APPTAG;
423 item->expected = be32_to_cpu(cqe->expected_trans_sig) & 0xffff;
424 item->actual = be32_to_cpu(cqe->actual_trans_sig) & 0xffff;
426 pr_err("Got signature completion error with bad syndrome %04x\n",
430 item->sig_err_offset = be64_to_cpu(cqe->err_offset);
431 item->key = be32_to_cpu(cqe->mkey);
434 static void sw_send_comp(struct mlx5_ib_qp *qp, int num_entries,
435 struct ib_wc *wc, int *npolled)
437 struct mlx5_ib_wq *wq;
444 cur = wq->head - wq->tail;
450 for (i = 0; i < cur && np < num_entries; i++) {
451 idx = wq->last_poll & (wq->wqe_cnt - 1);
452 wc->wr_id = wq->wrid[idx];
453 wc->status = IB_WC_WR_FLUSH_ERR;
454 wc->vendor_err = MLX5_CQE_SYNDROME_WR_FLUSH_ERR;
459 wq->last_poll = wq->w_list[idx].next;
464 static void sw_recv_comp(struct mlx5_ib_qp *qp, int num_entries,
465 struct ib_wc *wc, int *npolled)
467 struct mlx5_ib_wq *wq;
473 cur = wq->head - wq->tail;
479 for (i = 0; i < cur && np < num_entries; i++) {
480 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
481 wc->status = IB_WC_WR_FLUSH_ERR;
482 wc->vendor_err = MLX5_CQE_SYNDROME_WR_FLUSH_ERR;
491 static void mlx5_ib_poll_sw_comp(struct mlx5_ib_cq *cq, int num_entries,
492 struct ib_wc *wc, int *npolled)
494 struct mlx5_ib_qp *qp;
497 /* Find uncompleted WQEs belonging to that cq and retrun mmics ones */
498 list_for_each_entry(qp, &cq->list_send_qp, cq_send_list) {
499 sw_send_comp(qp, num_entries, wc + *npolled, npolled);
500 if (*npolled >= num_entries)
504 list_for_each_entry(qp, &cq->list_recv_qp, cq_recv_list) {
505 sw_recv_comp(qp, num_entries, wc + *npolled, npolled);
506 if (*npolled >= num_entries)
511 static int mlx5_poll_one(struct mlx5_ib_cq *cq,
512 struct mlx5_ib_qp **cur_qp,
515 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
516 struct mlx5_err_cqe *err_cqe;
517 struct mlx5_cqe64 *cqe64;
518 struct mlx5_core_qp *mqp;
519 struct mlx5_ib_wq *wq;
520 struct mlx5_sig_err_cqe *sig_err_cqe;
521 struct mlx5_core_mr *mmkey;
522 struct mlx5_ib_mr *mr;
531 cqe = next_cqe_sw(cq);
535 cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
537 ++cq->mcq.cons_index;
539 /* Make sure we read CQ entry contents after we've checked the
544 opcode = cqe64->op_own >> 4;
545 if (unlikely(opcode == MLX5_CQE_RESIZE_CQ)) {
546 if (likely(cq->resize_buf)) {
547 free_cq_buf(dev, &cq->buf);
548 cq->buf = *cq->resize_buf;
549 kfree(cq->resize_buf);
550 cq->resize_buf = NULL;
553 mlx5_ib_warn(dev, "unexpected resize cqe\n");
557 qpn = ntohl(cqe64->sop_drop_qpn) & 0xffffff;
558 if (!*cur_qp || (qpn != (*cur_qp)->ibqp.qp_num)) {
559 /* We do not have to take the QP table lock here,
560 * because CQs will be locked while QPs are removed
563 mqp = __mlx5_qp_lookup(dev->mdev, qpn);
564 *cur_qp = to_mibqp(mqp);
567 wc->qp = &(*cur_qp)->ibqp;
571 wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
572 idx = wqe_ctr & (wq->wqe_cnt - 1);
573 handle_good_req(wc, cqe64, wq, idx);
574 handle_atomics(*cur_qp, cqe64, wq->last_poll, idx);
575 wc->wr_id = wq->wrid[idx];
576 wq->tail = wq->wqe_head[idx] + 1;
577 wc->status = IB_WC_SUCCESS;
579 case MLX5_CQE_RESP_WR_IMM:
580 case MLX5_CQE_RESP_SEND:
581 case MLX5_CQE_RESP_SEND_IMM:
582 case MLX5_CQE_RESP_SEND_INV:
583 handle_responder(wc, cqe64, *cur_qp);
584 wc->status = IB_WC_SUCCESS;
586 case MLX5_CQE_RESIZE_CQ:
588 case MLX5_CQE_REQ_ERR:
589 case MLX5_CQE_RESP_ERR:
590 err_cqe = (struct mlx5_err_cqe *)cqe64;
591 mlx5_handle_error_cqe(dev, err_cqe, wc);
592 mlx5_ib_dbg(dev, "%s error cqe on cqn 0x%x:\n",
593 opcode == MLX5_CQE_REQ_ERR ?
594 "Requestor" : "Responder", cq->mcq.cqn);
595 mlx5_ib_dbg(dev, "syndrome 0x%x, vendor syndrome 0x%x\n",
596 err_cqe->syndrome, err_cqe->vendor_err_synd);
597 if (opcode == MLX5_CQE_REQ_ERR) {
599 wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
600 idx = wqe_ctr & (wq->wqe_cnt - 1);
601 wc->wr_id = wq->wrid[idx];
602 wq->tail = wq->wqe_head[idx] + 1;
604 struct mlx5_ib_srq *srq;
606 if ((*cur_qp)->ibqp.srq) {
607 srq = to_msrq((*cur_qp)->ibqp.srq);
608 wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
609 wc->wr_id = srq->wrid[wqe_ctr];
610 mlx5_ib_free_srq_wqe(srq, wqe_ctr);
613 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
618 case MLX5_CQE_SIG_ERR:
619 sig_err_cqe = (struct mlx5_sig_err_cqe *)cqe64;
621 spin_lock_irqsave(&dev->mdev->priv.mr_table.lock, flags);
622 mmkey = __mlx5_mr_lookup(dev->mdev,
623 mlx5_base_mkey(be32_to_cpu(sig_err_cqe->mkey)));
624 mr = to_mibmr(mmkey);
625 get_sig_err_item(sig_err_cqe, &mr->sig->err_item);
626 mr->sig->sig_err_exists = true;
627 mr->sig->sigerr_count++;
629 mlx5_ib_warn(dev, "CQN: 0x%x Got SIGERR on key: 0x%x err_type %x err_offset %llx expected %x actual %x\n",
630 cq->mcq.cqn, mr->sig->err_item.key,
631 mr->sig->err_item.err_type,
632 (long long)mr->sig->err_item.sig_err_offset,
633 mr->sig->err_item.expected,
634 mr->sig->err_item.actual);
636 spin_unlock_irqrestore(&dev->mdev->priv.mr_table.lock, flags);
643 static int poll_soft_wc(struct mlx5_ib_cq *cq, int num_entries,
646 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
647 struct mlx5_ib_wc *soft_wc, *next;
650 list_for_each_entry_safe(soft_wc, next, &cq->wc_list, list) {
651 if (npolled >= num_entries)
654 mlx5_ib_dbg(dev, "polled software generated completion on CQ 0x%x\n",
657 wc[npolled++] = soft_wc->wc;
658 list_del(&soft_wc->list);
665 int mlx5_ib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
667 struct mlx5_ib_cq *cq = to_mcq(ibcq);
668 struct mlx5_ib_qp *cur_qp = NULL;
669 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
670 struct mlx5_core_dev *mdev = dev->mdev;
675 spin_lock_irqsave(&cq->lock, flags);
676 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
677 mlx5_ib_poll_sw_comp(cq, num_entries, wc, &npolled);
681 if (unlikely(!list_empty(&cq->wc_list)))
682 soft_polled = poll_soft_wc(cq, num_entries, wc);
684 for (npolled = 0; npolled < num_entries - soft_polled; npolled++) {
685 if (mlx5_poll_one(cq, &cur_qp, wc + soft_polled + npolled))
690 mlx5_cq_set_ci(&cq->mcq);
692 spin_unlock_irqrestore(&cq->lock, flags);
694 return soft_polled + npolled;
697 int mlx5_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
699 struct mlx5_core_dev *mdev = to_mdev(ibcq->device)->mdev;
700 struct mlx5_ib_cq *cq = to_mcq(ibcq);
701 void __iomem *uar_page = mdev->priv.uuari.uars[0].map;
702 unsigned long irq_flags;
705 spin_lock_irqsave(&cq->lock, irq_flags);
706 if (cq->notify_flags != IB_CQ_NEXT_COMP)
707 cq->notify_flags = flags & IB_CQ_SOLICITED_MASK;
709 if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !list_empty(&cq->wc_list))
711 spin_unlock_irqrestore(&cq->lock, irq_flags);
713 mlx5_cq_arm(&cq->mcq,
714 (flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?
715 MLX5_CQ_DB_REQ_NOT_SOL : MLX5_CQ_DB_REQ_NOT,
717 MLX5_GET_DOORBELL_LOCK(&mdev->priv.cq_uar_lock),
718 to_mcq(ibcq)->mcq.cons_index);
723 static int alloc_cq_buf(struct mlx5_ib_dev *dev, struct mlx5_ib_cq_buf *buf,
724 int nent, int cqe_size)
728 err = mlx5_buf_alloc(dev->mdev, nent * cqe_size,
729 2 * PAGE_SIZE, &buf->buf);
733 buf->cqe_size = cqe_size;
739 static int create_cq_user(struct mlx5_ib_dev *dev, struct ib_udata *udata,
740 struct ib_ucontext *context, struct mlx5_ib_cq *cq,
741 int entries, u32 **cqb,
742 int *cqe_size, int *index, int *inlen)
744 struct mlx5_ib_create_cq ucmd;
754 (udata->inlen - sizeof(struct ib_uverbs_cmd_hdr) <
755 sizeof(ucmd)) ? (sizeof(ucmd) -
756 sizeof(ucmd.reserved)) : sizeof(ucmd);
758 if (ib_copy_from_udata(&ucmd, udata, ucmdlen))
761 if (ucmdlen == sizeof(ucmd) &&
765 if (ucmd.cqe_size != 64 && ucmd.cqe_size != 128)
768 *cqe_size = ucmd.cqe_size;
770 cq->buf.umem = ib_umem_get(context, ucmd.buf_addr,
771 entries * ucmd.cqe_size,
772 IB_ACCESS_LOCAL_WRITE, 1);
773 if (IS_ERR(cq->buf.umem)) {
774 err = PTR_ERR(cq->buf.umem);
778 err = mlx5_ib_db_map_user(to_mucontext(context), ucmd.db_addr,
783 mlx5_ib_cont_pages(cq->buf.umem, ucmd.buf_addr, &npages, &page_shift,
785 mlx5_ib_dbg(dev, "addr 0x%llx, size %u, npages %d, page_shift %d, ncont %d\n",
786 (long long)ucmd.buf_addr, entries * ucmd.cqe_size, npages, page_shift, ncont);
788 *inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
789 MLX5_FLD_SZ_BYTES(create_cq_in, pas[0]) * ncont;
790 *cqb = mlx5_vzalloc(*inlen);
796 pas = (__be64 *)MLX5_ADDR_OF(create_cq_in, *cqb, pas);
797 mlx5_ib_populate_pas(dev, cq->buf.umem, page_shift, pas, 0);
799 cqc = MLX5_ADDR_OF(create_cq_in, *cqb, cq_context);
800 MLX5_SET(cqc, cqc, log_page_size,
801 page_shift - MLX5_ADAPTER_PAGE_SHIFT);
803 *index = to_mucontext(context)->uuari.uars[0].index;
808 mlx5_ib_db_unmap_user(to_mucontext(context), &cq->db);
811 ib_umem_release(cq->buf.umem);
815 static void destroy_cq_user(struct mlx5_ib_cq *cq, struct ib_ucontext *context)
817 mlx5_ib_db_unmap_user(to_mucontext(context), &cq->db);
818 ib_umem_release(cq->buf.umem);
821 static void init_cq_buf(struct mlx5_ib_cq *cq, struct mlx5_ib_cq_buf *buf)
825 struct mlx5_cqe64 *cqe64;
827 for (i = 0; i < buf->nent; i++) {
828 cqe = get_cqe_from_buf(buf, i, buf->cqe_size);
829 cqe64 = buf->cqe_size == 64 ? cqe : cqe + 64;
830 cqe64->op_own = MLX5_CQE_INVALID << 4;
834 static int create_cq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
835 int entries, int cqe_size,
836 u32 **cqb, int *index, int *inlen)
842 err = mlx5_db_alloc(dev->mdev, &cq->db);
846 cq->mcq.set_ci_db = cq->db.db;
847 cq->mcq.arm_db = cq->db.db + 1;
848 cq->mcq.cqe_sz = cqe_size;
850 err = alloc_cq_buf(dev, &cq->buf, entries, cqe_size);
854 init_cq_buf(cq, &cq->buf);
856 *inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
857 MLX5_FLD_SZ_BYTES(create_cq_in, pas[0]) * cq->buf.buf.npages;
858 *cqb = mlx5_vzalloc(*inlen);
864 pas = (__be64 *)MLX5_ADDR_OF(create_cq_in, *cqb, pas);
865 mlx5_fill_page_array(&cq->buf.buf, pas);
867 cqc = MLX5_ADDR_OF(create_cq_in, *cqb, cq_context);
868 MLX5_SET(cqc, cqc, log_page_size,
869 cq->buf.buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
871 *index = dev->mdev->priv.uuari.uars[0].index;
876 free_cq_buf(dev, &cq->buf);
879 mlx5_db_free(dev->mdev, &cq->db);
883 static void destroy_cq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq)
885 free_cq_buf(dev, &cq->buf);
886 mlx5_db_free(dev->mdev, &cq->db);
889 static void notify_soft_wc_handler(struct work_struct *work)
891 struct mlx5_ib_cq *cq = container_of(work, struct mlx5_ib_cq,
894 cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
897 struct ib_cq *mlx5_ib_create_cq(struct ib_device *ibdev,
898 const struct ib_cq_init_attr *attr,
899 struct ib_ucontext *context,
900 struct ib_udata *udata)
902 int entries = attr->cqe;
903 int vector = attr->comp_vector;
904 struct mlx5_ib_dev *dev = to_mdev(ibdev);
905 struct mlx5_ib_cq *cq;
906 int uninitialized_var(index);
907 int uninitialized_var(inlen);
916 (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz))))
917 return ERR_PTR(-EINVAL);
919 if (check_cq_create_flags(attr->flags))
920 return ERR_PTR(-EOPNOTSUPP);
922 entries = roundup_pow_of_two(entries + 1);
923 if (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz)))
924 return ERR_PTR(-EINVAL);
926 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
928 return ERR_PTR(-ENOMEM);
930 cq->ibcq.cqe = entries - 1;
931 mutex_init(&cq->resize_mutex);
932 spin_lock_init(&cq->lock);
933 cq->resize_buf = NULL;
934 cq->resize_umem = NULL;
935 cq->create_flags = attr->flags;
936 INIT_LIST_HEAD(&cq->list_send_qp);
937 INIT_LIST_HEAD(&cq->list_recv_qp);
940 err = create_cq_user(dev, udata, context, cq, entries,
941 &cqb, &cqe_size, &index, &inlen);
945 cqe_size = cache_line_size() == 128 ? 128 : 64;
946 err = create_cq_kernel(dev, cq, entries, cqe_size, &cqb,
951 INIT_WORK(&cq->notify_work, notify_soft_wc_handler);
954 err = mlx5_vector2eqn(dev->mdev, vector, &eqn, &irqn);
958 cq->cqe_size = cqe_size;
960 cqc = MLX5_ADDR_OF(create_cq_in, cqb, cq_context);
961 MLX5_SET(cqc, cqc, cqe_sz, cqe_sz_to_mlx_sz(cqe_size));
962 MLX5_SET(cqc, cqc, log_cq_size, ilog2(entries));
963 MLX5_SET(cqc, cqc, uar_page, index);
964 MLX5_SET(cqc, cqc, c_eqn, eqn);
965 MLX5_SET64(cqc, cqc, dbr_addr, cq->db.dma);
966 if (cq->create_flags & IB_CQ_FLAGS_IGNORE_OVERRUN)
967 MLX5_SET(cqc, cqc, oi, 1);
969 err = mlx5_core_create_cq(dev->mdev, &cq->mcq, cqb, inlen);
973 mlx5_ib_dbg(dev, "cqn 0x%x\n", cq->mcq.cqn);
975 cq->mcq.comp = mlx5_ib_cq_comp;
976 cq->mcq.event = mlx5_ib_cq_event;
978 INIT_LIST_HEAD(&cq->wc_list);
981 if (ib_copy_to_udata(udata, &cq->mcq.cqn, sizeof(__u32))) {
991 mlx5_core_destroy_cq(dev->mdev, &cq->mcq);
996 destroy_cq_user(cq, context);
998 destroy_cq_kernel(dev, cq);
1003 return ERR_PTR(err);
1007 int mlx5_ib_destroy_cq(struct ib_cq *cq)
1009 struct mlx5_ib_dev *dev = to_mdev(cq->device);
1010 struct mlx5_ib_cq *mcq = to_mcq(cq);
1011 struct ib_ucontext *context = NULL;
1014 context = cq->uobject->context;
1016 mlx5_core_destroy_cq(dev->mdev, &mcq->mcq);
1018 destroy_cq_user(mcq, context);
1020 destroy_cq_kernel(dev, mcq);
1027 static int is_equal_rsn(struct mlx5_cqe64 *cqe64, u32 rsn)
1029 return rsn == (ntohl(cqe64->sop_drop_qpn) & 0xffffff);
1032 void __mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 rsn, struct mlx5_ib_srq *srq)
1034 struct mlx5_cqe64 *cqe64, *dest64;
1043 /* First we need to find the current producer index, so we
1044 * know where to start cleaning from. It doesn't matter if HW
1045 * adds new entries after this loop -- the QP we're worried
1046 * about is already in RESET, so the new entries won't come
1047 * from our QP and therefore don't need to be checked.
1049 for (prod_index = cq->mcq.cons_index; get_sw_cqe(cq, prod_index); prod_index++)
1050 if (prod_index == cq->mcq.cons_index + cq->ibcq.cqe)
1053 /* Now sweep backwards through the CQ, removing CQ entries
1054 * that match our QP by copying older entries on top of them.
1056 while ((int) --prod_index - (int) cq->mcq.cons_index >= 0) {
1057 cqe = get_cqe(cq, prod_index & cq->ibcq.cqe);
1058 cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
1059 if (is_equal_rsn(cqe64, rsn)) {
1060 if (srq && (ntohl(cqe64->srqn) & 0xffffff))
1061 mlx5_ib_free_srq_wqe(srq, be16_to_cpu(cqe64->wqe_counter));
1063 } else if (nfreed) {
1064 dest = get_cqe(cq, (prod_index + nfreed) & cq->ibcq.cqe);
1065 dest64 = (cq->mcq.cqe_sz == 64) ? dest : dest + 64;
1066 owner_bit = dest64->op_own & MLX5_CQE_OWNER_MASK;
1067 memcpy(dest, cqe, cq->mcq.cqe_sz);
1068 dest64->op_own = owner_bit |
1069 (dest64->op_own & ~MLX5_CQE_OWNER_MASK);
1074 cq->mcq.cons_index += nfreed;
1075 /* Make sure update of buffer contents is done before
1076 * updating consumer index.
1079 mlx5_cq_set_ci(&cq->mcq);
1083 void mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 qpn, struct mlx5_ib_srq *srq)
1088 spin_lock_irq(&cq->lock);
1089 __mlx5_ib_cq_clean(cq, qpn, srq);
1090 spin_unlock_irq(&cq->lock);
1093 int mlx5_ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
1095 struct mlx5_ib_dev *dev = to_mdev(cq->device);
1096 struct mlx5_ib_cq *mcq = to_mcq(cq);
1099 if (!MLX5_CAP_GEN(dev->mdev, cq_moderation))
1102 err = mlx5_core_modify_cq_moderation(dev->mdev, &mcq->mcq,
1103 cq_period, cq_count);
1105 mlx5_ib_warn(dev, "modify cq 0x%x failed\n", mcq->mcq.cqn);
1110 static int resize_user(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
1111 int entries, struct ib_udata *udata, int *npas,
1112 int *page_shift, int *cqe_size)
1114 struct mlx5_ib_resize_cq ucmd;
1115 struct ib_umem *umem;
1118 struct ib_ucontext *context = cq->buf.umem->context;
1120 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
1124 if (ucmd.reserved0 || ucmd.reserved1)
1127 umem = ib_umem_get(context, ucmd.buf_addr, entries * ucmd.cqe_size,
1128 IB_ACCESS_LOCAL_WRITE, 1);
1130 err = PTR_ERR(umem);
1134 mlx5_ib_cont_pages(umem, ucmd.buf_addr, &npages, page_shift,
1137 cq->resize_umem = umem;
1138 *cqe_size = ucmd.cqe_size;
1143 static void un_resize_user(struct mlx5_ib_cq *cq)
1145 ib_umem_release(cq->resize_umem);
1148 static int resize_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
1149 int entries, int cqe_size)
1153 cq->resize_buf = kzalloc(sizeof(*cq->resize_buf), GFP_KERNEL);
1154 if (!cq->resize_buf)
1157 err = alloc_cq_buf(dev, cq->resize_buf, entries, cqe_size);
1161 init_cq_buf(cq, cq->resize_buf);
1166 kfree(cq->resize_buf);
1170 static void un_resize_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq)
1172 free_cq_buf(dev, cq->resize_buf);
1173 cq->resize_buf = NULL;
1176 static int copy_resize_cqes(struct mlx5_ib_cq *cq)
1178 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
1179 struct mlx5_cqe64 *scqe64;
1180 struct mlx5_cqe64 *dcqe64;
1189 ssize = cq->buf.cqe_size;
1190 dsize = cq->resize_buf->cqe_size;
1191 if (ssize != dsize) {
1192 mlx5_ib_warn(dev, "resize from different cqe size is not supported\n");
1196 i = cq->mcq.cons_index;
1197 scqe = get_sw_cqe(cq, i);
1198 scqe64 = ssize == 64 ? scqe : scqe + 64;
1201 mlx5_ib_warn(dev, "expected cqe in sw ownership\n");
1205 while ((scqe64->op_own >> 4) != MLX5_CQE_RESIZE_CQ) {
1206 dcqe = get_cqe_from_buf(cq->resize_buf,
1207 (i + 1) & (cq->resize_buf->nent),
1209 dcqe64 = dsize == 64 ? dcqe : dcqe + 64;
1210 sw_own = sw_ownership_bit(i + 1, cq->resize_buf->nent);
1211 memcpy(dcqe, scqe, dsize);
1212 dcqe64->op_own = (dcqe64->op_own & ~MLX5_CQE_OWNER_MASK) | sw_own;
1215 scqe = get_sw_cqe(cq, i);
1216 scqe64 = ssize == 64 ? scqe : scqe + 64;
1218 mlx5_ib_warn(dev, "expected cqe in sw ownership\n");
1222 if (scqe == start_cqe) {
1223 pr_warn("resize CQ failed to get resize CQE, CQN 0x%x\n",
1228 ++cq->mcq.cons_index;
1232 int mlx5_ib_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
1234 struct mlx5_ib_dev *dev = to_mdev(ibcq->device);
1235 struct mlx5_ib_cq *cq = to_mcq(ibcq);
1243 int uninitialized_var(cqe_size);
1244 unsigned long flags;
1246 if (!MLX5_CAP_GEN(dev->mdev, cq_resize)) {
1247 pr_info("Firmware does not support resize CQ\n");
1252 entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz))) {
1253 mlx5_ib_warn(dev, "wrong entries number %d, max %d\n",
1255 1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz));
1259 entries = roundup_pow_of_two(entries + 1);
1260 if (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz)) + 1)
1263 if (entries == ibcq->cqe + 1)
1266 mutex_lock(&cq->resize_mutex);
1268 err = resize_user(dev, cq, entries, udata, &npas, &page_shift,
1272 err = resize_kernel(dev, cq, entries, cqe_size);
1274 npas = cq->resize_buf->buf.npages;
1275 page_shift = cq->resize_buf->buf.page_shift;
1282 inlen = MLX5_ST_SZ_BYTES(modify_cq_in) +
1283 MLX5_FLD_SZ_BYTES(modify_cq_in, pas[0]) * npas;
1285 in = mlx5_vzalloc(inlen);
1291 pas = (__be64 *)MLX5_ADDR_OF(modify_cq_in, in, pas);
1293 mlx5_ib_populate_pas(dev, cq->resize_umem, page_shift,
1296 mlx5_fill_page_array(&cq->resize_buf->buf, pas);
1298 MLX5_SET(modify_cq_in, in,
1299 modify_field_select_resize_field_select.resize_field_select.resize_field_select,
1300 MLX5_MODIFY_CQ_MASK_LOG_SIZE |
1301 MLX5_MODIFY_CQ_MASK_PG_OFFSET |
1302 MLX5_MODIFY_CQ_MASK_PG_SIZE);
1304 cqc = MLX5_ADDR_OF(modify_cq_in, in, cq_context);
1306 MLX5_SET(cqc, cqc, log_page_size,
1307 page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1308 MLX5_SET(cqc, cqc, cqe_sz, cqe_sz_to_mlx_sz(cqe_size));
1309 MLX5_SET(cqc, cqc, log_cq_size, ilog2(entries));
1311 MLX5_SET(modify_cq_in, in, op_mod, MLX5_CQ_OPMOD_RESIZE);
1312 MLX5_SET(modify_cq_in, in, cqn, cq->mcq.cqn);
1314 err = mlx5_core_modify_cq(dev->mdev, &cq->mcq, in, inlen);
1319 cq->ibcq.cqe = entries - 1;
1320 ib_umem_release(cq->buf.umem);
1321 cq->buf.umem = cq->resize_umem;
1322 cq->resize_umem = NULL;
1324 struct mlx5_ib_cq_buf tbuf;
1327 spin_lock_irqsave(&cq->lock, flags);
1328 if (cq->resize_buf) {
1329 err = copy_resize_cqes(cq);
1332 cq->buf = *cq->resize_buf;
1333 kfree(cq->resize_buf);
1334 cq->resize_buf = NULL;
1338 cq->ibcq.cqe = entries - 1;
1339 spin_unlock_irqrestore(&cq->lock, flags);
1341 free_cq_buf(dev, &tbuf);
1343 mutex_unlock(&cq->resize_mutex);
1355 un_resize_kernel(dev, cq);
1357 mutex_unlock(&cq->resize_mutex);
1361 int mlx5_ib_get_cqe_size(struct mlx5_ib_dev *dev, struct ib_cq *ibcq)
1363 struct mlx5_ib_cq *cq;
1369 return cq->cqe_size;
1372 /* Called from atomic context */
1373 int mlx5_ib_generate_wc(struct ib_cq *ibcq, struct ib_wc *wc)
1375 struct mlx5_ib_wc *soft_wc;
1376 struct mlx5_ib_cq *cq = to_mcq(ibcq);
1377 unsigned long flags;
1379 soft_wc = kmalloc(sizeof(*soft_wc), GFP_ATOMIC);
1384 spin_lock_irqsave(&cq->lock, flags);
1385 list_add_tail(&soft_wc->list, &cq->wc_list);
1386 if (cq->notify_flags == IB_CQ_NEXT_COMP ||
1387 wc->status != IB_WC_SUCCESS) {
1388 cq->notify_flags = 0;
1389 schedule_work(&cq->notify_work);
1391 spin_unlock_irqrestore(&cq->lock, flags);