mdio_bus: fix devm_mdiobus_alloc_size export
[firefly-linux-kernel-4.4.55.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 pointer to the mii_bus, or NULL if none found.
171  *
172  * Because the association of a device_node and mii_bus is made via
173  * of_mdiobus_register(), the mii_bus cannot be found before it is
174  * registered with of_mdiobus_register().
175  *
176  */
177 struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
178 {
179         struct device *d;
180
181         if (!mdio_bus_np)
182                 return NULL;
183
184         d = class_find_device(&mdio_bus_class, NULL,  mdio_bus_np,
185                               of_mdio_bus_match);
186
187         return d ? to_mii_bus(d) : NULL;
188 }
189 EXPORT_SYMBOL(of_mdio_find_bus);
190 #endif
191
192 /**
193  * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
194  * @bus: target mii_bus
195  *
196  * Description: Called by a bus driver to bring up all the PHYs
197  *   on a given bus, and attach them to the bus.
198  *
199  * Returns 0 on success or < 0 on error.
200  */
201 int mdiobus_register(struct mii_bus *bus)
202 {
203         int i, err;
204
205         if (NULL == bus || NULL == bus->name ||
206             NULL == bus->read || NULL == bus->write)
207                 return -EINVAL;
208
209         BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
210                bus->state != MDIOBUS_UNREGISTERED);
211
212         bus->dev.parent = bus->parent;
213         bus->dev.class = &mdio_bus_class;
214         bus->dev.groups = NULL;
215         dev_set_name(&bus->dev, "%s", bus->id);
216
217         err = device_register(&bus->dev);
218         if (err) {
219                 pr_err("mii_bus %s failed to register\n", bus->id);
220                 put_device(&bus->dev);
221                 return -EINVAL;
222         }
223
224         mutex_init(&bus->mdio_lock);
225
226         if (bus->reset)
227                 bus->reset(bus);
228
229         for (i = 0; i < PHY_MAX_ADDR; i++) {
230                 if ((bus->phy_mask & (1 << i)) == 0) {
231                         struct phy_device *phydev;
232
233                         phydev = mdiobus_scan(bus, i);
234                         if (IS_ERR(phydev)) {
235                                 err = PTR_ERR(phydev);
236                                 goto error;
237                         }
238                 }
239         }
240
241         bus->state = MDIOBUS_REGISTERED;
242         pr_info("%s: probed\n", bus->name);
243         return 0;
244
245 error:
246         while (--i >= 0) {
247                 if (bus->phy_map[i])
248                         device_unregister(&bus->phy_map[i]->dev);
249         }
250         device_del(&bus->dev);
251         return err;
252 }
253 EXPORT_SYMBOL(mdiobus_register);
254
255 void mdiobus_unregister(struct mii_bus *bus)
256 {
257         int i;
258
259         BUG_ON(bus->state != MDIOBUS_REGISTERED);
260         bus->state = MDIOBUS_UNREGISTERED;
261
262         device_del(&bus->dev);
263         for (i = 0; i < PHY_MAX_ADDR; i++) {
264                 if (bus->phy_map[i])
265                         device_unregister(&bus->phy_map[i]->dev);
266                 bus->phy_map[i] = NULL;
267         }
268 }
269 EXPORT_SYMBOL(mdiobus_unregister);
270
271 /**
272  * mdiobus_free - free a struct mii_bus
273  * @bus: mii_bus to free
274  *
275  * This function releases the reference to the underlying device
276  * object in the mii_bus.  If this is the last reference, the mii_bus
277  * will be freed.
278  */
279 void mdiobus_free(struct mii_bus *bus)
280 {
281         /* For compatibility with error handling in drivers. */
282         if (bus->state == MDIOBUS_ALLOCATED) {
283                 kfree(bus);
284                 return;
285         }
286
287         BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
288         bus->state = MDIOBUS_RELEASED;
289
290         put_device(&bus->dev);
291 }
292 EXPORT_SYMBOL(mdiobus_free);
293
294 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
295 {
296         struct phy_device *phydev;
297         int err;
298
299         phydev = get_phy_device(bus, addr, false);
300         if (IS_ERR(phydev) || phydev == NULL)
301                 return phydev;
302
303         err = phy_device_register(phydev);
304         if (err) {
305                 phy_device_free(phydev);
306                 return NULL;
307         }
308
309         return phydev;
310 }
311 EXPORT_SYMBOL(mdiobus_scan);
312
313 /**
314  * mdiobus_read - Convenience function for reading a given MII mgmt register
315  * @bus: the mii_bus struct
316  * @addr: the phy address
317  * @regnum: register number to read
318  *
319  * NOTE: MUST NOT be called from interrupt context,
320  * because the bus read/write functions may wait for an interrupt
321  * to conclude the operation.
322  */
323 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
324 {
325         int retval;
326
327         BUG_ON(in_interrupt());
328
329         mutex_lock(&bus->mdio_lock);
330         retval = bus->read(bus, addr, regnum);
331         mutex_unlock(&bus->mdio_lock);
332
333         return retval;
334 }
335 EXPORT_SYMBOL(mdiobus_read);
336
337 /**
338  * mdiobus_write - Convenience function for writing a given MII mgmt register
339  * @bus: the mii_bus struct
340  * @addr: the phy address
341  * @regnum: register number to write
342  * @val: value to write to @regnum
343  *
344  * NOTE: MUST NOT be called from interrupt context,
345  * because the bus read/write functions may wait for an interrupt
346  * to conclude the operation.
347  */
348 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
349 {
350         int err;
351
352         BUG_ON(in_interrupt());
353
354         mutex_lock(&bus->mdio_lock);
355         err = bus->write(bus, addr, regnum, val);
356         mutex_unlock(&bus->mdio_lock);
357
358         return err;
359 }
360 EXPORT_SYMBOL(mdiobus_write);
361
362 /**
363  * mdio_bus_match - determine if given PHY driver supports the given PHY device
364  * @dev: target PHY device
365  * @drv: given PHY driver
366  *
367  * Description: Given a PHY device, and a PHY driver, return 1 if
368  *   the driver supports the device.  Otherwise, return 0.
369  */
370 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
371 {
372         struct phy_device *phydev = to_phy_device(dev);
373         struct phy_driver *phydrv = to_phy_driver(drv);
374
375         if (of_driver_match_device(dev, drv))
376                 return 1;
377
378         if (phydrv->match_phy_device)
379                 return phydrv->match_phy_device(phydev);
380
381         return (phydrv->phy_id & phydrv->phy_id_mask) ==
382                 (phydev->phy_id & phydrv->phy_id_mask);
383 }
384
385 #ifdef CONFIG_PM
386
387 static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
388 {
389         struct device_driver *drv = phydev->dev.driver;
390         struct phy_driver *phydrv = to_phy_driver(drv);
391         struct net_device *netdev = phydev->attached_dev;
392
393         if (!drv || !phydrv->suspend)
394                 return false;
395
396         /* PHY not attached? May suspend. */
397         if (!netdev)
398                 return true;
399
400         /* Don't suspend PHY if the attched netdev parent may wakeup.
401          * The parent may point to a PCI device, as in tg3 driver.
402          */
403         if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
404                 return false;
405
406         /* Also don't suspend PHY if the netdev itself may wakeup. This
407          * is the case for devices w/o underlaying pwr. mgmt. aware bus,
408          * e.g. SoC devices.
409          */
410         if (device_may_wakeup(&netdev->dev))
411                 return false;
412
413         return true;
414 }
415
416 static int mdio_bus_suspend(struct device *dev)
417 {
418         struct phy_driver *phydrv = to_phy_driver(dev->driver);
419         struct phy_device *phydev = to_phy_device(dev);
420
421         /* We must stop the state machine manually, otherwise it stops out of
422          * control, possibly with the phydev->lock held. Upon resume, netdev
423          * may call phy routines that try to grab the same lock, and that may
424          * lead to a deadlock.
425          */
426         if (phydev->attached_dev && phydev->adjust_link)
427                 phy_stop_machine(phydev);
428
429         if (!mdio_bus_phy_may_suspend(phydev))
430                 return 0;
431
432         return phydrv->suspend(phydev);
433 }
434
435 static int mdio_bus_resume(struct device *dev)
436 {
437         struct phy_driver *phydrv = to_phy_driver(dev->driver);
438         struct phy_device *phydev = to_phy_device(dev);
439         int ret;
440
441         if (!mdio_bus_phy_may_suspend(phydev))
442                 goto no_resume;
443
444         ret = phydrv->resume(phydev);
445         if (ret < 0)
446                 return ret;
447
448 no_resume:
449         if (phydev->attached_dev && phydev->adjust_link)
450                 phy_start_machine(phydev);
451
452         return 0;
453 }
454
455 static int mdio_bus_restore(struct device *dev)
456 {
457         struct phy_device *phydev = to_phy_device(dev);
458         struct net_device *netdev = phydev->attached_dev;
459         int ret;
460
461         if (!netdev)
462                 return 0;
463
464         ret = phy_init_hw(phydev);
465         if (ret < 0)
466                 return ret;
467
468         /* The PHY needs to renegotiate. */
469         phydev->link = 0;
470         phydev->state = PHY_UP;
471
472         phy_start_machine(phydev);
473
474         return 0;
475 }
476
477 static const struct dev_pm_ops mdio_bus_pm_ops = {
478         .suspend = mdio_bus_suspend,
479         .resume = mdio_bus_resume,
480         .freeze = mdio_bus_suspend,
481         .thaw = mdio_bus_resume,
482         .restore = mdio_bus_restore,
483 };
484
485 #define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops)
486
487 #else
488
489 #define MDIO_BUS_PM_OPS NULL
490
491 #endif /* CONFIG_PM */
492
493 static ssize_t
494 phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
495 {
496         struct phy_device *phydev = to_phy_device(dev);
497
498         return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
499 }
500 static DEVICE_ATTR_RO(phy_id);
501
502 static ssize_t
503 phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
504 {
505         struct phy_device *phydev = to_phy_device(dev);
506
507         return sprintf(buf, "%s\n", phy_modes(phydev->interface));
508 }
509 static DEVICE_ATTR_RO(phy_interface);
510
511 static ssize_t
512 phy_has_fixups_show(struct device *dev, struct device_attribute *attr, char *buf)
513 {
514         struct phy_device *phydev = to_phy_device(dev);
515
516         return sprintf(buf, "%d\n", phydev->has_fixups);
517 }
518 static DEVICE_ATTR_RO(phy_has_fixups);
519
520 static struct attribute *mdio_dev_attrs[] = {
521         &dev_attr_phy_id.attr,
522         &dev_attr_phy_interface.attr,
523         &dev_attr_phy_has_fixups.attr,
524         NULL,
525 };
526 ATTRIBUTE_GROUPS(mdio_dev);
527
528 struct bus_type mdio_bus_type = {
529         .name           = "mdio_bus",
530         .match          = mdio_bus_match,
531         .pm             = MDIO_BUS_PM_OPS,
532         .dev_groups     = mdio_dev_groups,
533 };
534 EXPORT_SYMBOL(mdio_bus_type);
535
536 int __init mdio_bus_init(void)
537 {
538         int ret;
539
540         ret = class_register(&mdio_bus_class);
541         if (!ret) {
542                 ret = bus_register(&mdio_bus_type);
543                 if (ret)
544                         class_unregister(&mdio_bus_class);
545         }
546
547         return ret;
548 }
549
550 void mdio_bus_exit(void)
551 {
552         class_unregister(&mdio_bus_class);
553         bus_unregister(&mdio_bus_type);
554 }