1 From: Russell King <rmk+kernel@arm.linux.org.uk>
2 Date: Sat, 12 Sep 2015 18:43:39 +0100
3 Subject: [PATCH] sfp: add phylink based SFP module support
5 Add support for SFP hotpluggable modules via phylink. This supports
6 both copper and optical SFP modules, which require different Serdes
7 modes in order to properly negotiate the link.
9 Optical SFP modules typically require the Serdes link to be talking
10 1000base-X mode - this is the gigabit ethernet mode defined by the
13 Copper SFP modules typically integrate a PHY in the module to convert
14 from Serdes to copper, and the PHY will be configured by the vendor
15 to either present a 1000base-X Serdes link (for fixed 1000base-T) or
16 a SGMII Serdes link. However, this is vendor defined, so we instead
17 detect the PHY, switch the link to SGMII mode, and use traditional
18 PHY based negotiation.
20 Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
22 - set port and port capability depending on connector type
23 - move autoneg mode setting to probe function
24 - set "supported" speed capabilities depending on reported ethernet
26 - checks for short read
27 - dump eeprom base ID when checksum fails
29 create mode 100644 drivers/net/phy/sfp.c
30 create mode 100644 include/linux/sfp.h
32 --- a/drivers/net/phy/Kconfig
33 +++ b/drivers/net/phy/Kconfig
34 @@ -256,6 +256,11 @@ endif # RTL8366_SMI
36 comment "MII PHY device drivers"
39 + tristate "SFP cage support"
40 + depends on I2C && PHYLINK
46 --- a/drivers/net/phy/Makefile
47 +++ b/drivers/net/phy/Makefile
48 @@ -41,6 +41,8 @@ obj-$(CONFIG_MDIO_SUN4I) += mdio-sun4i.o
49 obj-$(CONFIG_MDIO_THUNDER) += mdio-thunder.o
50 obj-$(CONFIG_MDIO_XGENE) += mdio-xgene.o
52 +obj-$(CONFIG_SFP) += sfp.o
54 obj-$(CONFIG_AMD_PHY) += amd.o
55 obj-$(CONFIG_AQUANTIA_PHY) += aquantia.o
56 obj-$(CONFIG_AT803X_PHY) += at803x.o
58 +++ b/drivers/net/phy/sfp.c
60 +#include <linux/delay.h>
61 +#include <linux/gpio.h>
62 +#include <linux/i2c.h>
63 +#include <linux/interrupt.h>
64 +#include <linux/jiffies.h>
65 +#include <linux/module.h>
66 +#include <linux/mutex.h>
67 +#include <linux/netdevice.h>
68 +#include <linux/of.h>
69 +#include <linux/of_net.h>
70 +#include <linux/phylink.h>
71 +#include <linux/platform_device.h>
72 +#include <linux/sfp.h>
73 +#include <linux/slab.h>
74 +#include <linux/workqueue.h>
76 +#include "mdio-i2c.h"
87 + SFP_F_PRESENT = BIT(GPIO_MODDEF0),
88 + SFP_F_LOS = BIT(GPIO_LOS),
89 + SFP_F_TX_FAULT = BIT(GPIO_TX_FAULT),
90 + SFP_F_TX_DISABLE = BIT(GPIO_TX_DISABLE),
91 + SFP_F_RATE_SELECT = BIT(GPIO_RATE_SELECT),
120 +static const char *gpio_of_names[] = {
128 +static const enum gpiod_flags gpio_flags[] = {
136 +#define T_INIT_JIFFIES msecs_to_jiffies(300)
137 +#define T_RESET_US 10
138 +#define T_FAULT_RECOVER msecs_to_jiffies(1000)
140 +/* SFP module presence detection is poor: the three MOD DEF signals are
141 + * the same length on the PCB, which means it's possible for MOD DEF 0 to
142 + * connect before the I2C bus on MOD DEF 1/2.
144 + * The SFP MSA specifies 300ms as t_init (the time taken for TX_FAULT to
145 + * be deasserted) but makes no mention of the earliest time before we can
146 + * access the I2C EEPROM. However, Avago modules require 300ms.
148 +#define T_PROBE_INIT msecs_to_jiffies(300)
149 +#define T_PROBE_RETRY msecs_to_jiffies(100)
152 + * SFP modules appear to always have their PHY configured for bus address
153 + * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
155 +#define SFP_PHY_ADDR 22
158 + * Give this long for the PHY to reset.
160 +#define T_PHY_RESET_MS 50
162 +static DEFINE_MUTEX(sfp_mutex);
165 + struct device *dev;
166 + struct i2c_adapter *i2c;
167 + struct mii_bus *i2c_mii;
168 + struct net_device *ndev;
169 + struct phylink *phylink;
170 + struct phy_device *mod_phy;
172 + unsigned int (*get_state)(struct sfp *);
173 + void (*set_state)(struct sfp *, unsigned int);
174 + int (*read)(struct sfp *, bool, u8, void *, size_t);
176 + struct gpio_desc *gpio[GPIO_MAX];
178 + unsigned int state;
179 + struct delayed_work poll;
180 + struct delayed_work timeout;
181 + struct mutex sm_mutex;
182 + unsigned char sm_mod_state;
183 + unsigned char sm_dev_state;
184 + unsigned short sm_state;
185 + unsigned int sm_retries;
187 + struct sfp_eeprom_id id;
189 + struct notifier_block netdev_nb;
192 +static unsigned long poll_jiffies;
194 +static unsigned int sfp_gpio_get_state(struct sfp *sfp)
196 + unsigned int i, state, v;
198 + for (i = state = 0; i < GPIO_MAX; i++) {
199 + if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
202 + v = gpiod_get_value_cansleep(sfp->gpio[i]);
210 +static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
212 + if (state & SFP_F_PRESENT) {
213 + /* If the module is present, drive the signals */
214 + if (sfp->gpio[GPIO_TX_DISABLE])
215 + gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
216 + state & SFP_F_TX_DISABLE);
217 + if (state & SFP_F_RATE_SELECT)
218 + gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT],
219 + state & SFP_F_RATE_SELECT);
221 + /* Otherwise, let them float to the pull-ups */
222 + if (sfp->gpio[GPIO_TX_DISABLE])
223 + gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
224 + if (state & SFP_F_RATE_SELECT)
225 + gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]);
229 +static int sfp__i2c_read(struct i2c_adapter *i2c, u8 bus_addr, u8 dev_addr,
230 + void *buf, size_t len)
232 + struct i2c_msg msgs[2];
235 + msgs[0].addr = bus_addr;
238 + msgs[0].buf = &dev_addr;
239 + msgs[1].addr = bus_addr;
240 + msgs[1].flags = I2C_M_RD;
244 + ret = i2c_transfer(i2c, msgs, ARRAY_SIZE(msgs));
248 + return ret == ARRAY_SIZE(msgs) ? len : 0;
251 +static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 addr, void *buf,
254 + return sfp__i2c_read(sfp->i2c, a2 ? 0x51 : 0x50, addr, buf, len);
257 +static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
259 + struct mii_bus *i2c_mii;
262 + if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
266 + sfp->read = sfp_i2c_read;
268 + i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
269 + if (IS_ERR(i2c_mii))
270 + return PTR_ERR(i2c_mii);
272 + i2c_mii->name = "SFP I2C Bus";
273 + i2c_mii->phy_mask = ~0;
275 + ret = mdiobus_register(i2c_mii);
277 + mdiobus_free(i2c_mii);
281 + sfp->i2c_mii = i2c_mii;
288 +static unsigned int sfp_get_state(struct sfp *sfp)
290 + return sfp->get_state(sfp);
293 +static void sfp_set_state(struct sfp *sfp, unsigned int state)
295 + sfp->set_state(sfp, state);
298 +static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
300 + return sfp->read(sfp, a2, addr, buf, len);
303 +static unsigned int sfp_check(void *buf, size_t len)
307 + for (p = buf, check = 0; len; p++, len--)
314 +static void sfp_module_tx_disable(struct sfp *sfp)
316 + dev_dbg(sfp->dev, "tx disable %u -> %u\n",
317 + sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
318 + sfp->state |= SFP_F_TX_DISABLE;
319 + sfp_set_state(sfp, sfp->state);
322 +static void sfp_module_tx_enable(struct sfp *sfp)
324 + dev_dbg(sfp->dev, "tx disable %u -> %u\n",
325 + sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
326 + sfp->state &= ~SFP_F_TX_DISABLE;
327 + sfp_set_state(sfp, sfp->state);
330 +static void sfp_module_tx_fault_reset(struct sfp *sfp)
332 + unsigned int state = sfp->state;
334 + if (state & SFP_F_TX_DISABLE)
337 + sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
339 + udelay(T_RESET_US);
341 + sfp_set_state(sfp, state);
344 +/* SFP state machine */
345 +static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
348 + mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
351 + cancel_delayed_work(&sfp->timeout);
354 +static void sfp_sm_next(struct sfp *sfp, unsigned int state,
355 + unsigned int timeout)
357 + sfp->sm_state = state;
358 + sfp_sm_set_timer(sfp, timeout);
361 +static void sfp_sm_ins_next(struct sfp *sfp, unsigned int state, unsigned int timeout)
363 + sfp->sm_mod_state = state;
364 + sfp_sm_set_timer(sfp, timeout);
367 +static void sfp_sm_phy_detach(struct sfp *sfp)
369 + phy_stop(sfp->mod_phy);
371 + phylink_disconnect_phy(sfp->phylink);
372 + phy_device_remove(sfp->mod_phy);
373 + phy_device_free(sfp->mod_phy);
374 + sfp->mod_phy = NULL;
377 +static void sfp_sm_probe_phy(struct sfp *sfp)
379 + struct phy_device *phy;
382 + msleep(T_PHY_RESET_MS);
384 + phy = mdiobus_scan(sfp->i2c_mii, SFP_PHY_ADDR);
386 + dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
390 + dev_info(sfp->dev, "no PHY detected\n");
394 + if (sfp->phylink) {
395 + err = phylink_connect_phy(sfp->phylink, phy);
397 + phy_device_remove(phy);
398 + phy_device_free(phy);
399 + dev_err(sfp->dev, "phylink_connect_phy failed: %d\n",
405 + sfp->mod_phy = phy;
409 +static void sfp_sm_link_up(struct sfp *sfp)
412 + phylink_enable(sfp->phylink);
414 + sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
417 +static void sfp_sm_link_down(struct sfp *sfp)
420 + phylink_disable(sfp->phylink);
423 +static void sfp_sm_link_check_los(struct sfp *sfp)
425 + unsigned int los = sfp->state & SFP_F_LOS;
427 + /* FIXME: what if neither SFP_OPTIONS_LOS_INVERTED nor
428 + * SFP_OPTIONS_LOS_NORMAL are set? For now, we assume
429 + * the same as SFP_OPTIONS_LOS_NORMAL set.
431 + if (sfp->id.ext.options & SFP_OPTIONS_LOS_INVERTED)
435 + sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
437 + sfp_sm_link_up(sfp);
440 +static void sfp_sm_fault(struct sfp *sfp, bool warn)
442 + if (sfp->sm_retries && !--sfp->sm_retries) {
443 + dev_err(sfp->dev, "module persistently indicates fault, disabling\n");
444 + sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
447 + dev_err(sfp->dev, "module transmit fault indicated\n");
449 + sfp_sm_next(sfp, SFP_S_TX_FAULT, T_FAULT_RECOVER);
453 +static void sfp_sm_mod_init(struct sfp *sfp)
455 + sfp_module_tx_enable(sfp);
457 + /* Wait t_init before indicating that the link is up, provided the
458 + * current state indicates no TX_FAULT. If TX_FAULT clears before
459 + * this time, that's fine too.
461 + sfp_sm_next(sfp, SFP_S_INIT, T_INIT_JIFFIES);
462 + sfp->sm_retries = 5;
464 + if (sfp->phylink) {
465 + /* Setting the serdes link mode is guesswork: there's no
466 + * field in the EEPROM which indicates what mode should
469 + * If it's a gigabit-only fiber module, it probably does
470 + * not have a PHY, so switch to 802.3z negotiation mode.
471 + * Otherwise, switch to SGMII mode (which is required to
472 + * support non-gigabit speeds) and probe for a PHY.
474 + if (sfp->id.base.e1000_base_t ||
475 + sfp->id.base.e100_base_lx ||
476 + sfp->id.base.e100_base_fx)
477 + sfp_sm_probe_phy(sfp);
481 +static int sfp_sm_mod_probe(struct sfp *sfp)
483 + /* SFP module inserted - read I2C data */
484 + struct sfp_eeprom_id id;
493 + err = sfp_read(sfp, false, 0, &id, sizeof(id));
495 + dev_err(sfp->dev, "failed to read EEPROM: %d\n", err);
499 + if (err != sizeof(id)) {
500 + dev_err(sfp->dev, "EEPROM short read: %d\n", err);
504 + /* Validate the checksum over the base structure */
505 + check = sfp_check(&id.base, sizeof(id.base) - 1);
506 + if (check != id.base.cc_base) {
508 + "EEPROM base structure checksum failure: 0x%02x\n",
510 + print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
511 + 16, 1, &id, sizeof(id.base) - 1, true);
515 + check = sfp_check(&id.ext, sizeof(id.ext) - 1);
516 + if (check != id.ext.cc_ext) {
518 + "EEPROM extended structure checksum failure: 0x%02x\n",
520 + memset(&id.ext, 0, sizeof(id.ext));
525 + memcpy(vendor, sfp->id.base.vendor_name, 16);
527 + memcpy(part, sfp->id.base.vendor_pn, 16);
529 + memcpy(rev, sfp->id.base.vendor_rev, 4);
531 + memcpy(sn, sfp->id.ext.vendor_sn, 16);
533 + memcpy(date, sfp->id.ext.datecode, 8);
536 + dev_info(sfp->dev, "module %s %s rev %s sn %s dc %s\n", vendor, part, rev, sn, date);
538 + /* We only support SFP modules, not the legacy GBIC modules. */
539 + if (sfp->id.base.phys_id != SFP_PHYS_ID_SFP ||
540 + sfp->id.base.phys_ext_id != SFP_PHYS_EXT_ID_SFP) {
541 + dev_err(sfp->dev, "module is not SFP - phys id 0x%02x 0x%02x\n",
542 + sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
547 + * What isn't clear from the SFP documentation is whether this
548 + * specifies the encoding expected on the TD/RD lines, or whether
549 + * the TD/RD lines are always 8b10b encoded, but the transceiver
550 + * converts. Eg, think of a copper SFP supporting 1G/100M/10M
551 + * ethernet: this requires 8b10b encoding for 1G, 4b5b for 100M,
552 + * and manchester for 10M.
554 + /* 1Gbit ethernet requires 8b10b encoding */
555 + if (sfp->id.base.encoding != SFP_ENCODING_8B10B) {
556 + dev_err(sfp->dev, "module does not support 8B10B encoding\n");
560 + if (sfp->phylink) {
561 + __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
565 + phylink_set(support, Autoneg);
566 + phylink_set(support, Pause);
567 + phylink_set(support, Asym_Pause);
569 + /* Set ethtool support from the compliance fields. */
570 + if (sfp->id.base.e10g_base_sr)
571 + phylink_set(support, 10000baseSR_Full);
572 + if (sfp->id.base.e10g_base_lr)
573 + phylink_set(support, 10000baseLR_Full);
574 + if (sfp->id.base.e10g_base_lrm)
575 + phylink_set(support, 10000baseLRM_Full);
576 + if (sfp->id.base.e10g_base_er)
577 + phylink_set(support, 10000baseER_Full);
578 + if (sfp->id.base.e1000_base_sx ||
579 + sfp->id.base.e1000_base_lx ||
580 + sfp->id.base.e1000_base_cx)
581 + phylink_set(support, 1000baseX_Full);
582 + if (sfp->id.base.e1000_base_t) {
583 + phylink_set(support, 1000baseT_Half);
584 + phylink_set(support, 1000baseT_Full);
587 + /* port is the physical connector, set this from the
590 + switch (sfp->id.base.connector) {
591 + case SFP_CONNECTOR_SC:
592 + case SFP_CONNECTOR_FIBERJACK:
593 + case SFP_CONNECTOR_LC:
594 + case SFP_CONNECTOR_MT_RJ:
595 + case SFP_CONNECTOR_MU:
596 + case SFP_CONNECTOR_OPTICAL_PIGTAIL:
597 + phylink_set(support, FIBRE);
601 + case SFP_CONNECTOR_RJ45:
602 + phylink_set(support, TP);
606 + case SFP_CONNECTOR_UNSPEC:
607 + if (sfp->id.base.e1000_base_t) {
608 + phylink_set(support, TP);
613 + case SFP_CONNECTOR_SG: /* guess */
614 + case SFP_CONNECTOR_MPO_1X12:
615 + case SFP_CONNECTOR_MPO_2X16:
616 + case SFP_CONNECTOR_HSSDC_II:
617 + case SFP_CONNECTOR_COPPER_PIGTAIL:
618 + case SFP_CONNECTOR_NOSEPARATE:
619 + case SFP_CONNECTOR_MXC_2X16:
621 + /* a guess at the supported link modes */
622 + dev_warn(sfp->dev, "Guessing link modes, please report...\n");
623 + phylink_set(support, 1000baseT_Half);
624 + phylink_set(support, 1000baseT_Full);
629 + /* Setting the serdes link mode is guesswork: there's no
630 + * field in the EEPROM which indicates what mode should
633 + * If it's a gigabit-only fiber module, it probably does
634 + * not have a PHY, so switch to 802.3z negotiation mode.
635 + * Otherwise, switch to SGMII mode (which is required to
636 + * support non-gigabit speeds) and probe for a PHY.
638 + if (!sfp->id.base.e1000_base_t &&
639 + !sfp->id.base.e100_base_lx &&
640 + !sfp->id.base.e100_base_fx) {
641 + mode = MLO_AN_8023Z;
643 + mode = MLO_AN_SGMII;
646 + phylink_set_link(sfp->phylink, mode, port, support);
652 +static void sfp_sm_mod_remove(struct sfp *sfp)
655 + sfp_sm_phy_detach(sfp);
657 + sfp_module_tx_disable(sfp);
659 + memset(&sfp->id, 0, sizeof(sfp->id));
661 + dev_info(sfp->dev, "module removed\n");
664 +static void sfp_sm_event(struct sfp *sfp, unsigned int event)
666 + mutex_lock(&sfp->sm_mutex);
668 + dev_dbg(sfp->dev, "SM: enter %u:%u:%u event %u\n",
669 + sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state, event);
671 + /* This state machine tracks the insert/remove state of
672 + * the module, and handles probing the on-board EEPROM.
674 + switch (sfp->sm_mod_state) {
676 + if (event == SFP_E_INSERT) {
677 + sfp_module_tx_disable(sfp);
678 + sfp_sm_ins_next(sfp, SFP_MOD_PROBE, T_PROBE_INIT);
682 + case SFP_MOD_PROBE:
683 + if (event == SFP_E_REMOVE) {
684 + sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
685 + } else if (event == SFP_E_TIMEOUT) {
686 + int err = sfp_sm_mod_probe(sfp);
689 + sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
690 + else if (err == -EAGAIN)
691 + sfp_sm_set_timer(sfp, T_PROBE_RETRY);
693 + sfp_sm_ins_next(sfp, SFP_MOD_ERROR, 0);
697 + case SFP_MOD_PRESENT:
698 + case SFP_MOD_ERROR:
699 + if (event == SFP_E_REMOVE) {
700 + sfp_sm_mod_remove(sfp);
701 + sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
706 + /* This state machine tracks the netdev up/down state */
707 + switch (sfp->sm_dev_state) {
709 + if (event == SFP_E_DEV_UP)
710 + sfp->sm_dev_state = SFP_DEV_UP;
714 + if (event == SFP_E_DEV_DOWN) {
715 + /* If the module has a PHY, avoid raising TX disable
716 + * as this resets the PHY. Otherwise, raise it to
717 + * turn the laser off.
720 + sfp_module_tx_disable(sfp);
721 + sfp->sm_dev_state = SFP_DEV_DOWN;
726 + /* Some events are global */
727 + if (sfp->sm_state != SFP_S_DOWN &&
728 + (sfp->sm_mod_state != SFP_MOD_PRESENT ||
729 + sfp->sm_dev_state != SFP_DEV_UP)) {
730 + if (sfp->sm_state == SFP_S_LINK_UP &&
731 + sfp->sm_dev_state == SFP_DEV_UP)
732 + sfp_sm_link_down(sfp);
734 + sfp_sm_phy_detach(sfp);
735 + sfp_sm_next(sfp, SFP_S_DOWN, 0);
736 + mutex_unlock(&sfp->sm_mutex);
740 + /* The main state machine */
741 + switch (sfp->sm_state) {
743 + if (sfp->sm_mod_state == SFP_MOD_PRESENT &&
744 + sfp->sm_dev_state == SFP_DEV_UP)
745 + sfp_sm_mod_init(sfp);
749 + if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT)
750 + sfp_sm_fault(sfp, true);
751 + else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR)
752 + sfp_sm_link_check_los(sfp);
755 + case SFP_S_WAIT_LOS:
756 + if (event == SFP_E_TX_FAULT)
757 + sfp_sm_fault(sfp, true);
759 + (sfp->id.ext.options & SFP_OPTIONS_LOS_INVERTED ?
760 + SFP_E_LOS_HIGH : SFP_E_LOS_LOW))
761 + sfp_sm_link_up(sfp);
764 + case SFP_S_LINK_UP:
765 + if (event == SFP_E_TX_FAULT) {
766 + sfp_sm_link_down(sfp);
767 + sfp_sm_fault(sfp, true);
768 + } else if (event ==
769 + (sfp->id.ext.options & SFP_OPTIONS_LOS_INVERTED ?
770 + SFP_E_LOS_LOW : SFP_E_LOS_HIGH)) {
771 + sfp_sm_link_down(sfp);
772 + sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
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);
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);
792 + case SFP_S_TX_DISABLE:
796 + dev_dbg(sfp->dev, "SM: exit %u:%u:%u\n",
797 + sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state);
799 + mutex_unlock(&sfp->sm_mutex);
803 +static int sfp_phy_module_info(struct phy_device *phy,
804 + struct ethtool_modinfo *modinfo)
806 + struct sfp *sfp = phy->priv;
808 + /* locking... and check module is present */
810 + if (sfp->id.ext.sff8472_compliance) {
811 + modinfo->type = ETH_MODULE_SFF_8472;
812 + modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
814 + modinfo->type = ETH_MODULE_SFF_8079;
815 + modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
820 +static int sfp_phy_module_eeprom(struct phy_device *phy,
821 + struct ethtool_eeprom *ee, u8 *data)
823 + struct sfp *sfp = phy->priv;
824 + unsigned int first, last, len;
830 + first = ee->offset;
831 + last = ee->offset + ee->len;
832 + if (first < ETH_MODULE_SFF_8079_LEN) {
834 + if (len > ETH_MODULE_SFF_8079_LEN)
835 + len = ETH_MODULE_SFF_8079_LEN;
838 + ret = sfp->read(sfp, false, first, data, len);
845 + if (first >= ETH_MODULE_SFF_8079_LEN && last > first) {
846 + len = last - first;
848 + ret = sfp->read(sfp, true, first, data, len);
856 +static void sfp_timeout(struct work_struct *work)
858 + struct sfp *sfp = container_of(work, struct sfp, timeout.work);
860 + sfp_sm_event(sfp, SFP_E_TIMEOUT);
863 +static void sfp_check_state(struct sfp *sfp)
865 + unsigned int state, i, changed;
867 + state = sfp_get_state(sfp);
868 + changed = state ^ sfp->state;
869 + changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
871 + for (i = 0; i < GPIO_MAX; i++)
872 + if (changed & BIT(i))
873 + dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i],
874 + !!(sfp->state & BIT(i)), !!(state & BIT(i)));
876 + state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT);
877 + sfp->state = state;
879 + if (changed & SFP_F_PRESENT)
880 + sfp_sm_event(sfp, state & SFP_F_PRESENT ?
881 + SFP_E_INSERT : SFP_E_REMOVE);
883 + if (changed & SFP_F_TX_FAULT)
884 + sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
885 + SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
887 + if (changed & SFP_F_LOS)
888 + sfp_sm_event(sfp, state & SFP_F_LOS ?
889 + SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
892 +static irqreturn_t sfp_irq(int irq, void *data)
894 + struct sfp *sfp = data;
896 + sfp_check_state(sfp);
898 + return IRQ_HANDLED;
901 +static void sfp_poll(struct work_struct *work)
903 + struct sfp *sfp = container_of(work, struct sfp, poll.work);
905 + sfp_check_state(sfp);
906 + mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
909 +static int sfp_netdev_notify(struct notifier_block *nb, unsigned long act, void *data)
911 + struct sfp *sfp = container_of(nb, struct sfp, netdev_nb);
912 + struct netdev_notifier_info *info = data;
913 + struct net_device *ndev = info->dev;
915 + if (!sfp->ndev || ndev != sfp->ndev)
916 + return NOTIFY_DONE;
920 + sfp_sm_event(sfp, SFP_E_DEV_UP);
923 + case NETDEV_GOING_DOWN:
924 + sfp_sm_event(sfp, SFP_E_DEV_DOWN);
927 + case NETDEV_UNREGISTER:
928 + if (sfp->mod_phy && sfp->phylink)
929 + phylink_disconnect_phy(sfp->phylink);
930 + sfp->phylink = NULL;
931 + dev_put(sfp->ndev);
938 +static struct sfp *sfp_alloc(struct device *dev)
942 + sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
944 + return ERR_PTR(-ENOMEM);
948 + mutex_init(&sfp->sm_mutex);
949 + INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
950 + INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
952 + sfp->netdev_nb.notifier_call = sfp_netdev_notify;
957 +static void sfp_destroy(struct sfp *sfp)
959 + cancel_delayed_work_sync(&sfp->poll);
960 + cancel_delayed_work_sync(&sfp->timeout);
961 + if (sfp->i2c_mii) {
962 + mdiobus_unregister(sfp->i2c_mii);
963 + mdiobus_free(sfp->i2c_mii);
966 + i2c_put_adapter(sfp->i2c);
967 + of_node_put(sfp->dev->of_node);
971 +static void sfp_cleanup(void *data)
973 + struct sfp *sfp = data;
978 +static int sfp_probe(struct platform_device *pdev)
984 + sfp = sfp_alloc(&pdev->dev);
986 + return PTR_ERR(sfp);
988 + platform_set_drvdata(pdev, sfp);
990 + err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
994 + if (pdev->dev.of_node) {
995 + struct device_node *node = pdev->dev.of_node;
996 + struct device_node *np;
998 + np = of_parse_phandle(node, "i2c-bus", 0);
1000 + struct i2c_adapter *i2c;
1002 + i2c = of_find_i2c_adapter_by_node(np);
1005 + return -EPROBE_DEFER;
1007 + err = sfp_i2c_configure(sfp, i2c);
1009 + i2c_put_adapter(i2c);
1014 + for (i = 0; i < GPIO_MAX; i++) {
1015 + sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
1016 + gpio_of_names[i], gpio_flags[i]);
1017 + if (IS_ERR(sfp->gpio[i]))
1018 + return PTR_ERR(sfp->gpio[i]);
1021 + sfp->get_state = sfp_gpio_get_state;
1022 + sfp->set_state = sfp_gpio_set_state;
1024 + np = of_parse_phandle(node, "sfp,ethernet", 0);
1026 + dev_err(sfp->dev, "missing sfp,ethernet property\n");
1030 + sfp->ndev = of_find_net_device_by_node(np);
1032 + dev_err(sfp->dev, "ethernet device not found\n");
1033 + return -EPROBE_DEFER;
1036 + dev_hold(sfp->ndev);
1037 + put_device(&sfp->ndev->dev);
1039 + sfp->phylink = phylink_lookup_by_netdev(sfp->ndev);
1040 + if (!sfp->phylink) {
1041 + dev_err(sfp->dev, "phylink for %s not found\n",
1042 + netdev_name(sfp->ndev));
1043 + return -EPROBE_DEFER;
1046 + phylink_disable(sfp->phylink);
1049 + sfp->state = sfp_get_state(sfp);
1050 + if (sfp->gpio[GPIO_TX_DISABLE] &&
1051 + gpiod_get_value_cansleep(sfp->gpio[GPIO_TX_DISABLE]))
1052 + sfp->state |= SFP_F_TX_DISABLE;
1053 + if (sfp->gpio[GPIO_RATE_SELECT] &&
1054 + gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT]))
1055 + sfp->state |= SFP_F_RATE_SELECT;
1056 + sfp_set_state(sfp, sfp->state);
1057 + sfp_module_tx_disable(sfp);
1058 + if (sfp->state & SFP_F_PRESENT)
1059 + sfp_sm_event(sfp, SFP_E_INSERT);
1061 + for (i = 0; i < GPIO_MAX; i++) {
1062 + if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
1065 + irq = gpiod_to_irq(sfp->gpio[i]);
1071 + err = devm_request_threaded_irq(sfp->dev, irq, NULL, sfp_irq,
1073 + IRQF_TRIGGER_RISING |
1074 + IRQF_TRIGGER_FALLING,
1075 + dev_name(sfp->dev), sfp);
1081 + mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
1083 + register_netdevice_notifier(&sfp->netdev_nb);
1088 +static int sfp_remove(struct platform_device *pdev)
1090 + struct sfp *sfp = platform_get_drvdata(pdev);
1092 + unregister_netdevice_notifier(&sfp->netdev_nb);
1094 + dev_put(sfp->ndev);
1099 +static const struct of_device_id sfp_of_match[] = {
1100 + { .compatible = "sff,sfp", },
1103 +MODULE_DEVICE_TABLE(of, sfp_of_match);
1105 +static struct platform_driver sfp_driver = {
1106 + .probe = sfp_probe,
1107 + .remove = sfp_remove,
1110 + .of_match_table = sfp_of_match,
1114 +static int sfp_init(void)
1116 + poll_jiffies = msecs_to_jiffies(100);
1118 + return platform_driver_register(&sfp_driver);
1120 +module_init(sfp_init);
1122 +static void sfp_exit(void)
1124 + platform_driver_unregister(&sfp_driver);
1126 +module_exit(sfp_exit);
1128 +MODULE_ALIAS("platform:sfp");
1129 +MODULE_AUTHOR("Russell King");
1130 +MODULE_LICENSE("GPL v2");
1132 +++ b/include/linux/sfp.h
1134 +#ifndef LINUX_SFP_H
1135 +#define LINUX_SFP_H
1137 +struct __packed sfp_eeprom_base {
1141 +#if defined __BIG_ENDIAN_BITFIELD
1142 + u8 e10g_base_er:1;
1143 + u8 e10g_base_lrm:1;
1144 + u8 e10g_base_lr:1;
1145 + u8 e10g_base_sr:1;
1148 + u8 if_1x_copper_active:1;
1149 + u8 if_1x_copper_passive:1;
1151 + u8 escon_mmf_1310_led:1;
1152 + u8 escon_smf_1310_laser:1;
1153 + u8 sonet_oc192_short_reach:1;
1154 + u8 sonet_reach_bit1:1;
1155 + u8 sonet_reach_bit2:1;
1156 + u8 sonet_oc48_long_reach:1;
1157 + u8 sonet_oc48_intermediate_reach:1;
1158 + u8 sonet_oc48_short_reach:1;
1160 + u8 unallocated_5_7:1;
1161 + u8 sonet_oc12_smf_long_reach:1;
1162 + u8 sonet_oc12_smf_intermediate_reach:1;
1163 + u8 sonet_oc12_short_reach:1;
1164 + u8 unallocated_5_3:1;
1165 + u8 sonet_oc3_smf_long_reach:1;
1166 + u8 sonet_oc3_smf_intermediate_reach:1;
1167 + u8 sonet_oc3_short_reach:1;
1171 + u8 e100_base_fx:1;
1172 + u8 e100_base_lx:1;
1173 + u8 e1000_base_t:1;
1174 + u8 e1000_base_cx:1;
1175 + u8 e1000_base_lx:1;
1176 + u8 e1000_base_sx:1;
1185 + u8 fc_tech_electrical_inter_enclosure:1;
1187 + u8 fc_tech_electrical_intra_enclosure:1;
1191 + u8 sfp_ct_active:1;
1192 + u8 sfp_ct_passive:1;
1193 + u8 unallocated_8_1:1;
1194 + u8 unallocated_8_0:1;
1202 + u8 unallocated_9_1:1;
1205 + u8 fc_speed_1200:1;
1206 + u8 fc_speed_800:1;
1207 + u8 fc_speed_1600:1;
1208 + u8 fc_speed_400:1;
1209 + u8 fc_speed_3200:1;
1210 + u8 fc_speed_200:1;
1211 + u8 unallocated_10_1:1;
1212 + u8 fc_speed_100:1;
1213 +#elif defined __LITTLE_ENDIAN_BITFIELD
1214 + u8 if_1x_copper_passive:1;
1215 + u8 if_1x_copper_active:1;
1218 + u8 e10g_base_sr:1;
1219 + u8 e10g_base_lr:1;
1220 + u8 e10g_base_lrm:1;
1221 + u8 e10g_base_er:1;
1223 + u8 sonet_oc3_short_reach:1;
1224 + u8 sonet_oc3_smf_intermediate_reach:1;
1225 + u8 sonet_oc3_smf_long_reach:1;
1226 + u8 unallocated_5_3:1;
1227 + u8 sonet_oc12_short_reach:1;
1228 + u8 sonet_oc12_smf_intermediate_reach:1;
1229 + u8 sonet_oc12_smf_long_reach:1;
1230 + u8 unallocated_5_7:1;
1232 + u8 sonet_oc48_short_reach:1;
1233 + u8 sonet_oc48_intermediate_reach:1;
1234 + u8 sonet_oc48_long_reach:1;
1235 + u8 sonet_reach_bit2:1;
1236 + u8 sonet_reach_bit1:1;
1237 + u8 sonet_oc192_short_reach:1;
1238 + u8 escon_smf_1310_laser:1;
1239 + u8 escon_mmf_1310_led:1;
1241 + u8 e1000_base_sx:1;
1242 + u8 e1000_base_lx:1;
1243 + u8 e1000_base_cx:1;
1244 + u8 e1000_base_t:1;
1245 + u8 e100_base_lx:1;
1246 + u8 e100_base_fx:1;
1250 + u8 fc_tech_electrical_inter_enclosure:1;
1259 + u8 unallocated_8_0:1;
1260 + u8 unallocated_8_1:1;
1261 + u8 sfp_ct_passive:1;
1262 + u8 sfp_ct_active:1;
1266 + u8 fc_tech_electrical_intra_enclosure:1;
1269 + u8 unallocated_9_1:1;
1277 + u8 fc_speed_100:1;
1278 + u8 unallocated_10_1:1;
1279 + u8 fc_speed_200:1;
1280 + u8 fc_speed_3200:1;
1281 + u8 fc_speed_400:1;
1282 + u8 fc_speed_1600:1;
1283 + u8 fc_speed_800:1;
1284 + u8 fc_speed_1200:1;
1286 +#error Unknown Endian
1292 + char vendor_name[16];
1294 + char vendor_oui[3];
1295 + char vendor_pn[16];
1296 + char vendor_rev[4];
1298 + __be16 optical_wavelength;
1305 +struct __packed sfp_eeprom_ext {
1309 + char vendor_sn[16];
1313 + u8 sff8472_compliance;
1317 +struct __packed sfp_eeprom_id {
1318 + struct sfp_eeprom_base base;
1319 + struct sfp_eeprom_ext ext;
1322 +/* SFP EEPROM registers */
1324 + SFP_PHYS_ID = 0x00,
1325 + SFP_PHYS_EXT_ID = 0x01,
1326 + SFP_CONNECTOR = 0x02,
1327 + SFP_COMPLIANCE = 0x03,
1328 + SFP_ENCODING = 0x0b,
1329 + SFP_BR_NOMINAL = 0x0c,
1330 + SFP_RATE_ID = 0x0d,
1331 + SFP_LINK_LEN_SM_KM = 0x0e,
1332 + SFP_LINK_LEN_SM_100M = 0x0f,
1333 + SFP_LINK_LEN_50UM_OM2_10M = 0x10,
1334 + SFP_LINK_LEN_62_5UM_OM1_10M = 0x11,
1335 + SFP_LINK_LEN_COPPER_1M = 0x12,
1336 + SFP_LINK_LEN_50UM_OM4_10M = 0x12,
1337 + SFP_LINK_LEN_50UM_OM3_10M = 0x13,
1338 + SFP_VENDOR_NAME = 0x14,
1339 + SFP_VENDOR_OUI = 0x25,
1340 + SFP_VENDOR_PN = 0x28,
1341 + SFP_VENDOR_REV = 0x38,
1342 + SFP_OPTICAL_WAVELENGTH_MSB = 0x3c,
1343 + SFP_OPTICAL_WAVELENGTH_LSB = 0x3d,
1344 + SFP_CABLE_SPEC = 0x3c,
1345 + SFP_CC_BASE = 0x3f,
1346 + SFP_OPTIONS = 0x40, /* 2 bytes, MSB, LSB */
1347 + SFP_BR_MAX = 0x42,
1348 + SFP_BR_MIN = 0x43,
1349 + SFP_VENDOR_SN = 0x44,
1350 + SFP_DATECODE = 0x54,
1351 + SFP_DIAGMON = 0x5c,
1352 + SFP_ENHOPTS = 0x5d,
1353 + SFP_SFF8472_COMPLIANCE = 0x5e,
1354 + SFP_CC_EXT = 0x5f,
1356 + SFP_PHYS_ID_SFP = 0x03,
1357 + SFP_PHYS_EXT_ID_SFP = 0x04,
1358 + SFP_CONNECTOR_UNSPEC = 0x00,
1359 + /* codes 01-05 not supportable on SFP, but some modules have single SC */
1360 + SFP_CONNECTOR_SC = 0x01,
1361 + SFP_CONNECTOR_FIBERJACK = 0x06,
1362 + SFP_CONNECTOR_LC = 0x07,
1363 + SFP_CONNECTOR_MT_RJ = 0x08,
1364 + SFP_CONNECTOR_MU = 0x09,
1365 + SFP_CONNECTOR_SG = 0x0a,
1366 + SFP_CONNECTOR_OPTICAL_PIGTAIL = 0x0b,
1367 + SFP_CONNECTOR_MPO_1X12 = 0x0c,
1368 + SFP_CONNECTOR_MPO_2X16 = 0x0d,
1369 + SFP_CONNECTOR_HSSDC_II = 0x20,
1370 + SFP_CONNECTOR_COPPER_PIGTAIL = 0x21,
1371 + SFP_CONNECTOR_RJ45 = 0x22,
1372 + SFP_CONNECTOR_NOSEPARATE = 0x23,
1373 + SFP_CONNECTOR_MXC_2X16 = 0x24,
1374 + SFP_ENCODING_UNSPEC = 0x00,
1375 + SFP_ENCODING_8B10B = 0x01,
1376 + SFP_ENCODING_4B5B = 0x02,
1377 + SFP_ENCODING_NRZ = 0x03,
1378 + SFP_ENCODING_MANCHESTER = 0x04,
1379 + SFP_OPTIONS_HIGH_POWER_LEVEL = BIT(13),
1380 + SFP_OPTIONS_PAGING_A2 = BIT(12),
1381 + SFP_OPTIONS_RETIMER = BIT(11),
1382 + SFP_OPTIONS_COOLED_XCVR = BIT(10),
1383 + SFP_OPTIONS_POWER_DECL = BIT(9),
1384 + SFP_OPTIONS_RX_LINEAR_OUT = BIT(8),
1385 + SFP_OPTIONS_RX_DECISION_THRESH = BIT(7),
1386 + SFP_OPTIONS_TUNABLE_TX = BIT(6),
1387 + SFP_OPTIONS_RATE_SELECT = BIT(5),
1388 + SFP_OPTIONS_TX_DISABLE = BIT(4),
1389 + SFP_OPTIONS_TX_FAULT = BIT(3),
1390 + SFP_OPTIONS_LOS_INVERTED = BIT(2),
1391 + SFP_OPTIONS_LOS_NORMAL = BIT(1),
1392 + SFP_DIAGMON_DDM = BIT(6),
1393 + SFP_DIAGMON_INT_CAL = BIT(5),
1394 + SFP_DIAGMON_EXT_CAL = BIT(4),
1395 + SFP_DIAGMON_RXPWR_AVG = BIT(3),
1396 + SFP_DIAGMON_ADDRMODE = BIT(2),
1397 + SFP_ENHOPTS_ALARMWARN = BIT(7),
1398 + SFP_ENHOPTS_SOFT_TX_DISABLE = BIT(6),
1399 + SFP_ENHOPTS_SOFT_TX_FAULT = BIT(5),
1400 + SFP_ENHOPTS_SOFT_RX_LOS = BIT(4),
1401 + SFP_ENHOPTS_SOFT_RATE_SELECT = BIT(3),
1402 + SFP_ENHOPTS_APP_SELECT_SFF8079 = BIT(2),
1403 + SFP_ENHOPTS_SOFT_RATE_SFF8431 = BIT(1),
1404 + SFP_SFF8472_COMPLIANCE_NONE = 0x00,
1405 + SFP_SFF8472_COMPLIANCE_REV9_3 = 0x01,
1406 + SFP_SFF8472_COMPLIANCE_REV9_5 = 0x02,
1407 + SFP_SFF8472_COMPLIANCE_REV10_2 = 0x03,
1408 + SFP_SFF8472_COMPLIANCE_REV10_4 = 0x04,
1409 + SFP_SFF8472_COMPLIANCE_REV11_0 = 0x05,
1410 + SFP_SFF8472_COMPLIANCE_REV11_3 = 0x06,
1411 + SFP_SFF8472_COMPLIANCE_REV11_4 = 0x07,
1412 + SFP_SFF8472_COMPLIANCE_REV12_0 = 0x08,
1415 +/* SFP Diagnostics */
1417 + /* Alarm and warnings stored MSB at lower address then LSB */
1418 + SFP_TEMP_HIGH_ALARM = 0x00,
1419 + SFP_TEMP_LOW_ALARM = 0x02,
1420 + SFP_TEMP_HIGH_WARN = 0x04,
1421 + SFP_TEMP_LOW_WARN = 0x06,
1422 + SFP_VOLT_HIGH_ALARM = 0x08,
1423 + SFP_VOLT_LOW_ALARM = 0x0a,
1424 + SFP_VOLT_HIGH_WARN = 0x0c,
1425 + SFP_VOLT_LOW_WARN = 0x0e,
1426 + SFP_BIAS_HIGH_ALARM = 0x10,
1427 + SFP_BIAS_LOW_ALARM = 0x12,
1428 + SFP_BIAS_HIGH_WARN = 0x14,
1429 + SFP_BIAS_LOW_WARN = 0x16,
1430 + SFP_TXPWR_HIGH_ALARM = 0x18,
1431 + SFP_TXPWR_LOW_ALARM = 0x1a,
1432 + SFP_TXPWR_HIGH_WARN = 0x1c,
1433 + SFP_TXPWR_LOW_WARN = 0x1e,
1434 + SFP_RXPWR_HIGH_ALARM = 0x20,
1435 + SFP_RXPWR_LOW_ALARM = 0x22,
1436 + SFP_RXPWR_HIGH_WARN = 0x24,
1437 + SFP_RXPWR_LOW_WARN = 0x26,
1438 + SFP_LASER_TEMP_HIGH_ALARM = 0x28,
1439 + SFP_LASER_TEMP_LOW_ALARM = 0x2a,
1440 + SFP_LASER_TEMP_HIGH_WARN = 0x2c,
1441 + SFP_LASER_TEMP_LOW_WARN = 0x2e,
1442 + SFP_TEC_CUR_HIGH_ALARM = 0x30,
1443 + SFP_TEC_CUR_LOW_ALARM = 0x32,
1444 + SFP_TEC_CUR_HIGH_WARN = 0x34,
1445 + SFP_TEC_CUR_LOW_WARN = 0x36,
1446 + SFP_CAL_RXPWR4 = 0x38,
1447 + SFP_CAL_RXPWR3 = 0x3c,
1448 + SFP_CAL_RXPWR2 = 0x40,
1449 + SFP_CAL_RXPWR1 = 0x44,
1450 + SFP_CAL_RXPWR0 = 0x48,
1451 + SFP_CAL_TXI_SLOPE = 0x4c,
1452 + SFP_CAL_TXI_OFFSET = 0x4e,
1453 + SFP_CAL_TXPWR_SLOPE = 0x50,
1454 + SFP_CAL_TXPWR_OFFSET = 0x52,
1455 + SFP_CAL_T_SLOPE = 0x54,
1456 + SFP_CAL_T_OFFSET = 0x56,
1457 + SFP_CAL_V_SLOPE = 0x58,
1458 + SFP_CAL_V_OFFSET = 0x5a,
1459 + SFP_CHKSUM = 0x5f,
1463 + SFP_TX_BIAS = 0x64,
1464 + SFP_TX_POWER = 0x66,
1465 + SFP_RX_POWER = 0x68,
1466 + SFP_LASER_TEMP = 0x6a,
1467 + SFP_TEC_CUR = 0x6c,
1469 + SFP_STATUS = 0x6e,
1472 + SFP_EXT_STATUS = 0x76,