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