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