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