blob: 1b6dbe156a3708692a743cd58fc3351d4b01e533 [file] [log] [blame]
Roland Dreier225c7b12007-05-08 18:00:38 -07001/*
2 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
Jack Morgenstein51a379d2008-07-25 10:32:52 -07003 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
Roland Dreier225c7b12007-05-08 18:00:38 -07004 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34#include <linux/module.h>
35#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070037#include <linux/errno.h>
Eli Cohenfa417f72010-10-24 21:08:52 -070038#include <linux/netdevice.h>
39#include <linux/inetdevice.h>
40#include <linux/rtnetlink.h>
Eli Cohen4c3eb3c2010-08-26 17:19:22 +030041#include <linux/if_vlan.h>
Moni Shouad487ee72013-12-12 18:03:13 +020042#include <net/ipv6.h>
43#include <net/addrconf.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070044
45#include <rdma/ib_smi.h>
46#include <rdma/ib_user_verbs.h>
Eli Cohenfa417f72010-10-24 21:08:52 -070047#include <rdma/ib_addr.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070048
49#include <linux/mlx4/driver.h>
50#include <linux/mlx4/cmd.h>
51
52#include "mlx4_ib.h"
53#include "user.h"
54
Jack Morgensteinb1d8eb52012-06-19 11:21:35 +030055#define DRV_NAME MLX4_IB_DRV_NAME
Amir Vadai169a1d82014-02-19 17:47:31 +020056#define DRV_VERSION "2.2-1"
57#define DRV_RELDATE "Feb 2014"
Roland Dreier225c7b12007-05-08 18:00:38 -070058
Hadar Hen Zionf77c0162013-08-14 13:58:31 +030059#define MLX4_IB_FLOW_MAX_PRIO 0xFFF
Matan Baraka37a1a42013-11-07 15:25:16 +020060#define MLX4_IB_FLOW_QPN_MASK 0xFFFFFF
Hadar Hen Zionf77c0162013-08-14 13:58:31 +030061
Roland Dreier225c7b12007-05-08 18:00:38 -070062MODULE_AUTHOR("Roland Dreier");
63MODULE_DESCRIPTION("Mellanox ConnectX HCA InfiniBand driver");
64MODULE_LICENSE("Dual BSD/GPL");
65MODULE_VERSION(DRV_VERSION);
66
Jack Morgensteina0c64a12012-08-03 08:40:49 +000067int mlx4_ib_sm_guid_assign = 1;
68module_param_named(sm_guid_assign, mlx4_ib_sm_guid_assign, int, 0444);
69MODULE_PARM_DESC(sm_guid_assign, "Enable SM alias_GUID assignment if sm_guid_assign > 0 (Default: 1)");
70
Roland Dreier68f39482008-02-04 20:20:44 -080071static const char mlx4_ib_version[] =
Roland Dreier225c7b12007-05-08 18:00:38 -070072 DRV_NAME ": Mellanox ConnectX InfiniBand driver v"
73 DRV_VERSION " (" DRV_RELDATE ")\n";
74
Eli Cohenfa417f72010-10-24 21:08:52 -070075struct update_gid_work {
76 struct work_struct work;
77 union ib_gid gids[128];
78 struct mlx4_ib_dev *dev;
79 int port;
80};
81
Jack Morgenstein3806d082012-08-03 08:40:58 +000082static void do_slave_init(struct mlx4_ib_dev *ibdev, int slave, int do_init);
83
Eli Cohenfa417f72010-10-24 21:08:52 -070084static struct workqueue_struct *wq;
85
Roland Dreier225c7b12007-05-08 18:00:38 -070086static void init_query_mad(struct ib_smp *mad)
87{
88 mad->base_version = 1;
89 mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
90 mad->class_version = 1;
91 mad->method = IB_MGMT_METHOD_GET;
92}
93
Eli Cohen4c3eb3c2010-08-26 17:19:22 +030094static union ib_gid zgid;
95
Hadar Hen Zionf77c0162013-08-14 13:58:31 +030096static int check_flow_steering_support(struct mlx4_dev *dev)
97{
Matan Barak0a9b7d52013-11-07 15:25:15 +020098 int eth_num_ports = 0;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +030099 int ib_num_ports = 0;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +0300100
Matan Barak0a9b7d52013-11-07 15:25:15 +0200101 int dmfs = dev->caps.steering_mode == MLX4_STEERING_MODE_DEVICE_MANAGED;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +0300102
Matan Barak0a9b7d52013-11-07 15:25:15 +0200103 if (dmfs) {
104 int i;
105 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH)
106 eth_num_ports++;
107 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
108 ib_num_ports++;
109 dmfs &= (!ib_num_ports ||
110 (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_DMFS_IPOIB)) &&
111 (!eth_num_ports ||
112 (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FS_EN));
113 if (ib_num_ports && mlx4_is_mfunc(dev)) {
114 pr_warn("Device managed flow steering is unavailable for IB port in multifunction env.\n");
115 dmfs = 0;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +0300116 }
Hadar Hen Zionf77c0162013-08-14 13:58:31 +0300117 }
Matan Barak0a9b7d52013-11-07 15:25:15 +0200118 return dmfs;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +0300119}
120
Roland Dreier225c7b12007-05-08 18:00:38 -0700121static int mlx4_ib_query_device(struct ib_device *ibdev,
122 struct ib_device_attr *props)
123{
124 struct mlx4_ib_dev *dev = to_mdev(ibdev);
125 struct ib_smp *in_mad = NULL;
126 struct ib_smp *out_mad = NULL;
127 int err = -ENOMEM;
128
129 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
130 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
131 if (!in_mad || !out_mad)
132 goto out;
133
134 init_query_mad(in_mad);
135 in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
136
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000137 err = mlx4_MAD_IFC(to_mdev(ibdev), MLX4_MAD_IFC_IGNORE_KEYS,
138 1, NULL, NULL, in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -0700139 if (err)
140 goto out;
141
142 memset(props, 0, sizeof *props);
143
144 props->fw_ver = dev->dev->caps.fw_ver;
145 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
146 IB_DEVICE_PORT_ACTIVE_EVENT |
147 IB_DEVICE_SYS_IMAGE_GUID |
Ron Livne521e5752008-07-14 23:48:48 -0700148 IB_DEVICE_RC_RNR_NAK_GEN |
149 IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Roland Dreier225c7b12007-05-08 18:00:38 -0700150 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_PKEY_CNTR)
151 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
152 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_QKEY_CNTR)
153 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
154 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_APM)
155 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
156 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UD_AV_PORT)
157 props->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
Eli Cohen8ff095e2008-04-16 21:01:10 -0700158 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_IPOIB_CSUM)
159 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
Eli Cohen417608c2009-11-12 11:19:44 -0800160 if (dev->dev->caps.max_gso_sz && dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BLH)
Eli Cohenb832be12008-04-16 21:09:27 -0700161 props->device_cap_flags |= IB_DEVICE_UD_TSO;
Roland Dreier95d04f02008-07-23 08:12:26 -0700162 if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_RESERVED_LKEY)
163 props->device_cap_flags |= IB_DEVICE_LOCAL_DMA_LKEY;
164 if ((dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_LOCAL_INV) &&
165 (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_REMOTE_INV) &&
166 (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_FAST_REG_WR))
167 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Sean Hefty0a1405d2011-06-02 11:32:15 -0700168 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC)
169 props->device_cap_flags |= IB_DEVICE_XRC;
Shani Michaelib4253882013-02-06 16:19:16 +0000170 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_MEM_WINDOW)
171 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW;
172 if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_TYPE_2_WIN) {
173 if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_WIN_TYPE_2B)
174 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW_TYPE_2B;
175 else
176 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW_TYPE_2A;
Matan Barak0a9b7d52013-11-07 15:25:15 +0200177 if (dev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED)
Hadar Hen Zionf77c0162013-08-14 13:58:31 +0300178 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
Shani Michaelib4253882013-02-06 16:19:16 +0000179 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700180
181 props->vendor_id = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
182 0xffffff;
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +0000183 props->vendor_part_id = dev->dev->pdev->device;
Roland Dreier225c7b12007-05-08 18:00:38 -0700184 props->hw_ver = be32_to_cpup((__be32 *) (out_mad->data + 32));
185 memcpy(&props->sys_image_guid, out_mad->data + 4, 8);
186
187 props->max_mr_size = ~0ull;
188 props->page_size_cap = dev->dev->caps.page_size_cap;
Jack Morgenstein5a0d0a62013-11-03 10:03:23 +0200189 props->max_qp = dev->dev->quotas.qp;
Sagi Grimbergfc2d0042012-05-24 16:08:08 +0300190 props->max_qp_wr = dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE;
Roland Dreier225c7b12007-05-08 18:00:38 -0700191 props->max_sge = min(dev->dev->caps.max_sq_sg,
192 dev->dev->caps.max_rq_sg);
Jack Morgenstein5a0d0a62013-11-03 10:03:23 +0200193 props->max_cq = dev->dev->quotas.cq;
Roland Dreier225c7b12007-05-08 18:00:38 -0700194 props->max_cqe = dev->dev->caps.max_cqes;
Jack Morgenstein5a0d0a62013-11-03 10:03:23 +0200195 props->max_mr = dev->dev->quotas.mpt;
Roland Dreier225c7b12007-05-08 18:00:38 -0700196 props->max_pd = dev->dev->caps.num_pds - dev->dev->caps.reserved_pds;
197 props->max_qp_rd_atom = dev->dev->caps.max_qp_dest_rdma;
198 props->max_qp_init_rd_atom = dev->dev->caps.max_qp_init_rdma;
199 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Jack Morgenstein5a0d0a62013-11-03 10:03:23 +0200200 props->max_srq = dev->dev->quotas.srq;
Jack Morgensteinc8681f12007-06-21 13:39:10 -0700201 props->max_srq_wr = dev->dev->caps.max_srq_wqes - 1;
Roland Dreier225c7b12007-05-08 18:00:38 -0700202 props->max_srq_sge = dev->dev->caps.max_srq_sge;
Eli Cohen5a0fd092010-10-07 16:24:16 +0200203 props->max_fast_reg_page_list_len = MLX4_MAX_FAST_REG_PAGES;
Roland Dreier225c7b12007-05-08 18:00:38 -0700204 props->local_ca_ack_delay = dev->dev->caps.local_ca_ack_delay;
205 props->atomic_cap = dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_ATOMIC ?
206 IB_ATOMIC_HCA : IB_ATOMIC_NONE;
Dotan Barak47e956b2012-07-11 15:39:29 +0000207 props->masked_atomic_cap = props->atomic_cap;
Roland Dreier5ae2a7a2007-06-18 08:15:02 -0700208 props->max_pkeys = dev->dev->caps.pkey_table_len[1];
Roland Dreier225c7b12007-05-08 18:00:38 -0700209 props->max_mcast_grp = dev->dev->caps.num_mgms + dev->dev->caps.num_amgms;
210 props->max_mcast_qp_attach = dev->dev->caps.num_qp_per_mgm;
211 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
212 props->max_mcast_grp;
Eli Cohena5bbe892012-02-09 18:10:06 +0200213 props->max_map_per_fmr = dev->dev->caps.max_fmr_maps;
Roland Dreier225c7b12007-05-08 18:00:38 -0700214
215out:
216 kfree(in_mad);
217 kfree(out_mad);
218
219 return err;
220}
221
Eli Cohenfa417f72010-10-24 21:08:52 -0700222static enum rdma_link_layer
223mlx4_ib_port_link_layer(struct ib_device *device, u8 port_num)
224{
225 struct mlx4_dev *dev = to_mdev(device)->dev;
226
Jack Morgenstein65dab252011-12-13 04:10:41 +0000227 return dev->caps.port_mask[port_num] == MLX4_PORT_TYPE_IB ?
Eli Cohenfa417f72010-10-24 21:08:52 -0700228 IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
229}
230
231static int ib_link_query_port(struct ib_device *ibdev, u8 port,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000232 struct ib_port_attr *props, int netw_view)
Eli Cohenfa417f72010-10-24 21:08:52 -0700233{
Or Gerlitza9c766b2012-01-11 19:00:29 +0200234 struct ib_smp *in_mad = NULL;
235 struct ib_smp *out_mad = NULL;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300236 int ext_active_speed;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000237 int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
Or Gerlitza9c766b2012-01-11 19:00:29 +0200238 int err = -ENOMEM;
239
240 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
241 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
242 if (!in_mad || !out_mad)
243 goto out;
244
245 init_query_mad(in_mad);
246 in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
247 in_mad->attr_mod = cpu_to_be32(port);
248
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000249 if (mlx4_is_mfunc(to_mdev(ibdev)->dev) && netw_view)
250 mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
251
252 err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port, NULL, NULL,
Or Gerlitza9c766b2012-01-11 19:00:29 +0200253 in_mad, out_mad);
254 if (err)
255 goto out;
256
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300257
Eli Cohenfa417f72010-10-24 21:08:52 -0700258 props->lid = be16_to_cpup((__be16 *) (out_mad->data + 16));
259 props->lmc = out_mad->data[34] & 0x7;
260 props->sm_lid = be16_to_cpup((__be16 *) (out_mad->data + 18));
261 props->sm_sl = out_mad->data[36] & 0xf;
262 props->state = out_mad->data[32] & 0xf;
263 props->phys_state = out_mad->data[33] >> 4;
264 props->port_cap_flags = be32_to_cpup((__be32 *) (out_mad->data + 20));
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000265 if (netw_view)
266 props->gid_tbl_len = out_mad->data[50];
267 else
268 props->gid_tbl_len = to_mdev(ibdev)->dev->caps.gid_table_len[port];
Eli Cohenfa417f72010-10-24 21:08:52 -0700269 props->max_msg_sz = to_mdev(ibdev)->dev->caps.max_msg_sz;
270 props->pkey_tbl_len = to_mdev(ibdev)->dev->caps.pkey_table_len[port];
271 props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46));
272 props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));
273 props->active_width = out_mad->data[31] & 0xf;
274 props->active_speed = out_mad->data[35] >> 4;
275 props->max_mtu = out_mad->data[41] & 0xf;
276 props->active_mtu = out_mad->data[36] >> 4;
277 props->subnet_timeout = out_mad->data[51] & 0x1f;
278 props->max_vl_num = out_mad->data[37] >> 4;
279 props->init_type_reply = out_mad->data[41] >> 4;
280
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300281 /* Check if extended speeds (EDR/FDR/...) are supported */
282 if (props->port_cap_flags & IB_PORT_EXTENDED_SPEEDS_SUP) {
283 ext_active_speed = out_mad->data[62] >> 4;
284
285 switch (ext_active_speed) {
286 case 1:
Or Gerlitz2e966912012-02-28 18:49:50 +0200287 props->active_speed = IB_SPEED_FDR;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300288 break;
289 case 2:
Or Gerlitz2e966912012-02-28 18:49:50 +0200290 props->active_speed = IB_SPEED_EDR;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300291 break;
292 }
293 }
294
295 /* If reported active speed is QDR, check if is FDR-10 */
Or Gerlitz2e966912012-02-28 18:49:50 +0200296 if (props->active_speed == IB_SPEED_QDR) {
Or Gerlitz8154c072012-03-06 15:50:50 +0200297 init_query_mad(in_mad);
298 in_mad->attr_id = MLX4_ATTR_EXTENDED_PORT_INFO;
299 in_mad->attr_mod = cpu_to_be32(port);
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300300
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000301 err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port,
Or Gerlitz8154c072012-03-06 15:50:50 +0200302 NULL, NULL, in_mad, out_mad);
303 if (err)
Jesper Juhlbf6b47d2012-04-11 23:43:29 +0200304 goto out;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300305
Or Gerlitz8154c072012-03-06 15:50:50 +0200306 /* Checking LinkSpeedActive for FDR-10 */
307 if (out_mad->data[15] & 0x1)
308 props->active_speed = IB_SPEED_FDR10;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300309 }
Or Gerlitzd2ef4062012-04-02 17:45:20 +0300310
311 /* Avoid wrong speed value returned by FW if the IB link is down. */
312 if (props->state == IB_PORT_DOWN)
313 props->active_speed = IB_SPEED_SDR;
314
Or Gerlitza9c766b2012-01-11 19:00:29 +0200315out:
316 kfree(in_mad);
317 kfree(out_mad);
318 return err;
Eli Cohenfa417f72010-10-24 21:08:52 -0700319}
320
321static u8 state_to_phys_state(enum ib_port_state state)
322{
323 return state == IB_PORT_ACTIVE ? 5 : 3;
324}
325
326static int eth_link_query_port(struct ib_device *ibdev, u8 port,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000327 struct ib_port_attr *props, int netw_view)
Eli Cohenfa417f72010-10-24 21:08:52 -0700328{
Or Gerlitza9c766b2012-01-11 19:00:29 +0200329
330 struct mlx4_ib_dev *mdev = to_mdev(ibdev);
331 struct mlx4_ib_iboe *iboe = &mdev->iboe;
Eli Cohenfa417f72010-10-24 21:08:52 -0700332 struct net_device *ndev;
333 enum ib_mtu tmp;
Or Gerlitza9c766b2012-01-11 19:00:29 +0200334 struct mlx4_cmd_mailbox *mailbox;
335 int err = 0;
Eli Cohenfa417f72010-10-24 21:08:52 -0700336
Or Gerlitza9c766b2012-01-11 19:00:29 +0200337 mailbox = mlx4_alloc_cmd_mailbox(mdev->dev);
338 if (IS_ERR(mailbox))
339 return PTR_ERR(mailbox);
340
341 err = mlx4_cmd_box(mdev->dev, 0, mailbox->dma, port, 0,
342 MLX4_CMD_QUERY_PORT, MLX4_CMD_TIME_CLASS_B,
343 MLX4_CMD_WRAPPED);
344 if (err)
345 goto out;
346
347 props->active_width = (((u8 *)mailbox->buf)[5] == 0x40) ?
348 IB_WIDTH_4X : IB_WIDTH_1X;
Or Gerlitz2e966912012-02-28 18:49:50 +0200349 props->active_speed = IB_SPEED_QDR;
Moni Shouab4a26a22014-02-09 11:54:34 +0200350 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_IP_BASED_GIDS;
Or Gerlitza9c766b2012-01-11 19:00:29 +0200351 props->gid_tbl_len = mdev->dev->caps.gid_table_len[port];
352 props->max_msg_sz = mdev->dev->caps.max_msg_sz;
Eli Cohenfa417f72010-10-24 21:08:52 -0700353 props->pkey_tbl_len = 1;
Or Gerlitzbcacb892011-10-10 10:53:41 +0200354 props->max_mtu = IB_MTU_4096;
Or Gerlitza9c766b2012-01-11 19:00:29 +0200355 props->max_vl_num = 2;
Eli Cohenfa417f72010-10-24 21:08:52 -0700356 props->state = IB_PORT_DOWN;
357 props->phys_state = state_to_phys_state(props->state);
358 props->active_mtu = IB_MTU_256;
359 spin_lock(&iboe->lock);
360 ndev = iboe->netdevs[port - 1];
361 if (!ndev)
Or Gerlitza9c766b2012-01-11 19:00:29 +0200362 goto out_unlock;
Eli Cohenfa417f72010-10-24 21:08:52 -0700363
364 tmp = iboe_get_mtu(ndev->mtu);
365 props->active_mtu = tmp ? min(props->max_mtu, tmp) : IB_MTU_256;
366
Eli Cohen21d606092010-11-11 21:05:58 +0000367 props->state = (netif_running(ndev) && netif_carrier_ok(ndev)) ?
Eli Cohenfa417f72010-10-24 21:08:52 -0700368 IB_PORT_ACTIVE : IB_PORT_DOWN;
369 props->phys_state = state_to_phys_state(props->state);
Or Gerlitza9c766b2012-01-11 19:00:29 +0200370out_unlock:
Eli Cohenfa417f72010-10-24 21:08:52 -0700371 spin_unlock(&iboe->lock);
Or Gerlitza9c766b2012-01-11 19:00:29 +0200372out:
373 mlx4_free_cmd_mailbox(mdev->dev, mailbox);
374 return err;
Eli Cohenfa417f72010-10-24 21:08:52 -0700375}
376
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000377int __mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
378 struct ib_port_attr *props, int netw_view)
Roland Dreier225c7b12007-05-08 18:00:38 -0700379{
Or Gerlitza9c766b2012-01-11 19:00:29 +0200380 int err;
Roland Dreier225c7b12007-05-08 18:00:38 -0700381
382 memset(props, 0, sizeof *props);
383
Eli Cohenfa417f72010-10-24 21:08:52 -0700384 err = mlx4_ib_port_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND ?
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000385 ib_link_query_port(ibdev, port, props, netw_view) :
386 eth_link_query_port(ibdev, port, props, netw_view);
Roland Dreier225c7b12007-05-08 18:00:38 -0700387
388 return err;
389}
390
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000391static int mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
392 struct ib_port_attr *props)
393{
394 /* returns host view */
395 return __mlx4_ib_query_port(ibdev, port, props, 0);
396}
397
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000398int __mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
399 union ib_gid *gid, int netw_view)
Roland Dreier225c7b12007-05-08 18:00:38 -0700400{
401 struct ib_smp *in_mad = NULL;
402 struct ib_smp *out_mad = NULL;
403 int err = -ENOMEM;
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000404 struct mlx4_ib_dev *dev = to_mdev(ibdev);
405 int clear = 0;
406 int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
Roland Dreier225c7b12007-05-08 18:00:38 -0700407
408 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
409 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
410 if (!in_mad || !out_mad)
411 goto out;
412
413 init_query_mad(in_mad);
414 in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
415 in_mad->attr_mod = cpu_to_be32(port);
416
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000417 if (mlx4_is_mfunc(dev->dev) && netw_view)
418 mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
419
420 err = mlx4_MAD_IFC(dev, mad_ifc_flags, port, NULL, NULL, in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -0700421 if (err)
422 goto out;
423
424 memcpy(gid->raw, out_mad->data + 8, 8);
425
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000426 if (mlx4_is_mfunc(dev->dev) && !netw_view) {
427 if (index) {
428 /* For any index > 0, return the null guid */
429 err = 0;
430 clear = 1;
431 goto out;
432 }
433 }
434
Roland Dreier225c7b12007-05-08 18:00:38 -0700435 init_query_mad(in_mad);
436 in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
437 in_mad->attr_mod = cpu_to_be32(index / 8);
438
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000439 err = mlx4_MAD_IFC(dev, mad_ifc_flags, port,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000440 NULL, NULL, in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -0700441 if (err)
442 goto out;
443
444 memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
445
446out:
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000447 if (clear)
448 memset(gid->raw + 8, 0, 8);
Roland Dreier225c7b12007-05-08 18:00:38 -0700449 kfree(in_mad);
450 kfree(out_mad);
451 return err;
452}
453
Eli Cohenfa417f72010-10-24 21:08:52 -0700454static int iboe_query_gid(struct ib_device *ibdev, u8 port, int index,
455 union ib_gid *gid)
456{
457 struct mlx4_ib_dev *dev = to_mdev(ibdev);
458
459 *gid = dev->iboe.gid_table[port - 1][index];
460
461 return 0;
462}
463
464static int mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
465 union ib_gid *gid)
466{
467 if (rdma_port_get_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND)
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000468 return __mlx4_ib_query_gid(ibdev, port, index, gid, 0);
Eli Cohenfa417f72010-10-24 21:08:52 -0700469 else
470 return iboe_query_gid(ibdev, port, index, gid);
471}
472
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000473int __mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
474 u16 *pkey, int netw_view)
Roland Dreier225c7b12007-05-08 18:00:38 -0700475{
476 struct ib_smp *in_mad = NULL;
477 struct ib_smp *out_mad = NULL;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000478 int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
Roland Dreier225c7b12007-05-08 18:00:38 -0700479 int err = -ENOMEM;
480
481 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
482 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
483 if (!in_mad || !out_mad)
484 goto out;
485
486 init_query_mad(in_mad);
487 in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
488 in_mad->attr_mod = cpu_to_be32(index / 32);
489
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000490 if (mlx4_is_mfunc(to_mdev(ibdev)->dev) && netw_view)
491 mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
492
493 err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port, NULL, NULL,
494 in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -0700495 if (err)
496 goto out;
497
498 *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
499
500out:
501 kfree(in_mad);
502 kfree(out_mad);
503 return err;
504}
505
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000506static int mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
507{
508 return __mlx4_ib_query_pkey(ibdev, port, index, pkey, 0);
509}
510
Roland Dreier225c7b12007-05-08 18:00:38 -0700511static int mlx4_ib_modify_device(struct ib_device *ibdev, int mask,
512 struct ib_device_modify *props)
513{
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000514 struct mlx4_cmd_mailbox *mailbox;
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000515 unsigned long flags;
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000516
Roland Dreier225c7b12007-05-08 18:00:38 -0700517 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
518 return -EOPNOTSUPP;
519
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000520 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
521 return 0;
522
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +0000523 if (mlx4_is_slave(to_mdev(ibdev)->dev))
524 return -EOPNOTSUPP;
525
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000526 spin_lock_irqsave(&to_mdev(ibdev)->sm_lock, flags);
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000527 memcpy(ibdev->node_desc, props->node_desc, 64);
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000528 spin_unlock_irqrestore(&to_mdev(ibdev)->sm_lock, flags);
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000529
530 /*
531 * If possible, pass node desc to FW, so it can generate
532 * a 144 trap. If cmd fails, just ignore.
533 */
534 mailbox = mlx4_alloc_cmd_mailbox(to_mdev(ibdev)->dev);
535 if (IS_ERR(mailbox))
536 return 0;
537
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000538 memcpy(mailbox->buf, props->node_desc, 64);
539 mlx4_cmd(to_mdev(ibdev)->dev, mailbox->dma, 1, 0,
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +0000540 MLX4_CMD_SET_NODE, MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000541
542 mlx4_free_cmd_mailbox(to_mdev(ibdev)->dev, mailbox);
Roland Dreier225c7b12007-05-08 18:00:38 -0700543
544 return 0;
545}
546
547static int mlx4_SET_PORT(struct mlx4_ib_dev *dev, u8 port, int reset_qkey_viols,
548 u32 cap_mask)
549{
550 struct mlx4_cmd_mailbox *mailbox;
551 int err;
Eli Cohenfa417f72010-10-24 21:08:52 -0700552 u8 is_eth = dev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
Roland Dreier225c7b12007-05-08 18:00:38 -0700553
554 mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
555 if (IS_ERR(mailbox))
556 return PTR_ERR(mailbox);
557
Roland Dreier5ae2a7a2007-06-18 08:15:02 -0700558 if (dev->dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
559 *(u8 *) mailbox->buf = !!reset_qkey_viols << 6;
560 ((__be32 *) mailbox->buf)[2] = cpu_to_be32(cap_mask);
561 } else {
562 ((u8 *) mailbox->buf)[3] = !!reset_qkey_viols;
563 ((__be32 *) mailbox->buf)[1] = cpu_to_be32(cap_mask);
564 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700565
Eli Cohenfa417f72010-10-24 21:08:52 -0700566 err = mlx4_cmd(dev->dev, mailbox->dma, port, is_eth, MLX4_CMD_SET_PORT,
Jack Morgensteinf9baff52011-12-13 04:10:51 +0000567 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
Roland Dreier225c7b12007-05-08 18:00:38 -0700568
569 mlx4_free_cmd_mailbox(dev->dev, mailbox);
570 return err;
571}
572
573static int mlx4_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
574 struct ib_port_modify *props)
575{
576 struct ib_port_attr attr;
577 u32 cap_mask;
578 int err;
579
580 mutex_lock(&to_mdev(ibdev)->cap_mask_mutex);
581
582 err = mlx4_ib_query_port(ibdev, port, &attr);
583 if (err)
584 goto out;
585
586 cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
587 ~props->clr_port_cap_mask;
588
589 err = mlx4_SET_PORT(to_mdev(ibdev), port,
590 !!(mask & IB_PORT_RESET_QKEY_CNTR),
591 cap_mask);
592
593out:
594 mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
595 return err;
596}
597
598static struct ib_ucontext *mlx4_ib_alloc_ucontext(struct ib_device *ibdev,
599 struct ib_udata *udata)
600{
601 struct mlx4_ib_dev *dev = to_mdev(ibdev);
602 struct mlx4_ib_ucontext *context;
Or Gerlitz08ff3232012-10-21 14:59:24 +0000603 struct mlx4_ib_alloc_ucontext_resp_v3 resp_v3;
Roland Dreier225c7b12007-05-08 18:00:38 -0700604 struct mlx4_ib_alloc_ucontext_resp resp;
605 int err;
606
Jack Morgenstein3b4a8cd2009-09-05 20:24:50 -0700607 if (!dev->ib_active)
608 return ERR_PTR(-EAGAIN);
609
Or Gerlitz08ff3232012-10-21 14:59:24 +0000610 if (ibdev->uverbs_abi_ver == MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION) {
611 resp_v3.qp_tab_size = dev->dev->caps.num_qps;
612 resp_v3.bf_reg_size = dev->dev->caps.bf_reg_size;
613 resp_v3.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
614 } else {
615 resp.dev_caps = dev->dev->caps.userspace_caps;
616 resp.qp_tab_size = dev->dev->caps.num_qps;
617 resp.bf_reg_size = dev->dev->caps.bf_reg_size;
618 resp.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
619 resp.cqe_size = dev->dev->caps.cqe_size;
620 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700621
622 context = kmalloc(sizeof *context, GFP_KERNEL);
623 if (!context)
624 return ERR_PTR(-ENOMEM);
625
626 err = mlx4_uar_alloc(to_mdev(ibdev)->dev, &context->uar);
627 if (err) {
628 kfree(context);
629 return ERR_PTR(err);
630 }
631
632 INIT_LIST_HEAD(&context->db_page_list);
633 mutex_init(&context->db_page_mutex);
634
Or Gerlitz08ff3232012-10-21 14:59:24 +0000635 if (ibdev->uverbs_abi_ver == MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION)
636 err = ib_copy_to_udata(udata, &resp_v3, sizeof(resp_v3));
637 else
638 err = ib_copy_to_udata(udata, &resp, sizeof(resp));
639
Roland Dreier225c7b12007-05-08 18:00:38 -0700640 if (err) {
641 mlx4_uar_free(to_mdev(ibdev)->dev, &context->uar);
642 kfree(context);
643 return ERR_PTR(-EFAULT);
644 }
645
646 return &context->ibucontext;
647}
648
649static int mlx4_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
650{
651 struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
652
653 mlx4_uar_free(to_mdev(ibcontext->device)->dev, &context->uar);
654 kfree(context);
655
656 return 0;
657}
658
659static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
660{
661 struct mlx4_ib_dev *dev = to_mdev(context->device);
662
663 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
664 return -EINVAL;
665
666 if (vma->vm_pgoff == 0) {
667 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
668
669 if (io_remap_pfn_range(vma, vma->vm_start,
670 to_mucontext(context)->uar.pfn,
671 PAGE_SIZE, vma->vm_page_prot))
672 return -EAGAIN;
673 } else if (vma->vm_pgoff == 1 && dev->dev->caps.bf_reg_size != 0) {
Roland Dreiere1d60ec2009-03-30 08:31:05 -0700674 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
Roland Dreier225c7b12007-05-08 18:00:38 -0700675
676 if (io_remap_pfn_range(vma, vma->vm_start,
677 to_mucontext(context)->uar.pfn +
678 dev->dev->caps.num_uars,
679 PAGE_SIZE, vma->vm_page_prot))
680 return -EAGAIN;
681 } else
682 return -EINVAL;
683
684 return 0;
685}
686
687static struct ib_pd *mlx4_ib_alloc_pd(struct ib_device *ibdev,
688 struct ib_ucontext *context,
689 struct ib_udata *udata)
690{
691 struct mlx4_ib_pd *pd;
692 int err;
693
694 pd = kmalloc(sizeof *pd, GFP_KERNEL);
695 if (!pd)
696 return ERR_PTR(-ENOMEM);
697
698 err = mlx4_pd_alloc(to_mdev(ibdev)->dev, &pd->pdn);
699 if (err) {
700 kfree(pd);
701 return ERR_PTR(err);
702 }
703
704 if (context)
705 if (ib_copy_to_udata(udata, &pd->pdn, sizeof (__u32))) {
706 mlx4_pd_free(to_mdev(ibdev)->dev, pd->pdn);
707 kfree(pd);
708 return ERR_PTR(-EFAULT);
709 }
710
711 return &pd->ibpd;
712}
713
714static int mlx4_ib_dealloc_pd(struct ib_pd *pd)
715{
716 mlx4_pd_free(to_mdev(pd->device)->dev, to_mpd(pd)->pdn);
717 kfree(pd);
718
719 return 0;
720}
721
Sean Hefty012a8ff2011-06-02 09:01:33 -0700722static struct ib_xrcd *mlx4_ib_alloc_xrcd(struct ib_device *ibdev,
723 struct ib_ucontext *context,
724 struct ib_udata *udata)
725{
726 struct mlx4_ib_xrcd *xrcd;
727 int err;
728
729 if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC))
730 return ERR_PTR(-ENOSYS);
731
732 xrcd = kmalloc(sizeof *xrcd, GFP_KERNEL);
733 if (!xrcd)
734 return ERR_PTR(-ENOMEM);
735
736 err = mlx4_xrcd_alloc(to_mdev(ibdev)->dev, &xrcd->xrcdn);
737 if (err)
738 goto err1;
739
740 xrcd->pd = ib_alloc_pd(ibdev);
741 if (IS_ERR(xrcd->pd)) {
742 err = PTR_ERR(xrcd->pd);
743 goto err2;
744 }
745
746 xrcd->cq = ib_create_cq(ibdev, NULL, NULL, xrcd, 1, 0);
747 if (IS_ERR(xrcd->cq)) {
748 err = PTR_ERR(xrcd->cq);
749 goto err3;
750 }
751
752 return &xrcd->ibxrcd;
753
754err3:
755 ib_dealloc_pd(xrcd->pd);
756err2:
757 mlx4_xrcd_free(to_mdev(ibdev)->dev, xrcd->xrcdn);
758err1:
759 kfree(xrcd);
760 return ERR_PTR(err);
761}
762
763static int mlx4_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
764{
765 ib_destroy_cq(to_mxrcd(xrcd)->cq);
766 ib_dealloc_pd(to_mxrcd(xrcd)->pd);
767 mlx4_xrcd_free(to_mdev(xrcd->device)->dev, to_mxrcd(xrcd)->xrcdn);
768 kfree(xrcd);
769
770 return 0;
771}
772
Eli Cohenfa417f72010-10-24 21:08:52 -0700773static int add_gid_entry(struct ib_qp *ibqp, union ib_gid *gid)
774{
775 struct mlx4_ib_qp *mqp = to_mqp(ibqp);
776 struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
777 struct mlx4_ib_gid_entry *ge;
778
779 ge = kzalloc(sizeof *ge, GFP_KERNEL);
780 if (!ge)
781 return -ENOMEM;
782
783 ge->gid = *gid;
784 if (mlx4_ib_add_mc(mdev, mqp, gid)) {
785 ge->port = mqp->port;
786 ge->added = 1;
787 }
788
789 mutex_lock(&mqp->mutex);
790 list_add_tail(&ge->list, &mqp->gid_list);
791 mutex_unlock(&mqp->mutex);
792
793 return 0;
794}
795
796int mlx4_ib_add_mc(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
797 union ib_gid *gid)
798{
Eli Cohenfa417f72010-10-24 21:08:52 -0700799 struct net_device *ndev;
800 int ret = 0;
801
802 if (!mqp->port)
803 return 0;
804
805 spin_lock(&mdev->iboe.lock);
806 ndev = mdev->iboe.netdevs[mqp->port - 1];
807 if (ndev)
808 dev_hold(ndev);
809 spin_unlock(&mdev->iboe.lock);
810
811 if (ndev) {
Eli Cohenfa417f72010-10-24 21:08:52 -0700812 ret = 1;
Eli Cohenfa417f72010-10-24 21:08:52 -0700813 dev_put(ndev);
814 }
815
816 return ret;
817}
818
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +0000819struct mlx4_ib_steering {
820 struct list_head list;
821 u64 reg_id;
822 union ib_gid gid;
823};
824
Hadar Hen Zionf77c0162013-08-14 13:58:31 +0300825static int parse_flow_attr(struct mlx4_dev *dev,
Matan Baraka37a1a42013-11-07 15:25:16 +0200826 u32 qp_num,
Hadar Hen Zionf77c0162013-08-14 13:58:31 +0300827 union ib_flow_spec *ib_spec,
828 struct _rule_hw *mlx4_spec)
829{
830 enum mlx4_net_trans_rule_id type;
831
832 switch (ib_spec->type) {
833 case IB_FLOW_SPEC_ETH:
834 type = MLX4_NET_TRANS_RULE_ID_ETH;
835 memcpy(mlx4_spec->eth.dst_mac, ib_spec->eth.val.dst_mac,
836 ETH_ALEN);
837 memcpy(mlx4_spec->eth.dst_mac_msk, ib_spec->eth.mask.dst_mac,
838 ETH_ALEN);
839 mlx4_spec->eth.vlan_tag = ib_spec->eth.val.vlan_tag;
840 mlx4_spec->eth.vlan_tag_msk = ib_spec->eth.mask.vlan_tag;
841 break;
Matan Baraka37a1a42013-11-07 15:25:16 +0200842 case IB_FLOW_SPEC_IB:
843 type = MLX4_NET_TRANS_RULE_ID_IB;
844 mlx4_spec->ib.l3_qpn =
845 cpu_to_be32(qp_num);
846 mlx4_spec->ib.qpn_mask =
847 cpu_to_be32(MLX4_IB_FLOW_QPN_MASK);
848 break;
849
Hadar Hen Zionf77c0162013-08-14 13:58:31 +0300850
851 case IB_FLOW_SPEC_IPV4:
852 type = MLX4_NET_TRANS_RULE_ID_IPV4;
853 mlx4_spec->ipv4.src_ip = ib_spec->ipv4.val.src_ip;
854 mlx4_spec->ipv4.src_ip_msk = ib_spec->ipv4.mask.src_ip;
855 mlx4_spec->ipv4.dst_ip = ib_spec->ipv4.val.dst_ip;
856 mlx4_spec->ipv4.dst_ip_msk = ib_spec->ipv4.mask.dst_ip;
857 break;
858
859 case IB_FLOW_SPEC_TCP:
860 case IB_FLOW_SPEC_UDP:
861 type = ib_spec->type == IB_FLOW_SPEC_TCP ?
862 MLX4_NET_TRANS_RULE_ID_TCP :
863 MLX4_NET_TRANS_RULE_ID_UDP;
864 mlx4_spec->tcp_udp.dst_port = ib_spec->tcp_udp.val.dst_port;
865 mlx4_spec->tcp_udp.dst_port_msk = ib_spec->tcp_udp.mask.dst_port;
866 mlx4_spec->tcp_udp.src_port = ib_spec->tcp_udp.val.src_port;
867 mlx4_spec->tcp_udp.src_port_msk = ib_spec->tcp_udp.mask.src_port;
868 break;
869
870 default:
871 return -EINVAL;
872 }
873 if (mlx4_map_sw_to_hw_steering_id(dev, type) < 0 ||
874 mlx4_hw_rule_sz(dev, type) < 0)
875 return -EINVAL;
876 mlx4_spec->id = cpu_to_be16(mlx4_map_sw_to_hw_steering_id(dev, type));
877 mlx4_spec->size = mlx4_hw_rule_sz(dev, type) >> 2;
878 return mlx4_hw_rule_sz(dev, type);
879}
880
Matan Baraka37a1a42013-11-07 15:25:16 +0200881struct default_rules {
882 __u32 mandatory_fields[IB_FLOW_SPEC_SUPPORT_LAYERS];
883 __u32 mandatory_not_fields[IB_FLOW_SPEC_SUPPORT_LAYERS];
884 __u32 rules_create_list[IB_FLOW_SPEC_SUPPORT_LAYERS];
885 __u8 link_layer;
886};
887static const struct default_rules default_table[] = {
888 {
889 .mandatory_fields = {IB_FLOW_SPEC_IPV4},
890 .mandatory_not_fields = {IB_FLOW_SPEC_ETH},
891 .rules_create_list = {IB_FLOW_SPEC_IB},
892 .link_layer = IB_LINK_LAYER_INFINIBAND
893 }
894};
895
896static int __mlx4_ib_default_rules_match(struct ib_qp *qp,
897 struct ib_flow_attr *flow_attr)
898{
899 int i, j, k;
900 void *ib_flow;
901 const struct default_rules *pdefault_rules = default_table;
902 u8 link_layer = rdma_port_get_link_layer(qp->device, flow_attr->port);
903
904 for (i = 0; i < sizeof(default_table)/sizeof(default_table[0]); i++,
905 pdefault_rules++) {
906 __u32 field_types[IB_FLOW_SPEC_SUPPORT_LAYERS];
907 memset(&field_types, 0, sizeof(field_types));
908
909 if (link_layer != pdefault_rules->link_layer)
910 continue;
911
912 ib_flow = flow_attr + 1;
913 /* we assume the specs are sorted */
914 for (j = 0, k = 0; k < IB_FLOW_SPEC_SUPPORT_LAYERS &&
915 j < flow_attr->num_of_specs; k++) {
916 union ib_flow_spec *current_flow =
917 (union ib_flow_spec *)ib_flow;
918
919 /* same layer but different type */
920 if (((current_flow->type & IB_FLOW_SPEC_LAYER_MASK) ==
921 (pdefault_rules->mandatory_fields[k] &
922 IB_FLOW_SPEC_LAYER_MASK)) &&
923 (current_flow->type !=
924 pdefault_rules->mandatory_fields[k]))
925 goto out;
926
927 /* same layer, try match next one */
928 if (current_flow->type ==
929 pdefault_rules->mandatory_fields[k]) {
930 j++;
931 ib_flow +=
932 ((union ib_flow_spec *)ib_flow)->size;
933 }
934 }
935
936 ib_flow = flow_attr + 1;
937 for (j = 0; j < flow_attr->num_of_specs;
938 j++, ib_flow += ((union ib_flow_spec *)ib_flow)->size)
939 for (k = 0; k < IB_FLOW_SPEC_SUPPORT_LAYERS; k++)
940 /* same layer and same type */
941 if (((union ib_flow_spec *)ib_flow)->type ==
942 pdefault_rules->mandatory_not_fields[k])
943 goto out;
944
945 return i;
946 }
947out:
948 return -1;
949}
950
951static int __mlx4_ib_create_default_rules(
952 struct mlx4_ib_dev *mdev,
953 struct ib_qp *qp,
954 const struct default_rules *pdefault_rules,
955 struct _rule_hw *mlx4_spec) {
956 int size = 0;
957 int i;
958
959 for (i = 0; i < sizeof(pdefault_rules->rules_create_list)/
960 sizeof(pdefault_rules->rules_create_list[0]); i++) {
961 int ret;
962 union ib_flow_spec ib_spec;
963 switch (pdefault_rules->rules_create_list[i]) {
964 case 0:
965 /* no rule */
966 continue;
967 case IB_FLOW_SPEC_IB:
968 ib_spec.type = IB_FLOW_SPEC_IB;
969 ib_spec.size = sizeof(struct ib_flow_spec_ib);
970
971 break;
972 default:
973 /* invalid rule */
974 return -EINVAL;
975 }
976 /* We must put empty rule, qpn is being ignored */
977 ret = parse_flow_attr(mdev->dev, 0, &ib_spec,
978 mlx4_spec);
979 if (ret < 0) {
980 pr_info("invalid parsing\n");
981 return -EINVAL;
982 }
983
984 mlx4_spec = (void *)mlx4_spec + ret;
985 size += ret;
986 }
987 return size;
988}
989
Hadar Hen Zionf77c0162013-08-14 13:58:31 +0300990static int __mlx4_ib_create_flow(struct ib_qp *qp, struct ib_flow_attr *flow_attr,
991 int domain,
992 enum mlx4_net_trans_promisc_mode flow_type,
993 u64 *reg_id)
994{
995 int ret, i;
996 int size = 0;
997 void *ib_flow;
998 struct mlx4_ib_dev *mdev = to_mdev(qp->device);
999 struct mlx4_cmd_mailbox *mailbox;
1000 struct mlx4_net_trans_rule_hw_ctrl *ctrl;
Matan Baraka37a1a42013-11-07 15:25:16 +02001001 int default_flow;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001002
1003 static const u16 __mlx4_domain[] = {
1004 [IB_FLOW_DOMAIN_USER] = MLX4_DOMAIN_UVERBS,
1005 [IB_FLOW_DOMAIN_ETHTOOL] = MLX4_DOMAIN_ETHTOOL,
1006 [IB_FLOW_DOMAIN_RFS] = MLX4_DOMAIN_RFS,
1007 [IB_FLOW_DOMAIN_NIC] = MLX4_DOMAIN_NIC,
1008 };
1009
1010 if (flow_attr->priority > MLX4_IB_FLOW_MAX_PRIO) {
1011 pr_err("Invalid priority value %d\n", flow_attr->priority);
1012 return -EINVAL;
1013 }
1014
1015 if (domain >= IB_FLOW_DOMAIN_NUM) {
1016 pr_err("Invalid domain value %d\n", domain);
1017 return -EINVAL;
1018 }
1019
1020 if (mlx4_map_sw_to_hw_steering_mode(mdev->dev, flow_type) < 0)
1021 return -EINVAL;
1022
1023 mailbox = mlx4_alloc_cmd_mailbox(mdev->dev);
1024 if (IS_ERR(mailbox))
1025 return PTR_ERR(mailbox);
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001026 ctrl = mailbox->buf;
1027
1028 ctrl->prio = cpu_to_be16(__mlx4_domain[domain] |
1029 flow_attr->priority);
1030 ctrl->type = mlx4_map_sw_to_hw_steering_mode(mdev->dev, flow_type);
1031 ctrl->port = flow_attr->port;
1032 ctrl->qpn = cpu_to_be32(qp->qp_num);
1033
1034 ib_flow = flow_attr + 1;
1035 size += sizeof(struct mlx4_net_trans_rule_hw_ctrl);
Matan Baraka37a1a42013-11-07 15:25:16 +02001036 /* Add default flows */
1037 default_flow = __mlx4_ib_default_rules_match(qp, flow_attr);
1038 if (default_flow >= 0) {
1039 ret = __mlx4_ib_create_default_rules(
1040 mdev, qp, default_table + default_flow,
1041 mailbox->buf + size);
1042 if (ret < 0) {
1043 mlx4_free_cmd_mailbox(mdev->dev, mailbox);
1044 return -EINVAL;
1045 }
1046 size += ret;
1047 }
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001048 for (i = 0; i < flow_attr->num_of_specs; i++) {
Matan Baraka37a1a42013-11-07 15:25:16 +02001049 ret = parse_flow_attr(mdev->dev, qp->qp_num, ib_flow,
1050 mailbox->buf + size);
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001051 if (ret < 0) {
1052 mlx4_free_cmd_mailbox(mdev->dev, mailbox);
1053 return -EINVAL;
1054 }
1055 ib_flow += ((union ib_flow_spec *) ib_flow)->size;
1056 size += ret;
1057 }
1058
1059 ret = mlx4_cmd_imm(mdev->dev, mailbox->dma, reg_id, size >> 2, 0,
1060 MLX4_QP_FLOW_STEERING_ATTACH, MLX4_CMD_TIME_CLASS_A,
1061 MLX4_CMD_NATIVE);
1062 if (ret == -ENOMEM)
1063 pr_err("mcg table is full. Fail to register network rule.\n");
1064 else if (ret == -ENXIO)
1065 pr_err("Device managed flow steering is disabled. Fail to register network rule.\n");
1066 else if (ret)
1067 pr_err("Invalid argumant. Fail to register network rule.\n");
1068
1069 mlx4_free_cmd_mailbox(mdev->dev, mailbox);
1070 return ret;
1071}
1072
1073static int __mlx4_ib_destroy_flow(struct mlx4_dev *dev, u64 reg_id)
1074{
1075 int err;
1076 err = mlx4_cmd(dev, reg_id, 0, 0,
1077 MLX4_QP_FLOW_STEERING_DETACH, MLX4_CMD_TIME_CLASS_A,
1078 MLX4_CMD_NATIVE);
1079 if (err)
1080 pr_err("Fail to detach network rule. registration id = 0x%llx\n",
1081 reg_id);
1082 return err;
1083}
1084
1085static struct ib_flow *mlx4_ib_create_flow(struct ib_qp *qp,
1086 struct ib_flow_attr *flow_attr,
1087 int domain)
1088{
1089 int err = 0, i = 0;
1090 struct mlx4_ib_flow *mflow;
1091 enum mlx4_net_trans_promisc_mode type[2];
1092
1093 memset(type, 0, sizeof(type));
1094
1095 mflow = kzalloc(sizeof(*mflow), GFP_KERNEL);
1096 if (!mflow) {
1097 err = -ENOMEM;
1098 goto err_free;
1099 }
1100
1101 switch (flow_attr->type) {
1102 case IB_FLOW_ATTR_NORMAL:
1103 type[0] = MLX4_FS_REGULAR;
1104 break;
1105
1106 case IB_FLOW_ATTR_ALL_DEFAULT:
1107 type[0] = MLX4_FS_ALL_DEFAULT;
1108 break;
1109
1110 case IB_FLOW_ATTR_MC_DEFAULT:
1111 type[0] = MLX4_FS_MC_DEFAULT;
1112 break;
1113
1114 case IB_FLOW_ATTR_SNIFFER:
1115 type[0] = MLX4_FS_UC_SNIFFER;
1116 type[1] = MLX4_FS_MC_SNIFFER;
1117 break;
1118
1119 default:
1120 err = -EINVAL;
1121 goto err_free;
1122 }
1123
1124 while (i < ARRAY_SIZE(type) && type[i]) {
1125 err = __mlx4_ib_create_flow(qp, flow_attr, domain, type[i],
1126 &mflow->reg_id[i]);
1127 if (err)
1128 goto err_free;
1129 i++;
1130 }
1131
1132 return &mflow->ibflow;
1133
1134err_free:
1135 kfree(mflow);
1136 return ERR_PTR(err);
1137}
1138
1139static int mlx4_ib_destroy_flow(struct ib_flow *flow_id)
1140{
1141 int err, ret = 0;
1142 int i = 0;
1143 struct mlx4_ib_dev *mdev = to_mdev(flow_id->qp->device);
1144 struct mlx4_ib_flow *mflow = to_mflow(flow_id);
1145
1146 while (i < ARRAY_SIZE(mflow->reg_id) && mflow->reg_id[i]) {
1147 err = __mlx4_ib_destroy_flow(mdev->dev, mflow->reg_id[i]);
1148 if (err)
1149 ret = err;
1150 i++;
1151 }
1152
1153 kfree(mflow);
1154 return ret;
1155}
1156
Roland Dreier225c7b12007-05-08 18:00:38 -07001157static int mlx4_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
1158{
Eli Cohenfa417f72010-10-24 21:08:52 -07001159 int err;
1160 struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
1161 struct mlx4_ib_qp *mqp = to_mqp(ibqp);
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001162 u64 reg_id;
1163 struct mlx4_ib_steering *ib_steering = NULL;
Moni Shouad487ee72013-12-12 18:03:13 +02001164 enum mlx4_protocol prot = (gid->raw[1] == 0x0e) ?
1165 MLX4_PROT_IB_IPV4 : MLX4_PROT_IB_IPV6;
Eli Cohenfa417f72010-10-24 21:08:52 -07001166
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001167 if (mdev->dev->caps.steering_mode ==
1168 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1169 ib_steering = kmalloc(sizeof(*ib_steering), GFP_KERNEL);
1170 if (!ib_steering)
1171 return -ENOMEM;
1172 }
1173
1174 err = mlx4_multicast_attach(mdev->dev, &mqp->mqp, gid->raw, mqp->port,
1175 !!(mqp->flags &
1176 MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK),
Moni Shouad487ee72013-12-12 18:03:13 +02001177 prot, &reg_id);
Eli Cohenfa417f72010-10-24 21:08:52 -07001178 if (err)
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001179 goto err_malloc;
Eli Cohenfa417f72010-10-24 21:08:52 -07001180
1181 err = add_gid_entry(ibqp, gid);
1182 if (err)
1183 goto err_add;
1184
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001185 if (ib_steering) {
1186 memcpy(ib_steering->gid.raw, gid->raw, 16);
1187 ib_steering->reg_id = reg_id;
1188 mutex_lock(&mqp->mutex);
1189 list_add(&ib_steering->list, &mqp->steering_rules);
1190 mutex_unlock(&mqp->mutex);
1191 }
Eli Cohenfa417f72010-10-24 21:08:52 -07001192 return 0;
1193
1194err_add:
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001195 mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
Moni Shouad487ee72013-12-12 18:03:13 +02001196 prot, reg_id);
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001197err_malloc:
1198 kfree(ib_steering);
1199
Eli Cohenfa417f72010-10-24 21:08:52 -07001200 return err;
1201}
1202
1203static struct mlx4_ib_gid_entry *find_gid_entry(struct mlx4_ib_qp *qp, u8 *raw)
1204{
1205 struct mlx4_ib_gid_entry *ge;
1206 struct mlx4_ib_gid_entry *tmp;
1207 struct mlx4_ib_gid_entry *ret = NULL;
1208
1209 list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
1210 if (!memcmp(raw, ge->gid.raw, 16)) {
1211 ret = ge;
1212 break;
1213 }
1214 }
1215
1216 return ret;
Roland Dreier225c7b12007-05-08 18:00:38 -07001217}
1218
1219static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
1220{
Eli Cohenfa417f72010-10-24 21:08:52 -07001221 int err;
1222 struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
1223 struct mlx4_ib_qp *mqp = to_mqp(ibqp);
Eli Cohenfa417f72010-10-24 21:08:52 -07001224 struct net_device *ndev;
1225 struct mlx4_ib_gid_entry *ge;
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001226 u64 reg_id = 0;
Moni Shouad487ee72013-12-12 18:03:13 +02001227 enum mlx4_protocol prot = (gid->raw[1] == 0x0e) ?
1228 MLX4_PROT_IB_IPV4 : MLX4_PROT_IB_IPV6;
Eli Cohenfa417f72010-10-24 21:08:52 -07001229
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001230 if (mdev->dev->caps.steering_mode ==
1231 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1232 struct mlx4_ib_steering *ib_steering;
1233
1234 mutex_lock(&mqp->mutex);
1235 list_for_each_entry(ib_steering, &mqp->steering_rules, list) {
1236 if (!memcmp(ib_steering->gid.raw, gid->raw, 16)) {
1237 list_del(&ib_steering->list);
1238 break;
1239 }
1240 }
1241 mutex_unlock(&mqp->mutex);
1242 if (&ib_steering->list == &mqp->steering_rules) {
1243 pr_err("Couldn't find reg_id for mgid. Steering rule is left attached\n");
1244 return -EINVAL;
1245 }
1246 reg_id = ib_steering->reg_id;
1247 kfree(ib_steering);
1248 }
1249
1250 err = mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
Moni Shouad487ee72013-12-12 18:03:13 +02001251 prot, reg_id);
Eli Cohenfa417f72010-10-24 21:08:52 -07001252 if (err)
1253 return err;
1254
1255 mutex_lock(&mqp->mutex);
1256 ge = find_gid_entry(mqp, gid->raw);
1257 if (ge) {
1258 spin_lock(&mdev->iboe.lock);
1259 ndev = ge->added ? mdev->iboe.netdevs[ge->port - 1] : NULL;
1260 if (ndev)
1261 dev_hold(ndev);
1262 spin_unlock(&mdev->iboe.lock);
Moni Shouad487ee72013-12-12 18:03:13 +02001263 if (ndev)
Eli Cohenfa417f72010-10-24 21:08:52 -07001264 dev_put(ndev);
Eli Cohenfa417f72010-10-24 21:08:52 -07001265 list_del(&ge->list);
1266 kfree(ge);
1267 } else
Shlomo Pongratz987c8f82012-04-29 17:04:26 +03001268 pr_warn("could not find mgid entry\n");
Eli Cohenfa417f72010-10-24 21:08:52 -07001269
1270 mutex_unlock(&mqp->mutex);
1271
1272 return 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07001273}
1274
1275static int init_node_data(struct mlx4_ib_dev *dev)
1276{
1277 struct ib_smp *in_mad = NULL;
1278 struct ib_smp *out_mad = NULL;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00001279 int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
Roland Dreier225c7b12007-05-08 18:00:38 -07001280 int err = -ENOMEM;
1281
1282 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
1283 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
1284 if (!in_mad || !out_mad)
1285 goto out;
1286
1287 init_query_mad(in_mad);
1288 in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00001289 if (mlx4_is_master(dev->dev))
1290 mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
Roland Dreier225c7b12007-05-08 18:00:38 -07001291
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00001292 err = mlx4_MAD_IFC(dev, mad_ifc_flags, 1, NULL, NULL, in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -07001293 if (err)
1294 goto out;
1295
1296 memcpy(dev->ib_dev.node_desc, out_mad->data, 64);
1297
1298 in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
1299
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00001300 err = mlx4_MAD_IFC(dev, mad_ifc_flags, 1, NULL, NULL, in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -07001301 if (err)
1302 goto out;
1303
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +00001304 dev->dev->rev_id = be32_to_cpup((__be32 *) (out_mad->data + 32));
Roland Dreier225c7b12007-05-08 18:00:38 -07001305 memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
1306
1307out:
1308 kfree(in_mad);
1309 kfree(out_mad);
1310 return err;
1311}
1312
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001313static ssize_t show_hca(struct device *device, struct device_attribute *attr,
1314 char *buf)
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001315{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001316 struct mlx4_ib_dev *dev =
1317 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001318 return sprintf(buf, "MT%d\n", dev->dev->pdev->device);
1319}
1320
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001321static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
1322 char *buf)
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001323{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001324 struct mlx4_ib_dev *dev =
1325 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001326 return sprintf(buf, "%d.%d.%d\n", (int) (dev->dev->caps.fw_ver >> 32),
1327 (int) (dev->dev->caps.fw_ver >> 16) & 0xffff,
1328 (int) dev->dev->caps.fw_ver & 0xffff);
1329}
1330
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001331static ssize_t show_rev(struct device *device, struct device_attribute *attr,
1332 char *buf)
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001333{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001334 struct mlx4_ib_dev *dev =
1335 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001336 return sprintf(buf, "%x\n", dev->dev->rev_id);
1337}
1338
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001339static ssize_t show_board(struct device *device, struct device_attribute *attr,
1340 char *buf)
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001341{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001342 struct mlx4_ib_dev *dev =
1343 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
1344 return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN,
1345 dev->dev->board_id);
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001346}
1347
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001348static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
1349static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
1350static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
1351static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001352
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001353static struct device_attribute *mlx4_class_attributes[] = {
1354 &dev_attr_hw_rev,
1355 &dev_attr_fw_ver,
1356 &dev_attr_hca_type,
1357 &dev_attr_board_id
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001358};
1359
Moni Shouaacc4fcc2014-02-05 15:12:58 +02001360static void mlx4_addrconf_ifid_eui48(u8 *eui, u16 vlan_id,
1361 struct net_device *dev)
1362{
1363 memcpy(eui, dev->dev_addr, 3);
1364 memcpy(eui + 5, dev->dev_addr + 3, 3);
1365 if (vlan_id < 0x1000) {
1366 eui[3] = vlan_id >> 8;
1367 eui[4] = vlan_id & 0xff;
1368 } else {
1369 eui[3] = 0xff;
1370 eui[4] = 0xfe;
1371 }
1372 eui[0] ^= 2;
1373}
1374
Eli Cohenfa417f72010-10-24 21:08:52 -07001375static void update_gids_task(struct work_struct *work)
1376{
1377 struct update_gid_work *gw = container_of(work, struct update_gid_work, work);
1378 struct mlx4_cmd_mailbox *mailbox;
1379 union ib_gid *gids;
1380 int err;
1381 struct mlx4_dev *dev = gw->dev->dev;
Eli Cohenfa417f72010-10-24 21:08:52 -07001382
1383 mailbox = mlx4_alloc_cmd_mailbox(dev);
1384 if (IS_ERR(mailbox)) {
Shlomo Pongratz987c8f82012-04-29 17:04:26 +03001385 pr_warn("update gid table failed %ld\n", PTR_ERR(mailbox));
Eli Cohenfa417f72010-10-24 21:08:52 -07001386 return;
1387 }
1388
1389 gids = mailbox->buf;
1390 memcpy(gids, gw->gids, sizeof gw->gids);
1391
1392 err = mlx4_cmd(dev, mailbox->dma, MLX4_SET_PORT_GID_TABLE << 8 | gw->port,
Jack Morgensteinf9baff52011-12-13 04:10:51 +00001393 1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +00001394 MLX4_CMD_WRAPPED);
Eli Cohenfa417f72010-10-24 21:08:52 -07001395 if (err)
Shlomo Pongratz987c8f82012-04-29 17:04:26 +03001396 pr_warn("set port command failed\n");
Moni Shouad487ee72013-12-12 18:03:13 +02001397 else
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03001398 mlx4_ib_dispatch_event(gw->dev, gw->port, IB_EVENT_GID_CHANGE);
Eli Cohenfa417f72010-10-24 21:08:52 -07001399
1400 mlx4_free_cmd_mailbox(dev, mailbox);
1401 kfree(gw);
1402}
1403
Moni Shouad487ee72013-12-12 18:03:13 +02001404static void reset_gids_task(struct work_struct *work)
Eli Cohenfa417f72010-10-24 21:08:52 -07001405{
Moni Shouad487ee72013-12-12 18:03:13 +02001406 struct update_gid_work *gw =
1407 container_of(work, struct update_gid_work, work);
1408 struct mlx4_cmd_mailbox *mailbox;
1409 union ib_gid *gids;
1410 int err;
Moni Shouad487ee72013-12-12 18:03:13 +02001411 struct mlx4_dev *dev = gw->dev->dev;
Eli Cohenfa417f72010-10-24 21:08:52 -07001412
Moni Shouad487ee72013-12-12 18:03:13 +02001413 mailbox = mlx4_alloc_cmd_mailbox(dev);
1414 if (IS_ERR(mailbox)) {
1415 pr_warn("reset gid table failed\n");
1416 goto free;
1417 }
1418
1419 gids = mailbox->buf;
1420 memcpy(gids, gw->gids, sizeof(gw->gids));
1421
Moni Shoua50714562014-02-05 15:13:01 +02001422 if (mlx4_ib_port_link_layer(&gw->dev->ib_dev, gw->port) ==
1423 IB_LINK_LAYER_ETHERNET) {
1424 err = mlx4_cmd(dev, mailbox->dma,
1425 MLX4_SET_PORT_GID_TABLE << 8 | gw->port,
1426 1, MLX4_CMD_SET_PORT,
1427 MLX4_CMD_TIME_CLASS_B,
1428 MLX4_CMD_WRAPPED);
1429 if (err)
1430 pr_warn(KERN_WARNING
1431 "set port %d command failed\n", gw->port);
Moni Shouad487ee72013-12-12 18:03:13 +02001432 }
1433
1434 mlx4_free_cmd_mailbox(dev, mailbox);
1435free:
1436 kfree(gw);
1437}
1438
1439static int update_gid_table(struct mlx4_ib_dev *dev, int port,
Moni Shouaacc4fcc2014-02-05 15:12:58 +02001440 union ib_gid *gid, int clear,
1441 int default_gid)
Moni Shouad487ee72013-12-12 18:03:13 +02001442{
1443 struct update_gid_work *work;
1444 int i;
1445 int need_update = 0;
1446 int free = -1;
1447 int found = -1;
1448 int max_gids;
1449
Moni Shouaacc4fcc2014-02-05 15:12:58 +02001450 if (default_gid) {
1451 free = 0;
1452 } else {
1453 max_gids = dev->dev->caps.gid_table_len[port];
1454 for (i = 1; i < max_gids; ++i) {
1455 if (!memcmp(&dev->iboe.gid_table[port - 1][i], gid,
Moni Shouad487ee72013-12-12 18:03:13 +02001456 sizeof(*gid)))
Moni Shouaacc4fcc2014-02-05 15:12:58 +02001457 found = i;
1458
1459 if (clear) {
1460 if (found >= 0) {
1461 need_update = 1;
1462 dev->iboe.gid_table[port - 1][found] =
1463 zgid;
1464 break;
1465 }
1466 } else {
1467 if (found >= 0)
1468 break;
1469
1470 if (free < 0 &&
1471 !memcmp(&dev->iboe.gid_table[port - 1][i],
1472 &zgid, sizeof(*gid)))
1473 free = i;
1474 }
Moni Shouad487ee72013-12-12 18:03:13 +02001475 }
1476 }
1477
1478 if (found == -1 && !clear && free >= 0) {
1479 dev->iboe.gid_table[port - 1][free] = *gid;
1480 need_update = 1;
1481 }
1482
1483 if (!need_update)
1484 return 0;
1485
1486 work = kzalloc(sizeof(*work), GFP_ATOMIC);
Eli Cohenfa417f72010-10-24 21:08:52 -07001487 if (!work)
1488 return -ENOMEM;
1489
Moni Shouad487ee72013-12-12 18:03:13 +02001490 memcpy(work->gids, dev->iboe.gid_table[port - 1], sizeof(work->gids));
1491 INIT_WORK(&work->work, update_gids_task);
1492 work->port = port;
1493 work->dev = dev;
1494 queue_work(wq, &work->work);
Eli Cohenfa417f72010-10-24 21:08:52 -07001495
Moni Shouad487ee72013-12-12 18:03:13 +02001496 return 0;
1497}
Eli Cohenfa417f72010-10-24 21:08:52 -07001498
Moni Shouaacc4fcc2014-02-05 15:12:58 +02001499static void mlx4_make_default_gid(struct net_device *dev, union ib_gid *gid)
1500{
1501 gid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
1502 mlx4_addrconf_ifid_eui48(&gid->raw[8], 0xffff, dev);
1503}
1504
1505
Moni Shoua50714562014-02-05 15:13:01 +02001506static int reset_gid_table(struct mlx4_ib_dev *dev, u8 port)
Moni Shouad487ee72013-12-12 18:03:13 +02001507{
1508 struct update_gid_work *work;
Eli Cohen4c3eb3c2010-08-26 17:19:22 +03001509
Moni Shouad487ee72013-12-12 18:03:13 +02001510 work = kzalloc(sizeof(*work), GFP_ATOMIC);
1511 if (!work)
1512 return -ENOMEM;
Moni Shoua50714562014-02-05 15:13:01 +02001513
1514 memset(dev->iboe.gid_table[port - 1], 0, sizeof(work->gids));
Moni Shouad487ee72013-12-12 18:03:13 +02001515 memset(work->gids, 0, sizeof(work->gids));
1516 INIT_WORK(&work->work, reset_gids_task);
1517 work->dev = dev;
Moni Shoua50714562014-02-05 15:13:01 +02001518 work->port = port;
Moni Shouad487ee72013-12-12 18:03:13 +02001519 queue_work(wq, &work->work);
1520 return 0;
1521}
Eli Cohen4c3eb3c2010-08-26 17:19:22 +03001522
Moni Shouad487ee72013-12-12 18:03:13 +02001523static int mlx4_ib_addr_event(int event, struct net_device *event_netdev,
1524 struct mlx4_ib_dev *ibdev, union ib_gid *gid)
1525{
1526 struct mlx4_ib_iboe *iboe;
1527 int port = 0;
1528 struct net_device *real_dev = rdma_vlan_dev_real_dev(event_netdev) ?
1529 rdma_vlan_dev_real_dev(event_netdev) :
1530 event_netdev;
Moni Shouaacc4fcc2014-02-05 15:12:58 +02001531 union ib_gid default_gid;
1532
1533 mlx4_make_default_gid(real_dev, &default_gid);
1534
1535 if (!memcmp(gid, &default_gid, sizeof(*gid)))
1536 return 0;
Moni Shouad487ee72013-12-12 18:03:13 +02001537
1538 if (event != NETDEV_DOWN && event != NETDEV_UP)
1539 return 0;
1540
1541 if ((real_dev != event_netdev) &&
1542 (event == NETDEV_DOWN) &&
1543 rdma_link_local_addr((struct in6_addr *)gid))
1544 return 0;
1545
1546 iboe = &ibdev->iboe;
1547 spin_lock(&iboe->lock);
1548
Matan Barak82373702014-03-19 18:11:49 +02001549 for (port = 1; port <= ibdev->dev->caps.num_ports; ++port)
Moni Shouad487ee72013-12-12 18:03:13 +02001550 if ((netif_is_bond_master(real_dev) &&
1551 (real_dev == iboe->masters[port - 1])) ||
1552 (!netif_is_bond_master(real_dev) &&
1553 (real_dev == iboe->netdevs[port - 1])))
1554 update_gid_table(ibdev, port, gid,
Moni Shouaacc4fcc2014-02-05 15:12:58 +02001555 event == NETDEV_DOWN, 0);
Moni Shouad487ee72013-12-12 18:03:13 +02001556
1557 spin_unlock(&iboe->lock);
Eli Cohenfa417f72010-10-24 21:08:52 -07001558 return 0;
Eli Cohen4c3eb3c2010-08-26 17:19:22 +03001559
Eli Cohenfa417f72010-10-24 21:08:52 -07001560}
1561
Moni Shouad487ee72013-12-12 18:03:13 +02001562static u8 mlx4_ib_get_dev_port(struct net_device *dev,
1563 struct mlx4_ib_dev *ibdev)
Eli Cohenfa417f72010-10-24 21:08:52 -07001564{
Moni Shouad487ee72013-12-12 18:03:13 +02001565 u8 port = 0;
1566 struct mlx4_ib_iboe *iboe;
1567 struct net_device *real_dev = rdma_vlan_dev_real_dev(dev) ?
1568 rdma_vlan_dev_real_dev(dev) : dev;
Eli Cohenfa417f72010-10-24 21:08:52 -07001569
Moni Shouad487ee72013-12-12 18:03:13 +02001570 iboe = &ibdev->iboe;
Moni Shouad487ee72013-12-12 18:03:13 +02001571
Matan Barak82373702014-03-19 18:11:49 +02001572 for (port = 1; port <= ibdev->dev->caps.num_ports; ++port)
Moni Shouad487ee72013-12-12 18:03:13 +02001573 if ((netif_is_bond_master(real_dev) &&
1574 (real_dev == iboe->masters[port - 1])) ||
1575 (!netif_is_bond_master(real_dev) &&
1576 (real_dev == iboe->netdevs[port - 1])))
1577 break;
1578
Matan Barak82373702014-03-19 18:11:49 +02001579 if ((port == 0) || (port > ibdev->dev->caps.num_ports))
Moni Shouad487ee72013-12-12 18:03:13 +02001580 return 0;
1581 else
1582 return port;
Eli Cohenfa417f72010-10-24 21:08:52 -07001583}
1584
Moni Shouad487ee72013-12-12 18:03:13 +02001585static int mlx4_ib_inet_event(struct notifier_block *this, unsigned long event,
Eli Cohenfa417f72010-10-24 21:08:52 -07001586 void *ptr)
1587{
Moni Shouad487ee72013-12-12 18:03:13 +02001588 struct mlx4_ib_dev *ibdev;
1589 struct in_ifaddr *ifa = ptr;
1590 union ib_gid gid;
1591 struct net_device *event_netdev = ifa->ifa_dev->dev;
1592
1593 ipv6_addr_set_v4mapped(ifa->ifa_address, (struct in6_addr *)&gid);
1594
1595 ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb_inet);
1596
1597 mlx4_ib_addr_event(event, event_netdev, ibdev, &gid);
1598 return NOTIFY_DONE;
1599}
1600
Roland Dreier27cdef62014-01-19 15:18:49 -08001601#if IS_ENABLED(CONFIG_IPV6)
Moni Shouad487ee72013-12-12 18:03:13 +02001602static int mlx4_ib_inet6_event(struct notifier_block *this, unsigned long event,
1603 void *ptr)
1604{
1605 struct mlx4_ib_dev *ibdev;
1606 struct inet6_ifaddr *ifa = ptr;
1607 union ib_gid *gid = (union ib_gid *)&ifa->addr;
1608 struct net_device *event_netdev = ifa->idev->dev;
1609
1610 ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb_inet6);
1611
1612 mlx4_ib_addr_event(event, event_netdev, ibdev, gid);
1613 return NOTIFY_DONE;
1614}
1615#endif
1616
1617static void mlx4_ib_get_dev_addr(struct net_device *dev,
1618 struct mlx4_ib_dev *ibdev, u8 port)
1619{
1620 struct in_device *in_dev;
Roland Dreier27cdef62014-01-19 15:18:49 -08001621#if IS_ENABLED(CONFIG_IPV6)
Moni Shouad487ee72013-12-12 18:03:13 +02001622 struct inet6_dev *in6_dev;
1623 union ib_gid *pgid;
1624 struct inet6_ifaddr *ifp;
1625#endif
1626 union ib_gid gid;
1627
1628
Matan Barak82373702014-03-19 18:11:49 +02001629 if ((port == 0) || (port > ibdev->dev->caps.num_ports))
Moni Shouad487ee72013-12-12 18:03:13 +02001630 return;
1631
1632 /* IPv4 gids */
1633 in_dev = in_dev_get(dev);
1634 if (in_dev) {
1635 for_ifa(in_dev) {
1636 /*ifa->ifa_address;*/
1637 ipv6_addr_set_v4mapped(ifa->ifa_address,
1638 (struct in6_addr *)&gid);
Moni Shouaacc4fcc2014-02-05 15:12:58 +02001639 update_gid_table(ibdev, port, &gid, 0, 0);
Moni Shouad487ee72013-12-12 18:03:13 +02001640 }
1641 endfor_ifa(in_dev);
1642 in_dev_put(in_dev);
1643 }
Roland Dreier27cdef62014-01-19 15:18:49 -08001644#if IS_ENABLED(CONFIG_IPV6)
Moni Shouad487ee72013-12-12 18:03:13 +02001645 /* IPv6 gids */
1646 in6_dev = in6_dev_get(dev);
1647 if (in6_dev) {
1648 read_lock_bh(&in6_dev->lock);
1649 list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
1650 pgid = (union ib_gid *)&ifp->addr;
Moni Shouaacc4fcc2014-02-05 15:12:58 +02001651 update_gid_table(ibdev, port, pgid, 0, 0);
Moni Shouad487ee72013-12-12 18:03:13 +02001652 }
1653 read_unlock_bh(&in6_dev->lock);
1654 in6_dev_put(in6_dev);
1655 }
1656#endif
1657}
1658
Moni Shouaacc4fcc2014-02-05 15:12:58 +02001659static void mlx4_ib_set_default_gid(struct mlx4_ib_dev *ibdev,
1660 struct net_device *dev, u8 port)
1661{
1662 union ib_gid gid;
1663 mlx4_make_default_gid(dev, &gid);
1664 update_gid_table(ibdev, port, &gid, 0, 1);
1665}
1666
Moni Shouad487ee72013-12-12 18:03:13 +02001667static int mlx4_ib_init_gid_table(struct mlx4_ib_dev *ibdev)
1668{
1669 struct net_device *dev;
Moni Shouaddf8bd32014-02-05 15:13:00 +02001670 struct mlx4_ib_iboe *iboe = &ibdev->iboe;
Moni Shoua50714562014-02-05 15:13:01 +02001671 int i;
Moni Shouad487ee72013-12-12 18:03:13 +02001672
Moni Shoua50714562014-02-05 15:13:01 +02001673 for (i = 1; i <= ibdev->num_ports; ++i)
1674 if (reset_gid_table(ibdev, i))
1675 return -1;
Moni Shouad487ee72013-12-12 18:03:13 +02001676
1677 read_lock(&dev_base_lock);
Moni Shouaddf8bd32014-02-05 15:13:00 +02001678 spin_lock(&iboe->lock);
Moni Shouad487ee72013-12-12 18:03:13 +02001679
1680 for_each_netdev(&init_net, dev) {
1681 u8 port = mlx4_ib_get_dev_port(dev, ibdev);
1682 if (port)
1683 mlx4_ib_get_dev_addr(dev, ibdev, port);
1684 }
1685
Moni Shouaddf8bd32014-02-05 15:13:00 +02001686 spin_unlock(&iboe->lock);
Moni Shouad487ee72013-12-12 18:03:13 +02001687 read_unlock(&dev_base_lock);
1688
1689 return 0;
1690}
1691
1692static void mlx4_ib_scan_netdevs(struct mlx4_ib_dev *ibdev)
1693{
1694 struct mlx4_ib_iboe *iboe;
1695 int port;
1696
1697 iboe = &ibdev->iboe;
1698
1699 spin_lock(&iboe->lock);
1700 mlx4_foreach_ib_transport_port(port, ibdev->dev) {
Moni Shouaad4885d22014-02-05 15:13:02 +02001701 enum ib_port_state port_state = IB_PORT_NOP;
Moni Shouad487ee72013-12-12 18:03:13 +02001702 struct net_device *old_master = iboe->masters[port - 1];
Moni Shouaad4885d22014-02-05 15:13:02 +02001703 struct net_device *curr_netdev;
Moni Shouad487ee72013-12-12 18:03:13 +02001704 struct net_device *curr_master;
Moni Shouaad4885d22014-02-05 15:13:02 +02001705
Moni Shouad487ee72013-12-12 18:03:13 +02001706 iboe->netdevs[port - 1] =
1707 mlx4_get_protocol_dev(ibdev->dev, MLX4_PROT_ETH, port);
Moni Shouaacc4fcc2014-02-05 15:12:58 +02001708 if (iboe->netdevs[port - 1])
1709 mlx4_ib_set_default_gid(ibdev,
1710 iboe->netdevs[port - 1], port);
Moni Shouaad4885d22014-02-05 15:13:02 +02001711 curr_netdev = iboe->netdevs[port - 1];
Moni Shouad487ee72013-12-12 18:03:13 +02001712
1713 if (iboe->netdevs[port - 1] &&
1714 netif_is_bond_slave(iboe->netdevs[port - 1])) {
Moni Shouad487ee72013-12-12 18:03:13 +02001715 iboe->masters[port - 1] = netdev_master_upper_dev_get(
1716 iboe->netdevs[port - 1]);
Moni Shouaad4885d22014-02-05 15:13:02 +02001717 } else {
1718 iboe->masters[port - 1] = NULL;
Moni Shouad487ee72013-12-12 18:03:13 +02001719 }
1720 curr_master = iboe->masters[port - 1];
1721
Moni Shouaad4885d22014-02-05 15:13:02 +02001722 if (curr_netdev) {
1723 port_state = (netif_running(curr_netdev) && netif_carrier_ok(curr_netdev)) ?
1724 IB_PORT_ACTIVE : IB_PORT_DOWN;
1725 mlx4_ib_set_default_gid(ibdev, curr_netdev, port);
1726 } else {
1727 reset_gid_table(ibdev, port);
1728 }
1729 /* if using bonding/team and a slave port is down, we don't the bond IP
1730 * based gids in the table since flows that select port by gid may get
1731 * the down port.
1732 */
1733 if (curr_master && (port_state == IB_PORT_DOWN)) {
1734 reset_gid_table(ibdev, port);
1735 mlx4_ib_set_default_gid(ibdev, curr_netdev, port);
1736 }
Moni Shouad487ee72013-12-12 18:03:13 +02001737 /* if bonding is used it is possible that we add it to masters
Moni Shouaad4885d22014-02-05 15:13:02 +02001738 * only after IP address is assigned to the net bonding
1739 * interface.
1740 */
1741 if (curr_master && (old_master != curr_master)) {
1742 reset_gid_table(ibdev, port);
1743 mlx4_ib_set_default_gid(ibdev, curr_netdev, port);
Moni Shouad487ee72013-12-12 18:03:13 +02001744 mlx4_ib_get_dev_addr(curr_master, ibdev, port);
Moni Shouaad4885d22014-02-05 15:13:02 +02001745 }
1746
1747 if (!curr_master && (old_master != curr_master)) {
1748 reset_gid_table(ibdev, port);
1749 mlx4_ib_set_default_gid(ibdev, curr_netdev, port);
1750 mlx4_ib_get_dev_addr(curr_netdev, ibdev, port);
1751 }
Moni Shouad487ee72013-12-12 18:03:13 +02001752 }
1753
1754 spin_unlock(&iboe->lock);
1755}
1756
1757static int mlx4_ib_netdev_event(struct notifier_block *this,
1758 unsigned long event, void *ptr)
1759{
Jiri Pirko351638e2013-05-28 01:30:21 +00001760 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eli Cohenfa417f72010-10-24 21:08:52 -07001761 struct mlx4_ib_dev *ibdev;
Eli Cohenfa417f72010-10-24 21:08:52 -07001762
1763 if (!net_eq(dev_net(dev), &init_net))
1764 return NOTIFY_DONE;
1765
1766 ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb);
Moni Shouad487ee72013-12-12 18:03:13 +02001767 mlx4_ib_scan_netdevs(ibdev);
Eli Cohenfa417f72010-10-24 21:08:52 -07001768
1769 return NOTIFY_DONE;
1770}
1771
Jack Morgenstein54679e12012-08-03 08:40:43 +00001772static void init_pkeys(struct mlx4_ib_dev *ibdev)
1773{
1774 int port;
1775 int slave;
1776 int i;
1777
1778 if (mlx4_is_master(ibdev->dev)) {
1779 for (slave = 0; slave <= ibdev->dev->num_vfs; ++slave) {
1780 for (port = 1; port <= ibdev->dev->caps.num_ports; ++port) {
1781 for (i = 0;
1782 i < ibdev->dev->phys_caps.pkey_phys_table_len[port];
1783 ++i) {
1784 ibdev->pkeys.virt2phys_pkey[slave][port - 1][i] =
1785 /* master has the identity virt2phys pkey mapping */
1786 (slave == mlx4_master_func_num(ibdev->dev) || !i) ? i :
1787 ibdev->dev->phys_caps.pkey_phys_table_len[port] - 1;
1788 mlx4_sync_pkey_table(ibdev->dev, slave, port, i,
1789 ibdev->pkeys.virt2phys_pkey[slave][port - 1][i]);
1790 }
1791 }
1792 }
1793 /* initialize pkey cache */
1794 for (port = 1; port <= ibdev->dev->caps.num_ports; ++port) {
1795 for (i = 0;
1796 i < ibdev->dev->phys_caps.pkey_phys_table_len[port];
1797 ++i)
1798 ibdev->pkeys.phys_pkey_cache[port-1][i] =
1799 (i) ? 0 : 0xFFFF;
1800 }
1801 }
1802}
1803
Shlomo Pongratze605b742012-04-29 17:04:27 +03001804static void mlx4_ib_alloc_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev)
1805{
Dan Carpenter4661bd72014-03-28 11:23:07 +03001806 char name[80];
Shlomo Pongratze605b742012-04-29 17:04:27 +03001807 int eq_per_port = 0;
1808 int added_eqs = 0;
1809 int total_eqs = 0;
1810 int i, j, eq;
1811
Shlomo Pongratz3aac6ff2012-05-24 16:08:07 +03001812 /* Legacy mode or comp_pool is not large enough */
1813 if (dev->caps.comp_pool == 0 ||
1814 dev->caps.num_ports > dev->caps.comp_pool)
Shlomo Pongratze605b742012-04-29 17:04:27 +03001815 return;
1816
1817 eq_per_port = rounddown_pow_of_two(dev->caps.comp_pool/
1818 dev->caps.num_ports);
1819
1820 /* Init eq table */
1821 added_eqs = 0;
1822 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
1823 added_eqs += eq_per_port;
1824
1825 total_eqs = dev->caps.num_comp_vectors + added_eqs;
1826
1827 ibdev->eq_table = kzalloc(total_eqs * sizeof(int), GFP_KERNEL);
1828 if (!ibdev->eq_table)
1829 return;
1830
1831 ibdev->eq_added = added_eqs;
1832
1833 eq = 0;
1834 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB) {
1835 for (j = 0; j < eq_per_port; j++) {
Dan Carpenter4661bd72014-03-28 11:23:07 +03001836 snprintf(name, sizeof(name), "mlx4-ib-%d-%d@%s",
1837 i, j, dev->pdev->bus->name);
Shlomo Pongratze605b742012-04-29 17:04:27 +03001838 /* Set IRQ for specific name (per ring) */
Amir Vadaid9236c32012-07-18 22:33:51 +00001839 if (mlx4_assign_eq(dev, name, NULL,
1840 &ibdev->eq_table[eq])) {
Shlomo Pongratze605b742012-04-29 17:04:27 +03001841 /* Use legacy (same as mlx4_en driver) */
1842 pr_warn("Can't allocate EQ %d; reverting to legacy\n", eq);
1843 ibdev->eq_table[eq] =
1844 (eq % dev->caps.num_comp_vectors);
1845 }
1846 eq++;
1847 }
1848 }
1849
1850 /* Fill the reset of the vector with legacy EQ */
1851 for (i = 0, eq = added_eqs; i < dev->caps.num_comp_vectors; i++)
1852 ibdev->eq_table[eq++] = i;
1853
1854 /* Advertise the new number of EQs to clients */
1855 ibdev->ib_dev.num_comp_vectors = total_eqs;
1856}
1857
1858static void mlx4_ib_free_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev)
1859{
1860 int i;
Shlomo Pongratz3aac6ff2012-05-24 16:08:07 +03001861
1862 /* no additional eqs were added */
1863 if (!ibdev->eq_table)
1864 return;
Shlomo Pongratze605b742012-04-29 17:04:27 +03001865
1866 /* Reset the advertised EQ number */
1867 ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;
1868
1869 /* Free only the added eqs */
1870 for (i = 0; i < ibdev->eq_added; i++) {
1871 /* Don't free legacy eqs if used */
1872 if (ibdev->eq_table[i] <= dev->caps.num_comp_vectors)
1873 continue;
1874 mlx4_release_eq(dev, ibdev->eq_table[i]);
1875 }
1876
Shlomo Pongratze605b742012-04-29 17:04:27 +03001877 kfree(ibdev->eq_table);
Shlomo Pongratze605b742012-04-29 17:04:27 +03001878}
1879
Roland Dreier225c7b12007-05-08 18:00:38 -07001880static void *mlx4_ib_add(struct mlx4_dev *dev)
1881{
1882 struct mlx4_ib_dev *ibdev;
Roland Dreier22e7ef92009-01-09 13:22:29 -08001883 int num_ports = 0;
Jack Morgenstein035b1032012-05-10 23:28:09 +03001884 int i, j;
Eli Cohenfa417f72010-10-24 21:08:52 -07001885 int err;
1886 struct mlx4_ib_iboe *iboe;
Matan Barak41966702014-02-02 17:06:47 +02001887 int ib_num_ports = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07001888
Shlomo Pongratz987c8f82012-04-29 17:04:26 +03001889 pr_info_once("%s", mlx4_ib_version);
Roland Dreier68f39482008-02-04 20:20:44 -08001890
Jack Morgenstein026149c2012-08-03 08:40:55 +00001891 num_ports = 0;
Eli Cohenfa417f72010-10-24 21:08:52 -07001892 mlx4_foreach_ib_transport_port(i, dev)
Roland Dreier22e7ef92009-01-09 13:22:29 -08001893 num_ports++;
1894
1895 /* No point in registering a device with no ports... */
1896 if (num_ports == 0)
1897 return NULL;
1898
Roland Dreier225c7b12007-05-08 18:00:38 -07001899 ibdev = (struct mlx4_ib_dev *) ib_alloc_device(sizeof *ibdev);
1900 if (!ibdev) {
1901 dev_err(&dev->pdev->dev, "Device struct alloc failed\n");
1902 return NULL;
1903 }
1904
Eli Cohenfa417f72010-10-24 21:08:52 -07001905 iboe = &ibdev->iboe;
1906
Roland Dreier225c7b12007-05-08 18:00:38 -07001907 if (mlx4_pd_alloc(dev, &ibdev->priv_pdn))
1908 goto err_dealloc;
1909
1910 if (mlx4_uar_alloc(dev, &ibdev->priv_uar))
1911 goto err_pd;
1912
Roland Dreier4979d182011-01-12 09:50:36 -08001913 ibdev->uar_map = ioremap((phys_addr_t) ibdev->priv_uar.pfn << PAGE_SHIFT,
1914 PAGE_SIZE);
Roland Dreier225c7b12007-05-08 18:00:38 -07001915 if (!ibdev->uar_map)
1916 goto err_uar;
Jack Morgenstein26c6bc72007-05-13 17:18:23 +03001917 MLX4_INIT_DOORBELL_LOCK(&ibdev->uar_lock);
Roland Dreier225c7b12007-05-08 18:00:38 -07001918
Roland Dreier225c7b12007-05-08 18:00:38 -07001919 ibdev->dev = dev;
1920
1921 strlcpy(ibdev->ib_dev.name, "mlx4_%d", IB_DEVICE_NAME_MAX);
1922 ibdev->ib_dev.owner = THIS_MODULE;
1923 ibdev->ib_dev.node_type = RDMA_NODE_IB_CA;
Roland Dreier95d04f02008-07-23 08:12:26 -07001924 ibdev->ib_dev.local_dma_lkey = dev->caps.reserved_lkey;
Roland Dreier22e7ef92009-01-09 13:22:29 -08001925 ibdev->num_ports = num_ports;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07001926 ibdev->ib_dev.phys_port_cnt = ibdev->num_ports;
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08001927 ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;
Roland Dreier225c7b12007-05-08 18:00:38 -07001928 ibdev->ib_dev.dma_device = &dev->pdev->dev;
1929
Or Gerlitz08ff3232012-10-21 14:59:24 +00001930 if (dev->caps.userspace_caps)
1931 ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_ABI_VERSION;
1932 else
1933 ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION;
1934
Roland Dreier225c7b12007-05-08 18:00:38 -07001935 ibdev->ib_dev.uverbs_cmd_mask =
1936 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
1937 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
1938 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
1939 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
1940 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
1941 (1ull << IB_USER_VERBS_CMD_REG_MR) |
1942 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
1943 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
1944 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
Vladimir Sokolovskybbf8eed12008-04-16 21:09:33 -07001945 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
Roland Dreier225c7b12007-05-08 18:00:38 -07001946 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
1947 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
1948 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
Jack Morgenstein6a775e22007-06-21 12:27:47 +03001949 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
Roland Dreier225c7b12007-05-08 18:00:38 -07001950 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
1951 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
1952 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
1953 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
1954 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
Jack Morgenstein65541cb2007-06-21 13:03:11 +03001955 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
Sean Hefty18abd5e2011-06-02 10:43:26 -07001956 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
Sean Hefty42849b22011-08-11 13:57:43 -07001957 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
1958 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Roland Dreier225c7b12007-05-08 18:00:38 -07001959
1960 ibdev->ib_dev.query_device = mlx4_ib_query_device;
1961 ibdev->ib_dev.query_port = mlx4_ib_query_port;
Eli Cohenfa417f72010-10-24 21:08:52 -07001962 ibdev->ib_dev.get_link_layer = mlx4_ib_port_link_layer;
Roland Dreier225c7b12007-05-08 18:00:38 -07001963 ibdev->ib_dev.query_gid = mlx4_ib_query_gid;
1964 ibdev->ib_dev.query_pkey = mlx4_ib_query_pkey;
1965 ibdev->ib_dev.modify_device = mlx4_ib_modify_device;
1966 ibdev->ib_dev.modify_port = mlx4_ib_modify_port;
1967 ibdev->ib_dev.alloc_ucontext = mlx4_ib_alloc_ucontext;
1968 ibdev->ib_dev.dealloc_ucontext = mlx4_ib_dealloc_ucontext;
1969 ibdev->ib_dev.mmap = mlx4_ib_mmap;
1970 ibdev->ib_dev.alloc_pd = mlx4_ib_alloc_pd;
1971 ibdev->ib_dev.dealloc_pd = mlx4_ib_dealloc_pd;
1972 ibdev->ib_dev.create_ah = mlx4_ib_create_ah;
1973 ibdev->ib_dev.query_ah = mlx4_ib_query_ah;
1974 ibdev->ib_dev.destroy_ah = mlx4_ib_destroy_ah;
1975 ibdev->ib_dev.create_srq = mlx4_ib_create_srq;
1976 ibdev->ib_dev.modify_srq = mlx4_ib_modify_srq;
Jack Morgenstein65541cb2007-06-21 13:03:11 +03001977 ibdev->ib_dev.query_srq = mlx4_ib_query_srq;
Roland Dreier225c7b12007-05-08 18:00:38 -07001978 ibdev->ib_dev.destroy_srq = mlx4_ib_destroy_srq;
1979 ibdev->ib_dev.post_srq_recv = mlx4_ib_post_srq_recv;
1980 ibdev->ib_dev.create_qp = mlx4_ib_create_qp;
1981 ibdev->ib_dev.modify_qp = mlx4_ib_modify_qp;
Jack Morgenstein6a775e22007-06-21 12:27:47 +03001982 ibdev->ib_dev.query_qp = mlx4_ib_query_qp;
Roland Dreier225c7b12007-05-08 18:00:38 -07001983 ibdev->ib_dev.destroy_qp = mlx4_ib_destroy_qp;
1984 ibdev->ib_dev.post_send = mlx4_ib_post_send;
1985 ibdev->ib_dev.post_recv = mlx4_ib_post_recv;
1986 ibdev->ib_dev.create_cq = mlx4_ib_create_cq;
Eli Cohen3fdcb972008-04-16 21:09:33 -07001987 ibdev->ib_dev.modify_cq = mlx4_ib_modify_cq;
Vladimir Sokolovskybbf8eed12008-04-16 21:09:33 -07001988 ibdev->ib_dev.resize_cq = mlx4_ib_resize_cq;
Roland Dreier225c7b12007-05-08 18:00:38 -07001989 ibdev->ib_dev.destroy_cq = mlx4_ib_destroy_cq;
1990 ibdev->ib_dev.poll_cq = mlx4_ib_poll_cq;
1991 ibdev->ib_dev.req_notify_cq = mlx4_ib_arm_cq;
1992 ibdev->ib_dev.get_dma_mr = mlx4_ib_get_dma_mr;
1993 ibdev->ib_dev.reg_user_mr = mlx4_ib_reg_user_mr;
1994 ibdev->ib_dev.dereg_mr = mlx4_ib_dereg_mr;
Roland Dreier95d04f02008-07-23 08:12:26 -07001995 ibdev->ib_dev.alloc_fast_reg_mr = mlx4_ib_alloc_fast_reg_mr;
1996 ibdev->ib_dev.alloc_fast_reg_page_list = mlx4_ib_alloc_fast_reg_page_list;
1997 ibdev->ib_dev.free_fast_reg_page_list = mlx4_ib_free_fast_reg_page_list;
Roland Dreier225c7b12007-05-08 18:00:38 -07001998 ibdev->ib_dev.attach_mcast = mlx4_ib_mcg_attach;
1999 ibdev->ib_dev.detach_mcast = mlx4_ib_mcg_detach;
2000 ibdev->ib_dev.process_mad = mlx4_ib_process_mad;
2001
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +00002002 if (!mlx4_is_slave(ibdev->dev)) {
2003 ibdev->ib_dev.alloc_fmr = mlx4_ib_fmr_alloc;
2004 ibdev->ib_dev.map_phys_fmr = mlx4_ib_map_phys_fmr;
2005 ibdev->ib_dev.unmap_fmr = mlx4_ib_unmap_fmr;
2006 ibdev->ib_dev.dealloc_fmr = mlx4_ib_fmr_dealloc;
2007 }
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +03002008
Shani Michaelib4253882013-02-06 16:19:16 +00002009 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_MEM_WINDOW ||
2010 dev->caps.bmme_flags & MLX4_BMME_FLAG_TYPE_2_WIN) {
2011 ibdev->ib_dev.alloc_mw = mlx4_ib_alloc_mw;
2012 ibdev->ib_dev.bind_mw = mlx4_ib_bind_mw;
2013 ibdev->ib_dev.dealloc_mw = mlx4_ib_dealloc_mw;
2014
2015 ibdev->ib_dev.uverbs_cmd_mask |=
2016 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
2017 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
2018 }
2019
Sean Hefty012a8ff2011-06-02 09:01:33 -07002020 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) {
2021 ibdev->ib_dev.alloc_xrcd = mlx4_ib_alloc_xrcd;
2022 ibdev->ib_dev.dealloc_xrcd = mlx4_ib_dealloc_xrcd;
2023 ibdev->ib_dev.uverbs_cmd_mask |=
2024 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
2025 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
2026 }
2027
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03002028 if (check_flow_steering_support(dev)) {
Matan Barak0a9b7d52013-11-07 15:25:15 +02002029 ibdev->steering_support = MLX4_STEERING_MODE_DEVICE_MANAGED;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03002030 ibdev->ib_dev.create_flow = mlx4_ib_create_flow;
2031 ibdev->ib_dev.destroy_flow = mlx4_ib_destroy_flow;
2032
Yann Droneaudf21519b2013-11-06 23:21:49 +01002033 ibdev->ib_dev.uverbs_ex_cmd_mask |=
2034 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
2035 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03002036 }
2037
Shlomo Pongratze605b742012-04-29 17:04:27 +03002038 mlx4_ib_alloc_eqs(dev, ibdev);
2039
Eli Cohenfa417f72010-10-24 21:08:52 -07002040 spin_lock_init(&iboe->lock);
2041
Roland Dreier225c7b12007-05-08 18:00:38 -07002042 if (init_node_data(ibdev))
2043 goto err_map;
2044
Or Gerlitzcfcde112011-06-15 14:49:57 +00002045 for (i = 0; i < ibdev->num_ports; ++i) {
2046 if (mlx4_ib_port_link_layer(&ibdev->ib_dev, i + 1) ==
2047 IB_LINK_LAYER_ETHERNET) {
2048 err = mlx4_counter_alloc(ibdev->dev, &ibdev->counters[i]);
2049 if (err)
2050 ibdev->counters[i] = -1;
Dan Carpenter3839d8a2014-03-28 11:21:39 +03002051 } else {
2052 ibdev->counters[i] = -1;
2053 }
Or Gerlitzcfcde112011-06-15 14:49:57 +00002054 }
2055
Matan Barak41966702014-02-02 17:06:47 +02002056 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
2057 ib_num_ports++;
2058
Roland Dreier225c7b12007-05-08 18:00:38 -07002059 spin_lock_init(&ibdev->sm_lock);
2060 mutex_init(&ibdev->cap_mask_mutex);
2061
Matan Barak41966702014-02-02 17:06:47 +02002062 if (ibdev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED &&
2063 ib_num_ports) {
Matan Barakc1c98502013-11-07 15:25:17 +02002064 ibdev->steer_qpn_count = MLX4_IB_UC_MAX_NUM_QPS;
2065 err = mlx4_qp_reserve_range(dev, ibdev->steer_qpn_count,
2066 MLX4_IB_UC_STEER_QPN_ALIGN,
2067 &ibdev->steer_qpn_base);
2068 if (err)
2069 goto err_counter;
2070
2071 ibdev->ib_uc_qpns_bitmap =
2072 kmalloc(BITS_TO_LONGS(ibdev->steer_qpn_count) *
2073 sizeof(long),
2074 GFP_KERNEL);
2075 if (!ibdev->ib_uc_qpns_bitmap) {
2076 dev_err(&dev->pdev->dev, "bit map alloc failed\n");
2077 goto err_steer_qp_release;
2078 }
2079
2080 bitmap_zero(ibdev->ib_uc_qpns_bitmap, ibdev->steer_qpn_count);
2081
2082 err = mlx4_FLOW_STEERING_IB_UC_QP_RANGE(
2083 dev, ibdev->steer_qpn_base,
2084 ibdev->steer_qpn_base +
2085 ibdev->steer_qpn_count - 1);
2086 if (err)
2087 goto err_steer_free_bitmap;
2088 }
2089
Ralph Campbell9a6edb62010-05-06 17:03:25 -07002090 if (ib_register_device(&ibdev->ib_dev, NULL))
Matan Barakc1c98502013-11-07 15:25:17 +02002091 goto err_steer_free_bitmap;
Roland Dreier225c7b12007-05-08 18:00:38 -07002092
2093 if (mlx4_ib_mad_init(ibdev))
2094 goto err_reg;
2095
Jack Morgensteinfc065732012-08-03 08:40:42 +00002096 if (mlx4_ib_init_sriov(ibdev))
2097 goto err_mad;
2098
Moni Shouad487ee72013-12-12 18:03:13 +02002099 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE) {
2100 if (!iboe->nb.notifier_call) {
2101 iboe->nb.notifier_call = mlx4_ib_netdev_event;
2102 err = register_netdevice_notifier(&iboe->nb);
2103 if (err) {
2104 iboe->nb.notifier_call = NULL;
2105 goto err_notif;
2106 }
2107 }
2108 if (!iboe->nb_inet.notifier_call) {
2109 iboe->nb_inet.notifier_call = mlx4_ib_inet_event;
2110 err = register_inetaddr_notifier(&iboe->nb_inet);
2111 if (err) {
2112 iboe->nb_inet.notifier_call = NULL;
2113 goto err_notif;
2114 }
2115 }
Roland Dreier27cdef62014-01-19 15:18:49 -08002116#if IS_ENABLED(CONFIG_IPV6)
Moni Shouad487ee72013-12-12 18:03:13 +02002117 if (!iboe->nb_inet6.notifier_call) {
2118 iboe->nb_inet6.notifier_call = mlx4_ib_inet6_event;
2119 err = register_inet6addr_notifier(&iboe->nb_inet6);
2120 if (err) {
2121 iboe->nb_inet6.notifier_call = NULL;
2122 goto err_notif;
2123 }
2124 }
2125#endif
Moni Shouaad4885d22014-02-05 15:13:02 +02002126 for (i = 1 ; i <= ibdev->num_ports ; ++i)
2127 reset_gid_table(ibdev, i);
Moni Shoua4ce5a5742014-02-05 15:12:59 +02002128 rtnl_lock();
Moni Shouad487ee72013-12-12 18:03:13 +02002129 mlx4_ib_scan_netdevs(ibdev);
Moni Shoua4ce5a5742014-02-05 15:12:59 +02002130 rtnl_unlock();
Moni Shouad487ee72013-12-12 18:03:13 +02002131 mlx4_ib_init_gid_table(ibdev);
Eli Cohenfa417f72010-10-24 21:08:52 -07002132 }
2133
Jack Morgenstein035b1032012-05-10 23:28:09 +03002134 for (j = 0; j < ARRAY_SIZE(mlx4_class_attributes); ++j) {
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002135 if (device_create_file(&ibdev->ib_dev.dev,
Jack Morgenstein035b1032012-05-10 23:28:09 +03002136 mlx4_class_attributes[j]))
Eli Cohenfa417f72010-10-24 21:08:52 -07002137 goto err_notif;
Jack Morgensteincd9281d2007-09-18 09:14:18 +02002138 }
2139
Jack Morgenstein3b4a8cd2009-09-05 20:24:50 -07002140 ibdev->ib_active = true;
2141
Jack Morgenstein54679e12012-08-03 08:40:43 +00002142 if (mlx4_is_mfunc(ibdev->dev))
2143 init_pkeys(ibdev);
2144
Jack Morgenstein3806d082012-08-03 08:40:58 +00002145 /* create paravirt contexts for any VFs which are active */
2146 if (mlx4_is_master(ibdev->dev)) {
2147 for (j = 0; j < MLX4_MFUNC_MAX; j++) {
2148 if (j == mlx4_master_func_num(ibdev->dev))
2149 continue;
2150 if (mlx4_is_slave_active(ibdev->dev, j))
2151 do_slave_init(ibdev, j, 1);
2152 }
2153 }
Roland Dreier225c7b12007-05-08 18:00:38 -07002154 return ibdev;
2155
Eli Cohenfa417f72010-10-24 21:08:52 -07002156err_notif:
Moni Shouad487ee72013-12-12 18:03:13 +02002157 if (ibdev->iboe.nb.notifier_call) {
2158 if (unregister_netdevice_notifier(&ibdev->iboe.nb))
2159 pr_warn("failure unregistering notifier\n");
2160 ibdev->iboe.nb.notifier_call = NULL;
2161 }
2162 if (ibdev->iboe.nb_inet.notifier_call) {
2163 if (unregister_inetaddr_notifier(&ibdev->iboe.nb_inet))
2164 pr_warn("failure unregistering notifier\n");
2165 ibdev->iboe.nb_inet.notifier_call = NULL;
2166 }
Roland Dreier27cdef62014-01-19 15:18:49 -08002167#if IS_ENABLED(CONFIG_IPV6)
Moni Shouad487ee72013-12-12 18:03:13 +02002168 if (ibdev->iboe.nb_inet6.notifier_call) {
2169 if (unregister_inet6addr_notifier(&ibdev->iboe.nb_inet6))
2170 pr_warn("failure unregistering notifier\n");
2171 ibdev->iboe.nb_inet6.notifier_call = NULL;
2172 }
2173#endif
Eli Cohenfa417f72010-10-24 21:08:52 -07002174 flush_workqueue(wq);
2175
Jack Morgensteinfc065732012-08-03 08:40:42 +00002176 mlx4_ib_close_sriov(ibdev);
2177
2178err_mad:
2179 mlx4_ib_mad_cleanup(ibdev);
2180
Roland Dreier225c7b12007-05-08 18:00:38 -07002181err_reg:
2182 ib_unregister_device(&ibdev->ib_dev);
2183
Matan Barakc1c98502013-11-07 15:25:17 +02002184err_steer_free_bitmap:
2185 kfree(ibdev->ib_uc_qpns_bitmap);
2186
2187err_steer_qp_release:
2188 if (ibdev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED)
2189 mlx4_qp_release_range(dev, ibdev->steer_qpn_base,
2190 ibdev->steer_qpn_count);
Or Gerlitzcfcde112011-06-15 14:49:57 +00002191err_counter:
2192 for (; i; --i)
Roland Dreier4af3ce02011-12-06 10:47:37 -08002193 if (ibdev->counters[i - 1] != -1)
2194 mlx4_counter_free(ibdev->dev, ibdev->counters[i - 1]);
Or Gerlitzcfcde112011-06-15 14:49:57 +00002195
Roland Dreier225c7b12007-05-08 18:00:38 -07002196err_map:
2197 iounmap(ibdev->uar_map);
2198
2199err_uar:
2200 mlx4_uar_free(dev, &ibdev->priv_uar);
2201
2202err_pd:
2203 mlx4_pd_free(dev, ibdev->priv_pdn);
2204
2205err_dealloc:
2206 ib_dealloc_device(&ibdev->ib_dev);
2207
2208 return NULL;
2209}
2210
Matan Barakc1c98502013-11-07 15:25:17 +02002211int mlx4_ib_steer_qp_alloc(struct mlx4_ib_dev *dev, int count, int *qpn)
2212{
2213 int offset;
2214
2215 WARN_ON(!dev->ib_uc_qpns_bitmap);
2216
2217 offset = bitmap_find_free_region(dev->ib_uc_qpns_bitmap,
2218 dev->steer_qpn_count,
2219 get_count_order(count));
2220 if (offset < 0)
2221 return offset;
2222
2223 *qpn = dev->steer_qpn_base + offset;
2224 return 0;
2225}
2226
2227void mlx4_ib_steer_qp_free(struct mlx4_ib_dev *dev, u32 qpn, int count)
2228{
2229 if (!qpn ||
2230 dev->steering_support != MLX4_STEERING_MODE_DEVICE_MANAGED)
2231 return;
2232
2233 BUG_ON(qpn < dev->steer_qpn_base);
2234
2235 bitmap_release_region(dev->ib_uc_qpns_bitmap,
2236 qpn - dev->steer_qpn_base,
2237 get_count_order(count));
2238}
2239
2240int mlx4_ib_steer_qp_reg(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
2241 int is_attach)
2242{
2243 int err;
2244 size_t flow_size;
2245 struct ib_flow_attr *flow = NULL;
2246 struct ib_flow_spec_ib *ib_spec;
2247
2248 if (is_attach) {
2249 flow_size = sizeof(struct ib_flow_attr) +
2250 sizeof(struct ib_flow_spec_ib);
2251 flow = kzalloc(flow_size, GFP_KERNEL);
2252 if (!flow)
2253 return -ENOMEM;
2254 flow->port = mqp->port;
2255 flow->num_of_specs = 1;
2256 flow->size = flow_size;
2257 ib_spec = (struct ib_flow_spec_ib *)(flow + 1);
2258 ib_spec->type = IB_FLOW_SPEC_IB;
2259 ib_spec->size = sizeof(struct ib_flow_spec_ib);
2260 /* Add an empty rule for IB L2 */
2261 memset(&ib_spec->mask, 0, sizeof(ib_spec->mask));
2262
2263 err = __mlx4_ib_create_flow(&mqp->ibqp, flow,
2264 IB_FLOW_DOMAIN_NIC,
2265 MLX4_FS_REGULAR,
2266 &mqp->reg_id);
2267 } else {
2268 err = __mlx4_ib_destroy_flow(mdev->dev, mqp->reg_id);
2269 }
2270 kfree(flow);
2271 return err;
2272}
2273
Roland Dreier225c7b12007-05-08 18:00:38 -07002274static void mlx4_ib_remove(struct mlx4_dev *dev, void *ibdev_ptr)
2275{
2276 struct mlx4_ib_dev *ibdev = ibdev_ptr;
2277 int p;
2278
Jack Morgensteinfc065732012-08-03 08:40:42 +00002279 mlx4_ib_close_sriov(ibdev);
Yevgeny Petrilina6a47772009-03-18 19:49:54 -07002280 mlx4_ib_mad_cleanup(ibdev);
2281 ib_unregister_device(&ibdev->ib_dev);
Eli Cohenfa417f72010-10-24 21:08:52 -07002282 if (ibdev->iboe.nb.notifier_call) {
2283 if (unregister_netdevice_notifier(&ibdev->iboe.nb))
Shlomo Pongratz987c8f82012-04-29 17:04:26 +03002284 pr_warn("failure unregistering notifier\n");
Eli Cohenfa417f72010-10-24 21:08:52 -07002285 ibdev->iboe.nb.notifier_call = NULL;
2286 }
Matan Barakc1c98502013-11-07 15:25:17 +02002287
2288 if (ibdev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED) {
2289 mlx4_qp_release_range(dev, ibdev->steer_qpn_base,
2290 ibdev->steer_qpn_count);
2291 kfree(ibdev->ib_uc_qpns_bitmap);
2292 }
2293
Moni Shouad487ee72013-12-12 18:03:13 +02002294 if (ibdev->iboe.nb_inet.notifier_call) {
2295 if (unregister_inetaddr_notifier(&ibdev->iboe.nb_inet))
2296 pr_warn("failure unregistering notifier\n");
2297 ibdev->iboe.nb_inet.notifier_call = NULL;
2298 }
Roland Dreier27cdef62014-01-19 15:18:49 -08002299#if IS_ENABLED(CONFIG_IPV6)
Moni Shouad487ee72013-12-12 18:03:13 +02002300 if (ibdev->iboe.nb_inet6.notifier_call) {
2301 if (unregister_inet6addr_notifier(&ibdev->iboe.nb_inet6))
2302 pr_warn("failure unregistering notifier\n");
2303 ibdev->iboe.nb_inet6.notifier_call = NULL;
2304 }
2305#endif
Roland Dreierfb1b5032014-01-22 23:24:21 -08002306
Eli Cohenfa417f72010-10-24 21:08:52 -07002307 iounmap(ibdev->uar_map);
Or Gerlitzcfcde112011-06-15 14:49:57 +00002308 for (p = 0; p < ibdev->num_ports; ++p)
Roland Dreier4af3ce02011-12-06 10:47:37 -08002309 if (ibdev->counters[p] != -1)
2310 mlx4_counter_free(ibdev->dev, ibdev->counters[p]);
Eli Cohenfa417f72010-10-24 21:08:52 -07002311 mlx4_foreach_port(p, dev, MLX4_PORT_TYPE_IB)
Roland Dreier225c7b12007-05-08 18:00:38 -07002312 mlx4_CLOSE_PORT(dev, p);
2313
Shlomo Pongratze605b742012-04-29 17:04:27 +03002314 mlx4_ib_free_eqs(dev, ibdev);
2315
Roland Dreier225c7b12007-05-08 18:00:38 -07002316 mlx4_uar_free(dev, &ibdev->priv_uar);
2317 mlx4_pd_free(dev, ibdev->priv_pdn);
2318 ib_dealloc_device(&ibdev->ib_dev);
2319}
2320
Jack Morgensteinfc065732012-08-03 08:40:42 +00002321static void do_slave_init(struct mlx4_ib_dev *ibdev, int slave, int do_init)
2322{
2323 struct mlx4_ib_demux_work **dm = NULL;
2324 struct mlx4_dev *dev = ibdev->dev;
2325 int i;
2326 unsigned long flags;
Matan Barak449fc482014-03-19 18:11:52 +02002327 struct mlx4_active_ports actv_ports;
2328 unsigned int ports;
2329 unsigned int first_port;
Jack Morgensteinfc065732012-08-03 08:40:42 +00002330
2331 if (!mlx4_is_master(dev))
2332 return;
2333
Matan Barak449fc482014-03-19 18:11:52 +02002334 actv_ports = mlx4_get_active_ports(dev, slave);
2335 ports = bitmap_weight(actv_ports.ports, dev->caps.num_ports);
2336 first_port = find_first_bit(actv_ports.ports, dev->caps.num_ports);
2337
2338 dm = kcalloc(ports, sizeof(*dm), GFP_ATOMIC);
Jack Morgensteinfc065732012-08-03 08:40:42 +00002339 if (!dm) {
2340 pr_err("failed to allocate memory for tunneling qp update\n");
2341 goto out;
2342 }
2343
Matan Barak449fc482014-03-19 18:11:52 +02002344 for (i = 0; i < ports; i++) {
Jack Morgensteinfc065732012-08-03 08:40:42 +00002345 dm[i] = kmalloc(sizeof (struct mlx4_ib_demux_work), GFP_ATOMIC);
2346 if (!dm[i]) {
2347 pr_err("failed to allocate memory for tunneling qp update work struct\n");
2348 for (i = 0; i < dev->caps.num_ports; i++) {
2349 if (dm[i])
2350 kfree(dm[i]);
2351 }
2352 goto out;
2353 }
2354 }
2355 /* initialize or tear down tunnel QPs for the slave */
Matan Barak449fc482014-03-19 18:11:52 +02002356 for (i = 0; i < ports; i++) {
Jack Morgensteinfc065732012-08-03 08:40:42 +00002357 INIT_WORK(&dm[i]->work, mlx4_ib_tunnels_update_work);
Matan Barak449fc482014-03-19 18:11:52 +02002358 dm[i]->port = first_port + i + 1;
Jack Morgensteinfc065732012-08-03 08:40:42 +00002359 dm[i]->slave = slave;
2360 dm[i]->do_init = do_init;
2361 dm[i]->dev = ibdev;
2362 spin_lock_irqsave(&ibdev->sriov.going_down_lock, flags);
2363 if (!ibdev->sriov.is_going_down)
2364 queue_work(ibdev->sriov.demux[i].ud_wq, &dm[i]->work);
2365 spin_unlock_irqrestore(&ibdev->sriov.going_down_lock, flags);
2366 }
2367out:
Syam Sidhardhanc89d1272013-02-24 23:20:05 +00002368 kfree(dm);
Jack Morgensteinfc065732012-08-03 08:40:42 +00002369 return;
2370}
2371
Roland Dreier225c7b12007-05-08 18:00:38 -07002372static void mlx4_ib_event(struct mlx4_dev *dev, void *ibdev_ptr,
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03002373 enum mlx4_dev_event event, unsigned long param)
Roland Dreier225c7b12007-05-08 18:00:38 -07002374{
2375 struct ib_event ibev;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002376 struct mlx4_ib_dev *ibdev = to_mdev((struct ib_device *) ibdev_ptr);
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03002377 struct mlx4_eqe *eqe = NULL;
2378 struct ib_event_work *ew;
Jack Morgensteinfc065732012-08-03 08:40:42 +00002379 int p = 0;
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03002380
2381 if (event == MLX4_DEV_EVENT_PORT_MGMT_CHANGE)
2382 eqe = (struct mlx4_eqe *)param;
2383 else
Jack Morgensteinfc065732012-08-03 08:40:42 +00002384 p = (int) param;
Roland Dreier225c7b12007-05-08 18:00:38 -07002385
2386 switch (event) {
Roland Dreier37608ee2008-04-16 21:01:08 -07002387 case MLX4_DEV_EVENT_PORT_UP:
Jack Morgensteinfc065732012-08-03 08:40:42 +00002388 if (p > ibdev->num_ports)
2389 return;
Jack Morgensteina0c64a12012-08-03 08:40:49 +00002390 if (mlx4_is_master(dev) &&
2391 rdma_port_get_link_layer(&ibdev->ib_dev, p) ==
2392 IB_LINK_LAYER_INFINIBAND) {
2393 mlx4_ib_invalidate_all_guid_record(ibdev, p);
2394 }
Roland Dreier37608ee2008-04-16 21:01:08 -07002395 ibev.event = IB_EVENT_PORT_ACTIVE;
Roland Dreier225c7b12007-05-08 18:00:38 -07002396 break;
2397
Roland Dreier37608ee2008-04-16 21:01:08 -07002398 case MLX4_DEV_EVENT_PORT_DOWN:
Jack Morgensteinfc065732012-08-03 08:40:42 +00002399 if (p > ibdev->num_ports)
2400 return;
Roland Dreier37608ee2008-04-16 21:01:08 -07002401 ibev.event = IB_EVENT_PORT_ERR;
2402 break;
2403
2404 case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
Jack Morgenstein3b4a8cd2009-09-05 20:24:50 -07002405 ibdev->ib_active = false;
Roland Dreier225c7b12007-05-08 18:00:38 -07002406 ibev.event = IB_EVENT_DEVICE_FATAL;
2407 break;
2408
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03002409 case MLX4_DEV_EVENT_PORT_MGMT_CHANGE:
2410 ew = kmalloc(sizeof *ew, GFP_ATOMIC);
2411 if (!ew) {
2412 pr_err("failed to allocate memory for events work\n");
2413 break;
2414 }
2415
2416 INIT_WORK(&ew->work, handle_port_mgmt_change_event);
2417 memcpy(&ew->ib_eqe, eqe, sizeof *eqe);
2418 ew->ib_dev = ibdev;
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +00002419 /* need to queue only for port owner, which uses GEN_EQE */
2420 if (mlx4_is_master(dev))
2421 queue_work(wq, &ew->work);
2422 else
2423 handle_port_mgmt_change_event(&ew->work);
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03002424 return;
2425
Jack Morgensteinfc065732012-08-03 08:40:42 +00002426 case MLX4_DEV_EVENT_SLAVE_INIT:
2427 /* here, p is the slave id */
2428 do_slave_init(ibdev, p, 1);
2429 return;
2430
2431 case MLX4_DEV_EVENT_SLAVE_SHUTDOWN:
2432 /* here, p is the slave id */
2433 do_slave_init(ibdev, p, 0);
2434 return;
2435
Roland Dreier225c7b12007-05-08 18:00:38 -07002436 default:
2437 return;
2438 }
2439
2440 ibev.device = ibdev_ptr;
Jack Morgensteinfc065732012-08-03 08:40:42 +00002441 ibev.element.port_num = (u8) p;
Roland Dreier225c7b12007-05-08 18:00:38 -07002442
2443 ib_dispatch_event(&ibev);
2444}
2445
2446static struct mlx4_interface mlx4_ib_interface = {
Eli Cohenfa417f72010-10-24 21:08:52 -07002447 .add = mlx4_ib_add,
2448 .remove = mlx4_ib_remove,
2449 .event = mlx4_ib_event,
Yevgeny Petrilin03455842011-03-22 22:38:17 +00002450 .protocol = MLX4_PROT_IB_IPV6
Roland Dreier225c7b12007-05-08 18:00:38 -07002451};
2452
2453static int __init mlx4_ib_init(void)
2454{
Eli Cohenfa417f72010-10-24 21:08:52 -07002455 int err;
2456
2457 wq = create_singlethread_workqueue("mlx4_ib");
2458 if (!wq)
2459 return -ENOMEM;
2460
Oren Duerb9c5d6a2012-08-03 08:40:46 +00002461 err = mlx4_ib_mcg_init();
2462 if (err)
2463 goto clean_wq;
2464
Eli Cohenfa417f72010-10-24 21:08:52 -07002465 err = mlx4_register_interface(&mlx4_ib_interface);
Oren Duerb9c5d6a2012-08-03 08:40:46 +00002466 if (err)
2467 goto clean_mcg;
Eli Cohenfa417f72010-10-24 21:08:52 -07002468
2469 return 0;
Oren Duerb9c5d6a2012-08-03 08:40:46 +00002470
2471clean_mcg:
2472 mlx4_ib_mcg_destroy();
2473
2474clean_wq:
2475 destroy_workqueue(wq);
2476 return err;
Roland Dreier225c7b12007-05-08 18:00:38 -07002477}
2478
2479static void __exit mlx4_ib_cleanup(void)
2480{
2481 mlx4_unregister_interface(&mlx4_ib_interface);
Oren Duerb9c5d6a2012-08-03 08:40:46 +00002482 mlx4_ib_mcg_destroy();
Eli Cohenfa417f72010-10-24 21:08:52 -07002483 destroy_workqueue(wq);
Roland Dreier225c7b12007-05-08 18:00:38 -07002484}
2485
2486module_init(mlx4_ib_init);
2487module_exit(mlx4_ib_cleanup);