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/module.h>
39 #include <linux/init.h>
40 #include <linux/errno.h>
41 #include <linux/pci.h>
42 #include <linux/interrupt.h>
44 #include "mthca_dev.h"
45 #include "mthca_config_reg.h"
46 #include "mthca_cmd.h"
47 #include "mthca_profile.h"
48 #include "mthca_memfree.h"
50 MODULE_AUTHOR("Roland Dreier");
51 MODULE_DESCRIPTION("Mellanox InfiniBand HCA low-level driver");
52 MODULE_LICENSE("Dual BSD/GPL");
53 MODULE_VERSION(DRV_VERSION);
55 #ifdef CONFIG_INFINIBAND_MTHCA_DEBUG
57 int mthca_debug_level = 0;
58 module_param_named(debug_level, mthca_debug_level, int, 0644);
59 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
61 #endif /* CONFIG_INFINIBAND_MTHCA_DEBUG */
66 module_param(msi_x, int, 0444);
67 MODULE_PARM_DESC(msi_x, "attempt to use MSI-X if nonzero");
70 module_param(msi, int, 0444);
71 MODULE_PARM_DESC(msi, "attempt to use MSI if nonzero");
73 #else /* CONFIG_PCI_MSI */
78 #endif /* CONFIG_PCI_MSI */
80 static const char mthca_version[] __devinitdata =
81 DRV_NAME ": Mellanox InfiniBand HCA driver v"
82 DRV_VERSION " (" DRV_RELDATE ")\n";
84 static struct mthca_profile default_profile = {
91 .num_udav = 1 << 15, /* Tavor only */
92 .fmr_reserved_mtts = 1 << 18, /* Tavor only */
93 .uarc_size = 1 << 18, /* Arbel only */
96 static int __devinit mthca_tune_pci(struct mthca_dev *mdev)
101 /* First try to max out Read Byte Count */
102 cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_PCIX);
104 if (pci_read_config_word(mdev->pdev, cap + PCI_X_CMD, &val)) {
105 mthca_err(mdev, "Couldn't read PCI-X command register, "
109 val = (val & ~PCI_X_CMD_MAX_READ) | (3 << 2);
110 if (pci_write_config_word(mdev->pdev, cap + PCI_X_CMD, val)) {
111 mthca_err(mdev, "Couldn't write PCI-X command register, "
115 } else if (!(mdev->mthca_flags & MTHCA_FLAG_PCIE))
116 mthca_info(mdev, "No PCI-X capability, not setting RBC.\n");
118 cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_EXP);
120 if (pci_read_config_word(mdev->pdev, cap + PCI_EXP_DEVCTL, &val)) {
121 mthca_err(mdev, "Couldn't read PCI Express device control "
122 "register, aborting.\n");
125 val = (val & ~PCI_EXP_DEVCTL_READRQ) | (5 << 12);
126 if (pci_write_config_word(mdev->pdev, cap + PCI_EXP_DEVCTL, val)) {
127 mthca_err(mdev, "Couldn't write PCI Express device control "
128 "register, aborting.\n");
131 } else if (mdev->mthca_flags & MTHCA_FLAG_PCIE)
132 mthca_info(mdev, "No PCI Express capability, "
133 "not setting Max Read Request Size.\n");
138 static int __devinit mthca_dev_lim(struct mthca_dev *mdev, struct mthca_dev_lim *dev_lim)
143 err = mthca_QUERY_DEV_LIM(mdev, dev_lim, &status);
145 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
149 mthca_err(mdev, "QUERY_DEV_LIM returned status 0x%02x, "
150 "aborting.\n", status);
153 if (dev_lim->min_page_sz > PAGE_SIZE) {
154 mthca_err(mdev, "HCA minimum page size of %d bigger than "
155 "kernel PAGE_SIZE of %ld, aborting.\n",
156 dev_lim->min_page_sz, PAGE_SIZE);
159 if (dev_lim->num_ports > MTHCA_MAX_PORTS) {
160 mthca_err(mdev, "HCA has %d ports, but we only support %d, "
162 dev_lim->num_ports, MTHCA_MAX_PORTS);
166 if (dev_lim->uar_size > pci_resource_len(mdev->pdev, 2)) {
167 mthca_err(mdev, "HCA reported UAR size of 0x%x bigger than "
168 "PCI resource 2 size of 0x%lx, aborting.\n",
169 dev_lim->uar_size, pci_resource_len(mdev->pdev, 2));
173 mdev->limits.num_ports = dev_lim->num_ports;
174 mdev->limits.vl_cap = dev_lim->max_vl;
175 mdev->limits.mtu_cap = dev_lim->max_mtu;
176 mdev->limits.gid_table_len = dev_lim->max_gids;
177 mdev->limits.pkey_table_len = dev_lim->max_pkeys;
178 mdev->limits.local_ca_ack_delay = dev_lim->local_ca_ack_delay;
179 mdev->limits.max_sg = dev_lim->max_sg;
180 mdev->limits.max_wqes = dev_lim->max_qp_sz;
181 mdev->limits.max_qp_init_rdma = dev_lim->max_requester_per_qp;
182 mdev->limits.reserved_qps = dev_lim->reserved_qps;
183 mdev->limits.max_srq_wqes = dev_lim->max_srq_sz;
184 mdev->limits.reserved_srqs = dev_lim->reserved_srqs;
185 mdev->limits.reserved_eecs = dev_lim->reserved_eecs;
186 mdev->limits.max_desc_sz = dev_lim->max_desc_sz;
188 * Subtract 1 from the limit because we need to allocate a
189 * spare CQE so the HCA HW can tell the difference between an
190 * empty CQ and a full CQ.
192 mdev->limits.max_cqes = dev_lim->max_cq_sz - 1;
193 mdev->limits.reserved_cqs = dev_lim->reserved_cqs;
194 mdev->limits.reserved_eqs = dev_lim->reserved_eqs;
195 mdev->limits.reserved_mtts = dev_lim->reserved_mtts;
196 mdev->limits.reserved_mrws = dev_lim->reserved_mrws;
197 mdev->limits.reserved_uars = dev_lim->reserved_uars;
198 mdev->limits.reserved_pds = dev_lim->reserved_pds;
199 mdev->limits.port_width_cap = dev_lim->max_port_width;
200 mdev->limits.page_size_cap = ~(u32) (dev_lim->min_page_sz - 1);
201 mdev->limits.flags = dev_lim->flags;
203 * For old FW that doesn't return static rate support, use a
204 * value of 0x3 (only static rate values of 0 or 1 are handled),
205 * except on Sinai, where even old FW can handle static rate
208 if (dev_lim->stat_rate_support)
209 mdev->limits.stat_rate_support = dev_lim->stat_rate_support;
210 else if (mdev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
211 mdev->limits.stat_rate_support = 0xf;
213 mdev->limits.stat_rate_support = 0x3;
215 /* IB_DEVICE_RESIZE_MAX_WR not supported by driver.
216 May be doable since hardware supports it for SRQ.
218 IB_DEVICE_N_NOTIFY_CQ is supported by hardware but not by driver.
220 IB_DEVICE_SRQ_RESIZE is supported by hardware but SRQ is not
221 supported by driver. */
222 mdev->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
223 IB_DEVICE_PORT_ACTIVE_EVENT |
224 IB_DEVICE_SYS_IMAGE_GUID |
225 IB_DEVICE_RC_RNR_NAK_GEN;
227 if (dev_lim->flags & DEV_LIM_FLAG_BAD_PKEY_CNTR)
228 mdev->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
230 if (dev_lim->flags & DEV_LIM_FLAG_BAD_QKEY_CNTR)
231 mdev->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
233 if (dev_lim->flags & DEV_LIM_FLAG_RAW_MULTI)
234 mdev->device_cap_flags |= IB_DEVICE_RAW_MULTI;
236 if (dev_lim->flags & DEV_LIM_FLAG_AUTO_PATH_MIG)
237 mdev->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
239 if (dev_lim->flags & DEV_LIM_FLAG_UD_AV_PORT_ENFORCE)
240 mdev->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
242 if (dev_lim->flags & DEV_LIM_FLAG_SRQ)
243 mdev->mthca_flags |= MTHCA_FLAG_SRQ;
248 static int __devinit mthca_init_tavor(struct mthca_dev *mdev)
252 struct mthca_dev_lim dev_lim;
253 struct mthca_profile profile;
254 struct mthca_init_hca_param init_hca;
256 err = mthca_SYS_EN(mdev, &status);
258 mthca_err(mdev, "SYS_EN command failed, aborting.\n");
262 mthca_err(mdev, "SYS_EN returned status 0x%02x, "
263 "aborting.\n", status);
267 err = mthca_QUERY_FW(mdev, &status);
269 mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
273 mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
274 "aborting.\n", status);
278 err = mthca_QUERY_DDR(mdev, &status);
280 mthca_err(mdev, "QUERY_DDR command failed, aborting.\n");
284 mthca_err(mdev, "QUERY_DDR returned status 0x%02x, "
285 "aborting.\n", status);
290 err = mthca_dev_lim(mdev, &dev_lim);
292 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
296 profile = default_profile;
297 profile.num_uar = dev_lim.uar_size / PAGE_SIZE;
298 profile.uarc_size = 0;
299 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
300 profile.num_srq = dev_lim.max_srqs;
302 err = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
306 err = mthca_INIT_HCA(mdev, &init_hca, &status);
308 mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
312 mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
313 "aborting.\n", status);
321 mthca_SYS_DIS(mdev, &status);
326 static int __devinit mthca_load_fw(struct mthca_dev *mdev)
331 /* FIXME: use HCA-attached memory for FW if present */
333 mdev->fw.arbel.fw_icm =
334 mthca_alloc_icm(mdev, mdev->fw.arbel.fw_pages,
335 GFP_HIGHUSER | __GFP_NOWARN);
336 if (!mdev->fw.arbel.fw_icm) {
337 mthca_err(mdev, "Couldn't allocate FW area, aborting.\n");
341 err = mthca_MAP_FA(mdev, mdev->fw.arbel.fw_icm, &status);
343 mthca_err(mdev, "MAP_FA command failed, aborting.\n");
347 mthca_err(mdev, "MAP_FA returned status 0x%02x, aborting.\n", status);
351 err = mthca_RUN_FW(mdev, &status);
353 mthca_err(mdev, "RUN_FW command failed, aborting.\n");
357 mthca_err(mdev, "RUN_FW returned status 0x%02x, aborting.\n", status);
365 mthca_UNMAP_FA(mdev, &status);
368 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
372 static int __devinit mthca_init_icm(struct mthca_dev *mdev,
373 struct mthca_dev_lim *dev_lim,
374 struct mthca_init_hca_param *init_hca,
381 err = mthca_SET_ICM_SIZE(mdev, icm_size, &aux_pages, &status);
383 mthca_err(mdev, "SET_ICM_SIZE command failed, aborting.\n");
387 mthca_err(mdev, "SET_ICM_SIZE returned status 0x%02x, "
388 "aborting.\n", status);
392 mthca_dbg(mdev, "%lld KB of HCA context requires %lld KB aux memory.\n",
393 (unsigned long long) icm_size >> 10,
394 (unsigned long long) aux_pages << 2);
396 mdev->fw.arbel.aux_icm = mthca_alloc_icm(mdev, aux_pages,
397 GFP_HIGHUSER | __GFP_NOWARN);
398 if (!mdev->fw.arbel.aux_icm) {
399 mthca_err(mdev, "Couldn't allocate aux memory, aborting.\n");
403 err = mthca_MAP_ICM_AUX(mdev, mdev->fw.arbel.aux_icm, &status);
405 mthca_err(mdev, "MAP_ICM_AUX command failed, aborting.\n");
409 mthca_err(mdev, "MAP_ICM_AUX returned status 0x%02x, aborting.\n", status);
414 err = mthca_map_eq_icm(mdev, init_hca->eqc_base);
416 mthca_err(mdev, "Failed to map EQ context memory, aborting.\n");
420 mdev->mr_table.mtt_table = mthca_alloc_icm_table(mdev, init_hca->mtt_base,
422 mdev->limits.num_mtt_segs,
423 mdev->limits.reserved_mtts, 1);
424 if (!mdev->mr_table.mtt_table) {
425 mthca_err(mdev, "Failed to map MTT context memory, aborting.\n");
430 mdev->mr_table.mpt_table = mthca_alloc_icm_table(mdev, init_hca->mpt_base,
431 dev_lim->mpt_entry_sz,
432 mdev->limits.num_mpts,
433 mdev->limits.reserved_mrws, 1);
434 if (!mdev->mr_table.mpt_table) {
435 mthca_err(mdev, "Failed to map MPT context memory, aborting.\n");
440 mdev->qp_table.qp_table = mthca_alloc_icm_table(mdev, init_hca->qpc_base,
441 dev_lim->qpc_entry_sz,
442 mdev->limits.num_qps,
443 mdev->limits.reserved_qps, 0);
444 if (!mdev->qp_table.qp_table) {
445 mthca_err(mdev, "Failed to map QP context memory, aborting.\n");
450 mdev->qp_table.eqp_table = mthca_alloc_icm_table(mdev, init_hca->eqpc_base,
451 dev_lim->eqpc_entry_sz,
452 mdev->limits.num_qps,
453 mdev->limits.reserved_qps, 0);
454 if (!mdev->qp_table.eqp_table) {
455 mthca_err(mdev, "Failed to map EQP context memory, aborting.\n");
460 mdev->qp_table.rdb_table = mthca_alloc_icm_table(mdev, init_hca->rdb_base,
461 MTHCA_RDB_ENTRY_SIZE,
462 mdev->limits.num_qps <<
463 mdev->qp_table.rdb_shift,
465 if (!mdev->qp_table.rdb_table) {
466 mthca_err(mdev, "Failed to map RDB context memory, aborting\n");
471 mdev->cq_table.table = mthca_alloc_icm_table(mdev, init_hca->cqc_base,
472 dev_lim->cqc_entry_sz,
473 mdev->limits.num_cqs,
474 mdev->limits.reserved_cqs, 0);
475 if (!mdev->cq_table.table) {
476 mthca_err(mdev, "Failed to map CQ context memory, aborting.\n");
481 if (mdev->mthca_flags & MTHCA_FLAG_SRQ) {
482 mdev->srq_table.table =
483 mthca_alloc_icm_table(mdev, init_hca->srqc_base,
484 dev_lim->srq_entry_sz,
485 mdev->limits.num_srqs,
486 mdev->limits.reserved_srqs, 0);
487 if (!mdev->srq_table.table) {
488 mthca_err(mdev, "Failed to map SRQ context memory, "
496 * It's not strictly required, but for simplicity just map the
497 * whole multicast group table now. The table isn't very big
498 * and it's a lot easier than trying to track ref counts.
500 mdev->mcg_table.table = mthca_alloc_icm_table(mdev, init_hca->mc_base,
501 MTHCA_MGM_ENTRY_SIZE,
502 mdev->limits.num_mgms +
503 mdev->limits.num_amgms,
504 mdev->limits.num_mgms +
505 mdev->limits.num_amgms,
507 if (!mdev->mcg_table.table) {
508 mthca_err(mdev, "Failed to map MCG context memory, aborting.\n");
516 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
517 mthca_free_icm_table(mdev, mdev->srq_table.table);
520 mthca_free_icm_table(mdev, mdev->cq_table.table);
523 mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
526 mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
529 mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
532 mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
535 mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
538 mthca_unmap_eq_icm(mdev);
541 mthca_UNMAP_ICM_AUX(mdev, &status);
544 mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
549 static void mthca_free_icms(struct mthca_dev *mdev)
553 mthca_free_icm_table(mdev, mdev->mcg_table.table);
554 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
555 mthca_free_icm_table(mdev, mdev->srq_table.table);
556 mthca_free_icm_table(mdev, mdev->cq_table.table);
557 mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
558 mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
559 mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
560 mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
561 mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
562 mthca_unmap_eq_icm(mdev);
564 mthca_UNMAP_ICM_AUX(mdev, &status);
565 mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
568 static int __devinit mthca_init_arbel(struct mthca_dev *mdev)
570 struct mthca_dev_lim dev_lim;
571 struct mthca_profile profile;
572 struct mthca_init_hca_param init_hca;
577 err = mthca_QUERY_FW(mdev, &status);
579 mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
583 mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
584 "aborting.\n", status);
588 err = mthca_ENABLE_LAM(mdev, &status);
590 mthca_err(mdev, "ENABLE_LAM command failed, aborting.\n");
593 if (status == MTHCA_CMD_STAT_LAM_NOT_PRE) {
594 mthca_dbg(mdev, "No HCA-attached memory (running in MemFree mode)\n");
595 mdev->mthca_flags |= MTHCA_FLAG_NO_LAM;
597 mthca_err(mdev, "ENABLE_LAM returned status 0x%02x, "
598 "aborting.\n", status);
602 err = mthca_load_fw(mdev);
604 mthca_err(mdev, "Failed to start FW, aborting.\n");
608 err = mthca_dev_lim(mdev, &dev_lim);
610 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
614 profile = default_profile;
615 profile.num_uar = dev_lim.uar_size / PAGE_SIZE;
616 profile.num_udav = 0;
617 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
618 profile.num_srq = dev_lim.max_srqs;
620 icm_size = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
621 if ((int) icm_size < 0) {
626 err = mthca_init_icm(mdev, &dev_lim, &init_hca, icm_size);
630 err = mthca_INIT_HCA(mdev, &init_hca, &status);
632 mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
636 mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
637 "aborting.\n", status);
645 mthca_free_icms(mdev);
648 mthca_UNMAP_FA(mdev, &status);
649 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
652 if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
653 mthca_DISABLE_LAM(mdev, &status);
658 static void mthca_close_hca(struct mthca_dev *mdev)
662 mthca_CLOSE_HCA(mdev, 0, &status);
664 if (mthca_is_memfree(mdev)) {
665 mthca_free_icms(mdev);
667 mthca_UNMAP_FA(mdev, &status);
668 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
670 if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
671 mthca_DISABLE_LAM(mdev, &status);
673 mthca_SYS_DIS(mdev, &status);
676 static int __devinit mthca_init_hca(struct mthca_dev *mdev)
680 struct mthca_adapter adapter;
682 if (mthca_is_memfree(mdev))
683 err = mthca_init_arbel(mdev);
685 err = mthca_init_tavor(mdev);
690 err = mthca_QUERY_ADAPTER(mdev, &adapter, &status);
692 mthca_err(mdev, "QUERY_ADAPTER command failed, aborting.\n");
696 mthca_err(mdev, "QUERY_ADAPTER returned status 0x%02x, "
697 "aborting.\n", status);
702 mdev->eq_table.inta_pin = adapter.inta_pin;
703 mdev->rev_id = adapter.revision_id;
704 memcpy(mdev->board_id, adapter.board_id, sizeof mdev->board_id);
709 mthca_close_hca(mdev);
713 static int __devinit mthca_setup_hca(struct mthca_dev *dev)
718 MTHCA_INIT_DOORBELL_LOCK(&dev->doorbell_lock);
720 err = mthca_init_uar_table(dev);
722 mthca_err(dev, "Failed to initialize "
723 "user access region table, aborting.\n");
727 err = mthca_uar_alloc(dev, &dev->driver_uar);
729 mthca_err(dev, "Failed to allocate driver access region, "
731 goto err_uar_table_free;
734 dev->kar = ioremap(dev->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
736 mthca_err(dev, "Couldn't map kernel access region, "
742 err = mthca_init_pd_table(dev);
744 mthca_err(dev, "Failed to initialize "
745 "protection domain table, aborting.\n");
749 err = mthca_init_mr_table(dev);
751 mthca_err(dev, "Failed to initialize "
752 "memory region table, aborting.\n");
753 goto err_pd_table_free;
756 err = mthca_pd_alloc(dev, 1, &dev->driver_pd);
758 mthca_err(dev, "Failed to create driver PD, "
760 goto err_mr_table_free;
763 err = mthca_init_eq_table(dev);
765 mthca_err(dev, "Failed to initialize "
766 "event queue table, aborting.\n");
770 err = mthca_cmd_use_events(dev);
772 mthca_err(dev, "Failed to switch to event-driven "
773 "firmware commands, aborting.\n");
774 goto err_eq_table_free;
777 err = mthca_NOP(dev, &status);
779 mthca_err(dev, "NOP command failed to generate interrupt (IRQ %d), aborting.\n",
780 dev->mthca_flags & MTHCA_FLAG_MSI_X ?
781 dev->eq_table.eq[MTHCA_EQ_CMD].msi_x_vector :
783 if (dev->mthca_flags & (MTHCA_FLAG_MSI | MTHCA_FLAG_MSI_X))
784 mthca_err(dev, "Try again with MSI/MSI-X disabled.\n");
786 mthca_err(dev, "BIOS or ACPI interrupt routing problem?\n");
791 mthca_dbg(dev, "NOP command IRQ test passed\n");
793 err = mthca_init_cq_table(dev);
795 mthca_err(dev, "Failed to initialize "
796 "completion queue table, aborting.\n");
800 err = mthca_init_srq_table(dev);
802 mthca_err(dev, "Failed to initialize "
803 "shared receive queue table, aborting.\n");
804 goto err_cq_table_free;
807 err = mthca_init_qp_table(dev);
809 mthca_err(dev, "Failed to initialize "
810 "queue pair table, aborting.\n");
811 goto err_srq_table_free;
814 err = mthca_init_av_table(dev);
816 mthca_err(dev, "Failed to initialize "
817 "address vector table, aborting.\n");
818 goto err_qp_table_free;
821 err = mthca_init_mcg_table(dev);
823 mthca_err(dev, "Failed to initialize "
824 "multicast group table, aborting.\n");
825 goto err_av_table_free;
831 mthca_cleanup_av_table(dev);
834 mthca_cleanup_qp_table(dev);
837 mthca_cleanup_srq_table(dev);
840 mthca_cleanup_cq_table(dev);
843 mthca_cmd_use_polling(dev);
846 mthca_cleanup_eq_table(dev);
849 mthca_pd_free(dev, &dev->driver_pd);
852 mthca_cleanup_mr_table(dev);
855 mthca_cleanup_pd_table(dev);
861 mthca_uar_free(dev, &dev->driver_uar);
864 mthca_cleanup_uar_table(dev);
868 static int __devinit mthca_request_regions(struct pci_dev *pdev,
874 * We can't just use pci_request_regions() because the MSI-X
875 * table is right in the middle of the first BAR. If we did
876 * pci_request_region and grab all of the first BAR, then
877 * setting up MSI-X would fail, since the PCI core wants to do
878 * request_mem_region on the MSI-X vector table.
880 * So just request what we need right now, and request any
881 * other regions we need when setting up EQs.
883 if (!request_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
884 MTHCA_HCR_SIZE, DRV_NAME))
887 err = pci_request_region(pdev, 2, DRV_NAME);
889 goto err_bar2_failed;
892 err = pci_request_region(pdev, 4, DRV_NAME);
894 goto err_bar4_failed;
900 pci_release_region(pdev, 2);
903 release_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
909 static void mthca_release_regions(struct pci_dev *pdev,
913 pci_release_region(pdev, 4);
915 pci_release_region(pdev, 2);
917 release_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
921 static int __devinit mthca_enable_msi_x(struct mthca_dev *mdev)
923 struct msix_entry entries[3];
926 entries[0].entry = 0;
927 entries[1].entry = 1;
928 entries[2].entry = 2;
930 err = pci_enable_msix(mdev->pdev, entries, ARRAY_SIZE(entries));
933 mthca_info(mdev, "Only %d MSI-X vectors available, "
934 "not using MSI-X\n", err);
938 mdev->eq_table.eq[MTHCA_EQ_COMP ].msi_x_vector = entries[0].vector;
939 mdev->eq_table.eq[MTHCA_EQ_ASYNC].msi_x_vector = entries[1].vector;
940 mdev->eq_table.eq[MTHCA_EQ_CMD ].msi_x_vector = entries[2].vector;
945 /* Types of supported HCA */
948 ARBEL_COMPAT, /* MT25208 in Tavor compat mode */
949 ARBEL_NATIVE, /* MT25208 with extended features */
953 #define MTHCA_FW_VER(major, minor, subminor) \
954 (((u64) (major) << 32) | ((u64) (minor) << 16) | (u64) (subminor))
959 } mthca_hca_table[] = {
960 [TAVOR] = { .latest_fw = MTHCA_FW_VER(3, 4, 0),
962 [ARBEL_COMPAT] = { .latest_fw = MTHCA_FW_VER(4, 7, 400),
963 .flags = MTHCA_FLAG_PCIE },
964 [ARBEL_NATIVE] = { .latest_fw = MTHCA_FW_VER(5, 1, 0),
965 .flags = MTHCA_FLAG_MEMFREE |
967 [SINAI] = { .latest_fw = MTHCA_FW_VER(1, 0, 800),
968 .flags = MTHCA_FLAG_MEMFREE |
970 MTHCA_FLAG_SINAI_OPT }
973 static int __devinit mthca_init_one(struct pci_dev *pdev,
974 const struct pci_device_id *id)
976 static int mthca_version_printed = 0;
979 struct mthca_dev *mdev;
981 if (!mthca_version_printed) {
982 printk(KERN_INFO "%s", mthca_version);
983 ++mthca_version_printed;
986 printk(KERN_INFO PFX "Initializing %s\n",
989 if (id->driver_data >= ARRAY_SIZE(mthca_hca_table)) {
990 printk(KERN_ERR PFX "%s has invalid driver data %lx\n",
991 pci_name(pdev), id->driver_data);
995 err = pci_enable_device(pdev);
997 dev_err(&pdev->dev, "Cannot enable PCI device, "
1003 * Check for BARs. We expect 0: 1MB, 2: 8MB, 4: DDR (may not
1006 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
1007 pci_resource_len(pdev, 0) != 1 << 20) {
1008 dev_err(&pdev->dev, "Missing DCS, aborting.\n");
1010 goto err_disable_pdev;
1012 if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
1013 dev_err(&pdev->dev, "Missing UAR, aborting.\n");
1015 goto err_disable_pdev;
1017 if (!(pci_resource_flags(pdev, 4) & IORESOURCE_MEM))
1020 err = mthca_request_regions(pdev, ddr_hidden);
1022 dev_err(&pdev->dev, "Cannot obtain PCI resources, "
1024 goto err_disable_pdev;
1027 pci_set_master(pdev);
1029 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
1031 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask.\n");
1032 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1034 dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting.\n");
1038 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
1040 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit "
1041 "consistent PCI DMA mask.\n");
1042 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
1044 dev_err(&pdev->dev, "Can't set consistent PCI DMA mask, "
1050 mdev = (struct mthca_dev *) ib_alloc_device(sizeof *mdev);
1052 dev_err(&pdev->dev, "Device struct alloc failed, "
1060 mdev->mthca_flags = mthca_hca_table[id->driver_data].flags;
1062 mdev->mthca_flags |= MTHCA_FLAG_DDR_HIDDEN;
1065 * Now reset the HCA before we touch the PCI capabilities or
1066 * attempt a firmware command, since a boot ROM may have left
1067 * the HCA in an undefined state.
1069 err = mthca_reset(mdev);
1071 mthca_err(mdev, "Failed to reset HCA, aborting.\n");
1075 if (msi_x && !mthca_enable_msi_x(mdev))
1076 mdev->mthca_flags |= MTHCA_FLAG_MSI_X;
1077 if (msi && !(mdev->mthca_flags & MTHCA_FLAG_MSI_X) &&
1078 !pci_enable_msi(pdev))
1079 mdev->mthca_flags |= MTHCA_FLAG_MSI;
1081 if (mthca_cmd_init(mdev)) {
1082 mthca_err(mdev, "Failed to init command interface, aborting.\n");
1086 err = mthca_tune_pci(mdev);
1090 err = mthca_init_hca(mdev);
1094 if (mdev->fw_ver < mthca_hca_table[id->driver_data].latest_fw) {
1095 mthca_warn(mdev, "HCA FW version %d.%d.%d is old (%d.%d.%d is current).\n",
1096 (int) (mdev->fw_ver >> 32), (int) (mdev->fw_ver >> 16) & 0xffff,
1097 (int) (mdev->fw_ver & 0xffff),
1098 (int) (mthca_hca_table[id->driver_data].latest_fw >> 32),
1099 (int) (mthca_hca_table[id->driver_data].latest_fw >> 16) & 0xffff,
1100 (int) (mthca_hca_table[id->driver_data].latest_fw & 0xffff));
1101 mthca_warn(mdev, "If you have problems, try updating your HCA FW.\n");
1104 err = mthca_setup_hca(mdev);
1108 err = mthca_register_device(mdev);
1112 err = mthca_create_agents(mdev);
1114 goto err_unregister;
1116 pci_set_drvdata(pdev, mdev);
1121 mthca_unregister_device(mdev);
1124 mthca_cleanup_mcg_table(mdev);
1125 mthca_cleanup_av_table(mdev);
1126 mthca_cleanup_qp_table(mdev);
1127 mthca_cleanup_srq_table(mdev);
1128 mthca_cleanup_cq_table(mdev);
1129 mthca_cmd_use_polling(mdev);
1130 mthca_cleanup_eq_table(mdev);
1132 mthca_pd_free(mdev, &mdev->driver_pd);
1134 mthca_cleanup_mr_table(mdev);
1135 mthca_cleanup_pd_table(mdev);
1136 mthca_cleanup_uar_table(mdev);
1139 mthca_close_hca(mdev);
1142 mthca_cmd_cleanup(mdev);
1145 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1146 pci_disable_msix(pdev);
1147 if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1148 pci_disable_msi(pdev);
1150 ib_dealloc_device(&mdev->ib_dev);
1153 mthca_release_regions(pdev, ddr_hidden);
1156 pci_disable_device(pdev);
1157 pci_set_drvdata(pdev, NULL);
1161 static void __devexit mthca_remove_one(struct pci_dev *pdev)
1163 struct mthca_dev *mdev = pci_get_drvdata(pdev);
1168 mthca_free_agents(mdev);
1169 mthca_unregister_device(mdev);
1171 for (p = 1; p <= mdev->limits.num_ports; ++p)
1172 mthca_CLOSE_IB(mdev, p, &status);
1174 mthca_cleanup_mcg_table(mdev);
1175 mthca_cleanup_av_table(mdev);
1176 mthca_cleanup_qp_table(mdev);
1177 mthca_cleanup_srq_table(mdev);
1178 mthca_cleanup_cq_table(mdev);
1179 mthca_cmd_use_polling(mdev);
1180 mthca_cleanup_eq_table(mdev);
1182 mthca_pd_free(mdev, &mdev->driver_pd);
1184 mthca_cleanup_mr_table(mdev);
1185 mthca_cleanup_pd_table(mdev);
1188 mthca_uar_free(mdev, &mdev->driver_uar);
1189 mthca_cleanup_uar_table(mdev);
1190 mthca_close_hca(mdev);
1191 mthca_cmd_cleanup(mdev);
1193 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1194 pci_disable_msix(pdev);
1195 if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1196 pci_disable_msi(pdev);
1198 ib_dealloc_device(&mdev->ib_dev);
1199 mthca_release_regions(pdev, mdev->mthca_flags &
1200 MTHCA_FLAG_DDR_HIDDEN);
1201 pci_disable_device(pdev);
1202 pci_set_drvdata(pdev, NULL);
1206 static struct pci_device_id mthca_pci_table[] = {
1207 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_TAVOR),
1208 .driver_data = TAVOR },
1209 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_TAVOR),
1210 .driver_data = TAVOR },
1211 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
1212 .driver_data = ARBEL_COMPAT },
1213 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
1214 .driver_data = ARBEL_COMPAT },
1215 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL),
1216 .driver_data = ARBEL_NATIVE },
1217 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL),
1218 .driver_data = ARBEL_NATIVE },
1219 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI),
1220 .driver_data = SINAI },
1221 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI),
1222 .driver_data = SINAI },
1223 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
1224 .driver_data = SINAI },
1225 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
1226 .driver_data = SINAI },
1230 MODULE_DEVICE_TABLE(pci, mthca_pci_table);
1232 static struct pci_driver mthca_driver = {
1234 .id_table = mthca_pci_table,
1235 .probe = mthca_init_one,
1236 .remove = __devexit_p(mthca_remove_one)
1239 static int __init mthca_init(void)
1243 ret = pci_register_driver(&mthca_driver);
1244 return ret < 0 ? ret : 0;
1247 static void __exit mthca_cleanup(void)
1249 pci_unregister_driver(&mthca_driver);
1252 module_init(mthca_init);
1253 module_exit(mthca_cleanup);