2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Mellanox Technologies. 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_eq.c 1382 2004-12-24 02:21:02Z roland $
36 #include <linux/errno.h>
37 #include <linux/interrupt.h>
38 #include <linux/pci.h>
40 #include "mthca_dev.h"
41 #include "mthca_cmd.h"
42 #include "mthca_config_reg.h"
45 MTHCA_NUM_ASYNC_EQE
= 0x80,
46 MTHCA_NUM_CMD_EQE
= 0x80,
47 MTHCA_NUM_SPARE_EQE
= 0x80,
48 MTHCA_EQ_ENTRY_SIZE
= 0x20
52 * Must be packed because start is 64 bits but only aligned to 32 bits.
54 struct mthca_eq_context
{
57 __be32 logsize_usrpage
;
58 __be32 tavor_pd
; /* reserved for Arbel */
61 __be32 arbel_pd
; /* lost_count for Tavor */
64 __be32 consumer_index
;
65 __be32 producer_index
;
67 } __attribute__((packed
));
69 #define MTHCA_EQ_STATUS_OK ( 0 << 28)
70 #define MTHCA_EQ_STATUS_OVERFLOW ( 9 << 28)
71 #define MTHCA_EQ_STATUS_WRITE_FAIL (10 << 28)
72 #define MTHCA_EQ_OWNER_SW ( 0 << 24)
73 #define MTHCA_EQ_OWNER_HW ( 1 << 24)
74 #define MTHCA_EQ_FLAG_TR ( 1 << 18)
75 #define MTHCA_EQ_FLAG_OI ( 1 << 17)
76 #define MTHCA_EQ_STATE_ARMED ( 1 << 8)
77 #define MTHCA_EQ_STATE_FIRED ( 2 << 8)
78 #define MTHCA_EQ_STATE_ALWAYS_ARMED ( 3 << 8)
79 #define MTHCA_EQ_STATE_ARBEL ( 8 << 8)
82 MTHCA_EVENT_TYPE_COMP
= 0x00,
83 MTHCA_EVENT_TYPE_PATH_MIG
= 0x01,
84 MTHCA_EVENT_TYPE_COMM_EST
= 0x02,
85 MTHCA_EVENT_TYPE_SQ_DRAINED
= 0x03,
86 MTHCA_EVENT_TYPE_SRQ_QP_LAST_WQE
= 0x13,
87 MTHCA_EVENT_TYPE_SRQ_LIMIT
= 0x14,
88 MTHCA_EVENT_TYPE_CQ_ERROR
= 0x04,
89 MTHCA_EVENT_TYPE_WQ_CATAS_ERROR
= 0x05,
90 MTHCA_EVENT_TYPE_EEC_CATAS_ERROR
= 0x06,
91 MTHCA_EVENT_TYPE_PATH_MIG_FAILED
= 0x07,
92 MTHCA_EVENT_TYPE_WQ_INVAL_REQ_ERROR
= 0x10,
93 MTHCA_EVENT_TYPE_WQ_ACCESS_ERROR
= 0x11,
94 MTHCA_EVENT_TYPE_SRQ_CATAS_ERROR
= 0x12,
95 MTHCA_EVENT_TYPE_LOCAL_CATAS_ERROR
= 0x08,
96 MTHCA_EVENT_TYPE_PORT_CHANGE
= 0x09,
97 MTHCA_EVENT_TYPE_EQ_OVERFLOW
= 0x0f,
98 MTHCA_EVENT_TYPE_ECC_DETECT
= 0x0e,
99 MTHCA_EVENT_TYPE_CMD
= 0x0a
102 #define MTHCA_ASYNC_EVENT_MASK ((1ULL << MTHCA_EVENT_TYPE_PATH_MIG) | \
103 (1ULL << MTHCA_EVENT_TYPE_COMM_EST) | \
104 (1ULL << MTHCA_EVENT_TYPE_SQ_DRAINED) | \
105 (1ULL << MTHCA_EVENT_TYPE_CQ_ERROR) | \
106 (1ULL << MTHCA_EVENT_TYPE_WQ_CATAS_ERROR) | \
107 (1ULL << MTHCA_EVENT_TYPE_EEC_CATAS_ERROR) | \
108 (1ULL << MTHCA_EVENT_TYPE_PATH_MIG_FAILED) | \
109 (1ULL << MTHCA_EVENT_TYPE_WQ_INVAL_REQ_ERROR) | \
110 (1ULL << MTHCA_EVENT_TYPE_WQ_ACCESS_ERROR) | \
111 (1ULL << MTHCA_EVENT_TYPE_LOCAL_CATAS_ERROR) | \
112 (1ULL << MTHCA_EVENT_TYPE_PORT_CHANGE) | \
113 (1ULL << MTHCA_EVENT_TYPE_ECC_DETECT))
114 #define MTHCA_SRQ_EVENT_MASK ((1ULL << MTHCA_EVENT_TYPE_SRQ_CATAS_ERROR) | \
115 (1ULL << MTHCA_EVENT_TYPE_SRQ_QP_LAST_WQE) | \
116 (1ULL << MTHCA_EVENT_TYPE_SRQ_LIMIT))
117 #define MTHCA_CMD_EVENT_MASK (1ULL << MTHCA_EVENT_TYPE_CMD)
119 #define MTHCA_EQ_DB_INC_CI (1 << 24)
120 #define MTHCA_EQ_DB_REQ_NOT (2 << 24)
121 #define MTHCA_EQ_DB_DISARM_CQ (3 << 24)
122 #define MTHCA_EQ_DB_SET_CI (4 << 24)
123 #define MTHCA_EQ_DB_ALWAYS_ARM (5 << 24)
134 } __attribute__((packed
)) comp
;
142 } __attribute__((packed
)) cmd
;
145 } __attribute__((packed
)) qp
;
148 } __attribute__((packed
)) srq
;
154 } __attribute__((packed
)) cq_err
;
158 } __attribute__((packed
)) port_change
;
162 } __attribute__((packed
));
164 #define MTHCA_EQ_ENTRY_OWNER_SW (0 << 7)
165 #define MTHCA_EQ_ENTRY_OWNER_HW (1 << 7)
167 static inline u64
async_mask(struct mthca_dev
*dev
)
169 return dev
->mthca_flags
& MTHCA_FLAG_SRQ
?
170 MTHCA_ASYNC_EVENT_MASK
| MTHCA_SRQ_EVENT_MASK
:
171 MTHCA_ASYNC_EVENT_MASK
;
174 static inline void tavor_set_eq_ci(struct mthca_dev
*dev
, struct mthca_eq
*eq
, u32 ci
)
178 doorbell
[0] = cpu_to_be32(MTHCA_EQ_DB_SET_CI
| eq
->eqn
);
179 doorbell
[1] = cpu_to_be32(ci
& (eq
->nent
- 1));
182 * This barrier makes sure that all updates to ownership bits
183 * done by set_eqe_hw() hit memory before the consumer index
184 * is updated. set_eq_ci() allows the HCA to possibly write
185 * more EQ entries, and we want to avoid the exceedingly
186 * unlikely possibility of the HCA writing an entry and then
187 * having set_eqe_hw() overwrite the owner field.
190 mthca_write64(doorbell
,
191 dev
->kar
+ MTHCA_EQ_DOORBELL
,
192 MTHCA_GET_DOORBELL_LOCK(&dev
->doorbell_lock
));
195 static inline void arbel_set_eq_ci(struct mthca_dev
*dev
, struct mthca_eq
*eq
, u32 ci
)
197 /* See comment in tavor_set_eq_ci() above. */
199 __raw_writel((__force u32
) cpu_to_be32(ci
),
200 dev
->eq_regs
.arbel
.eq_set_ci_base
+ eq
->eqn
* 8);
201 /* We still want ordering, just not swabbing, so add a barrier */
205 static inline void set_eq_ci(struct mthca_dev
*dev
, struct mthca_eq
*eq
, u32 ci
)
207 if (mthca_is_memfree(dev
))
208 arbel_set_eq_ci(dev
, eq
, ci
);
210 tavor_set_eq_ci(dev
, eq
, ci
);
213 static inline void tavor_eq_req_not(struct mthca_dev
*dev
, int eqn
)
217 doorbell
[0] = cpu_to_be32(MTHCA_EQ_DB_REQ_NOT
| eqn
);
220 mthca_write64(doorbell
,
221 dev
->kar
+ MTHCA_EQ_DOORBELL
,
222 MTHCA_GET_DOORBELL_LOCK(&dev
->doorbell_lock
));
225 static inline void arbel_eq_req_not(struct mthca_dev
*dev
, u32 eqn_mask
)
227 writel(eqn_mask
, dev
->eq_regs
.arbel
.eq_arm
);
230 static inline void disarm_cq(struct mthca_dev
*dev
, int eqn
, int cqn
)
232 if (!mthca_is_memfree(dev
)) {
235 doorbell
[0] = cpu_to_be32(MTHCA_EQ_DB_DISARM_CQ
| eqn
);
236 doorbell
[1] = cpu_to_be32(cqn
);
238 mthca_write64(doorbell
,
239 dev
->kar
+ MTHCA_EQ_DOORBELL
,
240 MTHCA_GET_DOORBELL_LOCK(&dev
->doorbell_lock
));
244 static inline struct mthca_eqe
*get_eqe(struct mthca_eq
*eq
, u32 entry
)
246 unsigned long off
= (entry
& (eq
->nent
- 1)) * MTHCA_EQ_ENTRY_SIZE
;
247 return eq
->page_list
[off
/ PAGE_SIZE
].buf
+ off
% PAGE_SIZE
;
250 static inline struct mthca_eqe
* next_eqe_sw(struct mthca_eq
*eq
)
252 struct mthca_eqe
* eqe
;
253 eqe
= get_eqe(eq
, eq
->cons_index
);
254 return (MTHCA_EQ_ENTRY_OWNER_HW
& eqe
->owner
) ? NULL
: eqe
;
257 static inline void set_eqe_hw(struct mthca_eqe
*eqe
)
259 eqe
->owner
= MTHCA_EQ_ENTRY_OWNER_HW
;
262 static void port_change(struct mthca_dev
*dev
, int port
, int active
)
264 struct ib_event record
;
266 mthca_dbg(dev
, "Port change to %s for port %d\n",
267 active
? "active" : "down", port
);
269 record
.device
= &dev
->ib_dev
;
270 record
.event
= active
? IB_EVENT_PORT_ACTIVE
: IB_EVENT_PORT_ERR
;
271 record
.element
.port_num
= port
;
273 ib_dispatch_event(&record
);
276 static int mthca_eq_int(struct mthca_dev
*dev
, struct mthca_eq
*eq
)
278 struct mthca_eqe
*eqe
;
283 while ((eqe
= next_eqe_sw(eq
))) {
285 * Make sure we read EQ entry contents after we've
286 * checked the ownership bit.
291 case MTHCA_EVENT_TYPE_COMP
:
292 disarm_cqn
= be32_to_cpu(eqe
->event
.comp
.cqn
) & 0xffffff;
293 disarm_cq(dev
, eq
->eqn
, disarm_cqn
);
294 mthca_cq_completion(dev
, disarm_cqn
);
297 case MTHCA_EVENT_TYPE_PATH_MIG
:
298 mthca_qp_event(dev
, be32_to_cpu(eqe
->event
.qp
.qpn
) & 0xffffff,
302 case MTHCA_EVENT_TYPE_COMM_EST
:
303 mthca_qp_event(dev
, be32_to_cpu(eqe
->event
.qp
.qpn
) & 0xffffff,
307 case MTHCA_EVENT_TYPE_SQ_DRAINED
:
308 mthca_qp_event(dev
, be32_to_cpu(eqe
->event
.qp
.qpn
) & 0xffffff,
309 IB_EVENT_SQ_DRAINED
);
312 case MTHCA_EVENT_TYPE_SRQ_QP_LAST_WQE
:
313 mthca_qp_event(dev
, be32_to_cpu(eqe
->event
.qp
.qpn
) & 0xffffff,
314 IB_EVENT_QP_LAST_WQE_REACHED
);
317 case MTHCA_EVENT_TYPE_SRQ_LIMIT
:
318 mthca_srq_event(dev
, be32_to_cpu(eqe
->event
.srq
.srqn
) & 0xffffff,
319 IB_EVENT_SRQ_LIMIT_REACHED
);
322 case MTHCA_EVENT_TYPE_WQ_CATAS_ERROR
:
323 mthca_qp_event(dev
, be32_to_cpu(eqe
->event
.qp
.qpn
) & 0xffffff,
327 case MTHCA_EVENT_TYPE_PATH_MIG_FAILED
:
328 mthca_qp_event(dev
, be32_to_cpu(eqe
->event
.qp
.qpn
) & 0xffffff,
329 IB_EVENT_PATH_MIG_ERR
);
332 case MTHCA_EVENT_TYPE_WQ_INVAL_REQ_ERROR
:
333 mthca_qp_event(dev
, be32_to_cpu(eqe
->event
.qp
.qpn
) & 0xffffff,
334 IB_EVENT_QP_REQ_ERR
);
337 case MTHCA_EVENT_TYPE_WQ_ACCESS_ERROR
:
338 mthca_qp_event(dev
, be32_to_cpu(eqe
->event
.qp
.qpn
) & 0xffffff,
339 IB_EVENT_QP_ACCESS_ERR
);
342 case MTHCA_EVENT_TYPE_CMD
:
344 be16_to_cpu(eqe
->event
.cmd
.token
),
345 eqe
->event
.cmd
.status
,
346 be64_to_cpu(eqe
->event
.cmd
.out_param
));
349 case MTHCA_EVENT_TYPE_PORT_CHANGE
:
351 (be32_to_cpu(eqe
->event
.port_change
.port
) >> 28) & 3,
352 eqe
->subtype
== 0x4);
355 case MTHCA_EVENT_TYPE_CQ_ERROR
:
356 mthca_warn(dev
, "CQ %s on CQN %06x\n",
357 eqe
->event
.cq_err
.syndrome
== 1 ?
358 "overrun" : "access violation",
359 be32_to_cpu(eqe
->event
.cq_err
.cqn
) & 0xffffff);
360 mthca_cq_event(dev
, be32_to_cpu(eqe
->event
.cq_err
.cqn
),
364 case MTHCA_EVENT_TYPE_EQ_OVERFLOW
:
365 mthca_warn(dev
, "EQ overrun on EQN %d\n", eq
->eqn
);
368 case MTHCA_EVENT_TYPE_EEC_CATAS_ERROR
:
369 case MTHCA_EVENT_TYPE_SRQ_CATAS_ERROR
:
370 case MTHCA_EVENT_TYPE_LOCAL_CATAS_ERROR
:
371 case MTHCA_EVENT_TYPE_ECC_DETECT
:
373 mthca_warn(dev
, "Unhandled event %02x(%02x) on EQ %d\n",
374 eqe
->type
, eqe
->subtype
, eq
->eqn
);
384 * The HCA will think the queue has overflowed if we
385 * don't tell it we've been processing events. We
386 * create our EQs with MTHCA_NUM_SPARE_EQE extra
387 * entries, so we must update our consumer index at
390 if (unlikely(set_ci
>= MTHCA_NUM_SPARE_EQE
)) {
392 * Conditional on hca_type is OK here because
393 * this is a rare case, not the fast path.
395 set_eq_ci(dev
, eq
, eq
->cons_index
);
401 * Rely on caller to set consumer index so that we don't have
402 * to test hca_type in our interrupt handling fast path.
407 static irqreturn_t
mthca_tavor_interrupt(int irq
, void *dev_ptr
)
409 struct mthca_dev
*dev
= dev_ptr
;
413 if (dev
->eq_table
.clr_mask
)
414 writel(dev
->eq_table
.clr_mask
, dev
->eq_table
.clr_int
);
416 ecr
= readl(dev
->eq_regs
.tavor
.ecr_base
+ 4);
420 writel(ecr
, dev
->eq_regs
.tavor
.ecr_base
+
421 MTHCA_ECR_CLR_BASE
- MTHCA_ECR_BASE
+ 4);
423 for (i
= 0; i
< MTHCA_NUM_EQ
; ++i
)
424 if (ecr
& dev
->eq_table
.eq
[i
].eqn_mask
) {
425 if (mthca_eq_int(dev
, &dev
->eq_table
.eq
[i
]))
426 tavor_set_eq_ci(dev
, &dev
->eq_table
.eq
[i
],
427 dev
->eq_table
.eq
[i
].cons_index
);
428 tavor_eq_req_not(dev
, dev
->eq_table
.eq
[i
].eqn
);
434 static irqreturn_t
mthca_tavor_msi_x_interrupt(int irq
, void *eq_ptr
)
436 struct mthca_eq
*eq
= eq_ptr
;
437 struct mthca_dev
*dev
= eq
->dev
;
439 mthca_eq_int(dev
, eq
);
440 tavor_set_eq_ci(dev
, eq
, eq
->cons_index
);
441 tavor_eq_req_not(dev
, eq
->eqn
);
443 /* MSI-X vectors always belong to us */
447 static irqreturn_t
mthca_arbel_interrupt(int irq
, void *dev_ptr
)
449 struct mthca_dev
*dev
= dev_ptr
;
453 if (dev
->eq_table
.clr_mask
)
454 writel(dev
->eq_table
.clr_mask
, dev
->eq_table
.clr_int
);
456 for (i
= 0; i
< MTHCA_NUM_EQ
; ++i
)
457 if (mthca_eq_int(dev
, &dev
->eq_table
.eq
[i
])) {
459 arbel_set_eq_ci(dev
, &dev
->eq_table
.eq
[i
],
460 dev
->eq_table
.eq
[i
].cons_index
);
463 arbel_eq_req_not(dev
, dev
->eq_table
.arm_mask
);
465 return IRQ_RETVAL(work
);
468 static irqreturn_t
mthca_arbel_msi_x_interrupt(int irq
, void *eq_ptr
)
470 struct mthca_eq
*eq
= eq_ptr
;
471 struct mthca_dev
*dev
= eq
->dev
;
473 mthca_eq_int(dev
, eq
);
474 arbel_set_eq_ci(dev
, eq
, eq
->cons_index
);
475 arbel_eq_req_not(dev
, eq
->eqn_mask
);
477 /* MSI-X vectors always belong to us */
481 static int mthca_create_eq(struct mthca_dev
*dev
,
487 u64
*dma_list
= NULL
;
489 struct mthca_mailbox
*mailbox
;
490 struct mthca_eq_context
*eq_context
;
496 eq
->nent
= roundup_pow_of_two(max(nent
, 2));
497 npages
= ALIGN(eq
->nent
* MTHCA_EQ_ENTRY_SIZE
, PAGE_SIZE
) / PAGE_SIZE
;
499 eq
->page_list
= kmalloc(npages
* sizeof *eq
->page_list
,
504 for (i
= 0; i
< npages
; ++i
)
505 eq
->page_list
[i
].buf
= NULL
;
507 dma_list
= kmalloc(npages
* sizeof *dma_list
, GFP_KERNEL
);
511 mailbox
= mthca_alloc_mailbox(dev
, GFP_KERNEL
);
514 eq_context
= mailbox
->buf
;
516 for (i
= 0; i
< npages
; ++i
) {
517 eq
->page_list
[i
].buf
= dma_alloc_coherent(&dev
->pdev
->dev
,
518 PAGE_SIZE
, &t
, GFP_KERNEL
);
519 if (!eq
->page_list
[i
].buf
)
520 goto err_out_free_pages
;
523 pci_unmap_addr_set(&eq
->page_list
[i
], mapping
, t
);
525 memset(eq
->page_list
[i
].buf
, 0, PAGE_SIZE
);
528 for (i
= 0; i
< eq
->nent
; ++i
)
529 set_eqe_hw(get_eqe(eq
, i
));
531 eq
->eqn
= mthca_alloc(&dev
->eq_table
.alloc
);
533 goto err_out_free_pages
;
535 err
= mthca_mr_alloc_phys(dev
, dev
->driver_pd
.pd_num
,
536 dma_list
, PAGE_SHIFT
, npages
,
537 0, npages
* PAGE_SIZE
,
538 MTHCA_MPT_FLAG_LOCAL_WRITE
|
539 MTHCA_MPT_FLAG_LOCAL_READ
,
542 goto err_out_free_eq
;
544 memset(eq_context
, 0, sizeof *eq_context
);
545 eq_context
->flags
= cpu_to_be32(MTHCA_EQ_STATUS_OK
|
547 MTHCA_EQ_STATE_ARMED
|
549 if (mthca_is_memfree(dev
))
550 eq_context
->flags
|= cpu_to_be32(MTHCA_EQ_STATE_ARBEL
);
552 eq_context
->logsize_usrpage
= cpu_to_be32((ffs(eq
->nent
) - 1) << 24);
553 if (mthca_is_memfree(dev
)) {
554 eq_context
->arbel_pd
= cpu_to_be32(dev
->driver_pd
.pd_num
);
556 eq_context
->logsize_usrpage
|= cpu_to_be32(dev
->driver_uar
.index
);
557 eq_context
->tavor_pd
= cpu_to_be32(dev
->driver_pd
.pd_num
);
559 eq_context
->intr
= intr
;
560 eq_context
->lkey
= cpu_to_be32(eq
->mr
.ibmr
.lkey
);
562 err
= mthca_SW2HW_EQ(dev
, mailbox
, eq
->eqn
, &status
);
564 mthca_warn(dev
, "SW2HW_EQ failed (%d)\n", err
);
565 goto err_out_free_mr
;
568 mthca_warn(dev
, "SW2HW_EQ returned status 0x%02x\n",
571 goto err_out_free_mr
;
575 mthca_free_mailbox(dev
, mailbox
);
577 eq
->eqn_mask
= swab32(1 << eq
->eqn
);
580 dev
->eq_table
.arm_mask
|= eq
->eqn_mask
;
582 mthca_dbg(dev
, "Allocated EQ %d with %d entries\n",
588 mthca_free_mr(dev
, &eq
->mr
);
591 mthca_free(&dev
->eq_table
.alloc
, eq
->eqn
);
594 for (i
= 0; i
< npages
; ++i
)
595 if (eq
->page_list
[i
].buf
)
596 dma_free_coherent(&dev
->pdev
->dev
, PAGE_SIZE
,
597 eq
->page_list
[i
].buf
,
598 pci_unmap_addr(&eq
->page_list
[i
],
601 mthca_free_mailbox(dev
, mailbox
);
604 kfree(eq
->page_list
);
611 static void mthca_free_eq(struct mthca_dev
*dev
,
614 struct mthca_mailbox
*mailbox
;
617 int npages
= (eq
->nent
* MTHCA_EQ_ENTRY_SIZE
+ PAGE_SIZE
- 1) /
621 mailbox
= mthca_alloc_mailbox(dev
, GFP_KERNEL
);
625 err
= mthca_HW2SW_EQ(dev
, mailbox
, eq
->eqn
, &status
);
627 mthca_warn(dev
, "HW2SW_EQ failed (%d)\n", err
);
629 mthca_warn(dev
, "HW2SW_EQ returned status 0x%02x\n", status
);
631 dev
->eq_table
.arm_mask
&= ~eq
->eqn_mask
;
634 mthca_dbg(dev
, "Dumping EQ context %02x:\n", eq
->eqn
);
635 for (i
= 0; i
< sizeof (struct mthca_eq_context
) / 4; ++i
) {
637 printk("[%02x] ", i
* 4);
638 printk(" %08x", be32_to_cpup(mailbox
->buf
+ i
* 4));
639 if ((i
+ 1) % 4 == 0)
644 mthca_free_mr(dev
, &eq
->mr
);
645 for (i
= 0; i
< npages
; ++i
)
646 pci_free_consistent(dev
->pdev
, PAGE_SIZE
,
647 eq
->page_list
[i
].buf
,
648 pci_unmap_addr(&eq
->page_list
[i
], mapping
));
650 kfree(eq
->page_list
);
651 mthca_free_mailbox(dev
, mailbox
);
654 static void mthca_free_irqs(struct mthca_dev
*dev
)
658 if (dev
->eq_table
.have_irq
)
659 free_irq(dev
->pdev
->irq
, dev
);
660 for (i
= 0; i
< MTHCA_NUM_EQ
; ++i
)
661 if (dev
->eq_table
.eq
[i
].have_irq
)
662 free_irq(dev
->eq_table
.eq
[i
].msi_x_vector
,
663 dev
->eq_table
.eq
+ i
);
666 static int mthca_map_reg(struct mthca_dev
*dev
,
667 unsigned long offset
, unsigned long size
,
670 unsigned long base
= pci_resource_start(dev
->pdev
, 0);
672 if (!request_mem_region(base
+ offset
, size
, DRV_NAME
))
675 *map
= ioremap(base
+ offset
, size
);
677 release_mem_region(base
+ offset
, size
);
684 static void mthca_unmap_reg(struct mthca_dev
*dev
, unsigned long offset
,
685 unsigned long size
, void __iomem
*map
)
687 unsigned long base
= pci_resource_start(dev
->pdev
, 0);
689 release_mem_region(base
+ offset
, size
);
693 static int mthca_map_eq_regs(struct mthca_dev
*dev
)
695 if (mthca_is_memfree(dev
)) {
697 * We assume that the EQ arm and EQ set CI registers
698 * fall within the first BAR. We can't trust the
699 * values firmware gives us, since those addresses are
700 * valid on the HCA's side of the PCI bus but not
701 * necessarily the host side.
703 if (mthca_map_reg(dev
, (pci_resource_len(dev
->pdev
, 0) - 1) &
704 dev
->fw
.arbel
.clr_int_base
, MTHCA_CLR_INT_SIZE
,
706 mthca_err(dev
, "Couldn't map interrupt clear register, "
712 * Add 4 because we limit ourselves to EQs 0 ... 31,
713 * so we only need the low word of the register.
715 if (mthca_map_reg(dev
, ((pci_resource_len(dev
->pdev
, 0) - 1) &
716 dev
->fw
.arbel
.eq_arm_base
) + 4, 4,
717 &dev
->eq_regs
.arbel
.eq_arm
)) {
718 mthca_err(dev
, "Couldn't map EQ arm register, aborting.\n");
719 mthca_unmap_reg(dev
, (pci_resource_len(dev
->pdev
, 0) - 1) &
720 dev
->fw
.arbel
.clr_int_base
, MTHCA_CLR_INT_SIZE
,
725 if (mthca_map_reg(dev
, (pci_resource_len(dev
->pdev
, 0) - 1) &
726 dev
->fw
.arbel
.eq_set_ci_base
,
727 MTHCA_EQ_SET_CI_SIZE
,
728 &dev
->eq_regs
.arbel
.eq_set_ci_base
)) {
729 mthca_err(dev
, "Couldn't map EQ CI register, aborting.\n");
730 mthca_unmap_reg(dev
, ((pci_resource_len(dev
->pdev
, 0) - 1) &
731 dev
->fw
.arbel
.eq_arm_base
) + 4, 4,
732 dev
->eq_regs
.arbel
.eq_arm
);
733 mthca_unmap_reg(dev
, (pci_resource_len(dev
->pdev
, 0) - 1) &
734 dev
->fw
.arbel
.clr_int_base
, MTHCA_CLR_INT_SIZE
,
739 if (mthca_map_reg(dev
, MTHCA_CLR_INT_BASE
, MTHCA_CLR_INT_SIZE
,
741 mthca_err(dev
, "Couldn't map interrupt clear register, "
746 if (mthca_map_reg(dev
, MTHCA_ECR_BASE
,
747 MTHCA_ECR_SIZE
+ MTHCA_ECR_CLR_SIZE
,
748 &dev
->eq_regs
.tavor
.ecr_base
)) {
749 mthca_err(dev
, "Couldn't map ecr register, "
751 mthca_unmap_reg(dev
, MTHCA_CLR_INT_BASE
, MTHCA_CLR_INT_SIZE
,
761 static void mthca_unmap_eq_regs(struct mthca_dev
*dev
)
763 if (mthca_is_memfree(dev
)) {
764 mthca_unmap_reg(dev
, (pci_resource_len(dev
->pdev
, 0) - 1) &
765 dev
->fw
.arbel
.eq_set_ci_base
,
766 MTHCA_EQ_SET_CI_SIZE
,
767 dev
->eq_regs
.arbel
.eq_set_ci_base
);
768 mthca_unmap_reg(dev
, ((pci_resource_len(dev
->pdev
, 0) - 1) &
769 dev
->fw
.arbel
.eq_arm_base
) + 4, 4,
770 dev
->eq_regs
.arbel
.eq_arm
);
771 mthca_unmap_reg(dev
, (pci_resource_len(dev
->pdev
, 0) - 1) &
772 dev
->fw
.arbel
.clr_int_base
, MTHCA_CLR_INT_SIZE
,
775 mthca_unmap_reg(dev
, MTHCA_ECR_BASE
,
776 MTHCA_ECR_SIZE
+ MTHCA_ECR_CLR_SIZE
,
777 dev
->eq_regs
.tavor
.ecr_base
);
778 mthca_unmap_reg(dev
, MTHCA_CLR_INT_BASE
, MTHCA_CLR_INT_SIZE
,
783 int mthca_map_eq_icm(struct mthca_dev
*dev
, u64 icm_virt
)
789 * We assume that mapping one page is enough for the whole EQ
790 * context table. This is fine with all current HCAs, because
791 * we only use 32 EQs and each EQ uses 32 bytes of context
792 * memory, or 1 KB total.
794 dev
->eq_table
.icm_virt
= icm_virt
;
795 dev
->eq_table
.icm_page
= alloc_page(GFP_HIGHUSER
);
796 if (!dev
->eq_table
.icm_page
)
798 dev
->eq_table
.icm_dma
= pci_map_page(dev
->pdev
, dev
->eq_table
.icm_page
, 0,
799 PAGE_SIZE
, PCI_DMA_BIDIRECTIONAL
);
800 if (pci_dma_mapping_error(dev
->eq_table
.icm_dma
)) {
801 __free_page(dev
->eq_table
.icm_page
);
805 ret
= mthca_MAP_ICM_page(dev
, dev
->eq_table
.icm_dma
, icm_virt
, &status
);
809 pci_unmap_page(dev
->pdev
, dev
->eq_table
.icm_dma
, PAGE_SIZE
,
810 PCI_DMA_BIDIRECTIONAL
);
811 __free_page(dev
->eq_table
.icm_page
);
817 void mthca_unmap_eq_icm(struct mthca_dev
*dev
)
821 mthca_UNMAP_ICM(dev
, dev
->eq_table
.icm_virt
, 1, &status
);
822 pci_unmap_page(dev
->pdev
, dev
->eq_table
.icm_dma
, PAGE_SIZE
,
823 PCI_DMA_BIDIRECTIONAL
);
824 __free_page(dev
->eq_table
.icm_page
);
827 int mthca_init_eq_table(struct mthca_dev
*dev
)
834 err
= mthca_alloc_init(&dev
->eq_table
.alloc
,
836 dev
->limits
.num_eqs
- 1,
837 dev
->limits
.reserved_eqs
);
841 err
= mthca_map_eq_regs(dev
);
845 if (dev
->mthca_flags
& MTHCA_FLAG_MSI
||
846 dev
->mthca_flags
& MTHCA_FLAG_MSI_X
) {
847 dev
->eq_table
.clr_mask
= 0;
849 dev
->eq_table
.clr_mask
=
850 swab32(1 << (dev
->eq_table
.inta_pin
& 31));
851 dev
->eq_table
.clr_int
= dev
->clr_base
+
852 (dev
->eq_table
.inta_pin
< 32 ? 4 : 0);
855 dev
->eq_table
.arm_mask
= 0;
857 intr
= (dev
->mthca_flags
& MTHCA_FLAG_MSI
) ?
858 128 : dev
->eq_table
.inta_pin
;
860 err
= mthca_create_eq(dev
, dev
->limits
.num_cqs
+ MTHCA_NUM_SPARE_EQE
,
861 (dev
->mthca_flags
& MTHCA_FLAG_MSI_X
) ? 128 : intr
,
862 &dev
->eq_table
.eq
[MTHCA_EQ_COMP
]);
866 err
= mthca_create_eq(dev
, MTHCA_NUM_ASYNC_EQE
+ MTHCA_NUM_SPARE_EQE
,
867 (dev
->mthca_flags
& MTHCA_FLAG_MSI_X
) ? 129 : intr
,
868 &dev
->eq_table
.eq
[MTHCA_EQ_ASYNC
]);
872 err
= mthca_create_eq(dev
, MTHCA_NUM_CMD_EQE
+ MTHCA_NUM_SPARE_EQE
,
873 (dev
->mthca_flags
& MTHCA_FLAG_MSI_X
) ? 130 : intr
,
874 &dev
->eq_table
.eq
[MTHCA_EQ_CMD
]);
878 if (dev
->mthca_flags
& MTHCA_FLAG_MSI_X
) {
879 static const char *eq_name
[] = {
880 [MTHCA_EQ_COMP
] = DRV_NAME
" (comp)",
881 [MTHCA_EQ_ASYNC
] = DRV_NAME
" (async)",
882 [MTHCA_EQ_CMD
] = DRV_NAME
" (cmd)"
885 for (i
= 0; i
< MTHCA_NUM_EQ
; ++i
) {
886 err
= request_irq(dev
->eq_table
.eq
[i
].msi_x_vector
,
887 mthca_is_memfree(dev
) ?
888 mthca_arbel_msi_x_interrupt
:
889 mthca_tavor_msi_x_interrupt
,
890 0, eq_name
[i
], dev
->eq_table
.eq
+ i
);
893 dev
->eq_table
.eq
[i
].have_irq
= 1;
896 err
= request_irq(dev
->pdev
->irq
,
897 mthca_is_memfree(dev
) ?
898 mthca_arbel_interrupt
:
899 mthca_tavor_interrupt
,
900 IRQF_SHARED
, DRV_NAME
, dev
);
903 dev
->eq_table
.have_irq
= 1;
906 err
= mthca_MAP_EQ(dev
, async_mask(dev
),
907 0, dev
->eq_table
.eq
[MTHCA_EQ_ASYNC
].eqn
, &status
);
909 mthca_warn(dev
, "MAP_EQ for async EQ %d failed (%d)\n",
910 dev
->eq_table
.eq
[MTHCA_EQ_ASYNC
].eqn
, err
);
912 mthca_warn(dev
, "MAP_EQ for async EQ %d returned status 0x%02x\n",
913 dev
->eq_table
.eq
[MTHCA_EQ_ASYNC
].eqn
, status
);
915 err
= mthca_MAP_EQ(dev
, MTHCA_CMD_EVENT_MASK
,
916 0, dev
->eq_table
.eq
[MTHCA_EQ_CMD
].eqn
, &status
);
918 mthca_warn(dev
, "MAP_EQ for cmd EQ %d failed (%d)\n",
919 dev
->eq_table
.eq
[MTHCA_EQ_CMD
].eqn
, err
);
921 mthca_warn(dev
, "MAP_EQ for cmd EQ %d returned status 0x%02x\n",
922 dev
->eq_table
.eq
[MTHCA_EQ_CMD
].eqn
, status
);
924 for (i
= 0; i
< MTHCA_NUM_EQ
; ++i
)
925 if (mthca_is_memfree(dev
))
926 arbel_eq_req_not(dev
, dev
->eq_table
.eq
[i
].eqn_mask
);
928 tavor_eq_req_not(dev
, dev
->eq_table
.eq
[i
].eqn
);
933 mthca_free_irqs(dev
);
934 mthca_free_eq(dev
, &dev
->eq_table
.eq
[MTHCA_EQ_CMD
]);
937 mthca_free_eq(dev
, &dev
->eq_table
.eq
[MTHCA_EQ_ASYNC
]);
940 mthca_free_eq(dev
, &dev
->eq_table
.eq
[MTHCA_EQ_COMP
]);
943 mthca_unmap_eq_regs(dev
);
946 mthca_alloc_cleanup(&dev
->eq_table
.alloc
);
950 void mthca_cleanup_eq_table(struct mthca_dev
*dev
)
955 mthca_free_irqs(dev
);
957 mthca_MAP_EQ(dev
, async_mask(dev
),
958 1, dev
->eq_table
.eq
[MTHCA_EQ_ASYNC
].eqn
, &status
);
959 mthca_MAP_EQ(dev
, MTHCA_CMD_EVENT_MASK
,
960 1, dev
->eq_table
.eq
[MTHCA_EQ_CMD
].eqn
, &status
);
962 for (i
= 0; i
< MTHCA_NUM_EQ
; ++i
)
963 mthca_free_eq(dev
, &dev
->eq_table
.eq
[i
]);
965 mthca_unmap_eq_regs(dev
);
967 mthca_alloc_cleanup(&dev
->eq_table
.alloc
);