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 {
SFP_E_INSERT = 0,
SFP_E_REMOVE,
SFP_E_DEV_ATTACH,
SFP_E_DEV_DETACH,
SFP_E_DEV_DOWN,
SFP_E_DEV_UP,
SFP_E_TX_FAULT,
......@@ -45,16 +47,21 @@ enum {
SFP_E_TIMEOUT,
SFP_MOD_EMPTY = 0,
SFP_MOD_ERROR,
SFP_MOD_PROBE,
SFP_MOD_WAITDEV,
SFP_MOD_HPOWER,
SFP_MOD_WAITPWR,
SFP_MOD_PRESENT,
SFP_MOD_ERROR,
SFP_DEV_DOWN = 0,
SFP_DEV_DETACHED = 0,
SFP_DEV_DOWN,
SFP_DEV_UP,
SFP_S_DOWN = 0,
SFP_S_WAIT,
SFP_S_INIT,
SFP_S_INIT_TX_FAULT,
SFP_S_WAIT_LOS,
SFP_S_LINK_UP,
SFP_S_TX_FAULT,
......@@ -64,10 +71,12 @@ enum {
static const char * const mod_state_strings[] = {
[SFP_MOD_EMPTY] = "empty",
[SFP_MOD_ERROR] = "error",
[SFP_MOD_PROBE] = "probe",
[SFP_MOD_WAITDEV] = "waitdev",
[SFP_MOD_HPOWER] = "hpower",
[SFP_MOD_WAITPWR] = "waitpwr",
[SFP_MOD_PRESENT] = "present",
[SFP_MOD_ERROR] = "error",
};
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[] = {
[SFP_DEV_DETACHED] = "detached",
[SFP_DEV_DOWN] = "down",
[SFP_DEV_UP] = "up",
};
......@@ -92,6 +102,8 @@ static const char *dev_state_to_str(unsigned short dev_state)
static const char * const event_strings[] = {
[SFP_E_INSERT] = "insert",
[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_UP] = "dev_up",
[SFP_E_TX_FAULT] = "tx_fault",
......@@ -110,7 +122,9 @@ static const char *event_to_str(unsigned short event)
static const char * const sm_state_strings[] = {
[SFP_S_DOWN] = "down",
[SFP_S_WAIT] = "wait",
[SFP_S_INIT] = "init",
[SFP_S_INIT_TX_FAULT] = "init_tx_fault",
[SFP_S_WAIT_LOS] = "wait_los",
[SFP_S_LINK_UP] = "link_up",
[SFP_S_TX_FAULT] = "tx_fault",
......@@ -141,6 +155,7 @@ static const enum gpiod_flags gpio_flags[] = {
GPIOD_ASIS,
};
#define T_WAIT msecs_to_jiffies(50)
#define T_INIT_JIFFIES msecs_to_jiffies(300)
#define T_RESET_US 10
#define T_FAULT_RECOVER msecs_to_jiffies(1000)
......@@ -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
* 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
* be deasserted) but makes no mention of the earliest time before we can
* access the I2C EEPROM. However, Avago modules require 300ms.
* The SFF-8472 specifies t_serial ("Time from power on until module is
* ready for data transmission over the two wire serial bus.") as 300ms.
*/
#define T_PROBE_INIT msecs_to_jiffies(300)
#define T_HPOWER_LEVEL msecs_to_jiffies(300)
#define T_PROBE_RETRY msecs_to_jiffies(100)
#define T_SERIAL msecs_to_jiffies(300)
#define T_HPOWER_LEVEL msecs_to_jiffies(300)
#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
* 0x56 (which with mdio-i2c, translates to a PHY address of 22).
*/
#define SFP_PHY_ADDR 22
/* Give this long for the PHY to reset. */
#define T_PHY_RESET_MS 50
struct sff_data {
unsigned int gpios;
bool (*module_supported)(const struct sfp_eeprom_id *id);
......@@ -187,20 +201,25 @@ struct sfp {
struct gpio_desc *gpio[GPIO_MAX];
int gpio_irq[GPIO_MAX];
bool attached;
struct mutex st_mutex; /* Protects state */
unsigned int state;
struct delayed_work poll;
struct delayed_work timeout;
struct mutex sm_mutex; /* Protects state machine */
unsigned char sm_mod_state;
unsigned char sm_mod_tries_init;
unsigned char sm_mod_tries;
unsigned char sm_dev_state;
unsigned short sm_state;
unsigned int sm_retries;
struct sfp_eeprom_id id;
unsigned int module_power_mW;
#if IS_ENABLED(CONFIG_HWMON)
struct sfp_diag diag;
struct delayed_work hwmon_probe;
unsigned int hwmon_tries;
struct device *hwmon_dev;
char *hwmon_name;
#endif
......@@ -1142,29 +1161,27 @@ static const struct hwmon_chip_info sfp_hwmon_chip_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;
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));
if (err < 0)
return err;
if (err < 0) {
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);
if (!sfp->hwmon_name)
return -ENODEV;
if (!sfp->hwmon_name) {
dev_err(sfp->dev, "out of memory for hwmon name\n");
return;
}
for (i = 0; sfp->hwmon_name[i]; i++)
if (hwmon_is_bad_char(sfp->hwmon_name[i]))
......@@ -1174,18 +1191,52 @@ static int sfp_hwmon_insert(struct sfp *sfp)
sfp->hwmon_name, sfp,
&sfp_hwmon_chip_info,
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)
{
cancel_delayed_work_sync(&sfp->hwmon_probe);
if (!IS_ERR_OR_NULL(sfp->hwmon_dev)) {
hwmon_device_unregister(sfp->hwmon_dev);
sfp->hwmon_dev = NULL;
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
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 int sfp_hwmon_init(struct sfp *sfp)
{
return 0;
}
static void sfp_hwmon_exit(struct sfp *sfp)
{
}
#endif
/* Helpers */
......@@ -1245,7 +1305,7 @@ static void sfp_sm_next(struct sfp *sfp, unsigned int state,
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)
{
sfp->sm_mod_state = state;
......@@ -1266,8 +1326,6 @@ static void sfp_sm_probe_phy(struct sfp *sfp)
struct phy_device *phy;
int err;
msleep(T_PHY_RESET_MS);
phy = mdiobus_scan(sfp->i2c_mii, SFP_PHY_ADDR);
if (phy == ERR_PTR(-ENODEV)) {
dev_info(sfp->dev, "no PHY detected\n");
......@@ -1335,7 +1393,7 @@ static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event)
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) {
dev_err(sfp->dev,
......@@ -1345,21 +1403,17 @@ static void sfp_sm_fault(struct sfp *sfp, bool warn)
if (warn)
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)
{
sfp_module_tx_enable(sfp);
}
/* 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.
*/
sfp_sm_next(sfp, SFP_S_INIT, T_INIT_JIFFIES);
sfp->sm_retries = 5;
static void sfp_sm_probe_for_phy(struct sfp *sfp)
{
/* Setting the serdes link mode is guesswork: there's no
* field in the EEPROM which indicates what mode should
* be used.
......@@ -1375,69 +1429,83 @@ static void sfp_sm_mod_init(struct sfp *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;
u8 val;
int err;
u32 power_mW = 1000;
power = 1000;
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))
power = 2000;
if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE &&
(sfp->id.ext.diagmon & (SFP_DIAGMON_DDM | SFP_DIAGMON_ADDRMODE)) !=
SFP_DIAGMON_DDM) {
/* The module appears not to implement bus address 0xa2,
* or requires an address change sequence, so assume that
* the module powers up in the indicated power mode.
*/
if (power > sfp->max_power_mW) {
power_mW = 2000;
if (power_mW > sfp->max_power_mW) {
/* Module power specification exceeds the allowed maximum. */
if (sfp->id.ext.sff8472_compliance ==
SFP_SFF8472_COMPLIANCE_NONE &&
!(sfp->id.ext.diagmon & SFP_DIAGMON_DDM)) {
/* The module appears not to implement bus address
* 0xa2, so assume that the module powers up in the
* indicated mode.
*/
dev_err(sfp->dev,
"Host does not support %u.%uW modules\n",
power / 1000, (power / 100) % 10);
power_mW / 1000, (power_mW / 100) % 10);
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,
"Host does not support %u.%uW modules, module left in power mode 1\n",
power / 1000, (power / 100) % 10);
"Address Change Sequence not supported but module requies %u.%uW, module may not be functional\n",
power_mW / 1000, (power_mW / 100) % 10);
return 0;
}
if (power <= 1000)
return 0;
sfp->module_power_mW = power_mW;
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));
if (err != sizeof(val)) {
dev_err(sfp->dev, "Failed to read EEPROM: %d\n", err);
err = -EAGAIN;
goto err;
return -EAGAIN;
}
val |= BIT(0);
if (enable)
val |= BIT(0);
else
val &= ~BIT(0);
err = sfp_write(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
if (err != sizeof(val)) {
dev_err(sfp->dev, "Failed to write EEPROM: %d\n", err);
err = -EAGAIN;
goto err;
return -EAGAIN;
}
dev_info(sfp->dev, "Module switched to %u.%uW power level\n",
power / 1000, (power / 100) % 10);
return T_HPOWER_LEVEL;
if (enable)
dev_info(sfp->dev, "Module switched to %u.%uW power level\n",
sfp->module_power_mW / 1000,
(sfp->module_power_mW / 100) % 10);
err:
return err;
return 0;
}
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 */
struct sfp_eeprom_id id;
......@@ -1447,7 +1515,8 @@ static int sfp_sm_mod_probe(struct sfp *sfp)
ret = sfp_read(sfp, false, 0, &id, sizeof(id));
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;
}
......@@ -1517,106 +1586,174 @@ static int sfp_sm_mod_probe(struct sfp *sfp)
dev_warn(sfp->dev,
"module address swap to access page 0xA2 is not supported.\n");
ret = sfp_hwmon_insert(sfp);
if (ret < 0)
return ret;
ret = sfp_module_insert(sfp->sfp_bus, &sfp->id);
/* Parse the module power requirement */
ret = sfp_module_parse_power(sfp);
if (ret < 0)
return ret;
return sfp_sm_mod_hpower(sfp);
return 0;
}
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);
if (sfp->mod_phy)
sfp_sm_phy_detach(sfp);
sfp_module_tx_disable(sfp);
memset(&sfp->id, 0, sizeof(sfp->id));
sfp->module_power_mW = 0;
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",
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));
case SFP_DEV_DOWN:
if (event == SFP_E_DEV_DETACH)
sfp->sm_dev_state = SFP_DEV_DETACHED;
else if (event == SFP_E_DEV_UP)
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) {
default:
if (event == SFP_E_INSERT && sfp->attached) {
sfp_module_tx_disable(sfp);
sfp_sm_ins_next(sfp, SFP_MOD_PROBE, T_PROBE_INIT);
if (event == SFP_E_INSERT) {
sfp_sm_mod_next(sfp, SFP_MOD_PROBE, T_SERIAL);
sfp->sm_mod_tries_init = R_PROBE_RETRY_INIT;
sfp->sm_mod_tries = R_PROBE_RETRY_SLOW;
}
break;
case SFP_MOD_PROBE:
if (event == SFP_E_REMOVE) {
sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
} else if (event == SFP_E_TIMEOUT) {
int val = sfp_sm_mod_probe(sfp);
if (val == 0)
sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
else if (val > 0)
sfp_sm_ins_next(sfp, SFP_MOD_HPOWER, val);
else if (val != -EAGAIN)
sfp_sm_ins_next(sfp, SFP_MOD_ERROR, 0);
else
sfp_sm_set_timer(sfp, T_PROBE_RETRY);
/* Wait for T_PROBE_INIT to time out */
if (event != SFP_E_TIMEOUT)
break;
err = sfp_sm_mod_probe(sfp, sfp->sm_mod_tries == 1);
if (err == -EAGAIN) {
if (sfp->sm_mod_tries_init &&
--sfp->sm_mod_tries_init) {
sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT);
break;
} else if (sfp->sm_mod_tries && --sfp->sm_mod_tries) {
if (sfp->sm_mod_tries == R_PROBE_RETRY_SLOW - 1)
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:
if (event == SFP_E_TIMEOUT) {
sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0);
/* fall through */
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;
}
/* fallthrough */
case SFP_MOD_PRESENT:
case SFP_MOD_ERROR:
if (event == SFP_E_REMOVE) {
sfp_sm_mod_remove(sfp);
sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
/* If this is a power level 1 module, we are done */
if (sfp->module_power_mW <= 1000)
goto insert;
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;
}
/* This state machine tracks the netdev up/down state */
switch (sfp->sm_dev_state) {
default:
if (event == SFP_E_DEV_UP)
sfp->sm_dev_state = SFP_DEV_UP;
case SFP_MOD_WAITPWR:
/* Wait for T_HPOWER_LEVEL to time out */
if (event != SFP_E_TIMEOUT)
break;
insert:
sfp_sm_mod_next(sfp, SFP_MOD_PRESENT, 0);
break;
case SFP_DEV_UP:
if (event == SFP_E_DEV_DOWN) {
/* 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;
}
case SFP_MOD_PRESENT:
case SFP_MOD_ERROR:
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 */
if (sfp->sm_state != SFP_S_DOWN &&
(sfp->sm_mod_state != SFP_MOD_PRESENT ||
......@@ -1626,29 +1763,83 @@ static void sfp_sm_event(struct sfp *sfp, unsigned int event)
sfp_sm_link_down(sfp);
if (sfp->mod_phy)
sfp_sm_phy_detach(sfp);
sfp_module_tx_disable(sfp);
sfp_sm_next(sfp, SFP_S_DOWN, 0);
mutex_unlock(&sfp->sm_mutex);
return;
}
/* The main state machine */
switch (sfp->sm_state) {
case SFP_S_DOWN:
if (sfp->sm_mod_state == SFP_MOD_PRESENT &&
sfp->sm_dev_state == SFP_DEV_UP)
sfp_sm_mod_init(sfp);
if (sfp->sm_mod_state != SFP_MOD_PRESENT ||
sfp->sm_dev_state != SFP_DEV_UP)
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;
case SFP_S_INIT:
if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT)
sfp_sm_fault(sfp, true);
else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR)
if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
/* TX_FAULT is still asserted after t_init, so assume
* 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);
/* 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;
case SFP_S_WAIT_LOS:
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))
sfp_sm_link_up(sfp);
break;
......@@ -1656,7 +1847,7 @@ static void sfp_sm_event(struct sfp *sfp, unsigned int event)
case SFP_S_LINK_UP:
if (event == SFP_E_TX_FAULT) {
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)) {
sfp_sm_link_down(sfp);
sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
......@@ -1672,7 +1863,7 @@ static void sfp_sm_event(struct sfp *sfp, unsigned int event)
case SFP_S_REINIT:
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) {
dev_info(sfp->dev, "module transmit fault recovered\n");
sfp_sm_link_check_los(sfp);
......@@ -1682,6 +1873,21 @@ static void sfp_sm_event(struct sfp *sfp, unsigned int event)
case SFP_S_TX_DISABLE:
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",
mod_state_to_str(sfp->sm_mod_state),
......@@ -1693,15 +1899,12 @@ static void sfp_sm_event(struct sfp *sfp, unsigned int event)
static void sfp_attach(struct sfp *sfp)
{
sfp->attached = true;
if (sfp->state & SFP_F_PRESENT)
sfp_sm_event(sfp, SFP_E_INSERT);
sfp_sm_event(sfp, SFP_E_DEV_ATTACH);
}
static void sfp_detach(struct sfp *sfp)
{
sfp->attached = false;
sfp_sm_event(sfp, SFP_E_REMOVE);
sfp_sm_event(sfp, SFP_E_DEV_DETACH);
}
static void sfp_start(struct sfp *sfp)
......@@ -1846,6 +2049,8 @@ static struct sfp *sfp_alloc(struct device *dev)
INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
sfp_hwmon_init(sfp);
return sfp;
}
......@@ -1853,6 +2058,8 @@ static void sfp_cleanup(void *data)
{
struct sfp *sfp = data;
sfp_hwmon_exit(sfp);
cancel_delayed_work_sync(&sfp->poll);
cancel_delayed_work_sync(&sfp->timeout);
if (sfp->i2c_mii) {
......@@ -1964,6 +2171,11 @@ static int sfp_probe(struct platform_device *pdev)
sfp->state |= SFP_F_RATE_SELECT;
sfp_set_state(sfp, sfp->state);
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++) {
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