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