Skip to content

Commit f1e911d

Browse files
hkallweitdavem330
authored andcommitted
r8169: add basic phylib support
Add basic phylib support to r8169. All now unneeded old PHY handling code will be removed in subsequent patches. Signed-off-by: Heiner Kallweit <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent fcaccc8 commit f1e911d

File tree

2 files changed

+128
-32
lines changed

2 files changed

+128
-32
lines changed

drivers/net/ethernet/realtek/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ config R8169
9999
depends on PCI
100100
select FW_LOADER
101101
select CRC32
102+
select PHYLIB
102103
select MII
103104
---help---
104105
Say Y here if you have a Realtek 8169 PCI Gigabit Ethernet adapter.

drivers/net/ethernet/realtek/r8169.c

Lines changed: 127 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/delay.h>
1717
#include <linux/ethtool.h>
1818
#include <linux/mii.h>
19+
#include <linux/phy.h>
1920
#include <linux/if_vlan.h>
2021
#include <linux/crc32.h>
2122
#include <linux/in.h>
@@ -754,6 +755,7 @@ struct rtl8169_private {
754755
} wk;
755756

756757
struct mii_if_info mii;
758+
struct mii_bus *mii_bus;
757759
dma_addr_t counters_phys_addr;
758760
struct rtl8169_counters *counters;
759761
struct rtl8169_tc_offsets tc_offset;
@@ -1444,11 +1446,6 @@ static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
14441446
return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
14451447
}
14461448

1447-
static unsigned int rtl8169_xmii_link_ok(struct rtl8169_private *tp)
1448-
{
1449-
return RTL_R8(tp, PHYstatus) & LinkStatus;
1450-
}
1451-
14521449
static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
14531450
{
14541451
unsigned int val;
@@ -1513,25 +1510,6 @@ static void rtl_link_chg_patch(struct rtl8169_private *tp)
15131510
}
15141511
}
15151512

1516-
static void rtl8169_check_link_status(struct net_device *dev,
1517-
struct rtl8169_private *tp)
1518-
{
1519-
struct device *d = tp_to_dev(tp);
1520-
1521-
if (rtl8169_xmii_link_ok(tp)) {
1522-
rtl_link_chg_patch(tp);
1523-
/* This is to cancel a scheduled suspend if there's one. */
1524-
pm_request_resume(d);
1525-
netif_carrier_on(dev);
1526-
if (net_ratelimit())
1527-
netif_info(tp, ifup, dev, "link up\n");
1528-
} else {
1529-
netif_carrier_off(dev);
1530-
netif_info(tp, ifdown, dev, "link down\n");
1531-
pm_runtime_idle(d);
1532-
}
1533-
}
1534-
15351513
#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
15361514

15371515
static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
@@ -6221,7 +6199,6 @@ static void rtl_reset_work(struct rtl8169_private *tp)
62216199
napi_enable(&tp->napi);
62226200
rtl_hw_start(tp);
62236201
netif_wake_queue(dev);
6224-
rtl8169_check_link_status(dev, tp);
62256202
}
62266203

62276204
static void rtl8169_tx_timeout(struct net_device *dev)
@@ -6838,7 +6815,7 @@ static void rtl_slow_event_work(struct rtl8169_private *tp)
68386815
rtl8169_pcierr_interrupt(dev);
68396816

68406817
if (status & LinkChg)
6841-
rtl8169_check_link_status(dev, tp);
6818+
phy_mac_interrupt(dev->phydev);
68426819

68436820
rtl_irq_enable_all(tp);
68446821
}
@@ -6920,10 +6897,52 @@ static void rtl8169_rx_missed(struct net_device *dev)
69206897
RTL_W32(tp, RxMissed, 0);
69216898
}
69226899

6900+
static void r8169_phylink_handler(struct net_device *ndev)
6901+
{
6902+
struct rtl8169_private *tp = netdev_priv(ndev);
6903+
6904+
if (netif_carrier_ok(ndev)) {
6905+
rtl_link_chg_patch(tp);
6906+
pm_request_resume(&tp->pci_dev->dev);
6907+
} else {
6908+
pm_runtime_idle(&tp->pci_dev->dev);
6909+
}
6910+
6911+
if (net_ratelimit())
6912+
phy_print_status(ndev->phydev);
6913+
}
6914+
6915+
static int r8169_phy_connect(struct rtl8169_private *tp)
6916+
{
6917+
struct phy_device *phydev = mdiobus_get_phy(tp->mii_bus, 0);
6918+
phy_interface_t phy_mode;
6919+
int ret;
6920+
6921+
phy_mode = tp->mii.supports_gmii ? PHY_INTERFACE_MODE_GMII :
6922+
PHY_INTERFACE_MODE_MII;
6923+
6924+
ret = phy_connect_direct(tp->dev, phydev, r8169_phylink_handler,
6925+
phy_mode);
6926+
if (ret)
6927+
return ret;
6928+
6929+
if (!tp->mii.supports_gmii)
6930+
phy_set_max_speed(phydev, SPEED_100);
6931+
6932+
/* Ensure to advertise everything, incl. pause */
6933+
phydev->advertising = phydev->supported;
6934+
6935+
phy_attached_info(phydev);
6936+
6937+
return 0;
6938+
}
6939+
69236940
static void rtl8169_down(struct net_device *dev)
69246941
{
69256942
struct rtl8169_private *tp = netdev_priv(dev);
69266943

6944+
phy_stop(dev->phydev);
6945+
69276946
napi_disable(&tp->napi);
69286947
netif_stop_queue(dev);
69296948

@@ -6963,6 +6982,8 @@ static int rtl8169_close(struct net_device *dev)
69636982

69646983
cancel_work_sync(&tp->wk.work);
69656984

6985+
phy_disconnect(dev->phydev);
6986+
69666987
pci_free_irq(pdev, 0, tp);
69676988

69686989
dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
@@ -7023,6 +7044,10 @@ static int rtl_open(struct net_device *dev)
70237044
if (retval < 0)
70247045
goto err_release_fw_2;
70257046

7047+
retval = r8169_phy_connect(tp);
7048+
if (retval)
7049+
goto err_free_irq;
7050+
70267051
rtl_lock_work(tp);
70277052

70287053
set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
@@ -7038,16 +7063,17 @@ static int rtl_open(struct net_device *dev)
70387063
if (!rtl8169_init_counter_offsets(tp))
70397064
netif_warn(tp, hw, dev, "counter reset/update failed\n");
70407065

7066+
phy_start(dev->phydev);
70417067
netif_start_queue(dev);
70427068

70437069
rtl_unlock_work(tp);
70447070

70457071
pm_runtime_put_sync(&pdev->dev);
7046-
7047-
rtl8169_check_link_status(dev, tp);
70487072
out:
70497073
return retval;
70507074

7075+
err_free_irq:
7076+
pci_free_irq(pdev, 0, tp);
70517077
err_release_fw_2:
70527078
rtl_release_firmware(tp);
70537079
rtl8169_rx_clear(tp);
@@ -7126,6 +7152,7 @@ static void rtl8169_net_suspend(struct net_device *dev)
71267152
if (!netif_running(dev))
71277153
return;
71287154

7155+
phy_stop(dev->phydev);
71297156
netif_device_detach(dev);
71307157
netif_stop_queue(dev);
71317158

@@ -7158,6 +7185,8 @@ static void __rtl8169_resume(struct net_device *dev)
71587185
rtl_pll_power_up(tp);
71597186
rtl8169_init_phy(dev, tp);
71607187

7188+
phy_start(tp->dev->phydev);
7189+
71617190
rtl_lock_work(tp);
71627191
napi_enable(&tp->napi);
71637192
set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
@@ -7303,6 +7332,7 @@ static void rtl_remove_one(struct pci_dev *pdev)
73037332
netif_napi_del(&tp->napi);
73047333

73057334
unregister_netdev(dev);
7335+
mdiobus_unregister(tp->mii_bus);
73067336

73077337
rtl_release_firmware(tp);
73087338

@@ -7388,6 +7418,65 @@ DECLARE_RTL_COND(rtl_rxtx_empty_cond)
73887418
return (RTL_R8(tp, MCU) & RXTX_EMPTY) == RXTX_EMPTY;
73897419
}
73907420

7421+
static int r8169_mdio_read_reg(struct mii_bus *mii_bus, int phyaddr, int phyreg)
7422+
{
7423+
struct rtl8169_private *tp = mii_bus->priv;
7424+
7425+
if (phyaddr > 0)
7426+
return -ENODEV;
7427+
7428+
return rtl_readphy(tp, phyreg);
7429+
}
7430+
7431+
static int r8169_mdio_write_reg(struct mii_bus *mii_bus, int phyaddr,
7432+
int phyreg, u16 val)
7433+
{
7434+
struct rtl8169_private *tp = mii_bus->priv;
7435+
7436+
if (phyaddr > 0)
7437+
return -ENODEV;
7438+
7439+
rtl_writephy(tp, phyreg, val);
7440+
7441+
return 0;
7442+
}
7443+
7444+
static int r8169_mdio_register(struct rtl8169_private *tp)
7445+
{
7446+
struct pci_dev *pdev = tp->pci_dev;
7447+
struct phy_device *phydev;
7448+
struct mii_bus *new_bus;
7449+
int ret;
7450+
7451+
new_bus = devm_mdiobus_alloc(&pdev->dev);
7452+
if (!new_bus)
7453+
return -ENOMEM;
7454+
7455+
new_bus->name = "r8169";
7456+
new_bus->priv = tp;
7457+
new_bus->parent = &pdev->dev;
7458+
new_bus->irq[0] = PHY_IGNORE_INTERRUPT;
7459+
snprintf(new_bus->id, MII_BUS_ID_SIZE, "r8169-%x",
7460+
PCI_DEVID(pdev->bus->number, pdev->devfn));
7461+
7462+
new_bus->read = r8169_mdio_read_reg;
7463+
new_bus->write = r8169_mdio_write_reg;
7464+
7465+
ret = mdiobus_register(new_bus);
7466+
if (ret)
7467+
return ret;
7468+
7469+
phydev = mdiobus_get_phy(new_bus, 0);
7470+
if (!phydev) {
7471+
mdiobus_unregister(new_bus);
7472+
return -ENODEV;
7473+
}
7474+
7475+
tp->mii_bus = new_bus;
7476+
7477+
return 0;
7478+
}
7479+
73917480
static void rtl_hw_init_8168g(struct rtl8169_private *tp)
73927481
{
73937482
u32 data;
@@ -7644,10 +7733,14 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
76447733

76457734
pci_set_drvdata(pdev, dev);
76467735

7647-
rc = register_netdev(dev);
7648-
if (rc < 0)
7736+
rc = r8169_mdio_register(tp);
7737+
if (rc)
76497738
return rc;
76507739

7740+
rc = register_netdev(dev);
7741+
if (rc)
7742+
goto err_mdio_unregister;
7743+
76517744
netif_info(tp, probe, dev, "%s, %pM, XID %08x, IRQ %d\n",
76527745
rtl_chip_infos[chipset].name, dev->dev_addr,
76537746
(u32)(RTL_R32(tp, TxConfig) & 0xfcf0f8ff),
@@ -7662,12 +7755,14 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
76627755
if (r8168_check_dash(tp))
76637756
rtl8168_driver_start(tp);
76647757

7665-
netif_carrier_off(dev);
7666-
76677758
if (pci_dev_run_wake(pdev))
76687759
pm_runtime_put_sync(&pdev->dev);
76697760

76707761
return 0;
7762+
7763+
err_mdio_unregister:
7764+
mdiobus_unregister(tp->mii_bus);
7765+
return rc;
76717766
}
76727767

76737768
static struct pci_driver rtl8169_pci_driver = {

0 commit comments

Comments
 (0)