2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * $Id: mthca_cq.c 1369 2004-12-20 16:17:07Z roland $
36 #include <linux/init.h>
37 #include <linux/hardirq.h>
41 #include "mthca_dev.h"
42 #include "mthca_cmd.h"
43 #include "mthca_memfree.h"
46 MTHCA_MAX_DIRECT_CQ_SIZE = 4 * PAGE_SIZE
50 MTHCA_CQ_ENTRY_SIZE = 0x20
54 * Must be packed because start is 64 bits but only aligned to 32 bits.
56 struct mthca_cq_context {
60 u32 error_eqn; /* Tavor only */
64 u32 last_notified_index;
65 u32 solicit_producer_index;
69 u32 ci_db; /* Arbel only */
70 u32 state_db; /* Arbel only */
72 } __attribute__((packed));
74 #define MTHCA_CQ_STATUS_OK ( 0 << 28)
75 #define MTHCA_CQ_STATUS_OVERFLOW ( 9 << 28)
76 #define MTHCA_CQ_STATUS_WRITE_FAIL (10 << 28)
77 #define MTHCA_CQ_FLAG_TR ( 1 << 18)
78 #define MTHCA_CQ_FLAG_OI ( 1 << 17)
79 #define MTHCA_CQ_STATE_DISARMED ( 0 << 8)
80 #define MTHCA_CQ_STATE_ARMED ( 1 << 8)
81 #define MTHCA_CQ_STATE_ARMED_SOL ( 4 << 8)
82 #define MTHCA_EQ_STATE_FIRED (10 << 8)
85 MTHCA_ERROR_CQE_OPCODE_MASK = 0xfe
89 SYNDROME_LOCAL_LENGTH_ERR = 0x01,
90 SYNDROME_LOCAL_QP_OP_ERR = 0x02,
91 SYNDROME_LOCAL_EEC_OP_ERR = 0x03,
92 SYNDROME_LOCAL_PROT_ERR = 0x04,
93 SYNDROME_WR_FLUSH_ERR = 0x05,
94 SYNDROME_MW_BIND_ERR = 0x06,
95 SYNDROME_BAD_RESP_ERR = 0x10,
96 SYNDROME_LOCAL_ACCESS_ERR = 0x11,
97 SYNDROME_REMOTE_INVAL_REQ_ERR = 0x12,
98 SYNDROME_REMOTE_ACCESS_ERR = 0x13,
99 SYNDROME_REMOTE_OP_ERR = 0x14,
100 SYNDROME_RETRY_EXC_ERR = 0x15,
101 SYNDROME_RNR_RETRY_EXC_ERR = 0x16,
102 SYNDROME_LOCAL_RDD_VIOL_ERR = 0x20,
103 SYNDROME_REMOTE_INVAL_RD_REQ_ERR = 0x21,
104 SYNDROME_REMOTE_ABORTED_ERR = 0x22,
105 SYNDROME_INVAL_EECN_ERR = 0x23,
106 SYNDROME_INVAL_EEC_STATE_ERR = 0x24
115 u32 imm_etype_pkey_eec;
124 struct mthca_err_cqe {
137 #define MTHCA_CQ_ENTRY_OWNER_SW (0 << 7)
138 #define MTHCA_CQ_ENTRY_OWNER_HW (1 << 7)
140 #define MTHCA_TAVOR_CQ_DB_INC_CI (1 << 24)
141 #define MTHCA_TAVOR_CQ_DB_REQ_NOT (2 << 24)
142 #define MTHCA_TAVOR_CQ_DB_REQ_NOT_SOL (3 << 24)
143 #define MTHCA_TAVOR_CQ_DB_SET_CI (4 << 24)
144 #define MTHCA_TAVOR_CQ_DB_REQ_NOT_MULT (5 << 24)
146 #define MTHCA_ARBEL_CQ_DB_REQ_NOT_SOL (1 << 24)
147 #define MTHCA_ARBEL_CQ_DB_REQ_NOT (2 << 24)
148 #define MTHCA_ARBEL_CQ_DB_REQ_NOT_MULT (3 << 24)
150 static inline struct mthca_cqe *get_cqe(struct mthca_cq *cq, int entry)
153 return cq->queue.direct.buf + (entry * MTHCA_CQ_ENTRY_SIZE);
155 return cq->queue.page_list[entry * MTHCA_CQ_ENTRY_SIZE / PAGE_SIZE].buf
156 + (entry * MTHCA_CQ_ENTRY_SIZE) % PAGE_SIZE;
159 static inline struct mthca_cqe *cqe_sw(struct mthca_cq *cq, int i)
161 struct mthca_cqe *cqe = get_cqe(cq, i);
162 return MTHCA_CQ_ENTRY_OWNER_HW & cqe->owner ? NULL : cqe;
165 static inline struct mthca_cqe *next_cqe_sw(struct mthca_cq *cq)
167 return cqe_sw(cq, cq->cons_index & cq->ibcq.cqe);
170 static inline void set_cqe_hw(struct mthca_cqe *cqe)
172 cqe->owner = MTHCA_CQ_ENTRY_OWNER_HW;
176 * incr is ignored in native Arbel (mem-free) mode, so cq->cons_index
177 * should be correct before calling update_cons_index().
179 static inline void update_cons_index(struct mthca_dev *dev, struct mthca_cq *cq,
184 if (mthca_is_memfree(dev)) {
185 *cq->set_ci_db = cpu_to_be32(cq->cons_index);
188 doorbell[0] = cpu_to_be32(MTHCA_TAVOR_CQ_DB_INC_CI | cq->cqn);
189 doorbell[1] = cpu_to_be32(incr - 1);
191 mthca_write64(doorbell,
192 dev->kar + MTHCA_CQ_DOORBELL,
193 MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
197 void mthca_cq_event(struct mthca_dev *dev, u32 cqn)
201 cq = mthca_array_get(&dev->cq_table.cq, cqn & (dev->limits.num_cqs - 1));
204 mthca_warn(dev, "Completion event for bogus CQ %08x\n", cqn);
210 cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
213 void mthca_cq_clean(struct mthca_dev *dev, u32 cqn, u32 qpn)
216 struct mthca_cqe *cqe;
220 spin_lock_irq(&dev->cq_table.lock);
221 cq = mthca_array_get(&dev->cq_table.cq, cqn & (dev->limits.num_cqs - 1));
223 atomic_inc(&cq->refcount);
224 spin_unlock_irq(&dev->cq_table.lock);
229 spin_lock_irq(&cq->lock);
232 * First we need to find the current producer index, so we
233 * know where to start cleaning from. It doesn't matter if HW
234 * adds new entries after this loop -- the QP we're worried
235 * about is already in RESET, so the new entries won't come
236 * from our QP and therefore don't need to be checked.
238 for (prod_index = cq->cons_index;
239 cqe_sw(cq, prod_index & cq->ibcq.cqe);
241 if (prod_index == cq->cons_index + cq->ibcq.cqe)
245 mthca_dbg(dev, "Cleaning QPN %06x from CQN %06x; ci %d, pi %d\n",
246 qpn, cqn, cq->cons_index, prod_index);
249 * Now sweep backwards through the CQ, removing CQ entries
250 * that match our QP by copying older entries on top of them.
252 while (prod_index > cq->cons_index) {
253 cqe = get_cqe(cq, (prod_index - 1) & cq->ibcq.cqe);
254 if (cqe->my_qpn == cpu_to_be32(qpn))
257 memcpy(get_cqe(cq, (prod_index - 1 + nfreed) &
260 MTHCA_CQ_ENTRY_SIZE);
266 cq->cons_index += nfreed;
267 update_cons_index(dev, cq, nfreed);
270 spin_unlock_irq(&cq->lock);
271 if (atomic_dec_and_test(&cq->refcount))
275 static int handle_error_cqe(struct mthca_dev *dev, struct mthca_cq *cq,
276 struct mthca_qp *qp, int wqe_index, int is_send,
277 struct mthca_err_cqe *cqe,
278 struct ib_wc *entry, int *free_cqe)
284 if (1 && cqe->syndrome != SYNDROME_WR_FLUSH_ERR) {
287 mthca_dbg(dev, "%x/%d: error CQE -> QPN %06x, WQE @ %08x\n",
288 cq->cqn, cq->cons_index, be32_to_cpu(cqe->my_qpn),
289 be32_to_cpu(cqe->wqe));
291 for (j = 0; j < 8; ++j)
292 printk(KERN_DEBUG " [%2x] %08x\n",
293 j * 4, be32_to_cpu(((u32 *) cqe)[j]));
297 * For completions in error, only work request ID, status (and
298 * freed resource count for RD) have to be set.
300 switch (cqe->syndrome) {
301 case SYNDROME_LOCAL_LENGTH_ERR:
302 entry->status = IB_WC_LOC_LEN_ERR;
304 case SYNDROME_LOCAL_QP_OP_ERR:
305 entry->status = IB_WC_LOC_QP_OP_ERR;
307 case SYNDROME_LOCAL_EEC_OP_ERR:
308 entry->status = IB_WC_LOC_EEC_OP_ERR;
310 case SYNDROME_LOCAL_PROT_ERR:
311 entry->status = IB_WC_LOC_PROT_ERR;
313 case SYNDROME_WR_FLUSH_ERR:
314 entry->status = IB_WC_WR_FLUSH_ERR;
316 case SYNDROME_MW_BIND_ERR:
317 entry->status = IB_WC_MW_BIND_ERR;
319 case SYNDROME_BAD_RESP_ERR:
320 entry->status = IB_WC_BAD_RESP_ERR;
322 case SYNDROME_LOCAL_ACCESS_ERR:
323 entry->status = IB_WC_LOC_ACCESS_ERR;
325 case SYNDROME_REMOTE_INVAL_REQ_ERR:
326 entry->status = IB_WC_REM_INV_REQ_ERR;
328 case SYNDROME_REMOTE_ACCESS_ERR:
329 entry->status = IB_WC_REM_ACCESS_ERR;
331 case SYNDROME_REMOTE_OP_ERR:
332 entry->status = IB_WC_REM_OP_ERR;
334 case SYNDROME_RETRY_EXC_ERR:
335 entry->status = IB_WC_RETRY_EXC_ERR;
337 case SYNDROME_RNR_RETRY_EXC_ERR:
338 entry->status = IB_WC_RNR_RETRY_EXC_ERR;
340 case SYNDROME_LOCAL_RDD_VIOL_ERR:
341 entry->status = IB_WC_LOC_RDD_VIOL_ERR;
343 case SYNDROME_REMOTE_INVAL_RD_REQ_ERR:
344 entry->status = IB_WC_REM_INV_RD_REQ_ERR;
346 case SYNDROME_REMOTE_ABORTED_ERR:
347 entry->status = IB_WC_REM_ABORT_ERR;
349 case SYNDROME_INVAL_EECN_ERR:
350 entry->status = IB_WC_INV_EECN_ERR;
352 case SYNDROME_INVAL_EEC_STATE_ERR:
353 entry->status = IB_WC_INV_EEC_STATE_ERR;
356 entry->status = IB_WC_GENERAL_ERR;
360 err = mthca_free_err_wqe(dev, qp, is_send, wqe_index, &dbd, &new_wqe);
365 * If we're at the end of the WQE chain, or we've used up our
366 * doorbell count, free the CQE. Otherwise just update it for
367 * the next poll operation.
369 if (!(new_wqe & cpu_to_be32(0x3f)) || (!cqe->db_cnt && dbd))
372 cqe->db_cnt = cpu_to_be16(be16_to_cpu(cqe->db_cnt) - dbd);
374 cqe->syndrome = SYNDROME_WR_FLUSH_ERR;
381 static void dump_cqe(struct mthca_cqe *cqe)
385 for (j = 0; j < 8; ++j)
386 printk(KERN_DEBUG " [%2x] %08x\n",
387 j * 4, be32_to_cpu(((u32 *) cqe)[j]));
390 static inline int mthca_poll_one(struct mthca_dev *dev,
392 struct mthca_qp **cur_qp,
397 struct mthca_cqe *cqe;
404 cqe = next_cqe_sw(cq);
409 * Make sure we read CQ entry contents after we've checked the
415 mthca_dbg(dev, "%x/%d: CQE -> QPN %06x, WQE @ %08x\n",
416 cq->cqn, cq->cons_index, be32_to_cpu(cqe->my_qpn),
417 be32_to_cpu(cqe->wqe));
422 is_error = (cqe->opcode & MTHCA_ERROR_CQE_OPCODE_MASK) ==
423 MTHCA_ERROR_CQE_OPCODE_MASK;
424 is_send = is_error ? cqe->opcode & 0x01 : cqe->is_send & 0x80;
426 if (!*cur_qp || be32_to_cpu(cqe->my_qpn) != (*cur_qp)->qpn) {
428 * We do not have to take the QP table lock here,
429 * because CQs will be locked while QPs are removed
432 *cur_qp = mthca_array_get(&dev->qp_table.qp,
433 be32_to_cpu(cqe->my_qpn) &
434 (dev->limits.num_qps - 1));
436 mthca_warn(dev, "CQ entry for unknown QP %06x\n",
437 be32_to_cpu(cqe->my_qpn) & 0xffffff);
443 entry->qp_num = (*cur_qp)->qpn;
447 wqe_index = ((be32_to_cpu(cqe->wqe) - (*cur_qp)->send_wqe_offset)
449 entry->wr_id = (*cur_qp)->wrid[wqe_index +
453 wqe_index = be32_to_cpu(cqe->wqe) >> wq->wqe_shift;
454 entry->wr_id = (*cur_qp)->wrid[wqe_index];
457 if (wq->last_comp < wqe_index)
458 wq->tail += wqe_index - wq->last_comp;
460 wq->tail += wqe_index + wq->max - wq->last_comp;
462 wq->last_comp = wqe_index;
465 mthca_dbg(dev, "%s completion for QP %06x, index %d (nr %d)\n",
466 is_send ? "Send" : "Receive",
467 (*cur_qp)->qpn, wqe_index, wq->max);
470 err = handle_error_cqe(dev, cq, *cur_qp, wqe_index, is_send,
471 (struct mthca_err_cqe *) cqe,
478 switch (cqe->opcode) {
479 case MTHCA_OPCODE_RDMA_WRITE:
480 entry->opcode = IB_WC_RDMA_WRITE;
482 case MTHCA_OPCODE_RDMA_WRITE_IMM:
483 entry->opcode = IB_WC_RDMA_WRITE;
484 entry->wc_flags |= IB_WC_WITH_IMM;
486 case MTHCA_OPCODE_SEND:
487 entry->opcode = IB_WC_SEND;
489 case MTHCA_OPCODE_SEND_IMM:
490 entry->opcode = IB_WC_SEND;
491 entry->wc_flags |= IB_WC_WITH_IMM;
493 case MTHCA_OPCODE_RDMA_READ:
494 entry->opcode = IB_WC_RDMA_READ;
495 entry->byte_len = be32_to_cpu(cqe->byte_cnt);
497 case MTHCA_OPCODE_ATOMIC_CS:
498 entry->opcode = IB_WC_COMP_SWAP;
499 entry->byte_len = be32_to_cpu(cqe->byte_cnt);
501 case MTHCA_OPCODE_ATOMIC_FA:
502 entry->opcode = IB_WC_FETCH_ADD;
503 entry->byte_len = be32_to_cpu(cqe->byte_cnt);
505 case MTHCA_OPCODE_BIND_MW:
506 entry->opcode = IB_WC_BIND_MW;
509 entry->opcode = MTHCA_OPCODE_INVALID;
513 entry->byte_len = be32_to_cpu(cqe->byte_cnt);
514 switch (cqe->opcode & 0x1f) {
515 case IB_OPCODE_SEND_LAST_WITH_IMMEDIATE:
516 case IB_OPCODE_SEND_ONLY_WITH_IMMEDIATE:
517 entry->wc_flags = IB_WC_WITH_IMM;
518 entry->imm_data = cqe->imm_etype_pkey_eec;
519 entry->opcode = IB_WC_RECV;
521 case IB_OPCODE_RDMA_WRITE_LAST_WITH_IMMEDIATE:
522 case IB_OPCODE_RDMA_WRITE_ONLY_WITH_IMMEDIATE:
523 entry->wc_flags = IB_WC_WITH_IMM;
524 entry->imm_data = cqe->imm_etype_pkey_eec;
525 entry->opcode = IB_WC_RECV_RDMA_WITH_IMM;
529 entry->opcode = IB_WC_RECV;
532 entry->slid = be16_to_cpu(cqe->rlid);
533 entry->sl = be16_to_cpu(cqe->sl_g_mlpath) >> 12;
534 entry->src_qp = be32_to_cpu(cqe->rqpn) & 0xffffff;
535 entry->dlid_path_bits = be16_to_cpu(cqe->sl_g_mlpath) & 0x7f;
536 entry->pkey_index = be32_to_cpu(cqe->imm_etype_pkey_eec) >> 16;
537 entry->wc_flags |= be16_to_cpu(cqe->sl_g_mlpath) & 0x80 ?
541 entry->status = IB_WC_SUCCESS;
544 if (likely(free_cqe)) {
553 int mthca_poll_cq(struct ib_cq *ibcq, int num_entries,
556 struct mthca_dev *dev = to_mdev(ibcq->device);
557 struct mthca_cq *cq = to_mcq(ibcq);
558 struct mthca_qp *qp = NULL;
564 spin_lock_irqsave(&cq->lock, flags);
566 for (npolled = 0; npolled < num_entries; ++npolled) {
567 err = mthca_poll_one(dev, cq, &qp,
568 &freed, entry + npolled);
575 update_cons_index(dev, cq, freed);
578 spin_unlock_irqrestore(&cq->lock, flags);
580 return err == 0 || err == -EAGAIN ? npolled : err;
583 int mthca_tavor_arm_cq(struct ib_cq *cq, enum ib_cq_notify notify)
587 doorbell[0] = cpu_to_be32((notify == IB_CQ_SOLICITED ?
588 MTHCA_TAVOR_CQ_DB_REQ_NOT_SOL :
589 MTHCA_TAVOR_CQ_DB_REQ_NOT) |
591 doorbell[1] = 0xffffffff;
593 mthca_write64(doorbell,
594 to_mdev(cq->device)->kar + MTHCA_CQ_DOORBELL,
595 MTHCA_GET_DOORBELL_LOCK(&to_mdev(cq->device)->doorbell_lock));
600 int mthca_arbel_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify notify)
602 struct mthca_cq *cq = to_mcq(ibcq);
608 ci = cpu_to_be32(cq->cons_index);
611 doorbell[1] = cpu_to_be32((cq->cqn << 8) | (2 << 5) | (sn << 3) |
612 (notify == IB_CQ_SOLICITED ? 1 : 2));
614 mthca_write_db_rec(doorbell, cq->arm_db);
617 * Make sure that the doorbell record in host memory is
618 * written before ringing the doorbell via PCI MMIO.
622 doorbell[0] = cpu_to_be32((sn << 28) |
623 (notify == IB_CQ_SOLICITED ?
624 MTHCA_ARBEL_CQ_DB_REQ_NOT_SOL :
625 MTHCA_ARBEL_CQ_DB_REQ_NOT) |
629 mthca_write64(doorbell,
630 to_mdev(ibcq->device)->kar + MTHCA_CQ_DOORBELL,
631 MTHCA_GET_DOORBELL_LOCK(&to_mdev(ibcq->device)->doorbell_lock));
636 static void mthca_free_cq_buf(struct mthca_dev *dev, struct mthca_cq *cq)
642 pci_free_consistent(dev->pdev,
643 (cq->ibcq.cqe + 1) * MTHCA_CQ_ENTRY_SIZE,
644 cq->queue.direct.buf,
645 pci_unmap_addr(&cq->queue.direct,
648 size = (cq->ibcq.cqe + 1) * MTHCA_CQ_ENTRY_SIZE;
649 for (i = 0; i < (size + PAGE_SIZE - 1) / PAGE_SIZE; ++i)
650 if (cq->queue.page_list[i].buf)
651 pci_free_consistent(dev->pdev, PAGE_SIZE,
652 cq->queue.page_list[i].buf,
653 pci_unmap_addr(&cq->queue.page_list[i],
656 kfree(cq->queue.page_list);
660 static int mthca_alloc_cq_buf(struct mthca_dev *dev, int size,
665 u64 *dma_list = NULL;
669 if (size <= MTHCA_MAX_DIRECT_CQ_SIZE) {
672 shift = get_order(size) + PAGE_SHIFT;
674 cq->queue.direct.buf = pci_alloc_consistent(dev->pdev,
676 if (!cq->queue.direct.buf)
679 pci_unmap_addr_set(&cq->queue.direct, mapping, t);
681 memset(cq->queue.direct.buf, 0, size);
683 while (t & ((1 << shift) - 1)) {
688 dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
692 for (i = 0; i < npages; ++i)
693 dma_list[i] = t + i * (1 << shift);
696 npages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
699 dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
703 cq->queue.page_list = kmalloc(npages * sizeof *cq->queue.page_list,
705 if (!cq->queue.page_list)
708 for (i = 0; i < npages; ++i)
709 cq->queue.page_list[i].buf = NULL;
711 for (i = 0; i < npages; ++i) {
712 cq->queue.page_list[i].buf =
713 pci_alloc_consistent(dev->pdev, PAGE_SIZE, &t);
714 if (!cq->queue.page_list[i].buf)
718 pci_unmap_addr_set(&cq->queue.page_list[i], mapping, t);
720 memset(cq->queue.page_list[i].buf, 0, PAGE_SIZE);
724 err = mthca_mr_alloc_phys(dev, dev->driver_pd.pd_num,
725 dma_list, shift, npages,
727 MTHCA_MPT_FLAG_LOCAL_WRITE |
728 MTHCA_MPT_FLAG_LOCAL_READ,
738 mthca_free_cq_buf(dev, cq);
746 int mthca_init_cq(struct mthca_dev *dev, int nent,
749 int size = nent * MTHCA_CQ_ENTRY_SIZE;
750 void *mailbox = NULL;
751 struct mthca_cq_context *cq_context;
758 cq->ibcq.cqe = nent - 1;
760 cq->cqn = mthca_alloc(&dev->cq_table.alloc);
764 if (mthca_is_memfree(dev)) {
767 err = mthca_table_get(dev, dev->cq_table.table, cq->cqn);
773 cq->set_ci_db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_CQ_SET_CI,
774 cq->cqn, &cq->set_ci_db);
775 if (cq->set_ci_db_index < 0)
778 cq->arm_db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_CQ_ARM,
779 cq->cqn, &cq->arm_db);
780 if (cq->arm_db_index < 0)
784 mailbox = kmalloc(sizeof (struct mthca_cq_context) + MTHCA_CMD_MAILBOX_EXTRA,
787 goto err_out_mailbox;
789 cq_context = MAILBOX_ALIGN(mailbox);
791 err = mthca_alloc_cq_buf(dev, size, cq);
793 goto err_out_mailbox;
795 for (i = 0; i < nent; ++i)
796 set_cqe_hw(get_cqe(cq, i));
798 spin_lock_init(&cq->lock);
799 atomic_set(&cq->refcount, 1);
800 init_waitqueue_head(&cq->wait);
802 memset(cq_context, 0, sizeof *cq_context);
803 cq_context->flags = cpu_to_be32(MTHCA_CQ_STATUS_OK |
804 MTHCA_CQ_STATE_DISARMED |
806 cq_context->start = cpu_to_be64(0);
807 cq_context->logsize_usrpage = cpu_to_be32((ffs(nent) - 1) << 24 |
808 dev->driver_uar.index);
809 cq_context->error_eqn = cpu_to_be32(dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn);
810 cq_context->comp_eqn = cpu_to_be32(dev->eq_table.eq[MTHCA_EQ_COMP].eqn);
811 cq_context->pd = cpu_to_be32(dev->driver_pd.pd_num);
812 cq_context->lkey = cpu_to_be32(cq->mr.ibmr.lkey);
813 cq_context->cqn = cpu_to_be32(cq->cqn);
815 if (mthca_is_memfree(dev)) {
816 cq_context->ci_db = cpu_to_be32(cq->set_ci_db_index);
817 cq_context->state_db = cpu_to_be32(cq->arm_db_index);
820 err = mthca_SW2HW_CQ(dev, cq_context, cq->cqn, &status);
822 mthca_warn(dev, "SW2HW_CQ failed (%d)\n", err);
823 goto err_out_free_mr;
827 mthca_warn(dev, "SW2HW_CQ returned status 0x%02x\n",
830 goto err_out_free_mr;
833 spin_lock_irq(&dev->cq_table.lock);
834 if (mthca_array_set(&dev->cq_table.cq,
835 cq->cqn & (dev->limits.num_cqs - 1),
837 spin_unlock_irq(&dev->cq_table.lock);
838 goto err_out_free_mr;
840 spin_unlock_irq(&dev->cq_table.lock);
849 mthca_free_mr(dev, &cq->mr);
850 mthca_free_cq_buf(dev, cq);
855 if (mthca_is_memfree(dev))
856 mthca_free_db(dev, MTHCA_DB_TYPE_CQ_ARM, cq->arm_db_index);
859 if (mthca_is_memfree(dev))
860 mthca_free_db(dev, MTHCA_DB_TYPE_CQ_SET_CI, cq->set_ci_db_index);
863 mthca_table_put(dev, dev->cq_table.table, cq->cqn);
866 mthca_free(&dev->cq_table.alloc, cq->cqn);
871 void mthca_free_cq(struct mthca_dev *dev,
880 mailbox = kmalloc(sizeof (struct mthca_cq_context) + MTHCA_CMD_MAILBOX_EXTRA,
883 mthca_warn(dev, "No memory for mailbox to free CQ.\n");
887 err = mthca_HW2SW_CQ(dev, MAILBOX_ALIGN(mailbox), cq->cqn, &status);
889 mthca_warn(dev, "HW2SW_CQ failed (%d)\n", err);
891 mthca_warn(dev, "HW2SW_CQ returned status 0x%02x\n",
895 u32 *ctx = MAILBOX_ALIGN(mailbox);
898 printk(KERN_ERR "context for CQN %x (cons index %x, next sw %d)\n",
899 cq->cqn, cq->cons_index, !!next_cqe_sw(cq));
900 for (j = 0; j < 16; ++j)
901 printk(KERN_ERR "[%2x] %08x\n", j * 4, be32_to_cpu(ctx[j]));
904 spin_lock_irq(&dev->cq_table.lock);
905 mthca_array_clear(&dev->cq_table.cq,
906 cq->cqn & (dev->limits.num_cqs - 1));
907 spin_unlock_irq(&dev->cq_table.lock);
909 if (dev->mthca_flags & MTHCA_FLAG_MSI_X)
910 synchronize_irq(dev->eq_table.eq[MTHCA_EQ_COMP].msi_x_vector);
912 synchronize_irq(dev->pdev->irq);
914 atomic_dec(&cq->refcount);
915 wait_event(cq->wait, !atomic_read(&cq->refcount));
917 mthca_free_mr(dev, &cq->mr);
918 mthca_free_cq_buf(dev, cq);
920 if (mthca_is_memfree(dev)) {
921 mthca_free_db(dev, MTHCA_DB_TYPE_CQ_ARM, cq->arm_db_index);
922 mthca_free_db(dev, MTHCA_DB_TYPE_CQ_SET_CI, cq->set_ci_db_index);
923 mthca_table_put(dev, dev->cq_table.table, cq->cqn);
926 mthca_free(&dev->cq_table.alloc, cq->cqn);
930 int __devinit mthca_init_cq_table(struct mthca_dev *dev)
934 spin_lock_init(&dev->cq_table.lock);
936 err = mthca_alloc_init(&dev->cq_table.alloc,
939 dev->limits.reserved_cqs);
943 err = mthca_array_init(&dev->cq_table.cq,
944 dev->limits.num_cqs);
946 mthca_alloc_cleanup(&dev->cq_table.alloc);
951 void __devexit mthca_cleanup_cq_table(struct mthca_dev *dev)
953 mthca_array_cleanup(&dev->cq_table.cq, dev->limits.num_cqs);
954 mthca_alloc_cleanup(&dev->cq_table.alloc);