0

I'd like to find a way to figure out physical slot of a PCI-E device from the bus address. I would like to use to modify a driver/kernel module, so it would enumerate the devices (with the same ID) and disambiguate the device files according to physical slot. Like /dev/device_physslot . The driver will run on Ubuntu 18

lspci is capable to show physical slot number in the verbose presentation

However, as I found out, it accomplishes it over sysfs, which cannot be accessed from kernel module.

So I need to do it somehow with system calls. Or perhaps it is possible to figure out, where sysfs gets /sys/bus/pci/slots/slot_num/address property?

Andrey Pro
  • 501
  • 1
  • 10
  • 22
  • "it accomplishes it over sysfs, which cannot be accessed from kernel module." - A kernel module **can** access files, including the ones under sysfs. – Tsyvarev Mar 04 '22 at 23:20
  • @Tsyvarev Not really. https://stackoverflow.com/questions/275386/file-i-o-in-a-linux-kernel-module Also trying to replicate `lspci`'s method would also reqire to traverse sysfs directory structure (to discover enumerated physical slots), which is highly unlikely to be possible in kernel mode. – Andrey Pro Mar 05 '22 at 00:26
  • The kernel reads the DMI informstion. – stark Mar 05 '22 at 02:11
  • @AndreyPro: [The question](https://stackoverflow.com/questions/275386/file-i-o-in-a-linux-kernel-module) referenced by you doesn't tell that accessing files from the kernel is impossible, it just tells that this is generally a bad idea. As for iterating directory, it is also **possible** from the kernel: https://stackoverflow.com/questions/32617244/how-to-read-contents-of-a-directory-recursively-in-linux-kernel. – Tsyvarev Mar 05 '22 at 11:43
  • @Tsyvarev I looked into your suggestion and I can't understand how to use `dir_context` and `iterate_dir` to get the information about directory. – Andrey Pro Mar 05 '22 at 18:30
  • 1
    Usually, a udev rule is used to create symlinks to a kernel device name, with a more meaningful name. Udev rules cannot see the PCI physical slot number directly, but rules can run programs to do tricky stuff such as figuring out the physical slot number. – Ian Abbott Mar 07 '22 at 16:41
  • Is it too hard to read the source code? (Check _drivers/pci/slot.c_) – 0andriy Mar 15 '22 at 17:51

0 Answers0