summaryrefslogtreecommitdiffstats
path: root/private/ntos/nthals/extender/pci/port.c
blob: e78ba1537bfd7829a76af1b9d6b5264e45199c32 (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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/*++

Copyright (c) 1995  Microsoft Corporation

Module Name:

    port.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,PcipAssignSlotResources)
#pragma alloc_text(PAGE,PcipQueryBusSlots)
#pragma alloc_text(PAGE,PcipHibernateBus)
#pragma alloc_text(PAGE,PcipResumeBus)
#pragma alloc_text(PAGE,PcipSuspendNotification)
#pragma alloc_text(PAGE,PcipReferenceDeviceHandler)
#endif


ULONG
PcipGetBusData (
    IN PBUS_HANDLER BusHandler,
    IN PBUS_HANDLER RootHandler,
    IN PCI_SLOT_NUMBER SlotNumber,
    IN PUCHAR Buffer,
    IN ULONG Offset,
    IN ULONG Length
    )
/*++

Routine Description:

    The function returns the Pci bus data for a device.

Arguments:

    BusNumber - Indicates which bus.

    VendorSpecificDevice - The VendorID (low Word) and DeviceID (High Word)

    Buffer - Supplies the space to store the data.

    Length - Supplies a count in bytes of the maximum amount to return.

Return Value:

    Returns the amount of data stored into the buffer.

    If this PCI slot has never been set, then the configuration information
    returned is zeroed.

--*/
{
    PPCI_COMMON_CONFIG  PciData;
    UCHAR               iBuffer[PCI_COMMON_HDR_LENGTH];
    PPCIBUSDATA         BusData;
    ULONG               Len;
    ULONG               i, bit;

    BusData = (PPCIBUSDATA) BusHandler->BusData;

    if (Length > sizeof (PCI_COMMON_CONFIG)) {
        Length = sizeof (PCI_COMMON_CONFIG);
    }

    Len = 0;
    PciData = (PPCI_COMMON_CONFIG) iBuffer;

    if (Offset >= PCI_COMMON_HDR_LENGTH) {
        //
        // The user did not request any data from the common
        // header.  Verify the PCI device exists, then continue
        // in the device specific area.
        //

        BusData->ReadConfig (BusHandler, SlotNumber, PciData, 0, sizeof (ULONG));

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

    } else {

        //
        // Caller requested at least some data within the
        // common header.  Read the whole header, effect the
        // fields we need to and then copy the user's requested
        // bytes from the header
        //


        //
        // Read this PCI devices slot data
        //

        Len = PCI_COMMON_HDR_LENGTH;
        BusData->ReadConfig (BusHandler, SlotNumber, PciData, 0, Len);

        if (PciData->VendorID == PCI_INVALID_VENDORID  ||
            PCI_CONFIG_TYPE (PciData) != PCI_DEVICE_TYPE) {
            PciData->VendorID = PCI_INVALID_VENDORID;
            Len = 2;       // only return invalid id

        } else {

            BusData->Pin2Line (BusHandler, RootHandler, SlotNumber, PciData);
        }

        //
        // Copy whatever data overlaps into the callers buffer
        //

        if (Len < Offset) {
            // no data at caller's buffer
            return 0;
        }

        Len -= Offset;
        if (Len > Length) {
            Len = Length;
        }

        RtlMoveMemory(Buffer, iBuffer + Offset, Len);

        Offset += Len;
        Buffer += Len;
        Length -= Len;
    }

    if (Length) {
        if (Offset >= PCI_COMMON_HDR_LENGTH) {
            //
            // The remaining Buffer comes from the Device Specific
            // area - put on the kitten gloves and read from it.
            //
            // Specific read/writes to the PCI device specific area
            // are guarenteed:
            //
            //    Not to read/write any byte outside the area specified
            //    by the caller.  (this may cause WORD or BYTE references
            //    to the area in order to read the non-dword aligned
            //    ends of the request)
            //
            //    To use a WORD access if the requested length is exactly
            //    a WORD long.
            //
            //    To use a BYTE access if the requested length is exactly
            //    a BYTE long.
            //

            BusData->ReadConfig (BusHandler, SlotNumber, Buffer, Offset, Length);
            Len += Length;
        }
    }

    return Len;
}


ULONG
PcipGetDeviceData (
    IN struct _BUS_HANDLER      *BusHandler,
    IN struct _BUS_HANDLER      *RootHandler,
    IN PDEVICE_HANDLER_OBJECT   DeviceHandler,
    IN ULONG                    DataType,
    IN PUCHAR                   Buffer,
    IN ULONG                    Offset,
    IN ULONG                    Length
    )
{
    ULONG               Status;
    PDEVICE_DATA        DeviceData;
    PCI_SLOT_NUMBER     SlotNumber;

    Status = 0;
    DeviceData = DeviceHandler2DeviceData (DeviceHandler);

    //
    // Verify caller has a valid DeviceHandler object
    //

    if (!DeviceData->Valid) {

        //
        // Obsolete object, return no data
        //

        return 0;
    }

    //
    // Get the device's data.
    //

    if (DataType == 0) {

        //
        // Type 0 is the same as GetBusData for the slot
        //

        SlotNumber.u.AsULONG = DeviceHandler->SlotNumber;
        Status =  PcipGetBusData (
                    BusHandler,
                    RootHandler,
                    SlotNumber,
                    Buffer,
                    Offset,
                    Length
                    );
    }

    return Status;
}


ULONG
PcipSetBusData (
    IN PBUS_HANDLER BusHandler,
    IN PBUS_HANDLER RootHandler,
    IN PCI_SLOT_NUMBER SlotNumber,
    IN PUCHAR Buffer,
    IN ULONG Offset,
    IN ULONG Length
    )
{
    PPCI_COMMON_CONFIG  PciData, PciData2;
    UCHAR               iBuffer[PCI_COMMON_HDR_LENGTH];
    UCHAR               iBuffer2[PCI_COMMON_HDR_LENGTH];
    PPCIBUSDATA         BusData;
    ULONG               Len, cnt;

    BusData = (PPCIBUSDATA) BusHandler->BusData;

    if (Length > sizeof (PCI_COMMON_CONFIG)) {
        Length = sizeof (PCI_COMMON_CONFIG);
    }

    Len = 0;
    PciData = (PPCI_COMMON_CONFIG) iBuffer;
    PciData2 = (PPCI_COMMON_CONFIG) iBuffer2;

    if (Offset >= PCI_COMMON_HDR_LENGTH) {
        //
        // The user did not request any data from the common
        // header.  Verify the PCI device exists, then continue in
        // the device specific area.
        //

        BusData->ReadConfig (BusHandler, SlotNumber, PciData, 0, sizeof(ULONG));

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

    } else {

        //
        // Caller requested to set at least some data within the
        // common header.
        //

        Len = PCI_COMMON_HDR_LENGTH;
        BusData->ReadConfig (BusHandler, SlotNumber, PciData, 0, Len);
        if (PciData->VendorID == PCI_INVALID_VENDORID  ||
            PCI_CONFIG_TYPE (PciData) != PCI_DEVICE_TYPE) {

            // no device, or header type unkown
            return 0;
        }

        //
        // Copy COMMON_HDR values to buffer2, then overlay callers changes.
        //

        RtlMoveMemory (iBuffer2, iBuffer, Len);
        BusData->Pin2Line (BusHandler, RootHandler, SlotNumber, PciData2);

        Len -= Offset;
        if (Len > Length) {
            Len = Length;
        }

        RtlMoveMemory (iBuffer2+Offset, Buffer, Len);

        // in case interrupt line or pin was editted
        BusData->Line2Pin (BusHandler, RootHandler, SlotNumber, PciData2, PciData);

#if DBG
        //
        // Verify R/O fields haven't changed
        //
        if (PciData2->VendorID   != PciData->VendorID       ||
            PciData2->DeviceID   != PciData->DeviceID       ||
            PciData2->RevisionID != PciData->RevisionID     ||
            PciData2->ProgIf     != PciData->ProgIf         ||
            PciData2->SubClass   != PciData->SubClass       ||
            PciData2->BaseClass  != PciData->BaseClass      ||
            PciData2->HeaderType != PciData->HeaderType     ||
            PciData2->BaseClass  != PciData->BaseClass      ||
            PciData2->u.type0.MinimumGrant   != PciData->u.type0.MinimumGrant   ||
            PciData2->u.type0.MaximumLatency != PciData->u.type0.MaximumLatency) {
                DbgPrint ("PCI SetBusData: Read-Only configuration value changed\n");
                DbgBreakPoint ();
        }
#endif
        //
        // Set new PCI configuration
        //

        BusData->WriteConfig (BusHandler, SlotNumber, iBuffer2+Offset, Offset, Len);

        Offset += Len;
        Buffer += Len;
        Length -= Len;
    }

    if (Length) {
        if (Offset >= PCI_COMMON_HDR_LENGTH) {
            //
            // The remaining Buffer comes from the Device Specific
            // area - put on the kitten gloves and write it
            //
            // Specific read/writes to the PCI device specific area
            // are guarenteed:
            //
            //    Not to read/write any byte outside the area specified
            //    by the caller.  (this may cause WORD or BYTE references
            //    to the area in order to read the non-dword aligned
            //    ends of the request)
            //
            //    To use a WORD access if the requested length is exactly
            //    a WORD long.
            //
            //    To use a BYTE access if the requested length is exactly
            //    a BYTE long.
            //

            BusData->WriteConfig (BusHandler, SlotNumber, Buffer, Offset, Length);
            Len += Length;
        }
    }

    return Len;
}

ULONG
PcipSetDeviceData (
    IN struct _BUS_HANDLER      *BusHandler,
    IN struct _BUS_HANDLER      *RootHandler,
    IN PDEVICE_HANDLER_OBJECT   DeviceHandler,
    IN ULONG                    DataType,
    IN PUCHAR                   Buffer,
    IN ULONG                    Offset,
    IN ULONG                    Length
    )
{
    ULONG               Status;
    PDEVICE_DATA        DeviceData;
    PCI_SLOT_NUMBER     SlotNumber;

    Status = 0;
    DeviceData = DeviceHandler2DeviceData (DeviceHandler);

    //
    // Verify caller has a valid DeviceHandler object
    //

    if (!DeviceData->Valid) {

        //
        // Obsolete object, return no data
        //

        return 0;
    }

    //
    // Get the device's data.
    //

    if (DataType == 0) {

        //
        // Type 0 is the same as SetBusData for the slot
        //

        SlotNumber.u.AsULONG = DeviceHandler->SlotNumber;
        Status =  PcipGetBusData (
                    BusHandler,
                    RootHandler,
                    SlotNumber,
                    Buffer,
                    Offset,
                    Length
                    );
    }

    return Status;
}



NTSTATUS
PcipAssignSlotResources (
    IN PBUS_HANDLER             BusHandler,
    IN PBUS_HANDLER             RootHandler,
    IN PUNICODE_STRING          RegistryPath,
    IN PUNICODE_STRING          DriverClassName       OPTIONAL,
    IN PDRIVER_OBJECT           DriverObject,
    IN PDEVICE_OBJECT           DeviceObject          OPTIONAL,
    IN ULONG                    SlotNumber,
    IN OUT PCM_RESOURCE_LIST   *AllocatedResources
    )
{
    CTL_ASSIGN_RESOURCES    AssignResources;
    KEVENT                  CompletionEvent;
    ULONG                   BufferSize;
    PDEVICE_HANDLER_OBJECT  DeviceHandler;
    NTSTATUS                Status;

    PAGED_CODE ();

    //
    // Foreward this request through a DeviceControl such that it
    // gets the proper synchronzation on the device
    //

    DeviceHandler = BusHandler->ReferenceDeviceHandler (
                        BusHandler,
                        BusHandler,
                        SlotNumber
                        );

    if (!DeviceHandler) {
        return STATUS_NO_SUCH_DEVICE;
    }

    AssignResources.RegistryPath        = RegistryPath;
    AssignResources.DriverClassName     = DriverClassName;
    AssignResources.DriverObject        = DriverObject;
    AssignResources.AllocatedResources  = AllocatedResources;
    BufferSize = sizeof (AssignResources);

    //
    // Make synchrous DeviceControl request
    //

    Status = HalDeviceControl (
                DeviceHandler,
                DeviceObject,
                BCTL_ASSIGN_SLOT_RESOURCES,
                &AssignResources,
                &BufferSize,
                NULL,
                NULL
                );

    //
    // Free the reference to DeviceHandler
    //

    ObDereferenceObject (DeviceHandler);

    //
    // Done
    //

    return Status;
}


NTSTATUS
PcipQueryBusSlots (
    IN PBUS_HANDLER BusHandler,
    IN PBUS_HANDLER RootHandler,
    IN ULONG        BufferSize,
    OUT PULONG      SlotNumbers,
    OUT PULONG      ReturnedLength
    )
{
    PSINGLE_LIST_ENTRY  Link;
    PPCI_PORT           PciPort;
    PDEVICE_DATA        DeviceData;
    ULONG               cnt;

    PAGED_CODE ();

    PciPort = PCIPORTDATA (BusHandler);

    //
    // Synchronize will new devices being added
    //

    ExAcquireFastMutex (&PcipMutex);

    //
    // Fill in returned buffer length, or what size buffer is needed
    //


    *ReturnedLength = PciPort->NoValidSlots  * sizeof (ULONG);
    if (BufferSize < *ReturnedLength) {

        //
        // Callers buffer is not large enough
        //

        ExReleaseFastMutex (&PcipMutex);
        return STATUS_BUFFER_TOO_SMALL;
    }

    //
    // Return caller all the possible slot number
    //

    cnt = 0;
    for (Link = PciPort->ValidSlots.Next; Link; Link = Link->Next) {
        DeviceData = CONTAINING_RECORD (Link, DEVICE_DATA, Next);
        if (DeviceData->Valid) {
            cnt += 1;
            *(SlotNumbers++) = DeviceDataSlot(DeviceData);
        }
    }

    *ReturnedLength = cnt * sizeof (ULONG);
    ExReleaseFastMutex (&PcipMutex);
    return STATUS_SUCCESS;
}


NTSTATUS
PcipDeviceControl (
    IN PHAL_DEVICE_CONTROL_CONTEXT Context
    )
{
    ULONG           i;
    ULONG           ControlCode;
    ULONG           Junk;
    PULONG          BufferLength;
    PLIST_ENTRY     OldTail;
    BOOLEAN         UseWorker;

    for (i=0; PcipControl[i].ControlHandler; i++) {
        if (PcipControl[i].ControlCode == Context->DeviceControl.ControlCode) {

            //
            // Found DeviceControl handler
            //

            Context->ContextControlHandler = (ULONG) (PcipControl + i);

            //
            // Verify callers buffer is the min required length
            //


            if (*Context->DeviceControl.BufferLength < PcipControl[i].MinBuffer) {
                Context->DeviceControl.Status = STATUS_BUFFER_TOO_SMALL;
                *Context->DeviceControl.BufferLength = PcipControl[i].MinBuffer;
                HalCompleteDeviceControl (Context);
                return STATUS_BUFFER_TOO_SMALL;
            }

            if (KeGetCurrentIrql() < DISPATCH_LEVEL ||
                 (Context->DeviceControl.ControlCode == BCTL_SET_POWER &&
                 *((PPOWER_STATE) Context->DeviceControl.Buffer) == PowerUp)){

                //
                // All slot controls, expect a power up request, may touch
                // paged code or data.  If the current irql is low enough or
                // this is a power up go dispatch now; otherwise, queue the
                // request to a worker thread.
                //

                PcipDispatchControl (Context);

            } else {

                //
                // Enqueue to worker thread
                //

                ExInterlockedInsertTailList (
                    &PcipControlWorkerList,
                    (PLIST_ENTRY) &Context->ContextWorkQueue,
                    &PcipSpinlock
                    );

                //
                // Make sure worker is requested
                //

                PcipStartWorker ();
            }


            return STATUS_PENDING;
        }
    }

    //
    // Unkown control code
    //

    return STATUS_INVALID_PARAMETER;
}

PDEVICE_HANDLER_OBJECT
PcipReferenceDeviceHandler (
    IN struct _BUS_HANDLER      *BusHandler,
    IN struct _BUS_HANDLER      *RootHandler,
    IN PCI_SLOT_NUMBER           SlotNumber
    )
{
    PDEVICE_DATA            DeviceData;
    PDEVICE_HANDLER_OBJECT  DeviceHandler;
    PPCI_PORT               PciPort;
    NTSTATUS                Status;

    PAGED_CODE ();

    ExAcquireFastMutex (&PcipMutex);

    PciPort = PCIPORTDATA(BusHandler);
    DeviceData = PcipFindDeviceData (PciPort, SlotNumber);
    DeviceHandler = NULL;
    if (DeviceData) {
        DeviceHandler = DeviceData2DeviceHandler (DeviceData);
        Status = ObReferenceObjectByPointer(
                    DeviceHandler,
                    FILE_READ_DATA | FILE_WRITE_DATA,
                    *IoDeviceHandlerObjectType,
                    KernelMode
                    );

        if (!NT_SUCCESS(Status)) {
            DeviceHandler = NULL;
        }
    }

    ExReleaseFastMutex (&PcipMutex);
    return DeviceHandler;
}



NTSTATUS
PcipHibernateBus (
    IN PBUS_HANDLER BusHandler,
    IN PBUS_HANDLER RootHandler
    )
{
    return STATUS_NOT_IMPLEMENTED;
}




NTSTATUS
PcipResumeBus (
    IN PBUS_HANDLER BusHandler,
    IN PBUS_HANDLER RootHandler
    )
{
    return STATUS_NOT_IMPLEMENTED;
}

VOID
PcipSuspendNotification (
    IN PVOID    CallbackContext,
    IN PVOID    Argument1,
    IN PVOID    Argument2
    )
{
    PAGED_CODE();

    switch ((ULONG) Argument1) {
        case 0:
            //
            // Lock code down which might be needed to perform a suspend
            //

            ASSERT (PciCodeLock == NULL);
            PciCodeLock = MmLockPagableCodeSection (&PcipHibernateBus);
            break;

        case 1:
            //
            // Release the code lock
            //

            MmUnlockPagableImageSection (PciCodeLock);
            PciCodeLock = NULL;
            break;
    }
}