Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 29 Aug 2021 16:40:02 GMT
From:      Mark Johnston <markj@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: c53f23984220 - stable/13 - bhyve: Use pci(4) to access I/O port BARs
Message-ID:  <202108291640.17TGe236015584@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by markj:

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

commit c53f23984220bfd34c198966874c369006ea3246
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2021-08-14 14:42:34 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2021-08-29 16:39:53 +0000

    bhyve: Use pci(4) to access I/O port BARs
    
    This removes the dependency on /dev/io.
    
    PR:             251046
    Reviewed by:    jhb
    Sponsored by:   The FreeBSD Foundation
    
    (cherry picked from commit 42375556e5b2e68746d999b43d124040b6affb91)
---
 usr.sbin/bhyve/pci_passthru.c | 65 ++++++++++++++++++-------------------------
 1 file changed, 27 insertions(+), 38 deletions(-)

diff --git a/usr.sbin/bhyve/pci_passthru.c b/usr.sbin/bhyve/pci_passthru.c
index 9028369217d4..2c6a2ebd8dd9 100644
--- a/usr.sbin/bhyve/pci_passthru.c
+++ b/usr.sbin/bhyve/pci_passthru.c
@@ -69,10 +69,6 @@ __FBSDID("$FreeBSD$");
 #define	_PATH_DEVPCI	"/dev/pci"
 #endif
 
-#ifndef	_PATH_DEVIO
-#define	_PATH_DEVIO	"/dev/io"
-#endif
-
 #ifndef _PATH_MEM
 #define	_PATH_MEM	"/dev/mem"
 #endif
@@ -83,7 +79,6 @@ __FBSDID("$FreeBSD$");
 #define MSIX_CAPLEN 12
 
 static int pcifd = -1;
-static int iofd = -1;
 static int memfd = -1;
 
 struct passthru_softc {
@@ -649,8 +644,8 @@ passthru_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl)
 	const char *value;
 #ifndef WITHOUT_CAPSICUM
 	cap_rights_t rights;
-	cap_ioctl_t pci_ioctls[] = { PCIOCREAD, PCIOCWRITE, PCIOCGETBAR };
-	cap_ioctl_t io_ioctls[] = { IODEV_PIO };
+	cap_ioctl_t pci_ioctls[] =
+	    { PCIOCREAD, PCIOCWRITE, PCIOCGETBAR, PCIOCBARIO };
 #endif
 
 	sc = NULL;
@@ -681,21 +676,6 @@ passthru_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl)
 		errx(EX_OSERR, "Unable to apply rights for sandbox");
 #endif
 
-	if (iofd < 0) {
-		iofd = open(_PATH_DEVIO, O_RDWR, 0);
-		if (iofd < 0) {
-			warn("failed to open %s", _PATH_DEVIO);
-			return (error);
-		}
-	}
-
-#ifndef WITHOUT_CAPSICUM
-	if (caph_rights_limit(iofd, &rights) == -1)
-		errx(EX_OSERR, "Unable to apply rights for sandbox");
-	if (caph_ioctls_limit(iofd, io_ioctls, nitems(io_ioctls)) == -1)
-		errx(EX_OSERR, "Unable to apply rights for sandbox");
-#endif
-
 	if (memfd < 0) {
 		memfd = open(_PATH_MEM, O_RDWR, 0);
 		if (memfd < 0) {
@@ -910,7 +890,7 @@ passthru_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
 	       uint64_t offset, int size, uint64_t value)
 {
 	struct passthru_softc *sc;
-	struct iodev_pio_req pio;
+	struct pci_bar_ioreq pio;
 
 	sc = pi->pi_arg;
 
@@ -918,13 +898,18 @@ passthru_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
 		msix_table_write(ctx, vcpu, sc, offset, size, value);
 	} else {
 		assert(pi->pi_bar[baridx].type == PCIBAR_IO);
-		bzero(&pio, sizeof(struct iodev_pio_req));
-		pio.access = IODEV_PIO_WRITE;
-		pio.port = sc->psc_bar[baridx].addr + offset;
-		pio.width = size;
-		pio.val = value;
-		
-		(void)ioctl(iofd, IODEV_PIO, &pio);
+		assert(size == 1 || size == 2 || size == 4);
+		assert(offset <= UINT32_MAX && offset + size <= UINT32_MAX);
+
+		bzero(&pio, sizeof(pio));
+		pio.pbi_sel = sc->psc_sel;
+		pio.pbi_op = PCIBARIO_WRITE;
+		pio.pbi_bar = baridx;
+		pio.pbi_offset = (uint32_t)offset;
+		pio.pbi_width = size;
+		pio.pbi_value = (uint32_t)value;
+
+		(void)ioctl(pcifd, PCIOCBARIO, &pio);
 	}
 }
 
@@ -933,7 +918,7 @@ passthru_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
 	      uint64_t offset, int size)
 {
 	struct passthru_softc *sc;
-	struct iodev_pio_req pio;
+	struct pci_bar_ioreq pio;
 	uint64_t val;
 
 	sc = pi->pi_arg;
@@ -942,15 +927,19 @@ passthru_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
 		val = msix_table_read(sc, offset, size);
 	} else {
 		assert(pi->pi_bar[baridx].type == PCIBAR_IO);
-		bzero(&pio, sizeof(struct iodev_pio_req));
-		pio.access = IODEV_PIO_READ;
-		pio.port = sc->psc_bar[baridx].addr + offset;
-		pio.width = size;
-		pio.val = 0;
+		assert(size == 1 || size == 2 || size == 4);
+		assert(offset <= UINT32_MAX && offset + size <= UINT32_MAX);
+
+		bzero(&pio, sizeof(pio));
+		pio.pbi_sel = sc->psc_sel;
+		pio.pbi_op = PCIBARIO_READ;
+		pio.pbi_bar = baridx;
+		pio.pbi_offset = (uint32_t)offset;
+		pio.pbi_width = size;
 
-		(void)ioctl(iofd, IODEV_PIO, &pio);
+		(void)ioctl(pcifd, PCIOCBARIO, &pio);
 
-		val = pio.val;
+		val = pio.pbi_value;
 	}
 
 	return (val);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202108291640.17TGe236015584>