@@ -231,6 +231,9 @@ class Diagnostics:
231231 def __init__ (self , device_bdf : Optional [str ] = None ):
232232 self .device_bdf = device_bdf
233233 self .checks : List [Check ] = []
234+ # Store device vendor/device IDs for remediation commands
235+ self ._vendor_id : Optional [str ] = None
236+ self ._device_id : Optional [str ] = None
234237
235238 # Public API --------------------------------------------------------------
236239 def run (self ) -> Report :
@@ -591,6 +594,9 @@ def _device_exists(self):
591594 path_manager = VFIOPathManager (self .device_bdf )
592595 if path_manager .device_path .exists ():
593596 vendor_id , device_id = path_manager .get_vendor_device_id ()
597+ # Store IDs for use in remediation commands (without 0x prefix)
598+ self ._vendor_id = vendor_id
599+ self ._device_id = device_id
594600 # Add 0x prefix for display consistency with existing behavior
595601 vendor = f"0x{ vendor_id } "
596602 device = f"0x{ device_id } "
@@ -828,22 +834,49 @@ def _device_driver_binding(self):
828834 commands = self ._bind_commands (self .device_bdf , None ),
829835 )
830836
831- @staticmethod
832- def _bind_commands (bdf : str , current : Optional [str ]) -> List [str ]:
833- cmds : list [str ] = [
834- (
837+ def _bind_commands (self , bdf : str , current : Optional [str ]) -> List [str ]:
838+ """Generate commands to bind device to vfio-pci driver.
839+
840+ Uses the new_id approach which is more robust:
841+ 1. Unbind from current driver (if any)
842+ 2. Register device IDs with vfio-pci via new_id (this auto-binds)
843+
844+ The new_id approach is preferred because:
845+ - It works even when the device has no current driver
846+ - It doesn't require the device to be pre-registered with vfio-pci
847+ - It handles the binding automatically after registration
848+ """
849+ cmds : list [str ] = []
850+
851+ # Step 1: Unbind from current driver if one is bound
852+ if current :
853+ cmds .append (
835854 safe_format (
836855 "echo '{bdf}' | sudo tee /sys/bus/pci/devices/{bdf}/driver/unbind" ,
837856 bdf = bdf ,
838857 )
839- if current
840- else ""
841- ),
842- safe_format (
843- "echo '{bdf}' | sudo tee /sys/bus/pci/drivers/vfio-pci/bind" , bdf = bdf
844- ),
845- ]
846- return [c for c in cmds if c ]
858+ )
859+
860+ # Step 2: Use new_id to register and auto-bind to vfio-pci
861+ # This is the recommended approach and works regardless of prior driver state
862+ if self ._vendor_id and self ._device_id :
863+ cmds .append (
864+ safe_format (
865+ "echo '{vid} {did}' | sudo tee /sys/bus/pci/drivers/vfio-pci/new_id" ,
866+ vid = self ._vendor_id ,
867+ did = self ._device_id ,
868+ )
869+ )
870+ else :
871+ # Fallback to direct bind if we don't have device IDs
872+ # This may fail if the device IDs aren't pre-registered
873+ cmds .append (
874+ safe_format (
875+ "echo '{bdf}' | sudo tee /sys/bus/pci/drivers/vfio-pci/bind" , bdf = bdf
876+ )
877+ )
878+
879+ return cmds
847880
848881 def _device_node (self ):
849882 link = Path (
0 commit comments