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