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_dummy_open_stop(struct net_device *dev)
62 static struct devlink_port *
63 mlxsw_m_port_get_devlink_port(struct net_device *dev)
65 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev);
66 struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;
68 return mlxsw_core_port_devlink_port_get(mlxsw_m->core,
69 mlxsw_m_port->local_port);
72 static const struct net_device_ops mlxsw_m_port_netdev_ops = {
73 .ndo_open = mlxsw_m_port_dummy_open_stop,
74 .ndo_stop = mlxsw_m_port_dummy_open_stop,
75 .ndo_get_devlink_port = mlxsw_m_port_get_devlink_port,
78 static void mlxsw_m_module_get_drvinfo(struct net_device *dev,
79 struct ethtool_drvinfo *drvinfo)
81 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev);
82 struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;
84 strlcpy(drvinfo->driver, mlxsw_m->bus_info->device_kind,
85 sizeof(drvinfo->driver));
86 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
88 mlxsw_m->bus_info->fw_rev.major,
89 mlxsw_m->bus_info->fw_rev.minor,
90 mlxsw_m->bus_info->fw_rev.subminor);
91 strlcpy(drvinfo->bus_info, mlxsw_m->bus_info->device_name,
92 sizeof(drvinfo->bus_info));
95 static int mlxsw_m_get_module_info(struct net_device *netdev,
96 struct ethtool_modinfo *modinfo)
98 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
99 struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
101 return mlxsw_env_get_module_info(core, mlxsw_m_port->module, modinfo);
105 mlxsw_m_get_module_eeprom(struct net_device *netdev, struct ethtool_eeprom *ee,
108 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
109 struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
111 return mlxsw_env_get_module_eeprom(netdev, core, mlxsw_m_port->module,
115 static const struct ethtool_ops mlxsw_m_port_ethtool_ops = {
116 .get_drvinfo = mlxsw_m_module_get_drvinfo,
117 .get_module_info = mlxsw_m_get_module_info,
118 .get_module_eeprom = mlxsw_m_get_module_eeprom,
122 mlxsw_m_port_module_info_get(struct mlxsw_m *mlxsw_m, u8 local_port,
123 u8 *p_module, u8 *p_width)
125 char pmlp_pl[MLXSW_REG_PMLP_LEN];
128 mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
129 err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(pmlp), pmlp_pl);
132 *p_module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0);
133 *p_width = mlxsw_reg_pmlp_width_get(pmlp_pl);
139 mlxsw_m_port_dev_addr_get(struct mlxsw_m_port *mlxsw_m_port)
141 struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;
142 struct net_device *dev = mlxsw_m_port->dev;
143 char ppad_pl[MLXSW_REG_PPAD_LEN];
146 mlxsw_reg_ppad_pack(ppad_pl, false, 0);
147 err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(ppad), ppad_pl);
150 mlxsw_reg_ppad_mac_memcpy_from(ppad_pl, dev->dev_addr);
151 /* The last byte value in base mac address is guaranteed
152 * to be such it does not overflow when adding local_port
155 dev->dev_addr[ETH_ALEN - 1] += mlxsw_m_port->module + 1;
160 mlxsw_m_port_create(struct mlxsw_m *mlxsw_m, u8 local_port, u8 module)
162 struct mlxsw_m_port *mlxsw_m_port;
163 struct net_device *dev;
166 err = mlxsw_core_port_init(mlxsw_m->core, local_port,
167 module + 1, false, 0, false,
168 0, mlxsw_m->base_mac,
169 sizeof(mlxsw_m->base_mac));
171 dev_err(mlxsw_m->bus_info->dev, "Port %d: Failed to init core port\n",
176 dev = alloc_etherdev(sizeof(struct mlxsw_m_port));
179 goto err_alloc_etherdev;
182 SET_NETDEV_DEV(dev, mlxsw_m->bus_info->dev);
183 dev_net_set(dev, mlxsw_core_net(mlxsw_m->core));
184 mlxsw_m_port = netdev_priv(dev);
185 mlxsw_m_port->dev = dev;
186 mlxsw_m_port->mlxsw_m = mlxsw_m;
187 mlxsw_m_port->local_port = local_port;
188 mlxsw_m_port->module = module;
190 dev->netdev_ops = &mlxsw_m_port_netdev_ops;
191 dev->ethtool_ops = &mlxsw_m_port_ethtool_ops;
193 err = mlxsw_m_port_dev_addr_get(mlxsw_m_port);
195 dev_err(mlxsw_m->bus_info->dev, "Port %d: Unable to get port mac address\n",
196 mlxsw_m_port->local_port);
197 goto err_dev_addr_get;
200 netif_carrier_off(dev);
201 mlxsw_m->ports[local_port] = mlxsw_m_port;
202 err = register_netdev(dev);
204 dev_err(mlxsw_m->bus_info->dev, "Port %d: Failed to register netdev\n",
205 mlxsw_m_port->local_port);
206 goto err_register_netdev;
209 mlxsw_core_port_eth_set(mlxsw_m->core, mlxsw_m_port->local_port,
215 mlxsw_m->ports[local_port] = NULL;
219 mlxsw_core_port_fini(mlxsw_m->core, local_port);
223 static void mlxsw_m_port_remove(struct mlxsw_m *mlxsw_m, u8 local_port)
225 struct mlxsw_m_port *mlxsw_m_port = mlxsw_m->ports[local_port];
227 mlxsw_core_port_clear(mlxsw_m->core, local_port, mlxsw_m);
228 unregister_netdev(mlxsw_m_port->dev); /* This calls ndo_stop */
229 mlxsw_m->ports[local_port] = NULL;
230 free_netdev(mlxsw_m_port->dev);
231 mlxsw_core_port_fini(mlxsw_m->core, local_port);
234 static int mlxsw_m_port_module_map(struct mlxsw_m *mlxsw_m, u8 local_port,
240 /* Fill out to local port mapping array */
241 err = mlxsw_m_port_module_info_get(mlxsw_m, local_port, &module,
248 /* Skip, if port belongs to the cluster */
249 if (module == *last_module)
251 *last_module = module;
252 mlxsw_m->module_to_port[module] = ++mlxsw_m->max_ports;
257 static void mlxsw_m_port_module_unmap(struct mlxsw_m *mlxsw_m, u8 module)
259 mlxsw_m->module_to_port[module] = -1;
262 static int mlxsw_m_ports_create(struct mlxsw_m *mlxsw_m)
264 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_m->core);
265 u8 last_module = max_ports;
269 mlxsw_m->ports = kcalloc(max_ports, sizeof(*mlxsw_m->ports),
274 mlxsw_m->module_to_port = kmalloc_array(max_ports, sizeof(int),
276 if (!mlxsw_m->module_to_port) {
278 goto err_module_to_port_alloc;
281 /* Invalidate the entries of module to local port mapping array */
282 for (i = 0; i < max_ports; i++)
283 mlxsw_m->module_to_port[i] = -1;
285 /* Fill out module to local port mapping array */
286 for (i = 1; i < max_ports; i++) {
287 err = mlxsw_m_port_module_map(mlxsw_m, i, &last_module);
289 goto err_module_to_port_map;
292 /* Create port objects for each valid entry */
293 for (i = 0; i < mlxsw_m->max_ports; i++) {
294 if (mlxsw_m->module_to_port[i] > 0 &&
295 !mlxsw_core_port_is_xm(mlxsw_m->core, i)) {
296 err = mlxsw_m_port_create(mlxsw_m,
297 mlxsw_m->module_to_port[i],
300 goto err_module_to_port_create;
306 err_module_to_port_create:
307 for (i--; i >= 0; i--) {
308 if (mlxsw_m->module_to_port[i] > 0)
309 mlxsw_m_port_remove(mlxsw_m,
310 mlxsw_m->module_to_port[i]);
313 err_module_to_port_map:
314 for (i--; i > 0; i--)
315 mlxsw_m_port_module_unmap(mlxsw_m, i);
316 kfree(mlxsw_m->module_to_port);
317 err_module_to_port_alloc:
318 kfree(mlxsw_m->ports);
322 static void mlxsw_m_ports_remove(struct mlxsw_m *mlxsw_m)
326 for (i = 0; i < mlxsw_m->max_ports; i++) {
327 if (mlxsw_m->module_to_port[i] > 0) {
328 mlxsw_m_port_remove(mlxsw_m,
329 mlxsw_m->module_to_port[i]);
330 mlxsw_m_port_module_unmap(mlxsw_m, i);
334 kfree(mlxsw_m->module_to_port);
335 kfree(mlxsw_m->ports);
338 static int mlxsw_m_fw_rev_validate(struct mlxsw_m *mlxsw_m)
340 const struct mlxsw_fw_rev *rev = &mlxsw_m->bus_info->fw_rev;
342 /* Validate driver and FW are compatible.
343 * Do not check major version, since it defines chip type, while
344 * driver is supposed to support any type.
346 if (mlxsw_core_fw_rev_minor_subminor_validate(rev, &mlxsw_m_fw_rev))
349 dev_err(mlxsw_m->bus_info->dev, "The firmware version %d.%d.%d is incompatible with the driver (required >= %d.%d.%d)\n",
350 rev->major, rev->minor, rev->subminor, rev->major,
351 mlxsw_m_fw_rev.minor, mlxsw_m_fw_rev.subminor);
356 static int mlxsw_m_init(struct mlxsw_core *mlxsw_core,
357 const struct mlxsw_bus_info *mlxsw_bus_info,
358 struct netlink_ext_ack *extack)
360 struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core);
363 mlxsw_m->core = mlxsw_core;
364 mlxsw_m->bus_info = mlxsw_bus_info;
366 err = mlxsw_m_fw_rev_validate(mlxsw_m);
370 err = mlxsw_m_base_mac_get(mlxsw_m);
372 dev_err(mlxsw_m->bus_info->dev, "Failed to get base mac\n");
376 err = mlxsw_m_ports_create(mlxsw_m);
378 dev_err(mlxsw_m->bus_info->dev, "Failed to create ports\n");
385 static void mlxsw_m_fini(struct mlxsw_core *mlxsw_core)
387 struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core);
389 mlxsw_m_ports_remove(mlxsw_m);
392 static const struct mlxsw_config_profile mlxsw_m_config_profile;
394 static struct mlxsw_driver mlxsw_m_driver = {
395 .kind = mlxsw_m_driver_name,
396 .priv_size = sizeof(struct mlxsw_m),
397 .init = mlxsw_m_init,
398 .fini = mlxsw_m_fini,
399 .profile = &mlxsw_m_config_profile,
400 .res_query_enabled = true,
403 static const struct i2c_device_id mlxsw_m_i2c_id[] = {
404 { "mlxsw_minimal", 0},
408 static struct i2c_driver mlxsw_m_i2c_driver = {
409 .driver.name = "mlxsw_minimal",
410 .class = I2C_CLASS_HWMON,
411 .id_table = mlxsw_m_i2c_id,
414 static int __init mlxsw_m_module_init(void)
418 err = mlxsw_core_driver_register(&mlxsw_m_driver);
422 err = mlxsw_i2c_driver_register(&mlxsw_m_i2c_driver);
424 goto err_i2c_driver_register;
428 err_i2c_driver_register:
429 mlxsw_core_driver_unregister(&mlxsw_m_driver);
434 static void __exit mlxsw_m_module_exit(void)
436 mlxsw_i2c_driver_unregister(&mlxsw_m_i2c_driver);
437 mlxsw_core_driver_unregister(&mlxsw_m_driver);
440 module_init(mlxsw_m_module_init);
441 module_exit(mlxsw_m_module_exit);
443 MODULE_LICENSE("Dual BSD/GPL");
444 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
445 MODULE_DESCRIPTION("Mellanox minimal driver");
446 MODULE_DEVICE_TABLE(i2c, mlxsw_m_i2c_id);