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