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";
19 #define MLXSW_M_FWREV_MINOR 2000
20 #define MLXSW_M_FWREV_SUBMINOR 1886
22 static const struct mlxsw_fw_rev mlxsw_m_fw_rev = {
23 .minor = MLXSW_M_FWREV_MINOR,
24 .subminor = MLXSW_M_FWREV_SUBMINOR,
30 struct mlxsw_m_port **ports;
32 struct mlxsw_core *core;
33 const struct mlxsw_bus_info *bus_info;
34 u8 base_mac[ETH_ALEN];
39 struct net_device *dev;
40 struct mlxsw_m *mlxsw_m;
45 static int mlxsw_m_base_mac_get(struct mlxsw_m *mlxsw_m)
47 char spad_pl[MLXSW_REG_SPAD_LEN] = {0};
50 err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(spad), spad_pl);
53 mlxsw_reg_spad_base_mac_memcpy_from(spad_pl, mlxsw_m->base_mac);
57 static int mlxsw_m_port_open(struct net_device *dev)
59 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev);
60 struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;
62 return mlxsw_env_module_port_up(mlxsw_m->core, 0,
63 mlxsw_m_port->module);
66 static int mlxsw_m_port_stop(struct net_device *dev)
68 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev);
69 struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;
71 mlxsw_env_module_port_down(mlxsw_m->core, 0, mlxsw_m_port->module);
75 static struct devlink_port *
76 mlxsw_m_port_get_devlink_port(struct net_device *dev)
78 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev);
79 struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;
81 return mlxsw_core_port_devlink_port_get(mlxsw_m->core,
82 mlxsw_m_port->local_port);
85 static const struct net_device_ops mlxsw_m_port_netdev_ops = {
86 .ndo_open = mlxsw_m_port_open,
87 .ndo_stop = mlxsw_m_port_stop,
88 .ndo_get_devlink_port = mlxsw_m_port_get_devlink_port,
91 static void mlxsw_m_module_get_drvinfo(struct net_device *dev,
92 struct ethtool_drvinfo *drvinfo)
94 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev);
95 struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;
97 strlcpy(drvinfo->driver, mlxsw_m->bus_info->device_kind,
98 sizeof(drvinfo->driver));
99 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
101 mlxsw_m->bus_info->fw_rev.major,
102 mlxsw_m->bus_info->fw_rev.minor,
103 mlxsw_m->bus_info->fw_rev.subminor);
104 strlcpy(drvinfo->bus_info, mlxsw_m->bus_info->device_name,
105 sizeof(drvinfo->bus_info));
108 static int mlxsw_m_get_module_info(struct net_device *netdev,
109 struct ethtool_modinfo *modinfo)
111 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
112 struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
114 return mlxsw_env_get_module_info(netdev, core, 0, mlxsw_m_port->module,
119 mlxsw_m_get_module_eeprom(struct net_device *netdev, struct ethtool_eeprom *ee,
122 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
123 struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
125 return mlxsw_env_get_module_eeprom(netdev, core, 0,
126 mlxsw_m_port->module, ee, data);
130 mlxsw_m_get_module_eeprom_by_page(struct net_device *netdev,
131 const struct ethtool_module_eeprom *page,
132 struct netlink_ext_ack *extack)
134 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
135 struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
137 return mlxsw_env_get_module_eeprom_by_page(core, 0,
138 mlxsw_m_port->module,
142 static int mlxsw_m_reset(struct net_device *netdev, u32 *flags)
144 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
145 struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
147 return mlxsw_env_reset_module(netdev, core, 0, mlxsw_m_port->module,
152 mlxsw_m_get_module_power_mode(struct net_device *netdev,
153 struct ethtool_module_power_mode_params *params,
154 struct netlink_ext_ack *extack)
156 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
157 struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
159 return mlxsw_env_get_module_power_mode(core, 0, mlxsw_m_port->module,
164 mlxsw_m_set_module_power_mode(struct net_device *netdev,
165 const struct ethtool_module_power_mode_params *params,
166 struct netlink_ext_ack *extack)
168 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
169 struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
171 return mlxsw_env_set_module_power_mode(core, 0, mlxsw_m_port->module,
172 params->policy, extack);
175 static const struct ethtool_ops mlxsw_m_port_ethtool_ops = {
176 .get_drvinfo = mlxsw_m_module_get_drvinfo,
177 .get_module_info = mlxsw_m_get_module_info,
178 .get_module_eeprom = mlxsw_m_get_module_eeprom,
179 .get_module_eeprom_by_page = mlxsw_m_get_module_eeprom_by_page,
180 .reset = mlxsw_m_reset,
181 .get_module_power_mode = mlxsw_m_get_module_power_mode,
182 .set_module_power_mode = mlxsw_m_set_module_power_mode,
186 mlxsw_m_port_module_info_get(struct mlxsw_m *mlxsw_m, u16 local_port,
187 u8 *p_module, u8 *p_width)
189 char pmlp_pl[MLXSW_REG_PMLP_LEN];
192 mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
193 err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(pmlp), pmlp_pl);
196 *p_module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0);
197 *p_width = mlxsw_reg_pmlp_width_get(pmlp_pl);
203 mlxsw_m_port_dev_addr_get(struct mlxsw_m_port *mlxsw_m_port)
205 struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;
206 char ppad_pl[MLXSW_REG_PPAD_LEN];
210 mlxsw_reg_ppad_pack(ppad_pl, false, 0);
211 err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(ppad), ppad_pl);
214 mlxsw_reg_ppad_mac_memcpy_from(ppad_pl, addr);
215 eth_hw_addr_gen(mlxsw_m_port->dev, addr, mlxsw_m_port->module + 1);
220 mlxsw_m_port_create(struct mlxsw_m *mlxsw_m, u16 local_port, u8 module)
222 struct mlxsw_m_port *mlxsw_m_port;
223 struct net_device *dev;
226 err = mlxsw_core_port_init(mlxsw_m->core, local_port, 0,
227 module + 1, false, 0, false,
228 0, mlxsw_m->base_mac,
229 sizeof(mlxsw_m->base_mac));
231 dev_err(mlxsw_m->bus_info->dev, "Port %d: Failed to init core port\n",
236 dev = alloc_etherdev(sizeof(struct mlxsw_m_port));
239 goto err_alloc_etherdev;
242 SET_NETDEV_DEV(dev, mlxsw_m->bus_info->dev);
243 dev_net_set(dev, mlxsw_core_net(mlxsw_m->core));
244 mlxsw_m_port = netdev_priv(dev);
245 mlxsw_m_port->dev = dev;
246 mlxsw_m_port->mlxsw_m = mlxsw_m;
247 mlxsw_m_port->local_port = local_port;
248 mlxsw_m_port->module = module;
250 dev->netdev_ops = &mlxsw_m_port_netdev_ops;
251 dev->ethtool_ops = &mlxsw_m_port_ethtool_ops;
253 err = mlxsw_m_port_dev_addr_get(mlxsw_m_port);
255 dev_err(mlxsw_m->bus_info->dev, "Port %d: Unable to get port mac address\n",
256 mlxsw_m_port->local_port);
257 goto err_dev_addr_get;
260 netif_carrier_off(dev);
261 mlxsw_m->ports[local_port] = mlxsw_m_port;
262 err = register_netdev(dev);
264 dev_err(mlxsw_m->bus_info->dev, "Port %d: Failed to register netdev\n",
265 mlxsw_m_port->local_port);
266 goto err_register_netdev;
269 mlxsw_core_port_eth_set(mlxsw_m->core, mlxsw_m_port->local_port,
275 mlxsw_m->ports[local_port] = NULL;
279 mlxsw_core_port_fini(mlxsw_m->core, local_port);
283 static void mlxsw_m_port_remove(struct mlxsw_m *mlxsw_m, u16 local_port)
285 struct mlxsw_m_port *mlxsw_m_port = mlxsw_m->ports[local_port];
287 mlxsw_core_port_clear(mlxsw_m->core, local_port, mlxsw_m);
288 unregister_netdev(mlxsw_m_port->dev); /* This calls ndo_stop */
289 mlxsw_m->ports[local_port] = NULL;
290 free_netdev(mlxsw_m_port->dev);
291 mlxsw_core_port_fini(mlxsw_m->core, local_port);
294 static int mlxsw_m_port_module_map(struct mlxsw_m *mlxsw_m, u16 local_port,
297 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_m->core);
301 /* Fill out to local port mapping array */
302 err = mlxsw_m_port_module_info_get(mlxsw_m, local_port, &module,
309 /* Skip, if port belongs to the cluster */
310 if (module == *last_module)
312 *last_module = module;
314 if (WARN_ON_ONCE(module >= max_ports))
316 mlxsw_env_module_port_map(mlxsw_m->core, 0, module);
317 mlxsw_m->module_to_port[module] = ++mlxsw_m->max_ports;
322 static void mlxsw_m_port_module_unmap(struct mlxsw_m *mlxsw_m, u8 module)
324 mlxsw_m->module_to_port[module] = -1;
325 mlxsw_env_module_port_unmap(mlxsw_m->core, 0, module);
328 static int mlxsw_m_ports_create(struct mlxsw_m *mlxsw_m)
330 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_m->core);
331 struct devlink *devlink = priv_to_devlink(mlxsw_m->core);
332 u8 last_module = max_ports;
336 mlxsw_m->ports = kcalloc(max_ports, sizeof(*mlxsw_m->ports),
341 mlxsw_m->module_to_port = kmalloc_array(max_ports, sizeof(int),
343 if (!mlxsw_m->module_to_port) {
345 goto err_module_to_port_alloc;
348 /* Invalidate the entries of module to local port mapping array */
349 for (i = 0; i < max_ports; i++)
350 mlxsw_m->module_to_port[i] = -1;
352 /* Fill out module to local port mapping array */
353 for (i = 1; i < max_ports; i++) {
354 err = mlxsw_m_port_module_map(mlxsw_m, i, &last_module);
356 goto err_module_to_port_map;
359 /* Create port objects for each valid entry */
361 for (i = 0; i < mlxsw_m->max_ports; i++) {
362 if (mlxsw_m->module_to_port[i] > 0) {
363 err = mlxsw_m_port_create(mlxsw_m,
364 mlxsw_m->module_to_port[i],
367 goto err_module_to_port_create;
370 devl_unlock(devlink);
374 err_module_to_port_create:
375 for (i--; i >= 0; i--) {
376 if (mlxsw_m->module_to_port[i] > 0)
377 mlxsw_m_port_remove(mlxsw_m,
378 mlxsw_m->module_to_port[i]);
380 devl_unlock(devlink);
382 err_module_to_port_map:
383 for (i--; i > 0; i--)
384 mlxsw_m_port_module_unmap(mlxsw_m, i);
385 kfree(mlxsw_m->module_to_port);
386 err_module_to_port_alloc:
387 kfree(mlxsw_m->ports);
391 static void mlxsw_m_ports_remove(struct mlxsw_m *mlxsw_m)
393 struct devlink *devlink = priv_to_devlink(mlxsw_m->core);
397 for (i = 0; i < mlxsw_m->max_ports; i++) {
398 if (mlxsw_m->module_to_port[i] > 0) {
399 mlxsw_m_port_remove(mlxsw_m,
400 mlxsw_m->module_to_port[i]);
401 mlxsw_m_port_module_unmap(mlxsw_m, i);
404 devl_unlock(devlink);
406 kfree(mlxsw_m->module_to_port);
407 kfree(mlxsw_m->ports);
410 static int mlxsw_m_fw_rev_validate(struct mlxsw_m *mlxsw_m)
412 const struct mlxsw_fw_rev *rev = &mlxsw_m->bus_info->fw_rev;
414 /* Validate driver and FW are compatible.
415 * Do not check major version, since it defines chip type, while
416 * driver is supposed to support any type.
418 if (mlxsw_core_fw_rev_minor_subminor_validate(rev, &mlxsw_m_fw_rev))
421 dev_err(mlxsw_m->bus_info->dev, "The firmware version %d.%d.%d is incompatible with the driver (required >= %d.%d.%d)\n",
422 rev->major, rev->minor, rev->subminor, rev->major,
423 mlxsw_m_fw_rev.minor, mlxsw_m_fw_rev.subminor);
428 static int mlxsw_m_init(struct mlxsw_core *mlxsw_core,
429 const struct mlxsw_bus_info *mlxsw_bus_info,
430 struct netlink_ext_ack *extack)
432 struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core);
435 mlxsw_m->core = mlxsw_core;
436 mlxsw_m->bus_info = mlxsw_bus_info;
438 err = mlxsw_m_fw_rev_validate(mlxsw_m);
442 err = mlxsw_m_base_mac_get(mlxsw_m);
444 dev_err(mlxsw_m->bus_info->dev, "Failed to get base mac\n");
448 err = mlxsw_m_ports_create(mlxsw_m);
450 dev_err(mlxsw_m->bus_info->dev, "Failed to create ports\n");
457 static void mlxsw_m_fini(struct mlxsw_core *mlxsw_core)
459 struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core);
461 mlxsw_m_ports_remove(mlxsw_m);
464 static const struct mlxsw_config_profile mlxsw_m_config_profile;
466 static struct mlxsw_driver mlxsw_m_driver = {
467 .kind = mlxsw_m_driver_name,
468 .priv_size = sizeof(struct mlxsw_m),
469 .init = mlxsw_m_init,
470 .fini = mlxsw_m_fini,
471 .profile = &mlxsw_m_config_profile,
474 static const struct i2c_device_id mlxsw_m_i2c_id[] = {
475 { "mlxsw_minimal", 0},
479 static struct i2c_driver mlxsw_m_i2c_driver = {
480 .driver.name = "mlxsw_minimal",
481 .class = I2C_CLASS_HWMON,
482 .id_table = mlxsw_m_i2c_id,
485 static int __init mlxsw_m_module_init(void)
489 err = mlxsw_core_driver_register(&mlxsw_m_driver);
493 err = mlxsw_i2c_driver_register(&mlxsw_m_i2c_driver);
495 goto err_i2c_driver_register;
499 err_i2c_driver_register:
500 mlxsw_core_driver_unregister(&mlxsw_m_driver);
505 static void __exit mlxsw_m_module_exit(void)
507 mlxsw_i2c_driver_unregister(&mlxsw_m_i2c_driver);
508 mlxsw_core_driver_unregister(&mlxsw_m_driver);
511 module_init(mlxsw_m_module_init);
512 module_exit(mlxsw_m_module_exit);
514 MODULE_LICENSE("Dual BSD/GPL");
515 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
516 MODULE_DESCRIPTION("Mellanox minimal driver");
517 MODULE_DEVICE_TABLE(i2c, mlxsw_m_i2c_id);