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