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
168 bool (*module_supported
)(const struct sfp_eeprom_id
*id
);
173 struct i2c_adapter
*i2c
;
174 struct mii_bus
*i2c_mii
;
175 struct sfp_bus
*sfp_bus
;
176 struct phy_device
*mod_phy
;
177 const struct sff_data
*type
;
180 unsigned int (*get_state
)(struct sfp
*);
181 void (*set_state
)(struct sfp
*, unsigned int);
182 int (*read
)(struct sfp
*, bool, u8
, void *, size_t);
183 int (*write
)(struct sfp
*, bool, u8
, void *, size_t);
185 struct gpio_desc
*gpio
[GPIO_MAX
];
188 struct delayed_work poll
;
189 struct delayed_work timeout
;
190 struct mutex sm_mutex
;
191 unsigned char sm_mod_state
;
192 unsigned char sm_dev_state
;
193 unsigned short sm_state
;
194 unsigned int sm_retries
;
196 struct sfp_eeprom_id id
;
197 #if IS_ENABLED(CONFIG_HWMON)
198 struct sfp_diag diag
;
199 struct device
*hwmon_dev
;
205 static bool sff_module_supported(const struct sfp_eeprom_id
*id
)
207 return id
->base
.phys_id
== SFP_PHYS_ID_SFF
&&
208 id
->base
.phys_ext_id
== SFP_PHYS_EXT_ID_SFP
;
211 static const struct sff_data sff_data
= {
212 .gpios
= SFP_F_LOS
| SFP_F_TX_FAULT
| SFP_F_TX_DISABLE
,
213 .module_supported
= sff_module_supported
,
216 static bool sfp_module_supported(const struct sfp_eeprom_id
*id
)
218 return id
->base
.phys_id
== SFP_PHYS_ID_SFP
&&
219 id
->base
.phys_ext_id
== SFP_PHYS_EXT_ID_SFP
;
222 static const struct sff_data sfp_data
= {
223 .gpios
= SFP_F_PRESENT
| SFP_F_LOS
| SFP_F_TX_FAULT
|
224 SFP_F_TX_DISABLE
| SFP_F_RATE_SELECT
,
225 .module_supported
= sfp_module_supported
,
228 static const struct of_device_id sfp_of_match
[] = {
229 { .compatible
= "sff,sff", .data
= &sff_data
, },
230 { .compatible
= "sff,sfp", .data
= &sfp_data
, },
233 MODULE_DEVICE_TABLE(of
, sfp_of_match
);
235 static unsigned long poll_jiffies
;
237 static unsigned int sfp_gpio_get_state(struct sfp
*sfp
)
239 unsigned int i
, state
, v
;
241 for (i
= state
= 0; i
< GPIO_MAX
; i
++) {
242 if (gpio_flags
[i
] != GPIOD_IN
|| !sfp
->gpio
[i
])
245 v
= gpiod_get_value_cansleep(sfp
->gpio
[i
]);
253 static unsigned int sff_gpio_get_state(struct sfp
*sfp
)
255 return sfp_gpio_get_state(sfp
) | SFP_F_PRESENT
;
258 static void sfp_gpio_set_state(struct sfp
*sfp
, unsigned int state
)
260 if (state
& SFP_F_PRESENT
) {
261 /* If the module is present, drive the signals */
262 if (sfp
->gpio
[GPIO_TX_DISABLE
])
263 gpiod_direction_output(sfp
->gpio
[GPIO_TX_DISABLE
],
264 state
& SFP_F_TX_DISABLE
);
265 if (state
& SFP_F_RATE_SELECT
)
266 gpiod_direction_output(sfp
->gpio
[GPIO_RATE_SELECT
],
267 state
& SFP_F_RATE_SELECT
);
269 /* Otherwise, let them float to the pull-ups */
270 if (sfp
->gpio
[GPIO_TX_DISABLE
])
271 gpiod_direction_input(sfp
->gpio
[GPIO_TX_DISABLE
]);
272 if (state
& SFP_F_RATE_SELECT
)
273 gpiod_direction_input(sfp
->gpio
[GPIO_RATE_SELECT
]);
277 static int sfp_i2c_read(struct sfp
*sfp
, bool a2
, u8 dev_addr
, void *buf
,
280 struct i2c_msg msgs
[2];
281 u8 bus_addr
= a2
? 0x51 : 0x50;
284 msgs
[0].addr
= bus_addr
;
287 msgs
[0].buf
= &dev_addr
;
288 msgs
[1].addr
= bus_addr
;
289 msgs
[1].flags
= I2C_M_RD
;
293 ret
= i2c_transfer(sfp
->i2c
, msgs
, ARRAY_SIZE(msgs
));
297 return ret
== ARRAY_SIZE(msgs
) ? len
: 0;
300 static int sfp_i2c_write(struct sfp
*sfp
, bool a2
, u8 dev_addr
, void *buf
,
303 struct i2c_msg msgs
[1];
304 u8 bus_addr
= a2
? 0x51 : 0x50;
307 msgs
[0].addr
= bus_addr
;
309 msgs
[0].len
= 1 + len
;
310 msgs
[0].buf
= kmalloc(1 + len
, GFP_KERNEL
);
314 msgs
[0].buf
[0] = dev_addr
;
315 memcpy(&msgs
[0].buf
[1], buf
, len
);
317 ret
= i2c_transfer(sfp
->i2c
, msgs
, ARRAY_SIZE(msgs
));
324 return ret
== ARRAY_SIZE(msgs
) ? len
: 0;
327 static int sfp_i2c_configure(struct sfp
*sfp
, struct i2c_adapter
*i2c
)
329 struct mii_bus
*i2c_mii
;
332 if (!i2c_check_functionality(i2c
, I2C_FUNC_I2C
))
336 sfp
->read
= sfp_i2c_read
;
337 sfp
->write
= sfp_i2c_write
;
339 i2c_mii
= mdio_i2c_alloc(sfp
->dev
, i2c
);
341 return PTR_ERR(i2c_mii
);
343 i2c_mii
->name
= "SFP I2C Bus";
344 i2c_mii
->phy_mask
= ~0;
346 ret
= mdiobus_register(i2c_mii
);
348 mdiobus_free(i2c_mii
);
352 sfp
->i2c_mii
= i2c_mii
;
358 static unsigned int sfp_get_state(struct sfp
*sfp
)
360 return sfp
->get_state(sfp
);
363 static void sfp_set_state(struct sfp
*sfp
, unsigned int state
)
365 sfp
->set_state(sfp
, state
);
368 static int sfp_read(struct sfp
*sfp
, bool a2
, u8 addr
, void *buf
, size_t len
)
370 return sfp
->read(sfp
, a2
, addr
, buf
, len
);
373 static int sfp_write(struct sfp
*sfp
, bool a2
, u8 addr
, void *buf
, size_t len
)
375 return sfp
->write(sfp
, a2
, addr
, buf
, len
);
378 static unsigned int sfp_check(void *buf
, size_t len
)
382 for (p
= buf
, check
= 0; len
; p
++, len
--)
389 #if IS_ENABLED(CONFIG_HWMON)
390 static umode_t
sfp_hwmon_is_visible(const void *data
,
391 enum hwmon_sensor_types type
,
392 u32 attr
, int channel
)
394 const struct sfp
*sfp
= data
;
399 case hwmon_temp_min_alarm
:
400 case hwmon_temp_max_alarm
:
401 case hwmon_temp_lcrit_alarm
:
402 case hwmon_temp_crit_alarm
:
405 case hwmon_temp_lcrit
:
406 case hwmon_temp_crit
:
407 if (!(sfp
->id
.ext
.enhopts
& SFP_ENHOPTS_ALARMWARN
))
410 case hwmon_temp_input
:
417 case hwmon_in_min_alarm
:
418 case hwmon_in_max_alarm
:
419 case hwmon_in_lcrit_alarm
:
420 case hwmon_in_crit_alarm
:
425 if (!(sfp
->id
.ext
.enhopts
& SFP_ENHOPTS_ALARMWARN
))
435 case hwmon_curr_min_alarm
:
436 case hwmon_curr_max_alarm
:
437 case hwmon_curr_lcrit_alarm
:
438 case hwmon_curr_crit_alarm
:
441 case hwmon_curr_lcrit
:
442 case hwmon_curr_crit
:
443 if (!(sfp
->id
.ext
.enhopts
& SFP_ENHOPTS_ALARMWARN
))
446 case hwmon_curr_input
:
452 /* External calibration of receive power requires
453 * floating point arithmetic. Doing that in the kernel
454 * is not easy, so just skip it. If the module does
455 * not require external calibration, we can however
456 * show receiver power, since FP is then not needed.
458 if (sfp
->id
.ext
.diagmon
& SFP_DIAGMON_EXT_CAL
&&
462 case hwmon_power_min_alarm
:
463 case hwmon_power_max_alarm
:
464 case hwmon_power_lcrit_alarm
:
465 case hwmon_power_crit_alarm
:
466 case hwmon_power_min
:
467 case hwmon_power_max
:
468 case hwmon_power_lcrit
:
469 case hwmon_power_crit
:
470 if (!(sfp
->id
.ext
.enhopts
& SFP_ENHOPTS_ALARMWARN
))
473 case hwmon_power_input
:
483 static int sfp_hwmon_read_sensor(struct sfp
*sfp
, int reg
, long *value
)
488 err
= sfp_read(sfp
, true, reg
, &val
, sizeof(val
));
492 *value
= be16_to_cpu(val
);
497 static void sfp_hwmon_to_rx_power(long *value
)
499 *value
= DIV_ROUND_CLOSEST(*value
, 100);
502 static void sfp_hwmon_calibrate(struct sfp
*sfp
, unsigned int slope
, int offset
,
505 if (sfp
->id
.ext
.diagmon
& SFP_DIAGMON_EXT_CAL
)
506 *value
= DIV_ROUND_CLOSEST(*value
* slope
, 256) + offset
;
509 static void sfp_hwmon_calibrate_temp(struct sfp
*sfp
, long *value
)
511 sfp_hwmon_calibrate(sfp
, be16_to_cpu(sfp
->diag
.cal_t_slope
),
512 be16_to_cpu(sfp
->diag
.cal_t_offset
), value
);
514 if (*value
>= 0x8000)
517 *value
= DIV_ROUND_CLOSEST(*value
* 1000, 256);
520 static void sfp_hwmon_calibrate_vcc(struct sfp
*sfp
, long *value
)
522 sfp_hwmon_calibrate(sfp
, be16_to_cpu(sfp
->diag
.cal_v_slope
),
523 be16_to_cpu(sfp
->diag
.cal_v_offset
), value
);
525 *value
= DIV_ROUND_CLOSEST(*value
, 10);
528 static void sfp_hwmon_calibrate_bias(struct sfp
*sfp
, long *value
)
530 sfp_hwmon_calibrate(sfp
, be16_to_cpu(sfp
->diag
.cal_txi_slope
),
531 be16_to_cpu(sfp
->diag
.cal_txi_offset
), value
);
533 *value
= DIV_ROUND_CLOSEST(*value
, 500);
536 static void sfp_hwmon_calibrate_tx_power(struct sfp
*sfp
, long *value
)
538 sfp_hwmon_calibrate(sfp
, be16_to_cpu(sfp
->diag
.cal_txpwr_slope
),
539 be16_to_cpu(sfp
->diag
.cal_txpwr_offset
), value
);
541 *value
= DIV_ROUND_CLOSEST(*value
, 10);
544 static int sfp_hwmon_read_temp(struct sfp
*sfp
, int reg
, long *value
)
548 err
= sfp_hwmon_read_sensor(sfp
, reg
, value
);
552 sfp_hwmon_calibrate_temp(sfp
, value
);
557 static int sfp_hwmon_read_vcc(struct sfp
*sfp
, int reg
, long *value
)
561 err
= sfp_hwmon_read_sensor(sfp
, reg
, value
);
565 sfp_hwmon_calibrate_vcc(sfp
, value
);
570 static int sfp_hwmon_read_bias(struct sfp
*sfp
, int reg
, long *value
)
574 err
= sfp_hwmon_read_sensor(sfp
, reg
, value
);
578 sfp_hwmon_calibrate_bias(sfp
, value
);
583 static int sfp_hwmon_read_tx_power(struct sfp
*sfp
, int reg
, long *value
)
587 err
= sfp_hwmon_read_sensor(sfp
, reg
, value
);
591 sfp_hwmon_calibrate_tx_power(sfp
, value
);
596 static int sfp_hwmon_read_rx_power(struct sfp
*sfp
, int reg
, long *value
)
600 err
= sfp_hwmon_read_sensor(sfp
, reg
, value
);
604 sfp_hwmon_to_rx_power(value
);
609 static int sfp_hwmon_temp(struct sfp
*sfp
, u32 attr
, long *value
)
615 case hwmon_temp_input
:
616 return sfp_hwmon_read_temp(sfp
, SFP_TEMP
, value
);
618 case hwmon_temp_lcrit
:
619 *value
= be16_to_cpu(sfp
->diag
.temp_low_alarm
);
620 sfp_hwmon_calibrate_temp(sfp
, value
);
624 *value
= be16_to_cpu(sfp
->diag
.temp_low_warn
);
625 sfp_hwmon_calibrate_temp(sfp
, value
);
628 *value
= be16_to_cpu(sfp
->diag
.temp_high_warn
);
629 sfp_hwmon_calibrate_temp(sfp
, value
);
632 case hwmon_temp_crit
:
633 *value
= be16_to_cpu(sfp
->diag
.temp_high_alarm
);
634 sfp_hwmon_calibrate_temp(sfp
, value
);
637 case hwmon_temp_lcrit_alarm
:
638 err
= sfp_read(sfp
, true, SFP_ALARM0
, &status
, sizeof(status
));
642 *value
= !!(status
& SFP_ALARM0_TEMP_LOW
);
645 case hwmon_temp_min_alarm
:
646 err
= sfp_read(sfp
, true, SFP_WARN0
, &status
, sizeof(status
));
650 *value
= !!(status
& SFP_WARN0_TEMP_LOW
);
653 case hwmon_temp_max_alarm
:
654 err
= sfp_read(sfp
, true, SFP_WARN0
, &status
, sizeof(status
));
658 *value
= !!(status
& SFP_WARN0_TEMP_HIGH
);
661 case hwmon_temp_crit_alarm
:
662 err
= sfp_read(sfp
, true, SFP_ALARM0
, &status
, sizeof(status
));
666 *value
= !!(status
& SFP_ALARM0_TEMP_HIGH
);
675 static int sfp_hwmon_vcc(struct sfp
*sfp
, u32 attr
, long *value
)
682 return sfp_hwmon_read_vcc(sfp
, SFP_VCC
, value
);
685 *value
= be16_to_cpu(sfp
->diag
.volt_low_alarm
);
686 sfp_hwmon_calibrate_vcc(sfp
, value
);
690 *value
= be16_to_cpu(sfp
->diag
.volt_low_warn
);
691 sfp_hwmon_calibrate_vcc(sfp
, value
);
695 *value
= be16_to_cpu(sfp
->diag
.volt_high_warn
);
696 sfp_hwmon_calibrate_vcc(sfp
, value
);
700 *value
= be16_to_cpu(sfp
->diag
.volt_high_alarm
);
701 sfp_hwmon_calibrate_vcc(sfp
, value
);
704 case hwmon_in_lcrit_alarm
:
705 err
= sfp_read(sfp
, true, SFP_ALARM0
, &status
, sizeof(status
));
709 *value
= !!(status
& SFP_ALARM0_VCC_LOW
);
712 case hwmon_in_min_alarm
:
713 err
= sfp_read(sfp
, true, SFP_WARN0
, &status
, sizeof(status
));
717 *value
= !!(status
& SFP_WARN0_VCC_LOW
);
720 case hwmon_in_max_alarm
:
721 err
= sfp_read(sfp
, true, SFP_WARN0
, &status
, sizeof(status
));
725 *value
= !!(status
& SFP_WARN0_VCC_HIGH
);
728 case hwmon_in_crit_alarm
:
729 err
= sfp_read(sfp
, true, SFP_ALARM0
, &status
, sizeof(status
));
733 *value
= !!(status
& SFP_ALARM0_VCC_HIGH
);
742 static int sfp_hwmon_bias(struct sfp
*sfp
, u32 attr
, long *value
)
748 case hwmon_curr_input
:
749 return sfp_hwmon_read_bias(sfp
, SFP_TX_BIAS
, value
);
751 case hwmon_curr_lcrit
:
752 *value
= be16_to_cpu(sfp
->diag
.bias_low_alarm
);
753 sfp_hwmon_calibrate_bias(sfp
, value
);
757 *value
= be16_to_cpu(sfp
->diag
.bias_low_warn
);
758 sfp_hwmon_calibrate_bias(sfp
, value
);
762 *value
= be16_to_cpu(sfp
->diag
.bias_high_warn
);
763 sfp_hwmon_calibrate_bias(sfp
, value
);
766 case hwmon_curr_crit
:
767 *value
= be16_to_cpu(sfp
->diag
.bias_high_alarm
);
768 sfp_hwmon_calibrate_bias(sfp
, value
);
771 case hwmon_curr_lcrit_alarm
:
772 err
= sfp_read(sfp
, true, SFP_ALARM0
, &status
, sizeof(status
));
776 *value
= !!(status
& SFP_ALARM0_TX_BIAS_LOW
);
779 case hwmon_curr_min_alarm
:
780 err
= sfp_read(sfp
, true, SFP_WARN0
, &status
, sizeof(status
));
784 *value
= !!(status
& SFP_WARN0_TX_BIAS_LOW
);
787 case hwmon_curr_max_alarm
:
788 err
= sfp_read(sfp
, true, SFP_WARN0
, &status
, sizeof(status
));
792 *value
= !!(status
& SFP_WARN0_TX_BIAS_HIGH
);
795 case hwmon_curr_crit_alarm
:
796 err
= sfp_read(sfp
, true, SFP_ALARM0
, &status
, sizeof(status
));
800 *value
= !!(status
& SFP_ALARM0_TX_BIAS_HIGH
);
809 static int sfp_hwmon_tx_power(struct sfp
*sfp
, u32 attr
, long *value
)
815 case hwmon_power_input
:
816 return sfp_hwmon_read_tx_power(sfp
, SFP_TX_POWER
, value
);
818 case hwmon_power_lcrit
:
819 *value
= be16_to_cpu(sfp
->diag
.txpwr_low_alarm
);
820 sfp_hwmon_calibrate_tx_power(sfp
, value
);
823 case hwmon_power_min
:
824 *value
= be16_to_cpu(sfp
->diag
.txpwr_low_warn
);
825 sfp_hwmon_calibrate_tx_power(sfp
, value
);
828 case hwmon_power_max
:
829 *value
= be16_to_cpu(sfp
->diag
.txpwr_high_warn
);
830 sfp_hwmon_calibrate_tx_power(sfp
, value
);
833 case hwmon_power_crit
:
834 *value
= be16_to_cpu(sfp
->diag
.txpwr_high_alarm
);
835 sfp_hwmon_calibrate_tx_power(sfp
, value
);
838 case hwmon_power_lcrit_alarm
:
839 err
= sfp_read(sfp
, true, SFP_ALARM0
, &status
, sizeof(status
));
843 *value
= !!(status
& SFP_ALARM0_TXPWR_LOW
);
846 case hwmon_power_min_alarm
:
847 err
= sfp_read(sfp
, true, SFP_WARN0
, &status
, sizeof(status
));
851 *value
= !!(status
& SFP_WARN0_TXPWR_LOW
);
854 case hwmon_power_max_alarm
:
855 err
= sfp_read(sfp
, true, SFP_WARN0
, &status
, sizeof(status
));
859 *value
= !!(status
& SFP_WARN0_TXPWR_HIGH
);
862 case hwmon_power_crit_alarm
:
863 err
= sfp_read(sfp
, true, SFP_ALARM0
, &status
, sizeof(status
));
867 *value
= !!(status
& SFP_ALARM0_TXPWR_HIGH
);
876 static int sfp_hwmon_rx_power(struct sfp
*sfp
, u32 attr
, long *value
)
882 case hwmon_power_input
:
883 return sfp_hwmon_read_rx_power(sfp
, SFP_RX_POWER
, value
);
885 case hwmon_power_lcrit
:
886 *value
= be16_to_cpu(sfp
->diag
.rxpwr_low_alarm
);
887 sfp_hwmon_to_rx_power(value
);
890 case hwmon_power_min
:
891 *value
= be16_to_cpu(sfp
->diag
.rxpwr_low_warn
);
892 sfp_hwmon_to_rx_power(value
);
895 case hwmon_power_max
:
896 *value
= be16_to_cpu(sfp
->diag
.rxpwr_high_warn
);
897 sfp_hwmon_to_rx_power(value
);
900 case hwmon_power_crit
:
901 *value
= be16_to_cpu(sfp
->diag
.rxpwr_high_alarm
);
902 sfp_hwmon_to_rx_power(value
);
905 case hwmon_power_lcrit_alarm
:
906 err
= sfp_read(sfp
, true, SFP_ALARM1
, &status
, sizeof(status
));
910 *value
= !!(status
& SFP_ALARM1_RXPWR_LOW
);
913 case hwmon_power_min_alarm
:
914 err
= sfp_read(sfp
, true, SFP_WARN1
, &status
, sizeof(status
));
918 *value
= !!(status
& SFP_WARN1_RXPWR_LOW
);
921 case hwmon_power_max_alarm
:
922 err
= sfp_read(sfp
, true, SFP_WARN1
, &status
, sizeof(status
));
926 *value
= !!(status
& SFP_WARN1_RXPWR_HIGH
);
929 case hwmon_power_crit_alarm
:
930 err
= sfp_read(sfp
, true, SFP_ALARM1
, &status
, sizeof(status
));
934 *value
= !!(status
& SFP_ALARM1_RXPWR_HIGH
);
943 static int sfp_hwmon_read(struct device
*dev
, enum hwmon_sensor_types type
,
944 u32 attr
, int channel
, long *value
)
946 struct sfp
*sfp
= dev_get_drvdata(dev
);
950 return sfp_hwmon_temp(sfp
, attr
, value
);
952 return sfp_hwmon_vcc(sfp
, attr
, value
);
954 return sfp_hwmon_bias(sfp
, attr
, value
);
958 return sfp_hwmon_tx_power(sfp
, attr
, value
);
960 return sfp_hwmon_rx_power(sfp
, attr
, value
);
969 static const struct hwmon_ops sfp_hwmon_ops
= {
970 .is_visible
= sfp_hwmon_is_visible
,
971 .read
= sfp_hwmon_read
,
974 static u32 sfp_hwmon_chip_config
[] = {
979 static const struct hwmon_channel_info sfp_hwmon_chip
= {
981 .config
= sfp_hwmon_chip_config
,
984 static u32 sfp_hwmon_temp_config
[] = {
986 HWMON_T_MAX
| HWMON_T_MIN
|
987 HWMON_T_MAX_ALARM
| HWMON_T_MIN_ALARM
|
988 HWMON_T_CRIT
| HWMON_T_LCRIT
|
989 HWMON_T_CRIT_ALARM
| HWMON_T_LCRIT_ALARM
,
993 static const struct hwmon_channel_info sfp_hwmon_temp_channel_info
= {
995 .config
= sfp_hwmon_temp_config
,
998 static u32 sfp_hwmon_vcc_config
[] = {
1000 HWMON_I_MAX
| HWMON_I_MIN
|
1001 HWMON_I_MAX_ALARM
| HWMON_I_MIN_ALARM
|
1002 HWMON_I_CRIT
| HWMON_I_LCRIT
|
1003 HWMON_I_CRIT_ALARM
| HWMON_I_LCRIT_ALARM
,
1007 static const struct hwmon_channel_info sfp_hwmon_vcc_channel_info
= {
1009 .config
= sfp_hwmon_vcc_config
,
1012 static u32 sfp_hwmon_bias_config
[] = {
1014 HWMON_C_MAX
| HWMON_C_MIN
|
1015 HWMON_C_MAX_ALARM
| HWMON_C_MIN_ALARM
|
1016 HWMON_C_CRIT
| HWMON_C_LCRIT
|
1017 HWMON_C_CRIT_ALARM
| HWMON_C_LCRIT_ALARM
,
1021 static const struct hwmon_channel_info sfp_hwmon_bias_channel_info
= {
1023 .config
= sfp_hwmon_bias_config
,
1026 static u32 sfp_hwmon_power_config
[] = {
1027 /* Transmit power */
1029 HWMON_P_MAX
| HWMON_P_MIN
|
1030 HWMON_P_MAX_ALARM
| HWMON_P_MIN_ALARM
|
1031 HWMON_P_CRIT
| HWMON_P_LCRIT
|
1032 HWMON_P_CRIT_ALARM
| HWMON_P_LCRIT_ALARM
,
1035 HWMON_P_MAX
| HWMON_P_MIN
|
1036 HWMON_P_MAX_ALARM
| HWMON_P_MIN_ALARM
|
1037 HWMON_P_CRIT
| HWMON_P_LCRIT
|
1038 HWMON_P_CRIT_ALARM
| HWMON_P_LCRIT_ALARM
,
1042 static const struct hwmon_channel_info sfp_hwmon_power_channel_info
= {
1043 .type
= hwmon_power
,
1044 .config
= sfp_hwmon_power_config
,
1047 static const struct hwmon_channel_info
*sfp_hwmon_info
[] = {
1049 &sfp_hwmon_vcc_channel_info
,
1050 &sfp_hwmon_temp_channel_info
,
1051 &sfp_hwmon_bias_channel_info
,
1052 &sfp_hwmon_power_channel_info
,
1056 static const struct hwmon_chip_info sfp_hwmon_chip_info
= {
1057 .ops
= &sfp_hwmon_ops
,
1058 .info
= sfp_hwmon_info
,
1061 static int sfp_hwmon_insert(struct sfp
*sfp
)
1065 if (sfp
->id
.ext
.sff8472_compliance
== SFP_SFF8472_COMPLIANCE_NONE
)
1068 if (!(sfp
->id
.ext
.diagmon
& SFP_DIAGMON_DDM
))
1071 if (sfp
->id
.ext
.diagmon
& SFP_DIAGMON_ADDRMODE
)
1072 /* This driver in general does not support address
1077 err
= sfp_read(sfp
, true, 0, &sfp
->diag
, sizeof(sfp
->diag
));
1081 sfp
->hwmon_name
= kstrdup(dev_name(sfp
->dev
), GFP_KERNEL
);
1082 if (!sfp
->hwmon_name
)
1085 for (i
= 0; sfp
->hwmon_name
[i
]; i
++)
1086 if (hwmon_is_bad_char(sfp
->hwmon_name
[i
]))
1087 sfp
->hwmon_name
[i
] = '_';
1089 sfp
->hwmon_dev
= hwmon_device_register_with_info(sfp
->dev
,
1090 sfp
->hwmon_name
, sfp
,
1091 &sfp_hwmon_chip_info
,
1094 return PTR_ERR_OR_ZERO(sfp
->hwmon_dev
);
1097 static void sfp_hwmon_remove(struct sfp
*sfp
)
1099 if (!IS_ERR_OR_NULL(sfp
->hwmon_dev
)) {
1100 hwmon_device_unregister(sfp
->hwmon_dev
);
1101 sfp
->hwmon_dev
= NULL
;
1102 kfree(sfp
->hwmon_name
);
1106 static int sfp_hwmon_insert(struct sfp
*sfp
)
1111 static void sfp_hwmon_remove(struct sfp
*sfp
)
1117 static void sfp_module_tx_disable(struct sfp
*sfp
)
1119 dev_dbg(sfp
->dev
, "tx disable %u -> %u\n",
1120 sfp
->state
& SFP_F_TX_DISABLE
? 1 : 0, 1);
1121 sfp
->state
|= SFP_F_TX_DISABLE
;
1122 sfp_set_state(sfp
, sfp
->state
);
1125 static void sfp_module_tx_enable(struct sfp
*sfp
)
1127 dev_dbg(sfp
->dev
, "tx disable %u -> %u\n",
1128 sfp
->state
& SFP_F_TX_DISABLE
? 1 : 0, 0);
1129 sfp
->state
&= ~SFP_F_TX_DISABLE
;
1130 sfp_set_state(sfp
, sfp
->state
);
1133 static void sfp_module_tx_fault_reset(struct sfp
*sfp
)
1135 unsigned int state
= sfp
->state
;
1137 if (state
& SFP_F_TX_DISABLE
)
1140 sfp_set_state(sfp
, state
| SFP_F_TX_DISABLE
);
1144 sfp_set_state(sfp
, state
);
1147 /* SFP state machine */
1148 static void sfp_sm_set_timer(struct sfp
*sfp
, unsigned int timeout
)
1151 mod_delayed_work(system_power_efficient_wq
, &sfp
->timeout
,
1154 cancel_delayed_work(&sfp
->timeout
);
1157 static void sfp_sm_next(struct sfp
*sfp
, unsigned int state
,
1158 unsigned int timeout
)
1160 sfp
->sm_state
= state
;
1161 sfp_sm_set_timer(sfp
, timeout
);
1164 static void sfp_sm_ins_next(struct sfp
*sfp
, unsigned int state
,
1165 unsigned int timeout
)
1167 sfp
->sm_mod_state
= state
;
1168 sfp_sm_set_timer(sfp
, timeout
);
1171 static void sfp_sm_phy_detach(struct sfp
*sfp
)
1173 phy_stop(sfp
->mod_phy
);
1174 sfp_remove_phy(sfp
->sfp_bus
);
1175 phy_device_remove(sfp
->mod_phy
);
1176 phy_device_free(sfp
->mod_phy
);
1177 sfp
->mod_phy
= NULL
;
1180 static void sfp_sm_probe_phy(struct sfp
*sfp
)
1182 struct phy_device
*phy
;
1185 msleep(T_PHY_RESET_MS
);
1187 phy
= mdiobus_scan(sfp
->i2c_mii
, SFP_PHY_ADDR
);
1188 if (phy
== ERR_PTR(-ENODEV
)) {
1189 dev_info(sfp
->dev
, "no PHY detected\n");
1193 dev_err(sfp
->dev
, "mdiobus scan returned %ld\n", PTR_ERR(phy
));
1197 err
= sfp_add_phy(sfp
->sfp_bus
, phy
);
1199 phy_device_remove(phy
);
1200 phy_device_free(phy
);
1201 dev_err(sfp
->dev
, "sfp_add_phy failed: %d\n", err
);
1209 static void sfp_sm_link_up(struct sfp
*sfp
)
1211 sfp_link_up(sfp
->sfp_bus
);
1212 sfp_sm_next(sfp
, SFP_S_LINK_UP
, 0);
1215 static void sfp_sm_link_down(struct sfp
*sfp
)
1217 sfp_link_down(sfp
->sfp_bus
);
1220 static void sfp_sm_link_check_los(struct sfp
*sfp
)
1222 unsigned int los
= sfp
->state
& SFP_F_LOS
;
1224 /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL
1225 * are set, we assume that no LOS signal is available.
1227 if (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_INVERTED
))
1229 else if (!(sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_NORMAL
)))
1233 sfp_sm_next(sfp
, SFP_S_WAIT_LOS
, 0);
1235 sfp_sm_link_up(sfp
);
1238 static bool sfp_los_event_active(struct sfp
*sfp
, unsigned int event
)
1240 return (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_INVERTED
) &&
1241 event
== SFP_E_LOS_LOW
) ||
1242 (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_NORMAL
) &&
1243 event
== SFP_E_LOS_HIGH
);
1246 static bool sfp_los_event_inactive(struct sfp
*sfp
, unsigned int event
)
1248 return (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_INVERTED
) &&
1249 event
== SFP_E_LOS_HIGH
) ||
1250 (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_NORMAL
) &&
1251 event
== SFP_E_LOS_LOW
);
1254 static void sfp_sm_fault(struct sfp
*sfp
, bool warn
)
1256 if (sfp
->sm_retries
&& !--sfp
->sm_retries
) {
1258 "module persistently indicates fault, disabling\n");
1259 sfp_sm_next(sfp
, SFP_S_TX_DISABLE
, 0);
1262 dev_err(sfp
->dev
, "module transmit fault indicated\n");
1264 sfp_sm_next(sfp
, SFP_S_TX_FAULT
, T_FAULT_RECOVER
);
1268 static void sfp_sm_mod_init(struct sfp
*sfp
)
1270 sfp_module_tx_enable(sfp
);
1272 /* Wait t_init before indicating that the link is up, provided the
1273 * current state indicates no TX_FAULT. If TX_FAULT clears before
1274 * this time, that's fine too.
1276 sfp_sm_next(sfp
, SFP_S_INIT
, T_INIT_JIFFIES
);
1277 sfp
->sm_retries
= 5;
1279 /* Setting the serdes link mode is guesswork: there's no
1280 * field in the EEPROM which indicates what mode should
1283 * If it's a gigabit-only fiber module, it probably does
1284 * not have a PHY, so switch to 802.3z negotiation mode.
1285 * Otherwise, switch to SGMII mode (which is required to
1286 * support non-gigabit speeds) and probe for a PHY.
1288 if (sfp
->id
.base
.e1000_base_t
||
1289 sfp
->id
.base
.e100_base_lx
||
1290 sfp
->id
.base
.e100_base_fx
)
1291 sfp_sm_probe_phy(sfp
);
1294 static int sfp_sm_mod_hpower(struct sfp
*sfp
)
1301 if (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_POWER_DECL
))
1303 if (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL
))
1306 if (sfp
->id
.ext
.sff8472_compliance
== SFP_SFF8472_COMPLIANCE_NONE
&&
1307 (sfp
->id
.ext
.diagmon
& (SFP_DIAGMON_DDM
| SFP_DIAGMON_ADDRMODE
)) !=
1309 /* The module appears not to implement bus address 0xa2,
1310 * or requires an address change sequence, so assume that
1311 * the module powers up in the indicated power mode.
1313 if (power
> sfp
->max_power_mW
) {
1315 "Host does not support %u.%uW modules\n",
1316 power
/ 1000, (power
/ 100) % 10);
1322 if (power
> sfp
->max_power_mW
) {
1324 "Host does not support %u.%uW modules, module left in power mode 1\n",
1325 power
/ 1000, (power
/ 100) % 10);
1332 err
= sfp_read(sfp
, true, SFP_EXT_STATUS
, &val
, sizeof(val
));
1333 if (err
!= sizeof(val
)) {
1334 dev_err(sfp
->dev
, "Failed to read EEPROM: %d\n", err
);
1341 err
= sfp_write(sfp
, true, SFP_EXT_STATUS
, &val
, sizeof(val
));
1342 if (err
!= sizeof(val
)) {
1343 dev_err(sfp
->dev
, "Failed to write EEPROM: %d\n", err
);
1348 dev_info(sfp
->dev
, "Module switched to %u.%uW power level\n",
1349 power
/ 1000, (power
/ 100) % 10);
1350 return T_HPOWER_LEVEL
;
1356 static int sfp_sm_mod_probe(struct sfp
*sfp
)
1358 /* SFP module inserted - read I2C data */
1359 struct sfp_eeprom_id id
;
1364 ret
= sfp_read(sfp
, false, 0, &id
, sizeof(id
));
1366 dev_err(sfp
->dev
, "failed to read EEPROM: %d\n", ret
);
1370 if (ret
!= sizeof(id
)) {
1371 dev_err(sfp
->dev
, "EEPROM short read: %d\n", ret
);
1375 /* Cotsworks do not seem to update the checksums when they
1376 * do the final programming with the final module part number,
1377 * serial number and date code.
1379 cotsworks
= !memcmp(id
.base
.vendor_name
, "COTSWORKS ", 16);
1381 /* Validate the checksum over the base structure */
1382 check
= sfp_check(&id
.base
, sizeof(id
.base
) - 1);
1383 if (check
!= id
.base
.cc_base
) {
1386 "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n",
1387 check
, id
.base
.cc_base
);
1390 "EEPROM base structure checksum failure: 0x%02x != 0x%02x\n",
1391 check
, id
.base
.cc_base
);
1392 print_hex_dump(KERN_ERR
, "sfp EE: ", DUMP_PREFIX_OFFSET
,
1393 16, 1, &id
, sizeof(id
), true);
1398 check
= sfp_check(&id
.ext
, sizeof(id
.ext
) - 1);
1399 if (check
!= id
.ext
.cc_ext
) {
1402 "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n",
1403 check
, id
.ext
.cc_ext
);
1406 "EEPROM extended structure checksum failure: 0x%02x != 0x%02x\n",
1407 check
, id
.ext
.cc_ext
);
1408 print_hex_dump(KERN_ERR
, "sfp EE: ", DUMP_PREFIX_OFFSET
,
1409 16, 1, &id
, sizeof(id
), true);
1410 memset(&id
.ext
, 0, sizeof(id
.ext
));
1416 dev_info(sfp
->dev
, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n",
1417 (int)sizeof(id
.base
.vendor_name
), id
.base
.vendor_name
,
1418 (int)sizeof(id
.base
.vendor_pn
), id
.base
.vendor_pn
,
1419 (int)sizeof(id
.base
.vendor_rev
), id
.base
.vendor_rev
,
1420 (int)sizeof(id
.ext
.vendor_sn
), id
.ext
.vendor_sn
,
1421 (int)sizeof(id
.ext
.datecode
), id
.ext
.datecode
);
1423 /* Check whether we support this module */
1424 if (!sfp
->type
->module_supported(&sfp
->id
)) {
1426 "module is not supported - phys id 0x%02x 0x%02x\n",
1427 sfp
->id
.base
.phys_id
, sfp
->id
.base
.phys_ext_id
);
1431 /* If the module requires address swap mode, warn about it */
1432 if (sfp
->id
.ext
.diagmon
& SFP_DIAGMON_ADDRMODE
)
1434 "module address swap to access page 0xA2 is not supported.\n");
1436 ret
= sfp_hwmon_insert(sfp
);
1440 ret
= sfp_module_insert(sfp
->sfp_bus
, &sfp
->id
);
1444 return sfp_sm_mod_hpower(sfp
);
1447 static void sfp_sm_mod_remove(struct sfp
*sfp
)
1449 sfp_module_remove(sfp
->sfp_bus
);
1451 sfp_hwmon_remove(sfp
);
1454 sfp_sm_phy_detach(sfp
);
1456 sfp_module_tx_disable(sfp
);
1458 memset(&sfp
->id
, 0, sizeof(sfp
->id
));
1460 dev_info(sfp
->dev
, "module removed\n");
1463 static void sfp_sm_event(struct sfp
*sfp
, unsigned int event
)
1465 mutex_lock(&sfp
->sm_mutex
);
1467 dev_dbg(sfp
->dev
, "SM: enter %s:%s:%s event %s\n",
1468 mod_state_to_str(sfp
->sm_mod_state
),
1469 dev_state_to_str(sfp
->sm_dev_state
),
1470 sm_state_to_str(sfp
->sm_state
),
1471 event_to_str(event
));
1473 /* This state machine tracks the insert/remove state of
1474 * the module, and handles probing the on-board EEPROM.
1476 switch (sfp
->sm_mod_state
) {
1478 if (event
== SFP_E_INSERT
) {
1479 sfp_module_tx_disable(sfp
);
1480 sfp_sm_ins_next(sfp
, SFP_MOD_PROBE
, T_PROBE_INIT
);
1485 if (event
== SFP_E_REMOVE
) {
1486 sfp_sm_ins_next(sfp
, SFP_MOD_EMPTY
, 0);
1487 } else if (event
== SFP_E_TIMEOUT
) {
1488 int val
= sfp_sm_mod_probe(sfp
);
1491 sfp_sm_ins_next(sfp
, SFP_MOD_PRESENT
, 0);
1493 sfp_sm_ins_next(sfp
, SFP_MOD_HPOWER
, val
);
1494 else if (val
!= -EAGAIN
)
1495 sfp_sm_ins_next(sfp
, SFP_MOD_ERROR
, 0);
1497 sfp_sm_set_timer(sfp
, T_PROBE_RETRY
);
1501 case SFP_MOD_HPOWER
:
1502 if (event
== SFP_E_TIMEOUT
) {
1503 sfp_sm_ins_next(sfp
, SFP_MOD_PRESENT
, 0);
1507 case SFP_MOD_PRESENT
:
1509 if (event
== SFP_E_REMOVE
) {
1510 sfp_sm_mod_remove(sfp
);
1511 sfp_sm_ins_next(sfp
, SFP_MOD_EMPTY
, 0);
1516 /* This state machine tracks the netdev up/down state */
1517 switch (sfp
->sm_dev_state
) {
1519 if (event
== SFP_E_DEV_UP
)
1520 sfp
->sm_dev_state
= SFP_DEV_UP
;
1524 if (event
== SFP_E_DEV_DOWN
) {
1525 /* If the module has a PHY, avoid raising TX disable
1526 * as this resets the PHY. Otherwise, raise it to
1527 * turn the laser off.
1530 sfp_module_tx_disable(sfp
);
1531 sfp
->sm_dev_state
= SFP_DEV_DOWN
;
1536 /* Some events are global */
1537 if (sfp
->sm_state
!= SFP_S_DOWN
&&
1538 (sfp
->sm_mod_state
!= SFP_MOD_PRESENT
||
1539 sfp
->sm_dev_state
!= SFP_DEV_UP
)) {
1540 if (sfp
->sm_state
== SFP_S_LINK_UP
&&
1541 sfp
->sm_dev_state
== SFP_DEV_UP
)
1542 sfp_sm_link_down(sfp
);
1544 sfp_sm_phy_detach(sfp
);
1545 sfp_sm_next(sfp
, SFP_S_DOWN
, 0);
1546 mutex_unlock(&sfp
->sm_mutex
);
1550 /* The main state machine */
1551 switch (sfp
->sm_state
) {
1553 if (sfp
->sm_mod_state
== SFP_MOD_PRESENT
&&
1554 sfp
->sm_dev_state
== SFP_DEV_UP
)
1555 sfp_sm_mod_init(sfp
);
1559 if (event
== SFP_E_TIMEOUT
&& sfp
->state
& SFP_F_TX_FAULT
)
1560 sfp_sm_fault(sfp
, true);
1561 else if (event
== SFP_E_TIMEOUT
|| event
== SFP_E_TX_CLEAR
)
1562 sfp_sm_link_check_los(sfp
);
1565 case SFP_S_WAIT_LOS
:
1566 if (event
== SFP_E_TX_FAULT
)
1567 sfp_sm_fault(sfp
, true);
1568 else if (sfp_los_event_inactive(sfp
, event
))
1569 sfp_sm_link_up(sfp
);
1573 if (event
== SFP_E_TX_FAULT
) {
1574 sfp_sm_link_down(sfp
);
1575 sfp_sm_fault(sfp
, true);
1576 } else if (sfp_los_event_active(sfp
, event
)) {
1577 sfp_sm_link_down(sfp
);
1578 sfp_sm_next(sfp
, SFP_S_WAIT_LOS
, 0);
1582 case SFP_S_TX_FAULT
:
1583 if (event
== SFP_E_TIMEOUT
) {
1584 sfp_module_tx_fault_reset(sfp
);
1585 sfp_sm_next(sfp
, SFP_S_REINIT
, T_INIT_JIFFIES
);
1590 if (event
== SFP_E_TIMEOUT
&& sfp
->state
& SFP_F_TX_FAULT
) {
1591 sfp_sm_fault(sfp
, false);
1592 } else if (event
== SFP_E_TIMEOUT
|| event
== SFP_E_TX_CLEAR
) {
1593 dev_info(sfp
->dev
, "module transmit fault recovered\n");
1594 sfp_sm_link_check_los(sfp
);
1598 case SFP_S_TX_DISABLE
:
1602 dev_dbg(sfp
->dev
, "SM: exit %s:%s:%s\n",
1603 mod_state_to_str(sfp
->sm_mod_state
),
1604 dev_state_to_str(sfp
->sm_dev_state
),
1605 sm_state_to_str(sfp
->sm_state
));
1607 mutex_unlock(&sfp
->sm_mutex
);
1610 static void sfp_start(struct sfp
*sfp
)
1612 sfp_sm_event(sfp
, SFP_E_DEV_UP
);
1615 static void sfp_stop(struct sfp
*sfp
)
1617 sfp_sm_event(sfp
, SFP_E_DEV_DOWN
);
1620 static int sfp_module_info(struct sfp
*sfp
, struct ethtool_modinfo
*modinfo
)
1622 /* locking... and check module is present */
1624 if (sfp
->id
.ext
.sff8472_compliance
&&
1625 !(sfp
->id
.ext
.diagmon
& SFP_DIAGMON_ADDRMODE
)) {
1626 modinfo
->type
= ETH_MODULE_SFF_8472
;
1627 modinfo
->eeprom_len
= ETH_MODULE_SFF_8472_LEN
;
1629 modinfo
->type
= ETH_MODULE_SFF_8079
;
1630 modinfo
->eeprom_len
= ETH_MODULE_SFF_8079_LEN
;
1635 static int sfp_module_eeprom(struct sfp
*sfp
, struct ethtool_eeprom
*ee
,
1638 unsigned int first
, last
, len
;
1645 last
= ee
->offset
+ ee
->len
;
1646 if (first
< ETH_MODULE_SFF_8079_LEN
) {
1647 len
= min_t(unsigned int, last
, ETH_MODULE_SFF_8079_LEN
);
1650 ret
= sfp_read(sfp
, false, first
, data
, len
);
1657 if (first
< ETH_MODULE_SFF_8472_LEN
&& last
> ETH_MODULE_SFF_8079_LEN
) {
1658 len
= min_t(unsigned int, last
, ETH_MODULE_SFF_8472_LEN
);
1660 first
-= ETH_MODULE_SFF_8079_LEN
;
1662 ret
= sfp_read(sfp
, true, first
, data
, len
);
1669 static const struct sfp_socket_ops sfp_module_ops
= {
1672 .module_info
= sfp_module_info
,
1673 .module_eeprom
= sfp_module_eeprom
,
1676 static void sfp_timeout(struct work_struct
*work
)
1678 struct sfp
*sfp
= container_of(work
, struct sfp
, timeout
.work
);
1681 sfp_sm_event(sfp
, SFP_E_TIMEOUT
);
1685 static void sfp_check_state(struct sfp
*sfp
)
1687 unsigned int state
, i
, changed
;
1689 state
= sfp_get_state(sfp
);
1690 changed
= state
^ sfp
->state
;
1691 changed
&= SFP_F_PRESENT
| SFP_F_LOS
| SFP_F_TX_FAULT
;
1693 for (i
= 0; i
< GPIO_MAX
; i
++)
1694 if (changed
& BIT(i
))
1695 dev_dbg(sfp
->dev
, "%s %u -> %u\n", gpio_of_names
[i
],
1696 !!(sfp
->state
& BIT(i
)), !!(state
& BIT(i
)));
1698 state
|= sfp
->state
& (SFP_F_TX_DISABLE
| SFP_F_RATE_SELECT
);
1702 if (changed
& SFP_F_PRESENT
)
1703 sfp_sm_event(sfp
, state
& SFP_F_PRESENT
?
1704 SFP_E_INSERT
: SFP_E_REMOVE
);
1706 if (changed
& SFP_F_TX_FAULT
)
1707 sfp_sm_event(sfp
, state
& SFP_F_TX_FAULT
?
1708 SFP_E_TX_FAULT
: SFP_E_TX_CLEAR
);
1710 if (changed
& SFP_F_LOS
)
1711 sfp_sm_event(sfp
, state
& SFP_F_LOS
?
1712 SFP_E_LOS_HIGH
: SFP_E_LOS_LOW
);
1716 static irqreturn_t
sfp_irq(int irq
, void *data
)
1718 struct sfp
*sfp
= data
;
1720 sfp_check_state(sfp
);
1725 static void sfp_poll(struct work_struct
*work
)
1727 struct sfp
*sfp
= container_of(work
, struct sfp
, poll
.work
);
1729 sfp_check_state(sfp
);
1730 mod_delayed_work(system_wq
, &sfp
->poll
, poll_jiffies
);
1733 static struct sfp
*sfp_alloc(struct device
*dev
)
1737 sfp
= kzalloc(sizeof(*sfp
), GFP_KERNEL
);
1739 return ERR_PTR(-ENOMEM
);
1743 mutex_init(&sfp
->sm_mutex
);
1744 INIT_DELAYED_WORK(&sfp
->poll
, sfp_poll
);
1745 INIT_DELAYED_WORK(&sfp
->timeout
, sfp_timeout
);
1750 static void sfp_cleanup(void *data
)
1752 struct sfp
*sfp
= data
;
1754 cancel_delayed_work_sync(&sfp
->poll
);
1755 cancel_delayed_work_sync(&sfp
->timeout
);
1757 mdiobus_unregister(sfp
->i2c_mii
);
1758 mdiobus_free(sfp
->i2c_mii
);
1761 i2c_put_adapter(sfp
->i2c
);
1765 static int sfp_probe(struct platform_device
*pdev
)
1767 const struct sff_data
*sff
;
1772 sfp
= sfp_alloc(&pdev
->dev
);
1774 return PTR_ERR(sfp
);
1776 platform_set_drvdata(pdev
, sfp
);
1778 err
= devm_add_action(sfp
->dev
, sfp_cleanup
, sfp
);
1782 sff
= sfp
->type
= &sfp_data
;
1784 if (pdev
->dev
.of_node
) {
1785 struct device_node
*node
= pdev
->dev
.of_node
;
1786 const struct of_device_id
*id
;
1787 struct i2c_adapter
*i2c
;
1788 struct device_node
*np
;
1790 id
= of_match_node(sfp_of_match
, node
);
1794 sff
= sfp
->type
= id
->data
;
1796 np
= of_parse_phandle(node
, "i2c-bus", 0);
1798 dev_err(sfp
->dev
, "missing 'i2c-bus' property\n");
1802 i2c
= of_find_i2c_adapter_by_node(np
);
1805 return -EPROBE_DEFER
;
1807 err
= sfp_i2c_configure(sfp
, i2c
);
1809 i2c_put_adapter(i2c
);
1814 for (i
= 0; i
< GPIO_MAX
; i
++)
1815 if (sff
->gpios
& BIT(i
)) {
1816 sfp
->gpio
[i
] = devm_gpiod_get_optional(sfp
->dev
,
1817 gpio_of_names
[i
], gpio_flags
[i
]);
1818 if (IS_ERR(sfp
->gpio
[i
]))
1819 return PTR_ERR(sfp
->gpio
[i
]);
1822 sfp
->get_state
= sfp_gpio_get_state
;
1823 sfp
->set_state
= sfp_gpio_set_state
;
1825 /* Modules that have no detect signal are always present */
1826 if (!(sfp
->gpio
[GPIO_MODDEF0
]))
1827 sfp
->get_state
= sff_gpio_get_state
;
1829 device_property_read_u32(&pdev
->dev
, "maximum-power-milliwatt",
1830 &sfp
->max_power_mW
);
1831 if (!sfp
->max_power_mW
)
1832 sfp
->max_power_mW
= 1000;
1834 dev_info(sfp
->dev
, "Host maximum power %u.%uW\n",
1835 sfp
->max_power_mW
/ 1000, (sfp
->max_power_mW
/ 100) % 10);
1837 sfp
->sfp_bus
= sfp_register_socket(sfp
->dev
, sfp
, &sfp_module_ops
);
1841 /* Get the initial state, and always signal TX disable,
1842 * since the network interface will not be up.
1844 sfp
->state
= sfp_get_state(sfp
) | SFP_F_TX_DISABLE
;
1846 if (sfp
->gpio
[GPIO_RATE_SELECT
] &&
1847 gpiod_get_value_cansleep(sfp
->gpio
[GPIO_RATE_SELECT
]))
1848 sfp
->state
|= SFP_F_RATE_SELECT
;
1849 sfp_set_state(sfp
, sfp
->state
);
1850 sfp_module_tx_disable(sfp
);
1852 if (sfp
->state
& SFP_F_PRESENT
)
1853 sfp_sm_event(sfp
, SFP_E_INSERT
);
1856 for (i
= 0; i
< GPIO_MAX
; i
++) {
1857 if (gpio_flags
[i
] != GPIOD_IN
|| !sfp
->gpio
[i
])
1860 irq
= gpiod_to_irq(sfp
->gpio
[i
]);
1866 err
= devm_request_threaded_irq(sfp
->dev
, irq
, NULL
, sfp_irq
,
1868 IRQF_TRIGGER_RISING
|
1869 IRQF_TRIGGER_FALLING
,
1870 dev_name(sfp
->dev
), sfp
);
1876 mod_delayed_work(system_wq
, &sfp
->poll
, poll_jiffies
);
1878 /* We could have an issue in cases no Tx disable pin is available or
1879 * wired as modules using a laser as their light source will continue to
1880 * be active when the fiber is removed. This could be a safety issue and
1881 * we should at least warn the user about that.
1883 if (!sfp
->gpio
[GPIO_TX_DISABLE
])
1885 "No tx_disable pin: SFP modules will always be emitting.\n");
1890 static int sfp_remove(struct platform_device
*pdev
)
1892 struct sfp
*sfp
= platform_get_drvdata(pdev
);
1894 sfp_unregister_socket(sfp
->sfp_bus
);
1899 static struct platform_driver sfp_driver
= {
1901 .remove
= sfp_remove
,
1904 .of_match_table
= sfp_of_match
,
1908 static int sfp_init(void)
1910 poll_jiffies
= msecs_to_jiffies(100);
1912 return platform_driver_register(&sfp_driver
);
1914 module_init(sfp_init
);
1916 static void sfp_exit(void)
1918 platform_driver_unregister(&sfp_driver
);
1920 module_exit(sfp_exit
);
1922 MODULE_ALIAS("platform:sfp");
1923 MODULE_AUTHOR("Russell King");
1924 MODULE_LICENSE("GPL v2");