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