1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2016-2019 Mellanox Technologies. All rights reserved */
4 #include <linux/netdevice.h>
5 #include <linux/etherdevice.h>
6 #include <linux/ethtool.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/types.h>
17 static const char mlxsw_m_driver_name[] = "mlxsw_minimal";
22 struct mlxsw_m_port **ports;
24 struct mlxsw_core *core;
25 const struct mlxsw_bus_info *bus_info;
26 u8 base_mac[ETH_ALEN];
31 struct net_device *dev;
32 struct mlxsw_m *mlxsw_m;
37 static int mlxsw_m_port_dummy_open_stop(struct net_device *dev)
43 mlxsw_m_port_get_phys_port_name(struct net_device *dev, char *name, size_t len)
45 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev);
46 struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
47 u8 local_port = mlxsw_m_port->local_port;
49 return mlxsw_core_port_get_phys_port_name(core, local_port, name, len);
52 static int mlxsw_m_port_get_port_parent_id(struct net_device *dev,
53 struct netdev_phys_item_id *ppid)
55 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev);
56 struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;
58 ppid->id_len = sizeof(mlxsw_m->base_mac);
59 memcpy(&ppid->id, &mlxsw_m->base_mac, ppid->id_len);
64 static const struct net_device_ops mlxsw_m_port_netdev_ops = {
65 .ndo_open = mlxsw_m_port_dummy_open_stop,
66 .ndo_stop = mlxsw_m_port_dummy_open_stop,
67 .ndo_get_phys_port_name = mlxsw_m_port_get_phys_port_name,
68 .ndo_get_port_parent_id = mlxsw_m_port_get_port_parent_id,
71 static int mlxsw_m_get_module_info(struct net_device *netdev,
72 struct ethtool_modinfo *modinfo)
74 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
75 struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
77 return mlxsw_env_get_module_info(core, mlxsw_m_port->module, modinfo);
81 mlxsw_m_get_module_eeprom(struct net_device *netdev, struct ethtool_eeprom *ee,
84 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
85 struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
87 return mlxsw_env_get_module_eeprom(netdev, core, mlxsw_m_port->module,
91 static const struct ethtool_ops mlxsw_m_port_ethtool_ops = {
92 .get_module_info = mlxsw_m_get_module_info,
93 .get_module_eeprom = mlxsw_m_get_module_eeprom,
97 mlxsw_m_port_module_info_get(struct mlxsw_m *mlxsw_m, u8 local_port,
98 u8 *p_module, u8 *p_width)
100 char pmlp_pl[MLXSW_REG_PMLP_LEN];
103 mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
104 err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(pmlp), pmlp_pl);
107 *p_module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0);
108 *p_width = mlxsw_reg_pmlp_width_get(pmlp_pl);
114 mlxsw_m_port_dev_addr_get(struct mlxsw_m_port *mlxsw_m_port)
116 struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;
117 struct net_device *dev = mlxsw_m_port->dev;
118 char ppad_pl[MLXSW_REG_PPAD_LEN];
121 mlxsw_reg_ppad_pack(ppad_pl, false, 0);
122 err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(ppad), ppad_pl);
125 mlxsw_reg_ppad_mac_memcpy_from(ppad_pl, dev->dev_addr);
126 /* The last byte value in base mac address is guaranteed
127 * to be such it does not overflow when adding local_port
130 dev->dev_addr[ETH_ALEN - 1] += mlxsw_m_port->module + 1;
135 mlxsw_m_port_create(struct mlxsw_m *mlxsw_m, u8 local_port, u8 module)
137 struct mlxsw_m_port *mlxsw_m_port;
138 struct net_device *dev;
141 err = mlxsw_core_port_init(mlxsw_m->core, local_port);
143 dev_err(mlxsw_m->bus_info->dev, "Port %d: Failed to init core port\n",
148 dev = alloc_etherdev(sizeof(struct mlxsw_m_port));
151 goto err_alloc_etherdev;
154 SET_NETDEV_DEV(dev, mlxsw_m->bus_info->dev);
155 mlxsw_m_port = netdev_priv(dev);
156 mlxsw_m_port->dev = dev;
157 mlxsw_m_port->mlxsw_m = mlxsw_m;
158 mlxsw_m_port->local_port = local_port;
159 mlxsw_m_port->module = module;
161 dev->netdev_ops = &mlxsw_m_port_netdev_ops;
162 dev->ethtool_ops = &mlxsw_m_port_ethtool_ops;
164 err = mlxsw_m_port_dev_addr_get(mlxsw_m_port);
166 dev_err(mlxsw_m->bus_info->dev, "Port %d: Unable to get port mac address\n",
167 mlxsw_m_port->local_port);
168 goto err_dev_addr_get;
171 netif_carrier_off(dev);
172 mlxsw_m->ports[local_port] = mlxsw_m_port;
173 err = register_netdev(dev);
175 dev_err(mlxsw_m->bus_info->dev, "Port %d: Failed to register netdev\n",
176 mlxsw_m_port->local_port);
177 goto err_register_netdev;
180 mlxsw_core_port_eth_set(mlxsw_m->core, mlxsw_m_port->local_port,
181 mlxsw_m_port, dev, module + 1, false, 0);
186 mlxsw_m->ports[local_port] = NULL;
190 mlxsw_core_port_fini(mlxsw_m->core, local_port);
194 static void mlxsw_m_port_remove(struct mlxsw_m *mlxsw_m, u8 local_port)
196 struct mlxsw_m_port *mlxsw_m_port = mlxsw_m->ports[local_port];
198 mlxsw_core_port_clear(mlxsw_m->core, local_port, mlxsw_m);
199 unregister_netdev(mlxsw_m_port->dev); /* This calls ndo_stop */
200 mlxsw_m->ports[local_port] = NULL;
201 free_netdev(mlxsw_m_port->dev);
202 mlxsw_core_port_fini(mlxsw_m->core, local_port);
205 static int mlxsw_m_port_module_map(struct mlxsw_m *mlxsw_m, u8 local_port,
211 /* Fill out to local port mapping array */
212 err = mlxsw_m_port_module_info_get(mlxsw_m, local_port, &module,
219 /* Skip, if port belongs to the cluster */
220 if (module == *last_module)
222 *last_module = module;
223 mlxsw_m->module_to_port[module] = ++mlxsw_m->max_ports;
228 static void mlxsw_m_port_module_unmap(struct mlxsw_m *mlxsw_m, u8 module)
230 mlxsw_m->module_to_port[module] = -1;
233 static int mlxsw_m_ports_create(struct mlxsw_m *mlxsw_m)
235 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_m->core);
236 u8 last_module = max_ports;
240 mlxsw_m->ports = kcalloc(max_ports, sizeof(*mlxsw_m->ports),
245 mlxsw_m->module_to_port = kmalloc_array(max_ports, sizeof(int),
247 if (!mlxsw_m->module_to_port) {
249 goto err_module_to_port_alloc;
252 /* Invalidate the entries of module to local port mapping array */
253 for (i = 0; i < max_ports; i++)
254 mlxsw_m->module_to_port[i] = -1;
256 /* Fill out module to local port mapping array */
257 for (i = 1; i < max_ports; i++) {
258 err = mlxsw_m_port_module_map(mlxsw_m, i, &last_module);
260 goto err_module_to_port_map;
263 /* Create port objects for each valid entry */
264 for (i = 0; i < mlxsw_m->max_ports; i++) {
265 if (mlxsw_m->module_to_port[i] > 0) {
266 err = mlxsw_m_port_create(mlxsw_m,
267 mlxsw_m->module_to_port[i],
270 goto err_module_to_port_create;
276 err_module_to_port_create:
277 for (i--; i >= 0; i--) {
278 if (mlxsw_m->module_to_port[i] > 0)
279 mlxsw_m_port_remove(mlxsw_m,
280 mlxsw_m->module_to_port[i]);
283 err_module_to_port_map:
284 for (i--; i > 0; i--)
285 mlxsw_m_port_module_unmap(mlxsw_m, i);
286 kfree(mlxsw_m->module_to_port);
287 err_module_to_port_alloc:
288 kfree(mlxsw_m->ports);
292 static void mlxsw_m_ports_remove(struct mlxsw_m *mlxsw_m)
296 for (i = 0; i < mlxsw_m->max_ports; i++) {
297 if (mlxsw_m->module_to_port[i] > 0) {
298 mlxsw_m_port_remove(mlxsw_m,
299 mlxsw_m->module_to_port[i]);
300 mlxsw_m_port_module_unmap(mlxsw_m, i);
304 kfree(mlxsw_m->module_to_port);
305 kfree(mlxsw_m->ports);
308 static int mlxsw_m_init(struct mlxsw_core *mlxsw_core,
309 const struct mlxsw_bus_info *mlxsw_bus_info)
311 struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core);
314 mlxsw_m->core = mlxsw_core;
315 mlxsw_m->bus_info = mlxsw_bus_info;
317 err = mlxsw_m_ports_create(mlxsw_m);
319 dev_err(mlxsw_m->bus_info->dev, "Failed to create ports\n");
326 static void mlxsw_m_fini(struct mlxsw_core *mlxsw_core)
328 struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core);
330 mlxsw_m_ports_remove(mlxsw_m);
333 static const struct mlxsw_config_profile mlxsw_m_config_profile;
335 static struct mlxsw_driver mlxsw_m_driver = {
336 .kind = mlxsw_m_driver_name,
337 .priv_size = sizeof(struct mlxsw_m),
338 .init = mlxsw_m_init,
339 .fini = mlxsw_m_fini,
340 .profile = &mlxsw_m_config_profile,
341 .res_query_enabled = true,
344 static const struct i2c_device_id mlxsw_m_i2c_id[] = {
345 { "mlxsw_minimal", 0},
349 static struct i2c_driver mlxsw_m_i2c_driver = {
350 .driver.name = "mlxsw_minimal",
351 .class = I2C_CLASS_HWMON,
352 .id_table = mlxsw_m_i2c_id,
355 static int __init mlxsw_m_module_init(void)
359 err = mlxsw_core_driver_register(&mlxsw_m_driver);
363 err = mlxsw_i2c_driver_register(&mlxsw_m_i2c_driver);
365 goto err_i2c_driver_register;
369 err_i2c_driver_register:
370 mlxsw_core_driver_unregister(&mlxsw_m_driver);
375 static void __exit mlxsw_m_module_exit(void)
377 mlxsw_i2c_driver_unregister(&mlxsw_m_i2c_driver);
378 mlxsw_core_driver_unregister(&mlxsw_m_driver);
381 module_init(mlxsw_m_module_init);
382 module_exit(mlxsw_m_module_exit);
384 MODULE_LICENSE("Dual BSD/GPL");
385 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
386 MODULE_DESCRIPTION("Mellanox minimal driver");
387 MODULE_DEVICE_TABLE(i2c, mlxsw_m_i2c_id);