2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 * $Id: mthca_main.c 1396 2004-12-28 04:10:27Z roland $
37 #include <linux/config.h>
38 #include <linux/version.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/errno.h>
42 #include <linux/pci.h>
43 #include <linux/interrupt.h>
45 #include "mthca_dev.h"
46 #include "mthca_config_reg.h"
47 #include "mthca_cmd.h"
48 #include "mthca_profile.h"
49 #include "mthca_memfree.h"
51 MODULE_AUTHOR("Roland Dreier");
52 MODULE_DESCRIPTION("Mellanox InfiniBand HCA low-level driver");
53 MODULE_LICENSE("Dual BSD/GPL");
54 MODULE_VERSION(DRV_VERSION);
59 module_param(msi_x, int, 0444);
60 MODULE_PARM_DESC(msi_x, "attempt to use MSI-X if nonzero");
63 module_param(msi, int, 0444);
64 MODULE_PARM_DESC(msi, "attempt to use MSI if nonzero");
66 #else /* CONFIG_PCI_MSI */
71 #endif /* CONFIG_PCI_MSI */
73 static const char mthca_version[] __devinitdata =
74 DRV_NAME ": Mellanox InfiniBand HCA driver v"
75 DRV_VERSION " (" DRV_RELDATE ")\n";
77 static struct mthca_profile default_profile = {
84 .num_udav = 1 << 15, /* Tavor only */
85 .fmr_reserved_mtts = 1 << 18, /* Tavor only */
86 .uarc_size = 1 << 18, /* Arbel only */
89 static int __devinit mthca_tune_pci(struct mthca_dev *mdev)
94 /* First try to max out Read Byte Count */
95 cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_PCIX);
97 if (pci_read_config_word(mdev->pdev, cap + PCI_X_CMD, &val)) {
98 mthca_err(mdev, "Couldn't read PCI-X command register, "
102 val = (val & ~PCI_X_CMD_MAX_READ) | (3 << 2);
103 if (pci_write_config_word(mdev->pdev, cap + PCI_X_CMD, val)) {
104 mthca_err(mdev, "Couldn't write PCI-X command register, "
108 } else if (!(mdev->mthca_flags & MTHCA_FLAG_PCIE))
109 mthca_info(mdev, "No PCI-X capability, not setting RBC.\n");
111 cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_EXP);
113 if (pci_read_config_word(mdev->pdev, cap + PCI_EXP_DEVCTL, &val)) {
114 mthca_err(mdev, "Couldn't read PCI Express device control "
115 "register, aborting.\n");
118 val = (val & ~PCI_EXP_DEVCTL_READRQ) | (5 << 12);
119 if (pci_write_config_word(mdev->pdev, cap + PCI_EXP_DEVCTL, val)) {
120 mthca_err(mdev, "Couldn't write PCI Express device control "
121 "register, aborting.\n");
124 } else if (mdev->mthca_flags & MTHCA_FLAG_PCIE)
125 mthca_info(mdev, "No PCI Express capability, "
126 "not setting Max Read Request Size.\n");
131 static int __devinit mthca_dev_lim(struct mthca_dev *mdev, struct mthca_dev_lim *dev_lim)
136 err = mthca_QUERY_DEV_LIM(mdev, dev_lim, &status);
138 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
142 mthca_err(mdev, "QUERY_DEV_LIM returned status 0x%02x, "
143 "aborting.\n", status);
146 if (dev_lim->min_page_sz > PAGE_SIZE) {
147 mthca_err(mdev, "HCA minimum page size of %d bigger than "
148 "kernel PAGE_SIZE of %ld, aborting.\n",
149 dev_lim->min_page_sz, PAGE_SIZE);
152 if (dev_lim->num_ports > MTHCA_MAX_PORTS) {
153 mthca_err(mdev, "HCA has %d ports, but we only support %d, "
155 dev_lim->num_ports, MTHCA_MAX_PORTS);
159 mdev->limits.num_ports = dev_lim->num_ports;
160 mdev->limits.vl_cap = dev_lim->max_vl;
161 mdev->limits.mtu_cap = dev_lim->max_mtu;
162 mdev->limits.gid_table_len = dev_lim->max_gids;
163 mdev->limits.pkey_table_len = dev_lim->max_pkeys;
164 mdev->limits.local_ca_ack_delay = dev_lim->local_ca_ack_delay;
165 mdev->limits.max_sg = dev_lim->max_sg;
166 mdev->limits.reserved_qps = dev_lim->reserved_qps;
167 mdev->limits.reserved_srqs = dev_lim->reserved_srqs;
168 mdev->limits.reserved_eecs = dev_lim->reserved_eecs;
169 mdev->limits.reserved_cqs = dev_lim->reserved_cqs;
170 mdev->limits.reserved_eqs = dev_lim->reserved_eqs;
171 mdev->limits.reserved_mtts = dev_lim->reserved_mtts;
172 mdev->limits.reserved_mrws = dev_lim->reserved_mrws;
173 mdev->limits.reserved_uars = dev_lim->reserved_uars;
174 mdev->limits.reserved_pds = dev_lim->reserved_pds;
176 /* IB_DEVICE_RESIZE_MAX_WR not supported by driver.
177 May be doable since hardware supports it for SRQ.
179 IB_DEVICE_N_NOTIFY_CQ is supported by hardware but not by driver.
181 IB_DEVICE_SRQ_RESIZE is supported by hardware but SRQ is not
182 supported by driver. */
183 mdev->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
184 IB_DEVICE_PORT_ACTIVE_EVENT |
185 IB_DEVICE_SYS_IMAGE_GUID |
186 IB_DEVICE_RC_RNR_NAK_GEN;
188 if (dev_lim->flags & DEV_LIM_FLAG_BAD_PKEY_CNTR)
189 mdev->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
191 if (dev_lim->flags & DEV_LIM_FLAG_BAD_QKEY_CNTR)
192 mdev->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
194 if (dev_lim->flags & DEV_LIM_FLAG_RAW_MULTI)
195 mdev->device_cap_flags |= IB_DEVICE_RAW_MULTI;
197 if (dev_lim->flags & DEV_LIM_FLAG_AUTO_PATH_MIG)
198 mdev->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
200 if (dev_lim->flags & DEV_LIM_FLAG_UD_AV_PORT_ENFORCE)
201 mdev->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
203 if (dev_lim->flags & DEV_LIM_FLAG_SRQ)
204 mdev->mthca_flags |= MTHCA_FLAG_SRQ;
209 static int __devinit mthca_init_tavor(struct mthca_dev *mdev)
213 struct mthca_dev_lim dev_lim;
214 struct mthca_profile profile;
215 struct mthca_init_hca_param init_hca;
216 struct mthca_adapter adapter;
218 err = mthca_SYS_EN(mdev, &status);
220 mthca_err(mdev, "SYS_EN command failed, aborting.\n");
224 mthca_err(mdev, "SYS_EN returned status 0x%02x, "
225 "aborting.\n", status);
229 err = mthca_QUERY_FW(mdev, &status);
231 mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
235 mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
236 "aborting.\n", status);
240 err = mthca_QUERY_DDR(mdev, &status);
242 mthca_err(mdev, "QUERY_DDR command failed, aborting.\n");
246 mthca_err(mdev, "QUERY_DDR returned status 0x%02x, "
247 "aborting.\n", status);
252 err = mthca_dev_lim(mdev, &dev_lim);
254 profile = default_profile;
255 profile.num_uar = dev_lim.uar_size / PAGE_SIZE;
256 profile.uarc_size = 0;
258 err = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
262 err = mthca_INIT_HCA(mdev, &init_hca, &status);
264 mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
268 mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
269 "aborting.\n", status);
274 err = mthca_QUERY_ADAPTER(mdev, &adapter, &status);
276 mthca_err(mdev, "QUERY_ADAPTER command failed, aborting.\n");
280 mthca_err(mdev, "QUERY_ADAPTER returned status 0x%02x, "
281 "aborting.\n", status);
286 mdev->eq_table.inta_pin = adapter.inta_pin;
287 mdev->rev_id = adapter.revision_id;
292 mthca_CLOSE_HCA(mdev, 0, &status);
295 mthca_SYS_DIS(mdev, &status);
300 static int __devinit mthca_load_fw(struct mthca_dev *mdev)
305 /* FIXME: use HCA-attached memory for FW if present */
307 mdev->fw.arbel.fw_icm =
308 mthca_alloc_icm(mdev, mdev->fw.arbel.fw_pages,
309 GFP_HIGHUSER | __GFP_NOWARN);
310 if (!mdev->fw.arbel.fw_icm) {
311 mthca_err(mdev, "Couldn't allocate FW area, aborting.\n");
315 err = mthca_MAP_FA(mdev, mdev->fw.arbel.fw_icm, &status);
317 mthca_err(mdev, "MAP_FA command failed, aborting.\n");
321 mthca_err(mdev, "MAP_FA returned status 0x%02x, aborting.\n", status);
325 err = mthca_RUN_FW(mdev, &status);
327 mthca_err(mdev, "RUN_FW command failed, aborting.\n");
331 mthca_err(mdev, "RUN_FW returned status 0x%02x, aborting.\n", status);
339 mthca_UNMAP_FA(mdev, &status);
342 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
346 static int __devinit mthca_init_icm(struct mthca_dev *mdev,
347 struct mthca_dev_lim *dev_lim,
348 struct mthca_init_hca_param *init_hca,
355 err = mthca_SET_ICM_SIZE(mdev, icm_size, &aux_pages, &status);
357 mthca_err(mdev, "SET_ICM_SIZE command failed, aborting.\n");
361 mthca_err(mdev, "SET_ICM_SIZE returned status 0x%02x, "
362 "aborting.\n", status);
366 mthca_dbg(mdev, "%lld KB of HCA context requires %lld KB aux memory.\n",
367 (unsigned long long) icm_size >> 10,
368 (unsigned long long) aux_pages << 2);
370 mdev->fw.arbel.aux_icm = mthca_alloc_icm(mdev, aux_pages,
371 GFP_HIGHUSER | __GFP_NOWARN);
372 if (!mdev->fw.arbel.aux_icm) {
373 mthca_err(mdev, "Couldn't allocate aux memory, aborting.\n");
377 err = mthca_MAP_ICM_AUX(mdev, mdev->fw.arbel.aux_icm, &status);
379 mthca_err(mdev, "MAP_ICM_AUX command failed, aborting.\n");
383 mthca_err(mdev, "MAP_ICM_AUX returned status 0x%02x, aborting.\n", status);
388 err = mthca_map_eq_icm(mdev, init_hca->eqc_base);
390 mthca_err(mdev, "Failed to map EQ context memory, aborting.\n");
394 mdev->mr_table.mtt_table = mthca_alloc_icm_table(mdev, init_hca->mtt_base,
396 mdev->limits.num_mtt_segs,
397 mdev->limits.reserved_mtts, 1);
398 if (!mdev->mr_table.mtt_table) {
399 mthca_err(mdev, "Failed to map MTT context memory, aborting.\n");
404 mdev->mr_table.mpt_table = mthca_alloc_icm_table(mdev, init_hca->mpt_base,
405 dev_lim->mpt_entry_sz,
406 mdev->limits.num_mpts,
407 mdev->limits.reserved_mrws, 1);
408 if (!mdev->mr_table.mpt_table) {
409 mthca_err(mdev, "Failed to map MPT context memory, aborting.\n");
414 mdev->qp_table.qp_table = mthca_alloc_icm_table(mdev, init_hca->qpc_base,
415 dev_lim->qpc_entry_sz,
416 mdev->limits.num_qps,
417 mdev->limits.reserved_qps, 0);
418 if (!mdev->qp_table.qp_table) {
419 mthca_err(mdev, "Failed to map QP context memory, aborting.\n");
424 mdev->qp_table.eqp_table = mthca_alloc_icm_table(mdev, init_hca->eqpc_base,
425 dev_lim->eqpc_entry_sz,
426 mdev->limits.num_qps,
427 mdev->limits.reserved_qps, 0);
428 if (!mdev->qp_table.eqp_table) {
429 mthca_err(mdev, "Failed to map EQP context memory, aborting.\n");
434 mdev->qp_table.rdb_table = mthca_alloc_icm_table(mdev, init_hca->rdb_base,
435 MTHCA_RDB_ENTRY_SIZE,
436 mdev->limits.num_qps <<
437 mdev->qp_table.rdb_shift,
439 if (!mdev->qp_table.rdb_table) {
440 mthca_err(mdev, "Failed to map RDB context memory, aborting\n");
445 mdev->cq_table.table = mthca_alloc_icm_table(mdev, init_hca->cqc_base,
446 dev_lim->cqc_entry_sz,
447 mdev->limits.num_cqs,
448 mdev->limits.reserved_cqs, 0);
449 if (!mdev->cq_table.table) {
450 mthca_err(mdev, "Failed to map CQ context memory, aborting.\n");
456 * It's not strictly required, but for simplicity just map the
457 * whole multicast group table now. The table isn't very big
458 * and it's a lot easier than trying to track ref counts.
460 mdev->mcg_table.table = mthca_alloc_icm_table(mdev, init_hca->mc_base,
461 MTHCA_MGM_ENTRY_SIZE,
462 mdev->limits.num_mgms +
463 mdev->limits.num_amgms,
464 mdev->limits.num_mgms +
465 mdev->limits.num_amgms,
467 if (!mdev->mcg_table.table) {
468 mthca_err(mdev, "Failed to map MCG context memory, aborting.\n");
476 mthca_free_icm_table(mdev, mdev->cq_table.table);
479 mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
482 mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
485 mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
488 mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
491 mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
494 mthca_unmap_eq_icm(mdev);
497 mthca_UNMAP_ICM_AUX(mdev, &status);
500 mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
505 static int __devinit mthca_init_arbel(struct mthca_dev *mdev)
507 struct mthca_dev_lim dev_lim;
508 struct mthca_profile profile;
509 struct mthca_init_hca_param init_hca;
510 struct mthca_adapter adapter;
515 err = mthca_QUERY_FW(mdev, &status);
517 mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
521 mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
522 "aborting.\n", status);
526 err = mthca_ENABLE_LAM(mdev, &status);
528 mthca_err(mdev, "ENABLE_LAM command failed, aborting.\n");
531 if (status == MTHCA_CMD_STAT_LAM_NOT_PRE) {
532 mthca_dbg(mdev, "No HCA-attached memory (running in MemFree mode)\n");
533 mdev->mthca_flags |= MTHCA_FLAG_NO_LAM;
535 mthca_err(mdev, "ENABLE_LAM returned status 0x%02x, "
536 "aborting.\n", status);
540 err = mthca_load_fw(mdev);
542 mthca_err(mdev, "Failed to start FW, aborting.\n");
546 err = mthca_dev_lim(mdev, &dev_lim);
548 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
552 profile = default_profile;
553 profile.num_uar = dev_lim.uar_size / PAGE_SIZE;
554 profile.num_udav = 0;
556 icm_size = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
557 if ((int) icm_size < 0) {
562 err = mthca_init_icm(mdev, &dev_lim, &init_hca, icm_size);
566 err = mthca_INIT_HCA(mdev, &init_hca, &status);
568 mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
572 mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
573 "aborting.\n", status);
578 err = mthca_QUERY_ADAPTER(mdev, &adapter, &status);
580 mthca_err(mdev, "QUERY_ADAPTER command failed, aborting.\n");
584 mthca_err(mdev, "QUERY_ADAPTER returned status 0x%02x, "
585 "aborting.\n", status);
590 mdev->eq_table.inta_pin = adapter.inta_pin;
591 mdev->rev_id = adapter.revision_id;
596 mthca_free_icm_table(mdev, mdev->cq_table.table);
597 mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
598 mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
599 mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
600 mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
601 mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
602 mthca_unmap_eq_icm(mdev);
604 mthca_UNMAP_ICM_AUX(mdev, &status);
605 mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
608 mthca_UNMAP_FA(mdev, &status);
609 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
612 if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
613 mthca_DISABLE_LAM(mdev, &status);
618 static int __devinit mthca_init_hca(struct mthca_dev *mdev)
620 if (mthca_is_memfree(mdev))
621 return mthca_init_arbel(mdev);
623 return mthca_init_tavor(mdev);
626 static int __devinit mthca_setup_hca(struct mthca_dev *dev)
631 MTHCA_INIT_DOORBELL_LOCK(&dev->doorbell_lock);
633 err = mthca_init_uar_table(dev);
635 mthca_err(dev, "Failed to initialize "
636 "user access region table, aborting.\n");
640 err = mthca_uar_alloc(dev, &dev->driver_uar);
642 mthca_err(dev, "Failed to allocate driver access region, "
644 goto err_uar_table_free;
647 dev->kar = ioremap(dev->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
649 mthca_err(dev, "Couldn't map kernel access region, "
655 err = mthca_init_pd_table(dev);
657 mthca_err(dev, "Failed to initialize "
658 "protection domain table, aborting.\n");
662 err = mthca_init_mr_table(dev);
664 mthca_err(dev, "Failed to initialize "
665 "memory region table, aborting.\n");
666 goto err_pd_table_free;
669 err = mthca_pd_alloc(dev, 1, &dev->driver_pd);
671 mthca_err(dev, "Failed to create driver PD, "
673 goto err_mr_table_free;
676 err = mthca_init_eq_table(dev);
678 mthca_err(dev, "Failed to initialize "
679 "event queue table, aborting.\n");
683 err = mthca_cmd_use_events(dev);
685 mthca_err(dev, "Failed to switch to event-driven "
686 "firmware commands, aborting.\n");
687 goto err_eq_table_free;
690 err = mthca_NOP(dev, &status);
692 mthca_err(dev, "NOP command failed to generate interrupt (IRQ %d), aborting.\n",
693 dev->mthca_flags & MTHCA_FLAG_MSI_X ?
694 dev->eq_table.eq[MTHCA_EQ_CMD].msi_x_vector :
696 if (dev->mthca_flags & (MTHCA_FLAG_MSI | MTHCA_FLAG_MSI_X))
697 mthca_err(dev, "Try again with MSI/MSI-X disabled.\n");
699 mthca_err(dev, "BIOS or ACPI interrupt routing problem?\n");
704 mthca_dbg(dev, "NOP command IRQ test passed\n");
706 err = mthca_init_cq_table(dev);
708 mthca_err(dev, "Failed to initialize "
709 "completion queue table, aborting.\n");
713 err = mthca_init_qp_table(dev);
715 mthca_err(dev, "Failed to initialize "
716 "queue pair table, aborting.\n");
717 goto err_cq_table_free;
720 err = mthca_init_av_table(dev);
722 mthca_err(dev, "Failed to initialize "
723 "address vector table, aborting.\n");
724 goto err_qp_table_free;
727 err = mthca_init_mcg_table(dev);
729 mthca_err(dev, "Failed to initialize "
730 "multicast group table, aborting.\n");
731 goto err_av_table_free;
737 mthca_cleanup_av_table(dev);
740 mthca_cleanup_qp_table(dev);
743 mthca_cleanup_cq_table(dev);
746 mthca_cmd_use_polling(dev);
749 mthca_cleanup_eq_table(dev);
752 mthca_pd_free(dev, &dev->driver_pd);
755 mthca_cleanup_mr_table(dev);
758 mthca_cleanup_pd_table(dev);
764 mthca_uar_free(dev, &dev->driver_uar);
767 mthca_cleanup_uar_table(dev);
771 static int __devinit mthca_request_regions(struct pci_dev *pdev,
777 * We can't just use pci_request_regions() because the MSI-X
778 * table is right in the middle of the first BAR. If we did
779 * pci_request_region and grab all of the first BAR, then
780 * setting up MSI-X would fail, since the PCI core wants to do
781 * request_mem_region on the MSI-X vector table.
783 * So just request what we need right now, and request any
784 * other regions we need when setting up EQs.
786 if (!request_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
787 MTHCA_HCR_SIZE, DRV_NAME))
790 err = pci_request_region(pdev, 2, DRV_NAME);
792 goto err_bar2_failed;
795 err = pci_request_region(pdev, 4, DRV_NAME);
797 goto err_bar4_failed;
803 pci_release_region(pdev, 2);
806 release_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
812 static void mthca_release_regions(struct pci_dev *pdev,
816 pci_release_region(pdev, 4);
818 pci_release_region(pdev, 2);
820 release_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
824 static int __devinit mthca_enable_msi_x(struct mthca_dev *mdev)
826 struct msix_entry entries[3];
829 entries[0].entry = 0;
830 entries[1].entry = 1;
831 entries[2].entry = 2;
833 err = pci_enable_msix(mdev->pdev, entries, ARRAY_SIZE(entries));
836 mthca_info(mdev, "Only %d MSI-X vectors available, "
837 "not using MSI-X\n", err);
841 mdev->eq_table.eq[MTHCA_EQ_COMP ].msi_x_vector = entries[0].vector;
842 mdev->eq_table.eq[MTHCA_EQ_ASYNC].msi_x_vector = entries[1].vector;
843 mdev->eq_table.eq[MTHCA_EQ_CMD ].msi_x_vector = entries[2].vector;
848 static void mthca_close_hca(struct mthca_dev *mdev)
852 mthca_CLOSE_HCA(mdev, 0, &status);
854 if (mthca_is_memfree(mdev)) {
855 mthca_free_icm_table(mdev, mdev->cq_table.table);
856 mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
857 mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
858 mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
859 mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
860 mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
861 mthca_unmap_eq_icm(mdev);
863 mthca_UNMAP_ICM_AUX(mdev, &status);
864 mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
866 mthca_UNMAP_FA(mdev, &status);
867 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
869 if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
870 mthca_DISABLE_LAM(mdev, &status);
872 mthca_SYS_DIS(mdev, &status);
875 /* Types of supported HCA */
878 ARBEL_COMPAT, /* MT25208 in Tavor compat mode */
879 ARBEL_NATIVE, /* MT25208 with extended features */
883 #define MTHCA_FW_VER(major, minor, subminor) \
884 (((u64) (major) << 32) | ((u64) (minor) << 16) | (u64) (subminor))
890 } mthca_hca_table[] = {
891 [TAVOR] = { .latest_fw = MTHCA_FW_VER(3, 3, 3), .is_memfree = 0, .is_pcie = 0 },
892 [ARBEL_COMPAT] = { .latest_fw = MTHCA_FW_VER(4, 7, 0), .is_memfree = 0, .is_pcie = 1 },
893 [ARBEL_NATIVE] = { .latest_fw = MTHCA_FW_VER(5, 1, 0), .is_memfree = 1, .is_pcie = 1 },
894 [SINAI] = { .latest_fw = MTHCA_FW_VER(1, 0, 1), .is_memfree = 1, .is_pcie = 1 }
897 static int __devinit mthca_init_one(struct pci_dev *pdev,
898 const struct pci_device_id *id)
900 static int mthca_version_printed = 0;
903 struct mthca_dev *mdev;
905 if (!mthca_version_printed) {
906 printk(KERN_INFO "%s", mthca_version);
907 ++mthca_version_printed;
910 printk(KERN_INFO PFX "Initializing %s (%s)\n",
911 pci_pretty_name(pdev), pci_name(pdev));
913 if (id->driver_data >= ARRAY_SIZE(mthca_hca_table)) {
914 printk(KERN_ERR PFX "%s (%s) has invalid driver data %lx\n",
915 pci_pretty_name(pdev), pci_name(pdev), id->driver_data);
919 err = pci_enable_device(pdev);
921 dev_err(&pdev->dev, "Cannot enable PCI device, "
927 * Check for BARs. We expect 0: 1MB, 2: 8MB, 4: DDR (may not
930 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
931 pci_resource_len(pdev, 0) != 1 << 20) {
932 dev_err(&pdev->dev, "Missing DCS, aborting.\n");
934 goto err_disable_pdev;
936 if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM) ||
937 pci_resource_len(pdev, 2) != 1 << 23) {
938 dev_err(&pdev->dev, "Missing UAR, aborting.\n");
940 goto err_disable_pdev;
942 if (!(pci_resource_flags(pdev, 4) & IORESOURCE_MEM))
945 err = mthca_request_regions(pdev, ddr_hidden);
947 dev_err(&pdev->dev, "Cannot obtain PCI resources, "
949 goto err_disable_pdev;
952 pci_set_master(pdev);
954 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
956 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask.\n");
957 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
959 dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting.\n");
963 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
965 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit "
966 "consistent PCI DMA mask.\n");
967 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
969 dev_err(&pdev->dev, "Can't set consistent PCI DMA mask, "
975 mdev = (struct mthca_dev *) ib_alloc_device(sizeof *mdev);
977 dev_err(&pdev->dev, "Device struct alloc failed, "
986 mdev->mthca_flags |= MTHCA_FLAG_DDR_HIDDEN;
987 if (mthca_hca_table[id->driver_data].is_memfree)
988 mdev->mthca_flags |= MTHCA_FLAG_MEMFREE;
989 if (mthca_hca_table[id->driver_data].is_pcie)
990 mdev->mthca_flags |= MTHCA_FLAG_PCIE;
993 * Now reset the HCA before we touch the PCI capabilities or
994 * attempt a firmware command, since a boot ROM may have left
995 * the HCA in an undefined state.
997 err = mthca_reset(mdev);
999 mthca_err(mdev, "Failed to reset HCA, aborting.\n");
1003 if (msi_x && !mthca_enable_msi_x(mdev))
1004 mdev->mthca_flags |= MTHCA_FLAG_MSI_X;
1005 if (msi && !(mdev->mthca_flags & MTHCA_FLAG_MSI_X) &&
1006 !pci_enable_msi(pdev))
1007 mdev->mthca_flags |= MTHCA_FLAG_MSI;
1009 if (mthca_cmd_init(mdev)) {
1010 mthca_err(mdev, "Failed to init command interface, aborting.\n");
1014 err = mthca_tune_pci(mdev);
1018 err = mthca_init_hca(mdev);
1022 if (mdev->fw_ver < mthca_hca_table[id->driver_data].latest_fw) {
1023 mthca_warn(mdev, "HCA FW version %x.%x.%x is old (%x.%x.%x is current).\n",
1024 (int) (mdev->fw_ver >> 32), (int) (mdev->fw_ver >> 16) & 0xffff,
1025 (int) (mdev->fw_ver & 0xffff),
1026 (int) (mthca_hca_table[id->driver_data].latest_fw >> 32),
1027 (int) (mthca_hca_table[id->driver_data].latest_fw >> 16) & 0xffff,
1028 (int) (mthca_hca_table[id->driver_data].latest_fw & 0xffff));
1029 mthca_warn(mdev, "If you have problems, try updating your HCA FW.\n");
1032 err = mthca_setup_hca(mdev);
1036 err = mthca_register_device(mdev);
1040 err = mthca_create_agents(mdev);
1042 goto err_unregister;
1044 pci_set_drvdata(pdev, mdev);
1049 mthca_unregister_device(mdev);
1052 mthca_cleanup_mcg_table(mdev);
1053 mthca_cleanup_av_table(mdev);
1054 mthca_cleanup_qp_table(mdev);
1055 mthca_cleanup_cq_table(mdev);
1056 mthca_cmd_use_polling(mdev);
1057 mthca_cleanup_eq_table(mdev);
1059 mthca_pd_free(mdev, &mdev->driver_pd);
1061 mthca_cleanup_mr_table(mdev);
1062 mthca_cleanup_pd_table(mdev);
1063 mthca_cleanup_uar_table(mdev);
1066 mthca_close_hca(mdev);
1069 mthca_cmd_cleanup(mdev);
1072 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1073 pci_disable_msix(pdev);
1074 if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1075 pci_disable_msi(pdev);
1077 ib_dealloc_device(&mdev->ib_dev);
1080 mthca_release_regions(pdev, ddr_hidden);
1083 pci_disable_device(pdev);
1084 pci_set_drvdata(pdev, NULL);
1088 static void __devexit mthca_remove_one(struct pci_dev *pdev)
1090 struct mthca_dev *mdev = pci_get_drvdata(pdev);
1095 mthca_free_agents(mdev);
1096 mthca_unregister_device(mdev);
1098 for (p = 1; p <= mdev->limits.num_ports; ++p)
1099 mthca_CLOSE_IB(mdev, p, &status);
1101 mthca_cleanup_mcg_table(mdev);
1102 mthca_cleanup_av_table(mdev);
1103 mthca_cleanup_qp_table(mdev);
1104 mthca_cleanup_cq_table(mdev);
1105 mthca_cmd_use_polling(mdev);
1106 mthca_cleanup_eq_table(mdev);
1108 mthca_pd_free(mdev, &mdev->driver_pd);
1110 mthca_cleanup_mr_table(mdev);
1111 mthca_cleanup_pd_table(mdev);
1114 mthca_uar_free(mdev, &mdev->driver_uar);
1115 mthca_cleanup_uar_table(mdev);
1116 mthca_close_hca(mdev);
1117 mthca_cmd_cleanup(mdev);
1119 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1120 pci_disable_msix(pdev);
1121 if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1122 pci_disable_msi(pdev);
1124 ib_dealloc_device(&mdev->ib_dev);
1125 mthca_release_regions(pdev, mdev->mthca_flags &
1126 MTHCA_FLAG_DDR_HIDDEN);
1127 pci_disable_device(pdev);
1128 pci_set_drvdata(pdev, NULL);
1132 static struct pci_device_id mthca_pci_table[] = {
1133 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_TAVOR),
1134 .driver_data = TAVOR },
1135 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_TAVOR),
1136 .driver_data = TAVOR },
1137 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
1138 .driver_data = ARBEL_COMPAT },
1139 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
1140 .driver_data = ARBEL_COMPAT },
1141 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL),
1142 .driver_data = ARBEL_NATIVE },
1143 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL),
1144 .driver_data = ARBEL_NATIVE },
1145 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI),
1146 .driver_data = SINAI },
1147 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI),
1148 .driver_data = SINAI },
1149 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
1150 .driver_data = SINAI },
1151 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
1152 .driver_data = SINAI },
1156 MODULE_DEVICE_TABLE(pci, mthca_pci_table);
1158 static struct pci_driver mthca_driver = {
1160 .id_table = mthca_pci_table,
1161 .probe = mthca_init_one,
1162 .remove = __devexit_p(mthca_remove_one)
1165 static int __init mthca_init(void)
1169 ret = pci_register_driver(&mthca_driver);
1170 return ret < 0 ? ret : 0;
1173 static void __exit mthca_cleanup(void)
1175 pci_unregister_driver(&mthca_driver);
1178 module_init(mthca_init);
1179 module_exit(mthca_cleanup);