1 #include <linux/ctype.h>
2 #include <linux/delay.h>
3 #include <linux/gpio/consumer.h>
4 #include <linux/hwmon.h>
6 #include <linux/interrupt.h>
7 #include <linux/jiffies.h>
8 #include <linux/module.h>
9 #include <linux/mutex.h>
11 #include <linux/phy.h>
12 #include <linux/platform_device.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/slab.h>
15 #include <linux/workqueue.h>
29 SFP_F_PRESENT
= BIT(GPIO_MODDEF0
),
30 SFP_F_LOS
= BIT(GPIO_LOS
),
31 SFP_F_TX_FAULT
= BIT(GPIO_TX_FAULT
),
32 SFP_F_TX_DISABLE
= BIT(GPIO_TX_DISABLE
),
33 SFP_F_RATE_SELECT
= BIT(GPIO_RATE_SELECT
),
63 static const char * const mod_state_strings
[] = {
64 [SFP_MOD_EMPTY
] = "empty",
65 [SFP_MOD_PROBE
] = "probe",
66 [SFP_MOD_HPOWER
] = "hpower",
67 [SFP_MOD_PRESENT
] = "present",
68 [SFP_MOD_ERROR
] = "error",
71 static const char *mod_state_to_str(unsigned short mod_state
)
73 if (mod_state
>= ARRAY_SIZE(mod_state_strings
))
74 return "Unknown module state";
75 return mod_state_strings
[mod_state
];
78 static const char * const dev_state_strings
[] = {
79 [SFP_DEV_DOWN
] = "down",
83 static const char *dev_state_to_str(unsigned short dev_state
)
85 if (dev_state
>= ARRAY_SIZE(dev_state_strings
))
86 return "Unknown device state";
87 return dev_state_strings
[dev_state
];
90 static const char * const event_strings
[] = {
91 [SFP_E_INSERT
] = "insert",
92 [SFP_E_REMOVE
] = "remove",
93 [SFP_E_DEV_DOWN
] = "dev_down",
94 [SFP_E_DEV_UP
] = "dev_up",
95 [SFP_E_TX_FAULT
] = "tx_fault",
96 [SFP_E_TX_CLEAR
] = "tx_clear",
97 [SFP_E_LOS_HIGH
] = "los_high",
98 [SFP_E_LOS_LOW
] = "los_low",
99 [SFP_E_TIMEOUT
] = "timeout",
102 static const char *event_to_str(unsigned short event
)
104 if (event
>= ARRAY_SIZE(event_strings
))
105 return "Unknown event";
106 return event_strings
[event
];
109 static const char * const sm_state_strings
[] = {
110 [SFP_S_DOWN
] = "down",
111 [SFP_S_INIT
] = "init",
112 [SFP_S_WAIT_LOS
] = "wait_los",
113 [SFP_S_LINK_UP
] = "link_up",
114 [SFP_S_TX_FAULT
] = "tx_fault",
115 [SFP_S_REINIT
] = "reinit",
116 [SFP_S_TX_DISABLE
] = "rx_disable",
119 static const char *sm_state_to_str(unsigned short sm_state
)
121 if (sm_state
>= ARRAY_SIZE(sm_state_strings
))
122 return "Unknown state";
123 return sm_state_strings
[sm_state
];
126 static const char *gpio_of_names
[] = {
134 static const enum gpiod_flags gpio_flags
[] = {
142 #define T_INIT_JIFFIES msecs_to_jiffies(300)
143 #define T_RESET_US 10
144 #define T_FAULT_RECOVER msecs_to_jiffies(1000)
146 /* SFP module presence detection is poor: the three MOD DEF signals are
147 * the same length on the PCB, which means it's possible for MOD DEF 0 to
148 * connect before the I2C bus on MOD DEF 1/2.
150 * The SFP MSA specifies 300ms as t_init (the time taken for TX_FAULT to
151 * be deasserted) but makes no mention of the earliest time before we can
152 * access the I2C EEPROM. However, Avago modules require 300ms.
154 #define T_PROBE_INIT msecs_to_jiffies(300)
155 #define T_HPOWER_LEVEL msecs_to_jiffies(300)
156 #define T_PROBE_RETRY msecs_to_jiffies(100)
158 /* SFP modules appear to always have their PHY configured for bus address
159 * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
161 #define SFP_PHY_ADDR 22
163 /* Give this long for the PHY to reset. */
164 #define T_PHY_RESET_MS 50
166 static DEFINE_MUTEX(sfp_mutex
);
170 bool (*module_supported
)(const struct sfp_eeprom_id
*id
);
175 struct i2c_adapter
*i2c
;
176 struct mii_bus
*i2c_mii
;
177 struct sfp_bus
*sfp_bus
;
178 struct phy_device
*mod_phy
;
179 const struct sff_data
*type
;
182 unsigned int (*get_state
)(struct sfp
*);
183 void (*set_state
)(struct sfp
*, unsigned int);
184 int (*read
)(struct sfp
*, bool, u8
, void *, size_t);
185 int (*write
)(struct sfp
*, bool, u8
, void *, size_t);
187 struct gpio_desc
*gpio
[GPIO_MAX
];
190 struct delayed_work poll
;
191 struct delayed_work timeout
;
192 struct mutex sm_mutex
;
193 unsigned char sm_mod_state
;
194 unsigned char sm_dev_state
;
195 unsigned short sm_state
;
196 unsigned int sm_retries
;
198 struct sfp_eeprom_id id
;
199 #if IS_ENABLED(CONFIG_HWMON)
200 struct sfp_diag diag
;
201 struct device
*hwmon_dev
;
207 static bool sff_module_supported(const struct sfp_eeprom_id
*id
)
209 return id
->base
.phys_id
== SFP_PHYS_ID_SFF
&&
210 id
->base
.phys_ext_id
== SFP_PHYS_EXT_ID_SFP
;
213 static const struct sff_data sff_data
= {
214 .gpios
= SFP_F_LOS
| SFP_F_TX_FAULT
| SFP_F_TX_DISABLE
,
215 .module_supported
= sff_module_supported
,
218 static bool sfp_module_supported(const struct sfp_eeprom_id
*id
)
220 return id
->base
.phys_id
== SFP_PHYS_ID_SFP
&&
221 id
->base
.phys_ext_id
== SFP_PHYS_EXT_ID_SFP
;
224 static const struct sff_data sfp_data
= {
225 .gpios
= SFP_F_PRESENT
| SFP_F_LOS
| SFP_F_TX_FAULT
|
226 SFP_F_TX_DISABLE
| SFP_F_RATE_SELECT
,
227 .module_supported
= sfp_module_supported
,
230 static const struct of_device_id sfp_of_match
[] = {
231 { .compatible
= "sff,sff", .data
= &sff_data
, },
232 { .compatible
= "sff,sfp", .data
= &sfp_data
, },
235 MODULE_DEVICE_TABLE(of
, sfp_of_match
);
237 static unsigned long poll_jiffies
;
239 static unsigned int sfp_gpio_get_state(struct sfp
*sfp
)
241 unsigned int i
, state
, v
;
243 for (i
= state
= 0; i
< GPIO_MAX
; i
++) {
244 if (gpio_flags
[i
] != GPIOD_IN
|| !sfp
->gpio
[i
])
247 v
= gpiod_get_value_cansleep(sfp
->gpio
[i
]);
255 static unsigned int sff_gpio_get_state(struct sfp
*sfp
)
257 return sfp_gpio_get_state(sfp
) | SFP_F_PRESENT
;
260 static void sfp_gpio_set_state(struct sfp
*sfp
, unsigned int state
)
262 if (state
& SFP_F_PRESENT
) {
263 /* If the module is present, drive the signals */
264 if (sfp
->gpio
[GPIO_TX_DISABLE
])
265 gpiod_direction_output(sfp
->gpio
[GPIO_TX_DISABLE
],
266 state
& SFP_F_TX_DISABLE
);
267 if (state
& SFP_F_RATE_SELECT
)
268 gpiod_direction_output(sfp
->gpio
[GPIO_RATE_SELECT
],
269 state
& SFP_F_RATE_SELECT
);
271 /* Otherwise, let them float to the pull-ups */
272 if (sfp
->gpio
[GPIO_TX_DISABLE
])
273 gpiod_direction_input(sfp
->gpio
[GPIO_TX_DISABLE
]);
274 if (state
& SFP_F_RATE_SELECT
)
275 gpiod_direction_input(sfp
->gpio
[GPIO_RATE_SELECT
]);
279 static int sfp_i2c_read(struct sfp
*sfp
, bool a2
, u8 dev_addr
, void *buf
,
282 struct i2c_msg msgs
[2];
283 u8 bus_addr
= a2
? 0x51 : 0x50;
286 msgs
[0].addr
= bus_addr
;
289 msgs
[0].buf
= &dev_addr
;
290 msgs
[1].addr
= bus_addr
;
291 msgs
[1].flags
= I2C_M_RD
;
295 ret
= i2c_transfer(sfp
->i2c
, msgs
, ARRAY_SIZE(msgs
));
299 return ret
== ARRAY_SIZE(msgs
) ? len
: 0;
302 static int sfp_i2c_write(struct sfp
*sfp
, bool a2
, u8 dev_addr
, void *buf
,
305 struct i2c_msg msgs
[1];
306 u8 bus_addr
= a2
? 0x51 : 0x50;
309 msgs
[0].addr
= bus_addr
;
311 msgs
[0].len
= 1 + len
;
312 msgs
[0].buf
= kmalloc(1 + len
, GFP_KERNEL
);
316 msgs
[0].buf
[0] = dev_addr
;
317 memcpy(&msgs
[0].buf
[1], buf
, len
);
319 ret
= i2c_transfer(sfp
->i2c
, msgs
, ARRAY_SIZE(msgs
));
326 return ret
== ARRAY_SIZE(msgs
) ? len
: 0;
329 static int sfp_i2c_configure(struct sfp
*sfp
, struct i2c_adapter
*i2c
)
331 struct mii_bus
*i2c_mii
;
334 if (!i2c_check_functionality(i2c
, I2C_FUNC_I2C
))
338 sfp
->read
= sfp_i2c_read
;
339 sfp
->write
= sfp_i2c_write
;
341 i2c_mii
= mdio_i2c_alloc(sfp
->dev
, i2c
);
343 return PTR_ERR(i2c_mii
);
345 i2c_mii
->name
= "SFP I2C Bus";
346 i2c_mii
->phy_mask
= ~0;
348 ret
= mdiobus_register(i2c_mii
);
350 mdiobus_free(i2c_mii
);
354 sfp
->i2c_mii
= i2c_mii
;
360 static unsigned int sfp_get_state(struct sfp
*sfp
)
362 return sfp
->get_state(sfp
);
365 static void sfp_set_state(struct sfp
*sfp
, unsigned int state
)
367 sfp
->set_state(sfp
, state
);
370 static int sfp_read(struct sfp
*sfp
, bool a2
, u8 addr
, void *buf
, size_t len
)
372 return sfp
->read(sfp
, a2
, addr
, buf
, len
);
375 static int sfp_write(struct sfp
*sfp
, bool a2
, u8 addr
, void *buf
, size_t len
)
377 return sfp
->write(sfp
, a2
, addr
, buf
, len
);
380 static unsigned int sfp_check(void *buf
, size_t len
)
384 for (p
= buf
, check
= 0; len
; p
++, len
--)
391 #if IS_ENABLED(CONFIG_HWMON)
392 static umode_t
sfp_hwmon_is_visible(const void *data
,
393 enum hwmon_sensor_types type
,
394 u32 attr
, int channel
)
396 const struct sfp
*sfp
= data
;
401 case hwmon_temp_min_alarm
:
402 case hwmon_temp_max_alarm
:
403 case hwmon_temp_lcrit_alarm
:
404 case hwmon_temp_crit_alarm
:
407 case hwmon_temp_lcrit
:
408 case hwmon_temp_crit
:
409 if (!(sfp
->id
.ext
.enhopts
& SFP_ENHOPTS_ALARMWARN
))
412 case hwmon_temp_input
:
419 case hwmon_in_min_alarm
:
420 case hwmon_in_max_alarm
:
421 case hwmon_in_lcrit_alarm
:
422 case hwmon_in_crit_alarm
:
427 if (!(sfp
->id
.ext
.enhopts
& SFP_ENHOPTS_ALARMWARN
))
437 case hwmon_curr_min_alarm
:
438 case hwmon_curr_max_alarm
:
439 case hwmon_curr_lcrit_alarm
:
440 case hwmon_curr_crit_alarm
:
443 case hwmon_curr_lcrit
:
444 case hwmon_curr_crit
:
445 if (!(sfp
->id
.ext
.enhopts
& SFP_ENHOPTS_ALARMWARN
))
448 case hwmon_curr_input
:
454 /* External calibration of receive power requires
455 * floating point arithmetic. Doing that in the kernel
456 * is not easy, so just skip it. If the module does
457 * not require external calibration, we can however
458 * show receiver power, since FP is then not needed.
460 if (sfp
->id
.ext
.diagmon
& SFP_DIAGMON_EXT_CAL
&&
464 case hwmon_power_min_alarm
:
465 case hwmon_power_max_alarm
:
466 case hwmon_power_lcrit_alarm
:
467 case hwmon_power_crit_alarm
:
468 case hwmon_power_min
:
469 case hwmon_power_max
:
470 case hwmon_power_lcrit
:
471 case hwmon_power_crit
:
472 if (!(sfp
->id
.ext
.enhopts
& SFP_ENHOPTS_ALARMWARN
))
475 case hwmon_power_input
:
485 static int sfp_hwmon_read_sensor(struct sfp
*sfp
, int reg
, long *value
)
490 err
= sfp_read(sfp
, true, reg
, &val
, sizeof(val
));
494 *value
= be16_to_cpu(val
);
499 static void sfp_hwmon_to_rx_power(long *value
)
501 *value
= DIV_ROUND_CLOSEST(*value
, 100);
504 static void sfp_hwmon_calibrate(struct sfp
*sfp
, unsigned int slope
, int offset
,
507 if (sfp
->id
.ext
.diagmon
& SFP_DIAGMON_EXT_CAL
)
508 *value
= DIV_ROUND_CLOSEST(*value
* slope
, 256) + offset
;
511 static void sfp_hwmon_calibrate_temp(struct sfp
*sfp
, long *value
)
513 sfp_hwmon_calibrate(sfp
, be16_to_cpu(sfp
->diag
.cal_t_slope
),
514 be16_to_cpu(sfp
->diag
.cal_t_offset
), value
);
516 if (*value
>= 0x8000)
519 *value
= DIV_ROUND_CLOSEST(*value
* 1000, 256);
522 static void sfp_hwmon_calibrate_vcc(struct sfp
*sfp
, long *value
)
524 sfp_hwmon_calibrate(sfp
, be16_to_cpu(sfp
->diag
.cal_v_slope
),
525 be16_to_cpu(sfp
->diag
.cal_v_offset
), value
);
527 *value
= DIV_ROUND_CLOSEST(*value
, 10);
530 static void sfp_hwmon_calibrate_bias(struct sfp
*sfp
, long *value
)
532 sfp_hwmon_calibrate(sfp
, be16_to_cpu(sfp
->diag
.cal_txi_slope
),
533 be16_to_cpu(sfp
->diag
.cal_txi_offset
), value
);
535 *value
= DIV_ROUND_CLOSEST(*value
, 500);
538 static void sfp_hwmon_calibrate_tx_power(struct sfp
*sfp
, long *value
)
540 sfp_hwmon_calibrate(sfp
, be16_to_cpu(sfp
->diag
.cal_txpwr_slope
),
541 be16_to_cpu(sfp
->diag
.cal_txpwr_offset
), value
);
543 *value
= DIV_ROUND_CLOSEST(*value
, 10);
546 static int sfp_hwmon_read_temp(struct sfp
*sfp
, int reg
, long *value
)
550 err
= sfp_hwmon_read_sensor(sfp
, reg
, value
);
554 sfp_hwmon_calibrate_temp(sfp
, value
);
559 static int sfp_hwmon_read_vcc(struct sfp
*sfp
, int reg
, long *value
)
563 err
= sfp_hwmon_read_sensor(sfp
, reg
, value
);
567 sfp_hwmon_calibrate_vcc(sfp
, value
);
572 static int sfp_hwmon_read_bias(struct sfp
*sfp
, int reg
, long *value
)
576 err
= sfp_hwmon_read_sensor(sfp
, reg
, value
);
580 sfp_hwmon_calibrate_bias(sfp
, value
);
585 static int sfp_hwmon_read_tx_power(struct sfp
*sfp
, int reg
, long *value
)
589 err
= sfp_hwmon_read_sensor(sfp
, reg
, value
);
593 sfp_hwmon_calibrate_tx_power(sfp
, value
);
598 static int sfp_hwmon_read_rx_power(struct sfp
*sfp
, int reg
, long *value
)
602 err
= sfp_hwmon_read_sensor(sfp
, reg
, value
);
606 sfp_hwmon_to_rx_power(value
);
611 static int sfp_hwmon_temp(struct sfp
*sfp
, u32 attr
, long *value
)
617 case hwmon_temp_input
:
618 return sfp_hwmon_read_temp(sfp
, SFP_TEMP
, value
);
620 case hwmon_temp_lcrit
:
621 *value
= be16_to_cpu(sfp
->diag
.temp_low_alarm
);
622 sfp_hwmon_calibrate_temp(sfp
, value
);
626 *value
= be16_to_cpu(sfp
->diag
.temp_low_warn
);
627 sfp_hwmon_calibrate_temp(sfp
, value
);
630 *value
= be16_to_cpu(sfp
->diag
.temp_high_warn
);
631 sfp_hwmon_calibrate_temp(sfp
, value
);
634 case hwmon_temp_crit
:
635 *value
= be16_to_cpu(sfp
->diag
.temp_high_alarm
);
636 sfp_hwmon_calibrate_temp(sfp
, value
);
639 case hwmon_temp_lcrit_alarm
:
640 err
= sfp_read(sfp
, true, SFP_ALARM0
, &status
, sizeof(status
));
644 *value
= !!(status
& SFP_ALARM0_TEMP_LOW
);
647 case hwmon_temp_min_alarm
:
648 err
= sfp_read(sfp
, true, SFP_WARN0
, &status
, sizeof(status
));
652 *value
= !!(status
& SFP_WARN0_TEMP_LOW
);
655 case hwmon_temp_max_alarm
:
656 err
= sfp_read(sfp
, true, SFP_WARN0
, &status
, sizeof(status
));
660 *value
= !!(status
& SFP_WARN0_TEMP_HIGH
);
663 case hwmon_temp_crit_alarm
:
664 err
= sfp_read(sfp
, true, SFP_ALARM0
, &status
, sizeof(status
));
668 *value
= !!(status
& SFP_ALARM0_TEMP_HIGH
);
677 static int sfp_hwmon_vcc(struct sfp
*sfp
, u32 attr
, long *value
)
684 return sfp_hwmon_read_vcc(sfp
, SFP_VCC
, value
);
687 *value
= be16_to_cpu(sfp
->diag
.volt_low_alarm
);
688 sfp_hwmon_calibrate_vcc(sfp
, value
);
692 *value
= be16_to_cpu(sfp
->diag
.volt_low_warn
);
693 sfp_hwmon_calibrate_vcc(sfp
, value
);
697 *value
= be16_to_cpu(sfp
->diag
.volt_high_warn
);
698 sfp_hwmon_calibrate_vcc(sfp
, value
);
702 *value
= be16_to_cpu(sfp
->diag
.volt_high_alarm
);
703 sfp_hwmon_calibrate_vcc(sfp
, value
);
706 case hwmon_in_lcrit_alarm
:
707 err
= sfp_read(sfp
, true, SFP_ALARM0
, &status
, sizeof(status
));
711 *value
= !!(status
& SFP_ALARM0_VCC_LOW
);
714 case hwmon_in_min_alarm
:
715 err
= sfp_read(sfp
, true, SFP_WARN0
, &status
, sizeof(status
));
719 *value
= !!(status
& SFP_WARN0_VCC_LOW
);
722 case hwmon_in_max_alarm
:
723 err
= sfp_read(sfp
, true, SFP_WARN0
, &status
, sizeof(status
));
727 *value
= !!(status
& SFP_WARN0_VCC_HIGH
);
730 case hwmon_in_crit_alarm
:
731 err
= sfp_read(sfp
, true, SFP_ALARM0
, &status
, sizeof(status
));
735 *value
= !!(status
& SFP_ALARM0_VCC_HIGH
);
744 static int sfp_hwmon_bias(struct sfp
*sfp
, u32 attr
, long *value
)
750 case hwmon_curr_input
:
751 return sfp_hwmon_read_bias(sfp
, SFP_TX_BIAS
, value
);
753 case hwmon_curr_lcrit
:
754 *value
= be16_to_cpu(sfp
->diag
.bias_low_alarm
);
755 sfp_hwmon_calibrate_bias(sfp
, value
);
759 *value
= be16_to_cpu(sfp
->diag
.bias_low_warn
);
760 sfp_hwmon_calibrate_bias(sfp
, value
);
764 *value
= be16_to_cpu(sfp
->diag
.bias_high_warn
);
765 sfp_hwmon_calibrate_bias(sfp
, value
);
768 case hwmon_curr_crit
:
769 *value
= be16_to_cpu(sfp
->diag
.bias_high_alarm
);
770 sfp_hwmon_calibrate_bias(sfp
, value
);
773 case hwmon_curr_lcrit_alarm
:
774 err
= sfp_read(sfp
, true, SFP_ALARM0
, &status
, sizeof(status
));
778 *value
= !!(status
& SFP_ALARM0_TX_BIAS_LOW
);
781 case hwmon_curr_min_alarm
:
782 err
= sfp_read(sfp
, true, SFP_WARN0
, &status
, sizeof(status
));
786 *value
= !!(status
& SFP_WARN0_TX_BIAS_LOW
);
789 case hwmon_curr_max_alarm
:
790 err
= sfp_read(sfp
, true, SFP_WARN0
, &status
, sizeof(status
));
794 *value
= !!(status
& SFP_WARN0_TX_BIAS_HIGH
);
797 case hwmon_curr_crit_alarm
:
798 err
= sfp_read(sfp
, true, SFP_ALARM0
, &status
, sizeof(status
));
802 *value
= !!(status
& SFP_ALARM0_TX_BIAS_HIGH
);
811 static int sfp_hwmon_tx_power(struct sfp
*sfp
, u32 attr
, long *value
)
817 case hwmon_power_input
:
818 return sfp_hwmon_read_tx_power(sfp
, SFP_TX_POWER
, value
);
820 case hwmon_power_lcrit
:
821 *value
= be16_to_cpu(sfp
->diag
.txpwr_low_alarm
);
822 sfp_hwmon_calibrate_tx_power(sfp
, value
);
825 case hwmon_power_min
:
826 *value
= be16_to_cpu(sfp
->diag
.txpwr_low_warn
);
827 sfp_hwmon_calibrate_tx_power(sfp
, value
);
830 case hwmon_power_max
:
831 *value
= be16_to_cpu(sfp
->diag
.txpwr_high_warn
);
832 sfp_hwmon_calibrate_tx_power(sfp
, value
);
835 case hwmon_power_crit
:
836 *value
= be16_to_cpu(sfp
->diag
.txpwr_high_alarm
);
837 sfp_hwmon_calibrate_tx_power(sfp
, value
);
840 case hwmon_power_lcrit_alarm
:
841 err
= sfp_read(sfp
, true, SFP_ALARM0
, &status
, sizeof(status
));
845 *value
= !!(status
& SFP_ALARM0_TXPWR_LOW
);
848 case hwmon_power_min_alarm
:
849 err
= sfp_read(sfp
, true, SFP_WARN0
, &status
, sizeof(status
));
853 *value
= !!(status
& SFP_WARN0_TXPWR_LOW
);
856 case hwmon_power_max_alarm
:
857 err
= sfp_read(sfp
, true, SFP_WARN0
, &status
, sizeof(status
));
861 *value
= !!(status
& SFP_WARN0_TXPWR_HIGH
);
864 case hwmon_power_crit_alarm
:
865 err
= sfp_read(sfp
, true, SFP_ALARM0
, &status
, sizeof(status
));
869 *value
= !!(status
& SFP_ALARM0_TXPWR_HIGH
);
878 static int sfp_hwmon_rx_power(struct sfp
*sfp
, u32 attr
, long *value
)
884 case hwmon_power_input
:
885 return sfp_hwmon_read_rx_power(sfp
, SFP_RX_POWER
, value
);
887 case hwmon_power_lcrit
:
888 *value
= be16_to_cpu(sfp
->diag
.rxpwr_low_alarm
);
889 sfp_hwmon_to_rx_power(value
);
892 case hwmon_power_min
:
893 *value
= be16_to_cpu(sfp
->diag
.rxpwr_low_warn
);
894 sfp_hwmon_to_rx_power(value
);
897 case hwmon_power_max
:
898 *value
= be16_to_cpu(sfp
->diag
.rxpwr_high_warn
);
899 sfp_hwmon_to_rx_power(value
);
902 case hwmon_power_crit
:
903 *value
= be16_to_cpu(sfp
->diag
.rxpwr_high_alarm
);
904 sfp_hwmon_to_rx_power(value
);
907 case hwmon_power_lcrit_alarm
:
908 err
= sfp_read(sfp
, true, SFP_ALARM1
, &status
, sizeof(status
));
912 *value
= !!(status
& SFP_ALARM1_RXPWR_LOW
);
915 case hwmon_power_min_alarm
:
916 err
= sfp_read(sfp
, true, SFP_WARN1
, &status
, sizeof(status
));
920 *value
= !!(status
& SFP_WARN1_RXPWR_LOW
);
923 case hwmon_power_max_alarm
:
924 err
= sfp_read(sfp
, true, SFP_WARN1
, &status
, sizeof(status
));
928 *value
= !!(status
& SFP_WARN1_RXPWR_HIGH
);
931 case hwmon_power_crit_alarm
:
932 err
= sfp_read(sfp
, true, SFP_ALARM1
, &status
, sizeof(status
));
936 *value
= !!(status
& SFP_ALARM1_RXPWR_HIGH
);
945 static int sfp_hwmon_read(struct device
*dev
, enum hwmon_sensor_types type
,
946 u32 attr
, int channel
, long *value
)
948 struct sfp
*sfp
= dev_get_drvdata(dev
);
952 return sfp_hwmon_temp(sfp
, attr
, value
);
954 return sfp_hwmon_vcc(sfp
, attr
, value
);
956 return sfp_hwmon_bias(sfp
, attr
, value
);
960 return sfp_hwmon_tx_power(sfp
, attr
, value
);
962 return sfp_hwmon_rx_power(sfp
, attr
, value
);
971 static const struct hwmon_ops sfp_hwmon_ops
= {
972 .is_visible
= sfp_hwmon_is_visible
,
973 .read
= sfp_hwmon_read
,
976 static u32 sfp_hwmon_chip_config
[] = {
981 static const struct hwmon_channel_info sfp_hwmon_chip
= {
983 .config
= sfp_hwmon_chip_config
,
986 static u32 sfp_hwmon_temp_config
[] = {
988 HWMON_T_MAX
| HWMON_T_MIN
|
989 HWMON_T_MAX_ALARM
| HWMON_T_MIN_ALARM
|
990 HWMON_T_CRIT
| HWMON_T_LCRIT
|
991 HWMON_T_CRIT_ALARM
| HWMON_T_LCRIT_ALARM
,
995 static const struct hwmon_channel_info sfp_hwmon_temp_channel_info
= {
997 .config
= sfp_hwmon_temp_config
,
1000 static u32 sfp_hwmon_vcc_config
[] = {
1002 HWMON_I_MAX
| HWMON_I_MIN
|
1003 HWMON_I_MAX_ALARM
| HWMON_I_MIN_ALARM
|
1004 HWMON_I_CRIT
| HWMON_I_LCRIT
|
1005 HWMON_I_CRIT_ALARM
| HWMON_I_LCRIT_ALARM
,
1009 static const struct hwmon_channel_info sfp_hwmon_vcc_channel_info
= {
1011 .config
= sfp_hwmon_vcc_config
,
1014 static u32 sfp_hwmon_bias_config
[] = {
1016 HWMON_C_MAX
| HWMON_C_MIN
|
1017 HWMON_C_MAX_ALARM
| HWMON_C_MIN_ALARM
|
1018 HWMON_C_CRIT
| HWMON_C_LCRIT
|
1019 HWMON_C_CRIT_ALARM
| HWMON_C_LCRIT_ALARM
,
1023 static const struct hwmon_channel_info sfp_hwmon_bias_channel_info
= {
1025 .config
= sfp_hwmon_bias_config
,
1028 static u32 sfp_hwmon_power_config
[] = {
1029 /* Transmit power */
1031 HWMON_P_MAX
| HWMON_P_MIN
|
1032 HWMON_P_MAX_ALARM
| HWMON_P_MIN_ALARM
|
1033 HWMON_P_CRIT
| HWMON_P_LCRIT
|
1034 HWMON_P_CRIT_ALARM
| HWMON_P_LCRIT_ALARM
,
1037 HWMON_P_MAX
| HWMON_P_MIN
|
1038 HWMON_P_MAX_ALARM
| HWMON_P_MIN_ALARM
|
1039 HWMON_P_CRIT
| HWMON_P_LCRIT
|
1040 HWMON_P_CRIT_ALARM
| HWMON_P_LCRIT_ALARM
,
1044 static const struct hwmon_channel_info sfp_hwmon_power_channel_info
= {
1045 .type
= hwmon_power
,
1046 .config
= sfp_hwmon_power_config
,
1049 static const struct hwmon_channel_info
*sfp_hwmon_info
[] = {
1051 &sfp_hwmon_vcc_channel_info
,
1052 &sfp_hwmon_temp_channel_info
,
1053 &sfp_hwmon_bias_channel_info
,
1054 &sfp_hwmon_power_channel_info
,
1058 static const struct hwmon_chip_info sfp_hwmon_chip_info
= {
1059 .ops
= &sfp_hwmon_ops
,
1060 .info
= sfp_hwmon_info
,
1063 static int sfp_hwmon_insert(struct sfp
*sfp
)
1067 if (sfp
->id
.ext
.sff8472_compliance
== SFP_SFF8472_COMPLIANCE_NONE
)
1070 if (!(sfp
->id
.ext
.diagmon
& SFP_DIAGMON_DDM
))
1073 if (sfp
->id
.ext
.diagmon
& SFP_DIAGMON_ADDRMODE
)
1074 /* This driver in general does not support address
1079 err
= sfp_read(sfp
, true, 0, &sfp
->diag
, sizeof(sfp
->diag
));
1083 sfp
->hwmon_name
= kstrdup(dev_name(sfp
->dev
), GFP_KERNEL
);
1084 if (!sfp
->hwmon_name
)
1087 for (i
= 0; sfp
->hwmon_name
[i
]; i
++)
1088 if (hwmon_is_bad_char(sfp
->hwmon_name
[i
]))
1089 sfp
->hwmon_name
[i
] = '_';
1091 sfp
->hwmon_dev
= hwmon_device_register_with_info(sfp
->dev
,
1092 sfp
->hwmon_name
, sfp
,
1093 &sfp_hwmon_chip_info
,
1096 return PTR_ERR_OR_ZERO(sfp
->hwmon_dev
);
1099 static void sfp_hwmon_remove(struct sfp
*sfp
)
1101 if (!IS_ERR_OR_NULL(sfp
->hwmon_dev
)) {
1102 hwmon_device_unregister(sfp
->hwmon_dev
);
1103 sfp
->hwmon_dev
= NULL
;
1104 kfree(sfp
->hwmon_name
);
1108 static int sfp_hwmon_insert(struct sfp
*sfp
)
1113 static void sfp_hwmon_remove(struct sfp
*sfp
)
1119 static void sfp_module_tx_disable(struct sfp
*sfp
)
1121 dev_dbg(sfp
->dev
, "tx disable %u -> %u\n",
1122 sfp
->state
& SFP_F_TX_DISABLE
? 1 : 0, 1);
1123 sfp
->state
|= SFP_F_TX_DISABLE
;
1124 sfp_set_state(sfp
, sfp
->state
);
1127 static void sfp_module_tx_enable(struct sfp
*sfp
)
1129 dev_dbg(sfp
->dev
, "tx disable %u -> %u\n",
1130 sfp
->state
& SFP_F_TX_DISABLE
? 1 : 0, 0);
1131 sfp
->state
&= ~SFP_F_TX_DISABLE
;
1132 sfp_set_state(sfp
, sfp
->state
);
1135 static void sfp_module_tx_fault_reset(struct sfp
*sfp
)
1137 unsigned int state
= sfp
->state
;
1139 if (state
& SFP_F_TX_DISABLE
)
1142 sfp_set_state(sfp
, state
| SFP_F_TX_DISABLE
);
1146 sfp_set_state(sfp
, state
);
1149 /* SFP state machine */
1150 static void sfp_sm_set_timer(struct sfp
*sfp
, unsigned int timeout
)
1153 mod_delayed_work(system_power_efficient_wq
, &sfp
->timeout
,
1156 cancel_delayed_work(&sfp
->timeout
);
1159 static void sfp_sm_next(struct sfp
*sfp
, unsigned int state
,
1160 unsigned int timeout
)
1162 sfp
->sm_state
= state
;
1163 sfp_sm_set_timer(sfp
, timeout
);
1166 static void sfp_sm_ins_next(struct sfp
*sfp
, unsigned int state
,
1167 unsigned int timeout
)
1169 sfp
->sm_mod_state
= state
;
1170 sfp_sm_set_timer(sfp
, timeout
);
1173 static void sfp_sm_phy_detach(struct sfp
*sfp
)
1175 phy_stop(sfp
->mod_phy
);
1176 sfp_remove_phy(sfp
->sfp_bus
);
1177 phy_device_remove(sfp
->mod_phy
);
1178 phy_device_free(sfp
->mod_phy
);
1179 sfp
->mod_phy
= NULL
;
1182 static void sfp_sm_probe_phy(struct sfp
*sfp
)
1184 struct phy_device
*phy
;
1187 msleep(T_PHY_RESET_MS
);
1189 phy
= mdiobus_scan(sfp
->i2c_mii
, SFP_PHY_ADDR
);
1190 if (phy
== ERR_PTR(-ENODEV
)) {
1191 dev_info(sfp
->dev
, "no PHY detected\n");
1195 dev_err(sfp
->dev
, "mdiobus scan returned %ld\n", PTR_ERR(phy
));
1199 err
= sfp_add_phy(sfp
->sfp_bus
, phy
);
1201 phy_device_remove(phy
);
1202 phy_device_free(phy
);
1203 dev_err(sfp
->dev
, "sfp_add_phy failed: %d\n", err
);
1211 static void sfp_sm_link_up(struct sfp
*sfp
)
1213 sfp_link_up(sfp
->sfp_bus
);
1214 sfp_sm_next(sfp
, SFP_S_LINK_UP
, 0);
1217 static void sfp_sm_link_down(struct sfp
*sfp
)
1219 sfp_link_down(sfp
->sfp_bus
);
1222 static void sfp_sm_link_check_los(struct sfp
*sfp
)
1224 unsigned int los
= sfp
->state
& SFP_F_LOS
;
1226 /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL
1227 * are set, we assume that no LOS signal is available.
1229 if (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_INVERTED
))
1231 else if (!(sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_NORMAL
)))
1235 sfp_sm_next(sfp
, SFP_S_WAIT_LOS
, 0);
1237 sfp_sm_link_up(sfp
);
1240 static bool sfp_los_event_active(struct sfp
*sfp
, unsigned int event
)
1242 return (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_INVERTED
) &&
1243 event
== SFP_E_LOS_LOW
) ||
1244 (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_NORMAL
) &&
1245 event
== SFP_E_LOS_HIGH
);
1248 static bool sfp_los_event_inactive(struct sfp
*sfp
, unsigned int event
)
1250 return (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_INVERTED
) &&
1251 event
== SFP_E_LOS_HIGH
) ||
1252 (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_NORMAL
) &&
1253 event
== SFP_E_LOS_LOW
);
1256 static void sfp_sm_fault(struct sfp
*sfp
, bool warn
)
1258 if (sfp
->sm_retries
&& !--sfp
->sm_retries
) {
1260 "module persistently indicates fault, disabling\n");
1261 sfp_sm_next(sfp
, SFP_S_TX_DISABLE
, 0);
1264 dev_err(sfp
->dev
, "module transmit fault indicated\n");
1266 sfp_sm_next(sfp
, SFP_S_TX_FAULT
, T_FAULT_RECOVER
);
1270 static void sfp_sm_mod_init(struct sfp
*sfp
)
1272 sfp_module_tx_enable(sfp
);
1274 /* Wait t_init before indicating that the link is up, provided the
1275 * current state indicates no TX_FAULT. If TX_FAULT clears before
1276 * this time, that's fine too.
1278 sfp_sm_next(sfp
, SFP_S_INIT
, T_INIT_JIFFIES
);
1279 sfp
->sm_retries
= 5;
1281 /* Setting the serdes link mode is guesswork: there's no
1282 * field in the EEPROM which indicates what mode should
1285 * If it's a gigabit-only fiber module, it probably does
1286 * not have a PHY, so switch to 802.3z negotiation mode.
1287 * Otherwise, switch to SGMII mode (which is required to
1288 * support non-gigabit speeds) and probe for a PHY.
1290 if (sfp
->id
.base
.e1000_base_t
||
1291 sfp
->id
.base
.e100_base_lx
||
1292 sfp
->id
.base
.e100_base_fx
)
1293 sfp_sm_probe_phy(sfp
);
1296 static int sfp_sm_mod_hpower(struct sfp
*sfp
)
1303 if (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_POWER_DECL
))
1305 if (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL
))
1308 if (sfp
->id
.ext
.sff8472_compliance
== SFP_SFF8472_COMPLIANCE_NONE
&&
1309 (sfp
->id
.ext
.diagmon
& (SFP_DIAGMON_DDM
| SFP_DIAGMON_ADDRMODE
)) !=
1311 /* The module appears not to implement bus address 0xa2,
1312 * or requires an address change sequence, so assume that
1313 * the module powers up in the indicated power mode.
1315 if (power
> sfp
->max_power_mW
) {
1317 "Host does not support %u.%uW modules\n",
1318 power
/ 1000, (power
/ 100) % 10);
1324 if (power
> sfp
->max_power_mW
) {
1326 "Host does not support %u.%uW modules, module left in power mode 1\n",
1327 power
/ 1000, (power
/ 100) % 10);
1334 err
= sfp_read(sfp
, true, SFP_EXT_STATUS
, &val
, sizeof(val
));
1335 if (err
!= sizeof(val
)) {
1336 dev_err(sfp
->dev
, "Failed to read EEPROM: %d\n", err
);
1343 err
= sfp_write(sfp
, true, SFP_EXT_STATUS
, &val
, sizeof(val
));
1344 if (err
!= sizeof(val
)) {
1345 dev_err(sfp
->dev
, "Failed to write EEPROM: %d\n", err
);
1350 dev_info(sfp
->dev
, "Module switched to %u.%uW power level\n",
1351 power
/ 1000, (power
/ 100) % 10);
1352 return T_HPOWER_LEVEL
;
1358 static int sfp_sm_mod_probe(struct sfp
*sfp
)
1360 /* SFP module inserted - read I2C data */
1361 struct sfp_eeprom_id id
;
1366 ret
= sfp_read(sfp
, false, 0, &id
, sizeof(id
));
1368 dev_err(sfp
->dev
, "failed to read EEPROM: %d\n", ret
);
1372 if (ret
!= sizeof(id
)) {
1373 dev_err(sfp
->dev
, "EEPROM short read: %d\n", ret
);
1377 /* Cotsworks do not seem to update the checksums when they
1378 * do the final programming with the final module part number,
1379 * serial number and date code.
1381 cotsworks
= !memcmp(id
.base
.vendor_name
, "COTSWORKS ", 16);
1383 /* Validate the checksum over the base structure */
1384 check
= sfp_check(&id
.base
, sizeof(id
.base
) - 1);
1385 if (check
!= id
.base
.cc_base
) {
1388 "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n",
1389 check
, id
.base
.cc_base
);
1392 "EEPROM base structure checksum failure: 0x%02x != 0x%02x\n",
1393 check
, id
.base
.cc_base
);
1394 print_hex_dump(KERN_ERR
, "sfp EE: ", DUMP_PREFIX_OFFSET
,
1395 16, 1, &id
, sizeof(id
), true);
1400 check
= sfp_check(&id
.ext
, sizeof(id
.ext
) - 1);
1401 if (check
!= id
.ext
.cc_ext
) {
1404 "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n",
1405 check
, id
.ext
.cc_ext
);
1408 "EEPROM extended structure checksum failure: 0x%02x != 0x%02x\n",
1409 check
, id
.ext
.cc_ext
);
1410 print_hex_dump(KERN_ERR
, "sfp EE: ", DUMP_PREFIX_OFFSET
,
1411 16, 1, &id
, sizeof(id
), true);
1412 memset(&id
.ext
, 0, sizeof(id
.ext
));
1418 dev_info(sfp
->dev
, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n",
1419 (int)sizeof(id
.base
.vendor_name
), id
.base
.vendor_name
,
1420 (int)sizeof(id
.base
.vendor_pn
), id
.base
.vendor_pn
,
1421 (int)sizeof(id
.base
.vendor_rev
), id
.base
.vendor_rev
,
1422 (int)sizeof(id
.ext
.vendor_sn
), id
.ext
.vendor_sn
,
1423 (int)sizeof(id
.ext
.datecode
), id
.ext
.datecode
);
1425 /* Check whether we support this module */
1426 if (!sfp
->type
->module_supported(&sfp
->id
)) {
1428 "module is not supported - phys id 0x%02x 0x%02x\n",
1429 sfp
->id
.base
.phys_id
, sfp
->id
.base
.phys_ext_id
);
1433 /* If the module requires address swap mode, warn about it */
1434 if (sfp
->id
.ext
.diagmon
& SFP_DIAGMON_ADDRMODE
)
1436 "module address swap to access page 0xA2 is not supported.\n");
1438 ret
= sfp_hwmon_insert(sfp
);
1442 ret
= sfp_module_insert(sfp
->sfp_bus
, &sfp
->id
);
1446 return sfp_sm_mod_hpower(sfp
);
1449 static void sfp_sm_mod_remove(struct sfp
*sfp
)
1451 sfp_module_remove(sfp
->sfp_bus
);
1453 sfp_hwmon_remove(sfp
);
1456 sfp_sm_phy_detach(sfp
);
1458 sfp_module_tx_disable(sfp
);
1460 memset(&sfp
->id
, 0, sizeof(sfp
->id
));
1462 dev_info(sfp
->dev
, "module removed\n");
1465 static void sfp_sm_event(struct sfp
*sfp
, unsigned int event
)
1467 mutex_lock(&sfp
->sm_mutex
);
1469 dev_dbg(sfp
->dev
, "SM: enter %s:%s:%s event %s\n",
1470 mod_state_to_str(sfp
->sm_mod_state
),
1471 dev_state_to_str(sfp
->sm_dev_state
),
1472 sm_state_to_str(sfp
->sm_state
),
1473 event_to_str(event
));
1475 /* This state machine tracks the insert/remove state of
1476 * the module, and handles probing the on-board EEPROM.
1478 switch (sfp
->sm_mod_state
) {
1480 if (event
== SFP_E_INSERT
) {
1481 sfp_module_tx_disable(sfp
);
1482 sfp_sm_ins_next(sfp
, SFP_MOD_PROBE
, T_PROBE_INIT
);
1487 if (event
== SFP_E_REMOVE
) {
1488 sfp_sm_ins_next(sfp
, SFP_MOD_EMPTY
, 0);
1489 } else if (event
== SFP_E_TIMEOUT
) {
1490 int val
= sfp_sm_mod_probe(sfp
);
1493 sfp_sm_ins_next(sfp
, SFP_MOD_PRESENT
, 0);
1495 sfp_sm_ins_next(sfp
, SFP_MOD_HPOWER
, val
);
1496 else if (val
!= -EAGAIN
)
1497 sfp_sm_ins_next(sfp
, SFP_MOD_ERROR
, 0);
1499 sfp_sm_set_timer(sfp
, T_PROBE_RETRY
);
1503 case SFP_MOD_HPOWER
:
1504 if (event
== SFP_E_TIMEOUT
) {
1505 sfp_sm_ins_next(sfp
, SFP_MOD_PRESENT
, 0);
1509 case SFP_MOD_PRESENT
:
1511 if (event
== SFP_E_REMOVE
) {
1512 sfp_sm_mod_remove(sfp
);
1513 sfp_sm_ins_next(sfp
, SFP_MOD_EMPTY
, 0);
1518 /* This state machine tracks the netdev up/down state */
1519 switch (sfp
->sm_dev_state
) {
1521 if (event
== SFP_E_DEV_UP
)
1522 sfp
->sm_dev_state
= SFP_DEV_UP
;
1526 if (event
== SFP_E_DEV_DOWN
) {
1527 /* If the module has a PHY, avoid raising TX disable
1528 * as this resets the PHY. Otherwise, raise it to
1529 * turn the laser off.
1532 sfp_module_tx_disable(sfp
);
1533 sfp
->sm_dev_state
= SFP_DEV_DOWN
;
1538 /* Some events are global */
1539 if (sfp
->sm_state
!= SFP_S_DOWN
&&
1540 (sfp
->sm_mod_state
!= SFP_MOD_PRESENT
||
1541 sfp
->sm_dev_state
!= SFP_DEV_UP
)) {
1542 if (sfp
->sm_state
== SFP_S_LINK_UP
&&
1543 sfp
->sm_dev_state
== SFP_DEV_UP
)
1544 sfp_sm_link_down(sfp
);
1546 sfp_sm_phy_detach(sfp
);
1547 sfp_sm_next(sfp
, SFP_S_DOWN
, 0);
1548 mutex_unlock(&sfp
->sm_mutex
);
1552 /* The main state machine */
1553 switch (sfp
->sm_state
) {
1555 if (sfp
->sm_mod_state
== SFP_MOD_PRESENT
&&
1556 sfp
->sm_dev_state
== SFP_DEV_UP
)
1557 sfp_sm_mod_init(sfp
);
1561 if (event
== SFP_E_TIMEOUT
&& sfp
->state
& SFP_F_TX_FAULT
)
1562 sfp_sm_fault(sfp
, true);
1563 else if (event
== SFP_E_TIMEOUT
|| event
== SFP_E_TX_CLEAR
)
1564 sfp_sm_link_check_los(sfp
);
1567 case SFP_S_WAIT_LOS
:
1568 if (event
== SFP_E_TX_FAULT
)
1569 sfp_sm_fault(sfp
, true);
1570 else if (sfp_los_event_inactive(sfp
, event
))
1571 sfp_sm_link_up(sfp
);
1575 if (event
== SFP_E_TX_FAULT
) {
1576 sfp_sm_link_down(sfp
);
1577 sfp_sm_fault(sfp
, true);
1578 } else if (sfp_los_event_active(sfp
, event
)) {
1579 sfp_sm_link_down(sfp
);
1580 sfp_sm_next(sfp
, SFP_S_WAIT_LOS
, 0);
1584 case SFP_S_TX_FAULT
:
1585 if (event
== SFP_E_TIMEOUT
) {
1586 sfp_module_tx_fault_reset(sfp
);
1587 sfp_sm_next(sfp
, SFP_S_REINIT
, T_INIT_JIFFIES
);
1592 if (event
== SFP_E_TIMEOUT
&& sfp
->state
& SFP_F_TX_FAULT
) {
1593 sfp_sm_fault(sfp
, false);
1594 } else if (event
== SFP_E_TIMEOUT
|| event
== SFP_E_TX_CLEAR
) {
1595 dev_info(sfp
->dev
, "module transmit fault recovered\n");
1596 sfp_sm_link_check_los(sfp
);
1600 case SFP_S_TX_DISABLE
:
1604 dev_dbg(sfp
->dev
, "SM: exit %s:%s:%s\n",
1605 mod_state_to_str(sfp
->sm_mod_state
),
1606 dev_state_to_str(sfp
->sm_dev_state
),
1607 sm_state_to_str(sfp
->sm_state
));
1609 mutex_unlock(&sfp
->sm_mutex
);
1612 static void sfp_start(struct sfp
*sfp
)
1614 sfp_sm_event(sfp
, SFP_E_DEV_UP
);
1617 static void sfp_stop(struct sfp
*sfp
)
1619 sfp_sm_event(sfp
, SFP_E_DEV_DOWN
);
1622 static int sfp_module_info(struct sfp
*sfp
, struct ethtool_modinfo
*modinfo
)
1624 /* locking... and check module is present */
1626 if (sfp
->id
.ext
.sff8472_compliance
&&
1627 !(sfp
->id
.ext
.diagmon
& SFP_DIAGMON_ADDRMODE
)) {
1628 modinfo
->type
= ETH_MODULE_SFF_8472
;
1629 modinfo
->eeprom_len
= ETH_MODULE_SFF_8472_LEN
;
1631 modinfo
->type
= ETH_MODULE_SFF_8079
;
1632 modinfo
->eeprom_len
= ETH_MODULE_SFF_8079_LEN
;
1637 static int sfp_module_eeprom(struct sfp
*sfp
, struct ethtool_eeprom
*ee
,
1640 unsigned int first
, last
, len
;
1647 last
= ee
->offset
+ ee
->len
;
1648 if (first
< ETH_MODULE_SFF_8079_LEN
) {
1649 len
= min_t(unsigned int, last
, ETH_MODULE_SFF_8079_LEN
);
1652 ret
= sfp_read(sfp
, false, first
, data
, len
);
1659 if (first
< ETH_MODULE_SFF_8472_LEN
&& last
> ETH_MODULE_SFF_8079_LEN
) {
1660 len
= min_t(unsigned int, last
, ETH_MODULE_SFF_8472_LEN
);
1662 first
-= ETH_MODULE_SFF_8079_LEN
;
1664 ret
= sfp_read(sfp
, true, first
, data
, len
);
1671 static const struct sfp_socket_ops sfp_module_ops
= {
1674 .module_info
= sfp_module_info
,
1675 .module_eeprom
= sfp_module_eeprom
,
1678 static void sfp_timeout(struct work_struct
*work
)
1680 struct sfp
*sfp
= container_of(work
, struct sfp
, timeout
.work
);
1683 sfp_sm_event(sfp
, SFP_E_TIMEOUT
);
1687 static void sfp_check_state(struct sfp
*sfp
)
1689 unsigned int state
, i
, changed
;
1691 state
= sfp_get_state(sfp
);
1692 changed
= state
^ sfp
->state
;
1693 changed
&= SFP_F_PRESENT
| SFP_F_LOS
| SFP_F_TX_FAULT
;
1695 for (i
= 0; i
< GPIO_MAX
; i
++)
1696 if (changed
& BIT(i
))
1697 dev_dbg(sfp
->dev
, "%s %u -> %u\n", gpio_of_names
[i
],
1698 !!(sfp
->state
& BIT(i
)), !!(state
& BIT(i
)));
1700 state
|= sfp
->state
& (SFP_F_TX_DISABLE
| SFP_F_RATE_SELECT
);
1704 if (changed
& SFP_F_PRESENT
)
1705 sfp_sm_event(sfp
, state
& SFP_F_PRESENT
?
1706 SFP_E_INSERT
: SFP_E_REMOVE
);
1708 if (changed
& SFP_F_TX_FAULT
)
1709 sfp_sm_event(sfp
, state
& SFP_F_TX_FAULT
?
1710 SFP_E_TX_FAULT
: SFP_E_TX_CLEAR
);
1712 if (changed
& SFP_F_LOS
)
1713 sfp_sm_event(sfp
, state
& SFP_F_LOS
?
1714 SFP_E_LOS_HIGH
: SFP_E_LOS_LOW
);
1718 static irqreturn_t
sfp_irq(int irq
, void *data
)
1720 struct sfp
*sfp
= data
;
1722 sfp_check_state(sfp
);
1727 static void sfp_poll(struct work_struct
*work
)
1729 struct sfp
*sfp
= container_of(work
, struct sfp
, poll
.work
);
1731 sfp_check_state(sfp
);
1732 mod_delayed_work(system_wq
, &sfp
->poll
, poll_jiffies
);
1735 static struct sfp
*sfp_alloc(struct device
*dev
)
1739 sfp
= kzalloc(sizeof(*sfp
), GFP_KERNEL
);
1741 return ERR_PTR(-ENOMEM
);
1745 mutex_init(&sfp
->sm_mutex
);
1746 INIT_DELAYED_WORK(&sfp
->poll
, sfp_poll
);
1747 INIT_DELAYED_WORK(&sfp
->timeout
, sfp_timeout
);
1752 static void sfp_cleanup(void *data
)
1754 struct sfp
*sfp
= data
;
1756 cancel_delayed_work_sync(&sfp
->poll
);
1757 cancel_delayed_work_sync(&sfp
->timeout
);
1759 mdiobus_unregister(sfp
->i2c_mii
);
1760 mdiobus_free(sfp
->i2c_mii
);
1763 i2c_put_adapter(sfp
->i2c
);
1767 static int sfp_probe(struct platform_device
*pdev
)
1769 const struct sff_data
*sff
;
1774 sfp
= sfp_alloc(&pdev
->dev
);
1776 return PTR_ERR(sfp
);
1778 platform_set_drvdata(pdev
, sfp
);
1780 err
= devm_add_action(sfp
->dev
, sfp_cleanup
, sfp
);
1784 sff
= sfp
->type
= &sfp_data
;
1786 if (pdev
->dev
.of_node
) {
1787 struct device_node
*node
= pdev
->dev
.of_node
;
1788 const struct of_device_id
*id
;
1789 struct i2c_adapter
*i2c
;
1790 struct device_node
*np
;
1792 id
= of_match_node(sfp_of_match
, node
);
1796 sff
= sfp
->type
= id
->data
;
1798 np
= of_parse_phandle(node
, "i2c-bus", 0);
1800 dev_err(sfp
->dev
, "missing 'i2c-bus' property\n");
1804 i2c
= of_find_i2c_adapter_by_node(np
);
1807 return -EPROBE_DEFER
;
1809 err
= sfp_i2c_configure(sfp
, i2c
);
1811 i2c_put_adapter(i2c
);
1816 for (i
= 0; i
< GPIO_MAX
; i
++)
1817 if (sff
->gpios
& BIT(i
)) {
1818 sfp
->gpio
[i
] = devm_gpiod_get_optional(sfp
->dev
,
1819 gpio_of_names
[i
], gpio_flags
[i
]);
1820 if (IS_ERR(sfp
->gpio
[i
]))
1821 return PTR_ERR(sfp
->gpio
[i
]);
1824 sfp
->get_state
= sfp_gpio_get_state
;
1825 sfp
->set_state
= sfp_gpio_set_state
;
1827 /* Modules that have no detect signal are always present */
1828 if (!(sfp
->gpio
[GPIO_MODDEF0
]))
1829 sfp
->get_state
= sff_gpio_get_state
;
1831 device_property_read_u32(&pdev
->dev
, "maximum-power-milliwatt",
1832 &sfp
->max_power_mW
);
1833 if (!sfp
->max_power_mW
)
1834 sfp
->max_power_mW
= 1000;
1836 dev_info(sfp
->dev
, "Host maximum power %u.%uW\n",
1837 sfp
->max_power_mW
/ 1000, (sfp
->max_power_mW
/ 100) % 10);
1839 sfp
->sfp_bus
= sfp_register_socket(sfp
->dev
, sfp
, &sfp_module_ops
);
1843 /* Get the initial state, and always signal TX disable,
1844 * since the network interface will not be up.
1846 sfp
->state
= sfp_get_state(sfp
) | SFP_F_TX_DISABLE
;
1848 if (sfp
->gpio
[GPIO_RATE_SELECT
] &&
1849 gpiod_get_value_cansleep(sfp
->gpio
[GPIO_RATE_SELECT
]))
1850 sfp
->state
|= SFP_F_RATE_SELECT
;
1851 sfp_set_state(sfp
, sfp
->state
);
1852 sfp_module_tx_disable(sfp
);
1854 if (sfp
->state
& SFP_F_PRESENT
)
1855 sfp_sm_event(sfp
, SFP_E_INSERT
);
1858 for (i
= 0; i
< GPIO_MAX
; i
++) {
1859 if (gpio_flags
[i
] != GPIOD_IN
|| !sfp
->gpio
[i
])
1862 irq
= gpiod_to_irq(sfp
->gpio
[i
]);
1868 err
= devm_request_threaded_irq(sfp
->dev
, irq
, NULL
, sfp_irq
,
1870 IRQF_TRIGGER_RISING
|
1871 IRQF_TRIGGER_FALLING
,
1872 dev_name(sfp
->dev
), sfp
);
1878 mod_delayed_work(system_wq
, &sfp
->poll
, poll_jiffies
);
1880 /* We could have an issue in cases no Tx disable pin is available or
1881 * wired as modules using a laser as their light source will continue to
1882 * be active when the fiber is removed. This could be a safety issue and
1883 * we should at least warn the user about that.
1885 if (!sfp
->gpio
[GPIO_TX_DISABLE
])
1887 "No tx_disable pin: SFP modules will always be emitting.\n");
1892 static int sfp_remove(struct platform_device
*pdev
)
1894 struct sfp
*sfp
= platform_get_drvdata(pdev
);
1896 sfp_unregister_socket(sfp
->sfp_bus
);
1901 static struct platform_driver sfp_driver
= {
1903 .remove
= sfp_remove
,
1906 .of_match_table
= sfp_of_match
,
1910 static int sfp_init(void)
1912 poll_jiffies
= msecs_to_jiffies(100);
1914 return platform_driver_register(&sfp_driver
);
1916 module_init(sfp_init
);
1918 static void sfp_exit(void)
1920 platform_driver_unregister(&sfp_driver
);
1922 module_exit(sfp_exit
);
1924 MODULE_ALIAS("platform:sfp");
1925 MODULE_AUTHOR("Russell King");
1926 MODULE_LICENSE("GPL v2");