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