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