blob: 76a68bb025c6d50b9cef89390ab736dc37a1e8d6 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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/kernel.h>
13#include <linux/string.h>
14#include <linux/errno.h>
15#include <linux/unistd.h>
16#include <linux/slab.h>
17#include <linux/interrupt.h>
18#include <linux/init.h>
19#include <linux/delay.h>
20#include <linux/netdevice.h>
21#include <linux/etherdevice.h>
22#include <linux/skbuff.h>
23#include <linux/mm.h>
24#include <linux/module.h>
25#include <linux/mii.h>
26#include <linux/ethtool.h>
27#include <linux/bitmap.h>
28#include <linux/phy.h>
29#include <linux/phy_led_triggers.h>
30#include <linux/sfp.h>
31#include <linux/mdio.h>
32#include <linux/io.h>
33#include <linux/uaccess.h>
34
35MODULE_DESCRIPTION("PHY library");
36MODULE_AUTHOR("Andy Fleming");
37MODULE_LICENSE("GPL");
38
39__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
40EXPORT_SYMBOL_GPL(phy_basic_features);
41
42__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
43EXPORT_SYMBOL_GPL(phy_basic_t1_features);
44
45__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
46EXPORT_SYMBOL_GPL(phy_gbit_features);
47
48__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
49EXPORT_SYMBOL_GPL(phy_gbit_fibre_features);
50
51__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init;
52EXPORT_SYMBOL_GPL(phy_gbit_all_ports_features);
53
54__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
55EXPORT_SYMBOL_GPL(phy_10gbit_features);
56
57__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init;
58EXPORT_SYMBOL_GPL(phy_10gbit_fec_features);
59
60const int phy_basic_ports_array[3] = {
61 ETHTOOL_LINK_MODE_Autoneg_BIT,
62 ETHTOOL_LINK_MODE_TP_BIT,
63 ETHTOOL_LINK_MODE_MII_BIT,
64};
65EXPORT_SYMBOL_GPL(phy_basic_ports_array);
66
67const int phy_fibre_port_array[1] = {
68 ETHTOOL_LINK_MODE_FIBRE_BIT,
69};
70EXPORT_SYMBOL_GPL(phy_fibre_port_array);
71
72const int phy_all_ports_features_array[7] = {
73 ETHTOOL_LINK_MODE_Autoneg_BIT,
74 ETHTOOL_LINK_MODE_TP_BIT,
75 ETHTOOL_LINK_MODE_MII_BIT,
76 ETHTOOL_LINK_MODE_FIBRE_BIT,
77 ETHTOOL_LINK_MODE_AUI_BIT,
78 ETHTOOL_LINK_MODE_BNC_BIT,
79 ETHTOOL_LINK_MODE_Backplane_BIT,
80};
81EXPORT_SYMBOL_GPL(phy_all_ports_features_array);
82
83const int phy_10_100_features_array[4] = {
84 ETHTOOL_LINK_MODE_10baseT_Half_BIT,
85 ETHTOOL_LINK_MODE_10baseT_Full_BIT,
86 ETHTOOL_LINK_MODE_100baseT_Half_BIT,
87 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
88};
89EXPORT_SYMBOL_GPL(phy_10_100_features_array);
90
91const int phy_basic_t1_features_array[2] = {
92 ETHTOOL_LINK_MODE_TP_BIT,
93 ETHTOOL_LINK_MODE_100baseT1_Full_BIT,
94};
95EXPORT_SYMBOL_GPL(phy_basic_t1_features_array);
96
97const int phy_gbit_features_array[2] = {
98 ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
99 ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
100};
101EXPORT_SYMBOL_GPL(phy_gbit_features_array);
102
103const int phy_10gbit_features_array[1] = {
104 ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
105};
106EXPORT_SYMBOL_GPL(phy_10gbit_features_array);
107
108const int phy_10gbit_fec_features_array[1] = {
109 ETHTOOL_LINK_MODE_10000baseR_FEC_BIT,
110};
111EXPORT_SYMBOL_GPL(phy_10gbit_fec_features_array);
112
113__ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init;
114EXPORT_SYMBOL_GPL(phy_10gbit_full_features);
115
116static const int phy_10gbit_full_features_array[] = {
117 ETHTOOL_LINK_MODE_10baseT_Full_BIT,
118 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
119 ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
120 ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
121};
122
123static void features_init(void)
124{
125 /* 10/100 half/full*/
126 linkmode_set_bit_array(phy_basic_ports_array,
127 ARRAY_SIZE(phy_basic_ports_array),
128 phy_basic_features);
129 linkmode_set_bit_array(phy_10_100_features_array,
130 ARRAY_SIZE(phy_10_100_features_array),
131 phy_basic_features);
132
133 /* 100 full, TP */
134 linkmode_set_bit_array(phy_basic_t1_features_array,
135 ARRAY_SIZE(phy_basic_t1_features_array),
136 phy_basic_t1_features);
137
138 /* 10/100 half/full + 1000 half/full */
139 linkmode_set_bit_array(phy_basic_ports_array,
140 ARRAY_SIZE(phy_basic_ports_array),
141 phy_gbit_features);
142 linkmode_set_bit_array(phy_10_100_features_array,
143 ARRAY_SIZE(phy_10_100_features_array),
144 phy_gbit_features);
145 linkmode_set_bit_array(phy_gbit_features_array,
146 ARRAY_SIZE(phy_gbit_features_array),
147 phy_gbit_features);
148
149 /* 10/100 half/full + 1000 half/full + fibre*/
150 linkmode_set_bit_array(phy_basic_ports_array,
151 ARRAY_SIZE(phy_basic_ports_array),
152 phy_gbit_fibre_features);
153 linkmode_set_bit_array(phy_10_100_features_array,
154 ARRAY_SIZE(phy_10_100_features_array),
155 phy_gbit_fibre_features);
156 linkmode_set_bit_array(phy_gbit_features_array,
157 ARRAY_SIZE(phy_gbit_features_array),
158 phy_gbit_fibre_features);
159 linkmode_set_bit_array(phy_fibre_port_array,
160 ARRAY_SIZE(phy_fibre_port_array),
161 phy_gbit_fibre_features);
162
163 /* 10/100 half/full + 1000 half/full + TP/MII/FIBRE/AUI/BNC/Backplane*/
164 linkmode_set_bit_array(phy_all_ports_features_array,
165 ARRAY_SIZE(phy_all_ports_features_array),
166 phy_gbit_all_ports_features);
167 linkmode_set_bit_array(phy_10_100_features_array,
168 ARRAY_SIZE(phy_10_100_features_array),
169 phy_gbit_all_ports_features);
170 linkmode_set_bit_array(phy_gbit_features_array,
171 ARRAY_SIZE(phy_gbit_features_array),
172 phy_gbit_all_ports_features);
173
174 /* 10/100 half/full + 1000 half/full + 10G full*/
175 linkmode_set_bit_array(phy_all_ports_features_array,
176 ARRAY_SIZE(phy_all_ports_features_array),
177 phy_10gbit_features);
178 linkmode_set_bit_array(phy_10_100_features_array,
179 ARRAY_SIZE(phy_10_100_features_array),
180 phy_10gbit_features);
181 linkmode_set_bit_array(phy_gbit_features_array,
182 ARRAY_SIZE(phy_gbit_features_array),
183 phy_10gbit_features);
184 linkmode_set_bit_array(phy_10gbit_features_array,
185 ARRAY_SIZE(phy_10gbit_features_array),
186 phy_10gbit_features);
187
188 /* 10/100/1000/10G full */
189 linkmode_set_bit_array(phy_all_ports_features_array,
190 ARRAY_SIZE(phy_all_ports_features_array),
191 phy_10gbit_full_features);
192 linkmode_set_bit_array(phy_10gbit_full_features_array,
193 ARRAY_SIZE(phy_10gbit_full_features_array),
194 phy_10gbit_full_features);
195 /* 10G FEC only */
196 linkmode_set_bit_array(phy_10gbit_fec_features_array,
197 ARRAY_SIZE(phy_10gbit_fec_features_array),
198 phy_10gbit_fec_features);
199}
200
201void phy_device_free(struct phy_device *phydev)
202{
203 put_device(&phydev->mdio.dev);
204}
205EXPORT_SYMBOL(phy_device_free);
206
207static void phy_mdio_device_free(struct mdio_device *mdiodev)
208{
209 struct phy_device *phydev;
210
211 phydev = container_of(mdiodev, struct phy_device, mdio);
212 phy_device_free(phydev);
213}
214
215static void phy_device_release(struct device *dev)
216{
217 kfree(to_phy_device(dev));
218}
219
220static void phy_mdio_device_remove(struct mdio_device *mdiodev)
221{
222 struct phy_device *phydev;
223
224 phydev = container_of(mdiodev, struct phy_device, mdio);
225 phy_device_remove(phydev);
226}
227
228static struct phy_driver genphy_driver;
229extern struct phy_driver genphy_c45_driver;
230
231static LIST_HEAD(phy_fixup_list);
232static DEFINE_MUTEX(phy_fixup_lock);
233
234#ifdef CONFIG_PM
235static 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
270out:
271 return !phydev->suspended;
272}
273
274static int mdio_bus_phy_suspend(struct device *dev)
275{
276 struct phy_device *phydev = to_phy_device(dev);
277
278 /* We must stop the state machine manually, otherwise it stops out of
279 * control, possibly with the phydev->lock held. Upon resume, netdev
280 * may call phy routines that try to grab the same lock, and that may
281 * lead to a deadlock.
282 */
283 if (phydev->attached_dev && phydev->adjust_link)
284 phy_stop_machine(phydev);
285
286 if (!mdio_bus_phy_may_suspend(phydev))
287 return 0;
288
289 phydev->suspended_by_mdio_bus = 1;
290
291 return phy_suspend(phydev);
292}
293
294static int mdio_bus_phy_resume(struct device *dev)
295{
296 struct phy_device *phydev = to_phy_device(dev);
297 int ret;
298
299 if (!phydev->suspended_by_mdio_bus)
300 goto no_resume;
301
302 phydev->suspended_by_mdio_bus = 0;
303
304 ret = phy_resume(phydev);
305 if (ret < 0)
306 return ret;
307
308no_resume:
309 if (phydev->attached_dev && phydev->adjust_link)
310 phy_start_machine(phydev);
311
312 return 0;
313}
314
315static int mdio_bus_phy_restore(struct device *dev)
316{
317 struct phy_device *phydev = to_phy_device(dev);
318 struct net_device *netdev = phydev->attached_dev;
319 int ret;
320
321 if (!netdev)
322 return 0;
323
324 ret = phy_init_hw(phydev);
325 if (ret < 0)
326 return ret;
327
328 if (phydev->attached_dev && phydev->adjust_link)
329 phy_start_machine(phydev);
330
331 return 0;
332}
333
334static const struct dev_pm_ops mdio_bus_phy_pm_ops = {
335 .suspend = mdio_bus_phy_suspend,
336 .resume = mdio_bus_phy_resume,
337 .freeze = mdio_bus_phy_suspend,
338 .thaw = mdio_bus_phy_resume,
339 .restore = mdio_bus_phy_restore,
340};
341
342#define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops)
343
344#else
345
346#define MDIO_BUS_PHY_PM_OPS NULL
347
348#endif /* CONFIG_PM */
349
350/**
351 * phy_register_fixup - creates a new phy_fixup and adds it to the list
352 * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
353 * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
354 * It can also be PHY_ANY_UID
355 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
356 * comparison
357 * @run: The actual code to be run when a matching PHY is found
358 */
359int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
360 int (*run)(struct phy_device *))
361{
362 struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
363
364 if (!fixup)
365 return -ENOMEM;
366
367 strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
368 fixup->phy_uid = phy_uid;
369 fixup->phy_uid_mask = phy_uid_mask;
370 fixup->run = run;
371
372 mutex_lock(&phy_fixup_lock);
373 list_add_tail(&fixup->list, &phy_fixup_list);
374 mutex_unlock(&phy_fixup_lock);
375
376 return 0;
377}
378EXPORT_SYMBOL(phy_register_fixup);
379
380/* Registers a fixup to be run on any PHY with the UID in phy_uid */
381int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
382 int (*run)(struct phy_device *))
383{
384 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
385}
386EXPORT_SYMBOL(phy_register_fixup_for_uid);
387
388/* Registers a fixup to be run on the PHY with id string bus_id */
389int phy_register_fixup_for_id(const char *bus_id,
390 int (*run)(struct phy_device *))
391{
392 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
393}
394EXPORT_SYMBOL(phy_register_fixup_for_id);
395
396/**
397 * phy_unregister_fixup - remove a phy_fixup from the list
398 * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list
399 * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list
400 * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison
401 */
402int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
403{
404 struct list_head *pos, *n;
405 struct phy_fixup *fixup;
406 int ret;
407
408 ret = -ENODEV;
409
410 mutex_lock(&phy_fixup_lock);
411 list_for_each_safe(pos, n, &phy_fixup_list) {
412 fixup = list_entry(pos, struct phy_fixup, list);
413
414 if ((!strcmp(fixup->bus_id, bus_id)) &&
415 ((fixup->phy_uid & phy_uid_mask) ==
416 (phy_uid & phy_uid_mask))) {
417 list_del(&fixup->list);
418 kfree(fixup);
419 ret = 0;
420 break;
421 }
422 }
423 mutex_unlock(&phy_fixup_lock);
424
425 return ret;
426}
427EXPORT_SYMBOL(phy_unregister_fixup);
428
429/* Unregisters a fixup of any PHY with the UID in phy_uid */
430int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask)
431{
432 return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);
433}
434EXPORT_SYMBOL(phy_unregister_fixup_for_uid);
435
436/* Unregisters a fixup of the PHY with id string bus_id */
437int phy_unregister_fixup_for_id(const char *bus_id)
438{
439 return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);
440}
441EXPORT_SYMBOL(phy_unregister_fixup_for_id);
442
443/* Returns 1 if fixup matches phydev in bus_id and phy_uid.
444 * Fixups can be set to match any in one or more fields.
445 */
446static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
447{
448 if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
449 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
450 return 0;
451
452 if ((fixup->phy_uid & fixup->phy_uid_mask) !=
453 (phydev->phy_id & fixup->phy_uid_mask))
454 if (fixup->phy_uid != PHY_ANY_UID)
455 return 0;
456
457 return 1;
458}
459
460/* Runs any matching fixups for this phydev */
461static int phy_scan_fixups(struct phy_device *phydev)
462{
463 struct phy_fixup *fixup;
464
465 mutex_lock(&phy_fixup_lock);
466 list_for_each_entry(fixup, &phy_fixup_list, list) {
467 if (phy_needs_fixup(phydev, fixup)) {
468 int err = fixup->run(phydev);
469
470 if (err < 0) {
471 mutex_unlock(&phy_fixup_lock);
472 return err;
473 }
474 phydev->has_fixups = true;
475 }
476 }
477 mutex_unlock(&phy_fixup_lock);
478
479 return 0;
480}
481
482static int phy_bus_match(struct device *dev, struct device_driver *drv)
483{
484 struct phy_device *phydev = to_phy_device(dev);
485 struct phy_driver *phydrv = to_phy_driver(drv);
486 const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
487 int i;
488
489 if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
490 return 0;
491
492 if (phydrv->match_phy_device)
493 return phydrv->match_phy_device(phydev);
494
495 if (phydev->is_c45) {
496 for (i = 1; i < num_ids; i++) {
497 if (phydev->c45_ids.device_ids[i] == 0xffffffff)
498 continue;
499
500 if ((phydrv->phy_id & phydrv->phy_id_mask) ==
501 (phydev->c45_ids.device_ids[i] &
502 phydrv->phy_id_mask))
503 return 1;
504 }
505 return 0;
506 } else {
507 return (phydrv->phy_id & phydrv->phy_id_mask) ==
508 (phydev->phy_id & phydrv->phy_id_mask);
509 }
510}
511
512static ssize_t
513phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
514{
515 struct phy_device *phydev = to_phy_device(dev);
516
517 return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
518}
519static DEVICE_ATTR_RO(phy_id);
520
521static ssize_t
522phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
523{
524 struct phy_device *phydev = to_phy_device(dev);
525 const char *mode = NULL;
526
527 if (phy_is_internal(phydev))
528 mode = "internal";
529 else
530 mode = phy_modes(phydev->interface);
531
532 return sprintf(buf, "%s\n", mode);
533}
534static DEVICE_ATTR_RO(phy_interface);
535
536static ssize_t
537phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
538 char *buf)
539{
540 struct phy_device *phydev = to_phy_device(dev);
541
542 return sprintf(buf, "%d\n", phydev->has_fixups);
543}
544static DEVICE_ATTR_RO(phy_has_fixups);
545
546static struct attribute *phy_dev_attrs[] = {
547 &dev_attr_phy_id.attr,
548 &dev_attr_phy_interface.attr,
549 &dev_attr_phy_has_fixups.attr,
550 NULL,
551};
552ATTRIBUTE_GROUPS(phy_dev);
553
554static const struct device_type mdio_bus_phy_type = {
555 .name = "PHY",
556 .groups = phy_dev_groups,
557 .release = phy_device_release,
558 .pm = MDIO_BUS_PHY_PM_OPS,
559};
560
561static int phy_request_driver_module(struct phy_device *dev, u32 phy_id)
562{
563 int ret;
564
565 ret = request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT,
566 MDIO_ID_ARGS(phy_id));
567 /* We only check for failures in executing the usermode binary,
568 * not whether a PHY driver module exists for the PHY ID.
569 * Accept -ENOENT because this may occur in case no initramfs exists,
570 * then modprobe isn't available.
571 */
572 if (IS_ENABLED(CONFIG_MODULES) && ret < 0 && ret != -ENOENT) {
573 phydev_err(dev, "error %d loading PHY driver module for ID 0x%08lx\n",
574 ret, (unsigned long)phy_id);
575 return ret;
576 }
577
578 return 0;
579}
580
581struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
582 bool is_c45,
583 struct phy_c45_device_ids *c45_ids)
584{
585 struct phy_device *dev;
586 struct mdio_device *mdiodev;
587 int ret = 0;
588
589 /* We allocate the device, and initialize the default values */
590 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
591 if (!dev)
592 return ERR_PTR(-ENOMEM);
593
594 mdiodev = &dev->mdio;
595 mdiodev->dev.parent = &bus->dev;
596 mdiodev->dev.bus = &mdio_bus_type;
597 mdiodev->dev.type = &mdio_bus_phy_type;
598 mdiodev->bus = bus;
599 mdiodev->bus_match = phy_bus_match;
600 mdiodev->addr = addr;
601 mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
602 mdiodev->device_free = phy_mdio_device_free;
603 mdiodev->device_remove = phy_mdio_device_remove;
604
605 dev->speed = SPEED_UNKNOWN;
606 dev->duplex = DUPLEX_UNKNOWN;
607 dev->pause = 0;
608 dev->asym_pause = 0;
609 dev->link = 0;
610 dev->interface = PHY_INTERFACE_MODE_GMII;
611
612 dev->autoneg = AUTONEG_ENABLE;
613
614 dev->is_c45 = is_c45;
615 dev->phy_id = phy_id;
616 if (c45_ids)
617 dev->c45_ids = *c45_ids;
618 dev->irq = bus->irq[addr];
619
620 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
621 device_initialize(&mdiodev->dev);
622
623 dev->state = PHY_DOWN;
624
625 mutex_init(&dev->lock);
626 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
627
628 /* Request the appropriate module unconditionally; don't
629 * bother trying to do so only if it isn't already loaded,
630 * because that gets complicated. A hotplug event would have
631 * done an unconditional modprobe anyway.
632 * We don't do normal hotplug because it won't work for MDIO
633 * -- because it relies on the device staying around for long
634 * enough for the driver to get loaded. With MDIO, the NIC
635 * driver will get bored and give up as soon as it finds that
636 * there's no driver _already_ loaded.
637 */
638 if (is_c45 && c45_ids) {
639 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
640 int i;
641
642 for (i = 1; i < num_ids; i++) {
643 if (c45_ids->device_ids[i] == 0xffffffff)
644 continue;
645
646 ret = phy_request_driver_module(dev,
647 c45_ids->device_ids[i]);
648 if (ret)
649 break;
650 }
651 } else {
652 ret = phy_request_driver_module(dev, phy_id);
653 }
654
655 if (ret) {
656 put_device(&mdiodev->dev);
657 dev = ERR_PTR(ret);
658 }
659
660 return dev;
661}
662EXPORT_SYMBOL(phy_device_create);
663
664/* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
665 * @bus: the target MII bus
666 * @addr: PHY address on the MII bus
667 * @dev_addr: MMD address in the PHY.
668 * @devices_in_package: where to store the devices in package information.
669 *
670 * Description: reads devices in package registers of a MMD at @dev_addr
671 * from PHY at @addr on @bus.
672 *
673 * Returns: 0 on success, -EIO on failure.
674 */
675static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
676 u32 *devices_in_package)
677{
678 int phy_reg, reg_addr;
679
680 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2;
681 phy_reg = mdiobus_read(bus, addr, reg_addr);
682 if (phy_reg < 0)
683 return -EIO;
684 *devices_in_package = phy_reg << 16;
685
686 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1;
687 phy_reg = mdiobus_read(bus, addr, reg_addr);
688 if (phy_reg < 0)
689 return -EIO;
690 *devices_in_package |= phy_reg;
691
692 /* Bit 0 doesn't represent a device, it indicates c22 regs presence */
693 *devices_in_package &= ~BIT(0);
694
695 return 0;
696}
697
698/**
699 * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
700 * @bus: the target MII bus
701 * @addr: PHY address on the MII bus
702 * @phy_id: where to store the ID retrieved.
703 * @c45_ids: where to store the c45 ID information.
704 *
705 * If the PHY devices-in-package appears to be valid, it and the
706 * corresponding identifiers are stored in @c45_ids, zero is stored
707 * in @phy_id. Otherwise 0xffffffff is stored in @phy_id. Returns
708 * zero on success.
709 *
710 */
711static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
712 struct phy_c45_device_ids *c45_ids) {
713 int phy_reg;
714 int i, reg_addr;
715 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
716 u32 *devs = &c45_ids->devices_in_package;
717
718 /* Find first non-zero Devices In package. Device zero is reserved
719 * for 802.3 c45 complied PHYs, so don't probe it at first.
720 */
721 for (i = 1; i < num_ids && *devs == 0; i++) {
722 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs);
723 if (phy_reg < 0)
724 return -EIO;
725
726 if ((*devs & 0x1fffffff) == 0x1fffffff) {
727 /* If mostly Fs, there is no device there,
728 * then let's continue to probe more, as some
729 * 10G PHYs have zero Devices In package,
730 * e.g. Cortina CS4315/CS4340 PHY.
731 */
732 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs);
733 if (phy_reg < 0)
734 return -EIO;
735 /* no device there, let's get out of here */
736 if ((*devs & 0x1fffffff) == 0x1fffffff) {
737 *phy_id = 0xffffffff;
738 return 0;
739 } else {
740 break;
741 }
742 }
743 }
744
745 /* Now probe Device Identifiers for each device present. */
746 for (i = 1; i < num_ids; i++) {
747 if (!(c45_ids->devices_in_package & (1 << i)))
748 continue;
749
750 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
751 phy_reg = mdiobus_read(bus, addr, reg_addr);
752 if (phy_reg < 0)
753 return -EIO;
754 c45_ids->device_ids[i] = phy_reg << 16;
755
756 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
757 phy_reg = mdiobus_read(bus, addr, reg_addr);
758 if (phy_reg < 0)
759 return -EIO;
760 c45_ids->device_ids[i] |= phy_reg;
761 }
762 *phy_id = 0;
763 return 0;
764}
765
766/**
767 * get_phy_id - reads the specified addr for its ID.
768 * @bus: the target MII bus
769 * @addr: PHY address on the MII bus
770 * @phy_id: where to store the ID retrieved.
771 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
772 * @c45_ids: where to store the c45 ID information.
773 *
774 * Description: In the case of a 802.3-c22 PHY, reads the ID registers
775 * of the PHY at @addr on the @bus, stores it in @phy_id and returns
776 * zero on success.
777 *
778 * In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and
779 * its return value is in turn returned.
780 *
781 */
782static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
783 bool is_c45, struct phy_c45_device_ids *c45_ids)
784{
785 int phy_reg;
786
787 if (is_c45)
788 return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
789
790 /* Grab the bits from PHYIR1, and put them in the upper half */
791 phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
792 if (phy_reg < 0) {
793 /* returning -ENODEV doesn't stop bus scanning */
794 return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
795 }
796
797 *phy_id = phy_reg << 16;
798
799 /* Grab the bits from PHYIR2, and put them in the lower half */
800 phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
801 if (phy_reg < 0) {
802 /* returning -ENODEV doesn't stop bus scanning */
803 return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
804 }
805
806 *phy_id |= phy_reg;
807
808 return 0;
809}
810
811/**
812 * get_phy_device - reads the specified PHY device and returns its @phy_device
813 * struct
814 * @bus: the target MII bus
815 * @addr: PHY address on the MII bus
816 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
817 *
818 * Description: Reads the ID registers of the PHY at @addr on the
819 * @bus, then allocates and returns the phy_device to represent it.
820 */
821struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
822{
823 struct phy_c45_device_ids c45_ids;
824 u32 phy_id = 0;
825 int r;
826
827 c45_ids.devices_in_package = 0;
828 memset(c45_ids.device_ids, 0xff, sizeof(c45_ids.device_ids));
829
830 r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
831 if (r)
832 return ERR_PTR(r);
833
834 /* If the phy_id is mostly Fs, there is no device there */
835 if ((phy_id & 0x1fffffff) == 0x1fffffff)
836 return ERR_PTR(-ENODEV);
837
838 return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
839}
840EXPORT_SYMBOL(get_phy_device);
841
842/**
843 * phy_device_register - Register the phy device on the MDIO bus
844 * @phydev: phy_device structure to be added to the MDIO bus
845 */
846int phy_device_register(struct phy_device *phydev)
847{
848 int err;
849
850 err = mdiobus_register_device(&phydev->mdio);
851 if (err)
852 return err;
853
854 /* Deassert the reset signal */
855 phy_device_reset(phydev, 0);
856
857 /* Run all of the fixups for this PHY */
858 err = phy_scan_fixups(phydev);
859 if (err) {
860 phydev_err(phydev, "failed to initialize\n");
861 goto out;
862 }
863
864 err = device_add(&phydev->mdio.dev);
865 if (err) {
866 phydev_err(phydev, "failed to add\n");
867 goto out;
868 }
869
870 return 0;
871
872 out:
873 /* Assert the reset signal */
874 phy_device_reset(phydev, 1);
875
876 mdiobus_unregister_device(&phydev->mdio);
877 return err;
878}
879EXPORT_SYMBOL(phy_device_register);
880
881/**
882 * phy_device_remove - Remove a previously registered phy device from the MDIO bus
883 * @phydev: phy_device structure to remove
884 *
885 * This doesn't free the phy_device itself, it merely reverses the effects
886 * of phy_device_register(). Use phy_device_free() to free the device
887 * after calling this function.
888 */
889void phy_device_remove(struct phy_device *phydev)
890{
891 device_del(&phydev->mdio.dev);
892
893 /* Assert the reset signal */
894 phy_device_reset(phydev, 1);
895
896 mdiobus_unregister_device(&phydev->mdio);
897}
898EXPORT_SYMBOL(phy_device_remove);
899
900/**
901 * phy_find_first - finds the first PHY device on the bus
902 * @bus: the target MII bus
903 */
904struct phy_device *phy_find_first(struct mii_bus *bus)
905{
906 struct phy_device *phydev;
907 int addr;
908
909 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
910 phydev = mdiobus_get_phy(bus, addr);
911 if (phydev)
912 return phydev;
913 }
914 return NULL;
915}
916EXPORT_SYMBOL(phy_find_first);
917
918static void phy_link_change(struct phy_device *phydev, bool up, bool do_carrier)
919{
920 struct net_device *netdev = phydev->attached_dev;
921
922 if (do_carrier) {
923 if (up)
924 netif_carrier_on(netdev);
925 else
926 netif_carrier_off(netdev);
927 }
928 phydev->adjust_link(netdev);
929}
930
931/**
932 * phy_prepare_link - prepares the PHY layer to monitor link status
933 * @phydev: target phy_device struct
934 * @handler: callback function for link status change notifications
935 *
936 * Description: Tells the PHY infrastructure to handle the
937 * gory details on monitoring link status (whether through
938 * polling or an interrupt), and to call back to the
939 * connected device driver when the link status changes.
940 * If you want to monitor your own link state, don't call
941 * this function.
942 */
943static void phy_prepare_link(struct phy_device *phydev,
944 void (*handler)(struct net_device *))
945{
946 phydev->adjust_link = handler;
947}
948
949/**
950 * phy_connect_direct - connect an ethernet device to a specific phy_device
951 * @dev: the network device to connect
952 * @phydev: the pointer to the phy device
953 * @handler: callback function for state change notifications
954 * @interface: PHY device's interface
955 */
956int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
957 void (*handler)(struct net_device *),
958 phy_interface_t interface)
959{
960 int rc;
961
962 if (!dev)
963 return -EINVAL;
964
965 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
966 if (rc)
967 return rc;
968
969 phy_prepare_link(phydev, handler);
970 if (phy_interrupt_is_valid(phydev))
971 phy_request_interrupt(phydev);
972
973 return 0;
974}
975EXPORT_SYMBOL(phy_connect_direct);
976
977/**
978 * phy_connect - connect an ethernet device to a PHY device
979 * @dev: the network device to connect
980 * @bus_id: the id string of the PHY device to connect
981 * @handler: callback function for state change notifications
982 * @interface: PHY device's interface
983 *
984 * Description: Convenience function for connecting ethernet
985 * devices to PHY devices. The default behavior is for
986 * the PHY infrastructure to handle everything, and only notify
987 * the connected driver when the link status changes. If you
988 * don't want, or can't use the provided functionality, you may
989 * choose to call only the subset of functions which provide
990 * the desired functionality.
991 */
992struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
993 void (*handler)(struct net_device *),
994 phy_interface_t interface)
995{
996 struct phy_device *phydev;
997 struct device *d;
998 int rc;
999
1000 /* Search the list of PHY devices on the mdio bus for the
1001 * PHY with the requested name
1002 */
1003 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
1004 if (!d) {
1005 pr_err("PHY %s not found\n", bus_id);
1006 return ERR_PTR(-ENODEV);
1007 }
1008 phydev = to_phy_device(d);
1009
1010 rc = phy_connect_direct(dev, phydev, handler, interface);
1011 put_device(d);
1012 if (rc)
1013 return ERR_PTR(rc);
1014
1015 return phydev;
1016}
1017EXPORT_SYMBOL(phy_connect);
1018
1019/**
1020 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
1021 * device
1022 * @phydev: target phy_device struct
1023 */
1024void phy_disconnect(struct phy_device *phydev)
1025{
1026 if (phy_is_started(phydev))
1027 phy_stop(phydev);
1028
1029 if (phy_interrupt_is_valid(phydev))
1030 phy_free_interrupt(phydev);
1031
1032 phydev->adjust_link = NULL;
1033
1034 phy_detach(phydev);
1035}
1036EXPORT_SYMBOL(phy_disconnect);
1037
1038/**
1039 * phy_poll_reset - Safely wait until a PHY reset has properly completed
1040 * @phydev: The PHY device to poll
1041 *
1042 * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
1043 * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
1044 * register must be polled until the BMCR_RESET bit clears.
1045 *
1046 * Furthermore, any attempts to write to PHY registers may have no effect
1047 * or even generate MDIO bus errors until this is complete.
1048 *
1049 * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
1050 * standard and do not fully reset after the BMCR_RESET bit is set, and may
1051 * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
1052 * effort to support such broken PHYs, this function is separate from the
1053 * standard phy_init_hw() which will zero all the other bits in the BMCR
1054 * and reapply all driver-specific and board-specific fixups.
1055 */
1056static int phy_poll_reset(struct phy_device *phydev)
1057{
1058 /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
1059 unsigned int retries = 12;
1060 int ret;
1061
1062 do {
1063 msleep(50);
1064 ret = phy_read(phydev, MII_BMCR);
1065 if (ret < 0)
1066 return ret;
1067 } while (ret & BMCR_RESET && --retries);
1068 if (ret & BMCR_RESET)
1069 return -ETIMEDOUT;
1070
1071 /* Some chips (smsc911x) may still need up to another 1ms after the
1072 * BMCR_RESET bit is cleared before they are usable.
1073 */
1074 msleep(1);
1075 return 0;
1076}
1077
1078int phy_init_hw(struct phy_device *phydev)
1079{
1080 int ret = 0;
1081
1082 /* Deassert the reset signal */
1083 phy_device_reset(phydev, 0);
1084
1085 if (!phydev->drv)
1086 return 0;
1087
1088 if (phydev->drv->soft_reset)
1089 ret = phydev->drv->soft_reset(phydev);
1090
1091 if (ret < 0)
1092 return ret;
1093
1094 ret = phy_scan_fixups(phydev);
1095 if (ret < 0)
1096 return ret;
1097
1098 if (phydev->drv->config_init)
1099 ret = phydev->drv->config_init(phydev);
1100
1101 return ret;
1102}
1103EXPORT_SYMBOL(phy_init_hw);
1104
1105void phy_attached_info(struct phy_device *phydev)
1106{
1107 phy_attached_print(phydev, NULL);
1108}
1109EXPORT_SYMBOL(phy_attached_info);
1110
1111#define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%s)"
1112void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1113{
1114 const char *drv_name = phydev->drv ? phydev->drv->name : "unbound";
1115 char *irq_str;
1116 char irq_num[8];
1117
1118 switch(phydev->irq) {
1119 case PHY_POLL:
1120 irq_str = "POLL";
1121 break;
1122 case PHY_IGNORE_INTERRUPT:
1123 irq_str = "IGNORE";
1124 break;
1125 default:
1126 snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
1127 irq_str = irq_num;
1128 break;
1129 }
1130
1131
1132 if (!fmt) {
1133 phydev_info(phydev, ATTACHED_FMT "\n",
1134 drv_name, phydev_name(phydev),
1135 irq_str);
1136 } else {
1137 va_list ap;
1138
1139 phydev_info(phydev, ATTACHED_FMT,
1140 drv_name, phydev_name(phydev),
1141 irq_str);
1142
1143 va_start(ap, fmt);
1144 vprintk(fmt, ap);
1145 va_end(ap);
1146 }
1147}
1148EXPORT_SYMBOL(phy_attached_print);
1149
1150static void phy_sysfs_create_links(struct phy_device *phydev)
1151{
1152 struct net_device *dev = phydev->attached_dev;
1153 int err;
1154
1155 if (!dev)
1156 return;
1157
1158 err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
1159 "attached_dev");
1160 if (err)
1161 return;
1162
1163 err = sysfs_create_link_nowarn(&dev->dev.kobj,
1164 &phydev->mdio.dev.kobj,
1165 "phydev");
1166 if (err) {
1167 dev_err(&dev->dev, "could not add device link to %s err %d\n",
1168 kobject_name(&phydev->mdio.dev.kobj),
1169 err);
1170 /* non-fatal - some net drivers can use one netdevice
1171 * with more then one phy
1172 */
1173 }
1174
1175 phydev->sysfs_links = true;
1176}
1177
1178static ssize_t
1179phy_standalone_show(struct device *dev, struct device_attribute *attr,
1180 char *buf)
1181{
1182 struct phy_device *phydev = to_phy_device(dev);
1183
1184 return sprintf(buf, "%d\n", !phydev->attached_dev);
1185}
1186static DEVICE_ATTR_RO(phy_standalone);
1187
1188/**
1189 * phy_sfp_attach - attach the SFP bus to the PHY upstream network device
1190 * @upstream: pointer to the phy device
1191 * @bus: sfp bus representing cage being attached
1192 *
1193 * This is used to fill in the sfp_upstream_ops .attach member.
1194 */
1195void phy_sfp_attach(void *upstream, struct sfp_bus *bus)
1196{
1197 struct phy_device *phydev = upstream;
1198
1199 if (phydev->attached_dev)
1200 phydev->attached_dev->sfp_bus = bus;
1201 phydev->sfp_bus_attached = true;
1202}
1203EXPORT_SYMBOL(phy_sfp_attach);
1204
1205/**
1206 * phy_sfp_detach - detach the SFP bus from the PHY upstream network device
1207 * @upstream: pointer to the phy device
1208 * @bus: sfp bus representing cage being attached
1209 *
1210 * This is used to fill in the sfp_upstream_ops .detach member.
1211 */
1212void phy_sfp_detach(void *upstream, struct sfp_bus *bus)
1213{
1214 struct phy_device *phydev = upstream;
1215
1216 if (phydev->attached_dev)
1217 phydev->attached_dev->sfp_bus = NULL;
1218 phydev->sfp_bus_attached = false;
1219}
1220EXPORT_SYMBOL(phy_sfp_detach);
1221
1222/**
1223 * phy_sfp_probe - probe for a SFP cage attached to this PHY device
1224 * @phydev: Pointer to phy_device
1225 * @ops: SFP's upstream operations
1226 */
1227int phy_sfp_probe(struct phy_device *phydev,
1228 const struct sfp_upstream_ops *ops)
1229{
1230 struct sfp_bus *bus;
1231 int ret;
1232
1233 if (phydev->mdio.dev.fwnode) {
1234 bus = sfp_bus_find_fwnode(phydev->mdio.dev.fwnode);
1235 if (IS_ERR(bus))
1236 return PTR_ERR(bus);
1237
1238 phydev->sfp_bus = bus;
1239
1240 ret = sfp_bus_add_upstream(bus, phydev, ops);
1241 sfp_bus_put(bus);
1242 }
1243 return 0;
1244}
1245EXPORT_SYMBOL(phy_sfp_probe);
1246
1247/**
1248 * phy_attach_direct - attach a network device to a given PHY device pointer
1249 * @dev: network device to attach
1250 * @phydev: Pointer to phy_device to attach
1251 * @flags: PHY device's dev_flags
1252 * @interface: PHY device's interface
1253 *
1254 * Description: Called by drivers to attach to a particular PHY
1255 * device. The phy_device is found, and properly hooked up
1256 * to the phy_driver. If no driver is attached, then a
1257 * generic driver is used. The phy_device is given a ptr to
1258 * the attaching device, and given a callback for link status
1259 * change. The phy_device is returned to the attaching driver.
1260 * This function takes a reference on the phy device.
1261 */
1262int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1263 u32 flags, phy_interface_t interface)
1264{
1265 struct mii_bus *bus = phydev->mdio.bus;
1266 struct device *d = &phydev->mdio.dev;
1267 struct module *ndev_owner = NULL;
1268 bool using_genphy = false;
1269 int err;
1270
1271 /* For Ethernet device drivers that register their own MDIO bus, we
1272 * will have bus->owner match ndev_mod, so we do not want to increment
1273 * our own module->refcnt here, otherwise we would not be able to
1274 * unload later on.
1275 */
1276 if (dev)
1277 ndev_owner = dev->dev.parent->driver->owner;
1278 if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
1279 phydev_err(phydev, "failed to get the bus module\n");
1280 return -EIO;
1281 }
1282
1283 get_device(d);
1284
1285 /* Assume that if there is no driver, that it doesn't
1286 * exist, and we should use the genphy driver.
1287 */
1288 if (!d->driver) {
1289 if (phydev->is_c45)
1290 d->driver = &genphy_c45_driver.mdiodrv.driver;
1291 else
1292 d->driver = &genphy_driver.mdiodrv.driver;
1293
1294 using_genphy = true;
1295 }
1296
1297 if (!try_module_get(d->driver->owner)) {
1298 phydev_err(phydev, "failed to get the device driver module\n");
1299 err = -EIO;
1300 goto error_put_device;
1301 }
1302
1303 if (using_genphy) {
1304 err = d->driver->probe(d);
1305 if (err >= 0)
1306 err = device_bind_driver(d);
1307
1308 if (err)
1309 goto error_module_put;
1310 }
1311
1312 if (phydev->attached_dev) {
1313 dev_err(&dev->dev, "PHY already attached\n");
1314 err = -EBUSY;
1315 goto error;
1316 }
1317
1318 phydev->phy_link_change = phy_link_change;
1319 if (dev) {
1320 phydev->attached_dev = dev;
1321 dev->phydev = phydev;
1322 }
1323
1324 if (phydev->sfp_bus_attached)
1325 dev->sfp_bus = phydev->sfp_bus;
1326
1327 /* Some Ethernet drivers try to connect to a PHY device before
1328 * calling register_netdevice() -> netdev_register_kobject() and
1329 * does the dev->dev.kobj initialization. Here we only check for
1330 * success which indicates that the network device kobject is
1331 * ready. Once we do that we still need to keep track of whether
1332 * links were successfully set up or not for phy_detach() to
1333 * remove them accordingly.
1334 */
1335 phydev->sysfs_links = false;
1336
1337 phy_sysfs_create_links(phydev);
1338
1339 if (!phydev->attached_dev) {
1340 err = sysfs_create_file(&phydev->mdio.dev.kobj,
1341 &dev_attr_phy_standalone.attr);
1342 if (err)
1343 phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n");
1344 }
1345
1346 phydev->dev_flags = flags;
1347
1348 phydev->interface = interface;
1349
1350 phydev->state = PHY_READY;
1351
1352 /* Initial carrier state is off as the phy is about to be
1353 * (re)initialized.
1354 */
1355 if (dev)
1356 netif_carrier_off(phydev->attached_dev);
1357
1358 /* Do initial configuration here, now that
1359 * we have certain key parameters
1360 * (dev_flags and interface)
1361 */
1362 err = phy_init_hw(phydev);
1363 if (err)
1364 goto error;
1365
1366 phy_resume(phydev);
1367 phy_led_triggers_register(phydev);
1368
1369 return err;
1370
1371error:
1372 /* phy_detach() does all of the cleanup below */
1373 phy_detach(phydev);
1374 return err;
1375
1376error_module_put:
1377 module_put(d->driver->owner);
1378 d->driver = NULL;
1379error_put_device:
1380 put_device(d);
1381 if (ndev_owner != bus->owner)
1382 module_put(bus->owner);
1383 return err;
1384}
1385EXPORT_SYMBOL(phy_attach_direct);
1386
1387/**
1388 * phy_attach - attach a network device to a particular PHY device
1389 * @dev: network device to attach
1390 * @bus_id: Bus ID of PHY device to attach
1391 * @interface: PHY device's interface
1392 *
1393 * Description: Same as phy_attach_direct() except that a PHY bus_id
1394 * string is passed instead of a pointer to a struct phy_device.
1395 */
1396struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1397 phy_interface_t interface)
1398{
1399 struct bus_type *bus = &mdio_bus_type;
1400 struct phy_device *phydev;
1401 struct device *d;
1402 int rc;
1403
1404 if (!dev)
1405 return ERR_PTR(-EINVAL);
1406
1407 /* Search the list of PHY devices on the mdio bus for the
1408 * PHY with the requested name
1409 */
1410 d = bus_find_device_by_name(bus, NULL, bus_id);
1411 if (!d) {
1412 pr_err("PHY %s not found\n", bus_id);
1413 return ERR_PTR(-ENODEV);
1414 }
1415 phydev = to_phy_device(d);
1416
1417 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
1418 put_device(d);
1419 if (rc)
1420 return ERR_PTR(rc);
1421
1422 return phydev;
1423}
1424EXPORT_SYMBOL(phy_attach);
1425
1426static bool phy_driver_is_genphy_kind(struct phy_device *phydev,
1427 struct device_driver *driver)
1428{
1429 struct device *d = &phydev->mdio.dev;
1430 bool ret = false;
1431
1432 if (!phydev->drv)
1433 return ret;
1434
1435 get_device(d);
1436 ret = d->driver == driver;
1437 put_device(d);
1438
1439 return ret;
1440}
1441
1442bool phy_driver_is_genphy(struct phy_device *phydev)
1443{
1444 return phy_driver_is_genphy_kind(phydev,
1445 &genphy_driver.mdiodrv.driver);
1446}
1447EXPORT_SYMBOL_GPL(phy_driver_is_genphy);
1448
1449bool phy_driver_is_genphy_10g(struct phy_device *phydev)
1450{
1451 return phy_driver_is_genphy_kind(phydev,
1452 &genphy_c45_driver.mdiodrv.driver);
1453}
1454EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g);
1455
1456/**
1457 * phy_detach - detach a PHY device from its network device
1458 * @phydev: target phy_device struct
1459 *
1460 * This detaches the phy device from its network device and the phy
1461 * driver, and drops the reference count taken in phy_attach_direct().
1462 */
1463void phy_detach(struct phy_device *phydev)
1464{
1465 struct net_device *dev = phydev->attached_dev;
1466 struct module *ndev_owner = NULL;
1467 struct mii_bus *bus;
1468
1469 if (phydev->drv && phydev->drv->detach)
1470 phydev->drv->detach(phydev);
1471
1472 if (phydev->sysfs_links) {
1473 if (dev)
1474 sysfs_remove_link(&dev->dev.kobj, "phydev");
1475 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
1476 }
1477
1478 if (!phydev->attached_dev)
1479 sysfs_remove_file(&phydev->mdio.dev.kobj,
1480 &dev_attr_phy_standalone.attr);
1481
1482 phy_suspend(phydev);
1483 if (dev) {
1484 phydev->attached_dev->phydev = NULL;
1485 phydev->attached_dev = NULL;
1486 }
1487 phydev->phylink = NULL;
1488
1489 phy_led_triggers_unregister(phydev);
1490
1491 if (phydev->mdio.dev.driver)
1492 module_put(phydev->mdio.dev.driver->owner);
1493
1494 /* If the device had no specific driver before (i.e. - it
1495 * was using the generic driver), we unbind the device
1496 * from the generic driver so that there's a chance a
1497 * real driver could be loaded
1498 */
1499 if (phy_driver_is_genphy(phydev) ||
1500 phy_driver_is_genphy_10g(phydev))
1501 device_release_driver(&phydev->mdio.dev);
1502
1503 /* Assert the reset signal */
1504 phy_device_reset(phydev, 1);
1505
1506 /*
1507 * The phydev might go away on the put_device() below, so avoid
1508 * a use-after-free bug by reading the underlying bus first.
1509 */
1510 bus = phydev->mdio.bus;
1511
1512 put_device(&phydev->mdio.dev);
1513 if (dev)
1514 ndev_owner = dev->dev.parent->driver->owner;
1515 if (ndev_owner != bus->owner)
1516 module_put(bus->owner);
1517}
1518EXPORT_SYMBOL(phy_detach);
1519
1520int phy_suspend(struct phy_device *phydev)
1521{
1522 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1523 struct net_device *netdev = phydev->attached_dev;
1524 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1525 int ret = 0;
1526
1527 /* If the device has WOL enabled, we cannot suspend the PHY */
1528 phy_ethtool_get_wol(phydev, &wol);
1529 if (wol.wolopts || (netdev && netdev->wol_enabled))
1530 return -EBUSY;
1531
1532 if (phydev->drv && phydrv->suspend)
1533 ret = phydrv->suspend(phydev);
1534
1535 if (ret)
1536 return ret;
1537
1538 phydev->suspended = true;
1539
1540 return ret;
1541}
1542EXPORT_SYMBOL(phy_suspend);
1543
1544int __phy_resume(struct phy_device *phydev)
1545{
1546 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1547 int ret = 0;
1548
1549 WARN_ON(!mutex_is_locked(&phydev->lock));
1550
1551 if (phydev->drv && phydrv->resume)
1552 ret = phydrv->resume(phydev);
1553
1554 if (ret)
1555 return ret;
1556
1557 phydev->suspended = false;
1558
1559 return ret;
1560}
1561EXPORT_SYMBOL(__phy_resume);
1562
1563int phy_resume(struct phy_device *phydev)
1564{
1565 int ret;
1566
1567 mutex_lock(&phydev->lock);
1568 ret = __phy_resume(phydev);
1569 mutex_unlock(&phydev->lock);
1570
1571 return ret;
1572}
1573EXPORT_SYMBOL(phy_resume);
1574
1575int phy_loopback(struct phy_device *phydev, bool enable)
1576{
1577 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1578 int ret = 0;
1579
1580 mutex_lock(&phydev->lock);
1581
1582 if (enable && phydev->loopback_enabled) {
1583 ret = -EBUSY;
1584 goto out;
1585 }
1586
1587 if (!enable && !phydev->loopback_enabled) {
1588 ret = -EINVAL;
1589 goto out;
1590 }
1591
1592 if (phydev->drv && phydrv->set_loopback)
1593 ret = phydrv->set_loopback(phydev, enable);
1594 else
1595 ret = -EOPNOTSUPP;
1596
1597 if (ret)
1598 goto out;
1599
1600 phydev->loopback_enabled = enable;
1601
1602out:
1603 mutex_unlock(&phydev->lock);
1604 return ret;
1605}
1606EXPORT_SYMBOL(phy_loopback);
1607
1608/**
1609 * phy_reset_after_clk_enable - perform a PHY reset if needed
1610 * @phydev: target phy_device struct
1611 *
1612 * Description: Some PHYs are known to need a reset after their refclk was
1613 * enabled. This function evaluates the flags and perform the reset if it's
1614 * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
1615 * was reset.
1616 */
1617int phy_reset_after_clk_enable(struct phy_device *phydev)
1618{
1619 if (!phydev || !phydev->drv)
1620 return -ENODEV;
1621
1622 if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
1623 phy_device_reset(phydev, 1);
1624 phy_device_reset(phydev, 0);
1625 return 1;
1626 }
1627
1628 return 0;
1629}
1630EXPORT_SYMBOL(phy_reset_after_clk_enable);
1631
1632/* Generic PHY support and helper functions */
1633
1634/**
1635 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
1636 * @phydev: target phy_device struct
1637 *
1638 * Description: Writes MII_ADVERTISE with the appropriate values,
1639 * after sanitizing the values to make sure we only advertise
1640 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
1641 * hasn't changed, and > 0 if it has changed.
1642 */
1643static int genphy_config_advert(struct phy_device *phydev)
1644{
1645 int err, bmsr, changed = 0;
1646 u32 adv;
1647
1648 /* Only allow advertising what this PHY supports */
1649 linkmode_and(phydev->advertising, phydev->advertising,
1650 phydev->supported);
1651
1652 adv = linkmode_adv_to_mii_adv_t(phydev->advertising);
1653
1654 /* Setup standard advertisement */
1655 err = phy_modify_changed(phydev, MII_ADVERTISE,
1656 ADVERTISE_ALL | ADVERTISE_100BASE4 |
1657 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
1658 adv);
1659 if (err < 0)
1660 return err;
1661 if (err > 0)
1662 changed = 1;
1663
1664 bmsr = phy_read(phydev, MII_BMSR);
1665 if (bmsr < 0)
1666 return bmsr;
1667
1668 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
1669 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
1670 * logical 1.
1671 */
1672 if (!(bmsr & BMSR_ESTATEN))
1673 return changed;
1674
1675 adv = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
1676
1677 err = phy_modify_changed(phydev, MII_CTRL1000,
1678 ADVERTISE_1000FULL | ADVERTISE_1000HALF,
1679 adv);
1680 if (err < 0)
1681 return err;
1682 if (err > 0)
1683 changed = 1;
1684
1685 return changed;
1686}
1687
1688/**
1689 * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters
1690 * @phydev: target phy_device struct
1691 *
1692 * Description: Writes MII_ADVERTISE with the appropriate values,
1693 * after sanitizing the values to make sure we only advertise
1694 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
1695 * hasn't changed, and > 0 if it has changed. This function is intended
1696 * for Clause 37 1000Base-X mode.
1697 */
1698static int genphy_c37_config_advert(struct phy_device *phydev)
1699{
1700 u16 adv = 0;
1701
1702 /* Only allow advertising what this PHY supports */
1703 linkmode_and(phydev->advertising, phydev->advertising,
1704 phydev->supported);
1705
1706 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
1707 phydev->advertising))
1708 adv |= ADVERTISE_1000XFULL;
1709 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
1710 phydev->advertising))
1711 adv |= ADVERTISE_1000XPAUSE;
1712 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
1713 phydev->advertising))
1714 adv |= ADVERTISE_1000XPSE_ASYM;
1715
1716 return phy_modify_changed(phydev, MII_ADVERTISE,
1717 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1718 ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM,
1719 adv);
1720}
1721
1722/**
1723 * genphy_config_eee_advert - disable unwanted eee mode advertisement
1724 * @phydev: target phy_device struct
1725 *
1726 * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
1727 * efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
1728 * changed, and 1 if it has changed.
1729 */
1730int genphy_config_eee_advert(struct phy_device *phydev)
1731{
1732 int err;
1733
1734 /* Nothing to disable */
1735 if (!phydev->eee_broken_modes)
1736 return 0;
1737
1738 err = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
1739 phydev->eee_broken_modes, 0);
1740 /* If the call failed, we assume that EEE is not supported */
1741 return err < 0 ? 0 : err;
1742}
1743EXPORT_SYMBOL(genphy_config_eee_advert);
1744
1745/**
1746 * genphy_setup_forced - configures/forces speed/duplex from @phydev
1747 * @phydev: target phy_device struct
1748 *
1749 * Description: Configures MII_BMCR to force speed/duplex
1750 * to the values in phydev. Assumes that the values are valid.
1751 * Please see phy_sanitize_settings().
1752 */
1753int genphy_setup_forced(struct phy_device *phydev)
1754{
1755 u16 ctl = 0;
1756
1757 phydev->pause = 0;
1758 phydev->asym_pause = 0;
1759
1760 if (SPEED_1000 == phydev->speed)
1761 ctl |= BMCR_SPEED1000;
1762 else if (SPEED_100 == phydev->speed)
1763 ctl |= BMCR_SPEED100;
1764
1765 if (DUPLEX_FULL == phydev->duplex)
1766 ctl |= BMCR_FULLDPLX;
1767
1768 return phy_modify(phydev, MII_BMCR,
1769 ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
1770}
1771EXPORT_SYMBOL(genphy_setup_forced);
1772
1773/**
1774 * genphy_restart_aneg - Enable and Restart Autonegotiation
1775 * @phydev: target phy_device struct
1776 */
1777int genphy_restart_aneg(struct phy_device *phydev)
1778{
1779 /* Don't isolate the PHY if we're negotiating */
1780 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
1781 BMCR_ANENABLE | BMCR_ANRESTART);
1782}
1783EXPORT_SYMBOL(genphy_restart_aneg);
1784
1785/**
1786 * __genphy_config_aneg - restart auto-negotiation or write BMCR
1787 * @phydev: target phy_device struct
1788 * @changed: whether autoneg is requested
1789 *
1790 * Description: If auto-negotiation is enabled, we configure the
1791 * advertising, and then restart auto-negotiation. If it is not
1792 * enabled, then we write the BMCR.
1793 */
1794int __genphy_config_aneg(struct phy_device *phydev, bool changed)
1795{
1796 int err;
1797
1798 if (genphy_config_eee_advert(phydev))
1799 changed = true;
1800
1801 if (AUTONEG_ENABLE != phydev->autoneg)
1802 return genphy_setup_forced(phydev);
1803
1804 err = genphy_config_advert(phydev);
1805 if (err < 0) /* error */
1806 return err;
1807 else if (err)
1808 changed = true;
1809
1810 if (!changed) {
1811 /* Advertisement hasn't changed, but maybe aneg was never on to
1812 * begin with? Or maybe phy was isolated?
1813 */
1814 int ctl = phy_read(phydev, MII_BMCR);
1815
1816 if (ctl < 0)
1817 return ctl;
1818
1819 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
1820 changed = true; /* do restart aneg */
1821 }
1822
1823 /* Only restart aneg if we are advertising something different
1824 * than we were before.
1825 */
1826 return changed ? genphy_restart_aneg(phydev) : 0;
1827}
1828EXPORT_SYMBOL(__genphy_config_aneg);
1829
1830/**
1831 * genphy_c37_config_aneg - restart auto-negotiation or write BMCR
1832 * @phydev: target phy_device struct
1833 *
1834 * Description: If auto-negotiation is enabled, we configure the
1835 * advertising, and then restart auto-negotiation. If it is not
1836 * enabled, then we write the BMCR. This function is intended
1837 * for use with Clause 37 1000Base-X mode.
1838 */
1839int genphy_c37_config_aneg(struct phy_device *phydev)
1840{
1841 int err, changed;
1842
1843 if (phydev->autoneg != AUTONEG_ENABLE)
1844 return genphy_setup_forced(phydev);
1845
1846 err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100,
1847 BMCR_SPEED1000);
1848 if (err)
1849 return err;
1850
1851 changed = genphy_c37_config_advert(phydev);
1852 if (changed < 0) /* error */
1853 return changed;
1854
1855 if (!changed) {
1856 /* Advertisement hasn't changed, but maybe aneg was never on to
1857 * begin with? Or maybe phy was isolated?
1858 */
1859 int ctl = phy_read(phydev, MII_BMCR);
1860
1861 if (ctl < 0)
1862 return ctl;
1863
1864 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
1865 changed = 1; /* do restart aneg */
1866 }
1867
1868 /* Only restart aneg if we are advertising something different
1869 * than we were before.
1870 */
1871 if (changed > 0)
1872 return genphy_restart_aneg(phydev);
1873
1874 return 0;
1875}
1876EXPORT_SYMBOL(genphy_c37_config_aneg);
1877
1878/**
1879 * genphy_aneg_done - return auto-negotiation status
1880 * @phydev: target phy_device struct
1881 *
1882 * Description: Reads the status register and returns 0 either if
1883 * auto-negotiation is incomplete, or if there was an error.
1884 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
1885 */
1886int genphy_aneg_done(struct phy_device *phydev)
1887{
1888 int retval = phy_read(phydev, MII_BMSR);
1889
1890 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1891}
1892EXPORT_SYMBOL(genphy_aneg_done);
1893
1894/**
1895 * genphy_update_link - update link status in @phydev
1896 * @phydev: target phy_device struct
1897 *
1898 * Description: Update the value in phydev->link to reflect the
1899 * current link value. In order to do this, we need to read
1900 * the status register twice, keeping the second value.
1901 */
1902int genphy_update_link(struct phy_device *phydev)
1903{
1904 int status = 0, bmcr;
1905
1906 bmcr = phy_read(phydev, MII_BMCR);
1907 if (bmcr < 0)
1908 return bmcr;
1909
1910 /* Autoneg is being started, therefore disregard BMSR value and
1911 * report link as down.
1912 */
1913 if (bmcr & BMCR_ANRESTART)
1914 goto done;
1915
1916 /* The link state is latched low so that momentary link
1917 * drops can be detected. Do not double-read the status
1918 * in polling mode to detect such short link drops except
1919 * the link was already down.
1920 */
1921 if (!phy_polling_mode(phydev) || !phydev->link) {
1922 status = phy_read(phydev, MII_BMSR);
1923 if (status < 0)
1924 return status;
1925 else if (status & BMSR_LSTATUS)
1926 goto done;
1927 }
1928
1929 /* Read link and autonegotiation status */
1930 status = phy_read(phydev, MII_BMSR);
1931 if (status < 0)
1932 return status;
1933done:
1934 phydev->link = status & BMSR_LSTATUS ? 1 : 0;
1935 phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0;
1936
1937 /* Consider the case that autoneg was started and "aneg complete"
1938 * bit has been reset, but "link up" bit not yet.
1939 */
1940 if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete)
1941 phydev->link = 0;
1942
1943 return 0;
1944}
1945EXPORT_SYMBOL(genphy_update_link);
1946
1947int genphy_read_lpa(struct phy_device *phydev)
1948{
1949 int lpa, lpagb;
1950
1951 if (phydev->autoneg == AUTONEG_ENABLE) {
1952 if (!phydev->autoneg_complete) {
1953 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
1954 0);
1955 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0);
1956 return 0;
1957 }
1958
1959 if (phydev->is_gigabit_capable) {
1960 lpagb = phy_read(phydev, MII_STAT1000);
1961 if (lpagb < 0)
1962 return lpagb;
1963
1964 if (lpagb & LPA_1000MSFAIL) {
1965 int adv = phy_read(phydev, MII_CTRL1000);
1966
1967 if (adv < 0)
1968 return adv;
1969
1970 if (adv & CTL1000_ENABLE_MASTER)
1971 phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
1972 else
1973 phydev_err(phydev, "Master/Slave resolution failed\n");
1974 return -ENOLINK;
1975 }
1976
1977 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
1978 lpagb);
1979 }
1980
1981 lpa = phy_read(phydev, MII_LPA);
1982 if (lpa < 0)
1983 return lpa;
1984
1985 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa);
1986 } else {
1987 linkmode_zero(phydev->lp_advertising);
1988 }
1989
1990 return 0;
1991}
1992EXPORT_SYMBOL(genphy_read_lpa);
1993
1994/**
1995 * genphy_read_status - check the link status and update current link state
1996 * @phydev: target phy_device struct
1997 *
1998 * Description: Check the link, then figure out the current state
1999 * by comparing what we advertise with what the link partner
2000 * advertises. Start by checking the gigabit possibilities,
2001 * then move on to 10/100.
2002 */
2003int genphy_read_status(struct phy_device *phydev)
2004{
2005 int err, old_link = phydev->link;
2006
2007 /* Update the link, but return if there was an error */
2008 err = genphy_update_link(phydev);
2009 if (err)
2010 return err;
2011
2012 /* why bother the PHY if nothing can have changed */
2013 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
2014 return 0;
2015
2016 phydev->speed = SPEED_UNKNOWN;
2017 phydev->duplex = DUPLEX_UNKNOWN;
2018 phydev->pause = 0;
2019 phydev->asym_pause = 0;
2020
2021 err = genphy_read_lpa(phydev);
2022 if (err < 0)
2023 return err;
2024
2025 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2026 phy_resolve_aneg_linkmode(phydev);
2027 } else if (phydev->autoneg == AUTONEG_DISABLE) {
2028 int bmcr = phy_read(phydev, MII_BMCR);
2029
2030 if (bmcr < 0)
2031 return bmcr;
2032
2033 if (bmcr & BMCR_FULLDPLX)
2034 phydev->duplex = DUPLEX_FULL;
2035 else
2036 phydev->duplex = DUPLEX_HALF;
2037
2038 if (bmcr & BMCR_SPEED1000)
2039 phydev->speed = SPEED_1000;
2040 else if (bmcr & BMCR_SPEED100)
2041 phydev->speed = SPEED_100;
2042 else
2043 phydev->speed = SPEED_10;
2044 }
2045
2046 return 0;
2047}
2048EXPORT_SYMBOL(genphy_read_status);
2049
2050/**
2051 * genphy_c37_read_status - check the link status and update current link state
2052 * @phydev: target phy_device struct
2053 *
2054 * Description: Check the link, then figure out the current state
2055 * by comparing what we advertise with what the link partner
2056 * advertises. This function is for Clause 37 1000Base-X mode.
2057 */
2058int genphy_c37_read_status(struct phy_device *phydev)
2059{
2060 int lpa, err, old_link = phydev->link;
2061
2062 /* Update the link, but return if there was an error */
2063 err = genphy_update_link(phydev);
2064 if (err)
2065 return err;
2066
2067 /* why bother the PHY if nothing can have changed */
2068 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
2069 return 0;
2070
2071 phydev->duplex = DUPLEX_UNKNOWN;
2072 phydev->pause = 0;
2073 phydev->asym_pause = 0;
2074
2075 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2076 lpa = phy_read(phydev, MII_LPA);
2077 if (lpa < 0)
2078 return lpa;
2079
2080 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2081 phydev->lp_advertising, lpa & LPA_LPACK);
2082 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2083 phydev->lp_advertising, lpa & LPA_1000XFULL);
2084 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2085 phydev->lp_advertising, lpa & LPA_1000XPAUSE);
2086 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2087 phydev->lp_advertising,
2088 lpa & LPA_1000XPAUSE_ASYM);
2089
2090 phy_resolve_aneg_linkmode(phydev);
2091 } else if (phydev->autoneg == AUTONEG_DISABLE) {
2092 int bmcr = phy_read(phydev, MII_BMCR);
2093
2094 if (bmcr < 0)
2095 return bmcr;
2096
2097 if (bmcr & BMCR_FULLDPLX)
2098 phydev->duplex = DUPLEX_FULL;
2099 else
2100 phydev->duplex = DUPLEX_HALF;
2101 }
2102
2103 return 0;
2104}
2105EXPORT_SYMBOL(genphy_c37_read_status);
2106
2107/**
2108 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
2109 * @phydev: target phy_device struct
2110 *
2111 * Description: Perform a software PHY reset using the standard
2112 * BMCR_RESET bit and poll for the reset bit to be cleared.
2113 *
2114 * Returns: 0 on success, < 0 on failure
2115 */
2116int genphy_soft_reset(struct phy_device *phydev)
2117{
2118 u16 res = BMCR_RESET;
2119 int ret;
2120
2121 if (phydev->autoneg == AUTONEG_ENABLE)
2122 res |= BMCR_ANRESTART;
2123
2124 ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res);
2125 if (ret < 0)
2126 return ret;
2127
2128 ret = phy_poll_reset(phydev);
2129 if (ret)
2130 return ret;
2131
2132 /* BMCR may be reset to defaults */
2133 if (phydev->autoneg == AUTONEG_DISABLE)
2134 ret = genphy_setup_forced(phydev);
2135
2136 return ret;
2137}
2138EXPORT_SYMBOL(genphy_soft_reset);
2139
2140/**
2141 * genphy_read_abilities - read PHY abilities from Clause 22 registers
2142 * @phydev: target phy_device struct
2143 *
2144 * Description: Reads the PHY's abilities and populates
2145 * phydev->supported accordingly.
2146 *
2147 * Returns: 0 on success, < 0 on failure
2148 */
2149int genphy_read_abilities(struct phy_device *phydev)
2150{
2151 int val;
2152
2153 linkmode_set_bit_array(phy_basic_ports_array,
2154 ARRAY_SIZE(phy_basic_ports_array),
2155 phydev->supported);
2156
2157 val = phy_read(phydev, MII_BMSR);
2158 if (val < 0)
2159 return val;
2160
2161 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported,
2162 val & BMSR_ANEGCAPABLE);
2163
2164 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported,
2165 val & BMSR_100FULL);
2166 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported,
2167 val & BMSR_100HALF);
2168 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported,
2169 val & BMSR_10FULL);
2170 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported,
2171 val & BMSR_10HALF);
2172
2173 if (val & BMSR_ESTATEN) {
2174 val = phy_read(phydev, MII_ESTATUS);
2175 if (val < 0)
2176 return val;
2177
2178 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2179 phydev->supported, val & ESTATUS_1000_TFULL);
2180 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
2181 phydev->supported, val & ESTATUS_1000_THALF);
2182 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2183 phydev->supported, val & ESTATUS_1000_XFULL);
2184 }
2185
2186 return 0;
2187}
2188EXPORT_SYMBOL(genphy_read_abilities);
2189
2190/* This is used for the phy device which doesn't support the MMD extended
2191 * register access, but it does have side effect when we are trying to access
2192 * the MMD register via indirect method.
2193 */
2194int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
2195{
2196 return -EOPNOTSUPP;
2197}
2198EXPORT_SYMBOL(genphy_read_mmd_unsupported);
2199
2200int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
2201 u16 regnum, u16 val)
2202{
2203 return -EOPNOTSUPP;
2204}
2205EXPORT_SYMBOL(genphy_write_mmd_unsupported);
2206
2207int genphy_suspend(struct phy_device *phydev)
2208{
2209 return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
2210}
2211EXPORT_SYMBOL(genphy_suspend);
2212
2213int genphy_resume(struct phy_device *phydev)
2214{
2215 return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
2216}
2217EXPORT_SYMBOL(genphy_resume);
2218
2219int genphy_loopback(struct phy_device *phydev, bool enable)
2220{
2221 return phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK,
2222 enable ? BMCR_LOOPBACK : 0);
2223}
2224EXPORT_SYMBOL(genphy_loopback);
2225
2226/**
2227 * phy_remove_link_mode - Remove a supported link mode
2228 * @phydev: phy_device structure to remove link mode from
2229 * @link_mode: Link mode to be removed
2230 *
2231 * Description: Some MACs don't support all link modes which the PHY
2232 * does. e.g. a 1G MAC often does not support 1000Half. Add a helper
2233 * to remove a link mode.
2234 */
2235void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode)
2236{
2237 linkmode_clear_bit(link_mode, phydev->supported);
2238 phy_advertise_supported(phydev);
2239}
2240EXPORT_SYMBOL(phy_remove_link_mode);
2241
2242static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src)
2243{
2244 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst,
2245 linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src));
2246 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst,
2247 linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src));
2248}
2249
2250/**
2251 * phy_advertise_supported - Advertise all supported modes
2252 * @phydev: target phy_device struct
2253 *
2254 * Description: Called to advertise all supported modes, doesn't touch
2255 * pause mode advertising.
2256 */
2257void phy_advertise_supported(struct phy_device *phydev)
2258{
2259 __ETHTOOL_DECLARE_LINK_MODE_MASK(new);
2260
2261 linkmode_copy(new, phydev->supported);
2262 phy_copy_pause_bits(new, phydev->advertising);
2263 linkmode_copy(phydev->advertising, new);
2264}
2265EXPORT_SYMBOL(phy_advertise_supported);
2266
2267/**
2268 * phy_support_sym_pause - Enable support of symmetrical pause
2269 * @phydev: target phy_device struct
2270 *
2271 * Description: Called by the MAC to indicate is supports symmetrical
2272 * Pause, but not asym pause.
2273 */
2274void phy_support_sym_pause(struct phy_device *phydev)
2275{
2276 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
2277 phy_copy_pause_bits(phydev->advertising, phydev->supported);
2278}
2279EXPORT_SYMBOL(phy_support_sym_pause);
2280
2281/**
2282 * phy_support_asym_pause - Enable support of asym pause
2283 * @phydev: target phy_device struct
2284 *
2285 * Description: Called by the MAC to indicate is supports Asym Pause.
2286 */
2287void phy_support_asym_pause(struct phy_device *phydev)
2288{
2289 phy_copy_pause_bits(phydev->advertising, phydev->supported);
2290}
2291EXPORT_SYMBOL(phy_support_asym_pause);
2292
2293/**
2294 * phy_set_sym_pause - Configure symmetric Pause
2295 * @phydev: target phy_device struct
2296 * @rx: Receiver Pause is supported
2297 * @tx: Transmit Pause is supported
2298 * @autoneg: Auto neg should be used
2299 *
2300 * Description: Configure advertised Pause support depending on if
2301 * receiver pause and pause auto neg is supported. Generally called
2302 * from the set_pauseparam .ndo.
2303 */
2304void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
2305 bool autoneg)
2306{
2307 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
2308
2309 if (rx && tx && autoneg)
2310 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2311 phydev->supported);
2312
2313 linkmode_copy(phydev->advertising, phydev->supported);
2314}
2315EXPORT_SYMBOL(phy_set_sym_pause);
2316
2317/**
2318 * phy_set_asym_pause - Configure Pause and Asym Pause
2319 * @phydev: target phy_device struct
2320 * @rx: Receiver Pause is supported
2321 * @tx: Transmit Pause is supported
2322 *
2323 * Description: Configure advertised Pause support depending on if
2324 * transmit and receiver pause is supported. If there has been a
2325 * change in adverting, trigger a new autoneg. Generally called from
2326 * the set_pauseparam .ndo.
2327 */
2328void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx)
2329{
2330 __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv);
2331
2332 linkmode_copy(oldadv, phydev->advertising);
2333
2334 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2335 phydev->advertising);
2336 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2337 phydev->advertising);
2338
2339 if (rx) {
2340 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2341 phydev->advertising);
2342 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2343 phydev->advertising);
2344 }
2345
2346 if (tx)
2347 linkmode_change_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2348 phydev->advertising);
2349
2350 if (!linkmode_equal(oldadv, phydev->advertising) &&
2351 phydev->autoneg)
2352 phy_start_aneg(phydev);
2353}
2354EXPORT_SYMBOL(phy_set_asym_pause);
2355
2356/**
2357 * phy_validate_pause - Test if the PHY/MAC support the pause configuration
2358 * @phydev: phy_device struct
2359 * @pp: requested pause configuration
2360 *
2361 * Description: Test if the PHY/MAC combination supports the Pause
2362 * configuration the user is requesting. Returns True if it is
2363 * supported, false otherwise.
2364 */
2365bool phy_validate_pause(struct phy_device *phydev,
2366 struct ethtool_pauseparam *pp)
2367{
2368 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2369 phydev->supported) && pp->rx_pause)
2370 return false;
2371
2372 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2373 phydev->supported) &&
2374 pp->rx_pause != pp->tx_pause)
2375 return false;
2376
2377 return true;
2378}
2379EXPORT_SYMBOL(phy_validate_pause);
2380
2381static bool phy_drv_supports_irq(struct phy_driver *phydrv)
2382{
2383 return phydrv->config_intr && phydrv->ack_interrupt;
2384}
2385
2386/**
2387 * phy_probe - probe and init a PHY device
2388 * @dev: device to probe and init
2389 *
2390 * Description: Take care of setting up the phy_device structure,
2391 * set the state to READY (the driver's init function should
2392 * set it to STARTING if needed).
2393 */
2394static int phy_probe(struct device *dev)
2395{
2396 struct phy_device *phydev = to_phy_device(dev);
2397 struct device_driver *drv = phydev->mdio.dev.driver;
2398 struct phy_driver *phydrv = to_phy_driver(drv);
2399 int err = 0;
2400
2401 phydev->drv = phydrv;
2402
2403 /* Disable the interrupt if the PHY doesn't support it
2404 * but the interrupt is still a valid one
2405 */
2406 if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev))
2407 phydev->irq = PHY_POLL;
2408
2409 if (phydrv->flags & PHY_IS_INTERNAL)
2410 phydev->is_internal = true;
2411
2412 mutex_lock(&phydev->lock);
2413
2414 if (phydev->drv->probe) {
2415 /* Deassert the reset signal */
2416 phy_device_reset(phydev, 0);
2417
2418 err = phydev->drv->probe(phydev);
2419 if (err) {
2420 /* Assert the reset signal */
2421 phy_device_reset(phydev, 1);
2422 goto out;
2423 }
2424 }
2425
2426 /* Start out supporting everything. Eventually,
2427 * a controller will attach, and may modify one
2428 * or both of these values
2429 */
2430 if (phydrv->features) {
2431 linkmode_copy(phydev->supported, phydrv->features);
2432 } else if (phydrv->get_features) {
2433 err = phydrv->get_features(phydev);
2434 } else if (phydev->is_c45) {
2435 err = genphy_c45_pma_read_abilities(phydev);
2436 } else {
2437 err = genphy_read_abilities(phydev);
2438 }
2439
2440 if (err)
2441 goto out;
2442
2443 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2444 phydev->supported))
2445 phydev->autoneg = 0;
2446
2447 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
2448 phydev->supported))
2449 phydev->is_gigabit_capable = 1;
2450 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2451 phydev->supported))
2452 phydev->is_gigabit_capable = 1;
2453
2454 of_set_phy_supported(phydev);
2455 phy_advertise_supported(phydev);
2456
2457 /* Get the EEE modes we want to prohibit. We will ask
2458 * the PHY stop advertising these mode later on
2459 */
2460 of_set_phy_eee_broken(phydev);
2461
2462 /* The Pause Frame bits indicate that the PHY can support passing
2463 * pause frames. During autonegotiation, the PHYs will determine if
2464 * they should allow pause frames to pass. The MAC driver should then
2465 * use that result to determine whether to enable flow control via
2466 * pause frames.
2467 *
2468 * Normally, PHY drivers should not set the Pause bits, and instead
2469 * allow phylib to do that. However, there may be some situations
2470 * (e.g. hardware erratum) where the driver wants to set only one
2471 * of these bits.
2472 */
2473 if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) &&
2474 !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) {
2475 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2476 phydev->supported);
2477 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2478 phydev->supported);
2479 }
2480
2481 /* Set the state to READY by default */
2482 phydev->state = PHY_READY;
2483
2484out:
2485 mutex_unlock(&phydev->lock);
2486
2487 return err;
2488}
2489
2490static int phy_remove(struct device *dev)
2491{
2492 struct phy_device *phydev = to_phy_device(dev);
2493
2494 cancel_delayed_work_sync(&phydev->state_queue);
2495
2496 mutex_lock(&phydev->lock);
2497 phydev->state = PHY_DOWN;
2498 mutex_unlock(&phydev->lock);
2499
2500 sfp_bus_del_upstream(phydev->sfp_bus);
2501 phydev->sfp_bus = NULL;
2502
2503 if (phydev->drv && phydev->drv->remove) {
2504 phydev->drv->remove(phydev);
2505
2506 /* Assert the reset signal */
2507 phy_device_reset(phydev, 1);
2508 }
2509 phydev->drv = NULL;
2510
2511 return 0;
2512}
2513
2514/**
2515 * phy_driver_register - register a phy_driver with the PHY layer
2516 * @new_driver: new phy_driver to register
2517 * @owner: module owning this PHY
2518 */
2519int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
2520{
2521 int retval;
2522
2523 /* Either the features are hard coded, or dynamically
2524 * determined. It cannot be both.
2525 */
2526 if (WARN_ON(new_driver->features && new_driver->get_features)) {
2527 pr_err("%s: features and get_features must not both be set\n",
2528 new_driver->name);
2529 return -EINVAL;
2530 }
2531
2532 new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
2533 new_driver->mdiodrv.driver.name = new_driver->name;
2534 new_driver->mdiodrv.driver.bus = &mdio_bus_type;
2535 new_driver->mdiodrv.driver.probe = phy_probe;
2536 new_driver->mdiodrv.driver.remove = phy_remove;
2537 new_driver->mdiodrv.driver.owner = owner;
2538
2539 retval = driver_register(&new_driver->mdiodrv.driver);
2540 if (retval) {
2541 pr_err("%s: Error %d in registering driver\n",
2542 new_driver->name, retval);
2543
2544 return retval;
2545 }
2546
2547 pr_debug("%s: Registered new driver\n", new_driver->name);
2548
2549 return 0;
2550}
2551EXPORT_SYMBOL(phy_driver_register);
2552
2553int phy_drivers_register(struct phy_driver *new_driver, int n,
2554 struct module *owner)
2555{
2556 int i, ret = 0;
2557
2558 for (i = 0; i < n; i++) {
2559 ret = phy_driver_register(new_driver + i, owner);
2560 if (ret) {
2561 while (i-- > 0)
2562 phy_driver_unregister(new_driver + i);
2563 break;
2564 }
2565 }
2566 return ret;
2567}
2568EXPORT_SYMBOL(phy_drivers_register);
2569
2570void phy_driver_unregister(struct phy_driver *drv)
2571{
2572 driver_unregister(&drv->mdiodrv.driver);
2573}
2574EXPORT_SYMBOL(phy_driver_unregister);
2575
2576void phy_drivers_unregister(struct phy_driver *drv, int n)
2577{
2578 int i;
2579
2580 for (i = 0; i < n; i++)
2581 phy_driver_unregister(drv + i);
2582}
2583EXPORT_SYMBOL(phy_drivers_unregister);
2584
2585static struct phy_driver genphy_driver = {
2586 .phy_id = 0xffffffff,
2587 .phy_id_mask = 0xffffffff,
2588 .name = "Generic PHY",
2589 .soft_reset = genphy_no_soft_reset,
2590 .get_features = genphy_read_abilities,
2591 .aneg_done = genphy_aneg_done,
2592 .suspend = genphy_suspend,
2593 .resume = genphy_resume,
2594 .set_loopback = genphy_loopback,
2595};
2596
2597static int __init phy_init(void)
2598{
2599 int rc;
2600
2601 rc = mdio_bus_init();
2602 if (rc)
2603 return rc;
2604
2605 features_init();
2606
2607 rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE);
2608 if (rc)
2609 goto err_c45;
2610
2611 rc = phy_driver_register(&genphy_driver, THIS_MODULE);
2612 if (rc) {
2613 phy_driver_unregister(&genphy_c45_driver);
2614err_c45:
2615 mdio_bus_exit();
2616 }
2617
2618 return rc;
2619}
2620
2621static void __exit phy_exit(void)
2622{
2623 phy_driver_unregister(&genphy_c45_driver);
2624 phy_driver_unregister(&genphy_driver);
2625 mdio_bus_exit();
2626}
2627
2628subsys_initcall(phy_init);
2629module_exit(phy_exit);