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 DEFINE_MUTEX(mthca_device_mutex);
85 #define MTHCA_DEFAULT_NUM_QP (1 << 16)
86 #define MTHCA_DEFAULT_RDB_PER_QP (1 << 2)
87 #define MTHCA_DEFAULT_NUM_CQ (1 << 16)
88 #define MTHCA_DEFAULT_NUM_MCG (1 << 13)
89 #define MTHCA_DEFAULT_NUM_MPT (1 << 17)
90 #define MTHCA_DEFAULT_NUM_MTT (1 << 20)
91 #define MTHCA_DEFAULT_NUM_UDAV (1 << 15)
92 #define MTHCA_DEFAULT_NUM_RESERVED_MTTS (1 << 18)
93 #define MTHCA_DEFAULT_NUM_UARC_SIZE (1 << 18)
95 static struct mthca_profile hca_profile = {
96 .num_qp = MTHCA_DEFAULT_NUM_QP,
97 .rdb_per_qp = MTHCA_DEFAULT_RDB_PER_QP,
98 .num_cq = MTHCA_DEFAULT_NUM_CQ,
99 .num_mcg = MTHCA_DEFAULT_NUM_MCG,
100 .num_mpt = MTHCA_DEFAULT_NUM_MPT,
101 .num_mtt = MTHCA_DEFAULT_NUM_MTT,
102 .num_udav = MTHCA_DEFAULT_NUM_UDAV, /* Tavor only */
103 .fmr_reserved_mtts = MTHCA_DEFAULT_NUM_RESERVED_MTTS, /* Tavor only */
104 .uarc_size = MTHCA_DEFAULT_NUM_UARC_SIZE, /* Arbel only */
107 module_param_named(num_qp, hca_profile.num_qp, int, 0444);
108 MODULE_PARM_DESC(num_qp, "maximum number of QPs per HCA");
110 module_param_named(rdb_per_qp, hca_profile.rdb_per_qp, int, 0444);
111 MODULE_PARM_DESC(rdb_per_qp, "number of RDB buffers per QP");
113 module_param_named(num_cq, hca_profile.num_cq, int, 0444);
114 MODULE_PARM_DESC(num_cq, "maximum number of CQs per HCA");
116 module_param_named(num_mcg, hca_profile.num_mcg, int, 0444);
117 MODULE_PARM_DESC(num_mcg, "maximum number of multicast groups per HCA");
119 module_param_named(num_mpt, hca_profile.num_mpt, int, 0444);
120 MODULE_PARM_DESC(num_mpt,
121 "maximum number of memory protection table entries per HCA");
123 module_param_named(num_mtt, hca_profile.num_mtt, int, 0444);
124 MODULE_PARM_DESC(num_mtt,
125 "maximum number of memory translation table segments per HCA");
127 module_param_named(num_udav, hca_profile.num_udav, int, 0444);
128 MODULE_PARM_DESC(num_udav, "maximum number of UD address vectors per HCA");
130 module_param_named(fmr_reserved_mtts, hca_profile.fmr_reserved_mtts, int, 0444);
131 MODULE_PARM_DESC(fmr_reserved_mtts,
132 "number of memory translation table segments reserved for FMR");
134 static const char mthca_version[] __devinitdata =
135 DRV_NAME ": Mellanox InfiniBand HCA driver v"
136 DRV_VERSION " (" DRV_RELDATE ")\n";
138 static int mthca_tune_pci(struct mthca_dev *mdev)
146 /* First try to max out Read Byte Count */
147 cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_PCIX);
149 if (pci_read_config_word(mdev->pdev, cap + PCI_X_CMD, &val)) {
150 mthca_err(mdev, "Couldn't read PCI-X command register, "
154 val = (val & ~PCI_X_CMD_MAX_READ) | (3 << 2);
155 if (pci_write_config_word(mdev->pdev, cap + PCI_X_CMD, val)) {
156 mthca_err(mdev, "Couldn't write PCI-X command register, "
160 } else if (!(mdev->mthca_flags & MTHCA_FLAG_PCIE))
161 mthca_info(mdev, "No PCI-X capability, not setting RBC.\n");
163 cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_EXP);
165 if (pci_read_config_word(mdev->pdev, cap + PCI_EXP_DEVCTL, &val)) {
166 mthca_err(mdev, "Couldn't read PCI Express device control "
167 "register, aborting.\n");
170 val = (val & ~PCI_EXP_DEVCTL_READRQ) | (5 << 12);
171 if (pci_write_config_word(mdev->pdev, cap + PCI_EXP_DEVCTL, val)) {
172 mthca_err(mdev, "Couldn't write PCI Express device control "
173 "register, aborting.\n");
176 } else if (mdev->mthca_flags & MTHCA_FLAG_PCIE)
177 mthca_info(mdev, "No PCI Express capability, "
178 "not setting Max Read Request Size.\n");
183 static int mthca_dev_lim(struct mthca_dev *mdev, struct mthca_dev_lim *dev_lim)
188 err = mthca_QUERY_DEV_LIM(mdev, dev_lim, &status);
190 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
194 mthca_err(mdev, "QUERY_DEV_LIM returned status 0x%02x, "
195 "aborting.\n", status);
198 if (dev_lim->min_page_sz > PAGE_SIZE) {
199 mthca_err(mdev, "HCA minimum page size of %d bigger than "
200 "kernel PAGE_SIZE of %ld, aborting.\n",
201 dev_lim->min_page_sz, PAGE_SIZE);
204 if (dev_lim->num_ports > MTHCA_MAX_PORTS) {
205 mthca_err(mdev, "HCA has %d ports, but we only support %d, "
207 dev_lim->num_ports, MTHCA_MAX_PORTS);
211 if (dev_lim->uar_size > pci_resource_len(mdev->pdev, 2)) {
212 mthca_err(mdev, "HCA reported UAR size of 0x%x bigger than "
213 "PCI resource 2 size of 0x%llx, aborting.\n",
215 (unsigned long long)pci_resource_len(mdev->pdev, 2));
219 mdev->limits.num_ports = dev_lim->num_ports;
220 mdev->limits.vl_cap = dev_lim->max_vl;
221 mdev->limits.mtu_cap = dev_lim->max_mtu;
222 mdev->limits.gid_table_len = dev_lim->max_gids;
223 mdev->limits.pkey_table_len = dev_lim->max_pkeys;
224 mdev->limits.local_ca_ack_delay = dev_lim->local_ca_ack_delay;
225 mdev->limits.max_sg = dev_lim->max_sg;
226 mdev->limits.max_wqes = dev_lim->max_qp_sz;
227 mdev->limits.max_qp_init_rdma = dev_lim->max_requester_per_qp;
228 mdev->limits.reserved_qps = dev_lim->reserved_qps;
229 mdev->limits.max_srq_wqes = dev_lim->max_srq_sz;
230 mdev->limits.reserved_srqs = dev_lim->reserved_srqs;
231 mdev->limits.reserved_eecs = dev_lim->reserved_eecs;
232 mdev->limits.max_desc_sz = dev_lim->max_desc_sz;
233 mdev->limits.max_srq_sge = mthca_max_srq_sge(mdev);
235 * Subtract 1 from the limit because we need to allocate a
236 * spare CQE so the HCA HW can tell the difference between an
237 * empty CQ and a full CQ.
239 mdev->limits.max_cqes = dev_lim->max_cq_sz - 1;
240 mdev->limits.reserved_cqs = dev_lim->reserved_cqs;
241 mdev->limits.reserved_eqs = dev_lim->reserved_eqs;
242 mdev->limits.reserved_mtts = dev_lim->reserved_mtts;
243 mdev->limits.reserved_mrws = dev_lim->reserved_mrws;
244 mdev->limits.reserved_uars = dev_lim->reserved_uars;
245 mdev->limits.reserved_pds = dev_lim->reserved_pds;
246 mdev->limits.port_width_cap = dev_lim->max_port_width;
247 mdev->limits.page_size_cap = ~(u32) (dev_lim->min_page_sz - 1);
248 mdev->limits.flags = dev_lim->flags;
250 * For old FW that doesn't return static rate support, use a
251 * value of 0x3 (only static rate values of 0 or 1 are handled),
252 * except on Sinai, where even old FW can handle static rate
255 if (dev_lim->stat_rate_support)
256 mdev->limits.stat_rate_support = dev_lim->stat_rate_support;
257 else if (mdev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
258 mdev->limits.stat_rate_support = 0xf;
260 mdev->limits.stat_rate_support = 0x3;
262 /* IB_DEVICE_RESIZE_MAX_WR not supported by driver.
263 May be doable since hardware supports it for SRQ.
265 IB_DEVICE_N_NOTIFY_CQ is supported by hardware but not by driver.
267 IB_DEVICE_SRQ_RESIZE is supported by hardware but SRQ is not
268 supported by driver. */
269 mdev->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
270 IB_DEVICE_PORT_ACTIVE_EVENT |
271 IB_DEVICE_SYS_IMAGE_GUID |
272 IB_DEVICE_RC_RNR_NAK_GEN;
274 if (dev_lim->flags & DEV_LIM_FLAG_BAD_PKEY_CNTR)
275 mdev->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
277 if (dev_lim->flags & DEV_LIM_FLAG_BAD_QKEY_CNTR)
278 mdev->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
280 if (dev_lim->flags & DEV_LIM_FLAG_RAW_MULTI)
281 mdev->device_cap_flags |= IB_DEVICE_RAW_MULTI;
283 if (dev_lim->flags & DEV_LIM_FLAG_AUTO_PATH_MIG)
284 mdev->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
286 if (dev_lim->flags & DEV_LIM_FLAG_UD_AV_PORT_ENFORCE)
287 mdev->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
289 if (dev_lim->flags & DEV_LIM_FLAG_SRQ)
290 mdev->mthca_flags |= MTHCA_FLAG_SRQ;
295 static int mthca_init_tavor(struct mthca_dev *mdev)
299 struct mthca_dev_lim dev_lim;
300 struct mthca_profile profile;
301 struct mthca_init_hca_param init_hca;
303 err = mthca_SYS_EN(mdev, &status);
305 mthca_err(mdev, "SYS_EN command failed, aborting.\n");
309 mthca_err(mdev, "SYS_EN returned status 0x%02x, "
310 "aborting.\n", status);
314 err = mthca_QUERY_FW(mdev, &status);
316 mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
320 mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
321 "aborting.\n", status);
325 err = mthca_QUERY_DDR(mdev, &status);
327 mthca_err(mdev, "QUERY_DDR command failed, aborting.\n");
331 mthca_err(mdev, "QUERY_DDR returned status 0x%02x, "
332 "aborting.\n", status);
337 err = mthca_dev_lim(mdev, &dev_lim);
339 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
343 profile = hca_profile;
344 profile.num_uar = dev_lim.uar_size / PAGE_SIZE;
345 profile.uarc_size = 0;
346 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
347 profile.num_srq = dev_lim.max_srqs;
349 err = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
353 err = mthca_INIT_HCA(mdev, &init_hca, &status);
355 mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
359 mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
360 "aborting.\n", status);
368 mthca_SYS_DIS(mdev, &status);
373 static int mthca_load_fw(struct mthca_dev *mdev)
378 /* FIXME: use HCA-attached memory for FW if present */
380 mdev->fw.arbel.fw_icm =
381 mthca_alloc_icm(mdev, mdev->fw.arbel.fw_pages,
382 GFP_HIGHUSER | __GFP_NOWARN, 0);
383 if (!mdev->fw.arbel.fw_icm) {
384 mthca_err(mdev, "Couldn't allocate FW area, aborting.\n");
388 err = mthca_MAP_FA(mdev, mdev->fw.arbel.fw_icm, &status);
390 mthca_err(mdev, "MAP_FA command failed, aborting.\n");
394 mthca_err(mdev, "MAP_FA returned status 0x%02x, aborting.\n", status);
398 err = mthca_RUN_FW(mdev, &status);
400 mthca_err(mdev, "RUN_FW command failed, aborting.\n");
404 mthca_err(mdev, "RUN_FW returned status 0x%02x, aborting.\n", status);
412 mthca_UNMAP_FA(mdev, &status);
415 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm, 0);
419 static int mthca_init_icm(struct mthca_dev *mdev,
420 struct mthca_dev_lim *dev_lim,
421 struct mthca_init_hca_param *init_hca,
428 err = mthca_SET_ICM_SIZE(mdev, icm_size, &aux_pages, &status);
430 mthca_err(mdev, "SET_ICM_SIZE command failed, aborting.\n");
434 mthca_err(mdev, "SET_ICM_SIZE returned status 0x%02x, "
435 "aborting.\n", status);
439 mthca_dbg(mdev, "%lld KB of HCA context requires %lld KB aux memory.\n",
440 (unsigned long long) icm_size >> 10,
441 (unsigned long long) aux_pages << 2);
443 mdev->fw.arbel.aux_icm = mthca_alloc_icm(mdev, aux_pages,
444 GFP_HIGHUSER | __GFP_NOWARN, 0);
445 if (!mdev->fw.arbel.aux_icm) {
446 mthca_err(mdev, "Couldn't allocate aux memory, aborting.\n");
450 err = mthca_MAP_ICM_AUX(mdev, mdev->fw.arbel.aux_icm, &status);
452 mthca_err(mdev, "MAP_ICM_AUX command failed, aborting.\n");
456 mthca_err(mdev, "MAP_ICM_AUX returned status 0x%02x, aborting.\n", status);
461 err = mthca_map_eq_icm(mdev, init_hca->eqc_base);
463 mthca_err(mdev, "Failed to map EQ context memory, aborting.\n");
467 /* CPU writes to non-reserved MTTs, while HCA might DMA to reserved mtts */
468 mdev->limits.reserved_mtts = ALIGN(mdev->limits.reserved_mtts * MTHCA_MTT_SEG_SIZE,
469 dma_get_cache_alignment()) / MTHCA_MTT_SEG_SIZE;
471 mdev->mr_table.mtt_table = mthca_alloc_icm_table(mdev, init_hca->mtt_base,
473 mdev->limits.num_mtt_segs,
474 mdev->limits.reserved_mtts,
476 if (!mdev->mr_table.mtt_table) {
477 mthca_err(mdev, "Failed to map MTT context memory, aborting.\n");
482 mdev->mr_table.mpt_table = mthca_alloc_icm_table(mdev, init_hca->mpt_base,
483 dev_lim->mpt_entry_sz,
484 mdev->limits.num_mpts,
485 mdev->limits.reserved_mrws,
487 if (!mdev->mr_table.mpt_table) {
488 mthca_err(mdev, "Failed to map MPT context memory, aborting.\n");
493 mdev->qp_table.qp_table = mthca_alloc_icm_table(mdev, init_hca->qpc_base,
494 dev_lim->qpc_entry_sz,
495 mdev->limits.num_qps,
496 mdev->limits.reserved_qps,
498 if (!mdev->qp_table.qp_table) {
499 mthca_err(mdev, "Failed to map QP context memory, aborting.\n");
504 mdev->qp_table.eqp_table = mthca_alloc_icm_table(mdev, init_hca->eqpc_base,
505 dev_lim->eqpc_entry_sz,
506 mdev->limits.num_qps,
507 mdev->limits.reserved_qps,
509 if (!mdev->qp_table.eqp_table) {
510 mthca_err(mdev, "Failed to map EQP context memory, aborting.\n");
515 mdev->qp_table.rdb_table = mthca_alloc_icm_table(mdev, init_hca->rdb_base,
516 MTHCA_RDB_ENTRY_SIZE,
517 mdev->limits.num_qps <<
518 mdev->qp_table.rdb_shift, 0,
520 if (!mdev->qp_table.rdb_table) {
521 mthca_err(mdev, "Failed to map RDB context memory, aborting\n");
526 mdev->cq_table.table = mthca_alloc_icm_table(mdev, init_hca->cqc_base,
527 dev_lim->cqc_entry_sz,
528 mdev->limits.num_cqs,
529 mdev->limits.reserved_cqs,
531 if (!mdev->cq_table.table) {
532 mthca_err(mdev, "Failed to map CQ context memory, aborting.\n");
537 if (mdev->mthca_flags & MTHCA_FLAG_SRQ) {
538 mdev->srq_table.table =
539 mthca_alloc_icm_table(mdev, init_hca->srqc_base,
540 dev_lim->srq_entry_sz,
541 mdev->limits.num_srqs,
542 mdev->limits.reserved_srqs,
544 if (!mdev->srq_table.table) {
545 mthca_err(mdev, "Failed to map SRQ context memory, "
553 * It's not strictly required, but for simplicity just map the
554 * whole multicast group table now. The table isn't very big
555 * and it's a lot easier than trying to track ref counts.
557 mdev->mcg_table.table = mthca_alloc_icm_table(mdev, init_hca->mc_base,
558 MTHCA_MGM_ENTRY_SIZE,
559 mdev->limits.num_mgms +
560 mdev->limits.num_amgms,
561 mdev->limits.num_mgms +
562 mdev->limits.num_amgms,
564 if (!mdev->mcg_table.table) {
565 mthca_err(mdev, "Failed to map MCG context memory, aborting.\n");
573 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
574 mthca_free_icm_table(mdev, mdev->srq_table.table);
577 mthca_free_icm_table(mdev, mdev->cq_table.table);
580 mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
583 mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
586 mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
589 mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
592 mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
595 mthca_unmap_eq_icm(mdev);
598 mthca_UNMAP_ICM_AUX(mdev, &status);
601 mthca_free_icm(mdev, mdev->fw.arbel.aux_icm, 0);
606 static void mthca_free_icms(struct mthca_dev *mdev)
610 mthca_free_icm_table(mdev, mdev->mcg_table.table);
611 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
612 mthca_free_icm_table(mdev, mdev->srq_table.table);
613 mthca_free_icm_table(mdev, mdev->cq_table.table);
614 mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
615 mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
616 mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
617 mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
618 mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
619 mthca_unmap_eq_icm(mdev);
621 mthca_UNMAP_ICM_AUX(mdev, &status);
622 mthca_free_icm(mdev, mdev->fw.arbel.aux_icm, 0);
625 static int mthca_init_arbel(struct mthca_dev *mdev)
627 struct mthca_dev_lim dev_lim;
628 struct mthca_profile profile;
629 struct mthca_init_hca_param init_hca;
634 err = mthca_QUERY_FW(mdev, &status);
636 mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
640 mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
641 "aborting.\n", status);
645 err = mthca_ENABLE_LAM(mdev, &status);
647 mthca_err(mdev, "ENABLE_LAM command failed, aborting.\n");
650 if (status == MTHCA_CMD_STAT_LAM_NOT_PRE) {
651 mthca_dbg(mdev, "No HCA-attached memory (running in MemFree mode)\n");
652 mdev->mthca_flags |= MTHCA_FLAG_NO_LAM;
654 mthca_err(mdev, "ENABLE_LAM returned status 0x%02x, "
655 "aborting.\n", status);
659 err = mthca_load_fw(mdev);
661 mthca_err(mdev, "Failed to start FW, aborting.\n");
665 err = mthca_dev_lim(mdev, &dev_lim);
667 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
671 profile = hca_profile;
672 profile.num_uar = dev_lim.uar_size / PAGE_SIZE;
673 profile.num_udav = 0;
674 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
675 profile.num_srq = dev_lim.max_srqs;
677 icm_size = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
678 if ((int) icm_size < 0) {
683 err = mthca_init_icm(mdev, &dev_lim, &init_hca, icm_size);
687 err = mthca_INIT_HCA(mdev, &init_hca, &status);
689 mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
693 mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
694 "aborting.\n", status);
702 mthca_free_icms(mdev);
705 mthca_UNMAP_FA(mdev, &status);
706 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm, 0);
709 if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
710 mthca_DISABLE_LAM(mdev, &status);
715 static void mthca_close_hca(struct mthca_dev *mdev)
719 mthca_CLOSE_HCA(mdev, 0, &status);
721 if (mthca_is_memfree(mdev)) {
722 mthca_free_icms(mdev);
724 mthca_UNMAP_FA(mdev, &status);
725 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm, 0);
727 if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
728 mthca_DISABLE_LAM(mdev, &status);
730 mthca_SYS_DIS(mdev, &status);
733 static int mthca_init_hca(struct mthca_dev *mdev)
737 struct mthca_adapter adapter;
739 if (mthca_is_memfree(mdev))
740 err = mthca_init_arbel(mdev);
742 err = mthca_init_tavor(mdev);
747 err = mthca_QUERY_ADAPTER(mdev, &adapter, &status);
749 mthca_err(mdev, "QUERY_ADAPTER command failed, aborting.\n");
753 mthca_err(mdev, "QUERY_ADAPTER returned status 0x%02x, "
754 "aborting.\n", status);
759 mdev->eq_table.inta_pin = adapter.inta_pin;
760 mdev->rev_id = adapter.revision_id;
761 memcpy(mdev->board_id, adapter.board_id, sizeof mdev->board_id);
766 mthca_close_hca(mdev);
770 static int mthca_setup_hca(struct mthca_dev *dev)
775 MTHCA_INIT_DOORBELL_LOCK(&dev->doorbell_lock);
777 err = mthca_init_uar_table(dev);
779 mthca_err(dev, "Failed to initialize "
780 "user access region table, aborting.\n");
784 err = mthca_uar_alloc(dev, &dev->driver_uar);
786 mthca_err(dev, "Failed to allocate driver access region, "
788 goto err_uar_table_free;
791 dev->kar = ioremap(dev->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
793 mthca_err(dev, "Couldn't map kernel access region, "
799 err = mthca_init_pd_table(dev);
801 mthca_err(dev, "Failed to initialize "
802 "protection domain table, aborting.\n");
806 err = mthca_init_mr_table(dev);
808 mthca_err(dev, "Failed to initialize "
809 "memory region table, aborting.\n");
810 goto err_pd_table_free;
813 err = mthca_pd_alloc(dev, 1, &dev->driver_pd);
815 mthca_err(dev, "Failed to create driver PD, "
817 goto err_mr_table_free;
820 err = mthca_init_eq_table(dev);
822 mthca_err(dev, "Failed to initialize "
823 "event queue table, aborting.\n");
827 err = mthca_cmd_use_events(dev);
829 mthca_err(dev, "Failed to switch to event-driven "
830 "firmware commands, aborting.\n");
831 goto err_eq_table_free;
834 err = mthca_NOP(dev, &status);
836 mthca_err(dev, "NOP command failed to generate interrupt (IRQ %d), aborting.\n",
837 dev->mthca_flags & MTHCA_FLAG_MSI_X ?
838 dev->eq_table.eq[MTHCA_EQ_CMD].msi_x_vector :
840 if (dev->mthca_flags & (MTHCA_FLAG_MSI | MTHCA_FLAG_MSI_X))
841 mthca_err(dev, "Try again with MSI/MSI-X disabled.\n");
843 mthca_err(dev, "BIOS or ACPI interrupt routing problem?\n");
848 mthca_dbg(dev, "NOP command IRQ test passed\n");
850 err = mthca_init_cq_table(dev);
852 mthca_err(dev, "Failed to initialize "
853 "completion queue table, aborting.\n");
857 err = mthca_init_srq_table(dev);
859 mthca_err(dev, "Failed to initialize "
860 "shared receive queue table, aborting.\n");
861 goto err_cq_table_free;
864 err = mthca_init_qp_table(dev);
866 mthca_err(dev, "Failed to initialize "
867 "queue pair table, aborting.\n");
868 goto err_srq_table_free;
871 err = mthca_init_av_table(dev);
873 mthca_err(dev, "Failed to initialize "
874 "address vector table, aborting.\n");
875 goto err_qp_table_free;
878 err = mthca_init_mcg_table(dev);
880 mthca_err(dev, "Failed to initialize "
881 "multicast group table, aborting.\n");
882 goto err_av_table_free;
888 mthca_cleanup_av_table(dev);
891 mthca_cleanup_qp_table(dev);
894 mthca_cleanup_srq_table(dev);
897 mthca_cleanup_cq_table(dev);
900 mthca_cmd_use_polling(dev);
903 mthca_cleanup_eq_table(dev);
906 mthca_pd_free(dev, &dev->driver_pd);
909 mthca_cleanup_mr_table(dev);
912 mthca_cleanup_pd_table(dev);
918 mthca_uar_free(dev, &dev->driver_uar);
921 mthca_cleanup_uar_table(dev);
925 static int mthca_request_regions(struct pci_dev *pdev, int ddr_hidden)
930 * We can't just use pci_request_regions() because the MSI-X
931 * table is right in the middle of the first BAR. If we did
932 * pci_request_region and grab all of the first BAR, then
933 * setting up MSI-X would fail, since the PCI core wants to do
934 * request_mem_region on the MSI-X vector table.
936 * So just request what we need right now, and request any
937 * other regions we need when setting up EQs.
939 if (!request_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
940 MTHCA_HCR_SIZE, DRV_NAME))
943 err = pci_request_region(pdev, 2, DRV_NAME);
945 goto err_bar2_failed;
948 err = pci_request_region(pdev, 4, DRV_NAME);
950 goto err_bar4_failed;
956 pci_release_region(pdev, 2);
959 release_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
965 static void mthca_release_regions(struct pci_dev *pdev,
969 pci_release_region(pdev, 4);
971 pci_release_region(pdev, 2);
973 release_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
977 static int mthca_enable_msi_x(struct mthca_dev *mdev)
979 struct msix_entry entries[3];
982 entries[0].entry = 0;
983 entries[1].entry = 1;
984 entries[2].entry = 2;
986 err = pci_enable_msix(mdev->pdev, entries, ARRAY_SIZE(entries));
989 mthca_info(mdev, "Only %d MSI-X vectors available, "
990 "not using MSI-X\n", err);
994 mdev->eq_table.eq[MTHCA_EQ_COMP ].msi_x_vector = entries[0].vector;
995 mdev->eq_table.eq[MTHCA_EQ_ASYNC].msi_x_vector = entries[1].vector;
996 mdev->eq_table.eq[MTHCA_EQ_CMD ].msi_x_vector = entries[2].vector;
1001 /* Types of supported HCA */
1003 TAVOR, /* MT23108 */
1004 ARBEL_COMPAT, /* MT25208 in Tavor compat mode */
1005 ARBEL_NATIVE, /* MT25208 with extended features */
1009 #define MTHCA_FW_VER(major, minor, subminor) \
1010 (((u64) (major) << 32) | ((u64) (minor) << 16) | (u64) (subminor))
1015 } mthca_hca_table[] = {
1016 [TAVOR] = { .latest_fw = MTHCA_FW_VER(3, 4, 0),
1018 [ARBEL_COMPAT] = { .latest_fw = MTHCA_FW_VER(4, 7, 600),
1019 .flags = MTHCA_FLAG_PCIE },
1020 [ARBEL_NATIVE] = { .latest_fw = MTHCA_FW_VER(5, 1, 400),
1021 .flags = MTHCA_FLAG_MEMFREE |
1023 [SINAI] = { .latest_fw = MTHCA_FW_VER(1, 1, 0),
1024 .flags = MTHCA_FLAG_MEMFREE |
1026 MTHCA_FLAG_SINAI_OPT }
1029 static int __mthca_init_one(struct pci_dev *pdev, int hca_type)
1033 struct mthca_dev *mdev;
1035 printk(KERN_INFO PFX "Initializing %s\n",
1038 err = pci_enable_device(pdev);
1040 dev_err(&pdev->dev, "Cannot enable PCI device, "
1046 * Check for BARs. We expect 0: 1MB, 2: 8MB, 4: DDR (may not
1049 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
1050 pci_resource_len(pdev, 0) != 1 << 20) {
1051 dev_err(&pdev->dev, "Missing DCS, aborting.\n");
1053 goto err_disable_pdev;
1055 if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
1056 dev_err(&pdev->dev, "Missing UAR, aborting.\n");
1058 goto err_disable_pdev;
1060 if (!(pci_resource_flags(pdev, 4) & IORESOURCE_MEM))
1063 err = mthca_request_regions(pdev, ddr_hidden);
1065 dev_err(&pdev->dev, "Cannot obtain PCI resources, "
1067 goto err_disable_pdev;
1070 pci_set_master(pdev);
1072 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
1074 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask.\n");
1075 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1077 dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting.\n");
1081 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
1083 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit "
1084 "consistent PCI DMA mask.\n");
1085 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
1087 dev_err(&pdev->dev, "Can't set consistent PCI DMA mask, "
1093 mdev = (struct mthca_dev *) ib_alloc_device(sizeof *mdev);
1095 dev_err(&pdev->dev, "Device struct alloc failed, "
1103 mdev->mthca_flags = mthca_hca_table[hca_type].flags;
1105 mdev->mthca_flags |= MTHCA_FLAG_DDR_HIDDEN;
1108 * Now reset the HCA before we touch the PCI capabilities or
1109 * attempt a firmware command, since a boot ROM may have left
1110 * the HCA in an undefined state.
1112 err = mthca_reset(mdev);
1114 mthca_err(mdev, "Failed to reset HCA, aborting.\n");
1118 if (msi_x && !mthca_enable_msi_x(mdev))
1119 mdev->mthca_flags |= MTHCA_FLAG_MSI_X;
1120 if (msi && !(mdev->mthca_flags & MTHCA_FLAG_MSI_X) &&
1121 !pci_enable_msi(pdev))
1122 mdev->mthca_flags |= MTHCA_FLAG_MSI;
1124 if (mthca_cmd_init(mdev)) {
1125 mthca_err(mdev, "Failed to init command interface, aborting.\n");
1129 err = mthca_tune_pci(mdev);
1133 err = mthca_init_hca(mdev);
1137 if (mdev->fw_ver < mthca_hca_table[hca_type].latest_fw) {
1138 mthca_warn(mdev, "HCA FW version %d.%d.%d is old (%d.%d.%d is current).\n",
1139 (int) (mdev->fw_ver >> 32), (int) (mdev->fw_ver >> 16) & 0xffff,
1140 (int) (mdev->fw_ver & 0xffff),
1141 (int) (mthca_hca_table[hca_type].latest_fw >> 32),
1142 (int) (mthca_hca_table[hca_type].latest_fw >> 16) & 0xffff,
1143 (int) (mthca_hca_table[hca_type].latest_fw & 0xffff));
1144 mthca_warn(mdev, "If you have problems, try updating your HCA FW.\n");
1147 err = mthca_setup_hca(mdev);
1151 err = mthca_register_device(mdev);
1155 err = mthca_create_agents(mdev);
1157 goto err_unregister;
1159 pci_set_drvdata(pdev, mdev);
1160 mdev->hca_type = hca_type;
1165 mthca_unregister_device(mdev);
1168 mthca_cleanup_mcg_table(mdev);
1169 mthca_cleanup_av_table(mdev);
1170 mthca_cleanup_qp_table(mdev);
1171 mthca_cleanup_srq_table(mdev);
1172 mthca_cleanup_cq_table(mdev);
1173 mthca_cmd_use_polling(mdev);
1174 mthca_cleanup_eq_table(mdev);
1176 mthca_pd_free(mdev, &mdev->driver_pd);
1178 mthca_cleanup_mr_table(mdev);
1179 mthca_cleanup_pd_table(mdev);
1180 mthca_cleanup_uar_table(mdev);
1183 mthca_close_hca(mdev);
1186 mthca_cmd_cleanup(mdev);
1189 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1190 pci_disable_msix(pdev);
1191 if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1192 pci_disable_msi(pdev);
1194 ib_dealloc_device(&mdev->ib_dev);
1197 mthca_release_regions(pdev, ddr_hidden);
1200 pci_disable_device(pdev);
1201 pci_set_drvdata(pdev, NULL);
1205 static void __mthca_remove_one(struct pci_dev *pdev)
1207 struct mthca_dev *mdev = pci_get_drvdata(pdev);
1212 mthca_free_agents(mdev);
1213 mthca_unregister_device(mdev);
1215 for (p = 1; p <= mdev->limits.num_ports; ++p)
1216 mthca_CLOSE_IB(mdev, p, &status);
1218 mthca_cleanup_mcg_table(mdev);
1219 mthca_cleanup_av_table(mdev);
1220 mthca_cleanup_qp_table(mdev);
1221 mthca_cleanup_srq_table(mdev);
1222 mthca_cleanup_cq_table(mdev);
1223 mthca_cmd_use_polling(mdev);
1224 mthca_cleanup_eq_table(mdev);
1226 mthca_pd_free(mdev, &mdev->driver_pd);
1228 mthca_cleanup_mr_table(mdev);
1229 mthca_cleanup_pd_table(mdev);
1232 mthca_uar_free(mdev, &mdev->driver_uar);
1233 mthca_cleanup_uar_table(mdev);
1234 mthca_close_hca(mdev);
1235 mthca_cmd_cleanup(mdev);
1237 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1238 pci_disable_msix(pdev);
1239 if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1240 pci_disable_msi(pdev);
1242 ib_dealloc_device(&mdev->ib_dev);
1243 mthca_release_regions(pdev, mdev->mthca_flags &
1244 MTHCA_FLAG_DDR_HIDDEN);
1245 pci_disable_device(pdev);
1246 pci_set_drvdata(pdev, NULL);
1250 int __mthca_restart_one(struct pci_dev *pdev)
1252 struct mthca_dev *mdev;
1254 mdev = pci_get_drvdata(pdev);
1257 __mthca_remove_one(pdev);
1258 return __mthca_init_one(pdev, mdev->hca_type);
1261 static int __devinit mthca_init_one(struct pci_dev *pdev,
1262 const struct pci_device_id *id)
1264 static int mthca_version_printed = 0;
1267 mutex_lock(&mthca_device_mutex);
1269 if (!mthca_version_printed) {
1270 printk(KERN_INFO "%s", mthca_version);
1271 ++mthca_version_printed;
1274 if (id->driver_data >= ARRAY_SIZE(mthca_hca_table)) {
1275 printk(KERN_ERR PFX "%s has invalid driver data %lx\n",
1276 pci_name(pdev), id->driver_data);
1277 mutex_unlock(&mthca_device_mutex);
1281 ret = __mthca_init_one(pdev, id->driver_data);
1283 mutex_unlock(&mthca_device_mutex);
1288 static void __devexit mthca_remove_one(struct pci_dev *pdev)
1290 mutex_lock(&mthca_device_mutex);
1291 __mthca_remove_one(pdev);
1292 mutex_unlock(&mthca_device_mutex);
1295 static struct pci_device_id mthca_pci_table[] = {
1296 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_TAVOR),
1297 .driver_data = TAVOR },
1298 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_TAVOR),
1299 .driver_data = TAVOR },
1300 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
1301 .driver_data = ARBEL_COMPAT },
1302 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
1303 .driver_data = ARBEL_COMPAT },
1304 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL),
1305 .driver_data = ARBEL_NATIVE },
1306 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL),
1307 .driver_data = ARBEL_NATIVE },
1308 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI),
1309 .driver_data = SINAI },
1310 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI),
1311 .driver_data = SINAI },
1312 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
1313 .driver_data = SINAI },
1314 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
1315 .driver_data = SINAI },
1319 MODULE_DEVICE_TABLE(pci, mthca_pci_table);
1321 static struct pci_driver mthca_driver = {
1323 .id_table = mthca_pci_table,
1324 .probe = mthca_init_one,
1325 .remove = __devexit_p(mthca_remove_one)
1328 static void __init __mthca_check_profile_val(const char *name, int *pval,
1331 /* value must be positive and power of 2 */
1332 int old_pval = *pval;
1335 *pval = pval_default;
1337 *pval = roundup_pow_of_two(old_pval);
1339 if (old_pval != *pval) {
1340 printk(KERN_WARNING PFX "Invalid value %d for %s in module parameter.\n",
1342 printk(KERN_WARNING PFX "Corrected %s to %d.\n", name, *pval);
1346 #define mthca_check_profile_val(name, default) \
1347 __mthca_check_profile_val(#name, &hca_profile.name, default)
1349 static void __init mthca_validate_profile(void)
1351 mthca_check_profile_val(num_qp, MTHCA_DEFAULT_NUM_QP);
1352 mthca_check_profile_val(rdb_per_qp, MTHCA_DEFAULT_RDB_PER_QP);
1353 mthca_check_profile_val(num_cq, MTHCA_DEFAULT_NUM_CQ);
1354 mthca_check_profile_val(num_mcg, MTHCA_DEFAULT_NUM_MCG);
1355 mthca_check_profile_val(num_mpt, MTHCA_DEFAULT_NUM_MPT);
1356 mthca_check_profile_val(num_mtt, MTHCA_DEFAULT_NUM_MTT);
1357 mthca_check_profile_val(num_udav, MTHCA_DEFAULT_NUM_UDAV);
1358 mthca_check_profile_val(fmr_reserved_mtts, MTHCA_DEFAULT_NUM_RESERVED_MTTS);
1360 if (hca_profile.fmr_reserved_mtts >= hca_profile.num_mtt) {
1361 printk(KERN_WARNING PFX "Invalid fmr_reserved_mtts module parameter %d.\n",
1362 hca_profile.fmr_reserved_mtts);
1363 printk(KERN_WARNING PFX "(Must be smaller than num_mtt %d)\n",
1364 hca_profile.num_mtt);
1365 hca_profile.fmr_reserved_mtts = hca_profile.num_mtt / 2;
1366 printk(KERN_WARNING PFX "Corrected fmr_reserved_mtts to %d.\n",
1367 hca_profile.fmr_reserved_mtts);
1371 static int __init mthca_init(void)
1375 mthca_validate_profile();
1377 ret = mthca_catas_init();
1381 ret = pci_register_driver(&mthca_driver);
1383 mthca_catas_cleanup();
1390 static void __exit mthca_cleanup(void)
1392 pci_unregister_driver(&mthca_driver);
1393 mthca_catas_cleanup();
1396 module_init(mthca_init);
1397 module_exit(mthca_cleanup);