aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid S. Miller <davem@davemloft.net>2018-05-11 12:03:06 -0400
committerDavid S. Miller <davem@davemloft.net>2018-05-11 12:03:06 -0400
commit4cbd7a7d3c0fb1373bf981c5498b51c050668acc (patch)
tree83d6a6dbfffe50ab2e7c4c1d073a461b5ca1da5e
parentec9efb523cb8daf7b9d2e5c9cb80b255b716a777 (diff)
parent58d56fcc3964f9be0a9ca42fd126bcd9dc7afc90 (diff)
Merge branch 'dsa-Plug-in-PHYLINK-support'
Florian Fainelli says: ==================== net: dsa: Plug in PHYLINK support This patch series adds PHYLINK support to DSA which is necessary to support more complex PHY and pluggable modules setups. Patch series can be found here: https://github.com/ffainelli/linux/commits/dsa-phylink-v2 This was tested on: - dsa-loop - bcm_sf2 - mv88e6xxx - b53 With a variety of test cases: - internal & external MDIO PHYs - MoCA with link notification through interrupt/MMIO register - built-in PHYs - ifconfig up/down for several cycles works - bind/unbind of the drivers Changes in v2: - fixed link configuration for mv88e6xxx (Andrew) after introducing polling This is technically v2 of what was posted back in March 2018, changes from last time: - fixed probe/remove of drivers - fixed missing gpiod_put() for link GPIOs - fixed polling of link GPIOs (Russell I would need your SoB on the patch you provided offline initially, added some modifications to it) - tested across a wider set of platforms And everything should still work as expected. Please be aware of the following: - switch drivers (like bcm_sf2) which may have user-facing network ports using fixed links would need to implement phylink_mac_ops to remain functional. PHYLINK does not create a phy_device for fixed links, therefore our call to adjust_link() from phylink_mac_link_{up,down} would not be calling into the driver. This *should not* affect CPU/DSA ports which are configured through adjust_link() but have no network devices - support for SFP/SFF is now possible, but switch drivers will still need some modifications to properly support those, including, but not limited to using the correct binding information. This will be submitted on top of this series Please do test on your respective platforms/switches and let me know if you find any issues, hopefully everything still works like before. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--drivers/net/dsa/bcm_sf2.c191
-rw-r--r--drivers/net/dsa/mv88e6xxx/chip.c83
-rw-r--r--drivers/net/dsa/mv88e6xxx/port.c39
-rw-r--r--drivers/net/dsa/mv88e6xxx/port.h3
-rw-r--r--drivers/net/phy/phylink.c20
-rw-r--r--include/net/dsa.h25
-rw-r--r--net/dsa/Kconfig2
-rw-r--r--net/dsa/dsa_priv.h9
-rw-r--r--net/dsa/slave.c293
9 files changed, 458 insertions, 207 deletions
diff --git a/drivers/net/dsa/bcm_sf2.c b/drivers/net/dsa/bcm_sf2.c
index 97236cfcbae4..ac621f44237a 100644
--- a/drivers/net/dsa/bcm_sf2.c
+++ b/drivers/net/dsa/bcm_sf2.c
@@ -16,6 +16,7 @@
16#include <linux/platform_device.h> 16#include <linux/platform_device.h>
17#include <linux/phy.h> 17#include <linux/phy.h>
18#include <linux/phy_fixed.h> 18#include <linux/phy_fixed.h>
19#include <linux/phylink.h>
19#include <linux/mii.h> 20#include <linux/mii.h>
20#include <linux/of.h> 21#include <linux/of.h>
21#include <linux/of_irq.h> 22#include <linux/of_irq.h>
@@ -306,7 +307,8 @@ static int bcm_sf2_sw_mdio_write(struct mii_bus *bus, int addr, int regnum,
306 307
307static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id) 308static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
308{ 309{
309 struct bcm_sf2_priv *priv = dev_id; 310 struct dsa_switch *ds = dev_id;
311 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
310 312
311 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) & 313 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
312 ~priv->irq0_mask; 314 ~priv->irq0_mask;
@@ -317,16 +319,21 @@ static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
317 319
318static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id) 320static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
319{ 321{
320 struct bcm_sf2_priv *priv = dev_id; 322 struct dsa_switch *ds = dev_id;
323 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
321 324
322 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) & 325 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
323 ~priv->irq1_mask; 326 ~priv->irq1_mask;
324 intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR); 327 intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
325 328
326 if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF)) 329 if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF)) {
327 priv->port_sts[7].link = 1; 330 priv->port_sts[7].link = true;
328 if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF)) 331 dsa_port_phylink_mac_change(ds, 7, true);
329 priv->port_sts[7].link = 0; 332 }
333 if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF)) {
334 priv->port_sts[7].link = false;
335 dsa_port_phylink_mac_change(ds, 7, false);
336 }
330 337
331 return IRQ_HANDLED; 338 return IRQ_HANDLED;
332} 339}
@@ -473,13 +480,56 @@ static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
473 return priv->hw_params.gphy_rev; 480 return priv->hw_params.gphy_rev;
474} 481}
475 482
476static void bcm_sf2_sw_adjust_link(struct dsa_switch *ds, int port, 483static void bcm_sf2_sw_validate(struct dsa_switch *ds, int port,
477 struct phy_device *phydev) 484 unsigned long *supported,
485 struct phylink_link_state *state)
486{
487 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
488
489 if (!phy_interface_mode_is_rgmii(state->interface) &&
490 state->interface != PHY_INTERFACE_MODE_MII &&
491 state->interface != PHY_INTERFACE_MODE_REVMII &&
492 state->interface != PHY_INTERFACE_MODE_GMII &&
493 state->interface != PHY_INTERFACE_MODE_INTERNAL &&
494 state->interface != PHY_INTERFACE_MODE_MOCA) {
495 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
496 dev_err(ds->dev,
497 "Unsupported interface: %d\n", state->interface);
498 return;
499 }
500
501 /* Allow all the expected bits */
502 phylink_set(mask, Autoneg);
503 phylink_set_port_modes(mask);
504 phylink_set(mask, Pause);
505 phylink_set(mask, Asym_Pause);
506
507 /* With the exclusion of MII and Reverse MII, we support Gigabit,
508 * including Half duplex
509 */
510 if (state->interface != PHY_INTERFACE_MODE_MII &&
511 state->interface != PHY_INTERFACE_MODE_REVMII) {
512 phylink_set(mask, 1000baseT_Full);
513 phylink_set(mask, 1000baseT_Half);
514 }
515
516 phylink_set(mask, 10baseT_Half);
517 phylink_set(mask, 10baseT_Full);
518 phylink_set(mask, 100baseT_Half);
519 phylink_set(mask, 100baseT_Full);
520
521 bitmap_and(supported, supported, mask,
522 __ETHTOOL_LINK_MODE_MASK_NBITS);
523 bitmap_and(state->advertising, state->advertising, mask,
524 __ETHTOOL_LINK_MODE_MASK_NBITS);
525}
526
527static void bcm_sf2_sw_mac_config(struct dsa_switch *ds, int port,
528 unsigned int mode,
529 const struct phylink_link_state *state)
478{ 530{
479 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds); 531 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
480 struct ethtool_eee *p = &priv->dev->ports[port].eee;
481 u32 id_mode_dis = 0, port_mode; 532 u32 id_mode_dis = 0, port_mode;
482 const char *str = NULL;
483 u32 reg, offset; 533 u32 reg, offset;
484 534
485 if (priv->type == BCM7445_DEVICE_ID) 535 if (priv->type == BCM7445_DEVICE_ID)
@@ -487,62 +537,48 @@ static void bcm_sf2_sw_adjust_link(struct dsa_switch *ds, int port,
487 else 537 else
488 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port); 538 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
489 539
490 switch (phydev->interface) { 540 switch (state->interface) {
491 case PHY_INTERFACE_MODE_RGMII: 541 case PHY_INTERFACE_MODE_RGMII:
492 str = "RGMII (no delay)";
493 id_mode_dis = 1; 542 id_mode_dis = 1;
543 /* fallthrough */
494 case PHY_INTERFACE_MODE_RGMII_TXID: 544 case PHY_INTERFACE_MODE_RGMII_TXID:
495 if (!str)
496 str = "RGMII (TX delay)";
497 port_mode = EXT_GPHY; 545 port_mode = EXT_GPHY;
498 break; 546 break;
499 case PHY_INTERFACE_MODE_MII: 547 case PHY_INTERFACE_MODE_MII:
500 str = "MII";
501 port_mode = EXT_EPHY; 548 port_mode = EXT_EPHY;
502 break; 549 break;
503 case PHY_INTERFACE_MODE_REVMII: 550 case PHY_INTERFACE_MODE_REVMII:
504 str = "Reverse MII";
505 port_mode = EXT_REVMII; 551 port_mode = EXT_REVMII;
506 break; 552 break;
507 default: 553 default:
508 /* All other PHYs: internal and MoCA */ 554 /* all other PHYs: internal and MoCA */
509 goto force_link;
510 }
511
512 /* If the link is down, just disable the interface to conserve power */
513 if (!phydev->link) {
514 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
515 reg &= ~RGMII_MODE_EN;
516 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
517 goto force_link; 555 goto force_link;
518 } 556 }
519 557
520 /* Clear id_mode_dis bit, and the existing port mode, but 558 /* Clear id_mode_dis bit, and the existing port mode, let
521 * make sure we enable the RGMII block for data to pass 559 * RGMII_MODE_EN bet set by mac_link_{up,down}
522 */ 560 */
523 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port)); 561 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
524 reg &= ~ID_MODE_DIS; 562 reg &= ~ID_MODE_DIS;
525 reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT); 563 reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
526 reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN); 564 reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
527 565
528 reg |= port_mode | RGMII_MODE_EN; 566 reg |= port_mode;
529 if (id_mode_dis) 567 if (id_mode_dis)
530 reg |= ID_MODE_DIS; 568 reg |= ID_MODE_DIS;
531 569
532 if (phydev->pause) { 570 if (state->pause & MLO_PAUSE_TXRX_MASK) {
533 if (phydev->asym_pause) 571 if (state->pause & MLO_PAUSE_TX)
534 reg |= TX_PAUSE_EN; 572 reg |= TX_PAUSE_EN;
535 reg |= RX_PAUSE_EN; 573 reg |= RX_PAUSE_EN;
536 } 574 }
537 575
538 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port)); 576 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
539 577
540 pr_info("Port %d configured for %s\n", port, str);
541
542force_link: 578force_link:
543 /* Force link settings detected from the PHY */ 579 /* Force link settings detected from the PHY */
544 reg = SW_OVERRIDE; 580 reg = SW_OVERRIDE;
545 switch (phydev->speed) { 581 switch (state->speed) {
546 case SPEED_1000: 582 case SPEED_1000:
547 reg |= SPDSTS_1000 << SPEED_SHIFT; 583 reg |= SPDSTS_1000 << SPEED_SHIFT;
548 break; 584 break;
@@ -551,33 +587,61 @@ force_link:
551 break; 587 break;
552 } 588 }
553 589
554 if (phydev->link) 590 if (state->link)
555 reg |= LINK_STS; 591 reg |= LINK_STS;
556 if (phydev->duplex == DUPLEX_FULL) 592 if (state->duplex == DUPLEX_FULL)
557 reg |= DUPLX_MODE; 593 reg |= DUPLX_MODE;
558 594
559 core_writel(priv, reg, offset); 595 core_writel(priv, reg, offset);
560
561 if (!phydev->is_pseudo_fixed_link)
562 p->eee_enabled = b53_eee_init(ds, port, phydev);
563} 596}
564 597
565static void bcm_sf2_sw_fixed_link_update(struct dsa_switch *ds, int port, 598static void bcm_sf2_sw_mac_link_set(struct dsa_switch *ds, int port,
566 struct fixed_phy_status *status) 599 phy_interface_t interface, bool link)
567{ 600{
568 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds); 601 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
569 u32 duplex, pause, offset;
570 u32 reg; 602 u32 reg;
571 603
572 if (priv->type == BCM7445_DEVICE_ID) 604 if (!phy_interface_mode_is_rgmii(interface) &&
573 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port); 605 interface != PHY_INTERFACE_MODE_MII &&
606 interface != PHY_INTERFACE_MODE_REVMII)
607 return;
608
609 /* If the link is down, just disable the interface to conserve power */
610 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
611 if (link)
612 reg |= RGMII_MODE_EN;
574 else 613 else
575 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port); 614 reg &= ~RGMII_MODE_EN;
615 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
616}
617
618static void bcm_sf2_sw_mac_link_down(struct dsa_switch *ds, int port,
619 unsigned int mode,
620 phy_interface_t interface)
621{
622 bcm_sf2_sw_mac_link_set(ds, port, interface, false);
623}
624
625static void bcm_sf2_sw_mac_link_up(struct dsa_switch *ds, int port,
626 unsigned int mode,
627 phy_interface_t interface,
628 struct phy_device *phydev)
629{
630 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
631 struct ethtool_eee *p = &priv->dev->ports[port].eee;
576 632
577 duplex = core_readl(priv, CORE_DUPSTS); 633 bcm_sf2_sw_mac_link_set(ds, port, interface, true);
578 pause = core_readl(priv, CORE_PAUSESTS);
579 634
580 status->link = 0; 635 if (mode == MLO_AN_PHY && phydev)
636 p->eee_enabled = b53_eee_init(ds, port, phydev);
637}
638
639static void bcm_sf2_sw_fixed_state(struct dsa_switch *ds, int port,
640 struct phylink_link_state *status)
641{
642 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
643
644 status->link = false;
581 645
582 /* MoCA port is special as we do not get link status from CORE_LNKSTS, 646 /* MoCA port is special as we do not get link status from CORE_LNKSTS,
583 * which means that we need to force the link at the port override 647 * which means that we need to force the link at the port override
@@ -596,28 +660,10 @@ static void bcm_sf2_sw_fixed_link_update(struct dsa_switch *ds, int port,
596 */ 660 */
597 if (!status->link) 661 if (!status->link)
598 netif_carrier_off(ds->ports[port].slave); 662 netif_carrier_off(ds->ports[port].slave);
599 status->duplex = 1; 663 status->duplex = DUPLEX_FULL;
600 } else { 664 } else {
601 status->link = 1; 665 status->link = true;
602 status->duplex = !!(duplex & (1 << port));
603 }
604
605 reg = core_readl(priv, offset);
606 reg |= SW_OVERRIDE;
607 if (status->link)
608 reg |= LINK_STS;
609 else
610 reg &= ~LINK_STS;
611 core_writel(priv, reg, offset);
612
613 if ((pause & (1 << port)) &&
614 (pause & (1 << (port + PAUSESTS_TX_PAUSE_SHIFT)))) {
615 status->asym_pause = 1;
616 status->pause = 1;
617 } 666 }
618
619 if (pause & (1 << port))
620 status->pause = 1;
621} 667}
622 668
623static void bcm_sf2_enable_acb(struct dsa_switch *ds) 669static void bcm_sf2_enable_acb(struct dsa_switch *ds)
@@ -861,8 +907,11 @@ static const struct dsa_switch_ops bcm_sf2_ops = {
861 .get_sset_count = b53_get_sset_count, 907 .get_sset_count = b53_get_sset_count,
862 .get_ethtool_phy_stats = b53_get_ethtool_phy_stats, 908 .get_ethtool_phy_stats = b53_get_ethtool_phy_stats,
863 .get_phy_flags = bcm_sf2_sw_get_phy_flags, 909 .get_phy_flags = bcm_sf2_sw_get_phy_flags,
864 .adjust_link = bcm_sf2_sw_adjust_link, 910 .phylink_validate = bcm_sf2_sw_validate,
865 .fixed_link_update = bcm_sf2_sw_fixed_link_update, 911 .phylink_mac_config = bcm_sf2_sw_mac_config,
912 .phylink_mac_link_down = bcm_sf2_sw_mac_link_down,
913 .phylink_mac_link_up = bcm_sf2_sw_mac_link_up,
914 .phylink_fixed_state = bcm_sf2_sw_fixed_state,
866 .suspend = bcm_sf2_sw_suspend, 915 .suspend = bcm_sf2_sw_suspend,
867 .resume = bcm_sf2_sw_resume, 916 .resume = bcm_sf2_sw_resume,
868 .get_wol = bcm_sf2_sw_get_wol, 917 .get_wol = bcm_sf2_sw_get_wol,
@@ -1065,14 +1114,14 @@ static int bcm_sf2_sw_probe(struct platform_device *pdev)
1065 bcm_sf2_intr_disable(priv); 1114 bcm_sf2_intr_disable(priv);
1066 1115
1067 ret = devm_request_irq(&pdev->dev, priv->irq0, bcm_sf2_switch_0_isr, 0, 1116 ret = devm_request_irq(&pdev->dev, priv->irq0, bcm_sf2_switch_0_isr, 0,
1068 "switch_0", priv); 1117 "switch_0", ds);
1069 if (ret < 0) { 1118 if (ret < 0) {
1070 pr_err("failed to request switch_0 IRQ\n"); 1119 pr_err("failed to request switch_0 IRQ\n");
1071 goto out_mdio; 1120 goto out_mdio;
1072 } 1121 }
1073 1122
1074 ret = devm_request_irq(&pdev->dev, priv->irq1, bcm_sf2_switch_1_isr, 0, 1123 ret = devm_request_irq(&pdev->dev, priv->irq1, bcm_sf2_switch_1_isr, 0,
1075 "switch_1", priv); 1124 "switch_1", ds);
1076 if (ret < 0) { 1125 if (ret < 0) {
1077 pr_err("failed to request switch_1 IRQ\n"); 1126 pr_err("failed to request switch_1 IRQ\n");
1078 goto out_mdio; 1127 goto out_mdio;
diff --git a/drivers/net/dsa/mv88e6xxx/chip.c b/drivers/net/dsa/mv88e6xxx/chip.c
index 5d933549dc49..1cebde80b101 100644
--- a/drivers/net/dsa/mv88e6xxx/chip.c
+++ b/drivers/net/dsa/mv88e6xxx/chip.c
@@ -31,6 +31,7 @@
31#include <linux/netdevice.h> 31#include <linux/netdevice.h>
32#include <linux/gpio/consumer.h> 32#include <linux/gpio/consumer.h>
33#include <linux/phy.h> 33#include <linux/phy.h>
34#include <linux/phylink.h>
34#include <net/dsa.h> 35#include <net/dsa.h>
35 36
36#include "chip.h" 37#include "chip.h"
@@ -580,6 +581,83 @@ static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
580 dev_err(ds->dev, "p%d: failed to configure MAC\n", port); 581 dev_err(ds->dev, "p%d: failed to configure MAC\n", port);
581} 582}
582 583
584static void mv88e6xxx_validate(struct dsa_switch *ds, int port,
585 unsigned long *supported,
586 struct phylink_link_state *state)
587{
588}
589
590static int mv88e6xxx_link_state(struct dsa_switch *ds, int port,
591 struct phylink_link_state *state)
592{
593 struct mv88e6xxx_chip *chip = ds->priv;
594 int err;
595
596 mutex_lock(&chip->reg_lock);
597 err = mv88e6xxx_port_link_state(chip, port, state);
598 mutex_unlock(&chip->reg_lock);
599
600 return err;
601}
602
603static void mv88e6xxx_mac_config(struct dsa_switch *ds, int port,
604 unsigned int mode,
605 const struct phylink_link_state *state)
606{
607 struct mv88e6xxx_chip *chip = ds->priv;
608 int speed, duplex, link, err;
609
610 if (mode == MLO_AN_PHY)
611 return;
612
613 if (mode == MLO_AN_FIXED) {
614 link = LINK_FORCED_UP;
615 speed = state->speed;
616 duplex = state->duplex;
617 } else {
618 speed = SPEED_UNFORCED;
619 duplex = DUPLEX_UNFORCED;
620 link = LINK_UNFORCED;
621 }
622
623 mutex_lock(&chip->reg_lock);
624 err = mv88e6xxx_port_setup_mac(chip, port, link, speed, duplex,
625 state->interface);
626 mutex_unlock(&chip->reg_lock);
627
628 if (err && err != -EOPNOTSUPP)
629 dev_err(ds->dev, "p%d: failed to configure MAC\n", port);
630}
631
632static void mv88e6xxx_mac_link_force(struct dsa_switch *ds, int port, int link)
633{
634 struct mv88e6xxx_chip *chip = ds->priv;
635 int err;
636
637 mutex_lock(&chip->reg_lock);
638 err = chip->info->ops->port_set_link(chip, port, link);
639 mutex_unlock(&chip->reg_lock);
640
641 if (err)
642 dev_err(chip->dev, "p%d: failed to force MAC link\n", port);
643}
644
645static void mv88e6xxx_mac_link_down(struct dsa_switch *ds, int port,
646 unsigned int mode,
647 phy_interface_t interface)
648{
649 if (mode == MLO_AN_FIXED)
650 mv88e6xxx_mac_link_force(ds, port, LINK_FORCED_DOWN);
651}
652
653static void mv88e6xxx_mac_link_up(struct dsa_switch *ds, int port,
654 unsigned int mode, phy_interface_t interface,
655 struct phy_device *phydev)
656{
657 if (mode == MLO_AN_FIXED)
658 mv88e6xxx_mac_link_force(ds, port, LINK_FORCED_UP);
659}
660
583static int mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port) 661static int mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
584{ 662{
585 if (!chip->info->ops->stats_snapshot) 663 if (!chip->info->ops->stats_snapshot)
@@ -4169,6 +4247,11 @@ static const struct dsa_switch_ops mv88e6xxx_switch_ops = {
4169 .get_tag_protocol = mv88e6xxx_get_tag_protocol, 4247 .get_tag_protocol = mv88e6xxx_get_tag_protocol,
4170 .setup = mv88e6xxx_setup, 4248 .setup = mv88e6xxx_setup,
4171 .adjust_link = mv88e6xxx_adjust_link, 4249 .adjust_link = mv88e6xxx_adjust_link,
4250 .phylink_validate = mv88e6xxx_validate,
4251 .phylink_mac_link_state = mv88e6xxx_link_state,
4252 .phylink_mac_config = mv88e6xxx_mac_config,
4253 .phylink_mac_link_down = mv88e6xxx_mac_link_down,
4254 .phylink_mac_link_up = mv88e6xxx_mac_link_up,
4172 .get_strings = mv88e6xxx_get_strings, 4255 .get_strings = mv88e6xxx_get_strings,
4173 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats, 4256 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
4174 .get_sset_count = mv88e6xxx_get_sset_count, 4257 .get_sset_count = mv88e6xxx_get_sset_count,
diff --git a/drivers/net/dsa/mv88e6xxx/port.c b/drivers/net/dsa/mv88e6xxx/port.c
index 6315774d72b3..429d0ebcd5b1 100644
--- a/drivers/net/dsa/mv88e6xxx/port.c
+++ b/drivers/net/dsa/mv88e6xxx/port.c
@@ -15,6 +15,7 @@
15#include <linux/bitfield.h> 15#include <linux/bitfield.h>
16#include <linux/if_bridge.h> 16#include <linux/if_bridge.h>
17#include <linux/phy.h> 17#include <linux/phy.h>
18#include <linux/phylink.h>
18 19
19#include "chip.h" 20#include "chip.h"
20#include "port.h" 21#include "port.h"
@@ -378,6 +379,44 @@ int mv88e6xxx_port_get_cmode(struct mv88e6xxx_chip *chip, int port, u8 *cmode)
378 return 0; 379 return 0;
379} 380}
380 381
382int mv88e6xxx_port_link_state(struct mv88e6xxx_chip *chip, int port,
383 struct phylink_link_state *state)
384{
385 int err;
386 u16 reg;
387
388 err = mv88e6xxx_port_read(chip, port, MV88E6XXX_PORT_STS, &reg);
389 if (err)
390 return err;
391
392 switch (reg & MV88E6XXX_PORT_STS_SPEED_MASK) {
393 case MV88E6XXX_PORT_STS_SPEED_10:
394 state->speed = SPEED_10;
395 break;
396 case MV88E6XXX_PORT_STS_SPEED_100:
397 state->speed = SPEED_100;
398 break;
399 case MV88E6XXX_PORT_STS_SPEED_1000:
400 state->speed = SPEED_1000;
401 break;
402 case MV88E6XXX_PORT_STS_SPEED_10000:
403 if ((reg &MV88E6XXX_PORT_STS_CMODE_MASK) ==
404 MV88E6XXX_PORT_STS_CMODE_2500BASEX)
405 state->speed = SPEED_2500;
406 else
407 state->speed = SPEED_10000;
408 break;
409 }
410
411 state->duplex = reg & MV88E6XXX_PORT_STS_DUPLEX ?
412 DUPLEX_FULL : DUPLEX_HALF;
413 state->link = !!(reg & MV88E6XXX_PORT_STS_LINK);
414 state->an_enabled = 1;
415 state->an_complete = state->link;
416
417 return 0;
418}
419
381/* Offset 0x02: Jamming Control 420/* Offset 0x02: Jamming Control
382 * 421 *
383 * Do not limit the period of time that this port can be paused for by 422 * Do not limit the period of time that this port can be paused for by
diff --git a/drivers/net/dsa/mv88e6xxx/port.h b/drivers/net/dsa/mv88e6xxx/port.h
index b16d5f0e6e9c..5e1db1b221ca 100644
--- a/drivers/net/dsa/mv88e6xxx/port.h
+++ b/drivers/net/dsa/mv88e6xxx/port.h
@@ -29,6 +29,7 @@
29#define MV88E6XXX_PORT_STS_SPEED_10 0x0000 29#define MV88E6XXX_PORT_STS_SPEED_10 0x0000
30#define MV88E6XXX_PORT_STS_SPEED_100 0x0100 30#define MV88E6XXX_PORT_STS_SPEED_100 0x0100
31#define MV88E6XXX_PORT_STS_SPEED_1000 0x0200 31#define MV88E6XXX_PORT_STS_SPEED_1000 0x0200
32#define MV88E6XXX_PORT_STS_SPEED_10000 0x0300
32#define MV88E6352_PORT_STS_EEE 0x0040 33#define MV88E6352_PORT_STS_EEE 0x0040
33#define MV88E6165_PORT_STS_AM_DIS 0x0040 34#define MV88E6165_PORT_STS_AM_DIS 0x0040
34#define MV88E6185_PORT_STS_MGMII 0x0040 35#define MV88E6185_PORT_STS_MGMII 0x0040
@@ -295,6 +296,8 @@ int mv88e6390_port_pause_limit(struct mv88e6xxx_chip *chip, int port, u8 in,
295int mv88e6390x_port_set_cmode(struct mv88e6xxx_chip *chip, int port, 296int mv88e6390x_port_set_cmode(struct mv88e6xxx_chip *chip, int port,
296 phy_interface_t mode); 297 phy_interface_t mode);
297int mv88e6xxx_port_get_cmode(struct mv88e6xxx_chip *chip, int port, u8 *cmode); 298int mv88e6xxx_port_get_cmode(struct mv88e6xxx_chip *chip, int port, u8 *cmode);
299int mv88e6xxx_port_link_state(struct mv88e6xxx_chip *chip, int port,
300 struct phylink_link_state *state);
298int mv88e6xxx_port_set_map_da(struct mv88e6xxx_chip *chip, int port); 301int mv88e6xxx_port_set_map_da(struct mv88e6xxx_chip *chip, int port);
299int mv88e6095_port_set_upstream_port(struct mv88e6xxx_chip *chip, int port, 302int mv88e6095_port_set_upstream_port(struct mv88e6xxx_chip *chip, int port,
300 int upstream_port); 303 int upstream_port);
diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
index c582b2d7546c..581ce93ecaf9 100644
--- a/drivers/net/phy/phylink.c
+++ b/drivers/net/phy/phylink.c
@@ -19,6 +19,7 @@
19#include <linux/phylink.h> 19#include <linux/phylink.h>
20#include <linux/rtnetlink.h> 20#include <linux/rtnetlink.h>
21#include <linux/spinlock.h> 21#include <linux/spinlock.h>
22#include <linux/timer.h>
22#include <linux/workqueue.h> 23#include <linux/workqueue.h>
23 24
24#include "sfp.h" 25#include "sfp.h"
@@ -54,6 +55,7 @@ struct phylink {
54 /* The link configuration settings */ 55 /* The link configuration settings */
55 struct phylink_link_state link_config; 56 struct phylink_link_state link_config;
56 struct gpio_desc *link_gpio; 57 struct gpio_desc *link_gpio;
58 struct timer_list link_poll;
57 void (*get_fixed_state)(struct net_device *dev, 59 void (*get_fixed_state)(struct net_device *dev,
58 struct phylink_link_state *s); 60 struct phylink_link_state *s);
59 61
@@ -360,7 +362,7 @@ static void phylink_get_fixed_state(struct phylink *pl, struct phylink_link_stat
360 if (pl->get_fixed_state) 362 if (pl->get_fixed_state)
361 pl->get_fixed_state(pl->netdev, state); 363 pl->get_fixed_state(pl->netdev, state);
362 else if (pl->link_gpio) 364 else if (pl->link_gpio)
363 state->link = !!gpiod_get_value(pl->link_gpio); 365 state->link = !!gpiod_get_value_cansleep(pl->link_gpio);
364} 366}
365 367
366/* Flow control is resolved according to our and the link partners 368/* Flow control is resolved according to our and the link partners
@@ -500,6 +502,15 @@ static void phylink_run_resolve(struct phylink *pl)
500 queue_work(system_power_efficient_wq, &pl->resolve); 502 queue_work(system_power_efficient_wq, &pl->resolve);
501} 503}
502 504
505static void phylink_fixed_poll(struct timer_list *t)
506{
507 struct phylink *pl = container_of(t, struct phylink, link_poll);
508
509 mod_timer(t, jiffies + HZ);
510
511 phylink_run_resolve(pl);
512}
513
503static const struct sfp_upstream_ops sfp_phylink_ops; 514static const struct sfp_upstream_ops sfp_phylink_ops;
504 515
505static int phylink_register_sfp(struct phylink *pl, 516static int phylink_register_sfp(struct phylink *pl,
@@ -572,6 +583,7 @@ struct phylink *phylink_create(struct net_device *ndev,
572 pl->link_config.an_enabled = true; 583 pl->link_config.an_enabled = true;
573 pl->ops = ops; 584 pl->ops = ops;
574 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state); 585 __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
586 timer_setup(&pl->link_poll, phylink_fixed_poll, 0);
575 587
576 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS); 588 bitmap_fill(pl->supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
577 linkmode_copy(pl->link_config.advertising, pl->supported); 589 linkmode_copy(pl->link_config.advertising, pl->supported);
@@ -612,6 +624,8 @@ void phylink_destroy(struct phylink *pl)
612{ 624{
613 if (pl->sfp_bus) 625 if (pl->sfp_bus)
614 sfp_unregister_upstream(pl->sfp_bus); 626 sfp_unregister_upstream(pl->sfp_bus);
627 if (!IS_ERR(pl->link_gpio))
628 gpiod_put(pl->link_gpio);
615 629
616 cancel_work_sync(&pl->resolve); 630 cancel_work_sync(&pl->resolve);
617 kfree(pl); 631 kfree(pl);
@@ -903,6 +917,8 @@ void phylink_start(struct phylink *pl)
903 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state); 917 clear_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
904 phylink_run_resolve(pl); 918 phylink_run_resolve(pl);
905 919
920 if (pl->link_an_mode == MLO_AN_FIXED && !IS_ERR(pl->link_gpio))
921 mod_timer(&pl->link_poll, jiffies + HZ);
906 if (pl->sfp_bus) 922 if (pl->sfp_bus)
907 sfp_upstream_start(pl->sfp_bus); 923 sfp_upstream_start(pl->sfp_bus);
908 if (pl->phydev) 924 if (pl->phydev)
@@ -927,6 +943,8 @@ void phylink_stop(struct phylink *pl)
927 phy_stop(pl->phydev); 943 phy_stop(pl->phydev);
928 if (pl->sfp_bus) 944 if (pl->sfp_bus)
929 sfp_upstream_stop(pl->sfp_bus); 945 sfp_upstream_stop(pl->sfp_bus);
946 if (pl->link_an_mode == MLO_AN_FIXED && !IS_ERR(pl->link_gpio))
947 del_timer_sync(&pl->link_poll);
930 948
931 set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state); 949 set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state);
932 queue_work(system_power_efficient_wq, &pl->resolve); 950 queue_work(system_power_efficient_wq, &pl->resolve);
diff --git a/include/net/dsa.h b/include/net/dsa.h
index 462e9741b210..fdbd6082945d 100644
--- a/include/net/dsa.h
+++ b/include/net/dsa.h
@@ -20,12 +20,14 @@
20#include <linux/of.h> 20#include <linux/of.h>
21#include <linux/ethtool.h> 21#include <linux/ethtool.h>
22#include <linux/net_tstamp.h> 22#include <linux/net_tstamp.h>
23#include <linux/phy.h>
23#include <net/devlink.h> 24#include <net/devlink.h>
24#include <net/switchdev.h> 25#include <net/switchdev.h>
25 26
26struct tc_action; 27struct tc_action;
27struct phy_device; 28struct phy_device;
28struct fixed_phy_status; 29struct fixed_phy_status;
30struct phylink_link_state;
29 31
30enum dsa_tag_protocol { 32enum dsa_tag_protocol {
31 DSA_TAG_PROTO_NONE = 0, 33 DSA_TAG_PROTO_NONE = 0,
@@ -199,6 +201,7 @@ struct dsa_port {
199 u8 stp_state; 201 u8 stp_state;
200 struct net_device *bridge_dev; 202 struct net_device *bridge_dev;
201 struct devlink_port devlink_port; 203 struct devlink_port devlink_port;
204 struct phylink *pl;
202 /* 205 /*
203 * Original copy of the master netdev ethtool_ops 206 * Original copy of the master netdev ethtool_ops
204 */ 207 */
@@ -354,6 +357,27 @@ struct dsa_switch_ops {
354 struct fixed_phy_status *st); 357 struct fixed_phy_status *st);
355 358
356 /* 359 /*
360 * PHYLINK integration
361 */
362 void (*phylink_validate)(struct dsa_switch *ds, int port,
363 unsigned long *supported,
364 struct phylink_link_state *state);
365 int (*phylink_mac_link_state)(struct dsa_switch *ds, int port,
366 struct phylink_link_state *state);
367 void (*phylink_mac_config)(struct dsa_switch *ds, int port,
368 unsigned int mode,
369 const struct phylink_link_state *state);
370 void (*phylink_mac_an_restart)(struct dsa_switch *ds, int port);
371 void (*phylink_mac_link_down)(struct dsa_switch *ds, int port,
372 unsigned int mode,
373 phy_interface_t interface);
374 void (*phylink_mac_link_up)(struct dsa_switch *ds, int port,
375 unsigned int mode,
376 phy_interface_t interface,
377 struct phy_device *phydev);
378 void (*phylink_fixed_state)(struct dsa_switch *ds, int port,
379 struct phylink_link_state *state);
380 /*
357 * ethtool hardware statistics. 381 * ethtool hardware statistics.
358 */ 382 */
359 void (*get_strings)(struct dsa_switch *ds, int port, 383 void (*get_strings)(struct dsa_switch *ds, int port,
@@ -595,5 +619,6 @@ static inline int call_dsa_notifiers(unsigned long val, struct net_device *dev,
595int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data); 619int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
596int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data); 620int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data);
597int dsa_port_get_phy_sset_count(struct dsa_port *dp); 621int dsa_port_get_phy_sset_count(struct dsa_port *dp);
622void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up);
598 623
599#endif 624#endif
diff --git a/net/dsa/Kconfig b/net/dsa/Kconfig
index bbf2c82cf7b2..4183e4ba27a5 100644
--- a/net/dsa/Kconfig
+++ b/net/dsa/Kconfig
@@ -9,7 +9,7 @@ config NET_DSA
9 depends on HAVE_NET_DSA && MAY_USE_DEVLINK 9 depends on HAVE_NET_DSA && MAY_USE_DEVLINK
10 depends on BRIDGE || BRIDGE=n 10 depends on BRIDGE || BRIDGE=n
11 select NET_SWITCHDEV 11 select NET_SWITCHDEV
12 select PHYLIB 12 select PHYLINK
13 ---help--- 13 ---help---
14 Say Y if you want to enable support for the hardware switches supported 14 Say Y if you want to enable support for the hardware switches supported
15 by the Distributed Switch Architecture. 15 by the Distributed Switch Architecture.
diff --git a/net/dsa/dsa_priv.h b/net/dsa/dsa_priv.h
index 053731473c99..3964c6f7a7c0 100644
--- a/net/dsa/dsa_priv.h
+++ b/net/dsa/dsa_priv.h
@@ -75,15 +75,6 @@ struct dsa_slave_priv {
75 /* DSA port data, such as switch, port index, etc. */ 75 /* DSA port data, such as switch, port index, etc. */
76 struct dsa_port *dp; 76 struct dsa_port *dp;
77 77
78 /*
79 * The phylib phy_device pointer for the PHY connected
80 * to this port.
81 */
82 phy_interface_t phy_interface;
83 int old_link;
84 int old_pause;
85 int old_duplex;
86
87#ifdef CONFIG_NET_POLL_CONTROLLER 78#ifdef CONFIG_NET_POLL_CONTROLLER
88 struct netpoll *netpoll; 79 struct netpoll *netpoll;
89#endif 80#endif
diff --git a/net/dsa/slave.c b/net/dsa/slave.c
index 746ab428a17a..1e3b6a6d8a40 100644
--- a/net/dsa/slave.c
+++ b/net/dsa/slave.c
@@ -13,6 +13,7 @@
13#include <linux/netdevice.h> 13#include <linux/netdevice.h>
14#include <linux/phy.h> 14#include <linux/phy.h>
15#include <linux/phy_fixed.h> 15#include <linux/phy_fixed.h>
16#include <linux/phylink.h>
16#include <linux/of_net.h> 17#include <linux/of_net.h>
17#include <linux/of_mdio.h> 18#include <linux/of_mdio.h>
18#include <linux/mdio.h> 19#include <linux/mdio.h>
@@ -97,8 +98,7 @@ static int dsa_slave_open(struct net_device *dev)
97 if (err) 98 if (err)
98 goto clear_promisc; 99 goto clear_promisc;
99 100
100 if (dev->phydev) 101 phylink_start(dp->pl);
101 phy_start(dev->phydev);
102 102
103 return 0; 103 return 0;
104 104
@@ -120,8 +120,7 @@ static int dsa_slave_close(struct net_device *dev)
120 struct net_device *master = dsa_slave_to_master(dev); 120 struct net_device *master = dsa_slave_to_master(dev);
121 struct dsa_port *dp = dsa_slave_to_port(dev); 121 struct dsa_port *dp = dsa_slave_to_port(dev);
122 122
123 if (dev->phydev) 123 phylink_stop(dp->pl);
124 phy_stop(dev->phydev);
125 124
126 dsa_port_disable(dp, dev->phydev); 125 dsa_port_disable(dp, dev->phydev);
127 126
@@ -272,10 +271,7 @@ static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
272 break; 271 break;
273 } 272 }
274 273
275 if (!dev->phydev) 274 return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
276 return -ENODEV;
277
278 return phy_mii_ioctl(dev->phydev, ifr, cmd);
279} 275}
280 276
281static int dsa_slave_port_attr_set(struct net_device *dev, 277static int dsa_slave_port_attr_set(struct net_device *dev,
@@ -498,14 +494,11 @@ dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
498 ds->ops->get_regs(ds, dp->index, regs, _p); 494 ds->ops->get_regs(ds, dp->index, regs, _p);
499} 495}
500 496
501static u32 dsa_slave_get_link(struct net_device *dev) 497static int dsa_slave_nway_reset(struct net_device *dev)
502{ 498{
503 if (!dev->phydev) 499 struct dsa_port *dp = dsa_slave_to_port(dev);
504 return -ENODEV;
505
506 genphy_update_link(dev->phydev);
507 500
508 return dev->phydev->link; 501 return phylink_ethtool_nway_reset(dp->pl);
509} 502}
510 503
511static int dsa_slave_get_eeprom_len(struct net_device *dev) 504static int dsa_slave_get_eeprom_len(struct net_device *dev)
@@ -619,6 +612,8 @@ static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
619 struct dsa_port *dp = dsa_slave_to_port(dev); 612 struct dsa_port *dp = dsa_slave_to_port(dev);
620 struct dsa_switch *ds = dp->ds; 613 struct dsa_switch *ds = dp->ds;
621 614
615 phylink_ethtool_get_wol(dp->pl, w);
616
622 if (ds->ops->get_wol) 617 if (ds->ops->get_wol)
623 ds->ops->get_wol(ds, dp->index, w); 618 ds->ops->get_wol(ds, dp->index, w);
624} 619}
@@ -629,6 +624,8 @@ static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
629 struct dsa_switch *ds = dp->ds; 624 struct dsa_switch *ds = dp->ds;
630 int ret = -EOPNOTSUPP; 625 int ret = -EOPNOTSUPP;
631 626
627 phylink_ethtool_set_wol(dp->pl, w);
628
632 if (ds->ops->set_wol) 629 if (ds->ops->set_wol)
633 ret = ds->ops->set_wol(ds, dp->index, w); 630 ret = ds->ops->set_wol(ds, dp->index, w);
634 631
@@ -652,13 +649,7 @@ static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
652 if (ret) 649 if (ret)
653 return ret; 650 return ret;
654 651
655 if (e->eee_enabled) { 652 return phylink_ethtool_set_eee(dp->pl, e);
656 ret = phy_init_eee(dev->phydev, 0);
657 if (ret)
658 return ret;
659 }
660
661 return phy_ethtool_set_eee(dev->phydev, e);
662} 653}
663 654
664static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e) 655static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
@@ -678,7 +669,23 @@ static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
678 if (ret) 669 if (ret)
679 return ret; 670 return ret;
680 671
681 return phy_ethtool_get_eee(dev->phydev, e); 672 return phylink_ethtool_get_eee(dp->pl, e);
673}
674
675static int dsa_slave_get_link_ksettings(struct net_device *dev,
676 struct ethtool_link_ksettings *cmd)
677{
678 struct dsa_port *dp = dsa_slave_to_port(dev);
679
680 return phylink_ethtool_ksettings_get(dp->pl, cmd);
681}
682
683static int dsa_slave_set_link_ksettings(struct net_device *dev,
684 const struct ethtool_link_ksettings *cmd)
685{
686 struct dsa_port *dp = dsa_slave_to_port(dev);
687
688 return phylink_ethtool_ksettings_set(dp->pl, cmd);
682} 689}
683 690
684#ifdef CONFIG_NET_POLL_CONTROLLER 691#ifdef CONFIG_NET_POLL_CONTROLLER
@@ -981,8 +988,8 @@ static const struct ethtool_ops dsa_slave_ethtool_ops = {
981 .get_drvinfo = dsa_slave_get_drvinfo, 988 .get_drvinfo = dsa_slave_get_drvinfo,
982 .get_regs_len = dsa_slave_get_regs_len, 989 .get_regs_len = dsa_slave_get_regs_len,
983 .get_regs = dsa_slave_get_regs, 990 .get_regs = dsa_slave_get_regs,
984 .nway_reset = phy_ethtool_nway_reset, 991 .nway_reset = dsa_slave_nway_reset,
985 .get_link = dsa_slave_get_link, 992 .get_link = ethtool_op_get_link,
986 .get_eeprom_len = dsa_slave_get_eeprom_len, 993 .get_eeprom_len = dsa_slave_get_eeprom_len,
987 .get_eeprom = dsa_slave_get_eeprom, 994 .get_eeprom = dsa_slave_get_eeprom,
988 .set_eeprom = dsa_slave_set_eeprom, 995 .set_eeprom = dsa_slave_set_eeprom,
@@ -993,8 +1000,8 @@ static const struct ethtool_ops dsa_slave_ethtool_ops = {
993 .get_wol = dsa_slave_get_wol, 1000 .get_wol = dsa_slave_get_wol,
994 .set_eee = dsa_slave_set_eee, 1001 .set_eee = dsa_slave_set_eee,
995 .get_eee = dsa_slave_get_eee, 1002 .get_eee = dsa_slave_get_eee,
996 .get_link_ksettings = phy_ethtool_get_link_ksettings, 1003 .get_link_ksettings = dsa_slave_get_link_ksettings,
997 .set_link_ksettings = phy_ethtool_set_link_ksettings, 1004 .set_link_ksettings = dsa_slave_set_link_ksettings,
998 .get_rxnfc = dsa_slave_get_rxnfc, 1005 .get_rxnfc = dsa_slave_get_rxnfc,
999 .set_rxnfc = dsa_slave_set_rxnfc, 1006 .set_rxnfc = dsa_slave_set_rxnfc,
1000 .get_ts_info = dsa_slave_get_ts_info, 1007 .get_ts_info = dsa_slave_get_ts_info,
@@ -1053,56 +1060,122 @@ static struct device_type dsa_type = {
1053 .name = "dsa", 1060 .name = "dsa",
1054}; 1061};
1055 1062
1056static void dsa_slave_adjust_link(struct net_device *dev) 1063static void dsa_slave_phylink_validate(struct net_device *dev,
1064 unsigned long *supported,
1065 struct phylink_link_state *state)
1057{ 1066{
1058 struct dsa_port *dp = dsa_slave_to_port(dev); 1067 struct dsa_port *dp = dsa_slave_to_port(dev);
1059 struct dsa_slave_priv *p = netdev_priv(dev);
1060 struct dsa_switch *ds = dp->ds; 1068 struct dsa_switch *ds = dp->ds;
1061 unsigned int status_changed = 0;
1062 1069
1063 if (p->old_link != dev->phydev->link) { 1070 if (!ds->ops->phylink_validate)
1064 status_changed = 1; 1071 return;
1065 p->old_link = dev->phydev->link;
1066 }
1067 1072
1068 if (p->old_duplex != dev->phydev->duplex) { 1073 ds->ops->phylink_validate(ds, dp->index, supported, state);
1069 status_changed = 1; 1074}
1070 p->old_duplex = dev->phydev->duplex;
1071 }
1072 1075
1073 if (p->old_pause != dev->phydev->pause) { 1076static int dsa_slave_phylink_mac_link_state(struct net_device *dev,
1074 status_changed = 1; 1077 struct phylink_link_state *state)
1075 p->old_pause = dev->phydev->pause; 1078{
1076 } 1079 struct dsa_port *dp = dsa_slave_to_port(dev);
1080 struct dsa_switch *ds = dp->ds;
1077 1081
1078 if (ds->ops->adjust_link && status_changed) 1082 /* Only called for SGMII and 802.3z */
1079 ds->ops->adjust_link(ds, dp->index, dev->phydev); 1083 if (!ds->ops->phylink_mac_link_state)
1084 return -EOPNOTSUPP;
1080 1085
1081 if (status_changed) 1086 return ds->ops->phylink_mac_link_state(ds, dp->index, state);
1082 phy_print_status(dev->phydev);
1083} 1087}
1084 1088
1085static int dsa_slave_fixed_link_update(struct net_device *dev, 1089static void dsa_slave_phylink_mac_config(struct net_device *dev,
1086 struct fixed_phy_status *status) 1090 unsigned int mode,
1091 const struct phylink_link_state *state)
1087{ 1092{
1088 struct dsa_switch *ds; 1093 struct dsa_port *dp = dsa_slave_to_port(dev);
1089 struct dsa_port *dp; 1094 struct dsa_switch *ds = dp->ds;
1090 1095
1091 if (dev) { 1096 if (!ds->ops->phylink_mac_config)
1092 dp = dsa_slave_to_port(dev); 1097 return;
1093 ds = dp->ds; 1098
1094 if (ds->ops->fixed_link_update) 1099 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
1095 ds->ops->fixed_link_update(ds, dp->index, status); 1100}
1101
1102static void dsa_slave_phylink_mac_an_restart(struct net_device *dev)
1103{
1104 struct dsa_port *dp = dsa_slave_to_port(dev);
1105 struct dsa_switch *ds = dp->ds;
1106
1107 if (!ds->ops->phylink_mac_an_restart)
1108 return;
1109
1110 ds->ops->phylink_mac_an_restart(ds, dp->index);
1111}
1112
1113static void dsa_slave_phylink_mac_link_down(struct net_device *dev,
1114 unsigned int mode,
1115 phy_interface_t interface)
1116{
1117 struct dsa_port *dp = dsa_slave_to_port(dev);
1118 struct dsa_switch *ds = dp->ds;
1119
1120 if (!ds->ops->phylink_mac_link_down) {
1121 if (ds->ops->adjust_link && dev->phydev)
1122 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1123 return;
1096 } 1124 }
1097 1125
1098 return 0; 1126 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1127}
1128
1129static void dsa_slave_phylink_mac_link_up(struct net_device *dev,
1130 unsigned int mode,
1131 phy_interface_t interface,
1132 struct phy_device *phydev)
1133{
1134 struct dsa_port *dp = dsa_slave_to_port(dev);
1135 struct dsa_switch *ds = dp->ds;
1136
1137 if (!ds->ops->phylink_mac_link_up) {
1138 if (ds->ops->adjust_link && dev->phydev)
1139 ds->ops->adjust_link(ds, dp->index, dev->phydev);
1140 return;
1141 }
1142
1143 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev);
1144}
1145
1146static const struct phylink_mac_ops dsa_slave_phylink_mac_ops = {
1147 .validate = dsa_slave_phylink_validate,
1148 .mac_link_state = dsa_slave_phylink_mac_link_state,
1149 .mac_config = dsa_slave_phylink_mac_config,
1150 .mac_an_restart = dsa_slave_phylink_mac_an_restart,
1151 .mac_link_down = dsa_slave_phylink_mac_link_down,
1152 .mac_link_up = dsa_slave_phylink_mac_link_up,
1153};
1154
1155void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1156{
1157 const struct dsa_port *dp = dsa_to_port(ds, port);
1158
1159 phylink_mac_change(dp->pl, up);
1160}
1161EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1162
1163static void dsa_slave_phylink_fixed_state(struct net_device *dev,
1164 struct phylink_link_state *state)
1165{
1166 struct dsa_port *dp = dsa_slave_to_port(dev);
1167 struct dsa_switch *ds = dp->ds;
1168
1169 /* No need to check that this operation is valid, the callback would
1170 * not be called if it was not.
1171 */
1172 ds->ops->phylink_fixed_state(ds, dp->index, state);
1099} 1173}
1100 1174
1101/* slave device setup *******************************************************/ 1175/* slave device setup *******************************************************/
1102static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr) 1176static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1103{ 1177{
1104 struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1178 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1105 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1106 struct dsa_switch *ds = dp->ds; 1179 struct dsa_switch *ds = dp->ds;
1107 1180
1108 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr); 1181 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
@@ -1111,75 +1184,54 @@ static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
1111 return -ENODEV; 1184 return -ENODEV;
1112 } 1185 }
1113 1186
1114 /* Use already configured phy mode */ 1187 return phylink_connect_phy(dp->pl, slave_dev->phydev);
1115 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
1116 p->phy_interface = slave_dev->phydev->interface;
1117
1118 return phy_connect_direct(slave_dev, slave_dev->phydev,
1119 dsa_slave_adjust_link, p->phy_interface);
1120} 1188}
1121 1189
1122static int dsa_slave_phy_setup(struct net_device *slave_dev) 1190static int dsa_slave_phy_setup(struct net_device *slave_dev)
1123{ 1191{
1124 struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1192 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1125 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1126 struct device_node *port_dn = dp->dn; 1193 struct device_node *port_dn = dp->dn;
1127 struct dsa_switch *ds = dp->ds; 1194 struct dsa_switch *ds = dp->ds;
1128 struct device_node *phy_dn;
1129 bool phy_is_fixed = false;
1130 u32 phy_flags = 0; 1195 u32 phy_flags = 0;
1131 int mode, ret; 1196 int mode, ret;
1132 1197
1133 mode = of_get_phy_mode(port_dn); 1198 mode = of_get_phy_mode(port_dn);
1134 if (mode < 0) 1199 if (mode < 0)
1135 mode = PHY_INTERFACE_MODE_NA; 1200 mode = PHY_INTERFACE_MODE_NA;
1136 p->phy_interface = mode;
1137 1201
1138 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0); 1202 dp->pl = phylink_create(slave_dev, of_fwnode_handle(port_dn), mode,
1139 if (!phy_dn && of_phy_is_fixed_link(port_dn)) { 1203 &dsa_slave_phylink_mac_ops);
1140 /* In the case of a fixed PHY, the DT node associated 1204 if (IS_ERR(dp->pl)) {
1141 * to the fixed PHY is the Port DT node 1205 netdev_err(slave_dev,
1142 */ 1206 "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1143 ret = of_phy_register_fixed_link(port_dn); 1207 return PTR_ERR(dp->pl);
1144 if (ret) {
1145 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
1146 return ret;
1147 }
1148 phy_is_fixed = true;
1149 phy_dn = of_node_get(port_dn);
1150 } 1208 }
1151 1209
1210 /* Register only if the switch provides such a callback, since this
1211 * callback takes precedence over polling the link GPIO in PHYLINK
1212 * (see phylink_get_fixed_state).
1213 */
1214 if (ds->ops->phylink_fixed_state)
1215 phylink_fixed_state_cb(dp->pl, dsa_slave_phylink_fixed_state);
1216
1152 if (ds->ops->get_phy_flags) 1217 if (ds->ops->get_phy_flags)
1153 phy_flags = ds->ops->get_phy_flags(ds, dp->index); 1218 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
1154 1219
1155 if (phy_dn) { 1220 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
1156 slave_dev->phydev = of_phy_connect(slave_dev, phy_dn, 1221 if (ret == -ENODEV) {
1157 dsa_slave_adjust_link, 1222 /* We could not connect to a designated PHY or SFP, so use the
1158 phy_flags, 1223 * switch internal MDIO bus instead
1159 p->phy_interface); 1224 */
1160 of_node_put(phy_dn);
1161 }
1162
1163 if (slave_dev->phydev && phy_is_fixed)
1164 fixed_phy_set_link_update(slave_dev->phydev,
1165 dsa_slave_fixed_link_update);
1166
1167 /* We could not connect to a designated PHY, so use the switch internal
1168 * MDIO bus instead
1169 */
1170 if (!slave_dev->phydev) {
1171 ret = dsa_slave_phy_connect(slave_dev, dp->index); 1225 ret = dsa_slave_phy_connect(slave_dev, dp->index);
1172 if (ret) { 1226 if (ret) {
1173 netdev_err(slave_dev, "failed to connect to port %d: %d\n", 1227 netdev_err(slave_dev,
1228 "failed to connect to port %d: %d\n",
1174 dp->index, ret); 1229 dp->index, ret);
1175 if (phy_is_fixed) 1230 phylink_destroy(dp->pl);
1176 of_phy_deregister_fixed_link(port_dn);
1177 return ret; 1231 return ret;
1178 } 1232 }
1179 } 1233 }
1180 1234
1181 phy_attached_info(slave_dev->phydev);
1182
1183 return 0; 1235 return 0;
1184} 1236}
1185 1237
@@ -1194,29 +1246,26 @@ static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1194 1246
1195int dsa_slave_suspend(struct net_device *slave_dev) 1247int dsa_slave_suspend(struct net_device *slave_dev)
1196{ 1248{
1197 struct dsa_slave_priv *p = netdev_priv(slave_dev); 1249 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1198 1250
1199 netif_device_detach(slave_dev); 1251 netif_device_detach(slave_dev);
1200 1252
1201 if (slave_dev->phydev) { 1253 rtnl_lock();
1202 phy_stop(slave_dev->phydev); 1254 phylink_stop(dp->pl);
1203 p->old_pause = -1; 1255 rtnl_unlock();
1204 p->old_link = -1;
1205 p->old_duplex = -1;
1206 phy_suspend(slave_dev->phydev);
1207 }
1208 1256
1209 return 0; 1257 return 0;
1210} 1258}
1211 1259
1212int dsa_slave_resume(struct net_device *slave_dev) 1260int dsa_slave_resume(struct net_device *slave_dev)
1213{ 1261{
1262 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1263
1214 netif_device_attach(slave_dev); 1264 netif_device_attach(slave_dev);
1215 1265
1216 if (slave_dev->phydev) { 1266 rtnl_lock();
1217 phy_resume(slave_dev->phydev); 1267 phylink_start(dp->pl);
1218 phy_start(slave_dev->phydev); 1268 rtnl_unlock();
1219 }
1220 1269
1221 return 0; 1270 return 0;
1222} 1271}
@@ -1281,11 +1330,6 @@ int dsa_slave_create(struct dsa_port *port)
1281 p->dp = port; 1330 p->dp = port;
1282 INIT_LIST_HEAD(&p->mall_tc_list); 1331 INIT_LIST_HEAD(&p->mall_tc_list);
1283 p->xmit = cpu_dp->tag_ops->xmit; 1332 p->xmit = cpu_dp->tag_ops->xmit;
1284
1285 p->old_pause = -1;
1286 p->old_link = -1;
1287 p->old_duplex = -1;
1288
1289 port->slave = slave_dev; 1333 port->slave = slave_dev;
1290 1334
1291 netif_carrier_off(slave_dev); 1335 netif_carrier_off(slave_dev);
@@ -1308,9 +1352,10 @@ int dsa_slave_create(struct dsa_port *port)
1308 return 0; 1352 return 0;
1309 1353
1310out_phy: 1354out_phy:
1311 phy_disconnect(slave_dev->phydev); 1355 rtnl_lock();
1312 if (of_phy_is_fixed_link(port->dn)) 1356 phylink_disconnect_phy(p->dp->pl);
1313 of_phy_deregister_fixed_link(port->dn); 1357 rtnl_unlock();
1358 phylink_destroy(p->dp->pl);
1314out_free: 1359out_free:
1315 free_percpu(p->stats64); 1360 free_percpu(p->stats64);
1316 free_netdev(slave_dev); 1361 free_netdev(slave_dev);
@@ -1322,17 +1367,15 @@ void dsa_slave_destroy(struct net_device *slave_dev)
1322{ 1367{
1323 struct dsa_port *dp = dsa_slave_to_port(slave_dev); 1368 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1324 struct dsa_slave_priv *p = netdev_priv(slave_dev); 1369 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1325 struct device_node *port_dn = dp->dn;
1326 1370
1327 netif_carrier_off(slave_dev); 1371 netif_carrier_off(slave_dev);
1328 if (slave_dev->phydev) { 1372 rtnl_lock();
1329 phy_disconnect(slave_dev->phydev); 1373 phylink_disconnect_phy(dp->pl);
1374 rtnl_unlock();
1330 1375
1331 if (of_phy_is_fixed_link(port_dn))
1332 of_phy_deregister_fixed_link(port_dn);
1333 }
1334 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER); 1376 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
1335 unregister_netdev(slave_dev); 1377 unregister_netdev(slave_dev);
1378 phylink_destroy(dp->pl);
1336 free_percpu(p->stats64); 1379 free_percpu(p->stats64);
1337 free_netdev(slave_dev); 1380 free_netdev(slave_dev);
1338} 1381}