forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
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,54 @@
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_link_up(struct phy_device *phydev)
60
+{
61
+ phydev->phy_link_change(phydev, true);
62
+ phy_led_trigger_change_speed(phydev);
63
+}
64
+
65
+static void phy_link_down(struct phy_device *phydev)
66
+{
67
+ phydev->phy_link_change(phydev, false);
68
+ phy_led_trigger_change_speed(phydev);
69
+}
70
+
71
+static const char *phy_pause_str(struct phy_device *phydev)
72
+{
73
+ bool local_pause, local_asym_pause;
74
+
75
+ if (phydev->autoneg == AUTONEG_DISABLE)
76
+ goto no_pause;
77
+
78
+ local_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
79
+ phydev->advertising);
80
+ local_asym_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
81
+ phydev->advertising);
82
+
83
+ if (local_pause && phydev->pause)
84
+ return "rx/tx";
85
+
86
+ if (local_asym_pause && phydev->asym_pause) {
87
+ if (local_pause)
88
+ return "rx";
89
+ if (phydev->pause)
90
+ return "tx";
91
+ }
92
+
93
+no_pause:
94
+ return "off";
95
+}
6596
6697 /**
6798 * phy_print_status - Convenience function to print out the current phy status
....@@ -71,10 +102,11 @@
71102 {
72103 if (phydev->link) {
73104 netdev_info(phydev->attached_dev,
74
- "Link is Up - %s/%s - flow control %s\n",
105
+ "Link is Up - %s/%s %s- flow control %s\n",
75106 phy_speed_to_str(phydev->speed),
76107 phy_duplex_to_str(phydev->duplex),
77
- phydev->pause ? "rx/tx" : "off");
108
+ phydev->downshifted_rate ? "(downshifted) " : "",
109
+ phy_pause_str(phydev));
78110 } else {
79111 netdev_info(phydev->attached_dev, "Link is Down\n");
80112 }
....@@ -92,10 +124,15 @@
92124 */
93125 static int phy_clear_interrupt(struct phy_device *phydev)
94126 {
95
- if (phydev->drv->ack_interrupt)
96
- return phydev->drv->ack_interrupt(phydev);
127
+ int ret = 0;
97128
98
- return 0;
129
+ if (phydev->drv->ack_interrupt) {
130
+ mutex_lock(&phydev->lock);
131
+ ret = phydev->drv->ack_interrupt(phydev);
132
+ mutex_unlock(&phydev->lock);
133
+ }
134
+
135
+ return ret;
99136 }
100137
101138 /**
....@@ -105,9 +142,9 @@
105142 *
106143 * Returns 0 on success or < 0 on error.
107144 */
108
-static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
145
+static int phy_config_interrupt(struct phy_device *phydev, bool interrupts)
109146 {
110
- phydev->interrupts = interrupts;
147
+ phydev->interrupts = interrupts ? 1 : 0;
111148 if (phydev->drv->config_intr)
112149 return phydev->drv->config_intr(phydev);
113150
....@@ -146,14 +183,10 @@
146183 {
147184 if (phydev->drv && phydev->drv->aneg_done)
148185 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);
186
+ else if (phydev->is_c45)
187
+ return genphy_c45_aneg_done(phydev);
188
+ else
189
+ return genphy_aneg_done(phydev);
157190 }
158191 EXPORT_SYMBOL(phy_aneg_done);
159192
....@@ -171,11 +204,9 @@
171204 * settings were found.
172205 */
173206 static const struct phy_setting *
174
-phy_find_valid(int speed, int duplex, u32 supported)
207
+phy_find_valid(int speed, int duplex, unsigned long *supported)
175208 {
176
- unsigned long mask = supported;
177
-
178
- return phy_lookup_setting(speed, duplex, &mask, BITS_PER_LONG, false);
209
+ return phy_lookup_setting(speed, duplex, supported, false);
179210 }
180211
181212 /**
....@@ -192,9 +223,7 @@
192223 unsigned int *speeds,
193224 unsigned int size)
194225 {
195
- unsigned long supported = phy->supported;
196
-
197
- return phy_speeds(speeds, size, &supported, BITS_PER_LONG);
226
+ return phy_speeds(speeds, size, phy->supported);
198227 }
199228
200229 /**
....@@ -206,11 +235,10 @@
206235 *
207236 * Description: Returns true if there is a valid setting, false otherwise.
208237 */
209
-static inline bool phy_check_valid(int speed, int duplex, u32 features)
238
+static inline bool phy_check_valid(int speed, int duplex,
239
+ unsigned long *features)
210240 {
211
- unsigned long mask = features;
212
-
213
- return !!phy_lookup_setting(speed, duplex, &mask, BITS_PER_LONG, true);
241
+ return !!phy_lookup_setting(speed, duplex, features, true);
214242 }
215243
216244 /**
....@@ -224,13 +252,9 @@
224252 static void phy_sanitize_settings(struct phy_device *phydev)
225253 {
226254 const struct phy_setting *setting;
227
- u32 features = phydev->supported;
228255
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);
256
+ setting = phy_find_valid(phydev->speed, phydev->duplex,
257
+ phydev->supported);
234258 if (setting) {
235259 phydev->speed = setting->speed;
236260 phydev->duplex = setting->duplex;
....@@ -241,145 +265,29 @@
241265 }
242266 }
243267
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
-
359268 void phy_ethtool_ksettings_get(struct phy_device *phydev,
360269 struct ethtool_link_ksettings *cmd)
361270 {
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);
271
+ mutex_lock(&phydev->lock);
272
+ linkmode_copy(cmd->link_modes.supported, phydev->supported);
273
+ linkmode_copy(cmd->link_modes.advertising, phydev->advertising);
274
+ linkmode_copy(cmd->link_modes.lp_advertising, phydev->lp_advertising);
370275
371276 cmd->base.speed = phydev->speed;
372277 cmd->base.duplex = phydev->duplex;
278
+ cmd->base.master_slave_cfg = phydev->master_slave_get;
279
+ cmd->base.master_slave_state = phydev->master_slave_state;
373280 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
374281 cmd->base.port = PORT_BNC;
375282 else
376
- cmd->base.port = PORT_MII;
283
+ cmd->base.port = phydev->port;
377284 cmd->base.transceiver = phy_is_internal(phydev) ?
378285 XCVR_INTERNAL : XCVR_EXTERNAL;
379286 cmd->base.phy_address = phydev->mdio.addr;
380287 cmd->base.autoneg = phydev->autoneg;
381288 cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
382289 cmd->base.eth_tp_mdix = phydev->mdix;
290
+ mutex_unlock(&phydev->lock);
383291 }
384292 EXPORT_SYMBOL(phy_ethtool_ksettings_get);
385293
....@@ -398,21 +306,37 @@
398306 struct mii_ioctl_data *mii_data = if_mii(ifr);
399307 u16 val = mii_data->val_in;
400308 bool change_autoneg = false;
309
+ int prtad, devad;
401310
402311 switch (cmd) {
403312 case SIOCGMIIPHY:
404313 mii_data->phy_id = phydev->mdio.addr;
405
- /* fall through */
314
+ fallthrough;
406315
407316 case SIOCGMIIREG:
408
- mii_data->val_out = mdiobus_read(phydev->mdio.bus,
409
- mii_data->phy_id,
410
- mii_data->reg_num);
317
+ if (mdio_phy_id_is_c45(mii_data->phy_id)) {
318
+ prtad = mdio_phy_id_prtad(mii_data->phy_id);
319
+ devad = mdio_phy_id_devad(mii_data->phy_id);
320
+ devad = mdiobus_c45_addr(devad, mii_data->reg_num);
321
+ } else {
322
+ prtad = mii_data->phy_id;
323
+ devad = mii_data->reg_num;
324
+ }
325
+ mii_data->val_out = mdiobus_read(phydev->mdio.bus, prtad,
326
+ devad);
411327 return 0;
412328
413329 case SIOCSMIIREG:
414
- if (mii_data->phy_id == phydev->mdio.addr) {
415
- switch (mii_data->reg_num) {
330
+ if (mdio_phy_id_is_c45(mii_data->phy_id)) {
331
+ prtad = mdio_phy_id_prtad(mii_data->phy_id);
332
+ devad = mdio_phy_id_devad(mii_data->phy_id);
333
+ devad = mdiobus_c45_addr(devad, mii_data->reg_num);
334
+ } else {
335
+ prtad = mii_data->phy_id;
336
+ devad = mii_data->reg_num;
337
+ }
338
+ if (prtad == phydev->mdio.addr) {
339
+ switch (devad) {
416340 case MII_BMCR:
417341 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
418342 if (phydev->autoneg == AUTONEG_ENABLE)
....@@ -435,7 +359,13 @@
435359 }
436360 break;
437361 case MII_ADVERTISE:
438
- phydev->advertising = mii_adv_to_ethtool_adv_t(val);
362
+ mii_adv_mod_linkmode_adv_t(phydev->advertising,
363
+ val);
364
+ change_autoneg = true;
365
+ break;
366
+ case MII_CTRL1000:
367
+ mii_ctrl1000_mod_linkmode_adv_t(phydev->advertising,
368
+ val);
439369 change_autoneg = true;
440370 break;
441371 default:
....@@ -444,11 +374,10 @@
444374 }
445375 }
446376
447
- mdiobus_write(phydev->mdio.bus, mii_data->phy_id,
448
- mii_data->reg_num, val);
377
+ mdiobus_write(phydev->mdio.bus, prtad, devad, val);
449378
450
- if (mii_data->phy_id == phydev->mdio.addr &&
451
- mii_data->reg_num == MII_BMCR &&
379
+ if (prtad == phydev->mdio.addr &&
380
+ devad == MII_BMCR &&
452381 val & BMCR_RESET)
453382 return phy_init_hw(phydev);
454383
....@@ -458,15 +387,289 @@
458387 return 0;
459388
460389 case SIOCSHWTSTAMP:
461
- if (phydev->drv && phydev->drv->hwtstamp)
462
- return phydev->drv->hwtstamp(phydev, ifr);
463
- /* fall through */
390
+ if (phydev->mii_ts && phydev->mii_ts->hwtstamp)
391
+ return phydev->mii_ts->hwtstamp(phydev->mii_ts, ifr);
392
+ fallthrough;
464393
465394 default:
466395 return -EOPNOTSUPP;
467396 }
468397 }
469398 EXPORT_SYMBOL(phy_mii_ioctl);
399
+
400
+/**
401
+ * phy_do_ioctl - generic ndo_do_ioctl implementation
402
+ * @dev: the net_device struct
403
+ * @ifr: &struct ifreq for socket ioctl's
404
+ * @cmd: ioctl cmd to execute
405
+ */
406
+int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
407
+{
408
+ if (!dev->phydev)
409
+ return -ENODEV;
410
+
411
+ return phy_mii_ioctl(dev->phydev, ifr, cmd);
412
+}
413
+EXPORT_SYMBOL(phy_do_ioctl);
414
+
415
+/**
416
+ * phy_do_ioctl_running - generic ndo_do_ioctl implementation but test first
417
+ *
418
+ * @dev: the net_device struct
419
+ * @ifr: &struct ifreq for socket ioctl's
420
+ * @cmd: ioctl cmd to execute
421
+ *
422
+ * Same as phy_do_ioctl, but ensures that net_device is running before
423
+ * handling the ioctl.
424
+ */
425
+int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd)
426
+{
427
+ if (!netif_running(dev))
428
+ return -ENODEV;
429
+
430
+ return phy_do_ioctl(dev, ifr, cmd);
431
+}
432
+EXPORT_SYMBOL(phy_do_ioctl_running);
433
+
434
+/**
435
+ * phy_queue_state_machine - Trigger the state machine to run soon
436
+ *
437
+ * @phydev: the phy_device struct
438
+ * @jiffies: Run the state machine after these jiffies
439
+ */
440
+void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies)
441
+{
442
+ mod_delayed_work(system_power_efficient_wq, &phydev->state_queue,
443
+ jiffies);
444
+}
445
+EXPORT_SYMBOL(phy_queue_state_machine);
446
+
447
+/**
448
+ * phy_queue_state_machine - Trigger the state machine to run now
449
+ *
450
+ * @phydev: the phy_device struct
451
+ */
452
+static void phy_trigger_machine(struct phy_device *phydev)
453
+{
454
+ phy_queue_state_machine(phydev, 0);
455
+}
456
+
457
+static void phy_abort_cable_test(struct phy_device *phydev)
458
+{
459
+ int err;
460
+
461
+ ethnl_cable_test_finished(phydev);
462
+
463
+ err = phy_init_hw(phydev);
464
+ if (err)
465
+ phydev_err(phydev, "Error while aborting cable test");
466
+}
467
+
468
+/**
469
+ * phy_ethtool_get_strings - Get the statistic counter names
470
+ *
471
+ * @phydev: the phy_device struct
472
+ * @data: Where to put the strings
473
+ */
474
+int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data)
475
+{
476
+ if (!phydev->drv)
477
+ return -EIO;
478
+
479
+ mutex_lock(&phydev->lock);
480
+ phydev->drv->get_strings(phydev, data);
481
+ mutex_unlock(&phydev->lock);
482
+
483
+ return 0;
484
+}
485
+EXPORT_SYMBOL(phy_ethtool_get_strings);
486
+
487
+/**
488
+ * phy_ethtool_get_sset_count - Get the number of statistic counters
489
+ *
490
+ * @phydev: the phy_device struct
491
+ */
492
+int phy_ethtool_get_sset_count(struct phy_device *phydev)
493
+{
494
+ int ret;
495
+
496
+ if (!phydev->drv)
497
+ return -EIO;
498
+
499
+ if (phydev->drv->get_sset_count &&
500
+ phydev->drv->get_strings &&
501
+ phydev->drv->get_stats) {
502
+ mutex_lock(&phydev->lock);
503
+ ret = phydev->drv->get_sset_count(phydev);
504
+ mutex_unlock(&phydev->lock);
505
+
506
+ return ret;
507
+ }
508
+
509
+ return -EOPNOTSUPP;
510
+}
511
+EXPORT_SYMBOL(phy_ethtool_get_sset_count);
512
+
513
+/**
514
+ * phy_ethtool_get_stats - Get the statistic counters
515
+ *
516
+ * @phydev: the phy_device struct
517
+ * @stats: What counters to get
518
+ * @data: Where to store the counters
519
+ */
520
+int phy_ethtool_get_stats(struct phy_device *phydev,
521
+ struct ethtool_stats *stats, u64 *data)
522
+{
523
+ if (!phydev->drv)
524
+ return -EIO;
525
+
526
+ mutex_lock(&phydev->lock);
527
+ phydev->drv->get_stats(phydev, stats, data);
528
+ mutex_unlock(&phydev->lock);
529
+
530
+ return 0;
531
+}
532
+EXPORT_SYMBOL(phy_ethtool_get_stats);
533
+
534
+/**
535
+ * phy_start_cable_test - Start a cable test
536
+ *
537
+ * @phydev: the phy_device struct
538
+ * @extack: extack for reporting useful error messages
539
+ */
540
+int phy_start_cable_test(struct phy_device *phydev,
541
+ struct netlink_ext_ack *extack)
542
+{
543
+ struct net_device *dev = phydev->attached_dev;
544
+ int err = -ENOMEM;
545
+
546
+ if (!(phydev->drv &&
547
+ phydev->drv->cable_test_start &&
548
+ phydev->drv->cable_test_get_status)) {
549
+ NL_SET_ERR_MSG(extack,
550
+ "PHY driver does not support cable testing");
551
+ return -EOPNOTSUPP;
552
+ }
553
+
554
+ mutex_lock(&phydev->lock);
555
+ if (phydev->state == PHY_CABLETEST) {
556
+ NL_SET_ERR_MSG(extack,
557
+ "PHY already performing a test");
558
+ err = -EBUSY;
559
+ goto out;
560
+ }
561
+
562
+ if (phydev->state < PHY_UP ||
563
+ phydev->state > PHY_CABLETEST) {
564
+ NL_SET_ERR_MSG(extack,
565
+ "PHY not configured. Try setting interface up");
566
+ err = -EBUSY;
567
+ goto out;
568
+ }
569
+
570
+ err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_NTF);
571
+ if (err)
572
+ goto out;
573
+
574
+ /* Mark the carrier down until the test is complete */
575
+ phy_link_down(phydev);
576
+
577
+ netif_testing_on(dev);
578
+ err = phydev->drv->cable_test_start(phydev);
579
+ if (err) {
580
+ netif_testing_off(dev);
581
+ phy_link_up(phydev);
582
+ goto out_free;
583
+ }
584
+
585
+ phydev->state = PHY_CABLETEST;
586
+
587
+ if (phy_polling_mode(phydev))
588
+ phy_trigger_machine(phydev);
589
+
590
+ mutex_unlock(&phydev->lock);
591
+
592
+ return 0;
593
+
594
+out_free:
595
+ ethnl_cable_test_free(phydev);
596
+out:
597
+ mutex_unlock(&phydev->lock);
598
+
599
+ return err;
600
+}
601
+EXPORT_SYMBOL(phy_start_cable_test);
602
+
603
+/**
604
+ * phy_start_cable_test_tdr - Start a raw TDR cable test
605
+ *
606
+ * @phydev: the phy_device struct
607
+ * @extack: extack for reporting useful error messages
608
+ * @config: Configuration of the test to run
609
+ */
610
+int phy_start_cable_test_tdr(struct phy_device *phydev,
611
+ struct netlink_ext_ack *extack,
612
+ const struct phy_tdr_config *config)
613
+{
614
+ struct net_device *dev = phydev->attached_dev;
615
+ int err = -ENOMEM;
616
+
617
+ if (!(phydev->drv &&
618
+ phydev->drv->cable_test_tdr_start &&
619
+ phydev->drv->cable_test_get_status)) {
620
+ NL_SET_ERR_MSG(extack,
621
+ "PHY driver does not support cable test TDR");
622
+ return -EOPNOTSUPP;
623
+ }
624
+
625
+ mutex_lock(&phydev->lock);
626
+ if (phydev->state == PHY_CABLETEST) {
627
+ NL_SET_ERR_MSG(extack,
628
+ "PHY already performing a test");
629
+ err = -EBUSY;
630
+ goto out;
631
+ }
632
+
633
+ if (phydev->state < PHY_UP ||
634
+ phydev->state > PHY_CABLETEST) {
635
+ NL_SET_ERR_MSG(extack,
636
+ "PHY not configured. Try setting interface up");
637
+ err = -EBUSY;
638
+ goto out;
639
+ }
640
+
641
+ err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_TDR_NTF);
642
+ if (err)
643
+ goto out;
644
+
645
+ /* Mark the carrier down until the test is complete */
646
+ phy_link_down(phydev);
647
+
648
+ netif_testing_on(dev);
649
+ err = phydev->drv->cable_test_tdr_start(phydev, config);
650
+ if (err) {
651
+ netif_testing_off(dev);
652
+ phy_link_up(phydev);
653
+ goto out_free;
654
+ }
655
+
656
+ phydev->state = PHY_CABLETEST;
657
+
658
+ if (phy_polling_mode(phydev))
659
+ phy_trigger_machine(phydev);
660
+
661
+ mutex_unlock(&phydev->lock);
662
+
663
+ return 0;
664
+
665
+out_free:
666
+ ethnl_cable_test_free(phydev);
667
+out:
668
+ mutex_unlock(&phydev->lock);
669
+
670
+ return err;
671
+}
672
+EXPORT_SYMBOL(phy_start_cable_test_tdr);
470673
471674 static int phy_config_aneg(struct phy_device *phydev)
472675 {
....@@ -477,68 +680,73 @@
477680 * allowed to call genphy_config_aneg()
478681 */
479682 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
480
- return -EOPNOTSUPP;
683
+ return genphy_c45_config_aneg(phydev);
481684
482685 return genphy_config_aneg(phydev);
483686 }
484687
485688 /**
486
- * phy_start_aneg_priv - start auto-negotiation for this PHY device
689
+ * phy_check_link_status - check link status and set state accordingly
487690 * @phydev: the phy_device struct
488
- * @sync: indicate whether we should wait for the workqueue cancelation
691
+ *
692
+ * Description: Check for link and whether autoneg was triggered / is running
693
+ * and set state accordingly
694
+ */
695
+static int phy_check_link_status(struct phy_device *phydev)
696
+{
697
+ int err;
698
+
699
+ WARN_ON(!mutex_is_locked(&phydev->lock));
700
+
701
+ /* Keep previous state if loopback is enabled because some PHYs
702
+ * report that Link is Down when loopback is enabled.
703
+ */
704
+ if (phydev->loopback_enabled)
705
+ return 0;
706
+
707
+ err = phy_read_status(phydev);
708
+ if (err)
709
+ return err;
710
+
711
+ if (phydev->link && phydev->state != PHY_RUNNING) {
712
+ phy_check_downshift(phydev);
713
+ phydev->state = PHY_RUNNING;
714
+ phy_link_up(phydev);
715
+ } else if (!phydev->link && phydev->state != PHY_NOLINK) {
716
+ phydev->state = PHY_NOLINK;
717
+ phy_link_down(phydev);
718
+ }
719
+
720
+ return 0;
721
+}
722
+
723
+/**
724
+ * _phy_start_aneg - start auto-negotiation for this PHY device
725
+ * @phydev: the phy_device struct
489726 *
490727 * Description: Sanitizes the settings (if we're not autonegotiating
491728 * them), and then calls the driver's config_aneg function.
492729 * If the PHYCONTROL Layer is operating, we change the state to
493730 * reflect the beginning of Auto-negotiation or forcing.
494731 */
495
-static int phy_start_aneg_priv(struct phy_device *phydev, bool sync)
732
+static int _phy_start_aneg(struct phy_device *phydev)
496733 {
497
- bool trigger = 0;
498734 int err;
735
+
736
+ lockdep_assert_held(&phydev->lock);
499737
500738 if (!phydev->drv)
501739 return -EIO;
502740
503
- mutex_lock(&phydev->lock);
504
-
505741 if (AUTONEG_DISABLE == phydev->autoneg)
506742 phy_sanitize_settings(phydev);
507743
508
- /* Invalidate LP advertising flags */
509
- phydev->lp_advertising = 0;
510
-
511744 err = phy_config_aneg(phydev);
512745 if (err < 0)
513
- goto out_unlock;
746
+ return err;
514747
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);
748
+ if (phy_is_started(phydev))
749
+ err = phy_check_link_status(phydev);
542750
543751 return err;
544752 }
....@@ -554,7 +762,13 @@
554762 */
555763 int phy_start_aneg(struct phy_device *phydev)
556764 {
557
- return phy_start_aneg_priv(phydev, true);
765
+ int err;
766
+
767
+ mutex_lock(&phydev->lock);
768
+ err = _phy_start_aneg(phydev);
769
+ mutex_unlock(&phydev->lock);
770
+
771
+ return err;
558772 }
559773 EXPORT_SYMBOL(phy_start_aneg);
560774
....@@ -574,6 +788,66 @@
574788 return ret < 0 ? ret : 0;
575789 }
576790
791
+int phy_ethtool_ksettings_set(struct phy_device *phydev,
792
+ const struct ethtool_link_ksettings *cmd)
793
+{
794
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
795
+ u8 autoneg = cmd->base.autoneg;
796
+ u8 duplex = cmd->base.duplex;
797
+ u32 speed = cmd->base.speed;
798
+
799
+ if (cmd->base.phy_address != phydev->mdio.addr)
800
+ return -EINVAL;
801
+
802
+ linkmode_copy(advertising, cmd->link_modes.advertising);
803
+
804
+ /* We make sure that we don't pass unsupported values in to the PHY */
805
+ linkmode_and(advertising, advertising, phydev->supported);
806
+
807
+ /* Verify the settings we care about. */
808
+ if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
809
+ return -EINVAL;
810
+
811
+ if (autoneg == AUTONEG_ENABLE && linkmode_empty(advertising))
812
+ return -EINVAL;
813
+
814
+ if (autoneg == AUTONEG_DISABLE &&
815
+ ((speed != SPEED_1000 &&
816
+ speed != SPEED_100 &&
817
+ speed != SPEED_10) ||
818
+ (duplex != DUPLEX_HALF &&
819
+ duplex != DUPLEX_FULL)))
820
+ return -EINVAL;
821
+
822
+ mutex_lock(&phydev->lock);
823
+ phydev->autoneg = autoneg;
824
+
825
+ if (autoneg == AUTONEG_DISABLE) {
826
+ phydev->speed = speed;
827
+ phydev->duplex = duplex;
828
+ }
829
+
830
+ linkmode_copy(phydev->advertising, advertising);
831
+
832
+ linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
833
+ phydev->advertising, autoneg == AUTONEG_ENABLE);
834
+
835
+ phydev->master_slave_set = cmd->base.master_slave_cfg;
836
+ phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
837
+
838
+ /* Restart the PHY */
839
+ if (phy_is_started(phydev)) {
840
+ phydev->state = PHY_UP;
841
+ phy_trigger_machine(phydev);
842
+ } else {
843
+ _phy_start_aneg(phydev);
844
+ }
845
+
846
+ mutex_unlock(&phydev->lock);
847
+ return 0;
848
+}
849
+EXPORT_SYMBOL(phy_ethtool_ksettings_set);
850
+
577851 /**
578852 * phy_speed_down - set speed to lowest speed supported by both link partners
579853 * @phydev: the phy_device struct
....@@ -589,20 +863,21 @@
589863 */
590864 int phy_speed_down(struct phy_device *phydev, bool sync)
591865 {
592
- u32 adv = phydev->lp_advertising & phydev->supported;
593
- u32 adv_old = phydev->advertising;
866
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
594867 int ret;
595868
596869 if (phydev->autoneg != AUTONEG_ENABLE)
597870 return 0;
598871
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;
872
+ linkmode_copy(adv_tmp, phydev->advertising);
604873
605
- if (phydev->advertising == adv_old)
874
+ ret = phy_speed_down_core(phydev);
875
+ if (ret)
876
+ return ret;
877
+
878
+ linkmode_copy(phydev->adv_old, adv_tmp);
879
+
880
+ if (linkmode_equal(phydev->advertising, adv_tmp))
606881 return 0;
607882
608883 ret = phy_config_aneg(phydev);
....@@ -621,15 +896,19 @@
621896 */
622897 int phy_speed_up(struct phy_device *phydev)
623898 {
624
- u32 mask = PHY_10BT_FEATURES | PHY_100BT_FEATURES | PHY_1000BT_FEATURES;
625
- u32 adv_old = phydev->advertising;
899
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp);
626900
627901 if (phydev->autoneg != AUTONEG_ENABLE)
628902 return 0;
629903
630
- phydev->advertising = (adv_old & ~mask) | (phydev->supported & mask);
904
+ if (linkmode_empty(phydev->adv_old))
905
+ return 0;
631906
632
- if (phydev->advertising == adv_old)
907
+ linkmode_copy(adv_tmp, phydev->advertising);
908
+ linkmode_copy(phydev->advertising, phydev->adv_old);
909
+ linkmode_zero(phydev->adv_old);
910
+
911
+ if (linkmode_equal(phydev->advertising, adv_tmp))
633912 return 0;
634913
635914 return phy_config_aneg(phydev);
....@@ -648,28 +927,9 @@
648927 */
649928 void phy_start_machine(struct phy_device *phydev)
650929 {
651
- queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
930
+ phy_trigger_machine(phydev);
652931 }
653932 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
-}
673933
674934 /**
675935 * phy_stop_machine - stop the PHY state machine tracking
....@@ -684,7 +944,7 @@
684944 cancel_delayed_work_sync(&phydev->state_queue);
685945
686946 mutex_lock(&phydev->lock);
687
- if (phydev->state > PHY_UP && phydev->state != PHY_HALTED)
947
+ if (phy_is_started(phydev))
688948 phydev->state = PHY_UP;
689949 mutex_unlock(&phydev->lock);
690950 }
....@@ -700,18 +960,20 @@
700960 */
701961 static void phy_error(struct phy_device *phydev)
702962 {
963
+ WARN_ON(1);
964
+
703965 mutex_lock(&phydev->lock);
704966 phydev->state = PHY_HALTED;
705967 mutex_unlock(&phydev->lock);
706968
707
- phy_trigger_machine(phydev, false);
969
+ phy_trigger_machine(phydev);
708970 }
709971
710972 /**
711973 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
712974 * @phydev: target phy_device struct
713975 */
714
-static int phy_disable_interrupts(struct phy_device *phydev)
976
+int phy_disable_interrupts(struct phy_device *phydev)
715977 {
716978 int err;
717979
....@@ -725,48 +987,33 @@
725987 }
726988
727989 /**
728
- * phy_change - Called by the phy_interrupt to handle PHY changes
729
- * @phydev: phy_device struct that interrupted
990
+ * phy_did_interrupt - Checks if the PHY generated an interrupt
991
+ * @phydev: target phy_device struct
730992 */
731
-static irqreturn_t phy_change(struct phy_device *phydev)
993
+static int phy_did_interrupt(struct phy_device *phydev)
732994 {
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
- }
995
+ int ret;
742996
743997 mutex_lock(&phydev->lock);
744
- if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
745
- phydev->state = PHY_CHANGELINK;
998
+ ret = phydev->drv->did_interrupt(phydev);
746999 mutex_unlock(&phydev->lock);
7471000
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;
1001
+ return ret;
7581002 }
7591003
7601004 /**
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
1005
+ * phy_handle_interrupt - Handle PHY interrupt
1006
+ * @phydev: target phy_device struct
7631007 */
764
-void phy_change_work(struct work_struct *work)
1008
+static irqreturn_t phy_handle_interrupt(struct phy_device *phydev)
7651009 {
766
- struct phy_device *phydev =
767
- container_of(work, struct phy_device, phy_queue);
1010
+ irqreturn_t ret;
7681011
769
- phy_change(phydev);
1012
+ mutex_lock(&phydev->lock);
1013
+ ret = phydev->drv->handle_interrupt(phydev);
1014
+ mutex_unlock(&phydev->lock);
1015
+
1016
+ return ret;
7701017 }
7711018
7721019 /**
....@@ -774,17 +1021,29 @@
7741021 * @irq: interrupt line
7751022 * @phy_dat: phy_device pointer
7761023 *
777
- * Description: When a PHY interrupt occurs, the handler disables
778
- * interrupts, and uses phy_change to handle the interrupt.
1024
+ * Description: Handle PHY interrupt
7791025 */
7801026 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
7811027 {
7821028 struct phy_device *phydev = phy_dat;
1029
+ struct phy_driver *drv = phydev->drv;
7831030
784
- if (PHY_HALTED == phydev->state)
785
- return IRQ_NONE; /* It can't be ours. */
1031
+ if (drv->handle_interrupt)
1032
+ return phy_handle_interrupt(phydev);
7861033
787
- return phy_change(phydev);
1034
+ if (drv->did_interrupt && !phy_did_interrupt(phydev))
1035
+ return IRQ_NONE;
1036
+
1037
+ /* reschedule state queue work to run as soon as possible */
1038
+ phy_trigger_machine(phydev);
1039
+
1040
+ /* did_interrupt() may have cleared the interrupt already */
1041
+ if (!drv->did_interrupt && phy_clear_interrupt(phydev)) {
1042
+ phy_error(phydev);
1043
+ return IRQ_NONE;
1044
+ }
1045
+
1046
+ return IRQ_HANDLED;
7881047 }
7891048
7901049 /**
....@@ -802,46 +1061,47 @@
8021061 }
8031062
8041063 /**
805
- * phy_start_interrupts - request and enable interrupts for a PHY device
1064
+ * phy_request_interrupt - request and enable interrupt for a PHY device
8061065 * @phydev: target phy_device struct
8071066 *
808
- * Description: Request the interrupt for the given PHY.
1067
+ * Description: Request and enable the interrupt for the given PHY.
8091068 * If this fails, then we set irq to PHY_POLL.
810
- * Otherwise, we enable the interrupts in the PHY.
8111069 * This should only be called with a valid IRQ number.
812
- * Returns 0 on success or < 0 on error.
8131070 */
814
-int phy_start_interrupts(struct phy_device *phydev)
1071
+void phy_request_interrupt(struct phy_device *phydev)
8151072 {
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
- }
1073
+ int err;
8241074
825
- return phy_enable_interrupts(phydev);
1075
+ err = request_threaded_irq(phydev->irq, NULL, phy_interrupt,
1076
+ IRQF_ONESHOT | IRQF_SHARED,
1077
+ phydev_name(phydev), phydev);
1078
+ if (err) {
1079
+ phydev_warn(phydev, "Error %d requesting IRQ %d, falling back to polling\n",
1080
+ err, phydev->irq);
1081
+ phydev->irq = PHY_POLL;
1082
+ } else {
1083
+ if (phy_enable_interrupts(phydev)) {
1084
+ phydev_warn(phydev, "Can't enable interrupt, falling back to polling\n");
1085
+ phy_free_interrupt(phydev);
1086
+ phydev->irq = PHY_POLL;
1087
+ }
1088
+ }
8261089 }
827
-EXPORT_SYMBOL(phy_start_interrupts);
1090
+EXPORT_SYMBOL(phy_request_interrupt);
8281091
8291092 /**
830
- * phy_stop_interrupts - disable interrupts from a PHY device
1093
+ * phy_free_interrupt - disable and free interrupt for a PHY device
8311094 * @phydev: target phy_device struct
1095
+ *
1096
+ * Description: Disable and free the interrupt for the given PHY.
1097
+ * This should only be called with a valid IRQ number.
8321098 */
833
-int phy_stop_interrupts(struct phy_device *phydev)
1099
+void phy_free_interrupt(struct phy_device *phydev)
8341100 {
835
- int err = phy_disable_interrupts(phydev);
836
-
837
- if (err)
838
- phy_error(phydev);
839
-
1101
+ phy_disable_interrupts(phydev);
8401102 free_irq(phydev->irq, phydev);
841
-
842
- return err;
8431103 }
844
-EXPORT_SYMBOL(phy_stop_interrupts);
1104
+EXPORT_SYMBOL(phy_free_interrupt);
8451105
8461106 /**
8471107 * phy_stop - Bring down the PHY link, and stop checking the status
....@@ -849,21 +1109,33 @@
8491109 */
8501110 void phy_stop(struct phy_device *phydev)
8511111 {
1112
+ struct net_device *dev = phydev->attached_dev;
1113
+
1114
+ if (!phy_is_started(phydev) && phydev->state != PHY_DOWN) {
1115
+ WARN(1, "called from state %s\n",
1116
+ phy_state_to_str(phydev->state));
1117
+ return;
1118
+ }
1119
+
8521120 mutex_lock(&phydev->lock);
8531121
854
- if (PHY_HALTED == phydev->state)
855
- goto out_unlock;
1122
+ if (phydev->state == PHY_CABLETEST) {
1123
+ phy_abort_cable_test(phydev);
1124
+ netif_testing_off(dev);
1125
+ }
8561126
857
- if (phy_interrupt_is_valid(phydev))
858
- phy_disable_interrupts(phydev);
1127
+ if (phydev->sfp_bus)
1128
+ sfp_upstream_stop(phydev->sfp_bus);
8591129
8601130 phydev->state = PHY_HALTED;
8611131
862
-out_unlock:
8631132 mutex_unlock(&phydev->lock);
8641133
1134
+ phy_state_machine(&phydev->state_queue.work);
1135
+ phy_stop_machine(phydev);
1136
+
8651137 /* Cannot call flush_scheduled_work() here as desired because
866
- * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
1138
+ * of rtnl_lock(), but PHY_HALTED shall guarantee irq handler
8671139 * will not reenable interrupts.
8681140 */
8691141 }
....@@ -881,50 +1153,27 @@
8811153 */
8821154 void phy_start(struct phy_device *phydev)
8831155 {
884
- int err = 0;
885
-
8861156 mutex_lock(&phydev->lock);
8871157
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;
1158
+ if (phydev->state != PHY_READY && phydev->state != PHY_HALTED) {
1159
+ WARN(1, "called from state %s\n",
1160
+ phy_state_to_str(phydev->state));
1161
+ goto out;
9101162 }
911
- mutex_unlock(&phydev->lock);
9121163
913
- phy_trigger_machine(phydev, true);
1164
+ if (phydev->sfp_bus)
1165
+ sfp_upstream_start(phydev->sfp_bus);
1166
+
1167
+ /* if phy was suspended, bring the physical link up again */
1168
+ __phy_resume(phydev);
1169
+
1170
+ phydev->state = PHY_UP;
1171
+
1172
+ phy_start_machine(phydev);
1173
+out:
1174
+ mutex_unlock(&phydev->lock);
9141175 }
9151176 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
-}
9281177
9291178 /**
9301179 * phy_state_machine - Handle the state machine
....@@ -935,198 +1184,84 @@
9351184 struct delayed_work *dwork = to_delayed_work(work);
9361185 struct phy_device *phydev =
9371186 container_of(dwork, struct phy_device, state_queue);
1187
+ struct net_device *dev = phydev->attached_dev;
9381188 bool needs_aneg = false, do_suspend = false;
9391189 enum phy_state old_state;
1190
+ bool finished = false;
9401191 int err = 0;
941
- int old_link;
9421192
9431193 mutex_lock(&phydev->lock);
9441194
9451195 old_state = phydev->state;
9461196
947
- if (phydev->drv && phydev->drv->link_change_notify)
948
- phydev->drv->link_change_notify(phydev);
949
-
9501197 switch (phydev->state) {
9511198 case PHY_DOWN:
952
- case PHY_STARTING:
9531199 case PHY_READY:
954
- case PHY_PENDING:
9551200 break;
9561201 case PHY_UP:
9571202 needs_aneg = true;
9581203
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;
9851204 break;
9861205 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;
10241206 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
- }
1207
+ err = phy_check_link_status(phydev);
10461208 break;
1047
- case PHY_CHANGELINK:
1048
- err = phy_read_status(phydev);
1049
- if (err)
1209
+ case PHY_CABLETEST:
1210
+ err = phydev->drv->cable_test_get_status(phydev, &finished);
1211
+ if (err) {
1212
+ phy_abort_cable_test(phydev);
1213
+ netif_testing_off(dev);
1214
+ needs_aneg = true;
1215
+ phydev->state = PHY_UP;
10501216 break;
1217
+ }
10511218
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);
1219
+ if (finished) {
1220
+ ethnl_cable_test_finished(phydev);
1221
+ netif_testing_off(dev);
1222
+ needs_aneg = true;
1223
+ phydev->state = PHY_UP;
10581224 }
10591225 break;
10601226 case PHY_HALTED:
10611227 if (phydev->link) {
10621228 phydev->link = 0;
1063
- phy_link_down(phydev, true);
1064
- do_suspend = true;
1229
+ phy_link_down(phydev);
10651230 }
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
- }
1231
+ do_suspend = true;
11051232 break;
11061233 }
11071234
11081235 mutex_unlock(&phydev->lock);
11091236
11101237 if (needs_aneg)
1111
- err = phy_start_aneg_priv(phydev, false);
1238
+ err = phy_start_aneg(phydev);
11121239 else if (do_suspend)
11131240 phy_suspend(phydev);
11141241
11151242 if (err < 0)
11161243 phy_error(phydev);
11171244
1118
- if (old_state != phydev->state)
1245
+ if (old_state != phydev->state) {
11191246 phydev_dbg(phydev, "PHY state change %s -> %s\n",
11201247 phy_state_to_str(old_state),
11211248 phy_state_to_str(phydev->state));
1249
+ if (phydev->drv && phydev->drv->link_change_notify)
1250
+ phydev->drv->link_change_notify(phydev);
1251
+ }
11221252
11231253 /* Only re-schedule a PHY state machine change if we are polling the
11241254 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
1125
- * between states from phy_mac_interrupt()
1255
+ * between states from phy_mac_interrupt().
1256
+ *
1257
+ * In state PHY_HALTED the PHY gets suspended, so rescheduling the
1258
+ * state machine would be pointless and possibly error prone when
1259
+ * called from phy_disconnect() synchronously.
11261260 */
1127
- if (phy_polling_mode(phydev))
1128
- queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
1129
- PHY_STATE_TIME * HZ);
1261
+ mutex_lock(&phydev->lock);
1262
+ if (phy_polling_mode(phydev) && phy_is_started(phydev))
1263
+ phy_queue_state_machine(phydev, PHY_STATE_TIME);
1264
+ mutex_unlock(&phydev->lock);
11301265 }
11311266
11321267 /**
....@@ -1139,9 +1274,33 @@
11391274 void phy_mac_interrupt(struct phy_device *phydev)
11401275 {
11411276 /* Trigger a state machine change */
1142
- queue_work(system_power_efficient_wq, &phydev->phy_queue);
1277
+ phy_trigger_machine(phydev);
11431278 }
11441279 EXPORT_SYMBOL(phy_mac_interrupt);
1280
+
1281
+static void mmd_eee_adv_to_linkmode(unsigned long *advertising, u16 eee_adv)
1282
+{
1283
+ linkmode_zero(advertising);
1284
+
1285
+ if (eee_adv & MDIO_EEE_100TX)
1286
+ linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
1287
+ advertising);
1288
+ if (eee_adv & MDIO_EEE_1000T)
1289
+ linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1290
+ advertising);
1291
+ if (eee_adv & MDIO_EEE_10GT)
1292
+ linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
1293
+ advertising);
1294
+ if (eee_adv & MDIO_EEE_1000KX)
1295
+ linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
1296
+ advertising);
1297
+ if (eee_adv & MDIO_EEE_10GKX4)
1298
+ linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
1299
+ advertising);
1300
+ if (eee_adv & MDIO_EEE_10GKR)
1301
+ linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
1302
+ advertising);
1303
+}
11451304
11461305 /**
11471306 * phy_init_eee - init and check the EEE feature
....@@ -1161,9 +1320,12 @@
11611320 /* According to 802.3az,the EEE is supported only in full duplex-mode.
11621321 */
11631322 if (phydev->duplex == DUPLEX_FULL) {
1323
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
1324
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(lp);
1325
+ __ETHTOOL_DECLARE_LINK_MODE_MASK(adv);
11641326 int eee_lp, eee_cap, eee_adv;
1165
- u32 lp, cap, adv;
11661327 int status;
1328
+ u32 cap;
11671329
11681330 /* Read phy status to properly get the right settings */
11691331 status = phy_read_status(phydev);
....@@ -1190,22 +1352,19 @@
11901352 if (eee_adv <= 0)
11911353 goto eee_exit_err;
11921354
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))
1355
+ mmd_eee_adv_to_linkmode(adv, eee_adv);
1356
+ mmd_eee_adv_to_linkmode(lp, eee_lp);
1357
+ linkmode_and(common, adv, lp);
1358
+
1359
+ if (!phy_check_valid(phydev->speed, phydev->duplex, common))
11961360 goto eee_exit_err;
11971361
1198
- if (clk_stop_enable) {
1362
+ if (clk_stop_enable)
11991363 /* Configure the PHY to stop receiving xMII
12001364 * clock while it is signaling LPI.
12011365 */
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
- }
1366
+ phy_set_bits_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1,
1367
+ MDIO_PCS_CTRL1_CLKSTOP_EN);
12091368
12101369 return 0; /* EEE supported */
12111370 }
....@@ -1256,12 +1415,15 @@
12561415 if (val < 0)
12571416 return val;
12581417 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1418
+ data->eee_enabled = !!data->advertised;
12591419
12601420 /* Get LP advertisement EEE */
12611421 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
12621422 if (val < 0)
12631423 return val;
12641424 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1425
+
1426
+ data->eee_active = !!(data->advertised & data->lp_advertised);
12651427
12661428 return 0;
12671429 }
....@@ -1276,7 +1438,7 @@
12761438 */
12771439 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
12781440 {
1279
- int cap, old_adv, adv, ret;
1441
+ int cap, old_adv, adv = 0, ret;
12801442
12811443 if (!phydev->drv)
12821444 return -EIO;
....@@ -1290,10 +1452,12 @@
12901452 if (old_adv < 0)
12911453 return old_adv;
12921454
1293
- adv = ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
1294
-
1295
- /* Mask prohibited EEE modes */
1296
- adv &= ~phydev->eee_broken_modes;
1455
+ if (data->eee_enabled) {
1456
+ adv = !data->advertised ? cap :
1457
+ ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
1458
+ /* Mask prohibited EEE modes */
1459
+ adv &= ~phydev->eee_broken_modes;
1460
+ }
12971461
12981462 if (old_adv != adv) {
12991463 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
....@@ -1314,6 +1478,12 @@
13141478 }
13151479 EXPORT_SYMBOL(phy_ethtool_set_eee);
13161480
1481
+/**
1482
+ * phy_ethtool_set_wol - Configure Wake On LAN
1483
+ *
1484
+ * @phydev: target phy_device struct
1485
+ * @wol: Configuration requested
1486
+ */
13171487 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
13181488 {
13191489 if (phydev->drv && phydev->drv->set_wol)
....@@ -1323,6 +1493,12 @@
13231493 }
13241494 EXPORT_SYMBOL(phy_ethtool_set_wol);
13251495
1496
+/**
1497
+ * phy_ethtool_get_wol - Get the current Wake On LAN configuration
1498
+ *
1499
+ * @phydev: target phy_device struct
1500
+ * @wol: Store the current configuration here
1501
+ */
13261502 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
13271503 {
13281504 if (phydev->drv && phydev->drv->get_wol)
....@@ -1356,6 +1532,10 @@
13561532 }
13571533 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
13581534
1535
+/**
1536
+ * phy_ethtool_nway_reset - Restart auto negotiation
1537
+ * @ndev: Network device to restart autoneg for
1538
+ */
13591539 int phy_ethtool_nway_reset(struct net_device *ndev)
13601540 {
13611541 struct phy_device *phydev = ndev->phydev;