上篇文章已经分析了探测PCI总线的部分代码,碍于篇幅,这里另启一篇。重点分析下pci_scan_root_bus函数
pci_scan_root_bus函数
struct pci_bus *pci_scan_root_bus(struct device *parent,int bus,struct pci_ops *ops,void *sysdata,struct list_head *resources) {struct pci_host_bridge_window *window;bool found =false;struct pci_bus *b;int max;/*寻找bus的资源*/ list_for_each_entry(window, resources, list)if (window->res->flags & IORESOURCE_BUS) { found=true;break; }/*创建bus对应的结构*/ b= pci_create_root_bus(parent, bus, ops, sysdata, resources);if (!b)return NULL;if (!found) { dev_info(&b->dev,"No busn resource found for root bus, will use [bus %02x-ff]\n", bus); pci_bus_insert_busn_res(b, bus,255); }/*遍历子总线*/ max= pci_scan_child_bus(b);if (!found) pci_bus_update_busn_res_end(b, max); pci_bus_add_devices(b);return b; }
这里首先寻找bus总线号资源,前面在x86_pci_root_bus_resources函数中已经分配了,所以这里理论上是已经分配好了,不过还是验证下!!内核中总是精益求精。接着调用了pci_create_root_bus函数创建了对应的bus结构,然后调用pci_scan_child_bus函数遍历该总线下所有的子总线。最后就调用pci_bus_add_devices添加设备。总体上就是这么几步,但是要弄清楚,还真是不小的工作量。我们一步步来: