1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 2 /* Copyright (c) 2016-2019 Mellanox Technologies. All rights reserved */ 3 4 #include <linux/netdevice.h> 5 #include <linux/etherdevice.h> 6 #include <linux/ethtool.h> 7 #include <linux/i2c.h> 8 #include <linux/kernel.h> 9 #include <linux/module.h> 10 #include <linux/mod_devicetable.h> 11 #include <linux/types.h> 12 13 #include "core.h" 14 #include "core_env.h" 15 #include "i2c.h" 16 17 static const char mlxsw_m_driver_name[] = "mlxsw_minimal"; 18 19 #define MLXSW_M_FWREV_MINOR 2000 20 #define MLXSW_M_FWREV_SUBMINOR 1886 21 22 static const struct mlxsw_fw_rev mlxsw_m_fw_rev = { 23 .minor = MLXSW_M_FWREV_MINOR, 24 .subminor = MLXSW_M_FWREV_SUBMINOR, 25 }; 26 27 struct mlxsw_m_port; 28 29 struct mlxsw_m { 30 struct mlxsw_m_port **ports; 31 int *module_to_port; 32 struct mlxsw_core *core; 33 const struct mlxsw_bus_info *bus_info; 34 u8 base_mac[ETH_ALEN]; 35 u8 max_ports; 36 }; 37 38 struct mlxsw_m_port { 39 struct net_device *dev; 40 struct mlxsw_m *mlxsw_m; 41 u8 local_port; 42 u8 module; 43 }; 44 45 static int mlxsw_m_base_mac_get(struct mlxsw_m *mlxsw_m) 46 { 47 char spad_pl[MLXSW_REG_SPAD_LEN] = {0}; 48 int err; 49 50 err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(spad), spad_pl); 51 if (err) 52 return err; 53 mlxsw_reg_spad_base_mac_memcpy_from(spad_pl, mlxsw_m->base_mac); 54 return 0; 55 } 56 57 static int mlxsw_m_port_dummy_open_stop(struct net_device *dev) 58 { 59 return 0; 60 } 61 62 static struct devlink_port * 63 mlxsw_m_port_get_devlink_port(struct net_device *dev) 64 { 65 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev); 66 struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m; 67 68 return mlxsw_core_port_devlink_port_get(mlxsw_m->core, 69 mlxsw_m_port->local_port); 70 } 71 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, 76 }; 77 78 static void mlxsw_m_module_get_drvinfo(struct net_device *dev, 79 struct ethtool_drvinfo *drvinfo) 80 { 81 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev); 82 struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m; 83 84 strlcpy(drvinfo->driver, mlxsw_m->bus_info->device_kind, 85 sizeof(drvinfo->driver)); 86 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), 87 "%d.%d.%d", 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)); 93 } 94 95 static int mlxsw_m_get_module_info(struct net_device *netdev, 96 struct ethtool_modinfo *modinfo) 97 { 98 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev); 99 struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core; 100 101 return mlxsw_env_get_module_info(core, mlxsw_m_port->module, modinfo); 102 } 103 104 static int 105 mlxsw_m_get_module_eeprom(struct net_device *netdev, struct ethtool_eeprom *ee, 106 u8 *data) 107 { 108 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev); 109 struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core; 110 111 return mlxsw_env_get_module_eeprom(netdev, core, mlxsw_m_port->module, 112 ee, data); 113 } 114 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, 119 }; 120 121 static int 122 mlxsw_m_port_module_info_get(struct mlxsw_m *mlxsw_m, u8 local_port, 123 u8 *p_module, u8 *p_width) 124 { 125 char pmlp_pl[MLXSW_REG_PMLP_LEN]; 126 int err; 127 128 mlxsw_reg_pmlp_pack(pmlp_pl, local_port); 129 err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(pmlp), pmlp_pl); 130 if (err) 131 return err; 132 *p_module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0); 133 *p_width = mlxsw_reg_pmlp_width_get(pmlp_pl); 134 135 return 0; 136 } 137 138 static int 139 mlxsw_m_port_dev_addr_get(struct mlxsw_m_port *mlxsw_m_port) 140 { 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]; 144 int err; 145 146 mlxsw_reg_ppad_pack(ppad_pl, false, 0); 147 err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(ppad), ppad_pl); 148 if (err) 149 return err; 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 153 * value. 154 */ 155 dev->dev_addr[ETH_ALEN - 1] += mlxsw_m_port->module + 1; 156 return 0; 157 } 158 159 static int 160 mlxsw_m_port_create(struct mlxsw_m *mlxsw_m, u8 local_port, u8 module) 161 { 162 struct mlxsw_m_port *mlxsw_m_port; 163 struct net_device *dev; 164 int err; 165 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)); 170 if (err) { 171 dev_err(mlxsw_m->bus_info->dev, "Port %d: Failed to init core port\n", 172 local_port); 173 return err; 174 } 175 176 dev = alloc_etherdev(sizeof(struct mlxsw_m_port)); 177 if (!dev) { 178 err = -ENOMEM; 179 goto err_alloc_etherdev; 180 } 181 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; 189 190 dev->netdev_ops = &mlxsw_m_port_netdev_ops; 191 dev->ethtool_ops = &mlxsw_m_port_ethtool_ops; 192 193 err = mlxsw_m_port_dev_addr_get(mlxsw_m_port); 194 if (err) { 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; 198 } 199 200 netif_carrier_off(dev); 201 mlxsw_m->ports[local_port] = mlxsw_m_port; 202 err = register_netdev(dev); 203 if (err) { 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; 207 } 208 209 mlxsw_core_port_eth_set(mlxsw_m->core, mlxsw_m_port->local_port, 210 mlxsw_m_port, dev); 211 212 return 0; 213 214 err_register_netdev: 215 mlxsw_m->ports[local_port] = NULL; 216 err_dev_addr_get: 217 free_netdev(dev); 218 err_alloc_etherdev: 219 mlxsw_core_port_fini(mlxsw_m->core, local_port); 220 return err; 221 } 222 223 static void mlxsw_m_port_remove(struct mlxsw_m *mlxsw_m, u8 local_port) 224 { 225 struct mlxsw_m_port *mlxsw_m_port = mlxsw_m->ports[local_port]; 226 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); 232 } 233 234 static int mlxsw_m_port_module_map(struct mlxsw_m *mlxsw_m, u8 local_port, 235 u8 *last_module) 236 { 237 u8 module, width; 238 int err; 239 240 /* Fill out to local port mapping array */ 241 err = mlxsw_m_port_module_info_get(mlxsw_m, local_port, &module, 242 &width); 243 if (err) 244 return err; 245 246 if (!width) 247 return 0; 248 /* Skip, if port belongs to the cluster */ 249 if (module == *last_module) 250 return 0; 251 *last_module = module; 252 mlxsw_m->module_to_port[module] = ++mlxsw_m->max_ports; 253 254 return 0; 255 } 256 257 static void mlxsw_m_port_module_unmap(struct mlxsw_m *mlxsw_m, u8 module) 258 { 259 mlxsw_m->module_to_port[module] = -1; 260 } 261 262 static int mlxsw_m_ports_create(struct mlxsw_m *mlxsw_m) 263 { 264 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_m->core); 265 u8 last_module = max_ports; 266 int i; 267 int err; 268 269 mlxsw_m->ports = kcalloc(max_ports, sizeof(*mlxsw_m->ports), 270 GFP_KERNEL); 271 if (!mlxsw_m->ports) 272 return -ENOMEM; 273 274 mlxsw_m->module_to_port = kmalloc_array(max_ports, sizeof(int), 275 GFP_KERNEL); 276 if (!mlxsw_m->module_to_port) { 277 err = -ENOMEM; 278 goto err_module_to_port_alloc; 279 } 280 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; 284 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); 288 if (err) 289 goto err_module_to_port_map; 290 } 291 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 err = mlxsw_m_port_create(mlxsw_m, 296 mlxsw_m->module_to_port[i], 297 i); 298 if (err) 299 goto err_module_to_port_create; 300 } 301 } 302 303 return 0; 304 305 err_module_to_port_create: 306 for (i--; i >= 0; i--) { 307 if (mlxsw_m->module_to_port[i] > 0) 308 mlxsw_m_port_remove(mlxsw_m, 309 mlxsw_m->module_to_port[i]); 310 } 311 i = max_ports; 312 err_module_to_port_map: 313 for (i--; i > 0; i--) 314 mlxsw_m_port_module_unmap(mlxsw_m, i); 315 kfree(mlxsw_m->module_to_port); 316 err_module_to_port_alloc: 317 kfree(mlxsw_m->ports); 318 return err; 319 } 320 321 static void mlxsw_m_ports_remove(struct mlxsw_m *mlxsw_m) 322 { 323 int i; 324 325 for (i = 0; i < mlxsw_m->max_ports; i++) { 326 if (mlxsw_m->module_to_port[i] > 0) { 327 mlxsw_m_port_remove(mlxsw_m, 328 mlxsw_m->module_to_port[i]); 329 mlxsw_m_port_module_unmap(mlxsw_m, i); 330 } 331 } 332 333 kfree(mlxsw_m->module_to_port); 334 kfree(mlxsw_m->ports); 335 } 336 337 static int mlxsw_m_fw_rev_validate(struct mlxsw_m *mlxsw_m) 338 { 339 const struct mlxsw_fw_rev *rev = &mlxsw_m->bus_info->fw_rev; 340 341 /* Validate driver and FW are compatible. 342 * Do not check major version, since it defines chip type, while 343 * driver is supposed to support any type. 344 */ 345 if (mlxsw_core_fw_rev_minor_subminor_validate(rev, &mlxsw_m_fw_rev)) 346 return 0; 347 348 dev_err(mlxsw_m->bus_info->dev, "The firmware version %d.%d.%d is incompatible with the driver (required >= %d.%d.%d)\n", 349 rev->major, rev->minor, rev->subminor, rev->major, 350 mlxsw_m_fw_rev.minor, mlxsw_m_fw_rev.subminor); 351 352 return -EINVAL; 353 } 354 355 static int mlxsw_m_init(struct mlxsw_core *mlxsw_core, 356 const struct mlxsw_bus_info *mlxsw_bus_info, 357 struct netlink_ext_ack *extack) 358 { 359 struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core); 360 int err; 361 362 mlxsw_m->core = mlxsw_core; 363 mlxsw_m->bus_info = mlxsw_bus_info; 364 365 err = mlxsw_m_fw_rev_validate(mlxsw_m); 366 if (err) 367 return err; 368 369 err = mlxsw_m_base_mac_get(mlxsw_m); 370 if (err) { 371 dev_err(mlxsw_m->bus_info->dev, "Failed to get base mac\n"); 372 return err; 373 } 374 375 err = mlxsw_m_ports_create(mlxsw_m); 376 if (err) { 377 dev_err(mlxsw_m->bus_info->dev, "Failed to create ports\n"); 378 return err; 379 } 380 381 return 0; 382 } 383 384 static void mlxsw_m_fini(struct mlxsw_core *mlxsw_core) 385 { 386 struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core); 387 388 mlxsw_m_ports_remove(mlxsw_m); 389 } 390 391 static const struct mlxsw_config_profile mlxsw_m_config_profile; 392 393 static struct mlxsw_driver mlxsw_m_driver = { 394 .kind = mlxsw_m_driver_name, 395 .priv_size = sizeof(struct mlxsw_m), 396 .init = mlxsw_m_init, 397 .fini = mlxsw_m_fini, 398 .profile = &mlxsw_m_config_profile, 399 .res_query_enabled = true, 400 }; 401 402 static const struct i2c_device_id mlxsw_m_i2c_id[] = { 403 { "mlxsw_minimal", 0}, 404 { }, 405 }; 406 407 static struct i2c_driver mlxsw_m_i2c_driver = { 408 .driver.name = "mlxsw_minimal", 409 .class = I2C_CLASS_HWMON, 410 .id_table = mlxsw_m_i2c_id, 411 }; 412 413 static int __init mlxsw_m_module_init(void) 414 { 415 int err; 416 417 err = mlxsw_core_driver_register(&mlxsw_m_driver); 418 if (err) 419 return err; 420 421 err = mlxsw_i2c_driver_register(&mlxsw_m_i2c_driver); 422 if (err) 423 goto err_i2c_driver_register; 424 425 return 0; 426 427 err_i2c_driver_register: 428 mlxsw_core_driver_unregister(&mlxsw_m_driver); 429 430 return err; 431 } 432 433 static void __exit mlxsw_m_module_exit(void) 434 { 435 mlxsw_i2c_driver_unregister(&mlxsw_m_i2c_driver); 436 mlxsw_core_driver_unregister(&mlxsw_m_driver); 437 } 438 439 module_init(mlxsw_m_module_init); 440 module_exit(mlxsw_m_module_exit); 441 442 MODULE_LICENSE("Dual BSD/GPL"); 443 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>"); 444 MODULE_DESCRIPTION("Mellanox minimal driver"); 445 MODULE_DEVICE_TABLE(i2c, mlxsw_m_i2c_id); 446