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"
50 #include "mthca_wqe.h"
54 #pragma alloc_text (PAGE, mthca_init_cq_table)
55 #pragma alloc_text (PAGE, mthca_cleanup_cq_table)
59 MTHCA_MAX_DIRECT_CQ_SIZE = 4 * PAGE_SIZE
63 * Must be packed because start is 64 bits but only aligned to 32 bits.
66 struct mthca_cq_context {
69 __be32 logsize_usrpage;
70 __be32 error_eqn; /* Tavor only */
74 __be32 last_notified_index;
75 __be32 solicit_producer_index;
76 __be32 consumer_index;
77 __be32 producer_index;
79 __be32 ci_db; /* Arbel only */
80 __be32 state_db; /* Arbel only */
85 #define MTHCA_CQ_STATUS_OK ( 0 << 28)
86 #define MTHCA_CQ_STATUS_OVERFLOW ( 9 << 28)
87 #define MTHCA_CQ_STATUS_WRITE_FAIL (10 << 28)
88 #define MTHCA_CQ_FLAG_TR ( 1 << 18)
89 #define MTHCA_CQ_FLAG_OI ( 1 << 17)
90 #define MTHCA_CQ_STATE_DISARMED ( 0 << 8)
91 #define MTHCA_CQ_STATE_ARMED ( 1 << 8)
92 #define MTHCA_CQ_STATE_ARMED_SOL ( 4 << 8)
93 #define MTHCA_EQ_STATE_FIRED (10 << 8)
96 MTHCA_ERROR_CQE_OPCODE_MASK = 0xfe
100 SYNDROME_LOCAL_LENGTH_ERR = 0x01,
101 SYNDROME_LOCAL_QP_OP_ERR = 0x02,
102 SYNDROME_LOCAL_EEC_OP_ERR = 0x03,
103 SYNDROME_LOCAL_PROT_ERR = 0x04,
104 SYNDROME_WR_FLUSH_ERR = 0x05,
105 SYNDROME_MW_BIND_ERR = 0x06,
106 SYNDROME_BAD_RESP_ERR = 0x10,
107 SYNDROME_LOCAL_ACCESS_ERR = 0x11,
108 SYNDROME_REMOTE_INVAL_REQ_ERR = 0x12,
109 SYNDROME_REMOTE_ACCESS_ERR = 0x13,
110 SYNDROME_REMOTE_OP_ERR = 0x14,
111 SYNDROME_RETRY_EXC_ERR = 0x15,
112 SYNDROME_RNR_RETRY_EXC_ERR = 0x16,
113 SYNDROME_LOCAL_RDD_VIOL_ERR = 0x20,
114 SYNDROME_REMOTE_INVAL_RD_REQ_ERR = 0x21,
115 SYNDROME_REMOTE_ABORTED_ERR = 0x22,
116 SYNDROME_INVAL_EECN_ERR = 0x23,
117 SYNDROME_INVAL_EEC_STATE_ERR = 0x24
126 __be32 imm_etype_pkey_eec;
135 struct mthca_err_cqe {
148 #define MTHCA_CQ_ENTRY_OWNER_SW (0 << 7)
149 #define MTHCA_CQ_ENTRY_OWNER_HW (1 << 7)
151 #define MTHCA_TAVOR_CQ_DB_INC_CI (1 << 24)
152 #define MTHCA_TAVOR_CQ_DB_REQ_NOT (2 << 24)
153 #define MTHCA_TAVOR_CQ_DB_REQ_NOT_SOL (3 << 24)
154 #define MTHCA_TAVOR_CQ_DB_SET_CI (4 << 24)
155 #define MTHCA_TAVOR_CQ_DB_REQ_NOT_MULT (5 << 24)
157 #define MTHCA_ARBEL_CQ_DB_REQ_NOT_SOL (1 << 24)
158 #define MTHCA_ARBEL_CQ_DB_REQ_NOT (2 << 24)
159 #define MTHCA_ARBEL_CQ_DB_REQ_NOT_MULT (3 << 24)
161 static inline struct mthca_cqe *get_cqe(struct mthca_cq *cq, int entry)
164 return (struct mthca_cqe *)((u8*)cq->queue.direct.page + (entry * MTHCA_CQ_ENTRY_SIZE));
166 return (struct mthca_cqe *)((u8*)cq->queue.page_list[entry * MTHCA_CQ_ENTRY_SIZE / PAGE_SIZE].page
167 + (entry * MTHCA_CQ_ENTRY_SIZE) % PAGE_SIZE);
170 static inline struct mthca_cqe *cqe_sw(struct mthca_cq *cq, int i)
172 struct mthca_cqe *cqe = get_cqe(cq, i);
173 return MTHCA_CQ_ENTRY_OWNER_HW & cqe->owner ? NULL : cqe;
176 static inline struct mthca_cqe *next_cqe_sw(struct mthca_cq *cq)
178 return cqe_sw(cq, cq->cons_index & cq->ibcq.cqe);
181 static inline void set_cqe_hw(struct mthca_cqe *cqe)
183 cqe->owner = MTHCA_CQ_ENTRY_OWNER_HW;
186 static void dump_cqe(u32 print_lvl, struct mthca_dev *dev, void *cqe_ptr)
188 __be32 *cqe = cqe_ptr;
189 UNREFERENCED_PARAMETER(dev);
190 UNUSED_PARAM_WOWPP(print_lvl);
192 (void) cqe; /* avoid warning if mthca_dbg compiled away... */
193 HCA_PRINT(print_lvl,HCA_DBG_CQ,("CQE contents \n"));
194 HCA_PRINT(print_lvl,HCA_DBG_CQ,("\t[%2x] %08x %08x %08x %08x\n",0,
195 cl_ntoh32(cqe[0]), cl_ntoh32(cqe[1]), cl_ntoh32(cqe[2]), cl_ntoh32(cqe[3])));
196 HCA_PRINT(print_lvl,HCA_DBG_CQ,("\t[%2x] %08x %08x %08x %08x \n",16,
197 cl_ntoh32(cqe[4]), cl_ntoh32(cqe[5]), cl_ntoh32(cqe[6]), cl_ntoh32(cqe[7])));
201 * incr is ignored in native Arbel (mem-free) mode, so cq->cons_index
202 * should be correct before calling update_cons_index().
204 static inline void update_cons_index(struct mthca_dev *dev, struct mthca_cq *cq,
209 if (mthca_is_memfree(dev)) {
210 *cq->set_ci_db = cl_hton32(cq->cons_index);
213 doorbell[0] = cl_hton32(MTHCA_TAVOR_CQ_DB_INC_CI | cq->cqn);
214 doorbell[1] = cl_hton32(incr - 1);
216 mthca_write64(doorbell,
217 dev->kar + MTHCA_CQ_DOORBELL,
218 MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
222 void mthca_cq_completion(struct mthca_dev *dev, u32 cqn)
226 cq = mthca_array_get(&dev->cq_table.cq, cqn & (dev->limits.num_cqs - 1));
229 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW ,("Completion event for bogus CQ %08x\n", cqn));
233 if (mthca_is_memfree(dev)) {
234 if (cq->ibcq.ucontext)
240 cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
243 void mthca_cq_event(struct mthca_dev *dev, u32 cqn,
244 enum ib_event_type event_type)
247 struct ib_event event;
250 spin_lock(&dev->cq_table.lock, &lh);
252 cq = mthca_array_get(&dev->cq_table.cq, cqn & (dev->limits.num_cqs - 1));
255 atomic_inc(&cq->refcount);
259 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW ,("Async event for bogus CQ %08x\n", cqn));
263 event.device = &dev->ib_dev;
264 event.event = event_type;
265 event.element.cq = &cq->ibcq;
266 if (cq->ibcq.event_handler)
267 cq->ibcq.event_handler(&event, cq->ibcq.cq_context);
269 if (atomic_dec_and_test(&cq->refcount))
273 void mthca_cq_clean(struct mthca_dev *dev, u32 cqn, u32 qpn,
274 struct mthca_srq *srq)
277 struct mthca_cqe *cqe;
283 spin_lock_irq(&dev->cq_table.lock, &lht);
284 cq = mthca_array_get(&dev->cq_table.cq, cqn & (dev->limits.num_cqs - 1));
286 atomic_inc(&cq->refcount);
287 spin_unlock_irq(&lht);
292 spin_lock_irq(&cq->lock, &lh);
295 * First we need to find the current producer index, so we
296 * know where to start cleaning from. It doesn't matter if HW
297 * adds new entries after this loop -- the QP we're worried
298 * about is already in RESET, so the new entries won't come
299 * from our QP and therefore don't need to be checked.
301 for (prod_index = cq->cons_index;
302 cqe_sw(cq, prod_index & cq->ibcq.cqe);
304 if (prod_index == cq->cons_index + cq->ibcq.cqe)
308 HCA_PRINT(TRACE_LEVEL_VERBOSE,HCA_DBG_LOW,("Cleaning QPN %06x from CQN %06x; ci %d, pi %d\n",
309 qpn, cqn, cq->cons_index, prod_index));
312 * Now sweep backwards through the CQ, removing CQ entries
313 * that match our QP by copying older entries on top of them.
315 while ((int) --prod_index - (int) cq->cons_index >= 0) {
316 cqe = get_cqe(cq, prod_index & cq->ibcq.cqe);
317 if (cqe->my_qpn == cl_hton32(qpn)) {
319 mthca_free_srq_wqe(srq, cl_ntoh32(cqe->wqe));
324 memcpy(get_cqe(cq, (prod_index + nfreed) & cq->ibcq.cqe),
325 cqe, MTHCA_CQ_ENTRY_SIZE);
331 cq->cons_index += nfreed;
332 update_cons_index(dev, cq, nfreed);
335 spin_unlock_irq(&lh);
336 if (atomic_dec_and_test(&cq->refcount))
340 static void handle_error_cqe(struct mthca_dev *dev, struct mthca_cq *cq,
341 struct mthca_qp *qp, int wqe_index, int is_send,
342 struct mthca_err_cqe *cqe,
343 struct _ib_wc *entry, int *free_cqe)
348 UNREFERENCED_PARAMETER(cq);
350 if (cqe->syndrome != SYNDROME_WR_FLUSH_ERR) {
351 HCA_PRINT(TRACE_LEVEL_INFORMATION ,HCA_DBG_CQ ,("Completion with errro "
352 "(QPN %06x, WQE @ %08x, CQN %06x, index %d)\n",
353 cl_ntoh32(cqe->my_qpn), cl_ntoh32(cqe->wqe),
354 cq->cqn, cq->cons_index));
355 dump_cqe(TRACE_LEVEL_INFORMATION, dev, cqe);
360 * For completions in error, only work request ID, status, vendor error
361 * (and freed resource count for RD) have to be set.
363 switch (cqe->syndrome) {
364 case SYNDROME_LOCAL_LENGTH_ERR:
365 entry->status = IB_WCS_LOCAL_LEN_ERR;
367 case SYNDROME_LOCAL_QP_OP_ERR:
368 entry->status = IB_WCS_LOCAL_OP_ERR;
370 case SYNDROME_LOCAL_PROT_ERR:
371 entry->status = IB_WCS_LOCAL_PROTECTION_ERR;
373 case SYNDROME_WR_FLUSH_ERR:
374 entry->status = IB_WCS_WR_FLUSHED_ERR;
376 case SYNDROME_MW_BIND_ERR:
377 entry->status = IB_WCS_MEM_WINDOW_BIND_ERR;
379 case SYNDROME_BAD_RESP_ERR:
380 entry->status = IB_WCS_BAD_RESP_ERR;
382 case SYNDROME_LOCAL_ACCESS_ERR:
383 entry->status = IB_WCS_LOCAL_ACCESS_ERR;
385 case SYNDROME_REMOTE_INVAL_REQ_ERR:
386 entry->status = IB_WCS_REM_INVALID_REQ_ERR;
388 case SYNDROME_REMOTE_ACCESS_ERR:
389 entry->status = IB_WCS_REM_ACCESS_ERR;
391 case SYNDROME_REMOTE_OP_ERR:
392 entry->status = IB_WCS_REM_OP_ERR;
394 case SYNDROME_RETRY_EXC_ERR:
395 entry->status = IB_WCS_TIMEOUT_RETRY_ERR;
397 case SYNDROME_RNR_RETRY_EXC_ERR:
398 entry->status = IB_WCS_RNR_RETRY_ERR;
400 case SYNDROME_REMOTE_INVAL_RD_REQ_ERR:
401 entry->status = IB_WCS_REM_INVALID_REQ_ERR;
403 case SYNDROME_REMOTE_ABORTED_ERR:
404 case SYNDROME_LOCAL_EEC_OP_ERR:
405 case SYNDROME_LOCAL_RDD_VIOL_ERR:
406 case SYNDROME_INVAL_EECN_ERR:
407 case SYNDROME_INVAL_EEC_STATE_ERR:
409 entry->status = IB_WCS_GENERAL_ERR;
413 entry->vendor_specific = cqe->vendor_err;
416 * Mem-free HCAs always generate one CQE per WQE, even in the
417 * error case, so we don't have to check the doorbell count, etc.
419 if (mthca_is_memfree(dev))
422 mthca_free_err_wqe(dev, qp, is_send, wqe_index, &dbd, &new_wqe);
425 * If we're at the end of the WQE chain, or we've used up our
426 * doorbell count, free the CQE. Otherwise just update it for
427 * the next poll operation.
429 if (!(new_wqe & cl_hton32(0x3f)) || (!cqe->db_cnt && dbd))
432 cqe->db_cnt = cl_hton16(cl_ntoh16(cqe->db_cnt) - (u16)dbd);
434 cqe->syndrome = SYNDROME_WR_FLUSH_ERR;
439 static inline int mthca_poll_one(struct mthca_dev *dev,
441 struct mthca_qp **cur_qp,
443 struct _ib_wc *entry)
446 struct mthca_cqe *cqe;
453 HCA_ENTER(HCA_DBG_CQ);
454 cqe = next_cqe_sw(cq);
459 * Make sure we read CQ entry contents after we've checked the
465 HCA_PRINT(TRACE_LEVEL_VERBOSE,HCA_DBG_CQ,("CQ: 0x%06x/%d: CQE -> QPN 0x%06x, WQE @ 0x%08x\n",
466 cq->cqn, cq->cons_index, cl_ntoh32(cqe->my_qpn),
467 cl_ntoh32(cqe->wqe)));
468 dump_cqe(TRACE_LEVEL_VERBOSE, dev, cqe);
471 is_error = (cqe->opcode & MTHCA_ERROR_CQE_OPCODE_MASK) ==
472 MTHCA_ERROR_CQE_OPCODE_MASK;
473 is_send = is_error ? cqe->opcode & 0x01 : cqe->is_send & 0x80;
475 if (!*cur_qp || cl_ntoh32(cqe->my_qpn) != (*cur_qp)->qpn) {
477 * We do not have to take the QP table lock here,
478 * because CQs will be locked while QPs are removed
481 *cur_qp = mthca_array_get(&dev->qp_table.qp,
482 cl_ntoh32(cqe->my_qpn) &
483 (dev->limits.num_qps - 1));
485 HCA_PRINT(TRACE_LEVEL_WARNING,HCA_DBG_CQ, ("CQ entry for unknown QP %06x\n",
486 cl_ntoh32(cqe->my_qpn) & 0xffffff));
494 wqe_index = ((cl_ntoh32(cqe->wqe) - (*cur_qp)->send_wqe_offset)
496 entry->wr_id = (*cur_qp)->wrid[wqe_index +
498 } else if ((*cur_qp)->ibqp.srq) {
499 struct mthca_srq *srq = to_msrq((*cur_qp)->ibqp.srq);
500 u32 wqe = cl_ntoh32(cqe->wqe);
502 wqe_index = wqe >> srq->wqe_shift;
503 entry->wr_id = srq->wrid[wqe_index];
504 mthca_free_srq_wqe(srq, wqe);
507 wqe_index = cl_ntoh32(cqe->wqe) >> wq->wqe_shift;
508 entry->wr_id = (*cur_qp)->wrid[wqe_index];
512 if (wq->last_comp < wqe_index)
513 wq->tail += wqe_index - wq->last_comp;
515 wq->tail += wqe_index + wq->max - wq->last_comp;
517 wq->last_comp = wqe_index;
521 entry->recv.ud.recv_opt = 0;
522 switch (cqe->opcode) {
523 case MTHCA_OPCODE_RDMA_WRITE:
524 entry->wc_type = IB_WC_RDMA_WRITE;
526 case MTHCA_OPCODE_RDMA_WRITE_IMM:
527 entry->wc_type = IB_WC_RDMA_WRITE;
528 entry->recv.ud.recv_opt |= IB_RECV_OPT_IMMEDIATE;
530 case MTHCA_OPCODE_SEND:
531 entry->wc_type = IB_WC_SEND;
533 case MTHCA_OPCODE_SEND_IMM:
534 entry->wc_type = IB_WC_SEND;
535 entry->recv.ud.recv_opt |= IB_RECV_OPT_IMMEDIATE;
537 case MTHCA_OPCODE_RDMA_READ:
538 entry->wc_type = IB_WC_RDMA_READ;
539 entry->length = cl_ntoh32(cqe->byte_cnt);
541 case MTHCA_OPCODE_ATOMIC_CS:
542 entry->wc_type = IB_WC_COMPARE_SWAP;
543 entry->length = MTHCA_BYTES_PER_ATOMIC_COMPL;
545 case MTHCA_OPCODE_ATOMIC_FA:
546 entry->wc_type = IB_WC_FETCH_ADD;
547 entry->length = MTHCA_BYTES_PER_ATOMIC_COMPL;
549 case MTHCA_OPCODE_BIND_MW:
550 entry->wc_type = IB_WC_MW_BIND;
553 entry->wc_type = IB_WC_SEND;
557 entry->length = cl_ntoh32(cqe->byte_cnt);
558 switch (cqe->opcode & 0x1f) {
559 case IB_OPCODE_SEND_LAST_WITH_IMMEDIATE:
560 case IB_OPCODE_SEND_ONLY_WITH_IMMEDIATE:
561 entry->recv.ud.recv_opt = IB_RECV_OPT_IMMEDIATE;
562 entry->recv.ud.immediate_data = cqe->imm_etype_pkey_eec;
563 entry->wc_type = IB_WC_RECV;
565 case IB_OPCODE_RDMA_WRITE_LAST_WITH_IMMEDIATE:
566 case IB_OPCODE_RDMA_WRITE_ONLY_WITH_IMMEDIATE:
567 entry->recv.ud.recv_opt = IB_RECV_OPT_IMMEDIATE;
568 entry->recv.ud.immediate_data = cqe->imm_etype_pkey_eec;
569 entry->wc_type = IB_WC_RECV_RDMA_WRITE;
572 entry->recv.ud.recv_opt = 0;
573 entry->wc_type = IB_WC_RECV;
576 entry->recv.ud.remote_lid = cqe->rlid;
577 entry->recv.ud.remote_qp = cqe->rqpn & 0xffffff00;
578 entry->recv.ud.pkey_index = (u16)(cl_ntoh32(cqe->imm_etype_pkey_eec) >> 16);
579 entry->recv.ud.remote_sl = (uint8_t)(cl_ntoh16(cqe->sl_g_mlpath) >> 12);
580 entry->recv.ud.path_bits = (uint8_t)(cl_ntoh16(cqe->sl_g_mlpath) & 0x7f);
581 entry->recv.ud.recv_opt |= cl_ntoh16(cqe->sl_g_mlpath) & 0x80 ?
582 IB_RECV_OPT_GRH_VALID : 0;
584 if (!is_send && cqe->rlid == 0){
585 HCA_PRINT(TRACE_LEVEL_INFORMATION,HCA_DBG_CQ,("found rlid == 0 \n "));
586 entry->recv.ud.recv_opt |= IB_RECV_OPT_FORWARD;
590 handle_error_cqe(dev, cq, *cur_qp, wqe_index, is_send,
591 (struct mthca_err_cqe *) cqe, entry, &free_cqe);
594 entry->status = IB_WCS_SUCCESS;
597 if (likely(free_cqe)) {
602 HCA_EXIT(HCA_DBG_CQ);
606 int mthca_poll_cq(struct ib_cq *ibcq, int num_entries,
607 struct _ib_wc *entry)
609 struct mthca_dev *dev = to_mdev(ibcq->device);
610 struct mthca_cq *cq = to_mcq(ibcq);
611 struct mthca_qp *qp = NULL;
617 spin_lock_irqsave(&cq->lock, &lh);
619 for (npolled = 0; npolled < num_entries; ++npolled) {
620 err = mthca_poll_one(dev, cq, &qp,
621 &freed, entry + npolled);
628 update_cons_index(dev, cq, freed);
631 spin_unlock_irqrestore(&lh);
633 return (err == 0 || err == -EAGAIN) ? npolled : err;
636 int mthca_poll_cq_list(
637 IN struct ib_cq *ibcq,
638 IN OUT ib_wc_t** const pp_free_wclist,
639 OUT ib_wc_t** const pp_done_wclist )
641 struct mthca_dev *dev = to_mdev(ibcq->device);
642 struct mthca_cq *cq = to_mcq(ibcq);
643 struct mthca_qp *qp = NULL;
646 ib_wc_t *wc_p, **next_pp;
649 HCA_ENTER(HCA_DBG_CQ);
651 spin_lock_irqsave(&cq->lock, &lh);
654 next_pp = pp_done_wclist;
655 wc_p = *pp_free_wclist;
658 err = mthca_poll_one(dev, cq, &qp, &freed, wc_p);
662 // prepare for the next loop
664 next_pp = &wc_p->p_next;
668 // prepare the results
669 *pp_free_wclist = wc_p; /* Set the head of the free list. */
670 *next_pp = NULL; /* Clear the tail of the done list. */
672 // update consumer index
675 update_cons_index(dev, cq, freed);
678 spin_unlock_irqrestore(&lh);
679 HCA_EXIT(HCA_DBG_CQ);
680 return (err == 0 || err == -EAGAIN)? 0 : err;
684 int mthca_tavor_arm_cq(struct ib_cq *cq, enum ib_cq_notify notify)
688 doorbell[0] = cl_hton32((notify == IB_CQ_SOLICITED ?
689 MTHCA_TAVOR_CQ_DB_REQ_NOT_SOL :
690 MTHCA_TAVOR_CQ_DB_REQ_NOT) |
692 doorbell[1] = (__be32) 0xffffffff;
694 mthca_write64(doorbell,
695 to_mdev(cq->device)->kar + MTHCA_CQ_DOORBELL,
696 MTHCA_GET_DOORBELL_LOCK(&to_mdev(cq->device)->doorbell_lock));
701 int mthca_arbel_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify notify)
703 struct mthca_cq *cq = to_mcq(ibcq);
709 ci = cl_hton32(cq->cons_index);
712 doorbell[1] = cl_hton32((cq->cqn << 8) | (2 << 5) | (sn << 3) |
713 (notify == IB_CQ_SOLICITED ? 1 : 2));
715 mthca_write_db_rec(doorbell, cq->arm_db);
718 * Make sure that the doorbell record in host memory is
719 * written before ringing the doorbell via PCI MMIO.
723 doorbell[0] = cl_hton32((sn << 28) |
724 (notify == IB_CQ_SOLICITED ?
725 MTHCA_ARBEL_CQ_DB_REQ_NOT_SOL :
726 MTHCA_ARBEL_CQ_DB_REQ_NOT) |
730 mthca_write64(doorbell,
731 to_mdev(ibcq->device)->kar + MTHCA_CQ_DOORBELL,
732 MTHCA_GET_DOORBELL_LOCK(&to_mdev(ibcq->device)->doorbell_lock));
737 static void mthca_free_cq_buf(struct mthca_dev *dev, struct mthca_cq *cq)
739 mthca_buf_free(dev, (cq->ibcq.cqe + 1) * MTHCA_CQ_ENTRY_SIZE,
740 &cq->queue, cq->is_direct, &cq->mr);
743 int mthca_init_cq(struct mthca_dev *dev, int nent,
744 struct mthca_ucontext *ctx, u32 pdn,
747 int size = NEXT_PAGE_ALIGN(nent * MTHCA_CQ_ENTRY_SIZE );
748 struct mthca_mailbox *mailbox;
749 struct mthca_cq_context *cq_context;
755 cq->ibcq.cqe = nent - 1;
756 cq->is_kernel = !ctx;
758 cq->cqn = mthca_alloc(&dev->cq_table.alloc);
762 if (mthca_is_memfree(dev)) {
763 err = mthca_table_get(dev, dev->cq_table.table, cq->cqn);
772 cq->set_ci_db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_CQ_SET_CI,
773 cq->cqn, &cq->set_ci_db);
774 if (cq->set_ci_db_index < 0)
777 cq->arm_db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_CQ_ARM,
778 cq->cqn, &cq->arm_db);
779 if (cq->arm_db_index < 0)
784 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
788 cq_context = mailbox->buf;
791 err = mthca_buf_alloc(dev, size, MTHCA_MAX_DIRECT_CQ_SIZE,
792 &cq->queue, &cq->is_direct,
793 &dev->driver_pd, 1, &cq->mr);
795 goto err_out_mailbox;
797 for (i = 0; i < nent; ++i)
798 set_cqe_hw(get_cqe(cq, i));
801 spin_lock_init(&cq->lock);
802 atomic_set(&cq->refcount, 1);
803 init_waitqueue_head(&cq->wait);
804 KeInitializeMutex(&cq->mutex, 0);
806 RtlZeroMemory(cq_context, sizeof *cq_context);
807 cq_context->flags = cl_hton32(MTHCA_CQ_STATUS_OK |
808 MTHCA_CQ_STATE_DISARMED |
810 cq_context->logsize_usrpage = cl_hton32((ffs(nent) - 1) << 24);
812 cq_context->logsize_usrpage |= cl_hton32(ctx->uar.index);
814 cq_context->logsize_usrpage |= cl_hton32(dev->driver_uar.index);
815 cq_context->error_eqn = cl_hton32(dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn);
816 cq_context->comp_eqn = cl_hton32(dev->eq_table.eq[MTHCA_EQ_COMP].eqn);
817 cq_context->pd = cl_hton32(pdn);
818 cq_context->lkey = cl_hton32(cq->mr.ibmr.lkey);
819 cq_context->cqn = cl_hton32(cq->cqn);
821 if (mthca_is_memfree(dev)) {
822 cq_context->ci_db = cl_hton32(cq->set_ci_db_index);
823 cq_context->state_db = cl_hton32(cq->arm_db_index);
826 err = mthca_SW2HW_CQ(dev, mailbox, cq->cqn, &status);
828 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW ,("SW2HW_CQ failed (%d)\n", err));
829 goto err_out_free_mr;
833 HCA_PRINT(TRACE_LEVEL_WARNING,HCA_DBG_LOW,("SW2HW_CQ returned status 0x%02x\n",
836 goto err_out_free_mr;
839 spin_lock_irq(&dev->cq_table.lock, &lh);
840 if (mthca_array_set(&dev->cq_table.cq,
841 cq->cqn & (dev->limits.num_cqs - 1),
843 spin_unlock_irq(&lh);
844 goto err_out_free_mr;
846 spin_unlock_irq(&lh);
850 mthca_free_mailbox(dev, mailbox);
856 mthca_free_cq_buf(dev, cq);
859 mthca_free_mailbox(dev, mailbox);
862 if (cq->is_kernel && mthca_is_memfree(dev))
863 mthca_free_db(dev, MTHCA_DB_TYPE_CQ_ARM, cq->arm_db_index);
866 if (cq->is_kernel && mthca_is_memfree(dev))
867 mthca_free_db(dev, MTHCA_DB_TYPE_CQ_SET_CI, cq->set_ci_db_index);
870 mthca_table_put(dev, dev->cq_table.table, cq->cqn);
873 mthca_free(&dev->cq_table.alloc, cq->cqn);
878 void mthca_free_cq(struct mthca_dev *dev,
881 struct mthca_mailbox *mailbox;
886 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
887 if (IS_ERR(mailbox)) {
888 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW ,("No memory for mailbox to free CQ.\n"));
892 err = mthca_HW2SW_CQ(dev, mailbox, cq->cqn, &status);
894 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW ,("HW2SW_CQ failed (%d)\n", err));
897 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW ,("HW2SW_CQ returned status 0x%02x\n", status));
900 __be32 *ctx = mailbox->buf;
902 UNUSED_PARAM_WOWPP(ctx);
903 UNUSED_PARAM_WOWPP(j);
905 HCA_PRINT(TRACE_LEVEL_VERBOSE ,HCA_DBG_LOW ,("context for CQN %x (cons index %x, next sw %d)\n",
906 cq->cqn, cq->cons_index,
907 cq->is_kernel ? !!next_cqe_sw(cq) : 0));
908 for (j = 0; j < 16; ++j)
909 HCA_PRINT(TRACE_LEVEL_VERBOSE ,HCA_DBG_LOW ,("[%2x] %08x\n", j * 4, cl_ntoh32(ctx[j])));
911 spin_lock_irq(&dev->cq_table.lock, &lh);
912 mthca_array_clear(&dev->cq_table.cq,
913 cq->cqn & (dev->limits.num_cqs - 1));
914 spin_unlock_irq(&lh);
916 /* wait for all RUNNING DPCs on that EQ to complete */
917 ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL);
920 atomic_dec(&cq->refcount);
921 wait_event(&cq->wait, !atomic_read(&cq->refcount));
924 mthca_free_cq_buf(dev, cq);
925 if (mthca_is_memfree(dev)) {
926 mthca_free_db(dev, MTHCA_DB_TYPE_CQ_ARM, cq->arm_db_index);
927 mthca_free_db(dev, MTHCA_DB_TYPE_CQ_SET_CI, cq->set_ci_db_index);
931 mthca_table_put(dev, dev->cq_table.table, cq->cqn);
932 mthca_free(&dev->cq_table.alloc, cq->cqn);
933 mthca_free_mailbox(dev, mailbox);
936 int mthca_init_cq_table(struct mthca_dev *dev)
940 spin_lock_init(&dev->cq_table.lock);
942 err = mthca_alloc_init(&dev->cq_table.alloc,
945 dev->limits.reserved_cqs);
949 err = mthca_array_init(&dev->cq_table.cq,
950 dev->limits.num_cqs);
952 mthca_alloc_cleanup(&dev->cq_table.alloc);
957 void mthca_cleanup_cq_table(struct mthca_dev *dev)
959 mthca_array_cleanup(&dev->cq_table.cq, dev->limits.num_cqs);
960 mthca_alloc_cleanup(&dev->cq_table.alloc);