blob: 5a6105f1ba6d83c017ec85a5e8031454eebf7e0e [file] [log] [blame]
Roland Dreier225c7b12007-05-08 18:00:38 -07001/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
Jack Morgenstein51a379d2008-07-25 10:32:52 -07004 * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
Roland Dreier225c7b12007-05-08 18:00:38 -07005 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
6 *
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
12 *
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
16 *
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
20 *
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
34 */
35
36#include <linux/module.h>
37#include <linux/init.h>
38#include <linux/errno.h>
39#include <linux/pci.h>
40#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Eli Cohenc1b43dc2011-03-22 22:38:41 +000042#include <linux/io-mapping.h>
Jack Morgensteinab9c17a2011-12-13 04:18:30 +000043#include <linux/delay.h>
Yuval Mintz90b1ebe2012-07-01 03:18:51 +000044#include <linux/netdevice.h>
Eyal Perryb046ffe2013-10-15 16:55:24 +020045#include <linux/kmod.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070046
47#include <linux/mlx4/device.h>
48#include <linux/mlx4/doorbell.h>
49
50#include "mlx4.h"
51#include "fw.h"
52#include "icm.h"
53
54MODULE_AUTHOR("Roland Dreier");
55MODULE_DESCRIPTION("Mellanox ConnectX HCA low-level driver");
56MODULE_LICENSE("Dual BSD/GPL");
57MODULE_VERSION(DRV_VERSION);
58
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -070059struct workqueue_struct *mlx4_wq;
60
Roland Dreier225c7b12007-05-08 18:00:38 -070061#ifdef CONFIG_MLX4_DEBUG
62
63int mlx4_debug_level = 0;
64module_param_named(debug_level, mlx4_debug_level, int, 0644);
65MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
66
67#endif /* CONFIG_MLX4_DEBUG */
68
69#ifdef CONFIG_PCI_MSI
70
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +030071static int msi_x = 1;
Roland Dreier225c7b12007-05-08 18:00:38 -070072module_param(msi_x, int, 0444);
73MODULE_PARM_DESC(msi_x, "attempt to use MSI-X if nonzero");
74
75#else /* CONFIG_PCI_MSI */
76
77#define msi_x (0)
78
79#endif /* CONFIG_PCI_MSI */
80
Jack Morgensteinab9c17a2011-12-13 04:18:30 +000081static int num_vfs;
82module_param(num_vfs, int, 0444);
83MODULE_PARM_DESC(num_vfs, "enable #num_vfs functions if num_vfs > 0");
84
85static int probe_vf;
86module_param(probe_vf, int, 0644);
87MODULE_PARM_DESC(probe_vf, "number of vfs to probe by pf driver (num_vfs > 0)");
88
Jack Morgenstein3c439b52012-12-06 17:12:00 +000089int mlx4_log_num_mgm_entry_size = MLX4_DEFAULT_MGM_LOG_ENTRY_SIZE;
Eugenia Emantayev0ec2c0f2011-12-13 04:16:02 +000090module_param_named(log_num_mgm_entry_size,
91 mlx4_log_num_mgm_entry_size, int, 0444);
92MODULE_PARM_DESC(log_num_mgm_entry_size, "log mgm size, that defines the num"
93 " of qp per mcg, for example:"
Jack Morgenstein3c439b52012-12-06 17:12:00 +000094 " 10 gives 248.range: 7 <="
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +000095 " log_num_mgm_entry_size <= 12."
Jack Morgenstein3c439b52012-12-06 17:12:00 +000096 " To activate device managed"
97 " flow steering when available, set to -1");
Eugenia Emantayev0ec2c0f2011-12-13 04:16:02 +000098
Eyal Perrybe902ab2013-12-19 21:20:15 +020099static bool enable_64b_cqe_eqe = true;
Or Gerlitz08ff3232012-10-21 14:59:24 +0000100module_param(enable_64b_cqe_eqe, bool, 0444);
101MODULE_PARM_DESC(enable_64b_cqe_eqe,
Eyal Perrybe902ab2013-12-19 21:20:15 +0200102 "Enable 64 byte CQEs/EQEs when the FW supports this (default: True)");
Or Gerlitz08ff3232012-10-21 14:59:24 +0000103
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000104#define HCA_GLOBAL_CAP_MASK 0
Or Gerlitz08ff3232012-10-21 14:59:24 +0000105
106#define PF_CONTEXT_BEHAVIOUR_MASK MLX4_FUNC_CAP_64B_EQE_CQE
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000107
Bill Pembertonf57e6842012-12-03 09:23:15 -0500108static char mlx4_version[] =
Roland Dreier225c7b12007-05-08 18:00:38 -0700109 DRV_NAME ": Mellanox ConnectX core driver v"
110 DRV_VERSION " (" DRV_RELDATE ")\n";
111
112static struct mlx4_profile default_profile = {
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000113 .num_qp = 1 << 18,
Roland Dreier225c7b12007-05-08 18:00:38 -0700114 .num_srq = 1 << 16,
Jack Morgensteinc9f2ba52007-07-17 13:11:43 +0300115 .rdmarc_per_qp = 1 << 4,
Roland Dreier225c7b12007-05-08 18:00:38 -0700116 .num_cq = 1 << 16,
117 .num_mcg = 1 << 13,
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000118 .num_mpt = 1 << 19,
Marcel Apfelbaum9fd7a1e2012-01-19 09:45:31 +0000119 .num_mtt = 1 << 20, /* It is really num mtt segements */
Roland Dreier225c7b12007-05-08 18:00:38 -0700120};
121
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000122static int log_num_mac = 7;
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700123module_param_named(log_num_mac, log_num_mac, int, 0444);
124MODULE_PARM_DESC(log_num_mac, "Log2 max number of MACs per ETH port (1-7)");
125
126static int log_num_vlan;
127module_param_named(log_num_vlan, log_num_vlan, int, 0444);
128MODULE_PARM_DESC(log_num_vlan, "Log2 max number of VLANs per ETH port (0-7)");
Or Gerlitzcb296882011-10-16 10:26:21 +0200129/* Log2 max number of VLANs per ETH port (0-7) */
130#define MLX4_LOG_NUM_VLANS 7
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700131
Rusty Russelleb939922011-12-19 14:08:01 +0000132static bool use_prio;
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700133module_param_named(use_prio, use_prio, bool, 0444);
134MODULE_PARM_DESC(use_prio, "Enable steering by VLAN priority on ETH ports "
135 "(0/1, default 0)");
136
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000137int log_mtts_per_seg = ilog2(MLX4_MTT_ENTRY_PER_SEG);
Eli Cohenab6bf422009-05-27 14:38:34 -0700138module_param_named(log_mtts_per_seg, log_mtts_per_seg, int, 0444);
Eli Cohen04986282010-09-20 08:42:38 +0200139MODULE_PARM_DESC(log_mtts_per_seg, "Log2 number of MTT entries per segment (1-7)");
Eli Cohenab6bf422009-05-27 14:38:34 -0700140
Yevgeny Petrilin8d0fc7b2011-12-19 04:00:34 +0000141static int port_type_array[2] = {MLX4_PORT_TYPE_NONE, MLX4_PORT_TYPE_NONE};
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000142static int arr_argc = 2;
143module_param_array(port_type_array, int, &arr_argc, 0444);
Yevgeny Petrilin8d0fc7b2011-12-19 04:00:34 +0000144MODULE_PARM_DESC(port_type_array, "Array of port types: HW_DEFAULT (0) is default "
145 "1 for IB, 2 for Ethernet");
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000146
147struct mlx4_port_config {
148 struct list_head list;
149 enum mlx4_port_type port_type[MLX4_MAX_PORTS + 1];
150 struct pci_dev *pdev;
151};
152
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700153int mlx4_check_port_params(struct mlx4_dev *dev,
154 enum mlx4_port_type *port_type)
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700155{
156 int i;
157
158 for (i = 0; i < dev->caps.num_ports - 1; i++) {
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700159 if (port_type[i] != port_type[i + 1]) {
160 if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP)) {
161 mlx4_err(dev, "Only same port types supported "
162 "on this HCA, aborting.\n");
163 return -EINVAL;
164 }
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700165 }
166 }
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700167
168 for (i = 0; i < dev->caps.num_ports; i++) {
169 if (!(port_type[i] & dev->caps.supported_type[i+1])) {
170 mlx4_err(dev, "Requested port type for port %d is not "
171 "supported on this HCA\n", i + 1);
172 return -EINVAL;
173 }
174 }
175 return 0;
176}
177
178static void mlx4_set_port_mask(struct mlx4_dev *dev)
179{
180 int i;
181
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700182 for (i = 1; i <= dev->caps.num_ports; ++i)
Jack Morgenstein65dab252011-12-13 04:10:41 +0000183 dev->caps.port_mask[i] = dev->caps.port_type[i];
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700184}
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +0000185
Roland Dreier3d73c282007-10-10 15:43:54 -0700186static int mlx4_dev_cap(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap)
Roland Dreier225c7b12007-05-08 18:00:38 -0700187{
188 int err;
Roland Dreier5ae2a7a2007-06-18 08:15:02 -0700189 int i;
Roland Dreier225c7b12007-05-08 18:00:38 -0700190
191 err = mlx4_QUERY_DEV_CAP(dev, dev_cap);
192 if (err) {
193 mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
194 return err;
195 }
196
197 if (dev_cap->min_page_sz > PAGE_SIZE) {
198 mlx4_err(dev, "HCA minimum page size of %d bigger than "
199 "kernel PAGE_SIZE of %ld, aborting.\n",
200 dev_cap->min_page_sz, PAGE_SIZE);
201 return -ENODEV;
202 }
203 if (dev_cap->num_ports > MLX4_MAX_PORTS) {
204 mlx4_err(dev, "HCA has %d ports, but we only support %d, "
205 "aborting.\n",
206 dev_cap->num_ports, MLX4_MAX_PORTS);
207 return -ENODEV;
208 }
209
210 if (dev_cap->uar_size > pci_resource_len(dev->pdev, 2)) {
211 mlx4_err(dev, "HCA reported UAR size of 0x%x bigger than "
212 "PCI resource 2 size of 0x%llx, aborting.\n",
213 dev_cap->uar_size,
214 (unsigned long long) pci_resource_len(dev->pdev, 2));
215 return -ENODEV;
216 }
217
218 dev->caps.num_ports = dev_cap->num_ports;
Marcel Apfelbaum3fc929e2012-05-30 09:14:51 +0000219 dev->phys_caps.num_phys_eqs = MLX4_MAX_EQ_NUM;
Roland Dreier5ae2a7a2007-06-18 08:15:02 -0700220 for (i = 1; i <= dev->caps.num_ports; ++i) {
221 dev->caps.vl_cap[i] = dev_cap->max_vl[i];
Yevgeny Petrilinb79acb42008-10-22 10:56:48 -0700222 dev->caps.ib_mtu_cap[i] = dev_cap->ib_mtu[i];
Jack Morgenstein66349612012-06-19 11:21:44 +0300223 dev->phys_caps.gid_phys_table_len[i] = dev_cap->max_gids[i];
224 dev->phys_caps.pkey_phys_table_len[i] = dev_cap->max_pkeys[i];
225 /* set gid and pkey table operating lengths by default
226 * to non-sriov values */
Roland Dreier5ae2a7a2007-06-18 08:15:02 -0700227 dev->caps.gid_table_len[i] = dev_cap->max_gids[i];
228 dev->caps.pkey_table_len[i] = dev_cap->max_pkeys[i];
229 dev->caps.port_width_cap[i] = dev_cap->max_port_width[i];
Yevgeny Petrilinb79acb42008-10-22 10:56:48 -0700230 dev->caps.eth_mtu_cap[i] = dev_cap->eth_mtu[i];
231 dev->caps.def_mac[i] = dev_cap->def_mac[i];
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700232 dev->caps.supported_type[i] = dev_cap->supported_port_types[i];
Yevgeny Petrilin8d0fc7b2011-12-19 04:00:34 +0000233 dev->caps.suggested_type[i] = dev_cap->suggested_type[i];
234 dev->caps.default_sense[i] = dev_cap->default_sense[i];
Yevgeny Petrilin76995172010-08-24 03:46:23 +0000235 dev->caps.trans_type[i] = dev_cap->trans_type[i];
236 dev->caps.vendor_oui[i] = dev_cap->vendor_oui[i];
237 dev->caps.wavelength[i] = dev_cap->wavelength[i];
238 dev->caps.trans_code[i] = dev_cap->trans_code[i];
Roland Dreier5ae2a7a2007-06-18 08:15:02 -0700239 }
240
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000241 dev->caps.uar_page_size = PAGE_SIZE;
Roland Dreier225c7b12007-05-08 18:00:38 -0700242 dev->caps.num_uars = dev_cap->uar_size / PAGE_SIZE;
Roland Dreier225c7b12007-05-08 18:00:38 -0700243 dev->caps.local_ca_ack_delay = dev_cap->local_ca_ack_delay;
244 dev->caps.bf_reg_size = dev_cap->bf_reg_size;
245 dev->caps.bf_regs_per_page = dev_cap->bf_regs_per_page;
246 dev->caps.max_sq_sg = dev_cap->max_sq_sg;
247 dev->caps.max_rq_sg = dev_cap->max_rq_sg;
248 dev->caps.max_wqes = dev_cap->max_qp_sz;
249 dev->caps.max_qp_init_rdma = dev_cap->max_requester_per_qp;
Roland Dreier225c7b12007-05-08 18:00:38 -0700250 dev->caps.max_srq_wqes = dev_cap->max_srq_sz;
251 dev->caps.max_srq_sge = dev_cap->max_rq_sg - 1;
252 dev->caps.reserved_srqs = dev_cap->reserved_srqs;
253 dev->caps.max_sq_desc_sz = dev_cap->max_sq_desc_sz;
254 dev->caps.max_rq_desc_sz = dev_cap->max_rq_desc_sz;
Roland Dreier225c7b12007-05-08 18:00:38 -0700255 /*
256 * Subtract 1 from the limit because we need to allocate a
257 * spare CQE so the HCA HW can tell the difference between an
258 * empty CQ and a full CQ.
259 */
260 dev->caps.max_cqes = dev_cap->max_cq_sz - 1;
261 dev->caps.reserved_cqs = dev_cap->reserved_cqs;
262 dev->caps.reserved_eqs = dev_cap->reserved_eqs;
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000263 dev->caps.reserved_mtts = dev_cap->reserved_mtts;
Roland Dreier225c7b12007-05-08 18:00:38 -0700264 dev->caps.reserved_mrws = dev_cap->reserved_mrws;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000265
266 /* The first 128 UARs are used for EQ doorbells */
267 dev->caps.reserved_uars = max_t(int, 128, dev_cap->reserved_uars);
Roland Dreier225c7b12007-05-08 18:00:38 -0700268 dev->caps.reserved_pds = dev_cap->reserved_pds;
Sean Hefty012a8ff2011-06-02 09:01:33 -0700269 dev->caps.reserved_xrcds = (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) ?
270 dev_cap->reserved_xrcds : 0;
271 dev->caps.max_xrcds = (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) ?
272 dev_cap->max_xrcds : 0;
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000273 dev->caps.mtt_entry_sz = dev_cap->mtt_entry_sz;
274
Dotan Barak149983af2007-06-26 15:55:28 +0300275 dev->caps.max_msg_sz = dev_cap->max_msg_sz;
Roland Dreier225c7b12007-05-08 18:00:38 -0700276 dev->caps.page_size_cap = ~(u32) (dev_cap->min_page_sz - 1);
277 dev->caps.flags = dev_cap->flags;
Shlomo Pongratzb3416f42012-04-29 17:04:25 +0300278 dev->caps.flags2 = dev_cap->flags2;
Roland Dreier95d04f02008-07-23 08:12:26 -0700279 dev->caps.bmme_flags = dev_cap->bmme_flags;
280 dev->caps.reserved_lkey = dev_cap->reserved_lkey;
Roland Dreier225c7b12007-05-08 18:00:38 -0700281 dev->caps.stat_rate_support = dev_cap->stat_rate_support;
Eli Cohenb832be12008-04-16 21:09:27 -0700282 dev->caps.max_gso_sz = dev_cap->max_gso_sz;
Shlomo Pongratzb3416f42012-04-29 17:04:25 +0300283 dev->caps.max_rss_tbl_sz = dev_cap->max_rss_tbl_sz;
Roland Dreier225c7b12007-05-08 18:00:38 -0700284
Roland Dreierca3e57a2012-09-27 09:53:05 -0700285 /* Sense port always allowed on supported devices for ConnectX-1 and -2 */
286 if (mlx4_priv(dev)->pci_dev_data & MLX4_PCI_DEV_FORCE_SENSE_PORT)
Yevgeny Petrilin58a60162011-12-19 04:00:26 +0000287 dev->caps.flags |= MLX4_DEV_CAP_FLAG_SENSE_SUPPORT;
Roland Dreieraadf4f32012-09-27 10:01:19 -0700288 /* Don't do sense port on multifunction devices (for now at least) */
289 if (mlx4_is_mfunc(dev))
290 dev->caps.flags &= ~MLX4_DEV_CAP_FLAG_SENSE_SUPPORT;
Yevgeny Petrilin58a60162011-12-19 04:00:26 +0000291
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700292 dev->caps.log_num_macs = log_num_mac;
Or Gerlitzcb296882011-10-16 10:26:21 +0200293 dev->caps.log_num_vlans = MLX4_LOG_NUM_VLANS;
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700294 dev->caps.log_num_prios = use_prio ? 3 : 0;
295
296 for (i = 1; i <= dev->caps.num_ports; ++i) {
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000297 dev->caps.port_type[i] = MLX4_PORT_TYPE_NONE;
298 if (dev->caps.supported_type[i]) {
299 /* if only ETH is supported - assign ETH */
300 if (dev->caps.supported_type[i] == MLX4_PORT_TYPE_ETH)
301 dev->caps.port_type[i] = MLX4_PORT_TYPE_ETH;
Jack Morgenstein105c3202012-06-19 11:21:43 +0300302 /* if only IB is supported, assign IB */
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000303 else if (dev->caps.supported_type[i] ==
Jack Morgenstein105c3202012-06-19 11:21:43 +0300304 MLX4_PORT_TYPE_IB)
305 dev->caps.port_type[i] = MLX4_PORT_TYPE_IB;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000306 else {
Jack Morgenstein105c3202012-06-19 11:21:43 +0300307 /* if IB and ETH are supported, we set the port
308 * type according to user selection of port type;
309 * if user selected none, take the FW hint */
310 if (port_type_array[i - 1] == MLX4_PORT_TYPE_NONE)
Yevgeny Petrilin8d0fc7b2011-12-19 04:00:34 +0000311 dev->caps.port_type[i] = dev->caps.suggested_type[i] ?
312 MLX4_PORT_TYPE_ETH : MLX4_PORT_TYPE_IB;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000313 else
Jack Morgenstein105c3202012-06-19 11:21:43 +0300314 dev->caps.port_type[i] = port_type_array[i - 1];
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000315 }
316 }
Yevgeny Petrilin8d0fc7b2011-12-19 04:00:34 +0000317 /*
318 * Link sensing is allowed on the port if 3 conditions are true:
319 * 1. Both protocols are supported on the port.
320 * 2. Different types are supported on the port
321 * 3. FW declared that it supports link sensing
322 */
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700323 mlx4_priv(dev)->sense.sense_allowed[i] =
Yevgeny Petrilin58a60162011-12-19 04:00:26 +0000324 ((dev->caps.supported_type[i] == MLX4_PORT_TYPE_AUTO) &&
Yevgeny Petrilin8d0fc7b2011-12-19 04:00:34 +0000325 (dev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP) &&
Yevgeny Petrilin58a60162011-12-19 04:00:26 +0000326 (dev->caps.flags & MLX4_DEV_CAP_FLAG_SENSE_SUPPORT));
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700327
Yevgeny Petrilin8d0fc7b2011-12-19 04:00:34 +0000328 /*
329 * If "default_sense" bit is set, we move the port to "AUTO" mode
330 * and perform sense_port FW command to try and set the correct
331 * port type from beginning
332 */
Yevgeny Petrilin46c46742011-12-29 07:42:34 +0000333 if (mlx4_priv(dev)->sense.sense_allowed[i] && dev->caps.default_sense[i]) {
Yevgeny Petrilin8d0fc7b2011-12-19 04:00:34 +0000334 enum mlx4_port_type sensed_port = MLX4_PORT_TYPE_NONE;
335 dev->caps.possible_type[i] = MLX4_PORT_TYPE_AUTO;
336 mlx4_SENSE_PORT(dev, i, &sensed_port);
337 if (sensed_port != MLX4_PORT_TYPE_NONE)
338 dev->caps.port_type[i] = sensed_port;
339 } else {
340 dev->caps.possible_type[i] = dev->caps.port_type[i];
341 }
342
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700343 if (dev->caps.log_num_macs > dev_cap->log_max_macs[i]) {
344 dev->caps.log_num_macs = dev_cap->log_max_macs[i];
345 mlx4_warn(dev, "Requested number of MACs is too much "
346 "for port %d, reducing to %d.\n",
347 i, 1 << dev->caps.log_num_macs);
348 }
349 if (dev->caps.log_num_vlans > dev_cap->log_max_vlans[i]) {
350 dev->caps.log_num_vlans = dev_cap->log_max_vlans[i];
351 mlx4_warn(dev, "Requested number of VLANs is too much "
352 "for port %d, reducing to %d.\n",
353 i, 1 << dev->caps.log_num_vlans);
354 }
355 }
356
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +0000357 dev->caps.max_counters = 1 << ilog2(dev_cap->max_counters);
358
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700359 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW] = dev_cap->reserved_qps;
360 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_ETH_ADDR] =
361 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_ADDR] =
362 (1 << dev->caps.log_num_macs) *
363 (1 << dev->caps.log_num_vlans) *
364 (1 << dev->caps.log_num_prios) *
365 dev->caps.num_ports;
366 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_EXCH] = MLX4_NUM_FEXCH;
367
368 dev->caps.reserved_qps = dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW] +
369 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_ETH_ADDR] +
370 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_ADDR] +
371 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_EXCH];
372
Jack Morgensteine2c76822012-08-03 08:40:41 +0000373 dev->caps.sqp_demux = (mlx4_is_master(dev)) ? MLX4_MAX_NUM_SLAVES : 0;
Or Gerlitz08ff3232012-10-21 14:59:24 +0000374
Jack Morgensteinb3051322013-08-01 19:55:01 +0300375 if (!enable_64b_cqe_eqe && !mlx4_is_slave(dev)) {
Or Gerlitz08ff3232012-10-21 14:59:24 +0000376 if (dev_cap->flags &
377 (MLX4_DEV_CAP_FLAG_64B_CQE | MLX4_DEV_CAP_FLAG_64B_EQE)) {
378 mlx4_warn(dev, "64B EQEs/CQEs supported by the device but not enabled\n");
379 dev->caps.flags &= ~MLX4_DEV_CAP_FLAG_64B_CQE;
380 dev->caps.flags &= ~MLX4_DEV_CAP_FLAG_64B_EQE;
381 }
382 }
383
Or Gerlitzf97b4b52013-01-10 15:18:35 +0000384 if ((dev->caps.flags &
Or Gerlitz08ff3232012-10-21 14:59:24 +0000385 (MLX4_DEV_CAP_FLAG_64B_CQE | MLX4_DEV_CAP_FLAG_64B_EQE)) &&
386 mlx4_is_master(dev))
387 dev->caps.function_caps |= MLX4_FUNC_CAP_64B_EQE_CQE;
388
Roland Dreier225c7b12007-05-08 18:00:38 -0700389 return 0;
390}
Eyal Perryb912b2f2014-01-05 17:41:08 +0200391
392static int mlx4_get_pcie_dev_link_caps(struct mlx4_dev *dev,
393 enum pci_bus_speed *speed,
394 enum pcie_link_width *width)
395{
396 u32 lnkcap1, lnkcap2;
397 int err1, err2;
398
399#define PCIE_MLW_CAP_SHIFT 4 /* start of MLW mask in link capabilities */
400
401 *speed = PCI_SPEED_UNKNOWN;
402 *width = PCIE_LNK_WIDTH_UNKNOWN;
403
404 err1 = pcie_capability_read_dword(dev->pdev, PCI_EXP_LNKCAP, &lnkcap1);
405 err2 = pcie_capability_read_dword(dev->pdev, PCI_EXP_LNKCAP2, &lnkcap2);
406 if (!err2 && lnkcap2) { /* PCIe r3.0-compliant */
407 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
408 *speed = PCIE_SPEED_8_0GT;
409 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
410 *speed = PCIE_SPEED_5_0GT;
411 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
412 *speed = PCIE_SPEED_2_5GT;
413 }
414 if (!err1) {
415 *width = (lnkcap1 & PCI_EXP_LNKCAP_MLW) >> PCIE_MLW_CAP_SHIFT;
416 if (!lnkcap2) { /* pre-r3.0 */
417 if (lnkcap1 & PCI_EXP_LNKCAP_SLS_5_0GB)
418 *speed = PCIE_SPEED_5_0GT;
419 else if (lnkcap1 & PCI_EXP_LNKCAP_SLS_2_5GB)
420 *speed = PCIE_SPEED_2_5GT;
421 }
422 }
423
424 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN) {
425 return err1 ? err1 :
426 err2 ? err2 : -EINVAL;
427 }
428 return 0;
429}
430
431static void mlx4_check_pcie_caps(struct mlx4_dev *dev)
432{
433 enum pcie_link_width width, width_cap;
434 enum pci_bus_speed speed, speed_cap;
435 int err;
436
437#define PCIE_SPEED_STR(speed) \
438 (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" : \
439 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" : \
440 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" : \
441 "Unknown")
442
443 err = mlx4_get_pcie_dev_link_caps(dev, &speed_cap, &width_cap);
444 if (err) {
445 mlx4_warn(dev,
446 "Unable to determine PCIe device BW capabilities\n");
447 return;
448 }
449
450 err = pcie_get_minimum_link(dev->pdev, &speed, &width);
451 if (err || speed == PCI_SPEED_UNKNOWN ||
452 width == PCIE_LNK_WIDTH_UNKNOWN) {
453 mlx4_warn(dev,
454 "Unable to determine PCI device chain minimum BW\n");
455 return;
456 }
457
458 if (width != width_cap || speed != speed_cap)
459 mlx4_warn(dev,
460 "PCIe BW is different than device's capability\n");
461
462 mlx4_info(dev, "PCIe link speed is %s, device supports %s\n",
463 PCIE_SPEED_STR(speed), PCIE_SPEED_STR(speed_cap));
464 mlx4_info(dev, "PCIe link width is x%d, device supports x%d\n",
465 width, width_cap);
466 return;
467}
468
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000469/*The function checks if there are live vf, return the num of them*/
470static int mlx4_how_many_lives_vf(struct mlx4_dev *dev)
471{
472 struct mlx4_priv *priv = mlx4_priv(dev);
473 struct mlx4_slave_state *s_state;
474 int i;
475 int ret = 0;
476
477 for (i = 1/*the ppf is 0*/; i < dev->num_slaves; ++i) {
478 s_state = &priv->mfunc.master.slave_state[i];
479 if (s_state->active && s_state->last_cmd !=
480 MLX4_COMM_CMD_RESET) {
481 mlx4_warn(dev, "%s: slave: %d is still active\n",
482 __func__, i);
483 ret++;
484 }
485 }
486 return ret;
487}
488
Jack Morgenstein396f2fe2012-06-19 11:21:42 +0300489int mlx4_get_parav_qkey(struct mlx4_dev *dev, u32 qpn, u32 *qkey)
490{
491 u32 qk = MLX4_RESERVED_QKEY_BASE;
Jack Morgenstein47605df2012-08-03 08:40:57 +0000492
493 if (qpn >= dev->phys_caps.base_tunnel_sqpn + 8 * MLX4_MFUNC_MAX ||
494 qpn < dev->phys_caps.base_proxy_sqpn)
Jack Morgenstein396f2fe2012-06-19 11:21:42 +0300495 return -EINVAL;
496
Jack Morgenstein47605df2012-08-03 08:40:57 +0000497 if (qpn >= dev->phys_caps.base_tunnel_sqpn)
Jack Morgenstein396f2fe2012-06-19 11:21:42 +0300498 /* tunnel qp */
Jack Morgenstein47605df2012-08-03 08:40:57 +0000499 qk += qpn - dev->phys_caps.base_tunnel_sqpn;
Jack Morgenstein396f2fe2012-06-19 11:21:42 +0300500 else
Jack Morgenstein47605df2012-08-03 08:40:57 +0000501 qk += qpn - dev->phys_caps.base_proxy_sqpn;
Jack Morgenstein396f2fe2012-06-19 11:21:42 +0300502 *qkey = qk;
503 return 0;
504}
505EXPORT_SYMBOL(mlx4_get_parav_qkey);
506
Jack Morgenstein54679e12012-08-03 08:40:43 +0000507void mlx4_sync_pkey_table(struct mlx4_dev *dev, int slave, int port, int i, int val)
508{
509 struct mlx4_priv *priv = container_of(dev, struct mlx4_priv, dev);
510
511 if (!mlx4_is_master(dev))
512 return;
513
514 priv->virt2phys_pkey[slave][port - 1][i] = val;
515}
516EXPORT_SYMBOL(mlx4_sync_pkey_table);
517
Jack Morgensteinafa8fd12012-08-03 08:40:56 +0000518void mlx4_put_slave_node_guid(struct mlx4_dev *dev, int slave, __be64 guid)
519{
520 struct mlx4_priv *priv = container_of(dev, struct mlx4_priv, dev);
521
522 if (!mlx4_is_master(dev))
523 return;
524
525 priv->slave_node_guids[slave] = guid;
526}
527EXPORT_SYMBOL(mlx4_put_slave_node_guid);
528
529__be64 mlx4_get_slave_node_guid(struct mlx4_dev *dev, int slave)
530{
531 struct mlx4_priv *priv = container_of(dev, struct mlx4_priv, dev);
532
533 if (!mlx4_is_master(dev))
534 return 0;
535
536 return priv->slave_node_guids[slave];
537}
538EXPORT_SYMBOL(mlx4_get_slave_node_guid);
539
Roland Dreiere10903b2012-02-26 01:48:12 -0800540int mlx4_is_slave_active(struct mlx4_dev *dev, int slave)
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000541{
542 struct mlx4_priv *priv = mlx4_priv(dev);
543 struct mlx4_slave_state *s_slave;
544
545 if (!mlx4_is_master(dev))
546 return 0;
547
548 s_slave = &priv->mfunc.master.slave_state[slave];
549 return !!s_slave->active;
550}
551EXPORT_SYMBOL(mlx4_is_slave_active);
552
Jack Morgenstein7b8157b2012-12-06 17:11:59 +0000553static void slave_adjust_steering_mode(struct mlx4_dev *dev,
554 struct mlx4_dev_cap *dev_cap,
555 struct mlx4_init_hca_param *hca_param)
556{
557 dev->caps.steering_mode = hca_param->steering_mode;
558 if (dev->caps.steering_mode == MLX4_STEERING_MODE_DEVICE_MANAGED) {
559 dev->caps.num_qp_per_mgm = dev_cap->fs_max_num_qp_per_entry;
560 dev->caps.fs_log_max_ucast_qp_range_size =
561 dev_cap->fs_log_max_ucast_qp_range_size;
562 } else
563 dev->caps.num_qp_per_mgm =
564 4 * ((1 << hca_param->log_mc_entry_sz)/16 - 2);
565
566 mlx4_dbg(dev, "Steering mode is: %s\n",
567 mlx4_steering_mode_str(dev->caps.steering_mode));
568}
569
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000570static int mlx4_slave_cap(struct mlx4_dev *dev)
571{
572 int err;
573 u32 page_size;
574 struct mlx4_dev_cap dev_cap;
575 struct mlx4_func_cap func_cap;
576 struct mlx4_init_hca_param hca_param;
577 int i;
578
579 memset(&hca_param, 0, sizeof(hca_param));
580 err = mlx4_QUERY_HCA(dev, &hca_param);
581 if (err) {
582 mlx4_err(dev, "QUERY_HCA command failed, aborting.\n");
583 return err;
584 }
585
586 /*fail if the hca has an unknown capability */
587 if ((hca_param.global_caps | HCA_GLOBAL_CAP_MASK) !=
588 HCA_GLOBAL_CAP_MASK) {
589 mlx4_err(dev, "Unknown hca global capabilities\n");
590 return -ENOSYS;
591 }
592
593 mlx4_log_num_mgm_entry_size = hca_param.log_mc_entry_sz;
594
Eugenia Emantayevddd8a6c2013-04-23 06:06:48 +0000595 dev->caps.hca_core_clock = hca_param.hca_core_clock;
596
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000597 memset(&dev_cap, 0, sizeof(dev_cap));
Jack Morgensteinb91cb3e2012-05-30 09:14:53 +0000598 dev->caps.max_qp_dest_rdma = 1 << hca_param.log_rd_per_qp;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000599 err = mlx4_dev_cap(dev, &dev_cap);
600 if (err) {
601 mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
602 return err;
603 }
604
Jack Morgensteinb91cb3e2012-05-30 09:14:53 +0000605 err = mlx4_QUERY_FW(dev);
606 if (err)
607 mlx4_err(dev, "QUERY_FW command failed: could not get FW version.\n");
608
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000609 page_size = ~dev->caps.page_size_cap + 1;
610 mlx4_warn(dev, "HCA minimum page size:%d\n", page_size);
611 if (page_size > PAGE_SIZE) {
612 mlx4_err(dev, "HCA minimum page size of %d bigger than "
613 "kernel PAGE_SIZE of %ld, aborting.\n",
614 page_size, PAGE_SIZE);
615 return -ENODEV;
616 }
617
618 /* slave gets uar page size from QUERY_HCA fw command */
619 dev->caps.uar_page_size = 1 << (hca_param.uar_page_sz + 12);
620
621 /* TODO: relax this assumption */
622 if (dev->caps.uar_page_size != PAGE_SIZE) {
623 mlx4_err(dev, "UAR size:%d != kernel PAGE_SIZE of %ld\n",
624 dev->caps.uar_page_size, PAGE_SIZE);
625 return -ENODEV;
626 }
627
628 memset(&func_cap, 0, sizeof(func_cap));
Jack Morgenstein47605df2012-08-03 08:40:57 +0000629 err = mlx4_QUERY_FUNC_CAP(dev, 0, &func_cap);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000630 if (err) {
Jack Morgenstein47605df2012-08-03 08:40:57 +0000631 mlx4_err(dev, "QUERY_FUNC_CAP general command failed, aborting (%d).\n",
632 err);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000633 return err;
634 }
635
636 if ((func_cap.pf_context_behaviour | PF_CONTEXT_BEHAVIOUR_MASK) !=
637 PF_CONTEXT_BEHAVIOUR_MASK) {
638 mlx4_err(dev, "Unknown pf context behaviour\n");
639 return -ENOSYS;
640 }
641
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000642 dev->caps.num_ports = func_cap.num_ports;
Jack Morgenstein5a0d0a62013-11-03 10:03:23 +0200643 dev->quotas.qp = func_cap.qp_quota;
644 dev->quotas.srq = func_cap.srq_quota;
645 dev->quotas.cq = func_cap.cq_quota;
646 dev->quotas.mpt = func_cap.mpt_quota;
647 dev->quotas.mtt = func_cap.mtt_quota;
648 dev->caps.num_qps = 1 << hca_param.log_num_qps;
649 dev->caps.num_srqs = 1 << hca_param.log_num_srqs;
650 dev->caps.num_cqs = 1 << hca_param.log_num_cqs;
651 dev->caps.num_mpts = 1 << hca_param.log_mpt_sz;
652 dev->caps.num_eqs = func_cap.max_eq;
653 dev->caps.reserved_eqs = func_cap.reserved_eq;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000654 dev->caps.num_pds = MLX4_NUM_PDS;
655 dev->caps.num_mgms = 0;
656 dev->caps.num_amgms = 0;
657
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000658 if (dev->caps.num_ports > MLX4_MAX_PORTS) {
659 mlx4_err(dev, "HCA has %d ports, but we only support %d, "
660 "aborting.\n", dev->caps.num_ports, MLX4_MAX_PORTS);
661 return -ENODEV;
662 }
663
Jack Morgenstein47605df2012-08-03 08:40:57 +0000664 dev->caps.qp0_tunnel = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
665 dev->caps.qp0_proxy = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
666 dev->caps.qp1_tunnel = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
667 dev->caps.qp1_proxy = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
668
669 if (!dev->caps.qp0_tunnel || !dev->caps.qp0_proxy ||
670 !dev->caps.qp1_tunnel || !dev->caps.qp1_proxy) {
671 err = -ENOMEM;
672 goto err_mem;
673 }
674
Jack Morgenstein66349612012-06-19 11:21:44 +0300675 for (i = 1; i <= dev->caps.num_ports; ++i) {
Jack Morgenstein47605df2012-08-03 08:40:57 +0000676 err = mlx4_QUERY_FUNC_CAP(dev, (u32) i, &func_cap);
677 if (err) {
678 mlx4_err(dev, "QUERY_FUNC_CAP port command failed for"
679 " port %d, aborting (%d).\n", i, err);
680 goto err_mem;
681 }
682 dev->caps.qp0_tunnel[i - 1] = func_cap.qp0_tunnel_qpn;
683 dev->caps.qp0_proxy[i - 1] = func_cap.qp0_proxy_qpn;
684 dev->caps.qp1_tunnel[i - 1] = func_cap.qp1_tunnel_qpn;
685 dev->caps.qp1_proxy[i - 1] = func_cap.qp1_proxy_qpn;
Jack Morgenstein6230bb22012-05-30 09:14:54 +0000686 dev->caps.port_mask[i] = dev->caps.port_type[i];
Hadar Hen Zion8e1a28e2013-12-19 21:20:12 +0200687 dev->caps.phys_port_id[i] = func_cap.phys_port_id;
Jack Morgenstein66349612012-06-19 11:21:44 +0300688 if (mlx4_get_slave_pkey_gid_tbl_len(dev, i,
689 &dev->caps.gid_table_len[i],
690 &dev->caps.pkey_table_len[i]))
Jack Morgenstein47605df2012-08-03 08:40:57 +0000691 goto err_mem;
Jack Morgenstein66349612012-06-19 11:21:44 +0300692 }
Jack Morgenstein6230bb22012-05-30 09:14:54 +0000693
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000694 if (dev->caps.uar_page_size * (dev->caps.num_uars -
695 dev->caps.reserved_uars) >
696 pci_resource_len(dev->pdev, 2)) {
697 mlx4_err(dev, "HCA reported UAR region size of 0x%x bigger than "
698 "PCI resource 2 size of 0x%llx, aborting.\n",
699 dev->caps.uar_page_size * dev->caps.num_uars,
700 (unsigned long long) pci_resource_len(dev->pdev, 2));
Jack Morgenstein47605df2012-08-03 08:40:57 +0000701 goto err_mem;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000702 }
703
Or Gerlitz08ff3232012-10-21 14:59:24 +0000704 if (hca_param.dev_cap_enabled & MLX4_DEV_CAP_64B_EQE_ENABLED) {
705 dev->caps.eqe_size = 64;
706 dev->caps.eqe_factor = 1;
707 } else {
708 dev->caps.eqe_size = 32;
709 dev->caps.eqe_factor = 0;
710 }
711
712 if (hca_param.dev_cap_enabled & MLX4_DEV_CAP_64B_CQE_ENABLED) {
713 dev->caps.cqe_size = 64;
714 dev->caps.userspace_caps |= MLX4_USER_DEV_CAP_64B_CQE;
715 } else {
716 dev->caps.cqe_size = 32;
717 }
718
Amir Vadaif9bd2d72013-06-20 14:58:10 +0300719 dev->caps.flags2 &= ~MLX4_DEV_CAP_FLAG2_TS;
720 mlx4_warn(dev, "Timestamping is not supported in slave mode.\n");
721
Jack Morgenstein7b8157b2012-12-06 17:11:59 +0000722 slave_adjust_steering_mode(dev, &dev_cap, &hca_param);
723
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000724 return 0;
Jack Morgenstein47605df2012-08-03 08:40:57 +0000725
726err_mem:
727 kfree(dev->caps.qp0_tunnel);
728 kfree(dev->caps.qp0_proxy);
729 kfree(dev->caps.qp1_tunnel);
730 kfree(dev->caps.qp1_proxy);
731 dev->caps.qp0_tunnel = dev->caps.qp0_proxy =
732 dev->caps.qp1_tunnel = dev->caps.qp1_proxy = NULL;
733
734 return err;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000735}
Roland Dreier225c7b12007-05-08 18:00:38 -0700736
Eyal Perryb046ffe2013-10-15 16:55:24 +0200737static void mlx4_request_modules(struct mlx4_dev *dev)
738{
739 int port;
740 int has_ib_port = false;
741 int has_eth_port = false;
742#define EN_DRV_NAME "mlx4_en"
743#define IB_DRV_NAME "mlx4_ib"
744
745 for (port = 1; port <= dev->caps.num_ports; port++) {
746 if (dev->caps.port_type[port] == MLX4_PORT_TYPE_IB)
747 has_ib_port = true;
748 else if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
749 has_eth_port = true;
750 }
751
752 if (has_ib_port)
753 request_module_nowait(IB_DRV_NAME);
754 if (has_eth_port)
755 request_module_nowait(EN_DRV_NAME);
756}
757
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700758/*
759 * Change the port configuration of the device.
760 * Every user of this function must hold the port mutex.
761 */
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700762int mlx4_change_port_types(struct mlx4_dev *dev,
763 enum mlx4_port_type *port_types)
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700764{
765 int err = 0;
766 int change = 0;
767 int port;
768
769 for (port = 0; port < dev->caps.num_ports; port++) {
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700770 /* Change the port type only if the new type is different
771 * from the current, and not set to Auto */
Yevgeny Petrilin3d8f9302012-02-21 03:41:07 +0000772 if (port_types[port] != dev->caps.port_type[port + 1])
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700773 change = 1;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700774 }
775 if (change) {
776 mlx4_unregister_device(dev);
777 for (port = 1; port <= dev->caps.num_ports; port++) {
778 mlx4_CLOSE_PORT(dev, port);
Yevgeny Petrilin1e0f03d2012-02-23 07:04:35 +0000779 dev->caps.port_type[port] = port_types[port - 1];
Jack Morgenstein66349612012-06-19 11:21:44 +0300780 err = mlx4_SET_PORT(dev, port, -1);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700781 if (err) {
782 mlx4_err(dev, "Failed to set port %d, "
783 "aborting\n", port);
784 goto out;
785 }
786 }
787 mlx4_set_port_mask(dev);
788 err = mlx4_register_device(dev);
Eyal Perryb046ffe2013-10-15 16:55:24 +0200789 if (err) {
790 mlx4_err(dev, "Failed to register device\n");
791 goto out;
792 }
793 mlx4_request_modules(dev);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700794 }
795
796out:
797 return err;
798}
799
800static ssize_t show_port_type(struct device *dev,
801 struct device_attribute *attr,
802 char *buf)
803{
804 struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
805 port_attr);
806 struct mlx4_dev *mdev = info->dev;
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700807 char type[8];
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700808
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700809 sprintf(type, "%s",
810 (mdev->caps.port_type[info->port] == MLX4_PORT_TYPE_IB) ?
811 "ib" : "eth");
812 if (mdev->caps.possible_type[info->port] == MLX4_PORT_TYPE_AUTO)
813 sprintf(buf, "auto (%s)\n", type);
814 else
815 sprintf(buf, "%s\n", type);
816
817 return strlen(buf);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700818}
819
820static ssize_t set_port_type(struct device *dev,
821 struct device_attribute *attr,
822 const char *buf, size_t count)
823{
824 struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
825 port_attr);
826 struct mlx4_dev *mdev = info->dev;
827 struct mlx4_priv *priv = mlx4_priv(mdev);
828 enum mlx4_port_type types[MLX4_MAX_PORTS];
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700829 enum mlx4_port_type new_types[MLX4_MAX_PORTS];
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700830 int i;
831 int err = 0;
832
833 if (!strcmp(buf, "ib\n"))
834 info->tmp_type = MLX4_PORT_TYPE_IB;
835 else if (!strcmp(buf, "eth\n"))
836 info->tmp_type = MLX4_PORT_TYPE_ETH;
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700837 else if (!strcmp(buf, "auto\n"))
838 info->tmp_type = MLX4_PORT_TYPE_AUTO;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700839 else {
840 mlx4_err(mdev, "%s is not supported port type\n", buf);
841 return -EINVAL;
842 }
843
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700844 mlx4_stop_sense(mdev);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700845 mutex_lock(&priv->port_mutex);
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700846 /* Possible type is always the one that was delivered */
847 mdev->caps.possible_type[info->port] = info->tmp_type;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700848
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700849 for (i = 0; i < mdev->caps.num_ports; i++) {
850 types[i] = priv->port[i+1].tmp_type ? priv->port[i+1].tmp_type :
851 mdev->caps.possible_type[i+1];
852 if (types[i] == MLX4_PORT_TYPE_AUTO)
853 types[i] = mdev->caps.port_type[i+1];
854 }
855
Yevgeny Petrilin58a60162011-12-19 04:00:26 +0000856 if (!(mdev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP) &&
857 !(mdev->caps.flags & MLX4_DEV_CAP_FLAG_SENSE_SUPPORT)) {
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700858 for (i = 1; i <= mdev->caps.num_ports; i++) {
859 if (mdev->caps.possible_type[i] == MLX4_PORT_TYPE_AUTO) {
860 mdev->caps.possible_type[i] = mdev->caps.port_type[i];
861 err = -EINVAL;
862 }
863 }
864 }
865 if (err) {
866 mlx4_err(mdev, "Auto sensing is not supported on this HCA. "
867 "Set only 'eth' or 'ib' for both ports "
868 "(should be the same)\n");
869 goto out;
870 }
871
872 mlx4_do_sense_ports(mdev, new_types, types);
873
874 err = mlx4_check_port_params(mdev, new_types);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700875 if (err)
876 goto out;
877
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700878 /* We are about to apply the changes after the configuration
879 * was verified, no need to remember the temporary types
880 * any more */
881 for (i = 0; i < mdev->caps.num_ports; i++)
882 priv->port[i + 1].tmp_type = 0;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700883
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700884 err = mlx4_change_port_types(mdev, new_types);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700885
886out:
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700887 mlx4_start_sense(mdev);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700888 mutex_unlock(&priv->port_mutex);
889 return err ? err : count;
890}
891
Or Gerlitz096335b2012-01-11 19:02:17 +0200892enum ibta_mtu {
893 IB_MTU_256 = 1,
894 IB_MTU_512 = 2,
895 IB_MTU_1024 = 3,
896 IB_MTU_2048 = 4,
897 IB_MTU_4096 = 5
898};
899
900static inline int int_to_ibta_mtu(int mtu)
901{
902 switch (mtu) {
903 case 256: return IB_MTU_256;
904 case 512: return IB_MTU_512;
905 case 1024: return IB_MTU_1024;
906 case 2048: return IB_MTU_2048;
907 case 4096: return IB_MTU_4096;
908 default: return -1;
909 }
910}
911
912static inline int ibta_mtu_to_int(enum ibta_mtu mtu)
913{
914 switch (mtu) {
915 case IB_MTU_256: return 256;
916 case IB_MTU_512: return 512;
917 case IB_MTU_1024: return 1024;
918 case IB_MTU_2048: return 2048;
919 case IB_MTU_4096: return 4096;
920 default: return -1;
921 }
922}
923
924static ssize_t show_port_ib_mtu(struct device *dev,
925 struct device_attribute *attr,
926 char *buf)
927{
928 struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
929 port_mtu_attr);
930 struct mlx4_dev *mdev = info->dev;
931
932 if (mdev->caps.port_type[info->port] == MLX4_PORT_TYPE_ETH)
933 mlx4_warn(mdev, "port level mtu is only used for IB ports\n");
934
935 sprintf(buf, "%d\n",
936 ibta_mtu_to_int(mdev->caps.port_ib_mtu[info->port]));
937 return strlen(buf);
938}
939
940static ssize_t set_port_ib_mtu(struct device *dev,
941 struct device_attribute *attr,
942 const char *buf, size_t count)
943{
944 struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
945 port_mtu_attr);
946 struct mlx4_dev *mdev = info->dev;
947 struct mlx4_priv *priv = mlx4_priv(mdev);
948 int err, port, mtu, ibta_mtu = -1;
949
950 if (mdev->caps.port_type[info->port] == MLX4_PORT_TYPE_ETH) {
951 mlx4_warn(mdev, "port level mtu is only used for IB ports\n");
952 return -EINVAL;
953 }
954
Dotan Barak618fad92013-06-25 12:09:36 +0300955 err = kstrtoint(buf, 0, &mtu);
956 if (!err)
Or Gerlitz096335b2012-01-11 19:02:17 +0200957 ibta_mtu = int_to_ibta_mtu(mtu);
958
Dotan Barak618fad92013-06-25 12:09:36 +0300959 if (err || ibta_mtu < 0) {
Or Gerlitz096335b2012-01-11 19:02:17 +0200960 mlx4_err(mdev, "%s is invalid IBTA mtu\n", buf);
961 return -EINVAL;
962 }
963
964 mdev->caps.port_ib_mtu[info->port] = ibta_mtu;
965
966 mlx4_stop_sense(mdev);
967 mutex_lock(&priv->port_mutex);
968 mlx4_unregister_device(mdev);
969 for (port = 1; port <= mdev->caps.num_ports; port++) {
970 mlx4_CLOSE_PORT(mdev, port);
Jack Morgenstein66349612012-06-19 11:21:44 +0300971 err = mlx4_SET_PORT(mdev, port, -1);
Or Gerlitz096335b2012-01-11 19:02:17 +0200972 if (err) {
973 mlx4_err(mdev, "Failed to set port %d, "
974 "aborting\n", port);
975 goto err_set_port;
976 }
977 }
978 err = mlx4_register_device(mdev);
979err_set_port:
980 mutex_unlock(&priv->port_mutex);
981 mlx4_start_sense(mdev);
982 return err ? err : count;
983}
984
Roland Dreiere8f9b2e2008-02-04 20:20:41 -0800985static int mlx4_load_fw(struct mlx4_dev *dev)
Roland Dreier225c7b12007-05-08 18:00:38 -0700986{
987 struct mlx4_priv *priv = mlx4_priv(dev);
988 int err;
989
990 priv->fw.fw_icm = mlx4_alloc_icm(dev, priv->fw.fw_pages,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300991 GFP_HIGHUSER | __GFP_NOWARN, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700992 if (!priv->fw.fw_icm) {
993 mlx4_err(dev, "Couldn't allocate FW area, aborting.\n");
994 return -ENOMEM;
995 }
996
997 err = mlx4_MAP_FA(dev, priv->fw.fw_icm);
998 if (err) {
999 mlx4_err(dev, "MAP_FA command failed, aborting.\n");
1000 goto err_free;
1001 }
1002
1003 err = mlx4_RUN_FW(dev);
1004 if (err) {
1005 mlx4_err(dev, "RUN_FW command failed, aborting.\n");
1006 goto err_unmap_fa;
1007 }
1008
1009 return 0;
1010
1011err_unmap_fa:
1012 mlx4_UNMAP_FA(dev);
1013
1014err_free:
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +03001015 mlx4_free_icm(dev, priv->fw.fw_icm, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001016 return err;
1017}
1018
Roland Dreiere8f9b2e2008-02-04 20:20:41 -08001019static int mlx4_init_cmpt_table(struct mlx4_dev *dev, u64 cmpt_base,
1020 int cmpt_entry_sz)
Roland Dreier225c7b12007-05-08 18:00:38 -07001021{
1022 struct mlx4_priv *priv = mlx4_priv(dev);
1023 int err;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001024 int num_eqs;
Roland Dreier225c7b12007-05-08 18:00:38 -07001025
1026 err = mlx4_init_icm_table(dev, &priv->qp_table.cmpt_table,
1027 cmpt_base +
1028 ((u64) (MLX4_CMPT_TYPE_QP *
1029 cmpt_entry_sz) << MLX4_CMPT_SHIFT),
1030 cmpt_entry_sz, dev->caps.num_qps,
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -07001031 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
1032 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001033 if (err)
1034 goto err;
1035
1036 err = mlx4_init_icm_table(dev, &priv->srq_table.cmpt_table,
1037 cmpt_base +
1038 ((u64) (MLX4_CMPT_TYPE_SRQ *
1039 cmpt_entry_sz) << MLX4_CMPT_SHIFT),
1040 cmpt_entry_sz, dev->caps.num_srqs,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +03001041 dev->caps.reserved_srqs, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001042 if (err)
1043 goto err_qp;
1044
1045 err = mlx4_init_icm_table(dev, &priv->cq_table.cmpt_table,
1046 cmpt_base +
1047 ((u64) (MLX4_CMPT_TYPE_CQ *
1048 cmpt_entry_sz) << MLX4_CMPT_SHIFT),
1049 cmpt_entry_sz, dev->caps.num_cqs,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +03001050 dev->caps.reserved_cqs, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001051 if (err)
1052 goto err_srq;
1053
Marcel Apfelbaum3fc929e2012-05-30 09:14:51 +00001054 num_eqs = (mlx4_is_master(dev)) ? dev->phys_caps.num_phys_eqs :
1055 dev->caps.num_eqs;
Roland Dreier225c7b12007-05-08 18:00:38 -07001056 err = mlx4_init_icm_table(dev, &priv->eq_table.cmpt_table,
1057 cmpt_base +
1058 ((u64) (MLX4_CMPT_TYPE_EQ *
1059 cmpt_entry_sz) << MLX4_CMPT_SHIFT),
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001060 cmpt_entry_sz, num_eqs, num_eqs, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001061 if (err)
1062 goto err_cq;
1063
1064 return 0;
1065
1066err_cq:
1067 mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
1068
1069err_srq:
1070 mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
1071
1072err_qp:
1073 mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
1074
1075err:
1076 return err;
1077}
1078
Roland Dreier3d73c282007-10-10 15:43:54 -07001079static int mlx4_init_icm(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap,
1080 struct mlx4_init_hca_param *init_hca, u64 icm_size)
Roland Dreier225c7b12007-05-08 18:00:38 -07001081{
1082 struct mlx4_priv *priv = mlx4_priv(dev);
1083 u64 aux_pages;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001084 int num_eqs;
Roland Dreier225c7b12007-05-08 18:00:38 -07001085 int err;
1086
1087 err = mlx4_SET_ICM_SIZE(dev, icm_size, &aux_pages);
1088 if (err) {
1089 mlx4_err(dev, "SET_ICM_SIZE command failed, aborting.\n");
1090 return err;
1091 }
1092
1093 mlx4_dbg(dev, "%lld KB of HCA context requires %lld KB aux memory.\n",
1094 (unsigned long long) icm_size >> 10,
1095 (unsigned long long) aux_pages << 2);
1096
1097 priv->fw.aux_icm = mlx4_alloc_icm(dev, aux_pages,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +03001098 GFP_HIGHUSER | __GFP_NOWARN, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001099 if (!priv->fw.aux_icm) {
1100 mlx4_err(dev, "Couldn't allocate aux memory, aborting.\n");
1101 return -ENOMEM;
1102 }
1103
1104 err = mlx4_MAP_ICM_AUX(dev, priv->fw.aux_icm);
1105 if (err) {
1106 mlx4_err(dev, "MAP_ICM_AUX command failed, aborting.\n");
1107 goto err_free_aux;
1108 }
1109
1110 err = mlx4_init_cmpt_table(dev, init_hca->cmpt_base, dev_cap->cmpt_entry_sz);
1111 if (err) {
1112 mlx4_err(dev, "Failed to map cMPT context memory, aborting.\n");
1113 goto err_unmap_aux;
1114 }
1115
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001116
Marcel Apfelbaum3fc929e2012-05-30 09:14:51 +00001117 num_eqs = (mlx4_is_master(dev)) ? dev->phys_caps.num_phys_eqs :
1118 dev->caps.num_eqs;
Roland Dreierfa0681d2009-09-05 20:24:49 -07001119 err = mlx4_init_icm_table(dev, &priv->eq_table.table,
1120 init_hca->eqc_base, dev_cap->eqc_entry_sz,
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001121 num_eqs, num_eqs, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001122 if (err) {
1123 mlx4_err(dev, "Failed to map EQ context memory, aborting.\n");
1124 goto err_unmap_cmpt;
1125 }
1126
Jack Morgensteind7bb58f2007-08-01 12:28:53 +03001127 /*
1128 * Reserved MTT entries must be aligned up to a cacheline
1129 * boundary, since the FW will write to them, while the driver
1130 * writes to all other MTT entries. (The variable
1131 * dev->caps.mtt_entry_sz below is really the MTT segment
1132 * size, not the raw entry size)
1133 */
1134 dev->caps.reserved_mtts =
1135 ALIGN(dev->caps.reserved_mtts * dev->caps.mtt_entry_sz,
1136 dma_get_cache_alignment()) / dev->caps.mtt_entry_sz;
1137
Roland Dreier225c7b12007-05-08 18:00:38 -07001138 err = mlx4_init_icm_table(dev, &priv->mr_table.mtt_table,
1139 init_hca->mtt_base,
1140 dev->caps.mtt_entry_sz,
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +00001141 dev->caps.num_mtts,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +03001142 dev->caps.reserved_mtts, 1, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001143 if (err) {
1144 mlx4_err(dev, "Failed to map MTT context memory, aborting.\n");
1145 goto err_unmap_eq;
1146 }
1147
1148 err = mlx4_init_icm_table(dev, &priv->mr_table.dmpt_table,
1149 init_hca->dmpt_base,
1150 dev_cap->dmpt_entry_sz,
1151 dev->caps.num_mpts,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +03001152 dev->caps.reserved_mrws, 1, 1);
Roland Dreier225c7b12007-05-08 18:00:38 -07001153 if (err) {
1154 mlx4_err(dev, "Failed to map dMPT context memory, aborting.\n");
1155 goto err_unmap_mtt;
1156 }
1157
1158 err = mlx4_init_icm_table(dev, &priv->qp_table.qp_table,
1159 init_hca->qpc_base,
1160 dev_cap->qpc_entry_sz,
1161 dev->caps.num_qps,
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -07001162 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
1163 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001164 if (err) {
1165 mlx4_err(dev, "Failed to map QP context memory, aborting.\n");
1166 goto err_unmap_dmpt;
1167 }
1168
1169 err = mlx4_init_icm_table(dev, &priv->qp_table.auxc_table,
1170 init_hca->auxc_base,
1171 dev_cap->aux_entry_sz,
1172 dev->caps.num_qps,
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -07001173 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
1174 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001175 if (err) {
1176 mlx4_err(dev, "Failed to map AUXC context memory, aborting.\n");
1177 goto err_unmap_qp;
1178 }
1179
1180 err = mlx4_init_icm_table(dev, &priv->qp_table.altc_table,
1181 init_hca->altc_base,
1182 dev_cap->altc_entry_sz,
1183 dev->caps.num_qps,
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -07001184 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
1185 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001186 if (err) {
1187 mlx4_err(dev, "Failed to map ALTC context memory, aborting.\n");
1188 goto err_unmap_auxc;
1189 }
1190
1191 err = mlx4_init_icm_table(dev, &priv->qp_table.rdmarc_table,
1192 init_hca->rdmarc_base,
1193 dev_cap->rdmarc_entry_sz << priv->qp_table.rdmarc_shift,
1194 dev->caps.num_qps,
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -07001195 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
1196 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001197 if (err) {
1198 mlx4_err(dev, "Failed to map RDMARC context memory, aborting\n");
1199 goto err_unmap_altc;
1200 }
1201
1202 err = mlx4_init_icm_table(dev, &priv->cq_table.table,
1203 init_hca->cqc_base,
1204 dev_cap->cqc_entry_sz,
1205 dev->caps.num_cqs,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +03001206 dev->caps.reserved_cqs, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001207 if (err) {
1208 mlx4_err(dev, "Failed to map CQ context memory, aborting.\n");
1209 goto err_unmap_rdmarc;
1210 }
1211
1212 err = mlx4_init_icm_table(dev, &priv->srq_table.table,
1213 init_hca->srqc_base,
1214 dev_cap->srq_entry_sz,
1215 dev->caps.num_srqs,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +03001216 dev->caps.reserved_srqs, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001217 if (err) {
1218 mlx4_err(dev, "Failed to map SRQ context memory, aborting.\n");
1219 goto err_unmap_cq;
1220 }
1221
1222 /*
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001223 * For flow steering device managed mode it is required to use
1224 * mlx4_init_icm_table. For B0 steering mode it's not strictly
1225 * required, but for simplicity just map the whole multicast
1226 * group table now. The table isn't very big and it's a lot
1227 * easier than trying to track ref counts.
Roland Dreier225c7b12007-05-08 18:00:38 -07001228 */
1229 err = mlx4_init_icm_table(dev, &priv->mcg_table.table,
Eugenia Emantayev0ec2c0f2011-12-13 04:16:02 +00001230 init_hca->mc_base,
1231 mlx4_get_mgm_entry_size(dev),
Roland Dreier225c7b12007-05-08 18:00:38 -07001232 dev->caps.num_mgms + dev->caps.num_amgms,
1233 dev->caps.num_mgms + dev->caps.num_amgms,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +03001234 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001235 if (err) {
1236 mlx4_err(dev, "Failed to map MCG context memory, aborting.\n");
1237 goto err_unmap_srq;
1238 }
1239
1240 return 0;
1241
1242err_unmap_srq:
1243 mlx4_cleanup_icm_table(dev, &priv->srq_table.table);
1244
1245err_unmap_cq:
1246 mlx4_cleanup_icm_table(dev, &priv->cq_table.table);
1247
1248err_unmap_rdmarc:
1249 mlx4_cleanup_icm_table(dev, &priv->qp_table.rdmarc_table);
1250
1251err_unmap_altc:
1252 mlx4_cleanup_icm_table(dev, &priv->qp_table.altc_table);
1253
1254err_unmap_auxc:
1255 mlx4_cleanup_icm_table(dev, &priv->qp_table.auxc_table);
1256
1257err_unmap_qp:
1258 mlx4_cleanup_icm_table(dev, &priv->qp_table.qp_table);
1259
1260err_unmap_dmpt:
1261 mlx4_cleanup_icm_table(dev, &priv->mr_table.dmpt_table);
1262
1263err_unmap_mtt:
1264 mlx4_cleanup_icm_table(dev, &priv->mr_table.mtt_table);
1265
1266err_unmap_eq:
Roland Dreierfa0681d2009-09-05 20:24:49 -07001267 mlx4_cleanup_icm_table(dev, &priv->eq_table.table);
Roland Dreier225c7b12007-05-08 18:00:38 -07001268
1269err_unmap_cmpt:
1270 mlx4_cleanup_icm_table(dev, &priv->eq_table.cmpt_table);
1271 mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
1272 mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
1273 mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
1274
1275err_unmap_aux:
1276 mlx4_UNMAP_ICM_AUX(dev);
1277
1278err_free_aux:
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +03001279 mlx4_free_icm(dev, priv->fw.aux_icm, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001280
1281 return err;
1282}
1283
1284static void mlx4_free_icms(struct mlx4_dev *dev)
1285{
1286 struct mlx4_priv *priv = mlx4_priv(dev);
1287
1288 mlx4_cleanup_icm_table(dev, &priv->mcg_table.table);
1289 mlx4_cleanup_icm_table(dev, &priv->srq_table.table);
1290 mlx4_cleanup_icm_table(dev, &priv->cq_table.table);
1291 mlx4_cleanup_icm_table(dev, &priv->qp_table.rdmarc_table);
1292 mlx4_cleanup_icm_table(dev, &priv->qp_table.altc_table);
1293 mlx4_cleanup_icm_table(dev, &priv->qp_table.auxc_table);
1294 mlx4_cleanup_icm_table(dev, &priv->qp_table.qp_table);
1295 mlx4_cleanup_icm_table(dev, &priv->mr_table.dmpt_table);
1296 mlx4_cleanup_icm_table(dev, &priv->mr_table.mtt_table);
Roland Dreierfa0681d2009-09-05 20:24:49 -07001297 mlx4_cleanup_icm_table(dev, &priv->eq_table.table);
Roland Dreier225c7b12007-05-08 18:00:38 -07001298 mlx4_cleanup_icm_table(dev, &priv->eq_table.cmpt_table);
1299 mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
1300 mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
1301 mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
Roland Dreier225c7b12007-05-08 18:00:38 -07001302
1303 mlx4_UNMAP_ICM_AUX(dev);
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +03001304 mlx4_free_icm(dev, priv->fw.aux_icm, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001305}
1306
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001307static void mlx4_slave_exit(struct mlx4_dev *dev)
1308{
1309 struct mlx4_priv *priv = mlx4_priv(dev);
1310
Roland Dreierf3d4c892012-09-25 21:24:07 -07001311 mutex_lock(&priv->cmd.slave_cmd_mutex);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001312 if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_RESET, 0, MLX4_COMM_TIME))
1313 mlx4_warn(dev, "Failed to close slave function.\n");
Roland Dreierf3d4c892012-09-25 21:24:07 -07001314 mutex_unlock(&priv->cmd.slave_cmd_mutex);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001315}
1316
Eli Cohenc1b43dc2011-03-22 22:38:41 +00001317static int map_bf_area(struct mlx4_dev *dev)
1318{
1319 struct mlx4_priv *priv = mlx4_priv(dev);
1320 resource_size_t bf_start;
1321 resource_size_t bf_len;
1322 int err = 0;
1323
Jack Morgenstein3d747472012-02-19 21:38:52 +00001324 if (!dev->caps.bf_reg_size)
1325 return -ENXIO;
1326
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001327 bf_start = pci_resource_start(dev->pdev, 2) +
1328 (dev->caps.num_uars << PAGE_SHIFT);
1329 bf_len = pci_resource_len(dev->pdev, 2) -
1330 (dev->caps.num_uars << PAGE_SHIFT);
Eli Cohenc1b43dc2011-03-22 22:38:41 +00001331 priv->bf_mapping = io_mapping_create_wc(bf_start, bf_len);
1332 if (!priv->bf_mapping)
1333 err = -ENOMEM;
1334
1335 return err;
1336}
1337
1338static void unmap_bf_area(struct mlx4_dev *dev)
1339{
1340 if (mlx4_priv(dev)->bf_mapping)
1341 io_mapping_free(mlx4_priv(dev)->bf_mapping);
1342}
1343
Amir Vadaiec693d42013-04-23 06:06:49 +00001344cycle_t mlx4_read_clock(struct mlx4_dev *dev)
1345{
1346 u32 clockhi, clocklo, clockhi1;
1347 cycle_t cycles;
1348 int i;
1349 struct mlx4_priv *priv = mlx4_priv(dev);
1350
1351 for (i = 0; i < 10; i++) {
1352 clockhi = swab32(readl(priv->clock_mapping));
1353 clocklo = swab32(readl(priv->clock_mapping + 4));
1354 clockhi1 = swab32(readl(priv->clock_mapping));
1355 if (clockhi == clockhi1)
1356 break;
1357 }
1358
1359 cycles = (u64) clockhi << 32 | (u64) clocklo;
1360
1361 return cycles;
1362}
1363EXPORT_SYMBOL_GPL(mlx4_read_clock);
1364
1365
Eugenia Emantayevddd8a6c2013-04-23 06:06:48 +00001366static int map_internal_clock(struct mlx4_dev *dev)
1367{
1368 struct mlx4_priv *priv = mlx4_priv(dev);
1369
1370 priv->clock_mapping =
1371 ioremap(pci_resource_start(dev->pdev, priv->fw.clock_bar) +
1372 priv->fw.clock_offset, MLX4_CLOCK_SIZE);
1373
1374 if (!priv->clock_mapping)
1375 return -ENOMEM;
1376
1377 return 0;
1378}
1379
1380static void unmap_internal_clock(struct mlx4_dev *dev)
1381{
1382 struct mlx4_priv *priv = mlx4_priv(dev);
1383
1384 if (priv->clock_mapping)
1385 iounmap(priv->clock_mapping);
1386}
1387
Roland Dreier225c7b12007-05-08 18:00:38 -07001388static void mlx4_close_hca(struct mlx4_dev *dev)
1389{
Eugenia Emantayevddd8a6c2013-04-23 06:06:48 +00001390 unmap_internal_clock(dev);
Eli Cohenc1b43dc2011-03-22 22:38:41 +00001391 unmap_bf_area(dev);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001392 if (mlx4_is_slave(dev))
1393 mlx4_slave_exit(dev);
1394 else {
1395 mlx4_CLOSE_HCA(dev, 0);
1396 mlx4_free_icms(dev);
1397 mlx4_UNMAP_FA(dev);
1398 mlx4_free_icm(dev, mlx4_priv(dev)->fw.fw_icm, 0);
1399 }
1400}
1401
1402static int mlx4_init_slave(struct mlx4_dev *dev)
1403{
1404 struct mlx4_priv *priv = mlx4_priv(dev);
1405 u64 dma = (u64) priv->mfunc.vhcr_dma;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001406 int ret_from_reset = 0;
1407 u32 slave_read;
1408 u32 cmd_channel_ver;
1409
Roland Dreierf3d4c892012-09-25 21:24:07 -07001410 mutex_lock(&priv->cmd.slave_cmd_mutex);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001411 priv->cmd.max_cmds = 1;
1412 mlx4_warn(dev, "Sending reset\n");
1413 ret_from_reset = mlx4_comm_cmd(dev, MLX4_COMM_CMD_RESET, 0,
1414 MLX4_COMM_TIME);
1415 /* if we are in the middle of flr the slave will try
1416 * NUM_OF_RESET_RETRIES times before leaving.*/
1417 if (ret_from_reset) {
1418 if (MLX4_DELAY_RESET_SLAVE == ret_from_reset) {
Jack Morgenstein5efe5352013-06-04 05:13:27 +00001419 mlx4_warn(dev, "slave is currently in the "
1420 "middle of FLR. Deferring probe.\n");
1421 mutex_unlock(&priv->cmd.slave_cmd_mutex);
1422 return -EPROBE_DEFER;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001423 } else
1424 goto err;
1425 }
1426
1427 /* check the driver version - the slave I/F revision
1428 * must match the master's */
1429 slave_read = swab32(readl(&priv->mfunc.comm->slave_read));
1430 cmd_channel_ver = mlx4_comm_get_version();
1431
1432 if (MLX4_COMM_GET_IF_REV(cmd_channel_ver) !=
1433 MLX4_COMM_GET_IF_REV(slave_read)) {
1434 mlx4_err(dev, "slave driver version is not supported"
1435 " by the master\n");
1436 goto err;
1437 }
1438
1439 mlx4_warn(dev, "Sending vhcr0\n");
1440 if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR0, dma >> 48,
1441 MLX4_COMM_TIME))
1442 goto err;
1443 if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR1, dma >> 32,
1444 MLX4_COMM_TIME))
1445 goto err;
1446 if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR2, dma >> 16,
1447 MLX4_COMM_TIME))
1448 goto err;
1449 if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR_EN, dma, MLX4_COMM_TIME))
1450 goto err;
Roland Dreierf3d4c892012-09-25 21:24:07 -07001451
1452 mutex_unlock(&priv->cmd.slave_cmd_mutex);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001453 return 0;
1454
1455err:
1456 mlx4_comm_cmd(dev, MLX4_COMM_CMD_RESET, 0, 0);
Roland Dreierf3d4c892012-09-25 21:24:07 -07001457 mutex_unlock(&priv->cmd.slave_cmd_mutex);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001458 return -EIO;
Roland Dreier225c7b12007-05-08 18:00:38 -07001459}
1460
Jack Morgenstein66349612012-06-19 11:21:44 +03001461static void mlx4_parav_master_pf_caps(struct mlx4_dev *dev)
1462{
1463 int i;
1464
1465 for (i = 1; i <= dev->caps.num_ports; i++) {
1466 dev->caps.gid_table_len[i] = 1;
1467 dev->caps.pkey_table_len[i] =
1468 dev->phys_caps.pkey_phys_table_len[i] - 1;
1469 }
1470}
1471
Jack Morgenstein3c439b52012-12-06 17:12:00 +00001472static int choose_log_fs_mgm_entry_size(int qp_per_entry)
1473{
1474 int i = MLX4_MIN_MGM_LOG_ENTRY_SIZE;
1475
1476 for (i = MLX4_MIN_MGM_LOG_ENTRY_SIZE; i <= MLX4_MAX_MGM_LOG_ENTRY_SIZE;
1477 i++) {
1478 if (qp_per_entry <= 4 * ((1 << i) / 16 - 2))
1479 break;
1480 }
1481
1482 return (i <= MLX4_MAX_MGM_LOG_ENTRY_SIZE) ? i : -1;
1483}
1484
Jack Morgenstein7b8157b2012-12-06 17:11:59 +00001485static void choose_steering_mode(struct mlx4_dev *dev,
1486 struct mlx4_dev_cap *dev_cap)
1487{
Jack Morgenstein3c439b52012-12-06 17:12:00 +00001488 if (mlx4_log_num_mgm_entry_size == -1 &&
1489 dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_FS_EN &&
Jack Morgenstein7b8157b2012-12-06 17:11:59 +00001490 (!mlx4_is_mfunc(dev) ||
Jack Morgenstein3c439b52012-12-06 17:12:00 +00001491 (dev_cap->fs_max_num_qp_per_entry >= (num_vfs + 1))) &&
1492 choose_log_fs_mgm_entry_size(dev_cap->fs_max_num_qp_per_entry) >=
1493 MLX4_MIN_MGM_LOG_ENTRY_SIZE) {
1494 dev->oper_log_mgm_entry_size =
1495 choose_log_fs_mgm_entry_size(dev_cap->fs_max_num_qp_per_entry);
Jack Morgenstein7b8157b2012-12-06 17:11:59 +00001496 dev->caps.steering_mode = MLX4_STEERING_MODE_DEVICE_MANAGED;
1497 dev->caps.num_qp_per_mgm = dev_cap->fs_max_num_qp_per_entry;
1498 dev->caps.fs_log_max_ucast_qp_range_size =
1499 dev_cap->fs_log_max_ucast_qp_range_size;
1500 } else {
1501 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER &&
1502 dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER)
1503 dev->caps.steering_mode = MLX4_STEERING_MODE_B0;
1504 else {
1505 dev->caps.steering_mode = MLX4_STEERING_MODE_A0;
1506
1507 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER ||
1508 dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER)
1509 mlx4_warn(dev, "Must have both UC_STEER and MC_STEER flags "
1510 "set to use B0 steering. Falling back to A0 steering mode.\n");
1511 }
Jack Morgenstein3c439b52012-12-06 17:12:00 +00001512 dev->oper_log_mgm_entry_size =
1513 mlx4_log_num_mgm_entry_size > 0 ?
1514 mlx4_log_num_mgm_entry_size :
1515 MLX4_DEFAULT_MGM_LOG_ENTRY_SIZE;
Jack Morgenstein7b8157b2012-12-06 17:11:59 +00001516 dev->caps.num_qp_per_mgm = mlx4_get_qp_per_mgm(dev);
1517 }
Jack Morgenstein3c439b52012-12-06 17:12:00 +00001518 mlx4_dbg(dev, "Steering mode is: %s, oper_log_mgm_entry_size = %d, "
1519 "modparam log_num_mgm_entry_size = %d\n",
1520 mlx4_steering_mode_str(dev->caps.steering_mode),
1521 dev->oper_log_mgm_entry_size,
1522 mlx4_log_num_mgm_entry_size);
Jack Morgenstein7b8157b2012-12-06 17:11:59 +00001523}
1524
Or Gerlitz7ffdf722013-12-23 16:09:43 +02001525static void choose_tunnel_offload_mode(struct mlx4_dev *dev,
1526 struct mlx4_dev_cap *dev_cap)
1527{
1528 if (dev->caps.steering_mode == MLX4_STEERING_MODE_DEVICE_MANAGED &&
1529 dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_VXLAN_OFFLOADS)
1530 dev->caps.tunnel_offload_mode = MLX4_TUNNEL_OFFLOAD_MODE_VXLAN;
1531 else
1532 dev->caps.tunnel_offload_mode = MLX4_TUNNEL_OFFLOAD_MODE_NONE;
1533
1534 mlx4_dbg(dev, "Tunneling offload mode is: %s\n", (dev->caps.tunnel_offload_mode
1535 == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) ? "vxlan" : "none");
1536}
1537
Roland Dreier3d73c282007-10-10 15:43:54 -07001538static int mlx4_init_hca(struct mlx4_dev *dev)
Roland Dreier225c7b12007-05-08 18:00:38 -07001539{
1540 struct mlx4_priv *priv = mlx4_priv(dev);
1541 struct mlx4_adapter adapter;
1542 struct mlx4_dev_cap dev_cap;
Vladimir Sokolovsky2d928652008-07-14 23:48:53 -07001543 struct mlx4_mod_stat_cfg mlx4_cfg;
Roland Dreier225c7b12007-05-08 18:00:38 -07001544 struct mlx4_profile profile;
1545 struct mlx4_init_hca_param init_hca;
1546 u64 icm_size;
1547 int err;
1548
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001549 if (!mlx4_is_slave(dev)) {
1550 err = mlx4_QUERY_FW(dev);
1551 if (err) {
1552 if (err == -EACCES)
1553 mlx4_info(dev, "non-primary physical function, skipping.\n");
1554 else
1555 mlx4_err(dev, "QUERY_FW command failed, aborting.\n");
Aviad Yehezkelbef772e2012-09-05 22:50:51 +00001556 return err;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001557 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001558
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001559 err = mlx4_load_fw(dev);
1560 if (err) {
1561 mlx4_err(dev, "Failed to start FW, aborting.\n");
Aviad Yehezkelbef772e2012-09-05 22:50:51 +00001562 return err;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001563 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001564
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001565 mlx4_cfg.log_pg_sz_m = 1;
1566 mlx4_cfg.log_pg_sz = 0;
1567 err = mlx4_MOD_STAT_CFG(dev, &mlx4_cfg);
1568 if (err)
1569 mlx4_warn(dev, "Failed to override log_pg_sz parameter\n");
Vladimir Sokolovsky2d928652008-07-14 23:48:53 -07001570
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001571 err = mlx4_dev_cap(dev, &dev_cap);
1572 if (err) {
1573 mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
1574 goto err_stop_fw;
1575 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001576
Jack Morgenstein7b8157b2012-12-06 17:11:59 +00001577 choose_steering_mode(dev, &dev_cap);
Or Gerlitz7ffdf722013-12-23 16:09:43 +02001578 choose_tunnel_offload_mode(dev, &dev_cap);
Jack Morgenstein7b8157b2012-12-06 17:11:59 +00001579
Hadar Hen Zion8e1a28e2013-12-19 21:20:12 +02001580 err = mlx4_get_phys_port_id(dev);
1581 if (err)
1582 mlx4_err(dev, "Fail to get physical port id\n");
1583
Jack Morgenstein66349612012-06-19 11:21:44 +03001584 if (mlx4_is_master(dev))
1585 mlx4_parav_master_pf_caps(dev);
1586
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001587 profile = default_profile;
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001588 if (dev->caps.steering_mode ==
1589 MLX4_STEERING_MODE_DEVICE_MANAGED)
1590 profile.num_mcg = MLX4_FS_NUM_MCG;
Roland Dreier225c7b12007-05-08 18:00:38 -07001591
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001592 icm_size = mlx4_make_profile(dev, &profile, &dev_cap,
1593 &init_hca);
1594 if ((long long) icm_size < 0) {
1595 err = icm_size;
1596 goto err_stop_fw;
1597 }
1598
Eli Cohena5bbe892012-02-09 18:10:06 +02001599 dev->caps.max_fmr_maps = (1 << (32 - ilog2(dev->caps.num_mpts))) - 1;
1600
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001601 init_hca.log_uar_sz = ilog2(dev->caps.num_uars);
1602 init_hca.uar_page_sz = PAGE_SHIFT - 12;
Shani Michaelie4488342013-02-06 16:19:11 +00001603 init_hca.mw_enabled = 0;
1604 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_MEM_WINDOW ||
1605 dev->caps.bmme_flags & MLX4_BMME_FLAG_TYPE_2_WIN)
1606 init_hca.mw_enabled = INIT_HCA_TPT_MW_ENABLE;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001607
1608 err = mlx4_init_icm(dev, &dev_cap, &init_hca, icm_size);
1609 if (err)
1610 goto err_stop_fw;
1611
1612 err = mlx4_INIT_HCA(dev, &init_hca);
1613 if (err) {
1614 mlx4_err(dev, "INIT_HCA command failed, aborting.\n");
1615 goto err_free_icm;
1616 }
Eugenia Emantayevddd8a6c2013-04-23 06:06:48 +00001617 /*
1618 * If TS is supported by FW
1619 * read HCA frequency by QUERY_HCA command
1620 */
1621 if (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS) {
1622 memset(&init_hca, 0, sizeof(init_hca));
1623 err = mlx4_QUERY_HCA(dev, &init_hca);
1624 if (err) {
1625 mlx4_err(dev, "QUERY_HCA command failed, disable timestamp.\n");
1626 dev->caps.flags2 &= ~MLX4_DEV_CAP_FLAG2_TS;
1627 } else {
1628 dev->caps.hca_core_clock =
1629 init_hca.hca_core_clock;
1630 }
1631
1632 /* In case we got HCA frequency 0 - disable timestamping
1633 * to avoid dividing by zero
1634 */
1635 if (!dev->caps.hca_core_clock) {
1636 dev->caps.flags2 &= ~MLX4_DEV_CAP_FLAG2_TS;
1637 mlx4_err(dev,
1638 "HCA frequency is 0. Timestamping is not supported.");
1639 } else if (map_internal_clock(dev)) {
1640 /*
1641 * Map internal clock,
1642 * in case of failure disable timestamping
1643 */
1644 dev->caps.flags2 &= ~MLX4_DEV_CAP_FLAG2_TS;
1645 mlx4_err(dev, "Failed to map internal clock. Timestamping is not supported.\n");
1646 }
1647 }
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001648 } else {
1649 err = mlx4_init_slave(dev);
1650 if (err) {
Jack Morgenstein5efe5352013-06-04 05:13:27 +00001651 if (err != -EPROBE_DEFER)
1652 mlx4_err(dev, "Failed to initialize slave\n");
Aviad Yehezkelbef772e2012-09-05 22:50:51 +00001653 return err;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001654 }
1655
1656 err = mlx4_slave_cap(dev);
1657 if (err) {
1658 mlx4_err(dev, "Failed to obtain slave caps\n");
1659 goto err_close;
1660 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001661 }
1662
Eli Cohenc1b43dc2011-03-22 22:38:41 +00001663 if (map_bf_area(dev))
1664 mlx4_dbg(dev, "Failed to map blue flame area\n");
1665
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001666 /*Only the master set the ports, all the rest got it from it.*/
1667 if (!mlx4_is_slave(dev))
1668 mlx4_set_port_mask(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07001669
1670 err = mlx4_QUERY_ADAPTER(dev, &adapter);
1671 if (err) {
1672 mlx4_err(dev, "QUERY_ADAPTER command failed, aborting.\n");
Aviad Yehezkelbef772e2012-09-05 22:50:51 +00001673 goto unmap_bf;
Roland Dreier225c7b12007-05-08 18:00:38 -07001674 }
1675
1676 priv->eq_table.inta_pin = adapter.inta_pin;
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001677 memcpy(dev->board_id, adapter.board_id, sizeof dev->board_id);
Roland Dreier225c7b12007-05-08 18:00:38 -07001678
1679 return 0;
1680
Aviad Yehezkelbef772e2012-09-05 22:50:51 +00001681unmap_bf:
Eugenia Emantayevddd8a6c2013-04-23 06:06:48 +00001682 unmap_internal_clock(dev);
Aviad Yehezkelbef772e2012-09-05 22:50:51 +00001683 unmap_bf_area(dev);
1684
Roland Dreier225c7b12007-05-08 18:00:38 -07001685err_close:
Dotan Barak41929ed2012-10-21 14:59:23 +00001686 if (mlx4_is_slave(dev))
1687 mlx4_slave_exit(dev);
1688 else
1689 mlx4_CLOSE_HCA(dev, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001690
1691err_free_icm:
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001692 if (!mlx4_is_slave(dev))
1693 mlx4_free_icms(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07001694
1695err_stop_fw:
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001696 if (!mlx4_is_slave(dev)) {
1697 mlx4_UNMAP_FA(dev);
1698 mlx4_free_icm(dev, priv->fw.fw_icm, 0);
1699 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001700 return err;
1701}
1702
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +00001703static int mlx4_init_counters_table(struct mlx4_dev *dev)
1704{
1705 struct mlx4_priv *priv = mlx4_priv(dev);
1706 int nent;
1707
1708 if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_COUNTERS))
1709 return -ENOENT;
1710
1711 nent = dev->caps.max_counters;
1712 return mlx4_bitmap_init(&priv->counters_bitmap, nent, nent - 1, 0, 0);
1713}
1714
1715static void mlx4_cleanup_counters_table(struct mlx4_dev *dev)
1716{
1717 mlx4_bitmap_cleanup(&mlx4_priv(dev)->counters_bitmap);
1718}
1719
Jack Morgensteinba062d52012-05-15 10:35:03 +00001720int __mlx4_counter_alloc(struct mlx4_dev *dev, u32 *idx)
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +00001721{
1722 struct mlx4_priv *priv = mlx4_priv(dev);
1723
1724 if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_COUNTERS))
1725 return -ENOENT;
1726
1727 *idx = mlx4_bitmap_alloc(&priv->counters_bitmap);
1728 if (*idx == -1)
1729 return -ENOMEM;
1730
1731 return 0;
1732}
Jack Morgensteinba062d52012-05-15 10:35:03 +00001733
1734int mlx4_counter_alloc(struct mlx4_dev *dev, u32 *idx)
1735{
1736 u64 out_param;
1737 int err;
1738
1739 if (mlx4_is_mfunc(dev)) {
1740 err = mlx4_cmd_imm(dev, 0, &out_param, RES_COUNTER,
1741 RES_OP_RESERVE, MLX4_CMD_ALLOC_RES,
1742 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
1743 if (!err)
1744 *idx = get_param_l(&out_param);
1745
1746 return err;
1747 }
1748 return __mlx4_counter_alloc(dev, idx);
1749}
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +00001750EXPORT_SYMBOL_GPL(mlx4_counter_alloc);
1751
Jack Morgensteinba062d52012-05-15 10:35:03 +00001752void __mlx4_counter_free(struct mlx4_dev *dev, u32 idx)
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +00001753{
Jack Morgenstein7c6d74d2013-12-08 16:50:17 +02001754 mlx4_bitmap_free(&mlx4_priv(dev)->counters_bitmap, idx, MLX4_USE_RR);
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +00001755 return;
1756}
Jack Morgensteinba062d52012-05-15 10:35:03 +00001757
1758void mlx4_counter_free(struct mlx4_dev *dev, u32 idx)
1759{
Jack Morgensteine7dbeba2013-03-07 03:46:54 +00001760 u64 in_param = 0;
Jack Morgensteinba062d52012-05-15 10:35:03 +00001761
1762 if (mlx4_is_mfunc(dev)) {
1763 set_param_l(&in_param, idx);
1764 mlx4_cmd(dev, in_param, RES_COUNTER, RES_OP_RESERVE,
1765 MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A,
1766 MLX4_CMD_WRAPPED);
1767 return;
1768 }
1769 __mlx4_counter_free(dev, idx);
1770}
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +00001771EXPORT_SYMBOL_GPL(mlx4_counter_free);
1772
Roland Dreier3d73c282007-10-10 15:43:54 -07001773static int mlx4_setup_hca(struct mlx4_dev *dev)
Roland Dreier225c7b12007-05-08 18:00:38 -07001774{
1775 struct mlx4_priv *priv = mlx4_priv(dev);
1776 int err;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07001777 int port;
Jack Morgenstein9a5aa622008-11-28 21:29:46 -08001778 __be32 ib_port_default_caps;
Roland Dreier225c7b12007-05-08 18:00:38 -07001779
Roland Dreier225c7b12007-05-08 18:00:38 -07001780 err = mlx4_init_uar_table(dev);
1781 if (err) {
1782 mlx4_err(dev, "Failed to initialize "
1783 "user access region table, aborting.\n");
1784 return err;
1785 }
1786
1787 err = mlx4_uar_alloc(dev, &priv->driver_uar);
1788 if (err) {
1789 mlx4_err(dev, "Failed to allocate driver access region, "
1790 "aborting.\n");
1791 goto err_uar_table_free;
1792 }
1793
Roland Dreier4979d182011-01-12 09:50:36 -08001794 priv->kar = ioremap((phys_addr_t) priv->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
Roland Dreier225c7b12007-05-08 18:00:38 -07001795 if (!priv->kar) {
1796 mlx4_err(dev, "Couldn't map kernel access region, "
1797 "aborting.\n");
1798 err = -ENOMEM;
1799 goto err_uar_free;
1800 }
1801
1802 err = mlx4_init_pd_table(dev);
1803 if (err) {
1804 mlx4_err(dev, "Failed to initialize "
1805 "protection domain table, aborting.\n");
1806 goto err_kar_unmap;
1807 }
1808
Sean Hefty012a8ff2011-06-02 09:01:33 -07001809 err = mlx4_init_xrcd_table(dev);
1810 if (err) {
1811 mlx4_err(dev, "Failed to initialize "
1812 "reliable connection domain table, aborting.\n");
1813 goto err_pd_table_free;
1814 }
1815
Roland Dreier225c7b12007-05-08 18:00:38 -07001816 err = mlx4_init_mr_table(dev);
1817 if (err) {
1818 mlx4_err(dev, "Failed to initialize "
1819 "memory region table, aborting.\n");
Sean Hefty012a8ff2011-06-02 09:01:33 -07001820 goto err_xrcd_table_free;
Roland Dreier225c7b12007-05-08 18:00:38 -07001821 }
1822
Yevgeny Petrilinfe6f7002013-07-28 18:54:21 +03001823 if (!mlx4_is_slave(dev)) {
1824 err = mlx4_init_mcg_table(dev);
1825 if (err) {
1826 mlx4_err(dev, "Failed to initialize multicast group table, aborting.\n");
1827 goto err_mr_table_free;
1828 }
1829 }
1830
Roland Dreier225c7b12007-05-08 18:00:38 -07001831 err = mlx4_init_eq_table(dev);
1832 if (err) {
1833 mlx4_err(dev, "Failed to initialize "
1834 "event queue table, aborting.\n");
Yevgeny Petrilinfe6f7002013-07-28 18:54:21 +03001835 goto err_mcg_table_free;
Roland Dreier225c7b12007-05-08 18:00:38 -07001836 }
1837
1838 err = mlx4_cmd_use_events(dev);
1839 if (err) {
1840 mlx4_err(dev, "Failed to switch to event-driven "
1841 "firmware commands, aborting.\n");
1842 goto err_eq_table_free;
1843 }
1844
1845 err = mlx4_NOP(dev);
1846 if (err) {
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +03001847 if (dev->flags & MLX4_FLAG_MSI_X) {
1848 mlx4_warn(dev, "NOP command failed to generate MSI-X "
1849 "interrupt IRQ %d).\n",
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08001850 priv->eq_table.eq[dev->caps.num_comp_vectors].irq);
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +03001851 mlx4_warn(dev, "Trying again without MSI-X.\n");
1852 } else {
1853 mlx4_err(dev, "NOP command failed to generate interrupt "
1854 "(IRQ %d), aborting.\n",
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08001855 priv->eq_table.eq[dev->caps.num_comp_vectors].irq);
Roland Dreier225c7b12007-05-08 18:00:38 -07001856 mlx4_err(dev, "BIOS or ACPI interrupt routing problem?\n");
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +03001857 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001858
1859 goto err_cmd_poll;
1860 }
1861
1862 mlx4_dbg(dev, "NOP command IRQ test passed\n");
1863
1864 err = mlx4_init_cq_table(dev);
1865 if (err) {
1866 mlx4_err(dev, "Failed to initialize "
1867 "completion queue table, aborting.\n");
1868 goto err_cmd_poll;
1869 }
1870
1871 err = mlx4_init_srq_table(dev);
1872 if (err) {
1873 mlx4_err(dev, "Failed to initialize "
1874 "shared receive queue table, aborting.\n");
1875 goto err_cq_table_free;
1876 }
1877
1878 err = mlx4_init_qp_table(dev);
1879 if (err) {
1880 mlx4_err(dev, "Failed to initialize "
1881 "queue pair table, aborting.\n");
1882 goto err_srq_table_free;
1883 }
1884
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +00001885 err = mlx4_init_counters_table(dev);
1886 if (err && err != -ENOENT) {
1887 mlx4_err(dev, "Failed to initialize counters table, aborting.\n");
Yevgeny Petrilinfe6f7002013-07-28 18:54:21 +03001888 goto err_qp_table_free;
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +00001889 }
1890
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001891 if (!mlx4_is_slave(dev)) {
1892 for (port = 1; port <= dev->caps.num_ports; port++) {
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001893 ib_port_default_caps = 0;
1894 err = mlx4_get_port_ib_caps(dev, port,
1895 &ib_port_default_caps);
1896 if (err)
1897 mlx4_warn(dev, "failed to get port %d default "
1898 "ib capabilities (%d). Continuing "
1899 "with caps = 0\n", port, err);
1900 dev->caps.ib_port_def_cap[port] = ib_port_default_caps;
Marcel Apfelbaum97285b72011-10-24 11:02:34 +02001901
Jack Morgenstein2aca1172012-06-19 11:21:41 +03001902 /* initialize per-slave default ib port capabilities */
1903 if (mlx4_is_master(dev)) {
1904 int i;
1905 for (i = 0; i < dev->num_slaves; i++) {
1906 if (i == mlx4_master_func_num(dev))
1907 continue;
1908 priv->mfunc.master.slave_state[i].ib_cap_mask[port] =
1909 ib_port_default_caps;
1910 }
1911 }
1912
Or Gerlitz096335b2012-01-11 19:02:17 +02001913 if (mlx4_is_mfunc(dev))
1914 dev->caps.port_ib_mtu[port] = IB_MTU_2048;
1915 else
1916 dev->caps.port_ib_mtu[port] = IB_MTU_4096;
Marcel Apfelbaum97285b72011-10-24 11:02:34 +02001917
Jack Morgenstein66349612012-06-19 11:21:44 +03001918 err = mlx4_SET_PORT(dev, port, mlx4_is_master(dev) ?
1919 dev->caps.pkey_table_len[port] : -1);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001920 if (err) {
1921 mlx4_err(dev, "Failed to set port %d, aborting\n",
1922 port);
1923 goto err_counters_table_free;
1924 }
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07001925 }
1926 }
1927
Roland Dreier225c7b12007-05-08 18:00:38 -07001928 return 0;
1929
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +00001930err_counters_table_free:
1931 mlx4_cleanup_counters_table(dev);
1932
Roland Dreier225c7b12007-05-08 18:00:38 -07001933err_qp_table_free:
1934 mlx4_cleanup_qp_table(dev);
1935
1936err_srq_table_free:
1937 mlx4_cleanup_srq_table(dev);
1938
1939err_cq_table_free:
1940 mlx4_cleanup_cq_table(dev);
1941
1942err_cmd_poll:
1943 mlx4_cmd_use_polling(dev);
1944
1945err_eq_table_free:
1946 mlx4_cleanup_eq_table(dev);
1947
Yevgeny Petrilinfe6f7002013-07-28 18:54:21 +03001948err_mcg_table_free:
1949 if (!mlx4_is_slave(dev))
1950 mlx4_cleanup_mcg_table(dev);
1951
Jack Morgensteinee49bd92007-07-12 17:50:45 +03001952err_mr_table_free:
Roland Dreier225c7b12007-05-08 18:00:38 -07001953 mlx4_cleanup_mr_table(dev);
1954
Sean Hefty012a8ff2011-06-02 09:01:33 -07001955err_xrcd_table_free:
1956 mlx4_cleanup_xrcd_table(dev);
1957
Roland Dreier225c7b12007-05-08 18:00:38 -07001958err_pd_table_free:
1959 mlx4_cleanup_pd_table(dev);
1960
1961err_kar_unmap:
1962 iounmap(priv->kar);
1963
1964err_uar_free:
1965 mlx4_uar_free(dev, &priv->driver_uar);
1966
1967err_uar_table_free:
1968 mlx4_cleanup_uar_table(dev);
1969 return err;
1970}
1971
Roland Dreiere8f9b2e2008-02-04 20:20:41 -08001972static void mlx4_enable_msi_x(struct mlx4_dev *dev)
Roland Dreier225c7b12007-05-08 18:00:38 -07001973{
1974 struct mlx4_priv *priv = mlx4_priv(dev);
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08001975 struct msix_entry *entries;
Yevgeny Petrilin0b7ca5a2011-03-22 22:37:47 +00001976 int nreq = min_t(int, dev->caps.num_ports *
Yuval Mintz90b1ebe2012-07-01 03:18:51 +00001977 min_t(int, netif_get_num_default_rss_queues() + 1,
1978 MAX_MSIX_P_PORT) + MSIX_LEGACY_SZ, MAX_MSIX);
Roland Dreier225c7b12007-05-08 18:00:38 -07001979 int err;
1980 int i;
1981
1982 if (msi_x) {
Or Gerlitzca4c7b32013-01-17 05:30:43 +00001983 nreq = min_t(int, dev->caps.num_eqs - dev->caps.reserved_eqs,
1984 nreq);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001985
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08001986 entries = kcalloc(nreq, sizeof *entries, GFP_KERNEL);
1987 if (!entries)
1988 goto no_msi;
1989
1990 for (i = 0; i < nreq; ++i)
Roland Dreier225c7b12007-05-08 18:00:38 -07001991 entries[i].entry = i;
1992
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08001993 retry:
1994 err = pci_enable_msix(dev->pdev, entries, nreq);
Roland Dreier225c7b12007-05-08 18:00:38 -07001995 if (err) {
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08001996 /* Try again if at least 2 vectors are available */
1997 if (err > 1) {
1998 mlx4_info(dev, "Requested %d vectors, "
1999 "but only %d MSI-X vectors available, "
2000 "trying again\n", nreq, err);
2001 nreq = err;
2002 goto retry;
2003 }
Nicolas Morey-Chaisemartin5bf0da72009-04-21 10:11:06 -07002004 kfree(entries);
Roland Dreier225c7b12007-05-08 18:00:38 -07002005 goto no_msi;
2006 }
2007
Yevgeny Petrilin0b7ca5a2011-03-22 22:37:47 +00002008 if (nreq <
2009 MSIX_LEGACY_SZ + dev->caps.num_ports * MIN_MSIX_P_PORT) {
2010 /*Working in legacy mode , all EQ's shared*/
2011 dev->caps.comp_pool = 0;
2012 dev->caps.num_comp_vectors = nreq - 1;
2013 } else {
2014 dev->caps.comp_pool = nreq - MSIX_LEGACY_SZ;
2015 dev->caps.num_comp_vectors = MSIX_LEGACY_SZ - 1;
2016 }
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08002017 for (i = 0; i < nreq; ++i)
Roland Dreier225c7b12007-05-08 18:00:38 -07002018 priv->eq_table.eq[i].irq = entries[i].vector;
2019
2020 dev->flags |= MLX4_FLAG_MSI_X;
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08002021
2022 kfree(entries);
Roland Dreier225c7b12007-05-08 18:00:38 -07002023 return;
2024 }
2025
2026no_msi:
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08002027 dev->caps.num_comp_vectors = 1;
Yevgeny Petrilin0b7ca5a2011-03-22 22:37:47 +00002028 dev->caps.comp_pool = 0;
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08002029
2030 for (i = 0; i < 2; ++i)
Roland Dreier225c7b12007-05-08 18:00:38 -07002031 priv->eq_table.eq[i].irq = dev->pdev->irq;
2032}
2033
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002034static int mlx4_init_port_info(struct mlx4_dev *dev, int port)
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -07002035{
2036 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002037 int err = 0;
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -07002038
2039 info->dev = dev;
2040 info->port = port;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002041 if (!mlx4_is_slave(dev)) {
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002042 mlx4_init_mac_table(dev, &info->mac_table);
2043 mlx4_init_vlan_table(dev, &info->vlan_table);
Yan Burman16a10ff2013-02-07 02:25:22 +00002044 info->base_qpn = mlx4_get_base_qpn(dev, port);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002045 }
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002046
2047 sprintf(info->dev_name, "mlx4_port%d", port);
2048 info->port_attr.attr.name = info->dev_name;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002049 if (mlx4_is_mfunc(dev))
2050 info->port_attr.attr.mode = S_IRUGO;
2051 else {
2052 info->port_attr.attr.mode = S_IRUGO | S_IWUSR;
2053 info->port_attr.store = set_port_type;
2054 }
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002055 info->port_attr.show = show_port_type;
Greg Kroah-Hartman3691c962010-03-15 14:01:55 -07002056 sysfs_attr_init(&info->port_attr.attr);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002057
2058 err = device_create_file(&dev->pdev->dev, &info->port_attr);
2059 if (err) {
2060 mlx4_err(dev, "Failed to create file for port %d\n", port);
2061 info->port = -1;
2062 }
2063
Or Gerlitz096335b2012-01-11 19:02:17 +02002064 sprintf(info->dev_mtu_name, "mlx4_port%d_mtu", port);
2065 info->port_mtu_attr.attr.name = info->dev_mtu_name;
2066 if (mlx4_is_mfunc(dev))
2067 info->port_mtu_attr.attr.mode = S_IRUGO;
2068 else {
2069 info->port_mtu_attr.attr.mode = S_IRUGO | S_IWUSR;
2070 info->port_mtu_attr.store = set_port_ib_mtu;
2071 }
2072 info->port_mtu_attr.show = show_port_ib_mtu;
2073 sysfs_attr_init(&info->port_mtu_attr.attr);
2074
2075 err = device_create_file(&dev->pdev->dev, &info->port_mtu_attr);
2076 if (err) {
2077 mlx4_err(dev, "Failed to create mtu file for port %d\n", port);
2078 device_remove_file(&info->dev->pdev->dev, &info->port_attr);
2079 info->port = -1;
2080 }
2081
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002082 return err;
2083}
2084
2085static void mlx4_cleanup_port_info(struct mlx4_port_info *info)
2086{
2087 if (info->port < 0)
2088 return;
2089
2090 device_remove_file(&info->dev->pdev->dev, &info->port_attr);
Or Gerlitz096335b2012-01-11 19:02:17 +02002091 device_remove_file(&info->dev->pdev->dev, &info->port_mtu_attr);
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -07002092}
2093
Yevgeny Petrilinb12d93d2011-03-22 22:38:24 +00002094static int mlx4_init_steering(struct mlx4_dev *dev)
2095{
2096 struct mlx4_priv *priv = mlx4_priv(dev);
2097 int num_entries = dev->caps.num_ports;
2098 int i, j;
2099
2100 priv->steer = kzalloc(sizeof(struct mlx4_steer) * num_entries, GFP_KERNEL);
2101 if (!priv->steer)
2102 return -ENOMEM;
2103
Eugenia Emantayev45b51362012-02-14 06:37:41 +00002104 for (i = 0; i < num_entries; i++)
Yevgeny Petrilinb12d93d2011-03-22 22:38:24 +00002105 for (j = 0; j < MLX4_NUM_STEERS; j++) {
2106 INIT_LIST_HEAD(&priv->steer[i].promisc_qps[j]);
2107 INIT_LIST_HEAD(&priv->steer[i].steer_entries[j]);
2108 }
Yevgeny Petrilinb12d93d2011-03-22 22:38:24 +00002109 return 0;
2110}
2111
2112static void mlx4_clear_steering(struct mlx4_dev *dev)
2113{
2114 struct mlx4_priv *priv = mlx4_priv(dev);
2115 struct mlx4_steer_index *entry, *tmp_entry;
2116 struct mlx4_promisc_qp *pqp, *tmp_pqp;
2117 int num_entries = dev->caps.num_ports;
2118 int i, j;
2119
2120 for (i = 0; i < num_entries; i++) {
2121 for (j = 0; j < MLX4_NUM_STEERS; j++) {
2122 list_for_each_entry_safe(pqp, tmp_pqp,
2123 &priv->steer[i].promisc_qps[j],
2124 list) {
2125 list_del(&pqp->list);
2126 kfree(pqp);
2127 }
2128 list_for_each_entry_safe(entry, tmp_entry,
2129 &priv->steer[i].steer_entries[j],
2130 list) {
2131 list_del(&entry->list);
2132 list_for_each_entry_safe(pqp, tmp_pqp,
2133 &entry->duplicates,
2134 list) {
2135 list_del(&pqp->list);
2136 kfree(pqp);
2137 }
2138 kfree(entry);
2139 }
2140 }
2141 }
2142 kfree(priv->steer);
2143}
2144
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002145static int extended_func_num(struct pci_dev *pdev)
2146{
2147 return PCI_SLOT(pdev->devfn) * 8 + PCI_FUNC(pdev->devfn);
2148}
2149
2150#define MLX4_OWNER_BASE 0x8069c
2151#define MLX4_OWNER_SIZE 4
2152
2153static int mlx4_get_ownership(struct mlx4_dev *dev)
2154{
2155 void __iomem *owner;
2156 u32 ret;
2157
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +00002158 if (pci_channel_offline(dev->pdev))
2159 return -EIO;
2160
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002161 owner = ioremap(pci_resource_start(dev->pdev, 0) + MLX4_OWNER_BASE,
2162 MLX4_OWNER_SIZE);
2163 if (!owner) {
2164 mlx4_err(dev, "Failed to obtain ownership bit\n");
2165 return -ENOMEM;
2166 }
2167
2168 ret = readl(owner);
2169 iounmap(owner);
2170 return (int) !!ret;
2171}
2172
2173static void mlx4_free_ownership(struct mlx4_dev *dev)
2174{
2175 void __iomem *owner;
2176
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +00002177 if (pci_channel_offline(dev->pdev))
2178 return;
2179
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002180 owner = ioremap(pci_resource_start(dev->pdev, 0) + MLX4_OWNER_BASE,
2181 MLX4_OWNER_SIZE);
2182 if (!owner) {
2183 mlx4_err(dev, "Failed to obtain ownership bit\n");
2184 return;
2185 }
2186 writel(0, owner);
2187 msleep(1000);
2188 iounmap(owner);
2189}
2190
Roland Dreier839f1242012-09-27 09:23:41 -07002191static int __mlx4_init_one(struct pci_dev *pdev, int pci_dev_data)
Roland Dreier225c7b12007-05-08 18:00:38 -07002192{
Roland Dreier225c7b12007-05-08 18:00:38 -07002193 struct mlx4_priv *priv;
2194 struct mlx4_dev *dev;
2195 int err;
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -07002196 int port;
Roland Dreier225c7b12007-05-08 18:00:38 -07002197
Joe Perches0a645e82010-07-10 07:22:46 +00002198 pr_info(DRV_NAME ": Initializing %s\n", pci_name(pdev));
Roland Dreier225c7b12007-05-08 18:00:38 -07002199
2200 err = pci_enable_device(pdev);
2201 if (err) {
2202 dev_err(&pdev->dev, "Cannot enable PCI device, "
2203 "aborting.\n");
2204 return err;
2205 }
Jack Morgenstein5a0d0a62013-11-03 10:03:23 +02002206
2207 /* Due to requirement that all VFs and the PF are *guaranteed* 2 MACS
2208 * per port, we must limit the number of VFs to 63 (since their are
2209 * 128 MACs)
2210 */
2211 if (num_vfs >= MLX4_MAX_NUM_VF) {
2212 dev_err(&pdev->dev,
2213 "Requested more VF's (%d) than allowed (%d)\n",
2214 num_vfs, MLX4_MAX_NUM_VF - 1);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002215 return -EINVAL;
2216 }
Jack Morgenstein30e514a2013-06-25 12:09:38 +03002217
2218 if (num_vfs < 0) {
2219 pr_err("num_vfs module parameter cannot be negative\n");
2220 return -EINVAL;
2221 }
Roland Dreier225c7b12007-05-08 18:00:38 -07002222 /*
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002223 * Check for BARs.
Roland Dreier225c7b12007-05-08 18:00:38 -07002224 */
Roland Dreier839f1242012-09-27 09:23:41 -07002225 if (!(pci_dev_data & MLX4_PCI_DEV_IS_VF) &&
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002226 !(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
2227 dev_err(&pdev->dev, "Missing DCS, aborting."
Roland Dreier839f1242012-09-27 09:23:41 -07002228 "(driver_data: 0x%x, pci_resource_flags(pdev, 0):0x%lx)\n",
2229 pci_dev_data, pci_resource_flags(pdev, 0));
Roland Dreier225c7b12007-05-08 18:00:38 -07002230 err = -ENODEV;
2231 goto err_disable_pdev;
2232 }
2233 if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
2234 dev_err(&pdev->dev, "Missing UAR, aborting.\n");
2235 err = -ENODEV;
2236 goto err_disable_pdev;
2237 }
2238
Roland Dreiera01df0f2009-09-05 20:24:48 -07002239 err = pci_request_regions(pdev, DRV_NAME);
Roland Dreier225c7b12007-05-08 18:00:38 -07002240 if (err) {
Roland Dreiera01df0f2009-09-05 20:24:48 -07002241 dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");
Roland Dreier225c7b12007-05-08 18:00:38 -07002242 goto err_disable_pdev;
2243 }
2244
Roland Dreier225c7b12007-05-08 18:00:38 -07002245 pci_set_master(pdev);
2246
Yang Hongyang6a355282009-04-06 19:01:13 -07002247 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
Roland Dreier225c7b12007-05-08 18:00:38 -07002248 if (err) {
2249 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask.\n");
Yang Hongyang284901a2009-04-06 19:01:15 -07002250 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Roland Dreier225c7b12007-05-08 18:00:38 -07002251 if (err) {
2252 dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting.\n");
Roland Dreiera01df0f2009-09-05 20:24:48 -07002253 goto err_release_regions;
Roland Dreier225c7b12007-05-08 18:00:38 -07002254 }
2255 }
Yang Hongyang6a355282009-04-06 19:01:13 -07002256 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
Roland Dreier225c7b12007-05-08 18:00:38 -07002257 if (err) {
2258 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit "
2259 "consistent PCI DMA mask.\n");
Yang Hongyang284901a2009-04-06 19:01:15 -07002260 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Roland Dreier225c7b12007-05-08 18:00:38 -07002261 if (err) {
2262 dev_err(&pdev->dev, "Can't set consistent PCI DMA mask, "
2263 "aborting.\n");
Roland Dreiera01df0f2009-09-05 20:24:48 -07002264 goto err_release_regions;
Roland Dreier225c7b12007-05-08 18:00:38 -07002265 }
2266 }
2267
David Dillow7f9e5c482011-01-17 02:09:44 +00002268 /* Allow large DMA segments, up to the firmware limit of 1 GB */
2269 dma_set_max_seg_size(&pdev->dev, 1024 * 1024 * 1024);
2270
Joe Perchesb2adaca2013-02-03 17:43:58 +00002271 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Roland Dreier225c7b12007-05-08 18:00:38 -07002272 if (!priv) {
Roland Dreier225c7b12007-05-08 18:00:38 -07002273 err = -ENOMEM;
Roland Dreiera01df0f2009-09-05 20:24:48 -07002274 goto err_release_regions;
Roland Dreier225c7b12007-05-08 18:00:38 -07002275 }
2276
2277 dev = &priv->dev;
2278 dev->pdev = pdev;
Roland Dreierb5814012007-06-07 11:51:58 -07002279 INIT_LIST_HEAD(&priv->ctx_list);
2280 spin_lock_init(&priv->ctx_lock);
Roland Dreier225c7b12007-05-08 18:00:38 -07002281
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002282 mutex_init(&priv->port_mutex);
2283
Yevgeny Petrilin62968832008-04-23 11:55:45 -07002284 INIT_LIST_HEAD(&priv->pgdir_list);
2285 mutex_init(&priv->pgdir_mutex);
2286
Eli Cohenc1b43dc2011-03-22 22:38:41 +00002287 INIT_LIST_HEAD(&priv->bf_list);
2288 mutex_init(&priv->bf_mutex);
2289
Sergei Shtylyovaca7a3a2011-06-23 04:44:30 +00002290 dev->rev_id = pdev->revision;
Eugenia Emantayev6e7136e2013-11-07 12:19:53 +02002291 dev->numa_node = dev_to_node(&pdev->dev);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002292 /* Detect if this device is a virtual function */
Roland Dreier839f1242012-09-27 09:23:41 -07002293 if (pci_dev_data & MLX4_PCI_DEV_IS_VF) {
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002294 /* When acting as pf, we normally skip vfs unless explicitly
2295 * requested to probe them. */
2296 if (num_vfs && extended_func_num(pdev) > probe_vf) {
2297 mlx4_warn(dev, "Skipping virtual function:%d\n",
2298 extended_func_num(pdev));
2299 err = -ENODEV;
2300 goto err_free_dev;
2301 }
2302 mlx4_warn(dev, "Detected virtual function - running in slave mode\n");
2303 dev->flags |= MLX4_FLAG_SLAVE;
2304 } else {
2305 /* We reset the device and enable SRIOV only for physical
2306 * devices. Try to claim ownership on the device;
2307 * if already taken, skip -- do not allow multiple PFs */
2308 err = mlx4_get_ownership(dev);
2309 if (err) {
2310 if (err < 0)
2311 goto err_free_dev;
2312 else {
2313 mlx4_warn(dev, "Multiple PFs not yet supported."
2314 " Skipping PF.\n");
2315 err = -EINVAL;
2316 goto err_free_dev;
2317 }
2318 }
Sergei Shtylyovaca7a3a2011-06-23 04:44:30 +00002319
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002320 if (num_vfs) {
Roland Dreier84b1f152012-09-25 17:09:42 -07002321 mlx4_warn(dev, "Enabling SR-IOV with %d VFs\n", num_vfs);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002322 err = pci_enable_sriov(pdev, num_vfs);
2323 if (err) {
Roland Dreier84b1f152012-09-25 17:09:42 -07002324 mlx4_err(dev, "Failed to enable SR-IOV, continuing without SR-IOV (err = %d).\n",
2325 err);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002326 err = 0;
2327 } else {
2328 mlx4_warn(dev, "Running in master mode\n");
2329 dev->flags |= MLX4_FLAG_SRIOV |
2330 MLX4_FLAG_MASTER;
2331 dev->num_vfs = num_vfs;
2332 }
2333 }
2334
Yevgeny Petrilinfe6f7002013-07-28 18:54:21 +03002335 atomic_set(&priv->opreq_count, 0);
2336 INIT_WORK(&priv->opreq_task, mlx4_opreq_action);
2337
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002338 /*
2339 * Now reset the HCA before we touch the PCI capabilities or
2340 * attempt a firmware command, since a boot ROM may have left
2341 * the HCA in an undefined state.
2342 */
2343 err = mlx4_reset(dev);
2344 if (err) {
2345 mlx4_err(dev, "Failed to reset HCA, aborting.\n");
2346 goto err_rel_own;
2347 }
Roland Dreier225c7b12007-05-08 18:00:38 -07002348 }
2349
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002350slave_start:
Eugenia Emantayev521130d2012-09-05 22:50:52 +00002351 err = mlx4_cmd_init(dev);
2352 if (err) {
Roland Dreier225c7b12007-05-08 18:00:38 -07002353 mlx4_err(dev, "Failed to init command interface, aborting.\n");
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002354 goto err_sriov;
2355 }
2356
2357 /* In slave functions, the communication channel must be initialized
2358 * before posting commands. Also, init num_slaves before calling
2359 * mlx4_init_hca */
2360 if (mlx4_is_mfunc(dev)) {
2361 if (mlx4_is_master(dev))
2362 dev->num_slaves = MLX4_MAX_NUM_SLAVES;
2363 else {
2364 dev->num_slaves = 0;
Jack Morgensteinf356fcbe2013-01-24 01:54:17 +00002365 err = mlx4_multi_func_init(dev);
2366 if (err) {
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002367 mlx4_err(dev, "Failed to init slave mfunc"
2368 " interface, aborting.\n");
2369 goto err_cmd;
2370 }
2371 }
Roland Dreier225c7b12007-05-08 18:00:38 -07002372 }
2373
2374 err = mlx4_init_hca(dev);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002375 if (err) {
2376 if (err == -EACCES) {
2377 /* Not primary Physical function
2378 * Running in slave mode */
2379 mlx4_cmd_cleanup(dev);
2380 dev->flags |= MLX4_FLAG_SLAVE;
2381 dev->flags &= ~MLX4_FLAG_MASTER;
2382 goto slave_start;
2383 } else
2384 goto err_mfunc;
2385 }
2386
Eyal Perryb912b2f2014-01-05 17:41:08 +02002387 /* check if the device is functioning at its maximum possible speed.
2388 * No return code for this call, just warn the user in case of PCI
2389 * express device capabilities are under-satisfied by the bus.
2390 */
2391 mlx4_check_pcie_caps(dev);
2392
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002393 /* In master functions, the communication channel must be initialized
2394 * after obtaining its address from fw */
2395 if (mlx4_is_master(dev)) {
Jack Morgensteinf356fcbe2013-01-24 01:54:17 +00002396 err = mlx4_multi_func_init(dev);
2397 if (err) {
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002398 mlx4_err(dev, "Failed to init master mfunc"
2399 "interface, aborting.\n");
2400 goto err_close;
2401 }
2402 }
Roland Dreier225c7b12007-05-08 18:00:38 -07002403
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08002404 err = mlx4_alloc_eq_table(dev);
2405 if (err)
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002406 goto err_master_mfunc;
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08002407
Yevgeny Petrilin0b7ca5a2011-03-22 22:37:47 +00002408 priv->msix_ctl.pool_bm = 0;
Yevgeny Petrilin730c41d2012-02-21 03:39:32 +00002409 mutex_init(&priv->msix_ctl.pool_lock);
Yevgeny Petrilin0b7ca5a2011-03-22 22:37:47 +00002410
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +03002411 mlx4_enable_msi_x(dev);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002412 if ((mlx4_is_mfunc(dev)) &&
2413 !(dev->flags & MLX4_FLAG_MSI_X)) {
Jack Morgensteinf356fcbe2013-01-24 01:54:17 +00002414 err = -ENOSYS;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002415 mlx4_err(dev, "INTx is not supported in multi-function mode."
2416 " aborting.\n");
Yevgeny Petrilinb12d93d2011-03-22 22:38:24 +00002417 goto err_free_eq;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002418 }
2419
2420 if (!mlx4_is_slave(dev)) {
2421 err = mlx4_init_steering(dev);
2422 if (err)
2423 goto err_free_eq;
2424 }
Yevgeny Petrilinb12d93d2011-03-22 22:38:24 +00002425
Roland Dreier225c7b12007-05-08 18:00:38 -07002426 err = mlx4_setup_hca(dev);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002427 if (err == -EBUSY && (dev->flags & MLX4_FLAG_MSI_X) &&
2428 !mlx4_is_mfunc(dev)) {
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +03002429 dev->flags &= ~MLX4_FLAG_MSI_X;
Yevgeny Petrilin9858d2d2012-06-25 00:24:12 +00002430 dev->caps.num_comp_vectors = 1;
2431 dev->caps.comp_pool = 0;
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +03002432 pci_disable_msix(pdev);
2433 err = mlx4_setup_hca(dev);
2434 }
2435
Roland Dreier225c7b12007-05-08 18:00:38 -07002436 if (err)
Yevgeny Petrilinb12d93d2011-03-22 22:38:24 +00002437 goto err_steer;
Roland Dreier225c7b12007-05-08 18:00:38 -07002438
Jack Morgenstein5a0d0a62013-11-03 10:03:23 +02002439 mlx4_init_quotas(dev);
2440
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002441 for (port = 1; port <= dev->caps.num_ports; port++) {
2442 err = mlx4_init_port_info(dev, port);
2443 if (err)
2444 goto err_port;
2445 }
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -07002446
Roland Dreier225c7b12007-05-08 18:00:38 -07002447 err = mlx4_register_device(dev);
2448 if (err)
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002449 goto err_port;
Roland Dreier225c7b12007-05-08 18:00:38 -07002450
Eyal Perryb046ffe2013-10-15 16:55:24 +02002451 mlx4_request_modules(dev);
2452
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -07002453 mlx4_sense_init(dev);
2454 mlx4_start_sense(dev);
2455
Roland Dreier839f1242012-09-27 09:23:41 -07002456 priv->pci_dev_data = pci_dev_data;
Roland Dreier225c7b12007-05-08 18:00:38 -07002457 pci_set_drvdata(pdev, dev);
2458
2459 return 0;
2460
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002461err_port:
Eli Cohenb4f77262010-01-06 12:54:39 -08002462 for (--port; port >= 1; --port)
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002463 mlx4_cleanup_port_info(&priv->port[port]);
2464
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +00002465 mlx4_cleanup_counters_table(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002466 mlx4_cleanup_qp_table(dev);
2467 mlx4_cleanup_srq_table(dev);
2468 mlx4_cleanup_cq_table(dev);
2469 mlx4_cmd_use_polling(dev);
2470 mlx4_cleanup_eq_table(dev);
Yevgeny Petrilinfe6f7002013-07-28 18:54:21 +03002471 mlx4_cleanup_mcg_table(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002472 mlx4_cleanup_mr_table(dev);
Sean Hefty012a8ff2011-06-02 09:01:33 -07002473 mlx4_cleanup_xrcd_table(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002474 mlx4_cleanup_pd_table(dev);
2475 mlx4_cleanup_uar_table(dev);
2476
Yevgeny Petrilinb12d93d2011-03-22 22:38:24 +00002477err_steer:
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002478 if (!mlx4_is_slave(dev))
2479 mlx4_clear_steering(dev);
Yevgeny Petrilinb12d93d2011-03-22 22:38:24 +00002480
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08002481err_free_eq:
2482 mlx4_free_eq_table(dev);
2483
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002484err_master_mfunc:
2485 if (mlx4_is_master(dev))
2486 mlx4_multi_func_cleanup(dev);
2487
Roland Dreier225c7b12007-05-08 18:00:38 -07002488err_close:
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +03002489 if (dev->flags & MLX4_FLAG_MSI_X)
2490 pci_disable_msix(pdev);
2491
Roland Dreier225c7b12007-05-08 18:00:38 -07002492 mlx4_close_hca(dev);
2493
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002494err_mfunc:
2495 if (mlx4_is_slave(dev))
2496 mlx4_multi_func_cleanup(dev);
2497
Roland Dreier225c7b12007-05-08 18:00:38 -07002498err_cmd:
2499 mlx4_cmd_cleanup(dev);
2500
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002501err_sriov:
Jack Morgenstein681372a2012-05-15 10:35:01 +00002502 if (dev->flags & MLX4_FLAG_SRIOV)
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002503 pci_disable_sriov(pdev);
2504
2505err_rel_own:
2506 if (!mlx4_is_slave(dev))
2507 mlx4_free_ownership(dev);
2508
Roland Dreier225c7b12007-05-08 18:00:38 -07002509err_free_dev:
Roland Dreier225c7b12007-05-08 18:00:38 -07002510 kfree(priv);
2511
Roland Dreiera01df0f2009-09-05 20:24:48 -07002512err_release_regions:
2513 pci_release_regions(pdev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002514
2515err_disable_pdev:
2516 pci_disable_device(pdev);
2517 pci_set_drvdata(pdev, NULL);
2518 return err;
2519}
2520
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002521static int mlx4_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
Roland Dreier3d73c282007-10-10 15:43:54 -07002522{
Joe Perches0a645e82010-07-10 07:22:46 +00002523 printk_once(KERN_INFO "%s", mlx4_version);
Roland Dreier3d73c282007-10-10 15:43:54 -07002524
Roland Dreier839f1242012-09-27 09:23:41 -07002525 return __mlx4_init_one(pdev, id->driver_data);
Roland Dreier3d73c282007-10-10 15:43:54 -07002526}
2527
2528static void mlx4_remove_one(struct pci_dev *pdev)
Roland Dreier225c7b12007-05-08 18:00:38 -07002529{
2530 struct mlx4_dev *dev = pci_get_drvdata(pdev);
2531 struct mlx4_priv *priv = mlx4_priv(dev);
2532 int p;
2533
2534 if (dev) {
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002535 /* in SRIOV it is not allowed to unload the pf's
2536 * driver while there are alive vf's */
2537 if (mlx4_is_master(dev)) {
2538 if (mlx4_how_many_lives_vf(dev))
2539 printk(KERN_ERR "Removing PF when there are assigned VF's !!!\n");
2540 }
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -07002541 mlx4_stop_sense(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002542 mlx4_unregister_device(dev);
2543
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002544 for (p = 1; p <= dev->caps.num_ports; p++) {
2545 mlx4_cleanup_port_info(&priv->port[p]);
Roland Dreier225c7b12007-05-08 18:00:38 -07002546 mlx4_CLOSE_PORT(dev, p);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002547 }
Roland Dreier225c7b12007-05-08 18:00:38 -07002548
Jack Morgensteinb8924952012-05-15 10:35:02 +00002549 if (mlx4_is_master(dev))
2550 mlx4_free_resource_tracker(dev,
2551 RES_TR_FREE_SLAVES_ONLY);
2552
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +00002553 mlx4_cleanup_counters_table(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002554 mlx4_cleanup_qp_table(dev);
2555 mlx4_cleanup_srq_table(dev);
2556 mlx4_cleanup_cq_table(dev);
2557 mlx4_cmd_use_polling(dev);
2558 mlx4_cleanup_eq_table(dev);
Yevgeny Petrilinfe6f7002013-07-28 18:54:21 +03002559 mlx4_cleanup_mcg_table(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002560 mlx4_cleanup_mr_table(dev);
Sean Hefty012a8ff2011-06-02 09:01:33 -07002561 mlx4_cleanup_xrcd_table(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002562 mlx4_cleanup_pd_table(dev);
2563
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002564 if (mlx4_is_master(dev))
Jack Morgensteinb8924952012-05-15 10:35:02 +00002565 mlx4_free_resource_tracker(dev,
2566 RES_TR_FREE_STRUCTS_ONLY);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002567
Roland Dreier225c7b12007-05-08 18:00:38 -07002568 iounmap(priv->kar);
2569 mlx4_uar_free(dev, &priv->driver_uar);
2570 mlx4_cleanup_uar_table(dev);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002571 if (!mlx4_is_slave(dev))
2572 mlx4_clear_steering(dev);
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08002573 mlx4_free_eq_table(dev);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002574 if (mlx4_is_master(dev))
2575 mlx4_multi_func_cleanup(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002576 mlx4_close_hca(dev);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002577 if (mlx4_is_slave(dev))
2578 mlx4_multi_func_cleanup(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002579 mlx4_cmd_cleanup(dev);
2580
2581 if (dev->flags & MLX4_FLAG_MSI_X)
2582 pci_disable_msix(pdev);
Jack Morgenstein681372a2012-05-15 10:35:01 +00002583 if (dev->flags & MLX4_FLAG_SRIOV) {
Roland Dreier84b1f152012-09-25 17:09:42 -07002584 mlx4_warn(dev, "Disabling SR-IOV\n");
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002585 pci_disable_sriov(pdev);
2586 }
Roland Dreier225c7b12007-05-08 18:00:38 -07002587
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002588 if (!mlx4_is_slave(dev))
2589 mlx4_free_ownership(dev);
Jack Morgenstein47605df2012-08-03 08:40:57 +00002590
2591 kfree(dev->caps.qp0_tunnel);
2592 kfree(dev->caps.qp0_proxy);
2593 kfree(dev->caps.qp1_tunnel);
2594 kfree(dev->caps.qp1_proxy);
2595
Roland Dreier225c7b12007-05-08 18:00:38 -07002596 kfree(priv);
Roland Dreiera01df0f2009-09-05 20:24:48 -07002597 pci_release_regions(pdev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002598 pci_disable_device(pdev);
2599 pci_set_drvdata(pdev, NULL);
2600 }
2601}
2602
Jack Morgensteinee49bd92007-07-12 17:50:45 +03002603int mlx4_restart_one(struct pci_dev *pdev)
2604{
Roland Dreier839f1242012-09-27 09:23:41 -07002605 struct mlx4_dev *dev = pci_get_drvdata(pdev);
2606 struct mlx4_priv *priv = mlx4_priv(dev);
2607 int pci_dev_data;
2608
2609 pci_dev_data = priv->pci_dev_data;
Jack Morgensteinee49bd92007-07-12 17:50:45 +03002610 mlx4_remove_one(pdev);
Roland Dreier839f1242012-09-27 09:23:41 -07002611 return __mlx4_init_one(pdev, pci_dev_data);
Jack Morgensteinee49bd92007-07-12 17:50:45 +03002612}
2613
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002614static DEFINE_PCI_DEVICE_TABLE(mlx4_pci_table) = {
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002615 /* MT25408 "Hermon" SDR */
Roland Dreierca3e57a2012-09-27 09:53:05 -07002616 { PCI_VDEVICE(MELLANOX, 0x6340), MLX4_PCI_DEV_FORCE_SENSE_PORT },
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002617 /* MT25408 "Hermon" DDR */
Roland Dreierca3e57a2012-09-27 09:53:05 -07002618 { PCI_VDEVICE(MELLANOX, 0x634a), MLX4_PCI_DEV_FORCE_SENSE_PORT },
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002619 /* MT25408 "Hermon" QDR */
Roland Dreierca3e57a2012-09-27 09:53:05 -07002620 { PCI_VDEVICE(MELLANOX, 0x6354), MLX4_PCI_DEV_FORCE_SENSE_PORT },
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002621 /* MT25408 "Hermon" DDR PCIe gen2 */
Roland Dreierca3e57a2012-09-27 09:53:05 -07002622 { PCI_VDEVICE(MELLANOX, 0x6732), MLX4_PCI_DEV_FORCE_SENSE_PORT },
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002623 /* MT25408 "Hermon" QDR PCIe gen2 */
Roland Dreierca3e57a2012-09-27 09:53:05 -07002624 { PCI_VDEVICE(MELLANOX, 0x673c), MLX4_PCI_DEV_FORCE_SENSE_PORT },
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002625 /* MT25408 "Hermon" EN 10GigE */
Roland Dreierca3e57a2012-09-27 09:53:05 -07002626 { PCI_VDEVICE(MELLANOX, 0x6368), MLX4_PCI_DEV_FORCE_SENSE_PORT },
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002627 /* MT25408 "Hermon" EN 10GigE PCIe gen2 */
Roland Dreierca3e57a2012-09-27 09:53:05 -07002628 { PCI_VDEVICE(MELLANOX, 0x6750), MLX4_PCI_DEV_FORCE_SENSE_PORT },
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002629 /* MT25458 ConnectX EN 10GBASE-T 10GigE */
Roland Dreierca3e57a2012-09-27 09:53:05 -07002630 { PCI_VDEVICE(MELLANOX, 0x6372), MLX4_PCI_DEV_FORCE_SENSE_PORT },
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002631 /* MT25458 ConnectX EN 10GBASE-T+Gen2 10GigE */
Roland Dreierca3e57a2012-09-27 09:53:05 -07002632 { PCI_VDEVICE(MELLANOX, 0x675a), MLX4_PCI_DEV_FORCE_SENSE_PORT },
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002633 /* MT26468 ConnectX EN 10GigE PCIe gen2*/
Roland Dreierca3e57a2012-09-27 09:53:05 -07002634 { PCI_VDEVICE(MELLANOX, 0x6764), MLX4_PCI_DEV_FORCE_SENSE_PORT },
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002635 /* MT26438 ConnectX EN 40GigE PCIe gen2 5GT/s */
Roland Dreierca3e57a2012-09-27 09:53:05 -07002636 { PCI_VDEVICE(MELLANOX, 0x6746), MLX4_PCI_DEV_FORCE_SENSE_PORT },
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002637 /* MT26478 ConnectX2 40GigE PCIe gen2 */
Roland Dreierca3e57a2012-09-27 09:53:05 -07002638 { PCI_VDEVICE(MELLANOX, 0x676e), MLX4_PCI_DEV_FORCE_SENSE_PORT },
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002639 /* MT25400 Family [ConnectX-2 Virtual Function] */
Roland Dreier839f1242012-09-27 09:23:41 -07002640 { PCI_VDEVICE(MELLANOX, 0x1002), MLX4_PCI_DEV_IS_VF },
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002641 /* MT27500 Family [ConnectX-3] */
2642 { PCI_VDEVICE(MELLANOX, 0x1003), 0 },
2643 /* MT27500 Family [ConnectX-3 Virtual Function] */
Roland Dreier839f1242012-09-27 09:23:41 -07002644 { PCI_VDEVICE(MELLANOX, 0x1004), MLX4_PCI_DEV_IS_VF },
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002645 { PCI_VDEVICE(MELLANOX, 0x1005), 0 }, /* MT27510 Family */
2646 { PCI_VDEVICE(MELLANOX, 0x1006), 0 }, /* MT27511 Family */
2647 { PCI_VDEVICE(MELLANOX, 0x1007), 0 }, /* MT27520 Family */
2648 { PCI_VDEVICE(MELLANOX, 0x1008), 0 }, /* MT27521 Family */
2649 { PCI_VDEVICE(MELLANOX, 0x1009), 0 }, /* MT27530 Family */
2650 { PCI_VDEVICE(MELLANOX, 0x100a), 0 }, /* MT27531 Family */
2651 { PCI_VDEVICE(MELLANOX, 0x100b), 0 }, /* MT27540 Family */
2652 { PCI_VDEVICE(MELLANOX, 0x100c), 0 }, /* MT27541 Family */
2653 { PCI_VDEVICE(MELLANOX, 0x100d), 0 }, /* MT27550 Family */
2654 { PCI_VDEVICE(MELLANOX, 0x100e), 0 }, /* MT27551 Family */
2655 { PCI_VDEVICE(MELLANOX, 0x100f), 0 }, /* MT27560 Family */
2656 { PCI_VDEVICE(MELLANOX, 0x1010), 0 }, /* MT27561 Family */
Roland Dreier225c7b12007-05-08 18:00:38 -07002657 { 0, }
2658};
2659
2660MODULE_DEVICE_TABLE(pci, mlx4_pci_table);
2661
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +00002662static pci_ers_result_t mlx4_pci_err_detected(struct pci_dev *pdev,
2663 pci_channel_state_t state)
2664{
2665 mlx4_remove_one(pdev);
2666
2667 return state == pci_channel_io_perm_failure ?
2668 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
2669}
2670
2671static pci_ers_result_t mlx4_pci_slot_reset(struct pci_dev *pdev)
2672{
Roland Dreier839f1242012-09-27 09:23:41 -07002673 int ret = __mlx4_init_one(pdev, 0);
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +00002674
2675 return ret ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
2676}
2677
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07002678static const struct pci_error_handlers mlx4_err_handler = {
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +00002679 .error_detected = mlx4_pci_err_detected,
2680 .slot_reset = mlx4_pci_slot_reset,
2681};
2682
Roland Dreier225c7b12007-05-08 18:00:38 -07002683static struct pci_driver mlx4_driver = {
2684 .name = DRV_NAME,
2685 .id_table = mlx4_pci_table,
2686 .probe = mlx4_init_one,
Gavin Shan367d56f2014-03-04 15:35:20 +08002687 .shutdown = mlx4_remove_one,
Bill Pembertonf57e6842012-12-03 09:23:15 -05002688 .remove = mlx4_remove_one,
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +00002689 .err_handler = &mlx4_err_handler,
Roland Dreier225c7b12007-05-08 18:00:38 -07002690};
2691
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002692static int __init mlx4_verify_params(void)
2693{
2694 if ((log_num_mac < 0) || (log_num_mac > 7)) {
Joe Perches0a645e82010-07-10 07:22:46 +00002695 pr_warning("mlx4_core: bad num_mac: %d\n", log_num_mac);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002696 return -1;
2697 }
2698
Or Gerlitzcb296882011-10-16 10:26:21 +02002699 if (log_num_vlan != 0)
2700 pr_warning("mlx4_core: log_num_vlan - obsolete module param, using %d\n",
2701 MLX4_LOG_NUM_VLANS);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002702
Eli Cohen04986282010-09-20 08:42:38 +02002703 if ((log_mtts_per_seg < 1) || (log_mtts_per_seg > 7)) {
Joe Perches0a645e82010-07-10 07:22:46 +00002704 pr_warning("mlx4_core: bad log_mtts_per_seg: %d\n", log_mtts_per_seg);
Eli Cohenab6bf422009-05-27 14:38:34 -07002705 return -1;
2706 }
2707
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002708 /* Check if module param for ports type has legal combination */
2709 if (port_type_array[0] == false && port_type_array[1] == true) {
2710 printk(KERN_WARNING "Module parameter configuration ETH/IB is not supported. Switching to default configuration IB/IB\n");
2711 port_type_array[0] = true;
2712 }
2713
Jack Morgenstein3c439b52012-12-06 17:12:00 +00002714 if (mlx4_log_num_mgm_entry_size != -1 &&
2715 (mlx4_log_num_mgm_entry_size < MLX4_MIN_MGM_LOG_ENTRY_SIZE ||
2716 mlx4_log_num_mgm_entry_size > MLX4_MAX_MGM_LOG_ENTRY_SIZE)) {
2717 pr_warning("mlx4_core: mlx4_log_num_mgm_entry_size (%d) not "
2718 "in legal range (-1 or %d..%d)\n",
2719 mlx4_log_num_mgm_entry_size,
2720 MLX4_MIN_MGM_LOG_ENTRY_SIZE,
2721 MLX4_MAX_MGM_LOG_ENTRY_SIZE);
2722 return -1;
2723 }
2724
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002725 return 0;
2726}
2727
Roland Dreier225c7b12007-05-08 18:00:38 -07002728static int __init mlx4_init(void)
2729{
2730 int ret;
2731
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002732 if (mlx4_verify_params())
2733 return -EINVAL;
2734
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -07002735 mlx4_catas_init();
2736
2737 mlx4_wq = create_singlethread_workqueue("mlx4");
2738 if (!mlx4_wq)
2739 return -ENOMEM;
Jack Morgensteinee49bd92007-07-12 17:50:45 +03002740
Roland Dreier225c7b12007-05-08 18:00:38 -07002741 ret = pci_register_driver(&mlx4_driver);
Wei Yang1b85ee02013-12-03 10:04:10 +08002742 if (ret < 0)
2743 destroy_workqueue(mlx4_wq);
Roland Dreier225c7b12007-05-08 18:00:38 -07002744 return ret < 0 ? ret : 0;
2745}
2746
2747static void __exit mlx4_cleanup(void)
2748{
2749 pci_unregister_driver(&mlx4_driver);
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -07002750 destroy_workqueue(mlx4_wq);
Roland Dreier225c7b12007-05-08 18:00:38 -07002751}
2752
2753module_init(mlx4_init);
2754module_exit(mlx4_cleanup);