Re: [PATCH v4 1/4] PCI: Introduce pci_dev_for_each_resource()

2023-03-14 Thread Andy Shevchenko
On Sat, Mar 11, 2023 at 04:54:32PM +0300, Dan Carpenter wrote:

> 059b4a086017fb Mika Westerberg 2023-03-10  246
> unsigned long type = resource_type(r);
> 999ed65ad12e37 Rene Herman 2008-07-25  247  
> 059b4a086017fb Mika Westerberg 2023-03-10 @248if 
> (type != IORESOURCE_IO || type != IORESOURCE_MEM ||
>   
> ^^
> This || needs to be &&.  This loop will always hit the continue path
> without doing anything.
> 
> 059b4a086017fb Mika Westerberg 2023-03-10  249
> resource_size(r) == 0)
> 0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  250
> continue;

Thanks, I'll fix in v5.

-- 
With Best Regards,
Andy Shevchenko





Re: [PATCH v4 1/4] PCI: Introduce pci_dev_for_each_resource()

2023-03-14 Thread Andy Shevchenko
On Fri, Mar 10, 2023 at 03:15:38PM -0700, Keith Busch wrote:
> On Fri, Mar 10, 2023 at 07:14:13PM +0200, Andy Shevchenko wrote:

...

> > +#define pci_dev_for_each_resource_p(dev, res)  
> > \
> > +   __pci_dev_for_each_resource(dev, res, i, unsigned int)
> 
> It looks dangerous to have a macro declare a variable when starting a new
> scope. How do you know the name 'i' won't clash with something defined above?

I'll rename. Thank you.

-- 
With Best Regards,
Andy Shevchenko





Re: [PATCH v4 1/4] PCI: Introduce pci_dev_for_each_resource()

2023-03-11 Thread Dan Carpenter
Hi Andy,

https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:
https://github.com/intel-lab-lkp/linux/commits/Andy-Shevchenko/PCI-Introduce-pci_dev_for_each_resource/20230311-011642
base:   https://git.kernel.org/pub/scm/linux/kernel/git/pci/pci.git next
patch link:
https://lore.kernel.org/r/20230310171416.23356-2-andriy.shevchenko%40linux.intel.com
patch subject: [PATCH v4 1/4] PCI: Introduce pci_dev_for_each_resource()
config: x86_64-randconfig-m001 
(https://download.01.org/0day-ci/archive/20230311/202303112149.xd47qkoy-...@intel.com/config)
compiler: gcc-11 (Debian 11.3.0-8) 11.3.0

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot 
| Reported-by: Dan Carpenter 
| Link: https://lore.kernel.org/r/202303112149.xd47qkoy-...@intel.com/

smatch warnings:
drivers/pnp/quirks.c:248 quirk_system_pci_resources() warn: was && intended 
here instead of ||?

vim +248 drivers/pnp/quirks.c

0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  229  static void 
quirk_system_pci_resources(struct pnp_dev *dev)
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  230  {
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  231  struct pci_dev *pdev = 
NULL;
059b4a086017fb Mika Westerberg 2023-03-10  232  struct resource *res, 
*r;
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  233  int i, j;
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  234  
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  235  /*
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  236   * Some BIOSes have PNP 
motherboard devices with resources that
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  237   * partially overlap 
PCI BARs.  The PNP system driver claims these
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  238   * motherboard 
resources, which prevents the normal PCI driver from
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  239   * requesting them 
later.
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  240   *
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  241   * This patch disables 
the PNP resources that conflict with PCI BARs
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  242   * so they won't be 
claimed by the PNP system driver.
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  243   */
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  244  for_each_pci_dev(pdev) {
059b4a086017fb Mika Westerberg 2023-03-10  245  
pci_dev_for_each_resource(pdev, r, i) {
059b4a086017fb Mika Westerberg 2023-03-10  246  
unsigned long type = resource_type(r);
999ed65ad12e37 Rene Herman 2008-07-25  247  
059b4a086017fb Mika Westerberg 2023-03-10 @248  if 
(type != IORESOURCE_IO || type != IORESOURCE_MEM ||

  ^^
This || needs to be &&.  This loop will always hit the continue path
without doing anything.

059b4a086017fb Mika Westerberg 2023-03-10  249  
resource_size(r) == 0)
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  250  
continue;
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  251  
059b4a086017fb Mika Westerberg 2023-03-10  252  if 
(r->flags & IORESOURCE_UNSET)
f7834c092c4299 Bjorn Helgaas   2015-03-03  253  
continue;
f7834c092c4299 Bjorn Helgaas   2015-03-03  254  
95ab3669f78306 Bjorn Helgaas   2008-04-28  255  for (j 
= 0;
999ed65ad12e37 Rene Herman 2008-07-25  256   
(res = pnp_get_resource(dev, type, j)); j++) {
aee3ad815dd291 Bjorn Helgaas   2008-06-27  257  
if (res->start == 0 && res->end == 0)
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  258  
continue;
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  259  
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  260  
/*
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  261  
 * If the PNP region doesn't overlap the PCI
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  262  
 * region at all, there's no problem.
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  263  
 */
059b4a086017fb Mika Westerberg 2023-03-10  264  
if (!resource_overlaps(res, r))
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  265  
continue;
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  266  
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  267  
/*
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  268  
 * If the PNP region completely encloses (or is
0509ad5e1a7d92 Bjorn Helgaas   2008-03-11  269  
 * at least as large as) the PCI region, t

Re: [PATCH v4 1/4] PCI: Introduce pci_dev_for_each_resource()

2023-03-10 Thread Keith Busch
On Fri, Mar 10, 2023 at 07:14:13PM +0200, Andy Shevchenko wrote:
> +#define __pci_dev_for_each_resource(dev, res, __i, vartype)  \
> + for (vartype __i = 0;   \
> + res = &(dev)->resource[__i], __i < PCI_NUM_RESOURCES;   \
> + __i++)

...

> +#define pci_dev_for_each_resource_p(dev, res)
> \
> + __pci_dev_for_each_resource(dev, res, i, unsigned int)

It looks dangerous to have a macro declare a variable when starting a new
scope. How do you know the name 'i' won't clash with something defined above?



Re: [PATCH v4 1/4] PCI: Introduce pci_dev_for_each_resource()

2023-03-10 Thread kernel test robot
Hi Andy,

I love your patch! Yet something to improve:

[auto build test ERROR on pci/next]
[also build test ERROR on pci/for-linus powerpc/next powerpc/fixes linus/master 
v6.3-rc1 next-20230310]
[cannot apply to soc/for-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:
https://github.com/intel-lab-lkp/linux/commits/Andy-Shevchenko/PCI-Introduce-pci_dev_for_each_resource/20230311-011642
base:   https://git.kernel.org/pub/scm/linux/kernel/git/pci/pci.git next
patch link:
https://lore.kernel.org/r/20230310171416.23356-2-andriy.shevchenko%40linux.intel.com
patch subject: [PATCH v4 1/4] PCI: Introduce pci_dev_for_each_resource()
config: m68k-allyesconfig 
(https://download.01.org/0day-ci/archive/20230311/202303110550.blo6p5ds-...@intel.com/config)
compiler: m68k-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
wget 
https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O 
~/bin/make.cross
chmod +x ~/bin/make.cross
# 
https://github.com/intel-lab-lkp/linux/commit/059b4a086017fbf2baacdbe0cc454f569f618ffd
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review 
Andy-Shevchenko/PCI-Introduce-pci_dev_for_each_resource/20230311-011642
git checkout 059b4a086017fbf2baacdbe0cc454f569f618ffd
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 
O=build_dir ARCH=m68k olddefconfig
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 
O=build_dir ARCH=m68k SHELL=/bin/bash drivers/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot 
| Link: 
https://lore.kernel.org/oe-kbuild-all/202303110550.blo6p5ds-...@intel.com/

All errors (new ones prefixed by >>):

   drivers/pnp/quirks.c: In function 'quirk_system_pci_resources':
>> drivers/pnp/quirks.c:245:17: error: implicit declaration of function 
>> 'pci_dev_for_each_resource' [-Werror=implicit-function-declaration]
 245 | pci_dev_for_each_resource(pdev, r, i) {
 | ^
>> drivers/pnp/quirks.c:245:54: error: expected ';' before '{' token
 245 | pci_dev_for_each_resource(pdev, r, i) {
 |  ^~
 |  ;
   drivers/pnp/quirks.c:233:16: warning: unused variable 'j' [-Wunused-variable]
 233 | int i, j;
 |^
   drivers/pnp/quirks.c:232:26: warning: unused variable 'res' 
[-Wunused-variable]
 232 | struct resource *res, *r;
 |  ^~~
   cc1: some warnings being treated as errors


vim +/pci_dev_for_each_resource +245 drivers/pnp/quirks.c

   228  
   229  static void quirk_system_pci_resources(struct pnp_dev *dev)
   230  {
   231  struct pci_dev *pdev = NULL;
   232  struct resource *res, *r;
   233  int i, j;
   234  
   235  /*
   236   * Some BIOSes have PNP motherboard devices with resources that
   237   * partially overlap PCI BARs.  The PNP system driver claims 
these
   238   * motherboard resources, which prevents the normal PCI driver 
from
   239   * requesting them later.
   240   *
   241   * This patch disables the PNP resources that conflict with PCI 
BARs
   242   * so they won't be claimed by the PNP system driver.
   243   */
   244  for_each_pci_dev(pdev) {
 > 245  pci_dev_for_each_resource(pdev, r, i) {
   246  unsigned long type = resource_type(r);
   247  
   248  if (type != IORESOURCE_IO || type != 
IORESOURCE_MEM ||
   249  resource_size(r) == 0)
   250  continue;
   251  
   252  if (r->flags & IORESOURCE_UNSET)
   253  continue;
   254  
   255  for (j = 0;
   256   (res = pnp_get_resource(dev, type, j)); 
j++) {
   257  if (res->start == 0 && res->end == 0)
   258  continue;
   259  
   260  /*
   261   * If the PNP region doesn't overlap 
the PCI
   262   * region at all, there's no problem.
   263   */
   264  if (!re

[PATCH v4 1/4] PCI: Introduce pci_dev_for_each_resource()

2023-03-10 Thread Andy Shevchenko
From: Mika Westerberg 

Instead of open-coding it everywhere introduce a tiny helper that can be
used to iterate over each resource of a PCI device, and convert the most
obvious users into it.

While at it drop doubled empty line before pdev_sort_resources().

No functional changes intended.

Suggested-by: Andy Shevchenko 
Signed-off-by: Mika Westerberg 
Signed-off-by: Andy Shevchenko 
Reviewed-by: Krzysztof WilczyƄski 
---
 .clang-format |  2 ++
 arch/alpha/kernel/pci.c   |  5 ++--
 arch/arm/kernel/bios32.c  | 16 ++---
 arch/arm/mach-dove/pcie.c | 10 
 arch/arm/mach-mv78xx0/pcie.c  | 10 
 arch/arm/mach-orion5x/pci.c   | 10 
 arch/mips/pci/ops-bcm63xx.c   |  8 +++
 arch/mips/pci/pci-legacy.c|  3 +--
 arch/powerpc/kernel/pci-common.c  | 21 
 arch/powerpc/platforms/4xx/pci.c  |  8 +++
 arch/powerpc/platforms/52xx/mpc52xx_pci.c |  4 ++--
 arch/powerpc/platforms/pseries/pci.c  | 16 ++---
 arch/sh/drivers/pci/pcie-sh7786.c | 10 
 arch/sparc/kernel/leon_pci.c  |  5 ++--
 arch/sparc/kernel/pci.c   | 10 
 arch/sparc/kernel/pcic.c  |  5 ++--
 drivers/pci/remove.c  |  5 ++--
 drivers/pci/setup-bus.c   | 27 -
 drivers/pci/setup-res.c   |  4 +---
 drivers/pci/vgaarb.c  | 17 -
 drivers/pci/xen-pcifront.c|  4 +---
 drivers/pnp/quirks.c  | 29 ---
 include/linux/pci.h   | 12 ++
 23 files changed, 110 insertions(+), 131 deletions(-)

diff --git a/.clang-format b/.clang-format
index d988e9fa9b26..266abb843654 100644
--- a/.clang-format
+++ b/.clang-format
@@ -520,6 +520,8 @@ ForEachMacros:
   - 'of_property_for_each_string'
   - 'of_property_for_each_u32'
   - 'pci_bus_for_each_resource'
+  - 'pci_dev_for_each_resource'
+  - 'pci_dev_for_each_resource_p'
   - 'pci_doe_for_each_off'
   - 'pcl_for_each_chunk'
   - 'pcl_for_each_segment'
diff --git a/arch/alpha/kernel/pci.c b/arch/alpha/kernel/pci.c
index 64fbfb0763b2..4458eb7f44f0 100644
--- a/arch/alpha/kernel/pci.c
+++ b/arch/alpha/kernel/pci.c
@@ -288,11 +288,10 @@ pcibios_claim_one_bus(struct pci_bus *b)
struct pci_bus *child_bus;
 
list_for_each_entry(dev, &b->devices, bus_list) {
+   struct resource *r;
int i;
 
-   for (i = 0; i < PCI_NUM_RESOURCES; i++) {
-   struct resource *r = &dev->resource[i];
-
+   pci_dev_for_each_resource(dev, r, i) {
if (r->parent || !r->start || !r->flags)
continue;
if (pci_has_flag(PCI_PROBE_ONLY) ||
diff --git a/arch/arm/kernel/bios32.c b/arch/arm/kernel/bios32.c
index e7ef2b5bea9c..5254734b23e6 100644
--- a/arch/arm/kernel/bios32.c
+++ b/arch/arm/kernel/bios32.c
@@ -142,15 +142,15 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_WINBOND2, 
PCI_DEVICE_ID_WINBOND2_89C940F,
  */
 static void pci_fixup_dec21285(struct pci_dev *dev)
 {
-   int i;
-
if (dev->devfn == 0) {
+   struct resource *r;
+
dev->class &= 0xff;
dev->class |= PCI_CLASS_BRIDGE_HOST << 8;
-   for (i = 0; i < PCI_NUM_RESOURCES; i++) {
-   dev->resource[i].start = 0;
-   dev->resource[i].end   = 0;
-   dev->resource[i].flags = 0;
+   pci_dev_for_each_resource_p(dev, r) {
+   r->start = 0;
+   r->end = 0;
+   r->flags = 0;
}
}
 }
@@ -162,13 +162,11 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_DEC, 
PCI_DEVICE_ID_DEC_21285, pci_fixup_d
 static void pci_fixup_ide_bases(struct pci_dev *dev)
 {
struct resource *r;
-   int i;
 
if ((dev->class >> 8) != PCI_CLASS_STORAGE_IDE)
return;
 
-   for (i = 0; i < PCI_NUM_RESOURCES; i++) {
-   r = dev->resource + i;
+   pci_dev_for_each_resource_p(dev, r) {
if ((r->start & ~0x80) == 0x374) {
r->start |= 2;
r->end = r->start;
diff --git a/arch/arm/mach-dove/pcie.c b/arch/arm/mach-dove/pcie.c
index 754ca381f600..58cecd79a204 100644
--- a/arch/arm/mach-dove/pcie.c
+++ b/arch/arm/mach-dove/pcie.c
@@ -142,14 +142,14 @@ static struct pci_ops pcie_ops = {
 static void rc_pci_fixup(struct pci_dev *dev)
 {
if (dev->bus->parent == NULL && dev->devfn == 0) {
-   int i;
+   struct resource *r;
 
dev->class &= 0xff;
dev->class |= PCI_CLASS_BRIDGE_HOST << 8;
-   for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
-