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