public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: "Michał Winiarski" <michal.winiarski@intel.com>
Cc: linux-pci@vger.kernel.org, intel-xe@lists.freedesktop.org,
	dri-devel@lists.freedesktop.org,
	LKML <linux-kernel@vger.kernel.org>,
	"Bjorn Helgaas" <bhelgaas@google.com>,
	"Christian König" <christian.koenig@amd.com>,
	"Krzysztof Wilczyński" <kw@linux.com>,
	"Rodrigo Vivi" <rodrigo.vivi@intel.com>,
	"Michal Wajdeczko" <michal.wajdeczko@intel.com>,
	"Lucas De Marchi" <lucas.demarchi@intel.com>,
	"Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
	"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
	"Maxime Ripard" <mripard@kernel.org>,
	"Thomas Zimmermann" <tzimmermann@suse.de>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Matt Roper" <matthew.d.roper@intel.com>
Subject: Re: [PATCH v4 3/7] PCI: Add a helper to convert between standard and IOV resources
Date: Wed, 6 Nov 2024 16:22:11 +0200 (EET)	[thread overview]
Message-ID: <10b4f173-619a-9913-99de-5d08b3fc854c@linux.intel.com> (raw)
In-Reply-To: <20241025215038.3125626-4-michal.winiarski@intel.com>

[-- Attachment #1: Type: text/plain, Size: 5780 bytes --]

On Fri, 25 Oct 2024, Michał Winiarski wrote:

> There are multiple places where conversions between IOV resources and
> standard resources are done.
> 
> Extract the logic to pci_resource_to_iov() and pci_resource_from_iov()
> helpers.
> 
> Suggested-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
> Signed-off-by: Michał Winiarski <michal.winiarski@intel.com>
> ---
>  drivers/pci/iov.c       | 20 ++++++++++----------
>  drivers/pci/pci.h       | 18 ++++++++++++++++++
>  drivers/pci/setup-bus.c |  2 +-
>  3 files changed, 29 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
> index 6bdc9950b9787..eedc1df56c49e 100644
> --- a/drivers/pci/iov.c
> +++ b/drivers/pci/iov.c
> @@ -151,7 +151,7 @@ resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
>  	if (!dev->is_physfn)
>  		return 0;
>  
> -	return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
> +	return dev->sriov->barsz[pci_resource_from_iov(resno)];
>  }
>  
>  static void pci_read_vf_config_common(struct pci_dev *virtfn)
> @@ -322,12 +322,12 @@ int pci_iov_add_virtfn(struct pci_dev *dev, int id)
>  	virtfn->multifunction = 0;
>  
>  	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
> -		res = &dev->resource[i + PCI_IOV_RESOURCES];
> +		res = &dev->resource[pci_resource_to_iov(i)];
>  		if (!res->parent)
>  			continue;
>  		virtfn->resource[i].name = pci_name(virtfn);
>  		virtfn->resource[i].flags = res->flags;
> -		size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
> +		size = pci_iov_resource_size(dev, pci_resource_to_iov(i));
>  		virtfn->resource[i].start = res->start + size * id;
>  		virtfn->resource[i].end = virtfn->resource[i].start + size - 1;
>  		rc = request_resource(res, &virtfn->resource[i]);
> @@ -624,8 +624,8 @@ static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
>  
>  	nres = 0;
>  	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
> -		bars |= (1 << (i + PCI_IOV_RESOURCES));
> -		res = &dev->resource[i + PCI_IOV_RESOURCES];
> +		bars |= (1 << pci_resource_to_iov(i));
> +		res = &dev->resource[pci_resource_to_iov(i)];
>  		if (res->parent)
>  			nres++;
>  	}
> @@ -786,8 +786,8 @@ static int sriov_init(struct pci_dev *dev, int pos)
>  
>  	nres = 0;
>  	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
> -		res = &dev->resource[i + PCI_IOV_RESOURCES];
> -		res_name = pci_resource_name(dev, i + PCI_IOV_RESOURCES);
> +		res = &dev->resource[pci_resource_to_iov(i)];
> +		res_name = pci_resource_name(dev, pci_resource_to_iov(i));
>  
>  		/*
>  		 * If it is already FIXED, don't change it, something
> @@ -844,7 +844,7 @@ static int sriov_init(struct pci_dev *dev, int pos)
>  	dev->is_physfn = 0;
>  failed:
>  	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
> -		res = &dev->resource[i + PCI_IOV_RESOURCES];
> +		res = &dev->resource[pci_resource_to_iov(i)];
>  		res->flags = 0;
>  	}
>  
> @@ -906,7 +906,7 @@ static void sriov_restore_state(struct pci_dev *dev)
>  	pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, ctrl);
>  
>  	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
> -		pci_update_resource(dev, i + PCI_IOV_RESOURCES);
> +		pci_update_resource(dev, pci_resource_to_iov(i));
>  
>  	pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
>  	pci_iov_set_numvfs(dev, iov->num_VFs);
> @@ -972,7 +972,7 @@ void pci_iov_update_resource(struct pci_dev *dev, int resno)
>  {
>  	struct pci_sriov *iov = dev->is_physfn ? dev->sriov : NULL;
>  	struct resource *res = dev->resource + resno;
> -	int vf_bar = resno - PCI_IOV_RESOURCES;
> +	int vf_bar = pci_resource_from_iov(resno);
>  	struct pci_bus_region region;
>  	u16 cmd;
>  	u32 new;
> diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
> index 48d345607e57e..1f8d88f0243b7 100644
> --- a/drivers/pci/pci.h
> +++ b/drivers/pci/pci.h
> @@ -584,6 +584,15 @@ static inline bool pci_resource_is_iov(int resno)
>  {
>  	return resno >= PCI_IOV_RESOURCES && resno <= PCI_IOV_RESOURCE_END;
>  }
> +static inline int pci_resource_to_iov(int resno)
> +{
> +	return resno + PCI_IOV_RESOURCES;
> +}
> +
> +static inline int pci_resource_from_iov(int resno)
> +{
> +	return resno - PCI_IOV_RESOURCES;
> +}

to/from feels wrong way around for me. What is named as "PCI resource from 
IOV" converts from PCI resource indexing to IOV compatible indexing, and 
vice versa.

>  extern const struct attribute_group sriov_pf_dev_attr_group;
>  extern const struct attribute_group sriov_vf_dev_attr_group;
>  #else
> @@ -608,6 +617,15 @@ static inline bool pci_resource_is_iov(int resno)
>  {
>  	return false;
>  }
> +static inline int pci_resource_to_iov(int resno)
> +{
> +	return -ENODEV;
> +}
> +
> +static inline int pci_resource_from_iov(int resno)
> +{
> +	return -ENODEV;
> +}

These seem dangerous as the errors are not checked by the callers. Perhaps 
put something like BUG_ON(1) there instead as it really is something that 
should never be called for real if CONFIG_PCI_IOV is not enabled, they are 
just to make compiler happy without #ifdefs in C code.

-- 
 i.

>  #endif /* CONFIG_PCI_IOV */
>  
>  #ifdef CONFIG_PCIE_PTM
> diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
> index ba293df10c050..c5ad7c4ad6eb1 100644
> --- a/drivers/pci/setup-bus.c
> +++ b/drivers/pci/setup-bus.c
> @@ -1778,7 +1778,7 @@ static int iov_resources_unassigned(struct pci_dev *dev, void *data)
>  	bool *unassigned = data;
>  
>  	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
> -		struct resource *r = &dev->resource[i + PCI_IOV_RESOURCES];
> +		struct resource *r = &dev->resource[pci_resource_to_iov(i)];
>  		struct pci_bus_region region;
>  
>  		/* Not assigned or rejected by kernel? */
> 

  parent reply	other threads:[~2024-11-06 14:22 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-25 21:50 [PATCH v4 0/7] PCI: VF resizable BAR Michał Winiarski
2024-10-25 21:50 ` [PATCH v4 1/7] PCI/IOV: Restore VF resizable BAR state after reset Michał Winiarski
2024-10-29 13:01   ` Christian König
2024-10-25 21:50 ` [PATCH v4 2/7] PCI: Add a helper to identify IOV resources Michał Winiarski
2024-10-29 13:18   ` Christian König
2024-10-25 21:50 ` [PATCH v4 3/7] PCI: Add a helper to convert between standard and " Michał Winiarski
2024-10-29 13:20   ` Christian König
2024-11-06 14:22   ` Ilpo Järvinen [this message]
2024-11-12 14:40     ` Michał Winiarski
2024-10-25 21:50 ` [PATCH v4 4/7] PCI: Allow IOV resources to be resized in pci_resize_resource() Michał Winiarski
2024-10-25 21:50 ` [PATCH v4 5/7] PCI/IOV: Check that VF BAR fits within the reservation Michał Winiarski
2024-10-28 11:20   ` Michał Winiarski
2024-10-28 16:56   ` Bjorn Helgaas
2024-10-30 11:43     ` Michał Winiarski
2024-10-30 16:55       ` Bjorn Helgaas
2024-11-12 14:31         ` Michał Winiarski
2024-10-25 21:50 ` [PATCH v4 6/7] PCI: Allow drivers to control VF BAR size Michał Winiarski
2024-10-28 16:50   ` Bjorn Helgaas
2024-11-12 14:55     ` Michał Winiarski
2024-10-25 21:50 ` [PATCH v4 7/7] drm/xe/pf: Set VF LMEM " Michał Winiarski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=10b4f173-619a-9913-99de-5d08b3fc854c@linux.intel.com \
    --to=ilpo.jarvinen@linux.intel.com \
    --cc=airlied@gmail.com \
    --cc=bhelgaas@google.com \
    --cc=christian.koenig@amd.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=kw@linux.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=lucas.demarchi@intel.com \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=matthew.d.roper@intel.com \
    --cc=michal.wajdeczko@intel.com \
    --cc=michal.winiarski@intel.com \
    --cc=mripard@kernel.org \
    --cc=rodrigo.vivi@intel.com \
    --cc=simona@ffwll.ch \
    --cc=thomas.hellstrom@linux.intel.com \
    --cc=tzimmermann@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox