These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / net / phy / phy_device.c
index d551df6..0bfbaba 100644 (file)
@@ -156,8 +156,8 @@ 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);
+       if (!dev)
+               return ERR_PTR(-ENOMEM);
 
        dev->dev.release = phy_device_release;
 
@@ -176,9 +176,9 @@ struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
        if (c45_ids)
                dev->c45_ids = *c45_ids;
        dev->bus = bus;
-       dev->dev.parent = bus->parent;
+       dev->dev.parent = &bus->dev;
        dev->dev.bus = &mdio_bus_type;
-       dev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
+       dev->irq = bus->irq ? bus->irq[addr] : PHY_POLL;
        dev_set_name(&dev->dev, PHY_ID_FMT, bus->id, addr);
 
        dev->state = PHY_DOWN;
@@ -205,6 +205,37 @@ struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
 }
 EXPORT_SYMBOL(phy_device_create);
 
+/* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
+ * @bus: the target MII bus
+ * @addr: PHY address on the MII bus
+ * @dev_addr: MMD address in the PHY.
+ * @devices_in_package: where to store the devices in package information.
+ *
+ * Description: reads devices in package registers of a MMD at @dev_addr
+ * from PHY at @addr on @bus.
+ *
+ * Returns: 0 on success, -EIO on failure.
+ */
+static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
+                                  u32 *devices_in_package)
+{
+       int phy_reg, reg_addr;
+
+       reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2;
+       phy_reg = mdiobus_read(bus, addr, reg_addr);
+       if (phy_reg < 0)
+               return -EIO;
+       *devices_in_package = (phy_reg & 0xffff) << 16;
+
+       reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1;
+       phy_reg = mdiobus_read(bus, addr, reg_addr);
+       if (phy_reg < 0)
+               return -EIO;
+       *devices_in_package |= (phy_reg & 0xffff);
+
+       return 0;
+}
+
 /**
  * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
  * @bus: the target MII bus
@@ -223,31 +254,32 @@ static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
        int phy_reg;
        int i, reg_addr;
        const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
+       u32 *devs = &c45_ids->devices_in_package;
 
-       /* Find first non-zero Devices In package.  Device
-        * zero is reserved, so don't probe it.
+       /* Find first non-zero Devices In package. Device zero is reserved
+        * for 802.3 c45 complied PHYs, so don't probe it at first.
         */
-       for (i = 1;
-            i < num_ids && c45_ids->devices_in_package == 0;
-            i++) {
-               reg_addr = MII_ADDR_C45 | i << 16 | MDIO_DEVS2;
-               phy_reg = mdiobus_read(bus, addr, reg_addr);
-               if (phy_reg < 0)
-                       return -EIO;
-               c45_ids->devices_in_package = (phy_reg & 0xffff) << 16;
-
-               reg_addr = MII_ADDR_C45 | i << 16 | MDIO_DEVS1;
-               phy_reg = mdiobus_read(bus, addr, reg_addr);
+       for (i = 1; i < num_ids && *devs == 0; i++) {
+               phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs);
                if (phy_reg < 0)
                        return -EIO;
-               c45_ids->devices_in_package |= (phy_reg & 0xffff);
 
-               /* If mostly Fs, there is no device there,
-                * let's get out of here.
-                */
-               if ((c45_ids->devices_in_package & 0x1fffffff) == 0x1fffffff) {
-                       *phy_id = 0xffffffff;
-                       return 0;
+               if ((*devs & 0x1fffffff) == 0x1fffffff) {
+                       /*  If mostly Fs, there is no device there,
+                        *  then let's continue to probe more, as some
+                        *  10G PHYs have zero Devices In package,
+                        *  e.g. Cortina CS4315/CS4340 PHY.
+                        */
+                       phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs);
+                       if (phy_reg < 0)
+                               return -EIO;
+                       /* no device there, let's get out of here */
+                       if ((*devs & 0x1fffffff) == 0x1fffffff) {
+                               *phy_id = 0xffffffff;
+                               return 0;
+                       } else {
+                               break;
+                       }
                }
        }
 
@@ -375,6 +407,24 @@ int phy_device_register(struct phy_device *phydev)
 }
 EXPORT_SYMBOL(phy_device_register);
 
+/**
+ * phy_device_remove - Remove a previously registered phy device from the MDIO bus
+ * @phydev: phy_device structure to remove
+ *
+ * This doesn't free the phy_device itself, it merely reverses the effects
+ * of phy_device_register(). Use phy_device_free() to free the device
+ * after calling this function.
+ */
+void phy_device_remove(struct phy_device *phydev)
+{
+       struct mii_bus *bus = phydev->bus;
+       int addr = phydev->addr;
+
+       device_del(&phydev->dev);
+       bus->phy_map[addr] = NULL;
+}
+EXPORT_SYMBOL(phy_device_remove);
+
 /**
  * phy_find_first - finds the first PHY device on the bus
  * @bus: the target MII bus
@@ -570,18 +620,26 @@ EXPORT_SYMBOL(phy_init_hw);
  *     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.
+ *     This function takes a reference on the phy device.
  */
 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
                      u32 flags, phy_interface_t interface)
 {
+       struct mii_bus *bus = phydev->bus;
        struct device *d = &phydev->dev;
-       struct module *bus_module;
        int err;
 
+       if (!try_module_get(bus->owner)) {
+               dev_err(&dev->dev, "failed to get the bus module\n");
+               return -EIO;
+       }
+
+       get_device(d);
+
        /* Assume that if there is no driver, that it doesn't
         * exist, and we should use the genphy driver.
         */
-       if (NULL == d->driver) {
+       if (!d->driver) {
                if (phydev->is_c45)
                        d->driver = &genphy_driver[GENPHY_DRV_10G].driver;
                else
@@ -592,20 +650,13 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
                        err = device_bind_driver(d);
 
                if (err)
-                       return err;
+                       goto error;
        }
 
        if (phydev->attached_dev) {
                dev_err(&dev->dev, "PHY already attached\n");
-               return -EBUSY;
-       }
-
-       /* Increment the bus module reference count */
-       bus_module = phydev->bus->dev.driver ?
-                    phydev->bus->dev.driver->owner : NULL;
-       if (!try_module_get(bus_module)) {
-               dev_err(&dev->dev, "failed to get the bus module\n");
-               return -EIO;
+               err = -EBUSY;
+               goto error;
        }
 
        phydev->attached_dev = dev;
@@ -628,6 +679,11 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
                phy_resume(phydev);
 
        return err;
+
+error:
+       put_device(d);
+       module_put(bus->owner);
+       return err;
 }
 EXPORT_SYMBOL(phy_attach_direct);
 
@@ -669,14 +725,15 @@ EXPORT_SYMBOL(phy_attach);
 /**
  * phy_detach - detach a PHY device from its network device
  * @phydev: target phy_device struct
+ *
+ * This detaches the phy device from its network device and the phy
+ * driver, and drops the reference count taken in phy_attach_direct().
  */
 void phy_detach(struct phy_device *phydev)
 {
+       struct mii_bus *bus;
        int i;
 
-       if (phydev->bus->dev.driver)
-               module_put(phydev->bus->dev.driver->owner);
-
        phydev->attached_dev->phydev = NULL;
        phydev->attached_dev = NULL;
        phy_suspend(phydev);
@@ -692,6 +749,15 @@ void phy_detach(struct phy_device *phydev)
                        break;
                }
        }
+
+       /*
+        * The phydev might go away on the put_device() below, so avoid
+        * a use-after-free bug by reading the underlying bus first.
+        */
+       bus = phydev->bus;
+
+       put_device(&phydev->dev);
+       module_put(bus->owner);
 }
 EXPORT_SYMBOL(phy_detach);
 
@@ -1197,6 +1263,44 @@ static int gen10g_resume(struct phy_device *phydev)
        return 0;
 }
 
+static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
+{
+       /* The default values for phydev->supported are provided by the PHY
+        * driver "features" member, we want to reset to sane defaults first
+        * before supporting higher speeds.
+        */
+       phydev->supported &= PHY_DEFAULT_FEATURES;
+
+       switch (max_speed) {
+       default:
+               return -ENOTSUPP;
+       case SPEED_1000:
+               phydev->supported |= PHY_1000BT_FEATURES;
+               /* fall through */
+       case SPEED_100:
+               phydev->supported |= PHY_100BT_FEATURES;
+               /* fall through */
+       case SPEED_10:
+               phydev->supported |= PHY_10BT_FEATURES;
+       }
+
+       return 0;
+}
+
+int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
+{
+       int err;
+
+       err = __set_phy_supported(phydev, max_speed);
+       if (err)
+               return err;
+
+       phydev->advertising = phydev->supported;
+
+       return 0;
+}
+EXPORT_SYMBOL(phy_set_max_speed);
+
 static void of_set_phy_supported(struct phy_device *phydev)
 {
        struct device_node *node = phydev->dev.of_node;
@@ -1208,25 +1312,8 @@ static void of_set_phy_supported(struct phy_device *phydev)
        if (!node)
                return;
 
-       if (!of_property_read_u32(node, "max-speed", &max_speed)) {
-               /* The default values for phydev->supported are provided by the PHY
-                * driver "features" member, we want to reset to sane defaults fist
-                * before supporting higher speeds.
-                */
-               phydev->supported &= PHY_DEFAULT_FEATURES;
-
-               switch (max_speed) {
-               default:
-                       return;
-
-               case SPEED_1000:
-                       phydev->supported |= PHY_1000BT_FEATURES;
-               case SPEED_100:
-                       phydev->supported |= PHY_100BT_FEATURES;
-               case SPEED_10:
-                       phydev->supported |= PHY_10BT_FEATURES;
-               }
-       }
+       if (!of_property_read_u32(node, "max-speed", &max_speed))
+               __set_phy_supported(phydev, max_speed);
 }
 
 /**