1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/debugfs.h> 3 #include <linux/delay.h> 4 #include <linux/gpio/consumer.h> 5 #include <linux/hwmon.h> 6 #include <linux/i2c.h> 7 #include <linux/interrupt.h> 8 #include <linux/jiffies.h> 9 #include <linux/mdio/mdio-i2c.h> 10 #include <linux/module.h> 11 #include <linux/mutex.h> 12 #include <linux/of.h> 13 #include <linux/phy.h> 14 #include <linux/platform_device.h> 15 #include <linux/rtnetlink.h> 16 #include <linux/slab.h> 17 #include <linux/workqueue.h> 18 19 #include "sfp.h" 20 #include "swphy.h" 21 22 enum { 23 GPIO_MODDEF0, 24 GPIO_LOS, 25 GPIO_TX_FAULT, 26 GPIO_TX_DISABLE, 27 GPIO_RATE_SELECT, 28 GPIO_MAX, 29 30 SFP_F_PRESENT = BIT(GPIO_MODDEF0), 31 SFP_F_LOS = BIT(GPIO_LOS), 32 SFP_F_TX_FAULT = BIT(GPIO_TX_FAULT), 33 SFP_F_TX_DISABLE = BIT(GPIO_TX_DISABLE), 34 SFP_F_RATE_SELECT = BIT(GPIO_RATE_SELECT), 35 36 SFP_E_INSERT = 0, 37 SFP_E_REMOVE, 38 SFP_E_DEV_ATTACH, 39 SFP_E_DEV_DETACH, 40 SFP_E_DEV_DOWN, 41 SFP_E_DEV_UP, 42 SFP_E_TX_FAULT, 43 SFP_E_TX_CLEAR, 44 SFP_E_LOS_HIGH, 45 SFP_E_LOS_LOW, 46 SFP_E_TIMEOUT, 47 48 SFP_MOD_EMPTY = 0, 49 SFP_MOD_ERROR, 50 SFP_MOD_PROBE, 51 SFP_MOD_WAITDEV, 52 SFP_MOD_HPOWER, 53 SFP_MOD_WAITPWR, 54 SFP_MOD_PRESENT, 55 56 SFP_DEV_DETACHED = 0, 57 SFP_DEV_DOWN, 58 SFP_DEV_UP, 59 60 SFP_S_DOWN = 0, 61 SFP_S_FAIL, 62 SFP_S_WAIT, 63 SFP_S_INIT, 64 SFP_S_INIT_PHY, 65 SFP_S_INIT_TX_FAULT, 66 SFP_S_WAIT_LOS, 67 SFP_S_LINK_UP, 68 SFP_S_TX_FAULT, 69 SFP_S_REINIT, 70 SFP_S_TX_DISABLE, 71 }; 72 73 static const char * const mod_state_strings[] = { 74 [SFP_MOD_EMPTY] = "empty", 75 [SFP_MOD_ERROR] = "error", 76 [SFP_MOD_PROBE] = "probe", 77 [SFP_MOD_WAITDEV] = "waitdev", 78 [SFP_MOD_HPOWER] = "hpower", 79 [SFP_MOD_WAITPWR] = "waitpwr", 80 [SFP_MOD_PRESENT] = "present", 81 }; 82 83 static const char *mod_state_to_str(unsigned short mod_state) 84 { 85 if (mod_state >= ARRAY_SIZE(mod_state_strings)) 86 return "Unknown module state"; 87 return mod_state_strings[mod_state]; 88 } 89 90 static const char * const dev_state_strings[] = { 91 [SFP_DEV_DETACHED] = "detached", 92 [SFP_DEV_DOWN] = "down", 93 [SFP_DEV_UP] = "up", 94 }; 95 96 static const char *dev_state_to_str(unsigned short dev_state) 97 { 98 if (dev_state >= ARRAY_SIZE(dev_state_strings)) 99 return "Unknown device state"; 100 return dev_state_strings[dev_state]; 101 } 102 103 static const char * const event_strings[] = { 104 [SFP_E_INSERT] = "insert", 105 [SFP_E_REMOVE] = "remove", 106 [SFP_E_DEV_ATTACH] = "dev_attach", 107 [SFP_E_DEV_DETACH] = "dev_detach", 108 [SFP_E_DEV_DOWN] = "dev_down", 109 [SFP_E_DEV_UP] = "dev_up", 110 [SFP_E_TX_FAULT] = "tx_fault", 111 [SFP_E_TX_CLEAR] = "tx_clear", 112 [SFP_E_LOS_HIGH] = "los_high", 113 [SFP_E_LOS_LOW] = "los_low", 114 [SFP_E_TIMEOUT] = "timeout", 115 }; 116 117 static const char *event_to_str(unsigned short event) 118 { 119 if (event >= ARRAY_SIZE(event_strings)) 120 return "Unknown event"; 121 return event_strings[event]; 122 } 123 124 static const char * const sm_state_strings[] = { 125 [SFP_S_DOWN] = "down", 126 [SFP_S_FAIL] = "fail", 127 [SFP_S_WAIT] = "wait", 128 [SFP_S_INIT] = "init", 129 [SFP_S_INIT_PHY] = "init_phy", 130 [SFP_S_INIT_TX_FAULT] = "init_tx_fault", 131 [SFP_S_WAIT_LOS] = "wait_los", 132 [SFP_S_LINK_UP] = "link_up", 133 [SFP_S_TX_FAULT] = "tx_fault", 134 [SFP_S_REINIT] = "reinit", 135 [SFP_S_TX_DISABLE] = "tx_disable", 136 }; 137 138 static const char *sm_state_to_str(unsigned short sm_state) 139 { 140 if (sm_state >= ARRAY_SIZE(sm_state_strings)) 141 return "Unknown state"; 142 return sm_state_strings[sm_state]; 143 } 144 145 static const char *gpio_names[] = { 146 "mod-def0", 147 "los", 148 "tx-fault", 149 "tx-disable", 150 "rate-select0", 151 }; 152 153 static const enum gpiod_flags gpio_flags[] = { 154 GPIOD_IN, 155 GPIOD_IN, 156 GPIOD_IN, 157 GPIOD_ASIS, 158 GPIOD_ASIS, 159 }; 160 161 /* t_start_up (SFF-8431) or t_init (SFF-8472) is the time required for a 162 * non-cooled module to initialise its laser safety circuitry. We wait 163 * an initial T_WAIT period before we check the tx fault to give any PHY 164 * on board (for a copper SFP) time to initialise. 165 */ 166 #define T_WAIT msecs_to_jiffies(50) 167 #define T_WAIT_ROLLBALL msecs_to_jiffies(25000) 168 #define T_START_UP msecs_to_jiffies(300) 169 #define T_START_UP_BAD_GPON msecs_to_jiffies(60000) 170 171 /* t_reset is the time required to assert the TX_DISABLE signal to reset 172 * an indicated TX_FAULT. 173 */ 174 #define T_RESET_US 10 175 #define T_FAULT_RECOVER msecs_to_jiffies(1000) 176 177 /* N_FAULT_INIT is the number of recovery attempts at module initialisation 178 * time. If the TX_FAULT signal is not deasserted after this number of 179 * attempts at clearing it, we decide that the module is faulty. 180 * N_FAULT is the same but after the module has initialised. 181 */ 182 #define N_FAULT_INIT 5 183 #define N_FAULT 5 184 185 /* T_PHY_RETRY is the time interval between attempts to probe the PHY. 186 * R_PHY_RETRY is the number of attempts. 187 */ 188 #define T_PHY_RETRY msecs_to_jiffies(50) 189 #define R_PHY_RETRY 12 190 191 /* SFP module presence detection is poor: the three MOD DEF signals are 192 * the same length on the PCB, which means it's possible for MOD DEF 0 to 193 * connect before the I2C bus on MOD DEF 1/2. 194 * 195 * The SFF-8472 specifies t_serial ("Time from power on until module is 196 * ready for data transmission over the two wire serial bus.") as 300ms. 197 */ 198 #define T_SERIAL msecs_to_jiffies(300) 199 #define T_HPOWER_LEVEL msecs_to_jiffies(300) 200 #define T_PROBE_RETRY_INIT msecs_to_jiffies(100) 201 #define R_PROBE_RETRY_INIT 10 202 #define T_PROBE_RETRY_SLOW msecs_to_jiffies(5000) 203 #define R_PROBE_RETRY_SLOW 12 204 205 /* SFP modules appear to always have their PHY configured for bus address 206 * 0x56 (which with mdio-i2c, translates to a PHY address of 22). 207 * RollBall SFPs access phy via SFP Enhanced Digital Diagnostic Interface 208 * via address 0x51 (mdio-i2c will use RollBall protocol on this address). 209 */ 210 #define SFP_PHY_ADDR 22 211 #define SFP_PHY_ADDR_ROLLBALL 17 212 213 /* SFP_EEPROM_BLOCK_SIZE is the size of data chunk to read the EEPROM 214 * at a time. Some SFP modules and also some Linux I2C drivers do not like 215 * reads longer than 16 bytes. 216 */ 217 #define SFP_EEPROM_BLOCK_SIZE 16 218 219 struct sff_data { 220 unsigned int gpios; 221 bool (*module_supported)(const struct sfp_eeprom_id *id); 222 }; 223 224 struct sfp { 225 struct device *dev; 226 struct i2c_adapter *i2c; 227 struct mii_bus *i2c_mii; 228 struct sfp_bus *sfp_bus; 229 enum mdio_i2c_proto mdio_protocol; 230 struct phy_device *mod_phy; 231 const struct sff_data *type; 232 size_t i2c_block_size; 233 u32 max_power_mW; 234 235 unsigned int (*get_state)(struct sfp *); 236 void (*set_state)(struct sfp *, unsigned int); 237 int (*read)(struct sfp *, bool, u8, void *, size_t); 238 int (*write)(struct sfp *, bool, u8, void *, size_t); 239 240 struct gpio_desc *gpio[GPIO_MAX]; 241 int gpio_irq[GPIO_MAX]; 242 243 bool need_poll; 244 245 struct mutex st_mutex; /* Protects state */ 246 unsigned int state_hw_mask; 247 unsigned int state_soft_mask; 248 unsigned int state; 249 struct delayed_work poll; 250 struct delayed_work timeout; 251 struct mutex sm_mutex; /* Protects state machine */ 252 unsigned char sm_mod_state; 253 unsigned char sm_mod_tries_init; 254 unsigned char sm_mod_tries; 255 unsigned char sm_dev_state; 256 unsigned short sm_state; 257 unsigned char sm_fault_retries; 258 unsigned char sm_phy_retries; 259 260 struct sfp_eeprom_id id; 261 unsigned int module_power_mW; 262 unsigned int module_t_start_up; 263 unsigned int module_t_wait; 264 bool tx_fault_ignore; 265 266 const struct sfp_quirk *quirk; 267 268 #if IS_ENABLED(CONFIG_HWMON) 269 struct sfp_diag diag; 270 struct delayed_work hwmon_probe; 271 unsigned int hwmon_tries; 272 struct device *hwmon_dev; 273 char *hwmon_name; 274 #endif 275 276 #if IS_ENABLED(CONFIG_DEBUG_FS) 277 struct dentry *debugfs_dir; 278 #endif 279 }; 280 281 static bool sff_module_supported(const struct sfp_eeprom_id *id) 282 { 283 return id->base.phys_id == SFF8024_ID_SFF_8472 && 284 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP; 285 } 286 287 static const struct sff_data sff_data = { 288 .gpios = SFP_F_LOS | SFP_F_TX_FAULT | SFP_F_TX_DISABLE, 289 .module_supported = sff_module_supported, 290 }; 291 292 static bool sfp_module_supported(const struct sfp_eeprom_id *id) 293 { 294 if (id->base.phys_id == SFF8024_ID_SFP && 295 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP) 296 return true; 297 298 /* SFP GPON module Ubiquiti U-Fiber Instant has in its EEPROM stored 299 * phys id SFF instead of SFP. Therefore mark this module explicitly 300 * as supported based on vendor name and pn match. 301 */ 302 if (id->base.phys_id == SFF8024_ID_SFF_8472 && 303 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP && 304 !memcmp(id->base.vendor_name, "UBNT ", 16) && 305 !memcmp(id->base.vendor_pn, "UF-INSTANT ", 16)) 306 return true; 307 308 return false; 309 } 310 311 static const struct sff_data sfp_data = { 312 .gpios = SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT | 313 SFP_F_TX_DISABLE | SFP_F_RATE_SELECT, 314 .module_supported = sfp_module_supported, 315 }; 316 317 static const struct of_device_id sfp_of_match[] = { 318 { .compatible = "sff,sff", .data = &sff_data, }, 319 { .compatible = "sff,sfp", .data = &sfp_data, }, 320 { }, 321 }; 322 MODULE_DEVICE_TABLE(of, sfp_of_match); 323 324 static void sfp_fixup_long_startup(struct sfp *sfp) 325 { 326 sfp->module_t_start_up = T_START_UP_BAD_GPON; 327 } 328 329 static void sfp_fixup_ignore_tx_fault(struct sfp *sfp) 330 { 331 sfp->tx_fault_ignore = true; 332 } 333 334 static void sfp_fixup_halny_gsfp(struct sfp *sfp) 335 { 336 /* Ignore the TX_FAULT and LOS signals on this module. 337 * these are possibly used for other purposes on this 338 * module, e.g. a serial port. 339 */ 340 sfp->state_hw_mask &= ~(SFP_F_TX_FAULT | SFP_F_LOS); 341 } 342 343 static void sfp_fixup_rollball(struct sfp *sfp) 344 { 345 sfp->mdio_protocol = MDIO_I2C_ROLLBALL; 346 sfp->module_t_wait = T_WAIT_ROLLBALL; 347 } 348 349 static void sfp_fixup_rollball_cc(struct sfp *sfp) 350 { 351 sfp_fixup_rollball(sfp); 352 353 /* Some RollBall SFPs may have wrong (zero) extended compliance code 354 * burned in EEPROM. For PHY probing we need the correct one. 355 */ 356 sfp->id.base.extended_cc = SFF8024_ECC_10GBASE_T_SFI; 357 } 358 359 static void sfp_quirk_2500basex(const struct sfp_eeprom_id *id, 360 unsigned long *modes, 361 unsigned long *interfaces) 362 { 363 linkmode_set_bit(ETHTOOL_LINK_MODE_2500baseX_Full_BIT, modes); 364 __set_bit(PHY_INTERFACE_MODE_2500BASEX, interfaces); 365 } 366 367 static void sfp_quirk_ubnt_uf_instant(const struct sfp_eeprom_id *id, 368 unsigned long *modes, 369 unsigned long *interfaces) 370 { 371 /* Ubiquiti U-Fiber Instant module claims that support all transceiver 372 * types including 10G Ethernet which is not truth. So clear all claimed 373 * modes and set only one mode which module supports: 1000baseX_Full. 374 */ 375 linkmode_zero(modes); 376 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, modes); 377 } 378 379 #define SFP_QUIRK(_v, _p, _m, _f) \ 380 { .vendor = _v, .part = _p, .modes = _m, .fixup = _f, } 381 #define SFP_QUIRK_M(_v, _p, _m) SFP_QUIRK(_v, _p, _m, NULL) 382 #define SFP_QUIRK_F(_v, _p, _f) SFP_QUIRK(_v, _p, NULL, _f) 383 384 static const struct sfp_quirk sfp_quirks[] = { 385 // Alcatel Lucent G-010S-P can operate at 2500base-X, but incorrectly 386 // report 2500MBd NRZ in their EEPROM 387 SFP_QUIRK_M("ALCATELLUCENT", "G010SP", sfp_quirk_2500basex), 388 389 // Alcatel Lucent G-010S-A can operate at 2500base-X, but report 3.2GBd 390 // NRZ in their EEPROM 391 SFP_QUIRK("ALCATELLUCENT", "3FE46541AA", sfp_quirk_2500basex, 392 sfp_fixup_long_startup), 393 394 SFP_QUIRK_F("HALNy", "HL-GSFP", sfp_fixup_halny_gsfp), 395 396 // HG MXPD-483II-F 2.5G supports 2500Base-X, but incorrectly reports 397 // 2600MBd in their EERPOM 398 SFP_QUIRK_M("HG GENUINE", "MXPD-483II", sfp_quirk_2500basex), 399 400 // Huawei MA5671A can operate at 2500base-X, but report 1.2GBd NRZ in 401 // their EEPROM 402 SFP_QUIRK("HUAWEI", "MA5671A", sfp_quirk_2500basex, 403 sfp_fixup_ignore_tx_fault), 404 405 // Lantech 8330-262D-E can operate at 2500base-X, but incorrectly report 406 // 2500MBd NRZ in their EEPROM 407 SFP_QUIRK_M("Lantech", "8330-262D-E", sfp_quirk_2500basex), 408 409 SFP_QUIRK_M("UBNT", "UF-INSTANT", sfp_quirk_ubnt_uf_instant), 410 411 SFP_QUIRK_F("OEM", "SFP-10G-T", sfp_fixup_rollball_cc), 412 SFP_QUIRK_F("OEM", "RTSFP-10", sfp_fixup_rollball_cc), 413 SFP_QUIRK_F("OEM", "RTSFP-10G", sfp_fixup_rollball_cc), 414 SFP_QUIRK_F("Turris", "RTSFP-10", sfp_fixup_rollball), 415 SFP_QUIRK_F("Turris", "RTSFP-10G", sfp_fixup_rollball), 416 }; 417 418 static size_t sfp_strlen(const char *str, size_t maxlen) 419 { 420 size_t size, i; 421 422 /* Trailing characters should be filled with space chars, but 423 * some manufacturers can't read SFF-8472 and use NUL. 424 */ 425 for (i = 0, size = 0; i < maxlen; i++) 426 if (str[i] != ' ' && str[i] != '\0') 427 size = i + 1; 428 429 return size; 430 } 431 432 static bool sfp_match(const char *qs, const char *str, size_t len) 433 { 434 if (!qs) 435 return true; 436 if (strlen(qs) != len) 437 return false; 438 return !strncmp(qs, str, len); 439 } 440 441 static const struct sfp_quirk *sfp_lookup_quirk(const struct sfp_eeprom_id *id) 442 { 443 const struct sfp_quirk *q; 444 unsigned int i; 445 size_t vs, ps; 446 447 vs = sfp_strlen(id->base.vendor_name, ARRAY_SIZE(id->base.vendor_name)); 448 ps = sfp_strlen(id->base.vendor_pn, ARRAY_SIZE(id->base.vendor_pn)); 449 450 for (i = 0, q = sfp_quirks; i < ARRAY_SIZE(sfp_quirks); i++, q++) 451 if (sfp_match(q->vendor, id->base.vendor_name, vs) && 452 sfp_match(q->part, id->base.vendor_pn, ps)) 453 return q; 454 455 return NULL; 456 } 457 458 static unsigned long poll_jiffies; 459 460 static unsigned int sfp_gpio_get_state(struct sfp *sfp) 461 { 462 unsigned int i, state, v; 463 464 for (i = state = 0; i < GPIO_MAX; i++) { 465 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i]) 466 continue; 467 468 v = gpiod_get_value_cansleep(sfp->gpio[i]); 469 if (v) 470 state |= BIT(i); 471 } 472 473 return state; 474 } 475 476 static unsigned int sff_gpio_get_state(struct sfp *sfp) 477 { 478 return sfp_gpio_get_state(sfp) | SFP_F_PRESENT; 479 } 480 481 static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state) 482 { 483 if (state & SFP_F_PRESENT) { 484 /* If the module is present, drive the signals */ 485 if (sfp->gpio[GPIO_TX_DISABLE]) 486 gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE], 487 state & SFP_F_TX_DISABLE); 488 if (state & SFP_F_RATE_SELECT) 489 gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT], 490 state & SFP_F_RATE_SELECT); 491 } else { 492 /* Otherwise, let them float to the pull-ups */ 493 if (sfp->gpio[GPIO_TX_DISABLE]) 494 gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]); 495 if (state & SFP_F_RATE_SELECT) 496 gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]); 497 } 498 } 499 500 static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf, 501 size_t len) 502 { 503 struct i2c_msg msgs[2]; 504 u8 bus_addr = a2 ? 0x51 : 0x50; 505 size_t block_size = sfp->i2c_block_size; 506 size_t this_len; 507 int ret; 508 509 msgs[0].addr = bus_addr; 510 msgs[0].flags = 0; 511 msgs[0].len = 1; 512 msgs[0].buf = &dev_addr; 513 msgs[1].addr = bus_addr; 514 msgs[1].flags = I2C_M_RD; 515 msgs[1].len = len; 516 msgs[1].buf = buf; 517 518 while (len) { 519 this_len = len; 520 if (this_len > block_size) 521 this_len = block_size; 522 523 msgs[1].len = this_len; 524 525 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs)); 526 if (ret < 0) 527 return ret; 528 529 if (ret != ARRAY_SIZE(msgs)) 530 break; 531 532 msgs[1].buf += this_len; 533 dev_addr += this_len; 534 len -= this_len; 535 } 536 537 return msgs[1].buf - (u8 *)buf; 538 } 539 540 static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf, 541 size_t len) 542 { 543 struct i2c_msg msgs[1]; 544 u8 bus_addr = a2 ? 0x51 : 0x50; 545 int ret; 546 547 msgs[0].addr = bus_addr; 548 msgs[0].flags = 0; 549 msgs[0].len = 1 + len; 550 msgs[0].buf = kmalloc(1 + len, GFP_KERNEL); 551 if (!msgs[0].buf) 552 return -ENOMEM; 553 554 msgs[0].buf[0] = dev_addr; 555 memcpy(&msgs[0].buf[1], buf, len); 556 557 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs)); 558 559 kfree(msgs[0].buf); 560 561 if (ret < 0) 562 return ret; 563 564 return ret == ARRAY_SIZE(msgs) ? len : 0; 565 } 566 567 static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c) 568 { 569 if (!i2c_check_functionality(i2c, I2C_FUNC_I2C)) 570 return -EINVAL; 571 572 sfp->i2c = i2c; 573 sfp->read = sfp_i2c_read; 574 sfp->write = sfp_i2c_write; 575 576 return 0; 577 } 578 579 static int sfp_i2c_mdiobus_create(struct sfp *sfp) 580 { 581 struct mii_bus *i2c_mii; 582 int ret; 583 584 i2c_mii = mdio_i2c_alloc(sfp->dev, sfp->i2c, sfp->mdio_protocol); 585 if (IS_ERR(i2c_mii)) 586 return PTR_ERR(i2c_mii); 587 588 i2c_mii->name = "SFP I2C Bus"; 589 i2c_mii->phy_mask = ~0; 590 591 ret = mdiobus_register(i2c_mii); 592 if (ret < 0) { 593 mdiobus_free(i2c_mii); 594 return ret; 595 } 596 597 sfp->i2c_mii = i2c_mii; 598 599 return 0; 600 } 601 602 static void sfp_i2c_mdiobus_destroy(struct sfp *sfp) 603 { 604 mdiobus_unregister(sfp->i2c_mii); 605 sfp->i2c_mii = NULL; 606 } 607 608 /* Interface */ 609 static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len) 610 { 611 return sfp->read(sfp, a2, addr, buf, len); 612 } 613 614 static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len) 615 { 616 return sfp->write(sfp, a2, addr, buf, len); 617 } 618 619 static int sfp_modify_u8(struct sfp *sfp, bool a2, u8 addr, u8 mask, u8 val) 620 { 621 int ret; 622 u8 old, v; 623 624 ret = sfp_read(sfp, a2, addr, &old, sizeof(old)); 625 if (ret != sizeof(old)) 626 return ret; 627 628 v = (old & ~mask) | (val & mask); 629 if (v == old) 630 return sizeof(v); 631 632 return sfp_write(sfp, a2, addr, &v, sizeof(v)); 633 } 634 635 static unsigned int sfp_soft_get_state(struct sfp *sfp) 636 { 637 unsigned int state = 0; 638 u8 status; 639 int ret; 640 641 ret = sfp_read(sfp, true, SFP_STATUS, &status, sizeof(status)); 642 if (ret == sizeof(status)) { 643 if (status & SFP_STATUS_RX_LOS) 644 state |= SFP_F_LOS; 645 if (status & SFP_STATUS_TX_FAULT) 646 state |= SFP_F_TX_FAULT; 647 } else { 648 dev_err_ratelimited(sfp->dev, 649 "failed to read SFP soft status: %pe\n", 650 ERR_PTR(ret)); 651 /* Preserve the current state */ 652 state = sfp->state; 653 } 654 655 return state & sfp->state_soft_mask; 656 } 657 658 static void sfp_soft_set_state(struct sfp *sfp, unsigned int state) 659 { 660 u8 mask = SFP_STATUS_TX_DISABLE_FORCE; 661 u8 val = 0; 662 663 if (state & SFP_F_TX_DISABLE) 664 val |= SFP_STATUS_TX_DISABLE_FORCE; 665 666 667 sfp_modify_u8(sfp, true, SFP_STATUS, mask, val); 668 } 669 670 static void sfp_soft_start_poll(struct sfp *sfp) 671 { 672 const struct sfp_eeprom_id *id = &sfp->id; 673 unsigned int mask = 0; 674 675 sfp->state_soft_mask = 0; 676 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_DISABLE) 677 mask |= SFP_F_TX_DISABLE; 678 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_FAULT) 679 mask |= SFP_F_TX_FAULT; 680 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_RX_LOS) 681 mask |= SFP_F_LOS; 682 683 // Poll the soft state for hardware pins we want to ignore 684 sfp->state_soft_mask = ~sfp->state_hw_mask & mask; 685 686 if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) && 687 !sfp->need_poll) 688 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies); 689 } 690 691 static void sfp_soft_stop_poll(struct sfp *sfp) 692 { 693 sfp->state_soft_mask = 0; 694 } 695 696 static unsigned int sfp_get_state(struct sfp *sfp) 697 { 698 unsigned int soft = sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT); 699 unsigned int state; 700 701 state = sfp->get_state(sfp) & sfp->state_hw_mask; 702 if (state & SFP_F_PRESENT && soft) 703 state |= sfp_soft_get_state(sfp); 704 705 return state; 706 } 707 708 static void sfp_set_state(struct sfp *sfp, unsigned int state) 709 { 710 sfp->set_state(sfp, state); 711 712 if (state & SFP_F_PRESENT && 713 sfp->state_soft_mask & SFP_F_TX_DISABLE) 714 sfp_soft_set_state(sfp, state); 715 } 716 717 static unsigned int sfp_check(void *buf, size_t len) 718 { 719 u8 *p, check; 720 721 for (p = buf, check = 0; len; p++, len--) 722 check += *p; 723 724 return check; 725 } 726 727 /* hwmon */ 728 #if IS_ENABLED(CONFIG_HWMON) 729 static umode_t sfp_hwmon_is_visible(const void *data, 730 enum hwmon_sensor_types type, 731 u32 attr, int channel) 732 { 733 const struct sfp *sfp = data; 734 735 switch (type) { 736 case hwmon_temp: 737 switch (attr) { 738 case hwmon_temp_min_alarm: 739 case hwmon_temp_max_alarm: 740 case hwmon_temp_lcrit_alarm: 741 case hwmon_temp_crit_alarm: 742 case hwmon_temp_min: 743 case hwmon_temp_max: 744 case hwmon_temp_lcrit: 745 case hwmon_temp_crit: 746 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN)) 747 return 0; 748 fallthrough; 749 case hwmon_temp_input: 750 case hwmon_temp_label: 751 return 0444; 752 default: 753 return 0; 754 } 755 case hwmon_in: 756 switch (attr) { 757 case hwmon_in_min_alarm: 758 case hwmon_in_max_alarm: 759 case hwmon_in_lcrit_alarm: 760 case hwmon_in_crit_alarm: 761 case hwmon_in_min: 762 case hwmon_in_max: 763 case hwmon_in_lcrit: 764 case hwmon_in_crit: 765 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN)) 766 return 0; 767 fallthrough; 768 case hwmon_in_input: 769 case hwmon_in_label: 770 return 0444; 771 default: 772 return 0; 773 } 774 case hwmon_curr: 775 switch (attr) { 776 case hwmon_curr_min_alarm: 777 case hwmon_curr_max_alarm: 778 case hwmon_curr_lcrit_alarm: 779 case hwmon_curr_crit_alarm: 780 case hwmon_curr_min: 781 case hwmon_curr_max: 782 case hwmon_curr_lcrit: 783 case hwmon_curr_crit: 784 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN)) 785 return 0; 786 fallthrough; 787 case hwmon_curr_input: 788 case hwmon_curr_label: 789 return 0444; 790 default: 791 return 0; 792 } 793 case hwmon_power: 794 /* External calibration of receive power requires 795 * floating point arithmetic. Doing that in the kernel 796 * is not easy, so just skip it. If the module does 797 * not require external calibration, we can however 798 * show receiver power, since FP is then not needed. 799 */ 800 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL && 801 channel == 1) 802 return 0; 803 switch (attr) { 804 case hwmon_power_min_alarm: 805 case hwmon_power_max_alarm: 806 case hwmon_power_lcrit_alarm: 807 case hwmon_power_crit_alarm: 808 case hwmon_power_min: 809 case hwmon_power_max: 810 case hwmon_power_lcrit: 811 case hwmon_power_crit: 812 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN)) 813 return 0; 814 fallthrough; 815 case hwmon_power_input: 816 case hwmon_power_label: 817 return 0444; 818 default: 819 return 0; 820 } 821 default: 822 return 0; 823 } 824 } 825 826 static int sfp_hwmon_read_sensor(struct sfp *sfp, int reg, long *value) 827 { 828 __be16 val; 829 int err; 830 831 err = sfp_read(sfp, true, reg, &val, sizeof(val)); 832 if (err < 0) 833 return err; 834 835 *value = be16_to_cpu(val); 836 837 return 0; 838 } 839 840 static void sfp_hwmon_to_rx_power(long *value) 841 { 842 *value = DIV_ROUND_CLOSEST(*value, 10); 843 } 844 845 static void sfp_hwmon_calibrate(struct sfp *sfp, unsigned int slope, int offset, 846 long *value) 847 { 848 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL) 849 *value = DIV_ROUND_CLOSEST(*value * slope, 256) + offset; 850 } 851 852 static void sfp_hwmon_calibrate_temp(struct sfp *sfp, long *value) 853 { 854 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_t_slope), 855 be16_to_cpu(sfp->diag.cal_t_offset), value); 856 857 if (*value >= 0x8000) 858 *value -= 0x10000; 859 860 *value = DIV_ROUND_CLOSEST(*value * 1000, 256); 861 } 862 863 static void sfp_hwmon_calibrate_vcc(struct sfp *sfp, long *value) 864 { 865 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_v_slope), 866 be16_to_cpu(sfp->diag.cal_v_offset), value); 867 868 *value = DIV_ROUND_CLOSEST(*value, 10); 869 } 870 871 static void sfp_hwmon_calibrate_bias(struct sfp *sfp, long *value) 872 { 873 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txi_slope), 874 be16_to_cpu(sfp->diag.cal_txi_offset), value); 875 876 *value = DIV_ROUND_CLOSEST(*value, 500); 877 } 878 879 static void sfp_hwmon_calibrate_tx_power(struct sfp *sfp, long *value) 880 { 881 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txpwr_slope), 882 be16_to_cpu(sfp->diag.cal_txpwr_offset), value); 883 884 *value = DIV_ROUND_CLOSEST(*value, 10); 885 } 886 887 static int sfp_hwmon_read_temp(struct sfp *sfp, int reg, long *value) 888 { 889 int err; 890 891 err = sfp_hwmon_read_sensor(sfp, reg, value); 892 if (err < 0) 893 return err; 894 895 sfp_hwmon_calibrate_temp(sfp, value); 896 897 return 0; 898 } 899 900 static int sfp_hwmon_read_vcc(struct sfp *sfp, int reg, long *value) 901 { 902 int err; 903 904 err = sfp_hwmon_read_sensor(sfp, reg, value); 905 if (err < 0) 906 return err; 907 908 sfp_hwmon_calibrate_vcc(sfp, value); 909 910 return 0; 911 } 912 913 static int sfp_hwmon_read_bias(struct sfp *sfp, int reg, long *value) 914 { 915 int err; 916 917 err = sfp_hwmon_read_sensor(sfp, reg, value); 918 if (err < 0) 919 return err; 920 921 sfp_hwmon_calibrate_bias(sfp, value); 922 923 return 0; 924 } 925 926 static int sfp_hwmon_read_tx_power(struct sfp *sfp, int reg, long *value) 927 { 928 int err; 929 930 err = sfp_hwmon_read_sensor(sfp, reg, value); 931 if (err < 0) 932 return err; 933 934 sfp_hwmon_calibrate_tx_power(sfp, value); 935 936 return 0; 937 } 938 939 static int sfp_hwmon_read_rx_power(struct sfp *sfp, int reg, long *value) 940 { 941 int err; 942 943 err = sfp_hwmon_read_sensor(sfp, reg, value); 944 if (err < 0) 945 return err; 946 947 sfp_hwmon_to_rx_power(value); 948 949 return 0; 950 } 951 952 static int sfp_hwmon_temp(struct sfp *sfp, u32 attr, long *value) 953 { 954 u8 status; 955 int err; 956 957 switch (attr) { 958 case hwmon_temp_input: 959 return sfp_hwmon_read_temp(sfp, SFP_TEMP, value); 960 961 case hwmon_temp_lcrit: 962 *value = be16_to_cpu(sfp->diag.temp_low_alarm); 963 sfp_hwmon_calibrate_temp(sfp, value); 964 return 0; 965 966 case hwmon_temp_min: 967 *value = be16_to_cpu(sfp->diag.temp_low_warn); 968 sfp_hwmon_calibrate_temp(sfp, value); 969 return 0; 970 case hwmon_temp_max: 971 *value = be16_to_cpu(sfp->diag.temp_high_warn); 972 sfp_hwmon_calibrate_temp(sfp, value); 973 return 0; 974 975 case hwmon_temp_crit: 976 *value = be16_to_cpu(sfp->diag.temp_high_alarm); 977 sfp_hwmon_calibrate_temp(sfp, value); 978 return 0; 979 980 case hwmon_temp_lcrit_alarm: 981 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 982 if (err < 0) 983 return err; 984 985 *value = !!(status & SFP_ALARM0_TEMP_LOW); 986 return 0; 987 988 case hwmon_temp_min_alarm: 989 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 990 if (err < 0) 991 return err; 992 993 *value = !!(status & SFP_WARN0_TEMP_LOW); 994 return 0; 995 996 case hwmon_temp_max_alarm: 997 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 998 if (err < 0) 999 return err; 1000 1001 *value = !!(status & SFP_WARN0_TEMP_HIGH); 1002 return 0; 1003 1004 case hwmon_temp_crit_alarm: 1005 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 1006 if (err < 0) 1007 return err; 1008 1009 *value = !!(status & SFP_ALARM0_TEMP_HIGH); 1010 return 0; 1011 default: 1012 return -EOPNOTSUPP; 1013 } 1014 1015 return -EOPNOTSUPP; 1016 } 1017 1018 static int sfp_hwmon_vcc(struct sfp *sfp, u32 attr, long *value) 1019 { 1020 u8 status; 1021 int err; 1022 1023 switch (attr) { 1024 case hwmon_in_input: 1025 return sfp_hwmon_read_vcc(sfp, SFP_VCC, value); 1026 1027 case hwmon_in_lcrit: 1028 *value = be16_to_cpu(sfp->diag.volt_low_alarm); 1029 sfp_hwmon_calibrate_vcc(sfp, value); 1030 return 0; 1031 1032 case hwmon_in_min: 1033 *value = be16_to_cpu(sfp->diag.volt_low_warn); 1034 sfp_hwmon_calibrate_vcc(sfp, value); 1035 return 0; 1036 1037 case hwmon_in_max: 1038 *value = be16_to_cpu(sfp->diag.volt_high_warn); 1039 sfp_hwmon_calibrate_vcc(sfp, value); 1040 return 0; 1041 1042 case hwmon_in_crit: 1043 *value = be16_to_cpu(sfp->diag.volt_high_alarm); 1044 sfp_hwmon_calibrate_vcc(sfp, value); 1045 return 0; 1046 1047 case hwmon_in_lcrit_alarm: 1048 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 1049 if (err < 0) 1050 return err; 1051 1052 *value = !!(status & SFP_ALARM0_VCC_LOW); 1053 return 0; 1054 1055 case hwmon_in_min_alarm: 1056 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 1057 if (err < 0) 1058 return err; 1059 1060 *value = !!(status & SFP_WARN0_VCC_LOW); 1061 return 0; 1062 1063 case hwmon_in_max_alarm: 1064 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 1065 if (err < 0) 1066 return err; 1067 1068 *value = !!(status & SFP_WARN0_VCC_HIGH); 1069 return 0; 1070 1071 case hwmon_in_crit_alarm: 1072 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 1073 if (err < 0) 1074 return err; 1075 1076 *value = !!(status & SFP_ALARM0_VCC_HIGH); 1077 return 0; 1078 default: 1079 return -EOPNOTSUPP; 1080 } 1081 1082 return -EOPNOTSUPP; 1083 } 1084 1085 static int sfp_hwmon_bias(struct sfp *sfp, u32 attr, long *value) 1086 { 1087 u8 status; 1088 int err; 1089 1090 switch (attr) { 1091 case hwmon_curr_input: 1092 return sfp_hwmon_read_bias(sfp, SFP_TX_BIAS, value); 1093 1094 case hwmon_curr_lcrit: 1095 *value = be16_to_cpu(sfp->diag.bias_low_alarm); 1096 sfp_hwmon_calibrate_bias(sfp, value); 1097 return 0; 1098 1099 case hwmon_curr_min: 1100 *value = be16_to_cpu(sfp->diag.bias_low_warn); 1101 sfp_hwmon_calibrate_bias(sfp, value); 1102 return 0; 1103 1104 case hwmon_curr_max: 1105 *value = be16_to_cpu(sfp->diag.bias_high_warn); 1106 sfp_hwmon_calibrate_bias(sfp, value); 1107 return 0; 1108 1109 case hwmon_curr_crit: 1110 *value = be16_to_cpu(sfp->diag.bias_high_alarm); 1111 sfp_hwmon_calibrate_bias(sfp, value); 1112 return 0; 1113 1114 case hwmon_curr_lcrit_alarm: 1115 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 1116 if (err < 0) 1117 return err; 1118 1119 *value = !!(status & SFP_ALARM0_TX_BIAS_LOW); 1120 return 0; 1121 1122 case hwmon_curr_min_alarm: 1123 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 1124 if (err < 0) 1125 return err; 1126 1127 *value = !!(status & SFP_WARN0_TX_BIAS_LOW); 1128 return 0; 1129 1130 case hwmon_curr_max_alarm: 1131 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 1132 if (err < 0) 1133 return err; 1134 1135 *value = !!(status & SFP_WARN0_TX_BIAS_HIGH); 1136 return 0; 1137 1138 case hwmon_curr_crit_alarm: 1139 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 1140 if (err < 0) 1141 return err; 1142 1143 *value = !!(status & SFP_ALARM0_TX_BIAS_HIGH); 1144 return 0; 1145 default: 1146 return -EOPNOTSUPP; 1147 } 1148 1149 return -EOPNOTSUPP; 1150 } 1151 1152 static int sfp_hwmon_tx_power(struct sfp *sfp, u32 attr, long *value) 1153 { 1154 u8 status; 1155 int err; 1156 1157 switch (attr) { 1158 case hwmon_power_input: 1159 return sfp_hwmon_read_tx_power(sfp, SFP_TX_POWER, value); 1160 1161 case hwmon_power_lcrit: 1162 *value = be16_to_cpu(sfp->diag.txpwr_low_alarm); 1163 sfp_hwmon_calibrate_tx_power(sfp, value); 1164 return 0; 1165 1166 case hwmon_power_min: 1167 *value = be16_to_cpu(sfp->diag.txpwr_low_warn); 1168 sfp_hwmon_calibrate_tx_power(sfp, value); 1169 return 0; 1170 1171 case hwmon_power_max: 1172 *value = be16_to_cpu(sfp->diag.txpwr_high_warn); 1173 sfp_hwmon_calibrate_tx_power(sfp, value); 1174 return 0; 1175 1176 case hwmon_power_crit: 1177 *value = be16_to_cpu(sfp->diag.txpwr_high_alarm); 1178 sfp_hwmon_calibrate_tx_power(sfp, value); 1179 return 0; 1180 1181 case hwmon_power_lcrit_alarm: 1182 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 1183 if (err < 0) 1184 return err; 1185 1186 *value = !!(status & SFP_ALARM0_TXPWR_LOW); 1187 return 0; 1188 1189 case hwmon_power_min_alarm: 1190 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 1191 if (err < 0) 1192 return err; 1193 1194 *value = !!(status & SFP_WARN0_TXPWR_LOW); 1195 return 0; 1196 1197 case hwmon_power_max_alarm: 1198 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 1199 if (err < 0) 1200 return err; 1201 1202 *value = !!(status & SFP_WARN0_TXPWR_HIGH); 1203 return 0; 1204 1205 case hwmon_power_crit_alarm: 1206 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 1207 if (err < 0) 1208 return err; 1209 1210 *value = !!(status & SFP_ALARM0_TXPWR_HIGH); 1211 return 0; 1212 default: 1213 return -EOPNOTSUPP; 1214 } 1215 1216 return -EOPNOTSUPP; 1217 } 1218 1219 static int sfp_hwmon_rx_power(struct sfp *sfp, u32 attr, long *value) 1220 { 1221 u8 status; 1222 int err; 1223 1224 switch (attr) { 1225 case hwmon_power_input: 1226 return sfp_hwmon_read_rx_power(sfp, SFP_RX_POWER, value); 1227 1228 case hwmon_power_lcrit: 1229 *value = be16_to_cpu(sfp->diag.rxpwr_low_alarm); 1230 sfp_hwmon_to_rx_power(value); 1231 return 0; 1232 1233 case hwmon_power_min: 1234 *value = be16_to_cpu(sfp->diag.rxpwr_low_warn); 1235 sfp_hwmon_to_rx_power(value); 1236 return 0; 1237 1238 case hwmon_power_max: 1239 *value = be16_to_cpu(sfp->diag.rxpwr_high_warn); 1240 sfp_hwmon_to_rx_power(value); 1241 return 0; 1242 1243 case hwmon_power_crit: 1244 *value = be16_to_cpu(sfp->diag.rxpwr_high_alarm); 1245 sfp_hwmon_to_rx_power(value); 1246 return 0; 1247 1248 case hwmon_power_lcrit_alarm: 1249 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status)); 1250 if (err < 0) 1251 return err; 1252 1253 *value = !!(status & SFP_ALARM1_RXPWR_LOW); 1254 return 0; 1255 1256 case hwmon_power_min_alarm: 1257 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status)); 1258 if (err < 0) 1259 return err; 1260 1261 *value = !!(status & SFP_WARN1_RXPWR_LOW); 1262 return 0; 1263 1264 case hwmon_power_max_alarm: 1265 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status)); 1266 if (err < 0) 1267 return err; 1268 1269 *value = !!(status & SFP_WARN1_RXPWR_HIGH); 1270 return 0; 1271 1272 case hwmon_power_crit_alarm: 1273 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status)); 1274 if (err < 0) 1275 return err; 1276 1277 *value = !!(status & SFP_ALARM1_RXPWR_HIGH); 1278 return 0; 1279 default: 1280 return -EOPNOTSUPP; 1281 } 1282 1283 return -EOPNOTSUPP; 1284 } 1285 1286 static int sfp_hwmon_read(struct device *dev, enum hwmon_sensor_types type, 1287 u32 attr, int channel, long *value) 1288 { 1289 struct sfp *sfp = dev_get_drvdata(dev); 1290 1291 switch (type) { 1292 case hwmon_temp: 1293 return sfp_hwmon_temp(sfp, attr, value); 1294 case hwmon_in: 1295 return sfp_hwmon_vcc(sfp, attr, value); 1296 case hwmon_curr: 1297 return sfp_hwmon_bias(sfp, attr, value); 1298 case hwmon_power: 1299 switch (channel) { 1300 case 0: 1301 return sfp_hwmon_tx_power(sfp, attr, value); 1302 case 1: 1303 return sfp_hwmon_rx_power(sfp, attr, value); 1304 default: 1305 return -EOPNOTSUPP; 1306 } 1307 default: 1308 return -EOPNOTSUPP; 1309 } 1310 } 1311 1312 static const char *const sfp_hwmon_power_labels[] = { 1313 "TX_power", 1314 "RX_power", 1315 }; 1316 1317 static int sfp_hwmon_read_string(struct device *dev, 1318 enum hwmon_sensor_types type, 1319 u32 attr, int channel, const char **str) 1320 { 1321 switch (type) { 1322 case hwmon_curr: 1323 switch (attr) { 1324 case hwmon_curr_label: 1325 *str = "bias"; 1326 return 0; 1327 default: 1328 return -EOPNOTSUPP; 1329 } 1330 break; 1331 case hwmon_temp: 1332 switch (attr) { 1333 case hwmon_temp_label: 1334 *str = "temperature"; 1335 return 0; 1336 default: 1337 return -EOPNOTSUPP; 1338 } 1339 break; 1340 case hwmon_in: 1341 switch (attr) { 1342 case hwmon_in_label: 1343 *str = "VCC"; 1344 return 0; 1345 default: 1346 return -EOPNOTSUPP; 1347 } 1348 break; 1349 case hwmon_power: 1350 switch (attr) { 1351 case hwmon_power_label: 1352 *str = sfp_hwmon_power_labels[channel]; 1353 return 0; 1354 default: 1355 return -EOPNOTSUPP; 1356 } 1357 break; 1358 default: 1359 return -EOPNOTSUPP; 1360 } 1361 1362 return -EOPNOTSUPP; 1363 } 1364 1365 static const struct hwmon_ops sfp_hwmon_ops = { 1366 .is_visible = sfp_hwmon_is_visible, 1367 .read = sfp_hwmon_read, 1368 .read_string = sfp_hwmon_read_string, 1369 }; 1370 1371 static const struct hwmon_channel_info *sfp_hwmon_info[] = { 1372 HWMON_CHANNEL_INFO(chip, 1373 HWMON_C_REGISTER_TZ), 1374 HWMON_CHANNEL_INFO(in, 1375 HWMON_I_INPUT | 1376 HWMON_I_MAX | HWMON_I_MIN | 1377 HWMON_I_MAX_ALARM | HWMON_I_MIN_ALARM | 1378 HWMON_I_CRIT | HWMON_I_LCRIT | 1379 HWMON_I_CRIT_ALARM | HWMON_I_LCRIT_ALARM | 1380 HWMON_I_LABEL), 1381 HWMON_CHANNEL_INFO(temp, 1382 HWMON_T_INPUT | 1383 HWMON_T_MAX | HWMON_T_MIN | 1384 HWMON_T_MAX_ALARM | HWMON_T_MIN_ALARM | 1385 HWMON_T_CRIT | HWMON_T_LCRIT | 1386 HWMON_T_CRIT_ALARM | HWMON_T_LCRIT_ALARM | 1387 HWMON_T_LABEL), 1388 HWMON_CHANNEL_INFO(curr, 1389 HWMON_C_INPUT | 1390 HWMON_C_MAX | HWMON_C_MIN | 1391 HWMON_C_MAX_ALARM | HWMON_C_MIN_ALARM | 1392 HWMON_C_CRIT | HWMON_C_LCRIT | 1393 HWMON_C_CRIT_ALARM | HWMON_C_LCRIT_ALARM | 1394 HWMON_C_LABEL), 1395 HWMON_CHANNEL_INFO(power, 1396 /* Transmit power */ 1397 HWMON_P_INPUT | 1398 HWMON_P_MAX | HWMON_P_MIN | 1399 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM | 1400 HWMON_P_CRIT | HWMON_P_LCRIT | 1401 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM | 1402 HWMON_P_LABEL, 1403 /* Receive power */ 1404 HWMON_P_INPUT | 1405 HWMON_P_MAX | HWMON_P_MIN | 1406 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM | 1407 HWMON_P_CRIT | HWMON_P_LCRIT | 1408 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM | 1409 HWMON_P_LABEL), 1410 NULL, 1411 }; 1412 1413 static const struct hwmon_chip_info sfp_hwmon_chip_info = { 1414 .ops = &sfp_hwmon_ops, 1415 .info = sfp_hwmon_info, 1416 }; 1417 1418 static void sfp_hwmon_probe(struct work_struct *work) 1419 { 1420 struct sfp *sfp = container_of(work, struct sfp, hwmon_probe.work); 1421 int err; 1422 1423 /* hwmon interface needs to access 16bit registers in atomic way to 1424 * guarantee coherency of the diagnostic monitoring data. If it is not 1425 * possible to guarantee coherency because EEPROM is broken in such way 1426 * that does not support atomic 16bit read operation then we have to 1427 * skip registration of hwmon device. 1428 */ 1429 if (sfp->i2c_block_size < 2) { 1430 dev_info(sfp->dev, 1431 "skipping hwmon device registration due to broken EEPROM\n"); 1432 dev_info(sfp->dev, 1433 "diagnostic EEPROM area cannot be read atomically to guarantee data coherency\n"); 1434 return; 1435 } 1436 1437 err = sfp_read(sfp, true, 0, &sfp->diag, sizeof(sfp->diag)); 1438 if (err < 0) { 1439 if (sfp->hwmon_tries--) { 1440 mod_delayed_work(system_wq, &sfp->hwmon_probe, 1441 T_PROBE_RETRY_SLOW); 1442 } else { 1443 dev_warn(sfp->dev, "hwmon probe failed: %pe\n", 1444 ERR_PTR(err)); 1445 } 1446 return; 1447 } 1448 1449 sfp->hwmon_name = hwmon_sanitize_name(dev_name(sfp->dev)); 1450 if (IS_ERR(sfp->hwmon_name)) { 1451 dev_err(sfp->dev, "out of memory for hwmon name\n"); 1452 return; 1453 } 1454 1455 sfp->hwmon_dev = hwmon_device_register_with_info(sfp->dev, 1456 sfp->hwmon_name, sfp, 1457 &sfp_hwmon_chip_info, 1458 NULL); 1459 if (IS_ERR(sfp->hwmon_dev)) 1460 dev_err(sfp->dev, "failed to register hwmon device: %ld\n", 1461 PTR_ERR(sfp->hwmon_dev)); 1462 } 1463 1464 static int sfp_hwmon_insert(struct sfp *sfp) 1465 { 1466 if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE) 1467 return 0; 1468 1469 if (!(sfp->id.ext.diagmon & SFP_DIAGMON_DDM)) 1470 return 0; 1471 1472 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE) 1473 /* This driver in general does not support address 1474 * change. 1475 */ 1476 return 0; 1477 1478 mod_delayed_work(system_wq, &sfp->hwmon_probe, 1); 1479 sfp->hwmon_tries = R_PROBE_RETRY_SLOW; 1480 1481 return 0; 1482 } 1483 1484 static void sfp_hwmon_remove(struct sfp *sfp) 1485 { 1486 cancel_delayed_work_sync(&sfp->hwmon_probe); 1487 if (!IS_ERR_OR_NULL(sfp->hwmon_dev)) { 1488 hwmon_device_unregister(sfp->hwmon_dev); 1489 sfp->hwmon_dev = NULL; 1490 kfree(sfp->hwmon_name); 1491 } 1492 } 1493 1494 static int sfp_hwmon_init(struct sfp *sfp) 1495 { 1496 INIT_DELAYED_WORK(&sfp->hwmon_probe, sfp_hwmon_probe); 1497 1498 return 0; 1499 } 1500 1501 static void sfp_hwmon_exit(struct sfp *sfp) 1502 { 1503 cancel_delayed_work_sync(&sfp->hwmon_probe); 1504 } 1505 #else 1506 static int sfp_hwmon_insert(struct sfp *sfp) 1507 { 1508 return 0; 1509 } 1510 1511 static void sfp_hwmon_remove(struct sfp *sfp) 1512 { 1513 } 1514 1515 static int sfp_hwmon_init(struct sfp *sfp) 1516 { 1517 return 0; 1518 } 1519 1520 static void sfp_hwmon_exit(struct sfp *sfp) 1521 { 1522 } 1523 #endif 1524 1525 /* Helpers */ 1526 static void sfp_module_tx_disable(struct sfp *sfp) 1527 { 1528 dev_dbg(sfp->dev, "tx disable %u -> %u\n", 1529 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1); 1530 sfp->state |= SFP_F_TX_DISABLE; 1531 sfp_set_state(sfp, sfp->state); 1532 } 1533 1534 static void sfp_module_tx_enable(struct sfp *sfp) 1535 { 1536 dev_dbg(sfp->dev, "tx disable %u -> %u\n", 1537 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0); 1538 sfp->state &= ~SFP_F_TX_DISABLE; 1539 sfp_set_state(sfp, sfp->state); 1540 } 1541 1542 #if IS_ENABLED(CONFIG_DEBUG_FS) 1543 static int sfp_debug_state_show(struct seq_file *s, void *data) 1544 { 1545 struct sfp *sfp = s->private; 1546 1547 seq_printf(s, "Module state: %s\n", 1548 mod_state_to_str(sfp->sm_mod_state)); 1549 seq_printf(s, "Module probe attempts: %d %d\n", 1550 R_PROBE_RETRY_INIT - sfp->sm_mod_tries_init, 1551 R_PROBE_RETRY_SLOW - sfp->sm_mod_tries); 1552 seq_printf(s, "Device state: %s\n", 1553 dev_state_to_str(sfp->sm_dev_state)); 1554 seq_printf(s, "Main state: %s\n", 1555 sm_state_to_str(sfp->sm_state)); 1556 seq_printf(s, "Fault recovery remaining retries: %d\n", 1557 sfp->sm_fault_retries); 1558 seq_printf(s, "PHY probe remaining retries: %d\n", 1559 sfp->sm_phy_retries); 1560 seq_printf(s, "moddef0: %d\n", !!(sfp->state & SFP_F_PRESENT)); 1561 seq_printf(s, "rx_los: %d\n", !!(sfp->state & SFP_F_LOS)); 1562 seq_printf(s, "tx_fault: %d\n", !!(sfp->state & SFP_F_TX_FAULT)); 1563 seq_printf(s, "tx_disable: %d\n", !!(sfp->state & SFP_F_TX_DISABLE)); 1564 return 0; 1565 } 1566 DEFINE_SHOW_ATTRIBUTE(sfp_debug_state); 1567 1568 static void sfp_debugfs_init(struct sfp *sfp) 1569 { 1570 sfp->debugfs_dir = debugfs_create_dir(dev_name(sfp->dev), NULL); 1571 1572 debugfs_create_file("state", 0600, sfp->debugfs_dir, sfp, 1573 &sfp_debug_state_fops); 1574 } 1575 1576 static void sfp_debugfs_exit(struct sfp *sfp) 1577 { 1578 debugfs_remove_recursive(sfp->debugfs_dir); 1579 } 1580 #else 1581 static void sfp_debugfs_init(struct sfp *sfp) 1582 { 1583 } 1584 1585 static void sfp_debugfs_exit(struct sfp *sfp) 1586 { 1587 } 1588 #endif 1589 1590 static void sfp_module_tx_fault_reset(struct sfp *sfp) 1591 { 1592 unsigned int state = sfp->state; 1593 1594 if (state & SFP_F_TX_DISABLE) 1595 return; 1596 1597 sfp_set_state(sfp, state | SFP_F_TX_DISABLE); 1598 1599 udelay(T_RESET_US); 1600 1601 sfp_set_state(sfp, state); 1602 } 1603 1604 /* SFP state machine */ 1605 static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout) 1606 { 1607 if (timeout) 1608 mod_delayed_work(system_power_efficient_wq, &sfp->timeout, 1609 timeout); 1610 else 1611 cancel_delayed_work(&sfp->timeout); 1612 } 1613 1614 static void sfp_sm_next(struct sfp *sfp, unsigned int state, 1615 unsigned int timeout) 1616 { 1617 sfp->sm_state = state; 1618 sfp_sm_set_timer(sfp, timeout); 1619 } 1620 1621 static void sfp_sm_mod_next(struct sfp *sfp, unsigned int state, 1622 unsigned int timeout) 1623 { 1624 sfp->sm_mod_state = state; 1625 sfp_sm_set_timer(sfp, timeout); 1626 } 1627 1628 static void sfp_sm_phy_detach(struct sfp *sfp) 1629 { 1630 sfp_remove_phy(sfp->sfp_bus); 1631 phy_device_remove(sfp->mod_phy); 1632 phy_device_free(sfp->mod_phy); 1633 sfp->mod_phy = NULL; 1634 } 1635 1636 static int sfp_sm_probe_phy(struct sfp *sfp, int addr, bool is_c45) 1637 { 1638 struct phy_device *phy; 1639 int err; 1640 1641 phy = get_phy_device(sfp->i2c_mii, addr, is_c45); 1642 if (phy == ERR_PTR(-ENODEV)) 1643 return PTR_ERR(phy); 1644 if (IS_ERR(phy)) { 1645 dev_err(sfp->dev, "mdiobus scan returned %pe\n", phy); 1646 return PTR_ERR(phy); 1647 } 1648 1649 err = phy_device_register(phy); 1650 if (err) { 1651 phy_device_free(phy); 1652 dev_err(sfp->dev, "phy_device_register failed: %pe\n", 1653 ERR_PTR(err)); 1654 return err; 1655 } 1656 1657 err = sfp_add_phy(sfp->sfp_bus, phy); 1658 if (err) { 1659 phy_device_remove(phy); 1660 phy_device_free(phy); 1661 dev_err(sfp->dev, "sfp_add_phy failed: %pe\n", ERR_PTR(err)); 1662 return err; 1663 } 1664 1665 sfp->mod_phy = phy; 1666 1667 return 0; 1668 } 1669 1670 static void sfp_sm_link_up(struct sfp *sfp) 1671 { 1672 sfp_link_up(sfp->sfp_bus); 1673 sfp_sm_next(sfp, SFP_S_LINK_UP, 0); 1674 } 1675 1676 static void sfp_sm_link_down(struct sfp *sfp) 1677 { 1678 sfp_link_down(sfp->sfp_bus); 1679 } 1680 1681 static void sfp_sm_link_check_los(struct sfp *sfp) 1682 { 1683 const __be16 los_inverted = cpu_to_be16(SFP_OPTIONS_LOS_INVERTED); 1684 const __be16 los_normal = cpu_to_be16(SFP_OPTIONS_LOS_NORMAL); 1685 __be16 los_options = sfp->id.ext.options & (los_inverted | los_normal); 1686 bool los = false; 1687 1688 /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL 1689 * are set, we assume that no LOS signal is available. If both are 1690 * set, we assume LOS is not implemented (and is meaningless.) 1691 */ 1692 if (los_options == los_inverted) 1693 los = !(sfp->state & SFP_F_LOS); 1694 else if (los_options == los_normal) 1695 los = !!(sfp->state & SFP_F_LOS); 1696 1697 if (los) 1698 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0); 1699 else 1700 sfp_sm_link_up(sfp); 1701 } 1702 1703 static bool sfp_los_event_active(struct sfp *sfp, unsigned int event) 1704 { 1705 const __be16 los_inverted = cpu_to_be16(SFP_OPTIONS_LOS_INVERTED); 1706 const __be16 los_normal = cpu_to_be16(SFP_OPTIONS_LOS_NORMAL); 1707 __be16 los_options = sfp->id.ext.options & (los_inverted | los_normal); 1708 1709 return (los_options == los_inverted && event == SFP_E_LOS_LOW) || 1710 (los_options == los_normal && event == SFP_E_LOS_HIGH); 1711 } 1712 1713 static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event) 1714 { 1715 const __be16 los_inverted = cpu_to_be16(SFP_OPTIONS_LOS_INVERTED); 1716 const __be16 los_normal = cpu_to_be16(SFP_OPTIONS_LOS_NORMAL); 1717 __be16 los_options = sfp->id.ext.options & (los_inverted | los_normal); 1718 1719 return (los_options == los_inverted && event == SFP_E_LOS_HIGH) || 1720 (los_options == los_normal && event == SFP_E_LOS_LOW); 1721 } 1722 1723 static void sfp_sm_fault(struct sfp *sfp, unsigned int next_state, bool warn) 1724 { 1725 if (sfp->sm_fault_retries && !--sfp->sm_fault_retries) { 1726 dev_err(sfp->dev, 1727 "module persistently indicates fault, disabling\n"); 1728 sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0); 1729 } else { 1730 if (warn) 1731 dev_err(sfp->dev, "module transmit fault indicated\n"); 1732 1733 sfp_sm_next(sfp, next_state, T_FAULT_RECOVER); 1734 } 1735 } 1736 1737 static int sfp_sm_add_mdio_bus(struct sfp *sfp) 1738 { 1739 if (sfp->mdio_protocol != MDIO_I2C_NONE) 1740 return sfp_i2c_mdiobus_create(sfp); 1741 1742 return 0; 1743 } 1744 1745 /* Probe a SFP for a PHY device if the module supports copper - the PHY 1746 * normally sits at I2C bus address 0x56, and may either be a clause 22 1747 * or clause 45 PHY. 1748 * 1749 * Clause 22 copper SFP modules normally operate in Cisco SGMII mode with 1750 * negotiation enabled, but some may be in 1000base-X - which is for the 1751 * PHY driver to determine. 1752 * 1753 * Clause 45 copper SFP+ modules (10G) appear to switch their interface 1754 * mode according to the negotiated line speed. 1755 */ 1756 static int sfp_sm_probe_for_phy(struct sfp *sfp) 1757 { 1758 int err = 0; 1759 1760 switch (sfp->mdio_protocol) { 1761 case MDIO_I2C_NONE: 1762 break; 1763 1764 case MDIO_I2C_MARVELL_C22: 1765 err = sfp_sm_probe_phy(sfp, SFP_PHY_ADDR, false); 1766 break; 1767 1768 case MDIO_I2C_C45: 1769 err = sfp_sm_probe_phy(sfp, SFP_PHY_ADDR, true); 1770 break; 1771 1772 case MDIO_I2C_ROLLBALL: 1773 err = sfp_sm_probe_phy(sfp, SFP_PHY_ADDR_ROLLBALL, true); 1774 break; 1775 } 1776 1777 return err; 1778 } 1779 1780 static int sfp_module_parse_power(struct sfp *sfp) 1781 { 1782 u32 power_mW = 1000; 1783 bool supports_a2; 1784 1785 if (sfp->id.ext.sff8472_compliance >= SFP_SFF8472_COMPLIANCE_REV10_2 && 1786 sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL)) 1787 power_mW = 1500; 1788 /* Added in Rev 11.9, but there is no compliance code for this */ 1789 if (sfp->id.ext.sff8472_compliance >= SFP_SFF8472_COMPLIANCE_REV11_4 && 1790 sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL)) 1791 power_mW = 2000; 1792 1793 /* Power level 1 modules (max. 1W) are always supported. */ 1794 if (power_mW <= 1000) { 1795 sfp->module_power_mW = power_mW; 1796 return 0; 1797 } 1798 1799 supports_a2 = sfp->id.ext.sff8472_compliance != 1800 SFP_SFF8472_COMPLIANCE_NONE || 1801 sfp->id.ext.diagmon & SFP_DIAGMON_DDM; 1802 1803 if (power_mW > sfp->max_power_mW) { 1804 /* Module power specification exceeds the allowed maximum. */ 1805 if (!supports_a2) { 1806 /* The module appears not to implement bus address 1807 * 0xa2, so assume that the module powers up in the 1808 * indicated mode. 1809 */ 1810 dev_err(sfp->dev, 1811 "Host does not support %u.%uW modules\n", 1812 power_mW / 1000, (power_mW / 100) % 10); 1813 return -EINVAL; 1814 } else { 1815 dev_warn(sfp->dev, 1816 "Host does not support %u.%uW modules, module left in power mode 1\n", 1817 power_mW / 1000, (power_mW / 100) % 10); 1818 return 0; 1819 } 1820 } 1821 1822 if (!supports_a2) { 1823 /* The module power level is below the host maximum and the 1824 * module appears not to implement bus address 0xa2, so assume 1825 * that the module powers up in the indicated mode. 1826 */ 1827 return 0; 1828 } 1829 1830 /* If the module requires a higher power mode, but also requires 1831 * an address change sequence, warn the user that the module may 1832 * not be functional. 1833 */ 1834 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE) { 1835 dev_warn(sfp->dev, 1836 "Address Change Sequence not supported but module requires %u.%uW, module may not be functional\n", 1837 power_mW / 1000, (power_mW / 100) % 10); 1838 return 0; 1839 } 1840 1841 sfp->module_power_mW = power_mW; 1842 1843 return 0; 1844 } 1845 1846 static int sfp_sm_mod_hpower(struct sfp *sfp, bool enable) 1847 { 1848 int err; 1849 1850 err = sfp_modify_u8(sfp, true, SFP_EXT_STATUS, 1851 SFP_EXT_STATUS_PWRLVL_SELECT, 1852 enable ? SFP_EXT_STATUS_PWRLVL_SELECT : 0); 1853 if (err != sizeof(u8)) { 1854 dev_err(sfp->dev, "failed to %sable high power: %pe\n", 1855 enable ? "en" : "dis", ERR_PTR(err)); 1856 return -EAGAIN; 1857 } 1858 1859 if (enable) 1860 dev_info(sfp->dev, "Module switched to %u.%uW power level\n", 1861 sfp->module_power_mW / 1000, 1862 (sfp->module_power_mW / 100) % 10); 1863 1864 return 0; 1865 } 1866 1867 /* GPON modules based on Realtek RTL8672 and RTL9601C chips (e.g. V-SOL 1868 * V2801F, CarlitoxxPro CPGOS03-0490, Ubiquiti U-Fiber Instant, ...) do 1869 * not support multibyte reads from the EEPROM. Each multi-byte read 1870 * operation returns just one byte of EEPROM followed by zeros. There is 1871 * no way to identify which modules are using Realtek RTL8672 and RTL9601C 1872 * chips. Moreover every OEM of V-SOL V2801F module puts its own vendor 1873 * name and vendor id into EEPROM, so there is even no way to detect if 1874 * module is V-SOL V2801F. Therefore check for those zeros in the read 1875 * data and then based on check switch to reading EEPROM to one byte 1876 * at a time. 1877 */ 1878 static bool sfp_id_needs_byte_io(struct sfp *sfp, void *buf, size_t len) 1879 { 1880 size_t i, block_size = sfp->i2c_block_size; 1881 1882 /* Already using byte IO */ 1883 if (block_size == 1) 1884 return false; 1885 1886 for (i = 1; i < len; i += block_size) { 1887 if (memchr_inv(buf + i, '\0', min(block_size - 1, len - i))) 1888 return false; 1889 } 1890 return true; 1891 } 1892 1893 static int sfp_cotsworks_fixup_check(struct sfp *sfp, struct sfp_eeprom_id *id) 1894 { 1895 u8 check; 1896 int err; 1897 1898 if (id->base.phys_id != SFF8024_ID_SFF_8472 || 1899 id->base.phys_ext_id != SFP_PHYS_EXT_ID_SFP || 1900 id->base.connector != SFF8024_CONNECTOR_LC) { 1901 dev_warn(sfp->dev, "Rewriting fiber module EEPROM with corrected values\n"); 1902 id->base.phys_id = SFF8024_ID_SFF_8472; 1903 id->base.phys_ext_id = SFP_PHYS_EXT_ID_SFP; 1904 id->base.connector = SFF8024_CONNECTOR_LC; 1905 err = sfp_write(sfp, false, SFP_PHYS_ID, &id->base, 3); 1906 if (err != 3) { 1907 dev_err(sfp->dev, 1908 "Failed to rewrite module EEPROM: %pe\n", 1909 ERR_PTR(err)); 1910 return err; 1911 } 1912 1913 /* Cotsworks modules have been found to require a delay between write operations. */ 1914 mdelay(50); 1915 1916 /* Update base structure checksum */ 1917 check = sfp_check(&id->base, sizeof(id->base) - 1); 1918 err = sfp_write(sfp, false, SFP_CC_BASE, &check, 1); 1919 if (err != 1) { 1920 dev_err(sfp->dev, 1921 "Failed to update base structure checksum in fiber module EEPROM: %pe\n", 1922 ERR_PTR(err)); 1923 return err; 1924 } 1925 } 1926 return 0; 1927 } 1928 1929 static int sfp_sm_mod_probe(struct sfp *sfp, bool report) 1930 { 1931 /* SFP module inserted - read I2C data */ 1932 struct sfp_eeprom_id id; 1933 bool cotsworks_sfbg; 1934 bool cotsworks; 1935 u8 check; 1936 int ret; 1937 1938 sfp->i2c_block_size = SFP_EEPROM_BLOCK_SIZE; 1939 1940 ret = sfp_read(sfp, false, 0, &id.base, sizeof(id.base)); 1941 if (ret < 0) { 1942 if (report) 1943 dev_err(sfp->dev, "failed to read EEPROM: %pe\n", 1944 ERR_PTR(ret)); 1945 return -EAGAIN; 1946 } 1947 1948 if (ret != sizeof(id.base)) { 1949 dev_err(sfp->dev, "EEPROM short read: %pe\n", ERR_PTR(ret)); 1950 return -EAGAIN; 1951 } 1952 1953 /* Some SFP modules (e.g. Nokia 3FE46541AA) lock up if read from 1954 * address 0x51 is just one byte at a time. Also SFF-8472 requires 1955 * that EEPROM supports atomic 16bit read operation for diagnostic 1956 * fields, so do not switch to one byte reading at a time unless it 1957 * is really required and we have no other option. 1958 */ 1959 if (sfp_id_needs_byte_io(sfp, &id.base, sizeof(id.base))) { 1960 dev_info(sfp->dev, 1961 "Detected broken RTL8672/RTL9601C emulated EEPROM\n"); 1962 dev_info(sfp->dev, 1963 "Switching to reading EEPROM to one byte at a time\n"); 1964 sfp->i2c_block_size = 1; 1965 1966 ret = sfp_read(sfp, false, 0, &id.base, sizeof(id.base)); 1967 if (ret < 0) { 1968 if (report) 1969 dev_err(sfp->dev, 1970 "failed to read EEPROM: %pe\n", 1971 ERR_PTR(ret)); 1972 return -EAGAIN; 1973 } 1974 1975 if (ret != sizeof(id.base)) { 1976 dev_err(sfp->dev, "EEPROM short read: %pe\n", 1977 ERR_PTR(ret)); 1978 return -EAGAIN; 1979 } 1980 } 1981 1982 /* Cotsworks do not seem to update the checksums when they 1983 * do the final programming with the final module part number, 1984 * serial number and date code. 1985 */ 1986 cotsworks = !memcmp(id.base.vendor_name, "COTSWORKS ", 16); 1987 cotsworks_sfbg = !memcmp(id.base.vendor_pn, "SFBG", 4); 1988 1989 /* Cotsworks SFF module EEPROM do not always have valid phys_id, 1990 * phys_ext_id, and connector bytes. Rewrite SFF EEPROM bytes if 1991 * Cotsworks PN matches and bytes are not correct. 1992 */ 1993 if (cotsworks && cotsworks_sfbg) { 1994 ret = sfp_cotsworks_fixup_check(sfp, &id); 1995 if (ret < 0) 1996 return ret; 1997 } 1998 1999 /* Validate the checksum over the base structure */ 2000 check = sfp_check(&id.base, sizeof(id.base) - 1); 2001 if (check != id.base.cc_base) { 2002 if (cotsworks) { 2003 dev_warn(sfp->dev, 2004 "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n", 2005 check, id.base.cc_base); 2006 } else { 2007 dev_err(sfp->dev, 2008 "EEPROM base structure checksum failure: 0x%02x != 0x%02x\n", 2009 check, id.base.cc_base); 2010 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET, 2011 16, 1, &id, sizeof(id), true); 2012 return -EINVAL; 2013 } 2014 } 2015 2016 ret = sfp_read(sfp, false, SFP_CC_BASE + 1, &id.ext, sizeof(id.ext)); 2017 if (ret < 0) { 2018 if (report) 2019 dev_err(sfp->dev, "failed to read EEPROM: %pe\n", 2020 ERR_PTR(ret)); 2021 return -EAGAIN; 2022 } 2023 2024 if (ret != sizeof(id.ext)) { 2025 dev_err(sfp->dev, "EEPROM short read: %pe\n", ERR_PTR(ret)); 2026 return -EAGAIN; 2027 } 2028 2029 check = sfp_check(&id.ext, sizeof(id.ext) - 1); 2030 if (check != id.ext.cc_ext) { 2031 if (cotsworks) { 2032 dev_warn(sfp->dev, 2033 "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n", 2034 check, id.ext.cc_ext); 2035 } else { 2036 dev_err(sfp->dev, 2037 "EEPROM extended structure checksum failure: 0x%02x != 0x%02x\n", 2038 check, id.ext.cc_ext); 2039 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET, 2040 16, 1, &id, sizeof(id), true); 2041 memset(&id.ext, 0, sizeof(id.ext)); 2042 } 2043 } 2044 2045 sfp->id = id; 2046 2047 dev_info(sfp->dev, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n", 2048 (int)sizeof(id.base.vendor_name), id.base.vendor_name, 2049 (int)sizeof(id.base.vendor_pn), id.base.vendor_pn, 2050 (int)sizeof(id.base.vendor_rev), id.base.vendor_rev, 2051 (int)sizeof(id.ext.vendor_sn), id.ext.vendor_sn, 2052 (int)sizeof(id.ext.datecode), id.ext.datecode); 2053 2054 /* Check whether we support this module */ 2055 if (!sfp->type->module_supported(&id)) { 2056 dev_err(sfp->dev, 2057 "module is not supported - phys id 0x%02x 0x%02x\n", 2058 sfp->id.base.phys_id, sfp->id.base.phys_ext_id); 2059 return -EINVAL; 2060 } 2061 2062 /* If the module requires address swap mode, warn about it */ 2063 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE) 2064 dev_warn(sfp->dev, 2065 "module address swap to access page 0xA2 is not supported.\n"); 2066 2067 /* Parse the module power requirement */ 2068 ret = sfp_module_parse_power(sfp); 2069 if (ret < 0) 2070 return ret; 2071 2072 /* Initialise state bits to use from hardware */ 2073 sfp->state_hw_mask = SFP_F_PRESENT; 2074 if (sfp->gpio[GPIO_TX_DISABLE]) 2075 sfp->state_hw_mask |= SFP_F_TX_DISABLE; 2076 if (sfp->gpio[GPIO_TX_FAULT]) 2077 sfp->state_hw_mask |= SFP_F_TX_FAULT; 2078 if (sfp->gpio[GPIO_LOS]) 2079 sfp->state_hw_mask |= SFP_F_LOS; 2080 2081 sfp->module_t_start_up = T_START_UP; 2082 sfp->module_t_wait = T_WAIT; 2083 2084 sfp->tx_fault_ignore = false; 2085 2086 if (sfp->id.base.extended_cc == SFF8024_ECC_10GBASE_T_SFI || 2087 sfp->id.base.extended_cc == SFF8024_ECC_10GBASE_T_SR || 2088 sfp->id.base.extended_cc == SFF8024_ECC_5GBASE_T || 2089 sfp->id.base.extended_cc == SFF8024_ECC_2_5GBASE_T) 2090 sfp->mdio_protocol = MDIO_I2C_C45; 2091 else if (sfp->id.base.e1000_base_t) 2092 sfp->mdio_protocol = MDIO_I2C_MARVELL_C22; 2093 else 2094 sfp->mdio_protocol = MDIO_I2C_NONE; 2095 2096 sfp->quirk = sfp_lookup_quirk(&id); 2097 if (sfp->quirk && sfp->quirk->fixup) 2098 sfp->quirk->fixup(sfp); 2099 2100 return 0; 2101 } 2102 2103 static void sfp_sm_mod_remove(struct sfp *sfp) 2104 { 2105 if (sfp->sm_mod_state > SFP_MOD_WAITDEV) 2106 sfp_module_remove(sfp->sfp_bus); 2107 2108 sfp_hwmon_remove(sfp); 2109 2110 memset(&sfp->id, 0, sizeof(sfp->id)); 2111 sfp->module_power_mW = 0; 2112 2113 dev_info(sfp->dev, "module removed\n"); 2114 } 2115 2116 /* This state machine tracks the upstream's state */ 2117 static void sfp_sm_device(struct sfp *sfp, unsigned int event) 2118 { 2119 switch (sfp->sm_dev_state) { 2120 default: 2121 if (event == SFP_E_DEV_ATTACH) 2122 sfp->sm_dev_state = SFP_DEV_DOWN; 2123 break; 2124 2125 case SFP_DEV_DOWN: 2126 if (event == SFP_E_DEV_DETACH) 2127 sfp->sm_dev_state = SFP_DEV_DETACHED; 2128 else if (event == SFP_E_DEV_UP) 2129 sfp->sm_dev_state = SFP_DEV_UP; 2130 break; 2131 2132 case SFP_DEV_UP: 2133 if (event == SFP_E_DEV_DETACH) 2134 sfp->sm_dev_state = SFP_DEV_DETACHED; 2135 else if (event == SFP_E_DEV_DOWN) 2136 sfp->sm_dev_state = SFP_DEV_DOWN; 2137 break; 2138 } 2139 } 2140 2141 /* This state machine tracks the insert/remove state of the module, probes 2142 * the on-board EEPROM, and sets up the power level. 2143 */ 2144 static void sfp_sm_module(struct sfp *sfp, unsigned int event) 2145 { 2146 int err; 2147 2148 /* Handle remove event globally, it resets this state machine */ 2149 if (event == SFP_E_REMOVE) { 2150 if (sfp->sm_mod_state > SFP_MOD_PROBE) 2151 sfp_sm_mod_remove(sfp); 2152 sfp_sm_mod_next(sfp, SFP_MOD_EMPTY, 0); 2153 return; 2154 } 2155 2156 /* Handle device detach globally */ 2157 if (sfp->sm_dev_state < SFP_DEV_DOWN && 2158 sfp->sm_mod_state > SFP_MOD_WAITDEV) { 2159 if (sfp->module_power_mW > 1000 && 2160 sfp->sm_mod_state > SFP_MOD_HPOWER) 2161 sfp_sm_mod_hpower(sfp, false); 2162 sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0); 2163 return; 2164 } 2165 2166 switch (sfp->sm_mod_state) { 2167 default: 2168 if (event == SFP_E_INSERT) { 2169 sfp_sm_mod_next(sfp, SFP_MOD_PROBE, T_SERIAL); 2170 sfp->sm_mod_tries_init = R_PROBE_RETRY_INIT; 2171 sfp->sm_mod_tries = R_PROBE_RETRY_SLOW; 2172 } 2173 break; 2174 2175 case SFP_MOD_PROBE: 2176 /* Wait for T_PROBE_INIT to time out */ 2177 if (event != SFP_E_TIMEOUT) 2178 break; 2179 2180 err = sfp_sm_mod_probe(sfp, sfp->sm_mod_tries == 1); 2181 if (err == -EAGAIN) { 2182 if (sfp->sm_mod_tries_init && 2183 --sfp->sm_mod_tries_init) { 2184 sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT); 2185 break; 2186 } else if (sfp->sm_mod_tries && --sfp->sm_mod_tries) { 2187 if (sfp->sm_mod_tries == R_PROBE_RETRY_SLOW - 1) 2188 dev_warn(sfp->dev, 2189 "please wait, module slow to respond\n"); 2190 sfp_sm_set_timer(sfp, T_PROBE_RETRY_SLOW); 2191 break; 2192 } 2193 } 2194 if (err < 0) { 2195 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0); 2196 break; 2197 } 2198 2199 /* Force a poll to re-read the hardware signal state after 2200 * sfp_sm_mod_probe() changed state_hw_mask. 2201 */ 2202 mod_delayed_work(system_wq, &sfp->poll, 1); 2203 2204 err = sfp_hwmon_insert(sfp); 2205 if (err) 2206 dev_warn(sfp->dev, "hwmon probe failed: %pe\n", 2207 ERR_PTR(err)); 2208 2209 sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0); 2210 fallthrough; 2211 case SFP_MOD_WAITDEV: 2212 /* Ensure that the device is attached before proceeding */ 2213 if (sfp->sm_dev_state < SFP_DEV_DOWN) 2214 break; 2215 2216 /* Report the module insertion to the upstream device */ 2217 err = sfp_module_insert(sfp->sfp_bus, &sfp->id, 2218 sfp->quirk); 2219 if (err < 0) { 2220 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0); 2221 break; 2222 } 2223 2224 /* If this is a power level 1 module, we are done */ 2225 if (sfp->module_power_mW <= 1000) 2226 goto insert; 2227 2228 sfp_sm_mod_next(sfp, SFP_MOD_HPOWER, 0); 2229 fallthrough; 2230 case SFP_MOD_HPOWER: 2231 /* Enable high power mode */ 2232 err = sfp_sm_mod_hpower(sfp, true); 2233 if (err < 0) { 2234 if (err != -EAGAIN) { 2235 sfp_module_remove(sfp->sfp_bus); 2236 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0); 2237 } else { 2238 sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT); 2239 } 2240 break; 2241 } 2242 2243 sfp_sm_mod_next(sfp, SFP_MOD_WAITPWR, T_HPOWER_LEVEL); 2244 break; 2245 2246 case SFP_MOD_WAITPWR: 2247 /* Wait for T_HPOWER_LEVEL to time out */ 2248 if (event != SFP_E_TIMEOUT) 2249 break; 2250 2251 insert: 2252 sfp_sm_mod_next(sfp, SFP_MOD_PRESENT, 0); 2253 break; 2254 2255 case SFP_MOD_PRESENT: 2256 case SFP_MOD_ERROR: 2257 break; 2258 } 2259 } 2260 2261 static void sfp_sm_main(struct sfp *sfp, unsigned int event) 2262 { 2263 unsigned long timeout; 2264 int ret; 2265 2266 /* Some events are global */ 2267 if (sfp->sm_state != SFP_S_DOWN && 2268 (sfp->sm_mod_state != SFP_MOD_PRESENT || 2269 sfp->sm_dev_state != SFP_DEV_UP)) { 2270 if (sfp->sm_state == SFP_S_LINK_UP && 2271 sfp->sm_dev_state == SFP_DEV_UP) 2272 sfp_sm_link_down(sfp); 2273 if (sfp->sm_state > SFP_S_INIT) 2274 sfp_module_stop(sfp->sfp_bus); 2275 if (sfp->mod_phy) 2276 sfp_sm_phy_detach(sfp); 2277 if (sfp->i2c_mii) 2278 sfp_i2c_mdiobus_destroy(sfp); 2279 sfp_module_tx_disable(sfp); 2280 sfp_soft_stop_poll(sfp); 2281 sfp_sm_next(sfp, SFP_S_DOWN, 0); 2282 return; 2283 } 2284 2285 /* The main state machine */ 2286 switch (sfp->sm_state) { 2287 case SFP_S_DOWN: 2288 if (sfp->sm_mod_state != SFP_MOD_PRESENT || 2289 sfp->sm_dev_state != SFP_DEV_UP) 2290 break; 2291 2292 if (!(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) 2293 sfp_soft_start_poll(sfp); 2294 2295 sfp_module_tx_enable(sfp); 2296 2297 /* Initialise the fault clearance retries */ 2298 sfp->sm_fault_retries = N_FAULT_INIT; 2299 2300 /* We need to check the TX_FAULT state, which is not defined 2301 * while TX_DISABLE is asserted. The earliest we want to do 2302 * anything (such as probe for a PHY) is 50ms (or more on 2303 * specific modules). 2304 */ 2305 sfp_sm_next(sfp, SFP_S_WAIT, sfp->module_t_wait); 2306 break; 2307 2308 case SFP_S_WAIT: 2309 if (event != SFP_E_TIMEOUT) 2310 break; 2311 2312 if (sfp->state & SFP_F_TX_FAULT) { 2313 /* Wait up to t_init (SFF-8472) or t_start_up (SFF-8431) 2314 * from the TX_DISABLE deassertion for the module to 2315 * initialise, which is indicated by TX_FAULT 2316 * deasserting. 2317 */ 2318 timeout = sfp->module_t_start_up; 2319 if (timeout > sfp->module_t_wait) 2320 timeout -= sfp->module_t_wait; 2321 else 2322 timeout = 1; 2323 2324 sfp_sm_next(sfp, SFP_S_INIT, timeout); 2325 } else { 2326 /* TX_FAULT is not asserted, assume the module has 2327 * finished initialising. 2328 */ 2329 goto init_done; 2330 } 2331 break; 2332 2333 case SFP_S_INIT: 2334 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) { 2335 /* TX_FAULT is still asserted after t_init 2336 * or t_start_up, so assume there is a fault. 2337 */ 2338 sfp_sm_fault(sfp, SFP_S_INIT_TX_FAULT, 2339 sfp->sm_fault_retries == N_FAULT_INIT); 2340 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) { 2341 init_done: 2342 /* Create mdiobus and start trying for PHY */ 2343 ret = sfp_sm_add_mdio_bus(sfp); 2344 if (ret < 0) { 2345 sfp_sm_next(sfp, SFP_S_FAIL, 0); 2346 break; 2347 } 2348 sfp->sm_phy_retries = R_PHY_RETRY; 2349 goto phy_probe; 2350 } 2351 break; 2352 2353 case SFP_S_INIT_PHY: 2354 if (event != SFP_E_TIMEOUT) 2355 break; 2356 phy_probe: 2357 /* TX_FAULT deasserted or we timed out with TX_FAULT 2358 * clear. Probe for the PHY and check the LOS state. 2359 */ 2360 ret = sfp_sm_probe_for_phy(sfp); 2361 if (ret == -ENODEV) { 2362 if (--sfp->sm_phy_retries) { 2363 sfp_sm_next(sfp, SFP_S_INIT_PHY, T_PHY_RETRY); 2364 break; 2365 } else { 2366 dev_info(sfp->dev, "no PHY detected\n"); 2367 } 2368 } else if (ret) { 2369 sfp_sm_next(sfp, SFP_S_FAIL, 0); 2370 break; 2371 } 2372 if (sfp_module_start(sfp->sfp_bus)) { 2373 sfp_sm_next(sfp, SFP_S_FAIL, 0); 2374 break; 2375 } 2376 sfp_sm_link_check_los(sfp); 2377 2378 /* Reset the fault retry count */ 2379 sfp->sm_fault_retries = N_FAULT; 2380 break; 2381 2382 case SFP_S_INIT_TX_FAULT: 2383 if (event == SFP_E_TIMEOUT) { 2384 sfp_module_tx_fault_reset(sfp); 2385 sfp_sm_next(sfp, SFP_S_INIT, sfp->module_t_start_up); 2386 } 2387 break; 2388 2389 case SFP_S_WAIT_LOS: 2390 if (event == SFP_E_TX_FAULT) 2391 sfp_sm_fault(sfp, SFP_S_TX_FAULT, true); 2392 else if (sfp_los_event_inactive(sfp, event)) 2393 sfp_sm_link_up(sfp); 2394 break; 2395 2396 case SFP_S_LINK_UP: 2397 if (event == SFP_E_TX_FAULT) { 2398 sfp_sm_link_down(sfp); 2399 sfp_sm_fault(sfp, SFP_S_TX_FAULT, true); 2400 } else if (sfp_los_event_active(sfp, event)) { 2401 sfp_sm_link_down(sfp); 2402 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0); 2403 } 2404 break; 2405 2406 case SFP_S_TX_FAULT: 2407 if (event == SFP_E_TIMEOUT) { 2408 sfp_module_tx_fault_reset(sfp); 2409 sfp_sm_next(sfp, SFP_S_REINIT, sfp->module_t_start_up); 2410 } 2411 break; 2412 2413 case SFP_S_REINIT: 2414 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) { 2415 sfp_sm_fault(sfp, SFP_S_TX_FAULT, false); 2416 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) { 2417 dev_info(sfp->dev, "module transmit fault recovered\n"); 2418 sfp_sm_link_check_los(sfp); 2419 } 2420 break; 2421 2422 case SFP_S_TX_DISABLE: 2423 break; 2424 } 2425 } 2426 2427 static void sfp_sm_event(struct sfp *sfp, unsigned int event) 2428 { 2429 mutex_lock(&sfp->sm_mutex); 2430 2431 dev_dbg(sfp->dev, "SM: enter %s:%s:%s event %s\n", 2432 mod_state_to_str(sfp->sm_mod_state), 2433 dev_state_to_str(sfp->sm_dev_state), 2434 sm_state_to_str(sfp->sm_state), 2435 event_to_str(event)); 2436 2437 sfp_sm_device(sfp, event); 2438 sfp_sm_module(sfp, event); 2439 sfp_sm_main(sfp, event); 2440 2441 dev_dbg(sfp->dev, "SM: exit %s:%s:%s\n", 2442 mod_state_to_str(sfp->sm_mod_state), 2443 dev_state_to_str(sfp->sm_dev_state), 2444 sm_state_to_str(sfp->sm_state)); 2445 2446 mutex_unlock(&sfp->sm_mutex); 2447 } 2448 2449 static void sfp_attach(struct sfp *sfp) 2450 { 2451 sfp_sm_event(sfp, SFP_E_DEV_ATTACH); 2452 } 2453 2454 static void sfp_detach(struct sfp *sfp) 2455 { 2456 sfp_sm_event(sfp, SFP_E_DEV_DETACH); 2457 } 2458 2459 static void sfp_start(struct sfp *sfp) 2460 { 2461 sfp_sm_event(sfp, SFP_E_DEV_UP); 2462 } 2463 2464 static void sfp_stop(struct sfp *sfp) 2465 { 2466 sfp_sm_event(sfp, SFP_E_DEV_DOWN); 2467 } 2468 2469 static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo) 2470 { 2471 /* locking... and check module is present */ 2472 2473 if (sfp->id.ext.sff8472_compliance && 2474 !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) { 2475 modinfo->type = ETH_MODULE_SFF_8472; 2476 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN; 2477 } else { 2478 modinfo->type = ETH_MODULE_SFF_8079; 2479 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN; 2480 } 2481 return 0; 2482 } 2483 2484 static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee, 2485 u8 *data) 2486 { 2487 unsigned int first, last, len; 2488 int ret; 2489 2490 if (!(sfp->state & SFP_F_PRESENT)) 2491 return -ENODEV; 2492 2493 if (ee->len == 0) 2494 return -EINVAL; 2495 2496 first = ee->offset; 2497 last = ee->offset + ee->len; 2498 if (first < ETH_MODULE_SFF_8079_LEN) { 2499 len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN); 2500 len -= first; 2501 2502 ret = sfp_read(sfp, false, first, data, len); 2503 if (ret < 0) 2504 return ret; 2505 2506 first += len; 2507 data += len; 2508 } 2509 if (first < ETH_MODULE_SFF_8472_LEN && last > ETH_MODULE_SFF_8079_LEN) { 2510 len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN); 2511 len -= first; 2512 first -= ETH_MODULE_SFF_8079_LEN; 2513 2514 ret = sfp_read(sfp, true, first, data, len); 2515 if (ret < 0) 2516 return ret; 2517 } 2518 return 0; 2519 } 2520 2521 static int sfp_module_eeprom_by_page(struct sfp *sfp, 2522 const struct ethtool_module_eeprom *page, 2523 struct netlink_ext_ack *extack) 2524 { 2525 if (!(sfp->state & SFP_F_PRESENT)) 2526 return -ENODEV; 2527 2528 if (page->bank) { 2529 NL_SET_ERR_MSG(extack, "Banks not supported"); 2530 return -EOPNOTSUPP; 2531 } 2532 2533 if (page->page) { 2534 NL_SET_ERR_MSG(extack, "Only page 0 supported"); 2535 return -EOPNOTSUPP; 2536 } 2537 2538 if (page->i2c_address != 0x50 && 2539 page->i2c_address != 0x51) { 2540 NL_SET_ERR_MSG(extack, "Only address 0x50 and 0x51 supported"); 2541 return -EOPNOTSUPP; 2542 } 2543 2544 return sfp_read(sfp, page->i2c_address == 0x51, page->offset, 2545 page->data, page->length); 2546 }; 2547 2548 static const struct sfp_socket_ops sfp_module_ops = { 2549 .attach = sfp_attach, 2550 .detach = sfp_detach, 2551 .start = sfp_start, 2552 .stop = sfp_stop, 2553 .module_info = sfp_module_info, 2554 .module_eeprom = sfp_module_eeprom, 2555 .module_eeprom_by_page = sfp_module_eeprom_by_page, 2556 }; 2557 2558 static void sfp_timeout(struct work_struct *work) 2559 { 2560 struct sfp *sfp = container_of(work, struct sfp, timeout.work); 2561 2562 rtnl_lock(); 2563 sfp_sm_event(sfp, SFP_E_TIMEOUT); 2564 rtnl_unlock(); 2565 } 2566 2567 static void sfp_check_state(struct sfp *sfp) 2568 { 2569 unsigned int state, i, changed; 2570 2571 mutex_lock(&sfp->st_mutex); 2572 state = sfp_get_state(sfp); 2573 changed = state ^ sfp->state; 2574 if (sfp->tx_fault_ignore) 2575 changed &= SFP_F_PRESENT | SFP_F_LOS; 2576 else 2577 changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT; 2578 2579 for (i = 0; i < GPIO_MAX; i++) 2580 if (changed & BIT(i)) 2581 dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_names[i], 2582 !!(sfp->state & BIT(i)), !!(state & BIT(i))); 2583 2584 state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT); 2585 sfp->state = state; 2586 2587 rtnl_lock(); 2588 if (changed & SFP_F_PRESENT) 2589 sfp_sm_event(sfp, state & SFP_F_PRESENT ? 2590 SFP_E_INSERT : SFP_E_REMOVE); 2591 2592 if (changed & SFP_F_TX_FAULT) 2593 sfp_sm_event(sfp, state & SFP_F_TX_FAULT ? 2594 SFP_E_TX_FAULT : SFP_E_TX_CLEAR); 2595 2596 if (changed & SFP_F_LOS) 2597 sfp_sm_event(sfp, state & SFP_F_LOS ? 2598 SFP_E_LOS_HIGH : SFP_E_LOS_LOW); 2599 rtnl_unlock(); 2600 mutex_unlock(&sfp->st_mutex); 2601 } 2602 2603 static irqreturn_t sfp_irq(int irq, void *data) 2604 { 2605 struct sfp *sfp = data; 2606 2607 sfp_check_state(sfp); 2608 2609 return IRQ_HANDLED; 2610 } 2611 2612 static void sfp_poll(struct work_struct *work) 2613 { 2614 struct sfp *sfp = container_of(work, struct sfp, poll.work); 2615 2616 sfp_check_state(sfp); 2617 2618 if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) || 2619 sfp->need_poll) 2620 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies); 2621 } 2622 2623 static struct sfp *sfp_alloc(struct device *dev) 2624 { 2625 struct sfp *sfp; 2626 2627 sfp = kzalloc(sizeof(*sfp), GFP_KERNEL); 2628 if (!sfp) 2629 return ERR_PTR(-ENOMEM); 2630 2631 sfp->dev = dev; 2632 sfp->i2c_block_size = SFP_EEPROM_BLOCK_SIZE; 2633 2634 mutex_init(&sfp->sm_mutex); 2635 mutex_init(&sfp->st_mutex); 2636 INIT_DELAYED_WORK(&sfp->poll, sfp_poll); 2637 INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout); 2638 2639 sfp_hwmon_init(sfp); 2640 2641 return sfp; 2642 } 2643 2644 static void sfp_cleanup(void *data) 2645 { 2646 struct sfp *sfp = data; 2647 2648 sfp_hwmon_exit(sfp); 2649 2650 cancel_delayed_work_sync(&sfp->poll); 2651 cancel_delayed_work_sync(&sfp->timeout); 2652 if (sfp->i2c_mii) { 2653 mdiobus_unregister(sfp->i2c_mii); 2654 mdiobus_free(sfp->i2c_mii); 2655 } 2656 if (sfp->i2c) 2657 i2c_put_adapter(sfp->i2c); 2658 kfree(sfp); 2659 } 2660 2661 static int sfp_i2c_get(struct sfp *sfp) 2662 { 2663 struct fwnode_handle *h; 2664 struct i2c_adapter *i2c; 2665 int err; 2666 2667 h = fwnode_find_reference(dev_fwnode(sfp->dev), "i2c-bus", 0); 2668 if (IS_ERR(h)) { 2669 dev_err(sfp->dev, "missing 'i2c-bus' property\n"); 2670 return -ENODEV; 2671 } 2672 2673 i2c = i2c_get_adapter_by_fwnode(h); 2674 if (!i2c) { 2675 err = -EPROBE_DEFER; 2676 goto put; 2677 } 2678 2679 err = sfp_i2c_configure(sfp, i2c); 2680 if (err) 2681 i2c_put_adapter(i2c); 2682 put: 2683 fwnode_handle_put(h); 2684 return err; 2685 } 2686 2687 static int sfp_probe(struct platform_device *pdev) 2688 { 2689 const struct sff_data *sff; 2690 char *sfp_irq_name; 2691 struct sfp *sfp; 2692 int err, i; 2693 2694 sfp = sfp_alloc(&pdev->dev); 2695 if (IS_ERR(sfp)) 2696 return PTR_ERR(sfp); 2697 2698 platform_set_drvdata(pdev, sfp); 2699 2700 err = devm_add_action_or_reset(sfp->dev, sfp_cleanup, sfp); 2701 if (err < 0) 2702 return err; 2703 2704 sff = device_get_match_data(sfp->dev); 2705 if (!sff) 2706 sff = &sfp_data; 2707 2708 sfp->type = sff; 2709 2710 err = sfp_i2c_get(sfp); 2711 if (err) 2712 return err; 2713 2714 for (i = 0; i < GPIO_MAX; i++) 2715 if (sff->gpios & BIT(i)) { 2716 sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev, 2717 gpio_names[i], gpio_flags[i]); 2718 if (IS_ERR(sfp->gpio[i])) 2719 return PTR_ERR(sfp->gpio[i]); 2720 } 2721 2722 sfp->state_hw_mask = SFP_F_PRESENT; 2723 2724 sfp->get_state = sfp_gpio_get_state; 2725 sfp->set_state = sfp_gpio_set_state; 2726 2727 /* Modules that have no detect signal are always present */ 2728 if (!(sfp->gpio[GPIO_MODDEF0])) 2729 sfp->get_state = sff_gpio_get_state; 2730 2731 device_property_read_u32(&pdev->dev, "maximum-power-milliwatt", 2732 &sfp->max_power_mW); 2733 if (sfp->max_power_mW < 1000) { 2734 if (sfp->max_power_mW) 2735 dev_warn(sfp->dev, 2736 "Firmware bug: host maximum power should be at least 1W\n"); 2737 sfp->max_power_mW = 1000; 2738 } 2739 2740 dev_info(sfp->dev, "Host maximum power %u.%uW\n", 2741 sfp->max_power_mW / 1000, (sfp->max_power_mW / 100) % 10); 2742 2743 /* Get the initial state, and always signal TX disable, 2744 * since the network interface will not be up. 2745 */ 2746 sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE; 2747 2748 if (sfp->gpio[GPIO_RATE_SELECT] && 2749 gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT])) 2750 sfp->state |= SFP_F_RATE_SELECT; 2751 sfp_set_state(sfp, sfp->state); 2752 sfp_module_tx_disable(sfp); 2753 if (sfp->state & SFP_F_PRESENT) { 2754 rtnl_lock(); 2755 sfp_sm_event(sfp, SFP_E_INSERT); 2756 rtnl_unlock(); 2757 } 2758 2759 for (i = 0; i < GPIO_MAX; i++) { 2760 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i]) 2761 continue; 2762 2763 sfp->gpio_irq[i] = gpiod_to_irq(sfp->gpio[i]); 2764 if (sfp->gpio_irq[i] < 0) { 2765 sfp->gpio_irq[i] = 0; 2766 sfp->need_poll = true; 2767 continue; 2768 } 2769 2770 sfp_irq_name = devm_kasprintf(sfp->dev, GFP_KERNEL, 2771 "%s-%s", dev_name(sfp->dev), 2772 gpio_names[i]); 2773 2774 if (!sfp_irq_name) 2775 return -ENOMEM; 2776 2777 err = devm_request_threaded_irq(sfp->dev, sfp->gpio_irq[i], 2778 NULL, sfp_irq, 2779 IRQF_ONESHOT | 2780 IRQF_TRIGGER_RISING | 2781 IRQF_TRIGGER_FALLING, 2782 sfp_irq_name, sfp); 2783 if (err) { 2784 sfp->gpio_irq[i] = 0; 2785 sfp->need_poll = true; 2786 } 2787 } 2788 2789 if (sfp->need_poll) 2790 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies); 2791 2792 /* We could have an issue in cases no Tx disable pin is available or 2793 * wired as modules using a laser as their light source will continue to 2794 * be active when the fiber is removed. This could be a safety issue and 2795 * we should at least warn the user about that. 2796 */ 2797 if (!sfp->gpio[GPIO_TX_DISABLE]) 2798 dev_warn(sfp->dev, 2799 "No tx_disable pin: SFP modules will always be emitting.\n"); 2800 2801 sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops); 2802 if (!sfp->sfp_bus) 2803 return -ENOMEM; 2804 2805 sfp_debugfs_init(sfp); 2806 2807 return 0; 2808 } 2809 2810 static int sfp_remove(struct platform_device *pdev) 2811 { 2812 struct sfp *sfp = platform_get_drvdata(pdev); 2813 2814 sfp_debugfs_exit(sfp); 2815 sfp_unregister_socket(sfp->sfp_bus); 2816 2817 rtnl_lock(); 2818 sfp_sm_event(sfp, SFP_E_REMOVE); 2819 rtnl_unlock(); 2820 2821 return 0; 2822 } 2823 2824 static void sfp_shutdown(struct platform_device *pdev) 2825 { 2826 struct sfp *sfp = platform_get_drvdata(pdev); 2827 int i; 2828 2829 for (i = 0; i < GPIO_MAX; i++) { 2830 if (!sfp->gpio_irq[i]) 2831 continue; 2832 2833 devm_free_irq(sfp->dev, sfp->gpio_irq[i], sfp); 2834 } 2835 2836 cancel_delayed_work_sync(&sfp->poll); 2837 cancel_delayed_work_sync(&sfp->timeout); 2838 } 2839 2840 static struct platform_driver sfp_driver = { 2841 .probe = sfp_probe, 2842 .remove = sfp_remove, 2843 .shutdown = sfp_shutdown, 2844 .driver = { 2845 .name = "sfp", 2846 .of_match_table = sfp_of_match, 2847 }, 2848 }; 2849 2850 static int sfp_init(void) 2851 { 2852 poll_jiffies = msecs_to_jiffies(100); 2853 2854 return platform_driver_register(&sfp_driver); 2855 } 2856 module_init(sfp_init); 2857 2858 static void sfp_exit(void) 2859 { 2860 platform_driver_unregister(&sfp_driver); 2861 } 2862 module_exit(sfp_exit); 2863 2864 MODULE_ALIAS("platform:sfp"); 2865 MODULE_AUTHOR("Russell King"); 2866 MODULE_LICENSE("GPL v2"); 2867