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