2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2005 Cisco Systems, Inc. All rights reserved.
5 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
6 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
8 * This software is available to you under a choice of one of two
9 * licenses. You may choose to be licensed under the terms of the GNU
10 * General Public License (GPL) Version 2, available from the file
11 * COPYING in the main directory of this source tree, or the
12 * OpenIB.org BSD license below:
14 * Redistribution and use in source and binary forms, with or
15 * without modification, are permitted provided that the following
18 * - Redistributions of source code must retain the above
19 * copyright notice, this list of conditions and the following
22 * - Redistributions in binary form must reproduce the above
23 * copyright notice, this list of conditions and the following
24 * disclaimer in the documentation and/or other materials
25 * provided with the distribution.
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
41 #include "mthca_dev.h"
42 #if defined(EVENT_TRACING)
46 #include "mthca_cq.tmh"
48 #include "mthca_cmd.h"
49 #include "mthca_memfree.h"
53 #pragma alloc_text (PAGE, mthca_init_cq_table)
54 #pragma alloc_text (PAGE, mthca_cleanup_cq_table)
58 MTHCA_MAX_DIRECT_CQ_SIZE = 4 * PAGE_SIZE
62 * Must be packed because start is 64 bits but only aligned to 32 bits.
65 struct mthca_cq_context {
68 __be32 logsize_usrpage;
69 __be32 error_eqn; /* Tavor only */
73 __be32 last_notified_index;
74 __be32 solicit_producer_index;
75 __be32 consumer_index;
76 __be32 producer_index;
78 __be32 ci_db; /* Arbel only */
79 __be32 state_db; /* Arbel only */
84 #define MTHCA_CQ_STATUS_OK ( 0 << 28)
85 #define MTHCA_CQ_STATUS_OVERFLOW ( 9 << 28)
86 #define MTHCA_CQ_STATUS_WRITE_FAIL (10 << 28)
87 #define MTHCA_CQ_FLAG_TR ( 1 << 18)
88 #define MTHCA_CQ_FLAG_OI ( 1 << 17)
89 #define MTHCA_CQ_STATE_DISARMED ( 0 << 8)
90 #define MTHCA_CQ_STATE_ARMED ( 1 << 8)
91 #define MTHCA_CQ_STATE_ARMED_SOL ( 4 << 8)
92 #define MTHCA_EQ_STATE_FIRED (10 << 8)
95 MTHCA_ERROR_CQE_OPCODE_MASK = 0xfe
99 SYNDROME_LOCAL_LENGTH_ERR = 0x01,
100 SYNDROME_LOCAL_QP_OP_ERR = 0x02,
101 SYNDROME_LOCAL_EEC_OP_ERR = 0x03,
102 SYNDROME_LOCAL_PROT_ERR = 0x04,
103 SYNDROME_WR_FLUSH_ERR = 0x05,
104 SYNDROME_MW_BIND_ERR = 0x06,
105 SYNDROME_BAD_RESP_ERR = 0x10,
106 SYNDROME_LOCAL_ACCESS_ERR = 0x11,
107 SYNDROME_REMOTE_INVAL_REQ_ERR = 0x12,
108 SYNDROME_REMOTE_ACCESS_ERR = 0x13,
109 SYNDROME_REMOTE_OP_ERR = 0x14,
110 SYNDROME_RETRY_EXC_ERR = 0x15,
111 SYNDROME_RNR_RETRY_EXC_ERR = 0x16,
112 SYNDROME_LOCAL_RDD_VIOL_ERR = 0x20,
113 SYNDROME_REMOTE_INVAL_RD_REQ_ERR = 0x21,
114 SYNDROME_REMOTE_ABORTED_ERR = 0x22,
115 SYNDROME_INVAL_EECN_ERR = 0x23,
116 SYNDROME_INVAL_EEC_STATE_ERR = 0x24
125 __be32 imm_etype_pkey_eec;
134 struct mthca_err_cqe {
147 #define MTHCA_CQ_ENTRY_OWNER_SW (0 << 7)
148 #define MTHCA_CQ_ENTRY_OWNER_HW (1 << 7)
150 #define MTHCA_TAVOR_CQ_DB_INC_CI (1 << 24)
151 #define MTHCA_TAVOR_CQ_DB_REQ_NOT (2 << 24)
152 #define MTHCA_TAVOR_CQ_DB_REQ_NOT_SOL (3 << 24)
153 #define MTHCA_TAVOR_CQ_DB_SET_CI (4 << 24)
154 #define MTHCA_TAVOR_CQ_DB_REQ_NOT_MULT (5 << 24)
156 #define MTHCA_ARBEL_CQ_DB_REQ_NOT_SOL (1 << 24)
157 #define MTHCA_ARBEL_CQ_DB_REQ_NOT (2 << 24)
158 #define MTHCA_ARBEL_CQ_DB_REQ_NOT_MULT (3 << 24)
160 static inline struct mthca_cqe *get_cqe(struct mthca_cq *cq, int entry)
163 return (struct mthca_cqe *)((u8*)cq->queue.direct.page + (entry * MTHCA_CQ_ENTRY_SIZE));
165 return (struct mthca_cqe *)((u8*)cq->queue.page_list[entry * MTHCA_CQ_ENTRY_SIZE / PAGE_SIZE].page
166 + (entry * MTHCA_CQ_ENTRY_SIZE) % PAGE_SIZE);
169 static inline struct mthca_cqe *cqe_sw(struct mthca_cq *cq, int i)
171 struct mthca_cqe *cqe = get_cqe(cq, i);
172 return MTHCA_CQ_ENTRY_OWNER_HW & cqe->owner ? NULL : cqe;
175 static inline struct mthca_cqe *next_cqe_sw(struct mthca_cq *cq)
177 return cqe_sw(cq, cq->cons_index & cq->ibcq.cqe);
180 static inline void set_cqe_hw(struct mthca_cqe *cqe)
182 cqe->owner = MTHCA_CQ_ENTRY_OWNER_HW;
185 static void dump_cqe(u32 print_lvl, struct mthca_dev *dev, void *cqe_ptr)
187 __be32 *cqe = cqe_ptr;
188 UNREFERENCED_PARAMETER(dev);
190 (void) cqe; /* avoid warning if mthca_dbg compiled away... */
191 HCA_PRINT(print_lvl,HCA_DBG_CQ,("CQE contents \n"));
192 HCA_PRINT(print_lvl,HCA_DBG_CQ,("\t[%2x] %08x %08x %08x %08x\n",0,
193 cl_ntoh32(cqe[0]), cl_ntoh32(cqe[1]), cl_ntoh32(cqe[2]), cl_ntoh32(cqe[3])));
194 HCA_PRINT(print_lvl,HCA_DBG_CQ,("\t[%2x] %08x %08x %08x %08x \n",16,
195 cl_ntoh32(cqe[4]), cl_ntoh32(cqe[5]), cl_ntoh32(cqe[6]), cl_ntoh32(cqe[7])));
199 * incr is ignored in native Arbel (mem-free) mode, so cq->cons_index
200 * should be correct before calling update_cons_index().
202 static inline void update_cons_index(struct mthca_dev *dev, struct mthca_cq *cq,
207 if (mthca_is_memfree(dev)) {
208 *cq->set_ci_db = cl_hton32(cq->cons_index);
211 doorbell[0] = cl_hton32(MTHCA_TAVOR_CQ_DB_INC_CI | cq->cqn);
212 doorbell[1] = cl_hton32(incr - 1);
214 mthca_write64(doorbell,
215 dev->kar + MTHCA_CQ_DOORBELL,
216 MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
220 void mthca_cq_completion(struct mthca_dev *dev, u32 cqn)
224 cq = mthca_array_get(&dev->cq_table.cq, cqn & (dev->limits.num_cqs - 1));
227 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW ,("Completion event for bogus CQ %08x\n", cqn));
231 if (mthca_is_memfree(dev)) {
232 if (cq->ibcq.ucontext)
238 cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
241 void mthca_cq_event(struct mthca_dev *dev, u32 cqn,
242 enum ib_event_type event_type)
245 struct ib_event event;
248 spin_lock(&dev->cq_table.lock, &lh);
250 cq = mthca_array_get(&dev->cq_table.cq, cqn & (dev->limits.num_cqs - 1));
253 atomic_inc(&cq->refcount);
257 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW ,("Async event for bogus CQ %08x\n", cqn));
261 event.device = &dev->ib_dev;
262 event.event = event_type;
263 event.element.cq = &cq->ibcq;
264 if (cq->ibcq.event_handler)
265 cq->ibcq.event_handler(&event, cq->ibcq.cq_context);
267 if (atomic_dec_and_test(&cq->refcount))
271 void mthca_cq_clean(struct mthca_dev *dev, u32 cqn, u32 qpn,
272 struct mthca_srq *srq)
275 struct mthca_cqe *cqe;
281 spin_lock_irq(&dev->cq_table.lock, &lht);
282 cq = mthca_array_get(&dev->cq_table.cq, cqn & (dev->limits.num_cqs - 1));
284 atomic_inc(&cq->refcount);
285 spin_unlock_irq(&lht);
290 spin_lock_irq(&cq->lock, &lh);
293 * First we need to find the current producer index, so we
294 * know where to start cleaning from. It doesn't matter if HW
295 * adds new entries after this loop -- the QP we're worried
296 * about is already in RESET, so the new entries won't come
297 * from our QP and therefore don't need to be checked.
299 for (prod_index = cq->cons_index;
300 cqe_sw(cq, prod_index & cq->ibcq.cqe);
302 if (prod_index == cq->cons_index + cq->ibcq.cqe)
306 HCA_PRINT(TRACE_LEVEL_VERBOSE,HCA_DBG_LOW,("Cleaning QPN %06x from CQN %06x; ci %d, pi %d\n",
307 qpn, cqn, cq->cons_index, prod_index));
310 * Now sweep backwards through the CQ, removing CQ entries
311 * that match our QP by copying older entries on top of them.
313 while ((int) --prod_index - (int) cq->cons_index >= 0) {
314 cqe = get_cqe(cq, prod_index & cq->ibcq.cqe);
315 if (cqe->my_qpn == cl_hton32(qpn)) {
317 mthca_free_srq_wqe(srq, cl_ntoh32(cqe->wqe));
322 memcpy(get_cqe(cq, (prod_index + nfreed) & cq->ibcq.cqe),
323 cqe, MTHCA_CQ_ENTRY_SIZE);
329 cq->cons_index += nfreed;
330 update_cons_index(dev, cq, nfreed);
333 spin_unlock_irq(&lh);
334 if (atomic_dec_and_test(&cq->refcount))
338 static void handle_error_cqe(struct mthca_dev *dev, struct mthca_cq *cq,
339 struct mthca_qp *qp, int wqe_index, int is_send,
340 struct mthca_err_cqe *cqe,
341 struct _ib_wc *entry, int *free_cqe)
346 UNREFERENCED_PARAMETER(cq);
348 if (cqe->syndrome != SYNDROME_WR_FLUSH_ERR) {
349 HCA_PRINT(TRACE_LEVEL_INFORMATION ,HCA_DBG_CQ ,("Completion with errro "
350 "(QPN %06x, WQE @ %08x, CQN %06x, index %d)\n",
351 cl_ntoh32(cqe->my_qpn), cl_ntoh32(cqe->wqe),
352 cq->cqn, cq->cons_index));
353 dump_cqe(TRACE_LEVEL_INFORMATION, dev, cqe);
358 * For completions in error, only work request ID, status, vendor error
359 * (and freed resource count for RD) have to be set.
361 switch (cqe->syndrome) {
362 case SYNDROME_LOCAL_LENGTH_ERR:
363 entry->status = IB_WCS_LOCAL_LEN_ERR;
365 case SYNDROME_LOCAL_QP_OP_ERR:
366 entry->status = IB_WCS_LOCAL_OP_ERR;
368 case SYNDROME_LOCAL_PROT_ERR:
369 entry->status = IB_WCS_LOCAL_PROTECTION_ERR;
371 case SYNDROME_WR_FLUSH_ERR:
372 entry->status = IB_WCS_WR_FLUSHED_ERR;
374 case SYNDROME_MW_BIND_ERR:
375 entry->status = IB_WCS_MEM_WINDOW_BIND_ERR;
377 case SYNDROME_BAD_RESP_ERR:
378 entry->status = IB_WCS_BAD_RESP_ERR;
380 case SYNDROME_LOCAL_ACCESS_ERR:
381 entry->status = IB_WCS_LOCAL_ACCESS_ERR;
383 case SYNDROME_REMOTE_INVAL_REQ_ERR:
384 entry->status = IB_WCS_REM_INVALID_REQ_ERR;
386 case SYNDROME_REMOTE_ACCESS_ERR:
387 entry->status = IB_WCS_REM_ACCESS_ERR;
389 case SYNDROME_REMOTE_OP_ERR:
390 entry->status = IB_WCS_REM_OP_ERR;
392 case SYNDROME_RETRY_EXC_ERR:
393 entry->status = IB_WCS_TIMEOUT_RETRY_ERR;
395 case SYNDROME_RNR_RETRY_EXC_ERR:
396 entry->status = IB_WCS_RNR_RETRY_ERR;
398 case SYNDROME_REMOTE_INVAL_RD_REQ_ERR:
399 entry->status = IB_WCS_REM_INVALID_REQ_ERR;
401 case SYNDROME_REMOTE_ABORTED_ERR:
402 case SYNDROME_LOCAL_EEC_OP_ERR:
403 case SYNDROME_LOCAL_RDD_VIOL_ERR:
404 case SYNDROME_INVAL_EECN_ERR:
405 case SYNDROME_INVAL_EEC_STATE_ERR:
407 entry->status = IB_WCS_GENERAL_ERR;
411 entry->vendor_specific = cqe->vendor_err;
414 * Mem-free HCAs always generate one CQE per WQE, even in the
415 * error case, so we don't have to check the doorbell count, etc.
417 if (mthca_is_memfree(dev))
420 mthca_free_err_wqe(dev, qp, is_send, wqe_index, &dbd, &new_wqe);
423 * If we're at the end of the WQE chain, or we've used up our
424 * doorbell count, free the CQE. Otherwise just update it for
425 * the next poll operation.
427 if (!(new_wqe & cl_hton32(0x3f)) || (!cqe->db_cnt && dbd))
430 cqe->db_cnt = cl_hton16(cl_ntoh16(cqe->db_cnt) - (u16)dbd);
432 cqe->syndrome = SYNDROME_WR_FLUSH_ERR;
437 static inline int mthca_poll_one(struct mthca_dev *dev,
439 struct mthca_qp **cur_qp,
441 struct _ib_wc *entry)
444 struct mthca_cqe *cqe;
451 HCA_ENTER(HCA_DBG_CQ);
452 cqe = next_cqe_sw(cq);
457 * Make sure we read CQ entry contents after we've checked the
463 HCA_PRINT(TRACE_LEVEL_VERBOSE,HCA_DBG_CQ,("CQ: 0x%06x/%d: CQE -> QPN 0x%06x, WQE @ 0x%08x\n",
464 cq->cqn, cq->cons_index, cl_ntoh32(cqe->my_qpn),
465 cl_ntoh32(cqe->wqe)));
466 dump_cqe(TRACE_LEVEL_VERBOSE, dev, cqe);
469 is_error = (cqe->opcode & MTHCA_ERROR_CQE_OPCODE_MASK) ==
470 MTHCA_ERROR_CQE_OPCODE_MASK;
471 is_send = is_error ? cqe->opcode & 0x01 : cqe->is_send & 0x80;
473 if (!*cur_qp || cl_ntoh32(cqe->my_qpn) != (*cur_qp)->qpn) {
475 * We do not have to take the QP table lock here,
476 * because CQs will be locked while QPs are removed
479 *cur_qp = mthca_array_get(&dev->qp_table.qp,
480 cl_ntoh32(cqe->my_qpn) &
481 (dev->limits.num_qps - 1));
483 HCA_PRINT(TRACE_LEVEL_WARNING,HCA_DBG_CQ, ("CQ entry for unknown QP %06x\n",
484 cl_ntoh32(cqe->my_qpn) & 0xffffff));
492 wqe_index = ((cl_ntoh32(cqe->wqe) - (*cur_qp)->send_wqe_offset)
494 entry->wr_id = (*cur_qp)->wrid[wqe_index +
496 } else if ((*cur_qp)->ibqp.srq) {
497 struct mthca_srq *srq = to_msrq((*cur_qp)->ibqp.srq);
498 u32 wqe = cl_ntoh32(cqe->wqe);
500 wqe_index = wqe >> srq->wqe_shift;
501 entry->wr_id = srq->wrid[wqe_index];
502 mthca_free_srq_wqe(srq, wqe);
505 wqe_index = cl_ntoh32(cqe->wqe) >> wq->wqe_shift;
506 entry->wr_id = (*cur_qp)->wrid[wqe_index];
510 if (wq->last_comp < wqe_index)
511 wq->tail += wqe_index - wq->last_comp;
513 wq->tail += wqe_index + wq->max - wq->last_comp;
515 wq->last_comp = wqe_index;
519 entry->recv.ud.recv_opt = 0;
520 switch (cqe->opcode) {
521 case MTHCA_OPCODE_RDMA_WRITE:
522 entry->wc_type = IB_WC_RDMA_WRITE;
524 case MTHCA_OPCODE_RDMA_WRITE_IMM:
525 entry->wc_type = IB_WC_RDMA_WRITE;
526 entry->recv.ud.recv_opt |= IB_RECV_OPT_IMMEDIATE;
528 case MTHCA_OPCODE_SEND:
529 entry->wc_type = IB_WC_SEND;
531 case MTHCA_OPCODE_SEND_IMM:
532 entry->wc_type = IB_WC_SEND;
533 entry->recv.ud.recv_opt |= IB_RECV_OPT_IMMEDIATE;
535 case MTHCA_OPCODE_RDMA_READ:
536 entry->wc_type = IB_WC_RDMA_READ;
537 entry->length = cl_ntoh32(cqe->byte_cnt);
539 case MTHCA_OPCODE_ATOMIC_CS:
540 entry->wc_type = IB_WC_COMPARE_SWAP;
541 entry->length = cl_ntoh32(cqe->byte_cnt);
543 case MTHCA_OPCODE_ATOMIC_FA:
544 entry->wc_type = IB_WC_FETCH_ADD;
545 entry->length = cl_ntoh32(cqe->byte_cnt);
547 case MTHCA_OPCODE_BIND_MW:
548 entry->wc_type = IB_WC_MW_BIND;
551 entry->wc_type = IB_WC_SEND;
555 entry->length = cl_ntoh32(cqe->byte_cnt);
556 switch (cqe->opcode & 0x1f) {
557 case IB_OPCODE_SEND_LAST_WITH_IMMEDIATE:
558 case IB_OPCODE_SEND_ONLY_WITH_IMMEDIATE:
559 entry->recv.ud.recv_opt = IB_RECV_OPT_IMMEDIATE;
560 entry->recv.ud.immediate_data = cqe->imm_etype_pkey_eec;
561 entry->wc_type = IB_WC_RECV;
563 case IB_OPCODE_RDMA_WRITE_LAST_WITH_IMMEDIATE:
564 case IB_OPCODE_RDMA_WRITE_ONLY_WITH_IMMEDIATE:
565 entry->recv.ud.recv_opt = IB_RECV_OPT_IMMEDIATE;
566 entry->recv.ud.immediate_data = cqe->imm_etype_pkey_eec;
567 entry->wc_type = IB_WC_RECV_RDMA_WRITE;
570 entry->recv.ud.recv_opt = 0;
571 entry->wc_type = IB_WC_RECV;
574 entry->recv.ud.remote_lid = cqe->rlid;
575 entry->recv.ud.remote_qp = cqe->rqpn & 0xffffff00;
576 entry->recv.ud.pkey_index = (u16)(cl_ntoh32(cqe->imm_etype_pkey_eec) >> 16);
577 entry->recv.ud.remote_sl = (uint8_t)(cl_ntoh16(cqe->sl_g_mlpath) >> 12);
578 entry->recv.ud.path_bits = (uint8_t)(cl_ntoh16(cqe->sl_g_mlpath) & 0x7f);
579 entry->recv.ud.recv_opt |= cl_ntoh16(cqe->sl_g_mlpath) & 0x80 ?
580 IB_RECV_OPT_GRH_VALID : 0;
582 if (!is_send && cqe->rlid == 0){
583 HCA_PRINT(TRACE_LEVEL_INFORMATION,HCA_DBG_CQ,("found rlid == 0 \n "));
584 entry->recv.ud.recv_opt |= IB_RECV_OPT_FORWARD;
588 handle_error_cqe(dev, cq, *cur_qp, wqe_index, is_send,
589 (struct mthca_err_cqe *) cqe, entry, &free_cqe);
592 entry->status = IB_WCS_SUCCESS;
595 if (likely(free_cqe)) {
600 HCA_EXIT(HCA_DBG_CQ);
604 int mthca_poll_cq(struct ib_cq *ibcq, int num_entries,
605 struct _ib_wc *entry)
607 struct mthca_dev *dev = to_mdev(ibcq->device);
608 struct mthca_cq *cq = to_mcq(ibcq);
609 struct mthca_qp *qp = NULL;
615 spin_lock_irqsave(&cq->lock, &lh);
617 for (npolled = 0; npolled < num_entries; ++npolled) {
618 err = mthca_poll_one(dev, cq, &qp,
619 &freed, entry + npolled);
626 update_cons_index(dev, cq, freed);
629 spin_unlock_irqrestore(&lh);
631 return (err == 0 || err == -EAGAIN) ? npolled : err;
634 int mthca_poll_cq_list(
635 IN struct ib_cq *ibcq,
636 IN OUT ib_wc_t** const pp_free_wclist,
637 OUT ib_wc_t** const pp_done_wclist )
639 struct mthca_dev *dev = to_mdev(ibcq->device);
640 struct mthca_cq *cq = to_mcq(ibcq);
641 struct mthca_qp *qp = NULL;
644 ib_wc_t *wc_p, **next_pp;
647 HCA_ENTER(HCA_DBG_CQ);
649 spin_lock_irqsave(&cq->lock, &lh);
652 next_pp = pp_done_wclist;
653 wc_p = *pp_free_wclist;
656 err = mthca_poll_one(dev, cq, &qp, &freed, wc_p);
660 // prepare for the next loop
662 next_pp = &wc_p->p_next;
666 // prepare the results
667 *pp_free_wclist = wc_p; /* Set the head of the free list. */
668 *next_pp = NULL; /* Clear the tail of the done list. */
670 // update consumer index
673 update_cons_index(dev, cq, freed);
676 spin_unlock_irqrestore(&lh);
677 HCA_EXIT(HCA_DBG_CQ);
678 return (err == 0 || err == -EAGAIN)? 0 : err;
682 int mthca_tavor_arm_cq(struct ib_cq *cq, enum ib_cq_notify notify)
686 doorbell[0] = cl_hton32((notify == IB_CQ_SOLICITED ?
687 MTHCA_TAVOR_CQ_DB_REQ_NOT_SOL :
688 MTHCA_TAVOR_CQ_DB_REQ_NOT) |
690 doorbell[1] = (__be32) 0xffffffff;
692 mthca_write64(doorbell,
693 to_mdev(cq->device)->kar + MTHCA_CQ_DOORBELL,
694 MTHCA_GET_DOORBELL_LOCK(&to_mdev(cq->device)->doorbell_lock));
699 int mthca_arbel_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify notify)
701 struct mthca_cq *cq = to_mcq(ibcq);
707 ci = cl_hton32(cq->cons_index);
710 doorbell[1] = cl_hton32((cq->cqn << 8) | (2 << 5) | (sn << 3) |
711 (notify == IB_CQ_SOLICITED ? 1 : 2));
713 mthca_write_db_rec(doorbell, cq->arm_db);
716 * Make sure that the doorbell record in host memory is
717 * written before ringing the doorbell via PCI MMIO.
721 doorbell[0] = cl_hton32((sn << 28) |
722 (notify == IB_CQ_SOLICITED ?
723 MTHCA_ARBEL_CQ_DB_REQ_NOT_SOL :
724 MTHCA_ARBEL_CQ_DB_REQ_NOT) |
728 mthca_write64(doorbell,
729 to_mdev(ibcq->device)->kar + MTHCA_CQ_DOORBELL,
730 MTHCA_GET_DOORBELL_LOCK(&to_mdev(ibcq->device)->doorbell_lock));
735 static void mthca_free_cq_buf(struct mthca_dev *dev, struct mthca_cq *cq)
737 mthca_buf_free(dev, (cq->ibcq.cqe + 1) * MTHCA_CQ_ENTRY_SIZE,
738 &cq->queue, cq->is_direct, &cq->mr);
741 int mthca_init_cq(struct mthca_dev *dev, int nent,
742 struct mthca_ucontext *ctx, u32 pdn,
745 int size = NEXT_PAGE_ALIGN(nent * MTHCA_CQ_ENTRY_SIZE );
746 struct mthca_mailbox *mailbox;
747 struct mthca_cq_context *cq_context;
753 cq->ibcq.cqe = nent - 1;
754 cq->is_kernel = !ctx;
756 cq->cqn = mthca_alloc(&dev->cq_table.alloc);
760 if (mthca_is_memfree(dev)) {
761 err = mthca_table_get(dev, dev->cq_table.table, cq->cqn);
770 cq->set_ci_db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_CQ_SET_CI,
771 cq->cqn, &cq->set_ci_db);
772 if (cq->set_ci_db_index < 0)
775 cq->arm_db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_CQ_ARM,
776 cq->cqn, &cq->arm_db);
777 if (cq->arm_db_index < 0)
782 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
786 cq_context = mailbox->buf;
789 err = mthca_buf_alloc(dev, size, MTHCA_MAX_DIRECT_CQ_SIZE,
790 &cq->queue, &cq->is_direct,
791 &dev->driver_pd, 1, &cq->mr);
793 goto err_out_mailbox;
795 for (i = 0; i < nent; ++i)
796 set_cqe_hw(get_cqe(cq, i));
799 spin_lock_init(&cq->lock);
800 atomic_set(&cq->refcount, 1);
801 init_waitqueue_head(&cq->wait);
802 KeInitializeMutex(&cq->mutex, 0);
804 RtlZeroMemory(cq_context, sizeof *cq_context);
805 cq_context->flags = cl_hton32(MTHCA_CQ_STATUS_OK |
806 MTHCA_CQ_STATE_DISARMED |
808 cq_context->logsize_usrpage = cl_hton32((ffs(nent) - 1) << 24);
810 cq_context->logsize_usrpage |= cl_hton32(ctx->uar.index);
812 cq_context->logsize_usrpage |= cl_hton32(dev->driver_uar.index);
813 cq_context->error_eqn = cl_hton32(dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn);
814 cq_context->comp_eqn = cl_hton32(dev->eq_table.eq[MTHCA_EQ_COMP].eqn);
815 cq_context->pd = cl_hton32(pdn);
816 cq_context->lkey = cl_hton32(cq->mr.ibmr.lkey);
817 cq_context->cqn = cl_hton32(cq->cqn);
819 if (mthca_is_memfree(dev)) {
820 cq_context->ci_db = cl_hton32(cq->set_ci_db_index);
821 cq_context->state_db = cl_hton32(cq->arm_db_index);
824 err = mthca_SW2HW_CQ(dev, mailbox, cq->cqn, &status);
826 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW ,("SW2HW_CQ failed (%d)\n", err));
827 goto err_out_free_mr;
831 HCA_PRINT(TRACE_LEVEL_WARNING,HCA_DBG_LOW,("SW2HW_CQ returned status 0x%02x\n",
834 goto err_out_free_mr;
837 spin_lock_irq(&dev->cq_table.lock, &lh);
838 if (mthca_array_set(&dev->cq_table.cq,
839 cq->cqn & (dev->limits.num_cqs - 1),
841 spin_unlock_irq(&lh);
842 goto err_out_free_mr;
844 spin_unlock_irq(&lh);
848 mthca_free_mailbox(dev, mailbox);
854 mthca_free_cq_buf(dev, cq);
857 mthca_free_mailbox(dev, mailbox);
860 if (cq->is_kernel && mthca_is_memfree(dev))
861 mthca_free_db(dev, MTHCA_DB_TYPE_CQ_ARM, cq->arm_db_index);
864 if (cq->is_kernel && mthca_is_memfree(dev))
865 mthca_free_db(dev, MTHCA_DB_TYPE_CQ_SET_CI, cq->set_ci_db_index);
868 mthca_table_put(dev, dev->cq_table.table, cq->cqn);
871 mthca_free(&dev->cq_table.alloc, cq->cqn);
876 void mthca_free_cq(struct mthca_dev *dev,
879 struct mthca_mailbox *mailbox;
884 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
885 if (IS_ERR(mailbox)) {
886 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW ,("No memory for mailbox to free CQ.\n"));
890 err = mthca_HW2SW_CQ(dev, mailbox, cq->cqn, &status);
892 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW ,("HW2SW_CQ failed (%d)\n", err));
895 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW ,("HW2SW_CQ returned status 0x%02x\n", status));
899 __be32 *ctx = mailbox->buf;
902 HCA_PRINT(TRACE_LEVEL_VERBOSE ,HCA_DBG_LOW ,("context for CQN %x (cons index %x, next sw %d)\n",
903 cq->cqn, cq->cons_index,
904 cq->is_kernel ? !!next_cqe_sw(cq) : 0));
905 for (j = 0; j < 16; ++j)
906 HCA_PRINT(TRACE_LEVEL_VERBOSE ,HCA_DBG_LOW ,("[%2x] %08x\n", j * 4, cl_ntoh32(ctx[j])));
909 spin_lock_irq(&dev->cq_table.lock, &lh);
910 mthca_array_clear(&dev->cq_table.cq,
911 cq->cqn & (dev->limits.num_cqs - 1));
912 spin_unlock_irq(&lh);
914 /* wait for all RUNNING DPCs on that EQ to complete */
916 ASSERT(KeGetCurrentIrql() <= DISPATCH_LEVEL);
917 // wait for DPCs, using this EQ, to complete
918 spin_lock_sync( &dev->eq_table.eq[MTHCA_EQ_COMP].lock);
919 //TODO: do we need that ?
920 spin_lock_sync( &dev->eq_table.eq[MTHCA_EQ_ASYNC].lock );
923 atomic_dec(&cq->refcount);
924 wait_event(&cq->wait, !atomic_read(&cq->refcount));
927 mthca_free_cq_buf(dev, cq);
928 if (mthca_is_memfree(dev)) {
929 mthca_free_db(dev, MTHCA_DB_TYPE_CQ_ARM, cq->arm_db_index);
930 mthca_free_db(dev, MTHCA_DB_TYPE_CQ_SET_CI, cq->set_ci_db_index);
934 mthca_table_put(dev, dev->cq_table.table, cq->cqn);
935 mthca_free(&dev->cq_table.alloc, cq->cqn);
936 mthca_free_mailbox(dev, mailbox);
939 int mthca_init_cq_table(struct mthca_dev *dev)
943 spin_lock_init(&dev->cq_table.lock);
945 err = mthca_alloc_init(&dev->cq_table.alloc,
948 dev->limits.reserved_cqs);
952 err = mthca_array_init(&dev->cq_table.cq,
953 dev->limits.num_cqs);
955 mthca_alloc_cleanup(&dev->cq_table.alloc);
960 void mthca_cleanup_cq_table(struct mthca_dev *dev)
962 mthca_array_cleanup(&dev->cq_table.cq, dev->limits.num_cqs);
963 mthca_alloc_cleanup(&dev->cq_table.alloc);