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