Skip to contentSkip to conversation

net: phy: xilinx: add Xilinx PHY driver

PATCH Superseded Robert Hancock 4 participants 8 messages May 31, 2019 – Jun 1, 2019

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: phy: xilinx: add Xilinx PHY driver

This adds a driver for the PHY device implemented in the Xilinx PCS/PMA Core logic. Aside from being a generic gigabit PHY, it includes an important register setting to disable the PHY isolation bit, which is required for the PHY to operate in 1000BaseX mode.

This version is a simplified version of the GPL 2+ version from the Xilinx kernel tree.

---
 drivers/net/phy/Kconfig      |  6 +++++
 drivers/net/phy/Makefile     |  1 +
 drivers/net/phy/xilinx_phy.c | 60 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+)
 create mode 100644 drivers/net/phy/xilinx_phy.c
drivers/net/phy/Kconfig +6 -0
index db5645b..101c794 100644
@@ -462,6 +462,12 @@ config VITESSE_PHY
 	---help---
 	  Currently supports the vsc8244
 
+config XILINX_PHY
+	tristate "Drivers for Xilinx PHYs"
+	help
+	  This module provides a driver for the PHY implemented in the
+	  Xilinx PCS/PMA Core.
+
 config XILINX_GMII2RGMII
 	tristate "Xilinx GMII2RGMII converter driver"
 	---help---
drivers/net/phy/Makefile +1 -0
index bac339e..f71359d 100644
@@ -92,3 +92,4 @@ obj-$(CONFIG_STE10XP)		+= ste10Xp.o
 obj-$(CONFIG_TERANETICS_PHY)	+= teranetics.o
 obj-$(CONFIG_VITESSE_PHY)	+= vitesse.o
 obj-$(CONFIG_XILINX_GMII2RGMII) += xilinx_gmii2rgmii.o
+obj-$(CONFIG_XILINX_PHY)	+= xilinx_phy.o
drivers/net/phy/xilinx_phy.c new file +60 -0

60 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/1 net: phy: Ensure scheduled work is cancelled during removal

It is possible that scheduled work started by the PHY driver is still outstanding when phy_device_remove is called if the PHY was initially started but never connected, and therefore phy_disconnect is never called. phy_stop does not guarantee that the scheduled work is stopped because it is called under rtnl_lock. This can cause an oops due to use-after-free if the delayed work fires after freeing the PHY device.

Ensure that the state_queue work is cancelled in both phy_device_remove and phy_remove paths.

---
 drivers/net/phy/phy_device.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/phy/phy_device.c +2 -0
index 2c879ba..1c90b33 100644
@@ -877,6 +877,8 @@ int phy_device_register(struct phy_device *phydev)
  */
 void phy_device_remove(struct phy_device *phydev)
 {
+	cancel_delayed_work_sync(&phydev->state_queue);
+
 	device_del(&phydev->mdio.dev);
 
 	/* Assert the reset signal */
Signature · 1 line
1.8.3.1
maintainer
#2 lore raw

Re: [PATCH net-next] net: phy: Ensure scheduled work is cancelled during removal

Robert

Please make sure you Cc: PHY patches to the PHY maintainers.

Heiner, this one is for you.

Andrew

Quoted 31 lines · Robert Hancock

It is possible that scheduled work started by the PHY driver is still outstanding when phy_device_remove is called if the PHY was initially started but never connected, and therefore phy_disconnect is never called. phy_stop does not guarantee that the scheduled work is stopped because it is called under rtnl_lock. This can cause an oops due to use-after-free if the delayed work fires after freeing the PHY device.

Ensure that the state_queue work is cancelled in both phy_device_remove and phy_remove paths.

---
 drivers/net/phy/phy_device.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/phy/phy_device.c +2 -0
index 2c879ba..1c90b33 100644
@@ -877,6 +877,8 @@ int phy_device_register(struct phy_device *phydev)
  */
 void phy_device_remove(struct phy_device *phydev)
 {
+	cancel_delayed_work_sync(&phydev->state_queue);
+
 	device_del(&phydev->mdio.dev);
 
 	/* Assert the reset signal */
Signature · 1 line
1.8.3.1
maintainer
#3 lore raw

Re: [PATCH net-next] net: phy: Ensure scheduled work is cancelled during removal

Quoted 16 lines · Andrew Lunn

Robert

Please make sure you Cc: PHY patches to the PHY maintainers.

Heiner, this one is for you.

Andrew

Quoted 7 lines · Robert Hancock

It is possible that scheduled work started by the PHY driver is still outstanding when phy_device_remove is called if the PHY was initially started but never connected, and therefore phy_disconnect is never called. phy_stop does not guarantee that the scheduled work is stopped because it is called under rtnl_lock. This can cause an oops due to use-after-free if the delayed work fires after freeing the PHY device.

The patch itself at least shouldn't do any harm. However the justification isn't fully convincing yet.
PHY drivers don't start any scheduled work. This queue is used by the phylib state machine. phy_stop usually isn't called under rtnl_lock, and it calls phy_stop_machine that cancels pending work.
Did you experience such an oops? Can you provide a call chain where your described scenario could happen?

Quoted 25 lines
Quoted 24 lines

Ensure that the state_queue work is cancelled in both phy_device_remove and phy_remove paths.

---
 drivers/net/phy/phy_device.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/phy/phy_device.c +2 -0
index 2c879ba..1c90b33 100644
@@ -877,6 +877,8 @@ int phy_device_register(struct phy_device *phydev)
  */
 void phy_device_remove(struct phy_device *phydev)
 {
+	cancel_delayed_work_sync(&phydev->state_queue);
+
 	device_del(&phydev->mdio.dev);
 
 	/* Assert the reset signal */
Signature · 1 line
1.8.3.1
#4 lore raw

Re: [PATCH net-next] net: phy: Ensure scheduled work is cancelled during removal

Quoted 8 lines · Andrew Lunn
Quoted 7 lines
Quoted 7 lines

It is possible that scheduled work started by the PHY driver is still outstanding when phy_device_remove is called if the PHY was initially started but never connected, and therefore phy_disconnect is never called. phy_stop does not guarantee that the scheduled work is stopped because it is called under rtnl_lock. This can cause an oops due to use-after-free if the delayed work fires after freeing the PHY device.

Quoted 7 lines

The patch itself at least shouldn't do any harm. However the justification isn't fully convincing yet.
PHY drivers don't start any scheduled work. This queue is used by the phylib state machine. phy_stop usually isn't called under rtnl_lock, and it calls phy_stop_machine that cancels pending work.
Did you experience such an oops? Can you provide a call chain where your described scenario could happen?

Upon further investigation, it appears that this change is no longer needed in the mainline. Previously (such as in 4.19 kernels as we are using), phy_stop did not call phy_stop_machine, only phy_disconnect did, so if the phy was started but never connected and disconnected before stopping it, the delayed work was not stopped. That sequence didn't occur often, but could happen in some failure cases which I believe was what I ran into during development when this change was originally made.

It looks like this was fixed in commit cbfd12b3e8c3542e8142aa041714ed614d3f67b0 "net: phy: ensure phylib state machine is stopped after calling phy_stop". So my patch can be dropped - but maybe that patch should be added to stable?

maintainer
#5 lore raw

Re: [PATCH net-next] net: phy: Ensure scheduled work is cancelled during removal

Quoted 16 lines · On Fri, May 31, 2019 at 09:22:16PM -0600, [email protected] wrote:
Quoted 8 lines · Andrew Lunn
Quoted 7 lines
Quoted 7 lines

It is possible that scheduled work started by the PHY driver is still outstanding when phy_device_remove is called if the PHY was initially started but never connected, and therefore phy_disconnect is never called. phy_stop does not guarantee that the scheduled work is stopped because it is called under rtnl_lock. This can cause an oops due to use-after-free if the delayed work fires after freeing the PHY device.

Quoted 7 lines

The patch itself at least shouldn't do any harm. However the justification isn't fully convincing yet.
PHY drivers don't start any scheduled work. This queue is used by the phylib state machine. phy_stop usually isn't called under rtnl_lock, and it calls phy_stop_machine that cancels pending work.
Did you experience such an oops? Can you provide a call chain where your described scenario could happen?

Quoted 3 lines

Upon further investigation, it appears that this change is no longer needed in the mainline. Previously (such as in 4.19 kernels as we are using),

Hi Robert

Please do all your testing on net-next. 4.19 is dead, in terms of development. There is no point in developing and testing on it patches intended for mainline.

Andrew

maintainer
#6 lore raw
In reply to Robert Hancock
Quoted 5 lines · Robert Hancock

This adds a driver for the PHY device implemented in the Xilinx PCS/PMA Core logic. Aside from being a generic gigabit PHY, it includes an important register setting to disable the PHY isolation bit, which is required for the PHY to operate in 1000BaseX mode.

Quoted 8 lines

This version is a simplified version of the GPL 2+ version from the Xilinx kernel tree.

---
 drivers/net/phy/Kconfig      |  6 +++++
 drivers/net/phy/Makefile     |  1 +
 drivers/net/phy/xilinx_phy.c | 60 ++++++++++++++++++++++++++++++++++++++++++++

Hi Robert

Please drop the _phy . No other driver does that.

Quoted 25 lines
 3 files changed, 67 insertions(+)
 create mode 100644 drivers/net/phy/xilinx_phy.c
drivers/net/phy/Kconfig +6 -0
index db5645b..101c794 100644
@@ -462,6 +462,12 @@ config VITESSE_PHY
 	---help---
 	  Currently supports the vsc8244
 
+config XILINX_PHY
+	tristate "Drivers for Xilinx PHYs"
+	help
+	  This module provides a driver for the PHY implemented in the
+	  Xilinx PCS/PMA Core.
+
 config XILINX_GMII2RGMII
 	tristate "Xilinx GMII2RGMII converter driver"
 	---help---

index bac339e..f71359d 100644

drivers/net/phy/Makefile obj-$(CONFIG_STE10XP) += ste10Xp.o · line 95

Quoted 4 lines
drivers/net/phy/Makefile +1 -0
@@ -92,3 +92,4 @@ obj-$(CONFIG_STE10XP)		+= ste10Xp.o
 obj-$(CONFIG_TERANETICS_PHY)	+= teranetics.o
 obj-$(CONFIG_VITESSE_PHY)	+= vitesse.o
 obj-$(CONFIG_XILINX_GMII2RGMII) += xilinx_gmii2rgmii.o
+obj-$(CONFIG_XILINX_PHY)	+= xilinx_phy.o

In kconfig you added it before CONFIG_XILINX_GMII2RGMII. Here you have it afterwards. Please be consistent.

Quoted 22 lines
drivers/net/phy/xilinx_phy.c new file +16 -0
new file mode 100644
index 0000000..2d468c7
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0+
+/* Xilinx PCS/PMA Core phy driver
+ *
+ * Copyright (C) 2019 SED Systems, a division of Calian Ltd.
+ *
+ * Based upon Xilinx version of this driver:
+ * Copyright (C) 2015 Xilinx, Inc.
+ *
+ * Description:
+ * This driver is developed for PCS/PMA Core.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mii.h>
+#include <linux/phy.h>

drivers/net/phy/xilinx_phy.c line 26

Quoted 10 lines
drivers/net/phy/xilinx_phy.c new file +9 -0
+
+/* Mask used for ID comparisons */
+#define XILINX_PHY_ID_MASK		0xfffffff0
+
+/* Known PHY IDs */
+#define XILINX_PHY_ID			0x01740c00
+
+#define XPCSPMA_PHY_CTRL_ISOLATE_DISABLE 0xFBFF
 
+static int xilinxphy_config_init(struct phy_device *phydev)

Please drop the phy here as well.

drivers/net/phy/xilinx_phy.c line 32

Quoted 6 lines
drivers/net/phy/xilinx_phy.c new file +6 -0
+{
+	int temp;
+
+	temp = phy_read(phydev, MII_BMCR);
+	temp &= XPCSPMA_PHY_CTRL_ISOLATE_DISABLE;
+	phy_write(phydev, MII_BMCR, temp);
    phy_modify(phydev, MII_BMCR, BMCR_ISOLATE);

drivers/net/phy/xilinx_phy.c line 42

Quoted 10 lines
drivers/net/phy/xilinx_phy.c new file +10 -0
+
+	return 0;
+}
+
+static struct phy_driver xilinx_drivers[] = {
+{
+	.phy_id		= XILINX_PHY_ID,
+	.phy_id_mask	= XILINX_PHY_ID_MASK,
+	.name		= "Xilinx PCS/PMA PHY",
+	.features	= PHY_GBIT_FEATURES,

Assuming the PHY follows C22 standards, that is not needed. The PHY itself tells you what it is capable of.

Andrew

#7 lore raw
In reply to Robert Hancock
Quoted 70 lines · Robert Hancock

This adds a driver for the PHY device implemented in the Xilinx PCS/PMA Core logic. Aside from being a generic gigabit PHY, it includes an important register setting to disable the PHY isolation bit, which is required for the PHY to operate in 1000BaseX mode.

This version is a simplified version of the GPL 2+ version from the Xilinx kernel tree.

---
 drivers/net/phy/Kconfig      |  6 +++++
 drivers/net/phy/Makefile     |  1 +
 drivers/net/phy/xilinx_phy.c | 60 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+)
 create mode 100644 drivers/net/phy/xilinx_phy.c
drivers/net/phy/Kconfig +6 -0
index db5645b..101c794 100644
@@ -462,6 +462,12 @@ config VITESSE_PHY
 	---help---
 	  Currently supports the vsc8244
 
+config XILINX_PHY
+	tristate "Drivers for Xilinx PHYs"
+	help
+	  This module provides a driver for the PHY implemented in the
+	  Xilinx PCS/PMA Core.
+
 config XILINX_GMII2RGMII
 	tristate "Xilinx GMII2RGMII converter driver"
 	---help---
drivers/net/phy/Makefile +1 -0
index bac339e..f71359d 100644
@@ -92,3 +92,4 @@ obj-$(CONFIG_STE10XP)		+= ste10Xp.o
 obj-$(CONFIG_TERANETICS_PHY)	+= teranetics.o
 obj-$(CONFIG_VITESSE_PHY)	+= vitesse.o
 obj-$(CONFIG_XILINX_GMII2RGMII) += xilinx_gmii2rgmii.o
+obj-$(CONFIG_XILINX_PHY)	+= xilinx_phy.o
drivers/net/phy/xilinx_phy.c new file +22 -0
new file mode 100644
index 0000000..2d468c7
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0+
+/* Xilinx PCS/PMA Core phy driver
+ *
+ * Copyright (C) 2019 SED Systems, a division of Calian Ltd.
+ *
+ * Based upon Xilinx version of this driver:
+ * Copyright (C) 2015 Xilinx, Inc.
+ *
+ * Description:
+ * This driver is developed for PCS/PMA Core.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mii.h>
+#include <linux/phy.h>
+
+/* Mask used for ID comparisons */
+#define XILINX_PHY_ID_MASK		0xfffffff0
+
+/* Known PHY IDs */
+#define XILINX_PHY_ID			0x01740c00

drivers/net/phy/xilinx_phy.c line 32

Quoted 10 lines
drivers/net/phy/xilinx_phy.c new file +10 -0
+
+#define XPCSPMA_PHY_CTRL_ISOLATE_DISABLE 0xFBFF
+
+static int xilinxphy_config_init(struct phy_device *phydev)
+{
+	int temp;
+
+	temp = phy_read(phydev, MII_BMCR);
+	temp &= XPCSPMA_PHY_CTRL_ISOLATE_DISABLE;
+	phy_write(phydev, MII_BMCR, temp);

The PHY library takes care of clearing the BMCR_ISOLATE bit, is not that working for you?

Signature · 1 line
Florian