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