1/*
2
3 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
4 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses.  You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 *     Redistribution and use in source and binary forms, with or
13 *     without modification, are permitted provided that the following
14 *     conditions are met:
15 *
16 *      - Redistributions of source code must retain the above
17 *        copyright notice, this list of conditions and the following
18 *        disclaimer.
19 *
20 *      - Redistributions in binary form must reproduce the above
21 *        copyright notice, this list of conditions and the following
22 *        disclaimer in the documentation and/or other materials
23 *        provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 */
34#include <rdma/ib_verbs.h>
35
36#include <mlx4ib.h>
37#include <debug.h>
38/*
39 #include <linux/module.h>
40
41 #ifdef __linux__
42 #include <linux/proc_fs.h>
43 #endif
44
45 #include <linux/slab.h>
46 */
47#include <linux/err.h>
48#include <linux/netdevice.h>
49/*
50 #include <linux/inetdevice.h>
51 #include <linux/if_vlan.h>
52 */
53#include <linux/bitops.h>
54#include <linux/if_ether.h>
55/*
56 #include <linux/fs.h>
57 */
58#include <linux/mm.h>
59#include <linux/log2.h>
60#include <linux/gfp.h>
61
62#include <rdma/ib_smi.h>
63#include <rdma/ib_user_verbs.h>
64#include <rdma/ib_addr.h>
65#include <rdma/ib_mad.h>
66
67#include <linux/mlx4/driver.h>
68#include <linux/mlx4/cmd.h>
69/*
70 #include <linux/sched.h>
71 */
72
73#include <barrelfish/deferred.h>
74#include <barrelfish/sys_debug.h>
75
76#include "mlx4_ib.h"
77#include "user.h"
78/*
79 #include "wc.h"
80
81 #define DRV_NAME	MLX4_IB_DRV_NAME
82 #define DRV_VERSION	"1.0"
83 #define DRV_RELDATE	"April 4, 2008"
84
85 #define MLX4_IB_DRIVER_PROC_DIR_NAME "driver/mlx4_ib"
86 #define MLX4_IB_MRS_PROC_DIR_NAME "mrs"
87
88 MODULE_AUTHOR("Roland Dreier");
89 MODULE_DESCRIPTION("Mellanox ConnectX HCA InfiniBand driver");
90 MODULE_LICENSE("Dual BSD/GPL");
91 MODULE_VERSION(DRV_VERSION);
92
93 int mlx4_ib_sm_guid_assign = 1;
94
95 #ifdef __linux__
96 struct proc_dir_entry *mlx4_mrs_dir_entry;
97 static struct proc_dir_entry *mlx4_ib_driver_dir_entry;
98 #endif
99
100 module_param_named(sm_guid_assign, mlx4_ib_sm_guid_assign, int, 0444);
101 MODULE_PARM_DESC(sm_guid_assign, "Enable SM alias_GUID assignment if sm_guid_assign > 0 (Default: 1)");
102
103 static char dev_assign_str[512];
104 //module_param_string(dev_assign_str, dev_assign_str, sizeof(dev_assign_str), 0644);
105 MODULE_PARM_DESC(dev_assign_str, "Map all device function numbers to "
106 "IB device numbers following the  pattern: "
107 "bb:dd.f-0,bb:dd.f-1,... (all numbers are hexadecimals)."
108 " Max supported devices - 32");
109
110 static const char mlx4_ib_version[] =
111 DRV_NAME ": Mellanox ConnectX InfiniBand driver v"
112 DRV_VERSION " (" DRV_RELDATE ")\n";
113
114 struct update_gid_work {
115 struct work_struct	work;
116 union ib_gid		gids[128];
117 struct mlx4_ib_dev     *dev;
118 int			port;
119 };
120
121 struct dev_rec {
122 int	bus;
123 int	dev;
124 int	func;
125 int	nr;
126 };
127
128 #define MAX_DR 32
129 static struct dev_rec dr[MAX_DR];
130
131 static void do_slave_init(struct mlx4_ib_dev *ibdev, int slave, int do_init);
132
133 static struct workqueue_struct *wq;
134 */
135static void init_query_mad(struct ib_smp *mad) {
136	mad->base_version = 1;
137	mad->mgmt_class = IB_MGMT_CLASS_SUBN_LID_ROUTED;
138	mad->class_version = 1;
139	mad->method = IB_MGMT_METHOD_GET;
140}
141/*
142 static union ib_gid zgid;
143
144 static int mlx4_ib_query_device(struct ib_device *ibdev,
145 struct ib_device_attr *props)
146 {
147 struct mlx4_ib_dev *dev = to_mdev(ibdev);
148 struct ib_smp *in_mad  = NULL;
149 struct ib_smp *out_mad = NULL;
150 int err = -ENOMEM;
151
152 in_mad  = calloc(1,sizeof *in_mad);
153 out_mad = malloc(sizeof *out_mad);
154 if (!in_mad || !out_mad)
155 goto out;
156
157 init_query_mad(in_mad);
158 in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
159
160 err = mlx4_MAD_IFC(to_mdev(ibdev), MLX4_MAD_IFC_IGNORE_KEYS,
161 1, NULL, NULL, in_mad, out_mad);
162 if (err)
163 goto out;
164
165 memset(props, 0, sizeof *props);
166
167 props->fw_ver = dev->dev->caps.fw_ver;
168 props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
169 IB_DEVICE_PORT_ACTIVE_EVENT		|
170 IB_DEVICE_SYS_IMAGE_GUID		|
171 IB_DEVICE_RC_RNR_NAK_GEN		|
172 IB_DEVICE_BLOCK_MULTICAST_LOOPBACK	|
173 IB_DEVICE_SHARED_MR;
174
175 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_PKEY_CNTR)
176 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
177 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_QKEY_CNTR)
178 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
179 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_APM)
180 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
181 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UD_AV_PORT)
182 props->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
183 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_IPOIB_CSUM)
184 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
185 if (dev->dev->caps.max_gso_sz && dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BLH)
186 props->device_cap_flags |= IB_DEVICE_UD_TSO;
187 if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_RESERVED_LKEY)
188 props->device_cap_flags |= IB_DEVICE_LOCAL_DMA_LKEY;
189 if ((dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_LOCAL_INV) &&
190 (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_REMOTE_INV) &&
191 (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_FAST_REG_WR))
192 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
193 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC)
194 props->device_cap_flags |= IB_DEVICE_XRC;
195
196 props->device_cap_flags |= IB_DEVICE_QPG;
197 if (dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS) {
198 props->device_cap_flags |= IB_DEVICE_UD_RSS;
199 props->max_rss_tbl_sz = dev->dev->caps.max_rss_tbl_sz;
200 }
201 props->vendor_id	   = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
202 0xffffff;
203 props->vendor_part_id	   = dev->dev->pdev->device;
204 props->hw_ver		   = be32_to_cpup((__be32 *) (out_mad->data + 32));
205 memcpy(&props->sys_image_guid, out_mad->data +	4, 8);
206
207 props->max_mr_size	   = ~0ull;
208 props->page_size_cap	   = dev->dev->caps.page_size_cap;
209 props->max_qp		   = dev->dev->quotas.qp;
210 props->max_qp_wr	   = dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE;
211 props->max_sge		   = min(dev->dev->caps.max_sq_sg,
212 dev->dev->caps.max_rq_sg);
213 props->max_cq		   = dev->dev->quotas.cq;
214 props->max_cqe		   = dev->dev->caps.max_cqes;
215 props->max_mr		   = dev->dev->quotas.mpt;
216 props->max_pd		   = dev->dev->caps.num_pds - dev->dev->caps.reserved_pds;
217 props->max_qp_rd_atom	   = dev->dev->caps.max_qp_dest_rdma;
218 props->max_qp_init_rd_atom = dev->dev->caps.max_qp_init_rdma;
219 props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
220 props->max_srq		   = dev->dev->quotas.srq;
221 props->max_srq_wr	   = dev->dev->caps.max_srq_wqes - 1;
222 props->max_srq_sge	   = dev->dev->caps.max_srq_sge;
223 props->max_fast_reg_page_list_len = MLX4_MAX_FAST_REG_PAGES;
224 props->local_ca_ack_delay  = dev->dev->caps.local_ca_ack_delay;
225 props->atomic_cap	   = dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_ATOMIC ?
226 IB_ATOMIC_HCA : IB_ATOMIC_NONE;
227 props->masked_atomic_cap   = props->atomic_cap;
228 props->max_pkeys	   = dev->dev->caps.pkey_table_len[1];
229 props->max_mcast_grp	   = dev->dev->caps.num_mgms + dev->dev->caps.num_amgms;
230 props->max_mcast_qp_attach = dev->dev->caps.num_qp_per_mgm;
231 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
232 props->max_mcast_grp;
233 props->max_map_per_fmr = dev->dev->caps.max_fmr_maps;
234
235 out:
236 free(in_mad);
237 free(out_mad);
238
239 return err;
240 }
241 */
242static enum rdma_link_layer mlx4_ib_port_link_layer(struct ib_device *device,
243		u8 port_num) {
244	struct mlx4_dev *dev = to_mdev(device)->dev;
245
246	return dev->caps.port_mask[port_num] == MLX4_PORT_TYPE_IB ?
247			IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
248}
249
250static int ib_link_query_port(struct ib_device *ibdev, u8 port,
251		struct ib_port_attr *props, int netw_view) {
252	struct ib_smp *in_mad = NULL;
253	struct ib_smp *out_mad = NULL;
254	int ext_active_speed;
255	int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
256	int err = -ENOMEM;
257
258	__be16 *aux;
259	__be32 *aux32;
260
261	in_mad = calloc(1, sizeof *in_mad);
262	out_mad = malloc(sizeof *out_mad);
263	if (!in_mad || !out_mad)
264		goto out;
265
266	init_query_mad(in_mad);
267	in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
268	in_mad->attr_mod = cpu_to_be32(port);
269
270	if (mlx4_is_mfunc(to_mdev(ibdev)->dev) && netw_view)
271		mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
272
273	err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port, NULL, NULL, in_mad,
274			out_mad);
275	if (err)
276		goto out;
277
278	aux = (__be16 *) (out_mad->data + 16);
279	props->lid = be16_to_cpup(aux);
280	props->lmc = out_mad->data[34] & 0x7;
281	aux = (__be16 *) (out_mad->data + 18);
282	props->sm_lid = be16_to_cpup(aux);
283	props->sm_sl = out_mad->data[36] & 0xf;
284	props->state = out_mad->data[32] & 0xf;
285	props->phys_state = out_mad->data[33] >> 4;
286	aux32 = (__be32 *) (out_mad->data + 20);
287	props->port_cap_flags = be32_to_cpup(aux32);
288	if (netw_view)
289		props->gid_tbl_len = out_mad->data[50];
290	else
291		props->gid_tbl_len = to_mdev(ibdev)->dev->caps.gid_table_len[port];
292	props->max_msg_sz = to_mdev(ibdev)->dev->caps.max_msg_sz;
293	props->pkey_tbl_len = to_mdev(ibdev)->dev->caps.pkey_table_len[port];
294	aux = (__be16 *) (out_mad->data + 46);
295	props->bad_pkey_cntr = be16_to_cpup(aux);
296	aux = (__be16 *) (out_mad->data + 48);
297	props->qkey_viol_cntr = be16_to_cpup(aux);
298	props->active_width = out_mad->data[31] & 0xf;
299	props->active_speed = out_mad->data[35] >> 4;
300	props->max_mtu = out_mad->data[41] & 0xf;
301	props->active_mtu = out_mad->data[36] >> 4;
302	props->subnet_timeout = out_mad->data[51] & 0x1f;
303	props->max_vl_num = out_mad->data[37] >> 4;
304	props->init_type_reply = out_mad->data[41] >> 4;
305
306	/*Check if extended speeds (EDR/FDR/...) are supported*/
307	if (props->port_cap_flags & IB_PORT_EXTENDED_SPEEDS_SUP) {
308		ext_active_speed = out_mad->data[62] >> 4;
309
310		switch (ext_active_speed) {
311		case 1:
312			props->active_speed = IB_SPEED_FDR;
313			break;
314		case 2:
315			props->active_speed = IB_SPEED_EDR;
316			break;
317		}
318	}
319
320	/*If reported active speed is QDR, check if is FDR-10*/
321	if (props->active_speed == IB_SPEED_QDR) {
322		init_query_mad(in_mad);
323		in_mad->attr_id = MLX4_ATTR_EXTENDED_PORT_INFO;
324		in_mad->attr_mod = cpu_to_be32(port);
325
326		err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port,
327		NULL, NULL, in_mad, out_mad);
328		if (err)
329			goto out;
330
331		/*Checking LinkSpeedActive for FDR-10*/
332		if (out_mad->data[15] & 0x1)
333			props->active_speed = IB_SPEED_FDR10;
334	}
335
336	/*Avoid wrong speed value returned by FW if the IB link is down.*/
337	if (props->state == IB_PORT_DOWN)
338		props->active_speed = IB_SPEED_SDR;
339
340	out: free(in_mad);
341	free(out_mad);
342	return err;
343}
344
345static u8 state_to_phys_state(enum ib_port_state state) {
346	return state == IB_PORT_ACTIVE ? 5 : 3;
347}
348
349static int eth_link_query_port(struct ib_device *ibdev, u8 port,
350		struct ib_port_attr *props, int netw_view) {
351
352	struct mlx4_ib_dev *mdev = to_mdev(ibdev);
353	struct mlx4_ib_iboe *iboe = &mdev->iboe;
354	struct net_device *ndev;
355	/*enum ib_mtu tmp;*/
356	struct mlx4_cmd_mailbox *mailbox;
357	int err = 0;
358
359	mailbox = mlx4_alloc_cmd_mailbox();
360	if (IS_ERR(mailbox))
361		return PTR_ERR(mailbox);
362
363	err = mlx4_cmd_box(mdev->dev, 0, mailbox->dma, port, 0, MLX4_CMD_QUERY_PORT,
364			MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
365	if (err)
366		goto out;
367
368	props->active_width =
369			(((u8 *) mailbox->buf)[5] == 0x40) ? IB_WIDTH_4X : IB_WIDTH_1X;
370	props->active_speed = IB_SPEED_QDR;
371	props->port_cap_flags = IB_PORT_CM_SUP;
372	if (netw_view)
373		props->gid_tbl_len = MLX4_ROCE_MAX_GIDS;
374	else
375		props->gid_tbl_len = mdev->dev->caps.gid_table_len[port];
376
377	props->max_msg_sz = mdev->dev->caps.max_msg_sz;
378	props->pkey_tbl_len = 1;
379	props->max_mtu = IB_MTU_4096;
380	props->max_vl_num = 2;
381	props->state = IB_PORT_DOWN;
382	props->phys_state = state_to_phys_state(props->state);
383	props->active_mtu = IB_MTU_256;
384	/*spin_lock(&iboe->lock);*/
385	ndev = iboe->netdevs[port - 1];
386	if (!ndev)
387		goto out_unlock;
388
389	//tmp = iboe_get_mtu(ndev->if_mtu);
390	props->active_mtu = /*tmp ? min(props->max_mtu, tmp) :*/IB_MTU_256;
391
392	props->state =
393			/*(netif_running(ndev) && netif_carrier_ok(ndev)) ?*/IB_PORT_ACTIVE /*: IB_PORT_DOWN*/;
394	props->phys_state = state_to_phys_state(props->state);
395	out_unlock: /*spin_unlock(&iboe->lock);*/
396	out: mlx4_free_cmd_mailbox(mailbox);
397	return err;
398}
399
400int __mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
401		struct ib_port_attr *props, int netw_view) {
402	int err;
403
404	memset(props, 0, sizeof *props);
405
406	err = mlx4_ib_port_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND ?
407			ib_link_query_port(ibdev, port, props, netw_view) :
408			eth_link_query_port(ibdev, port, props, netw_view);
409
410	return err;
411}
412
413static int mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
414		struct ib_port_attr *props) {
415	/*returns host view*/
416	return __mlx4_ib_query_port(ibdev, port, props, 0);
417}
418
419int __mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
420		union ib_gid *gid, int netw_view) {
421	struct ib_smp *in_mad = NULL;
422	struct ib_smp *out_mad = NULL;
423	int err = -ENOMEM;
424	struct mlx4_ib_dev *dev = to_mdev(ibdev);
425	int clear = 0;
426	int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
427
428	in_mad = calloc(1, sizeof *in_mad);
429	out_mad = malloc(sizeof *out_mad);
430	if (!in_mad || !out_mad)
431		goto out;
432
433	init_query_mad(in_mad);
434	in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
435	in_mad->attr_mod = cpu_to_be32(port);
436
437	if (mlx4_is_mfunc(dev->dev) && netw_view)
438		mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
439
440	err = mlx4_MAD_IFC(dev, mad_ifc_flags, port, NULL, NULL, in_mad, out_mad);
441	if (err)
442		goto out;
443
444	memcpy(gid->raw, out_mad->data + 8, 8);
445
446	if (mlx4_is_mfunc(dev->dev) && !netw_view) {
447		if (index) {
448			/* For any index > 0, return the null guid*/
449			err = 0;
450			clear = 1;
451			goto out;
452		}
453	}
454
455	init_query_mad(in_mad);
456	in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
457	in_mad->attr_mod = cpu_to_be32(index / 8);
458
459	err = mlx4_MAD_IFC(dev, mad_ifc_flags, port,
460	NULL, NULL, in_mad, out_mad);
461	if (err)
462		goto out;
463
464	memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
465
466	out: if (clear)
467		memset(gid->raw + 8, 0, 8);
468	free(in_mad);
469	free(out_mad);
470	return err;
471}
472
473static int iboe_query_gid(struct ib_device *ibdev, u8 port, int index,
474		union ib_gid *gid) {
475	struct mlx4_ib_dev *dev = to_mdev(ibdev);
476
477	*gid = dev->iboe.gid_table[port - 1][index];
478
479	return 0;
480}
481
482static int mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
483		union ib_gid *gid) {
484	if (rdma_port_get_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND)
485		return __mlx4_ib_query_gid(ibdev, port, index, gid, 0);
486	else
487		return iboe_query_gid(ibdev, port, index, gid);
488}
489
490int __mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey,
491		int netw_view) {
492	struct ib_smp *in_mad = NULL;
493	struct ib_smp *out_mad = NULL;
494	int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
495	int err = -ENOMEM;
496
497	in_mad = calloc(1, sizeof *in_mad);
498	out_mad = malloc(sizeof *out_mad);
499	if (!in_mad || !out_mad)
500		goto out;
501
502	init_query_mad(in_mad);
503	in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
504	in_mad->attr_mod = cpu_to_be32(index / 32);
505
506	if (mlx4_is_mfunc(to_mdev(ibdev)->dev) && netw_view)
507		mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
508
509	err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port, NULL, NULL, in_mad,
510			out_mad);
511	if (err)
512		goto out;
513
514	*pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
515
516	out: free(in_mad);
517	free(out_mad);
518	return err;
519}
520
521static int mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
522		u16 *pkey) {
523	return __mlx4_ib_query_pkey(ibdev, port, index, pkey, 0);
524}
525/*
526 static int mlx4_ib_modify_device(struct ib_device *ibdev, int mask,
527 struct ib_device_modify *props)
528 {
529 struct mlx4_cmd_mailbox *mailbox;
530 unsigned long flags;
531
532 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
533 return -EOPNOTSUPP;
534
535 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
536 return 0;
537
538 if (mlx4_is_slave(to_mdev(ibdev)->dev))
539 return -EOPNOTSUPP;
540
541 spin_lock_irqsave(&to_mdev(ibdev)->sm_lock, flags);
542 memcpy(ibdev->node_desc, props->node_desc, 64);
543 spin_unlock_irqrestore(&to_mdev(ibdev)->sm_lock, flags);
544
545
546 * If possible, pass node desc to FW, so it can generate
547 * a 144 trap.  If cmd fails, just ignore.
548
549 mailbox = mlx4_alloc_cmd_mailbox(to_mdev(ibdev)->dev);
550 if (IS_ERR(mailbox))
551 return 0;
552
553 memset(mailbox->buf, 0, 256);
554 memcpy(mailbox->buf, props->node_desc, 64);
555 mlx4_cmd(to_mdev(ibdev)->dev, mailbox->dma, 1, 0,
556 MLX4_CMD_SET_NODE, MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
557
558 mlx4_free_cmd_mailbox(to_mdev(ibdev)->dev, mailbox);
559
560 return 0;
561 }
562
563 static int mlx4_SET_PORT(struct mlx4_ib_dev *dev, u8 port, int reset_qkey_viols,
564 u32 cap_mask)
565 {
566 struct mlx4_cmd_mailbox *mailbox;
567 int err;
568 u8 is_eth = dev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
569
570 mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
571 if (IS_ERR(mailbox))
572 return PTR_ERR(mailbox);
573
574 memset(mailbox->buf, 0, 256);
575
576 if (dev->dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
577 *(u8 *) mailbox->buf	     = !!reset_qkey_viols << 6;
578 ((__be32 *) mailbox->buf)[2] = cpu_to_be32(cap_mask);
579 } else {
580 ((u8 *) mailbox->buf)[3]     = !!reset_qkey_viols;
581 ((__be32 *) mailbox->buf)[1] = cpu_to_be32(cap_mask);
582 }
583
584 err = mlx4_cmd(dev->dev, mailbox->dma, port, is_eth, MLX4_CMD_SET_PORT,
585 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
586
587 mlx4_free_cmd_mailbox(dev->dev, mailbox);
588 return err;
589 }
590
591 static int mlx4_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
592 struct ib_port_modify *props)
593 {
594 struct ib_port_attr attr;
595 u32 cap_mask;
596 int err;
597
598 mutex_lock(&to_mdev(ibdev)->cap_mask_mutex);
599
600 err = mlx4_ib_query_port(ibdev, port, &attr);
601 if (err)
602 goto out;
603
604 cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
605 ~props->clr_port_cap_mask;
606
607 err = mlx4_SET_PORT(to_mdev(ibdev), port,
608 !!(mask & IB_PORT_RESET_QKEY_CNTR),
609 cap_mask);
610
611 out:
612 mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
613 return err;
614 }
615
616 static struct ib_ucontext *mlx4_ib_alloc_ucontext(struct ib_device *ibdev,
617 struct ib_udata *udata)
618 {
619 struct mlx4_ib_dev *dev = to_mdev(ibdev);
620 struct mlx4_ib_ucontext *context;
621 struct mlx4_ib_alloc_ucontext_resp_v3 resp_v3;
622 struct mlx4_ib_alloc_ucontext_resp resp;
623 int err;
624
625 if (!dev->ib_active)
626 return ERR_PTR(-EAGAIN);
627
628 if (ibdev->uverbs_abi_ver == MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION) {
629 resp_v3.qp_tab_size      = dev->dev->caps.num_qps;
630 if (mlx4_wc_enabled()) {
631 resp_v3.bf_reg_size      = dev->dev->caps.bf_reg_size;
632 resp_v3.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
633 } else {
634 resp_v3.bf_reg_size      = 0;
635 resp_v3.bf_regs_per_page = 0;
636 }
637 } else {
638 resp.dev_caps	      = dev->dev->caps.userspace_caps;
639 resp.qp_tab_size      = dev->dev->caps.num_qps;
640 if (mlx4_wc_enabled()) {
641 resp.bf_reg_size      = dev->dev->caps.bf_reg_size;
642 resp.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
643 } else {
644 resp.bf_reg_size      = 0;
645 resp.bf_regs_per_page = 0;
646 }
647 resp.cqe_size	      = dev->dev->caps.cqe_size;
648 }
649
650 context = malloc(sizeof *context);
651 if (!context)
652 return ERR_PTR(-ENOMEM);
653
654 err = mlx4_uar_alloc(to_mdev(ibdev)->dev, &context->uar);
655 if (err) {
656 free(context);
657 return ERR_PTR(err);
658 }
659
660 INIT_LIST_HEAD(&context->db_page_list);
661 mutex_init(&context->db_page_mutex);
662
663 if (ibdev->uverbs_abi_ver == MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION)
664 err = ib_copy_to_udata(udata, &resp_v3, sizeof(resp_v3));
665 else
666 err = ib_copy_to_udata(udata, &resp, sizeof(resp));
667
668 if (err) {
669 mlx4_uar_free(to_mdev(ibdev)->dev, &context->uar);
670 free(context);
671 return ERR_PTR(-EFAULT);
672 }
673
674 return &context->ibucontext;
675 }
676
677 static int mlx4_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
678 {
679 struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
680
681 mlx4_uar_free(to_mdev(ibcontext->device)->dev, &context->uar);
682 free(context);
683
684 return 0;
685 }
686 #ifdef __linux__
687 static unsigned long mlx4_ib_get_unmapped_area(struct file *file,
688 unsigned long addr,
689 unsigned long len, unsigned long pgoff,
690 unsigned long flags)
691 {
692 struct mm_struct *mm;
693 struct vm_area_struct *vma;
694 unsigned long start_addr;
695 unsigned long page_size_order;
696 unsigned long  command;
697
698 mm = current->mm;
699 if (addr)
700 return current->mm->get_unmapped_area(file, addr, len,
701 pgoff, flags);
702
703 Last 8 bits hold the  command others are data per that command
704 command = pgoff & MLX4_IB_MMAP_CMD_MASK;
705 if (command != MLX4_IB_MMAP_GET_CONTIGUOUS_PAGES)
706 return current->mm->get_unmapped_area(file, addr, len,
707 pgoff, flags);
708
709 page_size_order = pgoff >> MLX4_IB_MMAP_CMD_BITS;
710 code is based on the huge-pages get_unmapped_area code
711 start_addr = mm->free_area_cache;
712
713 if (len <= mm->cached_hole_size)
714 start_addr = TASK_UNMAPPED_BASE;
715
716
717 full_search:
718 addr = ALIGN(start_addr, 1 << page_size_order);
719
720 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
721 At this point:  (!vma || addr < vma->vm_end).
722 if (TASK_SIZE - len < addr) {
723
724 * Start a new search - just in case we missed
725 * some holes.
726
727 if (start_addr != TASK_UNMAPPED_BASE) {
728 start_addr = TASK_UNMAPPED_BASE;
729 goto full_search;
730 }
731 return -ENOMEM;
732 }
733
734 if (!vma || addr + len <= vma->vm_start)
735 return addr;
736 addr = ALIGN(vma->vm_end, 1 << page_size_order);
737 }
738 }
739 #endif
740
741 static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
742 {
743 struct mlx4_ib_dev *dev = to_mdev(context->device);
744 int err;
745
746 Last 8 bits hold the  command others are data per that command
747 unsigned long  command = vma->vm_pgoff & MLX4_IB_MMAP_CMD_MASK;
748
749 if (command < MLX4_IB_MMAP_GET_CONTIGUOUS_PAGES) {
750 compatability handling for commands 0 & 1
751 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
752 return -EINVAL;
753 }
754 if (command == MLX4_IB_MMAP_UAR_PAGE) {
755 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
756
757 if (io_remap_pfn_range(vma, vma->vm_start,
758 to_mucontext(context)->uar.pfn,
759 PAGE_SIZE, vma->vm_page_prot))
760 return -EAGAIN;
761 } else if (command == MLX4_IB_MMAP_BLUE_FLAME_PAGE &&
762 dev->dev->caps.bf_reg_size != 0) {
763 vma->vm_page_prot = pgprot_wc(vma->vm_page_prot);
764
765 if (io_remap_pfn_range(vma, vma->vm_start,
766 to_mucontext(context)->uar.pfn +
767 dev->dev->caps.num_uars,
768 PAGE_SIZE, vma->vm_page_prot))
769 return -EAGAIN;
770 } else if (command == MLX4_IB_MMAP_GET_CONTIGUOUS_PAGES) {
771 Getting contiguous physical pages
772 unsigned long total_size = vma->vm_end - vma->vm_start;
773 unsigned long page_size_order = (vma->vm_pgoff) >>
774 MLX4_IB_MMAP_CMD_BITS;
775 struct ib_cmem *ib_cmem;
776 ib_cmem = ib_cmem_alloc_contiguous_pages(context, total_size,
777 page_size_order);
778 if (IS_ERR(ib_cmem)) {
779 err = PTR_ERR(ib_cmem);
780 return err;
781 }
782
783 err = ib_cmem_map_contiguous_pages_to_vma(ib_cmem, vma);
784 if (err) {
785 ib_cmem_release_contiguous_pages(ib_cmem);
786 return err;
787 }
788 return 0;
789 } else
790 return -EINVAL;
791
792 return 0;
793 }
794 */
795static struct ib_pd *mlx4_ib_alloc_pd(struct ib_device *ibdev,
796		struct ib_ucontext *context, struct ib_udata *udata) {
797	struct mlx4_ib_pd *pd;
798	int err;
799
800	pd = malloc(sizeof *pd);
801	if (!pd)
802		return ERR_PTR(-ENOMEM);
803
804	err = mlx4_pd_alloc(to_mdev(ibdev)->dev, &pd->pdn);
805	if (err) {
806		free(pd);
807		return ERR_PTR(err);
808	}
809
810	if (context)
811		if (ib_copy_to_udata(udata, &pd->pdn, sizeof(__u32 ))) {
812			/*mlx4_pd_free(to_mdev(ibdev)->dev, pd->pdn);*/
813			free(pd);
814			return ERR_PTR(-EFAULT);
815		}
816
817	return &pd->ibpd;
818}
819/*
820 static int mlx4_ib_dealloc_pd(struct ib_pd *pd)
821 {
822 mlx4_pd_free(to_mdev(pd->device)->dev, to_mpd(pd)->pdn);
823 free(pd);
824
825 return 0;
826 }
827
828 static struct ib_xrcd *mlx4_ib_alloc_xrcd(struct ib_device *ibdev,
829 struct ib_ucontext *context,
830 struct ib_udata *udata)
831 {
832 struct mlx4_ib_xrcd *xrcd;
833 int err;
834
835 if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC))
836 return ERR_PTR(-ENOSYS);
837
838 xrcd = malloc(sizeof *xrcd);
839 if (!xrcd)
840 return ERR_PTR(-ENOMEM);
841
842 err = mlx4_xrcd_alloc(to_mdev(ibdev)->dev, &xrcd->xrcdn);
843 if (err)
844 goto err1;
845
846 xrcd->pd = ib_alloc_pd(ibdev);
847 if (IS_ERR(xrcd->pd)) {
848 err = PTR_ERR(xrcd->pd);
849 goto err2;
850 }
851
852 xrcd->cq = ib_create_cq(ibdev, NULL, NULL, xrcd, 1, 0);
853 if (IS_ERR(xrcd->cq)) {
854 err = PTR_ERR(xrcd->cq);
855 goto err3;
856 }
857
858 return &xrcd->ibxrcd;
859
860 err3:
861 ib_dealloc_pd(xrcd->pd);
862 err2:
863 mlx4_xrcd_free(to_mdev(ibdev)->dev, xrcd->xrcdn);
864 err1:
865 free(xrcd);
866 return ERR_PTR(err);
867 }
868
869 static int mlx4_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
870 {
871 ib_destroy_cq(to_mxrcd(xrcd)->cq);
872 ib_dealloc_pd(to_mxrcd(xrcd)->pd);
873 mlx4_xrcd_free(to_mdev(xrcd->device)->dev, to_mxrcd(xrcd)->xrcdn);
874 free(xrcd);
875
876 return 0;
877 }
878
879 static int add_gid_entry(struct ib_qp *ibqp, union ib_gid *gid)
880 {
881 struct mlx4_ib_qp *mqp = to_mqp(ibqp);
882 struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
883 struct mlx4_ib_gid_entry *ge;
884
885 ge = calloc(1,sizeof *ge);
886 if (!ge)
887 return -ENOMEM;
888
889 ge->gid = *gid;
890 if (mlx4_ib_add_mc(mdev, mqp, gid)) {
891 ge->port = mqp->port;
892 ge->added = 1;
893 }
894
895 mutex_lock(&mqp->mutex);
896 list_add_tail(&ge->list, &mqp->gid_list);
897 mutex_unlock(&mqp->mutex);
898
899 return 0;
900 }
901
902 int mlx4_ib_add_mc(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
903 union ib_gid *gid)
904 {
905 u8 mac[6];
906 struct net_device *ndev;
907 int ret = 0;
908
909 if (!mqp->port)
910 return 0;
911
912 spin_lock(&mdev->iboe.lock);
913 ndev = mdev->iboe.netdevs[mqp->port - 1];
914 if (ndev)
915 dev_hold(ndev);
916 spin_unlock(&mdev->iboe.lock);
917
918 if (ndev) {
919 rdma_get_mcast_mac((struct in6_addr *)gid, mac);
920 rtnl_lock();
921 dev_mc_add(mdev->iboe.netdevs[mqp->port - 1], mac, 6, 0);
922 ret = 1;
923 rtnl_unlock();
924 dev_put(ndev);
925 }
926
927 return ret;
928 }
929
930 struct mlx4_ib_steering {
931 struct list_head list;
932 u64 reg_id;
933 union ib_gid gid;
934 };
935
936 static int mlx4_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
937 {
938 int err;
939 struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
940 struct mlx4_ib_qp *mqp = to_mqp(ibqp);
941 u64 reg_id;
942 struct mlx4_ib_steering *ib_steering = NULL;
943
944 if (mdev->dev->caps.steering_mode ==
945 MLX4_STEERING_MODE_DEVICE_MANAGED) {
946 ib_steering = malloc(sizeof(*ib_steering));
947 if (!ib_steering)
948 return -ENOMEM;
949 }
950
951 err = mlx4_multicast_attach(mdev->dev, &mqp->mqp, gid->raw, mqp->port,
952 !!(mqp->flags &
953 MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK),
954 MLX4_PROT_IB_IPV6, &reg_id);
955 if (err)
956 goto err_malloc;
957
958 err = add_gid_entry(ibqp, gid);
959 if (err)
960 goto err_add;
961
962 if (ib_steering) {
963 memcpy(ib_steering->gid.raw, gid->raw, 16);
964 ib_steering->reg_id = reg_id;
965 mutex_lock(&mqp->mutex);
966 list_add(&ib_steering->list, &mqp->steering_rules);
967 mutex_unlock(&mqp->mutex);
968 }
969 return 0;
970
971 err_add:
972 mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
973 MLX4_PROT_IB_IPV6, reg_id);
974 err_malloc:
975 free(ib_steering);
976
977 return err;
978 }
979 */
980enum {
981	IBV_FLOW_L4_NONE = 0,
982	IBV_FLOW_L4_OTHER = 3,
983	IBV_FLOW_L4_UDP = 5,
984	IBV_FLOW_L4_TCP = 6
985};
986
987struct mlx4_cm_steering {
988	struct list_head list;
989	u64 reg_id;
990	struct ib_flow_spec spec;
991};
992
993static int flow_spec_to_net_rule(struct ib_device *dev,
994		struct ib_flow_spec *flow_spec, struct list_head *rule_list_h) {
995	struct mlx4_spec_list *spec_l2, *spec_l3, *spec_l4;
996	u64 mac_msk = cpu_to_be64(MLX4_MAC_MASK << 16);
997
998	spec_l2 = calloc(1, sizeof *spec_l2);
999	if (!spec_l2)
1000		return -ENOMEM;
1001
1002	switch (flow_spec->type) {
1003	case IB_FLOW_ETH:
1004		spec_l2->id = MLX4_NET_TRANS_RULE_ID_ETH;
1005		memcpy(spec_l2->eth.dst_mac, flow_spec->l2_id.eth.mac, ETH_ALEN);
1006		memcpy(spec_l2->eth.dst_mac_msk, &mac_msk, ETH_ALEN);
1007		spec_l2->eth.ether_type = flow_spec->l2_id.eth.ethertype;
1008		if (flow_spec->l2_id.eth.vlan_present) {
1009			spec_l2->eth.vlan_id = flow_spec->l2_id.eth.vlan;
1010			spec_l2->eth.vlan_id_msk = cpu_to_be16(0x0fff);
1011		}
1012		break;
1013	case IB_FLOW_IB_UC:
1014		spec_l2->id = MLX4_NET_TRANS_RULE_ID_IB;
1015		if (flow_spec->l2_id.ib_uc.qpn) {
1016			spec_l2->ib.l3_qpn = cpu_to_be32(flow_spec->l2_id.ib_uc.qpn);
1017			spec_l2->ib.qpn_msk = cpu_to_be32(0xffffff);
1018		}
1019		break;
1020	case IB_FLOW_IB_MC_IPV4:
1021	case IB_FLOW_IB_MC_IPV6:
1022		spec_l2->id = MLX4_NET_TRANS_RULE_ID_IB;
1023		memcpy(spec_l2->ib.dst_gid, flow_spec->l2_id.ib_mc.mgid, 16);
1024		memset(spec_l2->ib.dst_gid_msk, 0xff, 16);
1025		break;
1026	}
1027
1028	list_add_tail(&spec_l2->list, rule_list_h);
1029
1030	if (flow_spec->l2_id.eth.ethertype == cpu_to_be16(ETH_P_IP)
1031			|| flow_spec->type != IB_FLOW_ETH) {
1032		spec_l3 = calloc(1, sizeof *spec_l3);
1033		if (!spec_l3)
1034			return -ENOMEM;
1035
1036		spec_l3->id = MLX4_NET_TRANS_RULE_ID_IPV4;
1037		spec_l3->ipv4.src_ip = flow_spec->src_ip;
1038		if (flow_spec->type != IB_FLOW_IB_MC_IPV4
1039				&& flow_spec->type != IB_FLOW_IB_MC_IPV6)
1040			spec_l3->ipv4.dst_ip = flow_spec->dst_ip;
1041
1042		if (spec_l3->ipv4.src_ip)
1043			spec_l3->ipv4.src_ip_msk = MLX4_BE_WORD_MASK;
1044		if (spec_l3->ipv4.dst_ip)
1045			spec_l3->ipv4.dst_ip_msk = MLX4_BE_WORD_MASK;
1046
1047		list_add_tail(&spec_l3->list, rule_list_h);
1048	}
1049
1050	if (flow_spec->l4_protocol) {
1051		spec_l4 = calloc(1, sizeof(*spec_l4));
1052		if (!spec_l4)
1053			return -ENOMEM;
1054
1055		spec_l4->tcp_udp.src_port = flow_spec->src_port;
1056		spec_l4->tcp_udp.dst_port = flow_spec->dst_port;
1057		if (spec_l4->tcp_udp.src_port)
1058			spec_l4->tcp_udp.src_port_msk = MLX4_BE_SHORT_MASK;
1059		if (spec_l4->tcp_udp.dst_port)
1060			spec_l4->tcp_udp.dst_port_msk = MLX4_BE_SHORT_MASK;
1061
1062		switch (flow_spec->l4_protocol) {
1063		case IBV_FLOW_L4_UDP:
1064			spec_l4->id = MLX4_NET_TRANS_RULE_ID_UDP;
1065			break;
1066		case IBV_FLOW_L4_TCP:
1067			spec_l4->id = MLX4_NET_TRANS_RULE_ID_TCP;
1068			break;
1069		default:
1070			MLX4_ERR("Unsupported l4 protocol.\n");
1071			free(spec_l4);
1072			return -EPROTONOSUPPORT;
1073		}
1074		list_add_tail(&spec_l4->list, rule_list_h);
1075	}
1076	return 0;
1077}
1078
1079static int __mlx4_ib_flow_attach(struct mlx4_ib_dev *mdev,
1080		struct mlx4_ib_qp *mqp, struct ib_flow_spec *flow_spec, int priority,
1081		int lock_qp) {
1082	u64 reg_id = 0;
1083	int err = 0;
1084	struct mlx4_cm_steering *cm_flow;
1085	struct mlx4_spec_list *spec, *tmp_spec;
1086
1087	struct mlx4_net_trans_rule rule = { .queue_mode = MLX4_NET_TRANS_Q_FIFO,
1088			.exclusive = 0, };
1089
1090	rule.promisc_mode = flow_spec->rule_type;
1091	rule.port = mqp->port;
1092	rule.qpn = mqp->mqp.qpn;
1093	INIT_LIST_HEAD(&rule.list);
1094
1095	cm_flow = malloc(sizeof(*cm_flow));
1096	if (!cm_flow)
1097		return -ENOMEM;
1098
1099	if (rule.promisc_mode == MLX4_FS_REGULAR) {
1100		rule.allow_loopback = !flow_spec->block_mc_loopback;
1101		rule.priority = MLX4_DOMAIN_UVERBS | priority;
1102		err = flow_spec_to_net_rule(&mdev->ib_dev, flow_spec, &rule.list);
1103		if (err)
1104			goto free_list;
1105	}
1106
1107	err = mlx4_flow_attach(mdev->dev, &rule, &reg_id);
1108	if (err)
1109		goto free_list;
1110
1111	memcpy(&cm_flow->spec, flow_spec, sizeof(*flow_spec));
1112	cm_flow->reg_id = reg_id;
1113
1114	if (lock_qp)
1115		/*mutex_lock(&mqp->mutex);*/
1116		list_add(&cm_flow->list, &mqp->rules_list);
1117	if (lock_qp)
1118		/*mutex_unlock(&mqp->mutex);*/
1119
1120		free_list:
1121		list_for_each_entry_safe(spec, tmp_spec, &rule.list, list)
1122		{
1123			list_del(&spec->list);
1124			free(spec);
1125		}
1126	if (err) {
1127		free(cm_flow);
1128		MLX4_ERR("Fail to attach flow steering rule\n");
1129	}
1130	return err;
1131}
1132
1133static int __mlx4_ib_flow_detach(struct mlx4_ib_dev *mdev,
1134		struct mlx4_ib_qp *mqp, struct ib_flow_spec *spec, int priority,
1135		int lock_qp) {
1136	struct mlx4_cm_steering *cm_flow = NULL;
1137	int ret;
1138
1139	if (lock_qp)
1140		/*mutex_lock(&mqp->mutex);*/
1141		list_for_each_entry(cm_flow, &mqp->rules_list, list)
1142		{
1143			if (!memcmp(&cm_flow->spec, spec, sizeof(*spec))) {
1144				list_del(&cm_flow->list);
1145				break;
1146			}
1147		}
1148	if (lock_qp)
1149		/*mutex_unlock(&mqp->mutex);*/
1150
1151		if (&cm_flow->list == &mqp->rules_list) {
1152			MLX4_ERR("Couldn't find reg_id for flow spec. "
1153					"Steering rule is left attached\n");
1154			return -EINVAL;
1155		}
1156
1157	ret = mlx4_flow_detach(mdev->dev, cm_flow->reg_id);
1158
1159	free(cm_flow);
1160	return ret;
1161}
1162/*
1163 static int mlx4_ib_flow_attach(struct ib_qp *qp, struct ib_flow_spec *flow_spec,
1164 int priority)
1165 {
1166 return __mlx4_ib_flow_attach(to_mdev(qp->device), to_mqp(qp),
1167 flow_spec, priority, 1);
1168 }
1169
1170 static int mlx4_ib_flow_detach(struct ib_qp *qp, struct ib_flow_spec *spec,
1171 int priority)
1172 {
1173 return __mlx4_ib_flow_detach(to_mdev(qp->device), to_mqp(qp),
1174 spec, priority, 1);
1175 }
1176
1177 static struct mlx4_ib_gid_entry *find_gid_entry(struct mlx4_ib_qp *qp, u8 *raw)
1178 {
1179 struct mlx4_ib_gid_entry *ge;
1180 struct mlx4_ib_gid_entry *tmp;
1181 struct mlx4_ib_gid_entry *ret = NULL;
1182
1183 list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
1184 if (!memcmp(raw, ge->gid.raw, 16)) {
1185 ret = ge;
1186 break;
1187 }
1188 }
1189
1190 return ret;
1191 }
1192
1193 static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
1194 {
1195 int err;
1196 struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
1197 struct mlx4_ib_qp *mqp = to_mqp(ibqp);
1198 u8 mac[6];
1199 struct net_device *ndev;
1200 struct mlx4_ib_gid_entry *ge;
1201 u64 reg_id = 0;
1202
1203 if (mdev->dev->caps.steering_mode ==
1204 MLX4_STEERING_MODE_DEVICE_MANAGED) {
1205 struct mlx4_ib_steering *ib_steering;
1206
1207 mutex_lock(&mqp->mutex);
1208 list_for_each_entry(ib_steering, &mqp->steering_rules, list) {
1209 if (!memcmp(ib_steering->gid.raw, gid->raw, 16)) {
1210 list_del(&ib_steering->list);
1211 break;
1212 }
1213 }
1214 mutex_unlock(&mqp->mutex);
1215 if (&ib_steering->list == &mqp->steering_rules) {
1216 pr_err("Couldn't find reg_id for mgid. Steering rule is left attached\n");
1217 return -EINVAL;
1218 }
1219 reg_id = ib_steering->reg_id;
1220 free(ib_steering);
1221 }
1222
1223 err = mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
1224 MLX4_PROT_IB_IPV6, reg_id);
1225 if (err)
1226 return err;
1227
1228 mutex_lock(&mqp->mutex);
1229 ge = find_gid_entry(mqp, gid->raw);
1230 if (ge) {
1231 spin_lock(&mdev->iboe.lock);
1232 ndev = ge->added ? mdev->iboe.netdevs[ge->port - 1] : NULL;
1233 if (ndev)
1234 dev_hold(ndev);
1235 spin_unlock(&mdev->iboe.lock);
1236 rdma_get_mcast_mac((struct in6_addr *)gid, mac);
1237 if (ndev) {
1238 rtnl_lock();
1239 dev_mc_delete(mdev->iboe.netdevs[ge->port - 1], mac, 6, 0);
1240 rtnl_unlock();
1241 dev_put(ndev);
1242 }
1243 list_del(&ge->list);
1244 free(ge);
1245 } else
1246 pr_warn("could not find mgid entry\n");
1247
1248 mutex_unlock(&mqp->mutex);
1249
1250 return 0;
1251 }
1252 */
1253static int init_node_data(struct mlx4_ib_dev *dev) {
1254	struct ib_smp *in_mad = NULL;
1255	struct ib_smp *out_mad = NULL;
1256	int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
1257	int err = -ENOMEM;
1258
1259	__be32 *aux;
1260
1261	in_mad = calloc(1, sizeof *in_mad);
1262	out_mad = calloc(1, sizeof *out_mad);
1263	if (!in_mad || !out_mad)
1264		goto out;
1265
1266	init_query_mad(in_mad);
1267	in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
1268	if (mlx4_is_master(dev->dev))
1269		mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
1270
1271	err = mlx4_MAD_IFC(dev, mad_ifc_flags, 1, NULL, NULL, in_mad, out_mad);
1272	if (err)
1273		goto out;
1274
1275	memcpy(dev->ib_dev.node_desc, out_mad->data, 64);
1276
1277	in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
1278
1279	err = mlx4_MAD_IFC(dev, mad_ifc_flags, 1, NULL, NULL, in_mad, out_mad);
1280	if (err)
1281		goto out;
1282
1283	aux = (__be32 *) (out_mad->data + 32);
1284	dev->dev->rev_id = be32_to_cpup(aux);
1285	memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
1286
1287	out: free(in_mad);
1288	free(out_mad);
1289	return err;
1290}
1291/*
1292 static ssize_t show_hca(struct device *device, struct device_attribute *attr,
1293 char *buf)
1294 {
1295 struct mlx4_ib_dev *dev =
1296 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
1297 return sprintf(buf, "MT%d\n", dev->dev->pdev->device);
1298 }
1299
1300 static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
1301 char *buf)
1302 {
1303 struct mlx4_ib_dev *dev =
1304 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
1305 return sprintf(buf, "%d.%d.%d\n", (int) (dev->dev->caps.fw_ver >> 32),
1306 (int) (dev->dev->caps.fw_ver >> 16) & 0xffff,
1307 (int) dev->dev->caps.fw_ver & 0xffff);
1308 }
1309
1310 static ssize_t show_rev(struct device *device, struct device_attribute *attr,
1311 char *buf)
1312 {
1313 struct mlx4_ib_dev *dev =
1314 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
1315 return sprintf(buf, "%x\n", dev->dev->rev_id);
1316 }
1317
1318 static ssize_t show_board(struct device *device, struct device_attribute *attr,
1319 char *buf)
1320 {
1321 struct mlx4_ib_dev *dev =
1322 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
1323 return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN,
1324 dev->dev->board_id);
1325 }
1326
1327 static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
1328 static DEVICE_ATTR(fw_ver,   S_IRUGO, show_fw_ver, NULL);
1329 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
1330 static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
1331
1332 static struct device_attribute *mlx4_class_attributes[] = {
1333 &dev_attr_hw_rev,
1334 &dev_attr_fw_ver,
1335 &dev_attr_hca_type,
1336 &dev_attr_board_id
1337 };
1338
1339 static void mlx4_addrconf_ifid_eui48(u8 *eui, u16 vlan_id, struct net_device *dev)
1340 {
1341 #ifdef __linux__
1342 memcpy(eui, dev->dev_addr, 3);
1343 memcpy(eui + 5, dev->dev_addr + 3, 3);
1344 #else
1345 memcpy(eui, IF_LLADDR(dev), 3);
1346 memcpy(eui + 5, IF_LLADDR(dev) + 3, 3);
1347 #endif
1348 if (vlan_id < 0x1000) {
1349 eui[3] = vlan_id >> 8;
1350 eui[4] = vlan_id & 0xff;
1351 } else {
1352 eui[3] = 0xff;
1353 eui[4] = 0xfe;
1354 }
1355 eui[0] ^= 2;
1356 }
1357
1358 static void update_gids_task(struct work_struct *work)
1359 {
1360 struct update_gid_work *gw = container_of(work, struct update_gid_work, work);
1361 struct mlx4_cmd_mailbox *mailbox;
1362 union ib_gid *gids;
1363 int err;
1364 struct mlx4_dev	*dev = gw->dev->dev;
1365
1366 mailbox = mlx4_alloc_cmd_mailbox(dev);
1367 if (IS_ERR(mailbox)) {
1368 pr_warn("update gid table failed %ld\n", PTR_ERR(mailbox));
1369 return;
1370 }
1371
1372 gids = mailbox->buf;
1373 memcpy(gids, gw->gids, sizeof gw->gids);
1374
1375 err = mlx4_cmd(dev, mailbox->dma, MLX4_SET_PORT_GID_TABLE << 8 | gw->port,
1376 1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
1377 MLX4_CMD_WRAPPED);
1378 if (err)
1379 pr_warn("set port command failed\n");
1380 else {
1381 memcpy(gw->dev->iboe.gid_table[gw->port - 1], gw->gids, sizeof gw->gids);
1382 mlx4_ib_dispatch_event(gw->dev, gw->port, IB_EVENT_GID_CHANGE);
1383 }
1384
1385 mlx4_free_cmd_mailbox(dev, mailbox);
1386 free(gw);
1387 }
1388
1389 static int update_ipv6_gids(struct mlx4_ib_dev *dev, int port, int clear)
1390 {
1391 struct net_device *ndev = dev->iboe.netdevs[port - 1];
1392 struct update_gid_work *work;
1393 struct net_device *tmp;
1394 int i;
1395 u8 *hits;
1396 union ib_gid gid;
1397 int index_free;
1398 int found;
1399 int need_update = 0;
1400 int max_gids;
1401 u16 vid;
1402
1403 work = calloc(1,sizeof *work);
1404 if (!work)
1405 return -ENOMEM;
1406
1407 hits = calloc(1,128);
1408 if (!hits) {
1409 free(work);
1410 return -ENOMEM;
1411 }
1412
1413 max_gids = dev->dev->caps.gid_table_len[port];
1414
1415 #ifdef __linux__
1416 rcu_read_lock();
1417 for_each_netdev_rcu(&init_net, tmp) {
1418 #else
1419 IFNET_RLOCK();
1420 TAILQ_FOREACH(tmp, &V_ifnet, if_link) {
1421 #endif
1422 if (ndev && (tmp == ndev || rdma_vlan_dev_real_dev(tmp) == ndev)) {
1423 gid.global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
1424 vid = rdma_vlan_dev_vlan_id(tmp);
1425 mlx4_addrconf_ifid_eui48(&gid.raw[8], vid, ndev);
1426 found = 0;
1427 index_free = -1;
1428 for (i = 0; i < max_gids; ++i) {
1429 if (index_free < 0 &&
1430 !memcmp(&dev->iboe.gid_table[port - 1][i], &zgid, sizeof zgid))
1431 index_free = i;
1432 if (!memcmp(&dev->iboe.gid_table[port - 1][i], &gid, sizeof gid)) {
1433 hits[i] = 1;
1434 found = 1;
1435 break;
1436 }
1437 }
1438
1439 if (!found) {
1440 if (tmp == ndev &&
1441 (memcmp(&dev->iboe.gid_table[port - 1][0],
1442 &gid, sizeof gid) ||
1443 !memcmp(&dev->iboe.gid_table[port - 1][0],
1444 &zgid, sizeof gid))) {
1445 dev->iboe.gid_table[port - 1][0] = gid;
1446 ++need_update;
1447 hits[0] = 1;
1448 } else if (index_free >= 0) {
1449 dev->iboe.gid_table[port - 1][index_free] = gid;
1450 hits[index_free] = 1;
1451 ++need_update;
1452 }
1453 }
1454 }
1455 #ifdef __linux__
1456 }
1457 rcu_read_unlock();
1458 #else
1459 }
1460 IFNET_RUNLOCK();
1461 #endif
1462
1463 for (i = 0; i < max_gids; ++i)
1464 if (!hits[i]) {
1465 if (memcmp(&dev->iboe.gid_table[port - 1][i], &zgid, sizeof zgid))
1466 ++need_update;
1467 dev->iboe.gid_table[port - 1][i] = zgid;
1468 }
1469
1470 if (need_update) {
1471 memcpy(work->gids, dev->iboe.gid_table[port - 1], sizeof work->gids);
1472 INIT_WORK(&work->work, update_gids_task);
1473 work->port = port;
1474 work->dev = dev;
1475 queue_work(wq, &work->work);
1476 } else
1477 free(work);
1478
1479 free(hits);
1480 return 0;
1481 }
1482
1483 static void handle_en_event(struct mlx4_ib_dev *dev, int port, unsigned long event)
1484 {
1485 switch (event) {
1486 case NETDEV_UP:
1487 #ifdef __linux__
1488 case NETDEV_CHANGEADDR:
1489 #endif
1490 update_ipv6_gids(dev, port, 0);
1491 break;
1492
1493 case NETDEV_DOWN:
1494 update_ipv6_gids(dev, port, 1);
1495 dev->iboe.netdevs[port - 1] = NULL;
1496 }
1497 }
1498
1499 static void netdev_added(struct mlx4_ib_dev *dev, int port)
1500 {
1501 update_ipv6_gids(dev, port, 0);
1502 }
1503
1504 static void netdev_removed(struct mlx4_ib_dev *dev, int port)
1505 {
1506 update_ipv6_gids(dev, port, 1);
1507 }
1508
1509 static int mlx4_ib_netdev_event(struct notifier_block *this, unsigned long event,
1510 void *ptr)
1511 {
1512 struct net_device *dev = ptr;
1513 struct mlx4_ib_dev *ibdev;
1514 struct net_device *oldnd;
1515 struct mlx4_ib_iboe *iboe;
1516 int port;
1517
1518 #ifdef __linux__
1519 if (!net_eq(dev_net(dev), &init_net))
1520 return NOTIFY_DONE;
1521 #endif
1522
1523 ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb);
1524 iboe = &ibdev->iboe;
1525
1526 spin_lock(&iboe->lock);
1527 mlx4_foreach_ib_transport_port(port, ibdev->dev) {
1528 oldnd = iboe->netdevs[port - 1];
1529 iboe->netdevs[port - 1] =
1530 mlx4_get_protocol_dev(ibdev->dev, MLX4_PROT_ETH, port);
1531 if (oldnd != iboe->netdevs[port - 1]) {
1532 if (iboe->netdevs[port - 1])
1533 netdev_added(ibdev, port);
1534 else
1535 netdev_removed(ibdev, port);
1536 }
1537 }
1538
1539 if (dev == iboe->netdevs[0] ||
1540 (iboe->netdevs[0] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[0]))
1541 handle_en_event(ibdev, 1, event);
1542 else if (dev == iboe->netdevs[1]
1543 || (iboe->netdevs[1] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[1]))
1544 handle_en_event(ibdev, 2, event);
1545
1546 spin_unlock(&iboe->lock);
1547
1548 return NOTIFY_DONE;
1549 }
1550
1551 static void init_pkeys(struct mlx4_ib_dev *ibdev)
1552 {
1553 int port;
1554 int slave;
1555 int i;
1556
1557 if (mlx4_is_master(ibdev->dev)) {
1558 for (slave = 0; slave <= ibdev->dev->num_vfs; ++slave) {
1559 for (port = 1; port <= ibdev->dev->caps.num_ports; ++port) {
1560 for (i = 0;
1561 i < ibdev->dev->phys_caps.pkey_phys_table_len[port];
1562 ++i) {
1563 ibdev->pkeys.virt2phys_pkey[slave][port - 1][i] =
1564 master has the identity virt2phys pkey mapping
1565 (slave == mlx4_master_func_num(ibdev->dev) || !i) ? i :
1566 ibdev->dev->phys_caps.pkey_phys_table_len[port] - 1;
1567 mlx4_sync_pkey_table(ibdev->dev, slave, port, i,
1568 ibdev->pkeys.virt2phys_pkey[slave][port - 1][i]);
1569 }
1570 }
1571 }
1572 initialize pkey cache
1573 for (port = 1; port <= ibdev->dev->caps.num_ports; ++port) {
1574 for (i = 0;
1575 i < ibdev->dev->phys_caps.pkey_phys_table_len[port];
1576 ++i)
1577 ibdev->pkeys.phys_pkey_cache[port-1][i] =
1578 (i) ? 0 : 0xFFFF;
1579 }
1580 }
1581 }
1582 */
1583static void mlx4_ib_alloc_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev) {
1584	/*char name[32];*/
1585	int eq_per_port = 0;
1586	int added_eqs = 0;
1587	int total_eqs = 0;
1588	int i, j, eq;
1589
1590	/*Legacy mode or comp_pool is not large enough*/
1591	if (dev->caps.comp_pool == 0 || dev->caps.num_ports > dev->caps.comp_pool)
1592		return;
1593
1594	eq_per_port = rounddown_pow_of_two(
1595			dev->caps.comp_pool / dev->caps.num_ports);
1596
1597	/*Init eq table*/
1598	added_eqs = 0;
1599	mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
1600		added_eqs += eq_per_port;
1601
1602	total_eqs = dev->caps.num_comp_vectors + added_eqs;
1603
1604	ibdev->eq_table = calloc(total_eqs, sizeof(int));
1605	if (!ibdev->eq_table)
1606		return;
1607
1608	ibdev->eq_added = added_eqs;
1609
1610	eq = 0;
1611	mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
1612	{
1613		for (j = 0; j < eq_per_port; j++) {
1614			/*snprintf(name, sizeof(name), "mlx4-ib-%d-%d@%d:%d:%d:%d", i, j,
1615			 pci_get_domain(dev->pdev->dev.bsddev),
1616			 pci_get_bus(dev->pdev->dev.bsddev),
1617			 PCI_SLOT(dev->pdev->devfn), PCI_FUNC(dev->pdev->devfn));*/
1618
1619			/*Set IRQ for specific name (per ring)*/
1620			//if (mlx4_assign_eq(dev, name, &ibdev->eq_table[eq])) {
1621			/*Use legacy (same as mlx4_en driver)*/
1622			//pr_warn("Can't allocate EQ %d; reverting to legacy\n", eq);
1623			ibdev->eq_table[eq] = (eq % dev->caps.num_comp_vectors);
1624			//}
1625			eq++;
1626		}
1627	}
1628
1629	/*Fill the reset of the vector with legacy EQ*/
1630	for (i = 0, eq = added_eqs; i < dev->caps.num_comp_vectors; i++)
1631		ibdev->eq_table[eq++] = i;
1632
1633	/*Advertise the new number of EQs to clients*/
1634	ibdev->ib_dev.num_comp_vectors = total_eqs;
1635}
1636/*
1637 static void mlx4_ib_free_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev)
1638 {
1639 int i;
1640
1641 no additional eqs were added
1642 if (!ibdev->eq_table)
1643 return;
1644
1645 Reset the advertised EQ number
1646 ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;
1647
1648 Free only the added eqs
1649 for (i = 0; i < ibdev->eq_added; i++) {
1650 Don't free legacy eqs if used
1651 if (ibdev->eq_table[i] <= dev->caps.num_comp_vectors)
1652 continue;
1653 mlx4_release_eq(dev, ibdev->eq_table[i]);
1654 }
1655
1656 free(ibdev->eq_table);
1657 }
1658
1659
1660 * create show function and a device_attribute struct pointing to
1661 * the function for _name
1662
1663 #define DEVICE_DIAG_RPRT_ATTR(_name, _offset, _op_mod)		\
1664static ssize_t show_rprt_##_name(struct device *dev,		\
1665				 struct device_attribute *attr,	\
1666				 char *buf){			\
1667	return show_diag_rprt(dev, buf, _offset, _op_mod);	\
1668}								\
1669static DEVICE_ATTR(_name, S_IRUGO, show_rprt_##_name, NULL);
1670
1671 #define MLX4_DIAG_RPRT_CLEAR_DIAGS 3
1672
1673 static size_t show_diag_rprt(struct device *device, char *buf,
1674 u32 offset, u8 op_modifier)
1675 {
1676 size_t ret;
1677 u32 counter_offset = offset;
1678 u32 diag_counter = 0;
1679 struct mlx4_ib_dev *dev = container_of(device, struct mlx4_ib_dev,
1680 ib_dev.dev);
1681
1682 ret = mlx4_query_diag_counters(dev->dev, 1, op_modifier,
1683 &counter_offset, &diag_counter);
1684 if (ret)
1685 return ret;
1686
1687 return sprintf(buf, "%d\n", diag_counter);
1688 }
1689
1690 static ssize_t clear_diag_counters(struct device *device,
1691 struct device_attribute *attr,
1692 const char *buf, size_t length)
1693 {
1694 size_t ret;
1695 struct mlx4_ib_dev *dev = container_of(device, struct mlx4_ib_dev,
1696 ib_dev.dev);
1697
1698 ret = mlx4_query_diag_counters(dev->dev, 0, MLX4_DIAG_RPRT_CLEAR_DIAGS,
1699 NULL, NULL);
1700 if (ret)
1701 return ret;
1702
1703 return length;
1704 }
1705
1706 DEVICE_DIAG_RPRT_ATTR(rq_num_lle	, 0x00, 2);
1707 DEVICE_DIAG_RPRT_ATTR(sq_num_lle	, 0x04, 2);
1708 DEVICE_DIAG_RPRT_ATTR(rq_num_lqpoe	, 0x08, 2);
1709 DEVICE_DIAG_RPRT_ATTR(sq_num_lqpoe 	, 0x0C, 2);
1710 DEVICE_DIAG_RPRT_ATTR(rq_num_lpe	, 0x18, 2);
1711 DEVICE_DIAG_RPRT_ATTR(sq_num_lpe	, 0x1C, 2);
1712 DEVICE_DIAG_RPRT_ATTR(rq_num_wrfe	, 0x20, 2);
1713 DEVICE_DIAG_RPRT_ATTR(sq_num_wrfe	, 0x24, 2);
1714 DEVICE_DIAG_RPRT_ATTR(sq_num_mwbe	, 0x2C, 2);
1715 DEVICE_DIAG_RPRT_ATTR(sq_num_bre	, 0x34, 2);
1716 DEVICE_DIAG_RPRT_ATTR(rq_num_lae	, 0x38, 2);
1717 DEVICE_DIAG_RPRT_ATTR(sq_num_rire	, 0x44, 2);
1718 DEVICE_DIAG_RPRT_ATTR(rq_num_rire	, 0x48, 2);
1719 DEVICE_DIAG_RPRT_ATTR(sq_num_rae	, 0x4C, 2);
1720 DEVICE_DIAG_RPRT_ATTR(rq_num_rae	, 0x50, 2);
1721 DEVICE_DIAG_RPRT_ATTR(sq_num_roe	, 0x54, 2);
1722 DEVICE_DIAG_RPRT_ATTR(sq_num_tree	, 0x5C, 2);
1723 DEVICE_DIAG_RPRT_ATTR(sq_num_rree	, 0x64, 2);
1724 DEVICE_DIAG_RPRT_ATTR(rq_num_rnr	, 0x68, 2);
1725 DEVICE_DIAG_RPRT_ATTR(sq_num_rnr	, 0x6C, 2);
1726 DEVICE_DIAG_RPRT_ATTR(rq_num_oos	, 0x100, 2);
1727 DEVICE_DIAG_RPRT_ATTR(sq_num_oos	, 0x104, 2);
1728 DEVICE_DIAG_RPRT_ATTR(rq_num_mce	, 0x108, 2);
1729 DEVICE_DIAG_RPRT_ATTR(rq_num_udsdprd	, 0x118, 2);
1730 DEVICE_DIAG_RPRT_ATTR(rq_num_ucsdprd	, 0x120, 2);
1731 DEVICE_DIAG_RPRT_ATTR(num_cqovf		, 0x1A0, 2);
1732 DEVICE_DIAG_RPRT_ATTR(num_eqovf		, 0x1A4, 2);
1733 DEVICE_DIAG_RPRT_ATTR(num_baddb		, 0x1A8, 2);
1734
1735 static DEVICE_ATTR(clear_diag, S_IWUSR, NULL, clear_diag_counters);
1736
1737 static struct attribute *diag_rprt_attrs[] = {
1738 &dev_attr_rq_num_lle.attr,
1739 &dev_attr_sq_num_lle.attr,
1740 &dev_attr_rq_num_lqpoe.attr,
1741 &dev_attr_sq_num_lqpoe.attr,
1742 &dev_attr_rq_num_lpe.attr,
1743 &dev_attr_sq_num_lpe.attr,
1744 &dev_attr_rq_num_wrfe.attr,
1745 &dev_attr_sq_num_wrfe.attr,
1746 &dev_attr_sq_num_mwbe.attr,
1747 &dev_attr_sq_num_bre.attr,
1748 &dev_attr_rq_num_lae.attr,
1749 &dev_attr_sq_num_rire.attr,
1750 &dev_attr_rq_num_rire.attr,
1751 &dev_attr_sq_num_rae.attr,
1752 &dev_attr_rq_num_rae.attr,
1753 &dev_attr_sq_num_roe.attr,
1754 &dev_attr_sq_num_tree.attr,
1755 &dev_attr_sq_num_rree.attr,
1756 &dev_attr_rq_num_rnr.attr,
1757 &dev_attr_sq_num_rnr.attr,
1758 &dev_attr_rq_num_oos.attr,
1759 &dev_attr_sq_num_oos.attr,
1760 &dev_attr_rq_num_mce.attr,
1761 &dev_attr_rq_num_udsdprd.attr,
1762 &dev_attr_rq_num_ucsdprd.attr,
1763 &dev_attr_num_cqovf.attr,
1764 &dev_attr_num_eqovf.attr,
1765 &dev_attr_num_baddb.attr,
1766 &dev_attr_clear_diag.attr,
1767 NULL
1768 };
1769
1770 static struct attribute_group diag_counters_group = {
1771 .name  = "diag_counters",
1772 .attrs  = diag_rprt_attrs
1773 };
1774
1775 #ifdef __linux__
1776 static int mlx4_ib_proc_init(void)
1777 {
1778 Creating procfs directories /proc/drivers/mlx4_ib/ &&
1779 /proc/drivers/mlx4_ib/mrs for further use by the driver.
1780
1781 int err;
1782
1783 mlx4_ib_driver_dir_entry = proc_mkdir(MLX4_IB_DRIVER_PROC_DIR_NAME,
1784 NULL);
1785 if (!mlx4_ib_driver_dir_entry) {
1786 pr_err("mlx4_ib_proc_init has failed for %s\n",
1787 MLX4_IB_DRIVER_PROC_DIR_NAME);
1788 err = -ENODEV;
1789 goto error;
1790 }
1791
1792 mlx4_mrs_dir_entry = proc_mkdir(MLX4_IB_MRS_PROC_DIR_NAME,
1793 mlx4_ib_driver_dir_entry);
1794 if (!mlx4_mrs_dir_entry) {
1795 pr_err("mlx4_ib_proc_init has failed for %s\n",
1796 MLX4_IB_MRS_PROC_DIR_NAME);
1797 err = -ENODEV;
1798 goto remove_entry;
1799 }
1800
1801 return 0;
1802
1803 remove_entry:
1804 remove_proc_entry(MLX4_IB_DRIVER_PROC_DIR_NAME,
1805 NULL);
1806 error:
1807 return err;
1808 }
1809 #endif
1810
1811 static void init_dev_assign(void)
1812 {
1813 int bus, slot, fn, ib_idx;
1814 char *p = dev_assign_str, *t;
1815 char curr_val[32] = {0};
1816 int ret;
1817 int j, i = 0;
1818
1819 memset(dr, 0, sizeof dr);
1820
1821 if (dev_assign_str[0] == 0)
1822 return;
1823
1824 while (strlen(p)) {
1825 ret = sscanf(p, "%02x:%02x.%x-%x", &bus, &slot, &fn, &ib_idx);
1826 if (ret != 4 || ib_idx < 0)
1827 goto err;
1828
1829 for (j = 0; j < i; j++)
1830 if (dr[j].nr == ib_idx)
1831 goto err;
1832
1833 dr[i].bus = bus;
1834 dr[i].dev = slot;
1835 dr[i].func = fn;
1836 dr[i].nr = ib_idx;
1837
1838 t = strchr(p, ',');
1839 sprintf(curr_val, "%02x:%02x.%x-%x", bus, slot, fn, ib_idx);
1840 if ((!t) && strlen(p) == strlen(curr_val))
1841 return;
1842
1843 if (!t || (t + 1) >= dev_assign_str + sizeof dev_assign_str)
1844 goto err;
1845
1846 ++i;
1847 if (i >= MAX_DR)
1848 goto err;
1849
1850 p = t + 1;
1851 }
1852
1853 return;
1854 err:
1855 memset(dr, 0, sizeof dr);
1856 printf(KERN_WARNING "mlx4_ib: The value of 'dev_assign_str' parameter "
1857 "is incorrect. The parameter value is discarded!");
1858 }
1859 */
1860
1861void *mlx4_ib_add(struct mlx4_dev *dev) {
1862	struct mlx4_ib_dev *ibdev;
1863	int num_ports = 0;
1864	int i/*, j*/;
1865	int err;
1866	struct mlx4_ib_iboe *iboe;
1867
1868	uint64_t current, ticks_per_msec;
1869
1870	/*printk(KERN_INFO "%s", mlx4_ib_version);*/
1871
1872	mlx4_foreach_ib_transport_port(i, dev)
1873		num_ports++;
1874
1875	/*No point in registering a device with no ports...*/
1876	if (num_ports == 0)
1877		return NULL;
1878
1879	ibdev = (struct mlx4_ib_dev *) ib_alloc_device(sizeof *ibdev);
1880	if (!ibdev) {
1881		MLX4_ERR("Device struct alloc failed\n");
1882		return NULL;
1883	}
1884
1885	iboe = &ibdev->iboe;
1886
1887	if (mlx4_pd_alloc(dev, &ibdev->priv_pdn))
1888		goto err_dealloc;
1889
1890	if (mlx4_uar_alloc(dev, &ibdev->priv_uar))
1891		goto err_pd;
1892
1893	ibdev->priv_uar.map = (void *) (ibdev->priv_uar.pfn << PAGE_SHIFT);
1894
1895	if (!ibdev->priv_uar.map)
1896		goto err_uar;
1897
1898	/*MLX4_INIT_DOORBELL_LOCK(&ibdev->uar_lock);*/
1899
1900	ibdev->dev = dev;
1901
1902	strlcpy(ibdev->ib_dev.name, "mlx4_%d", IB_DEVICE_NAME_MAX);
1903	/*ibdev->ib_dev.owner = THIS_MODULE;*/
1904	ibdev->ib_dev.node_type = RDMA_NODE_IB_CA;
1905	ibdev->ib_dev.local_dma_lkey = dev->caps.reserved_lkey;
1906	ibdev->num_ports = num_ports;
1907	ibdev->ib_dev.phys_port_cnt = ibdev->num_ports;
1908	ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;
1909	/*ibdev->ib_dev.dma_device = &dev->pdev->dev;*/
1910
1911	if (dev->caps.userspace_caps)
1912		ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_ABI_VERSION;
1913	else
1914		ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_NO_DEV_CAPS_ABI_VERSION;
1915
1916	ibdev->ib_dev.uverbs_cmd_mask = (1ull << IB_USER_VERBS_CMD_GET_CONTEXT)
1917			| (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)
1918			| (1ull << IB_USER_VERBS_CMD_QUERY_PORT)
1919			| (1ull << IB_USER_VERBS_CMD_ALLOC_PD)
1920			| (1ull << IB_USER_VERBS_CMD_DEALLOC_PD)
1921			| (1ull << IB_USER_VERBS_CMD_REG_MR)
1922			| (1ull << IB_USER_VERBS_CMD_DEREG_MR)
1923			| (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)
1924			| (1ull << IB_USER_VERBS_CMD_CREATE_CQ)
1925			| (1ull << IB_USER_VERBS_CMD_RESIZE_CQ)
1926			| (1ull << IB_USER_VERBS_CMD_DESTROY_CQ)
1927			| (1ull << IB_USER_VERBS_CMD_CREATE_QP)
1928			| (1ull << IB_USER_VERBS_CMD_MODIFY_QP)
1929			| (1ull << IB_USER_VERBS_CMD_QUERY_QP)
1930			| (1ull << IB_USER_VERBS_CMD_DESTROY_QP)
1931			| (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)
1932			| (1ull << IB_USER_VERBS_CMD_DETACH_MCAST)
1933			| (1ull << IB_USER_VERBS_CMD_CREATE_SRQ)
1934			| (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)
1935			| (1ull << IB_USER_VERBS_CMD_QUERY_SRQ)
1936			| (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)
1937			| (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)
1938			| (1ull << IB_USER_VERBS_CMD_OPEN_QP)
1939			| (1ull << IB_USER_VERBS_CMD_ATTACH_FLOW)
1940			| (1ull << IB_USER_VERBS_CMD_DETACH_FLOW)
1941			| (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ);
1942
1943	/*ibdev->ib_dev.query_device = mlx4_ib_query_device;
1944	 */
1945	ibdev->ib_dev.query_port = mlx4_ib_query_port;
1946	ibdev->ib_dev.get_link_layer = mlx4_ib_port_link_layer;
1947	ibdev->ib_dev.query_gid = mlx4_ib_query_gid;
1948	ibdev->ib_dev.query_pkey = mlx4_ib_query_pkey;
1949	/*
1950	 ibdev->ib_dev.modify_device = mlx4_ib_modify_device;
1951	 ibdev->ib_dev.modify_port = mlx4_ib_modify_port;
1952	 ibdev->ib_dev.alloc_ucontext = mlx4_ib_alloc_ucontext;
1953	 ibdev->ib_dev.dealloc_ucontext = mlx4_ib_dealloc_ucontext;
1954	 ibdev->ib_dev.mmap = mlx4_ib_mmap;
1955	 #ifdef __linux__
1956	 ibdev->ib_dev.get_unmapped_area = mlx4_ib_get_unmapped_area;
1957	 #endif
1958	 */
1959	ibdev->ib_dev.alloc_pd = mlx4_ib_alloc_pd;
1960	/*
1961	 ibdev->ib_dev.dealloc_pd = mlx4_ib_dealloc_pd;
1962	 */
1963	ibdev->ib_dev.create_ah = mlx4_ib_create_ah;
1964	/*
1965	 ibdev->ib_dev.query_ah = mlx4_ib_query_ah;
1966	 */
1967	ibdev->ib_dev.destroy_ah = mlx4_ib_destroy_ah;
1968	/*
1969	 ibdev->ib_dev.create_srq = mlx4_ib_create_srq;
1970	 ibdev->ib_dev.modify_srq = mlx4_ib_modify_srq;
1971	 ibdev->ib_dev.query_srq = mlx4_ib_query_srq;
1972	 ibdev->ib_dev.destroy_srq = mlx4_ib_destroy_srq;
1973	 ibdev->ib_dev.post_srq_recv = mlx4_ib_post_srq_recv;
1974	 */
1975	ibdev->ib_dev.create_qp = mlx4_ib_create_qp;
1976	ibdev->ib_dev.modify_qp = mlx4_ib_modify_qp;
1977	/*
1978	 ibdev->ib_dev.query_qp = mlx4_ib_query_qp;
1979	 ibdev->ib_dev.destroy_qp = mlx4_ib_destroy_qp;
1980	 */
1981	ibdev->ib_dev.post_send = mlx4_ib_post_send;
1982	ibdev->ib_dev.post_recv = mlx4_ib_post_recv;
1983	ibdev->ib_dev.create_cq = mlx4_ib_create_cq;
1984	/*
1985	 ibdev->ib_dev.modify_cq = mlx4_ib_modify_cq;
1986	 ibdev->ib_dev.resize_cq = mlx4_ib_resize_cq;
1987	 ibdev->ib_dev.destroy_cq = mlx4_ib_destroy_cq;
1988	 */
1989	ibdev->ib_dev.poll_cq = mlx4_ib_poll_cq;
1990	ibdev->ib_dev.req_notify_cq = mlx4_ib_arm_cq;
1991	ibdev->ib_dev.get_dma_mr = mlx4_ib_get_dma_mr;
1992	/*
1993	 ibdev->ib_dev.reg_user_mr = mlx4_ib_reg_user_mr;
1994	 ibdev->ib_dev.dereg_mr = mlx4_ib_dereg_mr;
1995	 ibdev->ib_dev.alloc_fast_reg_mr = mlx4_ib_alloc_fast_reg_mr;
1996	 ibdev->ib_dev.alloc_fast_reg_page_list = mlx4_ib_alloc_fast_reg_page_list;
1997	 ibdev->ib_dev.free_fast_reg_page_list = mlx4_ib_free_fast_reg_page_list;
1998	 ibdev->ib_dev.attach_mcast = mlx4_ib_mcg_attach;
1999	 ibdev->ib_dev.detach_mcast = mlx4_ib_mcg_detach;
2000	 ibdev->ib_dev.attach_flow = mlx4_ib_flow_attach;
2001	 ibdev->ib_dev.detach_flow = mlx4_ib_flow_detach;
2002	 */
2003	ibdev->ib_dev.process_mad = mlx4_ib_process_mad;
2004	/*
2005	 if (!mlx4_is_slave(ibdev->dev)) {
2006	 ibdev->ib_dev.alloc_fmr = mlx4_ib_fmr_alloc;
2007	 ibdev->ib_dev.map_phys_fmr = mlx4_ib_map_phys_fmr;
2008	 ibdev->ib_dev.unmap_fmr = mlx4_ib_unmap_fmr;
2009	 ibdev->ib_dev.dealloc_fmr = mlx4_ib_fmr_dealloc;
2010	 }
2011
2012	 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) {
2013	 ibdev->ib_dev.alloc_xrcd = mlx4_ib_alloc_xrcd;
2014	 ibdev->ib_dev.dealloc_xrcd = mlx4_ib_dealloc_xrcd;
2015	 ibdev->ib_dev.uverbs_cmd_mask |= (1ull << IB_USER_VERBS_CMD_OPEN_XRCD)
2016	 | (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
2017	 }*/
2018
2019	mlx4_ib_alloc_eqs(dev, ibdev);
2020
2021	/*spin_lock_init(&iboe->lock);*/
2022
2023	if (init_node_data(ibdev))
2024		goto err_map;
2025
2026	for (i = 0; i < ibdev->num_ports; ++i) {
2027		if (mlx4_ib_port_link_layer(&ibdev->ib_dev, i + 1)
2028				== IB_LINK_LAYER_ETHERNET) {
2029			err = mlx4_counter_alloc(ibdev->dev, i + 1, &ibdev->counters[i]);
2030			if (err)
2031				ibdev->counters[i] = -1;
2032		} else
2033			ibdev->counters[i] = -1;
2034	}
2035
2036	/*spin_lock_init(&ibdev->sm_lock);
2037	 mutex_init(&ibdev->cap_mask_mutex);*/
2038
2039	if (dev->caps.steering_mode == MLX4_STEERING_MODE_DEVICE_MANAGED
2040			&& !mlx4_is_slave(dev)) {
2041		ibdev->steer_qpn_count = MLX4_IB_UC_MAX_NUM_QPS;
2042		err = mlx4_qp_reserve_range(dev, ibdev->steer_qpn_count,
2043		MLX4_IB_UC_STEER_QPN_ALIGN, &ibdev->steer_qpn_base, 0);
2044		if (err)
2045			goto err_counter;
2046
2047		ibdev->ib_uc_qpns_bitmap = malloc(
2048		BITS_TO_LONGS(ibdev->steer_qpn_count) * sizeof(long));
2049		if (!ibdev->ib_uc_qpns_bitmap) {
2050			MLX4_ERR("bit map alloc failed\n");
2051			goto err_steer_qp_release;
2052		}
2053
2054		bitmap_zero(ibdev->ib_uc_qpns_bitmap, ibdev->steer_qpn_count);
2055
2056		err = mlx4_FLOW_STEERING_IB_UC_QP_RANGE(dev, ibdev->steer_qpn_base,
2057				ibdev->steer_qpn_base + ibdev->steer_qpn_count - 1);
2058		if (err)
2059			goto err_steer_free_bitmap;
2060	}
2061
2062	/*if (ib_register_device(&ibdev->ib_dev, NULL))
2063	 goto err_steer_free_bitmap;*/
2064
2065	/*XXX:replace ib_register_device with direct calls*/
2066	//ib_cache_setup_one(&ibdev->ib_dev); is not necesary
2067	ib_mad_init_device(&ibdev->ib_dev); //SI_ORDER_FIRST
2068
2069	/*if (mlx4_ib_mad_init(ibdev))
2070	 goto err_reg;
2071
2072	 if (mlx4_ib_init_sriov(ibdev))
2073	 goto err_mad;
2074
2075	 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE && !iboe->nb.notifier_call) {
2076	 iboe->nb.notifier_call = mlx4_ib_netdev_event;
2077	 err = register_netdevice_notifier(&iboe->nb);
2078	 if (err)
2079	 goto err_sriov;
2080	 }
2081
2082	 for (j = 0; j < ARRAY_SIZE(mlx4_class_attributes); ++j) {
2083	 if (device_create_file(&ibdev->ib_dev.dev, mlx4_class_attributes[j]))
2084	 goto err_notif;
2085	 }
2086	 if (sysfs_create_group(&ibdev->ib_dev.dev.kobj, &diag_counters_group))
2087	 goto err_notif;
2088	 */
2089	ibdev->ib_active = true;
2090	/*
2091	 if (mlx4_is_mfunc(ibdev->dev))
2092	 init_pkeys(ibdev);*/
2093
2094	/*create paravirt contexts for any VFs which are active*/
2095	/*if (mlx4_is_master(ibdev->dev)) {
2096	 for (j = 0; j < MLX4_MFUNC_MAX; j++) {
2097	 if (j == mlx4_master_func_num(ibdev->dev))
2098	 continue;
2099	 if (mlx4_is_slave_active(ibdev->dev, j))
2100	 do_slave_init(ibdev, j, 1);
2101	 }
2102	 }*/
2103
2104	got_up_irq = false;
2105	current = rdtsc();
2106	sys_debug_get_tsc_per_ms(&ticks_per_msec);
2107	while (!got_up_irq) {
2108		if (rdtsc() >= current + 100 * 1000 * ticks_per_msec) {
2109			break;
2110		}
2111		event_dispatch(get_default_waitset());
2112	}
2113
2114	//barrelfish_usleep(5 * 1000 * 1000);
2115	test_ib(&ibdev->ib_dev);
2116
2117	return ibdev;
2118
2119	/*err_notif:*//*if (unregister_netdevice_notifier(&ibdev->iboe.nb))
2120	 pr_warn("failure unregistering notifier\n");
2121	 flush_workqueue(wq);*/
2122
2123	/*err_sriov:*//* mlx4_ib_close_sriov(ibdev);*/
2124
2125	/*err_mad:*//*mlx4_ib_mad_cleanup(ibdev);*/
2126
2127	/*err_reg:*//*ib_unregister_device(&ibdev->ib_dev);*/
2128
2129	err_steer_free_bitmap: /* free(ibdev->ib_uc_qpns_bitmap);*/
2130
2131	err_steer_qp_release: /*if (dev->caps.steering_mode
2132	 == MLX4_STEERING_MODE_DEVICE_MANAGED)
2133	 mlx4_qp_release_range(dev, ibdev->steer_qpn_base,
2134	 ibdev->steer_qpn_count);*/
2135	err_counter: /*for (; i; --i)
2136	 if (ibdev->counters[i - 1] != -1)
2137	 mlx4_counter_free(ibdev->dev, i, ibdev->counters[i - 1]);*/
2138
2139	err_map: /*iounmap(ibdev->priv_uar.map);
2140	 mlx4_ib_free_eqs(dev, ibdev);*/
2141
2142	err_uar: /*mlx4_uar_free(dev, &ibdev->priv_uar);*/
2143
2144	err_pd: /*mlx4_pd_free(dev, ibdev->priv_pdn);*/
2145
2146	err_dealloc: /*ib_dealloc_device(&ibdev->ib_dev);*/
2147
2148	return NULL;
2149}
2150
2151int mlx4_ib_steer_qp_alloc(struct mlx4_ib_dev *dev, int count, int *qpn) {
2152	int offset;
2153
2154	assert(!!dev->ib_uc_qpns_bitmap);
2155
2156	offset = bitmap_find_free_region(dev->ib_uc_qpns_bitmap,
2157			dev->steer_qpn_count, get_count_order(count));
2158	if (offset < 0)
2159		return offset;
2160
2161	*qpn = dev->steer_qpn_base + offset;
2162	return 0;
2163}
2164/*
2165 void mlx4_ib_steer_qp_free(struct mlx4_ib_dev *dev, u32 qpn, int count)
2166 {
2167 if (!qpn ||
2168 dev->dev->caps.steering_mode != MLX4_STEERING_MODE_DEVICE_MANAGED)
2169 return;
2170
2171 BUG_ON(qpn < dev->steer_qpn_base);
2172
2173 bitmap_release_region(dev->ib_uc_qpns_bitmap,
2174 qpn - dev->steer_qpn_base, get_count_order(count));
2175 }
2176 */
2177int mlx4_ib_steer_qp_reg(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
2178		int is_attach) {
2179	struct ib_flow_spec spec = { .type = IB_FLOW_IB_UC, .l2_id.ib_uc.qpn =
2180			mqp->ibqp.qp_num, };
2181
2182	return is_attach ?
2183			__mlx4_ib_flow_attach(mdev, mqp, &spec, MLX4_DOMAIN_NIC, 0) :
2184			__mlx4_ib_flow_detach(mdev, mqp, &spec, MLX4_DOMAIN_NIC, 0);
2185}
2186/*
2187 static void mlx4_ib_remove(struct mlx4_dev *dev, void *ibdev_ptr)
2188 {
2189 struct mlx4_ib_dev *ibdev = ibdev_ptr;
2190 int p,j;
2191
2192 mlx4_ib_close_sriov(ibdev);
2193 sysfs_remove_group(&ibdev->ib_dev.dev.kobj, &diag_counters_group);
2194 mlx4_ib_mad_cleanup(ibdev);
2195
2196 for (j = 0; j < ARRAY_SIZE(mlx4_class_attributes); ++j) {
2197 device_remove_file(&ibdev->ib_dev.dev, mlx4_class_attributes[j]);
2198 }
2199
2200 ib_unregister_device(&ibdev->ib_dev);
2201
2202 if (dev->caps.steering_mode == MLX4_STEERING_MODE_DEVICE_MANAGED) {
2203 mlx4_qp_release_range(dev, ibdev->steer_qpn_base,
2204 ibdev->steer_qpn_count);
2205 free(ibdev->ib_uc_qpns_bitmap);
2206 }
2207
2208 if (ibdev->iboe.nb.notifier_call) {
2209 if (unregister_netdevice_notifier(&ibdev->iboe.nb))
2210 pr_warn("failure unregistering notifier\n");
2211 ibdev->iboe.nb.notifier_call = NULL;
2212 }
2213 iounmap(ibdev->priv_uar.map);
2214 for (p = 0; p < ibdev->num_ports; ++p)
2215 if (ibdev->counters[p] != -1)
2216 mlx4_counter_free(ibdev->dev, p + 1, ibdev->counters[p]);
2217 mlx4_foreach_port(p, dev, MLX4_PORT_TYPE_IB)
2218 mlx4_CLOSE_PORT(dev, p);
2219
2220 mlx4_ib_free_eqs(dev, ibdev);
2221
2222 mlx4_uar_free(dev, &ibdev->priv_uar);
2223 mlx4_pd_free(dev, ibdev->priv_pdn);
2224 ib_dealloc_device(&ibdev->ib_dev);
2225 }
2226
2227 static void do_slave_init(struct mlx4_ib_dev *ibdev, int slave, int do_init)
2228 {
2229 struct mlx4_ib_demux_work **dm = NULL;
2230 struct mlx4_dev *dev = ibdev->dev;
2231 int i;
2232 unsigned long flags;
2233
2234 if (!mlx4_is_master(dev))
2235 return;
2236
2237 dm = calloc(dev->caps.num_ports, sizeof *dm);
2238 if (!dm) {
2239 pr_err("failed to allocate memory for tunneling qp update\n");
2240 goto out;
2241 }
2242
2243 for (i = 0; i < dev->caps.num_ports; i++) {
2244 dm[i] = malloc(sizeof (struct mlx4_ib_demux_work));
2245 if (!dm[i]) {
2246 pr_err("failed to allocate memory for tunneling qp update work struct\n");
2247 for (i = 0; i < dev->caps.num_ports; i++) {
2248 if (dm[i])
2249 free(dm[i]);
2250 }
2251 goto out;
2252 }
2253 }
2254 initialize or tear down tunnel QPs for the slave
2255 for (i = 0; i < dev->caps.num_ports; i++) {
2256 INIT_WORK(&dm[i]->work, mlx4_ib_tunnels_update_work);
2257 dm[i]->port = i + 1;
2258 dm[i]->slave = slave;
2259 dm[i]->do_init = do_init;
2260 dm[i]->dev = ibdev;
2261 spin_lock_irqsave(&ibdev->sriov.going_down_lock, flags);
2262 if (!ibdev->sriov.is_going_down)
2263 queue_work(ibdev->sriov.demux[i].ud_wq, &dm[i]->work);
2264 spin_unlock_irqrestore(&ibdev->sriov.going_down_lock, flags);
2265 }
2266 out:
2267 if (dm)
2268 free(dm);
2269 return;
2270 }
2271
2272 static void mlx4_ib_event(struct mlx4_dev *dev, void *ibdev_ptr,
2273 enum mlx4_dev_event event, unsigned long param)
2274 {
2275 struct ib_event ibev;
2276 struct mlx4_ib_dev *ibdev = to_mdev((struct ib_device *) ibdev_ptr);
2277 struct mlx4_eqe *eqe = NULL;
2278 struct ib_event_work *ew;
2279 int p = 0;
2280
2281 if (event == MLX4_DEV_EVENT_PORT_MGMT_CHANGE)
2282 eqe = (struct mlx4_eqe *)param;
2283 else
2284 p = (int) param;
2285
2286 switch (event) {
2287 case MLX4_DEV_EVENT_PORT_UP:
2288 if (p > ibdev->num_ports)
2289 return;
2290 if (mlx4_is_master(dev) &&
2291 rdma_port_get_link_layer(&ibdev->ib_dev, p) ==
2292 IB_LINK_LAYER_INFINIBAND) {
2293 mlx4_ib_invalidate_all_guid_record(ibdev, p);
2294 }
2295 mlx4_ib_info((struct ib_device *) ibdev_ptr,
2296 "Port %d logical link is up\n", p);
2297 ibev.event = IB_EVENT_PORT_ACTIVE;
2298 break;
2299
2300 case MLX4_DEV_EVENT_PORT_DOWN:
2301 if (p > ibdev->num_ports)
2302 return;
2303 mlx4_ib_info((struct ib_device *) ibdev_ptr,
2304 "Port %d logical link is down\n", p);
2305 ibev.event = IB_EVENT_PORT_ERR;
2306 break;
2307
2308 case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
2309 ibdev->ib_active = false;
2310 ibev.event = IB_EVENT_DEVICE_FATAL;
2311 break;
2312
2313 case MLX4_DEV_EVENT_PORT_MGMT_CHANGE:
2314 ew = malloc(sizeof *ew);
2315 if (!ew) {
2316 pr_err("failed to allocate memory for events work\n");
2317 break;
2318 }
2319
2320 INIT_WORK(&ew->work, handle_port_mgmt_change_event);
2321 memcpy(&ew->ib_eqe, eqe, sizeof *eqe);
2322 ew->ib_dev = ibdev;
2323 need to queue only for port owner, which uses GEN_EQE
2324 if (mlx4_is_master(dev))
2325 queue_work(wq, &ew->work);
2326 else
2327 handle_port_mgmt_change_event(&ew->work);
2328 return;
2329
2330 case MLX4_DEV_EVENT_SLAVE_INIT:
2331 here, p is the slave id
2332 do_slave_init(ibdev, p, 1);
2333 return;
2334
2335 case MLX4_DEV_EVENT_SLAVE_SHUTDOWN:
2336 here, p is the slave id
2337 do_slave_init(ibdev, p, 0);
2338 return;
2339
2340 default:
2341 return;
2342 }
2343
2344 ibev.device	      = ibdev_ptr;
2345 ibev.element.port_num = (u8) p;
2346
2347 ib_dispatch_event(&ibev);
2348 }
2349
2350 static struct mlx4_interface mlx4_ib_interface = {
2351 .add		= mlx4_ib_add,
2352 .remove		= mlx4_ib_remove,
2353 .event		= mlx4_ib_event,
2354 .protocol	= MLX4_PROT_IB_IPV6
2355 };
2356
2357 static int __init mlx4_ib_init(void)
2358 {
2359 int err;
2360
2361 wq = create_singlethread_workqueue("mlx4_ib");
2362 if (!wq)
2363 return -ENOMEM;
2364
2365 #ifdef __linux__
2366 err = mlx4_ib_proc_init();
2367 if (err)
2368 goto clean_wq;
2369 #endif
2370
2371 err = mlx4_ib_mcg_init();
2372 if (err)
2373 goto clean_proc;
2374
2375 init_dev_assign();
2376
2377 err = mlx4_register_interface(&mlx4_ib_interface);
2378 if (err)
2379 goto clean_mcg;
2380
2381 return 0;
2382
2383 clean_mcg:
2384 mlx4_ib_mcg_destroy();
2385
2386 clean_proc:
2387 #ifdef __linux__
2388 remove_proc_entry(MLX4_IB_MRS_PROC_DIR_NAME,
2389 mlx4_ib_driver_dir_entry);
2390 remove_proc_entry(MLX4_IB_DRIVER_PROC_DIR_NAME, NULL);
2391
2392 clean_wq:
2393 #endif
2394 destroy_workqueue(wq);
2395 return err;
2396 }
2397
2398 static void __exit mlx4_ib_cleanup(void)
2399 {
2400 mlx4_unregister_interface(&mlx4_ib_interface);
2401 mlx4_ib_mcg_destroy();
2402 destroy_workqueue(wq);
2403
2404 Remove proc entries
2405 #ifdef __linux__
2406 remove_proc_entry(MLX4_IB_MRS_PROC_DIR_NAME,
2407 mlx4_ib_driver_dir_entry);
2408 remove_proc_entry(MLX4_IB_DRIVER_PROC_DIR_NAME, NULL);
2409 #endif
2410
2411 }
2412
2413 module_init_order(mlx4_ib_init, SI_ORDER_MIDDLE);
2414 module_exit(mlx4_ib_cleanup);
2415
2416 #undef MODULE_VERSION
2417 #include <sys/module.h>
2418 static int
2419 mlx4ib_evhand(module_t mod, int event, void *arg)
2420 {
2421 return (0);
2422 }
2423
2424 static moduledata_t mlx4ib_mod = {
2425 .name = "mlx4ib",
2426 .evhand = mlx4ib_evhand,
2427 };
2428
2429 DECLARE_MODULE(mlx4ib, mlx4ib_mod, SI_SUB_OFED_PREINIT, SI_ORDER_ANY);
2430 MODULE_DEPEND(mlx4ib, mlx4, 1, 1, 1);
2431 MODULE_DEPEND(mlx4ib, ibcore, 1, 1, 1);
2432 */
2433