blob: d9b53ba6fd336902ae48d1a2e09c73c4a6bf6e57 [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
1319/**
1320 * phy_attach_direct - attach a network device to a given PHY device pointer
1321 * @dev: network device to attach
1322 * @phydev: Pointer to phy_device to attach
1323 * @flags: PHY device's dev_flags
1324 * @interface: PHY device's interface
1325 *
1326 * Description: Called by drivers to attach to a particular PHY
1327 * device. The phy_device is found, and properly hooked up
1328 * to the phy_driver. If no driver is attached, then a
1329 * generic driver is used. The phy_device is given a ptr to
1330 * the attaching device, and given a callback for link status
1331 * change. The phy_device is returned to the attaching driver.
1332 * This function takes a reference on the phy device.
1333 */
1334int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1335 u32 flags, phy_interface_t interface)
1336{
1337 struct mii_bus *bus = phydev->mdio.bus;
1338 struct device *d = &phydev->mdio.dev;
1339 struct module *ndev_owner = NULL;
1340 bool using_genphy = false;
1341 int err;
1342
1343 /* For Ethernet device drivers that register their own MDIO bus, we
1344 * will have bus->owner match ndev_mod, so we do not want to increment
1345 * our own module->refcnt here, otherwise we would not be able to
1346 * unload later on.
1347 */
1348 if (dev)
1349 ndev_owner = dev->dev.parent->driver->owner;
1350 if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
1351 phydev_err(phydev, "failed to get the bus module\n");
1352 return -EIO;
1353 }
1354
1355 get_device(d);
1356
1357 /* Assume that if there is no driver, that it doesn't
1358 * exist, and we should use the genphy driver.
1359 */
1360 if (!d->driver) {
1361 if (phydev->is_c45)
1362 d->driver = &genphy_c45_driver.mdiodrv.driver;
1363 else
1364 d->driver = &genphy_driver.mdiodrv.driver;
1365
1366 using_genphy = true;
1367 }
1368
1369 if (!try_module_get(d->driver->owner)) {
1370 phydev_err(phydev, "failed to get the device driver module\n");
1371 err = -EIO;
1372 goto error_put_device;
1373 }
1374
1375 if (using_genphy) {
1376 err = d->driver->probe(d);
1377 if (err >= 0)
1378 err = device_bind_driver(d);
1379
1380 if (err)
1381 goto error_module_put;
1382 }
1383
1384 if (phydev->attached_dev) {
1385 dev_err(&dev->dev, "PHY already attached\n");
1386 err = -EBUSY;
1387 goto error;
1388 }
1389
1390 phydev->phy_link_change = phy_link_change;
1391 if (dev) {
1392 phydev->attached_dev = dev;
1393 dev->phydev = phydev;
1394
1395 if (phydev->sfp_bus_attached)
1396 dev->sfp_bus = phydev->sfp_bus;
1397 }
1398
1399 /* Some Ethernet drivers try to connect to a PHY device before
1400 * calling register_netdevice() -> netdev_register_kobject() and
1401 * does the dev->dev.kobj initialization. Here we only check for
1402 * success which indicates that the network device kobject is
1403 * ready. Once we do that we still need to keep track of whether
1404 * links were successfully set up or not for phy_detach() to
1405 * remove them accordingly.
1406 */
1407 phydev->sysfs_links = false;
1408
1409 phy_sysfs_create_links(phydev);
1410
1411 if (!phydev->attached_dev) {
1412 err = sysfs_create_file(&phydev->mdio.dev.kobj,
1413 &dev_attr_phy_standalone.attr);
1414 if (err)
1415 phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n");
1416 }
1417
1418 phydev->dev_flags |= flags;
1419
1420 phydev->interface = interface;
1421
1422 phydev->state = PHY_READY;
1423
1424 /* Port is set to PORT_TP by default and the actual PHY driver will set
1425 * it to different value depending on the PHY configuration. If we have
1426 * the generic PHY driver we can't figure it out, thus set the old
1427 * legacy PORT_MII value.
1428 */
1429 if (using_genphy)
1430 phydev->port = PORT_MII;
1431
1432 /* Initial carrier state is off as the phy is about to be
1433 * (re)initialized.
1434 */
1435 if (dev)
1436 netif_carrier_off(phydev->attached_dev);
1437
1438 /* Do initial configuration here, now that
1439 * we have certain key parameters
1440 * (dev_flags and interface)
1441 */
1442 err = phy_init_hw(phydev);
1443 if (err)
1444 goto error;
1445
1446 err = phy_disable_interrupts(phydev);
1447 if (err)
1448 return err;
1449
1450 phy_resume(phydev);
1451 phy_led_triggers_register(phydev);
1452
1453 return err;
1454
1455error:
1456 /* phy_detach() does all of the cleanup below */
1457 phy_detach(phydev);
1458 return err;
1459
1460error_module_put:
1461 module_put(d->driver->owner);
1462error_put_device:
1463 put_device(d);
1464 if (ndev_owner != bus->owner)
1465 module_put(bus->owner);
1466 return err;
1467}
1468EXPORT_SYMBOL(phy_attach_direct);
1469
1470/**
1471 * phy_attach - attach a network device to a particular PHY device
1472 * @dev: network device to attach
1473 * @bus_id: Bus ID of PHY device to attach
1474 * @interface: PHY device's interface
1475 *
1476 * Description: Same as phy_attach_direct() except that a PHY bus_id
1477 * string is passed instead of a pointer to a struct phy_device.
1478 */
1479struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1480 phy_interface_t interface)
1481{
1482 struct bus_type *bus = &mdio_bus_type;
1483 struct phy_device *phydev;
1484 struct device *d;
1485 int rc;
1486
1487 if (!dev)
1488 return ERR_PTR(-EINVAL);
1489
1490 /* Search the list of PHY devices on the mdio bus for the
1491 * PHY with the requested name
1492 */
1493 d = bus_find_device_by_name(bus, NULL, bus_id);
1494 if (!d) {
1495 pr_err("PHY %s not found\n", bus_id);
1496 return ERR_PTR(-ENODEV);
1497 }
1498 phydev = to_phy_device(d);
1499
1500 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
1501 put_device(d);
1502 if (rc)
1503 return ERR_PTR(rc);
1504
1505 return phydev;
1506}
1507EXPORT_SYMBOL(phy_attach);
1508
1509static bool phy_driver_is_genphy_kind(struct phy_device *phydev,
1510 struct device_driver *driver)
1511{
1512 struct device *d = &phydev->mdio.dev;
1513 bool ret = false;
1514
1515 if (!phydev->drv)
1516 return ret;
1517
1518 get_device(d);
1519 ret = d->driver == driver;
1520 put_device(d);
1521
1522 return ret;
1523}
1524
1525bool phy_driver_is_genphy(struct phy_device *phydev)
1526{
1527 return phy_driver_is_genphy_kind(phydev,
1528 &genphy_driver.mdiodrv.driver);
1529}
1530EXPORT_SYMBOL_GPL(phy_driver_is_genphy);
1531
1532bool phy_driver_is_genphy_10g(struct phy_device *phydev)
1533{
1534 return phy_driver_is_genphy_kind(phydev,
1535 &genphy_c45_driver.mdiodrv.driver);
1536}
1537EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g);
1538
1539/**
1540 * phy_package_join - join a common PHY group
1541 * @phydev: target phy_device struct
1542 * @addr: cookie and PHY address for global register access
1543 * @priv_size: if non-zero allocate this amount of bytes for private data
1544 *
1545 * This joins a PHY group and provides a shared storage for all phydevs in
1546 * this group. This is intended to be used for packages which contain
1547 * more than one PHY, for example a quad PHY transceiver.
1548 *
1549 * The addr parameter serves as a cookie which has to have the same value
1550 * for all members of one group and as a PHY address to access generic
1551 * registers of a PHY package. Usually, one of the PHY addresses of the
1552 * different PHYs in the package provides access to these global registers.
1553 * The address which is given here, will be used in the phy_package_read()
1554 * and phy_package_write() convenience functions. If your PHY doesn't have
1555 * global registers you can just pick any of the PHY addresses.
1556 *
1557 * This will set the shared pointer of the phydev to the shared storage.
1558 * If this is the first call for a this cookie the shared storage will be
1559 * allocated. If priv_size is non-zero, the given amount of bytes are
1560 * allocated for the priv member.
1561 *
1562 * Returns < 1 on error, 0 on success. Esp. calling phy_package_join()
1563 * with the same cookie but a different priv_size is an error.
1564 */
1565int phy_package_join(struct phy_device *phydev, int addr, size_t priv_size)
1566{
1567 struct mii_bus *bus = phydev->mdio.bus;
1568 struct phy_package_shared *shared;
1569 int ret;
1570
1571 if (addr < 0 || addr >= PHY_MAX_ADDR)
1572 return -EINVAL;
1573
1574 mutex_lock(&bus->shared_lock);
1575 shared = bus->shared[addr];
1576 if (!shared) {
1577 ret = -ENOMEM;
1578 shared = kzalloc(sizeof(*shared), GFP_KERNEL);
1579 if (!shared)
1580 goto err_unlock;
1581 if (priv_size) {
1582 shared->priv = kzalloc(priv_size, GFP_KERNEL);
1583 if (!shared->priv)
1584 goto err_free;
1585 shared->priv_size = priv_size;
1586 }
1587 shared->addr = addr;
1588 refcount_set(&shared->refcnt, 1);
1589 bus->shared[addr] = shared;
1590 } else {
1591 ret = -EINVAL;
1592 if (priv_size && priv_size != shared->priv_size)
1593 goto err_unlock;
1594 refcount_inc(&shared->refcnt);
1595 }
1596 mutex_unlock(&bus->shared_lock);
1597
1598 phydev->shared = shared;
1599
1600 return 0;
1601
1602err_free:
1603 kfree(shared);
1604err_unlock:
1605 mutex_unlock(&bus->shared_lock);
1606 return ret;
1607}
1608EXPORT_SYMBOL_GPL(phy_package_join);
1609
1610/**
1611 * phy_package_leave - leave a common PHY group
1612 * @phydev: target phy_device struct
1613 *
1614 * This leaves a PHY group created by phy_package_join(). If this phydev
1615 * was the last user of the shared data between the group, this data is
1616 * freed. Resets the phydev->shared pointer to NULL.
1617 */
1618void phy_package_leave(struct phy_device *phydev)
1619{
1620 struct phy_package_shared *shared = phydev->shared;
1621 struct mii_bus *bus = phydev->mdio.bus;
1622
1623 if (!shared)
1624 return;
1625
1626 if (refcount_dec_and_mutex_lock(&shared->refcnt, &bus->shared_lock)) {
1627 bus->shared[shared->addr] = NULL;
1628 mutex_unlock(&bus->shared_lock);
1629 kfree(shared->priv);
1630 kfree(shared);
1631 }
1632
1633 phydev->shared = NULL;
1634}
1635EXPORT_SYMBOL_GPL(phy_package_leave);
1636
1637static void devm_phy_package_leave(struct device *dev, void *res)
1638{
1639 phy_package_leave(*(struct phy_device **)res);
1640}
1641
1642/**
1643 * devm_phy_package_join - resource managed phy_package_join()
1644 * @dev: device that is registering this PHY package
1645 * @phydev: target phy_device struct
1646 * @addr: cookie and PHY address for global register access
1647 * @priv_size: if non-zero allocate this amount of bytes for private data
1648 *
1649 * Managed phy_package_join(). Shared storage fetched by this function,
1650 * phy_package_leave() is automatically called on driver detach. See
1651 * phy_package_join() for more information.
1652 */
1653int devm_phy_package_join(struct device *dev, struct phy_device *phydev,
1654 int addr, size_t priv_size)
1655{
1656 struct phy_device **ptr;
1657 int ret;
1658
1659 ptr = devres_alloc(devm_phy_package_leave, sizeof(*ptr),
1660 GFP_KERNEL);
1661 if (!ptr)
1662 return -ENOMEM;
1663
1664 ret = phy_package_join(phydev, addr, priv_size);
1665
1666 if (!ret) {
1667 *ptr = phydev;
1668 devres_add(dev, ptr);
1669 } else {
1670 devres_free(ptr);
1671 }
1672
1673 return ret;
1674}
1675EXPORT_SYMBOL_GPL(devm_phy_package_join);
1676
1677/**
1678 * phy_detach - detach a PHY device from its network device
1679 * @phydev: target phy_device struct
1680 *
1681 * This detaches the phy device from its network device and the phy
1682 * driver, and drops the reference count taken in phy_attach_direct().
1683 */
1684void phy_detach(struct phy_device *phydev)
1685{
1686 struct net_device *dev = phydev->attached_dev;
1687 struct module *ndev_owner = NULL;
1688 struct mii_bus *bus;
1689
1690 if (phydev->sysfs_links) {
1691 if (dev)
1692 sysfs_remove_link(&dev->dev.kobj, "phydev");
1693 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
1694 }
1695
1696 if (!phydev->attached_dev)
1697 sysfs_remove_file(&phydev->mdio.dev.kobj,
1698 &dev_attr_phy_standalone.attr);
1699
1700 phy_suspend(phydev);
1701 if (dev) {
1702 phydev->attached_dev->phydev = NULL;
1703 phydev->attached_dev = NULL;
1704 }
1705 phydev->phylink = NULL;
1706
1707 phy_led_triggers_unregister(phydev);
1708
1709 if (phydev->mdio.dev.driver)
1710 module_put(phydev->mdio.dev.driver->owner);
1711
1712 /* If the device had no specific driver before (i.e. - it
1713 * was using the generic driver), we unbind the device
1714 * from the generic driver so that there's a chance a
1715 * real driver could be loaded
1716 */
1717 if (phy_driver_is_genphy(phydev) ||
1718 phy_driver_is_genphy_10g(phydev))
1719 device_release_driver(&phydev->mdio.dev);
1720
1721 /* Assert the reset signal */
1722 phy_device_reset(phydev, 1);
1723
1724 /*
1725 * The phydev might go away on the put_device() below, so avoid
1726 * a use-after-free bug by reading the underlying bus first.
1727 */
1728 bus = phydev->mdio.bus;
1729
1730 put_device(&phydev->mdio.dev);
1731 if (dev)
1732 ndev_owner = dev->dev.parent->driver->owner;
1733 if (ndev_owner != bus->owner)
1734 module_put(bus->owner);
1735}
1736EXPORT_SYMBOL(phy_detach);
1737
1738int phy_suspend(struct phy_device *phydev)
1739{
1740 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1741 struct net_device *netdev = phydev->attached_dev;
1742 struct phy_driver *phydrv = phydev->drv;
1743 int ret;
1744
1745 if (phydev->suspended)
1746 return 0;
1747
1748 /* If the device has WOL enabled, we cannot suspend the PHY */
1749 phy_ethtool_get_wol(phydev, &wol);
1750 if (wol.wolopts || (netdev && netdev->wol_enabled))
1751 return -EBUSY;
1752
1753 if (!phydrv || !phydrv->suspend)
1754 return 0;
1755
1756 ret = phydrv->suspend(phydev);
1757 if (!ret)
1758 phydev->suspended = true;
1759
1760 return ret;
1761}
1762EXPORT_SYMBOL(phy_suspend);
1763
1764int __phy_resume(struct phy_device *phydev)
1765{
1766 struct phy_driver *phydrv = phydev->drv;
1767 int ret;
1768
1769 WARN_ON(!mutex_is_locked(&phydev->lock));
1770
1771 if (!phydrv || !phydrv->resume)
1772 return 0;
1773
1774 ret = phydrv->resume(phydev);
1775 if (!ret)
1776 phydev->suspended = false;
1777
1778 return ret;
1779}
1780EXPORT_SYMBOL(__phy_resume);
1781
1782int phy_resume(struct phy_device *phydev)
1783{
1784 int ret;
1785
1786 mutex_lock(&phydev->lock);
1787 ret = __phy_resume(phydev);
1788 mutex_unlock(&phydev->lock);
1789
1790 return ret;
1791}
1792EXPORT_SYMBOL(phy_resume);
1793
1794int phy_loopback(struct phy_device *phydev, bool enable)
1795{
1796 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1797 int ret = 0;
1798
1799 mutex_lock(&phydev->lock);
1800
1801 if (enable && phydev->loopback_enabled) {
1802 ret = -EBUSY;
1803 goto out;
1804 }
1805
1806 if (!enable && !phydev->loopback_enabled) {
1807 ret = -EINVAL;
1808 goto out;
1809 }
1810
1811 if (phydev->drv && phydrv->set_loopback)
1812 ret = phydrv->set_loopback(phydev, enable);
1813 else
1814 ret = -EOPNOTSUPP;
1815
1816 if (ret)
1817 goto out;
1818
1819 phydev->loopback_enabled = enable;
1820
1821out:
1822 mutex_unlock(&phydev->lock);
1823 return ret;
1824}
1825EXPORT_SYMBOL(phy_loopback);
1826
1827/**
1828 * phy_reset_after_clk_enable - perform a PHY reset if needed
1829 * @phydev: target phy_device struct
1830 *
1831 * Description: Some PHYs are known to need a reset after their refclk was
1832 * enabled. This function evaluates the flags and perform the reset if it's
1833 * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
1834 * was reset.
1835 */
1836int phy_reset_after_clk_enable(struct phy_device *phydev)
1837{
1838 if (!phydev || !phydev->drv)
1839 return -ENODEV;
1840
1841 if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
1842 phy_device_reset(phydev, 1);
1843 phy_device_reset(phydev, 0);
1844 return 1;
1845 }
1846
1847 return 0;
1848}
1849EXPORT_SYMBOL(phy_reset_after_clk_enable);
1850
1851/* Generic PHY support and helper functions */
1852
1853/**
1854 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
1855 * @phydev: target phy_device struct
1856 *
1857 * Description: Writes MII_ADVERTISE with the appropriate values,
1858 * after sanitizing the values to make sure we only advertise
1859 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
1860 * hasn't changed, and > 0 if it has changed.
1861 */
1862static int genphy_config_advert(struct phy_device *phydev)
1863{
1864 int err, bmsr, changed = 0;
1865 u32 adv;
1866
1867 /* Only allow advertising what this PHY supports */
1868 linkmode_and(phydev->advertising, phydev->advertising,
1869 phydev->supported);
1870
1871 adv = linkmode_adv_to_mii_adv_t(phydev->advertising);
1872
1873 /* Setup standard advertisement */
1874 err = phy_modify_changed(phydev, MII_ADVERTISE,
1875 ADVERTISE_ALL | ADVERTISE_100BASE4 |
1876 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
1877 adv);
1878 if (err < 0)
1879 return err;
1880 if (err > 0)
1881 changed = 1;
1882
1883 bmsr = phy_read(phydev, MII_BMSR);
1884 if (bmsr < 0)
1885 return bmsr;
1886
1887 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
1888 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
1889 * logical 1.
1890 */
1891 if (!(bmsr & BMSR_ESTATEN))
1892 return changed;
1893
1894 adv = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
1895
1896 err = phy_modify_changed(phydev, MII_CTRL1000,
1897 ADVERTISE_1000FULL | ADVERTISE_1000HALF,
1898 adv);
1899 if (err < 0)
1900 return err;
1901 if (err > 0)
1902 changed = 1;
1903
1904 return changed;
1905}
1906
1907/**
1908 * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters
1909 * @phydev: target phy_device struct
1910 *
1911 * Description: Writes MII_ADVERTISE with the appropriate values,
1912 * after sanitizing the values to make sure we only advertise
1913 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
1914 * hasn't changed, and > 0 if it has changed. This function is intended
1915 * for Clause 37 1000Base-X mode.
1916 */
1917static int genphy_c37_config_advert(struct phy_device *phydev)
1918{
1919 u16 adv = 0;
1920
1921 /* Only allow advertising what this PHY supports */
1922 linkmode_and(phydev->advertising, phydev->advertising,
1923 phydev->supported);
1924
1925 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
1926 phydev->advertising))
1927 adv |= ADVERTISE_1000XFULL;
1928 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
1929 phydev->advertising))
1930 adv |= ADVERTISE_1000XPAUSE;
1931 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
1932 phydev->advertising))
1933 adv |= ADVERTISE_1000XPSE_ASYM;
1934
1935 return phy_modify_changed(phydev, MII_ADVERTISE,
1936 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1937 ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM,
1938 adv);
1939}
1940
1941/**
1942 * genphy_config_eee_advert - disable unwanted eee mode advertisement
1943 * @phydev: target phy_device struct
1944 *
1945 * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
1946 * efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
1947 * changed, and 1 if it has changed.
1948 */
1949int genphy_config_eee_advert(struct phy_device *phydev)
1950{
1951 int err;
1952
1953 /* Nothing to disable */
1954 if (!phydev->eee_broken_modes)
1955 return 0;
1956
1957 err = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
1958 phydev->eee_broken_modes, 0);
1959 /* If the call failed, we assume that EEE is not supported */
1960 return err < 0 ? 0 : err;
1961}
1962EXPORT_SYMBOL(genphy_config_eee_advert);
1963
1964/**
1965 * genphy_setup_forced - configures/forces speed/duplex from @phydev
1966 * @phydev: target phy_device struct
1967 *
1968 * Description: Configures MII_BMCR to force speed/duplex
1969 * to the values in phydev. Assumes that the values are valid.
1970 * Please see phy_sanitize_settings().
1971 */
1972int genphy_setup_forced(struct phy_device *phydev)
1973{
1974 u16 ctl = 0;
1975
1976 phydev->pause = 0;
1977 phydev->asym_pause = 0;
1978
1979 if (SPEED_1000 == phydev->speed)
1980 ctl |= BMCR_SPEED1000;
1981 else if (SPEED_100 == phydev->speed)
1982 ctl |= BMCR_SPEED100;
1983
1984 if (DUPLEX_FULL == phydev->duplex)
1985 ctl |= BMCR_FULLDPLX;
1986
1987 return phy_modify(phydev, MII_BMCR,
1988 ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
1989}
1990EXPORT_SYMBOL(genphy_setup_forced);
1991
1992static int genphy_setup_master_slave(struct phy_device *phydev)
1993{
1994 u16 ctl = 0;
1995
1996 if (!phydev->is_gigabit_capable)
1997 return 0;
1998
1999 switch (phydev->master_slave_set) {
2000 case MASTER_SLAVE_CFG_MASTER_PREFERRED:
2001 ctl |= CTL1000_PREFER_MASTER;
2002 break;
2003 case MASTER_SLAVE_CFG_SLAVE_PREFERRED:
2004 break;
2005 case MASTER_SLAVE_CFG_MASTER_FORCE:
2006 ctl |= CTL1000_AS_MASTER;
2007 fallthrough;
2008 case MASTER_SLAVE_CFG_SLAVE_FORCE:
2009 ctl |= CTL1000_ENABLE_MASTER;
2010 break;
2011 case MASTER_SLAVE_CFG_UNKNOWN:
2012 case MASTER_SLAVE_CFG_UNSUPPORTED:
2013 return 0;
2014 default:
2015 phydev_warn(phydev, "Unsupported Master/Slave mode\n");
2016 return -EOPNOTSUPP;
2017 }
2018
2019 return phy_modify_changed(phydev, MII_CTRL1000,
2020 (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER |
2021 CTL1000_PREFER_MASTER), ctl);
2022}
2023
2024static int genphy_read_master_slave(struct phy_device *phydev)
2025{
2026 int cfg, state;
2027 int val;
2028
2029 if (!phydev->is_gigabit_capable) {
2030 phydev->master_slave_get = MASTER_SLAVE_CFG_UNSUPPORTED;
2031 phydev->master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
2032 return 0;
2033 }
2034
2035 phydev->master_slave_get = MASTER_SLAVE_CFG_UNKNOWN;
2036 phydev->master_slave_state = MASTER_SLAVE_STATE_UNKNOWN;
2037
2038 val = phy_read(phydev, MII_CTRL1000);
2039 if (val < 0)
2040 return val;
2041
2042 if (val & CTL1000_ENABLE_MASTER) {
2043 if (val & CTL1000_AS_MASTER)
2044 cfg = MASTER_SLAVE_CFG_MASTER_FORCE;
2045 else
2046 cfg = MASTER_SLAVE_CFG_SLAVE_FORCE;
2047 } else {
2048 if (val & CTL1000_PREFER_MASTER)
2049 cfg = MASTER_SLAVE_CFG_MASTER_PREFERRED;
2050 else
2051 cfg = MASTER_SLAVE_CFG_SLAVE_PREFERRED;
2052 }
2053
2054 val = phy_read(phydev, MII_STAT1000);
2055 if (val < 0)
2056 return val;
2057
2058 if (val & LPA_1000MSFAIL) {
2059 state = MASTER_SLAVE_STATE_ERR;
2060 } else if (phydev->link) {
2061 /* this bits are valid only for active link */
2062 if (val & LPA_1000MSRES)
2063 state = MASTER_SLAVE_STATE_MASTER;
2064 else
2065 state = MASTER_SLAVE_STATE_SLAVE;
2066 } else {
2067 state = MASTER_SLAVE_STATE_UNKNOWN;
2068 }
2069
2070 phydev->master_slave_get = cfg;
2071 phydev->master_slave_state = state;
2072
2073 return 0;
2074}
2075
2076/**
2077 * genphy_restart_aneg - Enable and Restart Autonegotiation
2078 * @phydev: target phy_device struct
2079 */
2080int genphy_restart_aneg(struct phy_device *phydev)
2081{
2082 /* Don't isolate the PHY if we're negotiating */
2083 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
2084 BMCR_ANENABLE | BMCR_ANRESTART);
2085}
2086EXPORT_SYMBOL(genphy_restart_aneg);
2087
2088/**
2089 * genphy_check_and_restart_aneg - Enable and restart auto-negotiation
2090 * @phydev: target phy_device struct
2091 * @restart: whether aneg restart is requested
2092 *
2093 * Check, and restart auto-negotiation if needed.
2094 */
2095int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart)
2096{
2097 int ret;
2098
2099 if (!restart) {
2100 /* Advertisement hasn't changed, but maybe aneg was never on to
2101 * begin with? Or maybe phy was isolated?
2102 */
2103 ret = phy_read(phydev, MII_BMCR);
2104 if (ret < 0)
2105 return ret;
2106
2107 if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE))
2108 restart = true;
2109 }
2110
2111 if (restart)
2112 return genphy_restart_aneg(phydev);
2113
2114 return 0;
2115}
2116EXPORT_SYMBOL(genphy_check_and_restart_aneg);
2117
2118/**
2119 * __genphy_config_aneg - restart auto-negotiation or write BMCR
2120 * @phydev: target phy_device struct
2121 * @changed: whether autoneg is requested
2122 *
2123 * Description: If auto-negotiation is enabled, we configure the
2124 * advertising, and then restart auto-negotiation. If it is not
2125 * enabled, then we write the BMCR.
2126 */
2127int __genphy_config_aneg(struct phy_device *phydev, bool changed)
2128{
2129 int err;
2130
2131 if (genphy_config_eee_advert(phydev))
2132 changed = true;
2133
2134 err = genphy_setup_master_slave(phydev);
2135 if (err < 0)
2136 return err;
2137 else if (err)
2138 changed = true;
2139
2140 if (AUTONEG_ENABLE != phydev->autoneg)
2141 return genphy_setup_forced(phydev);
2142
2143 err = genphy_config_advert(phydev);
2144 if (err < 0) /* error */
2145 return err;
2146 else if (err)
2147 changed = true;
2148
2149 return genphy_check_and_restart_aneg(phydev, changed);
2150}
2151EXPORT_SYMBOL(__genphy_config_aneg);
2152
2153/**
2154 * genphy_c37_config_aneg - restart auto-negotiation or write BMCR
2155 * @phydev: target phy_device struct
2156 *
2157 * Description: If auto-negotiation is enabled, we configure the
2158 * advertising, and then restart auto-negotiation. If it is not
2159 * enabled, then we write the BMCR. This function is intended
2160 * for use with Clause 37 1000Base-X mode.
2161 */
2162int genphy_c37_config_aneg(struct phy_device *phydev)
2163{
2164 int err, changed;
2165
2166 if (phydev->autoneg != AUTONEG_ENABLE)
2167 return genphy_setup_forced(phydev);
2168
2169 err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100,
2170 BMCR_SPEED1000);
2171 if (err)
2172 return err;
2173
2174 changed = genphy_c37_config_advert(phydev);
2175 if (changed < 0) /* error */
2176 return changed;
2177
2178 if (!changed) {
2179 /* Advertisement hasn't changed, but maybe aneg was never on to
2180 * begin with? Or maybe phy was isolated?
2181 */
2182 int ctl = phy_read(phydev, MII_BMCR);
2183
2184 if (ctl < 0)
2185 return ctl;
2186
2187 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
2188 changed = 1; /* do restart aneg */
2189 }
2190
2191 /* Only restart aneg if we are advertising something different
2192 * than we were before.
2193 */
2194 if (changed > 0)
2195 return genphy_restart_aneg(phydev);
2196
2197 return 0;
2198}
2199EXPORT_SYMBOL(genphy_c37_config_aneg);
2200
2201/**
2202 * genphy_aneg_done - return auto-negotiation status
2203 * @phydev: target phy_device struct
2204 *
2205 * Description: Reads the status register and returns 0 either if
2206 * auto-negotiation is incomplete, or if there was an error.
2207 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
2208 */
2209int genphy_aneg_done(struct phy_device *phydev)
2210{
2211 int retval = phy_read(phydev, MII_BMSR);
2212
2213 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
2214}
2215EXPORT_SYMBOL(genphy_aneg_done);
2216
2217/**
2218 * genphy_update_link - update link status in @phydev
2219 * @phydev: target phy_device struct
2220 *
2221 * Description: Update the value in phydev->link to reflect the
2222 * current link value. In order to do this, we need to read
2223 * the status register twice, keeping the second value.
2224 */
2225int genphy_update_link(struct phy_device *phydev)
2226{
2227 int status = 0, bmcr;
2228#ifdef CONFIG_MARVELL_88Q1110 //zw.wang phy driver support for Marvell_88q1110 on 20240417
2229 bmcr = phy_read_cl(phydev, MII_BMCR);
2230#else
2231 bmcr = phy_read(phydev, MII_BMCR);
2232#endif
2233 if (bmcr < 0)
2234 return bmcr;
2235
2236 /* Autoneg is being started, therefore disregard BMSR value and
2237 * report link as down.
2238 */
2239 if (bmcr & BMCR_ANRESTART)
2240 goto done;
2241
2242 /* The link state is latched low so that momentary link
2243 * drops can be detected. Do not double-read the status
2244 * in polling mode to detect such short link drops except
2245 * the link was already down.
2246 */
2247 if (!phy_polling_mode(phydev) || !phydev->link) {
2248#ifdef CONFIG_MARVELL_88Q1110 //zw.wang phy driver support for Marvell_88q1110 on 20240417
2249 status = phy_read_cl(phydev, MII_BMSR);
2250#else
2251 status = phy_read(phydev, MII_BMSR);
2252#endif
2253 if (status < 0)
2254 return status;
2255 else if (status & BMSR_LSTATUS)
2256 goto done;
2257 }
2258
2259 /* Read link and autonegotiation status */
2260#ifdef CONFIG_MARVELL_88Q1110 //zw.wang phy driver support for Marvell_88q1110 on 20240417
2261 status = phy_read_cl(phydev, MII_BMSR);
2262#else
2263 status = phy_read(phydev, MII_BMSR);
2264#endif
2265 if (status < 0)
2266 return status;
2267done:
2268 phydev->link = status & BMSR_LSTATUS ? 1 : 0;
2269 phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0;
2270
2271 /* Consider the case that autoneg was started and "aneg complete"
2272 * bit has been reset, but "link up" bit not yet.
2273 */
2274 if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete)
2275 phydev->link = 0;
2276
2277 return 0;
2278}
2279EXPORT_SYMBOL(genphy_update_link);
2280
2281int genphy_read_lpa(struct phy_device *phydev)
2282{
2283 int lpa, lpagb;
2284
2285 if (phydev->autoneg == AUTONEG_ENABLE) {
2286 if (!phydev->autoneg_complete) {
2287 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
2288 0);
2289 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0);
2290 return 0;
2291 }
2292
2293 if (phydev->is_gigabit_capable) {
2294 lpagb = phy_read(phydev, MII_STAT1000);
2295 if (lpagb < 0)
2296 return lpagb;
2297
2298 if (lpagb & LPA_1000MSFAIL) {
2299 int adv = phy_read(phydev, MII_CTRL1000);
2300
2301 if (adv < 0)
2302 return adv;
2303
2304 if (adv & CTL1000_ENABLE_MASTER)
2305 phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
2306 else
2307 phydev_err(phydev, "Master/Slave resolution failed\n");
2308 return -ENOLINK;
2309 }
2310
2311 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
2312 lpagb);
2313 }
2314
2315 lpa = phy_read(phydev, MII_LPA);
2316 if (lpa < 0)
2317 return lpa;
2318
2319 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa);
2320 } else {
2321 linkmode_zero(phydev->lp_advertising);
2322 }
2323
2324 return 0;
2325}
2326EXPORT_SYMBOL(genphy_read_lpa);
2327
2328/**
2329 * genphy_read_status_fixed - read the link parameters for !aneg mode
2330 * @phydev: target phy_device struct
2331 *
2332 * Read the current duplex and speed state for a PHY operating with
2333 * autonegotiation disabled.
2334 */
2335int genphy_read_status_fixed(struct phy_device *phydev)
2336{
2337#ifdef CONFIG_MARVELL_88Q1110 //zw.wang phy driver support for Marvell_88q1110 on 20240417
2338 int bmcr = phy_read_cl(phydev, MII_BMCR);
2339#else
2340 int bmcr = phy_read(phydev, MII_BMCR);
2341#endif
2342
2343 if (bmcr < 0)
2344 return bmcr;
2345
2346 if (bmcr & BMCR_FULLDPLX)
2347 phydev->duplex = DUPLEX_FULL;
2348 else
2349 phydev->duplex = DUPLEX_HALF;
2350
2351 if (bmcr & BMCR_SPEED1000)
2352 phydev->speed = SPEED_1000;
2353 else if (bmcr & BMCR_SPEED100)
2354 phydev->speed = SPEED_100;
2355 else
2356 phydev->speed = SPEED_10;
2357
2358 return 0;
2359}
2360EXPORT_SYMBOL(genphy_read_status_fixed);
2361
2362/**
2363 * genphy_read_status - check the link status and update current link state
2364 * @phydev: target phy_device struct
2365 *
2366 * Description: Check the link, then figure out the current state
2367 * by comparing what we advertise with what the link partner
2368 * advertises. Start by checking the gigabit possibilities,
2369 * then move on to 10/100.
2370 */
2371int genphy_read_status(struct phy_device *phydev)
2372{
2373 int err, old_link = phydev->link;
2374
2375 /* Update the link, but return if there was an error */
2376 err = genphy_update_link(phydev);
2377 if (err)
2378 return err;
2379
2380 /* why bother the PHY if nothing can have changed */
2381 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
2382 return 0;
2383
2384 phydev->speed = SPEED_UNKNOWN;
2385 phydev->duplex = DUPLEX_UNKNOWN;
2386 phydev->pause = 0;
2387 phydev->asym_pause = 0;
2388
2389 err = genphy_read_master_slave(phydev);
2390 if (err < 0)
2391 return err;
2392
2393 err = genphy_read_lpa(phydev);
2394 if (err < 0)
2395 return err;
2396
2397 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2398 phy_resolve_aneg_linkmode(phydev);
2399 } else if (phydev->autoneg == AUTONEG_DISABLE) {
2400 err = genphy_read_status_fixed(phydev);
2401 if (err < 0)
2402 return err;
2403 }
2404
2405 return 0;
2406}
2407EXPORT_SYMBOL(genphy_read_status);
2408
2409/**
2410 * genphy_c37_read_status - check the link status and update current link state
2411 * @phydev: target phy_device struct
2412 *
2413 * Description: Check the link, then figure out the current state
2414 * by comparing what we advertise with what the link partner
2415 * advertises. This function is for Clause 37 1000Base-X mode.
2416 */
2417int genphy_c37_read_status(struct phy_device *phydev)
2418{
2419 int lpa, err, old_link = phydev->link;
2420
2421 /* Update the link, but return if there was an error */
2422 err = genphy_update_link(phydev);
2423 if (err)
2424 return err;
2425
2426 /* why bother the PHY if nothing can have changed */
2427 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
2428 return 0;
2429
2430 phydev->duplex = DUPLEX_UNKNOWN;
2431 phydev->pause = 0;
2432 phydev->asym_pause = 0;
2433
2434 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2435 lpa = phy_read(phydev, MII_LPA);
2436 if (lpa < 0)
2437 return lpa;
2438
2439 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2440 phydev->lp_advertising, lpa & LPA_LPACK);
2441 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2442 phydev->lp_advertising, lpa & LPA_1000XFULL);
2443 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2444 phydev->lp_advertising, lpa & LPA_1000XPAUSE);
2445 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2446 phydev->lp_advertising,
2447 lpa & LPA_1000XPAUSE_ASYM);
2448
2449 phy_resolve_aneg_linkmode(phydev);
2450 } else if (phydev->autoneg == AUTONEG_DISABLE) {
2451 int bmcr = phy_read(phydev, MII_BMCR);
2452
2453 if (bmcr < 0)
2454 return bmcr;
2455
2456 if (bmcr & BMCR_FULLDPLX)
2457 phydev->duplex = DUPLEX_FULL;
2458 else
2459 phydev->duplex = DUPLEX_HALF;
2460 }
2461
2462 return 0;
2463}
2464EXPORT_SYMBOL(genphy_c37_read_status);
2465
2466/**
2467 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
2468 * @phydev: target phy_device struct
2469 *
2470 * Description: Perform a software PHY reset using the standard
2471 * BMCR_RESET bit and poll for the reset bit to be cleared.
2472 *
2473 * Returns: 0 on success, < 0 on failure
2474 */
2475int genphy_soft_reset(struct phy_device *phydev)
2476{
2477 u16 res = BMCR_RESET;
2478 int ret;
2479
2480 if (phydev->autoneg == AUTONEG_ENABLE)
2481 res |= BMCR_ANRESTART;
2482
2483 ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res);
2484 if (ret < 0)
2485 return ret;
2486
2487 /* Clause 22 states that setting bit BMCR_RESET sets control registers
2488 * to their default value. Therefore the POWER DOWN bit is supposed to
2489 * be cleared after soft reset.
2490 */
2491 phydev->suspended = 0;
2492
2493 ret = phy_poll_reset(phydev);
2494 if (ret)
2495 return ret;
2496
2497 /* BMCR may be reset to defaults */
2498 if (phydev->autoneg == AUTONEG_DISABLE)
2499 ret = genphy_setup_forced(phydev);
2500
2501 return ret;
2502}
2503EXPORT_SYMBOL(genphy_soft_reset);
2504
2505/**
2506 * genphy_read_abilities - read PHY abilities from Clause 22 registers
2507 * @phydev: target phy_device struct
2508 *
2509 * Description: Reads the PHY's abilities and populates
2510 * phydev->supported accordingly.
2511 *
2512 * Returns: 0 on success, < 0 on failure
2513 */
2514int genphy_read_abilities(struct phy_device *phydev)
2515{
2516 int val;
2517
2518 linkmode_set_bit_array(phy_basic_ports_array,
2519 ARRAY_SIZE(phy_basic_ports_array),
2520 phydev->supported);
2521
2522 val = phy_read(phydev, MII_BMSR);
2523 if (val < 0)
2524 return val;
2525
2526 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported,
2527 val & BMSR_ANEGCAPABLE);
2528
2529 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported,
2530 val & BMSR_100FULL);
2531 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported,
2532 val & BMSR_100HALF);
2533 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported,
2534 val & BMSR_10FULL);
2535 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported,
2536 val & BMSR_10HALF);
2537
2538 if (val & BMSR_ESTATEN) {
2539 val = phy_read(phydev, MII_ESTATUS);
2540 if (val < 0)
2541 return val;
2542
2543 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2544 phydev->supported, val & ESTATUS_1000_TFULL);
2545 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
2546 phydev->supported, val & ESTATUS_1000_THALF);
2547 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2548 phydev->supported, val & ESTATUS_1000_XFULL);
2549 }
2550
2551 return 0;
2552}
2553EXPORT_SYMBOL(genphy_read_abilities);
2554
2555/* This is used for the phy device which doesn't support the MMD extended
2556 * register access, but it does have side effect when we are trying to access
2557 * the MMD register via indirect method.
2558 */
2559int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
2560{
2561 return -EOPNOTSUPP;
2562}
2563EXPORT_SYMBOL(genphy_read_mmd_unsupported);
2564
2565int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
2566 u16 regnum, u16 val)
2567{
2568 return -EOPNOTSUPP;
2569}
2570EXPORT_SYMBOL(genphy_write_mmd_unsupported);
2571
2572int genphy_suspend(struct phy_device *phydev)
2573{
2574 return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
2575}
2576EXPORT_SYMBOL(genphy_suspend);
2577
2578int genphy_resume(struct phy_device *phydev)
2579{
2580 return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
2581}
2582EXPORT_SYMBOL(genphy_resume);
2583
2584int genphy_loopback(struct phy_device *phydev, bool enable)
2585{
2586 return phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK,
2587 enable ? BMCR_LOOPBACK : 0);
2588}
2589EXPORT_SYMBOL(genphy_loopback);
2590
2591/**
2592 * phy_remove_link_mode - Remove a supported link mode
2593 * @phydev: phy_device structure to remove link mode from
2594 * @link_mode: Link mode to be removed
2595 *
2596 * Description: Some MACs don't support all link modes which the PHY
2597 * does. e.g. a 1G MAC often does not support 1000Half. Add a helper
2598 * to remove a link mode.
2599 */
2600void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode)
2601{
2602 linkmode_clear_bit(link_mode, phydev->supported);
2603 phy_advertise_supported(phydev);
2604}
2605EXPORT_SYMBOL(phy_remove_link_mode);
2606
2607static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src)
2608{
2609 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst,
2610 linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src));
2611 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst,
2612 linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src));
2613}
2614
2615/**
2616 * phy_advertise_supported - Advertise all supported modes
2617 * @phydev: target phy_device struct
2618 *
2619 * Description: Called to advertise all supported modes, doesn't touch
2620 * pause mode advertising.
2621 */
2622void phy_advertise_supported(struct phy_device *phydev)
2623{
2624 __ETHTOOL_DECLARE_LINK_MODE_MASK(new);
2625
2626 linkmode_copy(new, phydev->supported);
2627 phy_copy_pause_bits(new, phydev->advertising);
2628 linkmode_copy(phydev->advertising, new);
2629}
2630EXPORT_SYMBOL(phy_advertise_supported);
2631
2632/**
2633 * phy_support_sym_pause - Enable support of symmetrical pause
2634 * @phydev: target phy_device struct
2635 *
2636 * Description: Called by the MAC to indicate is supports symmetrical
2637 * Pause, but not asym pause.
2638 */
2639void phy_support_sym_pause(struct phy_device *phydev)
2640{
2641 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
2642 phy_copy_pause_bits(phydev->advertising, phydev->supported);
2643}
2644EXPORT_SYMBOL(phy_support_sym_pause);
2645
2646/**
2647 * phy_support_asym_pause - Enable support of asym pause
2648 * @phydev: target phy_device struct
2649 *
2650 * Description: Called by the MAC to indicate is supports Asym Pause.
2651 */
2652void phy_support_asym_pause(struct phy_device *phydev)
2653{
2654 phy_copy_pause_bits(phydev->advertising, phydev->supported);
2655}
2656EXPORT_SYMBOL(phy_support_asym_pause);
2657
2658/**
2659 * phy_set_sym_pause - Configure symmetric Pause
2660 * @phydev: target phy_device struct
2661 * @rx: Receiver Pause is supported
2662 * @tx: Transmit Pause is supported
2663 * @autoneg: Auto neg should be used
2664 *
2665 * Description: Configure advertised Pause support depending on if
2666 * receiver pause and pause auto neg is supported. Generally called
2667 * from the set_pauseparam .ndo.
2668 */
2669void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
2670 bool autoneg)
2671{
2672 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
2673
2674 if (rx && tx && autoneg)
2675 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2676 phydev->supported);
2677
2678 linkmode_copy(phydev->advertising, phydev->supported);
2679}
2680EXPORT_SYMBOL(phy_set_sym_pause);
2681
2682/**
2683 * phy_set_asym_pause - Configure Pause and Asym Pause
2684 * @phydev: target phy_device struct
2685 * @rx: Receiver Pause is supported
2686 * @tx: Transmit Pause is supported
2687 *
2688 * Description: Configure advertised Pause support depending on if
2689 * transmit and receiver pause is supported. If there has been a
2690 * change in adverting, trigger a new autoneg. Generally called from
2691 * the set_pauseparam .ndo.
2692 */
2693void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx)
2694{
2695 __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv);
2696
2697 linkmode_copy(oldadv, phydev->advertising);
2698 linkmode_set_pause(phydev->advertising, tx, rx);
2699
2700 if (!linkmode_equal(oldadv, phydev->advertising) &&
2701 phydev->autoneg)
2702 phy_start_aneg(phydev);
2703}
2704EXPORT_SYMBOL(phy_set_asym_pause);
2705
2706/**
2707 * phy_validate_pause - Test if the PHY/MAC support the pause configuration
2708 * @phydev: phy_device struct
2709 * @pp: requested pause configuration
2710 *
2711 * Description: Test if the PHY/MAC combination supports the Pause
2712 * configuration the user is requesting. Returns True if it is
2713 * supported, false otherwise.
2714 */
2715bool phy_validate_pause(struct phy_device *phydev,
2716 struct ethtool_pauseparam *pp)
2717{
2718 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2719 phydev->supported) && pp->rx_pause)
2720 return false;
2721
2722 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2723 phydev->supported) &&
2724 pp->rx_pause != pp->tx_pause)
2725 return false;
2726
2727 return true;
2728}
2729EXPORT_SYMBOL(phy_validate_pause);
2730
2731/**
2732 * phy_get_pause - resolve negotiated pause modes
2733 * @phydev: phy_device struct
2734 * @tx_pause: pointer to bool to indicate whether transmit pause should be
2735 * enabled.
2736 * @rx_pause: pointer to bool to indicate whether receive pause should be
2737 * enabled.
2738 *
2739 * Resolve and return the flow control modes according to the negotiation
2740 * result. This includes checking that we are operating in full duplex mode.
2741 * See linkmode_resolve_pause() for further details.
2742 */
2743void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause)
2744{
2745 if (phydev->duplex != DUPLEX_FULL) {
2746 *tx_pause = false;
2747 *rx_pause = false;
2748 return;
2749 }
2750
2751 return linkmode_resolve_pause(phydev->advertising,
2752 phydev->lp_advertising,
2753 tx_pause, rx_pause);
2754}
2755EXPORT_SYMBOL(phy_get_pause);
2756
2757#if IS_ENABLED(CONFIG_OF_MDIO)
2758static int phy_get_int_delay_property(struct device *dev, const char *name)
2759{
2760 s32 int_delay;
2761 int ret;
2762
2763 ret = device_property_read_u32(dev, name, &int_delay);
2764 if (ret)
2765 return ret;
2766
2767 return int_delay;
2768}
2769#else
2770static int phy_get_int_delay_property(struct device *dev, const char *name)
2771{
2772 return -EINVAL;
2773}
2774#endif
2775
2776/**
2777 * phy_get_delay_index - returns the index of the internal delay
2778 * @phydev: phy_device struct
2779 * @dev: pointer to the devices device struct
2780 * @delay_values: array of delays the PHY supports
2781 * @size: the size of the delay array
2782 * @is_rx: boolean to indicate to get the rx internal delay
2783 *
2784 * Returns the index within the array of internal delay passed in.
2785 * If the device property is not present then the interface type is checked
2786 * if the interface defines use of internal delay then a 1 is returned otherwise
2787 * a 0 is returned.
2788 * The array must be in ascending order. If PHY does not have an ascending order
2789 * array then size = 0 and the value of the delay property is returned.
2790 * Return -EINVAL if the delay is invalid or cannot be found.
2791 */
2792s32 phy_get_internal_delay(struct phy_device *phydev, struct device *dev,
2793 const int *delay_values, int size, bool is_rx)
2794{
2795 s32 delay;
2796 int i;
2797
2798 if (is_rx) {
2799 delay = phy_get_int_delay_property(dev, "rx-internal-delay-ps");
2800 if (delay < 0 && size == 0) {
2801 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
2802 phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
2803 return 1;
2804 else
2805 return 0;
2806 }
2807
2808 } else {
2809 delay = phy_get_int_delay_property(dev, "tx-internal-delay-ps");
2810 if (delay < 0 && size == 0) {
2811 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
2812 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
2813 return 1;
2814 else
2815 return 0;
2816 }
2817 }
2818
2819 if (delay < 0)
2820 return delay;
2821
2822 if (delay && size == 0)
2823 return delay;
2824
2825 if (delay < delay_values[0] || delay > delay_values[size - 1]) {
2826 phydev_err(phydev, "Delay %d is out of range\n", delay);
2827 return -EINVAL;
2828 }
2829
2830 if (delay == delay_values[0])
2831 return 0;
2832
2833 for (i = 1; i < size; i++) {
2834 if (delay == delay_values[i])
2835 return i;
2836
2837 /* Find an approximate index by looking up the table */
2838 if (delay > delay_values[i - 1] &&
2839 delay < delay_values[i]) {
2840 if (delay - delay_values[i - 1] <
2841 delay_values[i] - delay)
2842 return i - 1;
2843 else
2844 return i;
2845 }
2846 }
2847
2848 phydev_err(phydev, "error finding internal delay index for %d\n",
2849 delay);
2850
2851 return -EINVAL;
2852}
2853EXPORT_SYMBOL(phy_get_internal_delay);
2854
2855static bool phy_drv_supports_irq(struct phy_driver *phydrv)
2856{
2857 return phydrv->config_intr && phydrv->ack_interrupt;
2858}
2859
2860/**
2861 * phy_probe - probe and init a PHY device
2862 * @dev: device to probe and init
2863 *
2864 * Description: Take care of setting up the phy_device structure,
2865 * set the state to READY (the driver's init function should
2866 * set it to STARTING if needed).
2867 */
2868static int phy_probe(struct device *dev)
2869{
2870 struct phy_device *phydev = to_phy_device(dev);
2871 struct device_driver *drv = phydev->mdio.dev.driver;
2872 struct phy_driver *phydrv = to_phy_driver(drv);
2873 int err = 0;
2874
2875 phydev->drv = phydrv;
2876
2877 /* Disable the interrupt if the PHY doesn't support it
2878 * but the interrupt is still a valid one
2879 */
2880 if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev))
2881 phydev->irq = PHY_POLL;
2882
2883 if (phydrv->flags & PHY_IS_INTERNAL)
2884 phydev->is_internal = true;
2885
2886 mutex_lock(&phydev->lock);
2887
2888 /* Deassert the reset signal */
2889 phy_device_reset(phydev, 0);
2890
2891 if (phydev->drv->probe) {
2892 err = phydev->drv->probe(phydev);
2893 if (err)
2894 goto out;
2895 }
2896
2897 /* Start out supporting everything. Eventually,
2898 * a controller will attach, and may modify one
2899 * or both of these values
2900 */
2901 if (phydrv->features) {
2902 linkmode_copy(phydev->supported, phydrv->features);
2903 } else if (phydrv->get_features) {
2904 err = phydrv->get_features(phydev);
2905 } else if (phydev->is_c45) {
2906 err = genphy_c45_pma_read_abilities(phydev);
2907 } else {
2908 err = genphy_read_abilities(phydev);
2909 }
2910
2911 if (err)
2912 goto out;
2913
2914 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2915 phydev->supported))
2916 phydev->autoneg = 0;
2917
2918 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
2919 phydev->supported))
2920 phydev->is_gigabit_capable = 1;
2921 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2922 phydev->supported))
2923 phydev->is_gigabit_capable = 1;
2924
2925 of_set_phy_supported(phydev);
2926 phy_advertise_supported(phydev);
2927
2928 /* Get the EEE modes we want to prohibit. We will ask
2929 * the PHY stop advertising these mode later on
2930 */
2931 of_set_phy_eee_broken(phydev);
2932
2933 /* The Pause Frame bits indicate that the PHY can support passing
2934 * pause frames. During autonegotiation, the PHYs will determine if
2935 * they should allow pause frames to pass. The MAC driver should then
2936 * use that result to determine whether to enable flow control via
2937 * pause frames.
2938 *
2939 * Normally, PHY drivers should not set the Pause bits, and instead
2940 * allow phylib to do that. However, there may be some situations
2941 * (e.g. hardware erratum) where the driver wants to set only one
2942 * of these bits.
2943 */
2944 if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) &&
2945 !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) {
2946 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2947 phydev->supported);
2948 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2949 phydev->supported);
2950 }
2951
2952 /* Set the state to READY by default */
2953 phydev->state = PHY_READY;
2954
2955out:
2956 /* Assert the reset signal */
2957 if (err)
2958 phy_device_reset(phydev, 1);
2959
2960 mutex_unlock(&phydev->lock);
2961
2962 return err;
2963}
2964
2965static int phy_remove(struct device *dev)
2966{
2967 struct phy_device *phydev = to_phy_device(dev);
2968
2969 cancel_delayed_work_sync(&phydev->state_queue);
2970
2971 mutex_lock(&phydev->lock);
2972 phydev->state = PHY_DOWN;
2973 mutex_unlock(&phydev->lock);
2974
2975 sfp_bus_del_upstream(phydev->sfp_bus);
2976 phydev->sfp_bus = NULL;
2977
2978 if (phydev->drv && phydev->drv->remove)
2979 phydev->drv->remove(phydev);
2980
2981 /* Assert the reset signal */
2982 phy_device_reset(phydev, 1);
2983
2984 phydev->drv = NULL;
2985
2986 return 0;
2987}
2988
2989/**
2990 * phy_driver_register - register a phy_driver with the PHY layer
2991 * @new_driver: new phy_driver to register
2992 * @owner: module owning this PHY
2993 */
2994int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
2995{
2996 int retval;
2997
2998 /* Either the features are hard coded, or dynamically
2999 * determined. It cannot be both.
3000 */
3001 if (WARN_ON(new_driver->features && new_driver->get_features)) {
3002 pr_err("%s: features and get_features must not both be set\n",
3003 new_driver->name);
3004 return -EINVAL;
3005 }
3006
3007 new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
3008 new_driver->mdiodrv.driver.name = new_driver->name;
3009 new_driver->mdiodrv.driver.bus = &mdio_bus_type;
3010 new_driver->mdiodrv.driver.probe = phy_probe;
3011 new_driver->mdiodrv.driver.remove = phy_remove;
3012 new_driver->mdiodrv.driver.owner = owner;
3013 new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
3014
3015 retval = driver_register(&new_driver->mdiodrv.driver);
3016 if (retval) {
3017 pr_err("%s: Error %d in registering driver\n",
3018 new_driver->name, retval);
3019
3020 return retval;
3021 }
3022
3023 pr_debug("%s: Registered new driver\n", new_driver->name);
3024
3025 return 0;
3026}
3027EXPORT_SYMBOL(phy_driver_register);
3028
3029int phy_drivers_register(struct phy_driver *new_driver, int n,
3030 struct module *owner)
3031{
3032 int i, ret = 0;
3033
3034 for (i = 0; i < n; i++) {
3035 ret = phy_driver_register(new_driver + i, owner);
3036 if (ret) {
3037 while (i-- > 0)
3038 phy_driver_unregister(new_driver + i);
3039 break;
3040 }
3041 }
3042 return ret;
3043}
3044EXPORT_SYMBOL(phy_drivers_register);
3045
3046void phy_driver_unregister(struct phy_driver *drv)
3047{
3048 driver_unregister(&drv->mdiodrv.driver);
3049}
3050EXPORT_SYMBOL(phy_driver_unregister);
3051
3052void phy_drivers_unregister(struct phy_driver *drv, int n)
3053{
3054 int i;
3055
3056 for (i = 0; i < n; i++)
3057 phy_driver_unregister(drv + i);
3058}
3059EXPORT_SYMBOL(phy_drivers_unregister);
3060
3061static struct phy_driver genphy_driver = {
3062 .phy_id = 0xffffffff,
3063 .phy_id_mask = 0xffffffff,
3064 .name = "Generic PHY",
3065 .get_features = genphy_read_abilities,
3066 .suspend = genphy_suspend,
3067 .resume = genphy_resume,
3068 .set_loopback = genphy_loopback,
3069};
3070
3071static const struct ethtool_phy_ops phy_ethtool_phy_ops = {
3072 .get_sset_count = phy_ethtool_get_sset_count,
3073 .get_strings = phy_ethtool_get_strings,
3074 .get_stats = phy_ethtool_get_stats,
3075 .start_cable_test = phy_start_cable_test,
3076 .start_cable_test_tdr = phy_start_cable_test_tdr,
3077};
3078
3079static int __init phy_init(void)
3080{
3081 int rc;
3082
3083 rc = mdio_bus_init();
3084 if (rc)
3085 return rc;
3086
3087 ethtool_set_ethtool_phy_ops(&phy_ethtool_phy_ops);
3088 features_init();
3089
3090 rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE);
3091 if (rc)
3092 goto err_c45;
3093
3094 rc = phy_driver_register(&genphy_driver, THIS_MODULE);
3095 if (rc) {
3096 phy_driver_unregister(&genphy_c45_driver);
3097err_c45:
3098 mdio_bus_exit();
3099 }
3100
3101 return rc;
3102}
3103
3104static void __exit phy_exit(void)
3105{
3106 phy_driver_unregister(&genphy_c45_driver);
3107 phy_driver_unregister(&genphy_driver);
3108 mdio_bus_exit();
3109 ethtool_set_ethtool_phy_ops(NULL);
3110}
3111
3112subsys_initcall(phy_init);
3113module_exit(phy_exit);