linux-kernelpci

How to find out physical slot number of a PCI(E) device with system calls?


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?


Solution

  • I found the answer to the question:

    static inline const char *pci_slot_name(const struct pci_slot *slot)
    

    https://elixir.bootlin.com/linux/v6.12/source/include/linux/pci.h#L84

    is the function that returns the physical slot number, same as lspci. It can be obtained from a struct pci_devpointer with:

    const char *phys_slot_num=pci_slot_name(/*struct pci_dev*/ pdev->slot);