xref: /openbmc/linux/drivers/infiniband/hw/mlx4/main.c (revision 56a0eccd)
1 /*
2  * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
3  * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
4  *
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>
36 #include <linux/slab.h>
37 #include <linux/errno.h>
38 #include <linux/netdevice.h>
39 #include <linux/inetdevice.h>
40 #include <linux/rtnetlink.h>
41 #include <linux/if_vlan.h>
42 #include <net/ipv6.h>
43 #include <net/addrconf.h>
44 #include <net/devlink.h>
45 
46 #include <rdma/ib_smi.h>
47 #include <rdma/ib_user_verbs.h>
48 #include <rdma/ib_addr.h>
49 #include <rdma/ib_cache.h>
50 
51 #include <net/bonding.h>
52 
53 #include <linux/mlx4/driver.h>
54 #include <linux/mlx4/cmd.h>
55 #include <linux/mlx4/qp.h>
56 
57 #include "mlx4_ib.h"
58 #include "user.h"
59 
60 #define DRV_NAME	MLX4_IB_DRV_NAME
61 #define DRV_VERSION	"2.2-1"
62 #define DRV_RELDATE	"Feb 2014"
63 
64 #define MLX4_IB_FLOW_MAX_PRIO 0xFFF
65 #define MLX4_IB_FLOW_QPN_MASK 0xFFFFFF
66 #define MLX4_IB_CARD_REV_A0   0xA0
67 
68 MODULE_AUTHOR("Roland Dreier");
69 MODULE_DESCRIPTION("Mellanox ConnectX HCA InfiniBand driver");
70 MODULE_LICENSE("Dual BSD/GPL");
71 MODULE_VERSION(DRV_VERSION);
72 
73 int mlx4_ib_sm_guid_assign = 0;
74 module_param_named(sm_guid_assign, mlx4_ib_sm_guid_assign, int, 0444);
75 MODULE_PARM_DESC(sm_guid_assign, "Enable SM alias_GUID assignment if sm_guid_assign > 0 (Default: 0)");
76 
77 static const char mlx4_ib_version[] =
78 	DRV_NAME ": Mellanox ConnectX InfiniBand driver v"
79 	DRV_VERSION " (" DRV_RELDATE ")\n";
80 
81 static void do_slave_init(struct mlx4_ib_dev *ibdev, int slave, int do_init);
82 
83 static struct workqueue_struct *wq;
84 
85 static 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 
93 static int check_flow_steering_support(struct mlx4_dev *dev)
94 {
95 	int eth_num_ports = 0;
96 	int ib_num_ports = 0;
97 
98 	int dmfs = dev->caps.steering_mode == MLX4_STEERING_MODE_DEVICE_MANAGED;
99 
100 	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;
113 		}
114 	}
115 	return dmfs;
116 }
117 
118 static 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 
129 static 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 
158 static int mlx4_ib_update_gids_v1(struct gid_entry *gids,
159 				  struct mlx4_ib_dev *ibdev,
160 				  u8 port_num)
161 {
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 
191 static 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 
238 static 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 
248 static 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) {
276 		if (!memcmp(&port_gid_table->gids[i].gid, gid, sizeof(*gid)) &&
277 		    (port_gid_table->gids[i].gid_type == attr->gid_type))  {
278 			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));
295 				port_gid_table->gids[free].gid_type = attr->gid_type;
296 				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 {
311 			for (i = 0; i < MLX4_MAX_PORT_GIDS; i++) {
312 				memcpy(&gids[i].gid, &port_gid_table->gids[i].gid, sizeof(union ib_gid));
313 				gids[i].gid_type = port_gid_table->gids[i].gid_type;
314 			}
315 		}
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 
327 static 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 
379 int 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;
390 	struct ib_gid_attr attr;
391 
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 
401 	ret = ib_get_cached_gid(&ibdev->ib_dev, port_num, index, &gid, &attr);
402 	if (ret)
403 		return ret;
404 
405 	if (attr.ndev)
406 		dev_put(attr.ndev);
407 
408 	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)
415 		if (!memcmp(&port_gid_table->gids[i].gid, &gid, sizeof(gid)) &&
416 		    attr.gid_type == port_gid_table->gids[i].gid_type) {
417 			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 
426 static int mlx4_ib_query_device(struct ib_device *ibdev,
427 				struct ib_device_attr *props,
428 				struct ib_udata *uhw)
429 {
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;
434 	int have_ib_ports;
435 	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;
438 
439 	if (uhw->inlen) {
440 		if (uhw->inlen < sizeof(cmd))
441 			return -EINVAL;
442 
443 		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);
456 	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 
464 	err = mlx4_MAD_IFC(to_mdev(ibdev), MLX4_MAD_IFC_IGNORE_KEYS,
465 			   1, NULL, NULL, in_mad, out_mad);
466 	if (err)
467 		goto out;
468 
469 	memset(props, 0, sizeof *props);
470 
471 	have_ib_ports = num_ib_ports(dev->dev);
472 
473 	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		|
477 		IB_DEVICE_RC_RNR_NAK_GEN		|
478 		IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
479 	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;
483 	if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_APM && have_ib_ports)
484 		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;
487 	if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_IPOIB_CSUM)
488 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
489 	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))
492 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
493 	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;
499 	if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC)
500 		props->device_cap_flags |= IB_DEVICE_XRC;
501 	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 	if (dev->steering_support ==  MLX4_STEERING_MODE_DEVICE_MANAGED)
509 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
510 	}
511 
512 	props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
513 
514 	props->vendor_id	   = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
515 		0xffffff;
516 	props->vendor_part_id	   = dev->dev->persist->pdev->device;
517 	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;
522 	props->max_qp		   = dev->dev->quotas.qp;
523 	props->max_qp_wr	   = dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE;
524 	props->max_sge		   = min(dev->dev->caps.max_sq_sg,
525 					 dev->dev->caps.max_rq_sg);
526 	props->max_sge_rd	   = MLX4_MAX_SGE_RD;
527 	props->max_cq		   = dev->dev->quotas.cq;
528 	props->max_cqe		   = dev->dev->caps.max_cqes;
529 	props->max_mr		   = dev->dev->quotas.mpt;
530 	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;
534 	props->max_srq		   = dev->dev->quotas.srq;
535 	props->max_srq_wr	   = dev->dev->caps.max_srq_wqes - 1;
536 	props->max_srq_sge	   = dev->dev->caps.max_srq_sge;
537 	props->max_fast_reg_page_list_len = MLX4_MAX_FAST_REG_PAGES;
538 	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;
541 	props->masked_atomic_cap   = props->atomic_cap;
542 	props->max_pkeys	   = dev->dev->caps.pkey_table_len[1];
543 	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;
547 	props->max_map_per_fmr = dev->dev->caps.max_fmr_maps;
548 	props->hca_core_clock = dev->dev->caps.hca_core_clock * 1000UL;
549 	props->timestamp_mask = 0xFFFFFFFFFFFFULL;
550 
551 	if (!mlx4_is_slave(dev->dev))
552 		err = mlx4_get_internal_clock_params(dev->dev, &clock_params);
553 
554 	if (uhw->outlen >= resp.response_length + sizeof(resp.hca_core_clock_offset)) {
555 		resp.response_length += sizeof(resp.hca_core_clock_offset);
556 		if (!err && !mlx4_is_slave(dev->dev)) {
557 			resp.comp_mask |= QUERY_DEVICE_RESP_MASK_TIMESTAMP;
558 			resp.hca_core_clock_offset = clock_params.offset % PAGE_SIZE;
559 		}
560 	}
561 
562 	if (uhw->outlen) {
563 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
564 		if (err)
565 			goto out;
566 	}
567 out:
568 	kfree(in_mad);
569 	kfree(out_mad);
570 
571 	return err;
572 }
573 
574 static enum rdma_link_layer
575 mlx4_ib_port_link_layer(struct ib_device *device, u8 port_num)
576 {
577 	struct mlx4_dev *dev = to_mdev(device)->dev;
578 
579 	return dev->caps.port_mask[port_num] == MLX4_PORT_TYPE_IB ?
580 		IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
581 }
582 
583 static int ib_link_query_port(struct ib_device *ibdev, u8 port,
584 			      struct ib_port_attr *props, int netw_view)
585 {
586 	struct ib_smp *in_mad  = NULL;
587 	struct ib_smp *out_mad = NULL;
588 	int ext_active_speed;
589 	int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
590 	int err = -ENOMEM;
591 
592 	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
593 	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
594 	if (!in_mad || !out_mad)
595 		goto out;
596 
597 	init_query_mad(in_mad);
598 	in_mad->attr_id  = IB_SMP_ATTR_PORT_INFO;
599 	in_mad->attr_mod = cpu_to_be32(port);
600 
601 	if (mlx4_is_mfunc(to_mdev(ibdev)->dev) && netw_view)
602 		mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
603 
604 	err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port, NULL, NULL,
605 				in_mad, out_mad);
606 	if (err)
607 		goto out;
608 
609 
610 	props->lid		= be16_to_cpup((__be16 *) (out_mad->data + 16));
611 	props->lmc		= out_mad->data[34] & 0x7;
612 	props->sm_lid		= be16_to_cpup((__be16 *) (out_mad->data + 18));
613 	props->sm_sl		= out_mad->data[36] & 0xf;
614 	props->state		= out_mad->data[32] & 0xf;
615 	props->phys_state	= out_mad->data[33] >> 4;
616 	props->port_cap_flags	= be32_to_cpup((__be32 *) (out_mad->data + 20));
617 	if (netw_view)
618 		props->gid_tbl_len = out_mad->data[50];
619 	else
620 		props->gid_tbl_len = to_mdev(ibdev)->dev->caps.gid_table_len[port];
621 	props->max_msg_sz	= to_mdev(ibdev)->dev->caps.max_msg_sz;
622 	props->pkey_tbl_len	= to_mdev(ibdev)->dev->caps.pkey_table_len[port];
623 	props->bad_pkey_cntr	= be16_to_cpup((__be16 *) (out_mad->data + 46));
624 	props->qkey_viol_cntr	= be16_to_cpup((__be16 *) (out_mad->data + 48));
625 	props->active_width	= out_mad->data[31] & 0xf;
626 	props->active_speed	= out_mad->data[35] >> 4;
627 	props->max_mtu		= out_mad->data[41] & 0xf;
628 	props->active_mtu	= out_mad->data[36] >> 4;
629 	props->subnet_timeout	= out_mad->data[51] & 0x1f;
630 	props->max_vl_num	= out_mad->data[37] >> 4;
631 	props->init_type_reply	= out_mad->data[41] >> 4;
632 
633 	/* Check if extended speeds (EDR/FDR/...) are supported */
634 	if (props->port_cap_flags & IB_PORT_EXTENDED_SPEEDS_SUP) {
635 		ext_active_speed = out_mad->data[62] >> 4;
636 
637 		switch (ext_active_speed) {
638 		case 1:
639 			props->active_speed = IB_SPEED_FDR;
640 			break;
641 		case 2:
642 			props->active_speed = IB_SPEED_EDR;
643 			break;
644 		}
645 	}
646 
647 	/* If reported active speed is QDR, check if is FDR-10 */
648 	if (props->active_speed == IB_SPEED_QDR) {
649 		init_query_mad(in_mad);
650 		in_mad->attr_id = MLX4_ATTR_EXTENDED_PORT_INFO;
651 		in_mad->attr_mod = cpu_to_be32(port);
652 
653 		err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port,
654 				   NULL, NULL, in_mad, out_mad);
655 		if (err)
656 			goto out;
657 
658 		/* Checking LinkSpeedActive for FDR-10 */
659 		if (out_mad->data[15] & 0x1)
660 			props->active_speed = IB_SPEED_FDR10;
661 	}
662 
663 	/* Avoid wrong speed value returned by FW if the IB link is down. */
664 	if (props->state == IB_PORT_DOWN)
665 		 props->active_speed = IB_SPEED_SDR;
666 
667 out:
668 	kfree(in_mad);
669 	kfree(out_mad);
670 	return err;
671 }
672 
673 static u8 state_to_phys_state(enum ib_port_state state)
674 {
675 	return state == IB_PORT_ACTIVE ? 5 : 3;
676 }
677 
678 static int eth_link_query_port(struct ib_device *ibdev, u8 port,
679 			       struct ib_port_attr *props, int netw_view)
680 {
681 
682 	struct mlx4_ib_dev *mdev = to_mdev(ibdev);
683 	struct mlx4_ib_iboe *iboe = &mdev->iboe;
684 	struct net_device *ndev;
685 	enum ib_mtu tmp;
686 	struct mlx4_cmd_mailbox *mailbox;
687 	int err = 0;
688 	int is_bonded = mlx4_is_bonded(mdev->dev);
689 
690 	mailbox = mlx4_alloc_cmd_mailbox(mdev->dev);
691 	if (IS_ERR(mailbox))
692 		return PTR_ERR(mailbox);
693 
694 	err = mlx4_cmd_box(mdev->dev, 0, mailbox->dma, port, 0,
695 			   MLX4_CMD_QUERY_PORT, MLX4_CMD_TIME_CLASS_B,
696 			   MLX4_CMD_WRAPPED);
697 	if (err)
698 		goto out;
699 
700 	props->active_width	=  (((u8 *)mailbox->buf)[5] == 0x40) ?
701 						IB_WIDTH_4X : IB_WIDTH_1X;
702 	props->active_speed	= IB_SPEED_QDR;
703 	props->port_cap_flags	= IB_PORT_CM_SUP | IB_PORT_IP_BASED_GIDS;
704 	props->gid_tbl_len	= mdev->dev->caps.gid_table_len[port];
705 	props->max_msg_sz	= mdev->dev->caps.max_msg_sz;
706 	props->pkey_tbl_len	= 1;
707 	props->max_mtu		= IB_MTU_4096;
708 	props->max_vl_num	= 2;
709 	props->state		= IB_PORT_DOWN;
710 	props->phys_state	= state_to_phys_state(props->state);
711 	props->active_mtu	= IB_MTU_256;
712 	spin_lock_bh(&iboe->lock);
713 	ndev = iboe->netdevs[port - 1];
714 	if (ndev && is_bonded) {
715 		rcu_read_lock(); /* required to get upper dev */
716 		ndev = netdev_master_upper_dev_get_rcu(ndev);
717 		rcu_read_unlock();
718 	}
719 	if (!ndev)
720 		goto out_unlock;
721 
722 	tmp = iboe_get_mtu(ndev->mtu);
723 	props->active_mtu = tmp ? min(props->max_mtu, tmp) : IB_MTU_256;
724 
725 	props->state		= (netif_running(ndev) && netif_carrier_ok(ndev)) ?
726 					IB_PORT_ACTIVE : IB_PORT_DOWN;
727 	props->phys_state	= state_to_phys_state(props->state);
728 out_unlock:
729 	spin_unlock_bh(&iboe->lock);
730 out:
731 	mlx4_free_cmd_mailbox(mdev->dev, mailbox);
732 	return err;
733 }
734 
735 int __mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
736 			 struct ib_port_attr *props, int netw_view)
737 {
738 	int err;
739 
740 	memset(props, 0, sizeof *props);
741 
742 	err = mlx4_ib_port_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND ?
743 		ib_link_query_port(ibdev, port, props, netw_view) :
744 				eth_link_query_port(ibdev, port, props, netw_view);
745 
746 	return err;
747 }
748 
749 static int mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
750 			      struct ib_port_attr *props)
751 {
752 	/* returns host view */
753 	return __mlx4_ib_query_port(ibdev, port, props, 0);
754 }
755 
756 int __mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
757 			union ib_gid *gid, int netw_view)
758 {
759 	struct ib_smp *in_mad  = NULL;
760 	struct ib_smp *out_mad = NULL;
761 	int err = -ENOMEM;
762 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
763 	int clear = 0;
764 	int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
765 
766 	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
767 	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
768 	if (!in_mad || !out_mad)
769 		goto out;
770 
771 	init_query_mad(in_mad);
772 	in_mad->attr_id  = IB_SMP_ATTR_PORT_INFO;
773 	in_mad->attr_mod = cpu_to_be32(port);
774 
775 	if (mlx4_is_mfunc(dev->dev) && netw_view)
776 		mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
777 
778 	err = mlx4_MAD_IFC(dev, mad_ifc_flags, port, NULL, NULL, in_mad, out_mad);
779 	if (err)
780 		goto out;
781 
782 	memcpy(gid->raw, out_mad->data + 8, 8);
783 
784 	if (mlx4_is_mfunc(dev->dev) && !netw_view) {
785 		if (index) {
786 			/* For any index > 0, return the null guid */
787 			err = 0;
788 			clear = 1;
789 			goto out;
790 		}
791 	}
792 
793 	init_query_mad(in_mad);
794 	in_mad->attr_id  = IB_SMP_ATTR_GUID_INFO;
795 	in_mad->attr_mod = cpu_to_be32(index / 8);
796 
797 	err = mlx4_MAD_IFC(dev, mad_ifc_flags, port,
798 			   NULL, NULL, in_mad, out_mad);
799 	if (err)
800 		goto out;
801 
802 	memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
803 
804 out:
805 	if (clear)
806 		memset(gid->raw + 8, 0, 8);
807 	kfree(in_mad);
808 	kfree(out_mad);
809 	return err;
810 }
811 
812 static int mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
813 			     union ib_gid *gid)
814 {
815 	int ret;
816 
817 	if (rdma_protocol_ib(ibdev, port))
818 		return __mlx4_ib_query_gid(ibdev, port, index, gid, 0);
819 
820 	if (!rdma_protocol_roce(ibdev, port))
821 		return -ENODEV;
822 
823 	if (!rdma_cap_roce_gid_table(ibdev, port))
824 		return -ENODEV;
825 
826 	ret = ib_get_cached_gid(ibdev, port, index, gid, NULL);
827 	if (ret == -EAGAIN) {
828 		memcpy(gid, &zgid, sizeof(*gid));
829 		return 0;
830 	}
831 
832 	return ret;
833 }
834 
835 int __mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
836 			 u16 *pkey, int netw_view)
837 {
838 	struct ib_smp *in_mad  = NULL;
839 	struct ib_smp *out_mad = NULL;
840 	int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
841 	int err = -ENOMEM;
842 
843 	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
844 	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
845 	if (!in_mad || !out_mad)
846 		goto out;
847 
848 	init_query_mad(in_mad);
849 	in_mad->attr_id  = IB_SMP_ATTR_PKEY_TABLE;
850 	in_mad->attr_mod = cpu_to_be32(index / 32);
851 
852 	if (mlx4_is_mfunc(to_mdev(ibdev)->dev) && netw_view)
853 		mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
854 
855 	err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port, NULL, NULL,
856 			   in_mad, out_mad);
857 	if (err)
858 		goto out;
859 
860 	*pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
861 
862 out:
863 	kfree(in_mad);
864 	kfree(out_mad);
865 	return err;
866 }
867 
868 static int mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
869 {
870 	return __mlx4_ib_query_pkey(ibdev, port, index, pkey, 0);
871 }
872 
873 static int mlx4_ib_modify_device(struct ib_device *ibdev, int mask,
874 				 struct ib_device_modify *props)
875 {
876 	struct mlx4_cmd_mailbox *mailbox;
877 	unsigned long flags;
878 
879 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
880 		return -EOPNOTSUPP;
881 
882 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
883 		return 0;
884 
885 	if (mlx4_is_slave(to_mdev(ibdev)->dev))
886 		return -EOPNOTSUPP;
887 
888 	spin_lock_irqsave(&to_mdev(ibdev)->sm_lock, flags);
889 	memcpy(ibdev->node_desc, props->node_desc, 64);
890 	spin_unlock_irqrestore(&to_mdev(ibdev)->sm_lock, flags);
891 
892 	/*
893 	 * If possible, pass node desc to FW, so it can generate
894 	 * a 144 trap.  If cmd fails, just ignore.
895 	 */
896 	mailbox = mlx4_alloc_cmd_mailbox(to_mdev(ibdev)->dev);
897 	if (IS_ERR(mailbox))
898 		return 0;
899 
900 	memcpy(mailbox->buf, props->node_desc, 64);
901 	mlx4_cmd(to_mdev(ibdev)->dev, mailbox->dma, 1, 0,
902 		 MLX4_CMD_SET_NODE, MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
903 
904 	mlx4_free_cmd_mailbox(to_mdev(ibdev)->dev, mailbox);
905 
906 	return 0;
907 }
908 
909 static int mlx4_ib_SET_PORT(struct mlx4_ib_dev *dev, u8 port, int reset_qkey_viols,
910 			    u32 cap_mask)
911 {
912 	struct mlx4_cmd_mailbox *mailbox;
913 	int err;
914 
915 	mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
916 	if (IS_ERR(mailbox))
917 		return PTR_ERR(mailbox);
918 
919 	if (dev->dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
920 		*(u8 *) mailbox->buf	     = !!reset_qkey_viols << 6;
921 		((__be32 *) mailbox->buf)[2] = cpu_to_be32(cap_mask);
922 	} else {
923 		((u8 *) mailbox->buf)[3]     = !!reset_qkey_viols;
924 		((__be32 *) mailbox->buf)[1] = cpu_to_be32(cap_mask);
925 	}
926 
927 	err = mlx4_cmd(dev->dev, mailbox->dma, port, MLX4_SET_PORT_IB_OPCODE,
928 		       MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
929 		       MLX4_CMD_WRAPPED);
930 
931 	mlx4_free_cmd_mailbox(dev->dev, mailbox);
932 	return err;
933 }
934 
935 static int mlx4_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
936 			       struct ib_port_modify *props)
937 {
938 	struct mlx4_ib_dev *mdev = to_mdev(ibdev);
939 	u8 is_eth = mdev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
940 	struct ib_port_attr attr;
941 	u32 cap_mask;
942 	int err;
943 
944 	/* return OK if this is RoCE. CM calls ib_modify_port() regardless
945 	 * of whether port link layer is ETH or IB. For ETH ports, qkey
946 	 * violations and port capabilities are not meaningful.
947 	 */
948 	if (is_eth)
949 		return 0;
950 
951 	mutex_lock(&mdev->cap_mask_mutex);
952 
953 	err = mlx4_ib_query_port(ibdev, port, &attr);
954 	if (err)
955 		goto out;
956 
957 	cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
958 		~props->clr_port_cap_mask;
959 
960 	err = mlx4_ib_SET_PORT(mdev, port,
961 			       !!(mask & IB_PORT_RESET_QKEY_CNTR),
962 			       cap_mask);
963 
964 out:
965 	mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
966 	return err;
967 }
968 
969 static struct ib_ucontext *mlx4_ib_alloc_ucontext(struct ib_device *ibdev,
970 						  struct ib_udata *udata)
971 {
972 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
973 	struct mlx4_ib_ucontext *context;
974 	struct mlx4_ib_alloc_ucontext_resp_v3 resp_v3;
975 	struct mlx4_ib_alloc_ucontext_resp resp;
976 	int err;
977 
978 	if (!dev->ib_active)
979 		return ERR_PTR(-EAGAIN);
980 
981 	if (ibdev->uverbs_abi_ver == MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION) {
982 		resp_v3.qp_tab_size      = dev->dev->caps.num_qps;
983 		resp_v3.bf_reg_size      = dev->dev->caps.bf_reg_size;
984 		resp_v3.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
985 	} else {
986 		resp.dev_caps	      = dev->dev->caps.userspace_caps;
987 		resp.qp_tab_size      = dev->dev->caps.num_qps;
988 		resp.bf_reg_size      = dev->dev->caps.bf_reg_size;
989 		resp.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
990 		resp.cqe_size	      = dev->dev->caps.cqe_size;
991 	}
992 
993 	context = kzalloc(sizeof(*context), GFP_KERNEL);
994 	if (!context)
995 		return ERR_PTR(-ENOMEM);
996 
997 	err = mlx4_uar_alloc(to_mdev(ibdev)->dev, &context->uar);
998 	if (err) {
999 		kfree(context);
1000 		return ERR_PTR(err);
1001 	}
1002 
1003 	INIT_LIST_HEAD(&context->db_page_list);
1004 	mutex_init(&context->db_page_mutex);
1005 
1006 	if (ibdev->uverbs_abi_ver == MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION)
1007 		err = ib_copy_to_udata(udata, &resp_v3, sizeof(resp_v3));
1008 	else
1009 		err = ib_copy_to_udata(udata, &resp, sizeof(resp));
1010 
1011 	if (err) {
1012 		mlx4_uar_free(to_mdev(ibdev)->dev, &context->uar);
1013 		kfree(context);
1014 		return ERR_PTR(-EFAULT);
1015 	}
1016 
1017 	return &context->ibucontext;
1018 }
1019 
1020 static int mlx4_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1021 {
1022 	struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
1023 
1024 	mlx4_uar_free(to_mdev(ibcontext->device)->dev, &context->uar);
1025 	kfree(context);
1026 
1027 	return 0;
1028 }
1029 
1030 static void  mlx4_ib_vma_open(struct vm_area_struct *area)
1031 {
1032 	/* vma_open is called when a new VMA is created on top of our VMA.
1033 	 * This is done through either mremap flow or split_vma (usually due
1034 	 * to mlock, madvise, munmap, etc.). We do not support a clone of the
1035 	 * vma, as this VMA is strongly hardware related. Therefore we set the
1036 	 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1037 	 * calling us again and trying to do incorrect actions. We assume that
1038 	 * the original vma size is exactly a single page that there will be no
1039 	 * "splitting" operations on.
1040 	 */
1041 	area->vm_ops = NULL;
1042 }
1043 
1044 static void  mlx4_ib_vma_close(struct vm_area_struct *area)
1045 {
1046 	struct mlx4_ib_vma_private_data *mlx4_ib_vma_priv_data;
1047 
1048 	/* It's guaranteed that all VMAs opened on a FD are closed before the
1049 	 * file itself is closed, therefore no sync is needed with the regular
1050 	 * closing flow. (e.g. mlx4_ib_dealloc_ucontext) However need a sync
1051 	 * with accessing the vma as part of mlx4_ib_disassociate_ucontext.
1052 	 * The close operation is usually called under mm->mmap_sem except when
1053 	 * process is exiting.  The exiting case is handled explicitly as part
1054 	 * of mlx4_ib_disassociate_ucontext.
1055 	 */
1056 	mlx4_ib_vma_priv_data = (struct mlx4_ib_vma_private_data *)
1057 				area->vm_private_data;
1058 
1059 	/* set the vma context pointer to null in the mlx4_ib driver's private
1060 	 * data to protect against a race condition in mlx4_ib_dissassociate_ucontext().
1061 	 */
1062 	mlx4_ib_vma_priv_data->vma = NULL;
1063 }
1064 
1065 static const struct vm_operations_struct mlx4_ib_vm_ops = {
1066 	.open = mlx4_ib_vma_open,
1067 	.close = mlx4_ib_vma_close
1068 };
1069 
1070 static void mlx4_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1071 {
1072 	int i;
1073 	int ret = 0;
1074 	struct vm_area_struct *vma;
1075 	struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
1076 	struct task_struct *owning_process  = NULL;
1077 	struct mm_struct   *owning_mm       = NULL;
1078 
1079 	owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
1080 	if (!owning_process)
1081 		return;
1082 
1083 	owning_mm = get_task_mm(owning_process);
1084 	if (!owning_mm) {
1085 		pr_info("no mm, disassociate ucontext is pending task termination\n");
1086 		while (1) {
1087 			/* make sure that task is dead before returning, it may
1088 			 * prevent a rare case of module down in parallel to a
1089 			 * call to mlx4_ib_vma_close.
1090 			 */
1091 			put_task_struct(owning_process);
1092 			msleep(1);
1093 			owning_process = get_pid_task(ibcontext->tgid,
1094 						      PIDTYPE_PID);
1095 			if (!owning_process ||
1096 			    owning_process->state == TASK_DEAD) {
1097 				pr_info("disassociate ucontext done, task was terminated\n");
1098 				/* in case task was dead need to release the task struct */
1099 				if (owning_process)
1100 					put_task_struct(owning_process);
1101 				return;
1102 			}
1103 		}
1104 	}
1105 
1106 	/* need to protect from a race on closing the vma as part of
1107 	 * mlx4_ib_vma_close().
1108 	 */
1109 	down_read(&owning_mm->mmap_sem);
1110 	for (i = 0; i < HW_BAR_COUNT; i++) {
1111 		vma = context->hw_bar_info[i].vma;
1112 		if (!vma)
1113 			continue;
1114 
1115 		ret = zap_vma_ptes(context->hw_bar_info[i].vma,
1116 				   context->hw_bar_info[i].vma->vm_start,
1117 				   PAGE_SIZE);
1118 		if (ret) {
1119 			pr_err("Error: zap_vma_ptes failed for index=%d, ret=%d\n", i, ret);
1120 			BUG_ON(1);
1121 		}
1122 
1123 		/* context going to be destroyed, should not access ops any more */
1124 		context->hw_bar_info[i].vma->vm_ops = NULL;
1125 	}
1126 
1127 	up_read(&owning_mm->mmap_sem);
1128 	mmput(owning_mm);
1129 	put_task_struct(owning_process);
1130 }
1131 
1132 static void mlx4_ib_set_vma_data(struct vm_area_struct *vma,
1133 				 struct mlx4_ib_vma_private_data *vma_private_data)
1134 {
1135 	vma_private_data->vma = vma;
1136 	vma->vm_private_data = vma_private_data;
1137 	vma->vm_ops =  &mlx4_ib_vm_ops;
1138 }
1139 
1140 static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
1141 {
1142 	struct mlx4_ib_dev *dev = to_mdev(context->device);
1143 	struct mlx4_ib_ucontext *mucontext = to_mucontext(context);
1144 
1145 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1146 		return -EINVAL;
1147 
1148 	if (vma->vm_pgoff == 0) {
1149 		/* We prevent double mmaping on same context */
1150 		if (mucontext->hw_bar_info[HW_BAR_DB].vma)
1151 			return -EINVAL;
1152 
1153 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1154 
1155 		if (io_remap_pfn_range(vma, vma->vm_start,
1156 				       to_mucontext(context)->uar.pfn,
1157 				       PAGE_SIZE, vma->vm_page_prot))
1158 			return -EAGAIN;
1159 
1160 		mlx4_ib_set_vma_data(vma, &mucontext->hw_bar_info[HW_BAR_DB]);
1161 
1162 	} else if (vma->vm_pgoff == 1 && dev->dev->caps.bf_reg_size != 0) {
1163 		/* We prevent double mmaping on same context */
1164 		if (mucontext->hw_bar_info[HW_BAR_BF].vma)
1165 			return -EINVAL;
1166 
1167 		vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1168 
1169 		if (io_remap_pfn_range(vma, vma->vm_start,
1170 				       to_mucontext(context)->uar.pfn +
1171 				       dev->dev->caps.num_uars,
1172 				       PAGE_SIZE, vma->vm_page_prot))
1173 			return -EAGAIN;
1174 
1175 		mlx4_ib_set_vma_data(vma, &mucontext->hw_bar_info[HW_BAR_BF]);
1176 
1177 	} else if (vma->vm_pgoff == 3) {
1178 		struct mlx4_clock_params params;
1179 		int ret;
1180 
1181 		/* We prevent double mmaping on same context */
1182 		if (mucontext->hw_bar_info[HW_BAR_CLOCK].vma)
1183 			return -EINVAL;
1184 
1185 		ret = mlx4_get_internal_clock_params(dev->dev, &params);
1186 
1187 		if (ret)
1188 			return ret;
1189 
1190 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1191 		if (io_remap_pfn_range(vma, vma->vm_start,
1192 				       (pci_resource_start(dev->dev->persist->pdev,
1193 							   params.bar) +
1194 					params.offset)
1195 				       >> PAGE_SHIFT,
1196 				       PAGE_SIZE, vma->vm_page_prot))
1197 			return -EAGAIN;
1198 
1199 		mlx4_ib_set_vma_data(vma,
1200 				     &mucontext->hw_bar_info[HW_BAR_CLOCK]);
1201 	} else {
1202 		return -EINVAL;
1203 	}
1204 
1205 	return 0;
1206 }
1207 
1208 static struct ib_pd *mlx4_ib_alloc_pd(struct ib_device *ibdev,
1209 				      struct ib_ucontext *context,
1210 				      struct ib_udata *udata)
1211 {
1212 	struct mlx4_ib_pd *pd;
1213 	int err;
1214 
1215 	pd = kmalloc(sizeof *pd, GFP_KERNEL);
1216 	if (!pd)
1217 		return ERR_PTR(-ENOMEM);
1218 
1219 	err = mlx4_pd_alloc(to_mdev(ibdev)->dev, &pd->pdn);
1220 	if (err) {
1221 		kfree(pd);
1222 		return ERR_PTR(err);
1223 	}
1224 
1225 	if (context)
1226 		if (ib_copy_to_udata(udata, &pd->pdn, sizeof (__u32))) {
1227 			mlx4_pd_free(to_mdev(ibdev)->dev, pd->pdn);
1228 			kfree(pd);
1229 			return ERR_PTR(-EFAULT);
1230 		}
1231 
1232 	return &pd->ibpd;
1233 }
1234 
1235 static int mlx4_ib_dealloc_pd(struct ib_pd *pd)
1236 {
1237 	mlx4_pd_free(to_mdev(pd->device)->dev, to_mpd(pd)->pdn);
1238 	kfree(pd);
1239 
1240 	return 0;
1241 }
1242 
1243 static struct ib_xrcd *mlx4_ib_alloc_xrcd(struct ib_device *ibdev,
1244 					  struct ib_ucontext *context,
1245 					  struct ib_udata *udata)
1246 {
1247 	struct mlx4_ib_xrcd *xrcd;
1248 	struct ib_cq_init_attr cq_attr = {};
1249 	int err;
1250 
1251 	if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC))
1252 		return ERR_PTR(-ENOSYS);
1253 
1254 	xrcd = kmalloc(sizeof *xrcd, GFP_KERNEL);
1255 	if (!xrcd)
1256 		return ERR_PTR(-ENOMEM);
1257 
1258 	err = mlx4_xrcd_alloc(to_mdev(ibdev)->dev, &xrcd->xrcdn);
1259 	if (err)
1260 		goto err1;
1261 
1262 	xrcd->pd = ib_alloc_pd(ibdev);
1263 	if (IS_ERR(xrcd->pd)) {
1264 		err = PTR_ERR(xrcd->pd);
1265 		goto err2;
1266 	}
1267 
1268 	cq_attr.cqe = 1;
1269 	xrcd->cq = ib_create_cq(ibdev, NULL, NULL, xrcd, &cq_attr);
1270 	if (IS_ERR(xrcd->cq)) {
1271 		err = PTR_ERR(xrcd->cq);
1272 		goto err3;
1273 	}
1274 
1275 	return &xrcd->ibxrcd;
1276 
1277 err3:
1278 	ib_dealloc_pd(xrcd->pd);
1279 err2:
1280 	mlx4_xrcd_free(to_mdev(ibdev)->dev, xrcd->xrcdn);
1281 err1:
1282 	kfree(xrcd);
1283 	return ERR_PTR(err);
1284 }
1285 
1286 static int mlx4_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
1287 {
1288 	ib_destroy_cq(to_mxrcd(xrcd)->cq);
1289 	ib_dealloc_pd(to_mxrcd(xrcd)->pd);
1290 	mlx4_xrcd_free(to_mdev(xrcd->device)->dev, to_mxrcd(xrcd)->xrcdn);
1291 	kfree(xrcd);
1292 
1293 	return 0;
1294 }
1295 
1296 static int add_gid_entry(struct ib_qp *ibqp, union ib_gid *gid)
1297 {
1298 	struct mlx4_ib_qp *mqp = to_mqp(ibqp);
1299 	struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
1300 	struct mlx4_ib_gid_entry *ge;
1301 
1302 	ge = kzalloc(sizeof *ge, GFP_KERNEL);
1303 	if (!ge)
1304 		return -ENOMEM;
1305 
1306 	ge->gid = *gid;
1307 	if (mlx4_ib_add_mc(mdev, mqp, gid)) {
1308 		ge->port = mqp->port;
1309 		ge->added = 1;
1310 	}
1311 
1312 	mutex_lock(&mqp->mutex);
1313 	list_add_tail(&ge->list, &mqp->gid_list);
1314 	mutex_unlock(&mqp->mutex);
1315 
1316 	return 0;
1317 }
1318 
1319 static void mlx4_ib_delete_counters_table(struct mlx4_ib_dev *ibdev,
1320 					  struct mlx4_ib_counters *ctr_table)
1321 {
1322 	struct counter_index *counter, *tmp_count;
1323 
1324 	mutex_lock(&ctr_table->mutex);
1325 	list_for_each_entry_safe(counter, tmp_count, &ctr_table->counters_list,
1326 				 list) {
1327 		if (counter->allocated)
1328 			mlx4_counter_free(ibdev->dev, counter->index);
1329 		list_del(&counter->list);
1330 		kfree(counter);
1331 	}
1332 	mutex_unlock(&ctr_table->mutex);
1333 }
1334 
1335 int mlx4_ib_add_mc(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
1336 		   union ib_gid *gid)
1337 {
1338 	struct net_device *ndev;
1339 	int ret = 0;
1340 
1341 	if (!mqp->port)
1342 		return 0;
1343 
1344 	spin_lock_bh(&mdev->iboe.lock);
1345 	ndev = mdev->iboe.netdevs[mqp->port - 1];
1346 	if (ndev)
1347 		dev_hold(ndev);
1348 	spin_unlock_bh(&mdev->iboe.lock);
1349 
1350 	if (ndev) {
1351 		ret = 1;
1352 		dev_put(ndev);
1353 	}
1354 
1355 	return ret;
1356 }
1357 
1358 struct mlx4_ib_steering {
1359 	struct list_head list;
1360 	struct mlx4_flow_reg_id reg_id;
1361 	union ib_gid gid;
1362 };
1363 
1364 static int parse_flow_attr(struct mlx4_dev *dev,
1365 			   u32 qp_num,
1366 			   union ib_flow_spec *ib_spec,
1367 			   struct _rule_hw *mlx4_spec)
1368 {
1369 	enum mlx4_net_trans_rule_id type;
1370 
1371 	switch (ib_spec->type) {
1372 	case IB_FLOW_SPEC_ETH:
1373 		type = MLX4_NET_TRANS_RULE_ID_ETH;
1374 		memcpy(mlx4_spec->eth.dst_mac, ib_spec->eth.val.dst_mac,
1375 		       ETH_ALEN);
1376 		memcpy(mlx4_spec->eth.dst_mac_msk, ib_spec->eth.mask.dst_mac,
1377 		       ETH_ALEN);
1378 		mlx4_spec->eth.vlan_tag = ib_spec->eth.val.vlan_tag;
1379 		mlx4_spec->eth.vlan_tag_msk = ib_spec->eth.mask.vlan_tag;
1380 		break;
1381 	case IB_FLOW_SPEC_IB:
1382 		type = MLX4_NET_TRANS_RULE_ID_IB;
1383 		mlx4_spec->ib.l3_qpn =
1384 			cpu_to_be32(qp_num);
1385 		mlx4_spec->ib.qpn_mask =
1386 			cpu_to_be32(MLX4_IB_FLOW_QPN_MASK);
1387 		break;
1388 
1389 
1390 	case IB_FLOW_SPEC_IPV4:
1391 		type = MLX4_NET_TRANS_RULE_ID_IPV4;
1392 		mlx4_spec->ipv4.src_ip = ib_spec->ipv4.val.src_ip;
1393 		mlx4_spec->ipv4.src_ip_msk = ib_spec->ipv4.mask.src_ip;
1394 		mlx4_spec->ipv4.dst_ip = ib_spec->ipv4.val.dst_ip;
1395 		mlx4_spec->ipv4.dst_ip_msk = ib_spec->ipv4.mask.dst_ip;
1396 		break;
1397 
1398 	case IB_FLOW_SPEC_TCP:
1399 	case IB_FLOW_SPEC_UDP:
1400 		type = ib_spec->type == IB_FLOW_SPEC_TCP ?
1401 					MLX4_NET_TRANS_RULE_ID_TCP :
1402 					MLX4_NET_TRANS_RULE_ID_UDP;
1403 		mlx4_spec->tcp_udp.dst_port = ib_spec->tcp_udp.val.dst_port;
1404 		mlx4_spec->tcp_udp.dst_port_msk = ib_spec->tcp_udp.mask.dst_port;
1405 		mlx4_spec->tcp_udp.src_port = ib_spec->tcp_udp.val.src_port;
1406 		mlx4_spec->tcp_udp.src_port_msk = ib_spec->tcp_udp.mask.src_port;
1407 		break;
1408 
1409 	default:
1410 		return -EINVAL;
1411 	}
1412 	if (mlx4_map_sw_to_hw_steering_id(dev, type) < 0 ||
1413 	    mlx4_hw_rule_sz(dev, type) < 0)
1414 		return -EINVAL;
1415 	mlx4_spec->id = cpu_to_be16(mlx4_map_sw_to_hw_steering_id(dev, type));
1416 	mlx4_spec->size = mlx4_hw_rule_sz(dev, type) >> 2;
1417 	return mlx4_hw_rule_sz(dev, type);
1418 }
1419 
1420 struct default_rules {
1421 	__u32 mandatory_fields[IB_FLOW_SPEC_SUPPORT_LAYERS];
1422 	__u32 mandatory_not_fields[IB_FLOW_SPEC_SUPPORT_LAYERS];
1423 	__u32 rules_create_list[IB_FLOW_SPEC_SUPPORT_LAYERS];
1424 	__u8  link_layer;
1425 };
1426 static const struct default_rules default_table[] = {
1427 	{
1428 		.mandatory_fields = {IB_FLOW_SPEC_IPV4},
1429 		.mandatory_not_fields = {IB_FLOW_SPEC_ETH},
1430 		.rules_create_list = {IB_FLOW_SPEC_IB},
1431 		.link_layer = IB_LINK_LAYER_INFINIBAND
1432 	}
1433 };
1434 
1435 static int __mlx4_ib_default_rules_match(struct ib_qp *qp,
1436 					 struct ib_flow_attr *flow_attr)
1437 {
1438 	int i, j, k;
1439 	void *ib_flow;
1440 	const struct default_rules *pdefault_rules = default_table;
1441 	u8 link_layer = rdma_port_get_link_layer(qp->device, flow_attr->port);
1442 
1443 	for (i = 0; i < ARRAY_SIZE(default_table); i++, pdefault_rules++) {
1444 		__u32 field_types[IB_FLOW_SPEC_SUPPORT_LAYERS];
1445 		memset(&field_types, 0, sizeof(field_types));
1446 
1447 		if (link_layer != pdefault_rules->link_layer)
1448 			continue;
1449 
1450 		ib_flow = flow_attr + 1;
1451 		/* we assume the specs are sorted */
1452 		for (j = 0, k = 0; k < IB_FLOW_SPEC_SUPPORT_LAYERS &&
1453 		     j < flow_attr->num_of_specs; k++) {
1454 			union ib_flow_spec *current_flow =
1455 				(union ib_flow_spec *)ib_flow;
1456 
1457 			/* same layer but different type */
1458 			if (((current_flow->type & IB_FLOW_SPEC_LAYER_MASK) ==
1459 			     (pdefault_rules->mandatory_fields[k] &
1460 			      IB_FLOW_SPEC_LAYER_MASK)) &&
1461 			    (current_flow->type !=
1462 			     pdefault_rules->mandatory_fields[k]))
1463 				goto out;
1464 
1465 			/* same layer, try match next one */
1466 			if (current_flow->type ==
1467 			    pdefault_rules->mandatory_fields[k]) {
1468 				j++;
1469 				ib_flow +=
1470 					((union ib_flow_spec *)ib_flow)->size;
1471 			}
1472 		}
1473 
1474 		ib_flow = flow_attr + 1;
1475 		for (j = 0; j < flow_attr->num_of_specs;
1476 		     j++, ib_flow += ((union ib_flow_spec *)ib_flow)->size)
1477 			for (k = 0; k < IB_FLOW_SPEC_SUPPORT_LAYERS; k++)
1478 				/* same layer and same type */
1479 				if (((union ib_flow_spec *)ib_flow)->type ==
1480 				    pdefault_rules->mandatory_not_fields[k])
1481 					goto out;
1482 
1483 		return i;
1484 	}
1485 out:
1486 	return -1;
1487 }
1488 
1489 static int __mlx4_ib_create_default_rules(
1490 		struct mlx4_ib_dev *mdev,
1491 		struct ib_qp *qp,
1492 		const struct default_rules *pdefault_rules,
1493 		struct _rule_hw *mlx4_spec) {
1494 	int size = 0;
1495 	int i;
1496 
1497 	for (i = 0; i < ARRAY_SIZE(pdefault_rules->rules_create_list); i++) {
1498 		int ret;
1499 		union ib_flow_spec ib_spec;
1500 		switch (pdefault_rules->rules_create_list[i]) {
1501 		case 0:
1502 			/* no rule */
1503 			continue;
1504 		case IB_FLOW_SPEC_IB:
1505 			ib_spec.type = IB_FLOW_SPEC_IB;
1506 			ib_spec.size = sizeof(struct ib_flow_spec_ib);
1507 
1508 			break;
1509 		default:
1510 			/* invalid rule */
1511 			return -EINVAL;
1512 		}
1513 		/* We must put empty rule, qpn is being ignored */
1514 		ret = parse_flow_attr(mdev->dev, 0, &ib_spec,
1515 				      mlx4_spec);
1516 		if (ret < 0) {
1517 			pr_info("invalid parsing\n");
1518 			return -EINVAL;
1519 		}
1520 
1521 		mlx4_spec = (void *)mlx4_spec + ret;
1522 		size += ret;
1523 	}
1524 	return size;
1525 }
1526 
1527 static int __mlx4_ib_create_flow(struct ib_qp *qp, struct ib_flow_attr *flow_attr,
1528 			  int domain,
1529 			  enum mlx4_net_trans_promisc_mode flow_type,
1530 			  u64 *reg_id)
1531 {
1532 	int ret, i;
1533 	int size = 0;
1534 	void *ib_flow;
1535 	struct mlx4_ib_dev *mdev = to_mdev(qp->device);
1536 	struct mlx4_cmd_mailbox *mailbox;
1537 	struct mlx4_net_trans_rule_hw_ctrl *ctrl;
1538 	int default_flow;
1539 
1540 	static const u16 __mlx4_domain[] = {
1541 		[IB_FLOW_DOMAIN_USER] = MLX4_DOMAIN_UVERBS,
1542 		[IB_FLOW_DOMAIN_ETHTOOL] = MLX4_DOMAIN_ETHTOOL,
1543 		[IB_FLOW_DOMAIN_RFS] = MLX4_DOMAIN_RFS,
1544 		[IB_FLOW_DOMAIN_NIC] = MLX4_DOMAIN_NIC,
1545 	};
1546 
1547 	if (flow_attr->priority > MLX4_IB_FLOW_MAX_PRIO) {
1548 		pr_err("Invalid priority value %d\n", flow_attr->priority);
1549 		return -EINVAL;
1550 	}
1551 
1552 	if (domain >= IB_FLOW_DOMAIN_NUM) {
1553 		pr_err("Invalid domain value %d\n", domain);
1554 		return -EINVAL;
1555 	}
1556 
1557 	if (mlx4_map_sw_to_hw_steering_mode(mdev->dev, flow_type) < 0)
1558 		return -EINVAL;
1559 
1560 	mailbox = mlx4_alloc_cmd_mailbox(mdev->dev);
1561 	if (IS_ERR(mailbox))
1562 		return PTR_ERR(mailbox);
1563 	ctrl = mailbox->buf;
1564 
1565 	ctrl->prio = cpu_to_be16(__mlx4_domain[domain] |
1566 				 flow_attr->priority);
1567 	ctrl->type = mlx4_map_sw_to_hw_steering_mode(mdev->dev, flow_type);
1568 	ctrl->port = flow_attr->port;
1569 	ctrl->qpn = cpu_to_be32(qp->qp_num);
1570 
1571 	ib_flow = flow_attr + 1;
1572 	size += sizeof(struct mlx4_net_trans_rule_hw_ctrl);
1573 	/* Add default flows */
1574 	default_flow = __mlx4_ib_default_rules_match(qp, flow_attr);
1575 	if (default_flow >= 0) {
1576 		ret = __mlx4_ib_create_default_rules(
1577 				mdev, qp, default_table + default_flow,
1578 				mailbox->buf + size);
1579 		if (ret < 0) {
1580 			mlx4_free_cmd_mailbox(mdev->dev, mailbox);
1581 			return -EINVAL;
1582 		}
1583 		size += ret;
1584 	}
1585 	for (i = 0; i < flow_attr->num_of_specs; i++) {
1586 		ret = parse_flow_attr(mdev->dev, qp->qp_num, ib_flow,
1587 				      mailbox->buf + size);
1588 		if (ret < 0) {
1589 			mlx4_free_cmd_mailbox(mdev->dev, mailbox);
1590 			return -EINVAL;
1591 		}
1592 		ib_flow += ((union ib_flow_spec *) ib_flow)->size;
1593 		size += ret;
1594 	}
1595 
1596 	ret = mlx4_cmd_imm(mdev->dev, mailbox->dma, reg_id, size >> 2, 0,
1597 			   MLX4_QP_FLOW_STEERING_ATTACH, MLX4_CMD_TIME_CLASS_A,
1598 			   MLX4_CMD_WRAPPED);
1599 	if (ret == -ENOMEM)
1600 		pr_err("mcg table is full. Fail to register network rule.\n");
1601 	else if (ret == -ENXIO)
1602 		pr_err("Device managed flow steering is disabled. Fail to register network rule.\n");
1603 	else if (ret)
1604 		pr_err("Invalid argumant. Fail to register network rule.\n");
1605 
1606 	mlx4_free_cmd_mailbox(mdev->dev, mailbox);
1607 	return ret;
1608 }
1609 
1610 static int __mlx4_ib_destroy_flow(struct mlx4_dev *dev, u64 reg_id)
1611 {
1612 	int err;
1613 	err = mlx4_cmd(dev, reg_id, 0, 0,
1614 		       MLX4_QP_FLOW_STEERING_DETACH, MLX4_CMD_TIME_CLASS_A,
1615 		       MLX4_CMD_WRAPPED);
1616 	if (err)
1617 		pr_err("Fail to detach network rule. registration id = 0x%llx\n",
1618 		       reg_id);
1619 	return err;
1620 }
1621 
1622 static int mlx4_ib_tunnel_steer_add(struct ib_qp *qp, struct ib_flow_attr *flow_attr,
1623 				    u64 *reg_id)
1624 {
1625 	void *ib_flow;
1626 	union ib_flow_spec *ib_spec;
1627 	struct mlx4_dev	*dev = to_mdev(qp->device)->dev;
1628 	int err = 0;
1629 
1630 	if (dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN ||
1631 	    dev->caps.dmfs_high_steer_mode == MLX4_STEERING_DMFS_A0_STATIC)
1632 		return 0; /* do nothing */
1633 
1634 	ib_flow = flow_attr + 1;
1635 	ib_spec = (union ib_flow_spec *)ib_flow;
1636 
1637 	if (ib_spec->type !=  IB_FLOW_SPEC_ETH || flow_attr->num_of_specs != 1)
1638 		return 0; /* do nothing */
1639 
1640 	err = mlx4_tunnel_steer_add(to_mdev(qp->device)->dev, ib_spec->eth.val.dst_mac,
1641 				    flow_attr->port, qp->qp_num,
1642 				    MLX4_DOMAIN_UVERBS | (flow_attr->priority & 0xff),
1643 				    reg_id);
1644 	return err;
1645 }
1646 
1647 static int mlx4_ib_add_dont_trap_rule(struct mlx4_dev *dev,
1648 				      struct ib_flow_attr *flow_attr,
1649 				      enum mlx4_net_trans_promisc_mode *type)
1650 {
1651 	int err = 0;
1652 
1653 	if (!(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_DMFS_UC_MC_SNIFFER) ||
1654 	    (dev->caps.dmfs_high_steer_mode == MLX4_STEERING_DMFS_A0_STATIC) ||
1655 	    (flow_attr->num_of_specs > 1) || (flow_attr->priority != 0)) {
1656 		return -EOPNOTSUPP;
1657 	}
1658 
1659 	if (flow_attr->num_of_specs == 0) {
1660 		type[0] = MLX4_FS_MC_SNIFFER;
1661 		type[1] = MLX4_FS_UC_SNIFFER;
1662 	} else {
1663 		union ib_flow_spec *ib_spec;
1664 
1665 		ib_spec = (union ib_flow_spec *)(flow_attr + 1);
1666 		if (ib_spec->type !=  IB_FLOW_SPEC_ETH)
1667 			return -EINVAL;
1668 
1669 		/* if all is zero than MC and UC */
1670 		if (is_zero_ether_addr(ib_spec->eth.mask.dst_mac)) {
1671 			type[0] = MLX4_FS_MC_SNIFFER;
1672 			type[1] = MLX4_FS_UC_SNIFFER;
1673 		} else {
1674 			u8 mac[ETH_ALEN] = {ib_spec->eth.mask.dst_mac[0] ^ 0x01,
1675 					    ib_spec->eth.mask.dst_mac[1],
1676 					    ib_spec->eth.mask.dst_mac[2],
1677 					    ib_spec->eth.mask.dst_mac[3],
1678 					    ib_spec->eth.mask.dst_mac[4],
1679 					    ib_spec->eth.mask.dst_mac[5]};
1680 
1681 			/* Above xor was only on MC bit, non empty mask is valid
1682 			 * only if this bit is set and rest are zero.
1683 			 */
1684 			if (!is_zero_ether_addr(&mac[0]))
1685 				return -EINVAL;
1686 
1687 			if (is_multicast_ether_addr(ib_spec->eth.val.dst_mac))
1688 				type[0] = MLX4_FS_MC_SNIFFER;
1689 			else
1690 				type[0] = MLX4_FS_UC_SNIFFER;
1691 		}
1692 	}
1693 
1694 	return err;
1695 }
1696 
1697 static struct ib_flow *mlx4_ib_create_flow(struct ib_qp *qp,
1698 				    struct ib_flow_attr *flow_attr,
1699 				    int domain)
1700 {
1701 	int err = 0, i = 0, j = 0;
1702 	struct mlx4_ib_flow *mflow;
1703 	enum mlx4_net_trans_promisc_mode type[2];
1704 	struct mlx4_dev *dev = (to_mdev(qp->device))->dev;
1705 	int is_bonded = mlx4_is_bonded(dev);
1706 
1707 	if ((flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) &&
1708 	    (flow_attr->type != IB_FLOW_ATTR_NORMAL))
1709 		return ERR_PTR(-EOPNOTSUPP);
1710 
1711 	memset(type, 0, sizeof(type));
1712 
1713 	mflow = kzalloc(sizeof(*mflow), GFP_KERNEL);
1714 	if (!mflow) {
1715 		err = -ENOMEM;
1716 		goto err_free;
1717 	}
1718 
1719 	switch (flow_attr->type) {
1720 	case IB_FLOW_ATTR_NORMAL:
1721 		/* If dont trap flag (continue match) is set, under specific
1722 		 * condition traffic be replicated to given qp,
1723 		 * without stealing it
1724 		 */
1725 		if (unlikely(flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)) {
1726 			err = mlx4_ib_add_dont_trap_rule(dev,
1727 							 flow_attr,
1728 							 type);
1729 			if (err)
1730 				goto err_free;
1731 		} else {
1732 			type[0] = MLX4_FS_REGULAR;
1733 		}
1734 		break;
1735 
1736 	case IB_FLOW_ATTR_ALL_DEFAULT:
1737 		type[0] = MLX4_FS_ALL_DEFAULT;
1738 		break;
1739 
1740 	case IB_FLOW_ATTR_MC_DEFAULT:
1741 		type[0] = MLX4_FS_MC_DEFAULT;
1742 		break;
1743 
1744 	case IB_FLOW_ATTR_SNIFFER:
1745 		type[0] = MLX4_FS_MIRROR_RX_PORT;
1746 		type[1] = MLX4_FS_MIRROR_SX_PORT;
1747 		break;
1748 
1749 	default:
1750 		err = -EINVAL;
1751 		goto err_free;
1752 	}
1753 
1754 	while (i < ARRAY_SIZE(type) && type[i]) {
1755 		err = __mlx4_ib_create_flow(qp, flow_attr, domain, type[i],
1756 					    &mflow->reg_id[i].id);
1757 		if (err)
1758 			goto err_create_flow;
1759 		if (is_bonded) {
1760 			/* Application always sees one port so the mirror rule
1761 			 * must be on port #2
1762 			 */
1763 			flow_attr->port = 2;
1764 			err = __mlx4_ib_create_flow(qp, flow_attr,
1765 						    domain, type[j],
1766 						    &mflow->reg_id[j].mirror);
1767 			flow_attr->port = 1;
1768 			if (err)
1769 				goto err_create_flow;
1770 			j++;
1771 		}
1772 
1773 		i++;
1774 	}
1775 
1776 	if (i < ARRAY_SIZE(type) && flow_attr->type == IB_FLOW_ATTR_NORMAL) {
1777 		err = mlx4_ib_tunnel_steer_add(qp, flow_attr,
1778 					       &mflow->reg_id[i].id);
1779 		if (err)
1780 			goto err_create_flow;
1781 
1782 		if (is_bonded) {
1783 			flow_attr->port = 2;
1784 			err = mlx4_ib_tunnel_steer_add(qp, flow_attr,
1785 						       &mflow->reg_id[j].mirror);
1786 			flow_attr->port = 1;
1787 			if (err)
1788 				goto err_create_flow;
1789 			j++;
1790 		}
1791 		/* function to create mirror rule */
1792 		i++;
1793 	}
1794 
1795 	return &mflow->ibflow;
1796 
1797 err_create_flow:
1798 	while (i) {
1799 		(void)__mlx4_ib_destroy_flow(to_mdev(qp->device)->dev,
1800 					     mflow->reg_id[i].id);
1801 		i--;
1802 	}
1803 
1804 	while (j) {
1805 		(void)__mlx4_ib_destroy_flow(to_mdev(qp->device)->dev,
1806 					     mflow->reg_id[j].mirror);
1807 		j--;
1808 	}
1809 err_free:
1810 	kfree(mflow);
1811 	return ERR_PTR(err);
1812 }
1813 
1814 static int mlx4_ib_destroy_flow(struct ib_flow *flow_id)
1815 {
1816 	int err, ret = 0;
1817 	int i = 0;
1818 	struct mlx4_ib_dev *mdev = to_mdev(flow_id->qp->device);
1819 	struct mlx4_ib_flow *mflow = to_mflow(flow_id);
1820 
1821 	while (i < ARRAY_SIZE(mflow->reg_id) && mflow->reg_id[i].id) {
1822 		err = __mlx4_ib_destroy_flow(mdev->dev, mflow->reg_id[i].id);
1823 		if (err)
1824 			ret = err;
1825 		if (mflow->reg_id[i].mirror) {
1826 			err = __mlx4_ib_destroy_flow(mdev->dev,
1827 						     mflow->reg_id[i].mirror);
1828 			if (err)
1829 				ret = err;
1830 		}
1831 		i++;
1832 	}
1833 
1834 	kfree(mflow);
1835 	return ret;
1836 }
1837 
1838 static int mlx4_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
1839 {
1840 	int err;
1841 	struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
1842 	struct mlx4_dev	*dev = mdev->dev;
1843 	struct mlx4_ib_qp *mqp = to_mqp(ibqp);
1844 	struct mlx4_ib_steering *ib_steering = NULL;
1845 	enum mlx4_protocol prot = MLX4_PROT_IB_IPV6;
1846 	struct mlx4_flow_reg_id	reg_id;
1847 
1848 	if (mdev->dev->caps.steering_mode ==
1849 	    MLX4_STEERING_MODE_DEVICE_MANAGED) {
1850 		ib_steering = kmalloc(sizeof(*ib_steering), GFP_KERNEL);
1851 		if (!ib_steering)
1852 			return -ENOMEM;
1853 	}
1854 
1855 	err = mlx4_multicast_attach(mdev->dev, &mqp->mqp, gid->raw, mqp->port,
1856 				    !!(mqp->flags &
1857 				       MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK),
1858 				    prot, &reg_id.id);
1859 	if (err) {
1860 		pr_err("multicast attach op failed, err %d\n", err);
1861 		goto err_malloc;
1862 	}
1863 
1864 	reg_id.mirror = 0;
1865 	if (mlx4_is_bonded(dev)) {
1866 		err = mlx4_multicast_attach(mdev->dev, &mqp->mqp, gid->raw,
1867 					    (mqp->port == 1) ? 2 : 1,
1868 					    !!(mqp->flags &
1869 					    MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK),
1870 					    prot, &reg_id.mirror);
1871 		if (err)
1872 			goto err_add;
1873 	}
1874 
1875 	err = add_gid_entry(ibqp, gid);
1876 	if (err)
1877 		goto err_add;
1878 
1879 	if (ib_steering) {
1880 		memcpy(ib_steering->gid.raw, gid->raw, 16);
1881 		ib_steering->reg_id = reg_id;
1882 		mutex_lock(&mqp->mutex);
1883 		list_add(&ib_steering->list, &mqp->steering_rules);
1884 		mutex_unlock(&mqp->mutex);
1885 	}
1886 	return 0;
1887 
1888 err_add:
1889 	mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
1890 			      prot, reg_id.id);
1891 	if (reg_id.mirror)
1892 		mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
1893 				      prot, reg_id.mirror);
1894 err_malloc:
1895 	kfree(ib_steering);
1896 
1897 	return err;
1898 }
1899 
1900 static struct mlx4_ib_gid_entry *find_gid_entry(struct mlx4_ib_qp *qp, u8 *raw)
1901 {
1902 	struct mlx4_ib_gid_entry *ge;
1903 	struct mlx4_ib_gid_entry *tmp;
1904 	struct mlx4_ib_gid_entry *ret = NULL;
1905 
1906 	list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
1907 		if (!memcmp(raw, ge->gid.raw, 16)) {
1908 			ret = ge;
1909 			break;
1910 		}
1911 	}
1912 
1913 	return ret;
1914 }
1915 
1916 static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
1917 {
1918 	int err;
1919 	struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
1920 	struct mlx4_dev *dev = mdev->dev;
1921 	struct mlx4_ib_qp *mqp = to_mqp(ibqp);
1922 	struct net_device *ndev;
1923 	struct mlx4_ib_gid_entry *ge;
1924 	struct mlx4_flow_reg_id reg_id = {0, 0};
1925 	enum mlx4_protocol prot =  MLX4_PROT_IB_IPV6;
1926 
1927 	if (mdev->dev->caps.steering_mode ==
1928 	    MLX4_STEERING_MODE_DEVICE_MANAGED) {
1929 		struct mlx4_ib_steering *ib_steering;
1930 
1931 		mutex_lock(&mqp->mutex);
1932 		list_for_each_entry(ib_steering, &mqp->steering_rules, list) {
1933 			if (!memcmp(ib_steering->gid.raw, gid->raw, 16)) {
1934 				list_del(&ib_steering->list);
1935 				break;
1936 			}
1937 		}
1938 		mutex_unlock(&mqp->mutex);
1939 		if (&ib_steering->list == &mqp->steering_rules) {
1940 			pr_err("Couldn't find reg_id for mgid. Steering rule is left attached\n");
1941 			return -EINVAL;
1942 		}
1943 		reg_id = ib_steering->reg_id;
1944 		kfree(ib_steering);
1945 	}
1946 
1947 	err = mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
1948 				    prot, reg_id.id);
1949 	if (err)
1950 		return err;
1951 
1952 	if (mlx4_is_bonded(dev)) {
1953 		err = mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
1954 					    prot, reg_id.mirror);
1955 		if (err)
1956 			return err;
1957 	}
1958 
1959 	mutex_lock(&mqp->mutex);
1960 	ge = find_gid_entry(mqp, gid->raw);
1961 	if (ge) {
1962 		spin_lock_bh(&mdev->iboe.lock);
1963 		ndev = ge->added ? mdev->iboe.netdevs[ge->port - 1] : NULL;
1964 		if (ndev)
1965 			dev_hold(ndev);
1966 		spin_unlock_bh(&mdev->iboe.lock);
1967 		if (ndev)
1968 			dev_put(ndev);
1969 		list_del(&ge->list);
1970 		kfree(ge);
1971 	} else
1972 		pr_warn("could not find mgid entry\n");
1973 
1974 	mutex_unlock(&mqp->mutex);
1975 
1976 	return 0;
1977 }
1978 
1979 static int init_node_data(struct mlx4_ib_dev *dev)
1980 {
1981 	struct ib_smp *in_mad  = NULL;
1982 	struct ib_smp *out_mad = NULL;
1983 	int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
1984 	int err = -ENOMEM;
1985 
1986 	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
1987 	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
1988 	if (!in_mad || !out_mad)
1989 		goto out;
1990 
1991 	init_query_mad(in_mad);
1992 	in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
1993 	if (mlx4_is_master(dev->dev))
1994 		mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
1995 
1996 	err = mlx4_MAD_IFC(dev, mad_ifc_flags, 1, NULL, NULL, in_mad, out_mad);
1997 	if (err)
1998 		goto out;
1999 
2000 	memcpy(dev->ib_dev.node_desc, out_mad->data, 64);
2001 
2002 	in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
2003 
2004 	err = mlx4_MAD_IFC(dev, mad_ifc_flags, 1, NULL, NULL, in_mad, out_mad);
2005 	if (err)
2006 		goto out;
2007 
2008 	dev->dev->rev_id = be32_to_cpup((__be32 *) (out_mad->data + 32));
2009 	memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
2010 
2011 out:
2012 	kfree(in_mad);
2013 	kfree(out_mad);
2014 	return err;
2015 }
2016 
2017 static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2018 			char *buf)
2019 {
2020 	struct mlx4_ib_dev *dev =
2021 		container_of(device, struct mlx4_ib_dev, ib_dev.dev);
2022 	return sprintf(buf, "MT%d\n", dev->dev->persist->pdev->device);
2023 }
2024 
2025 static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
2026 			   char *buf)
2027 {
2028 	struct mlx4_ib_dev *dev =
2029 		container_of(device, struct mlx4_ib_dev, ib_dev.dev);
2030 	return sprintf(buf, "%d.%d.%d\n", (int) (dev->dev->caps.fw_ver >> 32),
2031 		       (int) (dev->dev->caps.fw_ver >> 16) & 0xffff,
2032 		       (int) dev->dev->caps.fw_ver & 0xffff);
2033 }
2034 
2035 static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2036 			char *buf)
2037 {
2038 	struct mlx4_ib_dev *dev =
2039 		container_of(device, struct mlx4_ib_dev, ib_dev.dev);
2040 	return sprintf(buf, "%x\n", dev->dev->rev_id);
2041 }
2042 
2043 static ssize_t show_board(struct device *device, struct device_attribute *attr,
2044 			  char *buf)
2045 {
2046 	struct mlx4_ib_dev *dev =
2047 		container_of(device, struct mlx4_ib_dev, ib_dev.dev);
2048 	return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN,
2049 		       dev->dev->board_id);
2050 }
2051 
2052 static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
2053 static DEVICE_ATTR(fw_ver,   S_IRUGO, show_fw_ver, NULL);
2054 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
2055 static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
2056 
2057 static struct device_attribute *mlx4_class_attributes[] = {
2058 	&dev_attr_hw_rev,
2059 	&dev_attr_fw_ver,
2060 	&dev_attr_hca_type,
2061 	&dev_attr_board_id
2062 };
2063 
2064 #define MLX4_IB_INVALID_MAC	((u64)-1)
2065 static void mlx4_ib_update_qps(struct mlx4_ib_dev *ibdev,
2066 			       struct net_device *dev,
2067 			       int port)
2068 {
2069 	u64 new_smac = 0;
2070 	u64 release_mac = MLX4_IB_INVALID_MAC;
2071 	struct mlx4_ib_qp *qp;
2072 
2073 	read_lock(&dev_base_lock);
2074 	new_smac = mlx4_mac_to_u64(dev->dev_addr);
2075 	read_unlock(&dev_base_lock);
2076 
2077 	atomic64_set(&ibdev->iboe.mac[port - 1], new_smac);
2078 
2079 	/* no need for update QP1 and mac registration in non-SRIOV */
2080 	if (!mlx4_is_mfunc(ibdev->dev))
2081 		return;
2082 
2083 	mutex_lock(&ibdev->qp1_proxy_lock[port - 1]);
2084 	qp = ibdev->qp1_proxy[port - 1];
2085 	if (qp) {
2086 		int new_smac_index;
2087 		u64 old_smac;
2088 		struct mlx4_update_qp_params update_params;
2089 
2090 		mutex_lock(&qp->mutex);
2091 		old_smac = qp->pri.smac;
2092 		if (new_smac == old_smac)
2093 			goto unlock;
2094 
2095 		new_smac_index = mlx4_register_mac(ibdev->dev, port, new_smac);
2096 
2097 		if (new_smac_index < 0)
2098 			goto unlock;
2099 
2100 		update_params.smac_index = new_smac_index;
2101 		if (mlx4_update_qp(ibdev->dev, qp->mqp.qpn, MLX4_UPDATE_QP_SMAC,
2102 				   &update_params)) {
2103 			release_mac = new_smac;
2104 			goto unlock;
2105 		}
2106 		/* if old port was zero, no mac was yet registered for this QP */
2107 		if (qp->pri.smac_port)
2108 			release_mac = old_smac;
2109 		qp->pri.smac = new_smac;
2110 		qp->pri.smac_port = port;
2111 		qp->pri.smac_index = new_smac_index;
2112 	}
2113 
2114 unlock:
2115 	if (release_mac != MLX4_IB_INVALID_MAC)
2116 		mlx4_unregister_mac(ibdev->dev, port, release_mac);
2117 	if (qp)
2118 		mutex_unlock(&qp->mutex);
2119 	mutex_unlock(&ibdev->qp1_proxy_lock[port - 1]);
2120 }
2121 
2122 static void mlx4_ib_scan_netdevs(struct mlx4_ib_dev *ibdev,
2123 				 struct net_device *dev,
2124 				 unsigned long event)
2125 
2126 {
2127 	struct mlx4_ib_iboe *iboe;
2128 	int update_qps_port = -1;
2129 	int port;
2130 
2131 	ASSERT_RTNL();
2132 
2133 	iboe = &ibdev->iboe;
2134 
2135 	spin_lock_bh(&iboe->lock);
2136 	mlx4_foreach_ib_transport_port(port, ibdev->dev) {
2137 
2138 		iboe->netdevs[port - 1] =
2139 			mlx4_get_protocol_dev(ibdev->dev, MLX4_PROT_ETH, port);
2140 
2141 		if (dev == iboe->netdevs[port - 1] &&
2142 		    (event == NETDEV_CHANGEADDR || event == NETDEV_REGISTER ||
2143 		     event == NETDEV_UP || event == NETDEV_CHANGE))
2144 			update_qps_port = port;
2145 
2146 	}
2147 	spin_unlock_bh(&iboe->lock);
2148 
2149 	if (update_qps_port > 0)
2150 		mlx4_ib_update_qps(ibdev, dev, update_qps_port);
2151 }
2152 
2153 static int mlx4_ib_netdev_event(struct notifier_block *this,
2154 				unsigned long event, void *ptr)
2155 {
2156 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2157 	struct mlx4_ib_dev *ibdev;
2158 
2159 	if (!net_eq(dev_net(dev), &init_net))
2160 		return NOTIFY_DONE;
2161 
2162 	ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb);
2163 	mlx4_ib_scan_netdevs(ibdev, dev, event);
2164 
2165 	return NOTIFY_DONE;
2166 }
2167 
2168 static void init_pkeys(struct mlx4_ib_dev *ibdev)
2169 {
2170 	int port;
2171 	int slave;
2172 	int i;
2173 
2174 	if (mlx4_is_master(ibdev->dev)) {
2175 		for (slave = 0; slave <= ibdev->dev->persist->num_vfs;
2176 		     ++slave) {
2177 			for (port = 1; port <= ibdev->dev->caps.num_ports; ++port) {
2178 				for (i = 0;
2179 				     i < ibdev->dev->phys_caps.pkey_phys_table_len[port];
2180 				     ++i) {
2181 					ibdev->pkeys.virt2phys_pkey[slave][port - 1][i] =
2182 					/* master has the identity virt2phys pkey mapping */
2183 						(slave == mlx4_master_func_num(ibdev->dev) || !i) ? i :
2184 							ibdev->dev->phys_caps.pkey_phys_table_len[port] - 1;
2185 					mlx4_sync_pkey_table(ibdev->dev, slave, port, i,
2186 							     ibdev->pkeys.virt2phys_pkey[slave][port - 1][i]);
2187 				}
2188 			}
2189 		}
2190 		/* initialize pkey cache */
2191 		for (port = 1; port <= ibdev->dev->caps.num_ports; ++port) {
2192 			for (i = 0;
2193 			     i < ibdev->dev->phys_caps.pkey_phys_table_len[port];
2194 			     ++i)
2195 				ibdev->pkeys.phys_pkey_cache[port-1][i] =
2196 					(i) ? 0 : 0xFFFF;
2197 		}
2198 	}
2199 }
2200 
2201 static void mlx4_ib_alloc_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev)
2202 {
2203 	int i, j, eq = 0, total_eqs = 0;
2204 
2205 	ibdev->eq_table = kcalloc(dev->caps.num_comp_vectors,
2206 				  sizeof(ibdev->eq_table[0]), GFP_KERNEL);
2207 	if (!ibdev->eq_table)
2208 		return;
2209 
2210 	for (i = 1; i <= dev->caps.num_ports; i++) {
2211 		for (j = 0; j < mlx4_get_eqs_per_port(dev, i);
2212 		     j++, total_eqs++) {
2213 			if (i > 1 &&  mlx4_is_eq_shared(dev, total_eqs))
2214 				continue;
2215 			ibdev->eq_table[eq] = total_eqs;
2216 			if (!mlx4_assign_eq(dev, i,
2217 					    &ibdev->eq_table[eq]))
2218 				eq++;
2219 			else
2220 				ibdev->eq_table[eq] = -1;
2221 		}
2222 	}
2223 
2224 	for (i = eq; i < dev->caps.num_comp_vectors;
2225 	     ibdev->eq_table[i++] = -1)
2226 		;
2227 
2228 	/* Advertise the new number of EQs to clients */
2229 	ibdev->ib_dev.num_comp_vectors = eq;
2230 }
2231 
2232 static void mlx4_ib_free_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev)
2233 {
2234 	int i;
2235 	int total_eqs = ibdev->ib_dev.num_comp_vectors;
2236 
2237 	/* no eqs were allocated */
2238 	if (!ibdev->eq_table)
2239 		return;
2240 
2241 	/* Reset the advertised EQ number */
2242 	ibdev->ib_dev.num_comp_vectors = 0;
2243 
2244 	for (i = 0; i < total_eqs; i++)
2245 		mlx4_release_eq(dev, ibdev->eq_table[i]);
2246 
2247 	kfree(ibdev->eq_table);
2248 	ibdev->eq_table = NULL;
2249 }
2250 
2251 static int mlx4_port_immutable(struct ib_device *ibdev, u8 port_num,
2252 			       struct ib_port_immutable *immutable)
2253 {
2254 	struct ib_port_attr attr;
2255 	struct mlx4_ib_dev *mdev = to_mdev(ibdev);
2256 	int err;
2257 
2258 	err = mlx4_ib_query_port(ibdev, port_num, &attr);
2259 	if (err)
2260 		return err;
2261 
2262 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
2263 	immutable->gid_tbl_len = attr.gid_tbl_len;
2264 
2265 	if (mlx4_ib_port_link_layer(ibdev, port_num) == IB_LINK_LAYER_INFINIBAND) {
2266 		immutable->core_cap_flags = RDMA_CORE_PORT_IBA_IB;
2267 	} else {
2268 		if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE)
2269 			immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
2270 		if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2)
2271 			immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE |
2272 				RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
2273 	}
2274 
2275 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
2276 
2277 	return 0;
2278 }
2279 
2280 static void *mlx4_ib_add(struct mlx4_dev *dev)
2281 {
2282 	struct mlx4_ib_dev *ibdev;
2283 	int num_ports = 0;
2284 	int i, j;
2285 	int err;
2286 	struct mlx4_ib_iboe *iboe;
2287 	int ib_num_ports = 0;
2288 	int num_req_counters;
2289 	int allocated;
2290 	u32 counter_index;
2291 	struct counter_index *new_counter_index = NULL;
2292 
2293 	pr_info_once("%s", mlx4_ib_version);
2294 
2295 	num_ports = 0;
2296 	mlx4_foreach_ib_transport_port(i, dev)
2297 		num_ports++;
2298 
2299 	/* No point in registering a device with no ports... */
2300 	if (num_ports == 0)
2301 		return NULL;
2302 
2303 	ibdev = (struct mlx4_ib_dev *) ib_alloc_device(sizeof *ibdev);
2304 	if (!ibdev) {
2305 		dev_err(&dev->persist->pdev->dev,
2306 			"Device struct alloc failed\n");
2307 		return NULL;
2308 	}
2309 
2310 	iboe = &ibdev->iboe;
2311 
2312 	if (mlx4_pd_alloc(dev, &ibdev->priv_pdn))
2313 		goto err_dealloc;
2314 
2315 	if (mlx4_uar_alloc(dev, &ibdev->priv_uar))
2316 		goto err_pd;
2317 
2318 	ibdev->uar_map = ioremap((phys_addr_t) ibdev->priv_uar.pfn << PAGE_SHIFT,
2319 				 PAGE_SIZE);
2320 	if (!ibdev->uar_map)
2321 		goto err_uar;
2322 	MLX4_INIT_DOORBELL_LOCK(&ibdev->uar_lock);
2323 
2324 	ibdev->dev = dev;
2325 	ibdev->bond_next_port	= 0;
2326 
2327 	strlcpy(ibdev->ib_dev.name, "mlx4_%d", IB_DEVICE_NAME_MAX);
2328 	ibdev->ib_dev.owner		= THIS_MODULE;
2329 	ibdev->ib_dev.node_type		= RDMA_NODE_IB_CA;
2330 	ibdev->ib_dev.local_dma_lkey	= dev->caps.reserved_lkey;
2331 	ibdev->num_ports		= num_ports;
2332 	ibdev->ib_dev.phys_port_cnt     = mlx4_is_bonded(dev) ?
2333 						1 : ibdev->num_ports;
2334 	ibdev->ib_dev.num_comp_vectors	= dev->caps.num_comp_vectors;
2335 	ibdev->ib_dev.dma_device	= &dev->persist->pdev->dev;
2336 	ibdev->ib_dev.get_netdev	= mlx4_ib_get_netdev;
2337 	ibdev->ib_dev.add_gid		= mlx4_ib_add_gid;
2338 	ibdev->ib_dev.del_gid		= mlx4_ib_del_gid;
2339 
2340 	if (dev->caps.userspace_caps)
2341 		ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_ABI_VERSION;
2342 	else
2343 		ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION;
2344 
2345 	ibdev->ib_dev.uverbs_cmd_mask	=
2346 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
2347 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
2348 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
2349 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
2350 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
2351 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
2352 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
2353 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
2354 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
2355 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
2356 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
2357 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
2358 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
2359 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
2360 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
2361 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
2362 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
2363 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
2364 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
2365 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
2366 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
2367 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
2368 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
2369 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
2370 
2371 	ibdev->ib_dev.query_device	= mlx4_ib_query_device;
2372 	ibdev->ib_dev.query_port	= mlx4_ib_query_port;
2373 	ibdev->ib_dev.get_link_layer	= mlx4_ib_port_link_layer;
2374 	ibdev->ib_dev.query_gid		= mlx4_ib_query_gid;
2375 	ibdev->ib_dev.query_pkey	= mlx4_ib_query_pkey;
2376 	ibdev->ib_dev.modify_device	= mlx4_ib_modify_device;
2377 	ibdev->ib_dev.modify_port	= mlx4_ib_modify_port;
2378 	ibdev->ib_dev.alloc_ucontext	= mlx4_ib_alloc_ucontext;
2379 	ibdev->ib_dev.dealloc_ucontext	= mlx4_ib_dealloc_ucontext;
2380 	ibdev->ib_dev.mmap		= mlx4_ib_mmap;
2381 	ibdev->ib_dev.alloc_pd		= mlx4_ib_alloc_pd;
2382 	ibdev->ib_dev.dealloc_pd	= mlx4_ib_dealloc_pd;
2383 	ibdev->ib_dev.create_ah		= mlx4_ib_create_ah;
2384 	ibdev->ib_dev.query_ah		= mlx4_ib_query_ah;
2385 	ibdev->ib_dev.destroy_ah	= mlx4_ib_destroy_ah;
2386 	ibdev->ib_dev.create_srq	= mlx4_ib_create_srq;
2387 	ibdev->ib_dev.modify_srq	= mlx4_ib_modify_srq;
2388 	ibdev->ib_dev.query_srq		= mlx4_ib_query_srq;
2389 	ibdev->ib_dev.destroy_srq	= mlx4_ib_destroy_srq;
2390 	ibdev->ib_dev.post_srq_recv	= mlx4_ib_post_srq_recv;
2391 	ibdev->ib_dev.create_qp		= mlx4_ib_create_qp;
2392 	ibdev->ib_dev.modify_qp		= mlx4_ib_modify_qp;
2393 	ibdev->ib_dev.query_qp		= mlx4_ib_query_qp;
2394 	ibdev->ib_dev.destroy_qp	= mlx4_ib_destroy_qp;
2395 	ibdev->ib_dev.post_send		= mlx4_ib_post_send;
2396 	ibdev->ib_dev.post_recv		= mlx4_ib_post_recv;
2397 	ibdev->ib_dev.create_cq		= mlx4_ib_create_cq;
2398 	ibdev->ib_dev.modify_cq		= mlx4_ib_modify_cq;
2399 	ibdev->ib_dev.resize_cq		= mlx4_ib_resize_cq;
2400 	ibdev->ib_dev.destroy_cq	= mlx4_ib_destroy_cq;
2401 	ibdev->ib_dev.poll_cq		= mlx4_ib_poll_cq;
2402 	ibdev->ib_dev.req_notify_cq	= mlx4_ib_arm_cq;
2403 	ibdev->ib_dev.get_dma_mr	= mlx4_ib_get_dma_mr;
2404 	ibdev->ib_dev.reg_user_mr	= mlx4_ib_reg_user_mr;
2405 	ibdev->ib_dev.rereg_user_mr	= mlx4_ib_rereg_user_mr;
2406 	ibdev->ib_dev.dereg_mr		= mlx4_ib_dereg_mr;
2407 	ibdev->ib_dev.alloc_mr		= mlx4_ib_alloc_mr;
2408 	ibdev->ib_dev.map_mr_sg		= mlx4_ib_map_mr_sg;
2409 	ibdev->ib_dev.attach_mcast	= mlx4_ib_mcg_attach;
2410 	ibdev->ib_dev.detach_mcast	= mlx4_ib_mcg_detach;
2411 	ibdev->ib_dev.process_mad	= mlx4_ib_process_mad;
2412 	ibdev->ib_dev.get_port_immutable = mlx4_port_immutable;
2413 	ibdev->ib_dev.disassociate_ucontext = mlx4_ib_disassociate_ucontext;
2414 
2415 	if (!mlx4_is_slave(ibdev->dev)) {
2416 		ibdev->ib_dev.alloc_fmr		= mlx4_ib_fmr_alloc;
2417 		ibdev->ib_dev.map_phys_fmr	= mlx4_ib_map_phys_fmr;
2418 		ibdev->ib_dev.unmap_fmr		= mlx4_ib_unmap_fmr;
2419 		ibdev->ib_dev.dealloc_fmr	= mlx4_ib_fmr_dealloc;
2420 	}
2421 
2422 	if (dev->caps.flags & MLX4_DEV_CAP_FLAG_MEM_WINDOW ||
2423 	    dev->caps.bmme_flags & MLX4_BMME_FLAG_TYPE_2_WIN) {
2424 		ibdev->ib_dev.alloc_mw = mlx4_ib_alloc_mw;
2425 		ibdev->ib_dev.dealloc_mw = mlx4_ib_dealloc_mw;
2426 
2427 		ibdev->ib_dev.uverbs_cmd_mask |=
2428 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
2429 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
2430 	}
2431 
2432 	if (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) {
2433 		ibdev->ib_dev.alloc_xrcd = mlx4_ib_alloc_xrcd;
2434 		ibdev->ib_dev.dealloc_xrcd = mlx4_ib_dealloc_xrcd;
2435 		ibdev->ib_dev.uverbs_cmd_mask |=
2436 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
2437 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
2438 	}
2439 
2440 	if (check_flow_steering_support(dev)) {
2441 		ibdev->steering_support = MLX4_STEERING_MODE_DEVICE_MANAGED;
2442 		ibdev->ib_dev.create_flow	= mlx4_ib_create_flow;
2443 		ibdev->ib_dev.destroy_flow	= mlx4_ib_destroy_flow;
2444 
2445 		ibdev->ib_dev.uverbs_ex_cmd_mask	|=
2446 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
2447 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
2448 	}
2449 
2450 	ibdev->ib_dev.uverbs_ex_cmd_mask |=
2451 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
2452 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
2453 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP);
2454 
2455 	mlx4_ib_alloc_eqs(dev, ibdev);
2456 
2457 	spin_lock_init(&iboe->lock);
2458 
2459 	if (init_node_data(ibdev))
2460 		goto err_map;
2461 
2462 	for (i = 0; i < ibdev->num_ports; ++i) {
2463 		mutex_init(&ibdev->counters_table[i].mutex);
2464 		INIT_LIST_HEAD(&ibdev->counters_table[i].counters_list);
2465 	}
2466 
2467 	num_req_counters = mlx4_is_bonded(dev) ? 1 : ibdev->num_ports;
2468 	for (i = 0; i < num_req_counters; ++i) {
2469 		mutex_init(&ibdev->qp1_proxy_lock[i]);
2470 		allocated = 0;
2471 		if (mlx4_ib_port_link_layer(&ibdev->ib_dev, i + 1) ==
2472 						IB_LINK_LAYER_ETHERNET) {
2473 			err = mlx4_counter_alloc(ibdev->dev, &counter_index);
2474 			/* if failed to allocate a new counter, use default */
2475 			if (err)
2476 				counter_index =
2477 					mlx4_get_default_counter_index(dev,
2478 								       i + 1);
2479 			else
2480 				allocated = 1;
2481 		} else { /* IB_LINK_LAYER_INFINIBAND use the default counter */
2482 			counter_index = mlx4_get_default_counter_index(dev,
2483 								       i + 1);
2484 		}
2485 		new_counter_index = kmalloc(sizeof(*new_counter_index),
2486 					    GFP_KERNEL);
2487 		if (!new_counter_index) {
2488 			if (allocated)
2489 				mlx4_counter_free(ibdev->dev, counter_index);
2490 			goto err_counter;
2491 		}
2492 		new_counter_index->index = counter_index;
2493 		new_counter_index->allocated = allocated;
2494 		list_add_tail(&new_counter_index->list,
2495 			      &ibdev->counters_table[i].counters_list);
2496 		ibdev->counters_table[i].default_counter = counter_index;
2497 		pr_info("counter index %d for port %d allocated %d\n",
2498 			counter_index, i + 1, allocated);
2499 	}
2500 	if (mlx4_is_bonded(dev))
2501 		for (i = 1; i < ibdev->num_ports ; ++i) {
2502 			new_counter_index =
2503 					kmalloc(sizeof(struct counter_index),
2504 						GFP_KERNEL);
2505 			if (!new_counter_index)
2506 				goto err_counter;
2507 			new_counter_index->index = counter_index;
2508 			new_counter_index->allocated = 0;
2509 			list_add_tail(&new_counter_index->list,
2510 				      &ibdev->counters_table[i].counters_list);
2511 			ibdev->counters_table[i].default_counter =
2512 								counter_index;
2513 		}
2514 
2515 	mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
2516 		ib_num_ports++;
2517 
2518 	spin_lock_init(&ibdev->sm_lock);
2519 	mutex_init(&ibdev->cap_mask_mutex);
2520 	INIT_LIST_HEAD(&ibdev->qp_list);
2521 	spin_lock_init(&ibdev->reset_flow_resource_lock);
2522 
2523 	if (ibdev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED &&
2524 	    ib_num_ports) {
2525 		ibdev->steer_qpn_count = MLX4_IB_UC_MAX_NUM_QPS;
2526 		err = mlx4_qp_reserve_range(dev, ibdev->steer_qpn_count,
2527 					    MLX4_IB_UC_STEER_QPN_ALIGN,
2528 					    &ibdev->steer_qpn_base, 0);
2529 		if (err)
2530 			goto err_counter;
2531 
2532 		ibdev->ib_uc_qpns_bitmap =
2533 			kmalloc(BITS_TO_LONGS(ibdev->steer_qpn_count) *
2534 				sizeof(long),
2535 				GFP_KERNEL);
2536 		if (!ibdev->ib_uc_qpns_bitmap) {
2537 			dev_err(&dev->persist->pdev->dev,
2538 				"bit map alloc failed\n");
2539 			goto err_steer_qp_release;
2540 		}
2541 
2542 		bitmap_zero(ibdev->ib_uc_qpns_bitmap, ibdev->steer_qpn_count);
2543 
2544 		err = mlx4_FLOW_STEERING_IB_UC_QP_RANGE(
2545 				dev, ibdev->steer_qpn_base,
2546 				ibdev->steer_qpn_base +
2547 				ibdev->steer_qpn_count - 1);
2548 		if (err)
2549 			goto err_steer_free_bitmap;
2550 	}
2551 
2552 	for (j = 1; j <= ibdev->dev->caps.num_ports; j++)
2553 		atomic64_set(&iboe->mac[j - 1], ibdev->dev->caps.def_mac[j]);
2554 
2555 	if (ib_register_device(&ibdev->ib_dev, NULL))
2556 		goto err_steer_free_bitmap;
2557 
2558 	if (mlx4_ib_mad_init(ibdev))
2559 		goto err_reg;
2560 
2561 	if (mlx4_ib_init_sriov(ibdev))
2562 		goto err_mad;
2563 
2564 	if (dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE ||
2565 	    dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2) {
2566 		if (!iboe->nb.notifier_call) {
2567 			iboe->nb.notifier_call = mlx4_ib_netdev_event;
2568 			err = register_netdevice_notifier(&iboe->nb);
2569 			if (err) {
2570 				iboe->nb.notifier_call = NULL;
2571 				goto err_notif;
2572 			}
2573 		}
2574 		if (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2) {
2575 			err = mlx4_config_roce_v2_port(dev, ROCE_V2_UDP_DPORT);
2576 			if (err) {
2577 				goto err_notif;
2578 			}
2579 		}
2580 	}
2581 
2582 	for (j = 0; j < ARRAY_SIZE(mlx4_class_attributes); ++j) {
2583 		if (device_create_file(&ibdev->ib_dev.dev,
2584 				       mlx4_class_attributes[j]))
2585 			goto err_notif;
2586 	}
2587 
2588 	ibdev->ib_active = true;
2589 	mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
2590 		devlink_port_type_ib_set(mlx4_get_devlink_port(dev, i),
2591 					 &ibdev->ib_dev);
2592 
2593 	if (mlx4_is_mfunc(ibdev->dev))
2594 		init_pkeys(ibdev);
2595 
2596 	/* create paravirt contexts for any VFs which are active */
2597 	if (mlx4_is_master(ibdev->dev)) {
2598 		for (j = 0; j < MLX4_MFUNC_MAX; j++) {
2599 			if (j == mlx4_master_func_num(ibdev->dev))
2600 				continue;
2601 			if (mlx4_is_slave_active(ibdev->dev, j))
2602 				do_slave_init(ibdev, j, 1);
2603 		}
2604 	}
2605 	return ibdev;
2606 
2607 err_notif:
2608 	if (ibdev->iboe.nb.notifier_call) {
2609 		if (unregister_netdevice_notifier(&ibdev->iboe.nb))
2610 			pr_warn("failure unregistering notifier\n");
2611 		ibdev->iboe.nb.notifier_call = NULL;
2612 	}
2613 	flush_workqueue(wq);
2614 
2615 	mlx4_ib_close_sriov(ibdev);
2616 
2617 err_mad:
2618 	mlx4_ib_mad_cleanup(ibdev);
2619 
2620 err_reg:
2621 	ib_unregister_device(&ibdev->ib_dev);
2622 
2623 err_steer_free_bitmap:
2624 	kfree(ibdev->ib_uc_qpns_bitmap);
2625 
2626 err_steer_qp_release:
2627 	if (ibdev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED)
2628 		mlx4_qp_release_range(dev, ibdev->steer_qpn_base,
2629 				      ibdev->steer_qpn_count);
2630 err_counter:
2631 	for (i = 0; i < ibdev->num_ports; ++i)
2632 		mlx4_ib_delete_counters_table(ibdev, &ibdev->counters_table[i]);
2633 
2634 err_map:
2635 	iounmap(ibdev->uar_map);
2636 
2637 err_uar:
2638 	mlx4_uar_free(dev, &ibdev->priv_uar);
2639 
2640 err_pd:
2641 	mlx4_pd_free(dev, ibdev->priv_pdn);
2642 
2643 err_dealloc:
2644 	ib_dealloc_device(&ibdev->ib_dev);
2645 
2646 	return NULL;
2647 }
2648 
2649 int mlx4_ib_steer_qp_alloc(struct mlx4_ib_dev *dev, int count, int *qpn)
2650 {
2651 	int offset;
2652 
2653 	WARN_ON(!dev->ib_uc_qpns_bitmap);
2654 
2655 	offset = bitmap_find_free_region(dev->ib_uc_qpns_bitmap,
2656 					 dev->steer_qpn_count,
2657 					 get_count_order(count));
2658 	if (offset < 0)
2659 		return offset;
2660 
2661 	*qpn = dev->steer_qpn_base + offset;
2662 	return 0;
2663 }
2664 
2665 void mlx4_ib_steer_qp_free(struct mlx4_ib_dev *dev, u32 qpn, int count)
2666 {
2667 	if (!qpn ||
2668 	    dev->steering_support != MLX4_STEERING_MODE_DEVICE_MANAGED)
2669 		return;
2670 
2671 	BUG_ON(qpn < dev->steer_qpn_base);
2672 
2673 	bitmap_release_region(dev->ib_uc_qpns_bitmap,
2674 			      qpn - dev->steer_qpn_base,
2675 			      get_count_order(count));
2676 }
2677 
2678 int mlx4_ib_steer_qp_reg(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
2679 			 int is_attach)
2680 {
2681 	int err;
2682 	size_t flow_size;
2683 	struct ib_flow_attr *flow = NULL;
2684 	struct ib_flow_spec_ib *ib_spec;
2685 
2686 	if (is_attach) {
2687 		flow_size = sizeof(struct ib_flow_attr) +
2688 			    sizeof(struct ib_flow_spec_ib);
2689 		flow = kzalloc(flow_size, GFP_KERNEL);
2690 		if (!flow)
2691 			return -ENOMEM;
2692 		flow->port = mqp->port;
2693 		flow->num_of_specs = 1;
2694 		flow->size = flow_size;
2695 		ib_spec = (struct ib_flow_spec_ib *)(flow + 1);
2696 		ib_spec->type = IB_FLOW_SPEC_IB;
2697 		ib_spec->size = sizeof(struct ib_flow_spec_ib);
2698 		/* Add an empty rule for IB L2 */
2699 		memset(&ib_spec->mask, 0, sizeof(ib_spec->mask));
2700 
2701 		err = __mlx4_ib_create_flow(&mqp->ibqp, flow,
2702 					    IB_FLOW_DOMAIN_NIC,
2703 					    MLX4_FS_REGULAR,
2704 					    &mqp->reg_id);
2705 	} else {
2706 		err = __mlx4_ib_destroy_flow(mdev->dev, mqp->reg_id);
2707 	}
2708 	kfree(flow);
2709 	return err;
2710 }
2711 
2712 static void mlx4_ib_remove(struct mlx4_dev *dev, void *ibdev_ptr)
2713 {
2714 	struct mlx4_ib_dev *ibdev = ibdev_ptr;
2715 	int p;
2716 	int i;
2717 
2718 	mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
2719 		devlink_port_type_clear(mlx4_get_devlink_port(dev, i));
2720 	ibdev->ib_active = false;
2721 	flush_workqueue(wq);
2722 
2723 	mlx4_ib_close_sriov(ibdev);
2724 	mlx4_ib_mad_cleanup(ibdev);
2725 	ib_unregister_device(&ibdev->ib_dev);
2726 	if (ibdev->iboe.nb.notifier_call) {
2727 		if (unregister_netdevice_notifier(&ibdev->iboe.nb))
2728 			pr_warn("failure unregistering notifier\n");
2729 		ibdev->iboe.nb.notifier_call = NULL;
2730 	}
2731 
2732 	if (ibdev->steering_support == MLX4_STEERING_MODE_DEVICE_MANAGED) {
2733 		mlx4_qp_release_range(dev, ibdev->steer_qpn_base,
2734 				      ibdev->steer_qpn_count);
2735 		kfree(ibdev->ib_uc_qpns_bitmap);
2736 	}
2737 
2738 	iounmap(ibdev->uar_map);
2739 	for (p = 0; p < ibdev->num_ports; ++p)
2740 		mlx4_ib_delete_counters_table(ibdev, &ibdev->counters_table[p]);
2741 
2742 	mlx4_foreach_port(p, dev, MLX4_PORT_TYPE_IB)
2743 		mlx4_CLOSE_PORT(dev, p);
2744 
2745 	mlx4_ib_free_eqs(dev, ibdev);
2746 
2747 	mlx4_uar_free(dev, &ibdev->priv_uar);
2748 	mlx4_pd_free(dev, ibdev->priv_pdn);
2749 	ib_dealloc_device(&ibdev->ib_dev);
2750 }
2751 
2752 static void do_slave_init(struct mlx4_ib_dev *ibdev, int slave, int do_init)
2753 {
2754 	struct mlx4_ib_demux_work **dm = NULL;
2755 	struct mlx4_dev *dev = ibdev->dev;
2756 	int i;
2757 	unsigned long flags;
2758 	struct mlx4_active_ports actv_ports;
2759 	unsigned int ports;
2760 	unsigned int first_port;
2761 
2762 	if (!mlx4_is_master(dev))
2763 		return;
2764 
2765 	actv_ports = mlx4_get_active_ports(dev, slave);
2766 	ports = bitmap_weight(actv_ports.ports, dev->caps.num_ports);
2767 	first_port = find_first_bit(actv_ports.ports, dev->caps.num_ports);
2768 
2769 	dm = kcalloc(ports, sizeof(*dm), GFP_ATOMIC);
2770 	if (!dm) {
2771 		pr_err("failed to allocate memory for tunneling qp update\n");
2772 		return;
2773 	}
2774 
2775 	for (i = 0; i < ports; i++) {
2776 		dm[i] = kmalloc(sizeof (struct mlx4_ib_demux_work), GFP_ATOMIC);
2777 		if (!dm[i]) {
2778 			pr_err("failed to allocate memory for tunneling qp update work struct\n");
2779 			while (--i >= 0)
2780 				kfree(dm[i]);
2781 			goto out;
2782 		}
2783 		INIT_WORK(&dm[i]->work, mlx4_ib_tunnels_update_work);
2784 		dm[i]->port = first_port + i + 1;
2785 		dm[i]->slave = slave;
2786 		dm[i]->do_init = do_init;
2787 		dm[i]->dev = ibdev;
2788 	}
2789 	/* initialize or tear down tunnel QPs for the slave */
2790 	spin_lock_irqsave(&ibdev->sriov.going_down_lock, flags);
2791 	if (!ibdev->sriov.is_going_down) {
2792 		for (i = 0; i < ports; i++)
2793 			queue_work(ibdev->sriov.demux[i].ud_wq, &dm[i]->work);
2794 		spin_unlock_irqrestore(&ibdev->sriov.going_down_lock, flags);
2795 	} else {
2796 		spin_unlock_irqrestore(&ibdev->sriov.going_down_lock, flags);
2797 		for (i = 0; i < ports; i++)
2798 			kfree(dm[i]);
2799 	}
2800 out:
2801 	kfree(dm);
2802 	return;
2803 }
2804 
2805 static void mlx4_ib_handle_catas_error(struct mlx4_ib_dev *ibdev)
2806 {
2807 	struct mlx4_ib_qp *mqp;
2808 	unsigned long flags_qp;
2809 	unsigned long flags_cq;
2810 	struct mlx4_ib_cq *send_mcq, *recv_mcq;
2811 	struct list_head    cq_notify_list;
2812 	struct mlx4_cq *mcq;
2813 	unsigned long flags;
2814 
2815 	pr_warn("mlx4_ib_handle_catas_error was started\n");
2816 	INIT_LIST_HEAD(&cq_notify_list);
2817 
2818 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
2819 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
2820 
2821 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
2822 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
2823 		if (mqp->sq.tail != mqp->sq.head) {
2824 			send_mcq = to_mcq(mqp->ibqp.send_cq);
2825 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
2826 			if (send_mcq->mcq.comp &&
2827 			    mqp->ibqp.send_cq->comp_handler) {
2828 				if (!send_mcq->mcq.reset_notify_added) {
2829 					send_mcq->mcq.reset_notify_added = 1;
2830 					list_add_tail(&send_mcq->mcq.reset_notify,
2831 						      &cq_notify_list);
2832 				}
2833 			}
2834 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
2835 		}
2836 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
2837 		/* Now, handle the QP's receive queue */
2838 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
2839 		/* no handling is needed for SRQ */
2840 		if (!mqp->ibqp.srq) {
2841 			if (mqp->rq.tail != mqp->rq.head) {
2842 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
2843 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
2844 				if (recv_mcq->mcq.comp &&
2845 				    mqp->ibqp.recv_cq->comp_handler) {
2846 					if (!recv_mcq->mcq.reset_notify_added) {
2847 						recv_mcq->mcq.reset_notify_added = 1;
2848 						list_add_tail(&recv_mcq->mcq.reset_notify,
2849 							      &cq_notify_list);
2850 					}
2851 				}
2852 				spin_unlock_irqrestore(&recv_mcq->lock,
2853 						       flags_cq);
2854 			}
2855 		}
2856 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
2857 	}
2858 
2859 	list_for_each_entry(mcq, &cq_notify_list, reset_notify) {
2860 		mcq->comp(mcq);
2861 	}
2862 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
2863 	pr_warn("mlx4_ib_handle_catas_error ended\n");
2864 }
2865 
2866 static void handle_bonded_port_state_event(struct work_struct *work)
2867 {
2868 	struct ib_event_work *ew =
2869 		container_of(work, struct ib_event_work, work);
2870 	struct mlx4_ib_dev *ibdev = ew->ib_dev;
2871 	enum ib_port_state bonded_port_state = IB_PORT_NOP;
2872 	int i;
2873 	struct ib_event ibev;
2874 
2875 	kfree(ew);
2876 	spin_lock_bh(&ibdev->iboe.lock);
2877 	for (i = 0; i < MLX4_MAX_PORTS; ++i) {
2878 		struct net_device *curr_netdev = ibdev->iboe.netdevs[i];
2879 		enum ib_port_state curr_port_state;
2880 
2881 		if (!curr_netdev)
2882 			continue;
2883 
2884 		curr_port_state =
2885 			(netif_running(curr_netdev) &&
2886 			 netif_carrier_ok(curr_netdev)) ?
2887 			IB_PORT_ACTIVE : IB_PORT_DOWN;
2888 
2889 		bonded_port_state = (bonded_port_state != IB_PORT_ACTIVE) ?
2890 			curr_port_state : IB_PORT_ACTIVE;
2891 	}
2892 	spin_unlock_bh(&ibdev->iboe.lock);
2893 
2894 	ibev.device = &ibdev->ib_dev;
2895 	ibev.element.port_num = 1;
2896 	ibev.event = (bonded_port_state == IB_PORT_ACTIVE) ?
2897 		IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2898 
2899 	ib_dispatch_event(&ibev);
2900 }
2901 
2902 static void mlx4_ib_event(struct mlx4_dev *dev, void *ibdev_ptr,
2903 			  enum mlx4_dev_event event, unsigned long param)
2904 {
2905 	struct ib_event ibev;
2906 	struct mlx4_ib_dev *ibdev = to_mdev((struct ib_device *) ibdev_ptr);
2907 	struct mlx4_eqe *eqe = NULL;
2908 	struct ib_event_work *ew;
2909 	int p = 0;
2910 
2911 	if (mlx4_is_bonded(dev) &&
2912 	    ((event == MLX4_DEV_EVENT_PORT_UP) ||
2913 	    (event == MLX4_DEV_EVENT_PORT_DOWN))) {
2914 		ew = kmalloc(sizeof(*ew), GFP_ATOMIC);
2915 		if (!ew)
2916 			return;
2917 		INIT_WORK(&ew->work, handle_bonded_port_state_event);
2918 		ew->ib_dev = ibdev;
2919 		queue_work(wq, &ew->work);
2920 		return;
2921 	}
2922 
2923 	if (event == MLX4_DEV_EVENT_PORT_MGMT_CHANGE)
2924 		eqe = (struct mlx4_eqe *)param;
2925 	else
2926 		p = (int) param;
2927 
2928 	switch (event) {
2929 	case MLX4_DEV_EVENT_PORT_UP:
2930 		if (p > ibdev->num_ports)
2931 			return;
2932 		if (mlx4_is_master(dev) &&
2933 		    rdma_port_get_link_layer(&ibdev->ib_dev, p) ==
2934 			IB_LINK_LAYER_INFINIBAND) {
2935 			mlx4_ib_invalidate_all_guid_record(ibdev, p);
2936 		}
2937 		ibev.event = IB_EVENT_PORT_ACTIVE;
2938 		break;
2939 
2940 	case MLX4_DEV_EVENT_PORT_DOWN:
2941 		if (p > ibdev->num_ports)
2942 			return;
2943 		ibev.event = IB_EVENT_PORT_ERR;
2944 		break;
2945 
2946 	case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
2947 		ibdev->ib_active = false;
2948 		ibev.event = IB_EVENT_DEVICE_FATAL;
2949 		mlx4_ib_handle_catas_error(ibdev);
2950 		break;
2951 
2952 	case MLX4_DEV_EVENT_PORT_MGMT_CHANGE:
2953 		ew = kmalloc(sizeof *ew, GFP_ATOMIC);
2954 		if (!ew) {
2955 			pr_err("failed to allocate memory for events work\n");
2956 			break;
2957 		}
2958 
2959 		INIT_WORK(&ew->work, handle_port_mgmt_change_event);
2960 		memcpy(&ew->ib_eqe, eqe, sizeof *eqe);
2961 		ew->ib_dev = ibdev;
2962 		/* need to queue only for port owner, which uses GEN_EQE */
2963 		if (mlx4_is_master(dev))
2964 			queue_work(wq, &ew->work);
2965 		else
2966 			handle_port_mgmt_change_event(&ew->work);
2967 		return;
2968 
2969 	case MLX4_DEV_EVENT_SLAVE_INIT:
2970 		/* here, p is the slave id */
2971 		do_slave_init(ibdev, p, 1);
2972 		if (mlx4_is_master(dev)) {
2973 			int i;
2974 
2975 			for (i = 1; i <= ibdev->num_ports; i++) {
2976 				if (rdma_port_get_link_layer(&ibdev->ib_dev, i)
2977 					== IB_LINK_LAYER_INFINIBAND)
2978 					mlx4_ib_slave_alias_guid_event(ibdev,
2979 								       p, i,
2980 								       1);
2981 			}
2982 		}
2983 		return;
2984 
2985 	case MLX4_DEV_EVENT_SLAVE_SHUTDOWN:
2986 		if (mlx4_is_master(dev)) {
2987 			int i;
2988 
2989 			for (i = 1; i <= ibdev->num_ports; i++) {
2990 				if (rdma_port_get_link_layer(&ibdev->ib_dev, i)
2991 					== IB_LINK_LAYER_INFINIBAND)
2992 					mlx4_ib_slave_alias_guid_event(ibdev,
2993 								       p, i,
2994 								       0);
2995 			}
2996 		}
2997 		/* here, p is the slave id */
2998 		do_slave_init(ibdev, p, 0);
2999 		return;
3000 
3001 	default:
3002 		return;
3003 	}
3004 
3005 	ibev.device	      = ibdev_ptr;
3006 	ibev.element.port_num = mlx4_is_bonded(ibdev->dev) ? 1 : (u8)p;
3007 
3008 	ib_dispatch_event(&ibev);
3009 }
3010 
3011 static struct mlx4_interface mlx4_ib_interface = {
3012 	.add		= mlx4_ib_add,
3013 	.remove		= mlx4_ib_remove,
3014 	.event		= mlx4_ib_event,
3015 	.protocol	= MLX4_PROT_IB_IPV6,
3016 	.flags		= MLX4_INTFF_BONDING
3017 };
3018 
3019 static int __init mlx4_ib_init(void)
3020 {
3021 	int err;
3022 
3023 	wq = create_singlethread_workqueue("mlx4_ib");
3024 	if (!wq)
3025 		return -ENOMEM;
3026 
3027 	err = mlx4_ib_mcg_init();
3028 	if (err)
3029 		goto clean_wq;
3030 
3031 	err = mlx4_register_interface(&mlx4_ib_interface);
3032 	if (err)
3033 		goto clean_mcg;
3034 
3035 	return 0;
3036 
3037 clean_mcg:
3038 	mlx4_ib_mcg_destroy();
3039 
3040 clean_wq:
3041 	destroy_workqueue(wq);
3042 	return err;
3043 }
3044 
3045 static void __exit mlx4_ib_cleanup(void)
3046 {
3047 	mlx4_unregister_interface(&mlx4_ib_interface);
3048 	mlx4_ib_mcg_destroy();
3049 	destroy_workqueue(wq);
3050 }
3051 
3052 module_init(mlx4_ib_init);
3053 module_exit(mlx4_ib_cleanup);
3054