• Max Gurtovoy's avatar
    PCI / VFIO: Add 'override_only' support for VFIO PCI sub system · cc6711b0
    Max Gurtovoy authored
    Expose an 'override_only' helper macro (i.e.
    PCI_DRIVER_OVERRIDE_DEVICE_VFIO) for VFIO PCI sub system and add the
    required code to prefix its matching entries with "vfio_" in
    modules.alias file.
    
    It allows VFIO device drivers to include match entries in the
    modules.alias file produced by kbuild that are not used for normal
    driver autoprobing and module autoloading. Drivers using these match
    entries can be connected to the PCI device manually, by userspace, using
    the existing driver_override sysfs.
    
    For example the resulting modules.alias may have:
    
      alias pci:v000015B3d00001021sv*sd*bc*sc*i* mlx5_core
      alias vfio_pci:v000015B3d00001021sv*sd*bc*sc*i* mlx5_vfio_pci
      alias vfio_pci:v*d*sv*sd*bc*sc*i* vfio_pci
    
    In this example mlx5_core and mlx5_vfio_pci match to the same PCI
    device. The kernel will autoload and autobind to mlx5_core but the
    kernel and udev mechanisms will ignore mlx5_vfio_pci.
    
    When userspace wants to change a device to the VFIO subsystem it can
    implement a generic algorithm:
    
       1) Identify the sysfs path to the device:
        /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0
    
       2) Get the modalias string from the kernel:
        $ cat /sys/bus/pci/devices/0000:01:00.0/modalias
        pci:v000015B3d00001021sv000015B3sd00000001bc02sc00i00
    
       3) Prefix it with vfio_:
        vfio_pci:v000015B3d00001021sv000015B3sd00000001bc02sc00i00
    
       4) Search modules.alias for the above string and select the entry that
          has the fewest *'s:
        alias vfio_pci:v000015B3d00001021sv*sd*bc*sc*i* mlx5_vfio_pci
    
       5) modprobe the matched module name:
        $ modprobe mlx5_vfio_pci
    
       6) cat the matched module name to driver_override:
        echo mlx5_vfio_pci > /sys/bus/pci/devices/0000:01:00.0/driver_override
    
       7) unbind device from original module
         echo 0000:01:00.0 > /sys/bus/pci/devices/0000:01:00.0/driver/unbind
    
       8) probe PCI drivers (or explicitly bind to mlx5_vfio_pci)
        echo 0000:01:00.0 > /sys/bus/pci/drivers_probe
    
    The algorithm is independent of bus type. In future the other buses with
    VFIO device drivers, like platform and ACPI, can use this algorithm as
    well.
    
    This patch is the infrastructure to provide the information in the
    modules.alias to userspace. Convert the only VFIO pci_driver which results
    in one new line in the modules.alias:
    
      alias vfio_pci:v*d*sv*sd*bc*sc*i* vfio_pci
    
    Later series introduce additional HW specific VFIO PCI drivers, such as
    mlx5_vfio_pci.
    Signed-off-by: default avatarMax Gurtovoy <mgurtovoy@nvidia.com>
    Signed-off-by: default avatarJason Gunthorpe <jgg@nvidia.com>
    Acked-by: Bjorn Helgaas <bhelgaas@google.com>  # for pci.h
    Signed-off-by: default avatarYishai Hadas <yishaih@nvidia.com>
    Link: https://lore.kernel.org/r/20210826103912.128972-11-yishaih@nvidia.comSigned-off-by: default avatarAlex Williamson <alex.williamson@redhat.com>
    cc6711b0
devicetable-offsets.c 7.31 KB