2 * Copyright (c) 2005, 2006, 2007, 2008 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
34 #include <linux/init.h>
35 #include <linux/interrupt.h>
37 #include <linux/dma-mapping.h>
39 #include <linux/mlx4/cmd.h>
45 MLX4_NUM_ASYNC_EQE = 0x100,
46 MLX4_NUM_SPARE_EQE = 0x80,
47 MLX4_EQ_ENTRY_SIZE = 0x20
51 * Must be packed because start is 64 bits but only aligned to 32 bits.
53 struct mlx4_eq_context {
67 __be32 mtt_base_addr_l;
69 __be32 consumer_index;
70 __be32 producer_index;
74 #define MLX4_EQ_STATUS_OK ( 0 << 28)
75 #define MLX4_EQ_STATUS_WRITE_FAIL (10 << 28)
76 #define MLX4_EQ_OWNER_SW ( 0 << 24)
77 #define MLX4_EQ_OWNER_HW ( 1 << 24)
78 #define MLX4_EQ_FLAG_EC ( 1 << 18)
79 #define MLX4_EQ_FLAG_OI ( 1 << 17)
80 #define MLX4_EQ_STATE_ARMED ( 9 << 8)
81 #define MLX4_EQ_STATE_FIRED (10 << 8)
82 #define MLX4_EQ_STATE_ALWAYS_ARMED (11 << 8)
84 #define MLX4_ASYNC_EVENT_MASK ((1ull << MLX4_EVENT_TYPE_PATH_MIG) | \
85 (1ull << MLX4_EVENT_TYPE_COMM_EST) | \
86 (1ull << MLX4_EVENT_TYPE_SQ_DRAINED) | \
87 (1ull << MLX4_EVENT_TYPE_CQ_ERROR) | \
88 (1ull << MLX4_EVENT_TYPE_WQ_CATAS_ERROR) | \
89 (1ull << MLX4_EVENT_TYPE_EEC_CATAS_ERROR) | \
90 (1ull << MLX4_EVENT_TYPE_PATH_MIG_FAILED) | \
91 (1ull << MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR) | \
92 (1ull << MLX4_EVENT_TYPE_WQ_ACCESS_ERROR) | \
93 (1ull << MLX4_EVENT_TYPE_PORT_CHANGE) | \
94 (1ull << MLX4_EVENT_TYPE_ECC_DETECT) | \
95 (1ull << MLX4_EVENT_TYPE_SRQ_CATAS_ERROR) | \
96 (1ull << MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE) | \
97 (1ull << MLX4_EVENT_TYPE_SRQ_LIMIT) | \
98 (1ull << MLX4_EVENT_TYPE_CMD))
109 } __attribute__((packed)) comp;
117 } __attribute__((packed)) cmd;
120 } __attribute__((packed)) qp;
123 } __attribute__((packed)) srq;
129 } __attribute__((packed)) cq_err;
133 } __attribute__((packed)) port_change;
137 } __attribute__((packed));
139 static void eq_set_ci(struct mlx4_eq *eq, int req_not)
141 __raw_writel((__force u32) cpu_to_be32((eq->cons_index & 0xffffff) |
144 /* We still want ordering, just not swabbing, so add a barrier */
148 static struct mlx4_eqe *get_eqe(struct mlx4_eq *eq, u32 entry)
150 unsigned long off = (entry & (eq->nent - 1)) * MLX4_EQ_ENTRY_SIZE;
151 return eq->page_list[off / PAGE_SIZE].buf + off % PAGE_SIZE;
154 static struct mlx4_eqe *next_eqe_sw(struct mlx4_eq *eq)
156 struct mlx4_eqe *eqe = get_eqe(eq, eq->cons_index);
157 return !!(eqe->owner & 0x80) ^ !!(eq->cons_index & eq->nent) ? NULL : eqe;
160 static int mlx4_eq_int(struct mlx4_dev *dev, struct mlx4_eq *eq)
162 struct mlx4_eqe *eqe;
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,
207 eqe->subtype == MLX4_PORT_CHANGE_SUBTYPE_ACTIVE ?
208 MLX4_DEV_EVENT_PORT_UP :
209 MLX4_DEV_EVENT_PORT_DOWN,
210 be32_to_cpu(eqe->event.port_change.port) >> 28);
213 case MLX4_EVENT_TYPE_CQ_ERROR:
214 mlx4_warn(dev, "CQ %s on CQN %06x\n",
215 eqe->event.cq_err.syndrome == 1 ?
216 "overrun" : "access violation",
217 be32_to_cpu(eqe->event.cq_err.cqn) & 0xffffff);
218 mlx4_cq_event(dev, be32_to_cpu(eqe->event.cq_err.cqn),
222 case MLX4_EVENT_TYPE_EQ_OVERFLOW:
223 mlx4_warn(dev, "EQ overrun on EQN %d\n", eq->eqn);
226 case MLX4_EVENT_TYPE_EEC_CATAS_ERROR:
227 case MLX4_EVENT_TYPE_ECC_DETECT:
229 mlx4_warn(dev, "Unhandled event %02x(%02x) on EQ %d at index %u\n",
230 eqe->type, eqe->subtype, eq->eqn, eq->cons_index);
239 * The HCA will think the queue has overflowed if we
240 * don't tell it we've been processing events. We
241 * create our EQs with MLX4_NUM_SPARE_EQE extra
242 * entries, so we must update our consumer index at
245 if (unlikely(set_ci >= MLX4_NUM_SPARE_EQE)) {
247 * Conditional on hca_type is OK here because
248 * this is a rare case, not the fast path.
260 static irqreturn_t mlx4_interrupt(int irq, void *dev_ptr)
262 struct mlx4_dev *dev = dev_ptr;
263 struct mlx4_priv *priv = mlx4_priv(dev);
267 writel(priv->eq_table.clr_mask, priv->eq_table.clr_int);
269 for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i)
270 work |= mlx4_eq_int(dev, &priv->eq_table.eq[i]);
272 return IRQ_RETVAL(work);
275 static irqreturn_t mlx4_msi_x_interrupt(int irq, void *eq_ptr)
277 struct mlx4_eq *eq = eq_ptr;
278 struct mlx4_dev *dev = eq->dev;
280 mlx4_eq_int(dev, eq);
282 /* MSI-X vectors always belong to us */
286 static int mlx4_MAP_EQ(struct mlx4_dev *dev, u64 event_mask, int unmap,
289 return mlx4_cmd(dev, event_mask, (unmap << 31) | eq_num,
290 0, MLX4_CMD_MAP_EQ, MLX4_CMD_TIME_CLASS_B);
293 static int mlx4_SW2HW_EQ(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
296 return mlx4_cmd(dev, mailbox->dma, eq_num, 0, MLX4_CMD_SW2HW_EQ,
297 MLX4_CMD_TIME_CLASS_A);
300 static int mlx4_HW2SW_EQ(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
303 return mlx4_cmd_box(dev, 0, mailbox->dma, eq_num, 0, MLX4_CMD_HW2SW_EQ,
304 MLX4_CMD_TIME_CLASS_A);
307 static int mlx4_num_eq_uar(struct mlx4_dev *dev)
310 * Each UAR holds 4 EQ doorbells. To figure out how many UARs
311 * we need to map, take the difference of highest index and
312 * the lowest index we'll use and add 1.
314 return (dev->caps.num_comp_vectors + 1 + dev->caps.reserved_eqs) / 4 -
315 dev->caps.reserved_eqs / 4 + 1;
318 static void __iomem *mlx4_get_eq_uar(struct mlx4_dev *dev, struct mlx4_eq *eq)
320 struct mlx4_priv *priv = mlx4_priv(dev);
323 index = eq->eqn / 4 - dev->caps.reserved_eqs / 4;
325 if (!priv->eq_table.uar_map[index]) {
326 priv->eq_table.uar_map[index] =
327 ioremap(pci_resource_start(dev->pdev, 2) +
328 ((eq->eqn / 4) << PAGE_SHIFT),
330 if (!priv->eq_table.uar_map[index]) {
331 mlx4_err(dev, "Couldn't map EQ doorbell for EQN 0x%06x\n",
337 return priv->eq_table.uar_map[index] + 0x800 + 8 * (eq->eqn % 4);
340 static int mlx4_create_eq(struct mlx4_dev *dev, int nent,
341 u8 intr, struct mlx4_eq *eq)
343 struct mlx4_priv *priv = mlx4_priv(dev);
344 struct mlx4_cmd_mailbox *mailbox;
345 struct mlx4_eq_context *eq_context;
347 u64 *dma_list = NULL;
354 eq->nent = roundup_pow_of_two(max(nent, 2));
355 npages = PAGE_ALIGN(eq->nent * MLX4_EQ_ENTRY_SIZE) / PAGE_SIZE;
357 eq->page_list = kmalloc(npages * sizeof *eq->page_list,
362 for (i = 0; i < npages; ++i)
363 eq->page_list[i].buf = NULL;
365 dma_list = kmalloc(npages * sizeof *dma_list, GFP_KERNEL);
369 mailbox = mlx4_alloc_cmd_mailbox(dev);
372 eq_context = mailbox->buf;
374 for (i = 0; i < npages; ++i) {
375 eq->page_list[i].buf = dma_alloc_coherent(&dev->pdev->dev,
376 PAGE_SIZE, &t, GFP_KERNEL);
377 if (!eq->page_list[i].buf)
378 goto err_out_free_pages;
381 eq->page_list[i].map = t;
383 memset(eq->page_list[i].buf, 0, PAGE_SIZE);
386 eq->eqn = mlx4_bitmap_alloc(&priv->eq_table.bitmap);
388 goto err_out_free_pages;
390 eq->doorbell = mlx4_get_eq_uar(dev, eq);
393 goto err_out_free_eq;
396 err = mlx4_mtt_init(dev, npages, PAGE_SHIFT, &eq->mtt);
398 goto err_out_free_eq;
400 err = mlx4_write_mtt(dev, &eq->mtt, 0, npages, dma_list);
402 goto err_out_free_mtt;
404 memset(eq_context, 0, sizeof *eq_context);
405 eq_context->flags = cpu_to_be32(MLX4_EQ_STATUS_OK |
406 MLX4_EQ_STATE_ARMED);
407 eq_context->log_eq_size = ilog2(eq->nent);
408 eq_context->intr = intr;
409 eq_context->log_page_size = PAGE_SHIFT - MLX4_ICM_PAGE_SHIFT;
411 mtt_addr = mlx4_mtt_addr(dev, &eq->mtt);
412 eq_context->mtt_base_addr_h = mtt_addr >> 32;
413 eq_context->mtt_base_addr_l = cpu_to_be32(mtt_addr & 0xffffffff);
415 err = mlx4_SW2HW_EQ(dev, mailbox, eq->eqn);
417 mlx4_warn(dev, "SW2HW_EQ failed (%d)\n", err);
418 goto err_out_free_mtt;
422 mlx4_free_cmd_mailbox(dev, mailbox);
429 mlx4_mtt_cleanup(dev, &eq->mtt);
432 mlx4_bitmap_free(&priv->eq_table.bitmap, eq->eqn);
435 for (i = 0; i < npages; ++i)
436 if (eq->page_list[i].buf)
437 dma_free_coherent(&dev->pdev->dev, PAGE_SIZE,
438 eq->page_list[i].buf,
439 eq->page_list[i].map);
441 mlx4_free_cmd_mailbox(dev, mailbox);
444 kfree(eq->page_list);
451 static void mlx4_free_eq(struct mlx4_dev *dev,
454 struct mlx4_priv *priv = mlx4_priv(dev);
455 struct mlx4_cmd_mailbox *mailbox;
457 int npages = PAGE_ALIGN(MLX4_EQ_ENTRY_SIZE * eq->nent) / PAGE_SIZE;
460 mailbox = mlx4_alloc_cmd_mailbox(dev);
464 err = mlx4_HW2SW_EQ(dev, mailbox, eq->eqn);
466 mlx4_warn(dev, "HW2SW_EQ failed (%d)\n", err);
469 mlx4_dbg(dev, "Dumping EQ context %02x:\n", eq->eqn);
470 for (i = 0; i < sizeof (struct mlx4_eq_context) / 4; ++i) {
472 printk("[%02x] ", i * 4);
473 printk(" %08x", be32_to_cpup(mailbox->buf + i * 4));
474 if ((i + 1) % 4 == 0)
479 mlx4_mtt_cleanup(dev, &eq->mtt);
480 for (i = 0; i < npages; ++i)
481 pci_free_consistent(dev->pdev, PAGE_SIZE,
482 eq->page_list[i].buf,
483 eq->page_list[i].map);
485 kfree(eq->page_list);
486 mlx4_bitmap_free(&priv->eq_table.bitmap, eq->eqn);
487 mlx4_free_cmd_mailbox(dev, mailbox);
490 static void mlx4_free_irqs(struct mlx4_dev *dev)
492 struct mlx4_eq_table *eq_table = &mlx4_priv(dev)->eq_table;
495 if (eq_table->have_irq)
496 free_irq(dev->pdev->irq, dev);
497 for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i)
498 if (eq_table->eq[i].have_irq)
499 free_irq(eq_table->eq[i].irq, eq_table->eq + i);
501 kfree(eq_table->irq_names);
504 static int mlx4_map_clr_int(struct mlx4_dev *dev)
506 struct mlx4_priv *priv = mlx4_priv(dev);
508 priv->clr_base = ioremap(pci_resource_start(dev->pdev, priv->fw.clr_int_bar) +
509 priv->fw.clr_int_base, MLX4_CLR_INT_SIZE);
510 if (!priv->clr_base) {
511 mlx4_err(dev, "Couldn't map interrupt clear register, aborting.\n");
518 static void mlx4_unmap_clr_int(struct mlx4_dev *dev)
520 struct mlx4_priv *priv = mlx4_priv(dev);
522 iounmap(priv->clr_base);
525 int mlx4_map_eq_icm(struct mlx4_dev *dev, u64 icm_virt)
527 struct mlx4_priv *priv = mlx4_priv(dev);
531 * We assume that mapping one page is enough for the whole EQ
532 * context table. This is fine with all current HCAs, because
533 * we only use 32 EQs and each EQ uses 64 bytes of context
534 * memory, or 1 KB total.
536 priv->eq_table.icm_virt = icm_virt;
537 priv->eq_table.icm_page = alloc_page(GFP_HIGHUSER);
538 if (!priv->eq_table.icm_page)
540 priv->eq_table.icm_dma = pci_map_page(dev->pdev, priv->eq_table.icm_page, 0,
541 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
542 if (pci_dma_mapping_error(dev->pdev, priv->eq_table.icm_dma)) {
543 __free_page(priv->eq_table.icm_page);
547 ret = mlx4_MAP_ICM_page(dev, priv->eq_table.icm_dma, icm_virt);
549 pci_unmap_page(dev->pdev, priv->eq_table.icm_dma, PAGE_SIZE,
550 PCI_DMA_BIDIRECTIONAL);
551 __free_page(priv->eq_table.icm_page);
557 void mlx4_unmap_eq_icm(struct mlx4_dev *dev)
559 struct mlx4_priv *priv = mlx4_priv(dev);
561 mlx4_UNMAP_ICM(dev, priv->eq_table.icm_virt, 1);
562 pci_unmap_page(dev->pdev, priv->eq_table.icm_dma, PAGE_SIZE,
563 PCI_DMA_BIDIRECTIONAL);
564 __free_page(priv->eq_table.icm_page);
567 int mlx4_alloc_eq_table(struct mlx4_dev *dev)
569 struct mlx4_priv *priv = mlx4_priv(dev);
571 priv->eq_table.eq = kcalloc(dev->caps.num_eqs - dev->caps.reserved_eqs,
572 sizeof *priv->eq_table.eq, GFP_KERNEL);
573 if (!priv->eq_table.eq)
579 void mlx4_free_eq_table(struct mlx4_dev *dev)
581 kfree(mlx4_priv(dev)->eq_table.eq);
584 int mlx4_init_eq_table(struct mlx4_dev *dev)
586 struct mlx4_priv *priv = mlx4_priv(dev);
590 priv->eq_table.uar_map = kcalloc(sizeof *priv->eq_table.uar_map,
591 mlx4_num_eq_uar(dev), GFP_KERNEL);
592 if (!priv->eq_table.uar_map) {
597 err = mlx4_bitmap_init(&priv->eq_table.bitmap, dev->caps.num_eqs,
598 dev->caps.num_eqs - 1, dev->caps.reserved_eqs, 0);
602 for (i = 0; i < mlx4_num_eq_uar(dev); ++i)
603 priv->eq_table.uar_map[i] = NULL;
605 err = mlx4_map_clr_int(dev);
609 priv->eq_table.clr_mask =
610 swab32(1 << (priv->eq_table.inta_pin & 31));
611 priv->eq_table.clr_int = priv->clr_base +
612 (priv->eq_table.inta_pin < 32 ? 4 : 0);
614 priv->eq_table.irq_names = kmalloc(16 * dev->caps.num_comp_vectors, GFP_KERNEL);
615 if (!priv->eq_table.irq_names) {
620 for (i = 0; i < dev->caps.num_comp_vectors; ++i) {
621 err = mlx4_create_eq(dev, dev->caps.num_cqs + MLX4_NUM_SPARE_EQE,
622 (dev->flags & MLX4_FLAG_MSI_X) ? i : 0,
623 &priv->eq_table.eq[i]);
628 err = mlx4_create_eq(dev, MLX4_NUM_ASYNC_EQE + MLX4_NUM_SPARE_EQE,
629 (dev->flags & MLX4_FLAG_MSI_X) ? dev->caps.num_comp_vectors : 0,
630 &priv->eq_table.eq[dev->caps.num_comp_vectors]);
634 if (dev->flags & MLX4_FLAG_MSI_X) {
635 static const char async_eq_name[] = "mlx4-async";
638 for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i) {
639 if (i < dev->caps.num_comp_vectors) {
640 snprintf(priv->eq_table.irq_names + i * 16, 16,
642 eq_name = priv->eq_table.irq_names + i * 16;
644 eq_name = async_eq_name;
646 err = request_irq(priv->eq_table.eq[i].irq,
647 mlx4_msi_x_interrupt, 0, eq_name,
648 priv->eq_table.eq + i);
652 priv->eq_table.eq[i].have_irq = 1;
655 err = request_irq(dev->pdev->irq, mlx4_interrupt,
656 IRQF_SHARED, DRV_NAME, dev);
660 priv->eq_table.have_irq = 1;
663 err = mlx4_MAP_EQ(dev, MLX4_ASYNC_EVENT_MASK, 0,
664 priv->eq_table.eq[dev->caps.num_comp_vectors].eqn);
666 mlx4_warn(dev, "MAP_EQ for async EQ %d failed (%d)\n",
667 priv->eq_table.eq[dev->caps.num_comp_vectors].eqn, err);
669 for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i)
670 eq_set_ci(&priv->eq_table.eq[i], 1);
675 mlx4_free_eq(dev, &priv->eq_table.eq[dev->caps.num_comp_vectors]);
678 i = dev->caps.num_comp_vectors - 1;
682 mlx4_free_eq(dev, &priv->eq_table.eq[i]);
685 mlx4_unmap_clr_int(dev);
689 mlx4_bitmap_cleanup(&priv->eq_table.bitmap);
692 kfree(priv->eq_table.uar_map);
697 void mlx4_cleanup_eq_table(struct mlx4_dev *dev)
699 struct mlx4_priv *priv = mlx4_priv(dev);
702 mlx4_MAP_EQ(dev, MLX4_ASYNC_EVENT_MASK, 1,
703 priv->eq_table.eq[dev->caps.num_comp_vectors].eqn);
707 for (i = 0; i < dev->caps.num_comp_vectors + 1; ++i)
708 mlx4_free_eq(dev, &priv->eq_table.eq[i]);
710 mlx4_unmap_clr_int(dev);
712 for (i = 0; i < mlx4_num_eq_uar(dev); ++i)
713 if (priv->eq_table.uar_map[i])
714 iounmap(priv->eq_table.uar_map[i]);
716 mlx4_bitmap_cleanup(&priv->eq_table.bitmap);
718 kfree(priv->eq_table.uar_map);