git: 60ddb0cc3193 - stable/13 - LinuxKPI: pci.h split up pcim_iomap_regions_request_all()

From: Bjoern A. Zeeb <bz_at_FreeBSD.org>
Date: Mon, 17 Oct 2022 20:41:30 UTC
The branch stable/13 has been updated by bz:

URL: https://cgit.FreeBSD.org/src/commit/?id=60ddb0cc3193ca98042f260685bbbf93369b472b

commit 60ddb0cc3193ca98042f260685bbbf93369b472b
Author:     Bjoern A. Zeeb <bz@FreeBSD.org>
AuthorDate: 2022-09-21 19:41:37 +0000
Commit:     Bjoern A. Zeeb <bz@FreeBSD.org>
CommitDate: 2022-10-17 20:37:03 +0000

    LinuxKPI: pci.h split up pcim_iomap_regions_request_all()
    
    Factor out parts of pcim_iomap_regions_request_all() into
    pcim_iomap_regions() now needed for a driver.
    
    Sponsored by:   The FreeBSD Foundation
    Reviewed by:    hselasky
    Differential Revision: https://reviews.freebsd.org/D36654
    
    (cherry picked from commit 30048f61423bed87912686a7adfd12be9a4c7c27)
---
 sys/compat/linuxkpi/common/include/linux/pci.h | 52 +++++++++++++++++---------
 1 file changed, 35 insertions(+), 17 deletions(-)

diff --git a/sys/compat/linuxkpi/common/include/linux/pci.h b/sys/compat/linuxkpi/common/include/linux/pci.h
index 1734fb52baa9..6493391926e2 100644
--- a/sys/compat/linuxkpi/common/include/linux/pci.h
+++ b/sys/compat/linuxkpi/common/include/linux/pci.h
@@ -1471,28 +1471,17 @@ pcim_iomap_table(struct pci_dev *pdev)
 }
 
 static inline int
-pcim_iomap_regions_request_all(struct pci_dev *pdev, uint32_t mask, char *name)
+pcim_iomap_regions(struct pci_dev *pdev, uint32_t mask, const char *name)
 {
 	struct pcim_iomap_devres *dr;
 	void *res;
-	uint32_t mappings, requests, req_mask;
-	int bar, error;
+	uint32_t mappings;
+	int bar;
 
 	dr = lkpi_pcim_iomap_devres_find(pdev);
 	if (dr == NULL)
 		return (-ENOMEM);
 
-	/* Request all the BARs ("regions") we do not iomap. */
-	req_mask = ((1 << (PCIR_MAX_BAR_0 + 1)) - 1) & ~mask;
-	for (bar = requests = 0; requests != req_mask; bar++) {
-		if ((req_mask & (1 << bar)) == 0)
-			continue;
-		error = pci_request_region(pdev, bar, name);
-		if (error != 0 && error != -ENODEV)
-			goto err;
-		requests |= (1 << bar);
-	}
-
 	/* Now iomap all the requested (by "mask") ones. */
 	for (bar = mappings = 0; mappings != mask; bar++) {
 		if ((mask & (1 << bar)) == 0)
@@ -1515,7 +1504,6 @@ pcim_iomap_regions_request_all(struct pci_dev *pdev, uint32_t mask, char *name)
 	}
 
 	return (0);
-
 err:
 	for (bar = PCIR_MAX_BAR_0; bar >= 0; bar--) {
 		if ((mappings & (1 << bar)) != 0) {
@@ -1523,14 +1511,44 @@ err:
 			if (res == NULL)
 				continue;
 			pci_iounmap(pdev, res);
-		} else if ((requests & (1 << bar)) != 0) {
-			pci_release_region(pdev, bar);
 		}
 	}
 
 	return (-EINVAL);
 }
 
+static inline int
+pcim_iomap_regions_request_all(struct pci_dev *pdev, uint32_t mask, char *name)
+{
+	uint32_t requests, req_mask;
+	int bar, error;
+
+	/* Request all the BARs ("regions") we do not iomap. */
+	req_mask = ((1 << (PCIR_MAX_BAR_0 + 1)) - 1) & ~mask;
+	for (bar = requests = 0; requests != req_mask; bar++) {
+		if ((req_mask & (1 << bar)) == 0)
+			continue;
+		error = pci_request_region(pdev, bar, name);
+		if (error != 0 && error != -ENODEV)
+			goto err;
+		requests |= (1 << bar);
+	}
+
+	error = pcim_iomap_regions(pdev, mask, name);
+	if (error != 0)
+		goto err;
+
+	return (0);
+
+err:
+	for (bar = PCIR_MAX_BAR_0; bar >= 0; bar--) {
+		if ((requests & (1 << bar)) != 0)
+			pci_release_region(pdev, bar);
+	}
+
+	return (-EINVAL);
+}
+
 /* This is a FreeBSD extension so we can use bus_*(). */
 static inline void
 linuxkpi_pcim_want_to_use_bus_functions(struct pci_dev *pdev)