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/module.h>
38 #include <linux/init.h>
39 #include <linux/errno.h>
40 #include <linux/pci.h>
41 #include <linux/interrupt.h>
43 #include "mthca_dev.h"
44 #include "mthca_config_reg.h"
45 #include "mthca_cmd.h"
46 #include "mthca_profile.h"
47 #include "mthca_memfree.h"
49 MODULE_AUTHOR("Roland Dreier");
50 MODULE_DESCRIPTION("Mellanox InfiniBand HCA low-level driver");
51 MODULE_LICENSE("Dual BSD/GPL");
52 MODULE_VERSION(DRV_VERSION);
54 #ifdef CONFIG_INFINIBAND_MTHCA_DEBUG
56 int mthca_debug_level = 0;
57 module_param_named(debug_level, mthca_debug_level, int, 0644);
58 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
60 #endif /* CONFIG_INFINIBAND_MTHCA_DEBUG */
65 module_param(msi_x, int, 0444);
66 MODULE_PARM_DESC(msi_x, "attempt to use MSI-X if nonzero");
69 module_param(msi, int, 0444);
70 MODULE_PARM_DESC(msi, "attempt to use MSI if nonzero");
72 #else /* CONFIG_PCI_MSI */
77 #endif /* CONFIG_PCI_MSI */
79 static int tune_pci = 0;
80 module_param(tune_pci, int, 0444);
81 MODULE_PARM_DESC(tune_pci, "increase PCI burst from the default set by BIOS if nonzero");
83 static const char mthca_version[] __devinitdata =
84 DRV_NAME ": Mellanox InfiniBand HCA driver v"
85 DRV_VERSION " (" DRV_RELDATE ")\n";
87 static struct mthca_profile default_profile = {
94 .num_udav = 1 << 15, /* Tavor only */
95 .fmr_reserved_mtts = 1 << 18, /* Tavor only */
96 .uarc_size = 1 << 18, /* Arbel only */
99 static int __devinit mthca_tune_pci(struct mthca_dev *mdev)
107 /* First try to max out Read Byte Count */
108 cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_PCIX);
110 if (pci_read_config_word(mdev->pdev, cap + PCI_X_CMD, &val)) {
111 mthca_err(mdev, "Couldn't read PCI-X command register, "
115 val = (val & ~PCI_X_CMD_MAX_READ) | (3 << 2);
116 if (pci_write_config_word(mdev->pdev, cap + PCI_X_CMD, val)) {
117 mthca_err(mdev, "Couldn't write PCI-X command register, "
121 } else if (!(mdev->mthca_flags & MTHCA_FLAG_PCIE))
122 mthca_info(mdev, "No PCI-X capability, not setting RBC.\n");
124 cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_EXP);
126 if (pci_read_config_word(mdev->pdev, cap + PCI_EXP_DEVCTL, &val)) {
127 mthca_err(mdev, "Couldn't read PCI Express device control "
128 "register, aborting.\n");
131 val = (val & ~PCI_EXP_DEVCTL_READRQ) | (5 << 12);
132 if (pci_write_config_word(mdev->pdev, cap + PCI_EXP_DEVCTL, val)) {
133 mthca_err(mdev, "Couldn't write PCI Express device control "
134 "register, aborting.\n");
137 } else if (mdev->mthca_flags & MTHCA_FLAG_PCIE)
138 mthca_info(mdev, "No PCI Express capability, "
139 "not setting Max Read Request Size.\n");
144 static int __devinit mthca_dev_lim(struct mthca_dev *mdev, struct mthca_dev_lim *dev_lim)
149 err = mthca_QUERY_DEV_LIM(mdev, dev_lim, &status);
151 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
155 mthca_err(mdev, "QUERY_DEV_LIM returned status 0x%02x, "
156 "aborting.\n", status);
159 if (dev_lim->min_page_sz > PAGE_SIZE) {
160 mthca_err(mdev, "HCA minimum page size of %d bigger than "
161 "kernel PAGE_SIZE of %ld, aborting.\n",
162 dev_lim->min_page_sz, PAGE_SIZE);
165 if (dev_lim->num_ports > MTHCA_MAX_PORTS) {
166 mthca_err(mdev, "HCA has %d ports, but we only support %d, "
168 dev_lim->num_ports, MTHCA_MAX_PORTS);
172 if (dev_lim->uar_size > pci_resource_len(mdev->pdev, 2)) {
173 mthca_err(mdev, "HCA reported UAR size of 0x%x bigger than "
174 "PCI resource 2 size of 0x%llx, aborting.\n",
176 (unsigned long long)pci_resource_len(mdev->pdev, 2));
180 mdev->limits.num_ports = dev_lim->num_ports;
181 mdev->limits.vl_cap = dev_lim->max_vl;
182 mdev->limits.mtu_cap = dev_lim->max_mtu;
183 mdev->limits.gid_table_len = dev_lim->max_gids;
184 mdev->limits.pkey_table_len = dev_lim->max_pkeys;
185 mdev->limits.local_ca_ack_delay = dev_lim->local_ca_ack_delay;
186 mdev->limits.max_sg = dev_lim->max_sg;
187 mdev->limits.max_wqes = dev_lim->max_qp_sz;
188 mdev->limits.max_qp_init_rdma = dev_lim->max_requester_per_qp;
189 mdev->limits.reserved_qps = dev_lim->reserved_qps;
190 mdev->limits.max_srq_wqes = dev_lim->max_srq_sz;
191 mdev->limits.reserved_srqs = dev_lim->reserved_srqs;
192 mdev->limits.reserved_eecs = dev_lim->reserved_eecs;
193 mdev->limits.max_desc_sz = dev_lim->max_desc_sz;
194 mdev->limits.max_srq_sge = mthca_max_srq_sge(mdev);
196 * Subtract 1 from the limit because we need to allocate a
197 * spare CQE so the HCA HW can tell the difference between an
198 * empty CQ and a full CQ.
200 mdev->limits.max_cqes = dev_lim->max_cq_sz - 1;
201 mdev->limits.reserved_cqs = dev_lim->reserved_cqs;
202 mdev->limits.reserved_eqs = dev_lim->reserved_eqs;
203 mdev->limits.reserved_mtts = dev_lim->reserved_mtts;
204 mdev->limits.reserved_mrws = dev_lim->reserved_mrws;
205 mdev->limits.reserved_uars = dev_lim->reserved_uars;
206 mdev->limits.reserved_pds = dev_lim->reserved_pds;
207 mdev->limits.port_width_cap = dev_lim->max_port_width;
208 mdev->limits.page_size_cap = ~(u32) (dev_lim->min_page_sz - 1);
209 mdev->limits.flags = dev_lim->flags;
211 * For old FW that doesn't return static rate support, use a
212 * value of 0x3 (only static rate values of 0 or 1 are handled),
213 * except on Sinai, where even old FW can handle static rate
216 if (dev_lim->stat_rate_support)
217 mdev->limits.stat_rate_support = dev_lim->stat_rate_support;
218 else if (mdev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
219 mdev->limits.stat_rate_support = 0xf;
221 mdev->limits.stat_rate_support = 0x3;
223 /* IB_DEVICE_RESIZE_MAX_WR not supported by driver.
224 May be doable since hardware supports it for SRQ.
226 IB_DEVICE_N_NOTIFY_CQ is supported by hardware but not by driver.
228 IB_DEVICE_SRQ_RESIZE is supported by hardware but SRQ is not
229 supported by driver. */
230 mdev->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
231 IB_DEVICE_PORT_ACTIVE_EVENT |
232 IB_DEVICE_SYS_IMAGE_GUID |
233 IB_DEVICE_RC_RNR_NAK_GEN;
235 if (dev_lim->flags & DEV_LIM_FLAG_BAD_PKEY_CNTR)
236 mdev->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
238 if (dev_lim->flags & DEV_LIM_FLAG_BAD_QKEY_CNTR)
239 mdev->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
241 if (dev_lim->flags & DEV_LIM_FLAG_RAW_MULTI)
242 mdev->device_cap_flags |= IB_DEVICE_RAW_MULTI;
244 if (dev_lim->flags & DEV_LIM_FLAG_AUTO_PATH_MIG)
245 mdev->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
247 if (dev_lim->flags & DEV_LIM_FLAG_UD_AV_PORT_ENFORCE)
248 mdev->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
250 if (dev_lim->flags & DEV_LIM_FLAG_SRQ)
251 mdev->mthca_flags |= MTHCA_FLAG_SRQ;
256 static int __devinit mthca_init_tavor(struct mthca_dev *mdev)
260 struct mthca_dev_lim dev_lim;
261 struct mthca_profile profile;
262 struct mthca_init_hca_param init_hca;
264 err = mthca_SYS_EN(mdev, &status);
266 mthca_err(mdev, "SYS_EN command failed, aborting.\n");
270 mthca_err(mdev, "SYS_EN returned status 0x%02x, "
271 "aborting.\n", status);
275 err = mthca_QUERY_FW(mdev, &status);
277 mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
281 mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
282 "aborting.\n", status);
286 err = mthca_QUERY_DDR(mdev, &status);
288 mthca_err(mdev, "QUERY_DDR command failed, aborting.\n");
292 mthca_err(mdev, "QUERY_DDR returned status 0x%02x, "
293 "aborting.\n", status);
298 err = mthca_dev_lim(mdev, &dev_lim);
300 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
304 profile = default_profile;
305 profile.num_uar = dev_lim.uar_size / PAGE_SIZE;
306 profile.uarc_size = 0;
307 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
308 profile.num_srq = dev_lim.max_srqs;
310 err = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
314 err = mthca_INIT_HCA(mdev, &init_hca, &status);
316 mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
320 mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
321 "aborting.\n", status);
329 mthca_SYS_DIS(mdev, &status);
334 static int __devinit mthca_load_fw(struct mthca_dev *mdev)
339 /* FIXME: use HCA-attached memory for FW if present */
341 mdev->fw.arbel.fw_icm =
342 mthca_alloc_icm(mdev, mdev->fw.arbel.fw_pages,
343 GFP_HIGHUSER | __GFP_NOWARN);
344 if (!mdev->fw.arbel.fw_icm) {
345 mthca_err(mdev, "Couldn't allocate FW area, aborting.\n");
349 err = mthca_MAP_FA(mdev, mdev->fw.arbel.fw_icm, &status);
351 mthca_err(mdev, "MAP_FA command failed, aborting.\n");
355 mthca_err(mdev, "MAP_FA returned status 0x%02x, aborting.\n", status);
359 err = mthca_RUN_FW(mdev, &status);
361 mthca_err(mdev, "RUN_FW command failed, aborting.\n");
365 mthca_err(mdev, "RUN_FW returned status 0x%02x, aborting.\n", status);
373 mthca_UNMAP_FA(mdev, &status);
376 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
380 static int __devinit mthca_init_icm(struct mthca_dev *mdev,
381 struct mthca_dev_lim *dev_lim,
382 struct mthca_init_hca_param *init_hca,
389 err = mthca_SET_ICM_SIZE(mdev, icm_size, &aux_pages, &status);
391 mthca_err(mdev, "SET_ICM_SIZE command failed, aborting.\n");
395 mthca_err(mdev, "SET_ICM_SIZE returned status 0x%02x, "
396 "aborting.\n", status);
400 mthca_dbg(mdev, "%lld KB of HCA context requires %lld KB aux memory.\n",
401 (unsigned long long) icm_size >> 10,
402 (unsigned long long) aux_pages << 2);
404 mdev->fw.arbel.aux_icm = mthca_alloc_icm(mdev, aux_pages,
405 GFP_HIGHUSER | __GFP_NOWARN);
406 if (!mdev->fw.arbel.aux_icm) {
407 mthca_err(mdev, "Couldn't allocate aux memory, aborting.\n");
411 err = mthca_MAP_ICM_AUX(mdev, mdev->fw.arbel.aux_icm, &status);
413 mthca_err(mdev, "MAP_ICM_AUX command failed, aborting.\n");
417 mthca_err(mdev, "MAP_ICM_AUX returned status 0x%02x, aborting.\n", status);
422 err = mthca_map_eq_icm(mdev, init_hca->eqc_base);
424 mthca_err(mdev, "Failed to map EQ context memory, aborting.\n");
428 mdev->mr_table.mtt_table = mthca_alloc_icm_table(mdev, init_hca->mtt_base,
430 mdev->limits.num_mtt_segs,
431 mdev->limits.reserved_mtts, 1);
432 if (!mdev->mr_table.mtt_table) {
433 mthca_err(mdev, "Failed to map MTT context memory, aborting.\n");
438 mdev->mr_table.mpt_table = mthca_alloc_icm_table(mdev, init_hca->mpt_base,
439 dev_lim->mpt_entry_sz,
440 mdev->limits.num_mpts,
441 mdev->limits.reserved_mrws, 1);
442 if (!mdev->mr_table.mpt_table) {
443 mthca_err(mdev, "Failed to map MPT context memory, aborting.\n");
448 mdev->qp_table.qp_table = mthca_alloc_icm_table(mdev, init_hca->qpc_base,
449 dev_lim->qpc_entry_sz,
450 mdev->limits.num_qps,
451 mdev->limits.reserved_qps, 0);
452 if (!mdev->qp_table.qp_table) {
453 mthca_err(mdev, "Failed to map QP context memory, aborting.\n");
458 mdev->qp_table.eqp_table = mthca_alloc_icm_table(mdev, init_hca->eqpc_base,
459 dev_lim->eqpc_entry_sz,
460 mdev->limits.num_qps,
461 mdev->limits.reserved_qps, 0);
462 if (!mdev->qp_table.eqp_table) {
463 mthca_err(mdev, "Failed to map EQP context memory, aborting.\n");
468 mdev->qp_table.rdb_table = mthca_alloc_icm_table(mdev, init_hca->rdb_base,
469 MTHCA_RDB_ENTRY_SIZE,
470 mdev->limits.num_qps <<
471 mdev->qp_table.rdb_shift,
473 if (!mdev->qp_table.rdb_table) {
474 mthca_err(mdev, "Failed to map RDB context memory, aborting\n");
479 mdev->cq_table.table = mthca_alloc_icm_table(mdev, init_hca->cqc_base,
480 dev_lim->cqc_entry_sz,
481 mdev->limits.num_cqs,
482 mdev->limits.reserved_cqs, 0);
483 if (!mdev->cq_table.table) {
484 mthca_err(mdev, "Failed to map CQ context memory, aborting.\n");
489 if (mdev->mthca_flags & MTHCA_FLAG_SRQ) {
490 mdev->srq_table.table =
491 mthca_alloc_icm_table(mdev, init_hca->srqc_base,
492 dev_lim->srq_entry_sz,
493 mdev->limits.num_srqs,
494 mdev->limits.reserved_srqs, 0);
495 if (!mdev->srq_table.table) {
496 mthca_err(mdev, "Failed to map SRQ context memory, "
504 * It's not strictly required, but for simplicity just map the
505 * whole multicast group table now. The table isn't very big
506 * and it's a lot easier than trying to track ref counts.
508 mdev->mcg_table.table = mthca_alloc_icm_table(mdev, init_hca->mc_base,
509 MTHCA_MGM_ENTRY_SIZE,
510 mdev->limits.num_mgms +
511 mdev->limits.num_amgms,
512 mdev->limits.num_mgms +
513 mdev->limits.num_amgms,
515 if (!mdev->mcg_table.table) {
516 mthca_err(mdev, "Failed to map MCG context memory, aborting.\n");
524 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
525 mthca_free_icm_table(mdev, mdev->srq_table.table);
528 mthca_free_icm_table(mdev, mdev->cq_table.table);
531 mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
534 mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
537 mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
540 mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
543 mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
546 mthca_unmap_eq_icm(mdev);
549 mthca_UNMAP_ICM_AUX(mdev, &status);
552 mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
557 static void mthca_free_icms(struct mthca_dev *mdev)
561 mthca_free_icm_table(mdev, mdev->mcg_table.table);
562 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
563 mthca_free_icm_table(mdev, mdev->srq_table.table);
564 mthca_free_icm_table(mdev, mdev->cq_table.table);
565 mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
566 mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
567 mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
568 mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
569 mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
570 mthca_unmap_eq_icm(mdev);
572 mthca_UNMAP_ICM_AUX(mdev, &status);
573 mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
576 static int __devinit mthca_init_arbel(struct mthca_dev *mdev)
578 struct mthca_dev_lim dev_lim;
579 struct mthca_profile profile;
580 struct mthca_init_hca_param init_hca;
585 err = mthca_QUERY_FW(mdev, &status);
587 mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
591 mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
592 "aborting.\n", status);
596 err = mthca_ENABLE_LAM(mdev, &status);
598 mthca_err(mdev, "ENABLE_LAM command failed, aborting.\n");
601 if (status == MTHCA_CMD_STAT_LAM_NOT_PRE) {
602 mthca_dbg(mdev, "No HCA-attached memory (running in MemFree mode)\n");
603 mdev->mthca_flags |= MTHCA_FLAG_NO_LAM;
605 mthca_err(mdev, "ENABLE_LAM returned status 0x%02x, "
606 "aborting.\n", status);
610 err = mthca_load_fw(mdev);
612 mthca_err(mdev, "Failed to start FW, aborting.\n");
616 err = mthca_dev_lim(mdev, &dev_lim);
618 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
622 profile = default_profile;
623 profile.num_uar = dev_lim.uar_size / PAGE_SIZE;
624 profile.num_udav = 0;
625 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
626 profile.num_srq = dev_lim.max_srqs;
628 icm_size = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
629 if ((int) icm_size < 0) {
634 err = mthca_init_icm(mdev, &dev_lim, &init_hca, icm_size);
638 err = mthca_INIT_HCA(mdev, &init_hca, &status);
640 mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
644 mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
645 "aborting.\n", status);
653 mthca_free_icms(mdev);
656 mthca_UNMAP_FA(mdev, &status);
657 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
660 if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
661 mthca_DISABLE_LAM(mdev, &status);
666 static void mthca_close_hca(struct mthca_dev *mdev)
670 mthca_CLOSE_HCA(mdev, 0, &status);
672 if (mthca_is_memfree(mdev)) {
673 mthca_free_icms(mdev);
675 mthca_UNMAP_FA(mdev, &status);
676 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
678 if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
679 mthca_DISABLE_LAM(mdev, &status);
681 mthca_SYS_DIS(mdev, &status);
684 static int __devinit mthca_init_hca(struct mthca_dev *mdev)
688 struct mthca_adapter adapter;
690 if (mthca_is_memfree(mdev))
691 err = mthca_init_arbel(mdev);
693 err = mthca_init_tavor(mdev);
698 err = mthca_QUERY_ADAPTER(mdev, &adapter, &status);
700 mthca_err(mdev, "QUERY_ADAPTER command failed, aborting.\n");
704 mthca_err(mdev, "QUERY_ADAPTER returned status 0x%02x, "
705 "aborting.\n", status);
710 mdev->eq_table.inta_pin = adapter.inta_pin;
711 mdev->rev_id = adapter.revision_id;
712 memcpy(mdev->board_id, adapter.board_id, sizeof mdev->board_id);
717 mthca_close_hca(mdev);
721 static int __devinit mthca_setup_hca(struct mthca_dev *dev)
726 MTHCA_INIT_DOORBELL_LOCK(&dev->doorbell_lock);
728 err = mthca_init_uar_table(dev);
730 mthca_err(dev, "Failed to initialize "
731 "user access region table, aborting.\n");
735 err = mthca_uar_alloc(dev, &dev->driver_uar);
737 mthca_err(dev, "Failed to allocate driver access region, "
739 goto err_uar_table_free;
742 dev->kar = ioremap(dev->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
744 mthca_err(dev, "Couldn't map kernel access region, "
750 err = mthca_init_pd_table(dev);
752 mthca_err(dev, "Failed to initialize "
753 "protection domain table, aborting.\n");
757 err = mthca_init_mr_table(dev);
759 mthca_err(dev, "Failed to initialize "
760 "memory region table, aborting.\n");
761 goto err_pd_table_free;
764 err = mthca_pd_alloc(dev, 1, &dev->driver_pd);
766 mthca_err(dev, "Failed to create driver PD, "
768 goto err_mr_table_free;
771 err = mthca_init_eq_table(dev);
773 mthca_err(dev, "Failed to initialize "
774 "event queue table, aborting.\n");
778 err = mthca_cmd_use_events(dev);
780 mthca_err(dev, "Failed to switch to event-driven "
781 "firmware commands, aborting.\n");
782 goto err_eq_table_free;
785 err = mthca_NOP(dev, &status);
787 mthca_err(dev, "NOP command failed to generate interrupt (IRQ %d), aborting.\n",
788 dev->mthca_flags & MTHCA_FLAG_MSI_X ?
789 dev->eq_table.eq[MTHCA_EQ_CMD].msi_x_vector :
791 if (dev->mthca_flags & (MTHCA_FLAG_MSI | MTHCA_FLAG_MSI_X))
792 mthca_err(dev, "Try again with MSI/MSI-X disabled.\n");
794 mthca_err(dev, "BIOS or ACPI interrupt routing problem?\n");
799 mthca_dbg(dev, "NOP command IRQ test passed\n");
801 err = mthca_init_cq_table(dev);
803 mthca_err(dev, "Failed to initialize "
804 "completion queue table, aborting.\n");
808 err = mthca_init_srq_table(dev);
810 mthca_err(dev, "Failed to initialize "
811 "shared receive queue table, aborting.\n");
812 goto err_cq_table_free;
815 err = mthca_init_qp_table(dev);
817 mthca_err(dev, "Failed to initialize "
818 "queue pair table, aborting.\n");
819 goto err_srq_table_free;
822 err = mthca_init_av_table(dev);
824 mthca_err(dev, "Failed to initialize "
825 "address vector table, aborting.\n");
826 goto err_qp_table_free;
829 err = mthca_init_mcg_table(dev);
831 mthca_err(dev, "Failed to initialize "
832 "multicast group table, aborting.\n");
833 goto err_av_table_free;
839 mthca_cleanup_av_table(dev);
842 mthca_cleanup_qp_table(dev);
845 mthca_cleanup_srq_table(dev);
848 mthca_cleanup_cq_table(dev);
851 mthca_cmd_use_polling(dev);
854 mthca_cleanup_eq_table(dev);
857 mthca_pd_free(dev, &dev->driver_pd);
860 mthca_cleanup_mr_table(dev);
863 mthca_cleanup_pd_table(dev);
869 mthca_uar_free(dev, &dev->driver_uar);
872 mthca_cleanup_uar_table(dev);
876 static int __devinit mthca_request_regions(struct pci_dev *pdev,
882 * We can't just use pci_request_regions() because the MSI-X
883 * table is right in the middle of the first BAR. If we did
884 * pci_request_region and grab all of the first BAR, then
885 * setting up MSI-X would fail, since the PCI core wants to do
886 * request_mem_region on the MSI-X vector table.
888 * So just request what we need right now, and request any
889 * other regions we need when setting up EQs.
891 if (!request_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
892 MTHCA_HCR_SIZE, DRV_NAME))
895 err = pci_request_region(pdev, 2, DRV_NAME);
897 goto err_bar2_failed;
900 err = pci_request_region(pdev, 4, DRV_NAME);
902 goto err_bar4_failed;
908 pci_release_region(pdev, 2);
911 release_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
917 static void mthca_release_regions(struct pci_dev *pdev,
921 pci_release_region(pdev, 4);
923 pci_release_region(pdev, 2);
925 release_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
929 static int __devinit mthca_enable_msi_x(struct mthca_dev *mdev)
931 struct msix_entry entries[3];
934 entries[0].entry = 0;
935 entries[1].entry = 1;
936 entries[2].entry = 2;
938 err = pci_enable_msix(mdev->pdev, entries, ARRAY_SIZE(entries));
941 mthca_info(mdev, "Only %d MSI-X vectors available, "
942 "not using MSI-X\n", err);
946 mdev->eq_table.eq[MTHCA_EQ_COMP ].msi_x_vector = entries[0].vector;
947 mdev->eq_table.eq[MTHCA_EQ_ASYNC].msi_x_vector = entries[1].vector;
948 mdev->eq_table.eq[MTHCA_EQ_CMD ].msi_x_vector = entries[2].vector;
953 /* Types of supported HCA */
956 ARBEL_COMPAT, /* MT25208 in Tavor compat mode */
957 ARBEL_NATIVE, /* MT25208 with extended features */
961 #define MTHCA_FW_VER(major, minor, subminor) \
962 (((u64) (major) << 32) | ((u64) (minor) << 16) | (u64) (subminor))
967 } mthca_hca_table[] = {
968 [TAVOR] = { .latest_fw = MTHCA_FW_VER(3, 4, 0),
970 [ARBEL_COMPAT] = { .latest_fw = MTHCA_FW_VER(4, 7, 600),
971 .flags = MTHCA_FLAG_PCIE },
972 [ARBEL_NATIVE] = { .latest_fw = MTHCA_FW_VER(5, 1, 400),
973 .flags = MTHCA_FLAG_MEMFREE |
975 [SINAI] = { .latest_fw = MTHCA_FW_VER(1, 1, 0),
976 .flags = MTHCA_FLAG_MEMFREE |
978 MTHCA_FLAG_SINAI_OPT }
981 static int __devinit mthca_init_one(struct pci_dev *pdev,
982 const struct pci_device_id *id)
984 static int mthca_version_printed = 0;
987 struct mthca_dev *mdev;
989 if (!mthca_version_printed) {
990 printk(KERN_INFO "%s", mthca_version);
991 ++mthca_version_printed;
994 printk(KERN_INFO PFX "Initializing %s\n",
997 if (id->driver_data >= ARRAY_SIZE(mthca_hca_table)) {
998 printk(KERN_ERR PFX "%s has invalid driver data %lx\n",
999 pci_name(pdev), id->driver_data);
1003 err = pci_enable_device(pdev);
1005 dev_err(&pdev->dev, "Cannot enable PCI device, "
1011 * Check for BARs. We expect 0: 1MB, 2: 8MB, 4: DDR (may not
1014 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
1015 pci_resource_len(pdev, 0) != 1 << 20) {
1016 dev_err(&pdev->dev, "Missing DCS, aborting.\n");
1018 goto err_disable_pdev;
1020 if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
1021 dev_err(&pdev->dev, "Missing UAR, aborting.\n");
1023 goto err_disable_pdev;
1025 if (!(pci_resource_flags(pdev, 4) & IORESOURCE_MEM))
1028 err = mthca_request_regions(pdev, ddr_hidden);
1030 dev_err(&pdev->dev, "Cannot obtain PCI resources, "
1032 goto err_disable_pdev;
1035 pci_set_master(pdev);
1037 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
1039 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask.\n");
1040 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1042 dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting.\n");
1046 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
1048 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit "
1049 "consistent PCI DMA mask.\n");
1050 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
1052 dev_err(&pdev->dev, "Can't set consistent PCI DMA mask, "
1058 mdev = (struct mthca_dev *) ib_alloc_device(sizeof *mdev);
1060 dev_err(&pdev->dev, "Device struct alloc failed, "
1068 mdev->mthca_flags = mthca_hca_table[id->driver_data].flags;
1070 mdev->mthca_flags |= MTHCA_FLAG_DDR_HIDDEN;
1073 * Now reset the HCA before we touch the PCI capabilities or
1074 * attempt a firmware command, since a boot ROM may have left
1075 * the HCA in an undefined state.
1077 err = mthca_reset(mdev);
1079 mthca_err(mdev, "Failed to reset HCA, aborting.\n");
1083 if (msi_x && !mthca_enable_msi_x(mdev))
1084 mdev->mthca_flags |= MTHCA_FLAG_MSI_X;
1085 if (msi && !(mdev->mthca_flags & MTHCA_FLAG_MSI_X) &&
1086 !pci_enable_msi(pdev))
1087 mdev->mthca_flags |= MTHCA_FLAG_MSI;
1089 if (mthca_cmd_init(mdev)) {
1090 mthca_err(mdev, "Failed to init command interface, aborting.\n");
1094 err = mthca_tune_pci(mdev);
1098 err = mthca_init_hca(mdev);
1102 if (mdev->fw_ver < mthca_hca_table[id->driver_data].latest_fw) {
1103 mthca_warn(mdev, "HCA FW version %d.%d.%d is old (%d.%d.%d is current).\n",
1104 (int) (mdev->fw_ver >> 32), (int) (mdev->fw_ver >> 16) & 0xffff,
1105 (int) (mdev->fw_ver & 0xffff),
1106 (int) (mthca_hca_table[id->driver_data].latest_fw >> 32),
1107 (int) (mthca_hca_table[id->driver_data].latest_fw >> 16) & 0xffff,
1108 (int) (mthca_hca_table[id->driver_data].latest_fw & 0xffff));
1109 mthca_warn(mdev, "If you have problems, try updating your HCA FW.\n");
1112 err = mthca_setup_hca(mdev);
1116 err = mthca_register_device(mdev);
1120 err = mthca_create_agents(mdev);
1122 goto err_unregister;
1124 pci_set_drvdata(pdev, mdev);
1129 mthca_unregister_device(mdev);
1132 mthca_cleanup_mcg_table(mdev);
1133 mthca_cleanup_av_table(mdev);
1134 mthca_cleanup_qp_table(mdev);
1135 mthca_cleanup_srq_table(mdev);
1136 mthca_cleanup_cq_table(mdev);
1137 mthca_cmd_use_polling(mdev);
1138 mthca_cleanup_eq_table(mdev);
1140 mthca_pd_free(mdev, &mdev->driver_pd);
1142 mthca_cleanup_mr_table(mdev);
1143 mthca_cleanup_pd_table(mdev);
1144 mthca_cleanup_uar_table(mdev);
1147 mthca_close_hca(mdev);
1150 mthca_cmd_cleanup(mdev);
1153 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1154 pci_disable_msix(pdev);
1155 if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1156 pci_disable_msi(pdev);
1158 ib_dealloc_device(&mdev->ib_dev);
1161 mthca_release_regions(pdev, ddr_hidden);
1164 pci_disable_device(pdev);
1165 pci_set_drvdata(pdev, NULL);
1169 static void __devexit mthca_remove_one(struct pci_dev *pdev)
1171 struct mthca_dev *mdev = pci_get_drvdata(pdev);
1176 mthca_free_agents(mdev);
1177 mthca_unregister_device(mdev);
1179 for (p = 1; p <= mdev->limits.num_ports; ++p)
1180 mthca_CLOSE_IB(mdev, p, &status);
1182 mthca_cleanup_mcg_table(mdev);
1183 mthca_cleanup_av_table(mdev);
1184 mthca_cleanup_qp_table(mdev);
1185 mthca_cleanup_srq_table(mdev);
1186 mthca_cleanup_cq_table(mdev);
1187 mthca_cmd_use_polling(mdev);
1188 mthca_cleanup_eq_table(mdev);
1190 mthca_pd_free(mdev, &mdev->driver_pd);
1192 mthca_cleanup_mr_table(mdev);
1193 mthca_cleanup_pd_table(mdev);
1196 mthca_uar_free(mdev, &mdev->driver_uar);
1197 mthca_cleanup_uar_table(mdev);
1198 mthca_close_hca(mdev);
1199 mthca_cmd_cleanup(mdev);
1201 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1202 pci_disable_msix(pdev);
1203 if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1204 pci_disable_msi(pdev);
1206 ib_dealloc_device(&mdev->ib_dev);
1207 mthca_release_regions(pdev, mdev->mthca_flags &
1208 MTHCA_FLAG_DDR_HIDDEN);
1209 pci_disable_device(pdev);
1210 pci_set_drvdata(pdev, NULL);
1214 static struct pci_device_id mthca_pci_table[] = {
1215 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_TAVOR),
1216 .driver_data = TAVOR },
1217 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_TAVOR),
1218 .driver_data = TAVOR },
1219 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
1220 .driver_data = ARBEL_COMPAT },
1221 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
1222 .driver_data = ARBEL_COMPAT },
1223 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL),
1224 .driver_data = ARBEL_NATIVE },
1225 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL),
1226 .driver_data = ARBEL_NATIVE },
1227 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI),
1228 .driver_data = SINAI },
1229 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI),
1230 .driver_data = SINAI },
1231 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
1232 .driver_data = SINAI },
1233 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
1234 .driver_data = SINAI },
1238 MODULE_DEVICE_TABLE(pci, mthca_pci_table);
1240 static struct pci_driver mthca_driver = {
1242 .id_table = mthca_pci_table,
1243 .probe = mthca_init_one,
1244 .remove = __devexit_p(mthca_remove_one)
1247 static int __init mthca_init(void)
1251 ret = pci_register_driver(&mthca_driver);
1252 return ret < 0 ? ret : 0;
1255 static void __exit mthca_cleanup(void)
1257 pci_unregister_driver(&mthca_driver);
1260 module_init(mthca_init);
1261 module_exit(mthca_cleanup);