3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/hal/x86/dma.c
6 * PURPOSE: DMA functions
7 * PROGRAMMERS: David Welch (welch@mcmail.com)
8 * Filip Navara (navaraf@reactos.com)
14 * @page DMA Implementation Notes
20 * Abstract encapsulation of physically contiguous buffer that resides
21 * in memory accessible by both the DMA device / controller and the system.
22 * The map registers are allocated and distributed on demand and are
25 * The actual use of map registers is to allow transfers from/to buffer
26 * located in physical memory at address inaccessible by the DMA device /
27 * controller directly. For such transfers the map register buffers
28 * are used as intermediate data storage.
32 * A container for map registers (typically corresponding to one physical
33 * bus connection type). There can be master adapters for 24-bit address
34 * ranges, 32-bit address ranges, etc. Every time a new DMA adapter is
35 * created it's associated with a corresponding master adapter that
36 * is used for any map register allocation requests.
38 * - Bus-master / Slave DMA
40 * Slave DMA is term used for DMA transfers done by the system (E)ISA
41 * controller as opposed to transfers mastered by the device itself
44 * For slave DMA special care is taken to actually access the system
45 * controller and handle the transfers. The relevant code is in
46 * HalpDmaInitializeEisaAdapter, HalReadDmaCounter, IoFlushAdapterBuffers
51 * - Allocation of map registers
53 * Initial set of map registers is allocated on the system start to
54 * ensure that low memory won't get filled up later. Additional map
55 * registers are allocated as needed by HalpGrowMapBuffers. This
56 * routine is called on two places:
58 * - HalGetAdapter, since we're at PASSIVE_LEVEL and it's known that
59 * more map registers will probably be needed.
60 * - IoAllocateAdapterChannel (indirectly using HalpGrowMapBufferWorker
61 * since we're at DISPATCH_LEVEL and call HalpGrowMapBuffers directly)
62 * when no more map registers are free.
64 * Note that even if no more map registers can be allocated it's not
65 * the end of the world. The adapters waiting for free map registers
66 * are queued in the master adapter's queue and once one driver hands
67 * back it's map registers (using IoFreeMapRegisters or indirectly using
68 * the execution routine callback in IoAllocateAdapterChannel) the
69 * queue gets processed and the map registers are reassigned.
72 /* INCLUDES *****************************************************************/
80 #define MAX_SG_ELEMENTS 0x10
83 static KEVENT HalpDmaLock
;
84 static LIST_ENTRY HalpDmaAdapterList
;
85 static PADAPTER_OBJECT HalpEisaAdapter
[8];
87 static BOOLEAN HalpEisaDma
;
89 static PADAPTER_OBJECT HalpMasterAdapter
;
92 static const ULONG_PTR HalpEisaPortPage
[8] = {
93 FIELD_OFFSET(DMA_PAGE
, Channel0
),
94 FIELD_OFFSET(DMA_PAGE
, Channel1
),
95 FIELD_OFFSET(DMA_PAGE
, Channel2
),
96 FIELD_OFFSET(DMA_PAGE
, Channel3
),
98 FIELD_OFFSET(DMA_PAGE
, Channel5
),
99 FIELD_OFFSET(DMA_PAGE
, Channel6
),
100 FIELD_OFFSET(DMA_PAGE
, Channel7
)
104 static DMA_OPERATIONS HalpDmaOperations
= {
105 sizeof(DMA_OPERATIONS
),
106 (PPUT_DMA_ADAPTER
)HalPutDmaAdapter
,
107 (PALLOCATE_COMMON_BUFFER
)HalAllocateCommonBuffer
,
108 (PFREE_COMMON_BUFFER
)HalFreeCommonBuffer
,
109 NULL
, /* Initialized in HalpInitDma() */
110 NULL
, /* Initialized in HalpInitDma() */
111 NULL
, /* Initialized in HalpInitDma() */
112 NULL
, /* Initialized in HalpInitDma() */
113 NULL
, /* Initialized in HalpInitDma() */
114 (PGET_DMA_ALIGNMENT
)HalpDmaGetDmaAlignment
,
115 (PREAD_DMA_COUNTER
)HalReadDmaCounter
,
116 /* FIXME: Implement the S/G funtions. */
117 (PGET_SCATTER_GATHER_LIST
)HalGetScatterGatherList
,
118 (PPUT_SCATTER_GATHER_LIST
)HalPutScatterGatherList
,
119 NULL
/*(PCALCULATE_SCATTER_GATHER_LIST_SIZE)HalCalculateScatterGatherListSize*/,
120 NULL
/*(PBUILD_SCATTER_GATHER_LIST)HalBuildScatterGatherList*/,
121 NULL
/*(PBUILD_MDL_FROM_SCATTER_GATHER_LIST)HalBuildMdlFromScatterGatherList*/
125 #define MAX_MAP_REGISTERS 64
127 #define TAG_DMA ' AMD'
129 /* FUNCTIONS *****************************************************************/
137 * Initialize the DMA Operation table
139 HalpDmaOperations
.AllocateAdapterChannel
= (PALLOCATE_ADAPTER_CHANNEL
)IoAllocateAdapterChannel
;
140 HalpDmaOperations
.FlushAdapterBuffers
= (PFLUSH_ADAPTER_BUFFERS
)IoFlushAdapterBuffers
;
141 HalpDmaOperations
.FreeAdapterChannel
= (PFREE_ADAPTER_CHANNEL
)IoFreeAdapterChannel
;
142 HalpDmaOperations
.FreeMapRegisters
= (PFREE_MAP_REGISTERS
)IoFreeMapRegisters
;
143 HalpDmaOperations
.MapTransfer
= (PMAP_TRANSFER
)IoMapTransfer
;
146 * Check if Extended DMA is available. We're just going to do a random
149 WRITE_PORT_UCHAR((PUCHAR
)FIELD_OFFSET(EISA_CONTROL
, DmaController2Pages
.Channel2
), 0x2A);
150 if (READ_PORT_UCHAR((PUCHAR
)FIELD_OFFSET(EISA_CONTROL
, DmaController2Pages
.Channel2
)) == 0x2A)
156 * Intialize all the global variables and allocate master adapter with
159 InitializeListHead(&HalpDmaAdapterList
);
160 KeInitializeEvent(&HalpDmaLock
, NotificationEvent
, TRUE
);
161 HalpMasterAdapter
= HalpDmaAllocateMasterAdapter();
164 * Setup the HalDispatchTable callback for creating PnP DMA adapters. It's
165 * used by IoGetDmaAdapter in the kernel.
167 HalGetDmaAdapter
= HalpGetDmaAdapter
;
172 * @name HalpGetAdapterMaximumPhysicalAddress
174 * Get the maximum physical address acceptable by the device represented
175 * by the passed DMA adapter.
179 HalpGetAdapterMaximumPhysicalAddress(IN PADAPTER_OBJECT AdapterObject
)
181 PHYSICAL_ADDRESS HighestAddress
;
183 if (AdapterObject
->MasterDevice
)
185 if (AdapterObject
->Dma64BitAddresses
)
187 HighestAddress
.QuadPart
= 0xFFFFFFFFFFFFFFFFULL
;
188 return HighestAddress
;
190 else if (AdapterObject
->Dma32BitAddresses
)
192 HighestAddress
.QuadPart
= 0xFFFFFFFF;
193 return HighestAddress
;
197 HighestAddress
.QuadPart
= 0xFFFFFF;
198 return HighestAddress
;
203 * @name HalpGrowMapBuffers
205 * Allocate initial, or additional, map buffers for DMA master adapter.
207 * @param MasterAdapter
208 * DMA master adapter to allocate buffers for.
209 * @param SizeOfMapBuffers
210 * Size of the map buffers to allocate (not including the size
211 * already allocated).
215 HalpGrowMapBuffers(IN PADAPTER_OBJECT AdapterObject
,
216 IN ULONG SizeOfMapBuffers
)
218 PVOID VirtualAddress
;
219 PHYSICAL_ADDRESS PhysicalAddress
;
220 PHYSICAL_ADDRESS HighestAcceptableAddress
;
221 PHYSICAL_ADDRESS LowestAcceptableAddress
;
222 PHYSICAL_ADDRESS BoundryAddressMultiple
;
224 ULONG MapRegisterCount
;
226 /* Check if enough map register slots are available. */
227 MapRegisterCount
= BYTES_TO_PAGES(SizeOfMapBuffers
);
228 if (MapRegisterCount
+ AdapterObject
->NumberOfMapRegisters
> MAX_MAP_REGISTERS
)
230 DPRINT("No more map register slots available! (Current: %d | Requested: %d | Limit: %d)\n",
231 AdapterObject
->NumberOfMapRegisters
,
238 * Allocate memory for the new map registers. For 32-bit adapters we use
239 * two passes in order not to waste scare resource (low memory).
241 HighestAcceptableAddress
= HalpGetAdapterMaximumPhysicalAddress(AdapterObject
);
242 LowestAcceptableAddress
.HighPart
= 0;
243 LowestAcceptableAddress
.LowPart
= HighestAcceptableAddress
.LowPart
== 0xFFFFFFFF ? 0x1000000 : 0;
244 BoundryAddressMultiple
.QuadPart
= 0;
246 VirtualAddress
= MmAllocateContiguousMemorySpecifyCache(MapRegisterCount
<< PAGE_SHIFT
,
247 LowestAcceptableAddress
,
248 HighestAcceptableAddress
,
249 BoundryAddressMultiple
,
251 if (!(VirtualAddress
) && (LowestAcceptableAddress
.LowPart
))
253 LowestAcceptableAddress
.LowPart
= 0;
254 VirtualAddress
= MmAllocateContiguousMemorySpecifyCache(MapRegisterCount
<< PAGE_SHIFT
,
255 LowestAcceptableAddress
,
256 HighestAcceptableAddress
,
257 BoundryAddressMultiple
,
261 if (!VirtualAddress
) return FALSE
;
263 PhysicalAddress
= MmGetPhysicalAddress(VirtualAddress
);
266 * All the following must be done with the master adapter lock held
267 * to prevent corruption.
269 KeAcquireSpinLock(&AdapterObject
->SpinLock
, &OldIrql
);
272 * Setup map register entries for the buffer allocated. Each entry has
273 * a virtual and physical address and corresponds to PAGE_SIZE large
276 if (MapRegisterCount
> 0)
278 PROS_MAP_REGISTER_ENTRY CurrentEntry
, PreviousEntry
;
280 CurrentEntry
= AdapterObject
->MapRegisterBase
+ AdapterObject
->NumberOfMapRegisters
;
284 * Leave one entry free for every non-contiguous memory region
285 * in the map register bitmap. This ensures that we can search
286 * using RtlFindClearBits for contiguous map register regions.
288 * Also for non-EISA DMA leave one free entry for every 64Kb
289 * break, because the DMA controller can handle only coniguous
292 if (CurrentEntry
!= AdapterObject
->MapRegisterBase
)
294 PreviousEntry
= CurrentEntry
- 1;
295 if ((PreviousEntry
->PhysicalAddress
.LowPart
+ PAGE_SIZE
) == PhysicalAddress
.LowPart
)
299 if ((PreviousEntry
->PhysicalAddress
.LowPart
^ PhysicalAddress
.LowPart
) & 0xFFFF0000)
302 AdapterObject
->NumberOfMapRegisters
++;
309 AdapterObject
->NumberOfMapRegisters
++;
313 RtlClearBit(AdapterObject
->MapRegisters
,
314 (ULONG
)(CurrentEntry
- AdapterObject
->MapRegisterBase
));
315 CurrentEntry
->VirtualAddress
= VirtualAddress
;
316 CurrentEntry
->PhysicalAddress
= PhysicalAddress
;
318 PhysicalAddress
.LowPart
+= PAGE_SIZE
;
319 VirtualAddress
= (PVOID
)((ULONG_PTR
)VirtualAddress
+ PAGE_SIZE
);
322 AdapterObject
->NumberOfMapRegisters
++;
324 } while (MapRegisterCount
);
327 KeReleaseSpinLock(&AdapterObject
->SpinLock
, OldIrql
);
333 * @name HalpDmaAllocateMasterAdapter
335 * Helper routine to allocate and initialize master adapter object and it's
336 * associated map register buffers.
342 HalpDmaAllocateMasterAdapter(VOID
)
344 PADAPTER_OBJECT MasterAdapter
;
345 ULONG Size
, SizeOfBitmap
;
347 SizeOfBitmap
= MAX_MAP_REGISTERS
;
348 Size
= sizeof(ADAPTER_OBJECT
);
349 Size
+= sizeof(RTL_BITMAP
);
350 Size
+= (SizeOfBitmap
+ 7) >> 3;
352 MasterAdapter
= ExAllocatePoolWithTag(NonPagedPool
, Size
, TAG_DMA
);
353 if (!MasterAdapter
) return NULL
;
355 RtlZeroMemory(MasterAdapter
, Size
);
357 KeInitializeSpinLock(&MasterAdapter
->SpinLock
);
358 InitializeListHead(&MasterAdapter
->AdapterQueue
);
360 MasterAdapter
->MapRegisters
= (PVOID
)(MasterAdapter
+ 1);
361 RtlInitializeBitMap(MasterAdapter
->MapRegisters
,
362 (PULONG
)(MasterAdapter
->MapRegisters
+ 1),
364 RtlSetAllBits(MasterAdapter
->MapRegisters
);
365 MasterAdapter
->NumberOfMapRegisters
= 0;
366 MasterAdapter
->CommittedMapRegisters
= 0;
368 MasterAdapter
->MapRegisterBase
= ExAllocatePoolWithTag(NonPagedPool
,
370 sizeof(ROS_MAP_REGISTER_ENTRY
),
372 if (!MasterAdapter
->MapRegisterBase
)
374 ExFreePool(MasterAdapter
);
378 RtlZeroMemory(MasterAdapter
->MapRegisterBase
,
379 SizeOfBitmap
* sizeof(ROS_MAP_REGISTER_ENTRY
));
380 if (!HalpGrowMapBuffers(MasterAdapter
, 0x10000))
382 ExFreePool(MasterAdapter
);
386 return MasterAdapter
;
390 * @name HalpDmaAllocateChildAdapter
392 * Helper routine of HalGetAdapter. Allocate child adapter object and
393 * fill out some basic fields.
399 HalpDmaAllocateChildAdapter(IN ULONG NumberOfMapRegisters
,
400 IN PDEVICE_DESCRIPTION DeviceDescription
)
402 PADAPTER_OBJECT AdapterObject
;
403 OBJECT_ATTRIBUTES ObjectAttributes
;
407 InitializeObjectAttributes(&ObjectAttributes
,
409 OBJ_KERNEL_HANDLE
| OBJ_PERMANENT
,
413 Status
= ObCreateObject(KernelMode
,
418 sizeof(ADAPTER_OBJECT
),
421 (PVOID
)&AdapterObject
);
422 if (!NT_SUCCESS(Status
)) return NULL
;
424 Status
= ObReferenceObjectByPointer(AdapterObject
,
425 FILE_READ_DATA
| FILE_WRITE_DATA
,
428 if (!NT_SUCCESS(Status
)) return NULL
;
430 RtlZeroMemory(AdapterObject
, sizeof(ADAPTER_OBJECT
));
432 Status
= ObInsertObject(AdapterObject
,
434 FILE_READ_DATA
| FILE_WRITE_DATA
,
438 if (!NT_SUCCESS(Status
)) return NULL
;
442 AdapterObject
->DmaHeader
.Version
= (USHORT
)DeviceDescription
->Version
;
443 AdapterObject
->DmaHeader
.Size
= sizeof(ADAPTER_OBJECT
);
444 AdapterObject
->DmaHeader
.DmaOperations
= &HalpDmaOperations
;
445 AdapterObject
->MapRegistersPerChannel
= 1;
446 AdapterObject
->Dma32BitAddresses
= DeviceDescription
->Dma32BitAddresses
;
447 AdapterObject
->ChannelNumber
= 0xFF;
448 AdapterObject
->MasterAdapter
= HalpMasterAdapter
;
449 KeInitializeDeviceQueue(&AdapterObject
->ChannelWaitQueue
);
451 return AdapterObject
;
456 * @name HalpDmaInitializeEisaAdapter
458 * Setup DMA modes and extended modes for (E)ISA DMA adapter object.
462 HalpDmaInitializeEisaAdapter(IN PADAPTER_OBJECT AdapterObject
,
463 IN PDEVICE_DESCRIPTION DeviceDescription
)
466 DMA_MODE DmaMode
= {{0 }};
467 DMA_EXTENDED_MODE ExtendedMode
= {{ 0 }};
470 Controller
= (DeviceDescription
->DmaChannel
& 4) ? 2 : 1;
474 AdapterBaseVa
= (PVOID
)FIELD_OFFSET(EISA_CONTROL
, DmaController1
);
478 AdapterBaseVa
= (PVOID
)FIELD_OFFSET(EISA_CONTROL
, DmaController2
);
481 AdapterObject
->AdapterNumber
= Controller
;
482 AdapterObject
->ChannelNumber
= (UCHAR
)(DeviceDescription
->DmaChannel
& 3);
483 AdapterObject
->PagePort
= (PUCHAR
)HalpEisaPortPage
[DeviceDescription
->DmaChannel
];
484 AdapterObject
->Width16Bits
= FALSE
;
485 AdapterObject
->AdapterBaseVa
= AdapterBaseVa
;
489 ExtendedMode
.ChannelNumber
= AdapterObject
->ChannelNumber
;
491 switch (DeviceDescription
->DmaSpeed
)
493 case Compatible
: ExtendedMode
.TimingMode
= COMPATIBLE_TIMING
; break;
494 case TypeA
: ExtendedMode
.TimingMode
= TYPE_A_TIMING
; break;
495 case TypeB
: ExtendedMode
.TimingMode
= TYPE_B_TIMING
; break;
496 case TypeC
: ExtendedMode
.TimingMode
= BURST_TIMING
; break;
501 switch (DeviceDescription
->DmaWidth
)
503 case Width8Bits
: ExtendedMode
.TransferSize
= B_8BITS
; break;
504 case Width16Bits
: ExtendedMode
.TransferSize
= B_16BITS
; break;
505 case Width32Bits
: ExtendedMode
.TransferSize
= B_32BITS
; break;
512 WRITE_PORT_UCHAR((PUCHAR
)FIELD_OFFSET(EISA_CONTROL
, DmaExtendedMode1
),
517 WRITE_PORT_UCHAR((PUCHAR
)FIELD_OFFSET(EISA_CONTROL
, DmaExtendedMode2
),
524 * Validate setup for non-busmaster DMA adapter. Secondary controller
525 * supports only 16-bit transfers and main controller supports only
526 * 8-bit transfers. Anything else is invalid.
528 if (!DeviceDescription
->Master
)
530 if ((Controller
== 2) && (DeviceDescription
->DmaWidth
== Width16Bits
))
532 AdapterObject
->Width16Bits
= TRUE
;
534 else if ((Controller
!= 1) || (DeviceDescription
->DmaWidth
!= Width8Bits
))
541 DmaMode
.Channel
= AdapterObject
->ChannelNumber
;
542 DmaMode
.AutoInitialize
= DeviceDescription
->AutoInitialize
;
545 * Set the DMA request mode.
547 * For (E)ISA bus master devices just unmask (enable) the DMA channel
548 * and set it to cascade mode. Otherwise just select the right one
549 * bases on the passed device description.
551 if (DeviceDescription
->Master
)
553 DmaMode
.RequestMode
= CASCADE_REQUEST_MODE
;
556 /* Set the Request Data */
557 _PRAGMA_WARNING_SUPPRESS(__WARNING_DEREF_NULL_PTR
)
558 WRITE_PORT_UCHAR(&((PDMA1_CONTROL
)AdapterBaseVa
)->Mode
, DmaMode
.Byte
);
560 /* Unmask DMA Channel */
561 WRITE_PORT_UCHAR(&((PDMA1_CONTROL
)AdapterBaseVa
)->SingleMask
,
562 AdapterObject
->ChannelNumber
| DMA_CLEARMASK
);
566 /* Set the Request Data */
567 WRITE_PORT_UCHAR(&((PDMA2_CONTROL
)AdapterBaseVa
)->Mode
, DmaMode
.Byte
);
569 /* Unmask DMA Channel */
570 WRITE_PORT_UCHAR(&((PDMA2_CONTROL
)AdapterBaseVa
)->SingleMask
,
571 AdapterObject
->ChannelNumber
| DMA_CLEARMASK
);
576 if (DeviceDescription
->DemandMode
)
578 DmaMode
.RequestMode
= DEMAND_REQUEST_MODE
;
582 DmaMode
.RequestMode
= SINGLE_REQUEST_MODE
;
586 AdapterObject
->AdapterMode
= DmaMode
;
593 * @name HalGetAdapter
595 * Allocate an adapter object for DMA device.
597 * @param DeviceDescription
598 * Structure describing the attributes of the device.
599 * @param NumberOfMapRegisters
600 * On return filled with the maximum number of map registers the
601 * device driver can allocate for DMA transfer operations.
603 * @return The DMA adapter on success, NULL otherwise.
609 HalGetAdapter(IN PDEVICE_DESCRIPTION DeviceDescription
,
610 OUT PULONG NumberOfMapRegisters
)
612 PADAPTER_OBJECT AdapterObject
= NULL
;
617 /* Validate parameters in device description */
618 if (DeviceDescription
->Version
> DEVICE_DESCRIPTION_VERSION2
) return NULL
;
621 * See if we're going to use ISA/EISA DMA adapter. These adapters are
622 * special since they're reused.
624 * Also note that we check for channel number since there are only 8 DMA
625 * channels on ISA, so any request above this requires new adapter.
627 if (((DeviceDescription
->InterfaceType
== Eisa
) ||
628 (DeviceDescription
->InterfaceType
== Isa
)) || !(DeviceDescription
->Master
))
630 if (((DeviceDescription
->InterfaceType
== Isa
) ||
631 (DeviceDescription
->InterfaceType
== Eisa
)) &&
632 (DeviceDescription
->DmaChannel
>= 8))
647 * Disallow creating adapter for ISA/EISA DMA channel 4 since it's used
648 * for cascading the controllers and it's not available for software use.
650 if ((EisaAdapter
) && (DeviceDescription
->DmaChannel
== 4)) return NULL
;
653 * Calculate the number of map registers.
655 * - For EISA and PCI scatter/gather no map registers are needed.
656 * - For ISA slave scatter/gather one map register is needed.
657 * - For all other cases the number of map registers depends on
658 * DeviceDescription->MaximumLength.
660 MaximumLength
= DeviceDescription
->MaximumLength
& MAXLONG
;
661 if ((DeviceDescription
->ScatterGather
) &&
662 ((DeviceDescription
->InterfaceType
== Eisa
) ||
663 (DeviceDescription
->InterfaceType
== PCIBus
)))
667 else if ((DeviceDescription
->ScatterGather
) && !(DeviceDescription
->Master
))
674 * In the equation below the additional map register added by
675 * the "+1" accounts for the case when a transfer does not start
676 * at a page-aligned address.
678 MapRegisters
= BYTES_TO_PAGES(MaximumLength
) + 1;
679 if (MapRegisters
> 16) MapRegisters
= 16;
683 * Acquire the DMA lock that is used to protect adapter lists and
684 * EISA adapter array.
686 KeWaitForSingleObject(&HalpDmaLock
, Executive
, KernelMode
, FALSE
, NULL
);
689 * Now we must get ahold of the adapter object. For first eight ISA/EISA
690 * channels there are static adapter objects that are reused and updated
691 * on succesive HalGetAdapter calls. In other cases a new adapter object
692 * is always created and it's to the DMA adapter list (HalpDmaAdapterList).
696 AdapterObject
= HalpEisaAdapter
[DeviceDescription
->DmaChannel
];
699 if ((AdapterObject
->NeedsMapRegisters
) &&
700 (MapRegisters
> AdapterObject
->MapRegistersPerChannel
))
702 AdapterObject
->MapRegistersPerChannel
= MapRegisters
;
707 if (AdapterObject
== NULL
)
709 AdapterObject
= HalpDmaAllocateChildAdapter(MapRegisters
, DeviceDescription
);
710 if (AdapterObject
== NULL
)
712 KeSetEvent(&HalpDmaLock
, 0, 0);
718 HalpEisaAdapter
[DeviceDescription
->DmaChannel
] = AdapterObject
;
721 if (MapRegisters
> 0)
723 AdapterObject
->NeedsMapRegisters
= TRUE
;
724 AdapterObject
->MapRegistersPerChannel
= MapRegisters
;
728 AdapterObject
->NeedsMapRegisters
= FALSE
;
729 if (DeviceDescription
->Master
)
731 AdapterObject
->MapRegistersPerChannel
= BYTES_TO_PAGES(MaximumLength
) + 1;
735 AdapterObject
->MapRegistersPerChannel
= 1;
740 if (!EisaAdapter
) InsertTailList(&HalpDmaAdapterList
, &AdapterObject
->AdapterList
);
743 * Release the DMA lock. HalpDmaAdapterList and HalpEisaAdapter will
744 * no longer be touched, so we don't need it.
746 KeSetEvent(&HalpDmaLock
, 0, 0);
749 * Setup the values in the adapter object that are common for all
752 if (DeviceDescription
->Version
>= DEVICE_DESCRIPTION_VERSION1
)
754 AdapterObject
->IgnoreCount
= DeviceDescription
->IgnoreCount
;
758 AdapterObject
->IgnoreCount
= 0;
761 AdapterObject
->Dma32BitAddresses
= DeviceDescription
->Dma32BitAddresses
;
762 AdapterObject
->Dma64BitAddresses
= DeviceDescription
->Dma64BitAddresses
;
763 AdapterObject
->ScatterGather
= DeviceDescription
->ScatterGather
;
764 AdapterObject
->MasterDevice
= DeviceDescription
->Master
;
765 *NumberOfMapRegisters
= AdapterObject
->MapRegistersPerChannel
;
768 * For non-(E)ISA adapters we have already done all the work. On the
769 * other hand for (E)ISA adapters we must still setup the DMA modes
770 * and prepare the controller.
774 if (!HalpDmaInitializeEisaAdapter(AdapterObject
, DeviceDescription
))
776 ObDereferenceObject(AdapterObject
);
781 return AdapterObject
;
785 * @name HalpGetDmaAdapter
787 * Internal routine to allocate PnP DMA adapter object. It's exported through
788 * HalDispatchTable and used by IoGetDmaAdapter.
794 HalpGetDmaAdapter(IN PVOID Context
,
795 IN PDEVICE_DESCRIPTION DeviceDescription
,
796 OUT PULONG NumberOfMapRegisters
)
798 return &HalGetAdapter(DeviceDescription
, NumberOfMapRegisters
)->DmaHeader
;
802 * @name HalPutDmaAdapter
804 * Internal routine to free DMA adapter and resources for reuse. It's exported
805 * using the DMA_OPERATIONS interface by HalGetAdapter.
811 HalPutDmaAdapter(IN PADAPTER_OBJECT AdapterObject
)
813 if (AdapterObject
->ChannelNumber
== 0xFF)
815 KeWaitForSingleObject(&HalpDmaLock
, Executive
, KernelMode
, FALSE
, NULL
);
816 RemoveEntryList(&AdapterObject
->AdapterList
);
817 KeSetEvent(&HalpDmaLock
, 0, 0);
820 ObDereferenceObject(AdapterObject
);
824 * @name HalAllocateCommonBuffer
826 * Allocates memory that is visible to both the processor(s) and the DMA
829 * @param AdapterObject
830 * Adapter object representing the bus master or system dma controller.
832 * Number of bytes to allocate.
833 * @param LogicalAddress
834 * Logical address the driver can use to access the buffer.
835 * @param CacheEnabled
836 * Specifies if the memory can be cached.
838 * @return The base virtual address of the memory allocated or NULL on failure.
841 * On real NT x86 systems the CacheEnabled parameter is ignored, we honour
842 * it. If it proves to cause problems change it.
844 * @see HalFreeCommonBuffer
850 HalAllocateCommonBuffer(IN PADAPTER_OBJECT AdapterObject
,
852 IN PPHYSICAL_ADDRESS LogicalAddress
,
853 IN BOOLEAN CacheEnabled
)
855 PHYSICAL_ADDRESS LowestAcceptableAddress
;
856 PHYSICAL_ADDRESS HighestAcceptableAddress
;
857 PHYSICAL_ADDRESS BoundryAddressMultiple
;
858 PVOID VirtualAddress
;
860 LowestAcceptableAddress
.QuadPart
= 0;
861 HighestAcceptableAddress
= HalpGetAdapterMaximumPhysicalAddress(AdapterObject
);
862 BoundryAddressMultiple
.QuadPart
= 0;
865 * For bus-master DMA devices the buffer mustn't cross 4Gb boundary. For
866 * slave DMA devices the 64Kb boundary mustn't be crossed since the
867 * controller wouldn't be able to handle it.
869 if (AdapterObject
->MasterDevice
)
871 BoundryAddressMultiple
.HighPart
= 1;
875 BoundryAddressMultiple
.LowPart
= 0x10000;
878 VirtualAddress
= MmAllocateContiguousMemorySpecifyCache(Length
,
879 LowestAcceptableAddress
,
880 HighestAcceptableAddress
,
881 BoundryAddressMultiple
,
882 CacheEnabled
? MmCached
:
884 if (VirtualAddress
== NULL
) return NULL
;
886 *LogicalAddress
= MmGetPhysicalAddress(VirtualAddress
);
888 return VirtualAddress
;
892 * @name HalFreeCommonBuffer
894 * Free common buffer allocated with HalAllocateCommonBuffer.
896 * @see HalAllocateCommonBuffer
902 HalFreeCommonBuffer(IN PADAPTER_OBJECT AdapterObject
,
904 IN PHYSICAL_ADDRESS LogicalAddress
,
905 IN PVOID VirtualAddress
,
906 IN BOOLEAN CacheEnabled
)
908 MmFreeContiguousMemorySpecifyCache(VirtualAddress
,
910 CacheEnabled
? MmCached
: MmNonCached
);
913 typedef struct _SCATTER_GATHER_CONTEXT
{
914 PADAPTER_OBJECT AdapterObject
;
918 PDRIVER_LIST_CONTROL AdapterListControlRoutine
;
919 PVOID AdapterListControlContext
, MapRegisterBase
;
920 ULONG MapRegisterCount
;
921 BOOLEAN WriteToDevice
;
922 } SCATTER_GATHER_CONTEXT
, *PSCATTER_GATHER_CONTEXT
;
927 HalpScatterGatherAdapterControl(IN PDEVICE_OBJECT DeviceObject
,
929 IN PVOID MapRegisterBase
,
932 PSCATTER_GATHER_CONTEXT AdapterControlContext
= Context
;
933 PADAPTER_OBJECT AdapterObject
= AdapterControlContext
->AdapterObject
;
934 PSCATTER_GATHER_LIST ScatterGatherList
;
935 SCATTER_GATHER_ELEMENT TempElements
[MAX_SG_ELEMENTS
];
936 ULONG ElementCount
= 0, RemainingLength
= AdapterControlContext
->Length
;
937 PUCHAR CurrentVa
= AdapterControlContext
->CurrentVa
;
939 /* Store the map register base for later in HalPutScatterGatherList */
940 AdapterControlContext
->MapRegisterBase
= MapRegisterBase
;
942 while (RemainingLength
> 0 && ElementCount
< MAX_SG_ELEMENTS
)
944 TempElements
[ElementCount
].Length
= RemainingLength
;
945 TempElements
[ElementCount
].Reserved
= 0;
946 TempElements
[ElementCount
].Address
= IoMapTransfer(AdapterObject
,
947 AdapterControlContext
->Mdl
,
949 CurrentVa
+ (AdapterControlContext
->Length
- RemainingLength
),
950 &TempElements
[ElementCount
].Length
,
951 AdapterControlContext
->WriteToDevice
);
952 if (TempElements
[ElementCount
].Length
== 0)
955 DPRINT("Allocated one S/G element: 0x%I64u with length: 0x%x\n",
956 TempElements
[ElementCount
].Address
.QuadPart
,
957 TempElements
[ElementCount
].Length
);
959 ASSERT(TempElements
[ElementCount
].Length
<= RemainingLength
);
960 RemainingLength
-= TempElements
[ElementCount
].Length
;
964 if (RemainingLength
> 0)
966 DPRINT1("Scatter/gather list construction failed!\n");
967 return DeallocateObject
;
970 ScatterGatherList
= ExAllocatePoolWithTag(NonPagedPool
,
971 sizeof(SCATTER_GATHER_LIST
) + sizeof(SCATTER_GATHER_ELEMENT
) * ElementCount
,
973 ASSERT(ScatterGatherList
);
975 ScatterGatherList
->NumberOfElements
= ElementCount
;
976 ScatterGatherList
->Reserved
= (ULONG_PTR
)AdapterControlContext
;
977 RtlCopyMemory(ScatterGatherList
->Elements
,
979 sizeof(SCATTER_GATHER_ELEMENT
) * ElementCount
);
981 DPRINT("Initiating S/G DMA with %d element(s)\n", ElementCount
);
983 AdapterControlContext
->AdapterListControlRoutine(DeviceObject
,
986 AdapterControlContext
->AdapterListControlContext
);
988 return DeallocateObjectKeepRegisters
;
992 * @name HalGetScatterGatherList
994 * Creates a scatter-gather list to be using in scatter/gather DMA
996 * @param AdapterObject
997 * Adapter object representing the bus master or system dma controller.
998 * @param DeviceObject
999 * The device target for DMA.
1001 * The MDL that describes the buffer to be mapped.
1003 * The current VA in the buffer to be mapped for transfer.
1005 * Specifies the length of data in bytes to be mapped.
1006 * @param ExecutionRoutine
1007 * A caller supplied AdapterListControl routine to be called when DMA is available.
1009 * Context passed to the AdapterListControl routine.
1010 * @param WriteToDevice
1011 * Indicates direction of DMA operation.
1013 * @return The status of the operation.
1015 * @see HalPutScatterGatherList
1021 HalGetScatterGatherList(IN PADAPTER_OBJECT AdapterObject
,
1022 IN PDEVICE_OBJECT DeviceObject
,
1026 IN PDRIVER_LIST_CONTROL ExecutionRoutine
,
1028 IN BOOLEAN WriteToDevice
)
1030 PSCATTER_GATHER_CONTEXT AdapterControlContext
;
1032 AdapterControlContext
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(SCATTER_GATHER_CONTEXT
), TAG_DMA
);
1033 if (!AdapterControlContext
) return STATUS_INSUFFICIENT_RESOURCES
;
1035 AdapterControlContext
->AdapterObject
= AdapterObject
;
1036 AdapterControlContext
->Mdl
= Mdl
;
1037 AdapterControlContext
->CurrentVa
= CurrentVa
;
1038 AdapterControlContext
->Length
= Length
;
1039 AdapterControlContext
->MapRegisterCount
= PAGE_ROUND_UP(Length
) >> PAGE_SHIFT
;
1040 AdapterControlContext
->AdapterListControlRoutine
= ExecutionRoutine
;
1041 AdapterControlContext
->AdapterListControlContext
= Context
;
1042 AdapterControlContext
->WriteToDevice
= WriteToDevice
;
1044 return IoAllocateAdapterChannel(AdapterObject
,
1046 AdapterControlContext
->MapRegisterCount
,
1047 HalpScatterGatherAdapterControl
,
1048 AdapterControlContext
);
1052 * @name HalPutScatterGatherList
1054 * Frees a scatter-gather list allocated from HalGetScatterGatherList
1056 * @param AdapterObject
1057 * Adapter object representing the bus master or system dma controller.
1058 * @param ScatterGather
1059 * The scatter/gather list to be freed.
1060 * @param WriteToDevice
1061 * Indicates direction of DMA operation.
1065 * @see HalGetScatterGatherList
1071 HalPutScatterGatherList(IN PADAPTER_OBJECT AdapterObject
,
1072 IN PSCATTER_GATHER_LIST ScatterGather
,
1073 IN BOOLEAN WriteToDevice
)
1075 PSCATTER_GATHER_CONTEXT AdapterControlContext
= (PSCATTER_GATHER_CONTEXT
)ScatterGather
->Reserved
;
1078 for (i
= 0; i
< ScatterGather
->NumberOfElements
; i
++)
1080 IoFlushAdapterBuffers(AdapterObject
,
1081 AdapterControlContext
->Mdl
,
1082 AdapterControlContext
->MapRegisterBase
,
1083 AdapterControlContext
->CurrentVa
,
1084 ScatterGather
->Elements
[i
].Length
,
1085 AdapterControlContext
->WriteToDevice
);
1086 AdapterControlContext
->CurrentVa
+= ScatterGather
->Elements
[i
].Length
;
1089 IoFreeMapRegisters(AdapterObject
,
1090 AdapterControlContext
->MapRegisterBase
,
1091 AdapterControlContext
->MapRegisterCount
);
1093 DPRINT("S/G DMA has finished!\n");
1095 ExFreePoolWithTag(AdapterControlContext
, TAG_DMA
);
1096 ExFreePoolWithTag(ScatterGather
, TAG_DMA
);
1101 * @name HalpDmaGetDmaAlignment
1103 * Internal routine to return the DMA alignment requirement. It's exported
1104 * using the DMA_OPERATIONS interface by HalGetAdapter.
1106 * @see HalGetAdapter
1110 HalpDmaGetDmaAlignment(IN PADAPTER_OBJECT AdapterObject
)
1116 * @name HalReadDmaCounter
1118 * Read DMA operation progress counter.
1124 HalReadDmaCounter(IN PADAPTER_OBJECT AdapterObject
)
1127 ULONG Count
, OldCount
;
1129 ASSERT(!AdapterObject
->MasterDevice
);
1132 * Acquire the master adapter lock since we're going to mess with the
1133 * system DMA controller registers and we really don't want anyone
1134 * to do the same at the same time.
1136 KeAcquireSpinLock(&AdapterObject
->MasterAdapter
->SpinLock
, &OldIrql
);
1138 /* Send the request to the specific controller. */
1139 if (AdapterObject
->AdapterNumber
== 1)
1141 PDMA1_CONTROL DmaControl1
= AdapterObject
->AdapterBaseVa
;
1149 WRITE_PORT_UCHAR(&DmaControl1
->ClearBytePointer
, 0);
1152 Count
= READ_PORT_UCHAR(&DmaControl1
->DmaAddressCount
1153 [AdapterObject
->ChannelNumber
].DmaBaseCount
);
1154 Count
|= READ_PORT_UCHAR(&DmaControl1
->DmaAddressCount
1155 [AdapterObject
->ChannelNumber
].DmaBaseCount
) << 8;
1156 } while (0xffff00 & (OldCount
^ Count
));
1160 PDMA2_CONTROL DmaControl2
= AdapterObject
->AdapterBaseVa
;
1168 WRITE_PORT_UCHAR(&DmaControl2
->ClearBytePointer
, 0);
1171 Count
= READ_PORT_UCHAR(&DmaControl2
->DmaAddressCount
1172 [AdapterObject
->ChannelNumber
].DmaBaseCount
);
1173 Count
|= READ_PORT_UCHAR(&DmaControl2
->DmaAddressCount
1174 [AdapterObject
->ChannelNumber
].DmaBaseCount
) << 8;
1175 } while (0xffff00 & (OldCount
^ Count
));
1178 KeReleaseSpinLock(&AdapterObject
->MasterAdapter
->SpinLock
, OldIrql
);
1182 if (AdapterObject
->Width16Bits
) Count
*= 2;
1189 * @name HalpGrowMapBufferWorker
1191 * Helper routine of HalAllocateAdapterChannel for allocating map registers
1192 * at PASSIVE_LEVEL in work item.
1196 HalpGrowMapBufferWorker(IN PVOID DeferredContext
)
1198 PGROW_WORK_ITEM WorkItem
= (PGROW_WORK_ITEM
)DeferredContext
;
1203 * Try to allocate new map registers for the adapter.
1205 * NOTE: The NT implementation actually tries to allocate more map
1206 * registers than needed as an optimization.
1208 KeWaitForSingleObject(&HalpDmaLock
, Executive
, KernelMode
, FALSE
, NULL
);
1209 Succeeded
= HalpGrowMapBuffers(WorkItem
->AdapterObject
->MasterAdapter
,
1210 WorkItem
->NumberOfMapRegisters
<< PAGE_SHIFT
);
1211 KeSetEvent(&HalpDmaLock
, 0, 0);
1216 * Flush the adapter queue now that new map registers are ready. The
1217 * easiest way to do that is to call IoFreeMapRegisters to not free
1218 * any registers. Note that we use the magic (PVOID)2 map register
1219 * base to bypass the parameter checking.
1221 OldIrql
= KfRaiseIrql(DISPATCH_LEVEL
);
1222 IoFreeMapRegisters(WorkItem
->AdapterObject
, (PVOID
)2, 0);
1223 KfLowerIrql(OldIrql
);
1226 ExFreePool(WorkItem
);
1230 * @name HalAllocateAdapterChannel
1232 * Setup map registers for an adapter object.
1234 * @param AdapterObject
1235 * Pointer to an ADAPTER_OBJECT to set up.
1236 * @param WaitContextBlock
1237 * Context block to be used with ExecutionRoutine.
1238 * @param NumberOfMapRegisters
1239 * Number of map registers requested.
1240 * @param ExecutionRoutine
1241 * Callback to call when map registers are allocated.
1244 * If not enough map registers can be allocated then
1245 * STATUS_INSUFFICIENT_RESOURCES is returned. If the function
1246 * succeeds or the callback is queued for later delivering then
1247 * STATUS_SUCCESS is returned.
1249 * @see IoFreeAdapterChannel
1255 HalAllocateAdapterChannel(IN PADAPTER_OBJECT AdapterObject
,
1256 IN PWAIT_CONTEXT_BLOCK WaitContextBlock
,
1257 IN ULONG NumberOfMapRegisters
,
1258 IN PDRIVER_CONTROL ExecutionRoutine
)
1260 PADAPTER_OBJECT MasterAdapter
;
1261 PGROW_WORK_ITEM WorkItem
;
1262 ULONG Index
= MAXULONG
;
1266 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
1268 /* Set up the wait context block in case we can't run right away. */
1269 WaitContextBlock
->DeviceRoutine
= ExecutionRoutine
;
1270 WaitContextBlock
->NumberOfMapRegisters
= NumberOfMapRegisters
;
1272 /* Returns true if queued, else returns false and sets the queue to busy */
1273 if (KeInsertDeviceQueue(&AdapterObject
->ChannelWaitQueue
,
1274 &WaitContextBlock
->WaitQueueEntry
))
1276 return STATUS_SUCCESS
;
1279 MasterAdapter
= AdapterObject
->MasterAdapter
;
1281 AdapterObject
->NumberOfMapRegisters
= NumberOfMapRegisters
;
1282 AdapterObject
->CurrentWcb
= WaitContextBlock
;
1284 if ((NumberOfMapRegisters
) && (AdapterObject
->NeedsMapRegisters
))
1286 if (NumberOfMapRegisters
> AdapterObject
->MapRegistersPerChannel
)
1288 AdapterObject
->NumberOfMapRegisters
= 0;
1289 IoFreeAdapterChannel(AdapterObject
);
1290 return STATUS_INSUFFICIENT_RESOURCES
;
1294 * Get the map registers. This is partly complicated by the fact
1295 * that new map registers can only be allocated at PASSIVE_LEVEL
1296 * and we're currently at DISPATCH_LEVEL. The following code has
1299 * - If there is no adapter queued for map register allocation,
1300 * try to see if enough contiguous map registers are present.
1301 * In case they're we can just get them and proceed further.
1303 * - If some adapter is already present in the queue we must
1304 * respect the order of adapters asking for map registers and
1305 * so the fast case described above can't take place.
1306 * This case is also entered if not enough coniguous map
1307 * registers are present.
1309 * A work queue item is allocated and queued, the adapter is
1310 * also queued into the master adapter queue. The worker
1311 * routine does the job of allocating the map registers at
1312 * PASSIVE_LEVEL and calling the ExecutionRoutine.
1315 KeAcquireSpinLock(&MasterAdapter
->SpinLock
, &OldIrql
);
1317 if (IsListEmpty(&MasterAdapter
->AdapterQueue
))
1319 Index
= RtlFindClearBitsAndSet(MasterAdapter
->MapRegisters
, NumberOfMapRegisters
, 0);
1320 if (Index
!= MAXULONG
)
1322 AdapterObject
->MapRegisterBase
= MasterAdapter
->MapRegisterBase
+ Index
;
1323 if (!AdapterObject
->ScatterGather
)
1325 AdapterObject
->MapRegisterBase
= (PROS_MAP_REGISTER_ENTRY
)((ULONG_PTR
)AdapterObject
->MapRegisterBase
| MAP_BASE_SW_SG
);
1330 if (Index
== MAXULONG
)
1332 InsertTailList(&MasterAdapter
->AdapterQueue
, &AdapterObject
->AdapterQueue
);
1334 WorkItem
= ExAllocatePoolWithTag(NonPagedPool
,
1335 sizeof(GROW_WORK_ITEM
),
1339 ExInitializeWorkItem(&WorkItem
->WorkQueueItem
, HalpGrowMapBufferWorker
, WorkItem
);
1340 WorkItem
->AdapterObject
= AdapterObject
;
1341 WorkItem
->NumberOfMapRegisters
= NumberOfMapRegisters
;
1343 ExQueueWorkItem(&WorkItem
->WorkQueueItem
, DelayedWorkQueue
);
1346 KeReleaseSpinLock(&MasterAdapter
->SpinLock
, OldIrql
);
1348 return STATUS_SUCCESS
;
1351 KeReleaseSpinLock(&MasterAdapter
->SpinLock
, OldIrql
);
1355 AdapterObject
->MapRegisterBase
= NULL
;
1356 AdapterObject
->NumberOfMapRegisters
= 0;
1359 AdapterObject
->CurrentWcb
= WaitContextBlock
;
1361 Result
= ExecutionRoutine(WaitContextBlock
->DeviceObject
,
1362 WaitContextBlock
->CurrentIrp
,
1363 AdapterObject
->MapRegisterBase
,
1364 WaitContextBlock
->DeviceContext
);
1367 * Possible return values:
1370 * Don't free any resources, the ADAPTER_OBJECT is still in use and
1371 * the caller will call IoFreeAdapterChannel later.
1373 * - DeallocateObject
1374 * Deallocate the map registers and release the ADAPTER_OBJECT, so
1375 * someone else can use it.
1377 * - DeallocateObjectKeepRegisters
1378 * Release the ADAPTER_OBJECT, but hang on to the map registers. The
1379 * client will later call IoFreeMapRegisters.
1382 * IoFreeAdapterChannel runs the queue, so it must be called unless
1383 * the adapter object is not to be freed.
1385 if (Result
== DeallocateObject
)
1387 IoFreeAdapterChannel(AdapterObject
);
1389 else if (Result
== DeallocateObjectKeepRegisters
)
1391 AdapterObject
->NumberOfMapRegisters
= 0;
1392 IoFreeAdapterChannel(AdapterObject
);
1395 return STATUS_SUCCESS
;
1399 * @name IoFreeAdapterChannel
1401 * Free DMA resources allocated by IoAllocateAdapterChannel.
1403 * @param AdapterObject
1404 * Adapter object with resources to free.
1407 * This function releases map registers registers assigned to the DMA
1408 * adapter. After releasing the adapter, it checks the adapter's queue
1409 * and runs each queued device object in series until the queue is
1410 * empty. This is the only way the device queue is emptied.
1412 * @see IoAllocateAdapterChannel
1418 IoFreeAdapterChannel(IN PADAPTER_OBJECT AdapterObject
)
1420 PADAPTER_OBJECT MasterAdapter
;
1421 PKDEVICE_QUEUE_ENTRY DeviceQueueEntry
;
1422 PWAIT_CONTEXT_BLOCK WaitContextBlock
;
1423 ULONG Index
= MAXULONG
;
1427 MasterAdapter
= AdapterObject
->MasterAdapter
;
1432 * To keep map registers, call here with AdapterObject->
1433 * NumberOfMapRegisters set to zero. This trick is used in
1434 * HalAllocateAdapterChannel for example.
1436 if (AdapterObject
->NumberOfMapRegisters
)
1438 IoFreeMapRegisters(AdapterObject
,
1439 AdapterObject
->MapRegisterBase
,
1440 AdapterObject
->NumberOfMapRegisters
);
1443 DeviceQueueEntry
= KeRemoveDeviceQueue(&AdapterObject
->ChannelWaitQueue
);
1444 if (!DeviceQueueEntry
) break;
1446 WaitContextBlock
= CONTAINING_RECORD(DeviceQueueEntry
,
1450 AdapterObject
->CurrentWcb
= WaitContextBlock
;
1451 AdapterObject
->NumberOfMapRegisters
= WaitContextBlock
->NumberOfMapRegisters
;
1453 if ((WaitContextBlock
->NumberOfMapRegisters
) && (AdapterObject
->MasterAdapter
))
1455 KeAcquireSpinLock(&MasterAdapter
->SpinLock
, &OldIrql
);
1457 if (IsListEmpty(&MasterAdapter
->AdapterQueue
))
1459 Index
= RtlFindClearBitsAndSet(MasterAdapter
->MapRegisters
,
1460 WaitContextBlock
->NumberOfMapRegisters
,
1462 if (Index
!= MAXULONG
)
1464 AdapterObject
->MapRegisterBase
= MasterAdapter
->MapRegisterBase
+ Index
;
1465 if (!AdapterObject
->ScatterGather
)
1467 AdapterObject
->MapRegisterBase
=(PROS_MAP_REGISTER_ENTRY
)((ULONG_PTR
)AdapterObject
->MapRegisterBase
| MAP_BASE_SW_SG
);
1472 if (Index
== MAXULONG
)
1474 InsertTailList(&MasterAdapter
->AdapterQueue
, &AdapterObject
->AdapterQueue
);
1475 KeReleaseSpinLock(&MasterAdapter
->SpinLock
, OldIrql
);
1479 KeReleaseSpinLock(&MasterAdapter
->SpinLock
, OldIrql
);
1483 AdapterObject
->MapRegisterBase
= NULL
;
1484 AdapterObject
->NumberOfMapRegisters
= 0;
1487 /* Call the adapter control routine. */
1488 Result
= ((PDRIVER_CONTROL
)WaitContextBlock
->DeviceRoutine
)(WaitContextBlock
->DeviceObject
,
1489 WaitContextBlock
->CurrentIrp
,
1490 AdapterObject
->MapRegisterBase
,
1491 WaitContextBlock
->DeviceContext
);
1496 * We're done until the caller manually calls IoFreeAdapterChannel
1497 * or IoFreeMapRegisters.
1501 case DeallocateObjectKeepRegisters
:
1503 * Hide the map registers so they aren't deallocated next time
1506 AdapterObject
->NumberOfMapRegisters
= 0;
1516 * @name IoFreeMapRegisters
1518 * Free map registers reserved by the system for a DMA.
1520 * @param AdapterObject
1521 * DMA adapter to free map registers on.
1522 * @param MapRegisterBase
1523 * Handle to map registers to free.
1524 * @param NumberOfRegisters
1525 * Number of map registers to be freed.
1531 IoFreeMapRegisters(IN PADAPTER_OBJECT AdapterObject
,
1532 IN PVOID MapRegisterBase
,
1533 IN ULONG NumberOfMapRegisters
)
1535 PADAPTER_OBJECT MasterAdapter
= AdapterObject
->MasterAdapter
;
1536 PLIST_ENTRY ListEntry
;
1541 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
1543 if (!(MasterAdapter
) || !(MapRegisterBase
)) return;
1545 KeAcquireSpinLock(&MasterAdapter
->SpinLock
, &OldIrql
);
1547 if (NumberOfMapRegisters
!= 0)
1549 PROS_MAP_REGISTER_ENTRY RealMapRegisterBase
;
1551 RealMapRegisterBase
= (PROS_MAP_REGISTER_ENTRY
)((ULONG_PTR
)MapRegisterBase
& ~MAP_BASE_SW_SG
);
1552 RtlClearBits(MasterAdapter
->MapRegisters
,
1553 (ULONG
)(RealMapRegisterBase
- MasterAdapter
->MapRegisterBase
),
1554 NumberOfMapRegisters
);
1558 * Now that we freed few map registers it's time to look at the master
1559 * adapter queue and see if there is someone waiting for map registers.
1561 while (!IsListEmpty(&MasterAdapter
->AdapterQueue
))
1563 ListEntry
= RemoveHeadList(&MasterAdapter
->AdapterQueue
);
1564 AdapterObject
= CONTAINING_RECORD(ListEntry
, struct _ADAPTER_OBJECT
, AdapterQueue
);
1566 Index
= RtlFindClearBitsAndSet(MasterAdapter
->MapRegisters
,
1567 AdapterObject
->NumberOfMapRegisters
,
1569 if (Index
== MAXULONG
)
1571 InsertHeadList(&MasterAdapter
->AdapterQueue
, ListEntry
);
1575 KeReleaseSpinLock(&MasterAdapter
->SpinLock
, OldIrql
);
1577 AdapterObject
->MapRegisterBase
= MasterAdapter
->MapRegisterBase
+ Index
;
1578 if (!AdapterObject
->ScatterGather
)
1580 AdapterObject
->MapRegisterBase
=
1581 (PROS_MAP_REGISTER_ENTRY
)((ULONG_PTR
)AdapterObject
->MapRegisterBase
| MAP_BASE_SW_SG
);
1584 Result
= ((PDRIVER_CONTROL
)AdapterObject
->CurrentWcb
->DeviceRoutine
)(AdapterObject
->CurrentWcb
->DeviceObject
,
1585 AdapterObject
->CurrentWcb
->CurrentIrp
,
1586 AdapterObject
->MapRegisterBase
,
1587 AdapterObject
->CurrentWcb
->DeviceContext
);
1590 case DeallocateObjectKeepRegisters
:
1591 AdapterObject
->NumberOfMapRegisters
= 0;
1594 case DeallocateObject
:
1595 if (AdapterObject
->NumberOfMapRegisters
)
1597 KeAcquireSpinLock(&MasterAdapter
->SpinLock
, &OldIrql
);
1598 RtlClearBits(MasterAdapter
->MapRegisters
,
1599 (ULONG
)(AdapterObject
->MapRegisterBase
-
1600 MasterAdapter
->MapRegisterBase
),
1601 AdapterObject
->NumberOfMapRegisters
);
1602 KeReleaseSpinLock(&MasterAdapter
->SpinLock
, OldIrql
);
1605 IoFreeAdapterChannel(AdapterObject
);
1612 KeAcquireSpinLock(&MasterAdapter
->SpinLock
, &OldIrql
);
1615 KeReleaseSpinLock(&MasterAdapter
->SpinLock
, OldIrql
);
1619 * @name HalpCopyBufferMap
1621 * Helper function for copying data from/to map register buffers.
1623 * @see IoFlushAdapterBuffers, IoMapTransfer
1627 HalpCopyBufferMap(IN PMDL Mdl
,
1628 IN PROS_MAP_REGISTER_ENTRY MapRegisterBase
,
1631 IN BOOLEAN WriteToDevice
)
1633 ULONG CurrentLength
;
1634 ULONG_PTR CurrentAddress
;
1636 PVOID VirtualAddress
;
1638 VirtualAddress
= MmGetSystemAddressForMdlSafe(Mdl
, HighPagePriority
);
1639 if (!VirtualAddress
)
1642 * NOTE: On real NT a mechanism with reserved pages is implemented
1643 * to handle this case in a slow, but graceful non-fatal way.
1645 KeBugCheckEx(HAL_MEMORY_ALLOCATION
, PAGE_SIZE
, 0, (ULONG_PTR
)__FILE__
, 0);
1648 CurrentAddress
= (ULONG_PTR
)VirtualAddress
+
1649 (ULONG_PTR
)CurrentVa
-
1650 (ULONG_PTR
)MmGetMdlVirtualAddress(Mdl
);
1654 ByteOffset
= BYTE_OFFSET(CurrentAddress
);
1655 CurrentLength
= PAGE_SIZE
- ByteOffset
;
1656 if (CurrentLength
> Length
) CurrentLength
= Length
;
1660 RtlCopyMemory((PVOID
)((ULONG_PTR
)MapRegisterBase
->VirtualAddress
+ ByteOffset
),
1661 (PVOID
)CurrentAddress
,
1666 RtlCopyMemory((PVOID
)CurrentAddress
,
1667 (PVOID
)((ULONG_PTR
)MapRegisterBase
->VirtualAddress
+ ByteOffset
),
1671 Length
-= CurrentLength
;
1672 CurrentAddress
+= CurrentLength
;
1678 * @name IoFlushAdapterBuffers
1680 * Flush any data remaining in the DMA controller's memory into the host
1683 * @param AdapterObject
1684 * The adapter object to flush.
1686 * Original MDL to flush data into.
1687 * @param MapRegisterBase
1688 * Map register base that was just used by IoMapTransfer, etc.
1690 * Offset into Mdl to be flushed into, same as was passed to
1693 * Length of the buffer to be flushed into.
1694 * @param WriteToDevice
1695 * TRUE if it's a write, FALSE if it's a read.
1697 * @return TRUE in all cases.
1700 * This copies data from the map register-backed buffer to the user's
1701 * target buffer. Data are not in the user buffer until this function
1703 * For slave DMA transfers the controller channel is masked effectively
1704 * stopping the current transfer.
1710 IoFlushAdapterBuffers(IN PADAPTER_OBJECT AdapterObject
,
1712 IN PVOID MapRegisterBase
,
1715 IN BOOLEAN WriteToDevice
)
1717 BOOLEAN SlaveDma
= FALSE
;
1718 PROS_MAP_REGISTER_ENTRY RealMapRegisterBase
;
1719 PHYSICAL_ADDRESS HighestAcceptableAddress
;
1720 PHYSICAL_ADDRESS PhysicalAddress
;
1721 PPFN_NUMBER MdlPagesPtr
;
1724 ASSERT_IRQL_LESS_OR_EQUAL(DISPATCH_LEVEL
);
1725 ASSERT(AdapterObject
);
1727 if (!AdapterObject
->MasterDevice
)
1729 /* Mask out (disable) the DMA channel. */
1730 if (AdapterObject
->AdapterNumber
== 1)
1732 PDMA1_CONTROL DmaControl1
= AdapterObject
->AdapterBaseVa
;
1733 WRITE_PORT_UCHAR(&DmaControl1
->SingleMask
,
1734 AdapterObject
->ChannelNumber
| DMA_SETMASK
);
1738 PDMA2_CONTROL DmaControl2
= AdapterObject
->AdapterBaseVa
;
1739 WRITE_PORT_UCHAR(&DmaControl2
->SingleMask
,
1740 AdapterObject
->ChannelNumber
| DMA_SETMASK
);
1745 /* This can happen if the device supports hardware scatter/gather. */
1746 if (MapRegisterBase
== NULL
) return TRUE
;
1748 RealMapRegisterBase
= (PROS_MAP_REGISTER_ENTRY
)((ULONG_PTR
)MapRegisterBase
& ~MAP_BASE_SW_SG
);
1752 if ((ULONG_PTR
)MapRegisterBase
& MAP_BASE_SW_SG
)
1754 if (RealMapRegisterBase
->Counter
!= MAXULONG
)
1756 if ((SlaveDma
) && !(AdapterObject
->IgnoreCount
))
1758 Length
-= HalReadDmaCounter(AdapterObject
);
1761 HalpCopyBufferMap(Mdl
,
1762 RealMapRegisterBase
,
1769 MdlPagesPtr
= MmGetMdlPfnArray(Mdl
);
1770 MdlPagesPtr
+= ((ULONG_PTR
)CurrentVa
- (ULONG_PTR
)Mdl
->StartVa
) >> PAGE_SHIFT
;
1772 PhysicalAddress
.QuadPart
= *MdlPagesPtr
<< PAGE_SHIFT
;
1773 PhysicalAddress
.QuadPart
+= BYTE_OFFSET(CurrentVa
);
1775 HighestAcceptableAddress
= HalpGetAdapterMaximumPhysicalAddress(AdapterObject
);
1776 if ((PhysicalAddress
.QuadPart
+ Length
) > HighestAcceptableAddress
.QuadPart
)
1778 HalpCopyBufferMap(Mdl
,
1779 RealMapRegisterBase
,
1787 RealMapRegisterBase
->Counter
= 0;
1793 * @name IoMapTransfer
1795 * Map a DMA for transfer and do the DMA if it's a slave.
1797 * @param AdapterObject
1798 * Adapter object to do the DMA on. Bus-master may pass NULL.
1800 * Locked-down user buffer to DMA in to or out of.
1801 * @param MapRegisterBase
1802 * Handle to map registers to use for this dma.
1804 * Index into Mdl to transfer into/out of.
1806 * Length of transfer. Number of bytes actually transferred on
1808 * @param WriteToDevice
1809 * TRUE if it's an output DMA, FALSE otherwise.
1812 * A logical address that can be used to program a DMA controller, it's
1813 * not meaningful for slave DMA device.
1816 * This function does a copyover to contiguous memory <16MB represented
1817 * by the map registers if needed. If the buffer described by MDL can be
1818 * used as is no copyover is done.
1819 * If it's a slave transfer, this function actually performs it.
1825 IoMapTransfer(IN PADAPTER_OBJECT AdapterObject
,
1827 IN PVOID MapRegisterBase
,
1829 IN OUT PULONG Length
,
1830 IN BOOLEAN WriteToDevice
)
1832 PPFN_NUMBER MdlPagesPtr
;
1833 PFN_NUMBER MdlPage1
, MdlPage2
;
1835 ULONG TransferOffset
;
1836 ULONG TransferLength
;
1837 BOOLEAN UseMapRegisters
;
1838 PROS_MAP_REGISTER_ENTRY RealMapRegisterBase
;
1839 PHYSICAL_ADDRESS PhysicalAddress
;
1840 PHYSICAL_ADDRESS HighestAcceptableAddress
;
1842 DMA_MODE AdapterMode
;
1846 * Precalculate some values that are used in all cases.
1848 * ByteOffset is offset inside the page at which the transfer starts.
1849 * MdlPagesPtr is pointer inside the MDL page chain at the page where the
1851 * PhysicalAddress is physical address corresponding to the transfer
1852 * start page and offset.
1853 * TransferLength is the initial length of the transfer, which is reminder
1854 * of the first page. The actual value is calculated below.
1856 * Note that all the variables can change during the processing which
1857 * takes place below. These are just initial values.
1859 ByteOffset
= BYTE_OFFSET(CurrentVa
);
1861 MdlPagesPtr
= MmGetMdlPfnArray(Mdl
);
1862 MdlPagesPtr
+= ((ULONG_PTR
)CurrentVa
- (ULONG_PTR
)Mdl
->StartVa
) >> PAGE_SHIFT
;
1864 PhysicalAddress
.QuadPart
= *MdlPagesPtr
<< PAGE_SHIFT
;
1865 PhysicalAddress
.QuadPart
+= ByteOffset
;
1867 TransferLength
= PAGE_SIZE
- ByteOffset
;
1870 * Special case for bus master adapters with S/G support. We can directly
1871 * use the buffer specified by the MDL, so not much work has to be done.
1873 * Just return the passed VA's corresponding physical address and update
1874 * length to the number of physically contiguous bytes found. Also
1875 * pages crossing the 4Gb boundary aren't considered physically contiguous.
1877 if (MapRegisterBase
== NULL
)
1879 while (TransferLength
< *Length
)
1881 MdlPage1
= *MdlPagesPtr
;
1882 MdlPage2
= *(MdlPagesPtr
+ 1);
1883 if (MdlPage1
+ 1 != MdlPage2
) break;
1884 if ((MdlPage1
^ MdlPage2
) & ~0xFFFFF) break;
1885 TransferLength
+= PAGE_SIZE
;
1889 if (TransferLength
< *Length
) *Length
= TransferLength
;
1891 return PhysicalAddress
;
1895 * The code below applies to slave DMA adapters and bus master adapters
1896 * without hardward S/G support.
1898 RealMapRegisterBase
= (PROS_MAP_REGISTER_ENTRY
)((ULONG_PTR
)MapRegisterBase
& ~MAP_BASE_SW_SG
);
1901 * Try to calculate the size of the transfer. We can only transfer
1902 * pages that are physically contiguous and that don't cross the
1903 * 64Kb boundary (this limitation applies only for ISA controllers).
1905 while (TransferLength
< *Length
)
1907 MdlPage1
= *MdlPagesPtr
;
1908 MdlPage2
= *(MdlPagesPtr
+ 1);
1909 if (MdlPage1
+ 1 != MdlPage2
) break;
1910 if (!HalpEisaDma
&& ((MdlPage1
^ MdlPage2
) & ~0xF)) break;
1911 TransferLength
+= PAGE_SIZE
;
1915 if (TransferLength
> *Length
) TransferLength
= *Length
;
1918 * If we're about to simulate software S/G and not all the pages are
1919 * physically contiguous then we must use the map registers to store
1920 * the data and allow the whole transfer to proceed at once.
1922 if (((ULONG_PTR
)MapRegisterBase
& MAP_BASE_SW_SG
) && (TransferLength
< *Length
))
1924 UseMapRegisters
= TRUE
;
1925 PhysicalAddress
= RealMapRegisterBase
->PhysicalAddress
;
1926 PhysicalAddress
.QuadPart
+= ByteOffset
;
1927 TransferLength
= *Length
;
1928 RealMapRegisterBase
->Counter
= MAXULONG
;
1934 * This is ordinary DMA transfer, so just update the progress
1935 * counters. These are used by IoFlushAdapterBuffers to track
1936 * the transfer progress.
1938 UseMapRegisters
= FALSE
;
1939 Counter
= RealMapRegisterBase
->Counter
;
1940 RealMapRegisterBase
->Counter
+= BYTES_TO_PAGES(ByteOffset
+ TransferLength
);
1943 * Check if the buffer doesn't exceed the highest physical address
1944 * limit of the device. In that case we must use the map registers to
1947 HighestAcceptableAddress
= HalpGetAdapterMaximumPhysicalAddress(AdapterObject
);
1948 if ((PhysicalAddress
.QuadPart
+ TransferLength
) > HighestAcceptableAddress
.QuadPart
)
1950 UseMapRegisters
= TRUE
;
1951 PhysicalAddress
= RealMapRegisterBase
[Counter
].PhysicalAddress
;
1952 PhysicalAddress
.QuadPart
+= ByteOffset
;
1953 if ((ULONG_PTR
)MapRegisterBase
& MAP_BASE_SW_SG
)
1955 RealMapRegisterBase
->Counter
= MAXULONG
;
1962 * If we decided to use the map registers (see above) and we're about
1963 * to transfer data to the device then copy the buffers into the map
1966 if ((UseMapRegisters
) && (WriteToDevice
))
1968 HalpCopyBufferMap(Mdl
,
1969 RealMapRegisterBase
+ Counter
,
1976 * Return the length of transfer that actually takes place.
1978 *Length
= TransferLength
;
1981 * If we're doing slave (system) DMA then program the (E)ISA controller
1982 * to actually start the transfer.
1984 if ((AdapterObject
) && !(AdapterObject
->MasterDevice
))
1986 AdapterMode
= AdapterObject
->AdapterMode
;
1990 AdapterMode
.TransferType
= WRITE_TRANSFER
;
1994 AdapterMode
.TransferType
= READ_TRANSFER
;
1995 if (AdapterObject
->IgnoreCount
)
1997 RtlZeroMemory((PUCHAR
)RealMapRegisterBase
[Counter
].VirtualAddress
+ ByteOffset
,
2002 TransferOffset
= PhysicalAddress
.LowPart
& 0xFFFF;
2003 if (AdapterObject
->Width16Bits
)
2005 TransferLength
>>= 1;
2006 TransferOffset
>>= 1;
2009 KeAcquireSpinLock(&AdapterObject
->MasterAdapter
->SpinLock
, &OldIrql
);
2011 if (AdapterObject
->AdapterNumber
== 1)
2013 PDMA1_CONTROL DmaControl1
= AdapterObject
->AdapterBaseVa
;
2015 /* Reset Register */
2016 WRITE_PORT_UCHAR(&DmaControl1
->ClearBytePointer
, 0);
2019 WRITE_PORT_UCHAR(&DmaControl1
->Mode
, AdapterMode
.Byte
);
2021 /* Set the Offset Register */
2022 WRITE_PORT_UCHAR(&DmaControl1
->DmaAddressCount
[AdapterObject
->ChannelNumber
].DmaBaseAddress
,
2023 (UCHAR
)(TransferOffset
));
2024 WRITE_PORT_UCHAR(&DmaControl1
->DmaAddressCount
[AdapterObject
->ChannelNumber
].DmaBaseAddress
,
2025 (UCHAR
)(TransferOffset
>> 8));
2027 /* Set the Page Register */
2028 WRITE_PORT_UCHAR(AdapterObject
->PagePort
+ FIELD_OFFSET(EISA_CONTROL
, DmaController1Pages
),
2029 (UCHAR
)(PhysicalAddress
.LowPart
>> 16));
2032 WRITE_PORT_UCHAR(AdapterObject
->PagePort
+ FIELD_OFFSET(EISA_CONTROL
, DmaController2Pages
),
2036 /* Set the Length */
2037 WRITE_PORT_UCHAR(&DmaControl1
->DmaAddressCount
[AdapterObject
->ChannelNumber
].DmaBaseCount
,
2038 (UCHAR
)(TransferLength
- 1));
2039 WRITE_PORT_UCHAR(&DmaControl1
->DmaAddressCount
[AdapterObject
->ChannelNumber
].DmaBaseCount
,
2040 (UCHAR
)((TransferLength
- 1) >> 8));
2042 /* Unmask the Channel */
2043 WRITE_PORT_UCHAR(&DmaControl1
->SingleMask
, AdapterObject
->ChannelNumber
| DMA_CLEARMASK
);
2047 PDMA2_CONTROL DmaControl2
= AdapterObject
->AdapterBaseVa
;
2049 /* Reset Register */
2050 WRITE_PORT_UCHAR(&DmaControl2
->ClearBytePointer
, 0);
2053 WRITE_PORT_UCHAR(&DmaControl2
->Mode
, AdapterMode
.Byte
);
2055 /* Set the Offset Register */
2056 WRITE_PORT_UCHAR(&DmaControl2
->DmaAddressCount
[AdapterObject
->ChannelNumber
].DmaBaseAddress
,
2057 (UCHAR
)(TransferOffset
));
2058 WRITE_PORT_UCHAR(&DmaControl2
->DmaAddressCount
[AdapterObject
->ChannelNumber
].DmaBaseAddress
,
2059 (UCHAR
)(TransferOffset
>> 8));
2061 /* Set the Page Register */
2062 WRITE_PORT_UCHAR(AdapterObject
->PagePort
+ FIELD_OFFSET(EISA_CONTROL
, DmaController1Pages
),
2063 (UCHAR
)(PhysicalAddress
.u
.LowPart
>> 16));
2066 WRITE_PORT_UCHAR(AdapterObject
->PagePort
+ FIELD_OFFSET(EISA_CONTROL
, DmaController2Pages
),
2070 /* Set the Length */
2071 WRITE_PORT_UCHAR(&DmaControl2
->DmaAddressCount
[AdapterObject
->ChannelNumber
].DmaBaseCount
,
2072 (UCHAR
)(TransferLength
- 1));
2073 WRITE_PORT_UCHAR(&DmaControl2
->DmaAddressCount
[AdapterObject
->ChannelNumber
].DmaBaseCount
,
2074 (UCHAR
)((TransferLength
- 1) >> 8));
2076 /* Unmask the Channel */
2077 WRITE_PORT_UCHAR(&DmaControl2
->SingleMask
,
2078 AdapterObject
->ChannelNumber
| DMA_CLEARMASK
);
2081 KeReleaseSpinLock(&AdapterObject
->MasterAdapter
->SpinLock
, OldIrql
);
2085 * Return physical address of the buffer with data that is used for the
2086 * transfer. It can either point inside the Mdl that was passed by the
2087 * caller or into the map registers if the Mdl buffer can't be used
2090 return PhysicalAddress
;
2095 * @name HalFlushCommonBuffer
2101 HalFlushCommonBuffer(IN PADAPTER_OBJECT AdapterObject
,
2103 IN PHYSICAL_ADDRESS LogicalAddress
,
2104 IN PVOID VirtualAddress
)
2106 /* Function always returns true */
2115 HalAllocateCrashDumpRegisters(IN PADAPTER_OBJECT AdapterObject
,
2116 IN OUT PULONG NumberOfMapRegisters
)
2118 PADAPTER_OBJECT MasterAdapter
= AdapterObject
->MasterAdapter
;
2119 ULONG MapRegisterNumber
;
2121 /* Check if it needs map registers */
2122 if (AdapterObject
->NeedsMapRegisters
)
2124 /* Check if we have enough */
2125 if (*NumberOfMapRegisters
> AdapterObject
->MapRegistersPerChannel
)
2127 /* We don't, fail */
2128 AdapterObject
->NumberOfMapRegisters
= 0;
2132 /* Try to find free map registers */
2133 MapRegisterNumber
= RtlFindClearBitsAndSet(MasterAdapter
->MapRegisters
,
2134 *NumberOfMapRegisters
,
2137 /* Check if nothing was found */
2138 if (MapRegisterNumber
== MAXULONG
)
2140 /* No free registers found, so use the base registers */
2141 RtlSetBits(MasterAdapter
->MapRegisters
,
2143 *NumberOfMapRegisters
);
2144 MapRegisterNumber
= 0;
2147 /* Calculate the new base */
2148 AdapterObject
->MapRegisterBase
=
2149 (PROS_MAP_REGISTER_ENTRY
)(MasterAdapter
->MapRegisterBase
+
2152 /* Check if scatter gather isn't supported */
2153 if (!AdapterObject
->ScatterGather
)
2156 AdapterObject
->MapRegisterBase
=
2157 (PROS_MAP_REGISTER_ENTRY
)
2158 ((ULONG_PTR
)AdapterObject
->MapRegisterBase
| MAP_BASE_SW_SG
);
2163 AdapterObject
->MapRegisterBase
= NULL
;
2164 AdapterObject
->NumberOfMapRegisters
= 0;
2167 /* Return the base */
2168 return AdapterObject
->MapRegisterBase
;