blob: 7b82c1907f04288c88122d475a08ff9e30ef893a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
Tom Duffycd4e8fb2005-06-27 14:36:37 -07003 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07004 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
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.
24 *
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
32 * SOFTWARE.
33 *
34 * $Id: mthca_main.c 1396 2004-12-28 04:10:27Z roland $
35 */
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/module.h>
38#include <linux/init.h>
39#include <linux/errno.h>
40#include <linux/pci.h>
41#include <linux/interrupt.h>
42
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"
48
49MODULE_AUTHOR("Roland Dreier");
50MODULE_DESCRIPTION("Mellanox InfiniBand HCA low-level driver");
51MODULE_LICENSE("Dual BSD/GPL");
52MODULE_VERSION(DRV_VERSION);
53
Roland Dreier227c9392006-04-02 14:39:20 -070054#ifdef CONFIG_INFINIBAND_MTHCA_DEBUG
55
56int mthca_debug_level = 0;
57module_param_named(debug_level, mthca_debug_level, int, 0644);
58MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
59
60#endif /* CONFIG_INFINIBAND_MTHCA_DEBUG */
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#ifdef CONFIG_PCI_MSI
63
64static int msi_x = 0;
65module_param(msi_x, int, 0444);
66MODULE_PARM_DESC(msi_x, "attempt to use MSI-X if nonzero");
67
68static int msi = 0;
69module_param(msi, int, 0444);
70MODULE_PARM_DESC(msi, "attempt to use MSI if nonzero");
71
72#else /* CONFIG_PCI_MSI */
73
74#define msi_x (0)
75#define msi (0)
76
77#endif /* CONFIG_PCI_MSI */
78
Michael S. Tsirkinabf45db2006-04-05 15:47:16 +030079static int tune_pci = 0;
80module_param(tune_pci, int, 0444);
81MODULE_PARM_DESC(tune_pci, "increase PCI burst from the default set by BIOS if nonzero");
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static const char mthca_version[] __devinitdata =
Bernhard Fischer177214a2005-06-27 14:36:39 -070084 DRV_NAME ": Mellanox InfiniBand HCA driver v"
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 DRV_VERSION " (" DRV_RELDATE ")\n";
86
87static struct mthca_profile default_profile = {
Michael S. Tsirkine0f5fdc2005-04-16 15:26:30 -070088 .num_qp = 1 << 16,
89 .rdb_per_qp = 4,
90 .num_cq = 1 << 16,
91 .num_mcg = 1 << 13,
92 .num_mpt = 1 << 17,
93 .num_mtt = 1 << 20,
94 .num_udav = 1 << 15, /* Tavor only */
95 .fmr_reserved_mtts = 1 << 18, /* Tavor only */
96 .uarc_size = 1 << 18, /* Arbel only */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097};
98
99static int __devinit mthca_tune_pci(struct mthca_dev *mdev)
100{
101 int cap;
102 u16 val;
103
Michael S. Tsirkinabf45db2006-04-05 15:47:16 +0300104 if (!tune_pci)
105 return 0;
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 /* First try to max out Read Byte Count */
108 cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_PCIX);
109 if (cap) {
110 if (pci_read_config_word(mdev->pdev, cap + PCI_X_CMD, &val)) {
111 mthca_err(mdev, "Couldn't read PCI-X command register, "
112 "aborting.\n");
113 return -ENODEV;
114 }
115 val = (val & ~PCI_X_CMD_MAX_READ) | (3 << 2);
116 if (pci_write_config_word(mdev->pdev, cap + PCI_X_CMD, val)) {
117 mthca_err(mdev, "Couldn't write PCI-X command register, "
118 "aborting.\n");
119 return -ENODEV;
120 }
Roland Dreier68a3c212005-04-16 15:26:34 -0700121 } else if (!(mdev->mthca_flags & MTHCA_FLAG_PCIE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 mthca_info(mdev, "No PCI-X capability, not setting RBC.\n");
123
124 cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_EXP);
125 if (cap) {
126 if (pci_read_config_word(mdev->pdev, cap + PCI_EXP_DEVCTL, &val)) {
127 mthca_err(mdev, "Couldn't read PCI Express device control "
128 "register, aborting.\n");
129 return -ENODEV;
130 }
131 val = (val & ~PCI_EXP_DEVCTL_READRQ) | (5 << 12);
132 if (pci_write_config_word(mdev->pdev, cap + PCI_EXP_DEVCTL, val)) {
133 mthca_err(mdev, "Couldn't write PCI Express device control "
134 "register, aborting.\n");
135 return -ENODEV;
136 }
Roland Dreier68a3c212005-04-16 15:26:34 -0700137 } else if (mdev->mthca_flags & MTHCA_FLAG_PCIE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 mthca_info(mdev, "No PCI Express capability, "
139 "not setting Max Read Request Size.\n");
140
141 return 0;
142}
143
144static int __devinit mthca_dev_lim(struct mthca_dev *mdev, struct mthca_dev_lim *dev_lim)
145{
146 int err;
147 u8 status;
148
149 err = mthca_QUERY_DEV_LIM(mdev, dev_lim, &status);
150 if (err) {
151 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
152 return err;
153 }
154 if (status) {
155 mthca_err(mdev, "QUERY_DEV_LIM returned status 0x%02x, "
156 "aborting.\n", status);
157 return -EINVAL;
158 }
159 if (dev_lim->min_page_sz > PAGE_SIZE) {
160 mthca_err(mdev, "HCA minimum page size of %d bigger than "
161 "kernel PAGE_SIZE of %ld, aborting.\n",
162 dev_lim->min_page_sz, PAGE_SIZE);
163 return -ENODEV;
164 }
165 if (dev_lim->num_ports > MTHCA_MAX_PORTS) {
166 mthca_err(mdev, "HCA has %d ports, but we only support %d, "
167 "aborting.\n",
168 dev_lim->num_ports, MTHCA_MAX_PORTS);
169 return -ENODEV;
170 }
171
Michael S. Tsirkincbd29812006-01-30 15:20:35 -0800172 if (dev_lim->uar_size > pci_resource_len(mdev->pdev, 2)) {
173 mthca_err(mdev, "HCA reported UAR size of 0x%x bigger than "
Greg Kroah-Hartmane29419f2006-06-12 15:20:16 -0700174 "PCI resource 2 size of 0x%llx, aborting.\n",
175 dev_lim->uar_size,
176 (unsigned long long)pci_resource_len(mdev->pdev, 2));
Michael S. Tsirkincbd29812006-01-30 15:20:35 -0800177 return -ENODEV;
178 }
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 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;
Jack Morgensteinefaae8f2005-10-10 13:48:07 -0700187 mdev->limits.max_wqes = dev_lim->max_qp_sz;
188 mdev->limits.max_qp_init_rdma = dev_lim->max_requester_per_qp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 mdev->limits.reserved_qps = dev_lim->reserved_qps;
Jack Morgensteinefaae8f2005-10-10 13:48:07 -0700190 mdev->limits.max_srq_wqes = dev_lim->max_srq_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 mdev->limits.reserved_srqs = dev_lim->reserved_srqs;
192 mdev->limits.reserved_eecs = dev_lim->reserved_eecs;
Jack Morgenstein77369ed2005-11-09 11:26:07 -0800193 mdev->limits.max_desc_sz = dev_lim->max_desc_sz;
Jack Morgenstein59fef3b2006-04-11 18:16:27 +0300194 mdev->limits.max_srq_sge = mthca_max_srq_sge(mdev);
Jack Morgensteinefaae8f2005-10-10 13:48:07 -0700195 /*
196 * Subtract 1 from the limit because we need to allocate a
197 * spare CQE so the HCA HW can tell the difference between an
198 * empty CQ and a full CQ.
199 */
200 mdev->limits.max_cqes = dev_lim->max_cq_sz - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 mdev->limits.reserved_cqs = dev_lim->reserved_cqs;
202 mdev->limits.reserved_eqs = dev_lim->reserved_eqs;
203 mdev->limits.reserved_mtts = dev_lim->reserved_mtts;
204 mdev->limits.reserved_mrws = dev_lim->reserved_mrws;
205 mdev->limits.reserved_uars = dev_lim->reserved_uars;
206 mdev->limits.reserved_pds = dev_lim->reserved_pds;
Roland Dreierda6561c2005-08-17 07:39:10 -0700207 mdev->limits.port_width_cap = dev_lim->max_port_width;
Jack Morgenstein0f69ce12005-11-04 16:03:32 -0800208 mdev->limits.page_size_cap = ~(u32) (dev_lim->min_page_sz - 1);
Jack Morgenstein33033b792005-09-26 12:30:02 -0700209 mdev->limits.flags = dev_lim->flags;
Jack Morgensteinbf6a9e32006-04-10 09:43:47 -0700210 /*
211 * For old FW that doesn't return static rate support, use a
212 * value of 0x3 (only static rate values of 0 or 1 are handled),
213 * except on Sinai, where even old FW can handle static rate
214 * values of 2 and 3.
215 */
216 if (dev_lim->stat_rate_support)
217 mdev->limits.stat_rate_support = dev_lim->stat_rate_support;
218 else if (mdev->mthca_flags & MTHCA_FLAG_SINAI_OPT)
219 mdev->limits.stat_rate_support = 0xf;
220 else
221 mdev->limits.stat_rate_support = 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 /* IB_DEVICE_RESIZE_MAX_WR not supported by driver.
224 May be doable since hardware supports it for SRQ.
225
226 IB_DEVICE_N_NOTIFY_CQ is supported by hardware but not by driver.
227
228 IB_DEVICE_SRQ_RESIZE is supported by hardware but SRQ is not
229 supported by driver. */
230 mdev->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
231 IB_DEVICE_PORT_ACTIVE_EVENT |
232 IB_DEVICE_SYS_IMAGE_GUID |
233 IB_DEVICE_RC_RNR_NAK_GEN;
234
235 if (dev_lim->flags & DEV_LIM_FLAG_BAD_PKEY_CNTR)
236 mdev->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
237
238 if (dev_lim->flags & DEV_LIM_FLAG_BAD_QKEY_CNTR)
239 mdev->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
240
241 if (dev_lim->flags & DEV_LIM_FLAG_RAW_MULTI)
242 mdev->device_cap_flags |= IB_DEVICE_RAW_MULTI;
243
244 if (dev_lim->flags & DEV_LIM_FLAG_AUTO_PATH_MIG)
245 mdev->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
246
247 if (dev_lim->flags & DEV_LIM_FLAG_UD_AV_PORT_ENFORCE)
248 mdev->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
249
250 if (dev_lim->flags & DEV_LIM_FLAG_SRQ)
251 mdev->mthca_flags |= MTHCA_FLAG_SRQ;
252
253 return 0;
254}
255
256static int __devinit mthca_init_tavor(struct mthca_dev *mdev)
257{
258 u8 status;
259 int err;
260 struct mthca_dev_lim dev_lim;
261 struct mthca_profile profile;
262 struct mthca_init_hca_param init_hca;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 err = mthca_SYS_EN(mdev, &status);
265 if (err) {
266 mthca_err(mdev, "SYS_EN command failed, aborting.\n");
267 return err;
268 }
269 if (status) {
270 mthca_err(mdev, "SYS_EN returned status 0x%02x, "
271 "aborting.\n", status);
272 return -EINVAL;
273 }
274
275 err = mthca_QUERY_FW(mdev, &status);
276 if (err) {
277 mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
278 goto err_disable;
279 }
280 if (status) {
281 mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
282 "aborting.\n", status);
283 err = -EINVAL;
284 goto err_disable;
285 }
286 err = mthca_QUERY_DDR(mdev, &status);
287 if (err) {
288 mthca_err(mdev, "QUERY_DDR command failed, aborting.\n");
289 goto err_disable;
290 }
291 if (status) {
292 mthca_err(mdev, "QUERY_DDR returned status 0x%02x, "
293 "aborting.\n", status);
294 err = -EINVAL;
295 goto err_disable;
296 }
297
298 err = mthca_dev_lim(mdev, &dev_lim);
Jack Morgensteinaa2f9362006-01-05 16:12:01 -0800299 if (err) {
300 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
301 goto err_disable;
302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 profile = default_profile;
305 profile.num_uar = dev_lim.uar_size / PAGE_SIZE;
306 profile.uarc_size = 0;
Roland Dreierec34a922005-08-19 10:59:31 -0700307 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
308 profile.num_srq = dev_lim.max_srqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 err = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
311 if (err < 0)
312 goto err_disable;
313
314 err = mthca_INIT_HCA(mdev, &init_hca, &status);
315 if (err) {
316 mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
317 goto err_disable;
318 }
319 if (status) {
320 mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
321 "aborting.\n", status);
322 err = -EINVAL;
323 goto err_disable;
324 }
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return 0;
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328err_disable:
329 mthca_SYS_DIS(mdev, &status);
330
331 return err;
332}
333
334static int __devinit mthca_load_fw(struct mthca_dev *mdev)
335{
336 u8 status;
337 int err;
338
339 /* FIXME: use HCA-attached memory for FW if present */
340
341 mdev->fw.arbel.fw_icm =
342 mthca_alloc_icm(mdev, mdev->fw.arbel.fw_pages,
343 GFP_HIGHUSER | __GFP_NOWARN);
344 if (!mdev->fw.arbel.fw_icm) {
345 mthca_err(mdev, "Couldn't allocate FW area, aborting.\n");
346 return -ENOMEM;
347 }
348
349 err = mthca_MAP_FA(mdev, mdev->fw.arbel.fw_icm, &status);
350 if (err) {
351 mthca_err(mdev, "MAP_FA command failed, aborting.\n");
352 goto err_free;
353 }
354 if (status) {
355 mthca_err(mdev, "MAP_FA returned status 0x%02x, aborting.\n", status);
356 err = -EINVAL;
357 goto err_free;
358 }
359 err = mthca_RUN_FW(mdev, &status);
360 if (err) {
361 mthca_err(mdev, "RUN_FW command failed, aborting.\n");
362 goto err_unmap_fa;
363 }
364 if (status) {
365 mthca_err(mdev, "RUN_FW returned status 0x%02x, aborting.\n", status);
366 err = -EINVAL;
367 goto err_unmap_fa;
368 }
369
370 return 0;
371
372err_unmap_fa:
373 mthca_UNMAP_FA(mdev, &status);
374
375err_free:
376 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
377 return err;
378}
379
380static int __devinit mthca_init_icm(struct mthca_dev *mdev,
381 struct mthca_dev_lim *dev_lim,
382 struct mthca_init_hca_param *init_hca,
383 u64 icm_size)
384{
385 u64 aux_pages;
386 u8 status;
387 int err;
388
389 err = mthca_SET_ICM_SIZE(mdev, icm_size, &aux_pages, &status);
390 if (err) {
391 mthca_err(mdev, "SET_ICM_SIZE command failed, aborting.\n");
392 return err;
393 }
394 if (status) {
395 mthca_err(mdev, "SET_ICM_SIZE returned status 0x%02x, "
396 "aborting.\n", status);
397 return -EINVAL;
398 }
399
400 mthca_dbg(mdev, "%lld KB of HCA context requires %lld KB aux memory.\n",
401 (unsigned long long) icm_size >> 10,
402 (unsigned long long) aux_pages << 2);
403
404 mdev->fw.arbel.aux_icm = mthca_alloc_icm(mdev, aux_pages,
405 GFP_HIGHUSER | __GFP_NOWARN);
406 if (!mdev->fw.arbel.aux_icm) {
407 mthca_err(mdev, "Couldn't allocate aux memory, aborting.\n");
408 return -ENOMEM;
409 }
410
411 err = mthca_MAP_ICM_AUX(mdev, mdev->fw.arbel.aux_icm, &status);
412 if (err) {
413 mthca_err(mdev, "MAP_ICM_AUX command failed, aborting.\n");
414 goto err_free_aux;
415 }
416 if (status) {
417 mthca_err(mdev, "MAP_ICM_AUX returned status 0x%02x, aborting.\n", status);
418 err = -EINVAL;
419 goto err_free_aux;
420 }
421
422 err = mthca_map_eq_icm(mdev, init_hca->eqc_base);
423 if (err) {
424 mthca_err(mdev, "Failed to map EQ context memory, aborting.\n");
425 goto err_unmap_aux;
426 }
427
428 mdev->mr_table.mtt_table = mthca_alloc_icm_table(mdev, init_hca->mtt_base,
Roland Dreier44ea6682005-04-16 15:26:24 -0700429 MTHCA_MTT_SEG_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 mdev->limits.num_mtt_segs,
431 mdev->limits.reserved_mtts, 1);
432 if (!mdev->mr_table.mtt_table) {
433 mthca_err(mdev, "Failed to map MTT context memory, aborting.\n");
434 err = -ENOMEM;
435 goto err_unmap_eq;
436 }
437
438 mdev->mr_table.mpt_table = mthca_alloc_icm_table(mdev, init_hca->mpt_base,
439 dev_lim->mpt_entry_sz,
440 mdev->limits.num_mpts,
441 mdev->limits.reserved_mrws, 1);
442 if (!mdev->mr_table.mpt_table) {
443 mthca_err(mdev, "Failed to map MPT context memory, aborting.\n");
444 err = -ENOMEM;
445 goto err_unmap_mtt;
446 }
447
448 mdev->qp_table.qp_table = mthca_alloc_icm_table(mdev, init_hca->qpc_base,
449 dev_lim->qpc_entry_sz,
450 mdev->limits.num_qps,
451 mdev->limits.reserved_qps, 0);
452 if (!mdev->qp_table.qp_table) {
453 mthca_err(mdev, "Failed to map QP context memory, aborting.\n");
454 err = -ENOMEM;
455 goto err_unmap_mpt;
456 }
457
458 mdev->qp_table.eqp_table = mthca_alloc_icm_table(mdev, init_hca->eqpc_base,
459 dev_lim->eqpc_entry_sz,
460 mdev->limits.num_qps,
461 mdev->limits.reserved_qps, 0);
462 if (!mdev->qp_table.eqp_table) {
463 mthca_err(mdev, "Failed to map EQP context memory, aborting.\n");
464 err = -ENOMEM;
465 goto err_unmap_qp;
466 }
467
Roland Dreier08aeb142005-04-16 15:26:34 -0700468 mdev->qp_table.rdb_table = mthca_alloc_icm_table(mdev, init_hca->rdb_base,
469 MTHCA_RDB_ENTRY_SIZE,
470 mdev->limits.num_qps <<
471 mdev->qp_table.rdb_shift,
472 0, 0);
473 if (!mdev->qp_table.rdb_table) {
474 mthca_err(mdev, "Failed to map RDB context memory, aborting\n");
475 err = -ENOMEM;
Roland Dreier19272d42005-04-16 15:26:35 -0700476 goto err_unmap_eqp;
Roland Dreier08aeb142005-04-16 15:26:34 -0700477 }
478
479 mdev->cq_table.table = mthca_alloc_icm_table(mdev, init_hca->cqc_base,
Roland Dreierec34a922005-08-19 10:59:31 -0700480 dev_lim->cqc_entry_sz,
481 mdev->limits.num_cqs,
482 mdev->limits.reserved_cqs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (!mdev->cq_table.table) {
484 mthca_err(mdev, "Failed to map CQ context memory, aborting.\n");
485 err = -ENOMEM;
Roland Dreier08aeb142005-04-16 15:26:34 -0700486 goto err_unmap_rdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488
Roland Dreierec34a922005-08-19 10:59:31 -0700489 if (mdev->mthca_flags & MTHCA_FLAG_SRQ) {
490 mdev->srq_table.table =
491 mthca_alloc_icm_table(mdev, init_hca->srqc_base,
492 dev_lim->srq_entry_sz,
493 mdev->limits.num_srqs,
494 mdev->limits.reserved_srqs, 0);
495 if (!mdev->srq_table.table) {
496 mthca_err(mdev, "Failed to map SRQ context memory, "
497 "aborting.\n");
498 err = -ENOMEM;
499 goto err_unmap_cq;
500 }
501 }
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 /*
504 * It's not strictly required, but for simplicity just map the
505 * whole multicast group table now. The table isn't very big
506 * and it's a lot easier than trying to track ref counts.
507 */
508 mdev->mcg_table.table = mthca_alloc_icm_table(mdev, init_hca->mc_base,
509 MTHCA_MGM_ENTRY_SIZE,
510 mdev->limits.num_mgms +
511 mdev->limits.num_amgms,
512 mdev->limits.num_mgms +
513 mdev->limits.num_amgms,
514 0);
515 if (!mdev->mcg_table.table) {
516 mthca_err(mdev, "Failed to map MCG context memory, aborting.\n");
517 err = -ENOMEM;
Roland Dreierec34a922005-08-19 10:59:31 -0700518 goto err_unmap_srq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520
521 return 0;
522
Roland Dreierec34a922005-08-19 10:59:31 -0700523err_unmap_srq:
524 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
525 mthca_free_icm_table(mdev, mdev->srq_table.table);
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527err_unmap_cq:
528 mthca_free_icm_table(mdev, mdev->cq_table.table);
529
Roland Dreier08aeb142005-04-16 15:26:34 -0700530err_unmap_rdb:
531 mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533err_unmap_eqp:
534 mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
535
536err_unmap_qp:
537 mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
538
539err_unmap_mpt:
540 mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
541
542err_unmap_mtt:
543 mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
544
545err_unmap_eq:
546 mthca_unmap_eq_icm(mdev);
547
548err_unmap_aux:
549 mthca_UNMAP_ICM_AUX(mdev, &status);
550
551err_free_aux:
552 mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
553
554 return err;
555}
556
Michael S. Tsirkinaba7a222005-09-30 13:55:50 -0700557static void mthca_free_icms(struct mthca_dev *mdev)
558{
559 u8 status;
560
561 mthca_free_icm_table(mdev, mdev->mcg_table.table);
562 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
563 mthca_free_icm_table(mdev, mdev->srq_table.table);
564 mthca_free_icm_table(mdev, mdev->cq_table.table);
565 mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
566 mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
567 mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
568 mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
569 mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
570 mthca_unmap_eq_icm(mdev);
571
572 mthca_UNMAP_ICM_AUX(mdev, &status);
573 mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
574}
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576static int __devinit mthca_init_arbel(struct mthca_dev *mdev)
577{
578 struct mthca_dev_lim dev_lim;
579 struct mthca_profile profile;
580 struct mthca_init_hca_param init_hca;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 u64 icm_size;
582 u8 status;
583 int err;
584
585 err = mthca_QUERY_FW(mdev, &status);
586 if (err) {
587 mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
588 return err;
589 }
590 if (status) {
591 mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
592 "aborting.\n", status);
593 return -EINVAL;
594 }
595
596 err = mthca_ENABLE_LAM(mdev, &status);
597 if (err) {
598 mthca_err(mdev, "ENABLE_LAM command failed, aborting.\n");
599 return err;
600 }
601 if (status == MTHCA_CMD_STAT_LAM_NOT_PRE) {
602 mthca_dbg(mdev, "No HCA-attached memory (running in MemFree mode)\n");
603 mdev->mthca_flags |= MTHCA_FLAG_NO_LAM;
604 } else if (status) {
605 mthca_err(mdev, "ENABLE_LAM returned status 0x%02x, "
606 "aborting.\n", status);
607 return -EINVAL;
608 }
609
610 err = mthca_load_fw(mdev);
611 if (err) {
612 mthca_err(mdev, "Failed to start FW, aborting.\n");
613 goto err_disable;
614 }
615
616 err = mthca_dev_lim(mdev, &dev_lim);
617 if (err) {
618 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
619 goto err_stop_fw;
620 }
621
622 profile = default_profile;
623 profile.num_uar = dev_lim.uar_size / PAGE_SIZE;
624 profile.num_udav = 0;
Roland Dreierec34a922005-08-19 10:59:31 -0700625 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
626 profile.num_srq = dev_lim.max_srqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 icm_size = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
629 if ((int) icm_size < 0) {
630 err = icm_size;
631 goto err_stop_fw;
632 }
633
634 err = mthca_init_icm(mdev, &dev_lim, &init_hca, icm_size);
635 if (err)
636 goto err_stop_fw;
637
638 err = mthca_INIT_HCA(mdev, &init_hca, &status);
639 if (err) {
640 mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
641 goto err_free_icm;
642 }
643 if (status) {
644 mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
645 "aborting.\n", status);
646 err = -EINVAL;
647 goto err_free_icm;
648 }
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return 0;
651
652err_free_icm:
Michael S. Tsirkinaba7a222005-09-30 13:55:50 -0700653 mthca_free_icms(mdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655err_stop_fw:
656 mthca_UNMAP_FA(mdev, &status);
657 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
658
659err_disable:
660 if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
661 mthca_DISABLE_LAM(mdev, &status);
662
663 return err;
664}
665
Michael S. Tsirkin2e8b9812005-08-13 21:19:38 -0700666static void mthca_close_hca(struct mthca_dev *mdev)
667{
668 u8 status;
669
670 mthca_CLOSE_HCA(mdev, 0, &status);
671
672 if (mthca_is_memfree(mdev)) {
Michael S. Tsirkinaba7a222005-09-30 13:55:50 -0700673 mthca_free_icms(mdev);
Michael S. Tsirkin2e8b9812005-08-13 21:19:38 -0700674
675 mthca_UNMAP_FA(mdev, &status);
676 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
677
678 if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
679 mthca_DISABLE_LAM(mdev, &status);
680 } else
681 mthca_SYS_DIS(mdev, &status);
682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684static int __devinit mthca_init_hca(struct mthca_dev *mdev)
685{
Michael S. Tsirkin2e8b9812005-08-13 21:19:38 -0700686 u8 status;
687 int err;
688 struct mthca_adapter adapter;
689
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700690 if (mthca_is_memfree(mdev))
Michael S. Tsirkin2e8b9812005-08-13 21:19:38 -0700691 err = mthca_init_arbel(mdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 else
Michael S. Tsirkin2e8b9812005-08-13 21:19:38 -0700693 err = mthca_init_tavor(mdev);
694
695 if (err)
696 return err;
697
698 err = mthca_QUERY_ADAPTER(mdev, &adapter, &status);
699 if (err) {
700 mthca_err(mdev, "QUERY_ADAPTER command failed, aborting.\n");
701 goto err_close;
702 }
703 if (status) {
704 mthca_err(mdev, "QUERY_ADAPTER returned status 0x%02x, "
705 "aborting.\n", status);
706 err = -EINVAL;
707 goto err_close;
708 }
709
710 mdev->eq_table.inta_pin = adapter.inta_pin;
711 mdev->rev_id = adapter.revision_id;
712 memcpy(mdev->board_id, adapter.board_id, sizeof mdev->board_id);
713
714 return 0;
715
716err_close:
717 mthca_close_hca(mdev);
718 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
721static int __devinit mthca_setup_hca(struct mthca_dev *dev)
722{
723 int err;
724 u8 status;
725
726 MTHCA_INIT_DOORBELL_LOCK(&dev->doorbell_lock);
727
728 err = mthca_init_uar_table(dev);
729 if (err) {
730 mthca_err(dev, "Failed to initialize "
731 "user access region table, aborting.\n");
732 return err;
733 }
734
735 err = mthca_uar_alloc(dev, &dev->driver_uar);
736 if (err) {
737 mthca_err(dev, "Failed to allocate driver access region, "
738 "aborting.\n");
739 goto err_uar_table_free;
740 }
741
742 dev->kar = ioremap(dev->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
743 if (!dev->kar) {
744 mthca_err(dev, "Couldn't map kernel access region, "
745 "aborting.\n");
746 err = -ENOMEM;
747 goto err_uar_free;
748 }
749
750 err = mthca_init_pd_table(dev);
751 if (err) {
752 mthca_err(dev, "Failed to initialize "
753 "protection domain table, aborting.\n");
754 goto err_kar_unmap;
755 }
756
757 err = mthca_init_mr_table(dev);
758 if (err) {
759 mthca_err(dev, "Failed to initialize "
760 "memory region table, aborting.\n");
761 goto err_pd_table_free;
762 }
763
Roland Dreier99264c12005-07-07 17:57:18 -0700764 err = mthca_pd_alloc(dev, 1, &dev->driver_pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (err) {
766 mthca_err(dev, "Failed to create driver PD, "
767 "aborting.\n");
768 goto err_mr_table_free;
769 }
770
771 err = mthca_init_eq_table(dev);
772 if (err) {
773 mthca_err(dev, "Failed to initialize "
774 "event queue table, aborting.\n");
775 goto err_pd_free;
776 }
777
778 err = mthca_cmd_use_events(dev);
779 if (err) {
780 mthca_err(dev, "Failed to switch to event-driven "
781 "firmware commands, aborting.\n");
782 goto err_eq_table_free;
783 }
784
785 err = mthca_NOP(dev, &status);
786 if (err || status) {
Roland Dreier4ad81172005-04-16 15:26:21 -0700787 mthca_err(dev, "NOP command failed to generate interrupt (IRQ %d), aborting.\n",
788 dev->mthca_flags & MTHCA_FLAG_MSI_X ?
789 dev->eq_table.eq[MTHCA_EQ_CMD].msi_x_vector :
790 dev->pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (dev->mthca_flags & (MTHCA_FLAG_MSI | MTHCA_FLAG_MSI_X))
792 mthca_err(dev, "Try again with MSI/MSI-X disabled.\n");
793 else
794 mthca_err(dev, "BIOS or ACPI interrupt routing problem?\n");
795
796 goto err_cmd_poll;
797 }
798
799 mthca_dbg(dev, "NOP command IRQ test passed\n");
800
801 err = mthca_init_cq_table(dev);
802 if (err) {
803 mthca_err(dev, "Failed to initialize "
804 "completion queue table, aborting.\n");
805 goto err_cmd_poll;
806 }
807
Roland Dreierec34a922005-08-19 10:59:31 -0700808 err = mthca_init_srq_table(dev);
809 if (err) {
810 mthca_err(dev, "Failed to initialize "
811 "shared receive queue table, aborting.\n");
812 goto err_cq_table_free;
813 }
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 err = mthca_init_qp_table(dev);
816 if (err) {
817 mthca_err(dev, "Failed to initialize "
818 "queue pair table, aborting.\n");
Roland Dreierec34a922005-08-19 10:59:31 -0700819 goto err_srq_table_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821
822 err = mthca_init_av_table(dev);
823 if (err) {
824 mthca_err(dev, "Failed to initialize "
825 "address vector table, aborting.\n");
826 goto err_qp_table_free;
827 }
828
829 err = mthca_init_mcg_table(dev);
830 if (err) {
831 mthca_err(dev, "Failed to initialize "
832 "multicast group table, aborting.\n");
833 goto err_av_table_free;
834 }
835
836 return 0;
837
838err_av_table_free:
839 mthca_cleanup_av_table(dev);
840
841err_qp_table_free:
842 mthca_cleanup_qp_table(dev);
843
Roland Dreierec34a922005-08-19 10:59:31 -0700844err_srq_table_free:
845 mthca_cleanup_srq_table(dev);
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847err_cq_table_free:
848 mthca_cleanup_cq_table(dev);
849
850err_cmd_poll:
851 mthca_cmd_use_polling(dev);
852
853err_eq_table_free:
854 mthca_cleanup_eq_table(dev);
855
856err_pd_free:
857 mthca_pd_free(dev, &dev->driver_pd);
858
859err_mr_table_free:
860 mthca_cleanup_mr_table(dev);
861
862err_pd_table_free:
863 mthca_cleanup_pd_table(dev);
864
865err_kar_unmap:
866 iounmap(dev->kar);
867
868err_uar_free:
869 mthca_uar_free(dev, &dev->driver_uar);
870
871err_uar_table_free:
872 mthca_cleanup_uar_table(dev);
873 return err;
874}
875
876static int __devinit mthca_request_regions(struct pci_dev *pdev,
877 int ddr_hidden)
878{
879 int err;
880
881 /*
882 * We can't just use pci_request_regions() because the MSI-X
883 * table is right in the middle of the first BAR. If we did
884 * pci_request_region and grab all of the first BAR, then
885 * setting up MSI-X would fail, since the PCI core wants to do
886 * request_mem_region on the MSI-X vector table.
887 *
888 * So just request what we need right now, and request any
889 * other regions we need when setting up EQs.
890 */
891 if (!request_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
892 MTHCA_HCR_SIZE, DRV_NAME))
893 return -EBUSY;
894
895 err = pci_request_region(pdev, 2, DRV_NAME);
896 if (err)
897 goto err_bar2_failed;
898
899 if (!ddr_hidden) {
900 err = pci_request_region(pdev, 4, DRV_NAME);
901 if (err)
902 goto err_bar4_failed;
903 }
904
905 return 0;
906
907err_bar4_failed:
908 pci_release_region(pdev, 2);
909
910err_bar2_failed:
911 release_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
912 MTHCA_HCR_SIZE);
913
914 return err;
915}
916
917static void mthca_release_regions(struct pci_dev *pdev,
918 int ddr_hidden)
919{
920 if (!ddr_hidden)
921 pci_release_region(pdev, 4);
922
923 pci_release_region(pdev, 2);
924
925 release_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
926 MTHCA_HCR_SIZE);
927}
928
929static int __devinit mthca_enable_msi_x(struct mthca_dev *mdev)
930{
931 struct msix_entry entries[3];
932 int err;
933
934 entries[0].entry = 0;
935 entries[1].entry = 1;
936 entries[2].entry = 2;
937
938 err = pci_enable_msix(mdev->pdev, entries, ARRAY_SIZE(entries));
939 if (err) {
940 if (err > 0)
941 mthca_info(mdev, "Only %d MSI-X vectors available, "
942 "not using MSI-X\n", err);
943 return err;
944 }
945
946 mdev->eq_table.eq[MTHCA_EQ_COMP ].msi_x_vector = entries[0].vector;
947 mdev->eq_table.eq[MTHCA_EQ_ASYNC].msi_x_vector = entries[1].vector;
948 mdev->eq_table.eq[MTHCA_EQ_CMD ].msi_x_vector = entries[2].vector;
949
950 return 0;
951}
952
Roland Dreier68a3c212005-04-16 15:26:34 -0700953/* Types of supported HCA */
954enum {
955 TAVOR, /* MT23108 */
956 ARBEL_COMPAT, /* MT25208 in Tavor compat mode */
957 ARBEL_NATIVE, /* MT25208 with extended features */
958 SINAI /* MT25204 */
959};
960
961#define MTHCA_FW_VER(major, minor, subminor) \
962 (((u64) (major) << 32) | ((u64) (minor) << 16) | (u64) (subminor))
963
964static struct {
965 u64 latest_fw;
Eli Cohen651eaac2006-03-02 12:40:46 -0800966 u32 flags;
Roland Dreier68a3c212005-04-16 15:26:34 -0700967} mthca_hca_table[] = {
Roland Dreier6226bb52006-03-02 13:56:37 -0800968 [TAVOR] = { .latest_fw = MTHCA_FW_VER(3, 4, 0),
Eli Cohen651eaac2006-03-02 12:40:46 -0800969 .flags = 0 },
Michael S. Tsirkin834ac732006-08-22 22:45:06 +0300970 [ARBEL_COMPAT] = { .latest_fw = MTHCA_FW_VER(4, 7, 600),
Eli Cohen651eaac2006-03-02 12:40:46 -0800971 .flags = MTHCA_FLAG_PCIE },
Michael S. Tsirkin834ac732006-08-22 22:45:06 +0300972 [ARBEL_NATIVE] = { .latest_fw = MTHCA_FW_VER(5, 1, 400),
Eli Cohen651eaac2006-03-02 12:40:46 -0800973 .flags = MTHCA_FLAG_MEMFREE |
974 MTHCA_FLAG_PCIE },
Michael S. Tsirkin834ac732006-08-22 22:45:06 +0300975 [SINAI] = { .latest_fw = MTHCA_FW_VER(1, 1, 0),
Eli Cohen651eaac2006-03-02 12:40:46 -0800976 .flags = MTHCA_FLAG_MEMFREE |
977 MTHCA_FLAG_PCIE |
978 MTHCA_FLAG_SINAI_OPT }
Roland Dreier68a3c212005-04-16 15:26:34 -0700979};
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981static int __devinit mthca_init_one(struct pci_dev *pdev,
982 const struct pci_device_id *id)
983{
984 static int mthca_version_printed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 int ddr_hidden = 0;
986 int err;
987 struct mthca_dev *mdev;
988
989 if (!mthca_version_printed) {
990 printk(KERN_INFO "%s", mthca_version);
991 ++mthca_version_printed;
992 }
993
Adrian Bunk982245f2005-07-17 04:22:20 +0200994 printk(KERN_INFO PFX "Initializing %s\n",
995 pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Roland Dreier68a3c212005-04-16 15:26:34 -0700997 if (id->driver_data >= ARRAY_SIZE(mthca_hca_table)) {
Adrian Bunk982245f2005-07-17 04:22:20 +0200998 printk(KERN_ERR PFX "%s has invalid driver data %lx\n",
999 pci_name(pdev), id->driver_data);
Roland Dreier68a3c212005-04-16 15:26:34 -07001000 return -ENODEV;
1001 }
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 err = pci_enable_device(pdev);
1004 if (err) {
1005 dev_err(&pdev->dev, "Cannot enable PCI device, "
1006 "aborting.\n");
1007 return err;
1008 }
1009
1010 /*
1011 * Check for BARs. We expect 0: 1MB, 2: 8MB, 4: DDR (may not
1012 * be present)
1013 */
1014 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
1015 pci_resource_len(pdev, 0) != 1 << 20) {
Bernhard Fischer177214a2005-06-27 14:36:39 -07001016 dev_err(&pdev->dev, "Missing DCS, aborting.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 err = -ENODEV;
1018 goto err_disable_pdev;
1019 }
Michael S. Tsirkincbd29812006-01-30 15:20:35 -08001020 if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
Bernhard Fischer177214a2005-06-27 14:36:39 -07001021 dev_err(&pdev->dev, "Missing UAR, aborting.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 err = -ENODEV;
1023 goto err_disable_pdev;
1024 }
1025 if (!(pci_resource_flags(pdev, 4) & IORESOURCE_MEM))
1026 ddr_hidden = 1;
1027
1028 err = mthca_request_regions(pdev, ddr_hidden);
1029 if (err) {
1030 dev_err(&pdev->dev, "Cannot obtain PCI resources, "
1031 "aborting.\n");
1032 goto err_disable_pdev;
1033 }
1034
1035 pci_set_master(pdev);
1036
1037 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
1038 if (err) {
1039 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask.\n");
1040 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1041 if (err) {
1042 dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting.\n");
1043 goto err_free_res;
1044 }
1045 }
1046 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
1047 if (err) {
1048 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit "
1049 "consistent PCI DMA mask.\n");
1050 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
1051 if (err) {
1052 dev_err(&pdev->dev, "Can't set consistent PCI DMA mask, "
1053 "aborting.\n");
1054 goto err_free_res;
1055 }
1056 }
1057
1058 mdev = (struct mthca_dev *) ib_alloc_device(sizeof *mdev);
1059 if (!mdev) {
1060 dev_err(&pdev->dev, "Device struct alloc failed, "
1061 "aborting.\n");
1062 err = -ENOMEM;
1063 goto err_free_res;
1064 }
1065
Roland Dreier68a3c212005-04-16 15:26:34 -07001066 mdev->pdev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Eli Cohen651eaac2006-03-02 12:40:46 -08001068 mdev->mthca_flags = mthca_hca_table[id->driver_data].flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (ddr_hidden)
1070 mdev->mthca_flags |= MTHCA_FLAG_DDR_HIDDEN;
1071
1072 /*
1073 * Now reset the HCA before we touch the PCI capabilities or
1074 * attempt a firmware command, since a boot ROM may have left
1075 * the HCA in an undefined state.
1076 */
1077 err = mthca_reset(mdev);
1078 if (err) {
1079 mthca_err(mdev, "Failed to reset HCA, aborting.\n");
1080 goto err_free_dev;
1081 }
1082
1083 if (msi_x && !mthca_enable_msi_x(mdev))
1084 mdev->mthca_flags |= MTHCA_FLAG_MSI_X;
1085 if (msi && !(mdev->mthca_flags & MTHCA_FLAG_MSI_X) &&
1086 !pci_enable_msi(pdev))
1087 mdev->mthca_flags |= MTHCA_FLAG_MSI;
1088
Roland Dreier80fd8232005-06-27 14:36:45 -07001089 if (mthca_cmd_init(mdev)) {
1090 mthca_err(mdev, "Failed to init command interface, aborting.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 goto err_free_dev;
1092 }
1093
1094 err = mthca_tune_pci(mdev);
1095 if (err)
Roland Dreier80fd8232005-06-27 14:36:45 -07001096 goto err_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 err = mthca_init_hca(mdev);
1099 if (err)
Roland Dreier80fd8232005-06-27 14:36:45 -07001100 goto err_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Roland Dreier68a3c212005-04-16 15:26:34 -07001102 if (mdev->fw_ver < mthca_hca_table[id->driver_data].latest_fw) {
Roland Dreier87cfe322005-11-02 22:59:37 -08001103 mthca_warn(mdev, "HCA FW version %d.%d.%d is old (%d.%d.%d is current).\n",
Roland Dreier68a3c212005-04-16 15:26:34 -07001104 (int) (mdev->fw_ver >> 32), (int) (mdev->fw_ver >> 16) & 0xffff,
1105 (int) (mdev->fw_ver & 0xffff),
1106 (int) (mthca_hca_table[id->driver_data].latest_fw >> 32),
1107 (int) (mthca_hca_table[id->driver_data].latest_fw >> 16) & 0xffff,
1108 (int) (mthca_hca_table[id->driver_data].latest_fw & 0xffff));
1109 mthca_warn(mdev, "If you have problems, try updating your HCA FW.\n");
1110 }
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 err = mthca_setup_hca(mdev);
1113 if (err)
1114 goto err_close;
1115
1116 err = mthca_register_device(mdev);
1117 if (err)
1118 goto err_cleanup;
1119
1120 err = mthca_create_agents(mdev);
1121 if (err)
1122 goto err_unregister;
1123
1124 pci_set_drvdata(pdev, mdev);
1125
1126 return 0;
1127
1128err_unregister:
1129 mthca_unregister_device(mdev);
1130
1131err_cleanup:
1132 mthca_cleanup_mcg_table(mdev);
1133 mthca_cleanup_av_table(mdev);
1134 mthca_cleanup_qp_table(mdev);
Roland Dreierec34a922005-08-19 10:59:31 -07001135 mthca_cleanup_srq_table(mdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 mthca_cleanup_cq_table(mdev);
1137 mthca_cmd_use_polling(mdev);
1138 mthca_cleanup_eq_table(mdev);
1139
1140 mthca_pd_free(mdev, &mdev->driver_pd);
1141
1142 mthca_cleanup_mr_table(mdev);
1143 mthca_cleanup_pd_table(mdev);
1144 mthca_cleanup_uar_table(mdev);
1145
1146err_close:
1147 mthca_close_hca(mdev);
1148
Roland Dreier80fd8232005-06-27 14:36:45 -07001149err_cmd:
1150 mthca_cmd_cleanup(mdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152err_free_dev:
1153 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1154 pci_disable_msix(pdev);
1155 if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1156 pci_disable_msi(pdev);
1157
1158 ib_dealloc_device(&mdev->ib_dev);
1159
1160err_free_res:
1161 mthca_release_regions(pdev, ddr_hidden);
1162
1163err_disable_pdev:
1164 pci_disable_device(pdev);
1165 pci_set_drvdata(pdev, NULL);
1166 return err;
1167}
1168
1169static void __devexit mthca_remove_one(struct pci_dev *pdev)
1170{
1171 struct mthca_dev *mdev = pci_get_drvdata(pdev);
1172 u8 status;
1173 int p;
1174
1175 if (mdev) {
1176 mthca_free_agents(mdev);
1177 mthca_unregister_device(mdev);
1178
1179 for (p = 1; p <= mdev->limits.num_ports; ++p)
1180 mthca_CLOSE_IB(mdev, p, &status);
1181
1182 mthca_cleanup_mcg_table(mdev);
1183 mthca_cleanup_av_table(mdev);
1184 mthca_cleanup_qp_table(mdev);
Roland Dreierec34a922005-08-19 10:59:31 -07001185 mthca_cleanup_srq_table(mdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 mthca_cleanup_cq_table(mdev);
1187 mthca_cmd_use_polling(mdev);
1188 mthca_cleanup_eq_table(mdev);
1189
1190 mthca_pd_free(mdev, &mdev->driver_pd);
1191
1192 mthca_cleanup_mr_table(mdev);
1193 mthca_cleanup_pd_table(mdev);
1194
1195 iounmap(mdev->kar);
1196 mthca_uar_free(mdev, &mdev->driver_uar);
1197 mthca_cleanup_uar_table(mdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 mthca_close_hca(mdev);
Roland Dreier80fd8232005-06-27 14:36:45 -07001199 mthca_cmd_cleanup(mdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1202 pci_disable_msix(pdev);
1203 if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1204 pci_disable_msi(pdev);
1205
1206 ib_dealloc_device(&mdev->ib_dev);
1207 mthca_release_regions(pdev, mdev->mthca_flags &
1208 MTHCA_FLAG_DDR_HIDDEN);
1209 pci_disable_device(pdev);
1210 pci_set_drvdata(pdev, NULL);
1211 }
1212}
1213
1214static struct pci_device_id mthca_pci_table[] = {
1215 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_TAVOR),
1216 .driver_data = TAVOR },
1217 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_TAVOR),
1218 .driver_data = TAVOR },
1219 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
1220 .driver_data = ARBEL_COMPAT },
1221 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
1222 .driver_data = ARBEL_COMPAT },
1223 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL),
1224 .driver_data = ARBEL_NATIVE },
1225 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL),
1226 .driver_data = ARBEL_NATIVE },
Roland Dreier68a3c212005-04-16 15:26:34 -07001227 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI),
1228 .driver_data = SINAI },
1229 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI),
1230 .driver_data = SINAI },
1231 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
1232 .driver_data = SINAI },
1233 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
1234 .driver_data = SINAI },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 { 0, }
1236};
1237
1238MODULE_DEVICE_TABLE(pci, mthca_pci_table);
1239
1240static struct pci_driver mthca_driver = {
Bernhard Fischer177214a2005-06-27 14:36:39 -07001241 .name = DRV_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 .id_table = mthca_pci_table,
1243 .probe = mthca_init_one,
1244 .remove = __devexit_p(mthca_remove_one)
1245};
1246
1247static int __init mthca_init(void)
1248{
1249 int ret;
1250
1251 ret = pci_register_driver(&mthca_driver);
1252 return ret < 0 ? ret : 0;
1253}
1254
1255static void __exit mthca_cleanup(void)
1256{
1257 pci_unregister_driver(&mthca_driver);
1258}
1259
1260module_init(mthca_init);
1261module_exit(mthca_cleanup);