xref: /NextBSD/sys/ofed/drivers/infiniband/hw/mlx4/main.c (revision 84d351007654069f9643c8e4b4802a7f5f08ee42)
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/slab.h>
36 #include <linux/errno.h>
37 #include <linux/netdevice.h>
38 #include <linux/inetdevice.h>
39 #include <linux/if_vlan.h>
40 #include <linux/fs.h>
41 #include <net/ipv6.h>
42 
43 #include <rdma/ib_smi.h>
44 #include <rdma/ib_user_verbs.h>
45 #include <rdma/ib_user_verbs_exp.h>
46 #include <rdma/ib_addr.h>
47 
48 #include <linux/mlx4/driver.h>
49 #include <linux/mlx4/cmd.h>
50 #include <linux/sched.h>
51 #include <linux/page.h>
52 #include <linux/printk.h>
53 #include "mlx4_ib.h"
54 #include "mlx4_exp.h"
55 #include "user.h"
56 #include "wc.h"
57 
58 #define DRV_NAME	MLX4_IB_DRV_NAME
59 #define DRV_VERSION	"1.0"
60 #define DRV_RELDATE	__DATE__
61 
62 #define MLX4_IB_DRIVER_PROC_DIR_NAME "driver/mlx4_ib"
63 #define MLX4_IB_MRS_PROC_DIR_NAME "mrs"
64 #define MLX4_IB_FLOW_MAX_PRIO 0xFFF
65 #define MLX4_IB_FLOW_QPN_MASK 0xFFFFFF
66 
67 MODULE_AUTHOR("Roland Dreier");
68 MODULE_DESCRIPTION("Mellanox ConnectX HCA InfiniBand driver");
69 MODULE_LICENSE("Dual BSD/GPL");
70 #ifdef __linux__
71 MODULE_VERSION(DRV_VERSION);
72 #endif
73 
74 int mlx4_ib_sm_guid_assign = 1;
75 
76 module_param_named(sm_guid_assign, mlx4_ib_sm_guid_assign, int, 0444);
77 MODULE_PARM_DESC(sm_guid_assign, "Enable SM alias_GUID assignment if sm_guid_assign > 0 (Default: 1)");
78 
79 enum {
80 	MAX_NUM_STR_BITMAP = 1 << 15,
81 	DEFAULT_TBL_VAL = -1
82 };
83 
84 static struct mlx4_dbdf2val_lst dev_assign_str = {
85 	.name		= "dev_assign_str param",
86 	.num_vals	= 1,
87 	.def_val	= {DEFAULT_TBL_VAL},
88 	.range		= {0, MAX_NUM_STR_BITMAP - 1}
89 };
90 module_param_string(dev_assign_str, dev_assign_str.str,
91 		    sizeof(dev_assign_str.str), 0444);
92 MODULE_PARM_DESC(dev_assign_str,
93 		 "Map device function numbers to IB device numbers (e.g. '0000:04:00.0-0,002b:1c:0b.a-1,...').\n"
94 		 "\t\tHexadecimal digits for the device function (e.g. 002b:1c:0b.a) and decimal for IB device numbers (e.g. 1).\n"
95 		 "\t\tMax supported devices - 32");
96 
97 
98 static unsigned long *dev_num_str_bitmap;
99 static spinlock_t dev_num_str_lock;
100 
101 static const char mlx4_ib_version[] =
102 	DRV_NAME ": Mellanox ConnectX InfiniBand driver v"
103 	DRV_VERSION " (" DRV_RELDATE ")\n";
104 
105 struct update_gid_work {
106 	struct work_struct	work;
107 	union ib_gid		gids[128];
108 	struct mlx4_ib_dev     *dev;
109 	int			port;
110 };
111 
112 struct dev_rec {
113 	int	bus;
114 	int	dev;
115 	int	func;
116 	int	nr;
117 };
118 
119 static int dr_active;
120 
121 static void do_slave_init(struct mlx4_ib_dev *ibdev, int slave, int do_init);
122 
123 static void mlx4_ib_scan_netdevs(struct mlx4_ib_dev *ibdev, struct net_device*,
124 				 unsigned long);
125 
126 static u8 mlx4_ib_get_dev_port(struct net_device *dev,
127                                         struct mlx4_ib_dev *ibdev);
128 
129 static struct workqueue_struct *wq;
130 
init_query_mad(struct ib_smp * mad)131 static void init_query_mad(struct ib_smp *mad)
132 {
133 	mad->base_version  = 1;
134 	mad->mgmt_class    = IB_MGMT_CLASS_SUBN_LID_ROUTED;
135 	mad->class_version = 1;
136 	mad->method	   = IB_MGMT_METHOD_GET;
137 }
138 
139 static union ib_gid zgid;
140 
check_flow_steering_support(struct mlx4_dev * dev)141 static int check_flow_steering_support(struct mlx4_dev *dev)
142 {
143 	int eth_num_ports = 0;
144 	int ib_num_ports = 0;
145 	int dmfs = dev->caps.steering_mode == MLX4_STEERING_MODE_DEVICE_MANAGED;
146 
147 	if (dmfs) {
148 		int i;
149 		mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH)
150 			eth_num_ports++;
151 		mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
152 			ib_num_ports++;
153 		dmfs &= (!ib_num_ports ||
154 			 (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_DMFS_IPOIB)) &&
155 			(!eth_num_ports ||
156 			 (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FS_EN));
157 		if (ib_num_ports && mlx4_is_mfunc(dev)) {
158 			dmfs = 0;
159 		}
160 	}
161 	return dmfs;
162 }
163 
mlx4_ib_query_device(struct ib_device * ibdev,struct ib_device_attr * props)164 int mlx4_ib_query_device(struct ib_device *ibdev,
165 				struct ib_device_attr *props)
166 {
167 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
168 	struct ib_smp *in_mad  = NULL;
169 	struct ib_smp *out_mad = NULL;
170 	int err = -ENOMEM;
171 
172 	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
173 	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
174 	if (!in_mad || !out_mad)
175 		goto out;
176 
177 	init_query_mad(in_mad);
178 	in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
179 
180 	err = mlx4_MAD_IFC(to_mdev(ibdev), MLX4_MAD_IFC_IGNORE_KEYS,
181 			   1, NULL, NULL, in_mad, out_mad);
182 	if (err)
183 		goto out;
184 
185 	memset(props, 0, sizeof *props);
186 
187 	props->fw_ver = dev->dev->caps.fw_ver;
188 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
189 		IB_DEVICE_PORT_ACTIVE_EVENT		|
190 		IB_DEVICE_SYS_IMAGE_GUID		|
191 		IB_DEVICE_RC_RNR_NAK_GEN		|
192 		IB_DEVICE_BLOCK_MULTICAST_LOOPBACK	|
193 		IB_DEVICE_SHARED_MR;
194 
195 	if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_PKEY_CNTR)
196 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
197 	if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_QKEY_CNTR)
198 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
199 	if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_APM)
200 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
201 	if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UD_AV_PORT)
202 		props->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
203 	if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_IPOIB_CSUM)
204 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
205 	if (dev->dev->caps.max_gso_sz && dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BLH)
206 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
207 	if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_RESERVED_LKEY)
208 		props->device_cap_flags |= IB_DEVICE_LOCAL_DMA_LKEY;
209 	if ((dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_LOCAL_INV) &&
210 	    (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_REMOTE_INV) &&
211 	    (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_FAST_REG_WR))
212 		props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
213 	if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC)
214 		props->device_cap_flags |= IB_DEVICE_XRC;
215 	if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_CROSS_CHANNEL)
216 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
217 
218 	if (check_flow_steering_support(dev->dev))
219 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
220 
221 
222 	props->device_cap_flags |= IB_DEVICE_QPG;
223 	if (dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS) {
224 		props->device_cap_flags |= IB_DEVICE_UD_RSS;
225 		props->max_rss_tbl_sz = dev->dev->caps.max_rss_tbl_sz;
226 	}
227 	if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_MEM_WINDOW)
228 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW;
229 	if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_TYPE_2_WIN) {
230 		if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_WIN_TYPE_2B)
231 			props->device_cap_flags |= IB_DEVICE_MEM_WINDOW_TYPE_2B;
232 		else
233 			props->device_cap_flags |= IB_DEVICE_MEM_WINDOW_TYPE_2A;
234 	}
235 	props->vendor_id	   = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
236 		0xffffff;
237 	props->vendor_part_id	   = dev->dev->pdev->device;
238 	props->hw_ver		   = be32_to_cpup((__be32 *) (out_mad->data + 32));
239 	memcpy(&props->sys_image_guid, out_mad->data +	4, 8);
240 
241 	props->max_mr_size	   = ~0ull;
242 	props->page_size_cap	   = dev->dev->caps.page_size_cap;
243 	props->max_qp		   = dev->dev->quotas.qp;
244 	props->max_qp_wr	   = dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE;
245 	props->max_sge		   = min(dev->dev->caps.max_sq_sg,
246 					 dev->dev->caps.max_rq_sg);
247 	props->max_cq		   = dev->dev->quotas.cq;
248 	props->max_cqe		   = dev->dev->caps.max_cqes;
249 	props->max_mr		   = dev->dev->quotas.mpt;
250 	props->max_pd		   = dev->dev->caps.num_pds - dev->dev->caps.reserved_pds;
251 	props->max_qp_rd_atom	   = dev->dev->caps.max_qp_dest_rdma;
252 	props->max_qp_init_rd_atom = dev->dev->caps.max_qp_init_rdma;
253 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
254 	props->max_srq		   = dev->dev->quotas.srq;
255 	props->max_srq_wr	   = dev->dev->caps.max_srq_wqes - 1;
256 	props->max_srq_sge	   = dev->dev->caps.max_srq_sge;
257 	props->max_fast_reg_page_list_len = MLX4_MAX_FAST_REG_PAGES;
258 	props->local_ca_ack_delay  = dev->dev->caps.local_ca_ack_delay;
259 	props->atomic_cap	   = dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_ATOMIC ?
260 		IB_ATOMIC_HCA : IB_ATOMIC_NONE;
261 	props->masked_atomic_cap   = props->atomic_cap;
262 	props->max_pkeys	   = dev->dev->caps.pkey_table_len[1];
263 	props->max_mcast_grp	   = dev->dev->caps.num_mgms + dev->dev->caps.num_amgms;
264 	props->max_mcast_qp_attach = dev->dev->caps.num_qp_per_mgm;
265 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
266 					   props->max_mcast_grp;
267 	props->max_map_per_fmr = dev->dev->caps.max_fmr_maps;
268 	props->hca_core_clock = dev->dev->caps.hca_core_clock;
269 	if (dev->dev->caps.hca_core_clock > 0)
270 		props->comp_mask |= IB_DEVICE_ATTR_WITH_HCA_CORE_CLOCK;
271 	if (dev->dev->caps.cq_timestamp) {
272 		props->timestamp_mask = 0xFFFFFFFFFFFF;
273 		props->comp_mask |= IB_DEVICE_ATTR_WITH_TIMESTAMP_MASK;
274 	}
275 
276 out:
277 	kfree(in_mad);
278 	kfree(out_mad);
279 
280 	return err;
281 }
282 
283 static enum rdma_link_layer
mlx4_ib_port_link_layer(struct ib_device * device,u8 port_num)284 mlx4_ib_port_link_layer(struct ib_device *device, u8 port_num)
285 {
286 	struct mlx4_dev *dev = to_mdev(device)->dev;
287 
288 	return dev->caps.port_mask[port_num] == MLX4_PORT_TYPE_IB ?
289 		IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
290 }
291 
ib_link_query_port(struct ib_device * ibdev,u8 port,struct ib_port_attr * props,int netw_view)292 static int ib_link_query_port(struct ib_device *ibdev, u8 port,
293 			      struct ib_port_attr *props, int netw_view)
294 {
295 	struct ib_smp *in_mad  = NULL;
296 	struct ib_smp *out_mad = NULL;
297 	int ext_active_speed;
298 	int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
299 	int err = -ENOMEM;
300 
301 	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
302 	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
303 	if (!in_mad || !out_mad)
304 		goto out;
305 
306 	init_query_mad(in_mad);
307 	in_mad->attr_id  = IB_SMP_ATTR_PORT_INFO;
308 	in_mad->attr_mod = cpu_to_be32(port);
309 
310 	if (mlx4_is_mfunc(to_mdev(ibdev)->dev) && netw_view)
311 		mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
312 
313 	err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port, NULL, NULL,
314 				in_mad, out_mad);
315 	if (err)
316 		goto out;
317 
318 
319 	props->lid		= be16_to_cpup((__be16 *) (out_mad->data + 16));
320 	props->lmc		= out_mad->data[34] & 0x7;
321 	props->sm_lid		= be16_to_cpup((__be16 *) (out_mad->data + 18));
322 	props->sm_sl		= out_mad->data[36] & 0xf;
323 	props->state		= out_mad->data[32] & 0xf;
324 	props->phys_state	= out_mad->data[33] >> 4;
325 	props->port_cap_flags	= be32_to_cpup((__be32 *) (out_mad->data + 20));
326 	if (netw_view)
327 		props->gid_tbl_len = out_mad->data[50];
328 	else
329 		props->gid_tbl_len = to_mdev(ibdev)->dev->caps.gid_table_len[port];
330 	props->max_msg_sz	= to_mdev(ibdev)->dev->caps.max_msg_sz;
331 	props->pkey_tbl_len	= to_mdev(ibdev)->dev->caps.pkey_table_len[port];
332 	props->bad_pkey_cntr	= be16_to_cpup((__be16 *) (out_mad->data + 46));
333 	props->qkey_viol_cntr	= be16_to_cpup((__be16 *) (out_mad->data + 48));
334 	props->active_width	= out_mad->data[31] & 0xf;
335 	props->active_speed	= out_mad->data[35] >> 4;
336 	props->max_mtu		= out_mad->data[41] & 0xf;
337 	props->active_mtu	= out_mad->data[36] >> 4;
338 	props->subnet_timeout	= out_mad->data[51] & 0x1f;
339 	props->max_vl_num	= out_mad->data[37] >> 4;
340 	props->init_type_reply	= out_mad->data[41] >> 4;
341 
342 	/* Check if extended speeds (EDR/FDR/...) are supported */
343 	if (props->port_cap_flags & IB_PORT_EXTENDED_SPEEDS_SUP) {
344 		ext_active_speed = out_mad->data[62] >> 4;
345 
346 		switch (ext_active_speed) {
347 		case 1:
348 			props->active_speed = IB_SPEED_FDR;
349 			break;
350 		case 2:
351 			props->active_speed = IB_SPEED_EDR;
352 			break;
353 		}
354 	}
355 
356 	/* If reported active speed is QDR, check if is FDR-10 */
357 	if (props->active_speed == IB_SPEED_QDR) {
358 		init_query_mad(in_mad);
359 		in_mad->attr_id = MLX4_ATTR_EXTENDED_PORT_INFO;
360 		in_mad->attr_mod = cpu_to_be32(port);
361 
362 		err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port,
363 				   NULL, NULL, in_mad, out_mad);
364 		if (err)
365 			goto out;
366 
367 		/* Checking LinkSpeedActive for FDR-10 */
368 		if (out_mad->data[15] & 0x1)
369 			props->active_speed = IB_SPEED_FDR10;
370 	}
371 
372 	/* Avoid wrong speed value returned by FW if the IB link is down. */
373 	if (props->state == IB_PORT_DOWN)
374 		 props->active_speed = IB_SPEED_SDR;
375 
376 out:
377 	kfree(in_mad);
378 	kfree(out_mad);
379 	return err;
380 }
381 
state_to_phys_state(enum ib_port_state state)382 static u8 state_to_phys_state(enum ib_port_state state)
383 {
384 	return state == IB_PORT_ACTIVE ? 5 : 3;
385 }
386 
eth_link_query_port(struct ib_device * ibdev,u8 port,struct ib_port_attr * props,int netw_view)387 static int eth_link_query_port(struct ib_device *ibdev, u8 port,
388 			       struct ib_port_attr *props, int netw_view)
389 {
390 
391 	struct mlx4_ib_dev *mdev = to_mdev(ibdev);
392 	struct mlx4_ib_iboe *iboe = &mdev->iboe;
393 	struct net_device *ndev;
394 	enum ib_mtu tmp;
395 	struct mlx4_cmd_mailbox *mailbox;
396 	unsigned long flags;
397 	int err = 0;
398 
399 	mailbox = mlx4_alloc_cmd_mailbox(mdev->dev);
400 	if (IS_ERR(mailbox))
401 		return PTR_ERR(mailbox);
402 
403 	err = mlx4_cmd_box(mdev->dev, 0, mailbox->dma, port, 0,
404 			   MLX4_CMD_QUERY_PORT, MLX4_CMD_TIME_CLASS_B,
405 			   MLX4_CMD_WRAPPED);
406 	if (err)
407 		goto out;
408 
409 	props->active_width	=  (((u8 *)mailbox->buf)[5] == 0x40) ?
410 						IB_WIDTH_4X : IB_WIDTH_1X;
411 	props->active_speed	= IB_SPEED_QDR;
412 	props->port_cap_flags	= IB_PORT_CM_SUP;
413 	if (netw_view)
414 		props->gid_tbl_len = MLX4_ROCE_MAX_GIDS;
415 	else
416 		props->gid_tbl_len   = mdev->dev->caps.gid_table_len[port];
417 
418 	props->max_msg_sz	= mdev->dev->caps.max_msg_sz;
419 	props->pkey_tbl_len	= 1;
420 	props->max_mtu		= IB_MTU_4096;
421 	props->max_vl_num	= 2;
422 	props->state		= IB_PORT_DOWN;
423 	props->phys_state	= state_to_phys_state(props->state);
424 	props->active_mtu	= IB_MTU_256;
425 	spin_lock_irqsave(&iboe->lock, flags);
426 	ndev = iboe->netdevs[port - 1];
427 	if (!ndev)
428 		goto out_unlock;
429 
430 	tmp = iboe_get_mtu(ndev->if_mtu);
431 	props->active_mtu = tmp ? min(props->max_mtu, tmp) : IB_MTU_256;
432 
433 	props->state		= (netif_running(ndev) && netif_carrier_ok(ndev)) ?
434 					IB_PORT_ACTIVE : IB_PORT_DOWN;
435 	props->phys_state	= state_to_phys_state(props->state);
436 out_unlock:
437 	spin_unlock_irqrestore(&iboe->lock, flags);
438 out:
439 	mlx4_free_cmd_mailbox(mdev->dev, mailbox);
440 	return err;
441 }
442 
__mlx4_ib_query_port(struct ib_device * ibdev,u8 port,struct ib_port_attr * props,int netw_view)443 int __mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
444 			 struct ib_port_attr *props, int netw_view)
445 {
446 	int err;
447 
448 	memset(props, 0, sizeof *props);
449 
450 	err = mlx4_ib_port_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND ?
451 		ib_link_query_port(ibdev, port, props, netw_view) :
452 				eth_link_query_port(ibdev, port, props, netw_view);
453 
454 	return err;
455 }
456 
mlx4_ib_query_port(struct ib_device * ibdev,u8 port,struct ib_port_attr * props)457 static int mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
458 			      struct ib_port_attr *props)
459 {
460 	/* returns host view */
461 	return __mlx4_ib_query_port(ibdev, port, props, 0);
462 }
463 
__mlx4_ib_query_gid(struct ib_device * ibdev,u8 port,int index,union ib_gid * gid,int netw_view)464 int __mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
465 			union ib_gid *gid, int netw_view)
466 {
467 	struct ib_smp *in_mad  = NULL;
468 	struct ib_smp *out_mad = NULL;
469 	int err = -ENOMEM;
470 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
471 	int clear = 0;
472 	int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
473 
474 	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
475 	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
476 	if (!in_mad || !out_mad)
477 		goto out;
478 
479 	init_query_mad(in_mad);
480 	in_mad->attr_id  = IB_SMP_ATTR_PORT_INFO;
481 	in_mad->attr_mod = cpu_to_be32(port);
482 
483 	if (mlx4_is_mfunc(dev->dev) && netw_view)
484 		mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
485 
486 	err = mlx4_MAD_IFC(dev, mad_ifc_flags, port, NULL, NULL, in_mad, out_mad);
487 	if (err)
488 		goto out;
489 
490 	memcpy(gid->raw, out_mad->data + 8, 8);
491 
492 	if (mlx4_is_mfunc(dev->dev) && !netw_view) {
493 		if (index) {
494 			/* For any index > 0, return the null guid */
495 			err = 0;
496 			clear = 1;
497 			goto out;
498 		}
499 	}
500 
501 	init_query_mad(in_mad);
502 	in_mad->attr_id  = IB_SMP_ATTR_GUID_INFO;
503 	in_mad->attr_mod = cpu_to_be32(index / 8);
504 
505 	err = mlx4_MAD_IFC(dev, mad_ifc_flags, port,
506 			   NULL, NULL, in_mad, out_mad);
507 	if (err)
508 		goto out;
509 
510 	memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
511 
512 out:
513 	if (clear)
514 		memset(gid->raw + 8, 0, 8);
515 	kfree(in_mad);
516 	kfree(out_mad);
517 	return err;
518 }
519 
iboe_query_gid(struct ib_device * ibdev,u8 port,int index,union ib_gid * gid)520 static int iboe_query_gid(struct ib_device *ibdev, u8 port, int index,
521 			  union ib_gid *gid)
522 {
523 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
524 
525 	*gid = dev->iboe.gid_table[port - 1][index];
526 
527 	return 0;
528 }
529 
mlx4_ib_query_gid(struct ib_device * ibdev,u8 port,int index,union ib_gid * gid)530 static int mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
531 			     union ib_gid *gid)
532 {
533 	if (rdma_port_get_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND)
534 		return __mlx4_ib_query_gid(ibdev, port, index, gid, 0);
535 	else
536 		return iboe_query_gid(ibdev, port, index, gid);
537 }
538 
__mlx4_ib_query_pkey(struct ib_device * ibdev,u8 port,u16 index,u16 * pkey,int netw_view)539 int __mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
540 			 u16 *pkey, int netw_view)
541 {
542 	struct ib_smp *in_mad  = NULL;
543 	struct ib_smp *out_mad = NULL;
544 	int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
545 	int err = -ENOMEM;
546 
547 	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
548 	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
549 	if (!in_mad || !out_mad)
550 		goto out;
551 
552 	init_query_mad(in_mad);
553 	in_mad->attr_id  = IB_SMP_ATTR_PKEY_TABLE;
554 	in_mad->attr_mod = cpu_to_be32(index / 32);
555 
556 	if (mlx4_is_mfunc(to_mdev(ibdev)->dev) && netw_view)
557 		mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
558 
559 	err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port, NULL, NULL,
560 			   in_mad, out_mad);
561 	if (err)
562 		goto out;
563 
564 	*pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
565 
566 out:
567 	kfree(in_mad);
568 	kfree(out_mad);
569 	return err;
570 }
571 
mlx4_ib_query_pkey(struct ib_device * ibdev,u8 port,u16 index,u16 * pkey)572 static int mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
573 {
574 	return __mlx4_ib_query_pkey(ibdev, port, index, pkey, 0);
575 }
576 
mlx4_ib_modify_device(struct ib_device * ibdev,int mask,struct ib_device_modify * props)577 static int mlx4_ib_modify_device(struct ib_device *ibdev, int mask,
578 				 struct ib_device_modify *props)
579 {
580 	struct mlx4_cmd_mailbox *mailbox;
581 	unsigned long flags;
582 
583 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
584 		return -EOPNOTSUPP;
585 
586 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
587 		return 0;
588 
589 	if (mlx4_is_slave(to_mdev(ibdev)->dev))
590 		return -EOPNOTSUPP;
591 
592 	spin_lock_irqsave(&to_mdev(ibdev)->sm_lock, flags);
593 	memcpy(ibdev->node_desc, props->node_desc, 64);
594 	spin_unlock_irqrestore(&to_mdev(ibdev)->sm_lock, flags);
595 
596 	/*
597 	 * If possible, pass node desc to FW, so it can generate
598 	 * a 144 trap.  If cmd fails, just ignore.
599 	 */
600 	mailbox = mlx4_alloc_cmd_mailbox(to_mdev(ibdev)->dev);
601 	if (IS_ERR(mailbox))
602 		return 0;
603 
604 	memset(mailbox->buf, 0, 256);
605 	memcpy(mailbox->buf, props->node_desc, 64);
606 	mlx4_cmd(to_mdev(ibdev)->dev, mailbox->dma, 1, 0,
607 		 MLX4_CMD_SET_NODE, MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
608 
609 	mlx4_free_cmd_mailbox(to_mdev(ibdev)->dev, mailbox);
610 
611 	return 0;
612 }
613 
mlx4_SET_PORT(struct mlx4_ib_dev * dev,u8 port,int reset_qkey_viols,u32 cap_mask)614 static int mlx4_SET_PORT(struct mlx4_ib_dev *dev, u8 port, int reset_qkey_viols,
615 			 u32 cap_mask)
616 {
617 	struct mlx4_cmd_mailbox *mailbox;
618 	int err;
619 	u8 is_eth = dev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
620 
621 	mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
622 	if (IS_ERR(mailbox))
623 		return PTR_ERR(mailbox);
624 
625 	memset(mailbox->buf, 0, 256);
626 
627 	if (dev->dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
628 		*(u8 *) mailbox->buf	     = !!reset_qkey_viols << 6;
629 		((__be32 *) mailbox->buf)[2] = cpu_to_be32(cap_mask);
630 	} else {
631 		((u8 *) mailbox->buf)[3]     = !!reset_qkey_viols;
632 		((__be32 *) mailbox->buf)[1] = cpu_to_be32(cap_mask);
633 	}
634 
635 	err = mlx4_cmd(dev->dev, mailbox->dma, port, is_eth, MLX4_CMD_SET_PORT,
636 		       MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
637 
638 	mlx4_free_cmd_mailbox(dev->dev, mailbox);
639 	return err;
640 }
641 
mlx4_ib_modify_port(struct ib_device * ibdev,u8 port,int mask,struct ib_port_modify * props)642 static int mlx4_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
643 			       struct ib_port_modify *props)
644 {
645 	struct ib_port_attr attr;
646 	u32 cap_mask;
647 	int err;
648 
649 	mutex_lock(&to_mdev(ibdev)->cap_mask_mutex);
650 
651 	err = mlx4_ib_query_port(ibdev, port, &attr);
652 	if (err)
653 		goto out;
654 
655 	cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
656 		~props->clr_port_cap_mask;
657 
658 	err = mlx4_SET_PORT(to_mdev(ibdev), port,
659 			    !!(mask & IB_PORT_RESET_QKEY_CNTR),
660 			    cap_mask);
661 
662 out:
663 	mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
664 	return err;
665 }
666 
mlx4_ib_alloc_ucontext(struct ib_device * ibdev,struct ib_udata * udata)667 static struct ib_ucontext *mlx4_ib_alloc_ucontext(struct ib_device *ibdev,
668 						  struct ib_udata *udata)
669 {
670 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
671 	struct mlx4_ib_ucontext *context;
672 	struct mlx4_ib_alloc_ucontext_resp_v3 resp_v3;
673 	struct mlx4_ib_alloc_ucontext_resp resp;
674 	int err;
675 
676 	if (!dev->ib_active)
677 		return ERR_PTR(-EAGAIN);
678 
679 	if (ibdev->uverbs_abi_ver == MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION) {
680 		resp_v3.qp_tab_size      = dev->dev->caps.num_qps;
681 		if (mlx4_wc_enabled()) {
682 			resp_v3.bf_reg_size      = dev->dev->caps.bf_reg_size;
683 			resp_v3.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
684 		} else {
685 			resp_v3.bf_reg_size      = 0;
686 			resp_v3.bf_regs_per_page = 0;
687 		}
688 	} else {
689 		resp.dev_caps	      = dev->dev->caps.userspace_caps;
690 		resp.qp_tab_size      = dev->dev->caps.num_qps;
691 		if (mlx4_wc_enabled()) {
692 			resp.bf_reg_size      = dev->dev->caps.bf_reg_size;
693 			resp.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
694 		} else {
695 			resp.bf_reg_size      = 0;
696 			resp.bf_regs_per_page = 0;
697 		}
698 		resp.cqe_size	      = dev->dev->caps.cqe_size;
699 	}
700 
701 	context = kmalloc(sizeof *context, GFP_KERNEL);
702 	if (!context)
703 		return ERR_PTR(-ENOMEM);
704 
705 	err = mlx4_uar_alloc(to_mdev(ibdev)->dev, &context->uar);
706 	if (err) {
707 		kfree(context);
708 		return ERR_PTR(err);
709 	}
710 
711 	INIT_LIST_HEAD(&context->db_page_list);
712 	mutex_init(&context->db_page_mutex);
713 
714 	if (ibdev->uverbs_abi_ver == MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION)
715 		err = ib_copy_to_udata(udata, &resp_v3, sizeof(resp_v3));
716 	else
717 		err = ib_copy_to_udata(udata, &resp, sizeof(resp));
718 
719 	if (err) {
720 		mlx4_uar_free(to_mdev(ibdev)->dev, &context->uar);
721 		kfree(context);
722 		return ERR_PTR(-EFAULT);
723 	}
724 
725 	return &context->ibucontext;
726 }
727 
mlx4_ib_dealloc_ucontext(struct ib_ucontext * ibcontext)728 static int mlx4_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
729 {
730 	struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
731 
732 	mlx4_uar_free(to_mdev(ibcontext->device)->dev, &context->uar);
733 	kfree(context);
734 
735 	return 0;
736 }
737 
738 /* XXX FBSD has no support for get_unmapped_area function */
739 #if 0
740 static unsigned long mlx4_ib_get_unmapped_area(struct file *file,
741 			unsigned long addr,
742 			unsigned long len, unsigned long pgoff,
743 			unsigned long flags)
744 {
745 	struct mm_struct *mm;
746 	struct vm_area_struct *vma;
747 	unsigned long start_addr;
748 	unsigned long page_size_order;
749 	unsigned long  command;
750 
751 	mm = current->mm;
752 	if (addr)
753 		return current->mm->get_unmapped_area(file, addr, len,
754 						pgoff, flags);
755 
756 	/* Last 8 bits hold the  command others are data per that command */
757 	command = pgoff & MLX4_IB_MMAP_CMD_MASK;
758 	if (command != MLX4_IB_MMAP_GET_CONTIGUOUS_PAGES)
759 		return current->mm->get_unmapped_area(file, addr, len,
760 						pgoff, flags);
761 
762 	page_size_order = pgoff >> MLX4_IB_MMAP_CMD_BITS;
763 	/* code is based on the huge-pages get_unmapped_area code */
764 	start_addr = mm->free_area_cache;
765 
766 	if (len <= mm->cached_hole_size)
767 		start_addr = TASK_UNMAPPED_BASE;
768 
769 
770 full_search:
771 	addr = ALIGN(start_addr, 1 << page_size_order);
772 
773 	for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
774 		/* At this point:  (!vma || addr < vma->vm_end). */
775 		if (TASK_SIZE - len < addr) {
776 			/*
777 			 * Start a new search - just in case we missed
778 			 * some holes.
779 			 */
780 			if (start_addr != TASK_UNMAPPED_BASE) {
781 				start_addr = TASK_UNMAPPED_BASE;
782 				goto full_search;
783 			}
784 			return -ENOMEM;
785 		}
786 
787 		if (!vma || addr + len <= vma->vm_start)
788 			return addr;
789 		addr = ALIGN(vma->vm_end, 1 << page_size_order);
790 	}
791 }
792 #endif
793 
mlx4_ib_mmap(struct ib_ucontext * context,struct vm_area_struct * vma)794 static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
795 {
796 	struct mlx4_ib_dev *dev = to_mdev(context->device);
797 
798 	/* Last 8 bits hold the  command others are data per that command */
799 	unsigned long  command = vma->vm_pgoff & MLX4_IB_MMAP_CMD_MASK;
800 
801 	if (command < MLX4_IB_MMAP_GET_CONTIGUOUS_PAGES) {
802 		/* compatability handling for commands 0 & 1*/
803 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
804 			return -EINVAL;
805 	}
806 	if (command == MLX4_IB_MMAP_UAR_PAGE) {
807 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
808 
809 		if (io_remap_pfn_range(vma, vma->vm_start,
810 				       to_mucontext(context)->uar.pfn,
811 				       PAGE_SIZE, vma->vm_page_prot))
812 			return -EAGAIN;
813 	} else if (command == MLX4_IB_MMAP_BLUE_FLAME_PAGE &&
814 			dev->dev->caps.bf_reg_size != 0) {
815 		vma->vm_page_prot = pgprot_wc(vma->vm_page_prot);
816 
817 		if (io_remap_pfn_range(vma, vma->vm_start,
818 				       to_mucontext(context)->uar.pfn +
819 				       dev->dev->caps.num_uars,
820 				       PAGE_SIZE, vma->vm_page_prot))
821 			return -EAGAIN;
822 	} else if (command == MLX4_IB_MMAP_GET_HW_CLOCK) {
823 		struct mlx4_clock_params params;
824 		int ret;
825 
826 		ret = mlx4_get_internal_clock_params(dev->dev, &params);
827 		if (ret)
828 			return ret;
829 
830 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
831 
832 		if (io_remap_pfn_range(vma, vma->vm_start,
833 				       (pci_resource_start(dev->dev->pdev,
834 				       params.bar) + params.offset)
835 				       >> PAGE_SHIFT,
836 				       PAGE_SIZE, vma->vm_page_prot))
837 			return -EAGAIN;
838 	} else
839 		return -EINVAL;
840 
841 	return 0;
842 }
843 
mlx4_ib_ioctl(struct ib_ucontext * context,unsigned int cmd,unsigned long arg)844 static int mlx4_ib_ioctl(struct ib_ucontext *context, unsigned int cmd,
845 			 unsigned long arg)
846 {
847 	struct mlx4_ib_dev *dev = to_mdev(context->device);
848 	int ret;
849         int offset;
850 
851 	switch (cmd) {
852 	case MLX4_IOCHWCLOCKOFFSET: {
853 		struct mlx4_clock_params params;
854 		int ret;
855 		ret = mlx4_get_internal_clock_params(dev->dev, &params);
856 		if (!ret) {
857                         offset = params.offset % PAGE_SIZE;
858 			ret = put_user(offset,
859 					 (int *)arg);
860 			return sizeof(int);
861 		} else {
862 			return ret;
863 		}
864 	}
865 	default: {
866 		pr_err("mlx4_ib: invalid ioctl %u command with arg %lX\n",
867 		       cmd, arg);
868 		return -ENOTTY;
869 	}
870 	}
871 
872 	return ret;
873 }
874 
mlx4_ib_query_values(struct ib_device * device,int q_values,struct ib_device_values * values)875 static int mlx4_ib_query_values(struct ib_device *device, int q_values,
876 				struct ib_device_values *values)
877 {
878 	struct mlx4_ib_dev *dev = to_mdev(device);
879 	cycle_t cycles;
880 
881 	values->values_mask = 0;
882 	if (q_values & IBV_VALUES_HW_CLOCK) {
883 		cycles = mlx4_read_clock(dev->dev);
884 		if (cycles < 0) {
885 			values->hwclock = cycles & CORE_CLOCK_MASK;
886 			values->values_mask |= IBV_VALUES_HW_CLOCK;
887 		}
888 		q_values &= ~IBV_VALUES_HW_CLOCK;
889 	}
890 
891 	if (q_values)
892 		return -ENOTTY;
893 
894 	return 0;
895 }
896 
mlx4_ib_alloc_pd(struct ib_device * ibdev,struct ib_ucontext * context,struct ib_udata * udata)897 static struct ib_pd *mlx4_ib_alloc_pd(struct ib_device *ibdev,
898 				      struct ib_ucontext *context,
899 				      struct ib_udata *udata)
900 {
901 	struct mlx4_ib_pd *pd;
902 	int err;
903 
904 	pd = kmalloc(sizeof *pd, GFP_KERNEL);
905 	if (!pd)
906 		return ERR_PTR(-ENOMEM);
907 
908 	err = mlx4_pd_alloc(to_mdev(ibdev)->dev, &pd->pdn);
909 	if (err) {
910 		kfree(pd);
911 		return ERR_PTR(err);
912 	}
913 
914 	if (context)
915 		if (ib_copy_to_udata(udata, &pd->pdn, sizeof (__u32))) {
916 			mlx4_pd_free(to_mdev(ibdev)->dev, pd->pdn);
917 			kfree(pd);
918 			return ERR_PTR(-EFAULT);
919 		}
920 
921 	return &pd->ibpd;
922 }
923 
mlx4_ib_dealloc_pd(struct ib_pd * pd)924 static int mlx4_ib_dealloc_pd(struct ib_pd *pd)
925 {
926 	mlx4_pd_free(to_mdev(pd->device)->dev, to_mpd(pd)->pdn);
927 	kfree(pd);
928 
929 	return 0;
930 }
931 
mlx4_ib_alloc_xrcd(struct ib_device * ibdev,struct ib_ucontext * context,struct ib_udata * udata)932 static struct ib_xrcd *mlx4_ib_alloc_xrcd(struct ib_device *ibdev,
933 					  struct ib_ucontext *context,
934 					  struct ib_udata *udata)
935 {
936 	struct mlx4_ib_xrcd *xrcd;
937 	int err;
938 
939 	if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC))
940 		return ERR_PTR(-ENOSYS);
941 
942 	xrcd = kmalloc(sizeof *xrcd, GFP_KERNEL);
943 	if (!xrcd)
944 		return ERR_PTR(-ENOMEM);
945 
946 	err = mlx4_xrcd_alloc(to_mdev(ibdev)->dev, &xrcd->xrcdn);
947 	if (err)
948 		goto err1;
949 
950 	xrcd->pd = ib_alloc_pd(ibdev);
951 	if (IS_ERR(xrcd->pd)) {
952 		err = PTR_ERR(xrcd->pd);
953 		goto err2;
954 	}
955 
956 	xrcd->cq = ib_create_cq(ibdev, NULL, NULL, xrcd, 1, 0);
957 	if (IS_ERR(xrcd->cq)) {
958 		err = PTR_ERR(xrcd->cq);
959 		goto err3;
960 	}
961 
962 	return &xrcd->ibxrcd;
963 
964 err3:
965 	ib_dealloc_pd(xrcd->pd);
966 err2:
967 	mlx4_xrcd_free(to_mdev(ibdev)->dev, xrcd->xrcdn);
968 err1:
969 	kfree(xrcd);
970 	return ERR_PTR(err);
971 }
972 
mlx4_ib_dealloc_xrcd(struct ib_xrcd * xrcd)973 static int mlx4_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
974 {
975 	ib_destroy_cq(to_mxrcd(xrcd)->cq);
976 	ib_dealloc_pd(to_mxrcd(xrcd)->pd);
977 	mlx4_xrcd_free(to_mdev(xrcd->device)->dev, to_mxrcd(xrcd)->xrcdn);
978 	kfree(xrcd);
979 
980 	return 0;
981 }
982 
add_gid_entry(struct ib_qp * ibqp,union ib_gid * gid)983 static int add_gid_entry(struct ib_qp *ibqp, union ib_gid *gid)
984 {
985 	struct mlx4_ib_qp *mqp = to_mqp(ibqp);
986 	struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
987 	struct mlx4_ib_gid_entry *ge;
988 
989 	ge = kzalloc(sizeof *ge, GFP_KERNEL);
990 	if (!ge)
991 		return -ENOMEM;
992 
993 	ge->gid = *gid;
994 	if (mlx4_ib_add_mc(mdev, mqp, gid)) {
995 		ge->port = mqp->port;
996 		ge->added = 1;
997 	}
998 
999 	mutex_lock(&mqp->mutex);
1000 	list_add_tail(&ge->list, &mqp->gid_list);
1001 	mutex_unlock(&mqp->mutex);
1002 
1003 	return 0;
1004 }
1005 
mlx4_ib_add_mc(struct mlx4_ib_dev * mdev,struct mlx4_ib_qp * mqp,union ib_gid * gid)1006 int mlx4_ib_add_mc(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
1007 		   union ib_gid *gid)
1008 {
1009 	u8 mac[6];
1010 	struct net_device *ndev;
1011 	int ret = 0;
1012 
1013 	if (!mqp->port)
1014 		return 0;
1015 
1016 	spin_lock(&mdev->iboe.lock);
1017 	ndev = mdev->iboe.netdevs[mqp->port - 1];
1018 	if (ndev)
1019 		dev_hold(ndev);
1020 	spin_unlock(&mdev->iboe.lock);
1021 
1022 	if (ndev) {
1023 		rdma_get_mcast_mac((struct in6_addr *)gid, mac);
1024 		rtnl_lock();
1025 		dev_mc_add(mdev->iboe.netdevs[mqp->port - 1], mac, 6, 0);
1026 		ret = 1;
1027 		rtnl_unlock();
1028 		dev_put(ndev);
1029 	}
1030 
1031 	return ret;
1032 }
1033 
1034 struct mlx4_ib_steering {
1035 	struct list_head list;
1036 	u64 reg_id;
1037 	union ib_gid gid;
1038 };
1039 
parse_flow_attr(struct mlx4_dev * dev,union ib_flow_spec * ib_spec,struct _rule_hw * mlx4_spec)1040 static int parse_flow_attr(struct mlx4_dev *dev,
1041 			   union ib_flow_spec *ib_spec,
1042 			   struct _rule_hw *mlx4_spec)
1043 {
1044 	enum mlx4_net_trans_rule_id type;
1045 
1046 	switch (ib_spec->type) {
1047 	case IB_FLOW_SPEC_ETH:
1048 		type = MLX4_NET_TRANS_RULE_ID_ETH;
1049 		memcpy(mlx4_spec->eth.dst_mac, ib_spec->eth.val.dst_mac,
1050 		       ETH_ALEN);
1051 		memcpy(mlx4_spec->eth.dst_mac_msk, ib_spec->eth.mask.dst_mac,
1052 		       ETH_ALEN);
1053 		mlx4_spec->eth.vlan_tag = ib_spec->eth.val.vlan_tag;
1054 		mlx4_spec->eth.vlan_tag_msk = ib_spec->eth.mask.vlan_tag;
1055 		break;
1056 
1057 	case IB_FLOW_SPEC_IB:
1058 		type = MLX4_NET_TRANS_RULE_ID_IB;
1059 		mlx4_spec->ib.l3_qpn = ib_spec->ib.val.l3_type_qpn;
1060 		mlx4_spec->ib.qpn_mask = ib_spec->ib.mask.l3_type_qpn;
1061 		memcpy(&mlx4_spec->ib.dst_gid, ib_spec->ib.val.dst_gid, 16);
1062 		memcpy(&mlx4_spec->ib.dst_gid_msk,
1063 		       ib_spec->ib.mask.dst_gid, 16);
1064 		break;
1065 
1066 	case IB_FLOW_SPEC_IPV4:
1067 		type = MLX4_NET_TRANS_RULE_ID_IPV4;
1068 		mlx4_spec->ipv4.src_ip = ib_spec->ipv4.val.src_ip;
1069 		mlx4_spec->ipv4.src_ip_msk = ib_spec->ipv4.mask.src_ip;
1070 		mlx4_spec->ipv4.dst_ip = ib_spec->ipv4.val.dst_ip;
1071 		mlx4_spec->ipv4.dst_ip_msk = ib_spec->ipv4.mask.dst_ip;
1072 		break;
1073 
1074 	case IB_FLOW_SPEC_TCP:
1075 	case IB_FLOW_SPEC_UDP:
1076 		type = ib_spec->type == IB_FLOW_SPEC_TCP ?
1077 					MLX4_NET_TRANS_RULE_ID_TCP :
1078 					MLX4_NET_TRANS_RULE_ID_UDP;
1079 		mlx4_spec->tcp_udp.dst_port = ib_spec->tcp_udp.val.dst_port;
1080 		mlx4_spec->tcp_udp.dst_port_msk =
1081 			ib_spec->tcp_udp.mask.dst_port;
1082 		mlx4_spec->tcp_udp.src_port = ib_spec->tcp_udp.val.src_port;
1083 		mlx4_spec->tcp_udp.src_port_msk =
1084 			ib_spec->tcp_udp.mask.src_port;
1085 		break;
1086 
1087 	default:
1088 		return -EINVAL;
1089 	}
1090 	if (map_sw_to_hw_steering_id(dev, type) < 0 ||
1091 	    hw_rule_sz(dev, type) < 0)
1092 		return -EINVAL;
1093 	mlx4_spec->id = cpu_to_be16(map_sw_to_hw_steering_id(dev, type));
1094 	mlx4_spec->size = hw_rule_sz(dev, type) >> 2;
1095 	return hw_rule_sz(dev, type);
1096 }
1097 
__mlx4_ib_create_flow(struct ib_qp * qp,struct ib_flow_attr * flow_attr,int domain,enum mlx4_net_trans_promisc_mode flow_type,u64 * reg_id)1098 static int __mlx4_ib_create_flow(struct ib_qp *qp, struct ib_flow_attr *flow_attr,
1099 			  int domain,
1100 			  enum mlx4_net_trans_promisc_mode flow_type,
1101 			  u64 *reg_id)
1102 {
1103 	int ret, i;
1104 	int size = 0;
1105 	void *ib_flow;
1106 	struct mlx4_ib_dev *mdev = to_mdev(qp->device);
1107 	struct mlx4_cmd_mailbox *mailbox;
1108 	struct mlx4_net_trans_rule_hw_ctrl *ctrl;
1109 	size_t rule_size = sizeof(struct mlx4_net_trans_rule_hw_ctrl) +
1110 			   (sizeof(struct _rule_hw) * flow_attr->num_of_specs);
1111 
1112 	static const u16 __mlx4_domain[] = {
1113 		[IB_FLOW_DOMAIN_USER] = MLX4_DOMAIN_UVERBS,
1114 		[IB_FLOW_DOMAIN_ETHTOOL] = MLX4_DOMAIN_ETHTOOL,
1115 		[IB_FLOW_DOMAIN_RFS] = MLX4_DOMAIN_RFS,
1116 		[IB_FLOW_DOMAIN_NIC] = MLX4_DOMAIN_NIC,
1117 	};
1118 
1119 	if (flow_attr->priority > MLX4_IB_FLOW_MAX_PRIO) {
1120 		pr_err("Invalid priority value.\n");
1121 		return -EINVAL;
1122                     }
1123 	if (domain >= IB_FLOW_DOMAIN_NUM) {
1124 		pr_err("Invalid domain value.\n");
1125 		return -EINVAL;
1126 	}
1127 	if (map_sw_to_hw_steering_mode(mdev->dev, flow_type) < 0)
1128 		return -EINVAL;
1129 
1130 	mailbox = mlx4_alloc_cmd_mailbox(mdev->dev);
1131 	if (IS_ERR(mailbox))
1132 		return PTR_ERR(mailbox);
1133 	memset(mailbox->buf, 0, rule_size);
1134 	ctrl = mailbox->buf;
1135 
1136 	ctrl->prio = cpu_to_be16(__mlx4_domain[domain] |
1137 				 flow_attr->priority);
1138 	ctrl->type = map_sw_to_hw_steering_mode(mdev->dev, flow_type);
1139 	ctrl->port = flow_attr->port;
1140 	ctrl->qpn = cpu_to_be32(qp->qp_num);
1141 
1142 	if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_ALLOW_LOOP_BACK)
1143 		ctrl->flags = (1 << 3);
1144 
1145 	ib_flow = flow_attr + 1;
1146 	size += sizeof(struct mlx4_net_trans_rule_hw_ctrl);
1147 	for (i = 0; i < flow_attr->num_of_specs; i++) {
1148 		ret = parse_flow_attr(mdev->dev, ib_flow, mailbox->buf + size);
1149 		if (ret < 0) {
1150 			mlx4_free_cmd_mailbox(mdev->dev, mailbox);
1151 			return -EINVAL;
1152 		}
1153 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
1154 		size += ret;
1155 	}
1156 
1157 	ret = mlx4_cmd_imm(mdev->dev, mailbox->dma, reg_id, size >> 2, 0,
1158 			   MLX4_QP_FLOW_STEERING_ATTACH, MLX4_CMD_TIME_CLASS_A,
1159 			   MLX4_CMD_NATIVE);
1160 	if (ret == -ENOMEM)
1161 		pr_err("mcg table is full. Fail to register network rule.\n");
1162 	else if (ret == -ENXIO)
1163 		pr_err("Device managed flow steering is disabled. Fail to register network rule.\n");
1164 	else if (ret)
1165 		pr_err("Invalid argumant. Fail to register network rule.\n");
1166 	mlx4_free_cmd_mailbox(mdev->dev, mailbox);
1167 	return ret;
1168 }
1169 
__mlx4_ib_destroy_flow(struct mlx4_dev * dev,u64 reg_id)1170 static int __mlx4_ib_destroy_flow(struct mlx4_dev *dev, u64 reg_id)
1171 {
1172 	int err;
1173 	err = mlx4_cmd(dev, reg_id, 0, 0,
1174 		       MLX4_QP_FLOW_STEERING_DETACH, MLX4_CMD_TIME_CLASS_A,
1175 		       MLX4_CMD_NATIVE);
1176 	if (err)
1177 		pr_err("Fail to detach network rule. registration id = 0x%llx\n",
1178 		       (unsigned long long)reg_id);
1179 	return err;
1180 }
1181 
mlx4_ib_create_flow(struct ib_qp * qp,struct ib_flow_attr * flow_attr,int domain)1182 static struct ib_flow *mlx4_ib_create_flow(struct ib_qp *qp,
1183 				    struct ib_flow_attr *flow_attr,
1184 				    int domain)
1185 {
1186 	int err = 0, i = 0;
1187 	struct mlx4_ib_flow *mflow;
1188 	enum mlx4_net_trans_promisc_mode type[2];
1189 
1190 	memset(type, 0, sizeof(type));
1191 
1192 	mflow = kzalloc(sizeof(struct mlx4_ib_flow), GFP_KERNEL);
1193 	if (!mflow) {
1194 		err = -ENOMEM;
1195 		goto err_free;
1196 	}
1197 
1198 	switch (flow_attr->type) {
1199 	case IB_FLOW_ATTR_NORMAL:
1200 		type[0] = MLX4_FS_REGULAR;
1201 			break;
1202 
1203 	case IB_FLOW_ATTR_ALL_DEFAULT:
1204 		type[0] = MLX4_FS_ALL_DEFAULT;
1205 		break;
1206 
1207 	case IB_FLOW_ATTR_MC_DEFAULT:
1208 		type[0] = MLX4_FS_MC_DEFAULT;
1209 		break;
1210 
1211 	case IB_FLOW_ATTR_SNIFFER:
1212 		type[0] = MLX4_FS_UC_SNIFFER;
1213 		type[1] = MLX4_FS_MC_SNIFFER;
1214 		break;
1215 
1216 	default:
1217 		err = -EINVAL;
1218 		goto err_free;
1219 	}
1220 
1221 	while (i < ARRAY_SIZE(type) && type[i]) {
1222 		err = __mlx4_ib_create_flow(qp, flow_attr, domain, type[i],
1223 					    &mflow->reg_id[i]);
1224 	if (err)
1225 			goto err_free;
1226 		i++;
1227 	}
1228 
1229 	return &mflow->ibflow;
1230 
1231 err_free:
1232 	kfree(mflow);
1233 	return ERR_PTR(err);
1234 }
1235 
mlx4_ib_destroy_flow(struct ib_flow * flow_id)1236 static int mlx4_ib_destroy_flow(struct ib_flow *flow_id)
1237 {
1238 	int err, ret = 0;
1239 	int i = 0;
1240 	struct mlx4_ib_dev *mdev = to_mdev(flow_id->qp->device);
1241 	struct mlx4_ib_flow *mflow = to_mflow(flow_id);
1242 
1243 	while (i < ARRAY_SIZE(mflow->reg_id) && mflow->reg_id[i]) {
1244 		err = __mlx4_ib_destroy_flow(mdev->dev, mflow->reg_id[i]);
1245 		if (err)
1246 			ret = err;
1247 		i++;
1248 	}
1249 
1250 	kfree(mflow);
1251 	return ret;
1252 }
1253 
find_gid_entry(struct mlx4_ib_qp * qp,u8 * raw)1254 static struct mlx4_ib_gid_entry *find_gid_entry(struct mlx4_ib_qp *qp, u8 *raw)
1255 {
1256 	struct mlx4_ib_gid_entry *ge;
1257 	struct mlx4_ib_gid_entry *tmp;
1258 	struct mlx4_ib_gid_entry *ret = NULL;
1259 
1260 	list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
1261 		if (!memcmp(raw, ge->gid.raw, 16)) {
1262 			ret = ge;
1263 			break;
1264 		}
1265 	}
1266 
1267 	return ret;
1268 }
1269 
1270 
del_gid_entry(struct ib_qp * ibqp,union ib_gid * gid)1271 static int del_gid_entry(struct ib_qp *ibqp, union ib_gid *gid)
1272 {
1273 	struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
1274 	struct mlx4_ib_qp *mqp = to_mqp(ibqp);
1275 	struct mlx4_ib_gid_entry *ge;
1276 	struct net_device *ndev;
1277 	u8 mac[6];
1278 
1279 	mutex_lock(&mqp->mutex);
1280 	ge = find_gid_entry(mqp, gid->raw);
1281 	if (ge) {
1282 		spin_lock(&mdev->iboe.lock);
1283 		ndev = ge->added ? mdev->iboe.netdevs[ge->port - 1] : NULL;
1284 		if (ndev)
1285 			dev_hold(ndev);
1286 		spin_unlock(&mdev->iboe.lock);
1287 		rdma_get_mcast_mac((struct in6_addr *)gid, mac);
1288 		if (ndev) {
1289 			rtnl_lock();
1290 			dev_mc_delete(mdev->iboe.netdevs[ge->port - 1], mac, 6, 0);
1291 			rtnl_unlock();
1292 			dev_put(ndev);
1293 		}
1294 		list_del(&ge->list);
1295 		kfree(ge);
1296 	} else
1297 		pr_warn("could not find mgid entry\n");
1298 
1299 	mutex_unlock(&mqp->mutex);
1300 	return ge != 0 ? 0 : -EINVAL;
1301 }
1302 
_mlx4_ib_mcg_detach(struct ib_qp * ibqp,union ib_gid * gid,u16 lid,int count)1303 static int _mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid,
1304 			       int count)
1305 {
1306 	int err;
1307 	struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
1308 	struct mlx4_ib_qp *mqp = to_mqp(ibqp);
1309 	u64 reg_id = 0;
1310 	int record_err = 0;
1311 
1312 	if (mdev->dev->caps.steering_mode ==
1313 	    MLX4_STEERING_MODE_DEVICE_MANAGED) {
1314 		struct mlx4_ib_steering *ib_steering;
1315 		struct mlx4_ib_steering *tmp;
1316 		LIST_HEAD(temp);
1317 
1318 		mutex_lock(&mqp->mutex);
1319 		list_for_each_entry_safe(ib_steering, tmp, &mqp->steering_rules,
1320 					 list) {
1321 			if (memcmp(ib_steering->gid.raw, gid->raw, 16))
1322 				continue;
1323 
1324 			if (--count < 0)
1325 				break;
1326 
1327 			list_del(&ib_steering->list);
1328 			list_add(&ib_steering->list, &temp);
1329 		}
1330 		mutex_unlock(&mqp->mutex);
1331 		list_for_each_entry_safe(ib_steering, tmp, &temp,
1332 					 list) {
1333 			reg_id = ib_steering->reg_id;
1334 
1335 			err = mlx4_multicast_detach(mdev->dev, &mqp->mqp,
1336 					gid->raw,
1337 					(ibqp->qp_type == IB_QPT_RAW_PACKET) ?
1338 					MLX4_PROT_ETH : MLX4_PROT_IB_IPV6,
1339 					reg_id);
1340 			if (err) {
1341 				record_err = record_err ?: err;
1342 				continue;
1343 			}
1344 
1345 			err = del_gid_entry(ibqp, gid);
1346 			if (err) {
1347 				record_err = record_err ?: err;
1348 				continue;
1349 			}
1350 
1351 			list_del(&ib_steering->list);
1352 			kfree(ib_steering);
1353 		}
1354 		mutex_lock(&mqp->mutex);
1355 		list_for_each_entry(ib_steering, &temp, list) {
1356 			list_add(&ib_steering->list, &mqp->steering_rules);
1357 		}
1358 		mutex_unlock(&mqp->mutex);
1359 		if (count) {
1360 			pr_warn("Couldn't release all reg_ids for mgid. Steering rule is left attached\n");
1361 			return -EINVAL;
1362 		}
1363 
1364 	} else {
1365 		if (mdev->dev->caps.steering_mode == MLX4_STEERING_MODE_B0 &&
1366 		    ibqp->qp_type == IB_QPT_RAW_PACKET)
1367 			gid->raw[5] = mqp->port;
1368 
1369 		err = mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
1370 				(ibqp->qp_type == IB_QPT_RAW_PACKET) ?
1371 				MLX4_PROT_ETH : MLX4_PROT_IB_IPV6,
1372 				reg_id);
1373 		if (err)
1374 			return err;
1375 
1376 		err = del_gid_entry(ibqp, gid);
1377 
1378 		if (err)
1379 			return err;
1380 	}
1381 
1382 	return record_err;
1383 }
1384 
mlx4_ib_mcg_detach(struct ib_qp * ibqp,union ib_gid * gid,u16 lid)1385 static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
1386 {
1387 	struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
1388 	int count = (mdev->dev->caps.steering_mode ==
1389 		     MLX4_STEERING_MODE_DEVICE_MANAGED) ?
1390 		    mdev->dev->caps.num_ports : 1;
1391 
1392 	return _mlx4_ib_mcg_detach(ibqp, gid, lid, count);
1393 }
1394 
mlx4_ib_mcg_attach(struct ib_qp * ibqp,union ib_gid * gid,u16 lid)1395 static int mlx4_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
1396 {
1397 	int err = -ENODEV;
1398 	struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
1399 	struct mlx4_ib_qp *mqp = to_mqp(ibqp);
1400 	DECLARE_BITMAP(ports, MLX4_MAX_PORTS);
1401 	int i = 0;
1402 
1403 	if (mdev->dev->caps.steering_mode == MLX4_STEERING_MODE_B0 &&
1404 	    ibqp->qp_type == IB_QPT_RAW_PACKET)
1405 		gid->raw[5] = mqp->port;
1406 
1407 	if (mdev->dev->caps.steering_mode ==
1408 	    MLX4_STEERING_MODE_DEVICE_MANAGED) {
1409 		bitmap_fill(ports, mdev->dev->caps.num_ports);
1410 	} else {
1411 		if (mqp->port <= mdev->dev->caps.num_ports) {
1412 			bitmap_zero(ports, mdev->dev->caps.num_ports);
1413 			set_bit(0, ports);
1414 		} else {
1415 			return -EINVAL;
1416 		}
1417 	}
1418 
1419 	for (; i < mdev->dev->caps.num_ports; i++) {
1420 		u64 reg_id;
1421 		struct mlx4_ib_steering *ib_steering = NULL;
1422 		if (!test_bit(i, ports))
1423 			continue;
1424 		if (mdev->dev->caps.steering_mode ==
1425 		    MLX4_STEERING_MODE_DEVICE_MANAGED) {
1426 			ib_steering = kmalloc(sizeof(*ib_steering), GFP_KERNEL);
1427 			if (!ib_steering)
1428 				goto err_add;
1429 		}
1430 
1431 		err = mlx4_multicast_attach(mdev->dev, &mqp->mqp,
1432 			gid->raw, i + 1,
1433 			!!(mqp->flags &
1434 				MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK),
1435 			(ibqp->qp_type == IB_QPT_RAW_PACKET) ?
1436 			MLX4_PROT_ETH : MLX4_PROT_IB_IPV6,
1437 			&reg_id);
1438 		if (err) {
1439 			kfree(ib_steering);
1440 			goto err_add;
1441 		}
1442 
1443 		err = add_gid_entry(ibqp, gid);
1444 		if (err) {
1445 			mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
1446 					      MLX4_PROT_IB_IPV6, reg_id);
1447 			kfree(ib_steering);
1448 			goto err_add;
1449 		}
1450 
1451 		if (ib_steering) {
1452 			memcpy(ib_steering->gid.raw, gid->raw, 16);
1453 			mutex_lock(&mqp->mutex);
1454 			list_add(&ib_steering->list, &mqp->steering_rules);
1455 			mutex_unlock(&mqp->mutex);
1456 			ib_steering->reg_id = reg_id;
1457 		}
1458 	}
1459 
1460 
1461 	return 0;
1462 
1463 err_add:
1464 	if (i > 0)
1465 		_mlx4_ib_mcg_detach(ibqp, gid, lid, i);
1466 
1467 	return err;
1468 }
1469 
init_node_data(struct mlx4_ib_dev * dev)1470 static int init_node_data(struct mlx4_ib_dev *dev)
1471 {
1472 	struct ib_smp *in_mad  = NULL;
1473 	struct ib_smp *out_mad = NULL;
1474 	int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
1475 	int err = -ENOMEM;
1476 
1477 	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
1478 	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
1479 	if (!in_mad || !out_mad)
1480 		goto out;
1481 
1482 	init_query_mad(in_mad);
1483 	in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
1484 	if (mlx4_is_master(dev->dev))
1485 		mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
1486 
1487 	err = mlx4_MAD_IFC(dev, mad_ifc_flags, 1, NULL, NULL, in_mad, out_mad);
1488 	if (err)
1489 		goto out;
1490 
1491 	memcpy(dev->ib_dev.node_desc, out_mad->data, 64);
1492 
1493 	in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
1494 
1495 	err = mlx4_MAD_IFC(dev, mad_ifc_flags, 1, NULL, NULL, in_mad, out_mad);
1496 	if (err)
1497 		goto out;
1498 
1499 	dev->dev->rev_id = be32_to_cpup((__be32 *) (out_mad->data + 32));
1500 	memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
1501 
1502 out:
1503 	kfree(in_mad);
1504 	kfree(out_mad);
1505 	return err;
1506 }
1507 
show_hca(struct device * device,struct device_attribute * attr,char * buf)1508 static ssize_t show_hca(struct device *device, struct device_attribute *attr,
1509 			char *buf)
1510 {
1511 	struct mlx4_ib_dev *dev =
1512 		container_of(device, struct mlx4_ib_dev, ib_dev.dev);
1513 	return sprintf(buf, "MT%d\n", dev->dev->pdev->device);
1514 }
1515 
show_fw_ver(struct device * device,struct device_attribute * attr,char * buf)1516 static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
1517 			   char *buf)
1518 {
1519 	struct mlx4_ib_dev *dev =
1520 		container_of(device, struct mlx4_ib_dev, ib_dev.dev);
1521 	return sprintf(buf, "%d.%d.%d\n", (int) (dev->dev->caps.fw_ver >> 32),
1522 		       (int) (dev->dev->caps.fw_ver >> 16) & 0xffff,
1523 		       (int) dev->dev->caps.fw_ver & 0xffff);
1524 }
1525 
show_rev(struct device * device,struct device_attribute * attr,char * buf)1526 static ssize_t show_rev(struct device *device, struct device_attribute *attr,
1527 			char *buf)
1528 {
1529 	struct mlx4_ib_dev *dev =
1530 		container_of(device, struct mlx4_ib_dev, ib_dev.dev);
1531 	return sprintf(buf, "%x\n", dev->dev->rev_id);
1532 }
1533 
show_board(struct device * device,struct device_attribute * attr,char * buf)1534 static ssize_t show_board(struct device *device, struct device_attribute *attr,
1535 			  char *buf)
1536 {
1537 	struct mlx4_ib_dev *dev =
1538 		container_of(device, struct mlx4_ib_dev, ib_dev.dev);
1539 	return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN,
1540 		       dev->dev->board_id);
1541 }
1542 
show_vsd(struct device * device,struct device_attribute * attr,char * buf)1543 static ssize_t show_vsd(struct device *device, struct device_attribute *attr,
1544 			  char *buf)
1545 {
1546 	struct mlx4_ib_dev *dev =
1547 		container_of(device, struct mlx4_ib_dev, ib_dev.dev);
1548 	ssize_t len = MLX4_VSD_LEN;
1549 
1550 	if (dev->dev->vsd_vendor_id == PCI_VENDOR_ID_MELLANOX)
1551 		len = sprintf(buf, "%.*s\n", MLX4_VSD_LEN, dev->dev->vsd);
1552 	else
1553 		memcpy(buf, dev->dev->vsd, MLX4_VSD_LEN);
1554 
1555 	return len;
1556 }
1557 
1558 static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
1559 static DEVICE_ATTR(fw_ver,   S_IRUGO, show_fw_ver, NULL);
1560 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
1561 static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
1562 static DEVICE_ATTR(vsd,      S_IRUGO, show_vsd,    NULL);
1563 
1564 static struct device_attribute *mlx4_class_attributes[] = {
1565 	&dev_attr_hw_rev,
1566 	&dev_attr_fw_ver,
1567 	&dev_attr_hca_type,
1568 	&dev_attr_board_id,
1569 	&dev_attr_vsd
1570 };
1571 
mlx4_addrconf_ifid_eui48(u8 * eui,u16 vlan_id,struct net_device * dev,u8 port)1572 static void mlx4_addrconf_ifid_eui48(u8 *eui, u16 vlan_id, struct net_device *dev, u8 port)
1573 {
1574         memcpy(eui, IF_LLADDR(dev), 3);
1575         memcpy(eui + 5, IF_LLADDR(dev) + 3, 3);
1576 	if (vlan_id < 0x1000) {
1577 		eui[3] = vlan_id >> 8;
1578 		eui[4] = vlan_id & 0xff;
1579 	} else {
1580 		eui[3] = 0xff;
1581 		eui[4] = 0xfe;
1582 	}
1583 	eui[0] ^= 2;
1584 }
1585 
update_gids_task(struct work_struct * work)1586 static void update_gids_task(struct work_struct *work)
1587 {
1588 	struct update_gid_work *gw = container_of(work, struct update_gid_work, work);
1589 	struct mlx4_cmd_mailbox *mailbox;
1590 	union ib_gid *gids;
1591 	int err;
1592 	struct mlx4_dev	*dev = gw->dev->dev;
1593 
1594 
1595 	mailbox = mlx4_alloc_cmd_mailbox(dev);
1596 	if (IS_ERR(mailbox)) {
1597 		pr_warn("update gid table failed %ld\n", PTR_ERR(mailbox));
1598 		goto free;
1599 	}
1600 
1601 	gids = mailbox->buf;
1602 	memcpy(gids, gw->gids, sizeof gw->gids);
1603 
1604 	if (mlx4_ib_port_link_layer(&gw->dev->ib_dev, gw->port) ==
1605 					IB_LINK_LAYER_ETHERNET) {
1606 		err = mlx4_cmd(dev, mailbox->dma,
1607 			       MLX4_SET_PORT_GID_TABLE << 8 | gw->port,
1608 		       1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
1609 		       MLX4_CMD_WRAPPED);
1610 
1611 	if (err)
1612 		pr_warn("set port command failed\n");
1613 		else
1614 			mlx4_ib_dispatch_event(gw->dev, gw->port,
1615 					       IB_EVENT_GID_CHANGE);
1616 	}
1617 
1618 	mlx4_free_cmd_mailbox(dev, mailbox);
1619 free:
1620 	kfree(gw);
1621 }
1622 
reset_gids_task(struct work_struct * work)1623 static void reset_gids_task(struct work_struct *work)
1624 {
1625 	struct update_gid_work *gw =
1626 			container_of(work, struct update_gid_work, work);
1627 	struct mlx4_cmd_mailbox *mailbox;
1628 	union ib_gid *gids;
1629 	int err;
1630 	struct mlx4_dev	*dev = gw->dev->dev;
1631 
1632 	mailbox = mlx4_alloc_cmd_mailbox(dev);
1633 	if (IS_ERR(mailbox)) {
1634 		pr_warn("reset gid table failed\n");
1635 		goto free;
1636 	}
1637 
1638 	gids = mailbox->buf;
1639 	memcpy(gids, gw->gids, sizeof(gw->gids));
1640 
1641 	if (mlx4_ib_port_link_layer(&gw->dev->ib_dev, 1) ==
1642 					IB_LINK_LAYER_ETHERNET &&
1643 					dev->caps.num_ports > 0) {
1644 		err = mlx4_cmd(dev, mailbox->dma,
1645 			       MLX4_SET_PORT_GID_TABLE << 8 | 1,
1646 			       1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
1647 			       MLX4_CMD_WRAPPED);
1648 		if (err)
1649 			pr_warn("set port 1 command failed\n");
1650 	}
1651 
1652 	if (mlx4_ib_port_link_layer(&gw->dev->ib_dev, 2) ==
1653 					IB_LINK_LAYER_ETHERNET &&
1654 					dev->caps.num_ports > 1) {
1655 		err = mlx4_cmd(dev, mailbox->dma,
1656 			       MLX4_SET_PORT_GID_TABLE << 8 | 2,
1657 			       1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
1658 			       MLX4_CMD_WRAPPED);
1659 		if (err)
1660 			pr_warn("set port 2 command failed\n");
1661 	}
1662 
1663 	mlx4_free_cmd_mailbox(dev, mailbox);
1664 free:
1665 	kfree(gw);
1666 }
1667 
update_gid_table(struct mlx4_ib_dev * dev,int port,union ib_gid * gid,int clear,int default_gid)1668 static int update_gid_table(struct mlx4_ib_dev *dev, int port,
1669 		union ib_gid *gid, int clear, int default_gid)
1670 {
1671 	struct update_gid_work *work;
1672 	int i;
1673 	int need_update = 0;
1674 	int free = -1;
1675 	int found = -1;
1676 	int max_gids;
1677 	int start_index = !default_gid;
1678 
1679 	max_gids = dev->dev->caps.gid_table_len[port];
1680 	for (i = start_index; i < max_gids; ++i) {
1681 		if (!memcmp(&dev->iboe.gid_table[port - 1][i], gid,
1682 		    sizeof(*gid)))
1683 			found = i;
1684 
1685 		if (clear) {
1686 			if (found >= 0) {
1687 				need_update = 1;
1688 				dev->iboe.gid_table[port - 1][found] = zgid;
1689 					break;
1690 				}
1691 		} else {
1692 			if (found >= 0)
1693 				break;
1694 
1695 			if (free < 0 &&
1696 			    !memcmp(&dev->iboe.gid_table[port - 1][i],
1697 				    &zgid, sizeof(*gid)))
1698 				free = i;
1699 				}
1700 			}
1701 
1702 	if (found == -1 && !clear && free < 0) {
1703 		pr_err("GID table of port %d is full. Can't add "GID_PRINT_FMT"\n",
1704 		       port, GID_PRINT_ARGS(gid));
1705 		return -ENOMEM;
1706 		}
1707 	if (found == -1 && clear) {
1708 		pr_err(GID_PRINT_FMT" is not in GID table of port %d\n", GID_PRINT_ARGS(gid), port);
1709 		return -EINVAL;
1710         }
1711 	if (found == -1 && !clear && free >= 0) {
1712 		dev->iboe.gid_table[port - 1][free] = *gid;
1713 		need_update = 1;
1714         }
1715 
1716 	if (!need_update)
1717 		return 0;
1718 
1719 	work = kzalloc(sizeof *work, GFP_ATOMIC);
1720 	if (!work)
1721 		return -ENOMEM;
1722 
1723 	memcpy(work->gids, dev->iboe.gid_table[port - 1], sizeof(work->gids));
1724 		INIT_WORK(&work->work, update_gids_task);
1725 		work->port = port;
1726 		work->dev = dev;
1727 		queue_work(wq, &work->work);
1728 
1729 	return 0;
1730 }
1731 
reset_gid_table(struct mlx4_ib_dev * dev)1732 static int reset_gid_table(struct mlx4_ib_dev *dev)
1733 {
1734 	struct update_gid_work *work;
1735 
1736 
1737 	work = kzalloc(sizeof(*work), GFP_ATOMIC);
1738 	if (!work)
1739 		return -ENOMEM;
1740 
1741 	memset(dev->iboe.gid_table, 0, sizeof(dev->iboe.gid_table));
1742 	memset(work->gids, 0, sizeof(work->gids));
1743 	INIT_WORK(&work->work, reset_gids_task);
1744 	work->dev = dev;
1745 	queue_work(wq, &work->work);
1746 	return 0;
1747 }
1748 
1749 /* XXX BOND Related - stub (no support for these flags in FBSD)*/
netif_is_bond_master(struct net_device * dev)1750 static inline int netif_is_bond_master(struct net_device *dev)
1751 {
1752 #if 0
1753 	return (dev->flags & IFF_MASTER) && (dev->priv_flags & IFF_BONDING);
1754 #endif
1755         return 0;
1756 }
1757 
mlx4_make_default_gid(struct net_device * dev,union ib_gid * gid,u8 port)1758 static void mlx4_make_default_gid(struct  net_device *dev, union ib_gid *gid, u8 port)
1759 {
1760 	gid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
1761 	mlx4_addrconf_ifid_eui48(&gid->raw[8], 0xffff, dev, port);
1762 }
1763 
mlx4_ib_get_dev_port(struct net_device * dev,struct mlx4_ib_dev * ibdev)1764 static u8 mlx4_ib_get_dev_port(struct net_device *dev, struct mlx4_ib_dev *ibdev)
1765 {
1766 	u8 port = 0;
1767 	struct mlx4_ib_iboe *iboe;
1768 	struct net_device *real_dev = rdma_vlan_dev_real_dev(dev) ?
1769 				rdma_vlan_dev_real_dev(dev) : dev;
1770 
1771 	iboe = &ibdev->iboe;
1772 
1773 	for (port = 1; port <= MLX4_MAX_PORTS; ++port)
1774 		if ((netif_is_bond_master(real_dev) && (real_dev == iboe->masters[port - 1])) ||
1775 		    (!netif_is_bond_master(real_dev) && (real_dev == iboe->netdevs[port - 1])))
1776 		break;
1777 
1778 	return port > MLX4_MAX_PORTS ? 0 : port;
1779 }
1780 
mlx4_ib_get_dev_addr(struct net_device * dev,struct mlx4_ib_dev * ibdev,u8 port)1781 static void mlx4_ib_get_dev_addr(struct net_device *dev, struct mlx4_ib_dev *ibdev, u8 port)
1782 {
1783         struct ifaddr *ifa;
1784 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1785 	struct inet6_dev *in6_dev;
1786 	union ib_gid  *pgid;
1787 	struct inet6_ifaddr *ifp;
1788 #endif
1789 	union ib_gid gid;
1790 
1791 
1792 	if ((port == 0) || (port > MLX4_MAX_PORTS))
1793 		return;
1794 
1795 	/* IPv4 gids */
1796         TAILQ_FOREACH(ifa, &dev->if_addrhead, ifa_link) {
1797                 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET){
1798                         ipv6_addr_set_v4mapped(
1799 				((struct sockaddr_in *) ifa->ifa_addr)->sin_addr.s_addr,
1800 				(struct in6_addr *)&gid);
1801                         update_gid_table(ibdev, port, &gid, 0, 0);
1802                 }
1803 
1804         }
1805 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1806 	/* IPv6 gids */
1807 	in6_dev = in6_dev_get(dev);
1808 	if (in6_dev) {
1809 		read_lock_bh(&in6_dev->lock);
1810 		list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
1811 			pgid = (union ib_gid *)&ifp->addr;
1812 			update_gid_table(ibdev, port, pgid, 0, 0);
1813 	}
1814 		read_unlock_bh(&in6_dev->lock);
1815 		in6_dev_put(in6_dev);
1816 	}
1817 #endif
1818 }
1819 
mlx4_set_default_gid(struct mlx4_ib_dev * ibdev,struct net_device * dev,u8 port)1820 static void mlx4_set_default_gid(struct mlx4_ib_dev *ibdev,
1821 				 struct  net_device *dev, u8 port)
1822 {
1823 	union ib_gid gid;
1824 	mlx4_make_default_gid(dev, &gid, port);
1825 	update_gid_table(ibdev, port, &gid, 0, 1);
1826 }
1827 
mlx4_ib_init_gid_table(struct mlx4_ib_dev * ibdev)1828 static int mlx4_ib_init_gid_table(struct mlx4_ib_dev *ibdev)
1829 {
1830 	struct	net_device *dev;
1831 
1832 	if (reset_gid_table(ibdev))
1833 		return -1;
1834 
1835         IFNET_RLOCK_NOSLEEP();
1836         TAILQ_FOREACH(dev, &V_ifnet, if_link) {
1837 		u8 port = mlx4_ib_get_dev_port(dev, ibdev);
1838 		if (port) {
1839 			if (!rdma_vlan_dev_real_dev(dev) &&
1840 			    !netif_is_bond_master(dev))
1841 				mlx4_set_default_gid(ibdev, dev, port);
1842 			mlx4_ib_get_dev_addr(dev, ibdev, port);
1843 		}
1844 	}
1845 
1846         IFNET_RUNLOCK_NOSLEEP();
1847 
1848 	return 0;
1849 }
1850 
mlx4_ib_scan_netdevs(struct mlx4_ib_dev * ibdev,struct net_device * dev,unsigned long event)1851 static void mlx4_ib_scan_netdevs(struct mlx4_ib_dev *ibdev,
1852 				 struct net_device *dev, unsigned long event)
1853 {
1854 	struct mlx4_ib_iboe *iboe;
1855 	int port;
1856 	int init = 0;
1857 	unsigned long flags;
1858 
1859 	iboe = &ibdev->iboe;
1860 
1861 	spin_lock_irqsave(&iboe->lock, flags);
1862 	mlx4_foreach_ib_transport_port(port, ibdev->dev) {
1863 		struct net_device *old_netdev = iboe->netdevs[port - 1];
1864 /* XXX BOND related */
1865 #if 0
1866 		struct net_device *old_master = iboe->masters[port - 1];
1867 #endif
1868 		iboe->masters[port - 1] = NULL;
1869 		iboe->netdevs[port - 1] =
1870 			mlx4_get_protocol_dev(ibdev->dev, MLX4_PROT_ETH, port);
1871 
1872 
1873 		if (old_netdev != iboe->netdevs[port - 1])
1874 			init = 1;
1875 		if (dev == iboe->netdevs[port - 1] &&
1876 		    event == NETDEV_CHANGEADDR)
1877 			init = 1;
1878 /* XXX BOND related */
1879 #if 0
1880                 if (iboe->netdevs[port - 1] && netif_is_bond_slave(iboe->netdevs[port - 1]))
1881 			iboe->masters[port - 1] = iboe->netdevs[port - 1]->master;
1882 
1883 		/* if bonding is used it is possible that we add it to masters only after
1884 		   IP address is assigned to the net bonding interface */
1885 		if (old_master != iboe->masters[port - 1])
1886 			init = 1;
1887 #endif
1888 	}
1889 
1890 	spin_unlock_irqrestore(&iboe->lock, flags);
1891 
1892 	if (init)
1893 		if (mlx4_ib_init_gid_table(ibdev))
1894 			pr_warn("Fail to reset gid table\n");
1895 }
1896 
mlx4_ib_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)1897 static int mlx4_ib_netdev_event(struct notifier_block *this, unsigned long event,
1898 				void *ptr)
1899 {
1900 	struct net_device *dev = ptr;
1901 	struct mlx4_ib_dev *ibdev;
1902 
1903 	ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb);
1904 
1905 	mlx4_ib_scan_netdevs(ibdev, dev, event);
1906 
1907 	return NOTIFY_DONE;
1908 }
1909 
1910 /* This function initializes the gid table only if the event_netdev real device is an iboe
1911  * device, will be invoked by the inet/inet6 events */
mlx4_ib_inet_event(struct notifier_block * this,unsigned long event,void * ptr)1912 static int mlx4_ib_inet_event(struct notifier_block *this, unsigned long event,
1913                                 void *ptr)
1914 {
1915         struct net_device *event_netdev = ptr;
1916         struct mlx4_ib_dev *ibdev;
1917         struct mlx4_ib_iboe *ibdev_iboe;
1918         int port = 0;
1919 
1920         ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb_inet);
1921 
1922         struct net_device *real_dev = rdma_vlan_dev_real_dev(event_netdev) ?
1923                         rdma_vlan_dev_real_dev(event_netdev) :
1924                         event_netdev;
1925 
1926         ibdev_iboe = &ibdev->iboe;
1927 
1928         port = mlx4_ib_get_dev_port(real_dev, ibdev);
1929 
1930         /* Perform init_gid_table if the event real_dev is the net_device which represents this port,
1931          * otherwise this event is not related and would be ignored.*/
1932         if(port && (real_dev == ibdev_iboe->netdevs[port - 1]))
1933                 if (mlx4_ib_init_gid_table(ibdev))
1934                         pr_warn("Fail to reset gid table\n");
1935 
1936         return NOTIFY_DONE;
1937 }
1938 
1939 
init_pkeys(struct mlx4_ib_dev * ibdev)1940 static void init_pkeys(struct mlx4_ib_dev *ibdev)
1941 {
1942 	int port;
1943 	int slave;
1944 	int i;
1945 
1946 	if (mlx4_is_master(ibdev->dev)) {
1947 		for (slave = 0; slave <= ibdev->dev->num_vfs; ++slave) {
1948 			for (port = 1; port <= ibdev->dev->caps.num_ports; ++port) {
1949 				for (i = 0;
1950 				     i < ibdev->dev->phys_caps.pkey_phys_table_len[port];
1951 				     ++i) {
1952 					ibdev->pkeys.virt2phys_pkey[slave][port - 1][i] =
1953 					/* master has the identity virt2phys pkey mapping */
1954 						(slave == mlx4_master_func_num(ibdev->dev) || !i) ? i :
1955 							ibdev->dev->phys_caps.pkey_phys_table_len[port] - 1;
1956 					mlx4_sync_pkey_table(ibdev->dev, slave, port, i,
1957 							     ibdev->pkeys.virt2phys_pkey[slave][port - 1][i]);
1958 				}
1959 			}
1960 		}
1961 		/* initialize pkey cache */
1962 		for (port = 1; port <= ibdev->dev->caps.num_ports; ++port) {
1963 			for (i = 0;
1964 			     i < ibdev->dev->phys_caps.pkey_phys_table_len[port];
1965 			     ++i)
1966 				ibdev->pkeys.phys_pkey_cache[port-1][i] =
1967 					(i) ? 0 : 0xFFFF;
1968 		}
1969 	}
1970 }
1971 
mlx4_ib_alloc_eqs(struct mlx4_dev * dev,struct mlx4_ib_dev * ibdev)1972 static void mlx4_ib_alloc_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev)
1973 {
1974 	char name[32];
1975 	int eq_per_port = 0;
1976 	int added_eqs = 0;
1977 	int total_eqs = 0;
1978 	int i, j, eq;
1979 
1980 	/* Legacy mode or comp_pool is not large enough */
1981 	if (dev->caps.comp_pool == 0 ||
1982 	    dev->caps.num_ports > dev->caps.comp_pool)
1983 		return;
1984 
1985 	eq_per_port = rounddown_pow_of_two(dev->caps.comp_pool/
1986 					dev->caps.num_ports);
1987 
1988 	/* Init eq table */
1989 	added_eqs = 0;
1990 	mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
1991 		added_eqs += eq_per_port;
1992 
1993 	total_eqs = dev->caps.num_comp_vectors + added_eqs;
1994 
1995 	ibdev->eq_table = kzalloc(total_eqs * sizeof(int), GFP_KERNEL);
1996 	if (!ibdev->eq_table)
1997 		return;
1998 
1999 	ibdev->eq_added = added_eqs;
2000 
2001 	eq = 0;
2002 	mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB) {
2003 		for (j = 0; j < eq_per_port; j++) {
2004 			sprintf(name, "mlx4-ib-%d-%d@%d:%d:%d:%d", i, j,
2005 			    pci_get_domain(dev->pdev->dev.bsddev),
2006 			    pci_get_bus(dev->pdev->dev.bsddev),
2007 			    PCI_SLOT(dev->pdev->devfn),
2008 			    PCI_FUNC(dev->pdev->devfn));
2009 
2010 			/* Set IRQ for specific name (per ring) */
2011 			if (mlx4_assign_eq(dev, name,
2012 					   &ibdev->eq_table[eq])) {
2013 				/* Use legacy (same as mlx4_en driver) */
2014 				pr_warn("Can't allocate EQ %d; reverting to legacy\n", eq);
2015 				ibdev->eq_table[eq] =
2016 					(eq % dev->caps.num_comp_vectors);
2017 			}
2018 			eq++;
2019 		}
2020 	}
2021 
2022 	/* Fill the reset of the vector with legacy EQ */
2023 	for (i = 0, eq = added_eqs; i < dev->caps.num_comp_vectors; i++)
2024 		ibdev->eq_table[eq++] = i;
2025 
2026 	/* Advertise the new number of EQs to clients */
2027 	ibdev->ib_dev.num_comp_vectors = total_eqs;
2028 }
2029 
mlx4_ib_free_eqs(struct mlx4_dev * dev,struct mlx4_ib_dev * ibdev)2030 static void mlx4_ib_free_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev)
2031 {
2032 	int i;
2033 
2034 	/* no additional eqs were added */
2035 	if (!ibdev->eq_table)
2036 		return;
2037 
2038 	/* Reset the advertised EQ number */
2039 	ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;
2040 
2041 	/* Free only the added eqs */
2042 	for (i = 0; i < ibdev->eq_added; i++) {
2043 		/* Don't free legacy eqs if used */
2044 		if (ibdev->eq_table[i] <= dev->caps.num_comp_vectors)
2045 			continue;
2046 		mlx4_release_eq(dev, ibdev->eq_table[i]);
2047 	}
2048 
2049 	kfree(ibdev->eq_table);
2050 }
2051 
2052 /*
2053  * create show function and a device_attribute struct pointing to
2054  * the function for _name
2055  */
2056 #define DEVICE_DIAG_RPRT_ATTR(_name, _offset, _op_mod)		\
2057 static ssize_t show_rprt_##_name(struct device *dev,		\
2058 				 struct device_attribute *attr,	\
2059 				 char *buf){			\
2060 	return show_diag_rprt(dev, buf, _offset, _op_mod);	\
2061 }								\
2062 static DEVICE_ATTR(_name, S_IRUGO, show_rprt_##_name, NULL);
2063 
2064 #define MLX4_DIAG_RPRT_CLEAR_DIAGS 3
2065 
show_diag_rprt(struct device * device,char * buf,u32 offset,u8 op_modifier)2066 static size_t show_diag_rprt(struct device *device, char *buf,
2067 			     u32 offset, u8 op_modifier)
2068 {
2069 	size_t ret;
2070 	u32 counter_offset = offset;
2071 	u32 diag_counter = 0;
2072 	struct mlx4_ib_dev *dev = container_of(device, struct mlx4_ib_dev,
2073 					       ib_dev.dev);
2074 
2075 	ret = mlx4_query_diag_counters(dev->dev, 1, op_modifier,
2076 				       &counter_offset, &diag_counter);
2077 	if (ret)
2078 		return ret;
2079 
2080 	return sprintf(buf, "%d\n", diag_counter);
2081 }
2082 
clear_diag_counters(struct device * device,struct device_attribute * attr,const char * buf,size_t length)2083 static ssize_t clear_diag_counters(struct device *device,
2084 				   struct device_attribute *attr,
2085 				   const char *buf, size_t length)
2086 {
2087 	size_t ret;
2088 	struct mlx4_ib_dev *dev = container_of(device, struct mlx4_ib_dev,
2089 					       ib_dev.dev);
2090 
2091 	ret = mlx4_query_diag_counters(dev->dev, 0, MLX4_DIAG_RPRT_CLEAR_DIAGS,
2092 				       NULL, NULL);
2093 	if (ret)
2094 		return ret;
2095 
2096 	return length;
2097 }
2098 
2099 DEVICE_DIAG_RPRT_ATTR(rq_num_lle	, 0x00, 2);
2100 DEVICE_DIAG_RPRT_ATTR(sq_num_lle	, 0x04, 2);
2101 DEVICE_DIAG_RPRT_ATTR(rq_num_lqpoe	, 0x08, 2);
2102 DEVICE_DIAG_RPRT_ATTR(sq_num_lqpoe 	, 0x0C, 2);
2103 DEVICE_DIAG_RPRT_ATTR(rq_num_lpe	, 0x18, 2);
2104 DEVICE_DIAG_RPRT_ATTR(sq_num_lpe	, 0x1C, 2);
2105 DEVICE_DIAG_RPRT_ATTR(rq_num_wrfe	, 0x20, 2);
2106 DEVICE_DIAG_RPRT_ATTR(sq_num_wrfe	, 0x24, 2);
2107 DEVICE_DIAG_RPRT_ATTR(sq_num_mwbe	, 0x2C, 2);
2108 DEVICE_DIAG_RPRT_ATTR(sq_num_bre	, 0x34, 2);
2109 DEVICE_DIAG_RPRT_ATTR(rq_num_lae	, 0x38, 2);
2110 DEVICE_DIAG_RPRT_ATTR(sq_num_rire	, 0x44, 2);
2111 DEVICE_DIAG_RPRT_ATTR(rq_num_rire	, 0x48, 2);
2112 DEVICE_DIAG_RPRT_ATTR(sq_num_rae	, 0x4C, 2);
2113 DEVICE_DIAG_RPRT_ATTR(rq_num_rae	, 0x50, 2);
2114 DEVICE_DIAG_RPRT_ATTR(sq_num_roe	, 0x54, 2);
2115 DEVICE_DIAG_RPRT_ATTR(sq_num_tree	, 0x5C, 2);
2116 DEVICE_DIAG_RPRT_ATTR(sq_num_rree	, 0x64, 2);
2117 DEVICE_DIAG_RPRT_ATTR(rq_num_rnr	, 0x68, 2);
2118 DEVICE_DIAG_RPRT_ATTR(sq_num_rnr	, 0x6C, 2);
2119 DEVICE_DIAG_RPRT_ATTR(rq_num_oos	, 0x100, 2);
2120 DEVICE_DIAG_RPRT_ATTR(sq_num_oos	, 0x104, 2);
2121 DEVICE_DIAG_RPRT_ATTR(rq_num_mce	, 0x108, 2);
2122 DEVICE_DIAG_RPRT_ATTR(rq_num_udsdprd	, 0x118, 2);
2123 DEVICE_DIAG_RPRT_ATTR(rq_num_ucsdprd	, 0x120, 2);
2124 DEVICE_DIAG_RPRT_ATTR(num_cqovf		, 0x1A0, 2);
2125 DEVICE_DIAG_RPRT_ATTR(num_eqovf		, 0x1A4, 2);
2126 DEVICE_DIAG_RPRT_ATTR(num_baddb		, 0x1A8, 2);
2127 
2128 static DEVICE_ATTR(clear_diag, S_IWUSR, NULL, clear_diag_counters);
2129 
2130 static struct attribute *diag_rprt_attrs[] = {
2131 	&dev_attr_rq_num_lle.attr,
2132 	&dev_attr_sq_num_lle.attr,
2133 	&dev_attr_rq_num_lqpoe.attr,
2134 	&dev_attr_sq_num_lqpoe.attr,
2135 	&dev_attr_rq_num_lpe.attr,
2136 	&dev_attr_sq_num_lpe.attr,
2137 	&dev_attr_rq_num_wrfe.attr,
2138 	&dev_attr_sq_num_wrfe.attr,
2139 	&dev_attr_sq_num_mwbe.attr,
2140 	&dev_attr_sq_num_bre.attr,
2141 	&dev_attr_rq_num_lae.attr,
2142 	&dev_attr_sq_num_rire.attr,
2143 	&dev_attr_rq_num_rire.attr,
2144 	&dev_attr_sq_num_rae.attr,
2145 	&dev_attr_rq_num_rae.attr,
2146 	&dev_attr_sq_num_roe.attr,
2147 	&dev_attr_sq_num_tree.attr,
2148 	&dev_attr_sq_num_rree.attr,
2149 	&dev_attr_rq_num_rnr.attr,
2150 	&dev_attr_sq_num_rnr.attr,
2151 	&dev_attr_rq_num_oos.attr,
2152 	&dev_attr_sq_num_oos.attr,
2153 	&dev_attr_rq_num_mce.attr,
2154 	&dev_attr_rq_num_udsdprd.attr,
2155 	&dev_attr_rq_num_ucsdprd.attr,
2156 	&dev_attr_num_cqovf.attr,
2157 	&dev_attr_num_eqovf.attr,
2158 	&dev_attr_num_baddb.attr,
2159 	&dev_attr_clear_diag.attr,
2160 	NULL
2161 };
2162 
2163 static struct attribute_group diag_counters_group = {
2164 	.name  = "diag_counters",
2165 	.attrs  = diag_rprt_attrs
2166 };
2167 
init_dev_assign(void)2168 static void init_dev_assign(void)
2169 {
2170 	int i = 1;
2171 
2172 	spin_lock_init(&dev_num_str_lock);
2173 	if (mlx4_fill_dbdf2val_tbl(&dev_assign_str))
2174 		return;
2175 	dev_num_str_bitmap =
2176 		kmalloc(BITS_TO_LONGS(MAX_NUM_STR_BITMAP) * sizeof(long),
2177 			GFP_KERNEL);
2178 	if (!dev_num_str_bitmap) {
2179 		pr_warn("bitmap alloc failed -- cannot apply dev_assign_str parameter\n");
2180 		return;
2181 	}
2182 	bitmap_zero(dev_num_str_bitmap, MAX_NUM_STR_BITMAP);
2183 	while ((i < MLX4_DEVS_TBL_SIZE) && (dev_assign_str.tbl[i].dbdf !=
2184 	       MLX4_ENDOF_TBL)) {
2185 		if (bitmap_allocate_region(dev_num_str_bitmap,
2186 					   dev_assign_str.tbl[i].val[0], 0))
2187 			goto err;
2188 		i++;
2189 	}
2190 	dr_active = 1;
2191 	return;
2192 
2193 err:
2194 	kfree(dev_num_str_bitmap);
2195 	dev_num_str_bitmap = NULL;
2196 	pr_warn("mlx4_ib: The value of 'dev_assign_str' parameter "
2197 			    "is incorrect. The parameter value is discarded!");
2198 }
2199 
mlx4_ib_dev_idx(struct mlx4_dev * dev)2200 static int mlx4_ib_dev_idx(struct mlx4_dev *dev)
2201 {
2202 	int i, val;
2203 
2204 	if (!dr_active)
2205 		return -1;
2206 	if (!dev)
2207 		return -1;
2208 	if (mlx4_get_val(dev_assign_str.tbl, dev->pdev, 0, &val))
2209 		return -1;
2210 
2211 	if (val != DEFAULT_TBL_VAL) {
2212 		dev->flags |= MLX4_FLAG_DEV_NUM_STR;
2213 		return val;
2214 	}
2215 
2216 	spin_lock(&dev_num_str_lock);
2217 	i = bitmap_find_free_region(dev_num_str_bitmap, MAX_NUM_STR_BITMAP, 0);
2218 	spin_unlock(&dev_num_str_lock);
2219 	if (i >= 0)
2220 		return i;
2221 
2222 	return -1;
2223 }
2224 
mlx4_ib_add(struct mlx4_dev * dev)2225 static void *mlx4_ib_add(struct mlx4_dev *dev)
2226 {
2227 	struct mlx4_ib_dev *ibdev;
2228 	int num_ports = 0;
2229 	int i, j;
2230 	int err;
2231 	struct mlx4_ib_iboe *iboe;
2232 	int dev_idx;
2233 
2234         pr_info_once("%s", mlx4_ib_version);
2235 
2236 	mlx4_foreach_ib_transport_port(i, dev)
2237 		num_ports++;
2238 
2239 	/* No point in registering a device with no ports... */
2240 	if (num_ports == 0)
2241 		return NULL;
2242 
2243 	ibdev = (struct mlx4_ib_dev *) ib_alloc_device(sizeof *ibdev);
2244 	if (!ibdev) {
2245 		dev_err(&dev->pdev->dev, "Device struct alloc failed\n");
2246 		return NULL;
2247 	}
2248 
2249 	iboe = &ibdev->iboe;
2250 
2251 	if (mlx4_pd_alloc(dev, &ibdev->priv_pdn))
2252 		goto err_dealloc;
2253 
2254 	if (mlx4_uar_alloc(dev, &ibdev->priv_uar))
2255 		goto err_pd;
2256 
2257 	ibdev->priv_uar.map = ioremap(ibdev->priv_uar.pfn << PAGE_SHIFT,
2258 		PAGE_SIZE);
2259 
2260 	if (!ibdev->priv_uar.map)
2261 		goto err_uar;
2262 
2263 	MLX4_INIT_DOORBELL_LOCK(&ibdev->uar_lock);
2264 
2265 	ibdev->dev = dev;
2266 
2267 	dev_idx = mlx4_ib_dev_idx(dev);
2268 	if (dev_idx >= 0)
2269 		sprintf(ibdev->ib_dev.name, "mlx4_%d", dev_idx);
2270 	else
2271 	strlcpy(ibdev->ib_dev.name, "mlx4_%d", IB_DEVICE_NAME_MAX);
2272 
2273 	ibdev->ib_dev.owner		= THIS_MODULE;
2274 	ibdev->ib_dev.node_type		= RDMA_NODE_IB_CA;
2275 	ibdev->ib_dev.local_dma_lkey	= dev->caps.reserved_lkey;
2276 	ibdev->num_ports		= num_ports;
2277 	ibdev->ib_dev.phys_port_cnt     = ibdev->num_ports;
2278 	ibdev->ib_dev.num_comp_vectors	= dev->caps.num_comp_vectors;
2279 	ibdev->ib_dev.dma_device	= &dev->pdev->dev;
2280 
2281 	if (dev->caps.userspace_caps)
2282 		ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_ABI_VERSION;
2283 	else
2284 		ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION;
2285 
2286 	ibdev->ib_dev.uverbs_cmd_mask	=
2287 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
2288 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
2289 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
2290 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
2291 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
2292 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
2293 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
2294 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
2295 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
2296 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
2297 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
2298 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
2299 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
2300 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
2301 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
2302 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
2303 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
2304 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
2305 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
2306 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
2307 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
2308 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
2309 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
2310 
2311 	ibdev->ib_dev.query_device	= mlx4_ib_query_device;
2312 	ibdev->ib_dev.query_port	= mlx4_ib_query_port;
2313 	ibdev->ib_dev.get_link_layer	= mlx4_ib_port_link_layer;
2314 	ibdev->ib_dev.query_gid		= mlx4_ib_query_gid;
2315 	ibdev->ib_dev.query_pkey	= mlx4_ib_query_pkey;
2316 	ibdev->ib_dev.modify_device	= mlx4_ib_modify_device;
2317 	ibdev->ib_dev.modify_port	= mlx4_ib_modify_port;
2318 	ibdev->ib_dev.alloc_ucontext	= mlx4_ib_alloc_ucontext;
2319 	ibdev->ib_dev.dealloc_ucontext	= mlx4_ib_dealloc_ucontext;
2320 	ibdev->ib_dev.mmap		= mlx4_ib_mmap;
2321 /* XXX FBSD has no support for get_unmapped_area function */
2322 #if 0
2323 	ibdev->ib_dev.get_unmapped_area = mlx4_ib_get_unmapped_area;
2324 #endif
2325 	ibdev->ib_dev.alloc_pd		= mlx4_ib_alloc_pd;
2326 	ibdev->ib_dev.dealloc_pd	= mlx4_ib_dealloc_pd;
2327 	ibdev->ib_dev.create_ah		= mlx4_ib_create_ah;
2328 	ibdev->ib_dev.query_ah		= mlx4_ib_query_ah;
2329 	ibdev->ib_dev.destroy_ah	= mlx4_ib_destroy_ah;
2330 	ibdev->ib_dev.create_srq	= mlx4_ib_create_srq;
2331 	ibdev->ib_dev.modify_srq	= mlx4_ib_modify_srq;
2332 	ibdev->ib_dev.query_srq		= mlx4_ib_query_srq;
2333 	ibdev->ib_dev.destroy_srq	= mlx4_ib_destroy_srq;
2334 	ibdev->ib_dev.post_srq_recv	= mlx4_ib_post_srq_recv;
2335 	ibdev->ib_dev.create_qp		= mlx4_ib_create_qp;
2336 	ibdev->ib_dev.modify_qp		= mlx4_ib_modify_qp;
2337 	ibdev->ib_dev.query_qp		= mlx4_ib_query_qp;
2338 	ibdev->ib_dev.destroy_qp	= mlx4_ib_destroy_qp;
2339 	ibdev->ib_dev.post_send		= mlx4_ib_post_send;
2340 	ibdev->ib_dev.post_recv		= mlx4_ib_post_recv;
2341 	ibdev->ib_dev.create_cq		= mlx4_ib_create_cq;
2342 	ibdev->ib_dev.modify_cq		= mlx4_ib_modify_cq;
2343 	ibdev->ib_dev.resize_cq		= mlx4_ib_resize_cq;
2344 	ibdev->ib_dev.destroy_cq	= mlx4_ib_destroy_cq;
2345 	ibdev->ib_dev.poll_cq		= mlx4_ib_poll_cq;
2346 	ibdev->ib_dev.req_notify_cq	= mlx4_ib_arm_cq;
2347 	ibdev->ib_dev.get_dma_mr	= mlx4_ib_get_dma_mr;
2348 	ibdev->ib_dev.reg_user_mr	= mlx4_ib_reg_user_mr;
2349 	ibdev->ib_dev.dereg_mr		= mlx4_ib_dereg_mr;
2350 	ibdev->ib_dev.alloc_fast_reg_mr = mlx4_ib_alloc_fast_reg_mr;
2351 	ibdev->ib_dev.alloc_fast_reg_page_list = mlx4_ib_alloc_fast_reg_page_list;
2352 	ibdev->ib_dev.free_fast_reg_page_list  = mlx4_ib_free_fast_reg_page_list;
2353 	ibdev->ib_dev.attach_mcast	= mlx4_ib_mcg_attach;
2354 	ibdev->ib_dev.detach_mcast	= mlx4_ib_mcg_detach;
2355 	ibdev->ib_dev.process_mad	= mlx4_ib_process_mad;
2356 	ibdev->ib_dev.ioctl		= mlx4_ib_ioctl;
2357 	ibdev->ib_dev.query_values	= mlx4_ib_query_values;
2358 
2359 	if (!mlx4_is_slave(ibdev->dev)) {
2360 		ibdev->ib_dev.alloc_fmr		= mlx4_ib_fmr_alloc;
2361 		ibdev->ib_dev.map_phys_fmr	= mlx4_ib_map_phys_fmr;
2362 		ibdev->ib_dev.unmap_fmr		= mlx4_ib_unmap_fmr;
2363 		ibdev->ib_dev.dealloc_fmr	= mlx4_ib_fmr_dealloc;
2364 	}
2365 
2366 	if (dev->caps.flags & MLX4_DEV_CAP_FLAG_MEM_WINDOW) {
2367 		ibdev->ib_dev.alloc_mw = mlx4_ib_alloc_mw;
2368 		ibdev->ib_dev.bind_mw = mlx4_ib_bind_mw;
2369 		ibdev->ib_dev.dealloc_mw = mlx4_ib_dealloc_mw;
2370 
2371 		ibdev->ib_dev.uverbs_cmd_mask |=
2372 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
2373 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
2374 	}
2375 
2376 	if (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) {
2377 		ibdev->ib_dev.alloc_xrcd = mlx4_ib_alloc_xrcd;
2378 		ibdev->ib_dev.dealloc_xrcd = mlx4_ib_dealloc_xrcd;
2379 		ibdev->ib_dev.uverbs_cmd_mask |=
2380 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
2381 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
2382 	}
2383 
2384 	/*
2385 	 * Set experimental data
2386 	 */
2387 	ibdev->ib_dev.uverbs_exp_cmd_mask	=
2388 		(1ull << IB_USER_VERBS_EXP_CMD_CREATE_QP)	|
2389 		(1ull << IB_USER_VERBS_EXP_CMD_MODIFY_CQ)	|
2390 		(1ull << IB_USER_VERBS_EXP_CMD_QUERY_DEVICE)	|
2391 		(1ull << IB_USER_VERBS_EXP_CMD_CREATE_CQ);
2392 	ibdev->ib_dev.exp_create_qp	= mlx4_ib_exp_create_qp;
2393 	ibdev->ib_dev.exp_query_device	= mlx4_ib_exp_query_device;
2394 	if (check_flow_steering_support(dev)) {
2395 		ibdev->ib_dev.uverbs_ex_cmd_mask	|=
2396 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
2397 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
2398 		ibdev->ib_dev.create_flow	= mlx4_ib_create_flow;
2399 		ibdev->ib_dev.destroy_flow	= mlx4_ib_destroy_flow;
2400 	} else {
2401 		pr_debug("Device managed flow steering is unavailable for this configuration.\n");
2402 	}
2403 	/*
2404 	 * End of experimental data
2405 	 */
2406 
2407 	mlx4_ib_alloc_eqs(dev, ibdev);
2408 
2409 	spin_lock_init(&iboe->lock);
2410 
2411 	if (init_node_data(ibdev))
2412 		goto err_map;
2413 
2414 	for (i = 0; i < ibdev->num_ports; ++i) {
2415 		if (mlx4_ib_port_link_layer(&ibdev->ib_dev, i + 1) ==
2416 						IB_LINK_LAYER_ETHERNET) {
2417 			if (mlx4_is_slave(dev)) {
2418 				ibdev->counters[i].status = mlx4_counter_alloc(ibdev->dev,
2419 									       i + 1,
2420 									       &ibdev->counters[i].counter_index);
2421 			} else {/* allocating the PF IB default counter indices reserved in mlx4_init_counters_table */
2422 				ibdev->counters[i].counter_index = ((i + 1) << 1) - 1;
2423 				ibdev->counters[i].status = 0;
2424 			}
2425 
2426 			dev_info(&dev->pdev->dev,
2427 				 "%s: allocated counter index %d for port %d\n",
2428 				 __func__, ibdev->counters[i].counter_index, i+1);
2429 		} else {
2430 			ibdev->counters[i].counter_index = MLX4_SINK_COUNTER_INDEX;
2431 			ibdev->counters[i].status = -ENOSPC;
2432 		}
2433 	}
2434 
2435 	spin_lock_init(&ibdev->sm_lock);
2436 	mutex_init(&ibdev->cap_mask_mutex);
2437 
2438 	if (dev->caps.steering_mode == MLX4_STEERING_MODE_DEVICE_MANAGED &&
2439 	    !mlx4_is_mfunc(dev)) {
2440 		ibdev->steer_qpn_count = MLX4_IB_UC_MAX_NUM_QPS;
2441 		err = mlx4_qp_reserve_range(dev, ibdev->steer_qpn_count,
2442 					    MLX4_IB_UC_STEER_QPN_ALIGN, &ibdev->steer_qpn_base, 0);
2443 		if (err)
2444 			goto err_counter;
2445 
2446 		ibdev->ib_uc_qpns_bitmap =
2447 			kmalloc(BITS_TO_LONGS(ibdev->steer_qpn_count) *
2448 				sizeof(long),
2449 				GFP_KERNEL);
2450 		if (!ibdev->ib_uc_qpns_bitmap) {
2451 			dev_err(&dev->pdev->dev, "bit map alloc failed\n");
2452 			goto err_steer_qp_release;
2453 		}
2454 
2455 		bitmap_zero(ibdev->ib_uc_qpns_bitmap, ibdev->steer_qpn_count);
2456 
2457 		err = mlx4_FLOW_STEERING_IB_UC_QP_RANGE(dev, ibdev->steer_qpn_base,
2458 				ibdev->steer_qpn_base + ibdev->steer_qpn_count - 1);
2459 		if (err)
2460 			goto err_steer_free_bitmap;
2461 	}
2462 
2463 	if (ib_register_device(&ibdev->ib_dev, NULL))
2464 		goto err_steer_free_bitmap;
2465 
2466 	if (mlx4_ib_mad_init(ibdev))
2467 		goto err_reg;
2468 
2469 	if (mlx4_ib_init_sriov(ibdev))
2470 		goto err_mad;
2471 
2472 	if (dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE) {
2473 		if (!iboe->nb.notifier_call) {
2474 		iboe->nb.notifier_call = mlx4_ib_netdev_event;
2475 		err = register_netdevice_notifier(&iboe->nb);
2476 			if (err) {
2477 				iboe->nb.notifier_call = NULL;
2478 				goto err_notify;
2479 			}
2480 		}
2481 		if (!iboe->nb_inet.notifier_call) {
2482 			iboe->nb_inet.notifier_call = mlx4_ib_inet_event;
2483 			err = register_inetaddr_notifier(&iboe->nb_inet);
2484 			if (err) {
2485 				iboe->nb_inet.notifier_call = NULL;
2486 				goto err_notify;
2487 			}
2488 		}
2489 		mlx4_ib_scan_netdevs(ibdev, NULL, 0);
2490 	}
2491 	for (j = 0; j < ARRAY_SIZE(mlx4_class_attributes); ++j) {
2492 		if (device_create_file(&ibdev->ib_dev.dev,
2493 				       mlx4_class_attributes[j]))
2494 			goto err_notify;
2495 	}
2496 	if (sysfs_create_group(&ibdev->ib_dev.dev.kobj, &diag_counters_group))
2497 		goto err_notify;
2498 
2499 	ibdev->ib_active = true;
2500 
2501 	if (mlx4_is_mfunc(ibdev->dev))
2502 		init_pkeys(ibdev);
2503 
2504 	/* create paravirt contexts for any VFs which are active */
2505 	if (mlx4_is_master(ibdev->dev)) {
2506 		for (j = 0; j < MLX4_MFUNC_MAX; j++) {
2507 			if (j == mlx4_master_func_num(ibdev->dev))
2508 				continue;
2509 			if (mlx4_is_slave_active(ibdev->dev, j))
2510 				do_slave_init(ibdev, j, 1);
2511 		}
2512 	}
2513 	return ibdev;
2514 
2515 err_notify:
2516 	for (j = 0; j < ARRAY_SIZE(mlx4_class_attributes); ++j) {
2517                 device_remove_file(&ibdev->ib_dev.dev,
2518                         mlx4_class_attributes[j]);
2519         }
2520 
2521 	if (ibdev->iboe.nb.notifier_call) {
2522 	if (unregister_netdevice_notifier(&ibdev->iboe.nb))
2523 		pr_warn("failure unregistering notifier\n");
2524 		ibdev->iboe.nb.notifier_call = NULL;
2525 	}
2526 	if (ibdev->iboe.nb_inet.notifier_call) {
2527 		if (unregister_inetaddr_notifier(&ibdev->iboe.nb_inet))
2528 			pr_warn("failure unregistering notifier\n");
2529 		ibdev->iboe.nb_inet.notifier_call = NULL;
2530 	}
2531 	flush_workqueue(wq);
2532 
2533 	mlx4_ib_close_sriov(ibdev);
2534 
2535 err_mad:
2536 	mlx4_ib_mad_cleanup(ibdev);
2537 
2538 err_reg:
2539 	ib_unregister_device(&ibdev->ib_dev);
2540 
2541 err_steer_free_bitmap:
2542 	kfree(ibdev->ib_uc_qpns_bitmap);
2543 
2544 err_steer_qp_release:
2545 	if (dev->caps.steering_mode == MLX4_STEERING_MODE_DEVICE_MANAGED)
2546 		mlx4_qp_release_range(dev, ibdev->steer_qpn_base,
2547 				ibdev->steer_qpn_count);
2548 err_counter:
2549 	for (; i; --i) {
2550 		if (mlx4_ib_port_link_layer(&ibdev->ib_dev, i) ==
2551 						IB_LINK_LAYER_ETHERNET) {
2552 			mlx4_counter_free(ibdev->dev,
2553 					  i,
2554 					  ibdev->counters[i - 1].counter_index);
2555 		}
2556 	}
2557 
2558 err_map:
2559 	iounmap(ibdev->priv_uar.map);
2560 	mlx4_ib_free_eqs(dev, ibdev);
2561 
2562 err_uar:
2563 	mlx4_uar_free(dev, &ibdev->priv_uar);
2564 
2565 err_pd:
2566 	mlx4_pd_free(dev, ibdev->priv_pdn);
2567 
2568 err_dealloc:
2569 	ib_dealloc_device(&ibdev->ib_dev);
2570 
2571 	return NULL;
2572 }
2573 
mlx4_ib_steer_qp_alloc(struct mlx4_ib_dev * dev,int count,int * qpn)2574 int mlx4_ib_steer_qp_alloc(struct mlx4_ib_dev *dev, int count, int *qpn)
2575 {
2576 	int offset;
2577 
2578 	WARN_ON(!dev->ib_uc_qpns_bitmap);
2579 
2580 	offset = bitmap_find_free_region(dev->ib_uc_qpns_bitmap,
2581 					 dev->steer_qpn_count,
2582 					 get_count_order(count));
2583 	if (offset < 0)
2584 		return offset;
2585 
2586 	*qpn = dev->steer_qpn_base + offset;
2587 	return 0;
2588 }
2589 
mlx4_ib_steer_qp_free(struct mlx4_ib_dev * dev,u32 qpn,int count)2590 void mlx4_ib_steer_qp_free(struct mlx4_ib_dev *dev, u32 qpn, int count)
2591 {
2592 	if (!qpn ||
2593 	    dev->dev->caps.steering_mode != MLX4_STEERING_MODE_DEVICE_MANAGED)
2594 		return;
2595 
2596 	BUG_ON(qpn < dev->steer_qpn_base);
2597 
2598 	bitmap_release_region(dev->ib_uc_qpns_bitmap,
2599 			qpn - dev->steer_qpn_base, get_count_order(count));
2600 }
2601 
mlx4_ib_steer_qp_reg(struct mlx4_ib_dev * mdev,struct mlx4_ib_qp * mqp,int is_attach)2602 int mlx4_ib_steer_qp_reg(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
2603 			 int is_attach)
2604 {
2605 	int err;
2606 	size_t flow_size;
2607 	struct ib_flow_attr *flow = NULL;
2608 	struct ib_flow_spec_ib *ib_spec;
2609 
2610 	if (is_attach) {
2611 		flow_size = sizeof(struct ib_flow_attr) +
2612 			    sizeof(struct ib_flow_spec_ib);
2613 		flow = kzalloc(flow_size, GFP_KERNEL);
2614 		if (!flow)
2615 			return -ENOMEM;
2616 		flow->port = mqp->port;
2617 		flow->num_of_specs = 1;
2618 		flow->size = flow_size;
2619 		ib_spec = (struct ib_flow_spec_ib *)(flow + 1);
2620 		ib_spec->type = IB_FLOW_SPEC_IB;
2621 		ib_spec->size = sizeof(struct ib_flow_spec_ib);
2622 		ib_spec->val.l3_type_qpn = mqp->ibqp.qp_num;
2623 		ib_spec->mask.l3_type_qpn = MLX4_IB_FLOW_QPN_MASK;
2624 
2625 		err = __mlx4_ib_create_flow(&mqp->ibqp, flow,
2626 					    IB_FLOW_DOMAIN_NIC,
2627 					    MLX4_FS_REGULAR,
2628 					    &mqp->reg_id);
2629 	} else {
2630 		err = __mlx4_ib_destroy_flow(mdev->dev, mqp->reg_id);
2631 	}
2632 	kfree(flow);
2633 	return err;
2634 }
2635 
mlx4_ib_remove(struct mlx4_dev * dev,void * ibdev_ptr)2636 static void mlx4_ib_remove(struct mlx4_dev *dev, void *ibdev_ptr)
2637 {
2638 	struct mlx4_ib_dev *ibdev = ibdev_ptr;
2639 	int p, j;
2640 	int dev_idx, ret;
2641 
2642 	if (ibdev->iboe.nb_inet.notifier_call) {
2643 		if (unregister_inetaddr_notifier(&ibdev->iboe.nb_inet))
2644 			pr_warn("failure unregistering notifier\n");
2645 		ibdev->iboe.nb_inet.notifier_call = NULL;
2646 	}
2647 
2648 	mlx4_ib_close_sriov(ibdev);
2649 	sysfs_remove_group(&ibdev->ib_dev.dev.kobj, &diag_counters_group);
2650 	mlx4_ib_mad_cleanup(ibdev);
2651 
2652 	for (j = 0; j < ARRAY_SIZE(mlx4_class_attributes); ++j) {
2653 		device_remove_file(&ibdev->ib_dev.dev,
2654 			mlx4_class_attributes[j]);
2655 	}
2656 
2657 
2658 	dev_idx = -1;
2659 	if (dr_active && !(ibdev->dev->flags & MLX4_FLAG_DEV_NUM_STR)) {
2660 		ret = sscanf(ibdev->ib_dev.name, "mlx4_%d", &dev_idx);
2661 		if (ret != 1)
2662 			dev_idx = -1;
2663 	}
2664 	ib_unregister_device(&ibdev->ib_dev);
2665 	if (dev_idx >= 0) {
2666 		spin_lock(&dev_num_str_lock);
2667 		bitmap_release_region(dev_num_str_bitmap, dev_idx, 0);
2668 		spin_unlock(&dev_num_str_lock);
2669 	}
2670 
2671 	if (dev->caps.steering_mode == MLX4_STEERING_MODE_DEVICE_MANAGED) {
2672 		mlx4_qp_release_range(dev, ibdev->steer_qpn_base,
2673 				ibdev->steer_qpn_count);
2674 		kfree(ibdev->ib_uc_qpns_bitmap);
2675 	}
2676 
2677 	if (ibdev->iboe.nb.notifier_call) {
2678 		if (unregister_netdevice_notifier(&ibdev->iboe.nb))
2679 			pr_warn("failure unregistering notifier\n");
2680 		ibdev->iboe.nb.notifier_call = NULL;
2681 	}
2682 	iounmap(ibdev->priv_uar.map);
2683 
2684 	for (p = 0; p < ibdev->num_ports; ++p) {
2685 		if (mlx4_ib_port_link_layer(&ibdev->ib_dev, p + 1) ==
2686 						IB_LINK_LAYER_ETHERNET) {
2687 			mlx4_counter_free(ibdev->dev,
2688 					  p + 1,
2689 					  ibdev->counters[p].counter_index);
2690 		}
2691 	}
2692 
2693 	mlx4_foreach_port(p, dev, MLX4_PORT_TYPE_IB)
2694 		mlx4_CLOSE_PORT(dev, p);
2695 
2696 	mlx4_ib_free_eqs(dev, ibdev);
2697 
2698 	mlx4_uar_free(dev, &ibdev->priv_uar);
2699 	mlx4_pd_free(dev, ibdev->priv_pdn);
2700 	ib_dealloc_device(&ibdev->ib_dev);
2701 }
2702 
do_slave_init(struct mlx4_ib_dev * ibdev,int slave,int do_init)2703 static void do_slave_init(struct mlx4_ib_dev *ibdev, int slave, int do_init)
2704 {
2705 	struct mlx4_ib_demux_work **dm = NULL;
2706 	struct mlx4_dev *dev = ibdev->dev;
2707 	int i;
2708 	unsigned long flags;
2709 
2710 	if (!mlx4_is_master(dev))
2711 		return;
2712 
2713 	dm = kcalloc(dev->caps.num_ports, sizeof *dm, GFP_ATOMIC);
2714 	if (!dm) {
2715 		pr_err("failed to allocate memory for tunneling qp update\n");
2716 		goto out;
2717 	}
2718 
2719 	for (i = 0; i < dev->caps.num_ports; i++) {
2720 		dm[i] = kmalloc(sizeof (struct mlx4_ib_demux_work), GFP_ATOMIC);
2721 		if (!dm[i]) {
2722 			pr_err("failed to allocate memory for tunneling qp update work struct\n");
2723 			for (i = 0; i < dev->caps.num_ports; i++) {
2724 				if (dm[i])
2725 					kfree(dm[i]);
2726 			}
2727 			goto out;
2728 		}
2729 	}
2730 	/* initialize or tear down tunnel QPs for the slave */
2731 	for (i = 0; i < dev->caps.num_ports; i++) {
2732 		INIT_WORK(&dm[i]->work, mlx4_ib_tunnels_update_work);
2733 		dm[i]->port = i + 1;
2734 		dm[i]->slave = slave;
2735 		dm[i]->do_init = do_init;
2736 		dm[i]->dev = ibdev;
2737 		spin_lock_irqsave(&ibdev->sriov.going_down_lock, flags);
2738 		if (!ibdev->sriov.is_going_down)
2739 			queue_work(ibdev->sriov.demux[i].ud_wq, &dm[i]->work);
2740 		spin_unlock_irqrestore(&ibdev->sriov.going_down_lock, flags);
2741 	}
2742 out:
2743 	if (dm)
2744 		kfree(dm);
2745 	return;
2746 }
2747 
mlx4_ib_event(struct mlx4_dev * dev,void * ibdev_ptr,enum mlx4_dev_event event,unsigned long param)2748 static void mlx4_ib_event(struct mlx4_dev *dev, void *ibdev_ptr,
2749 			  enum mlx4_dev_event event, unsigned long param)
2750 {
2751 	struct ib_event ibev;
2752 	struct mlx4_ib_dev *ibdev = to_mdev((struct ib_device *) ibdev_ptr);
2753 	struct mlx4_eqe *eqe = NULL;
2754 	struct ib_event_work *ew;
2755 	int p = 0;
2756 
2757 	if (event == MLX4_DEV_EVENT_PORT_MGMT_CHANGE)
2758 		eqe = (struct mlx4_eqe *)param;
2759 	else
2760 		p = (int) param;
2761 
2762 	switch (event) {
2763 	case MLX4_DEV_EVENT_PORT_UP:
2764 		if (p > ibdev->num_ports)
2765 			return;
2766 		if (mlx4_is_master(dev) &&
2767 		    rdma_port_get_link_layer(&ibdev->ib_dev, p) ==
2768 			IB_LINK_LAYER_INFINIBAND) {
2769 			mlx4_ib_invalidate_all_guid_record(ibdev, p);
2770 		}
2771 		mlx4_ib_info((struct ib_device *) ibdev_ptr,
2772 			     "Port %d logical link is up\n", p);
2773 		ibev.event = IB_EVENT_PORT_ACTIVE;
2774 		break;
2775 
2776 	case MLX4_DEV_EVENT_PORT_DOWN:
2777 		if (p > ibdev->num_ports)
2778 			return;
2779 		mlx4_ib_info((struct ib_device *) ibdev_ptr,
2780 			     "Port %d logical link is down\n", p);
2781 		ibev.event = IB_EVENT_PORT_ERR;
2782 		break;
2783 
2784 	case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
2785 		ibdev->ib_active = false;
2786 		ibev.event = IB_EVENT_DEVICE_FATAL;
2787 		break;
2788 
2789 	case MLX4_DEV_EVENT_PORT_MGMT_CHANGE:
2790 		ew = kmalloc(sizeof *ew, GFP_ATOMIC);
2791 		if (!ew) {
2792 			pr_err("failed to allocate memory for events work\n");
2793 			break;
2794 		}
2795 
2796 		INIT_WORK(&ew->work, handle_port_mgmt_change_event);
2797 		memcpy(&ew->ib_eqe, eqe, sizeof *eqe);
2798 		ew->ib_dev = ibdev;
2799 		/* need to queue only for port owner, which uses GEN_EQE */
2800 		if (mlx4_is_master(dev))
2801 			queue_work(wq, &ew->work);
2802 		else
2803 			handle_port_mgmt_change_event(&ew->work);
2804 		return;
2805 
2806 	case MLX4_DEV_EVENT_SLAVE_INIT:
2807 		/* here, p is the slave id */
2808 		do_slave_init(ibdev, p, 1);
2809 		return;
2810 
2811 	case MLX4_DEV_EVENT_SLAVE_SHUTDOWN:
2812 		/* here, p is the slave id */
2813 		do_slave_init(ibdev, p, 0);
2814 		return;
2815 
2816 	default:
2817 		return;
2818 	}
2819 
2820 	ibev.device	      = ibdev_ptr;
2821 	ibev.element.port_num = (u8) p;
2822 
2823 	ib_dispatch_event(&ibev);
2824 }
2825 
2826 static struct mlx4_interface mlx4_ib_interface = {
2827 	.add		= mlx4_ib_add,
2828 	.remove		= mlx4_ib_remove,
2829 	.event		= mlx4_ib_event,
2830 	.protocol	= MLX4_PROT_IB_IPV6
2831 };
2832 
mlx4_ib_init(void)2833 static int __init mlx4_ib_init(void)
2834 {
2835 	int err;
2836 
2837 	wq = create_singlethread_workqueue("mlx4_ib");
2838 	if (!wq)
2839 		return -ENOMEM;
2840 
2841 	err = mlx4_ib_mcg_init();
2842 	if (err)
2843 		goto clean_proc;
2844 
2845 	init_dev_assign();
2846 
2847 	err = mlx4_register_interface(&mlx4_ib_interface);
2848 	if (err)
2849 		goto clean_mcg;
2850 
2851 	return 0;
2852 
2853 clean_mcg:
2854 	mlx4_ib_mcg_destroy();
2855 
2856 clean_proc:
2857 	destroy_workqueue(wq);
2858 	return err;
2859 }
2860 
mlx4_ib_cleanup(void)2861 static void __exit mlx4_ib_cleanup(void)
2862 {
2863 	mlx4_unregister_interface(&mlx4_ib_interface);
2864 	mlx4_ib_mcg_destroy();
2865 	destroy_workqueue(wq);
2866 
2867 	kfree(dev_num_str_bitmap);
2868 }
2869 
2870 module_init_order(mlx4_ib_init, SI_ORDER_MIDDLE);
2871 module_exit(mlx4_ib_cleanup);
2872 
2873 static int
mlx4ib_evhand(module_t mod,int event,void * arg)2874 mlx4ib_evhand(module_t mod, int event, void *arg)
2875 {
2876         return (0);
2877 }
2878 
2879 static moduledata_t mlx4ib_mod = {
2880         .name = "mlx4ib",
2881         .evhand = mlx4ib_evhand,
2882 };
2883 
2884 DECLARE_MODULE(mlx4ib, mlx4ib_mod, SI_SUB_SMP, SI_ORDER_ANY);
2885 MODULE_DEPEND(mlx4ib, mlx4, 1, 1, 1);
2886 MODULE_DEPEND(mlx4ib, ibcore, 1, 1, 1);
2887 MODULE_DEPEND(mlx4ib, linuxkpi, 1, 1, 1);
2888