blob: abce99ed565f74013faad73f2307fe978487b2bf [file] [log] [blame]
Roland Dreier225c7b12007-05-08 18:00:38 -07001/*
2 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
Jack Morgenstein51a379d2008-07-25 10:32:52 -07003 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
Roland Dreier225c7b12007-05-08 18:00:38 -07004 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070037#include <linux/errno.h>
Eli Cohenfa417f72010-10-24 21:08:52 -070038#include <linux/netdevice.h>
39#include <linux/inetdevice.h>
40#include <linux/rtnetlink.h>
Eli Cohen4c3eb3c2010-08-26 17:19:22 +030041#include <linux/if_vlan.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070042
43#include <rdma/ib_smi.h>
44#include <rdma/ib_user_verbs.h>
Eli Cohenfa417f72010-10-24 21:08:52 -070045#include <rdma/ib_addr.h>
Roland Dreier225c7b12007-05-08 18:00:38 -070046
47#include <linux/mlx4/driver.h>
48#include <linux/mlx4/cmd.h>
49
50#include "mlx4_ib.h"
51#include "user.h"
52
53#define DRV_NAME "mlx4_ib"
Jack Morgenstein068c4ea2008-04-16 21:09:35 -070054#define DRV_VERSION "1.0"
55#define DRV_RELDATE "April 4, 2008"
Roland Dreier225c7b12007-05-08 18:00:38 -070056
57MODULE_AUTHOR("Roland Dreier");
58MODULE_DESCRIPTION("Mellanox ConnectX HCA InfiniBand driver");
59MODULE_LICENSE("Dual BSD/GPL");
60MODULE_VERSION(DRV_VERSION);
61
Roland Dreier68f39482008-02-04 20:20:44 -080062static const char mlx4_ib_version[] =
Roland Dreier225c7b12007-05-08 18:00:38 -070063 DRV_NAME ": Mellanox ConnectX InfiniBand driver v"
64 DRV_VERSION " (" DRV_RELDATE ")\n";
65
Eli Cohenfa417f72010-10-24 21:08:52 -070066struct 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
Roland Dreier225c7b12007-05-08 18:00:38 -070075static 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
Eli Cohen4c3eb3c2010-08-26 17:19:22 +030083static union ib_gid zgid;
84
Roland Dreier225c7b12007-05-08 18:00:38 -070085static int mlx4_ib_query_device(struct ib_device *ibdev,
86 struct ib_device_attr *props)
87{
88 struct mlx4_ib_dev *dev = to_mdev(ibdev);
89 struct ib_smp *in_mad = NULL;
90 struct ib_smp *out_mad = NULL;
91 int err = -ENOMEM;
92
93 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
94 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
95 if (!in_mad || !out_mad)
96 goto out;
97
98 init_query_mad(in_mad);
99 in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
100
101 err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, 1, NULL, NULL, in_mad, out_mad);
102 if (err)
103 goto out;
104
105 memset(props, 0, sizeof *props);
106
107 props->fw_ver = dev->dev->caps.fw_ver;
108 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
109 IB_DEVICE_PORT_ACTIVE_EVENT |
110 IB_DEVICE_SYS_IMAGE_GUID |
Ron Livne521e5752008-07-14 23:48:48 -0700111 IB_DEVICE_RC_RNR_NAK_GEN |
112 IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Roland Dreier225c7b12007-05-08 18:00:38 -0700113 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_PKEY_CNTR)
114 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
115 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BAD_QKEY_CNTR)
116 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
117 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_APM)
118 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
119 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UD_AV_PORT)
120 props->device_cap_flags |= IB_DEVICE_UD_AV_PORT_ENFORCE;
Eli Cohen8ff095e2008-04-16 21:01:10 -0700121 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_IPOIB_CSUM)
122 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
Eli Cohen417608c2009-11-12 11:19:44 -0800123 if (dev->dev->caps.max_gso_sz && dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BLH)
Eli Cohenb832be12008-04-16 21:09:27 -0700124 props->device_cap_flags |= IB_DEVICE_UD_TSO;
Roland Dreier95d04f02008-07-23 08:12:26 -0700125 if (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_RESERVED_LKEY)
126 props->device_cap_flags |= IB_DEVICE_LOCAL_DMA_LKEY;
127 if ((dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_LOCAL_INV) &&
128 (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_REMOTE_INV) &&
129 (dev->dev->caps.bmme_flags & MLX4_BMME_FLAG_FAST_REG_WR))
130 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Sean Hefty0a1405d2011-06-02 11:32:15 -0700131 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC)
132 props->device_cap_flags |= IB_DEVICE_XRC;
Roland Dreier225c7b12007-05-08 18:00:38 -0700133
134 props->vendor_id = be32_to_cpup((__be32 *) (out_mad->data + 36)) &
135 0xffffff;
136 props->vendor_part_id = be16_to_cpup((__be16 *) (out_mad->data + 30));
137 props->hw_ver = be32_to_cpup((__be32 *) (out_mad->data + 32));
138 memcpy(&props->sys_image_guid, out_mad->data + 4, 8);
139
140 props->max_mr_size = ~0ull;
141 props->page_size_cap = dev->dev->caps.page_size_cap;
142 props->max_qp = dev->dev->caps.num_qps - dev->dev->caps.reserved_qps;
143 props->max_qp_wr = dev->dev->caps.max_wqes;
144 props->max_sge = min(dev->dev->caps.max_sq_sg,
145 dev->dev->caps.max_rq_sg);
146 props->max_cq = dev->dev->caps.num_cqs - dev->dev->caps.reserved_cqs;
147 props->max_cqe = dev->dev->caps.max_cqes;
148 props->max_mr = dev->dev->caps.num_mpts - dev->dev->caps.reserved_mrws;
149 props->max_pd = dev->dev->caps.num_pds - dev->dev->caps.reserved_pds;
150 props->max_qp_rd_atom = dev->dev->caps.max_qp_dest_rdma;
151 props->max_qp_init_rd_atom = dev->dev->caps.max_qp_init_rdma;
152 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
153 props->max_srq = dev->dev->caps.num_srqs - dev->dev->caps.reserved_srqs;
Jack Morgensteinc8681f12007-06-21 13:39:10 -0700154 props->max_srq_wr = dev->dev->caps.max_srq_wqes - 1;
Roland Dreier225c7b12007-05-08 18:00:38 -0700155 props->max_srq_sge = dev->dev->caps.max_srq_sge;
Eli Cohen5a0fd092010-10-07 16:24:16 +0200156 props->max_fast_reg_page_list_len = MLX4_MAX_FAST_REG_PAGES;
Roland Dreier225c7b12007-05-08 18:00:38 -0700157 props->local_ca_ack_delay = dev->dev->caps.local_ca_ack_delay;
158 props->atomic_cap = dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_ATOMIC ?
159 IB_ATOMIC_HCA : IB_ATOMIC_NONE;
Vladimir Sokolovsky6fa8f712010-04-14 17:23:39 +0300160 props->masked_atomic_cap = IB_ATOMIC_HCA;
Roland Dreier5ae2a7a2007-06-18 08:15:02 -0700161 props->max_pkeys = dev->dev->caps.pkey_table_len[1];
Roland Dreier225c7b12007-05-08 18:00:38 -0700162 props->max_mcast_grp = dev->dev->caps.num_mgms + dev->dev->caps.num_amgms;
163 props->max_mcast_qp_attach = dev->dev->caps.num_qp_per_mgm;
164 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
165 props->max_mcast_grp;
166 props->max_map_per_fmr = (1 << (32 - ilog2(dev->dev->caps.num_mpts))) - 1;
167
168out:
169 kfree(in_mad);
170 kfree(out_mad);
171
172 return err;
173}
174
Eli Cohenfa417f72010-10-24 21:08:52 -0700175static enum rdma_link_layer
176mlx4_ib_port_link_layer(struct ib_device *device, u8 port_num)
177{
178 struct mlx4_dev *dev = to_mdev(device)->dev;
179
Jack Morgenstein65dab252011-12-13 04:10:41 +0000180 return dev->caps.port_mask[port_num] == MLX4_PORT_TYPE_IB ?
Eli Cohenfa417f72010-10-24 21:08:52 -0700181 IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
182}
183
184static int ib_link_query_port(struct ib_device *ibdev, u8 port,
185 struct ib_port_attr *props,
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300186 struct ib_smp *in_mad,
Eli Cohenfa417f72010-10-24 21:08:52 -0700187 struct ib_smp *out_mad)
188{
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300189 int ext_active_speed;
190 int err;
191
Eli Cohenfa417f72010-10-24 21:08:52 -0700192 props->lid = be16_to_cpup((__be16 *) (out_mad->data + 16));
193 props->lmc = out_mad->data[34] & 0x7;
194 props->sm_lid = be16_to_cpup((__be16 *) (out_mad->data + 18));
195 props->sm_sl = out_mad->data[36] & 0xf;
196 props->state = out_mad->data[32] & 0xf;
197 props->phys_state = out_mad->data[33] >> 4;
198 props->port_cap_flags = be32_to_cpup((__be32 *) (out_mad->data + 20));
199 props->gid_tbl_len = to_mdev(ibdev)->dev->caps.gid_table_len[port];
200 props->max_msg_sz = to_mdev(ibdev)->dev->caps.max_msg_sz;
201 props->pkey_tbl_len = to_mdev(ibdev)->dev->caps.pkey_table_len[port];
202 props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46));
203 props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));
204 props->active_width = out_mad->data[31] & 0xf;
205 props->active_speed = out_mad->data[35] >> 4;
206 props->max_mtu = out_mad->data[41] & 0xf;
207 props->active_mtu = out_mad->data[36] >> 4;
208 props->subnet_timeout = out_mad->data[51] & 0x1f;
209 props->max_vl_num = out_mad->data[37] >> 4;
210 props->init_type_reply = out_mad->data[41] >> 4;
211
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300212 /* Check if extended speeds (EDR/FDR/...) are supported */
213 if (props->port_cap_flags & IB_PORT_EXTENDED_SPEEDS_SUP) {
214 ext_active_speed = out_mad->data[62] >> 4;
215
216 switch (ext_active_speed) {
217 case 1:
Or Gerlitz2e966912012-02-28 18:49:50 +0200218 props->active_speed = IB_SPEED_FDR;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300219 break;
220 case 2:
Or Gerlitz2e966912012-02-28 18:49:50 +0200221 props->active_speed = IB_SPEED_EDR;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300222 break;
223 }
224 }
225
226 /* If reported active speed is QDR, check if is FDR-10 */
Or Gerlitz2e966912012-02-28 18:49:50 +0200227 if (props->active_speed == IB_SPEED_QDR) {
Or Gerlitz8154c072012-03-06 15:50:50 +0200228 init_query_mad(in_mad);
229 in_mad->attr_id = MLX4_ATTR_EXTENDED_PORT_INFO;
230 in_mad->attr_mod = cpu_to_be32(port);
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300231
Or Gerlitz8154c072012-03-06 15:50:50 +0200232 err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port,
233 NULL, NULL, in_mad, out_mad);
234 if (err)
235 return err;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300236
Or Gerlitz8154c072012-03-06 15:50:50 +0200237 /* Checking LinkSpeedActive for FDR-10 */
238 if (out_mad->data[15] & 0x1)
239 props->active_speed = IB_SPEED_FDR10;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300240 }
241
Eli Cohenfa417f72010-10-24 21:08:52 -0700242 return 0;
243}
244
245static u8 state_to_phys_state(enum ib_port_state state)
246{
247 return state == IB_PORT_ACTIVE ? 5 : 3;
248}
249
250static int eth_link_query_port(struct ib_device *ibdev, u8 port,
251 struct ib_port_attr *props,
252 struct ib_smp *out_mad)
253{
254 struct mlx4_ib_iboe *iboe = &to_mdev(ibdev)->iboe;
255 struct net_device *ndev;
256 enum ib_mtu tmp;
257
Eli Cohen328266c2010-11-15 14:04:39 +0000258 props->active_width = IB_WIDTH_1X;
Or Gerlitz2e966912012-02-28 18:49:50 +0200259 props->active_speed = IB_SPEED_QDR;
Eli Cohenfa417f72010-10-24 21:08:52 -0700260 props->port_cap_flags = IB_PORT_CM_SUP;
261 props->gid_tbl_len = to_mdev(ibdev)->dev->caps.gid_table_len[port];
262 props->max_msg_sz = to_mdev(ibdev)->dev->caps.max_msg_sz;
263 props->pkey_tbl_len = 1;
264 props->bad_pkey_cntr = be16_to_cpup((__be16 *) (out_mad->data + 46));
265 props->qkey_viol_cntr = be16_to_cpup((__be16 *) (out_mad->data + 48));
Or Gerlitzbcacb892011-10-10 10:53:41 +0200266 props->max_mtu = IB_MTU_4096;
Eli Cohenfa417f72010-10-24 21:08:52 -0700267 props->subnet_timeout = 0;
268 props->max_vl_num = out_mad->data[37] >> 4;
269 props->init_type_reply = 0;
270 props->state = IB_PORT_DOWN;
271 props->phys_state = state_to_phys_state(props->state);
272 props->active_mtu = IB_MTU_256;
273 spin_lock(&iboe->lock);
274 ndev = iboe->netdevs[port - 1];
275 if (!ndev)
276 goto out;
277
278 tmp = iboe_get_mtu(ndev->mtu);
279 props->active_mtu = tmp ? min(props->max_mtu, tmp) : IB_MTU_256;
280
Eli Cohen21d606092010-11-11 21:05:58 +0000281 props->state = (netif_running(ndev) && netif_carrier_ok(ndev)) ?
Eli Cohenfa417f72010-10-24 21:08:52 -0700282 IB_PORT_ACTIVE : IB_PORT_DOWN;
283 props->phys_state = state_to_phys_state(props->state);
284
285out:
286 spin_unlock(&iboe->lock);
287 return 0;
288}
289
Roland Dreier225c7b12007-05-08 18:00:38 -0700290static int mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
291 struct ib_port_attr *props)
292{
293 struct ib_smp *in_mad = NULL;
294 struct ib_smp *out_mad = NULL;
295 int err = -ENOMEM;
296
297 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
298 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
299 if (!in_mad || !out_mad)
300 goto out;
301
302 memset(props, 0, sizeof *props);
303
304 init_query_mad(in_mad);
305 in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
306 in_mad->attr_mod = cpu_to_be32(port);
307
308 err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
309 if (err)
310 goto out;
311
Eli Cohenfa417f72010-10-24 21:08:52 -0700312 err = mlx4_ib_port_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND ?
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300313 ib_link_query_port(ibdev, port, props, in_mad, out_mad) :
Eli Cohenfa417f72010-10-24 21:08:52 -0700314 eth_link_query_port(ibdev, port, props, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -0700315
316out:
317 kfree(in_mad);
318 kfree(out_mad);
319
320 return err;
321}
322
Eli Cohenfa417f72010-10-24 21:08:52 -0700323static int __mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
324 union ib_gid *gid)
Roland Dreier225c7b12007-05-08 18:00:38 -0700325{
326 struct ib_smp *in_mad = NULL;
327 struct ib_smp *out_mad = NULL;
328 int err = -ENOMEM;
329
330 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
331 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
332 if (!in_mad || !out_mad)
333 goto out;
334
335 init_query_mad(in_mad);
336 in_mad->attr_id = IB_SMP_ATTR_PORT_INFO;
337 in_mad->attr_mod = cpu_to_be32(port);
338
339 err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
340 if (err)
341 goto out;
342
343 memcpy(gid->raw, out_mad->data + 8, 8);
344
345 init_query_mad(in_mad);
346 in_mad->attr_id = IB_SMP_ATTR_GUID_INFO;
347 in_mad->attr_mod = cpu_to_be32(index / 8);
348
349 err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
350 if (err)
351 goto out;
352
353 memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
354
355out:
356 kfree(in_mad);
357 kfree(out_mad);
358 return err;
359}
360
Eli Cohenfa417f72010-10-24 21:08:52 -0700361static int iboe_query_gid(struct ib_device *ibdev, u8 port, int index,
362 union ib_gid *gid)
363{
364 struct mlx4_ib_dev *dev = to_mdev(ibdev);
365
366 *gid = dev->iboe.gid_table[port - 1][index];
367
368 return 0;
369}
370
371static int mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
372 union ib_gid *gid)
373{
374 if (rdma_port_get_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND)
375 return __mlx4_ib_query_gid(ibdev, port, index, gid);
376 else
377 return iboe_query_gid(ibdev, port, index, gid);
378}
379
Roland Dreier225c7b12007-05-08 18:00:38 -0700380static int mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
381 u16 *pkey)
382{
383 struct ib_smp *in_mad = NULL;
384 struct ib_smp *out_mad = NULL;
385 int err = -ENOMEM;
386
387 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
388 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
389 if (!in_mad || !out_mad)
390 goto out;
391
392 init_query_mad(in_mad);
393 in_mad->attr_id = IB_SMP_ATTR_PKEY_TABLE;
394 in_mad->attr_mod = cpu_to_be32(index / 32);
395
396 err = mlx4_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
397 if (err)
398 goto out;
399
400 *pkey = be16_to_cpu(((__be16 *) out_mad->data)[index % 32]);
401
402out:
403 kfree(in_mad);
404 kfree(out_mad);
405 return err;
406}
407
408static int mlx4_ib_modify_device(struct ib_device *ibdev, int mask,
409 struct ib_device_modify *props)
410{
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000411 struct mlx4_cmd_mailbox *mailbox;
412
Roland Dreier225c7b12007-05-08 18:00:38 -0700413 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
414 return -EOPNOTSUPP;
415
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000416 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
417 return 0;
418
419 spin_lock(&to_mdev(ibdev)->sm_lock);
420 memcpy(ibdev->node_desc, props->node_desc, 64);
421 spin_unlock(&to_mdev(ibdev)->sm_lock);
422
423 /*
424 * If possible, pass node desc to FW, so it can generate
425 * a 144 trap. If cmd fails, just ignore.
426 */
427 mailbox = mlx4_alloc_cmd_mailbox(to_mdev(ibdev)->dev);
428 if (IS_ERR(mailbox))
429 return 0;
430
431 memset(mailbox->buf, 0, 256);
432 memcpy(mailbox->buf, props->node_desc, 64);
433 mlx4_cmd(to_mdev(ibdev)->dev, mailbox->dma, 1, 0,
Jack Morgensteinf9baff52011-12-13 04:10:51 +0000434 MLX4_CMD_SET_NODE, MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000435
436 mlx4_free_cmd_mailbox(to_mdev(ibdev)->dev, mailbox);
Roland Dreier225c7b12007-05-08 18:00:38 -0700437
438 return 0;
439}
440
441static int mlx4_SET_PORT(struct mlx4_ib_dev *dev, u8 port, int reset_qkey_viols,
442 u32 cap_mask)
443{
444 struct mlx4_cmd_mailbox *mailbox;
445 int err;
Eli Cohenfa417f72010-10-24 21:08:52 -0700446 u8 is_eth = dev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
Roland Dreier225c7b12007-05-08 18:00:38 -0700447
448 mailbox = mlx4_alloc_cmd_mailbox(dev->dev);
449 if (IS_ERR(mailbox))
450 return PTR_ERR(mailbox);
451
452 memset(mailbox->buf, 0, 256);
Roland Dreier5ae2a7a2007-06-18 08:15:02 -0700453
454 if (dev->dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
455 *(u8 *) mailbox->buf = !!reset_qkey_viols << 6;
456 ((__be32 *) mailbox->buf)[2] = cpu_to_be32(cap_mask);
457 } else {
458 ((u8 *) mailbox->buf)[3] = !!reset_qkey_viols;
459 ((__be32 *) mailbox->buf)[1] = cpu_to_be32(cap_mask);
460 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700461
Eli Cohenfa417f72010-10-24 21:08:52 -0700462 err = mlx4_cmd(dev->dev, mailbox->dma, port, is_eth, MLX4_CMD_SET_PORT,
Jack Morgensteinf9baff52011-12-13 04:10:51 +0000463 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
Roland Dreier225c7b12007-05-08 18:00:38 -0700464
465 mlx4_free_cmd_mailbox(dev->dev, mailbox);
466 return err;
467}
468
469static int mlx4_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
470 struct ib_port_modify *props)
471{
472 struct ib_port_attr attr;
473 u32 cap_mask;
474 int err;
475
476 mutex_lock(&to_mdev(ibdev)->cap_mask_mutex);
477
478 err = mlx4_ib_query_port(ibdev, port, &attr);
479 if (err)
480 goto out;
481
482 cap_mask = (attr.port_cap_flags | props->set_port_cap_mask) &
483 ~props->clr_port_cap_mask;
484
485 err = mlx4_SET_PORT(to_mdev(ibdev), port,
486 !!(mask & IB_PORT_RESET_QKEY_CNTR),
487 cap_mask);
488
489out:
490 mutex_unlock(&to_mdev(ibdev)->cap_mask_mutex);
491 return err;
492}
493
494static struct ib_ucontext *mlx4_ib_alloc_ucontext(struct ib_device *ibdev,
495 struct ib_udata *udata)
496{
497 struct mlx4_ib_dev *dev = to_mdev(ibdev);
498 struct mlx4_ib_ucontext *context;
499 struct mlx4_ib_alloc_ucontext_resp resp;
500 int err;
501
Jack Morgenstein3b4a8cd2009-09-05 20:24:50 -0700502 if (!dev->ib_active)
503 return ERR_PTR(-EAGAIN);
504
Roland Dreier225c7b12007-05-08 18:00:38 -0700505 resp.qp_tab_size = dev->dev->caps.num_qps;
506 resp.bf_reg_size = dev->dev->caps.bf_reg_size;
507 resp.bf_regs_per_page = dev->dev->caps.bf_regs_per_page;
508
509 context = kmalloc(sizeof *context, GFP_KERNEL);
510 if (!context)
511 return ERR_PTR(-ENOMEM);
512
513 err = mlx4_uar_alloc(to_mdev(ibdev)->dev, &context->uar);
514 if (err) {
515 kfree(context);
516 return ERR_PTR(err);
517 }
518
519 INIT_LIST_HEAD(&context->db_page_list);
520 mutex_init(&context->db_page_mutex);
521
522 err = ib_copy_to_udata(udata, &resp, sizeof resp);
523 if (err) {
524 mlx4_uar_free(to_mdev(ibdev)->dev, &context->uar);
525 kfree(context);
526 return ERR_PTR(-EFAULT);
527 }
528
529 return &context->ibucontext;
530}
531
532static int mlx4_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
533{
534 struct mlx4_ib_ucontext *context = to_mucontext(ibcontext);
535
536 mlx4_uar_free(to_mdev(ibcontext->device)->dev, &context->uar);
537 kfree(context);
538
539 return 0;
540}
541
542static int mlx4_ib_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
543{
544 struct mlx4_ib_dev *dev = to_mdev(context->device);
545
546 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
547 return -EINVAL;
548
549 if (vma->vm_pgoff == 0) {
550 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
551
552 if (io_remap_pfn_range(vma, vma->vm_start,
553 to_mucontext(context)->uar.pfn,
554 PAGE_SIZE, vma->vm_page_prot))
555 return -EAGAIN;
556 } else if (vma->vm_pgoff == 1 && dev->dev->caps.bf_reg_size != 0) {
Roland Dreiere1d60ec2009-03-30 08:31:05 -0700557 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
Roland Dreier225c7b12007-05-08 18:00:38 -0700558
559 if (io_remap_pfn_range(vma, vma->vm_start,
560 to_mucontext(context)->uar.pfn +
561 dev->dev->caps.num_uars,
562 PAGE_SIZE, vma->vm_page_prot))
563 return -EAGAIN;
564 } else
565 return -EINVAL;
566
567 return 0;
568}
569
570static struct ib_pd *mlx4_ib_alloc_pd(struct ib_device *ibdev,
571 struct ib_ucontext *context,
572 struct ib_udata *udata)
573{
574 struct mlx4_ib_pd *pd;
575 int err;
576
577 pd = kmalloc(sizeof *pd, GFP_KERNEL);
578 if (!pd)
579 return ERR_PTR(-ENOMEM);
580
581 err = mlx4_pd_alloc(to_mdev(ibdev)->dev, &pd->pdn);
582 if (err) {
583 kfree(pd);
584 return ERR_PTR(err);
585 }
586
587 if (context)
588 if (ib_copy_to_udata(udata, &pd->pdn, sizeof (__u32))) {
589 mlx4_pd_free(to_mdev(ibdev)->dev, pd->pdn);
590 kfree(pd);
591 return ERR_PTR(-EFAULT);
592 }
593
594 return &pd->ibpd;
595}
596
597static int mlx4_ib_dealloc_pd(struct ib_pd *pd)
598{
599 mlx4_pd_free(to_mdev(pd->device)->dev, to_mpd(pd)->pdn);
600 kfree(pd);
601
602 return 0;
603}
604
Sean Hefty012a8ff2011-06-02 09:01:33 -0700605static struct ib_xrcd *mlx4_ib_alloc_xrcd(struct ib_device *ibdev,
606 struct ib_ucontext *context,
607 struct ib_udata *udata)
608{
609 struct mlx4_ib_xrcd *xrcd;
610 int err;
611
612 if (!(to_mdev(ibdev)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC))
613 return ERR_PTR(-ENOSYS);
614
615 xrcd = kmalloc(sizeof *xrcd, GFP_KERNEL);
616 if (!xrcd)
617 return ERR_PTR(-ENOMEM);
618
619 err = mlx4_xrcd_alloc(to_mdev(ibdev)->dev, &xrcd->xrcdn);
620 if (err)
621 goto err1;
622
623 xrcd->pd = ib_alloc_pd(ibdev);
624 if (IS_ERR(xrcd->pd)) {
625 err = PTR_ERR(xrcd->pd);
626 goto err2;
627 }
628
629 xrcd->cq = ib_create_cq(ibdev, NULL, NULL, xrcd, 1, 0);
630 if (IS_ERR(xrcd->cq)) {
631 err = PTR_ERR(xrcd->cq);
632 goto err3;
633 }
634
635 return &xrcd->ibxrcd;
636
637err3:
638 ib_dealloc_pd(xrcd->pd);
639err2:
640 mlx4_xrcd_free(to_mdev(ibdev)->dev, xrcd->xrcdn);
641err1:
642 kfree(xrcd);
643 return ERR_PTR(err);
644}
645
646static int mlx4_ib_dealloc_xrcd(struct ib_xrcd *xrcd)
647{
648 ib_destroy_cq(to_mxrcd(xrcd)->cq);
649 ib_dealloc_pd(to_mxrcd(xrcd)->pd);
650 mlx4_xrcd_free(to_mdev(xrcd->device)->dev, to_mxrcd(xrcd)->xrcdn);
651 kfree(xrcd);
652
653 return 0;
654}
655
Eli Cohenfa417f72010-10-24 21:08:52 -0700656static int add_gid_entry(struct ib_qp *ibqp, union ib_gid *gid)
657{
658 struct mlx4_ib_qp *mqp = to_mqp(ibqp);
659 struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
660 struct mlx4_ib_gid_entry *ge;
661
662 ge = kzalloc(sizeof *ge, GFP_KERNEL);
663 if (!ge)
664 return -ENOMEM;
665
666 ge->gid = *gid;
667 if (mlx4_ib_add_mc(mdev, mqp, gid)) {
668 ge->port = mqp->port;
669 ge->added = 1;
670 }
671
672 mutex_lock(&mqp->mutex);
673 list_add_tail(&ge->list, &mqp->gid_list);
674 mutex_unlock(&mqp->mutex);
675
676 return 0;
677}
678
679int mlx4_ib_add_mc(struct mlx4_ib_dev *mdev, struct mlx4_ib_qp *mqp,
680 union ib_gid *gid)
681{
682 u8 mac[6];
683 struct net_device *ndev;
684 int ret = 0;
685
686 if (!mqp->port)
687 return 0;
688
689 spin_lock(&mdev->iboe.lock);
690 ndev = mdev->iboe.netdevs[mqp->port - 1];
691 if (ndev)
692 dev_hold(ndev);
693 spin_unlock(&mdev->iboe.lock);
694
695 if (ndev) {
696 rdma_get_mcast_mac((struct in6_addr *)gid, mac);
697 rtnl_lock();
698 dev_mc_add(mdev->iboe.netdevs[mqp->port - 1], mac);
699 ret = 1;
700 rtnl_unlock();
701 dev_put(ndev);
702 }
703
704 return ret;
705}
706
Roland Dreier225c7b12007-05-08 18:00:38 -0700707static int mlx4_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
708{
Eli Cohenfa417f72010-10-24 21:08:52 -0700709 int err;
710 struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
711 struct mlx4_ib_qp *mqp = to_mqp(ibqp);
712
Aleksey Seninda995a82010-12-02 11:44:49 +0000713 err = mlx4_multicast_attach(mdev->dev, &mqp->mqp, gid->raw,
714 !!(mqp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK),
Yevgeny Petrilin03455842011-03-22 22:38:17 +0000715 MLX4_PROT_IB_IPV6);
Eli Cohenfa417f72010-10-24 21:08:52 -0700716 if (err)
717 return err;
718
719 err = add_gid_entry(ibqp, gid);
720 if (err)
721 goto err_add;
722
723 return 0;
724
725err_add:
Yevgeny Petrilin03455842011-03-22 22:38:17 +0000726 mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw, MLX4_PROT_IB_IPV6);
Eli Cohenfa417f72010-10-24 21:08:52 -0700727 return err;
728}
729
730static struct mlx4_ib_gid_entry *find_gid_entry(struct mlx4_ib_qp *qp, u8 *raw)
731{
732 struct mlx4_ib_gid_entry *ge;
733 struct mlx4_ib_gid_entry *tmp;
734 struct mlx4_ib_gid_entry *ret = NULL;
735
736 list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
737 if (!memcmp(raw, ge->gid.raw, 16)) {
738 ret = ge;
739 break;
740 }
741 }
742
743 return ret;
Roland Dreier225c7b12007-05-08 18:00:38 -0700744}
745
746static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
747{
Eli Cohenfa417f72010-10-24 21:08:52 -0700748 int err;
749 struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
750 struct mlx4_ib_qp *mqp = to_mqp(ibqp);
751 u8 mac[6];
752 struct net_device *ndev;
753 struct mlx4_ib_gid_entry *ge;
754
755 err = mlx4_multicast_detach(mdev->dev,
Yevgeny Petrilin03455842011-03-22 22:38:17 +0000756 &mqp->mqp, gid->raw, MLX4_PROT_IB_IPV6);
Eli Cohenfa417f72010-10-24 21:08:52 -0700757 if (err)
758 return err;
759
760 mutex_lock(&mqp->mutex);
761 ge = find_gid_entry(mqp, gid->raw);
762 if (ge) {
763 spin_lock(&mdev->iboe.lock);
764 ndev = ge->added ? mdev->iboe.netdevs[ge->port - 1] : NULL;
765 if (ndev)
766 dev_hold(ndev);
767 spin_unlock(&mdev->iboe.lock);
768 rdma_get_mcast_mac((struct in6_addr *)gid, mac);
769 if (ndev) {
770 rtnl_lock();
771 dev_mc_del(mdev->iboe.netdevs[ge->port - 1], mac);
772 rtnl_unlock();
773 dev_put(ndev);
774 }
775 list_del(&ge->list);
776 kfree(ge);
777 } else
778 printk(KERN_WARNING "could not find mgid entry\n");
779
780 mutex_unlock(&mqp->mutex);
781
782 return 0;
Roland Dreier225c7b12007-05-08 18:00:38 -0700783}
784
785static int init_node_data(struct mlx4_ib_dev *dev)
786{
787 struct ib_smp *in_mad = NULL;
788 struct ib_smp *out_mad = NULL;
789 int err = -ENOMEM;
790
791 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
792 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
793 if (!in_mad || !out_mad)
794 goto out;
795
796 init_query_mad(in_mad);
797 in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
798
799 err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
800 if (err)
801 goto out;
802
803 memcpy(dev->ib_dev.node_desc, out_mad->data, 64);
804
805 in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
806
807 err = mlx4_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
808 if (err)
809 goto out;
810
811 memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
812
813out:
814 kfree(in_mad);
815 kfree(out_mad);
816 return err;
817}
818
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100819static ssize_t show_hca(struct device *device, struct device_attribute *attr,
820 char *buf)
Jack Morgensteincd9281d2007-09-18 09:14:18 +0200821{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100822 struct mlx4_ib_dev *dev =
823 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
Jack Morgensteincd9281d2007-09-18 09:14:18 +0200824 return sprintf(buf, "MT%d\n", dev->dev->pdev->device);
825}
826
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100827static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
828 char *buf)
Jack Morgensteincd9281d2007-09-18 09:14:18 +0200829{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100830 struct mlx4_ib_dev *dev =
831 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
Jack Morgensteincd9281d2007-09-18 09:14:18 +0200832 return sprintf(buf, "%d.%d.%d\n", (int) (dev->dev->caps.fw_ver >> 32),
833 (int) (dev->dev->caps.fw_ver >> 16) & 0xffff,
834 (int) dev->dev->caps.fw_ver & 0xffff);
835}
836
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100837static ssize_t show_rev(struct device *device, struct device_attribute *attr,
838 char *buf)
Jack Morgensteincd9281d2007-09-18 09:14:18 +0200839{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100840 struct mlx4_ib_dev *dev =
841 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
Jack Morgensteincd9281d2007-09-18 09:14:18 +0200842 return sprintf(buf, "%x\n", dev->dev->rev_id);
843}
844
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100845static ssize_t show_board(struct device *device, struct device_attribute *attr,
846 char *buf)
Jack Morgensteincd9281d2007-09-18 09:14:18 +0200847{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100848 struct mlx4_ib_dev *dev =
849 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
850 return sprintf(buf, "%.*s\n", MLX4_BOARD_ID_LEN,
851 dev->dev->board_id);
Jack Morgensteincd9281d2007-09-18 09:14:18 +0200852}
853
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100854static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
855static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
856static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
857static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
Jack Morgensteincd9281d2007-09-18 09:14:18 +0200858
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100859static struct device_attribute *mlx4_class_attributes[] = {
860 &dev_attr_hw_rev,
861 &dev_attr_fw_ver,
862 &dev_attr_hca_type,
863 &dev_attr_board_id
Jack Morgensteincd9281d2007-09-18 09:14:18 +0200864};
865
Eli Cohen4c3eb3c2010-08-26 17:19:22 +0300866static void mlx4_addrconf_ifid_eui48(u8 *eui, u16 vlan_id, struct net_device *dev)
Eli Cohenfa417f72010-10-24 21:08:52 -0700867{
868 memcpy(eui, dev->dev_addr, 3);
869 memcpy(eui + 5, dev->dev_addr + 3, 3);
Eli Cohen4c3eb3c2010-08-26 17:19:22 +0300870 if (vlan_id < 0x1000) {
871 eui[3] = vlan_id >> 8;
872 eui[4] = vlan_id & 0xff;
873 } else {
874 eui[3] = 0xff;
875 eui[4] = 0xfe;
876 }
Eli Cohenfa417f72010-10-24 21:08:52 -0700877 eui[0] ^= 2;
878}
879
880static void update_gids_task(struct work_struct *work)
881{
882 struct update_gid_work *gw = container_of(work, struct update_gid_work, work);
883 struct mlx4_cmd_mailbox *mailbox;
884 union ib_gid *gids;
885 int err;
886 struct mlx4_dev *dev = gw->dev->dev;
887 struct ib_event event;
888
889 mailbox = mlx4_alloc_cmd_mailbox(dev);
890 if (IS_ERR(mailbox)) {
891 printk(KERN_WARNING "update gid table failed %ld\n", PTR_ERR(mailbox));
892 return;
893 }
894
895 gids = mailbox->buf;
896 memcpy(gids, gw->gids, sizeof gw->gids);
897
898 err = mlx4_cmd(dev, mailbox->dma, MLX4_SET_PORT_GID_TABLE << 8 | gw->port,
Jack Morgensteinf9baff52011-12-13 04:10:51 +0000899 1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
900 MLX4_CMD_NATIVE);
Eli Cohenfa417f72010-10-24 21:08:52 -0700901 if (err)
902 printk(KERN_WARNING "set port command failed\n");
903 else {
904 memcpy(gw->dev->iboe.gid_table[gw->port - 1], gw->gids, sizeof gw->gids);
905 event.device = &gw->dev->ib_dev;
906 event.element.port_num = gw->port;
Or Gerlitz6451c712011-06-15 14:40:29 +0000907 event.event = IB_EVENT_GID_CHANGE;
Eli Cohenfa417f72010-10-24 21:08:52 -0700908 ib_dispatch_event(&event);
909 }
910
911 mlx4_free_cmd_mailbox(dev, mailbox);
912 kfree(gw);
913}
914
915static int update_ipv6_gids(struct mlx4_ib_dev *dev, int port, int clear)
916{
917 struct net_device *ndev = dev->iboe.netdevs[port - 1];
918 struct update_gid_work *work;
Eli Cohen4c3eb3c2010-08-26 17:19:22 +0300919 struct net_device *tmp;
920 int i;
921 u8 *hits;
922 int ret;
923 union ib_gid gid;
924 int free;
925 int found;
926 int need_update = 0;
927 u16 vid;
Eli Cohenfa417f72010-10-24 21:08:52 -0700928
929 work = kzalloc(sizeof *work, GFP_ATOMIC);
930 if (!work)
931 return -ENOMEM;
932
Eli Cohen4c3eb3c2010-08-26 17:19:22 +0300933 hits = kzalloc(128, GFP_ATOMIC);
934 if (!hits) {
935 ret = -ENOMEM;
936 goto out;
Eli Cohenfa417f72010-10-24 21:08:52 -0700937 }
938
Eric Dumazet22f4fbd2010-11-24 11:41:56 -0800939 rcu_read_lock();
940 for_each_netdev_rcu(&init_net, tmp) {
Eli Cohen4c3eb3c2010-08-26 17:19:22 +0300941 if (ndev && (tmp == ndev || rdma_vlan_dev_real_dev(tmp) == ndev)) {
942 gid.global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
943 vid = rdma_vlan_dev_vlan_id(tmp);
944 mlx4_addrconf_ifid_eui48(&gid.raw[8], vid, ndev);
945 found = 0;
946 free = -1;
947 for (i = 0; i < 128; ++i) {
948 if (free < 0 &&
949 !memcmp(&dev->iboe.gid_table[port - 1][i], &zgid, sizeof zgid))
950 free = i;
951 if (!memcmp(&dev->iboe.gid_table[port - 1][i], &gid, sizeof gid)) {
952 hits[i] = 1;
953 found = 1;
954 break;
955 }
956 }
Eli Cohenfa417f72010-10-24 21:08:52 -0700957
Eli Cohen4c3eb3c2010-08-26 17:19:22 +0300958 if (!found) {
959 if (tmp == ndev &&
960 (memcmp(&dev->iboe.gid_table[port - 1][0],
961 &gid, sizeof gid) ||
962 !memcmp(&dev->iboe.gid_table[port - 1][0],
963 &zgid, sizeof gid))) {
964 dev->iboe.gid_table[port - 1][0] = gid;
965 ++need_update;
966 hits[0] = 1;
967 } else if (free >= 0) {
968 dev->iboe.gid_table[port - 1][free] = gid;
969 hits[free] = 1;
970 ++need_update;
971 }
972 }
973 }
974 }
Eric Dumazet22f4fbd2010-11-24 11:41:56 -0800975 rcu_read_unlock();
Eli Cohen4c3eb3c2010-08-26 17:19:22 +0300976
977 for (i = 0; i < 128; ++i)
978 if (!hits[i]) {
979 if (memcmp(&dev->iboe.gid_table[port - 1][i], &zgid, sizeof zgid))
980 ++need_update;
981 dev->iboe.gid_table[port - 1][i] = zgid;
982 }
983
984 if (need_update) {
985 memcpy(work->gids, dev->iboe.gid_table[port - 1], sizeof work->gids);
986 INIT_WORK(&work->work, update_gids_task);
987 work->port = port;
988 work->dev = dev;
989 queue_work(wq, &work->work);
990 } else
991 kfree(work);
992
993 kfree(hits);
Eli Cohenfa417f72010-10-24 21:08:52 -0700994 return 0;
Eli Cohen4c3eb3c2010-08-26 17:19:22 +0300995
996out:
997 kfree(work);
998 return ret;
Eli Cohenfa417f72010-10-24 21:08:52 -0700999}
1000
1001static void handle_en_event(struct mlx4_ib_dev *dev, int port, unsigned long event)
1002{
1003 switch (event) {
1004 case NETDEV_UP:
Eli Cohen4c3eb3c2010-08-26 17:19:22 +03001005 case NETDEV_CHANGEADDR:
Eli Cohenfa417f72010-10-24 21:08:52 -07001006 update_ipv6_gids(dev, port, 0);
1007 break;
1008
1009 case NETDEV_DOWN:
1010 update_ipv6_gids(dev, port, 1);
1011 dev->iboe.netdevs[port - 1] = NULL;
1012 }
1013}
1014
1015static void netdev_added(struct mlx4_ib_dev *dev, int port)
1016{
1017 update_ipv6_gids(dev, port, 0);
1018}
1019
1020static void netdev_removed(struct mlx4_ib_dev *dev, int port)
1021{
1022 update_ipv6_gids(dev, port, 1);
1023}
1024
1025static int mlx4_ib_netdev_event(struct notifier_block *this, unsigned long event,
1026 void *ptr)
1027{
1028 struct net_device *dev = ptr;
1029 struct mlx4_ib_dev *ibdev;
1030 struct net_device *oldnd;
1031 struct mlx4_ib_iboe *iboe;
1032 int port;
1033
1034 if (!net_eq(dev_net(dev), &init_net))
1035 return NOTIFY_DONE;
1036
1037 ibdev = container_of(this, struct mlx4_ib_dev, iboe.nb);
1038 iboe = &ibdev->iboe;
1039
1040 spin_lock(&iboe->lock);
1041 mlx4_foreach_ib_transport_port(port, ibdev->dev) {
1042 oldnd = iboe->netdevs[port - 1];
1043 iboe->netdevs[port - 1] =
Yevgeny Petrilin03455842011-03-22 22:38:17 +00001044 mlx4_get_protocol_dev(ibdev->dev, MLX4_PROT_ETH, port);
Eli Cohenfa417f72010-10-24 21:08:52 -07001045 if (oldnd != iboe->netdevs[port - 1]) {
1046 if (iboe->netdevs[port - 1])
1047 netdev_added(ibdev, port);
1048 else
1049 netdev_removed(ibdev, port);
1050 }
1051 }
1052
Eli Cohen4c3eb3c2010-08-26 17:19:22 +03001053 if (dev == iboe->netdevs[0] ||
1054 (iboe->netdevs[0] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[0]))
Eli Cohenfa417f72010-10-24 21:08:52 -07001055 handle_en_event(ibdev, 1, event);
Eli Cohen4c3eb3c2010-08-26 17:19:22 +03001056 else if (dev == iboe->netdevs[1]
1057 || (iboe->netdevs[1] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[1]))
Eli Cohenfa417f72010-10-24 21:08:52 -07001058 handle_en_event(ibdev, 2, event);
1059
1060 spin_unlock(&iboe->lock);
1061
1062 return NOTIFY_DONE;
1063}
1064
Roland Dreier225c7b12007-05-08 18:00:38 -07001065static void *mlx4_ib_add(struct mlx4_dev *dev)
1066{
1067 struct mlx4_ib_dev *ibdev;
Roland Dreier22e7ef92009-01-09 13:22:29 -08001068 int num_ports = 0;
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001069 int i;
Eli Cohenfa417f72010-10-24 21:08:52 -07001070 int err;
1071 struct mlx4_ib_iboe *iboe;
Roland Dreier225c7b12007-05-08 18:00:38 -07001072
Marcin Slusarzf1aa78b2009-09-05 20:24:24 -07001073 printk_once(KERN_INFO "%s", mlx4_ib_version);
Roland Dreier68f39482008-02-04 20:20:44 -08001074
Jack Morgenstein8e59d252011-12-13 04:15:46 +00001075 if (mlx4_is_mfunc(dev)) {
1076 printk(KERN_WARNING "IB not yet supported in SRIOV\n");
1077 return NULL;
1078 }
1079
Eli Cohenfa417f72010-10-24 21:08:52 -07001080 mlx4_foreach_ib_transport_port(i, dev)
Roland Dreier22e7ef92009-01-09 13:22:29 -08001081 num_ports++;
1082
1083 /* No point in registering a device with no ports... */
1084 if (num_ports == 0)
1085 return NULL;
1086
Roland Dreier225c7b12007-05-08 18:00:38 -07001087 ibdev = (struct mlx4_ib_dev *) ib_alloc_device(sizeof *ibdev);
1088 if (!ibdev) {
1089 dev_err(&dev->pdev->dev, "Device struct alloc failed\n");
1090 return NULL;
1091 }
1092
Eli Cohenfa417f72010-10-24 21:08:52 -07001093 iboe = &ibdev->iboe;
1094
Roland Dreier225c7b12007-05-08 18:00:38 -07001095 if (mlx4_pd_alloc(dev, &ibdev->priv_pdn))
1096 goto err_dealloc;
1097
1098 if (mlx4_uar_alloc(dev, &ibdev->priv_uar))
1099 goto err_pd;
1100
Roland Dreier4979d182011-01-12 09:50:36 -08001101 ibdev->uar_map = ioremap((phys_addr_t) ibdev->priv_uar.pfn << PAGE_SHIFT,
1102 PAGE_SIZE);
Roland Dreier225c7b12007-05-08 18:00:38 -07001103 if (!ibdev->uar_map)
1104 goto err_uar;
Jack Morgenstein26c6bc72007-05-13 17:18:23 +03001105 MLX4_INIT_DOORBELL_LOCK(&ibdev->uar_lock);
Roland Dreier225c7b12007-05-08 18:00:38 -07001106
Roland Dreier225c7b12007-05-08 18:00:38 -07001107 ibdev->dev = dev;
1108
1109 strlcpy(ibdev->ib_dev.name, "mlx4_%d", IB_DEVICE_NAME_MAX);
1110 ibdev->ib_dev.owner = THIS_MODULE;
1111 ibdev->ib_dev.node_type = RDMA_NODE_IB_CA;
Roland Dreier95d04f02008-07-23 08:12:26 -07001112 ibdev->ib_dev.local_dma_lkey = dev->caps.reserved_lkey;
Roland Dreier22e7ef92009-01-09 13:22:29 -08001113 ibdev->num_ports = num_ports;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07001114 ibdev->ib_dev.phys_port_cnt = ibdev->num_ports;
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08001115 ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;
Roland Dreier225c7b12007-05-08 18:00:38 -07001116 ibdev->ib_dev.dma_device = &dev->pdev->dev;
1117
1118 ibdev->ib_dev.uverbs_abi_ver = MLX4_IB_UVERBS_ABI_VERSION;
1119 ibdev->ib_dev.uverbs_cmd_mask =
1120 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
1121 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
1122 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
1123 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
1124 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
1125 (1ull << IB_USER_VERBS_CMD_REG_MR) |
1126 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
1127 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
1128 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
Vladimir Sokolovskybbf8eed12008-04-16 21:09:33 -07001129 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
Roland Dreier225c7b12007-05-08 18:00:38 -07001130 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
1131 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
1132 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
Jack Morgenstein6a775e22007-06-21 12:27:47 +03001133 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
Roland Dreier225c7b12007-05-08 18:00:38 -07001134 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
1135 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
1136 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
1137 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
1138 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
Jack Morgenstein65541cb2007-06-21 13:03:11 +03001139 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
Sean Hefty18abd5e2011-06-02 10:43:26 -07001140 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
Sean Hefty42849b22011-08-11 13:57:43 -07001141 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
1142 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Roland Dreier225c7b12007-05-08 18:00:38 -07001143
1144 ibdev->ib_dev.query_device = mlx4_ib_query_device;
1145 ibdev->ib_dev.query_port = mlx4_ib_query_port;
Eli Cohenfa417f72010-10-24 21:08:52 -07001146 ibdev->ib_dev.get_link_layer = mlx4_ib_port_link_layer;
Roland Dreier225c7b12007-05-08 18:00:38 -07001147 ibdev->ib_dev.query_gid = mlx4_ib_query_gid;
1148 ibdev->ib_dev.query_pkey = mlx4_ib_query_pkey;
1149 ibdev->ib_dev.modify_device = mlx4_ib_modify_device;
1150 ibdev->ib_dev.modify_port = mlx4_ib_modify_port;
1151 ibdev->ib_dev.alloc_ucontext = mlx4_ib_alloc_ucontext;
1152 ibdev->ib_dev.dealloc_ucontext = mlx4_ib_dealloc_ucontext;
1153 ibdev->ib_dev.mmap = mlx4_ib_mmap;
1154 ibdev->ib_dev.alloc_pd = mlx4_ib_alloc_pd;
1155 ibdev->ib_dev.dealloc_pd = mlx4_ib_dealloc_pd;
1156 ibdev->ib_dev.create_ah = mlx4_ib_create_ah;
1157 ibdev->ib_dev.query_ah = mlx4_ib_query_ah;
1158 ibdev->ib_dev.destroy_ah = mlx4_ib_destroy_ah;
1159 ibdev->ib_dev.create_srq = mlx4_ib_create_srq;
1160 ibdev->ib_dev.modify_srq = mlx4_ib_modify_srq;
Jack Morgenstein65541cb2007-06-21 13:03:11 +03001161 ibdev->ib_dev.query_srq = mlx4_ib_query_srq;
Roland Dreier225c7b12007-05-08 18:00:38 -07001162 ibdev->ib_dev.destroy_srq = mlx4_ib_destroy_srq;
1163 ibdev->ib_dev.post_srq_recv = mlx4_ib_post_srq_recv;
1164 ibdev->ib_dev.create_qp = mlx4_ib_create_qp;
1165 ibdev->ib_dev.modify_qp = mlx4_ib_modify_qp;
Jack Morgenstein6a775e22007-06-21 12:27:47 +03001166 ibdev->ib_dev.query_qp = mlx4_ib_query_qp;
Roland Dreier225c7b12007-05-08 18:00:38 -07001167 ibdev->ib_dev.destroy_qp = mlx4_ib_destroy_qp;
1168 ibdev->ib_dev.post_send = mlx4_ib_post_send;
1169 ibdev->ib_dev.post_recv = mlx4_ib_post_recv;
1170 ibdev->ib_dev.create_cq = mlx4_ib_create_cq;
Eli Cohen3fdcb972008-04-16 21:09:33 -07001171 ibdev->ib_dev.modify_cq = mlx4_ib_modify_cq;
Vladimir Sokolovskybbf8eed12008-04-16 21:09:33 -07001172 ibdev->ib_dev.resize_cq = mlx4_ib_resize_cq;
Roland Dreier225c7b12007-05-08 18:00:38 -07001173 ibdev->ib_dev.destroy_cq = mlx4_ib_destroy_cq;
1174 ibdev->ib_dev.poll_cq = mlx4_ib_poll_cq;
1175 ibdev->ib_dev.req_notify_cq = mlx4_ib_arm_cq;
1176 ibdev->ib_dev.get_dma_mr = mlx4_ib_get_dma_mr;
1177 ibdev->ib_dev.reg_user_mr = mlx4_ib_reg_user_mr;
1178 ibdev->ib_dev.dereg_mr = mlx4_ib_dereg_mr;
Roland Dreier95d04f02008-07-23 08:12:26 -07001179 ibdev->ib_dev.alloc_fast_reg_mr = mlx4_ib_alloc_fast_reg_mr;
1180 ibdev->ib_dev.alloc_fast_reg_page_list = mlx4_ib_alloc_fast_reg_page_list;
1181 ibdev->ib_dev.free_fast_reg_page_list = mlx4_ib_free_fast_reg_page_list;
Roland Dreier225c7b12007-05-08 18:00:38 -07001182 ibdev->ib_dev.attach_mcast = mlx4_ib_mcg_attach;
1183 ibdev->ib_dev.detach_mcast = mlx4_ib_mcg_detach;
1184 ibdev->ib_dev.process_mad = mlx4_ib_process_mad;
1185
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +03001186 ibdev->ib_dev.alloc_fmr = mlx4_ib_fmr_alloc;
1187 ibdev->ib_dev.map_phys_fmr = mlx4_ib_map_phys_fmr;
1188 ibdev->ib_dev.unmap_fmr = mlx4_ib_unmap_fmr;
1189 ibdev->ib_dev.dealloc_fmr = mlx4_ib_fmr_dealloc;
1190
Sean Hefty012a8ff2011-06-02 09:01:33 -07001191 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC) {
1192 ibdev->ib_dev.alloc_xrcd = mlx4_ib_alloc_xrcd;
1193 ibdev->ib_dev.dealloc_xrcd = mlx4_ib_dealloc_xrcd;
1194 ibdev->ib_dev.uverbs_cmd_mask |=
1195 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
1196 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
1197 }
1198
Eli Cohenfa417f72010-10-24 21:08:52 -07001199 spin_lock_init(&iboe->lock);
1200
Roland Dreier225c7b12007-05-08 18:00:38 -07001201 if (init_node_data(ibdev))
1202 goto err_map;
1203
Or Gerlitzcfcde112011-06-15 14:49:57 +00001204 for (i = 0; i < ibdev->num_ports; ++i) {
1205 if (mlx4_ib_port_link_layer(&ibdev->ib_dev, i + 1) ==
1206 IB_LINK_LAYER_ETHERNET) {
1207 err = mlx4_counter_alloc(ibdev->dev, &ibdev->counters[i]);
1208 if (err)
1209 ibdev->counters[i] = -1;
1210 } else
1211 ibdev->counters[i] = -1;
1212 }
1213
Roland Dreier225c7b12007-05-08 18:00:38 -07001214 spin_lock_init(&ibdev->sm_lock);
1215 mutex_init(&ibdev->cap_mask_mutex);
1216
Ralph Campbell9a6edb62010-05-06 17:03:25 -07001217 if (ib_register_device(&ibdev->ib_dev, NULL))
Or Gerlitzcfcde112011-06-15 14:49:57 +00001218 goto err_counter;
Roland Dreier225c7b12007-05-08 18:00:38 -07001219
1220 if (mlx4_ib_mad_init(ibdev))
1221 goto err_reg;
1222
Eli Cohenfa417f72010-10-24 21:08:52 -07001223 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE && !iboe->nb.notifier_call) {
1224 iboe->nb.notifier_call = mlx4_ib_netdev_event;
1225 err = register_netdevice_notifier(&iboe->nb);
1226 if (err)
1227 goto err_reg;
1228 }
1229
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001230 for (i = 0; i < ARRAY_SIZE(mlx4_class_attributes); ++i) {
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001231 if (device_create_file(&ibdev->ib_dev.dev,
1232 mlx4_class_attributes[i]))
Eli Cohenfa417f72010-10-24 21:08:52 -07001233 goto err_notif;
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001234 }
1235
Jack Morgenstein3b4a8cd2009-09-05 20:24:50 -07001236 ibdev->ib_active = true;
1237
Roland Dreier225c7b12007-05-08 18:00:38 -07001238 return ibdev;
1239
Eli Cohenfa417f72010-10-24 21:08:52 -07001240err_notif:
1241 if (unregister_netdevice_notifier(&ibdev->iboe.nb))
1242 printk(KERN_WARNING "failure unregistering notifier\n");
1243 flush_workqueue(wq);
1244
Roland Dreier225c7b12007-05-08 18:00:38 -07001245err_reg:
1246 ib_unregister_device(&ibdev->ib_dev);
1247
Or Gerlitzcfcde112011-06-15 14:49:57 +00001248err_counter:
1249 for (; i; --i)
Roland Dreier4af3ce02011-12-06 10:47:37 -08001250 if (ibdev->counters[i - 1] != -1)
1251 mlx4_counter_free(ibdev->dev, ibdev->counters[i - 1]);
Or Gerlitzcfcde112011-06-15 14:49:57 +00001252
Roland Dreier225c7b12007-05-08 18:00:38 -07001253err_map:
1254 iounmap(ibdev->uar_map);
1255
1256err_uar:
1257 mlx4_uar_free(dev, &ibdev->priv_uar);
1258
1259err_pd:
1260 mlx4_pd_free(dev, ibdev->priv_pdn);
1261
1262err_dealloc:
1263 ib_dealloc_device(&ibdev->ib_dev);
1264
1265 return NULL;
1266}
1267
1268static void mlx4_ib_remove(struct mlx4_dev *dev, void *ibdev_ptr)
1269{
1270 struct mlx4_ib_dev *ibdev = ibdev_ptr;
1271 int p;
1272
Yevgeny Petrilina6a47772009-03-18 19:49:54 -07001273 mlx4_ib_mad_cleanup(ibdev);
1274 ib_unregister_device(&ibdev->ib_dev);
Eli Cohenfa417f72010-10-24 21:08:52 -07001275 if (ibdev->iboe.nb.notifier_call) {
1276 if (unregister_netdevice_notifier(&ibdev->iboe.nb))
1277 printk(KERN_WARNING "failure unregistering notifier\n");
1278 ibdev->iboe.nb.notifier_call = NULL;
1279 }
1280 iounmap(ibdev->uar_map);
Or Gerlitzcfcde112011-06-15 14:49:57 +00001281 for (p = 0; p < ibdev->num_ports; ++p)
Roland Dreier4af3ce02011-12-06 10:47:37 -08001282 if (ibdev->counters[p] != -1)
1283 mlx4_counter_free(ibdev->dev, ibdev->counters[p]);
Eli Cohenfa417f72010-10-24 21:08:52 -07001284 mlx4_foreach_port(p, dev, MLX4_PORT_TYPE_IB)
Roland Dreier225c7b12007-05-08 18:00:38 -07001285 mlx4_CLOSE_PORT(dev, p);
1286
Roland Dreier225c7b12007-05-08 18:00:38 -07001287 mlx4_uar_free(dev, &ibdev->priv_uar);
1288 mlx4_pd_free(dev, ibdev->priv_pdn);
1289 ib_dealloc_device(&ibdev->ib_dev);
1290}
1291
1292static void mlx4_ib_event(struct mlx4_dev *dev, void *ibdev_ptr,
Roland Dreier37608ee2008-04-16 21:01:08 -07001293 enum mlx4_dev_event event, int port)
Roland Dreier225c7b12007-05-08 18:00:38 -07001294{
1295 struct ib_event ibev;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07001296 struct mlx4_ib_dev *ibdev = to_mdev((struct ib_device *) ibdev_ptr);
1297
1298 if (port > ibdev->num_ports)
1299 return;
Roland Dreier225c7b12007-05-08 18:00:38 -07001300
1301 switch (event) {
Roland Dreier37608ee2008-04-16 21:01:08 -07001302 case MLX4_DEV_EVENT_PORT_UP:
1303 ibev.event = IB_EVENT_PORT_ACTIVE;
Roland Dreier225c7b12007-05-08 18:00:38 -07001304 break;
1305
Roland Dreier37608ee2008-04-16 21:01:08 -07001306 case MLX4_DEV_EVENT_PORT_DOWN:
1307 ibev.event = IB_EVENT_PORT_ERR;
1308 break;
1309
1310 case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
Jack Morgenstein3b4a8cd2009-09-05 20:24:50 -07001311 ibdev->ib_active = false;
Roland Dreier225c7b12007-05-08 18:00:38 -07001312 ibev.event = IB_EVENT_DEVICE_FATAL;
1313 break;
1314
1315 default:
1316 return;
1317 }
1318
1319 ibev.device = ibdev_ptr;
1320 ibev.element.port_num = port;
1321
1322 ib_dispatch_event(&ibev);
1323}
1324
1325static struct mlx4_interface mlx4_ib_interface = {
Eli Cohenfa417f72010-10-24 21:08:52 -07001326 .add = mlx4_ib_add,
1327 .remove = mlx4_ib_remove,
1328 .event = mlx4_ib_event,
Yevgeny Petrilin03455842011-03-22 22:38:17 +00001329 .protocol = MLX4_PROT_IB_IPV6
Roland Dreier225c7b12007-05-08 18:00:38 -07001330};
1331
1332static int __init mlx4_ib_init(void)
1333{
Eli Cohenfa417f72010-10-24 21:08:52 -07001334 int err;
1335
1336 wq = create_singlethread_workqueue("mlx4_ib");
1337 if (!wq)
1338 return -ENOMEM;
1339
1340 err = mlx4_register_interface(&mlx4_ib_interface);
1341 if (err) {
1342 destroy_workqueue(wq);
1343 return err;
1344 }
1345
1346 return 0;
Roland Dreier225c7b12007-05-08 18:00:38 -07001347}
1348
1349static void __exit mlx4_ib_cleanup(void)
1350{
1351 mlx4_unregister_interface(&mlx4_ib_interface);
Eli Cohenfa417f72010-10-24 21:08:52 -07001352 destroy_workqueue(wq);
Roland Dreier225c7b12007-05-08 18:00:38 -07001353}
1354
1355module_init(mlx4_ib_init);
1356module_exit(mlx4_ib_cleanup);