On Mon, Nov 11, 2024 at 03:43:12PM +0100, Niklas Cassel wrote: > On Fri, Nov 08, 2024 at 02:43:30PM -0500, Frank Li wrote: > > Perhaps create a helper function so that you don't need to duplicate it. Something like this on top of your series: diff --git a/drivers/pci/endpoint/functions/pci-epf-test.c b/drivers/pci/endpoint/functions/pci-epf-test.c index 8ede7aded03ee..b1707b4425432 100644 --- a/drivers/pci/endpoint/functions/pci-epf-test.c +++ b/drivers/pci/endpoint/functions/pci-epf-test.c @@ -656,16 +656,25 @@ static void pci_epf_test_raise_irq(struct pci_epf_test *epf_test, } } -static void pci_epf_enable_doorbell(struct pci_epf_test *epf_test, struct pci_epf_test_reg *reg) +static int pci_epf_get_doorbell_addr(struct pci_epf_test *epf_test, + enum pci_barno bar, u64 *db_base, + u64 *db_offset) { - enum pci_barno bar = reg->doorbell_bar; struct pci_epf *epf = epf_test->epf; - struct pci_epc *epc = epf->epc; - struct pci_epf_bar db_bar; struct msi_msg *msg; - u64 doorbell_addr; + u64 doorbell_addr, mask; u32 align; - int ret; + + if (bar < BAR_0 || bar == epf_test->test_reg_bar || !epf->db_msg) + return -EINVAL; + + msg = &epf->db_msg[0].msg; + doorbell_addr = msg->address_hi; + doorbell_addr <<= 32; + doorbell_addr |= msg->address_lo; + + if (!doorbell_addr) + return -EINVAL; align = epf_test->epc_features->align; align = align ? align : 128; @@ -673,17 +682,28 @@ static void pci_epf_enable_doorbell(struct pci_epf_test *epf_test, struct pci_ep if (epf_test->epc_features->bar[bar].type == BAR_FIXED) align = max(epf_test->epc_features->bar[bar].fixed_size, align); - if (bar < BAR_0 || bar == epf_test->test_reg_bar || !epf->db_msg) { + mask = align - 1; + *db_base = doorbell_addr & ~mask; + *db_offset = doorbell_addr & mask; + + return 0; +} + +static void pci_epf_enable_doorbell(struct pci_epf_test *epf_test, struct pci_epf_test_reg *reg) +{ + enum pci_barno bar = reg->doorbell_bar; + struct pci_epf *epf = epf_test->epf; + struct pci_epc *epc = epf->epc; + struct pci_epf_bar db_bar; + u64 db_base, db_offset; + int ret; + + if (pci_epf_get_doorbell_addr(epf_test, bar, &db_base, &db_offset)) { reg->status |= STATUS_DOORBELL_ENABLE_FAIL; return; } - msg = &epf->db_msg[0].msg; - doorbell_addr = msg->address_hi; - doorbell_addr <<= 32; - doorbell_addr |= msg->address_lo; - - db_bar.phys_addr = round_down(doorbell_addr, align); + db_bar.phys_addr = db_base; db_bar.barno = bar; db_bar.size = epf->bar[bar].size; db_bar.flags = epf->bar[bar].flags; @@ -1015,9 +1035,7 @@ static int pci_epf_test_bind(struct pci_epf *epf) ret = pci_epf_alloc_doorbell(epf, 1); if (!ret) { struct pci_epf_test_reg *reg = epf_test->reg[test_reg_bar]; - struct msi_msg *msg = &epf->db_msg[0].msg; - u32 align = epc_features->align; - u64 doorbell_addr; + u64 db_base, db_offset; enum pci_barno bar; bar = pci_epc_get_next_free_bar(epc_features, test_reg_bar + 1); @@ -1031,17 +1049,15 @@ static int pci_epf_test_bind(struct pci_epf *epf) return 0; } - align = align ? align : 128; - - if (epf_test->epc_features->bar[bar].type == BAR_FIXED) - align = max(epf_test->epc_features->bar[bar].fixed_size, align); - - doorbell_addr = msg->address_hi; - doorbell_addr <<= 32; - doorbell_addr |= msg->address_lo; + if (pci_epf_get_doorbell_addr(epf_test, bar, &db_base, + &db_offset)) { + dev_err(&epf->dev, "Failed to get doorbell address\n"); + free_irq(epf->db_msg[0].virq, epf_test); + return 0; + } - reg->doorbell_addr = doorbell_addr & (align - 1); - reg->doorbell_data = msg->data; + reg->doorbell_addr = db_offset; + reg->doorbell_data = epf->db_msg[0].msg.data; reg->doorbell_bar = bar; } > > Also one function is doing: > reg->doorbell_addr = doorbell_addr & (align - 1); > > to align, the other one is doing: > round_down(doorbell_addr, align); > > Which seems to be a bit inconsistent. I now see why you did this. One function is using the db offset, and the other is using the db base. I strongly suggest that you rename: reg->doorbell_addr to reg->doorbell_offset and PCI_ENDPOINT_TEST_DB_ADDR to PCI_ENDPOINT_TEST_DB_OFFSET since the current names are very confusing. Kind regards, Niklas