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