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%lx, aborting.\n",
176 dev_lim
->uar_size
, pci_resource_len(mdev
->pdev
, 2));
180 mdev
->limits
.num_ports
= dev_lim
->num_ports
;
181 mdev
->limits
.vl_cap
= dev_lim
->max_vl
;
182 mdev
->limits
.mtu_cap
= dev_lim
->max_mtu
;
183 mdev
->limits
.gid_table_len
= dev_lim
->max_gids
;
184 mdev
->limits
.pkey_table_len
= dev_lim
->max_pkeys
;
185 mdev
->limits
.local_ca_ack_delay
= dev_lim
->local_ca_ack_delay
;
186 mdev
->limits
.max_sg
= dev_lim
->max_sg
;
187 mdev
->limits
.max_wqes
= dev_lim
->max_qp_sz
;
188 mdev
->limits
.max_qp_init_rdma
= dev_lim
->max_requester_per_qp
;
189 mdev
->limits
.reserved_qps
= dev_lim
->reserved_qps
;
190 mdev
->limits
.max_srq_wqes
= dev_lim
->max_srq_sz
;
191 mdev
->limits
.reserved_srqs
= dev_lim
->reserved_srqs
;
192 mdev
->limits
.reserved_eecs
= dev_lim
->reserved_eecs
;
193 mdev
->limits
.max_desc_sz
= dev_lim
->max_desc_sz
;
195 * Subtract 1 from the limit because we need to allocate a
196 * spare CQE so the HCA HW can tell the difference between an
197 * empty CQ and a full CQ.
199 mdev
->limits
.max_cqes
= dev_lim
->max_cq_sz
- 1;
200 mdev
->limits
.reserved_cqs
= dev_lim
->reserved_cqs
;
201 mdev
->limits
.reserved_eqs
= dev_lim
->reserved_eqs
;
202 mdev
->limits
.reserved_mtts
= dev_lim
->reserved_mtts
;
203 mdev
->limits
.reserved_mrws
= dev_lim
->reserved_mrws
;
204 mdev
->limits
.reserved_uars
= dev_lim
->reserved_uars
;
205 mdev
->limits
.reserved_pds
= dev_lim
->reserved_pds
;
206 mdev
->limits
.port_width_cap
= dev_lim
->max_port_width
;
207 mdev
->limits
.page_size_cap
= ~(u32
) (dev_lim
->min_page_sz
- 1);
208 mdev
->limits
.flags
= dev_lim
->flags
;
210 * For old FW that doesn't return static rate support, use a
211 * value of 0x3 (only static rate values of 0 or 1 are handled),
212 * except on Sinai, where even old FW can handle static rate
215 if (dev_lim
->stat_rate_support
)
216 mdev
->limits
.stat_rate_support
= dev_lim
->stat_rate_support
;
217 else if (mdev
->mthca_flags
& MTHCA_FLAG_SINAI_OPT
)
218 mdev
->limits
.stat_rate_support
= 0xf;
220 mdev
->limits
.stat_rate_support
= 0x3;
222 /* IB_DEVICE_RESIZE_MAX_WR not supported by driver.
223 May be doable since hardware supports it for SRQ.
225 IB_DEVICE_N_NOTIFY_CQ is supported by hardware but not by driver.
227 IB_DEVICE_SRQ_RESIZE is supported by hardware but SRQ is not
228 supported by driver. */
229 mdev
->device_cap_flags
= IB_DEVICE_CHANGE_PHY_PORT
|
230 IB_DEVICE_PORT_ACTIVE_EVENT
|
231 IB_DEVICE_SYS_IMAGE_GUID
|
232 IB_DEVICE_RC_RNR_NAK_GEN
;
234 if (dev_lim
->flags
& DEV_LIM_FLAG_BAD_PKEY_CNTR
)
235 mdev
->device_cap_flags
|= IB_DEVICE_BAD_PKEY_CNTR
;
237 if (dev_lim
->flags
& DEV_LIM_FLAG_BAD_QKEY_CNTR
)
238 mdev
->device_cap_flags
|= IB_DEVICE_BAD_QKEY_CNTR
;
240 if (dev_lim
->flags
& DEV_LIM_FLAG_RAW_MULTI
)
241 mdev
->device_cap_flags
|= IB_DEVICE_RAW_MULTI
;
243 if (dev_lim
->flags
& DEV_LIM_FLAG_AUTO_PATH_MIG
)
244 mdev
->device_cap_flags
|= IB_DEVICE_AUTO_PATH_MIG
;
246 if (dev_lim
->flags
& DEV_LIM_FLAG_UD_AV_PORT_ENFORCE
)
247 mdev
->device_cap_flags
|= IB_DEVICE_UD_AV_PORT_ENFORCE
;
249 if (dev_lim
->flags
& DEV_LIM_FLAG_SRQ
)
250 mdev
->mthca_flags
|= MTHCA_FLAG_SRQ
;
255 static int __devinit
mthca_init_tavor(struct mthca_dev
*mdev
)
259 struct mthca_dev_lim dev_lim
;
260 struct mthca_profile profile
;
261 struct mthca_init_hca_param init_hca
;
263 err
= mthca_SYS_EN(mdev
, &status
);
265 mthca_err(mdev
, "SYS_EN command failed, aborting.\n");
269 mthca_err(mdev
, "SYS_EN returned status 0x%02x, "
270 "aborting.\n", status
);
274 err
= mthca_QUERY_FW(mdev
, &status
);
276 mthca_err(mdev
, "QUERY_FW command failed, aborting.\n");
280 mthca_err(mdev
, "QUERY_FW returned status 0x%02x, "
281 "aborting.\n", status
);
285 err
= mthca_QUERY_DDR(mdev
, &status
);
287 mthca_err(mdev
, "QUERY_DDR command failed, aborting.\n");
291 mthca_err(mdev
, "QUERY_DDR returned status 0x%02x, "
292 "aborting.\n", status
);
297 err
= mthca_dev_lim(mdev
, &dev_lim
);
299 mthca_err(mdev
, "QUERY_DEV_LIM command failed, aborting.\n");
303 profile
= default_profile
;
304 profile
.num_uar
= dev_lim
.uar_size
/ PAGE_SIZE
;
305 profile
.uarc_size
= 0;
306 if (mdev
->mthca_flags
& MTHCA_FLAG_SRQ
)
307 profile
.num_srq
= dev_lim
.max_srqs
;
309 err
= mthca_make_profile(mdev
, &profile
, &dev_lim
, &init_hca
);
313 err
= mthca_INIT_HCA(mdev
, &init_hca
, &status
);
315 mthca_err(mdev
, "INIT_HCA command failed, aborting.\n");
319 mthca_err(mdev
, "INIT_HCA returned status 0x%02x, "
320 "aborting.\n", status
);
328 mthca_SYS_DIS(mdev
, &status
);
333 static int __devinit
mthca_load_fw(struct mthca_dev
*mdev
)
338 /* FIXME: use HCA-attached memory for FW if present */
340 mdev
->fw
.arbel
.fw_icm
=
341 mthca_alloc_icm(mdev
, mdev
->fw
.arbel
.fw_pages
,
342 GFP_HIGHUSER
| __GFP_NOWARN
);
343 if (!mdev
->fw
.arbel
.fw_icm
) {
344 mthca_err(mdev
, "Couldn't allocate FW area, aborting.\n");
348 err
= mthca_MAP_FA(mdev
, mdev
->fw
.arbel
.fw_icm
, &status
);
350 mthca_err(mdev
, "MAP_FA command failed, aborting.\n");
354 mthca_err(mdev
, "MAP_FA returned status 0x%02x, aborting.\n", status
);
358 err
= mthca_RUN_FW(mdev
, &status
);
360 mthca_err(mdev
, "RUN_FW command failed, aborting.\n");
364 mthca_err(mdev
, "RUN_FW returned status 0x%02x, aborting.\n", status
);
372 mthca_UNMAP_FA(mdev
, &status
);
375 mthca_free_icm(mdev
, mdev
->fw
.arbel
.fw_icm
);
379 static int __devinit
mthca_init_icm(struct mthca_dev
*mdev
,
380 struct mthca_dev_lim
*dev_lim
,
381 struct mthca_init_hca_param
*init_hca
,
388 err
= mthca_SET_ICM_SIZE(mdev
, icm_size
, &aux_pages
, &status
);
390 mthca_err(mdev
, "SET_ICM_SIZE command failed, aborting.\n");
394 mthca_err(mdev
, "SET_ICM_SIZE returned status 0x%02x, "
395 "aborting.\n", status
);
399 mthca_dbg(mdev
, "%lld KB of HCA context requires %lld KB aux memory.\n",
400 (unsigned long long) icm_size
>> 10,
401 (unsigned long long) aux_pages
<< 2);
403 mdev
->fw
.arbel
.aux_icm
= mthca_alloc_icm(mdev
, aux_pages
,
404 GFP_HIGHUSER
| __GFP_NOWARN
);
405 if (!mdev
->fw
.arbel
.aux_icm
) {
406 mthca_err(mdev
, "Couldn't allocate aux memory, aborting.\n");
410 err
= mthca_MAP_ICM_AUX(mdev
, mdev
->fw
.arbel
.aux_icm
, &status
);
412 mthca_err(mdev
, "MAP_ICM_AUX command failed, aborting.\n");
416 mthca_err(mdev
, "MAP_ICM_AUX returned status 0x%02x, aborting.\n", status
);
421 err
= mthca_map_eq_icm(mdev
, init_hca
->eqc_base
);
423 mthca_err(mdev
, "Failed to map EQ context memory, aborting.\n");
427 mdev
->mr_table
.mtt_table
= mthca_alloc_icm_table(mdev
, init_hca
->mtt_base
,
429 mdev
->limits
.num_mtt_segs
,
430 mdev
->limits
.reserved_mtts
, 1);
431 if (!mdev
->mr_table
.mtt_table
) {
432 mthca_err(mdev
, "Failed to map MTT context memory, aborting.\n");
437 mdev
->mr_table
.mpt_table
= mthca_alloc_icm_table(mdev
, init_hca
->mpt_base
,
438 dev_lim
->mpt_entry_sz
,
439 mdev
->limits
.num_mpts
,
440 mdev
->limits
.reserved_mrws
, 1);
441 if (!mdev
->mr_table
.mpt_table
) {
442 mthca_err(mdev
, "Failed to map MPT context memory, aborting.\n");
447 mdev
->qp_table
.qp_table
= mthca_alloc_icm_table(mdev
, init_hca
->qpc_base
,
448 dev_lim
->qpc_entry_sz
,
449 mdev
->limits
.num_qps
,
450 mdev
->limits
.reserved_qps
, 0);
451 if (!mdev
->qp_table
.qp_table
) {
452 mthca_err(mdev
, "Failed to map QP context memory, aborting.\n");
457 mdev
->qp_table
.eqp_table
= mthca_alloc_icm_table(mdev
, init_hca
->eqpc_base
,
458 dev_lim
->eqpc_entry_sz
,
459 mdev
->limits
.num_qps
,
460 mdev
->limits
.reserved_qps
, 0);
461 if (!mdev
->qp_table
.eqp_table
) {
462 mthca_err(mdev
, "Failed to map EQP context memory, aborting.\n");
467 mdev
->qp_table
.rdb_table
= mthca_alloc_icm_table(mdev
, init_hca
->rdb_base
,
468 MTHCA_RDB_ENTRY_SIZE
,
469 mdev
->limits
.num_qps
<<
470 mdev
->qp_table
.rdb_shift
,
472 if (!mdev
->qp_table
.rdb_table
) {
473 mthca_err(mdev
, "Failed to map RDB context memory, aborting\n");
478 mdev
->cq_table
.table
= mthca_alloc_icm_table(mdev
, init_hca
->cqc_base
,
479 dev_lim
->cqc_entry_sz
,
480 mdev
->limits
.num_cqs
,
481 mdev
->limits
.reserved_cqs
, 0);
482 if (!mdev
->cq_table
.table
) {
483 mthca_err(mdev
, "Failed to map CQ context memory, aborting.\n");
488 if (mdev
->mthca_flags
& MTHCA_FLAG_SRQ
) {
489 mdev
->srq_table
.table
=
490 mthca_alloc_icm_table(mdev
, init_hca
->srqc_base
,
491 dev_lim
->srq_entry_sz
,
492 mdev
->limits
.num_srqs
,
493 mdev
->limits
.reserved_srqs
, 0);
494 if (!mdev
->srq_table
.table
) {
495 mthca_err(mdev
, "Failed to map SRQ context memory, "
503 * It's not strictly required, but for simplicity just map the
504 * whole multicast group table now. The table isn't very big
505 * and it's a lot easier than trying to track ref counts.
507 mdev
->mcg_table
.table
= mthca_alloc_icm_table(mdev
, init_hca
->mc_base
,
508 MTHCA_MGM_ENTRY_SIZE
,
509 mdev
->limits
.num_mgms
+
510 mdev
->limits
.num_amgms
,
511 mdev
->limits
.num_mgms
+
512 mdev
->limits
.num_amgms
,
514 if (!mdev
->mcg_table
.table
) {
515 mthca_err(mdev
, "Failed to map MCG context memory, aborting.\n");
523 if (mdev
->mthca_flags
& MTHCA_FLAG_SRQ
)
524 mthca_free_icm_table(mdev
, mdev
->srq_table
.table
);
527 mthca_free_icm_table(mdev
, mdev
->cq_table
.table
);
530 mthca_free_icm_table(mdev
, mdev
->qp_table
.rdb_table
);
533 mthca_free_icm_table(mdev
, mdev
->qp_table
.eqp_table
);
536 mthca_free_icm_table(mdev
, mdev
->qp_table
.qp_table
);
539 mthca_free_icm_table(mdev
, mdev
->mr_table
.mpt_table
);
542 mthca_free_icm_table(mdev
, mdev
->mr_table
.mtt_table
);
545 mthca_unmap_eq_icm(mdev
);
548 mthca_UNMAP_ICM_AUX(mdev
, &status
);
551 mthca_free_icm(mdev
, mdev
->fw
.arbel
.aux_icm
);
556 static void mthca_free_icms(struct mthca_dev
*mdev
)
560 mthca_free_icm_table(mdev
, mdev
->mcg_table
.table
);
561 if (mdev
->mthca_flags
& MTHCA_FLAG_SRQ
)
562 mthca_free_icm_table(mdev
, mdev
->srq_table
.table
);
563 mthca_free_icm_table(mdev
, mdev
->cq_table
.table
);
564 mthca_free_icm_table(mdev
, mdev
->qp_table
.rdb_table
);
565 mthca_free_icm_table(mdev
, mdev
->qp_table
.eqp_table
);
566 mthca_free_icm_table(mdev
, mdev
->qp_table
.qp_table
);
567 mthca_free_icm_table(mdev
, mdev
->mr_table
.mpt_table
);
568 mthca_free_icm_table(mdev
, mdev
->mr_table
.mtt_table
);
569 mthca_unmap_eq_icm(mdev
);
571 mthca_UNMAP_ICM_AUX(mdev
, &status
);
572 mthca_free_icm(mdev
, mdev
->fw
.arbel
.aux_icm
);
575 static int __devinit
mthca_init_arbel(struct mthca_dev
*mdev
)
577 struct mthca_dev_lim dev_lim
;
578 struct mthca_profile profile
;
579 struct mthca_init_hca_param init_hca
;
584 err
= mthca_QUERY_FW(mdev
, &status
);
586 mthca_err(mdev
, "QUERY_FW command failed, aborting.\n");
590 mthca_err(mdev
, "QUERY_FW returned status 0x%02x, "
591 "aborting.\n", status
);
595 err
= mthca_ENABLE_LAM(mdev
, &status
);
597 mthca_err(mdev
, "ENABLE_LAM command failed, aborting.\n");
600 if (status
== MTHCA_CMD_STAT_LAM_NOT_PRE
) {
601 mthca_dbg(mdev
, "No HCA-attached memory (running in MemFree mode)\n");
602 mdev
->mthca_flags
|= MTHCA_FLAG_NO_LAM
;
604 mthca_err(mdev
, "ENABLE_LAM returned status 0x%02x, "
605 "aborting.\n", status
);
609 err
= mthca_load_fw(mdev
);
611 mthca_err(mdev
, "Failed to start FW, aborting.\n");
615 err
= mthca_dev_lim(mdev
, &dev_lim
);
617 mthca_err(mdev
, "QUERY_DEV_LIM command failed, aborting.\n");
621 profile
= default_profile
;
622 profile
.num_uar
= dev_lim
.uar_size
/ PAGE_SIZE
;
623 profile
.num_udav
= 0;
624 if (mdev
->mthca_flags
& MTHCA_FLAG_SRQ
)
625 profile
.num_srq
= dev_lim
.max_srqs
;
627 icm_size
= mthca_make_profile(mdev
, &profile
, &dev_lim
, &init_hca
);
628 if ((int) icm_size
< 0) {
633 err
= mthca_init_icm(mdev
, &dev_lim
, &init_hca
, icm_size
);
637 err
= mthca_INIT_HCA(mdev
, &init_hca
, &status
);
639 mthca_err(mdev
, "INIT_HCA command failed, aborting.\n");
643 mthca_err(mdev
, "INIT_HCA returned status 0x%02x, "
644 "aborting.\n", status
);
652 mthca_free_icms(mdev
);
655 mthca_UNMAP_FA(mdev
, &status
);
656 mthca_free_icm(mdev
, mdev
->fw
.arbel
.fw_icm
);
659 if (!(mdev
->mthca_flags
& MTHCA_FLAG_NO_LAM
))
660 mthca_DISABLE_LAM(mdev
, &status
);
665 static void mthca_close_hca(struct mthca_dev
*mdev
)
669 mthca_CLOSE_HCA(mdev
, 0, &status
);
671 if (mthca_is_memfree(mdev
)) {
672 mthca_free_icms(mdev
);
674 mthca_UNMAP_FA(mdev
, &status
);
675 mthca_free_icm(mdev
, mdev
->fw
.arbel
.fw_icm
);
677 if (!(mdev
->mthca_flags
& MTHCA_FLAG_NO_LAM
))
678 mthca_DISABLE_LAM(mdev
, &status
);
680 mthca_SYS_DIS(mdev
, &status
);
683 static int __devinit
mthca_init_hca(struct mthca_dev
*mdev
)
687 struct mthca_adapter adapter
;
689 if (mthca_is_memfree(mdev
))
690 err
= mthca_init_arbel(mdev
);
692 err
= mthca_init_tavor(mdev
);
697 err
= mthca_QUERY_ADAPTER(mdev
, &adapter
, &status
);
699 mthca_err(mdev
, "QUERY_ADAPTER command failed, aborting.\n");
703 mthca_err(mdev
, "QUERY_ADAPTER returned status 0x%02x, "
704 "aborting.\n", status
);
709 mdev
->eq_table
.inta_pin
= adapter
.inta_pin
;
710 mdev
->rev_id
= adapter
.revision_id
;
711 memcpy(mdev
->board_id
, adapter
.board_id
, sizeof mdev
->board_id
);
716 mthca_close_hca(mdev
);
720 static int __devinit
mthca_setup_hca(struct mthca_dev
*dev
)
725 MTHCA_INIT_DOORBELL_LOCK(&dev
->doorbell_lock
);
727 err
= mthca_init_uar_table(dev
);
729 mthca_err(dev
, "Failed to initialize "
730 "user access region table, aborting.\n");
734 err
= mthca_uar_alloc(dev
, &dev
->driver_uar
);
736 mthca_err(dev
, "Failed to allocate driver access region, "
738 goto err_uar_table_free
;
741 dev
->kar
= ioremap(dev
->driver_uar
.pfn
<< PAGE_SHIFT
, PAGE_SIZE
);
743 mthca_err(dev
, "Couldn't map kernel access region, "
749 err
= mthca_init_pd_table(dev
);
751 mthca_err(dev
, "Failed to initialize "
752 "protection domain table, aborting.\n");
756 err
= mthca_init_mr_table(dev
);
758 mthca_err(dev
, "Failed to initialize "
759 "memory region table, aborting.\n");
760 goto err_pd_table_free
;
763 err
= mthca_pd_alloc(dev
, 1, &dev
->driver_pd
);
765 mthca_err(dev
, "Failed to create driver PD, "
767 goto err_mr_table_free
;
770 err
= mthca_init_eq_table(dev
);
772 mthca_err(dev
, "Failed to initialize "
773 "event queue table, aborting.\n");
777 err
= mthca_cmd_use_events(dev
);
779 mthca_err(dev
, "Failed to switch to event-driven "
780 "firmware commands, aborting.\n");
781 goto err_eq_table_free
;
784 err
= mthca_NOP(dev
, &status
);
786 mthca_err(dev
, "NOP command failed to generate interrupt (IRQ %d), aborting.\n",
787 dev
->mthca_flags
& MTHCA_FLAG_MSI_X
?
788 dev
->eq_table
.eq
[MTHCA_EQ_CMD
].msi_x_vector
:
790 if (dev
->mthca_flags
& (MTHCA_FLAG_MSI
| MTHCA_FLAG_MSI_X
))
791 mthca_err(dev
, "Try again with MSI/MSI-X disabled.\n");
793 mthca_err(dev
, "BIOS or ACPI interrupt routing problem?\n");
798 mthca_dbg(dev
, "NOP command IRQ test passed\n");
800 err
= mthca_init_cq_table(dev
);
802 mthca_err(dev
, "Failed to initialize "
803 "completion queue table, aborting.\n");
807 err
= mthca_init_srq_table(dev
);
809 mthca_err(dev
, "Failed to initialize "
810 "shared receive queue table, aborting.\n");
811 goto err_cq_table_free
;
814 err
= mthca_init_qp_table(dev
);
816 mthca_err(dev
, "Failed to initialize "
817 "queue pair table, aborting.\n");
818 goto err_srq_table_free
;
821 err
= mthca_init_av_table(dev
);
823 mthca_err(dev
, "Failed to initialize "
824 "address vector table, aborting.\n");
825 goto err_qp_table_free
;
828 err
= mthca_init_mcg_table(dev
);
830 mthca_err(dev
, "Failed to initialize "
831 "multicast group table, aborting.\n");
832 goto err_av_table_free
;
838 mthca_cleanup_av_table(dev
);
841 mthca_cleanup_qp_table(dev
);
844 mthca_cleanup_srq_table(dev
);
847 mthca_cleanup_cq_table(dev
);
850 mthca_cmd_use_polling(dev
);
853 mthca_cleanup_eq_table(dev
);
856 mthca_pd_free(dev
, &dev
->driver_pd
);
859 mthca_cleanup_mr_table(dev
);
862 mthca_cleanup_pd_table(dev
);
868 mthca_uar_free(dev
, &dev
->driver_uar
);
871 mthca_cleanup_uar_table(dev
);
875 static int __devinit
mthca_request_regions(struct pci_dev
*pdev
,
881 * We can't just use pci_request_regions() because the MSI-X
882 * table is right in the middle of the first BAR. If we did
883 * pci_request_region and grab all of the first BAR, then
884 * setting up MSI-X would fail, since the PCI core wants to do
885 * request_mem_region on the MSI-X vector table.
887 * So just request what we need right now, and request any
888 * other regions we need when setting up EQs.
890 if (!request_mem_region(pci_resource_start(pdev
, 0) + MTHCA_HCR_BASE
,
891 MTHCA_HCR_SIZE
, DRV_NAME
))
894 err
= pci_request_region(pdev
, 2, DRV_NAME
);
896 goto err_bar2_failed
;
899 err
= pci_request_region(pdev
, 4, DRV_NAME
);
901 goto err_bar4_failed
;
907 pci_release_region(pdev
, 2);
910 release_mem_region(pci_resource_start(pdev
, 0) + MTHCA_HCR_BASE
,
916 static void mthca_release_regions(struct pci_dev
*pdev
,
920 pci_release_region(pdev
, 4);
922 pci_release_region(pdev
, 2);
924 release_mem_region(pci_resource_start(pdev
, 0) + MTHCA_HCR_BASE
,
928 static int __devinit
mthca_enable_msi_x(struct mthca_dev
*mdev
)
930 struct msix_entry entries
[3];
933 entries
[0].entry
= 0;
934 entries
[1].entry
= 1;
935 entries
[2].entry
= 2;
937 err
= pci_enable_msix(mdev
->pdev
, entries
, ARRAY_SIZE(entries
));
940 mthca_info(mdev
, "Only %d MSI-X vectors available, "
941 "not using MSI-X\n", err
);
945 mdev
->eq_table
.eq
[MTHCA_EQ_COMP
].msi_x_vector
= entries
[0].vector
;
946 mdev
->eq_table
.eq
[MTHCA_EQ_ASYNC
].msi_x_vector
= entries
[1].vector
;
947 mdev
->eq_table
.eq
[MTHCA_EQ_CMD
].msi_x_vector
= entries
[2].vector
;
952 /* Types of supported HCA */
955 ARBEL_COMPAT
, /* MT25208 in Tavor compat mode */
956 ARBEL_NATIVE
, /* MT25208 with extended features */
960 #define MTHCA_FW_VER(major, minor, subminor) \
961 (((u64) (major) << 32) | ((u64) (minor) << 16) | (u64) (subminor))
966 } mthca_hca_table
[] = {
967 [TAVOR
] = { .latest_fw
= MTHCA_FW_VER(3, 4, 0),
969 [ARBEL_COMPAT
] = { .latest_fw
= MTHCA_FW_VER(4, 7, 400),
970 .flags
= MTHCA_FLAG_PCIE
},
971 [ARBEL_NATIVE
] = { .latest_fw
= MTHCA_FW_VER(5, 1, 0),
972 .flags
= MTHCA_FLAG_MEMFREE
|
974 [SINAI
] = { .latest_fw
= MTHCA_FW_VER(1, 0, 800),
975 .flags
= MTHCA_FLAG_MEMFREE
|
977 MTHCA_FLAG_SINAI_OPT
}
980 static int __devinit
mthca_init_one(struct pci_dev
*pdev
,
981 const struct pci_device_id
*id
)
983 static int mthca_version_printed
= 0;
986 struct mthca_dev
*mdev
;
988 if (!mthca_version_printed
) {
989 printk(KERN_INFO
"%s", mthca_version
);
990 ++mthca_version_printed
;
993 printk(KERN_INFO PFX
"Initializing %s\n",
996 if (id
->driver_data
>= ARRAY_SIZE(mthca_hca_table
)) {
997 printk(KERN_ERR PFX
"%s has invalid driver data %lx\n",
998 pci_name(pdev
), id
->driver_data
);
1002 err
= pci_enable_device(pdev
);
1004 dev_err(&pdev
->dev
, "Cannot enable PCI device, "
1010 * Check for BARs. We expect 0: 1MB, 2: 8MB, 4: DDR (may not
1013 if (!(pci_resource_flags(pdev
, 0) & IORESOURCE_MEM
) ||
1014 pci_resource_len(pdev
, 0) != 1 << 20) {
1015 dev_err(&pdev
->dev
, "Missing DCS, aborting.\n");
1017 goto err_disable_pdev
;
1019 if (!(pci_resource_flags(pdev
, 2) & IORESOURCE_MEM
)) {
1020 dev_err(&pdev
->dev
, "Missing UAR, aborting.\n");
1022 goto err_disable_pdev
;
1024 if (!(pci_resource_flags(pdev
, 4) & IORESOURCE_MEM
))
1027 err
= mthca_request_regions(pdev
, ddr_hidden
);
1029 dev_err(&pdev
->dev
, "Cannot obtain PCI resources, "
1031 goto err_disable_pdev
;
1034 pci_set_master(pdev
);
1036 err
= pci_set_dma_mask(pdev
, DMA_64BIT_MASK
);
1038 dev_warn(&pdev
->dev
, "Warning: couldn't set 64-bit PCI DMA mask.\n");
1039 err
= pci_set_dma_mask(pdev
, DMA_32BIT_MASK
);
1041 dev_err(&pdev
->dev
, "Can't set PCI DMA mask, aborting.\n");
1045 err
= pci_set_consistent_dma_mask(pdev
, DMA_64BIT_MASK
);
1047 dev_warn(&pdev
->dev
, "Warning: couldn't set 64-bit "
1048 "consistent PCI DMA mask.\n");
1049 err
= pci_set_consistent_dma_mask(pdev
, DMA_32BIT_MASK
);
1051 dev_err(&pdev
->dev
, "Can't set consistent PCI DMA mask, "
1057 mdev
= (struct mthca_dev
*) ib_alloc_device(sizeof *mdev
);
1059 dev_err(&pdev
->dev
, "Device struct alloc failed, "
1067 mdev
->mthca_flags
= mthca_hca_table
[id
->driver_data
].flags
;
1069 mdev
->mthca_flags
|= MTHCA_FLAG_DDR_HIDDEN
;
1072 * Now reset the HCA before we touch the PCI capabilities or
1073 * attempt a firmware command, since a boot ROM may have left
1074 * the HCA in an undefined state.
1076 err
= mthca_reset(mdev
);
1078 mthca_err(mdev
, "Failed to reset HCA, aborting.\n");
1082 if (msi_x
&& !mthca_enable_msi_x(mdev
))
1083 mdev
->mthca_flags
|= MTHCA_FLAG_MSI_X
;
1084 if (msi
&& !(mdev
->mthca_flags
& MTHCA_FLAG_MSI_X
) &&
1085 !pci_enable_msi(pdev
))
1086 mdev
->mthca_flags
|= MTHCA_FLAG_MSI
;
1088 if (mthca_cmd_init(mdev
)) {
1089 mthca_err(mdev
, "Failed to init command interface, aborting.\n");
1093 err
= mthca_tune_pci(mdev
);
1097 err
= mthca_init_hca(mdev
);
1101 if (mdev
->fw_ver
< mthca_hca_table
[id
->driver_data
].latest_fw
) {
1102 mthca_warn(mdev
, "HCA FW version %d.%d.%d is old (%d.%d.%d is current).\n",
1103 (int) (mdev
->fw_ver
>> 32), (int) (mdev
->fw_ver
>> 16) & 0xffff,
1104 (int) (mdev
->fw_ver
& 0xffff),
1105 (int) (mthca_hca_table
[id
->driver_data
].latest_fw
>> 32),
1106 (int) (mthca_hca_table
[id
->driver_data
].latest_fw
>> 16) & 0xffff,
1107 (int) (mthca_hca_table
[id
->driver_data
].latest_fw
& 0xffff));
1108 mthca_warn(mdev
, "If you have problems, try updating your HCA FW.\n");
1111 err
= mthca_setup_hca(mdev
);
1115 err
= mthca_register_device(mdev
);
1119 err
= mthca_create_agents(mdev
);
1121 goto err_unregister
;
1123 pci_set_drvdata(pdev
, mdev
);
1128 mthca_unregister_device(mdev
);
1131 mthca_cleanup_mcg_table(mdev
);
1132 mthca_cleanup_av_table(mdev
);
1133 mthca_cleanup_qp_table(mdev
);
1134 mthca_cleanup_srq_table(mdev
);
1135 mthca_cleanup_cq_table(mdev
);
1136 mthca_cmd_use_polling(mdev
);
1137 mthca_cleanup_eq_table(mdev
);
1139 mthca_pd_free(mdev
, &mdev
->driver_pd
);
1141 mthca_cleanup_mr_table(mdev
);
1142 mthca_cleanup_pd_table(mdev
);
1143 mthca_cleanup_uar_table(mdev
);
1146 mthca_close_hca(mdev
);
1149 mthca_cmd_cleanup(mdev
);
1152 if (mdev
->mthca_flags
& MTHCA_FLAG_MSI_X
)
1153 pci_disable_msix(pdev
);
1154 if (mdev
->mthca_flags
& MTHCA_FLAG_MSI
)
1155 pci_disable_msi(pdev
);
1157 ib_dealloc_device(&mdev
->ib_dev
);
1160 mthca_release_regions(pdev
, ddr_hidden
);
1163 pci_disable_device(pdev
);
1164 pci_set_drvdata(pdev
, NULL
);
1168 static void __devexit
mthca_remove_one(struct pci_dev
*pdev
)
1170 struct mthca_dev
*mdev
= pci_get_drvdata(pdev
);
1175 mthca_free_agents(mdev
);
1176 mthca_unregister_device(mdev
);
1178 for (p
= 1; p
<= mdev
->limits
.num_ports
; ++p
)
1179 mthca_CLOSE_IB(mdev
, p
, &status
);
1181 mthca_cleanup_mcg_table(mdev
);
1182 mthca_cleanup_av_table(mdev
);
1183 mthca_cleanup_qp_table(mdev
);
1184 mthca_cleanup_srq_table(mdev
);
1185 mthca_cleanup_cq_table(mdev
);
1186 mthca_cmd_use_polling(mdev
);
1187 mthca_cleanup_eq_table(mdev
);
1189 mthca_pd_free(mdev
, &mdev
->driver_pd
);
1191 mthca_cleanup_mr_table(mdev
);
1192 mthca_cleanup_pd_table(mdev
);
1195 mthca_uar_free(mdev
, &mdev
->driver_uar
);
1196 mthca_cleanup_uar_table(mdev
);
1197 mthca_close_hca(mdev
);
1198 mthca_cmd_cleanup(mdev
);
1200 if (mdev
->mthca_flags
& MTHCA_FLAG_MSI_X
)
1201 pci_disable_msix(pdev
);
1202 if (mdev
->mthca_flags
& MTHCA_FLAG_MSI
)
1203 pci_disable_msi(pdev
);
1205 ib_dealloc_device(&mdev
->ib_dev
);
1206 mthca_release_regions(pdev
, mdev
->mthca_flags
&
1207 MTHCA_FLAG_DDR_HIDDEN
);
1208 pci_disable_device(pdev
);
1209 pci_set_drvdata(pdev
, NULL
);
1213 static struct pci_device_id mthca_pci_table
[] = {
1214 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX
, PCI_DEVICE_ID_MELLANOX_TAVOR
),
1215 .driver_data
= TAVOR
},
1216 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN
, PCI_DEVICE_ID_MELLANOX_TAVOR
),
1217 .driver_data
= TAVOR
},
1218 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX
, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT
),
1219 .driver_data
= ARBEL_COMPAT
},
1220 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN
, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT
),
1221 .driver_data
= ARBEL_COMPAT
},
1222 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX
, PCI_DEVICE_ID_MELLANOX_ARBEL
),
1223 .driver_data
= ARBEL_NATIVE
},
1224 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN
, PCI_DEVICE_ID_MELLANOX_ARBEL
),
1225 .driver_data
= ARBEL_NATIVE
},
1226 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX
, PCI_DEVICE_ID_MELLANOX_SINAI
),
1227 .driver_data
= SINAI
},
1228 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN
, PCI_DEVICE_ID_MELLANOX_SINAI
),
1229 .driver_data
= SINAI
},
1230 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX
, PCI_DEVICE_ID_MELLANOX_SINAI_OLD
),
1231 .driver_data
= SINAI
},
1232 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN
, PCI_DEVICE_ID_MELLANOX_SINAI_OLD
),
1233 .driver_data
= SINAI
},
1237 MODULE_DEVICE_TABLE(pci
, mthca_pci_table
);
1239 static struct pci_driver mthca_driver
= {
1241 .id_table
= mthca_pci_table
,
1242 .probe
= mthca_init_one
,
1243 .remove
= __devexit_p(mthca_remove_one
)
1246 static int __init
mthca_init(void)
1250 ret
= pci_register_driver(&mthca_driver
);
1251 return ret
< 0 ? ret
: 0;
1254 static void __exit
mthca_cleanup(void)
1256 pci_unregister_driver(&mthca_driver
);
1259 module_init(mthca_init
);
1260 module_exit(mthca_cleanup
);