]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/net/mlx4/main.c
mlx4_en: Reporting HW revision in ethtool -i
[mv-sheeva.git] / drivers / net / mlx4 / main.c
1 /*
2  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4  * Copyright (c) 2005, 2006, 2007, 2008 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 #include <linux/slab.h>
42
43 #include <linux/mlx4/device.h>
44 #include <linux/mlx4/doorbell.h>
45
46 #include "mlx4.h"
47 #include "fw.h"
48 #include "icm.h"
49
50 MODULE_AUTHOR("Roland Dreier");
51 MODULE_DESCRIPTION("Mellanox ConnectX HCA low-level driver");
52 MODULE_LICENSE("Dual BSD/GPL");
53 MODULE_VERSION(DRV_VERSION);
54
55 struct workqueue_struct *mlx4_wq;
56
57 #ifdef CONFIG_MLX4_DEBUG
58
59 int mlx4_debug_level = 0;
60 module_param_named(debug_level, mlx4_debug_level, int, 0644);
61 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
62
63 #endif /* CONFIG_MLX4_DEBUG */
64
65 #ifdef CONFIG_PCI_MSI
66
67 static int msi_x = 1;
68 module_param(msi_x, int, 0444);
69 MODULE_PARM_DESC(msi_x, "attempt to use MSI-X if nonzero");
70
71 #else /* CONFIG_PCI_MSI */
72
73 #define msi_x (0)
74
75 #endif /* CONFIG_PCI_MSI */
76
77 static char mlx4_version[] __devinitdata =
78         DRV_NAME ": Mellanox ConnectX core driver v"
79         DRV_VERSION " (" DRV_RELDATE ")\n";
80
81 static struct mlx4_profile default_profile = {
82         .num_qp         = 1 << 17,
83         .num_srq        = 1 << 16,
84         .rdmarc_per_qp  = 1 << 4,
85         .num_cq         = 1 << 16,
86         .num_mcg        = 1 << 13,
87         .num_mpt        = 1 << 17,
88         .num_mtt        = 1 << 20,
89 };
90
91 static int log_num_mac = 2;
92 module_param_named(log_num_mac, log_num_mac, int, 0444);
93 MODULE_PARM_DESC(log_num_mac, "Log2 max number of MACs per ETH port (1-7)");
94
95 static int log_num_vlan;
96 module_param_named(log_num_vlan, log_num_vlan, int, 0444);
97 MODULE_PARM_DESC(log_num_vlan, "Log2 max number of VLANs per ETH port (0-7)");
98
99 static int use_prio;
100 module_param_named(use_prio, use_prio, bool, 0444);
101 MODULE_PARM_DESC(use_prio, "Enable steering by VLAN priority on ETH ports "
102                   "(0/1, default 0)");
103
104 static int log_mtts_per_seg = ilog2(MLX4_MTT_ENTRY_PER_SEG);
105 module_param_named(log_mtts_per_seg, log_mtts_per_seg, int, 0444);
106 MODULE_PARM_DESC(log_mtts_per_seg, "Log2 number of MTT entries per segment (1-7)");
107
108 int mlx4_check_port_params(struct mlx4_dev *dev,
109                            enum mlx4_port_type *port_type)
110 {
111         int i;
112
113         for (i = 0; i < dev->caps.num_ports - 1; i++) {
114                 if (port_type[i] != port_type[i + 1]) {
115                         if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP)) {
116                                 mlx4_err(dev, "Only same port types supported "
117                                          "on this HCA, aborting.\n");
118                                 return -EINVAL;
119                         }
120                         if (port_type[i] == MLX4_PORT_TYPE_ETH &&
121                             port_type[i + 1] == MLX4_PORT_TYPE_IB)
122                                 return -EINVAL;
123                 }
124         }
125
126         for (i = 0; i < dev->caps.num_ports; i++) {
127                 if (!(port_type[i] & dev->caps.supported_type[i+1])) {
128                         mlx4_err(dev, "Requested port type for port %d is not "
129                                       "supported on this HCA\n", i + 1);
130                         return -EINVAL;
131                 }
132         }
133         return 0;
134 }
135
136 static void mlx4_set_port_mask(struct mlx4_dev *dev)
137 {
138         int i;
139
140         dev->caps.port_mask = 0;
141         for (i = 1; i <= dev->caps.num_ports; ++i)
142                 if (dev->caps.port_type[i] == MLX4_PORT_TYPE_IB)
143                         dev->caps.port_mask |= 1 << (i - 1);
144 }
145 static int mlx4_dev_cap(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap)
146 {
147         int err;
148         int i;
149
150         err = mlx4_QUERY_DEV_CAP(dev, dev_cap);
151         if (err) {
152                 mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
153                 return err;
154         }
155
156         if (dev_cap->min_page_sz > PAGE_SIZE) {
157                 mlx4_err(dev, "HCA minimum page size of %d bigger than "
158                          "kernel PAGE_SIZE of %ld, aborting.\n",
159                          dev_cap->min_page_sz, PAGE_SIZE);
160                 return -ENODEV;
161         }
162         if (dev_cap->num_ports > MLX4_MAX_PORTS) {
163                 mlx4_err(dev, "HCA has %d ports, but we only support %d, "
164                          "aborting.\n",
165                          dev_cap->num_ports, MLX4_MAX_PORTS);
166                 return -ENODEV;
167         }
168
169         if (dev_cap->uar_size > pci_resource_len(dev->pdev, 2)) {
170                 mlx4_err(dev, "HCA reported UAR size of 0x%x bigger than "
171                          "PCI resource 2 size of 0x%llx, aborting.\n",
172                          dev_cap->uar_size,
173                          (unsigned long long) pci_resource_len(dev->pdev, 2));
174                 return -ENODEV;
175         }
176
177         dev->caps.num_ports          = dev_cap->num_ports;
178         for (i = 1; i <= dev->caps.num_ports; ++i) {
179                 dev->caps.vl_cap[i]         = dev_cap->max_vl[i];
180                 dev->caps.ib_mtu_cap[i]     = dev_cap->ib_mtu[i];
181                 dev->caps.gid_table_len[i]  = dev_cap->max_gids[i];
182                 dev->caps.pkey_table_len[i] = dev_cap->max_pkeys[i];
183                 dev->caps.port_width_cap[i] = dev_cap->max_port_width[i];
184                 dev->caps.eth_mtu_cap[i]    = dev_cap->eth_mtu[i];
185                 dev->caps.def_mac[i]        = dev_cap->def_mac[i];
186                 dev->caps.supported_type[i] = dev_cap->supported_port_types[i];
187                 dev->caps.trans_type[i]     = dev_cap->trans_type[i];
188                 dev->caps.vendor_oui[i]     = dev_cap->vendor_oui[i];
189                 dev->caps.wavelength[i]     = dev_cap->wavelength[i];
190                 dev->caps.trans_code[i]     = dev_cap->trans_code[i];
191         }
192
193         dev->caps.num_uars           = dev_cap->uar_size / PAGE_SIZE;
194         dev->caps.local_ca_ack_delay = dev_cap->local_ca_ack_delay;
195         dev->caps.bf_reg_size        = dev_cap->bf_reg_size;
196         dev->caps.bf_regs_per_page   = dev_cap->bf_regs_per_page;
197         dev->caps.max_sq_sg          = dev_cap->max_sq_sg;
198         dev->caps.max_rq_sg          = dev_cap->max_rq_sg;
199         dev->caps.max_wqes           = dev_cap->max_qp_sz;
200         dev->caps.max_qp_init_rdma   = dev_cap->max_requester_per_qp;
201         dev->caps.max_srq_wqes       = dev_cap->max_srq_sz;
202         dev->caps.max_srq_sge        = dev_cap->max_rq_sg - 1;
203         dev->caps.reserved_srqs      = dev_cap->reserved_srqs;
204         dev->caps.max_sq_desc_sz     = dev_cap->max_sq_desc_sz;
205         dev->caps.max_rq_desc_sz     = dev_cap->max_rq_desc_sz;
206         dev->caps.num_qp_per_mgm     = MLX4_QP_PER_MGM;
207         /*
208          * Subtract 1 from the limit because we need to allocate a
209          * spare CQE so the HCA HW can tell the difference between an
210          * empty CQ and a full CQ.
211          */
212         dev->caps.max_cqes           = dev_cap->max_cq_sz - 1;
213         dev->caps.reserved_cqs       = dev_cap->reserved_cqs;
214         dev->caps.reserved_eqs       = dev_cap->reserved_eqs;
215         dev->caps.mtts_per_seg       = 1 << log_mtts_per_seg;
216         dev->caps.reserved_mtts      = DIV_ROUND_UP(dev_cap->reserved_mtts,
217                                                     dev->caps.mtts_per_seg);
218         dev->caps.reserved_mrws      = dev_cap->reserved_mrws;
219         dev->caps.reserved_uars      = dev_cap->reserved_uars;
220         dev->caps.reserved_pds       = dev_cap->reserved_pds;
221         dev->caps.mtt_entry_sz       = dev->caps.mtts_per_seg * dev_cap->mtt_entry_sz;
222         dev->caps.max_msg_sz         = dev_cap->max_msg_sz;
223         dev->caps.page_size_cap      = ~(u32) (dev_cap->min_page_sz - 1);
224         dev->caps.flags              = dev_cap->flags;
225         dev->caps.bmme_flags         = dev_cap->bmme_flags;
226         dev->caps.reserved_lkey      = dev_cap->reserved_lkey;
227         dev->caps.stat_rate_support  = dev_cap->stat_rate_support;
228         dev->caps.udp_rss            = dev_cap->udp_rss;
229         dev->caps.loopback_support   = dev_cap->loopback_support;
230         dev->caps.wol                = dev_cap->wol;
231         dev->caps.max_gso_sz         = dev_cap->max_gso_sz;
232
233         dev->caps.log_num_macs  = log_num_mac;
234         dev->caps.log_num_vlans = log_num_vlan;
235         dev->caps.log_num_prios = use_prio ? 3 : 0;
236
237         for (i = 1; i <= dev->caps.num_ports; ++i) {
238                 if (dev->caps.supported_type[i] != MLX4_PORT_TYPE_ETH)
239                         dev->caps.port_type[i] = MLX4_PORT_TYPE_IB;
240                 else
241                         dev->caps.port_type[i] = MLX4_PORT_TYPE_ETH;
242                 dev->caps.possible_type[i] = dev->caps.port_type[i];
243                 mlx4_priv(dev)->sense.sense_allowed[i] =
244                         dev->caps.supported_type[i] == MLX4_PORT_TYPE_AUTO;
245
246                 if (dev->caps.log_num_macs > dev_cap->log_max_macs[i]) {
247                         dev->caps.log_num_macs = dev_cap->log_max_macs[i];
248                         mlx4_warn(dev, "Requested number of MACs is too much "
249                                   "for port %d, reducing to %d.\n",
250                                   i, 1 << dev->caps.log_num_macs);
251                 }
252                 if (dev->caps.log_num_vlans > dev_cap->log_max_vlans[i]) {
253                         dev->caps.log_num_vlans = dev_cap->log_max_vlans[i];
254                         mlx4_warn(dev, "Requested number of VLANs is too much "
255                                   "for port %d, reducing to %d.\n",
256                                   i, 1 << dev->caps.log_num_vlans);
257                 }
258         }
259
260         mlx4_set_port_mask(dev);
261
262         dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW] = dev_cap->reserved_qps;
263         dev->caps.reserved_qps_cnt[MLX4_QP_REGION_ETH_ADDR] =
264                 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_ADDR] =
265                 (1 << dev->caps.log_num_macs) *
266                 (1 << dev->caps.log_num_vlans) *
267                 (1 << dev->caps.log_num_prios) *
268                 dev->caps.num_ports;
269         dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_EXCH] = MLX4_NUM_FEXCH;
270
271         dev->caps.reserved_qps = dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW] +
272                 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_ETH_ADDR] +
273                 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_ADDR] +
274                 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_EXCH];
275
276         return 0;
277 }
278
279 /*
280  * Change the port configuration of the device.
281  * Every user of this function must hold the port mutex.
282  */
283 int mlx4_change_port_types(struct mlx4_dev *dev,
284                            enum mlx4_port_type *port_types)
285 {
286         int err = 0;
287         int change = 0;
288         int port;
289
290         for (port = 0; port <  dev->caps.num_ports; port++) {
291                 /* Change the port type only if the new type is different
292                  * from the current, and not set to Auto */
293                 if (port_types[port] != dev->caps.port_type[port + 1]) {
294                         change = 1;
295                         dev->caps.port_type[port + 1] = port_types[port];
296                 }
297         }
298         if (change) {
299                 mlx4_unregister_device(dev);
300                 for (port = 1; port <= dev->caps.num_ports; port++) {
301                         mlx4_CLOSE_PORT(dev, port);
302                         err = mlx4_SET_PORT(dev, port);
303                         if (err) {
304                                 mlx4_err(dev, "Failed to set port %d, "
305                                               "aborting\n", port);
306                                 goto out;
307                         }
308                 }
309                 mlx4_set_port_mask(dev);
310                 err = mlx4_register_device(dev);
311         }
312
313 out:
314         return err;
315 }
316
317 static ssize_t show_port_type(struct device *dev,
318                               struct device_attribute *attr,
319                               char *buf)
320 {
321         struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
322                                                    port_attr);
323         struct mlx4_dev *mdev = info->dev;
324         char type[8];
325
326         sprintf(type, "%s",
327                 (mdev->caps.port_type[info->port] == MLX4_PORT_TYPE_IB) ?
328                 "ib" : "eth");
329         if (mdev->caps.possible_type[info->port] == MLX4_PORT_TYPE_AUTO)
330                 sprintf(buf, "auto (%s)\n", type);
331         else
332                 sprintf(buf, "%s\n", type);
333
334         return strlen(buf);
335 }
336
337 static ssize_t set_port_type(struct device *dev,
338                              struct device_attribute *attr,
339                              const char *buf, size_t count)
340 {
341         struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
342                                                    port_attr);
343         struct mlx4_dev *mdev = info->dev;
344         struct mlx4_priv *priv = mlx4_priv(mdev);
345         enum mlx4_port_type types[MLX4_MAX_PORTS];
346         enum mlx4_port_type new_types[MLX4_MAX_PORTS];
347         int i;
348         int err = 0;
349
350         if (!strcmp(buf, "ib\n"))
351                 info->tmp_type = MLX4_PORT_TYPE_IB;
352         else if (!strcmp(buf, "eth\n"))
353                 info->tmp_type = MLX4_PORT_TYPE_ETH;
354         else if (!strcmp(buf, "auto\n"))
355                 info->tmp_type = MLX4_PORT_TYPE_AUTO;
356         else {
357                 mlx4_err(mdev, "%s is not supported port type\n", buf);
358                 return -EINVAL;
359         }
360
361         mlx4_stop_sense(mdev);
362         mutex_lock(&priv->port_mutex);
363         /* Possible type is always the one that was delivered */
364         mdev->caps.possible_type[info->port] = info->tmp_type;
365
366         for (i = 0; i < mdev->caps.num_ports; i++) {
367                 types[i] = priv->port[i+1].tmp_type ? priv->port[i+1].tmp_type :
368                                         mdev->caps.possible_type[i+1];
369                 if (types[i] == MLX4_PORT_TYPE_AUTO)
370                         types[i] = mdev->caps.port_type[i+1];
371         }
372
373         if (!(mdev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP)) {
374                 for (i = 1; i <= mdev->caps.num_ports; i++) {
375                         if (mdev->caps.possible_type[i] == MLX4_PORT_TYPE_AUTO) {
376                                 mdev->caps.possible_type[i] = mdev->caps.port_type[i];
377                                 err = -EINVAL;
378                         }
379                 }
380         }
381         if (err) {
382                 mlx4_err(mdev, "Auto sensing is not supported on this HCA. "
383                                "Set only 'eth' or 'ib' for both ports "
384                                "(should be the same)\n");
385                 goto out;
386         }
387
388         mlx4_do_sense_ports(mdev, new_types, types);
389
390         err = mlx4_check_port_params(mdev, new_types);
391         if (err)
392                 goto out;
393
394         /* We are about to apply the changes after the configuration
395          * was verified, no need to remember the temporary types
396          * any more */
397         for (i = 0; i < mdev->caps.num_ports; i++)
398                 priv->port[i + 1].tmp_type = 0;
399
400         err = mlx4_change_port_types(mdev, new_types);
401
402 out:
403         mlx4_start_sense(mdev);
404         mutex_unlock(&priv->port_mutex);
405         return err ? err : count;
406 }
407
408 static int mlx4_load_fw(struct mlx4_dev *dev)
409 {
410         struct mlx4_priv *priv = mlx4_priv(dev);
411         int err;
412
413         priv->fw.fw_icm = mlx4_alloc_icm(dev, priv->fw.fw_pages,
414                                          GFP_HIGHUSER | __GFP_NOWARN, 0);
415         if (!priv->fw.fw_icm) {
416                 mlx4_err(dev, "Couldn't allocate FW area, aborting.\n");
417                 return -ENOMEM;
418         }
419
420         err = mlx4_MAP_FA(dev, priv->fw.fw_icm);
421         if (err) {
422                 mlx4_err(dev, "MAP_FA command failed, aborting.\n");
423                 goto err_free;
424         }
425
426         err = mlx4_RUN_FW(dev);
427         if (err) {
428                 mlx4_err(dev, "RUN_FW command failed, aborting.\n");
429                 goto err_unmap_fa;
430         }
431
432         return 0;
433
434 err_unmap_fa:
435         mlx4_UNMAP_FA(dev);
436
437 err_free:
438         mlx4_free_icm(dev, priv->fw.fw_icm, 0);
439         return err;
440 }
441
442 static int mlx4_init_cmpt_table(struct mlx4_dev *dev, u64 cmpt_base,
443                                 int cmpt_entry_sz)
444 {
445         struct mlx4_priv *priv = mlx4_priv(dev);
446         int err;
447
448         err = mlx4_init_icm_table(dev, &priv->qp_table.cmpt_table,
449                                   cmpt_base +
450                                   ((u64) (MLX4_CMPT_TYPE_QP *
451                                           cmpt_entry_sz) << MLX4_CMPT_SHIFT),
452                                   cmpt_entry_sz, dev->caps.num_qps,
453                                   dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
454                                   0, 0);
455         if (err)
456                 goto err;
457
458         err = mlx4_init_icm_table(dev, &priv->srq_table.cmpt_table,
459                                   cmpt_base +
460                                   ((u64) (MLX4_CMPT_TYPE_SRQ *
461                                           cmpt_entry_sz) << MLX4_CMPT_SHIFT),
462                                   cmpt_entry_sz, dev->caps.num_srqs,
463                                   dev->caps.reserved_srqs, 0, 0);
464         if (err)
465                 goto err_qp;
466
467         err = mlx4_init_icm_table(dev, &priv->cq_table.cmpt_table,
468                                   cmpt_base +
469                                   ((u64) (MLX4_CMPT_TYPE_CQ *
470                                           cmpt_entry_sz) << MLX4_CMPT_SHIFT),
471                                   cmpt_entry_sz, dev->caps.num_cqs,
472                                   dev->caps.reserved_cqs, 0, 0);
473         if (err)
474                 goto err_srq;
475
476         err = mlx4_init_icm_table(dev, &priv->eq_table.cmpt_table,
477                                   cmpt_base +
478                                   ((u64) (MLX4_CMPT_TYPE_EQ *
479                                           cmpt_entry_sz) << MLX4_CMPT_SHIFT),
480                                   cmpt_entry_sz,
481                                   dev->caps.num_eqs, dev->caps.num_eqs, 0, 0);
482         if (err)
483                 goto err_cq;
484
485         return 0;
486
487 err_cq:
488         mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
489
490 err_srq:
491         mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
492
493 err_qp:
494         mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
495
496 err:
497         return err;
498 }
499
500 static int mlx4_init_icm(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap,
501                          struct mlx4_init_hca_param *init_hca, u64 icm_size)
502 {
503         struct mlx4_priv *priv = mlx4_priv(dev);
504         u64 aux_pages;
505         int err;
506
507         err = mlx4_SET_ICM_SIZE(dev, icm_size, &aux_pages);
508         if (err) {
509                 mlx4_err(dev, "SET_ICM_SIZE command failed, aborting.\n");
510                 return err;
511         }
512
513         mlx4_dbg(dev, "%lld KB of HCA context requires %lld KB aux memory.\n",
514                  (unsigned long long) icm_size >> 10,
515                  (unsigned long long) aux_pages << 2);
516
517         priv->fw.aux_icm = mlx4_alloc_icm(dev, aux_pages,
518                                           GFP_HIGHUSER | __GFP_NOWARN, 0);
519         if (!priv->fw.aux_icm) {
520                 mlx4_err(dev, "Couldn't allocate aux memory, aborting.\n");
521                 return -ENOMEM;
522         }
523
524         err = mlx4_MAP_ICM_AUX(dev, priv->fw.aux_icm);
525         if (err) {
526                 mlx4_err(dev, "MAP_ICM_AUX command failed, aborting.\n");
527                 goto err_free_aux;
528         }
529
530         err = mlx4_init_cmpt_table(dev, init_hca->cmpt_base, dev_cap->cmpt_entry_sz);
531         if (err) {
532                 mlx4_err(dev, "Failed to map cMPT context memory, aborting.\n");
533                 goto err_unmap_aux;
534         }
535
536         err = mlx4_init_icm_table(dev, &priv->eq_table.table,
537                                   init_hca->eqc_base, dev_cap->eqc_entry_sz,
538                                   dev->caps.num_eqs, dev->caps.num_eqs,
539                                   0, 0);
540         if (err) {
541                 mlx4_err(dev, "Failed to map EQ context memory, aborting.\n");
542                 goto err_unmap_cmpt;
543         }
544
545         /*
546          * Reserved MTT entries must be aligned up to a cacheline
547          * boundary, since the FW will write to them, while the driver
548          * writes to all other MTT entries. (The variable
549          * dev->caps.mtt_entry_sz below is really the MTT segment
550          * size, not the raw entry size)
551          */
552         dev->caps.reserved_mtts =
553                 ALIGN(dev->caps.reserved_mtts * dev->caps.mtt_entry_sz,
554                       dma_get_cache_alignment()) / dev->caps.mtt_entry_sz;
555
556         err = mlx4_init_icm_table(dev, &priv->mr_table.mtt_table,
557                                   init_hca->mtt_base,
558                                   dev->caps.mtt_entry_sz,
559                                   dev->caps.num_mtt_segs,
560                                   dev->caps.reserved_mtts, 1, 0);
561         if (err) {
562                 mlx4_err(dev, "Failed to map MTT context memory, aborting.\n");
563                 goto err_unmap_eq;
564         }
565
566         err = mlx4_init_icm_table(dev, &priv->mr_table.dmpt_table,
567                                   init_hca->dmpt_base,
568                                   dev_cap->dmpt_entry_sz,
569                                   dev->caps.num_mpts,
570                                   dev->caps.reserved_mrws, 1, 1);
571         if (err) {
572                 mlx4_err(dev, "Failed to map dMPT context memory, aborting.\n");
573                 goto err_unmap_mtt;
574         }
575
576         err = mlx4_init_icm_table(dev, &priv->qp_table.qp_table,
577                                   init_hca->qpc_base,
578                                   dev_cap->qpc_entry_sz,
579                                   dev->caps.num_qps,
580                                   dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
581                                   0, 0);
582         if (err) {
583                 mlx4_err(dev, "Failed to map QP context memory, aborting.\n");
584                 goto err_unmap_dmpt;
585         }
586
587         err = mlx4_init_icm_table(dev, &priv->qp_table.auxc_table,
588                                   init_hca->auxc_base,
589                                   dev_cap->aux_entry_sz,
590                                   dev->caps.num_qps,
591                                   dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
592                                   0, 0);
593         if (err) {
594                 mlx4_err(dev, "Failed to map AUXC context memory, aborting.\n");
595                 goto err_unmap_qp;
596         }
597
598         err = mlx4_init_icm_table(dev, &priv->qp_table.altc_table,
599                                   init_hca->altc_base,
600                                   dev_cap->altc_entry_sz,
601                                   dev->caps.num_qps,
602                                   dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
603                                   0, 0);
604         if (err) {
605                 mlx4_err(dev, "Failed to map ALTC context memory, aborting.\n");
606                 goto err_unmap_auxc;
607         }
608
609         err = mlx4_init_icm_table(dev, &priv->qp_table.rdmarc_table,
610                                   init_hca->rdmarc_base,
611                                   dev_cap->rdmarc_entry_sz << priv->qp_table.rdmarc_shift,
612                                   dev->caps.num_qps,
613                                   dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
614                                   0, 0);
615         if (err) {
616                 mlx4_err(dev, "Failed to map RDMARC context memory, aborting\n");
617                 goto err_unmap_altc;
618         }
619
620         err = mlx4_init_icm_table(dev, &priv->cq_table.table,
621                                   init_hca->cqc_base,
622                                   dev_cap->cqc_entry_sz,
623                                   dev->caps.num_cqs,
624                                   dev->caps.reserved_cqs, 0, 0);
625         if (err) {
626                 mlx4_err(dev, "Failed to map CQ context memory, aborting.\n");
627                 goto err_unmap_rdmarc;
628         }
629
630         err = mlx4_init_icm_table(dev, &priv->srq_table.table,
631                                   init_hca->srqc_base,
632                                   dev_cap->srq_entry_sz,
633                                   dev->caps.num_srqs,
634                                   dev->caps.reserved_srqs, 0, 0);
635         if (err) {
636                 mlx4_err(dev, "Failed to map SRQ context memory, aborting.\n");
637                 goto err_unmap_cq;
638         }
639
640         /*
641          * It's not strictly required, but for simplicity just map the
642          * whole multicast group table now.  The table isn't very big
643          * and it's a lot easier than trying to track ref counts.
644          */
645         err = mlx4_init_icm_table(dev, &priv->mcg_table.table,
646                                   init_hca->mc_base, MLX4_MGM_ENTRY_SIZE,
647                                   dev->caps.num_mgms + dev->caps.num_amgms,
648                                   dev->caps.num_mgms + dev->caps.num_amgms,
649                                   0, 0);
650         if (err) {
651                 mlx4_err(dev, "Failed to map MCG context memory, aborting.\n");
652                 goto err_unmap_srq;
653         }
654
655         return 0;
656
657 err_unmap_srq:
658         mlx4_cleanup_icm_table(dev, &priv->srq_table.table);
659
660 err_unmap_cq:
661         mlx4_cleanup_icm_table(dev, &priv->cq_table.table);
662
663 err_unmap_rdmarc:
664         mlx4_cleanup_icm_table(dev, &priv->qp_table.rdmarc_table);
665
666 err_unmap_altc:
667         mlx4_cleanup_icm_table(dev, &priv->qp_table.altc_table);
668
669 err_unmap_auxc:
670         mlx4_cleanup_icm_table(dev, &priv->qp_table.auxc_table);
671
672 err_unmap_qp:
673         mlx4_cleanup_icm_table(dev, &priv->qp_table.qp_table);
674
675 err_unmap_dmpt:
676         mlx4_cleanup_icm_table(dev, &priv->mr_table.dmpt_table);
677
678 err_unmap_mtt:
679         mlx4_cleanup_icm_table(dev, &priv->mr_table.mtt_table);
680
681 err_unmap_eq:
682         mlx4_cleanup_icm_table(dev, &priv->eq_table.table);
683
684 err_unmap_cmpt:
685         mlx4_cleanup_icm_table(dev, &priv->eq_table.cmpt_table);
686         mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
687         mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
688         mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
689
690 err_unmap_aux:
691         mlx4_UNMAP_ICM_AUX(dev);
692
693 err_free_aux:
694         mlx4_free_icm(dev, priv->fw.aux_icm, 0);
695
696         return err;
697 }
698
699 static void mlx4_free_icms(struct mlx4_dev *dev)
700 {
701         struct mlx4_priv *priv = mlx4_priv(dev);
702
703         mlx4_cleanup_icm_table(dev, &priv->mcg_table.table);
704         mlx4_cleanup_icm_table(dev, &priv->srq_table.table);
705         mlx4_cleanup_icm_table(dev, &priv->cq_table.table);
706         mlx4_cleanup_icm_table(dev, &priv->qp_table.rdmarc_table);
707         mlx4_cleanup_icm_table(dev, &priv->qp_table.altc_table);
708         mlx4_cleanup_icm_table(dev, &priv->qp_table.auxc_table);
709         mlx4_cleanup_icm_table(dev, &priv->qp_table.qp_table);
710         mlx4_cleanup_icm_table(dev, &priv->mr_table.dmpt_table);
711         mlx4_cleanup_icm_table(dev, &priv->mr_table.mtt_table);
712         mlx4_cleanup_icm_table(dev, &priv->eq_table.table);
713         mlx4_cleanup_icm_table(dev, &priv->eq_table.cmpt_table);
714         mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
715         mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
716         mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
717
718         mlx4_UNMAP_ICM_AUX(dev);
719         mlx4_free_icm(dev, priv->fw.aux_icm, 0);
720 }
721
722 static void mlx4_close_hca(struct mlx4_dev *dev)
723 {
724         mlx4_CLOSE_HCA(dev, 0);
725         mlx4_free_icms(dev);
726         mlx4_UNMAP_FA(dev);
727         mlx4_free_icm(dev, mlx4_priv(dev)->fw.fw_icm, 0);
728 }
729
730 static int mlx4_init_hca(struct mlx4_dev *dev)
731 {
732         struct mlx4_priv          *priv = mlx4_priv(dev);
733         struct mlx4_adapter        adapter;
734         struct mlx4_dev_cap        dev_cap;
735         struct mlx4_mod_stat_cfg   mlx4_cfg;
736         struct mlx4_profile        profile;
737         struct mlx4_init_hca_param init_hca;
738         u64 icm_size;
739         int err;
740
741         err = mlx4_QUERY_FW(dev);
742         if (err) {
743                 if (err == -EACCES)
744                         mlx4_info(dev, "non-primary physical function, skipping.\n");
745                 else
746                         mlx4_err(dev, "QUERY_FW command failed, aborting.\n");
747                 return err;
748         }
749
750         err = mlx4_load_fw(dev);
751         if (err) {
752                 mlx4_err(dev, "Failed to start FW, aborting.\n");
753                 return err;
754         }
755
756         mlx4_cfg.log_pg_sz_m = 1;
757         mlx4_cfg.log_pg_sz = 0;
758         err = mlx4_MOD_STAT_CFG(dev, &mlx4_cfg);
759         if (err)
760                 mlx4_warn(dev, "Failed to override log_pg_sz parameter\n");
761
762         err = mlx4_dev_cap(dev, &dev_cap);
763         if (err) {
764                 mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
765                 goto err_stop_fw;
766         }
767
768         profile = default_profile;
769
770         icm_size = mlx4_make_profile(dev, &profile, &dev_cap, &init_hca);
771         if ((long long) icm_size < 0) {
772                 err = icm_size;
773                 goto err_stop_fw;
774         }
775
776         init_hca.log_uar_sz = ilog2(dev->caps.num_uars);
777
778         err = mlx4_init_icm(dev, &dev_cap, &init_hca, icm_size);
779         if (err)
780                 goto err_stop_fw;
781
782         err = mlx4_INIT_HCA(dev, &init_hca);
783         if (err) {
784                 mlx4_err(dev, "INIT_HCA command failed, aborting.\n");
785                 goto err_free_icm;
786         }
787
788         err = mlx4_QUERY_ADAPTER(dev, &adapter);
789         if (err) {
790                 mlx4_err(dev, "QUERY_ADAPTER command failed, aborting.\n");
791                 goto err_close;
792         }
793
794         priv->eq_table.inta_pin = adapter.inta_pin;
795         memcpy(dev->board_id, adapter.board_id, sizeof dev->board_id);
796
797         return 0;
798
799 err_close:
800         mlx4_CLOSE_HCA(dev, 0);
801
802 err_free_icm:
803         mlx4_free_icms(dev);
804
805 err_stop_fw:
806         mlx4_UNMAP_FA(dev);
807         mlx4_free_icm(dev, priv->fw.fw_icm, 0);
808
809         return err;
810 }
811
812 static int mlx4_setup_hca(struct mlx4_dev *dev)
813 {
814         struct mlx4_priv *priv = mlx4_priv(dev);
815         int err;
816         int port;
817         __be32 ib_port_default_caps;
818
819         err = mlx4_init_uar_table(dev);
820         if (err) {
821                 mlx4_err(dev, "Failed to initialize "
822                          "user access region table, aborting.\n");
823                 return err;
824         }
825
826         err = mlx4_uar_alloc(dev, &priv->driver_uar);
827         if (err) {
828                 mlx4_err(dev, "Failed to allocate driver access region, "
829                          "aborting.\n");
830                 goto err_uar_table_free;
831         }
832
833         priv->kar = ioremap((phys_addr_t) priv->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
834         if (!priv->kar) {
835                 mlx4_err(dev, "Couldn't map kernel access region, "
836                          "aborting.\n");
837                 err = -ENOMEM;
838                 goto err_uar_free;
839         }
840
841         err = mlx4_init_pd_table(dev);
842         if (err) {
843                 mlx4_err(dev, "Failed to initialize "
844                          "protection domain table, aborting.\n");
845                 goto err_kar_unmap;
846         }
847
848         err = mlx4_init_mr_table(dev);
849         if (err) {
850                 mlx4_err(dev, "Failed to initialize "
851                          "memory region table, aborting.\n");
852                 goto err_pd_table_free;
853         }
854
855         err = mlx4_init_eq_table(dev);
856         if (err) {
857                 mlx4_err(dev, "Failed to initialize "
858                          "event queue table, aborting.\n");
859                 goto err_mr_table_free;
860         }
861
862         err = mlx4_cmd_use_events(dev);
863         if (err) {
864                 mlx4_err(dev, "Failed to switch to event-driven "
865                          "firmware commands, aborting.\n");
866                 goto err_eq_table_free;
867         }
868
869         err = mlx4_NOP(dev);
870         if (err) {
871                 if (dev->flags & MLX4_FLAG_MSI_X) {
872                         mlx4_warn(dev, "NOP command failed to generate MSI-X "
873                                   "interrupt IRQ %d).\n",
874                                   priv->eq_table.eq[dev->caps.num_comp_vectors].irq);
875                         mlx4_warn(dev, "Trying again without MSI-X.\n");
876                 } else {
877                         mlx4_err(dev, "NOP command failed to generate interrupt "
878                                  "(IRQ %d), aborting.\n",
879                                  priv->eq_table.eq[dev->caps.num_comp_vectors].irq);
880                         mlx4_err(dev, "BIOS or ACPI interrupt routing problem?\n");
881                 }
882
883                 goto err_cmd_poll;
884         }
885
886         mlx4_dbg(dev, "NOP command IRQ test passed\n");
887
888         err = mlx4_init_cq_table(dev);
889         if (err) {
890                 mlx4_err(dev, "Failed to initialize "
891                          "completion queue table, aborting.\n");
892                 goto err_cmd_poll;
893         }
894
895         err = mlx4_init_srq_table(dev);
896         if (err) {
897                 mlx4_err(dev, "Failed to initialize "
898                          "shared receive queue table, aborting.\n");
899                 goto err_cq_table_free;
900         }
901
902         err = mlx4_init_qp_table(dev);
903         if (err) {
904                 mlx4_err(dev, "Failed to initialize "
905                          "queue pair table, aborting.\n");
906                 goto err_srq_table_free;
907         }
908
909         err = mlx4_init_mcg_table(dev);
910         if (err) {
911                 mlx4_err(dev, "Failed to initialize "
912                          "multicast group table, aborting.\n");
913                 goto err_qp_table_free;
914         }
915
916         for (port = 1; port <= dev->caps.num_ports; port++) {
917                 ib_port_default_caps = 0;
918                 err = mlx4_get_port_ib_caps(dev, port, &ib_port_default_caps);
919                 if (err)
920                         mlx4_warn(dev, "failed to get port %d default "
921                                   "ib capabilities (%d). Continuing with "
922                                   "caps = 0\n", port, err);
923                 dev->caps.ib_port_def_cap[port] = ib_port_default_caps;
924                 err = mlx4_SET_PORT(dev, port);
925                 if (err) {
926                         mlx4_err(dev, "Failed to set port %d, aborting\n",
927                                 port);
928                         goto err_mcg_table_free;
929                 }
930         }
931
932         return 0;
933
934 err_mcg_table_free:
935         mlx4_cleanup_mcg_table(dev);
936
937 err_qp_table_free:
938         mlx4_cleanup_qp_table(dev);
939
940 err_srq_table_free:
941         mlx4_cleanup_srq_table(dev);
942
943 err_cq_table_free:
944         mlx4_cleanup_cq_table(dev);
945
946 err_cmd_poll:
947         mlx4_cmd_use_polling(dev);
948
949 err_eq_table_free:
950         mlx4_cleanup_eq_table(dev);
951
952 err_mr_table_free:
953         mlx4_cleanup_mr_table(dev);
954
955 err_pd_table_free:
956         mlx4_cleanup_pd_table(dev);
957
958 err_kar_unmap:
959         iounmap(priv->kar);
960
961 err_uar_free:
962         mlx4_uar_free(dev, &priv->driver_uar);
963
964 err_uar_table_free:
965         mlx4_cleanup_uar_table(dev);
966         return err;
967 }
968
969 static void mlx4_enable_msi_x(struct mlx4_dev *dev)
970 {
971         struct mlx4_priv *priv = mlx4_priv(dev);
972         struct msix_entry *entries;
973         int nreq = min_t(int, dev->caps.num_ports *
974                          min_t(int, num_online_cpus() + 1, MAX_MSIX_P_PORT)
975                                 + MSIX_LEGACY_SZ, MAX_MSIX);
976         int err;
977         int i;
978
979         if (msi_x) {
980                 nreq = min_t(int, dev->caps.num_eqs - dev->caps.reserved_eqs,
981                              nreq);
982                 entries = kcalloc(nreq, sizeof *entries, GFP_KERNEL);
983                 if (!entries)
984                         goto no_msi;
985
986                 for (i = 0; i < nreq; ++i)
987                         entries[i].entry = i;
988
989         retry:
990                 err = pci_enable_msix(dev->pdev, entries, nreq);
991                 if (err) {
992                         /* Try again if at least 2 vectors are available */
993                         if (err > 1) {
994                                 mlx4_info(dev, "Requested %d vectors, "
995                                           "but only %d MSI-X vectors available, "
996                                           "trying again\n", nreq, err);
997                                 nreq = err;
998                                 goto retry;
999                         }
1000                         kfree(entries);
1001                         goto no_msi;
1002                 }
1003
1004                 if (nreq <
1005                     MSIX_LEGACY_SZ + dev->caps.num_ports * MIN_MSIX_P_PORT) {
1006                         /*Working in legacy mode , all EQ's shared*/
1007                         dev->caps.comp_pool           = 0;
1008                         dev->caps.num_comp_vectors = nreq - 1;
1009                 } else {
1010                         dev->caps.comp_pool           = nreq - MSIX_LEGACY_SZ;
1011                         dev->caps.num_comp_vectors = MSIX_LEGACY_SZ - 1;
1012                 }
1013                 for (i = 0; i < nreq; ++i)
1014                         priv->eq_table.eq[i].irq = entries[i].vector;
1015
1016                 dev->flags |= MLX4_FLAG_MSI_X;
1017
1018                 kfree(entries);
1019                 return;
1020         }
1021
1022 no_msi:
1023         dev->caps.num_comp_vectors = 1;
1024         dev->caps.comp_pool        = 0;
1025
1026         for (i = 0; i < 2; ++i)
1027                 priv->eq_table.eq[i].irq = dev->pdev->irq;
1028 }
1029
1030 static int mlx4_init_port_info(struct mlx4_dev *dev, int port)
1031 {
1032         struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
1033         int err = 0;
1034
1035         info->dev = dev;
1036         info->port = port;
1037         mlx4_init_mac_table(dev, &info->mac_table);
1038         mlx4_init_vlan_table(dev, &info->vlan_table);
1039
1040         sprintf(info->dev_name, "mlx4_port%d", port);
1041         info->port_attr.attr.name = info->dev_name;
1042         info->port_attr.attr.mode = S_IRUGO | S_IWUSR;
1043         info->port_attr.show      = show_port_type;
1044         info->port_attr.store     = set_port_type;
1045         sysfs_attr_init(&info->port_attr.attr);
1046
1047         err = device_create_file(&dev->pdev->dev, &info->port_attr);
1048         if (err) {
1049                 mlx4_err(dev, "Failed to create file for port %d\n", port);
1050                 info->port = -1;
1051         }
1052
1053         return err;
1054 }
1055
1056 static void mlx4_cleanup_port_info(struct mlx4_port_info *info)
1057 {
1058         if (info->port < 0)
1059                 return;
1060
1061         device_remove_file(&info->dev->pdev->dev, &info->port_attr);
1062 }
1063
1064 static int __mlx4_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
1065 {
1066         struct mlx4_priv *priv;
1067         struct mlx4_dev *dev;
1068         int err;
1069         int port;
1070
1071         pr_info(DRV_NAME ": Initializing %s\n", pci_name(pdev));
1072
1073         err = pci_enable_device(pdev);
1074         if (err) {
1075                 dev_err(&pdev->dev, "Cannot enable PCI device, "
1076                         "aborting.\n");
1077                 return err;
1078         }
1079
1080         /*
1081          * Check for BARs.  We expect 0: 1MB
1082          */
1083         if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
1084             pci_resource_len(pdev, 0) != 1 << 20) {
1085                 dev_err(&pdev->dev, "Missing DCS, aborting.\n");
1086                 err = -ENODEV;
1087                 goto err_disable_pdev;
1088         }
1089         if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
1090                 dev_err(&pdev->dev, "Missing UAR, aborting.\n");
1091                 err = -ENODEV;
1092                 goto err_disable_pdev;
1093         }
1094
1095         err = pci_request_regions(pdev, DRV_NAME);
1096         if (err) {
1097                 dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");
1098                 goto err_disable_pdev;
1099         }
1100
1101         pci_set_master(pdev);
1102
1103         err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1104         if (err) {
1105                 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask.\n");
1106                 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1107                 if (err) {
1108                         dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting.\n");
1109                         goto err_release_regions;
1110                 }
1111         }
1112         err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1113         if (err) {
1114                 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit "
1115                          "consistent PCI DMA mask.\n");
1116                 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1117                 if (err) {
1118                         dev_err(&pdev->dev, "Can't set consistent PCI DMA mask, "
1119                                 "aborting.\n");
1120                         goto err_release_regions;
1121                 }
1122         }
1123
1124         priv = kzalloc(sizeof *priv, GFP_KERNEL);
1125         if (!priv) {
1126                 dev_err(&pdev->dev, "Device struct alloc failed, "
1127                         "aborting.\n");
1128                 err = -ENOMEM;
1129                 goto err_release_regions;
1130         }
1131
1132         dev       = &priv->dev;
1133         dev->pdev = pdev;
1134         INIT_LIST_HEAD(&priv->ctx_list);
1135         spin_lock_init(&priv->ctx_lock);
1136
1137         mutex_init(&priv->port_mutex);
1138
1139         INIT_LIST_HEAD(&priv->pgdir_list);
1140         mutex_init(&priv->pgdir_mutex);
1141
1142         pci_read_config_byte(pdev, PCI_REVISION_ID, &dev->rev_id);
1143
1144         /*
1145          * Now reset the HCA before we touch the PCI capabilities or
1146          * attempt a firmware command, since a boot ROM may have left
1147          * the HCA in an undefined state.
1148          */
1149         err = mlx4_reset(dev);
1150         if (err) {
1151                 mlx4_err(dev, "Failed to reset HCA, aborting.\n");
1152                 goto err_free_dev;
1153         }
1154
1155         if (mlx4_cmd_init(dev)) {
1156                 mlx4_err(dev, "Failed to init command interface, aborting.\n");
1157                 goto err_free_dev;
1158         }
1159
1160         err = mlx4_init_hca(dev);
1161         if (err)
1162                 goto err_cmd;
1163
1164         err = mlx4_alloc_eq_table(dev);
1165         if (err)
1166                 goto err_close;
1167
1168         priv->msix_ctl.pool_bm = 0;
1169         spin_lock_init(&priv->msix_ctl.pool_lock);
1170
1171         mlx4_enable_msi_x(dev);
1172
1173         err = mlx4_setup_hca(dev);
1174         if (err == -EBUSY && (dev->flags & MLX4_FLAG_MSI_X)) {
1175                 dev->flags &= ~MLX4_FLAG_MSI_X;
1176                 pci_disable_msix(pdev);
1177                 err = mlx4_setup_hca(dev);
1178         }
1179
1180         if (err)
1181                 goto err_free_eq;
1182
1183         for (port = 1; port <= dev->caps.num_ports; port++) {
1184                 err = mlx4_init_port_info(dev, port);
1185                 if (err)
1186                         goto err_port;
1187         }
1188
1189         err = mlx4_register_device(dev);
1190         if (err)
1191                 goto err_port;
1192
1193         mlx4_sense_init(dev);
1194         mlx4_start_sense(dev);
1195
1196         pci_set_drvdata(pdev, dev);
1197
1198         return 0;
1199
1200 err_port:
1201         for (--port; port >= 1; --port)
1202                 mlx4_cleanup_port_info(&priv->port[port]);
1203
1204         mlx4_cleanup_mcg_table(dev);
1205         mlx4_cleanup_qp_table(dev);
1206         mlx4_cleanup_srq_table(dev);
1207         mlx4_cleanup_cq_table(dev);
1208         mlx4_cmd_use_polling(dev);
1209         mlx4_cleanup_eq_table(dev);
1210         mlx4_cleanup_mr_table(dev);
1211         mlx4_cleanup_pd_table(dev);
1212         mlx4_cleanup_uar_table(dev);
1213
1214 err_free_eq:
1215         mlx4_free_eq_table(dev);
1216
1217 err_close:
1218         if (dev->flags & MLX4_FLAG_MSI_X)
1219                 pci_disable_msix(pdev);
1220
1221         mlx4_close_hca(dev);
1222
1223 err_cmd:
1224         mlx4_cmd_cleanup(dev);
1225
1226 err_free_dev:
1227         kfree(priv);
1228
1229 err_release_regions:
1230         pci_release_regions(pdev);
1231
1232 err_disable_pdev:
1233         pci_disable_device(pdev);
1234         pci_set_drvdata(pdev, NULL);
1235         return err;
1236 }
1237
1238 static int __devinit mlx4_init_one(struct pci_dev *pdev,
1239                                    const struct pci_device_id *id)
1240 {
1241         printk_once(KERN_INFO "%s", mlx4_version);
1242
1243         return __mlx4_init_one(pdev, id);
1244 }
1245
1246 static void mlx4_remove_one(struct pci_dev *pdev)
1247 {
1248         struct mlx4_dev  *dev  = pci_get_drvdata(pdev);
1249         struct mlx4_priv *priv = mlx4_priv(dev);
1250         int p;
1251
1252         if (dev) {
1253                 mlx4_stop_sense(dev);
1254                 mlx4_unregister_device(dev);
1255
1256                 for (p = 1; p <= dev->caps.num_ports; p++) {
1257                         mlx4_cleanup_port_info(&priv->port[p]);
1258                         mlx4_CLOSE_PORT(dev, p);
1259                 }
1260
1261                 mlx4_cleanup_mcg_table(dev);
1262                 mlx4_cleanup_qp_table(dev);
1263                 mlx4_cleanup_srq_table(dev);
1264                 mlx4_cleanup_cq_table(dev);
1265                 mlx4_cmd_use_polling(dev);
1266                 mlx4_cleanup_eq_table(dev);
1267                 mlx4_cleanup_mr_table(dev);
1268                 mlx4_cleanup_pd_table(dev);
1269
1270                 iounmap(priv->kar);
1271                 mlx4_uar_free(dev, &priv->driver_uar);
1272                 mlx4_cleanup_uar_table(dev);
1273                 mlx4_free_eq_table(dev);
1274                 mlx4_close_hca(dev);
1275                 mlx4_cmd_cleanup(dev);
1276
1277                 if (dev->flags & MLX4_FLAG_MSI_X)
1278                         pci_disable_msix(pdev);
1279
1280                 kfree(priv);
1281                 pci_release_regions(pdev);
1282                 pci_disable_device(pdev);
1283                 pci_set_drvdata(pdev, NULL);
1284         }
1285 }
1286
1287 int mlx4_restart_one(struct pci_dev *pdev)
1288 {
1289         mlx4_remove_one(pdev);
1290         return __mlx4_init_one(pdev, NULL);
1291 }
1292
1293 static DEFINE_PCI_DEVICE_TABLE(mlx4_pci_table) = {
1294         { PCI_VDEVICE(MELLANOX, 0x6340) }, /* MT25408 "Hermon" SDR */
1295         { PCI_VDEVICE(MELLANOX, 0x634a) }, /* MT25408 "Hermon" DDR */
1296         { PCI_VDEVICE(MELLANOX, 0x6354) }, /* MT25408 "Hermon" QDR */
1297         { PCI_VDEVICE(MELLANOX, 0x6732) }, /* MT25408 "Hermon" DDR PCIe gen2 */
1298         { PCI_VDEVICE(MELLANOX, 0x673c) }, /* MT25408 "Hermon" QDR PCIe gen2 */
1299         { PCI_VDEVICE(MELLANOX, 0x6368) }, /* MT25408 "Hermon" EN 10GigE */
1300         { PCI_VDEVICE(MELLANOX, 0x6750) }, /* MT25408 "Hermon" EN 10GigE PCIe gen2 */
1301         { PCI_VDEVICE(MELLANOX, 0x6372) }, /* MT25458 ConnectX EN 10GBASE-T 10GigE */
1302         { PCI_VDEVICE(MELLANOX, 0x675a) }, /* MT25458 ConnectX EN 10GBASE-T+Gen2 10GigE */
1303         { PCI_VDEVICE(MELLANOX, 0x6764) }, /* MT26468 ConnectX EN 10GigE PCIe gen2*/
1304         { PCI_VDEVICE(MELLANOX, 0x6746) }, /* MT26438 ConnectX EN 40GigE PCIe gen2 5GT/s */
1305         { PCI_VDEVICE(MELLANOX, 0x676e) }, /* MT26478 ConnectX2 40GigE PCIe gen2 */
1306         { PCI_VDEVICE(MELLANOX, 0x1002) }, /* MT25400 Family [ConnectX-2 Virtual Function] */
1307         { PCI_VDEVICE(MELLANOX, 0x1003) }, /* MT27500 Family [ConnectX-3] */
1308         { PCI_VDEVICE(MELLANOX, 0x1004) }, /* MT27500 Family [ConnectX-3 Virtual Function] */
1309         { PCI_VDEVICE(MELLANOX, 0x1005) }, /* MT27510 Family */
1310         { PCI_VDEVICE(MELLANOX, 0x1006) }, /* MT27511 Family */
1311         { PCI_VDEVICE(MELLANOX, 0x1007) }, /* MT27520 Family */
1312         { PCI_VDEVICE(MELLANOX, 0x1008) }, /* MT27521 Family */
1313         { PCI_VDEVICE(MELLANOX, 0x1009) }, /* MT27530 Family */
1314         { PCI_VDEVICE(MELLANOX, 0x100a) }, /* MT27531 Family */
1315         { PCI_VDEVICE(MELLANOX, 0x100b) }, /* MT27540 Family */
1316         { PCI_VDEVICE(MELLANOX, 0x100c) }, /* MT27541 Family */
1317         { PCI_VDEVICE(MELLANOX, 0x100d) }, /* MT27550 Family */
1318         { PCI_VDEVICE(MELLANOX, 0x100e) }, /* MT27551 Family */
1319         { PCI_VDEVICE(MELLANOX, 0x100f) }, /* MT27560 Family */
1320         { PCI_VDEVICE(MELLANOX, 0x1010) }, /* MT27561 Family */
1321         { 0, }
1322 };
1323
1324 MODULE_DEVICE_TABLE(pci, mlx4_pci_table);
1325
1326 static struct pci_driver mlx4_driver = {
1327         .name           = DRV_NAME,
1328         .id_table       = mlx4_pci_table,
1329         .probe          = mlx4_init_one,
1330         .remove         = __devexit_p(mlx4_remove_one)
1331 };
1332
1333 static int __init mlx4_verify_params(void)
1334 {
1335         if ((log_num_mac < 0) || (log_num_mac > 7)) {
1336                 pr_warning("mlx4_core: bad num_mac: %d\n", log_num_mac);
1337                 return -1;
1338         }
1339
1340         if ((log_num_vlan < 0) || (log_num_vlan > 7)) {
1341                 pr_warning("mlx4_core: bad num_vlan: %d\n", log_num_vlan);
1342                 return -1;
1343         }
1344
1345         if ((log_mtts_per_seg < 1) || (log_mtts_per_seg > 7)) {
1346                 pr_warning("mlx4_core: bad log_mtts_per_seg: %d\n", log_mtts_per_seg);
1347                 return -1;
1348         }
1349
1350         return 0;
1351 }
1352
1353 static int __init mlx4_init(void)
1354 {
1355         int ret;
1356
1357         if (mlx4_verify_params())
1358                 return -EINVAL;
1359
1360         mlx4_catas_init();
1361
1362         mlx4_wq = create_singlethread_workqueue("mlx4");
1363         if (!mlx4_wq)
1364                 return -ENOMEM;
1365
1366         ret = pci_register_driver(&mlx4_driver);
1367         return ret < 0 ? ret : 0;
1368 }
1369
1370 static void __exit mlx4_cleanup(void)
1371 {
1372         pci_unregister_driver(&mlx4_driver);
1373         destroy_workqueue(mlx4_wq);
1374 }
1375
1376 module_init(mlx4_init);
1377 module_exit(mlx4_cleanup);