net: phy: ensure Gigabit features are masked off if requested
[firefly-linux-kernel-4.4.55.git] / drivers / net / phy / phy_device.c
index 5f9ee61a4ab58a8fde3eb6941f7bb6ea48bca07a..82514e72b3d8b47538cc8c75a8e52f361d1361c8 100644 (file)
@@ -30,6 +30,7 @@
 #include <linux/mii.h>
 #include <linux/ethtool.h>
 #include <linux/phy.h>
+#include <linux/mdio.h>
 #include <linux/io.h>
 #include <linux/uaccess.h>
 
@@ -50,14 +51,17 @@ static void phy_device_release(struct device *dev)
        kfree(to_phy_device(dev));
 }
 
-static struct phy_driver genphy_driver;
+enum genphy_driver {
+       GENPHY_DRV_1G,
+       GENPHY_DRV_10G,
+       GENPHY_DRV_MAX
+};
+
+static struct phy_driver genphy_driver[GENPHY_DRV_MAX];
 
 static LIST_HEAD(phy_fixup_list);
 static DEFINE_MUTEX(phy_fixup_lock);
 
-static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
-                            u32 flags, phy_interface_t interface);
-
 /**
  * phy_register_fixup - creates a new phy_fixup and adds it to the list
  * @bus_id: A string which matches phydev->dev.bus_id (or PHY_ANY_ID)
@@ -70,9 +74,8 @@ static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
                       int (*run)(struct phy_device *))
 {
-       struct phy_fixup *fixup;
+       struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
 
-       fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
        if (!fixup)
                return -ENOMEM;
 
@@ -123,16 +126,14 @@ static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
 }
 
 /* Runs any matching fixups for this phydev */
-int phy_scan_fixups(struct phy_device *phydev)
+static int phy_scan_fixups(struct phy_device *phydev)
 {
        struct phy_fixup *fixup;
 
        mutex_lock(&phy_fixup_lock);
        list_for_each_entry(fixup, &phy_fixup_list, list) {
                if (phy_needs_fixup(phydev, fixup)) {
-                       int err;
-
-                       err = fixup->run(phydev);
+                       int err = fixup->run(phydev);
 
                        if (err < 0) {
                                mutex_unlock(&phy_fixup_lock);
@@ -144,7 +145,6 @@ int phy_scan_fixups(struct phy_device *phydev)
 
        return 0;
 }
-EXPORT_SYMBOL(phy_scan_fixups);
 
 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
                                     bool is_c45,
@@ -154,7 +154,6 @@ struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
 
        /* We allocate the device, and initialize the default values */
        dev = kzalloc(sizeof(*dev), GFP_KERNEL);
-
        if (NULL == dev)
                return (struct phy_device *)PTR_ERR((void *)-ENOMEM);
 
@@ -297,7 +296,6 @@ static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
 
        /* Grab the bits from PHYIR1, and put them in the upper half */
        phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
-
        if (phy_reg < 0)
                return -EIO;
 
@@ -305,7 +303,6 @@ static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
 
        /* Grab the bits from PHYIR2, and put them in the lower half */
        phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
-
        if (phy_reg < 0)
                return -EIO;
 
@@ -327,7 +324,6 @@ static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
 {
        struct phy_c45_device_ids c45_ids = {0};
-       struct phy_device *dev = NULL;
        u32 phy_id = 0;
        int r;
 
@@ -339,9 +335,7 @@ struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
        if ((phy_id & 0x1fffffff) == 0x1fffffff)
                return NULL;
 
-       dev = phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
-
-       return dev;
+       return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
 }
 EXPORT_SYMBOL(get_phy_device);
 
@@ -431,7 +425,7 @@ int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
                return rc;
 
        phy_prepare_link(phydev, handler);
-       phy_start_machine(phydev, NULL);
+       phy_start_machine(phydev);
        if (phydev->irq > 0)
                phy_start_interrupts(phydev);
 
@@ -570,13 +564,13 @@ EXPORT_SYMBOL(phy_init_hw);
  *
  * Description: Called by drivers to attach to a particular PHY
  *     device. The phy_device is found, and properly hooked up
- *     to the phy_driver.  If no driver is attached, then the
- *     genphy_driver is used.  The phy_device is given a ptr to
+ *     to the phy_driver.  If no driver is attached, then a
+ *     generic driver is used.  The phy_device is given a ptr to
  *     the attaching device, and given a callback for link status
  *     change.  The phy_device is returned to the attaching driver.
  */
-static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
-                            u32 flags, phy_interface_t interface)
+int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
+                     u32 flags, phy_interface_t interface)
 {
        struct device *d = &phydev->dev;
        int err;
@@ -585,12 +579,10 @@ static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
         * exist, and we should use the genphy driver.
         */
        if (NULL == d->driver) {
-               if (phydev->is_c45) {
-                       pr_err("No driver for phy %x\n", phydev->phy_id);
-                       return -ENODEV;
-               }
-
-               d->driver = &genphy_driver.driver;
+               if (phydev->is_c45)
+                       d->driver = &genphy_driver[GENPHY_DRV_10G].driver;
+               else
+                       d->driver = &genphy_driver[GENPHY_DRV_1G].driver;
 
                err = d->driver->probe(d);
                if (err >= 0)
@@ -626,6 +618,7 @@ static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
 
        return err;
 }
+EXPORT_SYMBOL(phy_attach_direct);
 
 /**
  * phy_attach - attach a network device to a particular PHY device
@@ -668,6 +661,7 @@ EXPORT_SYMBOL(phy_attach);
  */
 void phy_detach(struct phy_device *phydev)
 {
+       int i;
        phydev->attached_dev->phydev = NULL;
        phydev->attached_dev = NULL;
        phy_suspend(phydev);
@@ -677,8 +671,12 @@ void phy_detach(struct phy_device *phydev)
         * from the generic driver so that there's a chance a
         * real driver could be loaded
         */
-       if (phydev->dev.driver == &genphy_driver.driver)
-               device_release_driver(&phydev->dev);
+       for (i = 0; i < ARRAY_SIZE(genphy_driver); i++) {
+               if (phydev->dev.driver == &genphy_driver[i].driver) {
+                       device_release_driver(&phydev->dev);
+                       break;
+               }
+       }
 }
 EXPORT_SYMBOL(phy_detach);
 
@@ -721,7 +719,7 @@ int phy_resume(struct phy_device *phydev)
 static int genphy_config_advert(struct phy_device *phydev)
 {
        u32 advertise;
-       int oldadv, adv;
+       int oldadv, adv, bmsr;
        int err, changed = 0;
 
        /* Only allow advertising what this PHY supports */
@@ -730,7 +728,6 @@ static int genphy_config_advert(struct phy_device *phydev)
 
        /* Setup standard advertisement */
        adv = phy_read(phydev, MII_ADVERTISE);
-
        if (adv < 0)
                return adv;
 
@@ -747,27 +744,36 @@ static int genphy_config_advert(struct phy_device *phydev)
                changed = 1;
        }
 
-       /* Configure gigabit if it's supported */
-       if (phydev->supported & (SUPPORTED_1000baseT_Half |
-                                SUPPORTED_1000baseT_Full)) {
-               adv = phy_read(phydev, MII_CTRL1000);
+       bmsr = phy_read(phydev, MII_BMSR);
+       if (bmsr < 0)
+               return bmsr;
 
-               if (adv < 0)
-                       return adv;
+       /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
+        * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
+        * logical 1.
+        */
+       if (!(bmsr & BMSR_ESTATEN))
+               return changed;
 
-               oldadv = adv;
-               adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
-               adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
+       /* Configure gigabit if it's supported */
+       adv = phy_read(phydev, MII_CTRL1000);
+       if (adv < 0)
+               return adv;
 
-               if (adv != oldadv) {
-                       err = phy_write(phydev, MII_CTRL1000, adv);
+       oldadv = adv;
+       adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
 
-                       if (err < 0)
-                               return err;
+       if (phydev->supported & (SUPPORTED_1000baseT_Half |
+                                SUPPORTED_1000baseT_Full)) {
+               adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
+               if (adv != oldadv)
                        changed = 1;
-               }
        }
 
+       err = phy_write(phydev, MII_CTRL1000, adv);
+       if (err < 0)
+               return err;
+
        return changed;
 }
 
@@ -781,7 +787,6 @@ static int genphy_config_advert(struct phy_device *phydev)
  */
 int genphy_setup_forced(struct phy_device *phydev)
 {
-       int err;
        int ctl = 0;
 
        phydev->pause = 0;
@@ -795,9 +800,7 @@ int genphy_setup_forced(struct phy_device *phydev)
        if (DUPLEX_FULL == phydev->duplex)
                ctl |= BMCR_FULLDPLX;
 
-       err = phy_write(phydev, MII_BMCR, ctl);
-
-       return err;
+       return phy_write(phydev, MII_BMCR, ctl);
 }
 EXPORT_SYMBOL(genphy_setup_forced);
 
@@ -807,9 +810,7 @@ EXPORT_SYMBOL(genphy_setup_forced);
  */
 int genphy_restart_aneg(struct phy_device *phydev)
 {
-       int ctl;
-
-       ctl = phy_read(phydev, MII_BMCR);
+       int ctl = phy_read(phydev, MII_BMCR);
 
        if (ctl < 0)
                return ctl;
@@ -819,13 +820,10 @@ int genphy_restart_aneg(struct phy_device *phydev)
        /* Don't isolate the PHY if we're negotiating */
        ctl &= ~BMCR_ISOLATE;
 
-       ctl = phy_write(phydev, MII_BMCR, ctl);
-
-       return ctl;
+       return phy_write(phydev, MII_BMCR, ctl);
 }
 EXPORT_SYMBOL(genphy_restart_aneg);
 
-
 /**
  * genphy_config_aneg - restart auto-negotiation or write BMCR
  * @phydev: target phy_device struct
@@ -842,10 +840,8 @@ int genphy_config_aneg(struct phy_device *phydev)
                return genphy_setup_forced(phydev);
 
        result = genphy_config_advert(phydev);
-
        if (result < 0) /* error */
                return result;
-
        if (result == 0) {
                /* Advertisement hasn't changed, but maybe aneg was never on to
                 * begin with?  Or maybe phy was isolated?
@@ -869,6 +865,11 @@ int genphy_config_aneg(struct phy_device *phydev)
 }
 EXPORT_SYMBOL(genphy_config_aneg);
 
+static int gen10g_config_aneg(struct phy_device *phydev)
+{
+       return 0;
+}
+
 /**
  * genphy_update_link - update link status in @phydev
  * @phydev: target phy_device struct
@@ -883,13 +884,11 @@ int genphy_update_link(struct phy_device *phydev)
 
        /* Do a fake read */
        status = phy_read(phydev, MII_BMSR);
-
        if (status < 0)
                return status;
 
        /* Read link and autonegotiation status */
        status = phy_read(phydev, MII_BMSR);
-
        if (status < 0)
                return status;
 
@@ -929,12 +928,10 @@ int genphy_read_status(struct phy_device *phydev)
                if (phydev->supported & (SUPPORTED_1000baseT_Half
                                        | SUPPORTED_1000baseT_Full)) {
                        lpagb = phy_read(phydev, MII_STAT1000);
-
                        if (lpagb < 0)
                                return lpagb;
 
                        adv = phy_read(phydev, MII_CTRL1000);
-
                        if (adv < 0)
                                return adv;
 
@@ -944,14 +941,12 @@ int genphy_read_status(struct phy_device *phydev)
                }
 
                lpa = phy_read(phydev, MII_LPA);
-
                if (lpa < 0)
                        return lpa;
 
                phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa);
 
                adv = phy_read(phydev, MII_ADVERTISE);
-
                if (adv < 0)
                        return adv;
 
@@ -1006,6 +1001,33 @@ int genphy_read_status(struct phy_device *phydev)
 }
 EXPORT_SYMBOL(genphy_read_status);
 
+static int gen10g_read_status(struct phy_device *phydev)
+{
+       int devad, reg;
+       u32 mmd_mask = phydev->c45_ids.devices_in_package;
+
+       phydev->link = 1;
+
+       /* For now just lie and say it's 10G all the time */
+       phydev->speed = SPEED_10000;
+       phydev->duplex = DUPLEX_FULL;
+
+       for (devad = 0; mmd_mask; devad++, mmd_mask = mmd_mask >> 1) {
+               if (!(mmd_mask & 1))
+                       continue;
+
+               /* Read twice because link state is latched and a
+                * read moves the current state into the register
+                */
+               phy_read_mmd(phydev, devad, MDIO_STAT1);
+               reg = phy_read_mmd(phydev, devad, MDIO_STAT1);
+               if (reg < 0 || !(reg & MDIO_STAT1_LSTATUS))
+                       phydev->link = 0;
+       }
+
+       return 0;
+}
+
 static int genphy_config_init(struct phy_device *phydev)
 {
        int val;
@@ -1020,7 +1042,6 @@ static int genphy_config_init(struct phy_device *phydev)
 
        /* Do we support autonegotiation? */
        val = phy_read(phydev, MII_BMSR);
-
        if (val < 0)
                return val;
 
@@ -1038,7 +1059,6 @@ static int genphy_config_init(struct phy_device *phydev)
 
        if (val & BMSR_ESTATEN) {
                val = phy_read(phydev, MII_ESTATUS);
-
                if (val < 0)
                        return val;
 
@@ -1053,6 +1073,16 @@ static int genphy_config_init(struct phy_device *phydev)
 
        return 0;
 }
+
+static int gen10g_config_init(struct phy_device *phydev)
+{
+       /* Temporarily just say we support everything */
+       phydev->supported = SUPPORTED_10000baseT_Full;
+       phydev->advertising = SUPPORTED_10000baseT_Full;
+
+       return 0;
+}
+
 int genphy_suspend(struct phy_device *phydev)
 {
        int value;
@@ -1068,6 +1098,11 @@ int genphy_suspend(struct phy_device *phydev)
 }
 EXPORT_SYMBOL(genphy_suspend);
 
+static int gen10g_suspend(struct phy_device *phydev)
+{
+       return 0;
+}
+
 int genphy_resume(struct phy_device *phydev)
 {
        int value;
@@ -1083,6 +1118,11 @@ int genphy_resume(struct phy_device *phydev)
 }
 EXPORT_SYMBOL(genphy_resume);
 
+static int gen10g_resume(struct phy_device *phydev)
+{
+       return 0;
+}
+
 /**
  * phy_probe - probe and init a PHY device
  * @dev: device to probe and init
@@ -1093,15 +1133,11 @@ EXPORT_SYMBOL(genphy_resume);
  */
 static int phy_probe(struct device *dev)
 {
-       struct phy_device *phydev;
-       struct phy_driver *phydrv;
-       struct device_driver *drv;
+       struct phy_device *phydev = to_phy_device(dev);
+       struct device_driver *drv = phydev->dev.driver;
+       struct phy_driver *phydrv = to_phy_driver(drv);
        int err = 0;
 
-       phydev = to_phy_device(dev);
-
-       drv = phydev->dev.driver;
-       phydrv = to_phy_driver(drv);
        phydev->drv = phydrv;
 
        /* Disable the interrupt if the PHY doesn't support it
@@ -1136,9 +1172,7 @@ static int phy_probe(struct device *dev)
 
 static int phy_remove(struct device *dev)
 {
-       struct phy_device *phydev;
-
-       phydev = to_phy_device(dev);
+       struct phy_device *phydev = to_phy_device(dev);
 
        mutex_lock(&phydev->lock);
        phydev->state = PHY_DOWN;
@@ -1165,7 +1199,6 @@ int phy_driver_register(struct phy_driver *new_driver)
        new_driver->driver.remove = phy_remove;
 
        retval = driver_register(&new_driver->driver);
-
        if (retval) {
                pr_err("%s: Error %d in registering driver\n",
                       new_driver->name, retval);
@@ -1210,7 +1243,8 @@ void phy_drivers_unregister(struct phy_driver *drv, int n)
 }
 EXPORT_SYMBOL(phy_drivers_unregister);
 
-static struct phy_driver genphy_driver = {
+static struct phy_driver genphy_driver[] = {
+{
        .phy_id         = 0xffffffff,
        .phy_id_mask    = 0xffffffff,
        .name           = "Generic PHY",
@@ -1221,7 +1255,18 @@ static struct phy_driver genphy_driver = {
        .suspend        = genphy_suspend,
        .resume         = genphy_resume,
        .driver         = { .owner = THIS_MODULE, },
-};
+}, {
+       .phy_id         = 0xffffffff,
+       .phy_id_mask    = 0xffffffff,
+       .name           = "Generic 10G PHY",
+       .config_init    = gen10g_config_init,
+       .features       = 0,
+       .config_aneg    = gen10g_config_aneg,
+       .read_status    = gen10g_read_status,
+       .suspend        = gen10g_suspend,
+       .resume         = gen10g_resume,
+       .driver         = {.owner = THIS_MODULE, },
+} };
 
 static int __init phy_init(void)
 {
@@ -1231,7 +1276,8 @@ static int __init phy_init(void)
        if (rc)
                return rc;
 
-       rc = phy_driver_register(&genphy_driver);
+       rc = phy_drivers_register(genphy_driver,
+                                 ARRAY_SIZE(genphy_driver));
        if (rc)
                mdio_bus_exit();
 
@@ -1240,7 +1286,8 @@ static int __init phy_init(void)
 
 static void __exit phy_exit(void)
 {
-       phy_driver_unregister(&genphy_driver);
+       phy_drivers_unregister(genphy_driver,
+                              ARRAY_SIZE(genphy_driver));
        mdio_bus_exit();
 }