GNU Linux-libre 4.4.289-gnu1
[releases.git] / drivers / net / phy / mdio_bus.c
1 /* MDIO Bus interface
2  *
3  * Author: Andy Fleming
4  *
5  * Copyright (c) 2004 Freescale Semiconductor, Inc.
6  *
7  * This program is free software; you can redistribute  it and/or modify it
8  * under  the terms of  the GNU General  Public License as published by the
9  * Free Software Foundation;  either version 2 of the  License, or (at your
10  * option) any later version.
11  *
12  */
13
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/unistd.h>
20 #include <linux/slab.h>
21 #include <linux/interrupt.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/device.h>
25 #include <linux/of_device.h>
26 #include <linux/of_mdio.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/spinlock.h>
31 #include <linux/mm.h>
32 #include <linux/module.h>
33 #include <linux/mii.h>
34 #include <linux/ethtool.h>
35 #include <linux/phy.h>
36 #include <linux/io.h>
37 #include <linux/uaccess.h>
38
39 #include <asm/irq.h>
40
41 /**
42  * mdiobus_alloc_size - allocate a mii_bus structure
43  * @size: extra amount of memory to allocate for private storage.
44  * If non-zero, then bus->priv is points to that memory.
45  *
46  * Description: called by a bus driver to allocate an mii_bus
47  * structure to fill in.
48  */
49 struct mii_bus *mdiobus_alloc_size(size_t size)
50 {
51         struct mii_bus *bus;
52         size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
53         size_t alloc_size;
54
55         /* If we alloc extra space, it should be aligned */
56         if (size)
57                 alloc_size = aligned_size + size;
58         else
59                 alloc_size = sizeof(*bus);
60
61         bus = kzalloc(alloc_size, GFP_KERNEL);
62         if (bus) {
63                 bus->state = MDIOBUS_ALLOCATED;
64                 if (size)
65                         bus->priv = (void *)bus + aligned_size;
66         }
67
68         return bus;
69 }
70 EXPORT_SYMBOL(mdiobus_alloc_size);
71
72 static void _devm_mdiobus_free(struct device *dev, void *res)
73 {
74         mdiobus_free(*(struct mii_bus **)res);
75 }
76
77 static int devm_mdiobus_match(struct device *dev, void *res, void *data)
78 {
79         struct mii_bus **r = res;
80
81         if (WARN_ON(!r || !*r))
82                 return 0;
83
84         return *r == data;
85 }
86
87 /**
88  * devm_mdiobus_alloc_size - Resource-managed mdiobus_alloc_size()
89  * @dev:                Device to allocate mii_bus for
90  * @sizeof_priv:        Space to allocate for private structure.
91  *
92  * Managed mdiobus_alloc_size. mii_bus allocated with this function is
93  * automatically freed on driver detach.
94  *
95  * If an mii_bus allocated with this function needs to be freed separately,
96  * devm_mdiobus_free() must be used.
97  *
98  * RETURNS:
99  * Pointer to allocated mii_bus on success, NULL on failure.
100  */
101 struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv)
102 {
103         struct mii_bus **ptr, *bus;
104
105         ptr = devres_alloc(_devm_mdiobus_free, sizeof(*ptr), GFP_KERNEL);
106         if (!ptr)
107                 return NULL;
108
109         /* use raw alloc_dr for kmalloc caller tracing */
110         bus = mdiobus_alloc_size(sizeof_priv);
111         if (bus) {
112                 *ptr = bus;
113                 devres_add(dev, ptr);
114         } else {
115                 devres_free(ptr);
116         }
117
118         return bus;
119 }
120 EXPORT_SYMBOL_GPL(devm_mdiobus_alloc_size);
121
122 /**
123  * devm_mdiobus_free - Resource-managed mdiobus_free()
124  * @dev:                Device this mii_bus belongs to
125  * @bus:                the mii_bus associated with the device
126  *
127  * Free mii_bus allocated with devm_mdiobus_alloc_size().
128  */
129 void devm_mdiobus_free(struct device *dev, struct mii_bus *bus)
130 {
131         int rc;
132
133         rc = devres_release(dev, _devm_mdiobus_free,
134                             devm_mdiobus_match, bus);
135         WARN_ON(rc);
136 }
137 EXPORT_SYMBOL_GPL(devm_mdiobus_free);
138
139 /**
140  * mdiobus_release - mii_bus device release callback
141  * @d: the target struct device that contains the mii_bus
142  *
143  * Description: called when the last reference to an mii_bus is
144  * dropped, to free the underlying memory.
145  */
146 static void mdiobus_release(struct device *d)
147 {
148         struct mii_bus *bus = to_mii_bus(d);
149         BUG_ON(bus->state != MDIOBUS_RELEASED &&
150                /* for compatibility with error handling in drivers */
151                bus->state != MDIOBUS_ALLOCATED);
152         kfree(bus);
153 }
154
155 static struct class mdio_bus_class = {
156         .name           = "mdio_bus",
157         .dev_release    = mdiobus_release,
158 };
159
160 #if IS_ENABLED(CONFIG_OF_MDIO)
161 /* Helper function for of_mdio_find_bus */
162 static int of_mdio_bus_match(struct device *dev, const void *mdio_bus_np)
163 {
164         return dev->of_node == mdio_bus_np;
165 }
166 /**
167  * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
168  * @mdio_bus_np: Pointer to the mii_bus.
169  *
170  * Returns a reference to the mii_bus, or NULL if none found.  The
171  * embedded struct device will have its reference count incremented,
172  * and this must be put once the bus is finished with.
173  *
174  * Because the association of a device_node and mii_bus is made via
175  * of_mdiobus_register(), the mii_bus cannot be found before it is
176  * registered with of_mdiobus_register().
177  *
178  */
179 struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
180 {
181         struct device *d;
182
183         if (!mdio_bus_np)
184                 return NULL;
185
186         d = class_find_device(&mdio_bus_class, NULL,  mdio_bus_np,
187                               of_mdio_bus_match);
188
189         return d ? to_mii_bus(d) : NULL;
190 }
191 EXPORT_SYMBOL(of_mdio_find_bus);
192
193 /* Walk the list of subnodes of a mdio bus and look for a node that matches the
194  * phy's address with its 'reg' property. If found, set the of_node pointer for
195  * the phy. This allows auto-probed pyh devices to be supplied with information
196  * passed in via DT.
197  */
198 static void of_mdiobus_link_phydev(struct mii_bus *mdio,
199                                    struct phy_device *phydev)
200 {
201         struct device *dev = &phydev->dev;
202         struct device_node *child;
203
204         if (dev->of_node || !mdio->dev.of_node)
205                 return;
206
207         for_each_available_child_of_node(mdio->dev.of_node, child) {
208                 int addr;
209                 int ret;
210
211                 ret = of_property_read_u32(child, "reg", &addr);
212                 if (ret < 0) {
213                         dev_err(dev, "%s has invalid PHY address\n",
214                                 child->full_name);
215                         continue;
216                 }
217
218                 /* A PHY must have a reg property in the range [0-31] */
219                 if (addr >= PHY_MAX_ADDR) {
220                         dev_err(dev, "%s PHY address %i is too large\n",
221                                 child->full_name, addr);
222                         continue;
223                 }
224
225                 if (addr == phydev->addr) {
226                         dev->of_node = child;
227                         return;
228                 }
229         }
230 }
231 #else /* !IS_ENABLED(CONFIG_OF_MDIO) */
232 static inline void of_mdiobus_link_phydev(struct mii_bus *mdio,
233                                           struct phy_device *phydev)
234 {
235 }
236 #endif
237
238 /**
239  * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
240  * @bus: target mii_bus
241  * @owner: module containing bus accessor functions
242  *
243  * Description: Called by a bus driver to bring up all the PHYs
244  *   on a given bus, and attach them to the bus. Drivers should use
245  *   mdiobus_register() rather than __mdiobus_register() unless they
246  *   need to pass a specific owner module.
247  *
248  * Returns 0 on success or < 0 on error.
249  */
250 int __mdiobus_register(struct mii_bus *bus, struct module *owner)
251 {
252         int i, err;
253
254         if (NULL == bus || NULL == bus->name ||
255             NULL == bus->read || NULL == bus->write)
256                 return -EINVAL;
257
258         BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
259                bus->state != MDIOBUS_UNREGISTERED);
260
261         bus->owner = owner;
262         bus->dev.parent = bus->parent;
263         bus->dev.class = &mdio_bus_class;
264         bus->dev.groups = NULL;
265         dev_set_name(&bus->dev, "%s", bus->id);
266
267         /* We need to set state to MDIOBUS_UNREGISTERED to correctly release
268          * the device in mdiobus_free()
269          *
270          * State will be updated later in this function in case of success
271          */
272         bus->state = MDIOBUS_UNREGISTERED;
273
274         err = device_register(&bus->dev);
275         if (err) {
276                 pr_err("mii_bus %s failed to register\n", bus->id);
277                 return -EINVAL;
278         }
279
280         mutex_init(&bus->mdio_lock);
281
282         if (bus->reset)
283                 bus->reset(bus);
284
285         for (i = 0; i < PHY_MAX_ADDR; i++) {
286                 if ((bus->phy_mask & (1 << i)) == 0) {
287                         struct phy_device *phydev;
288
289                         phydev = mdiobus_scan(bus, i);
290                         if (IS_ERR(phydev)) {
291                                 err = PTR_ERR(phydev);
292                                 goto error;
293                         }
294                 }
295         }
296
297         bus->state = MDIOBUS_REGISTERED;
298         pr_info("%s: probed\n", bus->name);
299         return 0;
300
301 error:
302         while (--i >= 0) {
303                 struct phy_device *phydev = bus->phy_map[i];
304                 if (phydev) {
305                         phy_device_remove(phydev);
306                         phy_device_free(phydev);
307                 }
308         }
309         device_del(&bus->dev);
310         return err;
311 }
312 EXPORT_SYMBOL(__mdiobus_register);
313
314 void mdiobus_unregister(struct mii_bus *bus)
315 {
316         int i;
317
318         if (WARN_ON_ONCE(bus->state != MDIOBUS_REGISTERED))
319                 return;
320         bus->state = MDIOBUS_UNREGISTERED;
321
322         for (i = 0; i < PHY_MAX_ADDR; i++) {
323                 struct phy_device *phydev = bus->phy_map[i];
324                 if (phydev) {
325                         phy_device_remove(phydev);
326                         phy_device_free(phydev);
327                 }
328         }
329         device_del(&bus->dev);
330 }
331 EXPORT_SYMBOL(mdiobus_unregister);
332
333 /**
334  * mdiobus_free - free a struct mii_bus
335  * @bus: mii_bus to free
336  *
337  * This function releases the reference to the underlying device
338  * object in the mii_bus.  If this is the last reference, the mii_bus
339  * will be freed.
340  */
341 void mdiobus_free(struct mii_bus *bus)
342 {
343         /* For compatibility with error handling in drivers. */
344         if (bus->state == MDIOBUS_ALLOCATED) {
345                 kfree(bus);
346                 return;
347         }
348
349         BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
350         bus->state = MDIOBUS_RELEASED;
351
352         put_device(&bus->dev);
353 }
354 EXPORT_SYMBOL(mdiobus_free);
355
356 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
357 {
358         struct phy_device *phydev;
359         int err;
360
361         phydev = get_phy_device(bus, addr, false);
362         if (IS_ERR(phydev) || phydev == NULL)
363                 return phydev;
364
365         /*
366          * For DT, see if the auto-probed phy has a correspoding child
367          * in the bus node, and set the of_node pointer in this case.
368          */
369         of_mdiobus_link_phydev(bus, phydev);
370
371         err = phy_device_register(phydev);
372         if (err) {
373                 phy_device_free(phydev);
374                 return NULL;
375         }
376
377         return phydev;
378 }
379 EXPORT_SYMBOL(mdiobus_scan);
380
381 /**
382  * mdiobus_read_nested - Nested version of the mdiobus_read function
383  * @bus: the mii_bus struct
384  * @addr: the phy address
385  * @regnum: register number to read
386  *
387  * In case of nested MDIO bus access avoid lockdep false positives by
388  * using mutex_lock_nested().
389  *
390  * NOTE: MUST NOT be called from interrupt context,
391  * because the bus read/write functions may wait for an interrupt
392  * to conclude the operation.
393  */
394 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum)
395 {
396         int retval;
397
398         BUG_ON(in_interrupt());
399
400         mutex_lock_nested(&bus->mdio_lock, SINGLE_DEPTH_NESTING);
401         retval = bus->read(bus, addr, regnum);
402         mutex_unlock(&bus->mdio_lock);
403
404         return retval;
405 }
406 EXPORT_SYMBOL(mdiobus_read_nested);
407
408 /**
409  * mdiobus_read - Convenience function for reading a given MII mgmt register
410  * @bus: the mii_bus struct
411  * @addr: the phy address
412  * @regnum: register number to read
413  *
414  * NOTE: MUST NOT be called from interrupt context,
415  * because the bus read/write functions may wait for an interrupt
416  * to conclude the operation.
417  */
418 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
419 {
420         int retval;
421
422         BUG_ON(in_interrupt());
423
424         mutex_lock(&bus->mdio_lock);
425         retval = bus->read(bus, addr, regnum);
426         mutex_unlock(&bus->mdio_lock);
427
428         return retval;
429 }
430 EXPORT_SYMBOL(mdiobus_read);
431
432 /**
433  * mdiobus_write_nested - Nested version of the mdiobus_write function
434  * @bus: the mii_bus struct
435  * @addr: the phy address
436  * @regnum: register number to write
437  * @val: value to write to @regnum
438  *
439  * In case of nested MDIO bus access avoid lockdep false positives by
440  * using mutex_lock_nested().
441  *
442  * NOTE: MUST NOT be called from interrupt context,
443  * because the bus read/write functions may wait for an interrupt
444  * to conclude the operation.
445  */
446 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val)
447 {
448         int err;
449
450         BUG_ON(in_interrupt());
451
452         mutex_lock_nested(&bus->mdio_lock, SINGLE_DEPTH_NESTING);
453         err = bus->write(bus, addr, regnum, val);
454         mutex_unlock(&bus->mdio_lock);
455
456         return err;
457 }
458 EXPORT_SYMBOL(mdiobus_write_nested);
459
460 /**
461  * mdiobus_write - Convenience function for writing a given MII mgmt register
462  * @bus: the mii_bus struct
463  * @addr: the phy address
464  * @regnum: register number to write
465  * @val: value to write to @regnum
466  *
467  * NOTE: MUST NOT be called from interrupt context,
468  * because the bus read/write functions may wait for an interrupt
469  * to conclude the operation.
470  */
471 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
472 {
473         int err;
474
475         BUG_ON(in_interrupt());
476
477         mutex_lock(&bus->mdio_lock);
478         err = bus->write(bus, addr, regnum, val);
479         mutex_unlock(&bus->mdio_lock);
480
481         return err;
482 }
483 EXPORT_SYMBOL(mdiobus_write);
484
485 /**
486  * mdio_bus_match - determine if given PHY driver supports the given PHY device
487  * @dev: target PHY device
488  * @drv: given PHY driver
489  *
490  * Description: Given a PHY device, and a PHY driver, return 1 if
491  *   the driver supports the device.  Otherwise, return 0.
492  */
493 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
494 {
495         struct phy_device *phydev = to_phy_device(dev);
496         struct phy_driver *phydrv = to_phy_driver(drv);
497         const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
498         int i;
499
500         if (of_driver_match_device(dev, drv))
501                 return 1;
502
503         if (phydrv->match_phy_device)
504                 return phydrv->match_phy_device(phydev);
505
506         if (phydev->is_c45) {
507                 for (i = 1; i < num_ids; i++) {
508                         if (!(phydev->c45_ids.devices_in_package & (1 << i)))
509                                 continue;
510
511                         if ((phydrv->phy_id & phydrv->phy_id_mask) ==
512                             (phydev->c45_ids.device_ids[i] &
513                              phydrv->phy_id_mask))
514                                 return 1;
515                 }
516                 return 0;
517         } else {
518                 return (phydrv->phy_id & phydrv->phy_id_mask) ==
519                         (phydev->phy_id & phydrv->phy_id_mask);
520         }
521 }
522
523 #ifdef CONFIG_PM
524
525 static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
526 {
527         struct device_driver *drv = phydev->dev.driver;
528         struct phy_driver *phydrv = to_phy_driver(drv);
529         struct net_device *netdev = phydev->attached_dev;
530
531         if (!drv || !phydrv->suspend)
532                 return false;
533
534         /* PHY not attached? May suspend if the PHY has not already been
535          * suspended as part of a prior call to phy_disconnect() ->
536          * phy_detach() -> phy_suspend() because the parent netdev might be the
537          * MDIO bus driver and clock gated at this point.
538          */
539         if (!netdev)
540                 return !phydev->suspended;
541
542         /* Don't suspend PHY if the attched netdev parent may wakeup.
543          * The parent may point to a PCI device, as in tg3 driver.
544          */
545         if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
546                 return false;
547
548         /* Also don't suspend PHY if the netdev itself may wakeup. This
549          * is the case for devices w/o underlaying pwr. mgmt. aware bus,
550          * e.g. SoC devices.
551          */
552         if (device_may_wakeup(&netdev->dev))
553                 return false;
554
555         return true;
556 }
557
558 static int mdio_bus_suspend(struct device *dev)
559 {
560         struct phy_device *phydev = to_phy_device(dev);
561
562         /* We must stop the state machine manually, otherwise it stops out of
563          * control, possibly with the phydev->lock held. Upon resume, netdev
564          * may call phy routines that try to grab the same lock, and that may
565          * lead to a deadlock.
566          */
567         if (phydev->attached_dev && phydev->adjust_link)
568                 phy_stop_machine(phydev);
569
570         if (!mdio_bus_phy_may_suspend(phydev))
571                 return 0;
572
573         return phy_suspend(phydev);
574 }
575
576 static int mdio_bus_resume(struct device *dev)
577 {
578         struct phy_device *phydev = to_phy_device(dev);
579         int ret;
580
581         if (!mdio_bus_phy_may_suspend(phydev))
582                 goto no_resume;
583
584         ret = phy_resume(phydev);
585         if (ret < 0)
586                 return ret;
587
588 no_resume:
589         if (phydev->attached_dev && phydev->adjust_link)
590                 phy_start_machine(phydev);
591
592         return 0;
593 }
594
595 static int mdio_bus_restore(struct device *dev)
596 {
597         struct phy_device *phydev = to_phy_device(dev);
598         struct net_device *netdev = phydev->attached_dev;
599         int ret;
600
601         if (!netdev)
602                 return 0;
603
604         ret = phy_init_hw(phydev);
605         if (ret < 0)
606                 return ret;
607
608         /* The PHY needs to renegotiate. */
609         phydev->link = 0;
610         phydev->state = PHY_UP;
611
612         phy_start_machine(phydev);
613
614         return 0;
615 }
616
617 static const struct dev_pm_ops mdio_bus_pm_ops = {
618         .suspend = mdio_bus_suspend,
619         .resume = mdio_bus_resume,
620         .freeze = mdio_bus_suspend,
621         .thaw = mdio_bus_resume,
622         .restore = mdio_bus_restore,
623 };
624
625 #define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops)
626
627 #else
628
629 #define MDIO_BUS_PM_OPS NULL
630
631 #endif /* CONFIG_PM */
632
633 static ssize_t
634 phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
635 {
636         struct phy_device *phydev = to_phy_device(dev);
637
638         return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
639 }
640 static DEVICE_ATTR_RO(phy_id);
641
642 static ssize_t
643 phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
644 {
645         struct phy_device *phydev = to_phy_device(dev);
646         const char *mode = NULL;
647
648         if (phy_is_internal(phydev))
649                 mode = "internal";
650         else
651                 mode = phy_modes(phydev->interface);
652
653         return sprintf(buf, "%s\n", mode);
654 }
655 static DEVICE_ATTR_RO(phy_interface);
656
657 static ssize_t
658 phy_has_fixups_show(struct device *dev, struct device_attribute *attr, char *buf)
659 {
660         struct phy_device *phydev = to_phy_device(dev);
661
662         return sprintf(buf, "%d\n", phydev->has_fixups);
663 }
664 static DEVICE_ATTR_RO(phy_has_fixups);
665
666 static struct attribute *mdio_dev_attrs[] = {
667         &dev_attr_phy_id.attr,
668         &dev_attr_phy_interface.attr,
669         &dev_attr_phy_has_fixups.attr,
670         NULL,
671 };
672 ATTRIBUTE_GROUPS(mdio_dev);
673
674 struct bus_type mdio_bus_type = {
675         .name           = "mdio_bus",
676         .match          = mdio_bus_match,
677         .pm             = MDIO_BUS_PM_OPS,
678         .dev_groups     = mdio_dev_groups,
679 };
680 EXPORT_SYMBOL(mdio_bus_type);
681
682 int __init mdio_bus_init(void)
683 {
684         int ret;
685
686         ret = class_register(&mdio_bus_class);
687         if (!ret) {
688                 ret = bus_register(&mdio_bus_type);
689                 if (ret)
690                         class_unregister(&mdio_bus_class);
691         }
692
693         return ret;
694 }
695
696 void mdio_bus_exit(void)
697 {
698         class_unregister(&mdio_bus_class);
699         bus_unregister(&mdio_bus_type);
700 }