Skip to content

Commit 535bdbe

Browse files
mwiniarsbjorn-helgaas
authored andcommitted
PCI/IOV: Add pci_resource_num_to_vf_bar() to convert VF BAR number to/from IOV resource
There are multiple places where conversions between IOV resources and corresponding VF BAR numbers are done. Extract the logic to pci_resource_num_from_vf_bar() and pci_resource_num_to_vf_bar() helpers. Suggested-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com> Signed-off-by: Michał Winiarski <michal.winiarski@intel.com> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com> Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com> Acked-by: Christian König <christian.koenig@amd.com> Link: https://patch.msgid.link/20250702093522.518099-3-michal.winiarski@intel.com
1 parent 5a8f77e commit 535bdbe

3 files changed

Lines changed: 36 additions & 11 deletions

File tree

drivers/pci/iov.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno)
151151
if (!dev->is_physfn)
152152
return 0;
153153

154-
return dev->sriov->barsz[resno - PCI_IOV_RESOURCES];
154+
return dev->sriov->barsz[pci_resource_num_to_vf_bar(resno)];
155155
}
156156

157157
static void pci_read_vf_config_common(struct pci_dev *virtfn)
@@ -342,12 +342,14 @@ int pci_iov_add_virtfn(struct pci_dev *dev, int id)
342342
virtfn->multifunction = 0;
343343

344344
for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
345-
res = &dev->resource[i + PCI_IOV_RESOURCES];
345+
int idx = pci_resource_num_from_vf_bar(i);
346+
347+
res = &dev->resource[idx];
346348
if (!res->parent)
347349
continue;
348350
virtfn->resource[i].name = pci_name(virtfn);
349351
virtfn->resource[i].flags = res->flags;
350-
size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
352+
size = pci_iov_resource_size(dev, idx);
351353
resource_set_range(&virtfn->resource[i],
352354
res->start + size * id, size);
353355
rc = request_resource(res, &virtfn->resource[i]);
@@ -644,8 +646,10 @@ static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
644646

645647
nres = 0;
646648
for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
647-
bars |= (1 << (i + PCI_IOV_RESOURCES));
648-
res = &dev->resource[i + PCI_IOV_RESOURCES];
649+
int idx = pci_resource_num_from_vf_bar(i);
650+
651+
bars |= (1 << idx);
652+
res = &dev->resource[idx];
649653
if (res->parent)
650654
nres++;
651655
}
@@ -811,8 +815,10 @@ static int sriov_init(struct pci_dev *dev, int pos)
811815

812816
nres = 0;
813817
for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
814-
res = &dev->resource[i + PCI_IOV_RESOURCES];
815-
res_name = pci_resource_name(dev, i + PCI_IOV_RESOURCES);
818+
int idx = pci_resource_num_from_vf_bar(i);
819+
820+
res = &dev->resource[idx];
821+
res_name = pci_resource_name(dev, idx);
816822

817823
/*
818824
* If it is already FIXED, don't change it, something
@@ -871,7 +877,7 @@ static int sriov_init(struct pci_dev *dev, int pos)
871877
dev->is_physfn = 0;
872878
failed:
873879
for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
874-
res = &dev->resource[i + PCI_IOV_RESOURCES];
880+
res = &dev->resource[pci_resource_num_from_vf_bar(i)];
875881
res->flags = 0;
876882
}
877883

@@ -933,7 +939,7 @@ static void sriov_restore_state(struct pci_dev *dev)
933939
pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, ctrl);
934940

935941
for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
936-
pci_update_resource(dev, i + PCI_IOV_RESOURCES);
942+
pci_update_resource(dev, pci_resource_num_from_vf_bar(i));
937943

938944
pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
939945
pci_iov_set_numvfs(dev, iov->num_VFs);
@@ -999,7 +1005,7 @@ void pci_iov_update_resource(struct pci_dev *dev, int resno)
9991005
{
10001006
struct pci_sriov *iov = dev->is_physfn ? dev->sriov : NULL;
10011007
struct resource *res = pci_resource_n(dev, resno);
1002-
int vf_bar = resno - PCI_IOV_RESOURCES;
1008+
int vf_bar = pci_resource_num_to_vf_bar(resno);
10031009
struct pci_bus_region region;
10041010
u16 cmd;
10051011
u32 new;

drivers/pci/pci.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,14 @@ static inline bool pci_resource_is_iov(int resno)
722722
{
723723
return resno >= PCI_IOV_RESOURCES && resno <= PCI_IOV_RESOURCE_END;
724724
}
725+
static inline int pci_resource_num_from_vf_bar(int resno)
726+
{
727+
return resno + PCI_IOV_RESOURCES;
728+
}
729+
static inline int pci_resource_num_to_vf_bar(int resno)
730+
{
731+
return resno - PCI_IOV_RESOURCES;
732+
}
725733
extern const struct attribute_group sriov_pf_dev_attr_group;
726734
extern const struct attribute_group sriov_vf_dev_attr_group;
727735
#else
@@ -750,6 +758,16 @@ static inline bool pci_resource_is_iov(int resno)
750758
{
751759
return false;
752760
}
761+
static inline int pci_resource_num_from_vf_bar(int resno)
762+
{
763+
WARN_ON_ONCE(1);
764+
return -ENODEV;
765+
}
766+
static inline int pci_resource_num_to_vf_bar(int resno)
767+
{
768+
WARN_ON_ONCE(1);
769+
return -ENODEV;
770+
}
753771
#endif /* CONFIG_PCI_IOV */
754772

755773
#ifdef CONFIG_PCIE_TPH

drivers/pci/setup-bus.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1888,7 +1888,8 @@ static int iov_resources_unassigned(struct pci_dev *dev, void *data)
18881888
bool *unassigned = data;
18891889

18901890
for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
1891-
struct resource *r = &dev->resource[i + PCI_IOV_RESOURCES];
1891+
int idx = pci_resource_num_from_vf_bar(i);
1892+
struct resource *r = &dev->resource[idx];
18921893
struct pci_bus_region region;
18931894

18941895
/* Not assigned or rejected by kernel? */

0 commit comments

Comments
 (0)