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