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;
172 if (qp->ibqp.srq || qp->ibqp.xrcd) {
173 struct mlx5_core_srq *msrq = NULL;
176 msrq = mlx5_core_get_srq(dev->mdev,
177 be32_to_cpu(cqe->srqn));
178 srq = to_mibsrq(msrq);
180 srq = to_msrq(qp->ibqp.srq);
183 wqe_ctr = be16_to_cpu(cqe->wqe_counter);
184 wc->wr_id = srq->wrid[wqe_ctr];
185 mlx5_ib_free_srq_wqe(srq, wqe_ctr);
186 if (msrq && atomic_dec_and_test(&msrq->refcount))
187 complete(&msrq->free);
191 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
194 wc->byte_len = be32_to_cpu(cqe->byte_cnt);
196 switch (cqe->op_own >> 4) {
197 case MLX5_CQE_RESP_WR_IMM:
198 wc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
199 wc->wc_flags = IB_WC_WITH_IMM;
200 wc->ex.imm_data = cqe->imm_inval_pkey;
202 case MLX5_CQE_RESP_SEND:
203 wc->opcode = IB_WC_RECV;
204 wc->wc_flags = IB_WC_IP_CSUM_OK;
205 if (unlikely(!((cqe->hds_ip_ext & CQE_L3_OK) &&
206 (cqe->hds_ip_ext & CQE_L4_OK))))
209 case MLX5_CQE_RESP_SEND_IMM:
210 wc->opcode = IB_WC_RECV;
211 wc->wc_flags = IB_WC_WITH_IMM;
212 wc->ex.imm_data = cqe->imm_inval_pkey;
214 case MLX5_CQE_RESP_SEND_INV:
215 wc->opcode = IB_WC_RECV;
216 wc->wc_flags = IB_WC_WITH_INVALIDATE;
217 wc->ex.invalidate_rkey = be32_to_cpu(cqe->imm_inval_pkey);
220 wc->slid = be16_to_cpu(cqe->slid);
221 wc->sl = (be32_to_cpu(cqe->flags_rqpn) >> 24) & 0xf;
222 wc->src_qp = be32_to_cpu(cqe->flags_rqpn) & 0xffffff;
223 wc->dlid_path_bits = cqe->ml_path;
224 g = (be32_to_cpu(cqe->flags_rqpn) >> 28) & 3;
225 wc->wc_flags |= g ? IB_WC_GRH : 0;
226 if (unlikely(is_qp1(qp->ibqp.qp_type))) {
227 u16 pkey = be32_to_cpu(cqe->imm_inval_pkey) & 0xffff;
229 ib_find_cached_pkey(&dev->ib_dev, qp->port, pkey,
235 if (ll != IB_LINK_LAYER_ETHERNET)
238 switch (wc->sl & 0x3) {
239 case MLX5_CQE_ROCE_L3_HEADER_TYPE_GRH:
240 wc->network_hdr_type = RDMA_NETWORK_IB;
242 case MLX5_CQE_ROCE_L3_HEADER_TYPE_IPV6:
243 wc->network_hdr_type = RDMA_NETWORK_IPV6;
245 case MLX5_CQE_ROCE_L3_HEADER_TYPE_IPV4:
246 wc->network_hdr_type = RDMA_NETWORK_IPV4;
249 wc->wc_flags |= IB_WC_WITH_NETWORK_HDR_TYPE;
252 static void dump_cqe(struct mlx5_ib_dev *dev, struct mlx5_err_cqe *cqe)
254 __be32 *p = (__be32 *)cqe;
257 mlx5_ib_warn(dev, "dump error cqe\n");
258 for (i = 0; i < sizeof(*cqe) / 16; i++, p += 4)
259 pr_info("%08x %08x %08x %08x\n", be32_to_cpu(p[0]),
260 be32_to_cpu(p[1]), be32_to_cpu(p[2]),
264 static void mlx5_handle_error_cqe(struct mlx5_ib_dev *dev,
265 struct mlx5_err_cqe *cqe,
270 switch (cqe->syndrome) {
271 case MLX5_CQE_SYNDROME_LOCAL_LENGTH_ERR:
272 wc->status = IB_WC_LOC_LEN_ERR;
274 case MLX5_CQE_SYNDROME_LOCAL_QP_OP_ERR:
275 wc->status = IB_WC_LOC_QP_OP_ERR;
277 case MLX5_CQE_SYNDROME_LOCAL_PROT_ERR:
278 wc->status = IB_WC_LOC_PROT_ERR;
280 case MLX5_CQE_SYNDROME_WR_FLUSH_ERR:
282 wc->status = IB_WC_WR_FLUSH_ERR;
284 case MLX5_CQE_SYNDROME_MW_BIND_ERR:
285 wc->status = IB_WC_MW_BIND_ERR;
287 case MLX5_CQE_SYNDROME_BAD_RESP_ERR:
288 wc->status = IB_WC_BAD_RESP_ERR;
290 case MLX5_CQE_SYNDROME_LOCAL_ACCESS_ERR:
291 wc->status = IB_WC_LOC_ACCESS_ERR;
293 case MLX5_CQE_SYNDROME_REMOTE_INVAL_REQ_ERR:
294 wc->status = IB_WC_REM_INV_REQ_ERR;
296 case MLX5_CQE_SYNDROME_REMOTE_ACCESS_ERR:
297 wc->status = IB_WC_REM_ACCESS_ERR;
299 case MLX5_CQE_SYNDROME_REMOTE_OP_ERR:
300 wc->status = IB_WC_REM_OP_ERR;
302 case MLX5_CQE_SYNDROME_TRANSPORT_RETRY_EXC_ERR:
303 wc->status = IB_WC_RETRY_EXC_ERR;
306 case MLX5_CQE_SYNDROME_RNR_RETRY_EXC_ERR:
307 wc->status = IB_WC_RNR_RETRY_EXC_ERR;
310 case MLX5_CQE_SYNDROME_REMOTE_ABORTED_ERR:
311 wc->status = IB_WC_REM_ABORT_ERR;
314 wc->status = IB_WC_GENERAL_ERR;
318 wc->vendor_err = cqe->vendor_err_synd;
323 static int is_atomic_response(struct mlx5_ib_qp *qp, uint16_t idx)
325 /* TBD: waiting decision
330 static void *mlx5_get_atomic_laddr(struct mlx5_ib_qp *qp, uint16_t idx)
332 struct mlx5_wqe_data_seg *dpseg;
335 dpseg = mlx5_get_send_wqe(qp, idx) + sizeof(struct mlx5_wqe_ctrl_seg) +
336 sizeof(struct mlx5_wqe_raddr_seg) +
337 sizeof(struct mlx5_wqe_atomic_seg);
338 addr = (void *)(unsigned long)be64_to_cpu(dpseg->addr);
342 static void handle_atomic(struct mlx5_ib_qp *qp, struct mlx5_cqe64 *cqe64,
349 if (!is_atomic_response(qp, idx))
352 byte_count = be32_to_cpu(cqe64->byte_cnt);
353 addr = mlx5_get_atomic_laddr(qp, idx);
355 if (byte_count == 4) {
356 *(uint32_t *)addr = be32_to_cpu(*((__be32 *)addr));
358 for (i = 0; i < byte_count; i += 8) {
359 *(uint64_t *)addr = be64_to_cpu(*((__be64 *)addr));
367 static void handle_atomics(struct mlx5_ib_qp *qp, struct mlx5_cqe64 *cqe64,
373 idx = tail & (qp->sq.wqe_cnt - 1);
374 handle_atomic(qp, cqe64, idx);
378 tail = qp->sq.w_list[idx].next;
380 tail = qp->sq.w_list[idx].next;
381 qp->sq.last_poll = tail;
384 static void free_cq_buf(struct mlx5_ib_dev *dev, struct mlx5_ib_cq_buf *buf)
386 mlx5_buf_free(dev->mdev, &buf->buf);
389 static void get_sig_err_item(struct mlx5_sig_err_cqe *cqe,
390 struct ib_sig_err *item)
392 u16 syndrome = be16_to_cpu(cqe->syndrome);
394 #define GUARD_ERR (1 << 13)
395 #define APPTAG_ERR (1 << 12)
396 #define REFTAG_ERR (1 << 11)
398 if (syndrome & GUARD_ERR) {
399 item->err_type = IB_SIG_BAD_GUARD;
400 item->expected = be32_to_cpu(cqe->expected_trans_sig) >> 16;
401 item->actual = be32_to_cpu(cqe->actual_trans_sig) >> 16;
403 if (syndrome & REFTAG_ERR) {
404 item->err_type = IB_SIG_BAD_REFTAG;
405 item->expected = be32_to_cpu(cqe->expected_reftag);
406 item->actual = be32_to_cpu(cqe->actual_reftag);
408 if (syndrome & APPTAG_ERR) {
409 item->err_type = IB_SIG_BAD_APPTAG;
410 item->expected = be32_to_cpu(cqe->expected_trans_sig) & 0xffff;
411 item->actual = be32_to_cpu(cqe->actual_trans_sig) & 0xffff;
413 pr_err("Got signature completion error with bad syndrome %04x\n",
417 item->sig_err_offset = be64_to_cpu(cqe->err_offset);
418 item->key = be32_to_cpu(cqe->mkey);
421 static void sw_send_comp(struct mlx5_ib_qp *qp, int num_entries,
422 struct ib_wc *wc, int *npolled)
424 struct mlx5_ib_wq *wq;
431 cur = wq->head - wq->tail;
437 for (i = 0; i < cur && np < num_entries; i++) {
438 idx = wq->last_poll & (wq->wqe_cnt - 1);
439 wc->wr_id = wq->wrid[idx];
440 wc->status = IB_WC_WR_FLUSH_ERR;
441 wc->vendor_err = MLX5_CQE_SYNDROME_WR_FLUSH_ERR;
446 wq->last_poll = wq->w_list[idx].next;
451 static void sw_recv_comp(struct mlx5_ib_qp *qp, int num_entries,
452 struct ib_wc *wc, int *npolled)
454 struct mlx5_ib_wq *wq;
460 cur = wq->head - wq->tail;
466 for (i = 0; i < cur && np < num_entries; i++) {
467 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
468 wc->status = IB_WC_WR_FLUSH_ERR;
469 wc->vendor_err = MLX5_CQE_SYNDROME_WR_FLUSH_ERR;
478 static void mlx5_ib_poll_sw_comp(struct mlx5_ib_cq *cq, int num_entries,
479 struct ib_wc *wc, int *npolled)
481 struct mlx5_ib_qp *qp;
484 /* Find uncompleted WQEs belonging to that cq and retrun mmics ones */
485 list_for_each_entry(qp, &cq->list_send_qp, cq_send_list) {
486 sw_send_comp(qp, num_entries, wc + *npolled, npolled);
487 if (*npolled >= num_entries)
491 list_for_each_entry(qp, &cq->list_recv_qp, cq_recv_list) {
492 sw_recv_comp(qp, num_entries, wc + *npolled, npolled);
493 if (*npolled >= num_entries)
498 static int mlx5_poll_one(struct mlx5_ib_cq *cq,
499 struct mlx5_ib_qp **cur_qp,
502 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
503 struct mlx5_err_cqe *err_cqe;
504 struct mlx5_cqe64 *cqe64;
505 struct mlx5_core_qp *mqp;
506 struct mlx5_ib_wq *wq;
507 struct mlx5_sig_err_cqe *sig_err_cqe;
508 struct mlx5_core_mr *mmkey;
509 struct mlx5_ib_mr *mr;
518 cqe = next_cqe_sw(cq);
522 cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
524 ++cq->mcq.cons_index;
526 /* Make sure we read CQ entry contents after we've checked the
531 opcode = cqe64->op_own >> 4;
532 if (unlikely(opcode == MLX5_CQE_RESIZE_CQ)) {
533 if (likely(cq->resize_buf)) {
534 free_cq_buf(dev, &cq->buf);
535 cq->buf = *cq->resize_buf;
536 kfree(cq->resize_buf);
537 cq->resize_buf = NULL;
540 mlx5_ib_warn(dev, "unexpected resize cqe\n");
544 qpn = ntohl(cqe64->sop_drop_qpn) & 0xffffff;
545 if (!*cur_qp || (qpn != (*cur_qp)->ibqp.qp_num)) {
546 /* We do not have to take the QP table lock here,
547 * because CQs will be locked while QPs are removed
550 mqp = __mlx5_qp_lookup(dev->mdev, qpn);
551 *cur_qp = to_mibqp(mqp);
554 wc->qp = &(*cur_qp)->ibqp;
558 wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
559 idx = wqe_ctr & (wq->wqe_cnt - 1);
560 handle_good_req(wc, cqe64, wq, idx);
561 handle_atomics(*cur_qp, cqe64, wq->last_poll, idx);
562 wc->wr_id = wq->wrid[idx];
563 wq->tail = wq->wqe_head[idx] + 1;
564 wc->status = IB_WC_SUCCESS;
566 case MLX5_CQE_RESP_WR_IMM:
567 case MLX5_CQE_RESP_SEND:
568 case MLX5_CQE_RESP_SEND_IMM:
569 case MLX5_CQE_RESP_SEND_INV:
570 handle_responder(wc, cqe64, *cur_qp);
571 wc->status = IB_WC_SUCCESS;
573 case MLX5_CQE_RESIZE_CQ:
575 case MLX5_CQE_REQ_ERR:
576 case MLX5_CQE_RESP_ERR:
577 err_cqe = (struct mlx5_err_cqe *)cqe64;
578 mlx5_handle_error_cqe(dev, err_cqe, wc);
579 mlx5_ib_dbg(dev, "%s error cqe on cqn 0x%x:\n",
580 opcode == MLX5_CQE_REQ_ERR ?
581 "Requestor" : "Responder", cq->mcq.cqn);
582 mlx5_ib_dbg(dev, "syndrome 0x%x, vendor syndrome 0x%x\n",
583 err_cqe->syndrome, err_cqe->vendor_err_synd);
584 if (opcode == MLX5_CQE_REQ_ERR) {
586 wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
587 idx = wqe_ctr & (wq->wqe_cnt - 1);
588 wc->wr_id = wq->wrid[idx];
589 wq->tail = wq->wqe_head[idx] + 1;
591 struct mlx5_ib_srq *srq;
593 if ((*cur_qp)->ibqp.srq) {
594 srq = to_msrq((*cur_qp)->ibqp.srq);
595 wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
596 wc->wr_id = srq->wrid[wqe_ctr];
597 mlx5_ib_free_srq_wqe(srq, wqe_ctr);
600 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
605 case MLX5_CQE_SIG_ERR:
606 sig_err_cqe = (struct mlx5_sig_err_cqe *)cqe64;
608 spin_lock_irqsave(&dev->mdev->priv.mr_table.lock, flags);
609 mmkey = __mlx5_mr_lookup(dev->mdev,
610 mlx5_base_mkey(be32_to_cpu(sig_err_cqe->mkey)));
611 mr = to_mibmr(mmkey);
612 get_sig_err_item(sig_err_cqe, &mr->sig->err_item);
613 mr->sig->sig_err_exists = true;
614 mr->sig->sigerr_count++;
616 mlx5_ib_warn(dev, "CQN: 0x%x Got SIGERR on key: 0x%x err_type %x err_offset %llx expected %x actual %x\n",
617 cq->mcq.cqn, mr->sig->err_item.key,
618 mr->sig->err_item.err_type,
619 (long long)mr->sig->err_item.sig_err_offset,
620 mr->sig->err_item.expected,
621 mr->sig->err_item.actual);
623 spin_unlock_irqrestore(&dev->mdev->priv.mr_table.lock, flags);
630 static int poll_soft_wc(struct mlx5_ib_cq *cq, int num_entries,
633 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
634 struct mlx5_ib_wc *soft_wc, *next;
637 list_for_each_entry_safe(soft_wc, next, &cq->wc_list, list) {
638 if (npolled >= num_entries)
641 mlx5_ib_dbg(dev, "polled software generated completion on CQ 0x%x\n",
644 wc[npolled++] = soft_wc->wc;
645 list_del(&soft_wc->list);
652 int mlx5_ib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
654 struct mlx5_ib_cq *cq = to_mcq(ibcq);
655 struct mlx5_ib_qp *cur_qp = NULL;
656 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
657 struct mlx5_core_dev *mdev = dev->mdev;
662 spin_lock_irqsave(&cq->lock, flags);
663 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
664 mlx5_ib_poll_sw_comp(cq, num_entries, wc, &npolled);
668 if (unlikely(!list_empty(&cq->wc_list)))
669 soft_polled = poll_soft_wc(cq, num_entries, wc);
671 for (npolled = 0; npolled < num_entries - soft_polled; npolled++) {
672 if (mlx5_poll_one(cq, &cur_qp, wc + soft_polled + npolled))
677 mlx5_cq_set_ci(&cq->mcq);
679 spin_unlock_irqrestore(&cq->lock, flags);
681 return soft_polled + npolled;
684 int mlx5_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
686 struct mlx5_core_dev *mdev = to_mdev(ibcq->device)->mdev;
687 struct mlx5_ib_cq *cq = to_mcq(ibcq);
688 void __iomem *uar_page = mdev->priv.uuari.uars[0].map;
689 unsigned long irq_flags;
692 spin_lock_irqsave(&cq->lock, irq_flags);
693 if (cq->notify_flags != IB_CQ_NEXT_COMP)
694 cq->notify_flags = flags & IB_CQ_SOLICITED_MASK;
696 if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !list_empty(&cq->wc_list))
698 spin_unlock_irqrestore(&cq->lock, irq_flags);
700 mlx5_cq_arm(&cq->mcq,
701 (flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?
702 MLX5_CQ_DB_REQ_NOT_SOL : MLX5_CQ_DB_REQ_NOT,
704 MLX5_GET_DOORBELL_LOCK(&mdev->priv.cq_uar_lock),
705 to_mcq(ibcq)->mcq.cons_index);
710 static int alloc_cq_buf(struct mlx5_ib_dev *dev, struct mlx5_ib_cq_buf *buf,
711 int nent, int cqe_size)
715 err = mlx5_buf_alloc(dev->mdev, nent * cqe_size,
716 2 * PAGE_SIZE, &buf->buf);
720 buf->cqe_size = cqe_size;
726 static int create_cq_user(struct mlx5_ib_dev *dev, struct ib_udata *udata,
727 struct ib_ucontext *context, struct mlx5_ib_cq *cq,
728 int entries, u32 **cqb,
729 int *cqe_size, int *index, int *inlen)
731 struct mlx5_ib_create_cq ucmd;
741 (udata->inlen - sizeof(struct ib_uverbs_cmd_hdr) <
742 sizeof(ucmd)) ? (sizeof(ucmd) -
743 sizeof(ucmd.reserved)) : sizeof(ucmd);
745 if (ib_copy_from_udata(&ucmd, udata, ucmdlen))
748 if (ucmdlen == sizeof(ucmd) &&
752 if (ucmd.cqe_size != 64 && ucmd.cqe_size != 128)
755 *cqe_size = ucmd.cqe_size;
757 cq->buf.umem = ib_umem_get(context, ucmd.buf_addr,
758 entries * ucmd.cqe_size,
759 IB_ACCESS_LOCAL_WRITE, 1);
760 if (IS_ERR(cq->buf.umem)) {
761 err = PTR_ERR(cq->buf.umem);
765 err = mlx5_ib_db_map_user(to_mucontext(context), ucmd.db_addr,
770 mlx5_ib_cont_pages(cq->buf.umem, ucmd.buf_addr, &npages, &page_shift,
772 mlx5_ib_dbg(dev, "addr 0x%llx, size %u, npages %d, page_shift %d, ncont %d\n",
773 (long long)ucmd.buf_addr, entries * ucmd.cqe_size, npages, page_shift, ncont);
775 *inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
776 MLX5_FLD_SZ_BYTES(create_cq_in, pas[0]) * ncont;
777 *cqb = mlx5_vzalloc(*inlen);
783 pas = (__be64 *)MLX5_ADDR_OF(create_cq_in, *cqb, pas);
784 mlx5_ib_populate_pas(dev, cq->buf.umem, page_shift, pas, 0);
786 cqc = MLX5_ADDR_OF(create_cq_in, *cqb, cq_context);
787 MLX5_SET(cqc, cqc, log_page_size,
788 page_shift - MLX5_ADAPTER_PAGE_SHIFT);
790 *index = to_mucontext(context)->uuari.uars[0].index;
795 mlx5_ib_db_unmap_user(to_mucontext(context), &cq->db);
798 ib_umem_release(cq->buf.umem);
802 static void destroy_cq_user(struct mlx5_ib_cq *cq, struct ib_ucontext *context)
804 mlx5_ib_db_unmap_user(to_mucontext(context), &cq->db);
805 ib_umem_release(cq->buf.umem);
808 static void init_cq_buf(struct mlx5_ib_cq *cq, struct mlx5_ib_cq_buf *buf)
812 struct mlx5_cqe64 *cqe64;
814 for (i = 0; i < buf->nent; i++) {
815 cqe = get_cqe_from_buf(buf, i, buf->cqe_size);
816 cqe64 = buf->cqe_size == 64 ? cqe : cqe + 64;
817 cqe64->op_own = MLX5_CQE_INVALID << 4;
821 static int create_cq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
822 int entries, int cqe_size,
823 u32 **cqb, int *index, int *inlen)
829 err = mlx5_db_alloc(dev->mdev, &cq->db);
833 cq->mcq.set_ci_db = cq->db.db;
834 cq->mcq.arm_db = cq->db.db + 1;
835 cq->mcq.cqe_sz = cqe_size;
837 err = alloc_cq_buf(dev, &cq->buf, entries, cqe_size);
841 init_cq_buf(cq, &cq->buf);
843 *inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
844 MLX5_FLD_SZ_BYTES(create_cq_in, pas[0]) * cq->buf.buf.npages;
845 *cqb = mlx5_vzalloc(*inlen);
851 pas = (__be64 *)MLX5_ADDR_OF(create_cq_in, *cqb, pas);
852 mlx5_fill_page_array(&cq->buf.buf, pas);
854 cqc = MLX5_ADDR_OF(create_cq_in, *cqb, cq_context);
855 MLX5_SET(cqc, cqc, log_page_size,
856 cq->buf.buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
858 *index = dev->mdev->priv.uuari.uars[0].index;
863 free_cq_buf(dev, &cq->buf);
866 mlx5_db_free(dev->mdev, &cq->db);
870 static void destroy_cq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq)
872 free_cq_buf(dev, &cq->buf);
873 mlx5_db_free(dev->mdev, &cq->db);
876 static void notify_soft_wc_handler(struct work_struct *work)
878 struct mlx5_ib_cq *cq = container_of(work, struct mlx5_ib_cq,
881 cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
884 struct ib_cq *mlx5_ib_create_cq(struct ib_device *ibdev,
885 const struct ib_cq_init_attr *attr,
886 struct ib_ucontext *context,
887 struct ib_udata *udata)
889 int entries = attr->cqe;
890 int vector = attr->comp_vector;
891 struct mlx5_ib_dev *dev = to_mdev(ibdev);
892 struct mlx5_ib_cq *cq;
893 int uninitialized_var(index);
894 int uninitialized_var(inlen);
903 (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz))))
904 return ERR_PTR(-EINVAL);
906 if (check_cq_create_flags(attr->flags))
907 return ERR_PTR(-EOPNOTSUPP);
909 entries = roundup_pow_of_two(entries + 1);
910 if (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz)))
911 return ERR_PTR(-EINVAL);
913 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
915 return ERR_PTR(-ENOMEM);
917 cq->ibcq.cqe = entries - 1;
918 mutex_init(&cq->resize_mutex);
919 spin_lock_init(&cq->lock);
920 cq->resize_buf = NULL;
921 cq->resize_umem = NULL;
922 cq->create_flags = attr->flags;
923 INIT_LIST_HEAD(&cq->list_send_qp);
924 INIT_LIST_HEAD(&cq->list_recv_qp);
927 err = create_cq_user(dev, udata, context, cq, entries,
928 &cqb, &cqe_size, &index, &inlen);
932 cqe_size = cache_line_size() == 128 ? 128 : 64;
933 err = create_cq_kernel(dev, cq, entries, cqe_size, &cqb,
938 INIT_WORK(&cq->notify_work, notify_soft_wc_handler);
941 err = mlx5_vector2eqn(dev->mdev, vector, &eqn, &irqn);
945 cq->cqe_size = cqe_size;
947 cqc = MLX5_ADDR_OF(create_cq_in, cqb, cq_context);
948 MLX5_SET(cqc, cqc, cqe_sz, cqe_sz_to_mlx_sz(cqe_size));
949 MLX5_SET(cqc, cqc, log_cq_size, ilog2(entries));
950 MLX5_SET(cqc, cqc, uar_page, index);
951 MLX5_SET(cqc, cqc, c_eqn, eqn);
952 MLX5_SET64(cqc, cqc, dbr_addr, cq->db.dma);
953 if (cq->create_flags & IB_CQ_FLAGS_IGNORE_OVERRUN)
954 MLX5_SET(cqc, cqc, oi, 1);
956 err = mlx5_core_create_cq(dev->mdev, &cq->mcq,
957 (struct mlx5_create_cq_mbox_in *)cqb, inlen);
961 mlx5_ib_dbg(dev, "cqn 0x%x\n", cq->mcq.cqn);
963 cq->mcq.comp = mlx5_ib_cq_comp;
964 cq->mcq.event = mlx5_ib_cq_event;
966 INIT_LIST_HEAD(&cq->wc_list);
969 if (ib_copy_to_udata(udata, &cq->mcq.cqn, sizeof(__u32))) {
979 mlx5_core_destroy_cq(dev->mdev, &cq->mcq);
984 destroy_cq_user(cq, context);
986 destroy_cq_kernel(dev, cq);
995 int mlx5_ib_destroy_cq(struct ib_cq *cq)
997 struct mlx5_ib_dev *dev = to_mdev(cq->device);
998 struct mlx5_ib_cq *mcq = to_mcq(cq);
999 struct ib_ucontext *context = NULL;
1002 context = cq->uobject->context;
1004 mlx5_core_destroy_cq(dev->mdev, &mcq->mcq);
1006 destroy_cq_user(mcq, context);
1008 destroy_cq_kernel(dev, mcq);
1015 static int is_equal_rsn(struct mlx5_cqe64 *cqe64, u32 rsn)
1017 return rsn == (ntohl(cqe64->sop_drop_qpn) & 0xffffff);
1020 void __mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 rsn, struct mlx5_ib_srq *srq)
1022 struct mlx5_cqe64 *cqe64, *dest64;
1031 /* First we need to find the current producer index, so we
1032 * know where to start cleaning from. It doesn't matter if HW
1033 * adds new entries after this loop -- the QP we're worried
1034 * about is already in RESET, so the new entries won't come
1035 * from our QP and therefore don't need to be checked.
1037 for (prod_index = cq->mcq.cons_index; get_sw_cqe(cq, prod_index); prod_index++)
1038 if (prod_index == cq->mcq.cons_index + cq->ibcq.cqe)
1041 /* Now sweep backwards through the CQ, removing CQ entries
1042 * that match our QP by copying older entries on top of them.
1044 while ((int) --prod_index - (int) cq->mcq.cons_index >= 0) {
1045 cqe = get_cqe(cq, prod_index & cq->ibcq.cqe);
1046 cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
1047 if (is_equal_rsn(cqe64, rsn)) {
1048 if (srq && (ntohl(cqe64->srqn) & 0xffffff))
1049 mlx5_ib_free_srq_wqe(srq, be16_to_cpu(cqe64->wqe_counter));
1051 } else if (nfreed) {
1052 dest = get_cqe(cq, (prod_index + nfreed) & cq->ibcq.cqe);
1053 dest64 = (cq->mcq.cqe_sz == 64) ? dest : dest + 64;
1054 owner_bit = dest64->op_own & MLX5_CQE_OWNER_MASK;
1055 memcpy(dest, cqe, cq->mcq.cqe_sz);
1056 dest64->op_own = owner_bit |
1057 (dest64->op_own & ~MLX5_CQE_OWNER_MASK);
1062 cq->mcq.cons_index += nfreed;
1063 /* Make sure update of buffer contents is done before
1064 * updating consumer index.
1067 mlx5_cq_set_ci(&cq->mcq);
1071 void mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 qpn, struct mlx5_ib_srq *srq)
1076 spin_lock_irq(&cq->lock);
1077 __mlx5_ib_cq_clean(cq, qpn, srq);
1078 spin_unlock_irq(&cq->lock);
1081 int mlx5_ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
1083 struct mlx5_ib_dev *dev = to_mdev(cq->device);
1084 struct mlx5_ib_cq *mcq = to_mcq(cq);
1087 if (!MLX5_CAP_GEN(dev->mdev, cq_moderation))
1090 err = mlx5_core_modify_cq_moderation(dev->mdev, &mcq->mcq,
1091 cq_period, cq_count);
1093 mlx5_ib_warn(dev, "modify cq 0x%x failed\n", mcq->mcq.cqn);
1098 static int resize_user(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
1099 int entries, struct ib_udata *udata, int *npas,
1100 int *page_shift, int *cqe_size)
1102 struct mlx5_ib_resize_cq ucmd;
1103 struct ib_umem *umem;
1106 struct ib_ucontext *context = cq->buf.umem->context;
1108 err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
1112 if (ucmd.reserved0 || ucmd.reserved1)
1115 umem = ib_umem_get(context, ucmd.buf_addr, entries * ucmd.cqe_size,
1116 IB_ACCESS_LOCAL_WRITE, 1);
1118 err = PTR_ERR(umem);
1122 mlx5_ib_cont_pages(umem, ucmd.buf_addr, &npages, page_shift,
1125 cq->resize_umem = umem;
1126 *cqe_size = ucmd.cqe_size;
1131 static void un_resize_user(struct mlx5_ib_cq *cq)
1133 ib_umem_release(cq->resize_umem);
1136 static int resize_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
1137 int entries, int cqe_size)
1141 cq->resize_buf = kzalloc(sizeof(*cq->resize_buf), GFP_KERNEL);
1142 if (!cq->resize_buf)
1145 err = alloc_cq_buf(dev, cq->resize_buf, entries, cqe_size);
1149 init_cq_buf(cq, cq->resize_buf);
1154 kfree(cq->resize_buf);
1158 static void un_resize_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq)
1160 free_cq_buf(dev, cq->resize_buf);
1161 cq->resize_buf = NULL;
1164 static int copy_resize_cqes(struct mlx5_ib_cq *cq)
1166 struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
1167 struct mlx5_cqe64 *scqe64;
1168 struct mlx5_cqe64 *dcqe64;
1177 ssize = cq->buf.cqe_size;
1178 dsize = cq->resize_buf->cqe_size;
1179 if (ssize != dsize) {
1180 mlx5_ib_warn(dev, "resize from different cqe size is not supported\n");
1184 i = cq->mcq.cons_index;
1185 scqe = get_sw_cqe(cq, i);
1186 scqe64 = ssize == 64 ? scqe : scqe + 64;
1189 mlx5_ib_warn(dev, "expected cqe in sw ownership\n");
1193 while ((scqe64->op_own >> 4) != MLX5_CQE_RESIZE_CQ) {
1194 dcqe = get_cqe_from_buf(cq->resize_buf,
1195 (i + 1) & (cq->resize_buf->nent),
1197 dcqe64 = dsize == 64 ? dcqe : dcqe + 64;
1198 sw_own = sw_ownership_bit(i + 1, cq->resize_buf->nent);
1199 memcpy(dcqe, scqe, dsize);
1200 dcqe64->op_own = (dcqe64->op_own & ~MLX5_CQE_OWNER_MASK) | sw_own;
1203 scqe = get_sw_cqe(cq, i);
1204 scqe64 = ssize == 64 ? scqe : scqe + 64;
1206 mlx5_ib_warn(dev, "expected cqe in sw ownership\n");
1210 if (scqe == start_cqe) {
1211 pr_warn("resize CQ failed to get resize CQE, CQN 0x%x\n",
1216 ++cq->mcq.cons_index;
1220 int mlx5_ib_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
1222 struct mlx5_ib_dev *dev = to_mdev(ibcq->device);
1223 struct mlx5_ib_cq *cq = to_mcq(ibcq);
1231 int uninitialized_var(cqe_size);
1232 unsigned long flags;
1234 if (!MLX5_CAP_GEN(dev->mdev, cq_resize)) {
1235 pr_info("Firmware does not support resize CQ\n");
1240 entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz))) {
1241 mlx5_ib_warn(dev, "wrong entries number %d, max %d\n",
1243 1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz));
1247 entries = roundup_pow_of_two(entries + 1);
1248 if (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz)) + 1)
1251 if (entries == ibcq->cqe + 1)
1254 mutex_lock(&cq->resize_mutex);
1256 err = resize_user(dev, cq, entries, udata, &npas, &page_shift,
1260 err = resize_kernel(dev, cq, entries, cqe_size);
1262 npas = cq->resize_buf->buf.npages;
1263 page_shift = cq->resize_buf->buf.page_shift;
1270 inlen = MLX5_ST_SZ_BYTES(modify_cq_in) +
1271 MLX5_FLD_SZ_BYTES(modify_cq_in, pas[0]) * npas;
1273 in = mlx5_vzalloc(inlen);
1279 pas = (__be64 *)MLX5_ADDR_OF(modify_cq_in, in, pas);
1281 mlx5_ib_populate_pas(dev, cq->resize_umem, page_shift,
1284 mlx5_fill_page_array(&cq->resize_buf->buf, pas);
1286 MLX5_SET(modify_cq_in, in,
1287 modify_field_select_resize_field_select.resize_field_select.resize_field_select,
1288 MLX5_MODIFY_CQ_MASK_LOG_SIZE |
1289 MLX5_MODIFY_CQ_MASK_PG_OFFSET |
1290 MLX5_MODIFY_CQ_MASK_PG_SIZE);
1292 cqc = MLX5_ADDR_OF(modify_cq_in, in, cq_context);
1294 MLX5_SET(cqc, cqc, log_page_size,
1295 page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1296 MLX5_SET(cqc, cqc, cqe_sz, cqe_sz_to_mlx_sz(cqe_size));
1297 MLX5_SET(cqc, cqc, log_cq_size, ilog2(entries));
1299 MLX5_SET(modify_cq_in, in, op_mod, MLX5_CQ_OPMOD_RESIZE);
1300 MLX5_SET(modify_cq_in, in, cqn, cq->mcq.cqn);
1302 err = mlx5_core_modify_cq(dev->mdev, &cq->mcq,
1303 (struct mlx5_modify_cq_mbox_in *)in, inlen);
1308 cq->ibcq.cqe = entries - 1;
1309 ib_umem_release(cq->buf.umem);
1310 cq->buf.umem = cq->resize_umem;
1311 cq->resize_umem = NULL;
1313 struct mlx5_ib_cq_buf tbuf;
1316 spin_lock_irqsave(&cq->lock, flags);
1317 if (cq->resize_buf) {
1318 err = copy_resize_cqes(cq);
1321 cq->buf = *cq->resize_buf;
1322 kfree(cq->resize_buf);
1323 cq->resize_buf = NULL;
1327 cq->ibcq.cqe = entries - 1;
1328 spin_unlock_irqrestore(&cq->lock, flags);
1330 free_cq_buf(dev, &tbuf);
1332 mutex_unlock(&cq->resize_mutex);
1344 un_resize_kernel(dev, cq);
1346 mutex_unlock(&cq->resize_mutex);
1350 int mlx5_ib_get_cqe_size(struct mlx5_ib_dev *dev, struct ib_cq *ibcq)
1352 struct mlx5_ib_cq *cq;
1358 return cq->cqe_size;
1361 /* Called from atomic context */
1362 int mlx5_ib_generate_wc(struct ib_cq *ibcq, struct ib_wc *wc)
1364 struct mlx5_ib_wc *soft_wc;
1365 struct mlx5_ib_cq *cq = to_mcq(ibcq);
1366 unsigned long flags;
1368 soft_wc = kmalloc(sizeof(*soft_wc), GFP_ATOMIC);
1373 spin_lock_irqsave(&cq->lock, flags);
1374 list_add_tail(&soft_wc->list, &cq->wc_list);
1375 if (cq->notify_flags == IB_CQ_NEXT_COMP ||
1376 wc->status != IB_WC_SUCCESS) {
1377 cq->notify_flags = 0;
1378 schedule_work(&cq->notify_work);
1380 spin_unlock_irqrestore(&cq->lock, flags);