Files
Kevin Bowling c8343ba84a bhyve: Manage passthrough devices across guest FLR
bhyve emulates the guest PCI Command register so BAR sizing does not
disable physical decoding.  However, PCIe Device Control was passed
through.  A guest VFIO reset therefore performed a physical FLR, which
cleared physical Command, while the guest restored only its emulated
copy.  The device remained assigned with bus mastering disabled and
could not fetch DMA descriptors.

Intercept guest FLR writes and issue a PPT-managed reset.  Stop all
vCPUs, verify ownership, quiesce the function, perform only an FLR, and
restore the host-owned PCI configuration, decode, and bus-master state.
Keep the IOMMU domain in place.  bhyve removes guest BAR mappings before
this ioctl; a later guest MEMEN write recreates them.  Never escalate a
guest FLR to a power reset.

Reset the guest-owned Command, MSI, MSI-X, MSI-X table, INTx, and MRRS
state.  PCIe 6.2 section 6.6.2 explicitly preserves MPS across FLR.
Virtualize MPS, MRRS, and Completion Timeout.  Keep physical MPS and
completion-timeout policy host-owned, and apply physical MRRS with MPS
as its floor.  Keep Phantom Functions Enable host-owned because it
changes requester identities visible to the IOMMU.

Serialize guest configuration transactions per function and gate
trapped and direct BAR access across the reset.  Handle byte, word,
dword, and overlapping Device Control accesses.

A guest FLR can sleep for at least 100 ms.  Reserve the target function
while dropping the global PPT lock so a guest cannot delay PPT lifecycle
operations for other VMs.  Operations on the target wait for its reset
while other functions and VMs can proceed.

Check pcie_flr_supported() before destructive preparation so PPT applies
the generic PCI quirk policy.  This includes VFs such as the 82599 which
implement FLR without advertising it.

Validated with two E610 VFs in a Linux 7.0 guest using VFIO no-IOMMU and
DPDK testpmd with two queues per VF.  Byte, word, dword, and overlapping
FLR writes, 32 alternating resets, DPDK traffic, and ixgbevf
reattachment all completed while the sibling VF and host PCIe remained
healthy.  This fixes Linux VFIO no-IOMMU with DPDK PMDs.

Reviewed by:	markj
MFC after:	2 weeks
Sponsored by:	BBOX.io
2026-08-20 15:12:14 -07:00

158 lines
3.9 KiB
C

/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2011 NetApp, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/ioctl.h>
#include <machine/vmm.h>
#include <string.h>
#include "vmmapi.h"
#include "internal.h"
int
vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
{
struct vm_pptdev pptdev;
bzero(&pptdev, sizeof(pptdev));
pptdev.bus = bus;
pptdev.slot = slot;
pptdev.func = func;
return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
}
int
vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
{
struct vm_pptdev pptdev;
bzero(&pptdev, sizeof(pptdev));
pptdev.bus = bus;
pptdev.slot = slot;
pptdev.func = func;
return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
}
int
vm_reset_pptdev(struct vmctx *ctx, int bus, int slot, int func)
{
struct vm_pptdev pptdev;
bzero(&pptdev, sizeof(pptdev));
pptdev.bus = bus;
pptdev.slot = slot;
pptdev.func = func;
return (ioctl(ctx->fd, VM_RESET_PPTDEV, &pptdev));
}
int
vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
{
struct vm_pptdev_mmio pptmmio;
bzero(&pptmmio, sizeof(pptmmio));
pptmmio.bus = bus;
pptmmio.slot = slot;
pptmmio.func = func;
pptmmio.gpa = gpa;
pptmmio.len = len;
pptmmio.hpa = hpa;
return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
}
int
vm_unmap_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
vm_paddr_t gpa, size_t len)
{
struct vm_pptdev_mmio pptmmio;
bzero(&pptmmio, sizeof(pptmmio));
pptmmio.bus = bus;
pptmmio.slot = slot;
pptmmio.func = func;
pptmmio.gpa = gpa;
pptmmio.len = len;
return (ioctl(ctx->fd, VM_UNMAP_PPTDEV_MMIO, &pptmmio));
}
int
vm_setup_pptdev_msi(struct vmctx *ctx, int bus, int slot, int func,
uint64_t addr, uint64_t msg, int numvec)
{
struct vm_pptdev_msi pptmsi;
bzero(&pptmsi, sizeof(pptmsi));
pptmsi.bus = bus;
pptmsi.slot = slot;
pptmsi.func = func;
pptmsi.msg = msg;
pptmsi.addr = addr;
pptmsi.numvec = numvec;
return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
}
int
vm_setup_pptdev_msix(struct vmctx *ctx, int bus, int slot, int func,
int idx, uint64_t addr, uint64_t msg, uint32_t vector_control)
{
struct vm_pptdev_msix pptmsix;
bzero(&pptmsix, sizeof(pptmsix));
pptmsix.bus = bus;
pptmsix.slot = slot;
pptmsix.func = func;
pptmsix.idx = idx;
pptmsix.msg = msg;
pptmsix.addr = addr;
pptmsix.vector_control = vector_control;
return (ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix));
}
int
vm_disable_pptdev_msix(struct vmctx *ctx, int bus, int slot, int func)
{
struct vm_pptdev ppt;
bzero(&ppt, sizeof(ppt));
ppt.bus = bus;
ppt.slot = slot;
ppt.func = func;
return (ioctl(ctx->fd, VM_PPTDEV_DISABLE_MSIX, &ppt));
}