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, 2014 Mellanox Technologies. All rights reserved.
5 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
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:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
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.
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
36 #include <linux/kmod.h>
38 * kmod.h must be included before module.h since it includes (indirectly) sys/module.h
39 * To use the FBSD macro sys/module.h should define MODULE_VERSION before linux/module does.
41 #include <linux/module.h>
42 #include <linux/errno.h>
43 #include <linux/pci.h>
44 #include <linux/dma-mapping.h>
45 #include <linux/slab.h>
46 #include <linux/io-mapping.h>
47 #include <linux/delay.h>
48 #include <linux/netdevice.h>
49 #include <linux/string.h>
52 #include <linux/mlx4/device.h>
53 #include <linux/mlx4/doorbell.h>
58 #include "mlx4_stats.h"
60 /* Mellanox ConnectX HCA low-level driver */
62 struct workqueue_struct *mlx4_wq;
64 #ifdef CONFIG_MLX4_DEBUG
66 int mlx4_debug_level = 0;
67 module_param_named(debug_level, mlx4_debug_level, int, 0644);
68 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
70 #endif /* CONFIG_MLX4_DEBUG */
75 module_param(msi_x, int, 0444);
76 MODULE_PARM_DESC(msi_x, "0 - don't use MSI-X, 1 - use MSI-X, >1 - limit number of MSI-X irqs to msi_x (non-SRIOV only)");
78 #else /* CONFIG_PCI_MSI */
82 #endif /* CONFIG_PCI_MSI */
84 static int enable_sys_tune = 0;
85 module_param(enable_sys_tune, int, 0444);
86 MODULE_PARM_DESC(enable_sys_tune, "Tune the cpu's for better performance (default 0)");
89 module_param_named(block_loopback, mlx4_blck_lb, int, 0644);
90 MODULE_PARM_DESC(block_loopback, "Block multicast loopback packets if > 0 "
94 BDF_STR_SIZE = 8, /* bb:dd.f- */
95 DBDF_STR_SIZE = 13 /* mmmm:bb:dd.f- */
112 struct mlx4_dbdf2val_lst dbdf2val;
115 static struct param_data num_vfs = {
118 .name = "num_vfs param",
121 .range = {0, MLX4_MAX_NUM_VF}
124 module_param_string(num_vfs, num_vfs.dbdf2val.str,
125 sizeof(num_vfs.dbdf2val.str), 0444);
126 MODULE_PARM_DESC(num_vfs,
127 "Either single value (e.g. '5') to define uniform num_vfs value for all devices functions\n"
128 "\t\tor a string to map device function numbers to their num_vfs values (e.g. '0000:04:00.0-5,002b:1c:0b.a-15').\n"
129 "\t\tHexadecimal digits for the device function (e.g. 002b:1c:0b.a) and decimal for num_vfs value (e.g. 15).");
131 static struct param_data probe_vf = {
134 .name = "probe_vf param",
137 .range = {0, MLX4_MAX_NUM_VF}
140 module_param_string(probe_vf, probe_vf.dbdf2val.str,
141 sizeof(probe_vf.dbdf2val.str), 0444);
142 MODULE_PARM_DESC(probe_vf,
143 "Either single value (e.g. '3') to define uniform number of VFs to probe by the pf driver for all devices functions\n"
144 "\t\tor a string to map device function numbers to their probe_vf values (e.g. '0000:04:00.0-3,002b:1c:0b.a-13').\n"
145 "\t\tHexadecimal digits for the device function (e.g. 002b:1c:0b.a) and decimal for probe_vf value (e.g. 13).");
147 int mlx4_log_num_mgm_entry_size = MLX4_DEFAULT_MGM_LOG_ENTRY_SIZE;
149 module_param_named(log_num_mgm_entry_size,
150 mlx4_log_num_mgm_entry_size, int, 0444);
151 MODULE_PARM_DESC(log_num_mgm_entry_size, "log mgm size, that defines the num"
152 " of qp per mcg, for example:"
153 " 10 gives 248.range: 7 <="
154 " log_num_mgm_entry_size <= 12."
155 " To activate device managed"
156 " flow steering when available, set to -1");
158 static int high_rate_steer;
159 module_param(high_rate_steer, int, 0444);
160 MODULE_PARM_DESC(high_rate_steer, "Enable steering mode for higher packet rate"
163 static int fast_drop;
164 module_param_named(fast_drop, fast_drop, int, 0444);
165 MODULE_PARM_DESC(fast_drop,
166 "Enable fast packet drop when no recieve WQEs are posted");
168 int mlx4_enable_64b_cqe_eqe = 1;
169 module_param_named(enable_64b_cqe_eqe, mlx4_enable_64b_cqe_eqe, int, 0644);
170 MODULE_PARM_DESC(enable_64b_cqe_eqe,
171 "Enable 64 byte CQEs/EQEs when the the FW supports this if non-zero (default: 1)");
173 #define HCA_GLOBAL_CAP_MASK 0
175 #define PF_CONTEXT_BEHAVIOUR_MASK MLX4_FUNC_CAP_64B_EQE_CQE
177 static char mlx4_version[] __devinitdata =
178 DRV_NAME ": Mellanox ConnectX VPI driver v"
179 DRV_VERSION " (" DRV_RELDATE ")\n";
181 static int log_num_mac = 7;
182 module_param_named(log_num_mac, log_num_mac, int, 0444);
183 MODULE_PARM_DESC(log_num_mac, "Log2 max number of MACs per ETH port (1-7)");
185 static int log_num_vlan;
186 module_param_named(log_num_vlan, log_num_vlan, int, 0444);
187 MODULE_PARM_DESC(log_num_vlan,
188 "(Obsolete) Log2 max number of VLANs per ETH port (0-7)");
189 /* Log2 max number of VLANs per ETH port (0-7) */
190 #define MLX4_LOG_NUM_VLANS 7
192 int log_mtts_per_seg = ilog2(1);
193 module_param_named(log_mtts_per_seg, log_mtts_per_seg, int, 0444);
194 MODULE_PARM_DESC(log_mtts_per_seg, "Log2 number of MTT entries per segment "
195 "(0-7) (default: 0)");
197 static struct param_data port_type_array = {
198 .id = PORT_TYPE_ARRAY,
200 .name = "port_type_array param",
202 .def_val = {MLX4_PORT_TYPE_ETH, MLX4_PORT_TYPE_ETH},
203 .range = {MLX4_PORT_TYPE_IB, MLX4_PORT_TYPE_NA}
206 module_param_string(port_type_array, port_type_array.dbdf2val.str,
207 sizeof(port_type_array.dbdf2val.str), 0444);
208 MODULE_PARM_DESC(port_type_array,
209 "Either pair of values (e.g. '1,2') to define uniform port1/port2 types configuration for all devices functions\n"
210 "\t\tor a string to map device function numbers to their pair of port types values (e.g. '0000:04:00.0-1;2,002b:1c:0b.a-1;1').\n"
211 "\t\tValid port types: 1-ib, 2-eth, 3-auto, 4-N/A\n"
212 "\t\tIn case that only one port is available use the N/A port type for port2 (e.g '1,4').");
215 struct mlx4_port_config {
216 struct list_head list;
217 enum mlx4_port_type port_type[MLX4_MAX_PORTS + 1];
218 struct pci_dev *pdev;
221 #define MLX4_LOG_NUM_MTT 20
222 /* We limit to 30 as of a bit map issue which uses int and not uint.
223 see mlx4_buddy_init -> bitmap_zero which gets int.
225 #define MLX4_MAX_LOG_NUM_MTT 30
226 static struct mlx4_profile mod_param_profile = {
233 .num_mtt_segs = 0, /* max(20, 2*MTTs for host memory)) */
236 module_param_named(log_num_qp, mod_param_profile.num_qp, int, 0444);
237 MODULE_PARM_DESC(log_num_qp, "log maximum number of QPs per HCA (default: 19)");
239 module_param_named(log_num_srq, mod_param_profile.num_srq, int, 0444);
240 MODULE_PARM_DESC(log_num_srq, "log maximum number of SRQs per HCA "
243 module_param_named(log_rdmarc_per_qp, mod_param_profile.rdmarc_per_qp, int,
245 MODULE_PARM_DESC(log_rdmarc_per_qp, "log number of RDMARC buffers per QP "
248 module_param_named(log_num_cq, mod_param_profile.num_cq, int, 0444);
249 MODULE_PARM_DESC(log_num_cq, "log maximum number of CQs per HCA (default: 16)");
251 module_param_named(log_num_mcg, mod_param_profile.num_mcg, int, 0444);
252 MODULE_PARM_DESC(log_num_mcg, "log maximum number of multicast groups per HCA "
255 module_param_named(log_num_mpt, mod_param_profile.num_mpt, int, 0444);
256 MODULE_PARM_DESC(log_num_mpt,
257 "log maximum number of memory protection table entries per "
258 "HCA (default: 19)");
260 module_param_named(log_num_mtt, mod_param_profile.num_mtt_segs, int, 0444);
261 MODULE_PARM_DESC(log_num_mtt,
262 "log maximum number of memory translation table segments per "
263 "HCA (default: max(20, 2*MTTs for register all of the host memory limited to 30))");
267 MLX4_IF_STATE_EXTENDED
270 static inline u64 dbdf_to_u64(int domain, int bus, int dev, int fn)
272 return (domain << 20) | (bus << 12) | (dev << 4) | fn;
275 static inline void pr_bdf_err(const char *dbdf, const char *pname)
277 pr_warn("mlx4_core: '%s' is not valid bdf in '%s'\n", dbdf, pname);
280 static inline void pr_val_err(const char *dbdf, const char *pname,
283 pr_warn("mlx4_core: value '%s' of bdf '%s' in '%s' is not valid\n"
287 static inline void pr_out_of_range_bdf(const char *dbdf, int val,
288 struct mlx4_dbdf2val_lst *dbdf2val)
290 pr_warn("mlx4_core: value %d in bdf '%s' of '%s' is out of its valid range (%d,%d)\n"
291 , val, dbdf, dbdf2val->name , dbdf2val->range.min,
292 dbdf2val->range.max);
295 static inline void pr_out_of_range(struct mlx4_dbdf2val_lst *dbdf2val)
297 pr_warn("mlx4_core: value of '%s' is out of its valid range (%d,%d)\n"
298 , dbdf2val->name , dbdf2val->range.min, dbdf2val->range.max);
301 static inline int is_in_range(int val, struct mlx4_range *r)
303 return (val >= r->min && val <= r->max);
306 static int update_defaults(struct param_data *pdata)
308 long int val[MLX4_MAX_BDF_VALS];
310 char *t, *p = pdata->dbdf2val.str;
314 if (!strlen(p) || strchr(p, ':') || strchr(p, '.') || strchr(p, ';'))
318 case PORT_TYPE_ARRAY:
320 if (!t || t == p || (t - p) > sizeof(sval))
324 strncpy(sval, p, val_len);
327 ret = kstrtol(sval, 0, &val[0]);
330 if (ret || !is_in_range(val[0], &pdata->dbdf2val.range)) {
331 pr_out_of_range(&pdata->dbdf2val);
335 ret = kstrtol(t + 1, 0, &val[1]);
338 if (ret || !is_in_range(val[1], &pdata->dbdf2val.range)) {
339 pr_out_of_range(&pdata->dbdf2val);
343 pdata->dbdf2val.tbl[0].val[0] = val[0];
344 pdata->dbdf2val.tbl[0].val[1] = val[1];
349 ret = kstrtol(p, 0, &val[0]);
352 if (ret || !is_in_range(val[0], &pdata->dbdf2val.range)) {
353 pr_out_of_range(&pdata->dbdf2val);
356 pdata->dbdf2val.tbl[0].val[0] = val[0];
359 pdata->dbdf2val.tbl[1].dbdf = MLX4_ENDOF_TBL;
364 int mlx4_fill_dbdf2val_tbl(struct mlx4_dbdf2val_lst *dbdf2val_lst)
366 int domain, bus, dev, fn;
372 int j, k, str_size, i = 1;
375 p = dbdf2val_lst->str;
377 for (j = 0; j < dbdf2val_lst->num_vals; j++)
378 dbdf2val_lst->tbl[0].val[j] = dbdf2val_lst->def_val[j];
379 dbdf2val_lst->tbl[1].dbdf = MLX4_ENDOF_TBL;
381 str_size = strlen(dbdf2val_lst->str);
387 prfx_size = BDF_STR_SIZE;
389 strncpy(sbdf, p, prfx_size);
390 domain = DEFAULT_DOMAIN;
391 if (sscanf(sbdf, "%02x:%02x.%x-", &bus, &dev, &fn) != 3) {
392 prfx_size = DBDF_STR_SIZE;
394 strncpy(sbdf, p, prfx_size);
395 if (sscanf(sbdf, "%04x:%02x:%02x.%x-", &domain, &bus,
397 pr_bdf_err(sbdf, dbdf2val_lst->name);
400 sprintf(tmp, "%04x:%02x:%02x.%x-", domain, bus, dev,
403 sprintf(tmp, "%02x:%02x.%x-", bus, dev, fn);
406 if (strnicmp(sbdf, tmp, sizeof(tmp))) {
407 pr_bdf_err(sbdf, dbdf2val_lst->name);
411 dbdf = dbdf_to_u64(domain, bus, dev, fn);
413 for (j = 1; j < i; j++)
414 if (dbdf2val_lst->tbl[j].dbdf == dbdf) {
415 pr_warn("mlx4_core: in '%s', %s appears multiple times\n"
416 , dbdf2val_lst->name, sbdf);
420 if (i >= MLX4_DEVS_TBL_SIZE) {
421 pr_warn("mlx4_core: Too many devices in '%s'\n"
422 , dbdf2val_lst->name);
428 t = t ? t : p + strlen(p);
430 pr_val_err(sbdf, dbdf2val_lst->name, "");
434 for (k = 0; k < dbdf2val_lst->num_vals; k++) {
440 v = (k == dbdf2val_lst->num_vals - 1) ? t : strchr(p, vsep);
441 if (!v || v > t || v == p || (v - p) > sizeof(sval)) {
442 pr_val_err(sbdf, dbdf2val_lst->name, p);
446 strncpy(sval, p, val_len);
449 ret = kstrtol(sval, 0, &val);
452 pr_warn("mlx4_core: too many vals in bdf '%s' of '%s'\n"
453 , sbdf, dbdf2val_lst->name);
455 pr_val_err(sbdf, dbdf2val_lst->name,
459 if (!is_in_range(val, &dbdf2val_lst->range)) {
460 pr_out_of_range_bdf(sbdf, val, dbdf2val_lst);
464 dbdf2val_lst->tbl[i].val[k] = val;
470 dbdf2val_lst->tbl[i].dbdf = dbdf;
473 pr_warn("mlx4_core: expect separator '%c' before '%s' in '%s'\n"
474 , sep, p, dbdf2val_lst->name);
480 if (i < MLX4_DEVS_TBL_SIZE)
481 dbdf2val_lst->tbl[i].dbdf = MLX4_ENDOF_TBL;
487 dbdf2val_lst->tbl[1].dbdf = MLX4_ENDOF_TBL;
488 pr_warn("mlx4_core: The value of '%s' is incorrect. The value is discarded!\n"
489 , dbdf2val_lst->name);
493 EXPORT_SYMBOL(mlx4_fill_dbdf2val_tbl);
495 int mlx4_get_val(struct mlx4_dbdf2val *tbl, struct pci_dev *pdev, int idx,
501 *val = tbl[0].val[idx];
505 dbdf = dbdf_to_u64(pci_get_domain(pdev->dev.bsddev), pci_get_bus(pdev->dev.bsddev),
506 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
508 while ((i < MLX4_DEVS_TBL_SIZE) && (tbl[i].dbdf != MLX4_ENDOF_TBL)) {
509 if (tbl[i].dbdf == dbdf) {
510 *val = tbl[i].val[idx];
518 EXPORT_SYMBOL(mlx4_get_val);
520 static void process_mod_param_profile(struct mlx4_profile *profile)
524 TUNABLE_ULONG_FETCH("hw.realmem", (u_long *) &hwphyssz);
526 profile->num_qp = 1 << mod_param_profile.num_qp;
527 profile->num_srq = 1 << mod_param_profile.num_srq;
528 profile->rdmarc_per_qp = 1 << mod_param_profile.rdmarc_per_qp;
529 profile->num_cq = 1 << mod_param_profile.num_cq;
530 profile->num_mcg = 1 << mod_param_profile.num_mcg;
531 profile->num_mpt = 1 << mod_param_profile.num_mpt;
533 * We want to scale the number of MTTs with the size of the
534 * system memory, since it makes sense to register a lot of
535 * memory on a system with a lot of memory. As a heuristic,
536 * make sure we have enough MTTs to register twice the system
537 * memory (with PAGE_SIZE entries).
539 * This number has to be a power of two and fit into 32 bits
540 * due to device limitations. We cap this at 2^30 as of bit map
541 * limitation to work with int instead of uint (mlx4_buddy_init -> bitmap_zero)
542 * That limits us to 4TB of memory registration per HCA with
543 * 4KB pages, which is probably OK for the next few months.
545 if (mod_param_profile.num_mtt_segs)
546 profile->num_mtt_segs = 1 << mod_param_profile.num_mtt_segs;
548 profile->num_mtt_segs =
549 roundup_pow_of_two(max_t(unsigned,
550 1 << (MLX4_LOG_NUM_MTT - log_mtts_per_seg),
552 (MLX4_MAX_LOG_NUM_MTT -
555 >> log_mtts_per_seg)));
556 /* set the actual value, so it will be reflected to the user
558 mod_param_profile.num_mtt_segs = ilog2(profile->num_mtt_segs);
562 int mlx4_check_port_params(struct mlx4_dev *dev,
563 enum mlx4_port_type *port_type)
567 for (i = 0; i < dev->caps.num_ports - 1; i++) {
568 if (port_type[i] != port_type[i + 1]) {
569 if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP)) {
570 mlx4_err(dev, "Only same port types supported "
571 "on this HCA, aborting.\n");
577 for (i = 0; i < dev->caps.num_ports; i++) {
578 if (!(port_type[i] & dev->caps.supported_type[i+1])) {
579 mlx4_err(dev, "Requested port type for port %d is not "
580 "supported on this HCA\n", i + 1);
587 static void mlx4_set_port_mask(struct mlx4_dev *dev)
591 for (i = 1; i <= dev->caps.num_ports; ++i)
592 dev->caps.port_mask[i] = dev->caps.port_type[i];
595 static int mlx4_dev_cap(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap)
600 err = mlx4_QUERY_DEV_CAP(dev, dev_cap);
602 mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
606 if (dev_cap->min_page_sz > PAGE_SIZE) {
607 mlx4_err(dev, "HCA minimum page size of %d bigger than "
608 "kernel PAGE_SIZE of %d, aborting.\n",
609 dev_cap->min_page_sz, (int)PAGE_SIZE);
612 if (dev_cap->num_ports > MLX4_MAX_PORTS) {
613 mlx4_err(dev, "HCA has %d ports, but we only support %d, "
615 dev_cap->num_ports, MLX4_MAX_PORTS);
619 if (dev_cap->uar_size > pci_resource_len(dev->pdev, 2)) {
620 mlx4_err(dev, "HCA reported UAR size of 0x%x bigger than "
621 "PCI resource 2 size of 0x%llx, aborting.\n",
623 (unsigned long long) pci_resource_len(dev->pdev, 2));
627 dev->caps.num_ports = dev_cap->num_ports;
628 dev->phys_caps.num_phys_eqs = MLX4_MAX_EQ_NUM;
629 for (i = 1; i <= dev->caps.num_ports; ++i) {
630 dev->caps.vl_cap[i] = dev_cap->max_vl[i];
631 dev->caps.ib_mtu_cap[i] = dev_cap->ib_mtu[i];
632 dev->phys_caps.gid_phys_table_len[i] = dev_cap->max_gids[i];
633 dev->phys_caps.pkey_phys_table_len[i] = dev_cap->max_pkeys[i];
634 /* set gid and pkey table operating lengths by default
635 * to non-sriov values */
636 dev->caps.gid_table_len[i] = dev_cap->max_gids[i];
637 dev->caps.pkey_table_len[i] = dev_cap->max_pkeys[i];
638 dev->caps.port_width_cap[i] = dev_cap->max_port_width[i];
639 dev->caps.eth_mtu_cap[i] = dev_cap->eth_mtu[i];
640 dev->caps.def_mac[i] = dev_cap->def_mac[i];
641 dev->caps.supported_type[i] = dev_cap->supported_port_types[i];
642 dev->caps.suggested_type[i] = dev_cap->suggested_type[i];
643 dev->caps.default_sense[i] = dev_cap->default_sense[i];
644 dev->caps.trans_type[i] = dev_cap->trans_type[i];
645 dev->caps.vendor_oui[i] = dev_cap->vendor_oui[i];
646 dev->caps.wavelength[i] = dev_cap->wavelength[i];
647 dev->caps.trans_code[i] = dev_cap->trans_code[i];
650 dev->caps.uar_page_size = PAGE_SIZE;
651 dev->caps.num_uars = dev_cap->uar_size / PAGE_SIZE;
652 dev->caps.local_ca_ack_delay = dev_cap->local_ca_ack_delay;
653 dev->caps.bf_reg_size = dev_cap->bf_reg_size;
654 dev->caps.bf_regs_per_page = dev_cap->bf_regs_per_page;
655 dev->caps.max_sq_sg = dev_cap->max_sq_sg;
656 dev->caps.max_rq_sg = dev_cap->max_rq_sg;
657 dev->caps.max_wqes = dev_cap->max_qp_sz;
658 dev->caps.max_qp_init_rdma = dev_cap->max_requester_per_qp;
659 dev->caps.max_srq_wqes = dev_cap->max_srq_sz;
660 dev->caps.max_srq_sge = dev_cap->max_rq_sg - 1;
661 dev->caps.reserved_srqs = dev_cap->reserved_srqs;
662 dev->caps.max_sq_desc_sz = dev_cap->max_sq_desc_sz;
663 dev->caps.max_rq_desc_sz = dev_cap->max_rq_desc_sz;
665 * Subtract 1 from the limit because we need to allocate a
666 * spare CQE to enable resizing the CQ
668 dev->caps.max_cqes = dev_cap->max_cq_sz - 1;
669 dev->caps.reserved_cqs = dev_cap->reserved_cqs;
670 dev->caps.reserved_eqs = dev_cap->reserved_eqs;
671 dev->caps.reserved_mtts = dev_cap->reserved_mtts;
672 dev->caps.reserved_mrws = dev_cap->reserved_mrws;
674 /* The first 128 UARs are used for EQ doorbells */
675 dev->caps.reserved_uars = max_t(int, 128, dev_cap->reserved_uars);
676 dev->caps.reserved_pds = dev_cap->reserved_pds;
677 dev->caps.reserved_xrcds = (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) ?
678 dev_cap->reserved_xrcds : 0;
679 dev->caps.max_xrcds = (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) ?
680 dev_cap->max_xrcds : 0;
681 dev->caps.mtt_entry_sz = dev_cap->mtt_entry_sz;
683 dev->caps.max_msg_sz = dev_cap->max_msg_sz;
684 dev->caps.page_size_cap = ~(u32) (dev_cap->min_page_sz - 1);
685 dev->caps.flags = dev_cap->flags;
686 dev->caps.flags2 = dev_cap->flags2;
687 dev->caps.bmme_flags = dev_cap->bmme_flags;
688 dev->caps.reserved_lkey = dev_cap->reserved_lkey;
689 dev->caps.stat_rate_support = dev_cap->stat_rate_support;
690 dev->caps.cq_timestamp = dev_cap->timestamp_support;
691 dev->caps.max_gso_sz = dev_cap->max_gso_sz;
692 dev->caps.max_rss_tbl_sz = dev_cap->max_rss_tbl_sz;
694 /* Sense port always allowed on supported devices for ConnectX-1 and -2 */
695 if (mlx4_priv(dev)->pci_dev_data & MLX4_PCI_DEV_FORCE_SENSE_PORT)
696 dev->caps.flags |= MLX4_DEV_CAP_FLAG_SENSE_SUPPORT;
697 /* Don't do sense port on multifunction devices (for now at least) */
698 if (mlx4_is_mfunc(dev))
699 dev->caps.flags &= ~MLX4_DEV_CAP_FLAG_SENSE_SUPPORT;
701 dev->caps.log_num_macs = log_num_mac;
702 dev->caps.log_num_vlans = MLX4_LOG_NUM_VLANS;
704 dev->caps.fast_drop = fast_drop ?
705 !!(dev->caps.flags & MLX4_DEV_CAP_FLAG_FAST_DROP) :
708 for (i = 1; i <= dev->caps.num_ports; ++i) {
709 dev->caps.port_type[i] = MLX4_PORT_TYPE_NONE;
710 if (dev->caps.supported_type[i]) {
711 /* if only ETH is supported - assign ETH */
712 if (dev->caps.supported_type[i] == MLX4_PORT_TYPE_ETH)
713 dev->caps.port_type[i] = MLX4_PORT_TYPE_ETH;
714 /* if only IB is supported, assign IB */
715 else if (dev->caps.supported_type[i] ==
717 dev->caps.port_type[i] = MLX4_PORT_TYPE_IB;
720 * if IB and ETH are supported, we set the port
721 * type according to user selection of port type;
722 * if there is no user selection, take the FW hint
725 mlx4_get_val(port_type_array.dbdf2val.tbl,
726 pci_physfn(dev->pdev), i - 1,
728 if (pta == MLX4_PORT_TYPE_NONE) {
729 dev->caps.port_type[i] = dev->caps.suggested_type[i] ?
730 MLX4_PORT_TYPE_ETH : MLX4_PORT_TYPE_IB;
731 } else if (pta == MLX4_PORT_TYPE_NA) {
732 mlx4_err(dev, "Port %d is valid port. "
733 "It is not allowed to configure its type to N/A(%d)\n",
734 i, MLX4_PORT_TYPE_NA);
737 dev->caps.port_type[i] = pta;
742 * Link sensing is allowed on the port if 3 conditions are true:
743 * 1. Both protocols are supported on the port.
744 * 2. Different types are supported on the port
745 * 3. FW declared that it supports link sensing
747 mlx4_priv(dev)->sense.sense_allowed[i] =
748 ((dev->caps.supported_type[i] == MLX4_PORT_TYPE_AUTO) &&
749 (dev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP) &&
750 (dev->caps.flags & MLX4_DEV_CAP_FLAG_SENSE_SUPPORT));
752 /* Disablling auto sense for default Eth ports support */
753 mlx4_priv(dev)->sense.sense_allowed[i] = 0;
756 * If "default_sense" bit is set, we move the port to "AUTO" mode
757 * and perform sense_port FW command to try and set the correct
758 * port type from beginning
760 if (mlx4_priv(dev)->sense.sense_allowed[i] && dev->caps.default_sense[i]) {
761 enum mlx4_port_type sensed_port = MLX4_PORT_TYPE_NONE;
762 dev->caps.possible_type[i] = MLX4_PORT_TYPE_AUTO;
763 mlx4_SENSE_PORT(dev, i, &sensed_port);
764 if (sensed_port != MLX4_PORT_TYPE_NONE)
765 dev->caps.port_type[i] = sensed_port;
767 dev->caps.possible_type[i] = dev->caps.port_type[i];
770 if (dev->caps.log_num_macs > dev_cap->log_max_macs[i]) {
771 dev->caps.log_num_macs = dev_cap->log_max_macs[i];
772 mlx4_warn(dev, "Requested number of MACs is too much "
773 "for port %d, reducing to %d.\n",
774 i, 1 << dev->caps.log_num_macs);
776 if (dev->caps.log_num_vlans > dev_cap->log_max_vlans[i]) {
777 dev->caps.log_num_vlans = dev_cap->log_max_vlans[i];
778 mlx4_warn(dev, "Requested number of VLANs is too much "
779 "for port %d, reducing to %d.\n",
780 i, 1 << dev->caps.log_num_vlans);
784 dev->caps.max_basic_counters = dev_cap->max_basic_counters;
785 dev->caps.max_extended_counters = dev_cap->max_extended_counters;
786 /* support extended counters if available */
787 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_COUNTERS_EXT)
788 dev->caps.max_counters = dev->caps.max_extended_counters;
790 dev->caps.max_counters = dev->caps.max_basic_counters;
792 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW] = dev_cap->reserved_qps;
793 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_ETH_ADDR] =
794 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_ADDR] =
795 (1 << dev->caps.log_num_macs) *
796 (1 << dev->caps.log_num_vlans) *
798 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_EXCH] = MLX4_NUM_FEXCH;
800 dev->caps.reserved_qps = dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW] +
801 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_ETH_ADDR] +
802 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_ADDR] +
803 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_EXCH];
805 dev->caps.sync_qp = dev_cap->sync_qp;
806 if (dev->pdev->device == 0x1003)
807 dev->caps.cq_flags |= MLX4_DEV_CAP_CQ_FLAG_IO;
809 dev->caps.sqp_demux = (mlx4_is_master(dev)) ? MLX4_MAX_NUM_SLAVES : 0;
811 if (!mlx4_enable_64b_cqe_eqe && !mlx4_is_slave(dev)) {
813 (MLX4_DEV_CAP_FLAG_64B_CQE | MLX4_DEV_CAP_FLAG_64B_EQE)) {
814 mlx4_warn(dev, "64B EQEs/CQEs supported by the device but not enabled\n");
815 dev->caps.flags &= ~MLX4_DEV_CAP_FLAG_64B_CQE;
816 dev->caps.flags &= ~MLX4_DEV_CAP_FLAG_64B_EQE;
820 if ((dev->caps.flags &
821 (MLX4_DEV_CAP_FLAG_64B_CQE | MLX4_DEV_CAP_FLAG_64B_EQE)) &&
823 dev->caps.function_caps |= MLX4_FUNC_CAP_64B_EQE_CQE;
825 if (!mlx4_is_slave(dev)) {
826 for (i = 0; i < dev->caps.num_ports; ++i)
827 dev->caps.def_counter_index[i] = i << 1;
829 dev->caps.alloc_res_qp_mask =
830 (dev->caps.bf_reg_size ? MLX4_RESERVE_ETH_BF_QP : 0);
832 dev->caps.alloc_res_qp_mask = 0;
837 /*The function checks if there are live vf, return the num of them*/
838 static int mlx4_how_many_lives_vf(struct mlx4_dev *dev)
840 struct mlx4_priv *priv = mlx4_priv(dev);
841 struct mlx4_slave_state *s_state;
845 for (i = 1/*the ppf is 0*/; i < dev->num_slaves; ++i) {
846 s_state = &priv->mfunc.master.slave_state[i];
847 if (s_state->active && s_state->last_cmd !=
848 MLX4_COMM_CMD_RESET) {
849 mlx4_warn(dev, "%s: slave: %d is still active\n",
857 int mlx4_get_parav_qkey(struct mlx4_dev *dev, u32 qpn, u32 *qkey)
859 u32 qk = MLX4_RESERVED_QKEY_BASE;
861 if (qpn >= dev->phys_caps.base_tunnel_sqpn + 8 * MLX4_MFUNC_MAX ||
862 qpn < dev->phys_caps.base_proxy_sqpn)
865 if (qpn >= dev->phys_caps.base_tunnel_sqpn)
867 qk += qpn - dev->phys_caps.base_tunnel_sqpn;
869 qk += qpn - dev->phys_caps.base_proxy_sqpn;
873 EXPORT_SYMBOL(mlx4_get_parav_qkey);
875 void mlx4_sync_pkey_table(struct mlx4_dev *dev, int slave, int port, int i, int val)
877 struct mlx4_priv *priv = container_of(dev, struct mlx4_priv, dev);
879 if (!mlx4_is_master(dev))
882 priv->virt2phys_pkey[slave][port - 1][i] = val;
884 EXPORT_SYMBOL(mlx4_sync_pkey_table);
886 void mlx4_put_slave_node_guid(struct mlx4_dev *dev, int slave, __be64 guid)
888 struct mlx4_priv *priv = container_of(dev, struct mlx4_priv, dev);
890 if (!mlx4_is_master(dev))
893 priv->slave_node_guids[slave] = guid;
895 EXPORT_SYMBOL(mlx4_put_slave_node_guid);
897 __be64 mlx4_get_slave_node_guid(struct mlx4_dev *dev, int slave)
899 struct mlx4_priv *priv = container_of(dev, struct mlx4_priv, dev);
901 if (!mlx4_is_master(dev))
904 return priv->slave_node_guids[slave];
906 EXPORT_SYMBOL(mlx4_get_slave_node_guid);
908 int mlx4_is_slave_active(struct mlx4_dev *dev, int slave)
910 struct mlx4_priv *priv = mlx4_priv(dev);
911 struct mlx4_slave_state *s_slave;
913 if (!mlx4_is_master(dev))
916 s_slave = &priv->mfunc.master.slave_state[slave];
917 return !!s_slave->active;
919 EXPORT_SYMBOL(mlx4_is_slave_active);
921 static void slave_adjust_steering_mode(struct mlx4_dev *dev,
922 struct mlx4_dev_cap *dev_cap,
923 struct mlx4_init_hca_param *hca_param)
925 dev->caps.steering_mode = hca_param->steering_mode;
926 if (dev->caps.steering_mode == MLX4_STEERING_MODE_DEVICE_MANAGED)
927 dev->caps.num_qp_per_mgm = dev_cap->fs_max_num_qp_per_entry;
929 dev->caps.num_qp_per_mgm =
930 4 * ((1 << hca_param->log_mc_entry_sz)/16 - 2);
932 mlx4_dbg(dev, "Steering mode is: %s\n",
933 mlx4_steering_mode_str(dev->caps.steering_mode));
936 static int mlx4_slave_cap(struct mlx4_dev *dev)
940 struct mlx4_dev_cap dev_cap;
941 struct mlx4_func_cap func_cap;
942 struct mlx4_init_hca_param hca_param;
945 memset(&hca_param, 0, sizeof(hca_param));
946 err = mlx4_QUERY_HCA(dev, &hca_param);
948 mlx4_err(dev, "QUERY_HCA command failed, aborting.\n");
952 /*fail if the hca has an unknown capability */
953 if ((hca_param.global_caps | HCA_GLOBAL_CAP_MASK) !=
954 HCA_GLOBAL_CAP_MASK) {
955 mlx4_err(dev, "Unknown hca global capabilities\n");
959 mlx4_log_num_mgm_entry_size = hca_param.log_mc_entry_sz;
961 dev->caps.hca_core_clock = hca_param.hca_core_clock;
963 memset(&dev_cap, 0, sizeof(dev_cap));
964 dev->caps.max_qp_dest_rdma = 1 << hca_param.log_rd_per_qp;
965 err = mlx4_dev_cap(dev, &dev_cap);
967 mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
971 err = mlx4_QUERY_FW(dev);
973 mlx4_err(dev, "QUERY_FW command failed: could not get FW version.\n");
975 if (!hca_param.mw_enable) {
976 dev->caps.flags &= ~MLX4_DEV_CAP_FLAG_MEM_WINDOW;
977 dev->caps.bmme_flags &= ~MLX4_BMME_FLAG_TYPE_2_WIN;
980 page_size = ~dev->caps.page_size_cap + 1;
981 mlx4_warn(dev, "HCA minimum page size:%d\n", page_size);
982 if (page_size > PAGE_SIZE) {
983 mlx4_err(dev, "HCA minimum page size of %d bigger than "
984 "kernel PAGE_SIZE of %d, aborting.\n",
985 page_size, (int)PAGE_SIZE);
989 /* slave gets uar page size from QUERY_HCA fw command */
990 dev->caps.uar_page_size = 1 << (hca_param.uar_page_sz + 12);
992 /* TODO: relax this assumption */
993 if (dev->caps.uar_page_size != PAGE_SIZE) {
994 mlx4_err(dev, "UAR size:%d != kernel PAGE_SIZE of %d\n",
995 dev->caps.uar_page_size, (int)PAGE_SIZE);
999 memset(&func_cap, 0, sizeof(func_cap));
1000 err = mlx4_QUERY_FUNC_CAP(dev, 0, &func_cap);
1002 mlx4_err(dev, "QUERY_FUNC_CAP general command failed, aborting (%d).\n",
1007 if ((func_cap.pf_context_behaviour | PF_CONTEXT_BEHAVIOUR_MASK) !=
1008 PF_CONTEXT_BEHAVIOUR_MASK) {
1009 mlx4_err(dev, "Unknown pf context behaviour\n");
1013 dev->caps.num_ports = func_cap.num_ports;
1014 dev->quotas.qp = func_cap.qp_quota;
1015 dev->quotas.srq = func_cap.srq_quota;
1016 dev->quotas.cq = func_cap.cq_quota;
1017 dev->quotas.mpt = func_cap.mpt_quota;
1018 dev->quotas.mtt = func_cap.mtt_quota;
1019 dev->caps.num_qps = 1 << hca_param.log_num_qps;
1020 dev->caps.num_srqs = 1 << hca_param.log_num_srqs;
1021 dev->caps.num_cqs = 1 << hca_param.log_num_cqs;
1022 dev->caps.num_mpts = 1 << hca_param.log_mpt_sz;
1023 dev->caps.num_eqs = func_cap.max_eq;
1024 dev->caps.reserved_eqs = func_cap.reserved_eq;
1025 dev->caps.num_pds = MLX4_NUM_PDS;
1026 dev->caps.num_mgms = 0;
1027 dev->caps.num_amgms = 0;
1029 if (dev->caps.num_ports > MLX4_MAX_PORTS) {
1030 mlx4_err(dev, "HCA has %d ports, but we only support %d, "
1031 "aborting.\n", dev->caps.num_ports, MLX4_MAX_PORTS);
1035 dev->caps.qp0_tunnel = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
1036 dev->caps.qp0_proxy = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
1037 dev->caps.qp1_tunnel = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
1038 dev->caps.qp1_proxy = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
1040 if (!dev->caps.qp0_tunnel || !dev->caps.qp0_proxy ||
1041 !dev->caps.qp1_tunnel || !dev->caps.qp1_proxy) {
1046 for (i = 1; i <= dev->caps.num_ports; ++i) {
1047 err = mlx4_QUERY_FUNC_CAP(dev, (u32) i, &func_cap);
1049 mlx4_err(dev, "QUERY_FUNC_CAP port command failed for"
1050 " port %d, aborting (%d).\n", i, err);
1053 dev->caps.qp0_tunnel[i - 1] = func_cap.qp0_tunnel_qpn;
1054 dev->caps.qp0_proxy[i - 1] = func_cap.qp0_proxy_qpn;
1055 dev->caps.qp1_tunnel[i - 1] = func_cap.qp1_tunnel_qpn;
1056 dev->caps.qp1_proxy[i - 1] = func_cap.qp1_proxy_qpn;
1057 dev->caps.def_counter_index[i - 1] = func_cap.def_counter_index;
1059 dev->caps.port_mask[i] = dev->caps.port_type[i];
1060 err = mlx4_get_slave_pkey_gid_tbl_len(dev, i,
1061 &dev->caps.gid_table_len[i],
1062 &dev->caps.pkey_table_len[i]);
1067 if (dev->caps.uar_page_size * (dev->caps.num_uars -
1068 dev->caps.reserved_uars) >
1069 pci_resource_len(dev->pdev, 2)) {
1070 mlx4_err(dev, "HCA reported UAR region size of 0x%x bigger than "
1071 "PCI resource 2 size of 0x%llx, aborting.\n",
1072 dev->caps.uar_page_size * dev->caps.num_uars,
1073 (unsigned long long) pci_resource_len(dev->pdev, 2));
1078 if (hca_param.dev_cap_enabled & MLX4_DEV_CAP_64B_EQE_ENABLED) {
1079 dev->caps.eqe_size = 64;
1080 dev->caps.eqe_factor = 1;
1082 dev->caps.eqe_size = 32;
1083 dev->caps.eqe_factor = 0;
1086 if (hca_param.dev_cap_enabled & MLX4_DEV_CAP_64B_CQE_ENABLED) {
1087 dev->caps.cqe_size = 64;
1088 dev->caps.userspace_caps |= MLX4_USER_DEV_CAP_64B_CQE;
1090 dev->caps.cqe_size = 32;
1093 dev->caps.flags2 &= ~MLX4_DEV_CAP_FLAG2_TS;
1094 mlx4_warn(dev, "Timestamping is not supported in slave mode.\n");
1096 slave_adjust_steering_mode(dev, &dev_cap, &hca_param);
1098 if (func_cap.extra_flags & MLX4_QUERY_FUNC_FLAGS_BF_RES_QP &&
1099 dev->caps.bf_reg_size)
1100 dev->caps.alloc_res_qp_mask |= MLX4_RESERVE_ETH_BF_QP;
1105 kfree(dev->caps.qp0_tunnel);
1106 kfree(dev->caps.qp0_proxy);
1107 kfree(dev->caps.qp1_tunnel);
1108 kfree(dev->caps.qp1_proxy);
1109 dev->caps.qp0_tunnel = dev->caps.qp0_proxy =
1110 dev->caps.qp1_tunnel = dev->caps.qp1_proxy = NULL;
1115 static void mlx4_request_modules(struct mlx4_dev *dev)
1118 int has_ib_port = false;
1119 int has_eth_port = false;
1120 #define EN_DRV_NAME "mlx4_en"
1121 #define IB_DRV_NAME "mlx4_ib"
1123 for (port = 1; port <= dev->caps.num_ports; port++) {
1124 if (dev->caps.port_type[port] == MLX4_PORT_TYPE_IB)
1126 else if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
1127 has_eth_port = true;
1131 request_module_nowait(IB_DRV_NAME);
1133 request_module_nowait(EN_DRV_NAME);
1137 * Change the port configuration of the device.
1138 * Every user of this function must hold the port mutex.
1140 int mlx4_change_port_types(struct mlx4_dev *dev,
1141 enum mlx4_port_type *port_types)
1147 for (port = 0; port < dev->caps.num_ports; port++) {
1148 /* Change the port type only if the new type is different
1149 * from the current, and not set to Auto */
1150 if (port_types[port] != dev->caps.port_type[port + 1])
1154 mlx4_unregister_device(dev);
1155 for (port = 1; port <= dev->caps.num_ports; port++) {
1156 mlx4_CLOSE_PORT(dev, port);
1157 dev->caps.port_type[port] = port_types[port - 1];
1158 err = mlx4_SET_PORT(dev, port, -1);
1160 mlx4_err(dev, "Failed to set port %d, "
1161 "aborting\n", port);
1165 mlx4_set_port_mask(dev);
1166 err = mlx4_register_device(dev);
1168 mlx4_err(dev, "Failed to register device\n");
1171 mlx4_request_modules(dev);
1178 static ssize_t show_port_type(struct device *dev,
1179 struct device_attribute *attr,
1182 struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
1184 struct mlx4_dev *mdev = info->dev;
1188 (mdev->caps.port_type[info->port] == MLX4_PORT_TYPE_IB) ?
1190 if (mdev->caps.possible_type[info->port] == MLX4_PORT_TYPE_AUTO)
1191 sprintf(buf, "auto (%s)\n", type);
1193 sprintf(buf, "%s\n", type);
1198 static ssize_t set_port_type(struct device *dev,
1199 struct device_attribute *attr,
1200 const char *buf, size_t count)
1202 struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
1204 struct mlx4_dev *mdev = info->dev;
1205 struct mlx4_priv *priv = mlx4_priv(mdev);
1206 enum mlx4_port_type types[MLX4_MAX_PORTS];
1207 enum mlx4_port_type new_types[MLX4_MAX_PORTS];
1211 if (!strcmp(buf, "ib\n"))
1212 info->tmp_type = MLX4_PORT_TYPE_IB;
1213 else if (!strcmp(buf, "eth\n"))
1214 info->tmp_type = MLX4_PORT_TYPE_ETH;
1215 else if (!strcmp(buf, "auto\n"))
1216 info->tmp_type = MLX4_PORT_TYPE_AUTO;
1218 mlx4_err(mdev, "%s is not supported port type\n", buf);
1222 if ((info->tmp_type & mdev->caps.supported_type[info->port]) !=
1224 mlx4_err(mdev, "Requested port type for port %d is not supported on this HCA\n",
1229 mlx4_stop_sense(mdev);
1230 mutex_lock(&priv->port_mutex);
1231 /* Possible type is always the one that was delivered */
1232 mdev->caps.possible_type[info->port] = info->tmp_type;
1234 for (i = 0; i < mdev->caps.num_ports; i++) {
1235 types[i] = priv->port[i+1].tmp_type ? priv->port[i+1].tmp_type :
1236 mdev->caps.possible_type[i+1];
1237 if (types[i] == MLX4_PORT_TYPE_AUTO)
1238 types[i] = mdev->caps.port_type[i+1];
1241 if (!(mdev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP) &&
1242 !(mdev->caps.flags & MLX4_DEV_CAP_FLAG_SENSE_SUPPORT)) {
1243 for (i = 1; i <= mdev->caps.num_ports; i++) {
1244 if (mdev->caps.possible_type[i] == MLX4_PORT_TYPE_AUTO) {
1245 mdev->caps.possible_type[i] = mdev->caps.port_type[i];
1251 mlx4_err(mdev, "Auto sensing is not supported on this HCA. "
1252 "Set only 'eth' or 'ib' for both ports "
1253 "(should be the same)\n");
1257 mlx4_do_sense_ports(mdev, new_types, types);
1259 err = mlx4_check_port_params(mdev, new_types);
1263 /* We are about to apply the changes after the configuration
1264 * was verified, no need to remember the temporary types
1266 for (i = 0; i < mdev->caps.num_ports; i++)
1267 priv->port[i + 1].tmp_type = 0;
1269 err = mlx4_change_port_types(mdev, new_types);
1272 mlx4_start_sense(mdev);
1273 mutex_unlock(&priv->port_mutex);
1274 return err ? err : count;
1285 static inline int int_to_ibta_mtu(int mtu)
1288 case 256: return IB_MTU_256;
1289 case 512: return IB_MTU_512;
1290 case 1024: return IB_MTU_1024;
1291 case 2048: return IB_MTU_2048;
1292 case 4096: return IB_MTU_4096;
1297 static inline int ibta_mtu_to_int(enum ibta_mtu mtu)
1300 case IB_MTU_256: return 256;
1301 case IB_MTU_512: return 512;
1302 case IB_MTU_1024: return 1024;
1303 case IB_MTU_2048: return 2048;
1304 case IB_MTU_4096: return 4096;
1310 show_board(struct device *device, struct device_attribute *attr,
1313 struct mlx4_hca_info *info = container_of(attr, struct mlx4_hca_info,
1315 struct mlx4_dev *mdev = info->dev;
1317 return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN,
1322 show_hca(struct device *device, struct device_attribute *attr,
1325 struct mlx4_hca_info *info = container_of(attr, struct mlx4_hca_info,
1327 struct mlx4_dev *mdev = info->dev;
1329 return sprintf(buf, "MT%d\n", mdev->pdev->device);
1333 show_firmware_version(struct device *dev,
1334 struct device_attribute *attr,
1337 struct mlx4_hca_info *info = container_of(attr, struct mlx4_hca_info,
1339 struct mlx4_dev *mdev = info->dev;
1341 return sprintf(buf, "%d.%d.%d\n", (int)(mdev->caps.fw_ver >> 32),
1342 (int)(mdev->caps.fw_ver >> 16) & 0xffff,
1343 (int)mdev->caps.fw_ver & 0xffff);
1346 static ssize_t show_port_ib_mtu(struct device *dev,
1347 struct device_attribute *attr,
1350 struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
1352 struct mlx4_dev *mdev = info->dev;
1354 /* When port type is eth, port mtu value isn't used. */
1355 if (mdev->caps.port_type[info->port] == MLX4_PORT_TYPE_ETH)
1358 sprintf(buf, "%d\n",
1359 ibta_mtu_to_int(mdev->caps.port_ib_mtu[info->port]));
1363 static ssize_t set_port_ib_mtu(struct device *dev,
1364 struct device_attribute *attr,
1365 const char *buf, size_t count)
1367 struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
1369 struct mlx4_dev *mdev = info->dev;
1370 struct mlx4_priv *priv = mlx4_priv(mdev);
1371 int err, port, mtu, ibta_mtu = -1;
1373 if (mdev->caps.port_type[info->port] == MLX4_PORT_TYPE_ETH) {
1374 mlx4_warn(mdev, "port level mtu is only used for IB ports\n");
1378 mtu = (int) simple_strtol(buf, NULL, 0);
1379 ibta_mtu = int_to_ibta_mtu(mtu);
1382 mlx4_err(mdev, "%s is invalid IBTA mtu\n", buf);
1386 mdev->caps.port_ib_mtu[info->port] = ibta_mtu;
1388 mlx4_stop_sense(mdev);
1389 mutex_lock(&priv->port_mutex);
1390 mlx4_unregister_device(mdev);
1391 for (port = 1; port <= mdev->caps.num_ports; port++) {
1392 mlx4_CLOSE_PORT(mdev, port);
1393 err = mlx4_SET_PORT(mdev, port, -1);
1395 mlx4_err(mdev, "Failed to set port %d, "
1396 "aborting\n", port);
1400 err = mlx4_register_device(mdev);
1402 mutex_unlock(&priv->port_mutex);
1403 mlx4_start_sense(mdev);
1404 return err ? err : count;
1407 static int mlx4_load_fw(struct mlx4_dev *dev)
1409 struct mlx4_priv *priv = mlx4_priv(dev);
1410 int err, unmap_flag = 0;
1412 priv->fw.fw_icm = mlx4_alloc_icm(dev, priv->fw.fw_pages,
1413 GFP_HIGHUSER | __GFP_NOWARN, 0);
1414 if (!priv->fw.fw_icm) {
1415 mlx4_err(dev, "Couldn't allocate FW area, aborting.\n");
1419 err = mlx4_MAP_FA(dev, priv->fw.fw_icm);
1421 mlx4_err(dev, "MAP_FA command failed, aborting.\n");
1425 err = mlx4_RUN_FW(dev);
1427 mlx4_err(dev, "RUN_FW command failed, aborting.\n");
1434 unmap_flag = mlx4_UNMAP_FA(dev);
1436 pr_warn("mlx4_core: mlx4_UNMAP_FA failed.\n");
1440 mlx4_free_icm(dev, priv->fw.fw_icm, 0);
1444 static int mlx4_init_cmpt_table(struct mlx4_dev *dev, u64 cmpt_base,
1447 struct mlx4_priv *priv = mlx4_priv(dev);
1451 err = mlx4_init_icm_table(dev, &priv->qp_table.cmpt_table,
1453 ((u64) (MLX4_CMPT_TYPE_QP *
1454 cmpt_entry_sz) << MLX4_CMPT_SHIFT),
1455 cmpt_entry_sz, dev->caps.num_qps,
1456 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
1461 err = mlx4_init_icm_table(dev, &priv->srq_table.cmpt_table,
1463 ((u64) (MLX4_CMPT_TYPE_SRQ *
1464 cmpt_entry_sz) << MLX4_CMPT_SHIFT),
1465 cmpt_entry_sz, dev->caps.num_srqs,
1466 dev->caps.reserved_srqs, 0, 0);
1470 err = mlx4_init_icm_table(dev, &priv->cq_table.cmpt_table,
1472 ((u64) (MLX4_CMPT_TYPE_CQ *
1473 cmpt_entry_sz) << MLX4_CMPT_SHIFT),
1474 cmpt_entry_sz, dev->caps.num_cqs,
1475 dev->caps.reserved_cqs, 0, 0);
1479 num_eqs = (mlx4_is_master(dev)) ? dev->phys_caps.num_phys_eqs :
1481 err = mlx4_init_icm_table(dev, &priv->eq_table.cmpt_table,
1483 ((u64) (MLX4_CMPT_TYPE_EQ *
1484 cmpt_entry_sz) << MLX4_CMPT_SHIFT),
1485 cmpt_entry_sz, num_eqs, num_eqs, 0, 0);
1492 mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
1495 mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
1498 mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
1504 static int mlx4_init_icm(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap,
1505 struct mlx4_init_hca_param *init_hca, u64 icm_size)
1507 struct mlx4_priv *priv = mlx4_priv(dev);
1510 int err, unmap_flag = 0;
1512 err = mlx4_SET_ICM_SIZE(dev, icm_size, &aux_pages);
1514 mlx4_err(dev, "SET_ICM_SIZE command failed, aborting.\n");
1518 mlx4_dbg(dev, "%lld KB of HCA context requires %lld KB aux memory.\n",
1519 (unsigned long long) icm_size >> 10,
1520 (unsigned long long) aux_pages << 2);
1522 priv->fw.aux_icm = mlx4_alloc_icm(dev, aux_pages,
1523 GFP_HIGHUSER | __GFP_NOWARN, 0);
1524 if (!priv->fw.aux_icm) {
1525 mlx4_err(dev, "Couldn't allocate aux memory, aborting.\n");
1529 err = mlx4_MAP_ICM_AUX(dev, priv->fw.aux_icm);
1531 mlx4_err(dev, "MAP_ICM_AUX command failed, aborting.\n");
1535 err = mlx4_init_cmpt_table(dev, init_hca->cmpt_base, dev_cap->cmpt_entry_sz);
1537 mlx4_err(dev, "Failed to map cMPT context memory, aborting.\n");
1542 num_eqs = (mlx4_is_master(dev)) ? dev->phys_caps.num_phys_eqs :
1544 err = mlx4_init_icm_table(dev, &priv->eq_table.table,
1545 init_hca->eqc_base, dev_cap->eqc_entry_sz,
1546 num_eqs, num_eqs, 0, 0);
1548 mlx4_err(dev, "Failed to map EQ context memory, aborting.\n");
1549 goto err_unmap_cmpt;
1553 * Reserved MTT entries must be aligned up to a cacheline
1554 * boundary, since the FW will write to them, while the driver
1555 * writes to all other MTT entries. (The variable
1556 * dev->caps.mtt_entry_sz below is really the MTT segment
1557 * size, not the raw entry size)
1559 dev->caps.reserved_mtts =
1560 ALIGN(dev->caps.reserved_mtts * dev->caps.mtt_entry_sz,
1561 dma_get_cache_alignment()) / dev->caps.mtt_entry_sz;
1563 err = mlx4_init_icm_table(dev, &priv->mr_table.mtt_table,
1565 dev->caps.mtt_entry_sz,
1567 dev->caps.reserved_mtts, 1, 0);
1569 mlx4_err(dev, "Failed to map MTT context memory, aborting.\n");
1573 err = mlx4_init_icm_table(dev, &priv->mr_table.dmpt_table,
1574 init_hca->dmpt_base,
1575 dev_cap->dmpt_entry_sz,
1577 dev->caps.reserved_mrws, 1, 1);
1579 mlx4_err(dev, "Failed to map dMPT context memory, aborting.\n");
1583 err = mlx4_init_icm_table(dev, &priv->qp_table.qp_table,
1585 dev_cap->qpc_entry_sz,
1587 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
1590 mlx4_err(dev, "Failed to map QP context memory, aborting.\n");
1591 goto err_unmap_dmpt;
1594 err = mlx4_init_icm_table(dev, &priv->qp_table.auxc_table,
1595 init_hca->auxc_base,
1596 dev_cap->aux_entry_sz,
1598 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
1601 mlx4_err(dev, "Failed to map AUXC context memory, aborting.\n");
1605 err = mlx4_init_icm_table(dev, &priv->qp_table.altc_table,
1606 init_hca->altc_base,
1607 dev_cap->altc_entry_sz,
1609 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
1612 mlx4_err(dev, "Failed to map ALTC context memory, aborting.\n");
1613 goto err_unmap_auxc;
1616 err = mlx4_init_icm_table(dev, &priv->qp_table.rdmarc_table,
1617 init_hca->rdmarc_base,
1618 dev_cap->rdmarc_entry_sz << priv->qp_table.rdmarc_shift,
1620 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
1623 mlx4_err(dev, "Failed to map RDMARC context memory, aborting\n");
1624 goto err_unmap_altc;
1627 err = mlx4_init_icm_table(dev, &priv->cq_table.table,
1629 dev_cap->cqc_entry_sz,
1631 dev->caps.reserved_cqs, 0, 0);
1633 mlx4_err(dev, "Failed to map CQ context memory, aborting.\n");
1634 goto err_unmap_rdmarc;
1637 err = mlx4_init_icm_table(dev, &priv->srq_table.table,
1638 init_hca->srqc_base,
1639 dev_cap->srq_entry_sz,
1641 dev->caps.reserved_srqs, 0, 0);
1643 mlx4_err(dev, "Failed to map SRQ context memory, aborting.\n");
1648 * For flow steering device managed mode it is required to use
1649 * mlx4_init_icm_table. For B0 steering mode it's not strictly
1650 * required, but for simplicity just map the whole multicast
1651 * group table now. The table isn't very big and it's a lot
1652 * easier than trying to track ref counts.
1654 err = mlx4_init_icm_table(dev, &priv->mcg_table.table,
1656 mlx4_get_mgm_entry_size(dev),
1657 dev->caps.num_mgms + dev->caps.num_amgms,
1658 dev->caps.num_mgms + dev->caps.num_amgms,
1661 mlx4_err(dev, "Failed to map MCG context memory, aborting.\n");
1668 mlx4_cleanup_icm_table(dev, &priv->srq_table.table);
1671 mlx4_cleanup_icm_table(dev, &priv->cq_table.table);
1674 mlx4_cleanup_icm_table(dev, &priv->qp_table.rdmarc_table);
1677 mlx4_cleanup_icm_table(dev, &priv->qp_table.altc_table);
1680 mlx4_cleanup_icm_table(dev, &priv->qp_table.auxc_table);
1683 mlx4_cleanup_icm_table(dev, &priv->qp_table.qp_table);
1686 mlx4_cleanup_icm_table(dev, &priv->mr_table.dmpt_table);
1689 mlx4_cleanup_icm_table(dev, &priv->mr_table.mtt_table);
1692 mlx4_cleanup_icm_table(dev, &priv->eq_table.table);
1695 mlx4_cleanup_icm_table(dev, &priv->eq_table.cmpt_table);
1696 mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
1697 mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
1698 mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
1701 unmap_flag = mlx4_UNMAP_ICM_AUX(dev);
1703 pr_warn("mlx4_core: mlx4_UNMAP_ICM_AUX failed.\n");
1707 mlx4_free_icm(dev, priv->fw.aux_icm, 0);
1712 static void mlx4_free_icms(struct mlx4_dev *dev)
1714 struct mlx4_priv *priv = mlx4_priv(dev);
1716 mlx4_cleanup_icm_table(dev, &priv->mcg_table.table);
1717 mlx4_cleanup_icm_table(dev, &priv->srq_table.table);
1718 mlx4_cleanup_icm_table(dev, &priv->cq_table.table);
1719 mlx4_cleanup_icm_table(dev, &priv->qp_table.rdmarc_table);
1720 mlx4_cleanup_icm_table(dev, &priv->qp_table.altc_table);
1721 mlx4_cleanup_icm_table(dev, &priv->qp_table.auxc_table);
1722 mlx4_cleanup_icm_table(dev, &priv->qp_table.qp_table);
1723 mlx4_cleanup_icm_table(dev, &priv->mr_table.dmpt_table);
1724 mlx4_cleanup_icm_table(dev, &priv->mr_table.mtt_table);
1725 mlx4_cleanup_icm_table(dev, &priv->eq_table.table);
1726 mlx4_cleanup_icm_table(dev, &priv->eq_table.cmpt_table);
1727 mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
1728 mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
1729 mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
1731 if (!mlx4_UNMAP_ICM_AUX(dev))
1732 mlx4_free_icm(dev, priv->fw.aux_icm, 0);
1734 pr_warn("mlx4_core: mlx4_UNMAP_ICM_AUX failed.\n");
1737 static void mlx4_slave_exit(struct mlx4_dev *dev)
1739 struct mlx4_priv *priv = mlx4_priv(dev);
1741 mutex_lock(&priv->cmd.slave_cmd_mutex);
1742 if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_RESET, 0, MLX4_COMM_TIME))
1743 mlx4_warn(dev, "Failed to close slave function.\n");
1744 mutex_unlock(&priv->cmd.slave_cmd_mutex);
1747 static int map_bf_area(struct mlx4_dev *dev)
1749 struct mlx4_priv *priv = mlx4_priv(dev);
1750 resource_size_t bf_start;
1751 resource_size_t bf_len;
1754 if (!dev->caps.bf_reg_size)
1757 bf_start = pci_resource_start(dev->pdev, 2) +
1758 (dev->caps.num_uars << PAGE_SHIFT);
1759 bf_len = pci_resource_len(dev->pdev, 2) -
1760 (dev->caps.num_uars << PAGE_SHIFT);
1761 priv->bf_mapping = io_mapping_create_wc(bf_start, bf_len);
1762 if (!priv->bf_mapping)
1768 static void unmap_bf_area(struct mlx4_dev *dev)
1770 if (mlx4_priv(dev)->bf_mapping)
1771 io_mapping_free(mlx4_priv(dev)->bf_mapping);
1774 int mlx4_read_clock(struct mlx4_dev *dev)
1776 u32 clockhi, clocklo, clockhi1;
1779 struct mlx4_priv *priv = mlx4_priv(dev);
1781 if (!priv->clock_mapping)
1784 for (i = 0; i < 10; i++) {
1785 clockhi = swab32(readl(priv->clock_mapping));
1786 clocklo = swab32(readl(priv->clock_mapping + 4));
1787 clockhi1 = swab32(readl(priv->clock_mapping));
1788 if (clockhi == clockhi1)
1792 cycles = (u64) clockhi << 32 | (u64) clocklo;
1796 EXPORT_SYMBOL_GPL(mlx4_read_clock);
1799 static int map_internal_clock(struct mlx4_dev *dev)
1801 struct mlx4_priv *priv = mlx4_priv(dev);
1803 priv->clock_mapping = ioremap(pci_resource_start(dev->pdev,
1804 priv->fw.clock_bar) +
1805 priv->fw.clock_offset, MLX4_CLOCK_SIZE);
1807 if (!priv->clock_mapping)
1814 int mlx4_get_internal_clock_params(struct mlx4_dev *dev,
1815 struct mlx4_clock_params *params)
1817 struct mlx4_priv *priv = mlx4_priv(dev);
1819 if (mlx4_is_slave(dev))
1824 params->bar = priv->fw.clock_bar;
1825 params->offset = priv->fw.clock_offset;
1826 params->size = MLX4_CLOCK_SIZE;
1830 EXPORT_SYMBOL_GPL(mlx4_get_internal_clock_params);
1832 static void unmap_internal_clock(struct mlx4_dev *dev)
1834 struct mlx4_priv *priv = mlx4_priv(dev);
1836 if (priv->clock_mapping)
1837 iounmap(priv->clock_mapping);
1840 static void mlx4_close_hca(struct mlx4_dev *dev)
1842 unmap_internal_clock(dev);
1844 if (mlx4_is_slave(dev)) {
1845 mlx4_slave_exit(dev);
1847 mlx4_CLOSE_HCA(dev, 0);
1848 mlx4_free_icms(dev);
1850 if (!mlx4_UNMAP_FA(dev))
1851 mlx4_free_icm(dev, mlx4_priv(dev)->fw.fw_icm, 0);
1853 pr_warn("mlx4_core: mlx4_UNMAP_FA failed.\n");
1857 static int mlx4_init_slave(struct mlx4_dev *dev)
1859 struct mlx4_priv *priv = mlx4_priv(dev);
1860 u64 dma = (u64) priv->mfunc.vhcr_dma;
1861 int num_of_reset_retries = NUM_OF_RESET_RETRIES;
1862 int ret_from_reset = 0;
1864 u32 cmd_channel_ver;
1866 mutex_lock(&priv->cmd.slave_cmd_mutex);
1867 priv->cmd.max_cmds = 1;
1868 mlx4_warn(dev, "Sending reset\n");
1869 ret_from_reset = mlx4_comm_cmd(dev, MLX4_COMM_CMD_RESET, 0,
1871 /* if we are in the middle of flr the slave will try
1872 * NUM_OF_RESET_RETRIES times before leaving.*/
1873 if (ret_from_reset) {
1874 if (MLX4_DELAY_RESET_SLAVE == ret_from_reset) {
1875 msleep(SLEEP_TIME_IN_RESET);
1876 while (ret_from_reset && num_of_reset_retries) {
1877 mlx4_warn(dev, "slave is currently in the"
1878 "middle of FLR. retrying..."
1880 (NUM_OF_RESET_RETRIES -
1881 num_of_reset_retries + 1));
1883 mlx4_comm_cmd(dev, MLX4_COMM_CMD_RESET,
1885 num_of_reset_retries = num_of_reset_retries - 1;
1891 /* check the driver version - the slave I/F revision
1892 * must match the master's */
1893 slave_read = swab32(readl(&priv->mfunc.comm->slave_read));
1894 cmd_channel_ver = mlx4_comm_get_version();
1896 if (MLX4_COMM_GET_IF_REV(cmd_channel_ver) !=
1897 MLX4_COMM_GET_IF_REV(slave_read)) {
1898 mlx4_err(dev, "slave driver version is not supported"
1899 " by the master\n");
1903 mlx4_warn(dev, "Sending vhcr0\n");
1904 if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR0, dma >> 48,
1907 if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR1, dma >> 32,
1910 if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR2, dma >> 16,
1913 if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR_EN, dma, MLX4_COMM_TIME))
1916 mutex_unlock(&priv->cmd.slave_cmd_mutex);
1920 mlx4_comm_cmd(dev, MLX4_COMM_CMD_RESET, 0, 0);
1921 mutex_unlock(&priv->cmd.slave_cmd_mutex);
1925 static void mlx4_parav_master_pf_caps(struct mlx4_dev *dev)
1929 for (i = 1; i <= dev->caps.num_ports; i++) {
1930 if (dev->caps.port_type[i] == MLX4_PORT_TYPE_ETH)
1931 dev->caps.gid_table_len[i] =
1932 mlx4_get_slave_num_gids(dev, 0);
1934 dev->caps.gid_table_len[i] = 1;
1935 dev->caps.pkey_table_len[i] =
1936 dev->phys_caps.pkey_phys_table_len[i] - 1;
1940 static int choose_log_fs_mgm_entry_size(int qp_per_entry)
1942 int i = MLX4_MIN_MGM_LOG_ENTRY_SIZE;
1944 for (i = MLX4_MIN_MGM_LOG_ENTRY_SIZE; i <= MLX4_MAX_MGM_LOG_ENTRY_SIZE;
1946 if (qp_per_entry <= 4 * ((1 << i) / 16 - 2))
1950 return (i <= MLX4_MAX_MGM_LOG_ENTRY_SIZE) ? i : -1;
1953 static void choose_steering_mode(struct mlx4_dev *dev,
1954 struct mlx4_dev_cap *dev_cap)
1958 mlx4_get_val(num_vfs.dbdf2val.tbl, pci_physfn(dev->pdev), 0, &nvfs);
1959 if (high_rate_steer && !mlx4_is_mfunc(dev)) {
1960 dev->caps.flags &= ~(MLX4_DEV_CAP_FLAG_VEP_MC_STEER |
1961 MLX4_DEV_CAP_FLAG_VEP_UC_STEER);
1962 dev_cap->flags2 &= ~MLX4_DEV_CAP_FLAG2_FS_EN;
1965 if (mlx4_log_num_mgm_entry_size == -1 &&
1966 dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_FS_EN &&
1967 (!mlx4_is_mfunc(dev) ||
1968 (dev_cap->fs_max_num_qp_per_entry >= (nvfs + 1))) &&
1969 choose_log_fs_mgm_entry_size(dev_cap->fs_max_num_qp_per_entry) >=
1970 MLX4_MIN_MGM_LOG_ENTRY_SIZE) {
1971 dev->oper_log_mgm_entry_size =
1972 choose_log_fs_mgm_entry_size(dev_cap->fs_max_num_qp_per_entry);
1973 dev->caps.steering_mode = MLX4_STEERING_MODE_DEVICE_MANAGED;
1974 dev->caps.num_qp_per_mgm = dev_cap->fs_max_num_qp_per_entry;
1976 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER &&
1977 dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER)
1978 dev->caps.steering_mode = MLX4_STEERING_MODE_B0;
1980 dev->caps.steering_mode = MLX4_STEERING_MODE_A0;
1982 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER ||
1983 dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER)
1984 mlx4_warn(dev, "Must have both UC_STEER and MC_STEER flags "
1985 "set to use B0 steering. Falling back to A0 steering mode.\n");
1987 dev->oper_log_mgm_entry_size =
1988 mlx4_log_num_mgm_entry_size > 0 ?
1989 mlx4_log_num_mgm_entry_size :
1990 MLX4_DEFAULT_MGM_LOG_ENTRY_SIZE;
1991 dev->caps.num_qp_per_mgm = mlx4_get_qp_per_mgm(dev);
1993 mlx4_dbg(dev, "Steering mode is: %s, oper_log_mgm_entry_size = %d, "
1994 "log_num_mgm_entry_size = %d\n",
1995 mlx4_steering_mode_str(dev->caps.steering_mode),
1996 dev->oper_log_mgm_entry_size, mlx4_log_num_mgm_entry_size);
1999 static int mlx4_init_hca(struct mlx4_dev *dev)
2001 struct mlx4_priv *priv = mlx4_priv(dev);
2002 struct mlx4_dev_cap *dev_cap = NULL;
2003 struct mlx4_adapter adapter;
2004 struct mlx4_mod_stat_cfg mlx4_cfg;
2005 struct mlx4_profile profile;
2006 struct mlx4_init_hca_param init_hca;
2010 if (!mlx4_is_slave(dev)) {
2011 err = mlx4_QUERY_FW(dev);
2014 mlx4_info(dev, "non-primary physical function, skipping.\n");
2016 mlx4_err(dev, "QUERY_FW command failed, aborting.\n");
2020 err = mlx4_load_fw(dev);
2022 mlx4_err(dev, "Failed to start FW, aborting.\n");
2026 mlx4_cfg.log_pg_sz_m = 1;
2027 mlx4_cfg.log_pg_sz = 0;
2028 err = mlx4_MOD_STAT_CFG(dev, &mlx4_cfg);
2030 mlx4_warn(dev, "Failed to override log_pg_sz parameter\n");
2032 dev_cap = kzalloc(sizeof *dev_cap, GFP_KERNEL);
2034 mlx4_err(dev, "Failed to allocate memory for dev_cap\n");
2039 err = mlx4_dev_cap(dev, dev_cap);
2041 mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
2045 choose_steering_mode(dev, dev_cap);
2047 if (mlx4_is_master(dev))
2048 mlx4_parav_master_pf_caps(dev);
2050 process_mod_param_profile(&profile);
2051 if (dev->caps.steering_mode ==
2052 MLX4_STEERING_MODE_DEVICE_MANAGED)
2053 profile.num_mcg = MLX4_FS_NUM_MCG;
2055 icm_size = mlx4_make_profile(dev, &profile, dev_cap,
2057 if ((long long) icm_size < 0) {
2062 dev->caps.max_fmr_maps = (1 << (32 - ilog2(dev->caps.num_mpts))) - 1;
2064 init_hca.log_uar_sz = ilog2(dev->caps.num_uars);
2065 init_hca.uar_page_sz = PAGE_SHIFT - 12;
2067 err = mlx4_init_icm(dev, dev_cap, &init_hca, icm_size);
2071 init_hca.mw_enable = 1;
2073 err = mlx4_INIT_HCA(dev, &init_hca);
2075 mlx4_err(dev, "INIT_HCA command failed, aborting.\n");
2080 * Read HCA frequency by QUERY_HCA command
2082 if (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS) {
2083 memset(&init_hca, 0, sizeof(init_hca));
2084 err = mlx4_QUERY_HCA(dev, &init_hca);
2086 mlx4_err(dev, "QUERY_HCA command failed, disable timestamp.\n");
2087 dev->caps.flags2 &= ~MLX4_DEV_CAP_FLAG2_TS;
2089 dev->caps.hca_core_clock =
2090 init_hca.hca_core_clock;
2093 /* In case we got HCA frequency 0 - disable timestamping
2094 * to avoid dividing by zero
2096 if (!dev->caps.hca_core_clock) {
2097 dev->caps.flags2 &= ~MLX4_DEV_CAP_FLAG2_TS;
2098 mlx4_err(dev, "HCA frequency is 0. Timestamping is not supported.");
2099 } else if (map_internal_clock(dev)) {
2100 /* Map internal clock,
2101 * in case of failure disable timestamping
2103 dev->caps.flags2 &= ~MLX4_DEV_CAP_FLAG2_TS;
2104 mlx4_err(dev, "Failed to map internal clock. Timestamping is not supported.\n");
2108 err = mlx4_init_slave(dev);
2110 mlx4_err(dev, "Failed to initialize slave\n");
2114 err = mlx4_slave_cap(dev);
2116 mlx4_err(dev, "Failed to obtain slave caps\n");
2121 if (map_bf_area(dev))
2122 mlx4_dbg(dev, "Failed to map blue flame area\n");
2124 /* Only the master set the ports, all the rest got it from it.*/
2125 if (!mlx4_is_slave(dev))
2126 mlx4_set_port_mask(dev);
2128 err = mlx4_QUERY_ADAPTER(dev, &adapter);
2130 mlx4_err(dev, "QUERY_ADAPTER command failed, aborting.\n");
2134 priv->eq_table.inta_pin = adapter.inta_pin;
2135 memcpy(dev->board_id, adapter.board_id, sizeof dev->board_id);
2136 memcpy(dev->vsd, adapter.vsd, sizeof(dev->vsd));
2137 dev->vsd_vendor_id = adapter.vsd_vendor_id;
2139 if (!mlx4_is_slave(dev))
2145 if (!mlx4_is_slave(dev))
2146 unmap_internal_clock(dev);
2149 if (mlx4_is_slave(dev)) {
2150 kfree(dev->caps.qp0_tunnel);
2151 kfree(dev->caps.qp0_proxy);
2152 kfree(dev->caps.qp1_tunnel);
2153 kfree(dev->caps.qp1_proxy);
2157 if (mlx4_is_slave(dev))
2158 mlx4_slave_exit(dev);
2160 mlx4_CLOSE_HCA(dev, 0);
2163 if (!mlx4_is_slave(dev))
2164 mlx4_free_icms(dev);
2167 if (!mlx4_is_slave(dev)) {
2168 if (!mlx4_UNMAP_FA(dev))
2169 mlx4_free_icm(dev, priv->fw.fw_icm, 0);
2171 pr_warn("mlx4_core: mlx4_UNMAP_FA failed.\n");
2177 static int mlx4_init_counters_table(struct mlx4_dev *dev)
2179 struct mlx4_priv *priv = mlx4_priv(dev);
2180 int nent_pow2, port_indx, vf_index, num_counters;
2182 struct counter_index *new_counter_index;
2185 if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_COUNTERS))
2188 if (!mlx4_is_slave(dev) &&
2189 dev->caps.max_counters == dev->caps.max_extended_counters) {
2190 res = mlx4_cmd(dev, MLX4_IF_STATE_EXTENDED, 0, 0,
2191 MLX4_CMD_SET_IF_STAT,
2192 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
2194 mlx4_err(dev, "Failed to set extended counters (err=%d)\n", res);
2199 mutex_init(&priv->counters_table.mutex);
2201 if (mlx4_is_slave(dev)) {
2202 for (port_indx = 0; port_indx < dev->caps.num_ports; port_indx++) {
2203 INIT_LIST_HEAD(&priv->counters_table.global_port_list[port_indx]);
2204 if (dev->caps.def_counter_index[port_indx] != 0xFF) {
2205 new_counter_index = kmalloc(sizeof(struct counter_index), GFP_KERNEL);
2206 if (!new_counter_index)
2208 new_counter_index->index = dev->caps.def_counter_index[port_indx];
2209 list_add_tail(&new_counter_index->list, &priv->counters_table.global_port_list[port_indx]);
2212 mlx4_dbg(dev, "%s: slave allocated %d counters for %d ports\n",
2213 __func__, dev->caps.num_ports, dev->caps.num_ports);
2217 nent_pow2 = roundup_pow_of_two(dev->caps.max_counters);
2219 for (port_indx = 0; port_indx < dev->caps.num_ports; port_indx++) {
2220 INIT_LIST_HEAD(&priv->counters_table.global_port_list[port_indx]);
2221 /* allocating 2 counters per port for PFs */
2222 /* For the PF, the ETH default counters are 0,2; */
2223 /* and the RoCE default counters are 1,3 */
2224 for (num_counters = 0; num_counters < 2; num_counters++, index++) {
2225 new_counter_index = kmalloc(sizeof(struct counter_index), GFP_KERNEL);
2226 if (!new_counter_index)
2228 new_counter_index->index = index;
2229 list_add_tail(&new_counter_index->list,
2230 &priv->counters_table.global_port_list[port_indx]);
2234 if (mlx4_is_master(dev)) {
2235 for (vf_index = 0; vf_index < dev->num_vfs; vf_index++) {
2236 for (port_indx = 0; port_indx < dev->caps.num_ports; port_indx++) {
2237 INIT_LIST_HEAD(&priv->counters_table.vf_list[vf_index][port_indx]);
2238 new_counter_index = kmalloc(sizeof(struct counter_index), GFP_KERNEL);
2239 if (!new_counter_index)
2241 if (index < nent_pow2 - 2) {
2242 new_counter_index->index = index;
2245 new_counter_index->index = MLX4_SINK_COUNTER_INDEX;
2248 list_add_tail(&new_counter_index->list,
2249 &priv->counters_table.vf_list[vf_index][port_indx]);
2253 res = mlx4_bitmap_init(&priv->counters_table.bitmap,
2254 nent_pow2, nent_pow2 - 1,
2256 mlx4_dbg(dev, "%s: master allocated %d counters for %d VFs\n",
2257 __func__, index, dev->num_vfs);
2259 res = mlx4_bitmap_init(&priv->counters_table.bitmap,
2260 nent_pow2, nent_pow2 - 1,
2262 mlx4_dbg(dev, "%s: native allocated %d counters for %d ports\n",
2263 __func__, index, dev->caps.num_ports);
2270 static void mlx4_cleanup_counters_table(struct mlx4_dev *dev)
2272 struct mlx4_priv *priv = mlx4_priv(dev);
2274 struct counter_index *port, *tmp_port;
2275 struct counter_index *vf, *tmp_vf;
2277 mutex_lock(&priv->counters_table.mutex);
2279 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_COUNTERS) {
2280 for (i = 0; i < dev->caps.num_ports; i++) {
2281 list_for_each_entry_safe(port, tmp_port,
2282 &priv->counters_table.global_port_list[i],
2284 list_del(&port->list);
2288 if (!mlx4_is_slave(dev)) {
2289 for (i = 0; i < dev->num_vfs; i++) {
2290 for (j = 0; j < dev->caps.num_ports; j++) {
2291 list_for_each_entry_safe(vf, tmp_vf,
2292 &priv->counters_table.vf_list[i][j],
2294 /* clear the counter statistic */
2295 if (__mlx4_clear_if_stat(dev, vf->index))
2296 mlx4_dbg(dev, "%s: reset counter %d failed\n",
2297 __func__, vf->index);
2298 list_del(&vf->list);
2303 mlx4_bitmap_cleanup(&priv->counters_table.bitmap);
2306 mutex_unlock(&priv->counters_table.mutex);
2309 int __mlx4_slave_counters_free(struct mlx4_dev *dev, int slave)
2311 struct mlx4_priv *priv = mlx4_priv(dev);
2313 struct counter_index *vf, *tmp_vf;
2315 /* clean VF's counters for the next useg */
2316 if (slave > 0 && slave <= dev->num_vfs) {
2317 mlx4_dbg(dev, "%s: free counters of slave(%d)\n"
2320 mutex_lock(&priv->counters_table.mutex);
2321 for (i = 0; i < dev->caps.num_ports; i++) {
2323 list_for_each_entry_safe(vf, tmp_vf,
2324 &priv->counters_table.vf_list[slave - 1][i],
2326 /* clear the counter statistic */
2327 if (__mlx4_clear_if_stat(dev, vf->index))
2328 mlx4_dbg(dev, "%s: reset counter %d failed\n",
2329 __func__, vf->index);
2330 if (first++ && vf->index != MLX4_SINK_COUNTER_INDEX) {
2331 mlx4_dbg(dev, "%s: delete counter index %d for slave %d and port %d\n"
2332 , __func__, vf->index, slave, i + 1);
2333 mlx4_bitmap_free(&priv->counters_table.bitmap, vf->index, MLX4_USE_RR);
2334 list_del(&vf->list);
2337 mlx4_dbg(dev, "%s: can't delete default counter index %d for slave %d and port %d\n"
2338 , __func__, vf->index, slave, i + 1);
2342 mutex_unlock(&priv->counters_table.mutex);
2348 int __mlx4_counter_alloc(struct mlx4_dev *dev, int slave, int port, u32 *idx)
2350 struct mlx4_priv *priv = mlx4_priv(dev);
2351 struct counter_index *new_counter_index;
2353 if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_COUNTERS))
2356 if ((slave > MLX4_MAX_NUM_VF) || (slave < 0) ||
2357 (port < 0) || (port > MLX4_MAX_PORTS)) {
2358 mlx4_dbg(dev, "%s: invalid slave(%d) or port(%d) index\n",
2359 __func__, slave, port);
2363 /* handle old guest request does not support request by port index */
2365 *idx = MLX4_SINK_COUNTER_INDEX;
2366 mlx4_dbg(dev, "%s: allocated default counter index %d for slave %d port %d\n"
2367 , __func__, *idx, slave, port);
2371 mutex_lock(&priv->counters_table.mutex);
2373 *idx = mlx4_bitmap_alloc(&priv->counters_table.bitmap);
2374 /* if no resources return the default counter of the slave and port */
2376 if (slave == 0) { /* its the ethernet counter ?????? */
2377 new_counter_index = list_entry(priv->counters_table.global_port_list[port - 1].next,
2378 struct counter_index,
2381 new_counter_index = list_entry(priv->counters_table.vf_list[slave - 1][port - 1].next,
2382 struct counter_index,
2386 *idx = new_counter_index->index;
2387 mlx4_dbg(dev, "%s: allocated defualt counter index %d for slave %d port %d\n"
2388 , __func__, *idx, slave, port);
2392 if (slave == 0) { /* native or master */
2393 new_counter_index = kmalloc(sizeof(struct counter_index), GFP_KERNEL);
2394 if (!new_counter_index)
2396 new_counter_index->index = *idx;
2397 list_add_tail(&new_counter_index->list, &priv->counters_table.global_port_list[port - 1]);
2399 new_counter_index = kmalloc(sizeof(struct counter_index), GFP_KERNEL);
2400 if (!new_counter_index)
2402 new_counter_index->index = *idx;
2403 list_add_tail(&new_counter_index->list, &priv->counters_table.vf_list[slave - 1][port - 1]);
2406 mlx4_dbg(dev, "%s: allocated counter index %d for slave %d port %d\n"
2407 , __func__, *idx, slave, port);
2409 mutex_unlock(&priv->counters_table.mutex);
2413 mlx4_bitmap_free(&priv->counters_table.bitmap, *idx, MLX4_USE_RR);
2414 mutex_unlock(&priv->counters_table.mutex);
2415 *idx = MLX4_SINK_COUNTER_INDEX;
2416 mlx4_dbg(dev, "%s: failed err (%d)\n"
2417 , __func__, -ENOMEM);
2421 int mlx4_counter_alloc(struct mlx4_dev *dev, u8 port, u32 *idx)
2425 struct mlx4_priv *priv = mlx4_priv(dev);
2426 struct counter_index *new_counter_index, *c_index;
2428 if (mlx4_is_mfunc(dev)) {
2429 err = mlx4_cmd_imm(dev, 0, &out_param,
2430 ((u32) port) << 8 | (u32) RES_COUNTER,
2431 RES_OP_RESERVE, MLX4_CMD_ALLOC_RES,
2432 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
2434 *idx = get_param_l(&out_param);
2435 if (*idx == MLX4_SINK_COUNTER_INDEX)
2438 mutex_lock(&priv->counters_table.mutex);
2439 c_index = list_entry(priv->counters_table.global_port_list[port - 1].next,
2440 struct counter_index,
2442 mutex_unlock(&priv->counters_table.mutex);
2443 if (c_index->index == *idx)
2446 if (mlx4_is_slave(dev)) {
2447 new_counter_index = kmalloc(sizeof(struct counter_index), GFP_KERNEL);
2448 if (!new_counter_index) {
2449 mlx4_counter_free(dev, port, *idx);
2452 new_counter_index->index = *idx;
2453 mutex_lock(&priv->counters_table.mutex);
2454 list_add_tail(&new_counter_index->list, &priv->counters_table.global_port_list[port - 1]);
2455 mutex_unlock(&priv->counters_table.mutex);
2456 mlx4_dbg(dev, "%s: allocated counter index %d for port %d\n"
2457 , __func__, *idx, port);
2462 return __mlx4_counter_alloc(dev, 0, port, idx);
2464 EXPORT_SYMBOL_GPL(mlx4_counter_alloc);
2466 void __mlx4_counter_free(struct mlx4_dev *dev, int slave, int port, u32 idx)
2468 /* check if native or slave and deletes acordingly */
2469 struct mlx4_priv *priv = mlx4_priv(dev);
2470 struct counter_index *pf, *tmp_pf;
2471 struct counter_index *vf, *tmp_vf;
2475 if (idx == MLX4_SINK_COUNTER_INDEX) {
2476 mlx4_dbg(dev, "%s: try to delete default counter index %d for port %d\n"
2477 , __func__, idx, port);
2481 if ((slave > MLX4_MAX_NUM_VF) || (slave < 0) ||
2482 (port < 0) || (port > MLX4_MAX_PORTS)) {
2483 mlx4_warn(dev, "%s: deletion failed due to invalid slave(%d) or port(%d) index\n"
2484 , __func__, slave, idx);
2488 mutex_lock(&priv->counters_table.mutex);
2491 list_for_each_entry_safe(pf, tmp_pf,
2492 &priv->counters_table.global_port_list[port - 1],
2494 /* the first 2 counters are reserved */
2495 if (pf->index == idx) {
2496 /* clear the counter statistic */
2497 if (__mlx4_clear_if_stat(dev, pf->index))
2498 mlx4_dbg(dev, "%s: reset counter %d failed\n",
2499 __func__, pf->index);
2500 if (1 < first && idx != MLX4_SINK_COUNTER_INDEX) {
2501 list_del(&pf->list);
2503 mlx4_dbg(dev, "%s: delete counter index %d for native device (%d) port %d\n"
2504 , __func__, idx, slave, port);
2505 mlx4_bitmap_free(&priv->counters_table.bitmap, idx, MLX4_USE_RR);
2508 mlx4_dbg(dev, "%s: can't delete default counter index %d for native device (%d) port %d\n"
2509 , __func__, idx, slave, port);
2515 mlx4_dbg(dev, "%s: can't delete counter index %d for native device (%d) port %d\n"
2516 , __func__, idx, slave, port);
2519 list_for_each_entry_safe(vf, tmp_vf,
2520 &priv->counters_table.vf_list[slave - 1][port - 1],
2522 /* the first element is reserved */
2523 if (vf->index == idx) {
2524 /* clear the counter statistic */
2525 if (__mlx4_clear_if_stat(dev, vf->index))
2526 mlx4_dbg(dev, "%s: reset counter %d failed\n",
2527 __func__, vf->index);
2529 list_del(&vf->list);
2531 mlx4_dbg(dev, "%s: delete counter index %d for slave %d port %d\n",
2532 __func__, idx, slave, port);
2533 mlx4_bitmap_free(&priv->counters_table.bitmap, idx, MLX4_USE_RR);
2536 mlx4_dbg(dev, "%s: can't delete default slave (%d) counter index %d for port %d\n"
2537 , __func__, slave, idx, port);
2543 mlx4_dbg(dev, "%s: can't delete slave (%d) counter index %d for port %d\n"
2544 , __func__, slave, idx, port);
2548 mutex_unlock(&priv->counters_table.mutex);
2551 void mlx4_counter_free(struct mlx4_dev *dev, u8 port, u32 idx)
2554 struct mlx4_priv *priv = mlx4_priv(dev);
2555 struct counter_index *counter, *tmp_counter;
2558 if (mlx4_is_mfunc(dev)) {
2559 set_param_l(&in_param, idx);
2560 mlx4_cmd(dev, in_param,
2561 ((u32) port) << 8 | (u32) RES_COUNTER,
2563 MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A,
2566 if (mlx4_is_slave(dev) && idx != MLX4_SINK_COUNTER_INDEX) {
2567 mutex_lock(&priv->counters_table.mutex);
2568 list_for_each_entry_safe(counter, tmp_counter,
2569 &priv->counters_table.global_port_list[port - 1],
2571 if (counter->index == idx && first++) {
2572 list_del(&counter->list);
2574 mlx4_dbg(dev, "%s: delete counter index %d for port %d\n"
2575 , __func__, idx, port);
2576 mutex_unlock(&priv->counters_table.mutex);
2580 mutex_unlock(&priv->counters_table.mutex);
2585 __mlx4_counter_free(dev, 0, port, idx);
2587 EXPORT_SYMBOL_GPL(mlx4_counter_free);
2589 int __mlx4_clear_if_stat(struct mlx4_dev *dev,
2592 struct mlx4_cmd_mailbox *if_stat_mailbox = NULL;
2594 u32 if_stat_in_mod = (counter_index & 0xff) | (1 << 31);
2596 if (counter_index == MLX4_SINK_COUNTER_INDEX)
2599 if (mlx4_is_slave(dev))
2602 if_stat_mailbox = mlx4_alloc_cmd_mailbox(dev);
2603 if (IS_ERR(if_stat_mailbox)) {
2604 err = PTR_ERR(if_stat_mailbox);
2608 err = mlx4_cmd_box(dev, 0, if_stat_mailbox->dma, if_stat_in_mod, 0,
2609 MLX4_CMD_QUERY_IF_STAT, MLX4_CMD_TIME_CLASS_C,
2612 mlx4_free_cmd_mailbox(dev, if_stat_mailbox);
2616 u8 mlx4_get_default_counter_index(struct mlx4_dev *dev, int slave, int port)
2618 struct mlx4_priv *priv = mlx4_priv(dev);
2619 struct counter_index *new_counter_index;
2621 if (dev->caps.port_type[port] == MLX4_PORT_TYPE_IB) {
2622 mlx4_dbg(dev, "%s: return counter index %d for slave %d port (MLX4_PORT_TYPE_IB) %d\n",
2623 __func__, MLX4_SINK_COUNTER_INDEX, slave, port);
2624 return (u8)MLX4_SINK_COUNTER_INDEX;
2627 mutex_lock(&priv->counters_table.mutex);
2629 new_counter_index = list_entry(priv->counters_table.global_port_list[port - 1].next,
2630 struct counter_index,
2633 new_counter_index = list_entry(priv->counters_table.vf_list[slave - 1][port - 1].next,
2634 struct counter_index,
2637 mutex_unlock(&priv->counters_table.mutex);
2639 mlx4_dbg(dev, "%s: return counter index %d for slave %d port %d\n",
2640 __func__, new_counter_index->index, slave, port);
2643 return (u8)new_counter_index->index;
2646 int mlx4_get_vport_ethtool_stats(struct mlx4_dev *dev, int port,
2647 struct mlx4_en_vport_stats *vport_stats,
2650 struct mlx4_priv *priv = mlx4_priv(dev);
2651 struct mlx4_cmd_mailbox *if_stat_mailbox = NULL;
2652 union mlx4_counter *counter;
2655 struct counter_index *vport, *tmp_vport;
2660 if_stat_mailbox = mlx4_alloc_cmd_mailbox(dev);
2661 if (IS_ERR(if_stat_mailbox)) {
2662 err = PTR_ERR(if_stat_mailbox);
2666 mutex_lock(&priv->counters_table.mutex);
2667 list_for_each_entry_safe(vport, tmp_vport,
2668 &priv->counters_table.global_port_list[port - 1],
2670 if (vport->index == MLX4_SINK_COUNTER_INDEX)
2673 memset(if_stat_mailbox->buf, 0, sizeof(union mlx4_counter));
2674 if_stat_in_mod = (vport->index & 0xff) | ((reset & 1) << 31);
2675 err = mlx4_cmd_box(dev, 0, if_stat_mailbox->dma,
2677 MLX4_CMD_QUERY_IF_STAT,
2678 MLX4_CMD_TIME_CLASS_C,
2681 mlx4_dbg(dev, "%s: failed to read statistics for counter index %d\n",
2682 __func__, vport->index);
2685 counter = (union mlx4_counter *)if_stat_mailbox->buf;
2686 if ((counter->control.cnt_mode & 0xf) == 1) {
2687 vport_stats->rx_broadcast_packets += be64_to_cpu(counter->ext.counters[0].IfRxBroadcastFrames);
2688 vport_stats->rx_unicast_packets += be64_to_cpu(counter->ext.counters[0].IfRxUnicastFrames);
2689 vport_stats->rx_multicast_packets += be64_to_cpu(counter->ext.counters[0].IfRxMulticastFrames);
2690 vport_stats->tx_broadcast_packets += be64_to_cpu(counter->ext.counters[0].IfTxBroadcastFrames);
2691 vport_stats->tx_unicast_packets += be64_to_cpu(counter->ext.counters[0].IfTxUnicastFrames);
2692 vport_stats->tx_multicast_packets += be64_to_cpu(counter->ext.counters[0].IfTxMulticastFrames);
2693 vport_stats->rx_broadcast_bytes += be64_to_cpu(counter->ext.counters[0].IfRxBroadcastOctets);
2694 vport_stats->rx_unicast_bytes += be64_to_cpu(counter->ext.counters[0].IfRxUnicastOctets);
2695 vport_stats->rx_multicast_bytes += be64_to_cpu(counter->ext.counters[0].IfRxMulticastOctets);
2696 vport_stats->tx_broadcast_bytes += be64_to_cpu(counter->ext.counters[0].IfTxBroadcastOctets);
2697 vport_stats->tx_unicast_bytes += be64_to_cpu(counter->ext.counters[0].IfTxUnicastOctets);
2698 vport_stats->tx_multicast_bytes += be64_to_cpu(counter->ext.counters[0].IfTxMulticastOctets);
2699 vport_stats->rx_errors += be64_to_cpu(counter->ext.counters[0].IfRxErrorFrames);
2700 vport_stats->rx_dropped += be64_to_cpu(counter->ext.counters[0].IfRxNoBufferFrames);
2701 vport_stats->tx_errors += be64_to_cpu(counter->ext.counters[0].IfTxDroppedFrames);
2706 mutex_unlock(&priv->counters_table.mutex);
2707 mlx4_free_cmd_mailbox(dev, if_stat_mailbox);
2711 EXPORT_SYMBOL_GPL(mlx4_get_vport_ethtool_stats);
2713 static int mlx4_setup_hca(struct mlx4_dev *dev)
2715 struct mlx4_priv *priv = mlx4_priv(dev);
2718 __be32 ib_port_default_caps;
2720 err = mlx4_init_uar_table(dev);
2722 mlx4_err(dev, "Failed to initialize "
2723 "user access region table (err=%d), aborting.\n",
2728 err = mlx4_uar_alloc(dev, &priv->driver_uar);
2730 mlx4_err(dev, "Failed to allocate driver access region "
2731 "(err=%d), aborting.\n", err);
2732 goto err_uar_table_free;
2735 priv->kar = ioremap((phys_addr_t) priv->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
2737 mlx4_err(dev, "Couldn't map kernel access region, "
2743 err = mlx4_init_pd_table(dev);
2745 mlx4_err(dev, "Failed to initialize "
2746 "protection domain table (err=%d), aborting.\n", err);
2750 err = mlx4_init_xrcd_table(dev);
2752 mlx4_err(dev, "Failed to initialize "
2753 "reliable connection domain table (err=%d), "
2754 "aborting.\n", err);
2755 goto err_pd_table_free;
2758 err = mlx4_init_mr_table(dev);
2760 mlx4_err(dev, "Failed to initialize "
2761 "memory region table (err=%d), aborting.\n", err);
2762 goto err_xrcd_table_free;
2765 if (!mlx4_is_slave(dev)) {
2766 err = mlx4_init_mcg_table(dev);
2768 mlx4_err(dev, "Failed to initialize "
2769 "multicast group table (err=%d), aborting.\n",
2771 goto err_mr_table_free;
2775 err = mlx4_init_eq_table(dev);
2777 mlx4_err(dev, "Failed to initialize "
2778 "event queue table (err=%d), aborting.\n", err);
2779 goto err_mcg_table_free;
2782 err = mlx4_cmd_use_events(dev);
2784 mlx4_err(dev, "Failed to switch to event-driven "
2785 "firmware commands (err=%d), aborting.\n", err);
2786 goto err_eq_table_free;
2789 err = mlx4_NOP(dev);
2791 if (dev->flags & MLX4_FLAG_MSI_X) {
2792 mlx4_warn(dev, "NOP command failed to generate MSI-X "
2793 "interrupt IRQ %d).\n",
2794 priv->eq_table.eq[dev->caps.num_comp_vectors].irq);
2795 mlx4_warn(dev, "Trying again without MSI-X.\n");
2797 mlx4_err(dev, "NOP command failed to generate interrupt "
2798 "(IRQ %d), aborting.\n",
2799 priv->eq_table.eq[dev->caps.num_comp_vectors].irq);
2800 mlx4_err(dev, "BIOS or ACPI interrupt routing problem?\n");
2806 mlx4_dbg(dev, "NOP command IRQ test passed\n");
2808 err = mlx4_init_cq_table(dev);
2810 mlx4_err(dev, "Failed to initialize "
2811 "completion queue table (err=%d), aborting.\n", err);
2815 err = mlx4_init_srq_table(dev);
2817 mlx4_err(dev, "Failed to initialize "
2818 "shared receive queue table (err=%d), aborting.\n",
2820 goto err_cq_table_free;
2823 err = mlx4_init_qp_table(dev);
2825 mlx4_err(dev, "Failed to initialize "
2826 "queue pair table (err=%d), aborting.\n", err);
2827 goto err_srq_table_free;
2830 err = mlx4_init_counters_table(dev);
2831 if (err && err != -ENOENT) {
2832 mlx4_err(dev, "Failed to initialize counters table (err=%d), "
2833 "aborting.\n", err);
2834 goto err_qp_table_free;
2837 if (!mlx4_is_slave(dev)) {
2838 for (port = 1; port <= dev->caps.num_ports; port++) {
2839 ib_port_default_caps = 0;
2840 err = mlx4_get_port_ib_caps(dev, port,
2841 &ib_port_default_caps);
2843 mlx4_warn(dev, "failed to get port %d default "
2844 "ib capabilities (%d). Continuing "
2845 "with caps = 0\n", port, err);
2846 dev->caps.ib_port_def_cap[port] = ib_port_default_caps;
2848 /* initialize per-slave default ib port capabilities */
2849 if (mlx4_is_master(dev)) {
2851 for (i = 0; i < dev->num_slaves; i++) {
2852 if (i == mlx4_master_func_num(dev))
2854 priv->mfunc.master.slave_state[i].ib_cap_mask[port] =
2855 ib_port_default_caps;
2859 dev->caps.port_ib_mtu[port] = IB_MTU_4096;
2861 err = mlx4_SET_PORT(dev, port, mlx4_is_master(dev) ?
2862 dev->caps.pkey_table_len[port] : -1);
2864 mlx4_err(dev, "Failed to set port %d (err=%d), "
2865 "aborting\n", port, err);
2866 goto err_counters_table_free;
2873 err_counters_table_free:
2874 mlx4_cleanup_counters_table(dev);
2877 mlx4_cleanup_qp_table(dev);
2880 mlx4_cleanup_srq_table(dev);
2883 mlx4_cleanup_cq_table(dev);
2886 mlx4_cmd_use_polling(dev);
2889 mlx4_cleanup_eq_table(dev);
2892 if (!mlx4_is_slave(dev))
2893 mlx4_cleanup_mcg_table(dev);
2896 mlx4_cleanup_mr_table(dev);
2898 err_xrcd_table_free:
2899 mlx4_cleanup_xrcd_table(dev);
2902 mlx4_cleanup_pd_table(dev);
2908 mlx4_uar_free(dev, &priv->driver_uar);
2911 mlx4_cleanup_uar_table(dev);
2915 static void mlx4_enable_msi_x(struct mlx4_dev *dev)
2917 struct mlx4_priv *priv = mlx4_priv(dev);
2918 struct msix_entry *entries;
2919 int nreq = min_t(int, dev->caps.num_ports *
2920 min_t(int, num_possible_cpus() + 1, MAX_MSIX_P_PORT)
2921 + MSIX_LEGACY_SZ, MAX_MSIX);
2926 nreq = min_t(int, dev->caps.num_eqs - dev->caps.reserved_eqs,
2929 if (msi_x > 1 && !mlx4_is_mfunc(dev))
2930 nreq = min_t(int, nreq, msi_x);
2932 entries = kcalloc(nreq, sizeof *entries, GFP_KERNEL);
2936 for (i = 0; i < nreq; ++i)
2937 entries[i].entry = i;
2940 err = pci_enable_msix(dev->pdev, entries, nreq);
2942 /* Try again if at least 2 vectors are available */
2944 mlx4_info(dev, "Requested %d vectors, "
2945 "but only %d MSI-X vectors available, "
2946 "trying again\n", nreq, err);
2951 /* if error, or can't alloc even 1 IRQ */
2953 mlx4_err(dev, "No IRQs left, device can't "
2961 MSIX_LEGACY_SZ + dev->caps.num_ports * MIN_MSIX_P_PORT) {
2962 /*Working in legacy mode , all EQ's shared*/
2963 dev->caps.comp_pool = 0;
2964 dev->caps.num_comp_vectors = nreq - 1;
2966 dev->caps.comp_pool = nreq - MSIX_LEGACY_SZ;
2967 dev->caps.num_comp_vectors = MSIX_LEGACY_SZ - 1;
2969 for (i = 0; i < nreq; ++i)
2970 priv->eq_table.eq[i].irq = entries[i].vector;
2972 dev->flags |= MLX4_FLAG_MSI_X;
2979 dev->caps.num_comp_vectors = 1;
2980 dev->caps.comp_pool = 0;
2982 for (i = 0; i < 2; ++i)
2983 priv->eq_table.eq[i].irq = dev->pdev->irq;
2986 dev->caps.num_comp_vectors = 0;
2987 dev->caps.comp_pool = 0;
2992 mlx4_init_hca_info(struct mlx4_dev *dev)
2994 struct mlx4_hca_info *info = &mlx4_priv(dev)->hca_info;
2998 info->firmware_attr = (struct device_attribute)__ATTR(fw_ver, S_IRUGO,
2999 show_firmware_version, NULL);
3000 if (device_create_file(&dev->pdev->dev, &info->firmware_attr))
3001 mlx4_err(dev, "Failed to add file firmware version");
3003 info->hca_attr = (struct device_attribute)__ATTR(hca, S_IRUGO, show_hca,
3005 if (device_create_file(&dev->pdev->dev, &info->hca_attr))
3006 mlx4_err(dev, "Failed to add file hca type");
3008 info->board_attr = (struct device_attribute)__ATTR(board_id, S_IRUGO,
3010 if (device_create_file(&dev->pdev->dev, &info->board_attr))
3011 mlx4_err(dev, "Failed to add file board id type");
3014 static int mlx4_init_port_info(struct mlx4_dev *dev, int port)
3016 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
3021 if (!mlx4_is_slave(dev)) {
3022 mlx4_init_mac_table(dev, &info->mac_table);
3023 mlx4_init_vlan_table(dev, &info->vlan_table);
3024 info->base_qpn = mlx4_get_base_qpn(dev, port);
3027 sprintf(info->dev_name, "mlx4_port%d", port);
3028 info->port_attr.attr.name = info->dev_name;
3029 if (mlx4_is_mfunc(dev))
3030 info->port_attr.attr.mode = S_IRUGO;
3032 info->port_attr.attr.mode = S_IRUGO | S_IWUSR;
3033 info->port_attr.store = set_port_type;
3035 info->port_attr.show = show_port_type;
3036 sysfs_attr_init(&info->port_attr.attr);
3038 err = device_create_file(&dev->pdev->dev, &info->port_attr);
3040 mlx4_err(dev, "Failed to create file for port %d\n", port);
3044 sprintf(info->dev_mtu_name, "mlx4_port%d_mtu", port);
3045 info->port_mtu_attr.attr.name = info->dev_mtu_name;
3046 if (mlx4_is_mfunc(dev))
3047 info->port_mtu_attr.attr.mode = S_IRUGO;
3049 info->port_mtu_attr.attr.mode = S_IRUGO | S_IWUSR;
3050 info->port_mtu_attr.store = set_port_ib_mtu;
3052 info->port_mtu_attr.show = show_port_ib_mtu;
3053 sysfs_attr_init(&info->port_mtu_attr.attr);
3055 err = device_create_file(&dev->pdev->dev, &info->port_mtu_attr);
3057 mlx4_err(dev, "Failed to create mtu file for port %d\n", port);
3058 device_remove_file(&info->dev->pdev->dev, &info->port_attr);
3066 mlx4_cleanup_hca_info(struct mlx4_hca_info *info)
3068 device_remove_file(&info->dev->pdev->dev, &info->firmware_attr);
3069 device_remove_file(&info->dev->pdev->dev, &info->board_attr);
3070 device_remove_file(&info->dev->pdev->dev, &info->hca_attr);
3073 static void mlx4_cleanup_port_info(struct mlx4_port_info *info)
3078 device_remove_file(&info->dev->pdev->dev, &info->port_attr);
3079 device_remove_file(&info->dev->pdev->dev, &info->port_mtu_attr);
3082 static int mlx4_init_steering(struct mlx4_dev *dev)
3084 struct mlx4_priv *priv = mlx4_priv(dev);
3085 int num_entries = dev->caps.num_ports;
3088 priv->steer = kzalloc(sizeof(struct mlx4_steer) * num_entries, GFP_KERNEL);
3092 for (i = 0; i < num_entries; i++)
3093 for (j = 0; j < MLX4_NUM_STEERS; j++) {
3094 INIT_LIST_HEAD(&priv->steer[i].promisc_qps[j]);
3095 INIT_LIST_HEAD(&priv->steer[i].steer_entries[j]);
3100 static void mlx4_clear_steering(struct mlx4_dev *dev)
3102 struct mlx4_priv *priv = mlx4_priv(dev);
3103 struct mlx4_steer_index *entry, *tmp_entry;
3104 struct mlx4_promisc_qp *pqp, *tmp_pqp;
3105 int num_entries = dev->caps.num_ports;
3108 for (i = 0; i < num_entries; i++) {
3109 for (j = 0; j < MLX4_NUM_STEERS; j++) {
3110 list_for_each_entry_safe(pqp, tmp_pqp,
3111 &priv->steer[i].promisc_qps[j],
3113 list_del(&pqp->list);
3116 list_for_each_entry_safe(entry, tmp_entry,
3117 &priv->steer[i].steer_entries[j],
3119 list_del(&entry->list);
3120 list_for_each_entry_safe(pqp, tmp_pqp,
3123 list_del(&pqp->list);
3133 static int extended_func_num(struct pci_dev *pdev)
3135 return PCI_SLOT(pdev->devfn) * 8 + PCI_FUNC(pdev->devfn);
3138 #define MLX4_OWNER_BASE 0x8069c
3139 #define MLX4_OWNER_SIZE 4
3141 static int mlx4_get_ownership(struct mlx4_dev *dev)
3143 void __iomem *owner;
3146 if (pci_channel_offline(dev->pdev))
3149 owner = ioremap(pci_resource_start(dev->pdev, 0) + MLX4_OWNER_BASE,
3152 mlx4_err(dev, "Failed to obtain ownership bit\n");
3161 static void mlx4_free_ownership(struct mlx4_dev *dev)
3163 void __iomem *owner;
3165 if (pci_channel_offline(dev->pdev))
3168 owner = ioremap(pci_resource_start(dev->pdev, 0) + MLX4_OWNER_BASE,
3171 mlx4_err(dev, "Failed to obtain ownership bit\n");
3179 static int __mlx4_init_one(struct pci_dev *pdev, int pci_dev_data)
3181 struct mlx4_priv *priv;
3182 struct mlx4_dev *dev;
3187 pr_info(DRV_NAME ": Initializing %s\n", pci_name(pdev));
3189 err = pci_enable_device(pdev);
3191 dev_err(&pdev->dev, "Cannot enable PCI device, "
3196 mlx4_get_val(num_vfs.dbdf2val.tbl, pci_physfn(pdev), 0, &nvfs);
3197 mlx4_get_val(probe_vf.dbdf2val.tbl, pci_physfn(pdev), 0, &prb_vf);
3198 if (nvfs > MLX4_MAX_NUM_VF) {
3199 dev_err(&pdev->dev, "There are more VF's (%d) than allowed(%d)\n",
3200 nvfs, MLX4_MAX_NUM_VF);
3205 dev_err(&pdev->dev, "num_vfs module parameter cannot be negative\n");
3211 if (!(pci_dev_data & MLX4_PCI_DEV_IS_VF) &&
3212 !(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
3213 dev_err(&pdev->dev, "Missing DCS, aborting."
3214 "(driver_data: 0x%x, pci_resource_flags(pdev, 0):0x%x)\n",
3215 pci_dev_data, pci_resource_flags(pdev, 0));
3217 goto err_disable_pdev;
3219 if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
3220 dev_err(&pdev->dev, "Missing UAR, aborting.\n");
3222 goto err_disable_pdev;
3225 err = pci_request_regions(pdev, DRV_NAME);
3227 dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");
3228 goto err_disable_pdev;
3231 pci_set_master(pdev);
3233 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
3235 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask.\n");
3236 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3238 dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting.\n");
3239 goto err_release_regions;
3242 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
3244 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit "
3245 "consistent PCI DMA mask.\n");
3246 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
3248 dev_err(&pdev->dev, "Can't set consistent PCI DMA mask, "
3250 goto err_release_regions;
3254 /* Allow large DMA segments, up to the firmware limit of 1 GB */
3255 dma_set_max_seg_size(&pdev->dev, 1024 * 1024 * 1024);
3257 priv = kzalloc(sizeof *priv, GFP_KERNEL);
3259 dev_err(&pdev->dev, "Device struct alloc failed, "
3262 goto err_release_regions;
3267 INIT_LIST_HEAD(&priv->dev_list);
3268 INIT_LIST_HEAD(&priv->ctx_list);
3269 spin_lock_init(&priv->ctx_lock);
3271 mutex_init(&priv->port_mutex);
3273 INIT_LIST_HEAD(&priv->pgdir_list);
3274 mutex_init(&priv->pgdir_mutex);
3276 INIT_LIST_HEAD(&priv->bf_list);
3277 mutex_init(&priv->bf_mutex);
3279 dev->rev_id = pdev->revision;
3280 dev->numa_node = dev_to_node(&pdev->dev);
3281 /* Detect if this device is a virtual function */
3282 if (pci_dev_data & MLX4_PCI_DEV_IS_VF) {
3283 /* When acting as pf, we normally skip vfs unless explicitly
3284 * requested to probe them. */
3285 if (nvfs && extended_func_num(pdev) > prb_vf) {
3286 mlx4_warn(dev, "Skipping virtual function:%d\n",
3287 extended_func_num(pdev));
3291 mlx4_warn(dev, "Detected virtual function - running in slave mode\n");
3292 dev->flags |= MLX4_FLAG_SLAVE;
3294 /* We reset the device and enable SRIOV only for physical
3295 * devices. Try to claim ownership on the device;
3296 * if already taken, skip -- do not allow multiple PFs */
3297 err = mlx4_get_ownership(dev);
3302 mlx4_warn(dev, "Multiple PFs not yet supported."
3310 mlx4_warn(dev, "Enabling SR-IOV with %d VFs\n", nvfs);
3311 err = pci_enable_sriov(pdev, nvfs);
3313 mlx4_err(dev, "Failed to enable SR-IOV, continuing without SR-IOV (err = %d).\n",
3317 mlx4_warn(dev, "Running in master mode\n");
3318 dev->flags |= MLX4_FLAG_SRIOV |
3320 dev->num_vfs = nvfs;
3324 atomic_set(&priv->opreq_count, 0);
3325 INIT_WORK(&priv->opreq_task, mlx4_opreq_action);
3328 * Now reset the HCA before we touch the PCI capabilities or
3329 * attempt a firmware command, since a boot ROM may have left
3330 * the HCA in an undefined state.
3332 err = mlx4_reset(dev);
3334 mlx4_err(dev, "Failed to reset HCA, aborting.\n");
3340 err = mlx4_cmd_init(dev);
3342 mlx4_err(dev, "Failed to init command interface, aborting.\n");
3346 /* In slave functions, the communication channel must be initialized
3347 * before posting commands. Also, init num_slaves before calling
3349 if (mlx4_is_mfunc(dev)) {
3350 if (mlx4_is_master(dev))
3351 dev->num_slaves = MLX4_MAX_NUM_SLAVES;
3353 dev->num_slaves = 0;
3354 err = mlx4_multi_func_init(dev);
3356 mlx4_err(dev, "Failed to init slave mfunc"
3357 " interface, aborting.\n");
3363 err = mlx4_init_hca(dev);
3365 if (err == -EACCES) {
3366 /* Not primary Physical function
3367 * Running in slave mode */
3368 mlx4_cmd_cleanup(dev);
3369 dev->flags |= MLX4_FLAG_SLAVE;
3370 dev->flags &= ~MLX4_FLAG_MASTER;
3376 /* In master functions, the communication channel must be initialized
3377 * after obtaining its address from fw */
3378 if (mlx4_is_master(dev)) {
3379 err = mlx4_multi_func_init(dev);
3381 mlx4_err(dev, "Failed to init master mfunc"
3382 "interface, aborting.\n");
3387 err = mlx4_alloc_eq_table(dev);
3389 goto err_master_mfunc;
3391 priv->msix_ctl.pool_bm = 0;
3392 mutex_init(&priv->msix_ctl.pool_lock);
3394 mlx4_enable_msi_x(dev);
3396 /* no MSIX and no shared IRQ */
3397 if (!dev->caps.num_comp_vectors && !dev->caps.comp_pool) {
3402 if ((mlx4_is_mfunc(dev)) &&
3403 !(dev->flags & MLX4_FLAG_MSI_X)) {
3405 mlx4_err(dev, "INTx is not supported in multi-function mode."
3410 if (!mlx4_is_slave(dev)) {
3411 err = mlx4_init_steering(dev);
3416 err = mlx4_setup_hca(dev);
3417 if (err == -EBUSY && (dev->flags & MLX4_FLAG_MSI_X) &&
3418 !mlx4_is_mfunc(dev)) {
3419 dev->flags &= ~MLX4_FLAG_MSI_X;
3420 dev->caps.num_comp_vectors = 1;
3421 dev->caps.comp_pool = 0;
3422 pci_disable_msix(pdev);
3423 err = mlx4_setup_hca(dev);
3429 mlx4_init_quotas(dev);
3430 mlx4_init_hca_info(dev);
3432 for (port = 1; port <= dev->caps.num_ports; port++) {
3433 err = mlx4_init_port_info(dev, port);
3438 err = mlx4_register_device(dev);
3442 mlx4_request_modules(dev);
3444 mlx4_sense_init(dev);
3445 mlx4_start_sense(dev);
3447 priv->pci_dev_data = pci_dev_data;
3448 pci_set_drvdata(pdev, dev);
3453 for (--port; port >= 1; --port)
3454 mlx4_cleanup_port_info(&priv->port[port]);
3456 mlx4_cleanup_counters_table(dev);
3457 mlx4_cleanup_qp_table(dev);
3458 mlx4_cleanup_srq_table(dev);
3459 mlx4_cleanup_cq_table(dev);
3460 mlx4_cmd_use_polling(dev);
3461 mlx4_cleanup_eq_table(dev);
3462 mlx4_cleanup_mcg_table(dev);
3463 mlx4_cleanup_mr_table(dev);
3464 mlx4_cleanup_xrcd_table(dev);
3465 mlx4_cleanup_pd_table(dev);
3466 mlx4_cleanup_uar_table(dev);
3469 if (!mlx4_is_slave(dev))
3470 mlx4_clear_steering(dev);
3473 mlx4_free_eq_table(dev);
3476 if (mlx4_is_master(dev)) {
3477 mlx4_free_resource_tracker(dev, RES_TR_FREE_STRUCTS_ONLY);
3478 mlx4_multi_func_cleanup(dev);
3481 if (mlx4_is_slave(dev)) {
3482 kfree(dev->caps.qp0_tunnel);
3483 kfree(dev->caps.qp0_proxy);
3484 kfree(dev->caps.qp1_tunnel);
3485 kfree(dev->caps.qp1_proxy);
3489 if (dev->flags & MLX4_FLAG_MSI_X)
3490 pci_disable_msix(pdev);
3492 mlx4_close_hca(dev);
3495 if (mlx4_is_slave(dev))
3496 mlx4_multi_func_cleanup(dev);
3499 mlx4_cmd_cleanup(dev);
3502 if (dev->flags & MLX4_FLAG_SRIOV)
3503 pci_disable_sriov(pdev);
3505 if (!mlx4_is_slave(dev))
3506 mlx4_free_ownership(dev);
3511 err_release_regions:
3512 pci_release_regions(pdev);
3515 pci_disable_device(pdev);
3516 pci_set_drvdata(pdev, NULL);
3520 static int __devinit mlx4_init_one(struct pci_dev *pdev,
3521 const struct pci_device_id *id)
3523 device_set_desc(pdev->dev.bsddev, mlx4_version);
3524 return __mlx4_init_one(pdev, id->driver_data);
3527 static void mlx4_remove_one(struct pci_dev *pdev)
3529 struct mlx4_dev *dev = pci_get_drvdata(pdev);
3530 struct mlx4_priv *priv = mlx4_priv(dev);
3534 /* in SRIOV it is not allowed to unload the pf's
3535 * driver while there are alive vf's */
3536 if (mlx4_is_master(dev)) {
3537 if (mlx4_how_many_lives_vf(dev))
3538 mlx4_err(dev, "Removing PF when there are assigned VF's !!!\n");
3540 mlx4_stop_sense(dev);
3541 mlx4_unregister_device(dev);
3543 mlx4_cleanup_hca_info(&priv->hca_info);
3544 for (p = 1; p <= dev->caps.num_ports; p++) {
3545 mlx4_cleanup_port_info(&priv->port[p]);
3546 mlx4_CLOSE_PORT(dev, p);
3549 if (mlx4_is_master(dev))
3550 mlx4_free_resource_tracker(dev,
3551 RES_TR_FREE_SLAVES_ONLY);
3553 mlx4_cleanup_counters_table(dev);
3554 mlx4_cleanup_qp_table(dev);
3555 mlx4_cleanup_srq_table(dev);
3556 mlx4_cleanup_cq_table(dev);
3557 mlx4_cmd_use_polling(dev);
3558 mlx4_cleanup_eq_table(dev);
3559 mlx4_cleanup_mcg_table(dev);
3560 mlx4_cleanup_mr_table(dev);
3561 mlx4_cleanup_xrcd_table(dev);
3562 mlx4_cleanup_pd_table(dev);
3564 if (mlx4_is_master(dev))
3565 mlx4_free_resource_tracker(dev,
3566 RES_TR_FREE_STRUCTS_ONLY);
3569 mlx4_uar_free(dev, &priv->driver_uar);
3570 mlx4_cleanup_uar_table(dev);
3571 if (!mlx4_is_slave(dev))
3572 mlx4_clear_steering(dev);
3573 mlx4_free_eq_table(dev);
3574 if (mlx4_is_master(dev))
3575 mlx4_multi_func_cleanup(dev);
3576 mlx4_close_hca(dev);
3577 if (mlx4_is_slave(dev))
3578 mlx4_multi_func_cleanup(dev);
3579 mlx4_cmd_cleanup(dev);
3581 if (dev->flags & MLX4_FLAG_MSI_X)
3582 pci_disable_msix(pdev);
3583 if (dev->flags & MLX4_FLAG_SRIOV) {
3584 mlx4_warn(dev, "Disabling SR-IOV\n");
3585 pci_disable_sriov(pdev);
3588 if (!mlx4_is_slave(dev))
3589 mlx4_free_ownership(dev);
3591 kfree(dev->caps.qp0_tunnel);
3592 kfree(dev->caps.qp0_proxy);
3593 kfree(dev->caps.qp1_tunnel);
3594 kfree(dev->caps.qp1_proxy);
3597 pci_release_regions(pdev);
3598 pci_disable_device(pdev);
3599 pci_set_drvdata(pdev, NULL);
3603 static int restore_current_port_types(struct mlx4_dev *dev,
3604 enum mlx4_port_type *types,
3605 enum mlx4_port_type *poss_types)
3607 struct mlx4_priv *priv = mlx4_priv(dev);
3610 mlx4_stop_sense(dev);
3611 mutex_lock(&priv->port_mutex);
3612 for (i = 0; i < dev->caps.num_ports; i++)
3613 dev->caps.possible_type[i + 1] = poss_types[i];
3614 err = mlx4_change_port_types(dev, types);
3615 mlx4_start_sense(dev);
3616 mutex_unlock(&priv->port_mutex);
3620 int mlx4_restart_one(struct pci_dev *pdev)
3622 struct mlx4_dev *dev = pci_get_drvdata(pdev);
3623 struct mlx4_priv *priv = mlx4_priv(dev);
3624 enum mlx4_port_type curr_type[MLX4_MAX_PORTS];
3625 enum mlx4_port_type poss_type[MLX4_MAX_PORTS];
3626 int pci_dev_data, err, i;
3628 pci_dev_data = priv->pci_dev_data;
3629 for (i = 0; i < dev->caps.num_ports; i++) {
3630 curr_type[i] = dev->caps.port_type[i + 1];
3631 poss_type[i] = dev->caps.possible_type[i + 1];
3634 mlx4_remove_one(pdev);
3635 err = __mlx4_init_one(pdev, pci_dev_data);
3639 dev = pci_get_drvdata(pdev);
3640 err = restore_current_port_types(dev, curr_type, poss_type);
3642 mlx4_err(dev, "mlx4_restart_one: could not restore original port types (%d)\n",
3647 static DEFINE_PCI_DEVICE_TABLE(mlx4_pci_table) = {
3648 /* MT25408 "Hermon" SDR */
3649 { PCI_VDEVICE(MELLANOX, 0x6340),
3650 .driver_data = MLX4_PCI_DEV_FORCE_SENSE_PORT },
3651 /* MT25408 "Hermon" DDR */
3652 { PCI_VDEVICE(MELLANOX, 0x634a),
3653 .driver_data = MLX4_PCI_DEV_FORCE_SENSE_PORT },
3654 /* MT25408 "Hermon" QDR */
3655 { PCI_VDEVICE(MELLANOX, 0x6354),
3656 .driver_data = MLX4_PCI_DEV_FORCE_SENSE_PORT },
3657 /* MT25408 "Hermon" DDR PCIe gen2 */
3658 { PCI_VDEVICE(MELLANOX, 0x6732),
3659 .driver_data = MLX4_PCI_DEV_FORCE_SENSE_PORT },
3660 /* MT25408 "Hermon" QDR PCIe gen2 */
3661 { PCI_VDEVICE(MELLANOX, 0x673c),
3662 .driver_data = MLX4_PCI_DEV_FORCE_SENSE_PORT },
3663 /* MT25408 "Hermon" EN 10GigE */
3664 { PCI_VDEVICE(MELLANOX, 0x6368),
3665 .driver_data = MLX4_PCI_DEV_FORCE_SENSE_PORT },
3666 /* MT25408 "Hermon" EN 10GigE PCIe gen2 */
3667 { PCI_VDEVICE(MELLANOX, 0x6750),
3668 .driver_data = MLX4_PCI_DEV_FORCE_SENSE_PORT },
3669 /* MT25458 ConnectX EN 10GBASE-T 10GigE */
3670 { PCI_VDEVICE(MELLANOX, 0x6372),
3671 .driver_data = MLX4_PCI_DEV_FORCE_SENSE_PORT },
3672 /* MT25458 ConnectX EN 10GBASE-T+Gen2 10GigE */
3673 { PCI_VDEVICE(MELLANOX, 0x675a),
3674 .driver_data = MLX4_PCI_DEV_FORCE_SENSE_PORT },
3675 /* MT26468 ConnectX EN 10GigE PCIe gen2*/
3676 { PCI_VDEVICE(MELLANOX, 0x6764),
3677 .driver_data = MLX4_PCI_DEV_FORCE_SENSE_PORT },
3678 /* MT26438 ConnectX EN 40GigE PCIe gen2 5GT/s */
3679 { PCI_VDEVICE(MELLANOX, 0x6746),
3680 .driver_data = MLX4_PCI_DEV_FORCE_SENSE_PORT },
3681 /* MT26478 ConnectX2 40GigE PCIe gen2 */
3682 { PCI_VDEVICE(MELLANOX, 0x676e),
3683 .driver_data = MLX4_PCI_DEV_FORCE_SENSE_PORT },
3684 /* MT25400 Family [ConnectX-2 Virtual Function] */
3685 { PCI_VDEVICE(MELLANOX, 0x1002),
3686 .driver_data = MLX4_PCI_DEV_IS_VF },
3687 /* MT27500 Family [ConnectX-3] */
3688 { PCI_VDEVICE(MELLANOX, 0x1003) },
3689 /* MT27500 Family [ConnectX-3 Virtual Function] */
3690 { PCI_VDEVICE(MELLANOX, 0x1004),
3691 .driver_data = MLX4_PCI_DEV_IS_VF },
3692 { PCI_VDEVICE(MELLANOX, 0x1005) }, /* MT27510 Family */
3693 { PCI_VDEVICE(MELLANOX, 0x1006) }, /* MT27511 Family */
3694 { PCI_VDEVICE(MELLANOX, 0x1007) }, /* MT27520 Family */
3695 { PCI_VDEVICE(MELLANOX, 0x1008) }, /* MT27521 Family */
3696 { PCI_VDEVICE(MELLANOX, 0x1009) }, /* MT27530 Family */
3697 { PCI_VDEVICE(MELLANOX, 0x100a) }, /* MT27531 Family */
3698 { PCI_VDEVICE(MELLANOX, 0x100b) }, /* MT27540 Family */
3699 { PCI_VDEVICE(MELLANOX, 0x100c) }, /* MT27541 Family */
3700 { PCI_VDEVICE(MELLANOX, 0x100d) }, /* MT27550 Family */
3701 { PCI_VDEVICE(MELLANOX, 0x100e) }, /* MT27551 Family */
3702 { PCI_VDEVICE(MELLANOX, 0x100f) }, /* MT27560 Family */
3703 { PCI_VDEVICE(MELLANOX, 0x1010) }, /* MT27561 Family */
3707 MODULE_DEVICE_TABLE(pci, mlx4_pci_table);
3709 static pci_ers_result_t mlx4_pci_err_detected(struct pci_dev *pdev,
3710 pci_channel_state_t state)
3712 mlx4_remove_one(pdev);
3714 return state == pci_channel_io_perm_failure ?
3715 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
3718 static pci_ers_result_t mlx4_pci_slot_reset(struct pci_dev *pdev)
3720 int ret = __mlx4_init_one(pdev, 0);
3722 return ret ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
3725 static const struct pci_error_handlers mlx4_err_handler = {
3726 .error_detected = mlx4_pci_err_detected,
3727 .slot_reset = mlx4_pci_slot_reset,
3730 static int suspend(struct pci_dev *pdev, pm_message_t state)
3732 mlx4_remove_one(pdev);
3737 static int resume(struct pci_dev *pdev)
3739 return __mlx4_init_one(pdev, 0);
3742 static struct pci_driver mlx4_driver = {
3744 .id_table = mlx4_pci_table,
3745 .probe = mlx4_init_one,
3746 .remove = __devexit_p(mlx4_remove_one),
3749 .err_handler = &mlx4_err_handler,
3752 static int __init mlx4_verify_params(void)
3756 status = update_defaults(&port_type_array);
3757 if (status == INVALID_STR) {
3758 if (mlx4_fill_dbdf2val_tbl(&port_type_array.dbdf2val))
3760 } else if (status == INVALID_DATA) {
3764 status = update_defaults(&num_vfs);
3765 if (status == INVALID_STR) {
3766 if (mlx4_fill_dbdf2val_tbl(&num_vfs.dbdf2val))
3768 } else if (status == INVALID_DATA) {
3772 status = update_defaults(&probe_vf);
3773 if (status == INVALID_STR) {
3774 if (mlx4_fill_dbdf2val_tbl(&probe_vf.dbdf2val))
3776 } else if (status == INVALID_DATA) {
3781 pr_warn("mlx4_core: bad msi_x: %d\n", msi_x);
3785 if ((log_num_mac < 0) || (log_num_mac > 7)) {
3786 pr_warning("mlx4_core: bad num_mac: %d\n", log_num_mac);
3790 if (log_num_vlan != 0)
3791 pr_warning("mlx4_core: log_num_vlan - obsolete module param, using %d\n",
3792 MLX4_LOG_NUM_VLANS);
3794 if (mlx4_set_4k_mtu != -1)
3795 pr_warning("mlx4_core: set_4k_mtu - obsolete module param\n");
3797 if ((log_mtts_per_seg < 0) || (log_mtts_per_seg > 7)) {
3798 pr_warning("mlx4_core: bad log_mtts_per_seg: %d\n", log_mtts_per_seg);
3802 if (mlx4_log_num_mgm_entry_size != -1 &&
3803 (mlx4_log_num_mgm_entry_size < MLX4_MIN_MGM_LOG_ENTRY_SIZE ||
3804 mlx4_log_num_mgm_entry_size > MLX4_MAX_MGM_LOG_ENTRY_SIZE)) {
3805 pr_warning("mlx4_core: mlx4_log_num_mgm_entry_size (%d) not "
3806 "in legal range (-1 or %d..%d)\n",
3807 mlx4_log_num_mgm_entry_size,
3808 MLX4_MIN_MGM_LOG_ENTRY_SIZE,
3809 MLX4_MAX_MGM_LOG_ENTRY_SIZE);
3813 if (mod_param_profile.num_qp < 18 || mod_param_profile.num_qp > 23) {
3814 pr_warning("mlx4_core: bad log_num_qp: %d\n",
3815 mod_param_profile.num_qp);
3819 if (mod_param_profile.num_srq < 10) {
3820 pr_warning("mlx4_core: too low log_num_srq: %d\n",
3821 mod_param_profile.num_srq);
3825 if (mod_param_profile.num_cq < 10) {
3826 pr_warning("mlx4_core: too low log_num_cq: %d\n",
3827 mod_param_profile.num_cq);
3831 if (mod_param_profile.num_mpt < 10) {
3832 pr_warning("mlx4_core: too low log_num_mpt: %d\n",
3833 mod_param_profile.num_mpt);
3837 if (mod_param_profile.num_mtt_segs &&
3838 mod_param_profile.num_mtt_segs < 15) {
3839 pr_warning("mlx4_core: too low log_num_mtt: %d\n",
3840 mod_param_profile.num_mtt_segs);
3844 if (mod_param_profile.num_mtt_segs > MLX4_MAX_LOG_NUM_MTT) {
3845 pr_warning("mlx4_core: too high log_num_mtt: %d\n",
3846 mod_param_profile.num_mtt_segs);
3852 static int __init mlx4_init(void)
3856 if (mlx4_verify_params())
3861 mlx4_wq = create_singlethread_workqueue("mlx4");
3865 if (enable_sys_tune)
3868 ret = pci_register_driver(&mlx4_driver);
3875 if (enable_sys_tune)
3878 destroy_workqueue(mlx4_wq);
3883 static void __exit mlx4_cleanup(void)
3885 if (enable_sys_tune)
3888 pci_unregister_driver(&mlx4_driver);
3889 destroy_workqueue(mlx4_wq);
3892 module_init_order(mlx4_init, SI_ORDER_MIDDLE);
3893 module_exit(mlx4_cleanup);
3895 #include <sys/module.h>
3897 mlx4_evhand(module_t mod, int event, void *arg)
3902 static moduledata_t mlx4_mod = {
3904 .evhand = mlx4_evhand,
3906 MODULE_VERSION(mlx4, 1);
3907 DECLARE_MODULE(mlx4, mlx4_mod, SI_SUB_OFED_PREINIT, SI_ORDER_ANY);