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>
8 #include <linux/interrupt.h>
9 #include <linux/jiffies.h>
10 #include <linux/module.h>
11 #include <linux/mutex.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>
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),
74 static const char * const mod_state_strings[] = {
75 [SFP_MOD_EMPTY] = "empty",
76 [SFP_MOD_ERROR] = "error",
77 [SFP_MOD_PROBE] = "probe",
78 [SFP_MOD_WAITDEV] = "waitdev",
79 [SFP_MOD_HPOWER] = "hpower",
80 [SFP_MOD_WAITPWR] = "waitpwr",
81 [SFP_MOD_PRESENT] = "present",
84 static const char *mod_state_to_str(unsigned short mod_state)
86 if (mod_state >= ARRAY_SIZE(mod_state_strings))
87 return "Unknown module state";
88 return mod_state_strings[mod_state];
91 static const char * const dev_state_strings[] = {
92 [SFP_DEV_DETACHED] = "detached",
93 [SFP_DEV_DOWN] = "down",
97 static const char *dev_state_to_str(unsigned short dev_state)
99 if (dev_state >= ARRAY_SIZE(dev_state_strings))
100 return "Unknown device state";
101 return dev_state_strings[dev_state];
104 static const char * const event_strings[] = {
105 [SFP_E_INSERT] = "insert",
106 [SFP_E_REMOVE] = "remove",
107 [SFP_E_DEV_ATTACH] = "dev_attach",
108 [SFP_E_DEV_DETACH] = "dev_detach",
109 [SFP_E_DEV_DOWN] = "dev_down",
110 [SFP_E_DEV_UP] = "dev_up",
111 [SFP_E_TX_FAULT] = "tx_fault",
112 [SFP_E_TX_CLEAR] = "tx_clear",
113 [SFP_E_LOS_HIGH] = "los_high",
114 [SFP_E_LOS_LOW] = "los_low",
115 [SFP_E_TIMEOUT] = "timeout",
118 static const char *event_to_str(unsigned short event)
120 if (event >= ARRAY_SIZE(event_strings))
121 return "Unknown event";
122 return event_strings[event];
125 static const char * const sm_state_strings[] = {
126 [SFP_S_DOWN] = "down",
127 [SFP_S_FAIL] = "fail",
128 [SFP_S_WAIT] = "wait",
129 [SFP_S_INIT] = "init",
130 [SFP_S_INIT_PHY] = "init_phy",
131 [SFP_S_INIT_TX_FAULT] = "init_tx_fault",
132 [SFP_S_WAIT_LOS] = "wait_los",
133 [SFP_S_LINK_UP] = "link_up",
134 [SFP_S_TX_FAULT] = "tx_fault",
135 [SFP_S_REINIT] = "reinit",
136 [SFP_S_TX_DISABLE] = "rx_disable",
139 static const char *sm_state_to_str(unsigned short sm_state)
141 if (sm_state >= ARRAY_SIZE(sm_state_strings))
142 return "Unknown state";
143 return sm_state_strings[sm_state];
146 static const char *gpio_of_names[] = {
154 static const enum gpiod_flags gpio_flags[] = {
162 /* t_start_up (SFF-8431) or t_init (SFF-8472) is the time required for a
163 * non-cooled module to initialise its laser safety circuitry. We wait
164 * an initial T_WAIT period before we check the tx fault to give any PHY
165 * on board (for a copper SFP) time to initialise.
167 #define T_WAIT msecs_to_jiffies(50)
168 #define T_START_UP msecs_to_jiffies(300)
169 #define T_START_UP_BAD_GPON msecs_to_jiffies(60000)
171 /* t_reset is the time required to assert the TX_DISABLE signal to reset
172 * an indicated TX_FAULT.
174 #define T_RESET_US 10
175 #define T_FAULT_RECOVER msecs_to_jiffies(1000)
177 /* N_FAULT_INIT is the number of recovery attempts at module initialisation
178 * time. If the TX_FAULT signal is not deasserted after this number of
179 * attempts at clearing it, we decide that the module is faulty.
180 * N_FAULT is the same but after the module has initialised.
182 #define N_FAULT_INIT 5
185 /* T_PHY_RETRY is the time interval between attempts to probe the PHY.
186 * R_PHY_RETRY is the number of attempts.
188 #define T_PHY_RETRY msecs_to_jiffies(50)
189 #define R_PHY_RETRY 12
191 /* SFP module presence detection is poor: the three MOD DEF signals are
192 * the same length on the PCB, which means it's possible for MOD DEF 0 to
193 * connect before the I2C bus on MOD DEF 1/2.
195 * The SFF-8472 specifies t_serial ("Time from power on until module is
196 * ready for data transmission over the two wire serial bus.") as 300ms.
198 #define T_SERIAL msecs_to_jiffies(300)
199 #define T_HPOWER_LEVEL msecs_to_jiffies(300)
200 #define T_PROBE_RETRY_INIT msecs_to_jiffies(100)
201 #define R_PROBE_RETRY_INIT 10
202 #define T_PROBE_RETRY_SLOW msecs_to_jiffies(5000)
203 #define R_PROBE_RETRY_SLOW 12
205 /* SFP modules appear to always have their PHY configured for bus address
206 * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
208 #define SFP_PHY_ADDR 22
212 bool (*module_supported)(const struct sfp_eeprom_id *id);
217 struct i2c_adapter *i2c;
218 struct mii_bus *i2c_mii;
219 struct sfp_bus *sfp_bus;
220 struct phy_device *mod_phy;
221 const struct sff_data *type;
224 unsigned int (*get_state)(struct sfp *);
225 void (*set_state)(struct sfp *, unsigned int);
226 int (*read)(struct sfp *, bool, u8, void *, size_t);
227 int (*write)(struct sfp *, bool, u8, void *, size_t);
229 struct gpio_desc *gpio[GPIO_MAX];
230 int gpio_irq[GPIO_MAX];
234 struct mutex st_mutex; /* Protects state */
235 unsigned int state_soft_mask;
237 struct delayed_work poll;
238 struct delayed_work timeout;
239 struct mutex sm_mutex; /* Protects state machine */
240 unsigned char sm_mod_state;
241 unsigned char sm_mod_tries_init;
242 unsigned char sm_mod_tries;
243 unsigned char sm_dev_state;
244 unsigned short sm_state;
245 unsigned char sm_fault_retries;
246 unsigned char sm_phy_retries;
248 struct sfp_eeprom_id id;
249 unsigned int module_power_mW;
250 unsigned int module_t_start_up;
252 #if IS_ENABLED(CONFIG_HWMON)
253 struct sfp_diag diag;
254 struct delayed_work hwmon_probe;
255 unsigned int hwmon_tries;
256 struct device *hwmon_dev;
262 static bool sff_module_supported(const struct sfp_eeprom_id *id)
264 return id->base.phys_id == SFF8024_ID_SFF_8472 &&
265 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
268 static const struct sff_data sff_data = {
269 .gpios = SFP_F_LOS | SFP_F_TX_FAULT | SFP_F_TX_DISABLE,
270 .module_supported = sff_module_supported,
273 static bool sfp_module_supported(const struct sfp_eeprom_id *id)
275 return id->base.phys_id == SFF8024_ID_SFP &&
276 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
279 static const struct sff_data sfp_data = {
280 .gpios = SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT |
281 SFP_F_TX_DISABLE | SFP_F_RATE_SELECT,
282 .module_supported = sfp_module_supported,
285 static const struct of_device_id sfp_of_match[] = {
286 { .compatible = "sff,sff", .data = &sff_data, },
287 { .compatible = "sff,sfp", .data = &sfp_data, },
290 MODULE_DEVICE_TABLE(of, sfp_of_match);
292 static unsigned long poll_jiffies;
294 static unsigned int sfp_gpio_get_state(struct sfp *sfp)
296 unsigned int i, state, v;
298 for (i = state = 0; i < GPIO_MAX; i++) {
299 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
302 v = gpiod_get_value_cansleep(sfp->gpio[i]);
310 static unsigned int sff_gpio_get_state(struct sfp *sfp)
312 return sfp_gpio_get_state(sfp) | SFP_F_PRESENT;
315 static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
317 if (state & SFP_F_PRESENT) {
318 /* If the module is present, drive the signals */
319 if (sfp->gpio[GPIO_TX_DISABLE])
320 gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
321 state & SFP_F_TX_DISABLE);
322 if (state & SFP_F_RATE_SELECT)
323 gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT],
324 state & SFP_F_RATE_SELECT);
326 /* Otherwise, let them float to the pull-ups */
327 if (sfp->gpio[GPIO_TX_DISABLE])
328 gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
329 if (state & SFP_F_RATE_SELECT)
330 gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]);
334 static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
337 struct i2c_msg msgs[2];
338 u8 bus_addr = a2 ? 0x51 : 0x50;
342 msgs[0].addr = bus_addr;
345 msgs[0].buf = &dev_addr;
346 msgs[1].addr = bus_addr;
347 msgs[1].flags = I2C_M_RD;
356 msgs[1].len = this_len;
358 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
362 if (ret != ARRAY_SIZE(msgs))
365 msgs[1].buf += this_len;
366 dev_addr += this_len;
370 return msgs[1].buf - (u8 *)buf;
373 static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
376 struct i2c_msg msgs[1];
377 u8 bus_addr = a2 ? 0x51 : 0x50;
380 msgs[0].addr = bus_addr;
382 msgs[0].len = 1 + len;
383 msgs[0].buf = kmalloc(1 + len, GFP_KERNEL);
387 msgs[0].buf[0] = dev_addr;
388 memcpy(&msgs[0].buf[1], buf, len);
390 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
397 return ret == ARRAY_SIZE(msgs) ? len : 0;
400 static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
402 struct mii_bus *i2c_mii;
405 if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
409 sfp->read = sfp_i2c_read;
410 sfp->write = sfp_i2c_write;
412 i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
414 return PTR_ERR(i2c_mii);
416 i2c_mii->name = "SFP I2C Bus";
417 i2c_mii->phy_mask = ~0;
419 ret = mdiobus_register(i2c_mii);
421 mdiobus_free(i2c_mii);
425 sfp->i2c_mii = i2c_mii;
431 static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
433 return sfp->read(sfp, a2, addr, buf, len);
436 static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
438 return sfp->write(sfp, a2, addr, buf, len);
441 static unsigned int sfp_soft_get_state(struct sfp *sfp)
443 unsigned int state = 0;
446 if (sfp_read(sfp, true, SFP_STATUS, &status, sizeof(status)) ==
448 if (status & SFP_STATUS_RX_LOS)
450 if (status & SFP_STATUS_TX_FAULT)
451 state |= SFP_F_TX_FAULT;
454 return state & sfp->state_soft_mask;
457 static void sfp_soft_set_state(struct sfp *sfp, unsigned int state)
461 if (sfp_read(sfp, true, SFP_STATUS, &status, sizeof(status)) ==
463 if (state & SFP_F_TX_DISABLE)
464 status |= SFP_STATUS_TX_DISABLE_FORCE;
466 status &= ~SFP_STATUS_TX_DISABLE_FORCE;
468 sfp_write(sfp, true, SFP_STATUS, &status, sizeof(status));
472 static void sfp_soft_start_poll(struct sfp *sfp)
474 const struct sfp_eeprom_id *id = &sfp->id;
476 sfp->state_soft_mask = 0;
477 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_DISABLE &&
478 !sfp->gpio[GPIO_TX_DISABLE])
479 sfp->state_soft_mask |= SFP_F_TX_DISABLE;
480 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_FAULT &&
481 !sfp->gpio[GPIO_TX_FAULT])
482 sfp->state_soft_mask |= SFP_F_TX_FAULT;
483 if (id->ext.enhopts & SFP_ENHOPTS_SOFT_RX_LOS &&
484 !sfp->gpio[GPIO_LOS])
485 sfp->state_soft_mask |= SFP_F_LOS;
487 if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) &&
489 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
492 static void sfp_soft_stop_poll(struct sfp *sfp)
494 sfp->state_soft_mask = 0;
497 static unsigned int sfp_get_state(struct sfp *sfp)
499 unsigned int state = sfp->get_state(sfp);
501 if (state & SFP_F_PRESENT &&
502 sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT))
503 state |= sfp_soft_get_state(sfp);
508 static void sfp_set_state(struct sfp *sfp, unsigned int state)
510 sfp->set_state(sfp, state);
512 if (state & SFP_F_PRESENT &&
513 sfp->state_soft_mask & SFP_F_TX_DISABLE)
514 sfp_soft_set_state(sfp, state);
517 static unsigned int sfp_check(void *buf, size_t len)
521 for (p = buf, check = 0; len; p++, len--)
528 #if IS_ENABLED(CONFIG_HWMON)
529 static umode_t sfp_hwmon_is_visible(const void *data,
530 enum hwmon_sensor_types type,
531 u32 attr, int channel)
533 const struct sfp *sfp = data;
538 case hwmon_temp_min_alarm:
539 case hwmon_temp_max_alarm:
540 case hwmon_temp_lcrit_alarm:
541 case hwmon_temp_crit_alarm:
544 case hwmon_temp_lcrit:
545 case hwmon_temp_crit:
546 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
549 case hwmon_temp_input:
550 case hwmon_temp_label:
557 case hwmon_in_min_alarm:
558 case hwmon_in_max_alarm:
559 case hwmon_in_lcrit_alarm:
560 case hwmon_in_crit_alarm:
565 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
576 case hwmon_curr_min_alarm:
577 case hwmon_curr_max_alarm:
578 case hwmon_curr_lcrit_alarm:
579 case hwmon_curr_crit_alarm:
582 case hwmon_curr_lcrit:
583 case hwmon_curr_crit:
584 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
587 case hwmon_curr_input:
588 case hwmon_curr_label:
594 /* External calibration of receive power requires
595 * floating point arithmetic. Doing that in the kernel
596 * is not easy, so just skip it. If the module does
597 * not require external calibration, we can however
598 * show receiver power, since FP is then not needed.
600 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL &&
604 case hwmon_power_min_alarm:
605 case hwmon_power_max_alarm:
606 case hwmon_power_lcrit_alarm:
607 case hwmon_power_crit_alarm:
608 case hwmon_power_min:
609 case hwmon_power_max:
610 case hwmon_power_lcrit:
611 case hwmon_power_crit:
612 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
615 case hwmon_power_input:
616 case hwmon_power_label:
626 static int sfp_hwmon_read_sensor(struct sfp *sfp, int reg, long *value)
631 err = sfp_read(sfp, true, reg, &val, sizeof(val));
635 *value = be16_to_cpu(val);
640 static void sfp_hwmon_to_rx_power(long *value)
642 *value = DIV_ROUND_CLOSEST(*value, 10);
645 static void sfp_hwmon_calibrate(struct sfp *sfp, unsigned int slope, int offset,
648 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL)
649 *value = DIV_ROUND_CLOSEST(*value * slope, 256) + offset;
652 static void sfp_hwmon_calibrate_temp(struct sfp *sfp, long *value)
654 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_t_slope),
655 be16_to_cpu(sfp->diag.cal_t_offset), value);
657 if (*value >= 0x8000)
660 *value = DIV_ROUND_CLOSEST(*value * 1000, 256);
663 static void sfp_hwmon_calibrate_vcc(struct sfp *sfp, long *value)
665 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_v_slope),
666 be16_to_cpu(sfp->diag.cal_v_offset), value);
668 *value = DIV_ROUND_CLOSEST(*value, 10);
671 static void sfp_hwmon_calibrate_bias(struct sfp *sfp, long *value)
673 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txi_slope),
674 be16_to_cpu(sfp->diag.cal_txi_offset), value);
676 *value = DIV_ROUND_CLOSEST(*value, 500);
679 static void sfp_hwmon_calibrate_tx_power(struct sfp *sfp, long *value)
681 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txpwr_slope),
682 be16_to_cpu(sfp->diag.cal_txpwr_offset), value);
684 *value = DIV_ROUND_CLOSEST(*value, 10);
687 static int sfp_hwmon_read_temp(struct sfp *sfp, int reg, long *value)
691 err = sfp_hwmon_read_sensor(sfp, reg, value);
695 sfp_hwmon_calibrate_temp(sfp, value);
700 static int sfp_hwmon_read_vcc(struct sfp *sfp, int reg, long *value)
704 err = sfp_hwmon_read_sensor(sfp, reg, value);
708 sfp_hwmon_calibrate_vcc(sfp, value);
713 static int sfp_hwmon_read_bias(struct sfp *sfp, int reg, long *value)
717 err = sfp_hwmon_read_sensor(sfp, reg, value);
721 sfp_hwmon_calibrate_bias(sfp, value);
726 static int sfp_hwmon_read_tx_power(struct sfp *sfp, int reg, long *value)
730 err = sfp_hwmon_read_sensor(sfp, reg, value);
734 sfp_hwmon_calibrate_tx_power(sfp, value);
739 static int sfp_hwmon_read_rx_power(struct sfp *sfp, int reg, long *value)
743 err = sfp_hwmon_read_sensor(sfp, reg, value);
747 sfp_hwmon_to_rx_power(value);
752 static int sfp_hwmon_temp(struct sfp *sfp, u32 attr, long *value)
758 case hwmon_temp_input:
759 return sfp_hwmon_read_temp(sfp, SFP_TEMP, value);
761 case hwmon_temp_lcrit:
762 *value = be16_to_cpu(sfp->diag.temp_low_alarm);
763 sfp_hwmon_calibrate_temp(sfp, value);
767 *value = be16_to_cpu(sfp->diag.temp_low_warn);
768 sfp_hwmon_calibrate_temp(sfp, value);
771 *value = be16_to_cpu(sfp->diag.temp_high_warn);
772 sfp_hwmon_calibrate_temp(sfp, value);
775 case hwmon_temp_crit:
776 *value = be16_to_cpu(sfp->diag.temp_high_alarm);
777 sfp_hwmon_calibrate_temp(sfp, value);
780 case hwmon_temp_lcrit_alarm:
781 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
785 *value = !!(status & SFP_ALARM0_TEMP_LOW);
788 case hwmon_temp_min_alarm:
789 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
793 *value = !!(status & SFP_WARN0_TEMP_LOW);
796 case hwmon_temp_max_alarm:
797 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
801 *value = !!(status & SFP_WARN0_TEMP_HIGH);
804 case hwmon_temp_crit_alarm:
805 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
809 *value = !!(status & SFP_ALARM0_TEMP_HIGH);
818 static int sfp_hwmon_vcc(struct sfp *sfp, u32 attr, long *value)
825 return sfp_hwmon_read_vcc(sfp, SFP_VCC, value);
828 *value = be16_to_cpu(sfp->diag.volt_low_alarm);
829 sfp_hwmon_calibrate_vcc(sfp, value);
833 *value = be16_to_cpu(sfp->diag.volt_low_warn);
834 sfp_hwmon_calibrate_vcc(sfp, value);
838 *value = be16_to_cpu(sfp->diag.volt_high_warn);
839 sfp_hwmon_calibrate_vcc(sfp, value);
843 *value = be16_to_cpu(sfp->diag.volt_high_alarm);
844 sfp_hwmon_calibrate_vcc(sfp, value);
847 case hwmon_in_lcrit_alarm:
848 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
852 *value = !!(status & SFP_ALARM0_VCC_LOW);
855 case hwmon_in_min_alarm:
856 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
860 *value = !!(status & SFP_WARN0_VCC_LOW);
863 case hwmon_in_max_alarm:
864 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
868 *value = !!(status & SFP_WARN0_VCC_HIGH);
871 case hwmon_in_crit_alarm:
872 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
876 *value = !!(status & SFP_ALARM0_VCC_HIGH);
885 static int sfp_hwmon_bias(struct sfp *sfp, u32 attr, long *value)
891 case hwmon_curr_input:
892 return sfp_hwmon_read_bias(sfp, SFP_TX_BIAS, value);
894 case hwmon_curr_lcrit:
895 *value = be16_to_cpu(sfp->diag.bias_low_alarm);
896 sfp_hwmon_calibrate_bias(sfp, value);
900 *value = be16_to_cpu(sfp->diag.bias_low_warn);
901 sfp_hwmon_calibrate_bias(sfp, value);
905 *value = be16_to_cpu(sfp->diag.bias_high_warn);
906 sfp_hwmon_calibrate_bias(sfp, value);
909 case hwmon_curr_crit:
910 *value = be16_to_cpu(sfp->diag.bias_high_alarm);
911 sfp_hwmon_calibrate_bias(sfp, value);
914 case hwmon_curr_lcrit_alarm:
915 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
919 *value = !!(status & SFP_ALARM0_TX_BIAS_LOW);
922 case hwmon_curr_min_alarm:
923 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
927 *value = !!(status & SFP_WARN0_TX_BIAS_LOW);
930 case hwmon_curr_max_alarm:
931 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
935 *value = !!(status & SFP_WARN0_TX_BIAS_HIGH);
938 case hwmon_curr_crit_alarm:
939 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
943 *value = !!(status & SFP_ALARM0_TX_BIAS_HIGH);
952 static int sfp_hwmon_tx_power(struct sfp *sfp, u32 attr, long *value)
958 case hwmon_power_input:
959 return sfp_hwmon_read_tx_power(sfp, SFP_TX_POWER, value);
961 case hwmon_power_lcrit:
962 *value = be16_to_cpu(sfp->diag.txpwr_low_alarm);
963 sfp_hwmon_calibrate_tx_power(sfp, value);
966 case hwmon_power_min:
967 *value = be16_to_cpu(sfp->diag.txpwr_low_warn);
968 sfp_hwmon_calibrate_tx_power(sfp, value);
971 case hwmon_power_max:
972 *value = be16_to_cpu(sfp->diag.txpwr_high_warn);
973 sfp_hwmon_calibrate_tx_power(sfp, value);
976 case hwmon_power_crit:
977 *value = be16_to_cpu(sfp->diag.txpwr_high_alarm);
978 sfp_hwmon_calibrate_tx_power(sfp, value);
981 case hwmon_power_lcrit_alarm:
982 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
986 *value = !!(status & SFP_ALARM0_TXPWR_LOW);
989 case hwmon_power_min_alarm:
990 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
994 *value = !!(status & SFP_WARN0_TXPWR_LOW);
997 case hwmon_power_max_alarm:
998 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
1002 *value = !!(status & SFP_WARN0_TXPWR_HIGH);
1005 case hwmon_power_crit_alarm:
1006 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
1010 *value = !!(status & SFP_ALARM0_TXPWR_HIGH);
1019 static int sfp_hwmon_rx_power(struct sfp *sfp, u32 attr, long *value)
1025 case hwmon_power_input:
1026 return sfp_hwmon_read_rx_power(sfp, SFP_RX_POWER, value);
1028 case hwmon_power_lcrit:
1029 *value = be16_to_cpu(sfp->diag.rxpwr_low_alarm);
1030 sfp_hwmon_to_rx_power(value);
1033 case hwmon_power_min:
1034 *value = be16_to_cpu(sfp->diag.rxpwr_low_warn);
1035 sfp_hwmon_to_rx_power(value);
1038 case hwmon_power_max:
1039 *value = be16_to_cpu(sfp->diag.rxpwr_high_warn);
1040 sfp_hwmon_to_rx_power(value);
1043 case hwmon_power_crit:
1044 *value = be16_to_cpu(sfp->diag.rxpwr_high_alarm);
1045 sfp_hwmon_to_rx_power(value);
1048 case hwmon_power_lcrit_alarm:
1049 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
1053 *value = !!(status & SFP_ALARM1_RXPWR_LOW);
1056 case hwmon_power_min_alarm:
1057 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
1061 *value = !!(status & SFP_WARN1_RXPWR_LOW);
1064 case hwmon_power_max_alarm:
1065 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
1069 *value = !!(status & SFP_WARN1_RXPWR_HIGH);
1072 case hwmon_power_crit_alarm:
1073 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
1077 *value = !!(status & SFP_ALARM1_RXPWR_HIGH);
1086 static int sfp_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
1087 u32 attr, int channel, long *value)
1089 struct sfp *sfp = dev_get_drvdata(dev);
1093 return sfp_hwmon_temp(sfp, attr, value);
1095 return sfp_hwmon_vcc(sfp, attr, value);
1097 return sfp_hwmon_bias(sfp, attr, value);
1101 return sfp_hwmon_tx_power(sfp, attr, value);
1103 return sfp_hwmon_rx_power(sfp, attr, value);
1112 static const char *const sfp_hwmon_power_labels[] = {
1117 static int sfp_hwmon_read_string(struct device *dev,
1118 enum hwmon_sensor_types type,
1119 u32 attr, int channel, const char **str)
1124 case hwmon_curr_label:
1133 case hwmon_temp_label:
1134 *str = "temperature";
1142 case hwmon_in_label:
1151 case hwmon_power_label:
1152 *str = sfp_hwmon_power_labels[channel];
1165 static const struct hwmon_ops sfp_hwmon_ops = {
1166 .is_visible = sfp_hwmon_is_visible,
1167 .read = sfp_hwmon_read,
1168 .read_string = sfp_hwmon_read_string,
1171 static u32 sfp_hwmon_chip_config[] = {
1172 HWMON_C_REGISTER_TZ,
1176 static const struct hwmon_channel_info sfp_hwmon_chip = {
1178 .config = sfp_hwmon_chip_config,
1181 static u32 sfp_hwmon_temp_config[] = {
1183 HWMON_T_MAX | HWMON_T_MIN |
1184 HWMON_T_MAX_ALARM | HWMON_T_MIN_ALARM |
1185 HWMON_T_CRIT | HWMON_T_LCRIT |
1186 HWMON_T_CRIT_ALARM | HWMON_T_LCRIT_ALARM |
1191 static const struct hwmon_channel_info sfp_hwmon_temp_channel_info = {
1193 .config = sfp_hwmon_temp_config,
1196 static u32 sfp_hwmon_vcc_config[] = {
1198 HWMON_I_MAX | HWMON_I_MIN |
1199 HWMON_I_MAX_ALARM | HWMON_I_MIN_ALARM |
1200 HWMON_I_CRIT | HWMON_I_LCRIT |
1201 HWMON_I_CRIT_ALARM | HWMON_I_LCRIT_ALARM |
1206 static const struct hwmon_channel_info sfp_hwmon_vcc_channel_info = {
1208 .config = sfp_hwmon_vcc_config,
1211 static u32 sfp_hwmon_bias_config[] = {
1213 HWMON_C_MAX | HWMON_C_MIN |
1214 HWMON_C_MAX_ALARM | HWMON_C_MIN_ALARM |
1215 HWMON_C_CRIT | HWMON_C_LCRIT |
1216 HWMON_C_CRIT_ALARM | HWMON_C_LCRIT_ALARM |
1221 static const struct hwmon_channel_info sfp_hwmon_bias_channel_info = {
1223 .config = sfp_hwmon_bias_config,
1226 static u32 sfp_hwmon_power_config[] = {
1227 /* Transmit power */
1229 HWMON_P_MAX | HWMON_P_MIN |
1230 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
1231 HWMON_P_CRIT | HWMON_P_LCRIT |
1232 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM |
1236 HWMON_P_MAX | HWMON_P_MIN |
1237 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
1238 HWMON_P_CRIT | HWMON_P_LCRIT |
1239 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM |
1244 static const struct hwmon_channel_info sfp_hwmon_power_channel_info = {
1245 .type = hwmon_power,
1246 .config = sfp_hwmon_power_config,
1249 static const struct hwmon_channel_info *sfp_hwmon_info[] = {
1251 &sfp_hwmon_vcc_channel_info,
1252 &sfp_hwmon_temp_channel_info,
1253 &sfp_hwmon_bias_channel_info,
1254 &sfp_hwmon_power_channel_info,
1258 static const struct hwmon_chip_info sfp_hwmon_chip_info = {
1259 .ops = &sfp_hwmon_ops,
1260 .info = sfp_hwmon_info,
1263 static void sfp_hwmon_probe(struct work_struct *work)
1265 struct sfp *sfp = container_of(work, struct sfp, hwmon_probe.work);
1268 err = sfp_read(sfp, true, 0, &sfp->diag, sizeof(sfp->diag));
1270 if (sfp->hwmon_tries--) {
1271 mod_delayed_work(system_wq, &sfp->hwmon_probe,
1272 T_PROBE_RETRY_SLOW);
1274 dev_warn(sfp->dev, "hwmon probe failed: %d\n", err);
1279 sfp->hwmon_name = kstrdup(dev_name(sfp->dev), GFP_KERNEL);
1280 if (!sfp->hwmon_name) {
1281 dev_err(sfp->dev, "out of memory for hwmon name\n");
1285 for (i = 0; sfp->hwmon_name[i]; i++)
1286 if (hwmon_is_bad_char(sfp->hwmon_name[i]))
1287 sfp->hwmon_name[i] = '_';
1289 sfp->hwmon_dev = hwmon_device_register_with_info(sfp->dev,
1290 sfp->hwmon_name, sfp,
1291 &sfp_hwmon_chip_info,
1293 if (IS_ERR(sfp->hwmon_dev))
1294 dev_err(sfp->dev, "failed to register hwmon device: %ld\n",
1295 PTR_ERR(sfp->hwmon_dev));
1298 static int sfp_hwmon_insert(struct sfp *sfp)
1300 if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE)
1303 if (!(sfp->id.ext.diagmon & SFP_DIAGMON_DDM))
1306 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
1307 /* This driver in general does not support address
1312 mod_delayed_work(system_wq, &sfp->hwmon_probe, 1);
1313 sfp->hwmon_tries = R_PROBE_RETRY_SLOW;
1318 static void sfp_hwmon_remove(struct sfp *sfp)
1320 cancel_delayed_work_sync(&sfp->hwmon_probe);
1321 if (!IS_ERR_OR_NULL(sfp->hwmon_dev)) {
1322 hwmon_device_unregister(sfp->hwmon_dev);
1323 sfp->hwmon_dev = NULL;
1324 kfree(sfp->hwmon_name);
1328 static int sfp_hwmon_init(struct sfp *sfp)
1330 INIT_DELAYED_WORK(&sfp->hwmon_probe, sfp_hwmon_probe);
1335 static void sfp_hwmon_exit(struct sfp *sfp)
1337 cancel_delayed_work_sync(&sfp->hwmon_probe);
1340 static int sfp_hwmon_insert(struct sfp *sfp)
1345 static void sfp_hwmon_remove(struct sfp *sfp)
1349 static int sfp_hwmon_init(struct sfp *sfp)
1354 static void sfp_hwmon_exit(struct sfp *sfp)
1360 static void sfp_module_tx_disable(struct sfp *sfp)
1362 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1363 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
1364 sfp->state |= SFP_F_TX_DISABLE;
1365 sfp_set_state(sfp, sfp->state);
1368 static void sfp_module_tx_enable(struct sfp *sfp)
1370 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1371 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
1372 sfp->state &= ~SFP_F_TX_DISABLE;
1373 sfp_set_state(sfp, sfp->state);
1376 static void sfp_module_tx_fault_reset(struct sfp *sfp)
1378 unsigned int state = sfp->state;
1380 if (state & SFP_F_TX_DISABLE)
1383 sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
1387 sfp_set_state(sfp, state);
1390 /* SFP state machine */
1391 static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
1394 mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
1397 cancel_delayed_work(&sfp->timeout);
1400 static void sfp_sm_next(struct sfp *sfp, unsigned int state,
1401 unsigned int timeout)
1403 sfp->sm_state = state;
1404 sfp_sm_set_timer(sfp, timeout);
1407 static void sfp_sm_mod_next(struct sfp *sfp, unsigned int state,
1408 unsigned int timeout)
1410 sfp->sm_mod_state = state;
1411 sfp_sm_set_timer(sfp, timeout);
1414 static void sfp_sm_phy_detach(struct sfp *sfp)
1416 sfp_remove_phy(sfp->sfp_bus);
1417 phy_device_remove(sfp->mod_phy);
1418 phy_device_free(sfp->mod_phy);
1419 sfp->mod_phy = NULL;
1422 static int sfp_sm_probe_phy(struct sfp *sfp, bool is_c45)
1424 struct phy_device *phy;
1427 phy = get_phy_device(sfp->i2c_mii, SFP_PHY_ADDR, is_c45);
1428 if (phy == ERR_PTR(-ENODEV))
1429 return PTR_ERR(phy);
1431 dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
1432 return PTR_ERR(phy);
1435 err = phy_device_register(phy);
1437 phy_device_free(phy);
1438 dev_err(sfp->dev, "phy_device_register failed: %d\n", err);
1442 err = sfp_add_phy(sfp->sfp_bus, phy);
1444 phy_device_remove(phy);
1445 phy_device_free(phy);
1446 dev_err(sfp->dev, "sfp_add_phy failed: %d\n", err);
1455 static void sfp_sm_link_up(struct sfp *sfp)
1457 sfp_link_up(sfp->sfp_bus);
1458 sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
1461 static void sfp_sm_link_down(struct sfp *sfp)
1463 sfp_link_down(sfp->sfp_bus);
1466 static void sfp_sm_link_check_los(struct sfp *sfp)
1468 unsigned int los = sfp->state & SFP_F_LOS;
1470 /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL
1471 * are set, we assume that no LOS signal is available.
1473 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED))
1475 else if (!(sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL)))
1479 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
1481 sfp_sm_link_up(sfp);
1484 static bool sfp_los_event_active(struct sfp *sfp, unsigned int event)
1486 return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
1487 event == SFP_E_LOS_LOW) ||
1488 (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
1489 event == SFP_E_LOS_HIGH);
1492 static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event)
1494 return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
1495 event == SFP_E_LOS_HIGH) ||
1496 (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
1497 event == SFP_E_LOS_LOW);
1500 static void sfp_sm_fault(struct sfp *sfp, unsigned int next_state, bool warn)
1502 if (sfp->sm_fault_retries && !--sfp->sm_fault_retries) {
1504 "module persistently indicates fault, disabling\n");
1505 sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
1508 dev_err(sfp->dev, "module transmit fault indicated\n");
1510 sfp_sm_next(sfp, next_state, T_FAULT_RECOVER);
1514 /* Probe a SFP for a PHY device if the module supports copper - the PHY
1515 * normally sits at I2C bus address 0x56, and may either be a clause 22
1518 * Clause 22 copper SFP modules normally operate in Cisco SGMII mode with
1519 * negotiation enabled, but some may be in 1000base-X - which is for the
1520 * PHY driver to determine.
1522 * Clause 45 copper SFP+ modules (10G) appear to switch their interface
1523 * mode according to the negotiated line speed.
1525 static int sfp_sm_probe_for_phy(struct sfp *sfp)
1529 switch (sfp->id.base.extended_cc) {
1530 case SFF8024_ECC_10GBASE_T_SFI:
1531 case SFF8024_ECC_10GBASE_T_SR:
1532 case SFF8024_ECC_5GBASE_T:
1533 case SFF8024_ECC_2_5GBASE_T:
1534 err = sfp_sm_probe_phy(sfp, true);
1538 if (sfp->id.base.e1000_base_t)
1539 err = sfp_sm_probe_phy(sfp, false);
1545 static int sfp_module_parse_power(struct sfp *sfp)
1547 u32 power_mW = 1000;
1549 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL))
1551 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL))
1554 if (power_mW > sfp->max_power_mW) {
1555 /* Module power specification exceeds the allowed maximum. */
1556 if (sfp->id.ext.sff8472_compliance ==
1557 SFP_SFF8472_COMPLIANCE_NONE &&
1558 !(sfp->id.ext.diagmon & SFP_DIAGMON_DDM)) {
1559 /* The module appears not to implement bus address
1560 * 0xa2, so assume that the module powers up in the
1564 "Host does not support %u.%uW modules\n",
1565 power_mW / 1000, (power_mW / 100) % 10);
1569 "Host does not support %u.%uW modules, module left in power mode 1\n",
1570 power_mW / 1000, (power_mW / 100) % 10);
1575 /* If the module requires a higher power mode, but also requires
1576 * an address change sequence, warn the user that the module may
1577 * not be functional.
1579 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE && power_mW > 1000) {
1581 "Address Change Sequence not supported but module requires %u.%uW, module may not be functional\n",
1582 power_mW / 1000, (power_mW / 100) % 10);
1586 sfp->module_power_mW = power_mW;
1591 static int sfp_sm_mod_hpower(struct sfp *sfp, bool enable)
1596 err = sfp_read(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
1597 if (err != sizeof(val)) {
1598 dev_err(sfp->dev, "Failed to read EEPROM: %d\n", err);
1602 /* DM7052 reports as a high power module, responds to reads (with
1603 * all bytes 0xff) at 0x51 but does not accept writes. In any case,
1604 * if the bit is already set, we're already in high power mode.
1606 if (!!(val & BIT(0)) == enable)
1614 err = sfp_write(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
1615 if (err != sizeof(val)) {
1616 dev_err(sfp->dev, "Failed to write EEPROM: %d\n", err);
1621 dev_info(sfp->dev, "Module switched to %u.%uW power level\n",
1622 sfp->module_power_mW / 1000,
1623 (sfp->module_power_mW / 100) % 10);
1628 static int sfp_sm_mod_probe(struct sfp *sfp, bool report)
1630 /* SFP module inserted - read I2C data */
1631 struct sfp_eeprom_id id;
1636 ret = sfp_read(sfp, false, 0, &id, sizeof(id));
1639 dev_err(sfp->dev, "failed to read EEPROM: %d\n", ret);
1643 if (ret != sizeof(id)) {
1644 dev_err(sfp->dev, "EEPROM short read: %d\n", ret);
1648 /* Cotsworks do not seem to update the checksums when they
1649 * do the final programming with the final module part number,
1650 * serial number and date code.
1652 cotsworks = !memcmp(id.base.vendor_name, "COTSWORKS ", 16);
1654 /* Validate the checksum over the base structure */
1655 check = sfp_check(&id.base, sizeof(id.base) - 1);
1656 if (check != id.base.cc_base) {
1659 "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n",
1660 check, id.base.cc_base);
1663 "EEPROM base structure checksum failure: 0x%02x != 0x%02x\n",
1664 check, id.base.cc_base);
1665 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
1666 16, 1, &id, sizeof(id), true);
1671 check = sfp_check(&id.ext, sizeof(id.ext) - 1);
1672 if (check != id.ext.cc_ext) {
1675 "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n",
1676 check, id.ext.cc_ext);
1679 "EEPROM extended structure checksum failure: 0x%02x != 0x%02x\n",
1680 check, id.ext.cc_ext);
1681 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
1682 16, 1, &id, sizeof(id), true);
1683 memset(&id.ext, 0, sizeof(id.ext));
1689 dev_info(sfp->dev, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n",
1690 (int)sizeof(id.base.vendor_name), id.base.vendor_name,
1691 (int)sizeof(id.base.vendor_pn), id.base.vendor_pn,
1692 (int)sizeof(id.base.vendor_rev), id.base.vendor_rev,
1693 (int)sizeof(id.ext.vendor_sn), id.ext.vendor_sn,
1694 (int)sizeof(id.ext.datecode), id.ext.datecode);
1696 /* Check whether we support this module */
1697 if (!sfp->type->module_supported(&id)) {
1699 "module is not supported - phys id 0x%02x 0x%02x\n",
1700 sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
1704 /* If the module requires address swap mode, warn about it */
1705 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
1707 "module address swap to access page 0xA2 is not supported.\n");
1709 /* Parse the module power requirement */
1710 ret = sfp_module_parse_power(sfp);
1714 if (!memcmp(id.base.vendor_name, "ALCATELLUCENT ", 16) &&
1715 !memcmp(id.base.vendor_pn, "3FE46541AA ", 16))
1716 sfp->module_t_start_up = T_START_UP_BAD_GPON;
1718 sfp->module_t_start_up = T_START_UP;
1723 static void sfp_sm_mod_remove(struct sfp *sfp)
1725 if (sfp->sm_mod_state > SFP_MOD_WAITDEV)
1726 sfp_module_remove(sfp->sfp_bus);
1728 sfp_hwmon_remove(sfp);
1730 memset(&sfp->id, 0, sizeof(sfp->id));
1731 sfp->module_power_mW = 0;
1733 dev_info(sfp->dev, "module removed\n");
1736 /* This state machine tracks the upstream's state */
1737 static void sfp_sm_device(struct sfp *sfp, unsigned int event)
1739 switch (sfp->sm_dev_state) {
1741 if (event == SFP_E_DEV_ATTACH)
1742 sfp->sm_dev_state = SFP_DEV_DOWN;
1746 if (event == SFP_E_DEV_DETACH)
1747 sfp->sm_dev_state = SFP_DEV_DETACHED;
1748 else if (event == SFP_E_DEV_UP)
1749 sfp->sm_dev_state = SFP_DEV_UP;
1753 if (event == SFP_E_DEV_DETACH)
1754 sfp->sm_dev_state = SFP_DEV_DETACHED;
1755 else if (event == SFP_E_DEV_DOWN)
1756 sfp->sm_dev_state = SFP_DEV_DOWN;
1761 /* This state machine tracks the insert/remove state of the module, probes
1762 * the on-board EEPROM, and sets up the power level.
1764 static void sfp_sm_module(struct sfp *sfp, unsigned int event)
1768 /* Handle remove event globally, it resets this state machine */
1769 if (event == SFP_E_REMOVE) {
1770 if (sfp->sm_mod_state > SFP_MOD_PROBE)
1771 sfp_sm_mod_remove(sfp);
1772 sfp_sm_mod_next(sfp, SFP_MOD_EMPTY, 0);
1776 /* Handle device detach globally */
1777 if (sfp->sm_dev_state < SFP_DEV_DOWN &&
1778 sfp->sm_mod_state > SFP_MOD_WAITDEV) {
1779 if (sfp->module_power_mW > 1000 &&
1780 sfp->sm_mod_state > SFP_MOD_HPOWER)
1781 sfp_sm_mod_hpower(sfp, false);
1782 sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0);
1786 switch (sfp->sm_mod_state) {
1788 if (event == SFP_E_INSERT) {
1789 sfp_sm_mod_next(sfp, SFP_MOD_PROBE, T_SERIAL);
1790 sfp->sm_mod_tries_init = R_PROBE_RETRY_INIT;
1791 sfp->sm_mod_tries = R_PROBE_RETRY_SLOW;
1796 /* Wait for T_PROBE_INIT to time out */
1797 if (event != SFP_E_TIMEOUT)
1800 err = sfp_sm_mod_probe(sfp, sfp->sm_mod_tries == 1);
1801 if (err == -EAGAIN) {
1802 if (sfp->sm_mod_tries_init &&
1803 --sfp->sm_mod_tries_init) {
1804 sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT);
1806 } else if (sfp->sm_mod_tries && --sfp->sm_mod_tries) {
1807 if (sfp->sm_mod_tries == R_PROBE_RETRY_SLOW - 1)
1809 "please wait, module slow to respond\n");
1810 sfp_sm_set_timer(sfp, T_PROBE_RETRY_SLOW);
1815 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
1819 err = sfp_hwmon_insert(sfp);
1821 dev_warn(sfp->dev, "hwmon probe failed: %d\n", err);
1823 sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0);
1825 case SFP_MOD_WAITDEV:
1826 /* Ensure that the device is attached before proceeding */
1827 if (sfp->sm_dev_state < SFP_DEV_DOWN)
1830 /* Report the module insertion to the upstream device */
1831 err = sfp_module_insert(sfp->sfp_bus, &sfp->id);
1833 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
1837 /* If this is a power level 1 module, we are done */
1838 if (sfp->module_power_mW <= 1000)
1841 sfp_sm_mod_next(sfp, SFP_MOD_HPOWER, 0);
1843 case SFP_MOD_HPOWER:
1844 /* Enable high power mode */
1845 err = sfp_sm_mod_hpower(sfp, true);
1847 if (err != -EAGAIN) {
1848 sfp_module_remove(sfp->sfp_bus);
1849 sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
1851 sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT);
1856 sfp_sm_mod_next(sfp, SFP_MOD_WAITPWR, T_HPOWER_LEVEL);
1859 case SFP_MOD_WAITPWR:
1860 /* Wait for T_HPOWER_LEVEL to time out */
1861 if (event != SFP_E_TIMEOUT)
1865 sfp_sm_mod_next(sfp, SFP_MOD_PRESENT, 0);
1868 case SFP_MOD_PRESENT:
1874 static void sfp_sm_main(struct sfp *sfp, unsigned int event)
1876 unsigned long timeout;
1879 /* Some events are global */
1880 if (sfp->sm_state != SFP_S_DOWN &&
1881 (sfp->sm_mod_state != SFP_MOD_PRESENT ||
1882 sfp->sm_dev_state != SFP_DEV_UP)) {
1883 if (sfp->sm_state == SFP_S_LINK_UP &&
1884 sfp->sm_dev_state == SFP_DEV_UP)
1885 sfp_sm_link_down(sfp);
1886 if (sfp->sm_state > SFP_S_INIT)
1887 sfp_module_stop(sfp->sfp_bus);
1889 sfp_sm_phy_detach(sfp);
1890 sfp_module_tx_disable(sfp);
1891 sfp_soft_stop_poll(sfp);
1892 sfp_sm_next(sfp, SFP_S_DOWN, 0);
1896 /* The main state machine */
1897 switch (sfp->sm_state) {
1899 if (sfp->sm_mod_state != SFP_MOD_PRESENT ||
1900 sfp->sm_dev_state != SFP_DEV_UP)
1903 if (!(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE))
1904 sfp_soft_start_poll(sfp);
1906 sfp_module_tx_enable(sfp);
1908 /* Initialise the fault clearance retries */
1909 sfp->sm_fault_retries = N_FAULT_INIT;
1911 /* We need to check the TX_FAULT state, which is not defined
1912 * while TX_DISABLE is asserted. The earliest we want to do
1913 * anything (such as probe for a PHY) is 50ms.
1915 sfp_sm_next(sfp, SFP_S_WAIT, T_WAIT);
1919 if (event != SFP_E_TIMEOUT)
1922 if (sfp->state & SFP_F_TX_FAULT) {
1923 /* Wait up to t_init (SFF-8472) or t_start_up (SFF-8431)
1924 * from the TX_DISABLE deassertion for the module to
1925 * initialise, which is indicated by TX_FAULT
1928 timeout = sfp->module_t_start_up;
1929 if (timeout > T_WAIT)
1934 sfp_sm_next(sfp, SFP_S_INIT, timeout);
1936 /* TX_FAULT is not asserted, assume the module has
1937 * finished initialising.
1944 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
1945 /* TX_FAULT is still asserted after t_init or
1946 * or t_start_up, so assume there is a fault.
1948 sfp_sm_fault(sfp, SFP_S_INIT_TX_FAULT,
1949 sfp->sm_fault_retries == N_FAULT_INIT);
1950 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
1952 sfp->sm_phy_retries = R_PHY_RETRY;
1957 case SFP_S_INIT_PHY:
1958 if (event != SFP_E_TIMEOUT)
1961 /* TX_FAULT deasserted or we timed out with TX_FAULT
1962 * clear. Probe for the PHY and check the LOS state.
1964 ret = sfp_sm_probe_for_phy(sfp);
1965 if (ret == -ENODEV) {
1966 if (--sfp->sm_phy_retries) {
1967 sfp_sm_next(sfp, SFP_S_INIT_PHY, T_PHY_RETRY);
1970 dev_info(sfp->dev, "no PHY detected\n");
1973 sfp_sm_next(sfp, SFP_S_FAIL, 0);
1976 if (sfp_module_start(sfp->sfp_bus)) {
1977 sfp_sm_next(sfp, SFP_S_FAIL, 0);
1980 sfp_sm_link_check_los(sfp);
1982 /* Reset the fault retry count */
1983 sfp->sm_fault_retries = N_FAULT;
1986 case SFP_S_INIT_TX_FAULT:
1987 if (event == SFP_E_TIMEOUT) {
1988 sfp_module_tx_fault_reset(sfp);
1989 sfp_sm_next(sfp, SFP_S_INIT, sfp->module_t_start_up);
1993 case SFP_S_WAIT_LOS:
1994 if (event == SFP_E_TX_FAULT)
1995 sfp_sm_fault(sfp, SFP_S_TX_FAULT, true);
1996 else if (sfp_los_event_inactive(sfp, event))
1997 sfp_sm_link_up(sfp);
2001 if (event == SFP_E_TX_FAULT) {
2002 sfp_sm_link_down(sfp);
2003 sfp_sm_fault(sfp, SFP_S_TX_FAULT, true);
2004 } else if (sfp_los_event_active(sfp, event)) {
2005 sfp_sm_link_down(sfp);
2006 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
2010 case SFP_S_TX_FAULT:
2011 if (event == SFP_E_TIMEOUT) {
2012 sfp_module_tx_fault_reset(sfp);
2013 sfp_sm_next(sfp, SFP_S_REINIT, sfp->module_t_start_up);
2018 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
2019 sfp_sm_fault(sfp, SFP_S_TX_FAULT, false);
2020 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
2021 dev_info(sfp->dev, "module transmit fault recovered\n");
2022 sfp_sm_link_check_los(sfp);
2026 case SFP_S_TX_DISABLE:
2031 static void sfp_sm_event(struct sfp *sfp, unsigned int event)
2033 mutex_lock(&sfp->sm_mutex);
2035 dev_dbg(sfp->dev, "SM: enter %s:%s:%s event %s\n",
2036 mod_state_to_str(sfp->sm_mod_state),
2037 dev_state_to_str(sfp->sm_dev_state),
2038 sm_state_to_str(sfp->sm_state),
2039 event_to_str(event));
2041 sfp_sm_device(sfp, event);
2042 sfp_sm_module(sfp, event);
2043 sfp_sm_main(sfp, event);
2045 dev_dbg(sfp->dev, "SM: exit %s:%s:%s\n",
2046 mod_state_to_str(sfp->sm_mod_state),
2047 dev_state_to_str(sfp->sm_dev_state),
2048 sm_state_to_str(sfp->sm_state));
2050 mutex_unlock(&sfp->sm_mutex);
2053 static void sfp_attach(struct sfp *sfp)
2055 sfp_sm_event(sfp, SFP_E_DEV_ATTACH);
2058 static void sfp_detach(struct sfp *sfp)
2060 sfp_sm_event(sfp, SFP_E_DEV_DETACH);
2063 static void sfp_start(struct sfp *sfp)
2065 sfp_sm_event(sfp, SFP_E_DEV_UP);
2068 static void sfp_stop(struct sfp *sfp)
2070 sfp_sm_event(sfp, SFP_E_DEV_DOWN);
2073 static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo)
2075 /* locking... and check module is present */
2077 if (sfp->id.ext.sff8472_compliance &&
2078 !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) {
2079 modinfo->type = ETH_MODULE_SFF_8472;
2080 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
2082 modinfo->type = ETH_MODULE_SFF_8079;
2083 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
2088 static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee,
2091 unsigned int first, last, len;
2098 last = ee->offset + ee->len;
2099 if (first < ETH_MODULE_SFF_8079_LEN) {
2100 len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN);
2103 ret = sfp_read(sfp, false, first, data, len);
2110 if (first < ETH_MODULE_SFF_8472_LEN && last > ETH_MODULE_SFF_8079_LEN) {
2111 len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN);
2113 first -= ETH_MODULE_SFF_8079_LEN;
2115 ret = sfp_read(sfp, true, first, data, len);
2122 static const struct sfp_socket_ops sfp_module_ops = {
2123 .attach = sfp_attach,
2124 .detach = sfp_detach,
2127 .module_info = sfp_module_info,
2128 .module_eeprom = sfp_module_eeprom,
2131 static void sfp_timeout(struct work_struct *work)
2133 struct sfp *sfp = container_of(work, struct sfp, timeout.work);
2136 sfp_sm_event(sfp, SFP_E_TIMEOUT);
2140 static void sfp_check_state(struct sfp *sfp)
2142 unsigned int state, i, changed;
2144 mutex_lock(&sfp->st_mutex);
2145 state = sfp_get_state(sfp);
2146 changed = state ^ sfp->state;
2147 changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
2149 for (i = 0; i < GPIO_MAX; i++)
2150 if (changed & BIT(i))
2151 dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i],
2152 !!(sfp->state & BIT(i)), !!(state & BIT(i)));
2154 state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT);
2158 if (changed & SFP_F_PRESENT)
2159 sfp_sm_event(sfp, state & SFP_F_PRESENT ?
2160 SFP_E_INSERT : SFP_E_REMOVE);
2162 if (changed & SFP_F_TX_FAULT)
2163 sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
2164 SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
2166 if (changed & SFP_F_LOS)
2167 sfp_sm_event(sfp, state & SFP_F_LOS ?
2168 SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
2170 mutex_unlock(&sfp->st_mutex);
2173 static irqreturn_t sfp_irq(int irq, void *data)
2175 struct sfp *sfp = data;
2177 sfp_check_state(sfp);
2182 static void sfp_poll(struct work_struct *work)
2184 struct sfp *sfp = container_of(work, struct sfp, poll.work);
2186 sfp_check_state(sfp);
2188 if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) ||
2190 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
2193 static struct sfp *sfp_alloc(struct device *dev)
2197 sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
2199 return ERR_PTR(-ENOMEM);
2203 mutex_init(&sfp->sm_mutex);
2204 mutex_init(&sfp->st_mutex);
2205 INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
2206 INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
2208 sfp_hwmon_init(sfp);
2213 static void sfp_cleanup(void *data)
2215 struct sfp *sfp = data;
2217 sfp_hwmon_exit(sfp);
2219 cancel_delayed_work_sync(&sfp->poll);
2220 cancel_delayed_work_sync(&sfp->timeout);
2222 mdiobus_unregister(sfp->i2c_mii);
2223 mdiobus_free(sfp->i2c_mii);
2226 i2c_put_adapter(sfp->i2c);
2230 static int sfp_probe(struct platform_device *pdev)
2232 const struct sff_data *sff;
2233 struct i2c_adapter *i2c;
2237 sfp = sfp_alloc(&pdev->dev);
2239 return PTR_ERR(sfp);
2241 platform_set_drvdata(pdev, sfp);
2243 err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
2247 sff = sfp->type = &sfp_data;
2249 if (pdev->dev.of_node) {
2250 struct device_node *node = pdev->dev.of_node;
2251 const struct of_device_id *id;
2252 struct device_node *np;
2254 id = of_match_node(sfp_of_match, node);
2258 sff = sfp->type = id->data;
2260 np = of_parse_phandle(node, "i2c-bus", 0);
2262 dev_err(sfp->dev, "missing 'i2c-bus' property\n");
2266 i2c = of_find_i2c_adapter_by_node(np);
2268 } else if (has_acpi_companion(&pdev->dev)) {
2269 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
2270 struct fwnode_handle *fw = acpi_fwnode_handle(adev);
2271 struct fwnode_reference_args args;
2272 struct acpi_handle *acpi_handle;
2275 ret = acpi_node_get_property_reference(fw, "i2c-bus", 0, &args);
2276 if (ret || !is_acpi_device_node(args.fwnode)) {
2277 dev_err(&pdev->dev, "missing 'i2c-bus' property\n");
2281 acpi_handle = ACPI_HANDLE_FWNODE(args.fwnode);
2282 i2c = i2c_acpi_find_adapter_by_handle(acpi_handle);
2288 return -EPROBE_DEFER;
2290 err = sfp_i2c_configure(sfp, i2c);
2292 i2c_put_adapter(i2c);
2296 for (i = 0; i < GPIO_MAX; i++)
2297 if (sff->gpios & BIT(i)) {
2298 sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
2299 gpio_of_names[i], gpio_flags[i]);
2300 if (IS_ERR(sfp->gpio[i]))
2301 return PTR_ERR(sfp->gpio[i]);
2304 sfp->get_state = sfp_gpio_get_state;
2305 sfp->set_state = sfp_gpio_set_state;
2307 /* Modules that have no detect signal are always present */
2308 if (!(sfp->gpio[GPIO_MODDEF0]))
2309 sfp->get_state = sff_gpio_get_state;
2311 device_property_read_u32(&pdev->dev, "maximum-power-milliwatt",
2312 &sfp->max_power_mW);
2313 if (!sfp->max_power_mW)
2314 sfp->max_power_mW = 1000;
2316 dev_info(sfp->dev, "Host maximum power %u.%uW\n",
2317 sfp->max_power_mW / 1000, (sfp->max_power_mW / 100) % 10);
2319 /* Get the initial state, and always signal TX disable,
2320 * since the network interface will not be up.
2322 sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE;
2324 if (sfp->gpio[GPIO_RATE_SELECT] &&
2325 gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT]))
2326 sfp->state |= SFP_F_RATE_SELECT;
2327 sfp_set_state(sfp, sfp->state);
2328 sfp_module_tx_disable(sfp);
2329 if (sfp->state & SFP_F_PRESENT) {
2331 sfp_sm_event(sfp, SFP_E_INSERT);
2335 for (i = 0; i < GPIO_MAX; i++) {
2336 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
2339 sfp->gpio_irq[i] = gpiod_to_irq(sfp->gpio[i]);
2340 if (!sfp->gpio_irq[i]) {
2341 sfp->need_poll = true;
2345 err = devm_request_threaded_irq(sfp->dev, sfp->gpio_irq[i],
2348 IRQF_TRIGGER_RISING |
2349 IRQF_TRIGGER_FALLING,
2350 dev_name(sfp->dev), sfp);
2352 sfp->gpio_irq[i] = 0;
2353 sfp->need_poll = true;
2358 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
2360 /* We could have an issue in cases no Tx disable pin is available or
2361 * wired as modules using a laser as their light source will continue to
2362 * be active when the fiber is removed. This could be a safety issue and
2363 * we should at least warn the user about that.
2365 if (!sfp->gpio[GPIO_TX_DISABLE])
2367 "No tx_disable pin: SFP modules will always be emitting.\n");
2369 sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops);
2376 static int sfp_remove(struct platform_device *pdev)
2378 struct sfp *sfp = platform_get_drvdata(pdev);
2380 sfp_unregister_socket(sfp->sfp_bus);
2383 sfp_sm_event(sfp, SFP_E_REMOVE);
2389 static void sfp_shutdown(struct platform_device *pdev)
2391 struct sfp *sfp = platform_get_drvdata(pdev);
2394 for (i = 0; i < GPIO_MAX; i++) {
2395 if (!sfp->gpio_irq[i])
2398 devm_free_irq(sfp->dev, sfp->gpio_irq[i], sfp);
2401 cancel_delayed_work_sync(&sfp->poll);
2402 cancel_delayed_work_sync(&sfp->timeout);
2405 static struct platform_driver sfp_driver = {
2407 .remove = sfp_remove,
2408 .shutdown = sfp_shutdown,
2411 .of_match_table = sfp_of_match,
2415 static int sfp_init(void)
2417 poll_jiffies = msecs_to_jiffies(100);
2419 return platform_driver_register(&sfp_driver);
2421 module_init(sfp_init);
2423 static void sfp_exit(void)
2425 platform_driver_unregister(&sfp_driver);
2427 module_exit(sfp_exit);
2429 MODULE_ALIAS("platform:sfp");
2430 MODULE_AUTHOR("Russell King");
2431 MODULE_LICENSE("GPL v2");