blob: 4a67ffc1ddfbd4bb5610a82dc9037b16efbe7365 [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>
Jiri Pirko09d4d082016-02-26 17:32:24 +010044#include <net/devlink.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070045
46#include <rdma/ib_smi.h>
47#include <rdma/ib_user_verbs.h>
Eli Cohenfa417f72010-10-24 21:08:52 -070048#include <rdma/ib_addr.h>
Moni Shouae26be1b2015-07-30 18:33:29 +030049#include <rdma/ib_cache.h>
50
51#include <net/bonding.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070052
53#include <linux/mlx4/driver.h>
54#include <linux/mlx4/cmd.h>
Matan Barak9433c182014-05-15 15:29:28 +030055#include <linux/mlx4/qp.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070056
57#include "mlx4_ib.h"
Leon Romanovsky9ce28a22016-09-22 17:31:14 +030058#include <rdma/mlx4-abi.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070059
Jack Morgensteinb1d8eb52012-06-19 11:21:35 +030060#define DRV_NAME MLX4_IB_DRV_NAME
Amir Vadai169a1d82014-02-19 17:47:31 +020061#define DRV_VERSION "2.2-1"
62#define DRV_RELDATE "Feb 2014"
Roland Dreier225c7b12007-05-08 18:00:38 -070063
Hadar Hen Zionf77c0162013-08-14 13:58:31 +030064#define MLX4_IB_FLOW_MAX_PRIO 0xFFF
Matan Baraka37a1a42013-11-07 15:25:16 +020065#define MLX4_IB_FLOW_QPN_MASK 0xFFFFFF
Markus Stockhausen50e2ec92014-08-13 14:07:30 +000066#define MLX4_IB_CARD_REV_A0 0xA0
Hadar Hen Zionf77c0162013-08-14 13:58:31 +030067
Roland Dreier225c7b12007-05-08 18:00:38 -070068MODULE_AUTHOR("Roland Dreier");
69MODULE_DESCRIPTION("Mellanox ConnectX HCA InfiniBand driver");
70MODULE_LICENSE("Dual BSD/GPL");
71MODULE_VERSION(DRV_VERSION);
72
Yishai Hadas56c1d232015-02-12 09:49:43 +020073int mlx4_ib_sm_guid_assign = 0;
Jack Morgensteina0c64a12012-08-03 08:40:49 +000074module_param_named(sm_guid_assign, mlx4_ib_sm_guid_assign, int, 0444);
Yishai Hadas56c1d232015-02-12 09:49:43 +020075MODULE_PARM_DESC(sm_guid_assign, "Enable SM alias_GUID assignment if sm_guid_assign > 0 (Default: 0)");
Jack Morgensteina0c64a12012-08-03 08:40:49 +000076
Roland Dreier68f39482008-02-04 20:20:44 -080077static const char mlx4_ib_version[] =
Roland Dreier225c7b12007-05-08 18:00:38 -070078 DRV_NAME ": Mellanox ConnectX InfiniBand driver v"
79 DRV_VERSION " (" DRV_RELDATE ")\n";
80
Jack Morgenstein3806d082012-08-03 08:40:58 +000081static void do_slave_init(struct mlx4_ib_dev *ibdev, int slave, int do_init);
82
Eli Cohenfa417f72010-10-24 21:08:52 -070083static struct workqueue_struct *wq;
84
Roland Dreier225c7b12007-05-08 18:00:38 -070085static void init_query_mad(struct ib_smp *mad)
86{
87 mad->base_version = 1;
88 mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
89 mad->class_version = 1;
90 mad->method = IB_MGMT_METHOD_GET;
91}
92
Hadar Hen Zionf77c0162013-08-14 13:58:31 +030093static int check_flow_steering_support(struct mlx4_dev *dev)
94{
Matan Barak0a9b7d52013-11-07 15:25:15 +020095 int eth_num_ports = 0;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +030096 int ib_num_ports = 0;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +030097
Matan Barak0a9b7d52013-11-07 15:25:15 +020098 int dmfs = dev->caps.steering_mode == MLX4_STEERING_MODE_DEVICE_MANAGED;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +030099
Matan Barak0a9b7d52013-11-07 15:25:15 +0200100 if (dmfs) {
101 int i;
102 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH)
103 eth_num_ports++;
104 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
105 ib_num_ports++;
106 dmfs &= (!ib_num_ports ||
107 (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_DMFS_IPOIB)) &&
108 (!eth_num_ports ||
109 (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FS_EN));
110 if (ib_num_ports && mlx4_is_mfunc(dev)) {
111 pr_warn("Device managed flow steering is unavailable for IB port in multifunction env.\n");
112 dmfs = 0;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +0300113 }
Hadar Hen Zionf77c0162013-08-14 13:58:31 +0300114 }
Matan Barak0a9b7d52013-11-07 15:25:15 +0200115 return dmfs;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +0300116}
117
Jack Morgenstein3dec4872014-09-11 14:11:19 +0300118static int num_ib_ports(struct mlx4_dev *dev)
119{
120 int ib_ports = 0;
121 int i;
122
123 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
124 ib_ports++;
125
126 return ib_ports;
127}
128
Moni Shouae26be1b2015-07-30 18:33:29 +0300129static struct net_device *mlx4_ib_get_netdev(struct ib_device *device, u8 port_num)
130{
131 struct mlx4_ib_dev *ibdev = to_mdev(device);
132 struct net_device *dev;
133
134 rcu_read_lock();
135 dev = mlx4_get_protocol_dev(ibdev->dev, MLX4_PROT_ETH, port_num);
136
137 if (dev) {
138 if (mlx4_is_bonded(ibdev->dev)) {
139 struct net_device *upper = NULL;
140
141 upper = netdev_master_upper_dev_get_rcu(dev);
142 if (upper) {
143 struct net_device *active;
144
145 active = bond_option_active_slave_get_rcu(netdev_priv(upper));
146 if (active)
147 dev = active;
148 }
149 }
150 }
151 if (dev)
152 dev_hold(dev);
153
154 rcu_read_unlock();
155 return dev;
156}
157
Moni Shoua7e57b852016-01-14 17:50:35 +0200158static int mlx4_ib_update_gids_v1(struct gid_entry *gids,
159 struct mlx4_ib_dev *ibdev,
160 u8 port_num)
Moni Shouae26be1b2015-07-30 18:33:29 +0300161{
162 struct mlx4_cmd_mailbox *mailbox;
163 int err;
164 struct mlx4_dev *dev = ibdev->dev;
165 int i;
166 union ib_gid *gid_tbl;
167
168 mailbox = mlx4_alloc_cmd_mailbox(dev);
169 if (IS_ERR(mailbox))
170 return -ENOMEM;
171
172 gid_tbl = mailbox->buf;
173
174 for (i = 0; i < MLX4_MAX_PORT_GIDS; ++i)
175 memcpy(&gid_tbl[i], &gids[i].gid, sizeof(union ib_gid));
176
177 err = mlx4_cmd(dev, mailbox->dma,
178 MLX4_SET_PORT_GID_TABLE << 8 | port_num,
179 1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
180 MLX4_CMD_WRAPPED);
181 if (mlx4_is_bonded(dev))
182 err += mlx4_cmd(dev, mailbox->dma,
183 MLX4_SET_PORT_GID_TABLE << 8 | 2,
184 1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
185 MLX4_CMD_WRAPPED);
186
187 mlx4_free_cmd_mailbox(dev, mailbox);
188 return err;
189}
190
Moni Shoua7e57b852016-01-14 17:50:35 +0200191static int mlx4_ib_update_gids_v1_v2(struct gid_entry *gids,
192 struct mlx4_ib_dev *ibdev,
193 u8 port_num)
194{
195 struct mlx4_cmd_mailbox *mailbox;
196 int err;
197 struct mlx4_dev *dev = ibdev->dev;
198 int i;
199 struct {
200 union ib_gid gid;
201 __be32 rsrvd1[2];
202 __be16 rsrvd2;
203 u8 type;
204 u8 version;
205 __be32 rsrvd3;
206 } *gid_tbl;
207
208 mailbox = mlx4_alloc_cmd_mailbox(dev);
209 if (IS_ERR(mailbox))
210 return -ENOMEM;
211
212 gid_tbl = mailbox->buf;
213 for (i = 0; i < MLX4_MAX_PORT_GIDS; ++i) {
214 memcpy(&gid_tbl[i].gid, &gids[i].gid, sizeof(union ib_gid));
215 if (gids[i].gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) {
216 gid_tbl[i].version = 2;
217 if (!ipv6_addr_v4mapped((struct in6_addr *)&gids[i].gid))
218 gid_tbl[i].type = 1;
219 else
220 memset(&gid_tbl[i].gid, 0, 12);
221 }
222 }
223
224 err = mlx4_cmd(dev, mailbox->dma,
225 MLX4_SET_PORT_ROCE_ADDR << 8 | port_num,
226 1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
227 MLX4_CMD_WRAPPED);
228 if (mlx4_is_bonded(dev))
229 err += mlx4_cmd(dev, mailbox->dma,
230 MLX4_SET_PORT_ROCE_ADDR << 8 | 2,
231 1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
232 MLX4_CMD_WRAPPED);
233
234 mlx4_free_cmd_mailbox(dev, mailbox);
235 return err;
236}
237
238static int mlx4_ib_update_gids(struct gid_entry *gids,
239 struct mlx4_ib_dev *ibdev,
240 u8 port_num)
241{
242 if (ibdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2)
243 return mlx4_ib_update_gids_v1_v2(gids, ibdev, port_num);
244
245 return mlx4_ib_update_gids_v1(gids, ibdev, port_num);
246}
247
Moni Shouae26be1b2015-07-30 18:33:29 +0300248static int mlx4_ib_add_gid(struct ib_device *device,
249 u8 port_num,
250 unsigned int index,
251 const union ib_gid *gid,
252 const struct ib_gid_attr *attr,
253 void **context)
254{
255 struct mlx4_ib_dev *ibdev = to_mdev(device);
256 struct mlx4_ib_iboe *iboe = &ibdev->iboe;
257 struct mlx4_port_gid_table *port_gid_table;
258 int free = -1, found = -1;
259 int ret = 0;
260 int hw_update = 0;
261 int i;
262 struct gid_entry *gids = NULL;
263
264 if (!rdma_cap_roce_gid_table(device, port_num))
265 return -EINVAL;
266
267 if (port_num > MLX4_MAX_PORTS)
268 return -EINVAL;
269
270 if (!context)
271 return -EINVAL;
272
273 port_gid_table = &iboe->gids[port_num - 1];
274 spin_lock_bh(&iboe->lock);
275 for (i = 0; i < MLX4_MAX_PORT_GIDS; ++i) {
Moni Shouab699a852016-01-14 17:50:33 +0200276 if (!memcmp(&port_gid_table->gids[i].gid, gid, sizeof(*gid)) &&
277 (port_gid_table->gids[i].gid_type == attr->gid_type)) {
Moni Shouae26be1b2015-07-30 18:33:29 +0300278 found = i;
279 break;
280 }
281 if (free < 0 && !memcmp(&port_gid_table->gids[i].gid, &zgid, sizeof(*gid)))
282 free = i; /* HW has space */
283 }
284
285 if (found < 0) {
286 if (free < 0) {
287 ret = -ENOSPC;
288 } else {
289 port_gid_table->gids[free].ctx = kmalloc(sizeof(*port_gid_table->gids[free].ctx), GFP_ATOMIC);
290 if (!port_gid_table->gids[free].ctx) {
291 ret = -ENOMEM;
292 } else {
293 *context = port_gid_table->gids[free].ctx;
294 memcpy(&port_gid_table->gids[free].gid, gid, sizeof(*gid));
Moni Shouab699a852016-01-14 17:50:33 +0200295 port_gid_table->gids[free].gid_type = attr->gid_type;
Moni Shouae26be1b2015-07-30 18:33:29 +0300296 port_gid_table->gids[free].ctx->real_index = free;
297 port_gid_table->gids[free].ctx->refcount = 1;
298 hw_update = 1;
299 }
300 }
301 } else {
302 struct gid_cache_context *ctx = port_gid_table->gids[found].ctx;
303 *context = ctx;
304 ctx->refcount++;
305 }
306 if (!ret && hw_update) {
307 gids = kmalloc(sizeof(*gids) * MLX4_MAX_PORT_GIDS, GFP_ATOMIC);
308 if (!gids) {
309 ret = -ENOMEM;
310 } else {
Moni Shouab699a852016-01-14 17:50:33 +0200311 for (i = 0; i < MLX4_MAX_PORT_GIDS; i++) {
Moni Shouae26be1b2015-07-30 18:33:29 +0300312 memcpy(&gids[i].gid, &port_gid_table->gids[i].gid, sizeof(union ib_gid));
Moni Shouab699a852016-01-14 17:50:33 +0200313 gids[i].gid_type = port_gid_table->gids[i].gid_type;
314 }
Moni Shouae26be1b2015-07-30 18:33:29 +0300315 }
316 }
317 spin_unlock_bh(&iboe->lock);
318
319 if (!ret && hw_update) {
320 ret = mlx4_ib_update_gids(gids, ibdev, port_num);
321 kfree(gids);
322 }
323
324 return ret;
325}
326
327static int mlx4_ib_del_gid(struct ib_device *device,
328 u8 port_num,
329 unsigned int index,
330 void **context)
331{
332 struct gid_cache_context *ctx = *context;
333 struct mlx4_ib_dev *ibdev = to_mdev(device);
334 struct mlx4_ib_iboe *iboe = &ibdev->iboe;
335 struct mlx4_port_gid_table *port_gid_table;
336 int ret = 0;
337 int hw_update = 0;
338 struct gid_entry *gids = NULL;
339
340 if (!rdma_cap_roce_gid_table(device, port_num))
341 return -EINVAL;
342
343 if (port_num > MLX4_MAX_PORTS)
344 return -EINVAL;
345
346 port_gid_table = &iboe->gids[port_num - 1];
347 spin_lock_bh(&iboe->lock);
348 if (ctx) {
349 ctx->refcount--;
350 if (!ctx->refcount) {
351 unsigned int real_index = ctx->real_index;
352
353 memcpy(&port_gid_table->gids[real_index].gid, &zgid, sizeof(zgid));
354 kfree(port_gid_table->gids[real_index].ctx);
355 port_gid_table->gids[real_index].ctx = NULL;
356 hw_update = 1;
357 }
358 }
359 if (!ret && hw_update) {
360 int i;
361
362 gids = kmalloc(sizeof(*gids) * MLX4_MAX_PORT_GIDS, GFP_ATOMIC);
363 if (!gids) {
364 ret = -ENOMEM;
365 } else {
366 for (i = 0; i < MLX4_MAX_PORT_GIDS; i++)
367 memcpy(&gids[i].gid, &port_gid_table->gids[i].gid, sizeof(union ib_gid));
368 }
369 }
370 spin_unlock_bh(&iboe->lock);
371
372 if (!ret && hw_update) {
373 ret = mlx4_ib_update_gids(gids, ibdev, port_num);
374 kfree(gids);
375 }
376 return ret;
377}
378
379int mlx4_ib_gid_index_to_real_index(struct mlx4_ib_dev *ibdev,
380 u8 port_num, int index)
381{
382 struct mlx4_ib_iboe *iboe = &ibdev->iboe;
383 struct gid_cache_context *ctx = NULL;
384 union ib_gid gid;
385 struct mlx4_port_gid_table *port_gid_table;
386 int real_index = -EINVAL;
387 int i;
388 int ret;
389 unsigned long flags;
Moni Shouab699a852016-01-14 17:50:33 +0200390 struct ib_gid_attr attr;
Moni Shouae26be1b2015-07-30 18:33:29 +0300391
392 if (port_num > MLX4_MAX_PORTS)
393 return -EINVAL;
394
395 if (mlx4_is_bonded(ibdev->dev))
396 port_num = 1;
397
398 if (!rdma_cap_roce_gid_table(&ibdev->ib_dev, port_num))
399 return index;
400
Moni Shouab699a852016-01-14 17:50:33 +0200401 ret = ib_get_cached_gid(&ibdev->ib_dev, port_num, index, &gid, &attr);
Moni Shouae26be1b2015-07-30 18:33:29 +0300402 if (ret)
403 return ret;
404
Moni Shouab699a852016-01-14 17:50:33 +0200405 if (attr.ndev)
406 dev_put(attr.ndev);
407
Moni Shouae26be1b2015-07-30 18:33:29 +0300408 if (!memcmp(&gid, &zgid, sizeof(gid)))
409 return -EINVAL;
410
411 spin_lock_irqsave(&iboe->lock, flags);
412 port_gid_table = &iboe->gids[port_num - 1];
413
414 for (i = 0; i < MLX4_MAX_PORT_GIDS; ++i)
Moni Shouab699a852016-01-14 17:50:33 +0200415 if (!memcmp(&port_gid_table->gids[i].gid, &gid, sizeof(gid)) &&
416 attr.gid_type == port_gid_table->gids[i].gid_type) {
Moni Shouae26be1b2015-07-30 18:33:29 +0300417 ctx = port_gid_table->gids[i].ctx;
418 break;
419 }
420 if (ctx)
421 real_index = ctx->real_index;
422 spin_unlock_irqrestore(&iboe->lock, flags);
423 return real_index;
424}
425
Roland Dreier225c7b12007-05-08 18:00:38 -0700426static int mlx4_ib_query_device(struct ib_device *ibdev,
Matan Barak2528e332015-06-11 16:35:25 +0300427 struct ib_device_attr *props,
428 struct ib_udata *uhw)
Roland Dreier225c7b12007-05-08 18:00:38 -0700429{
430 struct mlx4_ib_dev *dev = to_mdev(ibdev);
431 struct ib_smp *in_mad = NULL;
432 struct ib_smp *out_mad = NULL;
433 int err = -ENOMEM;
Jack Morgenstein3dec4872014-09-11 14:11:19 +0300434 int have_ib_ports;
Matan Barak4b664c42015-06-11 16:35:27 +0300435 struct mlx4_uverbs_ex_query_device cmd;
436 struct mlx4_uverbs_ex_query_device_resp resp = {.comp_mask = 0};
437 struct mlx4_clock_params clock_params;
Roland Dreier225c7b12007-05-08 18:00:38 -0700438
Matan Barak4b664c42015-06-11 16:35:27 +0300439 if (uhw->inlen) {
440 if (uhw->inlen < sizeof(cmd))
441 return -EINVAL;
Matan Barak2528e332015-06-11 16:35:25 +0300442
Matan Barak4b664c42015-06-11 16:35:27 +0300443 err = ib_copy_from_udata(&cmd, uhw, sizeof(cmd));
444 if (err)
445 return err;
446
447 if (cmd.comp_mask)
448 return -EINVAL;
449
450 if (cmd.reserved)
451 return -EINVAL;
452 }
453
454 resp.response_length = offsetof(typeof(resp), response_length) +
455 sizeof(resp.response_length);
Roland Dreier225c7b12007-05-08 18:00:38 -0700456 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
457 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
458 if (!in_mad || !out_mad)
459 goto out;
460
461 init_query_mad(in_mad);
462 in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
463
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000464 err = mlx4_MAD_IFC(to_mdev(ibdev), MLX4_MAD_IFC_IGNORE_KEYS,
465 1, NULL, NULL, in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -0700466 if (err)
467 goto out;
468
469 memset(props, 0, sizeof *props);
470
Jack Morgenstein3dec4872014-09-11 14:11:19 +0300471 have_ib_ports = num_ib_ports(dev->dev);
472
Roland Dreier225c7b12007-05-08 18:00:38 -0700473 props->fw_ver = dev->dev->caps.fw_ver;
474 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
475 IB_DEVICE_PORT_ACTIVE_EVENT |
476 IB_DEVICE_SYS_IMAGE_GUID |
Ron Livne521e5752008-07-14 23:48:48 -0700477 IB_DEVICE_RC_RNR_NAK_GEN |
478 IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Roland Dreier225c7b12007-05-08 18:00:38 -0700479 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_PKEY_CNTR)
480 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
481 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_QKEY_CNTR)
482 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
Jack Morgenstein3dec4872014-09-11 14:11:19 +0300483 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_APM && have_ib_ports)
Roland Dreier225c7b12007-05-08 18:00:38 -0700484 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
485 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UD_AV_PORT)
486 props->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
Eli Cohen8ff095e2008-04-16 21:01:10 -0700487 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_IPOIB_CSUM)
488 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
Markus Stockhausen50e2ec92014-08-13 14:07:30 +0000489 if (dev->dev->caps.max_gso_sz &&
490 (dev->dev->rev_id != MLX4_IB_CARD_REV_A0) &&
491 (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BLH))
Eli Cohenb832be12008-04-16 21:09:27 -0700492 props->device_cap_flags |= IB_DEVICE_UD_TSO;
Roland Dreier95d04f02008-07-23 08:12:26 -0700493 if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_RESERVED_LKEY)
494 props->device_cap_flags |= IB_DEVICE_LOCAL_DMA_LKEY;
495 if ((dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_LOCAL_INV) &&
496 (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_REMOTE_INV) &&
497 (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_FAST_REG_WR))
498 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Sean Hefty0a1405d2011-06-02 11:32:15 -0700499 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC)
500 props->device_cap_flags |= IB_DEVICE_XRC;
Shani Michaelib4253882013-02-06 16:19:16 +0000501 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_MEM_WINDOW)
502 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW;
503 if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_TYPE_2_WIN) {
504 if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_WIN_TYPE_2B)
505 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW_TYPE_2B;
506 else
507 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW_TYPE_2A;
508 }
Bart Van Asscheca920f52016-06-03 07:58:32 -0700509 if (dev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED)
510 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
Roland Dreier225c7b12007-05-08 18:00:38 -0700511
Bodong Wang070b3992015-09-22 23:18:11 +0300512 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
513
Roland Dreier225c7b12007-05-08 18:00:38 -0700514 props->vendor_id = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
515 0xffffff;
Yishai Hadas872bf2f2015-01-25 16:59:35 +0200516 props->vendor_part_id = dev->dev->persist->pdev->device;
Roland Dreier225c7b12007-05-08 18:00:38 -0700517 props->hw_ver = be32_to_cpup((__be32 *) (out_mad->data + 32));
518 memcpy(&props->sys_image_guid, out_mad->data + 4, 8);
519
520 props->max_mr_size = ~0ull;
521 props->page_size_cap = dev->dev->caps.page_size_cap;
Jack Morgenstein5a0d0a62013-11-03 10:03:23 +0200522 props->max_qp = dev->dev->quotas.qp;
Sagi Grimbergfc2d0042012-05-24 16:08:08 +0300523 props->max_qp_wr = dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE;
Roland Dreier225c7b12007-05-08 18:00:38 -0700524 props->max_sge = min(dev->dev->caps.max_sq_sg,
525 dev->dev->caps.max_rq_sg);
Sagi Grimberga5e14ba2015-10-28 13:28:15 +0200526 props->max_sge_rd = MLX4_MAX_SGE_RD;
Jack Morgenstein5a0d0a62013-11-03 10:03:23 +0200527 props->max_cq = dev->dev->quotas.cq;
Roland Dreier225c7b12007-05-08 18:00:38 -0700528 props->max_cqe = dev->dev->caps.max_cqes;
Jack Morgenstein5a0d0a62013-11-03 10:03:23 +0200529 props->max_mr = dev->dev->quotas.mpt;
Roland Dreier225c7b12007-05-08 18:00:38 -0700530 props->max_pd = dev->dev->caps.num_pds - dev->dev->caps.reserved_pds;
531 props->max_qp_rd_atom = dev->dev->caps.max_qp_dest_rdma;
532 props->max_qp_init_rd_atom = dev->dev->caps.max_qp_init_rdma;
533 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Jack Morgenstein5a0d0a62013-11-03 10:03:23 +0200534 props->max_srq = dev->dev->quotas.srq;
Jack Morgensteinc8681f12007-06-21 13:39:10 -0700535 props->max_srq_wr = dev->dev->caps.max_srq_wqes - 1;
Roland Dreier225c7b12007-05-08 18:00:38 -0700536 props->max_srq_sge = dev->dev->caps.max_srq_sge;
Eli Cohen5a0fd092010-10-07 16:24:16 +0200537 props->max_fast_reg_page_list_len = MLX4_MAX_FAST_REG_PAGES;
Roland Dreier225c7b12007-05-08 18:00:38 -0700538 props->local_ca_ack_delay = dev->dev->caps.local_ca_ack_delay;
539 props->atomic_cap = dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_ATOMIC ?
540 IB_ATOMIC_HCA : IB_ATOMIC_NONE;
Dotan Barak47e956b2012-07-11 15:39:29 +0000541 props->masked_atomic_cap = props->atomic_cap;
Roland Dreier5ae2a7a2007-06-18 08:15:02 -0700542 props->max_pkeys = dev->dev->caps.pkey_table_len[1];
Roland Dreier225c7b12007-05-08 18:00:38 -0700543 props->max_mcast_grp = dev->dev->caps.num_mgms + dev->dev->caps.num_amgms;
544 props->max_mcast_qp_attach = dev->dev->caps.num_qp_per_mgm;
545 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
546 props->max_mcast_grp;
Eli Cohena5bbe892012-02-09 18:10:06 +0200547 props->max_map_per_fmr = dev->dev->caps.max_fmr_maps;
Matan Barak4b664c42015-06-11 16:35:27 +0300548 props->hca_core_clock = dev->dev->caps.hca_core_clock * 1000UL;
549 props->timestamp_mask = 0xFFFFFFFFFFFFULL;
Maor Gottlieb731e0412016-11-10 11:30:58 +0200550 props->max_ah = INT_MAX;
Roland Dreier225c7b12007-05-08 18:00:38 -0700551
Matan Barak8a7ff142015-07-01 14:31:02 +0300552 if (!mlx4_is_slave(dev->dev))
553 err = mlx4_get_internal_clock_params(dev->dev, &clock_params);
Matan Barak4b664c42015-06-11 16:35:27 +0300554
555 if (uhw->outlen >= resp.response_length + sizeof(resp.hca_core_clock_offset)) {
Matan Barak4b664c42015-06-11 16:35:27 +0300556 resp.response_length += sizeof(resp.hca_core_clock_offset);
Matan Barak8a7ff142015-07-01 14:31:02 +0300557 if (!err && !mlx4_is_slave(dev->dev)) {
558 resp.comp_mask |= QUERY_DEVICE_RESP_MASK_TIMESTAMP;
559 resp.hca_core_clock_offset = clock_params.offset % PAGE_SIZE;
560 }
Matan Barak4b664c42015-06-11 16:35:27 +0300561 }
562
563 if (uhw->outlen) {
564 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
565 if (err)
566 goto out;
567 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700568out:
569 kfree(in_mad);
570 kfree(out_mad);
571
572 return err;
573}
574
Eli Cohenfa417f72010-10-24 21:08:52 -0700575static enum rdma_link_layer
576mlx4_ib_port_link_layer(struct ib_device *device, u8 port_num)
577{
578 struct mlx4_dev *dev = to_mdev(device)->dev;
579
Jack Morgenstein65dab252011-12-13 04:10:41 +0000580 return dev->caps.port_mask[port_num] == MLX4_PORT_TYPE_IB ?
Eli Cohenfa417f72010-10-24 21:08:52 -0700581 IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
582}
583
584static int ib_link_query_port(struct ib_device *ibdev, u8 port,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000585 struct ib_port_attr *props, int netw_view)
Eli Cohenfa417f72010-10-24 21:08:52 -0700586{
Or Gerlitza9c766b2012-01-11 19:00:29 +0200587 struct ib_smp *in_mad = NULL;
588 struct ib_smp *out_mad = NULL;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300589 int ext_active_speed;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000590 int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
Or Gerlitza9c766b2012-01-11 19:00:29 +0200591 int err = -ENOMEM;
592
593 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
594 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
595 if (!in_mad || !out_mad)
596 goto out;
597
598 init_query_mad(in_mad);
599 in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
600 in_mad->attr_mod = cpu_to_be32(port);
601
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000602 if (mlx4_is_mfunc(to_mdev(ibdev)->dev) && netw_view)
603 mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
604
605 err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port, NULL, NULL,
Or Gerlitza9c766b2012-01-11 19:00:29 +0200606 in_mad, out_mad);
607 if (err)
608 goto out;
609
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300610
Eli Cohenfa417f72010-10-24 21:08:52 -0700611 props->lid = be16_to_cpup((__be16 *) (out_mad->data + 16));
612 props->lmc = out_mad->data[34] & 0x7;
613 props->sm_lid = be16_to_cpup((__be16 *) (out_mad->data + 18));
614 props->sm_sl = out_mad->data[36] & 0xf;
615 props->state = out_mad->data[32] & 0xf;
616 props->phys_state = out_mad->data[33] >> 4;
617 props->port_cap_flags = be32_to_cpup((__be32 *) (out_mad->data + 20));
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000618 if (netw_view)
619 props->gid_tbl_len = out_mad->data[50];
620 else
621 props->gid_tbl_len = to_mdev(ibdev)->dev->caps.gid_table_len[port];
Eli Cohenfa417f72010-10-24 21:08:52 -0700622 props->max_msg_sz = to_mdev(ibdev)->dev->caps.max_msg_sz;
623 props->pkey_tbl_len = to_mdev(ibdev)->dev->caps.pkey_table_len[port];
624 props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46));
625 props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));
626 props->active_width = out_mad->data[31] & 0xf;
627 props->active_speed = out_mad->data[35] >> 4;
628 props->max_mtu = out_mad->data[41] & 0xf;
629 props->active_mtu = out_mad->data[36] >> 4;
630 props->subnet_timeout = out_mad->data[51] & 0x1f;
631 props->max_vl_num = out_mad->data[37] >> 4;
632 props->init_type_reply = out_mad->data[41] >> 4;
633
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300634 /* Check if extended speeds (EDR/FDR/...) are supported */
635 if (props->port_cap_flags & IB_PORT_EXTENDED_SPEEDS_SUP) {
636 ext_active_speed = out_mad->data[62] >> 4;
637
638 switch (ext_active_speed) {
639 case 1:
Or Gerlitz2e966912012-02-28 18:49:50 +0200640 props->active_speed = IB_SPEED_FDR;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300641 break;
642 case 2:
Or Gerlitz2e966912012-02-28 18:49:50 +0200643 props->active_speed = IB_SPEED_EDR;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300644 break;
645 }
646 }
647
648 /* If reported active speed is QDR, check if is FDR-10 */
Or Gerlitz2e966912012-02-28 18:49:50 +0200649 if (props->active_speed == IB_SPEED_QDR) {
Or Gerlitz8154c072012-03-06 15:50:50 +0200650 init_query_mad(in_mad);
651 in_mad->attr_id = MLX4_ATTR_EXTENDED_PORT_INFO;
652 in_mad->attr_mod = cpu_to_be32(port);
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300653
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000654 err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port,
Or Gerlitz8154c072012-03-06 15:50:50 +0200655 NULL, NULL, in_mad, out_mad);
656 if (err)
Jesper Juhlbf6b47d2012-04-11 23:43:29 +0200657 goto out;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300658
Or Gerlitz8154c072012-03-06 15:50:50 +0200659 /* Checking LinkSpeedActive for FDR-10 */
660 if (out_mad->data[15] & 0x1)
661 props->active_speed = IB_SPEED_FDR10;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300662 }
Or Gerlitzd2ef4062012-04-02 17:45:20 +0300663
664 /* Avoid wrong speed value returned by FW if the IB link is down. */
665 if (props->state == IB_PORT_DOWN)
666 props->active_speed = IB_SPEED_SDR;
667
Or Gerlitza9c766b2012-01-11 19:00:29 +0200668out:
669 kfree(in_mad);
670 kfree(out_mad);
671 return err;
Eli Cohenfa417f72010-10-24 21:08:52 -0700672}
673
674static u8 state_to_phys_state(enum ib_port_state state)
675{
676 return state == IB_PORT_ACTIVE ? 5 : 3;
677}
678
679static int eth_link_query_port(struct ib_device *ibdev, u8 port,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000680 struct ib_port_attr *props, int netw_view)
Eli Cohenfa417f72010-10-24 21:08:52 -0700681{
Or Gerlitza9c766b2012-01-11 19:00:29 +0200682
683 struct mlx4_ib_dev *mdev = to_mdev(ibdev);
684 struct mlx4_ib_iboe *iboe = &mdev->iboe;
Eli Cohenfa417f72010-10-24 21:08:52 -0700685 struct net_device *ndev;
686 enum ib_mtu tmp;
Or Gerlitza9c766b2012-01-11 19:00:29 +0200687 struct mlx4_cmd_mailbox *mailbox;
688 int err = 0;
Moni Shouaa5750092015-02-03 16:48:37 +0200689 int is_bonded = mlx4_is_bonded(mdev->dev);
Eli Cohenfa417f72010-10-24 21:08:52 -0700690
Or Gerlitza9c766b2012-01-11 19:00:29 +0200691 mailbox = mlx4_alloc_cmd_mailbox(mdev->dev);
692 if (IS_ERR(mailbox))
693 return PTR_ERR(mailbox);
694
695 err = mlx4_cmd_box(mdev->dev, 0, mailbox->dma, port, 0,
696 MLX4_CMD_QUERY_PORT, MLX4_CMD_TIME_CLASS_B,
697 MLX4_CMD_WRAPPED);
698 if (err)
699 goto out;
700
Saeed Mahameed6fa26202016-11-10 11:30:59 +0200701 props->active_width = (((u8 *)mailbox->buf)[5] == 0x40) ||
702 (((u8 *)mailbox->buf)[5] == 0x20 /*56Gb*/) ?
703 IB_WIDTH_4X : IB_WIDTH_1X;
704 props->active_speed = (((u8 *)mailbox->buf)[5] == 0x20 /*56Gb*/) ?
705 IB_SPEED_FDR : IB_SPEED_QDR;
Moni Shouab4a26a22014-02-09 11:54:34 +0200706 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_IP_BASED_GIDS;
Or Gerlitza9c766b2012-01-11 19:00:29 +0200707 props->gid_tbl_len = mdev->dev->caps.gid_table_len[port];
708 props->max_msg_sz = mdev->dev->caps.max_msg_sz;
Eli Cohenfa417f72010-10-24 21:08:52 -0700709 props->pkey_tbl_len = 1;
Or Gerlitzbcacb892011-10-10 10:53:41 +0200710 props->max_mtu = IB_MTU_4096;
Or Gerlitza9c766b2012-01-11 19:00:29 +0200711 props->max_vl_num = 2;
Eli Cohenfa417f72010-10-24 21:08:52 -0700712 props->state = IB_PORT_DOWN;
713 props->phys_state = state_to_phys_state(props->state);
714 props->active_mtu = IB_MTU_256;
Jack Morgensteindba3ad22014-08-21 14:28:41 +0300715 spin_lock_bh(&iboe->lock);
Eli Cohenfa417f72010-10-24 21:08:52 -0700716 ndev = iboe->netdevs[port - 1];
Moni Shoua5070cd22015-07-30 18:33:30 +0300717 if (ndev && is_bonded) {
718 rcu_read_lock(); /* required to get upper dev */
719 ndev = netdev_master_upper_dev_get_rcu(ndev);
720 rcu_read_unlock();
721 }
Eli Cohenfa417f72010-10-24 21:08:52 -0700722 if (!ndev)
Or Gerlitza9c766b2012-01-11 19:00:29 +0200723 goto out_unlock;
Eli Cohenfa417f72010-10-24 21:08:52 -0700724
725 tmp = iboe_get_mtu(ndev->mtu);
726 props->active_mtu = tmp ? min(props->max_mtu, tmp) : IB_MTU_256;
727
Eli Cohen21d606092010-11-11 21:05:58 +0000728 props->state = (netif_running(ndev) && netif_carrier_ok(ndev)) ?
Eli Cohenfa417f72010-10-24 21:08:52 -0700729 IB_PORT_ACTIVE : IB_PORT_DOWN;
730 props->phys_state = state_to_phys_state(props->state);
Or Gerlitza9c766b2012-01-11 19:00:29 +0200731out_unlock:
Jack Morgensteindba3ad22014-08-21 14:28:41 +0300732 spin_unlock_bh(&iboe->lock);
Or Gerlitza9c766b2012-01-11 19:00:29 +0200733out:
734 mlx4_free_cmd_mailbox(mdev->dev, mailbox);
735 return err;
Eli Cohenfa417f72010-10-24 21:08:52 -0700736}
737
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000738int __mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
739 struct ib_port_attr *props, int netw_view)
Roland Dreier225c7b12007-05-08 18:00:38 -0700740{
Or Gerlitza9c766b2012-01-11 19:00:29 +0200741 int err;
Roland Dreier225c7b12007-05-08 18:00:38 -0700742
743 memset(props, 0, sizeof *props);
744
Eli Cohenfa417f72010-10-24 21:08:52 -0700745 err = mlx4_ib_port_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND ?
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000746 ib_link_query_port(ibdev, port, props, netw_view) :
747 eth_link_query_port(ibdev, port, props, netw_view);
Roland Dreier225c7b12007-05-08 18:00:38 -0700748
749 return err;
750}
751
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000752static int mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
753 struct ib_port_attr *props)
754{
755 /* returns host view */
756 return __mlx4_ib_query_port(ibdev, port, props, 0);
757}
758
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000759int __mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
760 union ib_gid *gid, int netw_view)
Roland Dreier225c7b12007-05-08 18:00:38 -0700761{
762 struct ib_smp *in_mad = NULL;
763 struct ib_smp *out_mad = NULL;
764 int err = -ENOMEM;
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000765 struct mlx4_ib_dev *dev = to_mdev(ibdev);
766 int clear = 0;
767 int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
Roland Dreier225c7b12007-05-08 18:00:38 -0700768
769 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
770 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
771 if (!in_mad || !out_mad)
772 goto out;
773
774 init_query_mad(in_mad);
775 in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
776 in_mad->attr_mod = cpu_to_be32(port);
777
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000778 if (mlx4_is_mfunc(dev->dev) && netw_view)
779 mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
780
781 err = mlx4_MAD_IFC(dev, mad_ifc_flags, port, NULL, NULL, in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -0700782 if (err)
783 goto out;
784
785 memcpy(gid->raw, out_mad->data + 8, 8);
786
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000787 if (mlx4_is_mfunc(dev->dev) && !netw_view) {
788 if (index) {
789 /* For any index > 0, return the null guid */
790 err = 0;
791 clear = 1;
792 goto out;
793 }
794 }
795
Roland Dreier225c7b12007-05-08 18:00:38 -0700796 init_query_mad(in_mad);
797 in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
798 in_mad->attr_mod = cpu_to_be32(index / 8);
799
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000800 err = mlx4_MAD_IFC(dev, mad_ifc_flags, port,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000801 NULL, NULL, in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -0700802 if (err)
803 goto out;
804
805 memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
806
807out:
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000808 if (clear)
809 memset(gid->raw + 8, 0, 8);
Roland Dreier225c7b12007-05-08 18:00:38 -0700810 kfree(in_mad);
811 kfree(out_mad);
812 return err;
813}
814
Eli Cohenfa417f72010-10-24 21:08:52 -0700815static int mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
816 union ib_gid *gid)
817{
Moni Shoua5070cd22015-07-30 18:33:30 +0300818 int ret;
819
820 if (rdma_protocol_ib(ibdev, port))
Jack Morgensteina0c64a12012-08-03 08:40:49 +0000821 return __mlx4_ib_query_gid(ibdev, port, index, gid, 0);
Moni Shoua5070cd22015-07-30 18:33:30 +0300822
823 if (!rdma_protocol_roce(ibdev, port))
824 return -ENODEV;
825
826 if (!rdma_cap_roce_gid_table(ibdev, port))
827 return -ENODEV;
828
Matan Barak55ee3ab2015-10-15 18:38:45 +0300829 ret = ib_get_cached_gid(ibdev, port, index, gid, NULL);
Moni Shoua5070cd22015-07-30 18:33:30 +0300830 if (ret == -EAGAIN) {
831 memcpy(gid, &zgid, sizeof(*gid));
832 return 0;
833 }
834
835 return ret;
Eli Cohenfa417f72010-10-24 21:08:52 -0700836}
837
Jack Morgensteinfd10ed82016-09-12 19:16:21 +0300838static int mlx4_ib_query_sl2vl(struct ib_device *ibdev, u8 port, u64 *sl2vl_tbl)
839{
840 union sl2vl_tbl_to_u64 sl2vl64;
841 struct ib_smp *in_mad = NULL;
842 struct ib_smp *out_mad = NULL;
843 int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
844 int err = -ENOMEM;
845 int jj;
846
847 if (mlx4_is_slave(to_mdev(ibdev)->dev)) {
848 *sl2vl_tbl = 0;
849 return 0;
850 }
851
852 in_mad = kzalloc(sizeof(*in_mad), GFP_KERNEL);
853 out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
854 if (!in_mad || !out_mad)
855 goto out;
856
857 init_query_mad(in_mad);
858 in_mad->attr_id = IB_SMP_ATTR_SL_TO_VL_TABLE;
859 in_mad->attr_mod = 0;
860
861 if (mlx4_is_mfunc(to_mdev(ibdev)->dev))
862 mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
863
864 err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port, NULL, NULL,
865 in_mad, out_mad);
866 if (err)
867 goto out;
868
869 for (jj = 0; jj < 8; jj++)
870 sl2vl64.sl8[jj] = ((struct ib_smp *)out_mad)->data[jj];
871 *sl2vl_tbl = sl2vl64.sl64;
872
873out:
874 kfree(in_mad);
875 kfree(out_mad);
876 return err;
877}
878
879static void mlx4_init_sl2vl_tbl(struct mlx4_ib_dev *mdev)
880{
881 u64 sl2vl;
882 int i;
883 int err;
884
885 for (i = 1; i <= mdev->dev->caps.num_ports; i++) {
886 if (mdev->dev->caps.port_type[i] == MLX4_PORT_TYPE_ETH)
887 continue;
888 err = mlx4_ib_query_sl2vl(&mdev->ib_dev, i, &sl2vl);
889 if (err) {
890 pr_err("Unable to get default sl to vl mapping for port %d. Using all zeroes (%d)\n",
891 i, err);
892 sl2vl = 0;
893 }
894 atomic64_set(&mdev->sl2vl[i - 1], sl2vl);
895 }
896}
897
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000898int __mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
899 u16 *pkey, int netw_view)
Roland Dreier225c7b12007-05-08 18:00:38 -0700900{
901 struct ib_smp *in_mad = NULL;
902 struct ib_smp *out_mad = NULL;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000903 int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
Roland Dreier225c7b12007-05-08 18:00:38 -0700904 int err = -ENOMEM;
905
906 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
907 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
908 if (!in_mad || !out_mad)
909 goto out;
910
911 init_query_mad(in_mad);
912 in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
913 in_mad->attr_mod = cpu_to_be32(index / 32);
914
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000915 if (mlx4_is_mfunc(to_mdev(ibdev)->dev) && netw_view)
916 mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
917
918 err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port, NULL, NULL,
919 in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -0700920 if (err)
921 goto out;
922
923 *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
924
925out:
926 kfree(in_mad);
927 kfree(out_mad);
928 return err;
929}
930
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000931static int mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
932{
933 return __mlx4_ib_query_pkey(ibdev, port, index, pkey, 0);
934}
935
Roland Dreier225c7b12007-05-08 18:00:38 -0700936static int mlx4_ib_modify_device(struct ib_device *ibdev, int mask,
937 struct ib_device_modify *props)
938{
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000939 struct mlx4_cmd_mailbox *mailbox;
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000940 unsigned long flags;
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000941
Roland Dreier225c7b12007-05-08 18:00:38 -0700942 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
943 return -EOPNOTSUPP;
944
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000945 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
946 return 0;
947
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +0000948 if (mlx4_is_slave(to_mdev(ibdev)->dev))
949 return -EOPNOTSUPP;
950
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000951 spin_lock_irqsave(&to_mdev(ibdev)->sm_lock, flags);
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700952 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000953 spin_unlock_irqrestore(&to_mdev(ibdev)->sm_lock, flags);
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000954
955 /*
956 * If possible, pass node desc to FW, so it can generate
957 * a 144 trap. If cmd fails, just ignore.
958 */
959 mailbox = mlx4_alloc_cmd_mailbox(to_mdev(ibdev)->dev);
960 if (IS_ERR(mailbox))
961 return 0;
962
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700963 memcpy(mailbox->buf, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000964 mlx4_cmd(to_mdev(ibdev)->dev, mailbox->dma, 1, 0,
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +0000965 MLX4_CMD_SET_NODE, MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000966
967 mlx4_free_cmd_mailbox(to_mdev(ibdev)->dev, mailbox);
Roland Dreier225c7b12007-05-08 18:00:38 -0700968
969 return 0;
970}
971
Jack Morgenstein61565012014-05-29 16:31:01 +0300972static int mlx4_ib_SET_PORT(struct mlx4_ib_dev *dev, u8 port, int reset_qkey_viols,
973 u32 cap_mask)
Roland Dreier225c7b12007-05-08 18:00:38 -0700974{
975 struct mlx4_cmd_mailbox *mailbox;
976 int err;
977
978 mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
979 if (IS_ERR(mailbox))
980 return PTR_ERR(mailbox);
981
Roland Dreier5ae2a7a2007-06-18 08:15:02 -0700982 if (dev->dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
983 *(u8 *) mailbox->buf = !!reset_qkey_viols << 6;
984 ((__be32 *) mailbox->buf)[2] = cpu_to_be32(cap_mask);
985 } else {
986 ((u8 *) mailbox->buf)[3] = !!reset_qkey_viols;
987 ((__be32 *) mailbox->buf)[1] = cpu_to_be32(cap_mask);
988 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700989
Ido Shamaya130b592015-04-02 16:31:19 +0300990 err = mlx4_cmd(dev->dev, mailbox->dma, port, MLX4_SET_PORT_IB_OPCODE,
991 MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
992 MLX4_CMD_WRAPPED);
Roland Dreier225c7b12007-05-08 18:00:38 -0700993
994 mlx4_free_cmd_mailbox(dev->dev, mailbox);
995 return err;
996}
997
998static int mlx4_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
999 struct ib_port_modify *props)
1000{
Jack Morgenstein61565012014-05-29 16:31:01 +03001001 struct mlx4_ib_dev *mdev = to_mdev(ibdev);
1002 u8 is_eth = mdev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
Roland Dreier225c7b12007-05-08 18:00:38 -07001003 struct ib_port_attr attr;
1004 u32 cap_mask;
1005 int err;
1006
Jack Morgenstein61565012014-05-29 16:31:01 +03001007 /* return OK if this is RoCE. CM calls ib_modify_port() regardless
1008 * of whether port link layer is ETH or IB. For ETH ports, qkey
1009 * violations and port capabilities are not meaningful.
1010 */
1011 if (is_eth)
1012 return 0;
1013
1014 mutex_lock(&mdev->cap_mask_mutex);
Roland Dreier225c7b12007-05-08 18:00:38 -07001015
1016 err = mlx4_ib_query_port(ibdev, port, &attr);
1017 if (err)
1018 goto out;
1019
1020 cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
1021 ~props->clr_port_cap_mask;
1022
Jack Morgenstein61565012014-05-29 16:31:01 +03001023 err = mlx4_ib_SET_PORT(mdev, port,
1024 !!(mask & IB_PORT_RESET_QKEY_CNTR),
1025 cap_mask);
Roland Dreier225c7b12007-05-08 18:00:38 -07001026
1027out:
1028 mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
1029 return err;
1030}
1031
1032static struct ib_ucontext *mlx4_ib_alloc_ucontext(struct ib_device *ibdev,
1033 struct ib_udata *udata)
1034{
1035 struct mlx4_ib_dev *dev = to_mdev(ibdev);
1036 struct mlx4_ib_ucontext *context;
Or Gerlitz08ff3232012-10-21 14:59:24 +00001037 struct mlx4_ib_alloc_ucontext_resp_v3 resp_v3;
Roland Dreier225c7b12007-05-08 18:00:38 -07001038 struct mlx4_ib_alloc_ucontext_resp resp;
1039 int err;
1040
Jack Morgenstein3b4a8cd2009-09-05 20:24:50 -07001041 if (!dev->ib_active)
1042 return ERR_PTR(-EAGAIN);
1043
Or Gerlitz08ff3232012-10-21 14:59:24 +00001044 if (ibdev->uverbs_abi_ver == MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION) {
1045 resp_v3.qp_tab_size = dev->dev->caps.num_qps;
1046 resp_v3.bf_reg_size = dev->dev->caps.bf_reg_size;
1047 resp_v3.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
1048 } else {
1049 resp.dev_caps = dev->dev->caps.userspace_caps;
1050 resp.qp_tab_size = dev->dev->caps.num_qps;
1051 resp.bf_reg_size = dev->dev->caps.bf_reg_size;
1052 resp.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
1053 resp.cqe_size = dev->dev->caps.cqe_size;
1054 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001055
Yishai Hadasae184dd2015-08-13 18:32:06 +03001056 context = kzalloc(sizeof(*context), GFP_KERNEL);
Roland Dreier225c7b12007-05-08 18:00:38 -07001057 if (!context)
1058 return ERR_PTR(-ENOMEM);
1059
1060 err = mlx4_uar_alloc(to_mdev(ibdev)->dev, &context->uar);
1061 if (err) {
1062 kfree(context);
1063 return ERR_PTR(err);
1064 }
1065
1066 INIT_LIST_HEAD(&context->db_page_list);
1067 mutex_init(&context->db_page_mutex);
1068
Or Gerlitz08ff3232012-10-21 14:59:24 +00001069 if (ibdev->uverbs_abi_ver == MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION)
1070 err = ib_copy_to_udata(udata, &resp_v3, sizeof(resp_v3));
1071 else
1072 err = ib_copy_to_udata(udata, &resp, sizeof(resp));
1073
Roland Dreier225c7b12007-05-08 18:00:38 -07001074 if (err) {
1075 mlx4_uar_free(to_mdev(ibdev)->dev, &context->uar);
1076 kfree(context);
1077 return ERR_PTR(-EFAULT);
1078 }
1079
1080 return &context->ibucontext;
1081}
1082
1083static int mlx4_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1084{
1085 struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
1086
1087 mlx4_uar_free(to_mdev(ibcontext->device)->dev, &context->uar);
1088 kfree(context);
1089
1090 return 0;
1091}
1092
Yishai Hadasae184dd2015-08-13 18:32:06 +03001093static void mlx4_ib_vma_open(struct vm_area_struct *area)
1094{
1095 /* vma_open is called when a new VMA is created on top of our VMA.
1096 * This is done through either mremap flow or split_vma (usually due
1097 * to mlock, madvise, munmap, etc.). We do not support a clone of the
1098 * vma, as this VMA is strongly hardware related. Therefore we set the
1099 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1100 * calling us again and trying to do incorrect actions. We assume that
1101 * the original vma size is exactly a single page that there will be no
1102 * "splitting" operations on.
1103 */
1104 area->vm_ops = NULL;
1105}
1106
1107static void mlx4_ib_vma_close(struct vm_area_struct *area)
1108{
1109 struct mlx4_ib_vma_private_data *mlx4_ib_vma_priv_data;
1110
1111 /* It's guaranteed that all VMAs opened on a FD are closed before the
1112 * file itself is closed, therefore no sync is needed with the regular
1113 * closing flow. (e.g. mlx4_ib_dealloc_ucontext) However need a sync
1114 * with accessing the vma as part of mlx4_ib_disassociate_ucontext.
1115 * The close operation is usually called under mm->mmap_sem except when
1116 * process is exiting. The exiting case is handled explicitly as part
1117 * of mlx4_ib_disassociate_ucontext.
1118 */
1119 mlx4_ib_vma_priv_data = (struct mlx4_ib_vma_private_data *)
1120 area->vm_private_data;
1121
1122 /* set the vma context pointer to null in the mlx4_ib driver's private
1123 * data to protect against a race condition in mlx4_ib_dissassociate_ucontext().
1124 */
1125 mlx4_ib_vma_priv_data->vma = NULL;
1126}
1127
1128static const struct vm_operations_struct mlx4_ib_vm_ops = {
1129 .open = mlx4_ib_vma_open,
1130 .close = mlx4_ib_vma_close
1131};
1132
1133static void mlx4_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1134{
1135 int i;
1136 int ret = 0;
1137 struct vm_area_struct *vma;
1138 struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
1139 struct task_struct *owning_process = NULL;
1140 struct mm_struct *owning_mm = NULL;
1141
1142 owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
1143 if (!owning_process)
1144 return;
1145
1146 owning_mm = get_task_mm(owning_process);
1147 if (!owning_mm) {
1148 pr_info("no mm, disassociate ucontext is pending task termination\n");
1149 while (1) {
1150 /* make sure that task is dead before returning, it may
1151 * prevent a rare case of module down in parallel to a
1152 * call to mlx4_ib_vma_close.
1153 */
1154 put_task_struct(owning_process);
1155 msleep(1);
1156 owning_process = get_pid_task(ibcontext->tgid,
1157 PIDTYPE_PID);
1158 if (!owning_process ||
1159 owning_process->state == TASK_DEAD) {
1160 pr_info("disassociate ucontext done, task was terminated\n");
1161 /* in case task was dead need to release the task struct */
1162 if (owning_process)
1163 put_task_struct(owning_process);
1164 return;
1165 }
1166 }
1167 }
1168
1169 /* need to protect from a race on closing the vma as part of
1170 * mlx4_ib_vma_close().
1171 */
1172 down_read(&owning_mm->mmap_sem);
1173 for (i = 0; i < HW_BAR_COUNT; i++) {
1174 vma = context->hw_bar_info[i].vma;
1175 if (!vma)
1176 continue;
1177
1178 ret = zap_vma_ptes(context->hw_bar_info[i].vma,
1179 context->hw_bar_info[i].vma->vm_start,
1180 PAGE_SIZE);
1181 if (ret) {
1182 pr_err("Error: zap_vma_ptes failed for index=%d, ret=%d\n", i, ret);
1183 BUG_ON(1);
1184 }
1185
1186 /* context going to be destroyed, should not access ops any more */
1187 context->hw_bar_info[i].vma->vm_ops = NULL;
1188 }
1189
1190 up_read(&owning_mm->mmap_sem);
1191 mmput(owning_mm);
1192 put_task_struct(owning_process);
1193}
1194
1195static void mlx4_ib_set_vma_data(struct vm_area_struct *vma,
1196 struct mlx4_ib_vma_private_data *vma_private_data)
1197{
1198 vma_private_data->vma = vma;
1199 vma->vm_private_data = vma_private_data;
1200 vma->vm_ops = &mlx4_ib_vm_ops;
1201}
1202
Roland Dreier225c7b12007-05-08 18:00:38 -07001203static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
1204{
1205 struct mlx4_ib_dev *dev = to_mdev(context->device);
Yishai Hadasae184dd2015-08-13 18:32:06 +03001206 struct mlx4_ib_ucontext *mucontext = to_mucontext(context);
Roland Dreier225c7b12007-05-08 18:00:38 -07001207
1208 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1209 return -EINVAL;
1210
1211 if (vma->vm_pgoff == 0) {
Yishai Hadasae184dd2015-08-13 18:32:06 +03001212 /* We prevent double mmaping on same context */
1213 if (mucontext->hw_bar_info[HW_BAR_DB].vma)
1214 return -EINVAL;
1215
Roland Dreier225c7b12007-05-08 18:00:38 -07001216 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1217
1218 if (io_remap_pfn_range(vma, vma->vm_start,
1219 to_mucontext(context)->uar.pfn,
1220 PAGE_SIZE, vma->vm_page_prot))
1221 return -EAGAIN;
Yishai Hadasae184dd2015-08-13 18:32:06 +03001222
1223 mlx4_ib_set_vma_data(vma, &mucontext->hw_bar_info[HW_BAR_DB]);
1224
Roland Dreier225c7b12007-05-08 18:00:38 -07001225 } else if (vma->vm_pgoff == 1 && dev->dev->caps.bf_reg_size != 0) {
Yishai Hadasae184dd2015-08-13 18:32:06 +03001226 /* We prevent double mmaping on same context */
1227 if (mucontext->hw_bar_info[HW_BAR_BF].vma)
1228 return -EINVAL;
1229
Roland Dreiere1d60ec2009-03-30 08:31:05 -07001230 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
Roland Dreier225c7b12007-05-08 18:00:38 -07001231
1232 if (io_remap_pfn_range(vma, vma->vm_start,
1233 to_mucontext(context)->uar.pfn +
1234 dev->dev->caps.num_uars,
1235 PAGE_SIZE, vma->vm_page_prot))
1236 return -EAGAIN;
Yishai Hadasae184dd2015-08-13 18:32:06 +03001237
1238 mlx4_ib_set_vma_data(vma, &mucontext->hw_bar_info[HW_BAR_BF]);
1239
Matan Barak52033cf2015-06-11 16:35:26 +03001240 } else if (vma->vm_pgoff == 3) {
1241 struct mlx4_clock_params params;
Yishai Hadasae184dd2015-08-13 18:32:06 +03001242 int ret;
1243
1244 /* We prevent double mmaping on same context */
1245 if (mucontext->hw_bar_info[HW_BAR_CLOCK].vma)
1246 return -EINVAL;
1247
1248 ret = mlx4_get_internal_clock_params(dev->dev, &params);
Matan Barak52033cf2015-06-11 16:35:26 +03001249
1250 if (ret)
1251 return ret;
1252
1253 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1254 if (io_remap_pfn_range(vma, vma->vm_start,
1255 (pci_resource_start(dev->dev->persist->pdev,
1256 params.bar) +
1257 params.offset)
1258 >> PAGE_SHIFT,
1259 PAGE_SIZE, vma->vm_page_prot))
1260 return -EAGAIN;
Yishai Hadasae184dd2015-08-13 18:32:06 +03001261
1262 mlx4_ib_set_vma_data(vma,
1263 &mucontext->hw_bar_info[HW_BAR_CLOCK]);
Matan Barak52033cf2015-06-11 16:35:26 +03001264 } else {
Roland Dreier225c7b12007-05-08 18:00:38 -07001265 return -EINVAL;
Matan Barak52033cf2015-06-11 16:35:26 +03001266 }
Roland Dreier225c7b12007-05-08 18:00:38 -07001267
1268 return 0;
1269}
1270
1271static struct ib_pd *mlx4_ib_alloc_pd(struct ib_device *ibdev,
1272 struct ib_ucontext *context,
1273 struct ib_udata *udata)
1274{
1275 struct mlx4_ib_pd *pd;
1276 int err;
1277
1278 pd = kmalloc(sizeof *pd, GFP_KERNEL);
1279 if (!pd)
1280 return ERR_PTR(-ENOMEM);
1281
1282 err = mlx4_pd_alloc(to_mdev(ibdev)->dev, &pd->pdn);
1283 if (err) {
1284 kfree(pd);
1285 return ERR_PTR(err);
1286 }
1287
1288 if (context)
1289 if (ib_copy_to_udata(udata, &pd->pdn, sizeof (__u32))) {
1290 mlx4_pd_free(to_mdev(ibdev)->dev, pd->pdn);
1291 kfree(pd);
1292 return ERR_PTR(-EFAULT);
1293 }
1294
1295 return &pd->ibpd;
1296}
1297
1298static int mlx4_ib_dealloc_pd(struct ib_pd *pd)
1299{
1300 mlx4_pd_free(to_mdev(pd->device)->dev, to_mpd(pd)->pdn);
1301 kfree(pd);
1302
1303 return 0;
1304}
1305
Sean Hefty012a8ff2011-06-02 09:01:33 -07001306static struct ib_xrcd *mlx4_ib_alloc_xrcd(struct ib_device *ibdev,
1307 struct ib_ucontext *context,
1308 struct ib_udata *udata)
1309{
1310 struct mlx4_ib_xrcd *xrcd;
Matan Barak8e372102015-06-11 16:35:21 +03001311 struct ib_cq_init_attr cq_attr = {};
Sean Hefty012a8ff2011-06-02 09:01:33 -07001312 int err;
1313
1314 if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC))
1315 return ERR_PTR(-ENOSYS);
1316
1317 xrcd = kmalloc(sizeof *xrcd, GFP_KERNEL);
1318 if (!xrcd)
1319 return ERR_PTR(-ENOMEM);
1320
1321 err = mlx4_xrcd_alloc(to_mdev(ibdev)->dev, &xrcd->xrcdn);
1322 if (err)
1323 goto err1;
1324
Christoph Hellwiged082d32016-09-05 12:56:17 +02001325 xrcd->pd = ib_alloc_pd(ibdev, 0);
Sean Hefty012a8ff2011-06-02 09:01:33 -07001326 if (IS_ERR(xrcd->pd)) {
1327 err = PTR_ERR(xrcd->pd);
1328 goto err2;
1329 }
1330
Matan Barak8e372102015-06-11 16:35:21 +03001331 cq_attr.cqe = 1;
1332 xrcd->cq = ib_create_cq(ibdev, NULL, NULL, xrcd, &cq_attr);
Sean Hefty012a8ff2011-06-02 09:01:33 -07001333 if (IS_ERR(xrcd->cq)) {
1334 err = PTR_ERR(xrcd->cq);
1335 goto err3;
1336 }
1337
1338 return &xrcd->ibxrcd;
1339
1340err3:
1341 ib_dealloc_pd(xrcd->pd);
1342err2:
1343 mlx4_xrcd_free(to_mdev(ibdev)->dev, xrcd->xrcdn);
1344err1:
1345 kfree(xrcd);
1346 return ERR_PTR(err);
1347}
1348
1349static int mlx4_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
1350{
1351 ib_destroy_cq(to_mxrcd(xrcd)->cq);
1352 ib_dealloc_pd(to_mxrcd(xrcd)->pd);
1353 mlx4_xrcd_free(to_mdev(xrcd->device)->dev, to_mxrcd(xrcd)->xrcdn);
1354 kfree(xrcd);
1355
1356 return 0;
1357}
1358
Eli Cohenfa417f72010-10-24 21:08:52 -07001359static int add_gid_entry(struct ib_qp *ibqp, union ib_gid *gid)
1360{
1361 struct mlx4_ib_qp *mqp = to_mqp(ibqp);
1362 struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
1363 struct mlx4_ib_gid_entry *ge;
1364
1365 ge = kzalloc(sizeof *ge, GFP_KERNEL);
1366 if (!ge)
1367 return -ENOMEM;
1368
1369 ge->gid = *gid;
1370 if (mlx4_ib_add_mc(mdev, mqp, gid)) {
1371 ge->port = mqp->port;
1372 ge->added = 1;
1373 }
1374
1375 mutex_lock(&mqp->mutex);
1376 list_add_tail(&ge->list, &mqp->gid_list);
1377 mutex_unlock(&mqp->mutex);
1378
1379 return 0;
1380}
1381
Eran Ben Elisha3ba8e312015-10-15 14:44:40 +03001382static void mlx4_ib_delete_counters_table(struct mlx4_ib_dev *ibdev,
1383 struct mlx4_ib_counters *ctr_table)
1384{
1385 struct counter_index *counter, *tmp_count;
1386
1387 mutex_lock(&ctr_table->mutex);
1388 list_for_each_entry_safe(counter, tmp_count, &ctr_table->counters_list,
1389 list) {
1390 if (counter->allocated)
1391 mlx4_counter_free(ibdev->dev, counter->index);
1392 list_del(&counter->list);
1393 kfree(counter);
1394 }
1395 mutex_unlock(&ctr_table->mutex);
1396}
1397
Eli Cohenfa417f72010-10-24 21:08:52 -07001398int mlx4_ib_add_mc(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
1399 union ib_gid *gid)
1400{
Eli Cohenfa417f72010-10-24 21:08:52 -07001401 struct net_device *ndev;
1402 int ret = 0;
1403
1404 if (!mqp->port)
1405 return 0;
1406
Jack Morgensteindba3ad22014-08-21 14:28:41 +03001407 spin_lock_bh(&mdev->iboe.lock);
Eli Cohenfa417f72010-10-24 21:08:52 -07001408 ndev = mdev->iboe.netdevs[mqp->port - 1];
1409 if (ndev)
1410 dev_hold(ndev);
Jack Morgensteindba3ad22014-08-21 14:28:41 +03001411 spin_unlock_bh(&mdev->iboe.lock);
Eli Cohenfa417f72010-10-24 21:08:52 -07001412
1413 if (ndev) {
Eli Cohenfa417f72010-10-24 21:08:52 -07001414 ret = 1;
Eli Cohenfa417f72010-10-24 21:08:52 -07001415 dev_put(ndev);
1416 }
1417
1418 return ret;
1419}
1420
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001421struct mlx4_ib_steering {
1422 struct list_head list;
Moni Shoua146d6e12015-02-03 16:48:38 +02001423 struct mlx4_flow_reg_id reg_id;
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001424 union ib_gid gid;
1425};
1426
Maor Gottlieb1f02a092016-08-30 16:58:30 +03001427#define LAST_ETH_FIELD vlan_tag
1428#define LAST_IB_FIELD sl
1429#define LAST_IPV4_FIELD dst_ip
1430#define LAST_TCP_UDP_FIELD src_port
1431
1432/* Field is the last supported field */
1433#define FIELDS_NOT_SUPPORTED(filter, field)\
1434 memchr_inv((void *)&filter.field +\
1435 sizeof(filter.field), 0,\
1436 sizeof(filter) -\
1437 offsetof(typeof(filter), field) -\
1438 sizeof(filter.field))
1439
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001440static int parse_flow_attr(struct mlx4_dev *dev,
Matan Baraka37a1a42013-11-07 15:25:16 +02001441 u32 qp_num,
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001442 union ib_flow_spec *ib_spec,
1443 struct _rule_hw *mlx4_spec)
1444{
1445 enum mlx4_net_trans_rule_id type;
1446
1447 switch (ib_spec->type) {
1448 case IB_FLOW_SPEC_ETH:
Maor Gottlieb1f02a092016-08-30 16:58:30 +03001449 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
1450 return -ENOTSUPP;
1451
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001452 type = MLX4_NET_TRANS_RULE_ID_ETH;
1453 memcpy(mlx4_spec->eth.dst_mac, ib_spec->eth.val.dst_mac,
1454 ETH_ALEN);
1455 memcpy(mlx4_spec->eth.dst_mac_msk, ib_spec->eth.mask.dst_mac,
1456 ETH_ALEN);
1457 mlx4_spec->eth.vlan_tag = ib_spec->eth.val.vlan_tag;
1458 mlx4_spec->eth.vlan_tag_msk = ib_spec->eth.mask.vlan_tag;
1459 break;
Matan Baraka37a1a42013-11-07 15:25:16 +02001460 case IB_FLOW_SPEC_IB:
Maor Gottlieb1f02a092016-08-30 16:58:30 +03001461 if (FIELDS_NOT_SUPPORTED(ib_spec->ib.mask, LAST_IB_FIELD))
1462 return -ENOTSUPP;
1463
Matan Baraka37a1a42013-11-07 15:25:16 +02001464 type = MLX4_NET_TRANS_RULE_ID_IB;
1465 mlx4_spec->ib.l3_qpn =
1466 cpu_to_be32(qp_num);
1467 mlx4_spec->ib.qpn_mask =
1468 cpu_to_be32(MLX4_IB_FLOW_QPN_MASK);
1469 break;
1470
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001471
1472 case IB_FLOW_SPEC_IPV4:
Maor Gottlieb1f02a092016-08-30 16:58:30 +03001473 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
1474 return -ENOTSUPP;
1475
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001476 type = MLX4_NET_TRANS_RULE_ID_IPV4;
1477 mlx4_spec->ipv4.src_ip = ib_spec->ipv4.val.src_ip;
1478 mlx4_spec->ipv4.src_ip_msk = ib_spec->ipv4.mask.src_ip;
1479 mlx4_spec->ipv4.dst_ip = ib_spec->ipv4.val.dst_ip;
1480 mlx4_spec->ipv4.dst_ip_msk = ib_spec->ipv4.mask.dst_ip;
1481 break;
1482
1483 case IB_FLOW_SPEC_TCP:
1484 case IB_FLOW_SPEC_UDP:
Maor Gottlieb1f02a092016-08-30 16:58:30 +03001485 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask, LAST_TCP_UDP_FIELD))
1486 return -ENOTSUPP;
1487
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001488 type = ib_spec->type == IB_FLOW_SPEC_TCP ?
1489 MLX4_NET_TRANS_RULE_ID_TCP :
1490 MLX4_NET_TRANS_RULE_ID_UDP;
1491 mlx4_spec->tcp_udp.dst_port = ib_spec->tcp_udp.val.dst_port;
1492 mlx4_spec->tcp_udp.dst_port_msk = ib_spec->tcp_udp.mask.dst_port;
1493 mlx4_spec->tcp_udp.src_port = ib_spec->tcp_udp.val.src_port;
1494 mlx4_spec->tcp_udp.src_port_msk = ib_spec->tcp_udp.mask.src_port;
1495 break;
1496
1497 default:
1498 return -EINVAL;
1499 }
1500 if (mlx4_map_sw_to_hw_steering_id(dev, type) < 0 ||
1501 mlx4_hw_rule_sz(dev, type) < 0)
1502 return -EINVAL;
1503 mlx4_spec->id = cpu_to_be16(mlx4_map_sw_to_hw_steering_id(dev, type));
1504 mlx4_spec->size = mlx4_hw_rule_sz(dev, type) >> 2;
1505 return mlx4_hw_rule_sz(dev, type);
1506}
1507
Matan Baraka37a1a42013-11-07 15:25:16 +02001508struct default_rules {
1509 __u32 mandatory_fields[IB_FLOW_SPEC_SUPPORT_LAYERS];
1510 __u32 mandatory_not_fields[IB_FLOW_SPEC_SUPPORT_LAYERS];
1511 __u32 rules_create_list[IB_FLOW_SPEC_SUPPORT_LAYERS];
1512 __u8 link_layer;
1513};
1514static const struct default_rules default_table[] = {
1515 {
1516 .mandatory_fields = {IB_FLOW_SPEC_IPV4},
1517 .mandatory_not_fields = {IB_FLOW_SPEC_ETH},
1518 .rules_create_list = {IB_FLOW_SPEC_IB},
1519 .link_layer = IB_LINK_LAYER_INFINIBAND
1520 }
1521};
1522
1523static int __mlx4_ib_default_rules_match(struct ib_qp *qp,
1524 struct ib_flow_attr *flow_attr)
1525{
1526 int i, j, k;
1527 void *ib_flow;
1528 const struct default_rules *pdefault_rules = default_table;
1529 u8 link_layer = rdma_port_get_link_layer(qp->device, flow_attr->port);
1530
Fabian Fredericka57f23f2014-08-12 19:20:07 -04001531 for (i = 0; i < ARRAY_SIZE(default_table); i++, pdefault_rules++) {
Matan Baraka37a1a42013-11-07 15:25:16 +02001532 __u32 field_types[IB_FLOW_SPEC_SUPPORT_LAYERS];
1533 memset(&field_types, 0, sizeof(field_types));
1534
1535 if (link_layer != pdefault_rules->link_layer)
1536 continue;
1537
1538 ib_flow = flow_attr + 1;
1539 /* we assume the specs are sorted */
1540 for (j = 0, k = 0; k < IB_FLOW_SPEC_SUPPORT_LAYERS &&
1541 j < flow_attr->num_of_specs; k++) {
1542 union ib_flow_spec *current_flow =
1543 (union ib_flow_spec *)ib_flow;
1544
1545 /* same layer but different type */
1546 if (((current_flow->type & IB_FLOW_SPEC_LAYER_MASK) ==
1547 (pdefault_rules->mandatory_fields[k] &
1548 IB_FLOW_SPEC_LAYER_MASK)) &&
1549 (current_flow->type !=
1550 pdefault_rules->mandatory_fields[k]))
1551 goto out;
1552
1553 /* same layer, try match next one */
1554 if (current_flow->type ==
1555 pdefault_rules->mandatory_fields[k]) {
1556 j++;
1557 ib_flow +=
1558 ((union ib_flow_spec *)ib_flow)->size;
1559 }
1560 }
1561
1562 ib_flow = flow_attr + 1;
1563 for (j = 0; j < flow_attr->num_of_specs;
1564 j++, ib_flow += ((union ib_flow_spec *)ib_flow)->size)
1565 for (k = 0; k < IB_FLOW_SPEC_SUPPORT_LAYERS; k++)
1566 /* same layer and same type */
1567 if (((union ib_flow_spec *)ib_flow)->type ==
1568 pdefault_rules->mandatory_not_fields[k])
1569 goto out;
1570
1571 return i;
1572 }
1573out:
1574 return -1;
1575}
1576
1577static int __mlx4_ib_create_default_rules(
1578 struct mlx4_ib_dev *mdev,
1579 struct ib_qp *qp,
1580 const struct default_rules *pdefault_rules,
1581 struct _rule_hw *mlx4_spec) {
1582 int size = 0;
1583 int i;
1584
Fabian Fredericka57f23f2014-08-12 19:20:07 -04001585 for (i = 0; i < ARRAY_SIZE(pdefault_rules->rules_create_list); i++) {
Matan Baraka37a1a42013-11-07 15:25:16 +02001586 int ret;
1587 union ib_flow_spec ib_spec;
1588 switch (pdefault_rules->rules_create_list[i]) {
1589 case 0:
1590 /* no rule */
1591 continue;
1592 case IB_FLOW_SPEC_IB:
1593 ib_spec.type = IB_FLOW_SPEC_IB;
1594 ib_spec.size = sizeof(struct ib_flow_spec_ib);
1595
1596 break;
1597 default:
1598 /* invalid rule */
1599 return -EINVAL;
1600 }
1601 /* We must put empty rule, qpn is being ignored */
1602 ret = parse_flow_attr(mdev->dev, 0, &ib_spec,
1603 mlx4_spec);
1604 if (ret < 0) {
1605 pr_info("invalid parsing\n");
1606 return -EINVAL;
1607 }
1608
1609 mlx4_spec = (void *)mlx4_spec + ret;
1610 size += ret;
1611 }
1612 return size;
1613}
1614
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001615static int __mlx4_ib_create_flow(struct ib_qp *qp, struct ib_flow_attr *flow_attr,
1616 int domain,
1617 enum mlx4_net_trans_promisc_mode flow_type,
1618 u64 *reg_id)
1619{
1620 int ret, i;
1621 int size = 0;
1622 void *ib_flow;
1623 struct mlx4_ib_dev *mdev = to_mdev(qp->device);
1624 struct mlx4_cmd_mailbox *mailbox;
1625 struct mlx4_net_trans_rule_hw_ctrl *ctrl;
Matan Baraka37a1a42013-11-07 15:25:16 +02001626 int default_flow;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001627
1628 static const u16 __mlx4_domain[] = {
1629 [IB_FLOW_DOMAIN_USER] = MLX4_DOMAIN_UVERBS,
1630 [IB_FLOW_DOMAIN_ETHTOOL] = MLX4_DOMAIN_ETHTOOL,
1631 [IB_FLOW_DOMAIN_RFS] = MLX4_DOMAIN_RFS,
1632 [IB_FLOW_DOMAIN_NIC] = MLX4_DOMAIN_NIC,
1633 };
1634
1635 if (flow_attr->priority > MLX4_IB_FLOW_MAX_PRIO) {
1636 pr_err("Invalid priority value %d\n", flow_attr->priority);
1637 return -EINVAL;
1638 }
1639
1640 if (domain >= IB_FLOW_DOMAIN_NUM) {
1641 pr_err("Invalid domain value %d\n", domain);
1642 return -EINVAL;
1643 }
1644
1645 if (mlx4_map_sw_to_hw_steering_mode(mdev->dev, flow_type) < 0)
1646 return -EINVAL;
1647
1648 mailbox = mlx4_alloc_cmd_mailbox(mdev->dev);
1649 if (IS_ERR(mailbox))
1650 return PTR_ERR(mailbox);
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001651 ctrl = mailbox->buf;
1652
1653 ctrl->prio = cpu_to_be16(__mlx4_domain[domain] |
1654 flow_attr->priority);
1655 ctrl->type = mlx4_map_sw_to_hw_steering_mode(mdev->dev, flow_type);
1656 ctrl->port = flow_attr->port;
1657 ctrl->qpn = cpu_to_be32(qp->qp_num);
1658
1659 ib_flow = flow_attr + 1;
1660 size += sizeof(struct mlx4_net_trans_rule_hw_ctrl);
Matan Baraka37a1a42013-11-07 15:25:16 +02001661 /* Add default flows */
1662 default_flow = __mlx4_ib_default_rules_match(qp, flow_attr);
1663 if (default_flow >= 0) {
1664 ret = __mlx4_ib_create_default_rules(
1665 mdev, qp, default_table + default_flow,
1666 mailbox->buf + size);
1667 if (ret < 0) {
1668 mlx4_free_cmd_mailbox(mdev->dev, mailbox);
1669 return -EINVAL;
1670 }
1671 size += ret;
1672 }
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001673 for (i = 0; i < flow_attr->num_of_specs; i++) {
Matan Baraka37a1a42013-11-07 15:25:16 +02001674 ret = parse_flow_attr(mdev->dev, qp->qp_num, ib_flow,
1675 mailbox->buf + size);
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001676 if (ret < 0) {
1677 mlx4_free_cmd_mailbox(mdev->dev, mailbox);
1678 return -EINVAL;
1679 }
1680 ib_flow += ((union ib_flow_spec *) ib_flow)->size;
1681 size += ret;
1682 }
1683
1684 ret = mlx4_cmd_imm(mdev->dev, mailbox->dma, reg_id, size >> 2, 0,
1685 MLX4_QP_FLOW_STEERING_ATTACH, MLX4_CMD_TIME_CLASS_A,
Matan Barak48564132015-05-31 09:30:15 +03001686 MLX4_CMD_WRAPPED);
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001687 if (ret == -ENOMEM)
1688 pr_err("mcg table is full. Fail to register network rule.\n");
1689 else if (ret == -ENXIO)
1690 pr_err("Device managed flow steering is disabled. Fail to register network rule.\n");
1691 else if (ret)
Colin Ian King35fc7b72016-04-25 20:26:50 +01001692 pr_err("Invalid argument. Fail to register network rule.\n");
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001693
1694 mlx4_free_cmd_mailbox(mdev->dev, mailbox);
1695 return ret;
1696}
1697
1698static int __mlx4_ib_destroy_flow(struct mlx4_dev *dev, u64 reg_id)
1699{
1700 int err;
1701 err = mlx4_cmd(dev, reg_id, 0, 0,
1702 MLX4_QP_FLOW_STEERING_DETACH, MLX4_CMD_TIME_CLASS_A,
Matan Barak48564132015-05-31 09:30:15 +03001703 MLX4_CMD_WRAPPED);
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001704 if (err)
1705 pr_err("Fail to detach network rule. registration id = 0x%llx\n",
1706 reg_id);
1707 return err;
1708}
1709
Or Gerlitzd2fce8a2014-08-27 16:47:49 +03001710static int mlx4_ib_tunnel_steer_add(struct ib_qp *qp, struct ib_flow_attr *flow_attr,
1711 u64 *reg_id)
1712{
1713 void *ib_flow;
1714 union ib_flow_spec *ib_spec;
1715 struct mlx4_dev *dev = to_mdev(qp->device)->dev;
1716 int err = 0;
1717
Or Gerlitz5eff6da2015-01-15 15:28:54 +02001718 if (dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN ||
1719 dev->caps.dmfs_high_steer_mode == MLX4_STEERING_DMFS_A0_STATIC)
Or Gerlitzd2fce8a2014-08-27 16:47:49 +03001720 return 0; /* do nothing */
1721
1722 ib_flow = flow_attr + 1;
1723 ib_spec = (union ib_flow_spec *)ib_flow;
1724
1725 if (ib_spec->type != IB_FLOW_SPEC_ETH || flow_attr->num_of_specs != 1)
1726 return 0; /* do nothing */
1727
1728 err = mlx4_tunnel_steer_add(to_mdev(qp->device)->dev, ib_spec->eth.val.dst_mac,
1729 flow_attr->port, qp->qp_num,
1730 MLX4_DOMAIN_UVERBS | (flow_attr->priority & 0xff),
1731 reg_id);
1732 return err;
1733}
1734
Marina Varshaver0e451e82016-02-18 18:31:06 +02001735static int mlx4_ib_add_dont_trap_rule(struct mlx4_dev *dev,
1736 struct ib_flow_attr *flow_attr,
1737 enum mlx4_net_trans_promisc_mode *type)
1738{
1739 int err = 0;
1740
1741 if (!(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_DMFS_UC_MC_SNIFFER) ||
1742 (dev->caps.dmfs_high_steer_mode == MLX4_STEERING_DMFS_A0_STATIC) ||
1743 (flow_attr->num_of_specs > 1) || (flow_attr->priority != 0)) {
1744 return -EOPNOTSUPP;
1745 }
1746
1747 if (flow_attr->num_of_specs == 0) {
1748 type[0] = MLX4_FS_MC_SNIFFER;
1749 type[1] = MLX4_FS_UC_SNIFFER;
1750 } else {
1751 union ib_flow_spec *ib_spec;
1752
1753 ib_spec = (union ib_flow_spec *)(flow_attr + 1);
1754 if (ib_spec->type != IB_FLOW_SPEC_ETH)
1755 return -EINVAL;
1756
1757 /* if all is zero than MC and UC */
1758 if (is_zero_ether_addr(ib_spec->eth.mask.dst_mac)) {
1759 type[0] = MLX4_FS_MC_SNIFFER;
1760 type[1] = MLX4_FS_UC_SNIFFER;
1761 } else {
1762 u8 mac[ETH_ALEN] = {ib_spec->eth.mask.dst_mac[0] ^ 0x01,
1763 ib_spec->eth.mask.dst_mac[1],
1764 ib_spec->eth.mask.dst_mac[2],
1765 ib_spec->eth.mask.dst_mac[3],
1766 ib_spec->eth.mask.dst_mac[4],
1767 ib_spec->eth.mask.dst_mac[5]};
1768
1769 /* Above xor was only on MC bit, non empty mask is valid
1770 * only if this bit is set and rest are zero.
1771 */
1772 if (!is_zero_ether_addr(&mac[0]))
1773 return -EINVAL;
1774
1775 if (is_multicast_ether_addr(ib_spec->eth.val.dst_mac))
1776 type[0] = MLX4_FS_MC_SNIFFER;
1777 else
1778 type[0] = MLX4_FS_UC_SNIFFER;
1779 }
1780 }
1781
1782 return err;
1783}
1784
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001785static struct ib_flow *mlx4_ib_create_flow(struct ib_qp *qp,
1786 struct ib_flow_attr *flow_attr,
1787 int domain)
1788{
Moni Shoua146d6e12015-02-03 16:48:38 +02001789 int err = 0, i = 0, j = 0;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001790 struct mlx4_ib_flow *mflow;
1791 enum mlx4_net_trans_promisc_mode type[2];
Moni Shoua146d6e12015-02-03 16:48:38 +02001792 struct mlx4_dev *dev = (to_mdev(qp->device))->dev;
1793 int is_bonded = mlx4_is_bonded(dev);
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001794
Yishai Hadas5533c182016-06-22 17:27:30 +03001795 if (flow_attr->port < 1 || flow_attr->port > qp->device->phys_port_cnt)
1796 return ERR_PTR(-EINVAL);
1797
Marina Varshaver0e451e82016-02-18 18:31:06 +02001798 if ((flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) &&
1799 (flow_attr->type != IB_FLOW_ATTR_NORMAL))
Marina Varshavera3100a72016-02-18 18:31:05 +02001800 return ERR_PTR(-EOPNOTSUPP);
1801
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001802 memset(type, 0, sizeof(type));
1803
1804 mflow = kzalloc(sizeof(*mflow), GFP_KERNEL);
1805 if (!mflow) {
1806 err = -ENOMEM;
1807 goto err_free;
1808 }
1809
1810 switch (flow_attr->type) {
1811 case IB_FLOW_ATTR_NORMAL:
Marina Varshaver0e451e82016-02-18 18:31:06 +02001812 /* If dont trap flag (continue match) is set, under specific
1813 * condition traffic be replicated to given qp,
1814 * without stealing it
1815 */
1816 if (unlikely(flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)) {
1817 err = mlx4_ib_add_dont_trap_rule(dev,
1818 flow_attr,
1819 type);
1820 if (err)
1821 goto err_free;
1822 } else {
1823 type[0] = MLX4_FS_REGULAR;
1824 }
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001825 break;
1826
1827 case IB_FLOW_ATTR_ALL_DEFAULT:
1828 type[0] = MLX4_FS_ALL_DEFAULT;
1829 break;
1830
1831 case IB_FLOW_ATTR_MC_DEFAULT:
1832 type[0] = MLX4_FS_MC_DEFAULT;
1833 break;
1834
1835 case IB_FLOW_ATTR_SNIFFER:
Marina Varshaver0e451e82016-02-18 18:31:06 +02001836 type[0] = MLX4_FS_MIRROR_RX_PORT;
1837 type[1] = MLX4_FS_MIRROR_SX_PORT;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001838 break;
1839
1840 default:
1841 err = -EINVAL;
1842 goto err_free;
1843 }
1844
1845 while (i < ARRAY_SIZE(type) && type[i]) {
1846 err = __mlx4_ib_create_flow(qp, flow_attr, domain, type[i],
Moni Shoua146d6e12015-02-03 16:48:38 +02001847 &mflow->reg_id[i].id);
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001848 if (err)
Or Gerlitz571e1b22014-10-30 15:59:28 +02001849 goto err_create_flow;
Moni Shoua146d6e12015-02-03 16:48:38 +02001850 if (is_bonded) {
Moni Shoua824c25c2015-02-08 11:49:33 +02001851 /* Application always sees one port so the mirror rule
1852 * must be on port #2
1853 */
Moni Shoua146d6e12015-02-03 16:48:38 +02001854 flow_attr->port = 2;
1855 err = __mlx4_ib_create_flow(qp, flow_attr,
1856 domain, type[j],
1857 &mflow->reg_id[j].mirror);
1858 flow_attr->port = 1;
1859 if (err)
1860 goto err_create_flow;
1861 j++;
1862 }
1863
Roland Dreier11562562015-05-29 23:11:27 -07001864 i++;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001865 }
1866
Or Gerlitzd2fce8a2014-08-27 16:47:49 +03001867 if (i < ARRAY_SIZE(type) && flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Moni Shoua146d6e12015-02-03 16:48:38 +02001868 err = mlx4_ib_tunnel_steer_add(qp, flow_attr,
1869 &mflow->reg_id[i].id);
Or Gerlitzd2fce8a2014-08-27 16:47:49 +03001870 if (err)
Or Gerlitz571e1b22014-10-30 15:59:28 +02001871 goto err_create_flow;
Roland Dreier11562562015-05-29 23:11:27 -07001872
Moni Shoua146d6e12015-02-03 16:48:38 +02001873 if (is_bonded) {
1874 flow_attr->port = 2;
1875 err = mlx4_ib_tunnel_steer_add(qp, flow_attr,
1876 &mflow->reg_id[j].mirror);
1877 flow_attr->port = 1;
1878 if (err)
1879 goto err_create_flow;
1880 j++;
1881 }
1882 /* function to create mirror rule */
Roland Dreier11562562015-05-29 23:11:27 -07001883 i++;
Or Gerlitzd2fce8a2014-08-27 16:47:49 +03001884 }
1885
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001886 return &mflow->ibflow;
1887
Or Gerlitz571e1b22014-10-30 15:59:28 +02001888err_create_flow:
1889 while (i) {
Moni Shoua146d6e12015-02-03 16:48:38 +02001890 (void)__mlx4_ib_destroy_flow(to_mdev(qp->device)->dev,
1891 mflow->reg_id[i].id);
Or Gerlitz571e1b22014-10-30 15:59:28 +02001892 i--;
1893 }
Moni Shoua146d6e12015-02-03 16:48:38 +02001894
1895 while (j) {
1896 (void)__mlx4_ib_destroy_flow(to_mdev(qp->device)->dev,
1897 mflow->reg_id[j].mirror);
1898 j--;
1899 }
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001900err_free:
1901 kfree(mflow);
1902 return ERR_PTR(err);
1903}
1904
1905static int mlx4_ib_destroy_flow(struct ib_flow *flow_id)
1906{
1907 int err, ret = 0;
1908 int i = 0;
1909 struct mlx4_ib_dev *mdev = to_mdev(flow_id->qp->device);
1910 struct mlx4_ib_flow *mflow = to_mflow(flow_id);
1911
Moni Shoua146d6e12015-02-03 16:48:38 +02001912 while (i < ARRAY_SIZE(mflow->reg_id) && mflow->reg_id[i].id) {
1913 err = __mlx4_ib_destroy_flow(mdev->dev, mflow->reg_id[i].id);
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001914 if (err)
1915 ret = err;
Moni Shoua146d6e12015-02-03 16:48:38 +02001916 if (mflow->reg_id[i].mirror) {
1917 err = __mlx4_ib_destroy_flow(mdev->dev,
1918 mflow->reg_id[i].mirror);
1919 if (err)
1920 ret = err;
1921 }
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03001922 i++;
1923 }
1924
1925 kfree(mflow);
1926 return ret;
1927}
1928
Roland Dreier225c7b12007-05-08 18:00:38 -07001929static int mlx4_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
1930{
Eli Cohenfa417f72010-10-24 21:08:52 -07001931 int err;
1932 struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
Moni Shoua146d6e12015-02-03 16:48:38 +02001933 struct mlx4_dev *dev = mdev->dev;
Eli Cohenfa417f72010-10-24 21:08:52 -07001934 struct mlx4_ib_qp *mqp = to_mqp(ibqp);
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001935 struct mlx4_ib_steering *ib_steering = NULL;
Or Gerlitze9a7faf2014-12-17 16:17:34 +02001936 enum mlx4_protocol prot = MLX4_PROT_IB_IPV6;
Moni Shoua146d6e12015-02-03 16:48:38 +02001937 struct mlx4_flow_reg_id reg_id;
Eli Cohenfa417f72010-10-24 21:08:52 -07001938
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001939 if (mdev->dev->caps.steering_mode ==
1940 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1941 ib_steering = kmalloc(sizeof(*ib_steering), GFP_KERNEL);
1942 if (!ib_steering)
1943 return -ENOMEM;
1944 }
1945
1946 err = mlx4_multicast_attach(mdev->dev, &mqp->mqp, gid->raw, mqp->port,
1947 !!(mqp->flags &
1948 MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK),
Moni Shoua146d6e12015-02-03 16:48:38 +02001949 prot, &reg_id.id);
Or Gerlitze9a7faf2014-12-17 16:17:34 +02001950 if (err) {
1951 pr_err("multicast attach op failed, err %d\n", err);
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001952 goto err_malloc;
Or Gerlitze9a7faf2014-12-17 16:17:34 +02001953 }
Eli Cohenfa417f72010-10-24 21:08:52 -07001954
Moni Shoua146d6e12015-02-03 16:48:38 +02001955 reg_id.mirror = 0;
1956 if (mlx4_is_bonded(dev)) {
Moni Shoua824c25c2015-02-08 11:49:33 +02001957 err = mlx4_multicast_attach(mdev->dev, &mqp->mqp, gid->raw,
1958 (mqp->port == 1) ? 2 : 1,
Moni Shoua146d6e12015-02-03 16:48:38 +02001959 !!(mqp->flags &
1960 MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK),
1961 prot, &reg_id.mirror);
1962 if (err)
1963 goto err_add;
1964 }
1965
Eli Cohenfa417f72010-10-24 21:08:52 -07001966 err = add_gid_entry(ibqp, gid);
1967 if (err)
1968 goto err_add;
1969
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001970 if (ib_steering) {
1971 memcpy(ib_steering->gid.raw, gid->raw, 16);
1972 ib_steering->reg_id = reg_id;
1973 mutex_lock(&mqp->mutex);
1974 list_add(&ib_steering->list, &mqp->steering_rules);
1975 mutex_unlock(&mqp->mutex);
1976 }
Eli Cohenfa417f72010-10-24 21:08:52 -07001977 return 0;
1978
1979err_add:
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001980 mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
Moni Shoua146d6e12015-02-03 16:48:38 +02001981 prot, reg_id.id);
1982 if (reg_id.mirror)
1983 mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
1984 prot, reg_id.mirror);
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00001985err_malloc:
1986 kfree(ib_steering);
1987
Eli Cohenfa417f72010-10-24 21:08:52 -07001988 return err;
1989}
1990
1991static struct mlx4_ib_gid_entry *find_gid_entry(struct mlx4_ib_qp *qp, u8 *raw)
1992{
1993 struct mlx4_ib_gid_entry *ge;
1994 struct mlx4_ib_gid_entry *tmp;
1995 struct mlx4_ib_gid_entry *ret = NULL;
1996
1997 list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
1998 if (!memcmp(raw, ge->gid.raw, 16)) {
1999 ret = ge;
2000 break;
2001 }
2002 }
2003
2004 return ret;
Roland Dreier225c7b12007-05-08 18:00:38 -07002005}
2006
2007static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2008{
Eli Cohenfa417f72010-10-24 21:08:52 -07002009 int err;
2010 struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
Moni Shoua146d6e12015-02-03 16:48:38 +02002011 struct mlx4_dev *dev = mdev->dev;
Eli Cohenfa417f72010-10-24 21:08:52 -07002012 struct mlx4_ib_qp *mqp = to_mqp(ibqp);
Eli Cohenfa417f72010-10-24 21:08:52 -07002013 struct net_device *ndev;
2014 struct mlx4_ib_gid_entry *ge;
Moni Shoua146d6e12015-02-03 16:48:38 +02002015 struct mlx4_flow_reg_id reg_id = {0, 0};
Or Gerlitze9a7faf2014-12-17 16:17:34 +02002016 enum mlx4_protocol prot = MLX4_PROT_IB_IPV6;
Eli Cohenfa417f72010-10-24 21:08:52 -07002017
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +00002018 if (mdev->dev->caps.steering_mode ==
2019 MLX4_STEERING_MODE_DEVICE_MANAGED) {
2020 struct mlx4_ib_steering *ib_steering;
2021
2022 mutex_lock(&mqp->mutex);
2023 list_for_each_entry(ib_steering, &mqp->steering_rules, list) {
2024 if (!memcmp(ib_steering->gid.raw, gid->raw, 16)) {
2025 list_del(&ib_steering->list);
2026 break;
2027 }
2028 }
2029 mutex_unlock(&mqp->mutex);
2030 if (&ib_steering->list == &mqp->steering_rules) {
2031 pr_err("Couldn't find reg_id for mgid. Steering rule is left attached\n");
2032 return -EINVAL;
2033 }
2034 reg_id = ib_steering->reg_id;
2035 kfree(ib_steering);
2036 }
2037
2038 err = mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
Moni Shoua146d6e12015-02-03 16:48:38 +02002039 prot, reg_id.id);
Eli Cohenfa417f72010-10-24 21:08:52 -07002040 if (err)
2041 return err;
2042
Moni Shoua146d6e12015-02-03 16:48:38 +02002043 if (mlx4_is_bonded(dev)) {
2044 err = mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
2045 prot, reg_id.mirror);
2046 if (err)
2047 return err;
2048 }
2049
Eli Cohenfa417f72010-10-24 21:08:52 -07002050 mutex_lock(&mqp->mutex);
2051 ge = find_gid_entry(mqp, gid->raw);
2052 if (ge) {
Jack Morgensteindba3ad22014-08-21 14:28:41 +03002053 spin_lock_bh(&mdev->iboe.lock);
Eli Cohenfa417f72010-10-24 21:08:52 -07002054 ndev = ge->added ? mdev->iboe.netdevs[ge->port - 1] : NULL;
2055 if (ndev)
2056 dev_hold(ndev);
Jack Morgensteindba3ad22014-08-21 14:28:41 +03002057 spin_unlock_bh(&mdev->iboe.lock);
Moni Shouad487ee72013-12-12 18:03:13 +02002058 if (ndev)
Eli Cohenfa417f72010-10-24 21:08:52 -07002059 dev_put(ndev);
Eli Cohenfa417f72010-10-24 21:08:52 -07002060 list_del(&ge->list);
2061 kfree(ge);
2062 } else
Shlomo Pongratz987c8f82012-04-29 17:04:26 +03002063 pr_warn("could not find mgid entry\n");
Eli Cohenfa417f72010-10-24 21:08:52 -07002064
2065 mutex_unlock(&mqp->mutex);
2066
2067 return 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07002068}
2069
2070static int init_node_data(struct mlx4_ib_dev *dev)
2071{
2072 struct ib_smp *in_mad = NULL;
2073 struct ib_smp *out_mad = NULL;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00002074 int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
Roland Dreier225c7b12007-05-08 18:00:38 -07002075 int err = -ENOMEM;
2076
2077 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
2078 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
2079 if (!in_mad || !out_mad)
2080 goto out;
2081
2082 init_query_mad(in_mad);
2083 in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00002084 if (mlx4_is_master(dev->dev))
2085 mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
Roland Dreier225c7b12007-05-08 18:00:38 -07002086
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00002087 err = mlx4_MAD_IFC(dev, mad_ifc_flags, 1, NULL, NULL, in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -07002088 if (err)
2089 goto out;
2090
Yuval Shaiabd99fde2016-08-25 10:57:07 -07002091 memcpy(dev->ib_dev.node_desc, out_mad->data, IB_DEVICE_NODE_DESC_MAX);
Roland Dreier225c7b12007-05-08 18:00:38 -07002092
2093 in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
2094
Jack Morgenstein0a9a0182012-08-03 08:40:45 +00002095 err = mlx4_MAD_IFC(dev, mad_ifc_flags, 1, NULL, NULL, in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -07002096 if (err)
2097 goto out;
2098
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +00002099 dev->dev->rev_id = be32_to_cpup((__be32 *) (out_mad->data + 32));
Roland Dreier225c7b12007-05-08 18:00:38 -07002100 memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
2101
2102out:
2103 kfree(in_mad);
2104 kfree(out_mad);
2105 return err;
2106}
2107
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002108static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2109 char *buf)
Jack Morgensteincd9281d2007-09-18 09:14:18 +02002110{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002111 struct mlx4_ib_dev *dev =
2112 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002113 return sprintf(buf, "MT%d\n", dev->dev->persist->pdev->device);
Jack Morgensteincd9281d2007-09-18 09:14:18 +02002114}
2115
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002116static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2117 char *buf)
Jack Morgensteincd9281d2007-09-18 09:14:18 +02002118{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002119 struct mlx4_ib_dev *dev =
2120 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
Jack Morgensteincd9281d2007-09-18 09:14:18 +02002121 return sprintf(buf, "%x\n", dev->dev->rev_id);
2122}
2123
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002124static ssize_t show_board(struct device *device, struct device_attribute *attr,
2125 char *buf)
Jack Morgensteincd9281d2007-09-18 09:14:18 +02002126{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002127 struct mlx4_ib_dev *dev =
2128 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
2129 return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN,
2130 dev->dev->board_id);
Jack Morgensteincd9281d2007-09-18 09:14:18 +02002131}
2132
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002133static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002134static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2135static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
Jack Morgensteincd9281d2007-09-18 09:14:18 +02002136
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002137static struct device_attribute *mlx4_class_attributes[] = {
2138 &dev_attr_hw_rev,
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002139 &dev_attr_hca_type,
2140 &dev_attr_board_id
Jack Morgensteincd9281d2007-09-18 09:14:18 +02002141};
2142
Mark Bloch3f85f2a2016-07-19 20:54:58 +03002143struct diag_counter {
2144 const char *name;
2145 u32 offset;
2146};
2147
2148#define DIAG_COUNTER(_name, _offset) \
2149 { .name = #_name, .offset = _offset }
2150
2151static const struct diag_counter diag_basic[] = {
2152 DIAG_COUNTER(rq_num_lle, 0x00),
2153 DIAG_COUNTER(sq_num_lle, 0x04),
2154 DIAG_COUNTER(rq_num_lqpoe, 0x08),
2155 DIAG_COUNTER(sq_num_lqpoe, 0x0C),
2156 DIAG_COUNTER(rq_num_lpe, 0x18),
2157 DIAG_COUNTER(sq_num_lpe, 0x1C),
2158 DIAG_COUNTER(rq_num_wrfe, 0x20),
2159 DIAG_COUNTER(sq_num_wrfe, 0x24),
2160 DIAG_COUNTER(sq_num_mwbe, 0x2C),
2161 DIAG_COUNTER(sq_num_bre, 0x34),
2162 DIAG_COUNTER(sq_num_rire, 0x44),
2163 DIAG_COUNTER(rq_num_rire, 0x48),
2164 DIAG_COUNTER(sq_num_rae, 0x4C),
2165 DIAG_COUNTER(rq_num_rae, 0x50),
2166 DIAG_COUNTER(sq_num_roe, 0x54),
2167 DIAG_COUNTER(sq_num_tree, 0x5C),
2168 DIAG_COUNTER(sq_num_rree, 0x64),
2169 DIAG_COUNTER(rq_num_rnr, 0x68),
2170 DIAG_COUNTER(sq_num_rnr, 0x6C),
2171 DIAG_COUNTER(rq_num_oos, 0x100),
2172 DIAG_COUNTER(sq_num_oos, 0x104),
2173};
2174
2175static const struct diag_counter diag_ext[] = {
2176 DIAG_COUNTER(rq_num_dup, 0x130),
2177 DIAG_COUNTER(sq_num_to, 0x134),
2178};
2179
2180static const struct diag_counter diag_device_only[] = {
2181 DIAG_COUNTER(num_cqovf, 0x1A0),
2182 DIAG_COUNTER(rq_num_udsdprd, 0x118),
2183};
2184
2185static struct rdma_hw_stats *mlx4_ib_alloc_hw_stats(struct ib_device *ibdev,
2186 u8 port_num)
2187{
2188 struct mlx4_ib_dev *dev = to_mdev(ibdev);
2189 struct mlx4_ib_diag_counters *diag = dev->diag_counters;
2190
2191 if (!diag[!!port_num].name)
2192 return NULL;
2193
2194 return rdma_alloc_hw_stats_struct(diag[!!port_num].name,
2195 diag[!!port_num].num_counters,
2196 RDMA_HW_STATS_DEFAULT_LIFESPAN);
2197}
2198
2199static int mlx4_ib_get_hw_stats(struct ib_device *ibdev,
2200 struct rdma_hw_stats *stats,
2201 u8 port, int index)
2202{
2203 struct mlx4_ib_dev *dev = to_mdev(ibdev);
2204 struct mlx4_ib_diag_counters *diag = dev->diag_counters;
2205 u32 hw_value[ARRAY_SIZE(diag_device_only) +
2206 ARRAY_SIZE(diag_ext) + ARRAY_SIZE(diag_basic)] = {};
2207 int ret;
2208 int i;
2209
2210 ret = mlx4_query_diag_counters(dev->dev,
2211 MLX4_OP_MOD_QUERY_TRANSPORT_CI_ERRORS,
2212 diag[!!port].offset, hw_value,
2213 diag[!!port].num_counters, port);
2214
2215 if (ret)
2216 return ret;
2217
2218 for (i = 0; i < diag[!!port].num_counters; i++)
2219 stats->value[i] = hw_value[i];
2220
2221 return diag[!!port].num_counters;
2222}
2223
2224static int __mlx4_ib_alloc_diag_counters(struct mlx4_ib_dev *ibdev,
2225 const char ***name,
2226 u32 **offset,
2227 u32 *num,
2228 bool port)
2229{
2230 u32 num_counters;
2231
2232 num_counters = ARRAY_SIZE(diag_basic);
2233
2234 if (ibdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_DIAG_PER_PORT)
2235 num_counters += ARRAY_SIZE(diag_ext);
2236
2237 if (!port)
2238 num_counters += ARRAY_SIZE(diag_device_only);
2239
2240 *name = kcalloc(num_counters, sizeof(**name), GFP_KERNEL);
2241 if (!*name)
2242 return -ENOMEM;
2243
2244 *offset = kcalloc(num_counters, sizeof(**offset), GFP_KERNEL);
2245 if (!*offset)
2246 goto err_name;
2247
2248 *num = num_counters;
2249
2250 return 0;
2251
2252err_name:
2253 kfree(*name);
2254 return -ENOMEM;
2255}
2256
2257static void mlx4_ib_fill_diag_counters(struct mlx4_ib_dev *ibdev,
2258 const char **name,
2259 u32 *offset,
2260 bool port)
2261{
2262 int i;
2263 int j;
2264
2265 for (i = 0, j = 0; i < ARRAY_SIZE(diag_basic); i++, j++) {
2266 name[i] = diag_basic[i].name;
2267 offset[i] = diag_basic[i].offset;
2268 }
2269
2270 if (ibdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_DIAG_PER_PORT) {
2271 for (i = 0; i < ARRAY_SIZE(diag_ext); i++, j++) {
2272 name[j] = diag_ext[i].name;
2273 offset[j] = diag_ext[i].offset;
2274 }
2275 }
2276
2277 if (!port) {
2278 for (i = 0; i < ARRAY_SIZE(diag_device_only); i++, j++) {
2279 name[j] = diag_device_only[i].name;
2280 offset[j] = diag_device_only[i].offset;
2281 }
2282 }
2283}
2284
2285static int mlx4_ib_alloc_diag_counters(struct mlx4_ib_dev *ibdev)
2286{
2287 struct mlx4_ib_diag_counters *diag = ibdev->diag_counters;
2288 int i;
2289 int ret;
2290 bool per_port = !!(ibdev->dev->caps.flags2 &
2291 MLX4_DEV_CAP_FLAG2_DIAG_PER_PORT);
2292
Kamal Heib69d269d382016-09-12 19:16:22 +03002293 if (mlx4_is_slave(ibdev->dev))
2294 return 0;
2295
Mark Bloch3f85f2a2016-07-19 20:54:58 +03002296 for (i = 0; i < MLX4_DIAG_COUNTERS_TYPES; i++) {
2297 /* i == 1 means we are building port counters */
2298 if (i && !per_port)
2299 continue;
2300
2301 ret = __mlx4_ib_alloc_diag_counters(ibdev, &diag[i].name,
2302 &diag[i].offset,
2303 &diag[i].num_counters, i);
2304 if (ret)
2305 goto err_alloc;
2306
2307 mlx4_ib_fill_diag_counters(ibdev, diag[i].name,
2308 diag[i].offset, i);
2309 }
2310
2311 ibdev->ib_dev.get_hw_stats = mlx4_ib_get_hw_stats;
2312 ibdev->ib_dev.alloc_hw_stats = mlx4_ib_alloc_hw_stats;
2313
2314 return 0;
2315
2316err_alloc:
2317 if (i) {
2318 kfree(diag[i - 1].name);
2319 kfree(diag[i - 1].offset);
2320 }
2321
2322 return ret;
2323}
2324
2325static void mlx4_ib_diag_cleanup(struct mlx4_ib_dev *ibdev)
2326{
2327 int i;
2328
2329 for (i = 0; i < MLX4_DIAG_COUNTERS_TYPES; i++) {
2330 kfree(ibdev->diag_counters[i].offset);
2331 kfree(ibdev->diag_counters[i].name);
2332 }
2333}
2334
Matan Barak9433c182014-05-15 15:29:28 +03002335#define MLX4_IB_INVALID_MAC ((u64)-1)
2336static void mlx4_ib_update_qps(struct mlx4_ib_dev *ibdev,
2337 struct net_device *dev,
2338 int port)
2339{
2340 u64 new_smac = 0;
2341 u64 release_mac = MLX4_IB_INVALID_MAC;
2342 struct mlx4_ib_qp *qp;
2343
2344 read_lock(&dev_base_lock);
2345 new_smac = mlx4_mac_to_u64(dev->dev_addr);
2346 read_unlock(&dev_base_lock);
2347
Jack Morgenstein3e0629c2014-09-11 14:11:17 +03002348 atomic64_set(&ibdev->iboe.mac[port - 1], new_smac);
2349
Jack Morgensteind24d9f42014-09-11 14:11:18 +03002350 /* no need for update QP1 and mac registration in non-SRIOV */
2351 if (!mlx4_is_mfunc(ibdev->dev))
2352 return;
2353
Matan Barak9433c182014-05-15 15:29:28 +03002354 mutex_lock(&ibdev->qp1_proxy_lock[port - 1]);
2355 qp = ibdev->qp1_proxy[port - 1];
2356 if (qp) {
2357 int new_smac_index;
Jack Morgenstein25476b02014-09-11 14:11:20 +03002358 u64 old_smac;
Matan Barak9433c182014-05-15 15:29:28 +03002359 struct mlx4_update_qp_params update_params;
2360
Jack Morgenstein25476b02014-09-11 14:11:20 +03002361 mutex_lock(&qp->mutex);
2362 old_smac = qp->pri.smac;
Matan Barak9433c182014-05-15 15:29:28 +03002363 if (new_smac == old_smac)
2364 goto unlock;
2365
2366 new_smac_index = mlx4_register_mac(ibdev->dev, port, new_smac);
2367
2368 if (new_smac_index < 0)
2369 goto unlock;
2370
2371 update_params.smac_index = new_smac_index;
Matan Barak09e05c32014-09-10 16:41:56 +03002372 if (mlx4_update_qp(ibdev->dev, qp->mqp.qpn, MLX4_UPDATE_QP_SMAC,
Matan Barak9433c182014-05-15 15:29:28 +03002373 &update_params)) {
2374 release_mac = new_smac;
2375 goto unlock;
2376 }
Jack Morgenstein25476b02014-09-11 14:11:20 +03002377 /* if old port was zero, no mac was yet registered for this QP */
2378 if (qp->pri.smac_port)
2379 release_mac = old_smac;
Matan Barak9433c182014-05-15 15:29:28 +03002380 qp->pri.smac = new_smac;
Jack Morgenstein25476b02014-09-11 14:11:20 +03002381 qp->pri.smac_port = port;
Matan Barak9433c182014-05-15 15:29:28 +03002382 qp->pri.smac_index = new_smac_index;
Matan Barak9433c182014-05-15 15:29:28 +03002383 }
2384
2385unlock:
Matan Barak9433c182014-05-15 15:29:28 +03002386 if (release_mac != MLX4_IB_INVALID_MAC)
2387 mlx4_unregister_mac(ibdev->dev, port, release_mac);
Jack Morgenstein25476b02014-09-11 14:11:20 +03002388 if (qp)
2389 mutex_unlock(&qp->mutex);
2390 mutex_unlock(&ibdev->qp1_proxy_lock[port - 1]);
Matan Barak9433c182014-05-15 15:29:28 +03002391}
2392
Matan Barak9433c182014-05-15 15:29:28 +03002393static void mlx4_ib_scan_netdevs(struct mlx4_ib_dev *ibdev,
2394 struct net_device *dev,
2395 unsigned long event)
2396
Moni Shouad487ee72013-12-12 18:03:13 +02002397{
2398 struct mlx4_ib_iboe *iboe;
Matan Barak9433c182014-05-15 15:29:28 +03002399 int update_qps_port = -1;
Moni Shouad487ee72013-12-12 18:03:13 +02002400 int port;
2401
Moni Shoua5070cd22015-07-30 18:33:30 +03002402 ASSERT_RTNL();
2403
Moni Shouad487ee72013-12-12 18:03:13 +02002404 iboe = &ibdev->iboe;
2405
Jack Morgensteindba3ad22014-08-21 14:28:41 +03002406 spin_lock_bh(&iboe->lock);
Moni Shouad487ee72013-12-12 18:03:13 +02002407 mlx4_foreach_ib_transport_port(port, ibdev->dev) {
Moni Shouaad4885d22014-02-05 15:13:02 +02002408
Moni Shouad487ee72013-12-12 18:03:13 +02002409 iboe->netdevs[port - 1] =
2410 mlx4_get_protocol_dev(ibdev->dev, MLX4_PROT_ETH, port);
Moni Shouad487ee72013-12-12 18:03:13 +02002411
Matan Barak9433c182014-05-15 15:29:28 +03002412 if (dev == iboe->netdevs[port - 1] &&
2413 (event == NETDEV_CHANGEADDR || event == NETDEV_REGISTER ||
2414 event == NETDEV_UP || event == NETDEV_CHANGE))
2415 update_qps_port = port;
2416
Moni Shouad487ee72013-12-12 18:03:13 +02002417 }
Jack Morgensteindba3ad22014-08-21 14:28:41 +03002418 spin_unlock_bh(&iboe->lock);
Matan Barak9433c182014-05-15 15:29:28 +03002419
2420 if (update_qps_port > 0)
2421 mlx4_ib_update_qps(ibdev, dev, update_qps_port);
Moni Shouad487ee72013-12-12 18:03:13 +02002422}
2423
2424static int mlx4_ib_netdev_event(struct notifier_block *this,
2425 unsigned long event, void *ptr)
2426{
Jiri Pirko351638e2013-05-28 01:30:21 +00002427 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eli Cohenfa417f72010-10-24 21:08:52 -07002428 struct mlx4_ib_dev *ibdev;
Eli Cohenfa417f72010-10-24 21:08:52 -07002429
2430 if (!net_eq(dev_net(dev), &init_net))
2431 return NOTIFY_DONE;
2432
2433 ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb);
Matan Barak9433c182014-05-15 15:29:28 +03002434 mlx4_ib_scan_netdevs(ibdev, dev, event);
Eli Cohenfa417f72010-10-24 21:08:52 -07002435
2436 return NOTIFY_DONE;
2437}
2438
Jack Morgenstein54679e12012-08-03 08:40:43 +00002439static void init_pkeys(struct mlx4_ib_dev *ibdev)
2440{
2441 int port;
2442 int slave;
2443 int i;
2444
2445 if (mlx4_is_master(ibdev->dev)) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002446 for (slave = 0; slave <= ibdev->dev->persist->num_vfs;
2447 ++slave) {
Jack Morgenstein54679e12012-08-03 08:40:43 +00002448 for (port = 1; port <= ibdev->dev->caps.num_ports; ++port) {
2449 for (i = 0;
2450 i < ibdev->dev->phys_caps.pkey_phys_table_len[port];
2451 ++i) {
2452 ibdev->pkeys.virt2phys_pkey[slave][port - 1][i] =
2453 /* master has the identity virt2phys pkey mapping */
2454 (slave == mlx4_master_func_num(ibdev->dev) || !i) ? i :
2455 ibdev->dev->phys_caps.pkey_phys_table_len[port] - 1;
2456 mlx4_sync_pkey_table(ibdev->dev, slave, port, i,
2457 ibdev->pkeys.virt2phys_pkey[slave][port - 1][i]);
2458 }
2459 }
2460 }
2461 /* initialize pkey cache */
2462 for (port = 1; port <= ibdev->dev->caps.num_ports; ++port) {
2463 for (i = 0;
2464 i < ibdev->dev->phys_caps.pkey_phys_table_len[port];
2465 ++i)
2466 ibdev->pkeys.phys_pkey_cache[port-1][i] =
2467 (i) ? 0 : 0xFFFF;
2468 }
2469 }
2470}
2471
Shlomo Pongratze605b742012-04-29 17:04:27 +03002472static void mlx4_ib_alloc_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev)
2473{
Matan Barakc66fa192015-05-31 09:30:16 +03002474 int i, j, eq = 0, total_eqs = 0;
Shlomo Pongratze605b742012-04-29 17:04:27 +03002475
Matan Barakc66fa192015-05-31 09:30:16 +03002476 ibdev->eq_table = kcalloc(dev->caps.num_comp_vectors,
2477 sizeof(ibdev->eq_table[0]), GFP_KERNEL);
Shlomo Pongratze605b742012-04-29 17:04:27 +03002478 if (!ibdev->eq_table)
2479 return;
2480
Matan Barakc66fa192015-05-31 09:30:16 +03002481 for (i = 1; i <= dev->caps.num_ports; i++) {
2482 for (j = 0; j < mlx4_get_eqs_per_port(dev, i);
2483 j++, total_eqs++) {
2484 if (i > 1 && mlx4_is_eq_shared(dev, total_eqs))
2485 continue;
2486 ibdev->eq_table[eq] = total_eqs;
2487 if (!mlx4_assign_eq(dev, i,
2488 &ibdev->eq_table[eq]))
2489 eq++;
2490 else
2491 ibdev->eq_table[eq] = -1;
Shlomo Pongratze605b742012-04-29 17:04:27 +03002492 }
2493 }
2494
Matan Barakc66fa192015-05-31 09:30:16 +03002495 for (i = eq; i < dev->caps.num_comp_vectors;
2496 ibdev->eq_table[i++] = -1)
2497 ;
Shlomo Pongratze605b742012-04-29 17:04:27 +03002498
2499 /* Advertise the new number of EQs to clients */
Matan Barakc66fa192015-05-31 09:30:16 +03002500 ibdev->ib_dev.num_comp_vectors = eq;
Shlomo Pongratze605b742012-04-29 17:04:27 +03002501}
2502
2503static void mlx4_ib_free_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev)
2504{
2505 int i;
Matan Barakc66fa192015-05-31 09:30:16 +03002506 int total_eqs = ibdev->ib_dev.num_comp_vectors;
Shlomo Pongratz3aac6ff2012-05-24 16:08:07 +03002507
Matan Barakc66fa192015-05-31 09:30:16 +03002508 /* no eqs were allocated */
Shlomo Pongratz3aac6ff2012-05-24 16:08:07 +03002509 if (!ibdev->eq_table)
2510 return;
Shlomo Pongratze605b742012-04-29 17:04:27 +03002511
2512 /* Reset the advertised EQ number */
Matan Barakc66fa192015-05-31 09:30:16 +03002513 ibdev->ib_dev.num_comp_vectors = 0;
Shlomo Pongratze605b742012-04-29 17:04:27 +03002514
Matan Barakc66fa192015-05-31 09:30:16 +03002515 for (i = 0; i < total_eqs; i++)
Shlomo Pongratze605b742012-04-29 17:04:27 +03002516 mlx4_release_eq(dev, ibdev->eq_table[i]);
Shlomo Pongratze605b742012-04-29 17:04:27 +03002517
Shlomo Pongratze605b742012-04-29 17:04:27 +03002518 kfree(ibdev->eq_table);
Matan Barakc66fa192015-05-31 09:30:16 +03002519 ibdev->eq_table = NULL;
Shlomo Pongratze605b742012-04-29 17:04:27 +03002520}
2521
Ira Weiny77386132015-05-13 20:02:58 -04002522static int mlx4_port_immutable(struct ib_device *ibdev, u8 port_num,
2523 struct ib_port_immutable *immutable)
2524{
2525 struct ib_port_attr attr;
Matan Barak4ed088e2016-01-14 17:50:43 +02002526 struct mlx4_ib_dev *mdev = to_mdev(ibdev);
Ira Weiny77386132015-05-13 20:02:58 -04002527 int err;
2528
2529 err = mlx4_ib_query_port(ibdev, port_num, &attr);
2530 if (err)
2531 return err;
2532
2533 immutable->pkey_tbl_len = attr.pkey_tbl_len;
2534 immutable->gid_tbl_len = attr.gid_tbl_len;
2535
Matan Barak4ed088e2016-01-14 17:50:43 +02002536 if (mlx4_ib_port_link_layer(ibdev, port_num) == IB_LINK_LAYER_INFINIBAND) {
Ira Weinyf9b22e32015-05-13 20:02:59 -04002537 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_IB;
Matan Barak4ed088e2016-01-14 17:50:43 +02002538 } else {
2539 if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE)
2540 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
2541 if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2)
2542 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE |
2543 RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
2544 }
Ira Weinyf9b22e32015-05-13 20:02:59 -04002545
Ira Weiny337877a2015-06-06 14:38:29 -04002546 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
2547
Ira Weiny77386132015-05-13 20:02:58 -04002548 return 0;
2549}
2550
Ira Weinye9db59f2016-06-15 02:22:00 -04002551static void get_fw_ver_str(struct ib_device *device, char *str,
2552 size_t str_len)
2553{
2554 struct mlx4_ib_dev *dev =
2555 container_of(device, struct mlx4_ib_dev, ib_dev);
2556 snprintf(str, str_len, "%d.%d.%d",
2557 (int) (dev->dev->caps.fw_ver >> 32),
2558 (int) (dev->dev->caps.fw_ver >> 16) & 0xffff,
2559 (int) dev->dev->caps.fw_ver & 0xffff);
2560}
2561
Roland Dreier225c7b12007-05-08 18:00:38 -07002562static void *mlx4_ib_add(struct mlx4_dev *dev)
2563{
2564 struct mlx4_ib_dev *ibdev;
Roland Dreier22e7ef92009-01-09 13:22:29 -08002565 int num_ports = 0;
Jack Morgenstein035b1032012-05-10 23:28:09 +03002566 int i, j;
Eli Cohenfa417f72010-10-24 21:08:52 -07002567 int err;
2568 struct mlx4_ib_iboe *iboe;
Matan Barak41966702014-02-02 17:06:47 +02002569 int ib_num_ports = 0;
Moni Shouaa5750092015-02-03 16:48:37 +02002570 int num_req_counters;
Eran Ben Elishac3abb512015-06-15 17:59:03 +03002571 int allocated;
2572 u32 counter_index;
Eran Ben Elisha3ba8e312015-10-15 14:44:40 +03002573 struct counter_index *new_counter_index = NULL;
Roland Dreier225c7b12007-05-08 18:00:38 -07002574
Shlomo Pongratz987c8f82012-04-29 17:04:26 +03002575 pr_info_once("%s", mlx4_ib_version);
Roland Dreier68f39482008-02-04 20:20:44 -08002576
Jack Morgenstein026149c2012-08-03 08:40:55 +00002577 num_ports = 0;
Eli Cohenfa417f72010-10-24 21:08:52 -07002578 mlx4_foreach_ib_transport_port(i, dev)
Roland Dreier22e7ef92009-01-09 13:22:29 -08002579 num_ports++;
2580
2581 /* No point in registering a device with no ports... */
2582 if (num_ports == 0)
2583 return NULL;
2584
Roland Dreier225c7b12007-05-08 18:00:38 -07002585 ibdev = (struct mlx4_ib_dev *) ib_alloc_device(sizeof *ibdev);
2586 if (!ibdev) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002587 dev_err(&dev->persist->pdev->dev,
2588 "Device struct alloc failed\n");
Roland Dreier225c7b12007-05-08 18:00:38 -07002589 return NULL;
2590 }
2591
Eli Cohenfa417f72010-10-24 21:08:52 -07002592 iboe = &ibdev->iboe;
2593
Roland Dreier225c7b12007-05-08 18:00:38 -07002594 if (mlx4_pd_alloc(dev, &ibdev->priv_pdn))
2595 goto err_dealloc;
2596
2597 if (mlx4_uar_alloc(dev, &ibdev->priv_uar))
2598 goto err_pd;
2599
Roland Dreier4979d182011-01-12 09:50:36 -08002600 ibdev->uar_map = ioremap((phys_addr_t) ibdev->priv_uar.pfn << PAGE_SHIFT,
2601 PAGE_SIZE);
Roland Dreier225c7b12007-05-08 18:00:38 -07002602 if (!ibdev->uar_map)
2603 goto err_uar;
Jack Morgenstein26c6bc72007-05-13 17:18:23 +03002604 MLX4_INIT_DOORBELL_LOCK(&ibdev->uar_lock);
Roland Dreier225c7b12007-05-08 18:00:38 -07002605
Roland Dreier225c7b12007-05-08 18:00:38 -07002606 ibdev->dev = dev;
Moni Shouac6215742015-02-03 16:48:39 +02002607 ibdev->bond_next_port = 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07002608
2609 strlcpy(ibdev->ib_dev.name, "mlx4_%d", IB_DEVICE_NAME_MAX);
2610 ibdev->ib_dev.owner = THIS_MODULE;
2611 ibdev->ib_dev.node_type = RDMA_NODE_IB_CA;
Roland Dreier95d04f02008-07-23 08:12:26 -07002612 ibdev->ib_dev.local_dma_lkey = dev->caps.reserved_lkey;
Roland Dreier22e7ef92009-01-09 13:22:29 -08002613 ibdev->num_ports = num_ports;
Moni Shouaa5750092015-02-03 16:48:37 +02002614 ibdev->ib_dev.phys_port_cnt = mlx4_is_bonded(dev) ?
2615 1 : ibdev->num_ports;
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08002616 ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002617 ibdev->ib_dev.dma_device = &dev->persist->pdev->dev;
Moni Shoua5070cd22015-07-30 18:33:30 +03002618 ibdev->ib_dev.get_netdev = mlx4_ib_get_netdev;
2619 ibdev->ib_dev.add_gid = mlx4_ib_add_gid;
2620 ibdev->ib_dev.del_gid = mlx4_ib_del_gid;
Roland Dreier225c7b12007-05-08 18:00:38 -07002621
Or Gerlitz08ff3232012-10-21 14:59:24 +00002622 if (dev->caps.userspace_caps)
2623 ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_ABI_VERSION;
2624 else
2625 ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION;
2626
Roland Dreier225c7b12007-05-08 18:00:38 -07002627 ibdev->ib_dev.uverbs_cmd_mask =
2628 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
2629 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
2630 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
2631 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
2632 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
2633 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Matan Barak93769322014-07-31 11:01:30 +03002634 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Roland Dreier225c7b12007-05-08 18:00:38 -07002635 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
2636 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
2637 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
Vladimir Sokolovskybbf8eed12008-04-16 21:09:33 -07002638 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
Roland Dreier225c7b12007-05-08 18:00:38 -07002639 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
2640 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
2641 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
Jack Morgenstein6a775e22007-06-21 12:27:47 +03002642 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
Roland Dreier225c7b12007-05-08 18:00:38 -07002643 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
2644 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
2645 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
2646 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
2647 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
Jack Morgenstein65541cb2007-06-21 13:03:11 +03002648 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
Sean Hefty18abd5e2011-06-02 10:43:26 -07002649 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
Sean Hefty42849b22011-08-11 13:57:43 -07002650 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
2651 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Roland Dreier225c7b12007-05-08 18:00:38 -07002652
2653 ibdev->ib_dev.query_device = mlx4_ib_query_device;
2654 ibdev->ib_dev.query_port = mlx4_ib_query_port;
Eli Cohenfa417f72010-10-24 21:08:52 -07002655 ibdev->ib_dev.get_link_layer = mlx4_ib_port_link_layer;
Roland Dreier225c7b12007-05-08 18:00:38 -07002656 ibdev->ib_dev.query_gid = mlx4_ib_query_gid;
2657 ibdev->ib_dev.query_pkey = mlx4_ib_query_pkey;
2658 ibdev->ib_dev.modify_device = mlx4_ib_modify_device;
2659 ibdev->ib_dev.modify_port = mlx4_ib_modify_port;
2660 ibdev->ib_dev.alloc_ucontext = mlx4_ib_alloc_ucontext;
2661 ibdev->ib_dev.dealloc_ucontext = mlx4_ib_dealloc_ucontext;
2662 ibdev->ib_dev.mmap = mlx4_ib_mmap;
2663 ibdev->ib_dev.alloc_pd = mlx4_ib_alloc_pd;
2664 ibdev->ib_dev.dealloc_pd = mlx4_ib_dealloc_pd;
2665 ibdev->ib_dev.create_ah = mlx4_ib_create_ah;
2666 ibdev->ib_dev.query_ah = mlx4_ib_query_ah;
2667 ibdev->ib_dev.destroy_ah = mlx4_ib_destroy_ah;
2668 ibdev->ib_dev.create_srq = mlx4_ib_create_srq;
2669 ibdev->ib_dev.modify_srq = mlx4_ib_modify_srq;
Jack Morgenstein65541cb2007-06-21 13:03:11 +03002670 ibdev->ib_dev.query_srq = mlx4_ib_query_srq;
Roland Dreier225c7b12007-05-08 18:00:38 -07002671 ibdev->ib_dev.destroy_srq = mlx4_ib_destroy_srq;
2672 ibdev->ib_dev.post_srq_recv = mlx4_ib_post_srq_recv;
2673 ibdev->ib_dev.create_qp = mlx4_ib_create_qp;
2674 ibdev->ib_dev.modify_qp = mlx4_ib_modify_qp;
Jack Morgenstein6a775e22007-06-21 12:27:47 +03002675 ibdev->ib_dev.query_qp = mlx4_ib_query_qp;
Roland Dreier225c7b12007-05-08 18:00:38 -07002676 ibdev->ib_dev.destroy_qp = mlx4_ib_destroy_qp;
2677 ibdev->ib_dev.post_send = mlx4_ib_post_send;
2678 ibdev->ib_dev.post_recv = mlx4_ib_post_recv;
2679 ibdev->ib_dev.create_cq = mlx4_ib_create_cq;
Eli Cohen3fdcb972008-04-16 21:09:33 -07002680 ibdev->ib_dev.modify_cq = mlx4_ib_modify_cq;
Vladimir Sokolovskybbf8eed12008-04-16 21:09:33 -07002681 ibdev->ib_dev.resize_cq = mlx4_ib_resize_cq;
Roland Dreier225c7b12007-05-08 18:00:38 -07002682 ibdev->ib_dev.destroy_cq = mlx4_ib_destroy_cq;
2683 ibdev->ib_dev.poll_cq = mlx4_ib_poll_cq;
2684 ibdev->ib_dev.req_notify_cq = mlx4_ib_arm_cq;
2685 ibdev->ib_dev.get_dma_mr = mlx4_ib_get_dma_mr;
2686 ibdev->ib_dev.reg_user_mr = mlx4_ib_reg_user_mr;
Matan Barak93769322014-07-31 11:01:30 +03002687 ibdev->ib_dev.rereg_user_mr = mlx4_ib_rereg_user_mr;
Roland Dreier225c7b12007-05-08 18:00:38 -07002688 ibdev->ib_dev.dereg_mr = mlx4_ib_dereg_mr;
Sagi Grimberg679e34d2015-07-30 10:32:42 +03002689 ibdev->ib_dev.alloc_mr = mlx4_ib_alloc_mr;
Sagi Grimberg1b2cd0f2015-10-13 19:11:27 +03002690 ibdev->ib_dev.map_mr_sg = mlx4_ib_map_mr_sg;
Roland Dreier225c7b12007-05-08 18:00:38 -07002691 ibdev->ib_dev.attach_mcast = mlx4_ib_mcg_attach;
2692 ibdev->ib_dev.detach_mcast = mlx4_ib_mcg_detach;
2693 ibdev->ib_dev.process_mad = mlx4_ib_process_mad;
Ira Weiny77386132015-05-13 20:02:58 -04002694 ibdev->ib_dev.get_port_immutable = mlx4_port_immutable;
Ira Weinye9db59f2016-06-15 02:22:00 -04002695 ibdev->ib_dev.get_dev_fw_str = get_fw_ver_str;
Yishai Hadasae184dd2015-08-13 18:32:06 +03002696 ibdev->ib_dev.disassociate_ucontext = mlx4_ib_disassociate_ucontext;
Roland Dreier225c7b12007-05-08 18:00:38 -07002697
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +00002698 if (!mlx4_is_slave(ibdev->dev)) {
2699 ibdev->ib_dev.alloc_fmr = mlx4_ib_fmr_alloc;
2700 ibdev->ib_dev.map_phys_fmr = mlx4_ib_map_phys_fmr;
2701 ibdev->ib_dev.unmap_fmr = mlx4_ib_unmap_fmr;
2702 ibdev->ib_dev.dealloc_fmr = mlx4_ib_fmr_dealloc;
2703 }
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +03002704
Shani Michaelib4253882013-02-06 16:19:16 +00002705 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_MEM_WINDOW ||
2706 dev->caps.bmme_flags & MLX4_BMME_FLAG_TYPE_2_WIN) {
2707 ibdev->ib_dev.alloc_mw = mlx4_ib_alloc_mw;
Shani Michaelib4253882013-02-06 16:19:16 +00002708 ibdev->ib_dev.dealloc_mw = mlx4_ib_dealloc_mw;
2709
2710 ibdev->ib_dev.uverbs_cmd_mask |=
2711 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
2712 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
2713 }
2714
Sean Hefty012a8ff2011-06-02 09:01:33 -07002715 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) {
2716 ibdev->ib_dev.alloc_xrcd = mlx4_ib_alloc_xrcd;
2717 ibdev->ib_dev.dealloc_xrcd = mlx4_ib_dealloc_xrcd;
2718 ibdev->ib_dev.uverbs_cmd_mask |=
2719 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
2720 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
2721 }
2722
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03002723 if (check_flow_steering_support(dev)) {
Matan Barak0a9b7d52013-11-07 15:25:15 +02002724 ibdev->steering_support = MLX4_STEERING_MODE_DEVICE_MANAGED;
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03002725 ibdev->ib_dev.create_flow = mlx4_ib_create_flow;
2726 ibdev->ib_dev.destroy_flow = mlx4_ib_destroy_flow;
2727
Yann Droneaudf21519b2013-11-06 23:21:49 +01002728 ibdev->ib_dev.uverbs_ex_cmd_mask |=
2729 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
2730 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
Hadar Hen Zionf77c0162013-08-14 13:58:31 +03002731 }
2732
Matan Barak4b664c42015-06-11 16:35:27 +03002733 ibdev->ib_dev.uverbs_ex_cmd_mask |=
2734 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
Eran Ben Elishafbfb6622015-10-15 14:44:42 +03002735 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
2736 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP);
Matan Barak4b664c42015-06-11 16:35:27 +03002737
Shlomo Pongratze605b742012-04-29 17:04:27 +03002738 mlx4_ib_alloc_eqs(dev, ibdev);
2739
Eli Cohenfa417f72010-10-24 21:08:52 -07002740 spin_lock_init(&iboe->lock);
2741
Roland Dreier225c7b12007-05-08 18:00:38 -07002742 if (init_node_data(ibdev))
2743 goto err_map;
Jack Morgensteinfd10ed82016-09-12 19:16:21 +03002744 mlx4_init_sl2vl_tbl(ibdev);
Roland Dreier225c7b12007-05-08 18:00:38 -07002745
Eran Ben Elisha3ba8e312015-10-15 14:44:40 +03002746 for (i = 0; i < ibdev->num_ports; ++i) {
2747 mutex_init(&ibdev->counters_table[i].mutex);
2748 INIT_LIST_HEAD(&ibdev->counters_table[i].counters_list);
2749 }
2750
Moni Shouaa5750092015-02-03 16:48:37 +02002751 num_req_counters = mlx4_is_bonded(dev) ? 1 : ibdev->num_ports;
2752 for (i = 0; i < num_req_counters; ++i) {
Matan Barak9433c182014-05-15 15:29:28 +03002753 mutex_init(&ibdev->qp1_proxy_lock[i]);
Eran Ben Elishac3abb512015-06-15 17:59:03 +03002754 allocated = 0;
Or Gerlitzcfcde112011-06-15 14:49:57 +00002755 if (mlx4_ib_port_link_layer(&ibdev->ib_dev, i + 1) ==
2756 IB_LINK_LAYER_ETHERNET) {
Eran Ben Elishac3abb512015-06-15 17:59:03 +03002757 err = mlx4_counter_alloc(ibdev->dev, &counter_index);
2758 /* if failed to allocate a new counter, use default */
Or Gerlitzcfcde112011-06-15 14:49:57 +00002759 if (err)
Eran Ben Elishac3abb512015-06-15 17:59:03 +03002760 counter_index =
2761 mlx4_get_default_counter_index(dev,
2762 i + 1);
2763 else
2764 allocated = 1;
2765 } else { /* IB_LINK_LAYER_INFINIBAND use the default counter */
2766 counter_index = mlx4_get_default_counter_index(dev,
2767 i + 1);
Dan Carpenter3839d8a2014-03-28 11:21:39 +03002768 }
Eran Ben Elisha3ba8e312015-10-15 14:44:40 +03002769 new_counter_index = kmalloc(sizeof(*new_counter_index),
2770 GFP_KERNEL);
2771 if (!new_counter_index) {
2772 if (allocated)
2773 mlx4_counter_free(ibdev->dev, counter_index);
2774 goto err_counter;
2775 }
2776 new_counter_index->index = counter_index;
2777 new_counter_index->allocated = allocated;
2778 list_add_tail(&new_counter_index->list,
2779 &ibdev->counters_table[i].counters_list);
2780 ibdev->counters_table[i].default_counter = counter_index;
Eran Ben Elishac3abb512015-06-15 17:59:03 +03002781 pr_info("counter index %d for port %d allocated %d\n",
2782 counter_index, i + 1, allocated);
Or Gerlitzcfcde112011-06-15 14:49:57 +00002783 }
Moni Shouaa5750092015-02-03 16:48:37 +02002784 if (mlx4_is_bonded(dev))
Eran Ben Elishac3abb512015-06-15 17:59:03 +03002785 for (i = 1; i < ibdev->num_ports ; ++i) {
Eran Ben Elisha3ba8e312015-10-15 14:44:40 +03002786 new_counter_index =
2787 kmalloc(sizeof(struct counter_index),
2788 GFP_KERNEL);
2789 if (!new_counter_index)
2790 goto err_counter;
2791 new_counter_index->index = counter_index;
2792 new_counter_index->allocated = 0;
2793 list_add_tail(&new_counter_index->list,
2794 &ibdev->counters_table[i].counters_list);
2795 ibdev->counters_table[i].default_counter =
2796 counter_index;
Eran Ben Elishac3abb512015-06-15 17:59:03 +03002797 }
Or Gerlitzcfcde112011-06-15 14:49:57 +00002798
Matan Barak41966702014-02-02 17:06:47 +02002799 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
2800 ib_num_ports++;
2801
Roland Dreier225c7b12007-05-08 18:00:38 -07002802 spin_lock_init(&ibdev->sm_lock);
2803 mutex_init(&ibdev->cap_mask_mutex);
Yishai Hadas35f05da2015-02-08 11:49:34 +02002804 INIT_LIST_HEAD(&ibdev->qp_list);
2805 spin_lock_init(&ibdev->reset_flow_resource_lock);
Roland Dreier225c7b12007-05-08 18:00:38 -07002806
Matan Barak41966702014-02-02 17:06:47 +02002807 if (ibdev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED &&
2808 ib_num_ports) {
Matan Barakc1c98502013-11-07 15:25:17 +02002809 ibdev->steer_qpn_count = MLX4_IB_UC_MAX_NUM_QPS;
2810 err = mlx4_qp_reserve_range(dev, ibdev->steer_qpn_count,
2811 MLX4_IB_UC_STEER_QPN_ALIGN,
Eugenia Emantayevddae0342014-12-11 10:57:54 +02002812 &ibdev->steer_qpn_base, 0);
Matan Barakc1c98502013-11-07 15:25:17 +02002813 if (err)
2814 goto err_counter;
2815
2816 ibdev->ib_uc_qpns_bitmap =
2817 kmalloc(BITS_TO_LONGS(ibdev->steer_qpn_count) *
2818 sizeof(long),
2819 GFP_KERNEL);
2820 if (!ibdev->ib_uc_qpns_bitmap) {
Yishai Hadas872bf2f2015-01-25 16:59:35 +02002821 dev_err(&dev->persist->pdev->dev,
2822 "bit map alloc failed\n");
Matan Barakc1c98502013-11-07 15:25:17 +02002823 goto err_steer_qp_release;
2824 }
2825
Eran Ben Elisha1f22e452016-11-10 11:31:00 +02002826 if (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_DMFS_IPOIB) {
2827 bitmap_zero(ibdev->ib_uc_qpns_bitmap,
2828 ibdev->steer_qpn_count);
2829 err = mlx4_FLOW_STEERING_IB_UC_QP_RANGE(
2830 dev, ibdev->steer_qpn_base,
2831 ibdev->steer_qpn_base +
2832 ibdev->steer_qpn_count - 1);
2833 if (err)
2834 goto err_steer_free_bitmap;
2835 } else {
2836 bitmap_fill(ibdev->ib_uc_qpns_bitmap,
2837 ibdev->steer_qpn_count);
2838 }
Matan Barakc1c98502013-11-07 15:25:17 +02002839 }
2840
Jack Morgenstein3e0629c2014-09-11 14:11:17 +03002841 for (j = 1; j <= ibdev->dev->caps.num_ports; j++)
2842 atomic64_set(&iboe->mac[j - 1], ibdev->dev->caps.def_mac[j]);
2843
Mark Bloch3f85f2a2016-07-19 20:54:58 +03002844 if (mlx4_ib_alloc_diag_counters(ibdev))
Matan Barakc1c98502013-11-07 15:25:17 +02002845 goto err_steer_free_bitmap;
Roland Dreier225c7b12007-05-08 18:00:38 -07002846
Mark Bloch3f85f2a2016-07-19 20:54:58 +03002847 if (ib_register_device(&ibdev->ib_dev, NULL))
2848 goto err_diag_counters;
2849
Roland Dreier225c7b12007-05-08 18:00:38 -07002850 if (mlx4_ib_mad_init(ibdev))
2851 goto err_reg;
2852
Jack Morgensteinfc065732012-08-03 08:40:42 +00002853 if (mlx4_ib_init_sriov(ibdev))
2854 goto err_mad;
2855
Moni Shoua71a39bb2016-01-14 17:50:40 +02002856 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE ||
2857 dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2) {
Moni Shouad487ee72013-12-12 18:03:13 +02002858 if (!iboe->nb.notifier_call) {
2859 iboe->nb.notifier_call = mlx4_ib_netdev_event;
2860 err = register_netdevice_notifier(&iboe->nb);
2861 if (err) {
2862 iboe->nb.notifier_call = NULL;
2863 goto err_notif;
2864 }
2865 }
Moni Shoua71a39bb2016-01-14 17:50:40 +02002866 if (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2) {
2867 err = mlx4_config_roce_v2_port(dev, ROCE_V2_UDP_DPORT);
2868 if (err) {
2869 goto err_notif;
2870 }
2871 }
Eli Cohenfa417f72010-10-24 21:08:52 -07002872 }
2873
Jack Morgenstein035b1032012-05-10 23:28:09 +03002874 for (j = 0; j < ARRAY_SIZE(mlx4_class_attributes); ++j) {
Tony Jonesf4e91eb2008-02-22 00:13:36 +01002875 if (device_create_file(&ibdev->ib_dev.dev,
Jack Morgenstein035b1032012-05-10 23:28:09 +03002876 mlx4_class_attributes[j]))
Eli Cohenfa417f72010-10-24 21:08:52 -07002877 goto err_notif;
Jack Morgensteincd9281d2007-09-18 09:14:18 +02002878 }
2879
Jack Morgenstein3b4a8cd2009-09-05 20:24:50 -07002880 ibdev->ib_active = true;
Jiri Pirko09d4d082016-02-26 17:32:24 +01002881 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
2882 devlink_port_type_ib_set(mlx4_get_devlink_port(dev, i),
2883 &ibdev->ib_dev);
Jack Morgenstein3b4a8cd2009-09-05 20:24:50 -07002884
Jack Morgenstein54679e12012-08-03 08:40:43 +00002885 if (mlx4_is_mfunc(ibdev->dev))
2886 init_pkeys(ibdev);
2887
Jack Morgenstein3806d082012-08-03 08:40:58 +00002888 /* create paravirt contexts for any VFs which are active */
2889 if (mlx4_is_master(ibdev->dev)) {
2890 for (j = 0; j < MLX4_MFUNC_MAX; j++) {
2891 if (j == mlx4_master_func_num(ibdev->dev))
2892 continue;
2893 if (mlx4_is_slave_active(ibdev->dev, j))
2894 do_slave_init(ibdev, j, 1);
2895 }
2896 }
Roland Dreier225c7b12007-05-08 18:00:38 -07002897 return ibdev;
2898
Eli Cohenfa417f72010-10-24 21:08:52 -07002899err_notif:
Moni Shouad487ee72013-12-12 18:03:13 +02002900 if (ibdev->iboe.nb.notifier_call) {
2901 if (unregister_netdevice_notifier(&ibdev->iboe.nb))
2902 pr_warn("failure unregistering notifier\n");
2903 ibdev->iboe.nb.notifier_call = NULL;
2904 }
Eli Cohenfa417f72010-10-24 21:08:52 -07002905 flush_workqueue(wq);
2906
Jack Morgensteinfc065732012-08-03 08:40:42 +00002907 mlx4_ib_close_sriov(ibdev);
2908
2909err_mad:
2910 mlx4_ib_mad_cleanup(ibdev);
2911
Roland Dreier225c7b12007-05-08 18:00:38 -07002912err_reg:
2913 ib_unregister_device(&ibdev->ib_dev);
2914
Mark Bloch3f85f2a2016-07-19 20:54:58 +03002915err_diag_counters:
2916 mlx4_ib_diag_cleanup(ibdev);
2917
Matan Barakc1c98502013-11-07 15:25:17 +02002918err_steer_free_bitmap:
2919 kfree(ibdev->ib_uc_qpns_bitmap);
2920
2921err_steer_qp_release:
2922 if (ibdev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED)
2923 mlx4_qp_release_range(dev, ibdev->steer_qpn_base,
2924 ibdev->steer_qpn_count);
Or Gerlitzcfcde112011-06-15 14:49:57 +00002925err_counter:
Eran Ben Elisha3ba8e312015-10-15 14:44:40 +03002926 for (i = 0; i < ibdev->num_ports; ++i)
2927 mlx4_ib_delete_counters_table(ibdev, &ibdev->counters_table[i]);
2928
Roland Dreier225c7b12007-05-08 18:00:38 -07002929err_map:
2930 iounmap(ibdev->uar_map);
2931
2932err_uar:
2933 mlx4_uar_free(dev, &ibdev->priv_uar);
2934
2935err_pd:
2936 mlx4_pd_free(dev, ibdev->priv_pdn);
2937
2938err_dealloc:
2939 ib_dealloc_device(&ibdev->ib_dev);
2940
2941 return NULL;
2942}
2943
Matan Barakc1c98502013-11-07 15:25:17 +02002944int mlx4_ib_steer_qp_alloc(struct mlx4_ib_dev *dev, int count, int *qpn)
2945{
2946 int offset;
2947
2948 WARN_ON(!dev->ib_uc_qpns_bitmap);
2949
2950 offset = bitmap_find_free_region(dev->ib_uc_qpns_bitmap,
2951 dev->steer_qpn_count,
2952 get_count_order(count));
2953 if (offset < 0)
2954 return offset;
2955
2956 *qpn = dev->steer_qpn_base + offset;
2957 return 0;
2958}
2959
2960void mlx4_ib_steer_qp_free(struct mlx4_ib_dev *dev, u32 qpn, int count)
2961{
2962 if (!qpn ||
2963 dev->steering_support != MLX4_STEERING_MODE_DEVICE_MANAGED)
2964 return;
2965
2966 BUG_ON(qpn < dev->steer_qpn_base);
2967
2968 bitmap_release_region(dev->ib_uc_qpns_bitmap,
2969 qpn - dev->steer_qpn_base,
2970 get_count_order(count));
2971}
2972
2973int mlx4_ib_steer_qp_reg(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
2974 int is_attach)
2975{
2976 int err;
2977 size_t flow_size;
2978 struct ib_flow_attr *flow = NULL;
2979 struct ib_flow_spec_ib *ib_spec;
2980
2981 if (is_attach) {
2982 flow_size = sizeof(struct ib_flow_attr) +
2983 sizeof(struct ib_flow_spec_ib);
2984 flow = kzalloc(flow_size, GFP_KERNEL);
2985 if (!flow)
2986 return -ENOMEM;
2987 flow->port = mqp->port;
2988 flow->num_of_specs = 1;
2989 flow->size = flow_size;
2990 ib_spec = (struct ib_flow_spec_ib *)(flow + 1);
2991 ib_spec->type = IB_FLOW_SPEC_IB;
2992 ib_spec->size = sizeof(struct ib_flow_spec_ib);
2993 /* Add an empty rule for IB L2 */
2994 memset(&ib_spec->mask, 0, sizeof(ib_spec->mask));
2995
2996 err = __mlx4_ib_create_flow(&mqp->ibqp, flow,
2997 IB_FLOW_DOMAIN_NIC,
2998 MLX4_FS_REGULAR,
2999 &mqp->reg_id);
3000 } else {
3001 err = __mlx4_ib_destroy_flow(mdev->dev, mqp->reg_id);
3002 }
3003 kfree(flow);
3004 return err;
3005}
3006
Roland Dreier225c7b12007-05-08 18:00:38 -07003007static void mlx4_ib_remove(struct mlx4_dev *dev, void *ibdev_ptr)
3008{
3009 struct mlx4_ib_dev *ibdev = ibdev_ptr;
3010 int p;
Jiri Pirko09d4d082016-02-26 17:32:24 +01003011 int i;
Roland Dreier225c7b12007-05-08 18:00:38 -07003012
Jiri Pirko09d4d082016-02-26 17:32:24 +01003013 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
3014 devlink_port_type_clear(mlx4_get_devlink_port(dev, i));
Moni Shoua4bf97152014-08-21 14:28:42 +03003015 ibdev->ib_active = false;
3016 flush_workqueue(wq);
3017
Jack Morgensteinfc065732012-08-03 08:40:42 +00003018 mlx4_ib_close_sriov(ibdev);
Yevgeny Petrilina6a47772009-03-18 19:49:54 -07003019 mlx4_ib_mad_cleanup(ibdev);
3020 ib_unregister_device(&ibdev->ib_dev);
Mark Bloch3f85f2a2016-07-19 20:54:58 +03003021 mlx4_ib_diag_cleanup(ibdev);
Eli Cohenfa417f72010-10-24 21:08:52 -07003022 if (ibdev->iboe.nb.notifier_call) {
3023 if (unregister_netdevice_notifier(&ibdev->iboe.nb))
Shlomo Pongratz987c8f82012-04-29 17:04:26 +03003024 pr_warn("failure unregistering notifier\n");
Eli Cohenfa417f72010-10-24 21:08:52 -07003025 ibdev->iboe.nb.notifier_call = NULL;
3026 }
Matan Barakc1c98502013-11-07 15:25:17 +02003027
3028 if (ibdev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED) {
3029 mlx4_qp_release_range(dev, ibdev->steer_qpn_base,
3030 ibdev->steer_qpn_count);
3031 kfree(ibdev->ib_uc_qpns_bitmap);
3032 }
3033
Eli Cohenfa417f72010-10-24 21:08:52 -07003034 iounmap(ibdev->uar_map);
Or Gerlitzcfcde112011-06-15 14:49:57 +00003035 for (p = 0; p < ibdev->num_ports; ++p)
Eran Ben Elisha3ba8e312015-10-15 14:44:40 +03003036 mlx4_ib_delete_counters_table(ibdev, &ibdev->counters_table[p]);
3037
Eli Cohenfa417f72010-10-24 21:08:52 -07003038 mlx4_foreach_port(p, dev, MLX4_PORT_TYPE_IB)
Roland Dreier225c7b12007-05-08 18:00:38 -07003039 mlx4_CLOSE_PORT(dev, p);
3040
Shlomo Pongratze605b742012-04-29 17:04:27 +03003041 mlx4_ib_free_eqs(dev, ibdev);
3042
Roland Dreier225c7b12007-05-08 18:00:38 -07003043 mlx4_uar_free(dev, &ibdev->priv_uar);
3044 mlx4_pd_free(dev, ibdev->priv_pdn);
3045 ib_dealloc_device(&ibdev->ib_dev);
3046}
3047
Jack Morgensteinfc065732012-08-03 08:40:42 +00003048static void do_slave_init(struct mlx4_ib_dev *ibdev, int slave, int do_init)
3049{
3050 struct mlx4_ib_demux_work **dm = NULL;
3051 struct mlx4_dev *dev = ibdev->dev;
3052 int i;
3053 unsigned long flags;
Matan Barak449fc482014-03-19 18:11:52 +02003054 struct mlx4_active_ports actv_ports;
3055 unsigned int ports;
3056 unsigned int first_port;
Jack Morgensteinfc065732012-08-03 08:40:42 +00003057
3058 if (!mlx4_is_master(dev))
3059 return;
3060
Matan Barak449fc482014-03-19 18:11:52 +02003061 actv_ports = mlx4_get_active_ports(dev, slave);
3062 ports = bitmap_weight(actv_ports.ports, dev->caps.num_ports);
3063 first_port = find_first_bit(actv_ports.ports, dev->caps.num_ports);
3064
3065 dm = kcalloc(ports, sizeof(*dm), GFP_ATOMIC);
Jack Morgensteinfc065732012-08-03 08:40:42 +00003066 if (!dm) {
3067 pr_err("failed to allocate memory for tunneling qp update\n");
Maninder Singha39a98f2015-07-08 09:43:35 +05303068 return;
Jack Morgensteinfc065732012-08-03 08:40:42 +00003069 }
3070
Matan Barak449fc482014-03-19 18:11:52 +02003071 for (i = 0; i < ports; i++) {
Jack Morgensteinfc065732012-08-03 08:40:42 +00003072 dm[i] = kmalloc(sizeof (struct mlx4_ib_demux_work), GFP_ATOMIC);
3073 if (!dm[i]) {
3074 pr_err("failed to allocate memory for tunneling qp update work struct\n");
Maninder Singha39a98f2015-07-08 09:43:35 +05303075 while (--i >= 0)
3076 kfree(dm[i]);
Jack Morgensteinfc065732012-08-03 08:40:42 +00003077 goto out;
3078 }
Jack Morgensteinfc065732012-08-03 08:40:42 +00003079 INIT_WORK(&dm[i]->work, mlx4_ib_tunnels_update_work);
Matan Barak449fc482014-03-19 18:11:52 +02003080 dm[i]->port = first_port + i + 1;
Jack Morgensteinfc065732012-08-03 08:40:42 +00003081 dm[i]->slave = slave;
3082 dm[i]->do_init = do_init;
3083 dm[i]->dev = ibdev;
Doug Ledfordd9a047a2015-07-09 10:21:08 -04003084 }
3085 /* initialize or tear down tunnel QPs for the slave */
3086 spin_lock_irqsave(&ibdev->sriov.going_down_lock, flags);
3087 if (!ibdev->sriov.is_going_down) {
3088 for (i = 0; i < ports; i++)
Jack Morgensteinfc065732012-08-03 08:40:42 +00003089 queue_work(ibdev->sriov.demux[i].ud_wq, &dm[i]->work);
3090 spin_unlock_irqrestore(&ibdev->sriov.going_down_lock, flags);
Doug Ledfordd9a047a2015-07-09 10:21:08 -04003091 } else {
3092 spin_unlock_irqrestore(&ibdev->sriov.going_down_lock, flags);
3093 for (i = 0; i < ports; i++)
3094 kfree(dm[i]);
Jack Morgensteinfc065732012-08-03 08:40:42 +00003095 }
3096out:
Syam Sidhardhanc89d1272013-02-24 23:20:05 +00003097 kfree(dm);
Jack Morgensteinfc065732012-08-03 08:40:42 +00003098 return;
3099}
3100
Yishai Hadas35f05da2015-02-08 11:49:34 +02003101static void mlx4_ib_handle_catas_error(struct mlx4_ib_dev *ibdev)
3102{
3103 struct mlx4_ib_qp *mqp;
3104 unsigned long flags_qp;
3105 unsigned long flags_cq;
3106 struct mlx4_ib_cq *send_mcq, *recv_mcq;
3107 struct list_head cq_notify_list;
3108 struct mlx4_cq *mcq;
3109 unsigned long flags;
3110
3111 pr_warn("mlx4_ib_handle_catas_error was started\n");
3112 INIT_LIST_HEAD(&cq_notify_list);
3113
3114 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
3115 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
3116
3117 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
3118 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
3119 if (mqp->sq.tail != mqp->sq.head) {
3120 send_mcq = to_mcq(mqp->ibqp.send_cq);
3121 spin_lock_irqsave(&send_mcq->lock, flags_cq);
3122 if (send_mcq->mcq.comp &&
3123 mqp->ibqp.send_cq->comp_handler) {
3124 if (!send_mcq->mcq.reset_notify_added) {
3125 send_mcq->mcq.reset_notify_added = 1;
3126 list_add_tail(&send_mcq->mcq.reset_notify,
3127 &cq_notify_list);
3128 }
3129 }
3130 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
3131 }
3132 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
3133 /* Now, handle the QP's receive queue */
3134 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
3135 /* no handling is needed for SRQ */
3136 if (!mqp->ibqp.srq) {
3137 if (mqp->rq.tail != mqp->rq.head) {
3138 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
3139 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
3140 if (recv_mcq->mcq.comp &&
3141 mqp->ibqp.recv_cq->comp_handler) {
3142 if (!recv_mcq->mcq.reset_notify_added) {
3143 recv_mcq->mcq.reset_notify_added = 1;
3144 list_add_tail(&recv_mcq->mcq.reset_notify,
3145 &cq_notify_list);
3146 }
3147 }
3148 spin_unlock_irqrestore(&recv_mcq->lock,
3149 flags_cq);
3150 }
3151 }
3152 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
3153 }
3154
3155 list_for_each_entry(mcq, &cq_notify_list, reset_notify) {
3156 mcq->comp(mcq);
3157 }
3158 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
3159 pr_warn("mlx4_ib_handle_catas_error ended\n");
3160}
3161
Moni Shouaa5750092015-02-03 16:48:37 +02003162static void handle_bonded_port_state_event(struct work_struct *work)
3163{
3164 struct ib_event_work *ew =
3165 container_of(work, struct ib_event_work, work);
3166 struct mlx4_ib_dev *ibdev = ew->ib_dev;
3167 enum ib_port_state bonded_port_state = IB_PORT_NOP;
3168 int i;
3169 struct ib_event ibev;
3170
3171 kfree(ew);
3172 spin_lock_bh(&ibdev->iboe.lock);
3173 for (i = 0; i < MLX4_MAX_PORTS; ++i) {
3174 struct net_device *curr_netdev = ibdev->iboe.netdevs[i];
Moni Shoua217e8b12015-03-18 16:51:35 +02003175 enum ib_port_state curr_port_state;
Moni Shouaa5750092015-02-03 16:48:37 +02003176
Moni Shoua217e8b12015-03-18 16:51:35 +02003177 if (!curr_netdev)
3178 continue;
3179
3180 curr_port_state =
Moni Shouaa5750092015-02-03 16:48:37 +02003181 (netif_running(curr_netdev) &&
3182 netif_carrier_ok(curr_netdev)) ?
3183 IB_PORT_ACTIVE : IB_PORT_DOWN;
3184
3185 bonded_port_state = (bonded_port_state != IB_PORT_ACTIVE) ?
3186 curr_port_state : IB_PORT_ACTIVE;
3187 }
3188 spin_unlock_bh(&ibdev->iboe.lock);
3189
3190 ibev.device = &ibdev->ib_dev;
3191 ibev.element.port_num = 1;
3192 ibev.event = (bonded_port_state == IB_PORT_ACTIVE) ?
3193 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
3194
3195 ib_dispatch_event(&ibev);
3196}
3197
Jack Morgensteinfd10ed82016-09-12 19:16:21 +03003198void mlx4_ib_sl2vl_update(struct mlx4_ib_dev *mdev, int port)
3199{
3200 u64 sl2vl;
3201 int err;
3202
3203 err = mlx4_ib_query_sl2vl(&mdev->ib_dev, port, &sl2vl);
3204 if (err) {
3205 pr_err("Unable to get current sl to vl mapping for port %d. Using all zeroes (%d)\n",
3206 port, err);
3207 sl2vl = 0;
3208 }
3209 atomic64_set(&mdev->sl2vl[port - 1], sl2vl);
3210}
3211
3212static void ib_sl2vl_update_work(struct work_struct *work)
3213{
3214 struct ib_event_work *ew = container_of(work, struct ib_event_work, work);
3215 struct mlx4_ib_dev *mdev = ew->ib_dev;
3216 int port = ew->port;
3217
3218 mlx4_ib_sl2vl_update(mdev, port);
3219
3220 kfree(ew);
3221}
3222
3223void mlx4_sched_ib_sl2vl_update_work(struct mlx4_ib_dev *ibdev,
3224 int port)
3225{
3226 struct ib_event_work *ew;
3227
3228 ew = kmalloc(sizeof(*ew), GFP_ATOMIC);
3229 if (ew) {
3230 INIT_WORK(&ew->work, ib_sl2vl_update_work);
3231 ew->port = port;
3232 ew->ib_dev = ibdev;
3233 queue_work(wq, &ew->work);
3234 } else {
3235 pr_err("failed to allocate memory for sl2vl update work\n");
3236 }
3237}
3238
Roland Dreier225c7b12007-05-08 18:00:38 -07003239static void mlx4_ib_event(struct mlx4_dev *dev, void *ibdev_ptr,
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03003240 enum mlx4_dev_event event, unsigned long param)
Roland Dreier225c7b12007-05-08 18:00:38 -07003241{
3242 struct ib_event ibev;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07003243 struct mlx4_ib_dev *ibdev = to_mdev((struct ib_device *) ibdev_ptr);
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03003244 struct mlx4_eqe *eqe = NULL;
3245 struct ib_event_work *ew;
Jack Morgensteinfc065732012-08-03 08:40:42 +00003246 int p = 0;
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03003247
Moni Shouaa5750092015-02-03 16:48:37 +02003248 if (mlx4_is_bonded(dev) &&
3249 ((event == MLX4_DEV_EVENT_PORT_UP) ||
3250 (event == MLX4_DEV_EVENT_PORT_DOWN))) {
3251 ew = kmalloc(sizeof(*ew), GFP_ATOMIC);
3252 if (!ew)
3253 return;
3254 INIT_WORK(&ew->work, handle_bonded_port_state_event);
3255 ew->ib_dev = ibdev;
3256 queue_work(wq, &ew->work);
3257 return;
3258 }
3259
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03003260 if (event == MLX4_DEV_EVENT_PORT_MGMT_CHANGE)
3261 eqe = (struct mlx4_eqe *)param;
3262 else
Jack Morgensteinfc065732012-08-03 08:40:42 +00003263 p = (int) param;
Roland Dreier225c7b12007-05-08 18:00:38 -07003264
3265 switch (event) {
Roland Dreier37608ee2008-04-16 21:01:08 -07003266 case MLX4_DEV_EVENT_PORT_UP:
Jack Morgensteinfc065732012-08-03 08:40:42 +00003267 if (p > ibdev->num_ports)
3268 return;
Jack Morgensteinfd10ed82016-09-12 19:16:21 +03003269 if (!mlx4_is_slave(dev) &&
Jack Morgensteina0c64a12012-08-03 08:40:49 +00003270 rdma_port_get_link_layer(&ibdev->ib_dev, p) ==
3271 IB_LINK_LAYER_INFINIBAND) {
Jack Morgensteinfd10ed82016-09-12 19:16:21 +03003272 if (mlx4_is_master(dev))
3273 mlx4_ib_invalidate_all_guid_record(ibdev, p);
3274 if (ibdev->dev->flags & MLX4_FLAG_SECURE_HOST &&
3275 !(ibdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SL_TO_VL_CHANGE_EVENT))
3276 mlx4_sched_ib_sl2vl_update_work(ibdev, p);
Jack Morgensteina0c64a12012-08-03 08:40:49 +00003277 }
Roland Dreier37608ee2008-04-16 21:01:08 -07003278 ibev.event = IB_EVENT_PORT_ACTIVE;
Roland Dreier225c7b12007-05-08 18:00:38 -07003279 break;
3280
Roland Dreier37608ee2008-04-16 21:01:08 -07003281 case MLX4_DEV_EVENT_PORT_DOWN:
Jack Morgensteinfc065732012-08-03 08:40:42 +00003282 if (p > ibdev->num_ports)
3283 return;
Roland Dreier37608ee2008-04-16 21:01:08 -07003284 ibev.event = IB_EVENT_PORT_ERR;
3285 break;
3286
3287 case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
Jack Morgenstein3b4a8cd2009-09-05 20:24:50 -07003288 ibdev->ib_active = false;
Roland Dreier225c7b12007-05-08 18:00:38 -07003289 ibev.event = IB_EVENT_DEVICE_FATAL;
Yishai Hadas35f05da2015-02-08 11:49:34 +02003290 mlx4_ib_handle_catas_error(ibdev);
Roland Dreier225c7b12007-05-08 18:00:38 -07003291 break;
3292
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03003293 case MLX4_DEV_EVENT_PORT_MGMT_CHANGE:
3294 ew = kmalloc(sizeof *ew, GFP_ATOMIC);
3295 if (!ew) {
3296 pr_err("failed to allocate memory for events work\n");
3297 break;
3298 }
3299
3300 INIT_WORK(&ew->work, handle_port_mgmt_change_event);
3301 memcpy(&ew->ib_eqe, eqe, sizeof *eqe);
3302 ew->ib_dev = ibdev;
Jack Morgenstein992e8e6e2012-08-03 08:40:54 +00003303 /* need to queue only for port owner, which uses GEN_EQE */
3304 if (mlx4_is_master(dev))
3305 queue_work(wq, &ew->work);
3306 else
3307 handle_port_mgmt_change_event(&ew->work);
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03003308 return;
3309
Jack Morgensteinfc065732012-08-03 08:40:42 +00003310 case MLX4_DEV_EVENT_SLAVE_INIT:
3311 /* here, p is the slave id */
3312 do_slave_init(ibdev, p, 1);
Yishai Hadasee59fa02015-03-03 17:28:49 +02003313 if (mlx4_is_master(dev)) {
3314 int i;
3315
3316 for (i = 1; i <= ibdev->num_ports; i++) {
3317 if (rdma_port_get_link_layer(&ibdev->ib_dev, i)
3318 == IB_LINK_LAYER_INFINIBAND)
3319 mlx4_ib_slave_alias_guid_event(ibdev,
3320 p, i,
3321 1);
3322 }
3323 }
Jack Morgensteinfc065732012-08-03 08:40:42 +00003324 return;
3325
3326 case MLX4_DEV_EVENT_SLAVE_SHUTDOWN:
Yishai Hadasee59fa02015-03-03 17:28:49 +02003327 if (mlx4_is_master(dev)) {
3328 int i;
3329
3330 for (i = 1; i <= ibdev->num_ports; i++) {
3331 if (rdma_port_get_link_layer(&ibdev->ib_dev, i)
3332 == IB_LINK_LAYER_INFINIBAND)
3333 mlx4_ib_slave_alias_guid_event(ibdev,
3334 p, i,
3335 0);
3336 }
3337 }
Jack Morgensteinfc065732012-08-03 08:40:42 +00003338 /* here, p is the slave id */
3339 do_slave_init(ibdev, p, 0);
3340 return;
3341
Roland Dreier225c7b12007-05-08 18:00:38 -07003342 default:
3343 return;
3344 }
3345
3346 ibev.device = ibdev_ptr;
Moni Shouaa5750092015-02-03 16:48:37 +02003347 ibev.element.port_num = mlx4_is_bonded(ibdev->dev) ? 1 : (u8)p;
Roland Dreier225c7b12007-05-08 18:00:38 -07003348
3349 ib_dispatch_event(&ibev);
3350}
3351
3352static struct mlx4_interface mlx4_ib_interface = {
Eli Cohenfa417f72010-10-24 21:08:52 -07003353 .add = mlx4_ib_add,
3354 .remove = mlx4_ib_remove,
3355 .event = mlx4_ib_event,
Moni Shouaa5750092015-02-03 16:48:37 +02003356 .protocol = MLX4_PROT_IB_IPV6,
3357 .flags = MLX4_INTFF_BONDING
Roland Dreier225c7b12007-05-08 18:00:38 -07003358};
3359
3360static int __init mlx4_ib_init(void)
3361{
Eli Cohenfa417f72010-10-24 21:08:52 -07003362 int err;
3363
Bhaktipriya Shridhar41cd3942016-08-15 23:42:48 +05303364 wq = alloc_ordered_workqueue("mlx4_ib", WQ_MEM_RECLAIM);
Eli Cohenfa417f72010-10-24 21:08:52 -07003365 if (!wq)
3366 return -ENOMEM;
3367
Oren Duerb9c5d6a2012-08-03 08:40:46 +00003368 err = mlx4_ib_mcg_init();
3369 if (err)
3370 goto clean_wq;
3371
Eli Cohenfa417f72010-10-24 21:08:52 -07003372 err = mlx4_register_interface(&mlx4_ib_interface);
Oren Duerb9c5d6a2012-08-03 08:40:46 +00003373 if (err)
3374 goto clean_mcg;
Eli Cohenfa417f72010-10-24 21:08:52 -07003375
3376 return 0;
Oren Duerb9c5d6a2012-08-03 08:40:46 +00003377
3378clean_mcg:
3379 mlx4_ib_mcg_destroy();
3380
3381clean_wq:
3382 destroy_workqueue(wq);
3383 return err;
Roland Dreier225c7b12007-05-08 18:00:38 -07003384}
3385
3386static void __exit mlx4_ib_cleanup(void)
3387{
3388 mlx4_unregister_interface(&mlx4_ib_interface);
Oren Duerb9c5d6a2012-08-03 08:40:46 +00003389 mlx4_ib_mcg_destroy();
Eli Cohenfa417f72010-10-24 21:08:52 -07003390 destroy_workqueue(wq);
Roland Dreier225c7b12007-05-08 18:00:38 -07003391}
3392
3393module_init(mlx4_ib_init);
3394module_exit(mlx4_ib_cleanup);