diff --git a/drivers/pci/endpoint/functions/pci-epf-test.c b/drivers/pci/endpoint/functions/pci-epf-test.c
index dfdd25cfc003..7d356b0201ae 100644
--- a/drivers/pci/endpoint/functions/pci-epf-test.c
+++ b/drivers/pci/endpoint/functions/pci-epf-test.c
@@ -738,9 +738,9 @@ static void pci_epf_test_enable_doorbell(struct pci_epf_test *epf_test,
reg->doorbell_bar = cpu_to_le32(bar);
msg = &epf->db_msg[0].msg;
- ret = pci_epf_align_inbound_addr(epf, bar, ((u64)msg->address_hi << 32) | msg->address_lo,
+ ret = pci_epf_align_inbound_addr(epf, epf->bar[bar].size,
+ ((u64)msg->address_hi << 32) | msg->address_lo,
&epf_test->db_bar.phys_addr, &offset);
-
if (ret)
goto err_doorbell;
diff --git a/drivers/pci/endpoint/pci-epf-core.c b/drivers/pci/endpoint/pci-epf-core.c
index c21d8e786eb3..b3d4117182e2 100644
--- a/drivers/pci/endpoint/pci-epf-core.c
+++ b/drivers/pci/endpoint/pci-epf-core.c
@@ -478,44 +478,36 @@ struct pci_epf *pci_epf_create(const char *name)
EXPORT_SYMBOL_GPL(pci_epf_create);
/**
- * pci_epf_align_inbound_addr() - Align the given address based on the BAR
- * alignment requirement
+ * pci_epf_align_inbound_addr() - Align the given address based on the BAR size
+ *
* @epf: the EPF device
+ * @bar_size: the current BAR size
* @addr: inbound address to be aligned
- * @bar: the BAR number corresponding to the given addr
- * @base: base address matching the @bar alignment requirement.
+ * @base: base address matching the @bar_size alignment requirement.
* @off: offset to be added to the @base address.
*
- * Helper function to align input 'addr' to base and offset, which match
- * BAR's alignment requirement.
+ * Helper function to align input 'addr' to base and offset, when dynamically
+ * changing a BAR.
*
* The pci_epf_alloc_space() function already accounts for alignment. This is
* primarily intended for use with other memory regions not allocated by
* pci_epf_alloc_space(), such as peripheral register spaces or the trigger
* address for a platform MSI controller.
+ *
+ * Since this function is only used when dynamically changing a BAR (i.e. when
+ * calling set_bar() twice, without ever calling clear_bar(), as calling
+ * clear_bar() would clear the BAR's PCI address assigned by the host), this
+ * function must align to the current BAR size, since we are not clearing the
+ * BAR configuration.
*/
-int pci_epf_align_inbound_addr(struct pci_epf *epf, enum pci_barno bar,
- u64 addr, dma_addr_t *base, size_t *off)
+int pci_epf_align_inbound_addr(struct pci_epf *epf, size_t bar_size, u64 addr,
+ dma_addr_t *base, size_t *off)
{
- const struct pci_epc_features *epc_features;
- u64 align;
-
- if (!base || !off)
+ if (!base || !off || !bar_size)
return -EINVAL;
- epc_features = pci_epc_get_features(epf->epc, epf->func_no, epf->vfunc_no);
- if (!epc_features) {
- dev_err(&epf->dev, "epc_features not implemented\n");
- return -EOPNOTSUPP;
- }
-
- align = epc_features->align;
- align = align ? align : 128;
- if (epc_features->bar[bar].type == BAR_FIXED)
- align = max(epc_features->bar[bar].fixed_size, align);
-
- *base = round_down(addr, align);
- *off = addr & (align - 1);
+ *base = round_down(addr, bar_size);
+ *off = addr & (bar_size - 1);
return 0;
}diff --git a/include/linux/pci-epf.h b/include/linux/pci-epf.h
index 0ca08f0d05d7..bcc8184325d2 100644
--- a/include/linux/pci-epf.h
+++ b/include/linux/pci-epf.h
@@ -242,8 +242,8 @@ void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar,
enum pci_epc_interface_type type);
-int pci_epf_align_inbound_addr(struct pci_epf *epf, enum pci_barno bar,
- u64 addr, dma_addr_t *base, size_t *off);
+int pci_epf_align_inbound_addr(struct pci_epf *epf, size_t bar_size, u64 addr,
+ dma_addr_t *base, size_t *off);
int pci_epf_bind(struct pci_epf *epf);
void pci_epf_unbind(struct pci_epf *epf);
int pci_epf_add_vepf(struct pci_epf *epf_pf, struct pci_epf *epf_vf);