Linux PCI subsystem development
 help / color / mirror / Atom feed
* [PATCH 0/5] Simplify code with _scoped() helper functions
@ 2024-08-28  7:38 Zhang Zekun
  2024-08-28  7:38 ` [PATCH 1/5] PCI: kirin: Use helper function for_each_available_child_of_node_scoped() Zhang Zekun
                   ` (4 more replies)
  0 siblings, 5 replies; 13+ messages in thread
From: Zhang Zekun @ 2024-08-28  7:38 UTC (permalink / raw)
  To: songxiaowei, wangbinghui, lpieralisi, kw, manivannan.sadhasivam,
	robh, bhelgaas, linux-pci, ryder.lee, jianjun.wang,
	sergio.paracuellos, angelogioacchino.delregno, matthias.bgg,
	alyssa, maz, thierry.reding
  Cc: zhangzekun11

Use _scoped() functions to iterate through the child node, and we don't
need to call of_node_put() manually. This can simplify the code a bit.

Zhang Zekun (5):
  PCI: kirin: Use helper function
    for_each_available_child_of_node_scoped()
  PCI: mediatek: Use helper function
    for_each_available_child_of_node_scoped()
  PCI: mt7621: Use helper function
    for_each_available_child_of_node_scoped()
  PCI: apple: Use helper function for_each_child_of_node_scoped()
  PCI: tegra: Use helper function for_each_child_of_node_scoped()

 drivers/pci/controller/dwc/pcie-kirin.c | 10 ++----
 drivers/pci/controller/pci-tegra.c      | 41 +++++++++----------------
 drivers/pci/controller/pcie-apple.c     |  4 +--
 drivers/pci/controller/pcie-mediatek.c  | 11 +++----
 drivers/pci/controller/pcie-mt7621.c    |  9 ++----
 5 files changed, 25 insertions(+), 50 deletions(-)

-- 
2.17.1


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH 1/5] PCI: kirin: Use helper function for_each_available_child_of_node_scoped()
  2024-08-28  7:38 [PATCH 0/5] Simplify code with _scoped() helper functions Zhang Zekun
@ 2024-08-28  7:38 ` Zhang Zekun
  2024-08-28 12:11   ` Jonathan Cameron
  2024-08-28  7:38 ` [PATCH 2/5] PCI: mediatek: " Zhang Zekun
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 13+ messages in thread
From: Zhang Zekun @ 2024-08-28  7:38 UTC (permalink / raw)
  To: songxiaowei, wangbinghui, lpieralisi, kw, manivannan.sadhasivam,
	robh, bhelgaas, linux-pci, ryder.lee, jianjun.wang,
	sergio.paracuellos, angelogioacchino.delregno, matthias.bgg,
	alyssa, maz, thierry.reding
  Cc: zhangzekun11

for_each_available_child_of_node_scoped() provides a scope-based cleanup
functinality to put the device_node automatically, and we don't need to
call of_node_put() directly.  Let's simplify the code a bit with the use
of these functions.

Signed-off-by: Zhang Zekun <zhangzekun11@huawei.com>
---
 drivers/pci/controller/dwc/pcie-kirin.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/drivers/pci/controller/dwc/pcie-kirin.c b/drivers/pci/controller/dwc/pcie-kirin.c
index 0a29136491b8..e9bda1746ca5 100644
--- a/drivers/pci/controller/dwc/pcie-kirin.c
+++ b/drivers/pci/controller/dwc/pcie-kirin.c
@@ -452,7 +452,7 @@ static long kirin_pcie_get_resource(struct kirin_pcie *kirin_pcie,
 				    struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
-	struct device_node *child, *node = dev->of_node;
+	struct device_node *node = dev->of_node;
 	void __iomem *apb_base;
 	int ret;
 
@@ -477,17 +477,13 @@ static long kirin_pcie_get_resource(struct kirin_pcie *kirin_pcie,
 		return ret;
 
 	/* Parse OF children */
-	for_each_available_child_of_node(node, child) {
+	for_each_available_child_of_node_scoped(node, child) {
 		ret = kirin_pcie_parse_port(kirin_pcie, pdev, child);
 		if (ret)
-			goto put_node;
+			return ret;
 	}
 
 	return 0;
-
-put_node:
-	of_node_put(child);
-	return ret;
 }
 
 static void kirin_pcie_sideband_dbi_w_mode(struct kirin_pcie *kirin_pcie,
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 2/5] PCI: mediatek: Use helper function for_each_available_child_of_node_scoped()
  2024-08-28  7:38 [PATCH 0/5] Simplify code with _scoped() helper functions Zhang Zekun
  2024-08-28  7:38 ` [PATCH 1/5] PCI: kirin: Use helper function for_each_available_child_of_node_scoped() Zhang Zekun
@ 2024-08-28  7:38 ` Zhang Zekun
  2024-08-28 12:16   ` Jonathan Cameron
  2024-08-28  7:38 ` [PATCH 3/5] PCI: mt7621: " Zhang Zekun
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 13+ messages in thread
From: Zhang Zekun @ 2024-08-28  7:38 UTC (permalink / raw)
  To: songxiaowei, wangbinghui, lpieralisi, kw, manivannan.sadhasivam,
	robh, bhelgaas, linux-pci, ryder.lee, jianjun.wang,
	sergio.paracuellos, angelogioacchino.delregno, matthias.bgg,
	alyssa, maz, thierry.reding
  Cc: zhangzekun11

for_each_available_child_of_node_scoped() provides a scope-based cleanup
functinality to put the device_node automatically, and we don't need to
call of_node_put() directly.  Let's simplify the code a bit with the use
of these functions.

Signed-off-by: Zhang Zekun <zhangzekun11@huawei.com>
---
 drivers/pci/controller/pcie-mediatek.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/drivers/pci/controller/pcie-mediatek.c b/drivers/pci/controller/pcie-mediatek.c
index 9be3cebd862e..d14e8d151c08 100644
--- a/drivers/pci/controller/pcie-mediatek.c
+++ b/drivers/pci/controller/pcie-mediatek.c
@@ -1042,24 +1042,24 @@ static int mtk_pcie_subsys_powerup(struct mtk_pcie *pcie)
 static int mtk_pcie_setup(struct mtk_pcie *pcie)
 {
 	struct device *dev = pcie->dev;
-	struct device_node *node = dev->of_node, *child;
+	struct device_node *node = dev->of_node;
 	struct mtk_pcie_port *port, *tmp;
 	int err, slot;
 
 	slot = of_get_pci_domain_nr(dev->of_node);
 	if (slot < 0) {
-		for_each_available_child_of_node(node, child) {
+		for_each_available_child_of_node_scoped(node, child) {
 			err = of_pci_get_devfn(child);
 			if (err < 0) {
 				dev_err(dev, "failed to get devfn: %d\n", err);
-				goto error_put_node;
+				return err;
 			}
 
 			slot = PCI_SLOT(err);
 
 			err = mtk_pcie_parse_port(pcie, child, slot);
 			if (err)
-				goto error_put_node;
+				return err;
 		}
 	} else {
 		err = mtk_pcie_parse_port(pcie, node, slot);
@@ -1080,9 +1080,6 @@ static int mtk_pcie_setup(struct mtk_pcie *pcie)
 		mtk_pcie_subsys_powerdown(pcie);
 
 	return 0;
-error_put_node:
-	of_node_put(child);
-	return err;
 }
 
 static int mtk_pcie_probe(struct platform_device *pdev)
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 3/5] PCI: mt7621: Use helper function for_each_available_child_of_node_scoped()
  2024-08-28  7:38 [PATCH 0/5] Simplify code with _scoped() helper functions Zhang Zekun
  2024-08-28  7:38 ` [PATCH 1/5] PCI: kirin: Use helper function for_each_available_child_of_node_scoped() Zhang Zekun
  2024-08-28  7:38 ` [PATCH 2/5] PCI: mediatek: " Zhang Zekun
@ 2024-08-28  7:38 ` Zhang Zekun
  2024-08-28  9:18   ` Sergio Paracuellos
  2024-08-28 12:17   ` Jonathan Cameron
  2024-08-28  7:38 ` [PATCH 4/5] PCI: apple: Use helper function for_each_child_of_node_scoped() Zhang Zekun
  2024-08-28  7:38 ` [PATCH 5/5] PCI: tegra: " Zhang Zekun
  4 siblings, 2 replies; 13+ messages in thread
From: Zhang Zekun @ 2024-08-28  7:38 UTC (permalink / raw)
  To: songxiaowei, wangbinghui, lpieralisi, kw, manivannan.sadhasivam,
	robh, bhelgaas, linux-pci, ryder.lee, jianjun.wang,
	sergio.paracuellos, angelogioacchino.delregno, matthias.bgg,
	alyssa, maz, thierry.reding
  Cc: zhangzekun11

for_each_available_child_of_node_scoped() provides a scope-based cleanup
functinality to put the device_node automatically, and we don't need to
call of_node_put() directly.  Let's simplify the code a bit with the use
of these functions.

Signed-off-by: Zhang Zekun <zhangzekun11@huawei.com>
---
 drivers/pci/controller/pcie-mt7621.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/drivers/pci/controller/pcie-mt7621.c b/drivers/pci/controller/pcie-mt7621.c
index 9b4754a45515..16c3df1c49a1 100644
--- a/drivers/pci/controller/pcie-mt7621.c
+++ b/drivers/pci/controller/pcie-mt7621.c
@@ -258,19 +258,18 @@ static int mt7621_pcie_parse_dt(struct mt7621_pcie *pcie)
 {
 	struct device *dev = pcie->dev;
 	struct platform_device *pdev = to_platform_device(dev);
-	struct device_node *node = dev->of_node, *child;
+	struct device_node *node = dev->of_node;
 	int err;
 
 	pcie->base = devm_platform_ioremap_resource(pdev, 0);
 	if (IS_ERR(pcie->base))
 		return PTR_ERR(pcie->base);
 
-	for_each_available_child_of_node(node, child) {
+	for_each_available_child_of_node_scoped(node, child) {
 		int slot;
 
 		err = of_pci_get_devfn(child);
 		if (err < 0) {
-			of_node_put(child);
 			dev_err(dev, "failed to parse devfn: %d\n", err);
 			return err;
 		}
@@ -278,10 +277,8 @@ static int mt7621_pcie_parse_dt(struct mt7621_pcie *pcie)
 		slot = PCI_SLOT(err);
 
 		err = mt7621_pcie_parse_port(pcie, child, slot);
-		if (err) {
-			of_node_put(child);
+		if (err)
 			return err;
-		}
 	}
 
 	return 0;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 4/5] PCI: apple: Use helper function for_each_child_of_node_scoped()
  2024-08-28  7:38 [PATCH 0/5] Simplify code with _scoped() helper functions Zhang Zekun
                   ` (2 preceding siblings ...)
  2024-08-28  7:38 ` [PATCH 3/5] PCI: mt7621: " Zhang Zekun
@ 2024-08-28  7:38 ` Zhang Zekun
  2024-08-28 12:19   ` Jonathan Cameron
  2024-08-28  7:38 ` [PATCH 5/5] PCI: tegra: " Zhang Zekun
  4 siblings, 1 reply; 13+ messages in thread
From: Zhang Zekun @ 2024-08-28  7:38 UTC (permalink / raw)
  To: songxiaowei, wangbinghui, lpieralisi, kw, manivannan.sadhasivam,
	robh, bhelgaas, linux-pci, ryder.lee, jianjun.wang,
	sergio.paracuellos, angelogioacchino.delregno, matthias.bgg,
	alyssa, maz, thierry.reding
  Cc: zhangzekun11

for_each_child_of_node_scoped() provides a scope-based cleanup
functinality to put the device_node automatically, and we don't need to
call of_node_put() directly.  Let's simplify the code a bit with the
use of these functions.

Signed-off-by: Zhang Zekun <zhangzekun11@huawei.com>
---
 drivers/pci/controller/pcie-apple.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c
index fefab2758a06..f1bd20a64b67 100644
--- a/drivers/pci/controller/pcie-apple.c
+++ b/drivers/pci/controller/pcie-apple.c
@@ -764,7 +764,6 @@ static int apple_pcie_init(struct pci_config_window *cfg)
 {
 	struct device *dev = cfg->parent;
 	struct platform_device *platform = to_platform_device(dev);
-	struct device_node *of_port;
 	struct apple_pcie *pcie;
 	int ret;
 
@@ -787,11 +786,10 @@ static int apple_pcie_init(struct pci_config_window *cfg)
 	if (ret)
 		return ret;
 
-	for_each_child_of_node(dev->of_node, of_port) {
+	for_each_child_of_node_scoped(dev->of_node, of_port) {
 		ret = apple_pcie_setup_port(pcie, of_port);
 		if (ret) {
 			dev_err(pcie->dev, "Port %pOF setup fail: %d\n", of_port, ret);
-			of_node_put(of_port);
 			return ret;
 		}
 	}
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 5/5] PCI: tegra: Use helper function for_each_child_of_node_scoped()
  2024-08-28  7:38 [PATCH 0/5] Simplify code with _scoped() helper functions Zhang Zekun
                   ` (3 preceding siblings ...)
  2024-08-28  7:38 ` [PATCH 4/5] PCI: apple: Use helper function for_each_child_of_node_scoped() Zhang Zekun
@ 2024-08-28  7:38 ` Zhang Zekun
  2024-08-28 12:21   ` Jonathan Cameron
  4 siblings, 1 reply; 13+ messages in thread
From: Zhang Zekun @ 2024-08-28  7:38 UTC (permalink / raw)
  To: songxiaowei, wangbinghui, lpieralisi, kw, manivannan.sadhasivam,
	robh, bhelgaas, linux-pci, ryder.lee, jianjun.wang,
	sergio.paracuellos, angelogioacchino.delregno, matthias.bgg,
	alyssa, maz, thierry.reding
  Cc: zhangzekun11

for_each_child_of_node_scoped() provides a scope-based cleanup
functinality to put the device_node automatically, and we don't need to
call of_node_put() directly.  Let's simplify the code a bit with the use
of these functions.

Signed-off-by: Zhang Zekun <zhangzekun11@huawei.com>
---
 drivers/pci/controller/pci-tegra.c | 41 ++++++++++--------------------
 1 file changed, 14 insertions(+), 27 deletions(-)

diff --git a/drivers/pci/controller/pci-tegra.c b/drivers/pci/controller/pci-tegra.c
index d7517c3976e7..f1214b3374da 100644
--- a/drivers/pci/controller/pci-tegra.c
+++ b/drivers/pci/controller/pci-tegra.c
@@ -2106,14 +2106,14 @@ static int tegra_pcie_get_regulators(struct tegra_pcie *pcie, u32 lane_mask)
 static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
 {
 	struct device *dev = pcie->dev;
-	struct device_node *np = dev->of_node, *port;
+	struct device_node *np = dev->of_node;
 	const struct tegra_pcie_soc *soc = pcie->soc;
 	u32 lanes = 0, mask = 0;
 	unsigned int lane = 0;
 	int err;
 
 	/* parse root ports */
-	for_each_child_of_node(np, port) {
+	for_each_child_of_node_scoped(np, port) {
 		struct tegra_pcie_port *rp;
 		unsigned int index;
 		u32 value;
@@ -2122,15 +2122,14 @@ static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
 		err = of_pci_get_devfn(port);
 		if (err < 0) {
 			dev_err(dev, "failed to parse address: %d\n", err);
-			goto err_node_put;
+			return err;
 		}
 
 		index = PCI_SLOT(err);
 
 		if (index < 1 || index > soc->num_ports) {
 			dev_err(dev, "invalid port number: %d\n", index);
-			err = -EINVAL;
-			goto err_node_put;
+			return -EINVAL;
 		}
 
 		index--;
@@ -2139,13 +2138,12 @@ static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
 		if (err < 0) {
 			dev_err(dev, "failed to parse # of lanes: %d\n",
 				err);
-			goto err_node_put;
+			return err;
 		}
 
 		if (value > 16) {
 			dev_err(dev, "invalid # of lanes: %u\n", value);
-			err = -EINVAL;
-			goto err_node_put;
+			return -EINVAL;
 		}
 
 		lanes |= value << (index << 3);
@@ -2159,15 +2157,13 @@ static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
 		lane += value;
 
 		rp = devm_kzalloc(dev, sizeof(*rp), GFP_KERNEL);
-		if (!rp) {
-			err = -ENOMEM;
-			goto err_node_put;
-		}
+		if (!rp)
+			return -ENOMEM;
 
 		err = of_address_to_resource(port, 0, &rp->regs);
 		if (err < 0) {
 			dev_err(dev, "failed to parse address: %d\n", err);
-			goto err_node_put;
+			return err;
 		}
 
 		INIT_LIST_HEAD(&rp->list);
@@ -2177,16 +2173,12 @@ static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
 		rp->np = port;
 
 		rp->base = devm_pci_remap_cfg_resource(dev, &rp->regs);
-		if (IS_ERR(rp->base)) {
-			err = PTR_ERR(rp->base);
-			goto err_node_put;
-		}
+		if (IS_ERR(rp->base))
+			return PTR_ERR(rp->base);
 
 		label = devm_kasprintf(dev, GFP_KERNEL, "pex-reset-%u", index);
-		if (!label) {
-			err = -ENOMEM;
-			goto err_node_put;
-		}
+		if (!label)
+			return -ENOMEM;
 
 		/*
 		 * Returns -ENOENT if reset-gpios property is not populated
@@ -2204,8 +2196,7 @@ static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
 			} else {
 				dev_err(dev, "failed to get reset GPIO: %ld\n",
 					PTR_ERR(rp->reset_gpio));
-				err = PTR_ERR(rp->reset_gpio);
-				goto err_node_put;
+				return PTR_ERR(rp->reset_gpio);
 			}
 		}
 
@@ -2223,10 +2214,6 @@ static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
 		return err;
 
 	return 0;
-
-err_node_put:
-	of_node_put(port);
-	return err;
 }
 
 /*
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH 3/5] PCI: mt7621: Use helper function for_each_available_child_of_node_scoped()
  2024-08-28  7:38 ` [PATCH 3/5] PCI: mt7621: " Zhang Zekun
@ 2024-08-28  9:18   ` Sergio Paracuellos
  2024-08-28 12:17   ` Jonathan Cameron
  1 sibling, 0 replies; 13+ messages in thread
From: Sergio Paracuellos @ 2024-08-28  9:18 UTC (permalink / raw)
  To: Zhang Zekun
  Cc: songxiaowei, wangbinghui, lpieralisi, kw, manivannan.sadhasivam,
	robh, bhelgaas, linux-pci, ryder.lee, jianjun.wang,
	angelogioacchino.delregno, matthias.bgg, alyssa, maz,
	thierry.reding

Hi Zhang,

On Wed, Aug 28, 2024 at 9:51 AM Zhang Zekun <zhangzekun11@huawei.com> wrote:
>
> for_each_available_child_of_node_scoped() provides a scope-based cleanup
> functinality to put the device_node automatically, and we don't need to

typo: s/functinality/functionality/g

> call of_node_put() directly.  Let's simplify the code a bit with the use
> of these functions.
>
> Signed-off-by: Zhang Zekun <zhangzekun11@huawei.com>
> ---
>  drivers/pci/controller/pcie-mt7621.c | 9 +++------
>  1 file changed, 3 insertions(+), 6 deletions(-)
>

With the typo fixed:
Reviewed-by: Sergio Paracuellos <sergio.paracuellos@gmail.com>

Thanks,
    Sergio Paracuellos

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 1/5] PCI: kirin: Use helper function for_each_available_child_of_node_scoped()
  2024-08-28  7:38 ` [PATCH 1/5] PCI: kirin: Use helper function for_each_available_child_of_node_scoped() Zhang Zekun
@ 2024-08-28 12:11   ` Jonathan Cameron
  2024-08-29  1:20     ` zhangzekun (A)
  0 siblings, 1 reply; 13+ messages in thread
From: Jonathan Cameron @ 2024-08-28 12:11 UTC (permalink / raw)
  To: Zhang Zekun
  Cc: songxiaowei, wangbinghui, lpieralisi, kw, manivannan.sadhasivam,
	robh, bhelgaas, linux-pci, ryder.lee, jianjun.wang,
	sergio.paracuellos, angelogioacchino.delregno, matthias.bgg,
	alyssa, maz, thierry.reding

On Wed, 28 Aug 2024 15:38:21 +0800
Zhang Zekun <zhangzekun11@huawei.com> wrote:

> for_each_available_child_of_node_scoped() provides a scope-based cleanup
> functinality to put the device_node automatically, and we don't need to
> call of_node_put() directly.  Let's simplify the code a bit with the use
> of these functions.
> 
> Signed-off-by: Zhang Zekun <zhangzekun11@huawei.com>
Hi.

Looks good.  A passing comment on another ugly bit of code in his
function that you could tidy up whilst here.

For what you have covered
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

> ---
>  drivers/pci/controller/dwc/pcie-kirin.c | 10 +++-------
>  1 file changed, 3 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/pci/controller/dwc/pcie-kirin.c b/drivers/pci/controller/dwc/pcie-kirin.c
> index 0a29136491b8..e9bda1746ca5 100644
> --- a/drivers/pci/controller/dwc/pcie-kirin.c
> +++ b/drivers/pci/controller/dwc/pcie-kirin.c
> @@ -452,7 +452,7 @@ static long kirin_pcie_get_resource(struct kirin_pcie *kirin_pcie,
>  				    struct platform_device *pdev)
>  {
>  	struct device *dev = &pdev->dev;
> -	struct device_node *child, *node = dev->of_node;
> +	struct device_node *node = dev->of_node;
>  	void __iomem *apb_base;
>  	int ret;
>  
> @@ -477,17 +477,13 @@ static long kirin_pcie_get_resource(struct kirin_pcie *kirin_pcie,
>  		return ret;
>  
Looking at this function I don't suppose you fancy also tidying up the oddity of:
	kirin_pcie->gpio_id_dwc_perst = of_get_named_gpio(dev->of_node,
							  "reset-gpios", 0);
	if (kirin_pcie->gpio_id_dwc_perst == -EPROBE_DEFER) {
		return -EPROBE_DEFER;
	} else if (!gpio_is_valid(kirin_pcie->gpio_id_dwc_perst)) {
		dev_err(dev, "unable to get a valid gpio pin\n");
		return -ENODEV;
	}

Where that else adds nothing and it could just be
	ret = of_get_named_gpio(dev->of_node, "reset-gpios", 0);
	if (ret < 0)
		return dev_err_probe(dev, ret,
				     "unable to get a valid gpio pin\n2);

	kirin_pcie->gpio_id_dwc_perst = ret;

or even update the gpio handling in general to use non deprecated
functions.

>  	/* Parse OF children */
> -	for_each_available_child_of_node(node, child) {
> +	for_each_available_child_of_node_scoped(node, child) {
>  		ret = kirin_pcie_parse_port(kirin_pcie, pdev, child);
>  		if (ret)
> -			goto put_node;
> +			return ret;
>  	}
>  
>  	return 0;
> -
> -put_node:
> -	of_node_put(child);
> -	return ret;
>  }
>  
>  static void kirin_pcie_sideband_dbi_w_mode(struct kirin_pcie *kirin_pcie,


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 2/5] PCI: mediatek: Use helper function for_each_available_child_of_node_scoped()
  2024-08-28  7:38 ` [PATCH 2/5] PCI: mediatek: " Zhang Zekun
@ 2024-08-28 12:16   ` Jonathan Cameron
  0 siblings, 0 replies; 13+ messages in thread
From: Jonathan Cameron @ 2024-08-28 12:16 UTC (permalink / raw)
  To: Zhang Zekun
  Cc: songxiaowei, wangbinghui, lpieralisi, kw, manivannan.sadhasivam,
	robh, bhelgaas, linux-pci, ryder.lee, jianjun.wang,
	sergio.paracuellos, angelogioacchino.delregno, matthias.bgg,
	alyssa, maz, thierry.reding

On Wed, 28 Aug 2024 15:38:22 +0800
Zhang Zekun <zhangzekun11@huawei.com> wrote:

> for_each_available_child_of_node_scoped() provides a scope-based cleanup
> functinality to put the device_node automatically, and we don't need to
> call of_node_put() directly.  Let's simplify the code a bit with the use
> of these functions.
> 
> Signed-off-by: Zhang Zekun <zhangzekun11@huawei.com>
A nice to have inline.

Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

> ---
>  drivers/pci/controller/pcie-mediatek.c | 11 ++++-------
>  1 file changed, 4 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/pci/controller/pcie-mediatek.c b/drivers/pci/controller/pcie-mediatek.c
> index 9be3cebd862e..d14e8d151c08 100644
> --- a/drivers/pci/controller/pcie-mediatek.c
> +++ b/drivers/pci/controller/pcie-mediatek.c
> @@ -1042,24 +1042,24 @@ static int mtk_pcie_subsys_powerup(struct mtk_pcie *pcie)
>  static int mtk_pcie_setup(struct mtk_pcie *pcie)
>  {
>  	struct device *dev = pcie->dev;
> -	struct device_node *node = dev->of_node, *child;
> +	struct device_node *node = dev->of_node;
>  	struct mtk_pcie_port *port, *tmp;
>  	int err, slot;
>  
>  	slot = of_get_pci_domain_nr(dev->of_node);
>  	if (slot < 0) {
> -		for_each_available_child_of_node(node, child) {
> +		for_each_available_child_of_node_scoped(node, child) {
>  			err = of_pci_get_devfn(child);
>  			if (err < 0) {
>  				dev_err(dev, "failed to get devfn: %d\n", err);
> -				goto error_put_node;
> +				return err;

Could do			return dev_err_probe(dev, err, "failed to get devfn\n");

>  			}
>  
>  			slot = PCI_SLOT(err);
>  
>  			err = mtk_pcie_parse_port(pcie, child, slot);
>  			if (err)
> -				goto error_put_node;
> +				return err;
>  		}
>  	} else {
>  		err = mtk_pcie_parse_port(pcie, node, slot);
> @@ -1080,9 +1080,6 @@ static int mtk_pcie_setup(struct mtk_pcie *pcie)
>  		mtk_pcie_subsys_powerdown(pcie);
>  
>  	return 0;
> -error_put_node:
> -	of_node_put(child);
> -	return err;
>  }
>  
>  static int mtk_pcie_probe(struct platform_device *pdev)


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 3/5] PCI: mt7621: Use helper function for_each_available_child_of_node_scoped()
  2024-08-28  7:38 ` [PATCH 3/5] PCI: mt7621: " Zhang Zekun
  2024-08-28  9:18   ` Sergio Paracuellos
@ 2024-08-28 12:17   ` Jonathan Cameron
  1 sibling, 0 replies; 13+ messages in thread
From: Jonathan Cameron @ 2024-08-28 12:17 UTC (permalink / raw)
  To: Zhang Zekun
  Cc: songxiaowei, wangbinghui, lpieralisi, kw, manivannan.sadhasivam,
	robh, bhelgaas, linux-pci, ryder.lee, jianjun.wang,
	sergio.paracuellos, angelogioacchino.delregno, matthias.bgg,
	alyssa, maz, thierry.reding

On Wed, 28 Aug 2024 15:38:23 +0800
Zhang Zekun <zhangzekun11@huawei.com> wrote:

> for_each_available_child_of_node_scoped() provides a scope-based cleanup
> functinality to put the device_node automatically, and we don't need to
> call of_node_put() directly.  Let's simplify the code a bit with the use
> of these functions.
> 
> Signed-off-by: Zhang Zekun <zhangzekun11@huawei.com>
Another case in here where return dev_err_probe()
would be nice, but looks good to me either way.

Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> ---
>  drivers/pci/controller/pcie-mt7621.c | 9 +++------
>  1 file changed, 3 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/pci/controller/pcie-mt7621.c b/drivers/pci/controller/pcie-mt7621.c
> index 9b4754a45515..16c3df1c49a1 100644
> --- a/drivers/pci/controller/pcie-mt7621.c
> +++ b/drivers/pci/controller/pcie-mt7621.c
> @@ -258,19 +258,18 @@ static int mt7621_pcie_parse_dt(struct mt7621_pcie *pcie)
>  {
>  	struct device *dev = pcie->dev;
>  	struct platform_device *pdev = to_platform_device(dev);
> -	struct device_node *node = dev->of_node, *child;
> +	struct device_node *node = dev->of_node;
>  	int err;
>  
>  	pcie->base = devm_platform_ioremap_resource(pdev, 0);
>  	if (IS_ERR(pcie->base))
>  		return PTR_ERR(pcie->base);
>  
> -	for_each_available_child_of_node(node, child) {
> +	for_each_available_child_of_node_scoped(node, child) {
>  		int slot;
>  
>  		err = of_pci_get_devfn(child);
>  		if (err < 0) {
> -			of_node_put(child);
>  			dev_err(dev, "failed to parse devfn: %d\n", err);
>  			return err;
>  		}
> @@ -278,10 +277,8 @@ static int mt7621_pcie_parse_dt(struct mt7621_pcie *pcie)
>  		slot = PCI_SLOT(err);
>  
>  		err = mt7621_pcie_parse_port(pcie, child, slot);
> -		if (err) {
> -			of_node_put(child);
> +		if (err)
>  			return err;
> -		}
>  	}
>  
>  	return 0;


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 4/5] PCI: apple: Use helper function for_each_child_of_node_scoped()
  2024-08-28  7:38 ` [PATCH 4/5] PCI: apple: Use helper function for_each_child_of_node_scoped() Zhang Zekun
@ 2024-08-28 12:19   ` Jonathan Cameron
  0 siblings, 0 replies; 13+ messages in thread
From: Jonathan Cameron @ 2024-08-28 12:19 UTC (permalink / raw)
  To: Zhang Zekun
  Cc: songxiaowei, wangbinghui, lpieralisi, kw, manivannan.sadhasivam,
	robh, bhelgaas, linux-pci, ryder.lee, jianjun.wang,
	sergio.paracuellos, angelogioacchino.delregno, matthias.bgg,
	alyssa, maz, thierry.reding

On Wed, 28 Aug 2024 15:38:24 +0800
Zhang Zekun <zhangzekun11@huawei.com> wrote:

> for_each_child_of_node_scoped() provides a scope-based cleanup
> functinality to put the device_node automatically, and we don't need to
> call of_node_put() directly.  Let's simplify the code a bit with the
> use of these functions.
> 
> Signed-off-by: Zhang Zekun <zhangzekun11@huawei.com>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 5/5] PCI: tegra: Use helper function for_each_child_of_node_scoped()
  2024-08-28  7:38 ` [PATCH 5/5] PCI: tegra: " Zhang Zekun
@ 2024-08-28 12:21   ` Jonathan Cameron
  0 siblings, 0 replies; 13+ messages in thread
From: Jonathan Cameron @ 2024-08-28 12:21 UTC (permalink / raw)
  To: Zhang Zekun
  Cc: songxiaowei, wangbinghui, lpieralisi, kw, manivannan.sadhasivam,
	robh, bhelgaas, linux-pci, ryder.lee, jianjun.wang,
	sergio.paracuellos, angelogioacchino.delregno, matthias.bgg,
	alyssa, maz, thierry.reding

On Wed, 28 Aug 2024 15:38:25 +0800
Zhang Zekun <zhangzekun11@huawei.com> wrote:

> for_each_child_of_node_scoped() provides a scope-based cleanup
> functinality to put the device_node automatically, and we don't need to
> call of_node_put() directly.  Let's simplify the code a bit with the use
> of these functions.
> 
> Signed-off-by: Zhang Zekun <zhangzekun11@huawei.com>
This one will heavily benefit from dev_err_probe()
So I'd strongly suggest using that throughout this function.

Jonathan

> ---
>  drivers/pci/controller/pci-tegra.c | 41 ++++++++++--------------------
>  1 file changed, 14 insertions(+), 27 deletions(-)
> 
> diff --git a/drivers/pci/controller/pci-tegra.c b/drivers/pci/controller/pci-tegra.c
> index d7517c3976e7..f1214b3374da 100644
> --- a/drivers/pci/controller/pci-tegra.c
> +++ b/drivers/pci/controller/pci-tegra.c
> @@ -2106,14 +2106,14 @@ static int tegra_pcie_get_regulators(struct tegra_pcie *pcie, u32 lane_mask)
>  static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
>  {
>  	struct device *dev = pcie->dev;
> -	struct device_node *np = dev->of_node, *port;
> +	struct device_node *np = dev->of_node;
>  	const struct tegra_pcie_soc *soc = pcie->soc;
>  	u32 lanes = 0, mask = 0;
>  	unsigned int lane = 0;
>  	int err;
>  
>  	/* parse root ports */
> -	for_each_child_of_node(np, port) {
> +	for_each_child_of_node_scoped(np, port) {
>  		struct tegra_pcie_port *rp;
>  		unsigned int index;
>  		u32 value;
> @@ -2122,15 +2122,14 @@ static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
>  		err = of_pci_get_devfn(port);
>  		if (err < 0) {
>  			dev_err(dev, "failed to parse address: %d\n", err);
> -			goto err_node_put;
> +			return err;
>  		}
>  
>  		index = PCI_SLOT(err);
>  
>  		if (index < 1 || index > soc->num_ports) {
>  			dev_err(dev, "invalid port number: %d\n", index);
> -			err = -EINVAL;
> -			goto err_node_put;
> +			return -EINVAL;
>  		}
>  
>  		index--;
> @@ -2139,13 +2138,12 @@ static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
>  		if (err < 0) {
>  			dev_err(dev, "failed to parse # of lanes: %d\n",
>  				err);
> -			goto err_node_put;
> +			return err;
>  		}
>  
>  		if (value > 16) {
>  			dev_err(dev, "invalid # of lanes: %u\n", value);
> -			err = -EINVAL;
> -			goto err_node_put;
> +			return -EINVAL;
>  		}
>  
>  		lanes |= value << (index << 3);
> @@ -2159,15 +2157,13 @@ static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
>  		lane += value;
>  
>  		rp = devm_kzalloc(dev, sizeof(*rp), GFP_KERNEL);
> -		if (!rp) {
> -			err = -ENOMEM;
> -			goto err_node_put;
> -		}
> +		if (!rp)
> +			return -ENOMEM;
>  
>  		err = of_address_to_resource(port, 0, &rp->regs);
>  		if (err < 0) {
>  			dev_err(dev, "failed to parse address: %d\n", err);
> -			goto err_node_put;
> +			return err;
>  		}
>  
>  		INIT_LIST_HEAD(&rp->list);
> @@ -2177,16 +2173,12 @@ static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
>  		rp->np = port;
>  
>  		rp->base = devm_pci_remap_cfg_resource(dev, &rp->regs);
> -		if (IS_ERR(rp->base)) {
> -			err = PTR_ERR(rp->base);
> -			goto err_node_put;
> -		}
> +		if (IS_ERR(rp->base))
> +			return PTR_ERR(rp->base);
>  
>  		label = devm_kasprintf(dev, GFP_KERNEL, "pex-reset-%u", index);
> -		if (!label) {
> -			err = -ENOMEM;
> -			goto err_node_put;
> -		}
> +		if (!label)
> +			return -ENOMEM;
>  
>  		/*
>  		 * Returns -ENOENT if reset-gpios property is not populated
> @@ -2204,8 +2196,7 @@ static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
>  			} else {
>  				dev_err(dev, "failed to get reset GPIO: %ld\n",
>  					PTR_ERR(rp->reset_gpio));
> -				err = PTR_ERR(rp->reset_gpio);
> -				goto err_node_put;
> +				return PTR_ERR(rp->reset_gpio);
>  			}
>  		}
>  
> @@ -2223,10 +2214,6 @@ static int tegra_pcie_parse_dt(struct tegra_pcie *pcie)
>  		return err;
>  
>  	return 0;
> -
> -err_node_put:
> -	of_node_put(port);
> -	return err;
>  }
>  
>  /*


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 1/5] PCI: kirin: Use helper function for_each_available_child_of_node_scoped()
  2024-08-28 12:11   ` Jonathan Cameron
@ 2024-08-29  1:20     ` zhangzekun (A)
  0 siblings, 0 replies; 13+ messages in thread
From: zhangzekun (A) @ 2024-08-29  1:20 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: songxiaowei, wangbinghui, lpieralisi, kw, manivannan.sadhasivam,
	robh, bhelgaas, linux-pci, ryder.lee, jianjun.wang,
	sergio.paracuellos, angelogioacchino.delregno, matthias.bgg,
	alyssa, maz, thierry.reding



在 2024/8/28 20:11, Jonathan Cameron 写道:
> On Wed, 28 Aug 2024 15:38:21 +0800
> Zhang Zekun <zhangzekun11@huawei.com> wrote:
> 
>> for_each_available_child_of_node_scoped() provides a scope-based cleanup
>> functinality to put the device_node automatically, and we don't need to
>> call of_node_put() directly.  Let's simplify the code a bit with the use
>> of these functions.
>>
>> Signed-off-by: Zhang Zekun <zhangzekun11@huawei.com>
> Hi.
> 
> Looks good.  A passing comment on another ugly bit of code in his
> function that you could tidy up whilst here.
> 
> For what you have covered
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> 
>> ---
>>   drivers/pci/controller/dwc/pcie-kirin.c | 10 +++-------
>>   1 file changed, 3 insertions(+), 7 deletions(-)
>>
>> diff --git a/drivers/pci/controller/dwc/pcie-kirin.c b/drivers/pci/controller/dwc/pcie-kirin.c
>> index 0a29136491b8..e9bda1746ca5 100644
>> --- a/drivers/pci/controller/dwc/pcie-kirin.c
>> +++ b/drivers/pci/controller/dwc/pcie-kirin.c
>> @@ -452,7 +452,7 @@ static long kirin_pcie_get_resource(struct kirin_pcie *kirin_pcie,
>>   				    struct platform_device *pdev)
>>   {
>>   	struct device *dev = &pdev->dev;
>> -	struct device_node *child, *node = dev->of_node;
>> +	struct device_node *node = dev->of_node;
>>   	void __iomem *apb_base;
>>   	int ret;
>>   
>> @@ -477,17 +477,13 @@ static long kirin_pcie_get_resource(struct kirin_pcie *kirin_pcie,
>>   		return ret;
>>   
> Looking at this function I don't suppose you fancy also tidying up the oddity of:
> 	kirin_pcie->gpio_id_dwc_perst = of_get_named_gpio(dev->of_node,
> 							  "reset-gpios", 0);
> 	if (kirin_pcie->gpio_id_dwc_perst == -EPROBE_DEFER) {
> 		return -EPROBE_DEFER;
> 	} else if (!gpio_is_valid(kirin_pcie->gpio_id_dwc_perst)) {
> 		dev_err(dev, "unable to get a valid gpio pin\n");
> 		return -ENODEV;
> 	}
> 
> Where that else adds nothing and it could just be
> 	ret = of_get_named_gpio(dev->of_node, "reset-gpios", 0);
> 	if (ret < 0)
> 		return dev_err_probe(dev, ret,
> 				     "unable to get a valid gpio pin\n2);
> 
> 	kirin_pcie->gpio_id_dwc_perst = ret;
> 
> or even update the gpio handling in general to use non deprecated
> functions.
> 

Hi, Jonathan,

Thanks for your review. I will send v2 to tidy up together.

Beset Regards,
Zekun

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2024-08-29  1:20 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-28  7:38 [PATCH 0/5] Simplify code with _scoped() helper functions Zhang Zekun
2024-08-28  7:38 ` [PATCH 1/5] PCI: kirin: Use helper function for_each_available_child_of_node_scoped() Zhang Zekun
2024-08-28 12:11   ` Jonathan Cameron
2024-08-29  1:20     ` zhangzekun (A)
2024-08-28  7:38 ` [PATCH 2/5] PCI: mediatek: " Zhang Zekun
2024-08-28 12:16   ` Jonathan Cameron
2024-08-28  7:38 ` [PATCH 3/5] PCI: mt7621: " Zhang Zekun
2024-08-28  9:18   ` Sergio Paracuellos
2024-08-28 12:17   ` Jonathan Cameron
2024-08-28  7:38 ` [PATCH 4/5] PCI: apple: Use helper function for_each_child_of_node_scoped() Zhang Zekun
2024-08-28 12:19   ` Jonathan Cameron
2024-08-28  7:38 ` [PATCH 5/5] PCI: tegra: " Zhang Zekun
2024-08-28 12:21   ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox