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