when Linux calls PCI driver's probe function?
Asked Answered
M

2

9

Before registering a PCI driver, we have to initialize struct pci_driver and pass it to pci_register_driver. One of fields of the structure is a pointer to driver's probe function.

My question is - when the kernel calls driver's probe routine. Is it guaranteed to be right after the call to pci_register_driver or may happen any other time? What is determining this behaviour?

UPDATE pci_register_driver is a macro expanded into __pci_register_driver, which in turn calls driver_register, and driver_register calls bus_add_driver.

There's a following code in bus_add_driver:

if (drv->bus->p->drivers_autoprobe) {
        error = driver_attach(drv);
        if (error)
            goto out_unregister;
}

driver_attach will call bus_for_each_dev with argument __driver_attach, which will invoke driver_probe_device

And driver_probe_device ultimately calls really_probe:

if (dev->bus->probe) {
    ret = dev->bus->probe(dev);

The one thing I'm not sure about, is if the flag drivers_autoprobe gets set for pci_bus.

Miquelmiquela answered 9/7, 2015 at 23:28 Comment(1)
#5060001Abbatial
D
8

After the PCI core within your linux kernel has enumerated your device during the link training phase (this occurs by default at boot), it will gather information about the End Point devices connected to it, this includes the Vendor id, and the device id. The PCI core will then iterate through all of the drivers that have been registered to it with the function `pci_register_driver' and see if the driver supports this vendor/device combination.

A driver identifies that it supports that vendor/device combination using the struct pci_device_id id_table field of the pci_driver structure.

A typical implementation would look something like this:

#define VENDOR 0xBEEF // vendor of EP device
#define DEVICE 0x1111 // device id of EP

static struct pci_device_id module_dev_table[] = {
    { PCI_DEVICE(VENDOR, DEVICE)},
    {0, },
};

// PCI driver structure used to register this driver with the kernel
static struct pci_driver fpga_driver = {
    .id_table   = module_dev_table,
    .probe      = module_probe,
    .remove     = module_remove,
    .suspend    = module_suspend,
    .resume     = module_resume,
};

When the PCI core identifies your driver as a driver that supports a device on the bus, your probe function will then be called.

So to answer your question, NO, your probe function is not guaranteed to be called immediately after you register your driver, and almost certainly will not be. Your probe function will be called immediately after PCI core enumeration/Link training identifies a device which your driver supports.

Debbee answered 14/7, 2015 at 17:44 Comment(0)
B
1

When kernel detects a PCI device on PCI bus, kernel gets the device name based on the device tree. After this kernel scans through list of registered drivers if any of the drivers handle this device. If yes, then kernel will call probe of that particular driver.

Bradfordbradlee answered 10/7, 2015 at 0:32 Comment(2)
upon boot the kernel detects a device on the PCI bus, but the driver has not been registered; later we insmod the driver, how will the kernel pick up the driver's probe function to proceed? Is there a dedicated thread?Miquelmiquela
In init() of driver, you will be registering your driver to a particular bus. It is necessary for your driver to hook to a particular bus. This bus driver then call the the probe of newly added driver.Bradfordbradlee

© 2022 - 2024 — McMap. All rights reserved.