xref: /openbmc/linux/drivers/net/phy/sfp.c (revision a17922de)
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>
16 
17 #include "mdio-i2c.h"
18 #include "sfp.h"
19 #include "swphy.h"
20 
21 enum {
22 	GPIO_MODDEF0,
23 	GPIO_LOS,
24 	GPIO_TX_FAULT,
25 	GPIO_TX_DISABLE,
26 	GPIO_RATE_SELECT,
27 	GPIO_MAX,
28 
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),
34 
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,
44 
45 	SFP_MOD_EMPTY = 0,
46 	SFP_MOD_PROBE,
47 	SFP_MOD_HPOWER,
48 	SFP_MOD_PRESENT,
49 	SFP_MOD_ERROR,
50 
51 	SFP_DEV_DOWN = 0,
52 	SFP_DEV_UP,
53 
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,
61 };
62 
63 static const char *gpio_of_names[] = {
64 	"mod-def0",
65 	"los",
66 	"tx-fault",
67 	"tx-disable",
68 	"rate-select0",
69 };
70 
71 static const enum gpiod_flags gpio_flags[] = {
72 	GPIOD_IN,
73 	GPIOD_IN,
74 	GPIOD_IN,
75 	GPIOD_ASIS,
76 	GPIOD_ASIS,
77 };
78 
79 #define T_INIT_JIFFIES	msecs_to_jiffies(300)
80 #define T_RESET_US	10
81 #define T_FAULT_RECOVER	msecs_to_jiffies(1000)
82 
83 /* SFP module presence detection is poor: the three MOD DEF signals are
84  * the same length on the PCB, which means it's possible for MOD DEF 0 to
85  * connect before the I2C bus on MOD DEF 1/2.
86  *
87  * The SFP MSA specifies 300ms as t_init (the time taken for TX_FAULT to
88  * be deasserted) but makes no mention of the earliest time before we can
89  * access the I2C EEPROM.  However, Avago modules require 300ms.
90  */
91 #define T_PROBE_INIT	msecs_to_jiffies(300)
92 #define T_HPOWER_LEVEL	msecs_to_jiffies(300)
93 #define T_PROBE_RETRY	msecs_to_jiffies(100)
94 
95 /* SFP modules appear to always have their PHY configured for bus address
96  * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
97  */
98 #define SFP_PHY_ADDR	22
99 
100 /* Give this long for the PHY to reset. */
101 #define T_PHY_RESET_MS	50
102 
103 static DEFINE_MUTEX(sfp_mutex);
104 
105 struct sff_data {
106 	unsigned int gpios;
107 	bool (*module_supported)(const struct sfp_eeprom_id *id);
108 };
109 
110 struct sfp {
111 	struct device *dev;
112 	struct i2c_adapter *i2c;
113 	struct mii_bus *i2c_mii;
114 	struct sfp_bus *sfp_bus;
115 	struct phy_device *mod_phy;
116 	const struct sff_data *type;
117 	u32 max_power_mW;
118 
119 	unsigned int (*get_state)(struct sfp *);
120 	void (*set_state)(struct sfp *, unsigned int);
121 	int (*read)(struct sfp *, bool, u8, void *, size_t);
122 	int (*write)(struct sfp *, bool, u8, void *, size_t);
123 
124 	struct gpio_desc *gpio[GPIO_MAX];
125 
126 	unsigned int state;
127 	struct delayed_work poll;
128 	struct delayed_work timeout;
129 	struct mutex sm_mutex;
130 	unsigned char sm_mod_state;
131 	unsigned char sm_dev_state;
132 	unsigned short sm_state;
133 	unsigned int sm_retries;
134 
135 	struct sfp_eeprom_id id;
136 #if IS_ENABLED(CONFIG_HWMON)
137 	struct sfp_diag diag;
138 	struct device *hwmon_dev;
139 	char *hwmon_name;
140 #endif
141 
142 };
143 
144 static bool sff_module_supported(const struct sfp_eeprom_id *id)
145 {
146 	return id->base.phys_id == SFP_PHYS_ID_SFF &&
147 	       id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
148 }
149 
150 static const struct sff_data sff_data = {
151 	.gpios = SFP_F_LOS | SFP_F_TX_FAULT | SFP_F_TX_DISABLE,
152 	.module_supported = sff_module_supported,
153 };
154 
155 static bool sfp_module_supported(const struct sfp_eeprom_id *id)
156 {
157 	return id->base.phys_id == SFP_PHYS_ID_SFP &&
158 	       id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
159 }
160 
161 static const struct sff_data sfp_data = {
162 	.gpios = SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT |
163 		 SFP_F_TX_DISABLE | SFP_F_RATE_SELECT,
164 	.module_supported = sfp_module_supported,
165 };
166 
167 static const struct of_device_id sfp_of_match[] = {
168 	{ .compatible = "sff,sff", .data = &sff_data, },
169 	{ .compatible = "sff,sfp", .data = &sfp_data, },
170 	{ },
171 };
172 MODULE_DEVICE_TABLE(of, sfp_of_match);
173 
174 static unsigned long poll_jiffies;
175 
176 static unsigned int sfp_gpio_get_state(struct sfp *sfp)
177 {
178 	unsigned int i, state, v;
179 
180 	for (i = state = 0; i < GPIO_MAX; i++) {
181 		if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
182 			continue;
183 
184 		v = gpiod_get_value_cansleep(sfp->gpio[i]);
185 		if (v)
186 			state |= BIT(i);
187 	}
188 
189 	return state;
190 }
191 
192 static unsigned int sff_gpio_get_state(struct sfp *sfp)
193 {
194 	return sfp_gpio_get_state(sfp) | SFP_F_PRESENT;
195 }
196 
197 static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
198 {
199 	if (state & SFP_F_PRESENT) {
200 		/* If the module is present, drive the signals */
201 		if (sfp->gpio[GPIO_TX_DISABLE])
202 			gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
203 					       state & SFP_F_TX_DISABLE);
204 		if (state & SFP_F_RATE_SELECT)
205 			gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT],
206 					       state & SFP_F_RATE_SELECT);
207 	} else {
208 		/* Otherwise, let them float to the pull-ups */
209 		if (sfp->gpio[GPIO_TX_DISABLE])
210 			gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
211 		if (state & SFP_F_RATE_SELECT)
212 			gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]);
213 	}
214 }
215 
216 static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
217 			size_t len)
218 {
219 	struct i2c_msg msgs[2];
220 	u8 bus_addr = a2 ? 0x51 : 0x50;
221 	int ret;
222 
223 	msgs[0].addr = bus_addr;
224 	msgs[0].flags = 0;
225 	msgs[0].len = 1;
226 	msgs[0].buf = &dev_addr;
227 	msgs[1].addr = bus_addr;
228 	msgs[1].flags = I2C_M_RD;
229 	msgs[1].len = len;
230 	msgs[1].buf = buf;
231 
232 	ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
233 	if (ret < 0)
234 		return ret;
235 
236 	return ret == ARRAY_SIZE(msgs) ? len : 0;
237 }
238 
239 static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
240 	size_t len)
241 {
242 	struct i2c_msg msgs[1];
243 	u8 bus_addr = a2 ? 0x51 : 0x50;
244 	int ret;
245 
246 	msgs[0].addr = bus_addr;
247 	msgs[0].flags = 0;
248 	msgs[0].len = 1 + len;
249 	msgs[0].buf = kmalloc(1 + len, GFP_KERNEL);
250 	if (!msgs[0].buf)
251 		return -ENOMEM;
252 
253 	msgs[0].buf[0] = dev_addr;
254 	memcpy(&msgs[0].buf[1], buf, len);
255 
256 	ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
257 
258 	kfree(msgs[0].buf);
259 
260 	if (ret < 0)
261 		return ret;
262 
263 	return ret == ARRAY_SIZE(msgs) ? len : 0;
264 }
265 
266 static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
267 {
268 	struct mii_bus *i2c_mii;
269 	int ret;
270 
271 	if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
272 		return -EINVAL;
273 
274 	sfp->i2c = i2c;
275 	sfp->read = sfp_i2c_read;
276 	sfp->write = sfp_i2c_write;
277 
278 	i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
279 	if (IS_ERR(i2c_mii))
280 		return PTR_ERR(i2c_mii);
281 
282 	i2c_mii->name = "SFP I2C Bus";
283 	i2c_mii->phy_mask = ~0;
284 
285 	ret = mdiobus_register(i2c_mii);
286 	if (ret < 0) {
287 		mdiobus_free(i2c_mii);
288 		return ret;
289 	}
290 
291 	sfp->i2c_mii = i2c_mii;
292 
293 	return 0;
294 }
295 
296 /* Interface */
297 static unsigned int sfp_get_state(struct sfp *sfp)
298 {
299 	return sfp->get_state(sfp);
300 }
301 
302 static void sfp_set_state(struct sfp *sfp, unsigned int state)
303 {
304 	sfp->set_state(sfp, state);
305 }
306 
307 static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
308 {
309 	return sfp->read(sfp, a2, addr, buf, len);
310 }
311 
312 static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
313 {
314 	return sfp->write(sfp, a2, addr, buf, len);
315 }
316 
317 static unsigned int sfp_check(void *buf, size_t len)
318 {
319 	u8 *p, check;
320 
321 	for (p = buf, check = 0; len; p++, len--)
322 		check += *p;
323 
324 	return check;
325 }
326 
327 /* hwmon */
328 #if IS_ENABLED(CONFIG_HWMON)
329 static umode_t sfp_hwmon_is_visible(const void *data,
330 				    enum hwmon_sensor_types type,
331 				    u32 attr, int channel)
332 {
333 	const struct sfp *sfp = data;
334 
335 	switch (type) {
336 	case hwmon_temp:
337 		switch (attr) {
338 		case hwmon_temp_input:
339 		case hwmon_temp_min_alarm:
340 		case hwmon_temp_max_alarm:
341 		case hwmon_temp_lcrit_alarm:
342 		case hwmon_temp_crit_alarm:
343 		case hwmon_temp_min:
344 		case hwmon_temp_max:
345 		case hwmon_temp_lcrit:
346 		case hwmon_temp_crit:
347 			return 0444;
348 		default:
349 			return 0;
350 		}
351 	case hwmon_in:
352 		switch (attr) {
353 		case hwmon_in_input:
354 		case hwmon_in_min_alarm:
355 		case hwmon_in_max_alarm:
356 		case hwmon_in_lcrit_alarm:
357 		case hwmon_in_crit_alarm:
358 		case hwmon_in_min:
359 		case hwmon_in_max:
360 		case hwmon_in_lcrit:
361 		case hwmon_in_crit:
362 			return 0444;
363 		default:
364 			return 0;
365 		}
366 	case hwmon_curr:
367 		switch (attr) {
368 		case hwmon_curr_input:
369 		case hwmon_curr_min_alarm:
370 		case hwmon_curr_max_alarm:
371 		case hwmon_curr_lcrit_alarm:
372 		case hwmon_curr_crit_alarm:
373 		case hwmon_curr_min:
374 		case hwmon_curr_max:
375 		case hwmon_curr_lcrit:
376 		case hwmon_curr_crit:
377 			return 0444;
378 		default:
379 			return 0;
380 		}
381 	case hwmon_power:
382 		/* External calibration of receive power requires
383 		 * floating point arithmetic. Doing that in the kernel
384 		 * is not easy, so just skip it. If the module does
385 		 * not require external calibration, we can however
386 		 * show receiver power, since FP is then not needed.
387 		 */
388 		if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL &&
389 		    channel == 1)
390 			return 0;
391 		switch (attr) {
392 		case hwmon_power_input:
393 		case hwmon_power_min_alarm:
394 		case hwmon_power_max_alarm:
395 		case hwmon_power_lcrit_alarm:
396 		case hwmon_power_crit_alarm:
397 		case hwmon_power_min:
398 		case hwmon_power_max:
399 		case hwmon_power_lcrit:
400 		case hwmon_power_crit:
401 			return 0444;
402 		default:
403 			return 0;
404 		}
405 	default:
406 		return 0;
407 	}
408 }
409 
410 static int sfp_hwmon_read_sensor(struct sfp *sfp, int reg, long *value)
411 {
412 	__be16 val;
413 	int err;
414 
415 	err = sfp_read(sfp, true, reg, &val, sizeof(val));
416 	if (err < 0)
417 		return err;
418 
419 	*value = be16_to_cpu(val);
420 
421 	return 0;
422 }
423 
424 static void sfp_hwmon_to_rx_power(long *value)
425 {
426 	*value = DIV_ROUND_CLOSEST(*value, 100);
427 }
428 
429 static void sfp_hwmon_calibrate(struct sfp *sfp, unsigned int slope, int offset,
430 				long *value)
431 {
432 	if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL)
433 		*value = DIV_ROUND_CLOSEST(*value * slope, 256) + offset;
434 }
435 
436 static void sfp_hwmon_calibrate_temp(struct sfp *sfp, long *value)
437 {
438 	sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_t_slope),
439 			    be16_to_cpu(sfp->diag.cal_t_offset), value);
440 
441 	if (*value >= 0x8000)
442 		*value -= 0x10000;
443 
444 	*value = DIV_ROUND_CLOSEST(*value * 1000, 256);
445 }
446 
447 static void sfp_hwmon_calibrate_vcc(struct sfp *sfp, long *value)
448 {
449 	sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_v_slope),
450 			    be16_to_cpu(sfp->diag.cal_v_offset), value);
451 
452 	*value = DIV_ROUND_CLOSEST(*value, 10);
453 }
454 
455 static void sfp_hwmon_calibrate_bias(struct sfp *sfp, long *value)
456 {
457 	sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txi_slope),
458 			    be16_to_cpu(sfp->diag.cal_txi_offset), value);
459 
460 	*value = DIV_ROUND_CLOSEST(*value, 500);
461 }
462 
463 static void sfp_hwmon_calibrate_tx_power(struct sfp *sfp, long *value)
464 {
465 	sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txpwr_slope),
466 			    be16_to_cpu(sfp->diag.cal_txpwr_offset), value);
467 
468 	*value = DIV_ROUND_CLOSEST(*value, 10);
469 }
470 
471 static int sfp_hwmon_read_temp(struct sfp *sfp, int reg, long *value)
472 {
473 	int err;
474 
475 	err = sfp_hwmon_read_sensor(sfp, reg, value);
476 	if (err < 0)
477 		return err;
478 
479 	sfp_hwmon_calibrate_temp(sfp, value);
480 
481 	return 0;
482 }
483 
484 static int sfp_hwmon_read_vcc(struct sfp *sfp, int reg, long *value)
485 {
486 	int err;
487 
488 	err = sfp_hwmon_read_sensor(sfp, reg, value);
489 	if (err < 0)
490 		return err;
491 
492 	sfp_hwmon_calibrate_vcc(sfp, value);
493 
494 	return 0;
495 }
496 
497 static int sfp_hwmon_read_bias(struct sfp *sfp, int reg, long *value)
498 {
499 	int err;
500 
501 	err = sfp_hwmon_read_sensor(sfp, reg, value);
502 	if (err < 0)
503 		return err;
504 
505 	sfp_hwmon_calibrate_bias(sfp, value);
506 
507 	return 0;
508 }
509 
510 static int sfp_hwmon_read_tx_power(struct sfp *sfp, int reg, long *value)
511 {
512 	int err;
513 
514 	err = sfp_hwmon_read_sensor(sfp, reg, value);
515 	if (err < 0)
516 		return err;
517 
518 	sfp_hwmon_calibrate_tx_power(sfp, value);
519 
520 	return 0;
521 }
522 
523 static int sfp_hwmon_read_rx_power(struct sfp *sfp, int reg, long *value)
524 {
525 	int err;
526 
527 	err = sfp_hwmon_read_sensor(sfp, reg, value);
528 	if (err < 0)
529 		return err;
530 
531 	sfp_hwmon_to_rx_power(value);
532 
533 	return 0;
534 }
535 
536 static int sfp_hwmon_temp(struct sfp *sfp, u32 attr, long *value)
537 {
538 	u8 status;
539 	int err;
540 
541 	switch (attr) {
542 	case hwmon_temp_input:
543 		return sfp_hwmon_read_temp(sfp, SFP_TEMP, value);
544 
545 	case hwmon_temp_lcrit:
546 		*value = be16_to_cpu(sfp->diag.temp_low_alarm);
547 		sfp_hwmon_calibrate_temp(sfp, value);
548 		return 0;
549 
550 	case hwmon_temp_min:
551 		*value = be16_to_cpu(sfp->diag.temp_low_warn);
552 		sfp_hwmon_calibrate_temp(sfp, value);
553 		return 0;
554 	case hwmon_temp_max:
555 		*value = be16_to_cpu(sfp->diag.temp_high_warn);
556 		sfp_hwmon_calibrate_temp(sfp, value);
557 		return 0;
558 
559 	case hwmon_temp_crit:
560 		*value = be16_to_cpu(sfp->diag.temp_high_alarm);
561 		sfp_hwmon_calibrate_temp(sfp, value);
562 		return 0;
563 
564 	case hwmon_temp_lcrit_alarm:
565 		err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
566 		if (err < 0)
567 			return err;
568 
569 		*value = !!(status & SFP_ALARM0_TEMP_LOW);
570 		return 0;
571 
572 	case hwmon_temp_min_alarm:
573 		err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
574 		if (err < 0)
575 			return err;
576 
577 		*value = !!(status & SFP_WARN0_TEMP_LOW);
578 		return 0;
579 
580 	case hwmon_temp_max_alarm:
581 		err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
582 		if (err < 0)
583 			return err;
584 
585 		*value = !!(status & SFP_WARN0_TEMP_HIGH);
586 		return 0;
587 
588 	case hwmon_temp_crit_alarm:
589 		err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
590 		if (err < 0)
591 			return err;
592 
593 		*value = !!(status & SFP_ALARM0_TEMP_HIGH);
594 		return 0;
595 	default:
596 		return -EOPNOTSUPP;
597 	}
598 
599 	return -EOPNOTSUPP;
600 }
601 
602 static int sfp_hwmon_vcc(struct sfp *sfp, u32 attr, long *value)
603 {
604 	u8 status;
605 	int err;
606 
607 	switch (attr) {
608 	case hwmon_in_input:
609 		return sfp_hwmon_read_vcc(sfp, SFP_VCC, value);
610 
611 	case hwmon_in_lcrit:
612 		*value = be16_to_cpu(sfp->diag.volt_low_alarm);
613 		sfp_hwmon_calibrate_vcc(sfp, value);
614 		return 0;
615 
616 	case hwmon_in_min:
617 		*value = be16_to_cpu(sfp->diag.volt_low_warn);
618 		sfp_hwmon_calibrate_vcc(sfp, value);
619 		return 0;
620 
621 	case hwmon_in_max:
622 		*value = be16_to_cpu(sfp->diag.volt_high_warn);
623 		sfp_hwmon_calibrate_vcc(sfp, value);
624 		return 0;
625 
626 	case hwmon_in_crit:
627 		*value = be16_to_cpu(sfp->diag.volt_high_alarm);
628 		sfp_hwmon_calibrate_vcc(sfp, value);
629 		return 0;
630 
631 	case hwmon_in_lcrit_alarm:
632 		err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
633 		if (err < 0)
634 			return err;
635 
636 		*value = !!(status & SFP_ALARM0_VCC_LOW);
637 		return 0;
638 
639 	case hwmon_in_min_alarm:
640 		err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
641 		if (err < 0)
642 			return err;
643 
644 		*value = !!(status & SFP_WARN0_VCC_LOW);
645 		return 0;
646 
647 	case hwmon_in_max_alarm:
648 		err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
649 		if (err < 0)
650 			return err;
651 
652 		*value = !!(status & SFP_WARN0_VCC_HIGH);
653 		return 0;
654 
655 	case hwmon_in_crit_alarm:
656 		err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
657 		if (err < 0)
658 			return err;
659 
660 		*value = !!(status & SFP_ALARM0_VCC_HIGH);
661 		return 0;
662 	default:
663 		return -EOPNOTSUPP;
664 	}
665 
666 	return -EOPNOTSUPP;
667 }
668 
669 static int sfp_hwmon_bias(struct sfp *sfp, u32 attr, long *value)
670 {
671 	u8 status;
672 	int err;
673 
674 	switch (attr) {
675 	case hwmon_curr_input:
676 		return sfp_hwmon_read_bias(sfp, SFP_TX_BIAS, value);
677 
678 	case hwmon_curr_lcrit:
679 		*value = be16_to_cpu(sfp->diag.bias_low_alarm);
680 		sfp_hwmon_calibrate_bias(sfp, value);
681 		return 0;
682 
683 	case hwmon_curr_min:
684 		*value = be16_to_cpu(sfp->diag.bias_low_warn);
685 		sfp_hwmon_calibrate_bias(sfp, value);
686 		return 0;
687 
688 	case hwmon_curr_max:
689 		*value = be16_to_cpu(sfp->diag.bias_high_warn);
690 		sfp_hwmon_calibrate_bias(sfp, value);
691 		return 0;
692 
693 	case hwmon_curr_crit:
694 		*value = be16_to_cpu(sfp->diag.bias_high_alarm);
695 		sfp_hwmon_calibrate_bias(sfp, value);
696 		return 0;
697 
698 	case hwmon_curr_lcrit_alarm:
699 		err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
700 		if (err < 0)
701 			return err;
702 
703 		*value = !!(status & SFP_ALARM0_TX_BIAS_LOW);
704 		return 0;
705 
706 	case hwmon_curr_min_alarm:
707 		err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
708 		if (err < 0)
709 			return err;
710 
711 		*value = !!(status & SFP_WARN0_TX_BIAS_LOW);
712 		return 0;
713 
714 	case hwmon_curr_max_alarm:
715 		err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
716 		if (err < 0)
717 			return err;
718 
719 		*value = !!(status & SFP_WARN0_TX_BIAS_HIGH);
720 		return 0;
721 
722 	case hwmon_curr_crit_alarm:
723 		err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
724 		if (err < 0)
725 			return err;
726 
727 		*value = !!(status & SFP_ALARM0_TX_BIAS_HIGH);
728 		return 0;
729 	default:
730 		return -EOPNOTSUPP;
731 	}
732 
733 	return -EOPNOTSUPP;
734 }
735 
736 static int sfp_hwmon_tx_power(struct sfp *sfp, u32 attr, long *value)
737 {
738 	u8 status;
739 	int err;
740 
741 	switch (attr) {
742 	case hwmon_power_input:
743 		return sfp_hwmon_read_tx_power(sfp, SFP_TX_POWER, value);
744 
745 	case hwmon_power_lcrit:
746 		*value = be16_to_cpu(sfp->diag.txpwr_low_alarm);
747 		sfp_hwmon_calibrate_tx_power(sfp, value);
748 		return 0;
749 
750 	case hwmon_power_min:
751 		*value = be16_to_cpu(sfp->diag.txpwr_low_warn);
752 		sfp_hwmon_calibrate_tx_power(sfp, value);
753 		return 0;
754 
755 	case hwmon_power_max:
756 		*value = be16_to_cpu(sfp->diag.txpwr_high_warn);
757 		sfp_hwmon_calibrate_tx_power(sfp, value);
758 		return 0;
759 
760 	case hwmon_power_crit:
761 		*value = be16_to_cpu(sfp->diag.txpwr_high_alarm);
762 		sfp_hwmon_calibrate_tx_power(sfp, value);
763 		return 0;
764 
765 	case hwmon_power_lcrit_alarm:
766 		err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
767 		if (err < 0)
768 			return err;
769 
770 		*value = !!(status & SFP_ALARM0_TXPWR_LOW);
771 		return 0;
772 
773 	case hwmon_power_min_alarm:
774 		err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
775 		if (err < 0)
776 			return err;
777 
778 		*value = !!(status & SFP_WARN0_TXPWR_LOW);
779 		return 0;
780 
781 	case hwmon_power_max_alarm:
782 		err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
783 		if (err < 0)
784 			return err;
785 
786 		*value = !!(status & SFP_WARN0_TXPWR_HIGH);
787 		return 0;
788 
789 	case hwmon_power_crit_alarm:
790 		err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
791 		if (err < 0)
792 			return err;
793 
794 		*value = !!(status & SFP_ALARM0_TXPWR_HIGH);
795 		return 0;
796 	default:
797 		return -EOPNOTSUPP;
798 	}
799 
800 	return -EOPNOTSUPP;
801 }
802 
803 static int sfp_hwmon_rx_power(struct sfp *sfp, u32 attr, long *value)
804 {
805 	u8 status;
806 	int err;
807 
808 	switch (attr) {
809 	case hwmon_power_input:
810 		return sfp_hwmon_read_rx_power(sfp, SFP_RX_POWER, value);
811 
812 	case hwmon_power_lcrit:
813 		*value = be16_to_cpu(sfp->diag.rxpwr_low_alarm);
814 		sfp_hwmon_to_rx_power(value);
815 		return 0;
816 
817 	case hwmon_power_min:
818 		*value = be16_to_cpu(sfp->diag.rxpwr_low_warn);
819 		sfp_hwmon_to_rx_power(value);
820 		return 0;
821 
822 	case hwmon_power_max:
823 		*value = be16_to_cpu(sfp->diag.rxpwr_high_warn);
824 		sfp_hwmon_to_rx_power(value);
825 		return 0;
826 
827 	case hwmon_power_crit:
828 		*value = be16_to_cpu(sfp->diag.rxpwr_high_alarm);
829 		sfp_hwmon_to_rx_power(value);
830 		return 0;
831 
832 	case hwmon_power_lcrit_alarm:
833 		err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
834 		if (err < 0)
835 			return err;
836 
837 		*value = !!(status & SFP_ALARM1_RXPWR_LOW);
838 		return 0;
839 
840 	case hwmon_power_min_alarm:
841 		err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
842 		if (err < 0)
843 			return err;
844 
845 		*value = !!(status & SFP_WARN1_RXPWR_LOW);
846 		return 0;
847 
848 	case hwmon_power_max_alarm:
849 		err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
850 		if (err < 0)
851 			return err;
852 
853 		*value = !!(status & SFP_WARN1_RXPWR_HIGH);
854 		return 0;
855 
856 	case hwmon_power_crit_alarm:
857 		err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
858 		if (err < 0)
859 			return err;
860 
861 		*value = !!(status & SFP_ALARM1_RXPWR_HIGH);
862 		return 0;
863 	default:
864 		return -EOPNOTSUPP;
865 	}
866 
867 	return -EOPNOTSUPP;
868 }
869 
870 static int sfp_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
871 			  u32 attr, int channel, long *value)
872 {
873 	struct sfp *sfp = dev_get_drvdata(dev);
874 
875 	switch (type) {
876 	case hwmon_temp:
877 		return sfp_hwmon_temp(sfp, attr, value);
878 	case hwmon_in:
879 		return sfp_hwmon_vcc(sfp, attr, value);
880 	case hwmon_curr:
881 		return sfp_hwmon_bias(sfp, attr, value);
882 	case hwmon_power:
883 		switch (channel) {
884 		case 0:
885 			return sfp_hwmon_tx_power(sfp, attr, value);
886 		case 1:
887 			return sfp_hwmon_rx_power(sfp, attr, value);
888 		default:
889 			return -EOPNOTSUPP;
890 		}
891 	default:
892 		return -EOPNOTSUPP;
893 	}
894 }
895 
896 static const struct hwmon_ops sfp_hwmon_ops = {
897 	.is_visible = sfp_hwmon_is_visible,
898 	.read = sfp_hwmon_read,
899 };
900 
901 static u32 sfp_hwmon_chip_config[] = {
902 	HWMON_C_REGISTER_TZ,
903 	0,
904 };
905 
906 static const struct hwmon_channel_info sfp_hwmon_chip = {
907 	.type = hwmon_chip,
908 	.config = sfp_hwmon_chip_config,
909 };
910 
911 static u32 sfp_hwmon_temp_config[] = {
912 	HWMON_T_INPUT |
913 	HWMON_T_MAX | HWMON_T_MIN |
914 	HWMON_T_MAX_ALARM | HWMON_T_MIN_ALARM |
915 	HWMON_T_CRIT | HWMON_T_LCRIT |
916 	HWMON_T_CRIT_ALARM | HWMON_T_LCRIT_ALARM,
917 	0,
918 };
919 
920 static const struct hwmon_channel_info sfp_hwmon_temp_channel_info = {
921 	.type = hwmon_temp,
922 	.config = sfp_hwmon_temp_config,
923 };
924 
925 static u32 sfp_hwmon_vcc_config[] = {
926 	HWMON_I_INPUT |
927 	HWMON_I_MAX | HWMON_I_MIN |
928 	HWMON_I_MAX_ALARM | HWMON_I_MIN_ALARM |
929 	HWMON_I_CRIT | HWMON_I_LCRIT |
930 	HWMON_I_CRIT_ALARM | HWMON_I_LCRIT_ALARM,
931 	0,
932 };
933 
934 static const struct hwmon_channel_info sfp_hwmon_vcc_channel_info = {
935 	.type = hwmon_in,
936 	.config = sfp_hwmon_vcc_config,
937 };
938 
939 static u32 sfp_hwmon_bias_config[] = {
940 	HWMON_C_INPUT |
941 	HWMON_C_MAX | HWMON_C_MIN |
942 	HWMON_C_MAX_ALARM | HWMON_C_MIN_ALARM |
943 	HWMON_C_CRIT | HWMON_C_LCRIT |
944 	HWMON_C_CRIT_ALARM | HWMON_C_LCRIT_ALARM,
945 	0,
946 };
947 
948 static const struct hwmon_channel_info sfp_hwmon_bias_channel_info = {
949 	.type = hwmon_curr,
950 	.config = sfp_hwmon_bias_config,
951 };
952 
953 static u32 sfp_hwmon_power_config[] = {
954 	/* Transmit power */
955 	HWMON_P_INPUT |
956 	HWMON_P_MAX | HWMON_P_MIN |
957 	HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
958 	HWMON_P_CRIT | HWMON_P_LCRIT |
959 	HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM,
960 	/* Receive power */
961 	HWMON_P_INPUT |
962 	HWMON_P_MAX | HWMON_P_MIN |
963 	HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
964 	HWMON_P_CRIT | HWMON_P_LCRIT |
965 	HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM,
966 	0,
967 };
968 
969 static const struct hwmon_channel_info sfp_hwmon_power_channel_info = {
970 	.type = hwmon_power,
971 	.config = sfp_hwmon_power_config,
972 };
973 
974 static const struct hwmon_channel_info *sfp_hwmon_info[] = {
975 	&sfp_hwmon_chip,
976 	&sfp_hwmon_vcc_channel_info,
977 	&sfp_hwmon_temp_channel_info,
978 	&sfp_hwmon_bias_channel_info,
979 	&sfp_hwmon_power_channel_info,
980 	NULL,
981 };
982 
983 static const struct hwmon_chip_info sfp_hwmon_chip_info = {
984 	.ops = &sfp_hwmon_ops,
985 	.info = sfp_hwmon_info,
986 };
987 
988 static int sfp_hwmon_insert(struct sfp *sfp)
989 {
990 	int err, i;
991 
992 	if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE)
993 		return 0;
994 
995 	if (!(sfp->id.ext.diagmon & SFP_DIAGMON_DDM))
996 		return 0;
997 
998 	if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
999 		/* This driver in general does not support address
1000 		 * change.
1001 		 */
1002 		return 0;
1003 
1004 	err = sfp_read(sfp, true, 0, &sfp->diag, sizeof(sfp->diag));
1005 	if (err < 0)
1006 		return err;
1007 
1008 	sfp->hwmon_name = kstrdup(dev_name(sfp->dev), GFP_KERNEL);
1009 	if (!sfp->hwmon_name)
1010 		return -ENODEV;
1011 
1012 	for (i = 0; sfp->hwmon_name[i]; i++)
1013 		if (hwmon_is_bad_char(sfp->hwmon_name[i]))
1014 			sfp->hwmon_name[i] = '_';
1015 
1016 	sfp->hwmon_dev = hwmon_device_register_with_info(sfp->dev,
1017 							 sfp->hwmon_name, sfp,
1018 							 &sfp_hwmon_chip_info,
1019 							 NULL);
1020 
1021 	return PTR_ERR_OR_ZERO(sfp->hwmon_dev);
1022 }
1023 
1024 static void sfp_hwmon_remove(struct sfp *sfp)
1025 {
1026 	hwmon_device_unregister(sfp->hwmon_dev);
1027 	kfree(sfp->hwmon_name);
1028 }
1029 #else
1030 static int sfp_hwmon_insert(struct sfp *sfp)
1031 {
1032 	return 0;
1033 }
1034 
1035 static void sfp_hwmon_remove(struct sfp *sfp)
1036 {
1037 }
1038 #endif
1039 
1040 /* Helpers */
1041 static void sfp_module_tx_disable(struct sfp *sfp)
1042 {
1043 	dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1044 		sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
1045 	sfp->state |= SFP_F_TX_DISABLE;
1046 	sfp_set_state(sfp, sfp->state);
1047 }
1048 
1049 static void sfp_module_tx_enable(struct sfp *sfp)
1050 {
1051 	dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1052 		sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
1053 	sfp->state &= ~SFP_F_TX_DISABLE;
1054 	sfp_set_state(sfp, sfp->state);
1055 }
1056 
1057 static void sfp_module_tx_fault_reset(struct sfp *sfp)
1058 {
1059 	unsigned int state = sfp->state;
1060 
1061 	if (state & SFP_F_TX_DISABLE)
1062 		return;
1063 
1064 	sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
1065 
1066 	udelay(T_RESET_US);
1067 
1068 	sfp_set_state(sfp, state);
1069 }
1070 
1071 /* SFP state machine */
1072 static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
1073 {
1074 	if (timeout)
1075 		mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
1076 				 timeout);
1077 	else
1078 		cancel_delayed_work(&sfp->timeout);
1079 }
1080 
1081 static void sfp_sm_next(struct sfp *sfp, unsigned int state,
1082 			unsigned int timeout)
1083 {
1084 	sfp->sm_state = state;
1085 	sfp_sm_set_timer(sfp, timeout);
1086 }
1087 
1088 static void sfp_sm_ins_next(struct sfp *sfp, unsigned int state,
1089 			    unsigned int timeout)
1090 {
1091 	sfp->sm_mod_state = state;
1092 	sfp_sm_set_timer(sfp, timeout);
1093 }
1094 
1095 static void sfp_sm_phy_detach(struct sfp *sfp)
1096 {
1097 	phy_stop(sfp->mod_phy);
1098 	sfp_remove_phy(sfp->sfp_bus);
1099 	phy_device_remove(sfp->mod_phy);
1100 	phy_device_free(sfp->mod_phy);
1101 	sfp->mod_phy = NULL;
1102 }
1103 
1104 static void sfp_sm_probe_phy(struct sfp *sfp)
1105 {
1106 	struct phy_device *phy;
1107 	int err;
1108 
1109 	msleep(T_PHY_RESET_MS);
1110 
1111 	phy = mdiobus_scan(sfp->i2c_mii, SFP_PHY_ADDR);
1112 	if (phy == ERR_PTR(-ENODEV)) {
1113 		dev_info(sfp->dev, "no PHY detected\n");
1114 		return;
1115 	}
1116 	if (IS_ERR(phy)) {
1117 		dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
1118 		return;
1119 	}
1120 
1121 	err = sfp_add_phy(sfp->sfp_bus, phy);
1122 	if (err) {
1123 		phy_device_remove(phy);
1124 		phy_device_free(phy);
1125 		dev_err(sfp->dev, "sfp_add_phy failed: %d\n", err);
1126 		return;
1127 	}
1128 
1129 	sfp->mod_phy = phy;
1130 	phy_start(phy);
1131 }
1132 
1133 static void sfp_sm_link_up(struct sfp *sfp)
1134 {
1135 	sfp_link_up(sfp->sfp_bus);
1136 	sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
1137 }
1138 
1139 static void sfp_sm_link_down(struct sfp *sfp)
1140 {
1141 	sfp_link_down(sfp->sfp_bus);
1142 }
1143 
1144 static void sfp_sm_link_check_los(struct sfp *sfp)
1145 {
1146 	unsigned int los = sfp->state & SFP_F_LOS;
1147 
1148 	/* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL
1149 	 * are set, we assume that no LOS signal is available.
1150 	 */
1151 	if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED))
1152 		los ^= SFP_F_LOS;
1153 	else if (!(sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL)))
1154 		los = 0;
1155 
1156 	if (los)
1157 		sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
1158 	else
1159 		sfp_sm_link_up(sfp);
1160 }
1161 
1162 static bool sfp_los_event_active(struct sfp *sfp, unsigned int event)
1163 {
1164 	return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
1165 		event == SFP_E_LOS_LOW) ||
1166 	       (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
1167 		event == SFP_E_LOS_HIGH);
1168 }
1169 
1170 static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event)
1171 {
1172 	return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
1173 		event == SFP_E_LOS_HIGH) ||
1174 	       (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
1175 		event == SFP_E_LOS_LOW);
1176 }
1177 
1178 static void sfp_sm_fault(struct sfp *sfp, bool warn)
1179 {
1180 	if (sfp->sm_retries && !--sfp->sm_retries) {
1181 		dev_err(sfp->dev,
1182 			"module persistently indicates fault, disabling\n");
1183 		sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
1184 	} else {
1185 		if (warn)
1186 			dev_err(sfp->dev, "module transmit fault indicated\n");
1187 
1188 		sfp_sm_next(sfp, SFP_S_TX_FAULT, T_FAULT_RECOVER);
1189 	}
1190 }
1191 
1192 static void sfp_sm_mod_init(struct sfp *sfp)
1193 {
1194 	sfp_module_tx_enable(sfp);
1195 
1196 	/* Wait t_init before indicating that the link is up, provided the
1197 	 * current state indicates no TX_FAULT.  If TX_FAULT clears before
1198 	 * this time, that's fine too.
1199 	 */
1200 	sfp_sm_next(sfp, SFP_S_INIT, T_INIT_JIFFIES);
1201 	sfp->sm_retries = 5;
1202 
1203 	/* Setting the serdes link mode is guesswork: there's no
1204 	 * field in the EEPROM which indicates what mode should
1205 	 * be used.
1206 	 *
1207 	 * If it's a gigabit-only fiber module, it probably does
1208 	 * not have a PHY, so switch to 802.3z negotiation mode.
1209 	 * Otherwise, switch to SGMII mode (which is required to
1210 	 * support non-gigabit speeds) and probe for a PHY.
1211 	 */
1212 	if (sfp->id.base.e1000_base_t ||
1213 	    sfp->id.base.e100_base_lx ||
1214 	    sfp->id.base.e100_base_fx)
1215 		sfp_sm_probe_phy(sfp);
1216 }
1217 
1218 static int sfp_sm_mod_hpower(struct sfp *sfp)
1219 {
1220 	u32 power;
1221 	u8 val;
1222 	int err;
1223 
1224 	power = 1000;
1225 	if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL))
1226 		power = 1500;
1227 	if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL))
1228 		power = 2000;
1229 
1230 	if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE &&
1231 	    (sfp->id.ext.diagmon & (SFP_DIAGMON_DDM | SFP_DIAGMON_ADDRMODE)) !=
1232 	    SFP_DIAGMON_DDM) {
1233 		/* The module appears not to implement bus address 0xa2,
1234 		 * or requires an address change sequence, so assume that
1235 		 * the module powers up in the indicated power mode.
1236 		 */
1237 		if (power > sfp->max_power_mW) {
1238 			dev_err(sfp->dev,
1239 				"Host does not support %u.%uW modules\n",
1240 				power / 1000, (power / 100) % 10);
1241 			return -EINVAL;
1242 		}
1243 		return 0;
1244 	}
1245 
1246 	if (power > sfp->max_power_mW) {
1247 		dev_warn(sfp->dev,
1248 			 "Host does not support %u.%uW modules, module left in power mode 1\n",
1249 			 power / 1000, (power / 100) % 10);
1250 		return 0;
1251 	}
1252 
1253 	if (power <= 1000)
1254 		return 0;
1255 
1256 	err = sfp_read(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
1257 	if (err != sizeof(val)) {
1258 		dev_err(sfp->dev, "Failed to read EEPROM: %d\n", err);
1259 		err = -EAGAIN;
1260 		goto err;
1261 	}
1262 
1263 	val |= BIT(0);
1264 
1265 	err = sfp_write(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
1266 	if (err != sizeof(val)) {
1267 		dev_err(sfp->dev, "Failed to write EEPROM: %d\n", err);
1268 		err = -EAGAIN;
1269 		goto err;
1270 	}
1271 
1272 	dev_info(sfp->dev, "Module switched to %u.%uW power level\n",
1273 		 power / 1000, (power / 100) % 10);
1274 	return T_HPOWER_LEVEL;
1275 
1276 err:
1277 	return err;
1278 }
1279 
1280 static int sfp_sm_mod_probe(struct sfp *sfp)
1281 {
1282 	/* SFP module inserted - read I2C data */
1283 	struct sfp_eeprom_id id;
1284 	bool cotsworks;
1285 	u8 check;
1286 	int ret;
1287 
1288 	ret = sfp_read(sfp, false, 0, &id, sizeof(id));
1289 	if (ret < 0) {
1290 		dev_err(sfp->dev, "failed to read EEPROM: %d\n", ret);
1291 		return -EAGAIN;
1292 	}
1293 
1294 	if (ret != sizeof(id)) {
1295 		dev_err(sfp->dev, "EEPROM short read: %d\n", ret);
1296 		return -EAGAIN;
1297 	}
1298 
1299 	/* Cotsworks do not seem to update the checksums when they
1300 	 * do the final programming with the final module part number,
1301 	 * serial number and date code.
1302 	 */
1303 	cotsworks = !memcmp(id.base.vendor_name, "COTSWORKS       ", 16);
1304 
1305 	/* Validate the checksum over the base structure */
1306 	check = sfp_check(&id.base, sizeof(id.base) - 1);
1307 	if (check != id.base.cc_base) {
1308 		if (cotsworks) {
1309 			dev_warn(sfp->dev,
1310 				 "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n",
1311 				 check, id.base.cc_base);
1312 		} else {
1313 			dev_err(sfp->dev,
1314 				"EEPROM base structure checksum failure: 0x%02x != 0x%02x\n",
1315 				check, id.base.cc_base);
1316 			print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
1317 				       16, 1, &id, sizeof(id), true);
1318 			return -EINVAL;
1319 		}
1320 	}
1321 
1322 	check = sfp_check(&id.ext, sizeof(id.ext) - 1);
1323 	if (check != id.ext.cc_ext) {
1324 		if (cotsworks) {
1325 			dev_warn(sfp->dev,
1326 				 "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n",
1327 				 check, id.ext.cc_ext);
1328 		} else {
1329 			dev_err(sfp->dev,
1330 				"EEPROM extended structure checksum failure: 0x%02x != 0x%02x\n",
1331 				check, id.ext.cc_ext);
1332 			print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
1333 				       16, 1, &id, sizeof(id), true);
1334 			memset(&id.ext, 0, sizeof(id.ext));
1335 		}
1336 	}
1337 
1338 	sfp->id = id;
1339 
1340 	dev_info(sfp->dev, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n",
1341 		 (int)sizeof(id.base.vendor_name), id.base.vendor_name,
1342 		 (int)sizeof(id.base.vendor_pn), id.base.vendor_pn,
1343 		 (int)sizeof(id.base.vendor_rev), id.base.vendor_rev,
1344 		 (int)sizeof(id.ext.vendor_sn), id.ext.vendor_sn,
1345 		 (int)sizeof(id.ext.datecode), id.ext.datecode);
1346 
1347 	/* Check whether we support this module */
1348 	if (!sfp->type->module_supported(&sfp->id)) {
1349 		dev_err(sfp->dev,
1350 			"module is not supported - phys id 0x%02x 0x%02x\n",
1351 			sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
1352 		return -EINVAL;
1353 	}
1354 
1355 	/* If the module requires address swap mode, warn about it */
1356 	if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
1357 		dev_warn(sfp->dev,
1358 			 "module address swap to access page 0xA2 is not supported.\n");
1359 
1360 	ret = sfp_hwmon_insert(sfp);
1361 	if (ret < 0)
1362 		return ret;
1363 
1364 	ret = sfp_module_insert(sfp->sfp_bus, &sfp->id);
1365 	if (ret < 0)
1366 		return ret;
1367 
1368 	return sfp_sm_mod_hpower(sfp);
1369 }
1370 
1371 static void sfp_sm_mod_remove(struct sfp *sfp)
1372 {
1373 	sfp_module_remove(sfp->sfp_bus);
1374 
1375 	sfp_hwmon_remove(sfp);
1376 
1377 	if (sfp->mod_phy)
1378 		sfp_sm_phy_detach(sfp);
1379 
1380 	sfp_module_tx_disable(sfp);
1381 
1382 	memset(&sfp->id, 0, sizeof(sfp->id));
1383 
1384 	dev_info(sfp->dev, "module removed\n");
1385 }
1386 
1387 static void sfp_sm_event(struct sfp *sfp, unsigned int event)
1388 {
1389 	mutex_lock(&sfp->sm_mutex);
1390 
1391 	dev_dbg(sfp->dev, "SM: enter %u:%u:%u event %u\n",
1392 		sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state, event);
1393 
1394 	/* This state machine tracks the insert/remove state of
1395 	 * the module, and handles probing the on-board EEPROM.
1396 	 */
1397 	switch (sfp->sm_mod_state) {
1398 	default:
1399 		if (event == SFP_E_INSERT) {
1400 			sfp_module_tx_disable(sfp);
1401 			sfp_sm_ins_next(sfp, SFP_MOD_PROBE, T_PROBE_INIT);
1402 		}
1403 		break;
1404 
1405 	case SFP_MOD_PROBE:
1406 		if (event == SFP_E_REMOVE) {
1407 			sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
1408 		} else if (event == SFP_E_TIMEOUT) {
1409 			int val = sfp_sm_mod_probe(sfp);
1410 
1411 			if (val == 0)
1412 				sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
1413 			else if (val > 0)
1414 				sfp_sm_ins_next(sfp, SFP_MOD_HPOWER, val);
1415 			else if (val != -EAGAIN)
1416 				sfp_sm_ins_next(sfp, SFP_MOD_ERROR, 0);
1417 			else
1418 				sfp_sm_set_timer(sfp, T_PROBE_RETRY);
1419 		}
1420 		break;
1421 
1422 	case SFP_MOD_HPOWER:
1423 		if (event == SFP_E_TIMEOUT) {
1424 			sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
1425 			break;
1426 		}
1427 		/* fallthrough */
1428 	case SFP_MOD_PRESENT:
1429 	case SFP_MOD_ERROR:
1430 		if (event == SFP_E_REMOVE) {
1431 			sfp_sm_mod_remove(sfp);
1432 			sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
1433 		}
1434 		break;
1435 	}
1436 
1437 	/* This state machine tracks the netdev up/down state */
1438 	switch (sfp->sm_dev_state) {
1439 	default:
1440 		if (event == SFP_E_DEV_UP)
1441 			sfp->sm_dev_state = SFP_DEV_UP;
1442 		break;
1443 
1444 	case SFP_DEV_UP:
1445 		if (event == SFP_E_DEV_DOWN) {
1446 			/* If the module has a PHY, avoid raising TX disable
1447 			 * as this resets the PHY. Otherwise, raise it to
1448 			 * turn the laser off.
1449 			 */
1450 			if (!sfp->mod_phy)
1451 				sfp_module_tx_disable(sfp);
1452 			sfp->sm_dev_state = SFP_DEV_DOWN;
1453 		}
1454 		break;
1455 	}
1456 
1457 	/* Some events are global */
1458 	if (sfp->sm_state != SFP_S_DOWN &&
1459 	    (sfp->sm_mod_state != SFP_MOD_PRESENT ||
1460 	     sfp->sm_dev_state != SFP_DEV_UP)) {
1461 		if (sfp->sm_state == SFP_S_LINK_UP &&
1462 		    sfp->sm_dev_state == SFP_DEV_UP)
1463 			sfp_sm_link_down(sfp);
1464 		if (sfp->mod_phy)
1465 			sfp_sm_phy_detach(sfp);
1466 		sfp_sm_next(sfp, SFP_S_DOWN, 0);
1467 		mutex_unlock(&sfp->sm_mutex);
1468 		return;
1469 	}
1470 
1471 	/* The main state machine */
1472 	switch (sfp->sm_state) {
1473 	case SFP_S_DOWN:
1474 		if (sfp->sm_mod_state == SFP_MOD_PRESENT &&
1475 		    sfp->sm_dev_state == SFP_DEV_UP)
1476 			sfp_sm_mod_init(sfp);
1477 		break;
1478 
1479 	case SFP_S_INIT:
1480 		if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT)
1481 			sfp_sm_fault(sfp, true);
1482 		else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR)
1483 			sfp_sm_link_check_los(sfp);
1484 		break;
1485 
1486 	case SFP_S_WAIT_LOS:
1487 		if (event == SFP_E_TX_FAULT)
1488 			sfp_sm_fault(sfp, true);
1489 		else if (sfp_los_event_inactive(sfp, event))
1490 			sfp_sm_link_up(sfp);
1491 		break;
1492 
1493 	case SFP_S_LINK_UP:
1494 		if (event == SFP_E_TX_FAULT) {
1495 			sfp_sm_link_down(sfp);
1496 			sfp_sm_fault(sfp, true);
1497 		} else if (sfp_los_event_active(sfp, event)) {
1498 			sfp_sm_link_down(sfp);
1499 			sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
1500 		}
1501 		break;
1502 
1503 	case SFP_S_TX_FAULT:
1504 		if (event == SFP_E_TIMEOUT) {
1505 			sfp_module_tx_fault_reset(sfp);
1506 			sfp_sm_next(sfp, SFP_S_REINIT, T_INIT_JIFFIES);
1507 		}
1508 		break;
1509 
1510 	case SFP_S_REINIT:
1511 		if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
1512 			sfp_sm_fault(sfp, false);
1513 		} else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
1514 			dev_info(sfp->dev, "module transmit fault recovered\n");
1515 			sfp_sm_link_check_los(sfp);
1516 		}
1517 		break;
1518 
1519 	case SFP_S_TX_DISABLE:
1520 		break;
1521 	}
1522 
1523 	dev_dbg(sfp->dev, "SM: exit %u:%u:%u\n",
1524 		sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state);
1525 
1526 	mutex_unlock(&sfp->sm_mutex);
1527 }
1528 
1529 static void sfp_start(struct sfp *sfp)
1530 {
1531 	sfp_sm_event(sfp, SFP_E_DEV_UP);
1532 }
1533 
1534 static void sfp_stop(struct sfp *sfp)
1535 {
1536 	sfp_sm_event(sfp, SFP_E_DEV_DOWN);
1537 }
1538 
1539 static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo)
1540 {
1541 	/* locking... and check module is present */
1542 
1543 	if (sfp->id.ext.sff8472_compliance &&
1544 	    !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) {
1545 		modinfo->type = ETH_MODULE_SFF_8472;
1546 		modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1547 	} else {
1548 		modinfo->type = ETH_MODULE_SFF_8079;
1549 		modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
1550 	}
1551 	return 0;
1552 }
1553 
1554 static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee,
1555 			     u8 *data)
1556 {
1557 	unsigned int first, last, len;
1558 	int ret;
1559 
1560 	if (ee->len == 0)
1561 		return -EINVAL;
1562 
1563 	first = ee->offset;
1564 	last = ee->offset + ee->len;
1565 	if (first < ETH_MODULE_SFF_8079_LEN) {
1566 		len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN);
1567 		len -= first;
1568 
1569 		ret = sfp_read(sfp, false, first, data, len);
1570 		if (ret < 0)
1571 			return ret;
1572 
1573 		first += len;
1574 		data += len;
1575 	}
1576 	if (first < ETH_MODULE_SFF_8472_LEN && last > ETH_MODULE_SFF_8079_LEN) {
1577 		len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN);
1578 		len -= first;
1579 		first -= ETH_MODULE_SFF_8079_LEN;
1580 
1581 		ret = sfp_read(sfp, true, first, data, len);
1582 		if (ret < 0)
1583 			return ret;
1584 	}
1585 	return 0;
1586 }
1587 
1588 static const struct sfp_socket_ops sfp_module_ops = {
1589 	.start = sfp_start,
1590 	.stop = sfp_stop,
1591 	.module_info = sfp_module_info,
1592 	.module_eeprom = sfp_module_eeprom,
1593 };
1594 
1595 static void sfp_timeout(struct work_struct *work)
1596 {
1597 	struct sfp *sfp = container_of(work, struct sfp, timeout.work);
1598 
1599 	rtnl_lock();
1600 	sfp_sm_event(sfp, SFP_E_TIMEOUT);
1601 	rtnl_unlock();
1602 }
1603 
1604 static void sfp_check_state(struct sfp *sfp)
1605 {
1606 	unsigned int state, i, changed;
1607 
1608 	state = sfp_get_state(sfp);
1609 	changed = state ^ sfp->state;
1610 	changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
1611 
1612 	for (i = 0; i < GPIO_MAX; i++)
1613 		if (changed & BIT(i))
1614 			dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i],
1615 				!!(sfp->state & BIT(i)), !!(state & BIT(i)));
1616 
1617 	state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT);
1618 	sfp->state = state;
1619 
1620 	rtnl_lock();
1621 	if (changed & SFP_F_PRESENT)
1622 		sfp_sm_event(sfp, state & SFP_F_PRESENT ?
1623 				SFP_E_INSERT : SFP_E_REMOVE);
1624 
1625 	if (changed & SFP_F_TX_FAULT)
1626 		sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
1627 				SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
1628 
1629 	if (changed & SFP_F_LOS)
1630 		sfp_sm_event(sfp, state & SFP_F_LOS ?
1631 				SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
1632 	rtnl_unlock();
1633 }
1634 
1635 static irqreturn_t sfp_irq(int irq, void *data)
1636 {
1637 	struct sfp *sfp = data;
1638 
1639 	sfp_check_state(sfp);
1640 
1641 	return IRQ_HANDLED;
1642 }
1643 
1644 static void sfp_poll(struct work_struct *work)
1645 {
1646 	struct sfp *sfp = container_of(work, struct sfp, poll.work);
1647 
1648 	sfp_check_state(sfp);
1649 	mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
1650 }
1651 
1652 static struct sfp *sfp_alloc(struct device *dev)
1653 {
1654 	struct sfp *sfp;
1655 
1656 	sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
1657 	if (!sfp)
1658 		return ERR_PTR(-ENOMEM);
1659 
1660 	sfp->dev = dev;
1661 
1662 	mutex_init(&sfp->sm_mutex);
1663 	INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
1664 	INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
1665 
1666 	return sfp;
1667 }
1668 
1669 static void sfp_cleanup(void *data)
1670 {
1671 	struct sfp *sfp = data;
1672 
1673 	cancel_delayed_work_sync(&sfp->poll);
1674 	cancel_delayed_work_sync(&sfp->timeout);
1675 	if (sfp->i2c_mii) {
1676 		mdiobus_unregister(sfp->i2c_mii);
1677 		mdiobus_free(sfp->i2c_mii);
1678 	}
1679 	if (sfp->i2c)
1680 		i2c_put_adapter(sfp->i2c);
1681 	kfree(sfp);
1682 }
1683 
1684 static int sfp_probe(struct platform_device *pdev)
1685 {
1686 	const struct sff_data *sff;
1687 	struct sfp *sfp;
1688 	bool poll = false;
1689 	int irq, err, i;
1690 
1691 	sfp = sfp_alloc(&pdev->dev);
1692 	if (IS_ERR(sfp))
1693 		return PTR_ERR(sfp);
1694 
1695 	platform_set_drvdata(pdev, sfp);
1696 
1697 	err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
1698 	if (err < 0)
1699 		return err;
1700 
1701 	sff = sfp->type = &sfp_data;
1702 
1703 	if (pdev->dev.of_node) {
1704 		struct device_node *node = pdev->dev.of_node;
1705 		const struct of_device_id *id;
1706 		struct i2c_adapter *i2c;
1707 		struct device_node *np;
1708 
1709 		id = of_match_node(sfp_of_match, node);
1710 		if (WARN_ON(!id))
1711 			return -EINVAL;
1712 
1713 		sff = sfp->type = id->data;
1714 
1715 		np = of_parse_phandle(node, "i2c-bus", 0);
1716 		if (!np) {
1717 			dev_err(sfp->dev, "missing 'i2c-bus' property\n");
1718 			return -ENODEV;
1719 		}
1720 
1721 		i2c = of_find_i2c_adapter_by_node(np);
1722 		of_node_put(np);
1723 		if (!i2c)
1724 			return -EPROBE_DEFER;
1725 
1726 		err = sfp_i2c_configure(sfp, i2c);
1727 		if (err < 0) {
1728 			i2c_put_adapter(i2c);
1729 			return err;
1730 		}
1731 	}
1732 
1733 	for (i = 0; i < GPIO_MAX; i++)
1734 		if (sff->gpios & BIT(i)) {
1735 			sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
1736 					   gpio_of_names[i], gpio_flags[i]);
1737 			if (IS_ERR(sfp->gpio[i]))
1738 				return PTR_ERR(sfp->gpio[i]);
1739 		}
1740 
1741 	sfp->get_state = sfp_gpio_get_state;
1742 	sfp->set_state = sfp_gpio_set_state;
1743 
1744 	/* Modules that have no detect signal are always present */
1745 	if (!(sfp->gpio[GPIO_MODDEF0]))
1746 		sfp->get_state = sff_gpio_get_state;
1747 
1748 	device_property_read_u32(&pdev->dev, "maximum-power-milliwatt",
1749 				 &sfp->max_power_mW);
1750 	if (!sfp->max_power_mW)
1751 		sfp->max_power_mW = 1000;
1752 
1753 	dev_info(sfp->dev, "Host maximum power %u.%uW\n",
1754 		 sfp->max_power_mW / 1000, (sfp->max_power_mW / 100) % 10);
1755 
1756 	sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops);
1757 	if (!sfp->sfp_bus)
1758 		return -ENOMEM;
1759 
1760 	/* Get the initial state, and always signal TX disable,
1761 	 * since the network interface will not be up.
1762 	 */
1763 	sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE;
1764 
1765 	if (sfp->gpio[GPIO_RATE_SELECT] &&
1766 	    gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT]))
1767 		sfp->state |= SFP_F_RATE_SELECT;
1768 	sfp_set_state(sfp, sfp->state);
1769 	sfp_module_tx_disable(sfp);
1770 	rtnl_lock();
1771 	if (sfp->state & SFP_F_PRESENT)
1772 		sfp_sm_event(sfp, SFP_E_INSERT);
1773 	rtnl_unlock();
1774 
1775 	for (i = 0; i < GPIO_MAX; i++) {
1776 		if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
1777 			continue;
1778 
1779 		irq = gpiod_to_irq(sfp->gpio[i]);
1780 		if (!irq) {
1781 			poll = true;
1782 			continue;
1783 		}
1784 
1785 		err = devm_request_threaded_irq(sfp->dev, irq, NULL, sfp_irq,
1786 						IRQF_ONESHOT |
1787 						IRQF_TRIGGER_RISING |
1788 						IRQF_TRIGGER_FALLING,
1789 						dev_name(sfp->dev), sfp);
1790 		if (err)
1791 			poll = true;
1792 	}
1793 
1794 	if (poll)
1795 		mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
1796 
1797 	/* We could have an issue in cases no Tx disable pin is available or
1798 	 * wired as modules using a laser as their light source will continue to
1799 	 * be active when the fiber is removed. This could be a safety issue and
1800 	 * we should at least warn the user about that.
1801 	 */
1802 	if (!sfp->gpio[GPIO_TX_DISABLE])
1803 		dev_warn(sfp->dev,
1804 			 "No tx_disable pin: SFP modules will always be emitting.\n");
1805 
1806 	return 0;
1807 }
1808 
1809 static int sfp_remove(struct platform_device *pdev)
1810 {
1811 	struct sfp *sfp = platform_get_drvdata(pdev);
1812 
1813 	sfp_unregister_socket(sfp->sfp_bus);
1814 
1815 	return 0;
1816 }
1817 
1818 static struct platform_driver sfp_driver = {
1819 	.probe = sfp_probe,
1820 	.remove = sfp_remove,
1821 	.driver = {
1822 		.name = "sfp",
1823 		.of_match_table = sfp_of_match,
1824 	},
1825 };
1826 
1827 static int sfp_init(void)
1828 {
1829 	poll_jiffies = msecs_to_jiffies(100);
1830 
1831 	return platform_driver_register(&sfp_driver);
1832 }
1833 module_init(sfp_init);
1834 
1835 static void sfp_exit(void)
1836 {
1837 	platform_driver_unregister(&sfp_driver);
1838 }
1839 module_exit(sfp_exit);
1840 
1841 MODULE_ALIAS("platform:sfp");
1842 MODULE_AUTHOR("Russell King");
1843 MODULE_LICENSE("GPL v2");
1844