summaryrefslogtreecommitdiffstats
path: root/drivers/net/ethernet/mellanox/mlx4/intf.c
blob: a371b970ac1e29d68411a3e8febdaad05e425df7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <linux/slab.h>
#include <linux/export.h>
#include <linux/errno.h>
#include <net/devlink.h>

#include "mlx4.h"

static DEFINE_MUTEX(intf_mutex);
static DEFINE_IDA(mlx4_adev_ida);

static bool is_eth_supported(struct mlx4_dev *dev)
{
	for (int port = 1; port <= dev->caps.num_ports; port++)
		if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
			return true;

	return false;
}

static bool is_ib_supported(struct mlx4_dev *dev)
{
	for (int port = 1; port <= dev->caps.num_ports; port++)
		if (dev->caps.port_type[port] == MLX4_PORT_TYPE_IB)
			return true;

	if (dev->caps.flags & MLX4_DEV_CAP_FLAG_IBOE)
		return true;

	return false;
}

static const struct mlx4_adev_device {
	const char *suffix;
	bool (*is_supported)(struct mlx4_dev *dev);
} mlx4_adev_devices[] = {
	{ "eth", is_eth_supported },
	{ "ib", is_ib_supported },
};

int mlx4_adev_init(struct mlx4_dev *dev)
{
	struct mlx4_priv *priv = mlx4_priv(dev);

	priv->adev_idx = ida_alloc(&mlx4_adev_ida, GFP_KERNEL);
	if (priv->adev_idx < 0)
		return priv->adev_idx;

	priv->adev = kcalloc(ARRAY_SIZE(mlx4_adev_devices),
			     sizeof(struct mlx4_adev *), GFP_KERNEL);
	if (!priv->adev) {
		ida_free(&mlx4_adev_ida, priv->adev_idx);
		return -ENOMEM;
	}

	return 0;
}

void mlx4_adev_cleanup(struct mlx4_dev *dev)
{
	struct mlx4_priv *priv = mlx4_priv(dev);

	kfree(priv->adev);
	ida_free(&mlx4_adev_ida, priv->adev_idx);
}

static void adev_release(struct device *dev)
{
	struct mlx4_adev *mlx4_adev =
		container_of(dev, struct mlx4_adev, adev.dev);
	struct mlx4_priv *priv = mlx4_priv(mlx4_adev->mdev);
	int idx = mlx4_adev->idx;

	kfree(mlx4_adev);
	priv->adev[idx] = NULL;
}

static struct mlx4_adev *add_adev(struct mlx4_dev *dev, int idx)
{
	struct mlx4_priv *priv = mlx4_priv(dev);
	const char *suffix = mlx4_adev_devices[idx].suffix;
	struct auxiliary_device *adev;
	struct mlx4_adev *madev;
	int ret;

	madev = kzalloc(sizeof(*madev), GFP_KERNEL);
	if (!madev)
		return ERR_PTR(-ENOMEM);

	adev = &madev->adev;
	adev->id = priv->adev_idx;
	adev->name = suffix;
	adev->dev.parent = &dev->persist->pdev->dev;
	adev->dev.release = adev_release;
	madev->mdev = dev;
	madev->idx = idx;

	ret = auxiliary_device_init(adev);
	if (ret) {
		kfree(madev);
		return ERR_PTR(ret);
	}

	ret = auxiliary_device_add(adev);
	if (ret) {
		auxiliary_device_uninit(adev);
		return ERR_PTR(ret);
	}
	return madev;
}

static void del_adev(struct auxiliary_device *adev)
{
	auxiliary_device_delete(adev);
	auxiliary_device_uninit(adev);
}

int mlx4_register_auxiliary_driver(struct mlx4_adrv *madrv)
{
	return auxiliary_driver_register(&madrv->adrv);
}
EXPORT_SYMBOL_GPL(mlx4_register_auxiliary_driver);

void mlx4_unregister_auxiliary_driver(struct mlx4_adrv *madrv)
{
	auxiliary_driver_unregister(&madrv->adrv);
}
EXPORT_SYMBOL_GPL(mlx4_unregister_auxiliary_driver);

int mlx4_do_bond(struct mlx4_dev *dev, bool enable)
{
	struct mlx4_priv *priv = mlx4_priv(dev);
	int i, ret;

	if (!(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_PORT_REMAP))
		return -EOPNOTSUPP;

	ret = mlx4_disable_rx_port_check(dev, enable);
	if (ret) {
		mlx4_err(dev, "Fail to %s rx port check\n",
			 enable ? "enable" : "disable");
		return ret;
	}
	if (enable) {
		dev->flags |= MLX4_FLAG_BONDED;
	} else {
		ret = mlx4_virt2phy_port_map(dev, 1, 2);
		if (ret) {
			mlx4_err(dev, "Fail to reset port map\n");
			return ret;
		}
		dev->flags &= ~MLX4_FLAG_BONDED;
	}

	mutex_lock(&intf_mutex);

	for (i = 0; i < ARRAY_SIZE(mlx4_adev_devices); i++) {
		struct mlx4_adev *madev = priv->adev[i];
		struct mlx4_adrv *madrv;
		enum mlx4_protocol protocol;

		if (!madev)
			continue;

		device_lock(&madev->adev.dev);
		if (!madev->adev.dev.driver) {
			device_unlock(&madev->adev.dev);
			continue;
		}

		madrv = container_of(madev->adev.dev.driver, struct mlx4_adrv,
				     adrv.driver);
		if (!(madrv->flags & MLX4_INTFF_BONDING)) {
			device_unlock(&madev->adev.dev);
			continue;
		}

		if (mlx4_is_mfunc(dev)) {
			mlx4_dbg(dev,
				 "SRIOV, disabled HA mode for intf proto %d\n",
				 madrv->protocol);
			device_unlock(&madev->adev.dev);
			continue;
		}

		protocol = madrv->protocol;
		device_unlock(&madev->adev.dev);

		del_adev(&madev->adev);
		priv->adev[i] = add_adev(dev, i);
		if (IS_ERR(priv->adev[i])) {
			mlx4_warn(dev, "Device[%d] (%s) failed to load\n", i,
				  mlx4_adev_devices[i].suffix);
			priv->adev[i] = NULL;
			continue;
		}

		mlx4_dbg(dev,
			 "Interface for protocol %d restarted with bonded mode %s\n",
			 protocol, enable ? "enabled" : "disabled");
	}

	mutex_unlock(&intf_mutex);

	return 0;
}

void mlx4_dispatch_event(struct mlx4_dev *dev, enum mlx4_dev_event type,
			 void *param)
{
	struct mlx4_priv *priv = mlx4_priv(dev);

	atomic_notifier_call_chain(&priv->event_nh, type, param);
}

int mlx4_register_event_notifier(struct mlx4_dev *dev,
				 struct notifier_block *nb)
{
	struct mlx4_priv *priv = mlx4_priv(dev);

	return atomic_notifier_chain_register(&priv->event_nh, nb);
}
EXPORT_SYMBOL(mlx4_register_event_notifier);

int mlx4_unregister_event_notifier(struct mlx4_dev *dev,
				   struct notifier_block *nb)
{
	struct mlx4_priv *priv = mlx4_priv(dev);

	return atomic_notifier_chain_unregister(&priv->event_nh, nb);
}
EXPORT_SYMBOL(mlx4_unregister_event_notifier);

static int add_drivers(struct mlx4_dev *dev)
{
	struct mlx4_priv *priv = mlx4_priv(dev);
	int i, ret = 0;

	for (i = 0; i < ARRAY_SIZE(mlx4_adev_devices); i++) {
		bool is_supported = false;

		if (priv->adev[i])
			continue;

		if (mlx4_adev_devices[i].is_supported)
			is_supported = mlx4_adev_devices[i].is_supported(dev);

		if (!is_supported)
			continue;

		priv->adev[i] = add_adev(dev, i);
		if (IS_ERR(priv->adev[i])) {
			mlx4_warn(dev, "Device[%d] (%s) failed to load\n", i,
				  mlx4_adev_devices[i].suffix);
			/* We continue to rescan drivers and leave to the caller
			 * to make decision if to release everything or
			 * continue. */
			ret = PTR_ERR(priv->adev[i]);
			priv->adev[i] = NULL;
		}
	}
	return ret;
}

static void delete_drivers(struct mlx4_dev *dev)
{
	struct mlx4_priv *priv = mlx4_priv(dev);
	bool delete_all;
	int i;

	delete_all = !(dev->persist->interface_state & MLX4_INTERFACE_STATE_UP);

	for (i = ARRAY_SIZE(mlx4_adev_devices) - 1; i >= 0; i--) {
		bool is_supported = false;

		if (!priv->adev[i])
			continue;

		if (mlx4_adev_devices[i].is_supported && !delete_all)
			is_supported = mlx4_adev_devices[i].is_supported(dev);

		if (is_supported)
			continue;

		del_adev(&priv->adev[i]->adev);
		priv->adev[i] = NULL;
	}
}

/* This function is used after mlx4_dev is reconfigured.
 */
static int rescan_drivers_locked(struct mlx4_dev *dev)
{
	lockdep_assert_held(&intf_mutex);

	delete_drivers(dev);
	if (!(dev->persist->interface_state & MLX4_INTERFACE_STATE_UP))
		return 0;

	return add_drivers(dev);
}

int mlx4_register_device(struct mlx4_dev *dev)
{
	int ret;

	mutex_lock(&intf_mutex);

	dev->persist->interface_state |= MLX4_INTERFACE_STATE_UP;

	ret = rescan_drivers_locked(dev);

	mutex_unlock(&intf_mutex);

	if (ret) {
		mlx4_unregister_device(dev);
		return ret;
	}

	mlx4_start_catas_poll(dev);

	return ret;
}

void mlx4_unregister_device(struct mlx4_dev *dev)
{
	if (!(dev->persist->interface_state & MLX4_INTERFACE_STATE_UP))
		return;

	mlx4_stop_catas_poll(dev);
	if (dev->persist->interface_state & MLX4_INTERFACE_STATE_DELETION &&
	    mlx4_is_slave(dev)) {
		/* In mlx4_remove_one on a VF */
		u32 slave_read =
			swab32(readl(&mlx4_priv(dev)->mfunc.comm->slave_read));

		if (mlx4_comm_internal_err(slave_read)) {
			mlx4_dbg(dev, "%s: comm channel is down, entering error state.\n",
				 __func__);
			mlx4_enter_error_state(dev->persist);
		}
	}
	mutex_lock(&intf_mutex);

	dev->persist->interface_state &= ~MLX4_INTERFACE_STATE_UP;

	rescan_drivers_locked(dev);

	mutex_unlock(&intf_mutex);
}

struct devlink_port *mlx4_get_devlink_port(struct mlx4_dev *dev, int port)
{
	struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];

	return &info->devlink_port;
}
EXPORT_SYMBOL_GPL(mlx4_get_devlink_port);