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