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