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