blob: 9a9dd32885a0877532fab74f91be3e08b3348bf7 [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
Roland Dreier0b0df6f2006-12-15 20:55:28 -080083DEFINE_MUTEX(mthca_device_mutex);
Jack Morgensteinb3b30f52006-08-15 21:11:18 +030084
Leonid Arsh82da7032006-12-10 13:40:17 +020085#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)
94
95static 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 */
105};
106
107module_param_named(num_qp, hca_profile.num_qp, int, 0444);
108MODULE_PARM_DESC(num_qp, "maximum number of QPs per HCA");
109
110module_param_named(rdb_per_qp, hca_profile.rdb_per_qp, int, 0444);
111MODULE_PARM_DESC(rdb_per_qp, "number of RDB buffers per QP");
112
113module_param_named(num_cq, hca_profile.num_cq, int, 0444);
114MODULE_PARM_DESC(num_cq, "maximum number of CQs per HCA");
115
116module_param_named(num_mcg, hca_profile.num_mcg, int, 0444);
117MODULE_PARM_DESC(num_mcg, "maximum number of multicast groups per HCA");
118
119module_param_named(num_mpt, hca_profile.num_mpt, int, 0444);
120MODULE_PARM_DESC(num_mpt,
121 "maximum number of memory protection table entries per HCA");
122
123module_param_named(num_mtt, hca_profile.num_mtt, int, 0444);
124MODULE_PARM_DESC(num_mtt,
125 "maximum number of memory translation table segments per HCA");
126
127module_param_named(num_udav, hca_profile.num_udav, int, 0444);
128MODULE_PARM_DESC(num_udav, "maximum number of UD address vectors per HCA");
129
130module_param_named(fmr_reserved_mtts, hca_profile.fmr_reserved_mtts, int, 0444);
131MODULE_PARM_DESC(fmr_reserved_mtts,
132 "number of memory translation table segments reserved for FMR");
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static const char mthca_version[] __devinitdata =
Bernhard Fischer177214a2005-06-27 14:36:39 -0700135 DRV_NAME ": Mellanox InfiniBand HCA driver v"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 DRV_VERSION " (" DRV_RELDATE ")\n";
137
Roland Dreierf4f3d0f2006-11-29 15:33:06 -0800138static int mthca_tune_pci(struct mthca_dev *mdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 int cap;
141 u16 val;
142
Michael S. Tsirkinabf45db2006-04-05 15:47:16 +0300143 if (!tune_pci)
144 return 0;
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 /* First try to max out Read Byte Count */
147 cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_PCIX);
148 if (cap) {
149 if (pci_read_config_word(mdev->pdev, cap + PCI_X_CMD, &val)) {
150 mthca_err(mdev, "Couldn't read PCI-X command register, "
151 "aborting.\n");
152 return -ENODEV;
153 }
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, "
157 "aborting.\n");
158 return -ENODEV;
159 }
Roland Dreier68a3c212005-04-16 15:26:34 -0700160 } else if (!(mdev->mthca_flags & MTHCA_FLAG_PCIE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 mthca_info(mdev, "No PCI-X capability, not setting RBC.\n");
162
163 cap = pci_find_capability(mdev->pdev, PCI_CAP_ID_EXP);
164 if (cap) {
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");
168 return -ENODEV;
169 }
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");
174 return -ENODEV;
175 }
Roland Dreier68a3c212005-04-16 15:26:34 -0700176 } else if (mdev->mthca_flags & MTHCA_FLAG_PCIE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 mthca_info(mdev, "No PCI Express capability, "
178 "not setting Max Read Request Size.\n");
179
180 return 0;
181}
182
Roland Dreierf4f3d0f2006-11-29 15:33:06 -0800183static int mthca_dev_lim(struct mthca_dev *mdev, struct mthca_dev_lim *dev_lim)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 int err;
186 u8 status;
187
188 err = mthca_QUERY_DEV_LIM(mdev, dev_lim, &status);
189 if (err) {
190 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
191 return err;
192 }
193 if (status) {
194 mthca_err(mdev, "QUERY_DEV_LIM returned status 0x%02x, "
195 "aborting.\n", status);
196 return -EINVAL;
197 }
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);
202 return -ENODEV;
203 }
204 if (dev_lim->num_ports > MTHCA_MAX_PORTS) {
205 mthca_err(mdev, "HCA has %d ports, but we only support %d, "
206 "aborting.\n",
207 dev_lim->num_ports, MTHCA_MAX_PORTS);
208 return -ENODEV;
209 }
210
Michael S. Tsirkincbd29812006-01-30 15:20:35 -0800211 if (dev_lim->uar_size > pci_resource_len(mdev->pdev, 2)) {
212 mthca_err(mdev, "HCA reported UAR size of 0x%x bigger than "
Greg Kroah-Hartmane29419f2006-06-12 15:20:16 -0700213 "PCI resource 2 size of 0x%llx, aborting.\n",
214 dev_lim->uar_size,
215 (unsigned long long)pci_resource_len(mdev->pdev, 2));
Michael S. Tsirkincbd29812006-01-30 15:20:35 -0800216 return -ENODEV;
217 }
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 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;
Jack Morgensteinefaae8f2005-10-10 13:48:07 -0700226 mdev->limits.max_wqes = dev_lim->max_qp_sz;
227 mdev->limits.max_qp_init_rdma = dev_lim->max_requester_per_qp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 mdev->limits.reserved_qps = dev_lim->reserved_qps;
Jack Morgensteinefaae8f2005-10-10 13:48:07 -0700229 mdev->limits.max_srq_wqes = dev_lim->max_srq_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 mdev->limits.reserved_srqs = dev_lim->reserved_srqs;
231 mdev->limits.reserved_eecs = dev_lim->reserved_eecs;
Jack Morgenstein77369ed2005-11-09 11:26:07 -0800232 mdev->limits.max_desc_sz = dev_lim->max_desc_sz;
Jack Morgenstein59fef3b2006-04-11 18:16:27 +0300233 mdev->limits.max_srq_sge = mthca_max_srq_sge(mdev);
Jack Morgensteinefaae8f2005-10-10 13:48:07 -0700234 /*
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.
238 */
239 mdev->limits.max_cqes = dev_lim->max_cq_sz - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 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;
Roland Dreierda6561c2005-08-17 07:39:10 -0700246 mdev->limits.port_width_cap = dev_lim->max_port_width;
Jack Morgenstein0f69ce12005-11-04 16:03:32 -0800247 mdev->limits.page_size_cap = ~(u32) (dev_lim->min_page_sz - 1);
Jack Morgenstein33033b72005-09-26 12:30:02 -0700248 mdev->limits.flags = dev_lim->flags;
Jack Morgensteinbf6a9e32006-04-10 09:43:47 -0700249 /*
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
253 * values of 2 and 3.
254 */
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;
259 else
260 mdev->limits.stat_rate_support = 0x3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 /* IB_DEVICE_RESIZE_MAX_WR not supported by driver.
263 May be doable since hardware supports it for SRQ.
264
265 IB_DEVICE_N_NOTIFY_CQ is supported by hardware but not by driver.
266
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;
273
274 if (dev_lim->flags & DEV_LIM_FLAG_BAD_PKEY_CNTR)
275 mdev->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
276
277 if (dev_lim->flags & DEV_LIM_FLAG_BAD_QKEY_CNTR)
278 mdev->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
279
280 if (dev_lim->flags & DEV_LIM_FLAG_RAW_MULTI)
281 mdev->device_cap_flags |= IB_DEVICE_RAW_MULTI;
282
283 if (dev_lim->flags & DEV_LIM_FLAG_AUTO_PATH_MIG)
284 mdev->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
285
286 if (dev_lim->flags & DEV_LIM_FLAG_UD_AV_PORT_ENFORCE)
287 mdev->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
288
289 if (dev_lim->flags & DEV_LIM_FLAG_SRQ)
290 mdev->mthca_flags |= MTHCA_FLAG_SRQ;
291
292 return 0;
293}
294
Roland Dreierf4f3d0f2006-11-29 15:33:06 -0800295static int mthca_init_tavor(struct mthca_dev *mdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 u8 status;
298 int err;
299 struct mthca_dev_lim dev_lim;
300 struct mthca_profile profile;
301 struct mthca_init_hca_param init_hca;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 err = mthca_SYS_EN(mdev, &status);
304 if (err) {
305 mthca_err(mdev, "SYS_EN command failed, aborting.\n");
306 return err;
307 }
308 if (status) {
309 mthca_err(mdev, "SYS_EN returned status 0x%02x, "
310 "aborting.\n", status);
311 return -EINVAL;
312 }
313
314 err = mthca_QUERY_FW(mdev, &status);
315 if (err) {
316 mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
317 goto err_disable;
318 }
319 if (status) {
320 mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
321 "aborting.\n", status);
322 err = -EINVAL;
323 goto err_disable;
324 }
325 err = mthca_QUERY_DDR(mdev, &status);
326 if (err) {
327 mthca_err(mdev, "QUERY_DDR command failed, aborting.\n");
328 goto err_disable;
329 }
330 if (status) {
331 mthca_err(mdev, "QUERY_DDR returned status 0x%02x, "
332 "aborting.\n", status);
333 err = -EINVAL;
334 goto err_disable;
335 }
336
337 err = mthca_dev_lim(mdev, &dev_lim);
Jack Morgensteinaa2f9362006-01-05 16:12:01 -0800338 if (err) {
339 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
340 goto err_disable;
341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Leonid Arsh82da7032006-12-10 13:40:17 +0200343 profile = hca_profile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 profile.num_uar = dev_lim.uar_size / PAGE_SIZE;
345 profile.uarc_size = 0;
Roland Dreierec34a922005-08-19 10:59:31 -0700346 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
347 profile.num_srq = dev_lim.max_srqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 err = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
350 if (err < 0)
351 goto err_disable;
352
353 err = mthca_INIT_HCA(mdev, &init_hca, &status);
354 if (err) {
355 mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
356 goto err_disable;
357 }
358 if (status) {
359 mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
360 "aborting.\n", status);
361 err = -EINVAL;
362 goto err_disable;
363 }
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return 0;
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367err_disable:
368 mthca_SYS_DIS(mdev, &status);
369
370 return err;
371}
372
Roland Dreierf4f3d0f2006-11-29 15:33:06 -0800373static int mthca_load_fw(struct mthca_dev *mdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 u8 status;
376 int err;
377
378 /* FIXME: use HCA-attached memory for FW if present */
379
380 mdev->fw.arbel.fw_icm =
381 mthca_alloc_icm(mdev, mdev->fw.arbel.fw_pages,
382 GFP_HIGHUSER | __GFP_NOWARN);
383 if (!mdev->fw.arbel.fw_icm) {
384 mthca_err(mdev, "Couldn't allocate FW area, aborting.\n");
385 return -ENOMEM;
386 }
387
388 err = mthca_MAP_FA(mdev, mdev->fw.arbel.fw_icm, &status);
389 if (err) {
390 mthca_err(mdev, "MAP_FA command failed, aborting.\n");
391 goto err_free;
392 }
393 if (status) {
394 mthca_err(mdev, "MAP_FA returned status 0x%02x, aborting.\n", status);
395 err = -EINVAL;
396 goto err_free;
397 }
398 err = mthca_RUN_FW(mdev, &status);
399 if (err) {
400 mthca_err(mdev, "RUN_FW command failed, aborting.\n");
401 goto err_unmap_fa;
402 }
403 if (status) {
404 mthca_err(mdev, "RUN_FW returned status 0x%02x, aborting.\n", status);
405 err = -EINVAL;
406 goto err_unmap_fa;
407 }
408
409 return 0;
410
411err_unmap_fa:
412 mthca_UNMAP_FA(mdev, &status);
413
414err_free:
415 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
416 return err;
417}
418
Roland Dreierf4f3d0f2006-11-29 15:33:06 -0800419static int mthca_init_icm(struct mthca_dev *mdev,
420 struct mthca_dev_lim *dev_lim,
421 struct mthca_init_hca_param *init_hca,
422 u64 icm_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 u64 aux_pages;
425 u8 status;
426 int err;
427
428 err = mthca_SET_ICM_SIZE(mdev, icm_size, &aux_pages, &status);
429 if (err) {
430 mthca_err(mdev, "SET_ICM_SIZE command failed, aborting.\n");
431 return err;
432 }
433 if (status) {
434 mthca_err(mdev, "SET_ICM_SIZE returned status 0x%02x, "
435 "aborting.\n", status);
436 return -EINVAL;
437 }
438
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);
442
443 mdev->fw.arbel.aux_icm = mthca_alloc_icm(mdev, aux_pages,
444 GFP_HIGHUSER | __GFP_NOWARN);
445 if (!mdev->fw.arbel.aux_icm) {
446 mthca_err(mdev, "Couldn't allocate aux memory, aborting.\n");
447 return -ENOMEM;
448 }
449
450 err = mthca_MAP_ICM_AUX(mdev, mdev->fw.arbel.aux_icm, &status);
451 if (err) {
452 mthca_err(mdev, "MAP_ICM_AUX command failed, aborting.\n");
453 goto err_free_aux;
454 }
455 if (status) {
456 mthca_err(mdev, "MAP_ICM_AUX returned status 0x%02x, aborting.\n", status);
457 err = -EINVAL;
458 goto err_free_aux;
459 }
460
461 err = mthca_map_eq_icm(mdev, init_hca->eqc_base);
462 if (err) {
463 mthca_err(mdev, "Failed to map EQ context memory, aborting.\n");
464 goto err_unmap_aux;
465 }
466
Michael S. Tsirkin1d1f19c2007-02-10 23:17:26 +0200467 /* 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;
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 mdev->mr_table.mtt_table = mthca_alloc_icm_table(mdev, init_hca->mtt_base,
Roland Dreier44ea6682005-04-16 15:26:24 -0700472 MTHCA_MTT_SEG_SIZE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 mdev->limits.num_mtt_segs,
474 mdev->limits.reserved_mtts, 1);
475 if (!mdev->mr_table.mtt_table) {
476 mthca_err(mdev, "Failed to map MTT context memory, aborting.\n");
477 err = -ENOMEM;
478 goto err_unmap_eq;
479 }
480
481 mdev->mr_table.mpt_table = mthca_alloc_icm_table(mdev, init_hca->mpt_base,
482 dev_lim->mpt_entry_sz,
483 mdev->limits.num_mpts,
484 mdev->limits.reserved_mrws, 1);
485 if (!mdev->mr_table.mpt_table) {
486 mthca_err(mdev, "Failed to map MPT context memory, aborting.\n");
487 err = -ENOMEM;
488 goto err_unmap_mtt;
489 }
490
491 mdev->qp_table.qp_table = mthca_alloc_icm_table(mdev, init_hca->qpc_base,
492 dev_lim->qpc_entry_sz,
493 mdev->limits.num_qps,
494 mdev->limits.reserved_qps, 0);
495 if (!mdev->qp_table.qp_table) {
496 mthca_err(mdev, "Failed to map QP context memory, aborting.\n");
497 err = -ENOMEM;
498 goto err_unmap_mpt;
499 }
500
501 mdev->qp_table.eqp_table = mthca_alloc_icm_table(mdev, init_hca->eqpc_base,
502 dev_lim->eqpc_entry_sz,
503 mdev->limits.num_qps,
504 mdev->limits.reserved_qps, 0);
505 if (!mdev->qp_table.eqp_table) {
506 mthca_err(mdev, "Failed to map EQP context memory, aborting.\n");
507 err = -ENOMEM;
508 goto err_unmap_qp;
509 }
510
Roland Dreier08aeb142005-04-16 15:26:34 -0700511 mdev->qp_table.rdb_table = mthca_alloc_icm_table(mdev, init_hca->rdb_base,
512 MTHCA_RDB_ENTRY_SIZE,
513 mdev->limits.num_qps <<
514 mdev->qp_table.rdb_shift,
515 0, 0);
516 if (!mdev->qp_table.rdb_table) {
517 mthca_err(mdev, "Failed to map RDB context memory, aborting\n");
518 err = -ENOMEM;
Roland Dreier19272d42005-04-16 15:26:35 -0700519 goto err_unmap_eqp;
Roland Dreier08aeb142005-04-16 15:26:34 -0700520 }
521
522 mdev->cq_table.table = mthca_alloc_icm_table(mdev, init_hca->cqc_base,
Roland Dreierec34a922005-08-19 10:59:31 -0700523 dev_lim->cqc_entry_sz,
524 mdev->limits.num_cqs,
525 mdev->limits.reserved_cqs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (!mdev->cq_table.table) {
527 mthca_err(mdev, "Failed to map CQ context memory, aborting.\n");
528 err = -ENOMEM;
Roland Dreier08aeb142005-04-16 15:26:34 -0700529 goto err_unmap_rdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531
Roland Dreierec34a922005-08-19 10:59:31 -0700532 if (mdev->mthca_flags & MTHCA_FLAG_SRQ) {
533 mdev->srq_table.table =
534 mthca_alloc_icm_table(mdev, init_hca->srqc_base,
535 dev_lim->srq_entry_sz,
536 mdev->limits.num_srqs,
537 mdev->limits.reserved_srqs, 0);
538 if (!mdev->srq_table.table) {
539 mthca_err(mdev, "Failed to map SRQ context memory, "
540 "aborting.\n");
541 err = -ENOMEM;
542 goto err_unmap_cq;
543 }
544 }
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /*
547 * It's not strictly required, but for simplicity just map the
548 * whole multicast group table now. The table isn't very big
549 * and it's a lot easier than trying to track ref counts.
550 */
551 mdev->mcg_table.table = mthca_alloc_icm_table(mdev, init_hca->mc_base,
552 MTHCA_MGM_ENTRY_SIZE,
553 mdev->limits.num_mgms +
554 mdev->limits.num_amgms,
555 mdev->limits.num_mgms +
556 mdev->limits.num_amgms,
557 0);
558 if (!mdev->mcg_table.table) {
559 mthca_err(mdev, "Failed to map MCG context memory, aborting.\n");
560 err = -ENOMEM;
Roland Dreierec34a922005-08-19 10:59:31 -0700561 goto err_unmap_srq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563
564 return 0;
565
Roland Dreierec34a922005-08-19 10:59:31 -0700566err_unmap_srq:
567 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
568 mthca_free_icm_table(mdev, mdev->srq_table.table);
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570err_unmap_cq:
571 mthca_free_icm_table(mdev, mdev->cq_table.table);
572
Roland Dreier08aeb142005-04-16 15:26:34 -0700573err_unmap_rdb:
574 mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576err_unmap_eqp:
577 mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
578
579err_unmap_qp:
580 mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
581
582err_unmap_mpt:
583 mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
584
585err_unmap_mtt:
586 mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
587
588err_unmap_eq:
589 mthca_unmap_eq_icm(mdev);
590
591err_unmap_aux:
592 mthca_UNMAP_ICM_AUX(mdev, &status);
593
594err_free_aux:
595 mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
596
597 return err;
598}
599
Michael S. Tsirkinaba7a222005-09-30 13:55:50 -0700600static void mthca_free_icms(struct mthca_dev *mdev)
601{
602 u8 status;
603
604 mthca_free_icm_table(mdev, mdev->mcg_table.table);
605 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
606 mthca_free_icm_table(mdev, mdev->srq_table.table);
607 mthca_free_icm_table(mdev, mdev->cq_table.table);
608 mthca_free_icm_table(mdev, mdev->qp_table.rdb_table);
609 mthca_free_icm_table(mdev, mdev->qp_table.eqp_table);
610 mthca_free_icm_table(mdev, mdev->qp_table.qp_table);
611 mthca_free_icm_table(mdev, mdev->mr_table.mpt_table);
612 mthca_free_icm_table(mdev, mdev->mr_table.mtt_table);
613 mthca_unmap_eq_icm(mdev);
614
615 mthca_UNMAP_ICM_AUX(mdev, &status);
616 mthca_free_icm(mdev, mdev->fw.arbel.aux_icm);
617}
618
Roland Dreierf4f3d0f2006-11-29 15:33:06 -0800619static int mthca_init_arbel(struct mthca_dev *mdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 struct mthca_dev_lim dev_lim;
622 struct mthca_profile profile;
623 struct mthca_init_hca_param init_hca;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 u64 icm_size;
625 u8 status;
626 int err;
627
628 err = mthca_QUERY_FW(mdev, &status);
629 if (err) {
630 mthca_err(mdev, "QUERY_FW command failed, aborting.\n");
631 return err;
632 }
633 if (status) {
634 mthca_err(mdev, "QUERY_FW returned status 0x%02x, "
635 "aborting.\n", status);
636 return -EINVAL;
637 }
638
639 err = mthca_ENABLE_LAM(mdev, &status);
640 if (err) {
641 mthca_err(mdev, "ENABLE_LAM command failed, aborting.\n");
642 return err;
643 }
644 if (status == MTHCA_CMD_STAT_LAM_NOT_PRE) {
645 mthca_dbg(mdev, "No HCA-attached memory (running in MemFree mode)\n");
646 mdev->mthca_flags |= MTHCA_FLAG_NO_LAM;
647 } else if (status) {
648 mthca_err(mdev, "ENABLE_LAM returned status 0x%02x, "
649 "aborting.\n", status);
650 return -EINVAL;
651 }
652
653 err = mthca_load_fw(mdev);
654 if (err) {
655 mthca_err(mdev, "Failed to start FW, aborting.\n");
656 goto err_disable;
657 }
658
659 err = mthca_dev_lim(mdev, &dev_lim);
660 if (err) {
661 mthca_err(mdev, "QUERY_DEV_LIM command failed, aborting.\n");
662 goto err_stop_fw;
663 }
664
Leonid Arsh82da7032006-12-10 13:40:17 +0200665 profile = hca_profile;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 profile.num_uar = dev_lim.uar_size / PAGE_SIZE;
667 profile.num_udav = 0;
Roland Dreierec34a922005-08-19 10:59:31 -0700668 if (mdev->mthca_flags & MTHCA_FLAG_SRQ)
669 profile.num_srq = dev_lim.max_srqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 icm_size = mthca_make_profile(mdev, &profile, &dev_lim, &init_hca);
672 if ((int) icm_size < 0) {
673 err = icm_size;
674 goto err_stop_fw;
675 }
676
677 err = mthca_init_icm(mdev, &dev_lim, &init_hca, icm_size);
678 if (err)
679 goto err_stop_fw;
680
681 err = mthca_INIT_HCA(mdev, &init_hca, &status);
682 if (err) {
683 mthca_err(mdev, "INIT_HCA command failed, aborting.\n");
684 goto err_free_icm;
685 }
686 if (status) {
687 mthca_err(mdev, "INIT_HCA returned status 0x%02x, "
688 "aborting.\n", status);
689 err = -EINVAL;
690 goto err_free_icm;
691 }
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return 0;
694
695err_free_icm:
Michael S. Tsirkinaba7a222005-09-30 13:55:50 -0700696 mthca_free_icms(mdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698err_stop_fw:
699 mthca_UNMAP_FA(mdev, &status);
700 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
701
702err_disable:
703 if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
704 mthca_DISABLE_LAM(mdev, &status);
705
706 return err;
707}
708
Michael S. Tsirkin2e8b9812005-08-13 21:19:38 -0700709static void mthca_close_hca(struct mthca_dev *mdev)
710{
711 u8 status;
712
713 mthca_CLOSE_HCA(mdev, 0, &status);
714
715 if (mthca_is_memfree(mdev)) {
Michael S. Tsirkinaba7a222005-09-30 13:55:50 -0700716 mthca_free_icms(mdev);
Michael S. Tsirkin2e8b9812005-08-13 21:19:38 -0700717
718 mthca_UNMAP_FA(mdev, &status);
719 mthca_free_icm(mdev, mdev->fw.arbel.fw_icm);
720
721 if (!(mdev->mthca_flags & MTHCA_FLAG_NO_LAM))
722 mthca_DISABLE_LAM(mdev, &status);
723 } else
724 mthca_SYS_DIS(mdev, &status);
725}
726
Roland Dreierf4f3d0f2006-11-29 15:33:06 -0800727static int mthca_init_hca(struct mthca_dev *mdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Michael S. Tsirkin2e8b9812005-08-13 21:19:38 -0700729 u8 status;
730 int err;
731 struct mthca_adapter adapter;
732
Roland Dreierd10ddbf2005-04-16 15:26:32 -0700733 if (mthca_is_memfree(mdev))
Michael S. Tsirkin2e8b9812005-08-13 21:19:38 -0700734 err = mthca_init_arbel(mdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 else
Michael S. Tsirkin2e8b9812005-08-13 21:19:38 -0700736 err = mthca_init_tavor(mdev);
737
738 if (err)
739 return err;
740
741 err = mthca_QUERY_ADAPTER(mdev, &adapter, &status);
742 if (err) {
743 mthca_err(mdev, "QUERY_ADAPTER command failed, aborting.\n");
744 goto err_close;
745 }
746 if (status) {
747 mthca_err(mdev, "QUERY_ADAPTER returned status 0x%02x, "
748 "aborting.\n", status);
749 err = -EINVAL;
750 goto err_close;
751 }
752
753 mdev->eq_table.inta_pin = adapter.inta_pin;
754 mdev->rev_id = adapter.revision_id;
755 memcpy(mdev->board_id, adapter.board_id, sizeof mdev->board_id);
756
757 return 0;
758
759err_close:
760 mthca_close_hca(mdev);
761 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
Roland Dreierf4f3d0f2006-11-29 15:33:06 -0800764static int mthca_setup_hca(struct mthca_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 int err;
767 u8 status;
768
769 MTHCA_INIT_DOORBELL_LOCK(&dev->doorbell_lock);
770
771 err = mthca_init_uar_table(dev);
772 if (err) {
773 mthca_err(dev, "Failed to initialize "
774 "user access region table, aborting.\n");
775 return err;
776 }
777
778 err = mthca_uar_alloc(dev, &dev->driver_uar);
779 if (err) {
780 mthca_err(dev, "Failed to allocate driver access region, "
781 "aborting.\n");
782 goto err_uar_table_free;
783 }
784
785 dev->kar = ioremap(dev->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
786 if (!dev->kar) {
787 mthca_err(dev, "Couldn't map kernel access region, "
788 "aborting.\n");
789 err = -ENOMEM;
790 goto err_uar_free;
791 }
792
793 err = mthca_init_pd_table(dev);
794 if (err) {
795 mthca_err(dev, "Failed to initialize "
796 "protection domain table, aborting.\n");
797 goto err_kar_unmap;
798 }
799
800 err = mthca_init_mr_table(dev);
801 if (err) {
802 mthca_err(dev, "Failed to initialize "
803 "memory region table, aborting.\n");
804 goto err_pd_table_free;
805 }
806
Roland Dreier99264c12005-07-07 17:57:18 -0700807 err = mthca_pd_alloc(dev, 1, &dev->driver_pd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (err) {
809 mthca_err(dev, "Failed to create driver PD, "
810 "aborting.\n");
811 goto err_mr_table_free;
812 }
813
814 err = mthca_init_eq_table(dev);
815 if (err) {
816 mthca_err(dev, "Failed to initialize "
817 "event queue table, aborting.\n");
818 goto err_pd_free;
819 }
820
821 err = mthca_cmd_use_events(dev);
822 if (err) {
823 mthca_err(dev, "Failed to switch to event-driven "
824 "firmware commands, aborting.\n");
825 goto err_eq_table_free;
826 }
827
828 err = mthca_NOP(dev, &status);
829 if (err || status) {
Roland Dreier4ad81172005-04-16 15:26:21 -0700830 mthca_err(dev, "NOP command failed to generate interrupt (IRQ %d), aborting.\n",
831 dev->mthca_flags & MTHCA_FLAG_MSI_X ?
832 dev->eq_table.eq[MTHCA_EQ_CMD].msi_x_vector :
833 dev->pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (dev->mthca_flags & (MTHCA_FLAG_MSI | MTHCA_FLAG_MSI_X))
835 mthca_err(dev, "Try again with MSI/MSI-X disabled.\n");
836 else
837 mthca_err(dev, "BIOS or ACPI interrupt routing problem?\n");
838
839 goto err_cmd_poll;
840 }
841
842 mthca_dbg(dev, "NOP command IRQ test passed\n");
843
844 err = mthca_init_cq_table(dev);
845 if (err) {
846 mthca_err(dev, "Failed to initialize "
847 "completion queue table, aborting.\n");
848 goto err_cmd_poll;
849 }
850
Roland Dreierec34a922005-08-19 10:59:31 -0700851 err = mthca_init_srq_table(dev);
852 if (err) {
853 mthca_err(dev, "Failed to initialize "
854 "shared receive queue table, aborting.\n");
855 goto err_cq_table_free;
856 }
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 err = mthca_init_qp_table(dev);
859 if (err) {
860 mthca_err(dev, "Failed to initialize "
861 "queue pair table, aborting.\n");
Roland Dreierec34a922005-08-19 10:59:31 -0700862 goto err_srq_table_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
865 err = mthca_init_av_table(dev);
866 if (err) {
867 mthca_err(dev, "Failed to initialize "
868 "address vector table, aborting.\n");
869 goto err_qp_table_free;
870 }
871
872 err = mthca_init_mcg_table(dev);
873 if (err) {
874 mthca_err(dev, "Failed to initialize "
875 "multicast group table, aborting.\n");
876 goto err_av_table_free;
877 }
878
879 return 0;
880
881err_av_table_free:
882 mthca_cleanup_av_table(dev);
883
884err_qp_table_free:
885 mthca_cleanup_qp_table(dev);
886
Roland Dreierec34a922005-08-19 10:59:31 -0700887err_srq_table_free:
888 mthca_cleanup_srq_table(dev);
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890err_cq_table_free:
891 mthca_cleanup_cq_table(dev);
892
893err_cmd_poll:
894 mthca_cmd_use_polling(dev);
895
896err_eq_table_free:
897 mthca_cleanup_eq_table(dev);
898
899err_pd_free:
900 mthca_pd_free(dev, &dev->driver_pd);
901
902err_mr_table_free:
903 mthca_cleanup_mr_table(dev);
904
905err_pd_table_free:
906 mthca_cleanup_pd_table(dev);
907
908err_kar_unmap:
909 iounmap(dev->kar);
910
911err_uar_free:
912 mthca_uar_free(dev, &dev->driver_uar);
913
914err_uar_table_free:
915 mthca_cleanup_uar_table(dev);
916 return err;
917}
918
Roland Dreierf4f3d0f2006-11-29 15:33:06 -0800919static int mthca_request_regions(struct pci_dev *pdev, int ddr_hidden)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 int err;
922
923 /*
924 * We can't just use pci_request_regions() because the MSI-X
925 * table is right in the middle of the first BAR. If we did
926 * pci_request_region and grab all of the first BAR, then
927 * setting up MSI-X would fail, since the PCI core wants to do
928 * request_mem_region on the MSI-X vector table.
929 *
930 * So just request what we need right now, and request any
931 * other regions we need when setting up EQs.
932 */
933 if (!request_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
934 MTHCA_HCR_SIZE, DRV_NAME))
935 return -EBUSY;
936
937 err = pci_request_region(pdev, 2, DRV_NAME);
938 if (err)
939 goto err_bar2_failed;
940
941 if (!ddr_hidden) {
942 err = pci_request_region(pdev, 4, DRV_NAME);
943 if (err)
944 goto err_bar4_failed;
945 }
946
947 return 0;
948
949err_bar4_failed:
950 pci_release_region(pdev, 2);
951
952err_bar2_failed:
953 release_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
954 MTHCA_HCR_SIZE);
955
956 return err;
957}
958
959static void mthca_release_regions(struct pci_dev *pdev,
960 int ddr_hidden)
961{
962 if (!ddr_hidden)
963 pci_release_region(pdev, 4);
964
965 pci_release_region(pdev, 2);
966
967 release_mem_region(pci_resource_start(pdev, 0) + MTHCA_HCR_BASE,
968 MTHCA_HCR_SIZE);
969}
970
Roland Dreierf4f3d0f2006-11-29 15:33:06 -0800971static int mthca_enable_msi_x(struct mthca_dev *mdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 struct msix_entry entries[3];
974 int err;
975
976 entries[0].entry = 0;
977 entries[1].entry = 1;
978 entries[2].entry = 2;
979
980 err = pci_enable_msix(mdev->pdev, entries, ARRAY_SIZE(entries));
981 if (err) {
982 if (err > 0)
983 mthca_info(mdev, "Only %d MSI-X vectors available, "
984 "not using MSI-X\n", err);
985 return err;
986 }
987
988 mdev->eq_table.eq[MTHCA_EQ_COMP ].msi_x_vector = entries[0].vector;
989 mdev->eq_table.eq[MTHCA_EQ_ASYNC].msi_x_vector = entries[1].vector;
990 mdev->eq_table.eq[MTHCA_EQ_CMD ].msi_x_vector = entries[2].vector;
991
992 return 0;
993}
994
Roland Dreier68a3c212005-04-16 15:26:34 -0700995/* Types of supported HCA */
996enum {
997 TAVOR, /* MT23108 */
998 ARBEL_COMPAT, /* MT25208 in Tavor compat mode */
999 ARBEL_NATIVE, /* MT25208 with extended features */
1000 SINAI /* MT25204 */
1001};
1002
1003#define MTHCA_FW_VER(major, minor, subminor) \
1004 (((u64) (major) << 32) | ((u64) (minor) << 16) | (u64) (subminor))
1005
1006static struct {
1007 u64 latest_fw;
Eli Cohen651eaac2006-03-02 12:40:46 -08001008 u32 flags;
Roland Dreier68a3c212005-04-16 15:26:34 -07001009} mthca_hca_table[] = {
Roland Dreier6226bb52006-03-02 13:56:37 -08001010 [TAVOR] = { .latest_fw = MTHCA_FW_VER(3, 4, 0),
Eli Cohen651eaac2006-03-02 12:40:46 -08001011 .flags = 0 },
Michael S. Tsirkin834ac732006-08-22 22:45:06 +03001012 [ARBEL_COMPAT] = { .latest_fw = MTHCA_FW_VER(4, 7, 600),
Eli Cohen651eaac2006-03-02 12:40:46 -08001013 .flags = MTHCA_FLAG_PCIE },
Michael S. Tsirkin834ac732006-08-22 22:45:06 +03001014 [ARBEL_NATIVE] = { .latest_fw = MTHCA_FW_VER(5, 1, 400),
Eli Cohen651eaac2006-03-02 12:40:46 -08001015 .flags = MTHCA_FLAG_MEMFREE |
1016 MTHCA_FLAG_PCIE },
Michael S. Tsirkin834ac732006-08-22 22:45:06 +03001017 [SINAI] = { .latest_fw = MTHCA_FW_VER(1, 1, 0),
Eli Cohen651eaac2006-03-02 12:40:46 -08001018 .flags = MTHCA_FLAG_MEMFREE |
1019 MTHCA_FLAG_PCIE |
1020 MTHCA_FLAG_SINAI_OPT }
Roland Dreier68a3c212005-04-16 15:26:34 -07001021};
1022
Jack Morgensteinb3b30f52006-08-15 21:11:18 +03001023static int __mthca_init_one(struct pci_dev *pdev, int hca_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 int ddr_hidden = 0;
1026 int err;
1027 struct mthca_dev *mdev;
1028
Adrian Bunk982245f2005-07-17 04:22:20 +02001029 printk(KERN_INFO PFX "Initializing %s\n",
1030 pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 err = pci_enable_device(pdev);
1033 if (err) {
1034 dev_err(&pdev->dev, "Cannot enable PCI device, "
1035 "aborting.\n");
1036 return err;
1037 }
1038
1039 /*
1040 * Check for BARs. We expect 0: 1MB, 2: 8MB, 4: DDR (may not
1041 * be present)
1042 */
1043 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
1044 pci_resource_len(pdev, 0) != 1 << 20) {
Bernhard Fischer177214a2005-06-27 14:36:39 -07001045 dev_err(&pdev->dev, "Missing DCS, aborting.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 err = -ENODEV;
1047 goto err_disable_pdev;
1048 }
Michael S. Tsirkincbd29812006-01-30 15:20:35 -08001049 if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
Bernhard Fischer177214a2005-06-27 14:36:39 -07001050 dev_err(&pdev->dev, "Missing UAR, aborting.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 err = -ENODEV;
1052 goto err_disable_pdev;
1053 }
1054 if (!(pci_resource_flags(pdev, 4) & IORESOURCE_MEM))
1055 ddr_hidden = 1;
1056
1057 err = mthca_request_regions(pdev, ddr_hidden);
1058 if (err) {
1059 dev_err(&pdev->dev, "Cannot obtain PCI resources, "
1060 "aborting.\n");
1061 goto err_disable_pdev;
1062 }
1063
1064 pci_set_master(pdev);
1065
1066 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
1067 if (err) {
1068 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask.\n");
1069 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1070 if (err) {
1071 dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting.\n");
1072 goto err_free_res;
1073 }
1074 }
1075 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
1076 if (err) {
1077 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit "
1078 "consistent PCI DMA mask.\n");
1079 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
1080 if (err) {
1081 dev_err(&pdev->dev, "Can't set consistent PCI DMA mask, "
1082 "aborting.\n");
1083 goto err_free_res;
1084 }
1085 }
1086
1087 mdev = (struct mthca_dev *) ib_alloc_device(sizeof *mdev);
1088 if (!mdev) {
1089 dev_err(&pdev->dev, "Device struct alloc failed, "
1090 "aborting.\n");
1091 err = -ENOMEM;
1092 goto err_free_res;
1093 }
1094
Roland Dreier68a3c212005-04-16 15:26:34 -07001095 mdev->pdev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Jack Morgensteinb3b30f52006-08-15 21:11:18 +03001097 mdev->mthca_flags = mthca_hca_table[hca_type].flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (ddr_hidden)
1099 mdev->mthca_flags |= MTHCA_FLAG_DDR_HIDDEN;
1100
1101 /*
1102 * Now reset the HCA before we touch the PCI capabilities or
1103 * attempt a firmware command, since a boot ROM may have left
1104 * the HCA in an undefined state.
1105 */
1106 err = mthca_reset(mdev);
1107 if (err) {
1108 mthca_err(mdev, "Failed to reset HCA, aborting.\n");
1109 goto err_free_dev;
1110 }
1111
1112 if (msi_x && !mthca_enable_msi_x(mdev))
1113 mdev->mthca_flags |= MTHCA_FLAG_MSI_X;
1114 if (msi && !(mdev->mthca_flags & MTHCA_FLAG_MSI_X) &&
1115 !pci_enable_msi(pdev))
1116 mdev->mthca_flags |= MTHCA_FLAG_MSI;
1117
Roland Dreier80fd8232005-06-27 14:36:45 -07001118 if (mthca_cmd_init(mdev)) {
1119 mthca_err(mdev, "Failed to init command interface, aborting.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 goto err_free_dev;
1121 }
1122
1123 err = mthca_tune_pci(mdev);
1124 if (err)
Roland Dreier80fd8232005-06-27 14:36:45 -07001125 goto err_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 err = mthca_init_hca(mdev);
1128 if (err)
Roland Dreier80fd8232005-06-27 14:36:45 -07001129 goto err_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Jack Morgensteinb3b30f52006-08-15 21:11:18 +03001131 if (mdev->fw_ver < mthca_hca_table[hca_type].latest_fw) {
Roland Dreier87cfe322005-11-02 22:59:37 -08001132 mthca_warn(mdev, "HCA FW version %d.%d.%d is old (%d.%d.%d is current).\n",
Roland Dreier68a3c212005-04-16 15:26:34 -07001133 (int) (mdev->fw_ver >> 32), (int) (mdev->fw_ver >> 16) & 0xffff,
1134 (int) (mdev->fw_ver & 0xffff),
Jack Morgensteinb3b30f52006-08-15 21:11:18 +03001135 (int) (mthca_hca_table[hca_type].latest_fw >> 32),
1136 (int) (mthca_hca_table[hca_type].latest_fw >> 16) & 0xffff,
1137 (int) (mthca_hca_table[hca_type].latest_fw & 0xffff));
Roland Dreier68a3c212005-04-16 15:26:34 -07001138 mthca_warn(mdev, "If you have problems, try updating your HCA FW.\n");
1139 }
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 err = mthca_setup_hca(mdev);
1142 if (err)
1143 goto err_close;
1144
1145 err = mthca_register_device(mdev);
1146 if (err)
1147 goto err_cleanup;
1148
1149 err = mthca_create_agents(mdev);
1150 if (err)
1151 goto err_unregister;
1152
1153 pci_set_drvdata(pdev, mdev);
Jack Morgensteinb3b30f52006-08-15 21:11:18 +03001154 mdev->hca_type = hca_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 return 0;
1157
1158err_unregister:
1159 mthca_unregister_device(mdev);
1160
1161err_cleanup:
1162 mthca_cleanup_mcg_table(mdev);
1163 mthca_cleanup_av_table(mdev);
1164 mthca_cleanup_qp_table(mdev);
Roland Dreierec34a922005-08-19 10:59:31 -07001165 mthca_cleanup_srq_table(mdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 mthca_cleanup_cq_table(mdev);
1167 mthca_cmd_use_polling(mdev);
1168 mthca_cleanup_eq_table(mdev);
1169
1170 mthca_pd_free(mdev, &mdev->driver_pd);
1171
1172 mthca_cleanup_mr_table(mdev);
1173 mthca_cleanup_pd_table(mdev);
1174 mthca_cleanup_uar_table(mdev);
1175
1176err_close:
1177 mthca_close_hca(mdev);
1178
Roland Dreier80fd8232005-06-27 14:36:45 -07001179err_cmd:
1180 mthca_cmd_cleanup(mdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182err_free_dev:
1183 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1184 pci_disable_msix(pdev);
1185 if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1186 pci_disable_msi(pdev);
1187
1188 ib_dealloc_device(&mdev->ib_dev);
1189
1190err_free_res:
1191 mthca_release_regions(pdev, ddr_hidden);
1192
1193err_disable_pdev:
1194 pci_disable_device(pdev);
1195 pci_set_drvdata(pdev, NULL);
1196 return err;
1197}
1198
Jack Morgensteinb3b30f52006-08-15 21:11:18 +03001199static void __mthca_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 struct mthca_dev *mdev = pci_get_drvdata(pdev);
1202 u8 status;
1203 int p;
1204
1205 if (mdev) {
1206 mthca_free_agents(mdev);
1207 mthca_unregister_device(mdev);
1208
1209 for (p = 1; p <= mdev->limits.num_ports; ++p)
1210 mthca_CLOSE_IB(mdev, p, &status);
1211
1212 mthca_cleanup_mcg_table(mdev);
1213 mthca_cleanup_av_table(mdev);
1214 mthca_cleanup_qp_table(mdev);
Roland Dreierec34a922005-08-19 10:59:31 -07001215 mthca_cleanup_srq_table(mdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 mthca_cleanup_cq_table(mdev);
1217 mthca_cmd_use_polling(mdev);
1218 mthca_cleanup_eq_table(mdev);
1219
1220 mthca_pd_free(mdev, &mdev->driver_pd);
1221
1222 mthca_cleanup_mr_table(mdev);
1223 mthca_cleanup_pd_table(mdev);
1224
1225 iounmap(mdev->kar);
1226 mthca_uar_free(mdev, &mdev->driver_uar);
1227 mthca_cleanup_uar_table(mdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 mthca_close_hca(mdev);
Roland Dreier80fd8232005-06-27 14:36:45 -07001229 mthca_cmd_cleanup(mdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 if (mdev->mthca_flags & MTHCA_FLAG_MSI_X)
1232 pci_disable_msix(pdev);
1233 if (mdev->mthca_flags & MTHCA_FLAG_MSI)
1234 pci_disable_msi(pdev);
1235
1236 ib_dealloc_device(&mdev->ib_dev);
1237 mthca_release_regions(pdev, mdev->mthca_flags &
1238 MTHCA_FLAG_DDR_HIDDEN);
1239 pci_disable_device(pdev);
1240 pci_set_drvdata(pdev, NULL);
1241 }
1242}
1243
Jack Morgensteinb3b30f52006-08-15 21:11:18 +03001244int __mthca_restart_one(struct pci_dev *pdev)
1245{
1246 struct mthca_dev *mdev;
1247
1248 mdev = pci_get_drvdata(pdev);
1249 if (!mdev)
1250 return -ENODEV;
1251 __mthca_remove_one(pdev);
1252 return __mthca_init_one(pdev, mdev->hca_type);
1253}
1254
1255static int __devinit mthca_init_one(struct pci_dev *pdev,
Roland Dreierf4f3d0f2006-11-29 15:33:06 -08001256 const struct pci_device_id *id)
Jack Morgensteinb3b30f52006-08-15 21:11:18 +03001257{
1258 static int mthca_version_printed = 0;
1259 int ret;
1260
1261 mutex_lock(&mthca_device_mutex);
1262
1263 if (!mthca_version_printed) {
1264 printk(KERN_INFO "%s", mthca_version);
1265 ++mthca_version_printed;
1266 }
1267
1268 if (id->driver_data >= ARRAY_SIZE(mthca_hca_table)) {
1269 printk(KERN_ERR PFX "%s has invalid driver data %lx\n",
1270 pci_name(pdev), id->driver_data);
1271 mutex_unlock(&mthca_device_mutex);
1272 return -ENODEV;
1273 }
1274
1275 ret = __mthca_init_one(pdev, id->driver_data);
1276
1277 mutex_unlock(&mthca_device_mutex);
1278
1279 return ret;
1280}
1281
1282static void __devexit mthca_remove_one(struct pci_dev *pdev)
1283{
1284 mutex_lock(&mthca_device_mutex);
1285 __mthca_remove_one(pdev);
1286 mutex_unlock(&mthca_device_mutex);
1287}
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289static struct pci_device_id mthca_pci_table[] = {
1290 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_TAVOR),
1291 .driver_data = TAVOR },
1292 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_TAVOR),
1293 .driver_data = TAVOR },
1294 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
1295 .driver_data = ARBEL_COMPAT },
1296 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT),
1297 .driver_data = ARBEL_COMPAT },
1298 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_ARBEL),
1299 .driver_data = ARBEL_NATIVE },
1300 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_ARBEL),
1301 .driver_data = ARBEL_NATIVE },
Roland Dreier68a3c212005-04-16 15:26:34 -07001302 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI),
1303 .driver_data = SINAI },
1304 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI),
1305 .driver_data = SINAI },
1306 { PCI_DEVICE(PCI_VENDOR_ID_MELLANOX, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
1307 .driver_data = SINAI },
1308 { PCI_DEVICE(PCI_VENDOR_ID_TOPSPIN, PCI_DEVICE_ID_MELLANOX_SINAI_OLD),
1309 .driver_data = SINAI },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 { 0, }
1311};
1312
1313MODULE_DEVICE_TABLE(pci, mthca_pci_table);
1314
1315static struct pci_driver mthca_driver = {
Bernhard Fischer177214a2005-06-27 14:36:39 -07001316 .name = DRV_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 .id_table = mthca_pci_table,
1318 .probe = mthca_init_one,
1319 .remove = __devexit_p(mthca_remove_one)
1320};
1321
Leonid Arsh82da7032006-12-10 13:40:17 +02001322static void __init __mthca_check_profile_val(const char *name, int *pval,
1323 int pval_default)
1324{
1325 /* value must be positive and power of 2 */
1326 int old_pval = *pval;
1327
1328 if (old_pval <= 0)
1329 *pval = pval_default;
1330 else
1331 *pval = roundup_pow_of_two(old_pval);
1332
1333 if (old_pval != *pval) {
1334 printk(KERN_WARNING PFX "Invalid value %d for %s in module parameter.\n",
1335 old_pval, name);
1336 printk(KERN_WARNING PFX "Corrected %s to %d.\n", name, *pval);
1337 }
1338}
1339
1340#define mthca_check_profile_val(name, default) \
1341 __mthca_check_profile_val(#name, &hca_profile.name, default)
1342
1343static void __init mthca_validate_profile(void)
1344{
1345 mthca_check_profile_val(num_qp, MTHCA_DEFAULT_NUM_QP);
1346 mthca_check_profile_val(rdb_per_qp, MTHCA_DEFAULT_RDB_PER_QP);
1347 mthca_check_profile_val(num_cq, MTHCA_DEFAULT_NUM_CQ);
1348 mthca_check_profile_val(num_mcg, MTHCA_DEFAULT_NUM_MCG);
1349 mthca_check_profile_val(num_mpt, MTHCA_DEFAULT_NUM_MPT);
1350 mthca_check_profile_val(num_mtt, MTHCA_DEFAULT_NUM_MTT);
1351 mthca_check_profile_val(num_udav, MTHCA_DEFAULT_NUM_UDAV);
1352 mthca_check_profile_val(fmr_reserved_mtts, MTHCA_DEFAULT_NUM_RESERVED_MTTS);
1353
1354 if (hca_profile.fmr_reserved_mtts >= hca_profile.num_mtt) {
1355 printk(KERN_WARNING PFX "Invalid fmr_reserved_mtts module parameter %d.\n",
1356 hca_profile.fmr_reserved_mtts);
1357 printk(KERN_WARNING PFX "(Must be smaller than num_mtt %d)\n",
1358 hca_profile.num_mtt);
1359 hca_profile.fmr_reserved_mtts = hca_profile.num_mtt / 2;
1360 printk(KERN_WARNING PFX "Corrected fmr_reserved_mtts to %d.\n",
1361 hca_profile.fmr_reserved_mtts);
1362 }
1363}
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365static int __init mthca_init(void)
1366{
1367 int ret;
1368
Leonid Arsh82da7032006-12-10 13:40:17 +02001369 mthca_validate_profile();
1370
Jack Morgensteinb3b30f52006-08-15 21:11:18 +03001371 ret = mthca_catas_init();
1372 if (ret)
1373 return ret;
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 ret = pci_register_driver(&mthca_driver);
Jack Morgensteinb3b30f52006-08-15 21:11:18 +03001376 if (ret < 0) {
1377 mthca_catas_cleanup();
1378 return ret;
1379 }
1380
1381 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
1383
1384static void __exit mthca_cleanup(void)
1385{
1386 pci_unregister_driver(&mthca_driver);
Jack Morgensteinb3b30f52006-08-15 21:11:18 +03001387 mthca_catas_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
1390module_init(mthca_init);
1391module_exit(mthca_cleanup);