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 u16 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_open(struct net_device *dev) 58 { 59 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev); 60 struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m; 61 62 return mlxsw_env_module_port_up(mlxsw_m->core, 0, 63 mlxsw_m_port->module); 64 } 65 66 static int mlxsw_m_port_stop(struct net_device *dev) 67 { 68 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev); 69 struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m; 70 71 mlxsw_env_module_port_down(mlxsw_m->core, 0, mlxsw_m_port->module); 72 return 0; 73 } 74 75 static struct devlink_port * 76 mlxsw_m_port_get_devlink_port(struct net_device *dev) 77 { 78 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev); 79 struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m; 80 81 return mlxsw_core_port_devlink_port_get(mlxsw_m->core, 82 mlxsw_m_port->local_port); 83 } 84 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, 89 }; 90 91 static void mlxsw_m_module_get_drvinfo(struct net_device *dev, 92 struct ethtool_drvinfo *drvinfo) 93 { 94 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev); 95 struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m; 96 97 strlcpy(drvinfo->driver, mlxsw_m->bus_info->device_kind, 98 sizeof(drvinfo->driver)); 99 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), 100 "%d.%d.%d", 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)); 106 } 107 108 static int mlxsw_m_get_module_info(struct net_device *netdev, 109 struct ethtool_modinfo *modinfo) 110 { 111 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev); 112 struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core; 113 114 return mlxsw_env_get_module_info(netdev, core, 0, mlxsw_m_port->module, 115 modinfo); 116 } 117 118 static int 119 mlxsw_m_get_module_eeprom(struct net_device *netdev, struct ethtool_eeprom *ee, 120 u8 *data) 121 { 122 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev); 123 struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core; 124 125 return mlxsw_env_get_module_eeprom(netdev, core, 0, 126 mlxsw_m_port->module, ee, data); 127 } 128 129 static int 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) 133 { 134 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev); 135 struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core; 136 137 return mlxsw_env_get_module_eeprom_by_page(core, 0, 138 mlxsw_m_port->module, 139 page, extack); 140 } 141 142 static int mlxsw_m_reset(struct net_device *netdev, u32 *flags) 143 { 144 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev); 145 struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core; 146 147 return mlxsw_env_reset_module(netdev, core, 0, mlxsw_m_port->module, 148 flags); 149 } 150 151 static int 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) 155 { 156 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev); 157 struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core; 158 159 return mlxsw_env_get_module_power_mode(core, 0, mlxsw_m_port->module, 160 params, extack); 161 } 162 163 static int 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) 167 { 168 struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev); 169 struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core; 170 171 return mlxsw_env_set_module_power_mode(core, 0, mlxsw_m_port->module, 172 params->policy, extack); 173 } 174 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, 183 }; 184 185 static int 186 mlxsw_m_port_module_info_get(struct mlxsw_m *mlxsw_m, u16 local_port, 187 u8 *p_module, u8 *p_width) 188 { 189 char pmlp_pl[MLXSW_REG_PMLP_LEN]; 190 int err; 191 192 mlxsw_reg_pmlp_pack(pmlp_pl, local_port); 193 err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(pmlp), pmlp_pl); 194 if (err) 195 return err; 196 *p_module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0); 197 *p_width = mlxsw_reg_pmlp_width_get(pmlp_pl); 198 199 return 0; 200 } 201 202 static int 203 mlxsw_m_port_dev_addr_get(struct mlxsw_m_port *mlxsw_m_port) 204 { 205 struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m; 206 char ppad_pl[MLXSW_REG_PPAD_LEN]; 207 u8 addr[ETH_ALEN]; 208 int err; 209 210 mlxsw_reg_ppad_pack(ppad_pl, false, 0); 211 err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(ppad), ppad_pl); 212 if (err) 213 return err; 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); 216 return 0; 217 } 218 219 static int 220 mlxsw_m_port_create(struct mlxsw_m *mlxsw_m, u16 local_port, u8 module) 221 { 222 struct mlxsw_m_port *mlxsw_m_port; 223 struct net_device *dev; 224 int err; 225 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)); 230 if (err) { 231 dev_err(mlxsw_m->bus_info->dev, "Port %d: Failed to init core port\n", 232 local_port); 233 return err; 234 } 235 236 dev = alloc_etherdev(sizeof(struct mlxsw_m_port)); 237 if (!dev) { 238 err = -ENOMEM; 239 goto err_alloc_etherdev; 240 } 241 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; 249 250 dev->netdev_ops = &mlxsw_m_port_netdev_ops; 251 dev->ethtool_ops = &mlxsw_m_port_ethtool_ops; 252 253 err = mlxsw_m_port_dev_addr_get(mlxsw_m_port); 254 if (err) { 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; 258 } 259 260 netif_carrier_off(dev); 261 mlxsw_m->ports[local_port] = mlxsw_m_port; 262 err = register_netdev(dev); 263 if (err) { 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; 267 } 268 269 mlxsw_core_port_eth_set(mlxsw_m->core, mlxsw_m_port->local_port, 270 mlxsw_m_port, dev); 271 272 return 0; 273 274 err_register_netdev: 275 mlxsw_m->ports[local_port] = NULL; 276 err_dev_addr_get: 277 free_netdev(dev); 278 err_alloc_etherdev: 279 mlxsw_core_port_fini(mlxsw_m->core, local_port); 280 return err; 281 } 282 283 static void mlxsw_m_port_remove(struct mlxsw_m *mlxsw_m, u16 local_port) 284 { 285 struct mlxsw_m_port *mlxsw_m_port = mlxsw_m->ports[local_port]; 286 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); 292 } 293 294 static int mlxsw_m_port_module_map(struct mlxsw_m *mlxsw_m, u16 local_port, 295 u8 *last_module) 296 { 297 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_m->core); 298 u8 module, width; 299 int err; 300 301 /* Fill out to local port mapping array */ 302 err = mlxsw_m_port_module_info_get(mlxsw_m, local_port, &module, 303 &width); 304 if (err) 305 return err; 306 307 if (!width) 308 return 0; 309 /* Skip, if port belongs to the cluster */ 310 if (module == *last_module) 311 return 0; 312 *last_module = module; 313 314 if (WARN_ON_ONCE(module >= max_ports)) 315 return -EINVAL; 316 mlxsw_env_module_port_map(mlxsw_m->core, 0, module); 317 mlxsw_m->module_to_port[module] = ++mlxsw_m->max_ports; 318 319 return 0; 320 } 321 322 static void mlxsw_m_port_module_unmap(struct mlxsw_m *mlxsw_m, u8 module) 323 { 324 mlxsw_m->module_to_port[module] = -1; 325 mlxsw_env_module_port_unmap(mlxsw_m->core, 0, module); 326 } 327 328 static int mlxsw_m_ports_create(struct mlxsw_m *mlxsw_m) 329 { 330 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_m->core); 331 u8 last_module = max_ports; 332 int i; 333 int err; 334 335 mlxsw_m->ports = kcalloc(max_ports, sizeof(*mlxsw_m->ports), 336 GFP_KERNEL); 337 if (!mlxsw_m->ports) 338 return -ENOMEM; 339 340 mlxsw_m->module_to_port = kmalloc_array(max_ports, sizeof(int), 341 GFP_KERNEL); 342 if (!mlxsw_m->module_to_port) { 343 err = -ENOMEM; 344 goto err_module_to_port_alloc; 345 } 346 347 /* Invalidate the entries of module to local port mapping array */ 348 for (i = 0; i < max_ports; i++) 349 mlxsw_m->module_to_port[i] = -1; 350 351 /* Fill out module to local port mapping array */ 352 for (i = 1; i < max_ports; i++) { 353 err = mlxsw_m_port_module_map(mlxsw_m, i, &last_module); 354 if (err) 355 goto err_module_to_port_map; 356 } 357 358 /* Create port objects for each valid entry */ 359 for (i = 0; i < mlxsw_m->max_ports; i++) { 360 if (mlxsw_m->module_to_port[i] > 0) { 361 err = mlxsw_m_port_create(mlxsw_m, 362 mlxsw_m->module_to_port[i], 363 i); 364 if (err) 365 goto err_module_to_port_create; 366 } 367 } 368 369 return 0; 370 371 err_module_to_port_create: 372 for (i--; i >= 0; i--) { 373 if (mlxsw_m->module_to_port[i] > 0) 374 mlxsw_m_port_remove(mlxsw_m, 375 mlxsw_m->module_to_port[i]); 376 } 377 i = max_ports; 378 err_module_to_port_map: 379 for (i--; i > 0; i--) 380 mlxsw_m_port_module_unmap(mlxsw_m, i); 381 kfree(mlxsw_m->module_to_port); 382 err_module_to_port_alloc: 383 kfree(mlxsw_m->ports); 384 return err; 385 } 386 387 static void mlxsw_m_ports_remove(struct mlxsw_m *mlxsw_m) 388 { 389 int i; 390 391 for (i = 0; i < mlxsw_m->max_ports; i++) { 392 if (mlxsw_m->module_to_port[i] > 0) { 393 mlxsw_m_port_remove(mlxsw_m, 394 mlxsw_m->module_to_port[i]); 395 mlxsw_m_port_module_unmap(mlxsw_m, i); 396 } 397 } 398 399 kfree(mlxsw_m->module_to_port); 400 kfree(mlxsw_m->ports); 401 } 402 403 static int mlxsw_m_fw_rev_validate(struct mlxsw_m *mlxsw_m) 404 { 405 const struct mlxsw_fw_rev *rev = &mlxsw_m->bus_info->fw_rev; 406 407 /* Validate driver and FW are compatible. 408 * Do not check major version, since it defines chip type, while 409 * driver is supposed to support any type. 410 */ 411 if (mlxsw_core_fw_rev_minor_subminor_validate(rev, &mlxsw_m_fw_rev)) 412 return 0; 413 414 dev_err(mlxsw_m->bus_info->dev, "The firmware version %d.%d.%d is incompatible with the driver (required >= %d.%d.%d)\n", 415 rev->major, rev->minor, rev->subminor, rev->major, 416 mlxsw_m_fw_rev.minor, mlxsw_m_fw_rev.subminor); 417 418 return -EINVAL; 419 } 420 421 static int mlxsw_m_init(struct mlxsw_core *mlxsw_core, 422 const struct mlxsw_bus_info *mlxsw_bus_info, 423 struct netlink_ext_ack *extack) 424 { 425 struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core); 426 int err; 427 428 mlxsw_m->core = mlxsw_core; 429 mlxsw_m->bus_info = mlxsw_bus_info; 430 431 err = mlxsw_m_fw_rev_validate(mlxsw_m); 432 if (err) 433 return err; 434 435 err = mlxsw_m_base_mac_get(mlxsw_m); 436 if (err) { 437 dev_err(mlxsw_m->bus_info->dev, "Failed to get base mac\n"); 438 return err; 439 } 440 441 err = mlxsw_m_ports_create(mlxsw_m); 442 if (err) { 443 dev_err(mlxsw_m->bus_info->dev, "Failed to create ports\n"); 444 return err; 445 } 446 447 return 0; 448 } 449 450 static void mlxsw_m_fini(struct mlxsw_core *mlxsw_core) 451 { 452 struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core); 453 454 mlxsw_m_ports_remove(mlxsw_m); 455 } 456 457 static const struct mlxsw_config_profile mlxsw_m_config_profile; 458 459 static struct mlxsw_driver mlxsw_m_driver = { 460 .kind = mlxsw_m_driver_name, 461 .priv_size = sizeof(struct mlxsw_m), 462 .init = mlxsw_m_init, 463 .fini = mlxsw_m_fini, 464 .profile = &mlxsw_m_config_profile, 465 }; 466 467 static const struct i2c_device_id mlxsw_m_i2c_id[] = { 468 { "mlxsw_minimal", 0}, 469 { }, 470 }; 471 472 static struct i2c_driver mlxsw_m_i2c_driver = { 473 .driver.name = "mlxsw_minimal", 474 .class = I2C_CLASS_HWMON, 475 .id_table = mlxsw_m_i2c_id, 476 }; 477 478 static int __init mlxsw_m_module_init(void) 479 { 480 int err; 481 482 err = mlxsw_core_driver_register(&mlxsw_m_driver); 483 if (err) 484 return err; 485 486 err = mlxsw_i2c_driver_register(&mlxsw_m_i2c_driver); 487 if (err) 488 goto err_i2c_driver_register; 489 490 return 0; 491 492 err_i2c_driver_register: 493 mlxsw_core_driver_unregister(&mlxsw_m_driver); 494 495 return err; 496 } 497 498 static void __exit mlxsw_m_module_exit(void) 499 { 500 mlxsw_i2c_driver_unregister(&mlxsw_m_i2c_driver); 501 mlxsw_core_driver_unregister(&mlxsw_m_driver); 502 } 503 504 module_init(mlxsw_m_module_init); 505 module_exit(mlxsw_m_module_exit); 506 507 MODULE_LICENSE("Dual BSD/GPL"); 508 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>"); 509 MODULE_DESCRIPTION("Mellanox minimal driver"); 510 MODULE_DEVICE_TABLE(i2c, mlxsw_m_i2c_id); 511