blob: db7866b6f75259e7de9fd07c0490ccc69e211336 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0+
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/* 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009 */
10
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000011#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>
Olivier Deprez157378f2022-04-04 15:47:50 +020018#include <linux/netlink.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000019#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>
Olivier Deprez157378f2022-04-04 15:47:50 +020025#include <linux/ethtool_netlink.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000026#include <linux/phy.h>
27#include <linux/phy_led_triggers.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020028#include <linux/sfp.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000029#include <linux/workqueue.h>
30#include <linux/mdio.h>
31#include <linux/io.h>
32#include <linux/uaccess.h>
33#include <linux/atomic.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020034#include <net/netlink.h>
35#include <net/genetlink.h>
36#include <net/sock.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000037
David Brazdil0f672f62019-12-10 10:32:29 +000038#define PHY_STATE_TIME HZ
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000039
40#define PHY_STATE_STR(_state) \
41 case PHY_##_state: \
42 return __stringify(_state); \
43
44static const char *phy_state_to_str(enum phy_state st)
45{
46 switch (st) {
47 PHY_STATE_STR(DOWN)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000048 PHY_STATE_STR(READY)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000049 PHY_STATE_STR(UP)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000050 PHY_STATE_STR(RUNNING)
51 PHY_STATE_STR(NOLINK)
Olivier Deprez157378f2022-04-04 15:47:50 +020052 PHY_STATE_STR(CABLETEST)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000053 PHY_STATE_STR(HALTED)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000054 }
55
56 return NULL;
57}
58
David Brazdil0f672f62019-12-10 10:32:29 +000059static void phy_link_up(struct phy_device *phydev)
60{
Olivier Deprez157378f2022-04-04 15:47:50 +020061 phydev->phy_link_change(phydev, true);
David Brazdil0f672f62019-12-10 10:32:29 +000062 phy_led_trigger_change_speed(phydev);
63}
64
Olivier Deprez157378f2022-04-04 15:47:50 +020065static void phy_link_down(struct phy_device *phydev)
David Brazdil0f672f62019-12-10 10:32:29 +000066{
Olivier Deprez157378f2022-04-04 15:47:50 +020067 phydev->phy_link_change(phydev, false);
David Brazdil0f672f62019-12-10 10:32:29 +000068 phy_led_trigger_change_speed(phydev);
69}
70
71static const char *phy_pause_str(struct phy_device *phydev)
72{
73 bool local_pause, local_asym_pause;
74
75 if (phydev->autoneg == AUTONEG_DISABLE)
76 goto no_pause;
77
78 local_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
79 phydev->advertising);
80 local_asym_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
81 phydev->advertising);
82
83 if (local_pause && phydev->pause)
84 return "rx/tx";
85
86 if (local_asym_pause && phydev->asym_pause) {
87 if (local_pause)
88 return "rx";
89 if (phydev->pause)
90 return "tx";
91 }
92
93no_pause:
94 return "off";
95}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000096
97/**
98 * phy_print_status - Convenience function to print out the current phy status
99 * @phydev: the phy_device struct
100 */
101void phy_print_status(struct phy_device *phydev)
102{
103 if (phydev->link) {
104 netdev_info(phydev->attached_dev,
Olivier Deprez157378f2022-04-04 15:47:50 +0200105 "Link is Up - %s/%s %s- flow control %s\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000106 phy_speed_to_str(phydev->speed),
107 phy_duplex_to_str(phydev->duplex),
Olivier Deprez157378f2022-04-04 15:47:50 +0200108 phydev->downshifted_rate ? "(downshifted) " : "",
David Brazdil0f672f62019-12-10 10:32:29 +0000109 phy_pause_str(phydev));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000110 } else {
111 netdev_info(phydev->attached_dev, "Link is Down\n");
112 }
113}
114EXPORT_SYMBOL(phy_print_status);
115
116/**
117 * phy_clear_interrupt - Ack the phy device's interrupt
118 * @phydev: the phy_device struct
119 *
120 * If the @phydev driver has an ack_interrupt function, call it to
121 * ack and clear the phy device's interrupt.
122 *
123 * Returns 0 on success or < 0 on error.
124 */
125static int phy_clear_interrupt(struct phy_device *phydev)
126{
127 if (phydev->drv->ack_interrupt)
128 return phydev->drv->ack_interrupt(phydev);
129
130 return 0;
131}
132
133/**
134 * phy_config_interrupt - configure the PHY device for the requested interrupts
135 * @phydev: the phy_device struct
136 * @interrupts: interrupt flags to configure for this @phydev
137 *
138 * Returns 0 on success or < 0 on error.
139 */
David Brazdil0f672f62019-12-10 10:32:29 +0000140static int phy_config_interrupt(struct phy_device *phydev, bool interrupts)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000141{
David Brazdil0f672f62019-12-10 10:32:29 +0000142 phydev->interrupts = interrupts ? 1 : 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000143 if (phydev->drv->config_intr)
144 return phydev->drv->config_intr(phydev);
145
146 return 0;
147}
148
149/**
150 * phy_restart_aneg - restart auto-negotiation
151 * @phydev: target phy_device struct
152 *
153 * Restart the autonegotiation on @phydev. Returns >= 0 on success or
154 * negative errno on error.
155 */
156int phy_restart_aneg(struct phy_device *phydev)
157{
158 int ret;
159
160 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
161 ret = genphy_c45_restart_aneg(phydev);
162 else
163 ret = genphy_restart_aneg(phydev);
164
165 return ret;
166}
167EXPORT_SYMBOL_GPL(phy_restart_aneg);
168
169/**
170 * phy_aneg_done - return auto-negotiation status
171 * @phydev: target phy_device struct
172 *
173 * Description: Return the auto-negotiation status from this @phydev
174 * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
175 * is still pending.
176 */
177int phy_aneg_done(struct phy_device *phydev)
178{
179 if (phydev->drv && phydev->drv->aneg_done)
180 return phydev->drv->aneg_done(phydev);
David Brazdil0f672f62019-12-10 10:32:29 +0000181 else if (phydev->is_c45)
182 return genphy_c45_aneg_done(phydev);
183 else
184 return genphy_aneg_done(phydev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000185}
186EXPORT_SYMBOL(phy_aneg_done);
187
188/**
189 * phy_find_valid - find a PHY setting that matches the requested parameters
190 * @speed: desired speed
191 * @duplex: desired duplex
192 * @supported: mask of supported link modes
193 *
194 * Locate a supported phy setting that is, in priority order:
195 * - an exact match for the specified speed and duplex mode
196 * - a match for the specified speed, or slower speed
197 * - the slowest supported speed
198 * Returns the matched phy_setting entry, or %NULL if no supported phy
199 * settings were found.
200 */
201static const struct phy_setting *
David Brazdil0f672f62019-12-10 10:32:29 +0000202phy_find_valid(int speed, int duplex, unsigned long *supported)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000203{
David Brazdil0f672f62019-12-10 10:32:29 +0000204 return phy_lookup_setting(speed, duplex, supported, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000205}
206
207/**
208 * phy_supported_speeds - return all speeds currently supported by a phy device
209 * @phy: The phy device to return supported speeds of.
210 * @speeds: buffer to store supported speeds in.
211 * @size: size of speeds buffer.
212 *
213 * Description: Returns the number of supported speeds, and fills the speeds
214 * buffer with the supported speeds. If speeds buffer is too small to contain
215 * all currently supported speeds, will return as many speeds as can fit.
216 */
217unsigned int phy_supported_speeds(struct phy_device *phy,
218 unsigned int *speeds,
219 unsigned int size)
220{
David Brazdil0f672f62019-12-10 10:32:29 +0000221 return phy_speeds(speeds, size, phy->supported);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000222}
223
224/**
225 * phy_check_valid - check if there is a valid PHY setting which matches
226 * speed, duplex, and feature mask
227 * @speed: speed to match
228 * @duplex: duplex to match
229 * @features: A mask of the valid settings
230 *
231 * Description: Returns true if there is a valid setting, false otherwise.
232 */
David Brazdil0f672f62019-12-10 10:32:29 +0000233static inline bool phy_check_valid(int speed, int duplex,
234 unsigned long *features)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000235{
David Brazdil0f672f62019-12-10 10:32:29 +0000236 return !!phy_lookup_setting(speed, duplex, features, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000237}
238
239/**
240 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
241 * @phydev: the target phy_device struct
242 *
243 * Description: Make sure the PHY is set to supported speeds and
244 * duplexes. Drop down by one in this order: 1000/FULL,
245 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
246 */
247static void phy_sanitize_settings(struct phy_device *phydev)
248{
249 const struct phy_setting *setting;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000250
David Brazdil0f672f62019-12-10 10:32:29 +0000251 setting = phy_find_valid(phydev->speed, phydev->duplex,
252 phydev->supported);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000253 if (setting) {
254 phydev->speed = setting->speed;
255 phydev->duplex = setting->duplex;
256 } else {
257 /* We failed to find anything (no supported speeds?) */
258 phydev->speed = SPEED_UNKNOWN;
259 phydev->duplex = DUPLEX_UNKNOWN;
260 }
261}
262
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000263void phy_ethtool_ksettings_get(struct phy_device *phydev,
264 struct ethtool_link_ksettings *cmd)
265{
Olivier Deprez157378f2022-04-04 15:47:50 +0200266 mutex_lock(&phydev->lock);
David Brazdil0f672f62019-12-10 10:32:29 +0000267 linkmode_copy(cmd->link_modes.supported, phydev->supported);
268 linkmode_copy(cmd->link_modes.advertising, phydev->advertising);
269 linkmode_copy(cmd->link_modes.lp_advertising, phydev->lp_advertising);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000270
271 cmd->base.speed = phydev->speed;
272 cmd->base.duplex = phydev->duplex;
Olivier Deprez157378f2022-04-04 15:47:50 +0200273 cmd->base.master_slave_cfg = phydev->master_slave_get;
274 cmd->base.master_slave_state = phydev->master_slave_state;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000275 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
276 cmd->base.port = PORT_BNC;
277 else
Olivier Deprez157378f2022-04-04 15:47:50 +0200278 cmd->base.port = phydev->port;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000279 cmd->base.transceiver = phy_is_internal(phydev) ?
280 XCVR_INTERNAL : XCVR_EXTERNAL;
281 cmd->base.phy_address = phydev->mdio.addr;
282 cmd->base.autoneg = phydev->autoneg;
283 cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
284 cmd->base.eth_tp_mdix = phydev->mdix;
Olivier Deprez157378f2022-04-04 15:47:50 +0200285 mutex_unlock(&phydev->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000286}
287EXPORT_SYMBOL(phy_ethtool_ksettings_get);
288
289/**
290 * phy_mii_ioctl - generic PHY MII ioctl interface
291 * @phydev: the phy_device struct
292 * @ifr: &struct ifreq for socket ioctl's
293 * @cmd: ioctl cmd to execute
294 *
295 * Note that this function is currently incompatible with the
296 * PHYCONTROL layer. It changes registers without regard to
297 * current state. Use at own risk.
298 */
299int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
300{
301 struct mii_ioctl_data *mii_data = if_mii(ifr);
302 u16 val = mii_data->val_in;
303 bool change_autoneg = false;
David Brazdil0f672f62019-12-10 10:32:29 +0000304 int prtad, devad;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000305
306 switch (cmd) {
307 case SIOCGMIIPHY:
308 mii_data->phy_id = phydev->mdio.addr;
Olivier Deprez157378f2022-04-04 15:47:50 +0200309 fallthrough;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000310
311 case SIOCGMIIREG:
David Brazdil0f672f62019-12-10 10:32:29 +0000312 if (mdio_phy_id_is_c45(mii_data->phy_id)) {
313 prtad = mdio_phy_id_prtad(mii_data->phy_id);
314 devad = mdio_phy_id_devad(mii_data->phy_id);
Olivier Deprez157378f2022-04-04 15:47:50 +0200315 devad = mdiobus_c45_addr(devad, mii_data->reg_num);
David Brazdil0f672f62019-12-10 10:32:29 +0000316 } else {
317 prtad = mii_data->phy_id;
318 devad = mii_data->reg_num;
319 }
320 mii_data->val_out = mdiobus_read(phydev->mdio.bus, prtad,
321 devad);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000322 return 0;
323
324 case SIOCSMIIREG:
David Brazdil0f672f62019-12-10 10:32:29 +0000325 if (mdio_phy_id_is_c45(mii_data->phy_id)) {
326 prtad = mdio_phy_id_prtad(mii_data->phy_id);
327 devad = mdio_phy_id_devad(mii_data->phy_id);
Olivier Deprez157378f2022-04-04 15:47:50 +0200328 devad = mdiobus_c45_addr(devad, mii_data->reg_num);
David Brazdil0f672f62019-12-10 10:32:29 +0000329 } else {
330 prtad = mii_data->phy_id;
331 devad = mii_data->reg_num;
332 }
333 if (prtad == phydev->mdio.addr) {
334 switch (devad) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000335 case MII_BMCR:
336 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
337 if (phydev->autoneg == AUTONEG_ENABLE)
338 change_autoneg = true;
339 phydev->autoneg = AUTONEG_DISABLE;
340 if (val & BMCR_FULLDPLX)
341 phydev->duplex = DUPLEX_FULL;
342 else
343 phydev->duplex = DUPLEX_HALF;
344 if (val & BMCR_SPEED1000)
345 phydev->speed = SPEED_1000;
346 else if (val & BMCR_SPEED100)
347 phydev->speed = SPEED_100;
348 else phydev->speed = SPEED_10;
349 }
350 else {
351 if (phydev->autoneg == AUTONEG_DISABLE)
352 change_autoneg = true;
353 phydev->autoneg = AUTONEG_ENABLE;
354 }
355 break;
356 case MII_ADVERTISE:
David Brazdil0f672f62019-12-10 10:32:29 +0000357 mii_adv_mod_linkmode_adv_t(phydev->advertising,
358 val);
359 change_autoneg = true;
360 break;
361 case MII_CTRL1000:
362 mii_ctrl1000_mod_linkmode_adv_t(phydev->advertising,
363 val);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000364 change_autoneg = true;
365 break;
366 default:
367 /* do nothing */
368 break;
369 }
370 }
371
David Brazdil0f672f62019-12-10 10:32:29 +0000372 mdiobus_write(phydev->mdio.bus, prtad, devad, val);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000373
David Brazdil0f672f62019-12-10 10:32:29 +0000374 if (prtad == phydev->mdio.addr &&
375 devad == MII_BMCR &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000376 val & BMCR_RESET)
377 return phy_init_hw(phydev);
378
379 if (change_autoneg)
380 return phy_start_aneg(phydev);
381
382 return 0;
383
384 case SIOCSHWTSTAMP:
Olivier Deprez157378f2022-04-04 15:47:50 +0200385 if (phydev->mii_ts && phydev->mii_ts->hwtstamp)
386 return phydev->mii_ts->hwtstamp(phydev->mii_ts, ifr);
387 fallthrough;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000388
389 default:
390 return -EOPNOTSUPP;
391 }
392}
393EXPORT_SYMBOL(phy_mii_ioctl);
394
Olivier Deprez157378f2022-04-04 15:47:50 +0200395/**
396 * phy_do_ioctl - generic ndo_do_ioctl implementation
397 * @dev: the net_device struct
398 * @ifr: &struct ifreq for socket ioctl's
399 * @cmd: ioctl cmd to execute
400 */
401int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
402{
403 if (!dev->phydev)
404 return -ENODEV;
405
406 return phy_mii_ioctl(dev->phydev, ifr, cmd);
407}
408EXPORT_SYMBOL(phy_do_ioctl);
409
410/**
411 * phy_do_ioctl_running - generic ndo_do_ioctl implementation but test first
412 *
413 * @dev: the net_device struct
414 * @ifr: &struct ifreq for socket ioctl's
415 * @cmd: ioctl cmd to execute
416 *
417 * Same as phy_do_ioctl, but ensures that net_device is running before
418 * handling the ioctl.
419 */
420int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd)
421{
422 if (!netif_running(dev))
423 return -ENODEV;
424
425 return phy_do_ioctl(dev, ifr, cmd);
426}
427EXPORT_SYMBOL(phy_do_ioctl_running);
428
429/**
430 * phy_queue_state_machine - Trigger the state machine to run soon
431 *
432 * @phydev: the phy_device struct
433 * @jiffies: Run the state machine after these jiffies
434 */
David Brazdil0f672f62019-12-10 10:32:29 +0000435void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies)
436{
437 mod_delayed_work(system_power_efficient_wq, &phydev->state_queue,
438 jiffies);
439}
440EXPORT_SYMBOL(phy_queue_state_machine);
441
Olivier Deprez157378f2022-04-04 15:47:50 +0200442/**
443 * phy_queue_state_machine - Trigger the state machine to run now
444 *
445 * @phydev: the phy_device struct
446 */
David Brazdil0f672f62019-12-10 10:32:29 +0000447static void phy_trigger_machine(struct phy_device *phydev)
448{
449 phy_queue_state_machine(phydev, 0);
450}
451
Olivier Deprez157378f2022-04-04 15:47:50 +0200452static void phy_abort_cable_test(struct phy_device *phydev)
453{
454 int err;
455
456 ethnl_cable_test_finished(phydev);
457
458 err = phy_init_hw(phydev);
459 if (err)
460 phydev_err(phydev, "Error while aborting cable test");
461}
462
463/**
464 * phy_ethtool_get_strings - Get the statistic counter names
465 *
466 * @phydev: the phy_device struct
467 * @data: Where to put the strings
468 */
469int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data)
470{
471 if (!phydev->drv)
472 return -EIO;
473
474 mutex_lock(&phydev->lock);
475 phydev->drv->get_strings(phydev, data);
476 mutex_unlock(&phydev->lock);
477
478 return 0;
479}
480EXPORT_SYMBOL(phy_ethtool_get_strings);
481
482/**
483 * phy_ethtool_get_sset_count - Get the number of statistic counters
484 *
485 * @phydev: the phy_device struct
486 */
487int phy_ethtool_get_sset_count(struct phy_device *phydev)
488{
489 int ret;
490
491 if (!phydev->drv)
492 return -EIO;
493
494 if (phydev->drv->get_sset_count &&
495 phydev->drv->get_strings &&
496 phydev->drv->get_stats) {
497 mutex_lock(&phydev->lock);
498 ret = phydev->drv->get_sset_count(phydev);
499 mutex_unlock(&phydev->lock);
500
501 return ret;
502 }
503
504 return -EOPNOTSUPP;
505}
506EXPORT_SYMBOL(phy_ethtool_get_sset_count);
507
508/**
509 * phy_ethtool_get_stats - Get the statistic counters
510 *
511 * @phydev: the phy_device struct
512 * @stats: What counters to get
513 * @data: Where to store the counters
514 */
515int phy_ethtool_get_stats(struct phy_device *phydev,
516 struct ethtool_stats *stats, u64 *data)
517{
518 if (!phydev->drv)
519 return -EIO;
520
521 mutex_lock(&phydev->lock);
522 phydev->drv->get_stats(phydev, stats, data);
523 mutex_unlock(&phydev->lock);
524
525 return 0;
526}
527EXPORT_SYMBOL(phy_ethtool_get_stats);
528
529/**
530 * phy_start_cable_test - Start a cable test
531 *
532 * @phydev: the phy_device struct
533 * @extack: extack for reporting useful error messages
534 */
535int phy_start_cable_test(struct phy_device *phydev,
536 struct netlink_ext_ack *extack)
537{
538 struct net_device *dev = phydev->attached_dev;
539 int err = -ENOMEM;
540
541 if (!(phydev->drv &&
542 phydev->drv->cable_test_start &&
543 phydev->drv->cable_test_get_status)) {
544 NL_SET_ERR_MSG(extack,
545 "PHY driver does not support cable testing");
546 return -EOPNOTSUPP;
547 }
548
549 mutex_lock(&phydev->lock);
550 if (phydev->state == PHY_CABLETEST) {
551 NL_SET_ERR_MSG(extack,
552 "PHY already performing a test");
553 err = -EBUSY;
554 goto out;
555 }
556
557 if (phydev->state < PHY_UP ||
558 phydev->state > PHY_CABLETEST) {
559 NL_SET_ERR_MSG(extack,
560 "PHY not configured. Try setting interface up");
561 err = -EBUSY;
562 goto out;
563 }
564
565 err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_NTF);
566 if (err)
567 goto out;
568
569 /* Mark the carrier down until the test is complete */
570 phy_link_down(phydev);
571
572 netif_testing_on(dev);
573 err = phydev->drv->cable_test_start(phydev);
574 if (err) {
575 netif_testing_off(dev);
576 phy_link_up(phydev);
577 goto out_free;
578 }
579
580 phydev->state = PHY_CABLETEST;
581
582 if (phy_polling_mode(phydev))
583 phy_trigger_machine(phydev);
584
585 mutex_unlock(&phydev->lock);
586
587 return 0;
588
589out_free:
590 ethnl_cable_test_free(phydev);
591out:
592 mutex_unlock(&phydev->lock);
593
594 return err;
595}
596EXPORT_SYMBOL(phy_start_cable_test);
597
598/**
599 * phy_start_cable_test_tdr - Start a raw TDR cable test
600 *
601 * @phydev: the phy_device struct
602 * @extack: extack for reporting useful error messages
603 * @config: Configuration of the test to run
604 */
605int phy_start_cable_test_tdr(struct phy_device *phydev,
606 struct netlink_ext_ack *extack,
607 const struct phy_tdr_config *config)
608{
609 struct net_device *dev = phydev->attached_dev;
610 int err = -ENOMEM;
611
612 if (!(phydev->drv &&
613 phydev->drv->cable_test_tdr_start &&
614 phydev->drv->cable_test_get_status)) {
615 NL_SET_ERR_MSG(extack,
616 "PHY driver does not support cable test TDR");
617 return -EOPNOTSUPP;
618 }
619
620 mutex_lock(&phydev->lock);
621 if (phydev->state == PHY_CABLETEST) {
622 NL_SET_ERR_MSG(extack,
623 "PHY already performing a test");
624 err = -EBUSY;
625 goto out;
626 }
627
628 if (phydev->state < PHY_UP ||
629 phydev->state > PHY_CABLETEST) {
630 NL_SET_ERR_MSG(extack,
631 "PHY not configured. Try setting interface up");
632 err = -EBUSY;
633 goto out;
634 }
635
636 err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_TDR_NTF);
637 if (err)
638 goto out;
639
640 /* Mark the carrier down until the test is complete */
641 phy_link_down(phydev);
642
643 netif_testing_on(dev);
644 err = phydev->drv->cable_test_tdr_start(phydev, config);
645 if (err) {
646 netif_testing_off(dev);
647 phy_link_up(phydev);
648 goto out_free;
649 }
650
651 phydev->state = PHY_CABLETEST;
652
653 if (phy_polling_mode(phydev))
654 phy_trigger_machine(phydev);
655
656 mutex_unlock(&phydev->lock);
657
658 return 0;
659
660out_free:
661 ethnl_cable_test_free(phydev);
662out:
663 mutex_unlock(&phydev->lock);
664
665 return err;
666}
667EXPORT_SYMBOL(phy_start_cable_test_tdr);
668
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000669static int phy_config_aneg(struct phy_device *phydev)
670{
671 if (phydev->drv->config_aneg)
672 return phydev->drv->config_aneg(phydev);
673
674 /* Clause 45 PHYs that don't implement Clause 22 registers are not
675 * allowed to call genphy_config_aneg()
676 */
677 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
David Brazdil0f672f62019-12-10 10:32:29 +0000678 return genphy_c45_config_aneg(phydev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000679
680 return genphy_config_aneg(phydev);
681}
682
683/**
David Brazdil0f672f62019-12-10 10:32:29 +0000684 * phy_check_link_status - check link status and set state accordingly
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000685 * @phydev: the phy_device struct
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000686 *
David Brazdil0f672f62019-12-10 10:32:29 +0000687 * Description: Check for link and whether autoneg was triggered / is running
688 * and set state accordingly
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000689 */
David Brazdil0f672f62019-12-10 10:32:29 +0000690static int phy_check_link_status(struct phy_device *phydev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000691{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000692 int err;
693
David Brazdil0f672f62019-12-10 10:32:29 +0000694 WARN_ON(!mutex_is_locked(&phydev->lock));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000695
David Brazdil0f672f62019-12-10 10:32:29 +0000696 /* Keep previous state if loopback is enabled because some PHYs
697 * report that Link is Down when loopback is enabled.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000698 */
David Brazdil0f672f62019-12-10 10:32:29 +0000699 if (phydev->loopback_enabled)
700 return 0;
701
702 err = phy_read_status(phydev);
703 if (err)
704 return err;
705
706 if (phydev->link && phydev->state != PHY_RUNNING) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200707 phy_check_downshift(phydev);
David Brazdil0f672f62019-12-10 10:32:29 +0000708 phydev->state = PHY_RUNNING;
709 phy_link_up(phydev);
710 } else if (!phydev->link && phydev->state != PHY_NOLINK) {
711 phydev->state = PHY_NOLINK;
Olivier Deprez157378f2022-04-04 15:47:50 +0200712 phy_link_down(phydev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000713 }
714
David Brazdil0f672f62019-12-10 10:32:29 +0000715 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000716}
717
718/**
Olivier Deprez157378f2022-04-04 15:47:50 +0200719 * _phy_start_aneg - start auto-negotiation for this PHY device
720 * @phydev: the phy_device struct
721 *
722 * Description: Sanitizes the settings (if we're not autonegotiating
723 * them), and then calls the driver's config_aneg function.
724 * If the PHYCONTROL Layer is operating, we change the state to
725 * reflect the beginning of Auto-negotiation or forcing.
726 */
727static int _phy_start_aneg(struct phy_device *phydev)
728{
729 int err;
730
731 lockdep_assert_held(&phydev->lock);
732
733 if (!phydev->drv)
734 return -EIO;
735
736 if (AUTONEG_DISABLE == phydev->autoneg)
737 phy_sanitize_settings(phydev);
738
739 err = phy_config_aneg(phydev);
740 if (err < 0)
741 return err;
742
743 if (phy_is_started(phydev))
744 err = phy_check_link_status(phydev);
745
746 return err;
747}
748
749/**
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000750 * phy_start_aneg - start auto-negotiation for this PHY device
751 * @phydev: the phy_device struct
752 *
753 * Description: Sanitizes the settings (if we're not autonegotiating
754 * them), and then calls the driver's config_aneg function.
755 * If the PHYCONTROL Layer is operating, we change the state to
756 * reflect the beginning of Auto-negotiation or forcing.
757 */
758int phy_start_aneg(struct phy_device *phydev)
759{
David Brazdil0f672f62019-12-10 10:32:29 +0000760 int err;
761
David Brazdil0f672f62019-12-10 10:32:29 +0000762 mutex_lock(&phydev->lock);
Olivier Deprez157378f2022-04-04 15:47:50 +0200763 err = _phy_start_aneg(phydev);
David Brazdil0f672f62019-12-10 10:32:29 +0000764 mutex_unlock(&phydev->lock);
765
766 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000767}
768EXPORT_SYMBOL(phy_start_aneg);
769
770static int phy_poll_aneg_done(struct phy_device *phydev)
771{
772 unsigned int retries = 100;
773 int ret;
774
775 do {
776 msleep(100);
777 ret = phy_aneg_done(phydev);
778 } while (!ret && --retries);
779
780 if (!ret)
781 return -ETIMEDOUT;
782
783 return ret < 0 ? ret : 0;
784}
785
Olivier Deprez157378f2022-04-04 15:47:50 +0200786int phy_ethtool_ksettings_set(struct phy_device *phydev,
787 const struct ethtool_link_ksettings *cmd)
788{
789 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
790 u8 autoneg = cmd->base.autoneg;
791 u8 duplex = cmd->base.duplex;
792 u32 speed = cmd->base.speed;
793
794 if (cmd->base.phy_address != phydev->mdio.addr)
795 return -EINVAL;
796
797 linkmode_copy(advertising, cmd->link_modes.advertising);
798
799 /* We make sure that we don't pass unsupported values in to the PHY */
800 linkmode_and(advertising, advertising, phydev->supported);
801
802 /* Verify the settings we care about. */
803 if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
804 return -EINVAL;
805
806 if (autoneg == AUTONEG_ENABLE && linkmode_empty(advertising))
807 return -EINVAL;
808
809 if (autoneg == AUTONEG_DISABLE &&
810 ((speed != SPEED_1000 &&
811 speed != SPEED_100 &&
812 speed != SPEED_10) ||
813 (duplex != DUPLEX_HALF &&
814 duplex != DUPLEX_FULL)))
815 return -EINVAL;
816
817 mutex_lock(&phydev->lock);
818 phydev->autoneg = autoneg;
819
820 if (autoneg == AUTONEG_DISABLE) {
821 phydev->speed = speed;
822 phydev->duplex = duplex;
823 }
824
825 linkmode_copy(phydev->advertising, advertising);
826
827 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
828 phydev->advertising, autoneg == AUTONEG_ENABLE);
829
830 phydev->master_slave_set = cmd->base.master_slave_cfg;
831 phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
832
833 /* Restart the PHY */
834 if (phy_is_started(phydev)) {
835 phydev->state = PHY_UP;
836 phy_trigger_machine(phydev);
837 } else {
838 _phy_start_aneg(phydev);
839 }
840
841 mutex_unlock(&phydev->lock);
842 return 0;
843}
844EXPORT_SYMBOL(phy_ethtool_ksettings_set);
845
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000846/**
847 * phy_speed_down - set speed to lowest speed supported by both link partners
848 * @phydev: the phy_device struct
849 * @sync: perform action synchronously
850 *
851 * Description: Typically used to save energy when waiting for a WoL packet
852 *
853 * WARNING: Setting sync to false may cause the system being unable to suspend
854 * in case the PHY generates an interrupt when finishing the autonegotiation.
855 * This interrupt may wake up the system immediately after suspend.
856 * Therefore use sync = false only if you're sure it's safe with the respective
857 * network chip.
858 */
859int phy_speed_down(struct phy_device *phydev, bool sync)
860{
David Brazdil0f672f62019-12-10 10:32:29 +0000861 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000862 int ret;
863
864 if (phydev->autoneg != AUTONEG_ENABLE)
865 return 0;
866
David Brazdil0f672f62019-12-10 10:32:29 +0000867 linkmode_copy(adv_tmp, phydev->advertising);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000868
David Brazdil0f672f62019-12-10 10:32:29 +0000869 ret = phy_speed_down_core(phydev);
870 if (ret)
871 return ret;
872
873 linkmode_copy(phydev->adv_old, adv_tmp);
874
875 if (linkmode_equal(phydev->advertising, adv_tmp))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000876 return 0;
877
878 ret = phy_config_aneg(phydev);
879 if (ret)
880 return ret;
881
882 return sync ? phy_poll_aneg_done(phydev) : 0;
883}
884EXPORT_SYMBOL_GPL(phy_speed_down);
885
886/**
887 * phy_speed_up - (re)set advertised speeds to all supported speeds
888 * @phydev: the phy_device struct
889 *
890 * Description: Used to revert the effect of phy_speed_down
891 */
892int phy_speed_up(struct phy_device *phydev)
893{
David Brazdil0f672f62019-12-10 10:32:29 +0000894 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000895
896 if (phydev->autoneg != AUTONEG_ENABLE)
897 return 0;
898
David Brazdil0f672f62019-12-10 10:32:29 +0000899 if (linkmode_empty(phydev->adv_old))
900 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000901
David Brazdil0f672f62019-12-10 10:32:29 +0000902 linkmode_copy(adv_tmp, phydev->advertising);
903 linkmode_copy(phydev->advertising, phydev->adv_old);
904 linkmode_zero(phydev->adv_old);
905
906 if (linkmode_equal(phydev->advertising, adv_tmp))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000907 return 0;
908
909 return phy_config_aneg(phydev);
910}
911EXPORT_SYMBOL_GPL(phy_speed_up);
912
913/**
914 * phy_start_machine - start PHY state machine tracking
915 * @phydev: the phy_device struct
916 *
917 * Description: The PHY infrastructure can run a state machine
918 * which tracks whether the PHY is starting up, negotiating,
919 * etc. This function starts the delayed workqueue which tracks
920 * the state of the PHY. If you want to maintain your own state machine,
921 * do not call this function.
922 */
923void phy_start_machine(struct phy_device *phydev)
924{
David Brazdil0f672f62019-12-10 10:32:29 +0000925 phy_trigger_machine(phydev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000926}
927EXPORT_SYMBOL_GPL(phy_start_machine);
928
929/**
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000930 * phy_stop_machine - stop the PHY state machine tracking
931 * @phydev: target phy_device struct
932 *
933 * Description: Stops the state machine delayed workqueue, sets the
934 * state to UP (unless it wasn't up yet). This function must be
935 * called BEFORE phy_detach.
936 */
937void phy_stop_machine(struct phy_device *phydev)
938{
939 cancel_delayed_work_sync(&phydev->state_queue);
940
941 mutex_lock(&phydev->lock);
David Brazdil0f672f62019-12-10 10:32:29 +0000942 if (phy_is_started(phydev))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000943 phydev->state = PHY_UP;
944 mutex_unlock(&phydev->lock);
945}
946
947/**
948 * phy_error - enter HALTED state for this PHY device
949 * @phydev: target phy_device struct
950 *
951 * Moves the PHY to the HALTED state in response to a read
952 * or write error, and tells the controller the link is down.
953 * Must not be called from interrupt context, or while the
954 * phydev->lock is held.
955 */
956static void phy_error(struct phy_device *phydev)
957{
David Brazdil0f672f62019-12-10 10:32:29 +0000958 WARN_ON(1);
959
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000960 mutex_lock(&phydev->lock);
961 phydev->state = PHY_HALTED;
962 mutex_unlock(&phydev->lock);
963
David Brazdil0f672f62019-12-10 10:32:29 +0000964 phy_trigger_machine(phydev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000965}
966
967/**
968 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
969 * @phydev: target phy_device struct
970 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200971int phy_disable_interrupts(struct phy_device *phydev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000972{
973 int err;
974
975 /* Disable PHY interrupts */
976 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
977 if (err)
978 return err;
979
980 /* Clear the interrupt */
981 return phy_clear_interrupt(phydev);
982}
983
984/**
David Brazdil0f672f62019-12-10 10:32:29 +0000985 * phy_interrupt - PHY interrupt handler
986 * @irq: interrupt line
987 * @phy_dat: phy_device pointer
988 *
989 * Description: Handle PHY interrupt
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000990 */
David Brazdil0f672f62019-12-10 10:32:29 +0000991static irqreturn_t phy_interrupt(int irq, void *phy_dat)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000992{
David Brazdil0f672f62019-12-10 10:32:29 +0000993 struct phy_device *phydev = phy_dat;
Olivier Deprez157378f2022-04-04 15:47:50 +0200994 struct phy_driver *drv = phydev->drv;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000995
Olivier Deprez157378f2022-04-04 15:47:50 +0200996 if (drv->handle_interrupt)
997 return drv->handle_interrupt(phydev);
998
999 if (drv->did_interrupt && !drv->did_interrupt(phydev))
David Brazdil0f672f62019-12-10 10:32:29 +00001000 return IRQ_NONE;
1001
Olivier Deprez157378f2022-04-04 15:47:50 +02001002 /* reschedule state queue work to run as soon as possible */
1003 phy_trigger_machine(phydev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001004
Olivier Deprez0e641232021-09-23 10:07:05 +02001005 /* did_interrupt() may have cleared the interrupt already */
Olivier Deprez157378f2022-04-04 15:47:50 +02001006 if (!drv->did_interrupt && phy_clear_interrupt(phydev)) {
1007 phy_error(phydev);
1008 return IRQ_NONE;
1009 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001010
Olivier Deprez157378f2022-04-04 15:47:50 +02001011 return IRQ_HANDLED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001012}
1013
1014/**
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001015 * phy_enable_interrupts - Enable the interrupts from the PHY side
1016 * @phydev: target phy_device struct
1017 */
1018static int phy_enable_interrupts(struct phy_device *phydev)
1019{
1020 int err = phy_clear_interrupt(phydev);
1021
1022 if (err < 0)
1023 return err;
1024
1025 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
1026}
1027
1028/**
David Brazdil0f672f62019-12-10 10:32:29 +00001029 * phy_request_interrupt - request and enable interrupt for a PHY device
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001030 * @phydev: target phy_device struct
1031 *
David Brazdil0f672f62019-12-10 10:32:29 +00001032 * Description: Request and enable the interrupt for the given PHY.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001033 * If this fails, then we set irq to PHY_POLL.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001034 * This should only be called with a valid IRQ number.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001035 */
David Brazdil0f672f62019-12-10 10:32:29 +00001036void phy_request_interrupt(struct phy_device *phydev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001037{
David Brazdil0f672f62019-12-10 10:32:29 +00001038 int err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001039
David Brazdil0f672f62019-12-10 10:32:29 +00001040 err = request_threaded_irq(phydev->irq, NULL, phy_interrupt,
1041 IRQF_ONESHOT | IRQF_SHARED,
1042 phydev_name(phydev), phydev);
1043 if (err) {
1044 phydev_warn(phydev, "Error %d requesting IRQ %d, falling back to polling\n",
1045 err, phydev->irq);
1046 phydev->irq = PHY_POLL;
1047 } else {
1048 if (phy_enable_interrupts(phydev)) {
1049 phydev_warn(phydev, "Can't enable interrupt, falling back to polling\n");
1050 phy_free_interrupt(phydev);
1051 phydev->irq = PHY_POLL;
1052 }
1053 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001054}
David Brazdil0f672f62019-12-10 10:32:29 +00001055EXPORT_SYMBOL(phy_request_interrupt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001056
1057/**
David Brazdil0f672f62019-12-10 10:32:29 +00001058 * phy_free_interrupt - disable and free interrupt for a PHY device
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001059 * @phydev: target phy_device struct
David Brazdil0f672f62019-12-10 10:32:29 +00001060 *
1061 * Description: Disable and free the interrupt for the given PHY.
1062 * This should only be called with a valid IRQ number.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001063 */
David Brazdil0f672f62019-12-10 10:32:29 +00001064void phy_free_interrupt(struct phy_device *phydev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001065{
David Brazdil0f672f62019-12-10 10:32:29 +00001066 phy_disable_interrupts(phydev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001067 free_irq(phydev->irq, phydev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001068}
David Brazdil0f672f62019-12-10 10:32:29 +00001069EXPORT_SYMBOL(phy_free_interrupt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001070
1071/**
1072 * phy_stop - Bring down the PHY link, and stop checking the status
1073 * @phydev: target phy_device struct
1074 */
1075void phy_stop(struct phy_device *phydev)
1076{
Olivier Deprez157378f2022-04-04 15:47:50 +02001077 struct net_device *dev = phydev->attached_dev;
1078
Olivier Deprez0e641232021-09-23 10:07:05 +02001079 if (!phy_is_started(phydev) && phydev->state != PHY_DOWN) {
David Brazdil0f672f62019-12-10 10:32:29 +00001080 WARN(1, "called from state %s\n",
1081 phy_state_to_str(phydev->state));
1082 return;
1083 }
1084
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001085 mutex_lock(&phydev->lock);
1086
Olivier Deprez157378f2022-04-04 15:47:50 +02001087 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001095 phydev->state = PHY_HALTED;
1096
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001097 mutex_unlock(&phydev->lock);
1098
David Brazdil0f672f62019-12-10 10:32:29 +00001099 phy_state_machine(&phydev->state_queue.work);
1100 phy_stop_machine(phydev);
1101
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001102 /* Cannot call flush_scheduled_work() here as desired because
David Brazdil0f672f62019-12-10 10:32:29 +00001103 * of rtnl_lock(), but PHY_HALTED shall guarantee irq handler
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001104 * will not reenable interrupts.
1105 */
1106}
1107EXPORT_SYMBOL(phy_stop);
1108
1109/**
1110 * phy_start - start or restart a PHY device
1111 * @phydev: target phy_device struct
1112 *
1113 * Description: Indicates the attached device's readiness to
1114 * handle PHY-related work. Used during startup to start the
1115 * PHY, and after a call to phy_stop() to resume operation.
1116 * Also used to indicate the MDIO bus has cleared an error
1117 * condition.
1118 */
1119void phy_start(struct phy_device *phydev)
1120{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001121 mutex_lock(&phydev->lock);
1122
David Brazdil0f672f62019-12-10 10:32:29 +00001123 if (phydev->state != PHY_READY && phydev->state != PHY_HALTED) {
1124 WARN(1, "called from state %s\n",
1125 phy_state_to_str(phydev->state));
1126 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001127 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001128
Olivier Deprez157378f2022-04-04 15:47:50 +02001129 if (phydev->sfp_bus)
1130 sfp_upstream_start(phydev->sfp_bus);
1131
David Brazdil0f672f62019-12-10 10:32:29 +00001132 /* if phy was suspended, bring the physical link up again */
1133 __phy_resume(phydev);
1134
1135 phydev->state = PHY_UP;
1136
1137 phy_start_machine(phydev);
1138out:
1139 mutex_unlock(&phydev->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001140}
1141EXPORT_SYMBOL(phy_start);
1142
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001143/**
1144 * phy_state_machine - Handle the state machine
1145 * @work: work_struct that describes the work to be done
1146 */
1147void phy_state_machine(struct work_struct *work)
1148{
1149 struct delayed_work *dwork = to_delayed_work(work);
1150 struct phy_device *phydev =
1151 container_of(dwork, struct phy_device, state_queue);
Olivier Deprez157378f2022-04-04 15:47:50 +02001152 struct net_device *dev = phydev->attached_dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001153 bool needs_aneg = false, do_suspend = false;
1154 enum phy_state old_state;
Olivier Deprez157378f2022-04-04 15:47:50 +02001155 bool finished = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001156 int err = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001157
1158 mutex_lock(&phydev->lock);
1159
1160 old_state = phydev->state;
1161
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001162 switch (phydev->state) {
1163 case PHY_DOWN:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001164 case PHY_READY:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001165 break;
1166 case PHY_UP:
1167 needs_aneg = true;
1168
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001169 break;
1170 case PHY_NOLINK:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001171 case PHY_RUNNING:
David Brazdil0f672f62019-12-10 10:32:29 +00001172 err = phy_check_link_status(phydev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001173 break;
Olivier Deprez157378f2022-04-04 15:47:50 +02001174 case PHY_CABLETEST:
1175 err = phydev->drv->cable_test_get_status(phydev, &finished);
1176 if (err) {
1177 phy_abort_cable_test(phydev);
1178 netif_testing_off(dev);
1179 needs_aneg = true;
1180 phydev->state = PHY_UP;
1181 break;
1182 }
1183
1184 if (finished) {
1185 ethnl_cable_test_finished(phydev);
1186 netif_testing_off(dev);
1187 needs_aneg = true;
1188 phydev->state = PHY_UP;
1189 }
1190 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001191 case PHY_HALTED:
1192 if (phydev->link) {
1193 phydev->link = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02001194 phy_link_down(phydev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001195 }
David Brazdil0f672f62019-12-10 10:32:29 +00001196 do_suspend = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001197 break;
1198 }
1199
1200 mutex_unlock(&phydev->lock);
1201
1202 if (needs_aneg)
David Brazdil0f672f62019-12-10 10:32:29 +00001203 err = phy_start_aneg(phydev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001204 else if (do_suspend)
1205 phy_suspend(phydev);
1206
1207 if (err < 0)
1208 phy_error(phydev);
1209
David Brazdil0f672f62019-12-10 10:32:29 +00001210 if (old_state != phydev->state) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001211 phydev_dbg(phydev, "PHY state change %s -> %s\n",
1212 phy_state_to_str(old_state),
1213 phy_state_to_str(phydev->state));
David Brazdil0f672f62019-12-10 10:32:29 +00001214 if (phydev->drv && phydev->drv->link_change_notify)
1215 phydev->drv->link_change_notify(phydev);
1216 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001217
1218 /* Only re-schedule a PHY state machine change if we are polling the
1219 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
David Brazdil0f672f62019-12-10 10:32:29 +00001220 * between states from phy_mac_interrupt().
1221 *
1222 * In state PHY_HALTED the PHY gets suspended, so rescheduling the
1223 * state machine would be pointless and possibly error prone when
1224 * called from phy_disconnect() synchronously.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001225 */
David Brazdil0f672f62019-12-10 10:32:29 +00001226 mutex_lock(&phydev->lock);
1227 if (phy_polling_mode(phydev) && phy_is_started(phydev))
1228 phy_queue_state_machine(phydev, PHY_STATE_TIME);
1229 mutex_unlock(&phydev->lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001230}
1231
1232/**
1233 * phy_mac_interrupt - MAC says the link has changed
1234 * @phydev: phy_device struct with changed link
1235 *
1236 * The MAC layer is able to indicate there has been a change in the PHY link
1237 * status. Trigger the state machine and work a work queue.
1238 */
1239void phy_mac_interrupt(struct phy_device *phydev)
1240{
1241 /* Trigger a state machine change */
David Brazdil0f672f62019-12-10 10:32:29 +00001242 phy_trigger_machine(phydev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001243}
1244EXPORT_SYMBOL(phy_mac_interrupt);
1245
David Brazdil0f672f62019-12-10 10:32:29 +00001246static void mmd_eee_adv_to_linkmode(unsigned long *advertising, u16 eee_adv)
1247{
1248 linkmode_zero(advertising);
1249
1250 if (eee_adv & MDIO_EEE_100TX)
1251 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
1252 advertising);
1253 if (eee_adv & MDIO_EEE_1000T)
1254 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1255 advertising);
1256 if (eee_adv & MDIO_EEE_10GT)
1257 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
1258 advertising);
1259 if (eee_adv & MDIO_EEE_1000KX)
1260 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
1261 advertising);
1262 if (eee_adv & MDIO_EEE_10GKX4)
1263 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
1264 advertising);
1265 if (eee_adv & MDIO_EEE_10GKR)
1266 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
1267 advertising);
1268}
1269
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001270/**
1271 * phy_init_eee - init and check the EEE feature
1272 * @phydev: target phy_device struct
1273 * @clk_stop_enable: PHY may stop the clock during LPI
1274 *
1275 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1276 * is supported by looking at the MMD registers 3.20 and 7.60/61
1277 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1278 * bit if required.
1279 */
1280int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1281{
1282 if (!phydev->drv)
1283 return -EIO;
1284
1285 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1286 */
1287 if (phydev->duplex == DUPLEX_FULL) {
David Brazdil0f672f62019-12-10 10:32:29 +00001288 __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
1289 __ETHTOOL_DECLARE_LINK_MODE_MASK(lp);
1290 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001291 int eee_lp, eee_cap, eee_adv;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001292 int status;
David Brazdil0f672f62019-12-10 10:32:29 +00001293 u32 cap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001294
1295 /* Read phy status to properly get the right settings */
1296 status = phy_read_status(phydev);
1297 if (status)
1298 return status;
1299
1300 /* First check if the EEE ability is supported */
1301 eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1302 if (eee_cap <= 0)
1303 goto eee_exit_err;
1304
1305 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
1306 if (!cap)
1307 goto eee_exit_err;
1308
1309 /* Check which link settings negotiated and verify it in
1310 * the EEE advertising registers.
1311 */
1312 eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1313 if (eee_lp <= 0)
1314 goto eee_exit_err;
1315
1316 eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1317 if (eee_adv <= 0)
1318 goto eee_exit_err;
1319
David Brazdil0f672f62019-12-10 10:32:29 +00001320 mmd_eee_adv_to_linkmode(adv, eee_adv);
1321 mmd_eee_adv_to_linkmode(lp, eee_lp);
1322 linkmode_and(common, adv, lp);
1323
1324 if (!phy_check_valid(phydev->speed, phydev->duplex, common))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001325 goto eee_exit_err;
1326
David Brazdil0f672f62019-12-10 10:32:29 +00001327 if (clk_stop_enable)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001328 /* Configure the PHY to stop receiving xMII
1329 * clock while it is signaling LPI.
1330 */
David Brazdil0f672f62019-12-10 10:32:29 +00001331 phy_set_bits_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1,
1332 MDIO_PCS_CTRL1_CLKSTOP_EN);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001333
1334 return 0; /* EEE supported */
1335 }
1336eee_exit_err:
1337 return -EPROTONOSUPPORT;
1338}
1339EXPORT_SYMBOL(phy_init_eee);
1340
1341/**
1342 * phy_get_eee_err - report the EEE wake error count
1343 * @phydev: target phy_device struct
1344 *
1345 * Description: it is to report the number of time where the PHY
1346 * failed to complete its normal wake sequence.
1347 */
1348int phy_get_eee_err(struct phy_device *phydev)
1349{
1350 if (!phydev->drv)
1351 return -EIO;
1352
1353 return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
1354}
1355EXPORT_SYMBOL(phy_get_eee_err);
1356
1357/**
1358 * phy_ethtool_get_eee - get EEE supported and status
1359 * @phydev: target phy_device struct
1360 * @data: ethtool_eee data
1361 *
1362 * Description: it reportes the Supported/Advertisement/LP Advertisement
1363 * capabilities.
1364 */
1365int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1366{
1367 int val;
1368
1369 if (!phydev->drv)
1370 return -EIO;
1371
1372 /* Get Supported EEE */
1373 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1374 if (val < 0)
1375 return val;
1376 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1377
1378 /* Get advertisement EEE */
1379 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1380 if (val < 0)
1381 return val;
1382 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
David Brazdil0f672f62019-12-10 10:32:29 +00001383 data->eee_enabled = !!data->advertised;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001384
1385 /* Get LP advertisement EEE */
1386 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1387 if (val < 0)
1388 return val;
1389 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1390
David Brazdil0f672f62019-12-10 10:32:29 +00001391 data->eee_active = !!(data->advertised & data->lp_advertised);
1392
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001393 return 0;
1394}
1395EXPORT_SYMBOL(phy_ethtool_get_eee);
1396
1397/**
1398 * phy_ethtool_set_eee - set EEE supported and status
1399 * @phydev: target phy_device struct
1400 * @data: ethtool_eee data
1401 *
1402 * Description: it is to program the Advertisement EEE register.
1403 */
1404int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1405{
David Brazdil0f672f62019-12-10 10:32:29 +00001406 int cap, old_adv, adv = 0, ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001407
1408 if (!phydev->drv)
1409 return -EIO;
1410
1411 /* Get Supported EEE */
1412 cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1413 if (cap < 0)
1414 return cap;
1415
1416 old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1417 if (old_adv < 0)
1418 return old_adv;
1419
David Brazdil0f672f62019-12-10 10:32:29 +00001420 if (data->eee_enabled) {
1421 adv = !data->advertised ? cap :
1422 ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
1423 /* Mask prohibited EEE modes */
1424 adv &= ~phydev->eee_broken_modes;
1425 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001426
1427 if (old_adv != adv) {
1428 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1429 if (ret < 0)
1430 return ret;
1431
1432 /* Restart autonegotiation so the new modes get sent to the
1433 * link partner.
1434 */
Olivier Deprez0e641232021-09-23 10:07:05 +02001435 if (phydev->autoneg == AUTONEG_ENABLE) {
1436 ret = phy_restart_aneg(phydev);
1437 if (ret < 0)
1438 return ret;
1439 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001440 }
1441
1442 return 0;
1443}
1444EXPORT_SYMBOL(phy_ethtool_set_eee);
1445
Olivier Deprez157378f2022-04-04 15:47:50 +02001446/**
1447 * phy_ethtool_set_wol - Configure Wake On LAN
1448 *
1449 * @phydev: target phy_device struct
1450 * @wol: Configuration requested
1451 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001452int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1453{
1454 if (phydev->drv && phydev->drv->set_wol)
1455 return phydev->drv->set_wol(phydev, wol);
1456
1457 return -EOPNOTSUPP;
1458}
1459EXPORT_SYMBOL(phy_ethtool_set_wol);
1460
Olivier Deprez157378f2022-04-04 15:47:50 +02001461/**
1462 * phy_ethtool_get_wol - Get the current Wake On LAN configuration
1463 *
1464 * @phydev: target phy_device struct
1465 * @wol: Store the current configuration here
1466 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001467void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1468{
1469 if (phydev->drv && phydev->drv->get_wol)
1470 phydev->drv->get_wol(phydev, wol);
1471}
1472EXPORT_SYMBOL(phy_ethtool_get_wol);
1473
1474int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1475 struct ethtool_link_ksettings *cmd)
1476{
1477 struct phy_device *phydev = ndev->phydev;
1478
1479 if (!phydev)
1480 return -ENODEV;
1481
1482 phy_ethtool_ksettings_get(phydev, cmd);
1483
1484 return 0;
1485}
1486EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1487
1488int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1489 const struct ethtool_link_ksettings *cmd)
1490{
1491 struct phy_device *phydev = ndev->phydev;
1492
1493 if (!phydev)
1494 return -ENODEV;
1495
1496 return phy_ethtool_ksettings_set(phydev, cmd);
1497}
1498EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
1499
Olivier Deprez157378f2022-04-04 15:47:50 +02001500/**
1501 * phy_ethtool_nway_reset - Restart auto negotiation
1502 * @ndev: Network device to restart autoneg for
1503 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001504int phy_ethtool_nway_reset(struct net_device *ndev)
1505{
1506 struct phy_device *phydev = ndev->phydev;
1507
1508 if (!phydev)
1509 return -ENODEV;
1510
1511 if (!phydev->drv)
1512 return -EIO;
1513
1514 return phy_restart_aneg(phydev);
1515}
1516EXPORT_SYMBOL(phy_ethtool_nway_reset);