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