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