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