Subversion Repositories Kolibri OS

Rev

Rev 1633 | Rev 2187 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1628 serge 1
 
2
#include 
3
#include 
4
#include 
5
#include 
6
7
 
1631 serge 8
1628 serge 9
 
1631 serge 10
11
 
12
 
13
 
14
 
1628 serge 15
16
 
1631 serge 17
#define CARDBUS_RESERVE_BUSNR   3
18
19
 
20
{
21
    return 0;
22
};
23
24
 
1628 serge 25
 * pci_ari_enabled - query ARI forwarding status
1631 serge 26
 * @bus: the PCI bus
27
 *
28
 * Returns 1 if ARI forwarding is enabled, or 0 if not enabled;
29
 */
30
static inline int pci_ari_enabled(struct pci_bus *bus)
31
{
32
    return bus->self && bus->self->ari_enabled;
33
}
34
35
 
36
 * Translate the low bits of the PCI base
37
 * to the resource type
38
 */
39
static inline unsigned int pci_calc_resource_flags(unsigned int flags)
40
{
41
    if (flags & PCI_BASE_ADDRESS_SPACE_IO)
42
        return IORESOURCE_IO;
43
44
 
45
        return IORESOURCE_MEM | IORESOURCE_PREFETCH;
46
47
 
48
}
49
50
 
51
{
52
    u64 size = mask & maxbase;  /* Find the significant bits */
53
    if (!size)
54
        return 0;
55
56
 
57
       from that the extent.  */
58
    size = (size & ~(size-1)) - 1;
59
60
 
61
       already been programmed with all 1s.  */
62
    if (base == maxbase && ((base | size) & mask) != mask)
63
        return 0;
64
65
 
66
}
67
68
 
69
{
70
    if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
71
        res->flags = bar & ~PCI_BASE_ADDRESS_IO_MASK;
72
        return pci_bar_io;
73
    }
74
75
 
76
77
 
78
        return pci_bar_mem64;
79
    return pci_bar_mem32;
80
}
81
82
 
83
 
84
 
85
 * pci_read_base - read a PCI BAR
86
 * @dev: the PCI device
87
 * @type: type of the BAR
88
 * @res: resource buffer to be filled in
89
 * @pos: BAR position in the config space
90
 *
91
 * Returns 1 if the BAR is 64-bit, or 0 if 32-bit.
92
 */
93
int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
94
            struct resource *res, unsigned int pos)
95
{
96
    u32 l, sz, mask;
97
    u16 orig_cmd;
98
99
 
100
101
 
102
        pci_read_config_word(dev, PCI_COMMAND, &orig_cmd);
103
        pci_write_config_word(dev, PCI_COMMAND,
104
            orig_cmd & ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO));
105
    }
106
107
 
108
109
 
110
    pci_write_config_dword(dev, pos, l | mask);
111
    pci_read_config_dword(dev, pos, &sz);
112
    pci_write_config_dword(dev, pos, l);
113
114
 
115
        pci_write_config_word(dev, PCI_COMMAND, orig_cmd);
116
117
 
118
     * All bits set in sz means the device isn't working properly.
119
     * If the BAR isn't implemented, all bits must be 0.  If it's a
120
     * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit
121
     * 1 must be clear.
122
     */
123
    if (!sz || sz == 0xffffffff)
124
        goto fail;
125
126
 
127
     * I don't know how l can have all bits set.  Copied from old code.
128
     * Maybe it fixes a bug on some ancient platform.
129
     */
130
    if (l == 0xffffffff)
131
        l = 0;
132
133
 
134
        type = decode_bar(res, l);
135
        res->flags |= pci_calc_resource_flags(l) | IORESOURCE_SIZEALIGN;
136
        if (type == pci_bar_io) {
137
            l &= PCI_BASE_ADDRESS_IO_MASK;
138
            mask = PCI_BASE_ADDRESS_IO_MASK & IO_SPACE_LIMIT;
139
        } else {
140
            l &= PCI_BASE_ADDRESS_MEM_MASK;
141
            mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
142
        }
143
    } else {
144
        res->flags |= (l & IORESOURCE_ROM_ENABLE);
145
        l &= PCI_ROM_ADDRESS_MASK;
146
        mask = (u32)PCI_ROM_ADDRESS_MASK;
147
    }
148
149
 
150
        u64 l64 = l;
151
        u64 sz64 = sz;
152
        u64 mask64 = mask | (u64)~0 << 32;
153
154
 
155
        pci_write_config_dword(dev, pos + 4, ~0);
156
        pci_read_config_dword(dev, pos + 4, &sz);
157
        pci_write_config_dword(dev, pos + 4, l);
158
159
 
160
        sz64 |= ((u64)sz << 32);
161
162
 
163
164
 
165
            goto fail;
166
167
 
168
            dbgprintf("%s reg %x: can't handle 64-bit BAR\n",
169
                __FUNCTION__, pos);
170
            goto fail;
171
        }
172
173
 
174
        if ((sizeof(resource_size_t) < 8) && l) {
175
            /* Address above 32-bit boundary; disable the BAR */
176
            pci_write_config_dword(dev, pos, 0);
177
            pci_write_config_dword(dev, pos + 4, 0);
178
            res->start = 0;
179
            res->end = sz64;
180
        } else {
181
            res->start = l64;
182
            res->end = l64 + sz64;
183
        dbgprintf("%s reg %x: %pR\n", __FUNCTION__, pos, res);
184
        }
185
    } else {
186
        sz = pci_size(l, sz, mask);
187
188
 
189
            goto fail;
190
191
 
192
        res->end = l + sz;
193
194
 
195
    }
196
197
 
198
    return (type == pci_bar_mem64) ? 1 : 0;
199
 fail:
200
    res->flags = 0;
201
    goto out;
202
}
203
204
 
205
{
206
    unsigned int pos, reg;
207
208
 
209
        struct resource *res = &dev->resource[pos];
210
        reg = PCI_BASE_ADDRESS_0 + (pos << 2);
211
        pos += __pci_read_base(dev, pci_bar_unknown, res, reg);
212
    }
213
214
 
215
        struct resource *res = &dev->resource[PCI_ROM_RESOURCE];
216
        dev->rom_base_reg = rom;
217
        res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH |
218
                IORESOURCE_READONLY | IORESOURCE_CACHEABLE |
219
                IORESOURCE_SIZEALIGN;
220
        __pci_read_base(dev, pci_bar_mem32, res, rom);
221
    }
222
}
223
224
 
225
226
 
227
{
228
    struct pci_dev *dev = child->self;
229
    struct resource *res;
230
    int i;
231
232
 
233
        return;
234
235
 
236
         child->secondary, child->subordinate,
237
         dev->transparent ? " (subtractive decode)" : "");
238
239
 
240
    for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
241
        child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];
242
243
 
244
    pci_read_bridge_mmio(child);
245
    pci_read_bridge_mmio_pref(child);
246
247
 
248
        pci_bus_for_each_resource(child->parent, res, i) {
249
            if (res) {
250
                pci_bus_add_resource(child, res,
251
                             PCI_SUBTRACTIVE_DECODE);
252
                dbgprintf("  bridge window %pR (subtractive decode)\n", res);
253
            }
254
        }
255
    }
256
}
257
258
 
259
260
 
261
{
262
    struct pci_bus *b;
263
264
 
265
    if (b) {
266
        INIT_LIST_HEAD(&b->node);
267
        INIT_LIST_HEAD(&b->children);
268
        INIT_LIST_HEAD(&b->devices);
269
        INIT_LIST_HEAD(&b->slots);
270
        INIT_LIST_HEAD(&b->resources);
271
//        b->max_bus_speed = PCI_SPEED_UNKNOWN;
272
//        b->cur_bus_speed = PCI_SPEED_UNKNOWN;
273
    }
274
    return b;
275
}
276
277
 
278
 
279
280
 
281
    PCI_SPEED_UNKNOWN,      /* 0 */
282
    PCI_SPEED_66MHz_PCIX,       /* 1 */
283
    PCI_SPEED_100MHz_PCIX,      /* 2 */
284
    PCI_SPEED_133MHz_PCIX,      /* 3 */
285
    PCI_SPEED_UNKNOWN,      /* 4 */
286
    PCI_SPEED_66MHz_PCIX_ECC,   /* 5 */
287
    PCI_SPEED_100MHz_PCIX_ECC,  /* 6 */
288
    PCI_SPEED_133MHz_PCIX_ECC,  /* 7 */
289
    PCI_SPEED_UNKNOWN,      /* 8 */
290
    PCI_SPEED_66MHz_PCIX_266,   /* 9 */
291
    PCI_SPEED_100MHz_PCIX_266,  /* A */
292
    PCI_SPEED_133MHz_PCIX_266,  /* B */
293
    PCI_SPEED_UNKNOWN,      /* C */
294
    PCI_SPEED_66MHz_PCIX_533,   /* D */
295
    PCI_SPEED_100MHz_PCIX_533,  /* E */
296
    PCI_SPEED_133MHz_PCIX_533   /* F */
297
};
298
299
 
300
    PCI_SPEED_UNKNOWN,      /* 0 */
301
    PCIE_SPEED_2_5GT,       /* 1 */
302
    PCIE_SPEED_5_0GT,       /* 2 */
303
    PCIE_SPEED_8_0GT,       /* 3 */
304
    PCI_SPEED_UNKNOWN,      /* 4 */
305
    PCI_SPEED_UNKNOWN,      /* 5 */
306
    PCI_SPEED_UNKNOWN,      /* 6 */
307
    PCI_SPEED_UNKNOWN,      /* 7 */
308
    PCI_SPEED_UNKNOWN,      /* 8 */
309
    PCI_SPEED_UNKNOWN,      /* 9 */
310
    PCI_SPEED_UNKNOWN,      /* A */
311
    PCI_SPEED_UNKNOWN,      /* B */
312
    PCI_SPEED_UNKNOWN,      /* C */
313
    PCI_SPEED_UNKNOWN,      /* D */
314
    PCI_SPEED_UNKNOWN,      /* E */
315
    PCI_SPEED_UNKNOWN       /* F */
316
};
317
318
 
319
 
320
{
321
    struct pci_dev *bridge = bus->self;
322
    int pos;
323
324
 
325
    if (!pos)
326
        pos = pci_find_capability(bridge, PCI_CAP_ID_AGP3);
327
    if (pos) {
328
        u32 agpstat, agpcmd;
329
330
 
331
        bus->max_bus_speed = agp_speed(agpstat & 8, agpstat & 7);
332
333
 
334
        bus->cur_bus_speed = agp_speed(agpstat & 8, agpcmd & 7);
335
    }
336
337
 
338
    if (pos) {
339
        u16 status;
340
        enum pci_bus_speed max;
341
        pci_read_config_word(bridge, pos + 2, &status);
342
343
 
344
            max = PCI_SPEED_133MHz_PCIX_533;
345
        } else if (status & 0x4000) {
346
            max = PCI_SPEED_133MHz_PCIX_266;
347
        } else if (status & 0x0002) {
348
            if (((status >> 12) & 0x3) == 2) {
349
                max = PCI_SPEED_133MHz_PCIX_ECC;
350
            } else {
351
                max = PCI_SPEED_133MHz_PCIX;
352
            }
353
        } else {
354
            max = PCI_SPEED_66MHz_PCIX;
355
        }
356
357
 
358
        bus->cur_bus_speed = pcix_bus_speed[(status >> 6) & 0xf];
359
360
 
361
    }
362
363
 
364
    if (pos) {
365
        u32 linkcap;
366
        u16 linksta;
367
368
 
369
        bus->max_bus_speed = pcie_link_speed[linkcap & 0xf];
370
371
 
372
        pcie_update_link_speed(bus, linksta);
373
    }
374
}
375
376
 
377
378
 
379
 
380
                       struct pci_dev *bridge, int busnr)
381
{
382
    struct pci_bus *child;
383
    int i;
384
385
 
386
     * Allocate a new bus, and inherit stuff from the parent..
387
     */
388
    child = pci_alloc_bus();
389
    if (!child)
390
        return NULL;
391
392
 
393
    child->ops = parent->ops;
394
    child->sysdata = parent->sysdata;
395
    child->bus_flags = parent->bus_flags;
396
397
 
398
     * now as the parent is not properly set up yet.  This device will get
399
     * registered later in pci_bus_add_devices()
400
     */
401
//    child->dev.class = &pcibus_class;
402
//    dev_set_name(&child->dev, "%04x:%02x", pci_domain_nr(child), busnr);
403
404
 
405
     * Set up the primary, secondary and subordinate
406
     * bus numbers.
407
     */
408
    child->number = child->secondary = busnr;
409
    child->primary = parent->secondary;
410
    child->subordinate = 0xff;
411
412
 
413
        return child;
414
415
 
416
//    child->bridge = get_device(&bridge->dev);
417
418
 
419
420
 
421
    for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
422
        child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];
423
        child->resource[i]->name = child->name;
424
    }
425
    bridge->subordinate = child;
426
427
 
428
}
429
430
 
431
{
432
    struct pci_bus *child;
433
434
 
435
    if (child) {
436
//        down_write(&pci_bus_sem);
437
        list_add_tail(&child->node, &parent->children);
438
//        up_write(&pci_bus_sem);
439
    }
440
    return child;
441
}
442
443
 
444
 
445
{
446
    struct pci_bus *parent = child->parent;
447
448
 
449
       we're going to re-assign all bus numbers. */
450
    if (!pcibios_assign_all_busses())
451
        return;
452
453
 
454
        parent->subordinate = max;
455
        pci_write_config_byte(parent->self, PCI_SUBORDINATE_BUS, max);
456
        parent = parent->parent;
457
    }
458
}
459
460
 
461
 
462
 * If it's a bridge, configure it and scan the bus behind it.
463
 * For CardBus bridges, we don't scan behind as the devices will
464
 * be handled by the bridge driver itself.
465
 *
466
 * We need to process bridges in two passes -- first we scan those
467
 * already configured by the BIOS and after we are done with all of
468
 * them, we proceed to assigning numbers to the remaining buses in
469
 * order to avoid overlaps between old and new bus numbers.
470
 */
471
int pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass)
472
{
473
    struct pci_bus *child;
474
    int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
475
    u32 buses, i, j = 0;
476
    u16 bctl;
477
    u8 primary, secondary, subordinate;
478
    int broken = 0;
479
480
 
481
    primary = buses & 0xFF;
482
    secondary = (buses >> 8) & 0xFF;
483
    subordinate = (buses >> 16) & 0xFF;
484
485
 
486
        secondary, subordinate, pass);
487
488
 
489
    if (!pass &&
490
        (primary != bus->number || secondary <= bus->number)) {
491
        dbgprintf("bus configuration invalid, reconfiguring\n");
492
        broken = 1;
493
    }
494
495
 
496
       of bus errors (in some architectures) */
497
    pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);
498
    pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
499
                  bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
500
501
 
502
        !is_cardbus && !broken) {
503
        unsigned int cmax;
504
        /*
505
         * Bus already configured by firmware, process it in the first
506
         * pass and just note the configuration.
507
         */
508
        if (pass)
509
            goto out;
510
511
 
512
         * If we already got to this bus through a different bridge,
513
         * don't re-add it. This can happen with the i450NX chipset.
514
         *
515
         * However, we continue to descend down the hierarchy and
516
         * scan remaining child buses.
517
         */
518
        child = pci_find_bus(pci_domain_nr(bus), secondary);
519
        if (!child) {
520
            child = pci_add_new_bus(bus, dev, secondary);
521
            if (!child)
522
                goto out;
523
            child->primary = primary;
524
            child->subordinate = subordinate;
525
            child->bridge_ctl = bctl;
526
        }
527
528
 
529
        if (cmax > max)
530
            max = cmax;
531
        if (child->subordinate > max)
532
            max = child->subordinate;
533
    } else {
534
        /*
535
         * We need to assign a number to this bus which we always
536
         * do in the second pass.
537
         */
538
        if (!pass) {
539
            if (pcibios_assign_all_busses() || broken)
540
                /* Temporarily disable forwarding of the
541
                   configuration cycles on all bridges in
542
                   this bus segment to avoid possible
543
                   conflicts in the second pass between two
544
                   bridges programmed with overlapping
545
                   bus ranges. */
546
                pci_write_config_dword(dev, PCI_PRIMARY_BUS,
547
                               buses & ~0xffffff);
548
            goto out;
549
        }
550
551
 
552
        pci_write_config_word(dev, PCI_STATUS, 0xffff);
553
554
 
555
         * This can happen when a bridge is hot-plugged */
556
        if (pci_find_bus(pci_domain_nr(bus), max+1))
557
            goto out;
558
        child = pci_add_new_bus(bus, dev, ++max);
559
        buses = (buses & 0xff000000)
560
              | ((unsigned int)(child->primary)     <<  0)
561
              | ((unsigned int)(child->secondary)   <<  8)
562
              | ((unsigned int)(child->subordinate) << 16);
563
564
 
565
         * yenta.c forces a secondary latency timer of 176.
566
         * Copy that behaviour here.
567
         */
568
        if (is_cardbus) {
569
            buses &= ~0xff000000;
570
            buses |= CARDBUS_LATENCY_TIMER << 24;
571
        }
572
573
 
574
         * We need to blast all three values with a single write.
575
         */
576
        pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
577
578
 
579
            child->bridge_ctl = bctl;
580
            /*
581
             * Adjust subordinate busnr in parent buses.
582
             * We do this before scanning for children because
583
             * some devices may not be detected if the bios
584
             * was lazy.
585
             */
586
            pci_fixup_parent_subordinate_busnr(child, max);
587
            /* Now we can scan all subordinate buses... */
588
            max = pci_scan_child_bus(child);
589
            /*
590
             * now fix it up again since we have found
591
             * the real value of max.
592
             */
593
            pci_fixup_parent_subordinate_busnr(child, max);
594
        } else {
595
            /*
596
             * For CardBus bridges, we leave 4 bus numbers
597
             * as cards with a PCI-to-PCI bridge can be
598
             * inserted later.
599
             */
600
            for (i=0; i
601
                struct pci_bus *parent = bus;
602
                if (pci_find_bus(pci_domain_nr(bus),
603
                            max+i+1))
604
                    break;
605
                while (parent->parent) {
606
                    if ((!pcibios_assign_all_busses()) &&
607
                        (parent->subordinate > max) &&
608
                        (parent->subordinate <= max+i)) {
609
                        j = 1;
610
                    }
611
                    parent = parent->parent;
612
                }
613
                if (j) {
614
                    /*
615
                     * Often, there are two cardbus bridges
616
                     * -- try to leave one valid bus number
617
                     * for each one.
618
                     */
619
                    i /= 2;
620
                    break;
621
                }
622
            }
623
            max += i;
624
            pci_fixup_parent_subordinate_busnr(child, max);
625
        }
626
        /*
627
         * Set the subordinate bus number to its real value.
628
         */
629
        child->subordinate = max;
630
        pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
631
    }
632
633
 
634
        (is_cardbus ? "PCI CardBus %04x:%02x" : "PCI Bus %04x:%02x"),
635
        pci_domain_nr(bus), child->number);
636
637
 
638
    while (bus->parent) {
639
        if ((child->subordinate > bus->subordinate) ||
640
            (child->number > bus->subordinate) ||
641
            (child->number < bus->number) ||
642
            (child->subordinate < bus->number)) {
643
            dbgprintf("[bus %02x-%02x] %s "
644
                "hidden behind%s bridge %s [bus %02x-%02x]\n",
645
                child->number, child->subordinate,
646
                (bus->number > child->subordinate &&
647
                 bus->subordinate < child->number) ?
648
                    "wholly" : "partially",
649
                bus->self->transparent ? " transparent" : "",
650
                "FIX BRIDGE NAME",
651
                bus->number, bus->subordinate);
652
        }
653
        bus = bus->parent;
654
    }
655
656
 
657
    pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
658
659
 
660
}
661
662
 
663
{
664
    int pos;
665
    u16 reg16;
666
667
 
668
    if (!pos)
669
        return;
670
    pdev->is_pcie = 1;
671
    pdev->pcie_cap = pos;
672
    pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, ®16);
673
    pdev->pcie_type = (reg16 & PCI_EXP_FLAGS_TYPE) >> 4;
674
}
675
676
 
677
{
678
    int pos;
679
    u16 reg16;
680
    u32 reg32;
681
682
 
683
    if (!pos)
684
        return;
685
    pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, ®16);
686
    if (!(reg16 & PCI_EXP_FLAGS_SLOT))
687
        return;
688
    pci_read_config_dword(pdev, pos + PCI_EXP_SLTCAP, ®32);
689
    if (reg32 & PCI_EXP_SLTCAP_HPC)
690
        pdev->is_hotplug_bridge = 1;
691
}
692
693
 
694
 * Read interrupt line and base address registers.
695
 * The architecture-dependent code can tweak these, of course.
696
 */
697
static void pci_read_irq(struct pci_dev *dev)
698
{
699
    unsigned char irq;
700
701
 
702
    dev->pin = irq;
703
    if (irq)
704
        pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
705
    dev->irq = irq;
706
}
707
708
 
709
 
710
 * pci_setup_device - fill in class and map information of a device
1628 serge 711
 * @dev: the device structure to fill
712
 *
713
 * Initialize the device structure with information about the device's
714
 * vendor,class,memory and IO-space addresses,IRQ lines etc.
715
 * Called at initialisation of the PCI subsystem and by CardBus services.
716
 * Returns 0 on success and negative if unknown type of device (not normal,
717
 * bridge or CardBus).
718
 */
719
int pci_setup_device(struct pci_dev *dev)
720
{
721
    u32 class;
722
    u8 hdr_type;
723
    struct pci_slot *slot;
724
    int pos = 0;
725
726
 
727
        return -EIO;
728
729
 
730
//    dev->dev.parent = dev->bus->bridge;
731
//    dev->dev.bus = &pci_bus_type;
732
    dev->hdr_type = hdr_type & 0x7f;
733
    dev->multifunction = !!(hdr_type & 0x80);
734
    dev->error_state = pci_channel_io_normal;
735
    set_pcie_port_type(dev);
736
737
 
738
        if (PCI_SLOT(dev->devfn) == slot->number)
739
            dev->slot = slot;
740
741
 
742
       set this higher, assuming the system even supports it.  */
743
    dev->dma_mask = 0xffffffff;
744
745
 
746
//             dev->bus->number, PCI_SLOT(dev->devfn),
747
//             PCI_FUNC(dev->devfn));
748
749
 
750
    dev->revision = class & 0xff;
751
    class >>= 8;                    /* upper 3 bytes */
752
    dev->class = class;
753
    class >>= 8;
754
755
 
756
         dev->vendor, dev->device, class, dev->hdr_type);
757
758
 
759
    dev->cfg_size = pci_cfg_space_size(dev);
760
761
 
762
    dev->current_state = PCI_UNKNOWN;
763
764
 
765
//    pci_fixup_device(pci_fixup_early, dev);
766
    /* device class may be changed after fixup */
767
    class = dev->class >> 8;
768
769
 
770
    case PCI_HEADER_TYPE_NORMAL:            /* standard header */
771
        if (class == PCI_CLASS_BRIDGE_PCI)
772
            goto bad;
773
        pci_read_irq(dev);
774
        pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
775
        pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
776
        pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device);
777
778
 
779
         *  Do the ugly legacy mode stuff here rather than broken chip
780
         *  quirk code. Legacy mode ATA controllers have fixed
781
         *  addresses. These are not always echoed in BAR0-3, and
782
         *  BAR0-3 in a few cases contain junk!
783
         */
784
        if (class == PCI_CLASS_STORAGE_IDE) {
785
            u8 progif;
786
            pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
787
            if ((progif & 1) == 0) {
788
                dev->resource[0].start = 0x1F0;
789
                dev->resource[0].end = 0x1F7;
790
                dev->resource[0].flags = LEGACY_IO_RESOURCE;
791
                dev->resource[1].start = 0x3F6;
792
                dev->resource[1].end = 0x3F6;
793
                dev->resource[1].flags = LEGACY_IO_RESOURCE;
794
            }
795
            if ((progif & 4) == 0) {
796
                dev->resource[2].start = 0x170;
797
                dev->resource[2].end = 0x177;
798
                dev->resource[2].flags = LEGACY_IO_RESOURCE;
799
                dev->resource[3].start = 0x376;
800
                dev->resource[3].end = 0x376;
801
                dev->resource[3].flags = LEGACY_IO_RESOURCE;
802
            }
803
        }
804
        break;
805
806
 
807
        if (class != PCI_CLASS_BRIDGE_PCI)
808
            goto bad;
809
        /* The PCI-to-PCI bridge spec requires that subtractive
810
           decoding (i.e. transparent) bridge must have programming
811
           interface code of 0x01. */
812
        pci_read_irq(dev);
813
        dev->transparent = ((dev->class & 0xff) == 1);
814
        pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
815
        set_pcie_hotplug_bridge(dev);
816
        pos = pci_find_capability(dev, PCI_CAP_ID_SSVID);
817
        if (pos) {
818
            pci_read_config_word(dev, pos + PCI_SSVID_VENDOR_ID, &dev->subsystem_vendor);
819
            pci_read_config_word(dev, pos + PCI_SSVID_DEVICE_ID, &dev->subsystem_device);
820
        }
821
        break;
822
823
 
824
        if (class != PCI_CLASS_BRIDGE_CARDBUS)
825
            goto bad;
826
        pci_read_irq(dev);
827
        pci_read_bases(dev, 1, 0);
828
        pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
829
        pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
830
        break;
831
832
 
833
        dbgprintf("unknown header type %02x, "
834
            "ignoring device\n", dev->hdr_type);
835
        return -EIO;
836
837
 
838
        dbgprintf("ignoring class %02x (doesn't match header "
839
            "type %02x)\n", class, dev->hdr_type);
840
        dev->class = PCI_CLASS_NOT_DEFINED;
841
    }
842
843
 
844
    return 0;
845
}
846
847
 
848
 
849
 
850
{
851
    struct pci_dev *dev;
852
853
 
854
    if (!dev)
855
        return NULL;
856
857
 
858
859
 
860
}
861
862
 
863
 * Read the config data for a PCI device, sanity-check it
864
 * and fill in the dev structure...
865
 */
866
static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
867
{
868
    struct pci_dev *dev;
869
    u32 l;
870
    int timeout = 10;
871
872
 
873
        return NULL;
874
875
 
876
    if (l == 0xffffffff || l == 0x00000000 ||
877
        l == 0x0000ffff || l == 0xffff0000)
1633 serge 878
        return NULL;
1628 serge 879
880
 
881
    while (l == 0xffff0001) {
882
        delay(timeout/10);
883
        timeout *= 2;
884
        if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, &l))
885
            return NULL;
886
        /* Card hasn't responded in 60 seconds?  Must be stuck. */
887
        if (timeout > 60 * 1000) {
1631 serge 888
            printk(KERN_WARNING "pci %04x:%02x:%02x.%d: not "
1628 serge 889
                    "responding\n", pci_domain_nr(bus),
890
                    bus->number, PCI_SLOT(devfn),
891
                    PCI_FUNC(devfn));
892
            return NULL;
893
        }
894
    }
895
896
 
897
    if (!dev)
898
        return NULL;
899
900
 
901
    dev->busnr = bus->number;
1867 serge 902
    dev->devfn = devfn;
1628 serge 903
    dev->vendor = l & 0xffff;
904
    dev->device = (l >> 16) & 0xffff;
905
906
 
907
        kfree(dev);
908
        return NULL;
909
    }
910
911
 
912
}
913
914
 
1631 serge 915
{
916
//    device_initialize(&dev->dev);
917
//    dev->dev.release = pci_release_dev;
918
//    pci_dev_get(dev);
919
1628 serge 920
 
1631 serge 921
//    dev->dev.dma_parms = &dev->dma_parms;
922
//    dev->dev.coherent_dma_mask = 0xffffffffull;
923
924
 
925
//    pci_set_dma_seg_boundary(dev, 0xffffffff);
926
927
 
928
//    pci_fixup_device(pci_fixup_header, dev);
929
930
 
931
    dev->state_saved = false;
932
933
 
934
//    pci_init_capabilities(dev);
935
936
 
937
     * Add the device to our list of discovered devices
938
     * and the bus list for fixup functions, etc.
939
     */
940
//    down_write(&pci_bus_sem);
941
    list_add_tail(&dev->bus_list, &bus->devices);
942
//    up_write(&pci_bus_sem);
943
}
944
945
 
1628 serge 946
{
947
    struct pci_dev *dev;
948
949
 
950
    if (dev) {
951
//        pci_dev_put(dev);
952
        return dev;
953
    }
954
955
 
956
    if (!dev)
957
        return NULL;
958
959
 
960
961
 
962
}
963
964
 
965
{
966
    u16 cap;
967
    unsigned pos, next_fn;
968
969
 
970
        return 0;
971
972
 
973
    if (!pos)
974
        return 0;
975
    pci_read_config_word(dev, pos + 4, &cap);
976
    next_fn = cap >> 8;
977
    if (next_fn <= fn)
978
        return 0;
979
    return next_fn;
980
}
981
982
 
983
{
984
    return (fn + 1) % 8;
985
}
986
987
 
988
{
989
    return 0;
990
}
991
992
 
993
{
994
    struct pci_dev *parent = bus->self;
995
    if (!parent || !pci_is_pcie(parent))
996
        return 0;
997
    if (parent->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
998
        parent->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
999
        return 1;
1000
    return 0;
1001
}
1002
1003
 
1004
 * pci_scan_slot - scan a PCI slot on a bus for devices.
1005
 * @bus: PCI bus to scan
1006
 * @devfn: slot number to scan (must have zero function.)
1007
 *
1008
 * Scan a PCI slot on the specified PCI bus for devices, adding
1009
 * discovered devices to the @bus->devices list.  New devices
1010
 * will not have is_added set.
1011
 *
1012
 * Returns the number of new devices found.
1013
 */
1014
int pci_scan_slot(struct pci_bus *bus, int devfn)
1015
{
1016
    unsigned fn, nr = 0;
1017
    struct pci_dev *dev;
1018
    unsigned (*next_fn)(struct pci_dev *, unsigned) = no_next_fn;
1019
1020
 
1021
        return 0; /* Already scanned the entire slot */
1022
1023
 
1024
    if (!dev)
1025
        return 0;
1026
    if (!dev->is_added)
1027
        nr++;
1028
1029
 
1030
        next_fn = next_ari_fn;
1031
    else if (dev->multifunction)
1032
        next_fn = next_trad_fn;
1033
1034
 
1035
        dev = pci_scan_single_device(bus, devfn + fn);
1036
        if (dev) {
1037
            if (!dev->is_added)
1038
                nr++;
1039
            dev->multifunction = 1;
1040
        }
1041
    }
1042
1043
 
1044
//    if (bus->self && nr)
1631 serge 1045
//        pcie_aspm_init_link_state(bus->self);
1046
1628 serge 1047
 
1048
}
1049
1050
 
1051
 
1052
{
1053
    unsigned int devfn, pass, max = bus->secondary;
1054
    struct pci_dev *dev;
1055
1056
 
1057
1058
 
1059
    for (devfn = 0; devfn < 0x100; devfn += 8)
1060
        pci_scan_slot(bus, devfn);
1061
1062
 
1063
    max += pci_iov_bus_range(bus);
1064
1065
 
1066
     * After performing arch-dependent fixup of the bus, look behind
1067
     * all PCI-to-PCI bridges on this bus.
1068
     */
1069
    if (!bus->is_added) {
1070
        dbgprintf("fixups for bus\n");
1071
//        pcibios_fixup_bus(bus);
1631 serge 1072
        if (pci_is_root_bus(bus))
1628 serge 1073
            bus->is_added = 1;
1074
    }
1075
1076
 
1077
        list_for_each_entry(dev, &bus->devices, bus_list) {
1078
            if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1079
                dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
1080
                max = pci_scan_bridge(bus, dev, max, pass);
1081
        }
1082
1083
 
1084
     * We've scanned the bus and so we know all about what's on
1085
     * the other side of any bridges that may be on this bus plus
1086
     * any devices.
1087
     *
1088
     * Return how far we've got finding sub-buses.
1089
     */
1090
    dbgprintf("bus scan returning with max=%02x\n", max);
1091
    return max;
1092
}
1093
1094
 
1095
 * pci_cfg_space_size - get the configuration space size of the PCI device.
1096
 * @dev: PCI device
1097
 *
1098
 * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
1099
 * have 4096 bytes.  Even if the device is capable, that doesn't mean we can
1100
 * access it.  Maybe we don't have a way to generate extended config space
1101
 * accesses, or the device is behind a reverse Express bridge.  So we try
1102
 * reading the dword at 0x100 which must either be 0 or a valid extended
1103
 * capability header.
1104
 */
1105
int pci_cfg_space_size_ext(struct pci_dev *dev)
1106
{
1107
    u32 status;
1108
    int pos = PCI_CFG_SPACE_SIZE;
1109
1110
 
1111
        goto fail;
1112
    if (status == 0xffffffff)
1113
        goto fail;
1114
1115
 
1116
1117
 
1118
    return PCI_CFG_SPACE_SIZE;
1119
}
1120
1121
 
1122
{
1123
    int pos;
1124
    u32 status;
1125
    u16 class;
1126
1127
 
1128
    if (class == PCI_CLASS_BRIDGE_HOST)
1129
        return pci_cfg_space_size_ext(dev);
1130
1131
 
1132
    if (!pos) {
1133
        pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
1134
        if (!pos)
1135
            goto fail;
1136
1137
 
1138
        if (!(status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ)))
1139
            goto fail;
1140
    }
1141
1142
 
1143
1144
 
1145
    return PCI_CFG_SPACE_SIZE;
1146
}
1147
1148
 
1149
 
1631 serge 1150
 
1151
{
1152
    int error;
1153
    struct pci_bus *b, *b2;
1154
1155
 
1156
    if (!b)
1157
        return NULL;
1158
1159
 
1160
    b->ops = ops;
1161
1162
 
1163
    if (b2) {
1164
        /* If we already got to this bus through a different bridge, ignore it */
1165
        dbgprintf("bus already known\n");
1166
        goto err_out;
1167
    }
1168
1169
 
1170
    list_add_tail(&b->node, &pci_root_buses);
1171
//    up_write(&pci_bus_sem);
1172
1173
 
1174
    b->resource[0] = &ioport_resource;
1175
    b->resource[1] = &iomem_resource;
1176
1177
 
1178
1179
 
1180
    kfree(b);
1181
    return NULL;
1182
}
1183