22// SPDX-License-Identifier: Apache-2.0
33
44use crate :: VfioError ;
5- use crate :: sysfs:: { SysfsRoot , read_sysfs_trimmed , write_sysfs} ;
5+ use crate :: sysfs:: { SysfsRoot , write_sysfs} ;
66use std:: collections:: HashMap ;
7- use std:: fs;
87use std:: sync:: { LazyLock , Mutex } ;
98use std:: time:: Duration ;
109
@@ -19,20 +18,12 @@ static VFIO_ID_REFCOUNTS: LazyLock<Mutex<HashMap<String, usize>>> =
1918 LazyLock :: new ( || Mutex :: new ( HashMap :: new ( ) ) ) ;
2019
2120pub ( crate ) fn current_driver_name ( sysfs : & SysfsRoot , bdf : & str ) -> Option < String > {
22- let driver_link = sysfs. pci_device ( bdf) . join ( "driver" ) ;
23- fs:: read_link ( & driver_link)
24- . ok ( )
25- . and_then ( |p| p. file_name ( ) . map ( |n| n. to_string_lossy ( ) . into_owned ( ) ) )
21+ sysfs. pci_device_ref ( bdf) . driver_name ( )
2622}
2723
2824/// Read vendor and device IDs from sysfs and format as `"VVVV DDDD"` (no `0x` prefix).
2925pub ( crate ) fn vfio_id_string ( sysfs : & SysfsRoot , bdf : & str ) -> Option < String > {
30- let dev_dir = sysfs. pci_device ( bdf) ;
31- let vendor = read_sysfs_trimmed ( & dev_dir. join ( "vendor" ) ) . ok ( ) ?;
32- let device = read_sysfs_trimmed ( & dev_dir. join ( "device" ) ) . ok ( ) ?;
33- let vendor_hex = vendor. strip_prefix ( "0x" ) . unwrap_or ( & vendor) ;
34- let device_hex = device. strip_prefix ( "0x" ) . unwrap_or ( & device) ;
35- Some ( format ! ( "{vendor_hex} {device_hex}" ) )
26+ sysfs. pci_device_ref ( bdf) . vfio_id_string ( )
3627}
3728
3829/// Best-effort registration of a device's vendor:device ID with `vfio-pci`.
@@ -159,9 +150,9 @@ pub(crate) fn clear_vfio_id_refcounts() {
159150/// with `driver_override=vfio-pci` and would silently re-bind to vfio-pci on
160151/// the next probe event.
161152fn cleanup_partial_bind ( sysfs : & SysfsRoot , bdf : & str ) {
162- let override_path = sysfs. pci_device ( bdf) . join ( "driver_override" ) ;
163- if override_path . exists ( )
164- && let Err ( err) = write_sysfs ( & override_path , " \n " )
153+ let device = sysfs. pci_device_ref ( bdf) ;
154+ if device . driver_override_path ( ) . exists ( )
155+ && let Err ( err) = device . clear_driver_override ( )
165156 {
166157 tracing:: warn!( bdf, %err, "failed to clear driver_override during bind rollback" ) ;
167158 }
@@ -179,7 +170,7 @@ pub(crate) fn bind_device_to_vfio(sysfs: &SysfsRoot, bdf: &str) -> Result<bool,
179170 if drv == "vfio-pci" {
180171 return Ok ( false ) ;
181172 }
182- let unbind_path = sysfs. pci_device ( bdf) . join ( "driver/unbind" ) ;
173+ let unbind_path = sysfs. pci_device_ref ( bdf) . driver_unbind_path ( ) ;
183174 write_sysfs ( & unbind_path, bdf) . map_err ( |e| VfioError :: BindFailed {
184175 bdf : bdf. to_string ( ) ,
185176 reason : format ! ( "unbind from {drv}: {e}" ) ,
@@ -189,7 +180,7 @@ pub(crate) fn bind_device_to_vfio(sysfs: &SysfsRoot, bdf: &str) -> Result<bool,
189180
190181 register_vfio_new_id ( sysfs, bdf) ;
191182
192- let override_path = sysfs. pci_device ( bdf) . join ( "driver_override" ) ;
183+ let override_path = sysfs. pci_device_ref ( bdf) . driver_override_path ( ) ;
193184 if let Err ( e) = write_sysfs ( & override_path, "vfio-pci" ) {
194185 deregister_vfio_new_id ( sysfs, bdf) ;
195186 return Err ( VfioError :: BindFailed {
@@ -251,7 +242,7 @@ pub(crate) fn restore_to_host_driver_ex(
251242 bdf : & str ,
252243 skip_deregister : bool ,
253244) -> Result < ( ) , VfioError > {
254- let dev_dir = sysfs. pci_device ( bdf) ;
245+ let device = sysfs. pci_device_ref ( bdf) ;
255246
256247 if !skip_deregister {
257248 // Deregister the device ID from vfio-pci's match table before
@@ -260,20 +251,21 @@ pub(crate) fn restore_to_host_driver_ex(
260251 deregister_vfio_new_id ( sysfs, bdf) ;
261252 }
262253
263- let unbind_path = dev_dir . join ( "driver/unbind" ) ;
254+ let unbind_path = device . driver_unbind_path ( ) ;
264255 if unbind_path. exists ( ) {
265256 write_sysfs ( & unbind_path, bdf) . map_err ( |e| VfioError :: UnbindFailed {
266257 bdf : bdf. to_string ( ) ,
267258 reason : format ! ( "unbind: {e}" ) ,
268259 } ) ?;
269260 }
270261
271- let override_path = dev_dir. join ( "driver_override" ) ;
272- if override_path. exists ( ) {
273- write_sysfs ( & override_path, "\n " ) . map_err ( |e| VfioError :: UnbindFailed {
274- bdf : bdf. to_string ( ) ,
275- reason : format ! ( "clear driver_override: {e}" ) ,
276- } ) ?;
262+ if device. driver_override_path ( ) . exists ( ) {
263+ device
264+ . clear_driver_override ( )
265+ . map_err ( |e| VfioError :: UnbindFailed {
266+ bdf : bdf. to_string ( ) ,
267+ reason : format ! ( "clear driver_override: {e}" ) ,
268+ } ) ?;
277269 }
278270
279271 let probe = sysfs. drivers_probe ( ) ;
0 commit comments