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