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/unaligned.h> 18 #include <linux/workqueue.h> 19 20 #include "sfp.h" 21 22 enum { 23 GPIO_MODDEF0, 24 GPIO_LOS, 25 GPIO_TX_FAULT, 26 GPIO_TX_DISABLE, 27 GPIO_RS0, 28 GPIO_RS1, 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_RS0 = BIT(GPIO_RS0), 36 SFP_F_RS1 = BIT(GPIO_RS1), 37 38 SFP_F_OUTPUTS = SFP_F_TX_DISABLE | SFP_F_RS0 | SFP_F_RS1, 39 40 SFP_E_INSERT = 0, 41 SFP_E_REMOVE, 42 SFP_E_DEV_ATTACH, 43 SFP_E_DEV_DETACH, 44 SFP_E_DEV_DOWN, 45 SFP_E_DEV_UP, 46 SFP_E_TX_FAULT, 47 SFP_E_TX_CLEAR, 48 SFP_E_LOS_HIGH, 49 SFP_E_LOS_LOW, 50 SFP_E_TIMEOUT, 51 52 SFP_MOD_EMPTY = 0, 53 SFP_MOD_ERROR, 54 SFP_MOD_PROBE, 55 SFP_MOD_WAITDEV, 56 SFP_MOD_HPOWER, 57 SFP_MOD_WAITPWR, 58 SFP_MOD_PRESENT, 59 60 SFP_DEV_DETACHED = 0, 61 SFP_DEV_DOWN, 62 SFP_DEV_UP, 63 64 SFP_S_DOWN = 0, 65 SFP_S_FAIL, 66 SFP_S_WAIT, 67 SFP_S_INIT, 68 SFP_S_INIT_PHY, 69 SFP_S_INIT_TX_FAULT, 70 SFP_S_WAIT_LOS, 71 SFP_S_LINK_UP, 72 SFP_S_TX_FAULT, 73 SFP_S_REINIT, 74 SFP_S_TX_DISABLE, 75 }; 76 77 static const char * const mod_state_strings[] = { 78 [SFP_MOD_EMPTY] = "empty", 79 [SFP_MOD_ERROR] = "error", 80 [SFP_MOD_PROBE] = "probe", 81 [SFP_MOD_WAITDEV] = "waitdev", 82 [SFP_MOD_HPOWER] = "hpower", 83 [SFP_MOD_WAITPWR] = "waitpwr", 84 [SFP_MOD_PRESENT] = "present", 85 }; 86 87 static const char *mod_state_to_str(unsigned short mod_state) 88 { 89 if (mod_state >= ARRAY_SIZE(mod_state_strings)) 90 return "Unknown module state"; 91 return mod_state_strings[mod_state]; 92 } 93 94 static const char * const dev_state_strings[] = { 95 [SFP_DEV_DETACHED] = "detached", 96 [SFP_DEV_DOWN] = "down", 97 [SFP_DEV_UP] = "up", 98 }; 99 100 static const char *dev_state_to_str(unsigned short dev_state) 101 { 102 if (dev_state >= ARRAY_SIZE(dev_state_strings)) 103 return "Unknown device state"; 104 return dev_state_strings[dev_state]; 105 } 106 107 static const char * const event_strings[] = { 108 [SFP_E_INSERT] = "insert", 109 [SFP_E_REMOVE] = "remove", 110 [SFP_E_DEV_ATTACH] = "dev_attach", 111 [SFP_E_DEV_DETACH] = "dev_detach", 112 [SFP_E_DEV_DOWN] = "dev_down", 113 [SFP_E_DEV_UP] = "dev_up", 114 [SFP_E_TX_FAULT] = "tx_fault", 115 [SFP_E_TX_CLEAR] = "tx_clear", 116 [SFP_E_LOS_HIGH] = "los_high", 117 [SFP_E_LOS_LOW] = "los_low", 118 [SFP_E_TIMEOUT] = "timeout", 119 }; 120 121 static const char *event_to_str(unsigned short event) 122 { 123 if (event >= ARRAY_SIZE(event_strings)) 124 return "Unknown event"; 125 return event_strings[event]; 126 } 127 128 static const char * const sm_state_strings[] = { 129 [SFP_S_DOWN] = "down", 130 [SFP_S_FAIL] = "fail", 131 [SFP_S_WAIT] = "wait", 132 [SFP_S_INIT] = "init", 133 [SFP_S_INIT_PHY] = "init_phy", 134 [SFP_S_INIT_TX_FAULT] = "init_tx_fault", 135 [SFP_S_WAIT_LOS] = "wait_los", 136 [SFP_S_LINK_UP] = "link_up", 137 [SFP_S_TX_FAULT] = "tx_fault", 138 [SFP_S_REINIT] = "reinit", 139 [SFP_S_TX_DISABLE] = "tx_disable", 140 }; 141 142 static const char *sm_state_to_str(unsigned short sm_state) 143 { 144 if (sm_state >= ARRAY_SIZE(sm_state_strings)) 145 return "Unknown state"; 146 return sm_state_strings[sm_state]; 147 } 148 149 static const char *gpio_names[] = { 150 "mod-def0", 151 "los", 152 "tx-fault", 153 "tx-disable", 154 "rate-select0", 155 "rate-select1", 156 }; 157 158 static const enum gpiod_flags gpio_flags[] = { 159 GPIOD_IN, 160 GPIOD_IN, 161 GPIOD_IN, 162 GPIOD_ASIS, 163 GPIOD_ASIS, 164 GPIOD_ASIS, 165 }; 166 167 /* t_start_up (SFF-8431) or t_init (SFF-8472) is the time required for a 168 * non-cooled module to initialise its laser safety circuitry. We wait 169 * an initial T_WAIT period before we check the tx fault to give any PHY 170 * on board (for a copper SFP) time to initialise. 171 */ 172 #define T_WAIT msecs_to_jiffies(50) 173 #define T_START_UP msecs_to_jiffies(300) 174 #define T_START_UP_BAD_GPON msecs_to_jiffies(60000) 175 176 /* t_reset is the time required to assert the TX_DISABLE signal to reset 177 * an indicated TX_FAULT. 178 */ 179 #define T_RESET_US 10 180 #define T_FAULT_RECOVER msecs_to_jiffies(1000) 181 182 /* N_FAULT_INIT is the number of recovery attempts at module initialisation 183 * time. If the TX_FAULT signal is not deasserted after this number of 184 * attempts at clearing it, we decide that the module is faulty. 185 * N_FAULT is the same but after the module has initialised. 186 */ 187 #define N_FAULT_INIT 5 188 #define N_FAULT 5 189 190 /* T_PHY_RETRY is the time interval between attempts to probe the PHY. 191 * R_PHY_RETRY is the number of attempts. 192 */ 193 #define T_PHY_RETRY msecs_to_jiffies(50) 194 #define R_PHY_RETRY 25 195 196 /* SFP module presence detection is poor: the three MOD DEF signals are 197 * the same length on the PCB, which means it's possible for MOD DEF 0 to 198 * connect before the I2C bus on MOD DEF 1/2. 199 * 200 * The SFF-8472 specifies t_serial ("Time from power on until module is 201 * ready for data transmission over the two wire serial bus.") as 300ms. 202 */ 203 #define T_SERIAL msecs_to_jiffies(300) 204 #define T_HPOWER_LEVEL msecs_to_jiffies(300) 205 #define T_PROBE_RETRY_INIT msecs_to_jiffies(100) 206 #define R_PROBE_RETRY_INIT 10 207 #define T_PROBE_RETRY_SLOW msecs_to_jiffies(5000) 208 #define R_PROBE_RETRY_SLOW 12 209 210 /* Polling interval and consecutive-failure threshold for the I2C presence 211 * probe used on boards without a MOD_DEF0 GPIO (see sfp_i2c_get_state()). 212 * A single successful read asserts presence immediately; R_PROBE_ABSENT 213 * consecutive failures are required to declare a live module removed, to ride 214 * out a transient I2C error. Insertion is thus detected within 215 * T_PROBE_PRESENT and removal within T_PROBE_PRESENT * R_PROBE_ABSENT. 216 */ 217 #define T_PROBE_PRESENT msecs_to_jiffies(500) 218 #define R_PROBE_ABSENT 3 219 220 /* SFP modules appear to always have their PHY configured for bus address 221 * 0x56 (which with mdio-i2c, translates to a PHY address of 22). 222 * RollBall SFPs access phy via SFP Enhanced Digital Diagnostic Interface 223 * via address 0x51 (mdio-i2c will use RollBall protocol on this address). 224 */ 225 #define SFP_PHY_ADDR 22 226 #define SFP_PHY_ADDR_ROLLBALL 17 227 228 /* SFP_EEPROM_BLOCK_SIZE is the size of data chunk to read the EEPROM 229 * at a time. Some SFP modules and also some Linux I2C drivers do not like 230 * reads longer than 16 bytes. 231 */ 232 #define SFP_EEPROM_BLOCK_SIZE 16 233 234 #define SFP_POLL_INTERVAL msecs_to_jiffies(100) 235 236 struct sff_data { 237 unsigned int gpios; 238 bool (*module_supported)(const struct sfp_eeprom_id *id); 239 }; 240 241 struct sfp { 242 struct device *dev; 243 struct i2c_adapter *i2c; 244 struct mii_bus *i2c_mii; 245 struct sfp_bus *sfp_bus; 246 enum mdio_i2c_proto mdio_protocol; 247 struct phy_device *mod_phy; 248 const struct sff_data *type; 249 size_t i2c_max_block_size; 250 size_t i2c_block_size; 251 u32 max_power_mW; 252 253 unsigned int (*get_state)(struct sfp *); 254 void (*set_state)(struct sfp *, unsigned int); 255 int (*read)(struct sfp *, bool, u8, void *, size_t); 256 int (*write)(struct sfp *, bool, u8, void *, size_t); 257 258 struct gpio_desc *gpio[GPIO_MAX]; 259 int gpio_irq[GPIO_MAX]; 260 261 bool need_poll; 262 263 /* I2C-probed presence, for boards without a MOD_DEF0 GPIO. 264 * Access rules: st_mutex held (updated from the poll/state machine). 265 */ 266 bool i2c_present; 267 u8 i2c_present_nak; 268 unsigned long i2c_present_next; 269 270 /* Access rules: 271 * state_hw_drive: st_mutex held 272 * state_hw_mask: st_mutex held 273 * state_soft_mask: st_mutex held 274 * state: st_mutex held unless reading input bits 275 */ 276 struct mutex st_mutex; /* Protects state */ 277 unsigned int state_hw_drive; 278 unsigned int state_hw_mask; 279 unsigned int state_soft_mask; 280 unsigned int state_ignore_mask; 281 unsigned int state; 282 283 struct delayed_work poll; 284 struct delayed_work timeout; 285 struct mutex sm_mutex; /* Protects state machine */ 286 unsigned char sm_mod_state; 287 unsigned char sm_mod_tries_init; 288 unsigned char sm_mod_tries; 289 unsigned char sm_dev_state; 290 unsigned short sm_state; 291 unsigned char sm_fault_retries; 292 unsigned char sm_phy_retries; 293 294 struct sfp_eeprom_id id; 295 unsigned int module_power_mW; 296 unsigned int module_t_start_up; 297 unsigned int module_t_wait; 298 unsigned int phy_t_retry; 299 300 unsigned int rate_kbd; 301 unsigned int rs_threshold_kbd; 302 unsigned int rs_state_mask; 303 304 bool have_a2; 305 306 const struct sfp_quirk *quirk; 307 308 #if IS_ENABLED(CONFIG_HWMON) 309 struct sfp_diag diag; 310 struct delayed_work hwmon_probe; 311 unsigned int hwmon_tries; 312 struct device *hwmon_dev; 313 char *hwmon_name; 314 #endif 315 316 #if IS_ENABLED(CONFIG_DEBUG_FS) 317 struct dentry *debugfs_dir; 318 #endif 319 }; 320 321 static void sfp_schedule_poll(struct sfp *sfp) 322 { 323 mod_delayed_work(system_percpu_wq, &sfp->poll, SFP_POLL_INTERVAL); 324 } 325 326 static bool sff_module_supported(const struct sfp_eeprom_id *id) 327 { 328 return id->base.phys_id == SFF8024_ID_SFF_8472 && 329 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP; 330 } 331 332 static const struct sff_data sff_data = { 333 .gpios = SFP_F_LOS | SFP_F_TX_FAULT | SFP_F_TX_DISABLE, 334 .module_supported = sff_module_supported, 335 }; 336 337 static bool sfp_module_supported(const struct sfp_eeprom_id *id) 338 { 339 if (id->base.phys_id == SFF8024_ID_SFP && 340 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP) 341 return true; 342 343 /* SFP GPON module Ubiquiti U-Fiber Instant has in its EEPROM stored 344 * phys id SFF instead of SFP. Therefore mark this module explicitly 345 * as supported based on vendor name and pn match. 346 */ 347 if (id->base.phys_id == SFF8024_ID_SFF_8472 && 348 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP && 349 !memcmp(id->base.vendor_name, "UBNT ", 16) && 350 !memcmp(id->base.vendor_pn, "UF-INSTANT ", 16)) 351 return true; 352 353 return false; 354 } 355 356 static const struct sff_data sfp_data = { 357 .gpios = SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT | 358 SFP_F_TX_DISABLE | SFP_F_RS0 | SFP_F_RS1, 359 .module_supported = sfp_module_supported, 360 }; 361 362 static const struct of_device_id sfp_of_match[] = { 363 { .compatible = "sff,sff", .data = &sff_data, }, 364 { .compatible = "sff,sfp", .data = &sfp_data, }, 365 { }, 366 }; 367 MODULE_DEVICE_TABLE(of, sfp_of_match); 368 369 static void sfp_fixup_long_startup(struct sfp *sfp) 370 { 371 sfp->module_t_start_up = T_START_UP_BAD_GPON; 372 } 373 374 static void sfp_fixup_ignore_los(struct sfp *sfp) 375 { 376 /* This forces LOS to zero, so we ignore transitions */ 377 sfp->state_ignore_mask |= SFP_F_LOS; 378 /* Make sure that LOS options are clear */ 379 sfp->id.ext.options &= ~cpu_to_be16(SFP_OPTIONS_LOS_INVERTED | 380 SFP_OPTIONS_LOS_NORMAL); 381 } 382 383 static void sfp_fixup_ignore_tx_fault(struct sfp *sfp) 384 { 385 sfp->state_ignore_mask |= SFP_F_TX_FAULT; 386 } 387 388 static void sfp_fixup_ignore_tx_fault_and_los(struct sfp *sfp) 389 { 390 sfp_fixup_ignore_tx_fault(sfp); 391 sfp_fixup_ignore_los(sfp); 392 } 393 394 static void sfp_fixup_ignore_hw(struct sfp *sfp, unsigned int mask) 395 { 396 sfp->state_hw_mask &= ~mask; 397 } 398 399 static void sfp_fixup_nokia(struct sfp *sfp) 400 { 401 sfp_fixup_long_startup(sfp); 402 sfp_fixup_ignore_los(sfp); 403 } 404 405 // For 10GBASE-T short-reach modules 406 static void sfp_fixup_10gbaset_30m(struct sfp *sfp) 407 { 408 sfp->id.base.connector = SFF8024_CONNECTOR_RJ45; 409 sfp->id.base.extended_cc = SFF8024_ECC_10GBASE_T_SR; 410 } 411 412 static void sfp_fixup_rollball(struct sfp *sfp) 413 { 414 sfp->mdio_protocol = MDIO_I2C_ROLLBALL; 415 416 /* RollBall modules may disallow access to PHY registers for up to 25 417 * seconds, and the reads return 0xffff before that. Increase the time 418 * between PHY probe retries from 50ms to 1s so that we will wait for 419 * the PHY for a sufficient amount of time. 420 */ 421 sfp->phy_t_retry = msecs_to_jiffies(1000); 422 } 423 424 static void sfp_fixup_rollball_wait4s(struct sfp *sfp) 425 { 426 sfp_fixup_rollball(sfp); 427 428 /* The RollBall fixup is not enough for FS modules, the PHY chip inside 429 * them does not return 0xffff for PHY ID registers in all MMDs for the 430 * while initializing. They need a 4 second wait before accessing PHY. 431 */ 432 sfp->module_t_wait = msecs_to_jiffies(4000); 433 } 434 435 static void sfp_fixup_xikestor_2_5g(struct sfp *sfp) 436 { 437 sfp_fixup_rollball(sfp); 438 439 /* This module does not immediately respond to Rollball commands. Add 440 * a small delay to avoid unnecessary PHY access attempts. Note that 441 * the delay should not be too long, as the RTL8221B-VB-CG PHY inside 442 * breaks when reading some registers from MMD 30 if it has already 443 * established a link (which takes about 4 seconds after reset). 444 */ 445 sfp->module_t_wait = msecs_to_jiffies(1000); 446 } 447 448 static void sfp_fixup_fs_10gt(struct sfp *sfp) 449 { 450 sfp_fixup_10gbaset_30m(sfp); 451 sfp_fixup_rollball_wait4s(sfp); 452 } 453 454 static void sfp_fixup_halny_gsfp(struct sfp *sfp) 455 { 456 /* Ignore the TX_FAULT and LOS signals on this module. 457 * these are possibly used for other purposes on this 458 * module, e.g. a serial port. 459 */ 460 sfp_fixup_ignore_hw(sfp, SFP_F_TX_FAULT | SFP_F_LOS); 461 } 462 463 static void sfp_fixup_potron(struct sfp *sfp) 464 { 465 /* 466 * The TX_FAULT and LOS pins on this device are used for serial 467 * communication, so ignore them. Additionally, provide extra 468 * time for this device to fully start up. 469 */ 470 471 sfp_fixup_long_startup(sfp); 472 sfp_fixup_ignore_hw(sfp, SFP_F_TX_FAULT | SFP_F_LOS); 473 } 474 475 static void sfp_fixup_rollball_cc(struct sfp *sfp) 476 { 477 sfp_fixup_rollball(sfp); 478 479 /* Some RollBall SFPs may have wrong (zero) extended compliance code 480 * burned in EEPROM. For PHY probing we need the correct one. 481 */ 482 sfp->id.base.extended_cc = SFF8024_ECC_10GBASE_T_SFI; 483 } 484 485 static void sfp_quirk_2500basex(const struct sfp_eeprom_id *id, 486 struct sfp_module_caps *caps) 487 { 488 linkmode_set_bit(ETHTOOL_LINK_MODE_2500baseX_Full_BIT, 489 caps->link_modes); 490 __set_bit(PHY_INTERFACE_MODE_2500BASEX, caps->interfaces); 491 } 492 493 static void sfp_quirk_disable_autoneg(const struct sfp_eeprom_id *id, 494 struct sfp_module_caps *caps) 495 { 496 linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, caps->link_modes); 497 } 498 499 static void sfp_quirk_oem_2_5g(const struct sfp_eeprom_id *id, 500 struct sfp_module_caps *caps) 501 { 502 /* Copper 2.5G SFP */ 503 linkmode_set_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 504 caps->link_modes); 505 __set_bit(PHY_INTERFACE_MODE_2500BASEX, caps->interfaces); 506 sfp_quirk_disable_autoneg(id, caps); 507 } 508 509 static void sfp_quirk_ubnt_uf_instant(const struct sfp_eeprom_id *id, 510 struct sfp_module_caps *caps) 511 { 512 /* Ubiquiti U-Fiber Instant module claims that support all transceiver 513 * types including 10G Ethernet which is not truth. So clear all claimed 514 * modes and set only one mode which module supports: 1000baseX_Full, 515 * along with the Autoneg and pause bits. 516 */ 517 linkmode_zero(caps->link_modes); 518 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, 519 caps->link_modes); 520 linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, caps->link_modes); 521 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, caps->link_modes); 522 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, caps->link_modes); 523 524 phy_interface_zero(caps->interfaces); 525 __set_bit(PHY_INTERFACE_MODE_1000BASEX, caps->interfaces); 526 } 527 528 #define SFP_QUIRK(_v, _p, _s, _f) \ 529 { .vendor = _v, .part = _p, .support = _s, .fixup = _f, } 530 #define SFP_QUIRK_S(_v, _p, _s) SFP_QUIRK(_v, _p, _s, NULL) 531 #define SFP_QUIRK_F(_v, _p, _f) SFP_QUIRK(_v, _p, NULL, _f) 532 /* Like SFP_QUIRK_F, but matches the part as a prefix; the vendor name 533 * is still matched exactly. Use for modules whose EEPROM vendor PN 534 * field reads back with garbage past the legitimate characters instead 535 * of the SFF-8472-mandated space padding, so sfp_strlen can't trim the 536 * field down to the legitimate length. 537 */ 538 #define SFP_QUIRK_F_PREFIX(_v, _p, _f) \ 539 { .vendor = _v, .part = _p, .support = NULL, .fixup = _f, \ 540 .part_prefix_match = true } 541 542 static const struct sfp_quirk sfp_quirks[] = { 543 // Alcatel Lucent G-010S-P can operate at 2500base-X, but incorrectly 544 // report 2500MBd NRZ in their EEPROM 545 SFP_QUIRK("ALCATELLUCENT", "G010SP", sfp_quirk_2500basex, 546 sfp_fixup_ignore_tx_fault), 547 548 // Alcatel Lucent G-010S-A can operate at 2500base-X, but report 3.2GBd 549 // NRZ in their EEPROM 550 SFP_QUIRK("ALCATELLUCENT", "3FE46541AA", sfp_quirk_2500basex, 551 sfp_fixup_nokia), 552 553 SFP_QUIRK_F("BIDB", "X-ONU-SFPP", sfp_fixup_potron), 554 555 // FLYPRO SFP-10GT-CS-30M uses Rollball protocol to talk to the PHY. 556 SFP_QUIRK_F("FLYPRO", "SFP-10GT-CS-30M", sfp_fixup_rollball), 557 558 // Fiberstore SFP-10G-T doesn't identify as copper, uses the Rollball 559 // protocol to talk to the PHY and needs 4 sec wait before probing the 560 // PHY. 561 SFP_QUIRK_F("FS", "SFP-10G-T", sfp_fixup_fs_10gt), 562 563 // Fiberstore SFP-2.5G-T and SFP-10GM-T uses Rollball protocol to talk 564 // to the PHY and needs 4 sec wait before probing the PHY. 565 SFP_QUIRK_F("FS", "SFP-2.5G-T", sfp_fixup_rollball_wait4s), 566 SFP_QUIRK_F("FS", "SFP-10GM-T", sfp_fixup_rollball_wait4s), 567 568 // Fiberstore GPON-ONU-34-20BI can operate at 2500base-X, but report 1.2GBd 569 // NRZ in their EEPROM 570 SFP_QUIRK("FS", "GPON-ONU-34-20BI", sfp_quirk_2500basex, 571 sfp_fixup_ignore_tx_fault), 572 573 // Fiberstore XGS-SFP-ONT-MACI is a MAC-mode XGS-PON ONT stick with 574 // ONT-class serial-passthrough TX_FAULT/LOS wiring and slow startup; 575 // mask both signals and extend T_START_UP via the potron fixup. The 576 // PN is the product name (XGS-SFP-ONT-MAC-I) truncated at the 16-byte 577 // field width, so the field is fully occupied and matches exactly. 578 SFP_QUIRK_F("FS", "XGS-SFP-ONT-MACI", sfp_fixup_potron), 579 580 SFP_QUIRK_F("HALNy", "HL-GSFP", sfp_fixup_halny_gsfp), 581 582 SFP_QUIRK_F("H-COM", "SPP425H-GAB4", sfp_fixup_potron), 583 584 // HG MXPD-483II-F 2.5G supports 2500Base-X, but incorrectly reports 585 // 2600MBd in their EERPOM 586 SFP_QUIRK_S("HG GENUINE", "MXPD-483II", sfp_quirk_2500basex), 587 588 // Huawei MA5671A can operate at 2500base-X, but report 1.2GBd NRZ in 589 // their EEPROM 590 SFP_QUIRK("HUAWEI", "MA5671A", sfp_quirk_2500basex, 591 sfp_fixup_ignore_tx_fault_and_los), 592 593 // Hisense LXT-010S-H is a GPON ONT SFP (sold as LEOX LXT-010S-H) that 594 // can operate at 2500base-X, but reports 1000BASE-LX / 1300MBd in its 595 // EEPROM 596 SFP_QUIRK("Hisense-Leox", "LXT-010S-H", sfp_quirk_2500basex, 597 sfp_fixup_ignore_tx_fault), 598 599 // Hisense ZNID-GPON-2311NA can operate at 2500base-X, but reports 600 // 1000BASE-LX / 1300MBd in its EEPROM 601 SFP_QUIRK("Hisense", "ZNID-GPON-2311NA", sfp_quirk_2500basex, 602 sfp_fixup_ignore_tx_fault), 603 604 // HSGQ HSGQ-XPON-Stick can operate at 2500base-X, but reports 605 // 1000BASE-LX / 1300MBd in its EEPROM 606 SFP_QUIRK("HSGQ", "HSGQ-XPON-Stick", sfp_quirk_2500basex, 607 sfp_fixup_ignore_tx_fault), 608 609 // Lantech 8330-262D-E and 8330-265D can operate at 2500base-X, but 610 // incorrectly report 2500MBd NRZ in their EEPROM. 611 // Some 8330-265D modules have inverted LOS, while all of them report 612 // normal LOS in EEPROM. Therefore we need to ignore LOS entirely. 613 SFP_QUIRK_S("Lantech", "8330-262D-E", sfp_quirk_2500basex), 614 SFP_QUIRK("Lantech", "8330-265D", sfp_quirk_2500basex, 615 sfp_fixup_ignore_los), 616 617 SFP_QUIRK_S("UBNT", "UF-INSTANT", sfp_quirk_ubnt_uf_instant), 618 619 // Walsun HXSX-ATR[CI]-1 don't identify as copper, and use the 620 // Rollball protocol to talk to the PHY. 621 SFP_QUIRK_F("Walsun", "HXSX-ATRC-1", sfp_fixup_fs_10gt), 622 SFP_QUIRK_F("Walsun", "HXSX-ATRI-1", sfp_fixup_fs_10gt), 623 624 SFP_QUIRK_F("YV", "SFP+ONU-XGSPON", sfp_fixup_potron), 625 626 // HORACO HC-10GE-113C uses Rollball protocol to talk to the PHY. 627 SFP_QUIRK_F("OEM", "HC-10GE-113C", sfp_fixup_rollball), 628 629 // OEM SFP-GE-T is a 1000Base-T module with broken TX_FAULT indicator 630 SFP_QUIRK_F("OEM", "SFP-GE-T", sfp_fixup_ignore_tx_fault), 631 632 SFP_QUIRK_F("OEM", "SFP-10G-T", sfp_fixup_rollball_cc), 633 SFP_QUIRK_S("OEM", "SFP-2.5G-T", sfp_quirk_oem_2_5g), 634 SFP_QUIRK_S("OEM", "SFP-2.5G-BX10-D", sfp_quirk_2500basex), 635 SFP_QUIRK_S("OEM", "SFP-2.5G-BX10-U", sfp_quirk_2500basex), 636 SFP_QUIRK_S("OEM", "SFP-2.5G-LH03-B", sfp_quirk_2500basex), 637 SFP_QUIRK_S("OEM", "SFP-2.5G-LH20-A", sfp_quirk_2500basex), 638 SFP_QUIRK_F("OEM", "RTSFP-10", sfp_fixup_rollball_cc), 639 SFP_QUIRK_F("OEM", "RTSFP-10G", sfp_fixup_rollball_cc), 640 641 // OEM XGSPONST2001 is an XGS-PON ONT stick with broken TX_FAULT and 642 // LOS indicators and slow startup, just like potron. On cold 643 // power-up the EEPROM vendor PN field reads back with non-printable 644 // garbage past the legitimate string instead of space padding, so 645 // match the part as a prefix. 646 SFP_QUIRK_F_PREFIX("OEM", "XGSPONST2001", sfp_fixup_potron), 647 648 SFP_QUIRK_F("Turris", "RTSFP-2.5G", sfp_fixup_rollball), 649 SFP_QUIRK_F("Turris", "RTSFP-10", sfp_fixup_rollball), 650 SFP_QUIRK_F("Turris", "RTSFP-10G", sfp_fixup_rollball), 651 652 SFP_QUIRK_F("XikeStor", "SKT-2.5G-100M", sfp_fixup_xikestor_2_5g), 653 654 SFP_QUIRK_S("ZOERAX", "SFP-2.5G-T", sfp_quirk_oem_2_5g), 655 }; 656 657 static size_t sfp_strlen(const char *str, size_t maxlen) 658 { 659 size_t size, i; 660 661 /* Trailing characters should be filled with space chars, but 662 * some manufacturers can't read SFF-8472 and use NUL. 663 */ 664 for (i = 0, size = 0; i < maxlen; i++) 665 if (str[i] != ' ' && str[i] != '\0') 666 size = i + 1; 667 668 return size; 669 } 670 671 static bool sfp_match(const char *qs, const char *str, size_t len, bool prefix) 672 { 673 size_t qs_len; 674 675 if (!qs) 676 return true; 677 qs_len = strlen(qs); 678 if (prefix ? qs_len > len : qs_len != len) 679 return false; 680 return !strncmp(qs, str, qs_len); 681 } 682 683 static const struct sfp_quirk *sfp_lookup_quirk(const struct sfp_eeprom_id *id) 684 { 685 const struct sfp_quirk *q; 686 unsigned int i; 687 size_t vs, ps; 688 689 vs = sfp_strlen(id->base.vendor_name, ARRAY_SIZE(id->base.vendor_name)); 690 ps = sfp_strlen(id->base.vendor_pn, ARRAY_SIZE(id->base.vendor_pn)); 691 692 for (i = 0, q = sfp_quirks; i < ARRAY_SIZE(sfp_quirks); i++, q++) 693 if (sfp_match(q->vendor, id->base.vendor_name, vs, false) && 694 sfp_match(q->part, id->base.vendor_pn, ps, 695 q->part_prefix_match)) 696 return q; 697 698 return NULL; 699 } 700 701 static unsigned int sfp_gpio_get_state(struct sfp *sfp) 702 { 703 unsigned int i, state, v; 704 705 for (i = state = 0; i < GPIO_MAX; i++) { 706 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i]) 707 continue; 708 709 v = gpiod_get_value_cansleep(sfp->gpio[i]); 710 if (v) 711 state |= BIT(i); 712 } 713 714 return state; 715 } 716 717 static unsigned int sff_gpio_get_state(struct sfp *sfp) 718 { 719 return sfp_gpio_get_state(sfp) | SFP_F_PRESENT; 720 } 721 722 static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state) 723 { 724 unsigned int drive; 725 726 if (state & SFP_F_PRESENT) 727 /* If the module is present, drive the requested signals */ 728 drive = sfp->state_hw_drive; 729 else 730 /* Otherwise, let them float to the pull-ups */ 731 drive = 0; 732 733 if (sfp->gpio[GPIO_TX_DISABLE]) { 734 if (drive & SFP_F_TX_DISABLE) 735 gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE], 736 state & SFP_F_TX_DISABLE); 737 else 738 gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]); 739 } 740 741 if (sfp->gpio[GPIO_RS0]) { 742 if (drive & SFP_F_RS0) 743 gpiod_direction_output(sfp->gpio[GPIO_RS0], 744 state & SFP_F_RS0); 745 else 746 gpiod_direction_input(sfp->gpio[GPIO_RS0]); 747 } 748 749 if (sfp->gpio[GPIO_RS1]) { 750 if (drive & SFP_F_RS1) 751 gpiod_direction_output(sfp->gpio[GPIO_RS1], 752 state & SFP_F_RS1); 753 else 754 gpiod_direction_input(sfp->gpio[GPIO_RS1]); 755 } 756 } 757 758 static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf, 759 size_t len) 760 { 761 struct i2c_msg msgs[2]; 762 u8 bus_addr = a2 ? 0x51 : 0x50; 763 size_t block_size = sfp->i2c_block_size; 764 size_t this_len; 765 int ret; 766 767 msgs[0].addr = bus_addr; 768 msgs[0].flags = 0; 769 msgs[0].len = 1; 770 msgs[0].buf = &dev_addr; 771 msgs[1].addr = bus_addr; 772 msgs[1].flags = I2C_M_RD; 773 msgs[1].len = len; 774 msgs[1].buf = buf; 775 776 while (len) { 777 this_len = len; 778 if (this_len > block_size) 779 this_len = block_size; 780 781 msgs[1].len = this_len; 782 783 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs)); 784 if (ret < 0) 785 return ret; 786 787 if (ret != ARRAY_SIZE(msgs)) 788 break; 789 790 msgs[1].buf += this_len; 791 dev_addr += this_len; 792 len -= this_len; 793 } 794 795 return msgs[1].buf - (u8 *)buf; 796 } 797 798 static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf, 799 size_t len) 800 { 801 struct i2c_msg msgs[1]; 802 u8 bus_addr = a2 ? 0x51 : 0x50; 803 int ret; 804 805 msgs[0].addr = bus_addr; 806 msgs[0].flags = 0; 807 msgs[0].len = 1 + len; 808 msgs[0].buf = kmalloc(1 + len, GFP_KERNEL); 809 if (!msgs[0].buf) 810 return -ENOMEM; 811 812 msgs[0].buf[0] = dev_addr; 813 memcpy(&msgs[0].buf[1], buf, len); 814 815 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs)); 816 817 kfree(msgs[0].buf); 818 819 if (ret < 0) 820 return ret; 821 822 return ret == ARRAY_SIZE(msgs) ? len : 0; 823 } 824 825 static int sfp_smbus_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf, 826 size_t len) 827 { 828 union i2c_smbus_data smbus_data = {0}; 829 u8 bus_addr = a2 ? 0x51 : 0x50; 830 size_t this_len, transferred; 831 u32 functionality; 832 u8 *data = buf; 833 int ret; 834 835 functionality = i2c_get_functionality(sfp->i2c); 836 837 while (len) { 838 this_len = min(len, sfp->i2c_block_size); 839 840 if (functionality & I2C_FUNC_SMBUS_READ_I2C_BLOCK) { 841 smbus_data.block[0] = this_len; 842 ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0, 843 I2C_SMBUS_READ, dev_addr, 844 I2C_SMBUS_I2C_BLOCK_DATA, &smbus_data); 845 if (ret < 0) 846 return ret; 847 848 transferred = min_t(size_t, smbus_data.block[0], this_len); 849 if (!transferred) 850 return -EIO; 851 852 memcpy(data, &smbus_data.block[1], transferred); 853 } else if (this_len >= 2 && 854 (functionality & I2C_FUNC_SMBUS_READ_WORD_DATA)) { 855 ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0, 856 I2C_SMBUS_READ, dev_addr, 857 I2C_SMBUS_WORD_DATA, &smbus_data); 858 if (ret < 0) 859 return ret; 860 861 put_unaligned_le16(smbus_data.word, data); 862 transferred = 2; 863 } else { 864 ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0, 865 I2C_SMBUS_READ, dev_addr, 866 I2C_SMBUS_BYTE_DATA, &smbus_data); 867 if (ret < 0) 868 return ret; 869 870 *data = smbus_data.byte; 871 transferred = 1; 872 } 873 874 data += transferred; 875 len -= transferred; 876 dev_addr += transferred; 877 } 878 879 return data - (u8 *)buf; 880 } 881 882 static int sfp_smbus_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf, 883 size_t len) 884 { 885 union i2c_smbus_data smbus_data; 886 u8 bus_addr = a2 ? 0x51 : 0x50; 887 size_t this_len, transferred; 888 u32 functionality; 889 u8 *data = buf; 890 int ret; 891 892 functionality = i2c_get_functionality(sfp->i2c); 893 894 while (len) { 895 this_len = min(len, sfp->i2c_block_size); 896 897 if (functionality & I2C_FUNC_SMBUS_WRITE_I2C_BLOCK) { 898 smbus_data.block[0] = this_len; 899 memcpy(&smbus_data.block[1], data, this_len); 900 901 ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0, 902 I2C_SMBUS_WRITE, dev_addr, 903 I2C_SMBUS_I2C_BLOCK_DATA, &smbus_data); 904 if (ret < 0) 905 return ret; 906 907 transferred = this_len; 908 } else if (this_len >= 2 && 909 (functionality & I2C_FUNC_SMBUS_WRITE_WORD_DATA)) { 910 smbus_data.word = get_unaligned_le16(data); 911 ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0, 912 I2C_SMBUS_WRITE, dev_addr, 913 I2C_SMBUS_WORD_DATA, &smbus_data); 914 if (ret < 0) 915 return ret; 916 917 transferred = 2; 918 } else { 919 smbus_data.byte = *data; 920 ret = i2c_smbus_xfer(sfp->i2c, bus_addr, 0, 921 I2C_SMBUS_WRITE, dev_addr, 922 I2C_SMBUS_BYTE_DATA, &smbus_data); 923 if (ret < 0) 924 return ret; 925 926 transferred = 1; 927 } 928 929 data += transferred; 930 len -= transferred; 931 dev_addr += transferred; 932 } 933 934 return data - (u8 *)buf; 935 } 936 937 static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c) 938 { 939 size_t max_block_size; 940 941 sfp->i2c = i2c; 942 943 if (i2c_check_functionality(i2c, I2C_FUNC_I2C)) { 944 sfp->read = sfp_i2c_read; 945 sfp->write = sfp_i2c_write; 946 max_block_size = SFP_EEPROM_BLOCK_SIZE; 947 } else if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_BYTE_DATA) || 948 i2c_check_functionality(i2c, I2C_FUNC_SMBUS_I2C_BLOCK)) { 949 /* Either protocol alone covers any length: I2C-block carries 950 * 1..32 bytes per xfer, byte iterates one byte at a time. 951 */ 952 sfp->read = sfp_smbus_read; 953 sfp->write = sfp_smbus_write; 954 955 if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_I2C_BLOCK)) 956 max_block_size = SFP_EEPROM_BLOCK_SIZE; 957 else if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_WORD_DATA)) 958 max_block_size = 2; 959 else 960 max_block_size = 1; 961 } else if (WARN_ONCE(i2c_check_functionality(i2c, I2C_FUNC_SMBUS_WORD_DATA), 962 "SMBus word-only adapter; odd-length transfers will fail\n")) { 963 /* Word-only: even-length xfers work; odd-length xfers fall 964 * to BYTE, which the adapter does not advertise and will 965 * likely fail. 966 */ 967 sfp->read = sfp_smbus_read; 968 sfp->write = sfp_smbus_write; 969 max_block_size = 2; 970 } else { 971 sfp->i2c = NULL; 972 return -EINVAL; 973 } 974 975 if (i2c->quirks && i2c->quirks->max_read_len) 976 max_block_size = min(max_block_size, i2c->quirks->max_read_len); 977 if (i2c->quirks && i2c->quirks->max_write_len) 978 max_block_size = min(max_block_size, i2c->quirks->max_write_len); 979 980 sfp->i2c_max_block_size = max_block_size; 981 sfp->i2c_block_size = sfp->i2c_max_block_size; 982 return 0; 983 } 984 985 static int sfp_i2c_mdiobus_create(struct sfp *sfp) 986 { 987 struct mii_bus *i2c_mii; 988 int ret; 989 990 i2c_mii = mdio_i2c_alloc(sfp->dev, sfp->i2c, sfp->mdio_protocol); 991 if (IS_ERR(i2c_mii)) 992 return PTR_ERR(i2c_mii); 993 994 i2c_mii->name = "SFP I2C Bus"; 995 i2c_mii->phy_mask = ~0; 996 997 ret = mdiobus_register(i2c_mii); 998 if (ret < 0) { 999 mdiobus_free(i2c_mii); 1000 return ret; 1001 } 1002 1003 sfp->i2c_mii = i2c_mii; 1004 1005 return 0; 1006 } 1007 1008 static void sfp_i2c_mdiobus_destroy(struct sfp *sfp) 1009 { 1010 mdiobus_unregister(sfp->i2c_mii); 1011 mdiobus_free(sfp->i2c_mii); 1012 sfp->i2c_mii = NULL; 1013 } 1014 1015 /* Interface */ 1016 static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len) 1017 { 1018 return sfp->read(sfp, a2, addr, buf, len); 1019 } 1020 1021 /* Probe whether a module is physically present by attempting a single-byte 1022 * I2C read of the EEPROM identifier (an empty cage NAKs). Used as the presence 1023 * source on boards that do not wire MOD_DEF0 to a GPIO. 1024 */ 1025 static bool sfp_module_present_i2c(struct sfp *sfp) 1026 { 1027 u8 id; 1028 1029 return sfp_read(sfp, false, SFP_PHYS_ID, &id, sizeof(id)) == sizeof(id); 1030 } 1031 1032 /* get_state variant for boards without a MOD_DEF0 GPIO. Instead of assuming 1033 * the module is always present, derive SFP_F_PRESENT from a throttled I2C 1034 * probe so that hot-insertion and removal are detected. A single ACK asserts 1035 * presence; R_PROBE_ABSENT consecutive failures clear it, to ride out a 1036 * transient I2C error on a live module. 1037 */ 1038 static unsigned int sfp_i2c_get_state(struct sfp *sfp) 1039 { 1040 unsigned int state = sfp_gpio_get_state(sfp); 1041 1042 if (time_after_eq(jiffies, sfp->i2c_present_next)) { 1043 if (sfp_module_present_i2c(sfp)) { 1044 sfp->i2c_present = true; 1045 sfp->i2c_present_nak = 0; 1046 } else if (sfp->i2c_present && 1047 ++sfp->i2c_present_nak >= R_PROBE_ABSENT) { 1048 sfp->i2c_present = false; 1049 sfp->i2c_present_nak = 0; 1050 } 1051 sfp->i2c_present_next = jiffies + T_PROBE_PRESENT; 1052 } 1053 1054 if (sfp->i2c_present) 1055 state |= SFP_F_PRESENT; 1056 1057 return state; 1058 } 1059 1060 static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len) 1061 { 1062 return sfp->write(sfp, a2, addr, buf, len); 1063 } 1064 1065 static int sfp_modify_u8(struct sfp *sfp, bool a2, u8 addr, u8 mask, u8 val) 1066 { 1067 int ret; 1068 u8 old, v; 1069 1070 ret = sfp_read(sfp, a2, addr, &old, sizeof(old)); 1071 if (ret != sizeof(old)) 1072 return ret; 1073 1074 v = (old & ~mask) | (val & mask); 1075 if (v == old) 1076 return sizeof(v); 1077 1078 return sfp_write(sfp, a2, addr, &v, sizeof(v)); 1079 } 1080 1081 static unsigned int sfp_soft_get_state(struct sfp *sfp) 1082 { 1083 unsigned int state = 0; 1084 u8 status; 1085 int ret; 1086 1087 ret = sfp_read(sfp, true, SFP_STATUS, &status, sizeof(status)); 1088 if (ret == sizeof(status)) { 1089 if (status & SFP_STATUS_RX_LOS) 1090 state |= SFP_F_LOS; 1091 if (status & SFP_STATUS_TX_FAULT) 1092 state |= SFP_F_TX_FAULT; 1093 } else { 1094 dev_err_ratelimited(sfp->dev, 1095 "failed to read SFP soft status: %pe\n", 1096 ERR_PTR(ret)); 1097 /* Preserve the current state */ 1098 state = sfp->state; 1099 } 1100 1101 return state & sfp->state_soft_mask; 1102 } 1103 1104 static void sfp_soft_set_state(struct sfp *sfp, unsigned int state, 1105 unsigned int soft) 1106 { 1107 u8 mask = 0; 1108 u8 val = 0; 1109 1110 if (soft & SFP_F_TX_DISABLE) 1111 mask |= SFP_STATUS_TX_DISABLE_FORCE; 1112 if (state & SFP_F_TX_DISABLE) 1113 val |= SFP_STATUS_TX_DISABLE_FORCE; 1114 1115 if (soft & SFP_F_RS0) 1116 mask |= SFP_STATUS_RS0_SELECT; 1117 if (state & SFP_F_RS0) 1118 val |= SFP_STATUS_RS0_SELECT; 1119 1120 if (mask) 1121 sfp_modify_u8(sfp, true, SFP_STATUS, mask, val); 1122 1123 val = mask = 0; 1124 if (soft & SFP_F_RS1) 1125 mask |= SFP_EXT_STATUS_RS1_SELECT; 1126 if (state & SFP_F_RS1) 1127 val |= SFP_EXT_STATUS_RS1_SELECT; 1128 1129 if (mask) 1130 sfp_modify_u8(sfp, true, SFP_EXT_STATUS, mask, val); 1131 } 1132 1133 static void sfp_soft_start_poll(struct sfp *sfp) 1134 { 1135 const struct sfp_eeprom_id *id = &sfp->id; 1136 unsigned int mask = 0; 1137 1138 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_DISABLE) 1139 mask |= SFP_F_TX_DISABLE; 1140 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_FAULT) 1141 mask |= SFP_F_TX_FAULT; 1142 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_RX_LOS) 1143 mask |= SFP_F_LOS; 1144 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_RATE_SELECT) 1145 mask |= sfp->rs_state_mask; 1146 1147 mutex_lock(&sfp->st_mutex); 1148 // Poll the soft state for hardware pins we want to ignore 1149 sfp->state_soft_mask = ~sfp->state_hw_mask & ~sfp->state_ignore_mask & 1150 mask; 1151 1152 if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) && 1153 !sfp->need_poll) 1154 sfp_schedule_poll(sfp); 1155 mutex_unlock(&sfp->st_mutex); 1156 } 1157 1158 static void sfp_soft_stop_poll(struct sfp *sfp) 1159 { 1160 mutex_lock(&sfp->st_mutex); 1161 sfp->state_soft_mask = 0; 1162 mutex_unlock(&sfp->st_mutex); 1163 } 1164 1165 /* sfp_get_state() - must be called with st_mutex held, or in the 1166 * initialisation path. 1167 */ 1168 static unsigned int sfp_get_state(struct sfp *sfp) 1169 { 1170 unsigned int soft = sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT); 1171 unsigned int state; 1172 1173 state = sfp->get_state(sfp) & sfp->state_hw_mask; 1174 if (state & SFP_F_PRESENT && soft) 1175 state |= sfp_soft_get_state(sfp); 1176 1177 return state; 1178 } 1179 1180 /* sfp_set_state() - must be called with st_mutex held, or in the 1181 * initialisation path. 1182 */ 1183 static void sfp_set_state(struct sfp *sfp, unsigned int state) 1184 { 1185 unsigned int soft; 1186 1187 sfp->set_state(sfp, state); 1188 1189 soft = sfp->state_soft_mask & SFP_F_OUTPUTS; 1190 if (state & SFP_F_PRESENT && soft) 1191 sfp_soft_set_state(sfp, state, soft); 1192 } 1193 1194 static void sfp_mod_state(struct sfp *sfp, unsigned int mask, unsigned int set) 1195 { 1196 mutex_lock(&sfp->st_mutex); 1197 sfp->state = (sfp->state & ~mask) | set; 1198 sfp_set_state(sfp, sfp->state); 1199 mutex_unlock(&sfp->st_mutex); 1200 } 1201 1202 static unsigned int sfp_check(void *buf, size_t len) 1203 { 1204 u8 *p, check; 1205 1206 for (p = buf, check = 0; len; p++, len--) 1207 check += *p; 1208 1209 return check; 1210 } 1211 1212 /* hwmon */ 1213 #if IS_ENABLED(CONFIG_HWMON) 1214 static umode_t sfp_hwmon_is_visible(const void *data, 1215 enum hwmon_sensor_types type, 1216 u32 attr, int channel) 1217 { 1218 const struct sfp *sfp = data; 1219 1220 switch (type) { 1221 case hwmon_temp: 1222 switch (attr) { 1223 case hwmon_temp_min_alarm: 1224 case hwmon_temp_max_alarm: 1225 case hwmon_temp_lcrit_alarm: 1226 case hwmon_temp_crit_alarm: 1227 case hwmon_temp_min: 1228 case hwmon_temp_max: 1229 case hwmon_temp_lcrit: 1230 case hwmon_temp_crit: 1231 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN)) 1232 return 0; 1233 fallthrough; 1234 case hwmon_temp_input: 1235 case hwmon_temp_label: 1236 return 0444; 1237 default: 1238 return 0; 1239 } 1240 case hwmon_in: 1241 switch (attr) { 1242 case hwmon_in_min_alarm: 1243 case hwmon_in_max_alarm: 1244 case hwmon_in_lcrit_alarm: 1245 case hwmon_in_crit_alarm: 1246 case hwmon_in_min: 1247 case hwmon_in_max: 1248 case hwmon_in_lcrit: 1249 case hwmon_in_crit: 1250 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN)) 1251 return 0; 1252 fallthrough; 1253 case hwmon_in_input: 1254 case hwmon_in_label: 1255 return 0444; 1256 default: 1257 return 0; 1258 } 1259 case hwmon_curr: 1260 switch (attr) { 1261 case hwmon_curr_min_alarm: 1262 case hwmon_curr_max_alarm: 1263 case hwmon_curr_lcrit_alarm: 1264 case hwmon_curr_crit_alarm: 1265 case hwmon_curr_min: 1266 case hwmon_curr_max: 1267 case hwmon_curr_lcrit: 1268 case hwmon_curr_crit: 1269 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN)) 1270 return 0; 1271 fallthrough; 1272 case hwmon_curr_input: 1273 case hwmon_curr_label: 1274 return 0444; 1275 default: 1276 return 0; 1277 } 1278 case hwmon_power: 1279 /* External calibration of receive power requires 1280 * floating point arithmetic. Doing that in the kernel 1281 * is not easy, so just skip it. If the module does 1282 * not require external calibration, we can however 1283 * show receiver power, since FP is then not needed. 1284 */ 1285 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL && 1286 channel == 1) 1287 return 0; 1288 switch (attr) { 1289 case hwmon_power_min_alarm: 1290 case hwmon_power_max_alarm: 1291 case hwmon_power_lcrit_alarm: 1292 case hwmon_power_crit_alarm: 1293 case hwmon_power_min: 1294 case hwmon_power_max: 1295 case hwmon_power_lcrit: 1296 case hwmon_power_crit: 1297 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN)) 1298 return 0; 1299 fallthrough; 1300 case hwmon_power_input: 1301 case hwmon_power_label: 1302 return 0444; 1303 default: 1304 return 0; 1305 } 1306 default: 1307 return 0; 1308 } 1309 } 1310 1311 static int sfp_hwmon_read_sensor(struct sfp *sfp, int reg, long *value) 1312 { 1313 __be16 val; 1314 int err; 1315 1316 err = sfp_read(sfp, true, reg, &val, sizeof(val)); 1317 if (err < 0) 1318 return err; 1319 1320 *value = be16_to_cpu(val); 1321 1322 return 0; 1323 } 1324 1325 static void sfp_hwmon_to_rx_power(long *value) 1326 { 1327 *value = DIV_ROUND_CLOSEST(*value, 10); 1328 } 1329 1330 static void sfp_hwmon_calibrate(struct sfp *sfp, unsigned int slope, int offset, 1331 long *value) 1332 { 1333 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL) 1334 *value = DIV_ROUND_CLOSEST(*value * slope, 256) + offset; 1335 } 1336 1337 static void sfp_hwmon_calibrate_temp(struct sfp *sfp, long *value) 1338 { 1339 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_t_slope), 1340 be16_to_cpu(sfp->diag.cal_t_offset), value); 1341 1342 if (*value >= 0x8000) 1343 *value -= 0x10000; 1344 1345 *value = DIV_ROUND_CLOSEST(*value * 1000, 256); 1346 } 1347 1348 static void sfp_hwmon_calibrate_vcc(struct sfp *sfp, long *value) 1349 { 1350 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_v_slope), 1351 be16_to_cpu(sfp->diag.cal_v_offset), value); 1352 1353 *value = DIV_ROUND_CLOSEST(*value, 10); 1354 } 1355 1356 static void sfp_hwmon_calibrate_bias(struct sfp *sfp, long *value) 1357 { 1358 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txi_slope), 1359 be16_to_cpu(sfp->diag.cal_txi_offset), value); 1360 1361 *value = DIV_ROUND_CLOSEST(*value, 500); 1362 } 1363 1364 static void sfp_hwmon_calibrate_tx_power(struct sfp *sfp, long *value) 1365 { 1366 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txpwr_slope), 1367 be16_to_cpu(sfp->diag.cal_txpwr_offset), value); 1368 1369 *value = DIV_ROUND_CLOSEST(*value, 10); 1370 } 1371 1372 static int sfp_hwmon_read_temp(struct sfp *sfp, int reg, long *value) 1373 { 1374 int err; 1375 1376 err = sfp_hwmon_read_sensor(sfp, reg, value); 1377 if (err < 0) 1378 return err; 1379 1380 sfp_hwmon_calibrate_temp(sfp, value); 1381 1382 return 0; 1383 } 1384 1385 static int sfp_hwmon_read_vcc(struct sfp *sfp, int reg, long *value) 1386 { 1387 int err; 1388 1389 err = sfp_hwmon_read_sensor(sfp, reg, value); 1390 if (err < 0) 1391 return err; 1392 1393 sfp_hwmon_calibrate_vcc(sfp, value); 1394 1395 return 0; 1396 } 1397 1398 static int sfp_hwmon_read_bias(struct sfp *sfp, int reg, long *value) 1399 { 1400 int err; 1401 1402 err = sfp_hwmon_read_sensor(sfp, reg, value); 1403 if (err < 0) 1404 return err; 1405 1406 sfp_hwmon_calibrate_bias(sfp, value); 1407 1408 return 0; 1409 } 1410 1411 static int sfp_hwmon_read_tx_power(struct sfp *sfp, int reg, long *value) 1412 { 1413 int err; 1414 1415 err = sfp_hwmon_read_sensor(sfp, reg, value); 1416 if (err < 0) 1417 return err; 1418 1419 sfp_hwmon_calibrate_tx_power(sfp, value); 1420 1421 return 0; 1422 } 1423 1424 static int sfp_hwmon_read_rx_power(struct sfp *sfp, int reg, long *value) 1425 { 1426 int err; 1427 1428 err = sfp_hwmon_read_sensor(sfp, reg, value); 1429 if (err < 0) 1430 return err; 1431 1432 sfp_hwmon_to_rx_power(value); 1433 1434 return 0; 1435 } 1436 1437 static int sfp_hwmon_temp(struct sfp *sfp, u32 attr, long *value) 1438 { 1439 u8 status; 1440 int err; 1441 1442 switch (attr) { 1443 case hwmon_temp_input: 1444 return sfp_hwmon_read_temp(sfp, SFP_TEMP, value); 1445 1446 case hwmon_temp_lcrit: 1447 *value = be16_to_cpu(sfp->diag.temp_low_alarm); 1448 sfp_hwmon_calibrate_temp(sfp, value); 1449 return 0; 1450 1451 case hwmon_temp_min: 1452 *value = be16_to_cpu(sfp->diag.temp_low_warn); 1453 sfp_hwmon_calibrate_temp(sfp, value); 1454 return 0; 1455 case hwmon_temp_max: 1456 *value = be16_to_cpu(sfp->diag.temp_high_warn); 1457 sfp_hwmon_calibrate_temp(sfp, value); 1458 return 0; 1459 1460 case hwmon_temp_crit: 1461 *value = be16_to_cpu(sfp->diag.temp_high_alarm); 1462 sfp_hwmon_calibrate_temp(sfp, value); 1463 return 0; 1464 1465 case hwmon_temp_lcrit_alarm: 1466 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 1467 if (err < 0) 1468 return err; 1469 1470 *value = !!(status & SFP_ALARM0_TEMP_LOW); 1471 return 0; 1472 1473 case hwmon_temp_min_alarm: 1474 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 1475 if (err < 0) 1476 return err; 1477 1478 *value = !!(status & SFP_WARN0_TEMP_LOW); 1479 return 0; 1480 1481 case hwmon_temp_max_alarm: 1482 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 1483 if (err < 0) 1484 return err; 1485 1486 *value = !!(status & SFP_WARN0_TEMP_HIGH); 1487 return 0; 1488 1489 case hwmon_temp_crit_alarm: 1490 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 1491 if (err < 0) 1492 return err; 1493 1494 *value = !!(status & SFP_ALARM0_TEMP_HIGH); 1495 return 0; 1496 default: 1497 return -EOPNOTSUPP; 1498 } 1499 1500 return -EOPNOTSUPP; 1501 } 1502 1503 static int sfp_hwmon_vcc(struct sfp *sfp, u32 attr, long *value) 1504 { 1505 u8 status; 1506 int err; 1507 1508 switch (attr) { 1509 case hwmon_in_input: 1510 return sfp_hwmon_read_vcc(sfp, SFP_VCC, value); 1511 1512 case hwmon_in_lcrit: 1513 *value = be16_to_cpu(sfp->diag.volt_low_alarm); 1514 sfp_hwmon_calibrate_vcc(sfp, value); 1515 return 0; 1516 1517 case hwmon_in_min: 1518 *value = be16_to_cpu(sfp->diag.volt_low_warn); 1519 sfp_hwmon_calibrate_vcc(sfp, value); 1520 return 0; 1521 1522 case hwmon_in_max: 1523 *value = be16_to_cpu(sfp->diag.volt_high_warn); 1524 sfp_hwmon_calibrate_vcc(sfp, value); 1525 return 0; 1526 1527 case hwmon_in_crit: 1528 *value = be16_to_cpu(sfp->diag.volt_high_alarm); 1529 sfp_hwmon_calibrate_vcc(sfp, value); 1530 return 0; 1531 1532 case hwmon_in_lcrit_alarm: 1533 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 1534 if (err < 0) 1535 return err; 1536 1537 *value = !!(status & SFP_ALARM0_VCC_LOW); 1538 return 0; 1539 1540 case hwmon_in_min_alarm: 1541 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 1542 if (err < 0) 1543 return err; 1544 1545 *value = !!(status & SFP_WARN0_VCC_LOW); 1546 return 0; 1547 1548 case hwmon_in_max_alarm: 1549 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 1550 if (err < 0) 1551 return err; 1552 1553 *value = !!(status & SFP_WARN0_VCC_HIGH); 1554 return 0; 1555 1556 case hwmon_in_crit_alarm: 1557 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 1558 if (err < 0) 1559 return err; 1560 1561 *value = !!(status & SFP_ALARM0_VCC_HIGH); 1562 return 0; 1563 default: 1564 return -EOPNOTSUPP; 1565 } 1566 1567 return -EOPNOTSUPP; 1568 } 1569 1570 static int sfp_hwmon_bias(struct sfp *sfp, u32 attr, long *value) 1571 { 1572 u8 status; 1573 int err; 1574 1575 switch (attr) { 1576 case hwmon_curr_input: 1577 return sfp_hwmon_read_bias(sfp, SFP_TX_BIAS, value); 1578 1579 case hwmon_curr_lcrit: 1580 *value = be16_to_cpu(sfp->diag.bias_low_alarm); 1581 sfp_hwmon_calibrate_bias(sfp, value); 1582 return 0; 1583 1584 case hwmon_curr_min: 1585 *value = be16_to_cpu(sfp->diag.bias_low_warn); 1586 sfp_hwmon_calibrate_bias(sfp, value); 1587 return 0; 1588 1589 case hwmon_curr_max: 1590 *value = be16_to_cpu(sfp->diag.bias_high_warn); 1591 sfp_hwmon_calibrate_bias(sfp, value); 1592 return 0; 1593 1594 case hwmon_curr_crit: 1595 *value = be16_to_cpu(sfp->diag.bias_high_alarm); 1596 sfp_hwmon_calibrate_bias(sfp, value); 1597 return 0; 1598 1599 case hwmon_curr_lcrit_alarm: 1600 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 1601 if (err < 0) 1602 return err; 1603 1604 *value = !!(status & SFP_ALARM0_TX_BIAS_LOW); 1605 return 0; 1606 1607 case hwmon_curr_min_alarm: 1608 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 1609 if (err < 0) 1610 return err; 1611 1612 *value = !!(status & SFP_WARN0_TX_BIAS_LOW); 1613 return 0; 1614 1615 case hwmon_curr_max_alarm: 1616 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 1617 if (err < 0) 1618 return err; 1619 1620 *value = !!(status & SFP_WARN0_TX_BIAS_HIGH); 1621 return 0; 1622 1623 case hwmon_curr_crit_alarm: 1624 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 1625 if (err < 0) 1626 return err; 1627 1628 *value = !!(status & SFP_ALARM0_TX_BIAS_HIGH); 1629 return 0; 1630 default: 1631 return -EOPNOTSUPP; 1632 } 1633 1634 return -EOPNOTSUPP; 1635 } 1636 1637 static int sfp_hwmon_tx_power(struct sfp *sfp, u32 attr, long *value) 1638 { 1639 u8 status; 1640 int err; 1641 1642 switch (attr) { 1643 case hwmon_power_input: 1644 return sfp_hwmon_read_tx_power(sfp, SFP_TX_POWER, value); 1645 1646 case hwmon_power_lcrit: 1647 *value = be16_to_cpu(sfp->diag.txpwr_low_alarm); 1648 sfp_hwmon_calibrate_tx_power(sfp, value); 1649 return 0; 1650 1651 case hwmon_power_min: 1652 *value = be16_to_cpu(sfp->diag.txpwr_low_warn); 1653 sfp_hwmon_calibrate_tx_power(sfp, value); 1654 return 0; 1655 1656 case hwmon_power_max: 1657 *value = be16_to_cpu(sfp->diag.txpwr_high_warn); 1658 sfp_hwmon_calibrate_tx_power(sfp, value); 1659 return 0; 1660 1661 case hwmon_power_crit: 1662 *value = be16_to_cpu(sfp->diag.txpwr_high_alarm); 1663 sfp_hwmon_calibrate_tx_power(sfp, value); 1664 return 0; 1665 1666 case hwmon_power_lcrit_alarm: 1667 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 1668 if (err < 0) 1669 return err; 1670 1671 *value = !!(status & SFP_ALARM0_TXPWR_LOW); 1672 return 0; 1673 1674 case hwmon_power_min_alarm: 1675 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 1676 if (err < 0) 1677 return err; 1678 1679 *value = !!(status & SFP_WARN0_TXPWR_LOW); 1680 return 0; 1681 1682 case hwmon_power_max_alarm: 1683 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 1684 if (err < 0) 1685 return err; 1686 1687 *value = !!(status & SFP_WARN0_TXPWR_HIGH); 1688 return 0; 1689 1690 case hwmon_power_crit_alarm: 1691 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 1692 if (err < 0) 1693 return err; 1694 1695 *value = !!(status & SFP_ALARM0_TXPWR_HIGH); 1696 return 0; 1697 default: 1698 return -EOPNOTSUPP; 1699 } 1700 1701 return -EOPNOTSUPP; 1702 } 1703 1704 static int sfp_hwmon_rx_power(struct sfp *sfp, u32 attr, long *value) 1705 { 1706 u8 status; 1707 int err; 1708 1709 switch (attr) { 1710 case hwmon_power_input: 1711 return sfp_hwmon_read_rx_power(sfp, SFP_RX_POWER, value); 1712 1713 case hwmon_power_lcrit: 1714 *value = be16_to_cpu(sfp->diag.rxpwr_low_alarm); 1715 sfp_hwmon_to_rx_power(value); 1716 return 0; 1717 1718 case hwmon_power_min: 1719 *value = be16_to_cpu(sfp->diag.rxpwr_low_warn); 1720 sfp_hwmon_to_rx_power(value); 1721 return 0; 1722 1723 case hwmon_power_max: 1724 *value = be16_to_cpu(sfp->diag.rxpwr_high_warn); 1725 sfp_hwmon_to_rx_power(value); 1726 return 0; 1727 1728 case hwmon_power_crit: 1729 *value = be16_to_cpu(sfp->diag.rxpwr_high_alarm); 1730 sfp_hwmon_to_rx_power(value); 1731 return 0; 1732 1733 case hwmon_power_lcrit_alarm: 1734 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status)); 1735 if (err < 0) 1736 return err; 1737 1738 *value = !!(status & SFP_ALARM1_RXPWR_LOW); 1739 return 0; 1740 1741 case hwmon_power_min_alarm: 1742 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status)); 1743 if (err < 0) 1744 return err; 1745 1746 *value = !!(status & SFP_WARN1_RXPWR_LOW); 1747 return 0; 1748 1749 case hwmon_power_max_alarm: 1750 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status)); 1751 if (err < 0) 1752 return err; 1753 1754 *value = !!(status & SFP_WARN1_RXPWR_HIGH); 1755 return 0; 1756 1757 case hwmon_power_crit_alarm: 1758 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status)); 1759 if (err < 0) 1760 return err; 1761 1762 *value = !!(status & SFP_ALARM1_RXPWR_HIGH); 1763 return 0; 1764 default: 1765 return -EOPNOTSUPP; 1766 } 1767 1768 return -EOPNOTSUPP; 1769 } 1770 1771 static int sfp_hwmon_read(struct device *dev, enum hwmon_sensor_types type, 1772 u32 attr, int channel, long *value) 1773 { 1774 struct sfp *sfp = dev_get_drvdata(dev); 1775 1776 switch (type) { 1777 case hwmon_temp: 1778 return sfp_hwmon_temp(sfp, attr, value); 1779 case hwmon_in: 1780 return sfp_hwmon_vcc(sfp, attr, value); 1781 case hwmon_curr: 1782 return sfp_hwmon_bias(sfp, attr, value); 1783 case hwmon_power: 1784 switch (channel) { 1785 case 0: 1786 return sfp_hwmon_tx_power(sfp, attr, value); 1787 case 1: 1788 return sfp_hwmon_rx_power(sfp, attr, value); 1789 default: 1790 return -EOPNOTSUPP; 1791 } 1792 default: 1793 return -EOPNOTSUPP; 1794 } 1795 } 1796 1797 static const char *const sfp_hwmon_power_labels[] = { 1798 "TX_power", 1799 "RX_power", 1800 }; 1801 1802 static int sfp_hwmon_read_string(struct device *dev, 1803 enum hwmon_sensor_types type, 1804 u32 attr, int channel, const char **str) 1805 { 1806 switch (type) { 1807 case hwmon_curr: 1808 switch (attr) { 1809 case hwmon_curr_label: 1810 *str = "bias"; 1811 return 0; 1812 default: 1813 return -EOPNOTSUPP; 1814 } 1815 break; 1816 case hwmon_temp: 1817 switch (attr) { 1818 case hwmon_temp_label: 1819 *str = "temperature"; 1820 return 0; 1821 default: 1822 return -EOPNOTSUPP; 1823 } 1824 break; 1825 case hwmon_in: 1826 switch (attr) { 1827 case hwmon_in_label: 1828 *str = "VCC"; 1829 return 0; 1830 default: 1831 return -EOPNOTSUPP; 1832 } 1833 break; 1834 case hwmon_power: 1835 switch (attr) { 1836 case hwmon_power_label: 1837 *str = sfp_hwmon_power_labels[channel]; 1838 return 0; 1839 default: 1840 return -EOPNOTSUPP; 1841 } 1842 break; 1843 default: 1844 return -EOPNOTSUPP; 1845 } 1846 1847 return -EOPNOTSUPP; 1848 } 1849 1850 static const struct hwmon_ops sfp_hwmon_ops = { 1851 .is_visible = sfp_hwmon_is_visible, 1852 .read = sfp_hwmon_read, 1853 .read_string = sfp_hwmon_read_string, 1854 }; 1855 1856 static const struct hwmon_channel_info * const sfp_hwmon_info[] = { 1857 HWMON_CHANNEL_INFO(chip, 1858 HWMON_C_REGISTER_TZ), 1859 HWMON_CHANNEL_INFO(in, 1860 HWMON_I_INPUT | 1861 HWMON_I_MAX | HWMON_I_MIN | 1862 HWMON_I_MAX_ALARM | HWMON_I_MIN_ALARM | 1863 HWMON_I_CRIT | HWMON_I_LCRIT | 1864 HWMON_I_CRIT_ALARM | HWMON_I_LCRIT_ALARM | 1865 HWMON_I_LABEL), 1866 HWMON_CHANNEL_INFO(temp, 1867 HWMON_T_INPUT | 1868 HWMON_T_MAX | HWMON_T_MIN | 1869 HWMON_T_MAX_ALARM | HWMON_T_MIN_ALARM | 1870 HWMON_T_CRIT | HWMON_T_LCRIT | 1871 HWMON_T_CRIT_ALARM | HWMON_T_LCRIT_ALARM | 1872 HWMON_T_LABEL), 1873 HWMON_CHANNEL_INFO(curr, 1874 HWMON_C_INPUT | 1875 HWMON_C_MAX | HWMON_C_MIN | 1876 HWMON_C_MAX_ALARM | HWMON_C_MIN_ALARM | 1877 HWMON_C_CRIT | HWMON_C_LCRIT | 1878 HWMON_C_CRIT_ALARM | HWMON_C_LCRIT_ALARM | 1879 HWMON_C_LABEL), 1880 HWMON_CHANNEL_INFO(power, 1881 /* Transmit power */ 1882 HWMON_P_INPUT | 1883 HWMON_P_MAX | HWMON_P_MIN | 1884 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM | 1885 HWMON_P_CRIT | HWMON_P_LCRIT | 1886 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM | 1887 HWMON_P_LABEL, 1888 /* Receive power */ 1889 HWMON_P_INPUT | 1890 HWMON_P_MAX | HWMON_P_MIN | 1891 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM | 1892 HWMON_P_CRIT | HWMON_P_LCRIT | 1893 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM | 1894 HWMON_P_LABEL), 1895 NULL, 1896 }; 1897 1898 static const struct hwmon_chip_info sfp_hwmon_chip_info = { 1899 .ops = &sfp_hwmon_ops, 1900 .info = sfp_hwmon_info, 1901 }; 1902 1903 static void sfp_hwmon_probe(struct work_struct *work) 1904 { 1905 struct sfp *sfp = container_of(work, struct sfp, hwmon_probe.work); 1906 int err; 1907 1908 /* hwmon interface needs to access 16bit registers in atomic way to 1909 * guarantee coherency of the diagnostic monitoring data. If it is not 1910 * possible to guarantee coherency because EEPROM is broken in such way 1911 * that does not support atomic 16bit read operation then we have to 1912 * skip registration of hwmon device. 1913 */ 1914 if (sfp->i2c_block_size < 2) { 1915 dev_info(sfp->dev, 1916 "skipping hwmon device registration\n"); 1917 dev_info(sfp->dev, 1918 "diagnostic EEPROM area cannot be read atomically to guarantee data coherency\n"); 1919 return; 1920 } 1921 1922 err = sfp_read(sfp, true, 0, &sfp->diag, sizeof(sfp->diag)); 1923 if (err < 0) { 1924 if (sfp->hwmon_tries--) { 1925 mod_delayed_work(system_percpu_wq, &sfp->hwmon_probe, 1926 T_PROBE_RETRY_SLOW); 1927 } else { 1928 dev_warn(sfp->dev, "hwmon probe failed: %pe\n", 1929 ERR_PTR(err)); 1930 } 1931 return; 1932 } 1933 1934 sfp->hwmon_name = hwmon_sanitize_name(dev_name(sfp->dev)); 1935 if (IS_ERR(sfp->hwmon_name)) { 1936 dev_err(sfp->dev, "out of memory for hwmon name\n"); 1937 return; 1938 } 1939 1940 sfp->hwmon_dev = hwmon_device_register_with_info(sfp->dev, 1941 sfp->hwmon_name, sfp, 1942 &sfp_hwmon_chip_info, 1943 NULL); 1944 if (IS_ERR(sfp->hwmon_dev)) 1945 dev_err(sfp->dev, "failed to register hwmon device: %ld\n", 1946 PTR_ERR(sfp->hwmon_dev)); 1947 } 1948 1949 static int sfp_hwmon_insert(struct sfp *sfp) 1950 { 1951 if (sfp->have_a2 && sfp->id.ext.diagmon & SFP_DIAGMON_DDM) { 1952 mod_delayed_work(system_percpu_wq, &sfp->hwmon_probe, 1); 1953 sfp->hwmon_tries = R_PROBE_RETRY_SLOW; 1954 } 1955 1956 return 0; 1957 } 1958 1959 static void sfp_hwmon_remove(struct sfp *sfp) 1960 { 1961 cancel_delayed_work_sync(&sfp->hwmon_probe); 1962 if (!IS_ERR_OR_NULL(sfp->hwmon_dev)) { 1963 hwmon_device_unregister(sfp->hwmon_dev); 1964 sfp->hwmon_dev = NULL; 1965 } 1966 1967 if (!IS_ERR_OR_NULL(sfp->hwmon_name)) { 1968 kfree(sfp->hwmon_name); 1969 sfp->hwmon_name = NULL; 1970 } 1971 } 1972 1973 static int sfp_hwmon_init(struct sfp *sfp) 1974 { 1975 INIT_DELAYED_WORK(&sfp->hwmon_probe, sfp_hwmon_probe); 1976 1977 return 0; 1978 } 1979 1980 static void sfp_hwmon_exit(struct sfp *sfp) 1981 { 1982 cancel_delayed_work_sync(&sfp->hwmon_probe); 1983 } 1984 #else 1985 static int sfp_hwmon_insert(struct sfp *sfp) 1986 { 1987 return 0; 1988 } 1989 1990 static void sfp_hwmon_remove(struct sfp *sfp) 1991 { 1992 } 1993 1994 static int sfp_hwmon_init(struct sfp *sfp) 1995 { 1996 return 0; 1997 } 1998 1999 static void sfp_hwmon_exit(struct sfp *sfp) 2000 { 2001 } 2002 #endif 2003 2004 /* Helpers */ 2005 static void sfp_module_tx_disable(struct sfp *sfp) 2006 { 2007 dev_dbg(sfp->dev, "tx disable %u -> %u\n", 2008 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1); 2009 sfp_mod_state(sfp, SFP_F_TX_DISABLE, SFP_F_TX_DISABLE); 2010 } 2011 2012 static void sfp_module_tx_enable(struct sfp *sfp) 2013 { 2014 dev_dbg(sfp->dev, "tx disable %u -> %u\n", 2015 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0); 2016 sfp_mod_state(sfp, SFP_F_TX_DISABLE, 0); 2017 } 2018 2019 #if IS_ENABLED(CONFIG_DEBUG_FS) 2020 static int sfp_debug_state_show(struct seq_file *s, void *data) 2021 { 2022 struct sfp *sfp = s->private; 2023 2024 seq_printf(s, "Module state: %s\n", 2025 mod_state_to_str(sfp->sm_mod_state)); 2026 seq_printf(s, "Module probe attempts: %d %d\n", 2027 R_PROBE_RETRY_INIT - sfp->sm_mod_tries_init, 2028 R_PROBE_RETRY_SLOW - sfp->sm_mod_tries); 2029 seq_printf(s, "Device state: %s\n", 2030 dev_state_to_str(sfp->sm_dev_state)); 2031 seq_printf(s, "Main state: %s\n", 2032 sm_state_to_str(sfp->sm_state)); 2033 seq_printf(s, "Fault recovery remaining retries: %d\n", 2034 sfp->sm_fault_retries); 2035 seq_printf(s, "PHY probe remaining retries: %d\n", 2036 sfp->sm_phy_retries); 2037 seq_printf(s, "Signalling rate: %u kBd\n", sfp->rate_kbd); 2038 seq_printf(s, "Rate select threshold: %u kBd\n", 2039 sfp->rs_threshold_kbd); 2040 seq_printf(s, "moddef0: %d\n", !!(sfp->state & SFP_F_PRESENT)); 2041 seq_printf(s, "rx_los: %d\n", !!(sfp->state & SFP_F_LOS)); 2042 seq_printf(s, "tx_fault: %d\n", !!(sfp->state & SFP_F_TX_FAULT)); 2043 seq_printf(s, "tx_disable: %d\n", !!(sfp->state & SFP_F_TX_DISABLE)); 2044 seq_printf(s, "rs0: %d\n", !!(sfp->state & SFP_F_RS0)); 2045 seq_printf(s, "rs1: %d\n", !!(sfp->state & SFP_F_RS1)); 2046 return 0; 2047 } 2048 DEFINE_SHOW_ATTRIBUTE(sfp_debug_state); 2049 2050 static void sfp_debugfs_init(struct sfp *sfp) 2051 { 2052 sfp->debugfs_dir = debugfs_create_dir(dev_name(sfp->dev), NULL); 2053 2054 debugfs_create_file("state", 0600, sfp->debugfs_dir, sfp, 2055 &sfp_debug_state_fops); 2056 } 2057 2058 static void sfp_debugfs_exit(struct sfp *sfp) 2059 { 2060 debugfs_remove_recursive(sfp->debugfs_dir); 2061 } 2062 #else 2063 static void sfp_debugfs_init(struct sfp *sfp) 2064 { 2065 } 2066 2067 static void sfp_debugfs_exit(struct sfp *sfp) 2068 { 2069 } 2070 #endif 2071 2072 static void sfp_module_tx_fault_reset(struct sfp *sfp) 2073 { 2074 unsigned int state; 2075 2076 mutex_lock(&sfp->st_mutex); 2077 state = sfp->state; 2078 if (!(state & SFP_F_TX_DISABLE)) { 2079 sfp_set_state(sfp, state | SFP_F_TX_DISABLE); 2080 2081 udelay(T_RESET_US); 2082 2083 sfp_set_state(sfp, state); 2084 } 2085 mutex_unlock(&sfp->st_mutex); 2086 } 2087 2088 /* SFP state machine */ 2089 static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout) 2090 { 2091 if (timeout) 2092 mod_delayed_work(system_power_efficient_wq, &sfp->timeout, 2093 timeout); 2094 else 2095 cancel_delayed_work(&sfp->timeout); 2096 } 2097 2098 static void sfp_sm_next(struct sfp *sfp, unsigned int state, 2099 unsigned int timeout) 2100 { 2101 sfp->sm_state = state; 2102 sfp_sm_set_timer(sfp, timeout); 2103 } 2104 2105 static void sfp_sm_mod_next(struct sfp *sfp, unsigned int state, 2106 unsigned int timeout) 2107 { 2108 sfp->sm_mod_state = state; 2109 sfp_sm_set_timer(sfp, timeout); 2110 } 2111 2112 static void sfp_sm_phy_detach(struct sfp *sfp) 2113 { 2114 sfp_remove_phy(sfp->sfp_bus); 2115 phy_device_remove(sfp->mod_phy); 2116 phy_device_free(sfp->mod_phy); 2117 sfp->mod_phy = NULL; 2118 } 2119 2120 static int sfp_sm_probe_phy(struct sfp *sfp, int addr, bool is_c45) 2121 { 2122 struct phy_device *phy; 2123 int err; 2124 2125 phy = get_phy_device(sfp->i2c_mii, addr, is_c45); 2126 if (phy == ERR_PTR(-ENODEV)) 2127 return PTR_ERR(phy); 2128 if (IS_ERR(phy)) { 2129 dev_err(sfp->dev, "mdiobus scan returned %pe\n", phy); 2130 return PTR_ERR(phy); 2131 } 2132 2133 /* Mark this PHY as being on a SFP module */ 2134 phy->is_on_sfp_module = true; 2135 2136 err = phy_device_register(phy); 2137 if (err) { 2138 phy_device_free(phy); 2139 dev_err(sfp->dev, "phy_device_register failed: %pe\n", 2140 ERR_PTR(err)); 2141 return err; 2142 } 2143 2144 err = sfp_add_phy(sfp->sfp_bus, phy); 2145 if (err) { 2146 phy_device_remove(phy); 2147 phy_device_free(phy); 2148 dev_err(sfp->dev, "sfp_add_phy failed: %pe\n", ERR_PTR(err)); 2149 return err; 2150 } 2151 2152 sfp->mod_phy = phy; 2153 2154 return 0; 2155 } 2156 2157 static void sfp_sm_link_up(struct sfp *sfp) 2158 { 2159 sfp_link_up(sfp->sfp_bus); 2160 sfp_sm_next(sfp, SFP_S_LINK_UP, 0); 2161 } 2162 2163 static void sfp_sm_link_down(struct sfp *sfp) 2164 { 2165 sfp_link_down(sfp->sfp_bus); 2166 } 2167 2168 static void sfp_sm_link_check_los(struct sfp *sfp) 2169 { 2170 const __be16 los_inverted = cpu_to_be16(SFP_OPTIONS_LOS_INVERTED); 2171 const __be16 los_normal = cpu_to_be16(SFP_OPTIONS_LOS_NORMAL); 2172 __be16 los_options = sfp->id.ext.options & (los_inverted | los_normal); 2173 bool los = false; 2174 2175 /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL 2176 * are set, we assume that no LOS signal is available. If both are 2177 * set, we assume LOS is not implemented (and is meaningless.) 2178 */ 2179 if (los_options == los_inverted) 2180 los = !(sfp->state & SFP_F_LOS); 2181 else if (los_options == los_normal) 2182 los = !!(sfp->state & SFP_F_LOS); 2183 2184 if (los) 2185 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0); 2186 else 2187 sfp_sm_link_up(sfp); 2188 } 2189 2190 static bool sfp_los_event_active(struct sfp *sfp, unsigned int event) 2191 { 2192 const __be16 los_inverted = cpu_to_be16(SFP_OPTIONS_LOS_INVERTED); 2193 const __be16 los_normal = cpu_to_be16(SFP_OPTIONS_LOS_NORMAL); 2194 __be16 los_options = sfp->id.ext.options & (los_inverted | los_normal); 2195 2196 return (los_options == los_inverted && event == SFP_E_LOS_LOW) || 2197 (los_options == los_normal && event == SFP_E_LOS_HIGH); 2198 } 2199 2200 static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event) 2201 { 2202 const __be16 los_inverted = cpu_to_be16(SFP_OPTIONS_LOS_INVERTED); 2203 const __be16 los_normal = cpu_to_be16(SFP_OPTIONS_LOS_NORMAL); 2204 __be16 los_options = sfp->id.ext.options & (los_inverted | los_normal); 2205 2206 return (los_options == los_inverted && event == SFP_E_LOS_HIGH) || 2207 (los_options == los_normal && event == SFP_E_LOS_LOW); 2208 } 2209 2210 static void sfp_sm_fault(struct sfp *sfp, unsigned int next_state, bool warn) 2211 { 2212 if (sfp->sm_fault_retries && !--sfp->sm_fault_retries) { 2213 dev_err(sfp->dev, 2214 "module persistently indicates fault, disabling\n"); 2215 sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0); 2216 } else { 2217 if (warn) 2218 dev_err(sfp->dev, "module transmit fault indicated\n"); 2219 2220 sfp_sm_next(sfp, next_state, T_FAULT_RECOVER); 2221 } 2222 } 2223 2224 static int sfp_sm_add_mdio_bus(struct sfp *sfp) 2225 { 2226 if (sfp->mdio_protocol != MDIO_I2C_NONE) 2227 return sfp_i2c_mdiobus_create(sfp); 2228 2229 return 0; 2230 } 2231 2232 /* Probe a SFP for a PHY device if the module supports copper - the PHY 2233 * normally sits at I2C bus address 0x56, and may either be a clause 22 2234 * or clause 45 PHY. 2235 * 2236 * Clause 22 copper SFP modules normally operate in Cisco SGMII mode with 2237 * negotiation enabled, but some may be in 1000base-X - which is for the 2238 * PHY driver to determine. 2239 * 2240 * Clause 45 copper SFP+ modules (10G) appear to switch their interface 2241 * mode according to the negotiated line speed. 2242 */ 2243 static int sfp_sm_probe_for_phy(struct sfp *sfp) 2244 { 2245 int err = 0; 2246 2247 switch (sfp->mdio_protocol) { 2248 case MDIO_I2C_NONE: 2249 break; 2250 2251 case MDIO_I2C_MARVELL_C22: 2252 err = sfp_sm_probe_phy(sfp, SFP_PHY_ADDR, false); 2253 break; 2254 2255 case MDIO_I2C_C45: 2256 err = sfp_sm_probe_phy(sfp, SFP_PHY_ADDR, true); 2257 break; 2258 2259 case MDIO_I2C_ROLLBALL: 2260 err = sfp_sm_probe_phy(sfp, SFP_PHY_ADDR_ROLLBALL, true); 2261 break; 2262 } 2263 2264 return err; 2265 } 2266 2267 static int sfp_module_parse_power(struct sfp *sfp) 2268 { 2269 u32 power_mW = 1000; 2270 bool supports_a2; 2271 2272 if (sfp->id.ext.sff8472_compliance >= SFP_SFF8472_COMPLIANCE_REV10_2 && 2273 sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL)) 2274 power_mW = 1500; 2275 /* Added in Rev 11.9, but there is no compliance code for this */ 2276 if (sfp->id.ext.sff8472_compliance >= SFP_SFF8472_COMPLIANCE_REV11_4 && 2277 sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL)) 2278 power_mW = 2000; 2279 2280 /* Power level 1 modules (max. 1W) are always supported. */ 2281 if (power_mW <= 1000) { 2282 sfp->module_power_mW = power_mW; 2283 return 0; 2284 } 2285 2286 supports_a2 = sfp->id.ext.sff8472_compliance != 2287 SFP_SFF8472_COMPLIANCE_NONE || 2288 sfp->id.ext.diagmon & SFP_DIAGMON_DDM; 2289 2290 if (power_mW > sfp->max_power_mW) { 2291 /* Module power specification exceeds the allowed maximum. */ 2292 if (!supports_a2) { 2293 /* The module appears not to implement bus address 2294 * 0xa2, so assume that the module powers up in the 2295 * indicated mode. 2296 */ 2297 dev_err(sfp->dev, 2298 "Host does not support %u.%uW modules\n", 2299 power_mW / 1000, (power_mW / 100) % 10); 2300 return -EINVAL; 2301 } else { 2302 dev_warn(sfp->dev, 2303 "Host does not support %u.%uW modules, module left in power mode 1\n", 2304 power_mW / 1000, (power_mW / 100) % 10); 2305 return 0; 2306 } 2307 } 2308 2309 if (!supports_a2) { 2310 /* The module power level is below the host maximum and the 2311 * module appears not to implement bus address 0xa2, so assume 2312 * that the module powers up in the indicated mode. 2313 */ 2314 return 0; 2315 } 2316 2317 /* If the module requires a higher power mode, but also requires 2318 * an address change sequence, warn the user that the module may 2319 * not be functional. 2320 */ 2321 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE) { 2322 dev_warn(sfp->dev, 2323 "Address Change Sequence not supported but module requires %u.%uW, module may not be functional\n", 2324 power_mW / 1000, (power_mW / 100) % 10); 2325 return 0; 2326 } 2327 2328 sfp->module_power_mW = power_mW; 2329 2330 return 0; 2331 } 2332 2333 static int sfp_sm_mod_hpower(struct sfp *sfp, bool enable) 2334 { 2335 int err; 2336 2337 err = sfp_modify_u8(sfp, true, SFP_EXT_STATUS, 2338 SFP_EXT_STATUS_PWRLVL_SELECT, 2339 enable ? SFP_EXT_STATUS_PWRLVL_SELECT : 0); 2340 if (err != sizeof(u8)) { 2341 dev_err(sfp->dev, "failed to %sable high power: %pe\n", 2342 enable ? "en" : "dis", ERR_PTR(err)); 2343 return -EAGAIN; 2344 } 2345 2346 if (enable) 2347 dev_info(sfp->dev, "Module switched to %u.%uW power level\n", 2348 sfp->module_power_mW / 1000, 2349 (sfp->module_power_mW / 100) % 10); 2350 2351 return 0; 2352 } 2353 2354 static void sfp_module_parse_rate_select(struct sfp *sfp) 2355 { 2356 u8 rate_id; 2357 2358 sfp->rs_threshold_kbd = 0; 2359 sfp->rs_state_mask = 0; 2360 2361 if (!(sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_RATE_SELECT))) 2362 /* No support for RateSelect */ 2363 return; 2364 2365 /* Default to INF-8074 RateSelect operation. The signalling threshold 2366 * rate is not well specified, so always select "Full Bandwidth", but 2367 * SFF-8079 reveals that it is understood that RS0 will be low for 2368 * 1.0625Gb/s and high for 2.125Gb/s. Choose a value half-way between. 2369 * This method exists prior to SFF-8472. 2370 */ 2371 sfp->rs_state_mask = SFP_F_RS0; 2372 sfp->rs_threshold_kbd = 1594; 2373 2374 /* Parse the rate identifier, which is complicated due to history: 2375 * SFF-8472 rev 9.5 marks this field as reserved. 2376 * SFF-8079 references SFF-8472 rev 9.5 and defines bit 0. SFF-8472 2377 * compliance is not required. 2378 * SFF-8472 rev 10.2 defines this field using values 0..4 2379 * SFF-8472 rev 11.0 redefines this field with bit 0 for SFF-8079 2380 * and even values. 2381 */ 2382 rate_id = sfp->id.base.rate_id; 2383 if (rate_id == 0) 2384 /* Unspecified */ 2385 return; 2386 2387 /* SFF-8472 rev 10.0..10.4 did not account for SFF-8079 using bit 0, 2388 * and allocated value 3 to SFF-8431 independent tx/rx rate select. 2389 * Convert this to a SFF-8472 rev 11.0 rate identifier. 2390 */ 2391 if (sfp->id.ext.sff8472_compliance >= SFP_SFF8472_COMPLIANCE_REV10_2 && 2392 sfp->id.ext.sff8472_compliance < SFP_SFF8472_COMPLIANCE_REV11_0 && 2393 rate_id == 3) 2394 rate_id = SFF_RID_8431; 2395 2396 if (rate_id & SFF_RID_8079) { 2397 /* SFF-8079 RateSelect / Application Select in conjunction with 2398 * SFF-8472 rev 9.5. SFF-8079 defines rate_id as a bitfield 2399 * with only bit 0 used, which takes precedence over SFF-8472. 2400 */ 2401 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_APP_SELECT_SFF8079)) { 2402 /* SFF-8079 Part 1 - rate selection between Fibre 2403 * Channel 1.0625/2.125/4.25 Gbd modes. Note that RS0 2404 * is high for 2125, so we have to subtract 1 to 2405 * include it. 2406 */ 2407 sfp->rs_threshold_kbd = 2125 - 1; 2408 sfp->rs_state_mask = SFP_F_RS0; 2409 } 2410 return; 2411 } 2412 2413 /* SFF-8472 rev 9.5 does not define the rate identifier */ 2414 if (sfp->id.ext.sff8472_compliance <= SFP_SFF8472_COMPLIANCE_REV9_5) 2415 return; 2416 2417 /* SFF-8472 rev 11.0 defines rate_id as a numerical value which will 2418 * always have bit 0 clear due to SFF-8079's bitfield usage of rate_id. 2419 */ 2420 switch (rate_id) { 2421 case SFF_RID_8431_RX_ONLY: 2422 sfp->rs_threshold_kbd = 4250; 2423 sfp->rs_state_mask = SFP_F_RS0; 2424 break; 2425 2426 case SFF_RID_8431_TX_ONLY: 2427 sfp->rs_threshold_kbd = 4250; 2428 sfp->rs_state_mask = SFP_F_RS1; 2429 break; 2430 2431 case SFF_RID_8431: 2432 sfp->rs_threshold_kbd = 4250; 2433 sfp->rs_state_mask = SFP_F_RS0 | SFP_F_RS1; 2434 break; 2435 2436 case SFF_RID_10G8G: 2437 sfp->rs_threshold_kbd = 9000; 2438 sfp->rs_state_mask = SFP_F_RS0 | SFP_F_RS1; 2439 break; 2440 } 2441 } 2442 2443 /* GPON modules based on Realtek RTL8672 and RTL9601C chips (e.g. V-SOL 2444 * V2801F, CarlitoxxPro CPGOS03-0490, Ubiquiti U-Fiber Instant, ...) do 2445 * not support multibyte reads from the EEPROM. Each multi-byte read 2446 * operation returns just one byte of EEPROM followed by zeros. There is 2447 * no way to identify which modules are using Realtek RTL8672 and RTL9601C 2448 * chips. Moreover every OEM of V-SOL V2801F module puts its own vendor 2449 * name and vendor id into EEPROM, so there is even no way to detect if 2450 * module is V-SOL V2801F. Therefore check for those zeros in the read 2451 * data and then based on check switch to reading EEPROM to one byte 2452 * at a time. 2453 */ 2454 static bool sfp_id_needs_byte_io(struct sfp *sfp, void *buf, size_t len) 2455 { 2456 size_t i, block_size = sfp->i2c_block_size; 2457 2458 /* Already using byte IO */ 2459 if (block_size == 1) 2460 return false; 2461 2462 for (i = 1; i < len; i += block_size) { 2463 if (memchr_inv(buf + i, '\0', min(block_size - 1, len - i))) 2464 return false; 2465 } 2466 return true; 2467 } 2468 2469 static int sfp_cotsworks_fixup_check(struct sfp *sfp, struct sfp_eeprom_id *id) 2470 { 2471 u8 check; 2472 int err; 2473 2474 if (id->base.phys_id != SFF8024_ID_SFF_8472 || 2475 id->base.phys_ext_id != SFP_PHYS_EXT_ID_SFP || 2476 id->base.connector != SFF8024_CONNECTOR_LC) { 2477 dev_warn(sfp->dev, "Rewriting fiber module EEPROM with corrected values\n"); 2478 id->base.phys_id = SFF8024_ID_SFF_8472; 2479 id->base.phys_ext_id = SFP_PHYS_EXT_ID_SFP; 2480 id->base.connector = SFF8024_CONNECTOR_LC; 2481 err = sfp_write(sfp, false, SFP_PHYS_ID, &id->base, 3); 2482 if (err != 3) { 2483 dev_err(sfp->dev, 2484 "Failed to rewrite module EEPROM: %pe\n", 2485 ERR_PTR(err)); 2486 return err; 2487 } 2488 2489 /* Cotsworks modules have been found to require a delay between write operations. */ 2490 mdelay(50); 2491 2492 /* Update base structure checksum */ 2493 check = sfp_check(&id->base, sizeof(id->base) - 1); 2494 err = sfp_write(sfp, false, SFP_CC_BASE, &check, 1); 2495 if (err != 1) { 2496 dev_err(sfp->dev, 2497 "Failed to update base structure checksum in fiber module EEPROM: %pe\n", 2498 ERR_PTR(err)); 2499 return err; 2500 } 2501 } 2502 return 0; 2503 } 2504 2505 static int sfp_module_parse_sff8472(struct sfp *sfp) 2506 { 2507 /* If the module requires address swap mode, warn about it */ 2508 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE) 2509 dev_warn(sfp->dev, 2510 "module address swap to access page 0xA2 is not supported.\n"); 2511 else 2512 sfp->have_a2 = true; 2513 2514 return 0; 2515 } 2516 2517 static int sfp_sm_mod_probe(struct sfp *sfp, bool report) 2518 { 2519 /* SFP module inserted - read I2C data */ 2520 struct sfp_eeprom_id id; 2521 bool cotsworks_sfbg; 2522 unsigned int mask; 2523 bool cotsworks; 2524 u8 check; 2525 int ret; 2526 2527 sfp->i2c_block_size = sfp->i2c_max_block_size; 2528 2529 ret = sfp_read(sfp, false, 0, &id.base, sizeof(id.base)); 2530 if (ret < 0) { 2531 if (report) 2532 dev_err(sfp->dev, "failed to read EEPROM: %pe\n", 2533 ERR_PTR(ret)); 2534 return -EAGAIN; 2535 } 2536 2537 if (ret != sizeof(id.base)) { 2538 dev_err(sfp->dev, "EEPROM short read: %pe\n", ERR_PTR(ret)); 2539 return -EAGAIN; 2540 } 2541 2542 /* Some SFP modules (e.g. Nokia 3FE46541AA) lock up if read from 2543 * address 0x51 is just one byte at a time. Also SFF-8472 requires 2544 * that EEPROM supports atomic 16bit read operation for diagnostic 2545 * fields, so do not switch to one byte reading at a time unless it 2546 * is really required and we have no other option. 2547 */ 2548 if (sfp_id_needs_byte_io(sfp, &id.base, sizeof(id.base))) { 2549 dev_info(sfp->dev, 2550 "Detected broken RTL8672/RTL9601C emulated EEPROM\n"); 2551 dev_info(sfp->dev, 2552 "Switching to reading EEPROM to one byte at a time\n"); 2553 sfp->i2c_block_size = 1; 2554 2555 ret = sfp_read(sfp, false, 0, &id.base, sizeof(id.base)); 2556 if (ret < 0) { 2557 if (report) 2558 dev_err(sfp->dev, 2559 "failed to read EEPROM: %pe\n", 2560 ERR_PTR(ret)); 2561 return -EAGAIN; 2562 } 2563 2564 if (ret != sizeof(id.base)) { 2565 dev_err(sfp->dev, "EEPROM short read: %pe\n", 2566 ERR_PTR(ret)); 2567 return -EAGAIN; 2568 } 2569 } 2570 2571 /* Cotsworks do not seem to update the checksums when they 2572 * do the final programming with the final module part number, 2573 * serial number and date code. 2574 */ 2575 cotsworks = !memcmp(id.base.vendor_name, "COTSWORKS ", 16); 2576 cotsworks_sfbg = !memcmp(id.base.vendor_pn, "SFBG", 4); 2577 2578 /* Cotsworks SFF module EEPROM do not always have valid phys_id, 2579 * phys_ext_id, and connector bytes. Rewrite SFF EEPROM bytes if 2580 * Cotsworks PN matches and bytes are not correct. 2581 */ 2582 if (cotsworks && cotsworks_sfbg) { 2583 ret = sfp_cotsworks_fixup_check(sfp, &id); 2584 if (ret < 0) 2585 return ret; 2586 } 2587 2588 /* Validate the checksum over the base structure */ 2589 check = sfp_check(&id.base, sizeof(id.base) - 1); 2590 if (check != id.base.cc_base) { 2591 if (cotsworks) { 2592 dev_warn(sfp->dev, 2593 "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n", 2594 check, id.base.cc_base); 2595 } else { 2596 dev_err(sfp->dev, 2597 "EEPROM base structure checksum failure: 0x%02x != 0x%02x\n", 2598 check, id.base.cc_base); 2599 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET, 2600 16, 1, &id, sizeof(id), true); 2601 return -EINVAL; 2602 } 2603 } 2604 2605 ret = sfp_read(sfp, false, SFP_CC_BASE + 1, &id.ext, sizeof(id.ext)); 2606 if (ret < 0) { 2607 if (report) 2608 dev_err(sfp->dev, "failed to read EEPROM: %pe\n", 2609 ERR_PTR(ret)); 2610 return -EAGAIN; 2611 } 2612 2613 if (ret != sizeof(id.ext)) { 2614 dev_err(sfp->dev, "EEPROM short read: %pe\n", ERR_PTR(ret)); 2615 return -EAGAIN; 2616 } 2617 2618 check = sfp_check(&id.ext, sizeof(id.ext) - 1); 2619 if (check != id.ext.cc_ext) { 2620 if (cotsworks) { 2621 dev_warn(sfp->dev, 2622 "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n", 2623 check, id.ext.cc_ext); 2624 } else { 2625 dev_err(sfp->dev, 2626 "EEPROM extended structure checksum failure: 0x%02x != 0x%02x\n", 2627 check, id.ext.cc_ext); 2628 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET, 2629 16, 1, &id, sizeof(id), true); 2630 memset(&id.ext, 0, sizeof(id.ext)); 2631 } 2632 } 2633 2634 sfp->id = id; 2635 2636 dev_info(sfp->dev, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n", 2637 (int)sizeof(id.base.vendor_name), id.base.vendor_name, 2638 (int)sizeof(id.base.vendor_pn), id.base.vendor_pn, 2639 (int)sizeof(id.base.vendor_rev), id.base.vendor_rev, 2640 (int)sizeof(id.ext.vendor_sn), id.ext.vendor_sn, 2641 (int)sizeof(id.ext.datecode), id.ext.datecode); 2642 2643 /* Check whether we support this module */ 2644 if (!sfp->type->module_supported(&id)) { 2645 dev_err(sfp->dev, 2646 "module is not supported - phys id 0x%02x 0x%02x\n", 2647 sfp->id.base.phys_id, sfp->id.base.phys_ext_id); 2648 return -EINVAL; 2649 } 2650 2651 if (sfp->id.ext.sff8472_compliance != SFP_SFF8472_COMPLIANCE_NONE) { 2652 ret = sfp_module_parse_sff8472(sfp); 2653 if (ret < 0) 2654 return ret; 2655 } 2656 2657 /* Parse the module power requirement */ 2658 ret = sfp_module_parse_power(sfp); 2659 if (ret < 0) 2660 return ret; 2661 2662 sfp_module_parse_rate_select(sfp); 2663 2664 mask = SFP_F_PRESENT; 2665 if (sfp->gpio[GPIO_TX_DISABLE]) 2666 mask |= SFP_F_TX_DISABLE; 2667 if (sfp->gpio[GPIO_TX_FAULT]) 2668 mask |= SFP_F_TX_FAULT; 2669 if (sfp->gpio[GPIO_LOS]) 2670 mask |= SFP_F_LOS; 2671 if (sfp->gpio[GPIO_RS0]) 2672 mask |= SFP_F_RS0; 2673 if (sfp->gpio[GPIO_RS1]) 2674 mask |= SFP_F_RS1; 2675 2676 sfp->module_t_start_up = T_START_UP; 2677 sfp->module_t_wait = T_WAIT; 2678 sfp->phy_t_retry = T_PHY_RETRY; 2679 2680 sfp->state_ignore_mask = 0; 2681 2682 if (sfp->id.base.extended_cc == SFF8024_ECC_10GBASE_T_SFI || 2683 sfp->id.base.extended_cc == SFF8024_ECC_10GBASE_T_SR || 2684 sfp->id.base.extended_cc == SFF8024_ECC_5GBASE_T || 2685 sfp->id.base.extended_cc == SFF8024_ECC_2_5GBASE_T) 2686 sfp->mdio_protocol = MDIO_I2C_C45; 2687 else if (sfp->id.base.e1000_base_t) 2688 sfp->mdio_protocol = MDIO_I2C_MARVELL_C22; 2689 else 2690 sfp->mdio_protocol = MDIO_I2C_NONE; 2691 2692 sfp->quirk = sfp_lookup_quirk(&id); 2693 2694 mutex_lock(&sfp->st_mutex); 2695 /* Initialise state bits to use from hardware */ 2696 sfp->state_hw_mask = mask; 2697 2698 /* We want to drive the rate select pins that the module is using */ 2699 sfp->state_hw_drive |= sfp->rs_state_mask; 2700 2701 if (sfp->quirk && sfp->quirk->fixup) 2702 sfp->quirk->fixup(sfp); 2703 2704 sfp->state_hw_mask &= ~sfp->state_ignore_mask; 2705 mutex_unlock(&sfp->st_mutex); 2706 2707 return 0; 2708 } 2709 2710 static void sfp_sm_mod_remove(struct sfp *sfp) 2711 { 2712 if (sfp->sm_mod_state > SFP_MOD_WAITDEV) 2713 sfp_module_remove(sfp->sfp_bus); 2714 2715 sfp_hwmon_remove(sfp); 2716 2717 memset(&sfp->id, 0, sizeof(sfp->id)); 2718 sfp->module_power_mW = 0; 2719 sfp->state_hw_drive = SFP_F_TX_DISABLE; 2720 sfp->have_a2 = false; 2721 2722 dev_info(sfp->dev, "module removed\n"); 2723 } 2724 2725 /* This state machine tracks the upstream's state */ 2726 static void sfp_sm_device(struct sfp *sfp, unsigned int event) 2727 { 2728 switch (sfp->sm_dev_state) { 2729 default: 2730 if (event == SFP_E_DEV_ATTACH) 2731 sfp->sm_dev_state = SFP_DEV_DOWN; 2732 break; 2733 2734 case SFP_DEV_DOWN: 2735 if (event == SFP_E_DEV_DETACH) 2736 sfp->sm_dev_state = SFP_DEV_DETACHED; 2737 else if (event == SFP_E_DEV_UP) 2738 sfp->sm_dev_state = SFP_DEV_UP; 2739 break; 2740 2741 case SFP_DEV_UP: 2742 if (event == SFP_E_DEV_DETACH) 2743 sfp->sm_dev_state = SFP_DEV_DETACHED; 2744 else if (event == SFP_E_DEV_DOWN) 2745 sfp->sm_dev_state = SFP_DEV_DOWN; 2746 break; 2747 } 2748 } 2749 2750 /* This state machine tracks the insert/remove state of the module, probes 2751 * the on-board EEPROM, and sets up the power level. 2752 */ 2753 static void sfp_sm_module(struct sfp *sfp, unsigned int event) 2754 { 2755 int err; 2756 2757 /* Handle remove event globally, it resets this state machine */ 2758 if (event == SFP_E_REMOVE) { 2759 sfp_sm_mod_remove(sfp); 2760 sfp_sm_mod_next(sfp, SFP_MOD_EMPTY, 0); 2761 return; 2762 } 2763 2764 /* Handle device detach globally */ 2765 if (sfp->sm_dev_state < SFP_DEV_DOWN && 2766 sfp->sm_mod_state > SFP_MOD_WAITDEV) { 2767 if (sfp->module_power_mW > 1000 && 2768 sfp->sm_mod_state > SFP_MOD_HPOWER) 2769 sfp_sm_mod_hpower(sfp, false); 2770 sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0); 2771 return; 2772 } 2773 2774 switch (sfp->sm_mod_state) { 2775 default: 2776 if (event == SFP_E_INSERT) { 2777 sfp_sm_mod_next(sfp, SFP_MOD_PROBE, T_SERIAL); 2778 sfp->sm_mod_tries_init = R_PROBE_RETRY_INIT; 2779 sfp->sm_mod_tries = R_PROBE_RETRY_SLOW; 2780 } 2781 break; 2782 2783 case SFP_MOD_PROBE: 2784 /* Wait for T_PROBE_INIT to time out */ 2785 if (event != SFP_E_TIMEOUT) 2786 break; 2787 2788 err = sfp_sm_mod_probe(sfp, sfp->sm_mod_tries == 1); 2789 if (err == -EAGAIN) { 2790 if (sfp->sm_mod_tries_init && 2791 --sfp->sm_mod_tries_init) { 2792 sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT); 2793 break; 2794 } else if (sfp->sm_mod_tries && --sfp->sm_mod_tries) { 2795 if (sfp->sm_mod_tries == R_PROBE_RETRY_SLOW - 1) 2796 dev_warn(sfp->dev, 2797 "please wait, module slow to respond\n"); 2798 sfp_sm_set_timer(sfp, T_PROBE_RETRY_SLOW); 2799 break; 2800 } 2801 } 2802 if (err < 0) { 2803 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0); 2804 break; 2805 } 2806 2807 /* Force a poll to re-read the hardware signal state after 2808 * sfp_sm_mod_probe() changed state_hw_mask. 2809 */ 2810 mod_delayed_work(system_percpu_wq, &sfp->poll, 1); 2811 2812 err = sfp_hwmon_insert(sfp); 2813 if (err) 2814 dev_warn(sfp->dev, "hwmon probe failed: %pe\n", 2815 ERR_PTR(err)); 2816 2817 sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0); 2818 fallthrough; 2819 case SFP_MOD_WAITDEV: 2820 /* Ensure that the device is attached before proceeding */ 2821 if (sfp->sm_dev_state < SFP_DEV_DOWN) 2822 break; 2823 2824 /* Report the module insertion to the upstream device */ 2825 err = sfp_module_insert(sfp->sfp_bus, &sfp->id, 2826 sfp->quirk); 2827 if (err < 0) { 2828 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0); 2829 break; 2830 } 2831 2832 /* If this is a power level 1 module, we are done */ 2833 if (sfp->module_power_mW <= 1000) 2834 goto insert; 2835 2836 sfp_sm_mod_next(sfp, SFP_MOD_HPOWER, 0); 2837 fallthrough; 2838 case SFP_MOD_HPOWER: 2839 /* Enable high power mode */ 2840 err = sfp_sm_mod_hpower(sfp, true); 2841 if (err < 0) { 2842 if (err != -EAGAIN) { 2843 sfp_module_remove(sfp->sfp_bus); 2844 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0); 2845 } else { 2846 sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT); 2847 } 2848 break; 2849 } 2850 2851 sfp_sm_mod_next(sfp, SFP_MOD_WAITPWR, T_HPOWER_LEVEL); 2852 break; 2853 2854 case SFP_MOD_WAITPWR: 2855 /* Wait for T_HPOWER_LEVEL to time out */ 2856 if (event != SFP_E_TIMEOUT) 2857 break; 2858 2859 insert: 2860 sfp_sm_mod_next(sfp, SFP_MOD_PRESENT, 0); 2861 break; 2862 2863 case SFP_MOD_PRESENT: 2864 case SFP_MOD_ERROR: 2865 break; 2866 } 2867 } 2868 2869 static void sfp_sm_main(struct sfp *sfp, unsigned int event) 2870 { 2871 unsigned long timeout; 2872 int ret; 2873 2874 /* Some events are global */ 2875 if (sfp->sm_state != SFP_S_DOWN && 2876 (sfp->sm_mod_state != SFP_MOD_PRESENT || 2877 sfp->sm_dev_state != SFP_DEV_UP)) { 2878 if (sfp->sm_state == SFP_S_LINK_UP && 2879 sfp->sm_dev_state == SFP_DEV_UP) 2880 sfp_sm_link_down(sfp); 2881 if (sfp->sm_state > SFP_S_INIT) 2882 sfp_module_stop(sfp->sfp_bus); 2883 if (sfp->mod_phy) 2884 sfp_sm_phy_detach(sfp); 2885 if (sfp->i2c_mii) 2886 sfp_i2c_mdiobus_destroy(sfp); 2887 sfp_module_tx_disable(sfp); 2888 sfp_soft_stop_poll(sfp); 2889 sfp_sm_next(sfp, SFP_S_DOWN, 0); 2890 return; 2891 } 2892 2893 /* The main state machine */ 2894 switch (sfp->sm_state) { 2895 case SFP_S_DOWN: 2896 if (sfp->sm_mod_state != SFP_MOD_PRESENT || 2897 sfp->sm_dev_state != SFP_DEV_UP) 2898 break; 2899 2900 /* Only use the soft state bits if we have access to the A2h 2901 * memory, which implies that we have some level of SFF-8472 2902 * compliance. 2903 */ 2904 if (sfp->have_a2) 2905 sfp_soft_start_poll(sfp); 2906 2907 sfp_module_tx_enable(sfp); 2908 2909 /* Initialise the fault clearance retries */ 2910 sfp->sm_fault_retries = N_FAULT_INIT; 2911 2912 /* We need to check the TX_FAULT state, which is not defined 2913 * while TX_DISABLE is asserted. The earliest we want to do 2914 * anything (such as probe for a PHY) is 50ms (or more on 2915 * specific modules). 2916 */ 2917 sfp_sm_next(sfp, SFP_S_WAIT, sfp->module_t_wait); 2918 break; 2919 2920 case SFP_S_WAIT: 2921 if (event != SFP_E_TIMEOUT) 2922 break; 2923 2924 if (sfp->state & SFP_F_TX_FAULT) { 2925 /* Wait up to t_init (SFF-8472) or t_start_up (SFF-8431) 2926 * from the TX_DISABLE deassertion for the module to 2927 * initialise, which is indicated by TX_FAULT 2928 * deasserting. 2929 */ 2930 timeout = sfp->module_t_start_up; 2931 if (timeout > sfp->module_t_wait) 2932 timeout -= sfp->module_t_wait; 2933 else 2934 timeout = 1; 2935 2936 sfp_sm_next(sfp, SFP_S_INIT, timeout); 2937 } else { 2938 /* TX_FAULT is not asserted, assume the module has 2939 * finished initialising. 2940 */ 2941 goto init_done; 2942 } 2943 break; 2944 2945 case SFP_S_INIT: 2946 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) { 2947 /* TX_FAULT is still asserted after t_init 2948 * or t_start_up, so assume there is a fault. 2949 */ 2950 sfp_sm_fault(sfp, SFP_S_INIT_TX_FAULT, 2951 sfp->sm_fault_retries == N_FAULT_INIT); 2952 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) { 2953 init_done: 2954 /* Create mdiobus and start trying for PHY */ 2955 ret = sfp_sm_add_mdio_bus(sfp); 2956 if (ret < 0) { 2957 sfp_sm_next(sfp, SFP_S_FAIL, 0); 2958 break; 2959 } 2960 sfp->sm_phy_retries = R_PHY_RETRY; 2961 goto phy_probe; 2962 } 2963 break; 2964 2965 case SFP_S_INIT_PHY: 2966 if (event != SFP_E_TIMEOUT) 2967 break; 2968 phy_probe: 2969 /* TX_FAULT deasserted or we timed out with TX_FAULT 2970 * clear. Probe for the PHY and check the LOS state. 2971 */ 2972 ret = sfp_sm_probe_for_phy(sfp); 2973 if (ret == -ENODEV) { 2974 if (--sfp->sm_phy_retries) { 2975 sfp_sm_next(sfp, SFP_S_INIT_PHY, 2976 sfp->phy_t_retry); 2977 dev_dbg(sfp->dev, 2978 "no PHY detected, %u tries left\n", 2979 sfp->sm_phy_retries); 2980 break; 2981 } else { 2982 dev_info(sfp->dev, "no PHY detected\n"); 2983 } 2984 } else if (ret) { 2985 sfp_sm_next(sfp, SFP_S_FAIL, 0); 2986 break; 2987 } 2988 if (sfp_module_start(sfp->sfp_bus)) { 2989 sfp_sm_next(sfp, SFP_S_FAIL, 0); 2990 break; 2991 } 2992 sfp_sm_link_check_los(sfp); 2993 2994 /* Reset the fault retry count */ 2995 sfp->sm_fault_retries = N_FAULT; 2996 break; 2997 2998 case SFP_S_INIT_TX_FAULT: 2999 if (event == SFP_E_TIMEOUT) { 3000 sfp_module_tx_fault_reset(sfp); 3001 sfp_sm_next(sfp, SFP_S_INIT, sfp->module_t_start_up); 3002 } 3003 break; 3004 3005 case SFP_S_WAIT_LOS: 3006 if (event == SFP_E_TX_FAULT) 3007 sfp_sm_fault(sfp, SFP_S_TX_FAULT, true); 3008 else if (sfp_los_event_inactive(sfp, event)) 3009 sfp_sm_link_up(sfp); 3010 break; 3011 3012 case SFP_S_LINK_UP: 3013 if (event == SFP_E_TX_FAULT) { 3014 sfp_sm_link_down(sfp); 3015 sfp_sm_fault(sfp, SFP_S_TX_FAULT, true); 3016 } else if (sfp_los_event_active(sfp, event)) { 3017 sfp_sm_link_down(sfp); 3018 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0); 3019 } 3020 break; 3021 3022 case SFP_S_TX_FAULT: 3023 if (event == SFP_E_TIMEOUT) { 3024 sfp_module_tx_fault_reset(sfp); 3025 sfp_sm_next(sfp, SFP_S_REINIT, sfp->module_t_start_up); 3026 } 3027 break; 3028 3029 case SFP_S_REINIT: 3030 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) { 3031 sfp_sm_fault(sfp, SFP_S_TX_FAULT, false); 3032 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) { 3033 dev_info(sfp->dev, "module transmit fault recovered\n"); 3034 sfp_sm_link_check_los(sfp); 3035 } 3036 break; 3037 3038 case SFP_S_TX_DISABLE: 3039 break; 3040 } 3041 } 3042 3043 static void __sfp_sm_event(struct sfp *sfp, unsigned int event) 3044 { 3045 dev_dbg(sfp->dev, "SM: enter %s:%s:%s event %s\n", 3046 mod_state_to_str(sfp->sm_mod_state), 3047 dev_state_to_str(sfp->sm_dev_state), 3048 sm_state_to_str(sfp->sm_state), 3049 event_to_str(event)); 3050 3051 sfp_sm_device(sfp, event); 3052 sfp_sm_module(sfp, event); 3053 sfp_sm_main(sfp, event); 3054 3055 dev_dbg(sfp->dev, "SM: exit %s:%s:%s\n", 3056 mod_state_to_str(sfp->sm_mod_state), 3057 dev_state_to_str(sfp->sm_dev_state), 3058 sm_state_to_str(sfp->sm_state)); 3059 } 3060 3061 static void sfp_sm_event(struct sfp *sfp, unsigned int event) 3062 { 3063 mutex_lock(&sfp->sm_mutex); 3064 __sfp_sm_event(sfp, event); 3065 mutex_unlock(&sfp->sm_mutex); 3066 } 3067 3068 static void sfp_attach(struct sfp *sfp) 3069 { 3070 sfp_sm_event(sfp, SFP_E_DEV_ATTACH); 3071 } 3072 3073 static void sfp_detach(struct sfp *sfp) 3074 { 3075 sfp_sm_event(sfp, SFP_E_DEV_DETACH); 3076 } 3077 3078 static void sfp_start(struct sfp *sfp) 3079 { 3080 sfp_sm_event(sfp, SFP_E_DEV_UP); 3081 } 3082 3083 static void sfp_stop(struct sfp *sfp) 3084 { 3085 sfp_sm_event(sfp, SFP_E_DEV_DOWN); 3086 } 3087 3088 static void sfp_set_signal_rate(struct sfp *sfp, unsigned int rate_kbd) 3089 { 3090 unsigned int set; 3091 3092 sfp->rate_kbd = rate_kbd; 3093 3094 if (rate_kbd > sfp->rs_threshold_kbd) 3095 set = sfp->rs_state_mask; 3096 else 3097 set = 0; 3098 3099 sfp_mod_state(sfp, SFP_F_RS0 | SFP_F_RS1, set); 3100 } 3101 3102 static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo) 3103 { 3104 /* locking... and check module is present */ 3105 3106 if (sfp->id.ext.sff8472_compliance && 3107 !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) { 3108 modinfo->type = ETH_MODULE_SFF_8472; 3109 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN; 3110 } else { 3111 modinfo->type = ETH_MODULE_SFF_8079; 3112 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN; 3113 } 3114 return 0; 3115 } 3116 3117 static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee, 3118 u8 *data) 3119 { 3120 unsigned int first, last, len; 3121 int ret; 3122 3123 if (!(sfp->state & SFP_F_PRESENT)) 3124 return -ENODEV; 3125 3126 if (ee->len == 0) 3127 return -EINVAL; 3128 3129 first = ee->offset; 3130 last = ee->offset + ee->len; 3131 if (first < ETH_MODULE_SFF_8079_LEN) { 3132 len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN); 3133 len -= first; 3134 3135 ret = sfp_read(sfp, false, first, data, len); 3136 if (ret < 0) 3137 return ret; 3138 3139 first += len; 3140 data += len; 3141 } 3142 if (first < ETH_MODULE_SFF_8472_LEN && last > ETH_MODULE_SFF_8079_LEN) { 3143 len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN); 3144 len -= first; 3145 first -= ETH_MODULE_SFF_8079_LEN; 3146 3147 ret = sfp_read(sfp, true, first, data, len); 3148 if (ret < 0) 3149 return ret; 3150 } 3151 return 0; 3152 } 3153 3154 static int sfp_module_eeprom_by_page(struct sfp *sfp, 3155 const struct ethtool_module_eeprom *page, 3156 struct netlink_ext_ack *extack) 3157 { 3158 if (!(sfp->state & SFP_F_PRESENT)) 3159 return -ENODEV; 3160 3161 if (page->bank) { 3162 NL_SET_ERR_MSG(extack, "Banks not supported"); 3163 return -EOPNOTSUPP; 3164 } 3165 3166 if (page->page) { 3167 NL_SET_ERR_MSG(extack, "Only page 0 supported"); 3168 return -EOPNOTSUPP; 3169 } 3170 3171 if (page->i2c_address != 0x50 && 3172 page->i2c_address != 0x51) { 3173 NL_SET_ERR_MSG(extack, "Only address 0x50 and 0x51 supported"); 3174 return -EOPNOTSUPP; 3175 } 3176 3177 return sfp_read(sfp, page->i2c_address == 0x51, page->offset, 3178 page->data, page->length); 3179 }; 3180 3181 static const struct sfp_socket_ops sfp_module_ops = { 3182 .attach = sfp_attach, 3183 .detach = sfp_detach, 3184 .start = sfp_start, 3185 .stop = sfp_stop, 3186 .set_signal_rate = sfp_set_signal_rate, 3187 .module_info = sfp_module_info, 3188 .module_eeprom = sfp_module_eeprom, 3189 .module_eeprom_by_page = sfp_module_eeprom_by_page, 3190 }; 3191 3192 static void sfp_timeout(struct work_struct *work) 3193 { 3194 struct sfp *sfp = container_of(work, struct sfp, timeout.work); 3195 3196 rtnl_lock(); 3197 sfp_sm_event(sfp, SFP_E_TIMEOUT); 3198 rtnl_unlock(); 3199 } 3200 3201 static void sfp_check_state(struct sfp *sfp) 3202 { 3203 unsigned int state, i, changed; 3204 3205 rtnl_lock(); 3206 mutex_lock(&sfp->st_mutex); 3207 state = sfp_get_state(sfp); 3208 changed = state ^ sfp->state; 3209 changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT; 3210 3211 for (i = 0; i < GPIO_MAX; i++) 3212 if (changed & BIT(i)) 3213 dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_names[i], 3214 !!(sfp->state & BIT(i)), !!(state & BIT(i))); 3215 3216 state |= sfp->state & SFP_F_OUTPUTS; 3217 sfp->state = state; 3218 mutex_unlock(&sfp->st_mutex); 3219 3220 mutex_lock(&sfp->sm_mutex); 3221 if (changed & SFP_F_PRESENT) 3222 __sfp_sm_event(sfp, state & SFP_F_PRESENT ? 3223 SFP_E_INSERT : SFP_E_REMOVE); 3224 3225 if (changed & SFP_F_TX_FAULT) 3226 __sfp_sm_event(sfp, state & SFP_F_TX_FAULT ? 3227 SFP_E_TX_FAULT : SFP_E_TX_CLEAR); 3228 3229 if (changed & SFP_F_LOS) 3230 __sfp_sm_event(sfp, state & SFP_F_LOS ? 3231 SFP_E_LOS_HIGH : SFP_E_LOS_LOW); 3232 mutex_unlock(&sfp->sm_mutex); 3233 rtnl_unlock(); 3234 } 3235 3236 static irqreturn_t sfp_irq(int irq, void *data) 3237 { 3238 struct sfp *sfp = data; 3239 3240 sfp_check_state(sfp); 3241 3242 return IRQ_HANDLED; 3243 } 3244 3245 static void sfp_poll(struct work_struct *work) 3246 { 3247 struct sfp *sfp = container_of(work, struct sfp, poll.work); 3248 3249 sfp_check_state(sfp); 3250 3251 // st_mutex doesn't need to be held here for state_soft_mask, 3252 // it's unimportant if we race while reading this. 3253 if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) || 3254 sfp->need_poll) 3255 sfp_schedule_poll(sfp); 3256 } 3257 3258 static struct sfp *sfp_alloc(struct device *dev) 3259 { 3260 struct sfp *sfp; 3261 3262 sfp = kzalloc_obj(*sfp); 3263 if (!sfp) 3264 return ERR_PTR(-ENOMEM); 3265 3266 sfp->dev = dev; 3267 3268 mutex_init(&sfp->sm_mutex); 3269 mutex_init(&sfp->st_mutex); 3270 INIT_DELAYED_WORK(&sfp->poll, sfp_poll); 3271 INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout); 3272 3273 sfp_hwmon_init(sfp); 3274 3275 return sfp; 3276 } 3277 3278 static void sfp_cleanup(void *data) 3279 { 3280 struct sfp *sfp = data; 3281 3282 sfp_hwmon_exit(sfp); 3283 3284 cancel_delayed_work_sync(&sfp->poll); 3285 cancel_delayed_work_sync(&sfp->timeout); 3286 if (sfp->i2c_mii) { 3287 mdiobus_unregister(sfp->i2c_mii); 3288 mdiobus_free(sfp->i2c_mii); 3289 } 3290 if (sfp->i2c) 3291 i2c_put_adapter(sfp->i2c); 3292 kfree(sfp); 3293 } 3294 3295 static int sfp_i2c_get(struct sfp *sfp) 3296 { 3297 struct fwnode_handle *h; 3298 struct i2c_adapter *i2c; 3299 int err; 3300 3301 h = fwnode_find_reference(dev_fwnode(sfp->dev), "i2c-bus", 0); 3302 if (IS_ERR(h)) { 3303 dev_err(sfp->dev, "missing 'i2c-bus' property\n"); 3304 return -ENODEV; 3305 } 3306 3307 i2c = i2c_get_adapter_by_fwnode(h); 3308 if (!i2c) { 3309 err = -EPROBE_DEFER; 3310 goto put; 3311 } 3312 3313 err = sfp_i2c_configure(sfp, i2c); 3314 if (err) 3315 i2c_put_adapter(i2c); 3316 put: 3317 fwnode_handle_put(h); 3318 return err; 3319 } 3320 3321 static int sfp_probe(struct platform_device *pdev) 3322 { 3323 const struct sff_data *sff; 3324 char *sfp_irq_name; 3325 struct sfp *sfp; 3326 int err, i; 3327 3328 sfp = sfp_alloc(&pdev->dev); 3329 if (IS_ERR(sfp)) 3330 return PTR_ERR(sfp); 3331 3332 platform_set_drvdata(pdev, sfp); 3333 3334 err = devm_add_action_or_reset(sfp->dev, sfp_cleanup, sfp); 3335 if (err < 0) 3336 return err; 3337 3338 sff = device_get_match_data(sfp->dev); 3339 if (!sff) 3340 sff = &sfp_data; 3341 3342 sfp->type = sff; 3343 3344 err = sfp_i2c_get(sfp); 3345 if (err) 3346 return err; 3347 3348 for (i = 0; i < GPIO_MAX; i++) 3349 if (sff->gpios & BIT(i)) { 3350 sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev, 3351 gpio_names[i], gpio_flags[i]); 3352 if (IS_ERR(sfp->gpio[i])) 3353 return PTR_ERR(sfp->gpio[i]); 3354 } 3355 3356 sfp->state_hw_mask = SFP_F_PRESENT; 3357 sfp->state_hw_drive = SFP_F_TX_DISABLE; 3358 3359 sfp->get_state = sfp_gpio_get_state; 3360 sfp->set_state = sfp_gpio_set_state; 3361 3362 /* An SFP cage with no MOD_DEF0 GPIO has no hardware presence signal. 3363 * Assuming the module is always present traps an empty cage in 3364 * MOD_ERROR and never detects hot-insertion, so derive presence from a 3365 * throttled I2C probe and poll for changes instead. sfp_i2c_configure() 3366 * has already set i2c_max_block_size; seed i2c_block_size so the 3367 * presence read does not issue a zero-length transfer before the first 3368 * EEPROM read. Seed i2c_present_next to jiffies so the first probe 3369 * happens immediately (a zero value would be in the past relative to 3370 * the negative INITIAL_JIFFIES at boot and delay detection). 3371 * 3372 * A soldered-down module (sff,sff) has no presence signal and is 3373 * genuinely always present, so it keeps the always-present behaviour; 3374 * the I2C probe is gated on the cage type advertising SFP_F_PRESENT. 3375 */ 3376 if (!sfp->gpio[GPIO_MODDEF0]) { 3377 if (sff->gpios & SFP_F_PRESENT) { 3378 sfp->get_state = sfp_i2c_get_state; 3379 sfp->i2c_block_size = sfp->i2c_max_block_size; 3380 sfp->i2c_present_next = jiffies; 3381 sfp->need_poll = true; 3382 } else { 3383 sfp->get_state = sff_gpio_get_state; 3384 } 3385 } 3386 3387 device_property_read_u32(&pdev->dev, "maximum-power-milliwatt", 3388 &sfp->max_power_mW); 3389 if (sfp->max_power_mW < 1000) { 3390 if (sfp->max_power_mW) 3391 dev_warn(sfp->dev, 3392 "Firmware bug: host maximum power should be at least 1W\n"); 3393 sfp->max_power_mW = 1000; 3394 } 3395 3396 dev_info(sfp->dev, "Host maximum power %u.%uW\n", 3397 sfp->max_power_mW / 1000, (sfp->max_power_mW / 100) % 10); 3398 3399 /* Get the initial state, and always signal TX disable, 3400 * since the network interface will not be up. 3401 */ 3402 sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE; 3403 3404 if (sfp->gpio[GPIO_RS0] && 3405 gpiod_get_value_cansleep(sfp->gpio[GPIO_RS0])) 3406 sfp->state |= SFP_F_RS0; 3407 sfp_set_state(sfp, sfp->state); 3408 sfp_module_tx_disable(sfp); 3409 if (sfp->state & SFP_F_PRESENT) { 3410 rtnl_lock(); 3411 sfp_sm_event(sfp, SFP_E_INSERT); 3412 rtnl_unlock(); 3413 } 3414 3415 for (i = 0; i < GPIO_MAX; i++) { 3416 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i]) 3417 continue; 3418 3419 sfp->gpio_irq[i] = gpiod_to_irq(sfp->gpio[i]); 3420 if (sfp->gpio_irq[i] < 0) { 3421 sfp->gpio_irq[i] = 0; 3422 sfp->need_poll = true; 3423 continue; 3424 } 3425 3426 sfp_irq_name = devm_kasprintf(sfp->dev, GFP_KERNEL, 3427 "%s-%s", dev_name(sfp->dev), 3428 gpio_names[i]); 3429 3430 if (!sfp_irq_name) 3431 return -ENOMEM; 3432 3433 err = devm_request_threaded_irq(sfp->dev, sfp->gpio_irq[i], 3434 NULL, sfp_irq, 3435 IRQF_ONESHOT | 3436 IRQF_TRIGGER_RISING | 3437 IRQF_TRIGGER_FALLING, 3438 sfp_irq_name, sfp); 3439 if (err) { 3440 sfp->gpio_irq[i] = 0; 3441 sfp->need_poll = true; 3442 } 3443 } 3444 3445 if (sfp->need_poll) 3446 sfp_schedule_poll(sfp); 3447 3448 /* We could have an issue in cases no Tx disable pin is available or 3449 * wired as modules using a laser as their light source will continue to 3450 * be active when the fiber is removed. This could be a safety issue and 3451 * we should at least warn the user about that. 3452 */ 3453 if (!sfp->gpio[GPIO_TX_DISABLE]) 3454 dev_warn(sfp->dev, 3455 "No tx_disable pin: SFP modules will always be emitting.\n"); 3456 3457 sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops); 3458 if (!sfp->sfp_bus) 3459 return -ENOMEM; 3460 3461 if (sfp->i2c_max_block_size < 2) 3462 dev_warn(sfp->dev, 3463 "Please note:\n" 3464 "This SFP cage is accessed via an SMBus only capable of single byte\n" 3465 "transactions. Some features are disabled, other may be unreliable or\n" 3466 "sporadically fail. Use with caution. There is nothing that the kernel\n" 3467 "or community can do to fix it, the kernel will try best efforts. Please\n" 3468 "verify any problems on hardware that supports multi-byte I2C transactions.\n"); 3469 3470 sfp_debugfs_init(sfp); 3471 3472 return 0; 3473 } 3474 3475 static void sfp_remove(struct platform_device *pdev) 3476 { 3477 struct sfp *sfp = platform_get_drvdata(pdev); 3478 3479 sfp_debugfs_exit(sfp); 3480 sfp_unregister_socket(sfp->sfp_bus); 3481 3482 rtnl_lock(); 3483 sfp_sm_event(sfp, SFP_E_REMOVE); 3484 rtnl_unlock(); 3485 } 3486 3487 static void sfp_shutdown(struct platform_device *pdev) 3488 { 3489 struct sfp *sfp = platform_get_drvdata(pdev); 3490 int i; 3491 3492 for (i = 0; i < GPIO_MAX; i++) { 3493 if (!sfp->gpio_irq[i]) 3494 continue; 3495 3496 devm_free_irq(sfp->dev, sfp->gpio_irq[i], sfp); 3497 } 3498 3499 cancel_delayed_work_sync(&sfp->poll); 3500 cancel_delayed_work_sync(&sfp->timeout); 3501 } 3502 3503 static struct platform_driver sfp_driver = { 3504 .probe = sfp_probe, 3505 .remove = sfp_remove, 3506 .shutdown = sfp_shutdown, 3507 .driver = { 3508 .name = "sfp", 3509 .of_match_table = sfp_of_match, 3510 }, 3511 }; 3512 3513 module_platform_driver(sfp_driver); 3514 3515 MODULE_ALIAS("platform:sfp"); 3516 MODULE_AUTHOR("Russell King"); 3517 MODULE_LICENSE("GPL v2"); 3518 MODULE_DESCRIPTION("SFP cage support"); 3519