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