blob: b179909ac8756b5da36a0eb05cece1543e0fff1c [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>
Moni Shouae26be1b2015-07-30 18:33:29 +030048#include <rdma/ib_cache.h>
49
50#include <net/bonding.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070051
52#include <linux/mlx4/driver.h>
53#include <linux/mlx4/cmd.h>
Matan Barak9433c182014-05-15 15:29:28 +030054#include <linux/mlx4/qp.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070055
56#include "mlx4_ib.h"
57#include "user.h"
58
Jack Morgensteinb1d8eb52012-06-19 11:21:35 +030059#define DRV_NAME MLX4_IB_DRV_NAME
Amir Vadai169a1d82014-02-19 17:47:31 +020060#define DRV_VERSION "2.2-1"
61#define DRV_RELDATE "Feb 2014"
Roland Dreier225c7b12007-05-08 18:00:38 -070062
Hadar Hen Zionf77c0162013-08-14 13:58:31 +030063#define MLX4_IB_FLOW_MAX_PRIO 0xFFF
Matan Baraka37a1a42013-11-07 15:25:16 +020064#define MLX4_IB_FLOW_QPN_MASK 0xFFFFFF
Markus Stockhausen50e2ec92014-08-13 14:07:30 +000065#define MLX4_IB_CARD_REV_A0 0xA0
Hadar Hen Zionf77c0162013-08-14 13:58:31 +030066
Roland Dreier225c7b12007-05-08 18:00:38 -070067MODULE_AUTHOR("Roland Dreier");
68MODULE_DESCRIPTION("Mellanox ConnectX HCA InfiniBand driver");
69MODULE_LICENSE("Dual BSD/GPL");
70MODULE_VERSION(DRV_VERSION);
71
Yishai Hadas56c1d232015-02-12 09:49:43 +020072int mlx4_ib_sm_guid_assign = 0;
Jack Morgensteina0c64a12012-08-03 08:40:49 +000073module_param_named(sm_guid_assign, mlx4_ib_sm_guid_assign, int, 0444);
Yishai Hadas56c1d232015-02-12 09:49:43 +020074MODULE_PARM_DESC(sm_guid_assign, "Enable SM alias_GUID assignment if sm_guid_assign > 0 (Default: 0)");
Jack Morgensteina0c64a12012-08-03 08:40:49 +000075
Roland Dreier68f39482008-02-04 20:20:44 -080076static const char mlx4_ib_version[] =
Roland Dreier225c7b12007-05-08 18:00:38 -070077 DRV_NAME ": Mellanox ConnectX InfiniBand driver v"
78 DRV_VERSION " (" DRV_RELDATE ")\n";
79
Jack Morgenstein3806d082012-08-03 08:40:58 +000080static void do_slave_init(struct mlx4_ib_dev *ibdev, int slave, int do_init);
81
Eli Cohenfa417f72010-10-24 21:08:52 -070082static struct workqueue_struct *wq;
83
Roland Dreier225c7b12007-05-08 18:00:38 -070084static void init_query_mad(struct ib_smp *mad)
85{
86 mad->base_version = 1;
87 mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
88 mad->class_version = 1;
89 mad->method = IB_MGMT_METHOD_GET;
90}
91
Hadar Hen Zionf77c0162013-08-14 13:58:31 +030092static int check_flow_steering_support(struct mlx4_dev *dev)
93{
Matan Barak0a9b7d52013-11-07 15:25:15 +020094 int eth_num_ports = 0;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +030095 int ib_num_ports = 0;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +030096
Matan Barak0a9b7d52013-11-07 15:25:15 +020097 int dmfs = dev->caps.steering_mode == MLX4_STEERING_MODE_DEVICE_MANAGED;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +030098
Matan Barak0a9b7d52013-11-07 15:25:15 +020099 if (dmfs) {
100 int i;
101 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH)
102 eth_num_ports++;
103 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
104 ib_num_ports++;
105 dmfs &= (!ib_num_ports ||
106 (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_DMFS_IPOIB)) &&
107 (!eth_num_ports ||
108 (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FS_EN));
109 if (ib_num_ports && mlx4_is_mfunc(dev)) {
110 pr_warn("Device managed flow steering is unavailable for IB port in multifunction env.\n");
111 dmfs = 0;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +0300112 }
Hadar Hen Zionf77c0162013-08-14 13:58:31 +0300113 }
Matan Barak0a9b7d52013-11-07 15:25:15 +0200114 return dmfs;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +0300115}
116
Jack Morgenstein3dec4872014-09-11 14:11:19 +0300117static int num_ib_ports(struct mlx4_dev *dev)
118{
119 int ib_ports = 0;
120 int i;
121
122 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
123 ib_ports++;
124
125 return ib_ports;
126}
127
Moni Shouae26be1b2015-07-30 18:33:29 +0300128static struct net_device *mlx4_ib_get_netdev(struct ib_device *device, u8 port_num)
129{
130 struct mlx4_ib_dev *ibdev = to_mdev(device);
131 struct net_device *dev;
132
133 rcu_read_lock();
134 dev = mlx4_get_protocol_dev(ibdev->dev, MLX4_PROT_ETH, port_num);
135
136 if (dev) {
137 if (mlx4_is_bonded(ibdev->dev)) {
138 struct net_device *upper = NULL;
139
140 upper = netdev_master_upper_dev_get_rcu(dev);
141 if (upper) {
142 struct net_device *active;
143
144 active = bond_option_active_slave_get_rcu(netdev_priv(upper));
145 if (active)
146 dev = active;
147 }
148 }
149 }
150 if (dev)
151 dev_hold(dev);
152
153 rcu_read_unlock();
154 return dev;
155}
156
157static int mlx4_ib_update_gids(struct gid_entry *gids,
158 struct mlx4_ib_dev *ibdev,
159 u8 port_num)
160{
161 struct mlx4_cmd_mailbox *mailbox;
162 int err;
163 struct mlx4_dev *dev = ibdev->dev;
164 int i;
165 union ib_gid *gid_tbl;
166
167 mailbox = mlx4_alloc_cmd_mailbox(dev);
168 if (IS_ERR(mailbox))
169 return -ENOMEM;
170
171 gid_tbl = mailbox->buf;
172
173 for (i = 0; i < MLX4_MAX_PORT_GIDS; ++i)
174 memcpy(&gid_tbl[i], &gids[i].gid, sizeof(union ib_gid));
175
176 err = mlx4_cmd(dev, mailbox->dma,
177 MLX4_SET_PORT_GID_TABLE << 8 | port_num,
178 1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
179 MLX4_CMD_WRAPPED);
180 if (mlx4_is_bonded(dev))
181 err += mlx4_cmd(dev, mailbox->dma,
182 MLX4_SET_PORT_GID_TABLE << 8 | 2,
183 1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
184 MLX4_CMD_WRAPPED);
185
186 mlx4_free_cmd_mailbox(dev, mailbox);
187 return err;
188}
189
190static int mlx4_ib_add_gid(struct ib_device *device,
191 u8 port_num,
192 unsigned int index,
193 const union ib_gid *gid,
194 const struct ib_gid_attr *attr,
195 void **context)
196{
197 struct mlx4_ib_dev *ibdev = to_mdev(device);
198 struct mlx4_ib_iboe *iboe = &ibdev->iboe;
199 struct mlx4_port_gid_table *port_gid_table;
200 int free = -1, found = -1;
201 int ret = 0;
202 int hw_update = 0;
203 int i;
204 struct gid_entry *gids = NULL;
205
206 if (!rdma_cap_roce_gid_table(device, port_num))
207 return -EINVAL;
208
209 if (port_num > MLX4_MAX_PORTS)
210 return -EINVAL;
211
212 if (!context)
213 return -EINVAL;
214
215 port_gid_table = &iboe->gids[port_num - 1];
216 spin_lock_bh(&iboe->lock);
217 for (i = 0; i < MLX4_MAX_PORT_GIDS; ++i) {
Moni Shouab699a852016-01-14 17:50:33 +0200218 if (!memcmp(&port_gid_table->gids[i].gid, gid, sizeof(*gid)) &&
219 (port_gid_table->gids[i].gid_type == attr->gid_type)) {
Moni Shouae26be1b2015-07-30 18:33:29 +0300220 found = i;
221 break;
222 }
223 if (free < 0 && !memcmp(&port_gid_table->gids[i].gid, &zgid, sizeof(*gid)))
224 free = i; /* HW has space */
225 }
226
227 if (found < 0) {
228 if (free < 0) {
229 ret = -ENOSPC;
230 } else {
231 port_gid_table->gids[free].ctx = kmalloc(sizeof(*port_gid_table->gids[free].ctx), GFP_ATOMIC);
232 if (!port_gid_table->gids[free].ctx) {
233 ret = -ENOMEM;
234 } else {
235 *context = port_gid_table->gids[free].ctx;
236 memcpy(&port_gid_table->gids[free].gid, gid, sizeof(*gid));
Moni Shouab699a852016-01-14 17:50:33 +0200237 port_gid_table->gids[free].gid_type = attr->gid_type;
Moni Shouae26be1b2015-07-30 18:33:29 +0300238 port_gid_table->gids[free].ctx->real_index = free;
239 port_gid_table->gids[free].ctx->refcount = 1;
240 hw_update = 1;
241 }
242 }
243 } else {
244 struct gid_cache_context *ctx = port_gid_table->gids[found].ctx;
245 *context = ctx;
246 ctx->refcount++;
247 }
248 if (!ret && hw_update) {
249 gids = kmalloc(sizeof(*gids) * MLX4_MAX_PORT_GIDS, GFP_ATOMIC);
250 if (!gids) {
251 ret = -ENOMEM;
252 } else {
Moni Shouab699a852016-01-14 17:50:33 +0200253 for (i = 0; i < MLX4_MAX_PORT_GIDS; i++) {
Moni Shouae26be1b2015-07-30 18:33:29 +0300254 memcpy(&gids[i].gid, &port_gid_table->gids[i].gid, sizeof(union ib_gid));
Moni Shouab699a852016-01-14 17:50:33 +0200255 gids[i].gid_type = port_gid_table->gids[i].gid_type;
256 }
Moni Shouae26be1b2015-07-30 18:33:29 +0300257 }
258 }
259 spin_unlock_bh(&iboe->lock);
260
261 if (!ret && hw_update) {
262 ret = mlx4_ib_update_gids(gids, ibdev, port_num);
263 kfree(gids);
264 }
265
266 return ret;
267}
268
269static int mlx4_ib_del_gid(struct ib_device *device,
270 u8 port_num,
271 unsigned int index,
272 void **context)
273{
274 struct gid_cache_context *ctx = *context;
275 struct mlx4_ib_dev *ibdev = to_mdev(device);
276 struct mlx4_ib_iboe *iboe = &ibdev->iboe;
277 struct mlx4_port_gid_table *port_gid_table;
278 int ret = 0;
279 int hw_update = 0;
280 struct gid_entry *gids = NULL;
281
282 if (!rdma_cap_roce_gid_table(device, port_num))
283 return -EINVAL;
284
285 if (port_num > MLX4_MAX_PORTS)
286 return -EINVAL;
287
288 port_gid_table = &iboe->gids[port_num - 1];
289 spin_lock_bh(&iboe->lock);
290 if (ctx) {
291 ctx->refcount--;
292 if (!ctx->refcount) {
293 unsigned int real_index = ctx->real_index;
294
295 memcpy(&port_gid_table->gids[real_index].gid, &zgid, sizeof(zgid));
296 kfree(port_gid_table->gids[real_index].ctx);
297 port_gid_table->gids[real_index].ctx = NULL;
298 hw_update = 1;
299 }
300 }
301 if (!ret && hw_update) {
302 int i;
303
304 gids = kmalloc(sizeof(*gids) * MLX4_MAX_PORT_GIDS, GFP_ATOMIC);
305 if (!gids) {
306 ret = -ENOMEM;
307 } else {
308 for (i = 0; i < MLX4_MAX_PORT_GIDS; i++)
309 memcpy(&gids[i].gid, &port_gid_table->gids[i].gid, sizeof(union ib_gid));
310 }
311 }
312 spin_unlock_bh(&iboe->lock);
313
314 if (!ret && hw_update) {
315 ret = mlx4_ib_update_gids(gids, ibdev, port_num);
316 kfree(gids);
317 }
318 return ret;
319}
320
321int mlx4_ib_gid_index_to_real_index(struct mlx4_ib_dev *ibdev,
322 u8 port_num, int index)
323{
324 struct mlx4_ib_iboe *iboe = &ibdev->iboe;
325 struct gid_cache_context *ctx = NULL;
326 union ib_gid gid;
327 struct mlx4_port_gid_table *port_gid_table;
328 int real_index = -EINVAL;
329 int i;
330 int ret;
331 unsigned long flags;
Moni Shouab699a852016-01-14 17:50:33 +0200332 struct ib_gid_attr attr;
Moni Shouae26be1b2015-07-30 18:33:29 +0300333
334 if (port_num > MLX4_MAX_PORTS)
335 return -EINVAL;
336
337 if (mlx4_is_bonded(ibdev->dev))
338 port_num = 1;
339
340 if (!rdma_cap_roce_gid_table(&ibdev->ib_dev, port_num))
341 return index;
342
Moni Shouab699a852016-01-14 17:50:33 +0200343 ret = ib_get_cached_gid(&ibdev->ib_dev, port_num, index, &gid, &attr);
Moni Shouae26be1b2015-07-30 18:33:29 +0300344 if (ret)
345 return ret;
346
Moni Shouab699a852016-01-14 17:50:33 +0200347 if (attr.ndev)
348 dev_put(attr.ndev);
349
Moni Shouae26be1b2015-07-30 18:33:29 +0300350 if (!memcmp(&gid, &zgid, sizeof(gid)))
351 return -EINVAL;
352
353 spin_lock_irqsave(&iboe->lock, flags);
354 port_gid_table = &iboe->gids[port_num - 1];
355
356 for (i = 0; i < MLX4_MAX_PORT_GIDS; ++i)
Moni Shouab699a852016-01-14 17:50:33 +0200357 if (!memcmp(&port_gid_table->gids[i].gid, &gid, sizeof(gid)) &&
358 attr.gid_type == port_gid_table->gids[i].gid_type) {
Moni Shouae26be1b2015-07-30 18:33:29 +0300359 ctx = port_gid_table->gids[i].ctx;
360 break;
361 }
362 if (ctx)
363 real_index = ctx->real_index;
364 spin_unlock_irqrestore(&iboe->lock, flags);
365 return real_index;
366}
367
Roland Dreier225c7b12007-05-08 18:00:38 -0700368static int mlx4_ib_query_device(struct ib_device *ibdev,
Matan Barak2528e332015-06-11 16:35:25 +0300369 struct ib_device_attr *props,
370 struct ib_udata *uhw)
Roland Dreier225c7b12007-05-08 18:00:38 -0700371{
372 struct mlx4_ib_dev *dev = to_mdev(ibdev);
373 struct ib_smp *in_mad = NULL;
374 struct ib_smp *out_mad = NULL;
375 int err = -ENOMEM;
Jack Morgenstein3dec4872014-09-11 14:11:19 +0300376 int have_ib_ports;
Matan Barak4b664c42015-06-11 16:35:27 +0300377 struct mlx4_uverbs_ex_query_device cmd;
378 struct mlx4_uverbs_ex_query_device_resp resp = {.comp_mask = 0};
379 struct mlx4_clock_params clock_params;
Roland Dreier225c7b12007-05-08 18:00:38 -0700380
Matan Barak4b664c42015-06-11 16:35:27 +0300381 if (uhw->inlen) {
382 if (uhw->inlen < sizeof(cmd))
383 return -EINVAL;
Matan Barak2528e332015-06-11 16:35:25 +0300384
Matan Barak4b664c42015-06-11 16:35:27 +0300385 err = ib_copy_from_udata(&cmd, uhw, sizeof(cmd));
386 if (err)
387 return err;
388
389 if (cmd.comp_mask)
390 return -EINVAL;
391
392 if (cmd.reserved)
393 return -EINVAL;
394 }
395
396 resp.response_length = offsetof(typeof(resp), response_length) +
397 sizeof(resp.response_length);
Roland Dreier225c7b12007-05-08 18:00:38 -0700398 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
399 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
400 if (!in_mad || !out_mad)
401 goto out;
402
403 init_query_mad(in_mad);
404 in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
405
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000406 err = mlx4_MAD_IFC(to_mdev(ibdev), MLX4_MAD_IFC_IGNORE_KEYS,
407 1, NULL, NULL, in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -0700408 if (err)
409 goto out;
410
411 memset(props, 0, sizeof *props);
412
Jack Morgenstein3dec4872014-09-11 14:11:19 +0300413 have_ib_ports = num_ib_ports(dev->dev);
414
Roland Dreier225c7b12007-05-08 18:00:38 -0700415 props->fw_ver = dev->dev->caps.fw_ver;
416 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
417 IB_DEVICE_PORT_ACTIVE_EVENT |
418 IB_DEVICE_SYS_IMAGE_GUID |
Ron Livne521e5752008-07-14 23:48:48 -0700419 IB_DEVICE_RC_RNR_NAK_GEN |
420 IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Roland Dreier225c7b12007-05-08 18:00:38 -0700421 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_PKEY_CNTR)
422 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
423 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_QKEY_CNTR)
424 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
Jack Morgenstein3dec4872014-09-11 14:11:19 +0300425 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_APM && have_ib_ports)
Roland Dreier225c7b12007-05-08 18:00:38 -0700426 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
427 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UD_AV_PORT)
428 props->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
Eli Cohen8ff095e2008-04-16 21:01:10 -0700429 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_IPOIB_CSUM)
430 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
Markus Stockhausen50e2ec92014-08-13 14:07:30 +0000431 if (dev->dev->caps.max_gso_sz &&
432 (dev->dev->rev_id != MLX4_IB_CARD_REV_A0) &&
433 (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BLH))
Eli Cohenb832be12008-04-16 21:09:27 -0700434 props->device_cap_flags |= IB_DEVICE_UD_TSO;
Roland Dreier95d04f02008-07-23 08:12:26 -0700435 if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_RESERVED_LKEY)
436 props->device_cap_flags |= IB_DEVICE_LOCAL_DMA_LKEY;
437 if ((dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_LOCAL_INV) &&
438 (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_REMOTE_INV) &&
439 (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_FAST_REG_WR))
440 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Sean Hefty0a1405d2011-06-02 11:32:15 -0700441 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC)
442 props->device_cap_flags |= IB_DEVICE_XRC;
Shani Michaelib4253882013-02-06 16:19:16 +0000443 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_MEM_WINDOW)
444 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW;
445 if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_TYPE_2_WIN) {
446 if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_WIN_TYPE_2B)
447 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW_TYPE_2B;
448 else
449 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW_TYPE_2A;
Matan Barak0a9b7d52013-11-07 15:25:15 +0200450 if (dev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED)
Hadar Hen Zionf77c0162013-08-14 13:58:31 +0300451 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
Shani Michaelib4253882013-02-06 16:19:16 +0000452 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700453
Bodong Wang070b3992015-09-22 23:18:11 +0300454 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
455
Roland Dreier225c7b12007-05-08 18:00:38 -0700456 props->vendor_id = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
457 0xffffff;
Yishai Hadas872bf2f2015-01-25 16:59:35 +0200458 props->vendor_part_id = dev->dev->persist->pdev->device;
Roland Dreier225c7b12007-05-08 18:00:38 -0700459 props->hw_ver = be32_to_cpup((__be32 *) (out_mad->data + 32));
460 memcpy(&props->sys_image_guid, out_mad->data + 4, 8);
461
462 props->max_mr_size = ~0ull;
463 props->page_size_cap = dev->dev->caps.page_size_cap;
Jack Morgenstein5a0d0a62013-11-03 10:03:23 +0200464 props->max_qp = dev->dev->quotas.qp;
Sagi Grimbergfc2d0042012-05-24 16:08:08 +0300465 props->max_qp_wr = dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE;
Roland Dreier225c7b12007-05-08 18:00:38 -0700466 props->max_sge = min(dev->dev->caps.max_sq_sg,
467 dev->dev->caps.max_rq_sg);
Sagi Grimberga5e14ba2015-10-28 13:28:15 +0200468 props->max_sge_rd = MLX4_MAX_SGE_RD;
Jack Morgenstein5a0d0a62013-11-03 10:03:23 +0200469 props->max_cq = dev->dev->quotas.cq;
Roland Dreier225c7b12007-05-08 18:00:38 -0700470 props->max_cqe = dev->dev->caps.max_cqes;
Jack Morgenstein5a0d0a62013-11-03 10:03:23 +0200471 props->max_mr = dev->dev->quotas.mpt;
Roland Dreier225c7b12007-05-08 18:00:38 -0700472 props->max_pd = dev->dev->caps.num_pds - dev->dev->caps.reserved_pds;
473 props->max_qp_rd_atom = dev->dev->caps.max_qp_dest_rdma;
474 props->max_qp_init_rd_atom = dev->dev->caps.max_qp_init_rdma;
475 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Jack Morgenstein5a0d0a62013-11-03 10:03:23 +0200476 props->max_srq = dev->dev->quotas.srq;
Jack Morgensteinc8681f12007-06-21 13:39:10 -0700477 props->max_srq_wr = dev->dev->caps.max_srq_wqes - 1;
Roland Dreier225c7b12007-05-08 18:00:38 -0700478 props->max_srq_sge = dev->dev->caps.max_srq_sge;
Eli Cohen5a0fd092010-10-07 16:24:16 +0200479 props->max_fast_reg_page_list_len = MLX4_MAX_FAST_REG_PAGES;
Roland Dreier225c7b12007-05-08 18:00:38 -0700480 props->local_ca_ack_delay = dev->dev->caps.local_ca_ack_delay;
481 props->atomic_cap = dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_ATOMIC ?
482 IB_ATOMIC_HCA : IB_ATOMIC_NONE;
Dotan Barak47e956b2012-07-11 15:39:29 +0000483 props->masked_atomic_cap = props->atomic_cap;
Roland Dreier5ae2a7a2007-06-18 08:15:02 -0700484 props->max_pkeys = dev->dev->caps.pkey_table_len[1];
Roland Dreier225c7b12007-05-08 18:00:38 -0700485 props->max_mcast_grp = dev->dev->caps.num_mgms + dev->dev->caps.num_amgms;
486 props->max_mcast_qp_attach = dev->dev->caps.num_qp_per_mgm;
487 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
488 props->max_mcast_grp;
Eli Cohena5bbe892012-02-09 18:10:06 +0200489 props->max_map_per_fmr = dev->dev->caps.max_fmr_maps;
Matan Barak4b664c42015-06-11 16:35:27 +0300490 props->hca_core_clock = dev->dev->caps.hca_core_clock * 1000UL;
491 props->timestamp_mask = 0xFFFFFFFFFFFFULL;
Roland Dreier225c7b12007-05-08 18:00:38 -0700492
Matan Barak8a7ff142015-07-01 14:31:02 +0300493 if (!mlx4_is_slave(dev->dev))
494 err = mlx4_get_internal_clock_params(dev->dev, &clock_params);
Matan Barak4b664c42015-06-11 16:35:27 +0300495
496 if (uhw->outlen >= resp.response_length + sizeof(resp.hca_core_clock_offset)) {
Matan Barak4b664c42015-06-11 16:35:27 +0300497 resp.response_length += sizeof(resp.hca_core_clock_offset);
Matan Barak8a7ff142015-07-01 14:31:02 +0300498 if (!err && !mlx4_is_slave(dev->dev)) {
499 resp.comp_mask |= QUERY_DEVICE_RESP_MASK_TIMESTAMP;
500 resp.hca_core_clock_offset = clock_params.offset % PAGE_SIZE;
501 }
Matan Barak4b664c42015-06-11 16:35:27 +0300502 }
503
504 if (uhw->outlen) {
505 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
506 if (err)
507 goto out;
508 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700509out:
510 kfree(in_mad);
511 kfree(out_mad);
512
513 return err;
514}
515
Eli Cohenfa417f72010-10-24 21:08:52 -0700516static enum rdma_link_layer
517mlx4_ib_port_link_layer(struct ib_device *device, u8 port_num)
518{
519 struct mlx4_dev *dev = to_mdev(device)->dev;
520
Jack Morgenstein65dab252011-12-13 04:10:41 +0000521 return dev->caps.port_mask[port_num] == MLX4_PORT_TYPE_IB ?
Eli Cohenfa417f72010-10-24 21:08:52 -0700522 IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
523}
524
525static int ib_link_query_port(struct ib_device *ibdev, u8 port,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000526 struct ib_port_attr *props, int netw_view)
Eli Cohenfa417f72010-10-24 21:08:52 -0700527{
Or Gerlitza9c766b2012-01-11 19:00:29 +0200528 struct ib_smp *in_mad = NULL;
529 struct ib_smp *out_mad = NULL;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300530 int ext_active_speed;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000531 int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
Or Gerlitza9c766b2012-01-11 19:00:29 +0200532 int err = -ENOMEM;
533
534 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
535 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
536 if (!in_mad || !out_mad)
537 goto out;
538
539 init_query_mad(in_mad);
540 in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
541 in_mad->attr_mod = cpu_to_be32(port);
542
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000543 if (mlx4_is_mfunc(to_mdev(ibdev)->dev) && netw_view)
544 mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
545
546 err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port, NULL, NULL,
Or Gerlitza9c766b2012-01-11 19:00:29 +0200547 in_mad, out_mad);
548 if (err)
549 goto out;
550
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300551
Eli Cohenfa417f72010-10-24 21:08:52 -0700552 props->lid = be16_to_cpup((__be16 *) (out_mad->data + 16));
553 props->lmc = out_mad->data[34] & 0x7;
554 props->sm_lid = be16_to_cpup((__be16 *) (out_mad->data + 18));
555 props->sm_sl = out_mad->data[36] & 0xf;
556 props->state = out_mad->data[32] & 0xf;
557 props->phys_state = out_mad->data[33] >> 4;
558 props->port_cap_flags = be32_to_cpup((__be32 *) (out_mad->data + 20));
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000559 if (netw_view)
560 props->gid_tbl_len = out_mad->data[50];
561 else
562 props->gid_tbl_len = to_mdev(ibdev)->dev->caps.gid_table_len[port];
Eli Cohenfa417f72010-10-24 21:08:52 -0700563 props->max_msg_sz = to_mdev(ibdev)->dev->caps.max_msg_sz;
564 props->pkey_tbl_len = to_mdev(ibdev)->dev->caps.pkey_table_len[port];
565 props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46));
566 props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));
567 props->active_width = out_mad->data[31] & 0xf;
568 props->active_speed = out_mad->data[35] >> 4;
569 props->max_mtu = out_mad->data[41] & 0xf;
570 props->active_mtu = out_mad->data[36] >> 4;
571 props->subnet_timeout = out_mad->data[51] & 0x1f;
572 props->max_vl_num = out_mad->data[37] >> 4;
573 props->init_type_reply = out_mad->data[41] >> 4;
574
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300575 /* Check if extended speeds (EDR/FDR/...) are supported */
576 if (props->port_cap_flags & IB_PORT_EXTENDED_SPEEDS_SUP) {
577 ext_active_speed = out_mad->data[62] >> 4;
578
579 switch (ext_active_speed) {
580 case 1:
Or Gerlitz2e966912012-02-28 18:49:50 +0200581 props->active_speed = IB_SPEED_FDR;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300582 break;
583 case 2:
Or Gerlitz2e966912012-02-28 18:49:50 +0200584 props->active_speed = IB_SPEED_EDR;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300585 break;
586 }
587 }
588
589 /* If reported active speed is QDR, check if is FDR-10 */
Or Gerlitz2e966912012-02-28 18:49:50 +0200590 if (props->active_speed == IB_SPEED_QDR) {
Or Gerlitz8154c072012-03-06 15:50:50 +0200591 init_query_mad(in_mad);
592 in_mad->attr_id = MLX4_ATTR_EXTENDED_PORT_INFO;
593 in_mad->attr_mod = cpu_to_be32(port);
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300594
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000595 err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port,
Or Gerlitz8154c072012-03-06 15:50:50 +0200596 NULL, NULL, in_mad, out_mad);
597 if (err)
Jesper Juhlbf6b47d2012-04-11 23:43:29 +0200598 goto out;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300599
Or Gerlitz8154c072012-03-06 15:50:50 +0200600 /* Checking LinkSpeedActive for FDR-10 */
601 if (out_mad->data[15] & 0x1)
602 props->active_speed = IB_SPEED_FDR10;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300603 }
Or Gerlitzd2ef4062012-04-02 17:45:20 +0300604
605 /* Avoid wrong speed value returned by FW if the IB link is down. */
606 if (props->state == IB_PORT_DOWN)
607 props->active_speed = IB_SPEED_SDR;
608
Or Gerlitza9c766b2012-01-11 19:00:29 +0200609out:
610 kfree(in_mad);
611 kfree(out_mad);
612 return err;
Eli Cohenfa417f72010-10-24 21:08:52 -0700613}
614
615static u8 state_to_phys_state(enum ib_port_state state)
616{
617 return state == IB_PORT_ACTIVE ? 5 : 3;
618}
619
620static int eth_link_query_port(struct ib_device *ibdev, u8 port,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000621 struct ib_port_attr *props, int netw_view)
Eli Cohenfa417f72010-10-24 21:08:52 -0700622{
Or Gerlitza9c766b2012-01-11 19:00:29 +0200623
624 struct mlx4_ib_dev *mdev = to_mdev(ibdev);
625 struct mlx4_ib_iboe *iboe = &mdev->iboe;
Eli Cohenfa417f72010-10-24 21:08:52 -0700626 struct net_device *ndev;
627 enum ib_mtu tmp;
Or Gerlitza9c766b2012-01-11 19:00:29 +0200628 struct mlx4_cmd_mailbox *mailbox;
629 int err = 0;
Moni Shouaa5750092015-02-03 16:48:37 +0200630 int is_bonded = mlx4_is_bonded(mdev->dev);
Eli Cohenfa417f72010-10-24 21:08:52 -0700631
Or Gerlitza9c766b2012-01-11 19:00:29 +0200632 mailbox = mlx4_alloc_cmd_mailbox(mdev->dev);
633 if (IS_ERR(mailbox))
634 return PTR_ERR(mailbox);
635
636 err = mlx4_cmd_box(mdev->dev, 0, mailbox->dma, port, 0,
637 MLX4_CMD_QUERY_PORT, MLX4_CMD_TIME_CLASS_B,
638 MLX4_CMD_WRAPPED);
639 if (err)
640 goto out;
641
642 props->active_width = (((u8 *)mailbox->buf)[5] == 0x40) ?
643 IB_WIDTH_4X : IB_WIDTH_1X;
Or Gerlitz2e966912012-02-28 18:49:50 +0200644 props->active_speed = IB_SPEED_QDR;
Moni Shouab4a26a22014-02-09 11:54:34 +0200645 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_IP_BASED_GIDS;
Or Gerlitza9c766b2012-01-11 19:00:29 +0200646 props->gid_tbl_len = mdev->dev->caps.gid_table_len[port];
647 props->max_msg_sz = mdev->dev->caps.max_msg_sz;
Eli Cohenfa417f72010-10-24 21:08:52 -0700648 props->pkey_tbl_len = 1;
Or Gerlitzbcacb892011-10-10 10:53:41 +0200649 props->max_mtu = IB_MTU_4096;
Or Gerlitza9c766b2012-01-11 19:00:29 +0200650 props->max_vl_num = 2;
Eli Cohenfa417f72010-10-24 21:08:52 -0700651 props->state = IB_PORT_DOWN;
652 props->phys_state = state_to_phys_state(props->state);
653 props->active_mtu = IB_MTU_256;
Jack Morgensteindba3ad22014-08-21 14:28:41 +0300654 spin_lock_bh(&iboe->lock);
Eli Cohenfa417f72010-10-24 21:08:52 -0700655 ndev = iboe->netdevs[port - 1];
Moni Shoua5070cd22015-07-30 18:33:30 +0300656 if (ndev && is_bonded) {
657 rcu_read_lock(); /* required to get upper dev */
658 ndev = netdev_master_upper_dev_get_rcu(ndev);
659 rcu_read_unlock();
660 }
Eli Cohenfa417f72010-10-24 21:08:52 -0700661 if (!ndev)
Or Gerlitza9c766b2012-01-11 19:00:29 +0200662 goto out_unlock;
Eli Cohenfa417f72010-10-24 21:08:52 -0700663
664 tmp = iboe_get_mtu(ndev->mtu);
665 props->active_mtu = tmp ? min(props->max_mtu, tmp) : IB_MTU_256;
666
Eli Cohen21d606092010-11-11 21:05:58 +0000667 props->state = (netif_running(ndev) && netif_carrier_ok(ndev)) ?
Eli Cohenfa417f72010-10-24 21:08:52 -0700668 IB_PORT_ACTIVE : IB_PORT_DOWN;
669 props->phys_state = state_to_phys_state(props->state);
Or Gerlitza9c766b2012-01-11 19:00:29 +0200670out_unlock:
Jack Morgensteindba3ad22014-08-21 14:28:41 +0300671 spin_unlock_bh(&iboe->lock);
Or Gerlitza9c766b2012-01-11 19:00:29 +0200672out:
673 mlx4_free_cmd_mailbox(mdev->dev, mailbox);
674 return err;
Eli Cohenfa417f72010-10-24 21:08:52 -0700675}
676
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000677int __mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
678 struct ib_port_attr *props, int netw_view)
Roland Dreier225c7b12007-05-08 18:00:38 -0700679{
Or Gerlitza9c766b2012-01-11 19:00:29 +0200680 int err;
Roland Dreier225c7b12007-05-08 18:00:38 -0700681
682 memset(props, 0, sizeof *props);
683
Eli Cohenfa417f72010-10-24 21:08:52 -0700684 err = mlx4_ib_port_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND ?
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000685 ib_link_query_port(ibdev, port, props, netw_view) :
686 eth_link_query_port(ibdev, port, props, netw_view);
Roland Dreier225c7b12007-05-08 18:00:38 -0700687
688 return err;
689}
690
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000691static int mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
692 struct ib_port_attr *props)
693{
694 /* returns host view */
695 return __mlx4_ib_query_port(ibdev, port, props, 0);
696}
697
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000698int __mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
699 union ib_gid *gid, int netw_view)
Roland Dreier225c7b12007-05-08 18:00:38 -0700700{
701 struct ib_smp *in_mad = NULL;
702 struct ib_smp *out_mad = NULL;
703 int err = -ENOMEM;
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000704 struct mlx4_ib_dev *dev = to_mdev(ibdev);
705 int clear = 0;
706 int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
Roland Dreier225c7b12007-05-08 18:00:38 -0700707
708 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
709 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
710 if (!in_mad || !out_mad)
711 goto out;
712
713 init_query_mad(in_mad);
714 in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
715 in_mad->attr_mod = cpu_to_be32(port);
716
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000717 if (mlx4_is_mfunc(dev->dev) && netw_view)
718 mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
719
720 err = mlx4_MAD_IFC(dev, mad_ifc_flags, port, NULL, NULL, in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -0700721 if (err)
722 goto out;
723
724 memcpy(gid->raw, out_mad->data + 8, 8);
725
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000726 if (mlx4_is_mfunc(dev->dev) && !netw_view) {
727 if (index) {
728 /* For any index > 0, return the null guid */
729 err = 0;
730 clear = 1;
731 goto out;
732 }
733 }
734
Roland Dreier225c7b12007-05-08 18:00:38 -0700735 init_query_mad(in_mad);
736 in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
737 in_mad->attr_mod = cpu_to_be32(index / 8);
738
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000739 err = mlx4_MAD_IFC(dev, mad_ifc_flags, port,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000740 NULL, NULL, in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -0700741 if (err)
742 goto out;
743
744 memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
745
746out:
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000747 if (clear)
748 memset(gid->raw + 8, 0, 8);
Roland Dreier225c7b12007-05-08 18:00:38 -0700749 kfree(in_mad);
750 kfree(out_mad);
751 return err;
752}
753
Eli Cohenfa417f72010-10-24 21:08:52 -0700754static int mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
755 union ib_gid *gid)
756{
Moni Shoua5070cd22015-07-30 18:33:30 +0300757 int ret;
758
759 if (rdma_protocol_ib(ibdev, port))
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000760 return __mlx4_ib_query_gid(ibdev, port, index, gid, 0);
Moni Shoua5070cd22015-07-30 18:33:30 +0300761
762 if (!rdma_protocol_roce(ibdev, port))
763 return -ENODEV;
764
765 if (!rdma_cap_roce_gid_table(ibdev, port))
766 return -ENODEV;
767
Matan Barak55ee3ab2015-10-15 18:38:45 +0300768 ret = ib_get_cached_gid(ibdev, port, index, gid, NULL);
Moni Shoua5070cd22015-07-30 18:33:30 +0300769 if (ret == -EAGAIN) {
770 memcpy(gid, &zgid, sizeof(*gid));
771 return 0;
772 }
773
774 return ret;
Eli Cohenfa417f72010-10-24 21:08:52 -0700775}
776
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000777int __mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
778 u16 *pkey, int netw_view)
Roland Dreier225c7b12007-05-08 18:00:38 -0700779{
780 struct ib_smp *in_mad = NULL;
781 struct ib_smp *out_mad = NULL;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000782 int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
Roland Dreier225c7b12007-05-08 18:00:38 -0700783 int err = -ENOMEM;
784
785 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
786 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
787 if (!in_mad || !out_mad)
788 goto out;
789
790 init_query_mad(in_mad);
791 in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
792 in_mad->attr_mod = cpu_to_be32(index / 32);
793
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000794 if (mlx4_is_mfunc(to_mdev(ibdev)->dev) && netw_view)
795 mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
796
797 err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port, NULL, NULL,
798 in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -0700799 if (err)
800 goto out;
801
802 *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
803
804out:
805 kfree(in_mad);
806 kfree(out_mad);
807 return err;
808}
809
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000810static int mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
811{
812 return __mlx4_ib_query_pkey(ibdev, port, index, pkey, 0);
813}
814
Roland Dreier225c7b12007-05-08 18:00:38 -0700815static int mlx4_ib_modify_device(struct ib_device *ibdev, int mask,
816 struct ib_device_modify *props)
817{
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000818 struct mlx4_cmd_mailbox *mailbox;
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000819 unsigned long flags;
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000820
Roland Dreier225c7b12007-05-08 18:00:38 -0700821 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
822 return -EOPNOTSUPP;
823
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000824 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
825 return 0;
826
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +0000827 if (mlx4_is_slave(to_mdev(ibdev)->dev))
828 return -EOPNOTSUPP;
829
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000830 spin_lock_irqsave(&to_mdev(ibdev)->sm_lock, flags);
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000831 memcpy(ibdev->node_desc, props->node_desc, 64);
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000832 spin_unlock_irqrestore(&to_mdev(ibdev)->sm_lock, flags);
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000833
834 /*
835 * If possible, pass node desc to FW, so it can generate
836 * a 144 trap. If cmd fails, just ignore.
837 */
838 mailbox = mlx4_alloc_cmd_mailbox(to_mdev(ibdev)->dev);
839 if (IS_ERR(mailbox))
840 return 0;
841
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000842 memcpy(mailbox->buf, props->node_desc, 64);
843 mlx4_cmd(to_mdev(ibdev)->dev, mailbox->dma, 1, 0,
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +0000844 MLX4_CMD_SET_NODE, MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000845
846 mlx4_free_cmd_mailbox(to_mdev(ibdev)->dev, mailbox);
Roland Dreier225c7b12007-05-08 18:00:38 -0700847
848 return 0;
849}
850
Jack Morgenstein61565012014-05-29 16:31:01 +0300851static int mlx4_ib_SET_PORT(struct mlx4_ib_dev *dev, u8 port, int reset_qkey_viols,
852 u32 cap_mask)
Roland Dreier225c7b12007-05-08 18:00:38 -0700853{
854 struct mlx4_cmd_mailbox *mailbox;
855 int err;
856
857 mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
858 if (IS_ERR(mailbox))
859 return PTR_ERR(mailbox);
860
Roland Dreier5ae2a7a2007-06-18 08:15:02 -0700861 if (dev->dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
862 *(u8 *) mailbox->buf = !!reset_qkey_viols << 6;
863 ((__be32 *) mailbox->buf)[2] = cpu_to_be32(cap_mask);
864 } else {
865 ((u8 *) mailbox->buf)[3] = !!reset_qkey_viols;
866 ((__be32 *) mailbox->buf)[1] = cpu_to_be32(cap_mask);
867 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700868
Ido Shamaya130b592015-04-02 16:31:19 +0300869 err = mlx4_cmd(dev->dev, mailbox->dma, port, MLX4_SET_PORT_IB_OPCODE,
870 MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
871 MLX4_CMD_WRAPPED);
Roland Dreier225c7b12007-05-08 18:00:38 -0700872
873 mlx4_free_cmd_mailbox(dev->dev, mailbox);
874 return err;
875}
876
877static int mlx4_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
878 struct ib_port_modify *props)
879{
Jack Morgenstein61565012014-05-29 16:31:01 +0300880 struct mlx4_ib_dev *mdev = to_mdev(ibdev);
881 u8 is_eth = mdev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
Roland Dreier225c7b12007-05-08 18:00:38 -0700882 struct ib_port_attr attr;
883 u32 cap_mask;
884 int err;
885
Jack Morgenstein61565012014-05-29 16:31:01 +0300886 /* return OK if this is RoCE. CM calls ib_modify_port() regardless
887 * of whether port link layer is ETH or IB. For ETH ports, qkey
888 * violations and port capabilities are not meaningful.
889 */
890 if (is_eth)
891 return 0;
892
893 mutex_lock(&mdev->cap_mask_mutex);
Roland Dreier225c7b12007-05-08 18:00:38 -0700894
895 err = mlx4_ib_query_port(ibdev, port, &attr);
896 if (err)
897 goto out;
898
899 cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
900 ~props->clr_port_cap_mask;
901
Jack Morgenstein61565012014-05-29 16:31:01 +0300902 err = mlx4_ib_SET_PORT(mdev, port,
903 !!(mask & IB_PORT_RESET_QKEY_CNTR),
904 cap_mask);
Roland Dreier225c7b12007-05-08 18:00:38 -0700905
906out:
907 mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
908 return err;
909}
910
911static struct ib_ucontext *mlx4_ib_alloc_ucontext(struct ib_device *ibdev,
912 struct ib_udata *udata)
913{
914 struct mlx4_ib_dev *dev = to_mdev(ibdev);
915 struct mlx4_ib_ucontext *context;
Or Gerlitz08ff3232012-10-21 14:59:24 +0000916 struct mlx4_ib_alloc_ucontext_resp_v3 resp_v3;
Roland Dreier225c7b12007-05-08 18:00:38 -0700917 struct mlx4_ib_alloc_ucontext_resp resp;
918 int err;
919
Jack Morgenstein3b4a8cd2009-09-05 20:24:50 -0700920 if (!dev->ib_active)
921 return ERR_PTR(-EAGAIN);
922
Or Gerlitz08ff3232012-10-21 14:59:24 +0000923 if (ibdev->uverbs_abi_ver == MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION) {
924 resp_v3.qp_tab_size = dev->dev->caps.num_qps;
925 resp_v3.bf_reg_size = dev->dev->caps.bf_reg_size;
926 resp_v3.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
927 } else {
928 resp.dev_caps = dev->dev->caps.userspace_caps;
929 resp.qp_tab_size = dev->dev->caps.num_qps;
930 resp.bf_reg_size = dev->dev->caps.bf_reg_size;
931 resp.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
932 resp.cqe_size = dev->dev->caps.cqe_size;
933 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700934
Yishai Hadasae184dd2015-08-13 18:32:06 +0300935 context = kzalloc(sizeof(*context), GFP_KERNEL);
Roland Dreier225c7b12007-05-08 18:00:38 -0700936 if (!context)
937 return ERR_PTR(-ENOMEM);
938
939 err = mlx4_uar_alloc(to_mdev(ibdev)->dev, &context->uar);
940 if (err) {
941 kfree(context);
942 return ERR_PTR(err);
943 }
944
945 INIT_LIST_HEAD(&context->db_page_list);
946 mutex_init(&context->db_page_mutex);
947
Or Gerlitz08ff3232012-10-21 14:59:24 +0000948 if (ibdev->uverbs_abi_ver == MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION)
949 err = ib_copy_to_udata(udata, &resp_v3, sizeof(resp_v3));
950 else
951 err = ib_copy_to_udata(udata, &resp, sizeof(resp));
952
Roland Dreier225c7b12007-05-08 18:00:38 -0700953 if (err) {
954 mlx4_uar_free(to_mdev(ibdev)->dev, &context->uar);
955 kfree(context);
956 return ERR_PTR(-EFAULT);
957 }
958
959 return &context->ibucontext;
960}
961
962static int mlx4_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
963{
964 struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
965
966 mlx4_uar_free(to_mdev(ibcontext->device)->dev, &context->uar);
967 kfree(context);
968
969 return 0;
970}
971
Yishai Hadasae184dd2015-08-13 18:32:06 +0300972static void mlx4_ib_vma_open(struct vm_area_struct *area)
973{
974 /* vma_open is called when a new VMA is created on top of our VMA.
975 * This is done through either mremap flow or split_vma (usually due
976 * to mlock, madvise, munmap, etc.). We do not support a clone of the
977 * vma, as this VMA is strongly hardware related. Therefore we set the
978 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
979 * calling us again and trying to do incorrect actions. We assume that
980 * the original vma size is exactly a single page that there will be no
981 * "splitting" operations on.
982 */
983 area->vm_ops = NULL;
984}
985
986static void mlx4_ib_vma_close(struct vm_area_struct *area)
987{
988 struct mlx4_ib_vma_private_data *mlx4_ib_vma_priv_data;
989
990 /* It's guaranteed that all VMAs opened on a FD are closed before the
991 * file itself is closed, therefore no sync is needed with the regular
992 * closing flow. (e.g. mlx4_ib_dealloc_ucontext) However need a sync
993 * with accessing the vma as part of mlx4_ib_disassociate_ucontext.
994 * The close operation is usually called under mm->mmap_sem except when
995 * process is exiting. The exiting case is handled explicitly as part
996 * of mlx4_ib_disassociate_ucontext.
997 */
998 mlx4_ib_vma_priv_data = (struct mlx4_ib_vma_private_data *)
999 area->vm_private_data;
1000
1001 /* set the vma context pointer to null in the mlx4_ib driver's private
1002 * data to protect against a race condition in mlx4_ib_dissassociate_ucontext().
1003 */
1004 mlx4_ib_vma_priv_data->vma = NULL;
1005}
1006
1007static const struct vm_operations_struct mlx4_ib_vm_ops = {
1008 .open = mlx4_ib_vma_open,
1009 .close = mlx4_ib_vma_close
1010};
1011
1012static void mlx4_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1013{
1014 int i;
1015 int ret = 0;
1016 struct vm_area_struct *vma;
1017 struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
1018 struct task_struct *owning_process = NULL;
1019 struct mm_struct *owning_mm = NULL;
1020
1021 owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
1022 if (!owning_process)
1023 return;
1024
1025 owning_mm = get_task_mm(owning_process);
1026 if (!owning_mm) {
1027 pr_info("no mm, disassociate ucontext is pending task termination\n");
1028 while (1) {
1029 /* make sure that task is dead before returning, it may
1030 * prevent a rare case of module down in parallel to a
1031 * call to mlx4_ib_vma_close.
1032 */
1033 put_task_struct(owning_process);
1034 msleep(1);
1035 owning_process = get_pid_task(ibcontext->tgid,
1036 PIDTYPE_PID);
1037 if (!owning_process ||
1038 owning_process->state == TASK_DEAD) {
1039 pr_info("disassociate ucontext done, task was terminated\n");
1040 /* in case task was dead need to release the task struct */
1041 if (owning_process)
1042 put_task_struct(owning_process);
1043 return;
1044 }
1045 }
1046 }
1047
1048 /* need to protect from a race on closing the vma as part of
1049 * mlx4_ib_vma_close().
1050 */
1051 down_read(&owning_mm->mmap_sem);
1052 for (i = 0; i < HW_BAR_COUNT; i++) {
1053 vma = context->hw_bar_info[i].vma;
1054 if (!vma)
1055 continue;
1056
1057 ret = zap_vma_ptes(context->hw_bar_info[i].vma,
1058 context->hw_bar_info[i].vma->vm_start,
1059 PAGE_SIZE);
1060 if (ret) {
1061 pr_err("Error: zap_vma_ptes failed for index=%d, ret=%d\n", i, ret);
1062 BUG_ON(1);
1063 }
1064
1065 /* context going to be destroyed, should not access ops any more */
1066 context->hw_bar_info[i].vma->vm_ops = NULL;
1067 }
1068
1069 up_read(&owning_mm->mmap_sem);
1070 mmput(owning_mm);
1071 put_task_struct(owning_process);
1072}
1073
1074static void mlx4_ib_set_vma_data(struct vm_area_struct *vma,
1075 struct mlx4_ib_vma_private_data *vma_private_data)
1076{
1077 vma_private_data->vma = vma;
1078 vma->vm_private_data = vma_private_data;
1079 vma->vm_ops = &mlx4_ib_vm_ops;
1080}
1081
Roland Dreier225c7b12007-05-08 18:00:38 -07001082static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
1083{
1084 struct mlx4_ib_dev *dev = to_mdev(context->device);
Yishai Hadasae184dd2015-08-13 18:32:06 +03001085 struct mlx4_ib_ucontext *mucontext = to_mucontext(context);
Roland Dreier225c7b12007-05-08 18:00:38 -07001086
1087 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1088 return -EINVAL;
1089
1090 if (vma->vm_pgoff == 0) {
Yishai Hadasae184dd2015-08-13 18:32:06 +03001091 /* We prevent double mmaping on same context */
1092 if (mucontext->hw_bar_info[HW_BAR_DB].vma)
1093 return -EINVAL;
1094
Roland Dreier225c7b12007-05-08 18:00:38 -07001095 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1096
1097 if (io_remap_pfn_range(vma, vma->vm_start,
1098 to_mucontext(context)->uar.pfn,
1099 PAGE_SIZE, vma->vm_page_prot))
1100 return -EAGAIN;
Yishai Hadasae184dd2015-08-13 18:32:06 +03001101
1102 mlx4_ib_set_vma_data(vma, &mucontext->hw_bar_info[HW_BAR_DB]);
1103
Roland Dreier225c7b12007-05-08 18:00:38 -07001104 } else if (vma->vm_pgoff == 1 && dev->dev->caps.bf_reg_size != 0) {
Yishai Hadasae184dd2015-08-13 18:32:06 +03001105 /* We prevent double mmaping on same context */
1106 if (mucontext->hw_bar_info[HW_BAR_BF].vma)
1107 return -EINVAL;
1108
Roland Dreiere1d60ec2009-03-30 08:31:05 -07001109 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
Roland Dreier225c7b12007-05-08 18:00:38 -07001110
1111 if (io_remap_pfn_range(vma, vma->vm_start,
1112 to_mucontext(context)->uar.pfn +
1113 dev->dev->caps.num_uars,
1114 PAGE_SIZE, vma->vm_page_prot))
1115 return -EAGAIN;
Yishai Hadasae184dd2015-08-13 18:32:06 +03001116
1117 mlx4_ib_set_vma_data(vma, &mucontext->hw_bar_info[HW_BAR_BF]);
1118
Matan Barak52033cf2015-06-11 16:35:26 +03001119 } else if (vma->vm_pgoff == 3) {
1120 struct mlx4_clock_params params;
Yishai Hadasae184dd2015-08-13 18:32:06 +03001121 int ret;
1122
1123 /* We prevent double mmaping on same context */
1124 if (mucontext->hw_bar_info[HW_BAR_CLOCK].vma)
1125 return -EINVAL;
1126
1127 ret = mlx4_get_internal_clock_params(dev->dev, &params);
Matan Barak52033cf2015-06-11 16:35:26 +03001128
1129 if (ret)
1130 return ret;
1131
1132 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1133 if (io_remap_pfn_range(vma, vma->vm_start,
1134 (pci_resource_start(dev->dev->persist->pdev,
1135 params.bar) +
1136 params.offset)
1137 >> PAGE_SHIFT,
1138 PAGE_SIZE, vma->vm_page_prot))
1139 return -EAGAIN;
Yishai Hadasae184dd2015-08-13 18:32:06 +03001140
1141 mlx4_ib_set_vma_data(vma,
1142 &mucontext->hw_bar_info[HW_BAR_CLOCK]);
Matan Barak52033cf2015-06-11 16:35:26 +03001143 } else {
Roland Dreier225c7b12007-05-08 18:00:38 -07001144 return -EINVAL;
Matan Barak52033cf2015-06-11 16:35:26 +03001145 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001146
1147 return 0;
1148}
1149
1150static struct ib_pd *mlx4_ib_alloc_pd(struct ib_device *ibdev,
1151 struct ib_ucontext *context,
1152 struct ib_udata *udata)
1153{
1154 struct mlx4_ib_pd *pd;
1155 int err;
1156
1157 pd = kmalloc(sizeof *pd, GFP_KERNEL);
1158 if (!pd)
1159 return ERR_PTR(-ENOMEM);
1160
1161 err = mlx4_pd_alloc(to_mdev(ibdev)->dev, &pd->pdn);
1162 if (err) {
1163 kfree(pd);
1164 return ERR_PTR(err);
1165 }
1166
1167 if (context)
1168 if (ib_copy_to_udata(udata, &pd->pdn, sizeof (__u32))) {
1169 mlx4_pd_free(to_mdev(ibdev)->dev, pd->pdn);
1170 kfree(pd);
1171 return ERR_PTR(-EFAULT);
1172 }
1173
1174 return &pd->ibpd;
1175}
1176
1177static int mlx4_ib_dealloc_pd(struct ib_pd *pd)
1178{
1179 mlx4_pd_free(to_mdev(pd->device)->dev, to_mpd(pd)->pdn);
1180 kfree(pd);
1181
1182 return 0;
1183}
1184
Sean Hefty012a8ff2011-06-02 09:01:33 -07001185static struct ib_xrcd *mlx4_ib_alloc_xrcd(struct ib_device *ibdev,
1186 struct ib_ucontext *context,
1187 struct ib_udata *udata)
1188{
1189 struct mlx4_ib_xrcd *xrcd;
Matan Barak8e372102015-06-11 16:35:21 +03001190 struct ib_cq_init_attr cq_attr = {};
Sean Hefty012a8ff2011-06-02 09:01:33 -07001191 int err;
1192
1193 if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC))
1194 return ERR_PTR(-ENOSYS);
1195
1196 xrcd = kmalloc(sizeof *xrcd, GFP_KERNEL);
1197 if (!xrcd)
1198 return ERR_PTR(-ENOMEM);
1199
1200 err = mlx4_xrcd_alloc(to_mdev(ibdev)->dev, &xrcd->xrcdn);
1201 if (err)
1202 goto err1;
1203
1204 xrcd->pd = ib_alloc_pd(ibdev);
1205 if (IS_ERR(xrcd->pd)) {
1206 err = PTR_ERR(xrcd->pd);
1207 goto err2;
1208 }
1209
Matan Barak8e372102015-06-11 16:35:21 +03001210 cq_attr.cqe = 1;
1211 xrcd->cq = ib_create_cq(ibdev, NULL, NULL, xrcd, &cq_attr);
Sean Hefty012a8ff2011-06-02 09:01:33 -07001212 if (IS_ERR(xrcd->cq)) {
1213 err = PTR_ERR(xrcd->cq);
1214 goto err3;
1215 }
1216
1217 return &xrcd->ibxrcd;
1218
1219err3:
1220 ib_dealloc_pd(xrcd->pd);
1221err2:
1222 mlx4_xrcd_free(to_mdev(ibdev)->dev, xrcd->xrcdn);
1223err1:
1224 kfree(xrcd);
1225 return ERR_PTR(err);
1226}
1227
1228static int mlx4_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
1229{
1230 ib_destroy_cq(to_mxrcd(xrcd)->cq);
1231 ib_dealloc_pd(to_mxrcd(xrcd)->pd);
1232 mlx4_xrcd_free(to_mdev(xrcd->device)->dev, to_mxrcd(xrcd)->xrcdn);
1233 kfree(xrcd);
1234
1235 return 0;
1236}
1237
Eli Cohenfa417f72010-10-24 21:08:52 -07001238static int add_gid_entry(struct ib_qp *ibqp, union ib_gid *gid)
1239{
1240 struct mlx4_ib_qp *mqp = to_mqp(ibqp);
1241 struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
1242 struct mlx4_ib_gid_entry *ge;
1243
1244 ge = kzalloc(sizeof *ge, GFP_KERNEL);
1245 if (!ge)
1246 return -ENOMEM;
1247
1248 ge->gid = *gid;
1249 if (mlx4_ib_add_mc(mdev, mqp, gid)) {
1250 ge->port = mqp->port;
1251 ge->added = 1;
1252 }
1253
1254 mutex_lock(&mqp->mutex);
1255 list_add_tail(&ge->list, &mqp->gid_list);
1256 mutex_unlock(&mqp->mutex);
1257
1258 return 0;
1259}
1260
Eran Ben Elisha3ba8e312015-10-15 14:44:40 +03001261static void mlx4_ib_delete_counters_table(struct mlx4_ib_dev *ibdev,
1262 struct mlx4_ib_counters *ctr_table)
1263{
1264 struct counter_index *counter, *tmp_count;
1265
1266 mutex_lock(&ctr_table->mutex);
1267 list_for_each_entry_safe(counter, tmp_count, &ctr_table->counters_list,
1268 list) {
1269 if (counter->allocated)
1270 mlx4_counter_free(ibdev->dev, counter->index);
1271 list_del(&counter->list);
1272 kfree(counter);
1273 }
1274 mutex_unlock(&ctr_table->mutex);
1275}
1276
Eli Cohenfa417f72010-10-24 21:08:52 -07001277int mlx4_ib_add_mc(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
1278 union ib_gid *gid)
1279{
Eli Cohenfa417f72010-10-24 21:08:52 -07001280 struct net_device *ndev;
1281 int ret = 0;
1282
1283 if (!mqp->port)
1284 return 0;
1285
Jack Morgensteindba3ad22014-08-21 14:28:41 +03001286 spin_lock_bh(&mdev->iboe.lock);
Eli Cohenfa417f72010-10-24 21:08:52 -07001287 ndev = mdev->iboe.netdevs[mqp->port - 1];
1288 if (ndev)
1289 dev_hold(ndev);
Jack Morgensteindba3ad22014-08-21 14:28:41 +03001290 spin_unlock_bh(&mdev->iboe.lock);
Eli Cohenfa417f72010-10-24 21:08:52 -07001291
1292 if (ndev) {
Eli Cohenfa417f72010-10-24 21:08:52 -07001293 ret = 1;
Eli Cohenfa417f72010-10-24 21:08:52 -07001294 dev_put(ndev);
1295 }
1296
1297 return ret;
1298}
1299
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001300struct mlx4_ib_steering {
1301 struct list_head list;
Moni Shoua146d6e12015-02-03 16:48:38 +02001302 struct mlx4_flow_reg_id reg_id;
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001303 union ib_gid gid;
1304};
1305
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001306static int parse_flow_attr(struct mlx4_dev *dev,
Matan Baraka37a1a42013-11-07 15:25:16 +02001307 u32 qp_num,
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001308 union ib_flow_spec *ib_spec,
1309 struct _rule_hw *mlx4_spec)
1310{
1311 enum mlx4_net_trans_rule_id type;
1312
1313 switch (ib_spec->type) {
1314 case IB_FLOW_SPEC_ETH:
1315 type = MLX4_NET_TRANS_RULE_ID_ETH;
1316 memcpy(mlx4_spec->eth.dst_mac, ib_spec->eth.val.dst_mac,
1317 ETH_ALEN);
1318 memcpy(mlx4_spec->eth.dst_mac_msk, ib_spec->eth.mask.dst_mac,
1319 ETH_ALEN);
1320 mlx4_spec->eth.vlan_tag = ib_spec->eth.val.vlan_tag;
1321 mlx4_spec->eth.vlan_tag_msk = ib_spec->eth.mask.vlan_tag;
1322 break;
Matan Baraka37a1a42013-11-07 15:25:16 +02001323 case IB_FLOW_SPEC_IB:
1324 type = MLX4_NET_TRANS_RULE_ID_IB;
1325 mlx4_spec->ib.l3_qpn =
1326 cpu_to_be32(qp_num);
1327 mlx4_spec->ib.qpn_mask =
1328 cpu_to_be32(MLX4_IB_FLOW_QPN_MASK);
1329 break;
1330
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001331
1332 case IB_FLOW_SPEC_IPV4:
1333 type = MLX4_NET_TRANS_RULE_ID_IPV4;
1334 mlx4_spec->ipv4.src_ip = ib_spec->ipv4.val.src_ip;
1335 mlx4_spec->ipv4.src_ip_msk = ib_spec->ipv4.mask.src_ip;
1336 mlx4_spec->ipv4.dst_ip = ib_spec->ipv4.val.dst_ip;
1337 mlx4_spec->ipv4.dst_ip_msk = ib_spec->ipv4.mask.dst_ip;
1338 break;
1339
1340 case IB_FLOW_SPEC_TCP:
1341 case IB_FLOW_SPEC_UDP:
1342 type = ib_spec->type == IB_FLOW_SPEC_TCP ?
1343 MLX4_NET_TRANS_RULE_ID_TCP :
1344 MLX4_NET_TRANS_RULE_ID_UDP;
1345 mlx4_spec->tcp_udp.dst_port = ib_spec->tcp_udp.val.dst_port;
1346 mlx4_spec->tcp_udp.dst_port_msk = ib_spec->tcp_udp.mask.dst_port;
1347 mlx4_spec->tcp_udp.src_port = ib_spec->tcp_udp.val.src_port;
1348 mlx4_spec->tcp_udp.src_port_msk = ib_spec->tcp_udp.mask.src_port;
1349 break;
1350
1351 default:
1352 return -EINVAL;
1353 }
1354 if (mlx4_map_sw_to_hw_steering_id(dev, type) < 0 ||
1355 mlx4_hw_rule_sz(dev, type) < 0)
1356 return -EINVAL;
1357 mlx4_spec->id = cpu_to_be16(mlx4_map_sw_to_hw_steering_id(dev, type));
1358 mlx4_spec->size = mlx4_hw_rule_sz(dev, type) >> 2;
1359 return mlx4_hw_rule_sz(dev, type);
1360}
1361
Matan Baraka37a1a42013-11-07 15:25:16 +02001362struct default_rules {
1363 __u32 mandatory_fields[IB_FLOW_SPEC_SUPPORT_LAYERS];
1364 __u32 mandatory_not_fields[IB_FLOW_SPEC_SUPPORT_LAYERS];
1365 __u32 rules_create_list[IB_FLOW_SPEC_SUPPORT_LAYERS];
1366 __u8 link_layer;
1367};
1368static const struct default_rules default_table[] = {
1369 {
1370 .mandatory_fields = {IB_FLOW_SPEC_IPV4},
1371 .mandatory_not_fields = {IB_FLOW_SPEC_ETH},
1372 .rules_create_list = {IB_FLOW_SPEC_IB},
1373 .link_layer = IB_LINK_LAYER_INFINIBAND
1374 }
1375};
1376
1377static int __mlx4_ib_default_rules_match(struct ib_qp *qp,
1378 struct ib_flow_attr *flow_attr)
1379{
1380 int i, j, k;
1381 void *ib_flow;
1382 const struct default_rules *pdefault_rules = default_table;
1383 u8 link_layer = rdma_port_get_link_layer(qp->device, flow_attr->port);
1384
Fabian Fredericka57f23f2014-08-12 19:20:07 -04001385 for (i = 0; i < ARRAY_SIZE(default_table); i++, pdefault_rules++) {
Matan Baraka37a1a42013-11-07 15:25:16 +02001386 __u32 field_types[IB_FLOW_SPEC_SUPPORT_LAYERS];
1387 memset(&field_types, 0, sizeof(field_types));
1388
1389 if (link_layer != pdefault_rules->link_layer)
1390 continue;
1391
1392 ib_flow = flow_attr + 1;
1393 /* we assume the specs are sorted */
1394 for (j = 0, k = 0; k < IB_FLOW_SPEC_SUPPORT_LAYERS &&
1395 j < flow_attr->num_of_specs; k++) {
1396 union ib_flow_spec *current_flow =
1397 (union ib_flow_spec *)ib_flow;
1398
1399 /* same layer but different type */
1400 if (((current_flow->type & IB_FLOW_SPEC_LAYER_MASK) ==
1401 (pdefault_rules->mandatory_fields[k] &
1402 IB_FLOW_SPEC_LAYER_MASK)) &&
1403 (current_flow->type !=
1404 pdefault_rules->mandatory_fields[k]))
1405 goto out;
1406
1407 /* same layer, try match next one */
1408 if (current_flow->type ==
1409 pdefault_rules->mandatory_fields[k]) {
1410 j++;
1411 ib_flow +=
1412 ((union ib_flow_spec *)ib_flow)->size;
1413 }
1414 }
1415
1416 ib_flow = flow_attr + 1;
1417 for (j = 0; j < flow_attr->num_of_specs;
1418 j++, ib_flow += ((union ib_flow_spec *)ib_flow)->size)
1419 for (k = 0; k < IB_FLOW_SPEC_SUPPORT_LAYERS; k++)
1420 /* same layer and same type */
1421 if (((union ib_flow_spec *)ib_flow)->type ==
1422 pdefault_rules->mandatory_not_fields[k])
1423 goto out;
1424
1425 return i;
1426 }
1427out:
1428 return -1;
1429}
1430
1431static int __mlx4_ib_create_default_rules(
1432 struct mlx4_ib_dev *mdev,
1433 struct ib_qp *qp,
1434 const struct default_rules *pdefault_rules,
1435 struct _rule_hw *mlx4_spec) {
1436 int size = 0;
1437 int i;
1438
Fabian Fredericka57f23f2014-08-12 19:20:07 -04001439 for (i = 0; i < ARRAY_SIZE(pdefault_rules->rules_create_list); i++) {
Matan Baraka37a1a42013-11-07 15:25:16 +02001440 int ret;
1441 union ib_flow_spec ib_spec;
1442 switch (pdefault_rules->rules_create_list[i]) {
1443 case 0:
1444 /* no rule */
1445 continue;
1446 case IB_FLOW_SPEC_IB:
1447 ib_spec.type = IB_FLOW_SPEC_IB;
1448 ib_spec.size = sizeof(struct ib_flow_spec_ib);
1449
1450 break;
1451 default:
1452 /* invalid rule */
1453 return -EINVAL;
1454 }
1455 /* We must put empty rule, qpn is being ignored */
1456 ret = parse_flow_attr(mdev->dev, 0, &ib_spec,
1457 mlx4_spec);
1458 if (ret < 0) {
1459 pr_info("invalid parsing\n");
1460 return -EINVAL;
1461 }
1462
1463 mlx4_spec = (void *)mlx4_spec + ret;
1464 size += ret;
1465 }
1466 return size;
1467}
1468
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001469static int __mlx4_ib_create_flow(struct ib_qp *qp, struct ib_flow_attr *flow_attr,
1470 int domain,
1471 enum mlx4_net_trans_promisc_mode flow_type,
1472 u64 *reg_id)
1473{
1474 int ret, i;
1475 int size = 0;
1476 void *ib_flow;
1477 struct mlx4_ib_dev *mdev = to_mdev(qp->device);
1478 struct mlx4_cmd_mailbox *mailbox;
1479 struct mlx4_net_trans_rule_hw_ctrl *ctrl;
Matan Baraka37a1a42013-11-07 15:25:16 +02001480 int default_flow;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001481
1482 static const u16 __mlx4_domain[] = {
1483 [IB_FLOW_DOMAIN_USER] = MLX4_DOMAIN_UVERBS,
1484 [IB_FLOW_DOMAIN_ETHTOOL] = MLX4_DOMAIN_ETHTOOL,
1485 [IB_FLOW_DOMAIN_RFS] = MLX4_DOMAIN_RFS,
1486 [IB_FLOW_DOMAIN_NIC] = MLX4_DOMAIN_NIC,
1487 };
1488
1489 if (flow_attr->priority > MLX4_IB_FLOW_MAX_PRIO) {
1490 pr_err("Invalid priority value %d\n", flow_attr->priority);
1491 return -EINVAL;
1492 }
1493
1494 if (domain >= IB_FLOW_DOMAIN_NUM) {
1495 pr_err("Invalid domain value %d\n", domain);
1496 return -EINVAL;
1497 }
1498
1499 if (mlx4_map_sw_to_hw_steering_mode(mdev->dev, flow_type) < 0)
1500 return -EINVAL;
1501
1502 mailbox = mlx4_alloc_cmd_mailbox(mdev->dev);
1503 if (IS_ERR(mailbox))
1504 return PTR_ERR(mailbox);
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001505 ctrl = mailbox->buf;
1506
1507 ctrl->prio = cpu_to_be16(__mlx4_domain[domain] |
1508 flow_attr->priority);
1509 ctrl->type = mlx4_map_sw_to_hw_steering_mode(mdev->dev, flow_type);
1510 ctrl->port = flow_attr->port;
1511 ctrl->qpn = cpu_to_be32(qp->qp_num);
1512
1513 ib_flow = flow_attr + 1;
1514 size += sizeof(struct mlx4_net_trans_rule_hw_ctrl);
Matan Baraka37a1a42013-11-07 15:25:16 +02001515 /* Add default flows */
1516 default_flow = __mlx4_ib_default_rules_match(qp, flow_attr);
1517 if (default_flow >= 0) {
1518 ret = __mlx4_ib_create_default_rules(
1519 mdev, qp, default_table + default_flow,
1520 mailbox->buf + size);
1521 if (ret < 0) {
1522 mlx4_free_cmd_mailbox(mdev->dev, mailbox);
1523 return -EINVAL;
1524 }
1525 size += ret;
1526 }
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001527 for (i = 0; i < flow_attr->num_of_specs; i++) {
Matan Baraka37a1a42013-11-07 15:25:16 +02001528 ret = parse_flow_attr(mdev->dev, qp->qp_num, ib_flow,
1529 mailbox->buf + size);
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001530 if (ret < 0) {
1531 mlx4_free_cmd_mailbox(mdev->dev, mailbox);
1532 return -EINVAL;
1533 }
1534 ib_flow += ((union ib_flow_spec *) ib_flow)->size;
1535 size += ret;
1536 }
1537
1538 ret = mlx4_cmd_imm(mdev->dev, mailbox->dma, reg_id, size >> 2, 0,
1539 MLX4_QP_FLOW_STEERING_ATTACH, MLX4_CMD_TIME_CLASS_A,
Matan Barak48564132015-05-31 09:30:15 +03001540 MLX4_CMD_WRAPPED);
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001541 if (ret == -ENOMEM)
1542 pr_err("mcg table is full. Fail to register network rule.\n");
1543 else if (ret == -ENXIO)
1544 pr_err("Device managed flow steering is disabled. Fail to register network rule.\n");
1545 else if (ret)
1546 pr_err("Invalid argumant. Fail to register network rule.\n");
1547
1548 mlx4_free_cmd_mailbox(mdev->dev, mailbox);
1549 return ret;
1550}
1551
1552static int __mlx4_ib_destroy_flow(struct mlx4_dev *dev, u64 reg_id)
1553{
1554 int err;
1555 err = mlx4_cmd(dev, reg_id, 0, 0,
1556 MLX4_QP_FLOW_STEERING_DETACH, MLX4_CMD_TIME_CLASS_A,
Matan Barak48564132015-05-31 09:30:15 +03001557 MLX4_CMD_WRAPPED);
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001558 if (err)
1559 pr_err("Fail to detach network rule. registration id = 0x%llx\n",
1560 reg_id);
1561 return err;
1562}
1563
Or Gerlitzd2fce8a2014-08-27 16:47:49 +03001564static int mlx4_ib_tunnel_steer_add(struct ib_qp *qp, struct ib_flow_attr *flow_attr,
1565 u64 *reg_id)
1566{
1567 void *ib_flow;
1568 union ib_flow_spec *ib_spec;
1569 struct mlx4_dev *dev = to_mdev(qp->device)->dev;
1570 int err = 0;
1571
Or Gerlitz5eff6da2015-01-15 15:28:54 +02001572 if (dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN ||
1573 dev->caps.dmfs_high_steer_mode == MLX4_STEERING_DMFS_A0_STATIC)
Or Gerlitzd2fce8a2014-08-27 16:47:49 +03001574 return 0; /* do nothing */
1575
1576 ib_flow = flow_attr + 1;
1577 ib_spec = (union ib_flow_spec *)ib_flow;
1578
1579 if (ib_spec->type != IB_FLOW_SPEC_ETH || flow_attr->num_of_specs != 1)
1580 return 0; /* do nothing */
1581
1582 err = mlx4_tunnel_steer_add(to_mdev(qp->device)->dev, ib_spec->eth.val.dst_mac,
1583 flow_attr->port, qp->qp_num,
1584 MLX4_DOMAIN_UVERBS | (flow_attr->priority & 0xff),
1585 reg_id);
1586 return err;
1587}
1588
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001589static struct ib_flow *mlx4_ib_create_flow(struct ib_qp *qp,
1590 struct ib_flow_attr *flow_attr,
1591 int domain)
1592{
Moni Shoua146d6e12015-02-03 16:48:38 +02001593 int err = 0, i = 0, j = 0;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001594 struct mlx4_ib_flow *mflow;
1595 enum mlx4_net_trans_promisc_mode type[2];
Moni Shoua146d6e12015-02-03 16:48:38 +02001596 struct mlx4_dev *dev = (to_mdev(qp->device))->dev;
1597 int is_bonded = mlx4_is_bonded(dev);
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001598
1599 memset(type, 0, sizeof(type));
1600
1601 mflow = kzalloc(sizeof(*mflow), GFP_KERNEL);
1602 if (!mflow) {
1603 err = -ENOMEM;
1604 goto err_free;
1605 }
1606
1607 switch (flow_attr->type) {
1608 case IB_FLOW_ATTR_NORMAL:
1609 type[0] = MLX4_FS_REGULAR;
1610 break;
1611
1612 case IB_FLOW_ATTR_ALL_DEFAULT:
1613 type[0] = MLX4_FS_ALL_DEFAULT;
1614 break;
1615
1616 case IB_FLOW_ATTR_MC_DEFAULT:
1617 type[0] = MLX4_FS_MC_DEFAULT;
1618 break;
1619
1620 case IB_FLOW_ATTR_SNIFFER:
1621 type[0] = MLX4_FS_UC_SNIFFER;
1622 type[1] = MLX4_FS_MC_SNIFFER;
1623 break;
1624
1625 default:
1626 err = -EINVAL;
1627 goto err_free;
1628 }
1629
1630 while (i < ARRAY_SIZE(type) && type[i]) {
1631 err = __mlx4_ib_create_flow(qp, flow_attr, domain, type[i],
Moni Shoua146d6e12015-02-03 16:48:38 +02001632 &mflow->reg_id[i].id);
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001633 if (err)
Or Gerlitz571e1b22014-10-30 15:59:28 +02001634 goto err_create_flow;
Moni Shoua146d6e12015-02-03 16:48:38 +02001635 if (is_bonded) {
Moni Shoua824c25c2015-02-08 11:49:33 +02001636 /* Application always sees one port so the mirror rule
1637 * must be on port #2
1638 */
Moni Shoua146d6e12015-02-03 16:48:38 +02001639 flow_attr->port = 2;
1640 err = __mlx4_ib_create_flow(qp, flow_attr,
1641 domain, type[j],
1642 &mflow->reg_id[j].mirror);
1643 flow_attr->port = 1;
1644 if (err)
1645 goto err_create_flow;
1646 j++;
1647 }
1648
Roland Dreier11562562015-05-29 23:11:27 -07001649 i++;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001650 }
1651
Or Gerlitzd2fce8a2014-08-27 16:47:49 +03001652 if (i < ARRAY_SIZE(type) && flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Moni Shoua146d6e12015-02-03 16:48:38 +02001653 err = mlx4_ib_tunnel_steer_add(qp, flow_attr,
1654 &mflow->reg_id[i].id);
Or Gerlitzd2fce8a2014-08-27 16:47:49 +03001655 if (err)
Or Gerlitz571e1b22014-10-30 15:59:28 +02001656 goto err_create_flow;
Roland Dreier11562562015-05-29 23:11:27 -07001657
Moni Shoua146d6e12015-02-03 16:48:38 +02001658 if (is_bonded) {
1659 flow_attr->port = 2;
1660 err = mlx4_ib_tunnel_steer_add(qp, flow_attr,
1661 &mflow->reg_id[j].mirror);
1662 flow_attr->port = 1;
1663 if (err)
1664 goto err_create_flow;
1665 j++;
1666 }
1667 /* function to create mirror rule */
Roland Dreier11562562015-05-29 23:11:27 -07001668 i++;
Or Gerlitzd2fce8a2014-08-27 16:47:49 +03001669 }
1670
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001671 return &mflow->ibflow;
1672
Or Gerlitz571e1b22014-10-30 15:59:28 +02001673err_create_flow:
1674 while (i) {
Moni Shoua146d6e12015-02-03 16:48:38 +02001675 (void)__mlx4_ib_destroy_flow(to_mdev(qp->device)->dev,
1676 mflow->reg_id[i].id);
Or Gerlitz571e1b22014-10-30 15:59:28 +02001677 i--;
1678 }
Moni Shoua146d6e12015-02-03 16:48:38 +02001679
1680 while (j) {
1681 (void)__mlx4_ib_destroy_flow(to_mdev(qp->device)->dev,
1682 mflow->reg_id[j].mirror);
1683 j--;
1684 }
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001685err_free:
1686 kfree(mflow);
1687 return ERR_PTR(err);
1688}
1689
1690static int mlx4_ib_destroy_flow(struct ib_flow *flow_id)
1691{
1692 int err, ret = 0;
1693 int i = 0;
1694 struct mlx4_ib_dev *mdev = to_mdev(flow_id->qp->device);
1695 struct mlx4_ib_flow *mflow = to_mflow(flow_id);
1696
Moni Shoua146d6e12015-02-03 16:48:38 +02001697 while (i < ARRAY_SIZE(mflow->reg_id) && mflow->reg_id[i].id) {
1698 err = __mlx4_ib_destroy_flow(mdev->dev, mflow->reg_id[i].id);
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001699 if (err)
1700 ret = err;
Moni Shoua146d6e12015-02-03 16:48:38 +02001701 if (mflow->reg_id[i].mirror) {
1702 err = __mlx4_ib_destroy_flow(mdev->dev,
1703 mflow->reg_id[i].mirror);
1704 if (err)
1705 ret = err;
1706 }
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001707 i++;
1708 }
1709
1710 kfree(mflow);
1711 return ret;
1712}
1713
Roland Dreier225c7b12007-05-08 18:00:38 -07001714static int mlx4_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
1715{
Eli Cohenfa417f72010-10-24 21:08:52 -07001716 int err;
1717 struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
Moni Shoua146d6e12015-02-03 16:48:38 +02001718 struct mlx4_dev *dev = mdev->dev;
Eli Cohenfa417f72010-10-24 21:08:52 -07001719 struct mlx4_ib_qp *mqp = to_mqp(ibqp);
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001720 struct mlx4_ib_steering *ib_steering = NULL;
Or Gerlitze9a7faf2014-12-17 16:17:34 +02001721 enum mlx4_protocol prot = MLX4_PROT_IB_IPV6;
Moni Shoua146d6e12015-02-03 16:48:38 +02001722 struct mlx4_flow_reg_id reg_id;
Eli Cohenfa417f72010-10-24 21:08:52 -07001723
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001724 if (mdev->dev->caps.steering_mode ==
1725 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1726 ib_steering = kmalloc(sizeof(*ib_steering), GFP_KERNEL);
1727 if (!ib_steering)
1728 return -ENOMEM;
1729 }
1730
1731 err = mlx4_multicast_attach(mdev->dev, &mqp->mqp, gid->raw, mqp->port,
1732 !!(mqp->flags &
1733 MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK),
Moni Shoua146d6e12015-02-03 16:48:38 +02001734 prot, &reg_id.id);
Or Gerlitze9a7faf2014-12-17 16:17:34 +02001735 if (err) {
1736 pr_err("multicast attach op failed, err %d\n", err);
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001737 goto err_malloc;
Or Gerlitze9a7faf2014-12-17 16:17:34 +02001738 }
Eli Cohenfa417f72010-10-24 21:08:52 -07001739
Moni Shoua146d6e12015-02-03 16:48:38 +02001740 reg_id.mirror = 0;
1741 if (mlx4_is_bonded(dev)) {
Moni Shoua824c25c2015-02-08 11:49:33 +02001742 err = mlx4_multicast_attach(mdev->dev, &mqp->mqp, gid->raw,
1743 (mqp->port == 1) ? 2 : 1,
Moni Shoua146d6e12015-02-03 16:48:38 +02001744 !!(mqp->flags &
1745 MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK),
1746 prot, &reg_id.mirror);
1747 if (err)
1748 goto err_add;
1749 }
1750
Eli Cohenfa417f72010-10-24 21:08:52 -07001751 err = add_gid_entry(ibqp, gid);
1752 if (err)
1753 goto err_add;
1754
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001755 if (ib_steering) {
1756 memcpy(ib_steering->gid.raw, gid->raw, 16);
1757 ib_steering->reg_id = reg_id;
1758 mutex_lock(&mqp->mutex);
1759 list_add(&ib_steering->list, &mqp->steering_rules);
1760 mutex_unlock(&mqp->mutex);
1761 }
Eli Cohenfa417f72010-10-24 21:08:52 -07001762 return 0;
1763
1764err_add:
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001765 mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
Moni Shoua146d6e12015-02-03 16:48:38 +02001766 prot, reg_id.id);
1767 if (reg_id.mirror)
1768 mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
1769 prot, reg_id.mirror);
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001770err_malloc:
1771 kfree(ib_steering);
1772
Eli Cohenfa417f72010-10-24 21:08:52 -07001773 return err;
1774}
1775
1776static struct mlx4_ib_gid_entry *find_gid_entry(struct mlx4_ib_qp *qp, u8 *raw)
1777{
1778 struct mlx4_ib_gid_entry *ge;
1779 struct mlx4_ib_gid_entry *tmp;
1780 struct mlx4_ib_gid_entry *ret = NULL;
1781
1782 list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
1783 if (!memcmp(raw, ge->gid.raw, 16)) {
1784 ret = ge;
1785 break;
1786 }
1787 }
1788
1789 return ret;
Roland Dreier225c7b12007-05-08 18:00:38 -07001790}
1791
1792static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
1793{
Eli Cohenfa417f72010-10-24 21:08:52 -07001794 int err;
1795 struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
Moni Shoua146d6e12015-02-03 16:48:38 +02001796 struct mlx4_dev *dev = mdev->dev;
Eli Cohenfa417f72010-10-24 21:08:52 -07001797 struct mlx4_ib_qp *mqp = to_mqp(ibqp);
Eli Cohenfa417f72010-10-24 21:08:52 -07001798 struct net_device *ndev;
1799 struct mlx4_ib_gid_entry *ge;
Moni Shoua146d6e12015-02-03 16:48:38 +02001800 struct mlx4_flow_reg_id reg_id = {0, 0};
Or Gerlitze9a7faf2014-12-17 16:17:34 +02001801 enum mlx4_protocol prot = MLX4_PROT_IB_IPV6;
Eli Cohenfa417f72010-10-24 21:08:52 -07001802
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001803 if (mdev->dev->caps.steering_mode ==
1804 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1805 struct mlx4_ib_steering *ib_steering;
1806
1807 mutex_lock(&mqp->mutex);
1808 list_for_each_entry(ib_steering, &mqp->steering_rules, list) {
1809 if (!memcmp(ib_steering->gid.raw, gid->raw, 16)) {
1810 list_del(&ib_steering->list);
1811 break;
1812 }
1813 }
1814 mutex_unlock(&mqp->mutex);
1815 if (&ib_steering->list == &mqp->steering_rules) {
1816 pr_err("Couldn't find reg_id for mgid. Steering rule is left attached\n");
1817 return -EINVAL;
1818 }
1819 reg_id = ib_steering->reg_id;
1820 kfree(ib_steering);
1821 }
1822
1823 err = mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
Moni Shoua146d6e12015-02-03 16:48:38 +02001824 prot, reg_id.id);
Eli Cohenfa417f72010-10-24 21:08:52 -07001825 if (err)
1826 return err;
1827
Moni Shoua146d6e12015-02-03 16:48:38 +02001828 if (mlx4_is_bonded(dev)) {
1829 err = mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
1830 prot, reg_id.mirror);
1831 if (err)
1832 return err;
1833 }
1834
Eli Cohenfa417f72010-10-24 21:08:52 -07001835 mutex_lock(&mqp->mutex);
1836 ge = find_gid_entry(mqp, gid->raw);
1837 if (ge) {
Jack Morgensteindba3ad22014-08-21 14:28:41 +03001838 spin_lock_bh(&mdev->iboe.lock);
Eli Cohenfa417f72010-10-24 21:08:52 -07001839 ndev = ge->added ? mdev->iboe.netdevs[ge->port - 1] : NULL;
1840 if (ndev)
1841 dev_hold(ndev);
Jack Morgensteindba3ad22014-08-21 14:28:41 +03001842 spin_unlock_bh(&mdev->iboe.lock);
Moni Shouad487ee72013-12-12 18:03:13 +02001843 if (ndev)
Eli Cohenfa417f72010-10-24 21:08:52 -07001844 dev_put(ndev);
Eli Cohenfa417f72010-10-24 21:08:52 -07001845 list_del(&ge->list);
1846 kfree(ge);
1847 } else
Shlomo Pongratz987c8f82012-04-29 17:04:26 +03001848 pr_warn("could not find mgid entry\n");
Eli Cohenfa417f72010-10-24 21:08:52 -07001849
1850 mutex_unlock(&mqp->mutex);
1851
1852 return 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07001853}
1854
1855static int init_node_data(struct mlx4_ib_dev *dev)
1856{
1857 struct ib_smp *in_mad = NULL;
1858 struct ib_smp *out_mad = NULL;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00001859 int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
Roland Dreier225c7b12007-05-08 18:00:38 -07001860 int err = -ENOMEM;
1861
1862 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
1863 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
1864 if (!in_mad || !out_mad)
1865 goto out;
1866
1867 init_query_mad(in_mad);
1868 in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00001869 if (mlx4_is_master(dev->dev))
1870 mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
Roland Dreier225c7b12007-05-08 18:00:38 -07001871
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00001872 err = mlx4_MAD_IFC(dev, mad_ifc_flags, 1, NULL, NULL, in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -07001873 if (err)
1874 goto out;
1875
1876 memcpy(dev->ib_dev.node_desc, out_mad->data, 64);
1877
1878 in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
1879
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00001880 err = mlx4_MAD_IFC(dev, mad_ifc_flags, 1, NULL, NULL, in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -07001881 if (err)
1882 goto out;
1883
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +00001884 dev->dev->rev_id = be32_to_cpup((__be32 *) (out_mad->data + 32));
Roland Dreier225c7b12007-05-08 18:00:38 -07001885 memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
1886
1887out:
1888 kfree(in_mad);
1889 kfree(out_mad);
1890 return err;
1891}
1892
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001893static ssize_t show_hca(struct device *device, struct device_attribute *attr,
1894 char *buf)
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001895{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001896 struct mlx4_ib_dev *dev =
1897 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
Yishai Hadas872bf2f2015-01-25 16:59:35 +02001898 return sprintf(buf, "MT%d\n", dev->dev->persist->pdev->device);
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001899}
1900
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001901static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
1902 char *buf)
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001903{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001904 struct mlx4_ib_dev *dev =
1905 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001906 return sprintf(buf, "%d.%d.%d\n", (int) (dev->dev->caps.fw_ver >> 32),
1907 (int) (dev->dev->caps.fw_ver >> 16) & 0xffff,
1908 (int) dev->dev->caps.fw_ver & 0xffff);
1909}
1910
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001911static ssize_t show_rev(struct device *device, struct device_attribute *attr,
1912 char *buf)
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001913{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001914 struct mlx4_ib_dev *dev =
1915 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001916 return sprintf(buf, "%x\n", dev->dev->rev_id);
1917}
1918
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001919static ssize_t show_board(struct device *device, struct device_attribute *attr,
1920 char *buf)
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001921{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001922 struct mlx4_ib_dev *dev =
1923 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
1924 return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN,
1925 dev->dev->board_id);
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001926}
1927
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001928static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
1929static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
1930static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
1931static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001932
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001933static struct device_attribute *mlx4_class_attributes[] = {
1934 &dev_attr_hw_rev,
1935 &dev_attr_fw_ver,
1936 &dev_attr_hca_type,
1937 &dev_attr_board_id
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001938};
1939
Matan Barak9433c182014-05-15 15:29:28 +03001940#define MLX4_IB_INVALID_MAC ((u64)-1)
1941static void mlx4_ib_update_qps(struct mlx4_ib_dev *ibdev,
1942 struct net_device *dev,
1943 int port)
1944{
1945 u64 new_smac = 0;
1946 u64 release_mac = MLX4_IB_INVALID_MAC;
1947 struct mlx4_ib_qp *qp;
1948
1949 read_lock(&dev_base_lock);
1950 new_smac = mlx4_mac_to_u64(dev->dev_addr);
1951 read_unlock(&dev_base_lock);
1952
Jack Morgenstein3e0629c2014-09-11 14:11:17 +03001953 atomic64_set(&ibdev->iboe.mac[port - 1], new_smac);
1954
Jack Morgensteind24d9f42014-09-11 14:11:18 +03001955 /* no need for update QP1 and mac registration in non-SRIOV */
1956 if (!mlx4_is_mfunc(ibdev->dev))
1957 return;
1958
Matan Barak9433c182014-05-15 15:29:28 +03001959 mutex_lock(&ibdev->qp1_proxy_lock[port - 1]);
1960 qp = ibdev->qp1_proxy[port - 1];
1961 if (qp) {
1962 int new_smac_index;
Jack Morgenstein25476b02014-09-11 14:11:20 +03001963 u64 old_smac;
Matan Barak9433c182014-05-15 15:29:28 +03001964 struct mlx4_update_qp_params update_params;
1965
Jack Morgenstein25476b02014-09-11 14:11:20 +03001966 mutex_lock(&qp->mutex);
1967 old_smac = qp->pri.smac;
Matan Barak9433c182014-05-15 15:29:28 +03001968 if (new_smac == old_smac)
1969 goto unlock;
1970
1971 new_smac_index = mlx4_register_mac(ibdev->dev, port, new_smac);
1972
1973 if (new_smac_index < 0)
1974 goto unlock;
1975
1976 update_params.smac_index = new_smac_index;
Matan Barak09e05c32014-09-10 16:41:56 +03001977 if (mlx4_update_qp(ibdev->dev, qp->mqp.qpn, MLX4_UPDATE_QP_SMAC,
Matan Barak9433c182014-05-15 15:29:28 +03001978 &update_params)) {
1979 release_mac = new_smac;
1980 goto unlock;
1981 }
Jack Morgenstein25476b02014-09-11 14:11:20 +03001982 /* if old port was zero, no mac was yet registered for this QP */
1983 if (qp->pri.smac_port)
1984 release_mac = old_smac;
Matan Barak9433c182014-05-15 15:29:28 +03001985 qp->pri.smac = new_smac;
Jack Morgenstein25476b02014-09-11 14:11:20 +03001986 qp->pri.smac_port = port;
Matan Barak9433c182014-05-15 15:29:28 +03001987 qp->pri.smac_index = new_smac_index;
Matan Barak9433c182014-05-15 15:29:28 +03001988 }
1989
1990unlock:
Matan Barak9433c182014-05-15 15:29:28 +03001991 if (release_mac != MLX4_IB_INVALID_MAC)
1992 mlx4_unregister_mac(ibdev->dev, port, release_mac);
Jack Morgenstein25476b02014-09-11 14:11:20 +03001993 if (qp)
1994 mutex_unlock(&qp->mutex);
1995 mutex_unlock(&ibdev->qp1_proxy_lock[port - 1]);
Matan Barak9433c182014-05-15 15:29:28 +03001996}
1997
Matan Barak9433c182014-05-15 15:29:28 +03001998static void mlx4_ib_scan_netdevs(struct mlx4_ib_dev *ibdev,
1999 struct net_device *dev,
2000 unsigned long event)
2001
Moni Shouad487ee72013-12-12 18:03:13 +02002002{
2003 struct mlx4_ib_iboe *iboe;
Matan Barak9433c182014-05-15 15:29:28 +03002004 int update_qps_port = -1;
Moni Shouad487ee72013-12-12 18:03:13 +02002005 int port;
2006
Moni Shoua5070cd22015-07-30 18:33:30 +03002007 ASSERT_RTNL();
2008
Moni Shouad487ee72013-12-12 18:03:13 +02002009 iboe = &ibdev->iboe;
2010
Jack Morgensteindba3ad22014-08-21 14:28:41 +03002011 spin_lock_bh(&iboe->lock);
Moni Shouad487ee72013-12-12 18:03:13 +02002012 mlx4_foreach_ib_transport_port(port, ibdev->dev) {
Moni Shouaad4885d22014-02-05 15:13:02 +02002013
Moni Shouad487ee72013-12-12 18:03:13 +02002014 iboe->netdevs[port - 1] =
2015 mlx4_get_protocol_dev(ibdev->dev, MLX4_PROT_ETH, port);
Moni Shouad487ee72013-12-12 18:03:13 +02002016
Matan Barak9433c182014-05-15 15:29:28 +03002017 if (dev == iboe->netdevs[port - 1] &&
2018 (event == NETDEV_CHANGEADDR || event == NETDEV_REGISTER ||
2019 event == NETDEV_UP || event == NETDEV_CHANGE))
2020 update_qps_port = port;
2021
Moni Shouad487ee72013-12-12 18:03:13 +02002022 }
Jack Morgensteindba3ad22014-08-21 14:28:41 +03002023 spin_unlock_bh(&iboe->lock);
Matan Barak9433c182014-05-15 15:29:28 +03002024
2025 if (update_qps_port > 0)
2026 mlx4_ib_update_qps(ibdev, dev, update_qps_port);
Moni Shouad487ee72013-12-12 18:03:13 +02002027}
2028
2029static int mlx4_ib_netdev_event(struct notifier_block *this,
2030 unsigned long event, void *ptr)
2031{
Jiri Pirko351638e2013-05-28 01:30:21 +00002032 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eli Cohenfa417f72010-10-24 21:08:52 -07002033 struct mlx4_ib_dev *ibdev;
Eli Cohenfa417f72010-10-24 21:08:52 -07002034
2035 if (!net_eq(dev_net(dev), &init_net))
2036 return NOTIFY_DONE;
2037
2038 ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb);
Matan Barak9433c182014-05-15 15:29:28 +03002039 mlx4_ib_scan_netdevs(ibdev, dev, event);
Eli Cohenfa417f72010-10-24 21:08:52 -07002040
2041 return NOTIFY_DONE;
2042}
2043
Jack Morgenstein54679e12012-08-03 08:40:43 +00002044static void init_pkeys(struct mlx4_ib_dev *ibdev)
2045{
2046 int port;
2047 int slave;
2048 int i;
2049
2050 if (mlx4_is_master(ibdev->dev)) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002051 for (slave = 0; slave <= ibdev->dev->persist->num_vfs;
2052 ++slave) {
Jack Morgenstein54679e12012-08-03 08:40:43 +00002053 for (port = 1; port <= ibdev->dev->caps.num_ports; ++port) {
2054 for (i = 0;
2055 i < ibdev->dev->phys_caps.pkey_phys_table_len[port];
2056 ++i) {
2057 ibdev->pkeys.virt2phys_pkey[slave][port - 1][i] =
2058 /* master has the identity virt2phys pkey mapping */
2059 (slave == mlx4_master_func_num(ibdev->dev) || !i) ? i :
2060 ibdev->dev->phys_caps.pkey_phys_table_len[port] - 1;
2061 mlx4_sync_pkey_table(ibdev->dev, slave, port, i,
2062 ibdev->pkeys.virt2phys_pkey[slave][port - 1][i]);
2063 }
2064 }
2065 }
2066 /* initialize pkey cache */
2067 for (port = 1; port <= ibdev->dev->caps.num_ports; ++port) {
2068 for (i = 0;
2069 i < ibdev->dev->phys_caps.pkey_phys_table_len[port];
2070 ++i)
2071 ibdev->pkeys.phys_pkey_cache[port-1][i] =
2072 (i) ? 0 : 0xFFFF;
2073 }
2074 }
2075}
2076
Shlomo Pongratze605b742012-04-29 17:04:27 +03002077static void mlx4_ib_alloc_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev)
2078{
Matan Barakc66fa192015-05-31 09:30:16 +03002079 int i, j, eq = 0, total_eqs = 0;
Shlomo Pongratze605b742012-04-29 17:04:27 +03002080
Matan Barakc66fa192015-05-31 09:30:16 +03002081 ibdev->eq_table = kcalloc(dev->caps.num_comp_vectors,
2082 sizeof(ibdev->eq_table[0]), GFP_KERNEL);
Shlomo Pongratze605b742012-04-29 17:04:27 +03002083 if (!ibdev->eq_table)
2084 return;
2085
Matan Barakc66fa192015-05-31 09:30:16 +03002086 for (i = 1; i <= dev->caps.num_ports; i++) {
2087 for (j = 0; j < mlx4_get_eqs_per_port(dev, i);
2088 j++, total_eqs++) {
2089 if (i > 1 && mlx4_is_eq_shared(dev, total_eqs))
2090 continue;
2091 ibdev->eq_table[eq] = total_eqs;
2092 if (!mlx4_assign_eq(dev, i,
2093 &ibdev->eq_table[eq]))
2094 eq++;
2095 else
2096 ibdev->eq_table[eq] = -1;
Shlomo Pongratze605b742012-04-29 17:04:27 +03002097 }
2098 }
2099
Matan Barakc66fa192015-05-31 09:30:16 +03002100 for (i = eq; i < dev->caps.num_comp_vectors;
2101 ibdev->eq_table[i++] = -1)
2102 ;
Shlomo Pongratze605b742012-04-29 17:04:27 +03002103
2104 /* Advertise the new number of EQs to clients */
Matan Barakc66fa192015-05-31 09:30:16 +03002105 ibdev->ib_dev.num_comp_vectors = eq;
Shlomo Pongratze605b742012-04-29 17:04:27 +03002106}
2107
2108static void mlx4_ib_free_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev)
2109{
2110 int i;
Matan Barakc66fa192015-05-31 09:30:16 +03002111 int total_eqs = ibdev->ib_dev.num_comp_vectors;
Shlomo Pongratz3aac6ff2012-05-24 16:08:07 +03002112
Matan Barakc66fa192015-05-31 09:30:16 +03002113 /* no eqs were allocated */
Shlomo Pongratz3aac6ff2012-05-24 16:08:07 +03002114 if (!ibdev->eq_table)
2115 return;
Shlomo Pongratze605b742012-04-29 17:04:27 +03002116
2117 /* Reset the advertised EQ number */
Matan Barakc66fa192015-05-31 09:30:16 +03002118 ibdev->ib_dev.num_comp_vectors = 0;
Shlomo Pongratze605b742012-04-29 17:04:27 +03002119
Matan Barakc66fa192015-05-31 09:30:16 +03002120 for (i = 0; i < total_eqs; i++)
Shlomo Pongratze605b742012-04-29 17:04:27 +03002121 mlx4_release_eq(dev, ibdev->eq_table[i]);
Shlomo Pongratze605b742012-04-29 17:04:27 +03002122
Shlomo Pongratze605b742012-04-29 17:04:27 +03002123 kfree(ibdev->eq_table);
Matan Barakc66fa192015-05-31 09:30:16 +03002124 ibdev->eq_table = NULL;
Shlomo Pongratze605b742012-04-29 17:04:27 +03002125}
2126
Ira Weiny77386132015-05-13 20:02:58 -04002127static int mlx4_port_immutable(struct ib_device *ibdev, u8 port_num,
2128 struct ib_port_immutable *immutable)
2129{
2130 struct ib_port_attr attr;
2131 int err;
2132
2133 err = mlx4_ib_query_port(ibdev, port_num, &attr);
2134 if (err)
2135 return err;
2136
2137 immutable->pkey_tbl_len = attr.pkey_tbl_len;
2138 immutable->gid_tbl_len = attr.gid_tbl_len;
2139
Ira Weinyf9b22e32015-05-13 20:02:59 -04002140 if (mlx4_ib_port_link_layer(ibdev, port_num) == IB_LINK_LAYER_INFINIBAND)
2141 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_IB;
2142 else
2143 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
2144
Ira Weiny337877a2015-06-06 14:38:29 -04002145 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
2146
Ira Weiny77386132015-05-13 20:02:58 -04002147 return 0;
2148}
2149
Roland Dreier225c7b12007-05-08 18:00:38 -07002150static void *mlx4_ib_add(struct mlx4_dev *dev)
2151{
2152 struct mlx4_ib_dev *ibdev;
Roland Dreier22e7ef92009-01-09 13:22:29 -08002153 int num_ports = 0;
Jack Morgenstein035b1032012-05-10 23:28:09 +03002154 int i, j;
Eli Cohenfa417f72010-10-24 21:08:52 -07002155 int err;
2156 struct mlx4_ib_iboe *iboe;
Matan Barak41966702014-02-02 17:06:47 +02002157 int ib_num_ports = 0;
Moni Shouaa5750092015-02-03 16:48:37 +02002158 int num_req_counters;
Eran Ben Elishac3abb512015-06-15 17:59:03 +03002159 int allocated;
2160 u32 counter_index;
Eran Ben Elisha3ba8e312015-10-15 14:44:40 +03002161 struct counter_index *new_counter_index = NULL;
Roland Dreier225c7b12007-05-08 18:00:38 -07002162
Shlomo Pongratz987c8f82012-04-29 17:04:26 +03002163 pr_info_once("%s", mlx4_ib_version);
Roland Dreier68f39482008-02-04 20:20:44 -08002164
Jack Morgenstein026149c2012-08-03 08:40:55 +00002165 num_ports = 0;
Eli Cohenfa417f72010-10-24 21:08:52 -07002166 mlx4_foreach_ib_transport_port(i, dev)
Roland Dreier22e7ef92009-01-09 13:22:29 -08002167 num_ports++;
2168
2169 /* No point in registering a device with no ports... */
2170 if (num_ports == 0)
2171 return NULL;
2172
Roland Dreier225c7b12007-05-08 18:00:38 -07002173 ibdev = (struct mlx4_ib_dev *) ib_alloc_device(sizeof *ibdev);
2174 if (!ibdev) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002175 dev_err(&dev->persist->pdev->dev,
2176 "Device struct alloc failed\n");
Roland Dreier225c7b12007-05-08 18:00:38 -07002177 return NULL;
2178 }
2179
Eli Cohenfa417f72010-10-24 21:08:52 -07002180 iboe = &ibdev->iboe;
2181
Roland Dreier225c7b12007-05-08 18:00:38 -07002182 if (mlx4_pd_alloc(dev, &ibdev->priv_pdn))
2183 goto err_dealloc;
2184
2185 if (mlx4_uar_alloc(dev, &ibdev->priv_uar))
2186 goto err_pd;
2187
Roland Dreier4979d182011-01-12 09:50:36 -08002188 ibdev->uar_map = ioremap((phys_addr_t) ibdev->priv_uar.pfn << PAGE_SHIFT,
2189 PAGE_SIZE);
Roland Dreier225c7b12007-05-08 18:00:38 -07002190 if (!ibdev->uar_map)
2191 goto err_uar;
Jack Morgenstein26c6bc72007-05-13 17:18:23 +03002192 MLX4_INIT_DOORBELL_LOCK(&ibdev->uar_lock);
Roland Dreier225c7b12007-05-08 18:00:38 -07002193
Roland Dreier225c7b12007-05-08 18:00:38 -07002194 ibdev->dev = dev;
Moni Shouac6215742015-02-03 16:48:39 +02002195 ibdev->bond_next_port = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07002196
2197 strlcpy(ibdev->ib_dev.name, "mlx4_%d", IB_DEVICE_NAME_MAX);
2198 ibdev->ib_dev.owner = THIS_MODULE;
2199 ibdev->ib_dev.node_type = RDMA_NODE_IB_CA;
Roland Dreier95d04f02008-07-23 08:12:26 -07002200 ibdev->ib_dev.local_dma_lkey = dev->caps.reserved_lkey;
Roland Dreier22e7ef92009-01-09 13:22:29 -08002201 ibdev->num_ports = num_ports;
Moni Shouaa5750092015-02-03 16:48:37 +02002202 ibdev->ib_dev.phys_port_cnt = mlx4_is_bonded(dev) ?
2203 1 : ibdev->num_ports;
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08002204 ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002205 ibdev->ib_dev.dma_device = &dev->persist->pdev->dev;
Moni Shoua5070cd22015-07-30 18:33:30 +03002206 ibdev->ib_dev.get_netdev = mlx4_ib_get_netdev;
2207 ibdev->ib_dev.add_gid = mlx4_ib_add_gid;
2208 ibdev->ib_dev.del_gid = mlx4_ib_del_gid;
Roland Dreier225c7b12007-05-08 18:00:38 -07002209
Or Gerlitz08ff3232012-10-21 14:59:24 +00002210 if (dev->caps.userspace_caps)
2211 ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_ABI_VERSION;
2212 else
2213 ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION;
2214
Roland Dreier225c7b12007-05-08 18:00:38 -07002215 ibdev->ib_dev.uverbs_cmd_mask =
2216 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
2217 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
2218 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
2219 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
2220 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
2221 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Matan Barak93769322014-07-31 11:01:30 +03002222 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Roland Dreier225c7b12007-05-08 18:00:38 -07002223 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
2224 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
2225 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
Vladimir Sokolovskybbf8eed12008-04-16 21:09:33 -07002226 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
Roland Dreier225c7b12007-05-08 18:00:38 -07002227 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
2228 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
2229 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
Jack Morgenstein6a775e22007-06-21 12:27:47 +03002230 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
Roland Dreier225c7b12007-05-08 18:00:38 -07002231 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
2232 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
2233 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
2234 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
2235 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
Jack Morgenstein65541cb2007-06-21 13:03:11 +03002236 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
Sean Hefty18abd5e2011-06-02 10:43:26 -07002237 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
Sean Hefty42849b22011-08-11 13:57:43 -07002238 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
2239 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Roland Dreier225c7b12007-05-08 18:00:38 -07002240
2241 ibdev->ib_dev.query_device = mlx4_ib_query_device;
2242 ibdev->ib_dev.query_port = mlx4_ib_query_port;
Eli Cohenfa417f72010-10-24 21:08:52 -07002243 ibdev->ib_dev.get_link_layer = mlx4_ib_port_link_layer;
Roland Dreier225c7b12007-05-08 18:00:38 -07002244 ibdev->ib_dev.query_gid = mlx4_ib_query_gid;
2245 ibdev->ib_dev.query_pkey = mlx4_ib_query_pkey;
2246 ibdev->ib_dev.modify_device = mlx4_ib_modify_device;
2247 ibdev->ib_dev.modify_port = mlx4_ib_modify_port;
2248 ibdev->ib_dev.alloc_ucontext = mlx4_ib_alloc_ucontext;
2249 ibdev->ib_dev.dealloc_ucontext = mlx4_ib_dealloc_ucontext;
2250 ibdev->ib_dev.mmap = mlx4_ib_mmap;
2251 ibdev->ib_dev.alloc_pd = mlx4_ib_alloc_pd;
2252 ibdev->ib_dev.dealloc_pd = mlx4_ib_dealloc_pd;
2253 ibdev->ib_dev.create_ah = mlx4_ib_create_ah;
2254 ibdev->ib_dev.query_ah = mlx4_ib_query_ah;
2255 ibdev->ib_dev.destroy_ah = mlx4_ib_destroy_ah;
2256 ibdev->ib_dev.create_srq = mlx4_ib_create_srq;
2257 ibdev->ib_dev.modify_srq = mlx4_ib_modify_srq;
Jack Morgenstein65541cb2007-06-21 13:03:11 +03002258 ibdev->ib_dev.query_srq = mlx4_ib_query_srq;
Roland Dreier225c7b12007-05-08 18:00:38 -07002259 ibdev->ib_dev.destroy_srq = mlx4_ib_destroy_srq;
2260 ibdev->ib_dev.post_srq_recv = mlx4_ib_post_srq_recv;
2261 ibdev->ib_dev.create_qp = mlx4_ib_create_qp;
2262 ibdev->ib_dev.modify_qp = mlx4_ib_modify_qp;
Jack Morgenstein6a775e22007-06-21 12:27:47 +03002263 ibdev->ib_dev.query_qp = mlx4_ib_query_qp;
Roland Dreier225c7b12007-05-08 18:00:38 -07002264 ibdev->ib_dev.destroy_qp = mlx4_ib_destroy_qp;
2265 ibdev->ib_dev.post_send = mlx4_ib_post_send;
2266 ibdev->ib_dev.post_recv = mlx4_ib_post_recv;
2267 ibdev->ib_dev.create_cq = mlx4_ib_create_cq;
Eli Cohen3fdcb972008-04-16 21:09:33 -07002268 ibdev->ib_dev.modify_cq = mlx4_ib_modify_cq;
Vladimir Sokolovskybbf8eed12008-04-16 21:09:33 -07002269 ibdev->ib_dev.resize_cq = mlx4_ib_resize_cq;
Roland Dreier225c7b12007-05-08 18:00:38 -07002270 ibdev->ib_dev.destroy_cq = mlx4_ib_destroy_cq;
2271 ibdev->ib_dev.poll_cq = mlx4_ib_poll_cq;
2272 ibdev->ib_dev.req_notify_cq = mlx4_ib_arm_cq;
2273 ibdev->ib_dev.get_dma_mr = mlx4_ib_get_dma_mr;
2274 ibdev->ib_dev.reg_user_mr = mlx4_ib_reg_user_mr;
Matan Barak93769322014-07-31 11:01:30 +03002275 ibdev->ib_dev.rereg_user_mr = mlx4_ib_rereg_user_mr;
Roland Dreier225c7b12007-05-08 18:00:38 -07002276 ibdev->ib_dev.dereg_mr = mlx4_ib_dereg_mr;
Sagi Grimberg679e34d2015-07-30 10:32:42 +03002277 ibdev->ib_dev.alloc_mr = mlx4_ib_alloc_mr;
Sagi Grimberg1b2cd0f2015-10-13 19:11:27 +03002278 ibdev->ib_dev.map_mr_sg = mlx4_ib_map_mr_sg;
Roland Dreier225c7b12007-05-08 18:00:38 -07002279 ibdev->ib_dev.attach_mcast = mlx4_ib_mcg_attach;
2280 ibdev->ib_dev.detach_mcast = mlx4_ib_mcg_detach;
2281 ibdev->ib_dev.process_mad = mlx4_ib_process_mad;
Ira Weiny77386132015-05-13 20:02:58 -04002282 ibdev->ib_dev.get_port_immutable = mlx4_port_immutable;
Yishai Hadasae184dd2015-08-13 18:32:06 +03002283 ibdev->ib_dev.disassociate_ucontext = mlx4_ib_disassociate_ucontext;
Roland Dreier225c7b12007-05-08 18:00:38 -07002284
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +00002285 if (!mlx4_is_slave(ibdev->dev)) {
2286 ibdev->ib_dev.alloc_fmr = mlx4_ib_fmr_alloc;
2287 ibdev->ib_dev.map_phys_fmr = mlx4_ib_map_phys_fmr;
2288 ibdev->ib_dev.unmap_fmr = mlx4_ib_unmap_fmr;
2289 ibdev->ib_dev.dealloc_fmr = mlx4_ib_fmr_dealloc;
2290 }
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +03002291
Shani Michaelib4253882013-02-06 16:19:16 +00002292 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_MEM_WINDOW ||
2293 dev->caps.bmme_flags & MLX4_BMME_FLAG_TYPE_2_WIN) {
2294 ibdev->ib_dev.alloc_mw = mlx4_ib_alloc_mw;
Shani Michaelib4253882013-02-06 16:19:16 +00002295 ibdev->ib_dev.dealloc_mw = mlx4_ib_dealloc_mw;
2296
2297 ibdev->ib_dev.uverbs_cmd_mask |=
2298 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
2299 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
2300 }
2301
Sean Hefty012a8ff2011-06-02 09:01:33 -07002302 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) {
2303 ibdev->ib_dev.alloc_xrcd = mlx4_ib_alloc_xrcd;
2304 ibdev->ib_dev.dealloc_xrcd = mlx4_ib_dealloc_xrcd;
2305 ibdev->ib_dev.uverbs_cmd_mask |=
2306 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
2307 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
2308 }
2309
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03002310 if (check_flow_steering_support(dev)) {
Matan Barak0a9b7d52013-11-07 15:25:15 +02002311 ibdev->steering_support = MLX4_STEERING_MODE_DEVICE_MANAGED;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03002312 ibdev->ib_dev.create_flow = mlx4_ib_create_flow;
2313 ibdev->ib_dev.destroy_flow = mlx4_ib_destroy_flow;
2314
Yann Droneaudf21519b2013-11-06 23:21:49 +01002315 ibdev->ib_dev.uverbs_ex_cmd_mask |=
2316 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
2317 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03002318 }
2319
Matan Barak4b664c42015-06-11 16:35:27 +03002320 ibdev->ib_dev.uverbs_ex_cmd_mask |=
2321 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
Eran Ben Elishafbfb6622015-10-15 14:44:42 +03002322 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
2323 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP);
Matan Barak4b664c42015-06-11 16:35:27 +03002324
Shlomo Pongratze605b742012-04-29 17:04:27 +03002325 mlx4_ib_alloc_eqs(dev, ibdev);
2326
Eli Cohenfa417f72010-10-24 21:08:52 -07002327 spin_lock_init(&iboe->lock);
2328
Roland Dreier225c7b12007-05-08 18:00:38 -07002329 if (init_node_data(ibdev))
2330 goto err_map;
2331
Eran Ben Elisha3ba8e312015-10-15 14:44:40 +03002332 for (i = 0; i < ibdev->num_ports; ++i) {
2333 mutex_init(&ibdev->counters_table[i].mutex);
2334 INIT_LIST_HEAD(&ibdev->counters_table[i].counters_list);
2335 }
2336
Moni Shouaa5750092015-02-03 16:48:37 +02002337 num_req_counters = mlx4_is_bonded(dev) ? 1 : ibdev->num_ports;
2338 for (i = 0; i < num_req_counters; ++i) {
Matan Barak9433c182014-05-15 15:29:28 +03002339 mutex_init(&ibdev->qp1_proxy_lock[i]);
Eran Ben Elishac3abb512015-06-15 17:59:03 +03002340 allocated = 0;
Or Gerlitzcfcde112011-06-15 14:49:57 +00002341 if (mlx4_ib_port_link_layer(&ibdev->ib_dev, i + 1) ==
2342 IB_LINK_LAYER_ETHERNET) {
Eran Ben Elishac3abb512015-06-15 17:59:03 +03002343 err = mlx4_counter_alloc(ibdev->dev, &counter_index);
2344 /* if failed to allocate a new counter, use default */
Or Gerlitzcfcde112011-06-15 14:49:57 +00002345 if (err)
Eran Ben Elishac3abb512015-06-15 17:59:03 +03002346 counter_index =
2347 mlx4_get_default_counter_index(dev,
2348 i + 1);
2349 else
2350 allocated = 1;
2351 } else { /* IB_LINK_LAYER_INFINIBAND use the default counter */
2352 counter_index = mlx4_get_default_counter_index(dev,
2353 i + 1);
Dan Carpenter3839d8a2014-03-28 11:21:39 +03002354 }
Eran Ben Elisha3ba8e312015-10-15 14:44:40 +03002355 new_counter_index = kmalloc(sizeof(*new_counter_index),
2356 GFP_KERNEL);
2357 if (!new_counter_index) {
2358 if (allocated)
2359 mlx4_counter_free(ibdev->dev, counter_index);
2360 goto err_counter;
2361 }
2362 new_counter_index->index = counter_index;
2363 new_counter_index->allocated = allocated;
2364 list_add_tail(&new_counter_index->list,
2365 &ibdev->counters_table[i].counters_list);
2366 ibdev->counters_table[i].default_counter = counter_index;
Eran Ben Elishac3abb512015-06-15 17:59:03 +03002367 pr_info("counter index %d for port %d allocated %d\n",
2368 counter_index, i + 1, allocated);
Or Gerlitzcfcde112011-06-15 14:49:57 +00002369 }
Moni Shouaa5750092015-02-03 16:48:37 +02002370 if (mlx4_is_bonded(dev))
Eran Ben Elishac3abb512015-06-15 17:59:03 +03002371 for (i = 1; i < ibdev->num_ports ; ++i) {
Eran Ben Elisha3ba8e312015-10-15 14:44:40 +03002372 new_counter_index =
2373 kmalloc(sizeof(struct counter_index),
2374 GFP_KERNEL);
2375 if (!new_counter_index)
2376 goto err_counter;
2377 new_counter_index->index = counter_index;
2378 new_counter_index->allocated = 0;
2379 list_add_tail(&new_counter_index->list,
2380 &ibdev->counters_table[i].counters_list);
2381 ibdev->counters_table[i].default_counter =
2382 counter_index;
Eran Ben Elishac3abb512015-06-15 17:59:03 +03002383 }
Or Gerlitzcfcde112011-06-15 14:49:57 +00002384
Matan Barak41966702014-02-02 17:06:47 +02002385 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
2386 ib_num_ports++;
2387
Roland Dreier225c7b12007-05-08 18:00:38 -07002388 spin_lock_init(&ibdev->sm_lock);
2389 mutex_init(&ibdev->cap_mask_mutex);
Yishai Hadas35f05da2015-02-08 11:49:34 +02002390 INIT_LIST_HEAD(&ibdev->qp_list);
2391 spin_lock_init(&ibdev->reset_flow_resource_lock);
Roland Dreier225c7b12007-05-08 18:00:38 -07002392
Matan Barak41966702014-02-02 17:06:47 +02002393 if (ibdev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED &&
2394 ib_num_ports) {
Matan Barakc1c98502013-11-07 15:25:17 +02002395 ibdev->steer_qpn_count = MLX4_IB_UC_MAX_NUM_QPS;
2396 err = mlx4_qp_reserve_range(dev, ibdev->steer_qpn_count,
2397 MLX4_IB_UC_STEER_QPN_ALIGN,
Eugenia Emantayevddae0342014-12-11 10:57:54 +02002398 &ibdev->steer_qpn_base, 0);
Matan Barakc1c98502013-11-07 15:25:17 +02002399 if (err)
2400 goto err_counter;
2401
2402 ibdev->ib_uc_qpns_bitmap =
2403 kmalloc(BITS_TO_LONGS(ibdev->steer_qpn_count) *
2404 sizeof(long),
2405 GFP_KERNEL);
2406 if (!ibdev->ib_uc_qpns_bitmap) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002407 dev_err(&dev->persist->pdev->dev,
2408 "bit map alloc failed\n");
Matan Barakc1c98502013-11-07 15:25:17 +02002409 goto err_steer_qp_release;
2410 }
2411
2412 bitmap_zero(ibdev->ib_uc_qpns_bitmap, ibdev->steer_qpn_count);
2413
2414 err = mlx4_FLOW_STEERING_IB_UC_QP_RANGE(
2415 dev, ibdev->steer_qpn_base,
2416 ibdev->steer_qpn_base +
2417 ibdev->steer_qpn_count - 1);
2418 if (err)
2419 goto err_steer_free_bitmap;
2420 }
2421
Jack Morgenstein3e0629c2014-09-11 14:11:17 +03002422 for (j = 1; j <= ibdev->dev->caps.num_ports; j++)
2423 atomic64_set(&iboe->mac[j - 1], ibdev->dev->caps.def_mac[j]);
2424
Ralph Campbell9a6edb62010-05-06 17:03:25 -07002425 if (ib_register_device(&ibdev->ib_dev, NULL))
Matan Barakc1c98502013-11-07 15:25:17 +02002426 goto err_steer_free_bitmap;
Roland Dreier225c7b12007-05-08 18:00:38 -07002427
2428 if (mlx4_ib_mad_init(ibdev))
2429 goto err_reg;
2430
Jack Morgensteinfc065732012-08-03 08:40:42 +00002431 if (mlx4_ib_init_sriov(ibdev))
2432 goto err_mad;
2433
Moni Shouad487ee72013-12-12 18:03:13 +02002434 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE) {
2435 if (!iboe->nb.notifier_call) {
2436 iboe->nb.notifier_call = mlx4_ib_netdev_event;
2437 err = register_netdevice_notifier(&iboe->nb);
2438 if (err) {
2439 iboe->nb.notifier_call = NULL;
2440 goto err_notif;
2441 }
2442 }
Eli Cohenfa417f72010-10-24 21:08:52 -07002443 }
2444
Jack Morgenstein035b1032012-05-10 23:28:09 +03002445 for (j = 0; j < ARRAY_SIZE(mlx4_class_attributes); ++j) {
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002446 if (device_create_file(&ibdev->ib_dev.dev,
Jack Morgenstein035b1032012-05-10 23:28:09 +03002447 mlx4_class_attributes[j]))
Eli Cohenfa417f72010-10-24 21:08:52 -07002448 goto err_notif;
Jack Morgensteincd9281d2007-09-18 09:14:18 +02002449 }
2450
Jack Morgenstein3b4a8cd2009-09-05 20:24:50 -07002451 ibdev->ib_active = true;
2452
Jack Morgenstein54679e12012-08-03 08:40:43 +00002453 if (mlx4_is_mfunc(ibdev->dev))
2454 init_pkeys(ibdev);
2455
Jack Morgenstein3806d082012-08-03 08:40:58 +00002456 /* create paravirt contexts for any VFs which are active */
2457 if (mlx4_is_master(ibdev->dev)) {
2458 for (j = 0; j < MLX4_MFUNC_MAX; j++) {
2459 if (j == mlx4_master_func_num(ibdev->dev))
2460 continue;
2461 if (mlx4_is_slave_active(ibdev->dev, j))
2462 do_slave_init(ibdev, j, 1);
2463 }
2464 }
Roland Dreier225c7b12007-05-08 18:00:38 -07002465 return ibdev;
2466
Eli Cohenfa417f72010-10-24 21:08:52 -07002467err_notif:
Moni Shouad487ee72013-12-12 18:03:13 +02002468 if (ibdev->iboe.nb.notifier_call) {
2469 if (unregister_netdevice_notifier(&ibdev->iboe.nb))
2470 pr_warn("failure unregistering notifier\n");
2471 ibdev->iboe.nb.notifier_call = NULL;
2472 }
Eli Cohenfa417f72010-10-24 21:08:52 -07002473 flush_workqueue(wq);
2474
Jack Morgensteinfc065732012-08-03 08:40:42 +00002475 mlx4_ib_close_sriov(ibdev);
2476
2477err_mad:
2478 mlx4_ib_mad_cleanup(ibdev);
2479
Roland Dreier225c7b12007-05-08 18:00:38 -07002480err_reg:
2481 ib_unregister_device(&ibdev->ib_dev);
2482
Matan Barakc1c98502013-11-07 15:25:17 +02002483err_steer_free_bitmap:
2484 kfree(ibdev->ib_uc_qpns_bitmap);
2485
2486err_steer_qp_release:
2487 if (ibdev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED)
2488 mlx4_qp_release_range(dev, ibdev->steer_qpn_base,
2489 ibdev->steer_qpn_count);
Or Gerlitzcfcde112011-06-15 14:49:57 +00002490err_counter:
Eran Ben Elisha3ba8e312015-10-15 14:44:40 +03002491 for (i = 0; i < ibdev->num_ports; ++i)
2492 mlx4_ib_delete_counters_table(ibdev, &ibdev->counters_table[i]);
2493
Roland Dreier225c7b12007-05-08 18:00:38 -07002494err_map:
2495 iounmap(ibdev->uar_map);
2496
2497err_uar:
2498 mlx4_uar_free(dev, &ibdev->priv_uar);
2499
2500err_pd:
2501 mlx4_pd_free(dev, ibdev->priv_pdn);
2502
2503err_dealloc:
2504 ib_dealloc_device(&ibdev->ib_dev);
2505
2506 return NULL;
2507}
2508
Matan Barakc1c98502013-11-07 15:25:17 +02002509int mlx4_ib_steer_qp_alloc(struct mlx4_ib_dev *dev, int count, int *qpn)
2510{
2511 int offset;
2512
2513 WARN_ON(!dev->ib_uc_qpns_bitmap);
2514
2515 offset = bitmap_find_free_region(dev->ib_uc_qpns_bitmap,
2516 dev->steer_qpn_count,
2517 get_count_order(count));
2518 if (offset < 0)
2519 return offset;
2520
2521 *qpn = dev->steer_qpn_base + offset;
2522 return 0;
2523}
2524
2525void mlx4_ib_steer_qp_free(struct mlx4_ib_dev *dev, u32 qpn, int count)
2526{
2527 if (!qpn ||
2528 dev->steering_support != MLX4_STEERING_MODE_DEVICE_MANAGED)
2529 return;
2530
2531 BUG_ON(qpn < dev->steer_qpn_base);
2532
2533 bitmap_release_region(dev->ib_uc_qpns_bitmap,
2534 qpn - dev->steer_qpn_base,
2535 get_count_order(count));
2536}
2537
2538int mlx4_ib_steer_qp_reg(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
2539 int is_attach)
2540{
2541 int err;
2542 size_t flow_size;
2543 struct ib_flow_attr *flow = NULL;
2544 struct ib_flow_spec_ib *ib_spec;
2545
2546 if (is_attach) {
2547 flow_size = sizeof(struct ib_flow_attr) +
2548 sizeof(struct ib_flow_spec_ib);
2549 flow = kzalloc(flow_size, GFP_KERNEL);
2550 if (!flow)
2551 return -ENOMEM;
2552 flow->port = mqp->port;
2553 flow->num_of_specs = 1;
2554 flow->size = flow_size;
2555 ib_spec = (struct ib_flow_spec_ib *)(flow + 1);
2556 ib_spec->type = IB_FLOW_SPEC_IB;
2557 ib_spec->size = sizeof(struct ib_flow_spec_ib);
2558 /* Add an empty rule for IB L2 */
2559 memset(&ib_spec->mask, 0, sizeof(ib_spec->mask));
2560
2561 err = __mlx4_ib_create_flow(&mqp->ibqp, flow,
2562 IB_FLOW_DOMAIN_NIC,
2563 MLX4_FS_REGULAR,
2564 &mqp->reg_id);
2565 } else {
2566 err = __mlx4_ib_destroy_flow(mdev->dev, mqp->reg_id);
2567 }
2568 kfree(flow);
2569 return err;
2570}
2571
Roland Dreier225c7b12007-05-08 18:00:38 -07002572static void mlx4_ib_remove(struct mlx4_dev *dev, void *ibdev_ptr)
2573{
2574 struct mlx4_ib_dev *ibdev = ibdev_ptr;
2575 int p;
2576
Moni Shoua4bf97152014-08-21 14:28:42 +03002577 ibdev->ib_active = false;
2578 flush_workqueue(wq);
2579
Jack Morgensteinfc065732012-08-03 08:40:42 +00002580 mlx4_ib_close_sriov(ibdev);
Yevgeny Petrilina6a47772009-03-18 19:49:54 -07002581 mlx4_ib_mad_cleanup(ibdev);
2582 ib_unregister_device(&ibdev->ib_dev);
Eli Cohenfa417f72010-10-24 21:08:52 -07002583 if (ibdev->iboe.nb.notifier_call) {
2584 if (unregister_netdevice_notifier(&ibdev->iboe.nb))
Shlomo Pongratz987c8f82012-04-29 17:04:26 +03002585 pr_warn("failure unregistering notifier\n");
Eli Cohenfa417f72010-10-24 21:08:52 -07002586 ibdev->iboe.nb.notifier_call = NULL;
2587 }
Matan Barakc1c98502013-11-07 15:25:17 +02002588
2589 if (ibdev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED) {
2590 mlx4_qp_release_range(dev, ibdev->steer_qpn_base,
2591 ibdev->steer_qpn_count);
2592 kfree(ibdev->ib_uc_qpns_bitmap);
2593 }
2594
Eli Cohenfa417f72010-10-24 21:08:52 -07002595 iounmap(ibdev->uar_map);
Or Gerlitzcfcde112011-06-15 14:49:57 +00002596 for (p = 0; p < ibdev->num_ports; ++p)
Eran Ben Elisha3ba8e312015-10-15 14:44:40 +03002597 mlx4_ib_delete_counters_table(ibdev, &ibdev->counters_table[p]);
2598
Eli Cohenfa417f72010-10-24 21:08:52 -07002599 mlx4_foreach_port(p, dev, MLX4_PORT_TYPE_IB)
Roland Dreier225c7b12007-05-08 18:00:38 -07002600 mlx4_CLOSE_PORT(dev, p);
2601
Shlomo Pongratze605b742012-04-29 17:04:27 +03002602 mlx4_ib_free_eqs(dev, ibdev);
2603
Roland Dreier225c7b12007-05-08 18:00:38 -07002604 mlx4_uar_free(dev, &ibdev->priv_uar);
2605 mlx4_pd_free(dev, ibdev->priv_pdn);
2606 ib_dealloc_device(&ibdev->ib_dev);
2607}
2608
Jack Morgensteinfc065732012-08-03 08:40:42 +00002609static void do_slave_init(struct mlx4_ib_dev *ibdev, int slave, int do_init)
2610{
2611 struct mlx4_ib_demux_work **dm = NULL;
2612 struct mlx4_dev *dev = ibdev->dev;
2613 int i;
2614 unsigned long flags;
Matan Barak449fc482014-03-19 18:11:52 +02002615 struct mlx4_active_ports actv_ports;
2616 unsigned int ports;
2617 unsigned int first_port;
Jack Morgensteinfc065732012-08-03 08:40:42 +00002618
2619 if (!mlx4_is_master(dev))
2620 return;
2621
Matan Barak449fc482014-03-19 18:11:52 +02002622 actv_ports = mlx4_get_active_ports(dev, slave);
2623 ports = bitmap_weight(actv_ports.ports, dev->caps.num_ports);
2624 first_port = find_first_bit(actv_ports.ports, dev->caps.num_ports);
2625
2626 dm = kcalloc(ports, sizeof(*dm), GFP_ATOMIC);
Jack Morgensteinfc065732012-08-03 08:40:42 +00002627 if (!dm) {
2628 pr_err("failed to allocate memory for tunneling qp update\n");
Maninder Singha39a98f2015-07-08 09:43:35 +05302629 return;
Jack Morgensteinfc065732012-08-03 08:40:42 +00002630 }
2631
Matan Barak449fc482014-03-19 18:11:52 +02002632 for (i = 0; i < ports; i++) {
Jack Morgensteinfc065732012-08-03 08:40:42 +00002633 dm[i] = kmalloc(sizeof (struct mlx4_ib_demux_work), GFP_ATOMIC);
2634 if (!dm[i]) {
2635 pr_err("failed to allocate memory for tunneling qp update work struct\n");
Maninder Singha39a98f2015-07-08 09:43:35 +05302636 while (--i >= 0)
2637 kfree(dm[i]);
Jack Morgensteinfc065732012-08-03 08:40:42 +00002638 goto out;
2639 }
Jack Morgensteinfc065732012-08-03 08:40:42 +00002640 INIT_WORK(&dm[i]->work, mlx4_ib_tunnels_update_work);
Matan Barak449fc482014-03-19 18:11:52 +02002641 dm[i]->port = first_port + i + 1;
Jack Morgensteinfc065732012-08-03 08:40:42 +00002642 dm[i]->slave = slave;
2643 dm[i]->do_init = do_init;
2644 dm[i]->dev = ibdev;
Doug Ledfordd9a047a2015-07-09 10:21:08 -04002645 }
2646 /* initialize or tear down tunnel QPs for the slave */
2647 spin_lock_irqsave(&ibdev->sriov.going_down_lock, flags);
2648 if (!ibdev->sriov.is_going_down) {
2649 for (i = 0; i < ports; i++)
Jack Morgensteinfc065732012-08-03 08:40:42 +00002650 queue_work(ibdev->sriov.demux[i].ud_wq, &dm[i]->work);
2651 spin_unlock_irqrestore(&ibdev->sriov.going_down_lock, flags);
Doug Ledfordd9a047a2015-07-09 10:21:08 -04002652 } else {
2653 spin_unlock_irqrestore(&ibdev->sriov.going_down_lock, flags);
2654 for (i = 0; i < ports; i++)
2655 kfree(dm[i]);
Jack Morgensteinfc065732012-08-03 08:40:42 +00002656 }
2657out:
Syam Sidhardhanc89d1272013-02-24 23:20:05 +00002658 kfree(dm);
Jack Morgensteinfc065732012-08-03 08:40:42 +00002659 return;
2660}
2661
Yishai Hadas35f05da2015-02-08 11:49:34 +02002662static void mlx4_ib_handle_catas_error(struct mlx4_ib_dev *ibdev)
2663{
2664 struct mlx4_ib_qp *mqp;
2665 unsigned long flags_qp;
2666 unsigned long flags_cq;
2667 struct mlx4_ib_cq *send_mcq, *recv_mcq;
2668 struct list_head cq_notify_list;
2669 struct mlx4_cq *mcq;
2670 unsigned long flags;
2671
2672 pr_warn("mlx4_ib_handle_catas_error was started\n");
2673 INIT_LIST_HEAD(&cq_notify_list);
2674
2675 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
2676 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
2677
2678 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
2679 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
2680 if (mqp->sq.tail != mqp->sq.head) {
2681 send_mcq = to_mcq(mqp->ibqp.send_cq);
2682 spin_lock_irqsave(&send_mcq->lock, flags_cq);
2683 if (send_mcq->mcq.comp &&
2684 mqp->ibqp.send_cq->comp_handler) {
2685 if (!send_mcq->mcq.reset_notify_added) {
2686 send_mcq->mcq.reset_notify_added = 1;
2687 list_add_tail(&send_mcq->mcq.reset_notify,
2688 &cq_notify_list);
2689 }
2690 }
2691 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
2692 }
2693 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
2694 /* Now, handle the QP's receive queue */
2695 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
2696 /* no handling is needed for SRQ */
2697 if (!mqp->ibqp.srq) {
2698 if (mqp->rq.tail != mqp->rq.head) {
2699 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
2700 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
2701 if (recv_mcq->mcq.comp &&
2702 mqp->ibqp.recv_cq->comp_handler) {
2703 if (!recv_mcq->mcq.reset_notify_added) {
2704 recv_mcq->mcq.reset_notify_added = 1;
2705 list_add_tail(&recv_mcq->mcq.reset_notify,
2706 &cq_notify_list);
2707 }
2708 }
2709 spin_unlock_irqrestore(&recv_mcq->lock,
2710 flags_cq);
2711 }
2712 }
2713 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
2714 }
2715
2716 list_for_each_entry(mcq, &cq_notify_list, reset_notify) {
2717 mcq->comp(mcq);
2718 }
2719 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
2720 pr_warn("mlx4_ib_handle_catas_error ended\n");
2721}
2722
Moni Shouaa5750092015-02-03 16:48:37 +02002723static void handle_bonded_port_state_event(struct work_struct *work)
2724{
2725 struct ib_event_work *ew =
2726 container_of(work, struct ib_event_work, work);
2727 struct mlx4_ib_dev *ibdev = ew->ib_dev;
2728 enum ib_port_state bonded_port_state = IB_PORT_NOP;
2729 int i;
2730 struct ib_event ibev;
2731
2732 kfree(ew);
2733 spin_lock_bh(&ibdev->iboe.lock);
2734 for (i = 0; i < MLX4_MAX_PORTS; ++i) {
2735 struct net_device *curr_netdev = ibdev->iboe.netdevs[i];
Moni Shoua217e8b12015-03-18 16:51:35 +02002736 enum ib_port_state curr_port_state;
Moni Shouaa5750092015-02-03 16:48:37 +02002737
Moni Shoua217e8b12015-03-18 16:51:35 +02002738 if (!curr_netdev)
2739 continue;
2740
2741 curr_port_state =
Moni Shouaa5750092015-02-03 16:48:37 +02002742 (netif_running(curr_netdev) &&
2743 netif_carrier_ok(curr_netdev)) ?
2744 IB_PORT_ACTIVE : IB_PORT_DOWN;
2745
2746 bonded_port_state = (bonded_port_state != IB_PORT_ACTIVE) ?
2747 curr_port_state : IB_PORT_ACTIVE;
2748 }
2749 spin_unlock_bh(&ibdev->iboe.lock);
2750
2751 ibev.device = &ibdev->ib_dev;
2752 ibev.element.port_num = 1;
2753 ibev.event = (bonded_port_state == IB_PORT_ACTIVE) ?
2754 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2755
2756 ib_dispatch_event(&ibev);
2757}
2758
Roland Dreier225c7b12007-05-08 18:00:38 -07002759static void mlx4_ib_event(struct mlx4_dev *dev, void *ibdev_ptr,
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03002760 enum mlx4_dev_event event, unsigned long param)
Roland Dreier225c7b12007-05-08 18:00:38 -07002761{
2762 struct ib_event ibev;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07002763 struct mlx4_ib_dev *ibdev = to_mdev((struct ib_device *) ibdev_ptr);
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03002764 struct mlx4_eqe *eqe = NULL;
2765 struct ib_event_work *ew;
Jack Morgensteinfc065732012-08-03 08:40:42 +00002766 int p = 0;
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03002767
Moni Shouaa5750092015-02-03 16:48:37 +02002768 if (mlx4_is_bonded(dev) &&
2769 ((event == MLX4_DEV_EVENT_PORT_UP) ||
2770 (event == MLX4_DEV_EVENT_PORT_DOWN))) {
2771 ew = kmalloc(sizeof(*ew), GFP_ATOMIC);
2772 if (!ew)
2773 return;
2774 INIT_WORK(&ew->work, handle_bonded_port_state_event);
2775 ew->ib_dev = ibdev;
2776 queue_work(wq, &ew->work);
2777 return;
2778 }
2779
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03002780 if (event == MLX4_DEV_EVENT_PORT_MGMT_CHANGE)
2781 eqe = (struct mlx4_eqe *)param;
2782 else
Jack Morgensteinfc065732012-08-03 08:40:42 +00002783 p = (int) param;
Roland Dreier225c7b12007-05-08 18:00:38 -07002784
2785 switch (event) {
Roland Dreier37608ee2008-04-16 21:01:08 -07002786 case MLX4_DEV_EVENT_PORT_UP:
Jack Morgensteinfc065732012-08-03 08:40:42 +00002787 if (p > ibdev->num_ports)
2788 return;
Jack Morgensteina0c64a12012-08-03 08:40:49 +00002789 if (mlx4_is_master(dev) &&
2790 rdma_port_get_link_layer(&ibdev->ib_dev, p) ==
2791 IB_LINK_LAYER_INFINIBAND) {
2792 mlx4_ib_invalidate_all_guid_record(ibdev, p);
2793 }
Roland Dreier37608ee2008-04-16 21:01:08 -07002794 ibev.event = IB_EVENT_PORT_ACTIVE;
Roland Dreier225c7b12007-05-08 18:00:38 -07002795 break;
2796
Roland Dreier37608ee2008-04-16 21:01:08 -07002797 case MLX4_DEV_EVENT_PORT_DOWN:
Jack Morgensteinfc065732012-08-03 08:40:42 +00002798 if (p > ibdev->num_ports)
2799 return;
Roland Dreier37608ee2008-04-16 21:01:08 -07002800 ibev.event = IB_EVENT_PORT_ERR;
2801 break;
2802
2803 case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
Jack Morgenstein3b4a8cd2009-09-05 20:24:50 -07002804 ibdev->ib_active = false;
Roland Dreier225c7b12007-05-08 18:00:38 -07002805 ibev.event = IB_EVENT_DEVICE_FATAL;
Yishai Hadas35f05da2015-02-08 11:49:34 +02002806 mlx4_ib_handle_catas_error(ibdev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002807 break;
2808
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03002809 case MLX4_DEV_EVENT_PORT_MGMT_CHANGE:
2810 ew = kmalloc(sizeof *ew, GFP_ATOMIC);
2811 if (!ew) {
2812 pr_err("failed to allocate memory for events work\n");
2813 break;
2814 }
2815
2816 INIT_WORK(&ew->work, handle_port_mgmt_change_event);
2817 memcpy(&ew->ib_eqe, eqe, sizeof *eqe);
2818 ew->ib_dev = ibdev;
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +00002819 /* need to queue only for port owner, which uses GEN_EQE */
2820 if (mlx4_is_master(dev))
2821 queue_work(wq, &ew->work);
2822 else
2823 handle_port_mgmt_change_event(&ew->work);
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03002824 return;
2825
Jack Morgensteinfc065732012-08-03 08:40:42 +00002826 case MLX4_DEV_EVENT_SLAVE_INIT:
2827 /* here, p is the slave id */
2828 do_slave_init(ibdev, p, 1);
Yishai Hadasee59fa02015-03-03 17:28:49 +02002829 if (mlx4_is_master(dev)) {
2830 int i;
2831
2832 for (i = 1; i <= ibdev->num_ports; i++) {
2833 if (rdma_port_get_link_layer(&ibdev->ib_dev, i)
2834 == IB_LINK_LAYER_INFINIBAND)
2835 mlx4_ib_slave_alias_guid_event(ibdev,
2836 p, i,
2837 1);
2838 }
2839 }
Jack Morgensteinfc065732012-08-03 08:40:42 +00002840 return;
2841
2842 case MLX4_DEV_EVENT_SLAVE_SHUTDOWN:
Yishai Hadasee59fa02015-03-03 17:28:49 +02002843 if (mlx4_is_master(dev)) {
2844 int i;
2845
2846 for (i = 1; i <= ibdev->num_ports; i++) {
2847 if (rdma_port_get_link_layer(&ibdev->ib_dev, i)
2848 == IB_LINK_LAYER_INFINIBAND)
2849 mlx4_ib_slave_alias_guid_event(ibdev,
2850 p, i,
2851 0);
2852 }
2853 }
Jack Morgensteinfc065732012-08-03 08:40:42 +00002854 /* here, p is the slave id */
2855 do_slave_init(ibdev, p, 0);
2856 return;
2857
Roland Dreier225c7b12007-05-08 18:00:38 -07002858 default:
2859 return;
2860 }
2861
2862 ibev.device = ibdev_ptr;
Moni Shouaa5750092015-02-03 16:48:37 +02002863 ibev.element.port_num = mlx4_is_bonded(ibdev->dev) ? 1 : (u8)p;
Roland Dreier225c7b12007-05-08 18:00:38 -07002864
2865 ib_dispatch_event(&ibev);
2866}
2867
2868static struct mlx4_interface mlx4_ib_interface = {
Eli Cohenfa417f72010-10-24 21:08:52 -07002869 .add = mlx4_ib_add,
2870 .remove = mlx4_ib_remove,
2871 .event = mlx4_ib_event,
Moni Shouaa5750092015-02-03 16:48:37 +02002872 .protocol = MLX4_PROT_IB_IPV6,
2873 .flags = MLX4_INTFF_BONDING
Roland Dreier225c7b12007-05-08 18:00:38 -07002874};
2875
2876static int __init mlx4_ib_init(void)
2877{
Eli Cohenfa417f72010-10-24 21:08:52 -07002878 int err;
2879
2880 wq = create_singlethread_workqueue("mlx4_ib");
2881 if (!wq)
2882 return -ENOMEM;
2883
Oren Duerb9c5d6a2012-08-03 08:40:46 +00002884 err = mlx4_ib_mcg_init();
2885 if (err)
2886 goto clean_wq;
2887
Eli Cohenfa417f72010-10-24 21:08:52 -07002888 err = mlx4_register_interface(&mlx4_ib_interface);
Oren Duerb9c5d6a2012-08-03 08:40:46 +00002889 if (err)
2890 goto clean_mcg;
Eli Cohenfa417f72010-10-24 21:08:52 -07002891
2892 return 0;
Oren Duerb9c5d6a2012-08-03 08:40:46 +00002893
2894clean_mcg:
2895 mlx4_ib_mcg_destroy();
2896
2897clean_wq:
2898 destroy_workqueue(wq);
2899 return err;
Roland Dreier225c7b12007-05-08 18:00:38 -07002900}
2901
2902static void __exit mlx4_ib_cleanup(void)
2903{
2904 mlx4_unregister_interface(&mlx4_ib_interface);
Oren Duerb9c5d6a2012-08-03 08:40:46 +00002905 mlx4_ib_mcg_destroy();
Eli Cohenfa417f72010-10-24 21:08:52 -07002906 destroy_workqueue(wq);
Roland Dreier225c7b12007-05-08 18:00:38 -07002907}
2908
2909module_init(mlx4_ib_init);
2910module_exit(mlx4_ib_cleanup);