Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2568 lines
70 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. ixhwsup.c
  5. Abstract:
  6. This module contains the IoXxx routines for the NT I/O system that
  7. are hardware dependent. Were these routines not hardware dependent,
  8. they would reside in the iosubs.c module.
  9. Author:
  10. Darryl E. Havens (darrylh) 11-Apr-1990
  11. Environment:
  12. Kernel mode
  13. Revision History:
  14. --*/
  15. #include "halp.h"
  16. #include "eisa.h"
  17. #include "pci.h"
  18. #include "pcip.h"
  19. //
  20. //Only take the prototype, don't instantiate
  21. //
  22. #include <wdmguid.h>
  23. #include "halpnpp.h"
  24. #ifdef ALLOC_PRAGMA
  25. #pragma alloc_text(PAGE,HalGetAdapter)
  26. #pragma alloc_text(PAGE,HalpGetIsaIrqState)
  27. #endif
  28. //
  29. // The HalpNewAdapter event is used to serialize allocations
  30. // of new adapter objects, additions to the HalpEisaAdapter
  31. // array, and some global values (MasterAdapterObject) and some
  32. // adapter fields modified by HalpGrowMapBuffers.
  33. // (AdapterObject->NumberOfMapRegisters is assumed not to be
  34. // growable while this even is held)
  35. //
  36. // Note: We don't really need our own an event object for this.
  37. //
  38. #ifndef ACPI_HAL
  39. #define HalpNewAdapter HalpBusDatabaseEvent
  40. extern KEVENT HalpNewAdapter;
  41. #else
  42. KEVENT HalpNewAdapter;
  43. //
  44. //F-Type DMA interface globals
  45. //
  46. ISA_FTYPE_DMA_INTERFACE HalpFDMAInterface;
  47. ULONG HalpFDMAAvail=FALSE;
  48. ULONG HalpFDMAChecked=FALSE;
  49. #endif
  50. #define ACQUIRE_NEW_ADAPTER_LOCK() \
  51. { \
  52. KeWaitForSingleObject ( \
  53. &HalpNewAdapter, \
  54. WrExecutive, \
  55. KernelMode, \
  56. FALSE, \
  57. NULL \
  58. ); \
  59. }
  60. #define RELEASE_NEW_ADAPTER_LOCK() \
  61. KeSetEvent (&HalpNewAdapter, 0, FALSE)
  62. PVOID HalpEisaControlBase = 0;
  63. extern KSPIN_LOCK HalpSystemHardwareLock;
  64. //
  65. // Define save area for EISA adapter objects.
  66. //
  67. PADAPTER_OBJECT HalpEisaAdapter[8];
  68. //
  69. // DMA channel control values
  70. // Global, so zero initialized by the compiler.
  71. //
  72. DMA_CHANNEL_CONTEXT HalpDmaChannelState [EISA_DMA_CHANNELS] ;
  73. extern USHORT HalpEisaIrqMask;
  74. //
  75. // Keep a list of all the dma adapters for debugging purposes
  76. //
  77. LIST_ENTRY HalpDmaAdapterList;
  78. KSPIN_LOCK HalpDmaAdapterListLock;
  79. VOID
  80. HalpCopyBufferMap(
  81. IN PMDL Mdl,
  82. IN PTRANSLATION_ENTRY TranslationEntry,
  83. IN PVOID CurrentVa,
  84. IN ULONG Length,
  85. IN BOOLEAN WriteToDevice
  86. );
  87. PHYSICAL_ADDRESS
  88. HalpMapTransfer(
  89. IN PADAPTER_OBJECT AdapterObject,
  90. IN PMDL Mdl,
  91. IN PVOID MapRegisterBase,
  92. IN PVOID CurrentVa,
  93. IN OUT PULONG Length,
  94. IN BOOLEAN WriteToDevice
  95. );
  96. VOID
  97. HalpMapTransferHelper(
  98. IN PMDL Mdl,
  99. IN PVOID CurrentVa,
  100. IN ULONG TransferLength,
  101. IN PPFN_NUMBER PageFrame,
  102. IN OUT PULONG Length
  103. );
  104. NTSTATUS
  105. HalAllocateAdapterChannel(
  106. IN PADAPTER_OBJECT AdapterObject,
  107. IN PWAIT_CONTEXT_BLOCK Wcb,
  108. IN ULONG NumberOfMapRegisters,
  109. IN PDRIVER_CONTROL ExecutionRoutine
  110. )
  111. /*++
  112. Routine Description:
  113. This routine allocates the adapter channel specified by the adapter object.
  114. This is accomplished by placing the device object of the driver that wants
  115. to allocate the adapter on the adapter's queue. If the queue is already
  116. "busy", then the adapter has already been allocated, so the device object
  117. is simply placed onto the queue and waits until the adapter becomes free.
  118. Once the adapter becomes free (or if it already is), then the driver's
  119. execution routine is invoked.
  120. Also, a number of map registers may be allocated to the driver by specifying
  121. a non-zero value for NumberOfMapRegisters. Then the map register must be
  122. allocated from the master adapter. Once there are a sufficient number of
  123. map registers available, then the execution routine is called and the
  124. base address of the allocated map registers in the adapter is also passed
  125. to the driver's execution routine.
  126. Arguments:
  127. AdapterObject - Pointer to the adapter control object to allocate to the
  128. driver.
  129. Wcb - Supplies a wait context block for saving the allocation parameters.
  130. The DeviceObject, CurrentIrp and DeviceContext should be initalized.
  131. NumberOfMapRegisters - The number of map registers that are to be allocated
  132. from the channel, if any.
  133. ExecutionRoutine - The address of the driver's execution routine that is
  134. invoked once the adapter channel (and possibly map registers) have been
  135. allocated.
  136. Return Value:
  137. Returns STATUS_SUCESS unless too many map registers are requested.
  138. Notes:
  139. Note that this routine MUST be invoked at DISPATCH_LEVEL or above.
  140. --*/
  141. {
  142. PADAPTER_OBJECT MasterAdapter;
  143. BOOLEAN Busy = FALSE;
  144. IO_ALLOCATION_ACTION Action;
  145. KIRQL Irql;
  146. ULONG MapRegisterNumber;
  147. //
  148. // Begin by obtaining a pointer to the master adapter associated with this
  149. // request.
  150. //
  151. MasterAdapter = AdapterObject->MasterAdapter;
  152. //
  153. // Initialize the device object's wait context block in case this device
  154. // must wait before being able to allocate the adapter.
  155. //
  156. Wcb->DeviceRoutine = ExecutionRoutine;
  157. Wcb->NumberOfMapRegisters = NumberOfMapRegisters;
  158. //
  159. // Allocate the adapter object for this particular device. If the
  160. // adapter cannot be allocated because it has already been allocated
  161. // to another device, then return to the caller now; otherwise,
  162. // continue.
  163. //
  164. if (!KeInsertDeviceQueue( &AdapterObject->ChannelWaitQueue,
  165. &Wcb->WaitQueueEntry )) {
  166. //
  167. // Save the parameters in case there are not enough map registers.
  168. //
  169. AdapterObject->NumberOfMapRegisters = NumberOfMapRegisters;
  170. AdapterObject->CurrentWcb = Wcb;
  171. //
  172. // The adapter was not busy so it has been allocated. Now check
  173. // to see whether this driver wishes to allocate any map registers.
  174. // Ensure that this adapter has enough total map registers
  175. // to satisfy the request.
  176. //
  177. if (NumberOfMapRegisters != 0 && AdapterObject->NeedsMapRegisters) {
  178. //
  179. // Lock the map register bit map and the adapter queue in the
  180. // master adapter object. The channel structure offset is used as
  181. // a hint for the register search.
  182. //
  183. if (NumberOfMapRegisters > AdapterObject->MapRegistersPerChannel) {
  184. AdapterObject->NumberOfMapRegisters = 0;
  185. IoFreeAdapterChannel(AdapterObject);
  186. return (STATUS_INSUFFICIENT_RESOURCES);
  187. }
  188. KeAcquireSpinLock( &MasterAdapter->SpinLock, &Irql );
  189. MapRegisterNumber = (ULONG)-1;
  190. if (IsListEmpty( &MasterAdapter->AdapterQueue)) {
  191. MapRegisterNumber = RtlFindClearBitsAndSet(
  192. MasterAdapter->MapRegisters,
  193. NumberOfMapRegisters,
  194. 0
  195. );
  196. }
  197. if (MapRegisterNumber == -1) {
  198. PBUFFER_GROW_WORK_ITEM bufferWorkItem;
  199. //
  200. // There were not enough free map registers. Queue this request
  201. // on the master adapter where is will wait until some registers
  202. // are deallocated.
  203. //
  204. InsertTailList( &MasterAdapter->AdapterQueue,
  205. &AdapterObject->AdapterQueue
  206. );
  207. Busy = 1;
  208. //
  209. // Queue a work item to grow the map registers
  210. //
  211. bufferWorkItem =
  212. ExAllocatePoolWithTag( NonPagedPool,
  213. sizeof(BUFFER_GROW_WORK_ITEM),
  214. HAL_POOL_TAG);
  215. if (bufferWorkItem != NULL) {
  216. ExInitializeWorkItem( &bufferWorkItem->WorkItem,
  217. HalpGrowMapBufferWorker,
  218. bufferWorkItem );
  219. bufferWorkItem->AdapterObject = AdapterObject;
  220. bufferWorkItem->MapRegisterCount = NumberOfMapRegisters;
  221. ExQueueWorkItem( &bufferWorkItem->WorkItem,
  222. DelayedWorkQueue );
  223. }
  224. } else {
  225. //
  226. // Calculate the map register base from the allocated map
  227. // register and base of the master adapter object.
  228. //
  229. AdapterObject->MapRegisterBase = ((PTRANSLATION_ENTRY)
  230. MasterAdapter->MapRegisterBase + MapRegisterNumber);
  231. //
  232. // Set the no scatter/gather flag if scatter/gather not
  233. // supported.
  234. //
  235. if (!AdapterObject->ScatterGather) {
  236. AdapterObject->MapRegisterBase = (PVOID)
  237. ((ULONG_PTR) AdapterObject->MapRegisterBase | NO_SCATTER_GATHER);
  238. }
  239. }
  240. KeReleaseSpinLock( &MasterAdapter->SpinLock, Irql );
  241. } else {
  242. AdapterObject->MapRegisterBase = NULL;
  243. AdapterObject->NumberOfMapRegisters = 0;
  244. }
  245. //
  246. // If there were either enough map registers available or no map
  247. // registers needed to be allocated, invoke the driver's execution
  248. // routine now.
  249. //
  250. if (!Busy) {
  251. AdapterObject->CurrentWcb = Wcb;
  252. Action = ExecutionRoutine( Wcb->DeviceObject,
  253. Wcb->CurrentIrp,
  254. AdapterObject->MapRegisterBase,
  255. Wcb->DeviceContext );
  256. //
  257. // If the driver would like to have the adapter deallocated,
  258. // then release the adapter object.
  259. //
  260. if (Action == DeallocateObject) {
  261. IoFreeAdapterChannel( AdapterObject );
  262. } else if (Action == DeallocateObjectKeepRegisters) {
  263. //
  264. // Set the NumberOfMapRegisters = 0 in the adapter object.
  265. // This will keep IoFreeAdapterChannel from freeing the
  266. // registers. After this it is the driver's responsiblity to
  267. // keep track of the number of map registers.
  268. //
  269. AdapterObject->NumberOfMapRegisters = 0;
  270. IoFreeAdapterChannel(AdapterObject);
  271. }
  272. }
  273. }
  274. return (STATUS_SUCCESS);
  275. }
  276. VOID
  277. HalpGrowMapBufferWorker(
  278. IN PVOID Context
  279. )
  280. /*++
  281. Routine Description:
  282. This routine is called in the context of a work item from
  283. HalAllocateAdapterChannel() when it queues a map register allocation
  284. because map regiers are not available.
  285. Its purpose is to attempt to grow the map buffers for the adapter and,
  286. if successful, process queued adapter allocations.
  287. Arguments:
  288. Context - Actually a pointer to a BUFFER_GROW_WORK_ITEM structure.
  289. Return Value:
  290. None.
  291. --*/
  292. {
  293. PBUFFER_GROW_WORK_ITEM growWorkItem;
  294. PADAPTER_OBJECT masterAdapter;
  295. BOOLEAN allocated;
  296. ULONG bytesToGrow;
  297. KIRQL oldIrql;
  298. growWorkItem = (PBUFFER_GROW_WORK_ITEM)Context;
  299. masterAdapter = growWorkItem->AdapterObject->MasterAdapter;
  300. //
  301. // HalpGrowMapBuffers() takes a byte count
  302. //
  303. bytesToGrow = growWorkItem->MapRegisterCount * PAGE_SIZE +
  304. INCREMENT_MAP_BUFFER_SIZE;
  305. ACQUIRE_NEW_ADAPTER_LOCK();
  306. allocated = HalpGrowMapBuffers( masterAdapter,
  307. bytesToGrow );
  308. RELEASE_NEW_ADAPTER_LOCK();
  309. if (allocated != FALSE) {
  310. KeRaiseIrql( DISPATCH_LEVEL, &oldIrql );
  311. //
  312. // The map buffers were grown. It is likely that someone is waiting
  313. // in the adapter queue, so try to get things started.
  314. //
  315. // The code in IoFreeMapRegisters() does this, and it turns out
  316. // we can safely get it to do this work for us by freeing 0
  317. // map registers at a bogus (but non-NULL) register base.
  318. //
  319. IoFreeMapRegisters( growWorkItem->AdapterObject,
  320. (PVOID)2,
  321. 0 );
  322. KeLowerIrql( oldIrql );
  323. }
  324. ExFreePool( growWorkItem );
  325. }
  326. PVOID
  327. HalAllocateCrashDumpRegisters(
  328. IN PADAPTER_OBJECT AdapterObject,
  329. IN PULONG NumberOfMapRegisters
  330. )
  331. /*++
  332. Routine Description:
  333. This routine is called during the crash dump disk driver's initialization
  334. to allocate a number map registers permanently.
  335. Arguments:
  336. AdapterObject - Pointer to the adapter control object to allocate to the
  337. driver.
  338. NumberOfMapRegisters - Number of map registers requested. This field
  339. will be updated to reflect the actual number of registers allocated
  340. when the number is less than what was requested.
  341. Return Value:
  342. Returns STATUS_SUCESS if map registers allocated.
  343. --*/
  344. {
  345. PADAPTER_OBJECT MasterAdapter;
  346. ULONG MapRegisterNumber;
  347. //
  348. // Begin by obtaining a pointer to the master adapter associated with this
  349. // request.
  350. //
  351. MasterAdapter = AdapterObject->MasterAdapter;
  352. //
  353. // Check to see whether this driver needs to allocate any map registers.
  354. //
  355. if (AdapterObject->NeedsMapRegisters) {
  356. //
  357. // Ensure that this adapter has enough total map registers to satisfy
  358. // the request.
  359. //
  360. if (*NumberOfMapRegisters > AdapterObject->MapRegistersPerChannel) {
  361. AdapterObject->NumberOfMapRegisters = 0;
  362. return NULL;
  363. }
  364. //
  365. // Attempt to allocate the required number of map registers w/o
  366. // affecting those registers that were allocated when the system
  367. // crashed.
  368. //
  369. MapRegisterNumber = (ULONG)-1;
  370. MapRegisterNumber = RtlFindClearBitsAndSet(
  371. MasterAdapter->MapRegisters,
  372. *NumberOfMapRegisters,
  373. 0
  374. );
  375. if (MapRegisterNumber == (ULONG)-1) {
  376. //
  377. // Not enough free map registers were found, so they were busy
  378. // being used by the system when it crashed. Force the appropriate
  379. // number to be "allocated" at the base by simply overjamming the
  380. // bits and return the base map register as the start.
  381. //
  382. RtlSetBits(
  383. MasterAdapter->MapRegisters,
  384. 0,
  385. *NumberOfMapRegisters
  386. );
  387. MapRegisterNumber = 0;
  388. }
  389. //
  390. // Calculate the map register base from the allocated map
  391. // register and base of the master adapter object.
  392. //
  393. AdapterObject->MapRegisterBase = ((PTRANSLATION_ENTRY)
  394. MasterAdapter->MapRegisterBase + MapRegisterNumber);
  395. //
  396. // Set the no scatter/gather flag if scatter/gather not
  397. // supported.
  398. //
  399. if (!AdapterObject->ScatterGather) {
  400. AdapterObject->MapRegisterBase = (PVOID)
  401. ((ULONG_PTR) AdapterObject->MapRegisterBase | NO_SCATTER_GATHER);
  402. }
  403. } else {
  404. AdapterObject->MapRegisterBase = NULL;
  405. AdapterObject->NumberOfMapRegisters = 0;
  406. }
  407. return AdapterObject->MapRegisterBase;
  408. }
  409. #ifdef ACPI_HAL
  410. NTSTATUS
  411. HalpFDMANotificationCallback(
  412. IN PVOID NotificationStructure,
  413. IN PVOID Context
  414. )
  415. {
  416. PAGED_CODE();
  417. //
  418. // Something is happening to the ISA bus that we've registered on.
  419. //
  420. if (IsEqualGUID (&((PTARGET_DEVICE_REMOVAL_NOTIFICATION)NotificationStructure)->Event,
  421. &GUID_TARGET_DEVICE_QUERY_REMOVE)) {
  422. //
  423. // It's a query remove, just get out.
  424. // dereference the interface, and clean up our internal data
  425. //
  426. ACQUIRE_NEW_ADAPTER_LOCK();
  427. HalpFDMAInterface.InterfaceDereference(HalpFDMAInterface.Context);
  428. HalpFDMAAvail=FALSE;
  429. //
  430. // Set checked to false, so that if a new bus arrives we can begin anew.
  431. //
  432. HalpFDMAChecked=FALSE;
  433. RELEASE_NEW_ADAPTER_LOCK();
  434. }
  435. return STATUS_SUCCESS;
  436. }
  437. #endif
  438. VOID
  439. HalpAddAdapterToList(
  440. IN PADAPTER_OBJECT AdapterObject
  441. )
  442. /*++
  443. Routine Description:
  444. Adds the adapter object to the HalpDmaAdapterList. This is a separate
  445. function because HalGetAdapter is paged code and cannot acquire a spinlock.
  446. Arguments:
  447. AdapterObject - Supplies the adapter object to be added to HalpDmaAdapterList
  448. Return Value:
  449. None
  450. --*/
  451. {
  452. KIRQL Irql;
  453. KeAcquireSpinLock(&HalpDmaAdapterListLock,&Irql);
  454. InsertTailList(&HalpDmaAdapterList, &AdapterObject->AdapterList);
  455. KeReleaseSpinLock(&HalpDmaAdapterListLock, Irql);
  456. }
  457. PADAPTER_OBJECT
  458. HalGetAdapter(
  459. IN PDEVICE_DESCRIPTION DeviceDescriptor,
  460. OUT PULONG NumberOfMapRegisters
  461. )
  462. /*++
  463. Routine Description:
  464. This function returns the appropriate adapter object for the device defined
  465. in the device description structure. This code works for Isa and Eisa
  466. systems.
  467. Arguments:
  468. DeviceDescriptor - Supplies a description of the deivce.
  469. NumberOfMapRegisters - Returns the maximum number of map registers which
  470. may be allocated by the device driver.
  471. Return Value:
  472. A pointer to the requested adapter object or NULL if an adapter could not
  473. be created.
  474. --*/
  475. {
  476. PADAPTER_OBJECT adapterObject;
  477. PVOID adapterBaseVa;
  478. ULONG channelNumber;
  479. ULONG controllerNumber;
  480. DMA_EXTENDED_MODE extendedMode;
  481. UCHAR adapterMode;
  482. ULONG numberOfMapRegisters;
  483. BOOLEAN useChannel;
  484. ULONG maximumLength;
  485. UCHAR DataByte;
  486. BOOLEAN dma32Bit;
  487. BOOLEAN ChannelEnabled;
  488. KIRQL Irql;
  489. #ifdef ACPI_HAL
  490. NTSTATUS Status;
  491. #endif
  492. PAGED_CODE();
  493. //
  494. // Make sure this is the correct version.
  495. //
  496. if (DeviceDescriptor->Version > DEVICE_DESCRIPTION_VERSION2) {
  497. return ( NULL );
  498. }
  499. #if DBG
  500. if (DeviceDescriptor->Version == DEVICE_DESCRIPTION_VERSION1) {
  501. ASSERT (DeviceDescriptor->Reserved1 == FALSE);
  502. }
  503. #endif
  504. *((PUCHAR) &extendedMode) = 0;
  505. //
  506. // Determine if the the channel number is important. Master cards on
  507. // Eisa and Mca do not use a channel number.
  508. //
  509. if (DeviceDescriptor->InterfaceType != Isa &&
  510. DeviceDescriptor->Master) {
  511. useChannel = FALSE;
  512. } else {
  513. useChannel = TRUE;
  514. }
  515. // Support for ISA local bus machines:
  516. // If the driver is a Master but really does not want a channel since it
  517. // is using the local bus DMA, just don't use an ISA channel.
  518. //
  519. if (DeviceDescriptor->InterfaceType == Isa &&
  520. DeviceDescriptor->DmaChannel > 7) {
  521. useChannel = FALSE;
  522. }
  523. //
  524. // Determine if Eisa DMA is supported.
  525. //
  526. if (HalpBusType == MACHINE_TYPE_EISA) {
  527. WRITE_PORT_UCHAR(&((PEISA_CONTROL) HalpEisaControlBase)->DmaPageHighPort.Channel2, 0x55);
  528. DataByte = READ_PORT_UCHAR(&((PEISA_CONTROL) HalpEisaControlBase)->DmaPageHighPort.Channel2);
  529. if (DataByte == 0x55) {
  530. HalpEisaDma = TRUE;
  531. }
  532. }
  533. //
  534. // Limit the maximum length to 2 GB this is done so that the BYTES_TO_PAGES
  535. // macro works correctly.
  536. //
  537. maximumLength = DeviceDescriptor->MaximumLength & 0x7fffffff;
  538. //
  539. // Channel 4 cannot be used since it is used for chaining. Return null if
  540. // it is requested.
  541. //
  542. if (DeviceDescriptor->DmaChannel == 4 && useChannel) {
  543. return (NULL);
  544. }
  545. if (DeviceDescriptor->InterfaceType == PCIBus &&
  546. DeviceDescriptor->Master != FALSE &&
  547. DeviceDescriptor->ScatterGather != FALSE) {
  548. //
  549. // This device can handle 32 bits, even if the caller forgot to
  550. // set Dma32BitAddresses.
  551. //
  552. DeviceDescriptor->Dma32BitAddresses = TRUE;
  553. }
  554. dma32Bit = DeviceDescriptor->Dma32BitAddresses;
  555. //
  556. // Determine the number of map registers for this device.
  557. //
  558. if (DeviceDescriptor->ScatterGather &&
  559. //
  560. // If we are not in PAE mode or the device can handle 64 bit addresses,
  561. // then the device can DMA to any physical location
  562. //
  563. (HalPaeEnabled() == FALSE ||
  564. DeviceDescriptor->Dma64BitAddresses != FALSE) &&
  565. (LessThan16Mb ||
  566. DeviceDescriptor->InterfaceType == Eisa ||
  567. DeviceDescriptor->InterfaceType == PCIBus) ) {
  568. //
  569. // Since the device support scatter/Gather then map registers are not
  570. // required.
  571. //
  572. numberOfMapRegisters = 0;
  573. } else {
  574. ULONG maximumMapRegisters;
  575. ULONG mapBufferSize;
  576. maximumMapRegisters = HalpMaximumMapRegisters( dma32Bit );
  577. //
  578. // Determine the number of map registers required based on the maximum
  579. // transfer length, up to a maximum number.
  580. //
  581. numberOfMapRegisters = BYTES_TO_PAGES(maximumLength) + 1;
  582. if (numberOfMapRegisters > maximumMapRegisters) {
  583. numberOfMapRegisters = maximumMapRegisters;
  584. }
  585. //
  586. // Make sure there where enough registers allocated initalize to support
  587. // this size relaibly. This implies there must be to chunks equal to
  588. // the allocatd size. This is only a problem on Isa systems where the
  589. // map buffers cannot cross 64KB boundtires.
  590. //
  591. mapBufferSize = HalpMapBufferSize( dma32Bit );
  592. if (!HalpEisaDma &&
  593. numberOfMapRegisters > mapBufferSize / (PAGE_SIZE * 2)) {
  594. numberOfMapRegisters = (mapBufferSize / (PAGE_SIZE * 2));
  595. }
  596. //
  597. // If the device is not a master then it only needs one map register
  598. // and does scatter/Gather.
  599. //
  600. if (DeviceDescriptor->ScatterGather && !DeviceDescriptor->Master) {
  601. numberOfMapRegisters = 1;
  602. }
  603. }
  604. //
  605. // Set the channel number number.
  606. //
  607. if (useChannel != FALSE) {
  608. channelNumber = DeviceDescriptor->DmaChannel & 0x03;
  609. //
  610. // Set the adapter base address to the Base address register and
  611. // controller number.
  612. //
  613. if (!(DeviceDescriptor->DmaChannel & 0x04)) {
  614. controllerNumber = 1;
  615. adapterBaseVa =
  616. (PVOID) &((PEISA_CONTROL) HalpEisaControlBase)->Dma1BasePort;
  617. } else {
  618. controllerNumber = 2;
  619. adapterBaseVa =
  620. &((PEISA_CONTROL) HalpEisaControlBase)->Dma2BasePort;
  621. }
  622. } else {
  623. adapterBaseVa = NULL;
  624. }
  625. //
  626. // Determine if a new adapter object is necessary. If so then allocate it.
  627. //
  628. if (useChannel && HalpEisaAdapter[DeviceDescriptor->DmaChannel] != NULL) {
  629. adapterObject = HalpEisaAdapter[DeviceDescriptor->DmaChannel];
  630. if (adapterObject->NeedsMapRegisters) {
  631. if (numberOfMapRegisters > adapterObject->MapRegistersPerChannel) {
  632. adapterObject->MapRegistersPerChannel = numberOfMapRegisters;
  633. }
  634. }
  635. } else {
  636. //
  637. // Serialize before allocating a new adapter
  638. //
  639. ACQUIRE_NEW_ADAPTER_LOCK();
  640. //
  641. // Determine if a new adapter object has already been allocated.
  642. // If so use it, otherwise allocate a new adapter object
  643. //
  644. if (useChannel && HalpEisaAdapter[DeviceDescriptor->DmaChannel] != NULL) {
  645. adapterObject = HalpEisaAdapter[DeviceDescriptor->DmaChannel];
  646. if (adapterObject->NeedsMapRegisters) {
  647. if (numberOfMapRegisters > adapterObject->MapRegistersPerChannel) {
  648. adapterObject->MapRegistersPerChannel = numberOfMapRegisters;
  649. }
  650. }
  651. } else {
  652. //
  653. // Allocate an adapter object.
  654. //
  655. adapterObject =
  656. (PADAPTER_OBJECT) HalpAllocateAdapterEx( numberOfMapRegisters,
  657. adapterBaseVa,
  658. NULL,
  659. dma32Bit );
  660. if (adapterObject == NULL) {
  661. RELEASE_NEW_ADAPTER_LOCK();
  662. return (NULL);
  663. }
  664. if (useChannel) {
  665. HalpEisaAdapter[DeviceDescriptor->DmaChannel] = adapterObject;
  666. }
  667. //
  668. // Set the maximum number of map registers for this channel bus on
  669. // the number requested and the type of device.
  670. //
  671. if (numberOfMapRegisters) {
  672. PADAPTER_OBJECT masterAdapterObject;
  673. masterAdapterObject =
  674. HalpMasterAdapter( dma32Bit );
  675. //
  676. // The speicified number of registers are actually allowed to be
  677. // allocated.
  678. //
  679. adapterObject->MapRegistersPerChannel = numberOfMapRegisters;
  680. //
  681. // Increase the commitment for the map registers.
  682. //
  683. if (DeviceDescriptor->Master) {
  684. //
  685. // Master I/O devices use several sets of map registers double
  686. // their commitment.
  687. //
  688. masterAdapterObject->CommittedMapRegisters +=
  689. numberOfMapRegisters * 2;
  690. } else {
  691. masterAdapterObject->CommittedMapRegisters +=
  692. numberOfMapRegisters;
  693. }
  694. //
  695. // If the committed map registers is signicantly greater than the
  696. // number allocated then grow the map buffer.
  697. //
  698. if (masterAdapterObject->CommittedMapRegisters >
  699. masterAdapterObject->NumberOfMapRegisters ) {
  700. HalpGrowMapBuffers(
  701. masterAdapterObject,
  702. INCREMENT_MAP_BUFFER_SIZE
  703. );
  704. }
  705. adapterObject->NeedsMapRegisters = TRUE;
  706. } else {
  707. //
  708. // No real map registers were allocated. If this is a master
  709. // device, then the device can have as may registers as it wants.
  710. //
  711. adapterObject->NeedsMapRegisters = FALSE;
  712. if (DeviceDescriptor->Master) {
  713. adapterObject->MapRegistersPerChannel =
  714. BYTES_TO_PAGES( maximumLength ) + 1;
  715. } else {
  716. //
  717. // The device only gets one register. It must call
  718. // IoMapTransfer repeatedly to do a large transfer.
  719. //
  720. adapterObject->MapRegistersPerChannel = 1;
  721. }
  722. }
  723. }
  724. RELEASE_NEW_ADAPTER_LOCK();
  725. }
  726. adapterObject->IgnoreCount = FALSE;
  727. if (DeviceDescriptor->Version >= DEVICE_DESCRIPTION_VERSION1) {
  728. //
  729. // Move version 1 structure flags.
  730. // IgnoreCount is used on machines where the DMA Counter
  731. // is broken. (Namely PS/1 model 1000s). Setting this
  732. // bit informs the hal not to rely on the DmaCount to determine
  733. // how much data was DMAed.
  734. //
  735. adapterObject->IgnoreCount = DeviceDescriptor->IgnoreCount;
  736. }
  737. adapterObject->Dma32BitAddresses = DeviceDescriptor->Dma32BitAddresses;
  738. adapterObject->Dma64BitAddresses = DeviceDescriptor->Dma64BitAddresses;
  739. adapterObject->ScatterGather = DeviceDescriptor->ScatterGather;
  740. *NumberOfMapRegisters = adapterObject->MapRegistersPerChannel;
  741. if (DeviceDescriptor->Master) {
  742. adapterObject->MasterDevice = TRUE;
  743. } else {
  744. adapterObject->MasterDevice = FALSE;
  745. }
  746. //
  747. // If the channel number is not used then we are finished. The rest of
  748. // the work deals with channels.
  749. //
  750. if (!useChannel) {
  751. //
  752. // Add this adapter to our list
  753. //
  754. HalpAddAdapterToList(adapterObject);
  755. return (adapterObject);
  756. }
  757. //
  758. // Setup the pointers to all the random registers.
  759. //
  760. adapterObject->ChannelNumber = (UCHAR) channelNumber;
  761. if (controllerNumber == 1) {
  762. switch ((UCHAR)channelNumber) {
  763. case 0:
  764. adapterObject->PagePort = (PUCHAR) &((PDMA_PAGE) 0)->Channel0;
  765. break;
  766. case 1:
  767. adapterObject->PagePort = (PUCHAR) &((PDMA_PAGE) 0)->Channel1;
  768. break;
  769. case 2:
  770. adapterObject->PagePort = (PUCHAR) &((PDMA_PAGE) 0)->Channel2;
  771. break;
  772. case 3:
  773. adapterObject->PagePort = (PUCHAR) &((PDMA_PAGE) 0)->Channel3;
  774. break;
  775. }
  776. //
  777. // Set the adapter number.
  778. //
  779. adapterObject->AdapterNumber = 1;
  780. //
  781. // Save the extended mode register address.
  782. //
  783. adapterBaseVa =
  784. &((PEISA_CONTROL) HalpEisaControlBase)->Dma1ExtendedModePort;
  785. } else {
  786. switch (channelNumber) {
  787. case 1:
  788. adapterObject->PagePort = (PUCHAR) &((PDMA_PAGE) 0)->Channel5;
  789. break;
  790. case 2:
  791. adapterObject->PagePort = (PUCHAR) &((PDMA_PAGE) 0)->Channel6;
  792. break;
  793. case 3:
  794. adapterObject->PagePort = (PUCHAR) &((PDMA_PAGE) 0)->Channel7;
  795. break;
  796. }
  797. //
  798. // Set the adapter number.
  799. //
  800. adapterObject->AdapterNumber = 2;
  801. //
  802. // Save the extended mode register address.
  803. //
  804. adapterBaseVa =
  805. &((PEISA_CONTROL) HalpEisaControlBase)->Dma2ExtendedModePort;
  806. }
  807. adapterObject->Width16Bits = FALSE;
  808. #ifdef ACPI_HAL
  809. //
  810. //Keep this code here, because if we ever support dynamic ISA buses (ok, ok, stop laughing)
  811. //We'll want to be able to re-instantiate the interface to the ISAPNP driver for the new bus
  812. //
  813. //
  814. //Get the interface to the ISA bridge iff it supports an
  815. //interface to F-type DMA support
  816. //
  817. if (DeviceDescriptor->DmaSpeed == TypeF) {
  818. if (!HalpFDMAChecked) {
  819. PWSTR HalpFDMAInterfaceList;
  820. Status=IoGetDeviceInterfaces (&GUID_FDMA_INTERFACE_PRIVATE,NULL,0,&HalpFDMAInterfaceList);
  821. if (!NT_SUCCESS (Status)) {
  822. HalpFDMAAvail=FALSE;
  823. } else {
  824. if (HalpFDMAInterfaceList) {
  825. HalpFDMAAvail=TRUE;
  826. }
  827. }
  828. HalpFDMAChecked=TRUE;
  829. //
  830. // Motherboard devices TypeF dma support
  831. //
  832. if (HalpFDMAAvail) {
  833. PDEVICE_OBJECT HalpFDMADevObj;
  834. PFILE_OBJECT HalpFDMAFileObject;
  835. PIRP irp;
  836. KEVENT irpCompleted;
  837. IO_STATUS_BLOCK statusBlock;
  838. PIO_STACK_LOCATION irpStack;
  839. UNICODE_STRING localInterfaceName;
  840. //
  841. // Convert the symbolic link to an object reference
  842. //
  843. RtlInitUnicodeString (&localInterfaceName,HalpFDMAInterfaceList);
  844. Status = IoGetDeviceObjectPointer (&localInterfaceName,
  845. FILE_ALL_ACCESS,
  846. &HalpFDMAFileObject,
  847. &HalpFDMADevObj);
  848. ExFreePool (HalpFDMAInterfaceList);
  849. if (NT_SUCCESS (Status)) {
  850. PVOID HalpFDMANotificationHandle;
  851. //
  852. // Setup the IRP to get the interface
  853. //
  854. KeInitializeEvent(&irpCompleted, SynchronizationEvent, FALSE);
  855. irp = IoBuildSynchronousFsdRequest(IRP_MJ_PNP,
  856. HalpFDMADevObj,
  857. NULL, // Buffer
  858. 0, // Length
  859. 0, // StartingOffset
  860. &irpCompleted,
  861. &statusBlock
  862. );
  863. if (!irp) {
  864. HalpFDMAAvail=FALSE;
  865. goto noFtype;
  866. }
  867. irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
  868. irp->IoStatus.Information = 0;
  869. //
  870. // Initialize the stack location
  871. //
  872. irpStack = IoGetNextIrpStackLocation(irp);
  873. ASSERT(irpStack->MajorFunction == IRP_MJ_PNP);
  874. irpStack->MinorFunction = IRP_MN_QUERY_INTERFACE;
  875. irpStack->Parameters.QueryInterface.InterfaceType =
  876. &GUID_ISA_FDMA_INTERFACE;
  877. irpStack->Parameters.QueryInterface.Size =
  878. sizeof(ISA_FTYPE_DMA_INTERFACE);
  879. irpStack->Parameters.QueryInterface.Version = 1;
  880. irpStack->Parameters.QueryInterface.Interface =
  881. (PINTERFACE) &HalpFDMAInterface;
  882. //
  883. // Call the driver and wait for completion
  884. //
  885. Status = IoCallDriver(HalpFDMADevObj, irp);
  886. if (Status == STATUS_PENDING) {
  887. KeWaitForSingleObject(&irpCompleted,
  888. Executive,
  889. KernelMode,
  890. FALSE,
  891. NULL);
  892. Status = statusBlock.Status;
  893. }
  894. if (!NT_SUCCESS(Status)) {
  895. HalpFDMAAvail=FALSE;
  896. goto noFtype;
  897. }
  898. //
  899. // Now, register a callback so that the ISA bus can go
  900. // away.
  901. //
  902. IoRegisterPlugPlayNotification (EventCategoryTargetDeviceChange,
  903. 0,
  904. HalpFDMAFileObject,
  905. HalpFDMADevObj->DriverObject,
  906. HalpFDMANotificationCallback,
  907. 0,
  908. &HalpFDMANotificationHandle);
  909. //
  910. // Release the handle to the interface from IoGetDevicePointer
  911. //
  912. ObDereferenceObject (HalpFDMAFileObject);
  913. } else {
  914. HalpFDMAAvail=FALSE;
  915. }
  916. }
  917. }
  918. if (HalpFDMAAvail) {
  919. ULONG chMask;
  920. //
  921. // Fence this, so that no two people can ask for F-Type at once.
  922. //
  923. ACQUIRE_NEW_ADAPTER_LOCK();
  924. Status = HalpFDMAInterface.IsaSetFTypeChannel (HalpFDMAInterface.Context,DeviceDescriptor->DmaChannel,&chMask);
  925. RELEASE_NEW_ADAPTER_LOCK();
  926. #if DBG
  927. if (!(NT_SUCCESS (Status))) {
  928. DbgPrint ("HAL: Tried to get F-Type DMA for channel %d, "
  929. "but channel Mask %X already has it!\n",
  930. channelNumber,
  931. chMask);
  932. }
  933. #endif
  934. }
  935. }
  936. noFtype:
  937. #endif
  938. if (HalpEisaDma) {
  939. //
  940. // Initialzie the extended mode port.
  941. //
  942. extendedMode.ChannelNumber = (UCHAR)channelNumber;
  943. switch (DeviceDescriptor->DmaSpeed) {
  944. case Compatible:
  945. extendedMode.TimingMode = COMPATIBLITY_TIMING;
  946. break;
  947. case TypeA:
  948. extendedMode.TimingMode = TYPE_A_TIMING;
  949. break;
  950. case TypeB:
  951. extendedMode.TimingMode = TYPE_B_TIMING;
  952. break;
  953. case TypeC:
  954. extendedMode.TimingMode = BURST_TIMING;
  955. break;
  956. case TypeF:
  957. //
  958. // DMA chip should be set to compatibility mode
  959. // and the bridge handles type-f
  960. //
  961. extendedMode.TimingMode = COMPATIBLITY_TIMING;
  962. break;
  963. default:
  964. ObDereferenceObject( adapterObject );
  965. return (NULL);
  966. }
  967. switch (DeviceDescriptor->DmaWidth) {
  968. case Width8Bits:
  969. extendedMode.TransferSize = BY_BYTE_8_BITS;
  970. break;
  971. case Width16Bits:
  972. extendedMode.TransferSize = BY_BYTE_16_BITS;
  973. //
  974. // Note Width16bits should not be set here because there is no need
  975. // to shift the address and the transfer count.
  976. //
  977. break;
  978. case Width32Bits:
  979. extendedMode.TransferSize = BY_BYTE_32_BITS;
  980. break;
  981. default:
  982. ObDereferenceObject( adapterObject );
  983. return (NULL);
  984. }
  985. WRITE_PORT_UCHAR( adapterBaseVa, *((PUCHAR) &extendedMode));
  986. } else if (!DeviceDescriptor->Master) {
  987. switch (DeviceDescriptor->DmaWidth) {
  988. case Width8Bits:
  989. //
  990. // The channel must use controller 1.
  991. //
  992. if (controllerNumber != 1) {
  993. ObDereferenceObject( adapterObject );
  994. return (NULL);
  995. }
  996. break;
  997. case Width16Bits:
  998. //
  999. // The channel must use controller 2.
  1000. //
  1001. if (controllerNumber != 2) {
  1002. ObDereferenceObject( adapterObject );
  1003. return (NULL);
  1004. }
  1005. adapterObject->Width16Bits = TRUE;
  1006. break;
  1007. default:
  1008. ObDereferenceObject( adapterObject );
  1009. return (NULL);
  1010. }
  1011. }
  1012. //
  1013. // Initialize the adapter mode register value to the correct parameters,
  1014. // and save them in the adapter object.
  1015. //
  1016. ChannelEnabled = FALSE;
  1017. adapterMode = 0;
  1018. ((PDMA_EISA_MODE) &adapterMode)->Channel = adapterObject->ChannelNumber;
  1019. if (DeviceDescriptor->Master) {
  1020. ChannelEnabled = TRUE;
  1021. ((PDMA_EISA_MODE) &adapterMode)->RequestMode = CASCADE_REQUEST_MODE;
  1022. //
  1023. // Set the mode, and enable the request.
  1024. //
  1025. if (adapterObject->AdapterNumber == 1) {
  1026. //
  1027. // This request is for DMA controller 1
  1028. //
  1029. PDMA1_CONTROL dmaControl;
  1030. dmaControl = adapterObject->AdapterBaseVa;
  1031. WRITE_PORT_UCHAR( &dmaControl->Mode, adapterMode );
  1032. //
  1033. // Unmask the DMA channel.
  1034. //
  1035. WRITE_PORT_UCHAR(
  1036. &dmaControl->SingleMask,
  1037. (UCHAR) (DMA_CLEARMASK | adapterObject->ChannelNumber)
  1038. );
  1039. } else {
  1040. //
  1041. // This request is for DMA controller 2
  1042. //
  1043. PDMA2_CONTROL dmaControl;
  1044. dmaControl = adapterObject->AdapterBaseVa;
  1045. WRITE_PORT_UCHAR( &dmaControl->Mode, adapterMode );
  1046. //
  1047. // Unmask the DMA channel.
  1048. //
  1049. WRITE_PORT_UCHAR(
  1050. &dmaControl->SingleMask,
  1051. (UCHAR) (DMA_CLEARMASK | adapterObject->ChannelNumber)
  1052. );
  1053. }
  1054. } else if (DeviceDescriptor->DemandMode) {
  1055. ((PDMA_EISA_MODE) &adapterMode)->RequestMode = DEMAND_REQUEST_MODE;
  1056. } else {
  1057. ((PDMA_EISA_MODE) &adapterMode)->RequestMode = SINGLE_REQUEST_MODE;
  1058. }
  1059. if (DeviceDescriptor->AutoInitialize) {
  1060. ((PDMA_EISA_MODE) &adapterMode)->AutoInitialize = 1;
  1061. }
  1062. adapterObject->AdapterMode = adapterMode;
  1063. //
  1064. // Store the value we wrote to the Mode and Mask registers so that we
  1065. // can restore it after the machine sleeps.
  1066. //
  1067. HalpDmaChannelState [adapterObject->ChannelNumber + ((adapterObject->AdapterNumber - 1) * 4)].ChannelMode =
  1068. adapterMode;
  1069. HalpDmaChannelState [adapterObject->ChannelNumber + ((adapterObject->AdapterNumber - 1) * 4)].ChannelExtendedMode =
  1070. *((PUCHAR)&extendedMode);
  1071. HalpDmaChannelState [adapterObject->ChannelNumber + ((adapterObject->AdapterNumber - 1) * 4)].ChannelMask = (ChannelEnabled) ?
  1072. (UCHAR) (DMA_CLEARMASK | adapterObject->ChannelNumber):
  1073. (UCHAR) (DMA_SETMASK | adapterObject->ChannelNumber);
  1074. HalpDmaChannelState [adapterObject->ChannelNumber + ((adapterObject->AdapterNumber - 1) * 4)].ChannelProgrammed = TRUE;
  1075. return (adapterObject);
  1076. }
  1077. PHYSICAL_ADDRESS
  1078. IoMapTransfer(
  1079. IN PADAPTER_OBJECT AdapterObject,
  1080. IN PMDL Mdl,
  1081. IN PVOID MapRegisterBase,
  1082. IN PVOID CurrentVa,
  1083. IN OUT PULONG Length,
  1084. IN BOOLEAN WriteToDevice
  1085. )
  1086. /*++
  1087. Routine Description:
  1088. This routine is invoked to set up the map registers in the DMA controller
  1089. to allow a transfer to or from a device.
  1090. Arguments:
  1091. AdapterObject - Pointer to the adapter object representing the DMA
  1092. controller channel that has been allocated.
  1093. Mdl - Pointer to the MDL that describes the pages of memory that are
  1094. being read or written.
  1095. MapRegisterBase - The address of the base map register that has been
  1096. allocated to the device driver for use in mapping the transfer.
  1097. CurrentVa - Current virtual address in the buffer described by the MDL
  1098. that the transfer is being done to or from.
  1099. Length - Supplies the length of the transfer. This determines the
  1100. number of map registers that need to be written to map the transfer.
  1101. Returns the length of the transfer which was actually mapped.
  1102. WriteToDevice - Boolean value that indicates whether this is a write
  1103. to the device from memory (TRUE), or vice versa.
  1104. Return Value:
  1105. Returns the logical address that should be used bus master controllers.
  1106. --*/
  1107. {
  1108. ULONG transferLength;
  1109. PHYSICAL_ADDRESS returnAddress;
  1110. PPFN_NUMBER pageFrame;
  1111. ULONG pageOffset;
  1112. //
  1113. // If the adapter is a 32-bit bus master, take the fast path,
  1114. // otherwise call HalpMapTransfer for the slow path
  1115. //
  1116. if (MapRegisterBase == NULL) {
  1117. pageOffset = BYTE_OFFSET(CurrentVa);
  1118. //
  1119. // Calculate how much of the transfer is contiguous
  1120. //
  1121. transferLength = PAGE_SIZE - pageOffset;
  1122. pageFrame = MmGetMdlPfnArray(Mdl);
  1123. pageFrame += ((ULONG_PTR) CurrentVa - (ULONG_PTR) MmGetMdlBaseVa(Mdl)) >> PAGE_SHIFT;
  1124. //
  1125. // Compute the starting address of the transfer
  1126. //
  1127. returnAddress.QuadPart =
  1128. ((ULONG64)*pageFrame << PAGE_SHIFT) + pageOffset;
  1129. //
  1130. // If the transfer is not completely contained within
  1131. // a page, call the helper to compute the appropriate
  1132. // length.
  1133. //
  1134. if (transferLength < *Length) {
  1135. HalpMapTransferHelper(Mdl, CurrentVa, transferLength, pageFrame, Length);
  1136. }
  1137. return (returnAddress);
  1138. }
  1139. return (HalpMapTransfer(AdapterObject,
  1140. Mdl,
  1141. MapRegisterBase,
  1142. CurrentVa,
  1143. Length,
  1144. WriteToDevice));
  1145. }
  1146. VOID
  1147. HalpMapTransferHelper(
  1148. IN PMDL Mdl,
  1149. IN PVOID CurrentVa,
  1150. IN ULONG TransferLength,
  1151. IN PPFN_NUMBER PageFrame,
  1152. IN OUT PULONG Length
  1153. )
  1154. /*++
  1155. Routine Description:
  1156. Helper routine for bus master transfers that cross a page
  1157. boundary. This routine is separated out from the IoMapTransfer
  1158. fast path in order to minimize the total instruction path
  1159. length taken for the common network case where the entire
  1160. buffer being mapped is contained within one page.
  1161. Arguments:
  1162. Mdl - Pointer to the MDL that describes the pages of memory that are
  1163. being read or written.
  1164. CurrentVa - Current virtual address in the buffer described by the MDL
  1165. that the transfer is being done to or from.
  1166. TransferLength = Supplies the current transferLength
  1167. PageFrame - Supplies a pointer to the starting page frame of the transfer
  1168. Length - Supplies the length of the transfer. This determines the
  1169. number of map registers that need to be written to map the transfer.
  1170. Returns the length of the transfer which was actually mapped.
  1171. Return Value:
  1172. None. *Length will be updated
  1173. --*/
  1174. {
  1175. PFN_NUMBER thisPageFrame;
  1176. PFN_NUMBER nextPageFrame;
  1177. do {
  1178. thisPageFrame = *PageFrame;
  1179. PageFrame += 1;
  1180. nextPageFrame = *PageFrame;
  1181. if ((thisPageFrame + 1) != nextPageFrame) {
  1182. //
  1183. // The next page frame is not contiguous with this one,
  1184. // so break the transfer here.
  1185. //
  1186. break;
  1187. }
  1188. if (((thisPageFrame ^ nextPageFrame) & 0xFFFFFFFFFFF00000UI64) != 0) {
  1189. //
  1190. // The next page frame is contiguous with this one,
  1191. // but it crosses a 4GB boundary, another reason to
  1192. // break the transfer.
  1193. //
  1194. break;
  1195. }
  1196. TransferLength += PAGE_SIZE;
  1197. } while ( TransferLength < *Length );
  1198. //
  1199. // Limit the Length to the maximum TransferLength.
  1200. //
  1201. if (TransferLength < *Length) {
  1202. *Length = TransferLength;
  1203. }
  1204. }
  1205. PHYSICAL_ADDRESS
  1206. HalpMapTransfer(
  1207. IN PADAPTER_OBJECT AdapterObject,
  1208. IN PMDL Mdl,
  1209. IN PVOID MapRegisterBase,
  1210. IN PVOID CurrentVa,
  1211. IN OUT PULONG Length,
  1212. IN BOOLEAN WriteToDevice
  1213. )
  1214. /*++
  1215. Routine Description:
  1216. This routine is invoked to set up the map registers in the DMA controller
  1217. to allow a transfer to or from a device.
  1218. Arguments:
  1219. AdapterObject - Pointer to the adapter object representing the DMA
  1220. controller channel that has been allocated.
  1221. Mdl - Pointer to the MDL that describes the pages of memory that are
  1222. being read or written.
  1223. MapRegisterBase - The address of the base map register that has been
  1224. allocated to the device driver for use in mapping the transfer.
  1225. CurrentVa - Current virtual address in the buffer described by the MDL
  1226. that the transfer is being done to or from.
  1227. Length - Supplies the length of the transfer. This determines the
  1228. number of map registers that need to be written to map the transfer.
  1229. Returns the length of the transfer which was actually mapped.
  1230. WriteToDevice - Boolean value that indicates whether this is a write
  1231. to the device from memory (TRUE), or vice versa.
  1232. Return Value:
  1233. Returns the logical address that should be used bus master controllers.
  1234. --*/
  1235. {
  1236. BOOLEAN useBuffer;
  1237. ULONG transferLength;
  1238. PHYSICAL_ADDRESS logicalAddress;
  1239. PHYSICAL_ADDRESS returnAddress;
  1240. ULONG index;
  1241. PPFN_NUMBER pageFrame;
  1242. PUCHAR bytePointer;
  1243. UCHAR adapterMode;
  1244. UCHAR dataByte;
  1245. PTRANSLATION_ENTRY translationEntry;
  1246. ULONG pageOffset;
  1247. KIRQL Irql;
  1248. BOOLEAN masterDevice;
  1249. PHYSICAL_ADDRESS maximumPhysicalAddress;
  1250. masterDevice = AdapterObject == NULL || AdapterObject->MasterDevice ?
  1251. TRUE : FALSE;
  1252. pageOffset = BYTE_OFFSET(CurrentVa);
  1253. #if DBG
  1254. //
  1255. // Catch slave mode devices that seem to want to try and have more than one
  1256. // outstanding request. If they do then the bus locks.
  1257. //
  1258. if (!masterDevice) {
  1259. ASSERT (HalpDmaChannelState [AdapterObject->ChannelNumber + ((AdapterObject->AdapterNumber - 1) * 4)].ChannelBusy == FALSE);
  1260. HalpDmaChannelState [AdapterObject->ChannelNumber + ((AdapterObject->AdapterNumber - 1) * 4)].ChannelBusy =
  1261. TRUE;
  1262. }
  1263. #endif
  1264. //
  1265. // Calculate how much of the transfer is contiguous.
  1266. //
  1267. transferLength = PAGE_SIZE - pageOffset;
  1268. pageFrame = MmGetMdlPfnArray(Mdl);
  1269. pageFrame += ((ULONG_PTR) CurrentVa - (ULONG_PTR) MmGetMdlBaseVa(Mdl)) >> PAGE_SHIFT;
  1270. logicalAddress.QuadPart =
  1271. (((ULONGLONG)*pageFrame) << PAGE_SHIFT) + pageOffset;
  1272. //
  1273. // If the buffer is contigous and does not cross a 64 K bountry then
  1274. // just extend the buffer. The 64 K bountry restriction does not apply
  1275. // to Eisa systems.
  1276. //
  1277. if (HalpEisaDma) {
  1278. while ( transferLength < *Length ) {
  1279. if (*pageFrame + 1 != *(pageFrame + 1)) {
  1280. break;
  1281. }
  1282. transferLength += PAGE_SIZE;
  1283. pageFrame++;
  1284. }
  1285. } else {
  1286. while ( transferLength < *Length ) {
  1287. if (*pageFrame + 1 != *(pageFrame + 1) ||
  1288. (*pageFrame & ~0x0f) != (*(pageFrame + 1) & ~0x0f)) {
  1289. break;
  1290. }
  1291. transferLength += PAGE_SIZE;
  1292. pageFrame++;
  1293. }
  1294. }
  1295. //
  1296. // Limit the transferLength to the requested Length.
  1297. //
  1298. transferLength = transferLength > *Length ? *Length : transferLength;
  1299. ASSERT(MapRegisterBase != NULL);
  1300. //
  1301. // Strip no scatter/gather flag.
  1302. //
  1303. translationEntry = (PTRANSLATION_ENTRY) ((ULONG_PTR) MapRegisterBase & ~NO_SCATTER_GATHER);
  1304. if ((ULONG_PTR) MapRegisterBase & NO_SCATTER_GATHER
  1305. && transferLength < *Length) {
  1306. logicalAddress.QuadPart = translationEntry->PhysicalAddress + pageOffset;
  1307. translationEntry->Index = COPY_BUFFER;
  1308. index = 0;
  1309. transferLength = *Length;
  1310. useBuffer = TRUE;
  1311. } else {
  1312. //
  1313. // If there are map registers, then update the index to indicate
  1314. // how many have been used.
  1315. //
  1316. useBuffer = FALSE;
  1317. index = translationEntry->Index;
  1318. translationEntry->Index += ADDRESS_AND_SIZE_TO_SPAN_PAGES(
  1319. CurrentVa,
  1320. transferLength
  1321. );
  1322. //
  1323. // PeterJ added the following to catch drivers which don't call
  1324. // IoFlushAdapterBuffers. Calling IoMapTransfer repeatedly
  1325. // without calling IoFlushAdapterBuffers will run you out of
  1326. // map registers,.... Some PCI device drivers think they can
  1327. // get away with this because they do 32 bit direct transfers.
  1328. // Try plugging one of these into a system with > 4GB and see
  1329. // what happens to you.
  1330. //
  1331. ASSERT(translationEntry->Index <=
  1332. AdapterObject->MapRegistersPerChannel);
  1333. }
  1334. //
  1335. // It must require memory to be within the adapter's address range. If the
  1336. // logical address is greater than that which the adapter can directly
  1337. // access then map registers must be used
  1338. //
  1339. maximumPhysicalAddress =
  1340. HalpGetAdapterMaximumPhysicalAddress( AdapterObject );
  1341. if ((ULONGLONG)(logicalAddress.QuadPart + transferLength - 1) >
  1342. (ULONGLONG)maximumPhysicalAddress.QuadPart) {
  1343. logicalAddress.QuadPart = (translationEntry + index)->PhysicalAddress +
  1344. pageOffset;
  1345. useBuffer = TRUE;
  1346. if ((ULONG_PTR) MapRegisterBase & NO_SCATTER_GATHER) {
  1347. translationEntry->Index = COPY_BUFFER;
  1348. index = 0;
  1349. }
  1350. }
  1351. //
  1352. // Copy the data if necessary.
  1353. //
  1354. if (useBuffer && WriteToDevice) {
  1355. HalpCopyBufferMap(
  1356. Mdl,
  1357. translationEntry + index,
  1358. CurrentVa,
  1359. transferLength,
  1360. WriteToDevice
  1361. );
  1362. }
  1363. //
  1364. // Return the length.
  1365. //
  1366. *Length = transferLength;
  1367. //
  1368. // Return the logical address to transfer to.
  1369. //
  1370. returnAddress = logicalAddress;
  1371. //
  1372. // If no adapter was specificed then there is no more work to do so
  1373. // return.
  1374. //
  1375. if (AdapterObject == NULL || AdapterObject->MasterDevice) {
  1376. return (returnAddress);
  1377. }
  1378. //
  1379. // Determine the mode based on the transfer direction.
  1380. //
  1381. adapterMode = AdapterObject->AdapterMode;
  1382. if (WriteToDevice) {
  1383. ((PDMA_EISA_MODE) &adapterMode)->TransferType = (UCHAR) WRITE_TRANSFER;
  1384. } else {
  1385. ((PDMA_EISA_MODE) &adapterMode)->TransferType = (UCHAR) READ_TRANSFER;
  1386. if (AdapterObject->IgnoreCount) {
  1387. //
  1388. // When the DMA is over there will be no way to tell how much
  1389. // data was transfered, so the entire transfer length will be
  1390. // copied. To ensure that no stale data is returned to the
  1391. // caller zero the buffer before hand.
  1392. //
  1393. RtlZeroMemory (
  1394. (PUCHAR) translationEntry[index].VirtualAddress + pageOffset,
  1395. transferLength
  1396. );
  1397. }
  1398. }
  1399. bytePointer = (PUCHAR) &logicalAddress;
  1400. if (AdapterObject->Width16Bits) {
  1401. //
  1402. // If this is a 16 bit transfer then adjust the length and the address
  1403. // for the 16 bit DMA mode.
  1404. //
  1405. transferLength >>= 1;
  1406. //
  1407. // In 16 bit DMA mode the low 16 bits are shifted right one and the
  1408. // page register value is unchanged. So save the page register value
  1409. // and shift the logical address then restore the page value.
  1410. //
  1411. dataByte = bytePointer[2];
  1412. logicalAddress.QuadPart >>= 1;
  1413. bytePointer[2] = dataByte;
  1414. }
  1415. //
  1416. // grab the spinlock for the system DMA controller
  1417. //
  1418. KeAcquireSpinLock( &AdapterObject->MasterAdapter->SpinLock, &Irql );
  1419. //
  1420. // Determine the controller number based on the Adapter number.
  1421. //
  1422. if (AdapterObject->AdapterNumber == 1) {
  1423. //
  1424. // This request is for DMA controller 1
  1425. //
  1426. PDMA1_CONTROL dmaControl;
  1427. dmaControl = AdapterObject->AdapterBaseVa;
  1428. WRITE_PORT_UCHAR( &dmaControl->ClearBytePointer, 0 );
  1429. WRITE_PORT_UCHAR( &dmaControl->Mode, adapterMode );
  1430. WRITE_PORT_UCHAR(
  1431. &dmaControl->DmaAddressCount[AdapterObject->ChannelNumber]
  1432. .DmaBaseAddress,
  1433. bytePointer[0]
  1434. );
  1435. WRITE_PORT_UCHAR(
  1436. &dmaControl->DmaAddressCount[AdapterObject->ChannelNumber]
  1437. .DmaBaseAddress,
  1438. bytePointer[1]
  1439. );
  1440. WRITE_PORT_UCHAR(
  1441. ((PUCHAR) &((PEISA_CONTROL) HalpEisaControlBase)->DmaPageLowPort) +
  1442. (ULONG_PTR)AdapterObject->PagePort,
  1443. bytePointer[2]
  1444. );
  1445. if (HalpEisaDma) {
  1446. //
  1447. // Write the high page register with zero value. This enable a special mode
  1448. // which allows ties the page register and base count into a single 24 bit
  1449. // address register.
  1450. //
  1451. WRITE_PORT_UCHAR(
  1452. ((PUCHAR) &((PEISA_CONTROL) HalpEisaControlBase)->DmaPageHighPort) +
  1453. (ULONG_PTR)AdapterObject->PagePort,
  1454. 0
  1455. );
  1456. }
  1457. //
  1458. // Notify DMA chip of the length to transfer.
  1459. //
  1460. WRITE_PORT_UCHAR(
  1461. &dmaControl->DmaAddressCount[AdapterObject->ChannelNumber]
  1462. .DmaBaseCount,
  1463. (UCHAR) ((transferLength - 1) & 0xff)
  1464. );
  1465. WRITE_PORT_UCHAR(
  1466. &dmaControl->DmaAddressCount[AdapterObject->ChannelNumber]
  1467. .DmaBaseCount,
  1468. (UCHAR) ((transferLength - 1) >> 8)
  1469. );
  1470. //
  1471. // Set the DMA chip to read or write mode; and unmask it.
  1472. //
  1473. WRITE_PORT_UCHAR(
  1474. &dmaControl->SingleMask,
  1475. (UCHAR) (DMA_CLEARMASK | AdapterObject->ChannelNumber)
  1476. );
  1477. } else {
  1478. //
  1479. // This request is for DMA controller 2
  1480. //
  1481. PDMA2_CONTROL dmaControl;
  1482. dmaControl = AdapterObject->AdapterBaseVa;
  1483. WRITE_PORT_UCHAR( &dmaControl->ClearBytePointer, 0 );
  1484. WRITE_PORT_UCHAR( &dmaControl->Mode, adapterMode );
  1485. WRITE_PORT_UCHAR(
  1486. &dmaControl->DmaAddressCount[AdapterObject->ChannelNumber]
  1487. .DmaBaseAddress,
  1488. bytePointer[0]
  1489. );
  1490. WRITE_PORT_UCHAR(
  1491. &dmaControl->DmaAddressCount[AdapterObject->ChannelNumber]
  1492. .DmaBaseAddress,
  1493. bytePointer[1]
  1494. );
  1495. WRITE_PORT_UCHAR(
  1496. ((PUCHAR) &((PEISA_CONTROL) HalpEisaControlBase)->DmaPageLowPort) +
  1497. (ULONG_PTR)AdapterObject->PagePort,
  1498. bytePointer[2]
  1499. );
  1500. if (HalpEisaDma) {
  1501. //
  1502. // Write the high page register with zero value. This enable a
  1503. // special mode which allows ties the page register and base
  1504. // count into a single 24 bit address register.
  1505. //
  1506. WRITE_PORT_UCHAR(
  1507. ((PUCHAR) &((PEISA_CONTROL) HalpEisaControlBase)->DmaPageHighPort) +
  1508. (ULONG_PTR)AdapterObject->PagePort,
  1509. 0
  1510. );
  1511. }
  1512. //
  1513. // Notify DMA chip of the length to transfer.
  1514. //
  1515. WRITE_PORT_UCHAR(
  1516. &dmaControl->DmaAddressCount[AdapterObject->ChannelNumber]
  1517. .DmaBaseCount,
  1518. (UCHAR) ((transferLength - 1) & 0xff)
  1519. );
  1520. WRITE_PORT_UCHAR(
  1521. &dmaControl->DmaAddressCount[AdapterObject->ChannelNumber]
  1522. .DmaBaseCount,
  1523. (UCHAR) ((transferLength - 1) >> 8)
  1524. );
  1525. //
  1526. // Set the DMA chip to read or write mode; and unmask it.
  1527. //
  1528. WRITE_PORT_UCHAR(
  1529. &dmaControl->SingleMask,
  1530. (UCHAR) (DMA_CLEARMASK | AdapterObject->ChannelNumber)
  1531. );
  1532. }
  1533. //
  1534. // Record what we wrote to the mask register.
  1535. //
  1536. HalpDmaChannelState [AdapterObject->ChannelNumber + ((AdapterObject->AdapterNumber - 1) * 4)].ChannelMask =
  1537. (UCHAR) (DMA_CLEARMASK | AdapterObject->ChannelNumber);
  1538. KeReleaseSpinLock (&AdapterObject->MasterAdapter->SpinLock, Irql);
  1539. return (returnAddress);
  1540. }
  1541. BOOLEAN
  1542. IoFlushAdapterBuffers(
  1543. IN PADAPTER_OBJECT AdapterObject,
  1544. IN PMDL Mdl,
  1545. IN PVOID MapRegisterBase,
  1546. IN PVOID CurrentVa,
  1547. IN ULONG Length,
  1548. IN BOOLEAN WriteToDevice
  1549. )
  1550. /*++
  1551. Routine Description:
  1552. This routine flushes the DMA adapter object buffers. For the Jazz system
  1553. its clears the enable flag which aborts the dma.
  1554. Arguments:
  1555. AdapterObject - Pointer to the adapter object representing the DMA
  1556. controller channel.
  1557. Mdl - A pointer to a Memory Descriptor List (MDL) that maps the locked-down
  1558. buffer to/from which the I/O occured.
  1559. MapRegisterBase - A pointer to the base of the map registers in the adapter
  1560. or DMA controller.
  1561. CurrentVa - The current virtual address in the buffer described the the Mdl
  1562. where the I/O operation occurred.
  1563. Length - Supplies the length of the transfer.
  1564. WriteToDevice - Supplies a BOOLEAN value that indicates the direction of
  1565. the data transfer was to the device.
  1566. Return Value:
  1567. TRUE - No errors are detected so the transfer must succeed.
  1568. --*/
  1569. {
  1570. PTRANSLATION_ENTRY translationEntry;
  1571. PPFN_NUMBER pageFrame;
  1572. ULONG transferLength;
  1573. ULONG partialLength;
  1574. BOOLEAN masterDevice;
  1575. PHYSICAL_ADDRESS maximumPhysicalAddress;
  1576. ULONG maximumPhysicalPage;
  1577. masterDevice = AdapterObject == NULL || AdapterObject->MasterDevice ?
  1578. TRUE : FALSE;
  1579. //
  1580. // If this is a slave device, then stop the DMA controller.
  1581. //
  1582. if (!masterDevice) {
  1583. //
  1584. // Mask the DMA request line so that DMA requests cannot occur.
  1585. //
  1586. if (AdapterObject->AdapterNumber == 1) {
  1587. //
  1588. // This request is for DMA controller 1
  1589. //
  1590. PDMA1_CONTROL dmaControl;
  1591. dmaControl = AdapterObject->AdapterBaseVa;
  1592. WRITE_PORT_UCHAR(
  1593. &dmaControl->SingleMask,
  1594. (UCHAR) (DMA_SETMASK | AdapterObject->ChannelNumber)
  1595. );
  1596. } else {
  1597. //
  1598. // This request is for DMA controller 2
  1599. //
  1600. PDMA2_CONTROL dmaControl;
  1601. dmaControl = AdapterObject->AdapterBaseVa;
  1602. WRITE_PORT_UCHAR(
  1603. &dmaControl->SingleMask,
  1604. (UCHAR) (DMA_SETMASK | AdapterObject->ChannelNumber)
  1605. );
  1606. }
  1607. //
  1608. // Record what we wrote to the mask register.
  1609. //
  1610. HalpDmaChannelState [AdapterObject->ChannelNumber + ((AdapterObject->AdapterNumber - 1) * 4)].ChannelMask =
  1611. (UCHAR) (DMA_SETMASK | AdapterObject->ChannelNumber);
  1612. //
  1613. // Mark the channel as not in use
  1614. //
  1615. #if DBG
  1616. HalpDmaChannelState [AdapterObject->ChannelNumber + ((AdapterObject->AdapterNumber - 1) * 4)].ChannelBusy =
  1617. FALSE;
  1618. #endif
  1619. }
  1620. if (MapRegisterBase == NULL) {
  1621. return (TRUE);
  1622. }
  1623. //
  1624. // Determine if the data needs to be copied to the orginal buffer.
  1625. // This only occurs if the data tranfer is from the device, the
  1626. // MapReisterBase is not NULL and the transfer spans a page.
  1627. //
  1628. if (!WriteToDevice) {
  1629. //
  1630. // Strip no scatter/gather flag.
  1631. //
  1632. translationEntry = (PTRANSLATION_ENTRY) ((ULONG_PTR) MapRegisterBase & ~NO_SCATTER_GATHER);
  1633. //
  1634. // If this is not a master device, then just transfer the buffer.
  1635. //
  1636. if ((ULONG_PTR) MapRegisterBase & NO_SCATTER_GATHER) {
  1637. if (translationEntry->Index == COPY_BUFFER) {
  1638. if (!masterDevice && !AdapterObject->IgnoreCount) {
  1639. //
  1640. // Copy only the bytes that have actually been transfered.
  1641. //
  1642. //
  1643. Length -= HalReadDmaCounter(AdapterObject);
  1644. }
  1645. //
  1646. // The adapter does not support scatter/gather copy the buffer.
  1647. //
  1648. HalpCopyBufferMap(
  1649. Mdl,
  1650. translationEntry,
  1651. CurrentVa,
  1652. Length,
  1653. WriteToDevice
  1654. );
  1655. }
  1656. } else {
  1657. //
  1658. // Cycle through the pages of the transfer to determine if there
  1659. // are any which need to be copied back.
  1660. //
  1661. maximumPhysicalAddress =
  1662. HalpGetAdapterMaximumPhysicalAddress( AdapterObject );
  1663. maximumPhysicalPage =
  1664. (ULONG)(maximumPhysicalAddress.QuadPart >> PAGE_SHIFT);
  1665. transferLength = PAGE_SIZE - BYTE_OFFSET(CurrentVa);
  1666. partialLength = transferLength;
  1667. pageFrame = MmGetMdlPfnArray(Mdl);
  1668. pageFrame += ((ULONG_PTR) CurrentVa - (ULONG_PTR) MmGetMdlBaseVa(Mdl)) >> PAGE_SHIFT;
  1669. while ( transferLength <= Length ) {
  1670. if (*pageFrame > maximumPhysicalPage) {
  1671. HalpCopyBufferMap(
  1672. Mdl,
  1673. translationEntry,
  1674. CurrentVa,
  1675. partialLength,
  1676. WriteToDevice
  1677. );
  1678. }
  1679. (PCCHAR) CurrentVa += partialLength;
  1680. partialLength = PAGE_SIZE;
  1681. //
  1682. // Note that transferLength indicates the amount which will be
  1683. // transfered after the next loop; thus, it is updated with the
  1684. // new partial length.
  1685. //
  1686. transferLength += partialLength;
  1687. pageFrame++;
  1688. translationEntry++;
  1689. }
  1690. //
  1691. // Process the any remaining residue.
  1692. //
  1693. partialLength = Length - transferLength + partialLength;
  1694. if (partialLength && *pageFrame > maximumPhysicalPage) {
  1695. HalpCopyBufferMap(
  1696. Mdl,
  1697. translationEntry,
  1698. CurrentVa,
  1699. partialLength,
  1700. WriteToDevice
  1701. );
  1702. }
  1703. }
  1704. }
  1705. //
  1706. // Strip no scatter/gather flag.
  1707. //
  1708. translationEntry = (PTRANSLATION_ENTRY) ((ULONG_PTR) MapRegisterBase & ~NO_SCATTER_GATHER);
  1709. //
  1710. // Clear index in map register.
  1711. //
  1712. translationEntry->Index = 0;
  1713. return TRUE;
  1714. }
  1715. ULONG
  1716. HalReadDmaCounter(
  1717. IN PADAPTER_OBJECT AdapterObject
  1718. )
  1719. /*++
  1720. Routine Description:
  1721. This function reads the DMA counter and returns the number of bytes left
  1722. to be transfered.
  1723. Arguments:
  1724. AdapterObject - Supplies a pointer to the adapter object to be read.
  1725. Return Value:
  1726. Returns the number of bytes still be be transfered.
  1727. --*/
  1728. {
  1729. ULONG count;
  1730. ULONG high;
  1731. KIRQL Irql;
  1732. //
  1733. // Grab the spinlock for the system DMA controller.
  1734. //
  1735. KeAcquireSpinLock( &AdapterObject->MasterAdapter->SpinLock, &Irql );
  1736. //
  1737. // Determine the controller number based on the Adapter number.
  1738. //
  1739. if (AdapterObject->AdapterNumber == 1) {
  1740. //
  1741. // This request is for DMA controller 1
  1742. //
  1743. PDMA1_CONTROL dmaControl;
  1744. dmaControl = AdapterObject->AdapterBaseVa;
  1745. WRITE_PORT_UCHAR( &dmaControl->ClearBytePointer, 0 );
  1746. //
  1747. // Initialize count to a value which will not match.
  1748. //
  1749. count = 0xFFFF00;
  1750. //
  1751. // Loop until the same high byte is read twice.
  1752. //
  1753. do {
  1754. high = count;
  1755. WRITE_PORT_UCHAR( &dmaControl->ClearBytePointer, 0 );
  1756. //
  1757. // Read the current DMA count.
  1758. //
  1759. count = READ_PORT_UCHAR(
  1760. &dmaControl->DmaAddressCount[AdapterObject->ChannelNumber]
  1761. .DmaBaseCount
  1762. );
  1763. count |= READ_PORT_UCHAR(
  1764. &dmaControl->DmaAddressCount[AdapterObject->ChannelNumber]
  1765. .DmaBaseCount
  1766. ) << 8;
  1767. } while ((count & 0xFFFF00) != (high & 0xFFFF00));
  1768. } else {
  1769. //
  1770. // This request is for DMA controller 2
  1771. //
  1772. PDMA2_CONTROL dmaControl;
  1773. dmaControl = AdapterObject->AdapterBaseVa;
  1774. WRITE_PORT_UCHAR( &dmaControl->ClearBytePointer, 0 );
  1775. //
  1776. // Initialize count to a value which will not match.
  1777. //
  1778. count = 0xFFFF00;
  1779. //
  1780. // Loop until the same high byte is read twice.
  1781. //
  1782. do {
  1783. high = count;
  1784. WRITE_PORT_UCHAR( &dmaControl->ClearBytePointer, 0 );
  1785. //
  1786. // Read the current DMA count.
  1787. //
  1788. count = READ_PORT_UCHAR(
  1789. &dmaControl->DmaAddressCount[AdapterObject->ChannelNumber]
  1790. .DmaBaseCount
  1791. );
  1792. count |= READ_PORT_UCHAR(
  1793. &dmaControl->DmaAddressCount[AdapterObject->ChannelNumber]
  1794. .DmaBaseCount
  1795. ) << 8;
  1796. } while ((count & 0xFFFF00) != (high & 0xFFFF00));
  1797. }
  1798. //
  1799. // Release the spinlock for the system DMA controller.
  1800. //
  1801. KeReleaseSpinLock( &AdapterObject->MasterAdapter->SpinLock, Irql );
  1802. //
  1803. // The DMA counter has a bias of one and can only be 16 bit long.
  1804. //
  1805. count = (count + 1) & 0xFFFF;
  1806. //
  1807. // If this is a 16 bit dma the multiply the count by 2.
  1808. //
  1809. if (AdapterObject->Width16Bits) {
  1810. count *= 2;
  1811. }
  1812. return (count);
  1813. }
  1814. ULONG
  1815. HalpGetIsaIrqState(
  1816. ULONG Vector
  1817. )
  1818. {
  1819. ULONG vectorState = CM_RESOURCE_INTERRUPT_LATCHED;
  1820. if (HalpBusType == MACHINE_TYPE_EISA) {
  1821. if (HalpEisaIrqMask & (1 << Vector)) {
  1822. vectorState = CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE;
  1823. }
  1824. }
  1825. return vectorState;
  1826. }