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