blob: a6aa49fc1d68a6654d449bcc6fa90a7ad8f40f23 [file] [log] [blame]
Roland Dreier225c7b12007-05-08 18:00:38 -07001/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
5 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
6 *
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
12 *
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
16 *
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
20 *
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
34 */
35
36#include <linux/module.h>
37#include <linux/init.h>
38#include <linux/errno.h>
39#include <linux/pci.h>
40#include <linux/dma-mapping.h>
41
42#include <linux/mlx4/device.h>
43#include <linux/mlx4/doorbell.h>
44
45#include "mlx4.h"
46#include "fw.h"
47#include "icm.h"
48
49MODULE_AUTHOR("Roland Dreier");
50MODULE_DESCRIPTION("Mellanox ConnectX HCA low-level driver");
51MODULE_LICENSE("Dual BSD/GPL");
52MODULE_VERSION(DRV_VERSION);
53
54#ifdef CONFIG_MLX4_DEBUG
55
56int mlx4_debug_level = 0;
57module_param_named(debug_level, mlx4_debug_level, int, 0644);
58MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
59
60#endif /* CONFIG_MLX4_DEBUG */
61
62#ifdef CONFIG_PCI_MSI
63
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +030064static int msi_x = 1;
Roland Dreier225c7b12007-05-08 18:00:38 -070065module_param(msi_x, int, 0444);
66MODULE_PARM_DESC(msi_x, "attempt to use MSI-X if nonzero");
67
68#else /* CONFIG_PCI_MSI */
69
70#define msi_x (0)
71
72#endif /* CONFIG_PCI_MSI */
73
Roland Dreierf33afc22008-02-04 20:20:44 -080074static char mlx4_version[] __devinitdata =
Roland Dreier225c7b12007-05-08 18:00:38 -070075 DRV_NAME ": Mellanox ConnectX core driver v"
76 DRV_VERSION " (" DRV_RELDATE ")\n";
77
78static struct mlx4_profile default_profile = {
Jack Morgenstein9b1f3852008-04-16 21:09:32 -070079 .num_qp = 1 << 17,
Roland Dreier225c7b12007-05-08 18:00:38 -070080 .num_srq = 1 << 16,
Jack Morgensteinc9f2ba52007-07-17 13:11:43 +030081 .rdmarc_per_qp = 1 << 4,
Roland Dreier225c7b12007-05-08 18:00:38 -070082 .num_cq = 1 << 16,
83 .num_mcg = 1 << 13,
84 .num_mpt = 1 << 17,
85 .num_mtt = 1 << 20,
86};
87
Roland Dreier3d73c282007-10-10 15:43:54 -070088static int mlx4_dev_cap(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap)
Roland Dreier225c7b12007-05-08 18:00:38 -070089{
90 int err;
Roland Dreier5ae2a7a2007-06-18 08:15:02 -070091 int i;
Roland Dreier225c7b12007-05-08 18:00:38 -070092
93 err = mlx4_QUERY_DEV_CAP(dev, dev_cap);
94 if (err) {
95 mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
96 return err;
97 }
98
99 if (dev_cap->min_page_sz > PAGE_SIZE) {
100 mlx4_err(dev, "HCA minimum page size of %d bigger than "
101 "kernel PAGE_SIZE of %ld, aborting.\n",
102 dev_cap->min_page_sz, PAGE_SIZE);
103 return -ENODEV;
104 }
105 if (dev_cap->num_ports > MLX4_MAX_PORTS) {
106 mlx4_err(dev, "HCA has %d ports, but we only support %d, "
107 "aborting.\n",
108 dev_cap->num_ports, MLX4_MAX_PORTS);
109 return -ENODEV;
110 }
111
112 if (dev_cap->uar_size > pci_resource_len(dev->pdev, 2)) {
113 mlx4_err(dev, "HCA reported UAR size of 0x%x bigger than "
114 "PCI resource 2 size of 0x%llx, aborting.\n",
115 dev_cap->uar_size,
116 (unsigned long long) pci_resource_len(dev->pdev, 2));
117 return -ENODEV;
118 }
119
120 dev->caps.num_ports = dev_cap->num_ports;
Roland Dreier5ae2a7a2007-06-18 08:15:02 -0700121 for (i = 1; i <= dev->caps.num_ports; ++i) {
122 dev->caps.vl_cap[i] = dev_cap->max_vl[i];
123 dev->caps.mtu_cap[i] = dev_cap->max_mtu[i];
124 dev->caps.gid_table_len[i] = dev_cap->max_gids[i];
125 dev->caps.pkey_table_len[i] = dev_cap->max_pkeys[i];
126 dev->caps.port_width_cap[i] = dev_cap->max_port_width[i];
127 }
128
Roland Dreier225c7b12007-05-08 18:00:38 -0700129 dev->caps.num_uars = dev_cap->uar_size / PAGE_SIZE;
Roland Dreier225c7b12007-05-08 18:00:38 -0700130 dev->caps.local_ca_ack_delay = dev_cap->local_ca_ack_delay;
131 dev->caps.bf_reg_size = dev_cap->bf_reg_size;
132 dev->caps.bf_regs_per_page = dev_cap->bf_regs_per_page;
133 dev->caps.max_sq_sg = dev_cap->max_sq_sg;
134 dev->caps.max_rq_sg = dev_cap->max_rq_sg;
135 dev->caps.max_wqes = dev_cap->max_qp_sz;
136 dev->caps.max_qp_init_rdma = dev_cap->max_requester_per_qp;
137 dev->caps.reserved_qps = dev_cap->reserved_qps;
138 dev->caps.max_srq_wqes = dev_cap->max_srq_sz;
139 dev->caps.max_srq_sge = dev_cap->max_rq_sg - 1;
140 dev->caps.reserved_srqs = dev_cap->reserved_srqs;
141 dev->caps.max_sq_desc_sz = dev_cap->max_sq_desc_sz;
142 dev->caps.max_rq_desc_sz = dev_cap->max_rq_desc_sz;
143 dev->caps.num_qp_per_mgm = MLX4_QP_PER_MGM;
144 /*
145 * Subtract 1 from the limit because we need to allocate a
146 * spare CQE so the HCA HW can tell the difference between an
147 * empty CQ and a full CQ.
148 */
149 dev->caps.max_cqes = dev_cap->max_cq_sz - 1;
150 dev->caps.reserved_cqs = dev_cap->reserved_cqs;
151 dev->caps.reserved_eqs = dev_cap->reserved_eqs;
Roland Dreier121964e2007-10-09 19:59:16 -0700152 dev->caps.reserved_mtts = DIV_ROUND_UP(dev_cap->reserved_mtts,
153 MLX4_MTT_ENTRY_PER_SEG);
Roland Dreier225c7b12007-05-08 18:00:38 -0700154 dev->caps.reserved_mrws = dev_cap->reserved_mrws;
155 dev->caps.reserved_uars = dev_cap->reserved_uars;
156 dev->caps.reserved_pds = dev_cap->reserved_pds;
Roland Dreier225c7b12007-05-08 18:00:38 -0700157 dev->caps.mtt_entry_sz = MLX4_MTT_ENTRY_PER_SEG * dev_cap->mtt_entry_sz;
Dotan Barak149983af2007-06-26 15:55:28 +0300158 dev->caps.max_msg_sz = dev_cap->max_msg_sz;
Roland Dreier225c7b12007-05-08 18:00:38 -0700159 dev->caps.page_size_cap = ~(u32) (dev_cap->min_page_sz - 1);
160 dev->caps.flags = dev_cap->flags;
161 dev->caps.stat_rate_support = dev_cap->stat_rate_support;
Eli Cohenb832be12008-04-16 21:09:27 -0700162 dev->caps.max_gso_sz = dev_cap->max_gso_sz;
Roland Dreier225c7b12007-05-08 18:00:38 -0700163
164 return 0;
165}
166
Roland Dreiere8f9b2e2008-02-04 20:20:41 -0800167static int mlx4_load_fw(struct mlx4_dev *dev)
Roland Dreier225c7b12007-05-08 18:00:38 -0700168{
169 struct mlx4_priv *priv = mlx4_priv(dev);
170 int err;
171
172 priv->fw.fw_icm = mlx4_alloc_icm(dev, priv->fw.fw_pages,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300173 GFP_HIGHUSER | __GFP_NOWARN, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700174 if (!priv->fw.fw_icm) {
175 mlx4_err(dev, "Couldn't allocate FW area, aborting.\n");
176 return -ENOMEM;
177 }
178
179 err = mlx4_MAP_FA(dev, priv->fw.fw_icm);
180 if (err) {
181 mlx4_err(dev, "MAP_FA command failed, aborting.\n");
182 goto err_free;
183 }
184
185 err = mlx4_RUN_FW(dev);
186 if (err) {
187 mlx4_err(dev, "RUN_FW command failed, aborting.\n");
188 goto err_unmap_fa;
189 }
190
191 return 0;
192
193err_unmap_fa:
194 mlx4_UNMAP_FA(dev);
195
196err_free:
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300197 mlx4_free_icm(dev, priv->fw.fw_icm, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700198 return err;
199}
200
Roland Dreiere8f9b2e2008-02-04 20:20:41 -0800201static int mlx4_init_cmpt_table(struct mlx4_dev *dev, u64 cmpt_base,
202 int cmpt_entry_sz)
Roland Dreier225c7b12007-05-08 18:00:38 -0700203{
204 struct mlx4_priv *priv = mlx4_priv(dev);
205 int err;
206
207 err = mlx4_init_icm_table(dev, &priv->qp_table.cmpt_table,
208 cmpt_base +
209 ((u64) (MLX4_CMPT_TYPE_QP *
210 cmpt_entry_sz) << MLX4_CMPT_SHIFT),
211 cmpt_entry_sz, dev->caps.num_qps,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300212 dev->caps.reserved_qps, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700213 if (err)
214 goto err;
215
216 err = mlx4_init_icm_table(dev, &priv->srq_table.cmpt_table,
217 cmpt_base +
218 ((u64) (MLX4_CMPT_TYPE_SRQ *
219 cmpt_entry_sz) << MLX4_CMPT_SHIFT),
220 cmpt_entry_sz, dev->caps.num_srqs,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300221 dev->caps.reserved_srqs, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700222 if (err)
223 goto err_qp;
224
225 err = mlx4_init_icm_table(dev, &priv->cq_table.cmpt_table,
226 cmpt_base +
227 ((u64) (MLX4_CMPT_TYPE_CQ *
228 cmpt_entry_sz) << MLX4_CMPT_SHIFT),
229 cmpt_entry_sz, dev->caps.num_cqs,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300230 dev->caps.reserved_cqs, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700231 if (err)
232 goto err_srq;
233
234 err = mlx4_init_icm_table(dev, &priv->eq_table.cmpt_table,
235 cmpt_base +
236 ((u64) (MLX4_CMPT_TYPE_EQ *
237 cmpt_entry_sz) << MLX4_CMPT_SHIFT),
238 cmpt_entry_sz,
239 roundup_pow_of_two(MLX4_NUM_EQ +
240 dev->caps.reserved_eqs),
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300241 MLX4_NUM_EQ + dev->caps.reserved_eqs, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700242 if (err)
243 goto err_cq;
244
245 return 0;
246
247err_cq:
248 mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
249
250err_srq:
251 mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
252
253err_qp:
254 mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
255
256err:
257 return err;
258}
259
Roland Dreier3d73c282007-10-10 15:43:54 -0700260static int mlx4_init_icm(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap,
261 struct mlx4_init_hca_param *init_hca, u64 icm_size)
Roland Dreier225c7b12007-05-08 18:00:38 -0700262{
263 struct mlx4_priv *priv = mlx4_priv(dev);
264 u64 aux_pages;
265 int err;
266
267 err = mlx4_SET_ICM_SIZE(dev, icm_size, &aux_pages);
268 if (err) {
269 mlx4_err(dev, "SET_ICM_SIZE command failed, aborting.\n");
270 return err;
271 }
272
273 mlx4_dbg(dev, "%lld KB of HCA context requires %lld KB aux memory.\n",
274 (unsigned long long) icm_size >> 10,
275 (unsigned long long) aux_pages << 2);
276
277 priv->fw.aux_icm = mlx4_alloc_icm(dev, aux_pages,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300278 GFP_HIGHUSER | __GFP_NOWARN, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700279 if (!priv->fw.aux_icm) {
280 mlx4_err(dev, "Couldn't allocate aux memory, aborting.\n");
281 return -ENOMEM;
282 }
283
284 err = mlx4_MAP_ICM_AUX(dev, priv->fw.aux_icm);
285 if (err) {
286 mlx4_err(dev, "MAP_ICM_AUX command failed, aborting.\n");
287 goto err_free_aux;
288 }
289
290 err = mlx4_init_cmpt_table(dev, init_hca->cmpt_base, dev_cap->cmpt_entry_sz);
291 if (err) {
292 mlx4_err(dev, "Failed to map cMPT context memory, aborting.\n");
293 goto err_unmap_aux;
294 }
295
296 err = mlx4_map_eq_icm(dev, init_hca->eqc_base);
297 if (err) {
298 mlx4_err(dev, "Failed to map EQ context memory, aborting.\n");
299 goto err_unmap_cmpt;
300 }
301
Jack Morgensteind7bb58f2007-08-01 12:28:53 +0300302 /*
303 * Reserved MTT entries must be aligned up to a cacheline
304 * boundary, since the FW will write to them, while the driver
305 * writes to all other MTT entries. (The variable
306 * dev->caps.mtt_entry_sz below is really the MTT segment
307 * size, not the raw entry size)
308 */
309 dev->caps.reserved_mtts =
310 ALIGN(dev->caps.reserved_mtts * dev->caps.mtt_entry_sz,
311 dma_get_cache_alignment()) / dev->caps.mtt_entry_sz;
312
Roland Dreier225c7b12007-05-08 18:00:38 -0700313 err = mlx4_init_icm_table(dev, &priv->mr_table.mtt_table,
314 init_hca->mtt_base,
315 dev->caps.mtt_entry_sz,
316 dev->caps.num_mtt_segs,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300317 dev->caps.reserved_mtts, 1, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700318 if (err) {
319 mlx4_err(dev, "Failed to map MTT context memory, aborting.\n");
320 goto err_unmap_eq;
321 }
322
323 err = mlx4_init_icm_table(dev, &priv->mr_table.dmpt_table,
324 init_hca->dmpt_base,
325 dev_cap->dmpt_entry_sz,
326 dev->caps.num_mpts,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300327 dev->caps.reserved_mrws, 1, 1);
Roland Dreier225c7b12007-05-08 18:00:38 -0700328 if (err) {
329 mlx4_err(dev, "Failed to map dMPT context memory, aborting.\n");
330 goto err_unmap_mtt;
331 }
332
333 err = mlx4_init_icm_table(dev, &priv->qp_table.qp_table,
334 init_hca->qpc_base,
335 dev_cap->qpc_entry_sz,
336 dev->caps.num_qps,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300337 dev->caps.reserved_qps, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700338 if (err) {
339 mlx4_err(dev, "Failed to map QP context memory, aborting.\n");
340 goto err_unmap_dmpt;
341 }
342
343 err = mlx4_init_icm_table(dev, &priv->qp_table.auxc_table,
344 init_hca->auxc_base,
345 dev_cap->aux_entry_sz,
346 dev->caps.num_qps,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300347 dev->caps.reserved_qps, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700348 if (err) {
349 mlx4_err(dev, "Failed to map AUXC context memory, aborting.\n");
350 goto err_unmap_qp;
351 }
352
353 err = mlx4_init_icm_table(dev, &priv->qp_table.altc_table,
354 init_hca->altc_base,
355 dev_cap->altc_entry_sz,
356 dev->caps.num_qps,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300357 dev->caps.reserved_qps, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700358 if (err) {
359 mlx4_err(dev, "Failed to map ALTC context memory, aborting.\n");
360 goto err_unmap_auxc;
361 }
362
363 err = mlx4_init_icm_table(dev, &priv->qp_table.rdmarc_table,
364 init_hca->rdmarc_base,
365 dev_cap->rdmarc_entry_sz << priv->qp_table.rdmarc_shift,
366 dev->caps.num_qps,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300367 dev->caps.reserved_qps, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700368 if (err) {
369 mlx4_err(dev, "Failed to map RDMARC context memory, aborting\n");
370 goto err_unmap_altc;
371 }
372
373 err = mlx4_init_icm_table(dev, &priv->cq_table.table,
374 init_hca->cqc_base,
375 dev_cap->cqc_entry_sz,
376 dev->caps.num_cqs,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300377 dev->caps.reserved_cqs, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700378 if (err) {
379 mlx4_err(dev, "Failed to map CQ context memory, aborting.\n");
380 goto err_unmap_rdmarc;
381 }
382
383 err = mlx4_init_icm_table(dev, &priv->srq_table.table,
384 init_hca->srqc_base,
385 dev_cap->srq_entry_sz,
386 dev->caps.num_srqs,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300387 dev->caps.reserved_srqs, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700388 if (err) {
389 mlx4_err(dev, "Failed to map SRQ context memory, aborting.\n");
390 goto err_unmap_cq;
391 }
392
393 /*
394 * It's not strictly required, but for simplicity just map the
395 * whole multicast group table now. The table isn't very big
396 * and it's a lot easier than trying to track ref counts.
397 */
398 err = mlx4_init_icm_table(dev, &priv->mcg_table.table,
399 init_hca->mc_base, MLX4_MGM_ENTRY_SIZE,
400 dev->caps.num_mgms + dev->caps.num_amgms,
401 dev->caps.num_mgms + dev->caps.num_amgms,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300402 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700403 if (err) {
404 mlx4_err(dev, "Failed to map MCG context memory, aborting.\n");
405 goto err_unmap_srq;
406 }
407
408 return 0;
409
410err_unmap_srq:
411 mlx4_cleanup_icm_table(dev, &priv->srq_table.table);
412
413err_unmap_cq:
414 mlx4_cleanup_icm_table(dev, &priv->cq_table.table);
415
416err_unmap_rdmarc:
417 mlx4_cleanup_icm_table(dev, &priv->qp_table.rdmarc_table);
418
419err_unmap_altc:
420 mlx4_cleanup_icm_table(dev, &priv->qp_table.altc_table);
421
422err_unmap_auxc:
423 mlx4_cleanup_icm_table(dev, &priv->qp_table.auxc_table);
424
425err_unmap_qp:
426 mlx4_cleanup_icm_table(dev, &priv->qp_table.qp_table);
427
428err_unmap_dmpt:
429 mlx4_cleanup_icm_table(dev, &priv->mr_table.dmpt_table);
430
431err_unmap_mtt:
432 mlx4_cleanup_icm_table(dev, &priv->mr_table.mtt_table);
433
434err_unmap_eq:
435 mlx4_unmap_eq_icm(dev);
436
437err_unmap_cmpt:
438 mlx4_cleanup_icm_table(dev, &priv->eq_table.cmpt_table);
439 mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
440 mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
441 mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
442
443err_unmap_aux:
444 mlx4_UNMAP_ICM_AUX(dev);
445
446err_free_aux:
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300447 mlx4_free_icm(dev, priv->fw.aux_icm, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700448
449 return err;
450}
451
452static void mlx4_free_icms(struct mlx4_dev *dev)
453{
454 struct mlx4_priv *priv = mlx4_priv(dev);
455
456 mlx4_cleanup_icm_table(dev, &priv->mcg_table.table);
457 mlx4_cleanup_icm_table(dev, &priv->srq_table.table);
458 mlx4_cleanup_icm_table(dev, &priv->cq_table.table);
459 mlx4_cleanup_icm_table(dev, &priv->qp_table.rdmarc_table);
460 mlx4_cleanup_icm_table(dev, &priv->qp_table.altc_table);
461 mlx4_cleanup_icm_table(dev, &priv->qp_table.auxc_table);
462 mlx4_cleanup_icm_table(dev, &priv->qp_table.qp_table);
463 mlx4_cleanup_icm_table(dev, &priv->mr_table.dmpt_table);
464 mlx4_cleanup_icm_table(dev, &priv->mr_table.mtt_table);
465 mlx4_cleanup_icm_table(dev, &priv->eq_table.cmpt_table);
466 mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
467 mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
468 mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
469 mlx4_unmap_eq_icm(dev);
470
471 mlx4_UNMAP_ICM_AUX(dev);
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300472 mlx4_free_icm(dev, priv->fw.aux_icm, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700473}
474
475static void mlx4_close_hca(struct mlx4_dev *dev)
476{
477 mlx4_CLOSE_HCA(dev, 0);
478 mlx4_free_icms(dev);
479 mlx4_UNMAP_FA(dev);
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300480 mlx4_free_icm(dev, mlx4_priv(dev)->fw.fw_icm, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700481}
482
Roland Dreier3d73c282007-10-10 15:43:54 -0700483static int mlx4_init_hca(struct mlx4_dev *dev)
Roland Dreier225c7b12007-05-08 18:00:38 -0700484{
485 struct mlx4_priv *priv = mlx4_priv(dev);
486 struct mlx4_adapter adapter;
487 struct mlx4_dev_cap dev_cap;
488 struct mlx4_profile profile;
489 struct mlx4_init_hca_param init_hca;
490 u64 icm_size;
491 int err;
492
493 err = mlx4_QUERY_FW(dev);
494 if (err) {
495 mlx4_err(dev, "QUERY_FW command failed, aborting.\n");
496 return err;
497 }
498
499 err = mlx4_load_fw(dev);
500 if (err) {
501 mlx4_err(dev, "Failed to start FW, aborting.\n");
502 return err;
503 }
504
505 err = mlx4_dev_cap(dev, &dev_cap);
506 if (err) {
507 mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
508 goto err_stop_fw;
509 }
510
511 profile = default_profile;
512
513 icm_size = mlx4_make_profile(dev, &profile, &dev_cap, &init_hca);
514 if ((long long) icm_size < 0) {
515 err = icm_size;
516 goto err_stop_fw;
517 }
518
519 init_hca.log_uar_sz = ilog2(dev->caps.num_uars);
520
521 err = mlx4_init_icm(dev, &dev_cap, &init_hca, icm_size);
522 if (err)
523 goto err_stop_fw;
524
525 err = mlx4_INIT_HCA(dev, &init_hca);
526 if (err) {
527 mlx4_err(dev, "INIT_HCA command failed, aborting.\n");
528 goto err_free_icm;
529 }
530
531 err = mlx4_QUERY_ADAPTER(dev, &adapter);
532 if (err) {
533 mlx4_err(dev, "QUERY_ADAPTER command failed, aborting.\n");
534 goto err_close;
535 }
536
537 priv->eq_table.inta_pin = adapter.inta_pin;
Jack Morgensteincd9281d2007-09-18 09:14:18 +0200538 memcpy(dev->board_id, adapter.board_id, sizeof dev->board_id);
Roland Dreier225c7b12007-05-08 18:00:38 -0700539
540 return 0;
541
542err_close:
543 mlx4_close_hca(dev);
544
545err_free_icm:
546 mlx4_free_icms(dev);
547
548err_stop_fw:
549 mlx4_UNMAP_FA(dev);
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300550 mlx4_free_icm(dev, priv->fw.fw_icm, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700551
552 return err;
553}
554
Roland Dreier3d73c282007-10-10 15:43:54 -0700555static int mlx4_setup_hca(struct mlx4_dev *dev)
Roland Dreier225c7b12007-05-08 18:00:38 -0700556{
557 struct mlx4_priv *priv = mlx4_priv(dev);
558 int err;
559
Roland Dreier225c7b12007-05-08 18:00:38 -0700560 err = mlx4_init_uar_table(dev);
561 if (err) {
562 mlx4_err(dev, "Failed to initialize "
563 "user access region table, aborting.\n");
564 return err;
565 }
566
567 err = mlx4_uar_alloc(dev, &priv->driver_uar);
568 if (err) {
569 mlx4_err(dev, "Failed to allocate driver access region, "
570 "aborting.\n");
571 goto err_uar_table_free;
572 }
573
574 priv->kar = ioremap(priv->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
575 if (!priv->kar) {
576 mlx4_err(dev, "Couldn't map kernel access region, "
577 "aborting.\n");
578 err = -ENOMEM;
579 goto err_uar_free;
580 }
581
582 err = mlx4_init_pd_table(dev);
583 if (err) {
584 mlx4_err(dev, "Failed to initialize "
585 "protection domain table, aborting.\n");
586 goto err_kar_unmap;
587 }
588
589 err = mlx4_init_mr_table(dev);
590 if (err) {
591 mlx4_err(dev, "Failed to initialize "
592 "memory region table, aborting.\n");
593 goto err_pd_table_free;
594 }
595
Roland Dreier225c7b12007-05-08 18:00:38 -0700596 err = mlx4_init_eq_table(dev);
597 if (err) {
598 mlx4_err(dev, "Failed to initialize "
599 "event queue table, aborting.\n");
Jack Morgensteinee49bd92007-07-12 17:50:45 +0300600 goto err_mr_table_free;
Roland Dreier225c7b12007-05-08 18:00:38 -0700601 }
602
603 err = mlx4_cmd_use_events(dev);
604 if (err) {
605 mlx4_err(dev, "Failed to switch to event-driven "
606 "firmware commands, aborting.\n");
607 goto err_eq_table_free;
608 }
609
610 err = mlx4_NOP(dev);
611 if (err) {
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +0300612 if (dev->flags & MLX4_FLAG_MSI_X) {
613 mlx4_warn(dev, "NOP command failed to generate MSI-X "
614 "interrupt IRQ %d).\n",
615 priv->eq_table.eq[MLX4_EQ_ASYNC].irq);
616 mlx4_warn(dev, "Trying again without MSI-X.\n");
617 } else {
618 mlx4_err(dev, "NOP command failed to generate interrupt "
619 "(IRQ %d), aborting.\n",
620 priv->eq_table.eq[MLX4_EQ_ASYNC].irq);
Roland Dreier225c7b12007-05-08 18:00:38 -0700621 mlx4_err(dev, "BIOS or ACPI interrupt routing problem?\n");
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +0300622 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700623
624 goto err_cmd_poll;
625 }
626
627 mlx4_dbg(dev, "NOP command IRQ test passed\n");
628
629 err = mlx4_init_cq_table(dev);
630 if (err) {
631 mlx4_err(dev, "Failed to initialize "
632 "completion queue table, aborting.\n");
633 goto err_cmd_poll;
634 }
635
636 err = mlx4_init_srq_table(dev);
637 if (err) {
638 mlx4_err(dev, "Failed to initialize "
639 "shared receive queue table, aborting.\n");
640 goto err_cq_table_free;
641 }
642
643 err = mlx4_init_qp_table(dev);
644 if (err) {
645 mlx4_err(dev, "Failed to initialize "
646 "queue pair table, aborting.\n");
647 goto err_srq_table_free;
648 }
649
650 err = mlx4_init_mcg_table(dev);
651 if (err) {
652 mlx4_err(dev, "Failed to initialize "
653 "multicast group table, aborting.\n");
654 goto err_qp_table_free;
655 }
656
657 return 0;
658
659err_qp_table_free:
660 mlx4_cleanup_qp_table(dev);
661
662err_srq_table_free:
663 mlx4_cleanup_srq_table(dev);
664
665err_cq_table_free:
666 mlx4_cleanup_cq_table(dev);
667
668err_cmd_poll:
669 mlx4_cmd_use_polling(dev);
670
671err_eq_table_free:
672 mlx4_cleanup_eq_table(dev);
673
Jack Morgensteinee49bd92007-07-12 17:50:45 +0300674err_mr_table_free:
Roland Dreier225c7b12007-05-08 18:00:38 -0700675 mlx4_cleanup_mr_table(dev);
676
677err_pd_table_free:
678 mlx4_cleanup_pd_table(dev);
679
680err_kar_unmap:
681 iounmap(priv->kar);
682
683err_uar_free:
684 mlx4_uar_free(dev, &priv->driver_uar);
685
686err_uar_table_free:
687 mlx4_cleanup_uar_table(dev);
688 return err;
689}
690
Roland Dreiere8f9b2e2008-02-04 20:20:41 -0800691static void mlx4_enable_msi_x(struct mlx4_dev *dev)
Roland Dreier225c7b12007-05-08 18:00:38 -0700692{
693 struct mlx4_priv *priv = mlx4_priv(dev);
694 struct msix_entry entries[MLX4_NUM_EQ];
695 int err;
696 int i;
697
698 if (msi_x) {
699 for (i = 0; i < MLX4_NUM_EQ; ++i)
700 entries[i].entry = i;
701
702 err = pci_enable_msix(dev->pdev, entries, ARRAY_SIZE(entries));
703 if (err) {
704 if (err > 0)
705 mlx4_info(dev, "Only %d MSI-X vectors available, "
706 "not using MSI-X\n", err);
707 goto no_msi;
708 }
709
710 for (i = 0; i < MLX4_NUM_EQ; ++i)
711 priv->eq_table.eq[i].irq = entries[i].vector;
712
713 dev->flags |= MLX4_FLAG_MSI_X;
714 return;
715 }
716
717no_msi:
718 for (i = 0; i < MLX4_NUM_EQ; ++i)
719 priv->eq_table.eq[i].irq = dev->pdev->irq;
720}
721
Roland Dreier3d73c282007-10-10 15:43:54 -0700722static int __mlx4_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
Roland Dreier225c7b12007-05-08 18:00:38 -0700723{
Roland Dreier225c7b12007-05-08 18:00:38 -0700724 struct mlx4_priv *priv;
725 struct mlx4_dev *dev;
726 int err;
727
Roland Dreier225c7b12007-05-08 18:00:38 -0700728 printk(KERN_INFO PFX "Initializing %s\n",
729 pci_name(pdev));
730
731 err = pci_enable_device(pdev);
732 if (err) {
733 dev_err(&pdev->dev, "Cannot enable PCI device, "
734 "aborting.\n");
735 return err;
736 }
737
738 /*
Eli Cohen4ff08a72008-04-16 21:09:35 -0700739 * Check for BARs. We expect 0: 1MB
Roland Dreier225c7b12007-05-08 18:00:38 -0700740 */
741 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
742 pci_resource_len(pdev, 0) != 1 << 20) {
743 dev_err(&pdev->dev, "Missing DCS, aborting.\n");
744 err = -ENODEV;
745 goto err_disable_pdev;
746 }
747 if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
748 dev_err(&pdev->dev, "Missing UAR, aborting.\n");
749 err = -ENODEV;
750 goto err_disable_pdev;
751 }
752
753 err = pci_request_region(pdev, 0, DRV_NAME);
754 if (err) {
755 dev_err(&pdev->dev, "Cannot request control region, aborting.\n");
756 goto err_disable_pdev;
757 }
758
759 err = pci_request_region(pdev, 2, DRV_NAME);
760 if (err) {
761 dev_err(&pdev->dev, "Cannot request UAR region, aborting.\n");
762 goto err_release_bar0;
763 }
764
765 pci_set_master(pdev);
766
767 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
768 if (err) {
769 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask.\n");
770 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
771 if (err) {
772 dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting.\n");
773 goto err_release_bar2;
774 }
775 }
776 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
777 if (err) {
778 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit "
779 "consistent PCI DMA mask.\n");
780 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
781 if (err) {
782 dev_err(&pdev->dev, "Can't set consistent PCI DMA mask, "
783 "aborting.\n");
784 goto err_release_bar2;
785 }
786 }
787
788 priv = kzalloc(sizeof *priv, GFP_KERNEL);
789 if (!priv) {
790 dev_err(&pdev->dev, "Device struct alloc failed, "
791 "aborting.\n");
792 err = -ENOMEM;
793 goto err_release_bar2;
794 }
795
796 dev = &priv->dev;
797 dev->pdev = pdev;
Roland Dreierb5814012007-06-07 11:51:58 -0700798 INIT_LIST_HEAD(&priv->ctx_list);
799 spin_lock_init(&priv->ctx_lock);
Roland Dreier225c7b12007-05-08 18:00:38 -0700800
Yevgeny Petrilin62968832008-04-23 11:55:45 -0700801 INIT_LIST_HEAD(&priv->pgdir_list);
802 mutex_init(&priv->pgdir_mutex);
803
Roland Dreier225c7b12007-05-08 18:00:38 -0700804 /*
805 * Now reset the HCA before we touch the PCI capabilities or
806 * attempt a firmware command, since a boot ROM may have left
807 * the HCA in an undefined state.
808 */
809 err = mlx4_reset(dev);
810 if (err) {
811 mlx4_err(dev, "Failed to reset HCA, aborting.\n");
812 goto err_free_dev;
813 }
814
Roland Dreier225c7b12007-05-08 18:00:38 -0700815 if (mlx4_cmd_init(dev)) {
816 mlx4_err(dev, "Failed to init command interface, aborting.\n");
817 goto err_free_dev;
818 }
819
820 err = mlx4_init_hca(dev);
821 if (err)
822 goto err_cmd;
823
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +0300824 mlx4_enable_msi_x(dev);
825
Roland Dreier225c7b12007-05-08 18:00:38 -0700826 err = mlx4_setup_hca(dev);
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +0300827 if (err == -EBUSY && (dev->flags & MLX4_FLAG_MSI_X)) {
828 dev->flags &= ~MLX4_FLAG_MSI_X;
829 pci_disable_msix(pdev);
830 err = mlx4_setup_hca(dev);
831 }
832
Roland Dreier225c7b12007-05-08 18:00:38 -0700833 if (err)
834 goto err_close;
835
836 err = mlx4_register_device(dev);
837 if (err)
838 goto err_cleanup;
839
840 pci_set_drvdata(pdev, dev);
841
842 return 0;
843
844err_cleanup:
845 mlx4_cleanup_mcg_table(dev);
846 mlx4_cleanup_qp_table(dev);
847 mlx4_cleanup_srq_table(dev);
848 mlx4_cleanup_cq_table(dev);
849 mlx4_cmd_use_polling(dev);
850 mlx4_cleanup_eq_table(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -0700851 mlx4_cleanup_mr_table(dev);
852 mlx4_cleanup_pd_table(dev);
853 mlx4_cleanup_uar_table(dev);
854
855err_close:
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +0300856 if (dev->flags & MLX4_FLAG_MSI_X)
857 pci_disable_msix(pdev);
858
Roland Dreier225c7b12007-05-08 18:00:38 -0700859 mlx4_close_hca(dev);
860
861err_cmd:
862 mlx4_cmd_cleanup(dev);
863
864err_free_dev:
Roland Dreier225c7b12007-05-08 18:00:38 -0700865 kfree(priv);
866
867err_release_bar2:
868 pci_release_region(pdev, 2);
869
870err_release_bar0:
871 pci_release_region(pdev, 0);
872
873err_disable_pdev:
874 pci_disable_device(pdev);
875 pci_set_drvdata(pdev, NULL);
876 return err;
877}
878
Roland Dreier3d73c282007-10-10 15:43:54 -0700879static int __devinit mlx4_init_one(struct pci_dev *pdev,
880 const struct pci_device_id *id)
881{
882 static int mlx4_version_printed;
883
884 if (!mlx4_version_printed) {
885 printk(KERN_INFO "%s", mlx4_version);
886 ++mlx4_version_printed;
887 }
888
Roland Dreierb027cac2007-10-13 14:10:50 -0700889 return __mlx4_init_one(pdev, id);
Roland Dreier3d73c282007-10-10 15:43:54 -0700890}
891
892static void mlx4_remove_one(struct pci_dev *pdev)
Roland Dreier225c7b12007-05-08 18:00:38 -0700893{
894 struct mlx4_dev *dev = pci_get_drvdata(pdev);
895 struct mlx4_priv *priv = mlx4_priv(dev);
896 int p;
897
898 if (dev) {
899 mlx4_unregister_device(dev);
900
901 for (p = 1; p <= dev->caps.num_ports; ++p)
902 mlx4_CLOSE_PORT(dev, p);
903
904 mlx4_cleanup_mcg_table(dev);
905 mlx4_cleanup_qp_table(dev);
906 mlx4_cleanup_srq_table(dev);
907 mlx4_cleanup_cq_table(dev);
908 mlx4_cmd_use_polling(dev);
909 mlx4_cleanup_eq_table(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -0700910 mlx4_cleanup_mr_table(dev);
911 mlx4_cleanup_pd_table(dev);
912
913 iounmap(priv->kar);
914 mlx4_uar_free(dev, &priv->driver_uar);
915 mlx4_cleanup_uar_table(dev);
916 mlx4_close_hca(dev);
917 mlx4_cmd_cleanup(dev);
918
919 if (dev->flags & MLX4_FLAG_MSI_X)
920 pci_disable_msix(pdev);
921
922 kfree(priv);
923 pci_release_region(pdev, 2);
924 pci_release_region(pdev, 0);
925 pci_disable_device(pdev);
926 pci_set_drvdata(pdev, NULL);
927 }
928}
929
Jack Morgensteinee49bd92007-07-12 17:50:45 +0300930int mlx4_restart_one(struct pci_dev *pdev)
931{
932 mlx4_remove_one(pdev);
Roland Dreier3d73c282007-10-10 15:43:54 -0700933 return __mlx4_init_one(pdev, NULL);
Jack Morgensteinee49bd92007-07-12 17:50:45 +0300934}
935
Roland Dreier225c7b12007-05-08 18:00:38 -0700936static struct pci_device_id mlx4_pci_table[] = {
937 { PCI_VDEVICE(MELLANOX, 0x6340) }, /* MT25408 "Hermon" SDR */
938 { PCI_VDEVICE(MELLANOX, 0x634a) }, /* MT25408 "Hermon" DDR */
939 { PCI_VDEVICE(MELLANOX, 0x6354) }, /* MT25408 "Hermon" QDR */
Jack Morgenstein786f2382007-07-02 17:36:18 +0300940 { PCI_VDEVICE(MELLANOX, 0x6732) }, /* MT25408 "Hermon" DDR PCIe gen2 */
941 { PCI_VDEVICE(MELLANOX, 0x673c) }, /* MT25408 "Hermon" QDR PCIe gen2 */
Roland Dreier225c7b12007-05-08 18:00:38 -0700942 { 0, }
943};
944
945MODULE_DEVICE_TABLE(pci, mlx4_pci_table);
946
947static struct pci_driver mlx4_driver = {
948 .name = DRV_NAME,
949 .id_table = mlx4_pci_table,
950 .probe = mlx4_init_one,
951 .remove = __devexit_p(mlx4_remove_one)
952};
953
954static int __init mlx4_init(void)
955{
956 int ret;
957
Jack Morgensteinee49bd92007-07-12 17:50:45 +0300958 ret = mlx4_catas_init();
959 if (ret)
960 return ret;
961
Roland Dreier225c7b12007-05-08 18:00:38 -0700962 ret = pci_register_driver(&mlx4_driver);
963 return ret < 0 ? ret : 0;
964}
965
966static void __exit mlx4_cleanup(void)
967{
968 pci_unregister_driver(&mlx4_driver);
Jack Morgensteinee49bd92007-07-12 17:50:45 +0300969 mlx4_catas_cleanup();
Roland Dreier225c7b12007-05-08 18:00:38 -0700970}
971
972module_init(mlx4_init);
973module_exit(mlx4_cleanup);