projects/linux·blob master · refs

2106 lines · 52.3 KiB

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_obj(*curr_plca_cfg);
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_write_barrier - ensure the last write completed for this PHY device
1373 * @phydev: target phy_device struct
1374 *
1375 * MDIO bus controllers are not required to wait for write transactions to
1376 * complete before returning. Calling this function ensures that the previous
1377 * write has completed.
1378 */
1379static void phy_write_barrier(struct phy_device *phydev)
1380{
1381 if (mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, MII_PHYSID1) ==
1382 -EOPNOTSUPP)
1383 mdiobus_c45_read(phydev->mdio.bus, phydev->mdio.addr,
1384 MDIO_MMD_PMAPMD, MII_PHYSID1);
1385}
1386
1387/**
1388 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
1389 * @phydev: target phy_device struct
1390 */
1391int phy_disable_interrupts(struct phy_device *phydev)
1392{
1393 int err;
1394
1395 /* Disable PHY interrupts */
1396 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
1397 if (err)
1398 return err;
1399
1400 phy_write_barrier(phydev);
1401 return 0;
1402}
1403
1404/**
1405 * phy_interrupt - PHY interrupt handler
1406 * @irq: interrupt line
1407 * @phy_dat: phy_device pointer
1408 *
1409 * Description: Handle PHY interrupt
1410 */
1411static irqreturn_t phy_interrupt(int irq, void *phy_dat)
1412{
1413 struct phy_device *phydev = phy_dat;
1414 irqreturn_t ret;
1415
1416 /* Wakeup interrupts may occur during a system sleep transition.
1417 * Postpone handling until the PHY has resumed.
1418 */
1419 if (IS_ENABLED(CONFIG_PM_SLEEP) && phydev->irq_suspended) {
1420 struct net_device *netdev = phydev->attached_dev;
1421
1422 if (netdev) {
1423 struct device *parent = netdev->dev.parent;
1424
1425 if (netdev->ethtool->wol_enabled)
1426 pm_system_wakeup();
1427 else if (device_may_wakeup(&netdev->dev))
1428 pm_wakeup_dev_event(&netdev->dev, 0, true);
1429 else if (parent && device_may_wakeup(parent))
1430 pm_wakeup_dev_event(parent, 0, true);
1431 }
1432
1433 phydev->irq_rerun = 1;
1434 disable_irq_nosync(irq);
1435 return IRQ_HANDLED;
1436 }
1437
1438 mutex_lock(&phydev->lock);
1439 ret = phydev->drv->handle_interrupt(phydev);
1440 mutex_unlock(&phydev->lock);
1441
1442 return ret;
1443}
1444
1445/**
1446 * phy_enable_interrupts - Enable the interrupts from the PHY side
1447 * @phydev: target phy_device struct
1448 */
1449static int phy_enable_interrupts(struct phy_device *phydev)
1450{
1451 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
1452}
1453
1454/**
1455 * phy_update_stats - Update PHY device statistics if supported.
1456 * @phydev: Pointer to the PHY device structure.
1457 *
1458 * If the PHY driver provides an update_stats callback, this function
1459 * invokes it to update the PHY statistics. If not, it returns 0.
1460 *
1461 * Return: 0 on success, or a negative error code if the callback fails.
1462 */
1463static int phy_update_stats(struct phy_device *phydev)
1464{
1465 if (!phydev->drv->update_stats)
1466 return 0;
1467
1468 return phydev->drv->update_stats(phydev);
1469}
1470
1471/**
1472 * phy_request_interrupt - request and enable interrupt for a PHY device
1473 * @phydev: target phy_device struct
1474 *
1475 * Description: Request and enable the interrupt for the given PHY.
1476 * If this fails, then we set irq to PHY_POLL.
1477 * This should only be called with a valid IRQ number.
1478 */
1479void phy_request_interrupt(struct phy_device *phydev)
1480{
1481 int err;
1482
1483 err = request_threaded_irq(phydev->irq, NULL, phy_interrupt,
1484 IRQF_ONESHOT | IRQF_SHARED,
1485 phydev_name(phydev), phydev);
1486 if (err) {
1487 phydev_warn(phydev, "Error %d requesting IRQ %d, falling back to polling\n",
1488 err, phydev->irq);
1489 phydev->irq = PHY_POLL;
1490 } else {
1491 if (phy_enable_interrupts(phydev)) {
1492 phydev_warn(phydev, "Can't enable interrupt, falling back to polling\n");
1493 phy_free_interrupt(phydev);
1494 phydev->irq = PHY_POLL;
1495 }
1496 }
1497}
1498EXPORT_SYMBOL(phy_request_interrupt);
1499
1500/**
1501 * phy_free_interrupt - disable and free interrupt for a PHY device
1502 * @phydev: target phy_device struct
1503 *
1504 * Description: Disable and free the interrupt for the given PHY.
1505 * This should only be called with a valid IRQ number.
1506 */
1507void phy_free_interrupt(struct phy_device *phydev)
1508{
1509 phy_disable_interrupts(phydev);
1510 free_irq(phydev->irq, phydev);
1511}
1512EXPORT_SYMBOL(phy_free_interrupt);
1513
1514/**
1515 * phy_get_next_update_time - Determine the next PHY update time
1516 * @phydev: Pointer to the phy_device structure
1517 *
1518 * This function queries the PHY driver to get the time for the next polling
1519 * event. If the driver does not implement the callback, a default value is
1520 * used.
1521 *
1522 * Return: The time for the next polling event in jiffies
1523 */
1524static unsigned int phy_get_next_update_time(struct phy_device *phydev)
1525{
1526 if (phydev->drv && phydev->drv->get_next_update_time)
1527 return phydev->drv->get_next_update_time(phydev);
1528
1529 return PHY_STATE_TIME;
1530}
1531
1532enum phy_state_work {
1533 PHY_STATE_WORK_NONE,
1534 PHY_STATE_WORK_ANEG,
1535 PHY_STATE_WORK_SUSPEND,
1536};
1537
1538static enum phy_state_work _phy_state_machine(struct phy_device *phydev)
1539{
1540 enum phy_state_work state_work = PHY_STATE_WORK_NONE;
1541 struct net_device *dev = phydev->attached_dev;
1542 enum phy_state old_state = phydev->state;
1543 const void *func = NULL;
1544 bool finished = false;
1545 int err = 0;
1546
1547 switch (phydev->state) {
1548 case PHY_DOWN:
1549 case PHY_READY:
1550 break;
1551 case PHY_UP:
1552 state_work = PHY_STATE_WORK_ANEG;
1553 break;
1554 case PHY_NOLINK:
1555 case PHY_RUNNING:
1556 err = phy_check_link_status(phydev);
1557 func = &phy_check_link_status;
1558
1559 if (!err)
1560 err = phy_update_stats(phydev);
1561 break;
1562 case PHY_CABLETEST:
1563 err = phydev->drv->cable_test_get_status(phydev, &finished);
1564 if (err) {
1565 phy_abort_cable_test(phydev);
1566 netif_testing_off(dev);
1567 state_work = PHY_STATE_WORK_ANEG;
1568 phydev->state = PHY_UP;
1569 break;
1570 }
1571
1572 if (finished) {
1573 ethnl_cable_test_finished(phydev);
1574 netif_testing_off(dev);
1575 state_work = PHY_STATE_WORK_ANEG;
1576 phydev->state = PHY_UP;
1577 }
1578 break;
1579 case PHY_HALTED:
1580 if (phydev->link) {
1581 if (phydev->autoneg == AUTONEG_ENABLE) {
1582 phydev->speed = SPEED_UNKNOWN;
1583 phydev->duplex = DUPLEX_UNKNOWN;
1584 }
1585 if (phydev->master_slave_state !=
1586 MASTER_SLAVE_STATE_UNSUPPORTED)
1587 phydev->master_slave_state =
1588 MASTER_SLAVE_STATE_UNKNOWN;
1589 phydev->mdix = ETH_TP_MDI_INVALID;
1590 linkmode_zero(phydev->lp_advertising);
1591 }
1592 fallthrough;
1593 case PHY_ERROR:
1594 if (phydev->link) {
1595 phydev->link = 0;
1596 phydev->eee_active = false;
1597 phydev->enable_tx_lpi = false;
1598 phy_link_down(phydev);
1599 }
1600 state_work = PHY_STATE_WORK_SUSPEND;
1601 break;
1602 }
1603
1604 if (state_work == PHY_STATE_WORK_ANEG) {
1605 err = _phy_start_aneg(phydev);
1606 func = &_phy_start_aneg;
1607 }
1608
1609 if (err == -ENODEV)
1610 return state_work;
1611
1612 if (err < 0)
1613 phy_error_precise(phydev, func, err);
1614
1615 phy_process_state_change(phydev, old_state);
1616
1617 /* Only re-schedule a PHY state machine change if we are polling the
1618 * PHY, if PHY_MAC_INTERRUPT is set, then we will be moving
1619 * between states from phy_mac_interrupt().
1620 *
1621 * In state PHY_HALTED the PHY gets suspended, so rescheduling the
1622 * state machine would be pointless and possibly error prone when
1623 * called from phy_disconnect() synchronously.
1624 */
1625 if (phy_polling_mode(phydev) && phy_is_started(phydev))
1626 phy_queue_state_machine(phydev,
1627 phy_get_next_update_time(phydev));
1628
1629 return state_work;
1630}
1631
1632/* unlocked part of the PHY state machine */
1633static void _phy_state_machine_post_work(struct phy_device *phydev,
1634 enum phy_state_work state_work)
1635{
1636 if (state_work == PHY_STATE_WORK_SUSPEND)
1637 phy_suspend(phydev);
1638}
1639
1640/**
1641 * phy_state_machine - Handle the state machine
1642 * @work: work_struct that describes the work to be done
1643 */
1644void phy_state_machine(struct work_struct *work)
1645{
1646 struct delayed_work *dwork = to_delayed_work(work);
1647 struct phy_device *phydev =
1648 container_of(dwork, struct phy_device, state_queue);
1649 enum phy_state_work state_work;
1650
1651 mutex_lock(&phydev->lock);
1652 state_work = _phy_state_machine(phydev);
1653 mutex_unlock(&phydev->lock);
1654
1655 _phy_state_machine_post_work(phydev, state_work);
1656}
1657
1658/**
1659 * phy_stop - Bring down the PHY link, and stop checking the status
1660 * @phydev: target phy_device struct
1661 */
1662void phy_stop(struct phy_device *phydev)
1663{
1664 struct net_device *dev = phydev->attached_dev;
1665 enum phy_state_work state_work;
1666 enum phy_state old_state;
1667
1668 if (!phy_is_started(phydev) && phydev->state != PHY_DOWN &&
1669 phydev->state != PHY_ERROR) {
1670 WARN(1, "called from state %s\n",
1671 phy_state_to_str(phydev->state));
1672 return;
1673 }
1674
1675 mutex_lock(&phydev->lock);
1676 old_state = phydev->state;
1677
1678 if (phydev->state == PHY_CABLETEST) {
1679 phy_abort_cable_test(phydev);
1680 netif_testing_off(dev);
1681 }
1682
1683 if (phydev->sfp_bus)
1684 sfp_upstream_stop(phydev->sfp_bus);
1685
1686 phydev->state = PHY_HALTED;
1687 phy_process_state_change(phydev, old_state);
1688
1689 state_work = _phy_state_machine(phydev);
1690 mutex_unlock(&phydev->lock);
1691
1692 _phy_state_machine_post_work(phydev, state_work);
1693 phy_stop_machine(phydev);
1694
1695 /* Cannot call flush_scheduled_work() here as desired because
1696 * of rtnl_lock(), but PHY_HALTED shall guarantee irq handler
1697 * will not reenable interrupts.
1698 */
1699}
1700EXPORT_SYMBOL(phy_stop);
1701
1702/**
1703 * phy_start - start or restart a PHY device
1704 * @phydev: target phy_device struct
1705 *
1706 * Description: Indicates the attached device's readiness to
1707 * handle PHY-related work. Used during startup to start the
1708 * PHY, and after a call to phy_stop() to resume operation.
1709 * Also used to indicate the MDIO bus has cleared an error
1710 * condition.
1711 */
1712void phy_start(struct phy_device *phydev)
1713{
1714 mutex_lock(&phydev->lock);
1715
1716 if (phydev->state != PHY_READY && phydev->state != PHY_HALTED) {
1717 WARN(1, "called from state %s\n",
1718 phy_state_to_str(phydev->state));
1719 goto out;
1720 }
1721
1722 if (phydev->sfp_bus)
1723 sfp_upstream_start(phydev->sfp_bus);
1724
1725 /* if phy was suspended, bring the physical link up again */
1726 __phy_resume(phydev);
1727
1728 phydev->state = PHY_UP;
1729
1730 phy_start_machine(phydev);
1731out:
1732 mutex_unlock(&phydev->lock);
1733}
1734EXPORT_SYMBOL(phy_start);
1735
1736/**
1737 * phy_mac_interrupt - MAC says the link has changed
1738 * @phydev: phy_device struct with changed link
1739 *
1740 * The MAC layer is able to indicate there has been a change in the PHY link
1741 * status. Trigger the state machine and work a work queue.
1742 */
1743void phy_mac_interrupt(struct phy_device *phydev)
1744{
1745 /* Trigger a state machine change */
1746 phy_trigger_machine(phydev);
1747}
1748EXPORT_SYMBOL(phy_mac_interrupt);
1749
1750/**
1751 * phy_loopback - Configure loopback mode of PHY
1752 * @phydev: target phy_device struct
1753 * @enable: enable or disable loopback mode
1754 * @speed: enable loopback mode with speed
1755 *
1756 * Configure loopback mode of PHY and signal link down and link up if speed is
1757 * changing.
1758 *
1759 * Return: 0 on success, negative error code on failure.
1760 */
1761int phy_loopback(struct phy_device *phydev, bool enable, int speed)
1762{
1763 bool link_up = false;
1764 int ret = 0;
1765
1766 if (!phydev->drv)
1767 return -EIO;
1768
1769 mutex_lock(&phydev->lock);
1770
1771 if (enable && phydev->loopback_enabled) {
1772 ret = -EBUSY;
1773 goto out;
1774 }
1775
1776 if (!enable && !phydev->loopback_enabled) {
1777 ret = -EINVAL;
1778 goto out;
1779 }
1780
1781 if (enable) {
1782 /*
1783 * Link up is signaled with a defined speed. If speed changes,
1784 * then first link down and after that link up needs to be
1785 * signaled.
1786 */
1787 if (phydev->link && phydev->state == PHY_RUNNING) {
1788 /* link is up and signaled */
1789 if (speed && phydev->speed != speed) {
1790 /* signal link down and up for new speed */
1791 phydev->link = false;
1792 phydev->state = PHY_NOLINK;
1793 phy_link_down(phydev);
1794
1795 link_up = true;
1796 }
1797 } else {
1798 /* link is not signaled */
1799 if (speed) {
1800 /* signal link up for new speed */
1801 link_up = true;
1802 }
1803 }
1804 }
1805
1806 if (phydev->drv->set_loopback)
1807 ret = phydev->drv->set_loopback(phydev, enable, speed);
1808 else
1809 ret = genphy_loopback(phydev, enable, speed);
1810
1811 if (ret) {
1812 if (enable) {
1813 /* try to restore link if enabling loopback fails */
1814 if (phydev->drv->set_loopback)
1815 phydev->drv->set_loopback(phydev, false, 0);
1816 else
1817 genphy_loopback(phydev, false, 0);
1818 }
1819
1820 goto out;
1821 }
1822
1823 if (link_up) {
1824 phydev->link = true;
1825 phydev->state = PHY_RUNNING;
1826 phy_link_up(phydev);
1827 }
1828
1829 phydev->loopback_enabled = enable;
1830
1831out:
1832 mutex_unlock(&phydev->lock);
1833 return ret;
1834}
1835EXPORT_SYMBOL(phy_loopback);
1836
1837/**
1838 * phy_eee_tx_clock_stop_capable() - indicate whether the MAC can stop tx clock
1839 * @phydev: target phy_device struct
1840 *
1841 * Indicate whether the MAC can disable the transmit xMII clock while in LPI
1842 * state. Returns 1 if the MAC may stop the transmit clock, 0 if the MAC must
1843 * not stop the transmit clock, or negative error.
1844 */
1845int phy_eee_tx_clock_stop_capable(struct phy_device *phydev)
1846{
1847 int stat1;
1848
1849 stat1 = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_STAT1);
1850 if (stat1 < 0)
1851 return stat1;
1852
1853 return !!(stat1 & MDIO_PCS_STAT1_CLKSTOP_CAP);
1854}
1855EXPORT_SYMBOL_GPL(phy_eee_tx_clock_stop_capable);
1856
1857/**
1858 * phy_eee_rx_clock_stop() - configure PHY receive clock in LPI
1859 * @phydev: target phy_device struct
1860 * @clk_stop_enable: flag to indicate whether the clock can be stopped
1861 *
1862 * Configure whether the PHY can disable its receive clock during LPI mode,
1863 * See IEEE 802.3 sections 22.2.2.2, 35.2.2.10, and 45.2.3.1.4.
1864 *
1865 * Returns: 0 or negative error.
1866 */
1867int phy_eee_rx_clock_stop(struct phy_device *phydev, bool clk_stop_enable)
1868{
1869 /* Configure the PHY to stop receiving xMII
1870 * clock while it is signaling LPI.
1871 */
1872 return phy_modify_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1,
1873 MDIO_PCS_CTRL1_CLKSTOP_EN,
1874 clk_stop_enable ? MDIO_PCS_CTRL1_CLKSTOP_EN : 0);
1875}
1876EXPORT_SYMBOL_GPL(phy_eee_rx_clock_stop);
1877
1878/**
1879 * phy_init_eee - init and check the EEE feature
1880 * @phydev: target phy_device struct
1881 * @clk_stop_enable: PHY may stop the clock during LPI
1882 *
1883 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1884 * is supported by looking at the MMD registers 3.20 and 7.60/61
1885 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1886 * bit if required.
1887 */
1888int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1889{
1890 int ret;
1891
1892 if (!phydev->drv)
1893 return -EIO;
1894
1895 ret = genphy_c45_eee_is_active(phydev, NULL);
1896 if (ret < 0)
1897 return ret;
1898 if (!ret)
1899 return -EPROTONOSUPPORT;
1900
1901 if (clk_stop_enable)
1902 ret = phy_eee_rx_clock_stop(phydev, true);
1903
1904 return ret < 0 ? ret : 0;
1905}
1906EXPORT_SYMBOL(phy_init_eee);
1907
1908/**
1909 * phy_get_eee_err - report the EEE wake error count
1910 * @phydev: target phy_device struct
1911 *
1912 * Description: it is to report the number of time where the PHY
1913 * failed to complete its normal wake sequence.
1914 */
1915int phy_get_eee_err(struct phy_device *phydev)
1916{
1917 int ret;
1918
1919 if (!phydev->drv)
1920 return -EIO;
1921
1922 mutex_lock(&phydev->lock);
1923 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
1924 mutex_unlock(&phydev->lock);
1925
1926 return ret;
1927}
1928EXPORT_SYMBOL(phy_get_eee_err);
1929
1930/**
1931 * phy_ethtool_get_eee - get EEE supported and status
1932 * @phydev: target phy_device struct
1933 * @data: ethtool_keee data
1934 *
1935 * Description: get the current EEE settings, filling in all members of
1936 * @data.
1937 */
1938int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_keee *data)
1939{
1940 int ret;
1941
1942 if (!phydev->drv)
1943 return -EIO;
1944
1945 mutex_lock(&phydev->lock);
1946 ret = genphy_c45_ethtool_get_eee(phydev, data);
1947 eeecfg_to_eee(data, &phydev->eee_cfg);
1948 mutex_unlock(&phydev->lock);
1949
1950 return ret;
1951}
1952EXPORT_SYMBOL(phy_ethtool_get_eee);
1953
1954/**
1955 * phy_ethtool_set_eee_noneg - Adjusts MAC LPI configuration without PHY
1956 * renegotiation
1957 * @phydev: pointer to the target PHY device structure
1958 * @old_cfg: pointer to the eee_config structure containing the old EEE settings
1959 *
1960 * This function updates the Energy Efficient Ethernet (EEE) configuration
1961 * for cases where only the MAC's Low Power Idle (LPI) configuration changes,
1962 * without triggering PHY renegotiation. It ensures that the MAC is properly
1963 * informed of the new LPI settings by cycling the link down and up, which
1964 * is necessary for the MAC to adopt the new configuration. This adjustment
1965 * is done only if there is a change in the tx_lpi_enabled or tx_lpi_timer
1966 * configuration.
1967 */
1968static void phy_ethtool_set_eee_noneg(struct phy_device *phydev,
1969 const struct eee_config *old_cfg)
1970{
1971 bool enable_tx_lpi;
1972
1973 if (!phydev->link)
1974 return;
1975
1976 enable_tx_lpi = phydev->eee_cfg.tx_lpi_enabled && phydev->eee_active;
1977
1978 if (phydev->enable_tx_lpi != enable_tx_lpi ||
1979 phydev->eee_cfg.tx_lpi_timer != old_cfg->tx_lpi_timer) {
1980 phydev->enable_tx_lpi = false;
1981 phydev->link = false;
1982 phy_link_down(phydev);
1983 phydev->enable_tx_lpi = enable_tx_lpi;
1984 phydev->link = true;
1985 phy_link_up(phydev);
1986 }
1987}
1988
1989/**
1990 * phy_ethtool_set_eee - set EEE supported and status
1991 * @phydev: target phy_device struct
1992 * @data: ethtool_keee data
1993 *
1994 * Description: it is to program the Advertisement EEE register.
1995 */
1996int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_keee *data)
1997{
1998 struct eee_config old_cfg;
1999 int ret;
2000
2001 if (!phydev->drv)
2002 return -EIO;
2003
2004 mutex_lock(&phydev->lock);
2005
2006 old_cfg = phydev->eee_cfg;
2007 eee_to_eeecfg(&phydev->eee_cfg, data);
2008
2009 ret = genphy_c45_ethtool_set_eee(phydev, data);
2010 if (ret == 0)
2011 phy_ethtool_set_eee_noneg(phydev, &old_cfg);
2012 else if (ret < 0)
2013 phydev->eee_cfg = old_cfg;
2014
2015 mutex_unlock(&phydev->lock);
2016
2017 return ret < 0 ? ret : 0;
2018}
2019EXPORT_SYMBOL(phy_ethtool_set_eee);
2020
2021/**
2022 * phy_ethtool_set_wol - Configure Wake On LAN
2023 *
2024 * @phydev: target phy_device struct
2025 * @wol: Configuration requested
2026 */
2027int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
2028{
2029 int ret;
2030
2031 if (phydev->drv && phydev->drv->set_wol) {
2032 mutex_lock(&phydev->lock);
2033 ret = phydev->drv->set_wol(phydev, wol);
2034 mutex_unlock(&phydev->lock);
2035
2036 return ret;
2037 }
2038
2039 return -EOPNOTSUPP;
2040}
2041EXPORT_SYMBOL(phy_ethtool_set_wol);
2042
2043/**
2044 * phy_ethtool_get_wol - Get the current Wake On LAN configuration
2045 *
2046 * @phydev: target phy_device struct
2047 * @wol: Store the current configuration here
2048 */
2049void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
2050{
2051 if (phydev->drv && phydev->drv->get_wol) {
2052 mutex_lock(&phydev->lock);
2053 phydev->drv->get_wol(phydev, wol);
2054 mutex_unlock(&phydev->lock);
2055 }
2056}
2057EXPORT_SYMBOL(phy_ethtool_get_wol);
2058
2059int phy_ethtool_get_link_ksettings(struct net_device *ndev,
2060 struct ethtool_link_ksettings *cmd)
2061{
2062 struct phy_device *phydev = ndev->phydev;
2063
2064 if (!phydev)
2065 return -ENODEV;
2066
2067 phy_ethtool_ksettings_get(phydev, cmd);
2068
2069 return 0;
2070}
2071EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
2072
2073int phy_ethtool_set_link_ksettings(struct net_device *ndev,
2074 const struct ethtool_link_ksettings *cmd)
2075{
2076 struct phy_device *phydev = ndev->phydev;
2077
2078 if (!phydev)
2079 return -ENODEV;
2080
2081 return phy_ethtool_ksettings_set(phydev, cmd);
2082}
2083EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
2084
2085/**
2086 * phy_ethtool_nway_reset - Restart auto negotiation
2087 * @ndev: Network device to restart autoneg for
2088 */
2089int phy_ethtool_nway_reset(struct net_device *ndev)
2090{
2091 struct phy_device *phydev = ndev->phydev;
2092 int ret;
2093
2094 if (!phydev)
2095 return -ENODEV;
2096
2097 if (!phydev->drv)
2098 return -EIO;
2099
2100 mutex_lock(&phydev->lock);
2101 ret = phy_restart_aneg(phydev);
2102 mutex_unlock(&phydev->lock);
2103
2104 return ret;
2105}
2106EXPORT_SYMBOL(phy_ethtool_nway_reset);