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 struct mutex mthca_device_mutex;
85 static const char mthca_version[] __devinitdata =
86 DRV_NAME ": Mellanox InfiniBand HCA driver v"
87 DRV_VERSION " (" DRV_RELDATE ")\n";
89 static struct mthca_profile default_profile = {
96 .num_udav = 1 << 15, /* Tavor only */
97 .fmr_reserved_mtts = 1 << 18, /* Tavor only */
98 .uarc_size = 1 << 18, /* Arbel only */
101 static int __devinit mthca_tune_pci(struct mthca_dev *mdev)
109 /* First try to max out Read Byte Count */
110 cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_PCIX);
112 if (pci_read_config_word(mdev->pdev, cap + PCI_X_CMD, &val)) {
113 mthca_err(mdev, "Couldn't read PCI-X command register, "
117 val = (val & ~PCI_X_CMD_MAX_READ) | (3 << 2);
118 if (pci_write_config_word(mdev->pdev, cap + PCI_X_CMD, val)) {
119 mthca_err(mdev, "Couldn't write PCI-X command register, "
123 } else if (!(mdev->mthca_flags & MTHCA_FLAG_PCIE))
124 mthca_info(mdev, "No PCI-X capability, not setting RBC.\n");
126 cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_EXP);
128 if (pci_read_config_word(mdev->pdev, cap + PCI_EXP_DEVCTL, &val)) {
129 mthca_err(mdev, "Couldn't read PCI Express device control "
130 "register, aborting.\n");
133 val = (val & ~PCI_EXP_DEVCTL_READRQ) | (5 << 12);
134 if (pci_write_config_word(mdev->pdev, cap + PCI_EXP_DEVCTL, val)) {
135 mthca_err(mdev, "Couldn't write PCI Express device control "
136 "register, aborting.\n");
139 } else if (mdev->mthca_flags & MTHCA_FLAG_PCIE)
140 mthca_info(mdev, "No PCI Express capability, "
141 "not setting Max Read Request Size.\n");
146 static int __devinit mthca_dev_lim(struct mthca_dev *mdev, struct mthca_dev_lim *dev_lim)
151 err = mthca_QUERY_DEV_LIM(mdev, dev_lim, &status);
153 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
157 mthca_err(mdev, "QUERY_DEV_LIM returned status 0x%02x, "
158 "aborting.\n", status);
161 if (dev_lim->min_page_sz > PAGE_SIZE) {
162 mthca_err(mdev, "HCA minimum page size of %d bigger than "
163 "kernel PAGE_SIZE of %ld, aborting.\n",
164 dev_lim->min_page_sz, PAGE_SIZE);
167 if (dev_lim->num_ports > MTHCA_MAX_PORTS) {
168 mthca_err(mdev, "HCA has %d ports, but we only support %d, "
170 dev_lim->num_ports, MTHCA_MAX_PORTS);
174 if (dev_lim->uar_size > pci_resource_len(mdev->pdev, 2)) {
175 mthca_err(mdev, "HCA reported UAR size of 0x%x bigger than "
176 "PCI resource 2 size of 0x%llx, aborting.\n",
178 (unsigned long long)pci_resource_len(mdev->pdev, 2));
182 mdev->limits.num_ports = dev_lim->num_ports;
183 mdev->limits.vl_cap = dev_lim->max_vl;
184 mdev->limits.mtu_cap = dev_lim->max_mtu;
185 mdev->limits.gid_table_len = dev_lim->max_gids;
186 mdev->limits.pkey_table_len = dev_lim->max_pkeys;
187 mdev->limits.local_ca_ack_delay = dev_lim->local_ca_ack_delay;
188 mdev->limits.max_sg = dev_lim->max_sg;
189 mdev->limits.max_wqes = dev_lim->max_qp_sz;
190 mdev->limits.max_qp_init_rdma = dev_lim->max_requester_per_qp;
191 mdev->limits.reserved_qps = dev_lim->reserved_qps;
192 mdev->limits.max_srq_wqes = dev_lim->max_srq_sz;
193 mdev->limits.reserved_srqs = dev_lim->reserved_srqs;
194 mdev->limits.reserved_eecs = dev_lim->reserved_eecs;
195 mdev->limits.max_desc_sz = dev_lim->max_desc_sz;
196 mdev->limits.max_srq_sge = mthca_max_srq_sge(mdev);
198 * Subtract 1 from the limit because we need to allocate a
199 * spare CQE so the HCA HW can tell the difference between an
200 * empty CQ and a full CQ.
202 mdev->limits.max_cqes = dev_lim->max_cq_sz - 1;
203 mdev->limits.reserved_cqs = dev_lim->reserved_cqs;
204 mdev->limits.reserved_eqs = dev_lim->reserved_eqs;
205 mdev->limits.reserved_mtts = dev_lim->reserved_mtts;
206 mdev->limits.reserved_mrws = dev_lim->reserved_mrws;
207 mdev->limits.reserved_uars = dev_lim->reserved_uars;
208 mdev->limits.reserved_pds = dev_lim->reserved_pds;
209 mdev->limits.port_width_cap = dev_lim->max_port_width;
210 mdev->limits.page_size_cap = ~(u32) (dev_lim->min_page_sz - 1);
211 mdev->limits.flags = dev_lim->flags;
213 * For old FW that doesn't return static rate support, use a
214 * value of 0x3 (only static rate values of 0 or 1 are handled),
215 * except on Sinai, where even old FW can handle static rate
218 if (dev_lim->stat_rate_support)
219 mdev->limits.stat_rate_support = dev_lim->stat_rate_support;
220 else if (mdev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
221 mdev->limits.stat_rate_support = 0xf;
223 mdev->limits.stat_rate_support = 0x3;
225 /* IB_DEVICE_RESIZE_MAX_WR not supported by driver.
226 May be doable since hardware supports it for SRQ.
228 IB_DEVICE_N_NOTIFY_CQ is supported by hardware but not by driver.
230 IB_DEVICE_SRQ_RESIZE is supported by hardware but SRQ is not
231 supported by driver. */
232 mdev->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
233 IB_DEVICE_PORT_ACTIVE_EVENT |
234 IB_DEVICE_SYS_IMAGE_GUID |
235 IB_DEVICE_RC_RNR_NAK_GEN;
237 if (dev_lim->flags & DEV_LIM_FLAG_BAD_PKEY_CNTR)
238 mdev->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
240 if (dev_lim->flags & DEV_LIM_FLAG_BAD_QKEY_CNTR)
241 mdev->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
243 if (dev_lim->flags & DEV_LIM_FLAG_RAW_MULTI)
244 mdev->device_cap_flags |= IB_DEVICE_RAW_MULTI;
246 if (dev_lim->flags & DEV_LIM_FLAG_AUTO_PATH_MIG)
247 mdev->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
249 if (dev_lim->flags & DEV_LIM_FLAG_UD_AV_PORT_ENFORCE)
250 mdev->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
252 if (dev_lim->flags & DEV_LIM_FLAG_SRQ)
253 mdev->mthca_flags |= MTHCA_FLAG_SRQ;
258 static int __devinit mthca_init_tavor(struct mthca_dev *mdev)
262 struct mthca_dev_lim dev_lim;
263 struct mthca_profile profile;
264 struct mthca_init_hca_param init_hca;
266 err = mthca_SYS_EN(mdev, &status);
268 mthca_err(mdev, "SYS_EN command failed, aborting.\n");
272 mthca_err(mdev, "SYS_EN returned status 0x%02x, "
273 "aborting.\n", status);
277 err = mthca_QUERY_FW(mdev, &status);
279 mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
283 mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
284 "aborting.\n", status);
288 err = mthca_QUERY_DDR(mdev, &status);
290 mthca_err(mdev, "QUERY_DDR command failed, aborting.\n");
294 mthca_err(mdev, "QUERY_DDR returned status 0x%02x, "
295 "aborting.\n", status);
300 err = mthca_dev_lim(mdev, &dev_lim);
302 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
306 profile = default_profile;
307 profile.num_uar = dev_lim.uar_size / PAGE_SIZE;
308 profile.uarc_size = 0;
309 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
310 profile.num_srq = dev_lim.max_srqs;
312 err = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
316 err = mthca_INIT_HCA(mdev, &init_hca, &status);
318 mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
322 mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
323 "aborting.\n", status);
331 mthca_SYS_DIS(mdev, &status);
336 static int __devinit mthca_load_fw(struct mthca_dev *mdev)
341 /* FIXME: use HCA-attached memory for FW if present */
343 mdev->fw.arbel.fw_icm =
344 mthca_alloc_icm(mdev, mdev->fw.arbel.fw_pages,
345 GFP_HIGHUSER | __GFP_NOWARN);
346 if (!mdev->fw.arbel.fw_icm) {
347 mthca_err(mdev, "Couldn't allocate FW area, aborting.\n");
351 err = mthca_MAP_FA(mdev, mdev->fw.arbel.fw_icm, &status);
353 mthca_err(mdev, "MAP_FA command failed, aborting.\n");
357 mthca_err(mdev, "MAP_FA returned status 0x%02x, aborting.\n", status);
361 err = mthca_RUN_FW(mdev, &status);
363 mthca_err(mdev, "RUN_FW command failed, aborting.\n");
367 mthca_err(mdev, "RUN_FW returned status 0x%02x, aborting.\n", status);
375 mthca_UNMAP_FA(mdev, &status);
378 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
382 static int __devinit mthca_init_icm(struct mthca_dev *mdev,
383 struct mthca_dev_lim *dev_lim,
384 struct mthca_init_hca_param *init_hca,
391 err = mthca_SET_ICM_SIZE(mdev, icm_size, &aux_pages, &status);
393 mthca_err(mdev, "SET_ICM_SIZE command failed, aborting.\n");
397 mthca_err(mdev, "SET_ICM_SIZE returned status 0x%02x, "
398 "aborting.\n", status);
402 mthca_dbg(mdev, "%lld KB of HCA context requires %lld KB aux memory.\n",
403 (unsigned long long) icm_size >> 10,
404 (unsigned long long) aux_pages << 2);
406 mdev->fw.arbel.aux_icm = mthca_alloc_icm(mdev, aux_pages,
407 GFP_HIGHUSER | __GFP_NOWARN);
408 if (!mdev->fw.arbel.aux_icm) {
409 mthca_err(mdev, "Couldn't allocate aux memory, aborting.\n");
413 err = mthca_MAP_ICM_AUX(mdev, mdev->fw.arbel.aux_icm, &status);
415 mthca_err(mdev, "MAP_ICM_AUX command failed, aborting.\n");
419 mthca_err(mdev, "MAP_ICM_AUX returned status 0x%02x, aborting.\n", status);
424 err = mthca_map_eq_icm(mdev, init_hca->eqc_base);
426 mthca_err(mdev, "Failed to map EQ context memory, aborting.\n");
430 mdev->mr_table.mtt_table = mthca_alloc_icm_table(mdev, init_hca->mtt_base,
432 mdev->limits.num_mtt_segs,
433 mdev->limits.reserved_mtts, 1);
434 if (!mdev->mr_table.mtt_table) {
435 mthca_err(mdev, "Failed to map MTT context memory, aborting.\n");
440 mdev->mr_table.mpt_table = mthca_alloc_icm_table(mdev, init_hca->mpt_base,
441 dev_lim->mpt_entry_sz,
442 mdev->limits.num_mpts,
443 mdev->limits.reserved_mrws, 1);
444 if (!mdev->mr_table.mpt_table) {
445 mthca_err(mdev, "Failed to map MPT context memory, aborting.\n");
450 mdev->qp_table.qp_table = mthca_alloc_icm_table(mdev, init_hca->qpc_base,
451 dev_lim->qpc_entry_sz,
452 mdev->limits.num_qps,
453 mdev->limits.reserved_qps, 0);
454 if (!mdev->qp_table.qp_table) {
455 mthca_err(mdev, "Failed to map QP context memory, aborting.\n");
460 mdev->qp_table.eqp_table = mthca_alloc_icm_table(mdev, init_hca->eqpc_base,
461 dev_lim->eqpc_entry_sz,
462 mdev->limits.num_qps,
463 mdev->limits.reserved_qps, 0);
464 if (!mdev->qp_table.eqp_table) {
465 mthca_err(mdev, "Failed to map EQP context memory, aborting.\n");
470 mdev->qp_table.rdb_table = mthca_alloc_icm_table(mdev, init_hca->rdb_base,
471 MTHCA_RDB_ENTRY_SIZE,
472 mdev->limits.num_qps <<
473 mdev->qp_table.rdb_shift,
475 if (!mdev->qp_table.rdb_table) {
476 mthca_err(mdev, "Failed to map RDB context memory, aborting\n");
481 mdev->cq_table.table = mthca_alloc_icm_table(mdev, init_hca->cqc_base,
482 dev_lim->cqc_entry_sz,
483 mdev->limits.num_cqs,
484 mdev->limits.reserved_cqs, 0);
485 if (!mdev->cq_table.table) {
486 mthca_err(mdev, "Failed to map CQ context memory, aborting.\n");
491 if (mdev->mthca_flags & MTHCA_FLAG_SRQ) {
492 mdev->srq_table.table =
493 mthca_alloc_icm_table(mdev, init_hca->srqc_base,
494 dev_lim->srq_entry_sz,
495 mdev->limits.num_srqs,
496 mdev->limits.reserved_srqs, 0);
497 if (!mdev->srq_table.table) {
498 mthca_err(mdev, "Failed to map SRQ context memory, "
506 * It's not strictly required, but for simplicity just map the
507 * whole multicast group table now. The table isn't very big
508 * and it's a lot easier than trying to track ref counts.
510 mdev->mcg_table.table = mthca_alloc_icm_table(mdev, init_hca->mc_base,
511 MTHCA_MGM_ENTRY_SIZE,
512 mdev->limits.num_mgms +
513 mdev->limits.num_amgms,
514 mdev->limits.num_mgms +
515 mdev->limits.num_amgms,
517 if (!mdev->mcg_table.table) {
518 mthca_err(mdev, "Failed to map MCG context memory, aborting.\n");
526 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
527 mthca_free_icm_table(mdev, mdev->srq_table.table);
530 mthca_free_icm_table(mdev, mdev->cq_table.table);
533 mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
536 mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
539 mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
542 mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
545 mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
548 mthca_unmap_eq_icm(mdev);
551 mthca_UNMAP_ICM_AUX(mdev, &status);
554 mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
559 static void mthca_free_icms(struct mthca_dev *mdev)
563 mthca_free_icm_table(mdev, mdev->mcg_table.table);
564 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
565 mthca_free_icm_table(mdev, mdev->srq_table.table);
566 mthca_free_icm_table(mdev, mdev->cq_table.table);
567 mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
568 mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
569 mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
570 mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
571 mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
572 mthca_unmap_eq_icm(mdev);
574 mthca_UNMAP_ICM_AUX(mdev, &status);
575 mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
578 static int __devinit mthca_init_arbel(struct mthca_dev *mdev)
580 struct mthca_dev_lim dev_lim;
581 struct mthca_profile profile;
582 struct mthca_init_hca_param init_hca;
587 err = mthca_QUERY_FW(mdev, &status);
589 mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
593 mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
594 "aborting.\n", status);
598 err = mthca_ENABLE_LAM(mdev, &status);
600 mthca_err(mdev, "ENABLE_LAM command failed, aborting.\n");
603 if (status == MTHCA_CMD_STAT_LAM_NOT_PRE) {
604 mthca_dbg(mdev, "No HCA-attached memory (running in MemFree mode)\n");
605 mdev->mthca_flags |= MTHCA_FLAG_NO_LAM;
607 mthca_err(mdev, "ENABLE_LAM returned status 0x%02x, "
608 "aborting.\n", status);
612 err = mthca_load_fw(mdev);
614 mthca_err(mdev, "Failed to start FW, aborting.\n");
618 err = mthca_dev_lim(mdev, &dev_lim);
620 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
624 profile = default_profile;
625 profile.num_uar = dev_lim.uar_size / PAGE_SIZE;
626 profile.num_udav = 0;
627 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
628 profile.num_srq = dev_lim.max_srqs;
630 icm_size = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
631 if ((int) icm_size < 0) {
636 err = mthca_init_icm(mdev, &dev_lim, &init_hca, icm_size);
640 err = mthca_INIT_HCA(mdev, &init_hca, &status);
642 mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
646 mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
647 "aborting.\n", status);
655 mthca_free_icms(mdev);
658 mthca_UNMAP_FA(mdev, &status);
659 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
662 if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
663 mthca_DISABLE_LAM(mdev, &status);
668 static void mthca_close_hca(struct mthca_dev *mdev)
672 mthca_CLOSE_HCA(mdev, 0, &status);
674 if (mthca_is_memfree(mdev)) {
675 mthca_free_icms(mdev);
677 mthca_UNMAP_FA(mdev, &status);
678 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
680 if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
681 mthca_DISABLE_LAM(mdev, &status);
683 mthca_SYS_DIS(mdev, &status);
686 static int __devinit mthca_init_hca(struct mthca_dev *mdev)
690 struct mthca_adapter adapter;
692 if (mthca_is_memfree(mdev))
693 err = mthca_init_arbel(mdev);
695 err = mthca_init_tavor(mdev);
700 err = mthca_QUERY_ADAPTER(mdev, &adapter, &status);
702 mthca_err(mdev, "QUERY_ADAPTER command failed, aborting.\n");
706 mthca_err(mdev, "QUERY_ADAPTER returned status 0x%02x, "
707 "aborting.\n", status);
712 mdev->eq_table.inta_pin = adapter.inta_pin;
713 mdev->rev_id = adapter.revision_id;
714 memcpy(mdev->board_id, adapter.board_id, sizeof mdev->board_id);
719 mthca_close_hca(mdev);
723 static int __devinit mthca_setup_hca(struct mthca_dev *dev)
728 MTHCA_INIT_DOORBELL_LOCK(&dev->doorbell_lock);
730 err = mthca_init_uar_table(dev);
732 mthca_err(dev, "Failed to initialize "
733 "user access region table, aborting.\n");
737 err = mthca_uar_alloc(dev, &dev->driver_uar);
739 mthca_err(dev, "Failed to allocate driver access region, "
741 goto err_uar_table_free;
744 dev->kar = ioremap(dev->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
746 mthca_err(dev, "Couldn't map kernel access region, "
752 err = mthca_init_pd_table(dev);
754 mthca_err(dev, "Failed to initialize "
755 "protection domain table, aborting.\n");
759 err = mthca_init_mr_table(dev);
761 mthca_err(dev, "Failed to initialize "
762 "memory region table, aborting.\n");
763 goto err_pd_table_free;
766 err = mthca_pd_alloc(dev, 1, &dev->driver_pd);
768 mthca_err(dev, "Failed to create driver PD, "
770 goto err_mr_table_free;
773 err = mthca_init_eq_table(dev);
775 mthca_err(dev, "Failed to initialize "
776 "event queue table, aborting.\n");
780 err = mthca_cmd_use_events(dev);
782 mthca_err(dev, "Failed to switch to event-driven "
783 "firmware commands, aborting.\n");
784 goto err_eq_table_free;
787 err = mthca_NOP(dev, &status);
789 mthca_err(dev, "NOP command failed to generate interrupt (IRQ %d), aborting.\n",
790 dev->mthca_flags & MTHCA_FLAG_MSI_X ?
791 dev->eq_table.eq[MTHCA_EQ_CMD].msi_x_vector :
793 if (dev->mthca_flags & (MTHCA_FLAG_MSI | MTHCA_FLAG_MSI_X))
794 mthca_err(dev, "Try again with MSI/MSI-X disabled.\n");
796 mthca_err(dev, "BIOS or ACPI interrupt routing problem?\n");
801 mthca_dbg(dev, "NOP command IRQ test passed\n");
803 err = mthca_init_cq_table(dev);
805 mthca_err(dev, "Failed to initialize "
806 "completion queue table, aborting.\n");
810 err = mthca_init_srq_table(dev);
812 mthca_err(dev, "Failed to initialize "
813 "shared receive queue table, aborting.\n");
814 goto err_cq_table_free;
817 err = mthca_init_qp_table(dev);
819 mthca_err(dev, "Failed to initialize "
820 "queue pair table, aborting.\n");
821 goto err_srq_table_free;
824 err = mthca_init_av_table(dev);
826 mthca_err(dev, "Failed to initialize "
827 "address vector table, aborting.\n");
828 goto err_qp_table_free;
831 err = mthca_init_mcg_table(dev);
833 mthca_err(dev, "Failed to initialize "
834 "multicast group table, aborting.\n");
835 goto err_av_table_free;
841 mthca_cleanup_av_table(dev);
844 mthca_cleanup_qp_table(dev);
847 mthca_cleanup_srq_table(dev);
850 mthca_cleanup_cq_table(dev);
853 mthca_cmd_use_polling(dev);
856 mthca_cleanup_eq_table(dev);
859 mthca_pd_free(dev, &dev->driver_pd);
862 mthca_cleanup_mr_table(dev);
865 mthca_cleanup_pd_table(dev);
871 mthca_uar_free(dev, &dev->driver_uar);
874 mthca_cleanup_uar_table(dev);
878 static int __devinit mthca_request_regions(struct pci_dev *pdev,
884 * We can't just use pci_request_regions() because the MSI-X
885 * table is right in the middle of the first BAR. If we did
886 * pci_request_region and grab all of the first BAR, then
887 * setting up MSI-X would fail, since the PCI core wants to do
888 * request_mem_region on the MSI-X vector table.
890 * So just request what we need right now, and request any
891 * other regions we need when setting up EQs.
893 if (!request_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
894 MTHCA_HCR_SIZE, DRV_NAME))
897 err = pci_request_region(pdev, 2, DRV_NAME);
899 goto err_bar2_failed;
902 err = pci_request_region(pdev, 4, DRV_NAME);
904 goto err_bar4_failed;
910 pci_release_region(pdev, 2);
913 release_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
919 static void mthca_release_regions(struct pci_dev *pdev,
923 pci_release_region(pdev, 4);
925 pci_release_region(pdev, 2);
927 release_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
931 static int __devinit mthca_enable_msi_x(struct mthca_dev *mdev)
933 struct msix_entry entries[3];
936 entries[0].entry = 0;
937 entries[1].entry = 1;
938 entries[2].entry = 2;
940 err = pci_enable_msix(mdev->pdev, entries, ARRAY_SIZE(entries));
943 mthca_info(mdev, "Only %d MSI-X vectors available, "
944 "not using MSI-X\n", err);
948 mdev->eq_table.eq[MTHCA_EQ_COMP ].msi_x_vector = entries[0].vector;
949 mdev->eq_table.eq[MTHCA_EQ_ASYNC].msi_x_vector = entries[1].vector;
950 mdev->eq_table.eq[MTHCA_EQ_CMD ].msi_x_vector = entries[2].vector;
955 /* Types of supported HCA */
958 ARBEL_COMPAT, /* MT25208 in Tavor compat mode */
959 ARBEL_NATIVE, /* MT25208 with extended features */
963 #define MTHCA_FW_VER(major, minor, subminor) \
964 (((u64) (major) << 32) | ((u64) (minor) << 16) | (u64) (subminor))
969 } mthca_hca_table[] = {
970 [TAVOR] = { .latest_fw = MTHCA_FW_VER(3, 4, 0),
972 [ARBEL_COMPAT] = { .latest_fw = MTHCA_FW_VER(4, 7, 600),
973 .flags = MTHCA_FLAG_PCIE },
974 [ARBEL_NATIVE] = { .latest_fw = MTHCA_FW_VER(5, 1, 400),
975 .flags = MTHCA_FLAG_MEMFREE |
977 [SINAI] = { .latest_fw = MTHCA_FW_VER(1, 1, 0),
978 .flags = MTHCA_FLAG_MEMFREE |
980 MTHCA_FLAG_SINAI_OPT }
983 static int __mthca_init_one(struct pci_dev *pdev, int hca_type)
987 struct mthca_dev *mdev;
989 printk(KERN_INFO PFX "Initializing %s\n",
992 err = pci_enable_device(pdev);
994 dev_err(&pdev->dev, "Cannot enable PCI device, "
1000 * Check for BARs. We expect 0: 1MB, 2: 8MB, 4: DDR (may not
1003 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
1004 pci_resource_len(pdev, 0) != 1 << 20) {
1005 dev_err(&pdev->dev, "Missing DCS, aborting.\n");
1007 goto err_disable_pdev;
1009 if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
1010 dev_err(&pdev->dev, "Missing UAR, aborting.\n");
1012 goto err_disable_pdev;
1014 if (!(pci_resource_flags(pdev, 4) & IORESOURCE_MEM))
1017 err = mthca_request_regions(pdev, ddr_hidden);
1019 dev_err(&pdev->dev, "Cannot obtain PCI resources, "
1021 goto err_disable_pdev;
1024 pci_set_master(pdev);
1026 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
1028 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask.\n");
1029 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1031 dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting.\n");
1035 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
1037 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit "
1038 "consistent PCI DMA mask.\n");
1039 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
1041 dev_err(&pdev->dev, "Can't set consistent PCI DMA mask, "
1047 mdev = (struct mthca_dev *) ib_alloc_device(sizeof *mdev);
1049 dev_err(&pdev->dev, "Device struct alloc failed, "
1057 mdev->mthca_flags = mthca_hca_table[hca_type].flags;
1059 mdev->mthca_flags |= MTHCA_FLAG_DDR_HIDDEN;
1062 * Now reset the HCA before we touch the PCI capabilities or
1063 * attempt a firmware command, since a boot ROM may have left
1064 * the HCA in an undefined state.
1066 err = mthca_reset(mdev);
1068 mthca_err(mdev, "Failed to reset HCA, aborting.\n");
1072 if (msi_x && !mthca_enable_msi_x(mdev))
1073 mdev->mthca_flags |= MTHCA_FLAG_MSI_X;
1074 if (msi && !(mdev->mthca_flags & MTHCA_FLAG_MSI_X) &&
1075 !pci_enable_msi(pdev))
1076 mdev->mthca_flags |= MTHCA_FLAG_MSI;
1078 if (mthca_cmd_init(mdev)) {
1079 mthca_err(mdev, "Failed to init command interface, aborting.\n");
1083 err = mthca_tune_pci(mdev);
1087 err = mthca_init_hca(mdev);
1091 if (mdev->fw_ver < mthca_hca_table[hca_type].latest_fw) {
1092 mthca_warn(mdev, "HCA FW version %d.%d.%d is old (%d.%d.%d is current).\n",
1093 (int) (mdev->fw_ver >> 32), (int) (mdev->fw_ver >> 16) & 0xffff,
1094 (int) (mdev->fw_ver & 0xffff),
1095 (int) (mthca_hca_table[hca_type].latest_fw >> 32),
1096 (int) (mthca_hca_table[hca_type].latest_fw >> 16) & 0xffff,
1097 (int) (mthca_hca_table[hca_type].latest_fw & 0xffff));
1098 mthca_warn(mdev, "If you have problems, try updating your HCA FW.\n");
1101 err = mthca_setup_hca(mdev);
1105 err = mthca_register_device(mdev);
1109 err = mthca_create_agents(mdev);
1111 goto err_unregister;
1113 pci_set_drvdata(pdev, mdev);
1114 mdev->hca_type = hca_type;
1119 mthca_unregister_device(mdev);
1122 mthca_cleanup_mcg_table(mdev);
1123 mthca_cleanup_av_table(mdev);
1124 mthca_cleanup_qp_table(mdev);
1125 mthca_cleanup_srq_table(mdev);
1126 mthca_cleanup_cq_table(mdev);
1127 mthca_cmd_use_polling(mdev);
1128 mthca_cleanup_eq_table(mdev);
1130 mthca_pd_free(mdev, &mdev->driver_pd);
1132 mthca_cleanup_mr_table(mdev);
1133 mthca_cleanup_pd_table(mdev);
1134 mthca_cleanup_uar_table(mdev);
1137 mthca_close_hca(mdev);
1140 mthca_cmd_cleanup(mdev);
1143 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1144 pci_disable_msix(pdev);
1145 if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1146 pci_disable_msi(pdev);
1148 ib_dealloc_device(&mdev->ib_dev);
1151 mthca_release_regions(pdev, ddr_hidden);
1154 pci_disable_device(pdev);
1155 pci_set_drvdata(pdev, NULL);
1159 static void __mthca_remove_one(struct pci_dev *pdev)
1161 struct mthca_dev *mdev = pci_get_drvdata(pdev);
1166 mthca_free_agents(mdev);
1167 mthca_unregister_device(mdev);
1169 for (p = 1; p <= mdev->limits.num_ports; ++p)
1170 mthca_CLOSE_IB(mdev, p, &status);
1172 mthca_cleanup_mcg_table(mdev);
1173 mthca_cleanup_av_table(mdev);
1174 mthca_cleanup_qp_table(mdev);
1175 mthca_cleanup_srq_table(mdev);
1176 mthca_cleanup_cq_table(mdev);
1177 mthca_cmd_use_polling(mdev);
1178 mthca_cleanup_eq_table(mdev);
1180 mthca_pd_free(mdev, &mdev->driver_pd);
1182 mthca_cleanup_mr_table(mdev);
1183 mthca_cleanup_pd_table(mdev);
1186 mthca_uar_free(mdev, &mdev->driver_uar);
1187 mthca_cleanup_uar_table(mdev);
1188 mthca_close_hca(mdev);
1189 mthca_cmd_cleanup(mdev);
1191 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1192 pci_disable_msix(pdev);
1193 if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1194 pci_disable_msi(pdev);
1196 ib_dealloc_device(&mdev->ib_dev);
1197 mthca_release_regions(pdev, mdev->mthca_flags &
1198 MTHCA_FLAG_DDR_HIDDEN);
1199 pci_disable_device(pdev);
1200 pci_set_drvdata(pdev, NULL);
1204 int __mthca_restart_one(struct pci_dev *pdev)
1206 struct mthca_dev *mdev;
1208 mdev = pci_get_drvdata(pdev);
1211 __mthca_remove_one(pdev);
1212 return __mthca_init_one(pdev, mdev->hca_type);
1215 static int __devinit mthca_init_one(struct pci_dev *pdev,
1216 const struct pci_device_id *id)
1218 static int mthca_version_printed = 0;
1221 mutex_lock(&mthca_device_mutex);
1223 if (!mthca_version_printed) {
1224 printk(KERN_INFO "%s", mthca_version);
1225 ++mthca_version_printed;
1228 if (id->driver_data >= ARRAY_SIZE(mthca_hca_table)) {
1229 printk(KERN_ERR PFX "%s has invalid driver data %lx\n",
1230 pci_name(pdev), id->driver_data);
1231 mutex_unlock(&mthca_device_mutex);
1235 ret = __mthca_init_one(pdev, id->driver_data);
1237 mutex_unlock(&mthca_device_mutex);
1242 static void __devexit mthca_remove_one(struct pci_dev *pdev)
1244 mutex_lock(&mthca_device_mutex);
1245 __mthca_remove_one(pdev);
1246 mutex_unlock(&mthca_device_mutex);
1249 static struct pci_device_id mthca_pci_table[] = {
1250 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_TAVOR),
1251 .driver_data = TAVOR },
1252 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_TAVOR),
1253 .driver_data = TAVOR },
1254 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
1255 .driver_data = ARBEL_COMPAT },
1256 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
1257 .driver_data = ARBEL_COMPAT },
1258 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL),
1259 .driver_data = ARBEL_NATIVE },
1260 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL),
1261 .driver_data = ARBEL_NATIVE },
1262 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI),
1263 .driver_data = SINAI },
1264 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI),
1265 .driver_data = SINAI },
1266 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
1267 .driver_data = SINAI },
1268 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
1269 .driver_data = SINAI },
1273 MODULE_DEVICE_TABLE(pci, mthca_pci_table);
1275 static struct pci_driver mthca_driver = {
1277 .id_table = mthca_pci_table,
1278 .probe = mthca_init_one,
1279 .remove = __devexit_p(mthca_remove_one)
1282 static int __init mthca_init(void)
1286 mutex_init(&mthca_device_mutex);
1287 ret = mthca_catas_init();
1291 ret = pci_register_driver(&mthca_driver);
1293 mthca_catas_cleanup();
1300 static void __exit mthca_cleanup(void)
1302 pci_unregister_driver(&mthca_driver);
1303 mthca_catas_cleanup();
1306 module_init(mthca_init);
1307 module_exit(mthca_cleanup);