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