[net,v3,1/3] net: phylink: add phylink_expects_phy() method

Message ID 20230324081656.2969663-2-michael.wei.hong.sit@intel.com
State New
Headers
Series Fix PHY handle no longer parsing |

Commit Message

Sit, Michael Wei Hong March 24, 2023, 8:16 a.m. UTC
  Provide phylink_expects_phy() to allow MAC drivers to check if it
is expecting a PHY to attach to. Since fixed-linked setups do not
need to attach to a PHY.

Provides a boolean value as to if the MAC should expect a PHY.
returns true if a PHY is expected.

Signed-off-by: Michael Sit Wei Hong <michael.wei.hong.sit@intel.com>
---
 drivers/net/phy/phylink.c | 13 +++++++++++++
 include/linux/phylink.h   |  1 +
 2 files changed, 14 insertions(+)
  

Comments

Jakub Kicinski March 29, 2023, 1:57 a.m. UTC | #1
On Fri, 24 Mar 2023 16:16:54 +0800 Michael Sit Wei Hong wrote:
> Provide phylink_expects_phy() to allow MAC drivers to check if it
> is expecting a PHY to attach to. Since fixed-linked setups do not
> need to attach to a PHY.
> 
> Provides a boolean value as to if the MAC should expect a PHY.
> returns true if a PHY is expected.
> 
> Signed-off-by: Michael Sit Wei Hong <michael.wei.hong.sit@intel.com>

Russell, looks good?

> diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
> index 1a2f074685fa..5c2bd1370993 100644
> --- a/drivers/net/phy/phylink.c
> +++ b/drivers/net/phy/phylink.c
> @@ -1586,6 +1586,19 @@ void phylink_destroy(struct phylink *pl)
>  }
>  EXPORT_SYMBOL_GPL(phylink_destroy);
>  
> +/**
> + * phylink_expects_phy() - Determine if phylink expects a phy to be attached
> + * @pl: a pointer to a &struct phylink returned from phylink_create()
> + *
> + * Fixed-link mode does not need a PHY, returns a boolean value to check if
> + * phylink will be expecting a PHY to attach.
> + */
> +bool phylink_expects_phy(struct phylink *pl)
> +{
> +	return pl->cfg_link_an_mode != MLO_AN_FIXED;
> +}
> +EXPORT_SYMBOL_GPL(phylink_expects_phy);
> +
>  static void phylink_phy_change(struct phy_device *phydev, bool up)
>  {
>  	struct phylink *pl = phydev->phylink;
> diff --git a/include/linux/phylink.h b/include/linux/phylink.h
> index c492c26202b5..637698ed5cb6 100644
> --- a/include/linux/phylink.h
> +++ b/include/linux/phylink.h
> @@ -574,6 +574,7 @@ struct phylink *phylink_create(struct phylink_config *, struct fwnode_handle *,
>  			       phy_interface_t iface,
>  			       const struct phylink_mac_ops *mac_ops);
>  void phylink_destroy(struct phylink *);
> +bool phylink_expects_phy(struct phylink *pl);
>  
>  int phylink_connect_phy(struct phylink *, struct phy_device *);
>  int phylink_of_phy_connect(struct phylink *, struct device_node *, u32 flags);
  
Russell King (Oracle) March 29, 2023, 9:01 a.m. UTC | #2
On Tue, Mar 28, 2023 at 06:57:20PM -0700, Jakub Kicinski wrote:
> On Fri, 24 Mar 2023 16:16:54 +0800 Michael Sit Wei Hong wrote:
> > Provide phylink_expects_phy() to allow MAC drivers to check if it
> > is expecting a PHY to attach to. Since fixed-linked setups do not
> > need to attach to a PHY.
> > 
> > Provides a boolean value as to if the MAC should expect a PHY.
> > returns true if a PHY is expected.
> > 
> > Signed-off-by: Michael Sit Wei Hong <michael.wei.hong.sit@intel.com>
> 
> Russell, looks good?

Not really, given that phylink_attach_phy() will refuse to attach a PHY
when:

        if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
                    (pl->cfg_link_an_mode == MLO_AN_INBAND &&
                     phy_interface_mode_is_8023z(interface) && !pl->sfp_bus)))
                return -EINVAL;

So, if we introduce a helper named "phylink_expects_phy" that returns
true when cfg_link_an_mode == MLO_AN_INBAND and the interface mode
is e.g. 1000base-X, but then someone tries to attach a PHY, the kernel
spits out a warning, backtrace, and a return value of -EINVAL, things
are going to look really rather stupid.
  
Sit, Michael Wei Hong March 29, 2023, 9:34 a.m. UTC | #3
> -----Original Message-----
> From: Russell King <linux@armlinux.org.uk>
> Sent: Wednesday, March 29, 2023 5:01 PM
> To: Jakub Kicinski <kuba@kernel.org>
> Cc: Sit, Michael Wei Hong <michael.wei.hong.sit@intel.com>;
> Giuseppe Cavallaro <peppe.cavallaro@st.com>; Alexandre Torgue
> <alexandre.torgue@foss.st.com>; Jose Abreu
> <joabreu@synopsys.com>; David S . Miller <davem@davemloft.net>;
> Eric Dumazet <edumazet@google.com>; Paolo Abeni
> <pabeni@redhat.com>; Maxime Coquelin
> <mcoquelin.stm32@gmail.com>; Ong, Boon Leong
> <boon.leong.ong@intel.com>; netdev@vger.kernel.org; linux-
> stm32@st-md-mailman.stormreply.com; linux-arm-
> kernel@lists.infradead.org; linux-kernel@vger.kernel.org; Looi,
> Hong Aun <hong.aun.looi@intel.com>; Voon, Weifeng
> <weifeng.voon@intel.com>; Lai, Peter Jun Ann
> <peter.jun.ann.lai@intel.com>
> Subject: Re: [PATCH net v3 1/3] net: phylink: add
> phylink_expects_phy() method
> 
> On Tue, Mar 28, 2023 at 06:57:20PM -0700, Jakub Kicinski wrote:
> > On Fri, 24 Mar 2023 16:16:54 +0800 Michael Sit Wei Hong wrote:
> > > Provide phylink_expects_phy() to allow MAC drivers to check if it
> is
> > > expecting a PHY to attach to. Since fixed-linked setups do not
> need
> > > to attach to a PHY.
> > >
> > > Provides a boolean value as to if the MAC should expect a PHY.
> > > returns true if a PHY is expected.
> > >
> > > Signed-off-by: Michael Sit Wei Hong
> <michael.wei.hong.sit@intel.com>
> >
> > Russell, looks good?
> 
> Not really, given that phylink_attach_phy() will refuse to attach a
> PHY
> when:
> 
>         if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
>                     (pl->cfg_link_an_mode == MLO_AN_INBAND &&
>                      phy_interface_mode_is_8023z(interface) && !pl-
> >sfp_bus)))
>                 return -EINVAL;
> 
> So, if we introduce a helper named "phylink_expects_phy" that
> returns true when cfg_link_an_mode == MLO_AN_INBAND and the
> interface mode is e.g. 1000base-X, but then someone tries to attach
> a PHY, the kernel spits out a warning, backtrace, and a return value
> of -EINVAL, things are going to look really rather stupid.
> 
Should we check for these 3 conditions as well then?
(pl->cfg_link_an_mode == MLO_AN_INBAND &&
phy_interface_mode_is_8023z(interface) && !pl->sfp_bus)
to determine if phylink expects a phy.

> --
> RMK's Patch system:
> https://www.armlinux.org.uk/developer/patches/
> FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!
  
Russell King (Oracle) March 29, 2023, 9:48 a.m. UTC | #4
On Wed, Mar 29, 2023 at 09:34:05AM +0000, Sit, Michael Wei Hong wrote:
> 
> 
> > -----Original Message-----
> > From: Russell King <linux@armlinux.org.uk>
> > Sent: Wednesday, March 29, 2023 5:01 PM
> > To: Jakub Kicinski <kuba@kernel.org>
> > Cc: Sit, Michael Wei Hong <michael.wei.hong.sit@intel.com>;
> > Giuseppe Cavallaro <peppe.cavallaro@st.com>; Alexandre Torgue
> > <alexandre.torgue@foss.st.com>; Jose Abreu
> > <joabreu@synopsys.com>; David S . Miller <davem@davemloft.net>;
> > Eric Dumazet <edumazet@google.com>; Paolo Abeni
> > <pabeni@redhat.com>; Maxime Coquelin
> > <mcoquelin.stm32@gmail.com>; Ong, Boon Leong
> > <boon.leong.ong@intel.com>; netdev@vger.kernel.org; linux-
> > stm32@st-md-mailman.stormreply.com; linux-arm-
> > kernel@lists.infradead.org; linux-kernel@vger.kernel.org; Looi,
> > Hong Aun <hong.aun.looi@intel.com>; Voon, Weifeng
> > <weifeng.voon@intel.com>; Lai, Peter Jun Ann
> > <peter.jun.ann.lai@intel.com>
> > Subject: Re: [PATCH net v3 1/3] net: phylink: add
> > phylink_expects_phy() method
> > 
> > On Tue, Mar 28, 2023 at 06:57:20PM -0700, Jakub Kicinski wrote:
> > > On Fri, 24 Mar 2023 16:16:54 +0800 Michael Sit Wei Hong wrote:
> > > > Provide phylink_expects_phy() to allow MAC drivers to check if it
> > is
> > > > expecting a PHY to attach to. Since fixed-linked setups do not
> > need
> > > > to attach to a PHY.
> > > >
> > > > Provides a boolean value as to if the MAC should expect a PHY.
> > > > returns true if a PHY is expected.
> > > >
> > > > Signed-off-by: Michael Sit Wei Hong
> > <michael.wei.hong.sit@intel.com>
> > >
> > > Russell, looks good?
> > 
> > Not really, given that phylink_attach_phy() will refuse to attach a
> > PHY
> > when:
> > 
> >         if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
> >                     (pl->cfg_link_an_mode == MLO_AN_INBAND &&
> >                      phy_interface_mode_is_8023z(interface) && !pl-
> > >sfp_bus)))
> >                 return -EINVAL;
> > 
> > So, if we introduce a helper named "phylink_expects_phy" that
> > returns true when cfg_link_an_mode == MLO_AN_INBAND and the
> > interface mode is e.g. 1000base-X, but then someone tries to attach
> > a PHY, the kernel spits out a warning, backtrace, and a return value
> > of -EINVAL, things are going to look really rather stupid.
> > 
> Should we check for these 3 conditions as well then?
> (pl->cfg_link_an_mode == MLO_AN_INBAND &&
> phy_interface_mode_is_8023z(interface) && !pl->sfp_bus)
> to determine if phylink expects a phy.

If there's a sfp bus, then we don't expect a PHY from the MAC driver
(as there can only be one PHY attached), and as phylink_expects_phy()
is for the MAC driver to use, we should be returning false if
pl->sfp_bus != NULL.

	pl->cfg_link_an_mode == MLO_AN_FIXED ||
	(pl->cfg_link_an_mode == MLO_AN_INBAND &&
	 phy_interface_mode_is_8023z(interface))

Is true when we're in fixed-link mode, or if we're in in-band mode
and using 1000base-X or 25000base-X. These are the conditions that
we don't expect the MAC driver to give us a PHY.

To put that in positive logic, we expect a PHY from the MAC when
we're in PHY mode, or when we're in in-band mode and using SGMII,
QSGMII, USXGMII, RGMII, etc.

The reason for the extra "&& !pl->sfp_bus" in phylink_attach_phy()
is to allow SFPs to connect to the MAC using inband mode with
1000base-X and 2500base-X interface modes. These are not for the
MAC-side of things though.
  

Patch

diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c
index 1a2f074685fa..5c2bd1370993 100644
--- a/drivers/net/phy/phylink.c
+++ b/drivers/net/phy/phylink.c
@@ -1586,6 +1586,19 @@  void phylink_destroy(struct phylink *pl)
 }
 EXPORT_SYMBOL_GPL(phylink_destroy);
 
+/**
+ * phylink_expects_phy() - Determine if phylink expects a phy to be attached
+ * @pl: a pointer to a &struct phylink returned from phylink_create()
+ *
+ * Fixed-link mode does not need a PHY, returns a boolean value to check if
+ * phylink will be expecting a PHY to attach.
+ */
+bool phylink_expects_phy(struct phylink *pl)
+{
+	return pl->cfg_link_an_mode != MLO_AN_FIXED;
+}
+EXPORT_SYMBOL_GPL(phylink_expects_phy);
+
 static void phylink_phy_change(struct phy_device *phydev, bool up)
 {
 	struct phylink *pl = phydev->phylink;
diff --git a/include/linux/phylink.h b/include/linux/phylink.h
index c492c26202b5..637698ed5cb6 100644
--- a/include/linux/phylink.h
+++ b/include/linux/phylink.h
@@ -574,6 +574,7 @@  struct phylink *phylink_create(struct phylink_config *, struct fwnode_handle *,
 			       phy_interface_t iface,
 			       const struct phylink_mac_ops *mac_ops);
 void phylink_destroy(struct phylink *);
+bool phylink_expects_phy(struct phylink *pl);
 
 int phylink_connect_phy(struct phylink *, struct phy_device *);
 int phylink_of_phy_connect(struct phylink *, struct device_node *, u32 flags);