User Tools

Site Tools


phy_link_update

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
phy_link_update [2018/08/28 10:43]
rpjday [Explanation]
phy_link_update [2018/08/28 11:57] (current)
rpjday [freescale/ucc_geth.c]
Line 5: Line 5:
 Issues: Issues:
  
-  * Is [[https://​elixir.bootlin.com/​linux/​latest/​ident/​phy_link_change|phy_link_change()]] relevant?+  * Is [[https://​elixir.bootlin.com/​linux/​latest/​ident/​phy_link_change|phy_link_change()]] relevant? ​Don't think so.
  
 ===== Explanation ===== ===== Explanation =====
Line 91: Line 91:
 </​code>​ </​code>​
  
-===== struct ​phydevice ​=====+===== struct ​phy_device ​=====
  
 <​code>​ <​code>​
Line 110: Line 110:
 ... ...
 struct phy_device { struct phy_device {
-        unsigned link:1;+        unsigned link:​1; ​      // most recently read link state [01]
         enum phy_state state;         enum phy_state state;
         int speed;         int speed;
Line 116: Line 116:
 }; };
 </​code>​ </​code>​
-===== adjust_link() ===== 
  
-==== How many drivers? ====+Printing: 
 + 
 +<​code>​ 
 +netdev_info(ndev,​ "Link %s, speed %d, %s\n",​ 
 +                  phy->​link ​ ? "​up"​ : "​down",​ 
 +                  phy->​speed,​ 
 +                  phy->​duplex ? "​duplex"​ : "​simplex"​);​ 
 + 
 +</​code>​ 
 + 
 +===== How many drivers ​use adjust_link()=====
  
 <​code>​ <​code>​
Line 143: Line 152:
 qualcomm/​emac/​emac-mac.c qualcomm/​emac/​emac-mac.c
 $ $
 +</​code>​
 +
 +===== Examples =====
 +
 +==== freescale/​fs_enet/​fs_enet-main.c ====
 +
 +<​code>​
 +/​*-----------------------------------------------------------------------------
 + ​* ​ generic link-change handler - should be sufficient for most cases
 + ​*-----------------------------------------------------------------------------*/​
 +static void generic_adjust_link(struct ​ net_device *dev)
 +{
 +        struct fs_enet_private *fep = netdev_priv(dev);​
 +        struct phy_device *phydev = dev->​phydev;​
 +        int new_state = 0;
 +
 +        if (phydev->​link) {
 +                /* adjust to duplex mode */
 +                if (phydev->​duplex != fep->​oldduplex) {
 +                        new_state = 1;
 +                        fep->​oldduplex = phydev->​duplex;​
 +                }
 +
 +                if (phydev->​speed != fep->​oldspeed) {
 +                        new_state = 1;
 +                        fep->​oldspeed = phydev->​speed;​
 +                }
 +
 +                if (!fep->​oldlink) {
 +                        new_state = 1;
 +                        fep->​oldlink = 1;
 +                }
 +
 +                if (new_state)
 +                        fep->​ops->​restart(dev);​
 +        } else if (fep->​oldlink) {
 +                new_state = 1;
 +                fep->​oldlink = 0;
 +                fep->​oldspeed = 0;
 +                fep->​oldduplex = -1;
 +        }
 +
 +        if (new_state && netif_msg_link(fep))
 +                phy_print_status(phydev);​
 +}
 +
 +static void fs_adjust_link(struct net_device *dev)
 +{
 +        struct fs_enet_private *fep = netdev_priv(dev);​
 +        unsigned long flags;
 +
 +        spin_lock_irqsave(&​fep->​lock,​ flags);
 +
 +        if(fep->​ops->​adjust_link)
 +                fep->​ops->​adjust_link(dev);​
 +        else
 +                generic_adjust_link(dev);​
 +
 +        spin_unlock_irqrestore(&​fep->​lock,​ flags);
 +}
 +
 +static int fs_init_phy(struct net_device *dev)
 +{
 +        struct fs_enet_private *fep = netdev_priv(dev);​
 +        struct phy_device *phydev;
 +        phy_interface_t iface;
 +
 +        fep->​oldlink = 0;
 +        fep->​oldspeed = 0;
 +        fep->​oldduplex = -1;
 +
 +        iface = fep->​fpi->​use_rmii ?
 +                PHY_INTERFACE_MODE_RMII : PHY_INTERFACE_MODE_MII;​
 +
 +        phydev = of_phy_connect(dev,​ fep->​fpi->​phy_node,​ &​fs_adjust_link,​ 0,
 +                                iface);
 +        if (!phydev) {
 +                dev_err(&​dev->​dev,​ "Could not attach to PHY\n"​);​
 +                return -ENODEV;
 +        }
 +
 +        return 0;
 +}
 +</​code>​
 +
 +==== freescale/​gianfar.c ====
 +
 +<​code>​
 +static void adjust_link(struct net_device *dev)
 +{
 +        struct gfar_private *priv = netdev_priv(dev);​
 +        struct phy_device *phydev = dev->​phydev;​
 +
 +        if (unlikely(phydev->​link != priv->​oldlink ||
 +                     ​(phydev->​link && (phydev->​duplex != priv->​oldduplex ||
 +                                       ​phydev->​speed != priv->​oldspeed))))
 +                gfar_update_link_state(priv);​
 +}
 +</​code>​
 +
 +==== freescale/​dpaa/​dpaa_eth.c ====
 +
 +<​code>​
 +static void dpaa_adjust_link(struct net_device *net_dev)
 +{
 +        struct mac_device *mac_dev;
 +        struct dpaa_priv *priv;
 +
 +        priv = netdev_priv(net_dev);​
 +        mac_dev = priv->​mac_dev;​
 +        mac_dev->​adjust_link(mac_dev);​
 +}
 +</​code>​
 +
 +==== freescale/​ucc_geth.c ====
 +
 +<​code>​
 +/* Called every time the controller might need to be made
 + * aware of new link state. ​ The PHY code conveys this
 + * information through variables in the ugeth structure, and this
 + * function converts those variables into the appropriate
 + * register values, and can bring down the device if needed.
 + */
 +
 +static void adjust_link(struct net_device *dev)
 +{
 +        struct ucc_geth_private *ugeth = netdev_priv(dev);​
 +        struct ucc_geth __iomem *ug_regs;
 +        struct ucc_fast __iomem *uf_regs;
 +        struct phy_device *phydev = ugeth->​phydev;​
 +        int new_state = 0;
 +
 +        ug_regs = ugeth->​ug_regs;​
 +        uf_regs = ugeth->​uccf->​uf_regs;​
 +
 +        if (phydev->​link) { ...
 +</​code>​
 +
 +==== hisilicon/​hns/​hns_enet.c ====
 +
 +<​code>​
 +/**
 + ​*hns_nic_adjust_link - adjust net work mode by the phy stat or new param
 + ​*@ndev:​ net device
 + */
 +static void hns_nic_adjust_link(struct net_device *ndev)
 +{
 +        struct hns_nic_priv *priv = netdev_priv(ndev);​
 +        struct hnae_handle *h = priv->​ae_handle;​
 +        int state = 1;
 +
 +        if (ndev->​phydev) {
 +                h->​dev->​ops->​adjust_link(h,​ ndev->​phydev->​speed,​
 +                                         ​ndev->​phydev->​duplex);​
 +                state = ndev->​phydev->​link;​
 +        }
 +        state = state && h->​dev->​ops->​get_status(h);​
 +
 +        if (state != priv->​link) {
 +                if (state) {
 +                        netif_carrier_on(ndev);​
 +                        netif_tx_wake_all_queues(ndev);​
 +                        netdev_info(ndev,​ "link up\n"​);​
 +                } else {
 +                        netif_carrier_off(ndev);​
 +                        netdev_info(ndev,​ "link down\n"​);​
 +                }
 +                priv->​link = state;
 +        }
 +}
 </​code>​ </​code>​
phy_link_update.1535452984.txt.gz · Last modified: 2018/08/28 10:43 by rpjday