• Abhishek Sahu's avatar
    vfio/pci: wake-up devices around reset functions · 26a17b12
    Abhishek Sahu authored
    If 'vfio_pci_core_device::needs_pm_restore' is set (PCI device does
    not have No_Soft_Reset bit set in its PMCSR config register), then the
    current PCI state will be saved locally in
    'vfio_pci_core_device::pm_save' during D0->D3hot transition and same
    will be restored back during D3hot->D0 transition. For reset-related
    functionalities, vfio driver uses PCI reset API's. These
    API's internally change the PCI power state back to D0 first if
    the device power state is non-D0. This state change to D0 will happen
    without the involvement of vfio driver.
    
    Let's consider the following example:
    
    1. The device is in D3hot.
    2. User invokes VFIO_DEVICE_RESET ioctl.
    3. pci_try_reset_function() will be called which internally
       invokes pci_dev_save_and_disable().
    4. pci_set_power_state(dev, PCI_D0) will be called first.
    5. pci_save_state() will happen then.
    
    Now, for the devices which has NoSoftRst-, the pci_set_power_state()
    can trigger soft reset and the original PCI config state will be lost
    at step (4) and this state cannot be restored again. This original PCI
    state can include any setting which is performed by SBIOS or host
    linux kernel (for example LTR, ASPM L1 substates, etc.). When this
    soft reset will be triggered, then all these settings will be reset,
    and the device state saved at step (5) will also have this setting
    cleared so it cannot be restored. Since the vfio driver only exposes
    limited PCI capabilities to its user, so the vfio driver user also
    won't have the option to save and restore these capabilities state
    either and these original settings will be permanently lost.
    
    For pci_reset_bus() also, we can have the above situation.
    The other functions/devices can be in D3hot and the reset will change
    the power state of all devices to D0 without the involvement of vfio
    driver.
    
    So, before calling any reset-related API's, we need to make sure that
    the device state is D0. This is mainly to preserve the state around
    soft reset.
    
    For vfio_pci_core_disable(), we use __pci_reset_function_locked()
    which internally can use pci_pm_reset() for the function reset.
    pci_pm_reset() requires the device power state to be in D0, otherwise
    it returns error.
    
    This patch changes the device power state to D0 by invoking
    vfio_pci_set_power_state() explicitly before calling any reset related
    API's.
    
    Fixes: 51ef3a00 ("vfio/pci: Restore device state on PM transition")
    Signed-off-by: default avatarAbhishek Sahu <abhsahu@nvidia.com>
    Link: https://lore.kernel.org/r/20220217122107.22434-3-abhsahu@nvidia.comSigned-off-by: default avatarAlex Williamson <alex.williamson@redhat.com>
    26a17b12
vfio_pci_core.c 57.7 KB