GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / net / phy / phy_device.c
1 /* Framework for finding and configuring PHYs.
2  * Also contains generic PHY driver
3  *
4  * Author: Andy Fleming
5  *
6  * Copyright (c) 2004 Freescale Semiconductor, Inc.
7  *
8  * This program is free software; you can redistribute  it and/or modify it
9  * under  the terms of  the GNU General  Public License as published by the
10  * Free Software Foundation;  either version 2 of the  License, or (at your
11  * option) any later version.
12  *
13  */
14
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/errno.h>
20 #include <linux/unistd.h>
21 #include <linux/slab.h>
22 #include <linux/interrupt.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/skbuff.h>
28 #include <linux/mm.h>
29 #include <linux/module.h>
30 #include <linux/mii.h>
31 #include <linux/ethtool.h>
32 #include <linux/phy.h>
33 #include <linux/phy_led_triggers.h>
34 #include <linux/mdio.h>
35 #include <linux/io.h>
36 #include <linux/uaccess.h>
37 #include <linux/of.h>
38
39 #include <asm/irq.h>
40
41 MODULE_DESCRIPTION("PHY library");
42 MODULE_AUTHOR("Andy Fleming");
43 MODULE_LICENSE("GPL");
44
45 void phy_device_free(struct phy_device *phydev)
46 {
47         put_device(&phydev->mdio.dev);
48 }
49 EXPORT_SYMBOL(phy_device_free);
50
51 static void phy_mdio_device_free(struct mdio_device *mdiodev)
52 {
53         struct phy_device *phydev;
54
55         phydev = container_of(mdiodev, struct phy_device, mdio);
56         phy_device_free(phydev);
57 }
58
59 static void phy_device_release(struct device *dev)
60 {
61         kfree(to_phy_device(dev));
62 }
63
64 static void phy_mdio_device_remove(struct mdio_device *mdiodev)
65 {
66         struct phy_device *phydev;
67
68         phydev = container_of(mdiodev, struct phy_device, mdio);
69         phy_device_remove(phydev);
70 }
71
72 static struct phy_driver genphy_driver;
73 extern struct phy_driver genphy_10g_driver;
74
75 static LIST_HEAD(phy_fixup_list);
76 static DEFINE_MUTEX(phy_fixup_lock);
77
78 #ifdef CONFIG_PM
79 static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
80 {
81         struct device_driver *drv = phydev->mdio.dev.driver;
82         struct phy_driver *phydrv = to_phy_driver(drv);
83         struct net_device *netdev = phydev->attached_dev;
84
85         if (!drv || !phydrv->suspend)
86                 return false;
87
88         /* PHY not attached? May suspend if the PHY has not already been
89          * suspended as part of a prior call to phy_disconnect() ->
90          * phy_detach() -> phy_suspend() because the parent netdev might be the
91          * MDIO bus driver and clock gated at this point.
92          */
93         if (!netdev)
94                 goto out;
95
96         if (netdev->wol_enabled)
97                 return false;
98
99         /* As long as not all affected network drivers support the
100          * wol_enabled flag, let's check for hints that WoL is enabled.
101          * Don't suspend PHY if the attached netdev parent may wake up.
102          * The parent may point to a PCI device, as in tg3 driver.
103          */
104         if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
105                 return false;
106
107         /* Also don't suspend PHY if the netdev itself may wakeup. This
108          * is the case for devices w/o underlaying pwr. mgmt. aware bus,
109          * e.g. SoC devices.
110          */
111         if (device_may_wakeup(&netdev->dev))
112                 return false;
113
114 out:
115         return !phydev->suspended;
116 }
117
118 static int mdio_bus_phy_suspend(struct device *dev)
119 {
120         struct phy_device *phydev = to_phy_device(dev);
121
122         /* We must stop the state machine manually, otherwise it stops out of
123          * control, possibly with the phydev->lock held. Upon resume, netdev
124          * may call phy routines that try to grab the same lock, and that may
125          * lead to a deadlock.
126          */
127         if (phydev->attached_dev && phydev->adjust_link)
128                 phy_stop_machine(phydev);
129
130         if (!mdio_bus_phy_may_suspend(phydev))
131                 return 0;
132
133         phydev->suspended_by_mdio_bus = 1;
134
135         return phy_suspend(phydev);
136 }
137
138 static int mdio_bus_phy_resume(struct device *dev)
139 {
140         struct phy_device *phydev = to_phy_device(dev);
141         int ret;
142
143         if (!phydev->suspended_by_mdio_bus)
144                 goto no_resume;
145
146         phydev->suspended_by_mdio_bus = 0;
147
148         ret = phy_resume(phydev);
149         if (ret < 0)
150                 return ret;
151
152 no_resume:
153         if (phydev->attached_dev && phydev->adjust_link)
154                 phy_start_machine(phydev);
155
156         return 0;
157 }
158
159 static int mdio_bus_phy_restore(struct device *dev)
160 {
161         struct phy_device *phydev = to_phy_device(dev);
162         struct net_device *netdev = phydev->attached_dev;
163         int ret;
164
165         if (!netdev)
166                 return 0;
167
168         ret = phy_init_hw(phydev);
169         if (ret < 0)
170                 return ret;
171
172         if (phydev->attached_dev && phydev->adjust_link)
173                 phy_start_machine(phydev);
174
175         return 0;
176 }
177
178 static const struct dev_pm_ops mdio_bus_phy_pm_ops = {
179         .suspend = mdio_bus_phy_suspend,
180         .resume = mdio_bus_phy_resume,
181         .freeze = mdio_bus_phy_suspend,
182         .thaw = mdio_bus_phy_resume,
183         .restore = mdio_bus_phy_restore,
184 };
185
186 #define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops)
187
188 #else
189
190 #define MDIO_BUS_PHY_PM_OPS NULL
191
192 #endif /* CONFIG_PM */
193
194 /**
195  * phy_register_fixup - creates a new phy_fixup and adds it to the list
196  * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
197  * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
198  *      It can also be PHY_ANY_UID
199  * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
200  *      comparison
201  * @run: The actual code to be run when a matching PHY is found
202  */
203 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
204                        int (*run)(struct phy_device *))
205 {
206         struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
207
208         if (!fixup)
209                 return -ENOMEM;
210
211         strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
212         fixup->phy_uid = phy_uid;
213         fixup->phy_uid_mask = phy_uid_mask;
214         fixup->run = run;
215
216         mutex_lock(&phy_fixup_lock);
217         list_add_tail(&fixup->list, &phy_fixup_list);
218         mutex_unlock(&phy_fixup_lock);
219
220         return 0;
221 }
222 EXPORT_SYMBOL(phy_register_fixup);
223
224 /* Registers a fixup to be run on any PHY with the UID in phy_uid */
225 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
226                                int (*run)(struct phy_device *))
227 {
228         return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
229 }
230 EXPORT_SYMBOL(phy_register_fixup_for_uid);
231
232 /* Registers a fixup to be run on the PHY with id string bus_id */
233 int phy_register_fixup_for_id(const char *bus_id,
234                               int (*run)(struct phy_device *))
235 {
236         return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
237 }
238 EXPORT_SYMBOL(phy_register_fixup_for_id);
239
240 /**
241  * phy_unregister_fixup - remove a phy_fixup from the list
242  * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list
243  * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list
244  * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison
245  */
246 int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
247 {
248         struct list_head *pos, *n;
249         struct phy_fixup *fixup;
250         int ret;
251
252         ret = -ENODEV;
253
254         mutex_lock(&phy_fixup_lock);
255         list_for_each_safe(pos, n, &phy_fixup_list) {
256                 fixup = list_entry(pos, struct phy_fixup, list);
257
258                 if ((!strcmp(fixup->bus_id, bus_id)) &&
259                     ((fixup->phy_uid & phy_uid_mask) ==
260                      (phy_uid & phy_uid_mask))) {
261                         list_del(&fixup->list);
262                         kfree(fixup);
263                         ret = 0;
264                         break;
265                 }
266         }
267         mutex_unlock(&phy_fixup_lock);
268
269         return ret;
270 }
271 EXPORT_SYMBOL(phy_unregister_fixup);
272
273 /* Unregisters a fixup of any PHY with the UID in phy_uid */
274 int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask)
275 {
276         return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);
277 }
278 EXPORT_SYMBOL(phy_unregister_fixup_for_uid);
279
280 /* Unregisters a fixup of the PHY with id string bus_id */
281 int phy_unregister_fixup_for_id(const char *bus_id)
282 {
283         return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);
284 }
285 EXPORT_SYMBOL(phy_unregister_fixup_for_id);
286
287 /* Returns 1 if fixup matches phydev in bus_id and phy_uid.
288  * Fixups can be set to match any in one or more fields.
289  */
290 static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
291 {
292         if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
293                 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
294                         return 0;
295
296         if ((fixup->phy_uid & fixup->phy_uid_mask) !=
297             (phydev->phy_id & fixup->phy_uid_mask))
298                 if (fixup->phy_uid != PHY_ANY_UID)
299                         return 0;
300
301         return 1;
302 }
303
304 /* Runs any matching fixups for this phydev */
305 static int phy_scan_fixups(struct phy_device *phydev)
306 {
307         struct phy_fixup *fixup;
308
309         mutex_lock(&phy_fixup_lock);
310         list_for_each_entry(fixup, &phy_fixup_list, list) {
311                 if (phy_needs_fixup(phydev, fixup)) {
312                         int err = fixup->run(phydev);
313
314                         if (err < 0) {
315                                 mutex_unlock(&phy_fixup_lock);
316                                 return err;
317                         }
318                         phydev->has_fixups = true;
319                 }
320         }
321         mutex_unlock(&phy_fixup_lock);
322
323         return 0;
324 }
325
326 static int phy_bus_match(struct device *dev, struct device_driver *drv)
327 {
328         struct phy_device *phydev = to_phy_device(dev);
329         struct phy_driver *phydrv = to_phy_driver(drv);
330         const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
331         int i;
332
333         if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
334                 return 0;
335
336         if (phydrv->match_phy_device)
337                 return phydrv->match_phy_device(phydev);
338
339         if (phydev->is_c45) {
340                 for (i = 1; i < num_ids; i++) {
341                         if (!(phydev->c45_ids.devices_in_package & (1 << i)))
342                                 continue;
343
344                         if ((phydrv->phy_id & phydrv->phy_id_mask) ==
345                             (phydev->c45_ids.device_ids[i] &
346                              phydrv->phy_id_mask))
347                                 return 1;
348                 }
349                 return 0;
350         } else {
351                 return (phydrv->phy_id & phydrv->phy_id_mask) ==
352                         (phydev->phy_id & phydrv->phy_id_mask);
353         }
354 }
355
356 static ssize_t
357 phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
358 {
359         struct phy_device *phydev = to_phy_device(dev);
360
361         return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
362 }
363 static DEVICE_ATTR_RO(phy_id);
364
365 static ssize_t
366 phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
367 {
368         struct phy_device *phydev = to_phy_device(dev);
369         const char *mode = NULL;
370
371         if (phy_is_internal(phydev))
372                 mode = "internal";
373         else
374                 mode = phy_modes(phydev->interface);
375
376         return sprintf(buf, "%s\n", mode);
377 }
378 static DEVICE_ATTR_RO(phy_interface);
379
380 static ssize_t
381 phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
382                     char *buf)
383 {
384         struct phy_device *phydev = to_phy_device(dev);
385
386         return sprintf(buf, "%d\n", phydev->has_fixups);
387 }
388 static DEVICE_ATTR_RO(phy_has_fixups);
389
390 static struct attribute *phy_dev_attrs[] = {
391         &dev_attr_phy_id.attr,
392         &dev_attr_phy_interface.attr,
393         &dev_attr_phy_has_fixups.attr,
394         NULL,
395 };
396 ATTRIBUTE_GROUPS(phy_dev);
397
398 static const struct device_type mdio_bus_phy_type = {
399         .name = "PHY",
400         .groups = phy_dev_groups,
401         .release = phy_device_release,
402         .pm = MDIO_BUS_PHY_PM_OPS,
403 };
404
405 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
406                                      bool is_c45,
407                                      struct phy_c45_device_ids *c45_ids)
408 {
409         struct phy_device *dev;
410         struct mdio_device *mdiodev;
411
412         /* We allocate the device, and initialize the default values */
413         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
414         if (!dev)
415                 return ERR_PTR(-ENOMEM);
416
417         mdiodev = &dev->mdio;
418         mdiodev->dev.parent = &bus->dev;
419         mdiodev->dev.bus = &mdio_bus_type;
420         mdiodev->dev.type = &mdio_bus_phy_type;
421         mdiodev->bus = bus;
422         mdiodev->bus_match = phy_bus_match;
423         mdiodev->addr = addr;
424         mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
425         mdiodev->device_free = phy_mdio_device_free;
426         mdiodev->device_remove = phy_mdio_device_remove;
427
428         dev->speed = SPEED_UNKNOWN;
429         dev->duplex = DUPLEX_UNKNOWN;
430         dev->pause = 0;
431         dev->asym_pause = 0;
432         dev->link = 0;
433         dev->interface = PHY_INTERFACE_MODE_GMII;
434
435         dev->autoneg = AUTONEG_ENABLE;
436
437         dev->is_c45 = is_c45;
438         dev->phy_id = phy_id;
439         if (c45_ids)
440                 dev->c45_ids = *c45_ids;
441         dev->irq = bus->irq[addr];
442         dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
443
444         dev->state = PHY_DOWN;
445
446         mutex_init(&dev->lock);
447         INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
448         INIT_WORK(&dev->phy_queue, phy_change_work);
449
450         /* Request the appropriate module unconditionally; don't
451          * bother trying to do so only if it isn't already loaded,
452          * because that gets complicated. A hotplug event would have
453          * done an unconditional modprobe anyway.
454          * We don't do normal hotplug because it won't work for MDIO
455          * -- because it relies on the device staying around for long
456          * enough for the driver to get loaded. With MDIO, the NIC
457          * driver will get bored and give up as soon as it finds that
458          * there's no driver _already_ loaded.
459          */
460         request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id));
461
462         device_initialize(&mdiodev->dev);
463
464         return dev;
465 }
466 EXPORT_SYMBOL(phy_device_create);
467
468 /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
469  * @bus: the target MII bus
470  * @addr: PHY address on the MII bus
471  * @dev_addr: MMD address in the PHY.
472  * @devices_in_package: where to store the devices in package information.
473  *
474  * Description: reads devices in package registers of a MMD at @dev_addr
475  * from PHY at @addr on @bus.
476  *
477  * Returns: 0 on success, -EIO on failure.
478  */
479 static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
480                                    u32 *devices_in_package)
481 {
482         int phy_reg, reg_addr;
483
484         reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2;
485         phy_reg = mdiobus_read(bus, addr, reg_addr);
486         if (phy_reg < 0)
487                 return -EIO;
488         *devices_in_package = (phy_reg & 0xffff) << 16;
489
490         reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1;
491         phy_reg = mdiobus_read(bus, addr, reg_addr);
492         if (phy_reg < 0)
493                 return -EIO;
494         *devices_in_package |= (phy_reg & 0xffff);
495
496         return 0;
497 }
498
499 /**
500  * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
501  * @bus: the target MII bus
502  * @addr: PHY address on the MII bus
503  * @phy_id: where to store the ID retrieved.
504  * @c45_ids: where to store the c45 ID information.
505  *
506  *   If the PHY devices-in-package appears to be valid, it and the
507  *   corresponding identifiers are stored in @c45_ids, zero is stored
508  *   in @phy_id.  Otherwise 0xffffffff is stored in @phy_id.  Returns
509  *   zero on success.
510  *
511  */
512 static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
513                            struct phy_c45_device_ids *c45_ids) {
514         int phy_reg;
515         int i, reg_addr;
516         const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
517         u32 *devs = &c45_ids->devices_in_package;
518
519         /* Find first non-zero Devices In package. Device zero is reserved
520          * for 802.3 c45 complied PHYs, so don't probe it at first.
521          */
522         for (i = 1; i < num_ids && *devs == 0; i++) {
523                 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs);
524                 if (phy_reg < 0)
525                         return -EIO;
526
527                 if ((*devs & 0x1fffffff) == 0x1fffffff) {
528                         /*  If mostly Fs, there is no device there,
529                          *  then let's continue to probe more, as some
530                          *  10G PHYs have zero Devices In package,
531                          *  e.g. Cortina CS4315/CS4340 PHY.
532                          */
533                         phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs);
534                         if (phy_reg < 0)
535                                 return -EIO;
536                         /* no device there, let's get out of here */
537                         if ((*devs & 0x1fffffff) == 0x1fffffff) {
538                                 *phy_id = 0xffffffff;
539                                 return 0;
540                         } else {
541                                 break;
542                         }
543                 }
544         }
545
546         /* Now probe Device Identifiers for each device present. */
547         for (i = 1; i < num_ids; i++) {
548                 if (!(c45_ids->devices_in_package & (1 << i)))
549                         continue;
550
551                 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
552                 phy_reg = mdiobus_read(bus, addr, reg_addr);
553                 if (phy_reg < 0)
554                         return -EIO;
555                 c45_ids->device_ids[i] = (phy_reg & 0xffff) << 16;
556
557                 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
558                 phy_reg = mdiobus_read(bus, addr, reg_addr);
559                 if (phy_reg < 0)
560                         return -EIO;
561                 c45_ids->device_ids[i] |= (phy_reg & 0xffff);
562         }
563         *phy_id = 0;
564         return 0;
565 }
566
567 /**
568  * get_phy_id - reads the specified addr for its ID.
569  * @bus: the target MII bus
570  * @addr: PHY address on the MII bus
571  * @phy_id: where to store the ID retrieved.
572  * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
573  * @c45_ids: where to store the c45 ID information.
574  *
575  * Description: In the case of a 802.3-c22 PHY, reads the ID registers
576  *   of the PHY at @addr on the @bus, stores it in @phy_id and returns
577  *   zero on success.
578  *
579  *   In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and
580  *   its return value is in turn returned.
581  *
582  */
583 static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
584                       bool is_c45, struct phy_c45_device_ids *c45_ids)
585 {
586         int phy_reg;
587
588         if (is_c45)
589                 return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
590
591         /* Grab the bits from PHYIR1, and put them in the upper half */
592         phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
593         if (phy_reg < 0) {
594                 /* if there is no device, return without an error so scanning
595                  * the bus works properly
596                  */
597                 if (phy_reg == -EIO || phy_reg == -ENODEV) {
598                         *phy_id = 0xffffffff;
599                         return 0;
600                 }
601
602                 return -EIO;
603         }
604
605         *phy_id = (phy_reg & 0xffff) << 16;
606
607         /* Grab the bits from PHYIR2, and put them in the lower half */
608         phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
609         if (phy_reg < 0) {
610                 /* returning -ENODEV doesn't stop bus scanning */
611                 return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
612         }
613
614         *phy_id |= (phy_reg & 0xffff);
615
616         return 0;
617 }
618
619 /**
620  * get_phy_device - reads the specified PHY device and returns its @phy_device
621  *                  struct
622  * @bus: the target MII bus
623  * @addr: PHY address on the MII bus
624  * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
625  *
626  * Description: Reads the ID registers of the PHY at @addr on the
627  *   @bus, then allocates and returns the phy_device to represent it.
628  */
629 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
630 {
631         struct phy_c45_device_ids c45_ids = {0};
632         u32 phy_id = 0;
633         int r;
634
635         r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
636         if (r)
637                 return ERR_PTR(r);
638
639         /* If the phy_id is mostly Fs, there is no device there */
640         if ((phy_id & 0x1fffffff) == 0x1fffffff)
641                 return ERR_PTR(-ENODEV);
642
643         return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
644 }
645 EXPORT_SYMBOL(get_phy_device);
646
647 /**
648  * phy_device_register - Register the phy device on the MDIO bus
649  * @phydev: phy_device structure to be added to the MDIO bus
650  */
651 int phy_device_register(struct phy_device *phydev)
652 {
653         int err;
654
655         err = mdiobus_register_device(&phydev->mdio);
656         if (err)
657                 return err;
658
659         /* Deassert the reset signal */
660         phy_device_reset(phydev, 0);
661
662         /* Run all of the fixups for this PHY */
663         err = phy_scan_fixups(phydev);
664         if (err) {
665                 pr_err("PHY %d failed to initialize\n", phydev->mdio.addr);
666                 goto out;
667         }
668
669         err = device_add(&phydev->mdio.dev);
670         if (err) {
671                 pr_err("PHY %d failed to add\n", phydev->mdio.addr);
672                 goto out;
673         }
674
675         return 0;
676
677  out:
678         /* Assert the reset signal */
679         phy_device_reset(phydev, 1);
680
681         mdiobus_unregister_device(&phydev->mdio);
682         return err;
683 }
684 EXPORT_SYMBOL(phy_device_register);
685
686 /**
687  * phy_device_remove - Remove a previously registered phy device from the MDIO bus
688  * @phydev: phy_device structure to remove
689  *
690  * This doesn't free the phy_device itself, it merely reverses the effects
691  * of phy_device_register(). Use phy_device_free() to free the device
692  * after calling this function.
693  */
694 void phy_device_remove(struct phy_device *phydev)
695 {
696         device_del(&phydev->mdio.dev);
697
698         /* Assert the reset signal */
699         phy_device_reset(phydev, 1);
700
701         mdiobus_unregister_device(&phydev->mdio);
702 }
703 EXPORT_SYMBOL(phy_device_remove);
704
705 /**
706  * phy_find_first - finds the first PHY device on the bus
707  * @bus: the target MII bus
708  */
709 struct phy_device *phy_find_first(struct mii_bus *bus)
710 {
711         struct phy_device *phydev;
712         int addr;
713
714         for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
715                 phydev = mdiobus_get_phy(bus, addr);
716                 if (phydev)
717                         return phydev;
718         }
719         return NULL;
720 }
721 EXPORT_SYMBOL(phy_find_first);
722
723 static void phy_link_change(struct phy_device *phydev, bool up, bool do_carrier)
724 {
725         struct net_device *netdev = phydev->attached_dev;
726
727         if (do_carrier) {
728                 if (up)
729                         netif_carrier_on(netdev);
730                 else
731                         netif_carrier_off(netdev);
732         }
733         phydev->adjust_link(netdev);
734 }
735
736 /**
737  * phy_prepare_link - prepares the PHY layer to monitor link status
738  * @phydev: target phy_device struct
739  * @handler: callback function for link status change notifications
740  *
741  * Description: Tells the PHY infrastructure to handle the
742  *   gory details on monitoring link status (whether through
743  *   polling or an interrupt), and to call back to the
744  *   connected device driver when the link status changes.
745  *   If you want to monitor your own link state, don't call
746  *   this function.
747  */
748 static void phy_prepare_link(struct phy_device *phydev,
749                              void (*handler)(struct net_device *))
750 {
751         phydev->adjust_link = handler;
752 }
753
754 /**
755  * phy_connect_direct - connect an ethernet device to a specific phy_device
756  * @dev: the network device to connect
757  * @phydev: the pointer to the phy device
758  * @handler: callback function for state change notifications
759  * @interface: PHY device's interface
760  */
761 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
762                        void (*handler)(struct net_device *),
763                        phy_interface_t interface)
764 {
765         int rc;
766
767         if (!dev)
768                 return -EINVAL;
769
770         rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
771         if (rc)
772                 return rc;
773
774         phy_prepare_link(phydev, handler);
775         phy_start_machine(phydev);
776         if (phydev->irq > 0)
777                 phy_start_interrupts(phydev);
778
779         return 0;
780 }
781 EXPORT_SYMBOL(phy_connect_direct);
782
783 /**
784  * phy_connect - connect an ethernet device to a PHY device
785  * @dev: the network device to connect
786  * @bus_id: the id string of the PHY device to connect
787  * @handler: callback function for state change notifications
788  * @interface: PHY device's interface
789  *
790  * Description: Convenience function for connecting ethernet
791  *   devices to PHY devices.  The default behavior is for
792  *   the PHY infrastructure to handle everything, and only notify
793  *   the connected driver when the link status changes.  If you
794  *   don't want, or can't use the provided functionality, you may
795  *   choose to call only the subset of functions which provide
796  *   the desired functionality.
797  */
798 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
799                                void (*handler)(struct net_device *),
800                                phy_interface_t interface)
801 {
802         struct phy_device *phydev;
803         struct device *d;
804         int rc;
805
806         /* Search the list of PHY devices on the mdio bus for the
807          * PHY with the requested name
808          */
809         d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
810         if (!d) {
811                 pr_err("PHY %s not found\n", bus_id);
812                 return ERR_PTR(-ENODEV);
813         }
814         phydev = to_phy_device(d);
815
816         rc = phy_connect_direct(dev, phydev, handler, interface);
817         put_device(d);
818         if (rc)
819                 return ERR_PTR(rc);
820
821         return phydev;
822 }
823 EXPORT_SYMBOL(phy_connect);
824
825 /**
826  * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
827  *                  device
828  * @phydev: target phy_device struct
829  */
830 void phy_disconnect(struct phy_device *phydev)
831 {
832         if (phydev->irq > 0)
833                 phy_stop_interrupts(phydev);
834
835         phy_stop_machine(phydev);
836
837         phydev->adjust_link = NULL;
838
839         phy_detach(phydev);
840 }
841 EXPORT_SYMBOL(phy_disconnect);
842
843 /**
844  * phy_poll_reset - Safely wait until a PHY reset has properly completed
845  * @phydev: The PHY device to poll
846  *
847  * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
848  *   published in 2008, a PHY reset may take up to 0.5 seconds.  The MII BMCR
849  *   register must be polled until the BMCR_RESET bit clears.
850  *
851  *   Furthermore, any attempts to write to PHY registers may have no effect
852  *   or even generate MDIO bus errors until this is complete.
853  *
854  *   Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
855  *   standard and do not fully reset after the BMCR_RESET bit is set, and may
856  *   even *REQUIRE* a soft-reset to properly restart autonegotiation.  In an
857  *   effort to support such broken PHYs, this function is separate from the
858  *   standard phy_init_hw() which will zero all the other bits in the BMCR
859  *   and reapply all driver-specific and board-specific fixups.
860  */
861 static int phy_poll_reset(struct phy_device *phydev)
862 {
863         /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
864         unsigned int retries = 12;
865         int ret;
866
867         do {
868                 msleep(50);
869                 ret = phy_read(phydev, MII_BMCR);
870                 if (ret < 0)
871                         return ret;
872         } while (ret & BMCR_RESET && --retries);
873         if (ret & BMCR_RESET)
874                 return -ETIMEDOUT;
875
876         /* Some chips (smsc911x) may still need up to another 1ms after the
877          * BMCR_RESET bit is cleared before they are usable.
878          */
879         msleep(1);
880         return 0;
881 }
882
883 int phy_init_hw(struct phy_device *phydev)
884 {
885         int ret = 0;
886
887         /* Deassert the reset signal */
888         phy_device_reset(phydev, 0);
889
890         if (!phydev->drv || !phydev->drv->config_init)
891                 return 0;
892
893         if (phydev->drv->soft_reset)
894                 ret = phydev->drv->soft_reset(phydev);
895         else
896                 ret = genphy_soft_reset(phydev);
897
898         if (ret < 0)
899                 return ret;
900
901         ret = phy_scan_fixups(phydev);
902         if (ret < 0)
903                 return ret;
904
905         return phydev->drv->config_init(phydev);
906 }
907 EXPORT_SYMBOL(phy_init_hw);
908
909 void phy_attached_info(struct phy_device *phydev)
910 {
911         phy_attached_print(phydev, NULL);
912 }
913 EXPORT_SYMBOL(phy_attached_info);
914
915 #define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%s)"
916 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
917 {
918         const char *drv_name = phydev->drv ? phydev->drv->name : "unbound";
919         char *irq_str;
920         char irq_num[8];
921
922         switch(phydev->irq) {
923         case PHY_POLL:
924                 irq_str = "POLL";
925                 break;
926         case PHY_IGNORE_INTERRUPT:
927                 irq_str = "IGNORE";
928                 break;
929         default:
930                 snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
931                 irq_str = irq_num;
932                 break;
933         }
934
935
936         if (!fmt) {
937                 dev_info(&phydev->mdio.dev, ATTACHED_FMT "\n",
938                          drv_name, phydev_name(phydev),
939                          irq_str);
940         } else {
941                 va_list ap;
942
943                 dev_info(&phydev->mdio.dev, ATTACHED_FMT,
944                          drv_name, phydev_name(phydev),
945                          irq_str);
946
947                 va_start(ap, fmt);
948                 vprintk(fmt, ap);
949                 va_end(ap);
950         }
951 }
952 EXPORT_SYMBOL(phy_attached_print);
953
954 /**
955  * phy_attach_direct - attach a network device to a given PHY device pointer
956  * @dev: network device to attach
957  * @phydev: Pointer to phy_device to attach
958  * @flags: PHY device's dev_flags
959  * @interface: PHY device's interface
960  *
961  * Description: Called by drivers to attach to a particular PHY
962  *     device. The phy_device is found, and properly hooked up
963  *     to the phy_driver.  If no driver is attached, then a
964  *     generic driver is used.  The phy_device is given a ptr to
965  *     the attaching device, and given a callback for link status
966  *     change.  The phy_device is returned to the attaching driver.
967  *     This function takes a reference on the phy device.
968  */
969 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
970                       u32 flags, phy_interface_t interface)
971 {
972         struct module *ndev_owner = dev->dev.parent->driver->owner;
973         struct mii_bus *bus = phydev->mdio.bus;
974         struct device *d = &phydev->mdio.dev;
975         bool using_genphy = false;
976         int err;
977
978         /* For Ethernet device drivers that register their own MDIO bus, we
979          * will have bus->owner match ndev_mod, so we do not want to increment
980          * our own module->refcnt here, otherwise we would not be able to
981          * unload later on.
982          */
983         if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
984                 dev_err(&dev->dev, "failed to get the bus module\n");
985                 return -EIO;
986         }
987
988         get_device(d);
989
990         /* Assume that if there is no driver, that it doesn't
991          * exist, and we should use the genphy driver.
992          */
993         if (!d->driver) {
994                 if (phydev->is_c45)
995                         d->driver = &genphy_10g_driver.mdiodrv.driver;
996                 else
997                         d->driver = &genphy_driver.mdiodrv.driver;
998
999                 using_genphy = true;
1000         }
1001
1002         if (!try_module_get(d->driver->owner)) {
1003                 dev_err(&dev->dev, "failed to get the device driver module\n");
1004                 err = -EIO;
1005                 goto error_put_device;
1006         }
1007
1008         if (using_genphy) {
1009                 err = d->driver->probe(d);
1010                 if (err >= 0)
1011                         err = device_bind_driver(d);
1012
1013                 if (err)
1014                         goto error_module_put;
1015         }
1016
1017         if (phydev->attached_dev) {
1018                 dev_err(&dev->dev, "PHY already attached\n");
1019                 err = -EBUSY;
1020                 goto error;
1021         }
1022
1023         phydev->phy_link_change = phy_link_change;
1024         phydev->attached_dev = dev;
1025         dev->phydev = phydev;
1026
1027         /* Some Ethernet drivers try to connect to a PHY device before
1028          * calling register_netdevice() -> netdev_register_kobject() and
1029          * does the dev->dev.kobj initialization. Here we only check for
1030          * success which indicates that the network device kobject is
1031          * ready. Once we do that we still need to keep track of whether
1032          * links were successfully set up or not for phy_detach() to
1033          * remove them accordingly.
1034          */
1035         phydev->sysfs_links = false;
1036
1037         err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
1038                                 "attached_dev");
1039         if (!err) {
1040                 err = sysfs_create_link_nowarn(&dev->dev.kobj,
1041                                                &phydev->mdio.dev.kobj,
1042                                                "phydev");
1043                 if (err) {
1044                         dev_err(&dev->dev, "could not add device link to %s err %d\n",
1045                                 kobject_name(&phydev->mdio.dev.kobj),
1046                                 err);
1047                         /* non-fatal - some net drivers can use one netdevice
1048                          * with more then one phy
1049                          */
1050                 }
1051
1052                 phydev->sysfs_links = true;
1053         }
1054
1055         phydev->dev_flags = flags;
1056
1057         phydev->interface = interface;
1058
1059         phydev->state = PHY_READY;
1060
1061         /* Initial carrier state is off as the phy is about to be
1062          * (re)initialized.
1063          */
1064         netif_carrier_off(phydev->attached_dev);
1065
1066         /* Do initial configuration here, now that
1067          * we have certain key parameters
1068          * (dev_flags and interface)
1069          */
1070         err = phy_init_hw(phydev);
1071         if (err)
1072                 goto error;
1073
1074         phy_resume(phydev);
1075         phy_led_triggers_register(phydev);
1076
1077         return err;
1078
1079 error:
1080         /* phy_detach() does all of the cleanup below */
1081         phy_detach(phydev);
1082         return err;
1083
1084 error_module_put:
1085         module_put(d->driver->owner);
1086         d->driver = NULL;
1087 error_put_device:
1088         put_device(d);
1089         if (ndev_owner != bus->owner)
1090                 module_put(bus->owner);
1091         return err;
1092 }
1093 EXPORT_SYMBOL(phy_attach_direct);
1094
1095 /**
1096  * phy_attach - attach a network device to a particular PHY device
1097  * @dev: network device to attach
1098  * @bus_id: Bus ID of PHY device to attach
1099  * @interface: PHY device's interface
1100  *
1101  * Description: Same as phy_attach_direct() except that a PHY bus_id
1102  *     string is passed instead of a pointer to a struct phy_device.
1103  */
1104 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1105                               phy_interface_t interface)
1106 {
1107         struct bus_type *bus = &mdio_bus_type;
1108         struct phy_device *phydev;
1109         struct device *d;
1110         int rc;
1111
1112         if (!dev)
1113                 return ERR_PTR(-EINVAL);
1114
1115         /* Search the list of PHY devices on the mdio bus for the
1116          * PHY with the requested name
1117          */
1118         d = bus_find_device_by_name(bus, NULL, bus_id);
1119         if (!d) {
1120                 pr_err("PHY %s not found\n", bus_id);
1121                 return ERR_PTR(-ENODEV);
1122         }
1123         phydev = to_phy_device(d);
1124
1125         rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
1126         put_device(d);
1127         if (rc)
1128                 return ERR_PTR(rc);
1129
1130         return phydev;
1131 }
1132 EXPORT_SYMBOL(phy_attach);
1133
1134 /**
1135  * phy_detach - detach a PHY device from its network device
1136  * @phydev: target phy_device struct
1137  *
1138  * This detaches the phy device from its network device and the phy
1139  * driver, and drops the reference count taken in phy_attach_direct().
1140  */
1141 void phy_detach(struct phy_device *phydev)
1142 {
1143         struct net_device *dev = phydev->attached_dev;
1144         struct module *ndev_owner = dev->dev.parent->driver->owner;
1145         struct mii_bus *bus;
1146
1147         if (phydev->sysfs_links) {
1148                 sysfs_remove_link(&dev->dev.kobj, "phydev");
1149                 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
1150         }
1151         phy_suspend(phydev);
1152         phydev->attached_dev->phydev = NULL;
1153         phydev->attached_dev = NULL;
1154         phydev->phylink = NULL;
1155
1156         phy_led_triggers_unregister(phydev);
1157
1158         if (phydev->mdio.dev.driver)
1159                 module_put(phydev->mdio.dev.driver->owner);
1160
1161         /* If the device had no specific driver before (i.e. - it
1162          * was using the generic driver), we unbind the device
1163          * from the generic driver so that there's a chance a
1164          * real driver could be loaded
1165          */
1166         if (phydev->mdio.dev.driver == &genphy_10g_driver.mdiodrv.driver ||
1167             phydev->mdio.dev.driver == &genphy_driver.mdiodrv.driver)
1168                 device_release_driver(&phydev->mdio.dev);
1169
1170         /* Assert the reset signal */
1171         phy_device_reset(phydev, 1);
1172
1173         /*
1174          * The phydev might go away on the put_device() below, so avoid
1175          * a use-after-free bug by reading the underlying bus first.
1176          */
1177         bus = phydev->mdio.bus;
1178
1179         put_device(&phydev->mdio.dev);
1180         if (ndev_owner != bus->owner)
1181                 module_put(bus->owner);
1182 }
1183 EXPORT_SYMBOL(phy_detach);
1184
1185 int phy_suspend(struct phy_device *phydev)
1186 {
1187         struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1188         struct net_device *netdev = phydev->attached_dev;
1189         struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1190         int ret = 0;
1191
1192         /* If the device has WOL enabled, we cannot suspend the PHY */
1193         phy_ethtool_get_wol(phydev, &wol);
1194         if (wol.wolopts || (netdev && netdev->wol_enabled))
1195                 return -EBUSY;
1196
1197         if (phydev->drv && phydrv->suspend)
1198                 ret = phydrv->suspend(phydev);
1199
1200         if (ret)
1201                 return ret;
1202
1203         phydev->suspended = true;
1204
1205         return ret;
1206 }
1207 EXPORT_SYMBOL(phy_suspend);
1208
1209 int __phy_resume(struct phy_device *phydev)
1210 {
1211         struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1212         int ret = 0;
1213
1214         WARN_ON(!mutex_is_locked(&phydev->lock));
1215
1216         if (phydev->drv && phydrv->resume)
1217                 ret = phydrv->resume(phydev);
1218
1219         if (ret)
1220                 return ret;
1221
1222         phydev->suspended = false;
1223
1224         return ret;
1225 }
1226 EXPORT_SYMBOL(__phy_resume);
1227
1228 int phy_resume(struct phy_device *phydev)
1229 {
1230         int ret;
1231
1232         mutex_lock(&phydev->lock);
1233         ret = __phy_resume(phydev);
1234         mutex_unlock(&phydev->lock);
1235
1236         return ret;
1237 }
1238 EXPORT_SYMBOL(phy_resume);
1239
1240 int phy_loopback(struct phy_device *phydev, bool enable)
1241 {
1242         struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1243         int ret = 0;
1244
1245         mutex_lock(&phydev->lock);
1246
1247         if (enable && phydev->loopback_enabled) {
1248                 ret = -EBUSY;
1249                 goto out;
1250         }
1251
1252         if (!enable && !phydev->loopback_enabled) {
1253                 ret = -EINVAL;
1254                 goto out;
1255         }
1256
1257         if (phydev->drv && phydrv->set_loopback)
1258                 ret = phydrv->set_loopback(phydev, enable);
1259         else
1260                 ret = -EOPNOTSUPP;
1261
1262         if (ret)
1263                 goto out;
1264
1265         phydev->loopback_enabled = enable;
1266
1267 out:
1268         mutex_unlock(&phydev->lock);
1269         return ret;
1270 }
1271 EXPORT_SYMBOL(phy_loopback);
1272
1273 /**
1274  * phy_reset_after_clk_enable - perform a PHY reset if needed
1275  * @phydev: target phy_device struct
1276  *
1277  * Description: Some PHYs are known to need a reset after their refclk was
1278  *   enabled. This function evaluates the flags and perform the reset if it's
1279  *   needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
1280  *   was reset.
1281  */
1282 int phy_reset_after_clk_enable(struct phy_device *phydev)
1283 {
1284         if (!phydev || !phydev->drv)
1285                 return -ENODEV;
1286
1287         if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
1288                 phy_device_reset(phydev, 1);
1289                 phy_device_reset(phydev, 0);
1290                 return 1;
1291         }
1292
1293         return 0;
1294 }
1295 EXPORT_SYMBOL(phy_reset_after_clk_enable);
1296
1297 /* Generic PHY support and helper functions */
1298
1299 /**
1300  * genphy_config_advert - sanitize and advertise auto-negotiation parameters
1301  * @phydev: target phy_device struct
1302  *
1303  * Description: Writes MII_ADVERTISE with the appropriate values,
1304  *   after sanitizing the values to make sure we only advertise
1305  *   what is supported.  Returns < 0 on error, 0 if the PHY's advertisement
1306  *   hasn't changed, and > 0 if it has changed.
1307  */
1308 static int genphy_config_advert(struct phy_device *phydev)
1309 {
1310         u32 advertise;
1311         int oldadv, adv, bmsr;
1312         int err, changed = 0;
1313
1314         /* Only allow advertising what this PHY supports */
1315         phydev->advertising &= phydev->supported;
1316         advertise = phydev->advertising;
1317
1318         /* Setup standard advertisement */
1319         adv = phy_read(phydev, MII_ADVERTISE);
1320         if (adv < 0)
1321                 return adv;
1322
1323         oldadv = adv;
1324         adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
1325                  ADVERTISE_PAUSE_ASYM);
1326         adv |= ethtool_adv_to_mii_adv_t(advertise);
1327
1328         if (adv != oldadv) {
1329                 err = phy_write(phydev, MII_ADVERTISE, adv);
1330
1331                 if (err < 0)
1332                         return err;
1333                 changed = 1;
1334         }
1335
1336         bmsr = phy_read(phydev, MII_BMSR);
1337         if (bmsr < 0)
1338                 return bmsr;
1339
1340         /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
1341          * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
1342          * logical 1.
1343          */
1344         if (!(bmsr & BMSR_ESTATEN))
1345                 return changed;
1346
1347         /* Configure gigabit if it's supported */
1348         adv = phy_read(phydev, MII_CTRL1000);
1349         if (adv < 0)
1350                 return adv;
1351
1352         oldadv = adv;
1353         adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1354
1355         if (phydev->supported & (SUPPORTED_1000baseT_Half |
1356                                  SUPPORTED_1000baseT_Full)) {
1357                 adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
1358         }
1359
1360         if (adv != oldadv)
1361                 changed = 1;
1362
1363         err = phy_write(phydev, MII_CTRL1000, adv);
1364         if (err < 0)
1365                 return err;
1366
1367         return changed;
1368 }
1369
1370 /**
1371  * genphy_config_eee_advert - disable unwanted eee mode advertisement
1372  * @phydev: target phy_device struct
1373  *
1374  * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
1375  *   efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
1376  *   changed, and 1 if it has changed.
1377  */
1378 static int genphy_config_eee_advert(struct phy_device *phydev)
1379 {
1380         int broken = phydev->eee_broken_modes;
1381         int old_adv, adv;
1382
1383         /* Nothing to disable */
1384         if (!broken)
1385                 return 0;
1386
1387         /* If the following call fails, we assume that EEE is not
1388          * supported by the phy. If we read 0, EEE is not advertised
1389          * In both case, we don't need to continue
1390          */
1391         adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1392         if (adv <= 0)
1393                 return 0;
1394
1395         old_adv = adv;
1396         adv &= ~broken;
1397
1398         /* Advertising remains unchanged with the broken mask */
1399         if (old_adv == adv)
1400                 return 0;
1401
1402         phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1403
1404         return 1;
1405 }
1406
1407 /**
1408  * genphy_setup_forced - configures/forces speed/duplex from @phydev
1409  * @phydev: target phy_device struct
1410  *
1411  * Description: Configures MII_BMCR to force speed/duplex
1412  *   to the values in phydev. Assumes that the values are valid.
1413  *   Please see phy_sanitize_settings().
1414  */
1415 int genphy_setup_forced(struct phy_device *phydev)
1416 {
1417         u16 ctl = 0;
1418
1419         phydev->pause = 0;
1420         phydev->asym_pause = 0;
1421
1422         if (SPEED_1000 == phydev->speed)
1423                 ctl |= BMCR_SPEED1000;
1424         else if (SPEED_100 == phydev->speed)
1425                 ctl |= BMCR_SPEED100;
1426
1427         if (DUPLEX_FULL == phydev->duplex)
1428                 ctl |= BMCR_FULLDPLX;
1429
1430         return phy_modify(phydev, MII_BMCR,
1431                           ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
1432 }
1433 EXPORT_SYMBOL(genphy_setup_forced);
1434
1435 /**
1436  * genphy_restart_aneg - Enable and Restart Autonegotiation
1437  * @phydev: target phy_device struct
1438  */
1439 int genphy_restart_aneg(struct phy_device *phydev)
1440 {
1441         /* Don't isolate the PHY if we're negotiating */
1442         return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
1443                           BMCR_ANENABLE | BMCR_ANRESTART);
1444 }
1445 EXPORT_SYMBOL(genphy_restart_aneg);
1446
1447 /**
1448  * genphy_config_aneg - restart auto-negotiation or write BMCR
1449  * @phydev: target phy_device struct
1450  *
1451  * Description: If auto-negotiation is enabled, we configure the
1452  *   advertising, and then restart auto-negotiation.  If it is not
1453  *   enabled, then we write the BMCR.
1454  */
1455 int genphy_config_aneg(struct phy_device *phydev)
1456 {
1457         int err, changed;
1458
1459         changed = genphy_config_eee_advert(phydev);
1460
1461         if (AUTONEG_ENABLE != phydev->autoneg)
1462                 return genphy_setup_forced(phydev);
1463
1464         err = genphy_config_advert(phydev);
1465         if (err < 0) /* error */
1466                 return err;
1467
1468         changed |= err;
1469
1470         if (changed == 0) {
1471                 /* Advertisement hasn't changed, but maybe aneg was never on to
1472                  * begin with?  Or maybe phy was isolated?
1473                  */
1474                 int ctl = phy_read(phydev, MII_BMCR);
1475
1476                 if (ctl < 0)
1477                         return ctl;
1478
1479                 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
1480                         changed = 1; /* do restart aneg */
1481         }
1482
1483         /* Only restart aneg if we are advertising something different
1484          * than we were before.
1485          */
1486         if (changed > 0)
1487                 return genphy_restart_aneg(phydev);
1488
1489         return 0;
1490 }
1491 EXPORT_SYMBOL(genphy_config_aneg);
1492
1493 /**
1494  * genphy_aneg_done - return auto-negotiation status
1495  * @phydev: target phy_device struct
1496  *
1497  * Description: Reads the status register and returns 0 either if
1498  *   auto-negotiation is incomplete, or if there was an error.
1499  *   Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
1500  */
1501 int genphy_aneg_done(struct phy_device *phydev)
1502 {
1503         int retval = phy_read(phydev, MII_BMSR);
1504
1505         return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1506 }
1507 EXPORT_SYMBOL(genphy_aneg_done);
1508
1509 /**
1510  * genphy_update_link - update link status in @phydev
1511  * @phydev: target phy_device struct
1512  *
1513  * Description: Update the value in phydev->link to reflect the
1514  *   current link value.  In order to do this, we need to read
1515  *   the status register twice, keeping the second value.
1516  */
1517 int genphy_update_link(struct phy_device *phydev)
1518 {
1519         int status;
1520
1521         /* The link state is latched low so that momentary link
1522          * drops can be detected. Do not double-read the status
1523          * in polling mode to detect such short link drops.
1524          */
1525         if (!phy_polling_mode(phydev)) {
1526                 status = phy_read(phydev, MII_BMSR);
1527                 if (status < 0)
1528                         return status;
1529         }
1530
1531         /* Read link and autonegotiation status */
1532         status = phy_read(phydev, MII_BMSR);
1533         if (status < 0)
1534                 return status;
1535
1536         if ((status & BMSR_LSTATUS) == 0)
1537                 phydev->link = 0;
1538         else
1539                 phydev->link = 1;
1540
1541         return 0;
1542 }
1543 EXPORT_SYMBOL(genphy_update_link);
1544
1545 /**
1546  * genphy_read_status - check the link status and update current link state
1547  * @phydev: target phy_device struct
1548  *
1549  * Description: Check the link, then figure out the current state
1550  *   by comparing what we advertise with what the link partner
1551  *   advertises.  Start by checking the gigabit possibilities,
1552  *   then move on to 10/100.
1553  */
1554 int genphy_read_status(struct phy_device *phydev)
1555 {
1556         int adv;
1557         int err;
1558         int lpa;
1559         int lpagb = 0;
1560         int common_adv;
1561         int common_adv_gb = 0;
1562
1563         /* Update the link, but return if there was an error */
1564         err = genphy_update_link(phydev);
1565         if (err)
1566                 return err;
1567
1568         phydev->lp_advertising = 0;
1569
1570         if (AUTONEG_ENABLE == phydev->autoneg) {
1571                 if (phydev->supported & (SUPPORTED_1000baseT_Half
1572                                         | SUPPORTED_1000baseT_Full)) {
1573                         lpagb = phy_read(phydev, MII_STAT1000);
1574                         if (lpagb < 0)
1575                                 return lpagb;
1576
1577                         adv = phy_read(phydev, MII_CTRL1000);
1578                         if (adv < 0)
1579                                 return adv;
1580
1581                         if (lpagb & LPA_1000MSFAIL) {
1582                                 if (adv & CTL1000_ENABLE_MASTER)
1583                                         phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
1584                                 else
1585                                         phydev_err(phydev, "Master/Slave resolution failed\n");
1586                                 return -ENOLINK;
1587                         }
1588
1589                         phydev->lp_advertising =
1590                                 mii_stat1000_to_ethtool_lpa_t(lpagb);
1591                         common_adv_gb = lpagb & adv << 2;
1592                 }
1593
1594                 lpa = phy_read(phydev, MII_LPA);
1595                 if (lpa < 0)
1596                         return lpa;
1597
1598                 phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa);
1599
1600                 adv = phy_read(phydev, MII_ADVERTISE);
1601                 if (adv < 0)
1602                         return adv;
1603
1604                 common_adv = lpa & adv;
1605
1606                 phydev->speed = SPEED_10;
1607                 phydev->duplex = DUPLEX_HALF;
1608                 phydev->pause = 0;
1609                 phydev->asym_pause = 0;
1610
1611                 if (common_adv_gb & (LPA_1000FULL | LPA_1000HALF)) {
1612                         phydev->speed = SPEED_1000;
1613
1614                         if (common_adv_gb & LPA_1000FULL)
1615                                 phydev->duplex = DUPLEX_FULL;
1616                 } else if (common_adv & (LPA_100FULL | LPA_100HALF)) {
1617                         phydev->speed = SPEED_100;
1618
1619                         if (common_adv & LPA_100FULL)
1620                                 phydev->duplex = DUPLEX_FULL;
1621                 } else
1622                         if (common_adv & LPA_10FULL)
1623                                 phydev->duplex = DUPLEX_FULL;
1624
1625                 if (phydev->duplex == DUPLEX_FULL) {
1626                         phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
1627                         phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
1628                 }
1629         } else {
1630                 int bmcr = phy_read(phydev, MII_BMCR);
1631
1632                 if (bmcr < 0)
1633                         return bmcr;
1634
1635                 if (bmcr & BMCR_FULLDPLX)
1636                         phydev->duplex = DUPLEX_FULL;
1637                 else
1638                         phydev->duplex = DUPLEX_HALF;
1639
1640                 if (bmcr & BMCR_SPEED1000)
1641                         phydev->speed = SPEED_1000;
1642                 else if (bmcr & BMCR_SPEED100)
1643                         phydev->speed = SPEED_100;
1644                 else
1645                         phydev->speed = SPEED_10;
1646
1647                 phydev->pause = 0;
1648                 phydev->asym_pause = 0;
1649         }
1650
1651         return 0;
1652 }
1653 EXPORT_SYMBOL(genphy_read_status);
1654
1655 /**
1656  * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
1657  * @phydev: target phy_device struct
1658  *
1659  * Description: Perform a software PHY reset using the standard
1660  * BMCR_RESET bit and poll for the reset bit to be cleared.
1661  *
1662  * Returns: 0 on success, < 0 on failure
1663  */
1664 int genphy_soft_reset(struct phy_device *phydev)
1665 {
1666         int ret;
1667
1668         ret = phy_set_bits(phydev, MII_BMCR, BMCR_RESET);
1669         if (ret < 0)
1670                 return ret;
1671
1672         return phy_poll_reset(phydev);
1673 }
1674 EXPORT_SYMBOL(genphy_soft_reset);
1675
1676 int genphy_config_init(struct phy_device *phydev)
1677 {
1678         int val;
1679         u32 features;
1680
1681         features = (SUPPORTED_TP | SUPPORTED_MII
1682                         | SUPPORTED_AUI | SUPPORTED_FIBRE |
1683                         SUPPORTED_BNC | SUPPORTED_Pause | SUPPORTED_Asym_Pause);
1684
1685         /* Do we support autonegotiation? */
1686         val = phy_read(phydev, MII_BMSR);
1687         if (val < 0)
1688                 return val;
1689
1690         if (val & BMSR_ANEGCAPABLE)
1691                 features |= SUPPORTED_Autoneg;
1692
1693         if (val & BMSR_100FULL)
1694                 features |= SUPPORTED_100baseT_Full;
1695         if (val & BMSR_100HALF)
1696                 features |= SUPPORTED_100baseT_Half;
1697         if (val & BMSR_10FULL)
1698                 features |= SUPPORTED_10baseT_Full;
1699         if (val & BMSR_10HALF)
1700                 features |= SUPPORTED_10baseT_Half;
1701
1702         if (val & BMSR_ESTATEN) {
1703                 val = phy_read(phydev, MII_ESTATUS);
1704                 if (val < 0)
1705                         return val;
1706
1707                 if (val & ESTATUS_1000_TFULL)
1708                         features |= SUPPORTED_1000baseT_Full;
1709                 if (val & ESTATUS_1000_THALF)
1710                         features |= SUPPORTED_1000baseT_Half;
1711         }
1712
1713         phydev->supported &= features;
1714         phydev->advertising &= features;
1715
1716         return 0;
1717 }
1718 EXPORT_SYMBOL(genphy_config_init);
1719
1720 /* This is used for the phy device which doesn't support the MMD extended
1721  * register access, but it does have side effect when we are trying to access
1722  * the MMD register via indirect method.
1723  */
1724 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
1725 {
1726         return -EOPNOTSUPP;
1727 }
1728 EXPORT_SYMBOL(genphy_read_mmd_unsupported);
1729
1730 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
1731                                  u16 regnum, u16 val)
1732 {
1733         return -EOPNOTSUPP;
1734 }
1735 EXPORT_SYMBOL(genphy_write_mmd_unsupported);
1736
1737 int genphy_suspend(struct phy_device *phydev)
1738 {
1739         return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
1740 }
1741 EXPORT_SYMBOL(genphy_suspend);
1742
1743 int genphy_resume(struct phy_device *phydev)
1744 {
1745         return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
1746 }
1747 EXPORT_SYMBOL(genphy_resume);
1748
1749 int genphy_loopback(struct phy_device *phydev, bool enable)
1750 {
1751         return phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK,
1752                           enable ? BMCR_LOOPBACK : 0);
1753 }
1754 EXPORT_SYMBOL(genphy_loopback);
1755
1756 static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
1757 {
1758         switch (max_speed) {
1759         case SPEED_10:
1760                 phydev->supported &= ~PHY_100BT_FEATURES;
1761                 /* fall through */
1762         case SPEED_100:
1763                 phydev->supported &= ~PHY_1000BT_FEATURES;
1764                 break;
1765         case SPEED_1000:
1766                 break;
1767         default:
1768                 return -ENOTSUPP;
1769         }
1770
1771         return 0;
1772 }
1773
1774 int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
1775 {
1776         int err;
1777
1778         err = __set_phy_supported(phydev, max_speed);
1779         if (err)
1780                 return err;
1781
1782         phydev->advertising = phydev->supported;
1783
1784         return 0;
1785 }
1786 EXPORT_SYMBOL(phy_set_max_speed);
1787
1788 static void of_set_phy_supported(struct phy_device *phydev)
1789 {
1790         struct device_node *node = phydev->mdio.dev.of_node;
1791         u32 max_speed;
1792
1793         if (!IS_ENABLED(CONFIG_OF_MDIO))
1794                 return;
1795
1796         if (!node)
1797                 return;
1798
1799         if (!of_property_read_u32(node, "max-speed", &max_speed))
1800                 __set_phy_supported(phydev, max_speed);
1801 }
1802
1803 static void of_set_phy_eee_broken(struct phy_device *phydev)
1804 {
1805         struct device_node *node = phydev->mdio.dev.of_node;
1806         u32 broken = 0;
1807
1808         if (!IS_ENABLED(CONFIG_OF_MDIO))
1809                 return;
1810
1811         if (!node)
1812                 return;
1813
1814         if (of_property_read_bool(node, "eee-broken-100tx"))
1815                 broken |= MDIO_EEE_100TX;
1816         if (of_property_read_bool(node, "eee-broken-1000t"))
1817                 broken |= MDIO_EEE_1000T;
1818         if (of_property_read_bool(node, "eee-broken-10gt"))
1819                 broken |= MDIO_EEE_10GT;
1820         if (of_property_read_bool(node, "eee-broken-1000kx"))
1821                 broken |= MDIO_EEE_1000KX;
1822         if (of_property_read_bool(node, "eee-broken-10gkx4"))
1823                 broken |= MDIO_EEE_10GKX4;
1824         if (of_property_read_bool(node, "eee-broken-10gkr"))
1825                 broken |= MDIO_EEE_10GKR;
1826
1827         phydev->eee_broken_modes = broken;
1828 }
1829
1830 /**
1831  * phy_probe - probe and init a PHY device
1832  * @dev: device to probe and init
1833  *
1834  * Description: Take care of setting up the phy_device structure,
1835  *   set the state to READY (the driver's init function should
1836  *   set it to STARTING if needed).
1837  */
1838 static int phy_probe(struct device *dev)
1839 {
1840         struct phy_device *phydev = to_phy_device(dev);
1841         struct device_driver *drv = phydev->mdio.dev.driver;
1842         struct phy_driver *phydrv = to_phy_driver(drv);
1843         int err = 0;
1844
1845         phydev->drv = phydrv;
1846
1847         /* Disable the interrupt if the PHY doesn't support it
1848          * but the interrupt is still a valid one
1849          */
1850         if (!(phydrv->flags & PHY_HAS_INTERRUPT) &&
1851             phy_interrupt_is_valid(phydev))
1852                 phydev->irq = PHY_POLL;
1853
1854         if (phydrv->flags & PHY_IS_INTERNAL)
1855                 phydev->is_internal = true;
1856
1857         mutex_lock(&phydev->lock);
1858
1859         /* Start out supporting everything. Eventually,
1860          * a controller will attach, and may modify one
1861          * or both of these values
1862          */
1863         phydev->supported = phydrv->features;
1864         of_set_phy_supported(phydev);
1865         phydev->advertising = phydev->supported;
1866
1867         /* Get the EEE modes we want to prohibit. We will ask
1868          * the PHY stop advertising these mode later on
1869          */
1870         of_set_phy_eee_broken(phydev);
1871
1872         /* The Pause Frame bits indicate that the PHY can support passing
1873          * pause frames. During autonegotiation, the PHYs will determine if
1874          * they should allow pause frames to pass.  The MAC driver should then
1875          * use that result to determine whether to enable flow control via
1876          * pause frames.
1877          *
1878          * Normally, PHY drivers should not set the Pause bits, and instead
1879          * allow phylib to do that.  However, there may be some situations
1880          * (e.g. hardware erratum) where the driver wants to set only one
1881          * of these bits.
1882          */
1883         if (phydrv->features & (SUPPORTED_Pause | SUPPORTED_Asym_Pause)) {
1884                 phydev->supported &= ~(SUPPORTED_Pause | SUPPORTED_Asym_Pause);
1885                 phydev->supported |= phydrv->features &
1886                                      (SUPPORTED_Pause | SUPPORTED_Asym_Pause);
1887         } else {
1888                 phydev->supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
1889         }
1890
1891         /* Set the state to READY by default */
1892         phydev->state = PHY_READY;
1893
1894         if (phydev->drv->probe) {
1895                 /* Deassert the reset signal */
1896                 phy_device_reset(phydev, 0);
1897
1898                 err = phydev->drv->probe(phydev);
1899                 if (err) {
1900                         /* Assert the reset signal */
1901                         phy_device_reset(phydev, 1);
1902                 }
1903         }
1904
1905         mutex_unlock(&phydev->lock);
1906
1907         return err;
1908 }
1909
1910 static int phy_remove(struct device *dev)
1911 {
1912         struct phy_device *phydev = to_phy_device(dev);
1913
1914         cancel_delayed_work_sync(&phydev->state_queue);
1915
1916         mutex_lock(&phydev->lock);
1917         phydev->state = PHY_DOWN;
1918         mutex_unlock(&phydev->lock);
1919
1920         if (phydev->drv && phydev->drv->remove) {
1921                 phydev->drv->remove(phydev);
1922
1923                 /* Assert the reset signal */
1924                 phy_device_reset(phydev, 1);
1925         }
1926         phydev->drv = NULL;
1927
1928         return 0;
1929 }
1930
1931 /**
1932  * phy_driver_register - register a phy_driver with the PHY layer
1933  * @new_driver: new phy_driver to register
1934  * @owner: module owning this PHY
1935  */
1936 int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
1937 {
1938         int retval;
1939
1940         new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
1941         new_driver->mdiodrv.driver.name = new_driver->name;
1942         new_driver->mdiodrv.driver.bus = &mdio_bus_type;
1943         new_driver->mdiodrv.driver.probe = phy_probe;
1944         new_driver->mdiodrv.driver.remove = phy_remove;
1945         new_driver->mdiodrv.driver.owner = owner;
1946
1947         /* The following works around an issue where the PHY driver doesn't bind
1948          * to the device, resulting in the genphy driver being used instead of
1949          * the dedicated driver. The root cause of the issue isn't known yet
1950          * and seems to be in the base driver core. Once this is fixed we may
1951          * remove this workaround.
1952          */
1953         new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
1954
1955         retval = driver_register(&new_driver->mdiodrv.driver);
1956         if (retval) {
1957                 pr_err("%s: Error %d in registering driver\n",
1958                        new_driver->name, retval);
1959
1960                 return retval;
1961         }
1962
1963         pr_debug("%s: Registered new driver\n", new_driver->name);
1964
1965         return 0;
1966 }
1967 EXPORT_SYMBOL(phy_driver_register);
1968
1969 int phy_drivers_register(struct phy_driver *new_driver, int n,
1970                          struct module *owner)
1971 {
1972         int i, ret = 0;
1973
1974         for (i = 0; i < n; i++) {
1975                 ret = phy_driver_register(new_driver + i, owner);
1976                 if (ret) {
1977                         while (i-- > 0)
1978                                 phy_driver_unregister(new_driver + i);
1979                         break;
1980                 }
1981         }
1982         return ret;
1983 }
1984 EXPORT_SYMBOL(phy_drivers_register);
1985
1986 void phy_driver_unregister(struct phy_driver *drv)
1987 {
1988         driver_unregister(&drv->mdiodrv.driver);
1989 }
1990 EXPORT_SYMBOL(phy_driver_unregister);
1991
1992 void phy_drivers_unregister(struct phy_driver *drv, int n)
1993 {
1994         int i;
1995
1996         for (i = 0; i < n; i++)
1997                 phy_driver_unregister(drv + i);
1998 }
1999 EXPORT_SYMBOL(phy_drivers_unregister);
2000
2001 static struct phy_driver genphy_driver = {
2002         .phy_id         = 0xffffffff,
2003         .phy_id_mask    = 0xffffffff,
2004         .name           = "Generic PHY",
2005         .soft_reset     = genphy_no_soft_reset,
2006         .config_init    = genphy_config_init,
2007         .features       = PHY_GBIT_FEATURES | SUPPORTED_MII |
2008                           SUPPORTED_AUI | SUPPORTED_FIBRE |
2009                           SUPPORTED_BNC,
2010         .aneg_done      = genphy_aneg_done,
2011         .suspend        = genphy_suspend,
2012         .resume         = genphy_resume,
2013         .set_loopback   = genphy_loopback,
2014 };
2015
2016 static int __init phy_init(void)
2017 {
2018         int rc;
2019
2020         rc = mdio_bus_init();
2021         if (rc)
2022                 return rc;
2023
2024         rc = phy_driver_register(&genphy_10g_driver, THIS_MODULE);
2025         if (rc)
2026                 goto err_10g;
2027
2028         rc = phy_driver_register(&genphy_driver, THIS_MODULE);
2029         if (rc) {
2030                 phy_driver_unregister(&genphy_10g_driver);
2031 err_10g:
2032                 mdio_bus_exit();
2033         }
2034
2035         return rc;
2036 }
2037
2038 static void __exit phy_exit(void)
2039 {
2040         phy_driver_unregister(&genphy_10g_driver);
2041         phy_driver_unregister(&genphy_driver);
2042         mdio_bus_exit();
2043 }
2044
2045 subsys_initcall(phy_init);
2046 module_exit(phy_exit);