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