1 From bf0a000960234c0e773fadea47240c3cda0cab02 Mon Sep 17 00:00:00 2001
2 From: Russell King <rmk+kernel@arm.linux.org.uk>
3 Date: Sat, 12 Sep 2015 18:43:39 +0100
4 Subject: [PATCH 720/744] sfp: add phylink based SFP module support
6 Add support for SFP hotpluggable modules via phylink. This supports
7 both copper and optical SFP modules, which require different Serdes
8 modes in order to properly negotiate the link.
10 Optical SFP modules typically require the Serdes link to be talking
11 1000base-X mode - this is the gigabit ethernet mode defined by the
14 Copper SFP modules typically integrate a PHY in the module to convert
15 from Serdes to copper, and the PHY will be configured by the vendor
16 to either present a 1000base-X Serdes link (for fixed 1000base-T) or
17 a SGMII Serdes link. However, this is vendor defined, so we instead
18 detect the PHY, switch the link to SGMII mode, and use traditional
19 PHY based negotiation.
21 Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
23 drivers/net/phy/Kconfig | 5 +
24 drivers/net/phy/Makefile | 1 +
25 drivers/net/phy/sfp.c | 986 +++++++++++++++++++++++++++++++++++++++++++++++
26 include/linux/sfp.h | 339 ++++++++++++++++
27 4 files changed, 1331 insertions(+)
28 create mode 100644 drivers/net/phy/sfp.c
29 create mode 100644 include/linux/sfp.h
31 --- a/drivers/net/phy/Kconfig
32 +++ b/drivers/net/phy/Kconfig
33 @@ -225,6 +225,11 @@ config FIXED_PHY
35 Currently tested with mpc866ads and mpc8349e-mitx.
38 + tristate "SFP cage support"
39 + depends on I2C && PHYLINK
43 tristate "Support for bitbanged MDIO buses"
45 --- a/drivers/net/phy/Makefile
46 +++ b/drivers/net/phy/Makefile
47 @@ -61,3 +61,4 @@ obj-$(CONFIG_MDIO_MOXART) += mdio-moxart
48 obj-$(CONFIG_MDIO_BCM_UNIMAC) += mdio-bcm-unimac.o
49 obj-$(CONFIG_MICROCHIP_PHY) += microchip.o
50 obj-$(CONFIG_MDIO_BCM_IPROC) += mdio-bcm-iproc.o
51 +obj-$(CONFIG_SFP) += sfp.o
53 +++ b/drivers/net/phy/sfp.c
55 +#include <linux/delay.h>
56 +#include <linux/gpio.h>
57 +#include <linux/i2c.h>
58 +#include <linux/interrupt.h>
59 +#include <linux/jiffies.h>
60 +#include <linux/module.h>
61 +#include <linux/mutex.h>
62 +#include <linux/netdevice.h>
63 +#include <linux/of.h>
64 +#include <linux/of_net.h>
65 +#include <linux/phylink.h>
66 +#include <linux/platform_device.h>
67 +#include <linux/sfp.h>
68 +#include <linux/slab.h>
69 +#include <linux/workqueue.h>
71 +#include "mdio-i2c.h"
82 + SFP_F_PRESENT = BIT(GPIO_MODDEF0),
83 + SFP_F_LOS = BIT(GPIO_LOS),
84 + SFP_F_TX_FAULT = BIT(GPIO_TX_FAULT),
85 + SFP_F_TX_DISABLE = BIT(GPIO_TX_DISABLE),
86 + SFP_F_RATE_SELECT = BIT(GPIO_RATE_SELECT),
115 +static const char *gpio_of_names[] = {
123 +static const enum gpiod_flags gpio_flags[] = {
131 +#define T_INIT_JIFFIES msecs_to_jiffies(300)
132 +#define T_RESET_US 10
133 +#define T_FAULT_RECOVER msecs_to_jiffies(1000)
135 +/* SFP module presence detection is poor: the three MOD DEF signals are
136 + * the same length on the PCB, which means it's possible for MOD DEF 0 to
137 + * connect before the I2C bus on MOD DEF 1/2. Try to work around this
138 + * design bug by waiting 50ms before probing, and then retry every 250ms.
140 +#define T_PROBE_INIT msecs_to_jiffies(50)
141 +#define T_PROBE_RETRY msecs_to_jiffies(250)
144 + * SFP modules appear to always have their PHY configured for bus address
145 + * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
147 +#define SFP_PHY_ADDR 22
150 + * Give this long for the PHY to reset.
152 +#define T_PHY_RESET_MS 50
154 +static DEFINE_MUTEX(sfp_mutex);
157 + struct device *dev;
158 + struct i2c_adapter *i2c;
159 + struct mii_bus *i2c_mii;
160 + struct net_device *ndev;
161 + struct phylink *phylink;
162 + struct phy_device *mod_phy;
164 + unsigned int (*get_state)(struct sfp *);
165 + void (*set_state)(struct sfp *, unsigned int);
166 + int (*read)(struct sfp *, bool, u8, void *, size_t);
168 + struct gpio_desc *gpio[GPIO_MAX];
170 + unsigned int state;
171 + struct delayed_work poll;
172 + struct delayed_work timeout;
173 + struct mutex sm_mutex;
174 + unsigned char sm_mod_state;
175 + unsigned char sm_dev_state;
176 + unsigned short sm_state;
177 + unsigned int sm_retries;
179 + struct sfp_eeprom_id id;
181 + struct notifier_block netdev_nb;
184 +static unsigned long poll_jiffies;
186 +static unsigned int sfp_gpio_get_state(struct sfp *sfp)
188 + unsigned int i, state, v;
190 + for (i = state = 0; i < GPIO_MAX; i++) {
191 + if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
194 + v = gpiod_get_value_cansleep(sfp->gpio[i]);
202 +static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
204 + if (state & SFP_F_PRESENT) {
205 + /* If the module is present, drive the signals */
206 + if (sfp->gpio[GPIO_TX_DISABLE])
207 + gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
208 + state & SFP_F_TX_DISABLE);
209 + if (state & SFP_F_RATE_SELECT)
210 + gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT],
211 + state & SFP_F_RATE_SELECT);
213 + /* Otherwise, let them float to the pull-ups */
214 + if (sfp->gpio[GPIO_TX_DISABLE])
215 + gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
216 + if (state & SFP_F_RATE_SELECT)
217 + gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]);
221 +static int sfp__i2c_read(struct i2c_adapter *i2c, u8 bus_addr, u8 dev_addr,
222 + void *buf, size_t len)
224 + struct i2c_msg msgs[2];
227 + msgs[0].addr = bus_addr;
230 + msgs[0].buf = &dev_addr;
231 + msgs[1].addr = bus_addr;
232 + msgs[1].flags = I2C_M_RD;
236 + ret = i2c_transfer(i2c, msgs, ARRAY_SIZE(msgs));
240 + return ret == ARRAY_SIZE(msgs) ? len : 0;
243 +static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 addr, void *buf,
246 + return sfp__i2c_read(sfp->i2c, a2 ? 0x51 : 0x50, addr, buf, len);
249 +static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
251 + struct mii_bus *i2c_mii;
254 + if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
258 + sfp->read = sfp_i2c_read;
260 + i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
261 + if (IS_ERR(i2c_mii))
262 + return PTR_ERR(i2c_mii);
264 + i2c_mii->name = "SFP I2C Bus";
265 + i2c_mii->phy_mask = ~0;
267 + ret = mdiobus_register(i2c_mii);
269 + mdiobus_free(i2c_mii);
273 + sfp->i2c_mii = i2c_mii;
280 +static unsigned int sfp_get_state(struct sfp *sfp)
282 + return sfp->get_state(sfp);
285 +static void sfp_set_state(struct sfp *sfp, unsigned int state)
287 + sfp->set_state(sfp, state);
290 +static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
292 + return sfp->read(sfp, a2, addr, buf, len);
295 +static unsigned int sfp_check(void *buf, size_t len)
299 + for (p = buf, check = 0; len; p++, len--)
306 +static void sfp_module_tx_disable(struct sfp *sfp)
308 + dev_dbg(sfp->dev, "tx disable %u -> %u\n",
309 + sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
310 + sfp->state |= SFP_F_TX_DISABLE;
311 + sfp_set_state(sfp, sfp->state);
314 +static void sfp_module_tx_enable(struct sfp *sfp)
316 + dev_dbg(sfp->dev, "tx disable %u -> %u\n",
317 + sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
318 + sfp->state &= ~SFP_F_TX_DISABLE;
319 + sfp_set_state(sfp, sfp->state);
322 +static void sfp_module_tx_fault_reset(struct sfp *sfp)
324 + unsigned int state = sfp->state;
326 + if (state & SFP_F_TX_DISABLE)
329 + sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
331 + udelay(T_RESET_US);
333 + sfp_set_state(sfp, state);
336 +/* SFP state machine */
337 +static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
340 + mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
343 + cancel_delayed_work(&sfp->timeout);
346 +static void sfp_sm_next(struct sfp *sfp, unsigned int state,
347 + unsigned int timeout)
349 + sfp->sm_state = state;
350 + sfp_sm_set_timer(sfp, timeout);
353 +static void sfp_sm_ins_next(struct sfp *sfp, unsigned int state, unsigned int timeout)
355 + sfp->sm_mod_state = state;
356 + sfp_sm_set_timer(sfp, timeout);
359 +static void sfp_sm_phy_detach(struct sfp *sfp)
361 + phy_stop(sfp->mod_phy);
363 + phylink_disconnect_phy(sfp->phylink);
364 + phy_device_remove(sfp->mod_phy);
365 + phy_device_free(sfp->mod_phy);
366 + sfp->mod_phy = NULL;
369 +static void sfp_sm_probe_phy(struct sfp *sfp)
371 + struct phy_device *phy;
374 + msleep(T_PHY_RESET_MS);
376 + phy = mdiobus_scan(sfp->i2c_mii, SFP_PHY_ADDR);
378 + dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
382 + dev_info(sfp->dev, "no PHY detected\n");
386 + err = phylink_connect_phy(sfp->phylink, phy);
388 + phy_device_remove(phy);
389 + phy_device_free(phy);
390 + dev_err(sfp->dev, "phylink_connect_phy failed: %d\n", err);
394 + sfp->mod_phy = phy;
398 +static void sfp_sm_link_up(struct sfp *sfp)
401 + phylink_enable(sfp->phylink);
403 + sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
406 +static void sfp_sm_link_down(struct sfp *sfp)
409 + phylink_disable(sfp->phylink);
412 +static void sfp_sm_link_check_los(struct sfp *sfp)
414 + unsigned int los = sfp->state & SFP_F_LOS;
416 + /* FIXME: what if neither SFP_OPTIONS_LOS_INVERTED nor
417 + * SFP_OPTIONS_LOS_NORMAL are set? For now, we assume
418 + * the same as SFP_OPTIONS_LOS_NORMAL set.
420 + if (sfp->id.ext.options & SFP_OPTIONS_LOS_INVERTED)
424 + sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
426 + sfp_sm_link_up(sfp);
429 +static void sfp_sm_fault(struct sfp *sfp, bool warn)
431 + if (sfp->sm_retries && !--sfp->sm_retries) {
432 + dev_err(sfp->dev, "module persistently indicates fault, disabling\n");
433 + sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
436 + dev_err(sfp->dev, "module transmit fault indicated\n");
438 + sfp_sm_next(sfp, SFP_S_TX_FAULT, T_FAULT_RECOVER);
442 +static void sfp_sm_mod_init(struct sfp *sfp)
444 + sfp_module_tx_enable(sfp);
446 + /* Wait t_init before indicating that the link is up, provided the
447 + * current state indicates no TX_FAULT. If TX_FAULT clears before
448 + * this time, that's fine too.
450 + sfp_sm_next(sfp, SFP_S_INIT, T_INIT_JIFFIES);
451 + sfp->sm_retries = 5;
453 + if (sfp->phylink) {
454 + /* Setting the serdes link mode is guesswork: there's no
455 + * field in the EEPROM which indicates what mode should
458 + * If it's a gigabit-only fiber module, it probably does
459 + * not have a PHY, so switch to 802.3z negotiation mode.
460 + * Otherwise, switch to SGMII mode (which is required to
461 + * support non-gigabit speeds) and probe for a PHY.
463 + if (!sfp->id.base.e1000_base_t &&
464 + !sfp->id.base.e100_base_lx &&
465 + !sfp->id.base.e100_base_fx) {
466 + phylink_set_link_an_mode(sfp->phylink, MLO_AN_8023Z);
468 + phylink_set_link_an_mode(sfp->phylink, MLO_AN_SGMII);
469 + sfp_sm_probe_phy(sfp);
474 +static int sfp_sm_mod_probe(struct sfp *sfp)
476 + /* SFP module inserted - read I2C data */
477 + struct sfp_eeprom_id id;
486 + err = sfp_read(sfp, false, 0, &id, sizeof(id));
488 + dev_err(sfp->dev, "failed to read EEPROM: %d\n", err);
492 + /* Validate the checksum over the base structure */
493 + check = sfp_check(&id.base, sizeof(id.base) - 1);
494 + if (check != id.base.cc_base) {
496 + "EEPROM base structure checksum failure: 0x%02x\n",
501 + check = sfp_check(&id.ext, sizeof(id.ext) - 1);
502 + if (check != id.ext.cc_ext) {
504 + "EEPROM extended structure checksum failure: 0x%02x\n",
506 + memset(&id.ext, 0, sizeof(id.ext));
511 + memcpy(vendor, sfp->id.base.vendor_name, 16);
513 + memcpy(part, sfp->id.base.vendor_pn, 16);
515 + memcpy(rev, sfp->id.base.vendor_rev, 4);
517 + memcpy(sn, sfp->id.ext.vendor_sn, 16);
519 + memcpy(date, sfp->id.ext.datecode, 8);
522 + dev_info(sfp->dev, "module %s %s rev %s sn %s dc %s\n", vendor, part, rev, sn, date);
524 + /* We only support SFP modules, not the legacy GBIC modules. */
525 + if (sfp->id.base.phys_id != SFP_PHYS_ID_SFP ||
526 + sfp->id.base.phys_ext_id != SFP_PHYS_EXT_ID_SFP) {
527 + dev_err(sfp->dev, "module is not SFP - phys id 0x%02x 0x%02x\n",
528 + sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
533 + * What isn't clear from the SFP documentation is whether this
534 + * specifies the encoding expected on the TD/RD lines, or whether
535 + * the TD/RD lines are always 8b10b encoded, but the transceiver
536 + * converts. Eg, think of a copper SFP supporting 1G/100M/10M
537 + * ethernet: this requires 8b10b encoding for 1G, 4b5b for 100M,
538 + * and manchester for 10M.
540 + /* 1Gbit ethernet requires 8b10b encoding */
541 + if (sfp->id.base.encoding != SFP_ENCODING_8B10B) {
542 + dev_err(sfp->dev, "module does not support 8B10B encoding\n");
546 + if (sfp->phylink) {
550 + if (sfp->id.base.e1000_base_t) {
551 + support = SUPPORTED_TP;
554 + support = SUPPORTED_FIBRE;
557 + phylink_set_link_port(sfp->phylink, support, port);
563 +static void sfp_sm_mod_remove(struct sfp *sfp)
566 + sfp_sm_phy_detach(sfp);
568 + sfp_module_tx_disable(sfp);
570 + memset(&sfp->id, 0, sizeof(sfp->id));
572 + dev_info(sfp->dev, "module removed\n");
575 +static void sfp_sm_event(struct sfp *sfp, unsigned int event)
577 + mutex_lock(&sfp->sm_mutex);
579 + dev_dbg(sfp->dev, "SM: enter %u:%u:%u event %u\n",
580 + sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state, event);
582 + /* This state machine tracks the insert/remove state of
583 + * the module, and handles probing the on-board EEPROM.
585 + switch (sfp->sm_mod_state) {
587 + if (event == SFP_E_INSERT) {
588 + sfp_module_tx_disable(sfp);
589 + sfp_sm_ins_next(sfp, SFP_MOD_PROBE, T_PROBE_INIT);
593 + case SFP_MOD_PROBE:
594 + if (event == SFP_E_REMOVE) {
595 + sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
596 + } else if (event == SFP_E_TIMEOUT) {
597 + int err = sfp_sm_mod_probe(sfp);
600 + sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
601 + else if (err == -EAGAIN)
602 + sfp_sm_set_timer(sfp, T_PROBE_RETRY);
604 + sfp_sm_ins_next(sfp, SFP_MOD_ERROR, 0);
608 + case SFP_MOD_PRESENT:
609 + case SFP_MOD_ERROR:
610 + if (event == SFP_E_REMOVE) {
611 + sfp_sm_mod_remove(sfp);
612 + sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
617 + /* This state machine tracks the netdev up/down state */
618 + switch (sfp->sm_dev_state) {
620 + if (event == SFP_E_DEV_UP)
621 + sfp->sm_dev_state = SFP_DEV_UP;
625 + if (event == SFP_E_DEV_DOWN) {
626 + /* If the module has a PHY, avoid raising TX disable
627 + * as this resets the PHY. Otherwise, raise it to
628 + * turn the laser off.
631 + sfp_module_tx_disable(sfp);
632 + sfp->sm_dev_state = SFP_DEV_DOWN;
637 + /* Some events are global */
638 + if (sfp->sm_state != SFP_S_DOWN &&
639 + (sfp->sm_mod_state != SFP_MOD_PRESENT ||
640 + sfp->sm_dev_state != SFP_DEV_UP)) {
641 + if (sfp->sm_state == SFP_S_LINK_UP &&
642 + sfp->sm_dev_state == SFP_DEV_UP)
643 + sfp_sm_link_down(sfp);
645 + sfp_sm_phy_detach(sfp);
646 + sfp_sm_next(sfp, SFP_S_DOWN, 0);
647 + mutex_unlock(&sfp->sm_mutex);
651 + /* The main state machine */
652 + switch (sfp->sm_state) {
654 + if (sfp->sm_mod_state == SFP_MOD_PRESENT &&
655 + sfp->sm_dev_state == SFP_DEV_UP)
656 + sfp_sm_mod_init(sfp);
660 + if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT)
661 + sfp_sm_fault(sfp, true);
662 + else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR)
663 + sfp_sm_link_check_los(sfp);
666 + case SFP_S_WAIT_LOS:
667 + if (event == SFP_E_TX_FAULT)
668 + sfp_sm_fault(sfp, true);
670 + (sfp->id.ext.options & SFP_OPTIONS_LOS_INVERTED ?
671 + SFP_E_LOS_HIGH : SFP_E_LOS_LOW))
672 + sfp_sm_link_up(sfp);
675 + case SFP_S_LINK_UP:
676 + if (event == SFP_E_TX_FAULT) {
677 + sfp_sm_link_down(sfp);
678 + sfp_sm_fault(sfp, true);
679 + } else if (event ==
680 + (sfp->id.ext.options & SFP_OPTIONS_LOS_INVERTED ?
681 + SFP_E_LOS_LOW : SFP_E_LOS_HIGH)) {
682 + sfp_sm_link_down(sfp);
683 + sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
687 + case SFP_S_TX_FAULT:
688 + if (event == SFP_E_TIMEOUT) {
689 + sfp_module_tx_fault_reset(sfp);
690 + sfp_sm_next(sfp, SFP_S_REINIT, T_INIT_JIFFIES);
695 + if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
696 + sfp_sm_fault(sfp, false);
697 + } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
698 + dev_info(sfp->dev, "module transmit fault recovered\n");
699 + sfp_sm_link_check_los(sfp);
703 + case SFP_S_TX_DISABLE:
707 + dev_dbg(sfp->dev, "SM: exit %u:%u:%u\n",
708 + sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state);
710 + mutex_unlock(&sfp->sm_mutex);
714 +static int sfp_phy_module_info(struct phy_device *phy,
715 + struct ethtool_modinfo *modinfo)
717 + struct sfp *sfp = phy->priv;
719 + /* locking... and check module is present */
721 + if (sfp->id.ext.sff8472_compliance) {
722 + modinfo->type = ETH_MODULE_SFF_8472;
723 + modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
725 + modinfo->type = ETH_MODULE_SFF_8079;
726 + modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
731 +static int sfp_phy_module_eeprom(struct phy_device *phy,
732 + struct ethtool_eeprom *ee, u8 *data)
734 + struct sfp *sfp = phy->priv;
735 + unsigned int first, last, len;
741 + first = ee->offset;
742 + last = ee->offset + ee->len;
743 + if (first < ETH_MODULE_SFF_8079_LEN) {
745 + if (len > ETH_MODULE_SFF_8079_LEN)
746 + len = ETH_MODULE_SFF_8079_LEN;
749 + ret = sfp->read(sfp, false, first, data, len);
756 + if (first >= ETH_MODULE_SFF_8079_LEN && last > first) {
757 + len = last - first;
759 + ret = sfp->read(sfp, true, first, data, len);
767 +static void sfp_timeout(struct work_struct *work)
769 + struct sfp *sfp = container_of(work, struct sfp, timeout.work);
771 + sfp_sm_event(sfp, SFP_E_TIMEOUT);
774 +static void sfp_check_state(struct sfp *sfp)
776 + unsigned int state, i, changed;
778 + state = sfp_get_state(sfp);
779 + changed = state ^ sfp->state;
780 + changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
782 + for (i = 0; i < GPIO_MAX; i++)
783 + if (changed & BIT(i))
784 + dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i],
785 + !!(sfp->state & BIT(i)), !!(state & BIT(i)));
787 + state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT);
788 + sfp->state = state;
790 + if (changed & SFP_F_PRESENT)
791 + sfp_sm_event(sfp, state & SFP_F_PRESENT ?
792 + SFP_E_INSERT : SFP_E_REMOVE);
794 + if (changed & SFP_F_TX_FAULT)
795 + sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
796 + SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
798 + if (changed & SFP_F_LOS)
799 + sfp_sm_event(sfp, state & SFP_F_LOS ?
800 + SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
803 +static irqreturn_t sfp_irq(int irq, void *data)
805 + struct sfp *sfp = data;
807 + sfp_check_state(sfp);
809 + return IRQ_HANDLED;
812 +static void sfp_poll(struct work_struct *work)
814 + struct sfp *sfp = container_of(work, struct sfp, poll.work);
816 + sfp_check_state(sfp);
817 + mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
820 +static int sfp_netdev_notify(struct notifier_block *nb, unsigned long act, void *data)
822 + struct sfp *sfp = container_of(nb, struct sfp, netdev_nb);
823 + struct netdev_notifier_info *info = data;
824 + struct net_device *ndev = info->dev;
826 + if (!sfp->ndev || ndev != sfp->ndev)
827 + return NOTIFY_DONE;
831 + sfp_sm_event(sfp, SFP_E_DEV_UP);
834 + case NETDEV_GOING_DOWN:
835 + sfp_sm_event(sfp, SFP_E_DEV_DOWN);
838 + case NETDEV_UNREGISTER:
839 + if (sfp->mod_phy && sfp->phylink)
840 + phylink_disconnect_phy(sfp->phylink);
841 + sfp->phylink = NULL;
842 + dev_put(sfp->ndev);
849 +static struct sfp *sfp_alloc(struct device *dev)
853 + sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
855 + return ERR_PTR(-ENOMEM);
859 + mutex_init(&sfp->sm_mutex);
860 + INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
861 + INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
863 + sfp->netdev_nb.notifier_call = sfp_netdev_notify;
868 +static void sfp_destroy(struct sfp *sfp)
870 + cancel_delayed_work_sync(&sfp->poll);
871 + cancel_delayed_work_sync(&sfp->timeout);
872 + if (sfp->i2c_mii) {
873 + mdiobus_unregister(sfp->i2c_mii);
874 + mdiobus_free(sfp->i2c_mii);
877 + i2c_put_adapter(sfp->i2c);
878 + of_node_put(sfp->dev->of_node);
882 +static void sfp_cleanup(void *data)
884 + struct sfp *sfp = data;
889 +static int sfp_probe(struct platform_device *pdev)
895 + sfp = sfp_alloc(&pdev->dev);
897 + return PTR_ERR(sfp);
899 + platform_set_drvdata(pdev, sfp);
901 + err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
905 + if (pdev->dev.of_node) {
906 + struct device_node *node = pdev->dev.of_node;
907 + struct device_node *np;
909 + np = of_parse_phandle(node, "i2c-bus", 0);
911 + struct i2c_adapter *i2c;
913 + i2c = of_find_i2c_adapter_by_node(np);
916 + return -EPROBE_DEFER;
918 + err = sfp_i2c_configure(sfp, i2c);
920 + i2c_put_adapter(i2c);
925 + for (i = 0; i < GPIO_MAX; i++) {
926 + sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
927 + gpio_of_names[i], gpio_flags[i]);
928 + if (IS_ERR(sfp->gpio[i]))
929 + return PTR_ERR(sfp->gpio[i]);
932 + sfp->get_state = sfp_gpio_get_state;
933 + sfp->set_state = sfp_gpio_set_state;
935 + np = of_parse_phandle(node, "sfp,ethernet", 0);
937 + dev_err(sfp->dev, "missing sfp,ethernet property\n");
941 + sfp->ndev = of_find_net_device_by_node(np);
943 + dev_err(sfp->dev, "ethernet device not found\n");
944 + return -EPROBE_DEFER;
947 + dev_hold(sfp->ndev);
948 + put_device(&sfp->ndev->dev);
950 + sfp->phylink = phylink_lookup_by_netdev(sfp->ndev);
951 + if (!sfp->phylink) {
952 + dev_err(sfp->dev, "ethernet device not found\n");
953 + return -EPROBE_DEFER;
956 + phylink_disable(sfp->phylink);
959 + sfp->state = sfp_get_state(sfp);
960 + if (sfp->gpio[GPIO_TX_DISABLE] &&
961 + gpiod_get_value_cansleep(sfp->gpio[GPIO_TX_DISABLE]))
962 + sfp->state |= SFP_F_TX_DISABLE;
963 + if (sfp->gpio[GPIO_RATE_SELECT] &&
964 + gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT]))
965 + sfp->state |= SFP_F_RATE_SELECT;
966 + sfp_set_state(sfp, sfp->state);
967 + sfp_module_tx_disable(sfp);
968 + if (sfp->state & SFP_F_PRESENT)
969 + sfp_sm_event(sfp, SFP_E_INSERT);
971 + for (i = 0; i < GPIO_MAX; i++) {
972 + if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
975 + irq = gpiod_to_irq(sfp->gpio[i]);
981 + err = devm_request_threaded_irq(sfp->dev, irq, NULL, sfp_irq,
983 + IRQF_TRIGGER_RISING |
984 + IRQF_TRIGGER_FALLING,
985 + dev_name(sfp->dev), sfp);
991 + mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
993 + register_netdevice_notifier(&sfp->netdev_nb);
998 +static int sfp_remove(struct platform_device *pdev)
1000 + struct sfp *sfp = platform_get_drvdata(pdev);
1002 + unregister_netdevice_notifier(&sfp->netdev_nb);
1004 + dev_put(sfp->ndev);
1009 +static const struct of_device_id sfp_of_match[] = {
1010 + { .compatible = "sff,sfp", },
1013 +MODULE_DEVICE_TABLE(of, sfp_of_match);
1015 +static struct platform_driver sfp_driver = {
1016 + .probe = sfp_probe,
1017 + .remove = sfp_remove,
1020 + .of_match_table = sfp_of_match,
1024 +static int sfp_init(void)
1026 + poll_jiffies = msecs_to_jiffies(100);
1028 + return platform_driver_register(&sfp_driver);
1030 +module_init(sfp_init);
1032 +static void sfp_exit(void)
1034 + platform_driver_unregister(&sfp_driver);
1036 +module_exit(sfp_exit);
1038 +MODULE_ALIAS("platform:sfp");
1039 +MODULE_AUTHOR("Russell King");
1040 +MODULE_LICENSE("GPL v2");
1042 +++ b/include/linux/sfp.h
1044 +#ifndef LINUX_SFP_H
1045 +#define LINUX_SFP_H
1047 +struct __packed sfp_eeprom_base {
1051 +#if defined __BIG_ENDIAN_BITFIELD
1052 + u8 e10g_base_er:1;
1053 + u8 e10g_base_lrm:1;
1054 + u8 e10g_base_lr:1;
1055 + u8 e10g_base_sr:1;
1058 + u8 if_1x_copper_active:1;
1059 + u8 if_1x_copper_passive:1;
1061 + u8 escon_mmf_1310_led:1;
1062 + u8 escon_smf_1310_laser:1;
1063 + u8 sonet_oc192_short_reach:1;
1064 + u8 sonet_reach_bit1:1;
1065 + u8 sonet_reach_bit2:1;
1066 + u8 sonet_oc48_long_reach:1;
1067 + u8 sonet_oc48_intermediate_reach:1;
1068 + u8 sonet_oc48_short_reach:1;
1070 + u8 unallocated_5_7:1;
1071 + u8 sonet_oc12_smf_long_reach:1;
1072 + u8 sonet_oc12_smf_intermediate_reach:1;
1073 + u8 sonet_oc12_short_reach:1;
1074 + u8 unallocated_5_3:1;
1075 + u8 sonet_oc3_smf_long_reach:1;
1076 + u8 sonet_oc3_smf_intermediate_reach:1;
1077 + u8 sonet_oc3_short_reach:1;
1081 + u8 e100_base_fx:1;
1082 + u8 e100_base_lx:1;
1083 + u8 e1000_base_t:1;
1084 + u8 e1000_base_cx:1;
1085 + u8 e1000_base_lx:1;
1086 + u8 e1000_base_sx:1;
1095 + u8 fc_tech_electrical_inter_enclosure:1;
1097 + u8 fc_tech_electrical_intra_enclosure:1;
1101 + u8 sfp_ct_active:1;
1102 + u8 sfp_ct_passive:1;
1103 + u8 unallocated_8_1:1;
1104 + u8 unallocated_8_0:1;
1112 + u8 unallocated_9_1:1;
1115 + u8 fc_speed_1200:1;
1116 + u8 fc_speed_800:1;
1117 + u8 fc_speed_1600:1;
1118 + u8 fc_speed_400:1;
1119 + u8 fc_speed_3200:1;
1120 + u8 fc_speed_200:1;
1121 + u8 unallocated_10_1:1;
1122 + u8 fc_speed_100:1;
1123 +#elif defined __LITTLE_ENDIAN_BITFIELD
1124 + u8 if_1x_copper_passive:1;
1125 + u8 if_1x_copper_active:1;
1128 + u8 e10g_base_sr:1;
1129 + u8 e10g_base_lr:1;
1130 + u8 e10g_base_lrm:1;
1131 + u8 e10g_base_er:1;
1133 + u8 sonet_oc3_short_reach:1;
1134 + u8 sonet_oc3_smf_intermediate_reach:1;
1135 + u8 sonet_oc3_smf_long_reach:1;
1136 + u8 unallocated_5_3:1;
1137 + u8 sonet_oc12_short_reach:1;
1138 + u8 sonet_oc12_smf_intermediate_reach:1;
1139 + u8 sonet_oc12_smf_long_reach:1;
1140 + u8 unallocated_5_7:1;
1142 + u8 sonet_oc48_short_reach:1;
1143 + u8 sonet_oc48_intermediate_reach:1;
1144 + u8 sonet_oc48_long_reach:1;
1145 + u8 sonet_reach_bit2:1;
1146 + u8 sonet_reach_bit1:1;
1147 + u8 sonet_oc192_short_reach:1;
1148 + u8 escon_smf_1310_laser:1;
1149 + u8 escon_mmf_1310_led:1;
1151 + u8 e1000_base_sx:1;
1152 + u8 e1000_base_lx:1;
1153 + u8 e1000_base_cx:1;
1154 + u8 e1000_base_t:1;
1155 + u8 e100_base_lx:1;
1156 + u8 e100_base_fx:1;
1160 + u8 fc_tech_electrical_inter_enclosure:1;
1169 + u8 unallocated_8_0:1;
1170 + u8 unallocated_8_1:1;
1171 + u8 sfp_ct_passive:1;
1172 + u8 sfp_ct_active:1;
1176 + u8 fc_tech_electrical_intra_enclosure:1;
1179 + u8 unallocated_9_1:1;
1187 + u8 fc_speed_100:1;
1188 + u8 unallocated_10_1:1;
1189 + u8 fc_speed_200:1;
1190 + u8 fc_speed_3200:1;
1191 + u8 fc_speed_400:1;
1192 + u8 fc_speed_1600:1;
1193 + u8 fc_speed_800:1;
1194 + u8 fc_speed_1200:1;
1196 +#error Unknown Endian
1202 + char vendor_name[16];
1204 + char vendor_oui[3];
1205 + char vendor_pn[16];
1206 + char vendor_rev[4];
1208 + __be16 optical_wavelength;
1215 +struct __packed sfp_eeprom_ext {
1219 + char vendor_sn[16];
1223 + u8 sff8472_compliance;
1227 +struct __packed sfp_eeprom_id {
1228 + struct sfp_eeprom_base base;
1229 + struct sfp_eeprom_ext ext;
1232 +/* SFP EEPROM registers */
1234 + SFP_PHYS_ID = 0x00,
1235 + SFP_PHYS_EXT_ID = 0x01,
1236 + SFP_CONNECTOR = 0x02,
1237 + SFP_COMPLIANCE = 0x03,
1238 + SFP_ENCODING = 0x0b,
1239 + SFP_BR_NOMINAL = 0x0c,
1240 + SFP_RATE_ID = 0x0d,
1241 + SFP_LINK_LEN_SM_KM = 0x0e,
1242 + SFP_LINK_LEN_SM_100M = 0x0f,
1243 + SFP_LINK_LEN_50UM_OM2_10M = 0x10,
1244 + SFP_LINK_LEN_62_5UM_OM1_10M = 0x11,
1245 + SFP_LINK_LEN_COPPER_1M = 0x12,
1246 + SFP_LINK_LEN_50UM_OM4_10M = 0x12,
1247 + SFP_LINK_LEN_50UM_OM3_10M = 0x13,
1248 + SFP_VENDOR_NAME = 0x14,
1249 + SFP_VENDOR_OUI = 0x25,
1250 + SFP_VENDOR_PN = 0x28,
1251 + SFP_VENDOR_REV = 0x38,
1252 + SFP_OPTICAL_WAVELENGTH_MSB = 0x3c,
1253 + SFP_OPTICAL_WAVELENGTH_LSB = 0x3d,
1254 + SFP_CABLE_SPEC = 0x3c,
1255 + SFP_CC_BASE = 0x3f,
1256 + SFP_OPTIONS = 0x40, /* 2 bytes, MSB, LSB */
1257 + SFP_BR_MAX = 0x42,
1258 + SFP_BR_MIN = 0x43,
1259 + SFP_VENDOR_SN = 0x44,
1260 + SFP_DATECODE = 0x54,
1261 + SFP_DIAGMON = 0x5c,
1262 + SFP_ENHOPTS = 0x5d,
1263 + SFP_SFF8472_COMPLIANCE = 0x5e,
1264 + SFP_CC_EXT = 0x5f,
1266 + SFP_PHYS_ID_SFP = 0x03,
1267 + SFP_PHYS_EXT_ID_SFP = 0x04,
1268 + SFP_CONNECTOR_UNSPEC = 0x00,
1269 + /* codes 01-05 not supportable on SFP, but some modules have single SC */
1270 + SFP_CONNECTOR_SC = 0x01,
1271 + SFP_CONNECTOR_FIBERJACK = 0x06,
1272 + SFP_CONNECTOR_LC = 0x07,
1273 + SFP_CONNECTOR_MT_RJ = 0x08,
1274 + SFP_CONNECTOR_MU = 0x09,
1275 + SFP_CONNECTOR_SG = 0x0a,
1276 + SFP_CONNECTOR_OPTICAL_PIGTAIL = 0x0b,
1277 + SFP_CONNECTOR_HSSDC_II = 0x20,
1278 + SFP_CONNECTOR_COPPER_PIGTAIL = 0x21,
1279 + SFP_ENCODING_UNSPEC = 0x00,
1280 + SFP_ENCODING_8B10B = 0x01,
1281 + SFP_ENCODING_4B5B = 0x02,
1282 + SFP_ENCODING_NRZ = 0x03,
1283 + SFP_ENCODING_MANCHESTER = 0x04,
1284 + SFP_OPTIONS_HIGH_POWER_LEVEL = BIT(13),
1285 + SFP_OPTIONS_PAGING_A2 = BIT(12),
1286 + SFP_OPTIONS_RETIMER = BIT(11),
1287 + SFP_OPTIONS_COOLED_XCVR = BIT(10),
1288 + SFP_OPTIONS_POWER_DECL = BIT(9),
1289 + SFP_OPTIONS_RX_LINEAR_OUT = BIT(8),
1290 + SFP_OPTIONS_RX_DECISION_THRESH = BIT(7),
1291 + SFP_OPTIONS_TUNABLE_TX = BIT(6),
1292 + SFP_OPTIONS_RATE_SELECT = BIT(5),
1293 + SFP_OPTIONS_TX_DISABLE = BIT(4),
1294 + SFP_OPTIONS_TX_FAULT = BIT(3),
1295 + SFP_OPTIONS_LOS_INVERTED = BIT(2),
1296 + SFP_OPTIONS_LOS_NORMAL = BIT(1),
1297 + SFP_DIAGMON_DDM = BIT(6),
1298 + SFP_DIAGMON_INT_CAL = BIT(5),
1299 + SFP_DIAGMON_EXT_CAL = BIT(4),
1300 + SFP_DIAGMON_RXPWR_AVG = BIT(3),
1301 + SFP_DIAGMON_ADDRMODE = BIT(2),
1302 + SFP_ENHOPTS_ALARMWARN = BIT(7),
1303 + SFP_ENHOPTS_SOFT_TX_DISABLE = BIT(6),
1304 + SFP_ENHOPTS_SOFT_TX_FAULT = BIT(5),
1305 + SFP_ENHOPTS_SOFT_RX_LOS = BIT(4),
1306 + SFP_ENHOPTS_SOFT_RATE_SELECT = BIT(3),
1307 + SFP_ENHOPTS_APP_SELECT_SFF8079 = BIT(2),
1308 + SFP_ENHOPTS_SOFT_RATE_SFF8431 = BIT(1),
1309 + SFP_SFF8472_COMPLIANCE_NONE = 0x00,
1310 + SFP_SFF8472_COMPLIANCE_REV9_3 = 0x01,
1311 + SFP_SFF8472_COMPLIANCE_REV9_5 = 0x02,
1312 + SFP_SFF8472_COMPLIANCE_REV10_2 = 0x03,
1313 + SFP_SFF8472_COMPLIANCE_REV10_4 = 0x04,
1314 + SFP_SFF8472_COMPLIANCE_REV11_0 = 0x05,
1315 + SFP_SFF8472_COMPLIANCE_REV11_3 = 0x06,
1316 + SFP_SFF8472_COMPLIANCE_REV11_4 = 0x07,
1317 + SFP_SFF8472_COMPLIANCE_REV12_0 = 0x08,
1320 +/* SFP Diagnostics */
1322 + /* Alarm and warnings stored MSB at lower address then LSB */
1323 + SFP_TEMP_HIGH_ALARM = 0x00,
1324 + SFP_TEMP_LOW_ALARM = 0x02,
1325 + SFP_TEMP_HIGH_WARN = 0x04,
1326 + SFP_TEMP_LOW_WARN = 0x06,
1327 + SFP_VOLT_HIGH_ALARM = 0x08,
1328 + SFP_VOLT_LOW_ALARM = 0x0a,
1329 + SFP_VOLT_HIGH_WARN = 0x0c,
1330 + SFP_VOLT_LOW_WARN = 0x0e,
1331 + SFP_BIAS_HIGH_ALARM = 0x10,
1332 + SFP_BIAS_LOW_ALARM = 0x12,
1333 + SFP_BIAS_HIGH_WARN = 0x14,
1334 + SFP_BIAS_LOW_WARN = 0x16,
1335 + SFP_TXPWR_HIGH_ALARM = 0x18,
1336 + SFP_TXPWR_LOW_ALARM = 0x1a,
1337 + SFP_TXPWR_HIGH_WARN = 0x1c,
1338 + SFP_TXPWR_LOW_WARN = 0x1e,
1339 + SFP_RXPWR_HIGH_ALARM = 0x20,
1340 + SFP_RXPWR_LOW_ALARM = 0x22,
1341 + SFP_RXPWR_HIGH_WARN = 0x24,
1342 + SFP_RXPWR_LOW_WARN = 0x26,
1343 + SFP_LASER_TEMP_HIGH_ALARM = 0x28,
1344 + SFP_LASER_TEMP_LOW_ALARM = 0x2a,
1345 + SFP_LASER_TEMP_HIGH_WARN = 0x2c,
1346 + SFP_LASER_TEMP_LOW_WARN = 0x2e,
1347 + SFP_TEC_CUR_HIGH_ALARM = 0x30,
1348 + SFP_TEC_CUR_LOW_ALARM = 0x32,
1349 + SFP_TEC_CUR_HIGH_WARN = 0x34,
1350 + SFP_TEC_CUR_LOW_WARN = 0x36,
1351 + SFP_CAL_RXPWR4 = 0x38,
1352 + SFP_CAL_RXPWR3 = 0x3c,
1353 + SFP_CAL_RXPWR2 = 0x40,
1354 + SFP_CAL_RXPWR1 = 0x44,
1355 + SFP_CAL_RXPWR0 = 0x48,
1356 + SFP_CAL_TXI_SLOPE = 0x4c,
1357 + SFP_CAL_TXI_OFFSET = 0x4e,
1358 + SFP_CAL_TXPWR_SLOPE = 0x50,
1359 + SFP_CAL_TXPWR_OFFSET = 0x52,
1360 + SFP_CAL_T_SLOPE = 0x54,
1361 + SFP_CAL_T_OFFSET = 0x56,
1362 + SFP_CAL_V_SLOPE = 0x58,
1363 + SFP_CAL_V_OFFSET = 0x5a,
1364 + SFP_CHKSUM = 0x5f,
1368 + SFP_TX_BIAS = 0x64,
1369 + SFP_TX_POWER = 0x66,
1370 + SFP_RX_POWER = 0x68,
1371 + SFP_LASER_TEMP = 0x6a,
1372 + SFP_TEC_CUR = 0x6c,
1374 + SFP_STATUS = 0x6e,
1377 + SFP_EXT_STATUS = 0x76,