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