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