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