xref: /linux/drivers/net/phy/sfp.c (revision 6ebe6dbd6886af07b102aca42e44edbee94a22d9)
1 #include <linux/delay.h>
2 #include <linux/gpio/consumer.h>
3 #include <linux/i2c.h>
4 #include <linux/interrupt.h>
5 #include <linux/jiffies.h>
6 #include <linux/module.h>
7 #include <linux/mutex.h>
8 #include <linux/of.h>
9 #include <linux/phy.h>
10 #include <linux/platform_device.h>
11 #include <linux/rtnetlink.h>
12 #include <linux/slab.h>
13 #include <linux/workqueue.h>
14 
15 #include "mdio-i2c.h"
16 #include "sfp.h"
17 #include "swphy.h"
18 
19 enum {
20 	GPIO_MODDEF0,
21 	GPIO_LOS,
22 	GPIO_TX_FAULT,
23 	GPIO_TX_DISABLE,
24 	GPIO_RATE_SELECT,
25 	GPIO_MAX,
26 
27 	SFP_F_PRESENT = BIT(GPIO_MODDEF0),
28 	SFP_F_LOS = BIT(GPIO_LOS),
29 	SFP_F_TX_FAULT = BIT(GPIO_TX_FAULT),
30 	SFP_F_TX_DISABLE = BIT(GPIO_TX_DISABLE),
31 	SFP_F_RATE_SELECT = BIT(GPIO_RATE_SELECT),
32 
33 	SFP_E_INSERT = 0,
34 	SFP_E_REMOVE,
35 	SFP_E_DEV_DOWN,
36 	SFP_E_DEV_UP,
37 	SFP_E_TX_FAULT,
38 	SFP_E_TX_CLEAR,
39 	SFP_E_LOS_HIGH,
40 	SFP_E_LOS_LOW,
41 	SFP_E_TIMEOUT,
42 
43 	SFP_MOD_EMPTY = 0,
44 	SFP_MOD_PROBE,
45 	SFP_MOD_PRESENT,
46 	SFP_MOD_ERROR,
47 
48 	SFP_DEV_DOWN = 0,
49 	SFP_DEV_UP,
50 
51 	SFP_S_DOWN = 0,
52 	SFP_S_INIT,
53 	SFP_S_WAIT_LOS,
54 	SFP_S_LINK_UP,
55 	SFP_S_TX_FAULT,
56 	SFP_S_REINIT,
57 	SFP_S_TX_DISABLE,
58 };
59 
60 static const char *gpio_of_names[] = {
61 	"mod-def0",
62 	"los",
63 	"tx-fault",
64 	"tx-disable",
65 	"rate-select0",
66 };
67 
68 static const enum gpiod_flags gpio_flags[] = {
69 	GPIOD_IN,
70 	GPIOD_IN,
71 	GPIOD_IN,
72 	GPIOD_ASIS,
73 	GPIOD_ASIS,
74 };
75 
76 #define T_INIT_JIFFIES	msecs_to_jiffies(300)
77 #define T_RESET_US	10
78 #define T_FAULT_RECOVER	msecs_to_jiffies(1000)
79 
80 /* SFP module presence detection is poor: the three MOD DEF signals are
81  * the same length on the PCB, which means it's possible for MOD DEF 0 to
82  * connect before the I2C bus on MOD DEF 1/2.
83  *
84  * The SFP MSA specifies 300ms as t_init (the time taken for TX_FAULT to
85  * be deasserted) but makes no mention of the earliest time before we can
86  * access the I2C EEPROM.  However, Avago modules require 300ms.
87  */
88 #define T_PROBE_INIT	msecs_to_jiffies(300)
89 #define T_PROBE_RETRY	msecs_to_jiffies(100)
90 
91 /* SFP modules appear to always have their PHY configured for bus address
92  * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
93  */
94 #define SFP_PHY_ADDR	22
95 
96 /* Give this long for the PHY to reset. */
97 #define T_PHY_RESET_MS	50
98 
99 static DEFINE_MUTEX(sfp_mutex);
100 
101 struct sff_data {
102 	unsigned int gpios;
103 	bool (*module_supported)(const struct sfp_eeprom_id *id);
104 };
105 
106 struct sfp {
107 	struct device *dev;
108 	struct i2c_adapter *i2c;
109 	struct mii_bus *i2c_mii;
110 	struct sfp_bus *sfp_bus;
111 	struct phy_device *mod_phy;
112 	const struct sff_data *type;
113 
114 	unsigned int (*get_state)(struct sfp *);
115 	void (*set_state)(struct sfp *, unsigned int);
116 	int (*read)(struct sfp *, bool, u8, void *, size_t);
117 
118 	struct gpio_desc *gpio[GPIO_MAX];
119 
120 	unsigned int state;
121 	struct delayed_work poll;
122 	struct delayed_work timeout;
123 	struct mutex sm_mutex;
124 	unsigned char sm_mod_state;
125 	unsigned char sm_dev_state;
126 	unsigned short sm_state;
127 	unsigned int sm_retries;
128 
129 	struct sfp_eeprom_id id;
130 };
131 
132 static bool sff_module_supported(const struct sfp_eeprom_id *id)
133 {
134 	return id->base.phys_id == SFP_PHYS_ID_SFF &&
135 	       id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
136 }
137 
138 static const struct sff_data sff_data = {
139 	.gpios = SFP_F_LOS | SFP_F_TX_FAULT | SFP_F_TX_DISABLE,
140 	.module_supported = sff_module_supported,
141 };
142 
143 static bool sfp_module_supported(const struct sfp_eeprom_id *id)
144 {
145 	return id->base.phys_id == SFP_PHYS_ID_SFP &&
146 	       id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
147 }
148 
149 static const struct sff_data sfp_data = {
150 	.gpios = SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT |
151 		 SFP_F_TX_DISABLE | SFP_F_RATE_SELECT,
152 	.module_supported = sfp_module_supported,
153 };
154 
155 static const struct of_device_id sfp_of_match[] = {
156 	{ .compatible = "sff,sff", .data = &sff_data, },
157 	{ .compatible = "sff,sfp", .data = &sfp_data, },
158 	{ },
159 };
160 MODULE_DEVICE_TABLE(of, sfp_of_match);
161 
162 static unsigned long poll_jiffies;
163 
164 static unsigned int sfp_gpio_get_state(struct sfp *sfp)
165 {
166 	unsigned int i, state, v;
167 
168 	for (i = state = 0; i < GPIO_MAX; i++) {
169 		if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
170 			continue;
171 
172 		v = gpiod_get_value_cansleep(sfp->gpio[i]);
173 		if (v)
174 			state |= BIT(i);
175 	}
176 
177 	return state;
178 }
179 
180 static unsigned int sff_gpio_get_state(struct sfp *sfp)
181 {
182 	return sfp_gpio_get_state(sfp) | SFP_F_PRESENT;
183 }
184 
185 static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
186 {
187 	if (state & SFP_F_PRESENT) {
188 		/* If the module is present, drive the signals */
189 		if (sfp->gpio[GPIO_TX_DISABLE])
190 			gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
191 					       state & SFP_F_TX_DISABLE);
192 		if (state & SFP_F_RATE_SELECT)
193 			gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT],
194 					       state & SFP_F_RATE_SELECT);
195 	} else {
196 		/* Otherwise, let them float to the pull-ups */
197 		if (sfp->gpio[GPIO_TX_DISABLE])
198 			gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
199 		if (state & SFP_F_RATE_SELECT)
200 			gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]);
201 	}
202 }
203 
204 static int sfp__i2c_read(struct i2c_adapter *i2c, u8 bus_addr, u8 dev_addr,
205 			 void *buf, size_t len)
206 {
207 	struct i2c_msg msgs[2];
208 	int ret;
209 
210 	msgs[0].addr = bus_addr;
211 	msgs[0].flags = 0;
212 	msgs[0].len = 1;
213 	msgs[0].buf = &dev_addr;
214 	msgs[1].addr = bus_addr;
215 	msgs[1].flags = I2C_M_RD;
216 	msgs[1].len = len;
217 	msgs[1].buf = buf;
218 
219 	ret = i2c_transfer(i2c, msgs, ARRAY_SIZE(msgs));
220 	if (ret < 0)
221 		return ret;
222 
223 	return ret == ARRAY_SIZE(msgs) ? len : 0;
224 }
225 
226 static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 addr, void *buf,
227 			size_t len)
228 {
229 	return sfp__i2c_read(sfp->i2c, a2 ? 0x51 : 0x50, addr, buf, len);
230 }
231 
232 static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
233 {
234 	struct mii_bus *i2c_mii;
235 	int ret;
236 
237 	if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
238 		return -EINVAL;
239 
240 	sfp->i2c = i2c;
241 	sfp->read = sfp_i2c_read;
242 
243 	i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
244 	if (IS_ERR(i2c_mii))
245 		return PTR_ERR(i2c_mii);
246 
247 	i2c_mii->name = "SFP I2C Bus";
248 	i2c_mii->phy_mask = ~0;
249 
250 	ret = mdiobus_register(i2c_mii);
251 	if (ret < 0) {
252 		mdiobus_free(i2c_mii);
253 		return ret;
254 	}
255 
256 	sfp->i2c_mii = i2c_mii;
257 
258 	return 0;
259 }
260 
261 /* Interface */
262 static unsigned int sfp_get_state(struct sfp *sfp)
263 {
264 	return sfp->get_state(sfp);
265 }
266 
267 static void sfp_set_state(struct sfp *sfp, unsigned int state)
268 {
269 	sfp->set_state(sfp, state);
270 }
271 
272 static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
273 {
274 	return sfp->read(sfp, a2, addr, buf, len);
275 }
276 
277 static unsigned int sfp_check(void *buf, size_t len)
278 {
279 	u8 *p, check;
280 
281 	for (p = buf, check = 0; len; p++, len--)
282 		check += *p;
283 
284 	return check;
285 }
286 
287 /* Helpers */
288 static void sfp_module_tx_disable(struct sfp *sfp)
289 {
290 	dev_dbg(sfp->dev, "tx disable %u -> %u\n",
291 		sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
292 	sfp->state |= SFP_F_TX_DISABLE;
293 	sfp_set_state(sfp, sfp->state);
294 }
295 
296 static void sfp_module_tx_enable(struct sfp *sfp)
297 {
298 	dev_dbg(sfp->dev, "tx disable %u -> %u\n",
299 		sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
300 	sfp->state &= ~SFP_F_TX_DISABLE;
301 	sfp_set_state(sfp, sfp->state);
302 }
303 
304 static void sfp_module_tx_fault_reset(struct sfp *sfp)
305 {
306 	unsigned int state = sfp->state;
307 
308 	if (state & SFP_F_TX_DISABLE)
309 		return;
310 
311 	sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
312 
313 	udelay(T_RESET_US);
314 
315 	sfp_set_state(sfp, state);
316 }
317 
318 /* SFP state machine */
319 static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
320 {
321 	if (timeout)
322 		mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
323 				 timeout);
324 	else
325 		cancel_delayed_work(&sfp->timeout);
326 }
327 
328 static void sfp_sm_next(struct sfp *sfp, unsigned int state,
329 			unsigned int timeout)
330 {
331 	sfp->sm_state = state;
332 	sfp_sm_set_timer(sfp, timeout);
333 }
334 
335 static void sfp_sm_ins_next(struct sfp *sfp, unsigned int state,
336 			    unsigned int timeout)
337 {
338 	sfp->sm_mod_state = state;
339 	sfp_sm_set_timer(sfp, timeout);
340 }
341 
342 static void sfp_sm_phy_detach(struct sfp *sfp)
343 {
344 	phy_stop(sfp->mod_phy);
345 	sfp_remove_phy(sfp->sfp_bus);
346 	phy_device_remove(sfp->mod_phy);
347 	phy_device_free(sfp->mod_phy);
348 	sfp->mod_phy = NULL;
349 }
350 
351 static void sfp_sm_probe_phy(struct sfp *sfp)
352 {
353 	struct phy_device *phy;
354 	int err;
355 
356 	msleep(T_PHY_RESET_MS);
357 
358 	phy = mdiobus_scan(sfp->i2c_mii, SFP_PHY_ADDR);
359 	if (phy == ERR_PTR(-ENODEV)) {
360 		dev_info(sfp->dev, "no PHY detected\n");
361 		return;
362 	}
363 	if (IS_ERR(phy)) {
364 		dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
365 		return;
366 	}
367 
368 	err = sfp_add_phy(sfp->sfp_bus, phy);
369 	if (err) {
370 		phy_device_remove(phy);
371 		phy_device_free(phy);
372 		dev_err(sfp->dev, "sfp_add_phy failed: %d\n", err);
373 		return;
374 	}
375 
376 	sfp->mod_phy = phy;
377 	phy_start(phy);
378 }
379 
380 static void sfp_sm_link_up(struct sfp *sfp)
381 {
382 	sfp_link_up(sfp->sfp_bus);
383 	sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
384 }
385 
386 static void sfp_sm_link_down(struct sfp *sfp)
387 {
388 	sfp_link_down(sfp->sfp_bus);
389 }
390 
391 static void sfp_sm_link_check_los(struct sfp *sfp)
392 {
393 	unsigned int los = sfp->state & SFP_F_LOS;
394 
395 	/* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL
396 	 * are set, we assume that no LOS signal is available.
397 	 */
398 	if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED))
399 		los ^= SFP_F_LOS;
400 	else if (!(sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL)))
401 		los = 0;
402 
403 	if (los)
404 		sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
405 	else
406 		sfp_sm_link_up(sfp);
407 }
408 
409 static bool sfp_los_event_active(struct sfp *sfp, unsigned int event)
410 {
411 	return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
412 		event == SFP_E_LOS_LOW) ||
413 	       (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
414 		event == SFP_E_LOS_HIGH);
415 }
416 
417 static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event)
418 {
419 	return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
420 		event == SFP_E_LOS_HIGH) ||
421 	       (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
422 		event == SFP_E_LOS_LOW);
423 }
424 
425 static void sfp_sm_fault(struct sfp *sfp, bool warn)
426 {
427 	if (sfp->sm_retries && !--sfp->sm_retries) {
428 		dev_err(sfp->dev,
429 			"module persistently indicates fault, disabling\n");
430 		sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
431 	} else {
432 		if (warn)
433 			dev_err(sfp->dev, "module transmit fault indicated\n");
434 
435 		sfp_sm_next(sfp, SFP_S_TX_FAULT, T_FAULT_RECOVER);
436 	}
437 }
438 
439 static void sfp_sm_mod_init(struct sfp *sfp)
440 {
441 	sfp_module_tx_enable(sfp);
442 
443 	/* Wait t_init before indicating that the link is up, provided the
444 	 * current state indicates no TX_FAULT.  If TX_FAULT clears before
445 	 * this time, that's fine too.
446 	 */
447 	sfp_sm_next(sfp, SFP_S_INIT, T_INIT_JIFFIES);
448 	sfp->sm_retries = 5;
449 
450 	/* Setting the serdes link mode is guesswork: there's no
451 	 * field in the EEPROM which indicates what mode should
452 	 * be used.
453 	 *
454 	 * If it's a gigabit-only fiber module, it probably does
455 	 * not have a PHY, so switch to 802.3z negotiation mode.
456 	 * Otherwise, switch to SGMII mode (which is required to
457 	 * support non-gigabit speeds) and probe for a PHY.
458 	 */
459 	if (sfp->id.base.e1000_base_t ||
460 	    sfp->id.base.e100_base_lx ||
461 	    sfp->id.base.e100_base_fx)
462 		sfp_sm_probe_phy(sfp);
463 }
464 
465 static int sfp_sm_mod_probe(struct sfp *sfp)
466 {
467 	/* SFP module inserted - read I2C data */
468 	struct sfp_eeprom_id id;
469 	char vendor[17];
470 	char part[17];
471 	char sn[17];
472 	char date[9];
473 	char rev[5];
474 	u8 check;
475 	int err;
476 
477 	err = sfp_read(sfp, false, 0, &id, sizeof(id));
478 	if (err < 0) {
479 		dev_err(sfp->dev, "failed to read EEPROM: %d\n", err);
480 		return -EAGAIN;
481 	}
482 
483 	if (err != sizeof(id)) {
484 		dev_err(sfp->dev, "EEPROM short read: %d\n", err);
485 		return -EAGAIN;
486 	}
487 
488 	/* Validate the checksum over the base structure */
489 	check = sfp_check(&id.base, sizeof(id.base) - 1);
490 	if (check != id.base.cc_base) {
491 		dev_err(sfp->dev,
492 			"EEPROM base structure checksum failure: 0x%02x\n",
493 			check);
494 		print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
495 			       16, 1, &id, sizeof(id.base) - 1, true);
496 		return -EINVAL;
497 	}
498 
499 	check = sfp_check(&id.ext, sizeof(id.ext) - 1);
500 	if (check != id.ext.cc_ext) {
501 		dev_err(sfp->dev,
502 			"EEPROM extended structure checksum failure: 0x%02x\n",
503 			check);
504 		memset(&id.ext, 0, sizeof(id.ext));
505 	}
506 
507 	sfp->id = id;
508 
509 	memcpy(vendor, sfp->id.base.vendor_name, 16);
510 	vendor[16] = '\0';
511 	memcpy(part, sfp->id.base.vendor_pn, 16);
512 	part[16] = '\0';
513 	memcpy(rev, sfp->id.base.vendor_rev, 4);
514 	rev[4] = '\0';
515 	memcpy(sn, sfp->id.ext.vendor_sn, 16);
516 	sn[16] = '\0';
517 	memcpy(date, sfp->id.ext.datecode, 8);
518 	date[8] = '\0';
519 
520 	dev_info(sfp->dev, "module %s %s rev %s sn %s dc %s\n",
521 		 vendor, part, rev, sn, date);
522 
523 	/* Check whether we support this module */
524 	if (!sfp->type->module_supported(&sfp->id)) {
525 		dev_err(sfp->dev,
526 			"module is not supported - phys id 0x%02x 0x%02x\n",
527 			sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
528 		return -EINVAL;
529 	}
530 
531 	/* If the module requires address swap mode, warn about it */
532 	if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
533 		dev_warn(sfp->dev,
534 			 "module address swap to access page 0xA2 is not supported.\n");
535 
536 	return sfp_module_insert(sfp->sfp_bus, &sfp->id);
537 }
538 
539 static void sfp_sm_mod_remove(struct sfp *sfp)
540 {
541 	sfp_module_remove(sfp->sfp_bus);
542 
543 	if (sfp->mod_phy)
544 		sfp_sm_phy_detach(sfp);
545 
546 	sfp_module_tx_disable(sfp);
547 
548 	memset(&sfp->id, 0, sizeof(sfp->id));
549 
550 	dev_info(sfp->dev, "module removed\n");
551 }
552 
553 static void sfp_sm_event(struct sfp *sfp, unsigned int event)
554 {
555 	mutex_lock(&sfp->sm_mutex);
556 
557 	dev_dbg(sfp->dev, "SM: enter %u:%u:%u event %u\n",
558 		sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state, event);
559 
560 	/* This state machine tracks the insert/remove state of
561 	 * the module, and handles probing the on-board EEPROM.
562 	 */
563 	switch (sfp->sm_mod_state) {
564 	default:
565 		if (event == SFP_E_INSERT) {
566 			sfp_module_tx_disable(sfp);
567 			sfp_sm_ins_next(sfp, SFP_MOD_PROBE, T_PROBE_INIT);
568 		}
569 		break;
570 
571 	case SFP_MOD_PROBE:
572 		if (event == SFP_E_REMOVE) {
573 			sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
574 		} else if (event == SFP_E_TIMEOUT) {
575 			int err = sfp_sm_mod_probe(sfp);
576 
577 			if (err == 0)
578 				sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
579 			else if (err == -EAGAIN)
580 				sfp_sm_set_timer(sfp, T_PROBE_RETRY);
581 			else
582 				sfp_sm_ins_next(sfp, SFP_MOD_ERROR, 0);
583 		}
584 		break;
585 
586 	case SFP_MOD_PRESENT:
587 	case SFP_MOD_ERROR:
588 		if (event == SFP_E_REMOVE) {
589 			sfp_sm_mod_remove(sfp);
590 			sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
591 		}
592 		break;
593 	}
594 
595 	/* This state machine tracks the netdev up/down state */
596 	switch (sfp->sm_dev_state) {
597 	default:
598 		if (event == SFP_E_DEV_UP)
599 			sfp->sm_dev_state = SFP_DEV_UP;
600 		break;
601 
602 	case SFP_DEV_UP:
603 		if (event == SFP_E_DEV_DOWN) {
604 			/* If the module has a PHY, avoid raising TX disable
605 			 * as this resets the PHY. Otherwise, raise it to
606 			 * turn the laser off.
607 			 */
608 			if (!sfp->mod_phy)
609 				sfp_module_tx_disable(sfp);
610 			sfp->sm_dev_state = SFP_DEV_DOWN;
611 		}
612 		break;
613 	}
614 
615 	/* Some events are global */
616 	if (sfp->sm_state != SFP_S_DOWN &&
617 	    (sfp->sm_mod_state != SFP_MOD_PRESENT ||
618 	     sfp->sm_dev_state != SFP_DEV_UP)) {
619 		if (sfp->sm_state == SFP_S_LINK_UP &&
620 		    sfp->sm_dev_state == SFP_DEV_UP)
621 			sfp_sm_link_down(sfp);
622 		if (sfp->mod_phy)
623 			sfp_sm_phy_detach(sfp);
624 		sfp_sm_next(sfp, SFP_S_DOWN, 0);
625 		mutex_unlock(&sfp->sm_mutex);
626 		return;
627 	}
628 
629 	/* The main state machine */
630 	switch (sfp->sm_state) {
631 	case SFP_S_DOWN:
632 		if (sfp->sm_mod_state == SFP_MOD_PRESENT &&
633 		    sfp->sm_dev_state == SFP_DEV_UP)
634 			sfp_sm_mod_init(sfp);
635 		break;
636 
637 	case SFP_S_INIT:
638 		if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT)
639 			sfp_sm_fault(sfp, true);
640 		else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR)
641 			sfp_sm_link_check_los(sfp);
642 		break;
643 
644 	case SFP_S_WAIT_LOS:
645 		if (event == SFP_E_TX_FAULT)
646 			sfp_sm_fault(sfp, true);
647 		else if (sfp_los_event_inactive(sfp, event))
648 			sfp_sm_link_up(sfp);
649 		break;
650 
651 	case SFP_S_LINK_UP:
652 		if (event == SFP_E_TX_FAULT) {
653 			sfp_sm_link_down(sfp);
654 			sfp_sm_fault(sfp, true);
655 		} else if (sfp_los_event_active(sfp, event)) {
656 			sfp_sm_link_down(sfp);
657 			sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
658 		}
659 		break;
660 
661 	case SFP_S_TX_FAULT:
662 		if (event == SFP_E_TIMEOUT) {
663 			sfp_module_tx_fault_reset(sfp);
664 			sfp_sm_next(sfp, SFP_S_REINIT, T_INIT_JIFFIES);
665 		}
666 		break;
667 
668 	case SFP_S_REINIT:
669 		if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
670 			sfp_sm_fault(sfp, false);
671 		} else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
672 			dev_info(sfp->dev, "module transmit fault recovered\n");
673 			sfp_sm_link_check_los(sfp);
674 		}
675 		break;
676 
677 	case SFP_S_TX_DISABLE:
678 		break;
679 	}
680 
681 	dev_dbg(sfp->dev, "SM: exit %u:%u:%u\n",
682 		sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state);
683 
684 	mutex_unlock(&sfp->sm_mutex);
685 }
686 
687 static void sfp_start(struct sfp *sfp)
688 {
689 	sfp_sm_event(sfp, SFP_E_DEV_UP);
690 }
691 
692 static void sfp_stop(struct sfp *sfp)
693 {
694 	sfp_sm_event(sfp, SFP_E_DEV_DOWN);
695 }
696 
697 static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo)
698 {
699 	/* locking... and check module is present */
700 
701 	if (sfp->id.ext.sff8472_compliance &&
702 	    !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) {
703 		modinfo->type = ETH_MODULE_SFF_8472;
704 		modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
705 	} else {
706 		modinfo->type = ETH_MODULE_SFF_8079;
707 		modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
708 	}
709 	return 0;
710 }
711 
712 static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee,
713 			     u8 *data)
714 {
715 	unsigned int first, last, len;
716 	int ret;
717 
718 	if (ee->len == 0)
719 		return -EINVAL;
720 
721 	first = ee->offset;
722 	last = ee->offset + ee->len;
723 	if (first < ETH_MODULE_SFF_8079_LEN) {
724 		len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN);
725 		len -= first;
726 
727 		ret = sfp_read(sfp, false, first, data, len);
728 		if (ret < 0)
729 			return ret;
730 
731 		first += len;
732 		data += len;
733 	}
734 	if (first < ETH_MODULE_SFF_8472_LEN && last > ETH_MODULE_SFF_8079_LEN) {
735 		len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN);
736 		len -= first;
737 		first -= ETH_MODULE_SFF_8079_LEN;
738 
739 		ret = sfp_read(sfp, true, first, data, len);
740 		if (ret < 0)
741 			return ret;
742 	}
743 	return 0;
744 }
745 
746 static const struct sfp_socket_ops sfp_module_ops = {
747 	.start = sfp_start,
748 	.stop = sfp_stop,
749 	.module_info = sfp_module_info,
750 	.module_eeprom = sfp_module_eeprom,
751 };
752 
753 static void sfp_timeout(struct work_struct *work)
754 {
755 	struct sfp *sfp = container_of(work, struct sfp, timeout.work);
756 
757 	rtnl_lock();
758 	sfp_sm_event(sfp, SFP_E_TIMEOUT);
759 	rtnl_unlock();
760 }
761 
762 static void sfp_check_state(struct sfp *sfp)
763 {
764 	unsigned int state, i, changed;
765 
766 	state = sfp_get_state(sfp);
767 	changed = state ^ sfp->state;
768 	changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
769 
770 	for (i = 0; i < GPIO_MAX; i++)
771 		if (changed & BIT(i))
772 			dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i],
773 				!!(sfp->state & BIT(i)), !!(state & BIT(i)));
774 
775 	state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT);
776 	sfp->state = state;
777 
778 	rtnl_lock();
779 	if (changed & SFP_F_PRESENT)
780 		sfp_sm_event(sfp, state & SFP_F_PRESENT ?
781 				SFP_E_INSERT : SFP_E_REMOVE);
782 
783 	if (changed & SFP_F_TX_FAULT)
784 		sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
785 				SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
786 
787 	if (changed & SFP_F_LOS)
788 		sfp_sm_event(sfp, state & SFP_F_LOS ?
789 				SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
790 	rtnl_unlock();
791 }
792 
793 static irqreturn_t sfp_irq(int irq, void *data)
794 {
795 	struct sfp *sfp = data;
796 
797 	sfp_check_state(sfp);
798 
799 	return IRQ_HANDLED;
800 }
801 
802 static void sfp_poll(struct work_struct *work)
803 {
804 	struct sfp *sfp = container_of(work, struct sfp, poll.work);
805 
806 	sfp_check_state(sfp);
807 	mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
808 }
809 
810 static struct sfp *sfp_alloc(struct device *dev)
811 {
812 	struct sfp *sfp;
813 
814 	sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
815 	if (!sfp)
816 		return ERR_PTR(-ENOMEM);
817 
818 	sfp->dev = dev;
819 
820 	mutex_init(&sfp->sm_mutex);
821 	INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
822 	INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
823 
824 	return sfp;
825 }
826 
827 static void sfp_cleanup(void *data)
828 {
829 	struct sfp *sfp = data;
830 
831 	cancel_delayed_work_sync(&sfp->poll);
832 	cancel_delayed_work_sync(&sfp->timeout);
833 	if (sfp->i2c_mii) {
834 		mdiobus_unregister(sfp->i2c_mii);
835 		mdiobus_free(sfp->i2c_mii);
836 	}
837 	if (sfp->i2c)
838 		i2c_put_adapter(sfp->i2c);
839 	kfree(sfp);
840 }
841 
842 static int sfp_probe(struct platform_device *pdev)
843 {
844 	const struct sff_data *sff;
845 	struct sfp *sfp;
846 	bool poll = false;
847 	int irq, err, i;
848 
849 	sfp = sfp_alloc(&pdev->dev);
850 	if (IS_ERR(sfp))
851 		return PTR_ERR(sfp);
852 
853 	platform_set_drvdata(pdev, sfp);
854 
855 	err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
856 	if (err < 0)
857 		return err;
858 
859 	sff = sfp->type = &sfp_data;
860 
861 	if (pdev->dev.of_node) {
862 		struct device_node *node = pdev->dev.of_node;
863 		const struct of_device_id *id;
864 		struct device_node *np;
865 
866 		id = of_match_node(sfp_of_match, node);
867 		if (WARN_ON(!id))
868 			return -EINVAL;
869 
870 		sff = sfp->type = id->data;
871 
872 		np = of_parse_phandle(node, "i2c-bus", 0);
873 		if (np) {
874 			struct i2c_adapter *i2c;
875 
876 			i2c = of_find_i2c_adapter_by_node(np);
877 			of_node_put(np);
878 			if (!i2c)
879 				return -EPROBE_DEFER;
880 
881 			err = sfp_i2c_configure(sfp, i2c);
882 			if (err < 0) {
883 				i2c_put_adapter(i2c);
884 				return err;
885 			}
886 		}
887 	}
888 
889 	for (i = 0; i < GPIO_MAX; i++)
890 		if (sff->gpios & BIT(i)) {
891 			sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
892 					   gpio_of_names[i], gpio_flags[i]);
893 			if (IS_ERR(sfp->gpio[i]))
894 				return PTR_ERR(sfp->gpio[i]);
895 		}
896 
897 	sfp->get_state = sfp_gpio_get_state;
898 	sfp->set_state = sfp_gpio_set_state;
899 
900 	/* Modules that have no detect signal are always present */
901 	if (!(sfp->gpio[GPIO_MODDEF0]))
902 		sfp->get_state = sff_gpio_get_state;
903 
904 	sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops);
905 	if (!sfp->sfp_bus)
906 		return -ENOMEM;
907 
908 	/* Get the initial state, and always signal TX disable,
909 	 * since the network interface will not be up.
910 	 */
911 	sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE;
912 
913 	if (sfp->gpio[GPIO_RATE_SELECT] &&
914 	    gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT]))
915 		sfp->state |= SFP_F_RATE_SELECT;
916 	sfp_set_state(sfp, sfp->state);
917 	sfp_module_tx_disable(sfp);
918 	rtnl_lock();
919 	if (sfp->state & SFP_F_PRESENT)
920 		sfp_sm_event(sfp, SFP_E_INSERT);
921 	rtnl_unlock();
922 
923 	for (i = 0; i < GPIO_MAX; i++) {
924 		if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
925 			continue;
926 
927 		irq = gpiod_to_irq(sfp->gpio[i]);
928 		if (!irq) {
929 			poll = true;
930 			continue;
931 		}
932 
933 		err = devm_request_threaded_irq(sfp->dev, irq, NULL, sfp_irq,
934 						IRQF_ONESHOT |
935 						IRQF_TRIGGER_RISING |
936 						IRQF_TRIGGER_FALLING,
937 						dev_name(sfp->dev), sfp);
938 		if (err)
939 			poll = true;
940 	}
941 
942 	if (poll)
943 		mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
944 
945 	return 0;
946 }
947 
948 static int sfp_remove(struct platform_device *pdev)
949 {
950 	struct sfp *sfp = platform_get_drvdata(pdev);
951 
952 	sfp_unregister_socket(sfp->sfp_bus);
953 
954 	return 0;
955 }
956 
957 static struct platform_driver sfp_driver = {
958 	.probe = sfp_probe,
959 	.remove = sfp_remove,
960 	.driver = {
961 		.name = "sfp",
962 		.of_match_table = sfp_of_match,
963 	},
964 };
965 
966 static int sfp_init(void)
967 {
968 	poll_jiffies = msecs_to_jiffies(100);
969 
970 	return platform_driver_register(&sfp_driver);
971 }
972 module_init(sfp_init);
973 
974 static void sfp_exit(void)
975 {
976 	platform_driver_unregister(&sfp_driver);
977 }
978 module_exit(sfp_exit);
979 
980 MODULE_ALIAS("platform:sfp");
981 MODULE_AUTHOR("Russell King");
982 MODULE_LICENSE("GPL v2");
983