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