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