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