Merge tag 'stable/for-linus-3.6-rc1-tag' of git://git.kernel.org/pub/scm/linux/kernel...
[cascardo/linux.git] / drivers / infiniband / hw / mlx4 / main.c
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_DRV_NAME
54 #define DRV_VERSION     "1.0"
55 #define DRV_RELDATE     "April 4, 2008"
56
57 MODULE_AUTHOR("Roland Dreier");
58 MODULE_DESCRIPTION("Mellanox ConnectX HCA InfiniBand driver");
59 MODULE_LICENSE("Dual BSD/GPL");
60 MODULE_VERSION(DRV_VERSION);
61
62 static const char mlx4_ib_version[] =
63         DRV_NAME ": Mellanox ConnectX InfiniBand driver v"
64         DRV_VERSION " (" DRV_RELDATE ")\n";
65
66 struct 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
73 static struct workqueue_struct *wq;
74
75 static 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
83 static union ib_gid zgid;
84
85 static 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         if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC)
132                 props->device_cap_flags |= IB_DEVICE_XRC;
133
134         props->vendor_id           = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
135                 0xffffff;
136         props->vendor_part_id      = be16_to_cpup((__be16 *) (out_mad->data + 30));
137         props->hw_ver              = be32_to_cpup((__be32 *) (out_mad->data + 32));
138         memcpy(&props->sys_image_guid, out_mad->data +  4, 8);
139
140         props->max_mr_size         = ~0ull;
141         props->page_size_cap       = dev->dev->caps.page_size_cap;
142         props->max_qp              = dev->dev->caps.num_qps - dev->dev->caps.reserved_qps;
143         props->max_qp_wr           = dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE;
144         props->max_sge             = min(dev->dev->caps.max_sq_sg,
145                                          dev->dev->caps.max_rq_sg);
146         props->max_cq              = dev->dev->caps.num_cqs - dev->dev->caps.reserved_cqs;
147         props->max_cqe             = dev->dev->caps.max_cqes;
148         props->max_mr              = dev->dev->caps.num_mpts - dev->dev->caps.reserved_mrws;
149         props->max_pd              = dev->dev->caps.num_pds - dev->dev->caps.reserved_pds;
150         props->max_qp_rd_atom      = dev->dev->caps.max_qp_dest_rdma;
151         props->max_qp_init_rd_atom = dev->dev->caps.max_qp_init_rdma;
152         props->max_res_rd_atom     = props->max_qp_rd_atom * props->max_qp;
153         props->max_srq             = dev->dev->caps.num_srqs - dev->dev->caps.reserved_srqs;
154         props->max_srq_wr          = dev->dev->caps.max_srq_wqes - 1;
155         props->max_srq_sge         = dev->dev->caps.max_srq_sge;
156         props->max_fast_reg_page_list_len = MLX4_MAX_FAST_REG_PAGES;
157         props->local_ca_ack_delay  = dev->dev->caps.local_ca_ack_delay;
158         props->atomic_cap          = dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_ATOMIC ?
159                 IB_ATOMIC_HCA : IB_ATOMIC_NONE;
160         props->masked_atomic_cap   = props->atomic_cap;
161         props->max_pkeys           = dev->dev->caps.pkey_table_len[1];
162         props->max_mcast_grp       = dev->dev->caps.num_mgms + dev->dev->caps.num_amgms;
163         props->max_mcast_qp_attach = dev->dev->caps.num_qp_per_mgm;
164         props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
165                                            props->max_mcast_grp;
166         props->max_map_per_fmr = dev->dev->caps.max_fmr_maps;
167
168 out:
169         kfree(in_mad);
170         kfree(out_mad);
171
172         return err;
173 }
174
175 static enum rdma_link_layer
176 mlx4_ib_port_link_layer(struct ib_device *device, u8 port_num)
177 {
178         struct mlx4_dev *dev = to_mdev(device)->dev;
179
180         return dev->caps.port_mask[port_num] == MLX4_PORT_TYPE_IB ?
181                 IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
182 }
183
184 static int ib_link_query_port(struct ib_device *ibdev, u8 port,
185                               struct ib_port_attr *props)
186 {
187         struct ib_smp *in_mad  = NULL;
188         struct ib_smp *out_mad = NULL;
189         int ext_active_speed;
190         int err = -ENOMEM;
191
192         in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
193         out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
194         if (!in_mad || !out_mad)
195                 goto out;
196
197         init_query_mad(in_mad);
198         in_mad->attr_id  = IB_SMP_ATTR_PORT_INFO;
199         in_mad->attr_mod = cpu_to_be32(port);
200
201         err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL,
202                                 in_mad, out_mad);
203         if (err)
204                 goto out;
205
206
207         props->lid              = be16_to_cpup((__be16 *) (out_mad->data + 16));
208         props->lmc              = out_mad->data[34] & 0x7;
209         props->sm_lid           = be16_to_cpup((__be16 *) (out_mad->data + 18));
210         props->sm_sl            = out_mad->data[36] & 0xf;
211         props->state            = out_mad->data[32] & 0xf;
212         props->phys_state       = out_mad->data[33] >> 4;
213         props->port_cap_flags   = be32_to_cpup((__be32 *) (out_mad->data + 20));
214         props->gid_tbl_len      = to_mdev(ibdev)->dev->caps.gid_table_len[port];
215         props->max_msg_sz       = to_mdev(ibdev)->dev->caps.max_msg_sz;
216         props->pkey_tbl_len     = to_mdev(ibdev)->dev->caps.pkey_table_len[port];
217         props->bad_pkey_cntr    = be16_to_cpup((__be16 *) (out_mad->data + 46));
218         props->qkey_viol_cntr   = be16_to_cpup((__be16 *) (out_mad->data + 48));
219         props->active_width     = out_mad->data[31] & 0xf;
220         props->active_speed     = out_mad->data[35] >> 4;
221         props->max_mtu          = out_mad->data[41] & 0xf;
222         props->active_mtu       = out_mad->data[36] >> 4;
223         props->subnet_timeout   = out_mad->data[51] & 0x1f;
224         props->max_vl_num       = out_mad->data[37] >> 4;
225         props->init_type_reply  = out_mad->data[41] >> 4;
226
227         /* Check if extended speeds (EDR/FDR/...) are supported */
228         if (props->port_cap_flags & IB_PORT_EXTENDED_SPEEDS_SUP) {
229                 ext_active_speed = out_mad->data[62] >> 4;
230
231                 switch (ext_active_speed) {
232                 case 1:
233                         props->active_speed = IB_SPEED_FDR;
234                         break;
235                 case 2:
236                         props->active_speed = IB_SPEED_EDR;
237                         break;
238                 }
239         }
240
241         /* If reported active speed is QDR, check if is FDR-10 */
242         if (props->active_speed == IB_SPEED_QDR) {
243                 init_query_mad(in_mad);
244                 in_mad->attr_id = MLX4_ATTR_EXTENDED_PORT_INFO;
245                 in_mad->attr_mod = cpu_to_be32(port);
246
247                 err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port,
248                                    NULL, NULL, in_mad, out_mad);
249                 if (err)
250                         goto out;
251
252                 /* Checking LinkSpeedActive for FDR-10 */
253                 if (out_mad->data[15] & 0x1)
254                         props->active_speed = IB_SPEED_FDR10;
255         }
256
257         /* Avoid wrong speed value returned by FW if the IB link is down. */
258         if (props->state == IB_PORT_DOWN)
259                  props->active_speed = IB_SPEED_SDR;
260
261 out:
262         kfree(in_mad);
263         kfree(out_mad);
264         return err;
265 }
266
267 static u8 state_to_phys_state(enum ib_port_state state)
268 {
269         return state == IB_PORT_ACTIVE ? 5 : 3;
270 }
271
272 static int eth_link_query_port(struct ib_device *ibdev, u8 port,
273                                struct ib_port_attr *props)
274 {
275
276         struct mlx4_ib_dev *mdev = to_mdev(ibdev);
277         struct mlx4_ib_iboe *iboe = &mdev->iboe;
278         struct net_device *ndev;
279         enum ib_mtu tmp;
280         struct mlx4_cmd_mailbox *mailbox;
281         int err = 0;
282
283         mailbox = mlx4_alloc_cmd_mailbox(mdev->dev);
284         if (IS_ERR(mailbox))
285                 return PTR_ERR(mailbox);
286
287         err = mlx4_cmd_box(mdev->dev, 0, mailbox->dma, port, 0,
288                            MLX4_CMD_QUERY_PORT, MLX4_CMD_TIME_CLASS_B,
289                            MLX4_CMD_WRAPPED);
290         if (err)
291                 goto out;
292
293         props->active_width     =  (((u8 *)mailbox->buf)[5] == 0x40) ?
294                                                 IB_WIDTH_4X : IB_WIDTH_1X;
295         props->active_speed     = IB_SPEED_QDR;
296         props->port_cap_flags   = IB_PORT_CM_SUP;
297         props->gid_tbl_len      = mdev->dev->caps.gid_table_len[port];
298         props->max_msg_sz       = mdev->dev->caps.max_msg_sz;
299         props->pkey_tbl_len     = 1;
300         props->max_mtu          = IB_MTU_4096;
301         props->max_vl_num       = 2;
302         props->state            = IB_PORT_DOWN;
303         props->phys_state       = state_to_phys_state(props->state);
304         props->active_mtu       = IB_MTU_256;
305         spin_lock(&iboe->lock);
306         ndev = iboe->netdevs[port - 1];
307         if (!ndev)
308                 goto out_unlock;
309
310         tmp = iboe_get_mtu(ndev->mtu);
311         props->active_mtu = tmp ? min(props->max_mtu, tmp) : IB_MTU_256;
312
313         props->state            = (netif_running(ndev) && netif_carrier_ok(ndev)) ?
314                                         IB_PORT_ACTIVE : IB_PORT_DOWN;
315         props->phys_state       = state_to_phys_state(props->state);
316 out_unlock:
317         spin_unlock(&iboe->lock);
318 out:
319         mlx4_free_cmd_mailbox(mdev->dev, mailbox);
320         return err;
321 }
322
323 static int mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
324                               struct ib_port_attr *props)
325 {
326         int err;
327
328         memset(props, 0, sizeof *props);
329
330         err = mlx4_ib_port_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND ?
331                 ib_link_query_port(ibdev, port, props) :
332                                 eth_link_query_port(ibdev, port, props);
333
334         return err;
335 }
336
337 static int __mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
338                                union ib_gid *gid)
339 {
340         struct ib_smp *in_mad  = NULL;
341         struct ib_smp *out_mad = NULL;
342         int err = -ENOMEM;
343
344         in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
345         out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
346         if (!in_mad || !out_mad)
347                 goto out;
348
349         init_query_mad(in_mad);
350         in_mad->attr_id  = IB_SMP_ATTR_PORT_INFO;
351         in_mad->attr_mod = cpu_to_be32(port);
352
353         err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
354         if (err)
355                 goto out;
356
357         memcpy(gid->raw, out_mad->data + 8, 8);
358
359         init_query_mad(in_mad);
360         in_mad->attr_id  = IB_SMP_ATTR_GUID_INFO;
361         in_mad->attr_mod = cpu_to_be32(index / 8);
362
363         err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
364         if (err)
365                 goto out;
366
367         memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
368
369 out:
370         kfree(in_mad);
371         kfree(out_mad);
372         return err;
373 }
374
375 static int iboe_query_gid(struct ib_device *ibdev, u8 port, int index,
376                           union ib_gid *gid)
377 {
378         struct mlx4_ib_dev *dev = to_mdev(ibdev);
379
380         *gid = dev->iboe.gid_table[port - 1][index];
381
382         return 0;
383 }
384
385 static int mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
386                              union ib_gid *gid)
387 {
388         if (rdma_port_get_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND)
389                 return __mlx4_ib_query_gid(ibdev, port, index, gid);
390         else
391                 return iboe_query_gid(ibdev, port, index, gid);
392 }
393
394 static int mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
395                               u16 *pkey)
396 {
397         struct ib_smp *in_mad  = NULL;
398         struct ib_smp *out_mad = NULL;
399         int err = -ENOMEM;
400
401         in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
402         out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
403         if (!in_mad || !out_mad)
404                 goto out;
405
406         init_query_mad(in_mad);
407         in_mad->attr_id  = IB_SMP_ATTR_PKEY_TABLE;
408         in_mad->attr_mod = cpu_to_be32(index / 32);
409
410         err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
411         if (err)
412                 goto out;
413
414         *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
415
416 out:
417         kfree(in_mad);
418         kfree(out_mad);
419         return err;
420 }
421
422 static int mlx4_ib_modify_device(struct ib_device *ibdev, int mask,
423                                  struct ib_device_modify *props)
424 {
425         struct mlx4_cmd_mailbox *mailbox;
426
427         if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
428                 return -EOPNOTSUPP;
429
430         if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
431                 return 0;
432
433         spin_lock(&to_mdev(ibdev)->sm_lock);
434         memcpy(ibdev->node_desc, props->node_desc, 64);
435         spin_unlock(&to_mdev(ibdev)->sm_lock);
436
437         /*
438          * If possible, pass node desc to FW, so it can generate
439          * a 144 trap.  If cmd fails, just ignore.
440          */
441         mailbox = mlx4_alloc_cmd_mailbox(to_mdev(ibdev)->dev);
442         if (IS_ERR(mailbox))
443                 return 0;
444
445         memset(mailbox->buf, 0, 256);
446         memcpy(mailbox->buf, props->node_desc, 64);
447         mlx4_cmd(to_mdev(ibdev)->dev, mailbox->dma, 1, 0,
448                  MLX4_CMD_SET_NODE, MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
449
450         mlx4_free_cmd_mailbox(to_mdev(ibdev)->dev, mailbox);
451
452         return 0;
453 }
454
455 static int mlx4_SET_PORT(struct mlx4_ib_dev *dev, u8 port, int reset_qkey_viols,
456                          u32 cap_mask)
457 {
458         struct mlx4_cmd_mailbox *mailbox;
459         int err;
460         u8 is_eth = dev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
461
462         mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
463         if (IS_ERR(mailbox))
464                 return PTR_ERR(mailbox);
465
466         memset(mailbox->buf, 0, 256);
467
468         if (dev->dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
469                 *(u8 *) mailbox->buf         = !!reset_qkey_viols << 6;
470                 ((__be32 *) mailbox->buf)[2] = cpu_to_be32(cap_mask);
471         } else {
472                 ((u8 *) mailbox->buf)[3]     = !!reset_qkey_viols;
473                 ((__be32 *) mailbox->buf)[1] = cpu_to_be32(cap_mask);
474         }
475
476         err = mlx4_cmd(dev->dev, mailbox->dma, port, is_eth, MLX4_CMD_SET_PORT,
477                        MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
478
479         mlx4_free_cmd_mailbox(dev->dev, mailbox);
480         return err;
481 }
482
483 static int mlx4_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
484                                struct ib_port_modify *props)
485 {
486         struct ib_port_attr attr;
487         u32 cap_mask;
488         int err;
489
490         mutex_lock(&to_mdev(ibdev)->cap_mask_mutex);
491
492         err = mlx4_ib_query_port(ibdev, port, &attr);
493         if (err)
494                 goto out;
495
496         cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
497                 ~props->clr_port_cap_mask;
498
499         err = mlx4_SET_PORT(to_mdev(ibdev), port,
500                             !!(mask & IB_PORT_RESET_QKEY_CNTR),
501                             cap_mask);
502
503 out:
504         mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
505         return err;
506 }
507
508 static struct ib_ucontext *mlx4_ib_alloc_ucontext(struct ib_device *ibdev,
509                                                   struct ib_udata *udata)
510 {
511         struct mlx4_ib_dev *dev = to_mdev(ibdev);
512         struct mlx4_ib_ucontext *context;
513         struct mlx4_ib_alloc_ucontext_resp resp;
514         int err;
515
516         if (!dev->ib_active)
517                 return ERR_PTR(-EAGAIN);
518
519         resp.qp_tab_size      = dev->dev->caps.num_qps;
520         resp.bf_reg_size      = dev->dev->caps.bf_reg_size;
521         resp.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
522
523         context = kmalloc(sizeof *context, GFP_KERNEL);
524         if (!context)
525                 return ERR_PTR(-ENOMEM);
526
527         err = mlx4_uar_alloc(to_mdev(ibdev)->dev, &context->uar);
528         if (err) {
529                 kfree(context);
530                 return ERR_PTR(err);
531         }
532
533         INIT_LIST_HEAD(&context->db_page_list);
534         mutex_init(&context->db_page_mutex);
535
536         err = ib_copy_to_udata(udata, &resp, sizeof resp);
537         if (err) {
538                 mlx4_uar_free(to_mdev(ibdev)->dev, &context->uar);
539                 kfree(context);
540                 return ERR_PTR(-EFAULT);
541         }
542
543         return &context->ibucontext;
544 }
545
546 static int mlx4_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
547 {
548         struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
549
550         mlx4_uar_free(to_mdev(ibcontext->device)->dev, &context->uar);
551         kfree(context);
552
553         return 0;
554 }
555
556 static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
557 {
558         struct mlx4_ib_dev *dev = to_mdev(context->device);
559
560         if (vma->vm_end - vma->vm_start != PAGE_SIZE)
561                 return -EINVAL;
562
563         if (vma->vm_pgoff == 0) {
564                 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
565
566                 if (io_remap_pfn_range(vma, vma->vm_start,
567                                        to_mucontext(context)->uar.pfn,
568                                        PAGE_SIZE, vma->vm_page_prot))
569                         return -EAGAIN;
570         } else if (vma->vm_pgoff == 1 && dev->dev->caps.bf_reg_size != 0) {
571                 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
572
573                 if (io_remap_pfn_range(vma, vma->vm_start,
574                                        to_mucontext(context)->uar.pfn +
575                                        dev->dev->caps.num_uars,
576                                        PAGE_SIZE, vma->vm_page_prot))
577                         return -EAGAIN;
578         } else
579                 return -EINVAL;
580
581         return 0;
582 }
583
584 static struct ib_pd *mlx4_ib_alloc_pd(struct ib_device *ibdev,
585                                       struct ib_ucontext *context,
586                                       struct ib_udata *udata)
587 {
588         struct mlx4_ib_pd *pd;
589         int err;
590
591         pd = kmalloc(sizeof *pd, GFP_KERNEL);
592         if (!pd)
593                 return ERR_PTR(-ENOMEM);
594
595         err = mlx4_pd_alloc(to_mdev(ibdev)->dev, &pd->pdn);
596         if (err) {
597                 kfree(pd);
598                 return ERR_PTR(err);
599         }
600
601         if (context)
602                 if (ib_copy_to_udata(udata, &pd->pdn, sizeof (__u32))) {
603                         mlx4_pd_free(to_mdev(ibdev)->dev, pd->pdn);
604                         kfree(pd);
605                         return ERR_PTR(-EFAULT);
606                 }
607
608         return &pd->ibpd;
609 }
610
611 static int mlx4_ib_dealloc_pd(struct ib_pd *pd)
612 {
613         mlx4_pd_free(to_mdev(pd->device)->dev, to_mpd(pd)->pdn);
614         kfree(pd);
615
616         return 0;
617 }
618
619 static struct ib_xrcd *mlx4_ib_alloc_xrcd(struct ib_device *ibdev,
620                                           struct ib_ucontext *context,
621                                           struct ib_udata *udata)
622 {
623         struct mlx4_ib_xrcd *xrcd;
624         int err;
625
626         if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC))
627                 return ERR_PTR(-ENOSYS);
628
629         xrcd = kmalloc(sizeof *xrcd, GFP_KERNEL);
630         if (!xrcd)
631                 return ERR_PTR(-ENOMEM);
632
633         err = mlx4_xrcd_alloc(to_mdev(ibdev)->dev, &xrcd->xrcdn);
634         if (err)
635                 goto err1;
636
637         xrcd->pd = ib_alloc_pd(ibdev);
638         if (IS_ERR(xrcd->pd)) {
639                 err = PTR_ERR(xrcd->pd);
640                 goto err2;
641         }
642
643         xrcd->cq = ib_create_cq(ibdev, NULL, NULL, xrcd, 1, 0);
644         if (IS_ERR(xrcd->cq)) {
645                 err = PTR_ERR(xrcd->cq);
646                 goto err3;
647         }
648
649         return &xrcd->ibxrcd;
650
651 err3:
652         ib_dealloc_pd(xrcd->pd);
653 err2:
654         mlx4_xrcd_free(to_mdev(ibdev)->dev, xrcd->xrcdn);
655 err1:
656         kfree(xrcd);
657         return ERR_PTR(err);
658 }
659
660 static int mlx4_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
661 {
662         ib_destroy_cq(to_mxrcd(xrcd)->cq);
663         ib_dealloc_pd(to_mxrcd(xrcd)->pd);
664         mlx4_xrcd_free(to_mdev(xrcd->device)->dev, to_mxrcd(xrcd)->xrcdn);
665         kfree(xrcd);
666
667         return 0;
668 }
669
670 static int add_gid_entry(struct ib_qp *ibqp, union ib_gid *gid)
671 {
672         struct mlx4_ib_qp *mqp = to_mqp(ibqp);
673         struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
674         struct mlx4_ib_gid_entry *ge;
675
676         ge = kzalloc(sizeof *ge, GFP_KERNEL);
677         if (!ge)
678                 return -ENOMEM;
679
680         ge->gid = *gid;
681         if (mlx4_ib_add_mc(mdev, mqp, gid)) {
682                 ge->port = mqp->port;
683                 ge->added = 1;
684         }
685
686         mutex_lock(&mqp->mutex);
687         list_add_tail(&ge->list, &mqp->gid_list);
688         mutex_unlock(&mqp->mutex);
689
690         return 0;
691 }
692
693 int mlx4_ib_add_mc(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
694                    union ib_gid *gid)
695 {
696         u8 mac[6];
697         struct net_device *ndev;
698         int ret = 0;
699
700         if (!mqp->port)
701                 return 0;
702
703         spin_lock(&mdev->iboe.lock);
704         ndev = mdev->iboe.netdevs[mqp->port - 1];
705         if (ndev)
706                 dev_hold(ndev);
707         spin_unlock(&mdev->iboe.lock);
708
709         if (ndev) {
710                 rdma_get_mcast_mac((struct in6_addr *)gid, mac);
711                 rtnl_lock();
712                 dev_mc_add(mdev->iboe.netdevs[mqp->port - 1], mac);
713                 ret = 1;
714                 rtnl_unlock();
715                 dev_put(ndev);
716         }
717
718         return ret;
719 }
720
721 struct mlx4_ib_steering {
722         struct list_head list;
723         u64 reg_id;
724         union ib_gid gid;
725 };
726
727 static int mlx4_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
728 {
729         int err;
730         struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
731         struct mlx4_ib_qp *mqp = to_mqp(ibqp);
732         u64 reg_id;
733         struct mlx4_ib_steering *ib_steering = NULL;
734
735         if (mdev->dev->caps.steering_mode ==
736             MLX4_STEERING_MODE_DEVICE_MANAGED) {
737                 ib_steering = kmalloc(sizeof(*ib_steering), GFP_KERNEL);
738                 if (!ib_steering)
739                         return -ENOMEM;
740         }
741
742         err = mlx4_multicast_attach(mdev->dev, &mqp->mqp, gid->raw, mqp->port,
743                                     !!(mqp->flags &
744                                        MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK),
745                                     MLX4_PROT_IB_IPV6, &reg_id);
746         if (err)
747                 goto err_malloc;
748
749         err = add_gid_entry(ibqp, gid);
750         if (err)
751                 goto err_add;
752
753         if (ib_steering) {
754                 memcpy(ib_steering->gid.raw, gid->raw, 16);
755                 ib_steering->reg_id = reg_id;
756                 mutex_lock(&mqp->mutex);
757                 list_add(&ib_steering->list, &mqp->steering_rules);
758                 mutex_unlock(&mqp->mutex);
759         }
760         return 0;
761
762 err_add:
763         mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
764                               MLX4_PROT_IB_IPV6, reg_id);
765 err_malloc:
766         kfree(ib_steering);
767
768         return err;
769 }
770
771 static struct mlx4_ib_gid_entry *find_gid_entry(struct mlx4_ib_qp *qp, u8 *raw)
772 {
773         struct mlx4_ib_gid_entry *ge;
774         struct mlx4_ib_gid_entry *tmp;
775         struct mlx4_ib_gid_entry *ret = NULL;
776
777         list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
778                 if (!memcmp(raw, ge->gid.raw, 16)) {
779                         ret = ge;
780                         break;
781                 }
782         }
783
784         return ret;
785 }
786
787 static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
788 {
789         int err;
790         struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
791         struct mlx4_ib_qp *mqp = to_mqp(ibqp);
792         u8 mac[6];
793         struct net_device *ndev;
794         struct mlx4_ib_gid_entry *ge;
795         u64 reg_id = 0;
796
797         if (mdev->dev->caps.steering_mode ==
798             MLX4_STEERING_MODE_DEVICE_MANAGED) {
799                 struct mlx4_ib_steering *ib_steering;
800
801                 mutex_lock(&mqp->mutex);
802                 list_for_each_entry(ib_steering, &mqp->steering_rules, list) {
803                         if (!memcmp(ib_steering->gid.raw, gid->raw, 16)) {
804                                 list_del(&ib_steering->list);
805                                 break;
806                         }
807                 }
808                 mutex_unlock(&mqp->mutex);
809                 if (&ib_steering->list == &mqp->steering_rules) {
810                         pr_err("Couldn't find reg_id for mgid. Steering rule is left attached\n");
811                         return -EINVAL;
812                 }
813                 reg_id = ib_steering->reg_id;
814                 kfree(ib_steering);
815         }
816
817         err = mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
818                                     MLX4_PROT_IB_IPV6, reg_id);
819         if (err)
820                 return err;
821
822         mutex_lock(&mqp->mutex);
823         ge = find_gid_entry(mqp, gid->raw);
824         if (ge) {
825                 spin_lock(&mdev->iboe.lock);
826                 ndev = ge->added ? mdev->iboe.netdevs[ge->port - 1] : NULL;
827                 if (ndev)
828                         dev_hold(ndev);
829                 spin_unlock(&mdev->iboe.lock);
830                 rdma_get_mcast_mac((struct in6_addr *)gid, mac);
831                 if (ndev) {
832                         rtnl_lock();
833                         dev_mc_del(mdev->iboe.netdevs[ge->port - 1], mac);
834                         rtnl_unlock();
835                         dev_put(ndev);
836                 }
837                 list_del(&ge->list);
838                 kfree(ge);
839         } else
840                 pr_warn("could not find mgid entry\n");
841
842         mutex_unlock(&mqp->mutex);
843
844         return 0;
845 }
846
847 static int init_node_data(struct mlx4_ib_dev *dev)
848 {
849         struct ib_smp *in_mad  = NULL;
850         struct ib_smp *out_mad = NULL;
851         int err = -ENOMEM;
852
853         in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
854         out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
855         if (!in_mad || !out_mad)
856                 goto out;
857
858         init_query_mad(in_mad);
859         in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
860
861         err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
862         if (err)
863                 goto out;
864
865         memcpy(dev->ib_dev.node_desc, out_mad->data, 64);
866
867         in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
868
869         err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
870         if (err)
871                 goto out;
872
873         memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
874
875 out:
876         kfree(in_mad);
877         kfree(out_mad);
878         return err;
879 }
880
881 static ssize_t show_hca(struct device *device, struct device_attribute *attr,
882                         char *buf)
883 {
884         struct mlx4_ib_dev *dev =
885                 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
886         return sprintf(buf, "MT%d\n", dev->dev->pdev->device);
887 }
888
889 static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
890                            char *buf)
891 {
892         struct mlx4_ib_dev *dev =
893                 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
894         return sprintf(buf, "%d.%d.%d\n", (int) (dev->dev->caps.fw_ver >> 32),
895                        (int) (dev->dev->caps.fw_ver >> 16) & 0xffff,
896                        (int) dev->dev->caps.fw_ver & 0xffff);
897 }
898
899 static ssize_t show_rev(struct device *device, struct device_attribute *attr,
900                         char *buf)
901 {
902         struct mlx4_ib_dev *dev =
903                 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
904         return sprintf(buf, "%x\n", dev->dev->rev_id);
905 }
906
907 static ssize_t show_board(struct device *device, struct device_attribute *attr,
908                           char *buf)
909 {
910         struct mlx4_ib_dev *dev =
911                 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
912         return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN,
913                        dev->dev->board_id);
914 }
915
916 static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
917 static DEVICE_ATTR(fw_ver,   S_IRUGO, show_fw_ver, NULL);
918 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
919 static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
920
921 static struct device_attribute *mlx4_class_attributes[] = {
922         &dev_attr_hw_rev,
923         &dev_attr_fw_ver,
924         &dev_attr_hca_type,
925         &dev_attr_board_id
926 };
927
928 static void mlx4_addrconf_ifid_eui48(u8 *eui, u16 vlan_id, struct net_device *dev)
929 {
930         memcpy(eui, dev->dev_addr, 3);
931         memcpy(eui + 5, dev->dev_addr + 3, 3);
932         if (vlan_id < 0x1000) {
933                 eui[3] = vlan_id >> 8;
934                 eui[4] = vlan_id & 0xff;
935         } else {
936                 eui[3] = 0xff;
937                 eui[4] = 0xfe;
938         }
939         eui[0] ^= 2;
940 }
941
942 static void update_gids_task(struct work_struct *work)
943 {
944         struct update_gid_work *gw = container_of(work, struct update_gid_work, work);
945         struct mlx4_cmd_mailbox *mailbox;
946         union ib_gid *gids;
947         int err;
948         struct mlx4_dev *dev = gw->dev->dev;
949
950         mailbox = mlx4_alloc_cmd_mailbox(dev);
951         if (IS_ERR(mailbox)) {
952                 pr_warn("update gid table failed %ld\n", PTR_ERR(mailbox));
953                 return;
954         }
955
956         gids = mailbox->buf;
957         memcpy(gids, gw->gids, sizeof gw->gids);
958
959         err = mlx4_cmd(dev, mailbox->dma, MLX4_SET_PORT_GID_TABLE << 8 | gw->port,
960                        1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
961                        MLX4_CMD_NATIVE);
962         if (err)
963                 pr_warn("set port command failed\n");
964         else {
965                 memcpy(gw->dev->iboe.gid_table[gw->port - 1], gw->gids, sizeof gw->gids);
966                 mlx4_ib_dispatch_event(gw->dev, gw->port, IB_EVENT_GID_CHANGE);
967         }
968
969         mlx4_free_cmd_mailbox(dev, mailbox);
970         kfree(gw);
971 }
972
973 static int update_ipv6_gids(struct mlx4_ib_dev *dev, int port, int clear)
974 {
975         struct net_device *ndev = dev->iboe.netdevs[port - 1];
976         struct update_gid_work *work;
977         struct net_device *tmp;
978         int i;
979         u8 *hits;
980         int ret;
981         union ib_gid gid;
982         int free;
983         int found;
984         int need_update = 0;
985         u16 vid;
986
987         work = kzalloc(sizeof *work, GFP_ATOMIC);
988         if (!work)
989                 return -ENOMEM;
990
991         hits = kzalloc(128, GFP_ATOMIC);
992         if (!hits) {
993                 ret = -ENOMEM;
994                 goto out;
995         }
996
997         rcu_read_lock();
998         for_each_netdev_rcu(&init_net, tmp) {
999                 if (ndev && (tmp == ndev || rdma_vlan_dev_real_dev(tmp) == ndev)) {
1000                         gid.global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
1001                         vid = rdma_vlan_dev_vlan_id(tmp);
1002                         mlx4_addrconf_ifid_eui48(&gid.raw[8], vid, ndev);
1003                         found = 0;
1004                         free = -1;
1005                         for (i = 0; i < 128; ++i) {
1006                                 if (free < 0 &&
1007                                     !memcmp(&dev->iboe.gid_table[port - 1][i], &zgid, sizeof zgid))
1008                                         free = i;
1009                                 if (!memcmp(&dev->iboe.gid_table[port - 1][i], &gid, sizeof gid)) {
1010                                         hits[i] = 1;
1011                                         found = 1;
1012                                         break;
1013                                 }
1014                         }
1015
1016                         if (!found) {
1017                                 if (tmp == ndev &&
1018                                     (memcmp(&dev->iboe.gid_table[port - 1][0],
1019                                             &gid, sizeof gid) ||
1020                                      !memcmp(&dev->iboe.gid_table[port - 1][0],
1021                                              &zgid, sizeof gid))) {
1022                                         dev->iboe.gid_table[port - 1][0] = gid;
1023                                         ++need_update;
1024                                         hits[0] = 1;
1025                                 } else if (free >= 0) {
1026                                         dev->iboe.gid_table[port - 1][free] = gid;
1027                                         hits[free] = 1;
1028                                         ++need_update;
1029                                 }
1030                         }
1031                 }
1032         }
1033         rcu_read_unlock();
1034
1035         for (i = 0; i < 128; ++i)
1036                 if (!hits[i]) {
1037                         if (memcmp(&dev->iboe.gid_table[port - 1][i], &zgid, sizeof zgid))
1038                                 ++need_update;
1039                         dev->iboe.gid_table[port - 1][i] = zgid;
1040                 }
1041
1042         if (need_update) {
1043                 memcpy(work->gids, dev->iboe.gid_table[port - 1], sizeof work->gids);
1044                 INIT_WORK(&work->work, update_gids_task);
1045                 work->port = port;
1046                 work->dev = dev;
1047                 queue_work(wq, &work->work);
1048         } else
1049                 kfree(work);
1050
1051         kfree(hits);
1052         return 0;
1053
1054 out:
1055         kfree(work);
1056         return ret;
1057 }
1058
1059 static void handle_en_event(struct mlx4_ib_dev *dev, int port, unsigned long event)
1060 {
1061         switch (event) {
1062         case NETDEV_UP:
1063         case NETDEV_CHANGEADDR:
1064                 update_ipv6_gids(dev, port, 0);
1065                 break;
1066
1067         case NETDEV_DOWN:
1068                 update_ipv6_gids(dev, port, 1);
1069                 dev->iboe.netdevs[port - 1] = NULL;
1070         }
1071 }
1072
1073 static void netdev_added(struct mlx4_ib_dev *dev, int port)
1074 {
1075         update_ipv6_gids(dev, port, 0);
1076 }
1077
1078 static void netdev_removed(struct mlx4_ib_dev *dev, int port)
1079 {
1080         update_ipv6_gids(dev, port, 1);
1081 }
1082
1083 static int mlx4_ib_netdev_event(struct notifier_block *this, unsigned long event,
1084                                 void *ptr)
1085 {
1086         struct net_device *dev = ptr;
1087         struct mlx4_ib_dev *ibdev;
1088         struct net_device *oldnd;
1089         struct mlx4_ib_iboe *iboe;
1090         int port;
1091
1092         if (!net_eq(dev_net(dev), &init_net))
1093                 return NOTIFY_DONE;
1094
1095         ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb);
1096         iboe = &ibdev->iboe;
1097
1098         spin_lock(&iboe->lock);
1099         mlx4_foreach_ib_transport_port(port, ibdev->dev) {
1100                 oldnd = iboe->netdevs[port - 1];
1101                 iboe->netdevs[port - 1] =
1102                         mlx4_get_protocol_dev(ibdev->dev, MLX4_PROT_ETH, port);
1103                 if (oldnd != iboe->netdevs[port - 1]) {
1104                         if (iboe->netdevs[port - 1])
1105                                 netdev_added(ibdev, port);
1106                         else
1107                                 netdev_removed(ibdev, port);
1108                 }
1109         }
1110
1111         if (dev == iboe->netdevs[0] ||
1112             (iboe->netdevs[0] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[0]))
1113                 handle_en_event(ibdev, 1, event);
1114         else if (dev == iboe->netdevs[1]
1115                  || (iboe->netdevs[1] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[1]))
1116                 handle_en_event(ibdev, 2, event);
1117
1118         spin_unlock(&iboe->lock);
1119
1120         return NOTIFY_DONE;
1121 }
1122
1123 static void mlx4_ib_alloc_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev)
1124 {
1125         char name[32];
1126         int eq_per_port = 0;
1127         int added_eqs = 0;
1128         int total_eqs = 0;
1129         int i, j, eq;
1130
1131         /* Legacy mode or comp_pool is not large enough */
1132         if (dev->caps.comp_pool == 0 ||
1133             dev->caps.num_ports > dev->caps.comp_pool)
1134                 return;
1135
1136         eq_per_port = rounddown_pow_of_two(dev->caps.comp_pool/
1137                                         dev->caps.num_ports);
1138
1139         /* Init eq table */
1140         added_eqs = 0;
1141         mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB)
1142                 added_eqs += eq_per_port;
1143
1144         total_eqs = dev->caps.num_comp_vectors + added_eqs;
1145
1146         ibdev->eq_table = kzalloc(total_eqs * sizeof(int), GFP_KERNEL);
1147         if (!ibdev->eq_table)
1148                 return;
1149
1150         ibdev->eq_added = added_eqs;
1151
1152         eq = 0;
1153         mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_IB) {
1154                 for (j = 0; j < eq_per_port; j++) {
1155                         sprintf(name, "mlx4-ib-%d-%d@%s",
1156                                 i, j, dev->pdev->bus->name);
1157                         /* Set IRQ for specific name (per ring) */
1158                         if (mlx4_assign_eq(dev, name, NULL,
1159                                            &ibdev->eq_table[eq])) {
1160                                 /* Use legacy (same as mlx4_en driver) */
1161                                 pr_warn("Can't allocate EQ %d; reverting to legacy\n", eq);
1162                                 ibdev->eq_table[eq] =
1163                                         (eq % dev->caps.num_comp_vectors);
1164                         }
1165                         eq++;
1166                 }
1167         }
1168
1169         /* Fill the reset of the vector with legacy EQ */
1170         for (i = 0, eq = added_eqs; i < dev->caps.num_comp_vectors; i++)
1171                 ibdev->eq_table[eq++] = i;
1172
1173         /* Advertise the new number of EQs to clients */
1174         ibdev->ib_dev.num_comp_vectors = total_eqs;
1175 }
1176
1177 static void mlx4_ib_free_eqs(struct mlx4_dev *dev, struct mlx4_ib_dev *ibdev)
1178 {
1179         int i;
1180
1181         /* no additional eqs were added */
1182         if (!ibdev->eq_table)
1183                 return;
1184
1185         /* Reset the advertised EQ number */
1186         ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;
1187
1188         /* Free only the added eqs */
1189         for (i = 0; i < ibdev->eq_added; i++) {
1190                 /* Don't free legacy eqs if used */
1191                 if (ibdev->eq_table[i] <= dev->caps.num_comp_vectors)
1192                         continue;
1193                 mlx4_release_eq(dev, ibdev->eq_table[i]);
1194         }
1195
1196         kfree(ibdev->eq_table);
1197 }
1198
1199 static void *mlx4_ib_add(struct mlx4_dev *dev)
1200 {
1201         struct mlx4_ib_dev *ibdev;
1202         int num_ports = 0;
1203         int i, j;
1204         int err;
1205         struct mlx4_ib_iboe *iboe;
1206
1207         pr_info_once("%s", mlx4_ib_version);
1208
1209         if (mlx4_is_mfunc(dev)) {
1210                 pr_warn("IB not yet supported in SRIOV\n");
1211                 return NULL;
1212         }
1213
1214         mlx4_foreach_ib_transport_port(i, dev)
1215                 num_ports++;
1216
1217         /* No point in registering a device with no ports... */
1218         if (num_ports == 0)
1219                 return NULL;
1220
1221         ibdev = (struct mlx4_ib_dev *) ib_alloc_device(sizeof *ibdev);
1222         if (!ibdev) {
1223                 dev_err(&dev->pdev->dev, "Device struct alloc failed\n");
1224                 return NULL;
1225         }
1226
1227         iboe = &ibdev->iboe;
1228
1229         if (mlx4_pd_alloc(dev, &ibdev->priv_pdn))
1230                 goto err_dealloc;
1231
1232         if (mlx4_uar_alloc(dev, &ibdev->priv_uar))
1233                 goto err_pd;
1234
1235         ibdev->uar_map = ioremap((phys_addr_t) ibdev->priv_uar.pfn << PAGE_SHIFT,
1236                                  PAGE_SIZE);
1237         if (!ibdev->uar_map)
1238                 goto err_uar;
1239         MLX4_INIT_DOORBELL_LOCK(&ibdev->uar_lock);
1240
1241         ibdev->dev = dev;
1242
1243         strlcpy(ibdev->ib_dev.name, "mlx4_%d", IB_DEVICE_NAME_MAX);
1244         ibdev->ib_dev.owner             = THIS_MODULE;
1245         ibdev->ib_dev.node_type         = RDMA_NODE_IB_CA;
1246         ibdev->ib_dev.local_dma_lkey    = dev->caps.reserved_lkey;
1247         ibdev->num_ports                = num_ports;
1248         ibdev->ib_dev.phys_port_cnt     = ibdev->num_ports;
1249         ibdev->ib_dev.num_comp_vectors  = dev->caps.num_comp_vectors;
1250         ibdev->ib_dev.dma_device        = &dev->pdev->dev;
1251
1252         ibdev->ib_dev.uverbs_abi_ver    = MLX4_IB_UVERBS_ABI_VERSION;
1253         ibdev->ib_dev.uverbs_cmd_mask   =
1254                 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT)         |
1255                 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)        |
1256                 (1ull << IB_USER_VERBS_CMD_QUERY_PORT)          |
1257                 (1ull << IB_USER_VERBS_CMD_ALLOC_PD)            |
1258                 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD)          |
1259                 (1ull << IB_USER_VERBS_CMD_REG_MR)              |
1260                 (1ull << IB_USER_VERBS_CMD_DEREG_MR)            |
1261                 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
1262                 (1ull << IB_USER_VERBS_CMD_CREATE_CQ)           |
1263                 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ)           |
1264                 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ)          |
1265                 (1ull << IB_USER_VERBS_CMD_CREATE_QP)           |
1266                 (1ull << IB_USER_VERBS_CMD_MODIFY_QP)           |
1267                 (1ull << IB_USER_VERBS_CMD_QUERY_QP)            |
1268                 (1ull << IB_USER_VERBS_CMD_DESTROY_QP)          |
1269                 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)        |
1270                 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST)        |
1271                 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ)          |
1272                 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)          |
1273                 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ)           |
1274                 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)         |
1275                 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)         |
1276                 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
1277
1278         ibdev->ib_dev.query_device      = mlx4_ib_query_device;
1279         ibdev->ib_dev.query_port        = mlx4_ib_query_port;
1280         ibdev->ib_dev.get_link_layer    = mlx4_ib_port_link_layer;
1281         ibdev->ib_dev.query_gid         = mlx4_ib_query_gid;
1282         ibdev->ib_dev.query_pkey        = mlx4_ib_query_pkey;
1283         ibdev->ib_dev.modify_device     = mlx4_ib_modify_device;
1284         ibdev->ib_dev.modify_port       = mlx4_ib_modify_port;
1285         ibdev->ib_dev.alloc_ucontext    = mlx4_ib_alloc_ucontext;
1286         ibdev->ib_dev.dealloc_ucontext  = mlx4_ib_dealloc_ucontext;
1287         ibdev->ib_dev.mmap              = mlx4_ib_mmap;
1288         ibdev->ib_dev.alloc_pd          = mlx4_ib_alloc_pd;
1289         ibdev->ib_dev.dealloc_pd        = mlx4_ib_dealloc_pd;
1290         ibdev->ib_dev.create_ah         = mlx4_ib_create_ah;
1291         ibdev->ib_dev.query_ah          = mlx4_ib_query_ah;
1292         ibdev->ib_dev.destroy_ah        = mlx4_ib_destroy_ah;
1293         ibdev->ib_dev.create_srq        = mlx4_ib_create_srq;
1294         ibdev->ib_dev.modify_srq        = mlx4_ib_modify_srq;
1295         ibdev->ib_dev.query_srq         = mlx4_ib_query_srq;
1296         ibdev->ib_dev.destroy_srq       = mlx4_ib_destroy_srq;
1297         ibdev->ib_dev.post_srq_recv     = mlx4_ib_post_srq_recv;
1298         ibdev->ib_dev.create_qp         = mlx4_ib_create_qp;
1299         ibdev->ib_dev.modify_qp         = mlx4_ib_modify_qp;
1300         ibdev->ib_dev.query_qp          = mlx4_ib_query_qp;
1301         ibdev->ib_dev.destroy_qp        = mlx4_ib_destroy_qp;
1302         ibdev->ib_dev.post_send         = mlx4_ib_post_send;
1303         ibdev->ib_dev.post_recv         = mlx4_ib_post_recv;
1304         ibdev->ib_dev.create_cq         = mlx4_ib_create_cq;
1305         ibdev->ib_dev.modify_cq         = mlx4_ib_modify_cq;
1306         ibdev->ib_dev.resize_cq         = mlx4_ib_resize_cq;
1307         ibdev->ib_dev.destroy_cq        = mlx4_ib_destroy_cq;
1308         ibdev->ib_dev.poll_cq           = mlx4_ib_poll_cq;
1309         ibdev->ib_dev.req_notify_cq     = mlx4_ib_arm_cq;
1310         ibdev->ib_dev.get_dma_mr        = mlx4_ib_get_dma_mr;
1311         ibdev->ib_dev.reg_user_mr       = mlx4_ib_reg_user_mr;
1312         ibdev->ib_dev.dereg_mr          = mlx4_ib_dereg_mr;
1313         ibdev->ib_dev.alloc_fast_reg_mr = mlx4_ib_alloc_fast_reg_mr;
1314         ibdev->ib_dev.alloc_fast_reg_page_list = mlx4_ib_alloc_fast_reg_page_list;
1315         ibdev->ib_dev.free_fast_reg_page_list  = mlx4_ib_free_fast_reg_page_list;
1316         ibdev->ib_dev.attach_mcast      = mlx4_ib_mcg_attach;
1317         ibdev->ib_dev.detach_mcast      = mlx4_ib_mcg_detach;
1318         ibdev->ib_dev.process_mad       = mlx4_ib_process_mad;
1319
1320         ibdev->ib_dev.alloc_fmr         = mlx4_ib_fmr_alloc;
1321         ibdev->ib_dev.map_phys_fmr      = mlx4_ib_map_phys_fmr;
1322         ibdev->ib_dev.unmap_fmr         = mlx4_ib_unmap_fmr;
1323         ibdev->ib_dev.dealloc_fmr       = mlx4_ib_fmr_dealloc;
1324
1325         if (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) {
1326                 ibdev->ib_dev.alloc_xrcd = mlx4_ib_alloc_xrcd;
1327                 ibdev->ib_dev.dealloc_xrcd = mlx4_ib_dealloc_xrcd;
1328                 ibdev->ib_dev.uverbs_cmd_mask |=
1329                         (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
1330                         (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
1331         }
1332
1333         mlx4_ib_alloc_eqs(dev, ibdev);
1334
1335         spin_lock_init(&iboe->lock);
1336
1337         if (init_node_data(ibdev))
1338                 goto err_map;
1339
1340         for (i = 0; i < ibdev->num_ports; ++i) {
1341                 if (mlx4_ib_port_link_layer(&ibdev->ib_dev, i + 1) ==
1342                                                 IB_LINK_LAYER_ETHERNET) {
1343                         err = mlx4_counter_alloc(ibdev->dev, &ibdev->counters[i]);
1344                         if (err)
1345                                 ibdev->counters[i] = -1;
1346                 } else
1347                                 ibdev->counters[i] = -1;
1348         }
1349
1350         spin_lock_init(&ibdev->sm_lock);
1351         mutex_init(&ibdev->cap_mask_mutex);
1352
1353         if (ib_register_device(&ibdev->ib_dev, NULL))
1354                 goto err_counter;
1355
1356         if (mlx4_ib_mad_init(ibdev))
1357                 goto err_reg;
1358
1359         if (dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE && !iboe->nb.notifier_call) {
1360                 iboe->nb.notifier_call = mlx4_ib_netdev_event;
1361                 err = register_netdevice_notifier(&iboe->nb);
1362                 if (err)
1363                         goto err_reg;
1364         }
1365
1366         for (j = 0; j < ARRAY_SIZE(mlx4_class_attributes); ++j) {
1367                 if (device_create_file(&ibdev->ib_dev.dev,
1368                                        mlx4_class_attributes[j]))
1369                         goto err_notif;
1370         }
1371
1372         ibdev->ib_active = true;
1373
1374         return ibdev;
1375
1376 err_notif:
1377         if (unregister_netdevice_notifier(&ibdev->iboe.nb))
1378                 pr_warn("failure unregistering notifier\n");
1379         flush_workqueue(wq);
1380
1381 err_reg:
1382         ib_unregister_device(&ibdev->ib_dev);
1383
1384 err_counter:
1385         for (; i; --i)
1386                 if (ibdev->counters[i - 1] != -1)
1387                         mlx4_counter_free(ibdev->dev, ibdev->counters[i - 1]);
1388
1389 err_map:
1390         iounmap(ibdev->uar_map);
1391
1392 err_uar:
1393         mlx4_uar_free(dev, &ibdev->priv_uar);
1394
1395 err_pd:
1396         mlx4_pd_free(dev, ibdev->priv_pdn);
1397
1398 err_dealloc:
1399         ib_dealloc_device(&ibdev->ib_dev);
1400
1401         return NULL;
1402 }
1403
1404 static void mlx4_ib_remove(struct mlx4_dev *dev, void *ibdev_ptr)
1405 {
1406         struct mlx4_ib_dev *ibdev = ibdev_ptr;
1407         int p;
1408
1409         mlx4_ib_mad_cleanup(ibdev);
1410         ib_unregister_device(&ibdev->ib_dev);
1411         if (ibdev->iboe.nb.notifier_call) {
1412                 if (unregister_netdevice_notifier(&ibdev->iboe.nb))
1413                         pr_warn("failure unregistering notifier\n");
1414                 ibdev->iboe.nb.notifier_call = NULL;
1415         }
1416         iounmap(ibdev->uar_map);
1417         for (p = 0; p < ibdev->num_ports; ++p)
1418                 if (ibdev->counters[p] != -1)
1419                         mlx4_counter_free(ibdev->dev, ibdev->counters[p]);
1420         mlx4_foreach_port(p, dev, MLX4_PORT_TYPE_IB)
1421                 mlx4_CLOSE_PORT(dev, p);
1422
1423         mlx4_ib_free_eqs(dev, ibdev);
1424
1425         mlx4_uar_free(dev, &ibdev->priv_uar);
1426         mlx4_pd_free(dev, ibdev->priv_pdn);
1427         ib_dealloc_device(&ibdev->ib_dev);
1428 }
1429
1430 static void mlx4_ib_event(struct mlx4_dev *dev, void *ibdev_ptr,
1431                           enum mlx4_dev_event event, unsigned long param)
1432 {
1433         struct ib_event ibev;
1434         struct mlx4_ib_dev *ibdev = to_mdev((struct ib_device *) ibdev_ptr);
1435         struct mlx4_eqe *eqe = NULL;
1436         struct ib_event_work *ew;
1437         int port = 0;
1438
1439         if (event == MLX4_DEV_EVENT_PORT_MGMT_CHANGE)
1440                 eqe = (struct mlx4_eqe *)param;
1441         else
1442                 port = (u8)param;
1443
1444         if (port > ibdev->num_ports)
1445                 return;
1446
1447         switch (event) {
1448         case MLX4_DEV_EVENT_PORT_UP:
1449                 ibev.event = IB_EVENT_PORT_ACTIVE;
1450                 break;
1451
1452         case MLX4_DEV_EVENT_PORT_DOWN:
1453                 ibev.event = IB_EVENT_PORT_ERR;
1454                 break;
1455
1456         case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
1457                 ibdev->ib_active = false;
1458                 ibev.event = IB_EVENT_DEVICE_FATAL;
1459                 break;
1460
1461         case MLX4_DEV_EVENT_PORT_MGMT_CHANGE:
1462                 ew = kmalloc(sizeof *ew, GFP_ATOMIC);
1463                 if (!ew) {
1464                         pr_err("failed to allocate memory for events work\n");
1465                         break;
1466                 }
1467
1468                 INIT_WORK(&ew->work, handle_port_mgmt_change_event);
1469                 memcpy(&ew->ib_eqe, eqe, sizeof *eqe);
1470                 ew->ib_dev = ibdev;
1471                 handle_port_mgmt_change_event(&ew->work);
1472                 return;
1473
1474         default:
1475                 return;
1476         }
1477
1478         ibev.device           = ibdev_ptr;
1479         ibev.element.port_num = port;
1480
1481         ib_dispatch_event(&ibev);
1482 }
1483
1484 static struct mlx4_interface mlx4_ib_interface = {
1485         .add            = mlx4_ib_add,
1486         .remove         = mlx4_ib_remove,
1487         .event          = mlx4_ib_event,
1488         .protocol       = MLX4_PROT_IB_IPV6
1489 };
1490
1491 static int __init mlx4_ib_init(void)
1492 {
1493         int err;
1494
1495         wq = create_singlethread_workqueue("mlx4_ib");
1496         if (!wq)
1497                 return -ENOMEM;
1498
1499         err = mlx4_register_interface(&mlx4_ib_interface);
1500         if (err) {
1501                 destroy_workqueue(wq);
1502                 return err;
1503         }
1504
1505         return 0;
1506 }
1507
1508 static void __exit mlx4_ib_cleanup(void)
1509 {
1510         mlx4_unregister_interface(&mlx4_ib_interface);
1511         destroy_workqueue(wq);
1512 }
1513
1514 module_init(mlx4_ib_init);
1515 module_exit(mlx4_ib_cleanup);