Message ID | 20200418105432.11233-3-calvin.johnson@oss.nxp.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | ACPI support for xgmac_mdio and dpaa2-mac drivers | expand |
On Sat, Apr 18, 2020 at 04:24:32PM +0530, Calvin Johnson wrote: > Modify dpaa2_mac_connect() to support ACPI along with DT. > Modify dpaa2_mac_get_node() to get the dpmac fwnode from either > DT or ACPI. > Replace of_get_phy_mode with fwnode_get_phy_mode to get > phy-mode for a dpmac_node. > Define and use helper functions fwnode_phy_match() and > fwnode_phy_find_device() to find phy_dev that is later > connected to mac->phylink. > > Signed-off-by: Calvin Johnson <calvin.johnson@oss.nxp.com> > --- > > Changes in v2: > - Major change following other network drivers supporting ACPI > - dropped v1 patches 1, 2, 4, 5 and 6 as they are no longer valid > - incorporated other v1 review comments > > .../net/ethernet/freescale/dpaa2/dpaa2-mac.c | 122 ++++++++++++++---- > 1 file changed, 94 insertions(+), 28 deletions(-) > > diff --git a/drivers/net/ethernet/freescale/dpaa2/dpaa2-mac.c b/drivers/net/ethernet/freescale/dpaa2/dpaa2-mac.c > index 3ee236c5fc37..5a03da54a67f 100644 > --- a/drivers/net/ethernet/freescale/dpaa2/dpaa2-mac.c > +++ b/drivers/net/ethernet/freescale/dpaa2/dpaa2-mac.c > @@ -3,6 +3,9 @@ > > #include "dpaa2-eth.h" > #include "dpaa2-mac.h" > +#include <linux/acpi.h> > +#include <linux/phy.h> > +#include <linux/phylink.h> Why do you need linux/phy.h and linux/phylink.h here? Please try building the driver without; you'll find they are already included via dpaa2-mac.h. > +static int fwnode_phy_match(struct device *dev, const void *phy_fwnode) > +{ > + return dev->fwnode == phy_fwnode; > +} > + > +static struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode) > +{ > + struct device *d; > + struct mdio_device *mdiodev; > + > + if (!phy_fwnode) > + return NULL; > + > + d = bus_find_device(&mdio_bus_type, NULL, phy_fwnode, fwnode_phy_match); > + if (d) { > + mdiodev = to_mdio_device(d); > + if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) > + return to_phy_device(d); > + put_device(d); > + } > + > + return NULL; > +} This is groping around in the mdio bus implementation details; drivers must not do this layering violation. Please propose an interface in the mdio code to do what you need. > + > int dpaa2_mac_connect(struct dpaa2_mac *mac) > { > struct fsl_mc_device *dpmac_dev = mac->mc_dev; > struct net_device *net_dev = mac->net_dev; > - struct device_node *dpmac_node; > + struct fwnode_handle *dpmac_node = NULL; > + struct fwnode_reference_args args; > + struct phy_device *phy_dev; > struct phylink *phylink; > struct dpmac_attr attr; > + int status; > int err; > > err = dpmac_open(mac->mc_io, 0, dpmac_dev->obj_desc.id, > @@ -251,7 +299,7 @@ int dpaa2_mac_connect(struct dpaa2_mac *mac) > > mac->if_link_type = attr.link_type; > > - dpmac_node = dpaa2_mac_get_node(attr.id); > + dpmac_node = dpaa2_mac_get_node(&mac->mc_dev->dev, attr.id); > if (!dpmac_node) { > netdev_err(net_dev, "No dpmac@%d node found.\n", attr.id); > err = -ENODEV; > @@ -269,7 +317,7 @@ int dpaa2_mac_connect(struct dpaa2_mac *mac) > * error out if the interface mode requests them and there is no PHY > * to act upon them > */ > - if (of_phy_is_fixed_link(dpmac_node) && > + if (of_phy_is_fixed_link(to_of_node(dpmac_node)) && > (mac->if_mode == PHY_INTERFACE_MODE_RGMII_ID || > mac->if_mode == PHY_INTERFACE_MODE_RGMII_RXID || > mac->if_mode == PHY_INTERFACE_MODE_RGMII_TXID)) { > @@ -282,7 +330,7 @@ int dpaa2_mac_connect(struct dpaa2_mac *mac) > mac->phylink_config.type = PHYLINK_NETDEV; > > phylink = phylink_create(&mac->phylink_config, > - of_fwnode_handle(dpmac_node), mac->if_mode, > + dpmac_node, mac->if_mode, > &dpaa2_mac_phylink_ops); > if (IS_ERR(phylink)) { > err = PTR_ERR(phylink); > @@ -290,20 +338,38 @@ int dpaa2_mac_connect(struct dpaa2_mac *mac) > } > mac->phylink = phylink; > > - err = phylink_of_phy_connect(mac->phylink, dpmac_node, 0); > + if (is_of_node(dpmac_node)) > + err = phylink_of_phy_connect(mac->phylink, > + to_of_node(dpmac_node), 0); > + else if (is_acpi_node(dpmac_node)) { > + status = acpi_node_get_property_reference(dpmac_node, > + "phy-handle", > + 0, &args); > + if (ACPI_FAILURE(status)) > + goto err_phylink_destroy; > + phy_dev = fwnode_phy_find_device(args.fwnode); > + if (!phy_dev) > + goto err_phylink_destroy; > + > + err = phylink_connect_phy(mac->phylink, phy_dev); > + if (err) > + phy_detach(phy_dev); phy_detach() reverses the effect of phy_attach_direct(). This is not the correct cleanup function in this case, because the PHY hasn't been attached (and phylink_connect_phy() will clean up any effects it has on error.) You only need to reverse the effect of your fwnode_phy_find_device(), which phy_detach() is inappropriate for. In any case, if this method of getting a PHY is accepted by ACPI folk, it could be moved into a helper in phylink_fwnode_phy_connect() - and that really needs to happen before a patch adding this functionality is acceptable. > + } > if (err) { > - netdev_err(net_dev, "phylink_of_phy_connect() = %d\n", err); > + netdev_err(net_dev, "phylink_fwnode_phy_connect() = %d\n", err); That's a very misleading change - there is no function named as per your new name. > goto err_phylink_destroy; > } > > - of_node_put(dpmac_node); > + if (is_of_node(dpmac_node)) > + of_node_put(to_of_node(dpmac_node)); > > return 0; > > err_phylink_destroy: > phylink_destroy(mac->phylink); > err_put_node: > - of_node_put(dpmac_node); > + if (is_of_node(dpmac_node)) > + of_node_put(to_of_node(dpmac_node)); > err_close_dpmac: > dpmac_close(mac->mc_io, 0, dpmac_dev->mc_handle); > return err; > -- > 2.17.1 > >
> - err = phylink_of_phy_connect(mac->phylink, dpmac_node, 0); > + if (is_of_node(dpmac_node)) > + err = phylink_of_phy_connect(mac->phylink, > + to_of_node(dpmac_node), 0); > + else if (is_acpi_node(dpmac_node)) { > + status = acpi_node_get_property_reference(dpmac_node, > + "phy-handle", > + 0, &args); > + if (ACPI_FAILURE(status)) > + goto err_phylink_destroy; > + phy_dev = fwnode_phy_find_device(args.fwnode); > + if (!phy_dev) > + goto err_phylink_destroy; > + > + err = phylink_connect_phy(mac->phylink, phy_dev); > + if (err) > + phy_detach(phy_dev); So it looks like you need to add a phylink_fwnode_phy_connect(). And maybe on top of that you need a phylink_device_phy_connect()? So please stop. Take a step back, look at how the of_, device_, fwnode_, and acpi_ abstractions all stack on top of each other, then propose phylib and phylink core changes to implement these abstractions. Andrew
On Sat, Apr 18, 2020 at 12:38:13PM +0100, Russell King - ARM Linux admin wrote: > On Sat, Apr 18, 2020 at 04:24:32PM +0530, Calvin Johnson wrote: > > Modify dpaa2_mac_connect() to support ACPI along with DT. > > Modify dpaa2_mac_get_node() to get the dpmac fwnode from either > > DT or ACPI. > > Replace of_get_phy_mode with fwnode_get_phy_mode to get > > phy-mode for a dpmac_node. > > Define and use helper functions fwnode_phy_match() and > > fwnode_phy_find_device() to find phy_dev that is later > > connected to mac->phylink. > > > > Signed-off-by: Calvin Johnson <calvin.johnson@oss.nxp.com> > > --- > > > > Changes in v2: > > - Major change following other network drivers supporting ACPI > > - dropped v1 patches 1, 2, 4, 5 and 6 as they are no longer valid > > - incorporated other v1 review comments > > > > .../net/ethernet/freescale/dpaa2/dpaa2-mac.c | 122 ++++++++++++++---- > > 1 file changed, 94 insertions(+), 28 deletions(-) > > > > diff --git a/drivers/net/ethernet/freescale/dpaa2/dpaa2-mac.c b/drivers/net/ethernet/freescale/dpaa2/dpaa2-mac.c > > index 3ee236c5fc37..5a03da54a67f 100644 > > --- a/drivers/net/ethernet/freescale/dpaa2/dpaa2-mac.c > > +++ b/drivers/net/ethernet/freescale/dpaa2/dpaa2-mac.c > > @@ -3,6 +3,9 @@ > > > > #include "dpaa2-eth.h" > > #include "dpaa2-mac.h" > > +#include <linux/acpi.h> > > +#include <linux/phy.h> > > +#include <linux/phylink.h> > > Why do you need linux/phy.h and linux/phylink.h here? Please try > building the driver without; you'll find they are already included > via dpaa2-mac.h. You are right. I'll remove them in v3 > > +static int fwnode_phy_match(struct device *dev, const void *phy_fwnode) > > +{ > > + return dev->fwnode == phy_fwnode; > > +} > > + > > +static struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode) > > +{ > > + struct device *d; > > + struct mdio_device *mdiodev; > > + > > + if (!phy_fwnode) > > + return NULL; > > + > > + d = bus_find_device(&mdio_bus_type, NULL, phy_fwnode, fwnode_phy_match); > > + if (d) { > > + mdiodev = to_mdio_device(d); > > + if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) > > + return to_phy_device(d); > > + put_device(d); > > + } > > + > > + return NULL; > > +} > > This is groping around in the mdio bus implementation details; drivers > must not do this layering violation. Please propose an interface in > the mdio code to do what you need. I'll study and propose a solution. > > > + > > int dpaa2_mac_connect(struct dpaa2_mac *mac) > > { > > struct fsl_mc_device *dpmac_dev = mac->mc_dev; > > struct net_device *net_dev = mac->net_dev; > > - struct device_node *dpmac_node; > > + struct fwnode_handle *dpmac_node = NULL; > > + struct fwnode_reference_args args; > > + struct phy_device *phy_dev; > > struct phylink *phylink; > > struct dpmac_attr attr; > > + int status; > > int err; > > > > err = dpmac_open(mac->mc_io, 0, dpmac_dev->obj_desc.id, > > @@ -251,7 +299,7 @@ int dpaa2_mac_connect(struct dpaa2_mac *mac) > > > > mac->if_link_type = attr.link_type; > > > > - dpmac_node = dpaa2_mac_get_node(attr.id); > > + dpmac_node = dpaa2_mac_get_node(&mac->mc_dev->dev, attr.id); > > if (!dpmac_node) { > > netdev_err(net_dev, "No dpmac@%d node found.\n", attr.id); > > err = -ENODEV; > > @@ -269,7 +317,7 @@ int dpaa2_mac_connect(struct dpaa2_mac *mac) > > * error out if the interface mode requests them and there is no PHY > > * to act upon them > > */ > > - if (of_phy_is_fixed_link(dpmac_node) && > > + if (of_phy_is_fixed_link(to_of_node(dpmac_node)) && > > (mac->if_mode == PHY_INTERFACE_MODE_RGMII_ID || > > mac->if_mode == PHY_INTERFACE_MODE_RGMII_RXID || > > mac->if_mode == PHY_INTERFACE_MODE_RGMII_TXID)) { > > @@ -282,7 +330,7 @@ int dpaa2_mac_connect(struct dpaa2_mac *mac) > > mac->phylink_config.type = PHYLINK_NETDEV; > > > > phylink = phylink_create(&mac->phylink_config, > > - of_fwnode_handle(dpmac_node), mac->if_mode, > > + dpmac_node, mac->if_mode, > > &dpaa2_mac_phylink_ops); > > if (IS_ERR(phylink)) { > > err = PTR_ERR(phylink); > > @@ -290,20 +338,38 @@ int dpaa2_mac_connect(struct dpaa2_mac *mac) > > } > > mac->phylink = phylink; > > > > - err = phylink_of_phy_connect(mac->phylink, dpmac_node, 0); > > + if (is_of_node(dpmac_node)) > > + err = phylink_of_phy_connect(mac->phylink, > > + to_of_node(dpmac_node), 0); > > + else if (is_acpi_node(dpmac_node)) { > > + status = acpi_node_get_property_reference(dpmac_node, > > + "phy-handle", > > + 0, &args); > > + if (ACPI_FAILURE(status)) > > + goto err_phylink_destroy; > > + phy_dev = fwnode_phy_find_device(args.fwnode); > > + if (!phy_dev) > > + goto err_phylink_destroy; > > + > > + err = phylink_connect_phy(mac->phylink, phy_dev); > > + if (err) > > + phy_detach(phy_dev); > > phy_detach() reverses the effect of phy_attach_direct(). This is not > the correct cleanup function in this case, because the PHY hasn't been > attached (and phylink_connect_phy() will clean up any effects it has > on error.) You only need to reverse the effect of your > fwnode_phy_find_device(), which phy_detach() is inappropriate for. Got it. I'll repair this part. > > In any case, if this method of getting a PHY is accepted by ACPI folk, > it could be moved into a helper in phylink_fwnode_phy_connect() - and > that really needs to happen before a patch adding this functionality is > acceptable. There is already similar code in upstream kernel: https://elixir.bootlin.com/linux/v5.7-rc2/source/drivers/net/ethernet/apm/xgene/xgene_enet_hw.c#L825 It makes sense to have a generic helper. Will create one. Hope I can introduce it in the v3 patchset, ofcourse phylink_fwnode_phy_connect will be defined in a patch before it is called. Let me know if it is not okay. > > > + } > > if (err) { > > - netdev_err(net_dev, "phylink_of_phy_connect() = %d\n", err); > > + netdev_err(net_dev, "phylink_fwnode_phy_connect() = %d\n", err); > > That's a very misleading change - there is no function named as per your > new name. My bad. Sorry. Will correct it. Thanks Calvin
diff --git a/drivers/net/ethernet/freescale/dpaa2/dpaa2-mac.c b/drivers/net/ethernet/freescale/dpaa2/dpaa2-mac.c index 3ee236c5fc37..5a03da54a67f 100644 --- a/drivers/net/ethernet/freescale/dpaa2/dpaa2-mac.c +++ b/drivers/net/ethernet/freescale/dpaa2/dpaa2-mac.c @@ -3,6 +3,9 @@ #include "dpaa2-eth.h" #include "dpaa2-mac.h" +#include <linux/acpi.h> +#include <linux/phy.h> +#include <linux/phylink.h> #define phylink_to_dpaa2_mac(config) \ container_of((config), struct dpaa2_mac, phylink_config) @@ -23,38 +26,56 @@ static int phy_mode(enum dpmac_eth_if eth_if, phy_interface_t *if_mode) } /* Caller must call of_node_put on the returned value */ -static struct device_node *dpaa2_mac_get_node(u16 dpmac_id) +static struct fwnode_handle *dpaa2_mac_get_node(struct device *dev, + u16 dpmac_id) { - struct device_node *dpmacs, *dpmac = NULL; - u32 id; + struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; + struct fwnode_handle *dpmacs, *dpmac = NULL; + unsigned long long adr; + acpi_status status; int err; + u32 id; - dpmacs = of_find_node_by_name(NULL, "dpmacs"); - if (!dpmacs) - return NULL; + if (is_of_node(dev->parent->fwnode)) { + dpmacs = device_get_named_child_node(dev->parent, "dpmacs"); + if (!dpmacs) + return NULL; + + while ((dpmac = fwnode_get_next_child_node(dpmacs, dpmac))) { + err = fwnode_property_read_u32(dpmac, "reg", &id); + if (err) + continue; + if (id == dpmac_id) + return dpmac; + } - while ((dpmac = of_get_next_child(dpmacs, dpmac)) != NULL) { - err = of_property_read_u32(dpmac, "reg", &id); - if (err) - continue; - if (id == dpmac_id) - break; + } else if (is_acpi_node(dev->parent->fwnode)) { + device_for_each_child_node(dev->parent, dpmac) { + status = acpi_evaluate_integer(ACPI_HANDLE_FWNODE(dpmac), + "_ADR", NULL, &adr); + if (ACPI_FAILURE(status)) { + pr_debug("_ADR returned %d on %s\n", + status, (char *)buffer.pointer); + continue; + } else { + id = (u32)adr; + if (id == dpmac_id) + return dpmac; + } + } } - - of_node_put(dpmacs); - - return dpmac; + return NULL; } -static int dpaa2_mac_get_if_mode(struct device_node *node, +static int dpaa2_mac_get_if_mode(struct fwnode_handle *dpmac_node, struct dpmac_attr attr) { phy_interface_t if_mode; int err; - err = of_get_phy_mode(node, &if_mode); - if (!err) - return if_mode; + err = fwnode_get_phy_mode(dpmac_node); + if (err > 0) + return err; err = phy_mode(attr.eth_if, &if_mode); if (!err) @@ -227,13 +248,40 @@ bool dpaa2_mac_is_type_fixed(struct fsl_mc_device *dpmac_dev, return fixed; } +static int fwnode_phy_match(struct device *dev, const void *phy_fwnode) +{ + return dev->fwnode == phy_fwnode; +} + +static struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode) +{ + struct device *d; + struct mdio_device *mdiodev; + + if (!phy_fwnode) + return NULL; + + d = bus_find_device(&mdio_bus_type, NULL, phy_fwnode, fwnode_phy_match); + if (d) { + mdiodev = to_mdio_device(d); + if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) + return to_phy_device(d); + put_device(d); + } + + return NULL; +} + int dpaa2_mac_connect(struct dpaa2_mac *mac) { struct fsl_mc_device *dpmac_dev = mac->mc_dev; struct net_device *net_dev = mac->net_dev; - struct device_node *dpmac_node; + struct fwnode_handle *dpmac_node = NULL; + struct fwnode_reference_args args; + struct phy_device *phy_dev; struct phylink *phylink; struct dpmac_attr attr; + int status; int err; err = dpmac_open(mac->mc_io, 0, dpmac_dev->obj_desc.id, @@ -251,7 +299,7 @@ int dpaa2_mac_connect(struct dpaa2_mac *mac) mac->if_link_type = attr.link_type; - dpmac_node = dpaa2_mac_get_node(attr.id); + dpmac_node = dpaa2_mac_get_node(&mac->mc_dev->dev, attr.id); if (!dpmac_node) { netdev_err(net_dev, "No dpmac@%d node found.\n", attr.id); err = -ENODEV; @@ -269,7 +317,7 @@ int dpaa2_mac_connect(struct dpaa2_mac *mac) * error out if the interface mode requests them and there is no PHY * to act upon them */ - if (of_phy_is_fixed_link(dpmac_node) && + if (of_phy_is_fixed_link(to_of_node(dpmac_node)) && (mac->if_mode == PHY_INTERFACE_MODE_RGMII_ID || mac->if_mode == PHY_INTERFACE_MODE_RGMII_RXID || mac->if_mode == PHY_INTERFACE_MODE_RGMII_TXID)) { @@ -282,7 +330,7 @@ int dpaa2_mac_connect(struct dpaa2_mac *mac) mac->phylink_config.type = PHYLINK_NETDEV; phylink = phylink_create(&mac->phylink_config, - of_fwnode_handle(dpmac_node), mac->if_mode, + dpmac_node, mac->if_mode, &dpaa2_mac_phylink_ops); if (IS_ERR(phylink)) { err = PTR_ERR(phylink); @@ -290,20 +338,38 @@ int dpaa2_mac_connect(struct dpaa2_mac *mac) } mac->phylink = phylink; - err = phylink_of_phy_connect(mac->phylink, dpmac_node, 0); + if (is_of_node(dpmac_node)) + err = phylink_of_phy_connect(mac->phylink, + to_of_node(dpmac_node), 0); + else if (is_acpi_node(dpmac_node)) { + status = acpi_node_get_property_reference(dpmac_node, + "phy-handle", + 0, &args); + if (ACPI_FAILURE(status)) + goto err_phylink_destroy; + phy_dev = fwnode_phy_find_device(args.fwnode); + if (!phy_dev) + goto err_phylink_destroy; + + err = phylink_connect_phy(mac->phylink, phy_dev); + if (err) + phy_detach(phy_dev); + } if (err) { - netdev_err(net_dev, "phylink_of_phy_connect() = %d\n", err); + netdev_err(net_dev, "phylink_fwnode_phy_connect() = %d\n", err); goto err_phylink_destroy; } - of_node_put(dpmac_node); + if (is_of_node(dpmac_node)) + of_node_put(to_of_node(dpmac_node)); return 0; err_phylink_destroy: phylink_destroy(mac->phylink); err_put_node: - of_node_put(dpmac_node); + if (is_of_node(dpmac_node)) + of_node_put(to_of_node(dpmac_node)); err_close_dpmac: dpmac_close(mac->mc_io, 0, dpmac_dev->mc_handle); return err;
Modify dpaa2_mac_connect() to support ACPI along with DT. Modify dpaa2_mac_get_node() to get the dpmac fwnode from either DT or ACPI. Replace of_get_phy_mode with fwnode_get_phy_mode to get phy-mode for a dpmac_node. Define and use helper functions fwnode_phy_match() and fwnode_phy_find_device() to find phy_dev that is later connected to mac->phylink. Signed-off-by: Calvin Johnson <calvin.johnson@oss.nxp.com> --- Changes in v2: - Major change following other network drivers supporting ACPI - dropped v1 patches 1, 2, 4, 5 and 6 as they are no longer valid - incorporated other v1 review comments .../net/ethernet/freescale/dpaa2/dpaa2-mac.c | 122 ++++++++++++++---- 1 file changed, 94 insertions(+), 28 deletions(-)