Commit 7a28c886 authored by David S. Miller's avatar David S. Miller

Merge branch 'sfp-Allow-slow-to-initialise-GPON-modules-to-work'

Russell King says:

====================
sfp: Allow slow to initialise GPON modules to work

Some GPON modules take longer than the SFF MSA specified time to
initialise and respond to transactions on the I2C bus for either
both 0x50 and 0x51, or 0x51 bus addresses.  Technically these modules
are non-compliant with the SFP Multi-Source Agreement, they have
been around for some time, so are difficult to just ignore.

Most of the patch series is restructuring the code to make it more
readable, and split various things into separate functions.

We split the three state machines into three separate functions, and
re-arrange them to start probing the module as soon as a module has
been detected (without waiting for the network device.)  We try to
read the module's EEPROM, retrying quickly for the first second, and
then once every five seconds for about a minute until we have read
the EEPROM.  So that the kernel isn't entirely silent, we print a
message indicating that we're waiting for the module to respond after
the first second, or when all retries have expired.

Once the module ID has been read, we kick off a delayed work queue
which attempts to register the hwmon, retrying for up to a minute if
the monitoring parameters are unreadable; this allows us to proceed
with module initialisation independently of the hwmon state.

With high-power modules, we wait for the netdev to be attached before
switching the module power mode, and retry this in a similar way to
before until we have successfully read and written the EEPROM at 0x51.

We also move the handling of the TX_DISABLE signal entirely to the main
state machine, and avoid probing any on-board PHY while TX_FAULT is
set.
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 2fca4ac9 139d3a21
...@@ -36,6 +36,8 @@ enum { ...@@ -36,6 +36,8 @@ enum {
SFP_E_INSERT = 0, SFP_E_INSERT = 0,
SFP_E_REMOVE, SFP_E_REMOVE,
SFP_E_DEV_ATTACH,
SFP_E_DEV_DETACH,
SFP_E_DEV_DOWN, SFP_E_DEV_DOWN,
SFP_E_DEV_UP, SFP_E_DEV_UP,
SFP_E_TX_FAULT, SFP_E_TX_FAULT,
...@@ -45,16 +47,21 @@ enum { ...@@ -45,16 +47,21 @@ enum {
SFP_E_TIMEOUT, SFP_E_TIMEOUT,
SFP_MOD_EMPTY = 0, SFP_MOD_EMPTY = 0,
SFP_MOD_ERROR,
SFP_MOD_PROBE, SFP_MOD_PROBE,
SFP_MOD_WAITDEV,
SFP_MOD_HPOWER, SFP_MOD_HPOWER,
SFP_MOD_WAITPWR,
SFP_MOD_PRESENT, SFP_MOD_PRESENT,
SFP_MOD_ERROR,
SFP_DEV_DOWN = 0, SFP_DEV_DETACHED = 0,
SFP_DEV_DOWN,
SFP_DEV_UP, SFP_DEV_UP,
SFP_S_DOWN = 0, SFP_S_DOWN = 0,
SFP_S_WAIT,
SFP_S_INIT, SFP_S_INIT,
SFP_S_INIT_TX_FAULT,
SFP_S_WAIT_LOS, SFP_S_WAIT_LOS,
SFP_S_LINK_UP, SFP_S_LINK_UP,
SFP_S_TX_FAULT, SFP_S_TX_FAULT,
...@@ -64,10 +71,12 @@ enum { ...@@ -64,10 +71,12 @@ enum {
static const char * const mod_state_strings[] = { static const char * const mod_state_strings[] = {
[SFP_MOD_EMPTY] = "empty", [SFP_MOD_EMPTY] = "empty",
[SFP_MOD_ERROR] = "error",
[SFP_MOD_PROBE] = "probe", [SFP_MOD_PROBE] = "probe",
[SFP_MOD_WAITDEV] = "waitdev",
[SFP_MOD_HPOWER] = "hpower", [SFP_MOD_HPOWER] = "hpower",
[SFP_MOD_WAITPWR] = "waitpwr",
[SFP_MOD_PRESENT] = "present", [SFP_MOD_PRESENT] = "present",
[SFP_MOD_ERROR] = "error",
}; };
static const char *mod_state_to_str(unsigned short mod_state) static const char *mod_state_to_str(unsigned short mod_state)
...@@ -78,6 +87,7 @@ static const char *mod_state_to_str(unsigned short mod_state) ...@@ -78,6 +87,7 @@ static const char *mod_state_to_str(unsigned short mod_state)
} }
static const char * const dev_state_strings[] = { static const char * const dev_state_strings[] = {
[SFP_DEV_DETACHED] = "detached",
[SFP_DEV_DOWN] = "down", [SFP_DEV_DOWN] = "down",
[SFP_DEV_UP] = "up", [SFP_DEV_UP] = "up",
}; };
...@@ -92,6 +102,8 @@ static const char *dev_state_to_str(unsigned short dev_state) ...@@ -92,6 +102,8 @@ static const char *dev_state_to_str(unsigned short dev_state)
static const char * const event_strings[] = { static const char * const event_strings[] = {
[SFP_E_INSERT] = "insert", [SFP_E_INSERT] = "insert",
[SFP_E_REMOVE] = "remove", [SFP_E_REMOVE] = "remove",
[SFP_E_DEV_ATTACH] = "dev_attach",
[SFP_E_DEV_DETACH] = "dev_detach",
[SFP_E_DEV_DOWN] = "dev_down", [SFP_E_DEV_DOWN] = "dev_down",
[SFP_E_DEV_UP] = "dev_up", [SFP_E_DEV_UP] = "dev_up",
[SFP_E_TX_FAULT] = "tx_fault", [SFP_E_TX_FAULT] = "tx_fault",
...@@ -110,7 +122,9 @@ static const char *event_to_str(unsigned short event) ...@@ -110,7 +122,9 @@ static const char *event_to_str(unsigned short event)
static const char * const sm_state_strings[] = { static const char * const sm_state_strings[] = {
[SFP_S_DOWN] = "down", [SFP_S_DOWN] = "down",
[SFP_S_WAIT] = "wait",
[SFP_S_INIT] = "init", [SFP_S_INIT] = "init",
[SFP_S_INIT_TX_FAULT] = "init_tx_fault",
[SFP_S_WAIT_LOS] = "wait_los", [SFP_S_WAIT_LOS] = "wait_los",
[SFP_S_LINK_UP] = "link_up", [SFP_S_LINK_UP] = "link_up",
[SFP_S_TX_FAULT] = "tx_fault", [SFP_S_TX_FAULT] = "tx_fault",
...@@ -141,6 +155,7 @@ static const enum gpiod_flags gpio_flags[] = { ...@@ -141,6 +155,7 @@ static const enum gpiod_flags gpio_flags[] = {
GPIOD_ASIS, GPIOD_ASIS,
}; };
#define T_WAIT msecs_to_jiffies(50)
#define T_INIT_JIFFIES msecs_to_jiffies(300) #define T_INIT_JIFFIES msecs_to_jiffies(300)
#define T_RESET_US 10 #define T_RESET_US 10
#define T_FAULT_RECOVER msecs_to_jiffies(1000) #define T_FAULT_RECOVER msecs_to_jiffies(1000)
...@@ -149,22 +164,21 @@ static const enum gpiod_flags gpio_flags[] = { ...@@ -149,22 +164,21 @@ static const enum gpiod_flags gpio_flags[] = {
* the same length on the PCB, which means it's possible for MOD DEF 0 to * the same length on the PCB, which means it's possible for MOD DEF 0 to
* connect before the I2C bus on MOD DEF 1/2. * connect before the I2C bus on MOD DEF 1/2.
* *
* The SFP MSA specifies 300ms as t_init (the time taken for TX_FAULT to * The SFF-8472 specifies t_serial ("Time from power on until module is
* be deasserted) but makes no mention of the earliest time before we can * ready for data transmission over the two wire serial bus.") as 300ms.
* access the I2C EEPROM. However, Avago modules require 300ms.
*/ */
#define T_PROBE_INIT msecs_to_jiffies(300) #define T_SERIAL msecs_to_jiffies(300)
#define T_HPOWER_LEVEL msecs_to_jiffies(300) #define T_HPOWER_LEVEL msecs_to_jiffies(300)
#define T_PROBE_RETRY msecs_to_jiffies(100) #define T_PROBE_RETRY_INIT msecs_to_jiffies(100)
#define R_PROBE_RETRY_INIT 10
#define T_PROBE_RETRY_SLOW msecs_to_jiffies(5000)
#define R_PROBE_RETRY_SLOW 12
/* SFP modules appear to always have their PHY configured for bus address /* SFP modules appear to always have their PHY configured for bus address
* 0x56 (which with mdio-i2c, translates to a PHY address of 22). * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
*/ */
#define SFP_PHY_ADDR 22 #define SFP_PHY_ADDR 22
/* Give this long for the PHY to reset. */
#define T_PHY_RESET_MS 50
struct sff_data { struct sff_data {
unsigned int gpios; unsigned int gpios;
bool (*module_supported)(const struct sfp_eeprom_id *id); bool (*module_supported)(const struct sfp_eeprom_id *id);
...@@ -187,20 +201,25 @@ struct sfp { ...@@ -187,20 +201,25 @@ struct sfp {
struct gpio_desc *gpio[GPIO_MAX]; struct gpio_desc *gpio[GPIO_MAX];
int gpio_irq[GPIO_MAX]; int gpio_irq[GPIO_MAX];
bool attached;
struct mutex st_mutex; /* Protects state */ struct mutex st_mutex; /* Protects state */
unsigned int state; unsigned int state;
struct delayed_work poll; struct delayed_work poll;
struct delayed_work timeout; struct delayed_work timeout;
struct mutex sm_mutex; /* Protects state machine */ struct mutex sm_mutex; /* Protects state machine */
unsigned char sm_mod_state; unsigned char sm_mod_state;
unsigned char sm_mod_tries_init;
unsigned char sm_mod_tries;
unsigned char sm_dev_state; unsigned char sm_dev_state;
unsigned short sm_state; unsigned short sm_state;
unsigned int sm_retries; unsigned int sm_retries;
struct sfp_eeprom_id id; struct sfp_eeprom_id id;
unsigned int module_power_mW;
#if IS_ENABLED(CONFIG_HWMON) #if IS_ENABLED(CONFIG_HWMON)
struct sfp_diag diag; struct sfp_diag diag;
struct delayed_work hwmon_probe;
unsigned int hwmon_tries;
struct device *hwmon_dev; struct device *hwmon_dev;
char *hwmon_name; char *hwmon_name;
#endif #endif
...@@ -1142,29 +1161,27 @@ static const struct hwmon_chip_info sfp_hwmon_chip_info = { ...@@ -1142,29 +1161,27 @@ static const struct hwmon_chip_info sfp_hwmon_chip_info = {
.info = sfp_hwmon_info, .info = sfp_hwmon_info,
}; };
static int sfp_hwmon_insert(struct sfp *sfp) static void sfp_hwmon_probe(struct work_struct *work)
{ {
struct sfp *sfp = container_of(work, struct sfp, hwmon_probe.work);
int err, i; int err, i;
if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE)
return 0;
if (!(sfp->id.ext.diagmon & SFP_DIAGMON_DDM))
return 0;
if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
/* This driver in general does not support address
* change.
*/
return 0;
err = sfp_read(sfp, true, 0, &sfp->diag, sizeof(sfp->diag)); err = sfp_read(sfp, true, 0, &sfp->diag, sizeof(sfp->diag));
if (err < 0) if (err < 0) {
return err; if (sfp->hwmon_tries--) {
mod_delayed_work(system_wq, &sfp->hwmon_probe,
T_PROBE_RETRY_SLOW);
} else {
dev_warn(sfp->dev, "hwmon probe failed: %d\n", err);
}
return;
}
sfp->hwmon_name = kstrdup(dev_name(sfp->dev), GFP_KERNEL); sfp->hwmon_name = kstrdup(dev_name(sfp->dev), GFP_KERNEL);
if (!sfp->hwmon_name) if (!sfp->hwmon_name) {
return -ENODEV; dev_err(sfp->dev, "out of memory for hwmon name\n");
return;
}
for (i = 0; sfp->hwmon_name[i]; i++) for (i = 0; sfp->hwmon_name[i]; i++)
if (hwmon_is_bad_char(sfp->hwmon_name[i])) if (hwmon_is_bad_char(sfp->hwmon_name[i]))
...@@ -1174,18 +1191,52 @@ static int sfp_hwmon_insert(struct sfp *sfp) ...@@ -1174,18 +1191,52 @@ static int sfp_hwmon_insert(struct sfp *sfp)
sfp->hwmon_name, sfp, sfp->hwmon_name, sfp,
&sfp_hwmon_chip_info, &sfp_hwmon_chip_info,
NULL); NULL);
if (IS_ERR(sfp->hwmon_dev))
dev_err(sfp->dev, "failed to register hwmon device: %ld\n",
PTR_ERR(sfp->hwmon_dev));
}
static int sfp_hwmon_insert(struct sfp *sfp)
{
if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE)
return 0;
if (!(sfp->id.ext.diagmon & SFP_DIAGMON_DDM))
return 0;
if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
/* This driver in general does not support address
* change.
*/
return 0;
return PTR_ERR_OR_ZERO(sfp->hwmon_dev); mod_delayed_work(system_wq, &sfp->hwmon_probe, 1);
sfp->hwmon_tries = R_PROBE_RETRY_SLOW;
return 0;
} }
static void sfp_hwmon_remove(struct sfp *sfp) static void sfp_hwmon_remove(struct sfp *sfp)
{ {
cancel_delayed_work_sync(&sfp->hwmon_probe);
if (!IS_ERR_OR_NULL(sfp->hwmon_dev)) { if (!IS_ERR_OR_NULL(sfp->hwmon_dev)) {
hwmon_device_unregister(sfp->hwmon_dev); hwmon_device_unregister(sfp->hwmon_dev);
sfp->hwmon_dev = NULL; sfp->hwmon_dev = NULL;
kfree(sfp->hwmon_name); kfree(sfp->hwmon_name);
} }
} }
static int sfp_hwmon_init(struct sfp *sfp)
{
INIT_DELAYED_WORK(&sfp->hwmon_probe, sfp_hwmon_probe);
return 0;
}
static void sfp_hwmon_exit(struct sfp *sfp)
{
cancel_delayed_work_sync(&sfp->hwmon_probe);
}
#else #else
static int sfp_hwmon_insert(struct sfp *sfp) static int sfp_hwmon_insert(struct sfp *sfp)
{ {
...@@ -1195,6 +1246,15 @@ static int sfp_hwmon_insert(struct sfp *sfp) ...@@ -1195,6 +1246,15 @@ static int sfp_hwmon_insert(struct sfp *sfp)
static void sfp_hwmon_remove(struct sfp *sfp) static void sfp_hwmon_remove(struct sfp *sfp)
{ {
} }
static int sfp_hwmon_init(struct sfp *sfp)
{
return 0;
}
static void sfp_hwmon_exit(struct sfp *sfp)
{
}
#endif #endif
/* Helpers */ /* Helpers */
...@@ -1245,7 +1305,7 @@ static void sfp_sm_next(struct sfp *sfp, unsigned int state, ...@@ -1245,7 +1305,7 @@ static void sfp_sm_next(struct sfp *sfp, unsigned int state,
sfp_sm_set_timer(sfp, timeout); sfp_sm_set_timer(sfp, timeout);
} }
static void sfp_sm_ins_next(struct sfp *sfp, unsigned int state, static void sfp_sm_mod_next(struct sfp *sfp, unsigned int state,
unsigned int timeout) unsigned int timeout)
{ {
sfp->sm_mod_state = state; sfp->sm_mod_state = state;
...@@ -1266,8 +1326,6 @@ static void sfp_sm_probe_phy(struct sfp *sfp) ...@@ -1266,8 +1326,6 @@ static void sfp_sm_probe_phy(struct sfp *sfp)
struct phy_device *phy; struct phy_device *phy;
int err; int err;
msleep(T_PHY_RESET_MS);
phy = mdiobus_scan(sfp->i2c_mii, SFP_PHY_ADDR); phy = mdiobus_scan(sfp->i2c_mii, SFP_PHY_ADDR);
if (phy == ERR_PTR(-ENODEV)) { if (phy == ERR_PTR(-ENODEV)) {
dev_info(sfp->dev, "no PHY detected\n"); dev_info(sfp->dev, "no PHY detected\n");
...@@ -1335,7 +1393,7 @@ static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event) ...@@ -1335,7 +1393,7 @@ static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event)
event == SFP_E_LOS_LOW); event == SFP_E_LOS_LOW);
} }
static void sfp_sm_fault(struct sfp *sfp, bool warn) static void sfp_sm_fault(struct sfp *sfp, unsigned int next_state, bool warn)
{ {
if (sfp->sm_retries && !--sfp->sm_retries) { if (sfp->sm_retries && !--sfp->sm_retries) {
dev_err(sfp->dev, dev_err(sfp->dev,
...@@ -1345,21 +1403,17 @@ static void sfp_sm_fault(struct sfp *sfp, bool warn) ...@@ -1345,21 +1403,17 @@ static void sfp_sm_fault(struct sfp *sfp, bool warn)
if (warn) if (warn)
dev_err(sfp->dev, "module transmit fault indicated\n"); dev_err(sfp->dev, "module transmit fault indicated\n");
sfp_sm_next(sfp, SFP_S_TX_FAULT, T_FAULT_RECOVER); sfp_sm_next(sfp, next_state, T_FAULT_RECOVER);
} }
} }
static void sfp_sm_mod_init(struct sfp *sfp) static void sfp_sm_mod_init(struct sfp *sfp)
{ {
sfp_module_tx_enable(sfp); sfp_module_tx_enable(sfp);
}
/* Wait t_init before indicating that the link is up, provided the static void sfp_sm_probe_for_phy(struct sfp *sfp)
* current state indicates no TX_FAULT. If TX_FAULT clears before {
* this time, that's fine too.
*/
sfp_sm_next(sfp, SFP_S_INIT, T_INIT_JIFFIES);
sfp->sm_retries = 5;
/* Setting the serdes link mode is guesswork: there's no /* Setting the serdes link mode is guesswork: there's no
* field in the EEPROM which indicates what mode should * field in the EEPROM which indicates what mode should
* be used. * be used.
...@@ -1375,69 +1429,83 @@ static void sfp_sm_mod_init(struct sfp *sfp) ...@@ -1375,69 +1429,83 @@ static void sfp_sm_mod_init(struct sfp *sfp)
sfp_sm_probe_phy(sfp); sfp_sm_probe_phy(sfp);
} }
static int sfp_sm_mod_hpower(struct sfp *sfp) static int sfp_module_parse_power(struct sfp *sfp)
{ {
u32 power; u32 power_mW = 1000;
u8 val;
int err;
power = 1000;
if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL)) if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL))
power = 1500; power_mW = 1500;
if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL)) if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL))
power = 2000; power_mW = 2000;
if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE && if (power_mW > sfp->max_power_mW) {
(sfp->id.ext.diagmon & (SFP_DIAGMON_DDM | SFP_DIAGMON_ADDRMODE)) != /* Module power specification exceeds the allowed maximum. */
SFP_DIAGMON_DDM) { if (sfp->id.ext.sff8472_compliance ==
/* The module appears not to implement bus address 0xa2, SFP_SFF8472_COMPLIANCE_NONE &&
* or requires an address change sequence, so assume that !(sfp->id.ext.diagmon & SFP_DIAGMON_DDM)) {
* the module powers up in the indicated power mode. /* The module appears not to implement bus address
*/ * 0xa2, so assume that the module powers up in the
if (power > sfp->max_power_mW) { * indicated mode.
*/
dev_err(sfp->dev, dev_err(sfp->dev,
"Host does not support %u.%uW modules\n", "Host does not support %u.%uW modules\n",
power / 1000, (power / 100) % 10); power_mW / 1000, (power_mW / 100) % 10);
return -EINVAL; return -EINVAL;
} else {
dev_warn(sfp->dev,
"Host does not support %u.%uW modules, module left in power mode 1\n",
power_mW / 1000, (power_mW / 100) % 10);
return 0;
} }
return 0;
} }
if (power > sfp->max_power_mW) { /* If the module requires a higher power mode, but also requires
* an address change sequence, warn the user that the module may
* not be functional.
*/
if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE && power_mW > 1000) {
dev_warn(sfp->dev, dev_warn(sfp->dev,
"Host does not support %u.%uW modules, module left in power mode 1\n", "Address Change Sequence not supported but module requies %u.%uW, module may not be functional\n",
power / 1000, (power / 100) % 10); power_mW / 1000, (power_mW / 100) % 10);
return 0; return 0;
} }
if (power <= 1000) sfp->module_power_mW = power_mW;
return 0;
return 0;
}
static int sfp_sm_mod_hpower(struct sfp *sfp, bool enable)
{
u8 val;
int err;
err = sfp_read(sfp, true, SFP_EXT_STATUS, &val, sizeof(val)); err = sfp_read(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
if (err != sizeof(val)) { if (err != sizeof(val)) {
dev_err(sfp->dev, "Failed to read EEPROM: %d\n", err); dev_err(sfp->dev, "Failed to read EEPROM: %d\n", err);
err = -EAGAIN; return -EAGAIN;
goto err;
} }
val |= BIT(0); if (enable)
val |= BIT(0);
else
val &= ~BIT(0);
err = sfp_write(sfp, true, SFP_EXT_STATUS, &val, sizeof(val)); err = sfp_write(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
if (err != sizeof(val)) { if (err != sizeof(val)) {
dev_err(sfp->dev, "Failed to write EEPROM: %d\n", err); dev_err(sfp->dev, "Failed to write EEPROM: %d\n", err);
err = -EAGAIN; return -EAGAIN;
goto err;
} }
dev_info(sfp->dev, "Module switched to %u.%uW power level\n", if (enable)
power / 1000, (power / 100) % 10); dev_info(sfp->dev, "Module switched to %u.%uW power level\n",
return T_HPOWER_LEVEL; sfp->module_power_mW / 1000,
(sfp->module_power_mW / 100) % 10);
err: return 0;
return err;
} }
static int sfp_sm_mod_probe(struct sfp *sfp) static int sfp_sm_mod_probe(struct sfp *sfp, bool report)
{ {
/* SFP module inserted - read I2C data */ /* SFP module inserted - read I2C data */
struct sfp_eeprom_id id; struct sfp_eeprom_id id;
...@@ -1447,7 +1515,8 @@ static int sfp_sm_mod_probe(struct sfp *sfp) ...@@ -1447,7 +1515,8 @@ static int sfp_sm_mod_probe(struct sfp *sfp)
ret = sfp_read(sfp, false, 0, &id, sizeof(id)); ret = sfp_read(sfp, false, 0, &id, sizeof(id));
if (ret < 0) { if (ret < 0) {
dev_err(sfp->dev, "failed to read EEPROM: %d\n", ret); if (report)
dev_err(sfp->dev, "failed to read EEPROM: %d\n", ret);
return -EAGAIN; return -EAGAIN;
} }
...@@ -1517,106 +1586,174 @@ static int sfp_sm_mod_probe(struct sfp *sfp) ...@@ -1517,106 +1586,174 @@ static int sfp_sm_mod_probe(struct sfp *sfp)
dev_warn(sfp->dev, dev_warn(sfp->dev,
"module address swap to access page 0xA2 is not supported.\n"); "module address swap to access page 0xA2 is not supported.\n");
ret = sfp_hwmon_insert(sfp); /* Parse the module power requirement */
if (ret < 0) ret = sfp_module_parse_power(sfp);
return ret;
ret = sfp_module_insert(sfp->sfp_bus, &sfp->id);
if (ret < 0) if (ret < 0)
return ret; return ret;
return sfp_sm_mod_hpower(sfp); return 0;
} }
static void sfp_sm_mod_remove(struct sfp *sfp) static void sfp_sm_mod_remove(struct sfp *sfp)
{ {
sfp_module_remove(sfp->sfp_bus); if (sfp->sm_mod_state > SFP_MOD_WAITDEV)
sfp_module_remove(sfp->sfp_bus);
sfp_hwmon_remove(sfp); sfp_hwmon_remove(sfp);
if (sfp->mod_phy)
sfp_sm_phy_detach(sfp);
sfp_module_tx_disable(sfp);
memset(&sfp->id, 0, sizeof(sfp->id)); memset(&sfp->id, 0, sizeof(sfp->id));
sfp->module_power_mW = 0;
dev_info(sfp->dev, "module removed\n"); dev_info(sfp->dev, "module removed\n");
} }
static void sfp_sm_event(struct sfp *sfp, unsigned int event) /* This state machine tracks the upstream's state */
static void sfp_sm_device(struct sfp *sfp, unsigned int event)
{ {
mutex_lock(&sfp->sm_mutex); switch (sfp->sm_dev_state) {
default:
if (event == SFP_E_DEV_ATTACH)
sfp->sm_dev_state = SFP_DEV_DOWN;
break;
dev_dbg(sfp->dev, "SM: enter %s:%s:%s event %s\n", case SFP_DEV_DOWN:
mod_state_to_str(sfp->sm_mod_state), if (event == SFP_E_DEV_DETACH)
dev_state_to_str(sfp->sm_dev_state), sfp->sm_dev_state = SFP_DEV_DETACHED;
sm_state_to_str(sfp->sm_state), else if (event == SFP_E_DEV_UP)
event_to_str(event)); sfp->sm_dev_state = SFP_DEV_UP;
break;
case SFP_DEV_UP:
if (event == SFP_E_DEV_DETACH)
sfp->sm_dev_state = SFP_DEV_DETACHED;
else if (event == SFP_E_DEV_DOWN)
sfp->sm_dev_state = SFP_DEV_DOWN;
break;
}
}
/* This state machine tracks the insert/remove state of the module, probes
* the on-board EEPROM, and sets up the power level.
*/
static void sfp_sm_module(struct sfp *sfp, unsigned int event)
{
int err;
/* Handle remove event globally, it resets this state machine */
if (event == SFP_E_REMOVE) {
if (sfp->sm_mod_state > SFP_MOD_PROBE)
sfp_sm_mod_remove(sfp);
sfp_sm_mod_next(sfp, SFP_MOD_EMPTY, 0);
return;
}
/* Handle device detach globally */
if (sfp->sm_dev_state < SFP_DEV_DOWN &&
sfp->sm_mod_state > SFP_MOD_WAITDEV) {
if (sfp->module_power_mW > 1000 &&
sfp->sm_mod_state > SFP_MOD_HPOWER)
sfp_sm_mod_hpower(sfp, false);
sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0);
return;
}
/* This state machine tracks the insert/remove state of
* the module, and handles probing the on-board EEPROM.
*/
switch (sfp->sm_mod_state) { switch (sfp->sm_mod_state) {
default: default:
if (event == SFP_E_INSERT && sfp->attached) { if (event == SFP_E_INSERT) {
sfp_module_tx_disable(sfp); sfp_sm_mod_next(sfp, SFP_MOD_PROBE, T_SERIAL);
sfp_sm_ins_next(sfp, SFP_MOD_PROBE, T_PROBE_INIT); sfp->sm_mod_tries_init = R_PROBE_RETRY_INIT;
sfp->sm_mod_tries = R_PROBE_RETRY_SLOW;
} }
break; break;
case SFP_MOD_PROBE: case SFP_MOD_PROBE:
if (event == SFP_E_REMOVE) { /* Wait for T_PROBE_INIT to time out */
sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0); if (event != SFP_E_TIMEOUT)
} else if (event == SFP_E_TIMEOUT) { break;
int val = sfp_sm_mod_probe(sfp);
err = sfp_sm_mod_probe(sfp, sfp->sm_mod_tries == 1);
if (val == 0) if (err == -EAGAIN) {
sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0); if (sfp->sm_mod_tries_init &&
else if (val > 0) --sfp->sm_mod_tries_init) {
sfp_sm_ins_next(sfp, SFP_MOD_HPOWER, val); sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT);
else if (val != -EAGAIN) break;
sfp_sm_ins_next(sfp, SFP_MOD_ERROR, 0); } else if (sfp->sm_mod_tries && --sfp->sm_mod_tries) {
else if (sfp->sm_mod_tries == R_PROBE_RETRY_SLOW - 1)
sfp_sm_set_timer(sfp, T_PROBE_RETRY); dev_warn(sfp->dev,
"please wait, module slow to respond\n");
sfp_sm_set_timer(sfp, T_PROBE_RETRY_SLOW);
break;
}
}
if (err < 0) {
sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
break;
} }
break;
case SFP_MOD_HPOWER: sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0);
if (event == SFP_E_TIMEOUT) { /* fall through */
sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0); case SFP_MOD_WAITDEV:
/* Ensure that the device is attached before proceeding */
if (sfp->sm_dev_state < SFP_DEV_DOWN)
break;
/* Report the module insertion to the upstream device */
err = sfp_module_insert(sfp->sfp_bus, &sfp->id);
if (err < 0) {
sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
break; break;
} }
/* fallthrough */
case SFP_MOD_PRESENT: /* If this is a power level 1 module, we are done */
case SFP_MOD_ERROR: if (sfp->module_power_mW <= 1000)
if (event == SFP_E_REMOVE) { goto insert;
sfp_sm_mod_remove(sfp);
sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0); sfp_sm_mod_next(sfp, SFP_MOD_HPOWER, 0);
/* fall through */
case SFP_MOD_HPOWER:
/* Enable high power mode */
err = sfp_sm_mod_hpower(sfp, true);
if (err < 0) {
if (err != -EAGAIN) {
sfp_module_remove(sfp->sfp_bus);
sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0);
} else {
sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT);
}
break;
} }
sfp_sm_mod_next(sfp, SFP_MOD_WAITPWR, T_HPOWER_LEVEL);
break; break;
}
/* This state machine tracks the netdev up/down state */ case SFP_MOD_WAITPWR:
switch (sfp->sm_dev_state) { /* Wait for T_HPOWER_LEVEL to time out */
default: if (event != SFP_E_TIMEOUT)
if (event == SFP_E_DEV_UP) break;
sfp->sm_dev_state = SFP_DEV_UP;
insert:
sfp_sm_mod_next(sfp, SFP_MOD_PRESENT, 0);
break; break;
case SFP_DEV_UP: case SFP_MOD_PRESENT:
if (event == SFP_E_DEV_DOWN) { case SFP_MOD_ERROR:
/* If the module has a PHY, avoid raising TX disable
* as this resets the PHY. Otherwise, raise it to
* turn the laser off.
*/
if (!sfp->mod_phy)
sfp_module_tx_disable(sfp);
sfp->sm_dev_state = SFP_DEV_DOWN;
}
break; break;
} }
#if IS_ENABLED(CONFIG_HWMON)
if (sfp->sm_mod_state >= SFP_MOD_WAITDEV &&
IS_ERR_OR_NULL(sfp->hwmon_dev)) {
err = sfp_hwmon_insert(sfp);
if (err)
dev_warn(sfp->dev, "hwmon probe failed: %d\n", err);
}
#endif
}
static void sfp_sm_main(struct sfp *sfp, unsigned int event)
{
unsigned long timeout;
/* Some events are global */ /* Some events are global */
if (sfp->sm_state != SFP_S_DOWN && if (sfp->sm_state != SFP_S_DOWN &&
(sfp->sm_mod_state != SFP_MOD_PRESENT || (sfp->sm_mod_state != SFP_MOD_PRESENT ||
...@@ -1626,29 +1763,83 @@ static void sfp_sm_event(struct sfp *sfp, unsigned int event) ...@@ -1626,29 +1763,83 @@ static void sfp_sm_event(struct sfp *sfp, unsigned int event)
sfp_sm_link_down(sfp); sfp_sm_link_down(sfp);
if (sfp->mod_phy) if (sfp->mod_phy)
sfp_sm_phy_detach(sfp); sfp_sm_phy_detach(sfp);
sfp_module_tx_disable(sfp);
sfp_sm_next(sfp, SFP_S_DOWN, 0); sfp_sm_next(sfp, SFP_S_DOWN, 0);
mutex_unlock(&sfp->sm_mutex);
return; return;
} }
/* The main state machine */ /* The main state machine */
switch (sfp->sm_state) { switch (sfp->sm_state) {
case SFP_S_DOWN: case SFP_S_DOWN:
if (sfp->sm_mod_state == SFP_MOD_PRESENT && if (sfp->sm_mod_state != SFP_MOD_PRESENT ||
sfp->sm_dev_state == SFP_DEV_UP) sfp->sm_dev_state != SFP_DEV_UP)
sfp_sm_mod_init(sfp); break;
sfp_sm_mod_init(sfp);
/* Initialise the fault clearance retries */
sfp->sm_retries = 5;
/* We need to check the TX_FAULT state, which is not defined
* while TX_DISABLE is asserted. The earliest we want to do
* anything (such as probe for a PHY) is 50ms.
*/
sfp_sm_next(sfp, SFP_S_WAIT, T_WAIT);
break;
case SFP_S_WAIT:
if (event != SFP_E_TIMEOUT)
break;
if (sfp->state & SFP_F_TX_FAULT) {
/* Wait t_init before indicating that the link is up,
* provided the current state indicates no TX_FAULT. If
* TX_FAULT clears before this time, that's fine too.
*/
timeout = T_INIT_JIFFIES;
if (timeout > T_WAIT)
timeout -= T_WAIT;
else
timeout = 1;
sfp_sm_next(sfp, SFP_S_INIT, timeout);
} else {
/* TX_FAULT is not asserted, assume the module has
* finished initialising.
*/
goto init_done;
}
break; break;
case SFP_S_INIT: case SFP_S_INIT:
if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
sfp_sm_fault(sfp, true); /* TX_FAULT is still asserted after t_init, so assume
else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) * there is a fault.
*/
sfp_sm_fault(sfp, SFP_S_INIT_TX_FAULT,
sfp->sm_retries == 5);
} else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
init_done: /* TX_FAULT deasserted or we timed out with TX_FAULT
* clear. Probe for the PHY and check the LOS state.
*/
sfp_sm_probe_for_phy(sfp);
sfp_sm_link_check_los(sfp); sfp_sm_link_check_los(sfp);
/* Reset the fault retry count */
sfp->sm_retries = 5;
}
break;
case SFP_S_INIT_TX_FAULT:
if (event == SFP_E_TIMEOUT) {
sfp_module_tx_fault_reset(sfp);
sfp_sm_next(sfp, SFP_S_INIT, T_INIT_JIFFIES);
}
break; break;
case SFP_S_WAIT_LOS: case SFP_S_WAIT_LOS:
if (event == SFP_E_TX_FAULT) if (event == SFP_E_TX_FAULT)
sfp_sm_fault(sfp, true); sfp_sm_fault(sfp, SFP_S_TX_FAULT, true);
else if (sfp_los_event_inactive(sfp, event)) else if (sfp_los_event_inactive(sfp, event))
sfp_sm_link_up(sfp); sfp_sm_link_up(sfp);
break; break;
...@@ -1656,7 +1847,7 @@ static void sfp_sm_event(struct sfp *sfp, unsigned int event) ...@@ -1656,7 +1847,7 @@ static void sfp_sm_event(struct sfp *sfp, unsigned int event)
case SFP_S_LINK_UP: case SFP_S_LINK_UP:
if (event == SFP_E_TX_FAULT) { if (event == SFP_E_TX_FAULT) {
sfp_sm_link_down(sfp); sfp_sm_link_down(sfp);
sfp_sm_fault(sfp, true); sfp_sm_fault(sfp, SFP_S_TX_FAULT, true);
} else if (sfp_los_event_active(sfp, event)) { } else if (sfp_los_event_active(sfp, event)) {
sfp_sm_link_down(sfp); sfp_sm_link_down(sfp);
sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0); sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
...@@ -1672,7 +1863,7 @@ static void sfp_sm_event(struct sfp *sfp, unsigned int event) ...@@ -1672,7 +1863,7 @@ static void sfp_sm_event(struct sfp *sfp, unsigned int event)
case SFP_S_REINIT: case SFP_S_REINIT:
if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) { if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
sfp_sm_fault(sfp, false); sfp_sm_fault(sfp, SFP_S_TX_FAULT, false);
} else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) { } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
dev_info(sfp->dev, "module transmit fault recovered\n"); dev_info(sfp->dev, "module transmit fault recovered\n");
sfp_sm_link_check_los(sfp); sfp_sm_link_check_los(sfp);
...@@ -1682,6 +1873,21 @@ static void sfp_sm_event(struct sfp *sfp, unsigned int event) ...@@ -1682,6 +1873,21 @@ static void sfp_sm_event(struct sfp *sfp, unsigned int event)
case SFP_S_TX_DISABLE: case SFP_S_TX_DISABLE:
break; break;
} }
}
static void sfp_sm_event(struct sfp *sfp, unsigned int event)
{
mutex_lock(&sfp->sm_mutex);
dev_dbg(sfp->dev, "SM: enter %s:%s:%s event %s\n",
mod_state_to_str(sfp->sm_mod_state),
dev_state_to_str(sfp->sm_dev_state),
sm_state_to_str(sfp->sm_state),
event_to_str(event));
sfp_sm_device(sfp, event);
sfp_sm_module(sfp, event);
sfp_sm_main(sfp, event);
dev_dbg(sfp->dev, "SM: exit %s:%s:%s\n", dev_dbg(sfp->dev, "SM: exit %s:%s:%s\n",
mod_state_to_str(sfp->sm_mod_state), mod_state_to_str(sfp->sm_mod_state),
...@@ -1693,15 +1899,12 @@ static void sfp_sm_event(struct sfp *sfp, unsigned int event) ...@@ -1693,15 +1899,12 @@ static void sfp_sm_event(struct sfp *sfp, unsigned int event)
static void sfp_attach(struct sfp *sfp) static void sfp_attach(struct sfp *sfp)
{ {
sfp->attached = true; sfp_sm_event(sfp, SFP_E_DEV_ATTACH);
if (sfp->state & SFP_F_PRESENT)
sfp_sm_event(sfp, SFP_E_INSERT);
} }
static void sfp_detach(struct sfp *sfp) static void sfp_detach(struct sfp *sfp)
{ {
sfp->attached = false; sfp_sm_event(sfp, SFP_E_DEV_DETACH);
sfp_sm_event(sfp, SFP_E_REMOVE);
} }
static void sfp_start(struct sfp *sfp) static void sfp_start(struct sfp *sfp)
...@@ -1846,6 +2049,8 @@ static struct sfp *sfp_alloc(struct device *dev) ...@@ -1846,6 +2049,8 @@ static struct sfp *sfp_alloc(struct device *dev)
INIT_DELAYED_WORK(&sfp->poll, sfp_poll); INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout); INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
sfp_hwmon_init(sfp);
return sfp; return sfp;
} }
...@@ -1853,6 +2058,8 @@ static void sfp_cleanup(void *data) ...@@ -1853,6 +2058,8 @@ static void sfp_cleanup(void *data)
{ {
struct sfp *sfp = data; struct sfp *sfp = data;
sfp_hwmon_exit(sfp);
cancel_delayed_work_sync(&sfp->poll); cancel_delayed_work_sync(&sfp->poll);
cancel_delayed_work_sync(&sfp->timeout); cancel_delayed_work_sync(&sfp->timeout);
if (sfp->i2c_mii) { if (sfp->i2c_mii) {
...@@ -1964,6 +2171,11 @@ static int sfp_probe(struct platform_device *pdev) ...@@ -1964,6 +2171,11 @@ static int sfp_probe(struct platform_device *pdev)
sfp->state |= SFP_F_RATE_SELECT; sfp->state |= SFP_F_RATE_SELECT;
sfp_set_state(sfp, sfp->state); sfp_set_state(sfp, sfp->state);
sfp_module_tx_disable(sfp); sfp_module_tx_disable(sfp);
if (sfp->state & SFP_F_PRESENT) {
rtnl_lock();
sfp_sm_event(sfp, SFP_E_INSERT);
rtnl_unlock();
}
for (i = 0; i < GPIO_MAX; i++) { for (i = 0; i < GPIO_MAX; i++) {
if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i]) if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment