2 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 #include <linux/slab.h>
35 #include <linux/export.h>
36 #include <linux/errno.h>
37 #include <net/devlink.h>
41 static DEFINE_MUTEX(intf_mutex);
42 static DEFINE_IDA(mlx4_adev_ida);
44 static bool is_eth_supported(struct mlx4_dev *dev)
46 for (int port = 1; port <= dev->caps.num_ports; port++)
47 if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
53 static bool is_ib_supported(struct mlx4_dev *dev)
55 for (int port = 1; port <= dev->caps.num_ports; port++)
56 if (dev->caps.port_type[port] == MLX4_PORT_TYPE_IB)
59 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE)
65 static const struct mlx4_adev_device {
67 bool (*is_supported)(struct mlx4_dev *dev);
68 } mlx4_adev_devices[] = {
69 { "eth", is_eth_supported },
70 { "ib", is_ib_supported },
73 int mlx4_adev_init(struct mlx4_dev *dev)
75 struct mlx4_priv *priv = mlx4_priv(dev);
77 priv->adev_idx = ida_alloc(&mlx4_adev_ida, GFP_KERNEL);
78 if (priv->adev_idx < 0)
79 return priv->adev_idx;
81 priv->adev = kcalloc(ARRAY_SIZE(mlx4_adev_devices),
82 sizeof(struct mlx4_adev *), GFP_KERNEL);
84 ida_free(&mlx4_adev_ida, priv->adev_idx);
91 void mlx4_adev_cleanup(struct mlx4_dev *dev)
93 struct mlx4_priv *priv = mlx4_priv(dev);
96 ida_free(&mlx4_adev_ida, priv->adev_idx);
99 static void adev_release(struct device *dev)
101 struct mlx4_adev *mlx4_adev =
102 container_of(dev, struct mlx4_adev, adev.dev);
103 struct mlx4_priv *priv = mlx4_priv(mlx4_adev->mdev);
104 int idx = mlx4_adev->idx;
107 priv->adev[idx] = NULL;
110 static struct mlx4_adev *add_adev(struct mlx4_dev *dev, int idx)
112 struct mlx4_priv *priv = mlx4_priv(dev);
113 const char *suffix = mlx4_adev_devices[idx].suffix;
114 struct auxiliary_device *adev;
115 struct mlx4_adev *madev;
118 madev = kzalloc(sizeof(*madev), GFP_KERNEL);
120 return ERR_PTR(-ENOMEM);
123 adev->id = priv->adev_idx;
125 adev->dev.parent = &dev->persist->pdev->dev;
126 adev->dev.release = adev_release;
130 ret = auxiliary_device_init(adev);
136 ret = auxiliary_device_add(adev);
138 auxiliary_device_uninit(adev);
144 static void del_adev(struct auxiliary_device *adev)
146 auxiliary_device_delete(adev);
147 auxiliary_device_uninit(adev);
150 int mlx4_register_auxiliary_driver(struct mlx4_adrv *madrv)
152 return auxiliary_driver_register(&madrv->adrv);
154 EXPORT_SYMBOL_GPL(mlx4_register_auxiliary_driver);
156 void mlx4_unregister_auxiliary_driver(struct mlx4_adrv *madrv)
158 auxiliary_driver_unregister(&madrv->adrv);
160 EXPORT_SYMBOL_GPL(mlx4_unregister_auxiliary_driver);
162 int mlx4_do_bond(struct mlx4_dev *dev, bool enable)
164 struct mlx4_priv *priv = mlx4_priv(dev);
167 if (!(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_PORT_REMAP))
170 ret = mlx4_disable_rx_port_check(dev, enable);
172 mlx4_err(dev, "Fail to %s rx port check\n",
173 enable ? "enable" : "disable");
177 dev->flags |= MLX4_FLAG_BONDED;
179 ret = mlx4_virt2phy_port_map(dev, 1, 2);
181 mlx4_err(dev, "Fail to reset port map\n");
184 dev->flags &= ~MLX4_FLAG_BONDED;
187 mutex_lock(&intf_mutex);
189 for (i = 0; i < ARRAY_SIZE(mlx4_adev_devices); i++) {
190 struct mlx4_adev *madev = priv->adev[i];
191 struct mlx4_adrv *madrv;
192 enum mlx4_protocol protocol;
197 device_lock(&madev->adev.dev);
198 if (!madev->adev.dev.driver) {
199 device_unlock(&madev->adev.dev);
203 madrv = container_of(madev->adev.dev.driver, struct mlx4_adrv,
205 if (!(madrv->flags & MLX4_INTFF_BONDING)) {
206 device_unlock(&madev->adev.dev);
210 if (mlx4_is_mfunc(dev)) {
212 "SRIOV, disabled HA mode for intf proto %d\n",
214 device_unlock(&madev->adev.dev);
218 protocol = madrv->protocol;
219 device_unlock(&madev->adev.dev);
221 del_adev(&madev->adev);
222 priv->adev[i] = add_adev(dev, i);
223 if (IS_ERR(priv->adev[i])) {
224 mlx4_warn(dev, "Device[%d] (%s) failed to load\n", i,
225 mlx4_adev_devices[i].suffix);
226 priv->adev[i] = NULL;
231 "Interface for protocol %d restarted with bonded mode %s\n",
232 protocol, enable ? "enabled" : "disabled");
235 mutex_unlock(&intf_mutex);
240 void mlx4_dispatch_event(struct mlx4_dev *dev, enum mlx4_dev_event type,
243 struct mlx4_priv *priv = mlx4_priv(dev);
245 atomic_notifier_call_chain(&priv->event_nh, type, param);
248 int mlx4_register_event_notifier(struct mlx4_dev *dev,
249 struct notifier_block *nb)
251 struct mlx4_priv *priv = mlx4_priv(dev);
253 return atomic_notifier_chain_register(&priv->event_nh, nb);
255 EXPORT_SYMBOL(mlx4_register_event_notifier);
257 int mlx4_unregister_event_notifier(struct mlx4_dev *dev,
258 struct notifier_block *nb)
260 struct mlx4_priv *priv = mlx4_priv(dev);
262 return atomic_notifier_chain_unregister(&priv->event_nh, nb);
264 EXPORT_SYMBOL(mlx4_unregister_event_notifier);
266 static int add_drivers(struct mlx4_dev *dev)
268 struct mlx4_priv *priv = mlx4_priv(dev);
271 for (i = 0; i < ARRAY_SIZE(mlx4_adev_devices); i++) {
272 bool is_supported = false;
277 if (mlx4_adev_devices[i].is_supported)
278 is_supported = mlx4_adev_devices[i].is_supported(dev);
283 priv->adev[i] = add_adev(dev, i);
284 if (IS_ERR(priv->adev[i])) {
285 mlx4_warn(dev, "Device[%d] (%s) failed to load\n", i,
286 mlx4_adev_devices[i].suffix);
287 /* We continue to rescan drivers and leave to the caller
288 * to make decision if to release everything or
290 ret = PTR_ERR(priv->adev[i]);
291 priv->adev[i] = NULL;
297 static void delete_drivers(struct mlx4_dev *dev)
299 struct mlx4_priv *priv = mlx4_priv(dev);
303 delete_all = !(dev->persist->interface_state & MLX4_INTERFACE_STATE_UP);
305 for (i = ARRAY_SIZE(mlx4_adev_devices) - 1; i >= 0; i--) {
306 bool is_supported = false;
311 if (mlx4_adev_devices[i].is_supported && !delete_all)
312 is_supported = mlx4_adev_devices[i].is_supported(dev);
317 del_adev(&priv->adev[i]->adev);
318 priv->adev[i] = NULL;
322 /* This function is used after mlx4_dev is reconfigured.
324 static int rescan_drivers_locked(struct mlx4_dev *dev)
326 lockdep_assert_held(&intf_mutex);
329 if (!(dev->persist->interface_state & MLX4_INTERFACE_STATE_UP))
332 return add_drivers(dev);
335 int mlx4_register_device(struct mlx4_dev *dev)
339 mutex_lock(&intf_mutex);
341 dev->persist->interface_state |= MLX4_INTERFACE_STATE_UP;
343 ret = rescan_drivers_locked(dev);
345 mutex_unlock(&intf_mutex);
348 mlx4_unregister_device(dev);
352 mlx4_start_catas_poll(dev);
357 void mlx4_unregister_device(struct mlx4_dev *dev)
359 if (!(dev->persist->interface_state & MLX4_INTERFACE_STATE_UP))
362 mlx4_stop_catas_poll(dev);
363 if (dev->persist->interface_state & MLX4_INTERFACE_STATE_DELETION &&
364 mlx4_is_slave(dev)) {
365 /* In mlx4_remove_one on a VF */
367 swab32(readl(&mlx4_priv(dev)->mfunc.comm->slave_read));
369 if (mlx4_comm_internal_err(slave_read)) {
370 mlx4_dbg(dev, "%s: comm channel is down, entering error state.\n",
372 mlx4_enter_error_state(dev->persist);
375 mutex_lock(&intf_mutex);
377 dev->persist->interface_state &= ~MLX4_INTERFACE_STATE_UP;
379 rescan_drivers_locked(dev);
381 mutex_unlock(&intf_mutex);
384 struct devlink_port *mlx4_get_devlink_port(struct mlx4_dev *dev, int port)
386 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
388 return &info->devlink_port;
390 EXPORT_SYMBOL_GPL(mlx4_get_devlink_port);