summaryrefslogtreecommitdiffstats
path: root/private/ntos/nthals/extender/pci/bus.c
blob: bd6e3aaf57adaa9fdda9f8d2aa9394233bf673d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/*++

Copyright (c) 1995  Microsoft Corporation

Module Name:

    bus.c

Abstract:


Author:

    Ken Reneris (kenr) March-13-1885

Environment:

    Kernel mode only.

Revision History:

--*/

#include "pciport.h"


#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE,PcipCheckBus)
#pragma alloc_text(PAGE,PciCtlCheckDevice)
#pragma alloc_text(PAGE,PcipCrackBAR)
#pragma alloc_text(PAGE,PcipVerifyBarBits)
#pragma alloc_text(PAGE,PcipGetBarBits)
#pragma alloc_text(PAGE,PcipFindDeviceData)
#endif


VOID
PcipCheckBus (
    PPCI_PORT   PciPort,
    BOOLEAN     Initialize
    )
{
    NTSTATUS                Status;
    PBUS_HANDLER            Handler;
    PCI_SLOT_NUMBER         SlotNumber;
    ULONG                   Device, Function;
    PPCIBUSDATA             PciBusData;
    PPCI_COMMON_CONFIG      PciData;
    UCHAR                   buffer[PCI_COMMON_HDR_LENGTH];
    BOOLEAN                 BusCheck, SkipDevice;
    PDEVICE_DATA            DeviceData;
    PDEVICE_HANDLER_OBJECT  DeviceHandler;
    ULONG                   ObjectSize;
    OBJECT_ATTRIBUTES       ObjectAttributes;
    HANDLE                  Handle;
    PSINGLE_LIST_ENTRY      *Link;
    BOOLEAN                 State;
    POWER_STATE             PowerState;
    ULONG                   BufferSize;

    PAGED_CODE();

    Handler = PciPort->Handler;
    BusCheck = FALSE;
    PciBusData = (PPCIBUSDATA) (Handler->BusData);
    PciData = (PPCI_COMMON_CONFIG) buffer;

    //
    // We may be removing references to this bus handler, so add
    // a reference now
    //

    HalReferenceBusHandler (Handler);

    //
    // Check for any obslete device data entries
    //

    ExAcquireFastMutex (&PcipMutex);

    Link = &PciPort->ValidSlots.Next;
    while (*Link) {
        DeviceData = CONTAINING_RECORD (*Link, DEVICE_DATA, Next);
        if (!DeviceData->Valid) {

            //
            // Remove it from the list
            //

            *Link = (*Link)->Next;
            BusCheck = TRUE;
            PciPort->NoValidSlots -= 1;
            HalDereferenceBusHandler (Handler);

            //
            // Dereference each obsolete device handler object once.   This
            // counters the original reference made to the device handler
            // object when the object was created by this driver.
            //

            DeviceHandler = DeviceData2DeviceHandler(DeviceData);
            ObDereferenceObject (DeviceHandler);
            continue;
        }

        Link = & ((*Link)->Next);
    }

    ExReleaseFastMutex (&PcipMutex);


    //
    // Check the bus for new devices
    //

    SlotNumber.u.AsULONG = 0;
    for (Device=0; Device < PCI_MAX_DEVICES; Device++) {
        SlotNumber.u.bits.DeviceNumber = Device;
        for (Function=0; Function < PCI_MAX_FUNCTION; Function++) {
            SlotNumber.u.bits.FunctionNumber = Function;

            //
            // Read in the device id
            //

            PciBusData->ReadConfig (
                Handler,
                SlotNumber,
                PciData,
                0,
                PCI_COMMON_HDR_LENGTH
                );

            //
            // If not valid, skip it
            //

            if (PciData->VendorID == PCI_INVALID_VENDORID ||
                PciData->VendorID == 0) {
                break;
            }

            //
            // Check to see if this known configuration type
            //

            switch (PCI_CONFIG_TYPE(PciData)) {
                case PCI_DEVICE_TYPE:
                case PCI_BRIDGE_TYPE:
                    SkipDevice = FALSE;
                    break;
                default:
                    SkipDevice = TRUE;
                    break;
            }

            if (SkipDevice) {
                break;
            }

            ExAcquireFastMutex (&PcipMutex);
            DeviceData = PcipFindDeviceData (PciPort, SlotNumber);

            if (DeviceData == NULL) {

                //
                // Initialize the object attributes that will be used to create the
                // Device Handler Object.
                //

                InitializeObjectAttributes(
                    &ObjectAttributes,
                    NULL,
                    0,
                    NULL,
                    NULL
                    );

                ObjectSize = PcipDeviceHandlerObjectSize + sizeof (DEVICE_DATA);

                //
                // Create the object
                //

                Status = ObCreateObject(
                            KernelMode,
                            *IoDeviceHandlerObjectType,
                            &ObjectAttributes,
                            KernelMode,
                            NULL,
                            ObjectSize,
                            0,
                            0,
                            (PVOID *) &DeviceHandler
                            );

                if (NT_SUCCESS(Status)) {
                    RtlZeroMemory (DeviceHandler, ObjectSize);

                    DeviceHandler->Type = (USHORT) *IoDeviceHandlerObjectType;
                    DeviceHandler->Size = (USHORT) ObjectSize;
                    DeviceHandler->SlotNumber = SlotNumber.u.AsULONG;

                    //
                    // Get a reference to the object
                    //

                    Status = ObReferenceObjectByPointer(
                                DeviceHandler,
                                FILE_READ_DATA | FILE_WRITE_DATA,
                                *IoDeviceHandlerObjectType,
                                KernelMode
                                );
                }

                if (NT_SUCCESS(Status)) {

                    //
                    // Insert it into the object table
                    //

                    Status = ObInsertObject(
                                DeviceHandler,
                                NULL,
                                FILE_READ_DATA | FILE_WRITE_DATA,
                                0,
                                NULL,
                                &Handle
                            );
                }


                if (!NT_SUCCESS(Status)) {

                    //
                    // Object not created correctly
                    //

                    ExReleaseFastMutex (&PcipMutex);
                    break;
                }

                ZwClose (Handle);


                //
                // Intialize structure to track device
                //

                DebugPrint ((8, "PCI: adding slot %x (for device %04x-%04x)\n",
                    SlotNumber,
                    PciData->VendorID,
                    PciData->DeviceID
                    ));

                DeviceData = DeviceHandler2DeviceData (DeviceHandler);

                //
                // Get BAR decode bits which are supported
                //

                Status = PcipGetBarBits (DeviceData, PciPort->Handler);

                if (NT_SUCCESS(Status)) {

                    //
                    // Add it to the list of devices for this bus
                    //

                    DeviceHandler->BusHandler = Handler;
                    HalReferenceBusHandler (Handler);

                    DeviceData->Valid = TRUE;
                    PciPort->NoValidSlots += 1;
                    PushEntryList (&PciPort->ValidSlots, &DeviceData->Next);
                    BusCheck = TRUE;
                }

                //
                // Obtain an extra reference to the device handler for
                // sending some DeviceControls
                //

                ObReferenceObjectByPointer (
                    DeviceHandler,
                    FILE_READ_DATA | FILE_WRITE_DATA,
                    *IoDeviceHandlerObjectType,
                    KernelMode
                    );

                //
                // If we are installing the initail bus support, then
                // all devices are installed as locked & powered up.
                // If this is a dynamically located new device, then
                // the device is started as unlocked & powered down.
                //

                if (Initialize) {

                    //
                    // Set initial state as locked
                    //

                    State = TRUE;
                    BufferSize = sizeof (State);
                    HalDeviceControl (
                        DeviceHandler,
                        NULL,
                        BCTL_SET_LOCK,
                        &State,
                        &BufferSize,
                        NULL,
                        NULL
                        );

                    //
                    // Set initial power state as Powered Up
                    //

                    PowerState = PowerUp;
                    BufferSize = sizeof (PowerState);
                    HalDeviceControl (
                        DeviceHandler,
                        NULL,
                        BCTL_SET_POWER,
                        (PVOID) &PowerState,
                        &BufferSize,
                        NULL,
                        NULL
                        );
                }

                //
                // Free once
                //

                ObDereferenceObject (DeviceHandler);
            }

            ExReleaseFastMutex (&PcipMutex);
        }
    }

    //
    // Undo reference to bus handler from top of function
    //

    HalDereferenceBusHandler (Handler);

    //
    // Do we need to notify the system buscheck callback?
    //

    if (BusCheck) {
        ExNotifyCallback (
            PciHalCallbacks.BusCheck,
            (PVOID) PciPort->Handler->InterfaceType,
            (PVOID) PciPort->Handler->BusNumber
        );
    }

}

NTSTATUS
PcipVerifyBarBits (
    PDEVICE_DATA      DeviceData,
    PBUS_HANDLER    Handler
    )
{
    NTSTATUS        Status;

    PAGED_CODE ();

    Status = STATUS_SUCCESS;
    if (!DeviceData->BARBitsSet) {

        DeviceData->BARBitsSet = TRUE;
        Status = PcipGetBarBits (DeviceData, Handler);

        if (!NT_SUCCESS(Status)) {
            DeviceData->BARBitsSet = FALSE;
        }
    }

    return Status;
}



NTSTATUS
PcipGetBarBits (
    PDEVICE_DATA        DeviceData,
    PBUS_HANDLER        Handler
    )
{
    PPCIBUSDATA         PciBusData;
    PULONG              BaseAddress[PCI_TYPE0_ADDRESSES + 1];
    ULONG               NoBaseAddress, RomIndex;
    PULONG              BaseAddress2[PCI_TYPE0_ADDRESSES + 1];
    ULONG               NoBaseAddress2, RomIndex2;
    ULONG               j;
    PPCI_COMMON_CONFIG  PciData;
    UCHAR               buffer[PCI_COMMON_HDR_LENGTH];
    LONGLONG            bits, base, length, max;
    BOOLEAN             BrokenDevice;
    PCI_SLOT_NUMBER     SlotNumber;

    PAGED_CODE ();

    PciData = (PPCI_COMMON_CONFIG) buffer;
    PciBusData = (PPCIBUSDATA) (Handler->BusData);
    SlotNumber.u.AsULONG = DeviceDataSlot(DeviceData);

    //
    // Get the device's current configuration
    //

    if (!DeviceData->CurrentConfig) {
        DeviceData->CurrentConfig = (PPCI_COMMON_CONFIG)
                ExAllocatePoolWithTag (NonPagedPool, PCI_COMMON_HDR_LENGTH, 'cICP');

        if (!DeviceData->CurrentConfig) {
            return STATUS_INSUFFICIENT_RESOURCES;
        }
    }

    PciBusData->ReadConfig (
        Handler,
        SlotNumber,
        PciData,
        0,
        PCI_COMMON_HDR_LENGTH
        );

    RtlCopyMemory (DeviceData->CurrentConfig, PciData, PCI_COMMON_HDR_LENGTH);

    //
    // Get BaseAddress array, and check if ROM was originally enabled
    //

    DeviceData->EnableRom = PcipCalcBaseAddrPointers (
                            DeviceData,
                            PciData,
                            BaseAddress,
                            &NoBaseAddress,
                            &RomIndex
                            );

    //
    // If the device's current configuration isn't enabled, then set for
    // not powered on
    //

    if (!(PciData->Command & (PCI_ENABLE_IO_SPACE | PCI_ENABLE_MEMORY_SPACE))) {
        DeviceData->Power  = FALSE;
        DeviceData->Locked = FALSE;
    }

    //
    // Check to see if there are any BARs
    //

    for (j=0; j < NoBaseAddress; j++) {
        DeviceData->BARBits[j] = 0;
        if (*BaseAddress[j]) {
            DeviceData->BARBitsSet = TRUE;
        }
    }

    //
    // If not BarBitsSet, then don't attempt to determine the possible
    // device settings now
    //

    if (!DeviceData->BARBitsSet) {
        DebugPrint ((2, "PCI: ghost added device %04x-%04x in slot %x\n",
                PciData->VendorID,
                PciData->DeviceID,
                SlotNumber));

        return STATUS_SUCCESS;
    }

    //
    // Set BARs to all on, and read them back
    //

    DebugPrint ((3, "PCI: getting valid BAR bits on device %04x-%04x in slot %x\n",
        PciData->VendorID,
        PciData->DeviceID,
        SlotNumber
        ));


    PciData->Command &= ~(PCI_ENABLE_IO_SPACE |
                          PCI_ENABLE_MEMORY_SPACE |
                          PCI_ENABLE_BUS_MASTER);

    for (j=0; j < NoBaseAddress; j++) {
        *BaseAddress[j] = 0xFFFFFFFF;
    }

    PciBusData->WriteConfig (
        Handler,
        SlotNumber,
        PciData,
        0,
        PCI_COMMON_HDR_LENGTH
        );

    PciBusData->ReadConfig (
        Handler,
        SlotNumber,
        PciData,
        0,
        PCI_COMMON_HDR_LENGTH
        );


    PcipCalcBaseAddrPointers (
        DeviceData,
        PciData,
        BaseAddress,
        &NoBaseAddress,
        &RomIndex
        );

    PcipCalcBaseAddrPointers (
        DeviceData,
        DeviceData->CurrentConfig,
        BaseAddress2,
        &NoBaseAddress2,
        &RomIndex2
        );


    for (j=0; j < NoBaseAddress; j++) {
        if (*BaseAddress[j]) {

            //
            // Remember the original bits
            //

            DeviceData->BARBits[j] = *BaseAddress[j];
            if (Is64BitBaseAddress(*BaseAddress[j])) {
                DeviceData->BARBits[j+1] = *BaseAddress[j+1];
            }

            //
            // Crack bits & check for BrokenDevice
            //

            BrokenDevice = PcipCrackBAR (
                                BaseAddress2,
                                DeviceData->BARBits,
                                &j,
                                &base,
                                &length,
                                &max
                                );

            if (BrokenDevice) {
                DeviceData->BrokenDevice = TRUE;
            }
        }
    }

    if (DeviceData->BrokenDevice) {
        DebugPrint ((2, "PCI: added defective device %04x-%04x in slot %x\n",
                PciData->VendorID,
                PciData->DeviceID,
                SlotNumber));

    } else {
        DebugPrint ((2, "PCI: added device %04x-%04x in slot %x\n",
                PciData->VendorID,
                PciData->DeviceID,
                SlotNumber));
    }

    return STATUS_SUCCESS;
}

PDEVICE_DATA
PcipFindDeviceData (
     IN PPCI_PORT           PciPort,
     IN PCI_SLOT_NUMBER     SlotNumber
    )
{
    PDEVICE_DATA            DeviceData;
    PSINGLE_LIST_ENTRY      Link;
    PDEVICE_HANDLER_OBJECT  DeviceHandler;

    PAGED_CODE ();

    for (Link = PciPort->ValidSlots.Next; Link; Link = Link->Next) {
        DeviceData = CONTAINING_RECORD (Link, DEVICE_DATA, Next);
        DeviceHandler = DeviceData2DeviceHandler(DeviceData);
        if (DeviceHandler->SlotNumber == SlotNumber.u.AsULONG) {
            break;
        }
    }

    if (!Link) {
        return NULL;
    }

    return DeviceData;
}


BOOLEAN
PcipCrackBAR (
    IN PULONG       *BaseAddress,
    IN PULONG       BarBits,
    IN OUT PULONG   Index,
    OUT PLONGLONG   pbase,
    OUT PLONGLONG   plength,
    OUT PLONGLONG   pmax
    )
{
    LONGLONG    base, length, max, bits;
    BOOLEAN     Status;

    PAGED_CODE ();

    //
    // Get initial base & bits
    //

    base = *BaseAddress[*Index];
    bits = BarBits[*Index];

    if (Is64BitBaseAddress(base)) {
        *Index += 1;
        base |= ((LONGLONG) *BaseAddress[*Index]) << 32;
        bits |= ((LONGLONG) BarBits[*Index]) << 32;
    }

    //
    // Scan for first set bit, that's the BARs length and alignment
    //

    length = (base & PCI_ADDRESS_IO_SPACE) ? 1 << 2 : 1 << 4;
    while (!(bits & length)  &&  length) {
        length <<= 1;
    }

    //
    // Scan for last set bit, that's that BARs max address + 1
    //

    for (max = length; bits & max; max <<= 1) ;
    max -= 1;

    //
    // Check for defective BAR
    //

    Status = (bits & ~max) ? TRUE : FALSE;

    //
    // return results
    //

    *pbase   = base;
    *plength = length;
    *pmax    = max;
    return Status;
}