This source file includes following definitions.
- features_init
- phy_device_free
- phy_mdio_device_free
- phy_device_release
- phy_mdio_device_remove
- mdio_bus_phy_may_suspend
- mdio_bus_phy_suspend
- mdio_bus_phy_resume
- mdio_bus_phy_restore
- phy_register_fixup
- phy_register_fixup_for_uid
- phy_register_fixup_for_id
- phy_unregister_fixup
- phy_unregister_fixup_for_uid
- phy_unregister_fixup_for_id
- phy_needs_fixup
- phy_scan_fixups
- phy_bus_match
- phy_id_show
- phy_interface_show
- phy_has_fixups_show
- phy_request_driver_module
- phy_device_create
- get_phy_c45_devs_in_pkg
- get_phy_c45_ids
- get_phy_id
- get_phy_device
- phy_device_register
- phy_device_remove
- phy_find_first
- phy_link_change
- phy_prepare_link
- phy_connect_direct
- phy_connect
- phy_disconnect
- phy_poll_reset
- phy_init_hw
- phy_attached_info
- phy_attached_print
- phy_sysfs_create_links
- phy_standalone_show
- phy_attach_direct
- phy_attach
- phy_driver_is_genphy_kind
- phy_driver_is_genphy
- phy_driver_is_genphy_10g
- phy_detach
- phy_suspend
- __phy_resume
- phy_resume
- phy_loopback
- phy_reset_after_clk_enable
- genphy_config_advert
- genphy_config_eee_advert
- genphy_setup_forced
- genphy_restart_aneg
- __genphy_config_aneg
- genphy_aneg_done
- genphy_update_link
- genphy_read_lpa
- genphy_read_status
- genphy_soft_reset
- genphy_read_abilities
- genphy_read_mmd_unsupported
- genphy_write_mmd_unsupported
- genphy_suspend
- genphy_resume
- genphy_loopback
- phy_remove_link_mode
- phy_copy_pause_bits
- phy_advertise_supported
- phy_support_sym_pause
- phy_support_asym_pause
- phy_set_sym_pause
- phy_set_asym_pause
- phy_validate_pause
- phy_drv_supports_irq
- phy_probe
- phy_remove
- phy_driver_register
- phy_drivers_register
- phy_driver_unregister
- phy_drivers_unregister
- phy_init
- phy_exit
1
2
3
4
5
6
7
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/mdio.h>
31 #include <linux/io.h>
32 #include <linux/uaccess.h>
33
34 MODULE_DESCRIPTION("PHY library");
35 MODULE_AUTHOR("Andy Fleming");
36 MODULE_LICENSE("GPL");
37
38 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
39 EXPORT_SYMBOL_GPL(phy_basic_features);
40
41 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
42 EXPORT_SYMBOL_GPL(phy_basic_t1_features);
43
44 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
45 EXPORT_SYMBOL_GPL(phy_gbit_features);
46
47 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
48 EXPORT_SYMBOL_GPL(phy_gbit_fibre_features);
49
50 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init;
51 EXPORT_SYMBOL_GPL(phy_gbit_all_ports_features);
52
53 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
54 EXPORT_SYMBOL_GPL(phy_10gbit_features);
55
56 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init;
57 EXPORT_SYMBOL_GPL(phy_10gbit_fec_features);
58
59 const int phy_basic_ports_array[3] = {
60 ETHTOOL_LINK_MODE_Autoneg_BIT,
61 ETHTOOL_LINK_MODE_TP_BIT,
62 ETHTOOL_LINK_MODE_MII_BIT,
63 };
64 EXPORT_SYMBOL_GPL(phy_basic_ports_array);
65
66 const int phy_fibre_port_array[1] = {
67 ETHTOOL_LINK_MODE_FIBRE_BIT,
68 };
69 EXPORT_SYMBOL_GPL(phy_fibre_port_array);
70
71 const int phy_all_ports_features_array[7] = {
72 ETHTOOL_LINK_MODE_Autoneg_BIT,
73 ETHTOOL_LINK_MODE_TP_BIT,
74 ETHTOOL_LINK_MODE_MII_BIT,
75 ETHTOOL_LINK_MODE_FIBRE_BIT,
76 ETHTOOL_LINK_MODE_AUI_BIT,
77 ETHTOOL_LINK_MODE_BNC_BIT,
78 ETHTOOL_LINK_MODE_Backplane_BIT,
79 };
80 EXPORT_SYMBOL_GPL(phy_all_ports_features_array);
81
82 const int phy_10_100_features_array[4] = {
83 ETHTOOL_LINK_MODE_10baseT_Half_BIT,
84 ETHTOOL_LINK_MODE_10baseT_Full_BIT,
85 ETHTOOL_LINK_MODE_100baseT_Half_BIT,
86 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
87 };
88 EXPORT_SYMBOL_GPL(phy_10_100_features_array);
89
90 const int phy_basic_t1_features_array[2] = {
91 ETHTOOL_LINK_MODE_TP_BIT,
92 ETHTOOL_LINK_MODE_100baseT1_Full_BIT,
93 };
94 EXPORT_SYMBOL_GPL(phy_basic_t1_features_array);
95
96 const int phy_gbit_features_array[2] = {
97 ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
98 ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
99 };
100 EXPORT_SYMBOL_GPL(phy_gbit_features_array);
101
102 const int phy_10gbit_features_array[1] = {
103 ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
104 };
105 EXPORT_SYMBOL_GPL(phy_10gbit_features_array);
106
107 const int phy_10gbit_fec_features_array[1] = {
108 ETHTOOL_LINK_MODE_10000baseR_FEC_BIT,
109 };
110 EXPORT_SYMBOL_GPL(phy_10gbit_fec_features_array);
111
112 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init;
113 EXPORT_SYMBOL_GPL(phy_10gbit_full_features);
114
115 static const int phy_10gbit_full_features_array[] = {
116 ETHTOOL_LINK_MODE_10baseT_Full_BIT,
117 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
118 ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
119 ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
120 };
121
122 static void features_init(void)
123 {
124
125 linkmode_set_bit_array(phy_basic_ports_array,
126 ARRAY_SIZE(phy_basic_ports_array),
127 phy_basic_features);
128 linkmode_set_bit_array(phy_10_100_features_array,
129 ARRAY_SIZE(phy_10_100_features_array),
130 phy_basic_features);
131
132
133 linkmode_set_bit_array(phy_basic_t1_features_array,
134 ARRAY_SIZE(phy_basic_t1_features_array),
135 phy_basic_t1_features);
136
137
138 linkmode_set_bit_array(phy_basic_ports_array,
139 ARRAY_SIZE(phy_basic_ports_array),
140 phy_gbit_features);
141 linkmode_set_bit_array(phy_10_100_features_array,
142 ARRAY_SIZE(phy_10_100_features_array),
143 phy_gbit_features);
144 linkmode_set_bit_array(phy_gbit_features_array,
145 ARRAY_SIZE(phy_gbit_features_array),
146 phy_gbit_features);
147
148
149 linkmode_set_bit_array(phy_basic_ports_array,
150 ARRAY_SIZE(phy_basic_ports_array),
151 phy_gbit_fibre_features);
152 linkmode_set_bit_array(phy_10_100_features_array,
153 ARRAY_SIZE(phy_10_100_features_array),
154 phy_gbit_fibre_features);
155 linkmode_set_bit_array(phy_gbit_features_array,
156 ARRAY_SIZE(phy_gbit_features_array),
157 phy_gbit_fibre_features);
158 linkmode_set_bit_array(phy_fibre_port_array,
159 ARRAY_SIZE(phy_fibre_port_array),
160 phy_gbit_fibre_features);
161
162
163 linkmode_set_bit_array(phy_all_ports_features_array,
164 ARRAY_SIZE(phy_all_ports_features_array),
165 phy_gbit_all_ports_features);
166 linkmode_set_bit_array(phy_10_100_features_array,
167 ARRAY_SIZE(phy_10_100_features_array),
168 phy_gbit_all_ports_features);
169 linkmode_set_bit_array(phy_gbit_features_array,
170 ARRAY_SIZE(phy_gbit_features_array),
171 phy_gbit_all_ports_features);
172
173
174 linkmode_set_bit_array(phy_all_ports_features_array,
175 ARRAY_SIZE(phy_all_ports_features_array),
176 phy_10gbit_features);
177 linkmode_set_bit_array(phy_10_100_features_array,
178 ARRAY_SIZE(phy_10_100_features_array),
179 phy_10gbit_features);
180 linkmode_set_bit_array(phy_gbit_features_array,
181 ARRAY_SIZE(phy_gbit_features_array),
182 phy_10gbit_features);
183 linkmode_set_bit_array(phy_10gbit_features_array,
184 ARRAY_SIZE(phy_10gbit_features_array),
185 phy_10gbit_features);
186
187
188 linkmode_set_bit_array(phy_all_ports_features_array,
189 ARRAY_SIZE(phy_all_ports_features_array),
190 phy_10gbit_full_features);
191 linkmode_set_bit_array(phy_10gbit_full_features_array,
192 ARRAY_SIZE(phy_10gbit_full_features_array),
193 phy_10gbit_full_features);
194
195 linkmode_set_bit_array(phy_10gbit_fec_features_array,
196 ARRAY_SIZE(phy_10gbit_fec_features_array),
197 phy_10gbit_fec_features);
198 }
199
200 void phy_device_free(struct phy_device *phydev)
201 {
202 put_device(&phydev->mdio.dev);
203 }
204 EXPORT_SYMBOL(phy_device_free);
205
206 static void phy_mdio_device_free(struct mdio_device *mdiodev)
207 {
208 struct phy_device *phydev;
209
210 phydev = container_of(mdiodev, struct phy_device, mdio);
211 phy_device_free(phydev);
212 }
213
214 static void phy_device_release(struct device *dev)
215 {
216 kfree(to_phy_device(dev));
217 }
218
219 static void phy_mdio_device_remove(struct mdio_device *mdiodev)
220 {
221 struct phy_device *phydev;
222
223 phydev = container_of(mdiodev, struct phy_device, mdio);
224 phy_device_remove(phydev);
225 }
226
227 static struct phy_driver genphy_driver;
228 extern struct phy_driver genphy_c45_driver;
229
230 static LIST_HEAD(phy_fixup_list);
231 static DEFINE_MUTEX(phy_fixup_lock);
232
233 #ifdef CONFIG_PM
234 static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
235 {
236 struct device_driver *drv = phydev->mdio.dev.driver;
237 struct phy_driver *phydrv = to_phy_driver(drv);
238 struct net_device *netdev = phydev->attached_dev;
239
240 if (!drv || !phydrv->suspend)
241 return false;
242
243
244
245
246
247
248 if (!netdev)
249 goto out;
250
251 if (netdev->wol_enabled)
252 return false;
253
254
255
256
257
258
259 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
260 return false;
261
262
263
264
265
266 if (device_may_wakeup(&netdev->dev))
267 return false;
268
269 out:
270 return !phydev->suspended;
271 }
272
273 static int mdio_bus_phy_suspend(struct device *dev)
274 {
275 struct phy_device *phydev = to_phy_device(dev);
276
277
278
279
280
281
282 if (phydev->attached_dev && phydev->adjust_link)
283 phy_stop_machine(phydev);
284
285 if (!mdio_bus_phy_may_suspend(phydev))
286 return 0;
287
288 phydev->suspended_by_mdio_bus = 1;
289
290 return phy_suspend(phydev);
291 }
292
293 static int mdio_bus_phy_resume(struct device *dev)
294 {
295 struct phy_device *phydev = to_phy_device(dev);
296 int ret;
297
298 if (!phydev->suspended_by_mdio_bus)
299 goto no_resume;
300
301 phydev->suspended_by_mdio_bus = 0;
302
303 ret = phy_resume(phydev);
304 if (ret < 0)
305 return ret;
306
307 no_resume:
308 if (phydev->attached_dev && phydev->adjust_link)
309 phy_start_machine(phydev);
310
311 return 0;
312 }
313
314 static int mdio_bus_phy_restore(struct device *dev)
315 {
316 struct phy_device *phydev = to_phy_device(dev);
317 struct net_device *netdev = phydev->attached_dev;
318 int ret;
319
320 if (!netdev)
321 return 0;
322
323 ret = phy_init_hw(phydev);
324 if (ret < 0)
325 return ret;
326
327 if (phydev->attached_dev && phydev->adjust_link)
328 phy_start_machine(phydev);
329
330 return 0;
331 }
332
333 static const struct dev_pm_ops mdio_bus_phy_pm_ops = {
334 .suspend = mdio_bus_phy_suspend,
335 .resume = mdio_bus_phy_resume,
336 .freeze = mdio_bus_phy_suspend,
337 .thaw = mdio_bus_phy_resume,
338 .restore = mdio_bus_phy_restore,
339 };
340
341 #define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops)
342
343 #else
344
345 #define MDIO_BUS_PHY_PM_OPS NULL
346
347 #endif
348
349
350
351
352
353
354
355
356
357
358 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
359 int (*run)(struct phy_device *))
360 {
361 struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
362
363 if (!fixup)
364 return -ENOMEM;
365
366 strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
367 fixup->phy_uid = phy_uid;
368 fixup->phy_uid_mask = phy_uid_mask;
369 fixup->run = run;
370
371 mutex_lock(&phy_fixup_lock);
372 list_add_tail(&fixup->list, &phy_fixup_list);
373 mutex_unlock(&phy_fixup_lock);
374
375 return 0;
376 }
377 EXPORT_SYMBOL(phy_register_fixup);
378
379
380 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
381 int (*run)(struct phy_device *))
382 {
383 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
384 }
385 EXPORT_SYMBOL(phy_register_fixup_for_uid);
386
387
388 int phy_register_fixup_for_id(const char *bus_id,
389 int (*run)(struct phy_device *))
390 {
391 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
392 }
393 EXPORT_SYMBOL(phy_register_fixup_for_id);
394
395
396
397
398
399
400
401 int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
402 {
403 struct list_head *pos, *n;
404 struct phy_fixup *fixup;
405 int ret;
406
407 ret = -ENODEV;
408
409 mutex_lock(&phy_fixup_lock);
410 list_for_each_safe(pos, n, &phy_fixup_list) {
411 fixup = list_entry(pos, struct phy_fixup, list);
412
413 if ((!strcmp(fixup->bus_id, bus_id)) &&
414 ((fixup->phy_uid & phy_uid_mask) ==
415 (phy_uid & phy_uid_mask))) {
416 list_del(&fixup->list);
417 kfree(fixup);
418 ret = 0;
419 break;
420 }
421 }
422 mutex_unlock(&phy_fixup_lock);
423
424 return ret;
425 }
426 EXPORT_SYMBOL(phy_unregister_fixup);
427
428
429 int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask)
430 {
431 return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);
432 }
433 EXPORT_SYMBOL(phy_unregister_fixup_for_uid);
434
435
436 int phy_unregister_fixup_for_id(const char *bus_id)
437 {
438 return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);
439 }
440 EXPORT_SYMBOL(phy_unregister_fixup_for_id);
441
442
443
444
445 static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
446 {
447 if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
448 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
449 return 0;
450
451 if ((fixup->phy_uid & fixup->phy_uid_mask) !=
452 (phydev->phy_id & fixup->phy_uid_mask))
453 if (fixup->phy_uid != PHY_ANY_UID)
454 return 0;
455
456 return 1;
457 }
458
459
460 static int phy_scan_fixups(struct phy_device *phydev)
461 {
462 struct phy_fixup *fixup;
463
464 mutex_lock(&phy_fixup_lock);
465 list_for_each_entry(fixup, &phy_fixup_list, list) {
466 if (phy_needs_fixup(phydev, fixup)) {
467 int err = fixup->run(phydev);
468
469 if (err < 0) {
470 mutex_unlock(&phy_fixup_lock);
471 return err;
472 }
473 phydev->has_fixups = true;
474 }
475 }
476 mutex_unlock(&phy_fixup_lock);
477
478 return 0;
479 }
480
481 static int phy_bus_match(struct device *dev, struct device_driver *drv)
482 {
483 struct phy_device *phydev = to_phy_device(dev);
484 struct phy_driver *phydrv = to_phy_driver(drv);
485 const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
486 int i;
487
488 if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
489 return 0;
490
491 if (phydrv->match_phy_device)
492 return phydrv->match_phy_device(phydev);
493
494 if (phydev->is_c45) {
495 for (i = 1; i < num_ids; i++) {
496 if (phydev->c45_ids.device_ids[i] == 0xffffffff)
497 continue;
498
499 if ((phydrv->phy_id & phydrv->phy_id_mask) ==
500 (phydev->c45_ids.device_ids[i] &
501 phydrv->phy_id_mask))
502 return 1;
503 }
504 return 0;
505 } else {
506 return (phydrv->phy_id & phydrv->phy_id_mask) ==
507 (phydev->phy_id & phydrv->phy_id_mask);
508 }
509 }
510
511 static ssize_t
512 phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
513 {
514 struct phy_device *phydev = to_phy_device(dev);
515
516 return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
517 }
518 static DEVICE_ATTR_RO(phy_id);
519
520 static ssize_t
521 phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
522 {
523 struct phy_device *phydev = to_phy_device(dev);
524 const char *mode = NULL;
525
526 if (phy_is_internal(phydev))
527 mode = "internal";
528 else
529 mode = phy_modes(phydev->interface);
530
531 return sprintf(buf, "%s\n", mode);
532 }
533 static DEVICE_ATTR_RO(phy_interface);
534
535 static ssize_t
536 phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
537 char *buf)
538 {
539 struct phy_device *phydev = to_phy_device(dev);
540
541 return sprintf(buf, "%d\n", phydev->has_fixups);
542 }
543 static DEVICE_ATTR_RO(phy_has_fixups);
544
545 static struct attribute *phy_dev_attrs[] = {
546 &dev_attr_phy_id.attr,
547 &dev_attr_phy_interface.attr,
548 &dev_attr_phy_has_fixups.attr,
549 NULL,
550 };
551 ATTRIBUTE_GROUPS(phy_dev);
552
553 static const struct device_type mdio_bus_phy_type = {
554 .name = "PHY",
555 .groups = phy_dev_groups,
556 .release = phy_device_release,
557 .pm = MDIO_BUS_PHY_PM_OPS,
558 };
559
560 static int phy_request_driver_module(struct phy_device *dev, u32 phy_id)
561 {
562 int ret;
563
564 ret = request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT,
565 MDIO_ID_ARGS(phy_id));
566
567
568
569
570
571 if (IS_ENABLED(CONFIG_MODULES) && ret < 0 && ret != -ENOENT) {
572 phydev_err(dev, "error %d loading PHY driver module for ID 0x%08lx\n",
573 ret, (unsigned long)phy_id);
574 return ret;
575 }
576
577 return 0;
578 }
579
580 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
581 bool is_c45,
582 struct phy_c45_device_ids *c45_ids)
583 {
584 struct phy_device *dev;
585 struct mdio_device *mdiodev;
586 int ret = 0;
587
588
589 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
590 if (!dev)
591 return ERR_PTR(-ENOMEM);
592
593 mdiodev = &dev->mdio;
594 mdiodev->dev.parent = &bus->dev;
595 mdiodev->dev.bus = &mdio_bus_type;
596 mdiodev->dev.type = &mdio_bus_phy_type;
597 mdiodev->bus = bus;
598 mdiodev->bus_match = phy_bus_match;
599 mdiodev->addr = addr;
600 mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
601 mdiodev->device_free = phy_mdio_device_free;
602 mdiodev->device_remove = phy_mdio_device_remove;
603
604 dev->speed = SPEED_UNKNOWN;
605 dev->duplex = DUPLEX_UNKNOWN;
606 dev->pause = 0;
607 dev->asym_pause = 0;
608 dev->link = 0;
609 dev->interface = PHY_INTERFACE_MODE_GMII;
610
611 dev->autoneg = AUTONEG_ENABLE;
612
613 dev->is_c45 = is_c45;
614 dev->phy_id = phy_id;
615 if (c45_ids)
616 dev->c45_ids = *c45_ids;
617 dev->irq = bus->irq[addr];
618 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
619
620 dev->state = PHY_DOWN;
621
622 mutex_init(&dev->lock);
623 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
624
625
626
627
628
629
630
631
632
633
634
635 if (is_c45 && c45_ids) {
636 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
637 int i;
638
639 for (i = 1; i < num_ids; i++) {
640 if (c45_ids->device_ids[i] == 0xffffffff)
641 continue;
642
643 ret = phy_request_driver_module(dev,
644 c45_ids->device_ids[i]);
645 if (ret)
646 break;
647 }
648 } else {
649 ret = phy_request_driver_module(dev, phy_id);
650 }
651
652 if (!ret) {
653 device_initialize(&mdiodev->dev);
654 } else {
655 kfree(dev);
656 dev = ERR_PTR(ret);
657 }
658
659 return dev;
660 }
661 EXPORT_SYMBOL(phy_device_create);
662
663
664
665
666
667
668
669
670
671
672
673
674 static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
675 u32 *devices_in_package)
676 {
677 int phy_reg, reg_addr;
678
679 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2;
680 phy_reg = mdiobus_read(bus, addr, reg_addr);
681 if (phy_reg < 0)
682 return -EIO;
683 *devices_in_package = phy_reg << 16;
684
685 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1;
686 phy_reg = mdiobus_read(bus, addr, reg_addr);
687 if (phy_reg < 0)
688 return -EIO;
689 *devices_in_package |= phy_reg;
690
691
692 *devices_in_package &= ~BIT(0);
693
694 return 0;
695 }
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710 static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
711 struct phy_c45_device_ids *c45_ids) {
712 int phy_reg;
713 int i, reg_addr;
714 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
715 u32 *devs = &c45_ids->devices_in_package;
716
717
718
719
720 for (i = 1; i < num_ids && *devs == 0; i++) {
721 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs);
722 if (phy_reg < 0)
723 return -EIO;
724
725 if ((*devs & 0x1fffffff) == 0x1fffffff) {
726
727
728
729
730
731 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs);
732 if (phy_reg < 0)
733 return -EIO;
734
735 if ((*devs & 0x1fffffff) == 0x1fffffff) {
736 *phy_id = 0xffffffff;
737 return 0;
738 } else {
739 break;
740 }
741 }
742 }
743
744
745 for (i = 1; i < num_ids; i++) {
746 if (!(c45_ids->devices_in_package & (1 << i)))
747 continue;
748
749 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
750 phy_reg = mdiobus_read(bus, addr, reg_addr);
751 if (phy_reg < 0)
752 return -EIO;
753 c45_ids->device_ids[i] = phy_reg << 16;
754
755 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
756 phy_reg = mdiobus_read(bus, addr, reg_addr);
757 if (phy_reg < 0)
758 return -EIO;
759 c45_ids->device_ids[i] |= phy_reg;
760 }
761 *phy_id = 0;
762 return 0;
763 }
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781 static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
782 bool is_c45, struct phy_c45_device_ids *c45_ids)
783 {
784 int phy_reg;
785
786 if (is_c45)
787 return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
788
789
790 phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
791 if (phy_reg < 0) {
792
793 return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
794 }
795
796 *phy_id = phy_reg << 16;
797
798
799 phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
800 if (phy_reg < 0)
801 return -EIO;
802
803 *phy_id |= phy_reg;
804
805 return 0;
806 }
807
808
809
810
811
812
813
814
815
816
817
818 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
819 {
820 struct phy_c45_device_ids c45_ids;
821 u32 phy_id = 0;
822 int r;
823
824 c45_ids.devices_in_package = 0;
825 memset(c45_ids.device_ids, 0xff, sizeof(c45_ids.device_ids));
826
827 r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
828 if (r)
829 return ERR_PTR(r);
830
831
832 if ((phy_id & 0x1fffffff) == 0x1fffffff)
833 return ERR_PTR(-ENODEV);
834
835 return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
836 }
837 EXPORT_SYMBOL(get_phy_device);
838
839
840
841
842
843 int phy_device_register(struct phy_device *phydev)
844 {
845 int err;
846
847 err = mdiobus_register_device(&phydev->mdio);
848 if (err)
849 return err;
850
851
852 phy_device_reset(phydev, 0);
853
854
855 err = phy_scan_fixups(phydev);
856 if (err) {
857 phydev_err(phydev, "failed to initialize\n");
858 goto out;
859 }
860
861 err = device_add(&phydev->mdio.dev);
862 if (err) {
863 phydev_err(phydev, "failed to add\n");
864 goto out;
865 }
866
867 return 0;
868
869 out:
870
871 phy_device_reset(phydev, 1);
872
873 mdiobus_unregister_device(&phydev->mdio);
874 return err;
875 }
876 EXPORT_SYMBOL(phy_device_register);
877
878
879
880
881
882
883
884
885
886 void phy_device_remove(struct phy_device *phydev)
887 {
888 device_del(&phydev->mdio.dev);
889
890
891 phy_device_reset(phydev, 1);
892
893 mdiobus_unregister_device(&phydev->mdio);
894 }
895 EXPORT_SYMBOL(phy_device_remove);
896
897
898
899
900
901 struct phy_device *phy_find_first(struct mii_bus *bus)
902 {
903 struct phy_device *phydev;
904 int addr;
905
906 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
907 phydev = mdiobus_get_phy(bus, addr);
908 if (phydev)
909 return phydev;
910 }
911 return NULL;
912 }
913 EXPORT_SYMBOL(phy_find_first);
914
915 static void phy_link_change(struct phy_device *phydev, bool up, bool do_carrier)
916 {
917 struct net_device *netdev = phydev->attached_dev;
918
919 if (do_carrier) {
920 if (up)
921 netif_carrier_on(netdev);
922 else
923 netif_carrier_off(netdev);
924 }
925 phydev->adjust_link(netdev);
926 }
927
928
929
930
931
932
933
934
935
936
937
938
939
940 static void phy_prepare_link(struct phy_device *phydev,
941 void (*handler)(struct net_device *))
942 {
943 phydev->adjust_link = handler;
944 }
945
946
947
948
949
950
951
952
953 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
954 void (*handler)(struct net_device *),
955 phy_interface_t interface)
956 {
957 int rc;
958
959 if (!dev)
960 return -EINVAL;
961
962 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
963 if (rc)
964 return rc;
965
966 phy_prepare_link(phydev, handler);
967 if (phy_interrupt_is_valid(phydev))
968 phy_request_interrupt(phydev);
969
970 return 0;
971 }
972 EXPORT_SYMBOL(phy_connect_direct);
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
990 void (*handler)(struct net_device *),
991 phy_interface_t interface)
992 {
993 struct phy_device *phydev;
994 struct device *d;
995 int rc;
996
997
998
999
1000 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
1001 if (!d) {
1002 pr_err("PHY %s not found\n", bus_id);
1003 return ERR_PTR(-ENODEV);
1004 }
1005 phydev = to_phy_device(d);
1006
1007 rc = phy_connect_direct(dev, phydev, handler, interface);
1008 put_device(d);
1009 if (rc)
1010 return ERR_PTR(rc);
1011
1012 return phydev;
1013 }
1014 EXPORT_SYMBOL(phy_connect);
1015
1016
1017
1018
1019
1020
1021 void phy_disconnect(struct phy_device *phydev)
1022 {
1023 if (phy_is_started(phydev))
1024 phy_stop(phydev);
1025
1026 if (phy_interrupt_is_valid(phydev))
1027 phy_free_interrupt(phydev);
1028
1029 phydev->adjust_link = NULL;
1030
1031 phy_detach(phydev);
1032 }
1033 EXPORT_SYMBOL(phy_disconnect);
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053 static int phy_poll_reset(struct phy_device *phydev)
1054 {
1055
1056 unsigned int retries = 12;
1057 int ret;
1058
1059 do {
1060 msleep(50);
1061 ret = phy_read(phydev, MII_BMCR);
1062 if (ret < 0)
1063 return ret;
1064 } while (ret & BMCR_RESET && --retries);
1065 if (ret & BMCR_RESET)
1066 return -ETIMEDOUT;
1067
1068
1069
1070
1071 msleep(1);
1072 return 0;
1073 }
1074
1075 int phy_init_hw(struct phy_device *phydev)
1076 {
1077 int ret = 0;
1078
1079
1080 phy_device_reset(phydev, 0);
1081
1082 if (!phydev->drv)
1083 return 0;
1084
1085 if (phydev->drv->soft_reset)
1086 ret = phydev->drv->soft_reset(phydev);
1087
1088 if (ret < 0)
1089 return ret;
1090
1091 ret = phy_scan_fixups(phydev);
1092 if (ret < 0)
1093 return ret;
1094
1095 if (phydev->drv->config_init)
1096 ret = phydev->drv->config_init(phydev);
1097
1098 return ret;
1099 }
1100 EXPORT_SYMBOL(phy_init_hw);
1101
1102 void phy_attached_info(struct phy_device *phydev)
1103 {
1104 phy_attached_print(phydev, NULL);
1105 }
1106 EXPORT_SYMBOL(phy_attached_info);
1107
1108 #define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%s)"
1109 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1110 {
1111 const char *drv_name = phydev->drv ? phydev->drv->name : "unbound";
1112 char *irq_str;
1113 char irq_num[8];
1114
1115 switch(phydev->irq) {
1116 case PHY_POLL:
1117 irq_str = "POLL";
1118 break;
1119 case PHY_IGNORE_INTERRUPT:
1120 irq_str = "IGNORE";
1121 break;
1122 default:
1123 snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
1124 irq_str = irq_num;
1125 break;
1126 }
1127
1128
1129 if (!fmt) {
1130 phydev_info(phydev, ATTACHED_FMT "\n",
1131 drv_name, phydev_name(phydev),
1132 irq_str);
1133 } else {
1134 va_list ap;
1135
1136 phydev_info(phydev, ATTACHED_FMT,
1137 drv_name, phydev_name(phydev),
1138 irq_str);
1139
1140 va_start(ap, fmt);
1141 vprintk(fmt, ap);
1142 va_end(ap);
1143 }
1144 }
1145 EXPORT_SYMBOL(phy_attached_print);
1146
1147 static void phy_sysfs_create_links(struct phy_device *phydev)
1148 {
1149 struct net_device *dev = phydev->attached_dev;
1150 int err;
1151
1152 if (!dev)
1153 return;
1154
1155 err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
1156 "attached_dev");
1157 if (err)
1158 return;
1159
1160 err = sysfs_create_link_nowarn(&dev->dev.kobj,
1161 &phydev->mdio.dev.kobj,
1162 "phydev");
1163 if (err) {
1164 dev_err(&dev->dev, "could not add device link to %s err %d\n",
1165 kobject_name(&phydev->mdio.dev.kobj),
1166 err);
1167
1168
1169
1170 }
1171
1172 phydev->sysfs_links = true;
1173 }
1174
1175 static ssize_t
1176 phy_standalone_show(struct device *dev, struct device_attribute *attr,
1177 char *buf)
1178 {
1179 struct phy_device *phydev = to_phy_device(dev);
1180
1181 return sprintf(buf, "%d\n", !phydev->attached_dev);
1182 }
1183 static DEVICE_ATTR_RO(phy_standalone);
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1201 u32 flags, phy_interface_t interface)
1202 {
1203 struct mii_bus *bus = phydev->mdio.bus;
1204 struct device *d = &phydev->mdio.dev;
1205 struct module *ndev_owner = NULL;
1206 bool using_genphy = false;
1207 int err;
1208
1209
1210
1211
1212
1213
1214 if (dev)
1215 ndev_owner = dev->dev.parent->driver->owner;
1216 if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
1217 phydev_err(phydev, "failed to get the bus module\n");
1218 return -EIO;
1219 }
1220
1221 get_device(d);
1222
1223
1224
1225
1226 if (!d->driver) {
1227 if (phydev->is_c45)
1228 d->driver = &genphy_c45_driver.mdiodrv.driver;
1229 else
1230 d->driver = &genphy_driver.mdiodrv.driver;
1231
1232 using_genphy = true;
1233 }
1234
1235 if (!try_module_get(d->driver->owner)) {
1236 phydev_err(phydev, "failed to get the device driver module\n");
1237 err = -EIO;
1238 goto error_put_device;
1239 }
1240
1241 if (using_genphy) {
1242 err = d->driver->probe(d);
1243 if (err >= 0)
1244 err = device_bind_driver(d);
1245
1246 if (err)
1247 goto error_module_put;
1248 }
1249
1250 if (phydev->attached_dev) {
1251 dev_err(&dev->dev, "PHY already attached\n");
1252 err = -EBUSY;
1253 goto error;
1254 }
1255
1256 phydev->phy_link_change = phy_link_change;
1257 if (dev) {
1258 phydev->attached_dev = dev;
1259 dev->phydev = phydev;
1260 }
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270 phydev->sysfs_links = false;
1271
1272 phy_sysfs_create_links(phydev);
1273
1274 if (!phydev->attached_dev) {
1275 err = sysfs_create_file(&phydev->mdio.dev.kobj,
1276 &dev_attr_phy_standalone.attr);
1277 if (err)
1278 phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n");
1279 }
1280
1281 phydev->dev_flags = flags;
1282
1283 phydev->interface = interface;
1284
1285 phydev->state = PHY_READY;
1286
1287
1288
1289
1290 if (dev)
1291 netif_carrier_off(phydev->attached_dev);
1292
1293
1294
1295
1296
1297 err = phy_init_hw(phydev);
1298 if (err)
1299 goto error;
1300
1301 phy_resume(phydev);
1302 phy_led_triggers_register(phydev);
1303
1304 return err;
1305
1306 error:
1307
1308 phy_detach(phydev);
1309 return err;
1310
1311 error_module_put:
1312 module_put(d->driver->owner);
1313 error_put_device:
1314 put_device(d);
1315 if (ndev_owner != bus->owner)
1316 module_put(bus->owner);
1317 return err;
1318 }
1319 EXPORT_SYMBOL(phy_attach_direct);
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1331 phy_interface_t interface)
1332 {
1333 struct bus_type *bus = &mdio_bus_type;
1334 struct phy_device *phydev;
1335 struct device *d;
1336 int rc;
1337
1338 if (!dev)
1339 return ERR_PTR(-EINVAL);
1340
1341
1342
1343
1344 d = bus_find_device_by_name(bus, NULL, bus_id);
1345 if (!d) {
1346 pr_err("PHY %s not found\n", bus_id);
1347 return ERR_PTR(-ENODEV);
1348 }
1349 phydev = to_phy_device(d);
1350
1351 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
1352 put_device(d);
1353 if (rc)
1354 return ERR_PTR(rc);
1355
1356 return phydev;
1357 }
1358 EXPORT_SYMBOL(phy_attach);
1359
1360 static bool phy_driver_is_genphy_kind(struct phy_device *phydev,
1361 struct device_driver *driver)
1362 {
1363 struct device *d = &phydev->mdio.dev;
1364 bool ret = false;
1365
1366 if (!phydev->drv)
1367 return ret;
1368
1369 get_device(d);
1370 ret = d->driver == driver;
1371 put_device(d);
1372
1373 return ret;
1374 }
1375
1376 bool phy_driver_is_genphy(struct phy_device *phydev)
1377 {
1378 return phy_driver_is_genphy_kind(phydev,
1379 &genphy_driver.mdiodrv.driver);
1380 }
1381 EXPORT_SYMBOL_GPL(phy_driver_is_genphy);
1382
1383 bool phy_driver_is_genphy_10g(struct phy_device *phydev)
1384 {
1385 return phy_driver_is_genphy_kind(phydev,
1386 &genphy_c45_driver.mdiodrv.driver);
1387 }
1388 EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g);
1389
1390
1391
1392
1393
1394
1395
1396
1397 void phy_detach(struct phy_device *phydev)
1398 {
1399 struct net_device *dev = phydev->attached_dev;
1400 struct module *ndev_owner = NULL;
1401 struct mii_bus *bus;
1402
1403 if (phydev->sysfs_links) {
1404 if (dev)
1405 sysfs_remove_link(&dev->dev.kobj, "phydev");
1406 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
1407 }
1408
1409 if (!phydev->attached_dev)
1410 sysfs_remove_file(&phydev->mdio.dev.kobj,
1411 &dev_attr_phy_standalone.attr);
1412
1413 phy_suspend(phydev);
1414 if (dev) {
1415 phydev->attached_dev->phydev = NULL;
1416 phydev->attached_dev = NULL;
1417 }
1418 phydev->phylink = NULL;
1419
1420 phy_led_triggers_unregister(phydev);
1421
1422 module_put(phydev->mdio.dev.driver->owner);
1423
1424
1425
1426
1427
1428
1429 if (phy_driver_is_genphy(phydev) ||
1430 phy_driver_is_genphy_10g(phydev))
1431 device_release_driver(&phydev->mdio.dev);
1432
1433
1434
1435
1436
1437 bus = phydev->mdio.bus;
1438
1439 put_device(&phydev->mdio.dev);
1440 if (dev)
1441 ndev_owner = dev->dev.parent->driver->owner;
1442 if (ndev_owner != bus->owner)
1443 module_put(bus->owner);
1444
1445
1446 phy_device_reset(phydev, 1);
1447 }
1448 EXPORT_SYMBOL(phy_detach);
1449
1450 int phy_suspend(struct phy_device *phydev)
1451 {
1452 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1453 struct net_device *netdev = phydev->attached_dev;
1454 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1455 int ret = 0;
1456
1457
1458 phy_ethtool_get_wol(phydev, &wol);
1459 if (wol.wolopts || (netdev && netdev->wol_enabled))
1460 return -EBUSY;
1461
1462 if (phydev->drv && phydrv->suspend)
1463 ret = phydrv->suspend(phydev);
1464
1465 if (ret)
1466 return ret;
1467
1468 phydev->suspended = true;
1469
1470 return ret;
1471 }
1472 EXPORT_SYMBOL(phy_suspend);
1473
1474 int __phy_resume(struct phy_device *phydev)
1475 {
1476 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1477 int ret = 0;
1478
1479 WARN_ON(!mutex_is_locked(&phydev->lock));
1480
1481 if (phydev->drv && phydrv->resume)
1482 ret = phydrv->resume(phydev);
1483
1484 if (ret)
1485 return ret;
1486
1487 phydev->suspended = false;
1488
1489 return ret;
1490 }
1491 EXPORT_SYMBOL(__phy_resume);
1492
1493 int phy_resume(struct phy_device *phydev)
1494 {
1495 int ret;
1496
1497 mutex_lock(&phydev->lock);
1498 ret = __phy_resume(phydev);
1499 mutex_unlock(&phydev->lock);
1500
1501 return ret;
1502 }
1503 EXPORT_SYMBOL(phy_resume);
1504
1505 int phy_loopback(struct phy_device *phydev, bool enable)
1506 {
1507 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1508 int ret = 0;
1509
1510 mutex_lock(&phydev->lock);
1511
1512 if (enable && phydev->loopback_enabled) {
1513 ret = -EBUSY;
1514 goto out;
1515 }
1516
1517 if (!enable && !phydev->loopback_enabled) {
1518 ret = -EINVAL;
1519 goto out;
1520 }
1521
1522 if (phydev->drv && phydrv->set_loopback)
1523 ret = phydrv->set_loopback(phydev, enable);
1524 else
1525 ret = -EOPNOTSUPP;
1526
1527 if (ret)
1528 goto out;
1529
1530 phydev->loopback_enabled = enable;
1531
1532 out:
1533 mutex_unlock(&phydev->lock);
1534 return ret;
1535 }
1536 EXPORT_SYMBOL(phy_loopback);
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547 int phy_reset_after_clk_enable(struct phy_device *phydev)
1548 {
1549 if (!phydev || !phydev->drv)
1550 return -ENODEV;
1551
1552 if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
1553 phy_device_reset(phydev, 1);
1554 phy_device_reset(phydev, 0);
1555 return 1;
1556 }
1557
1558 return 0;
1559 }
1560 EXPORT_SYMBOL(phy_reset_after_clk_enable);
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573 static int genphy_config_advert(struct phy_device *phydev)
1574 {
1575 int err, bmsr, changed = 0;
1576 u32 adv;
1577
1578
1579 linkmode_and(phydev->advertising, phydev->advertising,
1580 phydev->supported);
1581
1582 adv = linkmode_adv_to_mii_adv_t(phydev->advertising);
1583
1584
1585 err = phy_modify_changed(phydev, MII_ADVERTISE,
1586 ADVERTISE_ALL | ADVERTISE_100BASE4 |
1587 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
1588 adv);
1589 if (err < 0)
1590 return err;
1591 if (err > 0)
1592 changed = 1;
1593
1594 bmsr = phy_read(phydev, MII_BMSR);
1595 if (bmsr < 0)
1596 return bmsr;
1597
1598
1599
1600
1601
1602 if (!(bmsr & BMSR_ESTATEN))
1603 return changed;
1604
1605 adv = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
1606
1607 err = phy_modify_changed(phydev, MII_CTRL1000,
1608 ADVERTISE_1000FULL | ADVERTISE_1000HALF,
1609 adv);
1610 if (err < 0)
1611 return err;
1612 if (err > 0)
1613 changed = 1;
1614
1615 return changed;
1616 }
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626 int genphy_config_eee_advert(struct phy_device *phydev)
1627 {
1628 int err;
1629
1630
1631 if (!phydev->eee_broken_modes)
1632 return 0;
1633
1634 err = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
1635 phydev->eee_broken_modes, 0);
1636
1637 return err < 0 ? 0 : err;
1638 }
1639 EXPORT_SYMBOL(genphy_config_eee_advert);
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649 int genphy_setup_forced(struct phy_device *phydev)
1650 {
1651 u16 ctl = 0;
1652
1653 phydev->pause = 0;
1654 phydev->asym_pause = 0;
1655
1656 if (SPEED_1000 == phydev->speed)
1657 ctl |= BMCR_SPEED1000;
1658 else if (SPEED_100 == phydev->speed)
1659 ctl |= BMCR_SPEED100;
1660
1661 if (DUPLEX_FULL == phydev->duplex)
1662 ctl |= BMCR_FULLDPLX;
1663
1664 return phy_modify(phydev, MII_BMCR,
1665 ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
1666 }
1667 EXPORT_SYMBOL(genphy_setup_forced);
1668
1669
1670
1671
1672
1673 int genphy_restart_aneg(struct phy_device *phydev)
1674 {
1675
1676 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
1677 BMCR_ANENABLE | BMCR_ANRESTART);
1678 }
1679 EXPORT_SYMBOL(genphy_restart_aneg);
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690 int __genphy_config_aneg(struct phy_device *phydev, bool changed)
1691 {
1692 int err;
1693
1694 if (genphy_config_eee_advert(phydev))
1695 changed = true;
1696
1697 if (AUTONEG_ENABLE != phydev->autoneg)
1698 return genphy_setup_forced(phydev);
1699
1700 err = genphy_config_advert(phydev);
1701 if (err < 0)
1702 return err;
1703 else if (err)
1704 changed = true;
1705
1706 if (!changed) {
1707
1708
1709
1710 int ctl = phy_read(phydev, MII_BMCR);
1711
1712 if (ctl < 0)
1713 return ctl;
1714
1715 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
1716 changed = true;
1717 }
1718
1719
1720
1721
1722 return changed ? genphy_restart_aneg(phydev) : 0;
1723 }
1724 EXPORT_SYMBOL(__genphy_config_aneg);
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734 int genphy_aneg_done(struct phy_device *phydev)
1735 {
1736 int retval = phy_read(phydev, MII_BMSR);
1737
1738 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1739 }
1740 EXPORT_SYMBOL(genphy_aneg_done);
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750 int genphy_update_link(struct phy_device *phydev)
1751 {
1752 int status = 0, bmcr;
1753
1754 bmcr = phy_read(phydev, MII_BMCR);
1755 if (bmcr < 0)
1756 return bmcr;
1757
1758
1759
1760
1761 if (bmcr & BMCR_ANRESTART)
1762 goto done;
1763
1764
1765
1766
1767
1768 if (!phy_polling_mode(phydev)) {
1769 status = phy_read(phydev, MII_BMSR);
1770 if (status < 0)
1771 return status;
1772 else if (status & BMSR_LSTATUS)
1773 goto done;
1774 }
1775
1776
1777 status = phy_read(phydev, MII_BMSR);
1778 if (status < 0)
1779 return status;
1780 done:
1781 phydev->link = status & BMSR_LSTATUS ? 1 : 0;
1782 phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0;
1783
1784
1785
1786
1787 if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete)
1788 phydev->link = 0;
1789
1790 return 0;
1791 }
1792 EXPORT_SYMBOL(genphy_update_link);
1793
1794 int genphy_read_lpa(struct phy_device *phydev)
1795 {
1796 int lpa, lpagb;
1797
1798 if (phydev->autoneg == AUTONEG_ENABLE) {
1799 if (!phydev->autoneg_complete) {
1800 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
1801 0);
1802 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0);
1803 return 0;
1804 }
1805
1806 if (phydev->is_gigabit_capable) {
1807 lpagb = phy_read(phydev, MII_STAT1000);
1808 if (lpagb < 0)
1809 return lpagb;
1810
1811 if (lpagb & LPA_1000MSFAIL) {
1812 int adv = phy_read(phydev, MII_CTRL1000);
1813
1814 if (adv < 0)
1815 return adv;
1816
1817 if (adv & CTL1000_ENABLE_MASTER)
1818 phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
1819 else
1820 phydev_err(phydev, "Master/Slave resolution failed\n");
1821 return -ENOLINK;
1822 }
1823
1824 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
1825 lpagb);
1826 }
1827
1828 lpa = phy_read(phydev, MII_LPA);
1829 if (lpa < 0)
1830 return lpa;
1831
1832 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa);
1833 } else {
1834 linkmode_zero(phydev->lp_advertising);
1835 }
1836
1837 return 0;
1838 }
1839 EXPORT_SYMBOL(genphy_read_lpa);
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850 int genphy_read_status(struct phy_device *phydev)
1851 {
1852 int err, old_link = phydev->link;
1853
1854
1855 err = genphy_update_link(phydev);
1856 if (err)
1857 return err;
1858
1859
1860 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
1861 return 0;
1862
1863 phydev->speed = SPEED_UNKNOWN;
1864 phydev->duplex = DUPLEX_UNKNOWN;
1865 phydev->pause = 0;
1866 phydev->asym_pause = 0;
1867
1868 err = genphy_read_lpa(phydev);
1869 if (err < 0)
1870 return err;
1871
1872 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
1873 phy_resolve_aneg_linkmode(phydev);
1874 } else if (phydev->autoneg == AUTONEG_DISABLE) {
1875 int bmcr = phy_read(phydev, MII_BMCR);
1876
1877 if (bmcr < 0)
1878 return bmcr;
1879
1880 if (bmcr & BMCR_FULLDPLX)
1881 phydev->duplex = DUPLEX_FULL;
1882 else
1883 phydev->duplex = DUPLEX_HALF;
1884
1885 if (bmcr & BMCR_SPEED1000)
1886 phydev->speed = SPEED_1000;
1887 else if (bmcr & BMCR_SPEED100)
1888 phydev->speed = SPEED_100;
1889 else
1890 phydev->speed = SPEED_10;
1891 }
1892
1893 return 0;
1894 }
1895 EXPORT_SYMBOL(genphy_read_status);
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906 int genphy_soft_reset(struct phy_device *phydev)
1907 {
1908 u16 res = BMCR_RESET;
1909 int ret;
1910
1911 if (phydev->autoneg == AUTONEG_ENABLE)
1912 res |= BMCR_ANRESTART;
1913
1914 ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res);
1915 if (ret < 0)
1916 return ret;
1917
1918 ret = phy_poll_reset(phydev);
1919 if (ret)
1920 return ret;
1921
1922
1923 if (phydev->autoneg == AUTONEG_DISABLE)
1924 ret = genphy_setup_forced(phydev);
1925
1926 return ret;
1927 }
1928 EXPORT_SYMBOL(genphy_soft_reset);
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939 int genphy_read_abilities(struct phy_device *phydev)
1940 {
1941 int val;
1942
1943 linkmode_set_bit_array(phy_basic_ports_array,
1944 ARRAY_SIZE(phy_basic_ports_array),
1945 phydev->supported);
1946
1947 val = phy_read(phydev, MII_BMSR);
1948 if (val < 0)
1949 return val;
1950
1951 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported,
1952 val & BMSR_ANEGCAPABLE);
1953
1954 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported,
1955 val & BMSR_100FULL);
1956 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported,
1957 val & BMSR_100HALF);
1958 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported,
1959 val & BMSR_10FULL);
1960 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported,
1961 val & BMSR_10HALF);
1962
1963 if (val & BMSR_ESTATEN) {
1964 val = phy_read(phydev, MII_ESTATUS);
1965 if (val < 0)
1966 return val;
1967
1968 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1969 phydev->supported, val & ESTATUS_1000_TFULL);
1970 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1971 phydev->supported, val & ESTATUS_1000_THALF);
1972 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
1973 phydev->supported, val & ESTATUS_1000_XFULL);
1974 }
1975
1976 return 0;
1977 }
1978 EXPORT_SYMBOL(genphy_read_abilities);
1979
1980
1981
1982
1983
1984 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
1985 {
1986 return -EOPNOTSUPP;
1987 }
1988 EXPORT_SYMBOL(genphy_read_mmd_unsupported);
1989
1990 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
1991 u16 regnum, u16 val)
1992 {
1993 return -EOPNOTSUPP;
1994 }
1995 EXPORT_SYMBOL(genphy_write_mmd_unsupported);
1996
1997 int genphy_suspend(struct phy_device *phydev)
1998 {
1999 return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
2000 }
2001 EXPORT_SYMBOL(genphy_suspend);
2002
2003 int genphy_resume(struct phy_device *phydev)
2004 {
2005 return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
2006 }
2007 EXPORT_SYMBOL(genphy_resume);
2008
2009 int genphy_loopback(struct phy_device *phydev, bool enable)
2010 {
2011 return phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK,
2012 enable ? BMCR_LOOPBACK : 0);
2013 }
2014 EXPORT_SYMBOL(genphy_loopback);
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode)
2026 {
2027 linkmode_clear_bit(link_mode, phydev->supported);
2028 phy_advertise_supported(phydev);
2029 }
2030 EXPORT_SYMBOL(phy_remove_link_mode);
2031
2032 static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src)
2033 {
2034 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst,
2035 linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src));
2036 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst,
2037 linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src));
2038 }
2039
2040
2041
2042
2043
2044
2045
2046
2047 void phy_advertise_supported(struct phy_device *phydev)
2048 {
2049 __ETHTOOL_DECLARE_LINK_MODE_MASK(new);
2050
2051 linkmode_copy(new, phydev->supported);
2052 phy_copy_pause_bits(new, phydev->advertising);
2053 linkmode_copy(phydev->advertising, new);
2054 }
2055 EXPORT_SYMBOL(phy_advertise_supported);
2056
2057
2058
2059
2060
2061
2062
2063
2064 void phy_support_sym_pause(struct phy_device *phydev)
2065 {
2066 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
2067 phy_copy_pause_bits(phydev->advertising, phydev->supported);
2068 }
2069 EXPORT_SYMBOL(phy_support_sym_pause);
2070
2071
2072
2073
2074
2075
2076
2077 void phy_support_asym_pause(struct phy_device *phydev)
2078 {
2079 phy_copy_pause_bits(phydev->advertising, phydev->supported);
2080 }
2081 EXPORT_SYMBOL(phy_support_asym_pause);
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
2095 bool autoneg)
2096 {
2097 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
2098
2099 if (rx && tx && autoneg)
2100 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2101 phydev->supported);
2102
2103 linkmode_copy(phydev->advertising, phydev->supported);
2104 }
2105 EXPORT_SYMBOL(phy_set_sym_pause);
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx)
2119 {
2120 __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv);
2121
2122 linkmode_copy(oldadv, phydev->advertising);
2123
2124 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2125 phydev->advertising);
2126 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2127 phydev->advertising);
2128
2129 if (rx) {
2130 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2131 phydev->advertising);
2132 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2133 phydev->advertising);
2134 }
2135
2136 if (tx)
2137 linkmode_change_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2138 phydev->advertising);
2139
2140 if (!linkmode_equal(oldadv, phydev->advertising) &&
2141 phydev->autoneg)
2142 phy_start_aneg(phydev);
2143 }
2144 EXPORT_SYMBOL(phy_set_asym_pause);
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155 bool phy_validate_pause(struct phy_device *phydev,
2156 struct ethtool_pauseparam *pp)
2157 {
2158 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2159 phydev->supported) && pp->rx_pause)
2160 return false;
2161
2162 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2163 phydev->supported) &&
2164 pp->rx_pause != pp->tx_pause)
2165 return false;
2166
2167 return true;
2168 }
2169 EXPORT_SYMBOL(phy_validate_pause);
2170
2171 static bool phy_drv_supports_irq(struct phy_driver *phydrv)
2172 {
2173 return phydrv->config_intr && phydrv->ack_interrupt;
2174 }
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184 static int phy_probe(struct device *dev)
2185 {
2186 struct phy_device *phydev = to_phy_device(dev);
2187 struct device_driver *drv = phydev->mdio.dev.driver;
2188 struct phy_driver *phydrv = to_phy_driver(drv);
2189 int err = 0;
2190
2191 phydev->drv = phydrv;
2192
2193
2194
2195
2196 if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev))
2197 phydev->irq = PHY_POLL;
2198
2199 if (phydrv->flags & PHY_IS_INTERNAL)
2200 phydev->is_internal = true;
2201
2202 mutex_lock(&phydev->lock);
2203
2204 if (phydev->drv->probe) {
2205
2206 phy_device_reset(phydev, 0);
2207
2208 err = phydev->drv->probe(phydev);
2209 if (err) {
2210
2211 phy_device_reset(phydev, 1);
2212 goto out;
2213 }
2214 }
2215
2216
2217
2218
2219
2220 if (phydrv->features) {
2221 linkmode_copy(phydev->supported, phydrv->features);
2222 } else if (phydrv->get_features) {
2223 err = phydrv->get_features(phydev);
2224 } else if (phydev->is_c45) {
2225 err = genphy_c45_pma_read_abilities(phydev);
2226 } else {
2227 err = genphy_read_abilities(phydev);
2228 }
2229
2230 if (err)
2231 goto out;
2232
2233 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2234 phydev->supported))
2235 phydev->autoneg = 0;
2236
2237 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
2238 phydev->supported))
2239 phydev->is_gigabit_capable = 1;
2240 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2241 phydev->supported))
2242 phydev->is_gigabit_capable = 1;
2243
2244 of_set_phy_supported(phydev);
2245 phy_advertise_supported(phydev);
2246
2247
2248
2249
2250 of_set_phy_eee_broken(phydev);
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263 if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) &&
2264 !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) {
2265 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2266 phydev->supported);
2267 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2268 phydev->supported);
2269 }
2270
2271
2272 phydev->state = PHY_READY;
2273
2274 out:
2275 mutex_unlock(&phydev->lock);
2276
2277 return err;
2278 }
2279
2280 static int phy_remove(struct device *dev)
2281 {
2282 struct phy_device *phydev = to_phy_device(dev);
2283
2284 cancel_delayed_work_sync(&phydev->state_queue);
2285
2286 mutex_lock(&phydev->lock);
2287 phydev->state = PHY_DOWN;
2288 mutex_unlock(&phydev->lock);
2289
2290 if (phydev->drv && phydev->drv->remove) {
2291 phydev->drv->remove(phydev);
2292
2293
2294 phy_device_reset(phydev, 1);
2295 }
2296 phydev->drv = NULL;
2297
2298 return 0;
2299 }
2300
2301
2302
2303
2304
2305
2306 int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
2307 {
2308 int retval;
2309
2310
2311
2312
2313 if (WARN_ON(new_driver->features && new_driver->get_features)) {
2314 pr_err("%s: features and get_features must not both be set\n",
2315 new_driver->name);
2316 return -EINVAL;
2317 }
2318
2319 new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
2320 new_driver->mdiodrv.driver.name = new_driver->name;
2321 new_driver->mdiodrv.driver.bus = &mdio_bus_type;
2322 new_driver->mdiodrv.driver.probe = phy_probe;
2323 new_driver->mdiodrv.driver.remove = phy_remove;
2324 new_driver->mdiodrv.driver.owner = owner;
2325
2326 retval = driver_register(&new_driver->mdiodrv.driver);
2327 if (retval) {
2328 pr_err("%s: Error %d in registering driver\n",
2329 new_driver->name, retval);
2330
2331 return retval;
2332 }
2333
2334 pr_debug("%s: Registered new driver\n", new_driver->name);
2335
2336 return 0;
2337 }
2338 EXPORT_SYMBOL(phy_driver_register);
2339
2340 int phy_drivers_register(struct phy_driver *new_driver, int n,
2341 struct module *owner)
2342 {
2343 int i, ret = 0;
2344
2345 for (i = 0; i < n; i++) {
2346 ret = phy_driver_register(new_driver + i, owner);
2347 if (ret) {
2348 while (i-- > 0)
2349 phy_driver_unregister(new_driver + i);
2350 break;
2351 }
2352 }
2353 return ret;
2354 }
2355 EXPORT_SYMBOL(phy_drivers_register);
2356
2357 void phy_driver_unregister(struct phy_driver *drv)
2358 {
2359 driver_unregister(&drv->mdiodrv.driver);
2360 }
2361 EXPORT_SYMBOL(phy_driver_unregister);
2362
2363 void phy_drivers_unregister(struct phy_driver *drv, int n)
2364 {
2365 int i;
2366
2367 for (i = 0; i < n; i++)
2368 phy_driver_unregister(drv + i);
2369 }
2370 EXPORT_SYMBOL(phy_drivers_unregister);
2371
2372 static struct phy_driver genphy_driver = {
2373 .phy_id = 0xffffffff,
2374 .phy_id_mask = 0xffffffff,
2375 .name = "Generic PHY",
2376 .soft_reset = genphy_no_soft_reset,
2377 .get_features = genphy_read_abilities,
2378 .aneg_done = genphy_aneg_done,
2379 .suspend = genphy_suspend,
2380 .resume = genphy_resume,
2381 .set_loopback = genphy_loopback,
2382 };
2383
2384 static int __init phy_init(void)
2385 {
2386 int rc;
2387
2388 rc = mdio_bus_init();
2389 if (rc)
2390 return rc;
2391
2392 features_init();
2393
2394 rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE);
2395 if (rc)
2396 goto err_c45;
2397
2398 rc = phy_driver_register(&genphy_driver, THIS_MODULE);
2399 if (rc) {
2400 phy_driver_unregister(&genphy_c45_driver);
2401 err_c45:
2402 mdio_bus_exit();
2403 }
2404
2405 return rc;
2406 }
2407
2408 static void __exit phy_exit(void)
2409 {
2410 phy_driver_unregister(&genphy_c45_driver);
2411 phy_driver_unregister(&genphy_driver);
2412 mdio_bus_exit();
2413 }
2414
2415 subsys_initcall(phy_init);
2416 module_exit(phy_exit);