Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0+
2/* Framework for configuring and reading PHY devices
3 * Based on code in sungem_phy.c and gianfar_phy.c
4 *
5 * Author: Andy Fleming
6 *
7 * Copyright (c) 2004 Freescale Semiconductor, Inc.
8 * Copyright (c) 2006, 2007 Maciej W. Rozycki
9 */
10
11#include <linux/kernel.h>
12#include <linux/string.h>
13#include <linux/errno.h>
14#include <linux/unistd.h>
15#include <linux/interrupt.h>
16#include <linux/delay.h>
17#include <linux/netdevice.h>
18#include <linux/netlink.h>
19#include <linux/etherdevice.h>
20#include <linux/skbuff.h>
21#include <linux/mm.h>
22#include <linux/module.h>
23#include <linux/mii.h>
24#include <linux/ethtool.h>
25#include <linux/ethtool_netlink.h>
26#include <linux/phy.h>
27#include <linux/phy_led_triggers.h>
28#include <linux/sfp.h>
29#include <linux/workqueue.h>
30#include <linux/mdio.h>
31#include <linux/io.h>
32#include <linux/uaccess.h>
33#include <linux/atomic.h>
34#include <linux/suspend.h>
35#include <net/netlink.h>
36#include <net/genetlink.h>
37#include <net/sock.h>
38
39#include "phylib-internal.h"
40#include "phy-caps.h"
41
42#define PHY_STATE_TIME HZ
43
44#define PHY_STATE_STR(_state) \
45 case PHY_##_state: \
46 return __stringify(_state); \
47
48static const char *phy_state_to_str(enum phy_state st)
49{
50 switch (st) {
51 PHY_STATE_STR(DOWN)
52 PHY_STATE_STR(READY)
53 PHY_STATE_STR(UP)
54 PHY_STATE_STR(RUNNING)
55 PHY_STATE_STR(NOLINK)
56 PHY_STATE_STR(CABLETEST)
57 PHY_STATE_STR(HALTED)
58 PHY_STATE_STR(ERROR)
59 }
60
61 return NULL;
62}
63
64static void phy_process_state_change(struct phy_device *phydev,
65 enum phy_state old_state)
66{
67 if (old_state != phydev->state) {
68 phydev_dbg(phydev, "PHY state change %s -> %s\n",
69 phy_state_to_str(old_state),
70 phy_state_to_str(phydev->state));
71 if (phydev->drv && phydev->drv->link_change_notify)
72 phydev->drv->link_change_notify(phydev);
73 }
74}
75
76static void phy_link_up(struct phy_device *phydev)
77{
78 phydev->phy_link_change(phydev, true);
79 phy_led_trigger_change_speed(phydev);
80}
81
82static void phy_link_down(struct phy_device *phydev)
83{
84 phydev->phy_link_change(phydev, false);
85 phy_led_trigger_change_speed(phydev);
86 WRITE_ONCE(phydev->link_down_events, phydev->link_down_events + 1);
87}
88
89static const char *phy_pause_str(struct phy_device *phydev)
90{
91 bool local_pause, local_asym_pause;
92
93 if (phydev->autoneg == AUTONEG_DISABLE)
94 goto no_pause;
95
96 local_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
97 phydev->advertising);
98 local_asym_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
99 phydev->advertising);
100
101 if (local_pause && phydev->pause)
102 return "rx/tx";
103
104 if (local_asym_pause && phydev->asym_pause) {
105 if (local_pause)
106 return "rx";
107 if (phydev->pause)
108 return "tx";
109 }
110
111no_pause:
112 return "off";
113}
114
115/**
116 * phy_print_status - Convenience function to print out the current phy status
117 * @phydev: the phy_device struct
118 */
119void phy_print_status(struct phy_device *phydev)
120{
121 if (phydev->link) {
122 netdev_info(phydev->attached_dev,
123 "Link is Up - %s/%s %s- flow control %s\n",
124 phy_speed_to_str(phydev->speed),
125 phy_duplex_to_str(phydev->duplex),
126 phydev->downshifted_rate ? "(downshifted) " : "",
127 phy_pause_str(phydev));
128 } else {
129 netdev_info(phydev->attached_dev, "Link is Down\n");
130 }
131}
132EXPORT_SYMBOL(phy_print_status);
133
134/**
135 * phy_get_rate_matching - determine if rate matching is supported
136 * @phydev: The phy device to return rate matching for
137 * @iface: The interface mode to use
138 *
139 * This determines the type of rate matching (if any) that @phy supports
140 * using @iface. @iface may be %PHY_INTERFACE_MODE_NA to determine if any
141 * interface supports rate matching.
142 *
143 * Return: The type of rate matching @phy supports for @iface, or
144 * %RATE_MATCH_NONE.
145 */
146int phy_get_rate_matching(struct phy_device *phydev,
147 phy_interface_t iface)
148{
149 int ret = RATE_MATCH_NONE;
150
151 if (phydev->drv->get_rate_matching) {
152 mutex_lock(&phydev->lock);
153 ret = phydev->drv->get_rate_matching(phydev, iface);
154 mutex_unlock(&phydev->lock);
155 }
156
157 return ret;
158}
159EXPORT_SYMBOL_GPL(phy_get_rate_matching);
160
161/**
162 * phy_config_interrupt - configure the PHY device for the requested interrupts
163 * @phydev: the phy_device struct
164 * @interrupts: interrupt flags to configure for this @phydev
165 *
166 * Returns 0 on success or < 0 on error.
167 */
168static int phy_config_interrupt(struct phy_device *phydev, bool interrupts)
169{
170 phydev->interrupts = interrupts ? 1 : 0;
171 if (phydev->drv->config_intr)
172 return phydev->drv->config_intr(phydev);
173
174 return 0;
175}
176
177/**
178 * phy_restart_aneg - restart auto-negotiation
179 * @phydev: target phy_device struct
180 *
181 * Restart the autonegotiation on @phydev. Returns >= 0 on success or
182 * negative errno on error.
183 */
184int phy_restart_aneg(struct phy_device *phydev)
185{
186 int ret;
187
188 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
189 ret = genphy_c45_restart_aneg(phydev);
190 else
191 ret = genphy_restart_aneg(phydev);
192
193 return ret;
194}
195EXPORT_SYMBOL_GPL(phy_restart_aneg);
196
197/**
198 * phy_aneg_done - return auto-negotiation status
199 * @phydev: target phy_device struct
200 *
201 * Description: Return the auto-negotiation status from this @phydev
202 * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
203 * is still pending.
204 */
205int phy_aneg_done(struct phy_device *phydev)
206{
207 if (phydev->drv && phydev->drv->aneg_done)
208 return phydev->drv->aneg_done(phydev);
209 else if (phydev->is_c45)
210 return genphy_c45_aneg_done(phydev);
211 else
212 return genphy_aneg_done(phydev);
213}
214EXPORT_SYMBOL(phy_aneg_done);
215
216/**
217 * phy_supported_speeds - return all speeds currently supported by a phy device
218 * @phy: The phy device to return supported speeds of.
219 * @speeds: buffer to store supported speeds in.
220 * @size: size of speeds buffer.
221 *
222 * Description: Returns the number of supported speeds, and fills the speeds
223 * buffer with the supported speeds. If speeds buffer is too small to contain
224 * all currently supported speeds, will return as many speeds as can fit.
225 */
226unsigned int phy_supported_speeds(struct phy_device *phy,
227 unsigned int *speeds,
228 unsigned int size)
229{
230 return phy_caps_speeds(speeds, size, phy->supported);
231}
232
233/**
234 * phy_check_valid - check if there is a valid PHY setting which matches
235 * speed, duplex, and feature mask
236 * @speed: speed to match
237 * @duplex: duplex to match
238 * @features: A mask of the valid settings
239 *
240 * Description: Returns true if there is a valid setting, false otherwise.
241 */
242bool phy_check_valid(int speed, int duplex, unsigned long *features)
243{
244 return phy_caps_valid(speed, duplex, features);
245}
246EXPORT_SYMBOL(phy_check_valid);
247
248/**
249 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
250 * @phydev: the target phy_device struct
251 *
252 * Description: Make sure the PHY is set to supported speeds and
253 * duplexes. Drop down by one in this order: 1000/FULL,
254 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
255 */
256static void phy_sanitize_settings(struct phy_device *phydev)
257{
258 const struct link_capabilities *c;
259
260 c = phy_caps_lookup(phydev->speed, phydev->duplex, phydev->supported,
261 false);
262
263 if (c) {
264 phydev->speed = c->speed;
265 phydev->duplex = c->duplex;
266 } else {
267 /* We failed to find anything (no supported speeds?) */
268 phydev->speed = SPEED_UNKNOWN;
269 phydev->duplex = DUPLEX_UNKNOWN;
270 }
271}
272
273void phy_ethtool_ksettings_get(struct phy_device *phydev,
274 struct ethtool_link_ksettings *cmd)
275{
276 mutex_lock(&phydev->lock);
277 linkmode_copy(cmd->link_modes.supported, phydev->supported);
278 linkmode_copy(cmd->link_modes.advertising, phydev->advertising);
279 linkmode_copy(cmd->link_modes.lp_advertising, phydev->lp_advertising);
280
281 cmd->base.speed = phydev->speed;
282 cmd->base.duplex = phydev->duplex;
283 cmd->base.master_slave_cfg = phydev->master_slave_get;
284 cmd->base.master_slave_state = phydev->master_slave_state;
285 cmd->base.rate_matching = phydev->rate_matching;
286 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
287 cmd->base.port = PORT_BNC;
288 else
289 cmd->base.port = phydev->port;
290 cmd->base.transceiver = phydev->is_internal ?
291 XCVR_INTERNAL : XCVR_EXTERNAL;
292 cmd->base.phy_address = phydev->mdio.addr;
293 cmd->base.autoneg = phydev->autoneg;
294 cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
295 cmd->base.eth_tp_mdix = phydev->mdix;
296 mutex_unlock(&phydev->lock);
297}
298EXPORT_SYMBOL(phy_ethtool_ksettings_get);
299
300/**
301 * phy_mii_ioctl - generic PHY MII ioctl interface
302 * @phydev: the phy_device struct
303 * @ifr: &struct ifreq for socket ioctl's
304 * @cmd: ioctl cmd to execute
305 *
306 * Note that this function is currently incompatible with the
307 * PHYCONTROL layer. It changes registers without regard to
308 * current state. Use at own risk.
309 */
310int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
311{
312 struct mii_ioctl_data *mii_data = if_mii(ifr);
313 struct kernel_hwtstamp_config kernel_cfg;
314 struct netlink_ext_ack extack = {};
315 u16 val = mii_data->val_in;
316 bool change_autoneg = false;
317 struct hwtstamp_config cfg;
318 int prtad, devad;
319 int ret;
320
321 switch (cmd) {
322 case SIOCGMIIPHY:
323 mii_data->phy_id = phydev->mdio.addr;
324 fallthrough;
325
326 case SIOCGMIIREG:
327 if (mdio_phy_id_is_c45(mii_data->phy_id)) {
328 prtad = mdio_phy_id_prtad(mii_data->phy_id);
329 devad = mdio_phy_id_devad(mii_data->phy_id);
330 ret = mdiobus_c45_read(phydev->mdio.bus, prtad, devad,
331 mii_data->reg_num);
332
333 } else {
334 ret = mdiobus_read(phydev->mdio.bus, mii_data->phy_id,
335 mii_data->reg_num);
336 }
337
338 if (ret < 0)
339 return ret;
340
341 mii_data->val_out = ret;
342
343 return 0;
344
345 case SIOCSMIIREG:
346 if (mdio_phy_id_is_c45(mii_data->phy_id)) {
347 prtad = mdio_phy_id_prtad(mii_data->phy_id);
348 devad = mdio_phy_id_devad(mii_data->phy_id);
349 } else {
350 prtad = mii_data->phy_id;
351 devad = mii_data->reg_num;
352 }
353 if (prtad == phydev->mdio.addr) {
354 switch (devad) {
355 case MII_BMCR:
356 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
357 if (phydev->autoneg == AUTONEG_ENABLE)
358 change_autoneg = true;
359 phydev->autoneg = AUTONEG_DISABLE;
360 if (val & BMCR_FULLDPLX)
361 phydev->duplex = DUPLEX_FULL;
362 else
363 phydev->duplex = DUPLEX_HALF;
364 if (val & BMCR_SPEED1000)
365 phydev->speed = SPEED_1000;
366 else if (val & BMCR_SPEED100)
367 phydev->speed = SPEED_100;
368 else phydev->speed = SPEED_10;
369 } else {
370 if (phydev->autoneg == AUTONEG_DISABLE)
371 change_autoneg = true;
372 phydev->autoneg = AUTONEG_ENABLE;
373 }
374 break;
375 case MII_ADVERTISE:
376 mii_adv_mod_linkmode_adv_t(phydev->advertising,
377 val);
378 change_autoneg = true;
379 break;
380 case MII_CTRL1000:
381 mii_ctrl1000_mod_linkmode_adv_t(phydev->advertising,
382 val);
383 change_autoneg = true;
384 break;
385 default:
386 /* do nothing */
387 break;
388 }
389 }
390
391 if (mdio_phy_id_is_c45(mii_data->phy_id))
392 mdiobus_c45_write(phydev->mdio.bus, prtad, devad,
393 mii_data->reg_num, val);
394 else
395 mdiobus_write(phydev->mdio.bus, prtad, devad, val);
396
397 if (prtad == phydev->mdio.addr &&
398 devad == MII_BMCR &&
399 val & BMCR_RESET)
400 return phy_init_hw(phydev);
401
402 if (change_autoneg)
403 return phy_start_aneg(phydev);
404
405 return 0;
406
407 case SIOCSHWTSTAMP:
408 if (phydev->mii_ts && phydev->mii_ts->hwtstamp_set) {
409 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
410 return -EFAULT;
411
412 hwtstamp_config_to_kernel(&kernel_cfg, &cfg);
413 ret = phydev->mii_ts->hwtstamp_set(phydev->mii_ts,
414 &kernel_cfg,
415 &extack);
416 if (ret)
417 return ret;
418
419 hwtstamp_config_from_kernel(&cfg, &kernel_cfg);
420 if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)))
421 return -EFAULT;
422
423 return 0;
424 }
425 fallthrough;
426
427 default:
428 return -EOPNOTSUPP;
429 }
430}
431EXPORT_SYMBOL(phy_mii_ioctl);
432
433/**
434 * phy_do_ioctl - generic ndo_eth_ioctl implementation
435 * @dev: the net_device struct
436 * @ifr: &struct ifreq for socket ioctl's
437 * @cmd: ioctl cmd to execute
438 */
439int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
440{
441 if (!dev->phydev)
442 return -ENODEV;
443
444 return phy_mii_ioctl(dev->phydev, ifr, cmd);
445}
446EXPORT_SYMBOL(phy_do_ioctl);
447
448/**
449 * phy_do_ioctl_running - generic ndo_eth_ioctl implementation but test first
450 *
451 * @dev: the net_device struct
452 * @ifr: &struct ifreq for socket ioctl's
453 * @cmd: ioctl cmd to execute
454 *
455 * Same as phy_do_ioctl, but ensures that net_device is running before
456 * handling the ioctl.
457 */
458int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd)
459{
460 if (!netif_running(dev))
461 return -ENODEV;
462
463 return phy_do_ioctl(dev, ifr, cmd);
464}
465EXPORT_SYMBOL(phy_do_ioctl_running);
466
467/**
468 * __phy_hwtstamp_get - Get hardware timestamping configuration from PHY
469 *
470 * @phydev: the PHY device structure
471 * @config: structure holding the timestamping configuration
472 *
473 * Query the PHY device for its current hardware timestamping configuration.
474 */
475int __phy_hwtstamp_get(struct phy_device *phydev,
476 struct kernel_hwtstamp_config *config)
477{
478 if (!phydev)
479 return -ENODEV;
480
481 if (phydev->mii_ts && phydev->mii_ts->hwtstamp_get)
482 return phydev->mii_ts->hwtstamp_get(phydev->mii_ts, config);
483
484 return -EOPNOTSUPP;
485}
486
487/**
488 * __phy_hwtstamp_set - Modify PHY hardware timestamping configuration
489 *
490 * @phydev: the PHY device structure
491 * @config: structure holding the timestamping configuration
492 * @extack: netlink extended ack structure, for error reporting
493 */
494int __phy_hwtstamp_set(struct phy_device *phydev,
495 struct kernel_hwtstamp_config *config,
496 struct netlink_ext_ack *extack)
497{
498 if (!phydev)
499 return -ENODEV;
500
501 if (phydev->mii_ts && phydev->mii_ts->hwtstamp_set)
502 return phydev->mii_ts->hwtstamp_set(phydev->mii_ts, config,
503 extack);
504
505 return -EOPNOTSUPP;
506}
507
508/**
509 * phy_queue_state_machine - Trigger the state machine to run soon
510 *
511 * @phydev: the phy_device struct
512 * @jiffies: Run the state machine after these jiffies
513 */
514static void phy_queue_state_machine(struct phy_device *phydev,
515 unsigned long jiffies)
516{
517 mod_delayed_work(system_power_efficient_wq, &phydev->state_queue,
518 jiffies);
519}
520
521/**
522 * phy_trigger_machine - Trigger the state machine to run now
523 *
524 * @phydev: the phy_device struct
525 */
526void phy_trigger_machine(struct phy_device *phydev)
527{
528 phy_queue_state_machine(phydev, 0);
529}
530EXPORT_SYMBOL(phy_trigger_machine);
531
532static void phy_abort_cable_test(struct phy_device *phydev)
533{
534 int err;
535
536 ethnl_cable_test_finished(phydev);
537
538 err = phy_init_hw(phydev);
539 if (err)
540 phydev_err(phydev, "Error while aborting cable test");
541}
542
543/**
544 * phy_ethtool_get_strings - Get the statistic counter names
545 *
546 * @phydev: the phy_device struct
547 * @data: Where to put the strings
548 */
549int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data)
550{
551 if (!phydev->drv)
552 return -EIO;
553
554 mutex_lock(&phydev->lock);
555 phydev->drv->get_strings(phydev, data);
556 mutex_unlock(&phydev->lock);
557
558 return 0;
559}
560EXPORT_SYMBOL(phy_ethtool_get_strings);
561
562/**
563 * phy_ethtool_get_sset_count - Get the number of statistic counters
564 *
565 * @phydev: the phy_device struct
566 */
567int phy_ethtool_get_sset_count(struct phy_device *phydev)
568{
569 int ret;
570
571 if (!phydev->drv)
572 return -EIO;
573
574 if (phydev->drv->get_sset_count &&
575 phydev->drv->get_strings &&
576 phydev->drv->get_stats) {
577 mutex_lock(&phydev->lock);
578 ret = phydev->drv->get_sset_count(phydev);
579 mutex_unlock(&phydev->lock);
580
581 return ret;
582 }
583
584 return -EOPNOTSUPP;
585}
586EXPORT_SYMBOL(phy_ethtool_get_sset_count);
587
588/**
589 * phy_ethtool_get_stats - Get the statistic counters
590 *
591 * @phydev: the phy_device struct
592 * @stats: What counters to get
593 * @data: Where to store the counters
594 */
595int phy_ethtool_get_stats(struct phy_device *phydev,
596 struct ethtool_stats *stats, u64 *data)
597{
598 if (!phydev->drv)
599 return -EIO;
600
601 mutex_lock(&phydev->lock);
602 phydev->drv->get_stats(phydev, stats, data);
603 mutex_unlock(&phydev->lock);
604
605 return 0;
606}
607EXPORT_SYMBOL(phy_ethtool_get_stats);
608
609/**
610 * __phy_ethtool_get_phy_stats - Retrieve standardized PHY statistics
611 * @phydev: Pointer to the PHY device
612 * @phy_stats: Pointer to ethtool_eth_phy_stats structure
613 * @phydev_stats: Pointer to ethtool_phy_stats structure
614 *
615 * Fetches PHY statistics using a kernel-defined interface for consistent
616 * diagnostics. Unlike phy_ethtool_get_stats(), which allows custom stats,
617 * this function enforces a standardized format for better interoperability.
618 */
619void __phy_ethtool_get_phy_stats(struct phy_device *phydev,
620 struct ethtool_eth_phy_stats *phy_stats,
621 struct ethtool_phy_stats *phydev_stats)
622{
623 if (!phydev->drv || !phydev->drv->get_phy_stats)
624 return;
625
626 mutex_lock(&phydev->lock);
627 phydev->drv->get_phy_stats(phydev, phy_stats, phydev_stats);
628 mutex_unlock(&phydev->lock);
629}
630
631/**
632 * __phy_ethtool_get_link_ext_stats - Retrieve extended link statistics for a PHY
633 * @phydev: Pointer to the PHY device
634 * @link_stats: Pointer to the structure to store extended link statistics
635 *
636 * Populates the ethtool_link_ext_stats structure with link down event counts
637 * and additional driver-specific link statistics, if available.
638 */
639void __phy_ethtool_get_link_ext_stats(struct phy_device *phydev,
640 struct ethtool_link_ext_stats *link_stats)
641{
642 link_stats->link_down_events = READ_ONCE(phydev->link_down_events);
643
644 if (!phydev->drv || !phydev->drv->get_link_stats)
645 return;
646
647 mutex_lock(&phydev->lock);
648 phydev->drv->get_link_stats(phydev, link_stats);
649 mutex_unlock(&phydev->lock);
650}
651
652/**
653 * phy_ethtool_get_plca_cfg - Get PLCA RS configuration
654 * @phydev: the phy_device struct
655 * @plca_cfg: where to store the retrieved configuration
656 *
657 * Retrieve the PLCA configuration from the PHY. Return 0 on success or a
658 * negative value if an error occurred.
659 */
660int phy_ethtool_get_plca_cfg(struct phy_device *phydev,
661 struct phy_plca_cfg *plca_cfg)
662{
663 int ret;
664
665 if (!phydev->drv) {
666 ret = -EIO;
667 goto out;
668 }
669
670 if (!phydev->drv->get_plca_cfg) {
671 ret = -EOPNOTSUPP;
672 goto out;
673 }
674
675 mutex_lock(&phydev->lock);
676 ret = phydev->drv->get_plca_cfg(phydev, plca_cfg);
677
678 mutex_unlock(&phydev->lock);
679out:
680 return ret;
681}
682
683/**
684 * plca_check_valid - Check PLCA configuration before enabling
685 * @phydev: the phy_device struct
686 * @plca_cfg: current PLCA configuration
687 * @extack: extack for reporting useful error messages
688 *
689 * Checks whether the PLCA and PHY configuration are consistent and it is safe
690 * to enable PLCA. Returns 0 on success or a negative value if the PLCA or PHY
691 * configuration is not consistent.
692 */
693static int plca_check_valid(struct phy_device *phydev,
694 const struct phy_plca_cfg *plca_cfg,
695 struct netlink_ext_ack *extack)
696{
697 int ret = 0;
698
699 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT1S_P2MP_Half_BIT,
700 phydev->advertising)) {
701 ret = -EOPNOTSUPP;
702 NL_SET_ERR_MSG(extack,
703 "Point to Multi-Point mode is not enabled");
704 } else if (plca_cfg->node_id >= 255) {
705 NL_SET_ERR_MSG(extack, "PLCA node ID is not set");
706 ret = -EINVAL;
707 }
708
709 return ret;
710}
711
712/**
713 * phy_ethtool_set_plca_cfg - Set PLCA RS configuration
714 * @phydev: the phy_device struct
715 * @plca_cfg: new PLCA configuration to apply
716 * @extack: extack for reporting useful error messages
717 *
718 * Sets the PLCA configuration in the PHY. Return 0 on success or a
719 * negative value if an error occurred.
720 */
721int phy_ethtool_set_plca_cfg(struct phy_device *phydev,
722 const struct phy_plca_cfg *plca_cfg,
723 struct netlink_ext_ack *extack)
724{
725 struct phy_plca_cfg *curr_plca_cfg;
726 int ret;
727
728 if (!phydev->drv) {
729 ret = -EIO;
730 goto out;
731 }
732
733 if (!phydev->drv->set_plca_cfg ||
734 !phydev->drv->get_plca_cfg) {
735 ret = -EOPNOTSUPP;
736 goto out;
737 }
738
739 curr_plca_cfg = kmalloc(sizeof(*curr_plca_cfg), GFP_KERNEL);
740 if (!curr_plca_cfg) {
741 ret = -ENOMEM;
742 goto out;
743 }
744
745 mutex_lock(&phydev->lock);
746
747 ret = phydev->drv->get_plca_cfg(phydev, curr_plca_cfg);
748 if (ret)
749 goto out_drv;
750
751 if (curr_plca_cfg->enabled < 0 && plca_cfg->enabled >= 0) {
752 NL_SET_ERR_MSG(extack,
753 "PHY does not support changing the PLCA 'enable' attribute");
754 ret = -EINVAL;
755 goto out_drv;
756 }
757
758 if (curr_plca_cfg->node_id < 0 && plca_cfg->node_id >= 0) {
759 NL_SET_ERR_MSG(extack,
760 "PHY does not support changing the PLCA 'local node ID' attribute");
761 ret = -EINVAL;
762 goto out_drv;
763 }
764
765 if (curr_plca_cfg->node_cnt < 0 && plca_cfg->node_cnt >= 0) {
766 NL_SET_ERR_MSG(extack,
767 "PHY does not support changing the PLCA 'node count' attribute");
768 ret = -EINVAL;
769 goto out_drv;
770 }
771
772 if (curr_plca_cfg->to_tmr < 0 && plca_cfg->to_tmr >= 0) {
773 NL_SET_ERR_MSG(extack,
774 "PHY does not support changing the PLCA 'TO timer' attribute");
775 ret = -EINVAL;
776 goto out_drv;
777 }
778
779 if (curr_plca_cfg->burst_cnt < 0 && plca_cfg->burst_cnt >= 0) {
780 NL_SET_ERR_MSG(extack,
781 "PHY does not support changing the PLCA 'burst count' attribute");
782 ret = -EINVAL;
783 goto out_drv;
784 }
785
786 if (curr_plca_cfg->burst_tmr < 0 && plca_cfg->burst_tmr >= 0) {
787 NL_SET_ERR_MSG(extack,
788 "PHY does not support changing the PLCA 'burst timer' attribute");
789 ret = -EINVAL;
790 goto out_drv;
791 }
792
793 // if enabling PLCA, perform a few sanity checks
794 if (plca_cfg->enabled > 0) {
795 // allow setting node_id concurrently with enabled
796 if (plca_cfg->node_id >= 0)
797 curr_plca_cfg->node_id = plca_cfg->node_id;
798
799 ret = plca_check_valid(phydev, curr_plca_cfg, extack);
800 if (ret)
801 goto out_drv;
802 }
803
804 ret = phydev->drv->set_plca_cfg(phydev, plca_cfg);
805
806out_drv:
807 kfree(curr_plca_cfg);
808 mutex_unlock(&phydev->lock);
809out:
810 return ret;
811}
812
813/**
814 * phy_ethtool_get_plca_status - Get PLCA RS status information
815 * @phydev: the phy_device struct
816 * @plca_st: where to store the retrieved status information
817 *
818 * Retrieve the PLCA status information from the PHY. Return 0 on success or a
819 * negative value if an error occurred.
820 */
821int phy_ethtool_get_plca_status(struct phy_device *phydev,
822 struct phy_plca_status *plca_st)
823{
824 int ret;
825
826 if (!phydev->drv) {
827 ret = -EIO;
828 goto out;
829 }
830
831 if (!phydev->drv->get_plca_status) {
832 ret = -EOPNOTSUPP;
833 goto out;
834 }
835
836 mutex_lock(&phydev->lock);
837 ret = phydev->drv->get_plca_status(phydev, plca_st);
838
839 mutex_unlock(&phydev->lock);
840out:
841 return ret;
842}
843
844/**
845 * phy_start_cable_test - Start a cable test
846 *
847 * @phydev: the phy_device struct
848 * @extack: extack for reporting useful error messages
849 */
850int phy_start_cable_test(struct phy_device *phydev,
851 struct netlink_ext_ack *extack)
852{
853 struct net_device *dev = phydev->attached_dev;
854 int err = -ENOMEM;
855
856 if (!(phydev->drv &&
857 phydev->drv->cable_test_start &&
858 phydev->drv->cable_test_get_status)) {
859 NL_SET_ERR_MSG(extack,
860 "PHY driver does not support cable testing");
861 return -EOPNOTSUPP;
862 }
863
864 mutex_lock(&phydev->lock);
865 if (phydev->state == PHY_CABLETEST) {
866 NL_SET_ERR_MSG(extack,
867 "PHY already performing a test");
868 err = -EBUSY;
869 goto out;
870 }
871
872 if (phydev->state < PHY_UP ||
873 phydev->state > PHY_CABLETEST) {
874 NL_SET_ERR_MSG(extack,
875 "PHY not configured. Try setting interface up");
876 err = -EBUSY;
877 goto out;
878 }
879
880 err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_NTF);
881 if (err)
882 goto out;
883
884 /* Mark the carrier down until the test is complete */
885 phy_link_down(phydev);
886
887 netif_testing_on(dev);
888 err = phydev->drv->cable_test_start(phydev);
889 if (err) {
890 netif_testing_off(dev);
891 phy_link_up(phydev);
892 goto out_free;
893 }
894
895 phydev->state = PHY_CABLETEST;
896
897 if (phy_polling_mode(phydev))
898 phy_trigger_machine(phydev);
899
900 mutex_unlock(&phydev->lock);
901
902 return 0;
903
904out_free:
905 ethnl_cable_test_free(phydev);
906out:
907 mutex_unlock(&phydev->lock);
908
909 return err;
910}
911EXPORT_SYMBOL(phy_start_cable_test);
912
913/**
914 * phy_start_cable_test_tdr - Start a raw TDR cable test
915 *
916 * @phydev: the phy_device struct
917 * @extack: extack for reporting useful error messages
918 * @config: Configuration of the test to run
919 */
920int phy_start_cable_test_tdr(struct phy_device *phydev,
921 struct netlink_ext_ack *extack,
922 const struct phy_tdr_config *config)
923{
924 struct net_device *dev = phydev->attached_dev;
925 int err = -ENOMEM;
926
927 if (!(phydev->drv &&
928 phydev->drv->cable_test_tdr_start &&
929 phydev->drv->cable_test_get_status)) {
930 NL_SET_ERR_MSG(extack,
931 "PHY driver does not support cable test TDR");
932 return -EOPNOTSUPP;
933 }
934
935 mutex_lock(&phydev->lock);
936 if (phydev->state == PHY_CABLETEST) {
937 NL_SET_ERR_MSG(extack,
938 "PHY already performing a test");
939 err = -EBUSY;
940 goto out;
941 }
942
943 if (phydev->state < PHY_UP ||
944 phydev->state > PHY_CABLETEST) {
945 NL_SET_ERR_MSG(extack,
946 "PHY not configured. Try setting interface up");
947 err = -EBUSY;
948 goto out;
949 }
950
951 err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_TDR_NTF);
952 if (err)
953 goto out;
954
955 /* Mark the carrier down until the test is complete */
956 phy_link_down(phydev);
957
958 netif_testing_on(dev);
959 err = phydev->drv->cable_test_tdr_start(phydev, config);
960 if (err) {
961 netif_testing_off(dev);
962 phy_link_up(phydev);
963 goto out_free;
964 }
965
966 phydev->state = PHY_CABLETEST;
967
968 if (phy_polling_mode(phydev))
969 phy_trigger_machine(phydev);
970
971 mutex_unlock(&phydev->lock);
972
973 return 0;
974
975out_free:
976 ethnl_cable_test_free(phydev);
977out:
978 mutex_unlock(&phydev->lock);
979
980 return err;
981}
982EXPORT_SYMBOL(phy_start_cable_test_tdr);
983
984int phy_config_aneg(struct phy_device *phydev)
985{
986 if (phydev->drv->config_aneg)
987 return phydev->drv->config_aneg(phydev);
988
989 /* Clause 45 PHYs that don't implement Clause 22 registers are not
990 * allowed to call genphy_config_aneg()
991 */
992 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
993 return genphy_c45_config_aneg(phydev);
994
995 return genphy_config_aneg(phydev);
996}
997EXPORT_SYMBOL(phy_config_aneg);
998
999/**
1000 * phy_check_link_status - check link status and set state accordingly
1001 * @phydev: the phy_device struct
1002 *
1003 * Description: Check for link and whether autoneg was triggered / is running
1004 * and set state accordingly
1005 */
1006static int phy_check_link_status(struct phy_device *phydev)
1007{
1008 int err;
1009
1010 lockdep_assert_held(&phydev->lock);
1011
1012 /* Keep previous state if loopback is enabled because some PHYs
1013 * report that Link is Down when loopback is enabled.
1014 */
1015 if (phydev->loopback_enabled)
1016 return 0;
1017
1018 err = phy_read_status(phydev);
1019 if (err)
1020 return err;
1021
1022 if (phydev->link && phydev->state != PHY_RUNNING) {
1023 phy_check_downshift(phydev);
1024 phydev->state = PHY_RUNNING;
1025 err = genphy_c45_eee_is_active(phydev, NULL);
1026 phydev->eee_active = err > 0;
1027 phydev->enable_tx_lpi = phydev->eee_cfg.tx_lpi_enabled &&
1028 phydev->eee_active;
1029
1030 phy_link_up(phydev);
1031 } else if (!phydev->link && phydev->state != PHY_NOLINK) {
1032 phydev->state = PHY_NOLINK;
1033 phydev->eee_active = false;
1034 phydev->enable_tx_lpi = false;
1035 phy_link_down(phydev);
1036 }
1037
1038 return 0;
1039}
1040
1041/**
1042 * phy_inband_caps - query which in-band signalling modes are supported
1043 * @phydev: a pointer to a &struct phy_device
1044 * @interface: the interface mode for the PHY
1045 *
1046 * Returns zero if it is unknown what in-band signalling is supported by the
1047 * PHY (e.g. because the PHY driver doesn't implement the method.) Otherwise,
1048 * returns a bit mask of the LINK_INBAND_* values from
1049 * &enum link_inband_signalling to describe which inband modes are supported
1050 * by the PHY for this interface mode.
1051 */
1052unsigned int phy_inband_caps(struct phy_device *phydev,
1053 phy_interface_t interface)
1054{
1055 if (phydev->drv && phydev->drv->inband_caps)
1056 return phydev->drv->inband_caps(phydev, interface);
1057
1058 return 0;
1059}
1060EXPORT_SYMBOL_GPL(phy_inband_caps);
1061
1062/**
1063 * phy_config_inband - configure the desired PHY in-band mode
1064 * @phydev: the phy_device struct
1065 * @modes: in-band modes to configure
1066 *
1067 * Description: disables, enables or enables-with-bypass in-band signalling
1068 * between the PHY and host system.
1069 *
1070 * Returns: zero on success, or negative errno value.
1071 */
1072int phy_config_inband(struct phy_device *phydev, unsigned int modes)
1073{
1074 lockdep_assert_held(&phydev->lock);
1075
1076 if (!!(modes & LINK_INBAND_DISABLE) +
1077 !!(modes & LINK_INBAND_ENABLE) +
1078 !!(modes & LINK_INBAND_BYPASS) != 1)
1079 return -EINVAL;
1080
1081 if (!phydev->drv)
1082 return -EIO;
1083 else if (!phydev->drv->config_inband)
1084 return -EOPNOTSUPP;
1085
1086 return phydev->drv->config_inband(phydev, modes);
1087}
1088EXPORT_SYMBOL(phy_config_inband);
1089
1090/**
1091 * _phy_start_aneg - start auto-negotiation for this PHY device
1092 * @phydev: the phy_device struct
1093 *
1094 * Description: Sanitizes the settings (if we're not autonegotiating
1095 * them), and then calls the driver's config_aneg function.
1096 * If the PHYCONTROL Layer is operating, we change the state to
1097 * reflect the beginning of Auto-negotiation or forcing.
1098 */
1099int _phy_start_aneg(struct phy_device *phydev)
1100{
1101 int err;
1102
1103 lockdep_assert_held(&phydev->lock);
1104
1105 if (!phydev->drv)
1106 return -EIO;
1107
1108 if (AUTONEG_DISABLE == phydev->autoneg)
1109 phy_sanitize_settings(phydev);
1110
1111 err = phy_config_aneg(phydev);
1112 if (err < 0)
1113 return err;
1114
1115 if (phy_is_started(phydev))
1116 err = phy_check_link_status(phydev);
1117
1118 return err;
1119}
1120EXPORT_SYMBOL(_phy_start_aneg);
1121
1122/**
1123 * phy_start_aneg - start auto-negotiation for this PHY device
1124 * @phydev: the phy_device struct
1125 *
1126 * Description: Sanitizes the settings (if we're not autonegotiating
1127 * them), and then calls the driver's config_aneg function.
1128 * If the PHYCONTROL Layer is operating, we change the state to
1129 * reflect the beginning of Auto-negotiation or forcing.
1130 */
1131int phy_start_aneg(struct phy_device *phydev)
1132{
1133 int err;
1134
1135 mutex_lock(&phydev->lock);
1136 err = _phy_start_aneg(phydev);
1137 mutex_unlock(&phydev->lock);
1138
1139 return err;
1140}
1141EXPORT_SYMBOL(phy_start_aneg);
1142
1143static int phy_poll_aneg_done(struct phy_device *phydev)
1144{
1145 unsigned int retries = 100;
1146 int ret;
1147
1148 do {
1149 msleep(100);
1150 ret = phy_aneg_done(phydev);
1151 } while (!ret && --retries);
1152
1153 if (!ret)
1154 return -ETIMEDOUT;
1155
1156 return ret < 0 ? ret : 0;
1157}
1158
1159int phy_ethtool_ksettings_set(struct phy_device *phydev,
1160 const struct ethtool_link_ksettings *cmd)
1161{
1162 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
1163 u8 autoneg = cmd->base.autoneg;
1164 u8 duplex = cmd->base.duplex;
1165 u32 speed = cmd->base.speed;
1166
1167 if (cmd->base.phy_address != phydev->mdio.addr)
1168 return -EINVAL;
1169
1170 linkmode_copy(advertising, cmd->link_modes.advertising);
1171
1172 /* We make sure that we don't pass unsupported values in to the PHY */
1173 linkmode_and(advertising, advertising, phydev->supported);
1174
1175 /* Verify the settings we care about. */
1176 if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
1177 return -EINVAL;
1178
1179 if (autoneg == AUTONEG_ENABLE &&
1180 (linkmode_empty(advertising) ||
1181 !linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
1182 phydev->supported)))
1183 return -EINVAL;
1184
1185 if (autoneg == AUTONEG_DISABLE &&
1186 ((speed != SPEED_1000 &&
1187 speed != SPEED_100 &&
1188 speed != SPEED_10) ||
1189 (duplex != DUPLEX_HALF &&
1190 duplex != DUPLEX_FULL)))
1191 return -EINVAL;
1192
1193 mutex_lock(&phydev->lock);
1194 phydev->autoneg = autoneg;
1195
1196 if (autoneg == AUTONEG_DISABLE) {
1197 phydev->speed = speed;
1198 phydev->duplex = duplex;
1199 }
1200
1201 linkmode_copy(phydev->advertising, advertising);
1202
1203 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
1204 phydev->advertising, autoneg == AUTONEG_ENABLE);
1205
1206 phydev->master_slave_set = cmd->base.master_slave_cfg;
1207 phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
1208
1209 /* Restart the PHY */
1210 if (phy_is_started(phydev)) {
1211 phydev->state = PHY_UP;
1212 phy_trigger_machine(phydev);
1213 } else {
1214 _phy_start_aneg(phydev);
1215 }
1216
1217 mutex_unlock(&phydev->lock);
1218 return 0;
1219}
1220EXPORT_SYMBOL(phy_ethtool_ksettings_set);
1221
1222/**
1223 * phy_speed_down - set speed to lowest speed supported by both link partners
1224 * @phydev: the phy_device struct
1225 * @sync: perform action synchronously
1226 *
1227 * Description: Typically used to save energy when waiting for a WoL packet
1228 *
1229 * WARNING: Setting sync to false may cause the system being unable to suspend
1230 * in case the PHY generates an interrupt when finishing the autonegotiation.
1231 * This interrupt may wake up the system immediately after suspend.
1232 * Therefore use sync = false only if you're sure it's safe with the respective
1233 * network chip.
1234 */
1235int phy_speed_down(struct phy_device *phydev, bool sync)
1236{
1237 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
1238 int ret = 0;
1239
1240 mutex_lock(&phydev->lock);
1241
1242 if (phydev->autoneg != AUTONEG_ENABLE)
1243 goto out;
1244
1245 linkmode_copy(adv_tmp, phydev->advertising);
1246
1247 ret = phy_speed_down_core(phydev);
1248 if (ret)
1249 goto out;
1250
1251 linkmode_copy(phydev->adv_old, adv_tmp);
1252
1253 if (linkmode_equal(phydev->advertising, adv_tmp)) {
1254 ret = 0;
1255 goto out;
1256 }
1257
1258 ret = phy_config_aneg(phydev);
1259 if (ret)
1260 goto out;
1261
1262 ret = sync ? phy_poll_aneg_done(phydev) : 0;
1263out:
1264 mutex_unlock(&phydev->lock);
1265
1266 return ret;
1267}
1268EXPORT_SYMBOL_GPL(phy_speed_down);
1269
1270/**
1271 * phy_speed_up - (re)set advertised speeds to all supported speeds
1272 * @phydev: the phy_device struct
1273 *
1274 * Description: Used to revert the effect of phy_speed_down
1275 */
1276int phy_speed_up(struct phy_device *phydev)
1277{
1278 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
1279 int ret = 0;
1280
1281 mutex_lock(&phydev->lock);
1282
1283 if (phydev->autoneg != AUTONEG_ENABLE)
1284 goto out;
1285
1286 if (linkmode_empty(phydev->adv_old))
1287 goto out;
1288
1289 linkmode_copy(adv_tmp, phydev->advertising);
1290 linkmode_copy(phydev->advertising, phydev->adv_old);
1291 linkmode_zero(phydev->adv_old);
1292
1293 if (linkmode_equal(phydev->advertising, adv_tmp))
1294 goto out;
1295
1296 ret = phy_config_aneg(phydev);
1297out:
1298 mutex_unlock(&phydev->lock);
1299
1300 return ret;
1301}
1302EXPORT_SYMBOL_GPL(phy_speed_up);
1303
1304/**
1305 * phy_start_machine - start PHY state machine tracking
1306 * @phydev: the phy_device struct
1307 *
1308 * Description: The PHY infrastructure can run a state machine
1309 * which tracks whether the PHY is starting up, negotiating,
1310 * etc. This function starts the delayed workqueue which tracks
1311 * the state of the PHY. If you want to maintain your own state machine,
1312 * do not call this function.
1313 */
1314void phy_start_machine(struct phy_device *phydev)
1315{
1316 phy_trigger_machine(phydev);
1317}
1318EXPORT_SYMBOL_GPL(phy_start_machine);
1319
1320/**
1321 * phy_stop_machine - stop the PHY state machine tracking
1322 * @phydev: target phy_device struct
1323 *
1324 * Description: Stops the state machine delayed workqueue, sets the
1325 * state to UP (unless it wasn't up yet). This function must be
1326 * called BEFORE phy_detach.
1327 */
1328void phy_stop_machine(struct phy_device *phydev)
1329{
1330 cancel_delayed_work_sync(&phydev->state_queue);
1331
1332 mutex_lock(&phydev->lock);
1333 if (phy_is_started(phydev))
1334 phydev->state = PHY_UP;
1335 mutex_unlock(&phydev->lock);
1336}
1337
1338static void phy_process_error(struct phy_device *phydev)
1339{
1340 /* phydev->lock must be held for the state change to be safe */
1341 if (!mutex_is_locked(&phydev->lock))
1342 phydev_err(phydev, "PHY-device data unsafe context\n");
1343
1344 phydev->state = PHY_ERROR;
1345
1346 phy_trigger_machine(phydev);
1347}
1348
1349static void phy_error_precise(struct phy_device *phydev,
1350 const void *func, int err)
1351{
1352 WARN(1, "%pS: returned: %d\n", func, err);
1353 phy_process_error(phydev);
1354}
1355
1356/**
1357 * phy_error - enter ERROR state for this PHY device
1358 * @phydev: target phy_device struct
1359 *
1360 * Moves the PHY to the ERROR state in response to a read
1361 * or write error, and tells the controller the link is down.
1362 * Must be called with phydev->lock held.
1363 */
1364void phy_error(struct phy_device *phydev)
1365{
1366 WARN_ON(1);
1367 phy_process_error(phydev);
1368}
1369EXPORT_SYMBOL(phy_error);
1370
1371/**
1372 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
1373 * @phydev: target phy_device struct
1374 */
1375int phy_disable_interrupts(struct phy_device *phydev)
1376{
1377 /* Disable PHY interrupts */
1378 return phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
1379}
1380
1381/**
1382 * phy_interrupt - PHY interrupt handler
1383 * @irq: interrupt line
1384 * @phy_dat: phy_device pointer
1385 *
1386 * Description: Handle PHY interrupt
1387 */
1388static irqreturn_t phy_interrupt(int irq, void *phy_dat)
1389{
1390 struct phy_device *phydev = phy_dat;
1391 irqreturn_t ret;
1392
1393 /* Wakeup interrupts may occur during a system sleep transition.
1394 * Postpone handling until the PHY has resumed.
1395 */
1396 if (IS_ENABLED(CONFIG_PM_SLEEP) && phydev->irq_suspended) {
1397 struct net_device *netdev = phydev->attached_dev;
1398
1399 if (netdev) {
1400 struct device *parent = netdev->dev.parent;
1401
1402 if (netdev->ethtool->wol_enabled)
1403 pm_system_wakeup();
1404 else if (device_may_wakeup(&netdev->dev))
1405 pm_wakeup_dev_event(&netdev->dev, 0, true);
1406 else if (parent && device_may_wakeup(parent))
1407 pm_wakeup_dev_event(parent, 0, true);
1408 }
1409
1410 phydev->irq_rerun = 1;
1411 disable_irq_nosync(irq);
1412 return IRQ_HANDLED;
1413 }
1414
1415 mutex_lock(&phydev->lock);
1416 ret = phydev->drv->handle_interrupt(phydev);
1417 mutex_unlock(&phydev->lock);
1418
1419 return ret;
1420}
1421
1422/**
1423 * phy_enable_interrupts - Enable the interrupts from the PHY side
1424 * @phydev: target phy_device struct
1425 */
1426static int phy_enable_interrupts(struct phy_device *phydev)
1427{
1428 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
1429}
1430
1431/**
1432 * phy_update_stats - Update PHY device statistics if supported.
1433 * @phydev: Pointer to the PHY device structure.
1434 *
1435 * If the PHY driver provides an update_stats callback, this function
1436 * invokes it to update the PHY statistics. If not, it returns 0.
1437 *
1438 * Return: 0 on success, or a negative error code if the callback fails.
1439 */
1440static int phy_update_stats(struct phy_device *phydev)
1441{
1442 if (!phydev->drv->update_stats)
1443 return 0;
1444
1445 return phydev->drv->update_stats(phydev);
1446}
1447
1448/**
1449 * phy_request_interrupt - request and enable interrupt for a PHY device
1450 * @phydev: target phy_device struct
1451 *
1452 * Description: Request and enable the interrupt for the given PHY.
1453 * If this fails, then we set irq to PHY_POLL.
1454 * This should only be called with a valid IRQ number.
1455 */
1456void phy_request_interrupt(struct phy_device *phydev)
1457{
1458 int err;
1459
1460 err = request_threaded_irq(phydev->irq, NULL, phy_interrupt,
1461 IRQF_ONESHOT | IRQF_SHARED,
1462 phydev_name(phydev), phydev);
1463 if (err) {
1464 phydev_warn(phydev, "Error %d requesting IRQ %d, falling back to polling\n",
1465 err, phydev->irq);
1466 phydev->irq = PHY_POLL;
1467 } else {
1468 if (phy_enable_interrupts(phydev)) {
1469 phydev_warn(phydev, "Can't enable interrupt, falling back to polling\n");
1470 phy_free_interrupt(phydev);
1471 phydev->irq = PHY_POLL;
1472 }
1473 }
1474}
1475EXPORT_SYMBOL(phy_request_interrupt);
1476
1477/**
1478 * phy_free_interrupt - disable and free interrupt for a PHY device
1479 * @phydev: target phy_device struct
1480 *
1481 * Description: Disable and free the interrupt for the given PHY.
1482 * This should only be called with a valid IRQ number.
1483 */
1484void phy_free_interrupt(struct phy_device *phydev)
1485{
1486 phy_disable_interrupts(phydev);
1487 free_irq(phydev->irq, phydev);
1488}
1489EXPORT_SYMBOL(phy_free_interrupt);
1490
1491/**
1492 * phy_get_next_update_time - Determine the next PHY update time
1493 * @phydev: Pointer to the phy_device structure
1494 *
1495 * This function queries the PHY driver to get the time for the next polling
1496 * event. If the driver does not implement the callback, a default value is
1497 * used.
1498 *
1499 * Return: The time for the next polling event in jiffies
1500 */
1501static unsigned int phy_get_next_update_time(struct phy_device *phydev)
1502{
1503 if (phydev->drv && phydev->drv->get_next_update_time)
1504 return phydev->drv->get_next_update_time(phydev);
1505
1506 return PHY_STATE_TIME;
1507}
1508
1509enum phy_state_work {
1510 PHY_STATE_WORK_NONE,
1511 PHY_STATE_WORK_ANEG,
1512 PHY_STATE_WORK_SUSPEND,
1513};
1514
1515static enum phy_state_work _phy_state_machine(struct phy_device *phydev)
1516{
1517 enum phy_state_work state_work = PHY_STATE_WORK_NONE;
1518 struct net_device *dev = phydev->attached_dev;
1519 enum phy_state old_state = phydev->state;
1520 const void *func = NULL;
1521 bool finished = false;
1522 int err = 0;
1523
1524 switch (phydev->state) {
1525 case PHY_DOWN:
1526 case PHY_READY:
1527 break;
1528 case PHY_UP:
1529 state_work = PHY_STATE_WORK_ANEG;
1530 break;
1531 case PHY_NOLINK:
1532 case PHY_RUNNING:
1533 err = phy_check_link_status(phydev);
1534 func = &phy_check_link_status;
1535
1536 if (!err)
1537 err = phy_update_stats(phydev);
1538 break;
1539 case PHY_CABLETEST:
1540 err = phydev->drv->cable_test_get_status(phydev, &finished);
1541 if (err) {
1542 phy_abort_cable_test(phydev);
1543 netif_testing_off(dev);
1544 state_work = PHY_STATE_WORK_ANEG;
1545 phydev->state = PHY_UP;
1546 break;
1547 }
1548
1549 if (finished) {
1550 ethnl_cable_test_finished(phydev);
1551 netif_testing_off(dev);
1552 state_work = PHY_STATE_WORK_ANEG;
1553 phydev->state = PHY_UP;
1554 }
1555 break;
1556 case PHY_HALTED:
1557 if (phydev->link) {
1558 if (phydev->autoneg == AUTONEG_ENABLE) {
1559 phydev->speed = SPEED_UNKNOWN;
1560 phydev->duplex = DUPLEX_UNKNOWN;
1561 }
1562 if (phydev->master_slave_state !=
1563 MASTER_SLAVE_STATE_UNSUPPORTED)
1564 phydev->master_slave_state =
1565 MASTER_SLAVE_STATE_UNKNOWN;
1566 phydev->mdix = ETH_TP_MDI_INVALID;
1567 linkmode_zero(phydev->lp_advertising);
1568 }
1569 fallthrough;
1570 case PHY_ERROR:
1571 if (phydev->link) {
1572 phydev->link = 0;
1573 phydev->eee_active = false;
1574 phydev->enable_tx_lpi = false;
1575 phy_link_down(phydev);
1576 }
1577 state_work = PHY_STATE_WORK_SUSPEND;
1578 break;
1579 }
1580
1581 if (state_work == PHY_STATE_WORK_ANEG) {
1582 err = _phy_start_aneg(phydev);
1583 func = &_phy_start_aneg;
1584 }
1585
1586 if (err == -ENODEV)
1587 return state_work;
1588
1589 if (err < 0)
1590 phy_error_precise(phydev, func, err);
1591
1592 phy_process_state_change(phydev, old_state);
1593
1594 /* Only re-schedule a PHY state machine change if we are polling the
1595 * PHY, if PHY_MAC_INTERRUPT is set, then we will be moving
1596 * between states from phy_mac_interrupt().
1597 *
1598 * In state PHY_HALTED the PHY gets suspended, so rescheduling the
1599 * state machine would be pointless and possibly error prone when
1600 * called from phy_disconnect() synchronously.
1601 */
1602 if (phy_polling_mode(phydev) && phy_is_started(phydev))
1603 phy_queue_state_machine(phydev,
1604 phy_get_next_update_time(phydev));
1605
1606 return state_work;
1607}
1608
1609/* unlocked part of the PHY state machine */
1610static void _phy_state_machine_post_work(struct phy_device *phydev,
1611 enum phy_state_work state_work)
1612{
1613 if (state_work == PHY_STATE_WORK_SUSPEND)
1614 phy_suspend(phydev);
1615}
1616
1617/**
1618 * phy_state_machine - Handle the state machine
1619 * @work: work_struct that describes the work to be done
1620 */
1621void phy_state_machine(struct work_struct *work)
1622{
1623 struct delayed_work *dwork = to_delayed_work(work);
1624 struct phy_device *phydev =
1625 container_of(dwork, struct phy_device, state_queue);
1626 enum phy_state_work state_work;
1627
1628 mutex_lock(&phydev->lock);
1629 state_work = _phy_state_machine(phydev);
1630 mutex_unlock(&phydev->lock);
1631
1632 _phy_state_machine_post_work(phydev, state_work);
1633}
1634
1635/**
1636 * phy_stop - Bring down the PHY link, and stop checking the status
1637 * @phydev: target phy_device struct
1638 */
1639void phy_stop(struct phy_device *phydev)
1640{
1641 struct net_device *dev = phydev->attached_dev;
1642 enum phy_state_work state_work;
1643 enum phy_state old_state;
1644
1645 if (!phy_is_started(phydev) && phydev->state != PHY_DOWN &&
1646 phydev->state != PHY_ERROR) {
1647 WARN(1, "called from state %s\n",
1648 phy_state_to_str(phydev->state));
1649 return;
1650 }
1651
1652 mutex_lock(&phydev->lock);
1653 old_state = phydev->state;
1654
1655 if (phydev->state == PHY_CABLETEST) {
1656 phy_abort_cable_test(phydev);
1657 netif_testing_off(dev);
1658 }
1659
1660 if (phydev->sfp_bus)
1661 sfp_upstream_stop(phydev->sfp_bus);
1662
1663 phydev->state = PHY_HALTED;
1664 phy_process_state_change(phydev, old_state);
1665
1666 state_work = _phy_state_machine(phydev);
1667 mutex_unlock(&phydev->lock);
1668
1669 _phy_state_machine_post_work(phydev, state_work);
1670 phy_stop_machine(phydev);
1671
1672 /* Cannot call flush_scheduled_work() here as desired because
1673 * of rtnl_lock(), but PHY_HALTED shall guarantee irq handler
1674 * will not reenable interrupts.
1675 */
1676}
1677EXPORT_SYMBOL(phy_stop);
1678
1679/**
1680 * phy_start - start or restart a PHY device
1681 * @phydev: target phy_device struct
1682 *
1683 * Description: Indicates the attached device's readiness to
1684 * handle PHY-related work. Used during startup to start the
1685 * PHY, and after a call to phy_stop() to resume operation.
1686 * Also used to indicate the MDIO bus has cleared an error
1687 * condition.
1688 */
1689void phy_start(struct phy_device *phydev)
1690{
1691 mutex_lock(&phydev->lock);
1692
1693 if (phydev->state != PHY_READY && phydev->state != PHY_HALTED) {
1694 WARN(1, "called from state %s\n",
1695 phy_state_to_str(phydev->state));
1696 goto out;
1697 }
1698
1699 if (phydev->sfp_bus)
1700 sfp_upstream_start(phydev->sfp_bus);
1701
1702 /* if phy was suspended, bring the physical link up again */
1703 __phy_resume(phydev);
1704
1705 phydev->state = PHY_UP;
1706
1707 phy_start_machine(phydev);
1708out:
1709 mutex_unlock(&phydev->lock);
1710}
1711EXPORT_SYMBOL(phy_start);
1712
1713/**
1714 * phy_mac_interrupt - MAC says the link has changed
1715 * @phydev: phy_device struct with changed link
1716 *
1717 * The MAC layer is able to indicate there has been a change in the PHY link
1718 * status. Trigger the state machine and work a work queue.
1719 */
1720void phy_mac_interrupt(struct phy_device *phydev)
1721{
1722 /* Trigger a state machine change */
1723 phy_trigger_machine(phydev);
1724}
1725EXPORT_SYMBOL(phy_mac_interrupt);
1726
1727/**
1728 * phy_loopback - Configure loopback mode of PHY
1729 * @phydev: target phy_device struct
1730 * @enable: enable or disable loopback mode
1731 * @speed: enable loopback mode with speed
1732 *
1733 * Configure loopback mode of PHY and signal link down and link up if speed is
1734 * changing.
1735 *
1736 * Return: 0 on success, negative error code on failure.
1737 */
1738int phy_loopback(struct phy_device *phydev, bool enable, int speed)
1739{
1740 bool link_up = false;
1741 int ret = 0;
1742
1743 if (!phydev->drv)
1744 return -EIO;
1745
1746 mutex_lock(&phydev->lock);
1747
1748 if (enable && phydev->loopback_enabled) {
1749 ret = -EBUSY;
1750 goto out;
1751 }
1752
1753 if (!enable && !phydev->loopback_enabled) {
1754 ret = -EINVAL;
1755 goto out;
1756 }
1757
1758 if (enable) {
1759 /*
1760 * Link up is signaled with a defined speed. If speed changes,
1761 * then first link down and after that link up needs to be
1762 * signaled.
1763 */
1764 if (phydev->link && phydev->state == PHY_RUNNING) {
1765 /* link is up and signaled */
1766 if (speed && phydev->speed != speed) {
1767 /* signal link down and up for new speed */
1768 phydev->link = false;
1769 phydev->state = PHY_NOLINK;
1770 phy_link_down(phydev);
1771
1772 link_up = true;
1773 }
1774 } else {
1775 /* link is not signaled */
1776 if (speed) {
1777 /* signal link up for new speed */
1778 link_up = true;
1779 }
1780 }
1781 }
1782
1783 if (phydev->drv->set_loopback)
1784 ret = phydev->drv->set_loopback(phydev, enable, speed);
1785 else
1786 ret = genphy_loopback(phydev, enable, speed);
1787
1788 if (ret) {
1789 if (enable) {
1790 /* try to restore link if enabling loopback fails */
1791 if (phydev->drv->set_loopback)
1792 phydev->drv->set_loopback(phydev, false, 0);
1793 else
1794 genphy_loopback(phydev, false, 0);
1795 }
1796
1797 goto out;
1798 }
1799
1800 if (link_up) {
1801 phydev->link = true;
1802 phydev->state = PHY_RUNNING;
1803 phy_link_up(phydev);
1804 }
1805
1806 phydev->loopback_enabled = enable;
1807
1808out:
1809 mutex_unlock(&phydev->lock);
1810 return ret;
1811}
1812EXPORT_SYMBOL(phy_loopback);
1813
1814/**
1815 * phy_eee_tx_clock_stop_capable() - indicate whether the MAC can stop tx clock
1816 * @phydev: target phy_device struct
1817 *
1818 * Indicate whether the MAC can disable the transmit xMII clock while in LPI
1819 * state. Returns 1 if the MAC may stop the transmit clock, 0 if the MAC must
1820 * not stop the transmit clock, or negative error.
1821 */
1822int phy_eee_tx_clock_stop_capable(struct phy_device *phydev)
1823{
1824 int stat1;
1825
1826 stat1 = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_STAT1);
1827 if (stat1 < 0)
1828 return stat1;
1829
1830 return !!(stat1 & MDIO_PCS_STAT1_CLKSTOP_CAP);
1831}
1832EXPORT_SYMBOL_GPL(phy_eee_tx_clock_stop_capable);
1833
1834/**
1835 * phy_eee_rx_clock_stop() - configure PHY receive clock in LPI
1836 * @phydev: target phy_device struct
1837 * @clk_stop_enable: flag to indicate whether the clock can be stopped
1838 *
1839 * Configure whether the PHY can disable its receive clock during LPI mode,
1840 * See IEEE 802.3 sections 22.2.2.2, 35.2.2.10, and 45.2.3.1.4.
1841 *
1842 * Returns: 0 or negative error.
1843 */
1844int phy_eee_rx_clock_stop(struct phy_device *phydev, bool clk_stop_enable)
1845{
1846 /* Configure the PHY to stop receiving xMII
1847 * clock while it is signaling LPI.
1848 */
1849 return phy_modify_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1,
1850 MDIO_PCS_CTRL1_CLKSTOP_EN,
1851 clk_stop_enable ? MDIO_PCS_CTRL1_CLKSTOP_EN : 0);
1852}
1853EXPORT_SYMBOL_GPL(phy_eee_rx_clock_stop);
1854
1855/**
1856 * phy_init_eee - init and check the EEE feature
1857 * @phydev: target phy_device struct
1858 * @clk_stop_enable: PHY may stop the clock during LPI
1859 *
1860 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1861 * is supported by looking at the MMD registers 3.20 and 7.60/61
1862 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1863 * bit if required.
1864 */
1865int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1866{
1867 int ret;
1868
1869 if (!phydev->drv)
1870 return -EIO;
1871
1872 ret = genphy_c45_eee_is_active(phydev, NULL);
1873 if (ret < 0)
1874 return ret;
1875 if (!ret)
1876 return -EPROTONOSUPPORT;
1877
1878 if (clk_stop_enable)
1879 ret = phy_eee_rx_clock_stop(phydev, true);
1880
1881 return ret < 0 ? ret : 0;
1882}
1883EXPORT_SYMBOL(phy_init_eee);
1884
1885/**
1886 * phy_get_eee_err - report the EEE wake error count
1887 * @phydev: target phy_device struct
1888 *
1889 * Description: it is to report the number of time where the PHY
1890 * failed to complete its normal wake sequence.
1891 */
1892int phy_get_eee_err(struct phy_device *phydev)
1893{
1894 int ret;
1895
1896 if (!phydev->drv)
1897 return -EIO;
1898
1899 mutex_lock(&phydev->lock);
1900 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
1901 mutex_unlock(&phydev->lock);
1902
1903 return ret;
1904}
1905EXPORT_SYMBOL(phy_get_eee_err);
1906
1907/**
1908 * phy_ethtool_get_eee - get EEE supported and status
1909 * @phydev: target phy_device struct
1910 * @data: ethtool_keee data
1911 *
1912 * Description: get the current EEE settings, filling in all members of
1913 * @data.
1914 */
1915int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_keee *data)
1916{
1917 int ret;
1918
1919 if (!phydev->drv)
1920 return -EIO;
1921
1922 mutex_lock(&phydev->lock);
1923 ret = genphy_c45_ethtool_get_eee(phydev, data);
1924 eeecfg_to_eee(data, &phydev->eee_cfg);
1925 mutex_unlock(&phydev->lock);
1926
1927 return ret;
1928}
1929EXPORT_SYMBOL(phy_ethtool_get_eee);
1930
1931/**
1932 * phy_ethtool_set_eee_noneg - Adjusts MAC LPI configuration without PHY
1933 * renegotiation
1934 * @phydev: pointer to the target PHY device structure
1935 * @old_cfg: pointer to the eee_config structure containing the old EEE settings
1936 *
1937 * This function updates the Energy Efficient Ethernet (EEE) configuration
1938 * for cases where only the MAC's Low Power Idle (LPI) configuration changes,
1939 * without triggering PHY renegotiation. It ensures that the MAC is properly
1940 * informed of the new LPI settings by cycling the link down and up, which
1941 * is necessary for the MAC to adopt the new configuration. This adjustment
1942 * is done only if there is a change in the tx_lpi_enabled or tx_lpi_timer
1943 * configuration.
1944 */
1945static void phy_ethtool_set_eee_noneg(struct phy_device *phydev,
1946 const struct eee_config *old_cfg)
1947{
1948 bool enable_tx_lpi;
1949
1950 if (!phydev->link)
1951 return;
1952
1953 enable_tx_lpi = phydev->eee_cfg.tx_lpi_enabled && phydev->eee_active;
1954
1955 if (phydev->enable_tx_lpi != enable_tx_lpi ||
1956 phydev->eee_cfg.tx_lpi_timer != old_cfg->tx_lpi_timer) {
1957 phydev->enable_tx_lpi = false;
1958 phydev->link = false;
1959 phy_link_down(phydev);
1960 phydev->enable_tx_lpi = enable_tx_lpi;
1961 phydev->link = true;
1962 phy_link_up(phydev);
1963 }
1964}
1965
1966/**
1967 * phy_ethtool_set_eee - set EEE supported and status
1968 * @phydev: target phy_device struct
1969 * @data: ethtool_keee data
1970 *
1971 * Description: it is to program the Advertisement EEE register.
1972 */
1973int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_keee *data)
1974{
1975 struct eee_config old_cfg;
1976 int ret;
1977
1978 if (!phydev->drv)
1979 return -EIO;
1980
1981 mutex_lock(&phydev->lock);
1982
1983 old_cfg = phydev->eee_cfg;
1984 eee_to_eeecfg(&phydev->eee_cfg, data);
1985
1986 ret = genphy_c45_ethtool_set_eee(phydev, data);
1987 if (ret == 0)
1988 phy_ethtool_set_eee_noneg(phydev, &old_cfg);
1989 else if (ret < 0)
1990 phydev->eee_cfg = old_cfg;
1991
1992 mutex_unlock(&phydev->lock);
1993
1994 return ret < 0 ? ret : 0;
1995}
1996EXPORT_SYMBOL(phy_ethtool_set_eee);
1997
1998/**
1999 * phy_ethtool_set_wol - Configure Wake On LAN
2000 *
2001 * @phydev: target phy_device struct
2002 * @wol: Configuration requested
2003 */
2004int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
2005{
2006 int ret;
2007
2008 if (phydev->drv && phydev->drv->set_wol) {
2009 mutex_lock(&phydev->lock);
2010 ret = phydev->drv->set_wol(phydev, wol);
2011 mutex_unlock(&phydev->lock);
2012
2013 return ret;
2014 }
2015
2016 return -EOPNOTSUPP;
2017}
2018EXPORT_SYMBOL(phy_ethtool_set_wol);
2019
2020/**
2021 * phy_ethtool_get_wol - Get the current Wake On LAN configuration
2022 *
2023 * @phydev: target phy_device struct
2024 * @wol: Store the current configuration here
2025 */
2026void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
2027{
2028 if (phydev->drv && phydev->drv->get_wol) {
2029 mutex_lock(&phydev->lock);
2030 phydev->drv->get_wol(phydev, wol);
2031 mutex_unlock(&phydev->lock);
2032 }
2033}
2034EXPORT_SYMBOL(phy_ethtool_get_wol);
2035
2036int phy_ethtool_get_link_ksettings(struct net_device *ndev,
2037 struct ethtool_link_ksettings *cmd)
2038{
2039 struct phy_device *phydev = ndev->phydev;
2040
2041 if (!phydev)
2042 return -ENODEV;
2043
2044 phy_ethtool_ksettings_get(phydev, cmd);
2045
2046 return 0;
2047}
2048EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
2049
2050int phy_ethtool_set_link_ksettings(struct net_device *ndev,
2051 const struct ethtool_link_ksettings *cmd)
2052{
2053 struct phy_device *phydev = ndev->phydev;
2054
2055 if (!phydev)
2056 return -ENODEV;
2057
2058 return phy_ethtool_ksettings_set(phydev, cmd);
2059}
2060EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
2061
2062/**
2063 * phy_ethtool_nway_reset - Restart auto negotiation
2064 * @ndev: Network device to restart autoneg for
2065 */
2066int phy_ethtool_nway_reset(struct net_device *ndev)
2067{
2068 struct phy_device *phydev = ndev->phydev;
2069 int ret;
2070
2071 if (!phydev)
2072 return -ENODEV;
2073
2074 if (!phydev->drv)
2075 return -EIO;
2076
2077 mutex_lock(&phydev->lock);
2078 ret = phy_restart_aneg(phydev);
2079 mutex_unlock(&phydev->lock);
2080
2081 return ret;
2082}
2083EXPORT_SYMBOL(phy_ethtool_nway_reset);