minimal.c 9.4 KB
Newer Older
1
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2
/* Copyright (c) 2016-2019 Mellanox Technologies. All rights reserved */
3

4 5 6
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
7 8 9 10 11 12 13
#include <linux/i2c.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mod_devicetable.h>
#include <linux/types.h>

#include "core.h"
14
#include "core_env.h"
15 16
#include "i2c.h"

17
static const char mlxsw_m_driver_name[] = "mlxsw_minimal";
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
struct mlxsw_m_port;

struct mlxsw_m {
	struct mlxsw_m_port **ports;
	int *module_to_port;
	struct mlxsw_core *core;
	const struct mlxsw_bus_info *bus_info;
	u8 base_mac[ETH_ALEN];
	u8 max_ports;
};

struct mlxsw_m_port {
	struct net_device *dev;
	struct mlxsw_m *mlxsw_m;
	u8 local_port;
	u8 module;
};

static int mlxsw_m_port_dummy_open_stop(struct net_device *dev)
{
	return 0;
}

static int
mlxsw_m_port_get_phys_port_name(struct net_device *dev, char *name, size_t len)
{
	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev);
	struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
	u8 local_port = mlxsw_m_port->local_port;

	return mlxsw_core_port_get_phys_port_name(core, local_port, name, len);
}

static int mlxsw_m_port_get_port_parent_id(struct net_device *dev,
					   struct netdev_phys_item_id *ppid)
{
	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev);
	struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;

	ppid->id_len = sizeof(mlxsw_m->base_mac);
	memcpy(&ppid->id, &mlxsw_m->base_mac, ppid->id_len);

	return 0;
}

static const struct net_device_ops mlxsw_m_port_netdev_ops = {
	.ndo_open		= mlxsw_m_port_dummy_open_stop,
	.ndo_stop		= mlxsw_m_port_dummy_open_stop,
	.ndo_get_phys_port_name	= mlxsw_m_port_get_phys_port_name,
	.ndo_get_port_parent_id	= mlxsw_m_port_get_port_parent_id,
};

static int mlxsw_m_get_module_info(struct net_device *netdev,
				   struct ethtool_modinfo *modinfo)
{
	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
	struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;

	return mlxsw_env_get_module_info(core, mlxsw_m_port->module, modinfo);
}

static int
mlxsw_m_get_module_eeprom(struct net_device *netdev, struct ethtool_eeprom *ee,
			  u8 *data)
{
	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
	struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;

	return mlxsw_env_get_module_eeprom(netdev, core, mlxsw_m_port->module,
					   ee, data);
}

static const struct ethtool_ops mlxsw_m_port_ethtool_ops = {
	.get_module_info	= mlxsw_m_get_module_info,
	.get_module_eeprom	= mlxsw_m_get_module_eeprom,
};

static int
mlxsw_m_port_module_info_get(struct mlxsw_m *mlxsw_m, u8 local_port,
			     u8 *p_module, u8 *p_width)
{
	char pmlp_pl[MLXSW_REG_PMLP_LEN];
	int err;

	mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
	err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(pmlp), pmlp_pl);
	if (err)
		return err;
	*p_module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0);
	*p_width = mlxsw_reg_pmlp_width_get(pmlp_pl);

	return 0;
}

static int
mlxsw_m_port_dev_addr_get(struct mlxsw_m_port *mlxsw_m_port)
{
	struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;
	struct net_device *dev = mlxsw_m_port->dev;
	char ppad_pl[MLXSW_REG_PPAD_LEN];
	int err;

	mlxsw_reg_ppad_pack(ppad_pl, false, 0);
	err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(ppad), ppad_pl);
	if (err)
		return err;
	mlxsw_reg_ppad_mac_memcpy_from(ppad_pl, dev->dev_addr);
	/* The last byte value in base mac address is guaranteed
	 * to be such it does not overflow when adding local_port
	 * value.
	 */
	dev->dev_addr[ETH_ALEN - 1] += mlxsw_m_port->module + 1;
	return 0;
}

static int
mlxsw_m_port_create(struct mlxsw_m *mlxsw_m, u8 local_port, u8 module)
{
	struct mlxsw_m_port *mlxsw_m_port;
	struct net_device *dev;
	int err;

	err = mlxsw_core_port_init(mlxsw_m->core, local_port);
	if (err) {
		dev_err(mlxsw_m->bus_info->dev, "Port %d: Failed to init core port\n",
			local_port);
		return err;
	}

	dev = alloc_etherdev(sizeof(struct mlxsw_m_port));
	if (!dev) {
		err = -ENOMEM;
		goto err_alloc_etherdev;
	}

	SET_NETDEV_DEV(dev, mlxsw_m->bus_info->dev);
	mlxsw_m_port = netdev_priv(dev);
	mlxsw_m_port->dev = dev;
	mlxsw_m_port->mlxsw_m = mlxsw_m;
	mlxsw_m_port->local_port = local_port;
	mlxsw_m_port->module = module;

	dev->netdev_ops = &mlxsw_m_port_netdev_ops;
	dev->ethtool_ops = &mlxsw_m_port_ethtool_ops;

	err = mlxsw_m_port_dev_addr_get(mlxsw_m_port);
	if (err) {
		dev_err(mlxsw_m->bus_info->dev, "Port %d: Unable to get port mac address\n",
			mlxsw_m_port->local_port);
		goto err_dev_addr_get;
	}

	netif_carrier_off(dev);
	mlxsw_m->ports[local_port] = mlxsw_m_port;
	err = register_netdev(dev);
	if (err) {
		dev_err(mlxsw_m->bus_info->dev, "Port %d: Failed to register netdev\n",
			mlxsw_m_port->local_port);
		goto err_register_netdev;
	}

	mlxsw_core_port_eth_set(mlxsw_m->core, mlxsw_m_port->local_port,
				mlxsw_m_port, dev, module + 1, false, 0);

	return 0;

err_register_netdev:
	mlxsw_m->ports[local_port] = NULL;
	free_netdev(dev);
err_dev_addr_get:
err_alloc_etherdev:
	mlxsw_core_port_fini(mlxsw_m->core, local_port);
	return err;
}

static void mlxsw_m_port_remove(struct mlxsw_m *mlxsw_m, u8 local_port)
{
	struct mlxsw_m_port *mlxsw_m_port = mlxsw_m->ports[local_port];

	mlxsw_core_port_clear(mlxsw_m->core, local_port, mlxsw_m);
	unregister_netdev(mlxsw_m_port->dev); /* This calls ndo_stop */
	mlxsw_m->ports[local_port] = NULL;
	free_netdev(mlxsw_m_port->dev);
	mlxsw_core_port_fini(mlxsw_m->core, local_port);
}

static int mlxsw_m_port_module_map(struct mlxsw_m *mlxsw_m, u8 local_port,
				   u8 *last_module)
{
	u8 module, width;
	int err;

	/* Fill out to local port mapping array */
	err = mlxsw_m_port_module_info_get(mlxsw_m, local_port, &module,
					   &width);
	if (err)
		return err;

	if (!width)
		return 0;
	/* Skip, if port belongs to the cluster */
	if (module == *last_module)
		return 0;
	*last_module = module;
	mlxsw_m->module_to_port[module] = ++mlxsw_m->max_ports;

	return 0;
}

static void mlxsw_m_port_module_unmap(struct mlxsw_m *mlxsw_m, u8 module)
{
	mlxsw_m->module_to_port[module] = -1;
}

static int mlxsw_m_ports_create(struct mlxsw_m *mlxsw_m)
{
	unsigned int max_ports = mlxsw_core_max_ports(mlxsw_m->core);
	u8 last_module = max_ports;
	int i;
	int err;

	mlxsw_m->ports = kcalloc(max_ports, sizeof(*mlxsw_m->ports),
				 GFP_KERNEL);
	if (!mlxsw_m->ports)
		return -ENOMEM;

	mlxsw_m->module_to_port = kmalloc_array(max_ports, sizeof(int),
						GFP_KERNEL);
	if (!mlxsw_m->module_to_port) {
		err = -ENOMEM;
		goto err_module_to_port_alloc;
	}

	/* Invalidate the entries of module to local port mapping array */
	for (i = 0; i < max_ports; i++)
		mlxsw_m->module_to_port[i] = -1;

	/* Fill out module to local port mapping array */
	for (i = 1; i < max_ports; i++) {
		err = mlxsw_m_port_module_map(mlxsw_m, i, &last_module);
		if (err)
			goto err_module_to_port_map;
	}

	/* Create port objects for each valid entry */
	for (i = 0; i < mlxsw_m->max_ports; i++) {
		if (mlxsw_m->module_to_port[i] > 0) {
			err = mlxsw_m_port_create(mlxsw_m,
						  mlxsw_m->module_to_port[i],
						  i);
			if (err)
				goto err_module_to_port_create;
		}
	}

	return 0;

err_module_to_port_create:
	for (i--; i >= 0; i--) {
		if (mlxsw_m->module_to_port[i] > 0)
			mlxsw_m_port_remove(mlxsw_m,
					    mlxsw_m->module_to_port[i]);
	}
	i = max_ports;
err_module_to_port_map:
	for (i--; i > 0; i--)
		mlxsw_m_port_module_unmap(mlxsw_m, i);
	kfree(mlxsw_m->module_to_port);
err_module_to_port_alloc:
	kfree(mlxsw_m->ports);
	return err;
}

static void mlxsw_m_ports_remove(struct mlxsw_m *mlxsw_m)
{
	int i;

	for (i = 0; i < mlxsw_m->max_ports; i++) {
		if (mlxsw_m->module_to_port[i] > 0) {
			mlxsw_m_port_remove(mlxsw_m,
					    mlxsw_m->module_to_port[i]);
			mlxsw_m_port_module_unmap(mlxsw_m, i);
		}
	}

	kfree(mlxsw_m->module_to_port);
	kfree(mlxsw_m->ports);
}

static int mlxsw_m_init(struct mlxsw_core *mlxsw_core,
			const struct mlxsw_bus_info *mlxsw_bus_info)
{
	struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core);
	int err;

	mlxsw_m->core = mlxsw_core;
	mlxsw_m->bus_info = mlxsw_bus_info;

	err = mlxsw_m_ports_create(mlxsw_m);
	if (err) {
		dev_err(mlxsw_m->bus_info->dev, "Failed to create ports\n");
		return err;
	}

	return 0;
}

static void mlxsw_m_fini(struct mlxsw_core *mlxsw_core)
{
	struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core);

	mlxsw_m_ports_remove(mlxsw_m);
}

333
static const struct mlxsw_config_profile mlxsw_m_config_profile;
334

335
static struct mlxsw_driver mlxsw_m_driver = {
336 337 338 339 340 341
	.kind			= mlxsw_m_driver_name,
	.priv_size		= sizeof(struct mlxsw_m),
	.init			= mlxsw_m_init,
	.fini			= mlxsw_m_fini,
	.profile		= &mlxsw_m_config_profile,
	.res_query_enabled	= true,
342 343
};

344
static const struct i2c_device_id mlxsw_m_i2c_id[] = {
345 346 347 348
	{ "mlxsw_minimal", 0},
	{ },
};

349
static struct i2c_driver mlxsw_m_i2c_driver = {
350 351
	.driver.name = "mlxsw_minimal",
	.class = I2C_CLASS_HWMON,
352
	.id_table = mlxsw_m_i2c_id,
353 354
};

355
static int __init mlxsw_m_module_init(void)
356 357 358
{
	int err;

359
	err = mlxsw_core_driver_register(&mlxsw_m_driver);
360 361 362
	if (err)
		return err;

363
	err = mlxsw_i2c_driver_register(&mlxsw_m_i2c_driver);
364 365 366 367 368 369
	if (err)
		goto err_i2c_driver_register;

	return 0;

err_i2c_driver_register:
370
	mlxsw_core_driver_unregister(&mlxsw_m_driver);
371 372 373 374

	return err;
}

375
static void __exit mlxsw_m_module_exit(void)
376
{
377 378
	mlxsw_i2c_driver_unregister(&mlxsw_m_i2c_driver);
	mlxsw_core_driver_unregister(&mlxsw_m_driver);
379 380
}

381 382
module_init(mlxsw_m_module_init);
module_exit(mlxsw_m_module_exit);
383 384 385 386

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
MODULE_DESCRIPTION("Mellanox minimal driver");
387
MODULE_DEVICE_TABLE(i2c, mlxsw_m_i2c_id);