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