xref: /openbmc/linux/drivers/net/phy/sfp.c (revision 9a6b55ac)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/acpi.h>
3 #include <linux/ctype.h>
4 #include <linux/delay.h>
5 #include <linux/gpio/consumer.h>
6 #include <linux/hwmon.h>
7 #include <linux/i2c.h>
8 #include <linux/interrupt.h>
9 #include <linux/jiffies.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/of.h>
13 #include <linux/phy.h>
14 #include <linux/platform_device.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/slab.h>
17 #include <linux/workqueue.h>
18 
19 #include "mdio-i2c.h"
20 #include "sfp.h"
21 #include "swphy.h"
22 
23 enum {
24 	GPIO_MODDEF0,
25 	GPIO_LOS,
26 	GPIO_TX_FAULT,
27 	GPIO_TX_DISABLE,
28 	GPIO_RATE_SELECT,
29 	GPIO_MAX,
30 
31 	SFP_F_PRESENT = BIT(GPIO_MODDEF0),
32 	SFP_F_LOS = BIT(GPIO_LOS),
33 	SFP_F_TX_FAULT = BIT(GPIO_TX_FAULT),
34 	SFP_F_TX_DISABLE = BIT(GPIO_TX_DISABLE),
35 	SFP_F_RATE_SELECT = BIT(GPIO_RATE_SELECT),
36 
37 	SFP_E_INSERT = 0,
38 	SFP_E_REMOVE,
39 	SFP_E_DEV_ATTACH,
40 	SFP_E_DEV_DETACH,
41 	SFP_E_DEV_DOWN,
42 	SFP_E_DEV_UP,
43 	SFP_E_TX_FAULT,
44 	SFP_E_TX_CLEAR,
45 	SFP_E_LOS_HIGH,
46 	SFP_E_LOS_LOW,
47 	SFP_E_TIMEOUT,
48 
49 	SFP_MOD_EMPTY = 0,
50 	SFP_MOD_ERROR,
51 	SFP_MOD_PROBE,
52 	SFP_MOD_WAITDEV,
53 	SFP_MOD_HPOWER,
54 	SFP_MOD_WAITPWR,
55 	SFP_MOD_PRESENT,
56 
57 	SFP_DEV_DETACHED = 0,
58 	SFP_DEV_DOWN,
59 	SFP_DEV_UP,
60 
61 	SFP_S_DOWN = 0,
62 	SFP_S_WAIT,
63 	SFP_S_INIT,
64 	SFP_S_INIT_TX_FAULT,
65 	SFP_S_WAIT_LOS,
66 	SFP_S_LINK_UP,
67 	SFP_S_TX_FAULT,
68 	SFP_S_REINIT,
69 	SFP_S_TX_DISABLE,
70 };
71 
72 static const char  * const mod_state_strings[] = {
73 	[SFP_MOD_EMPTY] = "empty",
74 	[SFP_MOD_ERROR] = "error",
75 	[SFP_MOD_PROBE] = "probe",
76 	[SFP_MOD_WAITDEV] = "waitdev",
77 	[SFP_MOD_HPOWER] = "hpower",
78 	[SFP_MOD_WAITPWR] = "waitpwr",
79 	[SFP_MOD_PRESENT] = "present",
80 };
81 
82 static const char *mod_state_to_str(unsigned short mod_state)
83 {
84 	if (mod_state >= ARRAY_SIZE(mod_state_strings))
85 		return "Unknown module state";
86 	return mod_state_strings[mod_state];
87 }
88 
89 static const char * const dev_state_strings[] = {
90 	[SFP_DEV_DETACHED] = "detached",
91 	[SFP_DEV_DOWN] = "down",
92 	[SFP_DEV_UP] = "up",
93 };
94 
95 static const char *dev_state_to_str(unsigned short dev_state)
96 {
97 	if (dev_state >= ARRAY_SIZE(dev_state_strings))
98 		return "Unknown device state";
99 	return dev_state_strings[dev_state];
100 }
101 
102 static const char * const event_strings[] = {
103 	[SFP_E_INSERT] = "insert",
104 	[SFP_E_REMOVE] = "remove",
105 	[SFP_E_DEV_ATTACH] = "dev_attach",
106 	[SFP_E_DEV_DETACH] = "dev_detach",
107 	[SFP_E_DEV_DOWN] = "dev_down",
108 	[SFP_E_DEV_UP] = "dev_up",
109 	[SFP_E_TX_FAULT] = "tx_fault",
110 	[SFP_E_TX_CLEAR] = "tx_clear",
111 	[SFP_E_LOS_HIGH] = "los_high",
112 	[SFP_E_LOS_LOW] = "los_low",
113 	[SFP_E_TIMEOUT] = "timeout",
114 };
115 
116 static const char *event_to_str(unsigned short event)
117 {
118 	if (event >= ARRAY_SIZE(event_strings))
119 		return "Unknown event";
120 	return event_strings[event];
121 }
122 
123 static const char * const sm_state_strings[] = {
124 	[SFP_S_DOWN] = "down",
125 	[SFP_S_WAIT] = "wait",
126 	[SFP_S_INIT] = "init",
127 	[SFP_S_INIT_TX_FAULT] = "init_tx_fault",
128 	[SFP_S_WAIT_LOS] = "wait_los",
129 	[SFP_S_LINK_UP] = "link_up",
130 	[SFP_S_TX_FAULT] = "tx_fault",
131 	[SFP_S_REINIT] = "reinit",
132 	[SFP_S_TX_DISABLE] = "rx_disable",
133 };
134 
135 static const char *sm_state_to_str(unsigned short sm_state)
136 {
137 	if (sm_state >= ARRAY_SIZE(sm_state_strings))
138 		return "Unknown state";
139 	return sm_state_strings[sm_state];
140 }
141 
142 static const char *gpio_of_names[] = {
143 	"mod-def0",
144 	"los",
145 	"tx-fault",
146 	"tx-disable",
147 	"rate-select0",
148 };
149 
150 static const enum gpiod_flags gpio_flags[] = {
151 	GPIOD_IN,
152 	GPIOD_IN,
153 	GPIOD_IN,
154 	GPIOD_ASIS,
155 	GPIOD_ASIS,
156 };
157 
158 #define T_WAIT		msecs_to_jiffies(50)
159 #define T_INIT_JIFFIES	msecs_to_jiffies(300)
160 #define T_RESET_US	10
161 #define T_FAULT_RECOVER	msecs_to_jiffies(1000)
162 
163 /* SFP module presence detection is poor: the three MOD DEF signals are
164  * the same length on the PCB, which means it's possible for MOD DEF 0 to
165  * connect before the I2C bus on MOD DEF 1/2.
166  *
167  * The SFF-8472 specifies t_serial ("Time from power on until module is
168  * ready for data transmission over the two wire serial bus.") as 300ms.
169  */
170 #define T_SERIAL		msecs_to_jiffies(300)
171 #define T_HPOWER_LEVEL		msecs_to_jiffies(300)
172 #define T_PROBE_RETRY_INIT	msecs_to_jiffies(100)
173 #define R_PROBE_RETRY_INIT	10
174 #define T_PROBE_RETRY_SLOW	msecs_to_jiffies(5000)
175 #define R_PROBE_RETRY_SLOW	12
176 
177 /* SFP modules appear to always have their PHY configured for bus address
178  * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
179  */
180 #define SFP_PHY_ADDR	22
181 
182 struct sff_data {
183 	unsigned int gpios;
184 	bool (*module_supported)(const struct sfp_eeprom_id *id);
185 };
186 
187 struct sfp {
188 	struct device *dev;
189 	struct i2c_adapter *i2c;
190 	struct mii_bus *i2c_mii;
191 	struct sfp_bus *sfp_bus;
192 	struct phy_device *mod_phy;
193 	const struct sff_data *type;
194 	u32 max_power_mW;
195 
196 	unsigned int (*get_state)(struct sfp *);
197 	void (*set_state)(struct sfp *, unsigned int);
198 	int (*read)(struct sfp *, bool, u8, void *, size_t);
199 	int (*write)(struct sfp *, bool, u8, void *, size_t);
200 
201 	struct gpio_desc *gpio[GPIO_MAX];
202 	int gpio_irq[GPIO_MAX];
203 
204 	bool need_poll;
205 
206 	struct mutex st_mutex;			/* Protects state */
207 	unsigned int state_soft_mask;
208 	unsigned int state;
209 	struct delayed_work poll;
210 	struct delayed_work timeout;
211 	struct mutex sm_mutex;			/* Protects state machine */
212 	unsigned char sm_mod_state;
213 	unsigned char sm_mod_tries_init;
214 	unsigned char sm_mod_tries;
215 	unsigned char sm_dev_state;
216 	unsigned short sm_state;
217 	unsigned int sm_retries;
218 
219 	struct sfp_eeprom_id id;
220 	unsigned int module_power_mW;
221 
222 #if IS_ENABLED(CONFIG_HWMON)
223 	struct sfp_diag diag;
224 	struct delayed_work hwmon_probe;
225 	unsigned int hwmon_tries;
226 	struct device *hwmon_dev;
227 	char *hwmon_name;
228 #endif
229 
230 };
231 
232 static bool sff_module_supported(const struct sfp_eeprom_id *id)
233 {
234 	return id->base.phys_id == SFP_PHYS_ID_SFF &&
235 	       id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
236 }
237 
238 static const struct sff_data sff_data = {
239 	.gpios = SFP_F_LOS | SFP_F_TX_FAULT | SFP_F_TX_DISABLE,
240 	.module_supported = sff_module_supported,
241 };
242 
243 static bool sfp_module_supported(const struct sfp_eeprom_id *id)
244 {
245 	return id->base.phys_id == SFP_PHYS_ID_SFP &&
246 	       id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
247 }
248 
249 static const struct sff_data sfp_data = {
250 	.gpios = SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT |
251 		 SFP_F_TX_DISABLE | SFP_F_RATE_SELECT,
252 	.module_supported = sfp_module_supported,
253 };
254 
255 static const struct of_device_id sfp_of_match[] = {
256 	{ .compatible = "sff,sff", .data = &sff_data, },
257 	{ .compatible = "sff,sfp", .data = &sfp_data, },
258 	{ },
259 };
260 MODULE_DEVICE_TABLE(of, sfp_of_match);
261 
262 static unsigned long poll_jiffies;
263 
264 static unsigned int sfp_gpio_get_state(struct sfp *sfp)
265 {
266 	unsigned int i, state, v;
267 
268 	for (i = state = 0; i < GPIO_MAX; i++) {
269 		if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
270 			continue;
271 
272 		v = gpiod_get_value_cansleep(sfp->gpio[i]);
273 		if (v)
274 			state |= BIT(i);
275 	}
276 
277 	return state;
278 }
279 
280 static unsigned int sff_gpio_get_state(struct sfp *sfp)
281 {
282 	return sfp_gpio_get_state(sfp) | SFP_F_PRESENT;
283 }
284 
285 static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
286 {
287 	if (state & SFP_F_PRESENT) {
288 		/* If the module is present, drive the signals */
289 		if (sfp->gpio[GPIO_TX_DISABLE])
290 			gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
291 					       state & SFP_F_TX_DISABLE);
292 		if (state & SFP_F_RATE_SELECT)
293 			gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT],
294 					       state & SFP_F_RATE_SELECT);
295 	} else {
296 		/* Otherwise, let them float to the pull-ups */
297 		if (sfp->gpio[GPIO_TX_DISABLE])
298 			gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
299 		if (state & SFP_F_RATE_SELECT)
300 			gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]);
301 	}
302 }
303 
304 static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
305 			size_t len)
306 {
307 	struct i2c_msg msgs[2];
308 	u8 bus_addr = a2 ? 0x51 : 0x50;
309 	size_t this_len;
310 	int ret;
311 
312 	msgs[0].addr = bus_addr;
313 	msgs[0].flags = 0;
314 	msgs[0].len = 1;
315 	msgs[0].buf = &dev_addr;
316 	msgs[1].addr = bus_addr;
317 	msgs[1].flags = I2C_M_RD;
318 	msgs[1].len = len;
319 	msgs[1].buf = buf;
320 
321 	while (len) {
322 		this_len = len;
323 		if (this_len > 16)
324 			this_len = 16;
325 
326 		msgs[1].len = this_len;
327 
328 		ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
329 		if (ret < 0)
330 			return ret;
331 
332 		if (ret != ARRAY_SIZE(msgs))
333 			break;
334 
335 		msgs[1].buf += this_len;
336 		dev_addr += this_len;
337 		len -= this_len;
338 	}
339 
340 	return msgs[1].buf - (u8 *)buf;
341 }
342 
343 static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
344 	size_t len)
345 {
346 	struct i2c_msg msgs[1];
347 	u8 bus_addr = a2 ? 0x51 : 0x50;
348 	int ret;
349 
350 	msgs[0].addr = bus_addr;
351 	msgs[0].flags = 0;
352 	msgs[0].len = 1 + len;
353 	msgs[0].buf = kmalloc(1 + len, GFP_KERNEL);
354 	if (!msgs[0].buf)
355 		return -ENOMEM;
356 
357 	msgs[0].buf[0] = dev_addr;
358 	memcpy(&msgs[0].buf[1], buf, len);
359 
360 	ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
361 
362 	kfree(msgs[0].buf);
363 
364 	if (ret < 0)
365 		return ret;
366 
367 	return ret == ARRAY_SIZE(msgs) ? len : 0;
368 }
369 
370 static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
371 {
372 	struct mii_bus *i2c_mii;
373 	int ret;
374 
375 	if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
376 		return -EINVAL;
377 
378 	sfp->i2c = i2c;
379 	sfp->read = sfp_i2c_read;
380 	sfp->write = sfp_i2c_write;
381 
382 	i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
383 	if (IS_ERR(i2c_mii))
384 		return PTR_ERR(i2c_mii);
385 
386 	i2c_mii->name = "SFP I2C Bus";
387 	i2c_mii->phy_mask = ~0;
388 
389 	ret = mdiobus_register(i2c_mii);
390 	if (ret < 0) {
391 		mdiobus_free(i2c_mii);
392 		return ret;
393 	}
394 
395 	sfp->i2c_mii = i2c_mii;
396 
397 	return 0;
398 }
399 
400 /* Interface */
401 static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
402 {
403 	return sfp->read(sfp, a2, addr, buf, len);
404 }
405 
406 static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
407 {
408 	return sfp->write(sfp, a2, addr, buf, len);
409 }
410 
411 static unsigned int sfp_soft_get_state(struct sfp *sfp)
412 {
413 	unsigned int state = 0;
414 	u8 status;
415 
416 	if (sfp_read(sfp, true, SFP_STATUS, &status, sizeof(status)) ==
417 		     sizeof(status)) {
418 		if (status & SFP_STATUS_RX_LOS)
419 			state |= SFP_F_LOS;
420 		if (status & SFP_STATUS_TX_FAULT)
421 			state |= SFP_F_TX_FAULT;
422 	}
423 
424 	return state & sfp->state_soft_mask;
425 }
426 
427 static void sfp_soft_set_state(struct sfp *sfp, unsigned int state)
428 {
429 	u8 status;
430 
431 	if (sfp_read(sfp, true, SFP_STATUS, &status, sizeof(status)) ==
432 		     sizeof(status)) {
433 		if (state & SFP_F_TX_DISABLE)
434 			status |= SFP_STATUS_TX_DISABLE_FORCE;
435 		else
436 			status &= ~SFP_STATUS_TX_DISABLE_FORCE;
437 
438 		sfp_write(sfp, true, SFP_STATUS, &status, sizeof(status));
439 	}
440 }
441 
442 static void sfp_soft_start_poll(struct sfp *sfp)
443 {
444 	const struct sfp_eeprom_id *id = &sfp->id;
445 
446 	sfp->state_soft_mask = 0;
447 	if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_DISABLE &&
448 	    !sfp->gpio[GPIO_TX_DISABLE])
449 		sfp->state_soft_mask |= SFP_F_TX_DISABLE;
450 	if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_FAULT &&
451 	    !sfp->gpio[GPIO_TX_FAULT])
452 		sfp->state_soft_mask |= SFP_F_TX_FAULT;
453 	if (id->ext.enhopts & SFP_ENHOPTS_SOFT_RX_LOS &&
454 	    !sfp->gpio[GPIO_LOS])
455 		sfp->state_soft_mask |= SFP_F_LOS;
456 
457 	if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) &&
458 	    !sfp->need_poll)
459 		mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
460 }
461 
462 static void sfp_soft_stop_poll(struct sfp *sfp)
463 {
464 	sfp->state_soft_mask = 0;
465 }
466 
467 static unsigned int sfp_get_state(struct sfp *sfp)
468 {
469 	unsigned int state = sfp->get_state(sfp);
470 
471 	if (state & SFP_F_PRESENT &&
472 	    sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT))
473 		state |= sfp_soft_get_state(sfp);
474 
475 	return state;
476 }
477 
478 static void sfp_set_state(struct sfp *sfp, unsigned int state)
479 {
480 	sfp->set_state(sfp, state);
481 
482 	if (state & SFP_F_PRESENT &&
483 	    sfp->state_soft_mask & SFP_F_TX_DISABLE)
484 		sfp_soft_set_state(sfp, state);
485 }
486 
487 static unsigned int sfp_check(void *buf, size_t len)
488 {
489 	u8 *p, check;
490 
491 	for (p = buf, check = 0; len; p++, len--)
492 		check += *p;
493 
494 	return check;
495 }
496 
497 /* hwmon */
498 #if IS_ENABLED(CONFIG_HWMON)
499 static umode_t sfp_hwmon_is_visible(const void *data,
500 				    enum hwmon_sensor_types type,
501 				    u32 attr, int channel)
502 {
503 	const struct sfp *sfp = data;
504 
505 	switch (type) {
506 	case hwmon_temp:
507 		switch (attr) {
508 		case hwmon_temp_min_alarm:
509 		case hwmon_temp_max_alarm:
510 		case hwmon_temp_lcrit_alarm:
511 		case hwmon_temp_crit_alarm:
512 		case hwmon_temp_min:
513 		case hwmon_temp_max:
514 		case hwmon_temp_lcrit:
515 		case hwmon_temp_crit:
516 			if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
517 				return 0;
518 			/* fall through */
519 		case hwmon_temp_input:
520 		case hwmon_temp_label:
521 			return 0444;
522 		default:
523 			return 0;
524 		}
525 	case hwmon_in:
526 		switch (attr) {
527 		case hwmon_in_min_alarm:
528 		case hwmon_in_max_alarm:
529 		case hwmon_in_lcrit_alarm:
530 		case hwmon_in_crit_alarm:
531 		case hwmon_in_min:
532 		case hwmon_in_max:
533 		case hwmon_in_lcrit:
534 		case hwmon_in_crit:
535 			if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
536 				return 0;
537 			/* fall through */
538 		case hwmon_in_input:
539 		case hwmon_in_label:
540 			return 0444;
541 		default:
542 			return 0;
543 		}
544 	case hwmon_curr:
545 		switch (attr) {
546 		case hwmon_curr_min_alarm:
547 		case hwmon_curr_max_alarm:
548 		case hwmon_curr_lcrit_alarm:
549 		case hwmon_curr_crit_alarm:
550 		case hwmon_curr_min:
551 		case hwmon_curr_max:
552 		case hwmon_curr_lcrit:
553 		case hwmon_curr_crit:
554 			if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
555 				return 0;
556 			/* fall through */
557 		case hwmon_curr_input:
558 		case hwmon_curr_label:
559 			return 0444;
560 		default:
561 			return 0;
562 		}
563 	case hwmon_power:
564 		/* External calibration of receive power requires
565 		 * floating point arithmetic. Doing that in the kernel
566 		 * is not easy, so just skip it. If the module does
567 		 * not require external calibration, we can however
568 		 * show receiver power, since FP is then not needed.
569 		 */
570 		if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL &&
571 		    channel == 1)
572 			return 0;
573 		switch (attr) {
574 		case hwmon_power_min_alarm:
575 		case hwmon_power_max_alarm:
576 		case hwmon_power_lcrit_alarm:
577 		case hwmon_power_crit_alarm:
578 		case hwmon_power_min:
579 		case hwmon_power_max:
580 		case hwmon_power_lcrit:
581 		case hwmon_power_crit:
582 			if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
583 				return 0;
584 			/* fall through */
585 		case hwmon_power_input:
586 		case hwmon_power_label:
587 			return 0444;
588 		default:
589 			return 0;
590 		}
591 	default:
592 		return 0;
593 	}
594 }
595 
596 static int sfp_hwmon_read_sensor(struct sfp *sfp, int reg, long *value)
597 {
598 	__be16 val;
599 	int err;
600 
601 	err = sfp_read(sfp, true, reg, &val, sizeof(val));
602 	if (err < 0)
603 		return err;
604 
605 	*value = be16_to_cpu(val);
606 
607 	return 0;
608 }
609 
610 static void sfp_hwmon_to_rx_power(long *value)
611 {
612 	*value = DIV_ROUND_CLOSEST(*value, 10);
613 }
614 
615 static void sfp_hwmon_calibrate(struct sfp *sfp, unsigned int slope, int offset,
616 				long *value)
617 {
618 	if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL)
619 		*value = DIV_ROUND_CLOSEST(*value * slope, 256) + offset;
620 }
621 
622 static void sfp_hwmon_calibrate_temp(struct sfp *sfp, long *value)
623 {
624 	sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_t_slope),
625 			    be16_to_cpu(sfp->diag.cal_t_offset), value);
626 
627 	if (*value >= 0x8000)
628 		*value -= 0x10000;
629 
630 	*value = DIV_ROUND_CLOSEST(*value * 1000, 256);
631 }
632 
633 static void sfp_hwmon_calibrate_vcc(struct sfp *sfp, long *value)
634 {
635 	sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_v_slope),
636 			    be16_to_cpu(sfp->diag.cal_v_offset), value);
637 
638 	*value = DIV_ROUND_CLOSEST(*value, 10);
639 }
640 
641 static void sfp_hwmon_calibrate_bias(struct sfp *sfp, long *value)
642 {
643 	sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txi_slope),
644 			    be16_to_cpu(sfp->diag.cal_txi_offset), value);
645 
646 	*value = DIV_ROUND_CLOSEST(*value, 500);
647 }
648 
649 static void sfp_hwmon_calibrate_tx_power(struct sfp *sfp, long *value)
650 {
651 	sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txpwr_slope),
652 			    be16_to_cpu(sfp->diag.cal_txpwr_offset), value);
653 
654 	*value = DIV_ROUND_CLOSEST(*value, 10);
655 }
656 
657 static int sfp_hwmon_read_temp(struct sfp *sfp, int reg, long *value)
658 {
659 	int err;
660 
661 	err = sfp_hwmon_read_sensor(sfp, reg, value);
662 	if (err < 0)
663 		return err;
664 
665 	sfp_hwmon_calibrate_temp(sfp, value);
666 
667 	return 0;
668 }
669 
670 static int sfp_hwmon_read_vcc(struct sfp *sfp, int reg, long *value)
671 {
672 	int err;
673 
674 	err = sfp_hwmon_read_sensor(sfp, reg, value);
675 	if (err < 0)
676 		return err;
677 
678 	sfp_hwmon_calibrate_vcc(sfp, value);
679 
680 	return 0;
681 }
682 
683 static int sfp_hwmon_read_bias(struct sfp *sfp, int reg, long *value)
684 {
685 	int err;
686 
687 	err = sfp_hwmon_read_sensor(sfp, reg, value);
688 	if (err < 0)
689 		return err;
690 
691 	sfp_hwmon_calibrate_bias(sfp, value);
692 
693 	return 0;
694 }
695 
696 static int sfp_hwmon_read_tx_power(struct sfp *sfp, int reg, long *value)
697 {
698 	int err;
699 
700 	err = sfp_hwmon_read_sensor(sfp, reg, value);
701 	if (err < 0)
702 		return err;
703 
704 	sfp_hwmon_calibrate_tx_power(sfp, value);
705 
706 	return 0;
707 }
708 
709 static int sfp_hwmon_read_rx_power(struct sfp *sfp, int reg, long *value)
710 {
711 	int err;
712 
713 	err = sfp_hwmon_read_sensor(sfp, reg, value);
714 	if (err < 0)
715 		return err;
716 
717 	sfp_hwmon_to_rx_power(value);
718 
719 	return 0;
720 }
721 
722 static int sfp_hwmon_temp(struct sfp *sfp, u32 attr, long *value)
723 {
724 	u8 status;
725 	int err;
726 
727 	switch (attr) {
728 	case hwmon_temp_input:
729 		return sfp_hwmon_read_temp(sfp, SFP_TEMP, value);
730 
731 	case hwmon_temp_lcrit:
732 		*value = be16_to_cpu(sfp->diag.temp_low_alarm);
733 		sfp_hwmon_calibrate_temp(sfp, value);
734 		return 0;
735 
736 	case hwmon_temp_min:
737 		*value = be16_to_cpu(sfp->diag.temp_low_warn);
738 		sfp_hwmon_calibrate_temp(sfp, value);
739 		return 0;
740 	case hwmon_temp_max:
741 		*value = be16_to_cpu(sfp->diag.temp_high_warn);
742 		sfp_hwmon_calibrate_temp(sfp, value);
743 		return 0;
744 
745 	case hwmon_temp_crit:
746 		*value = be16_to_cpu(sfp->diag.temp_high_alarm);
747 		sfp_hwmon_calibrate_temp(sfp, value);
748 		return 0;
749 
750 	case hwmon_temp_lcrit_alarm:
751 		err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
752 		if (err < 0)
753 			return err;
754 
755 		*value = !!(status & SFP_ALARM0_TEMP_LOW);
756 		return 0;
757 
758 	case hwmon_temp_min_alarm:
759 		err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
760 		if (err < 0)
761 			return err;
762 
763 		*value = !!(status & SFP_WARN0_TEMP_LOW);
764 		return 0;
765 
766 	case hwmon_temp_max_alarm:
767 		err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
768 		if (err < 0)
769 			return err;
770 
771 		*value = !!(status & SFP_WARN0_TEMP_HIGH);
772 		return 0;
773 
774 	case hwmon_temp_crit_alarm:
775 		err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
776 		if (err < 0)
777 			return err;
778 
779 		*value = !!(status & SFP_ALARM0_TEMP_HIGH);
780 		return 0;
781 	default:
782 		return -EOPNOTSUPP;
783 	}
784 
785 	return -EOPNOTSUPP;
786 }
787 
788 static int sfp_hwmon_vcc(struct sfp *sfp, u32 attr, long *value)
789 {
790 	u8 status;
791 	int err;
792 
793 	switch (attr) {
794 	case hwmon_in_input:
795 		return sfp_hwmon_read_vcc(sfp, SFP_VCC, value);
796 
797 	case hwmon_in_lcrit:
798 		*value = be16_to_cpu(sfp->diag.volt_low_alarm);
799 		sfp_hwmon_calibrate_vcc(sfp, value);
800 		return 0;
801 
802 	case hwmon_in_min:
803 		*value = be16_to_cpu(sfp->diag.volt_low_warn);
804 		sfp_hwmon_calibrate_vcc(sfp, value);
805 		return 0;
806 
807 	case hwmon_in_max:
808 		*value = be16_to_cpu(sfp->diag.volt_high_warn);
809 		sfp_hwmon_calibrate_vcc(sfp, value);
810 		return 0;
811 
812 	case hwmon_in_crit:
813 		*value = be16_to_cpu(sfp->diag.volt_high_alarm);
814 		sfp_hwmon_calibrate_vcc(sfp, value);
815 		return 0;
816 
817 	case hwmon_in_lcrit_alarm:
818 		err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
819 		if (err < 0)
820 			return err;
821 
822 		*value = !!(status & SFP_ALARM0_VCC_LOW);
823 		return 0;
824 
825 	case hwmon_in_min_alarm:
826 		err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
827 		if (err < 0)
828 			return err;
829 
830 		*value = !!(status & SFP_WARN0_VCC_LOW);
831 		return 0;
832 
833 	case hwmon_in_max_alarm:
834 		err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
835 		if (err < 0)
836 			return err;
837 
838 		*value = !!(status & SFP_WARN0_VCC_HIGH);
839 		return 0;
840 
841 	case hwmon_in_crit_alarm:
842 		err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
843 		if (err < 0)
844 			return err;
845 
846 		*value = !!(status & SFP_ALARM0_VCC_HIGH);
847 		return 0;
848 	default:
849 		return -EOPNOTSUPP;
850 	}
851 
852 	return -EOPNOTSUPP;
853 }
854 
855 static int sfp_hwmon_bias(struct sfp *sfp, u32 attr, long *value)
856 {
857 	u8 status;
858 	int err;
859 
860 	switch (attr) {
861 	case hwmon_curr_input:
862 		return sfp_hwmon_read_bias(sfp, SFP_TX_BIAS, value);
863 
864 	case hwmon_curr_lcrit:
865 		*value = be16_to_cpu(sfp->diag.bias_low_alarm);
866 		sfp_hwmon_calibrate_bias(sfp, value);
867 		return 0;
868 
869 	case hwmon_curr_min:
870 		*value = be16_to_cpu(sfp->diag.bias_low_warn);
871 		sfp_hwmon_calibrate_bias(sfp, value);
872 		return 0;
873 
874 	case hwmon_curr_max:
875 		*value = be16_to_cpu(sfp->diag.bias_high_warn);
876 		sfp_hwmon_calibrate_bias(sfp, value);
877 		return 0;
878 
879 	case hwmon_curr_crit:
880 		*value = be16_to_cpu(sfp->diag.bias_high_alarm);
881 		sfp_hwmon_calibrate_bias(sfp, value);
882 		return 0;
883 
884 	case hwmon_curr_lcrit_alarm:
885 		err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
886 		if (err < 0)
887 			return err;
888 
889 		*value = !!(status & SFP_ALARM0_TX_BIAS_LOW);
890 		return 0;
891 
892 	case hwmon_curr_min_alarm:
893 		err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
894 		if (err < 0)
895 			return err;
896 
897 		*value = !!(status & SFP_WARN0_TX_BIAS_LOW);
898 		return 0;
899 
900 	case hwmon_curr_max_alarm:
901 		err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
902 		if (err < 0)
903 			return err;
904 
905 		*value = !!(status & SFP_WARN0_TX_BIAS_HIGH);
906 		return 0;
907 
908 	case hwmon_curr_crit_alarm:
909 		err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
910 		if (err < 0)
911 			return err;
912 
913 		*value = !!(status & SFP_ALARM0_TX_BIAS_HIGH);
914 		return 0;
915 	default:
916 		return -EOPNOTSUPP;
917 	}
918 
919 	return -EOPNOTSUPP;
920 }
921 
922 static int sfp_hwmon_tx_power(struct sfp *sfp, u32 attr, long *value)
923 {
924 	u8 status;
925 	int err;
926 
927 	switch (attr) {
928 	case hwmon_power_input:
929 		return sfp_hwmon_read_tx_power(sfp, SFP_TX_POWER, value);
930 
931 	case hwmon_power_lcrit:
932 		*value = be16_to_cpu(sfp->diag.txpwr_low_alarm);
933 		sfp_hwmon_calibrate_tx_power(sfp, value);
934 		return 0;
935 
936 	case hwmon_power_min:
937 		*value = be16_to_cpu(sfp->diag.txpwr_low_warn);
938 		sfp_hwmon_calibrate_tx_power(sfp, value);
939 		return 0;
940 
941 	case hwmon_power_max:
942 		*value = be16_to_cpu(sfp->diag.txpwr_high_warn);
943 		sfp_hwmon_calibrate_tx_power(sfp, value);
944 		return 0;
945 
946 	case hwmon_power_crit:
947 		*value = be16_to_cpu(sfp->diag.txpwr_high_alarm);
948 		sfp_hwmon_calibrate_tx_power(sfp, value);
949 		return 0;
950 
951 	case hwmon_power_lcrit_alarm:
952 		err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
953 		if (err < 0)
954 			return err;
955 
956 		*value = !!(status & SFP_ALARM0_TXPWR_LOW);
957 		return 0;
958 
959 	case hwmon_power_min_alarm:
960 		err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
961 		if (err < 0)
962 			return err;
963 
964 		*value = !!(status & SFP_WARN0_TXPWR_LOW);
965 		return 0;
966 
967 	case hwmon_power_max_alarm:
968 		err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
969 		if (err < 0)
970 			return err;
971 
972 		*value = !!(status & SFP_WARN0_TXPWR_HIGH);
973 		return 0;
974 
975 	case hwmon_power_crit_alarm:
976 		err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
977 		if (err < 0)
978 			return err;
979 
980 		*value = !!(status & SFP_ALARM0_TXPWR_HIGH);
981 		return 0;
982 	default:
983 		return -EOPNOTSUPP;
984 	}
985 
986 	return -EOPNOTSUPP;
987 }
988 
989 static int sfp_hwmon_rx_power(struct sfp *sfp, u32 attr, long *value)
990 {
991 	u8 status;
992 	int err;
993 
994 	switch (attr) {
995 	case hwmon_power_input:
996 		return sfp_hwmon_read_rx_power(sfp, SFP_RX_POWER, value);
997 
998 	case hwmon_power_lcrit:
999 		*value = be16_to_cpu(sfp->diag.rxpwr_low_alarm);
1000 		sfp_hwmon_to_rx_power(value);
1001 		return 0;
1002 
1003 	case hwmon_power_min:
1004 		*value = be16_to_cpu(sfp->diag.rxpwr_low_warn);
1005 		sfp_hwmon_to_rx_power(value);
1006 		return 0;
1007 
1008 	case hwmon_power_max:
1009 		*value = be16_to_cpu(sfp->diag.rxpwr_high_warn);
1010 		sfp_hwmon_to_rx_power(value);
1011 		return 0;
1012 
1013 	case hwmon_power_crit:
1014 		*value = be16_to_cpu(sfp->diag.rxpwr_high_alarm);
1015 		sfp_hwmon_to_rx_power(value);
1016 		return 0;
1017 
1018 	case hwmon_power_lcrit_alarm:
1019 		err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
1020 		if (err < 0)
1021 			return err;
1022 
1023 		*value = !!(status & SFP_ALARM1_RXPWR_LOW);
1024 		return 0;
1025 
1026 	case hwmon_power_min_alarm:
1027 		err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
1028 		if (err < 0)
1029 			return err;
1030 
1031 		*value = !!(status & SFP_WARN1_RXPWR_LOW);
1032 		return 0;
1033 
1034 	case hwmon_power_max_alarm:
1035 		err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
1036 		if (err < 0)
1037 			return err;
1038 
1039 		*value = !!(status & SFP_WARN1_RXPWR_HIGH);
1040 		return 0;
1041 
1042 	case hwmon_power_crit_alarm:
1043 		err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
1044 		if (err < 0)
1045 			return err;
1046 
1047 		*value = !!(status & SFP_ALARM1_RXPWR_HIGH);
1048 		return 0;
1049 	default:
1050 		return -EOPNOTSUPP;
1051 	}
1052 
1053 	return -EOPNOTSUPP;
1054 }
1055 
1056 static int sfp_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
1057 			  u32 attr, int channel, long *value)
1058 {
1059 	struct sfp *sfp = dev_get_drvdata(dev);
1060 
1061 	switch (type) {
1062 	case hwmon_temp:
1063 		return sfp_hwmon_temp(sfp, attr, value);
1064 	case hwmon_in:
1065 		return sfp_hwmon_vcc(sfp, attr, value);
1066 	case hwmon_curr:
1067 		return sfp_hwmon_bias(sfp, attr, value);
1068 	case hwmon_power:
1069 		switch (channel) {
1070 		case 0:
1071 			return sfp_hwmon_tx_power(sfp, attr, value);
1072 		case 1:
1073 			return sfp_hwmon_rx_power(sfp, attr, value);
1074 		default:
1075 			return -EOPNOTSUPP;
1076 		}
1077 	default:
1078 		return -EOPNOTSUPP;
1079 	}
1080 }
1081 
1082 static const char *const sfp_hwmon_power_labels[] = {
1083 	"TX_power",
1084 	"RX_power",
1085 };
1086 
1087 static int sfp_hwmon_read_string(struct device *dev,
1088 				 enum hwmon_sensor_types type,
1089 				 u32 attr, int channel, const char **str)
1090 {
1091 	switch (type) {
1092 	case hwmon_curr:
1093 		switch (attr) {
1094 		case hwmon_curr_label:
1095 			*str = "bias";
1096 			return 0;
1097 		default:
1098 			return -EOPNOTSUPP;
1099 		}
1100 		break;
1101 	case hwmon_temp:
1102 		switch (attr) {
1103 		case hwmon_temp_label:
1104 			*str = "temperature";
1105 			return 0;
1106 		default:
1107 			return -EOPNOTSUPP;
1108 		}
1109 		break;
1110 	case hwmon_in:
1111 		switch (attr) {
1112 		case hwmon_in_label:
1113 			*str = "VCC";
1114 			return 0;
1115 		default:
1116 			return -EOPNOTSUPP;
1117 		}
1118 		break;
1119 	case hwmon_power:
1120 		switch (attr) {
1121 		case hwmon_power_label:
1122 			*str = sfp_hwmon_power_labels[channel];
1123 			return 0;
1124 		default:
1125 			return -EOPNOTSUPP;
1126 		}
1127 		break;
1128 	default:
1129 		return -EOPNOTSUPP;
1130 	}
1131 
1132 	return -EOPNOTSUPP;
1133 }
1134 
1135 static const struct hwmon_ops sfp_hwmon_ops = {
1136 	.is_visible = sfp_hwmon_is_visible,
1137 	.read = sfp_hwmon_read,
1138 	.read_string = sfp_hwmon_read_string,
1139 };
1140 
1141 static u32 sfp_hwmon_chip_config[] = {
1142 	HWMON_C_REGISTER_TZ,
1143 	0,
1144 };
1145 
1146 static const struct hwmon_channel_info sfp_hwmon_chip = {
1147 	.type = hwmon_chip,
1148 	.config = sfp_hwmon_chip_config,
1149 };
1150 
1151 static u32 sfp_hwmon_temp_config[] = {
1152 	HWMON_T_INPUT |
1153 	HWMON_T_MAX | HWMON_T_MIN |
1154 	HWMON_T_MAX_ALARM | HWMON_T_MIN_ALARM |
1155 	HWMON_T_CRIT | HWMON_T_LCRIT |
1156 	HWMON_T_CRIT_ALARM | HWMON_T_LCRIT_ALARM |
1157 	HWMON_T_LABEL,
1158 	0,
1159 };
1160 
1161 static const struct hwmon_channel_info sfp_hwmon_temp_channel_info = {
1162 	.type = hwmon_temp,
1163 	.config = sfp_hwmon_temp_config,
1164 };
1165 
1166 static u32 sfp_hwmon_vcc_config[] = {
1167 	HWMON_I_INPUT |
1168 	HWMON_I_MAX | HWMON_I_MIN |
1169 	HWMON_I_MAX_ALARM | HWMON_I_MIN_ALARM |
1170 	HWMON_I_CRIT | HWMON_I_LCRIT |
1171 	HWMON_I_CRIT_ALARM | HWMON_I_LCRIT_ALARM |
1172 	HWMON_I_LABEL,
1173 	0,
1174 };
1175 
1176 static const struct hwmon_channel_info sfp_hwmon_vcc_channel_info = {
1177 	.type = hwmon_in,
1178 	.config = sfp_hwmon_vcc_config,
1179 };
1180 
1181 static u32 sfp_hwmon_bias_config[] = {
1182 	HWMON_C_INPUT |
1183 	HWMON_C_MAX | HWMON_C_MIN |
1184 	HWMON_C_MAX_ALARM | HWMON_C_MIN_ALARM |
1185 	HWMON_C_CRIT | HWMON_C_LCRIT |
1186 	HWMON_C_CRIT_ALARM | HWMON_C_LCRIT_ALARM |
1187 	HWMON_C_LABEL,
1188 	0,
1189 };
1190 
1191 static const struct hwmon_channel_info sfp_hwmon_bias_channel_info = {
1192 	.type = hwmon_curr,
1193 	.config = sfp_hwmon_bias_config,
1194 };
1195 
1196 static u32 sfp_hwmon_power_config[] = {
1197 	/* Transmit power */
1198 	HWMON_P_INPUT |
1199 	HWMON_P_MAX | HWMON_P_MIN |
1200 	HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
1201 	HWMON_P_CRIT | HWMON_P_LCRIT |
1202 	HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM |
1203 	HWMON_P_LABEL,
1204 	/* Receive power */
1205 	HWMON_P_INPUT |
1206 	HWMON_P_MAX | HWMON_P_MIN |
1207 	HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
1208 	HWMON_P_CRIT | HWMON_P_LCRIT |
1209 	HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM |
1210 	HWMON_P_LABEL,
1211 	0,
1212 };
1213 
1214 static const struct hwmon_channel_info sfp_hwmon_power_channel_info = {
1215 	.type = hwmon_power,
1216 	.config = sfp_hwmon_power_config,
1217 };
1218 
1219 static const struct hwmon_channel_info *sfp_hwmon_info[] = {
1220 	&sfp_hwmon_chip,
1221 	&sfp_hwmon_vcc_channel_info,
1222 	&sfp_hwmon_temp_channel_info,
1223 	&sfp_hwmon_bias_channel_info,
1224 	&sfp_hwmon_power_channel_info,
1225 	NULL,
1226 };
1227 
1228 static const struct hwmon_chip_info sfp_hwmon_chip_info = {
1229 	.ops = &sfp_hwmon_ops,
1230 	.info = sfp_hwmon_info,
1231 };
1232 
1233 static void sfp_hwmon_probe(struct work_struct *work)
1234 {
1235 	struct sfp *sfp = container_of(work, struct sfp, hwmon_probe.work);
1236 	int err, i;
1237 
1238 	err = sfp_read(sfp, true, 0, &sfp->diag, sizeof(sfp->diag));
1239 	if (err < 0) {
1240 		if (sfp->hwmon_tries--) {
1241 			mod_delayed_work(system_wq, &sfp->hwmon_probe,
1242 					 T_PROBE_RETRY_SLOW);
1243 		} else {
1244 			dev_warn(sfp->dev, "hwmon probe failed: %d\n", err);
1245 		}
1246 		return;
1247 	}
1248 
1249 	sfp->hwmon_name = kstrdup(dev_name(sfp->dev), GFP_KERNEL);
1250 	if (!sfp->hwmon_name) {
1251 		dev_err(sfp->dev, "out of memory for hwmon name\n");
1252 		return;
1253 	}
1254 
1255 	for (i = 0; sfp->hwmon_name[i]; i++)
1256 		if (hwmon_is_bad_char(sfp->hwmon_name[i]))
1257 			sfp->hwmon_name[i] = '_';
1258 
1259 	sfp->hwmon_dev = hwmon_device_register_with_info(sfp->dev,
1260 							 sfp->hwmon_name, sfp,
1261 							 &sfp_hwmon_chip_info,
1262 							 NULL);
1263 	if (IS_ERR(sfp->hwmon_dev))
1264 		dev_err(sfp->dev, "failed to register hwmon device: %ld\n",
1265 			PTR_ERR(sfp->hwmon_dev));
1266 }
1267 
1268 static int sfp_hwmon_insert(struct sfp *sfp)
1269 {
1270 	if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE)
1271 		return 0;
1272 
1273 	if (!(sfp->id.ext.diagmon & SFP_DIAGMON_DDM))
1274 		return 0;
1275 
1276 	if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
1277 		/* This driver in general does not support address
1278 		 * change.
1279 		 */
1280 		return 0;
1281 
1282 	mod_delayed_work(system_wq, &sfp->hwmon_probe, 1);
1283 	sfp->hwmon_tries = R_PROBE_RETRY_SLOW;
1284 
1285 	return 0;
1286 }
1287 
1288 static void sfp_hwmon_remove(struct sfp *sfp)
1289 {
1290 	cancel_delayed_work_sync(&sfp->hwmon_probe);
1291 	if (!IS_ERR_OR_NULL(sfp->hwmon_dev)) {
1292 		hwmon_device_unregister(sfp->hwmon_dev);
1293 		sfp->hwmon_dev = NULL;
1294 		kfree(sfp->hwmon_name);
1295 	}
1296 }
1297 
1298 static int sfp_hwmon_init(struct sfp *sfp)
1299 {
1300 	INIT_DELAYED_WORK(&sfp->hwmon_probe, sfp_hwmon_probe);
1301 
1302 	return 0;
1303 }
1304 
1305 static void sfp_hwmon_exit(struct sfp *sfp)
1306 {
1307 	cancel_delayed_work_sync(&sfp->hwmon_probe);
1308 }
1309 #else
1310 static int sfp_hwmon_insert(struct sfp *sfp)
1311 {
1312 	return 0;
1313 }
1314 
1315 static void sfp_hwmon_remove(struct sfp *sfp)
1316 {
1317 }
1318 
1319 static int sfp_hwmon_init(struct sfp *sfp)
1320 {
1321 	return 0;
1322 }
1323 
1324 static void sfp_hwmon_exit(struct sfp *sfp)
1325 {
1326 }
1327 #endif
1328 
1329 /* Helpers */
1330 static void sfp_module_tx_disable(struct sfp *sfp)
1331 {
1332 	dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1333 		sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
1334 	sfp->state |= SFP_F_TX_DISABLE;
1335 	sfp_set_state(sfp, sfp->state);
1336 }
1337 
1338 static void sfp_module_tx_enable(struct sfp *sfp)
1339 {
1340 	dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1341 		sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
1342 	sfp->state &= ~SFP_F_TX_DISABLE;
1343 	sfp_set_state(sfp, sfp->state);
1344 }
1345 
1346 static void sfp_module_tx_fault_reset(struct sfp *sfp)
1347 {
1348 	unsigned int state = sfp->state;
1349 
1350 	if (state & SFP_F_TX_DISABLE)
1351 		return;
1352 
1353 	sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
1354 
1355 	udelay(T_RESET_US);
1356 
1357 	sfp_set_state(sfp, state);
1358 }
1359 
1360 /* SFP state machine */
1361 static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
1362 {
1363 	if (timeout)
1364 		mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
1365 				 timeout);
1366 	else
1367 		cancel_delayed_work(&sfp->timeout);
1368 }
1369 
1370 static void sfp_sm_next(struct sfp *sfp, unsigned int state,
1371 			unsigned int timeout)
1372 {
1373 	sfp->sm_state = state;
1374 	sfp_sm_set_timer(sfp, timeout);
1375 }
1376 
1377 static void sfp_sm_mod_next(struct sfp *sfp, unsigned int state,
1378 			    unsigned int timeout)
1379 {
1380 	sfp->sm_mod_state = state;
1381 	sfp_sm_set_timer(sfp, timeout);
1382 }
1383 
1384 static void sfp_sm_phy_detach(struct sfp *sfp)
1385 {
1386 	phy_stop(sfp->mod_phy);
1387 	sfp_remove_phy(sfp->sfp_bus);
1388 	phy_device_remove(sfp->mod_phy);
1389 	phy_device_free(sfp->mod_phy);
1390 	sfp->mod_phy = NULL;
1391 }
1392 
1393 static void sfp_sm_probe_phy(struct sfp *sfp)
1394 {
1395 	struct phy_device *phy;
1396 	int err;
1397 
1398 	phy = mdiobus_scan(sfp->i2c_mii, SFP_PHY_ADDR);
1399 	if (phy == ERR_PTR(-ENODEV)) {
1400 		dev_info(sfp->dev, "no PHY detected\n");
1401 		return;
1402 	}
1403 	if (IS_ERR(phy)) {
1404 		dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
1405 		return;
1406 	}
1407 
1408 	err = sfp_add_phy(sfp->sfp_bus, phy);
1409 	if (err) {
1410 		phy_device_remove(phy);
1411 		phy_device_free(phy);
1412 		dev_err(sfp->dev, "sfp_add_phy failed: %d\n", err);
1413 		return;
1414 	}
1415 
1416 	sfp->mod_phy = phy;
1417 	phy_start(phy);
1418 }
1419 
1420 static void sfp_sm_link_up(struct sfp *sfp)
1421 {
1422 	sfp_link_up(sfp->sfp_bus);
1423 	sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
1424 }
1425 
1426 static void sfp_sm_link_down(struct sfp *sfp)
1427 {
1428 	sfp_link_down(sfp->sfp_bus);
1429 }
1430 
1431 static void sfp_sm_link_check_los(struct sfp *sfp)
1432 {
1433 	unsigned int los = sfp->state & SFP_F_LOS;
1434 
1435 	/* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL
1436 	 * are set, we assume that no LOS signal is available.
1437 	 */
1438 	if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED))
1439 		los ^= SFP_F_LOS;
1440 	else if (!(sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL)))
1441 		los = 0;
1442 
1443 	if (los)
1444 		sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
1445 	else
1446 		sfp_sm_link_up(sfp);
1447 }
1448 
1449 static bool sfp_los_event_active(struct sfp *sfp, unsigned int event)
1450 {
1451 	return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
1452 		event == SFP_E_LOS_LOW) ||
1453 	       (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
1454 		event == SFP_E_LOS_HIGH);
1455 }
1456 
1457 static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event)
1458 {
1459 	return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
1460 		event == SFP_E_LOS_HIGH) ||
1461 	       (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
1462 		event == SFP_E_LOS_LOW);
1463 }
1464 
1465 static void sfp_sm_fault(struct sfp *sfp, unsigned int next_state, bool warn)
1466 {
1467 	if (sfp->sm_retries && !--sfp->sm_retries) {
1468 		dev_err(sfp->dev,
1469 			"module persistently indicates fault, disabling\n");
1470 		sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
1471 	} else {
1472 		if (warn)
1473 			dev_err(sfp->dev, "module transmit fault indicated\n");
1474 
1475 		sfp_sm_next(sfp, next_state, T_FAULT_RECOVER);
1476 	}
1477 }
1478 
1479 static void sfp_sm_probe_for_phy(struct sfp *sfp)
1480 {
1481 	/* Setting the serdes link mode is guesswork: there's no
1482 	 * field in the EEPROM which indicates what mode should
1483 	 * be used.
1484 	 *
1485 	 * If it's a gigabit-only fiber module, it probably does
1486 	 * not have a PHY, so switch to 802.3z negotiation mode.
1487 	 * Otherwise, switch to SGMII mode (which is required to
1488 	 * support non-gigabit speeds) and probe for a PHY.
1489 	 */
1490 	if (sfp->id.base.e1000_base_t ||
1491 	    sfp->id.base.e100_base_lx ||
1492 	    sfp->id.base.e100_base_fx)
1493 		sfp_sm_probe_phy(sfp);
1494 }
1495 
1496 static int sfp_module_parse_power(struct sfp *sfp)
1497 {
1498 	u32 power_mW = 1000;
1499 
1500 	if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL))
1501 		power_mW = 1500;
1502 	if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL))
1503 		power_mW = 2000;
1504 
1505 	if (power_mW > sfp->max_power_mW) {
1506 		/* Module power specification exceeds the allowed maximum. */
1507 		if (sfp->id.ext.sff8472_compliance ==
1508 			SFP_SFF8472_COMPLIANCE_NONE &&
1509 		    !(sfp->id.ext.diagmon & SFP_DIAGMON_DDM)) {
1510 			/* The module appears not to implement bus address
1511 			 * 0xa2, so assume that the module powers up in the
1512 			 * indicated mode.
1513 			 */
1514 			dev_err(sfp->dev,
1515 				"Host does not support %u.%uW modules\n",
1516 				power_mW / 1000, (power_mW / 100) % 10);
1517 			return -EINVAL;
1518 		} else {
1519 			dev_warn(sfp->dev,
1520 				 "Host does not support %u.%uW modules, module left in power mode 1\n",
1521 				 power_mW / 1000, (power_mW / 100) % 10);
1522 			return 0;
1523 		}
1524 	}
1525 
1526 	/* If the module requires a higher power mode, but also requires
1527 	 * an address change sequence, warn the user that the module may
1528 	 * not be functional.
1529 	 */
1530 	if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE && power_mW > 1000) {
1531 		dev_warn(sfp->dev,
1532 			 "Address Change Sequence not supported but module requires %u.%uW, module may not be functional\n",
1533 			 power_mW / 1000, (power_mW / 100) % 10);
1534 		return 0;
1535 	}
1536 
1537 	sfp->module_power_mW = power_mW;
1538 
1539 	return 0;
1540 }
1541 
1542 static int sfp_sm_mod_hpower(struct sfp *sfp, bool enable)
1543 {
1544 	u8 val;
1545 	int err;
1546 
1547 	err = sfp_read(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
1548 	if (err != sizeof(val)) {
1549 		dev_err(sfp->dev, "Failed to read EEPROM: %d\n", err);
1550 		return -EAGAIN;
1551 	}
1552 
1553 	if (enable)
1554 		val |= BIT(0);
1555 	else
1556 		val &= ~BIT(0);
1557 
1558 	err = sfp_write(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
1559 	if (err != sizeof(val)) {
1560 		dev_err(sfp->dev, "Failed to write EEPROM: %d\n", err);
1561 		return -EAGAIN;
1562 	}
1563 
1564 	if (enable)
1565 		dev_info(sfp->dev, "Module switched to %u.%uW power level\n",
1566 			 sfp->module_power_mW / 1000,
1567 			 (sfp->module_power_mW / 100) % 10);
1568 
1569 	return 0;
1570 }
1571 
1572 static int sfp_sm_mod_probe(struct sfp *sfp, bool report)
1573 {
1574 	/* SFP module inserted - read I2C data */
1575 	struct sfp_eeprom_id id;
1576 	bool cotsworks;
1577 	u8 check;
1578 	int ret;
1579 
1580 	ret = sfp_read(sfp, false, 0, &id, sizeof(id));
1581 	if (ret < 0) {
1582 		if (report)
1583 			dev_err(sfp->dev, "failed to read EEPROM: %d\n", ret);
1584 		return -EAGAIN;
1585 	}
1586 
1587 	if (ret != sizeof(id)) {
1588 		dev_err(sfp->dev, "EEPROM short read: %d\n", ret);
1589 		return -EAGAIN;
1590 	}
1591 
1592 	/* Cotsworks do not seem to update the checksums when they
1593 	 * do the final programming with the final module part number,
1594 	 * serial number and date code.
1595 	 */
1596 	cotsworks = !memcmp(id.base.vendor_name, "COTSWORKS       ", 16);
1597 
1598 	/* Validate the checksum over the base structure */
1599 	check = sfp_check(&id.base, sizeof(id.base) - 1);
1600 	if (check != id.base.cc_base) {
1601 		if (cotsworks) {
1602 			dev_warn(sfp->dev,
1603 				 "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n",
1604 				 check, id.base.cc_base);
1605 		} else {
1606 			dev_err(sfp->dev,
1607 				"EEPROM base structure checksum failure: 0x%02x != 0x%02x\n",
1608 				check, id.base.cc_base);
1609 			print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
1610 				       16, 1, &id, sizeof(id), true);
1611 			return -EINVAL;
1612 		}
1613 	}
1614 
1615 	check = sfp_check(&id.ext, sizeof(id.ext) - 1);
1616 	if (check != id.ext.cc_ext) {
1617 		if (cotsworks) {
1618 			dev_warn(sfp->dev,
1619 				 "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n",
1620 				 check, id.ext.cc_ext);
1621 		} else {
1622 			dev_err(sfp->dev,
1623 				"EEPROM extended structure checksum failure: 0x%02x != 0x%02x\n",
1624 				check, id.ext.cc_ext);
1625 			print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
1626 				       16, 1, &id, sizeof(id), true);
1627 			memset(&id.ext, 0, sizeof(id.ext));
1628 		}
1629 	}
1630 
1631 	sfp->id = id;
1632 
1633 	dev_info(sfp->dev, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n",
1634 		 (int)sizeof(id.base.vendor_name), id.base.vendor_name,
1635 		 (int)sizeof(id.base.vendor_pn), id.base.vendor_pn,
1636 		 (int)sizeof(id.base.vendor_rev), id.base.vendor_rev,
1637 		 (int)sizeof(id.ext.vendor_sn), id.ext.vendor_sn,
1638 		 (int)sizeof(id.ext.datecode), id.ext.datecode);
1639 
1640 	/* Check whether we support this module */
1641 	if (!sfp->type->module_supported(&id)) {
1642 		dev_err(sfp->dev,
1643 			"module is not supported - phys id 0x%02x 0x%02x\n",
1644 			sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
1645 		return -EINVAL;
1646 	}
1647 
1648 	/* If the module requires address swap mode, warn about it */
1649 	if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
1650 		dev_warn(sfp->dev,
1651 			 "module address swap to access page 0xA2 is not supported.\n");
1652 
1653 	/* Parse the module power requirement */
1654 	ret = sfp_module_parse_power(sfp);
1655 	if (ret < 0)
1656 		return ret;
1657 
1658 	return 0;
1659 }
1660 
1661 static void sfp_sm_mod_remove(struct sfp *sfp)
1662 {
1663 	if (sfp->sm_mod_state > SFP_MOD_WAITDEV)
1664 		sfp_module_remove(sfp->sfp_bus);
1665 
1666 	sfp_hwmon_remove(sfp);
1667 
1668 	memset(&sfp->id, 0, sizeof(sfp->id));
1669 	sfp->module_power_mW = 0;
1670 
1671 	dev_info(sfp->dev, "module removed\n");
1672 }
1673 
1674 /* This state machine tracks the upstream's state */
1675 static void sfp_sm_device(struct sfp *sfp, unsigned int event)
1676 {
1677 	switch (sfp->sm_dev_state) {
1678 	default:
1679 		if (event == SFP_E_DEV_ATTACH)
1680 			sfp->sm_dev_state = SFP_DEV_DOWN;
1681 		break;
1682 
1683 	case SFP_DEV_DOWN:
1684 		if (event == SFP_E_DEV_DETACH)
1685 			sfp->sm_dev_state = SFP_DEV_DETACHED;
1686 		else if (event == SFP_E_DEV_UP)
1687 			sfp->sm_dev_state = SFP_DEV_UP;
1688 		break;
1689 
1690 	case SFP_DEV_UP:
1691 		if (event == SFP_E_DEV_DETACH)
1692 			sfp->sm_dev_state = SFP_DEV_DETACHED;
1693 		else if (event == SFP_E_DEV_DOWN)
1694 			sfp->sm_dev_state = SFP_DEV_DOWN;
1695 		break;
1696 	}
1697 }
1698 
1699 /* This state machine tracks the insert/remove state of the module, probes
1700  * the on-board EEPROM, and sets up the power level.
1701  */
1702 static void sfp_sm_module(struct sfp *sfp, unsigned int event)
1703 {
1704 	int err;
1705 
1706 	/* Handle remove event globally, it resets this state machine */
1707 	if (event == SFP_E_REMOVE) {
1708 		if (sfp->sm_mod_state > SFP_MOD_PROBE)
1709 			sfp_sm_mod_remove(sfp);
1710 		sfp_sm_mod_next(sfp, SFP_MOD_EMPTY, 0);
1711 		return;
1712 	}
1713 
1714 	/* Handle device detach globally */
1715 	if (sfp->sm_dev_state < SFP_DEV_DOWN &&
1716 	    sfp->sm_mod_state > SFP_MOD_WAITDEV) {
1717 		if (sfp->module_power_mW > 1000 &&
1718 		    sfp->sm_mod_state > SFP_MOD_HPOWER)
1719 			sfp_sm_mod_hpower(sfp, false);
1720 		sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0);
1721 		return;
1722 	}
1723 
1724 	switch (sfp->sm_mod_state) {
1725 	default:
1726 		if (event == SFP_E_INSERT) {
1727 			sfp_sm_mod_next(sfp, SFP_MOD_PROBE, T_SERIAL);
1728 			sfp->sm_mod_tries_init = R_PROBE_RETRY_INIT;
1729 			sfp->sm_mod_tries = R_PROBE_RETRY_SLOW;
1730 		}
1731 		break;
1732 
1733 	case SFP_MOD_PROBE:
1734 		/* Wait for T_PROBE_INIT to time out */
1735 		if (event != SFP_E_TIMEOUT)
1736 			break;
1737 
1738 		err = sfp_sm_mod_probe(sfp, sfp->sm_mod_tries == 1);
1739 		if (err == -EAGAIN) {
1740 			if (sfp->sm_mod_tries_init &&
1741 			   --sfp->sm_mod_tries_init) {
1742 				sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT);
1743 				break;
1744 			} else if (sfp->sm_mod_tries && --sfp->sm_mod_tries) {
1745 				if (sfp->sm_mod_tries == R_PROBE_RETRY_SLOW - 1)
1746 					dev_warn(sfp->dev,
1747 						 "please wait, module slow to respond\n");
1748 				sfp_sm_set_timer(sfp, T_PROBE_RETRY_SLOW);
1749 				break;
1750 			}
1751 		}
1752 		if (err < 0) {
1753 			sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
1754 			break;
1755 		}
1756 
1757 		err = sfp_hwmon_insert(sfp);
1758 		if (err)
1759 			dev_warn(sfp->dev, "hwmon probe failed: %d\n", err);
1760 
1761 		sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0);
1762 		/* fall through */
1763 	case SFP_MOD_WAITDEV:
1764 		/* Ensure that the device is attached before proceeding */
1765 		if (sfp->sm_dev_state < SFP_DEV_DOWN)
1766 			break;
1767 
1768 		/* Report the module insertion to the upstream device */
1769 		err = sfp_module_insert(sfp->sfp_bus, &sfp->id);
1770 		if (err < 0) {
1771 			sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
1772 			break;
1773 		}
1774 
1775 		/* If this is a power level 1 module, we are done */
1776 		if (sfp->module_power_mW <= 1000)
1777 			goto insert;
1778 
1779 		sfp_sm_mod_next(sfp, SFP_MOD_HPOWER, 0);
1780 		/* fall through */
1781 	case SFP_MOD_HPOWER:
1782 		/* Enable high power mode */
1783 		err = sfp_sm_mod_hpower(sfp, true);
1784 		if (err < 0) {
1785 			if (err != -EAGAIN) {
1786 				sfp_module_remove(sfp->sfp_bus);
1787 				sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
1788 			} else {
1789 				sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT);
1790 			}
1791 			break;
1792 		}
1793 
1794 		sfp_sm_mod_next(sfp, SFP_MOD_WAITPWR, T_HPOWER_LEVEL);
1795 		break;
1796 
1797 	case SFP_MOD_WAITPWR:
1798 		/* Wait for T_HPOWER_LEVEL to time out */
1799 		if (event != SFP_E_TIMEOUT)
1800 			break;
1801 
1802 	insert:
1803 		sfp_sm_mod_next(sfp, SFP_MOD_PRESENT, 0);
1804 		break;
1805 
1806 	case SFP_MOD_PRESENT:
1807 	case SFP_MOD_ERROR:
1808 		break;
1809 	}
1810 }
1811 
1812 static void sfp_sm_main(struct sfp *sfp, unsigned int event)
1813 {
1814 	unsigned long timeout;
1815 
1816 	/* Some events are global */
1817 	if (sfp->sm_state != SFP_S_DOWN &&
1818 	    (sfp->sm_mod_state != SFP_MOD_PRESENT ||
1819 	     sfp->sm_dev_state != SFP_DEV_UP)) {
1820 		if (sfp->sm_state == SFP_S_LINK_UP &&
1821 		    sfp->sm_dev_state == SFP_DEV_UP)
1822 			sfp_sm_link_down(sfp);
1823 		if (sfp->mod_phy)
1824 			sfp_sm_phy_detach(sfp);
1825 		sfp_module_tx_disable(sfp);
1826 		sfp_soft_stop_poll(sfp);
1827 		sfp_sm_next(sfp, SFP_S_DOWN, 0);
1828 		return;
1829 	}
1830 
1831 	/* The main state machine */
1832 	switch (sfp->sm_state) {
1833 	case SFP_S_DOWN:
1834 		if (sfp->sm_mod_state != SFP_MOD_PRESENT ||
1835 		    sfp->sm_dev_state != SFP_DEV_UP)
1836 			break;
1837 
1838 		if (!(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE))
1839 			sfp_soft_start_poll(sfp);
1840 
1841 		sfp_module_tx_enable(sfp);
1842 
1843 		/* Initialise the fault clearance retries */
1844 		sfp->sm_retries = 5;
1845 
1846 		/* We need to check the TX_FAULT state, which is not defined
1847 		 * while TX_DISABLE is asserted. The earliest we want to do
1848 		 * anything (such as probe for a PHY) is 50ms.
1849 		 */
1850 		sfp_sm_next(sfp, SFP_S_WAIT, T_WAIT);
1851 		break;
1852 
1853 	case SFP_S_WAIT:
1854 		if (event != SFP_E_TIMEOUT)
1855 			break;
1856 
1857 		if (sfp->state & SFP_F_TX_FAULT) {
1858 			/* Wait t_init before indicating that the link is up,
1859 			 * provided the current state indicates no TX_FAULT. If
1860 			 * TX_FAULT clears before this time, that's fine too.
1861 			 */
1862 			timeout = T_INIT_JIFFIES;
1863 			if (timeout > T_WAIT)
1864 				timeout -= T_WAIT;
1865 			else
1866 				timeout = 1;
1867 
1868 			sfp_sm_next(sfp, SFP_S_INIT, timeout);
1869 		} else {
1870 			/* TX_FAULT is not asserted, assume the module has
1871 			 * finished initialising.
1872 			 */
1873 			goto init_done;
1874 		}
1875 		break;
1876 
1877 	case SFP_S_INIT:
1878 		if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
1879 			/* TX_FAULT is still asserted after t_init, so assume
1880 			 * there is a fault.
1881 			 */
1882 			sfp_sm_fault(sfp, SFP_S_INIT_TX_FAULT,
1883 				     sfp->sm_retries == 5);
1884 		} else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
1885 	init_done:	/* TX_FAULT deasserted or we timed out with TX_FAULT
1886 			 * clear.  Probe for the PHY and check the LOS state.
1887 			 */
1888 			sfp_sm_probe_for_phy(sfp);
1889 			sfp_sm_link_check_los(sfp);
1890 
1891 			/* Reset the fault retry count */
1892 			sfp->sm_retries = 5;
1893 		}
1894 		break;
1895 
1896 	case SFP_S_INIT_TX_FAULT:
1897 		if (event == SFP_E_TIMEOUT) {
1898 			sfp_module_tx_fault_reset(sfp);
1899 			sfp_sm_next(sfp, SFP_S_INIT, T_INIT_JIFFIES);
1900 		}
1901 		break;
1902 
1903 	case SFP_S_WAIT_LOS:
1904 		if (event == SFP_E_TX_FAULT)
1905 			sfp_sm_fault(sfp, SFP_S_TX_FAULT, true);
1906 		else if (sfp_los_event_inactive(sfp, event))
1907 			sfp_sm_link_up(sfp);
1908 		break;
1909 
1910 	case SFP_S_LINK_UP:
1911 		if (event == SFP_E_TX_FAULT) {
1912 			sfp_sm_link_down(sfp);
1913 			sfp_sm_fault(sfp, SFP_S_TX_FAULT, true);
1914 		} else if (sfp_los_event_active(sfp, event)) {
1915 			sfp_sm_link_down(sfp);
1916 			sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
1917 		}
1918 		break;
1919 
1920 	case SFP_S_TX_FAULT:
1921 		if (event == SFP_E_TIMEOUT) {
1922 			sfp_module_tx_fault_reset(sfp);
1923 			sfp_sm_next(sfp, SFP_S_REINIT, T_INIT_JIFFIES);
1924 		}
1925 		break;
1926 
1927 	case SFP_S_REINIT:
1928 		if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
1929 			sfp_sm_fault(sfp, SFP_S_TX_FAULT, false);
1930 		} else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
1931 			dev_info(sfp->dev, "module transmit fault recovered\n");
1932 			sfp_sm_link_check_los(sfp);
1933 		}
1934 		break;
1935 
1936 	case SFP_S_TX_DISABLE:
1937 		break;
1938 	}
1939 }
1940 
1941 static void sfp_sm_event(struct sfp *sfp, unsigned int event)
1942 {
1943 	mutex_lock(&sfp->sm_mutex);
1944 
1945 	dev_dbg(sfp->dev, "SM: enter %s:%s:%s event %s\n",
1946 		mod_state_to_str(sfp->sm_mod_state),
1947 		dev_state_to_str(sfp->sm_dev_state),
1948 		sm_state_to_str(sfp->sm_state),
1949 		event_to_str(event));
1950 
1951 	sfp_sm_device(sfp, event);
1952 	sfp_sm_module(sfp, event);
1953 	sfp_sm_main(sfp, event);
1954 
1955 	dev_dbg(sfp->dev, "SM: exit %s:%s:%s\n",
1956 		mod_state_to_str(sfp->sm_mod_state),
1957 		dev_state_to_str(sfp->sm_dev_state),
1958 		sm_state_to_str(sfp->sm_state));
1959 
1960 	mutex_unlock(&sfp->sm_mutex);
1961 }
1962 
1963 static void sfp_attach(struct sfp *sfp)
1964 {
1965 	sfp_sm_event(sfp, SFP_E_DEV_ATTACH);
1966 }
1967 
1968 static void sfp_detach(struct sfp *sfp)
1969 {
1970 	sfp_sm_event(sfp, SFP_E_DEV_DETACH);
1971 }
1972 
1973 static void sfp_start(struct sfp *sfp)
1974 {
1975 	sfp_sm_event(sfp, SFP_E_DEV_UP);
1976 }
1977 
1978 static void sfp_stop(struct sfp *sfp)
1979 {
1980 	sfp_sm_event(sfp, SFP_E_DEV_DOWN);
1981 }
1982 
1983 static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo)
1984 {
1985 	/* locking... and check module is present */
1986 
1987 	if (sfp->id.ext.sff8472_compliance &&
1988 	    !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) {
1989 		modinfo->type = ETH_MODULE_SFF_8472;
1990 		modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1991 	} else {
1992 		modinfo->type = ETH_MODULE_SFF_8079;
1993 		modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
1994 	}
1995 	return 0;
1996 }
1997 
1998 static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee,
1999 			     u8 *data)
2000 {
2001 	unsigned int first, last, len;
2002 	int ret;
2003 
2004 	if (ee->len == 0)
2005 		return -EINVAL;
2006 
2007 	first = ee->offset;
2008 	last = ee->offset + ee->len;
2009 	if (first < ETH_MODULE_SFF_8079_LEN) {
2010 		len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN);
2011 		len -= first;
2012 
2013 		ret = sfp_read(sfp, false, first, data, len);
2014 		if (ret < 0)
2015 			return ret;
2016 
2017 		first += len;
2018 		data += len;
2019 	}
2020 	if (first < ETH_MODULE_SFF_8472_LEN && last > ETH_MODULE_SFF_8079_LEN) {
2021 		len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN);
2022 		len -= first;
2023 		first -= ETH_MODULE_SFF_8079_LEN;
2024 
2025 		ret = sfp_read(sfp, true, first, data, len);
2026 		if (ret < 0)
2027 			return ret;
2028 	}
2029 	return 0;
2030 }
2031 
2032 static const struct sfp_socket_ops sfp_module_ops = {
2033 	.attach = sfp_attach,
2034 	.detach = sfp_detach,
2035 	.start = sfp_start,
2036 	.stop = sfp_stop,
2037 	.module_info = sfp_module_info,
2038 	.module_eeprom = sfp_module_eeprom,
2039 };
2040 
2041 static void sfp_timeout(struct work_struct *work)
2042 {
2043 	struct sfp *sfp = container_of(work, struct sfp, timeout.work);
2044 
2045 	rtnl_lock();
2046 	sfp_sm_event(sfp, SFP_E_TIMEOUT);
2047 	rtnl_unlock();
2048 }
2049 
2050 static void sfp_check_state(struct sfp *sfp)
2051 {
2052 	unsigned int state, i, changed;
2053 
2054 	mutex_lock(&sfp->st_mutex);
2055 	state = sfp_get_state(sfp);
2056 	changed = state ^ sfp->state;
2057 	changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
2058 
2059 	for (i = 0; i < GPIO_MAX; i++)
2060 		if (changed & BIT(i))
2061 			dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i],
2062 				!!(sfp->state & BIT(i)), !!(state & BIT(i)));
2063 
2064 	state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT);
2065 	sfp->state = state;
2066 
2067 	rtnl_lock();
2068 	if (changed & SFP_F_PRESENT)
2069 		sfp_sm_event(sfp, state & SFP_F_PRESENT ?
2070 				SFP_E_INSERT : SFP_E_REMOVE);
2071 
2072 	if (changed & SFP_F_TX_FAULT)
2073 		sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
2074 				SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
2075 
2076 	if (changed & SFP_F_LOS)
2077 		sfp_sm_event(sfp, state & SFP_F_LOS ?
2078 				SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
2079 	rtnl_unlock();
2080 	mutex_unlock(&sfp->st_mutex);
2081 }
2082 
2083 static irqreturn_t sfp_irq(int irq, void *data)
2084 {
2085 	struct sfp *sfp = data;
2086 
2087 	sfp_check_state(sfp);
2088 
2089 	return IRQ_HANDLED;
2090 }
2091 
2092 static void sfp_poll(struct work_struct *work)
2093 {
2094 	struct sfp *sfp = container_of(work, struct sfp, poll.work);
2095 
2096 	sfp_check_state(sfp);
2097 
2098 	if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) ||
2099 	    sfp->need_poll)
2100 		mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
2101 }
2102 
2103 static struct sfp *sfp_alloc(struct device *dev)
2104 {
2105 	struct sfp *sfp;
2106 
2107 	sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
2108 	if (!sfp)
2109 		return ERR_PTR(-ENOMEM);
2110 
2111 	sfp->dev = dev;
2112 
2113 	mutex_init(&sfp->sm_mutex);
2114 	mutex_init(&sfp->st_mutex);
2115 	INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
2116 	INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
2117 
2118 	sfp_hwmon_init(sfp);
2119 
2120 	return sfp;
2121 }
2122 
2123 static void sfp_cleanup(void *data)
2124 {
2125 	struct sfp *sfp = data;
2126 
2127 	sfp_hwmon_exit(sfp);
2128 
2129 	cancel_delayed_work_sync(&sfp->poll);
2130 	cancel_delayed_work_sync(&sfp->timeout);
2131 	if (sfp->i2c_mii) {
2132 		mdiobus_unregister(sfp->i2c_mii);
2133 		mdiobus_free(sfp->i2c_mii);
2134 	}
2135 	if (sfp->i2c)
2136 		i2c_put_adapter(sfp->i2c);
2137 	kfree(sfp);
2138 }
2139 
2140 static int sfp_probe(struct platform_device *pdev)
2141 {
2142 	const struct sff_data *sff;
2143 	struct i2c_adapter *i2c;
2144 	struct sfp *sfp;
2145 	int err, i;
2146 
2147 	sfp = sfp_alloc(&pdev->dev);
2148 	if (IS_ERR(sfp))
2149 		return PTR_ERR(sfp);
2150 
2151 	platform_set_drvdata(pdev, sfp);
2152 
2153 	err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
2154 	if (err < 0)
2155 		return err;
2156 
2157 	sff = sfp->type = &sfp_data;
2158 
2159 	if (pdev->dev.of_node) {
2160 		struct device_node *node = pdev->dev.of_node;
2161 		const struct of_device_id *id;
2162 		struct device_node *np;
2163 
2164 		id = of_match_node(sfp_of_match, node);
2165 		if (WARN_ON(!id))
2166 			return -EINVAL;
2167 
2168 		sff = sfp->type = id->data;
2169 
2170 		np = of_parse_phandle(node, "i2c-bus", 0);
2171 		if (!np) {
2172 			dev_err(sfp->dev, "missing 'i2c-bus' property\n");
2173 			return -ENODEV;
2174 		}
2175 
2176 		i2c = of_find_i2c_adapter_by_node(np);
2177 		of_node_put(np);
2178 	} else if (has_acpi_companion(&pdev->dev)) {
2179 		struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
2180 		struct fwnode_handle *fw = acpi_fwnode_handle(adev);
2181 		struct fwnode_reference_args args;
2182 		struct acpi_handle *acpi_handle;
2183 		int ret;
2184 
2185 		ret = acpi_node_get_property_reference(fw, "i2c-bus", 0, &args);
2186 		if (ret || !is_acpi_device_node(args.fwnode)) {
2187 			dev_err(&pdev->dev, "missing 'i2c-bus' property\n");
2188 			return -ENODEV;
2189 		}
2190 
2191 		acpi_handle = ACPI_HANDLE_FWNODE(args.fwnode);
2192 		i2c = i2c_acpi_find_adapter_by_handle(acpi_handle);
2193 	} else {
2194 		return -EINVAL;
2195 	}
2196 
2197 	if (!i2c)
2198 		return -EPROBE_DEFER;
2199 
2200 	err = sfp_i2c_configure(sfp, i2c);
2201 	if (err < 0) {
2202 		i2c_put_adapter(i2c);
2203 		return err;
2204 	}
2205 
2206 	for (i = 0; i < GPIO_MAX; i++)
2207 		if (sff->gpios & BIT(i)) {
2208 			sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
2209 					   gpio_of_names[i], gpio_flags[i]);
2210 			if (IS_ERR(sfp->gpio[i]))
2211 				return PTR_ERR(sfp->gpio[i]);
2212 		}
2213 
2214 	sfp->get_state = sfp_gpio_get_state;
2215 	sfp->set_state = sfp_gpio_set_state;
2216 
2217 	/* Modules that have no detect signal are always present */
2218 	if (!(sfp->gpio[GPIO_MODDEF0]))
2219 		sfp->get_state = sff_gpio_get_state;
2220 
2221 	device_property_read_u32(&pdev->dev, "maximum-power-milliwatt",
2222 				 &sfp->max_power_mW);
2223 	if (!sfp->max_power_mW)
2224 		sfp->max_power_mW = 1000;
2225 
2226 	dev_info(sfp->dev, "Host maximum power %u.%uW\n",
2227 		 sfp->max_power_mW / 1000, (sfp->max_power_mW / 100) % 10);
2228 
2229 	/* Get the initial state, and always signal TX disable,
2230 	 * since the network interface will not be up.
2231 	 */
2232 	sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE;
2233 
2234 	if (sfp->gpio[GPIO_RATE_SELECT] &&
2235 	    gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT]))
2236 		sfp->state |= SFP_F_RATE_SELECT;
2237 	sfp_set_state(sfp, sfp->state);
2238 	sfp_module_tx_disable(sfp);
2239 	if (sfp->state & SFP_F_PRESENT) {
2240 		rtnl_lock();
2241 		sfp_sm_event(sfp, SFP_E_INSERT);
2242 		rtnl_unlock();
2243 	}
2244 
2245 	for (i = 0; i < GPIO_MAX; i++) {
2246 		if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
2247 			continue;
2248 
2249 		sfp->gpio_irq[i] = gpiod_to_irq(sfp->gpio[i]);
2250 		if (!sfp->gpio_irq[i]) {
2251 			sfp->need_poll = true;
2252 			continue;
2253 		}
2254 
2255 		err = devm_request_threaded_irq(sfp->dev, sfp->gpio_irq[i],
2256 						NULL, sfp_irq,
2257 						IRQF_ONESHOT |
2258 						IRQF_TRIGGER_RISING |
2259 						IRQF_TRIGGER_FALLING,
2260 						dev_name(sfp->dev), sfp);
2261 		if (err) {
2262 			sfp->gpio_irq[i] = 0;
2263 			sfp->need_poll = true;
2264 		}
2265 	}
2266 
2267 	if (sfp->need_poll)
2268 		mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
2269 
2270 	/* We could have an issue in cases no Tx disable pin is available or
2271 	 * wired as modules using a laser as their light source will continue to
2272 	 * be active when the fiber is removed. This could be a safety issue and
2273 	 * we should at least warn the user about that.
2274 	 */
2275 	if (!sfp->gpio[GPIO_TX_DISABLE])
2276 		dev_warn(sfp->dev,
2277 			 "No tx_disable pin: SFP modules will always be emitting.\n");
2278 
2279 	sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops);
2280 	if (!sfp->sfp_bus)
2281 		return -ENOMEM;
2282 
2283 	return 0;
2284 }
2285 
2286 static int sfp_remove(struct platform_device *pdev)
2287 {
2288 	struct sfp *sfp = platform_get_drvdata(pdev);
2289 
2290 	sfp_unregister_socket(sfp->sfp_bus);
2291 
2292 	rtnl_lock();
2293 	sfp_sm_event(sfp, SFP_E_REMOVE);
2294 	rtnl_unlock();
2295 
2296 	return 0;
2297 }
2298 
2299 static void sfp_shutdown(struct platform_device *pdev)
2300 {
2301 	struct sfp *sfp = platform_get_drvdata(pdev);
2302 	int i;
2303 
2304 	for (i = 0; i < GPIO_MAX; i++) {
2305 		if (!sfp->gpio_irq[i])
2306 			continue;
2307 
2308 		devm_free_irq(sfp->dev, sfp->gpio_irq[i], sfp);
2309 	}
2310 
2311 	cancel_delayed_work_sync(&sfp->poll);
2312 	cancel_delayed_work_sync(&sfp->timeout);
2313 }
2314 
2315 static struct platform_driver sfp_driver = {
2316 	.probe = sfp_probe,
2317 	.remove = sfp_remove,
2318 	.shutdown = sfp_shutdown,
2319 	.driver = {
2320 		.name = "sfp",
2321 		.of_match_table = sfp_of_match,
2322 	},
2323 };
2324 
2325 static int sfp_init(void)
2326 {
2327 	poll_jiffies = msecs_to_jiffies(100);
2328 
2329 	return platform_driver_register(&sfp_driver);
2330 }
2331 module_init(sfp_init);
2332 
2333 static void sfp_exit(void)
2334 {
2335 	platform_driver_unregister(&sfp_driver);
2336 }
2337 module_exit(sfp_exit);
2338 
2339 MODULE_ALIAS("platform:sfp");
2340 MODULE_AUTHOR("Russell King");
2341 MODULE_LICENSE("GPL v2");
2342