mbox series

[net-next,v4,0/7] add generic PSE support

Message ID 20220828063021.3963761-1-o.rempel@pengutronix.de
Headers show
Series add generic PSE support | expand

Message

Oleksij Rempel Aug. 28, 2022, 6:30 a.m. UTC
Add generic support for the Ethernet Power Sourcing Equipment.

changes are listed within patches.

Oleksij Rempel (7):
  dt-bindings: net: phy: add PoDL PSE property
  net: add framework to support Ethernet PSE and PDs devices
  net: mdiobus: fwnode_mdiobus_register_phy() rework error handling
  net: mdiobus: search for PSE nodes by parsing PHY nodes.
  ethtool: add interface to interact with Ethernet Power Equipment
  dt-bindings: net: pse-dt: add bindings for generic PSE controller
  net: pse-pd: add regulator based PSE driver

 .../devicetree/bindings/net/ethernet-phy.yaml |   5 +
 .../bindings/net/pse-pd/pse-regulator.yaml    |  40 ++
 Documentation/networking/ethtool-netlink.rst  |  59 +++
 drivers/net/Kconfig                           |   2 +
 drivers/net/Makefile                          |   1 +
 drivers/net/mdio/fwnode_mdio.c                |  55 ++-
 drivers/net/phy/phy_device.c                  |   2 +
 drivers/net/pse-pd/Kconfig                    |  22 ++
 drivers/net/pse-pd/Makefile                   |   6 +
 drivers/net/pse-pd/pse_core.c                 | 363 ++++++++++++++++++
 drivers/net/pse-pd/pse_regulator.c            | 148 +++++++
 include/linux/phy.h                           |   2 +
 include/linux/pse-pd/pse.h                    | 155 ++++++++
 include/uapi/linux/ethtool.h                  |  45 +++
 include/uapi/linux/ethtool_netlink.h          |  17 +
 net/ethtool/Makefile                          |   3 +-
 net/ethtool/common.h                          |   1 +
 net/ethtool/netlink.c                         |  19 +
 net/ethtool/netlink.h                         |   4 +
 net/ethtool/pse-pd.c                          | 178 +++++++++
 20 files changed, 1115 insertions(+), 12 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/net/pse-pd/pse-regulator.yaml
 create mode 100644 drivers/net/pse-pd/Kconfig
 create mode 100644 drivers/net/pse-pd/Makefile
 create mode 100644 drivers/net/pse-pd/pse_core.c
 create mode 100644 drivers/net/pse-pd/pse_regulator.c
 create mode 100644 include/linux/pse-pd/pse.h
 create mode 100644 net/ethtool/pse-pd.c

Comments

Krzysztof Kozlowski Aug. 30, 2022, 5:42 p.m. UTC | #1
On 28/08/2022 09:30, Oleksij Rempel wrote:
> Add generic, regulator based PSE driver to support simple Power Sourcing
> Equipment without automatic classification support.
> 
> This driver was tested on 10Bast-T1L switch with regulator based PoDL PSE.
> 
> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
> ---
> changes v4:
> - rename to pse_regulator
> changes v2:
> - add regulator_enable test to the probe
> - migrate to the new PSE ethtool API
> ---
>  drivers/net/pse-pd/Kconfig         |  11 +++
>  drivers/net/pse-pd/Makefile        |   2 +
>  drivers/net/pse-pd/pse_regulator.c | 148 +++++++++++++++++++++++++++++
>  3 files changed, 161 insertions(+)
>  create mode 100644 drivers/net/pse-pd/pse_regulator.c
> 
> diff --git a/drivers/net/pse-pd/Kconfig b/drivers/net/pse-pd/Kconfig
> index 49c7f0bcff526..73d163704068a 100644
> --- a/drivers/net/pse-pd/Kconfig
> +++ b/drivers/net/pse-pd/Kconfig
> @@ -9,3 +9,14 @@ menuconfig PSE_CONTROLLER
>  	  Generic Power Sourcing Equipment Controller support.
>  
>  	  If unsure, say no.
> +
> +if PSE_CONTROLLER
> +
> +config PSE_REGULATOR
> +	tristate "Regulator based PSE controller"
> +	help
> +	  This module provides support for simple regulator based Ethernet Power
> +	  Sourcing Equipment without automatic classification support. For
> +	  example for basic implementation of PoDL (802.3bu) specification.
> +
> +endif
> diff --git a/drivers/net/pse-pd/Makefile b/drivers/net/pse-pd/Makefile
> index cfa780c7801dd..1b8aa4c70f0b9 100644
> --- a/drivers/net/pse-pd/Makefile
> +++ b/drivers/net/pse-pd/Makefile
> @@ -2,3 +2,5 @@
>  # Makefile for Linux PSE drivers
>  
>  obj-$(CONFIG_PSE_CONTROLLER) += pse_core.o
> +
> +obj-$(CONFIG_PSE_REGULATOR) += pse_regulator.o
> diff --git a/drivers/net/pse-pd/pse_regulator.c b/drivers/net/pse-pd/pse_regulator.c
> new file mode 100644
> index 0000000000000..46ea5b8215dcd
> --- /dev/null
> +++ b/drivers/net/pse-pd/pse_regulator.c
> @@ -0,0 +1,148 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +//
> +// Driver for the regulator based Ethernet Power Sourcing Equipment, without
> +// auto classification support.
> +//
> +// Copyright (c) 2022 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
> +//
> +
> +#include <linux/module.h>
> +#include <linux/of.h>
> +#include <linux/platform_device.h>
> +#include <linux/pse-pd/pse.h>
> +#include <linux/regulator/consumer.h>
> +
> +struct pse_reg_priv {
> +	struct pse_controller_dev pcdev;
> +	struct regulator *ps; /*power source */
> +	enum ethtool_podl_pse_admin_state admin_state;
> +};
> +
> +static struct pse_reg_priv *to_pse_reg(struct pse_controller_dev *pcdev)
> +{
> +	return container_of(pcdev, struct pse_reg_priv, pcdev);
> +}
> +
> +static int
> +pse_reg_ethtool_set_config(struct pse_controller_dev *pcdev, unsigned long id,
> +			   struct netlink_ext_ack *extack,
> +			   const struct pse_control_config *config)
> +{
> +	struct pse_reg_priv *priv = to_pse_reg(pcdev);
> +	int ret;
> +
> +	if (priv->admin_state == config->admin_cotrol)
> +		return 0;
> +
> +	switch (config->admin_cotrol) {
> +	case ETHTOOL_PODL_PSE_ADMIN_STATE_ENABLED:
> +		ret = regulator_enable(priv->ps);
> +		break;
> +	case ETHTOOL_PODL_PSE_ADMIN_STATE_DISABLED:
> +		ret = regulator_disable(priv->ps);
> +		break;
> +	default:
> +		dev_err(pcdev->dev, "Unknown admin state %i\n",
> +			config->admin_cotrol);
> +		ret = -ENOTSUPP;
> +	}
> +
> +	if (ret)
> +		return ret;
> +
> +	priv->admin_state = config->admin_cotrol;
> +
> +	return 0;
> +}
> +
> +static int
> +pse_reg_ethtool_get_status(struct pse_controller_dev *pcdev, unsigned long id,
> +			   struct netlink_ext_ack *extack,
> +			   struct pse_control_status *status)
> +{
> +	struct pse_reg_priv *priv = to_pse_reg(pcdev);
> +	int ret;
> +
> +	ret = regulator_is_enabled(priv->ps);
> +	if (ret < 0)
> +		return ret;
> +
> +	if (!ret)
> +		status->podl_pw_status = ETHTOOL_PODL_PSE_PW_D_STATUS_DISABLED;
> +	else
> +		status->podl_pw_status =
> +			ETHTOOL_PODL_PSE_PW_D_STATUS_DELIVERING;
> +
> +	status->podl_admin_state = priv->admin_state;
> +
> +	return 0;
> +}
> +
> +static const struct pse_controller_ops pse_reg_ops = {
> +	.ethtool_get_status = pse_reg_ethtool_get_status,
> +	.ethtool_set_config = pse_reg_ethtool_set_config,
> +};
> +
> +static int
> +pse_reg_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct pse_reg_priv *priv;
> +	int ret;
> +
> +	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
> +	if (!priv)
> +		return -ENOMEM;
> +
> +	if (!pdev->dev.of_node)
> +		return -ENOENT;
> +
> +	priv->ps = devm_regulator_get_exclusive(dev, "pse");
> +	if (IS_ERR(priv->ps)) {
> +		dev_err(dev, "failed to get PSE regulator (%pe)\n", priv->ps);

return dev_err_probe().

> +		return PTR_ERR(priv->ps);
> +	}
> +
> +	platform_set_drvdata(pdev, priv);
> +
> +	ret = regulator_is_enabled(priv->ps);
> +	if (ret < 0)
> +		return ret;
> +
> +	if (ret)
> +		priv->admin_state = ETHTOOL_PODL_PSE_ADMIN_STATE_ENABLED;
> +	else
> +		priv->admin_state = ETHTOOL_PODL_PSE_ADMIN_STATE_DISABLED;
> +
> +	priv->pcdev.owner = THIS_MODULE;
> +	priv->pcdev.ops = &pse_reg_ops;
> +	priv->pcdev.dev = dev;
> +	ret = devm_pse_controller_register(dev, &priv->pcdev);
> +	if (ret) {
> +		dev_err(dev, "failed to register PSE controller (%pe)\n",
> +			ERR_PTR(ret));

return dev_err_probe()

> +		return ret;
> +	}
> +
> +	return 0;
> +}
> +
> +static const struct of_device_id pse_reg_of_match[] = {
> +	{ .compatible = "podl-pse-regulator", },
> +	{ },
> +};
> +MODULE_DEVICE_TABLE(of, pse_reg_of_match);
> +
> +static struct platform_driver pse_reg_driver = {
> +	.probe		= pse_reg_probe,
> +	.driver		= {
> +		.name		= "PSE regulator",
> +		.of_match_table = of_match_ptr(pse_reg_of_match),

You need to compile test it... No of_match_ptr() or add maybe_unused to
the table.

Best regards,
Krzysztof
Rob Herring Aug. 30, 2022, 8:19 p.m. UTC | #2
On Sun, Aug 28, 2022 at 08:30:15AM +0200, Oleksij Rempel wrote:
> Add property to reference node representing a PoDL Power Sourcing Equipment.
> 
> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
> ---
>  Documentation/devicetree/bindings/net/ethernet-phy.yaml | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/Documentation/devicetree/bindings/net/ethernet-phy.yaml b/Documentation/devicetree/bindings/net/ethernet-phy.yaml
> index ed1415a4381f2..0b7b9dc69d454 100644
> --- a/Documentation/devicetree/bindings/net/ethernet-phy.yaml
> +++ b/Documentation/devicetree/bindings/net/ethernet-phy.yaml
> @@ -144,6 +144,11 @@ properties:
>        Mark the corresponding energy efficient ethernet mode as
>        broken and request the ethernet to stop advertising it.
>  
> +  ieee802.3-pse:
> +    $ref: /schemas/types.yaml#/definitions/phandle

If you are saying this is the only location we'll ever find 
'ieee802.3-pse', then defining it here is okay. Otherwise, it 
needs to be it's own file so there's exactly 1 type definition. As 
you've said it might have cells by defining #pse-cells, 'phandle' is 
the wrong type. 'phandle-array' is what you want, but then actual users 
need to define how many entries (i.e. maxItems). Just like all the 
other provider/consumer bindings are structured.

Rob
Jakub Kicinski Aug. 30, 2022, 10:46 p.m. UTC | #3
On Sun, 28 Aug 2022 08:30:19 +0200 Oleksij Rempel wrote:
> +  ``ETHTOOL_A_PODL_PSE_ADMIN_STATE``          u8  Operational state of the PoDL
> +                                                  PSE functions
> +  ``ETHTOOL_A_PODL_PSE_PW_D_STATUS``          u8  power detection status of the
> +                                                  PoDL PSE.

If you don't mind a nit pick - u32 on these, netlink rounds up
attribute size to 4B, there's no difference in size for the two
so no point limiting ourselves.
Jakub Kicinski Aug. 30, 2022, 10:52 p.m. UTC | #4
On Sun, 28 Aug 2022 08:30:19 +0200 Oleksij Rempel wrote:
> Add interface to support Power Sourcing Equipment. At current step it
> provides generic way to address all variants of PSE devices as defined
> in IEEE 802.3-2018 but support only objects specified for IEEE 802.3-2018 104.4
> PoDL Power Sourcing Equipment (PSE).
> 
> Currently supported and mandatory objects are:
> IEEE 802.3-2018 30.15.1.1.3 aPoDLPSEPowerDetectionStatus
> IEEE 802.3-2018 30.15.1.1.2 aPoDLPSEAdminState
> IEEE 802.3-2018 30.15.1.2.1 acPoDLPSEAdminControl

warning here:

drivers/net/pse-pd/pse_core.c:304: warning: Function parameter or member 'status' not described in 'pse_ethtool_get_status'