upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / drivers / pci / probe.c
1 /*
2  * probe.c - PCI detection and setup code
3  */
4
5 #include <linux/init.h>
6 #include <linux/pci.h>
7 #include <linux/slab.h>
8 #include <linux/module.h>
9 #include <linux/cpumask.h>
10
11 #undef DEBUG
12
13 #ifdef DEBUG
14 #define DBG(x...) printk(x)
15 #else
16 #define DBG(x...)
17 #endif
18
19 #define CARDBUS_LATENCY_TIMER   176     /* secondary latency timer */
20 #define CARDBUS_RESERVE_BUSNR   3
21 #define PCI_CFG_SPACE_SIZE      256
22 #define PCI_CFG_SPACE_EXP_SIZE  4096
23
24 /* Ugh.  Need to stop exporting this to modules. */
25 LIST_HEAD(pci_root_buses);
26 EXPORT_SYMBOL(pci_root_buses);
27
28 LIST_HEAD(pci_devices);
29
30 /*
31  * PCI Bus Class
32  */
33 static void release_pcibus_dev(struct class_device *class_dev)
34 {
35         struct pci_bus *pci_bus = to_pci_bus(class_dev);
36         if (pci_bus->bridge)
37                 put_device(pci_bus->bridge);
38         kfree(pci_bus);
39 }
40
41 static struct class pcibus_class = {
42         .name           = "pci_bus",
43         .release        = &release_pcibus_dev,
44 };
45
46 static int __init pcibus_class_init(void)
47 {
48         return class_register(&pcibus_class);
49 }
50 postcore_initcall(pcibus_class_init);
51
52 /*
53  * PCI Bus Class Devices
54  */
55 static ssize_t pci_bus_show_cpuaffinity(struct class_device *class_dev, char *buf)
56 {
57         cpumask_t cpumask = pcibus_to_cpumask((to_pci_bus(class_dev))->number);
58         int ret;
59
60         ret = cpumask_scnprintf(buf, PAGE_SIZE, cpumask);
61         if (ret < PAGE_SIZE)
62                 buf[ret++] = '\n';
63         return ret;
64 }
65 static CLASS_DEVICE_ATTR(cpuaffinity, S_IRUGO, pci_bus_show_cpuaffinity, NULL);
66
67 /*
68  * Translate the low bits of the PCI base
69  * to the resource type
70  */
71 static inline unsigned int pci_calc_resource_flags(unsigned int flags)
72 {
73         if (flags & PCI_BASE_ADDRESS_SPACE_IO)
74                 return IORESOURCE_IO;
75
76         if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
77                 return IORESOURCE_MEM | IORESOURCE_PREFETCH;
78
79         return IORESOURCE_MEM;
80 }
81
82 /*
83  * Find the extent of a PCI decode..
84  */
85 static u32 pci_size(u32 base, u32 maxbase, unsigned long mask)
86 {
87         u32 size = mask & maxbase;      /* Find the significant bits */
88         if (!size)
89                 return 0;
90
91         /* Get the lowest of them to find the decode size, and
92            from that the extent.  */
93         size = (size & ~(size-1)) - 1;
94
95         /* base == maxbase can be valid only if the BAR has
96            already been programmed with all 1s.  */
97         if (base == maxbase && ((base | size) & mask) != mask)
98                 return 0;
99
100         return size;
101 }
102
103 static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
104 {
105         unsigned int pos, reg, next;
106         u32 l, sz;
107         struct resource *res;
108
109         for(pos=0; pos<howmany; pos = next) {
110                 next = pos+1;
111                 res = &dev->resource[pos];
112                 res->name = pci_name(dev);
113                 reg = PCI_BASE_ADDRESS_0 + (pos << 2);
114                 pci_read_config_dword(dev, reg, &l);
115                 pci_write_config_dword(dev, reg, ~0);
116                 pci_read_config_dword(dev, reg, &sz);
117                 pci_write_config_dword(dev, reg, l);
118                 if (!sz || sz == 0xffffffff)
119                         continue;
120                 if (l == 0xffffffff)
121                         l = 0;
122                 if ((l & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_MEMORY) {
123                         sz = pci_size(l, sz, PCI_BASE_ADDRESS_MEM_MASK);
124                         if (!sz)
125                                 continue;
126                         res->start = l & PCI_BASE_ADDRESS_MEM_MASK;
127                         res->flags |= l & ~PCI_BASE_ADDRESS_MEM_MASK;
128                 } else {
129                         sz = pci_size(l, sz, PCI_BASE_ADDRESS_IO_MASK & 0xffff);
130                         if (!sz)
131                                 continue;
132                         res->start = l & PCI_BASE_ADDRESS_IO_MASK;
133                         res->flags |= l & ~PCI_BASE_ADDRESS_IO_MASK;
134                 }
135                 res->end = res->start + (unsigned long) sz;
136                 res->flags |= pci_calc_resource_flags(l);
137                 if ((l & (PCI_BASE_ADDRESS_SPACE | PCI_BASE_ADDRESS_MEM_TYPE_MASK))
138                     == (PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_TYPE_64)) {
139                         pci_read_config_dword(dev, reg+4, &l);
140                         next++;
141 #if BITS_PER_LONG == 64
142                         res->start |= ((unsigned long) l) << 32;
143                         res->end = res->start + sz;
144                         pci_write_config_dword(dev, reg+4, ~0);
145                         pci_read_config_dword(dev, reg+4, &sz);
146                         pci_write_config_dword(dev, reg+4, l);
147                         sz = pci_size(l, sz, 0xffffffff);
148                         if (sz) {
149                                 /* This BAR needs > 4GB?  Wow. */
150                                 res->end |= (unsigned long)sz<<32;
151                         }
152 #else
153                         if (l) {
154                                 printk(KERN_ERR "PCI: Unable to handle 64-bit address for device %s\n", pci_name(dev));
155                                 res->start = 0;
156                                 res->flags = 0;
157                                 continue;
158                         }
159 #endif
160                 }
161         }
162         if (rom) {
163                 dev->rom_base_reg = rom;
164                 res = &dev->resource[PCI_ROM_RESOURCE];
165                 res->name = pci_name(dev);
166                 pci_read_config_dword(dev, rom, &l);
167                 pci_write_config_dword(dev, rom, ~PCI_ROM_ADDRESS_ENABLE);
168                 pci_read_config_dword(dev, rom, &sz);
169                 pci_write_config_dword(dev, rom, l);
170                 if (l == 0xffffffff)
171                         l = 0;
172                 if (sz && sz != 0xffffffff) {
173                         sz = pci_size(l, sz, PCI_ROM_ADDRESS_MASK);
174                         if (sz) {
175                                 res->flags = (l & IORESOURCE_ROM_ENABLE) |
176                                   IORESOURCE_MEM | IORESOURCE_PREFETCH |
177                                   IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
178                                 res->start = l & PCI_ROM_ADDRESS_MASK;
179                                 res->end = res->start + (unsigned long) sz;
180                         }
181                 }
182         }
183 }
184
185 void __devinit pci_read_bridge_bases(struct pci_bus *child)
186 {
187         struct pci_dev *dev = child->self;
188         u8 io_base_lo, io_limit_lo;
189         u16 mem_base_lo, mem_limit_lo;
190         unsigned long base, limit;
191         struct resource *res;
192         int i;
193
194         if (!dev)               /* It's a host bus, nothing to read */
195                 return;
196
197         if (dev->transparent) {
198                 printk(KERN_INFO "PCI: Transparent bridge - %s\n", pci_name(dev));
199                 for(i = 0; i < PCI_BUS_NUM_RESOURCES; i++)
200                         child->resource[i] = child->parent->resource[i];
201                 return;
202         }
203
204         for(i=0; i<3; i++)
205                 child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];
206
207         res = child->resource[0];
208         pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
209         pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
210         base = (io_base_lo & PCI_IO_RANGE_MASK) << 8;
211         limit = (io_limit_lo & PCI_IO_RANGE_MASK) << 8;
212
213         if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
214                 u16 io_base_hi, io_limit_hi;
215                 pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
216                 pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
217                 base |= (io_base_hi << 16);
218                 limit |= (io_limit_hi << 16);
219         }
220
221         if (base <= limit) {
222                 res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
223                 res->start = base;
224                 res->end = limit + 0xfff;
225         }
226
227         res = child->resource[1];
228         pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
229         pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
230         base = (mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
231         limit = (mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
232         if (base <= limit) {
233                 res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM;
234                 res->start = base;
235                 res->end = limit + 0xfffff;
236         }
237
238         res = child->resource[2];
239         pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
240         pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
241         base = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
242         limit = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
243
244         if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
245                 u32 mem_base_hi, mem_limit_hi;
246                 pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
247                 pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
248
249                 /*
250                  * Some bridges set the base > limit by default, and some
251                  * (broken) BIOSes do not initialize them.  If we find
252                  * this, just assume they are not being used.
253                  */
254                 if (mem_base_hi <= mem_limit_hi) {
255 #if BITS_PER_LONG == 64
256                         base |= ((long) mem_base_hi) << 32;
257                         limit |= ((long) mem_limit_hi) << 32;
258 #else
259                         if (mem_base_hi || mem_limit_hi) {
260                                 printk(KERN_ERR "PCI: Unable to handle 64-bit address space for bridge %s\n", pci_name(dev));
261                                 return;
262                         }
263 #endif
264                 }
265         }
266         if (base <= limit) {
267                 res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM | IORESOURCE_PREFETCH;
268                 res->start = base;
269                 res->end = limit + 0xfffff;
270         }
271 }
272
273 static struct pci_bus * __devinit pci_alloc_bus(void)
274 {
275         struct pci_bus *b;
276
277         b = kmalloc(sizeof(*b), GFP_KERNEL);
278         if (b) {
279                 memset(b, 0, sizeof(*b));
280                 INIT_LIST_HEAD(&b->node);
281                 INIT_LIST_HEAD(&b->children);
282                 INIT_LIST_HEAD(&b->devices);
283         }
284         return b;
285 }
286
287 static struct pci_bus * __devinit
288 pci_alloc_child_bus(struct pci_bus *parent, struct pci_dev *bridge, int busnr)
289 {
290         struct pci_bus *child;
291         int i;
292
293         /*
294          * Allocate a new bus, and inherit stuff from the parent..
295          */
296         child = pci_alloc_bus();
297         if (!child)
298                 return NULL;
299
300         child->self = bridge;
301         child->parent = parent;
302         child->ops = parent->ops;
303         child->sysdata = parent->sysdata;
304         child->bridge = get_device(&bridge->dev);
305
306         child->class_dev.class = &pcibus_class;
307         sprintf(child->class_dev.class_id, "%04x:%02x", pci_domain_nr(child), busnr);
308         class_device_register(&child->class_dev);
309         class_device_create_file(&child->class_dev, &class_device_attr_cpuaffinity);
310
311         /*
312          * Set up the primary, secondary and subordinate
313          * bus numbers.
314          */
315         child->number = child->secondary = busnr;
316         child->primary = parent->secondary;
317         child->subordinate = 0xff;
318
319         /* Set up default resource pointers and names.. */
320         for (i = 0; i < 4; i++) {
321                 child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];
322                 child->resource[i]->name = child->name;
323         }
324         bridge->subordinate = child;
325
326         return child;
327 }
328
329 struct pci_bus * __devinit pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev, int busnr)
330 {
331         struct pci_bus *child;
332
333         child = pci_alloc_child_bus(parent, dev, busnr);
334         if (child)
335                 list_add_tail(&child->node, &parent->children);
336         return child;
337 }
338
339 unsigned int __devinit pci_scan_child_bus(struct pci_bus *bus);
340
341 /*
342  * If it's a bridge, configure it and scan the bus behind it.
343  * For CardBus bridges, we don't scan behind as the devices will
344  * be handled by the bridge driver itself.
345  *
346  * We need to process bridges in two passes -- first we scan those
347  * already configured by the BIOS and after we are done with all of
348  * them, we proceed to assigning numbers to the remaining buses in
349  * order to avoid overlaps between old and new bus numbers.
350  */
351 int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev * dev, int max, int pass)
352 {
353         struct pci_bus *child;
354         int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
355         u32 buses;
356         u16 bctl;
357
358         pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
359
360         DBG("Scanning behind PCI bridge %s, config %06x, pass %d\n",
361             pci_name(dev), buses & 0xffffff, pass);
362
363         /* Disable MasterAbortMode during probing to avoid reporting
364            of bus errors (in some architectures) */ 
365         pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);
366         pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
367                               bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
368
369         if ((buses & 0xffff00) && !pcibios_assign_all_busses() && !is_cardbus) {
370                 unsigned int cmax, busnr;
371                 /*
372                  * Bus already configured by firmware, process it in the first
373                  * pass and just note the configuration.
374                  */
375                 if (pass)
376                         return max;
377                 busnr = (buses >> 8) & 0xFF;
378                 child = pci_alloc_child_bus(bus, dev, busnr);
379                 if (!child)
380                         return max;
381                 child->primary = buses & 0xFF;
382                 child->subordinate = (buses >> 16) & 0xFF;
383                 child->bridge_ctl = bctl;
384
385                 cmax = pci_scan_child_bus(child);
386                 if (cmax > max)
387                         max = cmax;
388                 if (child->subordinate > max)
389                         max = child->subordinate;
390         } else {
391                 /*
392                  * We need to assign a number to this bus which we always
393                  * do in the second pass.
394                  */
395                 if (!pass)
396                         return max;
397
398                 /* Clear errors */
399                 pci_write_config_word(dev, PCI_STATUS, 0xffff);
400
401                 child = pci_alloc_child_bus(bus, dev, ++max);
402                 buses = (buses & 0xff000000)
403                       | ((unsigned int)(child->primary)     <<  0)
404                       | ((unsigned int)(child->secondary)   <<  8)
405                       | ((unsigned int)(child->subordinate) << 16);
406
407                 /*
408                  * yenta.c forces a secondary latency timer of 176.
409                  * Copy that behaviour here.
410                  */
411                 if (is_cardbus) {
412                         buses &= ~0xff000000;
413                         buses |= CARDBUS_LATENCY_TIMER << 24;
414                 }
415                         
416                 /*
417                  * We need to blast all three values with a single write.
418                  */
419                 pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
420
421                 if (!is_cardbus) {
422                         child->bridge_ctl = PCI_BRIDGE_CTL_NO_ISA;
423
424                         /* Now we can scan all subordinate buses... */
425                         max = pci_scan_child_bus(child);
426                 } else {
427                         /*
428                          * For CardBus bridges, we leave 4 bus numbers
429                          * as cards with a PCI-to-PCI bridge can be
430                          * inserted later.
431                          */
432                         max += CARDBUS_RESERVE_BUSNR;
433                 }
434                 /*
435                  * Set the subordinate bus number to its real value.
436                  */
437                 child->subordinate = max;
438                 pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
439         }
440
441         pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
442
443         sprintf(child->name, (is_cardbus ? "PCI CardBus #%02x" : "PCI Bus #%02x"), child->number);
444
445         return max;
446 }
447
448 /*
449  * Read interrupt line and base address registers.
450  * The architecture-dependent code can tweak these, of course.
451  */
452 static void pci_read_irq(struct pci_dev *dev)
453 {
454         unsigned char irq;
455
456         pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
457         if (irq)
458                 pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
459         dev->irq = irq;
460 }
461
462 /**
463  * pci_setup_device - fill in class and map information of a device
464  * @dev: the device structure to fill
465  *
466  * Initialize the device structure with information about the device's 
467  * vendor,class,memory and IO-space addresses,IRQ lines etc.
468  * Called at initialisation of the PCI subsystem and by CardBus services.
469  * Returns 0 on success and -1 if unknown type of device (not normal, bridge
470  * or CardBus).
471  */
472 static int pci_setup_device(struct pci_dev * dev)
473 {
474         u32 class;
475
476         dev->slot_name = dev->dev.bus_id;
477         sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
478                 dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
479
480         pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
481         class >>= 8;                                /* upper 3 bytes */
482         dev->class = class;
483         class >>= 8;
484
485         DBG("Found %02x:%02x [%04x/%04x] %06x %02x\n", dev->bus->number,
486             dev->devfn, dev->vendor, dev->device, class, dev->hdr_type);
487
488         /* "Unknown power state" */
489         dev->current_state = 4;
490
491         /* Early fixups, before probing the BARs */
492         pci_fixup_device(pci_fixup_early, dev);
493
494         switch (dev->hdr_type) {                    /* header type */
495         case PCI_HEADER_TYPE_NORMAL:                /* standard header */
496                 if (class == PCI_CLASS_BRIDGE_PCI)
497                         goto bad;
498                 pci_read_irq(dev);
499                 pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
500                 pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
501                 pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device);
502                 break;
503
504         case PCI_HEADER_TYPE_BRIDGE:                /* bridge header */
505                 if (class != PCI_CLASS_BRIDGE_PCI)
506                         goto bad;
507                 /* The PCI-to-PCI bridge spec requires that subtractive
508                    decoding (i.e. transparent) bridge must have programming
509                    interface code of 0x01. */ 
510                 dev->transparent = ((dev->class & 0xff) == 1);
511                 pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
512                 break;
513
514         case PCI_HEADER_TYPE_CARDBUS:               /* CardBus bridge header */
515                 if (class != PCI_CLASS_BRIDGE_CARDBUS)
516                         goto bad;
517                 pci_read_irq(dev);
518                 pci_read_bases(dev, 1, 0);
519                 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
520                 pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
521                 break;
522
523         default:                                    /* unknown header */
524                 printk(KERN_ERR "PCI: device %s has unknown header type %02x, ignoring.\n",
525                         pci_name(dev), dev->hdr_type);
526                 return -1;
527
528         bad:
529                 printk(KERN_ERR "PCI: %s: class %x doesn't match header type %02x. Ignoring class.\n",
530                        pci_name(dev), class, dev->hdr_type);
531                 dev->class = PCI_CLASS_NOT_DEFINED;
532         }
533
534         /* We found a fine healthy device, go go go... */
535         return 0;
536 }
537
538 /**
539  * pci_release_dev - free a pci device structure when all users of it are finished.
540  * @dev: device that's been disconnected
541  *
542  * Will be called only by the device core when all users of this pci device are
543  * done.
544  */
545 static void pci_release_dev(struct device *dev)
546 {
547         struct pci_dev *pci_dev;
548
549         pci_dev = to_pci_dev(dev);
550         kfree(pci_dev);
551 }
552
553 /**
554  * pci_cfg_space_size - get the configuration space size of the PCI device.
555  *
556  * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
557  * have 4096 bytes.  Even if the device is capable, that doesn't mean we can
558  * access it.  Maybe we don't have a way to generate extended config space
559  * accesses, or the device is behind a reverse Express bridge.  So we try
560  * reading the dword at 0x100 which must either be 0 or a valid extended
561  * capability header.
562  */
563 static int pci_cfg_space_size(struct pci_dev *dev)
564 {
565         int pos;
566         u32 status;
567
568         pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
569         if (!pos) {
570                 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
571                 if (!pos)
572                         goto fail;
573
574                 pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);
575                 if (!(status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ)))
576                         goto fail;
577         }
578
579         if (pci_read_config_dword(dev, 256, &status) != PCIBIOS_SUCCESSFUL)
580                 goto fail;
581         if (status == 0xffffffff)
582                 goto fail;
583
584         return PCI_CFG_SPACE_EXP_SIZE;
585
586  fail:
587         return PCI_CFG_SPACE_SIZE;
588 }
589
590 static void pci_release_bus_bridge_dev(struct device *dev)
591 {
592         kfree(dev);
593 }
594
595 /*
596  * Read the config data for a PCI device, sanity-check it
597  * and fill in the dev structure...
598  */
599 static struct pci_dev * __devinit
600 pci_scan_device(struct pci_bus *bus, int devfn)
601 {
602         struct pci_dev *dev;
603         u32 l;
604         u8 hdr_type;
605
606         if (pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type))
607                 return NULL;
608
609         if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, &l))
610                 return NULL;
611
612         /* some broken boards return 0 or ~0 if a slot is empty: */
613         if (l == 0xffffffff || l == 0x00000000 ||
614             l == 0x0000ffff || l == 0xffff0000)
615                 return NULL;
616
617         dev = kmalloc(sizeof(struct pci_dev), GFP_KERNEL);
618         if (!dev)
619                 return NULL;
620
621         memset(dev, 0, sizeof(struct pci_dev));
622         dev->bus = bus;
623         dev->sysdata = bus->sysdata;
624         dev->dev.parent = bus->bridge;
625         dev->dev.bus = &pci_bus_type;
626         dev->devfn = devfn;
627         dev->hdr_type = hdr_type & 0x7f;
628         dev->multifunction = !!(hdr_type & 0x80);
629         dev->vendor = l & 0xffff;
630         dev->device = (l >> 16) & 0xffff;
631         dev->cfg_size = pci_cfg_space_size(dev);
632
633         /* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
634            set this higher, assuming the system even supports it.  */
635         dev->dma_mask = 0xffffffff;
636         if (pci_setup_device(dev) < 0) {
637                 kfree(dev);
638                 return NULL;
639         }
640         device_initialize(&dev->dev);
641         dev->dev.release = pci_release_dev;
642         pci_dev_get(dev);
643
644         pci_name_device(dev);
645
646         dev->dev.dma_mask = &dev->dma_mask;
647         dev->dev.coherent_dma_mask = 0xffffffffull;
648
649         return dev;
650 }
651
652 struct pci_dev * __devinit
653 pci_scan_single_device(struct pci_bus *bus, int devfn)
654 {
655         struct pci_dev *dev;
656
657         dev = pci_scan_device(bus, devfn);
658         pci_scan_msi_device(dev);
659
660         if (!dev)
661                 return NULL;
662         
663         /* Fix up broken headers */
664         pci_fixup_device(pci_fixup_header, dev);
665
666         /*
667          * Add the device to our list of discovered devices
668          * and the bus list for fixup functions, etc.
669          */
670         INIT_LIST_HEAD(&dev->global_list);
671         list_add_tail(&dev->bus_list, &bus->devices);
672
673         return dev;
674 }
675
676 /**
677  * pci_scan_slot - scan a PCI slot on a bus for devices.
678  * @bus: PCI bus to scan
679  * @devfn: slot number to scan (must have zero function.)
680  *
681  * Scan a PCI slot on the specified PCI bus for devices, adding
682  * discovered devices to the @bus->devices list.  New devices
683  * will have an empty dev->global_list head.
684  */
685 int __devinit pci_scan_slot(struct pci_bus *bus, int devfn)
686 {
687         int func, nr = 0;
688         int scan_all_fns;
689
690         scan_all_fns = pcibios_scan_all_fns(bus, devfn);
691
692         for (func = 0; func < 8; func++, devfn++) {
693                 struct pci_dev *dev;
694
695                 dev = pci_scan_single_device(bus, devfn);
696                 if (dev) {
697                         nr++;
698
699                         /*
700                          * If this is a single function device,
701                          * don't scan past the first function.
702                          */
703                         if (!dev->multifunction) {
704                                 if (func > 0) {
705                                         dev->multifunction = 1;
706                                 } else {
707                                         break;
708                                 }
709                         }
710                 } else {
711                         if (func == 0 && !scan_all_fns)
712                                 break;
713                 }
714         }
715         return nr;
716 }
717
718 unsigned int __devinit pci_scan_child_bus(struct pci_bus *bus)
719 {
720         unsigned int devfn, pass, max = bus->secondary;
721         struct pci_dev *dev;
722
723         DBG("Scanning bus %02x\n", bus->number);
724
725         /* Go find them, Rover! */
726         for (devfn = 0; devfn < 0x100; devfn += 8)
727                 pci_scan_slot(bus, devfn);
728
729         /*
730          * After performing arch-dependent fixup of the bus, look behind
731          * all PCI-to-PCI bridges on this bus.
732          */
733         DBG("Fixups for bus %02x\n", bus->number);
734         pcibios_fixup_bus(bus);
735         for (pass=0; pass < 2; pass++)
736                 list_for_each_entry(dev, &bus->devices, bus_list) {
737                         if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
738                             dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
739                                 max = pci_scan_bridge(bus, dev, max, pass);
740                 }
741
742         /*
743          * We've scanned the bus and so we know all about what's on
744          * the other side of any bridges that may be on this bus plus
745          * any devices.
746          *
747          * Return how far we've got finding sub-buses.
748          */
749         DBG("Bus scan for %02x returning with max=%02x\n", bus->number, max);
750         return max;
751 }
752
753 unsigned int __devinit pci_do_scan_bus(struct pci_bus *bus)
754 {
755         unsigned int max;
756
757         max = pci_scan_child_bus(bus);
758
759         /*
760          * Make the discovered devices available.
761          */
762         pci_bus_add_devices(bus);
763
764         return max;
765 }
766
767 struct pci_bus * __devinit pci_scan_bus_parented(struct device *parent, int bus, struct pci_ops *ops, void *sysdata)
768 {
769         int error;
770         struct pci_bus *b;
771         struct device *dev;
772
773         b = pci_alloc_bus();
774         if (!b)
775                 return NULL;
776
777         dev = kmalloc(sizeof(*dev), GFP_KERNEL);
778         if (!dev){
779                 kfree(b);
780                 return NULL;
781         }
782
783         b->sysdata = sysdata;
784         b->ops = ops;
785
786         if (pci_find_bus(pci_domain_nr(b), bus)) {
787                 /* If we already got to this bus through a different bridge, ignore it */
788                 DBG("PCI: Bus %02x already known\n", bus);
789                 goto err_out;
790         }
791         list_add_tail(&b->node, &pci_root_buses);
792
793         memset(dev, 0, sizeof(*dev));
794         dev->parent = parent;
795         dev->release = pci_release_bus_bridge_dev;
796         sprintf(dev->bus_id, "pci%04x:%02x", pci_domain_nr(b), bus);
797         error = device_register(dev);
798         if (error)
799                 goto dev_reg_err;
800         b->bridge = get_device(dev);
801
802         b->class_dev.class = &pcibus_class;
803         sprintf(b->class_dev.class_id, "%04x:%02x", pci_domain_nr(b), bus);
804         error = class_device_register(&b->class_dev);
805         if (error)
806                 goto class_dev_reg_err;
807         error = class_device_create_file(&b->class_dev, &class_device_attr_cpuaffinity);
808         if (error)
809                 goto class_dev_create_file_err;
810
811         error = sysfs_create_link(&b->class_dev.kobj, &b->bridge->kobj, "bridge");
812         if (error)
813                 goto sys_create_link_err;
814
815         b->number = b->secondary = bus;
816         b->resource[0] = &ioport_resource;
817         b->resource[1] = &iomem_resource;
818
819         b->subordinate = pci_scan_child_bus(b);
820
821         pci_bus_add_devices(b);
822
823         return b;
824
825 sys_create_link_err:
826         class_device_remove_file(&b->class_dev, &class_device_attr_cpuaffinity);
827 class_dev_create_file_err:
828         class_device_unregister(&b->class_dev);
829 class_dev_reg_err:
830         device_unregister(dev);
831 dev_reg_err:
832         list_del(&b->node);
833 err_out:
834         kfree(dev);
835         kfree(b);
836         return NULL;
837 }
838 EXPORT_SYMBOL(pci_scan_bus_parented);
839
840 #ifdef CONFIG_HOTPLUG
841 EXPORT_SYMBOL(pci_add_new_bus);
842 EXPORT_SYMBOL(pci_do_scan_bus);
843 EXPORT_SYMBOL(pci_scan_slot);
844 EXPORT_SYMBOL(pci_scan_bridge);
845 EXPORT_SYMBOL(pci_scan_single_device);
846 EXPORT_SYMBOL_GPL(pci_scan_child_bus);
847 #endif