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 int tune_pci = 0;
81 module_param(tune_pci, int, 0444);
82 MODULE_PARM_DESC(tune_pci, "increase PCI burst from the default set by BIOS if nonzero");
84 static const char mthca_version[] __devinitdata =
85 DRV_NAME ": Mellanox InfiniBand HCA driver v"
86 DRV_VERSION " (" DRV_RELDATE ")\n";
88 static struct mthca_profile default_profile = {
95 .num_udav = 1 << 15, /* Tavor only */
96 .fmr_reserved_mtts = 1 << 18, /* Tavor only */
97 .uarc_size = 1 << 18, /* Arbel only */
100 static int __devinit mthca_tune_pci(struct mthca_dev *mdev)
108 /* First try to max out Read Byte Count */
109 cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_PCIX);
111 if (pci_read_config_word(mdev->pdev, cap + PCI_X_CMD, &val)) {
112 mthca_err(mdev, "Couldn't read PCI-X command register, "
116 val = (val & ~PCI_X_CMD_MAX_READ) | (3 << 2);
117 if (pci_write_config_word(mdev->pdev, cap + PCI_X_CMD, val)) {
118 mthca_err(mdev, "Couldn't write PCI-X command register, "
122 } else if (!(mdev->mthca_flags & MTHCA_FLAG_PCIE))
123 mthca_info(mdev, "No PCI-X capability, not setting RBC.\n");
125 cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_EXP);
127 if (pci_read_config_word(mdev->pdev, cap + PCI_EXP_DEVCTL, &val)) {
128 mthca_err(mdev, "Couldn't read PCI Express device control "
129 "register, aborting.\n");
132 val = (val & ~PCI_EXP_DEVCTL_READRQ) | (5 << 12);
133 if (pci_write_config_word(mdev->pdev, cap + PCI_EXP_DEVCTL, val)) {
134 mthca_err(mdev, "Couldn't write PCI Express device control "
135 "register, aborting.\n");
138 } else if (mdev->mthca_flags & MTHCA_FLAG_PCIE)
139 mthca_info(mdev, "No PCI Express capability, "
140 "not setting Max Read Request Size.\n");
145 static int __devinit mthca_dev_lim(struct mthca_dev *mdev, struct mthca_dev_lim *dev_lim)
150 err = mthca_QUERY_DEV_LIM(mdev, dev_lim, &status);
152 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
156 mthca_err(mdev, "QUERY_DEV_LIM returned status 0x%02x, "
157 "aborting.\n", status);
160 if (dev_lim->min_page_sz > PAGE_SIZE) {
161 mthca_err(mdev, "HCA minimum page size of %d bigger than "
162 "kernel PAGE_SIZE of %ld, aborting.\n",
163 dev_lim->min_page_sz, PAGE_SIZE);
166 if (dev_lim->num_ports > MTHCA_MAX_PORTS) {
167 mthca_err(mdev, "HCA has %d ports, but we only support %d, "
169 dev_lim->num_ports, MTHCA_MAX_PORTS);
173 if (dev_lim->uar_size > pci_resource_len(mdev->pdev, 2)) {
174 mthca_err(mdev, "HCA reported UAR size of 0x%x bigger than "
175 "PCI resource 2 size of 0x%llx, aborting.\n",
177 (unsigned long long)pci_resource_len(mdev->pdev, 2));
181 mdev->limits.num_ports = dev_lim->num_ports;
182 mdev->limits.vl_cap = dev_lim->max_vl;
183 mdev->limits.mtu_cap = dev_lim->max_mtu;
184 mdev->limits.gid_table_len = dev_lim->max_gids;
185 mdev->limits.pkey_table_len = dev_lim->max_pkeys;
186 mdev->limits.local_ca_ack_delay = dev_lim->local_ca_ack_delay;
187 mdev->limits.max_sg = dev_lim->max_sg;
188 mdev->limits.max_wqes = dev_lim->max_qp_sz;
189 mdev->limits.max_qp_init_rdma = dev_lim->max_requester_per_qp;
190 mdev->limits.reserved_qps = dev_lim->reserved_qps;
191 mdev->limits.max_srq_wqes = dev_lim->max_srq_sz;
192 mdev->limits.reserved_srqs = dev_lim->reserved_srqs;
193 mdev->limits.reserved_eecs = dev_lim->reserved_eecs;
194 mdev->limits.max_desc_sz = dev_lim->max_desc_sz;
195 mdev->limits.max_srq_sge = mthca_max_srq_sge(mdev);
197 * Subtract 1 from the limit because we need to allocate a
198 * spare CQE so the HCA HW can tell the difference between an
199 * empty CQ and a full CQ.
201 mdev->limits.max_cqes = dev_lim->max_cq_sz - 1;
202 mdev->limits.reserved_cqs = dev_lim->reserved_cqs;
203 mdev->limits.reserved_eqs = dev_lim->reserved_eqs;
204 mdev->limits.reserved_mtts = dev_lim->reserved_mtts;
205 mdev->limits.reserved_mrws = dev_lim->reserved_mrws;
206 mdev->limits.reserved_uars = dev_lim->reserved_uars;
207 mdev->limits.reserved_pds = dev_lim->reserved_pds;
208 mdev->limits.port_width_cap = dev_lim->max_port_width;
209 mdev->limits.page_size_cap = ~(u32) (dev_lim->min_page_sz - 1);
210 mdev->limits.flags = dev_lim->flags;
212 * For old FW that doesn't return static rate support, use a
213 * value of 0x3 (only static rate values of 0 or 1 are handled),
214 * except on Sinai, where even old FW can handle static rate
217 if (dev_lim->stat_rate_support)
218 mdev->limits.stat_rate_support = dev_lim->stat_rate_support;
219 else if (mdev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
220 mdev->limits.stat_rate_support = 0xf;
222 mdev->limits.stat_rate_support = 0x3;
224 /* IB_DEVICE_RESIZE_MAX_WR not supported by driver.
225 May be doable since hardware supports it for SRQ.
227 IB_DEVICE_N_NOTIFY_CQ is supported by hardware but not by driver.
229 IB_DEVICE_SRQ_RESIZE is supported by hardware but SRQ is not
230 supported by driver. */
231 mdev->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
232 IB_DEVICE_PORT_ACTIVE_EVENT |
233 IB_DEVICE_SYS_IMAGE_GUID |
234 IB_DEVICE_RC_RNR_NAK_GEN;
236 if (dev_lim->flags & DEV_LIM_FLAG_BAD_PKEY_CNTR)
237 mdev->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
239 if (dev_lim->flags & DEV_LIM_FLAG_BAD_QKEY_CNTR)
240 mdev->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
242 if (dev_lim->flags & DEV_LIM_FLAG_RAW_MULTI)
243 mdev->device_cap_flags |= IB_DEVICE_RAW_MULTI;
245 if (dev_lim->flags & DEV_LIM_FLAG_AUTO_PATH_MIG)
246 mdev->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
248 if (dev_lim->flags & DEV_LIM_FLAG_UD_AV_PORT_ENFORCE)
249 mdev->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
251 if (dev_lim->flags & DEV_LIM_FLAG_SRQ)
252 mdev->mthca_flags |= MTHCA_FLAG_SRQ;
257 static int __devinit mthca_init_tavor(struct mthca_dev *mdev)
261 struct mthca_dev_lim dev_lim;
262 struct mthca_profile profile;
263 struct mthca_init_hca_param init_hca;
265 err = mthca_SYS_EN(mdev, &status);
267 mthca_err(mdev, "SYS_EN command failed, aborting.\n");
271 mthca_err(mdev, "SYS_EN returned status 0x%02x, "
272 "aborting.\n", status);
276 err = mthca_QUERY_FW(mdev, &status);
278 mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
282 mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
283 "aborting.\n", status);
287 err = mthca_QUERY_DDR(mdev, &status);
289 mthca_err(mdev, "QUERY_DDR command failed, aborting.\n");
293 mthca_err(mdev, "QUERY_DDR returned status 0x%02x, "
294 "aborting.\n", status);
299 err = mthca_dev_lim(mdev, &dev_lim);
301 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
305 profile = default_profile;
306 profile.num_uar = dev_lim.uar_size / PAGE_SIZE;
307 profile.uarc_size = 0;
308 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
309 profile.num_srq = dev_lim.max_srqs;
311 err = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
315 err = mthca_INIT_HCA(mdev, &init_hca, &status);
317 mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
321 mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
322 "aborting.\n", status);
330 mthca_SYS_DIS(mdev, &status);
335 static int __devinit mthca_load_fw(struct mthca_dev *mdev)
340 /* FIXME: use HCA-attached memory for FW if present */
342 mdev->fw.arbel.fw_icm =
343 mthca_alloc_icm(mdev, mdev->fw.arbel.fw_pages,
344 GFP_HIGHUSER | __GFP_NOWARN);
345 if (!mdev->fw.arbel.fw_icm) {
346 mthca_err(mdev, "Couldn't allocate FW area, aborting.\n");
350 err = mthca_MAP_FA(mdev, mdev->fw.arbel.fw_icm, &status);
352 mthca_err(mdev, "MAP_FA command failed, aborting.\n");
356 mthca_err(mdev, "MAP_FA returned status 0x%02x, aborting.\n", status);
360 err = mthca_RUN_FW(mdev, &status);
362 mthca_err(mdev, "RUN_FW command failed, aborting.\n");
366 mthca_err(mdev, "RUN_FW returned status 0x%02x, aborting.\n", status);
374 mthca_UNMAP_FA(mdev, &status);
377 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
381 static int __devinit mthca_init_icm(struct mthca_dev *mdev,
382 struct mthca_dev_lim *dev_lim,
383 struct mthca_init_hca_param *init_hca,
390 err = mthca_SET_ICM_SIZE(mdev, icm_size, &aux_pages, &status);
392 mthca_err(mdev, "SET_ICM_SIZE command failed, aborting.\n");
396 mthca_err(mdev, "SET_ICM_SIZE returned status 0x%02x, "
397 "aborting.\n", status);
401 mthca_dbg(mdev, "%lld KB of HCA context requires %lld KB aux memory.\n",
402 (unsigned long long) icm_size >> 10,
403 (unsigned long long) aux_pages << 2);
405 mdev->fw.arbel.aux_icm = mthca_alloc_icm(mdev, aux_pages,
406 GFP_HIGHUSER | __GFP_NOWARN);
407 if (!mdev->fw.arbel.aux_icm) {
408 mthca_err(mdev, "Couldn't allocate aux memory, aborting.\n");
412 err = mthca_MAP_ICM_AUX(mdev, mdev->fw.arbel.aux_icm, &status);
414 mthca_err(mdev, "MAP_ICM_AUX command failed, aborting.\n");
418 mthca_err(mdev, "MAP_ICM_AUX returned status 0x%02x, aborting.\n", status);
423 err = mthca_map_eq_icm(mdev, init_hca->eqc_base);
425 mthca_err(mdev, "Failed to map EQ context memory, aborting.\n");
429 mdev->mr_table.mtt_table = mthca_alloc_icm_table(mdev, init_hca->mtt_base,
431 mdev->limits.num_mtt_segs,
432 mdev->limits.reserved_mtts, 1);
433 if (!mdev->mr_table.mtt_table) {
434 mthca_err(mdev, "Failed to map MTT context memory, aborting.\n");
439 mdev->mr_table.mpt_table = mthca_alloc_icm_table(mdev, init_hca->mpt_base,
440 dev_lim->mpt_entry_sz,
441 mdev->limits.num_mpts,
442 mdev->limits.reserved_mrws, 1);
443 if (!mdev->mr_table.mpt_table) {
444 mthca_err(mdev, "Failed to map MPT context memory, aborting.\n");
449 mdev->qp_table.qp_table = mthca_alloc_icm_table(mdev, init_hca->qpc_base,
450 dev_lim->qpc_entry_sz,
451 mdev->limits.num_qps,
452 mdev->limits.reserved_qps, 0);
453 if (!mdev->qp_table.qp_table) {
454 mthca_err(mdev, "Failed to map QP context memory, aborting.\n");
459 mdev->qp_table.eqp_table = mthca_alloc_icm_table(mdev, init_hca->eqpc_base,
460 dev_lim->eqpc_entry_sz,
461 mdev->limits.num_qps,
462 mdev->limits.reserved_qps, 0);
463 if (!mdev->qp_table.eqp_table) {
464 mthca_err(mdev, "Failed to map EQP context memory, aborting.\n");
469 mdev->qp_table.rdb_table = mthca_alloc_icm_table(mdev, init_hca->rdb_base,
470 MTHCA_RDB_ENTRY_SIZE,
471 mdev->limits.num_qps <<
472 mdev->qp_table.rdb_shift,
474 if (!mdev->qp_table.rdb_table) {
475 mthca_err(mdev, "Failed to map RDB context memory, aborting\n");
480 mdev->cq_table.table = mthca_alloc_icm_table(mdev, init_hca->cqc_base,
481 dev_lim->cqc_entry_sz,
482 mdev->limits.num_cqs,
483 mdev->limits.reserved_cqs, 0);
484 if (!mdev->cq_table.table) {
485 mthca_err(mdev, "Failed to map CQ context memory, aborting.\n");
490 if (mdev->mthca_flags & MTHCA_FLAG_SRQ) {
491 mdev->srq_table.table =
492 mthca_alloc_icm_table(mdev, init_hca->srqc_base,
493 dev_lim->srq_entry_sz,
494 mdev->limits.num_srqs,
495 mdev->limits.reserved_srqs, 0);
496 if (!mdev->srq_table.table) {
497 mthca_err(mdev, "Failed to map SRQ context memory, "
505 * It's not strictly required, but for simplicity just map the
506 * whole multicast group table now. The table isn't very big
507 * and it's a lot easier than trying to track ref counts.
509 mdev->mcg_table.table = mthca_alloc_icm_table(mdev, init_hca->mc_base,
510 MTHCA_MGM_ENTRY_SIZE,
511 mdev->limits.num_mgms +
512 mdev->limits.num_amgms,
513 mdev->limits.num_mgms +
514 mdev->limits.num_amgms,
516 if (!mdev->mcg_table.table) {
517 mthca_err(mdev, "Failed to map MCG context memory, aborting.\n");
525 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
526 mthca_free_icm_table(mdev, mdev->srq_table.table);
529 mthca_free_icm_table(mdev, mdev->cq_table.table);
532 mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
535 mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
538 mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
541 mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
544 mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
547 mthca_unmap_eq_icm(mdev);
550 mthca_UNMAP_ICM_AUX(mdev, &status);
553 mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
558 static void mthca_free_icms(struct mthca_dev *mdev)
562 mthca_free_icm_table(mdev, mdev->mcg_table.table);
563 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
564 mthca_free_icm_table(mdev, mdev->srq_table.table);
565 mthca_free_icm_table(mdev, mdev->cq_table.table);
566 mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
567 mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
568 mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
569 mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
570 mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
571 mthca_unmap_eq_icm(mdev);
573 mthca_UNMAP_ICM_AUX(mdev, &status);
574 mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
577 static int __devinit mthca_init_arbel(struct mthca_dev *mdev)
579 struct mthca_dev_lim dev_lim;
580 struct mthca_profile profile;
581 struct mthca_init_hca_param init_hca;
586 err = mthca_QUERY_FW(mdev, &status);
588 mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
592 mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
593 "aborting.\n", status);
597 err = mthca_ENABLE_LAM(mdev, &status);
599 mthca_err(mdev, "ENABLE_LAM command failed, aborting.\n");
602 if (status == MTHCA_CMD_STAT_LAM_NOT_PRE) {
603 mthca_dbg(mdev, "No HCA-attached memory (running in MemFree mode)\n");
604 mdev->mthca_flags |= MTHCA_FLAG_NO_LAM;
606 mthca_err(mdev, "ENABLE_LAM returned status 0x%02x, "
607 "aborting.\n", status);
611 err = mthca_load_fw(mdev);
613 mthca_err(mdev, "Failed to start FW, aborting.\n");
617 err = mthca_dev_lim(mdev, &dev_lim);
619 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
623 profile = default_profile;
624 profile.num_uar = dev_lim.uar_size / PAGE_SIZE;
625 profile.num_udav = 0;
626 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
627 profile.num_srq = dev_lim.max_srqs;
629 icm_size = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
630 if ((int) icm_size < 0) {
635 err = mthca_init_icm(mdev, &dev_lim, &init_hca, icm_size);
639 err = mthca_INIT_HCA(mdev, &init_hca, &status);
641 mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
645 mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
646 "aborting.\n", status);
654 mthca_free_icms(mdev);
657 mthca_UNMAP_FA(mdev, &status);
658 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
661 if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
662 mthca_DISABLE_LAM(mdev, &status);
667 static void mthca_close_hca(struct mthca_dev *mdev)
671 mthca_CLOSE_HCA(mdev, 0, &status);
673 if (mthca_is_memfree(mdev)) {
674 mthca_free_icms(mdev);
676 mthca_UNMAP_FA(mdev, &status);
677 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
679 if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
680 mthca_DISABLE_LAM(mdev, &status);
682 mthca_SYS_DIS(mdev, &status);
685 static int __devinit mthca_init_hca(struct mthca_dev *mdev)
689 struct mthca_adapter adapter;
691 if (mthca_is_memfree(mdev))
692 err = mthca_init_arbel(mdev);
694 err = mthca_init_tavor(mdev);
699 err = mthca_QUERY_ADAPTER(mdev, &adapter, &status);
701 mthca_err(mdev, "QUERY_ADAPTER command failed, aborting.\n");
705 mthca_err(mdev, "QUERY_ADAPTER returned status 0x%02x, "
706 "aborting.\n", status);
711 mdev->eq_table.inta_pin = adapter.inta_pin;
712 mdev->rev_id = adapter.revision_id;
713 memcpy(mdev->board_id, adapter.board_id, sizeof mdev->board_id);
718 mthca_close_hca(mdev);
722 static int __devinit mthca_setup_hca(struct mthca_dev *dev)
727 MTHCA_INIT_DOORBELL_LOCK(&dev->doorbell_lock);
729 err = mthca_init_uar_table(dev);
731 mthca_err(dev, "Failed to initialize "
732 "user access region table, aborting.\n");
736 err = mthca_uar_alloc(dev, &dev->driver_uar);
738 mthca_err(dev, "Failed to allocate driver access region, "
740 goto err_uar_table_free;
743 dev->kar = ioremap(dev->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
745 mthca_err(dev, "Couldn't map kernel access region, "
751 err = mthca_init_pd_table(dev);
753 mthca_err(dev, "Failed to initialize "
754 "protection domain table, aborting.\n");
758 err = mthca_init_mr_table(dev);
760 mthca_err(dev, "Failed to initialize "
761 "memory region table, aborting.\n");
762 goto err_pd_table_free;
765 err = mthca_pd_alloc(dev, 1, &dev->driver_pd);
767 mthca_err(dev, "Failed to create driver PD, "
769 goto err_mr_table_free;
772 err = mthca_init_eq_table(dev);
774 mthca_err(dev, "Failed to initialize "
775 "event queue table, aborting.\n");
779 err = mthca_cmd_use_events(dev);
781 mthca_err(dev, "Failed to switch to event-driven "
782 "firmware commands, aborting.\n");
783 goto err_eq_table_free;
786 err = mthca_NOP(dev, &status);
788 mthca_err(dev, "NOP command failed to generate interrupt (IRQ %d), aborting.\n",
789 dev->mthca_flags & MTHCA_FLAG_MSI_X ?
790 dev->eq_table.eq[MTHCA_EQ_CMD].msi_x_vector :
792 if (dev->mthca_flags & (MTHCA_FLAG_MSI | MTHCA_FLAG_MSI_X))
793 mthca_err(dev, "Try again with MSI/MSI-X disabled.\n");
795 mthca_err(dev, "BIOS or ACPI interrupt routing problem?\n");
800 mthca_dbg(dev, "NOP command IRQ test passed\n");
802 err = mthca_init_cq_table(dev);
804 mthca_err(dev, "Failed to initialize "
805 "completion queue table, aborting.\n");
809 err = mthca_init_srq_table(dev);
811 mthca_err(dev, "Failed to initialize "
812 "shared receive queue table, aborting.\n");
813 goto err_cq_table_free;
816 err = mthca_init_qp_table(dev);
818 mthca_err(dev, "Failed to initialize "
819 "queue pair table, aborting.\n");
820 goto err_srq_table_free;
823 err = mthca_init_av_table(dev);
825 mthca_err(dev, "Failed to initialize "
826 "address vector table, aborting.\n");
827 goto err_qp_table_free;
830 err = mthca_init_mcg_table(dev);
832 mthca_err(dev, "Failed to initialize "
833 "multicast group table, aborting.\n");
834 goto err_av_table_free;
840 mthca_cleanup_av_table(dev);
843 mthca_cleanup_qp_table(dev);
846 mthca_cleanup_srq_table(dev);
849 mthca_cleanup_cq_table(dev);
852 mthca_cmd_use_polling(dev);
855 mthca_cleanup_eq_table(dev);
858 mthca_pd_free(dev, &dev->driver_pd);
861 mthca_cleanup_mr_table(dev);
864 mthca_cleanup_pd_table(dev);
870 mthca_uar_free(dev, &dev->driver_uar);
873 mthca_cleanup_uar_table(dev);
877 static int __devinit mthca_request_regions(struct pci_dev *pdev,
883 * We can't just use pci_request_regions() because the MSI-X
884 * table is right in the middle of the first BAR. If we did
885 * pci_request_region and grab all of the first BAR, then
886 * setting up MSI-X would fail, since the PCI core wants to do
887 * request_mem_region on the MSI-X vector table.
889 * So just request what we need right now, and request any
890 * other regions we need when setting up EQs.
892 if (!request_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
893 MTHCA_HCR_SIZE, DRV_NAME))
896 err = pci_request_region(pdev, 2, DRV_NAME);
898 goto err_bar2_failed;
901 err = pci_request_region(pdev, 4, DRV_NAME);
903 goto err_bar4_failed;
909 pci_release_region(pdev, 2);
912 release_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
918 static void mthca_release_regions(struct pci_dev *pdev,
922 pci_release_region(pdev, 4);
924 pci_release_region(pdev, 2);
926 release_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
930 static int __devinit mthca_enable_msi_x(struct mthca_dev *mdev)
932 struct msix_entry entries[3];
935 entries[0].entry = 0;
936 entries[1].entry = 1;
937 entries[2].entry = 2;
939 err = pci_enable_msix(mdev->pdev, entries, ARRAY_SIZE(entries));
942 mthca_info(mdev, "Only %d MSI-X vectors available, "
943 "not using MSI-X\n", err);
947 mdev->eq_table.eq[MTHCA_EQ_COMP ].msi_x_vector = entries[0].vector;
948 mdev->eq_table.eq[MTHCA_EQ_ASYNC].msi_x_vector = entries[1].vector;
949 mdev->eq_table.eq[MTHCA_EQ_CMD ].msi_x_vector = entries[2].vector;
954 /* Types of supported HCA */
957 ARBEL_COMPAT, /* MT25208 in Tavor compat mode */
958 ARBEL_NATIVE, /* MT25208 with extended features */
962 #define MTHCA_FW_VER(major, minor, subminor) \
963 (((u64) (major) << 32) | ((u64) (minor) << 16) | (u64) (subminor))
968 } mthca_hca_table[] = {
969 [TAVOR] = { .latest_fw = MTHCA_FW_VER(3, 4, 0),
971 [ARBEL_COMPAT] = { .latest_fw = MTHCA_FW_VER(4, 7, 400),
972 .flags = MTHCA_FLAG_PCIE },
973 [ARBEL_NATIVE] = { .latest_fw = MTHCA_FW_VER(5, 1, 0),
974 .flags = MTHCA_FLAG_MEMFREE |
976 [SINAI] = { .latest_fw = MTHCA_FW_VER(1, 0, 800),
977 .flags = MTHCA_FLAG_MEMFREE |
979 MTHCA_FLAG_SINAI_OPT }
982 static int __devinit mthca_init_one(struct pci_dev *pdev,
983 const struct pci_device_id *id)
985 static int mthca_version_printed = 0;
988 struct mthca_dev *mdev;
990 if (!mthca_version_printed) {
991 printk(KERN_INFO "%s", mthca_version);
992 ++mthca_version_printed;
995 printk(KERN_INFO PFX "Initializing %s\n",
998 if (id->driver_data >= ARRAY_SIZE(mthca_hca_table)) {
999 printk(KERN_ERR PFX "%s has invalid driver data %lx\n",
1000 pci_name(pdev), id->driver_data);
1004 err = pci_enable_device(pdev);
1006 dev_err(&pdev->dev, "Cannot enable PCI device, "
1012 * Check for BARs. We expect 0: 1MB, 2: 8MB, 4: DDR (may not
1015 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
1016 pci_resource_len(pdev, 0) != 1 << 20) {
1017 dev_err(&pdev->dev, "Missing DCS, aborting.\n");
1019 goto err_disable_pdev;
1021 if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
1022 dev_err(&pdev->dev, "Missing UAR, aborting.\n");
1024 goto err_disable_pdev;
1026 if (!(pci_resource_flags(pdev, 4) & IORESOURCE_MEM))
1029 err = mthca_request_regions(pdev, ddr_hidden);
1031 dev_err(&pdev->dev, "Cannot obtain PCI resources, "
1033 goto err_disable_pdev;
1036 pci_set_master(pdev);
1038 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
1040 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask.\n");
1041 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1043 dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting.\n");
1047 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
1049 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit "
1050 "consistent PCI DMA mask.\n");
1051 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
1053 dev_err(&pdev->dev, "Can't set consistent PCI DMA mask, "
1059 mdev = (struct mthca_dev *) ib_alloc_device(sizeof *mdev);
1061 dev_err(&pdev->dev, "Device struct alloc failed, "
1069 mdev->mthca_flags = mthca_hca_table[id->driver_data].flags;
1071 mdev->mthca_flags |= MTHCA_FLAG_DDR_HIDDEN;
1074 * Now reset the HCA before we touch the PCI capabilities or
1075 * attempt a firmware command, since a boot ROM may have left
1076 * the HCA in an undefined state.
1078 err = mthca_reset(mdev);
1080 mthca_err(mdev, "Failed to reset HCA, aborting.\n");
1084 if (msi_x && !mthca_enable_msi_x(mdev))
1085 mdev->mthca_flags |= MTHCA_FLAG_MSI_X;
1086 if (msi && !(mdev->mthca_flags & MTHCA_FLAG_MSI_X) &&
1087 !pci_enable_msi(pdev))
1088 mdev->mthca_flags |= MTHCA_FLAG_MSI;
1090 if (mthca_cmd_init(mdev)) {
1091 mthca_err(mdev, "Failed to init command interface, aborting.\n");
1095 err = mthca_tune_pci(mdev);
1099 err = mthca_init_hca(mdev);
1103 if (mdev->fw_ver < mthca_hca_table[id->driver_data].latest_fw) {
1104 mthca_warn(mdev, "HCA FW version %d.%d.%d is old (%d.%d.%d is current).\n",
1105 (int) (mdev->fw_ver >> 32), (int) (mdev->fw_ver >> 16) & 0xffff,
1106 (int) (mdev->fw_ver & 0xffff),
1107 (int) (mthca_hca_table[id->driver_data].latest_fw >> 32),
1108 (int) (mthca_hca_table[id->driver_data].latest_fw >> 16) & 0xffff,
1109 (int) (mthca_hca_table[id->driver_data].latest_fw & 0xffff));
1110 mthca_warn(mdev, "If you have problems, try updating your HCA FW.\n");
1113 err = mthca_setup_hca(mdev);
1117 err = mthca_register_device(mdev);
1121 err = mthca_create_agents(mdev);
1123 goto err_unregister;
1125 pci_set_drvdata(pdev, mdev);
1130 mthca_unregister_device(mdev);
1133 mthca_cleanup_mcg_table(mdev);
1134 mthca_cleanup_av_table(mdev);
1135 mthca_cleanup_qp_table(mdev);
1136 mthca_cleanup_srq_table(mdev);
1137 mthca_cleanup_cq_table(mdev);
1138 mthca_cmd_use_polling(mdev);
1139 mthca_cleanup_eq_table(mdev);
1141 mthca_pd_free(mdev, &mdev->driver_pd);
1143 mthca_cleanup_mr_table(mdev);
1144 mthca_cleanup_pd_table(mdev);
1145 mthca_cleanup_uar_table(mdev);
1148 mthca_close_hca(mdev);
1151 mthca_cmd_cleanup(mdev);
1154 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1155 pci_disable_msix(pdev);
1156 if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1157 pci_disable_msi(pdev);
1159 ib_dealloc_device(&mdev->ib_dev);
1162 mthca_release_regions(pdev, ddr_hidden);
1165 pci_disable_device(pdev);
1166 pci_set_drvdata(pdev, NULL);
1170 static void __devexit mthca_remove_one(struct pci_dev *pdev)
1172 struct mthca_dev *mdev = pci_get_drvdata(pdev);
1177 mthca_free_agents(mdev);
1178 mthca_unregister_device(mdev);
1180 for (p = 1; p <= mdev->limits.num_ports; ++p)
1181 mthca_CLOSE_IB(mdev, p, &status);
1183 mthca_cleanup_mcg_table(mdev);
1184 mthca_cleanup_av_table(mdev);
1185 mthca_cleanup_qp_table(mdev);
1186 mthca_cleanup_srq_table(mdev);
1187 mthca_cleanup_cq_table(mdev);
1188 mthca_cmd_use_polling(mdev);
1189 mthca_cleanup_eq_table(mdev);
1191 mthca_pd_free(mdev, &mdev->driver_pd);
1193 mthca_cleanup_mr_table(mdev);
1194 mthca_cleanup_pd_table(mdev);
1197 mthca_uar_free(mdev, &mdev->driver_uar);
1198 mthca_cleanup_uar_table(mdev);
1199 mthca_close_hca(mdev);
1200 mthca_cmd_cleanup(mdev);
1202 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1203 pci_disable_msix(pdev);
1204 if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1205 pci_disable_msi(pdev);
1207 ib_dealloc_device(&mdev->ib_dev);
1208 mthca_release_regions(pdev, mdev->mthca_flags &
1209 MTHCA_FLAG_DDR_HIDDEN);
1210 pci_disable_device(pdev);
1211 pci_set_drvdata(pdev, NULL);
1215 static struct pci_device_id mthca_pci_table[] = {
1216 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_TAVOR),
1217 .driver_data = TAVOR },
1218 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_TAVOR),
1219 .driver_data = TAVOR },
1220 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
1221 .driver_data = ARBEL_COMPAT },
1222 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
1223 .driver_data = ARBEL_COMPAT },
1224 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL),
1225 .driver_data = ARBEL_NATIVE },
1226 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL),
1227 .driver_data = ARBEL_NATIVE },
1228 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI),
1229 .driver_data = SINAI },
1230 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI),
1231 .driver_data = SINAI },
1232 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
1233 .driver_data = SINAI },
1234 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
1235 .driver_data = SINAI },
1239 MODULE_DEVICE_TABLE(pci, mthca_pci_table);
1241 static struct pci_driver mthca_driver = {
1243 .id_table = mthca_pci_table,
1244 .probe = mthca_init_one,
1245 .remove = __devexit_p(mthca_remove_one)
1248 static int __init mthca_init(void)
1252 ret = pci_register_driver(&mthca_driver);
1253 return ret < 0 ? ret : 0;
1256 static void __exit mthca_cleanup(void)
1258 pci_unregister_driver(&mthca_driver);
1261 module_init(mthca_init);
1262 module_exit(mthca_cleanup);