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);
58 module_param(msi_x, int, 0444);
59 MODULE_PARM_DESC(msi_x, "attempt to use MSI-X if nonzero");
62 module_param(msi, int, 0444);
63 MODULE_PARM_DESC(msi, "attempt to use MSI if nonzero");
65 #else /* CONFIG_PCI_MSI */
70 #endif /* CONFIG_PCI_MSI */
72 static const char mthca_version[] __devinitdata =
73 DRV_NAME ": Mellanox InfiniBand HCA driver v"
74 DRV_VERSION " (" DRV_RELDATE ")\n";
76 static struct mthca_profile default_profile = {
83 .num_udav = 1 << 15, /* Tavor only */
84 .fmr_reserved_mtts = 1 << 18, /* Tavor only */
85 .uarc_size = 1 << 18, /* Arbel only */
88 static int __devinit mthca_tune_pci(struct mthca_dev *mdev)
93 /* First try to max out Read Byte Count */
94 cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_PCIX);
96 if (pci_read_config_word(mdev->pdev, cap + PCI_X_CMD, &val)) {
97 mthca_err(mdev, "Couldn't read PCI-X command register, "
101 val = (val & ~PCI_X_CMD_MAX_READ) | (3 << 2);
102 if (pci_write_config_word(mdev->pdev, cap + PCI_X_CMD, val)) {
103 mthca_err(mdev, "Couldn't write PCI-X command register, "
107 } else if (!(mdev->mthca_flags & MTHCA_FLAG_PCIE))
108 mthca_info(mdev, "No PCI-X capability, not setting RBC.\n");
110 cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_EXP);
112 if (pci_read_config_word(mdev->pdev, cap + PCI_EXP_DEVCTL, &val)) {
113 mthca_err(mdev, "Couldn't read PCI Express device control "
114 "register, aborting.\n");
117 val = (val & ~PCI_EXP_DEVCTL_READRQ) | (5 << 12);
118 if (pci_write_config_word(mdev->pdev, cap + PCI_EXP_DEVCTL, val)) {
119 mthca_err(mdev, "Couldn't write PCI Express device control "
120 "register, aborting.\n");
123 } else if (mdev->mthca_flags & MTHCA_FLAG_PCIE)
124 mthca_info(mdev, "No PCI Express capability, "
125 "not setting Max Read Request Size.\n");
130 static int __devinit mthca_dev_lim(struct mthca_dev *mdev, struct mthca_dev_lim *dev_lim)
135 err = mthca_QUERY_DEV_LIM(mdev, dev_lim, &status);
137 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
141 mthca_err(mdev, "QUERY_DEV_LIM returned status 0x%02x, "
142 "aborting.\n", status);
145 if (dev_lim->min_page_sz > PAGE_SIZE) {
146 mthca_err(mdev, "HCA minimum page size of %d bigger than "
147 "kernel PAGE_SIZE of %ld, aborting.\n",
148 dev_lim->min_page_sz, PAGE_SIZE);
151 if (dev_lim->num_ports > MTHCA_MAX_PORTS) {
152 mthca_err(mdev, "HCA has %d ports, but we only support %d, "
154 dev_lim->num_ports, MTHCA_MAX_PORTS);
158 mdev->limits.num_ports = dev_lim->num_ports;
159 mdev->limits.vl_cap = dev_lim->max_vl;
160 mdev->limits.mtu_cap = dev_lim->max_mtu;
161 mdev->limits.gid_table_len = dev_lim->max_gids;
162 mdev->limits.pkey_table_len = dev_lim->max_pkeys;
163 mdev->limits.local_ca_ack_delay = dev_lim->local_ca_ack_delay;
164 mdev->limits.max_sg = dev_lim->max_sg;
165 mdev->limits.max_wqes = dev_lim->max_qp_sz;
166 mdev->limits.max_qp_init_rdma = dev_lim->max_requester_per_qp;
167 mdev->limits.reserved_qps = dev_lim->reserved_qps;
168 mdev->limits.max_srq_wqes = dev_lim->max_srq_sz;
169 mdev->limits.reserved_srqs = dev_lim->reserved_srqs;
170 mdev->limits.reserved_eecs = dev_lim->reserved_eecs;
172 * Subtract 1 from the limit because we need to allocate a
173 * spare CQE so the HCA HW can tell the difference between an
174 * empty CQ and a full CQ.
176 mdev->limits.max_cqes = dev_lim->max_cq_sz - 1;
177 mdev->limits.reserved_cqs = dev_lim->reserved_cqs;
178 mdev->limits.reserved_eqs = dev_lim->reserved_eqs;
179 mdev->limits.reserved_mtts = dev_lim->reserved_mtts;
180 mdev->limits.reserved_mrws = dev_lim->reserved_mrws;
181 mdev->limits.reserved_uars = dev_lim->reserved_uars;
182 mdev->limits.reserved_pds = dev_lim->reserved_pds;
183 mdev->limits.port_width_cap = dev_lim->max_port_width;
184 mdev->limits.flags = dev_lim->flags;
186 /* IB_DEVICE_RESIZE_MAX_WR not supported by driver.
187 May be doable since hardware supports it for SRQ.
189 IB_DEVICE_N_NOTIFY_CQ is supported by hardware but not by driver.
191 IB_DEVICE_SRQ_RESIZE is supported by hardware but SRQ is not
192 supported by driver. */
193 mdev->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
194 IB_DEVICE_PORT_ACTIVE_EVENT |
195 IB_DEVICE_SYS_IMAGE_GUID |
196 IB_DEVICE_RC_RNR_NAK_GEN;
198 if (dev_lim->flags & DEV_LIM_FLAG_BAD_PKEY_CNTR)
199 mdev->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
201 if (dev_lim->flags & DEV_LIM_FLAG_BAD_QKEY_CNTR)
202 mdev->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
204 if (dev_lim->flags & DEV_LIM_FLAG_RAW_MULTI)
205 mdev->device_cap_flags |= IB_DEVICE_RAW_MULTI;
207 if (dev_lim->flags & DEV_LIM_FLAG_AUTO_PATH_MIG)
208 mdev->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
210 if (dev_lim->flags & DEV_LIM_FLAG_UD_AV_PORT_ENFORCE)
211 mdev->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
213 if (dev_lim->flags & DEV_LIM_FLAG_SRQ)
214 mdev->mthca_flags |= MTHCA_FLAG_SRQ;
219 static int __devinit mthca_init_tavor(struct mthca_dev *mdev)
223 struct mthca_dev_lim dev_lim;
224 struct mthca_profile profile;
225 struct mthca_init_hca_param init_hca;
227 err = mthca_SYS_EN(mdev, &status);
229 mthca_err(mdev, "SYS_EN command failed, aborting.\n");
233 mthca_err(mdev, "SYS_EN returned status 0x%02x, "
234 "aborting.\n", status);
238 err = mthca_QUERY_FW(mdev, &status);
240 mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
244 mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
245 "aborting.\n", status);
249 err = mthca_QUERY_DDR(mdev, &status);
251 mthca_err(mdev, "QUERY_DDR command failed, aborting.\n");
255 mthca_err(mdev, "QUERY_DDR returned status 0x%02x, "
256 "aborting.\n", status);
261 err = mthca_dev_lim(mdev, &dev_lim);
263 profile = default_profile;
264 profile.num_uar = dev_lim.uar_size / PAGE_SIZE;
265 profile.uarc_size = 0;
266 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
267 profile.num_srq = dev_lim.max_srqs;
269 err = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
273 err = mthca_INIT_HCA(mdev, &init_hca, &status);
275 mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
279 mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
280 "aborting.\n", status);
288 mthca_SYS_DIS(mdev, &status);
293 static int __devinit mthca_load_fw(struct mthca_dev *mdev)
298 /* FIXME: use HCA-attached memory for FW if present */
300 mdev->fw.arbel.fw_icm =
301 mthca_alloc_icm(mdev, mdev->fw.arbel.fw_pages,
302 GFP_HIGHUSER | __GFP_NOWARN);
303 if (!mdev->fw.arbel.fw_icm) {
304 mthca_err(mdev, "Couldn't allocate FW area, aborting.\n");
308 err = mthca_MAP_FA(mdev, mdev->fw.arbel.fw_icm, &status);
310 mthca_err(mdev, "MAP_FA command failed, aborting.\n");
314 mthca_err(mdev, "MAP_FA returned status 0x%02x, aborting.\n", status);
318 err = mthca_RUN_FW(mdev, &status);
320 mthca_err(mdev, "RUN_FW command failed, aborting.\n");
324 mthca_err(mdev, "RUN_FW returned status 0x%02x, aborting.\n", status);
332 mthca_UNMAP_FA(mdev, &status);
335 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
339 static int __devinit mthca_init_icm(struct mthca_dev *mdev,
340 struct mthca_dev_lim *dev_lim,
341 struct mthca_init_hca_param *init_hca,
348 err = mthca_SET_ICM_SIZE(mdev, icm_size, &aux_pages, &status);
350 mthca_err(mdev, "SET_ICM_SIZE command failed, aborting.\n");
354 mthca_err(mdev, "SET_ICM_SIZE returned status 0x%02x, "
355 "aborting.\n", status);
359 mthca_dbg(mdev, "%lld KB of HCA context requires %lld KB aux memory.\n",
360 (unsigned long long) icm_size >> 10,
361 (unsigned long long) aux_pages << 2);
363 mdev->fw.arbel.aux_icm = mthca_alloc_icm(mdev, aux_pages,
364 GFP_HIGHUSER | __GFP_NOWARN);
365 if (!mdev->fw.arbel.aux_icm) {
366 mthca_err(mdev, "Couldn't allocate aux memory, aborting.\n");
370 err = mthca_MAP_ICM_AUX(mdev, mdev->fw.arbel.aux_icm, &status);
372 mthca_err(mdev, "MAP_ICM_AUX command failed, aborting.\n");
376 mthca_err(mdev, "MAP_ICM_AUX returned status 0x%02x, aborting.\n", status);
381 err = mthca_map_eq_icm(mdev, init_hca->eqc_base);
383 mthca_err(mdev, "Failed to map EQ context memory, aborting.\n");
387 mdev->mr_table.mtt_table = mthca_alloc_icm_table(mdev, init_hca->mtt_base,
389 mdev->limits.num_mtt_segs,
390 mdev->limits.reserved_mtts, 1);
391 if (!mdev->mr_table.mtt_table) {
392 mthca_err(mdev, "Failed to map MTT context memory, aborting.\n");
397 mdev->mr_table.mpt_table = mthca_alloc_icm_table(mdev, init_hca->mpt_base,
398 dev_lim->mpt_entry_sz,
399 mdev->limits.num_mpts,
400 mdev->limits.reserved_mrws, 1);
401 if (!mdev->mr_table.mpt_table) {
402 mthca_err(mdev, "Failed to map MPT context memory, aborting.\n");
407 mdev->qp_table.qp_table = mthca_alloc_icm_table(mdev, init_hca->qpc_base,
408 dev_lim->qpc_entry_sz,
409 mdev->limits.num_qps,
410 mdev->limits.reserved_qps, 0);
411 if (!mdev->qp_table.qp_table) {
412 mthca_err(mdev, "Failed to map QP context memory, aborting.\n");
417 mdev->qp_table.eqp_table = mthca_alloc_icm_table(mdev, init_hca->eqpc_base,
418 dev_lim->eqpc_entry_sz,
419 mdev->limits.num_qps,
420 mdev->limits.reserved_qps, 0);
421 if (!mdev->qp_table.eqp_table) {
422 mthca_err(mdev, "Failed to map EQP context memory, aborting.\n");
427 mdev->qp_table.rdb_table = mthca_alloc_icm_table(mdev, init_hca->rdb_base,
428 MTHCA_RDB_ENTRY_SIZE,
429 mdev->limits.num_qps <<
430 mdev->qp_table.rdb_shift,
432 if (!mdev->qp_table.rdb_table) {
433 mthca_err(mdev, "Failed to map RDB context memory, aborting\n");
438 mdev->cq_table.table = mthca_alloc_icm_table(mdev, init_hca->cqc_base,
439 dev_lim->cqc_entry_sz,
440 mdev->limits.num_cqs,
441 mdev->limits.reserved_cqs, 0);
442 if (!mdev->cq_table.table) {
443 mthca_err(mdev, "Failed to map CQ context memory, aborting.\n");
448 if (mdev->mthca_flags & MTHCA_FLAG_SRQ) {
449 mdev->srq_table.table =
450 mthca_alloc_icm_table(mdev, init_hca->srqc_base,
451 dev_lim->srq_entry_sz,
452 mdev->limits.num_srqs,
453 mdev->limits.reserved_srqs, 0);
454 if (!mdev->srq_table.table) {
455 mthca_err(mdev, "Failed to map SRQ context memory, "
463 * It's not strictly required, but for simplicity just map the
464 * whole multicast group table now. The table isn't very big
465 * and it's a lot easier than trying to track ref counts.
467 mdev->mcg_table.table = mthca_alloc_icm_table(mdev, init_hca->mc_base,
468 MTHCA_MGM_ENTRY_SIZE,
469 mdev->limits.num_mgms +
470 mdev->limits.num_amgms,
471 mdev->limits.num_mgms +
472 mdev->limits.num_amgms,
474 if (!mdev->mcg_table.table) {
475 mthca_err(mdev, "Failed to map MCG context memory, aborting.\n");
483 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
484 mthca_free_icm_table(mdev, mdev->srq_table.table);
487 mthca_free_icm_table(mdev, mdev->cq_table.table);
490 mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
493 mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
496 mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
499 mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
502 mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
505 mthca_unmap_eq_icm(mdev);
508 mthca_UNMAP_ICM_AUX(mdev, &status);
511 mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
516 static void mthca_free_icms(struct mthca_dev *mdev)
520 mthca_free_icm_table(mdev, mdev->mcg_table.table);
521 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
522 mthca_free_icm_table(mdev, mdev->srq_table.table);
523 mthca_free_icm_table(mdev, mdev->cq_table.table);
524 mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
525 mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
526 mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
527 mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
528 mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
529 mthca_unmap_eq_icm(mdev);
531 mthca_UNMAP_ICM_AUX(mdev, &status);
532 mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
535 static int __devinit mthca_init_arbel(struct mthca_dev *mdev)
537 struct mthca_dev_lim dev_lim;
538 struct mthca_profile profile;
539 struct mthca_init_hca_param init_hca;
544 err = mthca_QUERY_FW(mdev, &status);
546 mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
550 mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
551 "aborting.\n", status);
555 err = mthca_ENABLE_LAM(mdev, &status);
557 mthca_err(mdev, "ENABLE_LAM command failed, aborting.\n");
560 if (status == MTHCA_CMD_STAT_LAM_NOT_PRE) {
561 mthca_dbg(mdev, "No HCA-attached memory (running in MemFree mode)\n");
562 mdev->mthca_flags |= MTHCA_FLAG_NO_LAM;
564 mthca_err(mdev, "ENABLE_LAM returned status 0x%02x, "
565 "aborting.\n", status);
569 err = mthca_load_fw(mdev);
571 mthca_err(mdev, "Failed to start FW, aborting.\n");
575 err = mthca_dev_lim(mdev, &dev_lim);
577 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
581 profile = default_profile;
582 profile.num_uar = dev_lim.uar_size / PAGE_SIZE;
583 profile.num_udav = 0;
584 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
585 profile.num_srq = dev_lim.max_srqs;
587 icm_size = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
588 if ((int) icm_size < 0) {
593 err = mthca_init_icm(mdev, &dev_lim, &init_hca, icm_size);
597 err = mthca_INIT_HCA(mdev, &init_hca, &status);
599 mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
603 mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
604 "aborting.\n", status);
612 mthca_free_icms(mdev);
615 mthca_UNMAP_FA(mdev, &status);
616 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
619 if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
620 mthca_DISABLE_LAM(mdev, &status);
625 static void mthca_close_hca(struct mthca_dev *mdev)
629 mthca_CLOSE_HCA(mdev, 0, &status);
631 if (mthca_is_memfree(mdev)) {
632 mthca_free_icms(mdev);
634 mthca_UNMAP_FA(mdev, &status);
635 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
637 if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
638 mthca_DISABLE_LAM(mdev, &status);
640 mthca_SYS_DIS(mdev, &status);
643 static int __devinit mthca_init_hca(struct mthca_dev *mdev)
647 struct mthca_adapter adapter;
649 if (mthca_is_memfree(mdev))
650 err = mthca_init_arbel(mdev);
652 err = mthca_init_tavor(mdev);
657 err = mthca_QUERY_ADAPTER(mdev, &adapter, &status);
659 mthca_err(mdev, "QUERY_ADAPTER command failed, aborting.\n");
663 mthca_err(mdev, "QUERY_ADAPTER returned status 0x%02x, "
664 "aborting.\n", status);
669 mdev->eq_table.inta_pin = adapter.inta_pin;
670 mdev->rev_id = adapter.revision_id;
671 memcpy(mdev->board_id, adapter.board_id, sizeof mdev->board_id);
676 mthca_close_hca(mdev);
680 static int __devinit mthca_setup_hca(struct mthca_dev *dev)
685 MTHCA_INIT_DOORBELL_LOCK(&dev->doorbell_lock);
687 err = mthca_init_uar_table(dev);
689 mthca_err(dev, "Failed to initialize "
690 "user access region table, aborting.\n");
694 err = mthca_uar_alloc(dev, &dev->driver_uar);
696 mthca_err(dev, "Failed to allocate driver access region, "
698 goto err_uar_table_free;
701 dev->kar = ioremap(dev->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
703 mthca_err(dev, "Couldn't map kernel access region, "
709 err = mthca_init_pd_table(dev);
711 mthca_err(dev, "Failed to initialize "
712 "protection domain table, aborting.\n");
716 err = mthca_init_mr_table(dev);
718 mthca_err(dev, "Failed to initialize "
719 "memory region table, aborting.\n");
720 goto err_pd_table_free;
723 err = mthca_pd_alloc(dev, 1, &dev->driver_pd);
725 mthca_err(dev, "Failed to create driver PD, "
727 goto err_mr_table_free;
730 err = mthca_init_eq_table(dev);
732 mthca_err(dev, "Failed to initialize "
733 "event queue table, aborting.\n");
737 err = mthca_cmd_use_events(dev);
739 mthca_err(dev, "Failed to switch to event-driven "
740 "firmware commands, aborting.\n");
741 goto err_eq_table_free;
744 err = mthca_NOP(dev, &status);
746 mthca_err(dev, "NOP command failed to generate interrupt (IRQ %d), aborting.\n",
747 dev->mthca_flags & MTHCA_FLAG_MSI_X ?
748 dev->eq_table.eq[MTHCA_EQ_CMD].msi_x_vector :
750 if (dev->mthca_flags & (MTHCA_FLAG_MSI | MTHCA_FLAG_MSI_X))
751 mthca_err(dev, "Try again with MSI/MSI-X disabled.\n");
753 mthca_err(dev, "BIOS or ACPI interrupt routing problem?\n");
758 mthca_dbg(dev, "NOP command IRQ test passed\n");
760 err = mthca_init_cq_table(dev);
762 mthca_err(dev, "Failed to initialize "
763 "completion queue table, aborting.\n");
767 err = mthca_init_srq_table(dev);
769 mthca_err(dev, "Failed to initialize "
770 "shared receive queue table, aborting.\n");
771 goto err_cq_table_free;
774 err = mthca_init_qp_table(dev);
776 mthca_err(dev, "Failed to initialize "
777 "queue pair table, aborting.\n");
778 goto err_srq_table_free;
781 err = mthca_init_av_table(dev);
783 mthca_err(dev, "Failed to initialize "
784 "address vector table, aborting.\n");
785 goto err_qp_table_free;
788 err = mthca_init_mcg_table(dev);
790 mthca_err(dev, "Failed to initialize "
791 "multicast group table, aborting.\n");
792 goto err_av_table_free;
798 mthca_cleanup_av_table(dev);
801 mthca_cleanup_qp_table(dev);
804 mthca_cleanup_srq_table(dev);
807 mthca_cleanup_cq_table(dev);
810 mthca_cmd_use_polling(dev);
813 mthca_cleanup_eq_table(dev);
816 mthca_pd_free(dev, &dev->driver_pd);
819 mthca_cleanup_mr_table(dev);
822 mthca_cleanup_pd_table(dev);
828 mthca_uar_free(dev, &dev->driver_uar);
831 mthca_cleanup_uar_table(dev);
835 static int __devinit mthca_request_regions(struct pci_dev *pdev,
841 * We can't just use pci_request_regions() because the MSI-X
842 * table is right in the middle of the first BAR. If we did
843 * pci_request_region and grab all of the first BAR, then
844 * setting up MSI-X would fail, since the PCI core wants to do
845 * request_mem_region on the MSI-X vector table.
847 * So just request what we need right now, and request any
848 * other regions we need when setting up EQs.
850 if (!request_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
851 MTHCA_HCR_SIZE, DRV_NAME))
854 err = pci_request_region(pdev, 2, DRV_NAME);
856 goto err_bar2_failed;
859 err = pci_request_region(pdev, 4, DRV_NAME);
861 goto err_bar4_failed;
867 pci_release_region(pdev, 2);
870 release_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
876 static void mthca_release_regions(struct pci_dev *pdev,
880 pci_release_region(pdev, 4);
882 pci_release_region(pdev, 2);
884 release_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
888 static int __devinit mthca_enable_msi_x(struct mthca_dev *mdev)
890 struct msix_entry entries[3];
893 entries[0].entry = 0;
894 entries[1].entry = 1;
895 entries[2].entry = 2;
897 err = pci_enable_msix(mdev->pdev, entries, ARRAY_SIZE(entries));
900 mthca_info(mdev, "Only %d MSI-X vectors available, "
901 "not using MSI-X\n", err);
905 mdev->eq_table.eq[MTHCA_EQ_COMP ].msi_x_vector = entries[0].vector;
906 mdev->eq_table.eq[MTHCA_EQ_ASYNC].msi_x_vector = entries[1].vector;
907 mdev->eq_table.eq[MTHCA_EQ_CMD ].msi_x_vector = entries[2].vector;
912 /* Types of supported HCA */
915 ARBEL_COMPAT, /* MT25208 in Tavor compat mode */
916 ARBEL_NATIVE, /* MT25208 with extended features */
920 #define MTHCA_FW_VER(major, minor, subminor) \
921 (((u64) (major) << 32) | ((u64) (minor) << 16) | (u64) (subminor))
927 } mthca_hca_table[] = {
928 [TAVOR] = { .latest_fw = MTHCA_FW_VER(3, 3, 3), .is_memfree = 0, .is_pcie = 0 },
929 [ARBEL_COMPAT] = { .latest_fw = MTHCA_FW_VER(4, 7, 0), .is_memfree = 0, .is_pcie = 1 },
930 [ARBEL_NATIVE] = { .latest_fw = MTHCA_FW_VER(5, 1, 0), .is_memfree = 1, .is_pcie = 1 },
931 [SINAI] = { .latest_fw = MTHCA_FW_VER(1, 0, 1), .is_memfree = 1, .is_pcie = 1 }
934 static int __devinit mthca_init_one(struct pci_dev *pdev,
935 const struct pci_device_id *id)
937 static int mthca_version_printed = 0;
940 struct mthca_dev *mdev;
942 if (!mthca_version_printed) {
943 printk(KERN_INFO "%s", mthca_version);
944 ++mthca_version_printed;
947 printk(KERN_INFO PFX "Initializing %s\n",
950 if (id->driver_data >= ARRAY_SIZE(mthca_hca_table)) {
951 printk(KERN_ERR PFX "%s has invalid driver data %lx\n",
952 pci_name(pdev), id->driver_data);
956 err = pci_enable_device(pdev);
958 dev_err(&pdev->dev, "Cannot enable PCI device, "
964 * Check for BARs. We expect 0: 1MB, 2: 8MB, 4: DDR (may not
967 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
968 pci_resource_len(pdev, 0) != 1 << 20) {
969 dev_err(&pdev->dev, "Missing DCS, aborting.\n");
971 goto err_disable_pdev;
973 if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM) ||
974 pci_resource_len(pdev, 2) != 1 << 23) {
975 dev_err(&pdev->dev, "Missing UAR, aborting.\n");
977 goto err_disable_pdev;
979 if (!(pci_resource_flags(pdev, 4) & IORESOURCE_MEM))
982 err = mthca_request_regions(pdev, ddr_hidden);
984 dev_err(&pdev->dev, "Cannot obtain PCI resources, "
986 goto err_disable_pdev;
989 pci_set_master(pdev);
991 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
993 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask.\n");
994 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
996 dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting.\n");
1000 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
1002 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit "
1003 "consistent PCI DMA mask.\n");
1004 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
1006 dev_err(&pdev->dev, "Can't set consistent PCI DMA mask, "
1012 mdev = (struct mthca_dev *) ib_alloc_device(sizeof *mdev);
1014 dev_err(&pdev->dev, "Device struct alloc failed, "
1023 mdev->mthca_flags |= MTHCA_FLAG_DDR_HIDDEN;
1024 if (mthca_hca_table[id->driver_data].is_memfree)
1025 mdev->mthca_flags |= MTHCA_FLAG_MEMFREE;
1026 if (mthca_hca_table[id->driver_data].is_pcie)
1027 mdev->mthca_flags |= MTHCA_FLAG_PCIE;
1030 * Now reset the HCA before we touch the PCI capabilities or
1031 * attempt a firmware command, since a boot ROM may have left
1032 * the HCA in an undefined state.
1034 err = mthca_reset(mdev);
1036 mthca_err(mdev, "Failed to reset HCA, aborting.\n");
1040 if (msi_x && !mthca_enable_msi_x(mdev))
1041 mdev->mthca_flags |= MTHCA_FLAG_MSI_X;
1042 if (msi && !(mdev->mthca_flags & MTHCA_FLAG_MSI_X) &&
1043 !pci_enable_msi(pdev))
1044 mdev->mthca_flags |= MTHCA_FLAG_MSI;
1046 if (mthca_cmd_init(mdev)) {
1047 mthca_err(mdev, "Failed to init command interface, aborting.\n");
1051 err = mthca_tune_pci(mdev);
1055 err = mthca_init_hca(mdev);
1059 if (mdev->fw_ver < mthca_hca_table[id->driver_data].latest_fw) {
1060 mthca_warn(mdev, "HCA FW version %x.%x.%x is old (%x.%x.%x is current).\n",
1061 (int) (mdev->fw_ver >> 32), (int) (mdev->fw_ver >> 16) & 0xffff,
1062 (int) (mdev->fw_ver & 0xffff),
1063 (int) (mthca_hca_table[id->driver_data].latest_fw >> 32),
1064 (int) (mthca_hca_table[id->driver_data].latest_fw >> 16) & 0xffff,
1065 (int) (mthca_hca_table[id->driver_data].latest_fw & 0xffff));
1066 mthca_warn(mdev, "If you have problems, try updating your HCA FW.\n");
1069 err = mthca_setup_hca(mdev);
1073 err = mthca_register_device(mdev);
1077 err = mthca_create_agents(mdev);
1079 goto err_unregister;
1081 pci_set_drvdata(pdev, mdev);
1086 mthca_unregister_device(mdev);
1089 mthca_cleanup_mcg_table(mdev);
1090 mthca_cleanup_av_table(mdev);
1091 mthca_cleanup_qp_table(mdev);
1092 mthca_cleanup_srq_table(mdev);
1093 mthca_cleanup_cq_table(mdev);
1094 mthca_cmd_use_polling(mdev);
1095 mthca_cleanup_eq_table(mdev);
1097 mthca_pd_free(mdev, &mdev->driver_pd);
1099 mthca_cleanup_mr_table(mdev);
1100 mthca_cleanup_pd_table(mdev);
1101 mthca_cleanup_uar_table(mdev);
1104 mthca_close_hca(mdev);
1107 mthca_cmd_cleanup(mdev);
1110 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1111 pci_disable_msix(pdev);
1112 if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1113 pci_disable_msi(pdev);
1115 ib_dealloc_device(&mdev->ib_dev);
1118 mthca_release_regions(pdev, ddr_hidden);
1121 pci_disable_device(pdev);
1122 pci_set_drvdata(pdev, NULL);
1126 static void __devexit mthca_remove_one(struct pci_dev *pdev)
1128 struct mthca_dev *mdev = pci_get_drvdata(pdev);
1133 mthca_free_agents(mdev);
1134 mthca_unregister_device(mdev);
1136 for (p = 1; p <= mdev->limits.num_ports; ++p)
1137 mthca_CLOSE_IB(mdev, p, &status);
1139 mthca_cleanup_mcg_table(mdev);
1140 mthca_cleanup_av_table(mdev);
1141 mthca_cleanup_qp_table(mdev);
1142 mthca_cleanup_srq_table(mdev);
1143 mthca_cleanup_cq_table(mdev);
1144 mthca_cmd_use_polling(mdev);
1145 mthca_cleanup_eq_table(mdev);
1147 mthca_pd_free(mdev, &mdev->driver_pd);
1149 mthca_cleanup_mr_table(mdev);
1150 mthca_cleanup_pd_table(mdev);
1153 mthca_uar_free(mdev, &mdev->driver_uar);
1154 mthca_cleanup_uar_table(mdev);
1155 mthca_close_hca(mdev);
1156 mthca_cmd_cleanup(mdev);
1158 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1159 pci_disable_msix(pdev);
1160 if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1161 pci_disable_msi(pdev);
1163 ib_dealloc_device(&mdev->ib_dev);
1164 mthca_release_regions(pdev, mdev->mthca_flags &
1165 MTHCA_FLAG_DDR_HIDDEN);
1166 pci_disable_device(pdev);
1167 pci_set_drvdata(pdev, NULL);
1171 static struct pci_device_id mthca_pci_table[] = {
1172 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_TAVOR),
1173 .driver_data = TAVOR },
1174 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_TAVOR),
1175 .driver_data = TAVOR },
1176 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
1177 .driver_data = ARBEL_COMPAT },
1178 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
1179 .driver_data = ARBEL_COMPAT },
1180 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL),
1181 .driver_data = ARBEL_NATIVE },
1182 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL),
1183 .driver_data = ARBEL_NATIVE },
1184 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI),
1185 .driver_data = SINAI },
1186 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI),
1187 .driver_data = SINAI },
1188 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
1189 .driver_data = SINAI },
1190 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
1191 .driver_data = SINAI },
1195 MODULE_DEVICE_TABLE(pci, mthca_pci_table);
1197 static struct pci_driver mthca_driver = {
1199 .owner = THIS_MODULE,
1200 .id_table = mthca_pci_table,
1201 .probe = mthca_init_one,
1202 .remove = __devexit_p(mthca_remove_one)
1205 static int __init mthca_init(void)
1209 ret = pci_register_driver(&mthca_driver);
1210 return ret < 0 ? ret : 0;
1213 static void __exit mthca_cleanup(void)
1215 pci_unregister_driver(&mthca_driver);
1218 module_init(mthca_init);
1219 module_exit(mthca_cleanup);