probe.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  1. /*
  2. * probe.c - PCI detection and setup code
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/delay.h>
  6. #include <linux/init.h>
  7. #include <linux/pci.h>
  8. #include <linux/slab.h>
  9. #include <linux/module.h>
  10. #include <linux/cpumask.h>
  11. #include <linux/pci-aspm.h>
  12. #include "pci.h"
  13. #define CARDBUS_LATENCY_TIMER 176 /* secondary latency timer */
  14. #define CARDBUS_RESERVE_BUSNR 3
  15. #define PCI_CFG_SPACE_SIZE 256
  16. #define PCI_CFG_SPACE_EXP_SIZE 4096
  17. /* Ugh. Need to stop exporting this to modules. */
  18. LIST_HEAD(pci_root_buses);
  19. EXPORT_SYMBOL(pci_root_buses);
  20. static int find_anything(struct device *dev, void *data)
  21. {
  22. return 1;
  23. }
  24. /*
  25. * Some device drivers need know if pci is initiated.
  26. * Basically, we think pci is not initiated when there
  27. * is no device to be found on the pci_bus_type.
  28. */
  29. int no_pci_devices(void)
  30. {
  31. struct device *dev;
  32. int no_devices;
  33. dev = bus_find_device(&pci_bus_type, NULL, NULL, find_anything);
  34. no_devices = (dev == NULL);
  35. put_device(dev);
  36. return no_devices;
  37. }
  38. EXPORT_SYMBOL(no_pci_devices);
  39. #ifdef HAVE_PCI_LEGACY
  40. /**
  41. * pci_create_legacy_files - create legacy I/O port and memory files
  42. * @b: bus to create files under
  43. *
  44. * Some platforms allow access to legacy I/O port and ISA memory space on
  45. * a per-bus basis. This routine creates the files and ties them into
  46. * their associated read, write and mmap files from pci-sysfs.c
  47. */
  48. static void pci_create_legacy_files(struct pci_bus *b)
  49. {
  50. b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
  51. GFP_ATOMIC);
  52. if (b->legacy_io) {
  53. b->legacy_io->attr.name = "legacy_io";
  54. b->legacy_io->size = 0xffff;
  55. b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
  56. b->legacy_io->read = pci_read_legacy_io;
  57. b->legacy_io->write = pci_write_legacy_io;
  58. device_create_bin_file(&b->dev, b->legacy_io);
  59. /* Allocated above after the legacy_io struct */
  60. b->legacy_mem = b->legacy_io + 1;
  61. b->legacy_mem->attr.name = "legacy_mem";
  62. b->legacy_mem->size = 1024*1024;
  63. b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
  64. b->legacy_mem->mmap = pci_mmap_legacy_mem;
  65. device_create_bin_file(&b->dev, b->legacy_mem);
  66. }
  67. }
  68. void pci_remove_legacy_files(struct pci_bus *b)
  69. {
  70. if (b->legacy_io) {
  71. device_remove_bin_file(&b->dev, b->legacy_io);
  72. device_remove_bin_file(&b->dev, b->legacy_mem);
  73. kfree(b->legacy_io); /* both are allocated here */
  74. }
  75. }
  76. #else /* !HAVE_PCI_LEGACY */
  77. static inline void pci_create_legacy_files(struct pci_bus *bus) { return; }
  78. void pci_remove_legacy_files(struct pci_bus *bus) { return; }
  79. #endif /* HAVE_PCI_LEGACY */
  80. /*
  81. * PCI Bus Class Devices
  82. */
  83. static ssize_t pci_bus_show_cpuaffinity(struct device *dev,
  84. int type,
  85. struct device_attribute *attr,
  86. char *buf)
  87. {
  88. int ret;
  89. cpumask_t cpumask;
  90. cpumask = pcibus_to_cpumask(to_pci_bus(dev));
  91. ret = type?
  92. cpulist_scnprintf(buf, PAGE_SIZE-2, cpumask):
  93. cpumask_scnprintf(buf, PAGE_SIZE-2, cpumask);
  94. buf[ret++] = '\n';
  95. buf[ret] = '\0';
  96. return ret;
  97. }
  98. static ssize_t inline pci_bus_show_cpumaskaffinity(struct device *dev,
  99. struct device_attribute *attr,
  100. char *buf)
  101. {
  102. return pci_bus_show_cpuaffinity(dev, 0, attr, buf);
  103. }
  104. static ssize_t inline pci_bus_show_cpulistaffinity(struct device *dev,
  105. struct device_attribute *attr,
  106. char *buf)
  107. {
  108. return pci_bus_show_cpuaffinity(dev, 1, attr, buf);
  109. }
  110. DEVICE_ATTR(cpuaffinity, S_IRUGO, pci_bus_show_cpumaskaffinity, NULL);
  111. DEVICE_ATTR(cpulistaffinity, S_IRUGO, pci_bus_show_cpulistaffinity, NULL);
  112. /*
  113. * PCI Bus Class
  114. */
  115. static void release_pcibus_dev(struct device *dev)
  116. {
  117. struct pci_bus *pci_bus = to_pci_bus(dev);
  118. if (pci_bus->bridge)
  119. put_device(pci_bus->bridge);
  120. kfree(pci_bus);
  121. }
  122. static struct class pcibus_class = {
  123. .name = "pci_bus",
  124. .dev_release = &release_pcibus_dev,
  125. };
  126. static int __init pcibus_class_init(void)
  127. {
  128. return class_register(&pcibus_class);
  129. }
  130. postcore_initcall(pcibus_class_init);
  131. /*
  132. * Translate the low bits of the PCI base
  133. * to the resource type
  134. */
  135. static inline unsigned int pci_calc_resource_flags(unsigned int flags)
  136. {
  137. if (flags & PCI_BASE_ADDRESS_SPACE_IO)
  138. return IORESOURCE_IO;
  139. if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
  140. return IORESOURCE_MEM | IORESOURCE_PREFETCH;
  141. return IORESOURCE_MEM;
  142. }
  143. static u64 pci_size(u64 base, u64 maxbase, u64 mask)
  144. {
  145. u64 size = mask & maxbase; /* Find the significant bits */
  146. if (!size)
  147. return 0;
  148. /* Get the lowest of them to find the decode size, and
  149. from that the extent. */
  150. size = (size & ~(size-1)) - 1;
  151. /* base == maxbase can be valid only if the BAR has
  152. already been programmed with all 1s. */
  153. if (base == maxbase && ((base | size) & mask) != mask)
  154. return 0;
  155. return size;
  156. }
  157. enum pci_bar_type {
  158. pci_bar_unknown, /* Standard PCI BAR probe */
  159. pci_bar_io, /* An io port BAR */
  160. pci_bar_mem32, /* A 32-bit memory BAR */
  161. pci_bar_mem64, /* A 64-bit memory BAR */
  162. };
  163. static inline enum pci_bar_type decode_bar(struct resource *res, u32 bar)
  164. {
  165. if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
  166. res->flags = bar & ~PCI_BASE_ADDRESS_IO_MASK;
  167. return pci_bar_io;
  168. }
  169. res->flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK;
  170. if (res->flags == PCI_BASE_ADDRESS_MEM_TYPE_64)
  171. return pci_bar_mem64;
  172. return pci_bar_mem32;
  173. }
  174. /*
  175. * If the type is not unknown, we assume that the lowest bit is 'enable'.
  176. * Returns 1 if the BAR was 64-bit and 0 if it was 32-bit.
  177. */
  178. static int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
  179. struct resource *res, unsigned int pos)
  180. {
  181. u32 l, sz, mask;
  182. mask = type ? ~PCI_ROM_ADDRESS_ENABLE : ~0;
  183. res->name = pci_name(dev);
  184. pci_read_config_dword(dev, pos, &l);
  185. pci_write_config_dword(dev, pos, mask);
  186. pci_read_config_dword(dev, pos, &sz);
  187. pci_write_config_dword(dev, pos, l);
  188. /*
  189. * All bits set in sz means the device isn't working properly.
  190. * If the BAR isn't implemented, all bits must be 0. If it's a
  191. * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit
  192. * 1 must be clear.
  193. */
  194. if (!sz || sz == 0xffffffff)
  195. goto fail;
  196. /*
  197. * I don't know how l can have all bits set. Copied from old code.
  198. * Maybe it fixes a bug on some ancient platform.
  199. */
  200. if (l == 0xffffffff)
  201. l = 0;
  202. if (type == pci_bar_unknown) {
  203. type = decode_bar(res, l);
  204. res->flags |= pci_calc_resource_flags(l) | IORESOURCE_SIZEALIGN;
  205. if (type == pci_bar_io) {
  206. l &= PCI_BASE_ADDRESS_IO_MASK;
  207. mask = PCI_BASE_ADDRESS_IO_MASK & 0xffff;
  208. } else {
  209. l &= PCI_BASE_ADDRESS_MEM_MASK;
  210. mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
  211. }
  212. } else {
  213. res->flags |= (l & IORESOURCE_ROM_ENABLE);
  214. l &= PCI_ROM_ADDRESS_MASK;
  215. mask = (u32)PCI_ROM_ADDRESS_MASK;
  216. }
  217. if (type == pci_bar_mem64) {
  218. u64 l64 = l;
  219. u64 sz64 = sz;
  220. u64 mask64 = mask | (u64)~0 << 32;
  221. pci_read_config_dword(dev, pos + 4, &l);
  222. pci_write_config_dword(dev, pos + 4, ~0);
  223. pci_read_config_dword(dev, pos + 4, &sz);
  224. pci_write_config_dword(dev, pos + 4, l);
  225. l64 |= ((u64)l << 32);
  226. sz64 |= ((u64)sz << 32);
  227. sz64 = pci_size(l64, sz64, mask64);
  228. if (!sz64)
  229. goto fail;
  230. if ((BITS_PER_LONG < 64) && (sz64 > 0x100000000ULL)) {
  231. dev_err(&dev->dev, "can't handle 64-bit BAR\n");
  232. goto fail;
  233. } else if ((BITS_PER_LONG < 64) && l) {
  234. /* Address above 32-bit boundary; disable the BAR */
  235. pci_write_config_dword(dev, pos, 0);
  236. pci_write_config_dword(dev, pos + 4, 0);
  237. res->start = 0;
  238. res->end = sz64;
  239. } else {
  240. res->start = l64;
  241. res->end = l64 + sz64;
  242. }
  243. } else {
  244. sz = pci_size(l, sz, mask);
  245. if (!sz)
  246. goto fail;
  247. res->start = l;
  248. res->end = l + sz;
  249. }
  250. out:
  251. return (type == pci_bar_mem64) ? 1 : 0;
  252. fail:
  253. res->flags = 0;
  254. goto out;
  255. }
  256. static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
  257. {
  258. unsigned int pos, reg;
  259. for (pos = 0; pos < howmany; pos++) {
  260. struct resource *res = &dev->resource[pos];
  261. reg = PCI_BASE_ADDRESS_0 + (pos << 2);
  262. pos += __pci_read_base(dev, pci_bar_unknown, res, reg);
  263. }
  264. if (rom) {
  265. struct resource *res = &dev->resource[PCI_ROM_RESOURCE];
  266. dev->rom_base_reg = rom;
  267. res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH |
  268. IORESOURCE_READONLY | IORESOURCE_CACHEABLE |
  269. IORESOURCE_SIZEALIGN;
  270. __pci_read_base(dev, pci_bar_mem32, res, rom);
  271. }
  272. }
  273. void __devinit pci_read_bridge_bases(struct pci_bus *child)
  274. {
  275. struct pci_dev *dev = child->self;
  276. u8 io_base_lo, io_limit_lo;
  277. u16 mem_base_lo, mem_limit_lo;
  278. unsigned long base, limit;
  279. struct resource *res;
  280. int i;
  281. if (!dev) /* It's a host bus, nothing to read */
  282. return;
  283. if (dev->transparent) {
  284. dev_info(&dev->dev, "transparent bridge\n");
  285. for(i = 3; i < PCI_BUS_NUM_RESOURCES; i++)
  286. child->resource[i] = child->parent->resource[i - 3];
  287. }
  288. for(i=0; i<3; i++)
  289. child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];
  290. res = child->resource[0];
  291. pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
  292. pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
  293. base = (io_base_lo & PCI_IO_RANGE_MASK) << 8;
  294. limit = (io_limit_lo & PCI_IO_RANGE_MASK) << 8;
  295. if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
  296. u16 io_base_hi, io_limit_hi;
  297. pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
  298. pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
  299. base |= (io_base_hi << 16);
  300. limit |= (io_limit_hi << 16);
  301. }
  302. if (base <= limit) {
  303. res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
  304. if (!res->start)
  305. res->start = base;
  306. if (!res->end)
  307. res->end = limit + 0xfff;
  308. }
  309. res = child->resource[1];
  310. pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
  311. pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
  312. base = (mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
  313. limit = (mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
  314. if (base <= limit) {
  315. res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM;
  316. res->start = base;
  317. res->end = limit + 0xfffff;
  318. }
  319. res = child->resource[2];
  320. pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
  321. pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
  322. base = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
  323. limit = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
  324. if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
  325. u32 mem_base_hi, mem_limit_hi;
  326. pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
  327. pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
  328. /*
  329. * Some bridges set the base > limit by default, and some
  330. * (broken) BIOSes do not initialize them. If we find
  331. * this, just assume they are not being used.
  332. */
  333. if (mem_base_hi <= mem_limit_hi) {
  334. #if BITS_PER_LONG == 64
  335. base |= ((long) mem_base_hi) << 32;
  336. limit |= ((long) mem_limit_hi) << 32;
  337. #else
  338. if (mem_base_hi || mem_limit_hi) {
  339. dev_err(&dev->dev, "can't handle 64-bit "
  340. "address space for bridge\n");
  341. return;
  342. }
  343. #endif
  344. }
  345. }
  346. if (base <= limit) {
  347. res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM | IORESOURCE_PREFETCH;
  348. res->start = base;
  349. res->end = limit + 0xfffff;
  350. }
  351. }
  352. static struct pci_bus * pci_alloc_bus(void)
  353. {
  354. struct pci_bus *b;
  355. b = kzalloc(sizeof(*b), GFP_KERNEL);
  356. if (b) {
  357. INIT_LIST_HEAD(&b->node);
  358. INIT_LIST_HEAD(&b->children);
  359. INIT_LIST_HEAD(&b->devices);
  360. INIT_LIST_HEAD(&b->slots);
  361. }
  362. return b;
  363. }
  364. static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
  365. struct pci_dev *bridge, int busnr)
  366. {
  367. struct pci_bus *child;
  368. int i;
  369. /*
  370. * Allocate a new bus, and inherit stuff from the parent..
  371. */
  372. child = pci_alloc_bus();
  373. if (!child)
  374. return NULL;
  375. child->self = bridge;
  376. child->parent = parent;
  377. child->ops = parent->ops;
  378. child->sysdata = parent->sysdata;
  379. child->bus_flags = parent->bus_flags;
  380. child->bridge = get_device(&bridge->dev);
  381. /* initialize some portions of the bus device, but don't register it
  382. * now as the parent is not properly set up yet. This device will get
  383. * registered later in pci_bus_add_devices()
  384. */
  385. child->dev.class = &pcibus_class;
  386. sprintf(child->dev.bus_id, "%04x:%02x", pci_domain_nr(child), busnr);
  387. /*
  388. * Set up the primary, secondary and subordinate
  389. * bus numbers.
  390. */
  391. child->number = child->secondary = busnr;
  392. child->primary = parent->secondary;
  393. child->subordinate = 0xff;
  394. /* Set up default resource pointers and names.. */
  395. for (i = 0; i < 4; i++) {
  396. child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];
  397. child->resource[i]->name = child->name;
  398. }
  399. bridge->subordinate = child;
  400. return child;
  401. }
  402. struct pci_bus *__ref pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev, int busnr)
  403. {
  404. struct pci_bus *child;
  405. child = pci_alloc_child_bus(parent, dev, busnr);
  406. if (child) {
  407. down_write(&pci_bus_sem);
  408. list_add_tail(&child->node, &parent->children);
  409. up_write(&pci_bus_sem);
  410. }
  411. return child;
  412. }
  413. static void pci_fixup_parent_subordinate_busnr(struct pci_bus *child, int max)
  414. {
  415. struct pci_bus *parent = child->parent;
  416. /* Attempts to fix that up are really dangerous unless
  417. we're going to re-assign all bus numbers. */
  418. if (!pcibios_assign_all_busses())
  419. return;
  420. while (parent->parent && parent->subordinate < max) {
  421. parent->subordinate = max;
  422. pci_write_config_byte(parent->self, PCI_SUBORDINATE_BUS, max);
  423. parent = parent->parent;
  424. }
  425. }
  426. /*
  427. * If it's a bridge, configure it and scan the bus behind it.
  428. * For CardBus bridges, we don't scan behind as the devices will
  429. * be handled by the bridge driver itself.
  430. *
  431. * We need to process bridges in two passes -- first we scan those
  432. * already configured by the BIOS and after we are done with all of
  433. * them, we proceed to assigning numbers to the remaining buses in
  434. * order to avoid overlaps between old and new bus numbers.
  435. */
  436. int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass)
  437. {
  438. struct pci_bus *child;
  439. int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
  440. u32 buses, i, j = 0;
  441. u16 bctl;
  442. pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
  443. dev_dbg(&dev->dev, "scanning behind bridge, config %06x, pass %d\n",
  444. buses & 0xffffff, pass);
  445. /* Disable MasterAbortMode during probing to avoid reporting
  446. of bus errors (in some architectures) */
  447. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);
  448. pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
  449. bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
  450. if ((buses & 0xffff00) && !pcibios_assign_all_busses() && !is_cardbus) {
  451. unsigned int cmax, busnr;
  452. /*
  453. * Bus already configured by firmware, process it in the first
  454. * pass and just note the configuration.
  455. */
  456. if (pass)
  457. goto out;
  458. busnr = (buses >> 8) & 0xFF;
  459. /*
  460. * If we already got to this bus through a different bridge,
  461. * ignore it. This can happen with the i450NX chipset.
  462. */
  463. if (pci_find_bus(pci_domain_nr(bus), busnr)) {
  464. dev_info(&dev->dev, "bus %04x:%02x already known\n",
  465. pci_domain_nr(bus), busnr);
  466. goto out;
  467. }
  468. child = pci_add_new_bus(bus, dev, busnr);
  469. if (!child)
  470. goto out;
  471. child->primary = buses & 0xFF;
  472. child->subordinate = (buses >> 16) & 0xFF;
  473. child->bridge_ctl = bctl;
  474. cmax = pci_scan_child_bus(child);
  475. if (cmax > max)
  476. max = cmax;
  477. if (child->subordinate > max)
  478. max = child->subordinate;
  479. } else {
  480. /*
  481. * We need to assign a number to this bus which we always
  482. * do in the second pass.
  483. */
  484. if (!pass) {
  485. if (pcibios_assign_all_busses())
  486. /* Temporarily disable forwarding of the
  487. configuration cycles on all bridges in
  488. this bus segment to avoid possible
  489. conflicts in the second pass between two
  490. bridges programmed with overlapping
  491. bus ranges. */
  492. pci_write_config_dword(dev, PCI_PRIMARY_BUS,
  493. buses & ~0xffffff);
  494. goto out;
  495. }
  496. /* Clear errors */
  497. pci_write_config_word(dev, PCI_STATUS, 0xffff);
  498. /* Prevent assigning a bus number that already exists.
  499. * This can happen when a bridge is hot-plugged */
  500. if (pci_find_bus(pci_domain_nr(bus), max+1))
  501. goto out;
  502. child = pci_add_new_bus(bus, dev, ++max);
  503. buses = (buses & 0xff000000)
  504. | ((unsigned int)(child->primary) << 0)
  505. | ((unsigned int)(child->secondary) << 8)
  506. | ((unsigned int)(child->subordinate) << 16);
  507. /*
  508. * yenta.c forces a secondary latency timer of 176.
  509. * Copy that behaviour here.
  510. */
  511. if (is_cardbus) {
  512. buses &= ~0xff000000;
  513. buses |= CARDBUS_LATENCY_TIMER << 24;
  514. }
  515. /*
  516. * We need to blast all three values with a single write.
  517. */
  518. pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
  519. if (!is_cardbus) {
  520. child->bridge_ctl = bctl;
  521. /*
  522. * Adjust subordinate busnr in parent buses.
  523. * We do this before scanning for children because
  524. * some devices may not be detected if the bios
  525. * was lazy.
  526. */
  527. pci_fixup_parent_subordinate_busnr(child, max);
  528. /* Now we can scan all subordinate buses... */
  529. max = pci_scan_child_bus(child);
  530. /*
  531. * now fix it up again since we have found
  532. * the real value of max.
  533. */
  534. pci_fixup_parent_subordinate_busnr(child, max);
  535. } else {
  536. /*
  537. * For CardBus bridges, we leave 4 bus numbers
  538. * as cards with a PCI-to-PCI bridge can be
  539. * inserted later.
  540. */
  541. for (i=0; i<CARDBUS_RESERVE_BUSNR; i++) {
  542. struct pci_bus *parent = bus;
  543. if (pci_find_bus(pci_domain_nr(bus),
  544. max+i+1))
  545. break;
  546. while (parent->parent) {
  547. if ((!pcibios_assign_all_busses()) &&
  548. (parent->subordinate > max) &&
  549. (parent->subordinate <= max+i)) {
  550. j = 1;
  551. }
  552. parent = parent->parent;
  553. }
  554. if (j) {
  555. /*
  556. * Often, there are two cardbus bridges
  557. * -- try to leave one valid bus number
  558. * for each one.
  559. */
  560. i /= 2;
  561. break;
  562. }
  563. }
  564. max += i;
  565. pci_fixup_parent_subordinate_busnr(child, max);
  566. }
  567. /*
  568. * Set the subordinate bus number to its real value.
  569. */
  570. child->subordinate = max;
  571. pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
  572. }
  573. sprintf(child->name,
  574. (is_cardbus ? "PCI CardBus %04x:%02x" : "PCI Bus %04x:%02x"),
  575. pci_domain_nr(bus), child->number);
  576. /* Has only triggered on CardBus, fixup is in yenta_socket */
  577. while (bus->parent) {
  578. if ((child->subordinate > bus->subordinate) ||
  579. (child->number > bus->subordinate) ||
  580. (child->number < bus->number) ||
  581. (child->subordinate < bus->number)) {
  582. pr_debug("PCI: Bus #%02x (-#%02x) is %s "
  583. "hidden behind%s bridge #%02x (-#%02x)\n",
  584. child->number, child->subordinate,
  585. (bus->number > child->subordinate &&
  586. bus->subordinate < child->number) ?
  587. "wholly" : "partially",
  588. bus->self->transparent ? " transparent" : "",
  589. bus->number, bus->subordinate);
  590. }
  591. bus = bus->parent;
  592. }
  593. out:
  594. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
  595. return max;
  596. }
  597. /*
  598. * Read interrupt line and base address registers.
  599. * The architecture-dependent code can tweak these, of course.
  600. */
  601. static void pci_read_irq(struct pci_dev *dev)
  602. {
  603. unsigned char irq;
  604. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
  605. dev->pin = irq;
  606. if (irq)
  607. pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
  608. dev->irq = irq;
  609. }
  610. #define LEGACY_IO_RESOURCE (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
  611. /**
  612. * pci_setup_device - fill in class and map information of a device
  613. * @dev: the device structure to fill
  614. *
  615. * Initialize the device structure with information about the device's
  616. * vendor,class,memory and IO-space addresses,IRQ lines etc.
  617. * Called at initialisation of the PCI subsystem and by CardBus services.
  618. * Returns 0 on success and -1 if unknown type of device (not normal, bridge
  619. * or CardBus).
  620. */
  621. static int pci_setup_device(struct pci_dev * dev)
  622. {
  623. u32 class;
  624. dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
  625. dev->bus->number, PCI_SLOT(dev->devfn),
  626. PCI_FUNC(dev->devfn));
  627. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
  628. dev->revision = class & 0xff;
  629. class >>= 8; /* upper 3 bytes */
  630. dev->class = class;
  631. class >>= 8;
  632. dev_dbg(&dev->dev, "found [%04x/%04x] class %06x header type %02x\n",
  633. dev->vendor, dev->device, class, dev->hdr_type);
  634. /* "Unknown power state" */
  635. dev->current_state = PCI_UNKNOWN;
  636. /* Early fixups, before probing the BARs */
  637. pci_fixup_device(pci_fixup_early, dev);
  638. class = dev->class >> 8;
  639. switch (dev->hdr_type) { /* header type */
  640. case PCI_HEADER_TYPE_NORMAL: /* standard header */
  641. if (class == PCI_CLASS_BRIDGE_PCI)
  642. goto bad;
  643. pci_read_irq(dev);
  644. pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
  645. pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
  646. pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device);
  647. /*
  648. * Do the ugly legacy mode stuff here rather than broken chip
  649. * quirk code. Legacy mode ATA controllers have fixed
  650. * addresses. These are not always echoed in BAR0-3, and
  651. * BAR0-3 in a few cases contain junk!
  652. */
  653. if (class == PCI_CLASS_STORAGE_IDE) {
  654. u8 progif;
  655. pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
  656. if ((progif & 1) == 0) {
  657. dev->resource[0].start = 0x1F0;
  658. dev->resource[0].end = 0x1F7;
  659. dev->resource[0].flags = LEGACY_IO_RESOURCE;
  660. dev->resource[1].start = 0x3F6;
  661. dev->resource[1].end = 0x3F6;
  662. dev->resource[1].flags = LEGACY_IO_RESOURCE;
  663. }
  664. if ((progif & 4) == 0) {
  665. dev->resource[2].start = 0x170;
  666. dev->resource[2].end = 0x177;
  667. dev->resource[2].flags = LEGACY_IO_RESOURCE;
  668. dev->resource[3].start = 0x376;
  669. dev->resource[3].end = 0x376;
  670. dev->resource[3].flags = LEGACY_IO_RESOURCE;
  671. }
  672. }
  673. break;
  674. case PCI_HEADER_TYPE_BRIDGE: /* bridge header */
  675. if (class != PCI_CLASS_BRIDGE_PCI)
  676. goto bad;
  677. /* The PCI-to-PCI bridge spec requires that subtractive
  678. decoding (i.e. transparent) bridge must have programming
  679. interface code of 0x01. */
  680. pci_read_irq(dev);
  681. dev->transparent = ((dev->class & 0xff) == 1);
  682. pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
  683. break;
  684. case PCI_HEADER_TYPE_CARDBUS: /* CardBus bridge header */
  685. if (class != PCI_CLASS_BRIDGE_CARDBUS)
  686. goto bad;
  687. pci_read_irq(dev);
  688. pci_read_bases(dev, 1, 0);
  689. pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
  690. pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
  691. break;
  692. default: /* unknown header */
  693. dev_err(&dev->dev, "unknown header type %02x, "
  694. "ignoring device\n", dev->hdr_type);
  695. return -1;
  696. bad:
  697. dev_err(&dev->dev, "ignoring class %02x (doesn't match header "
  698. "type %02x)\n", class, dev->hdr_type);
  699. dev->class = PCI_CLASS_NOT_DEFINED;
  700. }
  701. /* We found a fine healthy device, go go go... */
  702. return 0;
  703. }
  704. /**
  705. * pci_release_dev - free a pci device structure when all users of it are finished.
  706. * @dev: device that's been disconnected
  707. *
  708. * Will be called only by the device core when all users of this pci device are
  709. * done.
  710. */
  711. static void pci_release_dev(struct device *dev)
  712. {
  713. struct pci_dev *pci_dev;
  714. pci_dev = to_pci_dev(dev);
  715. pci_vpd_release(pci_dev);
  716. kfree(pci_dev);
  717. }
  718. static void set_pcie_port_type(struct pci_dev *pdev)
  719. {
  720. int pos;
  721. u16 reg16;
  722. pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  723. if (!pos)
  724. return;
  725. pdev->is_pcie = 1;
  726. pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, &reg16);
  727. pdev->pcie_type = (reg16 & PCI_EXP_FLAGS_TYPE) >> 4;
  728. }
  729. /**
  730. * pci_cfg_space_size - get the configuration space size of the PCI device.
  731. * @dev: PCI device
  732. *
  733. * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
  734. * have 4096 bytes. Even if the device is capable, that doesn't mean we can
  735. * access it. Maybe we don't have a way to generate extended config space
  736. * accesses, or the device is behind a reverse Express bridge. So we try
  737. * reading the dword at 0x100 which must either be 0 or a valid extended
  738. * capability header.
  739. */
  740. int pci_cfg_space_size_ext(struct pci_dev *dev)
  741. {
  742. u32 status;
  743. if (pci_read_config_dword(dev, 256, &status) != PCIBIOS_SUCCESSFUL)
  744. goto fail;
  745. if (status == 0xffffffff)
  746. goto fail;
  747. return PCI_CFG_SPACE_EXP_SIZE;
  748. fail:
  749. return PCI_CFG_SPACE_SIZE;
  750. }
  751. int pci_cfg_space_size(struct pci_dev *dev)
  752. {
  753. int pos;
  754. u32 status;
  755. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  756. if (!pos) {
  757. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  758. if (!pos)
  759. goto fail;
  760. pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);
  761. if (!(status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ)))
  762. goto fail;
  763. }
  764. return pci_cfg_space_size_ext(dev);
  765. fail:
  766. return PCI_CFG_SPACE_SIZE;
  767. }
  768. static void pci_release_bus_bridge_dev(struct device *dev)
  769. {
  770. kfree(dev);
  771. }
  772. struct pci_dev *alloc_pci_dev(void)
  773. {
  774. struct pci_dev *dev;
  775. dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
  776. if (!dev)
  777. return NULL;
  778. INIT_LIST_HEAD(&dev->bus_list);
  779. pci_msi_init_pci_dev(dev);
  780. return dev;
  781. }
  782. EXPORT_SYMBOL(alloc_pci_dev);
  783. /*
  784. * Read the config data for a PCI device, sanity-check it
  785. * and fill in the dev structure...
  786. */
  787. static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
  788. {
  789. struct pci_dev *dev;
  790. u32 l;
  791. u8 hdr_type;
  792. int delay = 1;
  793. if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, &l))
  794. return NULL;
  795. /* some broken boards return 0 or ~0 if a slot is empty: */
  796. if (l == 0xffffffff || l == 0x00000000 ||
  797. l == 0x0000ffff || l == 0xffff0000)
  798. return NULL;
  799. /* Configuration request Retry Status */
  800. while (l == 0xffff0001) {
  801. msleep(delay);
  802. delay *= 2;
  803. if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, &l))
  804. return NULL;
  805. /* Card hasn't responded in 60 seconds? Must be stuck. */
  806. if (delay > 60 * 1000) {
  807. printk(KERN_WARNING "pci %04x:%02x:%02x.%d: not "
  808. "responding\n", pci_domain_nr(bus),
  809. bus->number, PCI_SLOT(devfn),
  810. PCI_FUNC(devfn));
  811. return NULL;
  812. }
  813. }
  814. if (pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type))
  815. return NULL;
  816. dev = alloc_pci_dev();
  817. if (!dev)
  818. return NULL;
  819. dev->bus = bus;
  820. dev->sysdata = bus->sysdata;
  821. dev->dev.parent = bus->bridge;
  822. dev->dev.bus = &pci_bus_type;
  823. dev->devfn = devfn;
  824. dev->hdr_type = hdr_type & 0x7f;
  825. dev->multifunction = !!(hdr_type & 0x80);
  826. dev->vendor = l & 0xffff;
  827. dev->device = (l >> 16) & 0xffff;
  828. dev->cfg_size = pci_cfg_space_size(dev);
  829. dev->error_state = pci_channel_io_normal;
  830. set_pcie_port_type(dev);
  831. /* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
  832. set this higher, assuming the system even supports it. */
  833. dev->dma_mask = 0xffffffff;
  834. if (pci_setup_device(dev) < 0) {
  835. kfree(dev);
  836. return NULL;
  837. }
  838. pci_vpd_pci22_init(dev);
  839. return dev;
  840. }
  841. void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
  842. {
  843. device_initialize(&dev->dev);
  844. dev->dev.release = pci_release_dev;
  845. pci_dev_get(dev);
  846. dev->dev.dma_mask = &dev->dma_mask;
  847. dev->dev.dma_parms = &dev->dma_parms;
  848. dev->dev.coherent_dma_mask = 0xffffffffull;
  849. pci_set_dma_max_seg_size(dev, 65536);
  850. pci_set_dma_seg_boundary(dev, 0xffffffff);
  851. /* Fix up broken headers */
  852. pci_fixup_device(pci_fixup_header, dev);
  853. /* Initialize power management of the device */
  854. pci_pm_init(dev);
  855. /*
  856. * Add the device to our list of discovered devices
  857. * and the bus list for fixup functions, etc.
  858. */
  859. down_write(&pci_bus_sem);
  860. list_add_tail(&dev->bus_list, &bus->devices);
  861. up_write(&pci_bus_sem);
  862. }
  863. struct pci_dev *__ref pci_scan_single_device(struct pci_bus *bus, int devfn)
  864. {
  865. struct pci_dev *dev;
  866. dev = pci_scan_device(bus, devfn);
  867. if (!dev)
  868. return NULL;
  869. pci_device_add(dev, bus);
  870. return dev;
  871. }
  872. EXPORT_SYMBOL(pci_scan_single_device);
  873. /**
  874. * pci_scan_slot - scan a PCI slot on a bus for devices.
  875. * @bus: PCI bus to scan
  876. * @devfn: slot number to scan (must have zero function.)
  877. *
  878. * Scan a PCI slot on the specified PCI bus for devices, adding
  879. * discovered devices to the @bus->devices list. New devices
  880. * will not have is_added set.
  881. */
  882. int pci_scan_slot(struct pci_bus *bus, int devfn)
  883. {
  884. int func, nr = 0;
  885. int scan_all_fns;
  886. scan_all_fns = pcibios_scan_all_fns(bus, devfn);
  887. for (func = 0; func < 8; func++, devfn++) {
  888. struct pci_dev *dev;
  889. dev = pci_scan_single_device(bus, devfn);
  890. if (dev) {
  891. nr++;
  892. /*
  893. * If this is a single function device,
  894. * don't scan past the first function.
  895. */
  896. if (!dev->multifunction) {
  897. if (func > 0) {
  898. dev->multifunction = 1;
  899. } else {
  900. break;
  901. }
  902. }
  903. } else {
  904. if (func == 0 && !scan_all_fns)
  905. break;
  906. }
  907. }
  908. if (bus->self)
  909. pcie_aspm_init_link_state(bus->self);
  910. return nr;
  911. }
  912. unsigned int __devinit pci_scan_child_bus(struct pci_bus *bus)
  913. {
  914. unsigned int devfn, pass, max = bus->secondary;
  915. struct pci_dev *dev;
  916. pr_debug("PCI: Scanning bus %04x:%02x\n", pci_domain_nr(bus), bus->number);
  917. /* Go find them, Rover! */
  918. for (devfn = 0; devfn < 0x100; devfn += 8)
  919. pci_scan_slot(bus, devfn);
  920. /*
  921. * After performing arch-dependent fixup of the bus, look behind
  922. * all PCI-to-PCI bridges on this bus.
  923. */
  924. pr_debug("PCI: Fixups for bus %04x:%02x\n", pci_domain_nr(bus), bus->number);
  925. pcibios_fixup_bus(bus);
  926. for (pass=0; pass < 2; pass++)
  927. list_for_each_entry(dev, &bus->devices, bus_list) {
  928. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  929. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  930. max = pci_scan_bridge(bus, dev, max, pass);
  931. }
  932. /*
  933. * We've scanned the bus and so we know all about what's on
  934. * the other side of any bridges that may be on this bus plus
  935. * any devices.
  936. *
  937. * Return how far we've got finding sub-buses.
  938. */
  939. pr_debug("PCI: Bus scan for %04x:%02x returning with max=%02x\n",
  940. pci_domain_nr(bus), bus->number, max);
  941. return max;
  942. }
  943. void __attribute__((weak)) set_pci_bus_resources_arch_default(struct pci_bus *b)
  944. {
  945. }
  946. struct pci_bus * pci_create_bus(struct device *parent,
  947. int bus, struct pci_ops *ops, void *sysdata)
  948. {
  949. int error;
  950. struct pci_bus *b;
  951. struct device *dev;
  952. b = pci_alloc_bus();
  953. if (!b)
  954. return NULL;
  955. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  956. if (!dev){
  957. kfree(b);
  958. return NULL;
  959. }
  960. b->sysdata = sysdata;
  961. b->ops = ops;
  962. if (pci_find_bus(pci_domain_nr(b), bus)) {
  963. /* If we already got to this bus through a different bridge, ignore it */
  964. pr_debug("PCI: Bus %04x:%02x already known\n", pci_domain_nr(b), bus);
  965. goto err_out;
  966. }
  967. down_write(&pci_bus_sem);
  968. list_add_tail(&b->node, &pci_root_buses);
  969. up_write(&pci_bus_sem);
  970. memset(dev, 0, sizeof(*dev));
  971. dev->parent = parent;
  972. dev->release = pci_release_bus_bridge_dev;
  973. sprintf(dev->bus_id, "pci%04x:%02x", pci_domain_nr(b), bus);
  974. error = device_register(dev);
  975. if (error)
  976. goto dev_reg_err;
  977. b->bridge = get_device(dev);
  978. if (!parent)
  979. set_dev_node(b->bridge, pcibus_to_node(b));
  980. b->dev.class = &pcibus_class;
  981. b->dev.parent = b->bridge;
  982. sprintf(b->dev.bus_id, "%04x:%02x", pci_domain_nr(b), bus);
  983. error = device_register(&b->dev);
  984. if (error)
  985. goto class_dev_reg_err;
  986. error = device_create_file(&b->dev, &dev_attr_cpuaffinity);
  987. if (error)
  988. goto dev_create_file_err;
  989. /* Create legacy_io and legacy_mem files for this bus */
  990. pci_create_legacy_files(b);
  991. b->number = b->secondary = bus;
  992. b->resource[0] = &ioport_resource;
  993. b->resource[1] = &iomem_resource;
  994. set_pci_bus_resources_arch_default(b);
  995. return b;
  996. dev_create_file_err:
  997. device_unregister(&b->dev);
  998. class_dev_reg_err:
  999. device_unregister(dev);
  1000. dev_reg_err:
  1001. down_write(&pci_bus_sem);
  1002. list_del(&b->node);
  1003. up_write(&pci_bus_sem);
  1004. err_out:
  1005. kfree(dev);
  1006. kfree(b);
  1007. return NULL;
  1008. }
  1009. struct pci_bus * __devinit pci_scan_bus_parented(struct device *parent,
  1010. int bus, struct pci_ops *ops, void *sysdata)
  1011. {
  1012. struct pci_bus *b;
  1013. b = pci_create_bus(parent, bus, ops, sysdata);
  1014. if (b)
  1015. b->subordinate = pci_scan_child_bus(b);
  1016. return b;
  1017. }
  1018. EXPORT_SYMBOL(pci_scan_bus_parented);
  1019. #ifdef CONFIG_HOTPLUG
  1020. EXPORT_SYMBOL(pci_add_new_bus);
  1021. EXPORT_SYMBOL(pci_scan_slot);
  1022. EXPORT_SYMBOL(pci_scan_bridge);
  1023. EXPORT_SYMBOL_GPL(pci_scan_child_bus);
  1024. #endif
  1025. static int __init pci_sort_bf_cmp(const struct pci_dev *a, const struct pci_dev *b)
  1026. {
  1027. if (pci_domain_nr(a->bus) < pci_domain_nr(b->bus)) return -1;
  1028. else if (pci_domain_nr(a->bus) > pci_domain_nr(b->bus)) return 1;
  1029. if (a->bus->number < b->bus->number) return -1;
  1030. else if (a->bus->number > b->bus->number) return 1;
  1031. if (a->devfn < b->devfn) return -1;
  1032. else if (a->devfn > b->devfn) return 1;
  1033. return 0;
  1034. }
  1035. /*
  1036. * Yes, this forcably breaks the klist abstraction temporarily. It
  1037. * just wants to sort the klist, not change reference counts and
  1038. * take/drop locks rapidly in the process. It does all this while
  1039. * holding the lock for the list, so objects can't otherwise be
  1040. * added/removed while we're swizzling.
  1041. */
  1042. static void __init pci_insertion_sort_klist(struct pci_dev *a, struct list_head *list)
  1043. {
  1044. struct list_head *pos;
  1045. struct klist_node *n;
  1046. struct device *dev;
  1047. struct pci_dev *b;
  1048. list_for_each(pos, list) {
  1049. n = container_of(pos, struct klist_node, n_node);
  1050. dev = container_of(n, struct device, knode_bus);
  1051. b = to_pci_dev(dev);
  1052. if (pci_sort_bf_cmp(a, b) <= 0) {
  1053. list_move_tail(&a->dev.knode_bus.n_node, &b->dev.knode_bus.n_node);
  1054. return;
  1055. }
  1056. }
  1057. list_move_tail(&a->dev.knode_bus.n_node, list);
  1058. }
  1059. void __init pci_sort_breadthfirst(void)
  1060. {
  1061. LIST_HEAD(sorted_devices);
  1062. struct list_head *pos, *tmp;
  1063. struct klist_node *n;
  1064. struct device *dev;
  1065. struct pci_dev *pdev;
  1066. struct klist *device_klist;
  1067. device_klist = bus_get_device_klist(&pci_bus_type);
  1068. spin_lock(&device_klist->k_lock);
  1069. list_for_each_safe(pos, tmp, &device_klist->k_list) {
  1070. n = container_of(pos, struct klist_node, n_node);
  1071. dev = container_of(n, struct device, knode_bus);
  1072. pdev = to_pci_dev(dev);
  1073. pci_insertion_sort_klist(pdev, &sorted_devices);
  1074. }
  1075. list_splice(&sorted_devices, &device_klist->k_list);
  1076. spin_unlock(&device_klist->k_lock);
  1077. }