net: phy: sfp: Fix unregistering of HWMON SFP device
[linux-2.6/btrfs-unstable.git] / drivers / net / phy / sfp.c
blob6e13b8832bc7df94467211f07c1e7dba15a6e877
1 #include <linux/ctype.h>
2 #include <linux/delay.h>
3 #include <linux/gpio/consumer.h>
4 #include <linux/hwmon.h>
5 #include <linux/i2c.h>
6 #include <linux/interrupt.h>
7 #include <linux/jiffies.h>
8 #include <linux/module.h>
9 #include <linux/mutex.h>
10 #include <linux/of.h>
11 #include <linux/phy.h>
12 #include <linux/platform_device.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/slab.h>
15 #include <linux/workqueue.h>
17 #include "mdio-i2c.h"
18 #include "sfp.h"
19 #include "swphy.h"
21 enum {
22 GPIO_MODDEF0,
23 GPIO_LOS,
24 GPIO_TX_FAULT,
25 GPIO_TX_DISABLE,
26 GPIO_RATE_SELECT,
27 GPIO_MAX,
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),
35 SFP_E_INSERT = 0,
36 SFP_E_REMOVE,
37 SFP_E_DEV_DOWN,
38 SFP_E_DEV_UP,
39 SFP_E_TX_FAULT,
40 SFP_E_TX_CLEAR,
41 SFP_E_LOS_HIGH,
42 SFP_E_LOS_LOW,
43 SFP_E_TIMEOUT,
45 SFP_MOD_EMPTY = 0,
46 SFP_MOD_PROBE,
47 SFP_MOD_HPOWER,
48 SFP_MOD_PRESENT,
49 SFP_MOD_ERROR,
51 SFP_DEV_DOWN = 0,
52 SFP_DEV_UP,
54 SFP_S_DOWN = 0,
55 SFP_S_INIT,
56 SFP_S_WAIT_LOS,
57 SFP_S_LINK_UP,
58 SFP_S_TX_FAULT,
59 SFP_S_REINIT,
60 SFP_S_TX_DISABLE,
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",
80 [SFP_DEV_UP] = "up",
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[] = {
127 "mod-def0",
128 "los",
129 "tx-fault",
130 "tx-disable",
131 "rate-select0",
134 static const enum gpiod_flags gpio_flags[] = {
135 GPIOD_IN,
136 GPIOD_IN,
137 GPIOD_IN,
138 GPIOD_ASIS,
139 GPIOD_ASIS,
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);
168 struct sff_data {
169 unsigned int gpios;
170 bool (*module_supported)(const struct sfp_eeprom_id *id);
173 struct sfp {
174 struct device *dev;
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;
180 u32 max_power_mW;
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];
189 unsigned int state;
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;
202 char *hwmon_name;
203 #endif
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, },
233 { },
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])
245 continue;
247 v = gpiod_get_value_cansleep(sfp->gpio[i]);
248 if (v)
249 state |= BIT(i);
252 return state;
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);
270 } else {
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,
280 size_t len)
282 struct i2c_msg msgs[2];
283 u8 bus_addr = a2 ? 0x51 : 0x50;
284 int ret;
286 msgs[0].addr = bus_addr;
287 msgs[0].flags = 0;
288 msgs[0].len = 1;
289 msgs[0].buf = &dev_addr;
290 msgs[1].addr = bus_addr;
291 msgs[1].flags = I2C_M_RD;
292 msgs[1].len = len;
293 msgs[1].buf = buf;
295 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
296 if (ret < 0)
297 return ret;
299 return ret == ARRAY_SIZE(msgs) ? len : 0;
302 static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
303 size_t len)
305 struct i2c_msg msgs[1];
306 u8 bus_addr = a2 ? 0x51 : 0x50;
307 int ret;
309 msgs[0].addr = bus_addr;
310 msgs[0].flags = 0;
311 msgs[0].len = 1 + len;
312 msgs[0].buf = kmalloc(1 + len, GFP_KERNEL);
313 if (!msgs[0].buf)
314 return -ENOMEM;
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));
321 kfree(msgs[0].buf);
323 if (ret < 0)
324 return ret;
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;
332 int ret;
334 if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
335 return -EINVAL;
337 sfp->i2c = i2c;
338 sfp->read = sfp_i2c_read;
339 sfp->write = sfp_i2c_write;
341 i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
342 if (IS_ERR(i2c_mii))
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);
349 if (ret < 0) {
350 mdiobus_free(i2c_mii);
351 return ret;
354 sfp->i2c_mii = i2c_mii;
356 return 0;
359 /* Interface */
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)
382 u8 *p, check;
384 for (p = buf, check = 0; len; p++, len--)
385 check += *p;
387 return check;
390 /* hwmon */
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;
398 switch (type) {
399 case hwmon_temp:
400 switch (attr) {
401 case hwmon_temp_min_alarm:
402 case hwmon_temp_max_alarm:
403 case hwmon_temp_lcrit_alarm:
404 case hwmon_temp_crit_alarm:
405 case hwmon_temp_min:
406 case hwmon_temp_max:
407 case hwmon_temp_lcrit:
408 case hwmon_temp_crit:
409 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
410 return 0;
411 /* fall through */
412 case hwmon_temp_input:
413 return 0444;
414 default:
415 return 0;
417 case hwmon_in:
418 switch (attr) {
419 case hwmon_in_min_alarm:
420 case hwmon_in_max_alarm:
421 case hwmon_in_lcrit_alarm:
422 case hwmon_in_crit_alarm:
423 case hwmon_in_min:
424 case hwmon_in_max:
425 case hwmon_in_lcrit:
426 case hwmon_in_crit:
427 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
428 return 0;
429 /* fall through */
430 case hwmon_in_input:
431 return 0444;
432 default:
433 return 0;
435 case hwmon_curr:
436 switch (attr) {
437 case hwmon_curr_min_alarm:
438 case hwmon_curr_max_alarm:
439 case hwmon_curr_lcrit_alarm:
440 case hwmon_curr_crit_alarm:
441 case hwmon_curr_min:
442 case hwmon_curr_max:
443 case hwmon_curr_lcrit:
444 case hwmon_curr_crit:
445 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
446 return 0;
447 /* fall through */
448 case hwmon_curr_input:
449 return 0444;
450 default:
451 return 0;
453 case hwmon_power:
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 &&
461 channel == 1)
462 return 0;
463 switch (attr) {
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))
473 return 0;
474 /* fall through */
475 case hwmon_power_input:
476 return 0444;
477 default:
478 return 0;
480 default:
481 return 0;
485 static int sfp_hwmon_read_sensor(struct sfp *sfp, int reg, long *value)
487 __be16 val;
488 int err;
490 err = sfp_read(sfp, true, reg, &val, sizeof(val));
491 if (err < 0)
492 return err;
494 *value = be16_to_cpu(val);
496 return 0;
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,
505 long *value)
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)
517 *value -= 0x10000;
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)
548 int err;
550 err = sfp_hwmon_read_sensor(sfp, reg, value);
551 if (err < 0)
552 return err;
554 sfp_hwmon_calibrate_temp(sfp, value);
556 return 0;
559 static int sfp_hwmon_read_vcc(struct sfp *sfp, int reg, long *value)
561 int err;
563 err = sfp_hwmon_read_sensor(sfp, reg, value);
564 if (err < 0)
565 return err;
567 sfp_hwmon_calibrate_vcc(sfp, value);
569 return 0;
572 static int sfp_hwmon_read_bias(struct sfp *sfp, int reg, long *value)
574 int err;
576 err = sfp_hwmon_read_sensor(sfp, reg, value);
577 if (err < 0)
578 return err;
580 sfp_hwmon_calibrate_bias(sfp, value);
582 return 0;
585 static int sfp_hwmon_read_tx_power(struct sfp *sfp, int reg, long *value)
587 int err;
589 err = sfp_hwmon_read_sensor(sfp, reg, value);
590 if (err < 0)
591 return err;
593 sfp_hwmon_calibrate_tx_power(sfp, value);
595 return 0;
598 static int sfp_hwmon_read_rx_power(struct sfp *sfp, int reg, long *value)
600 int err;
602 err = sfp_hwmon_read_sensor(sfp, reg, value);
603 if (err < 0)
604 return err;
606 sfp_hwmon_to_rx_power(value);
608 return 0;
611 static int sfp_hwmon_temp(struct sfp *sfp, u32 attr, long *value)
613 u8 status;
614 int err;
616 switch (attr) {
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);
623 return 0;
625 case hwmon_temp_min:
626 *value = be16_to_cpu(sfp->diag.temp_low_warn);
627 sfp_hwmon_calibrate_temp(sfp, value);
628 return 0;
629 case hwmon_temp_max:
630 *value = be16_to_cpu(sfp->diag.temp_high_warn);
631 sfp_hwmon_calibrate_temp(sfp, value);
632 return 0;
634 case hwmon_temp_crit:
635 *value = be16_to_cpu(sfp->diag.temp_high_alarm);
636 sfp_hwmon_calibrate_temp(sfp, value);
637 return 0;
639 case hwmon_temp_lcrit_alarm:
640 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
641 if (err < 0)
642 return err;
644 *value = !!(status & SFP_ALARM0_TEMP_LOW);
645 return 0;
647 case hwmon_temp_min_alarm:
648 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
649 if (err < 0)
650 return err;
652 *value = !!(status & SFP_WARN0_TEMP_LOW);
653 return 0;
655 case hwmon_temp_max_alarm:
656 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
657 if (err < 0)
658 return err;
660 *value = !!(status & SFP_WARN0_TEMP_HIGH);
661 return 0;
663 case hwmon_temp_crit_alarm:
664 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
665 if (err < 0)
666 return err;
668 *value = !!(status & SFP_ALARM0_TEMP_HIGH);
669 return 0;
670 default:
671 return -EOPNOTSUPP;
674 return -EOPNOTSUPP;
677 static int sfp_hwmon_vcc(struct sfp *sfp, u32 attr, long *value)
679 u8 status;
680 int err;
682 switch (attr) {
683 case hwmon_in_input:
684 return sfp_hwmon_read_vcc(sfp, SFP_VCC, value);
686 case hwmon_in_lcrit:
687 *value = be16_to_cpu(sfp->diag.volt_low_alarm);
688 sfp_hwmon_calibrate_vcc(sfp, value);
689 return 0;
691 case hwmon_in_min:
692 *value = be16_to_cpu(sfp->diag.volt_low_warn);
693 sfp_hwmon_calibrate_vcc(sfp, value);
694 return 0;
696 case hwmon_in_max:
697 *value = be16_to_cpu(sfp->diag.volt_high_warn);
698 sfp_hwmon_calibrate_vcc(sfp, value);
699 return 0;
701 case hwmon_in_crit:
702 *value = be16_to_cpu(sfp->diag.volt_high_alarm);
703 sfp_hwmon_calibrate_vcc(sfp, value);
704 return 0;
706 case hwmon_in_lcrit_alarm:
707 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
708 if (err < 0)
709 return err;
711 *value = !!(status & SFP_ALARM0_VCC_LOW);
712 return 0;
714 case hwmon_in_min_alarm:
715 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
716 if (err < 0)
717 return err;
719 *value = !!(status & SFP_WARN0_VCC_LOW);
720 return 0;
722 case hwmon_in_max_alarm:
723 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
724 if (err < 0)
725 return err;
727 *value = !!(status & SFP_WARN0_VCC_HIGH);
728 return 0;
730 case hwmon_in_crit_alarm:
731 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
732 if (err < 0)
733 return err;
735 *value = !!(status & SFP_ALARM0_VCC_HIGH);
736 return 0;
737 default:
738 return -EOPNOTSUPP;
741 return -EOPNOTSUPP;
744 static int sfp_hwmon_bias(struct sfp *sfp, u32 attr, long *value)
746 u8 status;
747 int err;
749 switch (attr) {
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);
756 return 0;
758 case hwmon_curr_min:
759 *value = be16_to_cpu(sfp->diag.bias_low_warn);
760 sfp_hwmon_calibrate_bias(sfp, value);
761 return 0;
763 case hwmon_curr_max:
764 *value = be16_to_cpu(sfp->diag.bias_high_warn);
765 sfp_hwmon_calibrate_bias(sfp, value);
766 return 0;
768 case hwmon_curr_crit:
769 *value = be16_to_cpu(sfp->diag.bias_high_alarm);
770 sfp_hwmon_calibrate_bias(sfp, value);
771 return 0;
773 case hwmon_curr_lcrit_alarm:
774 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
775 if (err < 0)
776 return err;
778 *value = !!(status & SFP_ALARM0_TX_BIAS_LOW);
779 return 0;
781 case hwmon_curr_min_alarm:
782 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
783 if (err < 0)
784 return err;
786 *value = !!(status & SFP_WARN0_TX_BIAS_LOW);
787 return 0;
789 case hwmon_curr_max_alarm:
790 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
791 if (err < 0)
792 return err;
794 *value = !!(status & SFP_WARN0_TX_BIAS_HIGH);
795 return 0;
797 case hwmon_curr_crit_alarm:
798 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
799 if (err < 0)
800 return err;
802 *value = !!(status & SFP_ALARM0_TX_BIAS_HIGH);
803 return 0;
804 default:
805 return -EOPNOTSUPP;
808 return -EOPNOTSUPP;
811 static int sfp_hwmon_tx_power(struct sfp *sfp, u32 attr, long *value)
813 u8 status;
814 int err;
816 switch (attr) {
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);
823 return 0;
825 case hwmon_power_min:
826 *value = be16_to_cpu(sfp->diag.txpwr_low_warn);
827 sfp_hwmon_calibrate_tx_power(sfp, value);
828 return 0;
830 case hwmon_power_max:
831 *value = be16_to_cpu(sfp->diag.txpwr_high_warn);
832 sfp_hwmon_calibrate_tx_power(sfp, value);
833 return 0;
835 case hwmon_power_crit:
836 *value = be16_to_cpu(sfp->diag.txpwr_high_alarm);
837 sfp_hwmon_calibrate_tx_power(sfp, value);
838 return 0;
840 case hwmon_power_lcrit_alarm:
841 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
842 if (err < 0)
843 return err;
845 *value = !!(status & SFP_ALARM0_TXPWR_LOW);
846 return 0;
848 case hwmon_power_min_alarm:
849 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
850 if (err < 0)
851 return err;
853 *value = !!(status & SFP_WARN0_TXPWR_LOW);
854 return 0;
856 case hwmon_power_max_alarm:
857 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
858 if (err < 0)
859 return err;
861 *value = !!(status & SFP_WARN0_TXPWR_HIGH);
862 return 0;
864 case hwmon_power_crit_alarm:
865 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
866 if (err < 0)
867 return err;
869 *value = !!(status & SFP_ALARM0_TXPWR_HIGH);
870 return 0;
871 default:
872 return -EOPNOTSUPP;
875 return -EOPNOTSUPP;
878 static int sfp_hwmon_rx_power(struct sfp *sfp, u32 attr, long *value)
880 u8 status;
881 int err;
883 switch (attr) {
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);
890 return 0;
892 case hwmon_power_min:
893 *value = be16_to_cpu(sfp->diag.rxpwr_low_warn);
894 sfp_hwmon_to_rx_power(value);
895 return 0;
897 case hwmon_power_max:
898 *value = be16_to_cpu(sfp->diag.rxpwr_high_warn);
899 sfp_hwmon_to_rx_power(value);
900 return 0;
902 case hwmon_power_crit:
903 *value = be16_to_cpu(sfp->diag.rxpwr_high_alarm);
904 sfp_hwmon_to_rx_power(value);
905 return 0;
907 case hwmon_power_lcrit_alarm:
908 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
909 if (err < 0)
910 return err;
912 *value = !!(status & SFP_ALARM1_RXPWR_LOW);
913 return 0;
915 case hwmon_power_min_alarm:
916 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
917 if (err < 0)
918 return err;
920 *value = !!(status & SFP_WARN1_RXPWR_LOW);
921 return 0;
923 case hwmon_power_max_alarm:
924 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
925 if (err < 0)
926 return err;
928 *value = !!(status & SFP_WARN1_RXPWR_HIGH);
929 return 0;
931 case hwmon_power_crit_alarm:
932 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
933 if (err < 0)
934 return err;
936 *value = !!(status & SFP_ALARM1_RXPWR_HIGH);
937 return 0;
938 default:
939 return -EOPNOTSUPP;
942 return -EOPNOTSUPP;
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);
950 switch (type) {
951 case hwmon_temp:
952 return sfp_hwmon_temp(sfp, attr, value);
953 case hwmon_in:
954 return sfp_hwmon_vcc(sfp, attr, value);
955 case hwmon_curr:
956 return sfp_hwmon_bias(sfp, attr, value);
957 case hwmon_power:
958 switch (channel) {
959 case 0:
960 return sfp_hwmon_tx_power(sfp, attr, value);
961 case 1:
962 return sfp_hwmon_rx_power(sfp, attr, value);
963 default:
964 return -EOPNOTSUPP;
966 default:
967 return -EOPNOTSUPP;
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[] = {
977 HWMON_C_REGISTER_TZ,
981 static const struct hwmon_channel_info sfp_hwmon_chip = {
982 .type = hwmon_chip,
983 .config = sfp_hwmon_chip_config,
986 static u32 sfp_hwmon_temp_config[] = {
987 HWMON_T_INPUT |
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 = {
996 .type = hwmon_temp,
997 .config = sfp_hwmon_temp_config,
1000 static u32 sfp_hwmon_vcc_config[] = {
1001 HWMON_I_INPUT |
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 = {
1010 .type = hwmon_in,
1011 .config = sfp_hwmon_vcc_config,
1014 static u32 sfp_hwmon_bias_config[] = {
1015 HWMON_C_INPUT |
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 = {
1024 .type = hwmon_curr,
1025 .config = sfp_hwmon_bias_config,
1028 static u32 sfp_hwmon_power_config[] = {
1029 /* Transmit power */
1030 HWMON_P_INPUT |
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,
1035 /* Receive power */
1036 HWMON_P_INPUT |
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[] = {
1050 &sfp_hwmon_chip,
1051 &sfp_hwmon_vcc_channel_info,
1052 &sfp_hwmon_temp_channel_info,
1053 &sfp_hwmon_bias_channel_info,
1054 &sfp_hwmon_power_channel_info,
1055 NULL,
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)
1065 int err, i;
1067 if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE)
1068 return 0;
1070 if (!(sfp->id.ext.diagmon & SFP_DIAGMON_DDM))
1071 return 0;
1073 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
1074 /* This driver in general does not support address
1075 * change.
1077 return 0;
1079 err = sfp_read(sfp, true, 0, &sfp->diag, sizeof(sfp->diag));
1080 if (err < 0)
1081 return err;
1083 sfp->hwmon_name = kstrdup(dev_name(sfp->dev), GFP_KERNEL);
1084 if (!sfp->hwmon_name)
1085 return -ENODEV;
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,
1094 NULL);
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);
1107 #else
1108 static int sfp_hwmon_insert(struct sfp *sfp)
1110 return 0;
1113 static void sfp_hwmon_remove(struct sfp *sfp)
1116 #endif
1118 /* Helpers */
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)
1140 return;
1142 sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
1144 udelay(T_RESET_US);
1146 sfp_set_state(sfp, state);
1149 /* SFP state machine */
1150 static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
1152 if (timeout)
1153 mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
1154 timeout);
1155 else
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;
1185 int err;
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");
1192 return;
1194 if (IS_ERR(phy)) {
1195 dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
1196 return;
1199 err = sfp_add_phy(sfp->sfp_bus, phy);
1200 if (err) {
1201 phy_device_remove(phy);
1202 phy_device_free(phy);
1203 dev_err(sfp->dev, "sfp_add_phy failed: %d\n", err);
1204 return;
1207 sfp->mod_phy = phy;
1208 phy_start(phy);
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))
1230 los ^= SFP_F_LOS;
1231 else if (!(sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL)))
1232 los = 0;
1234 if (los)
1235 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
1236 else
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) {
1259 dev_err(sfp->dev,
1260 "module persistently indicates fault, disabling\n");
1261 sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
1262 } else {
1263 if (warn)
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
1283 * be used.
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)
1298 u32 power;
1299 u8 val;
1300 int err;
1302 power = 1000;
1303 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL))
1304 power = 1500;
1305 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL))
1306 power = 2000;
1308 if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE &&
1309 (sfp->id.ext.diagmon & (SFP_DIAGMON_DDM | SFP_DIAGMON_ADDRMODE)) !=
1310 SFP_DIAGMON_DDM) {
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) {
1316 dev_err(sfp->dev,
1317 "Host does not support %u.%uW modules\n",
1318 power / 1000, (power / 100) % 10);
1319 return -EINVAL;
1321 return 0;
1324 if (power > sfp->max_power_mW) {
1325 dev_warn(sfp->dev,
1326 "Host does not support %u.%uW modules, module left in power mode 1\n",
1327 power / 1000, (power / 100) % 10);
1328 return 0;
1331 if (power <= 1000)
1332 return 0;
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);
1337 err = -EAGAIN;
1338 goto err;
1341 val |= BIT(0);
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);
1346 err = -EAGAIN;
1347 goto 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;
1354 err:
1355 return err;
1358 static int sfp_sm_mod_probe(struct sfp *sfp)
1360 /* SFP module inserted - read I2C data */
1361 struct sfp_eeprom_id id;
1362 bool cotsworks;
1363 u8 check;
1364 int ret;
1366 ret = sfp_read(sfp, false, 0, &id, sizeof(id));
1367 if (ret < 0) {
1368 dev_err(sfp->dev, "failed to read EEPROM: %d\n", ret);
1369 return -EAGAIN;
1372 if (ret != sizeof(id)) {
1373 dev_err(sfp->dev, "EEPROM short read: %d\n", ret);
1374 return -EAGAIN;
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) {
1386 if (cotsworks) {
1387 dev_warn(sfp->dev,
1388 "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n",
1389 check, id.base.cc_base);
1390 } else {
1391 dev_err(sfp->dev,
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);
1396 return -EINVAL;
1400 check = sfp_check(&id.ext, sizeof(id.ext) - 1);
1401 if (check != id.ext.cc_ext) {
1402 if (cotsworks) {
1403 dev_warn(sfp->dev,
1404 "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n",
1405 check, id.ext.cc_ext);
1406 } else {
1407 dev_err(sfp->dev,
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));
1416 sfp->id = id;
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)) {
1427 dev_err(sfp->dev,
1428 "module is not supported - phys id 0x%02x 0x%02x\n",
1429 sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
1430 return -EINVAL;
1433 /* If the module requires address swap mode, warn about it */
1434 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
1435 dev_warn(sfp->dev,
1436 "module address swap to access page 0xA2 is not supported.\n");
1438 ret = sfp_hwmon_insert(sfp);
1439 if (ret < 0)
1440 return ret;
1442 ret = sfp_module_insert(sfp->sfp_bus, &sfp->id);
1443 if (ret < 0)
1444 return ret;
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);
1455 if (sfp->mod_phy)
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) {
1479 default:
1480 if (event == SFP_E_INSERT) {
1481 sfp_module_tx_disable(sfp);
1482 sfp_sm_ins_next(sfp, SFP_MOD_PROBE, T_PROBE_INIT);
1484 break;
1486 case SFP_MOD_PROBE:
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);
1492 if (val == 0)
1493 sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
1494 else if (val > 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);
1498 else
1499 sfp_sm_set_timer(sfp, T_PROBE_RETRY);
1501 break;
1503 case SFP_MOD_HPOWER:
1504 if (event == SFP_E_TIMEOUT) {
1505 sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
1506 break;
1508 /* fallthrough */
1509 case SFP_MOD_PRESENT:
1510 case SFP_MOD_ERROR:
1511 if (event == SFP_E_REMOVE) {
1512 sfp_sm_mod_remove(sfp);
1513 sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
1515 break;
1518 /* This state machine tracks the netdev up/down state */
1519 switch (sfp->sm_dev_state) {
1520 default:
1521 if (event == SFP_E_DEV_UP)
1522 sfp->sm_dev_state = SFP_DEV_UP;
1523 break;
1525 case 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.
1531 if (!sfp->mod_phy)
1532 sfp_module_tx_disable(sfp);
1533 sfp->sm_dev_state = SFP_DEV_DOWN;
1535 break;
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);
1545 if (sfp->mod_phy)
1546 sfp_sm_phy_detach(sfp);
1547 sfp_sm_next(sfp, SFP_S_DOWN, 0);
1548 mutex_unlock(&sfp->sm_mutex);
1549 return;
1552 /* The main state machine */
1553 switch (sfp->sm_state) {
1554 case SFP_S_DOWN:
1555 if (sfp->sm_mod_state == SFP_MOD_PRESENT &&
1556 sfp->sm_dev_state == SFP_DEV_UP)
1557 sfp_sm_mod_init(sfp);
1558 break;
1560 case SFP_S_INIT:
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);
1565 break;
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);
1572 break;
1574 case SFP_S_LINK_UP:
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);
1582 break;
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);
1589 break;
1591 case SFP_S_REINIT:
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);
1598 break;
1600 case SFP_S_TX_DISABLE:
1601 break;
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;
1630 } else {
1631 modinfo->type = ETH_MODULE_SFF_8079;
1632 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
1634 return 0;
1637 static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee,
1638 u8 *data)
1640 unsigned int first, last, len;
1641 int ret;
1643 if (ee->len == 0)
1644 return -EINVAL;
1646 first = ee->offset;
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);
1650 len -= first;
1652 ret = sfp_read(sfp, false, first, data, len);
1653 if (ret < 0)
1654 return ret;
1656 first += len;
1657 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);
1661 len -= first;
1662 first -= ETH_MODULE_SFF_8079_LEN;
1664 ret = sfp_read(sfp, true, first, data, len);
1665 if (ret < 0)
1666 return ret;
1668 return 0;
1671 static const struct sfp_socket_ops sfp_module_ops = {
1672 .start = sfp_start,
1673 .stop = sfp_stop,
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);
1682 rtnl_lock();
1683 sfp_sm_event(sfp, SFP_E_TIMEOUT);
1684 rtnl_unlock();
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);
1701 sfp->state = state;
1703 rtnl_lock();
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);
1715 rtnl_unlock();
1718 static irqreturn_t sfp_irq(int irq, void *data)
1720 struct sfp *sfp = data;
1722 sfp_check_state(sfp);
1724 return IRQ_HANDLED;
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)
1737 struct sfp *sfp;
1739 sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
1740 if (!sfp)
1741 return ERR_PTR(-ENOMEM);
1743 sfp->dev = dev;
1745 mutex_init(&sfp->sm_mutex);
1746 INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
1747 INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
1749 return sfp;
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);
1758 if (sfp->i2c_mii) {
1759 mdiobus_unregister(sfp->i2c_mii);
1760 mdiobus_free(sfp->i2c_mii);
1762 if (sfp->i2c)
1763 i2c_put_adapter(sfp->i2c);
1764 kfree(sfp);
1767 static int sfp_probe(struct platform_device *pdev)
1769 const struct sff_data *sff;
1770 struct sfp *sfp;
1771 bool poll = false;
1772 int irq, err, i;
1774 sfp = sfp_alloc(&pdev->dev);
1775 if (IS_ERR(sfp))
1776 return PTR_ERR(sfp);
1778 platform_set_drvdata(pdev, sfp);
1780 err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
1781 if (err < 0)
1782 return err;
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);
1793 if (WARN_ON(!id))
1794 return -EINVAL;
1796 sff = sfp->type = id->data;
1798 np = of_parse_phandle(node, "i2c-bus", 0);
1799 if (!np) {
1800 dev_err(sfp->dev, "missing 'i2c-bus' property\n");
1801 return -ENODEV;
1804 i2c = of_find_i2c_adapter_by_node(np);
1805 of_node_put(np);
1806 if (!i2c)
1807 return -EPROBE_DEFER;
1809 err = sfp_i2c_configure(sfp, i2c);
1810 if (err < 0) {
1811 i2c_put_adapter(i2c);
1812 return err;
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);
1840 if (!sfp->sfp_bus)
1841 return -ENOMEM;
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);
1853 rtnl_lock();
1854 if (sfp->state & SFP_F_PRESENT)
1855 sfp_sm_event(sfp, SFP_E_INSERT);
1856 rtnl_unlock();
1858 for (i = 0; i < GPIO_MAX; i++) {
1859 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
1860 continue;
1862 irq = gpiod_to_irq(sfp->gpio[i]);
1863 if (!irq) {
1864 poll = true;
1865 continue;
1868 err = devm_request_threaded_irq(sfp->dev, irq, NULL, sfp_irq,
1869 IRQF_ONESHOT |
1870 IRQF_TRIGGER_RISING |
1871 IRQF_TRIGGER_FALLING,
1872 dev_name(sfp->dev), sfp);
1873 if (err)
1874 poll = true;
1877 if (poll)
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])
1886 dev_warn(sfp->dev,
1887 "No tx_disable pin: SFP modules will always be emitting.\n");
1889 return 0;
1892 static int sfp_remove(struct platform_device *pdev)
1894 struct sfp *sfp = platform_get_drvdata(pdev);
1896 sfp_unregister_socket(sfp->sfp_bus);
1898 return 0;
1901 static struct platform_driver sfp_driver = {
1902 .probe = sfp_probe,
1903 .remove = sfp_remove,
1904 .driver = {
1905 .name = "sfp",
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");