blob: 48d0e90194cb19d2a322990425b478f9e178b623 [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>
Roland Dreier225c7b12007-05-08 18:00:38 -070045
46#include <linux/mlx4/device.h>
47#include <linux/mlx4/doorbell.h>
48
49#include "mlx4.h"
50#include "fw.h"
51#include "icm.h"
52
53MODULE_AUTHOR("Roland Dreier");
54MODULE_DESCRIPTION("Mellanox ConnectX HCA low-level driver");
55MODULE_LICENSE("Dual BSD/GPL");
56MODULE_VERSION(DRV_VERSION);
57
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -070058struct workqueue_struct *mlx4_wq;
59
Roland Dreier225c7b12007-05-08 18:00:38 -070060#ifdef CONFIG_MLX4_DEBUG
61
62int mlx4_debug_level = 0;
63module_param_named(debug_level, mlx4_debug_level, int, 0644);
64MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
65
66#endif /* CONFIG_MLX4_DEBUG */
67
68#ifdef CONFIG_PCI_MSI
69
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +030070static int msi_x = 1;
Roland Dreier225c7b12007-05-08 18:00:38 -070071module_param(msi_x, int, 0444);
72MODULE_PARM_DESC(msi_x, "attempt to use MSI-X if nonzero");
73
74#else /* CONFIG_PCI_MSI */
75
76#define msi_x (0)
77
78#endif /* CONFIG_PCI_MSI */
79
Jack Morgensteinab9c17a2011-12-13 04:18:30 +000080static int num_vfs;
81module_param(num_vfs, int, 0444);
82MODULE_PARM_DESC(num_vfs, "enable #num_vfs functions if num_vfs > 0");
83
84static int probe_vf;
85module_param(probe_vf, int, 0644);
86MODULE_PARM_DESC(probe_vf, "number of vfs to probe by pf driver (num_vfs > 0)");
87
Eugenia Emantayev0ec2c0f2011-12-13 04:16:02 +000088int mlx4_log_num_mgm_entry_size = 10;
89module_param_named(log_num_mgm_entry_size,
90 mlx4_log_num_mgm_entry_size, int, 0444);
91MODULE_PARM_DESC(log_num_mgm_entry_size, "log mgm size, that defines the num"
92 " of qp per mcg, for example:"
93 " 10 gives 248.range: 9<="
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +000094 " log_num_mgm_entry_size <= 12."
95 " Not in use with device managed"
96 " flow steering");
Eugenia Emantayev0ec2c0f2011-12-13 04:16:02 +000097
Jack Morgensteinab9c17a2011-12-13 04:18:30 +000098#define MLX4_VF (1 << 0)
99
100#define HCA_GLOBAL_CAP_MASK 0
101#define PF_CONTEXT_BEHAVIOUR_MASK 0
102
Roland Dreierf33afc22008-02-04 20:20:44 -0800103static char mlx4_version[] __devinitdata =
Roland Dreier225c7b12007-05-08 18:00:38 -0700104 DRV_NAME ": Mellanox ConnectX core driver v"
105 DRV_VERSION " (" DRV_RELDATE ")\n";
106
107static struct mlx4_profile default_profile = {
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000108 .num_qp = 1 << 18,
Roland Dreier225c7b12007-05-08 18:00:38 -0700109 .num_srq = 1 << 16,
Jack Morgensteinc9f2ba52007-07-17 13:11:43 +0300110 .rdmarc_per_qp = 1 << 4,
Roland Dreier225c7b12007-05-08 18:00:38 -0700111 .num_cq = 1 << 16,
112 .num_mcg = 1 << 13,
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000113 .num_mpt = 1 << 19,
Marcel Apfelbaum9fd7a1e2012-01-19 09:45:31 +0000114 .num_mtt = 1 << 20, /* It is really num mtt segements */
Roland Dreier225c7b12007-05-08 18:00:38 -0700115};
116
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000117static int log_num_mac = 7;
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700118module_param_named(log_num_mac, log_num_mac, int, 0444);
119MODULE_PARM_DESC(log_num_mac, "Log2 max number of MACs per ETH port (1-7)");
120
121static int log_num_vlan;
122module_param_named(log_num_vlan, log_num_vlan, int, 0444);
123MODULE_PARM_DESC(log_num_vlan, "Log2 max number of VLANs per ETH port (0-7)");
Or Gerlitzcb296882011-10-16 10:26:21 +0200124/* Log2 max number of VLANs per ETH port (0-7) */
125#define MLX4_LOG_NUM_VLANS 7
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700126
Rusty Russelleb939922011-12-19 14:08:01 +0000127static bool use_prio;
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700128module_param_named(use_prio, use_prio, bool, 0444);
129MODULE_PARM_DESC(use_prio, "Enable steering by VLAN priority on ETH ports "
130 "(0/1, default 0)");
131
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000132int log_mtts_per_seg = ilog2(MLX4_MTT_ENTRY_PER_SEG);
Eli Cohenab6bf422009-05-27 14:38:34 -0700133module_param_named(log_mtts_per_seg, log_mtts_per_seg, int, 0444);
Eli Cohen04986282010-09-20 08:42:38 +0200134MODULE_PARM_DESC(log_mtts_per_seg, "Log2 number of MTT entries per segment (1-7)");
Eli Cohenab6bf422009-05-27 14:38:34 -0700135
Yevgeny Petrilin8d0fc7b2011-12-19 04:00:34 +0000136static int port_type_array[2] = {MLX4_PORT_TYPE_NONE, MLX4_PORT_TYPE_NONE};
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000137static int arr_argc = 2;
138module_param_array(port_type_array, int, &arr_argc, 0444);
Yevgeny Petrilin8d0fc7b2011-12-19 04:00:34 +0000139MODULE_PARM_DESC(port_type_array, "Array of port types: HW_DEFAULT (0) is default "
140 "1 for IB, 2 for Ethernet");
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000141
142struct mlx4_port_config {
143 struct list_head list;
144 enum mlx4_port_type port_type[MLX4_MAX_PORTS + 1];
145 struct pci_dev *pdev;
146};
147
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700148int mlx4_check_port_params(struct mlx4_dev *dev,
149 enum mlx4_port_type *port_type)
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700150{
151 int i;
152
153 for (i = 0; i < dev->caps.num_ports - 1; i++) {
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700154 if (port_type[i] != port_type[i + 1]) {
155 if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP)) {
156 mlx4_err(dev, "Only same port types supported "
157 "on this HCA, aborting.\n");
158 return -EINVAL;
159 }
160 if (port_type[i] == MLX4_PORT_TYPE_ETH &&
161 port_type[i + 1] == MLX4_PORT_TYPE_IB)
162 return -EINVAL;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700163 }
164 }
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700165
166 for (i = 0; i < dev->caps.num_ports; i++) {
167 if (!(port_type[i] & dev->caps.supported_type[i+1])) {
168 mlx4_err(dev, "Requested port type for port %d is not "
169 "supported on this HCA\n", i + 1);
170 return -EINVAL;
171 }
172 }
173 return 0;
174}
175
176static void mlx4_set_port_mask(struct mlx4_dev *dev)
177{
178 int i;
179
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700180 for (i = 1; i <= dev->caps.num_ports; ++i)
Jack Morgenstein65dab252011-12-13 04:10:41 +0000181 dev->caps.port_mask[i] = dev->caps.port_type[i];
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700182}
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +0000183
Roland Dreier3d73c282007-10-10 15:43:54 -0700184static int mlx4_dev_cap(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap)
Roland Dreier225c7b12007-05-08 18:00:38 -0700185{
186 int err;
Roland Dreier5ae2a7a2007-06-18 08:15:02 -0700187 int i;
Roland Dreier225c7b12007-05-08 18:00:38 -0700188
189 err = mlx4_QUERY_DEV_CAP(dev, dev_cap);
190 if (err) {
191 mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
192 return err;
193 }
194
195 if (dev_cap->min_page_sz > PAGE_SIZE) {
196 mlx4_err(dev, "HCA minimum page size of %d bigger than "
197 "kernel PAGE_SIZE of %ld, aborting.\n",
198 dev_cap->min_page_sz, PAGE_SIZE);
199 return -ENODEV;
200 }
201 if (dev_cap->num_ports > MLX4_MAX_PORTS) {
202 mlx4_err(dev, "HCA has %d ports, but we only support %d, "
203 "aborting.\n",
204 dev_cap->num_ports, MLX4_MAX_PORTS);
205 return -ENODEV;
206 }
207
208 if (dev_cap->uar_size > pci_resource_len(dev->pdev, 2)) {
209 mlx4_err(dev, "HCA reported UAR size of 0x%x bigger than "
210 "PCI resource 2 size of 0x%llx, aborting.\n",
211 dev_cap->uar_size,
212 (unsigned long long) pci_resource_len(dev->pdev, 2));
213 return -ENODEV;
214 }
215
216 dev->caps.num_ports = dev_cap->num_ports;
Marcel Apfelbaum3fc929e2012-05-30 09:14:51 +0000217 dev->phys_caps.num_phys_eqs = MLX4_MAX_EQ_NUM;
Roland Dreier5ae2a7a2007-06-18 08:15:02 -0700218 for (i = 1; i <= dev->caps.num_ports; ++i) {
219 dev->caps.vl_cap[i] = dev_cap->max_vl[i];
Yevgeny Petrilinb79acb42008-10-22 10:56:48 -0700220 dev->caps.ib_mtu_cap[i] = dev_cap->ib_mtu[i];
Jack Morgenstein66349612012-06-19 11:21:44 +0300221 dev->phys_caps.gid_phys_table_len[i] = dev_cap->max_gids[i];
222 dev->phys_caps.pkey_phys_table_len[i] = dev_cap->max_pkeys[i];
223 /* set gid and pkey table operating lengths by default
224 * to non-sriov values */
Roland Dreier5ae2a7a2007-06-18 08:15:02 -0700225 dev->caps.gid_table_len[i] = dev_cap->max_gids[i];
226 dev->caps.pkey_table_len[i] = dev_cap->max_pkeys[i];
227 dev->caps.port_width_cap[i] = dev_cap->max_port_width[i];
Yevgeny Petrilinb79acb42008-10-22 10:56:48 -0700228 dev->caps.eth_mtu_cap[i] = dev_cap->eth_mtu[i];
229 dev->caps.def_mac[i] = dev_cap->def_mac[i];
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700230 dev->caps.supported_type[i] = dev_cap->supported_port_types[i];
Yevgeny Petrilin8d0fc7b2011-12-19 04:00:34 +0000231 dev->caps.suggested_type[i] = dev_cap->suggested_type[i];
232 dev->caps.default_sense[i] = dev_cap->default_sense[i];
Yevgeny Petrilin76995172010-08-24 03:46:23 +0000233 dev->caps.trans_type[i] = dev_cap->trans_type[i];
234 dev->caps.vendor_oui[i] = dev_cap->vendor_oui[i];
235 dev->caps.wavelength[i] = dev_cap->wavelength[i];
236 dev->caps.trans_code[i] = dev_cap->trans_code[i];
Roland Dreier5ae2a7a2007-06-18 08:15:02 -0700237 }
238
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000239 dev->caps.uar_page_size = PAGE_SIZE;
Roland Dreier225c7b12007-05-08 18:00:38 -0700240 dev->caps.num_uars = dev_cap->uar_size / PAGE_SIZE;
Roland Dreier225c7b12007-05-08 18:00:38 -0700241 dev->caps.local_ca_ack_delay = dev_cap->local_ca_ack_delay;
242 dev->caps.bf_reg_size = dev_cap->bf_reg_size;
243 dev->caps.bf_regs_per_page = dev_cap->bf_regs_per_page;
244 dev->caps.max_sq_sg = dev_cap->max_sq_sg;
245 dev->caps.max_rq_sg = dev_cap->max_rq_sg;
246 dev->caps.max_wqes = dev_cap->max_qp_sz;
247 dev->caps.max_qp_init_rdma = dev_cap->max_requester_per_qp;
Roland Dreier225c7b12007-05-08 18:00:38 -0700248 dev->caps.max_srq_wqes = dev_cap->max_srq_sz;
249 dev->caps.max_srq_sge = dev_cap->max_rq_sg - 1;
250 dev->caps.reserved_srqs = dev_cap->reserved_srqs;
251 dev->caps.max_sq_desc_sz = dev_cap->max_sq_desc_sz;
252 dev->caps.max_rq_desc_sz = dev_cap->max_rq_desc_sz;
Roland Dreier225c7b12007-05-08 18:00:38 -0700253 /*
254 * Subtract 1 from the limit because we need to allocate a
255 * spare CQE so the HCA HW can tell the difference between an
256 * empty CQ and a full CQ.
257 */
258 dev->caps.max_cqes = dev_cap->max_cq_sz - 1;
259 dev->caps.reserved_cqs = dev_cap->reserved_cqs;
260 dev->caps.reserved_eqs = dev_cap->reserved_eqs;
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000261 dev->caps.reserved_mtts = dev_cap->reserved_mtts;
Roland Dreier225c7b12007-05-08 18:00:38 -0700262 dev->caps.reserved_mrws = dev_cap->reserved_mrws;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000263
264 /* The first 128 UARs are used for EQ doorbells */
265 dev->caps.reserved_uars = max_t(int, 128, dev_cap->reserved_uars);
Roland Dreier225c7b12007-05-08 18:00:38 -0700266 dev->caps.reserved_pds = dev_cap->reserved_pds;
Sean Hefty012a8ff2011-06-02 09:01:33 -0700267 dev->caps.reserved_xrcds = (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) ?
268 dev_cap->reserved_xrcds : 0;
269 dev->caps.max_xrcds = (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) ?
270 dev_cap->max_xrcds : 0;
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000271 dev->caps.mtt_entry_sz = dev_cap->mtt_entry_sz;
272
Dotan Barak149983af2007-06-26 15:55:28 +0300273 dev->caps.max_msg_sz = dev_cap->max_msg_sz;
Roland Dreier225c7b12007-05-08 18:00:38 -0700274 dev->caps.page_size_cap = ~(u32) (dev_cap->min_page_sz - 1);
275 dev->caps.flags = dev_cap->flags;
Shlomo Pongratzb3416f42012-04-29 17:04:25 +0300276 dev->caps.flags2 = dev_cap->flags2;
Roland Dreier95d04f02008-07-23 08:12:26 -0700277 dev->caps.bmme_flags = dev_cap->bmme_flags;
278 dev->caps.reserved_lkey = dev_cap->reserved_lkey;
Roland Dreier225c7b12007-05-08 18:00:38 -0700279 dev->caps.stat_rate_support = dev_cap->stat_rate_support;
Eli Cohenb832be12008-04-16 21:09:27 -0700280 dev->caps.max_gso_sz = dev_cap->max_gso_sz;
Shlomo Pongratzb3416f42012-04-29 17:04:25 +0300281 dev->caps.max_rss_tbl_sz = dev_cap->max_rss_tbl_sz;
Roland Dreier225c7b12007-05-08 18:00:38 -0700282
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +0000283 if (dev_cap->flags2 & MLX4_DEV_CAP_FLAG2_FS_EN) {
284 dev->caps.steering_mode = MLX4_STEERING_MODE_DEVICE_MANAGED;
285 dev->caps.num_qp_per_mgm = dev_cap->fs_max_num_qp_per_entry;
286 dev->caps.fs_log_max_ucast_qp_range_size =
287 dev_cap->fs_log_max_ucast_qp_range_size;
Hadar Hen Zionc96d97f2012-07-05 04:03:44 +0000288 } else {
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +0000289 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER &&
290 dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER) {
291 dev->caps.steering_mode = MLX4_STEERING_MODE_B0;
292 } else {
293 dev->caps.steering_mode = MLX4_STEERING_MODE_A0;
Hadar Hen Zionc96d97f2012-07-05 04:03:44 +0000294
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +0000295 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER ||
296 dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER)
297 mlx4_warn(dev, "Must have UC_STEER and MC_STEER flags "
298 "set to use B0 steering. Falling back to A0 steering mode.\n");
299 }
300 dev->caps.num_qp_per_mgm = mlx4_get_qp_per_mgm(dev);
Hadar Hen Zionc96d97f2012-07-05 04:03:44 +0000301 }
302 mlx4_dbg(dev, "Steering mode is: %s\n",
303 mlx4_steering_mode_str(dev->caps.steering_mode));
Hadar Hen Zionc96d97f2012-07-05 04:03:44 +0000304
Yevgeny Petrilin58a60162011-12-19 04:00:26 +0000305 /* Sense port always allowed on supported devices for ConnectX1 and 2 */
306 if (dev->pdev->device != 0x1003)
307 dev->caps.flags |= MLX4_DEV_CAP_FLAG_SENSE_SUPPORT;
308
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700309 dev->caps.log_num_macs = log_num_mac;
Or Gerlitzcb296882011-10-16 10:26:21 +0200310 dev->caps.log_num_vlans = MLX4_LOG_NUM_VLANS;
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700311 dev->caps.log_num_prios = use_prio ? 3 : 0;
312
313 for (i = 1; i <= dev->caps.num_ports; ++i) {
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000314 dev->caps.port_type[i] = MLX4_PORT_TYPE_NONE;
315 if (dev->caps.supported_type[i]) {
316 /* if only ETH is supported - assign ETH */
317 if (dev->caps.supported_type[i] == MLX4_PORT_TYPE_ETH)
318 dev->caps.port_type[i] = MLX4_PORT_TYPE_ETH;
Jack Morgenstein105c3202012-06-19 11:21:43 +0300319 /* if only IB is supported, assign IB */
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000320 else if (dev->caps.supported_type[i] ==
Jack Morgenstein105c3202012-06-19 11:21:43 +0300321 MLX4_PORT_TYPE_IB)
322 dev->caps.port_type[i] = MLX4_PORT_TYPE_IB;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000323 else {
Jack Morgenstein105c3202012-06-19 11:21:43 +0300324 /* if IB and ETH are supported, we set the port
325 * type according to user selection of port type;
326 * if user selected none, take the FW hint */
327 if (port_type_array[i - 1] == MLX4_PORT_TYPE_NONE)
Yevgeny Petrilin8d0fc7b2011-12-19 04:00:34 +0000328 dev->caps.port_type[i] = dev->caps.suggested_type[i] ?
329 MLX4_PORT_TYPE_ETH : MLX4_PORT_TYPE_IB;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000330 else
Jack Morgenstein105c3202012-06-19 11:21:43 +0300331 dev->caps.port_type[i] = port_type_array[i - 1];
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000332 }
333 }
Yevgeny Petrilin8d0fc7b2011-12-19 04:00:34 +0000334 /*
335 * Link sensing is allowed on the port if 3 conditions are true:
336 * 1. Both protocols are supported on the port.
337 * 2. Different types are supported on the port
338 * 3. FW declared that it supports link sensing
339 */
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700340 mlx4_priv(dev)->sense.sense_allowed[i] =
Yevgeny Petrilin58a60162011-12-19 04:00:26 +0000341 ((dev->caps.supported_type[i] == MLX4_PORT_TYPE_AUTO) &&
Yevgeny Petrilin8d0fc7b2011-12-19 04:00:34 +0000342 (dev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP) &&
Yevgeny Petrilin58a60162011-12-19 04:00:26 +0000343 (dev->caps.flags & MLX4_DEV_CAP_FLAG_SENSE_SUPPORT));
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700344
Yevgeny Petrilin8d0fc7b2011-12-19 04:00:34 +0000345 /*
346 * If "default_sense" bit is set, we move the port to "AUTO" mode
347 * and perform sense_port FW command to try and set the correct
348 * port type from beginning
349 */
Yevgeny Petrilin46c46742011-12-29 07:42:34 +0000350 if (mlx4_priv(dev)->sense.sense_allowed[i] && dev->caps.default_sense[i]) {
Yevgeny Petrilin8d0fc7b2011-12-19 04:00:34 +0000351 enum mlx4_port_type sensed_port = MLX4_PORT_TYPE_NONE;
352 dev->caps.possible_type[i] = MLX4_PORT_TYPE_AUTO;
353 mlx4_SENSE_PORT(dev, i, &sensed_port);
354 if (sensed_port != MLX4_PORT_TYPE_NONE)
355 dev->caps.port_type[i] = sensed_port;
356 } else {
357 dev->caps.possible_type[i] = dev->caps.port_type[i];
358 }
359
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700360 if (dev->caps.log_num_macs > dev_cap->log_max_macs[i]) {
361 dev->caps.log_num_macs = dev_cap->log_max_macs[i];
362 mlx4_warn(dev, "Requested number of MACs is too much "
363 "for port %d, reducing to %d.\n",
364 i, 1 << dev->caps.log_num_macs);
365 }
366 if (dev->caps.log_num_vlans > dev_cap->log_max_vlans[i]) {
367 dev->caps.log_num_vlans = dev_cap->log_max_vlans[i];
368 mlx4_warn(dev, "Requested number of VLANs is too much "
369 "for port %d, reducing to %d.\n",
370 i, 1 << dev->caps.log_num_vlans);
371 }
372 }
373
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +0000374 dev->caps.max_counters = 1 << ilog2(dev_cap->max_counters);
375
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700376 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW] = dev_cap->reserved_qps;
377 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_ETH_ADDR] =
378 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_ADDR] =
379 (1 << dev->caps.log_num_macs) *
380 (1 << dev->caps.log_num_vlans) *
381 (1 << dev->caps.log_num_prios) *
382 dev->caps.num_ports;
383 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_EXCH] = MLX4_NUM_FEXCH;
384
385 dev->caps.reserved_qps = dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW] +
386 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_ETH_ADDR] +
387 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_ADDR] +
388 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FC_EXCH];
389
Roland Dreier225c7b12007-05-08 18:00:38 -0700390 return 0;
391}
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000392/*The function checks if there are live vf, return the num of them*/
393static int mlx4_how_many_lives_vf(struct mlx4_dev *dev)
394{
395 struct mlx4_priv *priv = mlx4_priv(dev);
396 struct mlx4_slave_state *s_state;
397 int i;
398 int ret = 0;
399
400 for (i = 1/*the ppf is 0*/; i < dev->num_slaves; ++i) {
401 s_state = &priv->mfunc.master.slave_state[i];
402 if (s_state->active && s_state->last_cmd !=
403 MLX4_COMM_CMD_RESET) {
404 mlx4_warn(dev, "%s: slave: %d is still active\n",
405 __func__, i);
406 ret++;
407 }
408 }
409 return ret;
410}
411
Jack Morgenstein396f2fe2012-06-19 11:21:42 +0300412int mlx4_get_parav_qkey(struct mlx4_dev *dev, u32 qpn, u32 *qkey)
413{
414 u32 qk = MLX4_RESERVED_QKEY_BASE;
415 if (qpn >= dev->caps.base_tunnel_sqpn + 8 * MLX4_MFUNC_MAX ||
416 qpn < dev->caps.sqp_start)
417 return -EINVAL;
418
419 if (qpn >= dev->caps.base_tunnel_sqpn)
420 /* tunnel qp */
421 qk += qpn - dev->caps.base_tunnel_sqpn;
422 else
423 qk += qpn - dev->caps.sqp_start;
424 *qkey = qk;
425 return 0;
426}
427EXPORT_SYMBOL(mlx4_get_parav_qkey);
428
Roland Dreiere10903b2012-02-26 01:48:12 -0800429int mlx4_is_slave_active(struct mlx4_dev *dev, int slave)
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000430{
431 struct mlx4_priv *priv = mlx4_priv(dev);
432 struct mlx4_slave_state *s_slave;
433
434 if (!mlx4_is_master(dev))
435 return 0;
436
437 s_slave = &priv->mfunc.master.slave_state[slave];
438 return !!s_slave->active;
439}
440EXPORT_SYMBOL(mlx4_is_slave_active);
441
442static int mlx4_slave_cap(struct mlx4_dev *dev)
443{
444 int err;
445 u32 page_size;
446 struct mlx4_dev_cap dev_cap;
447 struct mlx4_func_cap func_cap;
448 struct mlx4_init_hca_param hca_param;
449 int i;
450
451 memset(&hca_param, 0, sizeof(hca_param));
452 err = mlx4_QUERY_HCA(dev, &hca_param);
453 if (err) {
454 mlx4_err(dev, "QUERY_HCA command failed, aborting.\n");
455 return err;
456 }
457
458 /*fail if the hca has an unknown capability */
459 if ((hca_param.global_caps | HCA_GLOBAL_CAP_MASK) !=
460 HCA_GLOBAL_CAP_MASK) {
461 mlx4_err(dev, "Unknown hca global capabilities\n");
462 return -ENOSYS;
463 }
464
465 mlx4_log_num_mgm_entry_size = hca_param.log_mc_entry_sz;
466
467 memset(&dev_cap, 0, sizeof(dev_cap));
Jack Morgensteinb91cb3e2012-05-30 09:14:53 +0000468 dev->caps.max_qp_dest_rdma = 1 << hca_param.log_rd_per_qp;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000469 err = mlx4_dev_cap(dev, &dev_cap);
470 if (err) {
471 mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
472 return err;
473 }
474
Jack Morgensteinb91cb3e2012-05-30 09:14:53 +0000475 err = mlx4_QUERY_FW(dev);
476 if (err)
477 mlx4_err(dev, "QUERY_FW command failed: could not get FW version.\n");
478
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000479 page_size = ~dev->caps.page_size_cap + 1;
480 mlx4_warn(dev, "HCA minimum page size:%d\n", page_size);
481 if (page_size > PAGE_SIZE) {
482 mlx4_err(dev, "HCA minimum page size of %d bigger than "
483 "kernel PAGE_SIZE of %ld, aborting.\n",
484 page_size, PAGE_SIZE);
485 return -ENODEV;
486 }
487
488 /* slave gets uar page size from QUERY_HCA fw command */
489 dev->caps.uar_page_size = 1 << (hca_param.uar_page_sz + 12);
490
491 /* TODO: relax this assumption */
492 if (dev->caps.uar_page_size != PAGE_SIZE) {
493 mlx4_err(dev, "UAR size:%d != kernel PAGE_SIZE of %ld\n",
494 dev->caps.uar_page_size, PAGE_SIZE);
495 return -ENODEV;
496 }
497
498 memset(&func_cap, 0, sizeof(func_cap));
499 err = mlx4_QUERY_FUNC_CAP(dev, &func_cap);
500 if (err) {
501 mlx4_err(dev, "QUERY_FUNC_CAP command failed, aborting.\n");
502 return err;
503 }
504
505 if ((func_cap.pf_context_behaviour | PF_CONTEXT_BEHAVIOUR_MASK) !=
506 PF_CONTEXT_BEHAVIOUR_MASK) {
507 mlx4_err(dev, "Unknown pf context behaviour\n");
508 return -ENOSYS;
509 }
510
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000511 dev->caps.num_ports = func_cap.num_ports;
512 dev->caps.num_qps = func_cap.qp_quota;
513 dev->caps.num_srqs = func_cap.srq_quota;
514 dev->caps.num_cqs = func_cap.cq_quota;
515 dev->caps.num_eqs = func_cap.max_eq;
516 dev->caps.reserved_eqs = func_cap.reserved_eq;
517 dev->caps.num_mpts = func_cap.mpt_quota;
518 dev->caps.num_mtts = func_cap.mtt_quota;
519 dev->caps.num_pds = MLX4_NUM_PDS;
520 dev->caps.num_mgms = 0;
521 dev->caps.num_amgms = 0;
522
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000523 if (dev->caps.num_ports > MLX4_MAX_PORTS) {
524 mlx4_err(dev, "HCA has %d ports, but we only support %d, "
525 "aborting.\n", dev->caps.num_ports, MLX4_MAX_PORTS);
526 return -ENODEV;
527 }
528
Jack Morgenstein66349612012-06-19 11:21:44 +0300529 for (i = 1; i <= dev->caps.num_ports; ++i) {
Jack Morgenstein6230bb22012-05-30 09:14:54 +0000530 dev->caps.port_mask[i] = dev->caps.port_type[i];
Jack Morgenstein66349612012-06-19 11:21:44 +0300531 if (mlx4_get_slave_pkey_gid_tbl_len(dev, i,
532 &dev->caps.gid_table_len[i],
533 &dev->caps.pkey_table_len[i]))
534 return -ENODEV;
535 }
Jack Morgenstein6230bb22012-05-30 09:14:54 +0000536
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000537 if (dev->caps.uar_page_size * (dev->caps.num_uars -
538 dev->caps.reserved_uars) >
539 pci_resource_len(dev->pdev, 2)) {
540 mlx4_err(dev, "HCA reported UAR region size of 0x%x bigger than "
541 "PCI resource 2 size of 0x%llx, aborting.\n",
542 dev->caps.uar_page_size * dev->caps.num_uars,
543 (unsigned long long) pci_resource_len(dev->pdev, 2));
544 return -ENODEV;
545 }
546
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000547 return 0;
548}
Roland Dreier225c7b12007-05-08 18:00:38 -0700549
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700550/*
551 * Change the port configuration of the device.
552 * Every user of this function must hold the port mutex.
553 */
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700554int mlx4_change_port_types(struct mlx4_dev *dev,
555 enum mlx4_port_type *port_types)
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700556{
557 int err = 0;
558 int change = 0;
559 int port;
560
561 for (port = 0; port < dev->caps.num_ports; port++) {
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700562 /* Change the port type only if the new type is different
563 * from the current, and not set to Auto */
Yevgeny Petrilin3d8f9302012-02-21 03:41:07 +0000564 if (port_types[port] != dev->caps.port_type[port + 1])
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700565 change = 1;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700566 }
567 if (change) {
568 mlx4_unregister_device(dev);
569 for (port = 1; port <= dev->caps.num_ports; port++) {
570 mlx4_CLOSE_PORT(dev, port);
Yevgeny Petrilin1e0f03d2012-02-23 07:04:35 +0000571 dev->caps.port_type[port] = port_types[port - 1];
Jack Morgenstein66349612012-06-19 11:21:44 +0300572 err = mlx4_SET_PORT(dev, port, -1);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700573 if (err) {
574 mlx4_err(dev, "Failed to set port %d, "
575 "aborting\n", port);
576 goto out;
577 }
578 }
579 mlx4_set_port_mask(dev);
580 err = mlx4_register_device(dev);
581 }
582
583out:
584 return err;
585}
586
587static ssize_t show_port_type(struct device *dev,
588 struct device_attribute *attr,
589 char *buf)
590{
591 struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
592 port_attr);
593 struct mlx4_dev *mdev = info->dev;
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700594 char type[8];
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700595
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700596 sprintf(type, "%s",
597 (mdev->caps.port_type[info->port] == MLX4_PORT_TYPE_IB) ?
598 "ib" : "eth");
599 if (mdev->caps.possible_type[info->port] == MLX4_PORT_TYPE_AUTO)
600 sprintf(buf, "auto (%s)\n", type);
601 else
602 sprintf(buf, "%s\n", type);
603
604 return strlen(buf);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700605}
606
607static ssize_t set_port_type(struct device *dev,
608 struct device_attribute *attr,
609 const char *buf, size_t count)
610{
611 struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
612 port_attr);
613 struct mlx4_dev *mdev = info->dev;
614 struct mlx4_priv *priv = mlx4_priv(mdev);
615 enum mlx4_port_type types[MLX4_MAX_PORTS];
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700616 enum mlx4_port_type new_types[MLX4_MAX_PORTS];
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700617 int i;
618 int err = 0;
619
620 if (!strcmp(buf, "ib\n"))
621 info->tmp_type = MLX4_PORT_TYPE_IB;
622 else if (!strcmp(buf, "eth\n"))
623 info->tmp_type = MLX4_PORT_TYPE_ETH;
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700624 else if (!strcmp(buf, "auto\n"))
625 info->tmp_type = MLX4_PORT_TYPE_AUTO;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700626 else {
627 mlx4_err(mdev, "%s is not supported port type\n", buf);
628 return -EINVAL;
629 }
630
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700631 mlx4_stop_sense(mdev);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700632 mutex_lock(&priv->port_mutex);
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700633 /* Possible type is always the one that was delivered */
634 mdev->caps.possible_type[info->port] = info->tmp_type;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700635
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700636 for (i = 0; i < mdev->caps.num_ports; i++) {
637 types[i] = priv->port[i+1].tmp_type ? priv->port[i+1].tmp_type :
638 mdev->caps.possible_type[i+1];
639 if (types[i] == MLX4_PORT_TYPE_AUTO)
640 types[i] = mdev->caps.port_type[i+1];
641 }
642
Yevgeny Petrilin58a60162011-12-19 04:00:26 +0000643 if (!(mdev->caps.flags & MLX4_DEV_CAP_FLAG_DPDP) &&
644 !(mdev->caps.flags & MLX4_DEV_CAP_FLAG_SENSE_SUPPORT)) {
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700645 for (i = 1; i <= mdev->caps.num_ports; i++) {
646 if (mdev->caps.possible_type[i] == MLX4_PORT_TYPE_AUTO) {
647 mdev->caps.possible_type[i] = mdev->caps.port_type[i];
648 err = -EINVAL;
649 }
650 }
651 }
652 if (err) {
653 mlx4_err(mdev, "Auto sensing is not supported on this HCA. "
654 "Set only 'eth' or 'ib' for both ports "
655 "(should be the same)\n");
656 goto out;
657 }
658
659 mlx4_do_sense_ports(mdev, new_types, types);
660
661 err = mlx4_check_port_params(mdev, new_types);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700662 if (err)
663 goto out;
664
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700665 /* We are about to apply the changes after the configuration
666 * was verified, no need to remember the temporary types
667 * any more */
668 for (i = 0; i < mdev->caps.num_ports; i++)
669 priv->port[i + 1].tmp_type = 0;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700670
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700671 err = mlx4_change_port_types(mdev, new_types);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700672
673out:
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -0700674 mlx4_start_sense(mdev);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700675 mutex_unlock(&priv->port_mutex);
676 return err ? err : count;
677}
678
Or Gerlitz096335b2012-01-11 19:02:17 +0200679enum ibta_mtu {
680 IB_MTU_256 = 1,
681 IB_MTU_512 = 2,
682 IB_MTU_1024 = 3,
683 IB_MTU_2048 = 4,
684 IB_MTU_4096 = 5
685};
686
687static inline int int_to_ibta_mtu(int mtu)
688{
689 switch (mtu) {
690 case 256: return IB_MTU_256;
691 case 512: return IB_MTU_512;
692 case 1024: return IB_MTU_1024;
693 case 2048: return IB_MTU_2048;
694 case 4096: return IB_MTU_4096;
695 default: return -1;
696 }
697}
698
699static inline int ibta_mtu_to_int(enum ibta_mtu mtu)
700{
701 switch (mtu) {
702 case IB_MTU_256: return 256;
703 case IB_MTU_512: return 512;
704 case IB_MTU_1024: return 1024;
705 case IB_MTU_2048: return 2048;
706 case IB_MTU_4096: return 4096;
707 default: return -1;
708 }
709}
710
711static ssize_t show_port_ib_mtu(struct device *dev,
712 struct device_attribute *attr,
713 char *buf)
714{
715 struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
716 port_mtu_attr);
717 struct mlx4_dev *mdev = info->dev;
718
719 if (mdev->caps.port_type[info->port] == MLX4_PORT_TYPE_ETH)
720 mlx4_warn(mdev, "port level mtu is only used for IB ports\n");
721
722 sprintf(buf, "%d\n",
723 ibta_mtu_to_int(mdev->caps.port_ib_mtu[info->port]));
724 return strlen(buf);
725}
726
727static ssize_t set_port_ib_mtu(struct device *dev,
728 struct device_attribute *attr,
729 const char *buf, size_t count)
730{
731 struct mlx4_port_info *info = container_of(attr, struct mlx4_port_info,
732 port_mtu_attr);
733 struct mlx4_dev *mdev = info->dev;
734 struct mlx4_priv *priv = mlx4_priv(mdev);
735 int err, port, mtu, ibta_mtu = -1;
736
737 if (mdev->caps.port_type[info->port] == MLX4_PORT_TYPE_ETH) {
738 mlx4_warn(mdev, "port level mtu is only used for IB ports\n");
739 return -EINVAL;
740 }
741
742 err = sscanf(buf, "%d", &mtu);
743 if (err > 0)
744 ibta_mtu = int_to_ibta_mtu(mtu);
745
746 if (err <= 0 || ibta_mtu < 0) {
747 mlx4_err(mdev, "%s is invalid IBTA mtu\n", buf);
748 return -EINVAL;
749 }
750
751 mdev->caps.port_ib_mtu[info->port] = ibta_mtu;
752
753 mlx4_stop_sense(mdev);
754 mutex_lock(&priv->port_mutex);
755 mlx4_unregister_device(mdev);
756 for (port = 1; port <= mdev->caps.num_ports; port++) {
757 mlx4_CLOSE_PORT(mdev, port);
Jack Morgenstein66349612012-06-19 11:21:44 +0300758 err = mlx4_SET_PORT(mdev, port, -1);
Or Gerlitz096335b2012-01-11 19:02:17 +0200759 if (err) {
760 mlx4_err(mdev, "Failed to set port %d, "
761 "aborting\n", port);
762 goto err_set_port;
763 }
764 }
765 err = mlx4_register_device(mdev);
766err_set_port:
767 mutex_unlock(&priv->port_mutex);
768 mlx4_start_sense(mdev);
769 return err ? err : count;
770}
771
Roland Dreiere8f9b2e2008-02-04 20:20:41 -0800772static int mlx4_load_fw(struct mlx4_dev *dev)
Roland Dreier225c7b12007-05-08 18:00:38 -0700773{
774 struct mlx4_priv *priv = mlx4_priv(dev);
775 int err;
776
777 priv->fw.fw_icm = mlx4_alloc_icm(dev, priv->fw.fw_pages,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300778 GFP_HIGHUSER | __GFP_NOWARN, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700779 if (!priv->fw.fw_icm) {
780 mlx4_err(dev, "Couldn't allocate FW area, aborting.\n");
781 return -ENOMEM;
782 }
783
784 err = mlx4_MAP_FA(dev, priv->fw.fw_icm);
785 if (err) {
786 mlx4_err(dev, "MAP_FA command failed, aborting.\n");
787 goto err_free;
788 }
789
790 err = mlx4_RUN_FW(dev);
791 if (err) {
792 mlx4_err(dev, "RUN_FW command failed, aborting.\n");
793 goto err_unmap_fa;
794 }
795
796 return 0;
797
798err_unmap_fa:
799 mlx4_UNMAP_FA(dev);
800
801err_free:
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300802 mlx4_free_icm(dev, priv->fw.fw_icm, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700803 return err;
804}
805
Roland Dreiere8f9b2e2008-02-04 20:20:41 -0800806static int mlx4_init_cmpt_table(struct mlx4_dev *dev, u64 cmpt_base,
807 int cmpt_entry_sz)
Roland Dreier225c7b12007-05-08 18:00:38 -0700808{
809 struct mlx4_priv *priv = mlx4_priv(dev);
810 int err;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000811 int num_eqs;
Roland Dreier225c7b12007-05-08 18:00:38 -0700812
813 err = mlx4_init_icm_table(dev, &priv->qp_table.cmpt_table,
814 cmpt_base +
815 ((u64) (MLX4_CMPT_TYPE_QP *
816 cmpt_entry_sz) << MLX4_CMPT_SHIFT),
817 cmpt_entry_sz, dev->caps.num_qps,
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700818 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
819 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700820 if (err)
821 goto err;
822
823 err = mlx4_init_icm_table(dev, &priv->srq_table.cmpt_table,
824 cmpt_base +
825 ((u64) (MLX4_CMPT_TYPE_SRQ *
826 cmpt_entry_sz) << MLX4_CMPT_SHIFT),
827 cmpt_entry_sz, dev->caps.num_srqs,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300828 dev->caps.reserved_srqs, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700829 if (err)
830 goto err_qp;
831
832 err = mlx4_init_icm_table(dev, &priv->cq_table.cmpt_table,
833 cmpt_base +
834 ((u64) (MLX4_CMPT_TYPE_CQ *
835 cmpt_entry_sz) << MLX4_CMPT_SHIFT),
836 cmpt_entry_sz, dev->caps.num_cqs,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300837 dev->caps.reserved_cqs, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700838 if (err)
839 goto err_srq;
840
Marcel Apfelbaum3fc929e2012-05-30 09:14:51 +0000841 num_eqs = (mlx4_is_master(dev)) ? dev->phys_caps.num_phys_eqs :
842 dev->caps.num_eqs;
Roland Dreier225c7b12007-05-08 18:00:38 -0700843 err = mlx4_init_icm_table(dev, &priv->eq_table.cmpt_table,
844 cmpt_base +
845 ((u64) (MLX4_CMPT_TYPE_EQ *
846 cmpt_entry_sz) << MLX4_CMPT_SHIFT),
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000847 cmpt_entry_sz, num_eqs, num_eqs, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700848 if (err)
849 goto err_cq;
850
851 return 0;
852
853err_cq:
854 mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
855
856err_srq:
857 mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
858
859err_qp:
860 mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
861
862err:
863 return err;
864}
865
Roland Dreier3d73c282007-10-10 15:43:54 -0700866static int mlx4_init_icm(struct mlx4_dev *dev, struct mlx4_dev_cap *dev_cap,
867 struct mlx4_init_hca_param *init_hca, u64 icm_size)
Roland Dreier225c7b12007-05-08 18:00:38 -0700868{
869 struct mlx4_priv *priv = mlx4_priv(dev);
870 u64 aux_pages;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000871 int num_eqs;
Roland Dreier225c7b12007-05-08 18:00:38 -0700872 int err;
873
874 err = mlx4_SET_ICM_SIZE(dev, icm_size, &aux_pages);
875 if (err) {
876 mlx4_err(dev, "SET_ICM_SIZE command failed, aborting.\n");
877 return err;
878 }
879
880 mlx4_dbg(dev, "%lld KB of HCA context requires %lld KB aux memory.\n",
881 (unsigned long long) icm_size >> 10,
882 (unsigned long long) aux_pages << 2);
883
884 priv->fw.aux_icm = mlx4_alloc_icm(dev, aux_pages,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300885 GFP_HIGHUSER | __GFP_NOWARN, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700886 if (!priv->fw.aux_icm) {
887 mlx4_err(dev, "Couldn't allocate aux memory, aborting.\n");
888 return -ENOMEM;
889 }
890
891 err = mlx4_MAP_ICM_AUX(dev, priv->fw.aux_icm);
892 if (err) {
893 mlx4_err(dev, "MAP_ICM_AUX command failed, aborting.\n");
894 goto err_free_aux;
895 }
896
897 err = mlx4_init_cmpt_table(dev, init_hca->cmpt_base, dev_cap->cmpt_entry_sz);
898 if (err) {
899 mlx4_err(dev, "Failed to map cMPT context memory, aborting.\n");
900 goto err_unmap_aux;
901 }
902
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000903
Marcel Apfelbaum3fc929e2012-05-30 09:14:51 +0000904 num_eqs = (mlx4_is_master(dev)) ? dev->phys_caps.num_phys_eqs :
905 dev->caps.num_eqs;
Roland Dreierfa0681d2009-09-05 20:24:49 -0700906 err = mlx4_init_icm_table(dev, &priv->eq_table.table,
907 init_hca->eqc_base, dev_cap->eqc_entry_sz,
Jack Morgensteinab9c17a2011-12-13 04:18:30 +0000908 num_eqs, num_eqs, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700909 if (err) {
910 mlx4_err(dev, "Failed to map EQ context memory, aborting.\n");
911 goto err_unmap_cmpt;
912 }
913
Jack Morgensteind7bb58f2007-08-01 12:28:53 +0300914 /*
915 * Reserved MTT entries must be aligned up to a cacheline
916 * boundary, since the FW will write to them, while the driver
917 * writes to all other MTT entries. (The variable
918 * dev->caps.mtt_entry_sz below is really the MTT segment
919 * size, not the raw entry size)
920 */
921 dev->caps.reserved_mtts =
922 ALIGN(dev->caps.reserved_mtts * dev->caps.mtt_entry_sz,
923 dma_get_cache_alignment()) / dev->caps.mtt_entry_sz;
924
Roland Dreier225c7b12007-05-08 18:00:38 -0700925 err = mlx4_init_icm_table(dev, &priv->mr_table.mtt_table,
926 init_hca->mtt_base,
927 dev->caps.mtt_entry_sz,
Marcel Apfelbaum2b8fb282011-12-13 04:16:56 +0000928 dev->caps.num_mtts,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300929 dev->caps.reserved_mtts, 1, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700930 if (err) {
931 mlx4_err(dev, "Failed to map MTT context memory, aborting.\n");
932 goto err_unmap_eq;
933 }
934
935 err = mlx4_init_icm_table(dev, &priv->mr_table.dmpt_table,
936 init_hca->dmpt_base,
937 dev_cap->dmpt_entry_sz,
938 dev->caps.num_mpts,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300939 dev->caps.reserved_mrws, 1, 1);
Roland Dreier225c7b12007-05-08 18:00:38 -0700940 if (err) {
941 mlx4_err(dev, "Failed to map dMPT context memory, aborting.\n");
942 goto err_unmap_mtt;
943 }
944
945 err = mlx4_init_icm_table(dev, &priv->qp_table.qp_table,
946 init_hca->qpc_base,
947 dev_cap->qpc_entry_sz,
948 dev->caps.num_qps,
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700949 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
950 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700951 if (err) {
952 mlx4_err(dev, "Failed to map QP context memory, aborting.\n");
953 goto err_unmap_dmpt;
954 }
955
956 err = mlx4_init_icm_table(dev, &priv->qp_table.auxc_table,
957 init_hca->auxc_base,
958 dev_cap->aux_entry_sz,
959 dev->caps.num_qps,
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700960 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
961 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700962 if (err) {
963 mlx4_err(dev, "Failed to map AUXC context memory, aborting.\n");
964 goto err_unmap_qp;
965 }
966
967 err = mlx4_init_icm_table(dev, &priv->qp_table.altc_table,
968 init_hca->altc_base,
969 dev_cap->altc_entry_sz,
970 dev->caps.num_qps,
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700971 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
972 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700973 if (err) {
974 mlx4_err(dev, "Failed to map ALTC context memory, aborting.\n");
975 goto err_unmap_auxc;
976 }
977
978 err = mlx4_init_icm_table(dev, &priv->qp_table.rdmarc_table,
979 init_hca->rdmarc_base,
980 dev_cap->rdmarc_entry_sz << priv->qp_table.rdmarc_shift,
981 dev->caps.num_qps,
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700982 dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
983 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700984 if (err) {
985 mlx4_err(dev, "Failed to map RDMARC context memory, aborting\n");
986 goto err_unmap_altc;
987 }
988
989 err = mlx4_init_icm_table(dev, &priv->cq_table.table,
990 init_hca->cqc_base,
991 dev_cap->cqc_entry_sz,
992 dev->caps.num_cqs,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +0300993 dev->caps.reserved_cqs, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -0700994 if (err) {
995 mlx4_err(dev, "Failed to map CQ context memory, aborting.\n");
996 goto err_unmap_rdmarc;
997 }
998
999 err = mlx4_init_icm_table(dev, &priv->srq_table.table,
1000 init_hca->srqc_base,
1001 dev_cap->srq_entry_sz,
1002 dev->caps.num_srqs,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +03001003 dev->caps.reserved_srqs, 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001004 if (err) {
1005 mlx4_err(dev, "Failed to map SRQ context memory, aborting.\n");
1006 goto err_unmap_cq;
1007 }
1008
1009 /*
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001010 * For flow steering device managed mode it is required to use
1011 * mlx4_init_icm_table. For B0 steering mode it's not strictly
1012 * required, but for simplicity just map the whole multicast
1013 * group table now. The table isn't very big and it's a lot
1014 * easier than trying to track ref counts.
Roland Dreier225c7b12007-05-08 18:00:38 -07001015 */
1016 err = mlx4_init_icm_table(dev, &priv->mcg_table.table,
Eugenia Emantayev0ec2c0f2011-12-13 04:16:02 +00001017 init_hca->mc_base,
1018 mlx4_get_mgm_entry_size(dev),
Roland Dreier225c7b12007-05-08 18:00:38 -07001019 dev->caps.num_mgms + dev->caps.num_amgms,
1020 dev->caps.num_mgms + dev->caps.num_amgms,
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +03001021 0, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001022 if (err) {
1023 mlx4_err(dev, "Failed to map MCG context memory, aborting.\n");
1024 goto err_unmap_srq;
1025 }
1026
1027 return 0;
1028
1029err_unmap_srq:
1030 mlx4_cleanup_icm_table(dev, &priv->srq_table.table);
1031
1032err_unmap_cq:
1033 mlx4_cleanup_icm_table(dev, &priv->cq_table.table);
1034
1035err_unmap_rdmarc:
1036 mlx4_cleanup_icm_table(dev, &priv->qp_table.rdmarc_table);
1037
1038err_unmap_altc:
1039 mlx4_cleanup_icm_table(dev, &priv->qp_table.altc_table);
1040
1041err_unmap_auxc:
1042 mlx4_cleanup_icm_table(dev, &priv->qp_table.auxc_table);
1043
1044err_unmap_qp:
1045 mlx4_cleanup_icm_table(dev, &priv->qp_table.qp_table);
1046
1047err_unmap_dmpt:
1048 mlx4_cleanup_icm_table(dev, &priv->mr_table.dmpt_table);
1049
1050err_unmap_mtt:
1051 mlx4_cleanup_icm_table(dev, &priv->mr_table.mtt_table);
1052
1053err_unmap_eq:
Roland Dreierfa0681d2009-09-05 20:24:49 -07001054 mlx4_cleanup_icm_table(dev, &priv->eq_table.table);
Roland Dreier225c7b12007-05-08 18:00:38 -07001055
1056err_unmap_cmpt:
1057 mlx4_cleanup_icm_table(dev, &priv->eq_table.cmpt_table);
1058 mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
1059 mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
1060 mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
1061
1062err_unmap_aux:
1063 mlx4_UNMAP_ICM_AUX(dev);
1064
1065err_free_aux:
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +03001066 mlx4_free_icm(dev, priv->fw.aux_icm, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001067
1068 return err;
1069}
1070
1071static void mlx4_free_icms(struct mlx4_dev *dev)
1072{
1073 struct mlx4_priv *priv = mlx4_priv(dev);
1074
1075 mlx4_cleanup_icm_table(dev, &priv->mcg_table.table);
1076 mlx4_cleanup_icm_table(dev, &priv->srq_table.table);
1077 mlx4_cleanup_icm_table(dev, &priv->cq_table.table);
1078 mlx4_cleanup_icm_table(dev, &priv->qp_table.rdmarc_table);
1079 mlx4_cleanup_icm_table(dev, &priv->qp_table.altc_table);
1080 mlx4_cleanup_icm_table(dev, &priv->qp_table.auxc_table);
1081 mlx4_cleanup_icm_table(dev, &priv->qp_table.qp_table);
1082 mlx4_cleanup_icm_table(dev, &priv->mr_table.dmpt_table);
1083 mlx4_cleanup_icm_table(dev, &priv->mr_table.mtt_table);
Roland Dreierfa0681d2009-09-05 20:24:49 -07001084 mlx4_cleanup_icm_table(dev, &priv->eq_table.table);
Roland Dreier225c7b12007-05-08 18:00:38 -07001085 mlx4_cleanup_icm_table(dev, &priv->eq_table.cmpt_table);
1086 mlx4_cleanup_icm_table(dev, &priv->cq_table.cmpt_table);
1087 mlx4_cleanup_icm_table(dev, &priv->srq_table.cmpt_table);
1088 mlx4_cleanup_icm_table(dev, &priv->qp_table.cmpt_table);
Roland Dreier225c7b12007-05-08 18:00:38 -07001089
1090 mlx4_UNMAP_ICM_AUX(dev);
Jack Morgenstein5b0bf5e2007-08-01 12:28:20 +03001091 mlx4_free_icm(dev, priv->fw.aux_icm, 0);
Roland Dreier225c7b12007-05-08 18:00:38 -07001092}
1093
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001094static void mlx4_slave_exit(struct mlx4_dev *dev)
1095{
1096 struct mlx4_priv *priv = mlx4_priv(dev);
1097
1098 down(&priv->cmd.slave_sem);
1099 if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_RESET, 0, MLX4_COMM_TIME))
1100 mlx4_warn(dev, "Failed to close slave function.\n");
1101 up(&priv->cmd.slave_sem);
1102}
1103
Eli Cohenc1b43dc2011-03-22 22:38:41 +00001104static int map_bf_area(struct mlx4_dev *dev)
1105{
1106 struct mlx4_priv *priv = mlx4_priv(dev);
1107 resource_size_t bf_start;
1108 resource_size_t bf_len;
1109 int err = 0;
1110
Jack Morgenstein3d747472012-02-19 21:38:52 +00001111 if (!dev->caps.bf_reg_size)
1112 return -ENXIO;
1113
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001114 bf_start = pci_resource_start(dev->pdev, 2) +
1115 (dev->caps.num_uars << PAGE_SHIFT);
1116 bf_len = pci_resource_len(dev->pdev, 2) -
1117 (dev->caps.num_uars << PAGE_SHIFT);
Eli Cohenc1b43dc2011-03-22 22:38:41 +00001118 priv->bf_mapping = io_mapping_create_wc(bf_start, bf_len);
1119 if (!priv->bf_mapping)
1120 err = -ENOMEM;
1121
1122 return err;
1123}
1124
1125static void unmap_bf_area(struct mlx4_dev *dev)
1126{
1127 if (mlx4_priv(dev)->bf_mapping)
1128 io_mapping_free(mlx4_priv(dev)->bf_mapping);
1129}
1130
Roland Dreier225c7b12007-05-08 18:00:38 -07001131static void mlx4_close_hca(struct mlx4_dev *dev)
1132{
Eli Cohenc1b43dc2011-03-22 22:38:41 +00001133 unmap_bf_area(dev);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001134 if (mlx4_is_slave(dev))
1135 mlx4_slave_exit(dev);
1136 else {
1137 mlx4_CLOSE_HCA(dev, 0);
1138 mlx4_free_icms(dev);
1139 mlx4_UNMAP_FA(dev);
1140 mlx4_free_icm(dev, mlx4_priv(dev)->fw.fw_icm, 0);
1141 }
1142}
1143
1144static int mlx4_init_slave(struct mlx4_dev *dev)
1145{
1146 struct mlx4_priv *priv = mlx4_priv(dev);
1147 u64 dma = (u64) priv->mfunc.vhcr_dma;
1148 int num_of_reset_retries = NUM_OF_RESET_RETRIES;
1149 int ret_from_reset = 0;
1150 u32 slave_read;
1151 u32 cmd_channel_ver;
1152
1153 down(&priv->cmd.slave_sem);
1154 priv->cmd.max_cmds = 1;
1155 mlx4_warn(dev, "Sending reset\n");
1156 ret_from_reset = mlx4_comm_cmd(dev, MLX4_COMM_CMD_RESET, 0,
1157 MLX4_COMM_TIME);
1158 /* if we are in the middle of flr the slave will try
1159 * NUM_OF_RESET_RETRIES times before leaving.*/
1160 if (ret_from_reset) {
1161 if (MLX4_DELAY_RESET_SLAVE == ret_from_reset) {
1162 msleep(SLEEP_TIME_IN_RESET);
1163 while (ret_from_reset && num_of_reset_retries) {
1164 mlx4_warn(dev, "slave is currently in the"
1165 "middle of FLR. retrying..."
1166 "(try num:%d)\n",
1167 (NUM_OF_RESET_RETRIES -
1168 num_of_reset_retries + 1));
1169 ret_from_reset =
1170 mlx4_comm_cmd(dev, MLX4_COMM_CMD_RESET,
1171 0, MLX4_COMM_TIME);
1172 num_of_reset_retries = num_of_reset_retries - 1;
1173 }
1174 } else
1175 goto err;
1176 }
1177
1178 /* check the driver version - the slave I/F revision
1179 * must match the master's */
1180 slave_read = swab32(readl(&priv->mfunc.comm->slave_read));
1181 cmd_channel_ver = mlx4_comm_get_version();
1182
1183 if (MLX4_COMM_GET_IF_REV(cmd_channel_ver) !=
1184 MLX4_COMM_GET_IF_REV(slave_read)) {
1185 mlx4_err(dev, "slave driver version is not supported"
1186 " by the master\n");
1187 goto err;
1188 }
1189
1190 mlx4_warn(dev, "Sending vhcr0\n");
1191 if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR0, dma >> 48,
1192 MLX4_COMM_TIME))
1193 goto err;
1194 if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR1, dma >> 32,
1195 MLX4_COMM_TIME))
1196 goto err;
1197 if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR2, dma >> 16,
1198 MLX4_COMM_TIME))
1199 goto err;
1200 if (mlx4_comm_cmd(dev, MLX4_COMM_CMD_VHCR_EN, dma, MLX4_COMM_TIME))
1201 goto err;
1202 up(&priv->cmd.slave_sem);
1203 return 0;
1204
1205err:
1206 mlx4_comm_cmd(dev, MLX4_COMM_CMD_RESET, 0, 0);
1207 up(&priv->cmd.slave_sem);
1208 return -EIO;
Roland Dreier225c7b12007-05-08 18:00:38 -07001209}
1210
Jack Morgenstein66349612012-06-19 11:21:44 +03001211static void mlx4_parav_master_pf_caps(struct mlx4_dev *dev)
1212{
1213 int i;
1214
1215 for (i = 1; i <= dev->caps.num_ports; i++) {
1216 dev->caps.gid_table_len[i] = 1;
1217 dev->caps.pkey_table_len[i] =
1218 dev->phys_caps.pkey_phys_table_len[i] - 1;
1219 }
1220}
1221
Roland Dreier3d73c282007-10-10 15:43:54 -07001222static int mlx4_init_hca(struct mlx4_dev *dev)
Roland Dreier225c7b12007-05-08 18:00:38 -07001223{
1224 struct mlx4_priv *priv = mlx4_priv(dev);
1225 struct mlx4_adapter adapter;
1226 struct mlx4_dev_cap dev_cap;
Vladimir Sokolovsky2d928652008-07-14 23:48:53 -07001227 struct mlx4_mod_stat_cfg mlx4_cfg;
Roland Dreier225c7b12007-05-08 18:00:38 -07001228 struct mlx4_profile profile;
1229 struct mlx4_init_hca_param init_hca;
1230 u64 icm_size;
1231 int err;
1232
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001233 if (!mlx4_is_slave(dev)) {
1234 err = mlx4_QUERY_FW(dev);
1235 if (err) {
1236 if (err == -EACCES)
1237 mlx4_info(dev, "non-primary physical function, skipping.\n");
1238 else
1239 mlx4_err(dev, "QUERY_FW command failed, aborting.\n");
1240 goto unmap_bf;
1241 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001242
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001243 err = mlx4_load_fw(dev);
1244 if (err) {
1245 mlx4_err(dev, "Failed to start FW, aborting.\n");
1246 goto unmap_bf;
1247 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001248
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001249 mlx4_cfg.log_pg_sz_m = 1;
1250 mlx4_cfg.log_pg_sz = 0;
1251 err = mlx4_MOD_STAT_CFG(dev, &mlx4_cfg);
1252 if (err)
1253 mlx4_warn(dev, "Failed to override log_pg_sz parameter\n");
Vladimir Sokolovsky2d928652008-07-14 23:48:53 -07001254
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001255 err = mlx4_dev_cap(dev, &dev_cap);
1256 if (err) {
1257 mlx4_err(dev, "QUERY_DEV_CAP command failed, aborting.\n");
1258 goto err_stop_fw;
1259 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001260
Jack Morgenstein66349612012-06-19 11:21:44 +03001261 if (mlx4_is_master(dev))
1262 mlx4_parav_master_pf_caps(dev);
1263
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001264 priv->fs_hash_mode = MLX4_FS_L2_HASH;
1265
1266 switch (priv->fs_hash_mode) {
1267 case MLX4_FS_L2_HASH:
1268 init_hca.fs_hash_enable_bits = 0;
1269 break;
1270
1271 case MLX4_FS_L2_L3_L4_HASH:
1272 /* Enable flow steering with
1273 * udp unicast and tcp unicast
1274 */
1275 init_hca.fs_hash_enable_bits =
1276 MLX4_FS_UDP_UC_EN | MLX4_FS_TCP_UC_EN;
1277 break;
1278 }
1279
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001280 profile = default_profile;
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001281 if (dev->caps.steering_mode ==
1282 MLX4_STEERING_MODE_DEVICE_MANAGED)
1283 profile.num_mcg = MLX4_FS_NUM_MCG;
Roland Dreier225c7b12007-05-08 18:00:38 -07001284
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001285 icm_size = mlx4_make_profile(dev, &profile, &dev_cap,
1286 &init_hca);
1287 if ((long long) icm_size < 0) {
1288 err = icm_size;
1289 goto err_stop_fw;
1290 }
1291
Eli Cohena5bbe892012-02-09 18:10:06 +02001292 dev->caps.max_fmr_maps = (1 << (32 - ilog2(dev->caps.num_mpts))) - 1;
1293
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001294 init_hca.log_uar_sz = ilog2(dev->caps.num_uars);
1295 init_hca.uar_page_sz = PAGE_SHIFT - 12;
1296
1297 err = mlx4_init_icm(dev, &dev_cap, &init_hca, icm_size);
1298 if (err)
1299 goto err_stop_fw;
1300
1301 err = mlx4_INIT_HCA(dev, &init_hca);
1302 if (err) {
1303 mlx4_err(dev, "INIT_HCA command failed, aborting.\n");
1304 goto err_free_icm;
1305 }
1306 } else {
1307 err = mlx4_init_slave(dev);
1308 if (err) {
1309 mlx4_err(dev, "Failed to initialize slave\n");
1310 goto unmap_bf;
1311 }
1312
1313 err = mlx4_slave_cap(dev);
1314 if (err) {
1315 mlx4_err(dev, "Failed to obtain slave caps\n");
1316 goto err_close;
1317 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001318 }
1319
Eli Cohenc1b43dc2011-03-22 22:38:41 +00001320 if (map_bf_area(dev))
1321 mlx4_dbg(dev, "Failed to map blue flame area\n");
1322
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001323 /*Only the master set the ports, all the rest got it from it.*/
1324 if (!mlx4_is_slave(dev))
1325 mlx4_set_port_mask(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07001326
1327 err = mlx4_QUERY_ADAPTER(dev, &adapter);
1328 if (err) {
1329 mlx4_err(dev, "QUERY_ADAPTER command failed, aborting.\n");
1330 goto err_close;
1331 }
1332
1333 priv->eq_table.inta_pin = adapter.inta_pin;
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001334 memcpy(dev->board_id, adapter.board_id, sizeof dev->board_id);
Roland Dreier225c7b12007-05-08 18:00:38 -07001335
1336 return 0;
1337
1338err_close:
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001339 mlx4_close_hca(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07001340
1341err_free_icm:
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001342 if (!mlx4_is_slave(dev))
1343 mlx4_free_icms(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07001344
1345err_stop_fw:
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001346 if (!mlx4_is_slave(dev)) {
1347 mlx4_UNMAP_FA(dev);
1348 mlx4_free_icm(dev, priv->fw.fw_icm, 0);
1349 }
1350unmap_bf:
Eli Cohenc1b43dc2011-03-22 22:38:41 +00001351 unmap_bf_area(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07001352 return err;
1353}
1354
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +00001355static int mlx4_init_counters_table(struct mlx4_dev *dev)
1356{
1357 struct mlx4_priv *priv = mlx4_priv(dev);
1358 int nent;
1359
1360 if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_COUNTERS))
1361 return -ENOENT;
1362
1363 nent = dev->caps.max_counters;
1364 return mlx4_bitmap_init(&priv->counters_bitmap, nent, nent - 1, 0, 0);
1365}
1366
1367static void mlx4_cleanup_counters_table(struct mlx4_dev *dev)
1368{
1369 mlx4_bitmap_cleanup(&mlx4_priv(dev)->counters_bitmap);
1370}
1371
Jack Morgensteinba062d52012-05-15 10:35:03 +00001372int __mlx4_counter_alloc(struct mlx4_dev *dev, u32 *idx)
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +00001373{
1374 struct mlx4_priv *priv = mlx4_priv(dev);
1375
1376 if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_COUNTERS))
1377 return -ENOENT;
1378
1379 *idx = mlx4_bitmap_alloc(&priv->counters_bitmap);
1380 if (*idx == -1)
1381 return -ENOMEM;
1382
1383 return 0;
1384}
Jack Morgensteinba062d52012-05-15 10:35:03 +00001385
1386int mlx4_counter_alloc(struct mlx4_dev *dev, u32 *idx)
1387{
1388 u64 out_param;
1389 int err;
1390
1391 if (mlx4_is_mfunc(dev)) {
1392 err = mlx4_cmd_imm(dev, 0, &out_param, RES_COUNTER,
1393 RES_OP_RESERVE, MLX4_CMD_ALLOC_RES,
1394 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
1395 if (!err)
1396 *idx = get_param_l(&out_param);
1397
1398 return err;
1399 }
1400 return __mlx4_counter_alloc(dev, idx);
1401}
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +00001402EXPORT_SYMBOL_GPL(mlx4_counter_alloc);
1403
Jack Morgensteinba062d52012-05-15 10:35:03 +00001404void __mlx4_counter_free(struct mlx4_dev *dev, u32 idx)
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +00001405{
1406 mlx4_bitmap_free(&mlx4_priv(dev)->counters_bitmap, idx);
1407 return;
1408}
Jack Morgensteinba062d52012-05-15 10:35:03 +00001409
1410void mlx4_counter_free(struct mlx4_dev *dev, u32 idx)
1411{
1412 u64 in_param;
1413
1414 if (mlx4_is_mfunc(dev)) {
1415 set_param_l(&in_param, idx);
1416 mlx4_cmd(dev, in_param, RES_COUNTER, RES_OP_RESERVE,
1417 MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A,
1418 MLX4_CMD_WRAPPED);
1419 return;
1420 }
1421 __mlx4_counter_free(dev, idx);
1422}
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +00001423EXPORT_SYMBOL_GPL(mlx4_counter_free);
1424
Roland Dreier3d73c282007-10-10 15:43:54 -07001425static int mlx4_setup_hca(struct mlx4_dev *dev)
Roland Dreier225c7b12007-05-08 18:00:38 -07001426{
1427 struct mlx4_priv *priv = mlx4_priv(dev);
1428 int err;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07001429 int port;
Jack Morgenstein9a5aa622008-11-28 21:29:46 -08001430 __be32 ib_port_default_caps;
Roland Dreier225c7b12007-05-08 18:00:38 -07001431
Roland Dreier225c7b12007-05-08 18:00:38 -07001432 err = mlx4_init_uar_table(dev);
1433 if (err) {
1434 mlx4_err(dev, "Failed to initialize "
1435 "user access region table, aborting.\n");
1436 return err;
1437 }
1438
1439 err = mlx4_uar_alloc(dev, &priv->driver_uar);
1440 if (err) {
1441 mlx4_err(dev, "Failed to allocate driver access region, "
1442 "aborting.\n");
1443 goto err_uar_table_free;
1444 }
1445
Roland Dreier4979d182011-01-12 09:50:36 -08001446 priv->kar = ioremap((phys_addr_t) priv->driver_uar.pfn << PAGE_SHIFT, PAGE_SIZE);
Roland Dreier225c7b12007-05-08 18:00:38 -07001447 if (!priv->kar) {
1448 mlx4_err(dev, "Couldn't map kernel access region, "
1449 "aborting.\n");
1450 err = -ENOMEM;
1451 goto err_uar_free;
1452 }
1453
1454 err = mlx4_init_pd_table(dev);
1455 if (err) {
1456 mlx4_err(dev, "Failed to initialize "
1457 "protection domain table, aborting.\n");
1458 goto err_kar_unmap;
1459 }
1460
Sean Hefty012a8ff2011-06-02 09:01:33 -07001461 err = mlx4_init_xrcd_table(dev);
1462 if (err) {
1463 mlx4_err(dev, "Failed to initialize "
1464 "reliable connection domain table, aborting.\n");
1465 goto err_pd_table_free;
1466 }
1467
Roland Dreier225c7b12007-05-08 18:00:38 -07001468 err = mlx4_init_mr_table(dev);
1469 if (err) {
1470 mlx4_err(dev, "Failed to initialize "
1471 "memory region table, aborting.\n");
Sean Hefty012a8ff2011-06-02 09:01:33 -07001472 goto err_xrcd_table_free;
Roland Dreier225c7b12007-05-08 18:00:38 -07001473 }
1474
Roland Dreier225c7b12007-05-08 18:00:38 -07001475 err = mlx4_init_eq_table(dev);
1476 if (err) {
1477 mlx4_err(dev, "Failed to initialize "
1478 "event queue table, aborting.\n");
Jack Morgensteinee49bd92007-07-12 17:50:45 +03001479 goto err_mr_table_free;
Roland Dreier225c7b12007-05-08 18:00:38 -07001480 }
1481
1482 err = mlx4_cmd_use_events(dev);
1483 if (err) {
1484 mlx4_err(dev, "Failed to switch to event-driven "
1485 "firmware commands, aborting.\n");
1486 goto err_eq_table_free;
1487 }
1488
1489 err = mlx4_NOP(dev);
1490 if (err) {
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +03001491 if (dev->flags & MLX4_FLAG_MSI_X) {
1492 mlx4_warn(dev, "NOP command failed to generate MSI-X "
1493 "interrupt IRQ %d).\n",
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08001494 priv->eq_table.eq[dev->caps.num_comp_vectors].irq);
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +03001495 mlx4_warn(dev, "Trying again without MSI-X.\n");
1496 } else {
1497 mlx4_err(dev, "NOP command failed to generate interrupt "
1498 "(IRQ %d), aborting.\n",
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08001499 priv->eq_table.eq[dev->caps.num_comp_vectors].irq);
Roland Dreier225c7b12007-05-08 18:00:38 -07001500 mlx4_err(dev, "BIOS or ACPI interrupt routing problem?\n");
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +03001501 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001502
1503 goto err_cmd_poll;
1504 }
1505
1506 mlx4_dbg(dev, "NOP command IRQ test passed\n");
1507
1508 err = mlx4_init_cq_table(dev);
1509 if (err) {
1510 mlx4_err(dev, "Failed to initialize "
1511 "completion queue table, aborting.\n");
1512 goto err_cmd_poll;
1513 }
1514
1515 err = mlx4_init_srq_table(dev);
1516 if (err) {
1517 mlx4_err(dev, "Failed to initialize "
1518 "shared receive queue table, aborting.\n");
1519 goto err_cq_table_free;
1520 }
1521
1522 err = mlx4_init_qp_table(dev);
1523 if (err) {
1524 mlx4_err(dev, "Failed to initialize "
1525 "queue pair table, aborting.\n");
1526 goto err_srq_table_free;
1527 }
1528
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001529 if (!mlx4_is_slave(dev)) {
1530 err = mlx4_init_mcg_table(dev);
1531 if (err) {
1532 mlx4_err(dev, "Failed to initialize "
1533 "multicast group table, aborting.\n");
1534 goto err_qp_table_free;
1535 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001536 }
1537
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +00001538 err = mlx4_init_counters_table(dev);
1539 if (err && err != -ENOENT) {
1540 mlx4_err(dev, "Failed to initialize counters table, aborting.\n");
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001541 goto err_mcg_table_free;
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +00001542 }
1543
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001544 if (!mlx4_is_slave(dev)) {
1545 for (port = 1; port <= dev->caps.num_ports; port++) {
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001546 ib_port_default_caps = 0;
1547 err = mlx4_get_port_ib_caps(dev, port,
1548 &ib_port_default_caps);
1549 if (err)
1550 mlx4_warn(dev, "failed to get port %d default "
1551 "ib capabilities (%d). Continuing "
1552 "with caps = 0\n", port, err);
1553 dev->caps.ib_port_def_cap[port] = ib_port_default_caps;
Marcel Apfelbaum97285b72011-10-24 11:02:34 +02001554
Jack Morgenstein2aca1172012-06-19 11:21:41 +03001555 /* initialize per-slave default ib port capabilities */
1556 if (mlx4_is_master(dev)) {
1557 int i;
1558 for (i = 0; i < dev->num_slaves; i++) {
1559 if (i == mlx4_master_func_num(dev))
1560 continue;
1561 priv->mfunc.master.slave_state[i].ib_cap_mask[port] =
1562 ib_port_default_caps;
1563 }
1564 }
1565
Or Gerlitz096335b2012-01-11 19:02:17 +02001566 if (mlx4_is_mfunc(dev))
1567 dev->caps.port_ib_mtu[port] = IB_MTU_2048;
1568 else
1569 dev->caps.port_ib_mtu[port] = IB_MTU_4096;
Marcel Apfelbaum97285b72011-10-24 11:02:34 +02001570
Jack Morgenstein66349612012-06-19 11:21:44 +03001571 err = mlx4_SET_PORT(dev, port, mlx4_is_master(dev) ?
1572 dev->caps.pkey_table_len[port] : -1);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001573 if (err) {
1574 mlx4_err(dev, "Failed to set port %d, aborting\n",
1575 port);
1576 goto err_counters_table_free;
1577 }
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07001578 }
1579 }
1580
Roland Dreier225c7b12007-05-08 18:00:38 -07001581 return 0;
1582
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +00001583err_counters_table_free:
1584 mlx4_cleanup_counters_table(dev);
1585
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001586err_mcg_table_free:
1587 mlx4_cleanup_mcg_table(dev);
1588
Roland Dreier225c7b12007-05-08 18:00:38 -07001589err_qp_table_free:
1590 mlx4_cleanup_qp_table(dev);
1591
1592err_srq_table_free:
1593 mlx4_cleanup_srq_table(dev);
1594
1595err_cq_table_free:
1596 mlx4_cleanup_cq_table(dev);
1597
1598err_cmd_poll:
1599 mlx4_cmd_use_polling(dev);
1600
1601err_eq_table_free:
1602 mlx4_cleanup_eq_table(dev);
1603
Jack Morgensteinee49bd92007-07-12 17:50:45 +03001604err_mr_table_free:
Roland Dreier225c7b12007-05-08 18:00:38 -07001605 mlx4_cleanup_mr_table(dev);
1606
Sean Hefty012a8ff2011-06-02 09:01:33 -07001607err_xrcd_table_free:
1608 mlx4_cleanup_xrcd_table(dev);
1609
Roland Dreier225c7b12007-05-08 18:00:38 -07001610err_pd_table_free:
1611 mlx4_cleanup_pd_table(dev);
1612
1613err_kar_unmap:
1614 iounmap(priv->kar);
1615
1616err_uar_free:
1617 mlx4_uar_free(dev, &priv->driver_uar);
1618
1619err_uar_table_free:
1620 mlx4_cleanup_uar_table(dev);
1621 return err;
1622}
1623
Roland Dreiere8f9b2e2008-02-04 20:20:41 -08001624static void mlx4_enable_msi_x(struct mlx4_dev *dev)
Roland Dreier225c7b12007-05-08 18:00:38 -07001625{
1626 struct mlx4_priv *priv = mlx4_priv(dev);
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08001627 struct msix_entry *entries;
Yevgeny Petrilin0b7ca5a2011-03-22 22:37:47 +00001628 int nreq = min_t(int, dev->caps.num_ports *
Yuval Mintz90b1ebe2012-07-01 03:18:51 +00001629 min_t(int, netif_get_num_default_rss_queues() + 1,
1630 MAX_MSIX_P_PORT) + MSIX_LEGACY_SZ, MAX_MSIX);
Roland Dreier225c7b12007-05-08 18:00:38 -07001631 int err;
1632 int i;
1633
1634 if (msi_x) {
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001635 /* In multifunction mode each function gets 2 msi-X vectors
1636 * one for data path completions anf the other for asynch events
1637 * or command completions */
1638 if (mlx4_is_mfunc(dev)) {
1639 nreq = 2;
1640 } else {
1641 nreq = min_t(int, dev->caps.num_eqs -
1642 dev->caps.reserved_eqs, nreq);
1643 }
1644
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08001645 entries = kcalloc(nreq, sizeof *entries, GFP_KERNEL);
1646 if (!entries)
1647 goto no_msi;
1648
1649 for (i = 0; i < nreq; ++i)
Roland Dreier225c7b12007-05-08 18:00:38 -07001650 entries[i].entry = i;
1651
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08001652 retry:
1653 err = pci_enable_msix(dev->pdev, entries, nreq);
Roland Dreier225c7b12007-05-08 18:00:38 -07001654 if (err) {
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08001655 /* Try again if at least 2 vectors are available */
1656 if (err > 1) {
1657 mlx4_info(dev, "Requested %d vectors, "
1658 "but only %d MSI-X vectors available, "
1659 "trying again\n", nreq, err);
1660 nreq = err;
1661 goto retry;
1662 }
Nicolas Morey-Chaisemartin5bf0da72009-04-21 10:11:06 -07001663 kfree(entries);
Roland Dreier225c7b12007-05-08 18:00:38 -07001664 goto no_msi;
1665 }
1666
Yevgeny Petrilin0b7ca5a2011-03-22 22:37:47 +00001667 if (nreq <
1668 MSIX_LEGACY_SZ + dev->caps.num_ports * MIN_MSIX_P_PORT) {
1669 /*Working in legacy mode , all EQ's shared*/
1670 dev->caps.comp_pool = 0;
1671 dev->caps.num_comp_vectors = nreq - 1;
1672 } else {
1673 dev->caps.comp_pool = nreq - MSIX_LEGACY_SZ;
1674 dev->caps.num_comp_vectors = MSIX_LEGACY_SZ - 1;
1675 }
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08001676 for (i = 0; i < nreq; ++i)
Roland Dreier225c7b12007-05-08 18:00:38 -07001677 priv->eq_table.eq[i].irq = entries[i].vector;
1678
1679 dev->flags |= MLX4_FLAG_MSI_X;
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08001680
1681 kfree(entries);
Roland Dreier225c7b12007-05-08 18:00:38 -07001682 return;
1683 }
1684
1685no_msi:
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08001686 dev->caps.num_comp_vectors = 1;
Yevgeny Petrilin0b7ca5a2011-03-22 22:37:47 +00001687 dev->caps.comp_pool = 0;
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08001688
1689 for (i = 0; i < 2; ++i)
Roland Dreier225c7b12007-05-08 18:00:38 -07001690 priv->eq_table.eq[i].irq = dev->pdev->irq;
1691}
1692
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07001693static int mlx4_init_port_info(struct mlx4_dev *dev, int port)
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -07001694{
1695 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07001696 int err = 0;
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -07001697
1698 info->dev = dev;
1699 info->port = port;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001700 if (!mlx4_is_slave(dev)) {
1701 INIT_RADIX_TREE(&info->mac_tree, GFP_KERNEL);
1702 mlx4_init_mac_table(dev, &info->mac_table);
1703 mlx4_init_vlan_table(dev, &info->vlan_table);
1704 info->base_qpn =
1705 dev->caps.reserved_qps_base[MLX4_QP_REGION_ETH_ADDR] +
Yevgeny Petrilin06fa0a82011-08-03 16:38:59 -07001706 (port - 1) * (1 << log_num_mac);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001707 }
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07001708
1709 sprintf(info->dev_name, "mlx4_port%d", port);
1710 info->port_attr.attr.name = info->dev_name;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001711 if (mlx4_is_mfunc(dev))
1712 info->port_attr.attr.mode = S_IRUGO;
1713 else {
1714 info->port_attr.attr.mode = S_IRUGO | S_IWUSR;
1715 info->port_attr.store = set_port_type;
1716 }
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07001717 info->port_attr.show = show_port_type;
Greg Kroah-Hartman3691c962010-03-15 14:01:55 -07001718 sysfs_attr_init(&info->port_attr.attr);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07001719
1720 err = device_create_file(&dev->pdev->dev, &info->port_attr);
1721 if (err) {
1722 mlx4_err(dev, "Failed to create file for port %d\n", port);
1723 info->port = -1;
1724 }
1725
Or Gerlitz096335b2012-01-11 19:02:17 +02001726 sprintf(info->dev_mtu_name, "mlx4_port%d_mtu", port);
1727 info->port_mtu_attr.attr.name = info->dev_mtu_name;
1728 if (mlx4_is_mfunc(dev))
1729 info->port_mtu_attr.attr.mode = S_IRUGO;
1730 else {
1731 info->port_mtu_attr.attr.mode = S_IRUGO | S_IWUSR;
1732 info->port_mtu_attr.store = set_port_ib_mtu;
1733 }
1734 info->port_mtu_attr.show = show_port_ib_mtu;
1735 sysfs_attr_init(&info->port_mtu_attr.attr);
1736
1737 err = device_create_file(&dev->pdev->dev, &info->port_mtu_attr);
1738 if (err) {
1739 mlx4_err(dev, "Failed to create mtu file for port %d\n", port);
1740 device_remove_file(&info->dev->pdev->dev, &info->port_attr);
1741 info->port = -1;
1742 }
1743
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07001744 return err;
1745}
1746
1747static void mlx4_cleanup_port_info(struct mlx4_port_info *info)
1748{
1749 if (info->port < 0)
1750 return;
1751
1752 device_remove_file(&info->dev->pdev->dev, &info->port_attr);
Or Gerlitz096335b2012-01-11 19:02:17 +02001753 device_remove_file(&info->dev->pdev->dev, &info->port_mtu_attr);
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -07001754}
1755
Yevgeny Petrilinb12d93d2011-03-22 22:38:24 +00001756static int mlx4_init_steering(struct mlx4_dev *dev)
1757{
1758 struct mlx4_priv *priv = mlx4_priv(dev);
1759 int num_entries = dev->caps.num_ports;
1760 int i, j;
1761
1762 priv->steer = kzalloc(sizeof(struct mlx4_steer) * num_entries, GFP_KERNEL);
1763 if (!priv->steer)
1764 return -ENOMEM;
1765
Eugenia Emantayev45b51362012-02-14 06:37:41 +00001766 for (i = 0; i < num_entries; i++)
Yevgeny Petrilinb12d93d2011-03-22 22:38:24 +00001767 for (j = 0; j < MLX4_NUM_STEERS; j++) {
1768 INIT_LIST_HEAD(&priv->steer[i].promisc_qps[j]);
1769 INIT_LIST_HEAD(&priv->steer[i].steer_entries[j]);
1770 }
Yevgeny Petrilinb12d93d2011-03-22 22:38:24 +00001771 return 0;
1772}
1773
1774static void mlx4_clear_steering(struct mlx4_dev *dev)
1775{
1776 struct mlx4_priv *priv = mlx4_priv(dev);
1777 struct mlx4_steer_index *entry, *tmp_entry;
1778 struct mlx4_promisc_qp *pqp, *tmp_pqp;
1779 int num_entries = dev->caps.num_ports;
1780 int i, j;
1781
1782 for (i = 0; i < num_entries; i++) {
1783 for (j = 0; j < MLX4_NUM_STEERS; j++) {
1784 list_for_each_entry_safe(pqp, tmp_pqp,
1785 &priv->steer[i].promisc_qps[j],
1786 list) {
1787 list_del(&pqp->list);
1788 kfree(pqp);
1789 }
1790 list_for_each_entry_safe(entry, tmp_entry,
1791 &priv->steer[i].steer_entries[j],
1792 list) {
1793 list_del(&entry->list);
1794 list_for_each_entry_safe(pqp, tmp_pqp,
1795 &entry->duplicates,
1796 list) {
1797 list_del(&pqp->list);
1798 kfree(pqp);
1799 }
1800 kfree(entry);
1801 }
1802 }
1803 }
1804 kfree(priv->steer);
1805}
1806
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001807static int extended_func_num(struct pci_dev *pdev)
1808{
1809 return PCI_SLOT(pdev->devfn) * 8 + PCI_FUNC(pdev->devfn);
1810}
1811
1812#define MLX4_OWNER_BASE 0x8069c
1813#define MLX4_OWNER_SIZE 4
1814
1815static int mlx4_get_ownership(struct mlx4_dev *dev)
1816{
1817 void __iomem *owner;
1818 u32 ret;
1819
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +00001820 if (pci_channel_offline(dev->pdev))
1821 return -EIO;
1822
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001823 owner = ioremap(pci_resource_start(dev->pdev, 0) + MLX4_OWNER_BASE,
1824 MLX4_OWNER_SIZE);
1825 if (!owner) {
1826 mlx4_err(dev, "Failed to obtain ownership bit\n");
1827 return -ENOMEM;
1828 }
1829
1830 ret = readl(owner);
1831 iounmap(owner);
1832 return (int) !!ret;
1833}
1834
1835static void mlx4_free_ownership(struct mlx4_dev *dev)
1836{
1837 void __iomem *owner;
1838
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +00001839 if (pci_channel_offline(dev->pdev))
1840 return;
1841
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001842 owner = ioremap(pci_resource_start(dev->pdev, 0) + MLX4_OWNER_BASE,
1843 MLX4_OWNER_SIZE);
1844 if (!owner) {
1845 mlx4_err(dev, "Failed to obtain ownership bit\n");
1846 return;
1847 }
1848 writel(0, owner);
1849 msleep(1000);
1850 iounmap(owner);
1851}
1852
Roland Dreier3d73c282007-10-10 15:43:54 -07001853static int __mlx4_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
Roland Dreier225c7b12007-05-08 18:00:38 -07001854{
Roland Dreier225c7b12007-05-08 18:00:38 -07001855 struct mlx4_priv *priv;
1856 struct mlx4_dev *dev;
1857 int err;
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -07001858 int port;
Roland Dreier225c7b12007-05-08 18:00:38 -07001859
Joe Perches0a645e82010-07-10 07:22:46 +00001860 pr_info(DRV_NAME ": Initializing %s\n", pci_name(pdev));
Roland Dreier225c7b12007-05-08 18:00:38 -07001861
1862 err = pci_enable_device(pdev);
1863 if (err) {
1864 dev_err(&pdev->dev, "Cannot enable PCI device, "
1865 "aborting.\n");
1866 return err;
1867 }
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001868 if (num_vfs > MLX4_MAX_NUM_VF) {
1869 printk(KERN_ERR "There are more VF's (%d) than allowed(%d)\n",
1870 num_vfs, MLX4_MAX_NUM_VF);
1871 return -EINVAL;
1872 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001873 /*
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001874 * Check for BARs.
Roland Dreier225c7b12007-05-08 18:00:38 -07001875 */
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001876 if (((id == NULL) || !(id->driver_data & MLX4_VF)) &&
1877 !(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1878 dev_err(&pdev->dev, "Missing DCS, aborting."
1879 "(id == 0X%p, id->driver_data: 0x%lx,"
1880 " pci_resource_flags(pdev, 0):0x%lx)\n", id,
1881 id ? id->driver_data : 0, pci_resource_flags(pdev, 0));
Roland Dreier225c7b12007-05-08 18:00:38 -07001882 err = -ENODEV;
1883 goto err_disable_pdev;
1884 }
1885 if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
1886 dev_err(&pdev->dev, "Missing UAR, aborting.\n");
1887 err = -ENODEV;
1888 goto err_disable_pdev;
1889 }
1890
Roland Dreiera01df0f2009-09-05 20:24:48 -07001891 err = pci_request_regions(pdev, DRV_NAME);
Roland Dreier225c7b12007-05-08 18:00:38 -07001892 if (err) {
Roland Dreiera01df0f2009-09-05 20:24:48 -07001893 dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");
Roland Dreier225c7b12007-05-08 18:00:38 -07001894 goto err_disable_pdev;
1895 }
1896
Roland Dreier225c7b12007-05-08 18:00:38 -07001897 pci_set_master(pdev);
1898
Yang Hongyang6a355282009-04-06 19:01:13 -07001899 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
Roland Dreier225c7b12007-05-08 18:00:38 -07001900 if (err) {
1901 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask.\n");
Yang Hongyang284901a2009-04-06 19:01:15 -07001902 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Roland Dreier225c7b12007-05-08 18:00:38 -07001903 if (err) {
1904 dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting.\n");
Roland Dreiera01df0f2009-09-05 20:24:48 -07001905 goto err_release_regions;
Roland Dreier225c7b12007-05-08 18:00:38 -07001906 }
1907 }
Yang Hongyang6a355282009-04-06 19:01:13 -07001908 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
Roland Dreier225c7b12007-05-08 18:00:38 -07001909 if (err) {
1910 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit "
1911 "consistent PCI DMA mask.\n");
Yang Hongyang284901a2009-04-06 19:01:15 -07001912 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Roland Dreier225c7b12007-05-08 18:00:38 -07001913 if (err) {
1914 dev_err(&pdev->dev, "Can't set consistent PCI DMA mask, "
1915 "aborting.\n");
Roland Dreiera01df0f2009-09-05 20:24:48 -07001916 goto err_release_regions;
Roland Dreier225c7b12007-05-08 18:00:38 -07001917 }
1918 }
1919
David Dillow7f9e5c482011-01-17 02:09:44 +00001920 /* Allow large DMA segments, up to the firmware limit of 1 GB */
1921 dma_set_max_seg_size(&pdev->dev, 1024 * 1024 * 1024);
1922
Roland Dreier225c7b12007-05-08 18:00:38 -07001923 priv = kzalloc(sizeof *priv, GFP_KERNEL);
1924 if (!priv) {
1925 dev_err(&pdev->dev, "Device struct alloc failed, "
1926 "aborting.\n");
1927 err = -ENOMEM;
Roland Dreiera01df0f2009-09-05 20:24:48 -07001928 goto err_release_regions;
Roland Dreier225c7b12007-05-08 18:00:38 -07001929 }
1930
1931 dev = &priv->dev;
1932 dev->pdev = pdev;
Roland Dreierb5814012007-06-07 11:51:58 -07001933 INIT_LIST_HEAD(&priv->ctx_list);
1934 spin_lock_init(&priv->ctx_lock);
Roland Dreier225c7b12007-05-08 18:00:38 -07001935
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07001936 mutex_init(&priv->port_mutex);
1937
Yevgeny Petrilin62968832008-04-23 11:55:45 -07001938 INIT_LIST_HEAD(&priv->pgdir_list);
1939 mutex_init(&priv->pgdir_mutex);
1940
Eli Cohenc1b43dc2011-03-22 22:38:41 +00001941 INIT_LIST_HEAD(&priv->bf_list);
1942 mutex_init(&priv->bf_mutex);
1943
Sergei Shtylyovaca7a3a2011-06-23 04:44:30 +00001944 dev->rev_id = pdev->revision;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001945 /* Detect if this device is a virtual function */
1946 if (id && id->driver_data & MLX4_VF) {
1947 /* When acting as pf, we normally skip vfs unless explicitly
1948 * requested to probe them. */
1949 if (num_vfs && extended_func_num(pdev) > probe_vf) {
1950 mlx4_warn(dev, "Skipping virtual function:%d\n",
1951 extended_func_num(pdev));
1952 err = -ENODEV;
1953 goto err_free_dev;
1954 }
1955 mlx4_warn(dev, "Detected virtual function - running in slave mode\n");
1956 dev->flags |= MLX4_FLAG_SLAVE;
1957 } else {
1958 /* We reset the device and enable SRIOV only for physical
1959 * devices. Try to claim ownership on the device;
1960 * if already taken, skip -- do not allow multiple PFs */
1961 err = mlx4_get_ownership(dev);
1962 if (err) {
1963 if (err < 0)
1964 goto err_free_dev;
1965 else {
1966 mlx4_warn(dev, "Multiple PFs not yet supported."
1967 " Skipping PF.\n");
1968 err = -EINVAL;
1969 goto err_free_dev;
1970 }
1971 }
Sergei Shtylyovaca7a3a2011-06-23 04:44:30 +00001972
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001973 if (num_vfs) {
1974 mlx4_warn(dev, "Enabling sriov with:%d vfs\n", num_vfs);
1975 err = pci_enable_sriov(pdev, num_vfs);
1976 if (err) {
1977 mlx4_err(dev, "Failed to enable sriov,"
1978 "continuing without sriov enabled"
1979 " (err = %d).\n", err);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00001980 err = 0;
1981 } else {
1982 mlx4_warn(dev, "Running in master mode\n");
1983 dev->flags |= MLX4_FLAG_SRIOV |
1984 MLX4_FLAG_MASTER;
1985 dev->num_vfs = num_vfs;
1986 }
1987 }
1988
1989 /*
1990 * Now reset the HCA before we touch the PCI capabilities or
1991 * attempt a firmware command, since a boot ROM may have left
1992 * the HCA in an undefined state.
1993 */
1994 err = mlx4_reset(dev);
1995 if (err) {
1996 mlx4_err(dev, "Failed to reset HCA, aborting.\n");
1997 goto err_rel_own;
1998 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001999 }
2000
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002001slave_start:
Roland Dreier225c7b12007-05-08 18:00:38 -07002002 if (mlx4_cmd_init(dev)) {
2003 mlx4_err(dev, "Failed to init command interface, aborting.\n");
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002004 goto err_sriov;
2005 }
2006
2007 /* In slave functions, the communication channel must be initialized
2008 * before posting commands. Also, init num_slaves before calling
2009 * mlx4_init_hca */
2010 if (mlx4_is_mfunc(dev)) {
2011 if (mlx4_is_master(dev))
2012 dev->num_slaves = MLX4_MAX_NUM_SLAVES;
2013 else {
2014 dev->num_slaves = 0;
2015 if (mlx4_multi_func_init(dev)) {
2016 mlx4_err(dev, "Failed to init slave mfunc"
2017 " interface, aborting.\n");
2018 goto err_cmd;
2019 }
2020 }
Roland Dreier225c7b12007-05-08 18:00:38 -07002021 }
2022
2023 err = mlx4_init_hca(dev);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002024 if (err) {
2025 if (err == -EACCES) {
2026 /* Not primary Physical function
2027 * Running in slave mode */
2028 mlx4_cmd_cleanup(dev);
2029 dev->flags |= MLX4_FLAG_SLAVE;
2030 dev->flags &= ~MLX4_FLAG_MASTER;
2031 goto slave_start;
2032 } else
2033 goto err_mfunc;
2034 }
2035
2036 /* In master functions, the communication channel must be initialized
2037 * after obtaining its address from fw */
2038 if (mlx4_is_master(dev)) {
2039 if (mlx4_multi_func_init(dev)) {
2040 mlx4_err(dev, "Failed to init master mfunc"
2041 "interface, aborting.\n");
2042 goto err_close;
2043 }
2044 }
Roland Dreier225c7b12007-05-08 18:00:38 -07002045
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08002046 err = mlx4_alloc_eq_table(dev);
2047 if (err)
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002048 goto err_master_mfunc;
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08002049
Yevgeny Petrilin0b7ca5a2011-03-22 22:37:47 +00002050 priv->msix_ctl.pool_bm = 0;
Yevgeny Petrilin730c41d2012-02-21 03:39:32 +00002051 mutex_init(&priv->msix_ctl.pool_lock);
Yevgeny Petrilin0b7ca5a2011-03-22 22:37:47 +00002052
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +03002053 mlx4_enable_msi_x(dev);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002054 if ((mlx4_is_mfunc(dev)) &&
2055 !(dev->flags & MLX4_FLAG_MSI_X)) {
2056 mlx4_err(dev, "INTx is not supported in multi-function mode."
2057 " aborting.\n");
Yevgeny Petrilinb12d93d2011-03-22 22:38:24 +00002058 goto err_free_eq;
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002059 }
2060
2061 if (!mlx4_is_slave(dev)) {
2062 err = mlx4_init_steering(dev);
2063 if (err)
2064 goto err_free_eq;
2065 }
Yevgeny Petrilinb12d93d2011-03-22 22:38:24 +00002066
Roland Dreier225c7b12007-05-08 18:00:38 -07002067 err = mlx4_setup_hca(dev);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002068 if (err == -EBUSY && (dev->flags & MLX4_FLAG_MSI_X) &&
2069 !mlx4_is_mfunc(dev)) {
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +03002070 dev->flags &= ~MLX4_FLAG_MSI_X;
Yevgeny Petrilin9858d2d2012-06-25 00:24:12 +00002071 dev->caps.num_comp_vectors = 1;
2072 dev->caps.comp_pool = 0;
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +03002073 pci_disable_msix(pdev);
2074 err = mlx4_setup_hca(dev);
2075 }
2076
Roland Dreier225c7b12007-05-08 18:00:38 -07002077 if (err)
Yevgeny Petrilinb12d93d2011-03-22 22:38:24 +00002078 goto err_steer;
Roland Dreier225c7b12007-05-08 18:00:38 -07002079
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002080 for (port = 1; port <= dev->caps.num_ports; port++) {
2081 err = mlx4_init_port_info(dev, port);
2082 if (err)
2083 goto err_port;
2084 }
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -07002085
Roland Dreier225c7b12007-05-08 18:00:38 -07002086 err = mlx4_register_device(dev);
2087 if (err)
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002088 goto err_port;
Roland Dreier225c7b12007-05-08 18:00:38 -07002089
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -07002090 mlx4_sense_init(dev);
2091 mlx4_start_sense(dev);
2092
Roland Dreier225c7b12007-05-08 18:00:38 -07002093 pci_set_drvdata(pdev, dev);
2094
2095 return 0;
2096
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002097err_port:
Eli Cohenb4f77262010-01-06 12:54:39 -08002098 for (--port; port >= 1; --port)
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002099 mlx4_cleanup_port_info(&priv->port[port]);
2100
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +00002101 mlx4_cleanup_counters_table(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002102 mlx4_cleanup_mcg_table(dev);
2103 mlx4_cleanup_qp_table(dev);
2104 mlx4_cleanup_srq_table(dev);
2105 mlx4_cleanup_cq_table(dev);
2106 mlx4_cmd_use_polling(dev);
2107 mlx4_cleanup_eq_table(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002108 mlx4_cleanup_mr_table(dev);
Sean Hefty012a8ff2011-06-02 09:01:33 -07002109 mlx4_cleanup_xrcd_table(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002110 mlx4_cleanup_pd_table(dev);
2111 mlx4_cleanup_uar_table(dev);
2112
Yevgeny Petrilinb12d93d2011-03-22 22:38:24 +00002113err_steer:
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002114 if (!mlx4_is_slave(dev))
2115 mlx4_clear_steering(dev);
Yevgeny Petrilinb12d93d2011-03-22 22:38:24 +00002116
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08002117err_free_eq:
2118 mlx4_free_eq_table(dev);
2119
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002120err_master_mfunc:
2121 if (mlx4_is_master(dev))
2122 mlx4_multi_func_cleanup(dev);
2123
Roland Dreier225c7b12007-05-08 18:00:38 -07002124err_close:
Michael S. Tsirkin08fb1052007-08-07 16:08:28 +03002125 if (dev->flags & MLX4_FLAG_MSI_X)
2126 pci_disable_msix(pdev);
2127
Roland Dreier225c7b12007-05-08 18:00:38 -07002128 mlx4_close_hca(dev);
2129
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002130err_mfunc:
2131 if (mlx4_is_slave(dev))
2132 mlx4_multi_func_cleanup(dev);
2133
Roland Dreier225c7b12007-05-08 18:00:38 -07002134err_cmd:
2135 mlx4_cmd_cleanup(dev);
2136
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002137err_sriov:
Jack Morgenstein681372a2012-05-15 10:35:01 +00002138 if (dev->flags & MLX4_FLAG_SRIOV)
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002139 pci_disable_sriov(pdev);
2140
2141err_rel_own:
2142 if (!mlx4_is_slave(dev))
2143 mlx4_free_ownership(dev);
2144
Roland Dreier225c7b12007-05-08 18:00:38 -07002145err_free_dev:
Roland Dreier225c7b12007-05-08 18:00:38 -07002146 kfree(priv);
2147
Roland Dreiera01df0f2009-09-05 20:24:48 -07002148err_release_regions:
2149 pci_release_regions(pdev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002150
2151err_disable_pdev:
2152 pci_disable_device(pdev);
2153 pci_set_drvdata(pdev, NULL);
2154 return err;
2155}
2156
Roland Dreier3d73c282007-10-10 15:43:54 -07002157static int __devinit mlx4_init_one(struct pci_dev *pdev,
2158 const struct pci_device_id *id)
2159{
Joe Perches0a645e82010-07-10 07:22:46 +00002160 printk_once(KERN_INFO "%s", mlx4_version);
Roland Dreier3d73c282007-10-10 15:43:54 -07002161
Roland Dreierb027cac2007-10-13 14:10:50 -07002162 return __mlx4_init_one(pdev, id);
Roland Dreier3d73c282007-10-10 15:43:54 -07002163}
2164
2165static void mlx4_remove_one(struct pci_dev *pdev)
Roland Dreier225c7b12007-05-08 18:00:38 -07002166{
2167 struct mlx4_dev *dev = pci_get_drvdata(pdev);
2168 struct mlx4_priv *priv = mlx4_priv(dev);
2169 int p;
2170
2171 if (dev) {
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002172 /* in SRIOV it is not allowed to unload the pf's
2173 * driver while there are alive vf's */
2174 if (mlx4_is_master(dev)) {
2175 if (mlx4_how_many_lives_vf(dev))
2176 printk(KERN_ERR "Removing PF when there are assigned VF's !!!\n");
2177 }
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -07002178 mlx4_stop_sense(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002179 mlx4_unregister_device(dev);
2180
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002181 for (p = 1; p <= dev->caps.num_ports; p++) {
2182 mlx4_cleanup_port_info(&priv->port[p]);
Roland Dreier225c7b12007-05-08 18:00:38 -07002183 mlx4_CLOSE_PORT(dev, p);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002184 }
Roland Dreier225c7b12007-05-08 18:00:38 -07002185
Jack Morgensteinb8924952012-05-15 10:35:02 +00002186 if (mlx4_is_master(dev))
2187 mlx4_free_resource_tracker(dev,
2188 RES_TR_FREE_SLAVES_ONLY);
2189
Or Gerlitzf2a3f6a2011-06-15 14:47:14 +00002190 mlx4_cleanup_counters_table(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002191 mlx4_cleanup_mcg_table(dev);
2192 mlx4_cleanup_qp_table(dev);
2193 mlx4_cleanup_srq_table(dev);
2194 mlx4_cleanup_cq_table(dev);
2195 mlx4_cmd_use_polling(dev);
2196 mlx4_cleanup_eq_table(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002197 mlx4_cleanup_mr_table(dev);
Sean Hefty012a8ff2011-06-02 09:01:33 -07002198 mlx4_cleanup_xrcd_table(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002199 mlx4_cleanup_pd_table(dev);
2200
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002201 if (mlx4_is_master(dev))
Jack Morgensteinb8924952012-05-15 10:35:02 +00002202 mlx4_free_resource_tracker(dev,
2203 RES_TR_FREE_STRUCTS_ONLY);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002204
Roland Dreier225c7b12007-05-08 18:00:38 -07002205 iounmap(priv->kar);
2206 mlx4_uar_free(dev, &priv->driver_uar);
2207 mlx4_cleanup_uar_table(dev);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002208 if (!mlx4_is_slave(dev))
2209 mlx4_clear_steering(dev);
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08002210 mlx4_free_eq_table(dev);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002211 if (mlx4_is_master(dev))
2212 mlx4_multi_func_cleanup(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002213 mlx4_close_hca(dev);
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002214 if (mlx4_is_slave(dev))
2215 mlx4_multi_func_cleanup(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002216 mlx4_cmd_cleanup(dev);
2217
2218 if (dev->flags & MLX4_FLAG_MSI_X)
2219 pci_disable_msix(pdev);
Jack Morgenstein681372a2012-05-15 10:35:01 +00002220 if (dev->flags & MLX4_FLAG_SRIOV) {
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002221 mlx4_warn(dev, "Disabling sriov\n");
2222 pci_disable_sriov(pdev);
2223 }
Roland Dreier225c7b12007-05-08 18:00:38 -07002224
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002225 if (!mlx4_is_slave(dev))
2226 mlx4_free_ownership(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002227 kfree(priv);
Roland Dreiera01df0f2009-09-05 20:24:48 -07002228 pci_release_regions(pdev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002229 pci_disable_device(pdev);
2230 pci_set_drvdata(pdev, NULL);
2231 }
2232}
2233
Jack Morgensteinee49bd92007-07-12 17:50:45 +03002234int mlx4_restart_one(struct pci_dev *pdev)
2235{
2236 mlx4_remove_one(pdev);
Roland Dreier3d73c282007-10-10 15:43:54 -07002237 return __mlx4_init_one(pdev, NULL);
Jack Morgensteinee49bd92007-07-12 17:50:45 +03002238}
2239
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002240static DEFINE_PCI_DEVICE_TABLE(mlx4_pci_table) = {
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002241 /* MT25408 "Hermon" SDR */
2242 { PCI_VDEVICE(MELLANOX, 0x6340), 0 },
2243 /* MT25408 "Hermon" DDR */
2244 { PCI_VDEVICE(MELLANOX, 0x634a), 0 },
2245 /* MT25408 "Hermon" QDR */
2246 { PCI_VDEVICE(MELLANOX, 0x6354), 0 },
2247 /* MT25408 "Hermon" DDR PCIe gen2 */
2248 { PCI_VDEVICE(MELLANOX, 0x6732), 0 },
2249 /* MT25408 "Hermon" QDR PCIe gen2 */
2250 { PCI_VDEVICE(MELLANOX, 0x673c), 0 },
2251 /* MT25408 "Hermon" EN 10GigE */
2252 { PCI_VDEVICE(MELLANOX, 0x6368), 0 },
2253 /* MT25408 "Hermon" EN 10GigE PCIe gen2 */
2254 { PCI_VDEVICE(MELLANOX, 0x6750), 0 },
2255 /* MT25458 ConnectX EN 10GBASE-T 10GigE */
2256 { PCI_VDEVICE(MELLANOX, 0x6372), 0 },
2257 /* MT25458 ConnectX EN 10GBASE-T+Gen2 10GigE */
2258 { PCI_VDEVICE(MELLANOX, 0x675a), 0 },
2259 /* MT26468 ConnectX EN 10GigE PCIe gen2*/
2260 { PCI_VDEVICE(MELLANOX, 0x6764), 0 },
2261 /* MT26438 ConnectX EN 40GigE PCIe gen2 5GT/s */
2262 { PCI_VDEVICE(MELLANOX, 0x6746), 0 },
2263 /* MT26478 ConnectX2 40GigE PCIe gen2 */
2264 { PCI_VDEVICE(MELLANOX, 0x676e), 0 },
2265 /* MT25400 Family [ConnectX-2 Virtual Function] */
2266 { PCI_VDEVICE(MELLANOX, 0x1002), MLX4_VF },
2267 /* MT27500 Family [ConnectX-3] */
2268 { PCI_VDEVICE(MELLANOX, 0x1003), 0 },
2269 /* MT27500 Family [ConnectX-3 Virtual Function] */
2270 { PCI_VDEVICE(MELLANOX, 0x1004), MLX4_VF },
2271 { PCI_VDEVICE(MELLANOX, 0x1005), 0 }, /* MT27510 Family */
2272 { PCI_VDEVICE(MELLANOX, 0x1006), 0 }, /* MT27511 Family */
2273 { PCI_VDEVICE(MELLANOX, 0x1007), 0 }, /* MT27520 Family */
2274 { PCI_VDEVICE(MELLANOX, 0x1008), 0 }, /* MT27521 Family */
2275 { PCI_VDEVICE(MELLANOX, 0x1009), 0 }, /* MT27530 Family */
2276 { PCI_VDEVICE(MELLANOX, 0x100a), 0 }, /* MT27531 Family */
2277 { PCI_VDEVICE(MELLANOX, 0x100b), 0 }, /* MT27540 Family */
2278 { PCI_VDEVICE(MELLANOX, 0x100c), 0 }, /* MT27541 Family */
2279 { PCI_VDEVICE(MELLANOX, 0x100d), 0 }, /* MT27550 Family */
2280 { PCI_VDEVICE(MELLANOX, 0x100e), 0 }, /* MT27551 Family */
2281 { PCI_VDEVICE(MELLANOX, 0x100f), 0 }, /* MT27560 Family */
2282 { PCI_VDEVICE(MELLANOX, 0x1010), 0 }, /* MT27561 Family */
Roland Dreier225c7b12007-05-08 18:00:38 -07002283 { 0, }
2284};
2285
2286MODULE_DEVICE_TABLE(pci, mlx4_pci_table);
2287
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +00002288static pci_ers_result_t mlx4_pci_err_detected(struct pci_dev *pdev,
2289 pci_channel_state_t state)
2290{
2291 mlx4_remove_one(pdev);
2292
2293 return state == pci_channel_io_perm_failure ?
2294 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
2295}
2296
2297static pci_ers_result_t mlx4_pci_slot_reset(struct pci_dev *pdev)
2298{
2299 int ret = __mlx4_init_one(pdev, NULL);
2300
2301 return ret ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
2302}
2303
2304static struct pci_error_handlers mlx4_err_handler = {
2305 .error_detected = mlx4_pci_err_detected,
2306 .slot_reset = mlx4_pci_slot_reset,
2307};
2308
Roland Dreier225c7b12007-05-08 18:00:38 -07002309static struct pci_driver mlx4_driver = {
2310 .name = DRV_NAME,
2311 .id_table = mlx4_pci_table,
2312 .probe = mlx4_init_one,
Kleber Sacilotto de Souza57dbf292012-07-20 09:55:43 +00002313 .remove = __devexit_p(mlx4_remove_one),
2314 .err_handler = &mlx4_err_handler,
Roland Dreier225c7b12007-05-08 18:00:38 -07002315};
2316
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002317static int __init mlx4_verify_params(void)
2318{
2319 if ((log_num_mac < 0) || (log_num_mac > 7)) {
Joe Perches0a645e82010-07-10 07:22:46 +00002320 pr_warning("mlx4_core: bad num_mac: %d\n", log_num_mac);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002321 return -1;
2322 }
2323
Or Gerlitzcb296882011-10-16 10:26:21 +02002324 if (log_num_vlan != 0)
2325 pr_warning("mlx4_core: log_num_vlan - obsolete module param, using %d\n",
2326 MLX4_LOG_NUM_VLANS);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002327
Eli Cohen04986282010-09-20 08:42:38 +02002328 if ((log_mtts_per_seg < 1) || (log_mtts_per_seg > 7)) {
Joe Perches0a645e82010-07-10 07:22:46 +00002329 pr_warning("mlx4_core: bad log_mtts_per_seg: %d\n", log_mtts_per_seg);
Eli Cohenab6bf422009-05-27 14:38:34 -07002330 return -1;
2331 }
2332
Jack Morgensteinab9c17a2011-12-13 04:18:30 +00002333 /* Check if module param for ports type has legal combination */
2334 if (port_type_array[0] == false && port_type_array[1] == true) {
2335 printk(KERN_WARNING "Module parameter configuration ETH/IB is not supported. Switching to default configuration IB/IB\n");
2336 port_type_array[0] = true;
2337 }
2338
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002339 return 0;
2340}
2341
Roland Dreier225c7b12007-05-08 18:00:38 -07002342static int __init mlx4_init(void)
2343{
2344 int ret;
2345
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002346 if (mlx4_verify_params())
2347 return -EINVAL;
2348
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -07002349 mlx4_catas_init();
2350
2351 mlx4_wq = create_singlethread_workqueue("mlx4");
2352 if (!mlx4_wq)
2353 return -ENOMEM;
Jack Morgensteinee49bd92007-07-12 17:50:45 +03002354
Roland Dreier225c7b12007-05-08 18:00:38 -07002355 ret = pci_register_driver(&mlx4_driver);
2356 return ret < 0 ? ret : 0;
2357}
2358
2359static void __exit mlx4_cleanup(void)
2360{
2361 pci_unregister_driver(&mlx4_driver);
Yevgeny Petrilin27bf91d2009-03-18 19:45:11 -07002362 destroy_workqueue(mlx4_wq);
Roland Dreier225c7b12007-05-08 18:00:38 -07002363}
2364
2365module_init(mlx4_init);
2366module_exit(mlx4_cleanup);