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