blob: 45a6cc04036b9b177f8dd653c5fddcef90406e60 [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
Jack Morgensteinb1d8eb52012-06-19 11:21:35 +030053#define DRV_NAME MLX4_IB_DRV_NAME
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
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000101 err = mlx4_MAD_IFC(to_mdev(ibdev), MLX4_MAD_IFC_IGNORE_KEYS,
102 1, NULL, NULL, in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -0700103 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 |
Ron Livne521e5752008-07-14 23:48:48 -0700112 IB_DEVICE_RC_RNR_NAK_GEN |
113 IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Roland Dreier225c7b12007-05-08 18:00:38 -0700114 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;
Eli Cohen8ff095e2008-04-16 21:01:10 -0700122 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_IPOIB_CSUM)
123 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
Eli Cohen417608c2009-11-12 11:19:44 -0800124 if (dev->dev->caps.max_gso_sz && dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_BLH)
Eli Cohenb832be12008-04-16 21:09:27 -0700125 props->device_cap_flags |= IB_DEVICE_UD_TSO;
Roland Dreier95d04f02008-07-23 08:12:26 -0700126 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;
Sean Hefty0a1405d2011-06-02 11:32:15 -0700132 if (dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC)
133 props->device_cap_flags |= IB_DEVICE_XRC;
Roland Dreier225c7b12007-05-08 18:00:38 -0700134
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;
Sagi Grimbergfc2d0042012-05-24 16:08:08 +0300144 props->max_qp_wr = dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE;
Roland Dreier225c7b12007-05-08 18:00:38 -0700145 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;
Jack Morgensteinc8681f12007-06-21 13:39:10 -0700155 props->max_srq_wr = dev->dev->caps.max_srq_wqes - 1;
Roland Dreier225c7b12007-05-08 18:00:38 -0700156 props->max_srq_sge = dev->dev->caps.max_srq_sge;
Eli Cohen5a0fd092010-10-07 16:24:16 +0200157 props->max_fast_reg_page_list_len = MLX4_MAX_FAST_REG_PAGES;
Roland Dreier225c7b12007-05-08 18:00:38 -0700158 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;
Dotan Barak47e956b2012-07-11 15:39:29 +0000161 props->masked_atomic_cap = props->atomic_cap;
Roland Dreier5ae2a7a2007-06-18 08:15:02 -0700162 props->max_pkeys = dev->dev->caps.pkey_table_len[1];
Roland Dreier225c7b12007-05-08 18:00:38 -0700163 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;
Eli Cohena5bbe892012-02-09 18:10:06 +0200167 props->max_map_per_fmr = dev->dev->caps.max_fmr_maps;
Roland Dreier225c7b12007-05-08 18:00:38 -0700168
169out:
170 kfree(in_mad);
171 kfree(out_mad);
172
173 return err;
174}
175
Eli Cohenfa417f72010-10-24 21:08:52 -0700176static enum rdma_link_layer
177mlx4_ib_port_link_layer(struct ib_device *device, u8 port_num)
178{
179 struct mlx4_dev *dev = to_mdev(device)->dev;
180
Jack Morgenstein65dab252011-12-13 04:10:41 +0000181 return dev->caps.port_mask[port_num] == MLX4_PORT_TYPE_IB ?
Eli Cohenfa417f72010-10-24 21:08:52 -0700182 IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
183}
184
185static int ib_link_query_port(struct ib_device *ibdev, u8 port,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000186 struct ib_port_attr *props, int netw_view)
Eli Cohenfa417f72010-10-24 21:08:52 -0700187{
Or Gerlitza9c766b2012-01-11 19:00:29 +0200188 struct ib_smp *in_mad = NULL;
189 struct ib_smp *out_mad = NULL;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300190 int ext_active_speed;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000191 int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
Or Gerlitza9c766b2012-01-11 19:00:29 +0200192 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
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000203 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,
Or Gerlitza9c766b2012-01-11 19:00:29 +0200207 in_mad, out_mad);
208 if (err)
209 goto out;
210
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300211
Eli Cohenfa417f72010-10-24 21:08:52 -0700212 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));
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000219 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];
Eli Cohenfa417f72010-10-24 21:08:52 -0700223 props->max_msg_sz = to_mdev(ibdev)->dev->caps.max_msg_sz;
224 props->pkey_tbl_len = to_mdev(ibdev)->dev->caps.pkey_table_len[port];
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
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300235 /* 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:
Or Gerlitz2e966912012-02-28 18:49:50 +0200241 props->active_speed = IB_SPEED_FDR;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300242 break;
243 case 2:
Or Gerlitz2e966912012-02-28 18:49:50 +0200244 props->active_speed = IB_SPEED_EDR;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300245 break;
246 }
247 }
248
249 /* If reported active speed is QDR, check if is FDR-10 */
Or Gerlitz2e966912012-02-28 18:49:50 +0200250 if (props->active_speed == IB_SPEED_QDR) {
Or Gerlitz8154c072012-03-06 15:50:50 +0200251 init_query_mad(in_mad);
252 in_mad->attr_id = MLX4_ATTR_EXTENDED_PORT_INFO;
253 in_mad->attr_mod = cpu_to_be32(port);
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300254
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000255 err = mlx4_MAD_IFC(to_mdev(ibdev), mad_ifc_flags, port,
Or Gerlitz8154c072012-03-06 15:50:50 +0200256 NULL, NULL, in_mad, out_mad);
257 if (err)
Jesper Juhlbf6b47d2012-04-11 23:43:29 +0200258 goto out;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300259
Or Gerlitz8154c072012-03-06 15:50:50 +0200260 /* Checking LinkSpeedActive for FDR-10 */
261 if (out_mad->data[15] & 0x1)
262 props->active_speed = IB_SPEED_FDR10;
Marcel Apfelbauma5e12df2011-10-03 19:04:20 +0300263 }
Or Gerlitzd2ef4062012-04-02 17:45:20 +0300264
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
Or Gerlitza9c766b2012-01-11 19:00:29 +0200269out:
270 kfree(in_mad);
271 kfree(out_mad);
272 return err;
Eli Cohenfa417f72010-10-24 21:08:52 -0700273}
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,
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000281 struct ib_port_attr *props, int netw_view)
Eli Cohenfa417f72010-10-24 21:08:52 -0700282{
Or Gerlitza9c766b2012-01-11 19:00:29 +0200283
284 struct mlx4_ib_dev *mdev = to_mdev(ibdev);
285 struct mlx4_ib_iboe *iboe = &mdev->iboe;
Eli Cohenfa417f72010-10-24 21:08:52 -0700286 struct net_device *ndev;
287 enum ib_mtu tmp;
Or Gerlitza9c766b2012-01-11 19:00:29 +0200288 struct mlx4_cmd_mailbox *mailbox;
289 int err = 0;
Eli Cohenfa417f72010-10-24 21:08:52 -0700290
Or Gerlitza9c766b2012-01-11 19:00:29 +0200291 mailbox = mlx4_alloc_cmd_mailbox(mdev->dev);
292 if (IS_ERR(mailbox))
293 return PTR_ERR(mailbox);
294
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;
Or Gerlitz2e966912012-02-28 18:49:50 +0200303 props->active_speed = IB_SPEED_QDR;
Eli Cohenfa417f72010-10-24 21:08:52 -0700304 props->port_cap_flags = IB_PORT_CM_SUP;
Or Gerlitza9c766b2012-01-11 19:00:29 +0200305 props->gid_tbl_len = mdev->dev->caps.gid_table_len[port];
306 props->max_msg_sz = mdev->dev->caps.max_msg_sz;
Eli Cohenfa417f72010-10-24 21:08:52 -0700307 props->pkey_tbl_len = 1;
Or Gerlitzbcacb892011-10-10 10:53:41 +0200308 props->max_mtu = IB_MTU_4096;
Or Gerlitza9c766b2012-01-11 19:00:29 +0200309 props->max_vl_num = 2;
Eli Cohenfa417f72010-10-24 21:08:52 -0700310 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)
Or Gerlitza9c766b2012-01-11 19:00:29 +0200316 goto out_unlock;
Eli Cohenfa417f72010-10-24 21:08:52 -0700317
318 tmp = iboe_get_mtu(ndev->mtu);
319 props->active_mtu = tmp ? min(props->max_mtu, tmp) : IB_MTU_256;
320
Eli Cohen21d606092010-11-11 21:05:58 +0000321 props->state = (netif_running(ndev) && netif_carrier_ok(ndev)) ?
Eli Cohenfa417f72010-10-24 21:08:52 -0700322 IB_PORT_ACTIVE : IB_PORT_DOWN;
323 props->phys_state = state_to_phys_state(props->state);
Or Gerlitza9c766b2012-01-11 19:00:29 +0200324out_unlock:
Eli Cohenfa417f72010-10-24 21:08:52 -0700325 spin_unlock(&iboe->lock);
Or Gerlitza9c766b2012-01-11 19:00:29 +0200326out:
327 mlx4_free_cmd_mailbox(mdev->dev, mailbox);
328 return err;
Eli Cohenfa417f72010-10-24 21:08:52 -0700329}
330
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000331int __mlx4_ib_query_port(struct ib_device *ibdev, u8 port,
332 struct ib_port_attr *props, int netw_view)
Roland Dreier225c7b12007-05-08 18:00:38 -0700333{
Or Gerlitza9c766b2012-01-11 19:00:29 +0200334 int err;
Roland Dreier225c7b12007-05-08 18:00:38 -0700335
336 memset(props, 0, sizeof *props);
337
Eli Cohenfa417f72010-10-24 21:08:52 -0700338 err = mlx4_ib_port_link_layer(ibdev, port) == IB_LINK_LAYER_INFINIBAND ?
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000339 ib_link_query_port(ibdev, port, props, netw_view) :
340 eth_link_query_port(ibdev, port, props, netw_view);
Roland Dreier225c7b12007-05-08 18:00:38 -0700341
342 return err;
343}
344
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000345static 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
Eli Cohenfa417f72010-10-24 21:08:52 -0700352static int __mlx4_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
353 union ib_gid *gid)
Roland Dreier225c7b12007-05-08 18:00:38 -0700354{
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
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000368 err = mlx4_MAD_IFC(to_mdev(ibdev), MLX4_MAD_IFC_IGNORE_KEYS, port,
369 NULL, NULL, in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -0700370 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
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000379 err = mlx4_MAD_IFC(to_mdev(ibdev), MLX4_MAD_IFC_IGNORE_KEYS, port,
380 NULL, NULL, in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -0700381 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
Eli Cohenfa417f72010-10-24 21:08:52 -0700392static 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
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000411int __mlx4_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
412 u16 *pkey, int netw_view)
Roland Dreier225c7b12007-05-08 18:00:38 -0700413{
414 struct ib_smp *in_mad = NULL;
415 struct ib_smp *out_mad = NULL;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000416 int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
Roland Dreier225c7b12007-05-08 18:00:38 -0700417 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
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000428 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);
Roland Dreier225c7b12007-05-08 18:00:38 -0700433 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
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000444static 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
Roland Dreier225c7b12007-05-08 18:00:38 -0700449static int mlx4_ib_modify_device(struct ib_device *ibdev, int mask,
450 struct ib_device_modify *props)
451{
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000452 struct mlx4_cmd_mailbox *mailbox;
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000453 unsigned long flags;
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000454
Roland Dreier225c7b12007-05-08 18:00:38 -0700455 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
456 return -EOPNOTSUPP;
457
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000458 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
459 return 0;
460
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000461 spin_lock_irqsave(&to_mdev(ibdev)->sm_lock, flags);
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000462 memcpy(ibdev->node_desc, props->node_desc, 64);
Jack Morgensteindf7fba62012-08-03 08:26:45 +0000463 spin_unlock_irqrestore(&to_mdev(ibdev)->sm_lock, flags);
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000464
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,
Jack Morgensteinf9baff52011-12-13 04:10:51 +0000476 MLX4_CMD_SET_NODE, MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
Jack Morgensteind0d68b82010-10-04 12:11:34 +0000477
478 mlx4_free_cmd_mailbox(to_mdev(ibdev)->dev, mailbox);
Roland Dreier225c7b12007-05-08 18:00:38 -0700479
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;
Eli Cohenfa417f72010-10-24 21:08:52 -0700488 u8 is_eth = dev->dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
Roland Dreier225c7b12007-05-08 18:00:38 -0700489
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);
Roland Dreier5ae2a7a2007-06-18 08:15:02 -0700495
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 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700503
Eli Cohenfa417f72010-10-24 21:08:52 -0700504 err = mlx4_cmd(dev->dev, mailbox->dma, port, is_eth, MLX4_CMD_SET_PORT,
Jack Morgensteinf9baff52011-12-13 04:10:51 +0000505 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
Roland Dreier225c7b12007-05-08 18:00:38 -0700506
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
Jack Morgenstein3b4a8cd2009-09-05 20:24:50 -0700544 if (!dev->ib_active)
545 return ERR_PTR(-EAGAIN);
546
Roland Dreier225c7b12007-05-08 18:00:38 -0700547 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) {
Roland Dreiere1d60ec2009-03-30 08:31:05 -0700599 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
Roland Dreier225c7b12007-05-08 18:00:38 -0700600
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
Sean Hefty012a8ff2011-06-02 09:01:33 -0700647static 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
Eli Cohenfa417f72010-10-24 21:08:52 -0700698static 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
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +0000749struct mlx4_ib_steering {
750 struct list_head list;
751 u64 reg_id;
752 union ib_gid gid;
753};
754
Roland Dreier225c7b12007-05-08 18:00:38 -0700755static int mlx4_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
756{
Eli Cohenfa417f72010-10-24 21:08:52 -0700757 int err;
758 struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
759 struct mlx4_ib_qp *mqp = to_mqp(ibqp);
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +0000760 u64 reg_id;
761 struct mlx4_ib_steering *ib_steering = NULL;
Eli Cohenfa417f72010-10-24 21:08:52 -0700762
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +0000763 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 }
769
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);
Eli Cohenfa417f72010-10-24 21:08:52 -0700774 if (err)
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +0000775 goto err_malloc;
Eli Cohenfa417f72010-10-24 21:08:52 -0700776
777 err = add_gid_entry(ibqp, gid);
778 if (err)
779 goto err_add;
780
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +0000781 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 }
Eli Cohenfa417f72010-10-24 21:08:52 -0700788 return 0;
789
790err_add:
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +0000791 mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
792 MLX4_PROT_IB_IPV6, reg_id);
793err_malloc:
794 kfree(ib_steering);
795
Eli Cohenfa417f72010-10-24 21:08:52 -0700796 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;
Roland Dreier225c7b12007-05-08 18:00:38 -0700813}
814
815static int mlx4_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
816{
Eli Cohenfa417f72010-10-24 21:08:52 -0700817 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;
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +0000823 u64 reg_id = 0;
Eli Cohenfa417f72010-10-24 21:08:52 -0700824
Hadar Hen Zion0ff1fb62012-07-05 04:03:46 +0000825 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 }
844
845 err = mlx4_multicast_detach(mdev->dev, &mqp->mqp, gid->raw,
846 MLX4_PROT_IB_IPV6, reg_id);
Eli Cohenfa417f72010-10-24 21:08:52 -0700847 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
Shlomo Pongratz987c8f82012-04-29 17:04:26 +0300868 pr_warn("could not find mgid entry\n");
Eli Cohenfa417f72010-10-24 21:08:52 -0700869
870 mutex_unlock(&mqp->mutex);
871
872 return 0;
Roland Dreier225c7b12007-05-08 18:00:38 -0700873}
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;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000879 int mad_ifc_flags = MLX4_MAD_IFC_IGNORE_KEYS;
Roland Dreier225c7b12007-05-08 18:00:38 -0700880 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;
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000889 if (mlx4_is_master(dev->dev))
890 mad_ifc_flags |= MLX4_MAD_IFC_NET_VIEW;
Roland Dreier225c7b12007-05-08 18:00:38 -0700891
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000892 err = mlx4_MAD_IFC(dev, mad_ifc_flags, 1, NULL, NULL, in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -0700893 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
Jack Morgenstein0a9a0182012-08-03 08:40:45 +0000900 err = mlx4_MAD_IFC(dev, mad_ifc_flags, 1, NULL, NULL, in_mad, out_mad);
Roland Dreier225c7b12007-05-08 18:00:38 -0700901 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
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100912static ssize_t show_hca(struct device *device, struct device_attribute *attr,
913 char *buf)
Jack Morgensteincd9281d2007-09-18 09:14:18 +0200914{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100915 struct mlx4_ib_dev *dev =
916 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
Jack Morgensteincd9281d2007-09-18 09:14:18 +0200917 return sprintf(buf, "MT%d\n", dev->dev->pdev->device);
918}
919
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100920static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
921 char *buf)
Jack Morgensteincd9281d2007-09-18 09:14:18 +0200922{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100923 struct mlx4_ib_dev *dev =
924 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
Jack Morgensteincd9281d2007-09-18 09:14:18 +0200925 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
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100930static ssize_t show_rev(struct device *device, struct device_attribute *attr,
931 char *buf)
Jack Morgensteincd9281d2007-09-18 09:14:18 +0200932{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100933 struct mlx4_ib_dev *dev =
934 container_of(device, struct mlx4_ib_dev, ib_dev.dev);
Jack Morgensteincd9281d2007-09-18 09:14:18 +0200935 return sprintf(buf, "%x\n", dev->dev->rev_id);
936}
937
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100938static ssize_t show_board(struct device *device, struct device_attribute *attr,
939 char *buf)
Jack Morgensteincd9281d2007-09-18 09:14:18 +0200940{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100941 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);
Jack Morgensteincd9281d2007-09-18 09:14:18 +0200945}
946
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100947static 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);
Jack Morgensteincd9281d2007-09-18 09:14:18 +0200951
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100952static 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
Jack Morgensteincd9281d2007-09-18 09:14:18 +0200957};
958
Eli Cohen4c3eb3c2010-08-26 17:19:22 +0300959static void mlx4_addrconf_ifid_eui48(u8 *eui, u16 vlan_id, struct net_device *dev)
Eli Cohenfa417f72010-10-24 21:08:52 -0700960{
961 memcpy(eui, dev->dev_addr, 3);
962 memcpy(eui + 5, dev->dev_addr + 3, 3);
Eli Cohen4c3eb3c2010-08-26 17:19:22 +0300963 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 }
Eli Cohenfa417f72010-10-24 21:08:52 -0700970 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;
Eli Cohenfa417f72010-10-24 21:08:52 -0700980
981 mailbox = mlx4_alloc_cmd_mailbox(dev);
982 if (IS_ERR(mailbox)) {
Shlomo Pongratz987c8f82012-04-29 17:04:26 +0300983 pr_warn("update gid table failed %ld\n", PTR_ERR(mailbox));
Eli Cohenfa417f72010-10-24 21:08:52 -0700984 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,
Jack Morgensteinf9baff52011-12-13 04:10:51 +0000991 1, MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
992 MLX4_CMD_NATIVE);
Eli Cohenfa417f72010-10-24 21:08:52 -0700993 if (err)
Shlomo Pongratz987c8f82012-04-29 17:04:26 +0300994 pr_warn("set port command failed\n");
Eli Cohenfa417f72010-10-24 21:08:52 -0700995 else {
996 memcpy(gw->dev->iboe.gid_table[gw->port - 1], gw->gids, sizeof gw->gids);
Jack Morgenstein00f5ce92012-06-19 11:21:40 +0300997 mlx4_ib_dispatch_event(gw->dev, gw->port, IB_EVENT_GID_CHANGE);
Eli Cohenfa417f72010-10-24 21:08:52 -0700998 }
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;
Eli Cohen4c3eb3c2010-08-26 17:19:22 +03001008 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;
Eli Cohenfa417f72010-10-24 21:08:52 -07001017
1018 work = kzalloc(sizeof *work, GFP_ATOMIC);
1019 if (!work)
1020 return -ENOMEM;
1021
Eli Cohen4c3eb3c2010-08-26 17:19:22 +03001022 hits = kzalloc(128, GFP_ATOMIC);
1023 if (!hits) {
1024 ret = -ENOMEM;
1025 goto out;
Eli Cohenfa417f72010-10-24 21:08:52 -07001026 }
1027
Eric Dumazet22f4fbd2010-11-24 11:41:56 -08001028 rcu_read_lock();
1029 for_each_netdev_rcu(&init_net, tmp) {
Eli Cohen4c3eb3c2010-08-26 17:19:22 +03001030 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 }
Eli Cohenfa417f72010-10-24 21:08:52 -07001046
Eli Cohen4c3eb3c2010-08-26 17:19:22 +03001047 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 }
1063 }
Eric Dumazet22f4fbd2010-11-24 11:41:56 -08001064 rcu_read_unlock();
Eli Cohen4c3eb3c2010-08-26 17:19:22 +03001065
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 }
1072
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);
1081
1082 kfree(hits);
Eli Cohenfa417f72010-10-24 21:08:52 -07001083 return 0;
Eli Cohen4c3eb3c2010-08-26 17:19:22 +03001084
1085out:
1086 kfree(work);
1087 return ret;
Eli Cohenfa417f72010-10-24 21:08:52 -07001088}
1089
1090static void handle_en_event(struct mlx4_ib_dev *dev, int port, unsigned long event)
1091{
1092 switch (event) {
1093 case NETDEV_UP:
Eli Cohen4c3eb3c2010-08-26 17:19:22 +03001094 case NETDEV_CHANGEADDR:
Eli Cohenfa417f72010-10-24 21:08:52 -07001095 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] =
Yevgeny Petrilin03455842011-03-22 22:38:17 +00001133 mlx4_get_protocol_dev(ibdev->dev, MLX4_PROT_ETH, port);
Eli Cohenfa417f72010-10-24 21:08:52 -07001134 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
Eli Cohen4c3eb3c2010-08-26 17:19:22 +03001142 if (dev == iboe->netdevs[0] ||
1143 (iboe->netdevs[0] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[0]))
Eli Cohenfa417f72010-10-24 21:08:52 -07001144 handle_en_event(ibdev, 1, event);
Eli Cohen4c3eb3c2010-08-26 17:19:22 +03001145 else if (dev == iboe->netdevs[1]
1146 || (iboe->netdevs[1] && rdma_vlan_dev_real_dev(dev) == iboe->netdevs[1]))
Eli Cohenfa417f72010-10-24 21:08:52 -07001147 handle_en_event(ibdev, 2, event);
1148
1149 spin_unlock(&iboe->lock);
1150
1151 return NOTIFY_DONE;
1152}
1153
Jack Morgenstein54679e12012-08-03 08:40:43 +00001154static 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
Shlomo Pongratze605b742012-04-29 17:04:27 +03001186static 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
Shlomo Pongratz3aac6ff2012-05-24 16:08:07 +03001194 /* 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)
Shlomo Pongratze605b742012-04-29 17:04:27 +03001197 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) */
Amir Vadaid9236c32012-07-18 22:33:51 +00001221 if (mlx4_assign_eq(dev, name, NULL,
1222 &ibdev->eq_table[eq])) {
Shlomo Pongratze605b742012-04-29 17:04:27 +03001223 /* 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;
Shlomo Pongratz3aac6ff2012-05-24 16:08:07 +03001243
1244 /* no additional eqs were added */
1245 if (!ibdev->eq_table)
1246 return;
Shlomo Pongratze605b742012-04-29 17:04:27 +03001247
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
Shlomo Pongratze605b742012-04-29 17:04:27 +03001259 kfree(ibdev->eq_table);
Shlomo Pongratze605b742012-04-29 17:04:27 +03001260}
1261
Roland Dreier225c7b12007-05-08 18:00:38 -07001262static void *mlx4_ib_add(struct mlx4_dev *dev)
1263{
1264 struct mlx4_ib_dev *ibdev;
Roland Dreier22e7ef92009-01-09 13:22:29 -08001265 int num_ports = 0;
Jack Morgenstein035b1032012-05-10 23:28:09 +03001266 int i, j;
Eli Cohenfa417f72010-10-24 21:08:52 -07001267 int err;
1268 struct mlx4_ib_iboe *iboe;
Roland Dreier225c7b12007-05-08 18:00:38 -07001269
Shlomo Pongratz987c8f82012-04-29 17:04:26 +03001270 pr_info_once("%s", mlx4_ib_version);
Roland Dreier68f39482008-02-04 20:20:44 -08001271
Jack Morgenstein8e59d252011-12-13 04:15:46 +00001272 if (mlx4_is_mfunc(dev)) {
Shlomo Pongratz987c8f82012-04-29 17:04:26 +03001273 pr_warn("IB not yet supported in SRIOV\n");
Jack Morgenstein8e59d252011-12-13 04:15:46 +00001274 return NULL;
1275 }
1276
Eli Cohenfa417f72010-10-24 21:08:52 -07001277 mlx4_foreach_ib_transport_port(i, dev)
Roland Dreier22e7ef92009-01-09 13:22:29 -08001278 num_ports++;
1279
1280 /* No point in registering a device with no ports... */
1281 if (num_ports == 0)
1282 return NULL;
1283
Roland Dreier225c7b12007-05-08 18:00:38 -07001284 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
Eli Cohenfa417f72010-10-24 21:08:52 -07001290 iboe = &ibdev->iboe;
1291
Roland Dreier225c7b12007-05-08 18:00:38 -07001292 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
Roland Dreier4979d182011-01-12 09:50:36 -08001298 ibdev->uar_map = ioremap((phys_addr_t) ibdev->priv_uar.pfn << PAGE_SHIFT,
1299 PAGE_SIZE);
Roland Dreier225c7b12007-05-08 18:00:38 -07001300 if (!ibdev->uar_map)
1301 goto err_uar;
Jack Morgenstein26c6bc72007-05-13 17:18:23 +03001302 MLX4_INIT_DOORBELL_LOCK(&ibdev->uar_lock);
Roland Dreier225c7b12007-05-08 18:00:38 -07001303
Roland Dreier225c7b12007-05-08 18:00:38 -07001304 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;
Roland Dreier95d04f02008-07-23 08:12:26 -07001309 ibdev->ib_dev.local_dma_lkey = dev->caps.reserved_lkey;
Roland Dreier22e7ef92009-01-09 13:22:29 -08001310 ibdev->num_ports = num_ports;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07001311 ibdev->ib_dev.phys_port_cnt = ibdev->num_ports;
Yevgeny Petrilinb8dd7862008-12-22 07:15:03 -08001312 ibdev->ib_dev.num_comp_vectors = dev->caps.num_comp_vectors;
Roland Dreier225c7b12007-05-08 18:00:38 -07001313 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) |
Vladimir Sokolovskybbf8eed12008-04-16 21:09:33 -07001326 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
Roland Dreier225c7b12007-05-08 18:00:38 -07001327 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
1328 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
1329 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
Jack Morgenstein6a775e22007-06-21 12:27:47 +03001330 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
Roland Dreier225c7b12007-05-08 18:00:38 -07001331 (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) |
Jack Morgenstein65541cb2007-06-21 13:03:11 +03001336 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
Sean Hefty18abd5e2011-06-02 10:43:26 -07001337 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
Sean Hefty42849b22011-08-11 13:57:43 -07001338 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
1339 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Roland Dreier225c7b12007-05-08 18:00:38 -07001340
1341 ibdev->ib_dev.query_device = mlx4_ib_query_device;
1342 ibdev->ib_dev.query_port = mlx4_ib_query_port;
Eli Cohenfa417f72010-10-24 21:08:52 -07001343 ibdev->ib_dev.get_link_layer = mlx4_ib_port_link_layer;
Roland Dreier225c7b12007-05-08 18:00:38 -07001344 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;
Jack Morgenstein65541cb2007-06-21 13:03:11 +03001358 ibdev->ib_dev.query_srq = mlx4_ib_query_srq;
Roland Dreier225c7b12007-05-08 18:00:38 -07001359 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;
Jack Morgenstein6a775e22007-06-21 12:27:47 +03001363 ibdev->ib_dev.query_qp = mlx4_ib_query_qp;
Roland Dreier225c7b12007-05-08 18:00:38 -07001364 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;
Eli Cohen3fdcb972008-04-16 21:09:33 -07001368 ibdev->ib_dev.modify_cq = mlx4_ib_modify_cq;
Vladimir Sokolovskybbf8eed12008-04-16 21:09:33 -07001369 ibdev->ib_dev.resize_cq = mlx4_ib_resize_cq;
Roland Dreier225c7b12007-05-08 18:00:38 -07001370 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;
Roland Dreier95d04f02008-07-23 08:12:26 -07001376 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;
Roland Dreier225c7b12007-05-08 18:00:38 -07001379 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
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +03001383 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
Sean Hefty012a8ff2011-06-02 09:01:33 -07001388 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
Shlomo Pongratze605b742012-04-29 17:04:27 +03001396 mlx4_ib_alloc_eqs(dev, ibdev);
1397
Eli Cohenfa417f72010-10-24 21:08:52 -07001398 spin_lock_init(&iboe->lock);
1399
Roland Dreier225c7b12007-05-08 18:00:38 -07001400 if (init_node_data(ibdev))
1401 goto err_map;
1402
Or Gerlitzcfcde112011-06-15 14:49:57 +00001403 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
Roland Dreier225c7b12007-05-08 18:00:38 -07001413 spin_lock_init(&ibdev->sm_lock);
1414 mutex_init(&ibdev->cap_mask_mutex);
1415
Ralph Campbell9a6edb62010-05-06 17:03:25 -07001416 if (ib_register_device(&ibdev->ib_dev, NULL))
Or Gerlitzcfcde112011-06-15 14:49:57 +00001417 goto err_counter;
Roland Dreier225c7b12007-05-08 18:00:38 -07001418
1419 if (mlx4_ib_mad_init(ibdev))
1420 goto err_reg;
1421
Jack Morgensteinfc065732012-08-03 08:40:42 +00001422 if (mlx4_ib_init_sriov(ibdev))
1423 goto err_mad;
1424
Eli Cohenfa417f72010-10-24 21:08:52 -07001425 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)
Jack Morgensteinfc065732012-08-03 08:40:42 +00001429 goto err_sriov;
Eli Cohenfa417f72010-10-24 21:08:52 -07001430 }
1431
Jack Morgenstein035b1032012-05-10 23:28:09 +03001432 for (j = 0; j < ARRAY_SIZE(mlx4_class_attributes); ++j) {
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001433 if (device_create_file(&ibdev->ib_dev.dev,
Jack Morgenstein035b1032012-05-10 23:28:09 +03001434 mlx4_class_attributes[j]))
Eli Cohenfa417f72010-10-24 21:08:52 -07001435 goto err_notif;
Jack Morgensteincd9281d2007-09-18 09:14:18 +02001436 }
1437
Jack Morgenstein3b4a8cd2009-09-05 20:24:50 -07001438 ibdev->ib_active = true;
1439
Jack Morgenstein54679e12012-08-03 08:40:43 +00001440 if (mlx4_is_mfunc(ibdev->dev))
1441 init_pkeys(ibdev);
1442
Roland Dreier225c7b12007-05-08 18:00:38 -07001443 return ibdev;
1444
Eli Cohenfa417f72010-10-24 21:08:52 -07001445err_notif:
1446 if (unregister_netdevice_notifier(&ibdev->iboe.nb))
Shlomo Pongratz987c8f82012-04-29 17:04:26 +03001447 pr_warn("failure unregistering notifier\n");
Eli Cohenfa417f72010-10-24 21:08:52 -07001448 flush_workqueue(wq);
1449
Jack Morgensteinfc065732012-08-03 08:40:42 +00001450err_sriov:
1451 mlx4_ib_close_sriov(ibdev);
1452
1453err_mad:
1454 mlx4_ib_mad_cleanup(ibdev);
1455
Roland Dreier225c7b12007-05-08 18:00:38 -07001456err_reg:
1457 ib_unregister_device(&ibdev->ib_dev);
1458
Or Gerlitzcfcde112011-06-15 14:49:57 +00001459err_counter:
1460 for (; i; --i)
Roland Dreier4af3ce02011-12-06 10:47:37 -08001461 if (ibdev->counters[i - 1] != -1)
1462 mlx4_counter_free(ibdev->dev, ibdev->counters[i - 1]);
Or Gerlitzcfcde112011-06-15 14:49:57 +00001463
Roland Dreier225c7b12007-05-08 18:00:38 -07001464err_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
Jack Morgensteinfc065732012-08-03 08:40:42 +00001484 mlx4_ib_close_sriov(ibdev);
Yevgeny Petrilina6a47772009-03-18 19:49:54 -07001485 mlx4_ib_mad_cleanup(ibdev);
1486 ib_unregister_device(&ibdev->ib_dev);
Eli Cohenfa417f72010-10-24 21:08:52 -07001487 if (ibdev->iboe.nb.notifier_call) {
1488 if (unregister_netdevice_notifier(&ibdev->iboe.nb))
Shlomo Pongratz987c8f82012-04-29 17:04:26 +03001489 pr_warn("failure unregistering notifier\n");
Eli Cohenfa417f72010-10-24 21:08:52 -07001490 ibdev->iboe.nb.notifier_call = NULL;
1491 }
1492 iounmap(ibdev->uar_map);
Or Gerlitzcfcde112011-06-15 14:49:57 +00001493 for (p = 0; p < ibdev->num_ports; ++p)
Roland Dreier4af3ce02011-12-06 10:47:37 -08001494 if (ibdev->counters[p] != -1)
1495 mlx4_counter_free(ibdev->dev, ibdev->counters[p]);
Eli Cohenfa417f72010-10-24 21:08:52 -07001496 mlx4_foreach_port(p, dev, MLX4_PORT_TYPE_IB)
Roland Dreier225c7b12007-05-08 18:00:38 -07001497 mlx4_CLOSE_PORT(dev, p);
1498
Shlomo Pongratze605b742012-04-29 17:04:27 +03001499 mlx4_ib_free_eqs(dev, ibdev);
1500
Roland Dreier225c7b12007-05-08 18:00:38 -07001501 mlx4_uar_free(dev, &ibdev->priv_uar);
1502 mlx4_pd_free(dev, ibdev->priv_pdn);
1503 ib_dealloc_device(&ibdev->ib_dev);
1504}
1505
Jack Morgensteinfc065732012-08-03 08:40:42 +00001506static 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
Roland Dreier225c7b12007-05-08 18:00:38 -07001551static void mlx4_ib_event(struct mlx4_dev *dev, void *ibdev_ptr,
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03001552 enum mlx4_dev_event event, unsigned long param)
Roland Dreier225c7b12007-05-08 18:00:38 -07001553{
1554 struct ib_event ibev;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07001555 struct mlx4_ib_dev *ibdev = to_mdev((struct ib_device *) ibdev_ptr);
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03001556 struct mlx4_eqe *eqe = NULL;
1557 struct ib_event_work *ew;
Jack Morgensteinfc065732012-08-03 08:40:42 +00001558 int p = 0;
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03001559
1560 if (event == MLX4_DEV_EVENT_PORT_MGMT_CHANGE)
1561 eqe = (struct mlx4_eqe *)param;
1562 else
Jack Morgensteinfc065732012-08-03 08:40:42 +00001563 p = (int) param;
Roland Dreier225c7b12007-05-08 18:00:38 -07001564
1565 switch (event) {
Roland Dreier37608ee2008-04-16 21:01:08 -07001566 case MLX4_DEV_EVENT_PORT_UP:
Jack Morgensteinfc065732012-08-03 08:40:42 +00001567 if (p > ibdev->num_ports)
1568 return;
Roland Dreier37608ee2008-04-16 21:01:08 -07001569 ibev.event = IB_EVENT_PORT_ACTIVE;
Roland Dreier225c7b12007-05-08 18:00:38 -07001570 break;
1571
Roland Dreier37608ee2008-04-16 21:01:08 -07001572 case MLX4_DEV_EVENT_PORT_DOWN:
Jack Morgensteinfc065732012-08-03 08:40:42 +00001573 if (p > ibdev->num_ports)
1574 return;
Roland Dreier37608ee2008-04-16 21:01:08 -07001575 ibev.event = IB_EVENT_PORT_ERR;
1576 break;
1577
1578 case MLX4_DEV_EVENT_CATASTROPHIC_ERROR:
Jack Morgenstein3b4a8cd2009-09-05 20:24:50 -07001579 ibdev->ib_active = false;
Roland Dreier225c7b12007-05-08 18:00:38 -07001580 ibev.event = IB_EVENT_DEVICE_FATAL;
1581 break;
1582
Jack Morgenstein00f5ce92012-06-19 11:21:40 +03001583 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
Jack Morgensteinfc065732012-08-03 08:40:42 +00001596 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
Roland Dreier225c7b12007-05-08 18:00:38 -07001606 default:
1607 return;
1608 }
1609
1610 ibev.device = ibdev_ptr;
Jack Morgensteinfc065732012-08-03 08:40:42 +00001611 ibev.element.port_num = (u8) p;
Roland Dreier225c7b12007-05-08 18:00:38 -07001612
1613 ib_dispatch_event(&ibev);
1614}
1615
1616static struct mlx4_interface mlx4_ib_interface = {
Eli Cohenfa417f72010-10-24 21:08:52 -07001617 .add = mlx4_ib_add,
1618 .remove = mlx4_ib_remove,
1619 .event = mlx4_ib_event,
Yevgeny Petrilin03455842011-03-22 22:38:17 +00001620 .protocol = MLX4_PROT_IB_IPV6
Roland Dreier225c7b12007-05-08 18:00:38 -07001621};
1622
1623static int __init mlx4_ib_init(void)
1624{
Eli Cohenfa417f72010-10-24 21:08:52 -07001625 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;
Roland Dreier225c7b12007-05-08 18:00:38 -07001638}
1639
1640static void __exit mlx4_ib_cleanup(void)
1641{
1642 mlx4_unregister_interface(&mlx4_ib_interface);
Eli Cohenfa417f72010-10-24 21:08:52 -07001643 destroy_workqueue(wq);
Roland Dreier225c7b12007-05-08 18:00:38 -07001644}
1645
1646module_init(mlx4_ib_init);
1647module_exit(mlx4_ib_cleanup);