Commit 3b87cfef authored by David S. Miller's avatar David S. Miller
Browse files

Merge branch 'r8169-mark-device-as-detached-in-PCI-D3-and-improve-locking'



Heiner Kallweit says:

====================
r8169: mark device as detached in PCI D3 and improve locking

Mark the netdevice as detached whenever parent is in PCI D3hot and not
accessible. This mainly applies to runtime-suspend state.
In addition take RTNL lock in suspend calls, this allows to remove
the driver-specific mutex and improve PM callbacks in general.
====================

Reviewed-by: default avatarJakub Kicinski <kuba@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 8878adba 288302da
Loading
Loading
Loading
Loading
+41 −140
Original line number Diff line number Diff line
@@ -611,7 +611,6 @@ struct rtl8169_private {

	struct {
		DECLARE_BITMAP(flags, RTL_FLAG_MAX);
		struct mutex mutex;
		struct work_struct work;
	} wk;

@@ -663,16 +662,6 @@ static inline struct device *tp_to_dev(struct rtl8169_private *tp)
	return &tp->pci_dev->dev;
}

static void rtl_lock_work(struct rtl8169_private *tp)
{
	mutex_lock(&tp->wk.mutex);
}

static void rtl_unlock_work(struct rtl8169_private *tp)
{
	mutex_unlock(&tp->wk.mutex);
}

static void rtl_lock_config_regs(struct rtl8169_private *tp)
{
	RTL_W8(tp, Cfg9346, Cfg9346_Lock);
@@ -1348,10 +1337,8 @@ static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
{
	struct rtl8169_private *tp = netdev_priv(dev);

	rtl_lock_work(tp);
	wol->supported = WAKE_ANY;
	wol->wolopts = tp->saved_wolopts;
	rtl_unlock_work(tp);
}

static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
@@ -1422,24 +1409,13 @@ static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
{
	struct rtl8169_private *tp = netdev_priv(dev);
	struct device *d = tp_to_dev(tp);

	if (wol->wolopts & ~WAKE_ANY)
		return -EINVAL;

	pm_runtime_get_noresume(d);

	rtl_lock_work(tp);

	tp->saved_wolopts = wol->wolopts;

	if (pm_runtime_active(d))
	__rtl8169_set_wol(tp, tp->saved_wolopts);

	rtl_unlock_work(tp);

	pm_runtime_put_noidle(d);

	return 0;
}

@@ -1502,8 +1478,6 @@ static int rtl8169_set_features(struct net_device *dev,
{
	struct rtl8169_private *tp = netdev_priv(dev);

	rtl_lock_work(tp);

	rtl_set_rx_config_features(tp, features);

	if (features & NETIF_F_RXCSUM)
@@ -1521,8 +1495,6 @@ static int rtl8169_set_features(struct net_device *dev,
	RTL_W16(tp, CPlusCmd, tp->cp_cmd);
	rtl_pci_commit(tp);

	rtl_unlock_work(tp);

	return 0;
}

@@ -1548,10 +1520,8 @@ static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
	u32 *dw = p;
	int i;

	rtl_lock_work(tp);
	for (i = 0; i < R8169_REGS_SIZE; i += 4)
		memcpy_fromio(dw++, data++, 4);
	rtl_unlock_work(tp);
}

static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
@@ -1657,18 +1627,11 @@ static void rtl8169_get_ethtool_stats(struct net_device *dev,
				      struct ethtool_stats *stats, u64 *data)
{
	struct rtl8169_private *tp = netdev_priv(dev);
	struct device *d = tp_to_dev(tp);
	struct rtl8169_counters *counters = tp->counters;

	ASSERT_RTNL();

	pm_runtime_get_noresume(d);
	struct rtl8169_counters *counters;

	if (pm_runtime_active(d))
	counters = tp->counters;
	rtl8169_update_counters(tp);

	pm_runtime_put_noidle(d);

	data[0] = le64_to_cpu(counters->tx_packets);
	data[1] = le64_to_cpu(counters->rx_packets);
	data[2] = le64_to_cpu(counters->tx_errors);
@@ -1874,8 +1837,6 @@ static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
	units = DIV_ROUND_UP(ec->rx_coalesce_usecs * 1000U, scale);
	w |= FIELD_PREP(RTL_COALESCE_RX_USECS, units);

	rtl_lock_work(tp);

	RTL_W16(tp, IntrMitigate, w);

	/* Meaning of PktCntrDisable bit changed from RTL8168e-vl */
@@ -1891,56 +1852,32 @@ static int rtl_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
	RTL_W16(tp, CPlusCmd, tp->cp_cmd);
	rtl_pci_commit(tp);

	rtl_unlock_work(tp);

	return 0;
}

static int rtl8169_get_eee(struct net_device *dev, struct ethtool_eee *data)
{
	struct rtl8169_private *tp = netdev_priv(dev);
	struct device *d = tp_to_dev(tp);
	int ret;

	if (!rtl_supports_eee(tp))
		return -EOPNOTSUPP;

	pm_runtime_get_noresume(d);

	if (!pm_runtime_active(d)) {
		ret = -EOPNOTSUPP;
	} else {
		ret = phy_ethtool_get_eee(tp->phydev, data);
	}

	pm_runtime_put_noidle(d);

	return ret;
	return phy_ethtool_get_eee(tp->phydev, data);
}

static int rtl8169_set_eee(struct net_device *dev, struct ethtool_eee *data)
{
	struct rtl8169_private *tp = netdev_priv(dev);
	struct device *d = tp_to_dev(tp);
	int ret;

	if (!rtl_supports_eee(tp))
		return -EOPNOTSUPP;

	pm_runtime_get_noresume(d);

	if (!pm_runtime_active(d)) {
		ret = -EOPNOTSUPP;
		goto out;
	}

	ret = phy_ethtool_set_eee(tp->phydev, data);

	if (!ret)
		tp->eee_adv = phy_read_mmd(dev->phydev, MDIO_MMD_AN,
					   MDIO_AN_EEE_ADV);
out:
	pm_runtime_put_noidle(d);
	return ret;
}

@@ -2198,8 +2135,6 @@ static void rtl8169_init_phy(struct rtl8169_private *tp)

static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
{
	rtl_lock_work(tp);

	rtl_unlock_config_regs(tp);

	RTL_W32(tp, MAC4, addr[4] | addr[5] << 8);
@@ -2212,27 +2147,19 @@ static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
		rtl_rar_exgmac_set(tp, addr);

	rtl_lock_config_regs(tp);

	rtl_unlock_work(tp);
}

static int rtl_set_mac_address(struct net_device *dev, void *p)
{
	struct rtl8169_private *tp = netdev_priv(dev);
	struct device *d = tp_to_dev(tp);
	int ret;

	ret = eth_mac_addr(dev, p);
	if (ret)
		return ret;

	pm_runtime_get_noresume(d);

	if (pm_runtime_active(d))
	rtl_rar_set(tp, dev->dev_addr);

	pm_runtime_put_noidle(d);

	return 0;
}

@@ -3977,10 +3904,9 @@ no_reset:

static void rtl_reset_work(struct rtl8169_private *tp)
{
	struct net_device *dev = tp->dev;
	int i;

	netif_stop_queue(dev);
	netif_stop_queue(tp->dev);

	rtl8169_cleanup(tp, false);

@@ -3989,7 +3915,6 @@ static void rtl_reset_work(struct rtl8169_private *tp)

	napi_enable(&tp->napi);
	rtl_hw_start(tp);
	netif_wake_queue(dev);
}

static void rtl8169_tx_timeout(struct net_device *dev, unsigned int txqueue)
@@ -4568,16 +4493,18 @@ static void rtl_task(struct work_struct *work)
	struct rtl8169_private *tp =
		container_of(work, struct rtl8169_private, wk.work);

	rtl_lock_work(tp);
	rtnl_lock();

	if (!netif_running(tp->dev) ||
	    !test_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags))
		goto out_unlock;

	if (test_and_clear_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags))
	if (test_and_clear_bit(RTL_FLAG_TASK_RESET_PENDING, tp->wk.flags)) {
		rtl_reset_work(tp);
		netif_wake_queue(tp->dev);
	}
out_unlock:
	rtl_unlock_work(tp);
	rtnl_unlock();
}

static int rtl8169_poll(struct napi_struct *napi, int budget)
@@ -4639,8 +4566,6 @@ static int r8169_phy_connect(struct rtl8169_private *tp)

static void rtl8169_down(struct rtl8169_private *tp)
{
	rtl_lock_work(tp);

	/* Clear all task flags */
	bitmap_zero(tp->wk.flags, RTL_FLAG_MAX);

@@ -4651,8 +4576,17 @@ static void rtl8169_down(struct rtl8169_private *tp)
	rtl8169_cleanup(tp, true);

	rtl_pll_power_down(tp);
}

static void rtl8169_up(struct rtl8169_private *tp)
{
	rtl_pll_power_up(tp);
	rtl8169_init_phy(tp);
	napi_enable(&tp->napi);
	set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
	rtl_reset_work(tp);

	rtl_unlock_work(tp);
	phy_start(tp->phydev);
}

static int rtl8169_close(struct net_device *dev)
@@ -4730,25 +4664,10 @@ static int rtl_open(struct net_device *dev)
	if (retval)
		goto err_free_irq;

	rtl_lock_work(tp);

	set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);

	napi_enable(&tp->napi);

	rtl8169_init_phy(tp);

	rtl_pll_power_up(tp);

	rtl_hw_start(tp);

	rtl8169_up(tp);
	rtl8169_init_counter_offsets(tp);

	phy_start(tp->phydev);
	netif_start_queue(dev);

	rtl_unlock_work(tp);

	pm_runtime_put_sync(&pdev->dev);
out:
	return retval;
@@ -4820,10 +4739,9 @@ rtl8169_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)

static void rtl8169_net_suspend(struct rtl8169_private *tp)
{
	if (!netif_running(tp->dev))
		return;

	netif_device_detach(tp->dev);

	if (netif_running(tp->dev))
		rtl8169_down(tp);
}

@@ -4833,35 +4751,23 @@ static int __maybe_unused rtl8169_suspend(struct device *device)
{
	struct rtl8169_private *tp = dev_get_drvdata(device);

	rtnl_lock();
	rtl8169_net_suspend(tp);
	rtnl_unlock();

	return 0;
}

static void __rtl8169_resume(struct rtl8169_private *tp)
{
	netif_device_attach(tp->dev);

	rtl_pll_power_up(tp);
	rtl8169_init_phy(tp);

	phy_start(tp->phydev);

	rtl_lock_work(tp);
	napi_enable(&tp->napi);
	set_bit(RTL_FLAG_TASK_ENABLED, tp->wk.flags);
	rtl_reset_work(tp);
	rtl_unlock_work(tp);
}

static int __maybe_unused rtl8169_resume(struct device *device)
static int rtl8169_resume(struct device *device)
{
	struct rtl8169_private *tp = dev_get_drvdata(device);

	rtl_rar_set(tp, tp->dev->dev_addr);

	if (netif_running(tp->dev))
		__rtl8169_resume(tp);
	if (tp->TxDescArray)
		rtl8169_up(tp);

	netif_device_attach(tp->dev);

	return 0;
}
@@ -4870,14 +4776,15 @@ static int rtl8169_runtime_suspend(struct device *device)
{
	struct rtl8169_private *tp = dev_get_drvdata(device);

	if (!tp->TxDescArray)
	if (!tp->TxDescArray) {
		netif_device_detach(tp->dev);
		return 0;
	}

	rtl_lock_work(tp);
	rtnl_lock();
	__rtl8169_set_wol(tp, WAKE_PHY);
	rtl_unlock_work(tp);

	rtl8169_net_suspend(tp);
	rtnl_unlock();

	return 0;
}
@@ -4886,16 +4793,9 @@ static int rtl8169_runtime_resume(struct device *device)
{
	struct rtl8169_private *tp = dev_get_drvdata(device);

	rtl_rar_set(tp, tp->dev->dev_addr);

	rtl_lock_work(tp);
	__rtl8169_set_wol(tp, tp->saved_wolopts);
	rtl_unlock_work(tp);

	if (tp->TxDescArray)
		__rtl8169_resume(tp);

	return 0;
	return rtl8169_resume(device);
}

static int rtl8169_runtime_idle(struct device *device)
@@ -4937,7 +4837,9 @@ static void rtl_shutdown(struct pci_dev *pdev)
{
	struct rtl8169_private *tp = pci_get_drvdata(pdev);

	rtnl_lock();
	rtl8169_net_suspend(tp);
	rtnl_unlock();

	/* Restore original MAC address */
	rtl_rar_set(tp, tp->dev->perm_addr);
@@ -5343,7 +5245,6 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
		return rc;
	}

	mutex_init(&tp->wk.mutex);
	INIT_WORK(&tp->wk.work, rtl_task);
	u64_stats_init(&tp->rx_stats.syncp);
	u64_stats_init(&tp->tx_stats.syncp);
+8 −2
Original line number Diff line number Diff line
@@ -143,6 +143,7 @@
#include <linux/net_namespace.h>
#include <linux/indirect_call_wrapper.h>
#include <net/devlink.h>
#include <linux/pm_runtime.h>

#include "net-sysfs.h"

@@ -1492,8 +1493,13 @@ static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack)

	ASSERT_RTNL();

	if (!netif_device_present(dev)) {
		/* may be detached because parent is runtime-suspended */
		if (dev->dev.parent)
			pm_runtime_resume(dev->dev.parent);
		if (!netif_device_present(dev))
			return -ENODEV;
	}

	/* Block netpoll from trying to do any rx path servicing.
	 * If we don't do this there is a chance ndo_poll_controller