[RFC PATCH v4 35/38] PCI: endpoint: pci-epf-test: Add pci_epf_test_next_free_bar() helper

Koichiro Den
2026-01-18T13:55:58Z

Introduce pci_epf_test_next_free_bar(), a small helper that wraps
pci_epc_get_next_free_bar() and tracks the next starting BAR number.
Use it for selecting the test register BAR and the doorbell BAR.

An upcoming extension needs to reserve an additional BAR, and this
helper will help keep the code compact with the selection logic
being centralized.

No functional change intended.

Signed-off-by: Koichiro Den <den@valinux.co.jp>
---
drivers/pci/endpoint/functions/pci-epf-test.c | 20 ++++++++++++++++---
1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/drivers/pci/endpoint/functions/pci-epf-test.c b/drivers/pci/endpoint/functions/pci-epf-test.c
index 6ecbc2c2ff36..e560c3becebb 100644
--- a/drivers/pci/endpoint/functions/pci-epf-test.c
+++ b/drivers/pci/endpoint/functions/pci-epf-test.c
@@ -64,6 +64,7 @@ struct pci_epf_test {
void *reg[PCI_STD_NUM_BARS];
struct pci_epf *epf;
enum pci_barno test_reg_bar;
+ enum pci_barno next_free_bar;
size_t msix_table_offset;
struct delayed_work cmd_handler;
struct dma_chan *dma_chan_tx;
@@ -104,6 +105,18 @@ static struct pci_epf_header test_header = {

static size_t bar_size[] = { 512, 512, 1024, 16384, 131072, 1048576 };

+static enum pci_barno pci_epf_test_next_free_bar(struct pci_epf_test *epf_test)
+{
+ enum pci_barno bar;
+
+ bar = pci_epc_get_next_free_bar(epf_test->epc_features,
+ epf_test->next_free_bar);
+ if (bar != NO_BAR)
+ epf_test->next_free_bar = bar + 1;
+
+ return bar;
+}
+
static void pci_epf_test_dma_callback(void *param)
{
struct pci_epf_test *epf_test = param;
@@ -721,7 +734,7 @@ static void pci_epf_test_enable_doorbell(struct pci_epf_test *epf_test,
goto set_status_err;

msg = &epf->db_msg[0].msg;
- bar = pci_epc_get_next_free_bar(epf_test->epc_features, epf_test->test_reg_bar + 1);
+ bar = pci_epf_test_next_free_bar(epf_test);
if (bar < BAR_0)
goto err_doorbell_cleanup;

@@ -1110,13 +1123,14 @@ static int pci_epf_test_bind(struct pci_epf *epf)
dev_err(&epf->dev, "epc_features not implemented\n");
return -EOPNOTSUPP;
}
+ epf_test->epc_features = epc_features;
+ epf_test->next_free_bar = BAR_0;

- test_reg_bar = pci_epc_get_first_free_bar(epc_features);
+ test_reg_bar = pci_epf_test_next_free_bar(epf_test);
if (test_reg_bar < 0)
return -EINVAL;

epf_test->test_reg_bar = test_reg_bar;
- epf_test->epc_features = epc_features;

ret = pci_epf_test_alloc_space(epf);
if (ret)
--
2.51.0