2 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
3 * Copyright (c) 2005, 2006, 2007 Cisco Systems, 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
39 MLX4_NUM_ASYNC_EQE = 0x100,
40 MLX4_NUM_SPARE_EQE = 0x80,
41 MLX4_EQ_ENTRY_SIZE = 0x20
45 * Must be packed because start is 64 bits but only aligned to 32 bits.
47 struct mlx4_eq_context {
61 __be32 mtt_base_addr_l;
63 __be32 consumer_index;
64 __be32 producer_index;
68 #define MLX4_EQ_STATUS_OK ( 0 << 28)
69 #define MLX4_EQ_STATUS_WRITE_FAIL (10 << 28)
70 #define MLX4_EQ_OWNER_SW ( 0 << 24)
71 #define MLX4_EQ_OWNER_HW ( 1 << 24)
72 #define MLX4_EQ_FLAG_EC ( 1 << 18)
73 #define MLX4_EQ_FLAG_OI ( 1 << 17)
74 #define MLX4_EQ_STATE_ARMED ( 9 << 8)
75 #define MLX4_EQ_STATE_FIRED (10 << 8)
76 #define MLX4_EQ_STATE_ALWAYS_ARMED (11 << 8)
78 #define MLX4_ASYNC_EVENT_MASK ((1ull << MLX4_EVENT_TYPE_PATH_MIG) | \
79 (1ull << MLX4_EVENT_TYPE_COMM_EST) | \
80 (1ull << MLX4_EVENT_TYPE_SQ_DRAINED) | \
81 (1ull << MLX4_EVENT_TYPE_CQ_ERROR) | \
82 (1ull << MLX4_EVENT_TYPE_WQ_CATAS_ERROR) | \
83 (1ull << MLX4_EVENT_TYPE_EEC_CATAS_ERROR) | \
84 (1ull << MLX4_EVENT_TYPE_PATH_MIG_FAILED) | \
85 (1ull << MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR) | \
86 (1ull << MLX4_EVENT_TYPE_WQ_ACCESS_ERROR) | \
87 (1ull << MLX4_EVENT_TYPE_PORT_CHANGE) | \
88 (1ull << MLX4_EVENT_TYPE_ECC_DETECT) | \
89 (1ull << MLX4_EVENT_TYPE_SRQ_CATAS_ERROR) | \
90 (1ull << MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE) | \
91 (1ull << MLX4_EVENT_TYPE_SRQ_LIMIT) | \
92 (1ull << MLX4_EVENT_TYPE_CMD))
104 } __attribute__((packed)) comp;
112 } __attribute__((packed)) cmd;
115 } __attribute__((packed)) qp;
118 } __attribute__((packed)) srq;
124 } __attribute__((packed)) cq_err;
128 } __attribute__((packed)) port_change;
132 } __attribute__((packed));
135 static void eq_set_ci(struct mlx4_eq *eq, int req_not)
137 __raw_writel((__force u32) cpu_to_be32((eq->cons_index & 0xffffff) |
140 /* We still want ordering, just not swabbing, so add a barrier */
144 static struct mlx4_eqe *get_eqe(struct mlx4_eq *eq, u32 entry)
146 unsigned long off = (entry & (eq->nent - 1)) * MLX4_EQ_ENTRY_SIZE;
147 return (struct mlx4_eqe *)(eq->page_list[off / PAGE_SIZE].buf + off % PAGE_SIZE);
150 static struct mlx4_eqe *next_eqe_sw(struct mlx4_eq *eq)
152 struct mlx4_eqe *eqe = get_eqe(eq, eq->cons_index);
153 return !!(eqe->owner & 0x80) ^ !!(eq->cons_index & eq->nent) ? NULL : eqe;
156 #pragma warning( disable : 4706)
157 static int mlx4_eq_int(struct mlx4_dev *dev, struct mlx4_eq *eq)
159 struct mlx4_eqe *eqe;
163 static const uint32_t cDpcMaxTime = 10000; //max time to spend in a while loop
165 uint64_t start = cl_get_time_stamp();
167 while ((eqe = next_eqe_sw(eq))) {
169 * Make sure we read EQ entry contents after we've
170 * checked the ownership bit.
175 case MLX4_EVENT_TYPE_COMP:
176 cqn = be32_to_cpu(eqe->event.comp.cqn) & 0xffffff;
177 mlx4_cq_completion(dev, cqn);
180 case MLX4_EVENT_TYPE_PATH_MIG:
181 case MLX4_EVENT_TYPE_COMM_EST:
182 case MLX4_EVENT_TYPE_SQ_DRAINED:
183 case MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE:
184 case MLX4_EVENT_TYPE_WQ_CATAS_ERROR:
185 case MLX4_EVENT_TYPE_PATH_MIG_FAILED:
186 case MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
187 case MLX4_EVENT_TYPE_WQ_ACCESS_ERROR:
188 mlx4_qp_event(dev, be32_to_cpu(eqe->event.qp.qpn) & 0xffffff,
192 case MLX4_EVENT_TYPE_SRQ_LIMIT:
193 case MLX4_EVENT_TYPE_SRQ_CATAS_ERROR:
194 mlx4_srq_event(dev, be32_to_cpu(eqe->event.srq.srqn) & 0xffffff,
198 case MLX4_EVENT_TYPE_CMD:
200 be16_to_cpu(eqe->event.cmd.token),
201 eqe->event.cmd.status,
202 be64_to_cpu(eqe->event.cmd.out_param));
205 case MLX4_EVENT_TYPE_PORT_CHANGE:
206 mlx4_dispatch_event(dev, eqe->type, eqe->subtype,
207 be32_to_cpu(eqe->event.port_change.port) >> 28);
210 case MLX4_EVENT_TYPE_CQ_ERROR:
211 mlx4_warn(dev, "CQ %s on CQN %06x\n",
212 eqe->event.cq_err.syndrome == 1 ?
213 "overrun" : "access violation",
214 be32_to_cpu(eqe->event.cq_err.cqn) & 0xffffff);
215 mlx4_cq_event(dev, be32_to_cpu(eqe->event.cq_err.cqn),
219 case MLX4_EVENT_TYPE_EQ_OVERFLOW:
220 mlx4_warn(dev, "EQ overrun on EQN %d\n", eq->eqn);
223 case MLX4_EVENT_TYPE_EEC_CATAS_ERROR:
224 case MLX4_EVENT_TYPE_ECC_DETECT:
226 mlx4_warn(dev, "Unhandled event %02x(%02x) on EQ %d at index %u\n",
227 eqe->type, eqe->subtype, eq->eqn, eq->cons_index);
237 * The HCA will think the queue has overflowed if we
238 * don't tell it we've been processing events. We
239 * create our EQs with MLX4_NUM_SPARE_EQE extra
240 * entries, so we must update our consumer index at
243 if (unlikely(set_ci >= MLX4_NUM_SPARE_EQE)) {
245 * Conditional on hca_type is OK here because
246 * this is a rare case, not the fast path.
252 if (cl_get_time_stamp() - start > cDpcMaxTime ) {
253 break; //allow other DPCs as well
261 #pragma warning(disable:4706)
263 static void mlx4_dpc( PRKDPC dpc,
264 PVOID ctx, PVOID arg1, PVOID arg2 )
266 struct mlx4_eq *eq = ctx;
268 UNREFERENCED_PARAMETER(dpc);
269 UNREFERENCED_PARAMETER(arg1);
270 UNREFERENCED_PARAMETER(arg2);
272 spin_lock_dpc(&eq->lock);
273 mlx4_eq_int(eq->dev, eq);
274 spin_unlock_dpc(&eq->lock);
277 static BOOLEAN mlx4_interrupt(
278 IN struct _KINTERRUPT *Interrupt,
279 IN PVOID ServiceContext
282 struct mlx4_dev *dev = ServiceContext;
283 struct mlx4_priv *priv = mlx4_priv(dev);
287 UNUSED_PARAM(Interrupt);
289 writel(priv->eq_table.clr_mask, priv->eq_table.clr_int);
291 for (i = 0; i < MLX4_NUM_EQ; ++i) {
292 if ( next_eqe_sw(&priv->eq_table.eq[i]) ) {
294 /* another interrupt may happen instantly after writel above.
295 If it comes to another processor, mlx4_interrupt will be called
296 and try to schedule the same DPC. So we protect KeInsertQueueDpc
299 while(InterlockedCompareExchange(&dev->pdev->dpc_lock, 1, 0));
301 KeInsertQueueDpc(&priv->eq_table.eq[i].dpc, NULL, NULL);
302 InterlockedCompareExchange(&dev->pdev->dpc_lock, 0, 1);
305 /* re-arm the EQ for a case when interrupt comes before EQE
306 and we didn't scheduled the DPC */
307 eq_set_ci(&priv->eq_table.eq[i], 1);
311 return (BOOLEAN)work;
314 #ifdef CONFIG_PCI_MSI
317 static irqreturn_t mlx4_msi_x_interrupt(int irq, void *eq_ptr)
319 struct mlx4_eq *eq = eq_ptr;
320 struct mlx4_dev *dev = eq->dev;
322 mlx4_eq_int(dev, eq);
324 /* MSI-X vectors always belong to us */
330 static int mlx4_MAP_EQ(struct mlx4_dev *dev, u64 event_mask, int unmap,
333 return mlx4_cmd(dev, event_mask, (unmap << 31) | eq_num,
334 0, MLX4_CMD_MAP_EQ, MLX4_CMD_TIME_CLASS_B);
337 static int mlx4_SW2HW_EQ(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
340 return mlx4_cmd(dev, mailbox->dma.da, eq_num, 0, MLX4_CMD_SW2HW_EQ,
341 MLX4_CMD_TIME_CLASS_A);
344 static int mlx4_HW2SW_EQ(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
347 return mlx4_cmd_box(dev, 0, mailbox->dma.da, eq_num, 0, MLX4_CMD_HW2SW_EQ,
348 MLX4_CMD_TIME_CLASS_A);
351 static void __iomem *mlx4_get_eq_uar(struct mlx4_dev *dev, struct mlx4_eq *eq)
353 struct mlx4_priv *priv = mlx4_priv(dev);
356 index = eq->eqn / 4 - dev->caps.reserved_eqs / 4;
358 if (!priv->eq_table.uar_map[index]) {
359 priv->eq_table.uar_map[index] =
360 ioremap(pci_resource_start(dev->pdev, 2) +
361 ((eq->eqn / 4) << PAGE_SHIFT),
363 if (!priv->eq_table.uar_map[index]) {
364 mlx4_err(dev, "Couldn't map EQ doorbell for EQN 0x%06x\n",
370 return priv->eq_table.uar_map[index] + 0x800 + 8 * (eq->eqn % 4);
373 static int mlx4_create_eq(struct mlx4_dev *dev, int nent,
374 u8 intr, struct mlx4_eq *eq)
376 struct mlx4_priv *priv = mlx4_priv(dev);
377 struct mlx4_cmd_mailbox *mailbox;
378 struct mlx4_eq_context *eq_context;
380 u64 *dma_list = NULL;
387 eq->nent = roundup_pow_of_two(max(nent, 2));
388 npages = (int)(NEXT_PAGE_ALIGN(eq->nent * MLX4_EQ_ENTRY_SIZE) / PAGE_SIZE);
390 eq->page_list = kmalloc(npages * sizeof *eq->page_list,
395 for (i = 0; i < npages; ++i)
396 eq->page_list[i].buf = NULL;
398 dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
402 mailbox = mlx4_alloc_cmd_mailbox(dev);
405 eq_context = (struct mlx4_eq_context *)mailbox->buf;
407 for (i = 0; i < npages; ++i) {
408 eq->page_list[i].buf = dma_alloc_coherent(&dev->pdev->dev,
409 PAGE_SIZE, &t, GFP_KERNEL);
410 if (!eq->page_list[i].buf)
411 goto err_out_free_pages;
414 eq->page_list[i].map = t;
416 memset(eq->page_list[i].buf, 0, PAGE_SIZE);
419 eq->eqn = mlx4_bitmap_alloc(&priv->eq_table.bitmap);
421 goto err_out_free_pages;
423 eq->doorbell = mlx4_get_eq_uar(dev, eq);
426 goto err_out_free_eq;
429 err = mlx4_mtt_init(dev, npages, PAGE_SHIFT, &eq->mtt);
431 goto err_out_free_eq;
433 err = mlx4_write_mtt(dev, &eq->mtt, 0, npages, dma_list);
435 goto err_out_free_mtt;
437 memset(eq_context, 0, sizeof *eq_context);
438 eq_context->flags = cpu_to_be32(MLX4_EQ_STATUS_OK |
439 MLX4_EQ_STATE_ARMED);
440 eq_context->log_eq_size = (u8)ilog2(eq->nent);
441 eq_context->intr = intr;
442 eq_context->log_page_size = PAGE_SHIFT - MLX4_ICM_PAGE_SHIFT;
444 mtt_addr = mlx4_mtt_addr(dev, &eq->mtt);
445 eq_context->mtt_base_addr_h = (u8)(mtt_addr >> 32);
446 eq_context->mtt_base_addr_l = cpu_to_be32(mtt_addr & 0xffffffff);
448 err = mlx4_SW2HW_EQ(dev, mailbox, eq->eqn);
450 mlx4_warn(dev, "SW2HW_EQ failed (%d)\n", err);
451 goto err_out_free_mtt;
455 mlx4_free_cmd_mailbox(dev, mailbox);
462 mlx4_mtt_cleanup(dev, &eq->mtt);
465 mlx4_bitmap_free(&priv->eq_table.bitmap, eq->eqn);
468 for (i = 0; i < npages; ++i)
469 if (eq->page_list[i].buf)
470 dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
471 eq->page_list[i].buf,
472 eq->page_list[i].map);
474 mlx4_free_cmd_mailbox(dev, mailbox);
477 kfree(eq->page_list);
484 static void mlx4_free_eq(struct mlx4_dev *dev,
487 struct mlx4_priv *priv = mlx4_priv(dev);
488 struct mlx4_cmd_mailbox *mailbox;
490 int npages = (int)(NEXT_PAGE_ALIGN(MLX4_EQ_ENTRY_SIZE * eq->nent) / PAGE_SIZE);
493 mailbox = mlx4_alloc_cmd_mailbox(dev);
497 err = mlx4_HW2SW_EQ(dev, mailbox, eq->eqn);
499 mlx4_warn(dev, "HW2SW_EQ failed (%d)\n", err);
503 mlx4_dbg(dev, "Dumping EQ context %02x:\n", eq->eqn);
504 for (i = 0; i < sizeof (struct mlx4_eq_context) / 4; ++i) {
506 printk("[%02x] ", i * 4);
507 printk(" %08x", be32_to_cpup(mailbox->buf + i * 4));
508 if ((i + 1) % 4 == 0)
514 mlx4_mtt_cleanup(dev, &eq->mtt);
515 for (i = 0; i < npages; ++i)
516 pci_free_consistent(dev->pdev, PAGE_SIZE,
517 eq->page_list[i].buf,
518 eq->page_list[i].map);
520 kfree(eq->page_list);
521 mlx4_bitmap_free(&priv->eq_table.bitmap, eq->eqn);
522 mlx4_free_cmd_mailbox(dev, mailbox);
525 static void mlx4_free_irqs(struct mlx4_dev *dev)
527 struct mlx4_eq_table *eq_table = &mlx4_priv(dev)->eq_table;
529 if (eq_table->have_irq)
530 free_irq(dev->pdev->int_obj);
532 #ifdef CONFIG_PCI_MSI
535 for (i = 0; i < MLX4_NUM_EQ; ++i)
536 if (eq_table->eq[i].have_irq)
537 free_irq(eq_table->eq[i].irq, eq_table->eq + i);
542 static int mlx4_map_clr_int(struct mlx4_dev *dev)
544 struct mlx4_priv *priv = mlx4_priv(dev);
546 priv->clr_base = ioremap(pci_resource_start(dev->pdev, priv->fw.clr_int_bar) +
547 priv->fw.clr_int_base, MLX4_CLR_INT_SIZE);
548 if (!priv->clr_base) {
549 mlx4_err(dev, "Couldn't map interrupt clear register, aborting.\n");
556 static void mlx4_unmap_clr_int(struct mlx4_dev *dev)
558 struct mlx4_priv *priv = mlx4_priv(dev);
560 iounmap(priv->clr_base, MLX4_CLR_INT_SIZE);
563 int mlx4_map_eq_icm(struct mlx4_dev *dev, u64 icm_virt)
565 struct mlx4_priv *priv = mlx4_priv(dev);
569 * We assume that mapping one page is enough for the whole EQ
570 * context table. This is fine with all current HCAs, because
571 * we only use 32 EQs and each EQ uses 64 bytes of context
572 * memory, or 1 KB total.
574 priv->eq_table.icm_virt = icm_virt;
575 priv->eq_table.icm_page = alloc_page(dev->pdev, GFP_HIGHUSER);
576 if (!priv->eq_table.icm_page.da)
578 priv->eq_table.icm_dma = pci_map_page(dev->pdev, priv->eq_table.icm_page, 0,
579 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
580 if (pci_dma_mapping_error(priv->eq_table.icm_dma)) {
581 __free_page(dev->pdev, priv->eq_table.icm_page);
585 ret = mlx4_MAP_ICM_page(dev, priv->eq_table.icm_dma.da, icm_virt);
587 pci_unmap_page(dev->pdev, priv->eq_table.icm_dma, PAGE_SIZE,
588 PCI_DMA_BIDIRECTIONAL);
589 __free_page(dev->pdev, priv->eq_table.icm_page);
591 mlx4_dbg(dev,"mlx4_MAP_ICM_page: dma %#I64x, icm_virt %#I64x\n", priv->eq_table.icm_dma.da, icm_virt);
596 void mlx4_unmap_eq_icm(struct mlx4_dev *dev)
598 struct mlx4_priv *priv = mlx4_priv(dev);
600 mlx4_UNMAP_ICM(dev, priv->eq_table.icm_virt, 1);
601 pci_unmap_page(dev->pdev, priv->eq_table.icm_dma, PAGE_SIZE,
602 PCI_DMA_BIDIRECTIONAL);
603 __free_page(dev->pdev, priv->eq_table.icm_page);
606 int mlx4_init_eq_table(struct mlx4_dev *dev)
608 struct mlx4_priv *priv = mlx4_priv(dev);
612 err = mlx4_bitmap_init(&priv->eq_table.bitmap, dev->caps.num_eqs,
613 dev->caps.num_eqs - 1, dev->caps.reserved_eqs);
617 for (i = 0; i < ARRAY_SIZE(priv->eq_table.uar_map); ++i)
618 priv->eq_table.uar_map[i] = NULL;
620 err = mlx4_map_clr_int(dev);
624 priv->eq_table.clr_mask =
625 swab32(1 << (priv->eq_table.inta_pin & 31));
626 priv->eq_table.clr_int = priv->clr_base +
627 (priv->eq_table.inta_pin < 32 ? 4 : 0);
629 err = mlx4_create_eq(dev, dev->caps.num_cqs + MLX4_NUM_SPARE_EQE,
630 (dev->flags & MLX4_FLAG_MSI_X) ? MLX4_EQ_COMP : 0,
631 &priv->eq_table.eq[MLX4_EQ_COMP]);
635 err = mlx4_create_eq(dev, MLX4_NUM_ASYNC_EQE + MLX4_NUM_SPARE_EQE,
636 (dev->flags & MLX4_FLAG_MSI_X) ? MLX4_EQ_ASYNC : 0,
637 &priv->eq_table.eq[MLX4_EQ_ASYNC]);
641 #ifdef CONFIG_PCI_MSI
642 if (dev->flags & MLX4_FLAG_MSI_X) {
643 static const char *eq_name[] = {
644 [MLX4_EQ_COMP] = DRV_NAME " (comp)",
645 [MLX4_EQ_ASYNC] = DRV_NAME " (async)"
648 for (i = 0; i < MLX4_NUM_EQ; ++i) {
649 err = request_irq(priv->eq_table.eq[i].irq,
650 mlx4_msi_x_interrupt,
651 priv->eq_table.eq + i, eq_name[i], );
655 priv->eq_table.eq[i].have_irq = 1;
661 #ifdef USE_WDM_INTERRUPTS
662 err = request_irq( dev,
663 dev->pdev->int_info.u.Interrupt.Vector,
665 mlx4_dpc, &priv->eq_table.eq[0],
666 &dev->pdev->int_obj );
670 dev->pdev->dpc_lock = 0;
671 for (i = 0; i < MLX4_NUM_EQ; ++i) {
672 struct mlx4_eq * eq = &priv->eq_table.eq[i];
673 spin_lock_init( &eq->lock );
674 eq->isr = mlx4_interrupt;
676 KeInitializeDpc( &eq->dpc, mlx4_dpc, eq);
680 priv->eq_table.have_irq = 1;
683 err = mlx4_MAP_EQ(dev, MLX4_ASYNC_EVENT_MASK, 0,
684 priv->eq_table.eq[MLX4_EQ_ASYNC].eqn);
686 mlx4_warn(dev, "MAP_EQ for async EQ %d failed (%d)\n",
687 priv->eq_table.eq[MLX4_EQ_ASYNC].eqn, err);
689 for (i = 0; i < MLX4_NUM_EQ; ++i)
690 eq_set_ci(&priv->eq_table.eq[i], 1);
694 #ifdef USE_WDM_INTERRUPTS
696 mlx4_free_eq(dev, &priv->eq_table.eq[MLX4_EQ_ASYNC]);
700 mlx4_free_eq(dev, &priv->eq_table.eq[MLX4_EQ_COMP]);
703 mlx4_unmap_clr_int(dev);
707 mlx4_bitmap_cleanup(&priv->eq_table.bitmap);
711 void mlx4_cleanup_eq_table(struct mlx4_dev *dev)
713 struct mlx4_priv *priv = mlx4_priv(dev);
716 mlx4_MAP_EQ(dev, MLX4_ASYNC_EVENT_MASK, 1,
717 priv->eq_table.eq[MLX4_EQ_ASYNC].eqn);
721 for (i = 0; i < MLX4_NUM_EQ; ++i)
722 mlx4_free_eq(dev, &priv->eq_table.eq[i]);
724 mlx4_unmap_clr_int(dev);
726 for (i = 0; i < ARRAY_SIZE(priv->eq_table.uar_map); ++i)
727 if (priv->eq_table.uar_map[i])
728 iounmap(priv->eq_table.uar_map[i],PAGE_SIZE);
730 mlx4_bitmap_cleanup(&priv->eq_table.bitmap);