1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/acpi.h> 3 #include <linux/ctype.h> 4 #include <linux/delay.h> 5 #include <linux/gpio/consumer.h> 6 #include <linux/hwmon.h> 7 #include <linux/i2c.h> 8 #include <linux/interrupt.h> 9 #include <linux/jiffies.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 "mdio-i2c.h" 20 #include "sfp.h" 21 #include "swphy.h" 22 23 enum { 24 GPIO_MODDEF0, 25 GPIO_LOS, 26 GPIO_TX_FAULT, 27 GPIO_TX_DISABLE, 28 GPIO_RATE_SELECT, 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_RATE_SELECT = BIT(GPIO_RATE_SELECT), 36 37 SFP_E_INSERT = 0, 38 SFP_E_REMOVE, 39 SFP_E_DEV_ATTACH, 40 SFP_E_DEV_DETACH, 41 SFP_E_DEV_DOWN, 42 SFP_E_DEV_UP, 43 SFP_E_TX_FAULT, 44 SFP_E_TX_CLEAR, 45 SFP_E_LOS_HIGH, 46 SFP_E_LOS_LOW, 47 SFP_E_TIMEOUT, 48 49 SFP_MOD_EMPTY = 0, 50 SFP_MOD_ERROR, 51 SFP_MOD_PROBE, 52 SFP_MOD_WAITDEV, 53 SFP_MOD_HPOWER, 54 SFP_MOD_WAITPWR, 55 SFP_MOD_PRESENT, 56 57 SFP_DEV_DETACHED = 0, 58 SFP_DEV_DOWN, 59 SFP_DEV_UP, 60 61 SFP_S_DOWN = 0, 62 SFP_S_FAIL, 63 SFP_S_WAIT, 64 SFP_S_INIT, 65 SFP_S_INIT_PHY, 66 SFP_S_INIT_TX_FAULT, 67 SFP_S_WAIT_LOS, 68 SFP_S_LINK_UP, 69 SFP_S_TX_FAULT, 70 SFP_S_REINIT, 71 SFP_S_TX_DISABLE, 72 }; 73 74 static const char * const mod_state_strings[] = { 75 [SFP_MOD_EMPTY] = "empty", 76 [SFP_MOD_ERROR] = "error", 77 [SFP_MOD_PROBE] = "probe", 78 [SFP_MOD_WAITDEV] = "waitdev", 79 [SFP_MOD_HPOWER] = "hpower", 80 [SFP_MOD_WAITPWR] = "waitpwr", 81 [SFP_MOD_PRESENT] = "present", 82 }; 83 84 static const char *mod_state_to_str(unsigned short mod_state) 85 { 86 if (mod_state >= ARRAY_SIZE(mod_state_strings)) 87 return "Unknown module state"; 88 return mod_state_strings[mod_state]; 89 } 90 91 static const char * const dev_state_strings[] = { 92 [SFP_DEV_DETACHED] = "detached", 93 [SFP_DEV_DOWN] = "down", 94 [SFP_DEV_UP] = "up", 95 }; 96 97 static const char *dev_state_to_str(unsigned short dev_state) 98 { 99 if (dev_state >= ARRAY_SIZE(dev_state_strings)) 100 return "Unknown device state"; 101 return dev_state_strings[dev_state]; 102 } 103 104 static const char * const event_strings[] = { 105 [SFP_E_INSERT] = "insert", 106 [SFP_E_REMOVE] = "remove", 107 [SFP_E_DEV_ATTACH] = "dev_attach", 108 [SFP_E_DEV_DETACH] = "dev_detach", 109 [SFP_E_DEV_DOWN] = "dev_down", 110 [SFP_E_DEV_UP] = "dev_up", 111 [SFP_E_TX_FAULT] = "tx_fault", 112 [SFP_E_TX_CLEAR] = "tx_clear", 113 [SFP_E_LOS_HIGH] = "los_high", 114 [SFP_E_LOS_LOW] = "los_low", 115 [SFP_E_TIMEOUT] = "timeout", 116 }; 117 118 static const char *event_to_str(unsigned short event) 119 { 120 if (event >= ARRAY_SIZE(event_strings)) 121 return "Unknown event"; 122 return event_strings[event]; 123 } 124 125 static const char * const sm_state_strings[] = { 126 [SFP_S_DOWN] = "down", 127 [SFP_S_FAIL] = "fail", 128 [SFP_S_WAIT] = "wait", 129 [SFP_S_INIT] = "init", 130 [SFP_S_INIT_PHY] = "init_phy", 131 [SFP_S_INIT_TX_FAULT] = "init_tx_fault", 132 [SFP_S_WAIT_LOS] = "wait_los", 133 [SFP_S_LINK_UP] = "link_up", 134 [SFP_S_TX_FAULT] = "tx_fault", 135 [SFP_S_REINIT] = "reinit", 136 [SFP_S_TX_DISABLE] = "rx_disable", 137 }; 138 139 static const char *sm_state_to_str(unsigned short sm_state) 140 { 141 if (sm_state >= ARRAY_SIZE(sm_state_strings)) 142 return "Unknown state"; 143 return sm_state_strings[sm_state]; 144 } 145 146 static const char *gpio_of_names[] = { 147 "mod-def0", 148 "los", 149 "tx-fault", 150 "tx-disable", 151 "rate-select0", 152 }; 153 154 static const enum gpiod_flags gpio_flags[] = { 155 GPIOD_IN, 156 GPIOD_IN, 157 GPIOD_IN, 158 GPIOD_ASIS, 159 GPIOD_ASIS, 160 }; 161 162 /* t_start_up (SFF-8431) or t_init (SFF-8472) is the time required for a 163 * non-cooled module to initialise its laser safety circuitry. We wait 164 * an initial T_WAIT period before we check the tx fault to give any PHY 165 * on board (for a copper SFP) time to initialise. 166 */ 167 #define T_WAIT msecs_to_jiffies(50) 168 #define T_START_UP msecs_to_jiffies(300) 169 #define T_START_UP_BAD_GPON msecs_to_jiffies(60000) 170 171 /* t_reset is the time required to assert the TX_DISABLE signal to reset 172 * an indicated TX_FAULT. 173 */ 174 #define T_RESET_US 10 175 #define T_FAULT_RECOVER msecs_to_jiffies(1000) 176 177 /* N_FAULT_INIT is the number of recovery attempts at module initialisation 178 * time. If the TX_FAULT signal is not deasserted after this number of 179 * attempts at clearing it, we decide that the module is faulty. 180 * N_FAULT is the same but after the module has initialised. 181 */ 182 #define N_FAULT_INIT 5 183 #define N_FAULT 5 184 185 /* T_PHY_RETRY is the time interval between attempts to probe the PHY. 186 * R_PHY_RETRY is the number of attempts. 187 */ 188 #define T_PHY_RETRY msecs_to_jiffies(50) 189 #define R_PHY_RETRY 12 190 191 /* SFP module presence detection is poor: the three MOD DEF signals are 192 * the same length on the PCB, which means it's possible for MOD DEF 0 to 193 * connect before the I2C bus on MOD DEF 1/2. 194 * 195 * The SFF-8472 specifies t_serial ("Time from power on until module is 196 * ready for data transmission over the two wire serial bus.") as 300ms. 197 */ 198 #define T_SERIAL msecs_to_jiffies(300) 199 #define T_HPOWER_LEVEL msecs_to_jiffies(300) 200 #define T_PROBE_RETRY_INIT msecs_to_jiffies(100) 201 #define R_PROBE_RETRY_INIT 10 202 #define T_PROBE_RETRY_SLOW msecs_to_jiffies(5000) 203 #define R_PROBE_RETRY_SLOW 12 204 205 /* SFP modules appear to always have their PHY configured for bus address 206 * 0x56 (which with mdio-i2c, translates to a PHY address of 22). 207 */ 208 #define SFP_PHY_ADDR 22 209 210 struct sff_data { 211 unsigned int gpios; 212 bool (*module_supported)(const struct sfp_eeprom_id *id); 213 }; 214 215 struct sfp { 216 struct device *dev; 217 struct i2c_adapter *i2c; 218 struct mii_bus *i2c_mii; 219 struct sfp_bus *sfp_bus; 220 struct phy_device *mod_phy; 221 const struct sff_data *type; 222 u32 max_power_mW; 223 224 unsigned int (*get_state)(struct sfp *); 225 void (*set_state)(struct sfp *, unsigned int); 226 int (*read)(struct sfp *, bool, u8, void *, size_t); 227 int (*write)(struct sfp *, bool, u8, void *, size_t); 228 229 struct gpio_desc *gpio[GPIO_MAX]; 230 int gpio_irq[GPIO_MAX]; 231 232 bool need_poll; 233 234 struct mutex st_mutex; /* Protects state */ 235 unsigned int state_soft_mask; 236 unsigned int state; 237 struct delayed_work poll; 238 struct delayed_work timeout; 239 struct mutex sm_mutex; /* Protects state machine */ 240 unsigned char sm_mod_state; 241 unsigned char sm_mod_tries_init; 242 unsigned char sm_mod_tries; 243 unsigned char sm_dev_state; 244 unsigned short sm_state; 245 unsigned char sm_fault_retries; 246 unsigned char sm_phy_retries; 247 248 struct sfp_eeprom_id id; 249 unsigned int module_power_mW; 250 unsigned int module_t_start_up; 251 252 #if IS_ENABLED(CONFIG_HWMON) 253 struct sfp_diag diag; 254 struct delayed_work hwmon_probe; 255 unsigned int hwmon_tries; 256 struct device *hwmon_dev; 257 char *hwmon_name; 258 #endif 259 260 }; 261 262 static bool sff_module_supported(const struct sfp_eeprom_id *id) 263 { 264 return id->base.phys_id == SFF8024_ID_SFF_8472 && 265 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP; 266 } 267 268 static const struct sff_data sff_data = { 269 .gpios = SFP_F_LOS | SFP_F_TX_FAULT | SFP_F_TX_DISABLE, 270 .module_supported = sff_module_supported, 271 }; 272 273 static bool sfp_module_supported(const struct sfp_eeprom_id *id) 274 { 275 return id->base.phys_id == SFF8024_ID_SFP && 276 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP; 277 } 278 279 static const struct sff_data sfp_data = { 280 .gpios = SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT | 281 SFP_F_TX_DISABLE | SFP_F_RATE_SELECT, 282 .module_supported = sfp_module_supported, 283 }; 284 285 static const struct of_device_id sfp_of_match[] = { 286 { .compatible = "sff,sff", .data = &sff_data, }, 287 { .compatible = "sff,sfp", .data = &sfp_data, }, 288 { }, 289 }; 290 MODULE_DEVICE_TABLE(of, sfp_of_match); 291 292 static unsigned long poll_jiffies; 293 294 static unsigned int sfp_gpio_get_state(struct sfp *sfp) 295 { 296 unsigned int i, state, v; 297 298 for (i = state = 0; i < GPIO_MAX; i++) { 299 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i]) 300 continue; 301 302 v = gpiod_get_value_cansleep(sfp->gpio[i]); 303 if (v) 304 state |= BIT(i); 305 } 306 307 return state; 308 } 309 310 static unsigned int sff_gpio_get_state(struct sfp *sfp) 311 { 312 return sfp_gpio_get_state(sfp) | SFP_F_PRESENT; 313 } 314 315 static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state) 316 { 317 if (state & SFP_F_PRESENT) { 318 /* If the module is present, drive the signals */ 319 if (sfp->gpio[GPIO_TX_DISABLE]) 320 gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE], 321 state & SFP_F_TX_DISABLE); 322 if (state & SFP_F_RATE_SELECT) 323 gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT], 324 state & SFP_F_RATE_SELECT); 325 } else { 326 /* Otherwise, let them float to the pull-ups */ 327 if (sfp->gpio[GPIO_TX_DISABLE]) 328 gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]); 329 if (state & SFP_F_RATE_SELECT) 330 gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]); 331 } 332 } 333 334 static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf, 335 size_t len) 336 { 337 struct i2c_msg msgs[2]; 338 u8 bus_addr = a2 ? 0x51 : 0x50; 339 size_t this_len; 340 int ret; 341 342 msgs[0].addr = bus_addr; 343 msgs[0].flags = 0; 344 msgs[0].len = 1; 345 msgs[0].buf = &dev_addr; 346 msgs[1].addr = bus_addr; 347 msgs[1].flags = I2C_M_RD; 348 msgs[1].len = len; 349 msgs[1].buf = buf; 350 351 while (len) { 352 this_len = len; 353 if (this_len > 16) 354 this_len = 16; 355 356 msgs[1].len = this_len; 357 358 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs)); 359 if (ret < 0) 360 return ret; 361 362 if (ret != ARRAY_SIZE(msgs)) 363 break; 364 365 msgs[1].buf += this_len; 366 dev_addr += this_len; 367 len -= this_len; 368 } 369 370 return msgs[1].buf - (u8 *)buf; 371 } 372 373 static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf, 374 size_t len) 375 { 376 struct i2c_msg msgs[1]; 377 u8 bus_addr = a2 ? 0x51 : 0x50; 378 int ret; 379 380 msgs[0].addr = bus_addr; 381 msgs[0].flags = 0; 382 msgs[0].len = 1 + len; 383 msgs[0].buf = kmalloc(1 + len, GFP_KERNEL); 384 if (!msgs[0].buf) 385 return -ENOMEM; 386 387 msgs[0].buf[0] = dev_addr; 388 memcpy(&msgs[0].buf[1], buf, len); 389 390 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs)); 391 392 kfree(msgs[0].buf); 393 394 if (ret < 0) 395 return ret; 396 397 return ret == ARRAY_SIZE(msgs) ? len : 0; 398 } 399 400 static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c) 401 { 402 struct mii_bus *i2c_mii; 403 int ret; 404 405 if (!i2c_check_functionality(i2c, I2C_FUNC_I2C)) 406 return -EINVAL; 407 408 sfp->i2c = i2c; 409 sfp->read = sfp_i2c_read; 410 sfp->write = sfp_i2c_write; 411 412 i2c_mii = mdio_i2c_alloc(sfp->dev, i2c); 413 if (IS_ERR(i2c_mii)) 414 return PTR_ERR(i2c_mii); 415 416 i2c_mii->name = "SFP I2C Bus"; 417 i2c_mii->phy_mask = ~0; 418 419 ret = mdiobus_register(i2c_mii); 420 if (ret < 0) { 421 mdiobus_free(i2c_mii); 422 return ret; 423 } 424 425 sfp->i2c_mii = i2c_mii; 426 427 return 0; 428 } 429 430 /* Interface */ 431 static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len) 432 { 433 return sfp->read(sfp, a2, addr, buf, len); 434 } 435 436 static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len) 437 { 438 return sfp->write(sfp, a2, addr, buf, len); 439 } 440 441 static unsigned int sfp_soft_get_state(struct sfp *sfp) 442 { 443 unsigned int state = 0; 444 u8 status; 445 446 if (sfp_read(sfp, true, SFP_STATUS, &status, sizeof(status)) == 447 sizeof(status)) { 448 if (status & SFP_STATUS_RX_LOS) 449 state |= SFP_F_LOS; 450 if (status & SFP_STATUS_TX_FAULT) 451 state |= SFP_F_TX_FAULT; 452 } 453 454 return state & sfp->state_soft_mask; 455 } 456 457 static void sfp_soft_set_state(struct sfp *sfp, unsigned int state) 458 { 459 u8 status; 460 461 if (sfp_read(sfp, true, SFP_STATUS, &status, sizeof(status)) == 462 sizeof(status)) { 463 if (state & SFP_F_TX_DISABLE) 464 status |= SFP_STATUS_TX_DISABLE_FORCE; 465 else 466 status &= ~SFP_STATUS_TX_DISABLE_FORCE; 467 468 sfp_write(sfp, true, SFP_STATUS, &status, sizeof(status)); 469 } 470 } 471 472 static void sfp_soft_start_poll(struct sfp *sfp) 473 { 474 const struct sfp_eeprom_id *id = &sfp->id; 475 476 sfp->state_soft_mask = 0; 477 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_DISABLE && 478 !sfp->gpio[GPIO_TX_DISABLE]) 479 sfp->state_soft_mask |= SFP_F_TX_DISABLE; 480 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_FAULT && 481 !sfp->gpio[GPIO_TX_FAULT]) 482 sfp->state_soft_mask |= SFP_F_TX_FAULT; 483 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_RX_LOS && 484 !sfp->gpio[GPIO_LOS]) 485 sfp->state_soft_mask |= SFP_F_LOS; 486 487 if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) && 488 !sfp->need_poll) 489 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies); 490 } 491 492 static void sfp_soft_stop_poll(struct sfp *sfp) 493 { 494 sfp->state_soft_mask = 0; 495 } 496 497 static unsigned int sfp_get_state(struct sfp *sfp) 498 { 499 unsigned int state = sfp->get_state(sfp); 500 501 if (state & SFP_F_PRESENT && 502 sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT)) 503 state |= sfp_soft_get_state(sfp); 504 505 return state; 506 } 507 508 static void sfp_set_state(struct sfp *sfp, unsigned int state) 509 { 510 sfp->set_state(sfp, state); 511 512 if (state & SFP_F_PRESENT && 513 sfp->state_soft_mask & SFP_F_TX_DISABLE) 514 sfp_soft_set_state(sfp, state); 515 } 516 517 static unsigned int sfp_check(void *buf, size_t len) 518 { 519 u8 *p, check; 520 521 for (p = buf, check = 0; len; p++, len--) 522 check += *p; 523 524 return check; 525 } 526 527 /* hwmon */ 528 #if IS_ENABLED(CONFIG_HWMON) 529 static umode_t sfp_hwmon_is_visible(const void *data, 530 enum hwmon_sensor_types type, 531 u32 attr, int channel) 532 { 533 const struct sfp *sfp = data; 534 535 switch (type) { 536 case hwmon_temp: 537 switch (attr) { 538 case hwmon_temp_min_alarm: 539 case hwmon_temp_max_alarm: 540 case hwmon_temp_lcrit_alarm: 541 case hwmon_temp_crit_alarm: 542 case hwmon_temp_min: 543 case hwmon_temp_max: 544 case hwmon_temp_lcrit: 545 case hwmon_temp_crit: 546 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN)) 547 return 0; 548 /* fall through */ 549 case hwmon_temp_input: 550 case hwmon_temp_label: 551 return 0444; 552 default: 553 return 0; 554 } 555 case hwmon_in: 556 switch (attr) { 557 case hwmon_in_min_alarm: 558 case hwmon_in_max_alarm: 559 case hwmon_in_lcrit_alarm: 560 case hwmon_in_crit_alarm: 561 case hwmon_in_min: 562 case hwmon_in_max: 563 case hwmon_in_lcrit: 564 case hwmon_in_crit: 565 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN)) 566 return 0; 567 /* fall through */ 568 case hwmon_in_input: 569 case hwmon_in_label: 570 return 0444; 571 default: 572 return 0; 573 } 574 case hwmon_curr: 575 switch (attr) { 576 case hwmon_curr_min_alarm: 577 case hwmon_curr_max_alarm: 578 case hwmon_curr_lcrit_alarm: 579 case hwmon_curr_crit_alarm: 580 case hwmon_curr_min: 581 case hwmon_curr_max: 582 case hwmon_curr_lcrit: 583 case hwmon_curr_crit: 584 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN)) 585 return 0; 586 /* fall through */ 587 case hwmon_curr_input: 588 case hwmon_curr_label: 589 return 0444; 590 default: 591 return 0; 592 } 593 case hwmon_power: 594 /* External calibration of receive power requires 595 * floating point arithmetic. Doing that in the kernel 596 * is not easy, so just skip it. If the module does 597 * not require external calibration, we can however 598 * show receiver power, since FP is then not needed. 599 */ 600 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL && 601 channel == 1) 602 return 0; 603 switch (attr) { 604 case hwmon_power_min_alarm: 605 case hwmon_power_max_alarm: 606 case hwmon_power_lcrit_alarm: 607 case hwmon_power_crit_alarm: 608 case hwmon_power_min: 609 case hwmon_power_max: 610 case hwmon_power_lcrit: 611 case hwmon_power_crit: 612 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN)) 613 return 0; 614 /* fall through */ 615 case hwmon_power_input: 616 case hwmon_power_label: 617 return 0444; 618 default: 619 return 0; 620 } 621 default: 622 return 0; 623 } 624 } 625 626 static int sfp_hwmon_read_sensor(struct sfp *sfp, int reg, long *value) 627 { 628 __be16 val; 629 int err; 630 631 err = sfp_read(sfp, true, reg, &val, sizeof(val)); 632 if (err < 0) 633 return err; 634 635 *value = be16_to_cpu(val); 636 637 return 0; 638 } 639 640 static void sfp_hwmon_to_rx_power(long *value) 641 { 642 *value = DIV_ROUND_CLOSEST(*value, 10); 643 } 644 645 static void sfp_hwmon_calibrate(struct sfp *sfp, unsigned int slope, int offset, 646 long *value) 647 { 648 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL) 649 *value = DIV_ROUND_CLOSEST(*value * slope, 256) + offset; 650 } 651 652 static void sfp_hwmon_calibrate_temp(struct sfp *sfp, long *value) 653 { 654 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_t_slope), 655 be16_to_cpu(sfp->diag.cal_t_offset), value); 656 657 if (*value >= 0x8000) 658 *value -= 0x10000; 659 660 *value = DIV_ROUND_CLOSEST(*value * 1000, 256); 661 } 662 663 static void sfp_hwmon_calibrate_vcc(struct sfp *sfp, long *value) 664 { 665 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_v_slope), 666 be16_to_cpu(sfp->diag.cal_v_offset), value); 667 668 *value = DIV_ROUND_CLOSEST(*value, 10); 669 } 670 671 static void sfp_hwmon_calibrate_bias(struct sfp *sfp, long *value) 672 { 673 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txi_slope), 674 be16_to_cpu(sfp->diag.cal_txi_offset), value); 675 676 *value = DIV_ROUND_CLOSEST(*value, 500); 677 } 678 679 static void sfp_hwmon_calibrate_tx_power(struct sfp *sfp, long *value) 680 { 681 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txpwr_slope), 682 be16_to_cpu(sfp->diag.cal_txpwr_offset), value); 683 684 *value = DIV_ROUND_CLOSEST(*value, 10); 685 } 686 687 static int sfp_hwmon_read_temp(struct sfp *sfp, int reg, long *value) 688 { 689 int err; 690 691 err = sfp_hwmon_read_sensor(sfp, reg, value); 692 if (err < 0) 693 return err; 694 695 sfp_hwmon_calibrate_temp(sfp, value); 696 697 return 0; 698 } 699 700 static int sfp_hwmon_read_vcc(struct sfp *sfp, int reg, long *value) 701 { 702 int err; 703 704 err = sfp_hwmon_read_sensor(sfp, reg, value); 705 if (err < 0) 706 return err; 707 708 sfp_hwmon_calibrate_vcc(sfp, value); 709 710 return 0; 711 } 712 713 static int sfp_hwmon_read_bias(struct sfp *sfp, int reg, long *value) 714 { 715 int err; 716 717 err = sfp_hwmon_read_sensor(sfp, reg, value); 718 if (err < 0) 719 return err; 720 721 sfp_hwmon_calibrate_bias(sfp, value); 722 723 return 0; 724 } 725 726 static int sfp_hwmon_read_tx_power(struct sfp *sfp, int reg, long *value) 727 { 728 int err; 729 730 err = sfp_hwmon_read_sensor(sfp, reg, value); 731 if (err < 0) 732 return err; 733 734 sfp_hwmon_calibrate_tx_power(sfp, value); 735 736 return 0; 737 } 738 739 static int sfp_hwmon_read_rx_power(struct sfp *sfp, int reg, long *value) 740 { 741 int err; 742 743 err = sfp_hwmon_read_sensor(sfp, reg, value); 744 if (err < 0) 745 return err; 746 747 sfp_hwmon_to_rx_power(value); 748 749 return 0; 750 } 751 752 static int sfp_hwmon_temp(struct sfp *sfp, u32 attr, long *value) 753 { 754 u8 status; 755 int err; 756 757 switch (attr) { 758 case hwmon_temp_input: 759 return sfp_hwmon_read_temp(sfp, SFP_TEMP, value); 760 761 case hwmon_temp_lcrit: 762 *value = be16_to_cpu(sfp->diag.temp_low_alarm); 763 sfp_hwmon_calibrate_temp(sfp, value); 764 return 0; 765 766 case hwmon_temp_min: 767 *value = be16_to_cpu(sfp->diag.temp_low_warn); 768 sfp_hwmon_calibrate_temp(sfp, value); 769 return 0; 770 case hwmon_temp_max: 771 *value = be16_to_cpu(sfp->diag.temp_high_warn); 772 sfp_hwmon_calibrate_temp(sfp, value); 773 return 0; 774 775 case hwmon_temp_crit: 776 *value = be16_to_cpu(sfp->diag.temp_high_alarm); 777 sfp_hwmon_calibrate_temp(sfp, value); 778 return 0; 779 780 case hwmon_temp_lcrit_alarm: 781 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 782 if (err < 0) 783 return err; 784 785 *value = !!(status & SFP_ALARM0_TEMP_LOW); 786 return 0; 787 788 case hwmon_temp_min_alarm: 789 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 790 if (err < 0) 791 return err; 792 793 *value = !!(status & SFP_WARN0_TEMP_LOW); 794 return 0; 795 796 case hwmon_temp_max_alarm: 797 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 798 if (err < 0) 799 return err; 800 801 *value = !!(status & SFP_WARN0_TEMP_HIGH); 802 return 0; 803 804 case hwmon_temp_crit_alarm: 805 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 806 if (err < 0) 807 return err; 808 809 *value = !!(status & SFP_ALARM0_TEMP_HIGH); 810 return 0; 811 default: 812 return -EOPNOTSUPP; 813 } 814 815 return -EOPNOTSUPP; 816 } 817 818 static int sfp_hwmon_vcc(struct sfp *sfp, u32 attr, long *value) 819 { 820 u8 status; 821 int err; 822 823 switch (attr) { 824 case hwmon_in_input: 825 return sfp_hwmon_read_vcc(sfp, SFP_VCC, value); 826 827 case hwmon_in_lcrit: 828 *value = be16_to_cpu(sfp->diag.volt_low_alarm); 829 sfp_hwmon_calibrate_vcc(sfp, value); 830 return 0; 831 832 case hwmon_in_min: 833 *value = be16_to_cpu(sfp->diag.volt_low_warn); 834 sfp_hwmon_calibrate_vcc(sfp, value); 835 return 0; 836 837 case hwmon_in_max: 838 *value = be16_to_cpu(sfp->diag.volt_high_warn); 839 sfp_hwmon_calibrate_vcc(sfp, value); 840 return 0; 841 842 case hwmon_in_crit: 843 *value = be16_to_cpu(sfp->diag.volt_high_alarm); 844 sfp_hwmon_calibrate_vcc(sfp, value); 845 return 0; 846 847 case hwmon_in_lcrit_alarm: 848 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 849 if (err < 0) 850 return err; 851 852 *value = !!(status & SFP_ALARM0_VCC_LOW); 853 return 0; 854 855 case hwmon_in_min_alarm: 856 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 857 if (err < 0) 858 return err; 859 860 *value = !!(status & SFP_WARN0_VCC_LOW); 861 return 0; 862 863 case hwmon_in_max_alarm: 864 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 865 if (err < 0) 866 return err; 867 868 *value = !!(status & SFP_WARN0_VCC_HIGH); 869 return 0; 870 871 case hwmon_in_crit_alarm: 872 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 873 if (err < 0) 874 return err; 875 876 *value = !!(status & SFP_ALARM0_VCC_HIGH); 877 return 0; 878 default: 879 return -EOPNOTSUPP; 880 } 881 882 return -EOPNOTSUPP; 883 } 884 885 static int sfp_hwmon_bias(struct sfp *sfp, u32 attr, long *value) 886 { 887 u8 status; 888 int err; 889 890 switch (attr) { 891 case hwmon_curr_input: 892 return sfp_hwmon_read_bias(sfp, SFP_TX_BIAS, value); 893 894 case hwmon_curr_lcrit: 895 *value = be16_to_cpu(sfp->diag.bias_low_alarm); 896 sfp_hwmon_calibrate_bias(sfp, value); 897 return 0; 898 899 case hwmon_curr_min: 900 *value = be16_to_cpu(sfp->diag.bias_low_warn); 901 sfp_hwmon_calibrate_bias(sfp, value); 902 return 0; 903 904 case hwmon_curr_max: 905 *value = be16_to_cpu(sfp->diag.bias_high_warn); 906 sfp_hwmon_calibrate_bias(sfp, value); 907 return 0; 908 909 case hwmon_curr_crit: 910 *value = be16_to_cpu(sfp->diag.bias_high_alarm); 911 sfp_hwmon_calibrate_bias(sfp, value); 912 return 0; 913 914 case hwmon_curr_lcrit_alarm: 915 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 916 if (err < 0) 917 return err; 918 919 *value = !!(status & SFP_ALARM0_TX_BIAS_LOW); 920 return 0; 921 922 case hwmon_curr_min_alarm: 923 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 924 if (err < 0) 925 return err; 926 927 *value = !!(status & SFP_WARN0_TX_BIAS_LOW); 928 return 0; 929 930 case hwmon_curr_max_alarm: 931 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 932 if (err < 0) 933 return err; 934 935 *value = !!(status & SFP_WARN0_TX_BIAS_HIGH); 936 return 0; 937 938 case hwmon_curr_crit_alarm: 939 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 940 if (err < 0) 941 return err; 942 943 *value = !!(status & SFP_ALARM0_TX_BIAS_HIGH); 944 return 0; 945 default: 946 return -EOPNOTSUPP; 947 } 948 949 return -EOPNOTSUPP; 950 } 951 952 static int sfp_hwmon_tx_power(struct sfp *sfp, u32 attr, long *value) 953 { 954 u8 status; 955 int err; 956 957 switch (attr) { 958 case hwmon_power_input: 959 return sfp_hwmon_read_tx_power(sfp, SFP_TX_POWER, value); 960 961 case hwmon_power_lcrit: 962 *value = be16_to_cpu(sfp->diag.txpwr_low_alarm); 963 sfp_hwmon_calibrate_tx_power(sfp, value); 964 return 0; 965 966 case hwmon_power_min: 967 *value = be16_to_cpu(sfp->diag.txpwr_low_warn); 968 sfp_hwmon_calibrate_tx_power(sfp, value); 969 return 0; 970 971 case hwmon_power_max: 972 *value = be16_to_cpu(sfp->diag.txpwr_high_warn); 973 sfp_hwmon_calibrate_tx_power(sfp, value); 974 return 0; 975 976 case hwmon_power_crit: 977 *value = be16_to_cpu(sfp->diag.txpwr_high_alarm); 978 sfp_hwmon_calibrate_tx_power(sfp, value); 979 return 0; 980 981 case hwmon_power_lcrit_alarm: 982 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 983 if (err < 0) 984 return err; 985 986 *value = !!(status & SFP_ALARM0_TXPWR_LOW); 987 return 0; 988 989 case hwmon_power_min_alarm: 990 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 991 if (err < 0) 992 return err; 993 994 *value = !!(status & SFP_WARN0_TXPWR_LOW); 995 return 0; 996 997 case hwmon_power_max_alarm: 998 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); 999 if (err < 0) 1000 return err; 1001 1002 *value = !!(status & SFP_WARN0_TXPWR_HIGH); 1003 return 0; 1004 1005 case hwmon_power_crit_alarm: 1006 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); 1007 if (err < 0) 1008 return err; 1009 1010 *value = !!(status & SFP_ALARM0_TXPWR_HIGH); 1011 return 0; 1012 default: 1013 return -EOPNOTSUPP; 1014 } 1015 1016 return -EOPNOTSUPP; 1017 } 1018 1019 static int sfp_hwmon_rx_power(struct sfp *sfp, u32 attr, long *value) 1020 { 1021 u8 status; 1022 int err; 1023 1024 switch (attr) { 1025 case hwmon_power_input: 1026 return sfp_hwmon_read_rx_power(sfp, SFP_RX_POWER, value); 1027 1028 case hwmon_power_lcrit: 1029 *value = be16_to_cpu(sfp->diag.rxpwr_low_alarm); 1030 sfp_hwmon_to_rx_power(value); 1031 return 0; 1032 1033 case hwmon_power_min: 1034 *value = be16_to_cpu(sfp->diag.rxpwr_low_warn); 1035 sfp_hwmon_to_rx_power(value); 1036 return 0; 1037 1038 case hwmon_power_max: 1039 *value = be16_to_cpu(sfp->diag.rxpwr_high_warn); 1040 sfp_hwmon_to_rx_power(value); 1041 return 0; 1042 1043 case hwmon_power_crit: 1044 *value = be16_to_cpu(sfp->diag.rxpwr_high_alarm); 1045 sfp_hwmon_to_rx_power(value); 1046 return 0; 1047 1048 case hwmon_power_lcrit_alarm: 1049 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status)); 1050 if (err < 0) 1051 return err; 1052 1053 *value = !!(status & SFP_ALARM1_RXPWR_LOW); 1054 return 0; 1055 1056 case hwmon_power_min_alarm: 1057 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status)); 1058 if (err < 0) 1059 return err; 1060 1061 *value = !!(status & SFP_WARN1_RXPWR_LOW); 1062 return 0; 1063 1064 case hwmon_power_max_alarm: 1065 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status)); 1066 if (err < 0) 1067 return err; 1068 1069 *value = !!(status & SFP_WARN1_RXPWR_HIGH); 1070 return 0; 1071 1072 case hwmon_power_crit_alarm: 1073 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status)); 1074 if (err < 0) 1075 return err; 1076 1077 *value = !!(status & SFP_ALARM1_RXPWR_HIGH); 1078 return 0; 1079 default: 1080 return -EOPNOTSUPP; 1081 } 1082 1083 return -EOPNOTSUPP; 1084 } 1085 1086 static int sfp_hwmon_read(struct device *dev, enum hwmon_sensor_types type, 1087 u32 attr, int channel, long *value) 1088 { 1089 struct sfp *sfp = dev_get_drvdata(dev); 1090 1091 switch (type) { 1092 case hwmon_temp: 1093 return sfp_hwmon_temp(sfp, attr, value); 1094 case hwmon_in: 1095 return sfp_hwmon_vcc(sfp, attr, value); 1096 case hwmon_curr: 1097 return sfp_hwmon_bias(sfp, attr, value); 1098 case hwmon_power: 1099 switch (channel) { 1100 case 0: 1101 return sfp_hwmon_tx_power(sfp, attr, value); 1102 case 1: 1103 return sfp_hwmon_rx_power(sfp, attr, value); 1104 default: 1105 return -EOPNOTSUPP; 1106 } 1107 default: 1108 return -EOPNOTSUPP; 1109 } 1110 } 1111 1112 static const char *const sfp_hwmon_power_labels[] = { 1113 "TX_power", 1114 "RX_power", 1115 }; 1116 1117 static int sfp_hwmon_read_string(struct device *dev, 1118 enum hwmon_sensor_types type, 1119 u32 attr, int channel, const char **str) 1120 { 1121 switch (type) { 1122 case hwmon_curr: 1123 switch (attr) { 1124 case hwmon_curr_label: 1125 *str = "bias"; 1126 return 0; 1127 default: 1128 return -EOPNOTSUPP; 1129 } 1130 break; 1131 case hwmon_temp: 1132 switch (attr) { 1133 case hwmon_temp_label: 1134 *str = "temperature"; 1135 return 0; 1136 default: 1137 return -EOPNOTSUPP; 1138 } 1139 break; 1140 case hwmon_in: 1141 switch (attr) { 1142 case hwmon_in_label: 1143 *str = "VCC"; 1144 return 0; 1145 default: 1146 return -EOPNOTSUPP; 1147 } 1148 break; 1149 case hwmon_power: 1150 switch (attr) { 1151 case hwmon_power_label: 1152 *str = sfp_hwmon_power_labels[channel]; 1153 return 0; 1154 default: 1155 return -EOPNOTSUPP; 1156 } 1157 break; 1158 default: 1159 return -EOPNOTSUPP; 1160 } 1161 1162 return -EOPNOTSUPP; 1163 } 1164 1165 static const struct hwmon_ops sfp_hwmon_ops = { 1166 .is_visible = sfp_hwmon_is_visible, 1167 .read = sfp_hwmon_read, 1168 .read_string = sfp_hwmon_read_string, 1169 }; 1170 1171 static u32 sfp_hwmon_chip_config[] = { 1172 HWMON_C_REGISTER_TZ, 1173 0, 1174 }; 1175 1176 static const struct hwmon_channel_info sfp_hwmon_chip = { 1177 .type = hwmon_chip, 1178 .config = sfp_hwmon_chip_config, 1179 }; 1180 1181 static u32 sfp_hwmon_temp_config[] = { 1182 HWMON_T_INPUT | 1183 HWMON_T_MAX | HWMON_T_MIN | 1184 HWMON_T_MAX_ALARM | HWMON_T_MIN_ALARM | 1185 HWMON_T_CRIT | HWMON_T_LCRIT | 1186 HWMON_T_CRIT_ALARM | HWMON_T_LCRIT_ALARM | 1187 HWMON_T_LABEL, 1188 0, 1189 }; 1190 1191 static const struct hwmon_channel_info sfp_hwmon_temp_channel_info = { 1192 .type = hwmon_temp, 1193 .config = sfp_hwmon_temp_config, 1194 }; 1195 1196 static u32 sfp_hwmon_vcc_config[] = { 1197 HWMON_I_INPUT | 1198 HWMON_I_MAX | HWMON_I_MIN | 1199 HWMON_I_MAX_ALARM | HWMON_I_MIN_ALARM | 1200 HWMON_I_CRIT | HWMON_I_LCRIT | 1201 HWMON_I_CRIT_ALARM | HWMON_I_LCRIT_ALARM | 1202 HWMON_I_LABEL, 1203 0, 1204 }; 1205 1206 static const struct hwmon_channel_info sfp_hwmon_vcc_channel_info = { 1207 .type = hwmon_in, 1208 .config = sfp_hwmon_vcc_config, 1209 }; 1210 1211 static u32 sfp_hwmon_bias_config[] = { 1212 HWMON_C_INPUT | 1213 HWMON_C_MAX | HWMON_C_MIN | 1214 HWMON_C_MAX_ALARM | HWMON_C_MIN_ALARM | 1215 HWMON_C_CRIT | HWMON_C_LCRIT | 1216 HWMON_C_CRIT_ALARM | HWMON_C_LCRIT_ALARM | 1217 HWMON_C_LABEL, 1218 0, 1219 }; 1220 1221 static const struct hwmon_channel_info sfp_hwmon_bias_channel_info = { 1222 .type = hwmon_curr, 1223 .config = sfp_hwmon_bias_config, 1224 }; 1225 1226 static u32 sfp_hwmon_power_config[] = { 1227 /* Transmit power */ 1228 HWMON_P_INPUT | 1229 HWMON_P_MAX | HWMON_P_MIN | 1230 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM | 1231 HWMON_P_CRIT | HWMON_P_LCRIT | 1232 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM | 1233 HWMON_P_LABEL, 1234 /* Receive power */ 1235 HWMON_P_INPUT | 1236 HWMON_P_MAX | HWMON_P_MIN | 1237 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM | 1238 HWMON_P_CRIT | HWMON_P_LCRIT | 1239 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM | 1240 HWMON_P_LABEL, 1241 0, 1242 }; 1243 1244 static const struct hwmon_channel_info sfp_hwmon_power_channel_info = { 1245 .type = hwmon_power, 1246 .config = sfp_hwmon_power_config, 1247 }; 1248 1249 static const struct hwmon_channel_info *sfp_hwmon_info[] = { 1250 &sfp_hwmon_chip, 1251 &sfp_hwmon_vcc_channel_info, 1252 &sfp_hwmon_temp_channel_info, 1253 &sfp_hwmon_bias_channel_info, 1254 &sfp_hwmon_power_channel_info, 1255 NULL, 1256 }; 1257 1258 static const struct hwmon_chip_info sfp_hwmon_chip_info = { 1259 .ops = &sfp_hwmon_ops, 1260 .info = sfp_hwmon_info, 1261 }; 1262 1263 static void sfp_hwmon_probe(struct work_struct *work) 1264 { 1265 struct sfp *sfp = container_of(work, struct sfp, hwmon_probe.work); 1266 int err, i; 1267 1268 err = sfp_read(sfp, true, 0, &sfp->diag, sizeof(sfp->diag)); 1269 if (err < 0) { 1270 if (sfp->hwmon_tries--) { 1271 mod_delayed_work(system_wq, &sfp->hwmon_probe, 1272 T_PROBE_RETRY_SLOW); 1273 } else { 1274 dev_warn(sfp->dev, "hwmon probe failed: %d\n", err); 1275 } 1276 return; 1277 } 1278 1279 sfp->hwmon_name = kstrdup(dev_name(sfp->dev), GFP_KERNEL); 1280 if (!sfp->hwmon_name) { 1281 dev_err(sfp->dev, "out of memory for hwmon name\n"); 1282 return; 1283 } 1284 1285 for (i = 0; sfp->hwmon_name[i]; i++) 1286 if (hwmon_is_bad_char(sfp->hwmon_name[i])) 1287 sfp->hwmon_name[i] = '_'; 1288 1289 sfp->hwmon_dev = hwmon_device_register_with_info(sfp->dev, 1290 sfp->hwmon_name, sfp, 1291 &sfp_hwmon_chip_info, 1292 NULL); 1293 if (IS_ERR(sfp->hwmon_dev)) 1294 dev_err(sfp->dev, "failed to register hwmon device: %ld\n", 1295 PTR_ERR(sfp->hwmon_dev)); 1296 } 1297 1298 static int sfp_hwmon_insert(struct sfp *sfp) 1299 { 1300 if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE) 1301 return 0; 1302 1303 if (!(sfp->id.ext.diagmon & SFP_DIAGMON_DDM)) 1304 return 0; 1305 1306 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE) 1307 /* This driver in general does not support address 1308 * change. 1309 */ 1310 return 0; 1311 1312 mod_delayed_work(system_wq, &sfp->hwmon_probe, 1); 1313 sfp->hwmon_tries = R_PROBE_RETRY_SLOW; 1314 1315 return 0; 1316 } 1317 1318 static void sfp_hwmon_remove(struct sfp *sfp) 1319 { 1320 cancel_delayed_work_sync(&sfp->hwmon_probe); 1321 if (!IS_ERR_OR_NULL(sfp->hwmon_dev)) { 1322 hwmon_device_unregister(sfp->hwmon_dev); 1323 sfp->hwmon_dev = NULL; 1324 kfree(sfp->hwmon_name); 1325 } 1326 } 1327 1328 static int sfp_hwmon_init(struct sfp *sfp) 1329 { 1330 INIT_DELAYED_WORK(&sfp->hwmon_probe, sfp_hwmon_probe); 1331 1332 return 0; 1333 } 1334 1335 static void sfp_hwmon_exit(struct sfp *sfp) 1336 { 1337 cancel_delayed_work_sync(&sfp->hwmon_probe); 1338 } 1339 #else 1340 static int sfp_hwmon_insert(struct sfp *sfp) 1341 { 1342 return 0; 1343 } 1344 1345 static void sfp_hwmon_remove(struct sfp *sfp) 1346 { 1347 } 1348 1349 static int sfp_hwmon_init(struct sfp *sfp) 1350 { 1351 return 0; 1352 } 1353 1354 static void sfp_hwmon_exit(struct sfp *sfp) 1355 { 1356 } 1357 #endif 1358 1359 /* Helpers */ 1360 static void sfp_module_tx_disable(struct sfp *sfp) 1361 { 1362 dev_dbg(sfp->dev, "tx disable %u -> %u\n", 1363 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1); 1364 sfp->state |= SFP_F_TX_DISABLE; 1365 sfp_set_state(sfp, sfp->state); 1366 } 1367 1368 static void sfp_module_tx_enable(struct sfp *sfp) 1369 { 1370 dev_dbg(sfp->dev, "tx disable %u -> %u\n", 1371 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0); 1372 sfp->state &= ~SFP_F_TX_DISABLE; 1373 sfp_set_state(sfp, sfp->state); 1374 } 1375 1376 static void sfp_module_tx_fault_reset(struct sfp *sfp) 1377 { 1378 unsigned int state = sfp->state; 1379 1380 if (state & SFP_F_TX_DISABLE) 1381 return; 1382 1383 sfp_set_state(sfp, state | SFP_F_TX_DISABLE); 1384 1385 udelay(T_RESET_US); 1386 1387 sfp_set_state(sfp, state); 1388 } 1389 1390 /* SFP state machine */ 1391 static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout) 1392 { 1393 if (timeout) 1394 mod_delayed_work(system_power_efficient_wq, &sfp->timeout, 1395 timeout); 1396 else 1397 cancel_delayed_work(&sfp->timeout); 1398 } 1399 1400 static void sfp_sm_next(struct sfp *sfp, unsigned int state, 1401 unsigned int timeout) 1402 { 1403 sfp->sm_state = state; 1404 sfp_sm_set_timer(sfp, timeout); 1405 } 1406 1407 static void sfp_sm_mod_next(struct sfp *sfp, unsigned int state, 1408 unsigned int timeout) 1409 { 1410 sfp->sm_mod_state = state; 1411 sfp_sm_set_timer(sfp, timeout); 1412 } 1413 1414 static void sfp_sm_phy_detach(struct sfp *sfp) 1415 { 1416 sfp_remove_phy(sfp->sfp_bus); 1417 phy_device_remove(sfp->mod_phy); 1418 phy_device_free(sfp->mod_phy); 1419 sfp->mod_phy = NULL; 1420 } 1421 1422 static int sfp_sm_probe_phy(struct sfp *sfp, bool is_c45) 1423 { 1424 struct phy_device *phy; 1425 int err; 1426 1427 phy = get_phy_device(sfp->i2c_mii, SFP_PHY_ADDR, is_c45); 1428 if (phy == ERR_PTR(-ENODEV)) 1429 return PTR_ERR(phy); 1430 if (IS_ERR(phy)) { 1431 dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy)); 1432 return PTR_ERR(phy); 1433 } 1434 1435 err = phy_device_register(phy); 1436 if (err) { 1437 phy_device_free(phy); 1438 dev_err(sfp->dev, "phy_device_register failed: %d\n", err); 1439 return err; 1440 } 1441 1442 err = sfp_add_phy(sfp->sfp_bus, phy); 1443 if (err) { 1444 phy_device_remove(phy); 1445 phy_device_free(phy); 1446 dev_err(sfp->dev, "sfp_add_phy failed: %d\n", err); 1447 return err; 1448 } 1449 1450 sfp->mod_phy = phy; 1451 1452 return 0; 1453 } 1454 1455 static void sfp_sm_link_up(struct sfp *sfp) 1456 { 1457 sfp_link_up(sfp->sfp_bus); 1458 sfp_sm_next(sfp, SFP_S_LINK_UP, 0); 1459 } 1460 1461 static void sfp_sm_link_down(struct sfp *sfp) 1462 { 1463 sfp_link_down(sfp->sfp_bus); 1464 } 1465 1466 static void sfp_sm_link_check_los(struct sfp *sfp) 1467 { 1468 unsigned int los = sfp->state & SFP_F_LOS; 1469 1470 /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL 1471 * are set, we assume that no LOS signal is available. 1472 */ 1473 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED)) 1474 los ^= SFP_F_LOS; 1475 else if (!(sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL))) 1476 los = 0; 1477 1478 if (los) 1479 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0); 1480 else 1481 sfp_sm_link_up(sfp); 1482 } 1483 1484 static bool sfp_los_event_active(struct sfp *sfp, unsigned int event) 1485 { 1486 return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) && 1487 event == SFP_E_LOS_LOW) || 1488 (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) && 1489 event == SFP_E_LOS_HIGH); 1490 } 1491 1492 static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event) 1493 { 1494 return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) && 1495 event == SFP_E_LOS_HIGH) || 1496 (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) && 1497 event == SFP_E_LOS_LOW); 1498 } 1499 1500 static void sfp_sm_fault(struct sfp *sfp, unsigned int next_state, bool warn) 1501 { 1502 if (sfp->sm_fault_retries && !--sfp->sm_fault_retries) { 1503 dev_err(sfp->dev, 1504 "module persistently indicates fault, disabling\n"); 1505 sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0); 1506 } else { 1507 if (warn) 1508 dev_err(sfp->dev, "module transmit fault indicated\n"); 1509 1510 sfp_sm_next(sfp, next_state, T_FAULT_RECOVER); 1511 } 1512 } 1513 1514 /* Probe a SFP for a PHY device if the module supports copper - the PHY 1515 * normally sits at I2C bus address 0x56, and may either be a clause 22 1516 * or clause 45 PHY. 1517 * 1518 * Clause 22 copper SFP modules normally operate in Cisco SGMII mode with 1519 * negotiation enabled, but some may be in 1000base-X - which is for the 1520 * PHY driver to determine. 1521 * 1522 * Clause 45 copper SFP+ modules (10G) appear to switch their interface 1523 * mode according to the negotiated line speed. 1524 */ 1525 static int sfp_sm_probe_for_phy(struct sfp *sfp) 1526 { 1527 int err = 0; 1528 1529 switch (sfp->id.base.extended_cc) { 1530 case SFF8024_ECC_10GBASE_T_SFI: 1531 case SFF8024_ECC_10GBASE_T_SR: 1532 case SFF8024_ECC_5GBASE_T: 1533 case SFF8024_ECC_2_5GBASE_T: 1534 err = sfp_sm_probe_phy(sfp, true); 1535 break; 1536 1537 default: 1538 if (sfp->id.base.e1000_base_t) 1539 err = sfp_sm_probe_phy(sfp, false); 1540 break; 1541 } 1542 return err; 1543 } 1544 1545 static int sfp_module_parse_power(struct sfp *sfp) 1546 { 1547 u32 power_mW = 1000; 1548 1549 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL)) 1550 power_mW = 1500; 1551 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL)) 1552 power_mW = 2000; 1553 1554 if (power_mW > sfp->max_power_mW) { 1555 /* Module power specification exceeds the allowed maximum. */ 1556 if (sfp->id.ext.sff8472_compliance == 1557 SFP_SFF8472_COMPLIANCE_NONE && 1558 !(sfp->id.ext.diagmon & SFP_DIAGMON_DDM)) { 1559 /* The module appears not to implement bus address 1560 * 0xa2, so assume that the module powers up in the 1561 * indicated mode. 1562 */ 1563 dev_err(sfp->dev, 1564 "Host does not support %u.%uW modules\n", 1565 power_mW / 1000, (power_mW / 100) % 10); 1566 return -EINVAL; 1567 } else { 1568 dev_warn(sfp->dev, 1569 "Host does not support %u.%uW modules, module left in power mode 1\n", 1570 power_mW / 1000, (power_mW / 100) % 10); 1571 return 0; 1572 } 1573 } 1574 1575 /* If the module requires a higher power mode, but also requires 1576 * an address change sequence, warn the user that the module may 1577 * not be functional. 1578 */ 1579 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE && power_mW > 1000) { 1580 dev_warn(sfp->dev, 1581 "Address Change Sequence not supported but module requires %u.%uW, module may not be functional\n", 1582 power_mW / 1000, (power_mW / 100) % 10); 1583 return 0; 1584 } 1585 1586 sfp->module_power_mW = power_mW; 1587 1588 return 0; 1589 } 1590 1591 static int sfp_sm_mod_hpower(struct sfp *sfp, bool enable) 1592 { 1593 u8 val; 1594 int err; 1595 1596 err = sfp_read(sfp, true, SFP_EXT_STATUS, &val, sizeof(val)); 1597 if (err != sizeof(val)) { 1598 dev_err(sfp->dev, "Failed to read EEPROM: %d\n", err); 1599 return -EAGAIN; 1600 } 1601 1602 /* DM7052 reports as a high power module, responds to reads (with 1603 * all bytes 0xff) at 0x51 but does not accept writes. In any case, 1604 * if the bit is already set, we're already in high power mode. 1605 */ 1606 if (!!(val & BIT(0)) == enable) 1607 return 0; 1608 1609 if (enable) 1610 val |= BIT(0); 1611 else 1612 val &= ~BIT(0); 1613 1614 err = sfp_write(sfp, true, SFP_EXT_STATUS, &val, sizeof(val)); 1615 if (err != sizeof(val)) { 1616 dev_err(sfp->dev, "Failed to write EEPROM: %d\n", err); 1617 return -EAGAIN; 1618 } 1619 1620 if (enable) 1621 dev_info(sfp->dev, "Module switched to %u.%uW power level\n", 1622 sfp->module_power_mW / 1000, 1623 (sfp->module_power_mW / 100) % 10); 1624 1625 return 0; 1626 } 1627 1628 static int sfp_sm_mod_probe(struct sfp *sfp, bool report) 1629 { 1630 /* SFP module inserted - read I2C data */ 1631 struct sfp_eeprom_id id; 1632 bool cotsworks; 1633 u8 check; 1634 int ret; 1635 1636 ret = sfp_read(sfp, false, 0, &id, sizeof(id)); 1637 if (ret < 0) { 1638 if (report) 1639 dev_err(sfp->dev, "failed to read EEPROM: %d\n", ret); 1640 return -EAGAIN; 1641 } 1642 1643 if (ret != sizeof(id)) { 1644 dev_err(sfp->dev, "EEPROM short read: %d\n", ret); 1645 return -EAGAIN; 1646 } 1647 1648 /* Cotsworks do not seem to update the checksums when they 1649 * do the final programming with the final module part number, 1650 * serial number and date code. 1651 */ 1652 cotsworks = !memcmp(id.base.vendor_name, "COTSWORKS ", 16); 1653 1654 /* Validate the checksum over the base structure */ 1655 check = sfp_check(&id.base, sizeof(id.base) - 1); 1656 if (check != id.base.cc_base) { 1657 if (cotsworks) { 1658 dev_warn(sfp->dev, 1659 "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n", 1660 check, id.base.cc_base); 1661 } else { 1662 dev_err(sfp->dev, 1663 "EEPROM base structure checksum failure: 0x%02x != 0x%02x\n", 1664 check, id.base.cc_base); 1665 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET, 1666 16, 1, &id, sizeof(id), true); 1667 return -EINVAL; 1668 } 1669 } 1670 1671 check = sfp_check(&id.ext, sizeof(id.ext) - 1); 1672 if (check != id.ext.cc_ext) { 1673 if (cotsworks) { 1674 dev_warn(sfp->dev, 1675 "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n", 1676 check, id.ext.cc_ext); 1677 } else { 1678 dev_err(sfp->dev, 1679 "EEPROM extended structure checksum failure: 0x%02x != 0x%02x\n", 1680 check, id.ext.cc_ext); 1681 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET, 1682 16, 1, &id, sizeof(id), true); 1683 memset(&id.ext, 0, sizeof(id.ext)); 1684 } 1685 } 1686 1687 sfp->id = id; 1688 1689 dev_info(sfp->dev, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n", 1690 (int)sizeof(id.base.vendor_name), id.base.vendor_name, 1691 (int)sizeof(id.base.vendor_pn), id.base.vendor_pn, 1692 (int)sizeof(id.base.vendor_rev), id.base.vendor_rev, 1693 (int)sizeof(id.ext.vendor_sn), id.ext.vendor_sn, 1694 (int)sizeof(id.ext.datecode), id.ext.datecode); 1695 1696 /* Check whether we support this module */ 1697 if (!sfp->type->module_supported(&id)) { 1698 dev_err(sfp->dev, 1699 "module is not supported - phys id 0x%02x 0x%02x\n", 1700 sfp->id.base.phys_id, sfp->id.base.phys_ext_id); 1701 return -EINVAL; 1702 } 1703 1704 /* If the module requires address swap mode, warn about it */ 1705 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE) 1706 dev_warn(sfp->dev, 1707 "module address swap to access page 0xA2 is not supported.\n"); 1708 1709 /* Parse the module power requirement */ 1710 ret = sfp_module_parse_power(sfp); 1711 if (ret < 0) 1712 return ret; 1713 1714 if (!memcmp(id.base.vendor_name, "ALCATELLUCENT ", 16) && 1715 !memcmp(id.base.vendor_pn, "3FE46541AA ", 16)) 1716 sfp->module_t_start_up = T_START_UP_BAD_GPON; 1717 else 1718 sfp->module_t_start_up = T_START_UP; 1719 1720 return 0; 1721 } 1722 1723 static void sfp_sm_mod_remove(struct sfp *sfp) 1724 { 1725 if (sfp->sm_mod_state > SFP_MOD_WAITDEV) 1726 sfp_module_remove(sfp->sfp_bus); 1727 1728 sfp_hwmon_remove(sfp); 1729 1730 memset(&sfp->id, 0, sizeof(sfp->id)); 1731 sfp->module_power_mW = 0; 1732 1733 dev_info(sfp->dev, "module removed\n"); 1734 } 1735 1736 /* This state machine tracks the upstream's state */ 1737 static void sfp_sm_device(struct sfp *sfp, unsigned int event) 1738 { 1739 switch (sfp->sm_dev_state) { 1740 default: 1741 if (event == SFP_E_DEV_ATTACH) 1742 sfp->sm_dev_state = SFP_DEV_DOWN; 1743 break; 1744 1745 case SFP_DEV_DOWN: 1746 if (event == SFP_E_DEV_DETACH) 1747 sfp->sm_dev_state = SFP_DEV_DETACHED; 1748 else if (event == SFP_E_DEV_UP) 1749 sfp->sm_dev_state = SFP_DEV_UP; 1750 break; 1751 1752 case SFP_DEV_UP: 1753 if (event == SFP_E_DEV_DETACH) 1754 sfp->sm_dev_state = SFP_DEV_DETACHED; 1755 else if (event == SFP_E_DEV_DOWN) 1756 sfp->sm_dev_state = SFP_DEV_DOWN; 1757 break; 1758 } 1759 } 1760 1761 /* This state machine tracks the insert/remove state of the module, probes 1762 * the on-board EEPROM, and sets up the power level. 1763 */ 1764 static void sfp_sm_module(struct sfp *sfp, unsigned int event) 1765 { 1766 int err; 1767 1768 /* Handle remove event globally, it resets this state machine */ 1769 if (event == SFP_E_REMOVE) { 1770 if (sfp->sm_mod_state > SFP_MOD_PROBE) 1771 sfp_sm_mod_remove(sfp); 1772 sfp_sm_mod_next(sfp, SFP_MOD_EMPTY, 0); 1773 return; 1774 } 1775 1776 /* Handle device detach globally */ 1777 if (sfp->sm_dev_state < SFP_DEV_DOWN && 1778 sfp->sm_mod_state > SFP_MOD_WAITDEV) { 1779 if (sfp->module_power_mW > 1000 && 1780 sfp->sm_mod_state > SFP_MOD_HPOWER) 1781 sfp_sm_mod_hpower(sfp, false); 1782 sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0); 1783 return; 1784 } 1785 1786 switch (sfp->sm_mod_state) { 1787 default: 1788 if (event == SFP_E_INSERT) { 1789 sfp_sm_mod_next(sfp, SFP_MOD_PROBE, T_SERIAL); 1790 sfp->sm_mod_tries_init = R_PROBE_RETRY_INIT; 1791 sfp->sm_mod_tries = R_PROBE_RETRY_SLOW; 1792 } 1793 break; 1794 1795 case SFP_MOD_PROBE: 1796 /* Wait for T_PROBE_INIT to time out */ 1797 if (event != SFP_E_TIMEOUT) 1798 break; 1799 1800 err = sfp_sm_mod_probe(sfp, sfp->sm_mod_tries == 1); 1801 if (err == -EAGAIN) { 1802 if (sfp->sm_mod_tries_init && 1803 --sfp->sm_mod_tries_init) { 1804 sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT); 1805 break; 1806 } else if (sfp->sm_mod_tries && --sfp->sm_mod_tries) { 1807 if (sfp->sm_mod_tries == R_PROBE_RETRY_SLOW - 1) 1808 dev_warn(sfp->dev, 1809 "please wait, module slow to respond\n"); 1810 sfp_sm_set_timer(sfp, T_PROBE_RETRY_SLOW); 1811 break; 1812 } 1813 } 1814 if (err < 0) { 1815 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0); 1816 break; 1817 } 1818 1819 err = sfp_hwmon_insert(sfp); 1820 if (err) 1821 dev_warn(sfp->dev, "hwmon probe failed: %d\n", err); 1822 1823 sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0); 1824 /* fall through */ 1825 case SFP_MOD_WAITDEV: 1826 /* Ensure that the device is attached before proceeding */ 1827 if (sfp->sm_dev_state < SFP_DEV_DOWN) 1828 break; 1829 1830 /* Report the module insertion to the upstream device */ 1831 err = sfp_module_insert(sfp->sfp_bus, &sfp->id); 1832 if (err < 0) { 1833 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0); 1834 break; 1835 } 1836 1837 /* If this is a power level 1 module, we are done */ 1838 if (sfp->module_power_mW <= 1000) 1839 goto insert; 1840 1841 sfp_sm_mod_next(sfp, SFP_MOD_HPOWER, 0); 1842 /* fall through */ 1843 case SFP_MOD_HPOWER: 1844 /* Enable high power mode */ 1845 err = sfp_sm_mod_hpower(sfp, true); 1846 if (err < 0) { 1847 if (err != -EAGAIN) { 1848 sfp_module_remove(sfp->sfp_bus); 1849 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0); 1850 } else { 1851 sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT); 1852 } 1853 break; 1854 } 1855 1856 sfp_sm_mod_next(sfp, SFP_MOD_WAITPWR, T_HPOWER_LEVEL); 1857 break; 1858 1859 case SFP_MOD_WAITPWR: 1860 /* Wait for T_HPOWER_LEVEL to time out */ 1861 if (event != SFP_E_TIMEOUT) 1862 break; 1863 1864 insert: 1865 sfp_sm_mod_next(sfp, SFP_MOD_PRESENT, 0); 1866 break; 1867 1868 case SFP_MOD_PRESENT: 1869 case SFP_MOD_ERROR: 1870 break; 1871 } 1872 } 1873 1874 static void sfp_sm_main(struct sfp *sfp, unsigned int event) 1875 { 1876 unsigned long timeout; 1877 int ret; 1878 1879 /* Some events are global */ 1880 if (sfp->sm_state != SFP_S_DOWN && 1881 (sfp->sm_mod_state != SFP_MOD_PRESENT || 1882 sfp->sm_dev_state != SFP_DEV_UP)) { 1883 if (sfp->sm_state == SFP_S_LINK_UP && 1884 sfp->sm_dev_state == SFP_DEV_UP) 1885 sfp_sm_link_down(sfp); 1886 if (sfp->sm_state > SFP_S_INIT) 1887 sfp_module_stop(sfp->sfp_bus); 1888 if (sfp->mod_phy) 1889 sfp_sm_phy_detach(sfp); 1890 sfp_module_tx_disable(sfp); 1891 sfp_soft_stop_poll(sfp); 1892 sfp_sm_next(sfp, SFP_S_DOWN, 0); 1893 return; 1894 } 1895 1896 /* The main state machine */ 1897 switch (sfp->sm_state) { 1898 case SFP_S_DOWN: 1899 if (sfp->sm_mod_state != SFP_MOD_PRESENT || 1900 sfp->sm_dev_state != SFP_DEV_UP) 1901 break; 1902 1903 if (!(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) 1904 sfp_soft_start_poll(sfp); 1905 1906 sfp_module_tx_enable(sfp); 1907 1908 /* Initialise the fault clearance retries */ 1909 sfp->sm_fault_retries = N_FAULT_INIT; 1910 1911 /* We need to check the TX_FAULT state, which is not defined 1912 * while TX_DISABLE is asserted. The earliest we want to do 1913 * anything (such as probe for a PHY) is 50ms. 1914 */ 1915 sfp_sm_next(sfp, SFP_S_WAIT, T_WAIT); 1916 break; 1917 1918 case SFP_S_WAIT: 1919 if (event != SFP_E_TIMEOUT) 1920 break; 1921 1922 if (sfp->state & SFP_F_TX_FAULT) { 1923 /* Wait up to t_init (SFF-8472) or t_start_up (SFF-8431) 1924 * from the TX_DISABLE deassertion for the module to 1925 * initialise, which is indicated by TX_FAULT 1926 * deasserting. 1927 */ 1928 timeout = sfp->module_t_start_up; 1929 if (timeout > T_WAIT) 1930 timeout -= T_WAIT; 1931 else 1932 timeout = 1; 1933 1934 sfp_sm_next(sfp, SFP_S_INIT, timeout); 1935 } else { 1936 /* TX_FAULT is not asserted, assume the module has 1937 * finished initialising. 1938 */ 1939 goto init_done; 1940 } 1941 break; 1942 1943 case SFP_S_INIT: 1944 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) { 1945 /* TX_FAULT is still asserted after t_init or 1946 * or t_start_up, so assume there is a fault. 1947 */ 1948 sfp_sm_fault(sfp, SFP_S_INIT_TX_FAULT, 1949 sfp->sm_fault_retries == N_FAULT_INIT); 1950 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) { 1951 init_done: 1952 sfp->sm_phy_retries = R_PHY_RETRY; 1953 goto phy_probe; 1954 } 1955 break; 1956 1957 case SFP_S_INIT_PHY: 1958 if (event != SFP_E_TIMEOUT) 1959 break; 1960 phy_probe: 1961 /* TX_FAULT deasserted or we timed out with TX_FAULT 1962 * clear. Probe for the PHY and check the LOS state. 1963 */ 1964 ret = sfp_sm_probe_for_phy(sfp); 1965 if (ret == -ENODEV) { 1966 if (--sfp->sm_phy_retries) { 1967 sfp_sm_next(sfp, SFP_S_INIT_PHY, T_PHY_RETRY); 1968 break; 1969 } else { 1970 dev_info(sfp->dev, "no PHY detected\n"); 1971 } 1972 } else if (ret) { 1973 sfp_sm_next(sfp, SFP_S_FAIL, 0); 1974 break; 1975 } 1976 if (sfp_module_start(sfp->sfp_bus)) { 1977 sfp_sm_next(sfp, SFP_S_FAIL, 0); 1978 break; 1979 } 1980 sfp_sm_link_check_los(sfp); 1981 1982 /* Reset the fault retry count */ 1983 sfp->sm_fault_retries = N_FAULT; 1984 break; 1985 1986 case SFP_S_INIT_TX_FAULT: 1987 if (event == SFP_E_TIMEOUT) { 1988 sfp_module_tx_fault_reset(sfp); 1989 sfp_sm_next(sfp, SFP_S_INIT, sfp->module_t_start_up); 1990 } 1991 break; 1992 1993 case SFP_S_WAIT_LOS: 1994 if (event == SFP_E_TX_FAULT) 1995 sfp_sm_fault(sfp, SFP_S_TX_FAULT, true); 1996 else if (sfp_los_event_inactive(sfp, event)) 1997 sfp_sm_link_up(sfp); 1998 break; 1999 2000 case SFP_S_LINK_UP: 2001 if (event == SFP_E_TX_FAULT) { 2002 sfp_sm_link_down(sfp); 2003 sfp_sm_fault(sfp, SFP_S_TX_FAULT, true); 2004 } else if (sfp_los_event_active(sfp, event)) { 2005 sfp_sm_link_down(sfp); 2006 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0); 2007 } 2008 break; 2009 2010 case SFP_S_TX_FAULT: 2011 if (event == SFP_E_TIMEOUT) { 2012 sfp_module_tx_fault_reset(sfp); 2013 sfp_sm_next(sfp, SFP_S_REINIT, sfp->module_t_start_up); 2014 } 2015 break; 2016 2017 case SFP_S_REINIT: 2018 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) { 2019 sfp_sm_fault(sfp, SFP_S_TX_FAULT, false); 2020 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) { 2021 dev_info(sfp->dev, "module transmit fault recovered\n"); 2022 sfp_sm_link_check_los(sfp); 2023 } 2024 break; 2025 2026 case SFP_S_TX_DISABLE: 2027 break; 2028 } 2029 } 2030 2031 static void sfp_sm_event(struct sfp *sfp, unsigned int event) 2032 { 2033 mutex_lock(&sfp->sm_mutex); 2034 2035 dev_dbg(sfp->dev, "SM: enter %s:%s:%s event %s\n", 2036 mod_state_to_str(sfp->sm_mod_state), 2037 dev_state_to_str(sfp->sm_dev_state), 2038 sm_state_to_str(sfp->sm_state), 2039 event_to_str(event)); 2040 2041 sfp_sm_device(sfp, event); 2042 sfp_sm_module(sfp, event); 2043 sfp_sm_main(sfp, event); 2044 2045 dev_dbg(sfp->dev, "SM: exit %s:%s:%s\n", 2046 mod_state_to_str(sfp->sm_mod_state), 2047 dev_state_to_str(sfp->sm_dev_state), 2048 sm_state_to_str(sfp->sm_state)); 2049 2050 mutex_unlock(&sfp->sm_mutex); 2051 } 2052 2053 static void sfp_attach(struct sfp *sfp) 2054 { 2055 sfp_sm_event(sfp, SFP_E_DEV_ATTACH); 2056 } 2057 2058 static void sfp_detach(struct sfp *sfp) 2059 { 2060 sfp_sm_event(sfp, SFP_E_DEV_DETACH); 2061 } 2062 2063 static void sfp_start(struct sfp *sfp) 2064 { 2065 sfp_sm_event(sfp, SFP_E_DEV_UP); 2066 } 2067 2068 static void sfp_stop(struct sfp *sfp) 2069 { 2070 sfp_sm_event(sfp, SFP_E_DEV_DOWN); 2071 } 2072 2073 static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo) 2074 { 2075 /* locking... and check module is present */ 2076 2077 if (sfp->id.ext.sff8472_compliance && 2078 !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) { 2079 modinfo->type = ETH_MODULE_SFF_8472; 2080 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN; 2081 } else { 2082 modinfo->type = ETH_MODULE_SFF_8079; 2083 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN; 2084 } 2085 return 0; 2086 } 2087 2088 static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee, 2089 u8 *data) 2090 { 2091 unsigned int first, last, len; 2092 int ret; 2093 2094 if (ee->len == 0) 2095 return -EINVAL; 2096 2097 first = ee->offset; 2098 last = ee->offset + ee->len; 2099 if (first < ETH_MODULE_SFF_8079_LEN) { 2100 len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN); 2101 len -= first; 2102 2103 ret = sfp_read(sfp, false, first, data, len); 2104 if (ret < 0) 2105 return ret; 2106 2107 first += len; 2108 data += len; 2109 } 2110 if (first < ETH_MODULE_SFF_8472_LEN && last > ETH_MODULE_SFF_8079_LEN) { 2111 len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN); 2112 len -= first; 2113 first -= ETH_MODULE_SFF_8079_LEN; 2114 2115 ret = sfp_read(sfp, true, first, data, len); 2116 if (ret < 0) 2117 return ret; 2118 } 2119 return 0; 2120 } 2121 2122 static const struct sfp_socket_ops sfp_module_ops = { 2123 .attach = sfp_attach, 2124 .detach = sfp_detach, 2125 .start = sfp_start, 2126 .stop = sfp_stop, 2127 .module_info = sfp_module_info, 2128 .module_eeprom = sfp_module_eeprom, 2129 }; 2130 2131 static void sfp_timeout(struct work_struct *work) 2132 { 2133 struct sfp *sfp = container_of(work, struct sfp, timeout.work); 2134 2135 rtnl_lock(); 2136 sfp_sm_event(sfp, SFP_E_TIMEOUT); 2137 rtnl_unlock(); 2138 } 2139 2140 static void sfp_check_state(struct sfp *sfp) 2141 { 2142 unsigned int state, i, changed; 2143 2144 mutex_lock(&sfp->st_mutex); 2145 state = sfp_get_state(sfp); 2146 changed = state ^ sfp->state; 2147 changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT; 2148 2149 for (i = 0; i < GPIO_MAX; i++) 2150 if (changed & BIT(i)) 2151 dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i], 2152 !!(sfp->state & BIT(i)), !!(state & BIT(i))); 2153 2154 state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT); 2155 sfp->state = state; 2156 2157 rtnl_lock(); 2158 if (changed & SFP_F_PRESENT) 2159 sfp_sm_event(sfp, state & SFP_F_PRESENT ? 2160 SFP_E_INSERT : SFP_E_REMOVE); 2161 2162 if (changed & SFP_F_TX_FAULT) 2163 sfp_sm_event(sfp, state & SFP_F_TX_FAULT ? 2164 SFP_E_TX_FAULT : SFP_E_TX_CLEAR); 2165 2166 if (changed & SFP_F_LOS) 2167 sfp_sm_event(sfp, state & SFP_F_LOS ? 2168 SFP_E_LOS_HIGH : SFP_E_LOS_LOW); 2169 rtnl_unlock(); 2170 mutex_unlock(&sfp->st_mutex); 2171 } 2172 2173 static irqreturn_t sfp_irq(int irq, void *data) 2174 { 2175 struct sfp *sfp = data; 2176 2177 sfp_check_state(sfp); 2178 2179 return IRQ_HANDLED; 2180 } 2181 2182 static void sfp_poll(struct work_struct *work) 2183 { 2184 struct sfp *sfp = container_of(work, struct sfp, poll.work); 2185 2186 sfp_check_state(sfp); 2187 2188 if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) || 2189 sfp->need_poll) 2190 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies); 2191 } 2192 2193 static struct sfp *sfp_alloc(struct device *dev) 2194 { 2195 struct sfp *sfp; 2196 2197 sfp = kzalloc(sizeof(*sfp), GFP_KERNEL); 2198 if (!sfp) 2199 return ERR_PTR(-ENOMEM); 2200 2201 sfp->dev = dev; 2202 2203 mutex_init(&sfp->sm_mutex); 2204 mutex_init(&sfp->st_mutex); 2205 INIT_DELAYED_WORK(&sfp->poll, sfp_poll); 2206 INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout); 2207 2208 sfp_hwmon_init(sfp); 2209 2210 return sfp; 2211 } 2212 2213 static void sfp_cleanup(void *data) 2214 { 2215 struct sfp *sfp = data; 2216 2217 sfp_hwmon_exit(sfp); 2218 2219 cancel_delayed_work_sync(&sfp->poll); 2220 cancel_delayed_work_sync(&sfp->timeout); 2221 if (sfp->i2c_mii) { 2222 mdiobus_unregister(sfp->i2c_mii); 2223 mdiobus_free(sfp->i2c_mii); 2224 } 2225 if (sfp->i2c) 2226 i2c_put_adapter(sfp->i2c); 2227 kfree(sfp); 2228 } 2229 2230 static int sfp_probe(struct platform_device *pdev) 2231 { 2232 const struct sff_data *sff; 2233 struct i2c_adapter *i2c; 2234 struct sfp *sfp; 2235 int err, i; 2236 2237 sfp = sfp_alloc(&pdev->dev); 2238 if (IS_ERR(sfp)) 2239 return PTR_ERR(sfp); 2240 2241 platform_set_drvdata(pdev, sfp); 2242 2243 err = devm_add_action(sfp->dev, sfp_cleanup, sfp); 2244 if (err < 0) 2245 return err; 2246 2247 sff = sfp->type = &sfp_data; 2248 2249 if (pdev->dev.of_node) { 2250 struct device_node *node = pdev->dev.of_node; 2251 const struct of_device_id *id; 2252 struct device_node *np; 2253 2254 id = of_match_node(sfp_of_match, node); 2255 if (WARN_ON(!id)) 2256 return -EINVAL; 2257 2258 sff = sfp->type = id->data; 2259 2260 np = of_parse_phandle(node, "i2c-bus", 0); 2261 if (!np) { 2262 dev_err(sfp->dev, "missing 'i2c-bus' property\n"); 2263 return -ENODEV; 2264 } 2265 2266 i2c = of_find_i2c_adapter_by_node(np); 2267 of_node_put(np); 2268 } else if (has_acpi_companion(&pdev->dev)) { 2269 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev); 2270 struct fwnode_handle *fw = acpi_fwnode_handle(adev); 2271 struct fwnode_reference_args args; 2272 struct acpi_handle *acpi_handle; 2273 int ret; 2274 2275 ret = acpi_node_get_property_reference(fw, "i2c-bus", 0, &args); 2276 if (ret || !is_acpi_device_node(args.fwnode)) { 2277 dev_err(&pdev->dev, "missing 'i2c-bus' property\n"); 2278 return -ENODEV; 2279 } 2280 2281 acpi_handle = ACPI_HANDLE_FWNODE(args.fwnode); 2282 i2c = i2c_acpi_find_adapter_by_handle(acpi_handle); 2283 } else { 2284 return -EINVAL; 2285 } 2286 2287 if (!i2c) 2288 return -EPROBE_DEFER; 2289 2290 err = sfp_i2c_configure(sfp, i2c); 2291 if (err < 0) { 2292 i2c_put_adapter(i2c); 2293 return err; 2294 } 2295 2296 for (i = 0; i < GPIO_MAX; i++) 2297 if (sff->gpios & BIT(i)) { 2298 sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev, 2299 gpio_of_names[i], gpio_flags[i]); 2300 if (IS_ERR(sfp->gpio[i])) 2301 return PTR_ERR(sfp->gpio[i]); 2302 } 2303 2304 sfp->get_state = sfp_gpio_get_state; 2305 sfp->set_state = sfp_gpio_set_state; 2306 2307 /* Modules that have no detect signal are always present */ 2308 if (!(sfp->gpio[GPIO_MODDEF0])) 2309 sfp->get_state = sff_gpio_get_state; 2310 2311 device_property_read_u32(&pdev->dev, "maximum-power-milliwatt", 2312 &sfp->max_power_mW); 2313 if (!sfp->max_power_mW) 2314 sfp->max_power_mW = 1000; 2315 2316 dev_info(sfp->dev, "Host maximum power %u.%uW\n", 2317 sfp->max_power_mW / 1000, (sfp->max_power_mW / 100) % 10); 2318 2319 /* Get the initial state, and always signal TX disable, 2320 * since the network interface will not be up. 2321 */ 2322 sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE; 2323 2324 if (sfp->gpio[GPIO_RATE_SELECT] && 2325 gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT])) 2326 sfp->state |= SFP_F_RATE_SELECT; 2327 sfp_set_state(sfp, sfp->state); 2328 sfp_module_tx_disable(sfp); 2329 if (sfp->state & SFP_F_PRESENT) { 2330 rtnl_lock(); 2331 sfp_sm_event(sfp, SFP_E_INSERT); 2332 rtnl_unlock(); 2333 } 2334 2335 for (i = 0; i < GPIO_MAX; i++) { 2336 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i]) 2337 continue; 2338 2339 sfp->gpio_irq[i] = gpiod_to_irq(sfp->gpio[i]); 2340 if (!sfp->gpio_irq[i]) { 2341 sfp->need_poll = true; 2342 continue; 2343 } 2344 2345 err = devm_request_threaded_irq(sfp->dev, sfp->gpio_irq[i], 2346 NULL, sfp_irq, 2347 IRQF_ONESHOT | 2348 IRQF_TRIGGER_RISING | 2349 IRQF_TRIGGER_FALLING, 2350 dev_name(sfp->dev), sfp); 2351 if (err) { 2352 sfp->gpio_irq[i] = 0; 2353 sfp->need_poll = true; 2354 } 2355 } 2356 2357 if (sfp->need_poll) 2358 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies); 2359 2360 /* We could have an issue in cases no Tx disable pin is available or 2361 * wired as modules using a laser as their light source will continue to 2362 * be active when the fiber is removed. This could be a safety issue and 2363 * we should at least warn the user about that. 2364 */ 2365 if (!sfp->gpio[GPIO_TX_DISABLE]) 2366 dev_warn(sfp->dev, 2367 "No tx_disable pin: SFP modules will always be emitting.\n"); 2368 2369 sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops); 2370 if (!sfp->sfp_bus) 2371 return -ENOMEM; 2372 2373 return 0; 2374 } 2375 2376 static int sfp_remove(struct platform_device *pdev) 2377 { 2378 struct sfp *sfp = platform_get_drvdata(pdev); 2379 2380 sfp_unregister_socket(sfp->sfp_bus); 2381 2382 rtnl_lock(); 2383 sfp_sm_event(sfp, SFP_E_REMOVE); 2384 rtnl_unlock(); 2385 2386 return 0; 2387 } 2388 2389 static void sfp_shutdown(struct platform_device *pdev) 2390 { 2391 struct sfp *sfp = platform_get_drvdata(pdev); 2392 int i; 2393 2394 for (i = 0; i < GPIO_MAX; i++) { 2395 if (!sfp->gpio_irq[i]) 2396 continue; 2397 2398 devm_free_irq(sfp->dev, sfp->gpio_irq[i], sfp); 2399 } 2400 2401 cancel_delayed_work_sync(&sfp->poll); 2402 cancel_delayed_work_sync(&sfp->timeout); 2403 } 2404 2405 static struct platform_driver sfp_driver = { 2406 .probe = sfp_probe, 2407 .remove = sfp_remove, 2408 .shutdown = sfp_shutdown, 2409 .driver = { 2410 .name = "sfp", 2411 .of_match_table = sfp_of_match, 2412 }, 2413 }; 2414 2415 static int sfp_init(void) 2416 { 2417 poll_jiffies = msecs_to_jiffies(100); 2418 2419 return platform_driver_register(&sfp_driver); 2420 } 2421 module_init(sfp_init); 2422 2423 static void sfp_exit(void) 2424 { 2425 platform_driver_unregister(&sfp_driver); 2426 } 2427 module_exit(sfp_exit); 2428 2429 MODULE_ALIAS("platform:sfp"); 2430 MODULE_AUTHOR("Russell King"); 2431 MODULE_LICENSE("GPL v2"); 2432