main.c revision a5e12dff757b562bbecd6a2359fdc4c43d4d97de
1/*
2 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses.  You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 *     Redistribution and use in source and binary forms, with or
12 *     without modification, are permitted provided that the following
13 *     conditions are met:
14 *
15 *      - Redistributions of source code must retain the above
16 *        copyright notice, this list of conditions and the following
17 *        disclaimer.
18 *
19 *      - Redistributions in binary form must reproduce the above
20 *        copyright notice, this list of conditions and the following
21 *        disclaimer in the documentation and/or other materials
22 *        provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34#include <linux/module.h>
35#include <linux/init.h>
36#include <linux/slab.h>
37#include <linux/errno.h>
38#include <linux/netdevice.h>
39#include <linux/inetdevice.h>
40#include <linux/rtnetlink.h>
41#include <linux/if_vlan.h>
42
43#include <rdma/ib_smi.h>
44#include <rdma/ib_user_verbs.h>
45#include <rdma/ib_addr.h>
46
47#include <linux/mlx4/driver.h>
48#include <linux/mlx4/cmd.h>
49
50#include "mlx4_ib.h"
51#include "user.h"
52
53#define DRV_NAME	"mlx4_ib"
54#define DRV_VERSION	"1.0"
55#define DRV_RELDATE	"April 4, 2008"
56
57MODULE_AUTHOR("Roland Dreier");
58MODULE_DESCRIPTION("Mellanox ConnectX HCA InfiniBand driver");
59MODULE_LICENSE("Dual BSD/GPL");
60MODULE_VERSION(DRV_VERSION);
61
62static const char mlx4_ib_version[] =
63	DRV_NAME ": Mellanox ConnectX InfiniBand driver v"
64	DRV_VERSION " (" DRV_RELDATE ")\n";
65
66struct update_gid_work {
67	struct work_struct	work;
68	union ib_gid		gids[128];
69	struct mlx4_ib_dev     *dev;
70	int			port;
71};
72
73static struct workqueue_struct *wq;
74
75static void init_query_mad(struct ib_smp *mad)
76{
77	mad->base_version  = 1;
78	mad->mgmt_class    = IB_MGMT_CLASS_SUBN_LID_ROUTED;
79	mad->class_version = 1;
80	mad->method	   = IB_MGMT_METHOD_GET;
81}
82
83static union ib_gid zgid;
84
85static int mlx4_ib_query_device(struct ib_device *ibdev,
86				struct ib_device_attr *props)
87{
88	struct mlx4_ib_dev *dev = to_mdev(ibdev);
89	struct ib_smp *in_mad  = NULL;
90	struct ib_smp *out_mad = NULL;
91	int err = -ENOMEM;
92
93	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
94	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
95	if (!in_mad || !out_mad)
96		goto out;
97
98	init_query_mad(in_mad);
99	in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
100
101	err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, 1, NULL, NULL, in_mad, out_mad);
102	if (err)
103		goto out;
104
105	memset(props, 0, sizeof *props);
106
107	props->fw_ver = dev->dev->caps.fw_ver;
108	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
109		IB_DEVICE_PORT_ACTIVE_EVENT		|
110		IB_DEVICE_SYS_IMAGE_GUID		|
111		IB_DEVICE_RC_RNR_NAK_GEN		|
112		IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
113	if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_PKEY_CNTR)
114		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
115	if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_QKEY_CNTR)
116		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
117	if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_APM)
118		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
119	if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UD_AV_PORT)
120		props->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
121	if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_IPOIB_CSUM)
122		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
123	if (dev->dev->caps.max_gso_sz && dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BLH)
124		props->device_cap_flags |= IB_DEVICE_UD_TSO;
125	if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_RESERVED_LKEY)
126		props->device_cap_flags |= IB_DEVICE_LOCAL_DMA_LKEY;
127	if ((dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_LOCAL_INV) &&
128	    (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_REMOTE_INV) &&
129	    (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_FAST_REG_WR))
130		props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
131
132	props->vendor_id	   = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
133		0xffffff;
134	props->vendor_part_id	   = be16_to_cpup((__be16 *) (out_mad->data + 30));
135	props->hw_ver		   = be32_to_cpup((__be32 *) (out_mad->data + 32));
136	memcpy(&props->sys_image_guid, out_mad->data +	4, 8);
137
138	props->max_mr_size	   = ~0ull;
139	props->page_size_cap	   = dev->dev->caps.page_size_cap;
140	props->max_qp		   = dev->dev->caps.num_qps - dev->dev->caps.reserved_qps;
141	props->max_qp_wr	   = dev->dev->caps.max_wqes;
142	props->max_sge		   = min(dev->dev->caps.max_sq_sg,
143					 dev->dev->caps.max_rq_sg);
144	props->max_cq		   = dev->dev->caps.num_cqs - dev->dev->caps.reserved_cqs;
145	props->max_cqe		   = dev->dev->caps.max_cqes;
146	props->max_mr		   = dev->dev->caps.num_mpts - dev->dev->caps.reserved_mrws;
147	props->max_pd		   = dev->dev->caps.num_pds - dev->dev->caps.reserved_pds;
148	props->max_qp_rd_atom	   = dev->dev->caps.max_qp_dest_rdma;
149	props->max_qp_init_rd_atom = dev->dev->caps.max_qp_init_rdma;
150	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
151	props->max_srq		   = dev->dev->caps.num_srqs - dev->dev->caps.reserved_srqs;
152	props->max_srq_wr	   = dev->dev->caps.max_srq_wqes - 1;
153	props->max_srq_sge	   = dev->dev->caps.max_srq_sge;
154	props->max_fast_reg_page_list_len = MLX4_MAX_FAST_REG_PAGES;
155	props->local_ca_ack_delay  = dev->dev->caps.local_ca_ack_delay;
156	props->atomic_cap	   = dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_ATOMIC ?
157		IB_ATOMIC_HCA : IB_ATOMIC_NONE;
158	props->masked_atomic_cap   = IB_ATOMIC_HCA;
159	props->max_pkeys	   = dev->dev->caps.pkey_table_len[1];
160	props->max_mcast_grp	   = dev->dev->caps.num_mgms + dev->dev->caps.num_amgms;
161	props->max_mcast_qp_attach = dev->dev->caps.num_qp_per_mgm;
162	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
163					   props->max_mcast_grp;
164	props->max_map_per_fmr = (1 << (32 - ilog2(dev->dev->caps.num_mpts))) - 1;
165
166out:
167	kfree(in_mad);
168	kfree(out_mad);
169
170	return err;
171}
172
173static enum rdma_link_layer
174mlx4_ib_port_link_layer(struct ib_device *device, u8 port_num)
175{
176	struct mlx4_dev *dev = to_mdev(device)->dev;
177
178	return dev->caps.port_mask & (1 << (port_num - 1)) ?
179		IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
180}
181
182static int ib_link_query_port(struct ib_device *ibdev, u8 port,
183			      struct ib_port_attr *props,
184			      struct ib_smp *in_mad,
185			      struct ib_smp *out_mad)
186{
187	int ext_active_speed;
188	int err;
189
190	props->lid		= be16_to_cpup((__be16 *) (out_mad->data + 16));
191	props->lmc		= out_mad->data[34] & 0x7;
192	props->sm_lid		= be16_to_cpup((__be16 *) (out_mad->data + 18));
193	props->sm_sl		= out_mad->data[36] & 0xf;
194	props->state		= out_mad->data[32] & 0xf;
195	props->phys_state	= out_mad->data[33] >> 4;
196	props->port_cap_flags	= be32_to_cpup((__be32 *) (out_mad->data + 20));
197	props->gid_tbl_len	= to_mdev(ibdev)->dev->caps.gid_table_len[port];
198	props->max_msg_sz	= to_mdev(ibdev)->dev->caps.max_msg_sz;
199	props->pkey_tbl_len	= to_mdev(ibdev)->dev->caps.pkey_table_len[port];
200	props->bad_pkey_cntr	= be16_to_cpup((__be16 *) (out_mad->data + 46));
201	props->qkey_viol_cntr	= be16_to_cpup((__be16 *) (out_mad->data + 48));
202	props->active_width	= out_mad->data[31] & 0xf;
203	props->active_speed	= out_mad->data[35] >> 4;
204	props->max_mtu		= out_mad->data[41] & 0xf;
205	props->active_mtu	= out_mad->data[36] >> 4;
206	props->subnet_timeout	= out_mad->data[51] & 0x1f;
207	props->max_vl_num	= out_mad->data[37] >> 4;
208	props->init_type_reply	= out_mad->data[41] >> 4;
209
210	/* Check if extended speeds (EDR/FDR/...) are supported */
211	if (props->port_cap_flags & IB_PORT_EXTENDED_SPEEDS_SUP) {
212		ext_active_speed = out_mad->data[62] >> 4;
213
214		switch (ext_active_speed) {
215		case 1:
216			props->active_speed = 16; /* FDR */
217			break;
218		case 2:
219			props->active_speed = 32; /* EDR */
220			break;
221		}
222	}
223
224	/* If reported active speed is QDR, check if is FDR-10 */
225	if (props->active_speed == 4) {
226		if (to_mdev(ibdev)->dev->caps.ext_port_cap[port] &
227		    MLX_EXT_PORT_CAP_FLAG_EXTENDED_PORT_INFO) {
228			init_query_mad(in_mad);
229			in_mad->attr_id = MLX4_ATTR_EXTENDED_PORT_INFO;
230			in_mad->attr_mod = cpu_to_be32(port);
231
232			err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port,
233					   NULL, NULL, in_mad, out_mad);
234			if (err)
235				return err;
236
237			/* Checking LinkSpeedActive for FDR-10 */
238			if (out_mad->data[15] & 0x1)
239				props->active_speed = 8;
240		}
241	}
242
243	return 0;
244}
245
246static u8 state_to_phys_state(enum ib_port_state state)
247{
248	return state == IB_PORT_ACTIVE ? 5 : 3;
249}
250
251static int eth_link_query_port(struct ib_device *ibdev, u8 port,
252			       struct ib_port_attr *props,
253			       struct ib_smp *out_mad)
254{
255	struct mlx4_ib_iboe *iboe = &to_mdev(ibdev)->iboe;
256	struct net_device *ndev;
257	enum ib_mtu tmp;
258
259	props->active_width	= IB_WIDTH_1X;
260	props->active_speed	= 4;
261	props->port_cap_flags	= IB_PORT_CM_SUP;
262	props->gid_tbl_len	= to_mdev(ibdev)->dev->caps.gid_table_len[port];
263	props->max_msg_sz	= to_mdev(ibdev)->dev->caps.max_msg_sz;
264	props->pkey_tbl_len	= 1;
265	props->bad_pkey_cntr	= be16_to_cpup((__be16 *) (out_mad->data + 46));
266	props->qkey_viol_cntr	= be16_to_cpup((__be16 *) (out_mad->data + 48));
267	props->max_mtu		= IB_MTU_2048;
268	props->subnet_timeout	= 0;
269	props->max_vl_num	= out_mad->data[37] >> 4;
270	props->init_type_reply	= 0;
271	props->state		= IB_PORT_DOWN;
272	props->phys_state	= state_to_phys_state(props->state);
273	props->active_mtu	= IB_MTU_256;
274	spin_lock(&iboe->lock);
275	ndev = iboe->netdevs[port - 1];
276	if (!ndev)
277		goto out;
278
279	tmp = iboe_get_mtu(ndev->mtu);
280	props->active_mtu = tmp ? min(props->max_mtu, tmp) : IB_MTU_256;
281
282	props->state		= (netif_running(ndev) && netif_carrier_ok(ndev)) ?
283					IB_PORT_ACTIVE : IB_PORT_DOWN;
284	props->phys_state	= state_to_phys_state(props->state);
285
286out:
287	spin_unlock(&iboe->lock);
288	return 0;
289}
290
291static int mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
292			      struct ib_port_attr *props)
293{
294	struct ib_smp *in_mad  = NULL;
295	struct ib_smp *out_mad = NULL;
296	int err = -ENOMEM;
297
298	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
299	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
300	if (!in_mad || !out_mad)
301		goto out;
302
303	memset(props, 0, sizeof *props);
304
305	init_query_mad(in_mad);
306	in_mad->attr_id  = IB_SMP_ATTR_PORT_INFO;
307	in_mad->attr_mod = cpu_to_be32(port);
308
309	err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
310	if (err)
311		goto out;
312
313	err = mlx4_ib_port_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND ?
314		ib_link_query_port(ibdev, port, props, in_mad, out_mad) :
315		eth_link_query_port(ibdev, port, props, out_mad);
316
317out:
318	kfree(in_mad);
319	kfree(out_mad);
320
321	return err;
322}
323
324static int __mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
325			       union ib_gid *gid)
326{
327	struct ib_smp *in_mad  = NULL;
328	struct ib_smp *out_mad = NULL;
329	int err = -ENOMEM;
330
331	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
332	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
333	if (!in_mad || !out_mad)
334		goto out;
335
336	init_query_mad(in_mad);
337	in_mad->attr_id  = IB_SMP_ATTR_PORT_INFO;
338	in_mad->attr_mod = cpu_to_be32(port);
339
340	err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
341	if (err)
342		goto out;
343
344	memcpy(gid->raw, out_mad->data + 8, 8);
345
346	init_query_mad(in_mad);
347	in_mad->attr_id  = IB_SMP_ATTR_GUID_INFO;
348	in_mad->attr_mod = cpu_to_be32(index / 8);
349
350	err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
351	if (err)
352		goto out;
353
354	memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
355
356out:
357	kfree(in_mad);
358	kfree(out_mad);
359	return err;
360}
361
362static int iboe_query_gid(struct ib_device *ibdev, u8 port, int index,
363			  union ib_gid *gid)
364{
365	struct mlx4_ib_dev *dev = to_mdev(ibdev);
366
367	*gid = dev->iboe.gid_table[port - 1][index];
368
369	return 0;
370}
371
372static int mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
373			     union ib_gid *gid)
374{
375	if (rdma_port_get_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND)
376		return __mlx4_ib_query_gid(ibdev, port, index, gid);
377	else
378		return iboe_query_gid(ibdev, port, index, gid);
379}
380
381static int mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
382			      u16 *pkey)
383{
384	struct ib_smp *in_mad  = NULL;
385	struct ib_smp *out_mad = NULL;
386	int err = -ENOMEM;
387
388	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
389	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
390	if (!in_mad || !out_mad)
391		goto out;
392
393	init_query_mad(in_mad);
394	in_mad->attr_id  = IB_SMP_ATTR_PKEY_TABLE;
395	in_mad->attr_mod = cpu_to_be32(index / 32);
396
397	err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
398	if (err)
399		goto out;
400
401	*pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
402
403out:
404	kfree(in_mad);
405	kfree(out_mad);
406	return err;
407}
408
409static int mlx4_ib_modify_device(struct ib_device *ibdev, int mask,
410				 struct ib_device_modify *props)
411{
412	struct mlx4_cmd_mailbox *mailbox;
413
414	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
415		return -EOPNOTSUPP;
416
417	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
418		return 0;
419
420	spin_lock(&to_mdev(ibdev)->sm_lock);
421	memcpy(ibdev->node_desc, props->node_desc, 64);
422	spin_unlock(&to_mdev(ibdev)->sm_lock);
423
424	/*
425	 * If possible, pass node desc to FW, so it can generate
426	 * a 144 trap.  If cmd fails, just ignore.
427	 */
428	mailbox = mlx4_alloc_cmd_mailbox(to_mdev(ibdev)->dev);
429	if (IS_ERR(mailbox))
430		return 0;
431
432	memset(mailbox->buf, 0, 256);
433	memcpy(mailbox->buf, props->node_desc, 64);
434	mlx4_cmd(to_mdev(ibdev)->dev, mailbox->dma, 1, 0,
435		 MLX4_CMD_SET_NODE, MLX4_CMD_TIME_CLASS_A);
436
437	mlx4_free_cmd_mailbox(to_mdev(ibdev)->dev, mailbox);
438
439	return 0;
440}
441
442static int mlx4_SET_PORT(struct mlx4_ib_dev *dev, u8 port, int reset_qkey_viols,
443			 u32 cap_mask)
444{
445	struct mlx4_cmd_mailbox *mailbox;
446	int err;
447	u8 is_eth = dev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
448
449	mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
450	if (IS_ERR(mailbox))
451		return PTR_ERR(mailbox);
452
453	memset(mailbox->buf, 0, 256);
454
455	if (dev->dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
456		*(u8 *) mailbox->buf	     = !!reset_qkey_viols << 6;
457		((__be32 *) mailbox->buf)[2] = cpu_to_be32(cap_mask);
458	} else {
459		((u8 *) mailbox->buf)[3]     = !!reset_qkey_viols;
460		((__be32 *) mailbox->buf)[1] = cpu_to_be32(cap_mask);
461	}
462
463	err = mlx4_cmd(dev->dev, mailbox->dma, port, is_eth, MLX4_CMD_SET_PORT,
464		       MLX4_CMD_TIME_CLASS_B);
465
466	mlx4_free_cmd_mailbox(dev->dev, mailbox);
467	return err;
468}
469
470static int mlx4_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
471			       struct ib_port_modify *props)
472{
473	struct ib_port_attr attr;
474	u32 cap_mask;
475	int err;
476
477	mutex_lock(&to_mdev(ibdev)->cap_mask_mutex);
478
479	err = mlx4_ib_query_port(ibdev, port, &attr);
480	if (err)
481		goto out;
482
483	cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
484		~props->clr_port_cap_mask;
485
486	err = mlx4_SET_PORT(to_mdev(ibdev), port,
487			    !!(mask & IB_PORT_RESET_QKEY_CNTR),
488			    cap_mask);
489
490out:
491	mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
492	return err;
493}
494
495static struct ib_ucontext *mlx4_ib_alloc_ucontext(struct ib_device *ibdev,
496						  struct ib_udata *udata)
497{
498	struct mlx4_ib_dev *dev = to_mdev(ibdev);
499	struct mlx4_ib_ucontext *context;
500	struct mlx4_ib_alloc_ucontext_resp resp;
501	int err;
502
503	if (!dev->ib_active)
504		return ERR_PTR(-EAGAIN);
505
506	resp.qp_tab_size      = dev->dev->caps.num_qps;
507	resp.bf_reg_size      = dev->dev->caps.bf_reg_size;
508	resp.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
509
510	context = kmalloc(sizeof *context, GFP_KERNEL);
511	if (!context)
512		return ERR_PTR(-ENOMEM);
513
514	err = mlx4_uar_alloc(to_mdev(ibdev)->dev, &context->uar);
515	if (err) {
516		kfree(context);
517		return ERR_PTR(err);
518	}
519
520	INIT_LIST_HEAD(&context->db_page_list);
521	mutex_init(&context->db_page_mutex);
522
523	err = ib_copy_to_udata(udata, &resp, sizeof resp);
524	if (err) {
525		mlx4_uar_free(to_mdev(ibdev)->dev, &context->uar);
526		kfree(context);
527		return ERR_PTR(-EFAULT);
528	}
529
530	return &context->ibucontext;
531}
532
533static int mlx4_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
534{
535	struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
536
537	mlx4_uar_free(to_mdev(ibcontext->device)->dev, &context->uar);
538	kfree(context);
539
540	return 0;
541}
542
543static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
544{
545	struct mlx4_ib_dev *dev = to_mdev(context->device);
546
547	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
548		return -EINVAL;
549
550	if (vma->vm_pgoff == 0) {
551		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
552
553		if (io_remap_pfn_range(vma, vma->vm_start,
554				       to_mucontext(context)->uar.pfn,
555				       PAGE_SIZE, vma->vm_page_prot))
556			return -EAGAIN;
557	} else if (vma->vm_pgoff == 1 && dev->dev->caps.bf_reg_size != 0) {
558		vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
559
560		if (io_remap_pfn_range(vma, vma->vm_start,
561				       to_mucontext(context)->uar.pfn +
562				       dev->dev->caps.num_uars,
563				       PAGE_SIZE, vma->vm_page_prot))
564			return -EAGAIN;
565	} else
566		return -EINVAL;
567
568	return 0;
569}
570
571static struct ib_pd *mlx4_ib_alloc_pd(struct ib_device *ibdev,
572				      struct ib_ucontext *context,
573				      struct ib_udata *udata)
574{
575	struct mlx4_ib_pd *pd;
576	int err;
577
578	pd = kmalloc(sizeof *pd, GFP_KERNEL);
579	if (!pd)
580		return ERR_PTR(-ENOMEM);
581
582	err = mlx4_pd_alloc(to_mdev(ibdev)->dev, &pd->pdn);
583	if (err) {
584		kfree(pd);
585		return ERR_PTR(err);
586	}
587
588	if (context)
589		if (ib_copy_to_udata(udata, &pd->pdn, sizeof (__u32))) {
590			mlx4_pd_free(to_mdev(ibdev)->dev, pd->pdn);
591			kfree(pd);
592			return ERR_PTR(-EFAULT);
593		}
594
595	return &pd->ibpd;
596}
597
598static int mlx4_ib_dealloc_pd(struct ib_pd *pd)
599{
600	mlx4_pd_free(to_mdev(pd->device)->dev, to_mpd(pd)->pdn);
601	kfree(pd);
602
603	return 0;
604}
605
606static int add_gid_entry(struct ib_qp *ibqp, union ib_gid *gid)
607{
608	struct mlx4_ib_qp *mqp = to_mqp(ibqp);
609	struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
610	struct mlx4_ib_gid_entry *ge;
611
612	ge = kzalloc(sizeof *ge, GFP_KERNEL);
613	if (!ge)
614		return -ENOMEM;
615
616	ge->gid = *gid;
617	if (mlx4_ib_add_mc(mdev, mqp, gid)) {
618		ge->port = mqp->port;
619		ge->added = 1;
620	}
621
622	mutex_lock(&mqp->mutex);
623	list_add_tail(&ge->list, &mqp->gid_list);
624	mutex_unlock(&mqp->mutex);
625
626	return 0;
627}
628
629int mlx4_ib_add_mc(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
630		   union ib_gid *gid)
631{
632	u8 mac[6];
633	struct net_device *ndev;
634	int ret = 0;
635
636	if (!mqp->port)
637		return 0;
638
639	spin_lock(&mdev->iboe.lock);
640	ndev = mdev->iboe.netdevs[mqp->port - 1];
641	if (ndev)
642		dev_hold(ndev);
643	spin_unlock(&mdev->iboe.lock);
644
645	if (ndev) {
646		rdma_get_mcast_mac((struct in6_addr *)gid, mac);
647		rtnl_lock();
648		dev_mc_add(mdev->iboe.netdevs[mqp->port - 1], mac);
649		ret = 1;
650		rtnl_unlock();
651		dev_put(ndev);
652	}
653
654	return ret;
655}
656
657static int mlx4_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
658{
659	int err;
660	struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
661	struct mlx4_ib_qp *mqp = to_mqp(ibqp);
662
663	err = mlx4_multicast_attach(mdev->dev, &mqp->mqp, gid->raw,
664				    !!(mqp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK),
665				    MLX4_PROT_IB_IPV6);
666	if (err)
667		return err;
668
669	err = add_gid_entry(ibqp, gid);
670	if (err)
671		goto err_add;
672
673	return 0;
674
675err_add:
676	mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw, MLX4_PROT_IB_IPV6);
677	return err;
678}
679
680static struct mlx4_ib_gid_entry *find_gid_entry(struct mlx4_ib_qp *qp, u8 *raw)
681{
682	struct mlx4_ib_gid_entry *ge;
683	struct mlx4_ib_gid_entry *tmp;
684	struct mlx4_ib_gid_entry *ret = NULL;
685
686	list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
687		if (!memcmp(raw, ge->gid.raw, 16)) {
688			ret = ge;
689			break;
690		}
691	}
692
693	return ret;
694}
695
696static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
697{
698	int err;
699	struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
700	struct mlx4_ib_qp *mqp = to_mqp(ibqp);
701	u8 mac[6];
702	struct net_device *ndev;
703	struct mlx4_ib_gid_entry *ge;
704
705	err = mlx4_multicast_detach(mdev->dev,
706				    &mqp->mqp, gid->raw, MLX4_PROT_IB_IPV6);
707	if (err)
708		return err;
709
710	mutex_lock(&mqp->mutex);
711	ge = find_gid_entry(mqp, gid->raw);
712	if (ge) {
713		spin_lock(&mdev->iboe.lock);
714		ndev = ge->added ? mdev->iboe.netdevs[ge->port - 1] : NULL;
715		if (ndev)
716			dev_hold(ndev);
717		spin_unlock(&mdev->iboe.lock);
718		rdma_get_mcast_mac((struct in6_addr *)gid, mac);
719		if (ndev) {
720			rtnl_lock();
721			dev_mc_del(mdev->iboe.netdevs[ge->port - 1], mac);
722			rtnl_unlock();
723			dev_put(ndev);
724		}
725		list_del(&ge->list);
726		kfree(ge);
727	} else
728		printk(KERN_WARNING "could not find mgid entry\n");
729
730	mutex_unlock(&mqp->mutex);
731
732	return 0;
733}
734
735static int init_node_data(struct mlx4_ib_dev *dev)
736{
737	struct ib_smp *in_mad  = NULL;
738	struct ib_smp *out_mad = NULL;
739	int err = -ENOMEM;
740
741	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
742	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
743	if (!in_mad || !out_mad)
744		goto out;
745
746	init_query_mad(in_mad);
747	in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
748
749	err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
750	if (err)
751		goto out;
752
753	memcpy(dev->ib_dev.node_desc, out_mad->data, 64);
754
755	in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
756
757	err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
758	if (err)
759		goto out;
760
761	memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
762
763out:
764	kfree(in_mad);
765	kfree(out_mad);
766	return err;
767}
768
769static ssize_t show_hca(struct device *device, struct device_attribute *attr,
770			char *buf)
771{
772	struct mlx4_ib_dev *dev =
773		container_of(device, struct mlx4_ib_dev, ib_dev.dev);
774	return sprintf(buf, "MT%d\n", dev->dev->pdev->device);
775}
776
777static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
778			   char *buf)
779{
780	struct mlx4_ib_dev *dev =
781		container_of(device, struct mlx4_ib_dev, ib_dev.dev);
782	return sprintf(buf, "%d.%d.%d\n", (int) (dev->dev->caps.fw_ver >> 32),
783		       (int) (dev->dev->caps.fw_ver >> 16) & 0xffff,
784		       (int) dev->dev->caps.fw_ver & 0xffff);
785}
786
787static ssize_t show_rev(struct device *device, struct device_attribute *attr,
788			char *buf)
789{
790	struct mlx4_ib_dev *dev =
791		container_of(device, struct mlx4_ib_dev, ib_dev.dev);
792	return sprintf(buf, "%x\n", dev->dev->rev_id);
793}
794
795static ssize_t show_board(struct device *device, struct device_attribute *attr,
796			  char *buf)
797{
798	struct mlx4_ib_dev *dev =
799		container_of(device, struct mlx4_ib_dev, ib_dev.dev);
800	return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN,
801		       dev->dev->board_id);
802}
803
804static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
805static DEVICE_ATTR(fw_ver,   S_IRUGO, show_fw_ver, NULL);
806static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
807static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
808
809static struct device_attribute *mlx4_class_attributes[] = {
810	&dev_attr_hw_rev,
811	&dev_attr_fw_ver,
812	&dev_attr_hca_type,
813	&dev_attr_board_id
814};
815
816static void mlx4_addrconf_ifid_eui48(u8 *eui, u16 vlan_id, struct net_device *dev)
817{
818	memcpy(eui, dev->dev_addr, 3);
819	memcpy(eui + 5, dev->dev_addr + 3, 3);
820	if (vlan_id < 0x1000) {
821		eui[3] = vlan_id >> 8;
822		eui[4] = vlan_id & 0xff;
823	} else {
824		eui[3] = 0xff;
825		eui[4] = 0xfe;
826	}
827	eui[0] ^= 2;
828}
829
830static void update_gids_task(struct work_struct *work)
831{
832	struct update_gid_work *gw = container_of(work, struct update_gid_work, work);
833	struct mlx4_cmd_mailbox *mailbox;
834	union ib_gid *gids;
835	int err;
836	struct mlx4_dev	*dev = gw->dev->dev;
837	struct ib_event event;
838
839	mailbox = mlx4_alloc_cmd_mailbox(dev);
840	if (IS_ERR(mailbox)) {
841		printk(KERN_WARNING "update gid table failed %ld\n", PTR_ERR(mailbox));
842		return;
843	}
844
845	gids = mailbox->buf;
846	memcpy(gids, gw->gids, sizeof gw->gids);
847
848	err = mlx4_cmd(dev, mailbox->dma, MLX4_SET_PORT_GID_TABLE << 8 | gw->port,
849		       1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B);
850	if (err)
851		printk(KERN_WARNING "set port command failed\n");
852	else {
853		memcpy(gw->dev->iboe.gid_table[gw->port - 1], gw->gids, sizeof gw->gids);
854		event.device = &gw->dev->ib_dev;
855		event.element.port_num = gw->port;
856		event.event    = IB_EVENT_GID_CHANGE;
857		ib_dispatch_event(&event);
858	}
859
860	mlx4_free_cmd_mailbox(dev, mailbox);
861	kfree(gw);
862}
863
864static int update_ipv6_gids(struct mlx4_ib_dev *dev, int port, int clear)
865{
866	struct net_device *ndev = dev->iboe.netdevs[port - 1];
867	struct update_gid_work *work;
868	struct net_device *tmp;
869	int i;
870	u8 *hits;
871	int ret;
872	union ib_gid gid;
873	int free;
874	int found;
875	int need_update = 0;
876	u16 vid;
877
878	work = kzalloc(sizeof *work, GFP_ATOMIC);
879	if (!work)
880		return -ENOMEM;
881
882	hits = kzalloc(128, GFP_ATOMIC);
883	if (!hits) {
884		ret = -ENOMEM;
885		goto out;
886	}
887
888	rcu_read_lock();
889	for_each_netdev_rcu(&init_net, tmp) {
890		if (ndev && (tmp == ndev || rdma_vlan_dev_real_dev(tmp) == ndev)) {
891			gid.global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
892			vid = rdma_vlan_dev_vlan_id(tmp);
893			mlx4_addrconf_ifid_eui48(&gid.raw[8], vid, ndev);
894			found = 0;
895			free = -1;
896			for (i = 0; i < 128; ++i) {
897				if (free < 0 &&
898				    !memcmp(&dev->iboe.gid_table[port - 1][i], &zgid, sizeof zgid))
899					free = i;
900				if (!memcmp(&dev->iboe.gid_table[port - 1][i], &gid, sizeof gid)) {
901					hits[i] = 1;
902					found = 1;
903					break;
904				}
905			}
906
907			if (!found) {
908				if (tmp == ndev &&
909				    (memcmp(&dev->iboe.gid_table[port - 1][0],
910					    &gid, sizeof gid) ||
911				     !memcmp(&dev->iboe.gid_table[port - 1][0],
912					     &zgid, sizeof gid))) {
913					dev->iboe.gid_table[port - 1][0] = gid;
914					++need_update;
915					hits[0] = 1;
916				} else if (free >= 0) {
917					dev->iboe.gid_table[port - 1][free] = gid;
918					hits[free] = 1;
919					++need_update;
920				}
921			}
922		}
923	}
924	rcu_read_unlock();
925
926	for (i = 0; i < 128; ++i)
927		if (!hits[i]) {
928			if (memcmp(&dev->iboe.gid_table[port - 1][i], &zgid, sizeof zgid))
929				++need_update;
930			dev->iboe.gid_table[port - 1][i] = zgid;
931		}
932
933	if (need_update) {
934		memcpy(work->gids, dev->iboe.gid_table[port - 1], sizeof work->gids);
935		INIT_WORK(&work->work, update_gids_task);
936		work->port = port;
937		work->dev = dev;
938		queue_work(wq, &work->work);
939	} else
940		kfree(work);
941
942	kfree(hits);
943	return 0;
944
945out:
946	kfree(work);
947	return ret;
948}
949
950static void handle_en_event(struct mlx4_ib_dev *dev, int port, unsigned long event)
951{
952	switch (event) {
953	case NETDEV_UP:
954	case NETDEV_CHANGEADDR:
955		update_ipv6_gids(dev, port, 0);
956		break;
957
958	case NETDEV_DOWN:
959		update_ipv6_gids(dev, port, 1);
960		dev->iboe.netdevs[port - 1] = NULL;
961	}
962}
963
964static void netdev_added(struct mlx4_ib_dev *dev, int port)
965{
966	update_ipv6_gids(dev, port, 0);
967}
968
969static void netdev_removed(struct mlx4_ib_dev *dev, int port)
970{
971	update_ipv6_gids(dev, port, 1);
972}
973
974static int mlx4_ib_netdev_event(struct notifier_block *this, unsigned long event,
975				void *ptr)
976{
977	struct net_device *dev = ptr;
978	struct mlx4_ib_dev *ibdev;
979	struct net_device *oldnd;
980	struct mlx4_ib_iboe *iboe;
981	int port;
982
983	if (!net_eq(dev_net(dev), &init_net))
984		return NOTIFY_DONE;
985
986	ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb);
987	iboe = &ibdev->iboe;
988
989	spin_lock(&iboe->lock);
990	mlx4_foreach_ib_transport_port(port, ibdev->dev) {
991		oldnd = iboe->netdevs[port - 1];
992		iboe->netdevs[port - 1] =
993			mlx4_get_protocol_dev(ibdev->dev, MLX4_PROT_ETH, port);
994		if (oldnd != iboe->netdevs[port - 1]) {
995			if (iboe->netdevs[port - 1])
996				netdev_added(ibdev, port);
997			else
998				netdev_removed(ibdev, port);
999		}
1000	}
1001
1002	if (dev == iboe->netdevs[0] ||
1003	    (iboe->netdevs[0] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[0]))
1004		handle_en_event(ibdev, 1, event);
1005	else if (dev == iboe->netdevs[1]
1006		 || (iboe->netdevs[1] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[1]))
1007		handle_en_event(ibdev, 2, event);
1008
1009	spin_unlock(&iboe->lock);
1010
1011	return NOTIFY_DONE;
1012}
1013
1014static void *mlx4_ib_add(struct mlx4_dev *dev)
1015{
1016	struct mlx4_ib_dev *ibdev;
1017	int num_ports = 0;
1018	int i;
1019	int err;
1020	struct mlx4_ib_iboe *iboe;
1021
1022	printk_once(KERN_INFO "%s", mlx4_ib_version);
1023
1024	mlx4_foreach_ib_transport_port(i, dev)
1025		num_ports++;
1026
1027	/* No point in registering a device with no ports... */
1028	if (num_ports == 0)
1029		return NULL;
1030
1031	ibdev = (struct mlx4_ib_dev *) ib_alloc_device(sizeof *ibdev);
1032	if (!ibdev) {
1033		dev_err(&dev->pdev->dev, "Device struct alloc failed\n");
1034		return NULL;
1035	}
1036
1037	iboe = &ibdev->iboe;
1038
1039	if (mlx4_pd_alloc(dev, &ibdev->priv_pdn))
1040		goto err_dealloc;
1041
1042	if (mlx4_uar_alloc(dev, &ibdev->priv_uar))
1043		goto err_pd;
1044
1045	ibdev->uar_map = ioremap((phys_addr_t) ibdev->priv_uar.pfn << PAGE_SHIFT,
1046				 PAGE_SIZE);
1047	if (!ibdev->uar_map)
1048		goto err_uar;
1049	MLX4_INIT_DOORBELL_LOCK(&ibdev->uar_lock);
1050
1051	ibdev->dev = dev;
1052
1053	strlcpy(ibdev->ib_dev.name, "mlx4_%d", IB_DEVICE_NAME_MAX);
1054	ibdev->ib_dev.owner		= THIS_MODULE;
1055	ibdev->ib_dev.node_type		= RDMA_NODE_IB_CA;
1056	ibdev->ib_dev.local_dma_lkey	= dev->caps.reserved_lkey;
1057	ibdev->num_ports		= num_ports;
1058	ibdev->ib_dev.phys_port_cnt     = ibdev->num_ports;
1059	ibdev->ib_dev.num_comp_vectors	= dev->caps.num_comp_vectors;
1060	ibdev->ib_dev.dma_device	= &dev->pdev->dev;
1061
1062	ibdev->ib_dev.uverbs_abi_ver	= MLX4_IB_UVERBS_ABI_VERSION;
1063	ibdev->ib_dev.uverbs_cmd_mask	=
1064		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
1065		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
1066		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
1067		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
1068		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
1069		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
1070		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
1071		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
1072		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
1073		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
1074		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
1075		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
1076		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
1077		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
1078		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
1079		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
1080		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
1081		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
1082		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
1083		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
1084		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ);
1085
1086	ibdev->ib_dev.query_device	= mlx4_ib_query_device;
1087	ibdev->ib_dev.query_port	= mlx4_ib_query_port;
1088	ibdev->ib_dev.get_link_layer	= mlx4_ib_port_link_layer;
1089	ibdev->ib_dev.query_gid		= mlx4_ib_query_gid;
1090	ibdev->ib_dev.query_pkey	= mlx4_ib_query_pkey;
1091	ibdev->ib_dev.modify_device	= mlx4_ib_modify_device;
1092	ibdev->ib_dev.modify_port	= mlx4_ib_modify_port;
1093	ibdev->ib_dev.alloc_ucontext	= mlx4_ib_alloc_ucontext;
1094	ibdev->ib_dev.dealloc_ucontext	= mlx4_ib_dealloc_ucontext;
1095	ibdev->ib_dev.mmap		= mlx4_ib_mmap;
1096	ibdev->ib_dev.alloc_pd		= mlx4_ib_alloc_pd;
1097	ibdev->ib_dev.dealloc_pd	= mlx4_ib_dealloc_pd;
1098	ibdev->ib_dev.create_ah		= mlx4_ib_create_ah;
1099	ibdev->ib_dev.query_ah		= mlx4_ib_query_ah;
1100	ibdev->ib_dev.destroy_ah	= mlx4_ib_destroy_ah;
1101	ibdev->ib_dev.create_srq	= mlx4_ib_create_srq;
1102	ibdev->ib_dev.modify_srq	= mlx4_ib_modify_srq;
1103	ibdev->ib_dev.query_srq		= mlx4_ib_query_srq;
1104	ibdev->ib_dev.destroy_srq	= mlx4_ib_destroy_srq;
1105	ibdev->ib_dev.post_srq_recv	= mlx4_ib_post_srq_recv;
1106	ibdev->ib_dev.create_qp		= mlx4_ib_create_qp;
1107	ibdev->ib_dev.modify_qp		= mlx4_ib_modify_qp;
1108	ibdev->ib_dev.query_qp		= mlx4_ib_query_qp;
1109	ibdev->ib_dev.destroy_qp	= mlx4_ib_destroy_qp;
1110	ibdev->ib_dev.post_send		= mlx4_ib_post_send;
1111	ibdev->ib_dev.post_recv		= mlx4_ib_post_recv;
1112	ibdev->ib_dev.create_cq		= mlx4_ib_create_cq;
1113	ibdev->ib_dev.modify_cq		= mlx4_ib_modify_cq;
1114	ibdev->ib_dev.resize_cq		= mlx4_ib_resize_cq;
1115	ibdev->ib_dev.destroy_cq	= mlx4_ib_destroy_cq;
1116	ibdev->ib_dev.poll_cq		= mlx4_ib_poll_cq;
1117	ibdev->ib_dev.req_notify_cq	= mlx4_ib_arm_cq;
1118	ibdev->ib_dev.get_dma_mr	= mlx4_ib_get_dma_mr;
1119	ibdev->ib_dev.reg_user_mr	= mlx4_ib_reg_user_mr;
1120	ibdev->ib_dev.dereg_mr		= mlx4_ib_dereg_mr;
1121	ibdev->ib_dev.alloc_fast_reg_mr = mlx4_ib_alloc_fast_reg_mr;
1122	ibdev->ib_dev.alloc_fast_reg_page_list = mlx4_ib_alloc_fast_reg_page_list;
1123	ibdev->ib_dev.free_fast_reg_page_list  = mlx4_ib_free_fast_reg_page_list;
1124	ibdev->ib_dev.attach_mcast	= mlx4_ib_mcg_attach;
1125	ibdev->ib_dev.detach_mcast	= mlx4_ib_mcg_detach;
1126	ibdev->ib_dev.process_mad	= mlx4_ib_process_mad;
1127
1128	ibdev->ib_dev.alloc_fmr		= mlx4_ib_fmr_alloc;
1129	ibdev->ib_dev.map_phys_fmr	= mlx4_ib_map_phys_fmr;
1130	ibdev->ib_dev.unmap_fmr		= mlx4_ib_unmap_fmr;
1131	ibdev->ib_dev.dealloc_fmr	= mlx4_ib_fmr_dealloc;
1132
1133	spin_lock_init(&iboe->lock);
1134
1135	if (init_node_data(ibdev))
1136		goto err_map;
1137
1138	for (i = 0; i < ibdev->num_ports; ++i) {
1139		if (mlx4_ib_port_link_layer(&ibdev->ib_dev, i + 1) ==
1140						IB_LINK_LAYER_ETHERNET) {
1141			err = mlx4_counter_alloc(ibdev->dev, &ibdev->counters[i]);
1142			if (err)
1143				ibdev->counters[i] = -1;
1144		} else
1145				ibdev->counters[i] = -1;
1146	}
1147
1148	spin_lock_init(&ibdev->sm_lock);
1149	mutex_init(&ibdev->cap_mask_mutex);
1150
1151	if (ib_register_device(&ibdev->ib_dev, NULL))
1152		goto err_counter;
1153
1154	if (mlx4_ib_mad_init(ibdev))
1155		goto err_reg;
1156
1157	if (dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE && !iboe->nb.notifier_call) {
1158		iboe->nb.notifier_call = mlx4_ib_netdev_event;
1159		err = register_netdevice_notifier(&iboe->nb);
1160		if (err)
1161			goto err_reg;
1162	}
1163
1164	for (i = 0; i < ARRAY_SIZE(mlx4_class_attributes); ++i) {
1165		if (device_create_file(&ibdev->ib_dev.dev,
1166				       mlx4_class_attributes[i]))
1167			goto err_notif;
1168	}
1169
1170	ibdev->ib_active = true;
1171
1172	return ibdev;
1173
1174err_notif:
1175	if (unregister_netdevice_notifier(&ibdev->iboe.nb))
1176		printk(KERN_WARNING "failure unregistering notifier\n");
1177	flush_workqueue(wq);
1178
1179err_reg:
1180	ib_unregister_device(&ibdev->ib_dev);
1181
1182err_counter:
1183	for (; i; --i)
1184		mlx4_counter_free(ibdev->dev, ibdev->counters[i - 1]);
1185
1186err_map:
1187	iounmap(ibdev->uar_map);
1188
1189err_uar:
1190	mlx4_uar_free(dev, &ibdev->priv_uar);
1191
1192err_pd:
1193	mlx4_pd_free(dev, ibdev->priv_pdn);
1194
1195err_dealloc:
1196	ib_dealloc_device(&ibdev->ib_dev);
1197
1198	return NULL;
1199}
1200
1201static void mlx4_ib_remove(struct mlx4_dev *dev, void *ibdev_ptr)
1202{
1203	struct mlx4_ib_dev *ibdev = ibdev_ptr;
1204	int p;
1205
1206	mlx4_ib_mad_cleanup(ibdev);
1207	ib_unregister_device(&ibdev->ib_dev);
1208	if (ibdev->iboe.nb.notifier_call) {
1209		if (unregister_netdevice_notifier(&ibdev->iboe.nb))
1210			printk(KERN_WARNING "failure unregistering notifier\n");
1211		ibdev->iboe.nb.notifier_call = NULL;
1212	}
1213	iounmap(ibdev->uar_map);
1214	for (p = 0; p < ibdev->num_ports; ++p)
1215		mlx4_counter_free(ibdev->dev, ibdev->counters[p]);
1216	mlx4_foreach_port(p, dev, MLX4_PORT_TYPE_IB)
1217		mlx4_CLOSE_PORT(dev, p);
1218
1219	mlx4_uar_free(dev, &ibdev->priv_uar);
1220	mlx4_pd_free(dev, ibdev->priv_pdn);
1221	ib_dealloc_device(&ibdev->ib_dev);
1222}
1223
1224static void mlx4_ib_event(struct mlx4_dev *dev, void *ibdev_ptr,
1225			  enum mlx4_dev_event event, int port)
1226{
1227	struct ib_event ibev;
1228	struct mlx4_ib_dev *ibdev = to_mdev((struct ib_device *) ibdev_ptr);
1229
1230	if (port > ibdev->num_ports)
1231		return;
1232
1233	switch (event) {
1234	case MLX4_DEV_EVENT_PORT_UP:
1235		ibev.event = IB_EVENT_PORT_ACTIVE;
1236		break;
1237
1238	case MLX4_DEV_EVENT_PORT_DOWN:
1239		ibev.event = IB_EVENT_PORT_ERR;
1240		break;
1241
1242	case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
1243		ibdev->ib_active = false;
1244		ibev.event = IB_EVENT_DEVICE_FATAL;
1245		break;
1246
1247	default:
1248		return;
1249	}
1250
1251	ibev.device	      = ibdev_ptr;
1252	ibev.element.port_num = port;
1253
1254	ib_dispatch_event(&ibev);
1255}
1256
1257static struct mlx4_interface mlx4_ib_interface = {
1258	.add		= mlx4_ib_add,
1259	.remove		= mlx4_ib_remove,
1260	.event		= mlx4_ib_event,
1261	.protocol	= MLX4_PROT_IB_IPV6
1262};
1263
1264static int __init mlx4_ib_init(void)
1265{
1266	int err;
1267
1268	wq = create_singlethread_workqueue("mlx4_ib");
1269	if (!wq)
1270		return -ENOMEM;
1271
1272	err = mlx4_register_interface(&mlx4_ib_interface);
1273	if (err) {
1274		destroy_workqueue(wq);
1275		return err;
1276	}
1277
1278	return 0;
1279}
1280
1281static void __exit mlx4_ib_cleanup(void)
1282{
1283	mlx4_unregister_interface(&mlx4_ib_interface);
1284	destroy_workqueue(wq);
1285}
1286
1287module_init(mlx4_ib_init);
1288module_exit(mlx4_ib_cleanup);
1289