From nobody Sun Jan 09 02:39:56 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 895CE1933793; Sun, 9 Jan 2022 02:39:57 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4JWh4r5z94z3qTX; Sun, 9 Jan 2022 02:39:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 8CF8D1EEF; Sun, 9 Jan 2022 02:39:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 2092dulE095335; Sun, 9 Jan 2022 02:39:56 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2092dukt095334; Sun, 9 Jan 2022 02:39:56 GMT (envelope-from git) Date: Sun, 9 Jan 2022 02:39:56 GMT Message-Id: <202201090239.2092dukt095334@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: "Bjoern A. Zeeb" Subject: git: 7ea16192a01e - stable/13 - bhyve: passthru: enable BARs before possibly mmap(2)ing them List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: bz X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 7ea16192a01ea3de7b088ef6dc3cbf29d8e21a54 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1641695997; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=I2ltdRKi/TzCAOCyZ7A5A/szs5Tt56aU/p4IekomXQg=; b=YEepPjvpYkdsXXEKOtvnDqXBLWkla+OAJJvJRHZZNbifClQuGD8cR3V9J5hBRozy2LMSiq lSYPOp9Rk/bpF0zce+7YglQUqQxHTsSemFjkufcnsjQOJRZuKSdkntyUDrewE/52oSKbGs Scx2dB9XMI2Y+/6r35mH20hjRUtLEMkX+ScVhvnl1c2iLmpmQG7vTPeJpvvmHlbQ3yi+ST GYSyYRnJOkLiQgLhCco99PovnozATYM2DQECpzyXLXwSBQhBDYAwBJ8xMX4yS3oom0yCrJ 0A9HosjB3FM7iIcdVU0rJAr27Lx8+k0d9n6WpoNZNFtfGUm5zs24qdauT0przQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1641695997; a=rsa-sha256; cv=none; b=Z7YD+XPi6XyZT4C+FtKcH2AJPu2dpsCOInhf3L6DAR0gDe4QrV2rlG5u7McKsl/Ul2yvM/ XCddynBEyYknpAYSHdKxUWCsX7uvYESCZ9AUswV1OcIgzToF3COGMePa/j2sx+gMlXlStE 3tMF7MhcCclArkaRxV5eKqclZxWFwVyCdi2YZouvZmU3uAFgP2dNdFP8ozCYeZ/53MTt0g 5lhFt4atsYmVBpkdPMs5FfBjH6VbHzo9/BKNXaoa9Vg1KuVAGwlGDY7DvKuUyV4U2oUoho lyqDoX+zWZAX/TXLo5vHW47h/vlqnrgDPl44+YmxKC0nhBjJU3zfsCJTliogpw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by bz: URL: https://cgit.FreeBSD.org/src/commit/?id=7ea16192a01ea3de7b088ef6dc3cbf29d8e21a54 commit 7ea16192a01ea3de7b088ef6dc3cbf29d8e21a54 Author: Bjoern A. Zeeb AuthorDate: 2021-12-23 14:59:49 +0000 Commit: Bjoern A. Zeeb CommitDate: 2022-01-09 02:38:58 +0000 bhyve: passthru: enable BARs before possibly mmap(2)ing them The first time we start bhyve with a passthru device everything is fine as on boot we do enable BARs. If a driver (unload) inside bhyve disables the BAR(s) as some Linux drivers do, we need to make sure we re-enable them on next bhyve start. If we are trying to mmap a disabled BAR for MSI-X (PCIOCBARMMAP) the kernel will give us an EBUSY. While we were re-enabling the BAR(s) in the current code loop cfginit() was writing the changes out too late to the real hardware. Move the call to init_msix_table() after the register on the real hardware was updated. That way the kernel will be happy and the mmap will succeed and bhyve will start. Also simplify the code given the last argument to init_msix_table() is unused we do not need to do checks for each bar. [1] Some passthru devices only support MSI instead of MSI-X. For those devices the initialization of MSI-X table will fail. Re-add (or in the MFC case keep) the check erroneously removed in the initial commit. [2] PR: 260148 Pointed out by: markj [1] Sponsored by: The FreeBSD Foundation Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D33628 Submitted by: (C.Koehne beckhoff.com) [2] Reviewed by: manu, bz Differential Revision: https://reviews.freebsd.org/D33728 (cherry picked from commit f1442847c9404d4bc5f5524a0c3362dd39cb14f9) (cherry picked from commit 338a1be836308f6d807f8bfe9b335463d537abc4) --- usr.sbin/bhyve/pci_passthru.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/usr.sbin/bhyve/pci_passthru.c b/usr.sbin/bhyve/pci_passthru.c index 2e274829d783..436091cbe8ee 100644 --- a/usr.sbin/bhyve/pci_passthru.c +++ b/usr.sbin/bhyve/pci_passthru.c @@ -420,7 +420,7 @@ msix_table_write(struct vmctx *ctx, int vcpu, struct passthru_softc *sc, } static int -init_msix_table(struct vmctx *ctx, struct passthru_softc *sc, uint64_t base) +init_msix_table(struct vmctx *ctx, struct passthru_softc *sc) { struct pci_devinst *pi = sc->psc_pi; struct pci_bar_mmap pbm; @@ -538,13 +538,6 @@ cfginitbar(struct vmctx *ctx, struct passthru_softc *sc) if (error) return (-1); - /* The MSI-X table needs special handling */ - if (i == pci_msix_table_bar(pi)) { - error = init_msix_table(ctx, sc, base); - if (error) - return (-1); - } - /* * 64-bit BAR takes up two slots so skip the next one. */ @@ -586,6 +579,20 @@ cfginit(struct vmctx *ctx, struct pci_devinst *pi, int bus, int slot, int func) pci_set_cfgdata16(pi, PCIR_COMMAND, read_config(&sc->psc_sel, PCIR_COMMAND, 2)); + /* + * We need to do this after PCIR_COMMAND got possibly updated, e.g., + * a BAR was enabled, as otherwise the PCIOCBARMMAP might fail on us. + */ + if (pci_msix_table_bar(pi) >= 0) { + error = init_msix_table(ctx, sc); + if (error != 0) { + warnx( + "failed to initialize MSI-X table for PCI %d/%d/%d: %d", + bus, slot, func, error); + goto done; + } + } + error = 0; /* success */ done: return (error);