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