- 論壇徽章:
- 0
|
2. 網(wǎng)卡在PCI層的注冊
2.1 數(shù)據(jù)結(jié)構(gòu)
前面第一章講了總線、設(shè)備以及驅(qū)動方面的關(guān)系,也講到了大多數(shù)網(wǎng)卡設(shè)備實際上是一個PCI設(shè)備。因此,本章就講解網(wǎng)卡設(shè)備在注冊時是如何注冊到PCI總線上去的。在這里,以Intel的E100網(wǎng)卡驅(qū)動進行講解。
前面講到每個PCI設(shè)備都由一組參數(shù)唯一地標識,這些參數(shù)保存在結(jié)構(gòu)體pci_device_id中,如下所示:
- struct pci_device_id {
- __u32 vendor, device; /* Vendor and device ID or PCI_ANY_ID*/
- __u32 subvendor, subdevice; /* Subsystem ID's or PCI_ANY_ID */
- __u32 class, class_mask; /* (class,subclass,prog-if) triplet */
- kernel_ulong_t driver_data; /* Data private to the driver */
- };
復制代碼
每個PCI設(shè)備驅(qū)動都有一個pci_driver變量,它描述了一個PCI驅(qū)動的信息,如下所示:
- struct pci_driver {
- struct list_head node;
- char *name;
- const struct pci_device_id *id_table; /* must be non-NULL for probe to be called */
- int (*probe) (struct pci_dev *dev, const struct pci_device_id *id); /* New device inserted */
- void (*remove) (struct pci_dev *dev); /* Device removed (NULL if not a hot-plug capable driver) */
- int (*suspend) (struct pci_dev *dev, pm_message_t state); /* Device suspended */
- int (*suspend_late) (struct pci_dev *dev, pm_message_t state);
- int (*resume_early) (struct pci_dev *dev);
- int (*resume) (struct pci_dev *dev); /* Device woken up */
- int (*enable_wake) (struct pci_dev *dev, pci_power_t state, int enable); /* Enable wake event */
- void (*shutdown) (struct pci_dev *dev);
- struct pci_error_handlers *err_handler;
- struct device_driver driver;
- struct pci_dynids dynids;
- int multithread_probe;
- };
復制代碼
每個PCI驅(qū)動中都有一個id_table成員變量,記錄了當前這個驅(qū)動所能夠進行驅(qū)動的那些設(shè)備的ID值。
對于E100網(wǎng)卡驅(qū)動來說,它的pci_driver變量定義為:
- static struct pci_driver e100_driver = {
- .name = DRV_NAME,
- .id_table = e100_id_table,
- .probe = e100_probe,
- .remove = __devexit_p(e100_remove),
- #ifdef CONFIG_PM
- /* Power Management hooks */
- .suspend = e100_suspend,
- .resume = e100_resume,
- #endif
- .shutdown = e100_shutdown,
- .err_handler = &e100_err_handler,
- };
復制代碼
里面e100_id_table就表示該E100驅(qū)動所能夠支持的PCI設(shè)備的ID號,其定義為:
- #define INTEL_8255X_ETHERNET_DEVICE(device_id, ich) {\
- PCI_VENDOR_ID_INTEL, device_id, PCI_ANY_ID, PCI_ANY_ID, \
- PCI_CLASS_NETWORK_ETHERNET << 8, 0xFFFF00, ich }
- static struct pci_device_id e100_id_table[] = {
- INTEL_8255X_ETHERNET_DEVICE(0x1029, 0),
- INTEL_8255X_ETHERNET_DEVICE(0x1030, 0),
- …
- { 0, }
- };
復制代碼
當PCI層檢測到一個PCI設(shè)備能夠被某PCI驅(qū)動所支持時(這是通過函數(shù)pci_match_one_device來進行檢測的),就會調(diào)用這個PCI驅(qū)動上的probe函數(shù),在該函數(shù)中會對該特定的PCI設(shè)備進行一些具體的初始化等操作。比如對于E100設(shè)備驅(qū)動來說,其probe函數(shù)為e100_probe。在這個函數(shù)中,會對網(wǎng)卡設(shè)備進行初始化。
e100_probe主要就涉及到網(wǎng)卡設(shè)備net_device的初始化,我們現(xiàn)在先來關(guān)注一下從網(wǎng)卡注冊一直到調(diào)用e100_probe這一個過程的整個流程。
2.2 E100初始化
E100驅(qū)動程序的初始化是在函數(shù)e100_init_module()中的,如下:
- static int __init e100_init_module(void)
- {
- if(((1 << debug) - 1) & NETIF_MSG_DRV) {
- printk(KERN_INFO PFX "%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
- printk(KERN_INFO PFX "%s\n", DRV_COPYRIGHT);
- }
- return pci_register_driver(&e100_driver);
- }
復制代碼
在這個函數(shù)中,調(diào)用了pci_register_driver()函數(shù),對e100_driver這個驅(qū)動進行注冊。
2.3 PCI注冊
在前面我們已經(jīng)看到,PCI的注冊就是將PCI驅(qū)動程序掛載到其所在的總線的drivers鏈,同時掃描PCI設(shè)備,將它能夠進行驅(qū)動的設(shè)備掛載到driver上的devices鏈表上來,這里,我們將詳細地查看這整個流程的函數(shù)調(diào)用關(guān)系。
pci_register_driver()->__pci_register_driver()
- /**
- * __pci_register_driver - register a new pci driver
- * @drv: the driver structure to register
- * @owner: owner module of drv
- * @mod_name: module name string
- *
- * Adds the driver structure to the list of registered drivers.
- * Returns a negative value on error, otherwise 0.
- * If no error occurred, the driver remains registered even if
- * no device was claimed during registration.
- */
- int __pci_register_driver(struct pci_driver *drv, struct module *owner, const char *mod_name);
- 在函數(shù)中有幾個初始化語句:
- drv->driver.name = drv->name;
- drv->driver.bus = &pci_bus_type;
- drv->driver.owner = owner;
- drv->driver.mod_name = mod_name;
復制代碼
即是將PCI設(shè)備中的driver變量的總線指向pci_bus_type這個總線描述符,同時設(shè)置驅(qū)動的名字等。
pci_bus_type定義如下:
- struct bus_type pci_bus_type = {
- .name = "pci",
- .match = pci_bus_match,
- .uevent = pci_uevent,
- .probe = pci_device_probe,
- .remove = pci_device_remove,
- .suspend = pci_device_suspend,
- .suspend_late = pci_device_suspend_late,
- .resume_early = pci_device_resume_early,
- .resume = pci_device_resume,
- .shutdown = pci_device_shutdown,
- .dev_attrs = pci_dev_attrs,
- };
復制代碼
然后再調(diào)用函數(shù)driver_register(&drv->driver);通過這個函數(shù)將這個PCI驅(qū)動中的struct device_driver driver成員變量注冊到系統(tǒng)中去。
pci_register_driver()->__pci_register_driver()->driver_register()
driver_register()代碼如下:
- /**
- * driver_register - register driver with bus
- * @drv: driver to register
- *
- * We pass off most of the work to the bus_add_driver() call,
- * since most of the things we have to do deal with the bus
- * structures.
- *
- * The one interesting aspect is that we setup @drv->unloaded
- * as a completion that gets complete when the driver reference
- * count reaches 0.
- */
- int driver_register(struct device_driver * drv)
- {
- if ((drv->bus->probe && drv->probe) ||
- (drv->bus->remove && drv->remove) ||
- (drv->bus->shutdown && drv->shutdown)) {
- printk(KERN_WARNING "Driver '%s' needs updating - please use bus_type methods\n", drv->name);
- }
- klist_init(&drv->klist_devices, NULL, NULL);
- init_completion(&drv->unloaded);
- return bus_add_driver(drv);
- }
復制代碼
klist_init()是為設(shè)備驅(qū)動的klist_devices成員進行初始化,這個klist_devices是一個對鏈表進行操作的包裹結(jié)構(gòu),它會鏈接這個驅(qū)動能夠支持的那些設(shè)備。
最后就調(diào)用bus_add_driver()函數(shù)。這個函數(shù)的功能就是將這個驅(qū)動加到其所在的總線的驅(qū)動鏈上。
pci_register_driver()->__pci_register_driver()->driver_register()->bus_add_driver()->driver_attach()
在bus_add_driver()函數(shù)中,最重要的是調(diào)用driver_attach()函數(shù),其定義如下:
- /**
- * driver_attach - try to bind driver to devices.
- * @drv: driver.
- *
- * Walk the list of devices that the bus has on it and try to
- * match the driver with each one. If driver_probe_device()
- * returns 0 and the @dev->driver is set, we've found a
- * compatible pair.
- */
- int driver_attach(struct device_driver * drv)
- {
- return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
- }
復制代碼
該函數(shù)遍歷這個驅(qū)動所在的總線上的所有設(shè)備,然后將這些設(shè)備與當前驅(qū)動進行匹配,以檢測這個驅(qū)動是否能夠支持某個設(shè)備,也即是將設(shè)備與驅(qū)動聯(lián)系起來。
bus_for_each_dev函數(shù)是掃描在drv->bus這個總線上的所有設(shè)備,然后將每個設(shè)備以及當前驅(qū)動這兩個指針傳遞給__driver_attach函數(shù)。
pci_register_driver()->__pci_register_driver()->driver_register()->bus_add_driver()->driver_attach()->__driver_attach()
__driver_attach()函數(shù)是將驅(qū)動與設(shè)備聯(lián)系起來的函數(shù)。
- static int __driver_attach(struct device * dev, void * data)
- {
- struct device_driver * drv = data;
- /*
- * Lock device and try to bind to it. We drop the error
- * here and always return 0, because we need to keep trying
- * to bind to devices and some drivers will return an error
- * simply if it didn't support the device.
- *
- * driver_probe_device() will spit a warning if there
- * is an error.
- */
- if (dev->parent) /* Needed for USB */
- down(&dev->parent->sem);
- down(&dev->sem);
- if (!dev->driver)
- driver_probe_device(drv, dev);
- up(&dev->sem);
- if (dev->parent)
- up(&dev->parent->sem);
- return 0;
- }
復制代碼
在函數(shù)中有兩條語句:
- if (!dev->driver)
- driver_probe_device(drv, dev);
復制代碼
也即是判斷當前設(shè)備是否已經(jīng)注冊了一個驅(qū)動,如果沒有注冊驅(qū)動,則調(diào)用driver_probe_device()函數(shù)。
pci_register_driver()->__pci_register_driver()->driver_register()->bus_add_driver()->driver_attach()->__driver_attach()->driver_probe_device()
如下:
- /**
- * driver_probe_device - attempt to bind device & driver together
- * @drv: driver to bind a device to
- * @dev: device to try to bind to the driver
- *
- * First, we call the bus's match function, if one present, which should
- * compare the device IDs the driver supports with the device IDs of the
- * device. Note we don't do this ourselves because we don't know the
- * format of the ID structures, nor what is to be considered a match and
- * what is not.
- *
- * This function returns 1 if a match is found, an error if one occurs
- * (that is not -ENODEV or -ENXIO), and 0 otherwise.
- *
- * This function must be called with @dev->sem held. When called for a
- * USB interface, @dev->parent->sem must be held as well.
- */
- int driver_probe_device(struct device_driver * drv, struct device * dev)
- {
- struct stupid_thread_structure *data;
- struct task_struct *probe_task;
- int ret = 0;
- if (!device_is_registered(dev))
- return -ENODEV;
- if (drv->bus->match && !drv->bus->match(dev, drv))
- goto done;
- pr_debug("%s: Matched Device %s with Driver %s\n",
- drv->bus->name, dev->bus_id, drv->name);
- data = kmalloc(sizeof(*data), GFP_KERNEL);
- if (!data)
- return -ENOMEM;
- data->drv = drv;
- data->dev = dev;
- if (drv->multithread_probe) {
- probe_task = kthread_run(really_probe, data,
- "probe-%s", dev->bus_id);
- if (IS_ERR(probe_task))
- ret = really_probe(data);
- } else
- ret = really_probe(data);
- done:
- return ret;
- }
復制代碼
該函數(shù)首先會調(diào)用總線上的match函數(shù),以判斷當前的PCI驅(qū)動能否支持該PCI設(shè)備,如果可以,則繼續(xù)往后面執(zhí)行。
drv->bus->match函數(shù)也即是pci_bus_type中的match成員變量,它為pci_bus_match函數(shù)。
pci_register_driver()->__pci_register_driver()->driver_register()->bus_add_driver()->driver_attach()->__driver_attach()->driver_probe_device()->pci_bus_match()
- /**
- * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
- * @dev: the PCI device structure to match against
- * @drv: the device driver to search for matching PCI device id structures
- *
- * Used by a driver to check whether a PCI device present in the
- * system is in its list of supported devices. Returns the matching
- * pci_device_id structure or %NULL if there is no match.
- */
- static int pci_bus_match(struct device *dev, struct device_driver *drv)
- {
- struct pci_dev *pci_dev = to_pci_dev(dev);
- struct pci_driver *pci_drv = to_pci_driver(drv);
- const struct pci_device_id *found_id;
- found_id = pci_match_device(pci_drv, pci_dev);
- if (found_id)
- return 1;
- return 0;
- }
復制代碼
pci_bus_match函數(shù)的作用就是將PCI設(shè)備與PCI驅(qū)動進行比較以檢查該驅(qū)動是否能夠支持這個設(shè)備。在函數(shù)的最前面是兩個宏to_pci_dev和to_pci_driver。因為在函數(shù)執(zhí)行的過程中,雖然最開始傳進來的是pci_driver結(jié)構(gòu)與pci_dev結(jié)構(gòu),但是在執(zhí)行的時候卻取了這兩個結(jié)構(gòu)體中的device_driver和device成員變量,所以現(xiàn)在就要通過這兩個成員變量找到之前對應的pci_driver和pci_dev結(jié)構(gòu)的地址。
#define to_pci_dev(n) container_of(n, struct pci_dev, dev)
#define to_pci_driver(drv) container_of(drv,struct pci_driver, driver)
這兩個宏在<Linux Device Driver> 3rd書上有相應的講解,這里也就是找到E100的pci_driver:e100_driver以及該網(wǎng)卡設(shè)備的pci_dev結(jié)構(gòu),F(xiàn)在就要對它們進行比較以看它們之間是否能夠聯(lián)系起來。這是通過函數(shù)pci_match_device實現(xiàn)的。
pci_register_driver()->__pci_register_driver()->driver_register()->bus_add_driver()->driver_attach()->__driver_attach()->driver_probe_device()->pci_bus_match()->pci_match_device()
- /**
- * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
- * @drv: the PCI driver to match against
- * @dev: the PCI device structure to match against
- *
- * Used by a driver to check whether a PCI device present in the
- * system is in its list of supported devices. Returns the matching
- * pci_device_id structure or %NULL if there is no match.
- */
- const struct pci_device_id *pci_match_device(struct pci_driver *drv,
- struct pci_dev *dev)
- {
- struct pci_dynid *dynid;
- /* Look at the dynamic ids first, before the static ones */
- spin_lock(&drv->dynids.lock);
- list_for_each_entry(dynid, &drv->dynids.list, node) {
- if (pci_match_one_device(&dynid->id, dev)) {
- spin_unlock(&drv->dynids.lock);
- return &dynid->id;
- }
- }
- spin_unlock(&drv->dynids.lock);
- return pci_match_id(drv->id_table, dev);
- }
復制代碼
pci_match_one_driver函數(shù)的作用是將一個PCI設(shè)備與PCI驅(qū)動進行比較,以查看它們是否相匹配。如果相匹配,則返回匹配的pci_device_id結(jié)構(gòu)體指針。
此時,如果該PCI驅(qū)動已經(jīng)找到了一個可以想符的PCI設(shè)備,則返回,然后再退回到之前的driver_probe_device函數(shù)中。在該函數(shù)最后將調(diào)用really_probe函數(shù)。將device_driver與device結(jié)構(gòu)體指針作為參數(shù)傳遞到這個函數(shù)中。下面幾行是調(diào)用驅(qū)動或者總線的probe函數(shù)來掃描設(shè)備。
pci_register_driver()->__pci_register_driver()->driver_register()->bus_add_driver()->driver_attach()->__driver_attach()->driver_probe_device()->really_probe()
在函數(shù)really_probe()中:
- if (dev->bus->probe) {
- ret = dev->bus->probe(dev);
- if (ret)
- goto probe_failed;
- } else if (drv->probe) {
- ret = drv->probe(dev);
- if (ret)
- goto probe_failed;
- }
復制代碼
此時的dev->bus為pci_bus_type,其probe函數(shù)則對應為:pci_device_probe。
pci_register_driver()->__pci_register_driver()->driver_register()->bus_add_driver()->driver_attach()->__driver_attach()->driver_probe_device()->really_probe()->pci_device_probe()
同樣,在該函數(shù)中會獲得當前的PCI設(shè)備的pci_dev結(jié)構(gòu)體指針以及PCI驅(qū)動程序的pci_driver結(jié)構(gòu)體指針。分別使用宏to_pci_dev和to_pci_driver。最后則調(diào)用函數(shù)__pci_device_probe。在該函數(shù)中還會調(diào)用函數(shù)pci_call_probe,這是最后的函數(shù)
pci_register_driver()->__pci_register_driver()->driver_register()->bus_add_driver()->driver_attach()->__driver_attach()->driver_probe_device()->really_probe()->pci_device_probe()->__pci_device_probe()->pci_call_probe()
在函數(shù)pci_call_probe里有一條語句:
- static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
- const struct pci_device_id *id)
- {
- int error;
- /* 省略 */
- error = drv->probe(dev, id);
復制代碼
在此處就調(diào)用了pci_driver的probe函數(shù),對于這里的E100驅(qū)動來說,它的probe函數(shù)是最開始注冊的e100_probe函數(shù),在該函數(shù)中會完成對網(wǎng)卡設(shè)備net_device的初始化等操作。
- pci_register_driver()->__pci_register_driver()->driver_register()->bus_add_driver()->driver_attach()->__driver_attach()->driver_probe_device()->really_probe()->pci_device_probe()->__pci_device_probe()->pci_call_probe()->e100_probe()
復制代碼
到這里,我們對網(wǎng)卡驅(qū)動的PCI層的初始化分析就告一個段落了,剩下的部分就是網(wǎng)卡驅(qū)動對網(wǎng)卡設(shè)備本身的初始化等操作。 |
|