Skip to content

Commit

Permalink
alx: separate link speed/duplex fields
Browse files Browse the repository at this point in the history
As suggested by Ben Hutchings, use separate fields to track
current link speed and duplex setting.

Reported-by: Ben Hutchings <[email protected]>
Signed-off-by: Johannes Berg <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
jmberg authored and davem330 committed Jul 1, 2013
1 parent 4a134c3 commit a5b87cc
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 125 deletions.
31 changes: 6 additions & 25 deletions drivers/net/ethernet/atheros/alx/ethtool.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,8 @@ static int alx_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
}
}

if (hw->link_speed != SPEED_UNKNOWN) {
ethtool_cmd_speed_set(ecmd,
hw->link_speed - hw->link_speed % 10);
ecmd->duplex = hw->link_speed % 10;
} else {
ethtool_cmd_speed_set(ecmd, SPEED_UNKNOWN);
ecmd->duplex = DUPLEX_UNKNOWN;
}
ethtool_cmd_speed_set(ecmd, hw->link_speed);
ecmd->duplex = hw->duplex;

return 0;
}
Expand All @@ -110,24 +104,11 @@ static int alx_set_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
return -EINVAL;
adv_cfg = ecmd->advertising | ADVERTISED_Autoneg;
} else {
int speed = ethtool_cmd_speed(ecmd);

switch (speed + ecmd->duplex) {
case SPEED_10 + DUPLEX_HALF:
adv_cfg = ADVERTISED_10baseT_Half;
break;
case SPEED_10 + DUPLEX_FULL:
adv_cfg = ADVERTISED_10baseT_Full;
break;
case SPEED_100 + DUPLEX_HALF:
adv_cfg = ADVERTISED_100baseT_Half;
break;
case SPEED_100 + DUPLEX_FULL:
adv_cfg = ADVERTISED_100baseT_Full;
break;
default:
adv_cfg = alx_speed_to_ethadv(ethtool_cmd_speed(ecmd),
ecmd->duplex);

if (!adv_cfg || adv_cfg == ADVERTISED_1000baseT_Full)
return -EINVAL;
}
}

hw->adv_cfg = adv_cfg;
Expand Down
139 changes: 60 additions & 79 deletions drivers/net/ethernet/atheros/alx/hw.c
Original file line number Diff line number Diff line change
Expand Up @@ -624,12 +624,12 @@ void alx_start_mac(struct alx_hw *hw)
alx_write_mem32(hw, ALX_TXQ0, txq | ALX_TXQ0_EN);

mac = hw->rx_ctrl;
if (hw->link_speed % 10 == DUPLEX_FULL)
if (hw->duplex == DUPLEX_FULL)
mac |= ALX_MAC_CTRL_FULLD;
else
mac &= ~ALX_MAC_CTRL_FULLD;
ALX_SET_FIELD(mac, ALX_MAC_CTRL_SPEED,
hw->link_speed >= SPEED_1000 ? ALX_MAC_CTRL_SPEED_1000 :
hw->link_speed == SPEED_1000 ? ALX_MAC_CTRL_SPEED_1000 :
ALX_MAC_CTRL_SPEED_10_100);
mac |= ALX_MAC_CTRL_TX_EN | ALX_MAC_CTRL_RX_EN;
hw->rx_ctrl = mac;
Expand Down Expand Up @@ -790,28 +790,22 @@ void alx_post_phy_link(struct alx_hw *hw)
u16 phy_val, len, agc;
u8 revid = alx_hw_revision(hw);
bool adj_th = revid == ALX_REV_B0;
int speed;

if (hw->link_speed == SPEED_UNKNOWN)
speed = SPEED_UNKNOWN;
else
speed = hw->link_speed - hw->link_speed % 10;

if (revid != ALX_REV_B0 && !alx_is_rev_a(revid))
return;

/* 1000BT/AZ, wrong cable length */
if (speed != SPEED_UNKNOWN) {
if (hw->link_speed != SPEED_UNKNOWN) {
alx_read_phy_ext(hw, ALX_MIIEXT_PCS, ALX_MIIEXT_CLDCTRL6,
&phy_val);
len = ALX_GET_FIELD(phy_val, ALX_CLDCTRL6_CAB_LEN);
alx_read_phy_dbg(hw, ALX_MIIDBG_AGC, &phy_val);
agc = ALX_GET_FIELD(phy_val, ALX_AGC_2_VGA);

if ((speed == SPEED_1000 &&
if ((hw->link_speed == SPEED_1000 &&
(len > ALX_CLDCTRL6_CAB_LEN_SHORT1G ||
(len == 0 && agc > ALX_AGC_LONG1G_LIMT))) ||
(speed == SPEED_100 &&
(hw->link_speed == SPEED_100 &&
(len > ALX_CLDCTRL6_CAB_LEN_SHORT100M ||
(len == 0 && agc > ALX_AGC_LONG100M_LIMT)))) {
alx_write_phy_dbg(hw, ALX_MIIDBG_AZ_ANADECT,
Expand All @@ -831,10 +825,10 @@ void alx_post_phy_link(struct alx_hw *hw)

/* threshold adjust */
if (adj_th && hw->lnk_patch) {
if (speed == SPEED_100) {
if (hw->link_speed == SPEED_100) {
alx_write_phy_dbg(hw, ALX_MIIDBG_MSE16DB,
ALX_MSE16DB_UP);
} else if (speed == SPEED_1000) {
} else if (hw->link_speed == SPEED_1000) {
/*
* Giga link threshold, raise the tolerance of
* noise 50%
Expand Down Expand Up @@ -869,7 +863,7 @@ void alx_post_phy_link(struct alx_hw *hw)
* 1. phy link must be established before calling this function
* 2. wol option (pattern,magic,link,etc.) is configed before call it.
*/
int alx_pre_suspend(struct alx_hw *hw, int speed)
int alx_pre_suspend(struct alx_hw *hw, int speed, u8 duplex)
{
u32 master, mac, phy, val;
int err = 0;
Expand Down Expand Up @@ -897,9 +891,9 @@ int alx_pre_suspend(struct alx_hw *hw, int speed)
mac |= ALX_MAC_CTRL_RX_EN | ALX_MAC_CTRL_BRD_EN;
if (hw->sleep_ctrl & ALX_SLEEP_CIFS)
mac |= ALX_MAC_CTRL_TX_EN;
if (speed % 10 == DUPLEX_FULL)
if (duplex == DUPLEX_FULL)
mac |= ALX_MAC_CTRL_FULLD;
if (speed >= SPEED_1000)
if (speed == SPEED_1000)
ALX_SET_FIELD(mac, ALX_MAC_CTRL_SPEED,
ALX_MAC_CTRL_SPEED_1000);
phy |= ALX_PHY_CTRL_DSPRST_OUT;
Expand Down Expand Up @@ -938,7 +932,7 @@ bool alx_phy_configured(struct alx_hw *hw)
return cfg == hw_cfg;
}

int alx_get_phy_link(struct alx_hw *hw, int *speed)
int alx_read_phy_link(struct alx_hw *hw)
{
struct pci_dev *pdev = hw->pdev;
u16 bmsr, giga;
Expand All @@ -953,7 +947,8 @@ int alx_get_phy_link(struct alx_hw *hw, int *speed)
return err;

if (!(bmsr & BMSR_LSTATUS)) {
*speed = SPEED_UNKNOWN;
hw->link_speed = SPEED_UNKNOWN;
hw->duplex = DUPLEX_UNKNOWN;
return 0;
}

Expand All @@ -967,20 +962,20 @@ int alx_get_phy_link(struct alx_hw *hw, int *speed)

switch (giga & ALX_GIGA_PSSR_SPEED) {
case ALX_GIGA_PSSR_1000MBS:
*speed = SPEED_1000;
hw->link_speed = SPEED_1000;
break;
case ALX_GIGA_PSSR_100MBS:
*speed = SPEED_100;
hw->link_speed = SPEED_100;
break;
case ALX_GIGA_PSSR_10MBS:
*speed = SPEED_10;
hw->link_speed = SPEED_10;
break;
default:
goto wrong_speed;
}

*speed += (giga & ALX_GIGA_PSSR_DPLX) ? DUPLEX_FULL : DUPLEX_HALF;
return 1;
hw->duplex = (giga & ALX_GIGA_PSSR_DPLX) ? DUPLEX_FULL : DUPLEX_HALF;
return 0;

wrong_speed:
dev_err(&pdev->dev, "invalid PHY speed/duplex: 0x%x\n", giga);
Expand Down Expand Up @@ -1126,81 +1121,67 @@ void alx_configure_basic(struct alx_hw *hw)
alx_write_mem32(hw, ALX_WRR, val);
}

static inline u32 alx_speed_to_ethadv(int speed)
{
switch (speed) {
case SPEED_1000 + DUPLEX_FULL:
return ADVERTISED_1000baseT_Full;
case SPEED_100 + DUPLEX_FULL:
return ADVERTISED_100baseT_Full;
case SPEED_100 + DUPLEX_HALF:
return ADVERTISED_100baseT_Half;
case SPEED_10 + DUPLEX_FULL:
return ADVERTISED_10baseT_Full;
case SPEED_10 + DUPLEX_HALF:
return ADVERTISED_10baseT_Half;
default:
return 0;
}
}

int alx_select_powersaving_speed(struct alx_hw *hw, int *speed)
int alx_select_powersaving_speed(struct alx_hw *hw, int *speed, u8 *duplex)
{
int i, err, spd;
int i, err;
u16 lpa;

err = alx_get_phy_link(hw, &spd);
if (err < 0)
err = alx_read_phy_link(hw);
if (err)
return err;

if (spd == SPEED_UNKNOWN)
if (hw->link_speed == SPEED_UNKNOWN) {
*speed = SPEED_UNKNOWN;
*duplex = DUPLEX_UNKNOWN;
return 0;
}

err = alx_read_phy_reg(hw, MII_LPA, &lpa);
if (err)
return err;

if (!(lpa & LPA_LPACK)) {
*speed = spd;
*speed = hw->link_speed;
return 0;
}

if (lpa & LPA_10FULL)
*speed = SPEED_10 + DUPLEX_FULL;
else if (lpa & LPA_10HALF)
*speed = SPEED_10 + DUPLEX_HALF;
else if (lpa & LPA_100FULL)
*speed = SPEED_100 + DUPLEX_FULL;
else
*speed = SPEED_100 + DUPLEX_HALF;

if (*speed != spd) {
err = alx_write_phy_reg(hw, ALX_MII_IER, 0);
if (err)
return err;
err = alx_setup_speed_duplex(hw,
alx_speed_to_ethadv(*speed) |
ADVERTISED_Autoneg,
ALX_FC_ANEG | ALX_FC_RX |
ALX_FC_TX);
if (err)
return err;
if (lpa & LPA_10FULL) {
*speed = SPEED_10;
*duplex = DUPLEX_FULL;
} else if (lpa & LPA_10HALF) {
*speed = SPEED_10;
*duplex = DUPLEX_HALF;
} else if (lpa & LPA_100FULL) {
*speed = SPEED_100;
*duplex = DUPLEX_FULL;
} else {
*speed = SPEED_100;
*duplex = DUPLEX_HALF;
}

/* wait for linkup */
for (i = 0; i < ALX_MAX_SETUP_LNK_CYCLE; i++) {
int speed2;
if (*speed == hw->link_speed && *duplex == hw->duplex)
return 0;
err = alx_write_phy_reg(hw, ALX_MII_IER, 0);
if (err)
return err;
err = alx_setup_speed_duplex(hw, alx_speed_to_ethadv(*speed, *duplex) |
ADVERTISED_Autoneg, ALX_FC_ANEG |
ALX_FC_RX | ALX_FC_TX);
if (err)
return err;

msleep(100);
/* wait for linkup */
for (i = 0; i < ALX_MAX_SETUP_LNK_CYCLE; i++) {
msleep(100);

err = alx_get_phy_link(hw, &speed2);
if (err < 0)
return err;
if (speed2 != SPEED_UNKNOWN)
break;
}
if (i == ALX_MAX_SETUP_LNK_CYCLE)
return -ETIMEDOUT;
err = alx_read_phy_link(hw);
if (err < 0)
return err;
if (hw->link_speed != SPEED_UNKNOWN)
break;
}
if (i == ALX_MAX_SETUP_LNK_CYCLE)
return -ETIMEDOUT;

return 0;
}
Expand Down
24 changes: 20 additions & 4 deletions drivers/net/ethernet/atheros/alx/hw.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,11 @@ struct alx_hw {
u32 smb_timer;
/* SPEED_* + DUPLEX_*, SPEED_UNKNOWN if link is down */
int link_speed;
u8 duplex;

/* auto-neg advertisement or force mode config */
u32 adv_cfg;
u8 flowctrl;
u32 adv_cfg;

u32 sleep_ctrl;

Expand Down Expand Up @@ -478,12 +479,12 @@ void alx_reset_pcie(struct alx_hw *hw);
void alx_enable_aspm(struct alx_hw *hw, bool l0s_en, bool l1_en);
int alx_setup_speed_duplex(struct alx_hw *hw, u32 ethadv, u8 flowctrl);
void alx_post_phy_link(struct alx_hw *hw);
int alx_pre_suspend(struct alx_hw *hw, int speed);
int alx_pre_suspend(struct alx_hw *hw, int speed, u8 duplex);
int alx_read_phy_reg(struct alx_hw *hw, u16 reg, u16 *phy_data);
int alx_write_phy_reg(struct alx_hw *hw, u16 reg, u16 phy_data);
int alx_read_phy_ext(struct alx_hw *hw, u8 dev, u16 reg, u16 *pdata);
int alx_write_phy_ext(struct alx_hw *hw, u8 dev, u16 reg, u16 data);
int alx_get_phy_link(struct alx_hw *hw, int *speed);
int alx_read_phy_link(struct alx_hw *hw);
int alx_clear_phy_intr(struct alx_hw *hw);
int alx_config_wol(struct alx_hw *hw);
void alx_cfg_mac_flowcontrol(struct alx_hw *hw, u8 fc);
Expand All @@ -493,7 +494,22 @@ void alx_set_macaddr(struct alx_hw *hw, const u8 *addr);
bool alx_phy_configured(struct alx_hw *hw);
void alx_configure_basic(struct alx_hw *hw);
void alx_disable_rss(struct alx_hw *hw);
int alx_select_powersaving_speed(struct alx_hw *hw, int *speed);
int alx_select_powersaving_speed(struct alx_hw *hw, int *speed, u8 *duplex);
bool alx_get_phy_info(struct alx_hw *hw);

static inline u32 alx_speed_to_ethadv(int speed, u8 duplex)
{
if (speed == SPEED_1000 && duplex == DUPLEX_FULL)
return ADVERTISED_1000baseT_Full;
if (speed == SPEED_100 && duplex == DUPLEX_FULL)
return ADVERTISED_100baseT_Full;
if (speed == SPEED_100 && duplex== DUPLEX_HALF)
return ADVERTISED_100baseT_Half;
if (speed == SPEED_10 && duplex == DUPLEX_FULL)
return ADVERTISED_10baseT_Full;
if (speed == SPEED_10 && duplex == DUPLEX_HALF)
return ADVERTISED_10baseT_Half;
return 0;
}

#endif
Loading

0 comments on commit a5b87cc

Please sign in to comment.