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