blob: e223f0dc5437e362ad6fcce7456de052d70178f6 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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/etherdevice.h>
19#include <linux/skbuff.h>
20#include <linux/mm.h>
21#include <linux/module.h>
22#include <linux/mii.h>
23#include <linux/ethtool.h>
24#include <linux/phy.h>
25#include <linux/phy_led_triggers.h>
26#include <linux/sfp.h>
27#include <linux/workqueue.h>
28#include <linux/mdio.h>
29#include <linux/io.h>
30#include <linux/uaccess.h>
31#include <linux/atomic.h>
32
33#define PHY_STATE_TIME HZ
34
35#define PHY_STATE_STR(_state) \
36 case PHY_##_state: \
37 return __stringify(_state); \
38
39static const char *phy_state_to_str(enum phy_state st)
40{
41 switch (st) {
42 PHY_STATE_STR(DOWN)
43 PHY_STATE_STR(READY)
44 PHY_STATE_STR(UP)
45 PHY_STATE_STR(RUNNING)
46 PHY_STATE_STR(NOLINK)
47 PHY_STATE_STR(HALTED)
48 }
49
50 return NULL;
51}
52
53static void phy_link_up(struct phy_device *phydev)
54{
55 phydev->phy_link_change(phydev, true, true);
56 phy_led_trigger_change_speed(phydev);
57}
58
59static void phy_link_down(struct phy_device *phydev, bool do_carrier)
60{
61 phydev->phy_link_change(phydev, false, do_carrier);
62 phy_led_trigger_change_speed(phydev);
63}
64
65static const char *phy_pause_str(struct phy_device *phydev)
66{
67 bool local_pause, local_asym_pause;
68
69 if (phydev->autoneg == AUTONEG_DISABLE)
70 goto no_pause;
71
72 local_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
73 phydev->advertising);
74 local_asym_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
75 phydev->advertising);
76
77 if (local_pause && phydev->pause)
78 return "rx/tx";
79
80 if (local_asym_pause && phydev->asym_pause) {
81 if (local_pause)
82 return "rx";
83 if (phydev->pause)
84 return "tx";
85 }
86
87no_pause:
88 return "off";
89}
90
91/**
92 * phy_print_status - Convenience function to print out the current phy status
93 * @phydev: the phy_device struct
94 */
95void phy_print_status(struct phy_device *phydev)
96{
97 if (phydev->link) {
98 netdev_info(phydev->attached_dev,
99 "Link is Up - %s/%s - flow control %s\n",
100 phy_speed_to_str(phydev->speed),
101 phy_duplex_to_str(phydev->duplex),
102 phy_pause_str(phydev));
103 } else {
104 netdev_info(phydev->attached_dev, "Link is Down\n");
105 }
106}
107EXPORT_SYMBOL(phy_print_status);
108
109/**
110 * phy_clear_interrupt - Ack the phy device's interrupt
111 * @phydev: the phy_device struct
112 *
113 * If the @phydev driver has an ack_interrupt function, call it to
114 * ack and clear the phy device's interrupt.
115 *
116 * Returns 0 on success or < 0 on error.
117 */
118static int phy_clear_interrupt(struct phy_device *phydev)
119{
120 int ret = 0;
121
122 if (phydev->drv->ack_interrupt) {
123 mutex_lock(&phydev->lock);
124 ret = phydev->drv->ack_interrupt(phydev);
125 mutex_unlock(&phydev->lock);
126 }
127
128 return ret;
129}
130
131/**
132 * phy_config_interrupt - configure the PHY device for the requested interrupts
133 * @phydev: the phy_device struct
134 * @interrupts: interrupt flags to configure for this @phydev
135 *
136 * Returns 0 on success or < 0 on error.
137 */
138static int phy_config_interrupt(struct phy_device *phydev, bool interrupts)
139{
140 phydev->interrupts = interrupts ? 1 : 0;
141 if (phydev->drv->config_intr)
142 return phydev->drv->config_intr(phydev);
143
144 return 0;
145}
146
147/**
148 * phy_restart_aneg - restart auto-negotiation
149 * @phydev: target phy_device struct
150 *
151 * Restart the autonegotiation on @phydev. Returns >= 0 on success or
152 * negative errno on error.
153 */
154int phy_restart_aneg(struct phy_device *phydev)
155{
156 int ret;
157
158 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
159 ret = genphy_c45_restart_aneg(phydev);
160 else
161 ret = genphy_restart_aneg(phydev);
162
163 return ret;
164}
165EXPORT_SYMBOL_GPL(phy_restart_aneg);
166
167/**
168 * phy_aneg_done - return auto-negotiation status
169 * @phydev: target phy_device struct
170 *
171 * Description: Return the auto-negotiation status from this @phydev
172 * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
173 * is still pending.
174 */
175int phy_aneg_done(struct phy_device *phydev)
176{
177 if (phydev->drv && phydev->drv->aneg_done)
178 return phydev->drv->aneg_done(phydev);
179 else if (phydev->is_c45)
180 return genphy_c45_aneg_done(phydev);
181 else
182 return genphy_aneg_done(phydev);
183}
184EXPORT_SYMBOL(phy_aneg_done);
185
186/**
187 * phy_find_valid - find a PHY setting that matches the requested parameters
188 * @speed: desired speed
189 * @duplex: desired duplex
190 * @supported: mask of supported link modes
191 *
192 * Locate a supported phy setting that is, in priority order:
193 * - an exact match for the specified speed and duplex mode
194 * - a match for the specified speed, or slower speed
195 * - the slowest supported speed
196 * Returns the matched phy_setting entry, or %NULL if no supported phy
197 * settings were found.
198 */
199static const struct phy_setting *
200phy_find_valid(int speed, int duplex, unsigned long *supported)
201{
202 return phy_lookup_setting(speed, duplex, supported, false);
203}
204
205/**
206 * phy_supported_speeds - return all speeds currently supported by a phy device
207 * @phy: The phy device to return supported speeds of.
208 * @speeds: buffer to store supported speeds in.
209 * @size: size of speeds buffer.
210 *
211 * Description: Returns the number of supported speeds, and fills the speeds
212 * buffer with the supported speeds. If speeds buffer is too small to contain
213 * all currently supported speeds, will return as many speeds as can fit.
214 */
215unsigned int phy_supported_speeds(struct phy_device *phy,
216 unsigned int *speeds,
217 unsigned int size)
218{
219 return phy_speeds(speeds, size, phy->supported);
220}
221
222/**
223 * phy_check_valid - check if there is a valid PHY setting which matches
224 * speed, duplex, and feature mask
225 * @speed: speed to match
226 * @duplex: duplex to match
227 * @features: A mask of the valid settings
228 *
229 * Description: Returns true if there is a valid setting, false otherwise.
230 */
231static inline bool phy_check_valid(int speed, int duplex,
232 unsigned long *features)
233{
234 return !!phy_lookup_setting(speed, duplex, features, true);
235}
236
237/**
238 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
239 * @phydev: the target phy_device struct
240 *
241 * Description: Make sure the PHY is set to supported speeds and
242 * duplexes. Drop down by one in this order: 1000/FULL,
243 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
244 */
245static void phy_sanitize_settings(struct phy_device *phydev)
246{
247 const struct phy_setting *setting;
248
249 setting = phy_find_valid(phydev->speed, phydev->duplex,
250 phydev->supported);
251 if (setting) {
252 phydev->speed = setting->speed;
253 phydev->duplex = setting->duplex;
254 } else {
255 /* We failed to find anything (no supported speeds?) */
256 phydev->speed = SPEED_UNKNOWN;
257 phydev->duplex = DUPLEX_UNKNOWN;
258 }
259}
260
261/**
262 * phy_ethtool_sset - generic ethtool sset function, handles all the details
263 * @phydev: target phy_device struct
264 * @cmd: ethtool_cmd
265 *
266 * A few notes about parameter checking:
267 *
268 * - We don't set port or transceiver, so we don't care what they
269 * were set to.
270 * - phy_start_aneg() will make sure forced settings are sane, and
271 * choose the next best ones from the ones selected, so we don't
272 * care if ethtool tries to give us bad values.
273 */
274int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
275{
276 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
277 u32 speed = ethtool_cmd_speed(cmd);
278
279 if (cmd->phy_address != phydev->mdio.addr)
280 return -EINVAL;
281
282 /* We make sure that we don't pass unsupported values in to the PHY */
283 ethtool_convert_legacy_u32_to_link_mode(advertising, cmd->advertising);
284 linkmode_and(advertising, advertising, phydev->supported);
285
286 /* Verify the settings we care about. */
287 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
288 return -EINVAL;
289
290 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
291 return -EINVAL;
292
293 if (cmd->autoneg == AUTONEG_DISABLE &&
294 ((speed != SPEED_1000 &&
295 speed != SPEED_100 &&
296 speed != SPEED_10) ||
297 (cmd->duplex != DUPLEX_HALF &&
298 cmd->duplex != DUPLEX_FULL)))
299 return -EINVAL;
300
301 phydev->autoneg = cmd->autoneg;
302
303 phydev->speed = speed;
304
305 linkmode_copy(phydev->advertising, advertising);
306
307 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
308 phydev->advertising, AUTONEG_ENABLE == cmd->autoneg);
309
310 phydev->duplex = cmd->duplex;
311
312 phydev->mdix_ctrl = cmd->eth_tp_mdix_ctrl;
313
314 /* Restart the PHY */
315 phy_start_aneg(phydev);
316
317 return 0;
318}
319EXPORT_SYMBOL(phy_ethtool_sset);
320
321int phy_ethtool_ksettings_set(struct phy_device *phydev,
322 const struct ethtool_link_ksettings *cmd)
323{
324 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
325 u8 autoneg = cmd->base.autoneg;
326 u8 duplex = cmd->base.duplex;
327 u32 speed = cmd->base.speed;
328
329 if (cmd->base.phy_address != phydev->mdio.addr)
330 return -EINVAL;
331
332 linkmode_copy(advertising, cmd->link_modes.advertising);
333
334 /* We make sure that we don't pass unsupported values in to the PHY */
335 linkmode_and(advertising, advertising, phydev->supported);
336
337 /* Verify the settings we care about. */
338 if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
339 return -EINVAL;
340
341 if (autoneg == AUTONEG_ENABLE && linkmode_empty(advertising))
342 return -EINVAL;
343
344 if (autoneg == AUTONEG_DISABLE &&
345 ((speed != SPEED_1000 &&
346 speed != SPEED_100 &&
347 speed != SPEED_10) ||
348 (duplex != DUPLEX_HALF &&
349 duplex != DUPLEX_FULL)))
350 return -EINVAL;
351
352 phydev->autoneg = autoneg;
353
354 if (autoneg == AUTONEG_DISABLE) {
355 phydev->speed = speed;
356 phydev->duplex = duplex;
357 }
358
359 linkmode_copy(phydev->advertising, advertising);
360
361 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
362 phydev->advertising, autoneg == AUTONEG_ENABLE);
363
364 phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
365
366 /* Restart the PHY */
367 phy_start_aneg(phydev);
368
369 return 0;
370}
371EXPORT_SYMBOL(phy_ethtool_ksettings_set);
372
373void phy_ethtool_ksettings_get(struct phy_device *phydev,
374 struct ethtool_link_ksettings *cmd)
375{
376 mutex_lock(&phydev->lock);
377 linkmode_copy(cmd->link_modes.supported, phydev->supported);
378 linkmode_copy(cmd->link_modes.advertising, phydev->advertising);
379 linkmode_copy(cmd->link_modes.lp_advertising, phydev->lp_advertising);
380
381 cmd->base.speed = phydev->speed;
382 cmd->base.duplex = phydev->duplex;
383 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
384 cmd->base.port = PORT_BNC;
385 else
386 cmd->base.port = PORT_MII;
387 cmd->base.transceiver = phy_is_internal(phydev) ?
388 XCVR_INTERNAL : XCVR_EXTERNAL;
389 cmd->base.phy_address = phydev->mdio.addr;
390 cmd->base.autoneg = phydev->autoneg;
391 cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
392 cmd->base.eth_tp_mdix = phydev->mdix;
393 mutex_unlock(&phydev->lock);
394}
395EXPORT_SYMBOL(phy_ethtool_ksettings_get);
396
397/**
398 * phy_mii_ioctl - generic PHY MII ioctl interface
399 * @phydev: the phy_device struct
400 * @ifr: &struct ifreq for socket ioctl's
401 * @cmd: ioctl cmd to execute
402 *
403 * Note that this function is currently incompatible with the
404 * PHYCONTROL layer. It changes registers without regard to
405 * current state. Use at own risk.
406 */
407int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
408{
409 struct mii_ioctl_data *mii_data = if_mii(ifr);
410 u16 val = mii_data->val_in;
411 bool change_autoneg = false;
412 int prtad, devad;
413
414 switch (cmd) {
415 case SIOCGMIIPHY:
416 mii_data->phy_id = phydev->mdio.addr;
417 /* fall through */
418
419 case SIOCGMIIREG:
420 if (mdio_phy_id_is_c45(mii_data->phy_id)) {
421 prtad = mdio_phy_id_prtad(mii_data->phy_id);
422 devad = mdio_phy_id_devad(mii_data->phy_id);
423 devad = MII_ADDR_C45 | devad << 16 | mii_data->reg_num;
424 } else {
425 prtad = mii_data->phy_id;
426 devad = mii_data->reg_num;
427 }
428 mii_data->val_out = mdiobus_read(phydev->mdio.bus, prtad,
429 devad);
430 return 0;
431
432 case SIOCSMIIREG:
433 if (mdio_phy_id_is_c45(mii_data->phy_id)) {
434 prtad = mdio_phy_id_prtad(mii_data->phy_id);
435 devad = mdio_phy_id_devad(mii_data->phy_id);
436 devad = MII_ADDR_C45 | devad << 16 | mii_data->reg_num;
437 } else {
438 prtad = mii_data->phy_id;
439 devad = mii_data->reg_num;
440 }
441 if (prtad == phydev->mdio.addr) {
442 switch (devad) {
443 case MII_BMCR:
444 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
445 if (phydev->autoneg == AUTONEG_ENABLE)
446 change_autoneg = true;
447 phydev->autoneg = AUTONEG_DISABLE;
448 if (val & BMCR_FULLDPLX)
449 phydev->duplex = DUPLEX_FULL;
450 else
451 phydev->duplex = DUPLEX_HALF;
452 if (val & BMCR_SPEED1000)
453 phydev->speed = SPEED_1000;
454 else if (val & BMCR_SPEED100)
455 phydev->speed = SPEED_100;
456 else phydev->speed = SPEED_10;
457 }
458 else {
459 if (phydev->autoneg == AUTONEG_DISABLE)
460 change_autoneg = true;
461 phydev->autoneg = AUTONEG_ENABLE;
462 }
463 break;
464 case MII_ADVERTISE:
465 mii_adv_mod_linkmode_adv_t(phydev->advertising,
466 val);
467 change_autoneg = true;
468 break;
469 case MII_CTRL1000:
470 mii_ctrl1000_mod_linkmode_adv_t(phydev->advertising,
471 val);
472 change_autoneg = true;
473 break;
474 default:
475 /* do nothing */
476 break;
477 }
478 }
479
480 mdiobus_write(phydev->mdio.bus, prtad, devad, val);
481
482 if (prtad == phydev->mdio.addr &&
483 devad == MII_BMCR &&
484 val & BMCR_RESET)
485 return phy_init_hw(phydev);
486
487 if (change_autoneg)
488 return phy_start_aneg(phydev);
489
490 return 0;
491
492 case SIOCSHWTSTAMP:
493 if (phydev->drv && phydev->drv->hwtstamp)
494 return phydev->drv->hwtstamp(phydev, ifr);
495 /* fall through */
496
497 default:
498 return -EOPNOTSUPP;
499 }
500}
501EXPORT_SYMBOL(phy_mii_ioctl);
502
503void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies)
504{
505 mod_delayed_work(system_power_efficient_wq, &phydev->state_queue,
506 jiffies);
507}
508EXPORT_SYMBOL(phy_queue_state_machine);
509
510static void phy_trigger_machine(struct phy_device *phydev)
511{
512 phy_queue_state_machine(phydev, 0);
513}
514
515static int phy_config_aneg(struct phy_device *phydev)
516{
517 if (phydev->drv->config_aneg)
518 return phydev->drv->config_aneg(phydev);
519
520 /* Clause 45 PHYs that don't implement Clause 22 registers are not
521 * allowed to call genphy_config_aneg()
522 */
523 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
524 return genphy_c45_config_aneg(phydev);
525
526 return genphy_config_aneg(phydev);
527}
528
529/**
530 * phy_check_link_status - check link status and set state accordingly
531 * @phydev: the phy_device struct
532 *
533 * Description: Check for link and whether autoneg was triggered / is running
534 * and set state accordingly
535 */
536static int phy_check_link_status(struct phy_device *phydev)
537{
538 int err;
539
540 WARN_ON(!mutex_is_locked(&phydev->lock));
541
542 /* Keep previous state if loopback is enabled because some PHYs
543 * report that Link is Down when loopback is enabled.
544 */
545 if (phydev->loopback_enabled)
546 return 0;
547
548 err = phy_read_status(phydev);
549 if (err)
550 return err;
551
552 if (phydev->link && phydev->state != PHY_RUNNING) {
553 phydev->state = PHY_RUNNING;
554 phy_link_up(phydev);
555 } else if (!phydev->link && phydev->state != PHY_NOLINK) {
556 phydev->state = PHY_NOLINK;
557 phy_link_down(phydev, true);
558 }
559
560 return 0;
561}
562
563/**
564 * _phy_start_aneg - start auto-negotiation for this PHY device
565 * @phydev: the phy_device struct
566 *
567 * Description: Sanitizes the settings (if we're not autonegotiating
568 * them), and then calls the driver's config_aneg function.
569 * If the PHYCONTROL Layer is operating, we change the state to
570 * reflect the beginning of Auto-negotiation or forcing.
571 */
572static int _phy_start_aneg(struct phy_device *phydev)
573{
574 int err;
575
576 lockdep_assert_held(&phydev->lock);
577
578 if (!phydev->drv)
579 return -EIO;
580
581 if (AUTONEG_DISABLE == phydev->autoneg)
582 phy_sanitize_settings(phydev);
583
584 err = phy_config_aneg(phydev);
585 if (err < 0)
586 return err;
587
588 if (phy_is_started(phydev))
589 err = phy_check_link_status(phydev);
590
591 return err;
592}
593
594/**
595 * phy_start_aneg - start auto-negotiation for this PHY device
596 * @phydev: the phy_device struct
597 *
598 * Description: Sanitizes the settings (if we're not autonegotiating
599 * them), and then calls the driver's config_aneg function.
600 * If the PHYCONTROL Layer is operating, we change the state to
601 * reflect the beginning of Auto-negotiation or forcing.
602 */
603int phy_start_aneg(struct phy_device *phydev)
604{
605 int err;
606
607 mutex_lock(&phydev->lock);
608 err = _phy_start_aneg(phydev);
609 mutex_unlock(&phydev->lock);
610
611 return err;
612}
613EXPORT_SYMBOL(phy_start_aneg);
614
615static int phy_poll_aneg_done(struct phy_device *phydev)
616{
617 unsigned int retries = 100;
618 int ret;
619
620 do {
621 msleep(100);
622 ret = phy_aneg_done(phydev);
623 } while (!ret && --retries);
624
625 if (!ret)
626 return -ETIMEDOUT;
627
628 return ret < 0 ? ret : 0;
629}
630
631/**
632 * phy_speed_down - set speed to lowest speed supported by both link partners
633 * @phydev: the phy_device struct
634 * @sync: perform action synchronously
635 *
636 * Description: Typically used to save energy when waiting for a WoL packet
637 *
638 * WARNING: Setting sync to false may cause the system being unable to suspend
639 * in case the PHY generates an interrupt when finishing the autonegotiation.
640 * This interrupt may wake up the system immediately after suspend.
641 * Therefore use sync = false only if you're sure it's safe with the respective
642 * network chip.
643 */
644int phy_speed_down(struct phy_device *phydev, bool sync)
645{
646 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
647 int ret;
648
649 if (phydev->autoneg != AUTONEG_ENABLE)
650 return 0;
651
652 linkmode_copy(adv_tmp, phydev->advertising);
653
654 ret = phy_speed_down_core(phydev);
655 if (ret)
656 return ret;
657
658 linkmode_copy(phydev->adv_old, adv_tmp);
659
660 if (linkmode_equal(phydev->advertising, adv_tmp))
661 return 0;
662
663 ret = phy_config_aneg(phydev);
664 if (ret)
665 return ret;
666
667 return sync ? phy_poll_aneg_done(phydev) : 0;
668}
669EXPORT_SYMBOL_GPL(phy_speed_down);
670
671/**
672 * phy_speed_up - (re)set advertised speeds to all supported speeds
673 * @phydev: the phy_device struct
674 *
675 * Description: Used to revert the effect of phy_speed_down
676 */
677int phy_speed_up(struct phy_device *phydev)
678{
679 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
680
681 if (phydev->autoneg != AUTONEG_ENABLE)
682 return 0;
683
684 if (linkmode_empty(phydev->adv_old))
685 return 0;
686
687 linkmode_copy(adv_tmp, phydev->advertising);
688 linkmode_copy(phydev->advertising, phydev->adv_old);
689 linkmode_zero(phydev->adv_old);
690
691 if (linkmode_equal(phydev->advertising, adv_tmp))
692 return 0;
693
694 return phy_config_aneg(phydev);
695}
696EXPORT_SYMBOL_GPL(phy_speed_up);
697
698/**
699 * phy_start_machine - start PHY state machine tracking
700 * @phydev: the phy_device struct
701 *
702 * Description: The PHY infrastructure can run a state machine
703 * which tracks whether the PHY is starting up, negotiating,
704 * etc. This function starts the delayed workqueue which tracks
705 * the state of the PHY. If you want to maintain your own state machine,
706 * do not call this function.
707 */
708void phy_start_machine(struct phy_device *phydev)
709{
710 phy_trigger_machine(phydev);
711}
712EXPORT_SYMBOL_GPL(phy_start_machine);
713
714/**
715 * phy_stop_machine - stop the PHY state machine tracking
716 * @phydev: target phy_device struct
717 *
718 * Description: Stops the state machine delayed workqueue, sets the
719 * state to UP (unless it wasn't up yet). This function must be
720 * called BEFORE phy_detach.
721 */
722void phy_stop_machine(struct phy_device *phydev)
723{
724 cancel_delayed_work_sync(&phydev->state_queue);
725
726 mutex_lock(&phydev->lock);
727 if (phy_is_started(phydev))
728 phydev->state = PHY_UP;
729 mutex_unlock(&phydev->lock);
730}
731
732/**
733 * phy_error - enter HALTED state for this PHY device
734 * @phydev: target phy_device struct
735 *
736 * Moves the PHY to the HALTED state in response to a read
737 * or write error, and tells the controller the link is down.
738 * Must not be called from interrupt context, or while the
739 * phydev->lock is held.
740 */
741static void phy_error(struct phy_device *phydev)
742{
743 WARN_ON(1);
744
745 mutex_lock(&phydev->lock);
746 phydev->state = PHY_HALTED;
747 mutex_unlock(&phydev->lock);
748
749 phy_trigger_machine(phydev);
750}
751
752/**
753 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
754 * @phydev: target phy_device struct
755 */
756static int phy_disable_interrupts(struct phy_device *phydev)
757{
758 int err;
759
760 /* Disable PHY interrupts */
761 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
762 if (err)
763 return err;
764
765 /* Clear the interrupt */
766 return phy_clear_interrupt(phydev);
767}
768
769/**
770 * phy_did_interrupt - Checks if the PHY generated an interrupt
771 * @phydev: target phy_device struct
772 */
773static int phy_did_interrupt(struct phy_device *phydev)
774{
775 int ret;
776
777 mutex_lock(&phydev->lock);
778 ret = phydev->drv->did_interrupt(phydev);
779 mutex_unlock(&phydev->lock);
780
781 return ret;
782}
783
784/**
785 * phy_handle_interrupt - PHY specific interrupt handler
786 * @phydev: target phy_device struct
787 */
788static int phy_handle_interrupt(struct phy_device *phydev)
789{
790 int ret;
791
792 mutex_lock(&phydev->lock);
793 ret = phydev->drv->handle_interrupt(phydev);
794 mutex_unlock(&phydev->lock);
795
796 return ret;
797}
798
799/**
800 * phy_interrupt - PHY interrupt handler
801 * @irq: interrupt line
802 * @phy_dat: phy_device pointer
803 *
804 * Description: Handle PHY interrupt
805 */
806static irqreturn_t phy_interrupt(int irq, void *phy_dat)
807{
808 struct phy_device *phydev = phy_dat;
809
810 if (phydev->drv->did_interrupt && !phy_did_interrupt(phydev))
811 return IRQ_NONE;
812
813 if (phydev->drv->handle_interrupt) {
814 if (phy_handle_interrupt(phydev))
815 goto phy_err;
816 } else {
817 /* reschedule state queue work to run as soon as possible */
818 phy_trigger_machine(phydev);
819 }
820
821 /* did_interrupt() may have cleared the interrupt already */
822 if (!phydev->drv->did_interrupt && phy_clear_interrupt(phydev))
823 goto phy_err;
824 return IRQ_HANDLED;
825
826phy_err:
827 phy_error(phydev);
828 return IRQ_NONE;
829}
830
831/**
832 * phy_enable_interrupts - Enable the interrupts from the PHY side
833 * @phydev: target phy_device struct
834 */
835static int phy_enable_interrupts(struct phy_device *phydev)
836{
837 int err = phy_clear_interrupt(phydev);
838
839 if (err < 0)
840 return err;
841
842 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
843}
844
845/**
846 * phy_request_interrupt - request and enable interrupt for a PHY device
847 * @phydev: target phy_device struct
848 *
849 * Description: Request and enable the interrupt for the given PHY.
850 * If this fails, then we set irq to PHY_POLL.
851 * This should only be called with a valid IRQ number.
852 */
853void phy_request_interrupt(struct phy_device *phydev)
854{
855 int err;
856
857 err = request_threaded_irq(phydev->irq, NULL, phy_interrupt,
858 IRQF_ONESHOT | IRQF_SHARED,
859 phydev_name(phydev), phydev);
860 if (err) {
861 phydev_warn(phydev, "Error %d requesting IRQ %d, falling back to polling\n",
862 err, phydev->irq);
863 phydev->irq = PHY_POLL;
864 } else {
865 if (phy_enable_interrupts(phydev)) {
866 phydev_warn(phydev, "Can't enable interrupt, falling back to polling\n");
867 phy_free_interrupt(phydev);
868 phydev->irq = PHY_POLL;
869 }
870 }
871}
872EXPORT_SYMBOL(phy_request_interrupt);
873
874/**
875 * phy_free_interrupt - disable and free interrupt for a PHY device
876 * @phydev: target phy_device struct
877 *
878 * Description: Disable and free the interrupt for the given PHY.
879 * This should only be called with a valid IRQ number.
880 */
881void phy_free_interrupt(struct phy_device *phydev)
882{
883 phy_disable_interrupts(phydev);
884 free_irq(phydev->irq, phydev);
885}
886EXPORT_SYMBOL(phy_free_interrupt);
887
888/**
889 * phy_stop - Bring down the PHY link, and stop checking the status
890 * @phydev: target phy_device struct
891 */
892void phy_stop(struct phy_device *phydev)
893{
894 if (!phy_is_started(phydev) && phydev->state != PHY_DOWN) {
895 WARN(1, "called from state %s\n",
896 phy_state_to_str(phydev->state));
897 return;
898 }
899
900 mutex_lock(&phydev->lock);
901
902 if (phydev->sfp_bus)
903 sfp_upstream_stop(phydev->sfp_bus);
904
905 phydev->state = PHY_HALTED;
906
907 mutex_unlock(&phydev->lock);
908
909 phy_state_machine(&phydev->state_queue.work);
910 phy_stop_machine(phydev);
911
912 /* Cannot call flush_scheduled_work() here as desired because
913 * of rtnl_lock(), but PHY_HALTED shall guarantee irq handler
914 * will not reenable interrupts.
915 */
916}
917EXPORT_SYMBOL(phy_stop);
918
919/**
920 * phy_start - start or restart a PHY device
921 * @phydev: target phy_device struct
922 *
923 * Description: Indicates the attached device's readiness to
924 * handle PHY-related work. Used during startup to start the
925 * PHY, and after a call to phy_stop() to resume operation.
926 * Also used to indicate the MDIO bus has cleared an error
927 * condition.
928 */
929void phy_start(struct phy_device *phydev)
930{
931 mutex_lock(&phydev->lock);
932
933 if (phydev->state != PHY_READY && phydev->state != PHY_HALTED) {
934 WARN(1, "called from state %s\n",
935 phy_state_to_str(phydev->state));
936 goto out;
937 }
938
939 /* if phy was suspended, bring the physical link up again */
940 __phy_resume(phydev);
941
942 phydev->state = PHY_UP;
943
944 phy_start_machine(phydev);
945out:
946 mutex_unlock(&phydev->lock);
947}
948EXPORT_SYMBOL(phy_start);
949
950/**
951 * phy_state_machine - Handle the state machine
952 * @work: work_struct that describes the work to be done
953 */
954void phy_state_machine(struct work_struct *work)
955{
956 struct delayed_work *dwork = to_delayed_work(work);
957 struct phy_device *phydev =
958 container_of(dwork, struct phy_device, state_queue);
959 bool needs_aneg = false, do_suspend = false;
960 enum phy_state old_state;
961 int err = 0;
962
963 mutex_lock(&phydev->lock);
964
965 old_state = phydev->state;
966
967 if (phydev->sfp_bus)
968 sfp_upstream_start(phydev->sfp_bus);
969
970 switch (phydev->state) {
971 case PHY_DOWN:
972 case PHY_READY:
973 break;
974 case PHY_UP:
975 needs_aneg = true;
976
977 break;
978 case PHY_NOLINK:
979 case PHY_RUNNING:
980 err = phy_check_link_status(phydev);
981 break;
982 case PHY_HALTED:
983 if (phydev->link) {
984 phydev->link = 0;
985 phy_link_down(phydev, true);
986 }
987 do_suspend = true;
988 break;
989 }
990
991 mutex_unlock(&phydev->lock);
992
993 if (needs_aneg)
994 err = phy_start_aneg(phydev);
995 else if (do_suspend)
996 phy_suspend(phydev);
997
998 if (err < 0)
999 phy_error(phydev);
1000
1001 if (old_state != phydev->state) {
1002 phydev_dbg(phydev, "PHY state change %s -> %s\n",
1003 phy_state_to_str(old_state),
1004 phy_state_to_str(phydev->state));
1005 if (phydev->drv && phydev->drv->link_change_notify)
1006 phydev->drv->link_change_notify(phydev);
1007 }
1008
1009 /* Only re-schedule a PHY state machine change if we are polling the
1010 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
1011 * between states from phy_mac_interrupt().
1012 *
1013 * In state PHY_HALTED the PHY gets suspended, so rescheduling the
1014 * state machine would be pointless and possibly error prone when
1015 * called from phy_disconnect() synchronously.
1016 */
1017 mutex_lock(&phydev->lock);
1018 if (phy_polling_mode(phydev) && phy_is_started(phydev))
1019 phy_queue_state_machine(phydev, PHY_STATE_TIME);
1020 mutex_unlock(&phydev->lock);
1021}
1022
1023/**
1024 * phy_mac_interrupt - MAC says the link has changed
1025 * @phydev: phy_device struct with changed link
1026 *
1027 * The MAC layer is able to indicate there has been a change in the PHY link
1028 * status. Trigger the state machine and work a work queue.
1029 */
1030void phy_mac_interrupt(struct phy_device *phydev)
1031{
1032 /* Trigger a state machine change */
1033 phy_trigger_machine(phydev);
1034}
1035EXPORT_SYMBOL(phy_mac_interrupt);
1036
1037static void mmd_eee_adv_to_linkmode(unsigned long *advertising, u16 eee_adv)
1038{
1039 linkmode_zero(advertising);
1040
1041 if (eee_adv & MDIO_EEE_100TX)
1042 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
1043 advertising);
1044 if (eee_adv & MDIO_EEE_1000T)
1045 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1046 advertising);
1047 if (eee_adv & MDIO_EEE_10GT)
1048 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
1049 advertising);
1050 if (eee_adv & MDIO_EEE_1000KX)
1051 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
1052 advertising);
1053 if (eee_adv & MDIO_EEE_10GKX4)
1054 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
1055 advertising);
1056 if (eee_adv & MDIO_EEE_10GKR)
1057 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
1058 advertising);
1059}
1060
1061/**
1062 * phy_init_eee - init and check the EEE feature
1063 * @phydev: target phy_device struct
1064 * @clk_stop_enable: PHY may stop the clock during LPI
1065 *
1066 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1067 * is supported by looking at the MMD registers 3.20 and 7.60/61
1068 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1069 * bit if required.
1070 */
1071int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1072{
1073 if (!phydev->drv)
1074 return -EIO;
1075
1076 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1077 */
1078 if (phydev->duplex == DUPLEX_FULL) {
1079 __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
1080 __ETHTOOL_DECLARE_LINK_MODE_MASK(lp);
1081 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv);
1082 int eee_lp, eee_cap, eee_adv;
1083 int status;
1084 u32 cap;
1085
1086 /* Read phy status to properly get the right settings */
1087 status = phy_read_status(phydev);
1088 if (status)
1089 return status;
1090
1091 /* First check if the EEE ability is supported */
1092 eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1093 if (eee_cap <= 0)
1094 goto eee_exit_err;
1095
1096 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
1097 if (!cap)
1098 goto eee_exit_err;
1099
1100 /* Check which link settings negotiated and verify it in
1101 * the EEE advertising registers.
1102 */
1103 eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1104 if (eee_lp <= 0)
1105 goto eee_exit_err;
1106
1107 eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1108 if (eee_adv <= 0)
1109 goto eee_exit_err;
1110
1111 mmd_eee_adv_to_linkmode(adv, eee_adv);
1112 mmd_eee_adv_to_linkmode(lp, eee_lp);
1113 linkmode_and(common, adv, lp);
1114
1115 if (!phy_check_valid(phydev->speed, phydev->duplex, common))
1116 goto eee_exit_err;
1117
1118 if (clk_stop_enable)
1119 /* Configure the PHY to stop receiving xMII
1120 * clock while it is signaling LPI.
1121 */
1122 phy_set_bits_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1,
1123 MDIO_PCS_CTRL1_CLKSTOP_EN);
1124
1125 return 0; /* EEE supported */
1126 }
1127eee_exit_err:
1128 return -EPROTONOSUPPORT;
1129}
1130EXPORT_SYMBOL(phy_init_eee);
1131
1132/**
1133 * phy_get_eee_err - report the EEE wake error count
1134 * @phydev: target phy_device struct
1135 *
1136 * Description: it is to report the number of time where the PHY
1137 * failed to complete its normal wake sequence.
1138 */
1139int phy_get_eee_err(struct phy_device *phydev)
1140{
1141 if (!phydev->drv)
1142 return -EIO;
1143
1144 return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
1145}
1146EXPORT_SYMBOL(phy_get_eee_err);
1147
1148/**
1149 * phy_ethtool_get_eee - get EEE supported and status
1150 * @phydev: target phy_device struct
1151 * @data: ethtool_eee data
1152 *
1153 * Description: it reportes the Supported/Advertisement/LP Advertisement
1154 * capabilities.
1155 */
1156int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1157{
1158 int val;
1159
1160 if (!phydev->drv)
1161 return -EIO;
1162
1163 /* Get Supported EEE */
1164 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1165 if (val < 0)
1166 return val;
1167 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1168
1169 /* Get advertisement EEE */
1170 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1171 if (val < 0)
1172 return val;
1173 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1174 data->eee_enabled = !!data->advertised;
1175
1176 /* Get LP advertisement EEE */
1177 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1178 if (val < 0)
1179 return val;
1180 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1181
1182 data->eee_active = !!(data->advertised & data->lp_advertised);
1183
1184 return 0;
1185}
1186EXPORT_SYMBOL(phy_ethtool_get_eee);
1187
1188/**
1189 * phy_ethtool_set_eee - set EEE supported and status
1190 * @phydev: target phy_device struct
1191 * @data: ethtool_eee data
1192 *
1193 * Description: it is to program the Advertisement EEE register.
1194 */
1195int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1196{
1197 int cap, old_adv, adv = 0, ret;
1198
1199 if (!phydev->drv)
1200 return -EIO;
1201
1202 /* Get Supported EEE */
1203 cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1204 if (cap < 0)
1205 return cap;
1206
1207 old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1208 if (old_adv < 0)
1209 return old_adv;
1210
1211 if (data->eee_enabled) {
1212 adv = !data->advertised ? cap :
1213 ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
1214 /* Mask prohibited EEE modes */
1215 adv &= ~phydev->eee_broken_modes;
1216 }
1217
1218 if (old_adv != adv) {
1219 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1220 if (ret < 0)
1221 return ret;
1222
1223 /* Restart autonegotiation so the new modes get sent to the
1224 * link partner.
1225 */
1226 if (phydev->autoneg == AUTONEG_ENABLE) {
1227 ret = phy_restart_aneg(phydev);
1228 if (ret < 0)
1229 return ret;
1230 }
1231 }
1232
1233 return 0;
1234}
1235EXPORT_SYMBOL(phy_ethtool_set_eee);
1236
1237int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1238{
1239 if (phydev->drv && phydev->drv->set_wol)
1240 return phydev->drv->set_wol(phydev, wol);
1241
1242 return -EOPNOTSUPP;
1243}
1244EXPORT_SYMBOL(phy_ethtool_set_wol);
1245
1246void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1247{
1248 if (phydev->drv && phydev->drv->get_wol)
1249 phydev->drv->get_wol(phydev, wol);
1250}
1251EXPORT_SYMBOL(phy_ethtool_get_wol);
1252
1253int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1254 struct ethtool_link_ksettings *cmd)
1255{
1256 struct phy_device *phydev = ndev->phydev;
1257
1258 if (!phydev)
1259 return -ENODEV;
1260
1261 phy_ethtool_ksettings_get(phydev, cmd);
1262
1263 return 0;
1264}
1265EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1266
1267int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1268 const struct ethtool_link_ksettings *cmd)
1269{
1270 struct phy_device *phydev = ndev->phydev;
1271
1272 if (!phydev)
1273 return -ENODEV;
1274
1275 return phy_ethtool_ksettings_set(phydev, cmd);
1276}
1277EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
1278
1279int phy_ethtool_nway_reset(struct net_device *ndev)
1280{
1281 struct phy_device *phydev = ndev->phydev;
1282
1283 if (!phydev)
1284 return -ENODEV;
1285
1286 if (!phydev->drv)
1287 return -EIO;
1288
1289 return phy_restart_aneg(phydev);
1290}
1291EXPORT_SYMBOL(phy_ethtool_nway_reset);