net: dsa: fix of_mdio_find_bus() device refcount leak
[deliverable/linux.git] / drivers / net / phy / mdio_bus.c
CommitLineData
02d320c3 1/* MDIO Bus interface
00db8189
AF
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 */
8d242488
JP
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
00db8189 16#include <linux/kernel.h>
00db8189
AF
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>
3d1e4db2 24#include <linux/device.h>
a30e2c18 25#include <linux/of_device.h>
4085a7f0 26#include <linux/of_mdio.h>
00db8189
AF
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>
00db8189
AF
33#include <linux/mii.h>
34#include <linux/ethtool.h>
35#include <linux/phy.h>
02d320c3
SS
36#include <linux/io.h>
37#include <linux/uaccess.h>
00db8189 38
00db8189 39#include <asm/irq.h>
00db8189 40
298cf9be 41/**
eb8a54a7 42 * mdiobus_alloc_size - allocate a mii_bus structure
af58f1d6
RD
43 * @size: extra amount of memory to allocate for private storage.
44 * If non-zero, then bus->priv is points to that memory.
298cf9be
LB
45 *
46 * Description: called by a bus driver to allocate an mii_bus
47 * structure to fill in.
48 */
eb8a54a7 49struct mii_bus *mdiobus_alloc_size(size_t size)
298cf9be 50{
46abc021 51 struct mii_bus *bus;
eb8a54a7
TT
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);
46abc021 60
eb8a54a7
TT
61 bus = kzalloc(alloc_size, GFP_KERNEL);
62 if (bus) {
46abc021 63 bus->state = MDIOBUS_ALLOCATED;
eb8a54a7
TT
64 if (size)
65 bus->priv = (void *)bus + aligned_size;
66 }
46abc021
LB
67
68 return bus;
298cf9be 69}
eb8a54a7 70EXPORT_SYMBOL(mdiobus_alloc_size);
298cf9be 71
6d48f44b
GS
72static void _devm_mdiobus_free(struct device *dev, void *res)
73{
74 mdiobus_free(*(struct mii_bus **)res);
75}
76
77static 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 */
101struct 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}
93dccc59 120EXPORT_SYMBOL_GPL(devm_mdiobus_alloc_size);
6d48f44b
GS
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 */
129void 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}
137EXPORT_SYMBOL_GPL(devm_mdiobus_free);
138
46abc021
LB
139/**
140 * mdiobus_release - mii_bus device release callback
78c36b15 141 * @d: the target struct device that contains the mii_bus
46abc021
LB
142 *
143 * Description: called when the last reference to an mii_bus is
144 * dropped, to free the underlying memory.
145 */
146static void mdiobus_release(struct device *d)
147{
148 struct mii_bus *bus = to_mii_bus(d);
161c8d2f
KH
149 BUG_ON(bus->state != MDIOBUS_RELEASED &&
150 /* for compatibility with error handling in drivers */
151 bus->state != MDIOBUS_ALLOCATED);
46abc021
LB
152 kfree(bus);
153}
154
155static struct class mdio_bus_class = {
156 .name = "mdio_bus",
157 .dev_release = mdiobus_release,
158};
159
b943fbb0 160#if IS_ENABLED(CONFIG_OF_MDIO)
25106022 161/* Helper function for of_mdio_find_bus */
9f3b795a 162static int of_mdio_bus_match(struct device *dev, const void *mdio_bus_np)
25106022
DD
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.
f41ef2e7 168 * @mdio_bus_np: Pointer to the mii_bus.
25106022 169 *
a1364421
RK
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.
25106022
DD
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 */
179struct 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}
191EXPORT_SYMBOL(of_mdio_find_bus);
d9daa247
DM
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 */
198static 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) */
232static inline void of_mdiobus_link_phydev(struct mii_bus *mdio,
233 struct phy_device *phydev)
234{
235}
25106022
DD
236#endif
237
b3df0da8
RD
238/**
239 * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
240 * @bus: target mii_bus
e1393456 241 *
b3df0da8
RD
242 * Description: Called by a bus driver to bring up all the PHYs
243 * on a given bus, and attach them to the bus.
244 *
245 * Returns 0 on success or < 0 on error.
e1393456
AF
246 */
247int mdiobus_register(struct mii_bus *bus)
248{
161c8d2f 249 int i, err;
e1393456 250
e1393456 251 if (NULL == bus || NULL == bus->name ||
02d320c3 252 NULL == bus->read || NULL == bus->write)
e1393456
AF
253 return -EINVAL;
254
46abc021
LB
255 BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
256 bus->state != MDIOBUS_UNREGISTERED);
257
258 bus->dev.parent = bus->parent;
259 bus->dev.class = &mdio_bus_class;
260 bus->dev.groups = NULL;
036b6687 261 dev_set_name(&bus->dev, "%s", bus->id);
46abc021
LB
262
263 err = device_register(&bus->dev);
264 if (err) {
8d242488 265 pr_err("mii_bus %s failed to register\n", bus->id);
0c692d07 266 put_device(&bus->dev);
46abc021
LB
267 return -EINVAL;
268 }
269
d1e7fe4d
AB
270 mutex_init(&bus->mdio_lock);
271
e1393456
AF
272 if (bus->reset)
273 bus->reset(bus);
274
275 for (i = 0; i < PHY_MAX_ADDR; i++) {
4fd5f812
LB
276 if ((bus->phy_mask & (1 << i)) == 0) {
277 struct phy_device *phydev;
e1393456 278
4fd5f812 279 phydev = mdiobus_scan(bus, i);
161c8d2f 280 if (IS_ERR(phydev)) {
4fd5f812 281 err = PTR_ERR(phydev);
161c8d2f
KH
282 goto error;
283 }
64b1c2b4 284 }
e1393456
AF
285 }
286
161c8d2f 287 bus->state = MDIOBUS_REGISTERED;
e1393456 288 pr_info("%s: probed\n", bus->name);
161c8d2f 289 return 0;
e1393456 290
161c8d2f
KH
291error:
292 while (--i >= 0) {
293 if (bus->phy_map[i])
294 device_unregister(&bus->phy_map[i]->dev);
295 }
296 device_del(&bus->dev);
e1393456
AF
297 return err;
298}
299EXPORT_SYMBOL(mdiobus_register);
300
301void mdiobus_unregister(struct mii_bus *bus)
302{
303 int i;
304
46abc021
LB
305 BUG_ON(bus->state != MDIOBUS_REGISTERED);
306 bus->state = MDIOBUS_UNREGISTERED;
307
e1393456 308 for (i = 0; i < PHY_MAX_ADDR; i++) {
6f4a7f41 309 if (bus->phy_map[i])
e1393456 310 device_unregister(&bus->phy_map[i]->dev);
4dea547f 311 bus->phy_map[i] = NULL;
e1393456 312 }
b6c6aedc 313 device_del(&bus->dev);
e1393456
AF
314}
315EXPORT_SYMBOL(mdiobus_unregister);
316
298cf9be
LB
317/**
318 * mdiobus_free - free a struct mii_bus
319 * @bus: mii_bus to free
320 *
46abc021
LB
321 * This function releases the reference to the underlying device
322 * object in the mii_bus. If this is the last reference, the mii_bus
323 * will be freed.
298cf9be
LB
324 */
325void mdiobus_free(struct mii_bus *bus)
326{
02d320c3 327 /* For compatibility with error handling in drivers. */
46abc021
LB
328 if (bus->state == MDIOBUS_ALLOCATED) {
329 kfree(bus);
330 return;
331 }
332
333 BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
334 bus->state = MDIOBUS_RELEASED;
335
336 put_device(&bus->dev);
298cf9be
LB
337}
338EXPORT_SYMBOL(mdiobus_free);
339
4fd5f812
LB
340struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
341{
342 struct phy_device *phydev;
343 int err;
344
ac28b9f8 345 phydev = get_phy_device(bus, addr, false);
4fd5f812
LB
346 if (IS_ERR(phydev) || phydev == NULL)
347 return phydev;
348
86f6cf41
DM
349 /*
350 * For DT, see if the auto-probed phy has a correspoding child
351 * in the bus node, and set the of_node pointer in this case.
352 */
353 of_mdiobus_link_phydev(bus, phydev);
354
4dea547f 355 err = phy_device_register(phydev);
4fd5f812 356 if (err) {
4fd5f812 357 phy_device_free(phydev);
4dea547f 358 return NULL;
4fd5f812
LB
359 }
360
4fd5f812
LB
361 return phydev;
362}
363EXPORT_SYMBOL(mdiobus_scan);
364
2e888103
LB
365/**
366 * mdiobus_read - Convenience function for reading a given MII mgmt register
367 * @bus: the mii_bus struct
368 * @addr: the phy address
369 * @regnum: register number to read
370 *
371 * NOTE: MUST NOT be called from interrupt context,
372 * because the bus read/write functions may wait for an interrupt
373 * to conclude the operation.
374 */
abf35df2 375int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
2e888103
LB
376{
377 int retval;
378
379 BUG_ON(in_interrupt());
380
381 mutex_lock(&bus->mdio_lock);
382 retval = bus->read(bus, addr, regnum);
383 mutex_unlock(&bus->mdio_lock);
384
385 return retval;
386}
387EXPORT_SYMBOL(mdiobus_read);
388
389/**
390 * mdiobus_write - Convenience function for writing a given MII mgmt register
391 * @bus: the mii_bus struct
392 * @addr: the phy address
393 * @regnum: register number to write
394 * @val: value to write to @regnum
395 *
396 * NOTE: MUST NOT be called from interrupt context,
397 * because the bus read/write functions may wait for an interrupt
398 * to conclude the operation.
399 */
abf35df2 400int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
2e888103
LB
401{
402 int err;
403
404 BUG_ON(in_interrupt());
405
406 mutex_lock(&bus->mdio_lock);
407 err = bus->write(bus, addr, regnum, val);
408 mutex_unlock(&bus->mdio_lock);
409
410 return err;
411}
412EXPORT_SYMBOL(mdiobus_write);
413
b3df0da8
RD
414/**
415 * mdio_bus_match - determine if given PHY driver supports the given PHY device
416 * @dev: target PHY device
417 * @drv: given PHY driver
00db8189 418 *
b3df0da8
RD
419 * Description: Given a PHY device, and a PHY driver, return 1 if
420 * the driver supports the device. Otherwise, return 0.
00db8189
AF
421 */
422static int mdio_bus_match(struct device *dev, struct device_driver *drv)
423{
424 struct phy_device *phydev = to_phy_device(dev);
425 struct phy_driver *phydrv = to_phy_driver(drv);
e0536cd9
SX
426 const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
427 int i;
00db8189 428
a30e2c18
DD
429 if (of_driver_match_device(dev, drv))
430 return 1;
431
432 if (phydrv->match_phy_device)
433 return phydrv->match_phy_device(phydev);
434
e0536cd9
SX
435 if (phydev->is_c45) {
436 for (i = 1; i < num_ids; i++) {
437 if (!(phydev->c45_ids.devices_in_package & (1 << i)))
438 continue;
439
440 if ((phydrv->phy_id & phydrv->phy_id_mask) ==
441 (phydev->c45_ids.device_ids[i] &
442 phydrv->phy_id_mask))
443 return 1;
444 }
445 return 0;
446 } else {
447 return (phydrv->phy_id & phydrv->phy_id_mask) ==
448 (phydev->phy_id & phydrv->phy_id_mask);
449 }
00db8189
AF
450}
451
2f5cb434
AV
452#ifdef CONFIG_PM
453
3d1e4db2
AV
454static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
455{
456 struct device_driver *drv = phydev->dev.driver;
457 struct phy_driver *phydrv = to_phy_driver(drv);
458 struct net_device *netdev = phydev->attached_dev;
459
460 if (!drv || !phydrv->suspend)
461 return false;
462
803dd9c7
FF
463 /* PHY not attached? May suspend if the PHY has not already been
464 * suspended as part of a prior call to phy_disconnect() ->
465 * phy_detach() -> phy_suspend() because the parent netdev might be the
466 * MDIO bus driver and clock gated at this point.
467 */
3d1e4db2 468 if (!netdev)
803dd9c7 469 return !phydev->suspended;
3d1e4db2 470
02d320c3 471 /* Don't suspend PHY if the attched netdev parent may wakeup.
3d1e4db2
AV
472 * The parent may point to a PCI device, as in tg3 driver.
473 */
474 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
475 return false;
476
02d320c3 477 /* Also don't suspend PHY if the netdev itself may wakeup. This
3d1e4db2
AV
478 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
479 * e.g. SoC devices.
480 */
481 if (device_may_wakeup(&netdev->dev))
482 return false;
483
484 return true;
485}
486
2f5cb434 487static int mdio_bus_suspend(struct device *dev)
00db8189 488{
0f0ca340 489 struct phy_device *phydev = to_phy_device(dev);
00db8189 490
02d320c3 491 /* We must stop the state machine manually, otherwise it stops out of
541cd3ee
AV
492 * control, possibly with the phydev->lock held. Upon resume, netdev
493 * may call phy routines that try to grab the same lock, and that may
494 * lead to a deadlock.
495 */
fddd9101 496 if (phydev->attached_dev && phydev->adjust_link)
541cd3ee
AV
497 phy_stop_machine(phydev);
498
3d1e4db2
AV
499 if (!mdio_bus_phy_may_suspend(phydev))
500 return 0;
541cd3ee 501
9272efa2 502 return phy_suspend(phydev);
00db8189
AF
503}
504
2f5cb434 505static int mdio_bus_resume(struct device *dev)
00db8189 506{
0f0ca340 507 struct phy_device *phydev = to_phy_device(dev);
541cd3ee 508 int ret;
00db8189 509
3d1e4db2 510 if (!mdio_bus_phy_may_suspend(phydev))
541cd3ee
AV
511 goto no_resume;
512
9272efa2 513 ret = phy_resume(phydev);
541cd3ee
AV
514 if (ret < 0)
515 return ret;
516
517no_resume:
fddd9101 518 if (phydev->attached_dev && phydev->adjust_link)
29935aeb 519 phy_start_machine(phydev);
541cd3ee
AV
520
521 return 0;
00db8189
AF
522}
523
2f5cb434
AV
524static int mdio_bus_restore(struct device *dev)
525{
526 struct phy_device *phydev = to_phy_device(dev);
527 struct net_device *netdev = phydev->attached_dev;
528 int ret;
529
530 if (!netdev)
531 return 0;
532
533 ret = phy_init_hw(phydev);
534 if (ret < 0)
535 return ret;
536
537 /* The PHY needs to renegotiate. */
538 phydev->link = 0;
539 phydev->state = PHY_UP;
540
29935aeb 541 phy_start_machine(phydev);
2f5cb434
AV
542
543 return 0;
544}
545
02d320c3 546static const struct dev_pm_ops mdio_bus_pm_ops = {
2f5cb434
AV
547 .suspend = mdio_bus_suspend,
548 .resume = mdio_bus_resume,
549 .freeze = mdio_bus_suspend,
550 .thaw = mdio_bus_resume,
551 .restore = mdio_bus_restore,
552};
553
554#define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops)
555
556#else
557
558#define MDIO_BUS_PM_OPS NULL
559
560#endif /* CONFIG_PM */
561
56277f40
NB
562static ssize_t
563phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
564{
565 struct phy_device *phydev = to_phy_device(dev);
566
567 return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
568}
4192c749 569static DEVICE_ATTR_RO(phy_id);
56277f40 570
3d055d8d
FF
571static ssize_t
572phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
573{
574 struct phy_device *phydev = to_phy_device(dev);
574746dd 575 const char *mode = NULL;
3d055d8d 576
574746dd
FF
577 if (phy_is_internal(phydev))
578 mode = "internal";
579 else
580 mode = phy_modes(phydev->interface);
581
582 return sprintf(buf, "%s\n", mode);
3d055d8d
FF
583}
584static DEVICE_ATTR_RO(phy_interface);
585
8bed1285
FF
586static ssize_t
587phy_has_fixups_show(struct device *dev, struct device_attribute *attr, char *buf)
588{
589 struct phy_device *phydev = to_phy_device(dev);
590
591 return sprintf(buf, "%d\n", phydev->has_fixups);
592}
593static DEVICE_ATTR_RO(phy_has_fixups);
594
4192c749
GKH
595static struct attribute *mdio_dev_attrs[] = {
596 &dev_attr_phy_id.attr,
3d055d8d 597 &dev_attr_phy_interface.attr,
8bed1285 598 &dev_attr_phy_has_fixups.attr,
4192c749 599 NULL,
56277f40 600};
4192c749 601ATTRIBUTE_GROUPS(mdio_dev);
56277f40 602
00db8189
AF
603struct bus_type mdio_bus_type = {
604 .name = "mdio_bus",
605 .match = mdio_bus_match,
2f5cb434 606 .pm = MDIO_BUS_PM_OPS,
4192c749 607 .dev_groups = mdio_dev_groups,
00db8189 608};
11b0bacd 609EXPORT_SYMBOL(mdio_bus_type);
00db8189 610
67c4f3fa 611int __init mdio_bus_init(void)
00db8189 612{
46abc021
LB
613 int ret;
614
615 ret = class_register(&mdio_bus_class);
616 if (!ret) {
617 ret = bus_register(&mdio_bus_type);
618 if (ret)
619 class_unregister(&mdio_bus_class);
620 }
621
622 return ret;
00db8189
AF
623}
624
dc85dec6 625void mdio_bus_exit(void)
e1393456 626{
46abc021 627 class_unregister(&mdio_bus_class);
e1393456
AF
628 bus_unregister(&mdio_bus_type);
629}
This page took 1.138952 seconds and 5 git commands to generate.