From: Florian Fainelli <f.fainelli@gmail.com>
To: netdev@vger.kernel.org
Cc: Florian Fainelli <f.fainelli@gmail.com>,
Andrew Lunn <andrew@lunn.ch>,
Heiner Kallweit <hkallweit1@gmail.com>,
Russell King <linux@armlinux.org.uk>,
"David S. Miller" <davem@davemloft.net>,
Jakub Kicinski <kuba@kernel.org>,
Michal Kubecek <mkubecek@suse.cz>,
linux-kernel@vger.kernel.org (open list)
Subject: [PATCH net-next 3/4] net: phy: Automatically set-up cable test netdev_ops
Date: Wed, 1 Jul 2020 21:29:41 -0700 [thread overview]
Message-ID: <20200702042942.76674-4-f.fainelli@gmail.com> (raw)
In-Reply-To: <20200702042942.76674-1-f.fainelli@gmail.com>
Upon attach, override the net_device operations with the PHY library
cable test operations and conversely, upon detach, revert to the
original net_device operations.
This will allows us in a subsequent patch to finally decouple the
ethtool/cabletest from the PHY library hard depenencies.
Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
---
drivers/net/phy/phy_device.c | 32 ++++++++++++++++++++++++++++++++
include/linux/phy.h | 2 ++
2 files changed, 34 insertions(+)
diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index eb1068a77ce1..100d85541a06 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -1246,6 +1246,36 @@ phy_standalone_show(struct device *dev, struct device_attribute *attr,
}
static DEVICE_ATTR_RO(phy_standalone);
+static int phy_setup_netdev_ops(struct net_device *dev)
+{
+ struct phy_device *phydev = dev->phydev;
+ struct net_device_ops *ops;
+
+ ops = devm_kzalloc(&phydev->mdio.dev, sizeof(*ops), GFP_KERNEL);
+ if (!ops)
+ return -ENOMEM;
+
+ phydev->orig_ndo_ops = dev->netdev_ops;
+ if (phydev->orig_ndo_ops)
+ memcpy(ops, phydev->orig_ndo_ops, sizeof(*ops));
+
+ ops->ndo_cable_test_start = phy_start_cable_test;
+ ops->ndo_cable_test_tdr_start = phy_start_cable_test_tdr;
+
+ dev->netdev_ops = ops;
+
+ return 0;
+}
+
+static void phy_teardown_netdev_ops(struct net_device *dev)
+{
+ struct phy_device *phydev = dev->phydev;
+
+ if (phydev->orig_ndo_ops)
+ dev->netdev_ops = phydev->orig_ndo_ops;
+ phydev->orig_ndo_ops = NULL;
+}
+
/**
* phy_sfp_attach - attach the SFP bus to the PHY upstream network device
* @upstream: pointer to the phy device
@@ -1380,6 +1410,7 @@ int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
if (dev) {
phydev->attached_dev = dev;
dev->phydev = phydev;
+ phy_setup_netdev_ops(dev);
if (phydev->sfp_bus_attached)
dev->sfp_bus = phydev->sfp_bus;
@@ -1676,6 +1707,7 @@ void phy_detach(struct phy_device *phydev)
phy_suspend(phydev);
if (dev) {
+ phy_teardown_netdev_ops(dev);
phydev->attached_dev->phydev = NULL;
phydev->attached_dev = NULL;
}
diff --git a/include/linux/phy.h b/include/linux/phy.h
index 53b95c52869d..04e35afa43ae 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -544,6 +544,8 @@ struct phy_device {
/* MACsec management functions */
const struct macsec_ops *macsec_ops;
#endif
+ /* Original attached network device netdev_ops pointer */
+ const struct net_device_ops *orig_ndo_ops;
};
#define to_phy_device(d) container_of(to_mdio_device(d), \
struct phy_device, mdio)
--
2.25.1
next prev parent reply other threads:[~2020-07-02 4:29 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-07-02 4:29 [PATCH net-next 0/4] net: ethtool: Untangle PHYLIB dependency Florian Fainelli
2020-07-02 4:29 ` [PATCH net-next 1/4] net: Add cable test netdevice operations Florian Fainelli
2020-07-02 4:29 ` [PATCH net-next 2/4] net: phy: Change cable test arguments to net_device Florian Fainelli
2020-07-02 4:29 ` Florian Fainelli [this message]
2020-07-02 4:29 ` [PATCH net-next 4/4] net: ethtool: Remove PHYLIB dependency Florian Fainelli
2020-07-02 15:56 ` [PATCH net-next 0/4] net: ethtool: Untangle " Michal Kubecek
2020-07-02 16:34 ` Andrew Lunn
2020-07-02 16:35 ` Jakub Kicinski
2020-07-02 16:43 ` Florian Fainelli
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200702042942.76674-4-f.fainelli@gmail.com \
--to=f.fainelli@gmail.com \
--cc=andrew@lunn.ch \
--cc=davem@davemloft.net \
--cc=hkallweit1@gmail.com \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@armlinux.org.uk \
--cc=mkubecek@suse.cz \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).