xref: /linux/drivers/net/phy/phy_device.c (revision 1a9239bb4253f9076b5b4b2a1a4e8d7defd77a95)
1 // SPDX-License-Identifier: GPL-2.0+
2 /* Framework for finding and configuring PHYs.
3  * Also contains generic PHY driver
4  *
5  * Author: Andy Fleming
6  *
7  * Copyright (c) 2004 Freescale Semiconductor, Inc.
8  */
9 
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 
12 #include <linux/acpi.h>
13 #include <linux/bitmap.h>
14 #include <linux/delay.h>
15 #include <linux/errno.h>
16 #include <linux/etherdevice.h>
17 #include <linux/ethtool.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/io.h>
21 #include <linux/kernel.h>
22 #include <linux/list.h>
23 #include <linux/mdio.h>
24 #include <linux/mii.h>
25 #include <linux/mm.h>
26 #include <linux/module.h>
27 #include <linux/of.h>
28 #include <linux/netdevice.h>
29 #include <linux/phy.h>
30 #include <linux/phylib_stubs.h>
31 #include <linux/phy_led_triggers.h>
32 #include <linux/phy_link_topology.h>
33 #include <linux/pse-pd/pse.h>
34 #include <linux/property.h>
35 #include <linux/ptp_clock_kernel.h>
36 #include <linux/rtnetlink.h>
37 #include <linux/sfp.h>
38 #include <linux/skbuff.h>
39 #include <linux/slab.h>
40 #include <linux/string.h>
41 #include <linux/uaccess.h>
42 #include <linux/unistd.h>
43 
44 #include "phylib-internal.h"
45 #include "phy-caps.h"
46 
47 MODULE_DESCRIPTION("PHY library");
48 MODULE_AUTHOR("Andy Fleming");
49 MODULE_LICENSE("GPL");
50 
51 #define	PHY_ANY_ID	"MATCH ANY PHY"
52 #define	PHY_ANY_UID	0xffffffff
53 
54 struct phy_fixup {
55 	struct list_head list;
56 	char bus_id[MII_BUS_ID_SIZE + 3];
57 	u32 phy_uid;
58 	u32 phy_uid_mask;
59 	int (*run)(struct phy_device *phydev);
60 };
61 
62 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
63 EXPORT_SYMBOL_GPL(phy_basic_features);
64 
65 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
66 EXPORT_SYMBOL_GPL(phy_basic_t1_features);
67 
68 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1s_p2mp_features) __ro_after_init;
69 EXPORT_SYMBOL_GPL(phy_basic_t1s_p2mp_features);
70 
71 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
72 EXPORT_SYMBOL_GPL(phy_gbit_features);
73 
74 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
75 EXPORT_SYMBOL_GPL(phy_gbit_fibre_features);
76 
77 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
78 EXPORT_SYMBOL_GPL(phy_10gbit_features);
79 
80 const int phy_basic_ports_array[3] = {
81 	ETHTOOL_LINK_MODE_Autoneg_BIT,
82 	ETHTOOL_LINK_MODE_TP_BIT,
83 	ETHTOOL_LINK_MODE_MII_BIT,
84 };
85 EXPORT_SYMBOL_GPL(phy_basic_ports_array);
86 
87 static const int phy_all_ports_features_array[7] = {
88 	ETHTOOL_LINK_MODE_Autoneg_BIT,
89 	ETHTOOL_LINK_MODE_TP_BIT,
90 	ETHTOOL_LINK_MODE_MII_BIT,
91 	ETHTOOL_LINK_MODE_FIBRE_BIT,
92 	ETHTOOL_LINK_MODE_AUI_BIT,
93 	ETHTOOL_LINK_MODE_BNC_BIT,
94 	ETHTOOL_LINK_MODE_Backplane_BIT,
95 };
96 
97 static const int phy_10_100_features_array[4] = {
98 	ETHTOOL_LINK_MODE_10baseT_Half_BIT,
99 	ETHTOOL_LINK_MODE_10baseT_Full_BIT,
100 	ETHTOOL_LINK_MODE_100baseT_Half_BIT,
101 	ETHTOOL_LINK_MODE_100baseT_Full_BIT,
102 };
103 
104 static const int phy_basic_t1_features_array[3] = {
105 	ETHTOOL_LINK_MODE_TP_BIT,
106 	ETHTOOL_LINK_MODE_10baseT1L_Full_BIT,
107 	ETHTOOL_LINK_MODE_100baseT1_Full_BIT,
108 };
109 
110 static const int phy_basic_t1s_p2mp_features_array[2] = {
111 	ETHTOOL_LINK_MODE_TP_BIT,
112 	ETHTOOL_LINK_MODE_10baseT1S_P2MP_Half_BIT,
113 };
114 
115 static const int phy_gbit_features_array[2] = {
116 	ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
117 	ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
118 };
119 
120 static const int phy_eee_cap1_features_array[] = {
121 	ETHTOOL_LINK_MODE_100baseT_Full_BIT,
122 	ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
123 	ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
124 	ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
125 	ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
126 	ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
127 };
128 
129 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_eee_cap1_features) __ro_after_init;
130 EXPORT_SYMBOL_GPL(phy_eee_cap1_features);
131 
132 static const int phy_eee_cap2_features_array[] = {
133 	ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
134 	ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
135 };
136 
137 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_eee_cap2_features) __ro_after_init;
138 EXPORT_SYMBOL_GPL(phy_eee_cap2_features);
139 
features_init(void)140 static void features_init(void)
141 {
142 	/* 10/100 half/full*/
143 	linkmode_set_bit_array(phy_basic_ports_array,
144 			       ARRAY_SIZE(phy_basic_ports_array),
145 			       phy_basic_features);
146 	linkmode_set_bit_array(phy_10_100_features_array,
147 			       ARRAY_SIZE(phy_10_100_features_array),
148 			       phy_basic_features);
149 
150 	/* 100 full, TP */
151 	linkmode_set_bit_array(phy_basic_t1_features_array,
152 			       ARRAY_SIZE(phy_basic_t1_features_array),
153 			       phy_basic_t1_features);
154 
155 	/* 10 half, P2MP, TP */
156 	linkmode_set_bit_array(phy_basic_t1s_p2mp_features_array,
157 			       ARRAY_SIZE(phy_basic_t1s_p2mp_features_array),
158 			       phy_basic_t1s_p2mp_features);
159 
160 	/* 10/100 half/full + 1000 half/full */
161 	linkmode_set_bit_array(phy_basic_ports_array,
162 			       ARRAY_SIZE(phy_basic_ports_array),
163 			       phy_gbit_features);
164 	linkmode_set_bit_array(phy_10_100_features_array,
165 			       ARRAY_SIZE(phy_10_100_features_array),
166 			       phy_gbit_features);
167 	linkmode_set_bit_array(phy_gbit_features_array,
168 			       ARRAY_SIZE(phy_gbit_features_array),
169 			       phy_gbit_features);
170 
171 	/* 10/100 half/full + 1000 half/full + fibre*/
172 	linkmode_set_bit_array(phy_basic_ports_array,
173 			       ARRAY_SIZE(phy_basic_ports_array),
174 			       phy_gbit_fibre_features);
175 	linkmode_set_bit_array(phy_10_100_features_array,
176 			       ARRAY_SIZE(phy_10_100_features_array),
177 			       phy_gbit_fibre_features);
178 	linkmode_set_bit_array(phy_gbit_features_array,
179 			       ARRAY_SIZE(phy_gbit_features_array),
180 			       phy_gbit_fibre_features);
181 	linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, phy_gbit_fibre_features);
182 
183 	/* 10/100 half/full + 1000 half/full + 10G full*/
184 	linkmode_set_bit_array(phy_all_ports_features_array,
185 			       ARRAY_SIZE(phy_all_ports_features_array),
186 			       phy_10gbit_features);
187 	linkmode_set_bit_array(phy_10_100_features_array,
188 			       ARRAY_SIZE(phy_10_100_features_array),
189 			       phy_10gbit_features);
190 	linkmode_set_bit_array(phy_gbit_features_array,
191 			       ARRAY_SIZE(phy_gbit_features_array),
192 			       phy_10gbit_features);
193 	linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
194 			 phy_10gbit_features);
195 
196 	linkmode_set_bit_array(phy_eee_cap1_features_array,
197 			       ARRAY_SIZE(phy_eee_cap1_features_array),
198 			       phy_eee_cap1_features);
199 	linkmode_set_bit_array(phy_eee_cap2_features_array,
200 			       ARRAY_SIZE(phy_eee_cap2_features_array),
201 			       phy_eee_cap2_features);
202 
203 }
204 
phy_device_free(struct phy_device * phydev)205 void phy_device_free(struct phy_device *phydev)
206 {
207 	put_device(&phydev->mdio.dev);
208 }
209 EXPORT_SYMBOL(phy_device_free);
210 
phy_mdio_device_free(struct mdio_device * mdiodev)211 static void phy_mdio_device_free(struct mdio_device *mdiodev)
212 {
213 	struct phy_device *phydev;
214 
215 	phydev = container_of(mdiodev, struct phy_device, mdio);
216 	phy_device_free(phydev);
217 }
218 
phy_device_release(struct device * dev)219 static void phy_device_release(struct device *dev)
220 {
221 	fwnode_handle_put(dev->fwnode);
222 	kfree(to_phy_device(dev));
223 }
224 
phy_mdio_device_remove(struct mdio_device * mdiodev)225 static void phy_mdio_device_remove(struct mdio_device *mdiodev)
226 {
227 	struct phy_device *phydev;
228 
229 	phydev = container_of(mdiodev, struct phy_device, mdio);
230 	phy_device_remove(phydev);
231 }
232 
233 static struct phy_driver genphy_driver;
234 
235 static LIST_HEAD(phy_fixup_list);
236 static DEFINE_MUTEX(phy_fixup_lock);
237 
phy_drv_wol_enabled(struct phy_device * phydev)238 static bool phy_drv_wol_enabled(struct phy_device *phydev)
239 {
240 	struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
241 
242 	phy_ethtool_get_wol(phydev, &wol);
243 
244 	return wol.wolopts != 0;
245 }
246 
mdio_bus_phy_may_suspend(struct phy_device * phydev)247 static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
248 {
249 	struct device_driver *drv = phydev->mdio.dev.driver;
250 	struct phy_driver *phydrv = to_phy_driver(drv);
251 	struct net_device *netdev = phydev->attached_dev;
252 
253 	if (!drv || !phydrv->suspend)
254 		return false;
255 
256 	/* If the PHY on the mido bus is not attached but has WOL enabled
257 	 * we cannot suspend the PHY.
258 	 */
259 	if (!netdev && phy_drv_wol_enabled(phydev))
260 		return false;
261 
262 	/* PHY not attached? May suspend if the PHY has not already been
263 	 * suspended as part of a prior call to phy_disconnect() ->
264 	 * phy_detach() -> phy_suspend() because the parent netdev might be the
265 	 * MDIO bus driver and clock gated at this point.
266 	 */
267 	if (!netdev)
268 		goto out;
269 
270 	if (netdev->ethtool->wol_enabled)
271 		return false;
272 
273 	/* As long as not all affected network drivers support the
274 	 * wol_enabled flag, let's check for hints that WoL is enabled.
275 	 * Don't suspend PHY if the attached netdev parent may wake up.
276 	 * The parent may point to a PCI device, as in tg3 driver.
277 	 */
278 	if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
279 		return false;
280 
281 	/* Also don't suspend PHY if the netdev itself may wakeup. This
282 	 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
283 	 * e.g. SoC devices.
284 	 */
285 	if (device_may_wakeup(&netdev->dev))
286 		return false;
287 
288 out:
289 	return !phydev->suspended;
290 }
291 
mdio_bus_phy_suspend(struct device * dev)292 static __maybe_unused int mdio_bus_phy_suspend(struct device *dev)
293 {
294 	struct phy_device *phydev = to_phy_device(dev);
295 
296 	if (phydev->mac_managed_pm)
297 		return 0;
298 
299 	/* Wakeup interrupts may occur during the system sleep transition when
300 	 * the PHY is inaccessible. Set flag to postpone handling until the PHY
301 	 * has resumed. Wait for concurrent interrupt handler to complete.
302 	 */
303 	if (phy_interrupt_is_valid(phydev)) {
304 		phydev->irq_suspended = 1;
305 		synchronize_irq(phydev->irq);
306 	}
307 
308 	/* We must stop the state machine manually, otherwise it stops out of
309 	 * control, possibly with the phydev->lock held. Upon resume, netdev
310 	 * may call phy routines that try to grab the same lock, and that may
311 	 * lead to a deadlock.
312 	 */
313 	if (phydev->attached_dev && phydev->adjust_link)
314 		phy_stop_machine(phydev);
315 
316 	if (!mdio_bus_phy_may_suspend(phydev))
317 		return 0;
318 
319 	phydev->suspended_by_mdio_bus = 1;
320 
321 	return phy_suspend(phydev);
322 }
323 
mdio_bus_phy_resume(struct device * dev)324 static __maybe_unused int mdio_bus_phy_resume(struct device *dev)
325 {
326 	struct phy_device *phydev = to_phy_device(dev);
327 	int ret;
328 
329 	if (phydev->mac_managed_pm)
330 		return 0;
331 
332 	if (!phydev->suspended_by_mdio_bus)
333 		goto no_resume;
334 
335 	phydev->suspended_by_mdio_bus = 0;
336 
337 	/* If we managed to get here with the PHY state machine in a state
338 	 * neither PHY_HALTED, PHY_READY nor PHY_UP, this is an indication
339 	 * that something went wrong and we should most likely be using
340 	 * MAC managed PM, but we are not.
341 	 */
342 	WARN_ON(phydev->state != PHY_HALTED && phydev->state != PHY_READY &&
343 		phydev->state != PHY_UP);
344 
345 	ret = phy_init_hw(phydev);
346 	if (ret < 0)
347 		return ret;
348 
349 	ret = phy_resume(phydev);
350 	if (ret < 0)
351 		return ret;
352 no_resume:
353 	if (phy_interrupt_is_valid(phydev)) {
354 		phydev->irq_suspended = 0;
355 		synchronize_irq(phydev->irq);
356 
357 		/* Rerun interrupts which were postponed by phy_interrupt()
358 		 * because they occurred during the system sleep transition.
359 		 */
360 		if (phydev->irq_rerun) {
361 			phydev->irq_rerun = 0;
362 			enable_irq(phydev->irq);
363 			irq_wake_thread(phydev->irq, phydev);
364 		}
365 	}
366 
367 	if (phydev->attached_dev && phydev->adjust_link)
368 		phy_start_machine(phydev);
369 
370 	return 0;
371 }
372 
373 static SIMPLE_DEV_PM_OPS(mdio_bus_phy_pm_ops, mdio_bus_phy_suspend,
374 			 mdio_bus_phy_resume);
375 
376 /**
377  * phy_register_fixup - creates a new phy_fixup and adds it to the list
378  * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
379  * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
380  *	It can also be PHY_ANY_UID
381  * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
382  *	comparison
383  * @run: The actual code to be run when a matching PHY is found
384  */
phy_register_fixup(const char * bus_id,u32 phy_uid,u32 phy_uid_mask,int (* run)(struct phy_device *))385 static int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
386 			      int (*run)(struct phy_device *))
387 {
388 	struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
389 
390 	if (!fixup)
391 		return -ENOMEM;
392 
393 	strscpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
394 	fixup->phy_uid = phy_uid;
395 	fixup->phy_uid_mask = phy_uid_mask;
396 	fixup->run = run;
397 
398 	mutex_lock(&phy_fixup_lock);
399 	list_add_tail(&fixup->list, &phy_fixup_list);
400 	mutex_unlock(&phy_fixup_lock);
401 
402 	return 0;
403 }
404 
405 /* Registers a fixup to be run on any PHY with the UID in phy_uid */
phy_register_fixup_for_uid(u32 phy_uid,u32 phy_uid_mask,int (* run)(struct phy_device *))406 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
407 			       int (*run)(struct phy_device *))
408 {
409 	return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
410 }
411 EXPORT_SYMBOL(phy_register_fixup_for_uid);
412 
413 /* Registers a fixup to be run on the PHY with id string bus_id */
phy_register_fixup_for_id(const char * bus_id,int (* run)(struct phy_device *))414 int phy_register_fixup_for_id(const char *bus_id,
415 			      int (*run)(struct phy_device *))
416 {
417 	return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
418 }
419 EXPORT_SYMBOL(phy_register_fixup_for_id);
420 
421 /**
422  * phy_unregister_fixup - remove a phy_fixup from the list
423  * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list
424  * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list
425  * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison
426  */
phy_unregister_fixup(const char * bus_id,u32 phy_uid,u32 phy_uid_mask)427 int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
428 {
429 	struct list_head *pos, *n;
430 	struct phy_fixup *fixup;
431 	int ret;
432 
433 	ret = -ENODEV;
434 
435 	mutex_lock(&phy_fixup_lock);
436 	list_for_each_safe(pos, n, &phy_fixup_list) {
437 		fixup = list_entry(pos, struct phy_fixup, list);
438 
439 		if ((!strcmp(fixup->bus_id, bus_id)) &&
440 		    phy_id_compare(fixup->phy_uid, phy_uid, phy_uid_mask)) {
441 			list_del(&fixup->list);
442 			kfree(fixup);
443 			ret = 0;
444 			break;
445 		}
446 	}
447 	mutex_unlock(&phy_fixup_lock);
448 
449 	return ret;
450 }
451 EXPORT_SYMBOL(phy_unregister_fixup);
452 
453 /* Unregisters a fixup of any PHY with the UID in phy_uid */
phy_unregister_fixup_for_uid(u32 phy_uid,u32 phy_uid_mask)454 int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask)
455 {
456 	return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);
457 }
458 EXPORT_SYMBOL(phy_unregister_fixup_for_uid);
459 
460 /* Unregisters a fixup of the PHY with id string bus_id */
phy_unregister_fixup_for_id(const char * bus_id)461 int phy_unregister_fixup_for_id(const char *bus_id)
462 {
463 	return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);
464 }
465 EXPORT_SYMBOL(phy_unregister_fixup_for_id);
466 
467 /* Returns 1 if fixup matches phydev in bus_id and phy_uid.
468  * Fixups can be set to match any in one or more fields.
469  */
phy_needs_fixup(struct phy_device * phydev,struct phy_fixup * fixup)470 static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
471 {
472 	if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
473 		if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
474 			return 0;
475 
476 	if (!phy_id_compare(phydev->phy_id, fixup->phy_uid,
477 			    fixup->phy_uid_mask))
478 		if (fixup->phy_uid != PHY_ANY_UID)
479 			return 0;
480 
481 	return 1;
482 }
483 
484 /* Runs any matching fixups for this phydev */
phy_scan_fixups(struct phy_device * phydev)485 static int phy_scan_fixups(struct phy_device *phydev)
486 {
487 	struct phy_fixup *fixup;
488 
489 	mutex_lock(&phy_fixup_lock);
490 	list_for_each_entry(fixup, &phy_fixup_list, list) {
491 		if (phy_needs_fixup(phydev, fixup)) {
492 			int err = fixup->run(phydev);
493 
494 			if (err < 0) {
495 				mutex_unlock(&phy_fixup_lock);
496 				return err;
497 			}
498 			phydev->has_fixups = true;
499 		}
500 	}
501 	mutex_unlock(&phy_fixup_lock);
502 
503 	return 0;
504 }
505 
phy_bus_match(struct device * dev,const struct device_driver * drv)506 static int phy_bus_match(struct device *dev, const struct device_driver *drv)
507 {
508 	struct phy_device *phydev = to_phy_device(dev);
509 	const struct phy_driver *phydrv = to_phy_driver(drv);
510 	const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
511 	int i;
512 
513 	if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
514 		return 0;
515 
516 	if (phydrv->match_phy_device)
517 		return phydrv->match_phy_device(phydev);
518 
519 	if (phydev->is_c45) {
520 		for (i = 1; i < num_ids; i++) {
521 			if (phydev->c45_ids.device_ids[i] == 0xffffffff)
522 				continue;
523 
524 			if (phy_id_compare(phydev->c45_ids.device_ids[i],
525 					   phydrv->phy_id, phydrv->phy_id_mask))
526 				return 1;
527 		}
528 		return 0;
529 	} else {
530 		return phy_id_compare(phydev->phy_id, phydrv->phy_id,
531 				      phydrv->phy_id_mask);
532 	}
533 }
534 
535 static ssize_t
phy_id_show(struct device * dev,struct device_attribute * attr,char * buf)536 phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
537 {
538 	struct phy_device *phydev = to_phy_device(dev);
539 
540 	return sysfs_emit(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
541 }
542 static DEVICE_ATTR_RO(phy_id);
543 
544 static ssize_t
phy_interface_show(struct device * dev,struct device_attribute * attr,char * buf)545 phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
546 {
547 	struct phy_device *phydev = to_phy_device(dev);
548 	const char *mode = NULL;
549 
550 	if (phydev->is_internal)
551 		mode = "internal";
552 	else
553 		mode = phy_modes(phydev->interface);
554 
555 	return sysfs_emit(buf, "%s\n", mode);
556 }
557 static DEVICE_ATTR_RO(phy_interface);
558 
559 static ssize_t
phy_has_fixups_show(struct device * dev,struct device_attribute * attr,char * buf)560 phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
561 		    char *buf)
562 {
563 	struct phy_device *phydev = to_phy_device(dev);
564 
565 	return sysfs_emit(buf, "%d\n", phydev->has_fixups);
566 }
567 static DEVICE_ATTR_RO(phy_has_fixups);
568 
phy_dev_flags_show(struct device * dev,struct device_attribute * attr,char * buf)569 static ssize_t phy_dev_flags_show(struct device *dev,
570 				  struct device_attribute *attr,
571 				  char *buf)
572 {
573 	struct phy_device *phydev = to_phy_device(dev);
574 
575 	return sysfs_emit(buf, "0x%08x\n", phydev->dev_flags);
576 }
577 static DEVICE_ATTR_RO(phy_dev_flags);
578 
579 static struct attribute *phy_dev_attrs[] = {
580 	&dev_attr_phy_id.attr,
581 	&dev_attr_phy_interface.attr,
582 	&dev_attr_phy_has_fixups.attr,
583 	&dev_attr_phy_dev_flags.attr,
584 	NULL,
585 };
586 ATTRIBUTE_GROUPS(phy_dev);
587 
588 static const struct device_type mdio_bus_phy_type = {
589 	.name = "PHY",
590 	.groups = phy_dev_groups,
591 	.release = phy_device_release,
592 	.pm = pm_ptr(&mdio_bus_phy_pm_ops),
593 };
594 
phy_request_driver_module(struct phy_device * dev,u32 phy_id)595 static int phy_request_driver_module(struct phy_device *dev, u32 phy_id)
596 {
597 	int ret;
598 
599 	ret = request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT,
600 			     MDIO_ID_ARGS(phy_id));
601 	/* We only check for failures in executing the usermode binary,
602 	 * not whether a PHY driver module exists for the PHY ID.
603 	 * Accept -ENOENT because this may occur in case no initramfs exists,
604 	 * then modprobe isn't available.
605 	 */
606 	if (IS_ENABLED(CONFIG_MODULES) && ret < 0 && ret != -ENOENT) {
607 		phydev_err(dev, "error %d loading PHY driver module for ID 0x%08lx\n",
608 			   ret, (unsigned long)phy_id);
609 		return ret;
610 	}
611 
612 	return 0;
613 }
614 
phy_device_create(struct mii_bus * bus,int addr,u32 phy_id,bool is_c45,struct phy_c45_device_ids * c45_ids)615 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
616 				     bool is_c45,
617 				     struct phy_c45_device_ids *c45_ids)
618 {
619 	struct phy_device *dev;
620 	struct mdio_device *mdiodev;
621 	int ret = 0;
622 
623 	/* We allocate the device, and initialize the default values */
624 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
625 	if (!dev)
626 		return ERR_PTR(-ENOMEM);
627 
628 	mdiodev = &dev->mdio;
629 	mdiodev->dev.parent = &bus->dev;
630 	mdiodev->dev.bus = &mdio_bus_type;
631 	mdiodev->dev.type = &mdio_bus_phy_type;
632 	mdiodev->bus = bus;
633 	mdiodev->bus_match = phy_bus_match;
634 	mdiodev->addr = addr;
635 	mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
636 	mdiodev->device_free = phy_mdio_device_free;
637 	mdiodev->device_remove = phy_mdio_device_remove;
638 	mdiodev->reset_state = -1;
639 
640 	dev->speed = SPEED_UNKNOWN;
641 	dev->duplex = DUPLEX_UNKNOWN;
642 	dev->pause = 0;
643 	dev->asym_pause = 0;
644 	dev->link = 0;
645 	dev->port = PORT_TP;
646 	dev->interface = PHY_INTERFACE_MODE_GMII;
647 
648 	dev->autoneg = AUTONEG_ENABLE;
649 
650 	dev->pma_extable = -ENODATA;
651 	dev->is_c45 = is_c45;
652 	dev->phy_id = phy_id;
653 	if (c45_ids)
654 		dev->c45_ids = *c45_ids;
655 	dev->irq = bus->irq[addr];
656 
657 	dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
658 	device_initialize(&mdiodev->dev);
659 
660 	dev->state = PHY_DOWN;
661 	INIT_LIST_HEAD(&dev->leds);
662 
663 	mutex_init(&dev->lock);
664 	INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
665 
666 	/* Request the appropriate module unconditionally; don't
667 	 * bother trying to do so only if it isn't already loaded,
668 	 * because that gets complicated. A hotplug event would have
669 	 * done an unconditional modprobe anyway.
670 	 * We don't do normal hotplug because it won't work for MDIO
671 	 * -- because it relies on the device staying around for long
672 	 * enough for the driver to get loaded. With MDIO, the NIC
673 	 * driver will get bored and give up as soon as it finds that
674 	 * there's no driver _already_ loaded.
675 	 */
676 	if (is_c45 && c45_ids) {
677 		const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
678 		int i;
679 
680 		for (i = 1; i < num_ids; i++) {
681 			if (c45_ids->device_ids[i] == 0xffffffff)
682 				continue;
683 
684 			ret = phy_request_driver_module(dev,
685 						c45_ids->device_ids[i]);
686 			if (ret)
687 				break;
688 		}
689 	} else {
690 		ret = phy_request_driver_module(dev, phy_id);
691 	}
692 
693 	if (ret) {
694 		put_device(&mdiodev->dev);
695 		dev = ERR_PTR(ret);
696 	}
697 
698 	return dev;
699 }
700 EXPORT_SYMBOL(phy_device_create);
701 
702 /* phy_c45_probe_present - checks to see if a MMD is present in the package
703  * @bus: the target MII bus
704  * @prtad: PHY package address on the MII bus
705  * @devad: PHY device (MMD) address
706  *
707  * Read the MDIO_STAT2 register, and check whether a device is responding
708  * at this address.
709  *
710  * Returns: negative error number on bus access error, zero if no device
711  * is responding, or positive if a device is present.
712  */
phy_c45_probe_present(struct mii_bus * bus,int prtad,int devad)713 static int phy_c45_probe_present(struct mii_bus *bus, int prtad, int devad)
714 {
715 	int stat2;
716 
717 	stat2 = mdiobus_c45_read(bus, prtad, devad, MDIO_STAT2);
718 	if (stat2 < 0)
719 		return stat2;
720 
721 	return (stat2 & MDIO_STAT2_DEVPRST) == MDIO_STAT2_DEVPRST_VAL;
722 }
723 
724 /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
725  * @bus: the target MII bus
726  * @addr: PHY address on the MII bus
727  * @dev_addr: MMD address in the PHY.
728  * @devices_in_package: where to store the devices in package information.
729  *
730  * Description: reads devices in package registers of a MMD at @dev_addr
731  * from PHY at @addr on @bus.
732  *
733  * Returns: 0 on success, -EIO on failure.
734  */
get_phy_c45_devs_in_pkg(struct mii_bus * bus,int addr,int dev_addr,u32 * devices_in_package)735 static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
736 				   u32 *devices_in_package)
737 {
738 	int phy_reg;
739 
740 	phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS2);
741 	if (phy_reg < 0)
742 		return -EIO;
743 	*devices_in_package = phy_reg << 16;
744 
745 	phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS1);
746 	if (phy_reg < 0)
747 		return -EIO;
748 	*devices_in_package |= phy_reg;
749 
750 	return 0;
751 }
752 
753 /**
754  * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
755  * @bus: the target MII bus
756  * @addr: PHY address on the MII bus
757  * @c45_ids: where to store the c45 ID information.
758  *
759  * Read the PHY "devices in package". If this appears to be valid, read
760  * the PHY identifiers for each device. Return the "devices in package"
761  * and identifiers in @c45_ids.
762  *
763  * Returns zero on success, %-EIO on bus access error, or %-ENODEV if
764  * the "devices in package" is invalid or no device responds.
765  */
get_phy_c45_ids(struct mii_bus * bus,int addr,struct phy_c45_device_ids * c45_ids)766 static int get_phy_c45_ids(struct mii_bus *bus, int addr,
767 			   struct phy_c45_device_ids *c45_ids)
768 {
769 	const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
770 	u32 devs_in_pkg = 0;
771 	int i, ret, phy_reg;
772 
773 	/* Find first non-zero Devices In package. Device zero is reserved
774 	 * for 802.3 c45 complied PHYs, so don't probe it at first.
775 	 */
776 	for (i = 1; i < MDIO_MMD_NUM && (devs_in_pkg == 0 ||
777 	     (devs_in_pkg & 0x1fffffff) == 0x1fffffff); i++) {
778 		if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) {
779 			/* Check that there is a device present at this
780 			 * address before reading the devices-in-package
781 			 * register to avoid reading garbage from the PHY.
782 			 * Some PHYs (88x3310) vendor space is not IEEE802.3
783 			 * compliant.
784 			 */
785 			ret = phy_c45_probe_present(bus, addr, i);
786 			if (ret < 0)
787 				/* returning -ENODEV doesn't stop bus
788 				 * scanning
789 				 */
790 				return (phy_reg == -EIO ||
791 					phy_reg == -ENODEV) ? -ENODEV : -EIO;
792 
793 			if (!ret)
794 				continue;
795 		}
796 		phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, &devs_in_pkg);
797 		if (phy_reg < 0)
798 			return -EIO;
799 	}
800 
801 	if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff) {
802 		/* If mostly Fs, there is no device there, then let's probe
803 		 * MMD 0, as some 10G PHYs have zero Devices In package,
804 		 * e.g. Cortina CS4315/CS4340 PHY.
805 		 */
806 		phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, &devs_in_pkg);
807 		if (phy_reg < 0)
808 			return -EIO;
809 
810 		/* no device there, let's get out of here */
811 		if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff)
812 			return -ENODEV;
813 	}
814 
815 	/* Now probe Device Identifiers for each device present. */
816 	for (i = 1; i < num_ids; i++) {
817 		if (!(devs_in_pkg & (1 << i)))
818 			continue;
819 
820 		if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) {
821 			/* Probe the "Device Present" bits for the vendor MMDs
822 			 * to ignore these if they do not contain IEEE 802.3
823 			 * registers.
824 			 */
825 			ret = phy_c45_probe_present(bus, addr, i);
826 			if (ret < 0)
827 				return ret;
828 
829 			if (!ret)
830 				continue;
831 		}
832 
833 		phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID1);
834 		if (phy_reg < 0)
835 			return -EIO;
836 		c45_ids->device_ids[i] = phy_reg << 16;
837 
838 		phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID2);
839 		if (phy_reg < 0)
840 			return -EIO;
841 		c45_ids->device_ids[i] |= phy_reg;
842 	}
843 
844 	c45_ids->devices_in_package = devs_in_pkg;
845 	/* Bit 0 doesn't represent a device, it indicates c22 regs presence */
846 	c45_ids->mmds_present = devs_in_pkg & ~BIT(0);
847 
848 	return 0;
849 }
850 
851 /**
852  * get_phy_c22_id - reads the specified addr for its clause 22 ID.
853  * @bus: the target MII bus
854  * @addr: PHY address on the MII bus
855  * @phy_id: where to store the ID retrieved.
856  *
857  * Read the 802.3 clause 22 PHY ID from the PHY at @addr on the @bus,
858  * placing it in @phy_id. Return zero on successful read and the ID is
859  * valid, %-EIO on bus access error, or %-ENODEV if no device responds
860  * or invalid ID.
861  */
get_phy_c22_id(struct mii_bus * bus,int addr,u32 * phy_id)862 static int get_phy_c22_id(struct mii_bus *bus, int addr, u32 *phy_id)
863 {
864 	int phy_reg;
865 
866 	/* Grab the bits from PHYIR1, and put them in the upper half */
867 	phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
868 	if (phy_reg < 0) {
869 		/* returning -ENODEV doesn't stop bus scanning */
870 		return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
871 	}
872 
873 	*phy_id = phy_reg << 16;
874 
875 	/* Grab the bits from PHYIR2, and put them in the lower half */
876 	phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
877 	if (phy_reg < 0) {
878 		/* returning -ENODEV doesn't stop bus scanning */
879 		return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
880 	}
881 
882 	*phy_id |= phy_reg;
883 
884 	/* If the phy_id is mostly Fs, there is no device there */
885 	if ((*phy_id & 0x1fffffff) == 0x1fffffff)
886 		return -ENODEV;
887 
888 	return 0;
889 }
890 
891 /* Extract the phy ID from the compatible string of the form
892  * ethernet-phy-idAAAA.BBBB.
893  */
fwnode_get_phy_id(struct fwnode_handle * fwnode,u32 * phy_id)894 int fwnode_get_phy_id(struct fwnode_handle *fwnode, u32 *phy_id)
895 {
896 	unsigned int upper, lower;
897 	const char *cp;
898 	int ret;
899 
900 	ret = fwnode_property_read_string(fwnode, "compatible", &cp);
901 	if (ret)
902 		return ret;
903 
904 	if (sscanf(cp, "ethernet-phy-id%4x.%4x", &upper, &lower) != 2)
905 		return -EINVAL;
906 
907 	*phy_id = ((upper & GENMASK(15, 0)) << 16) | (lower & GENMASK(15, 0));
908 	return 0;
909 }
910 EXPORT_SYMBOL(fwnode_get_phy_id);
911 
912 /**
913  * get_phy_device - reads the specified PHY device and returns its @phy_device
914  *		    struct
915  * @bus: the target MII bus
916  * @addr: PHY address on the MII bus
917  * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
918  *
919  * Probe for a PHY at @addr on @bus.
920  *
921  * When probing for a clause 22 PHY, then read the ID registers. If we find
922  * a valid ID, allocate and return a &struct phy_device.
923  *
924  * When probing for a clause 45 PHY, read the "devices in package" registers.
925  * If the "devices in package" appears valid, read the ID registers for each
926  * MMD, allocate and return a &struct phy_device.
927  *
928  * Returns an allocated &struct phy_device on success, %-ENODEV if there is
929  * no PHY present, or %-EIO on bus access error.
930  */
get_phy_device(struct mii_bus * bus,int addr,bool is_c45)931 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
932 {
933 	struct phy_c45_device_ids c45_ids;
934 	u32 phy_id = 0;
935 	int r;
936 
937 	c45_ids.devices_in_package = 0;
938 	c45_ids.mmds_present = 0;
939 	memset(c45_ids.device_ids, 0xff, sizeof(c45_ids.device_ids));
940 
941 	if (is_c45)
942 		r = get_phy_c45_ids(bus, addr, &c45_ids);
943 	else
944 		r = get_phy_c22_id(bus, addr, &phy_id);
945 
946 	if (r)
947 		return ERR_PTR(r);
948 
949 	/* PHY device such as the Marvell Alaska 88E2110 will return a PHY ID
950 	 * of 0 when probed using get_phy_c22_id() with no error. Proceed to
951 	 * probe with C45 to see if we're able to get a valid PHY ID in the C45
952 	 * space, if successful, create the C45 PHY device.
953 	 */
954 	if (!is_c45 && phy_id == 0 && bus->read_c45) {
955 		r = get_phy_c45_ids(bus, addr, &c45_ids);
956 		if (!r)
957 			return phy_device_create(bus, addr, phy_id,
958 						 true, &c45_ids);
959 	}
960 
961 	return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
962 }
963 EXPORT_SYMBOL(get_phy_device);
964 
965 /**
966  * phy_device_register - Register the phy device on the MDIO bus
967  * @phydev: phy_device structure to be added to the MDIO bus
968  */
phy_device_register(struct phy_device * phydev)969 int phy_device_register(struct phy_device *phydev)
970 {
971 	int err;
972 
973 	err = mdiobus_register_device(&phydev->mdio);
974 	if (err)
975 		return err;
976 
977 	/* Deassert the reset signal */
978 	phy_device_reset(phydev, 0);
979 
980 	/* Run all of the fixups for this PHY */
981 	err = phy_scan_fixups(phydev);
982 	if (err) {
983 		phydev_err(phydev, "failed to initialize\n");
984 		goto out;
985 	}
986 
987 	err = device_add(&phydev->mdio.dev);
988 	if (err) {
989 		phydev_err(phydev, "failed to add\n");
990 		goto out;
991 	}
992 
993 	return 0;
994 
995  out:
996 	/* Assert the reset signal */
997 	phy_device_reset(phydev, 1);
998 
999 	mdiobus_unregister_device(&phydev->mdio);
1000 	return err;
1001 }
1002 EXPORT_SYMBOL(phy_device_register);
1003 
1004 /**
1005  * phy_device_remove - Remove a previously registered phy device from the MDIO bus
1006  * @phydev: phy_device structure to remove
1007  *
1008  * This doesn't free the phy_device itself, it merely reverses the effects
1009  * of phy_device_register(). Use phy_device_free() to free the device
1010  * after calling this function.
1011  */
phy_device_remove(struct phy_device * phydev)1012 void phy_device_remove(struct phy_device *phydev)
1013 {
1014 	unregister_mii_timestamper(phydev->mii_ts);
1015 	pse_control_put(phydev->psec);
1016 
1017 	device_del(&phydev->mdio.dev);
1018 
1019 	/* Assert the reset signal */
1020 	phy_device_reset(phydev, 1);
1021 
1022 	mdiobus_unregister_device(&phydev->mdio);
1023 }
1024 EXPORT_SYMBOL(phy_device_remove);
1025 
1026 /**
1027  * phy_get_c45_ids - Read 802.3-c45 IDs for phy device.
1028  * @phydev: phy_device structure to read 802.3-c45 IDs
1029  *
1030  * Returns zero on success, %-EIO on bus access error, or %-ENODEV if
1031  * the "devices in package" is invalid.
1032  */
phy_get_c45_ids(struct phy_device * phydev)1033 int phy_get_c45_ids(struct phy_device *phydev)
1034 {
1035 	return get_phy_c45_ids(phydev->mdio.bus, phydev->mdio.addr,
1036 			       &phydev->c45_ids);
1037 }
1038 EXPORT_SYMBOL(phy_get_c45_ids);
1039 
1040 /**
1041  * phy_find_first - finds the first PHY device on the bus
1042  * @bus: the target MII bus
1043  */
phy_find_first(struct mii_bus * bus)1044 struct phy_device *phy_find_first(struct mii_bus *bus)
1045 {
1046 	struct phy_device *phydev;
1047 	int addr;
1048 
1049 	for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
1050 		phydev = mdiobus_get_phy(bus, addr);
1051 		if (phydev)
1052 			return phydev;
1053 	}
1054 	return NULL;
1055 }
1056 EXPORT_SYMBOL(phy_find_first);
1057 
phy_link_change(struct phy_device * phydev,bool up)1058 static void phy_link_change(struct phy_device *phydev, bool up)
1059 {
1060 	struct net_device *netdev = phydev->attached_dev;
1061 
1062 	if (up)
1063 		netif_carrier_on(netdev);
1064 	else
1065 		netif_carrier_off(netdev);
1066 	phydev->adjust_link(netdev);
1067 	if (phydev->mii_ts && phydev->mii_ts->link_state)
1068 		phydev->mii_ts->link_state(phydev->mii_ts, phydev);
1069 }
1070 
1071 /**
1072  * phy_prepare_link - prepares the PHY layer to monitor link status
1073  * @phydev: target phy_device struct
1074  * @handler: callback function for link status change notifications
1075  *
1076  * Description: Tells the PHY infrastructure to handle the
1077  *   gory details on monitoring link status (whether through
1078  *   polling or an interrupt), and to call back to the
1079  *   connected device driver when the link status changes.
1080  *   If you want to monitor your own link state, don't call
1081  *   this function.
1082  */
phy_prepare_link(struct phy_device * phydev,void (* handler)(struct net_device *))1083 static void phy_prepare_link(struct phy_device *phydev,
1084 			     void (*handler)(struct net_device *))
1085 {
1086 	phydev->adjust_link = handler;
1087 }
1088 
1089 /**
1090  * phy_connect_direct - connect an ethernet device to a specific phy_device
1091  * @dev: the network device to connect
1092  * @phydev: the pointer to the phy device
1093  * @handler: callback function for state change notifications
1094  * @interface: PHY device's interface
1095  */
phy_connect_direct(struct net_device * dev,struct phy_device * phydev,void (* handler)(struct net_device *),phy_interface_t interface)1096 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
1097 		       void (*handler)(struct net_device *),
1098 		       phy_interface_t interface)
1099 {
1100 	int rc;
1101 
1102 	if (!dev)
1103 		return -EINVAL;
1104 
1105 	rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
1106 	if (rc)
1107 		return rc;
1108 
1109 	phy_prepare_link(phydev, handler);
1110 	if (phy_interrupt_is_valid(phydev))
1111 		phy_request_interrupt(phydev);
1112 
1113 	return 0;
1114 }
1115 EXPORT_SYMBOL(phy_connect_direct);
1116 
1117 /**
1118  * phy_connect - connect an ethernet device to a PHY device
1119  * @dev: the network device to connect
1120  * @bus_id: the id string of the PHY device to connect
1121  * @handler: callback function for state change notifications
1122  * @interface: PHY device's interface
1123  *
1124  * Description: Convenience function for connecting ethernet
1125  *   devices to PHY devices.  The default behavior is for
1126  *   the PHY infrastructure to handle everything, and only notify
1127  *   the connected driver when the link status changes.  If you
1128  *   don't want, or can't use the provided functionality, you may
1129  *   choose to call only the subset of functions which provide
1130  *   the desired functionality.
1131  */
phy_connect(struct net_device * dev,const char * bus_id,void (* handler)(struct net_device *),phy_interface_t interface)1132 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
1133 			       void (*handler)(struct net_device *),
1134 			       phy_interface_t interface)
1135 {
1136 	struct phy_device *phydev;
1137 	struct device *d;
1138 	int rc;
1139 
1140 	/* Search the list of PHY devices on the mdio bus for the
1141 	 * PHY with the requested name
1142 	 */
1143 	d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
1144 	if (!d) {
1145 		pr_err("PHY %s not found\n", bus_id);
1146 		return ERR_PTR(-ENODEV);
1147 	}
1148 	phydev = to_phy_device(d);
1149 
1150 	rc = phy_connect_direct(dev, phydev, handler, interface);
1151 	put_device(d);
1152 	if (rc)
1153 		return ERR_PTR(rc);
1154 
1155 	return phydev;
1156 }
1157 EXPORT_SYMBOL(phy_connect);
1158 
1159 /**
1160  * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
1161  *		    device
1162  * @phydev: target phy_device struct
1163  */
phy_disconnect(struct phy_device * phydev)1164 void phy_disconnect(struct phy_device *phydev)
1165 {
1166 	if (phy_is_started(phydev))
1167 		phy_stop(phydev);
1168 
1169 	if (phy_interrupt_is_valid(phydev))
1170 		phy_free_interrupt(phydev);
1171 
1172 	phydev->adjust_link = NULL;
1173 
1174 	phy_detach(phydev);
1175 }
1176 EXPORT_SYMBOL(phy_disconnect);
1177 
1178 /**
1179  * phy_poll_reset - Safely wait until a PHY reset has properly completed
1180  * @phydev: The PHY device to poll
1181  *
1182  * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
1183  *   published in 2008, a PHY reset may take up to 0.5 seconds.  The MII BMCR
1184  *   register must be polled until the BMCR_RESET bit clears.
1185  *
1186  *   Furthermore, any attempts to write to PHY registers may have no effect
1187  *   or even generate MDIO bus errors until this is complete.
1188  *
1189  *   Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
1190  *   standard and do not fully reset after the BMCR_RESET bit is set, and may
1191  *   even *REQUIRE* a soft-reset to properly restart autonegotiation.  In an
1192  *   effort to support such broken PHYs, this function is separate from the
1193  *   standard phy_init_hw() which will zero all the other bits in the BMCR
1194  *   and reapply all driver-specific and board-specific fixups.
1195  */
phy_poll_reset(struct phy_device * phydev)1196 static int phy_poll_reset(struct phy_device *phydev)
1197 {
1198 	/* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
1199 	int ret, val;
1200 
1201 	ret = phy_read_poll_timeout(phydev, MII_BMCR, val, !(val & BMCR_RESET),
1202 				    50000, 600000, true);
1203 	if (ret)
1204 		return ret;
1205 	/* Some chips (smsc911x) may still need up to another 1ms after the
1206 	 * BMCR_RESET bit is cleared before they are usable.
1207 	 */
1208 	msleep(1);
1209 	return 0;
1210 }
1211 
phy_init_hw(struct phy_device * phydev)1212 int phy_init_hw(struct phy_device *phydev)
1213 {
1214 	int ret = 0;
1215 
1216 	/* Deassert the reset signal */
1217 	phy_device_reset(phydev, 0);
1218 
1219 	if (!phydev->drv)
1220 		return 0;
1221 
1222 	if (phydev->drv->soft_reset) {
1223 		ret = phydev->drv->soft_reset(phydev);
1224 		if (ret < 0)
1225 			return ret;
1226 
1227 		/* see comment in genphy_soft_reset for an explanation */
1228 		phydev->suspended = 0;
1229 	}
1230 
1231 	ret = phy_scan_fixups(phydev);
1232 	if (ret < 0)
1233 		return ret;
1234 
1235 	phy_interface_zero(phydev->possible_interfaces);
1236 
1237 	if (phydev->drv->config_init) {
1238 		ret = phydev->drv->config_init(phydev);
1239 		if (ret < 0)
1240 			return ret;
1241 	}
1242 
1243 	if (phydev->drv->config_intr) {
1244 		ret = phydev->drv->config_intr(phydev);
1245 		if (ret < 0)
1246 			return ret;
1247 	}
1248 
1249 	return 0;
1250 }
1251 EXPORT_SYMBOL(phy_init_hw);
1252 
phy_attached_info(struct phy_device * phydev)1253 void phy_attached_info(struct phy_device *phydev)
1254 {
1255 	phy_attached_print(phydev, NULL);
1256 }
1257 EXPORT_SYMBOL(phy_attached_info);
1258 
1259 #define ATTACHED_FMT "attached PHY driver %s(mii_bus:phy_addr=%s, irq=%s)"
phy_attached_info_irq(struct phy_device * phydev)1260 char *phy_attached_info_irq(struct phy_device *phydev)
1261 {
1262 	char *irq_str;
1263 	char irq_num[8];
1264 
1265 	switch(phydev->irq) {
1266 	case PHY_POLL:
1267 		irq_str = "POLL";
1268 		break;
1269 	case PHY_MAC_INTERRUPT:
1270 		irq_str = "MAC";
1271 		break;
1272 	default:
1273 		snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
1274 		irq_str = irq_num;
1275 		break;
1276 	}
1277 
1278 	return kasprintf(GFP_KERNEL, "%s", irq_str);
1279 }
1280 EXPORT_SYMBOL(phy_attached_info_irq);
1281 
phy_attached_print(struct phy_device * phydev,const char * fmt,...)1282 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1283 {
1284 	const char *unbound = phydev->drv ? "" : "[unbound] ";
1285 	char *irq_str = phy_attached_info_irq(phydev);
1286 
1287 	if (!fmt) {
1288 		phydev_info(phydev, ATTACHED_FMT "\n", unbound,
1289 			    phydev_name(phydev), irq_str);
1290 	} else {
1291 		va_list ap;
1292 
1293 		phydev_info(phydev, ATTACHED_FMT, unbound,
1294 			    phydev_name(phydev), irq_str);
1295 
1296 		va_start(ap, fmt);
1297 		vprintk(fmt, ap);
1298 		va_end(ap);
1299 	}
1300 	kfree(irq_str);
1301 }
1302 EXPORT_SYMBOL(phy_attached_print);
1303 
phy_sysfs_create_links(struct phy_device * phydev)1304 static void phy_sysfs_create_links(struct phy_device *phydev)
1305 {
1306 	struct net_device *dev = phydev->attached_dev;
1307 	int err;
1308 
1309 	if (!dev)
1310 		return;
1311 
1312 	err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
1313 				"attached_dev");
1314 	if (err)
1315 		return;
1316 
1317 	err = sysfs_create_link_nowarn(&dev->dev.kobj,
1318 				       &phydev->mdio.dev.kobj,
1319 				       "phydev");
1320 	if (err) {
1321 		dev_err(&dev->dev, "could not add device link to %s err %d\n",
1322 			kobject_name(&phydev->mdio.dev.kobj),
1323 			err);
1324 		/* non-fatal - some net drivers can use one netdevice
1325 		 * with more then one phy
1326 		 */
1327 	}
1328 
1329 	phydev->sysfs_links = true;
1330 }
1331 
1332 static ssize_t
phy_standalone_show(struct device * dev,struct device_attribute * attr,char * buf)1333 phy_standalone_show(struct device *dev, struct device_attribute *attr,
1334 		    char *buf)
1335 {
1336 	struct phy_device *phydev = to_phy_device(dev);
1337 
1338 	return sysfs_emit(buf, "%d\n", !phydev->attached_dev);
1339 }
1340 static DEVICE_ATTR_RO(phy_standalone);
1341 
1342 /**
1343  * phy_sfp_connect_phy - Connect the SFP module's PHY to the upstream PHY
1344  * @upstream: pointer to the upstream phy device
1345  * @phy: pointer to the SFP module's phy device
1346  *
1347  * This helper allows keeping track of PHY devices on the link. It adds the
1348  * SFP module's phy to the phy namespace of the upstream phy
1349  *
1350  * Return: 0 on success, otherwise a negative error code.
1351  */
phy_sfp_connect_phy(void * upstream,struct phy_device * phy)1352 int phy_sfp_connect_phy(void *upstream, struct phy_device *phy)
1353 {
1354 	struct phy_device *phydev = upstream;
1355 	struct net_device *dev = phydev->attached_dev;
1356 
1357 	if (dev)
1358 		return phy_link_topo_add_phy(dev, phy, PHY_UPSTREAM_PHY, phydev);
1359 
1360 	return 0;
1361 }
1362 EXPORT_SYMBOL(phy_sfp_connect_phy);
1363 
1364 /**
1365  * phy_sfp_disconnect_phy - Disconnect the SFP module's PHY from the upstream PHY
1366  * @upstream: pointer to the upstream phy device
1367  * @phy: pointer to the SFP module's phy device
1368  *
1369  * This helper allows keeping track of PHY devices on the link. It removes the
1370  * SFP module's phy to the phy namespace of the upstream phy. As the module phy
1371  * will be destroyed, re-inserting the same module will add a new phy with a
1372  * new index.
1373  */
phy_sfp_disconnect_phy(void * upstream,struct phy_device * phy)1374 void phy_sfp_disconnect_phy(void *upstream, struct phy_device *phy)
1375 {
1376 	struct phy_device *phydev = upstream;
1377 	struct net_device *dev = phydev->attached_dev;
1378 
1379 	if (dev)
1380 		phy_link_topo_del_phy(dev, phy);
1381 }
1382 EXPORT_SYMBOL(phy_sfp_disconnect_phy);
1383 
1384 /**
1385  * phy_sfp_attach - attach the SFP bus to the PHY upstream network device
1386  * @upstream: pointer to the phy device
1387  * @bus: sfp bus representing cage being attached
1388  *
1389  * This is used to fill in the sfp_upstream_ops .attach member.
1390  */
phy_sfp_attach(void * upstream,struct sfp_bus * bus)1391 void phy_sfp_attach(void *upstream, struct sfp_bus *bus)
1392 {
1393 	struct phy_device *phydev = upstream;
1394 
1395 	if (phydev->attached_dev)
1396 		phydev->attached_dev->sfp_bus = bus;
1397 	phydev->sfp_bus_attached = true;
1398 }
1399 EXPORT_SYMBOL(phy_sfp_attach);
1400 
1401 /**
1402  * phy_sfp_detach - detach the SFP bus from the PHY upstream network device
1403  * @upstream: pointer to the phy device
1404  * @bus: sfp bus representing cage being attached
1405  *
1406  * This is used to fill in the sfp_upstream_ops .detach member.
1407  */
phy_sfp_detach(void * upstream,struct sfp_bus * bus)1408 void phy_sfp_detach(void *upstream, struct sfp_bus *bus)
1409 {
1410 	struct phy_device *phydev = upstream;
1411 
1412 	if (phydev->attached_dev)
1413 		phydev->attached_dev->sfp_bus = NULL;
1414 	phydev->sfp_bus_attached = false;
1415 }
1416 EXPORT_SYMBOL(phy_sfp_detach);
1417 
1418 /**
1419  * phy_sfp_probe - probe for a SFP cage attached to this PHY device
1420  * @phydev: Pointer to phy_device
1421  * @ops: SFP's upstream operations
1422  */
phy_sfp_probe(struct phy_device * phydev,const struct sfp_upstream_ops * ops)1423 int phy_sfp_probe(struct phy_device *phydev,
1424 		  const struct sfp_upstream_ops *ops)
1425 {
1426 	struct sfp_bus *bus;
1427 	int ret = 0;
1428 
1429 	if (phydev->mdio.dev.fwnode) {
1430 		bus = sfp_bus_find_fwnode(phydev->mdio.dev.fwnode);
1431 		if (IS_ERR(bus))
1432 			return PTR_ERR(bus);
1433 
1434 		phydev->sfp_bus = bus;
1435 
1436 		ret = sfp_bus_add_upstream(bus, phydev, ops);
1437 		sfp_bus_put(bus);
1438 	}
1439 	return ret;
1440 }
1441 EXPORT_SYMBOL(phy_sfp_probe);
1442 
phy_drv_supports_irq(const struct phy_driver * phydrv)1443 static bool phy_drv_supports_irq(const struct phy_driver *phydrv)
1444 {
1445 	return phydrv->config_intr && phydrv->handle_interrupt;
1446 }
1447 
1448 /**
1449  * phy_attach_direct - attach a network device to a given PHY device pointer
1450  * @dev: network device to attach
1451  * @phydev: Pointer to phy_device to attach
1452  * @flags: PHY device's dev_flags
1453  * @interface: PHY device's interface
1454  *
1455  * Description: Called by drivers to attach to a particular PHY
1456  *     device. The phy_device is found, and properly hooked up
1457  *     to the phy_driver.  If no driver is attached, then a
1458  *     generic driver is used.  The phy_device is given a ptr to
1459  *     the attaching device, and given a callback for link status
1460  *     change.  The phy_device is returned to the attaching driver.
1461  *     This function takes a reference on the phy device.
1462  */
phy_attach_direct(struct net_device * dev,struct phy_device * phydev,u32 flags,phy_interface_t interface)1463 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1464 		      u32 flags, phy_interface_t interface)
1465 {
1466 	struct mii_bus *bus = phydev->mdio.bus;
1467 	struct device *d = &phydev->mdio.dev;
1468 	struct module *ndev_owner = NULL;
1469 	bool using_genphy = false;
1470 	int err;
1471 
1472 	/* For Ethernet device drivers that register their own MDIO bus, we
1473 	 * will have bus->owner match ndev_mod, so we do not want to increment
1474 	 * our own module->refcnt here, otherwise we would not be able to
1475 	 * unload later on.
1476 	 */
1477 	if (dev)
1478 		ndev_owner = dev->dev.parent->driver->owner;
1479 	if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
1480 		phydev_err(phydev, "failed to get the bus module\n");
1481 		return -EIO;
1482 	}
1483 
1484 	get_device(d);
1485 
1486 	/* Assume that if there is no driver, that it doesn't
1487 	 * exist, and we should use the genphy driver.
1488 	 */
1489 	if (!d->driver) {
1490 		if (phydev->is_c45)
1491 			d->driver = &genphy_c45_driver.mdiodrv.driver;
1492 		else
1493 			d->driver = &genphy_driver.mdiodrv.driver;
1494 
1495 		using_genphy = true;
1496 	}
1497 
1498 	if (!try_module_get(d->driver->owner)) {
1499 		phydev_err(phydev, "failed to get the device driver module\n");
1500 		err = -EIO;
1501 		goto error_put_device;
1502 	}
1503 
1504 	if (using_genphy) {
1505 		err = d->driver->probe(d);
1506 		if (err >= 0)
1507 			err = device_bind_driver(d);
1508 
1509 		if (err)
1510 			goto error_module_put;
1511 	}
1512 
1513 	if (phydev->attached_dev) {
1514 		dev_err(&dev->dev, "PHY already attached\n");
1515 		err = -EBUSY;
1516 		goto error;
1517 	}
1518 
1519 	phydev->phy_link_change = phy_link_change;
1520 	if (dev) {
1521 		phydev->attached_dev = dev;
1522 		dev->phydev = phydev;
1523 
1524 		if (phydev->sfp_bus_attached)
1525 			dev->sfp_bus = phydev->sfp_bus;
1526 
1527 		err = phy_link_topo_add_phy(dev, phydev, PHY_UPSTREAM_MAC, dev);
1528 		if (err)
1529 			goto error;
1530 	}
1531 
1532 	/* Some Ethernet drivers try to connect to a PHY device before
1533 	 * calling register_netdevice() -> netdev_register_kobject() and
1534 	 * does the dev->dev.kobj initialization. Here we only check for
1535 	 * success which indicates that the network device kobject is
1536 	 * ready. Once we do that we still need to keep track of whether
1537 	 * links were successfully set up or not for phy_detach() to
1538 	 * remove them accordingly.
1539 	 */
1540 	phydev->sysfs_links = false;
1541 
1542 	phy_sysfs_create_links(phydev);
1543 
1544 	if (!phydev->attached_dev) {
1545 		err = sysfs_create_file(&phydev->mdio.dev.kobj,
1546 					&dev_attr_phy_standalone.attr);
1547 		if (err)
1548 			phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n");
1549 	}
1550 
1551 	phydev->dev_flags |= flags;
1552 
1553 	phydev->interface = interface;
1554 
1555 	phydev->state = PHY_READY;
1556 
1557 	phydev->interrupts = PHY_INTERRUPT_DISABLED;
1558 
1559 	/* PHYs can request to use poll mode even though they have an
1560 	 * associated interrupt line. This could be the case if they
1561 	 * detect a broken interrupt handling.
1562 	 */
1563 	if (phydev->dev_flags & PHY_F_NO_IRQ)
1564 		phydev->irq = PHY_POLL;
1565 
1566 	if (!phy_drv_supports_irq(phydev->drv) && phy_interrupt_is_valid(phydev))
1567 		phydev->irq = PHY_POLL;
1568 
1569 	/* Port is set to PORT_TP by default and the actual PHY driver will set
1570 	 * it to different value depending on the PHY configuration. If we have
1571 	 * the generic PHY driver we can't figure it out, thus set the old
1572 	 * legacy PORT_MII value.
1573 	 */
1574 	if (using_genphy)
1575 		phydev->port = PORT_MII;
1576 
1577 	/* Initial carrier state is off as the phy is about to be
1578 	 * (re)initialized.
1579 	 */
1580 	if (dev)
1581 		netif_carrier_off(phydev->attached_dev);
1582 
1583 	/* Do initial configuration here, now that
1584 	 * we have certain key parameters
1585 	 * (dev_flags and interface)
1586 	 */
1587 	err = phy_init_hw(phydev);
1588 	if (err)
1589 		goto error;
1590 
1591 	phy_resume(phydev);
1592 	if (!phydev->is_on_sfp_module)
1593 		phy_led_triggers_register(phydev);
1594 
1595 	/**
1596 	 * If the external phy used by current mac interface is managed by
1597 	 * another mac interface, so we should create a device link between
1598 	 * phy dev and mac dev.
1599 	 */
1600 	if (dev && phydev->mdio.bus->parent && dev->dev.parent != phydev->mdio.bus->parent)
1601 		phydev->devlink = device_link_add(dev->dev.parent, &phydev->mdio.dev,
1602 						  DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS);
1603 
1604 	return err;
1605 
1606 error:
1607 	/* phy_detach() does all of the cleanup below */
1608 	phy_detach(phydev);
1609 	return err;
1610 
1611 error_module_put:
1612 	module_put(d->driver->owner);
1613 	d->driver = NULL;
1614 error_put_device:
1615 	put_device(d);
1616 	if (ndev_owner != bus->owner)
1617 		module_put(bus->owner);
1618 	return err;
1619 }
1620 EXPORT_SYMBOL(phy_attach_direct);
1621 
1622 /**
1623  * phy_attach - attach a network device to a particular PHY device
1624  * @dev: network device to attach
1625  * @bus_id: Bus ID of PHY device to attach
1626  * @interface: PHY device's interface
1627  *
1628  * Description: Same as phy_attach_direct() except that a PHY bus_id
1629  *     string is passed instead of a pointer to a struct phy_device.
1630  */
phy_attach(struct net_device * dev,const char * bus_id,phy_interface_t interface)1631 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1632 			      phy_interface_t interface)
1633 {
1634 	struct phy_device *phydev;
1635 	struct device *d;
1636 	int rc;
1637 
1638 	if (!dev)
1639 		return ERR_PTR(-EINVAL);
1640 
1641 	/* Search the list of PHY devices on the mdio bus for the
1642 	 * PHY with the requested name
1643 	 */
1644 	d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
1645 	if (!d) {
1646 		pr_err("PHY %s not found\n", bus_id);
1647 		return ERR_PTR(-ENODEV);
1648 	}
1649 	phydev = to_phy_device(d);
1650 
1651 	rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
1652 	put_device(d);
1653 	if (rc)
1654 		return ERR_PTR(rc);
1655 
1656 	return phydev;
1657 }
1658 EXPORT_SYMBOL(phy_attach);
1659 
phy_driver_is_genphy_kind(struct phy_device * phydev,struct device_driver * driver)1660 static bool phy_driver_is_genphy_kind(struct phy_device *phydev,
1661 				      struct device_driver *driver)
1662 {
1663 	struct device *d = &phydev->mdio.dev;
1664 	bool ret = false;
1665 
1666 	if (!phydev->drv)
1667 		return ret;
1668 
1669 	get_device(d);
1670 	ret = d->driver == driver;
1671 	put_device(d);
1672 
1673 	return ret;
1674 }
1675 
phy_driver_is_genphy(struct phy_device * phydev)1676 bool phy_driver_is_genphy(struct phy_device *phydev)
1677 {
1678 	return phy_driver_is_genphy_kind(phydev,
1679 					 &genphy_driver.mdiodrv.driver);
1680 }
1681 EXPORT_SYMBOL_GPL(phy_driver_is_genphy);
1682 
phy_driver_is_genphy_10g(struct phy_device * phydev)1683 bool phy_driver_is_genphy_10g(struct phy_device *phydev)
1684 {
1685 	return phy_driver_is_genphy_kind(phydev,
1686 					 &genphy_c45_driver.mdiodrv.driver);
1687 }
1688 EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g);
1689 
1690 /**
1691  * phy_detach - detach a PHY device from its network device
1692  * @phydev: target phy_device struct
1693  *
1694  * This detaches the phy device from its network device and the phy
1695  * driver, and drops the reference count taken in phy_attach_direct().
1696  */
phy_detach(struct phy_device * phydev)1697 void phy_detach(struct phy_device *phydev)
1698 {
1699 	struct net_device *dev = phydev->attached_dev;
1700 	struct module *ndev_owner = NULL;
1701 	struct mii_bus *bus;
1702 
1703 	if (phydev->devlink)
1704 		device_link_del(phydev->devlink);
1705 
1706 	if (phydev->sysfs_links) {
1707 		if (dev)
1708 			sysfs_remove_link(&dev->dev.kobj, "phydev");
1709 		sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
1710 	}
1711 
1712 	if (!phydev->attached_dev)
1713 		sysfs_remove_file(&phydev->mdio.dev.kobj,
1714 				  &dev_attr_phy_standalone.attr);
1715 
1716 	phy_suspend(phydev);
1717 	if (dev) {
1718 		struct hwtstamp_provider *hwprov;
1719 
1720 		hwprov = rtnl_dereference(dev->hwprov);
1721 		/* Disable timestamp if it is the one selected */
1722 		if (hwprov && hwprov->phydev == phydev) {
1723 			rcu_assign_pointer(dev->hwprov, NULL);
1724 			kfree_rcu(hwprov, rcu_head);
1725 		}
1726 
1727 		phydev->attached_dev->phydev = NULL;
1728 		phydev->attached_dev = NULL;
1729 		phy_link_topo_del_phy(dev, phydev);
1730 	}
1731 	phydev->phylink = NULL;
1732 
1733 	if (!phydev->is_on_sfp_module)
1734 		phy_led_triggers_unregister(phydev);
1735 
1736 	if (phydev->mdio.dev.driver)
1737 		module_put(phydev->mdio.dev.driver->owner);
1738 
1739 	/* If the device had no specific driver before (i.e. - it
1740 	 * was using the generic driver), we unbind the device
1741 	 * from the generic driver so that there's a chance a
1742 	 * real driver could be loaded
1743 	 */
1744 	if (phy_driver_is_genphy(phydev) ||
1745 	    phy_driver_is_genphy_10g(phydev))
1746 		device_release_driver(&phydev->mdio.dev);
1747 
1748 	/* Assert the reset signal */
1749 	phy_device_reset(phydev, 1);
1750 
1751 	/*
1752 	 * The phydev might go away on the put_device() below, so avoid
1753 	 * a use-after-free bug by reading the underlying bus first.
1754 	 */
1755 	bus = phydev->mdio.bus;
1756 
1757 	put_device(&phydev->mdio.dev);
1758 	if (dev)
1759 		ndev_owner = dev->dev.parent->driver->owner;
1760 	if (ndev_owner != bus->owner)
1761 		module_put(bus->owner);
1762 }
1763 EXPORT_SYMBOL(phy_detach);
1764 
phy_suspend(struct phy_device * phydev)1765 int phy_suspend(struct phy_device *phydev)
1766 {
1767 	struct net_device *netdev = phydev->attached_dev;
1768 	const struct phy_driver *phydrv = phydev->drv;
1769 	int ret;
1770 
1771 	if (phydev->suspended || !phydrv)
1772 		return 0;
1773 
1774 	phydev->wol_enabled = phy_drv_wol_enabled(phydev) ||
1775 			      (netdev && netdev->ethtool->wol_enabled);
1776 	/* If the device has WOL enabled, we cannot suspend the PHY */
1777 	if (phydev->wol_enabled && !(phydrv->flags & PHY_ALWAYS_CALL_SUSPEND))
1778 		return -EBUSY;
1779 
1780 	if (!phydrv->suspend)
1781 		return 0;
1782 
1783 	ret = phydrv->suspend(phydev);
1784 	if (!ret)
1785 		phydev->suspended = true;
1786 
1787 	return ret;
1788 }
1789 EXPORT_SYMBOL(phy_suspend);
1790 
__phy_resume(struct phy_device * phydev)1791 int __phy_resume(struct phy_device *phydev)
1792 {
1793 	const struct phy_driver *phydrv = phydev->drv;
1794 	int ret;
1795 
1796 	lockdep_assert_held(&phydev->lock);
1797 
1798 	if (!phydrv || !phydrv->resume)
1799 		return 0;
1800 
1801 	ret = phydrv->resume(phydev);
1802 	if (!ret)
1803 		phydev->suspended = false;
1804 
1805 	return ret;
1806 }
1807 EXPORT_SYMBOL(__phy_resume);
1808 
phy_resume(struct phy_device * phydev)1809 int phy_resume(struct phy_device *phydev)
1810 {
1811 	int ret;
1812 
1813 	mutex_lock(&phydev->lock);
1814 	ret = __phy_resume(phydev);
1815 	mutex_unlock(&phydev->lock);
1816 
1817 	return ret;
1818 }
1819 EXPORT_SYMBOL(phy_resume);
1820 
1821 /**
1822  * phy_reset_after_clk_enable - perform a PHY reset if needed
1823  * @phydev: target phy_device struct
1824  *
1825  * Description: Some PHYs are known to need a reset after their refclk was
1826  *   enabled. This function evaluates the flags and perform the reset if it's
1827  *   needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
1828  *   was reset.
1829  */
phy_reset_after_clk_enable(struct phy_device * phydev)1830 int phy_reset_after_clk_enable(struct phy_device *phydev)
1831 {
1832 	if (!phydev || !phydev->drv)
1833 		return -ENODEV;
1834 
1835 	if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
1836 		phy_device_reset(phydev, 1);
1837 		phy_device_reset(phydev, 0);
1838 		return 1;
1839 	}
1840 
1841 	return 0;
1842 }
1843 EXPORT_SYMBOL(phy_reset_after_clk_enable);
1844 
1845 /* Generic PHY support and helper functions */
1846 
1847 /**
1848  * genphy_config_advert - sanitize and advertise auto-negotiation parameters
1849  * @phydev: target phy_device struct
1850  * @advert: auto-negotiation parameters to advertise
1851  *
1852  * Description: Writes MII_ADVERTISE with the appropriate values,
1853  *   after sanitizing the values to make sure we only advertise
1854  *   what is supported.  Returns < 0 on error, 0 if the PHY's advertisement
1855  *   hasn't changed, and > 0 if it has changed.
1856  */
genphy_config_advert(struct phy_device * phydev,const unsigned long * advert)1857 static int genphy_config_advert(struct phy_device *phydev,
1858 				const unsigned long *advert)
1859 {
1860 	int err, bmsr, changed = 0;
1861 	u32 adv;
1862 
1863 	adv = linkmode_adv_to_mii_adv_t(advert);
1864 
1865 	/* Setup standard advertisement */
1866 	err = phy_modify_changed(phydev, MII_ADVERTISE,
1867 				 ADVERTISE_ALL | ADVERTISE_100BASE4 |
1868 				 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
1869 				 adv);
1870 	if (err < 0)
1871 		return err;
1872 	if (err > 0)
1873 		changed = 1;
1874 
1875 	bmsr = phy_read(phydev, MII_BMSR);
1876 	if (bmsr < 0)
1877 		return bmsr;
1878 
1879 	/* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
1880 	 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
1881 	 * logical 1.
1882 	 */
1883 	if (!(bmsr & BMSR_ESTATEN))
1884 		return changed;
1885 
1886 	adv = linkmode_adv_to_mii_ctrl1000_t(advert);
1887 
1888 	err = phy_modify_changed(phydev, MII_CTRL1000,
1889 				 ADVERTISE_1000FULL | ADVERTISE_1000HALF,
1890 				 adv);
1891 	if (err < 0)
1892 		return err;
1893 	if (err > 0)
1894 		changed = 1;
1895 
1896 	return changed;
1897 }
1898 
1899 /**
1900  * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters
1901  * @phydev: target phy_device struct
1902  *
1903  * Description: Writes MII_ADVERTISE with the appropriate values,
1904  *   after sanitizing the values to make sure we only advertise
1905  *   what is supported.  Returns < 0 on error, 0 if the PHY's advertisement
1906  *   hasn't changed, and > 0 if it has changed. This function is intended
1907  *   for Clause 37 1000Base-X mode.
1908  */
genphy_c37_config_advert(struct phy_device * phydev)1909 static int genphy_c37_config_advert(struct phy_device *phydev)
1910 {
1911 	u16 adv = 0;
1912 
1913 	/* Only allow advertising what this PHY supports */
1914 	linkmode_and(phydev->advertising, phydev->advertising,
1915 		     phydev->supported);
1916 
1917 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
1918 			      phydev->advertising))
1919 		adv |= ADVERTISE_1000XFULL;
1920 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
1921 			      phydev->advertising))
1922 		adv |= ADVERTISE_1000XPAUSE;
1923 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
1924 			      phydev->advertising))
1925 		adv |= ADVERTISE_1000XPSE_ASYM;
1926 
1927 	return phy_modify_changed(phydev, MII_ADVERTISE,
1928 				  ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1929 				  ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM,
1930 				  adv);
1931 }
1932 
1933 /**
1934  * genphy_setup_forced - configures/forces speed/duplex from @phydev
1935  * @phydev: target phy_device struct
1936  *
1937  * Description: Configures MII_BMCR to force speed/duplex
1938  *   to the values in phydev. Assumes that the values are valid.
1939  *   Please see phy_sanitize_settings().
1940  */
genphy_setup_forced(struct phy_device * phydev)1941 int genphy_setup_forced(struct phy_device *phydev)
1942 {
1943 	u16 ctl;
1944 
1945 	phydev->pause = 0;
1946 	phydev->asym_pause = 0;
1947 
1948 	ctl = mii_bmcr_encode_fixed(phydev->speed, phydev->duplex);
1949 
1950 	return phy_modify(phydev, MII_BMCR,
1951 			  ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
1952 }
1953 EXPORT_SYMBOL(genphy_setup_forced);
1954 
genphy_setup_master_slave(struct phy_device * phydev)1955 static int genphy_setup_master_slave(struct phy_device *phydev)
1956 {
1957 	u16 ctl = 0;
1958 
1959 	if (!phydev->is_gigabit_capable)
1960 		return 0;
1961 
1962 	switch (phydev->master_slave_set) {
1963 	case MASTER_SLAVE_CFG_MASTER_PREFERRED:
1964 		ctl |= CTL1000_PREFER_MASTER;
1965 		break;
1966 	case MASTER_SLAVE_CFG_SLAVE_PREFERRED:
1967 		break;
1968 	case MASTER_SLAVE_CFG_MASTER_FORCE:
1969 		ctl |= CTL1000_AS_MASTER;
1970 		fallthrough;
1971 	case MASTER_SLAVE_CFG_SLAVE_FORCE:
1972 		ctl |= CTL1000_ENABLE_MASTER;
1973 		break;
1974 	case MASTER_SLAVE_CFG_UNKNOWN:
1975 	case MASTER_SLAVE_CFG_UNSUPPORTED:
1976 		return 0;
1977 	default:
1978 		phydev_warn(phydev, "Unsupported Master/Slave mode\n");
1979 		return -EOPNOTSUPP;
1980 	}
1981 
1982 	return phy_modify_changed(phydev, MII_CTRL1000,
1983 				  (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER |
1984 				   CTL1000_PREFER_MASTER), ctl);
1985 }
1986 
genphy_read_master_slave(struct phy_device * phydev)1987 int genphy_read_master_slave(struct phy_device *phydev)
1988 {
1989 	int cfg, state;
1990 	int val;
1991 
1992 	phydev->master_slave_get = MASTER_SLAVE_CFG_UNKNOWN;
1993 	phydev->master_slave_state = MASTER_SLAVE_STATE_UNKNOWN;
1994 
1995 	val = phy_read(phydev, MII_CTRL1000);
1996 	if (val < 0)
1997 		return val;
1998 
1999 	if (val & CTL1000_ENABLE_MASTER) {
2000 		if (val & CTL1000_AS_MASTER)
2001 			cfg = MASTER_SLAVE_CFG_MASTER_FORCE;
2002 		else
2003 			cfg = MASTER_SLAVE_CFG_SLAVE_FORCE;
2004 	} else {
2005 		if (val & CTL1000_PREFER_MASTER)
2006 			cfg = MASTER_SLAVE_CFG_MASTER_PREFERRED;
2007 		else
2008 			cfg = MASTER_SLAVE_CFG_SLAVE_PREFERRED;
2009 	}
2010 
2011 	val = phy_read(phydev, MII_STAT1000);
2012 	if (val < 0)
2013 		return val;
2014 
2015 	if (val & LPA_1000MSFAIL) {
2016 		state = MASTER_SLAVE_STATE_ERR;
2017 	} else if (phydev->link) {
2018 		/* this bits are valid only for active link */
2019 		if (val & LPA_1000MSRES)
2020 			state = MASTER_SLAVE_STATE_MASTER;
2021 		else
2022 			state = MASTER_SLAVE_STATE_SLAVE;
2023 	} else {
2024 		state = MASTER_SLAVE_STATE_UNKNOWN;
2025 	}
2026 
2027 	phydev->master_slave_get = cfg;
2028 	phydev->master_slave_state = state;
2029 
2030 	return 0;
2031 }
2032 EXPORT_SYMBOL(genphy_read_master_slave);
2033 
2034 /**
2035  * genphy_restart_aneg - Enable and Restart Autonegotiation
2036  * @phydev: target phy_device struct
2037  */
genphy_restart_aneg(struct phy_device * phydev)2038 int genphy_restart_aneg(struct phy_device *phydev)
2039 {
2040 	/* Don't isolate the PHY if we're negotiating */
2041 	return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
2042 			  BMCR_ANENABLE | BMCR_ANRESTART);
2043 }
2044 EXPORT_SYMBOL(genphy_restart_aneg);
2045 
2046 /**
2047  * genphy_check_and_restart_aneg - Enable and restart auto-negotiation
2048  * @phydev: target phy_device struct
2049  * @restart: whether aneg restart is requested
2050  *
2051  * Check, and restart auto-negotiation if needed.
2052  */
genphy_check_and_restart_aneg(struct phy_device * phydev,bool restart)2053 int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart)
2054 {
2055 	int ret;
2056 
2057 	if (!restart) {
2058 		/* Advertisement hasn't changed, but maybe aneg was never on to
2059 		 * begin with?  Or maybe phy was isolated?
2060 		 */
2061 		ret = phy_read(phydev, MII_BMCR);
2062 		if (ret < 0)
2063 			return ret;
2064 
2065 		if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE))
2066 			restart = true;
2067 	}
2068 
2069 	if (restart)
2070 		return genphy_restart_aneg(phydev);
2071 
2072 	return 0;
2073 }
2074 EXPORT_SYMBOL(genphy_check_and_restart_aneg);
2075 
2076 /**
2077  * __genphy_config_aneg - restart auto-negotiation or write BMCR
2078  * @phydev: target phy_device struct
2079  * @changed: whether autoneg is requested
2080  *
2081  * Description: If auto-negotiation is enabled, we configure the
2082  *   advertising, and then restart auto-negotiation.  If it is not
2083  *   enabled, then we write the BMCR.
2084  */
__genphy_config_aneg(struct phy_device * phydev,bool changed)2085 int __genphy_config_aneg(struct phy_device *phydev, bool changed)
2086 {
2087 	__ETHTOOL_DECLARE_LINK_MODE_MASK(fixed_advert);
2088 	const struct link_capabilities *c;
2089 	unsigned long *advert;
2090 	int err;
2091 
2092 	err = genphy_c45_an_config_eee_aneg(phydev);
2093 	if (err < 0)
2094 		return err;
2095 	else if (err)
2096 		changed = true;
2097 
2098 	err = genphy_setup_master_slave(phydev);
2099 	if (err < 0)
2100 		return err;
2101 	else if (err)
2102 		changed = true;
2103 
2104 	if (phydev->autoneg == AUTONEG_ENABLE) {
2105 		/* Only allow advertising what this PHY supports */
2106 		linkmode_and(phydev->advertising, phydev->advertising,
2107 			     phydev->supported);
2108 		advert = phydev->advertising;
2109 	} else if (phydev->speed < SPEED_1000) {
2110 		return genphy_setup_forced(phydev);
2111 	} else {
2112 		linkmode_zero(fixed_advert);
2113 
2114 		c = phy_caps_lookup(phydev->speed, phydev->duplex,
2115 				    phydev->supported, true);
2116 		if (c)
2117 			linkmode_and(fixed_advert, phydev->supported,
2118 				     c->linkmodes);
2119 
2120 		advert = fixed_advert;
2121 	}
2122 
2123 	err = genphy_config_advert(phydev, advert);
2124 	if (err < 0) /* error */
2125 		return err;
2126 	else if (err)
2127 		changed = true;
2128 
2129 	return genphy_check_and_restart_aneg(phydev, changed);
2130 }
2131 EXPORT_SYMBOL(__genphy_config_aneg);
2132 
2133 /**
2134  * genphy_c37_config_aneg - restart auto-negotiation or write BMCR
2135  * @phydev: target phy_device struct
2136  *
2137  * Description: If auto-negotiation is enabled, we configure the
2138  *   advertising, and then restart auto-negotiation.  If it is not
2139  *   enabled, then we write the BMCR. This function is intended
2140  *   for use with Clause 37 1000Base-X mode.
2141  */
genphy_c37_config_aneg(struct phy_device * phydev)2142 int genphy_c37_config_aneg(struct phy_device *phydev)
2143 {
2144 	int err, changed;
2145 
2146 	if (phydev->autoneg != AUTONEG_ENABLE)
2147 		return genphy_setup_forced(phydev);
2148 
2149 	err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100,
2150 			 BMCR_SPEED1000);
2151 	if (err)
2152 		return err;
2153 
2154 	changed = genphy_c37_config_advert(phydev);
2155 	if (changed < 0) /* error */
2156 		return changed;
2157 
2158 	if (!changed) {
2159 		/* Advertisement hasn't changed, but maybe aneg was never on to
2160 		 * begin with?  Or maybe phy was isolated?
2161 		 */
2162 		int ctl = phy_read(phydev, MII_BMCR);
2163 
2164 		if (ctl < 0)
2165 			return ctl;
2166 
2167 		if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
2168 			changed = 1; /* do restart aneg */
2169 	}
2170 
2171 	/* Only restart aneg if we are advertising something different
2172 	 * than we were before.
2173 	 */
2174 	if (changed > 0)
2175 		return genphy_restart_aneg(phydev);
2176 
2177 	return 0;
2178 }
2179 EXPORT_SYMBOL(genphy_c37_config_aneg);
2180 
2181 /**
2182  * genphy_aneg_done - return auto-negotiation status
2183  * @phydev: target phy_device struct
2184  *
2185  * Description: Reads the status register and returns 0 either if
2186  *   auto-negotiation is incomplete, or if there was an error.
2187  *   Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
2188  */
genphy_aneg_done(struct phy_device * phydev)2189 int genphy_aneg_done(struct phy_device *phydev)
2190 {
2191 	int retval = phy_read(phydev, MII_BMSR);
2192 
2193 	return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
2194 }
2195 EXPORT_SYMBOL(genphy_aneg_done);
2196 
2197 /**
2198  * genphy_update_link - update link status in @phydev
2199  * @phydev: target phy_device struct
2200  *
2201  * Description: Update the value in phydev->link to reflect the
2202  *   current link value.  In order to do this, we need to read
2203  *   the status register twice, keeping the second value.
2204  */
genphy_update_link(struct phy_device * phydev)2205 int genphy_update_link(struct phy_device *phydev)
2206 {
2207 	int status = 0, bmcr;
2208 
2209 	bmcr = phy_read(phydev, MII_BMCR);
2210 	if (bmcr < 0)
2211 		return bmcr;
2212 
2213 	/* Autoneg is being started, therefore disregard BMSR value and
2214 	 * report link as down.
2215 	 */
2216 	if (bmcr & BMCR_ANRESTART)
2217 		goto done;
2218 
2219 	/* The link state is latched low so that momentary link
2220 	 * drops can be detected. Do not double-read the status
2221 	 * in polling mode to detect such short link drops except
2222 	 * the link was already down.
2223 	 */
2224 	if (!phy_polling_mode(phydev) || !phydev->link) {
2225 		status = phy_read(phydev, MII_BMSR);
2226 		if (status < 0)
2227 			return status;
2228 		else if (status & BMSR_LSTATUS)
2229 			goto done;
2230 	}
2231 
2232 	/* Read link and autonegotiation status */
2233 	status = phy_read(phydev, MII_BMSR);
2234 	if (status < 0)
2235 		return status;
2236 done:
2237 	phydev->link = status & BMSR_LSTATUS ? 1 : 0;
2238 	phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0;
2239 
2240 	/* Consider the case that autoneg was started and "aneg complete"
2241 	 * bit has been reset, but "link up" bit not yet.
2242 	 */
2243 	if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete)
2244 		phydev->link = 0;
2245 
2246 	return 0;
2247 }
2248 EXPORT_SYMBOL(genphy_update_link);
2249 
genphy_read_lpa(struct phy_device * phydev)2250 int genphy_read_lpa(struct phy_device *phydev)
2251 {
2252 	int lpa, lpagb;
2253 
2254 	if (phydev->autoneg == AUTONEG_ENABLE) {
2255 		if (!phydev->autoneg_complete) {
2256 			mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
2257 							0);
2258 			mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0);
2259 			return 0;
2260 		}
2261 
2262 		if (phydev->is_gigabit_capable) {
2263 			lpagb = phy_read(phydev, MII_STAT1000);
2264 			if (lpagb < 0)
2265 				return lpagb;
2266 
2267 			if (lpagb & LPA_1000MSFAIL) {
2268 				int adv = phy_read(phydev, MII_CTRL1000);
2269 
2270 				if (adv < 0)
2271 					return adv;
2272 
2273 				if (adv & CTL1000_ENABLE_MASTER)
2274 					phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
2275 				else
2276 					phydev_err(phydev, "Master/Slave resolution failed\n");
2277 				return -ENOLINK;
2278 			}
2279 
2280 			mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
2281 							lpagb);
2282 		}
2283 
2284 		lpa = phy_read(phydev, MII_LPA);
2285 		if (lpa < 0)
2286 			return lpa;
2287 
2288 		mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa);
2289 	} else {
2290 		linkmode_zero(phydev->lp_advertising);
2291 	}
2292 
2293 	return 0;
2294 }
2295 EXPORT_SYMBOL(genphy_read_lpa);
2296 
2297 /**
2298  * genphy_read_status_fixed - read the link parameters for !aneg mode
2299  * @phydev: target phy_device struct
2300  *
2301  * Read the current duplex and speed state for a PHY operating with
2302  * autonegotiation disabled.
2303  */
genphy_read_status_fixed(struct phy_device * phydev)2304 int genphy_read_status_fixed(struct phy_device *phydev)
2305 {
2306 	int bmcr = phy_read(phydev, MII_BMCR);
2307 
2308 	if (bmcr < 0)
2309 		return bmcr;
2310 
2311 	if (bmcr & BMCR_FULLDPLX)
2312 		phydev->duplex = DUPLEX_FULL;
2313 	else
2314 		phydev->duplex = DUPLEX_HALF;
2315 
2316 	if (bmcr & BMCR_SPEED1000)
2317 		phydev->speed = SPEED_1000;
2318 	else if (bmcr & BMCR_SPEED100)
2319 		phydev->speed = SPEED_100;
2320 	else
2321 		phydev->speed = SPEED_10;
2322 
2323 	return 0;
2324 }
2325 EXPORT_SYMBOL(genphy_read_status_fixed);
2326 
2327 /**
2328  * genphy_read_status - check the link status and update current link state
2329  * @phydev: target phy_device struct
2330  *
2331  * Description: Check the link, then figure out the current state
2332  *   by comparing what we advertise with what the link partner
2333  *   advertises.  Start by checking the gigabit possibilities,
2334  *   then move on to 10/100.
2335  */
genphy_read_status(struct phy_device * phydev)2336 int genphy_read_status(struct phy_device *phydev)
2337 {
2338 	int err, old_link = phydev->link;
2339 
2340 	/* Update the link, but return if there was an error */
2341 	err = genphy_update_link(phydev);
2342 	if (err)
2343 		return err;
2344 
2345 	/* why bother the PHY if nothing can have changed */
2346 	if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
2347 		return 0;
2348 
2349 	phydev->master_slave_get = MASTER_SLAVE_CFG_UNSUPPORTED;
2350 	phydev->master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
2351 	phydev->speed = SPEED_UNKNOWN;
2352 	phydev->duplex = DUPLEX_UNKNOWN;
2353 	phydev->pause = 0;
2354 	phydev->asym_pause = 0;
2355 
2356 	if (phydev->is_gigabit_capable) {
2357 		err = genphy_read_master_slave(phydev);
2358 		if (err < 0)
2359 			return err;
2360 	}
2361 
2362 	err = genphy_read_lpa(phydev);
2363 	if (err < 0)
2364 		return err;
2365 
2366 	if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2367 		phy_resolve_aneg_linkmode(phydev);
2368 	} else if (phydev->autoneg == AUTONEG_DISABLE) {
2369 		err = genphy_read_status_fixed(phydev);
2370 		if (err < 0)
2371 			return err;
2372 	}
2373 
2374 	return 0;
2375 }
2376 EXPORT_SYMBOL(genphy_read_status);
2377 
2378 /**
2379  * genphy_c37_read_status - check the link status and update current link state
2380  * @phydev: target phy_device struct
2381  * @changed: pointer where to store if link changed
2382  *
2383  * Description: Check the link, then figure out the current state
2384  *   by comparing what we advertise with what the link partner
2385  *   advertises. This function is for Clause 37 1000Base-X mode.
2386  *
2387  *   If link has changed, @changed is set to true, false otherwise.
2388  */
genphy_c37_read_status(struct phy_device * phydev,bool * changed)2389 int genphy_c37_read_status(struct phy_device *phydev, bool *changed)
2390 {
2391 	int lpa, err, old_link = phydev->link;
2392 
2393 	/* Update the link, but return if there was an error */
2394 	err = genphy_update_link(phydev);
2395 	if (err)
2396 		return err;
2397 
2398 	/* why bother the PHY if nothing can have changed */
2399 	if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) {
2400 		*changed = false;
2401 		return 0;
2402 	}
2403 
2404 	/* Signal link has changed */
2405 	*changed = true;
2406 	phydev->duplex = DUPLEX_UNKNOWN;
2407 	phydev->pause = 0;
2408 	phydev->asym_pause = 0;
2409 
2410 	if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2411 		lpa = phy_read(phydev, MII_LPA);
2412 		if (lpa < 0)
2413 			return lpa;
2414 
2415 		linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2416 				 phydev->lp_advertising, lpa & LPA_LPACK);
2417 		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2418 				 phydev->lp_advertising, lpa & LPA_1000XFULL);
2419 		linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2420 				 phydev->lp_advertising, lpa & LPA_1000XPAUSE);
2421 		linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2422 				 phydev->lp_advertising,
2423 				 lpa & LPA_1000XPAUSE_ASYM);
2424 
2425 		phy_resolve_aneg_linkmode(phydev);
2426 	} else if (phydev->autoneg == AUTONEG_DISABLE) {
2427 		int bmcr = phy_read(phydev, MII_BMCR);
2428 
2429 		if (bmcr < 0)
2430 			return bmcr;
2431 
2432 		if (bmcr & BMCR_FULLDPLX)
2433 			phydev->duplex = DUPLEX_FULL;
2434 		else
2435 			phydev->duplex = DUPLEX_HALF;
2436 	}
2437 
2438 	return 0;
2439 }
2440 EXPORT_SYMBOL(genphy_c37_read_status);
2441 
2442 /**
2443  * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
2444  * @phydev: target phy_device struct
2445  *
2446  * Description: Perform a software PHY reset using the standard
2447  * BMCR_RESET bit and poll for the reset bit to be cleared.
2448  *
2449  * Returns: 0 on success, < 0 on failure
2450  */
genphy_soft_reset(struct phy_device * phydev)2451 int genphy_soft_reset(struct phy_device *phydev)
2452 {
2453 	u16 res = BMCR_RESET;
2454 	int ret;
2455 
2456 	if (phydev->autoneg == AUTONEG_ENABLE)
2457 		res |= BMCR_ANRESTART;
2458 
2459 	ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res);
2460 	if (ret < 0)
2461 		return ret;
2462 
2463 	/* Clause 22 states that setting bit BMCR_RESET sets control registers
2464 	 * to their default value. Therefore the POWER DOWN bit is supposed to
2465 	 * be cleared after soft reset.
2466 	 */
2467 	phydev->suspended = 0;
2468 
2469 	ret = phy_poll_reset(phydev);
2470 	if (ret)
2471 		return ret;
2472 
2473 	/* BMCR may be reset to defaults */
2474 	if (phydev->autoneg == AUTONEG_DISABLE)
2475 		ret = genphy_setup_forced(phydev);
2476 
2477 	return ret;
2478 }
2479 EXPORT_SYMBOL(genphy_soft_reset);
2480 
genphy_handle_interrupt_no_ack(struct phy_device * phydev)2481 irqreturn_t genphy_handle_interrupt_no_ack(struct phy_device *phydev)
2482 {
2483 	/* It seems there are cases where the interrupts are handled by another
2484 	 * entity (ie an IRQ controller embedded inside the PHY) and do not
2485 	 * need any other interraction from phylib. In this case, just trigger
2486 	 * the state machine directly.
2487 	 */
2488 	phy_trigger_machine(phydev);
2489 
2490 	return 0;
2491 }
2492 EXPORT_SYMBOL(genphy_handle_interrupt_no_ack);
2493 
2494 /**
2495  * genphy_read_abilities - read PHY abilities from Clause 22 registers
2496  * @phydev: target phy_device struct
2497  *
2498  * Description: Reads the PHY's abilities and populates
2499  * phydev->supported accordingly.
2500  *
2501  * Returns: 0 on success, < 0 on failure
2502  */
genphy_read_abilities(struct phy_device * phydev)2503 int genphy_read_abilities(struct phy_device *phydev)
2504 {
2505 	int val;
2506 
2507 	linkmode_set_bit_array(phy_basic_ports_array,
2508 			       ARRAY_SIZE(phy_basic_ports_array),
2509 			       phydev->supported);
2510 
2511 	val = phy_read(phydev, MII_BMSR);
2512 	if (val < 0)
2513 		return val;
2514 
2515 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported,
2516 			 val & BMSR_ANEGCAPABLE);
2517 
2518 	linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported,
2519 			 val & BMSR_100FULL);
2520 	linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported,
2521 			 val & BMSR_100HALF);
2522 	linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported,
2523 			 val & BMSR_10FULL);
2524 	linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported,
2525 			 val & BMSR_10HALF);
2526 
2527 	if (val & BMSR_ESTATEN) {
2528 		val = phy_read(phydev, MII_ESTATUS);
2529 		if (val < 0)
2530 			return val;
2531 
2532 		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2533 				 phydev->supported, val & ESTATUS_1000_TFULL);
2534 		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
2535 				 phydev->supported, val & ESTATUS_1000_THALF);
2536 		linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2537 				 phydev->supported, val & ESTATUS_1000_XFULL);
2538 	}
2539 
2540 	/* This is optional functionality. If not supported, we may get an error
2541 	 * which should be ignored.
2542 	 */
2543 	genphy_c45_read_eee_abilities(phydev);
2544 
2545 	return 0;
2546 }
2547 EXPORT_SYMBOL(genphy_read_abilities);
2548 
2549 /* This is used for the phy device which doesn't support the MMD extended
2550  * register access, but it does have side effect when we are trying to access
2551  * the MMD register via indirect method.
2552  */
genphy_read_mmd_unsupported(struct phy_device * phdev,int devad,u16 regnum)2553 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
2554 {
2555 	return -EOPNOTSUPP;
2556 }
2557 EXPORT_SYMBOL(genphy_read_mmd_unsupported);
2558 
genphy_write_mmd_unsupported(struct phy_device * phdev,int devnum,u16 regnum,u16 val)2559 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
2560 				 u16 regnum, u16 val)
2561 {
2562 	return -EOPNOTSUPP;
2563 }
2564 EXPORT_SYMBOL(genphy_write_mmd_unsupported);
2565 
genphy_suspend(struct phy_device * phydev)2566 int genphy_suspend(struct phy_device *phydev)
2567 {
2568 	return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
2569 }
2570 EXPORT_SYMBOL(genphy_suspend);
2571 
genphy_resume(struct phy_device * phydev)2572 int genphy_resume(struct phy_device *phydev)
2573 {
2574 	return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
2575 }
2576 EXPORT_SYMBOL(genphy_resume);
2577 
genphy_loopback(struct phy_device * phydev,bool enable,int speed)2578 int genphy_loopback(struct phy_device *phydev, bool enable, int speed)
2579 {
2580 	if (enable) {
2581 		u16 ctl = BMCR_LOOPBACK;
2582 		int ret, val;
2583 
2584 		if (speed == SPEED_10 || speed == SPEED_100 ||
2585 		    speed == SPEED_1000)
2586 			phydev->speed = speed;
2587 		else if (speed)
2588 			return -EINVAL;
2589 
2590 		ctl |= mii_bmcr_encode_fixed(phydev->speed, phydev->duplex);
2591 
2592 		phy_modify(phydev, MII_BMCR, ~0, ctl);
2593 
2594 		ret = phy_read_poll_timeout(phydev, MII_BMSR, val,
2595 					    val & BMSR_LSTATUS,
2596 				    5000, 500000, true);
2597 		if (ret)
2598 			return ret;
2599 	} else {
2600 		phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK, 0);
2601 
2602 		phy_config_aneg(phydev);
2603 	}
2604 
2605 	return 0;
2606 }
2607 EXPORT_SYMBOL(genphy_loopback);
2608 
2609 /**
2610  * phy_remove_link_mode - Remove a supported link mode
2611  * @phydev: phy_device structure to remove link mode from
2612  * @link_mode: Link mode to be removed
2613  *
2614  * Description: Some MACs don't support all link modes which the PHY
2615  * does.  e.g. a 1G MAC often does not support 1000Half. Add a helper
2616  * to remove a link mode.
2617  */
phy_remove_link_mode(struct phy_device * phydev,u32 link_mode)2618 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode)
2619 {
2620 	linkmode_clear_bit(link_mode, phydev->supported);
2621 	phy_advertise_supported(phydev);
2622 }
2623 EXPORT_SYMBOL(phy_remove_link_mode);
2624 
phy_copy_pause_bits(unsigned long * dst,unsigned long * src)2625 static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src)
2626 {
2627 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst,
2628 		linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src));
2629 	linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst,
2630 		linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src));
2631 }
2632 
2633 /**
2634  * phy_advertise_supported - Advertise all supported modes
2635  * @phydev: target phy_device struct
2636  *
2637  * Description: Called to advertise all supported modes, doesn't touch
2638  * pause mode advertising.
2639  */
phy_advertise_supported(struct phy_device * phydev)2640 void phy_advertise_supported(struct phy_device *phydev)
2641 {
2642 	__ETHTOOL_DECLARE_LINK_MODE_MASK(new);
2643 
2644 	linkmode_copy(new, phydev->supported);
2645 	phy_copy_pause_bits(new, phydev->advertising);
2646 	linkmode_copy(phydev->advertising, new);
2647 }
2648 EXPORT_SYMBOL(phy_advertise_supported);
2649 
2650 /**
2651  * phy_advertise_eee_all - Advertise all supported EEE modes
2652  * @phydev: target phy_device struct
2653  *
2654  * Description: Per default phylib preserves the EEE advertising at the time of
2655  * phy probing, which might be a subset of the supported EEE modes. Use this
2656  * function when all supported EEE modes should be advertised. This does not
2657  * trigger auto-negotiation, so must be called before phy_start()/
2658  * phylink_start() which will start auto-negotiation.
2659  */
phy_advertise_eee_all(struct phy_device * phydev)2660 void phy_advertise_eee_all(struct phy_device *phydev)
2661 {
2662 	linkmode_copy(phydev->advertising_eee, phydev->supported_eee);
2663 }
2664 EXPORT_SYMBOL_GPL(phy_advertise_eee_all);
2665 
2666 /**
2667  * phy_support_eee - Set initial EEE policy configuration
2668  * @phydev: Target phy_device struct
2669  *
2670  * This function configures the initial policy for Energy Efficient Ethernet
2671  * (EEE) on the specified PHY device, influencing that EEE capabilities are
2672  * advertised before the link is established. It should be called during PHY
2673  * registration by the MAC driver and/or the PHY driver (for SmartEEE PHYs)
2674  * if MAC supports LPI or PHY is capable to compensate missing LPI functionality
2675  * of the MAC.
2676  *
2677  * The function sets default EEE policy parameters, including preparing the PHY
2678  * to advertise EEE capabilities based on hardware support.
2679  *
2680  * It also sets the expected configuration for Low Power Idle (LPI) in the MAC
2681  * driver. If the PHY framework determines that both local and remote
2682  * advertisements support EEE, and the negotiated link mode is compatible with
2683  * EEE, it will set enable_tx_lpi = true. The MAC driver is expected to act on
2684  * this setting by enabling the LPI timer if enable_tx_lpi is set.
2685  */
phy_support_eee(struct phy_device * phydev)2686 void phy_support_eee(struct phy_device *phydev)
2687 {
2688 	linkmode_copy(phydev->advertising_eee, phydev->supported_eee);
2689 	phydev->eee_cfg.tx_lpi_enabled = true;
2690 	phydev->eee_cfg.eee_enabled = true;
2691 }
2692 EXPORT_SYMBOL(phy_support_eee);
2693 
2694 /**
2695  * phy_disable_eee - Disable EEE for the PHY
2696  * @phydev: Target phy_device struct
2697  *
2698  * This function is used by MAC drivers for MAC's which don't support EEE.
2699  * It disables EEE on the PHY layer.
2700  */
phy_disable_eee(struct phy_device * phydev)2701 void phy_disable_eee(struct phy_device *phydev)
2702 {
2703 	linkmode_zero(phydev->advertising_eee);
2704 	phydev->eee_cfg.tx_lpi_enabled = false;
2705 	phydev->eee_cfg.eee_enabled = false;
2706 	/* don't let userspace re-enable EEE advertisement */
2707 	linkmode_fill(phydev->eee_disabled_modes);
2708 }
2709 EXPORT_SYMBOL_GPL(phy_disable_eee);
2710 
2711 /**
2712  * phy_support_sym_pause - Enable support of symmetrical pause
2713  * @phydev: target phy_device struct
2714  *
2715  * Description: Called by the MAC to indicate is supports symmetrical
2716  * Pause, but not asym pause.
2717  */
phy_support_sym_pause(struct phy_device * phydev)2718 void phy_support_sym_pause(struct phy_device *phydev)
2719 {
2720 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
2721 	phy_copy_pause_bits(phydev->advertising, phydev->supported);
2722 }
2723 EXPORT_SYMBOL(phy_support_sym_pause);
2724 
2725 /**
2726  * phy_support_asym_pause - Enable support of asym pause
2727  * @phydev: target phy_device struct
2728  *
2729  * Description: Called by the MAC to indicate is supports Asym Pause.
2730  */
phy_support_asym_pause(struct phy_device * phydev)2731 void phy_support_asym_pause(struct phy_device *phydev)
2732 {
2733 	phy_copy_pause_bits(phydev->advertising, phydev->supported);
2734 }
2735 EXPORT_SYMBOL(phy_support_asym_pause);
2736 
2737 /**
2738  * phy_set_sym_pause - Configure symmetric Pause
2739  * @phydev: target phy_device struct
2740  * @rx: Receiver Pause is supported
2741  * @tx: Transmit Pause is supported
2742  * @autoneg: Auto neg should be used
2743  *
2744  * Description: Configure advertised Pause support depending on if
2745  * receiver pause and pause auto neg is supported. Generally called
2746  * from the set_pauseparam .ndo.
2747  */
phy_set_sym_pause(struct phy_device * phydev,bool rx,bool tx,bool autoneg)2748 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
2749 		       bool autoneg)
2750 {
2751 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
2752 
2753 	if (rx && tx && autoneg)
2754 		linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2755 				 phydev->supported);
2756 
2757 	linkmode_copy(phydev->advertising, phydev->supported);
2758 }
2759 EXPORT_SYMBOL(phy_set_sym_pause);
2760 
2761 /**
2762  * phy_set_asym_pause - Configure Pause and Asym Pause
2763  * @phydev: target phy_device struct
2764  * @rx: Receiver Pause is supported
2765  * @tx: Transmit Pause is supported
2766  *
2767  * Description: Configure advertised Pause support depending on if
2768  * transmit and receiver pause is supported. If there has been a
2769  * change in adverting, trigger a new autoneg. Generally called from
2770  * the set_pauseparam .ndo.
2771  */
phy_set_asym_pause(struct phy_device * phydev,bool rx,bool tx)2772 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx)
2773 {
2774 	__ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv);
2775 
2776 	linkmode_copy(oldadv, phydev->advertising);
2777 	linkmode_set_pause(phydev->advertising, tx, rx);
2778 
2779 	if (!linkmode_equal(oldadv, phydev->advertising) &&
2780 	    phydev->autoneg)
2781 		phy_start_aneg(phydev);
2782 }
2783 EXPORT_SYMBOL(phy_set_asym_pause);
2784 
2785 /**
2786  * phy_validate_pause - Test if the PHY/MAC support the pause configuration
2787  * @phydev: phy_device struct
2788  * @pp: requested pause configuration
2789  *
2790  * Description: Test if the PHY/MAC combination supports the Pause
2791  * configuration the user is requesting. Returns True if it is
2792  * supported, false otherwise.
2793  */
phy_validate_pause(struct phy_device * phydev,struct ethtool_pauseparam * pp)2794 bool phy_validate_pause(struct phy_device *phydev,
2795 			struct ethtool_pauseparam *pp)
2796 {
2797 	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2798 			       phydev->supported) && pp->rx_pause)
2799 		return false;
2800 
2801 	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2802 			       phydev->supported) &&
2803 	    pp->rx_pause != pp->tx_pause)
2804 		return false;
2805 
2806 	return true;
2807 }
2808 EXPORT_SYMBOL(phy_validate_pause);
2809 
2810 /**
2811  * phy_get_pause - resolve negotiated pause modes
2812  * @phydev: phy_device struct
2813  * @tx_pause: pointer to bool to indicate whether transmit pause should be
2814  * enabled.
2815  * @rx_pause: pointer to bool to indicate whether receive pause should be
2816  * enabled.
2817  *
2818  * Resolve and return the flow control modes according to the negotiation
2819  * result. This includes checking that we are operating in full duplex mode.
2820  * See linkmode_resolve_pause() for further details.
2821  */
phy_get_pause(struct phy_device * phydev,bool * tx_pause,bool * rx_pause)2822 void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause)
2823 {
2824 	if (phydev->duplex != DUPLEX_FULL) {
2825 		*tx_pause = false;
2826 		*rx_pause = false;
2827 		return;
2828 	}
2829 
2830 	return linkmode_resolve_pause(phydev->advertising,
2831 				      phydev->lp_advertising,
2832 				      tx_pause, rx_pause);
2833 }
2834 EXPORT_SYMBOL(phy_get_pause);
2835 
2836 #if IS_ENABLED(CONFIG_OF_MDIO)
phy_get_u32_property(struct device * dev,const char * name,u32 * val)2837 static int phy_get_u32_property(struct device *dev, const char *name, u32 *val)
2838 {
2839 	return device_property_read_u32(dev, name, val);
2840 }
2841 #else
phy_get_u32_property(struct device * dev,const char * name,u32 * val)2842 static int phy_get_u32_property(struct device *dev, const char *name, u32 *val)
2843 {
2844 	return -EINVAL;
2845 }
2846 #endif
2847 
2848 /**
2849  * phy_get_internal_delay - returns the index of the internal delay
2850  * @phydev: phy_device struct
2851  * @dev: pointer to the devices device struct
2852  * @delay_values: array of delays the PHY supports
2853  * @size: the size of the delay array
2854  * @is_rx: boolean to indicate to get the rx internal delay
2855  *
2856  * Returns the index within the array of internal delay passed in.
2857  * If the device property is not present then the interface type is checked
2858  * if the interface defines use of internal delay then a 1 is returned otherwise
2859  * a 0 is returned.
2860  * The array must be in ascending order. If PHY does not have an ascending order
2861  * array then size = 0 and the value of the delay property is returned.
2862  * Return -EINVAL if the delay is invalid or cannot be found.
2863  */
phy_get_internal_delay(struct phy_device * phydev,struct device * dev,const int * delay_values,int size,bool is_rx)2864 s32 phy_get_internal_delay(struct phy_device *phydev, struct device *dev,
2865 			   const int *delay_values, int size, bool is_rx)
2866 {
2867 	int i, ret;
2868 	u32 delay;
2869 
2870 	if (is_rx) {
2871 		ret = phy_get_u32_property(dev, "rx-internal-delay-ps", &delay);
2872 		if (ret < 0 && size == 0) {
2873 			if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
2874 			    phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
2875 				return 1;
2876 			else
2877 				return 0;
2878 		}
2879 
2880 	} else {
2881 		ret = phy_get_u32_property(dev, "tx-internal-delay-ps", &delay);
2882 		if (ret < 0 && size == 0) {
2883 			if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
2884 			    phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
2885 				return 1;
2886 			else
2887 				return 0;
2888 		}
2889 	}
2890 
2891 	if (ret < 0)
2892 		return ret;
2893 
2894 	if (size == 0)
2895 		return delay;
2896 
2897 	if (delay < delay_values[0] || delay > delay_values[size - 1]) {
2898 		phydev_err(phydev, "Delay %d is out of range\n", delay);
2899 		return -EINVAL;
2900 	}
2901 
2902 	if (delay == delay_values[0])
2903 		return 0;
2904 
2905 	for (i = 1; i < size; i++) {
2906 		if (delay == delay_values[i])
2907 			return i;
2908 
2909 		/* Find an approximate index by looking up the table */
2910 		if (delay > delay_values[i - 1] &&
2911 		    delay < delay_values[i]) {
2912 			if (delay - delay_values[i - 1] <
2913 			    delay_values[i] - delay)
2914 				return i - 1;
2915 			else
2916 				return i;
2917 		}
2918 	}
2919 
2920 	phydev_err(phydev, "error finding internal delay index for %d\n",
2921 		   delay);
2922 
2923 	return -EINVAL;
2924 }
2925 EXPORT_SYMBOL(phy_get_internal_delay);
2926 
2927 /**
2928  * phy_get_tx_amplitude_gain - stores tx amplitude gain in @val
2929  * @phydev: phy_device struct
2930  * @dev: pointer to the devices device struct
2931  * @linkmode: linkmode for which the tx amplitude gain should be retrieved
2932  * @val: tx amplitude gain
2933  *
2934  * Returns: 0 on success, < 0 on failure
2935  */
phy_get_tx_amplitude_gain(struct phy_device * phydev,struct device * dev,enum ethtool_link_mode_bit_indices linkmode,u32 * val)2936 int phy_get_tx_amplitude_gain(struct phy_device *phydev, struct device *dev,
2937 			      enum ethtool_link_mode_bit_indices linkmode,
2938 			      u32 *val)
2939 {
2940 	switch (linkmode) {
2941 	case ETHTOOL_LINK_MODE_100baseT_Full_BIT:
2942 		return phy_get_u32_property(dev,
2943 					    "tx-amplitude-100base-tx-percent",
2944 					    val);
2945 	default:
2946 		return -EINVAL;
2947 	}
2948 }
2949 EXPORT_SYMBOL_GPL(phy_get_tx_amplitude_gain);
2950 
phy_led_set_brightness(struct led_classdev * led_cdev,enum led_brightness value)2951 static int phy_led_set_brightness(struct led_classdev *led_cdev,
2952 				  enum led_brightness value)
2953 {
2954 	struct phy_led *phyled = to_phy_led(led_cdev);
2955 	struct phy_device *phydev = phyled->phydev;
2956 	int err;
2957 
2958 	mutex_lock(&phydev->lock);
2959 	err = phydev->drv->led_brightness_set(phydev, phyled->index, value);
2960 	mutex_unlock(&phydev->lock);
2961 
2962 	return err;
2963 }
2964 
phy_led_blink_set(struct led_classdev * led_cdev,unsigned long * delay_on,unsigned long * delay_off)2965 static int phy_led_blink_set(struct led_classdev *led_cdev,
2966 			     unsigned long *delay_on,
2967 			     unsigned long *delay_off)
2968 {
2969 	struct phy_led *phyled = to_phy_led(led_cdev);
2970 	struct phy_device *phydev = phyled->phydev;
2971 	int err;
2972 
2973 	mutex_lock(&phydev->lock);
2974 	err = phydev->drv->led_blink_set(phydev, phyled->index,
2975 					 delay_on, delay_off);
2976 	mutex_unlock(&phydev->lock);
2977 
2978 	return err;
2979 }
2980 
2981 static __maybe_unused struct device *
phy_led_hw_control_get_device(struct led_classdev * led_cdev)2982 phy_led_hw_control_get_device(struct led_classdev *led_cdev)
2983 {
2984 	struct phy_led *phyled = to_phy_led(led_cdev);
2985 	struct phy_device *phydev = phyled->phydev;
2986 
2987 	if (phydev->attached_dev)
2988 		return &phydev->attached_dev->dev;
2989 	return NULL;
2990 }
2991 
2992 static int __maybe_unused
phy_led_hw_control_get(struct led_classdev * led_cdev,unsigned long * rules)2993 phy_led_hw_control_get(struct led_classdev *led_cdev,
2994 		       unsigned long *rules)
2995 {
2996 	struct phy_led *phyled = to_phy_led(led_cdev);
2997 	struct phy_device *phydev = phyled->phydev;
2998 	int err;
2999 
3000 	mutex_lock(&phydev->lock);
3001 	err = phydev->drv->led_hw_control_get(phydev, phyled->index, rules);
3002 	mutex_unlock(&phydev->lock);
3003 
3004 	return err;
3005 }
3006 
3007 static int __maybe_unused
phy_led_hw_control_set(struct led_classdev * led_cdev,unsigned long rules)3008 phy_led_hw_control_set(struct led_classdev *led_cdev,
3009 		       unsigned long rules)
3010 {
3011 	struct phy_led *phyled = to_phy_led(led_cdev);
3012 	struct phy_device *phydev = phyled->phydev;
3013 	int err;
3014 
3015 	mutex_lock(&phydev->lock);
3016 	err = phydev->drv->led_hw_control_set(phydev, phyled->index, rules);
3017 	mutex_unlock(&phydev->lock);
3018 
3019 	return err;
3020 }
3021 
phy_led_hw_is_supported(struct led_classdev * led_cdev,unsigned long rules)3022 static __maybe_unused int phy_led_hw_is_supported(struct led_classdev *led_cdev,
3023 						  unsigned long rules)
3024 {
3025 	struct phy_led *phyled = to_phy_led(led_cdev);
3026 	struct phy_device *phydev = phyled->phydev;
3027 	int err;
3028 
3029 	mutex_lock(&phydev->lock);
3030 	err = phydev->drv->led_hw_is_supported(phydev, phyled->index, rules);
3031 	mutex_unlock(&phydev->lock);
3032 
3033 	return err;
3034 }
3035 
phy_leds_unregister(struct phy_device * phydev)3036 static void phy_leds_unregister(struct phy_device *phydev)
3037 {
3038 	struct phy_led *phyled, *tmp;
3039 
3040 	list_for_each_entry_safe(phyled, tmp, &phydev->leds, list) {
3041 		led_classdev_unregister(&phyled->led_cdev);
3042 		list_del(&phyled->list);
3043 	}
3044 }
3045 
of_phy_led(struct phy_device * phydev,struct device_node * led)3046 static int of_phy_led(struct phy_device *phydev,
3047 		      struct device_node *led)
3048 {
3049 	struct device *dev = &phydev->mdio.dev;
3050 	struct led_init_data init_data = {};
3051 	struct led_classdev *cdev;
3052 	unsigned long modes = 0;
3053 	struct phy_led *phyled;
3054 	u32 index;
3055 	int err;
3056 
3057 	phyled = devm_kzalloc(dev, sizeof(*phyled), GFP_KERNEL);
3058 	if (!phyled)
3059 		return -ENOMEM;
3060 
3061 	cdev = &phyled->led_cdev;
3062 	phyled->phydev = phydev;
3063 
3064 	err = of_property_read_u32(led, "reg", &index);
3065 	if (err)
3066 		return err;
3067 	if (index > U8_MAX)
3068 		return -EINVAL;
3069 
3070 	if (of_property_read_bool(led, "active-high"))
3071 		set_bit(PHY_LED_ACTIVE_HIGH, &modes);
3072 	if (of_property_read_bool(led, "active-low"))
3073 		set_bit(PHY_LED_ACTIVE_LOW, &modes);
3074 	if (of_property_read_bool(led, "inactive-high-impedance"))
3075 		set_bit(PHY_LED_INACTIVE_HIGH_IMPEDANCE, &modes);
3076 
3077 	if (WARN_ON(modes & BIT(PHY_LED_ACTIVE_LOW) &&
3078 		    modes & BIT(PHY_LED_ACTIVE_HIGH)))
3079 		return -EINVAL;
3080 
3081 	if (modes) {
3082 		/* Return error if asked to set polarity modes but not supported */
3083 		if (!phydev->drv->led_polarity_set)
3084 			return -EINVAL;
3085 
3086 		err = phydev->drv->led_polarity_set(phydev, index, modes);
3087 		if (err)
3088 			return err;
3089 	}
3090 
3091 	phyled->index = index;
3092 	if (phydev->drv->led_brightness_set)
3093 		cdev->brightness_set_blocking = phy_led_set_brightness;
3094 	if (phydev->drv->led_blink_set)
3095 		cdev->blink_set = phy_led_blink_set;
3096 
3097 #ifdef CONFIG_LEDS_TRIGGERS
3098 	if (phydev->drv->led_hw_is_supported &&
3099 	    phydev->drv->led_hw_control_set &&
3100 	    phydev->drv->led_hw_control_get) {
3101 		cdev->hw_control_is_supported = phy_led_hw_is_supported;
3102 		cdev->hw_control_set = phy_led_hw_control_set;
3103 		cdev->hw_control_get = phy_led_hw_control_get;
3104 		cdev->hw_control_trigger = "netdev";
3105 	}
3106 
3107 	cdev->hw_control_get_device = phy_led_hw_control_get_device;
3108 #endif
3109 	cdev->max_brightness = 1;
3110 	init_data.devicename = dev_name(&phydev->mdio.dev);
3111 	init_data.fwnode = of_fwnode_handle(led);
3112 	init_data.devname_mandatory = true;
3113 
3114 	err = led_classdev_register_ext(dev, cdev, &init_data);
3115 	if (err)
3116 		return err;
3117 
3118 	list_add(&phyled->list, &phydev->leds);
3119 
3120 	return 0;
3121 }
3122 
of_phy_leds(struct phy_device * phydev)3123 static int of_phy_leds(struct phy_device *phydev)
3124 {
3125 	struct device_node *node = phydev->mdio.dev.of_node;
3126 	struct device_node *leds;
3127 	int err;
3128 
3129 	if (!IS_ENABLED(CONFIG_OF_MDIO))
3130 		return 0;
3131 
3132 	if (!node)
3133 		return 0;
3134 
3135 	leds = of_get_child_by_name(node, "leds");
3136 	if (!leds)
3137 		return 0;
3138 
3139 	/* Check if the PHY driver have at least an OP to
3140 	 * set the LEDs.
3141 	 */
3142 	if (!(phydev->drv->led_brightness_set ||
3143 	      phydev->drv->led_blink_set ||
3144 	      phydev->drv->led_hw_control_set)) {
3145 		phydev_dbg(phydev, "ignoring leds node defined with no PHY driver support\n");
3146 		goto exit;
3147 	}
3148 
3149 	for_each_available_child_of_node_scoped(leds, led) {
3150 		err = of_phy_led(phydev, led);
3151 		if (err) {
3152 			of_node_put(leds);
3153 			phy_leds_unregister(phydev);
3154 			return err;
3155 		}
3156 	}
3157 
3158 exit:
3159 	of_node_put(leds);
3160 	return 0;
3161 }
3162 
3163 /**
3164  * fwnode_mdio_find_device - Given a fwnode, find the mdio_device
3165  * @fwnode: pointer to the mdio_device's fwnode
3166  *
3167  * If successful, returns a pointer to the mdio_device with the embedded
3168  * struct device refcount incremented by one, or NULL on failure.
3169  * The caller should call put_device() on the mdio_device after its use.
3170  */
fwnode_mdio_find_device(struct fwnode_handle * fwnode)3171 struct mdio_device *fwnode_mdio_find_device(struct fwnode_handle *fwnode)
3172 {
3173 	struct device *d;
3174 
3175 	if (!fwnode)
3176 		return NULL;
3177 
3178 	d = bus_find_device_by_fwnode(&mdio_bus_type, fwnode);
3179 	if (!d)
3180 		return NULL;
3181 
3182 	return to_mdio_device(d);
3183 }
3184 EXPORT_SYMBOL(fwnode_mdio_find_device);
3185 
3186 /**
3187  * fwnode_phy_find_device - For provided phy_fwnode, find phy_device.
3188  *
3189  * @phy_fwnode: Pointer to the phy's fwnode.
3190  *
3191  * If successful, returns a pointer to the phy_device with the embedded
3192  * struct device refcount incremented by one, or NULL on failure.
3193  */
fwnode_phy_find_device(struct fwnode_handle * phy_fwnode)3194 struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode)
3195 {
3196 	struct mdio_device *mdiodev;
3197 
3198 	mdiodev = fwnode_mdio_find_device(phy_fwnode);
3199 	if (!mdiodev)
3200 		return NULL;
3201 
3202 	if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY)
3203 		return to_phy_device(&mdiodev->dev);
3204 
3205 	put_device(&mdiodev->dev);
3206 
3207 	return NULL;
3208 }
3209 EXPORT_SYMBOL(fwnode_phy_find_device);
3210 
3211 /**
3212  * device_phy_find_device - For the given device, get the phy_device
3213  * @dev: Pointer to the given device
3214  *
3215  * Refer return conditions of fwnode_phy_find_device().
3216  */
device_phy_find_device(struct device * dev)3217 struct phy_device *device_phy_find_device(struct device *dev)
3218 {
3219 	return fwnode_phy_find_device(dev_fwnode(dev));
3220 }
3221 EXPORT_SYMBOL_GPL(device_phy_find_device);
3222 
3223 /**
3224  * fwnode_get_phy_node - Get the phy_node using the named reference.
3225  * @fwnode: Pointer to fwnode from which phy_node has to be obtained.
3226  *
3227  * Refer return conditions of fwnode_find_reference().
3228  * For ACPI, only "phy-handle" is supported. Legacy DT properties "phy"
3229  * and "phy-device" are not supported in ACPI. DT supports all the three
3230  * named references to the phy node.
3231  */
fwnode_get_phy_node(const struct fwnode_handle * fwnode)3232 struct fwnode_handle *fwnode_get_phy_node(const struct fwnode_handle *fwnode)
3233 {
3234 	struct fwnode_handle *phy_node;
3235 
3236 	/* Only phy-handle is used for ACPI */
3237 	phy_node = fwnode_find_reference(fwnode, "phy-handle", 0);
3238 	if (is_acpi_node(fwnode) || !IS_ERR(phy_node))
3239 		return phy_node;
3240 	phy_node = fwnode_find_reference(fwnode, "phy", 0);
3241 	if (IS_ERR(phy_node))
3242 		phy_node = fwnode_find_reference(fwnode, "phy-device", 0);
3243 	return phy_node;
3244 }
3245 EXPORT_SYMBOL_GPL(fwnode_get_phy_node);
3246 
3247 /**
3248  * phy_probe - probe and init a PHY device
3249  * @dev: device to probe and init
3250  *
3251  * Take care of setting up the phy_device structure, set the state to READY.
3252  */
phy_probe(struct device * dev)3253 static int phy_probe(struct device *dev)
3254 {
3255 	struct phy_device *phydev = to_phy_device(dev);
3256 	struct device_driver *drv = phydev->mdio.dev.driver;
3257 	struct phy_driver *phydrv = to_phy_driver(drv);
3258 	int err = 0;
3259 
3260 	phydev->drv = phydrv;
3261 
3262 	/* Disable the interrupt if the PHY doesn't support it
3263 	 * but the interrupt is still a valid one
3264 	 */
3265 	if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev))
3266 		phydev->irq = PHY_POLL;
3267 
3268 	if (phydrv->flags & PHY_IS_INTERNAL)
3269 		phydev->is_internal = true;
3270 
3271 	/* Deassert the reset signal */
3272 	phy_device_reset(phydev, 0);
3273 
3274 	if (phydev->drv->probe) {
3275 		err = phydev->drv->probe(phydev);
3276 		if (err)
3277 			goto out;
3278 	}
3279 
3280 	phy_disable_interrupts(phydev);
3281 
3282 	/* Start out supporting everything. Eventually,
3283 	 * a controller will attach, and may modify one
3284 	 * or both of these values
3285 	 */
3286 	if (phydrv->features) {
3287 		linkmode_copy(phydev->supported, phydrv->features);
3288 		genphy_c45_read_eee_abilities(phydev);
3289 	}
3290 	else if (phydrv->get_features)
3291 		err = phydrv->get_features(phydev);
3292 	else if (phydev->is_c45)
3293 		err = genphy_c45_pma_read_abilities(phydev);
3294 	else
3295 		err = genphy_read_abilities(phydev);
3296 
3297 	if (err)
3298 		goto out;
3299 
3300 	if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
3301 			       phydev->supported))
3302 		phydev->autoneg = 0;
3303 
3304 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
3305 			      phydev->supported))
3306 		phydev->is_gigabit_capable = 1;
3307 	if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
3308 			      phydev->supported))
3309 		phydev->is_gigabit_capable = 1;
3310 
3311 	of_set_phy_supported(phydev);
3312 	phy_advertise_supported(phydev);
3313 
3314 	/* Get PHY default EEE advertising modes and handle them as potentially
3315 	 * safe initial configuration.
3316 	 */
3317 	err = genphy_c45_read_eee_adv(phydev, phydev->advertising_eee);
3318 	if (err)
3319 		goto out;
3320 
3321 	/* Get the EEE modes we want to prohibit. */
3322 	of_set_phy_eee_broken(phydev);
3323 
3324 	/* Some PHYs may advertise, by default, not support EEE modes. So,
3325 	 * we need to clean them. In addition remove all disabled EEE modes.
3326 	 */
3327 	linkmode_and(phydev->advertising_eee, phydev->supported_eee,
3328 		     phydev->advertising_eee);
3329 	linkmode_andnot(phydev->advertising_eee, phydev->advertising_eee,
3330 			phydev->eee_disabled_modes);
3331 
3332 	/* There is no "enabled" flag. If PHY is advertising, assume it is
3333 	 * kind of enabled.
3334 	 */
3335 	phydev->eee_cfg.eee_enabled = !linkmode_empty(phydev->advertising_eee);
3336 
3337 	/* Get master/slave strap overrides */
3338 	of_set_phy_timing_role(phydev);
3339 
3340 	/* The Pause Frame bits indicate that the PHY can support passing
3341 	 * pause frames. During autonegotiation, the PHYs will determine if
3342 	 * they should allow pause frames to pass.  The MAC driver should then
3343 	 * use that result to determine whether to enable flow control via
3344 	 * pause frames.
3345 	 *
3346 	 * Normally, PHY drivers should not set the Pause bits, and instead
3347 	 * allow phylib to do that.  However, there may be some situations
3348 	 * (e.g. hardware erratum) where the driver wants to set only one
3349 	 * of these bits.
3350 	 */
3351 	if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) &&
3352 	    !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) {
3353 		linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
3354 				 phydev->supported);
3355 		linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
3356 				 phydev->supported);
3357 	}
3358 
3359 	/* Set the state to READY by default */
3360 	phydev->state = PHY_READY;
3361 
3362 	/* Get the LEDs from the device tree, and instantiate standard
3363 	 * LEDs for them.
3364 	 */
3365 	if (IS_ENABLED(CONFIG_PHYLIB_LEDS))
3366 		err = of_phy_leds(phydev);
3367 
3368 out:
3369 	/* Re-assert the reset signal on error */
3370 	if (err)
3371 		phy_device_reset(phydev, 1);
3372 
3373 	return err;
3374 }
3375 
phy_remove(struct device * dev)3376 static int phy_remove(struct device *dev)
3377 {
3378 	struct phy_device *phydev = to_phy_device(dev);
3379 
3380 	cancel_delayed_work_sync(&phydev->state_queue);
3381 
3382 	if (IS_ENABLED(CONFIG_PHYLIB_LEDS))
3383 		phy_leds_unregister(phydev);
3384 
3385 	phydev->state = PHY_DOWN;
3386 
3387 	sfp_bus_del_upstream(phydev->sfp_bus);
3388 	phydev->sfp_bus = NULL;
3389 
3390 	if (phydev->drv && phydev->drv->remove)
3391 		phydev->drv->remove(phydev);
3392 
3393 	/* Assert the reset signal */
3394 	phy_device_reset(phydev, 1);
3395 
3396 	phydev->drv = NULL;
3397 
3398 	return 0;
3399 }
3400 
3401 /**
3402  * phy_driver_register - register a phy_driver with the PHY layer
3403  * @new_driver: new phy_driver to register
3404  * @owner: module owning this PHY
3405  */
phy_driver_register(struct phy_driver * new_driver,struct module * owner)3406 int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
3407 {
3408 	int retval;
3409 
3410 	/* Either the features are hard coded, or dynamically
3411 	 * determined. It cannot be both.
3412 	 */
3413 	if (WARN_ON(new_driver->features && new_driver->get_features)) {
3414 		pr_err("%s: features and get_features must not both be set\n",
3415 		       new_driver->name);
3416 		return -EINVAL;
3417 	}
3418 
3419 	/* PHYLIB device drivers must not match using a DT compatible table
3420 	 * as this bypasses our checks that the mdiodev that is being matched
3421 	 * is backed by a struct phy_device. If such a case happens, we will
3422 	 * make out-of-bounds accesses and lockup in phydev->lock.
3423 	 */
3424 	if (WARN(new_driver->mdiodrv.driver.of_match_table,
3425 		 "%s: driver must not provide a DT match table\n",
3426 		 new_driver->name))
3427 		return -EINVAL;
3428 
3429 	new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
3430 	new_driver->mdiodrv.driver.name = new_driver->name;
3431 	new_driver->mdiodrv.driver.bus = &mdio_bus_type;
3432 	new_driver->mdiodrv.driver.probe = phy_probe;
3433 	new_driver->mdiodrv.driver.remove = phy_remove;
3434 	new_driver->mdiodrv.driver.owner = owner;
3435 	new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
3436 
3437 	retval = driver_register(&new_driver->mdiodrv.driver);
3438 	if (retval) {
3439 		pr_err("%s: Error %d in registering driver\n",
3440 		       new_driver->name, retval);
3441 
3442 		return retval;
3443 	}
3444 
3445 	pr_debug("%s: Registered new driver\n", new_driver->name);
3446 
3447 	return 0;
3448 }
3449 EXPORT_SYMBOL(phy_driver_register);
3450 
phy_drivers_register(struct phy_driver * new_driver,int n,struct module * owner)3451 int phy_drivers_register(struct phy_driver *new_driver, int n,
3452 			 struct module *owner)
3453 {
3454 	int i, ret = 0;
3455 
3456 	for (i = 0; i < n; i++) {
3457 		ret = phy_driver_register(new_driver + i, owner);
3458 		if (ret) {
3459 			while (i-- > 0)
3460 				phy_driver_unregister(new_driver + i);
3461 			break;
3462 		}
3463 	}
3464 	return ret;
3465 }
3466 EXPORT_SYMBOL(phy_drivers_register);
3467 
phy_driver_unregister(struct phy_driver * drv)3468 void phy_driver_unregister(struct phy_driver *drv)
3469 {
3470 	driver_unregister(&drv->mdiodrv.driver);
3471 }
3472 EXPORT_SYMBOL(phy_driver_unregister);
3473 
phy_drivers_unregister(struct phy_driver * drv,int n)3474 void phy_drivers_unregister(struct phy_driver *drv, int n)
3475 {
3476 	int i;
3477 
3478 	for (i = 0; i < n; i++)
3479 		phy_driver_unregister(drv + i);
3480 }
3481 EXPORT_SYMBOL(phy_drivers_unregister);
3482 
3483 static struct phy_driver genphy_driver = {
3484 	.phy_id		= 0xffffffff,
3485 	.phy_id_mask	= 0xffffffff,
3486 	.name		= "Generic PHY",
3487 	.get_features	= genphy_read_abilities,
3488 	.suspend	= genphy_suspend,
3489 	.resume		= genphy_resume,
3490 	.set_loopback   = genphy_loopback,
3491 };
3492 
3493 static const struct ethtool_phy_ops phy_ethtool_phy_ops = {
3494 	.get_sset_count		= phy_ethtool_get_sset_count,
3495 	.get_strings		= phy_ethtool_get_strings,
3496 	.get_stats		= phy_ethtool_get_stats,
3497 	.get_plca_cfg		= phy_ethtool_get_plca_cfg,
3498 	.set_plca_cfg		= phy_ethtool_set_plca_cfg,
3499 	.get_plca_status	= phy_ethtool_get_plca_status,
3500 	.start_cable_test	= phy_start_cable_test,
3501 	.start_cable_test_tdr	= phy_start_cable_test_tdr,
3502 };
3503 
3504 static const struct phylib_stubs __phylib_stubs = {
3505 	.hwtstamp_get = __phy_hwtstamp_get,
3506 	.hwtstamp_set = __phy_hwtstamp_set,
3507 	.get_phy_stats = __phy_ethtool_get_phy_stats,
3508 	.get_link_ext_stats = __phy_ethtool_get_link_ext_stats,
3509 };
3510 
phylib_register_stubs(void)3511 static void phylib_register_stubs(void)
3512 {
3513 	phylib_stubs = &__phylib_stubs;
3514 }
3515 
phylib_unregister_stubs(void)3516 static void phylib_unregister_stubs(void)
3517 {
3518 	phylib_stubs = NULL;
3519 }
3520 
phy_init(void)3521 static int __init phy_init(void)
3522 {
3523 	int rc;
3524 
3525 	rtnl_lock();
3526 	ethtool_set_ethtool_phy_ops(&phy_ethtool_phy_ops);
3527 	phylib_register_stubs();
3528 	rtnl_unlock();
3529 
3530 	rc = mdio_bus_init();
3531 	if (rc)
3532 		goto err_ethtool_phy_ops;
3533 
3534 	rc = phy_caps_init();
3535 	if (rc)
3536 		goto err_mdio_bus;
3537 
3538 	features_init();
3539 
3540 	rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE);
3541 	if (rc)
3542 		goto err_mdio_bus;
3543 
3544 	rc = phy_driver_register(&genphy_driver, THIS_MODULE);
3545 	if (rc)
3546 		goto err_c45;
3547 
3548 	return 0;
3549 
3550 err_c45:
3551 	phy_driver_unregister(&genphy_c45_driver);
3552 err_mdio_bus:
3553 	mdio_bus_exit();
3554 err_ethtool_phy_ops:
3555 	rtnl_lock();
3556 	phylib_unregister_stubs();
3557 	ethtool_set_ethtool_phy_ops(NULL);
3558 	rtnl_unlock();
3559 
3560 	return rc;
3561 }
3562 
phy_exit(void)3563 static void __exit phy_exit(void)
3564 {
3565 	phy_driver_unregister(&genphy_c45_driver);
3566 	phy_driver_unregister(&genphy_driver);
3567 	mdio_bus_exit();
3568 	rtnl_lock();
3569 	phylib_unregister_stubs();
3570 	ethtool_set_ethtool_phy_ops(NULL);
3571 	rtnl_unlock();
3572 }
3573 
3574 subsys_initcall(phy_init);
3575 module_exit(phy_exit);
3576