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