hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
kernel/drivers/net/phy/phy.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0+
12 /* Framework for configuring and reading PHY devices
23 * Based on code in sungem_phy.c and gianfar_phy.c
34 *
....@@ -5,15 +6,7 @@
56 *
67 * Copyright (c) 2004 Freescale Semiconductor, Inc.
78 * Copyright (c) 2006, 2007 Maciej W. Rozycki
8
- *
9
- * This program is free software; you can redistribute it and/or modify it
10
- * under the terms of the GNU General Public License as published by the
11
- * Free Software Foundation; either version 2 of the License, or (at your
12
- * option) any later version.
13
- *
149 */
15
-
16
-#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1710
1811 #include <linux/kernel.h>
1912 #include <linux/string.h>
....@@ -22,21 +15,27 @@
2215 #include <linux/interrupt.h>
2316 #include <linux/delay.h>
2417 #include <linux/netdevice.h>
18
+#include <linux/netlink.h>
2519 #include <linux/etherdevice.h>
2620 #include <linux/skbuff.h>
2721 #include <linux/mm.h>
2822 #include <linux/module.h>
2923 #include <linux/mii.h>
3024 #include <linux/ethtool.h>
25
+#include <linux/ethtool_netlink.h>
3126 #include <linux/phy.h>
3227 #include <linux/phy_led_triggers.h>
28
+#include <linux/sfp.h>
3329 #include <linux/workqueue.h>
3430 #include <linux/mdio.h>
3531 #include <linux/io.h>
3632 #include <linux/uaccess.h>
3733 #include <linux/atomic.h>
34
+#include <net/netlink.h>
35
+#include <net/genetlink.h>
36
+#include <net/sock.h>
3837
39
-#include <asm/irq.h>
38
+#define PHY_STATE_TIME HZ
4039
4140 #define PHY_STATE_STR(_state) \
4241 case PHY_##_state: \
....@@ -46,22 +45,66 @@
4645 {
4746 switch (st) {
4847 PHY_STATE_STR(DOWN)
49
- PHY_STATE_STR(STARTING)
5048 PHY_STATE_STR(READY)
51
- PHY_STATE_STR(PENDING)
5249 PHY_STATE_STR(UP)
53
- PHY_STATE_STR(AN)
5450 PHY_STATE_STR(RUNNING)
5551 PHY_STATE_STR(NOLINK)
56
- PHY_STATE_STR(FORCING)
57
- PHY_STATE_STR(CHANGELINK)
52
+ PHY_STATE_STR(CABLETEST)
5853 PHY_STATE_STR(HALTED)
59
- PHY_STATE_STR(RESUMING)
6054 }
6155
6256 return NULL;
6357 }
6458
59
+static void phy_process_state_change(struct phy_device *phydev,
60
+ enum phy_state old_state)
61
+{
62
+ if (old_state != phydev->state) {
63
+ phydev_dbg(phydev, "PHY state change %s -> %s\n",
64
+ phy_state_to_str(old_state),
65
+ phy_state_to_str(phydev->state));
66
+ if (phydev->drv && phydev->drv->link_change_notify)
67
+ phydev->drv->link_change_notify(phydev);
68
+ }
69
+}
70
+
71
+static void phy_link_up(struct phy_device *phydev)
72
+{
73
+ phydev->phy_link_change(phydev, true);
74
+ phy_led_trigger_change_speed(phydev);
75
+}
76
+
77
+static void phy_link_down(struct phy_device *phydev)
78
+{
79
+ phydev->phy_link_change(phydev, false);
80
+ phy_led_trigger_change_speed(phydev);
81
+}
82
+
83
+static const char *phy_pause_str(struct phy_device *phydev)
84
+{
85
+ bool local_pause, local_asym_pause;
86
+
87
+ if (phydev->autoneg == AUTONEG_DISABLE)
88
+ goto no_pause;
89
+
90
+ local_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
91
+ phydev->advertising);
92
+ local_asym_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
93
+ phydev->advertising);
94
+
95
+ if (local_pause && phydev->pause)
96
+ return "rx/tx";
97
+
98
+ if (local_asym_pause && phydev->asym_pause) {
99
+ if (local_pause)
100
+ return "rx";
101
+ if (phydev->pause)
102
+ return "tx";
103
+ }
104
+
105
+no_pause:
106
+ return "off";
107
+}
65108
66109 /**
67110 * phy_print_status - Convenience function to print out the current phy status
....@@ -71,10 +114,11 @@
71114 {
72115 if (phydev->link) {
73116 netdev_info(phydev->attached_dev,
74
- "Link is Up - %s/%s - flow control %s\n",
117
+ "Link is Up - %s/%s %s- flow control %s\n",
75118 phy_speed_to_str(phydev->speed),
76119 phy_duplex_to_str(phydev->duplex),
77
- phydev->pause ? "rx/tx" : "off");
120
+ phydev->downshifted_rate ? "(downshifted) " : "",
121
+ phy_pause_str(phydev));
78122 } else {
79123 netdev_info(phydev->attached_dev, "Link is Down\n");
80124 }
....@@ -92,10 +136,15 @@
92136 */
93137 static int phy_clear_interrupt(struct phy_device *phydev)
94138 {
95
- if (phydev->drv->ack_interrupt)
96
- return phydev->drv->ack_interrupt(phydev);
139
+ int ret = 0;
97140
98
- return 0;
141
+ if (phydev->drv->ack_interrupt) {
142
+ mutex_lock(&phydev->lock);
143
+ ret = phydev->drv->ack_interrupt(phydev);
144
+ mutex_unlock(&phydev->lock);
145
+ }
146
+
147
+ return ret;
99148 }
100149
101150 /**
....@@ -105,9 +154,9 @@
105154 *
106155 * Returns 0 on success or < 0 on error.
107156 */
108
-static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
157
+static int phy_config_interrupt(struct phy_device *phydev, bool interrupts)
109158 {
110
- phydev->interrupts = interrupts;
159
+ phydev->interrupts = interrupts ? 1 : 0;
111160 if (phydev->drv->config_intr)
112161 return phydev->drv->config_intr(phydev);
113162
....@@ -146,14 +195,10 @@
146195 {
147196 if (phydev->drv && phydev->drv->aneg_done)
148197 return phydev->drv->aneg_done(phydev);
149
-
150
- /* Avoid genphy_aneg_done() if the Clause 45 PHY does not
151
- * implement Clause 22 registers
152
- */
153
- if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
154
- return -EINVAL;
155
-
156
- return genphy_aneg_done(phydev);
198
+ else if (phydev->is_c45)
199
+ return genphy_c45_aneg_done(phydev);
200
+ else
201
+ return genphy_aneg_done(phydev);
157202 }
158203 EXPORT_SYMBOL(phy_aneg_done);
159204
....@@ -171,11 +216,9 @@
171216 * settings were found.
172217 */
173218 static const struct phy_setting *
174
-phy_find_valid(int speed, int duplex, u32 supported)
219
+phy_find_valid(int speed, int duplex, unsigned long *supported)
175220 {
176
- unsigned long mask = supported;
177
-
178
- return phy_lookup_setting(speed, duplex, &mask, BITS_PER_LONG, false);
221
+ return phy_lookup_setting(speed, duplex, supported, false);
179222 }
180223
181224 /**
....@@ -192,9 +235,7 @@
192235 unsigned int *speeds,
193236 unsigned int size)
194237 {
195
- unsigned long supported = phy->supported;
196
-
197
- return phy_speeds(speeds, size, &supported, BITS_PER_LONG);
238
+ return phy_speeds(speeds, size, phy->supported);
198239 }
199240
200241 /**
....@@ -206,11 +247,10 @@
206247 *
207248 * Description: Returns true if there is a valid setting, false otherwise.
208249 */
209
-static inline bool phy_check_valid(int speed, int duplex, u32 features)
250
+static inline bool phy_check_valid(int speed, int duplex,
251
+ unsigned long *features)
210252 {
211
- unsigned long mask = features;
212
-
213
- return !!phy_lookup_setting(speed, duplex, &mask, BITS_PER_LONG, true);
253
+ return !!phy_lookup_setting(speed, duplex, features, true);
214254 }
215255
216256 /**
....@@ -224,13 +264,9 @@
224264 static void phy_sanitize_settings(struct phy_device *phydev)
225265 {
226266 const struct phy_setting *setting;
227
- u32 features = phydev->supported;
228267
229
- /* Sanitize settings based on PHY capabilities */
230
- if ((features & SUPPORTED_Autoneg) == 0)
231
- phydev->autoneg = AUTONEG_DISABLE;
232
-
233
- setting = phy_find_valid(phydev->speed, phydev->duplex, features);
268
+ setting = phy_find_valid(phydev->speed, phydev->duplex,
269
+ phydev->supported);
234270 if (setting) {
235271 phydev->speed = setting->speed;
236272 phydev->duplex = setting->duplex;
....@@ -241,145 +277,29 @@
241277 }
242278 }
243279
244
-/**
245
- * phy_ethtool_sset - generic ethtool sset function, handles all the details
246
- * @phydev: target phy_device struct
247
- * @cmd: ethtool_cmd
248
- *
249
- * A few notes about parameter checking:
250
- *
251
- * - We don't set port or transceiver, so we don't care what they
252
- * were set to.
253
- * - phy_start_aneg() will make sure forced settings are sane, and
254
- * choose the next best ones from the ones selected, so we don't
255
- * care if ethtool tries to give us bad values.
256
- */
257
-int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
258
-{
259
- u32 speed = ethtool_cmd_speed(cmd);
260
-
261
- if (cmd->phy_address != phydev->mdio.addr)
262
- return -EINVAL;
263
-
264
- /* We make sure that we don't pass unsupported values in to the PHY */
265
- cmd->advertising &= phydev->supported;
266
-
267
- /* Verify the settings we care about. */
268
- if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
269
- return -EINVAL;
270
-
271
- if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
272
- return -EINVAL;
273
-
274
- if (cmd->autoneg == AUTONEG_DISABLE &&
275
- ((speed != SPEED_1000 &&
276
- speed != SPEED_100 &&
277
- speed != SPEED_10) ||
278
- (cmd->duplex != DUPLEX_HALF &&
279
- cmd->duplex != DUPLEX_FULL)))
280
- return -EINVAL;
281
-
282
- phydev->autoneg = cmd->autoneg;
283
-
284
- phydev->speed = speed;
285
-
286
- phydev->advertising = cmd->advertising;
287
-
288
- if (AUTONEG_ENABLE == cmd->autoneg)
289
- phydev->advertising |= ADVERTISED_Autoneg;
290
- else
291
- phydev->advertising &= ~ADVERTISED_Autoneg;
292
-
293
- phydev->duplex = cmd->duplex;
294
-
295
- phydev->mdix_ctrl = cmd->eth_tp_mdix_ctrl;
296
-
297
- /* Restart the PHY */
298
- phy_start_aneg(phydev);
299
-
300
- return 0;
301
-}
302
-EXPORT_SYMBOL(phy_ethtool_sset);
303
-
304
-int phy_ethtool_ksettings_set(struct phy_device *phydev,
305
- const struct ethtool_link_ksettings *cmd)
306
-{
307
- u8 autoneg = cmd->base.autoneg;
308
- u8 duplex = cmd->base.duplex;
309
- u32 speed = cmd->base.speed;
310
- u32 advertising;
311
-
312
- if (cmd->base.phy_address != phydev->mdio.addr)
313
- return -EINVAL;
314
-
315
- ethtool_convert_link_mode_to_legacy_u32(&advertising,
316
- cmd->link_modes.advertising);
317
-
318
- /* We make sure that we don't pass unsupported values in to the PHY */
319
- advertising &= phydev->supported;
320
-
321
- /* Verify the settings we care about. */
322
- if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
323
- return -EINVAL;
324
-
325
- if (autoneg == AUTONEG_ENABLE && advertising == 0)
326
- return -EINVAL;
327
-
328
- if (autoneg == AUTONEG_DISABLE &&
329
- ((speed != SPEED_1000 &&
330
- speed != SPEED_100 &&
331
- speed != SPEED_10) ||
332
- (duplex != DUPLEX_HALF &&
333
- duplex != DUPLEX_FULL)))
334
- return -EINVAL;
335
-
336
- phydev->autoneg = autoneg;
337
-
338
- if (autoneg == AUTONEG_DISABLE) {
339
- phydev->speed = speed;
340
- phydev->duplex = duplex;
341
- }
342
-
343
- phydev->advertising = advertising;
344
-
345
- if (autoneg == AUTONEG_ENABLE)
346
- phydev->advertising |= ADVERTISED_Autoneg;
347
- else
348
- phydev->advertising &= ~ADVERTISED_Autoneg;
349
-
350
- phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
351
-
352
- /* Restart the PHY */
353
- phy_start_aneg(phydev);
354
-
355
- return 0;
356
-}
357
-EXPORT_SYMBOL(phy_ethtool_ksettings_set);
358
-
359280 void phy_ethtool_ksettings_get(struct phy_device *phydev,
360281 struct ethtool_link_ksettings *cmd)
361282 {
362
- ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
363
- phydev->supported);
364
-
365
- ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
366
- phydev->advertising);
367
-
368
- ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
369
- phydev->lp_advertising);
283
+ mutex_lock(&phydev->lock);
284
+ linkmode_copy(cmd->link_modes.supported, phydev->supported);
285
+ linkmode_copy(cmd->link_modes.advertising, phydev->advertising);
286
+ linkmode_copy(cmd->link_modes.lp_advertising, phydev->lp_advertising);
370287
371288 cmd->base.speed = phydev->speed;
372289 cmd->base.duplex = phydev->duplex;
290
+ cmd->base.master_slave_cfg = phydev->master_slave_get;
291
+ cmd->base.master_slave_state = phydev->master_slave_state;
373292 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
374293 cmd->base.port = PORT_BNC;
375294 else
376
- cmd->base.port = PORT_MII;
295
+ cmd->base.port = phydev->port;
377296 cmd->base.transceiver = phy_is_internal(phydev) ?
378297 XCVR_INTERNAL : XCVR_EXTERNAL;
379298 cmd->base.phy_address = phydev->mdio.addr;
380299 cmd->base.autoneg = phydev->autoneg;
381300 cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
382301 cmd->base.eth_tp_mdix = phydev->mdix;
302
+ mutex_unlock(&phydev->lock);
383303 }
384304 EXPORT_SYMBOL(phy_ethtool_ksettings_get);
385305
....@@ -398,21 +318,37 @@
398318 struct mii_ioctl_data *mii_data = if_mii(ifr);
399319 u16 val = mii_data->val_in;
400320 bool change_autoneg = false;
321
+ int prtad, devad;
401322
402323 switch (cmd) {
403324 case SIOCGMIIPHY:
404325 mii_data->phy_id = phydev->mdio.addr;
405
- /* fall through */
326
+ fallthrough;
406327
407328 case SIOCGMIIREG:
408
- mii_data->val_out = mdiobus_read(phydev->mdio.bus,
409
- mii_data->phy_id,
410
- mii_data->reg_num);
329
+ if (mdio_phy_id_is_c45(mii_data->phy_id)) {
330
+ prtad = mdio_phy_id_prtad(mii_data->phy_id);
331
+ devad = mdio_phy_id_devad(mii_data->phy_id);
332
+ devad = mdiobus_c45_addr(devad, mii_data->reg_num);
333
+ } else {
334
+ prtad = mii_data->phy_id;
335
+ devad = mii_data->reg_num;
336
+ }
337
+ mii_data->val_out = mdiobus_read(phydev->mdio.bus, prtad,
338
+ devad);
411339 return 0;
412340
413341 case SIOCSMIIREG:
414
- if (mii_data->phy_id == phydev->mdio.addr) {
415
- switch (mii_data->reg_num) {
342
+ if (mdio_phy_id_is_c45(mii_data->phy_id)) {
343
+ prtad = mdio_phy_id_prtad(mii_data->phy_id);
344
+ devad = mdio_phy_id_devad(mii_data->phy_id);
345
+ devad = mdiobus_c45_addr(devad, mii_data->reg_num);
346
+ } else {
347
+ prtad = mii_data->phy_id;
348
+ devad = mii_data->reg_num;
349
+ }
350
+ if (prtad == phydev->mdio.addr) {
351
+ switch (devad) {
416352 case MII_BMCR:
417353 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
418354 if (phydev->autoneg == AUTONEG_ENABLE)
....@@ -435,7 +371,13 @@
435371 }
436372 break;
437373 case MII_ADVERTISE:
438
- phydev->advertising = mii_adv_to_ethtool_adv_t(val);
374
+ mii_adv_mod_linkmode_adv_t(phydev->advertising,
375
+ val);
376
+ change_autoneg = true;
377
+ break;
378
+ case MII_CTRL1000:
379
+ mii_ctrl1000_mod_linkmode_adv_t(phydev->advertising,
380
+ val);
439381 change_autoneg = true;
440382 break;
441383 default:
....@@ -444,11 +386,10 @@
444386 }
445387 }
446388
447
- mdiobus_write(phydev->mdio.bus, mii_data->phy_id,
448
- mii_data->reg_num, val);
389
+ mdiobus_write(phydev->mdio.bus, prtad, devad, val);
449390
450
- if (mii_data->phy_id == phydev->mdio.addr &&
451
- mii_data->reg_num == MII_BMCR &&
391
+ if (prtad == phydev->mdio.addr &&
392
+ devad == MII_BMCR &&
452393 val & BMCR_RESET)
453394 return phy_init_hw(phydev);
454395
....@@ -458,15 +399,289 @@
458399 return 0;
459400
460401 case SIOCSHWTSTAMP:
461
- if (phydev->drv && phydev->drv->hwtstamp)
462
- return phydev->drv->hwtstamp(phydev, ifr);
463
- /* fall through */
402
+ if (phydev->mii_ts && phydev->mii_ts->hwtstamp)
403
+ return phydev->mii_ts->hwtstamp(phydev->mii_ts, ifr);
404
+ fallthrough;
464405
465406 default:
466407 return -EOPNOTSUPP;
467408 }
468409 }
469410 EXPORT_SYMBOL(phy_mii_ioctl);
411
+
412
+/**
413
+ * phy_do_ioctl - generic ndo_do_ioctl implementation
414
+ * @dev: the net_device struct
415
+ * @ifr: &struct ifreq for socket ioctl's
416
+ * @cmd: ioctl cmd to execute
417
+ */
418
+int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
419
+{
420
+ if (!dev->phydev)
421
+ return -ENODEV;
422
+
423
+ return phy_mii_ioctl(dev->phydev, ifr, cmd);
424
+}
425
+EXPORT_SYMBOL(phy_do_ioctl);
426
+
427
+/**
428
+ * phy_do_ioctl_running - generic ndo_do_ioctl implementation but test first
429
+ *
430
+ * @dev: the net_device struct
431
+ * @ifr: &struct ifreq for socket ioctl's
432
+ * @cmd: ioctl cmd to execute
433
+ *
434
+ * Same as phy_do_ioctl, but ensures that net_device is running before
435
+ * handling the ioctl.
436
+ */
437
+int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd)
438
+{
439
+ if (!netif_running(dev))
440
+ return -ENODEV;
441
+
442
+ return phy_do_ioctl(dev, ifr, cmd);
443
+}
444
+EXPORT_SYMBOL(phy_do_ioctl_running);
445
+
446
+/**
447
+ * phy_queue_state_machine - Trigger the state machine to run soon
448
+ *
449
+ * @phydev: the phy_device struct
450
+ * @jiffies: Run the state machine after these jiffies
451
+ */
452
+void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies)
453
+{
454
+ mod_delayed_work(system_power_efficient_wq, &phydev->state_queue,
455
+ jiffies);
456
+}
457
+EXPORT_SYMBOL(phy_queue_state_machine);
458
+
459
+/**
460
+ * phy_queue_state_machine - Trigger the state machine to run now
461
+ *
462
+ * @phydev: the phy_device struct
463
+ */
464
+static void phy_trigger_machine(struct phy_device *phydev)
465
+{
466
+ phy_queue_state_machine(phydev, 0);
467
+}
468
+
469
+static void phy_abort_cable_test(struct phy_device *phydev)
470
+{
471
+ int err;
472
+
473
+ ethnl_cable_test_finished(phydev);
474
+
475
+ err = phy_init_hw(phydev);
476
+ if (err)
477
+ phydev_err(phydev, "Error while aborting cable test");
478
+}
479
+
480
+/**
481
+ * phy_ethtool_get_strings - Get the statistic counter names
482
+ *
483
+ * @phydev: the phy_device struct
484
+ * @data: Where to put the strings
485
+ */
486
+int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data)
487
+{
488
+ if (!phydev->drv)
489
+ return -EIO;
490
+
491
+ mutex_lock(&phydev->lock);
492
+ phydev->drv->get_strings(phydev, data);
493
+ mutex_unlock(&phydev->lock);
494
+
495
+ return 0;
496
+}
497
+EXPORT_SYMBOL(phy_ethtool_get_strings);
498
+
499
+/**
500
+ * phy_ethtool_get_sset_count - Get the number of statistic counters
501
+ *
502
+ * @phydev: the phy_device struct
503
+ */
504
+int phy_ethtool_get_sset_count(struct phy_device *phydev)
505
+{
506
+ int ret;
507
+
508
+ if (!phydev->drv)
509
+ return -EIO;
510
+
511
+ if (phydev->drv->get_sset_count &&
512
+ phydev->drv->get_strings &&
513
+ phydev->drv->get_stats) {
514
+ mutex_lock(&phydev->lock);
515
+ ret = phydev->drv->get_sset_count(phydev);
516
+ mutex_unlock(&phydev->lock);
517
+
518
+ return ret;
519
+ }
520
+
521
+ return -EOPNOTSUPP;
522
+}
523
+EXPORT_SYMBOL(phy_ethtool_get_sset_count);
524
+
525
+/**
526
+ * phy_ethtool_get_stats - Get the statistic counters
527
+ *
528
+ * @phydev: the phy_device struct
529
+ * @stats: What counters to get
530
+ * @data: Where to store the counters
531
+ */
532
+int phy_ethtool_get_stats(struct phy_device *phydev,
533
+ struct ethtool_stats *stats, u64 *data)
534
+{
535
+ if (!phydev->drv)
536
+ return -EIO;
537
+
538
+ mutex_lock(&phydev->lock);
539
+ phydev->drv->get_stats(phydev, stats, data);
540
+ mutex_unlock(&phydev->lock);
541
+
542
+ return 0;
543
+}
544
+EXPORT_SYMBOL(phy_ethtool_get_stats);
545
+
546
+/**
547
+ * phy_start_cable_test - Start a cable test
548
+ *
549
+ * @phydev: the phy_device struct
550
+ * @extack: extack for reporting useful error messages
551
+ */
552
+int phy_start_cable_test(struct phy_device *phydev,
553
+ struct netlink_ext_ack *extack)
554
+{
555
+ struct net_device *dev = phydev->attached_dev;
556
+ int err = -ENOMEM;
557
+
558
+ if (!(phydev->drv &&
559
+ phydev->drv->cable_test_start &&
560
+ phydev->drv->cable_test_get_status)) {
561
+ NL_SET_ERR_MSG(extack,
562
+ "PHY driver does not support cable testing");
563
+ return -EOPNOTSUPP;
564
+ }
565
+
566
+ mutex_lock(&phydev->lock);
567
+ if (phydev->state == PHY_CABLETEST) {
568
+ NL_SET_ERR_MSG(extack,
569
+ "PHY already performing a test");
570
+ err = -EBUSY;
571
+ goto out;
572
+ }
573
+
574
+ if (phydev->state < PHY_UP ||
575
+ phydev->state > PHY_CABLETEST) {
576
+ NL_SET_ERR_MSG(extack,
577
+ "PHY not configured. Try setting interface up");
578
+ err = -EBUSY;
579
+ goto out;
580
+ }
581
+
582
+ err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_NTF);
583
+ if (err)
584
+ goto out;
585
+
586
+ /* Mark the carrier down until the test is complete */
587
+ phy_link_down(phydev);
588
+
589
+ netif_testing_on(dev);
590
+ err = phydev->drv->cable_test_start(phydev);
591
+ if (err) {
592
+ netif_testing_off(dev);
593
+ phy_link_up(phydev);
594
+ goto out_free;
595
+ }
596
+
597
+ phydev->state = PHY_CABLETEST;
598
+
599
+ if (phy_polling_mode(phydev))
600
+ phy_trigger_machine(phydev);
601
+
602
+ mutex_unlock(&phydev->lock);
603
+
604
+ return 0;
605
+
606
+out_free:
607
+ ethnl_cable_test_free(phydev);
608
+out:
609
+ mutex_unlock(&phydev->lock);
610
+
611
+ return err;
612
+}
613
+EXPORT_SYMBOL(phy_start_cable_test);
614
+
615
+/**
616
+ * phy_start_cable_test_tdr - Start a raw TDR cable test
617
+ *
618
+ * @phydev: the phy_device struct
619
+ * @extack: extack for reporting useful error messages
620
+ * @config: Configuration of the test to run
621
+ */
622
+int phy_start_cable_test_tdr(struct phy_device *phydev,
623
+ struct netlink_ext_ack *extack,
624
+ const struct phy_tdr_config *config)
625
+{
626
+ struct net_device *dev = phydev->attached_dev;
627
+ int err = -ENOMEM;
628
+
629
+ if (!(phydev->drv &&
630
+ phydev->drv->cable_test_tdr_start &&
631
+ phydev->drv->cable_test_get_status)) {
632
+ NL_SET_ERR_MSG(extack,
633
+ "PHY driver does not support cable test TDR");
634
+ return -EOPNOTSUPP;
635
+ }
636
+
637
+ mutex_lock(&phydev->lock);
638
+ if (phydev->state == PHY_CABLETEST) {
639
+ NL_SET_ERR_MSG(extack,
640
+ "PHY already performing a test");
641
+ err = -EBUSY;
642
+ goto out;
643
+ }
644
+
645
+ if (phydev->state < PHY_UP ||
646
+ phydev->state > PHY_CABLETEST) {
647
+ NL_SET_ERR_MSG(extack,
648
+ "PHY not configured. Try setting interface up");
649
+ err = -EBUSY;
650
+ goto out;
651
+ }
652
+
653
+ err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_TDR_NTF);
654
+ if (err)
655
+ goto out;
656
+
657
+ /* Mark the carrier down until the test is complete */
658
+ phy_link_down(phydev);
659
+
660
+ netif_testing_on(dev);
661
+ err = phydev->drv->cable_test_tdr_start(phydev, config);
662
+ if (err) {
663
+ netif_testing_off(dev);
664
+ phy_link_up(phydev);
665
+ goto out_free;
666
+ }
667
+
668
+ phydev->state = PHY_CABLETEST;
669
+
670
+ if (phy_polling_mode(phydev))
671
+ phy_trigger_machine(phydev);
672
+
673
+ mutex_unlock(&phydev->lock);
674
+
675
+ return 0;
676
+
677
+out_free:
678
+ ethnl_cable_test_free(phydev);
679
+out:
680
+ mutex_unlock(&phydev->lock);
681
+
682
+ return err;
683
+}
684
+EXPORT_SYMBOL(phy_start_cable_test_tdr);
470685
471686 static int phy_config_aneg(struct phy_device *phydev)
472687 {
....@@ -477,68 +692,73 @@
477692 * allowed to call genphy_config_aneg()
478693 */
479694 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
480
- return -EOPNOTSUPP;
695
+ return genphy_c45_config_aneg(phydev);
481696
482697 return genphy_config_aneg(phydev);
483698 }
484699
485700 /**
486
- * phy_start_aneg_priv - start auto-negotiation for this PHY device
701
+ * phy_check_link_status - check link status and set state accordingly
487702 * @phydev: the phy_device struct
488
- * @sync: indicate whether we should wait for the workqueue cancelation
703
+ *
704
+ * Description: Check for link and whether autoneg was triggered / is running
705
+ * and set state accordingly
706
+ */
707
+static int phy_check_link_status(struct phy_device *phydev)
708
+{
709
+ int err;
710
+
711
+ WARN_ON(!mutex_is_locked(&phydev->lock));
712
+
713
+ /* Keep previous state if loopback is enabled because some PHYs
714
+ * report that Link is Down when loopback is enabled.
715
+ */
716
+ if (phydev->loopback_enabled)
717
+ return 0;
718
+
719
+ err = phy_read_status(phydev);
720
+ if (err)
721
+ return err;
722
+
723
+ if (phydev->link && phydev->state != PHY_RUNNING) {
724
+ phy_check_downshift(phydev);
725
+ phydev->state = PHY_RUNNING;
726
+ phy_link_up(phydev);
727
+ } else if (!phydev->link && phydev->state != PHY_NOLINK) {
728
+ phydev->state = PHY_NOLINK;
729
+ phy_link_down(phydev);
730
+ }
731
+
732
+ return 0;
733
+}
734
+
735
+/**
736
+ * _phy_start_aneg - start auto-negotiation for this PHY device
737
+ * @phydev: the phy_device struct
489738 *
490739 * Description: Sanitizes the settings (if we're not autonegotiating
491740 * them), and then calls the driver's config_aneg function.
492741 * If the PHYCONTROL Layer is operating, we change the state to
493742 * reflect the beginning of Auto-negotiation or forcing.
494743 */
495
-static int phy_start_aneg_priv(struct phy_device *phydev, bool sync)
744
+static int _phy_start_aneg(struct phy_device *phydev)
496745 {
497
- bool trigger = 0;
498746 int err;
747
+
748
+ lockdep_assert_held(&phydev->lock);
499749
500750 if (!phydev->drv)
501751 return -EIO;
502752
503
- mutex_lock(&phydev->lock);
504
-
505753 if (AUTONEG_DISABLE == phydev->autoneg)
506754 phy_sanitize_settings(phydev);
507755
508
- /* Invalidate LP advertising flags */
509
- phydev->lp_advertising = 0;
510
-
511756 err = phy_config_aneg(phydev);
512757 if (err < 0)
513
- goto out_unlock;
758
+ return err;
514759
515
- if (phydev->state != PHY_HALTED) {
516
- if (AUTONEG_ENABLE == phydev->autoneg) {
517
- phydev->state = PHY_AN;
518
- phydev->link_timeout = PHY_AN_TIMEOUT;
519
- } else {
520
- phydev->state = PHY_FORCING;
521
- phydev->link_timeout = PHY_FORCE_TIMEOUT;
522
- }
523
- }
524
-
525
- /* Re-schedule a PHY state machine to check PHY status because
526
- * negotiation may already be done and aneg interrupt may not be
527
- * generated.
528
- */
529
- if (!phy_polling_mode(phydev) && phydev->state == PHY_AN) {
530
- err = phy_aneg_done(phydev);
531
- if (err > 0) {
532
- trigger = true;
533
- err = 0;
534
- }
535
- }
536
-
537
-out_unlock:
538
- mutex_unlock(&phydev->lock);
539
-
540
- if (trigger)
541
- phy_trigger_machine(phydev, sync);
760
+ if (phy_is_started(phydev))
761
+ err = phy_check_link_status(phydev);
542762
543763 return err;
544764 }
....@@ -554,7 +774,13 @@
554774 */
555775 int phy_start_aneg(struct phy_device *phydev)
556776 {
557
- return phy_start_aneg_priv(phydev, true);
777
+ int err;
778
+
779
+ mutex_lock(&phydev->lock);
780
+ err = _phy_start_aneg(phydev);
781
+ mutex_unlock(&phydev->lock);
782
+
783
+ return err;
558784 }
559785 EXPORT_SYMBOL(phy_start_aneg);
560786
....@@ -574,6 +800,66 @@
574800 return ret < 0 ? ret : 0;
575801 }
576802
803
+int phy_ethtool_ksettings_set(struct phy_device *phydev,
804
+ const struct ethtool_link_ksettings *cmd)
805
+{
806
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
807
+ u8 autoneg = cmd->base.autoneg;
808
+ u8 duplex = cmd->base.duplex;
809
+ u32 speed = cmd->base.speed;
810
+
811
+ if (cmd->base.phy_address != phydev->mdio.addr)
812
+ return -EINVAL;
813
+
814
+ linkmode_copy(advertising, cmd->link_modes.advertising);
815
+
816
+ /* We make sure that we don't pass unsupported values in to the PHY */
817
+ linkmode_and(advertising, advertising, phydev->supported);
818
+
819
+ /* Verify the settings we care about. */
820
+ if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
821
+ return -EINVAL;
822
+
823
+ if (autoneg == AUTONEG_ENABLE && linkmode_empty(advertising))
824
+ return -EINVAL;
825
+
826
+ if (autoneg == AUTONEG_DISABLE &&
827
+ ((speed != SPEED_1000 &&
828
+ speed != SPEED_100 &&
829
+ speed != SPEED_10) ||
830
+ (duplex != DUPLEX_HALF &&
831
+ duplex != DUPLEX_FULL)))
832
+ return -EINVAL;
833
+
834
+ mutex_lock(&phydev->lock);
835
+ phydev->autoneg = autoneg;
836
+
837
+ if (autoneg == AUTONEG_DISABLE) {
838
+ phydev->speed = speed;
839
+ phydev->duplex = duplex;
840
+ }
841
+
842
+ linkmode_copy(phydev->advertising, advertising);
843
+
844
+ linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
845
+ phydev->advertising, autoneg == AUTONEG_ENABLE);
846
+
847
+ phydev->master_slave_set = cmd->base.master_slave_cfg;
848
+ phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
849
+
850
+ /* Restart the PHY */
851
+ if (phy_is_started(phydev)) {
852
+ phydev->state = PHY_UP;
853
+ phy_trigger_machine(phydev);
854
+ } else {
855
+ _phy_start_aneg(phydev);
856
+ }
857
+
858
+ mutex_unlock(&phydev->lock);
859
+ return 0;
860
+}
861
+EXPORT_SYMBOL(phy_ethtool_ksettings_set);
862
+
577863 /**
578864 * phy_speed_down - set speed to lowest speed supported by both link partners
579865 * @phydev: the phy_device struct
....@@ -589,20 +875,21 @@
589875 */
590876 int phy_speed_down(struct phy_device *phydev, bool sync)
591877 {
592
- u32 adv = phydev->lp_advertising & phydev->supported;
593
- u32 adv_old = phydev->advertising;
878
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
594879 int ret;
595880
596881 if (phydev->autoneg != AUTONEG_ENABLE)
597882 return 0;
598883
599
- if (adv & PHY_10BT_FEATURES)
600
- phydev->advertising &= ~(PHY_100BT_FEATURES |
601
- PHY_1000BT_FEATURES);
602
- else if (adv & PHY_100BT_FEATURES)
603
- phydev->advertising &= ~PHY_1000BT_FEATURES;
884
+ linkmode_copy(adv_tmp, phydev->advertising);
604885
605
- if (phydev->advertising == adv_old)
886
+ ret = phy_speed_down_core(phydev);
887
+ if (ret)
888
+ return ret;
889
+
890
+ linkmode_copy(phydev->adv_old, adv_tmp);
891
+
892
+ if (linkmode_equal(phydev->advertising, adv_tmp))
606893 return 0;
607894
608895 ret = phy_config_aneg(phydev);
....@@ -621,15 +908,19 @@
621908 */
622909 int phy_speed_up(struct phy_device *phydev)
623910 {
624
- u32 mask = PHY_10BT_FEATURES | PHY_100BT_FEATURES | PHY_1000BT_FEATURES;
625
- u32 adv_old = phydev->advertising;
911
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
626912
627913 if (phydev->autoneg != AUTONEG_ENABLE)
628914 return 0;
629915
630
- phydev->advertising = (adv_old & ~mask) | (phydev->supported & mask);
916
+ if (linkmode_empty(phydev->adv_old))
917
+ return 0;
631918
632
- if (phydev->advertising == adv_old)
919
+ linkmode_copy(adv_tmp, phydev->advertising);
920
+ linkmode_copy(phydev->advertising, phydev->adv_old);
921
+ linkmode_zero(phydev->adv_old);
922
+
923
+ if (linkmode_equal(phydev->advertising, adv_tmp))
633924 return 0;
634925
635926 return phy_config_aneg(phydev);
....@@ -648,28 +939,9 @@
648939 */
649940 void phy_start_machine(struct phy_device *phydev)
650941 {
651
- queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
942
+ phy_trigger_machine(phydev);
652943 }
653944 EXPORT_SYMBOL_GPL(phy_start_machine);
654
-
655
-/**
656
- * phy_trigger_machine - trigger the state machine to run
657
- *
658
- * @phydev: the phy_device struct
659
- * @sync: indicate whether we should wait for the workqueue cancelation
660
- *
661
- * Description: There has been a change in state which requires that the
662
- * state machine runs.
663
- */
664
-
665
-void phy_trigger_machine(struct phy_device *phydev, bool sync)
666
-{
667
- if (sync)
668
- cancel_delayed_work_sync(&phydev->state_queue);
669
- else
670
- cancel_delayed_work(&phydev->state_queue);
671
- queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
672
-}
673945
674946 /**
675947 * phy_stop_machine - stop the PHY state machine tracking
....@@ -684,7 +956,7 @@
684956 cancel_delayed_work_sync(&phydev->state_queue);
685957
686958 mutex_lock(&phydev->lock);
687
- if (phydev->state > PHY_UP && phydev->state != PHY_HALTED)
959
+ if (phy_is_started(phydev))
688960 phydev->state = PHY_UP;
689961 mutex_unlock(&phydev->lock);
690962 }
....@@ -700,18 +972,20 @@
700972 */
701973 static void phy_error(struct phy_device *phydev)
702974 {
975
+ WARN_ON(1);
976
+
703977 mutex_lock(&phydev->lock);
704978 phydev->state = PHY_HALTED;
705979 mutex_unlock(&phydev->lock);
706980
707
- phy_trigger_machine(phydev, false);
981
+ phy_trigger_machine(phydev);
708982 }
709983
710984 /**
711985 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
712986 * @phydev: target phy_device struct
713987 */
714
-static int phy_disable_interrupts(struct phy_device *phydev)
988
+int phy_disable_interrupts(struct phy_device *phydev)
715989 {
716990 int err;
717991
....@@ -725,48 +999,33 @@
725999 }
7261000
7271001 /**
728
- * phy_change - Called by the phy_interrupt to handle PHY changes
729
- * @phydev: phy_device struct that interrupted
1002
+ * phy_did_interrupt - Checks if the PHY generated an interrupt
1003
+ * @phydev: target phy_device struct
7301004 */
731
-static irqreturn_t phy_change(struct phy_device *phydev)
1005
+static int phy_did_interrupt(struct phy_device *phydev)
7321006 {
733
- if (phy_interrupt_is_valid(phydev)) {
734
- if (phydev->drv->did_interrupt &&
735
- !phydev->drv->did_interrupt(phydev))
736
- return IRQ_NONE;
737
-
738
- if (phydev->state == PHY_HALTED)
739
- if (phy_disable_interrupts(phydev))
740
- goto phy_err;
741
- }
1007
+ int ret;
7421008
7431009 mutex_lock(&phydev->lock);
744
- if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
745
- phydev->state = PHY_CHANGELINK;
1010
+ ret = phydev->drv->did_interrupt(phydev);
7461011 mutex_unlock(&phydev->lock);
7471012
748
- /* reschedule state queue work to run as soon as possible */
749
- phy_trigger_machine(phydev, true);
750
-
751
- if (phy_interrupt_is_valid(phydev) && phy_clear_interrupt(phydev))
752
- goto phy_err;
753
- return IRQ_HANDLED;
754
-
755
-phy_err:
756
- phy_error(phydev);
757
- return IRQ_NONE;
1013
+ return ret;
7581014 }
7591015
7601016 /**
761
- * phy_change_work - Scheduled by the phy_mac_interrupt to handle PHY changes
762
- * @work: work_struct that describes the work to be done
1017
+ * phy_handle_interrupt - Handle PHY interrupt
1018
+ * @phydev: target phy_device struct
7631019 */
764
-void phy_change_work(struct work_struct *work)
1020
+static irqreturn_t phy_handle_interrupt(struct phy_device *phydev)
7651021 {
766
- struct phy_device *phydev =
767
- container_of(work, struct phy_device, phy_queue);
1022
+ irqreturn_t ret;
7681023
769
- phy_change(phydev);
1024
+ mutex_lock(&phydev->lock);
1025
+ ret = phydev->drv->handle_interrupt(phydev);
1026
+ mutex_unlock(&phydev->lock);
1027
+
1028
+ return ret;
7701029 }
7711030
7721031 /**
....@@ -774,17 +1033,29 @@
7741033 * @irq: interrupt line
7751034 * @phy_dat: phy_device pointer
7761035 *
777
- * Description: When a PHY interrupt occurs, the handler disables
778
- * interrupts, and uses phy_change to handle the interrupt.
1036
+ * Description: Handle PHY interrupt
7791037 */
7801038 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
7811039 {
7821040 struct phy_device *phydev = phy_dat;
1041
+ struct phy_driver *drv = phydev->drv;
7831042
784
- if (PHY_HALTED == phydev->state)
785
- return IRQ_NONE; /* It can't be ours. */
1043
+ if (drv->handle_interrupt)
1044
+ return phy_handle_interrupt(phydev);
7861045
787
- return phy_change(phydev);
1046
+ if (drv->did_interrupt && !phy_did_interrupt(phydev))
1047
+ return IRQ_NONE;
1048
+
1049
+ /* reschedule state queue work to run as soon as possible */
1050
+ phy_trigger_machine(phydev);
1051
+
1052
+ /* did_interrupt() may have cleared the interrupt already */
1053
+ if (!drv->did_interrupt && phy_clear_interrupt(phydev)) {
1054
+ phy_error(phydev);
1055
+ return IRQ_NONE;
1056
+ }
1057
+
1058
+ return IRQ_HANDLED;
7881059 }
7891060
7901061 /**
....@@ -802,46 +1073,47 @@
8021073 }
8031074
8041075 /**
805
- * phy_start_interrupts - request and enable interrupts for a PHY device
1076
+ * phy_request_interrupt - request and enable interrupt for a PHY device
8061077 * @phydev: target phy_device struct
8071078 *
808
- * Description: Request the interrupt for the given PHY.
1079
+ * Description: Request and enable the interrupt for the given PHY.
8091080 * If this fails, then we set irq to PHY_POLL.
810
- * Otherwise, we enable the interrupts in the PHY.
8111081 * This should only be called with a valid IRQ number.
812
- * Returns 0 on success or < 0 on error.
8131082 */
814
-int phy_start_interrupts(struct phy_device *phydev)
1083
+void phy_request_interrupt(struct phy_device *phydev)
8151084 {
816
- if (request_threaded_irq(phydev->irq, NULL, phy_interrupt,
817
- IRQF_ONESHOT | IRQF_SHARED,
818
- phydev_name(phydev), phydev) < 0) {
819
- pr_warn("%s: Can't get IRQ %d (PHY)\n",
820
- phydev->mdio.bus->name, phydev->irq);
821
- phydev->irq = PHY_POLL;
822
- return 0;
823
- }
1085
+ int err;
8241086
825
- return phy_enable_interrupts(phydev);
1087
+ err = request_threaded_irq(phydev->irq, NULL, phy_interrupt,
1088
+ IRQF_ONESHOT | IRQF_SHARED,
1089
+ phydev_name(phydev), phydev);
1090
+ if (err) {
1091
+ phydev_warn(phydev, "Error %d requesting IRQ %d, falling back to polling\n",
1092
+ err, phydev->irq);
1093
+ phydev->irq = PHY_POLL;
1094
+ } else {
1095
+ if (phy_enable_interrupts(phydev)) {
1096
+ phydev_warn(phydev, "Can't enable interrupt, falling back to polling\n");
1097
+ phy_free_interrupt(phydev);
1098
+ phydev->irq = PHY_POLL;
1099
+ }
1100
+ }
8261101 }
827
-EXPORT_SYMBOL(phy_start_interrupts);
1102
+EXPORT_SYMBOL(phy_request_interrupt);
8281103
8291104 /**
830
- * phy_stop_interrupts - disable interrupts from a PHY device
1105
+ * phy_free_interrupt - disable and free interrupt for a PHY device
8311106 * @phydev: target phy_device struct
1107
+ *
1108
+ * Description: Disable and free the interrupt for the given PHY.
1109
+ * This should only be called with a valid IRQ number.
8321110 */
833
-int phy_stop_interrupts(struct phy_device *phydev)
1111
+void phy_free_interrupt(struct phy_device *phydev)
8341112 {
835
- int err = phy_disable_interrupts(phydev);
836
-
837
- if (err)
838
- phy_error(phydev);
839
-
1113
+ phy_disable_interrupts(phydev);
8401114 free_irq(phydev->irq, phydev);
841
-
842
- return err;
8431115 }
844
-EXPORT_SYMBOL(phy_stop_interrupts);
1116
+EXPORT_SYMBOL(phy_free_interrupt);
8451117
8461118 /**
8471119 * phy_stop - Bring down the PHY link, and stop checking the status
....@@ -849,21 +1121,36 @@
8491121 */
8501122 void phy_stop(struct phy_device *phydev)
8511123 {
1124
+ struct net_device *dev = phydev->attached_dev;
1125
+ enum phy_state old_state;
1126
+
1127
+ if (!phy_is_started(phydev) && phydev->state != PHY_DOWN) {
1128
+ WARN(1, "called from state %s\n",
1129
+ phy_state_to_str(phydev->state));
1130
+ return;
1131
+ }
1132
+
8521133 mutex_lock(&phydev->lock);
1134
+ old_state = phydev->state;
8531135
854
- if (PHY_HALTED == phydev->state)
855
- goto out_unlock;
1136
+ if (phydev->state == PHY_CABLETEST) {
1137
+ phy_abort_cable_test(phydev);
1138
+ netif_testing_off(dev);
1139
+ }
8561140
857
- if (phy_interrupt_is_valid(phydev))
858
- phy_disable_interrupts(phydev);
1141
+ if (phydev->sfp_bus)
1142
+ sfp_upstream_stop(phydev->sfp_bus);
8591143
8601144 phydev->state = PHY_HALTED;
1145
+ phy_process_state_change(phydev, old_state);
8611146
862
-out_unlock:
8631147 mutex_unlock(&phydev->lock);
8641148
1149
+ phy_state_machine(&phydev->state_queue.work);
1150
+ phy_stop_machine(phydev);
1151
+
8651152 /* Cannot call flush_scheduled_work() here as desired because
866
- * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
1153
+ * of rtnl_lock(), but PHY_HALTED shall guarantee irq handler
8671154 * will not reenable interrupts.
8681155 */
8691156 }
....@@ -881,50 +1168,27 @@
8811168 */
8821169 void phy_start(struct phy_device *phydev)
8831170 {
884
- int err = 0;
885
-
8861171 mutex_lock(&phydev->lock);
8871172
888
- switch (phydev->state) {
889
- case PHY_STARTING:
890
- phydev->state = PHY_PENDING;
891
- break;
892
- case PHY_READY:
893
- phydev->state = PHY_UP;
894
- break;
895
- case PHY_HALTED:
896
- /* if phy was suspended, bring the physical link up again */
897
- __phy_resume(phydev);
898
-
899
- /* make sure interrupts are re-enabled for the PHY */
900
- if (phy_interrupt_is_valid(phydev)) {
901
- err = phy_enable_interrupts(phydev);
902
- if (err < 0)
903
- break;
904
- }
905
-
906
- phydev->state = PHY_RESUMING;
907
- break;
908
- default:
909
- break;
1173
+ if (phydev->state != PHY_READY && phydev->state != PHY_HALTED) {
1174
+ WARN(1, "called from state %s\n",
1175
+ phy_state_to_str(phydev->state));
1176
+ goto out;
9101177 }
911
- mutex_unlock(&phydev->lock);
9121178
913
- phy_trigger_machine(phydev, true);
1179
+ if (phydev->sfp_bus)
1180
+ sfp_upstream_start(phydev->sfp_bus);
1181
+
1182
+ /* if phy was suspended, bring the physical link up again */
1183
+ __phy_resume(phydev);
1184
+
1185
+ phydev->state = PHY_UP;
1186
+
1187
+ phy_start_machine(phydev);
1188
+out:
1189
+ mutex_unlock(&phydev->lock);
9141190 }
9151191 EXPORT_SYMBOL(phy_start);
916
-
917
-static void phy_link_up(struct phy_device *phydev)
918
-{
919
- phydev->phy_link_change(phydev, true, true);
920
- phy_led_trigger_change_speed(phydev);
921
-}
922
-
923
-static void phy_link_down(struct phy_device *phydev, bool do_carrier)
924
-{
925
- phydev->phy_link_change(phydev, false, do_carrier);
926
- phy_led_trigger_change_speed(phydev);
927
-}
9281192
9291193 /**
9301194 * phy_state_machine - Handle the state machine
....@@ -935,198 +1199,78 @@
9351199 struct delayed_work *dwork = to_delayed_work(work);
9361200 struct phy_device *phydev =
9371201 container_of(dwork, struct phy_device, state_queue);
1202
+ struct net_device *dev = phydev->attached_dev;
9381203 bool needs_aneg = false, do_suspend = false;
9391204 enum phy_state old_state;
1205
+ bool finished = false;
9401206 int err = 0;
941
- int old_link;
9421207
9431208 mutex_lock(&phydev->lock);
9441209
9451210 old_state = phydev->state;
9461211
947
- if (phydev->drv && phydev->drv->link_change_notify)
948
- phydev->drv->link_change_notify(phydev);
949
-
9501212 switch (phydev->state) {
9511213 case PHY_DOWN:
952
- case PHY_STARTING:
9531214 case PHY_READY:
954
- case PHY_PENDING:
9551215 break;
9561216 case PHY_UP:
9571217 needs_aneg = true;
9581218
959
- phydev->link_timeout = PHY_AN_TIMEOUT;
960
-
961
- break;
962
- case PHY_AN:
963
- err = phy_read_status(phydev);
964
- if (err < 0)
965
- break;
966
-
967
- /* If the link is down, give up on negotiation for now */
968
- if (!phydev->link) {
969
- phydev->state = PHY_NOLINK;
970
- phy_link_down(phydev, true);
971
- break;
972
- }
973
-
974
- /* Check if negotiation is done. Break if there's an error */
975
- err = phy_aneg_done(phydev);
976
- if (err < 0)
977
- break;
978
-
979
- /* If AN is done, we're running */
980
- if (err > 0) {
981
- phydev->state = PHY_RUNNING;
982
- phy_link_up(phydev);
983
- } else if (0 == phydev->link_timeout--)
984
- needs_aneg = true;
9851219 break;
9861220 case PHY_NOLINK:
987
- if (!phy_polling_mode(phydev))
988
- break;
989
-
990
- err = phy_read_status(phydev);
991
- if (err)
992
- break;
993
-
994
- if (phydev->link) {
995
- if (AUTONEG_ENABLE == phydev->autoneg) {
996
- err = phy_aneg_done(phydev);
997
- if (err < 0)
998
- break;
999
-
1000
- if (!err) {
1001
- phydev->state = PHY_AN;
1002
- phydev->link_timeout = PHY_AN_TIMEOUT;
1003
- break;
1004
- }
1005
- }
1006
- phydev->state = PHY_RUNNING;
1007
- phy_link_up(phydev);
1008
- }
1009
- break;
1010
- case PHY_FORCING:
1011
- err = genphy_update_link(phydev);
1012
- if (err)
1013
- break;
1014
-
1015
- if (phydev->link) {
1016
- phydev->state = PHY_RUNNING;
1017
- phy_link_up(phydev);
1018
- } else {
1019
- if (0 == phydev->link_timeout--)
1020
- needs_aneg = true;
1021
- phy_link_down(phydev, false);
1022
- }
1023
- break;
10241221 case PHY_RUNNING:
1025
- /* Only register a CHANGE if we are polling and link changed
1026
- * since latest checking.
1027
- */
1028
- if (phy_polling_mode(phydev)) {
1029
- old_link = phydev->link;
1030
- err = phy_read_status(phydev);
1031
- if (err)
1032
- break;
1033
-
1034
- if (old_link != phydev->link)
1035
- phydev->state = PHY_CHANGELINK;
1036
- }
1037
- /*
1038
- * Failsafe: check that nobody set phydev->link=0 between two
1039
- * poll cycles, otherwise we won't leave RUNNING state as long
1040
- * as link remains down.
1041
- */
1042
- if (!phydev->link && phydev->state == PHY_RUNNING) {
1043
- phydev->state = PHY_CHANGELINK;
1044
- phydev_err(phydev, "no link in PHY_RUNNING\n");
1045
- }
1222
+ err = phy_check_link_status(phydev);
10461223 break;
1047
- case PHY_CHANGELINK:
1048
- err = phy_read_status(phydev);
1049
- if (err)
1224
+ case PHY_CABLETEST:
1225
+ err = phydev->drv->cable_test_get_status(phydev, &finished);
1226
+ if (err) {
1227
+ phy_abort_cable_test(phydev);
1228
+ netif_testing_off(dev);
1229
+ needs_aneg = true;
1230
+ phydev->state = PHY_UP;
10501231 break;
1232
+ }
10511233
1052
- if (phydev->link) {
1053
- phydev->state = PHY_RUNNING;
1054
- phy_link_up(phydev);
1055
- } else {
1056
- phydev->state = PHY_NOLINK;
1057
- phy_link_down(phydev, true);
1234
+ if (finished) {
1235
+ ethnl_cable_test_finished(phydev);
1236
+ netif_testing_off(dev);
1237
+ needs_aneg = true;
1238
+ phydev->state = PHY_UP;
10581239 }
10591240 break;
10601241 case PHY_HALTED:
10611242 if (phydev->link) {
10621243 phydev->link = 0;
1063
- phy_link_down(phydev, true);
1064
- do_suspend = true;
1244
+ phy_link_down(phydev);
10651245 }
1066
- break;
1067
- case PHY_RESUMING:
1068
- if (AUTONEG_ENABLE == phydev->autoneg) {
1069
- err = phy_aneg_done(phydev);
1070
- if (err < 0)
1071
- break;
1072
-
1073
- /* err > 0 if AN is done.
1074
- * Otherwise, it's 0, and we're still waiting for AN
1075
- */
1076
- if (err > 0) {
1077
- err = phy_read_status(phydev);
1078
- if (err)
1079
- break;
1080
-
1081
- if (phydev->link) {
1082
- phydev->state = PHY_RUNNING;
1083
- phy_link_up(phydev);
1084
- } else {
1085
- phydev->state = PHY_NOLINK;
1086
- phy_link_down(phydev, false);
1087
- }
1088
- } else {
1089
- phydev->state = PHY_AN;
1090
- phydev->link_timeout = PHY_AN_TIMEOUT;
1091
- }
1092
- } else {
1093
- err = phy_read_status(phydev);
1094
- if (err)
1095
- break;
1096
-
1097
- if (phydev->link) {
1098
- phydev->state = PHY_RUNNING;
1099
- phy_link_up(phydev);
1100
- } else {
1101
- phydev->state = PHY_NOLINK;
1102
- phy_link_down(phydev, false);
1103
- }
1104
- }
1246
+ do_suspend = true;
11051247 break;
11061248 }
11071249
11081250 mutex_unlock(&phydev->lock);
11091251
11101252 if (needs_aneg)
1111
- err = phy_start_aneg_priv(phydev, false);
1253
+ err = phy_start_aneg(phydev);
11121254 else if (do_suspend)
11131255 phy_suspend(phydev);
11141256
11151257 if (err < 0)
11161258 phy_error(phydev);
11171259
1118
- if (old_state != phydev->state)
1119
- phydev_dbg(phydev, "PHY state change %s -> %s\n",
1120
- phy_state_to_str(old_state),
1121
- phy_state_to_str(phydev->state));
1260
+ phy_process_state_change(phydev, old_state);
11221261
11231262 /* Only re-schedule a PHY state machine change if we are polling the
11241263 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
1125
- * between states from phy_mac_interrupt()
1264
+ * between states from phy_mac_interrupt().
1265
+ *
1266
+ * In state PHY_HALTED the PHY gets suspended, so rescheduling the
1267
+ * state machine would be pointless and possibly error prone when
1268
+ * called from phy_disconnect() synchronously.
11261269 */
1127
- if (phy_polling_mode(phydev))
1128
- queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
1129
- PHY_STATE_TIME * HZ);
1270
+ mutex_lock(&phydev->lock);
1271
+ if (phy_polling_mode(phydev) && phy_is_started(phydev))
1272
+ phy_queue_state_machine(phydev, PHY_STATE_TIME);
1273
+ mutex_unlock(&phydev->lock);
11301274 }
11311275
11321276 /**
....@@ -1139,9 +1283,33 @@
11391283 void phy_mac_interrupt(struct phy_device *phydev)
11401284 {
11411285 /* Trigger a state machine change */
1142
- queue_work(system_power_efficient_wq, &phydev->phy_queue);
1286
+ phy_trigger_machine(phydev);
11431287 }
11441288 EXPORT_SYMBOL(phy_mac_interrupt);
1289
+
1290
+static void mmd_eee_adv_to_linkmode(unsigned long *advertising, u16 eee_adv)
1291
+{
1292
+ linkmode_zero(advertising);
1293
+
1294
+ if (eee_adv & MDIO_EEE_100TX)
1295
+ linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
1296
+ advertising);
1297
+ if (eee_adv & MDIO_EEE_1000T)
1298
+ linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1299
+ advertising);
1300
+ if (eee_adv & MDIO_EEE_10GT)
1301
+ linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
1302
+ advertising);
1303
+ if (eee_adv & MDIO_EEE_1000KX)
1304
+ linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
1305
+ advertising);
1306
+ if (eee_adv & MDIO_EEE_10GKX4)
1307
+ linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
1308
+ advertising);
1309
+ if (eee_adv & MDIO_EEE_10GKR)
1310
+ linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
1311
+ advertising);
1312
+}
11451313
11461314 /**
11471315 * phy_init_eee - init and check the EEE feature
....@@ -1161,9 +1329,12 @@
11611329 /* According to 802.3az,the EEE is supported only in full duplex-mode.
11621330 */
11631331 if (phydev->duplex == DUPLEX_FULL) {
1332
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
1333
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(lp);
1334
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(adv);
11641335 int eee_lp, eee_cap, eee_adv;
1165
- u32 lp, cap, adv;
11661336 int status;
1337
+ u32 cap;
11671338
11681339 /* Read phy status to properly get the right settings */
11691340 status = phy_read_status(phydev);
....@@ -1190,22 +1361,19 @@
11901361 if (eee_adv <= 0)
11911362 goto eee_exit_err;
11921363
1193
- adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
1194
- lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
1195
- if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv))
1364
+ mmd_eee_adv_to_linkmode(adv, eee_adv);
1365
+ mmd_eee_adv_to_linkmode(lp, eee_lp);
1366
+ linkmode_and(common, adv, lp);
1367
+
1368
+ if (!phy_check_valid(phydev->speed, phydev->duplex, common))
11961369 goto eee_exit_err;
11971370
1198
- if (clk_stop_enable) {
1371
+ if (clk_stop_enable)
11991372 /* Configure the PHY to stop receiving xMII
12001373 * clock while it is signaling LPI.
12011374 */
1202
- int val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
1203
- if (val < 0)
1204
- return val;
1205
-
1206
- val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
1207
- phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, val);
1208
- }
1375
+ phy_set_bits_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1,
1376
+ MDIO_PCS_CTRL1_CLKSTOP_EN);
12091377
12101378 return 0; /* EEE supported */
12111379 }
....@@ -1256,12 +1424,15 @@
12561424 if (val < 0)
12571425 return val;
12581426 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1427
+ data->eee_enabled = !!data->advertised;
12591428
12601429 /* Get LP advertisement EEE */
12611430 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
12621431 if (val < 0)
12631432 return val;
12641433 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1434
+
1435
+ data->eee_active = !!(data->advertised & data->lp_advertised);
12651436
12661437 return 0;
12671438 }
....@@ -1276,7 +1447,7 @@
12761447 */
12771448 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
12781449 {
1279
- int cap, old_adv, adv, ret;
1450
+ int cap, old_adv, adv = 0, ret;
12801451
12811452 if (!phydev->drv)
12821453 return -EIO;
....@@ -1290,10 +1461,12 @@
12901461 if (old_adv < 0)
12911462 return old_adv;
12921463
1293
- adv = ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
1294
-
1295
- /* Mask prohibited EEE modes */
1296
- adv &= ~phydev->eee_broken_modes;
1464
+ if (data->eee_enabled) {
1465
+ adv = !data->advertised ? cap :
1466
+ ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
1467
+ /* Mask prohibited EEE modes */
1468
+ adv &= ~phydev->eee_broken_modes;
1469
+ }
12971470
12981471 if (old_adv != adv) {
12991472 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
....@@ -1314,6 +1487,12 @@
13141487 }
13151488 EXPORT_SYMBOL(phy_ethtool_set_eee);
13161489
1490
+/**
1491
+ * phy_ethtool_set_wol - Configure Wake On LAN
1492
+ *
1493
+ * @phydev: target phy_device struct
1494
+ * @wol: Configuration requested
1495
+ */
13171496 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
13181497 {
13191498 if (phydev->drv && phydev->drv->set_wol)
....@@ -1323,6 +1502,12 @@
13231502 }
13241503 EXPORT_SYMBOL(phy_ethtool_set_wol);
13251504
1505
+/**
1506
+ * phy_ethtool_get_wol - Get the current Wake On LAN configuration
1507
+ *
1508
+ * @phydev: target phy_device struct
1509
+ * @wol: Store the current configuration here
1510
+ */
13261511 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
13271512 {
13281513 if (phydev->drv && phydev->drv->get_wol)
....@@ -1356,6 +1541,10 @@
13561541 }
13571542 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
13581543
1544
+/**
1545
+ * phy_ethtool_nway_reset - Restart auto negotiation
1546
+ * @ndev: Network device to restart autoneg for
1547
+ */
13591548 int phy_ethtool_nway_reset(struct net_device *ndev)
13601549 {
13611550 struct phy_device *phydev = ndev->phydev;