Leaked source code of windows server 2003
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.

1482 lines
36 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 "bootx86.h"
  16. #include "arc.h"
  17. #include "ixfwhal.h"
  18. #include "eisa.h"
  19. #include "ntconfig.h"
  20. extern PHARDWARE_PTE HalPT;
  21. PVOID HalpEisaControlBase;
  22. //
  23. // Define save area for ESIA adapter objects.
  24. //
  25. PADAPTER_OBJECT HalpEisaAdapter[8];
  26. VOID
  27. HalpCopyBufferMap(
  28. IN PMDL Mdl,
  29. IN PTRANSLATION_ENTRY TranslationEntry,
  30. IN PVOID CurrentVa,
  31. IN ULONG Length,
  32. IN BOOLEAN WriteToDevice
  33. );
  34. ULONG
  35. IoMapTransferMca(
  36. IN PADAPTER_OBJECT AdapterObject,
  37. IN PMDL Mdl,
  38. IN PVOID MapRegisterBase,
  39. IN PVOID CurrentVa,
  40. IN OUT PULONG Length,
  41. IN BOOLEAN WriteToDevice
  42. );
  43. VOID
  44. HalpCopyBufferMap(
  45. IN PMDL Mdl,
  46. IN PTRANSLATION_ENTRY TranslationEntry,
  47. IN PVOID CurrentVa,
  48. IN ULONG Length,
  49. IN BOOLEAN WriteToDevice
  50. )
  51. /*++
  52. Routine Description:
  53. This routine copies the speicific data between the user's buffer and the
  54. map register buffer. First a the user buffer is mapped if necessary, then
  55. the data is copied. Finally the user buffer will be unmapped if
  56. neccessary.
  57. Arguments:
  58. Mdl - Pointer to the MDL that describes the pages of memory that are
  59. being read or written.
  60. TranslationEntry - The address of the base map register that has been
  61. allocated to the device driver for use in mapping the transfer.
  62. CurrentVa - Current virtual address in the buffer described by the MDL
  63. that the transfer is being done to or from.
  64. Length - The length of the transfer. This determines the number of map
  65. registers that need to be written to map the transfer.
  66. WriteToDevice - Boolean value that indicates whether this is a write
  67. to the device from memory (TRUE), or vice versa.
  68. Return Value:
  69. None.
  70. --*/
  71. {
  72. PCCHAR bufferAddress;
  73. BOOLEAN mapped;
  74. //
  75. // Check to see if the buffer needs to be mapped.
  76. //
  77. if ((Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA) == 0) {
  78. //
  79. // Map the buffer into system space.
  80. //
  81. bufferAddress = MmGetMdlVirtualAddress(Mdl);
  82. mapped = TRUE;
  83. } else {
  84. bufferAddress = Mdl->MappedSystemVa;
  85. mapped = FALSE;
  86. }
  87. //
  88. // Calculate the actual start of the buffer based on the system VA and
  89. // the current VA.
  90. //
  91. bufferAddress += (PCCHAR) CurrentVa - (PCCHAR) MmGetMdlVirtualAddress(Mdl);
  92. //
  93. // Copy the data between the user buffer and map buffer
  94. //
  95. if (WriteToDevice) {
  96. RtlMoveMemory( TranslationEntry->VirtualAddress, bufferAddress, Length);
  97. } else {
  98. RtlMoveMemory(bufferAddress, TranslationEntry->VirtualAddress, Length);
  99. }
  100. }
  101. PADAPTER_OBJECT
  102. HalGetAdapter(
  103. IN PDEVICE_DESCRIPTION DeviceDescriptor,
  104. OUT PULONG NumberOfMapRegisters
  105. )
  106. /*++
  107. Routine Description:
  108. This function returns the appropriate adapter object for the device defined
  109. in the device description structure. This code works for Isa and Eisa
  110. systems.
  111. Arguments:
  112. DeviceDescriptor - Supplies a description of the deivce.
  113. NumberOfMapRegisters - Returns the maximum number of map registers which
  114. may be allocated by the device driver.
  115. Return Value:
  116. A pointer to the requested adpater object or NULL if an adapter could not
  117. be created.
  118. --*/
  119. {
  120. PADAPTER_OBJECT adapterObject;
  121. PVOID adapterBaseVa;
  122. ULONG channelNumber;
  123. ULONG controllerNumber;
  124. DMA_EXTENDED_MODE extendedMode;
  125. UCHAR adapterMode;
  126. ULONG numberOfMapRegisters;
  127. BOOLEAN useChannel;
  128. ULONG maximumLength;
  129. //
  130. // Determine if the the channel number is important. Master cards on
  131. // Eisa and Mca do not use a channel number.
  132. //
  133. if (DeviceDescriptor->InterfaceType != Isa &&
  134. DeviceDescriptor->Master) {
  135. useChannel = FALSE;
  136. } else {
  137. useChannel = TRUE;
  138. }
  139. //
  140. // Support for ISA local bus machines:
  141. // If the driver is a Master but really does not want a channel since it
  142. // is using the local bus DMA, just don't use an ISA channel.
  143. //
  144. if (DeviceDescriptor->InterfaceType == Isa &&
  145. DeviceDescriptor->DmaChannel > 7) {
  146. useChannel = FALSE;
  147. }
  148. //
  149. // Limit the maximum length to 2 GB this is done so that the BYTES_TO_PAGES
  150. // macro works correctly.
  151. //
  152. maximumLength = DeviceDescriptor->MaximumLength & 0x7fffffff;
  153. //
  154. // Channel 4 cannot be used since it is used for chaining. Return null if
  155. // it is requested.
  156. //
  157. if (DeviceDescriptor->DmaChannel == 4 && useChannel &&
  158. DeviceDescriptor->InterfaceType != MicroChannel) {
  159. return(NULL);
  160. }
  161. //
  162. // Determine the number of map registers for this device.
  163. //
  164. if (DeviceDescriptor->ScatterGather && DeviceDescriptor->InterfaceType == Eisa) {
  165. //
  166. // Since the device support scatter/Gather then map registers are not
  167. // required.
  168. //
  169. numberOfMapRegisters = 0;
  170. } else {
  171. //
  172. // Determine the number of map registers required based on the maximum
  173. // transfer length, up to a maximum number.
  174. //
  175. numberOfMapRegisters = BYTES_TO_PAGES(maximumLength)
  176. + 1;
  177. numberOfMapRegisters = numberOfMapRegisters > MAXIMUM_ISA_MAP_REGISTER ?
  178. MAXIMUM_ISA_MAP_REGISTER : numberOfMapRegisters;
  179. }
  180. //
  181. // Set the channel number number.
  182. //
  183. channelNumber = DeviceDescriptor->DmaChannel & 0x03;
  184. //
  185. // Set the adapter base address to the Base address register and controller
  186. // number.
  187. //
  188. if (!(DeviceDescriptor->DmaChannel & 0x04)) {
  189. controllerNumber = 1;
  190. adapterBaseVa = (PVOID) &((PEISA_CONTROL) HalpEisaControlBase)->Dma1BasePort;
  191. } else {
  192. controllerNumber = 2;
  193. adapterBaseVa = &((PEISA_CONTROL) HalpEisaControlBase)->Dma2BasePort;
  194. }
  195. //
  196. // Determine if a new adapter object is necessary. If so then allocate it.
  197. //
  198. if (useChannel && HalpEisaAdapter[DeviceDescriptor->DmaChannel] != NULL) {
  199. adapterObject = HalpEisaAdapter[DeviceDescriptor->DmaChannel];
  200. } else {
  201. //
  202. // Allocate an adapter object.
  203. //
  204. adapterObject = (PADAPTER_OBJECT) IopAllocateAdapter(
  205. numberOfMapRegisters,
  206. adapterBaseVa,
  207. NULL
  208. );
  209. if (adapterObject == NULL) {
  210. return(NULL);
  211. }
  212. if (useChannel) {
  213. HalpEisaAdapter[DeviceDescriptor->DmaChannel] = adapterObject;
  214. }
  215. //
  216. // We never need map registers.
  217. //
  218. adapterObject->NeedsMapRegisters = FALSE;
  219. //
  220. // Set the maximum number of map registers for this channel bus on
  221. // the number requested and the type of device.
  222. //
  223. if (numberOfMapRegisters) {
  224. //
  225. // The speicified number of registers are actually allowed to be
  226. // allocated.
  227. //
  228. adapterObject->MapRegistersPerChannel = numberOfMapRegisters;
  229. } else {
  230. //
  231. // No real map registers were allocated. If this is a master
  232. // device, then the device can have as may registers as it wants.
  233. //
  234. if (DeviceDescriptor->Master) {
  235. adapterObject->MapRegistersPerChannel = BYTES_TO_PAGES(
  236. maximumLength
  237. )
  238. + 1;
  239. } else {
  240. //
  241. // The device only gets one register. It must call
  242. // IoMapTransfer repeatedly to do a large transfer.
  243. //
  244. adapterObject->MapRegistersPerChannel = 1;
  245. }
  246. }
  247. }
  248. *NumberOfMapRegisters = adapterObject->MapRegistersPerChannel;
  249. //
  250. // If the channel number is not used then we are finished. The rest of
  251. // the work deals with channels.
  252. //
  253. if (!useChannel) {
  254. return(adapterObject);
  255. }
  256. //
  257. // Setup the pointers to all the random registers.
  258. //
  259. adapterObject->ChannelNumber = (UCHAR) channelNumber;
  260. if (controllerNumber == 1) {
  261. switch ((UCHAR)channelNumber) {
  262. case 0:
  263. adapterObject->PagePort = (PUCHAR) &((PDMA_PAGE) 0)->Channel0;
  264. break;
  265. case 1:
  266. adapterObject->PagePort = (PUCHAR) &((PDMA_PAGE) 0)->Channel1;
  267. break;
  268. case 2:
  269. adapterObject->PagePort = (PUCHAR) &((PDMA_PAGE) 0)->Channel2;
  270. break;
  271. case 3:
  272. adapterObject->PagePort = (PUCHAR) &((PDMA_PAGE) 0)->Channel3;
  273. break;
  274. }
  275. //
  276. // Set the adapter number.
  277. //
  278. adapterObject->AdapterNumber = 1;
  279. //
  280. // Save the extended mode register address.
  281. //
  282. adapterBaseVa =
  283. &((PEISA_CONTROL) HalpEisaControlBase)->Dma1ExtendedModePort;
  284. } else {
  285. switch (channelNumber) {
  286. case 1:
  287. adapterObject->PagePort = (PUCHAR) &((PDMA_PAGE) 0)->Channel5;
  288. break;
  289. case 2:
  290. adapterObject->PagePort = (PUCHAR) &((PDMA_PAGE) 0)->Channel6;
  291. break;
  292. case 3:
  293. adapterObject->PagePort = (PUCHAR) &((PDMA_PAGE) 0)->Channel7;
  294. break;
  295. }
  296. //
  297. // Set the adapter number.
  298. //
  299. adapterObject->AdapterNumber = 2;
  300. //
  301. // Save the extended mode register address.
  302. //
  303. adapterBaseVa =
  304. &((PEISA_CONTROL) HalpEisaControlBase)->Dma2ExtendedModePort;
  305. }
  306. adapterObject->Width16Bits = FALSE;
  307. if (MachineType == MACHINE_TYPE_EISA) {
  308. //
  309. // Initialzie the extended mode port.
  310. //
  311. *((PUCHAR) &extendedMode) = 0;
  312. extendedMode.ChannelNumber = (UCHAR)channelNumber;
  313. switch (DeviceDescriptor->DmaSpeed) {
  314. case Compatible:
  315. extendedMode.TimingMode = COMPATIBLITY_TIMING;
  316. break;
  317. case TypeA:
  318. extendedMode.TimingMode = TYPE_A_TIMING;
  319. break;
  320. case TypeB:
  321. extendedMode.TimingMode = TYPE_B_TIMING;
  322. break;
  323. case TypeC:
  324. extendedMode.TimingMode = BURST_TIMING;
  325. break;
  326. default:
  327. return(NULL);
  328. }
  329. switch (DeviceDescriptor->DmaWidth) {
  330. case Width8Bits:
  331. extendedMode.TransferSize = BY_BYTE_8_BITS;
  332. break;
  333. case Width16Bits:
  334. extendedMode.TransferSize = BY_BYTE_16_BITS;
  335. //
  336. // Note Width16bits should not be set here because there is no need
  337. // to shift the address and the transfer count.
  338. //
  339. break;
  340. case Width32Bits:
  341. extendedMode.TransferSize = BY_BYTE_32_BITS;
  342. break;
  343. default:
  344. return(NULL);
  345. }
  346. WRITE_PORT_UCHAR( adapterBaseVa, *((PUCHAR) &extendedMode));
  347. } else if (!DeviceDescriptor->Master) {
  348. switch (DeviceDescriptor->DmaWidth) {
  349. case Width8Bits:
  350. //
  351. // The channel must use controller 1.
  352. //
  353. if (controllerNumber != 1) {
  354. return(NULL);
  355. }
  356. break;
  357. case Width16Bits:
  358. //
  359. // The channel must use controller 2.
  360. //
  361. if (controllerNumber != 2) {
  362. return(NULL);
  363. }
  364. adapterObject->Width16Bits = TRUE;
  365. break;
  366. default:
  367. return(NULL);
  368. }
  369. }
  370. //
  371. // Determine if this is an Isa adapter.
  372. //
  373. if (DeviceDescriptor->InterfaceType == Isa) {
  374. adapterObject->IsaDevice = TRUE;
  375. }
  376. //
  377. // Initialize the adapter mode register value to the correct parameters,
  378. // and save them in the adapter object.
  379. //
  380. adapterMode = 0;
  381. ((PDMA_EISA_MODE) &adapterMode)->Channel = adapterObject->ChannelNumber;
  382. adapterObject->MasterDevice = FALSE;
  383. if (DeviceDescriptor->Master) {
  384. adapterObject->MasterDevice = TRUE;
  385. ((PDMA_EISA_MODE) &adapterMode)->RequestMode = CASCADE_REQUEST_MODE;
  386. //
  387. // Set the mode, and enable the request.
  388. //
  389. if (adapterObject->AdapterNumber == 1) {
  390. //
  391. // This request is for DMA controller 1
  392. //
  393. PDMA1_CONTROL dmaControl;
  394. dmaControl = adapterObject->AdapterBaseVa;
  395. WRITE_PORT_UCHAR( &dmaControl->Mode, adapterMode );
  396. //
  397. // Unmask the DMA channel.
  398. //
  399. WRITE_PORT_UCHAR(
  400. &dmaControl->SingleMask,
  401. (UCHAR) (DMA_CLEARMASK | adapterObject->ChannelNumber)
  402. );
  403. } else {
  404. //
  405. // This request is for DMA controller 1
  406. //
  407. PDMA2_CONTROL dmaControl;
  408. dmaControl = adapterObject->AdapterBaseVa;
  409. WRITE_PORT_UCHAR( &dmaControl->Mode, adapterMode );
  410. //
  411. // Unmask the DMA channel.
  412. //
  413. WRITE_PORT_UCHAR(
  414. &dmaControl->SingleMask,
  415. (UCHAR) (DMA_CLEARMASK | adapterObject->ChannelNumber)
  416. );
  417. }
  418. } else if (DeviceDescriptor->DemandMode) {
  419. ((PDMA_EISA_MODE) &adapterMode)->RequestMode = DEMAND_REQUEST_MODE;
  420. } else {
  421. ((PDMA_EISA_MODE) &adapterMode)->RequestMode = SINGLE_REQUEST_MODE;
  422. }
  423. if (DeviceDescriptor->AutoInitialize) {
  424. ((PDMA_EISA_MODE) &adapterMode)->AutoInitialize = 1;
  425. }
  426. adapterObject->AdapterMode = adapterMode;
  427. return(adapterObject);
  428. }
  429. NTSTATUS
  430. IoAllocateAdapterChannel(
  431. IN PADAPTER_OBJECT AdapterObject,
  432. IN PDEVICE_OBJECT DeviceObject,
  433. IN ULONG NumberOfMapRegisters,
  434. IN PDRIVER_CONTROL ExecutionRoutine,
  435. IN PVOID Context
  436. )
  437. /*++
  438. Routine Description:
  439. This routine allocates the adapter channel specified by the adapter object.
  440. This is accomplished by placing the device object of the driver that wants
  441. to allocate the adapter on the adapter's queue. If the queue is already
  442. "busy", then the adapter has already been allocated, so the device object
  443. is simply placed onto the queue and waits until the adapter becomes free.
  444. Once the adapter becomes free (or if it already is), then the driver's
  445. execution routine is invoked.
  446. Also, a number of map registers may be allocated to the driver by specifying
  447. a non-zero value for NumberOfMapRegisters. If this is the case, then the
  448. base address of the allocated map registers in the adapter is also passed
  449. to the driver's execution routine.
  450. Arguments:
  451. AdapterObject - Pointer to the adapter control object to allocate to the
  452. driver.
  453. DeviceObject - Pointer to the driver's device object that represents the
  454. device allocating the adapter.
  455. NumberOfMapRegisters - The number of map registers that are to be allocated
  456. from the channel, if any.
  457. ExecutionRoutine - The address of the driver's execution routine that is
  458. invoked once the adapter channel (and possibly map registers) have been
  459. allocated.
  460. Context - An untyped longword context parameter passed to the driver's
  461. execution routine.
  462. Return Value:
  463. Returns STATUS_SUCESS unless too many map registers are requested.
  464. Notes:
  465. Note that this routine MUST be invoked at DISPATCH_LEVEL or above.
  466. --*/
  467. {
  468. IO_ALLOCATION_ACTION action;
  469. //
  470. // Make sure the adapter if free.
  471. //
  472. if (AdapterObject->AdapterInUse) {
  473. DbgPrint("IoAllocateAdapterChannel: Called while adapter in use.\n");
  474. }
  475. //
  476. // Make sure there are enough map registers.
  477. //
  478. if (NumberOfMapRegisters > AdapterObject->MapRegistersPerChannel) {
  479. DbgPrint("IoAllocateAdapterChannel: Out of map registers.\n");
  480. return(STATUS_INSUFFICIENT_RESOURCES);
  481. }
  482. action = ExecutionRoutine( DeviceObject,
  483. DeviceObject->CurrentIrp,
  484. AdapterObject->MapRegisterBase,
  485. Context );
  486. //
  487. // If the driver wishes to keep the map registers then
  488. // increment the current base and decrease the number of existing map
  489. // registers.
  490. //
  491. if (action == DeallocateObjectKeepRegisters &&
  492. AdapterObject->MapRegisterBase != NULL) {
  493. AdapterObject->MapRegistersPerChannel -= NumberOfMapRegisters;
  494. AdapterObject->MapRegisterBase = (PTRANSLATION_ENTRY) AdapterObject->MapRegisterBase + NumberOfMapRegisters;
  495. } else if (action == KeepObject) {
  496. AdapterObject->AdapterInUse = TRUE;
  497. }
  498. return(STATUS_SUCCESS);
  499. }
  500. VOID
  501. IoFreeAdapterChannel(
  502. IN PADAPTER_OBJECT AdapterObject
  503. )
  504. /*++
  505. Routine Description:
  506. This routine is invoked to deallocate the specified adapter object.
  507. Any map registers that were allocated are also automatically deallocated.
  508. No checks are made to ensure that the adapter is really allocated to
  509. a device object. However, if it is not, then kernel will bugcheck.
  510. If another device is waiting in the queue to allocate the adapter object
  511. it will be pulled from the queue and its execution routine will be
  512. invoked.
  513. Arguments:
  514. AdapterObject - Pointer to the adapter object to be deallocated.
  515. Return Value:
  516. None.
  517. --*/
  518. {
  519. AdapterObject->AdapterInUse = FALSE;
  520. }
  521. PHYSICAL_ADDRESS
  522. IoMapTransfer(
  523. IN PADAPTER_OBJECT AdapterObject,
  524. IN PMDL Mdl,
  525. IN PVOID MapRegisterBase,
  526. IN PVOID CurrentVa,
  527. IN OUT PULONG Length,
  528. IN BOOLEAN WriteToDevice
  529. )
  530. /*++
  531. Routine Description:
  532. This routine is invoked to set up the map registers in the DMA controller
  533. to allow a transfer to or from a device.
  534. Arguments:
  535. AdapterObject - Pointer to the adapter object representing the DMA
  536. controller channel that has been allocated.
  537. Mdl - Pointer to the MDL that describes the pages of memory that are
  538. being read or written.
  539. MapRegisterBase - The address of the base map register that has been
  540. allocated to the device driver for use in mapping the transfer.
  541. CurrentVa - Current virtual address in the buffer described by the MDL
  542. that the transfer is being done to or from.
  543. Length - Supplies the length of the transfer. This determines the
  544. number of map registers that need to be written to map the transfer.
  545. Returns the length of the transfer which was actually mapped.
  546. WriteToDevice - Boolean value that indicates whether this is a write
  547. to the device from memory (TRUE), or vice versa.
  548. Return Value:
  549. Returns the logical address that should be used bus master controllers.
  550. --*/
  551. {
  552. BOOLEAN useBuffer;
  553. ULONG transferLength;
  554. ULONG logicalAddress;
  555. PULONG pageFrame;
  556. PUCHAR bytePointer;
  557. UCHAR adapterMode;
  558. UCHAR dataByte;
  559. PTRANSLATION_ENTRY translationEntry;
  560. BOOLEAN masterDevice;
  561. PHYSICAL_ADDRESS ReturnAddress;
  562. masterDevice = AdapterObject == NULL || AdapterObject->MasterDevice ?
  563. TRUE : FALSE;
  564. translationEntry = MapRegisterBase;
  565. transferLength = *Length;
  566. //
  567. // Determine if the data transfer needs to use the map buffer.
  568. //
  569. if (translationEntry && !masterDevice &&
  570. ADDRESS_AND_SIZE_TO_SPAN_PAGES(CurrentVa, transferLength) > 1) {
  571. logicalAddress = translationEntry->PhysicalAddress;
  572. useBuffer = TRUE;
  573. } else {
  574. //
  575. // The transfer can only be done for one page.
  576. //
  577. transferLength = PAGE_SIZE - BYTE_OFFSET(CurrentVa);
  578. pageFrame = (PULONG)(Mdl+1);
  579. pageFrame += ((ULONG) CurrentVa - (ULONG) Mdl->StartVa) / PAGE_SIZE;
  580. logicalAddress = (*pageFrame << PAGE_SHIFT) + BYTE_OFFSET(CurrentVa);
  581. //
  582. // If the buffer is contigous and does not cross a 64 K boundary then
  583. // just extend the buffer.
  584. //
  585. while( transferLength < *Length ){
  586. if (*pageFrame + 1 != *(pageFrame + 1) ||
  587. (*pageFrame & ~0x0ffff) != (*(pageFrame + 1) & ~0x0ffff)) {
  588. break;
  589. }
  590. transferLength += PAGE_SIZE;
  591. pageFrame++;
  592. }
  593. transferLength = transferLength > *Length ? *Length : transferLength;
  594. useBuffer = FALSE;
  595. }
  596. //
  597. // Check to see if this device has any map registers allocated. If it
  598. // does, then it must require memory to be at less than 16 MB. If the
  599. // logical address is greater than 16MB then map registers must be used
  600. //
  601. if (translationEntry && logicalAddress >= MAXIMUM_PHYSICAL_ADDRESS) {
  602. logicalAddress = (translationEntry + translationEntry->Index)->
  603. PhysicalAddress;
  604. useBuffer = TRUE;
  605. }
  606. //
  607. // Return the length.
  608. //
  609. *Length = transferLength;
  610. //
  611. // Copy the data if necessary.
  612. //
  613. if (useBuffer && WriteToDevice) {
  614. HalpCopyBufferMap(
  615. Mdl,
  616. translationEntry + translationEntry->Index,
  617. CurrentVa,
  618. *Length,
  619. WriteToDevice
  620. );
  621. }
  622. //
  623. // If there are map registers, then update the index to indicate
  624. // how many have been used.
  625. //
  626. if (translationEntry) {
  627. translationEntry->Index += ADDRESS_AND_SIZE_TO_SPAN_PAGES(
  628. CurrentVa,
  629. transferLength
  630. );
  631. }
  632. //
  633. // If no adapter was specificed then there is no more work to do so
  634. // return.
  635. //
  636. if (masterDevice) {
  637. //
  638. // We only support 32 bits, but the return is 64. Just
  639. // zero extend
  640. //
  641. ReturnAddress.QuadPart = logicalAddress;
  642. return(ReturnAddress);
  643. }
  644. //
  645. // Determine the mode based on the transfer direction.
  646. //
  647. adapterMode = AdapterObject->AdapterMode;
  648. ((PDMA_EISA_MODE) &adapterMode)->TransferType = (UCHAR) (WriteToDevice ?
  649. WRITE_TRANSFER : READ_TRANSFER);
  650. ReturnAddress.QuadPart = logicalAddress;
  651. bytePointer = (PUCHAR) &logicalAddress;
  652. if (AdapterObject->Width16Bits) {
  653. //
  654. // If this is a 16 bit transfer then adjust the length and the address
  655. // for the 16 bit DMA mode.
  656. //
  657. transferLength >>= 1;
  658. //
  659. // In 16 bit DMA mode the low 16 bits are shifted right one and the
  660. // page register value is unchanged. So save the page register value
  661. // and shift the logical address then restore the page value.
  662. //
  663. dataByte = bytePointer[2];
  664. logicalAddress >>= 1;
  665. bytePointer[2] = dataByte;
  666. }
  667. //
  668. // Determine the controller number based on the Adapter number.
  669. //
  670. if (AdapterObject->AdapterNumber == 1) {
  671. //
  672. // This request is for DMA controller 1
  673. //
  674. PDMA1_CONTROL dmaControl;
  675. dmaControl = AdapterObject->AdapterBaseVa;
  676. WRITE_PORT_UCHAR( &dmaControl->ClearBytePointer, 0 );
  677. WRITE_PORT_UCHAR( &dmaControl->Mode, adapterMode );
  678. WRITE_PORT_UCHAR(
  679. &dmaControl->DmaAddressCount[AdapterObject->ChannelNumber]
  680. .DmaBaseAddress,
  681. bytePointer[0]
  682. );
  683. WRITE_PORT_UCHAR(
  684. &dmaControl->DmaAddressCount[AdapterObject->ChannelNumber]
  685. .DmaBaseAddress,
  686. bytePointer[1]
  687. );
  688. WRITE_PORT_UCHAR(
  689. ((PUCHAR) &((PEISA_CONTROL) HalpEisaControlBase)->DmaPageLowPort) +
  690. (ULONG)AdapterObject->PagePort,
  691. bytePointer[2]
  692. );
  693. #if 0
  694. //
  695. // Write the high page register with zero value. This enable a special mode
  696. // which allows ties the page register and base count into a single 24 bit
  697. // address register.
  698. //
  699. WRITE_PORT_UCHAR(
  700. ((PUCHAR) &((PEISA_CONTROL) HalpEisaControlBase)->DmaPageHighPort) +
  701. (ULONG)AdapterObject->PagePort,
  702. 0
  703. );
  704. #endif
  705. //
  706. // Notify DMA chip of the length to transfer.
  707. //
  708. WRITE_PORT_UCHAR(
  709. &dmaControl->DmaAddressCount[AdapterObject->ChannelNumber]
  710. .DmaBaseCount,
  711. (UCHAR) ((transferLength - 1) & 0xff)
  712. );
  713. WRITE_PORT_UCHAR(
  714. &dmaControl->DmaAddressCount[AdapterObject->ChannelNumber]
  715. .DmaBaseCount,
  716. (UCHAR) ((transferLength - 1) >> 8)
  717. );
  718. //
  719. // Set the DMA chip to read or write mode; and unmask it.
  720. //
  721. WRITE_PORT_UCHAR(
  722. &dmaControl->SingleMask,
  723. (UCHAR) (DMA_CLEARMASK | AdapterObject->ChannelNumber)
  724. );
  725. } else {
  726. //
  727. // This request is for DMA controller 2
  728. //
  729. PDMA2_CONTROL dmaControl;
  730. dmaControl = AdapterObject->AdapterBaseVa;
  731. WRITE_PORT_UCHAR( &dmaControl->ClearBytePointer, 0 );
  732. WRITE_PORT_UCHAR( &dmaControl->Mode, adapterMode );
  733. WRITE_PORT_UCHAR(
  734. &dmaControl->DmaAddressCount[AdapterObject->ChannelNumber]
  735. .DmaBaseAddress,
  736. bytePointer[0]
  737. );
  738. WRITE_PORT_UCHAR(
  739. &dmaControl->DmaAddressCount[AdapterObject->ChannelNumber]
  740. .DmaBaseAddress,
  741. bytePointer[1]
  742. );
  743. WRITE_PORT_UCHAR(
  744. ((PUCHAR) &((PEISA_CONTROL) HalpEisaControlBase)->DmaPageLowPort) +
  745. (ULONG)AdapterObject->PagePort,
  746. bytePointer[2]
  747. );
  748. #if 0
  749. //
  750. // Write the high page register with zero value. This enable a special mode
  751. // which allows ties the page register and base count into a single 24 bit
  752. // address register.
  753. //
  754. WRITE_PORT_UCHAR(
  755. ((PUCHAR) &((PEISA_CONTROL) HalpEisaControlBase)->DmaPageHighPort) +
  756. (ULONG)AdapterObject->PagePort,
  757. 0
  758. );
  759. #endif
  760. //
  761. // Notify DMA chip of the length to transfer.
  762. //
  763. WRITE_PORT_UCHAR(
  764. &dmaControl->DmaAddressCount[AdapterObject->ChannelNumber]
  765. .DmaBaseCount,
  766. (UCHAR) ((transferLength - 1) & 0xff)
  767. );
  768. WRITE_PORT_UCHAR(
  769. &dmaControl->DmaAddressCount[AdapterObject->ChannelNumber]
  770. .DmaBaseCount,
  771. (UCHAR) ((transferLength - 1) >> 8)
  772. );
  773. //
  774. // Set the DMA chip to read or write mode; and unmask it.
  775. //
  776. WRITE_PORT_UCHAR(
  777. &dmaControl->SingleMask,
  778. (UCHAR) (DMA_CLEARMASK | AdapterObject->ChannelNumber)
  779. );
  780. }
  781. return(ReturnAddress);
  782. }
  783. BOOLEAN
  784. IoFlushAdapterBuffers(
  785. IN PADAPTER_OBJECT AdapterObject,
  786. IN PMDL Mdl,
  787. IN PVOID MapRegisterBase,
  788. IN PVOID CurrentVa,
  789. IN ULONG Length,
  790. IN BOOLEAN WriteToDevice
  791. )
  792. /*++
  793. Routine Description:
  794. This routine flushes the DMA adpater object buffers. For the Jazz system
  795. its clears the enable flag which aborts the dma.
  796. Arguments:
  797. AdapterObject - Pointer to the adapter object representing the DMA
  798. controller channel.
  799. Mdl - A pointer to a Memory Descriptor List (MDL) that maps the locked-down
  800. buffer to/from which the I/O occured.
  801. MapRegisterBase - A pointer to the base of the map registers in the adapter
  802. or DMA controller.
  803. CurrentVa - The current virtual address in the buffer described the the Mdl
  804. where the I/O operation occurred.
  805. Length - Supplies the length of the transfer.
  806. WriteToDevice - Supplies a BOOLEAN value that indicates the direction of
  807. the data transfer was to the device.
  808. Return Value:
  809. TRUE - No errors are detected so the transfer must succeed.
  810. --*/
  811. {
  812. PTRANSLATION_ENTRY translationEntry;
  813. PULONG pageFrame;
  814. ULONG transferLength;
  815. ULONG partialLength;
  816. BOOLEAN masterDevice;
  817. BOOLEAN mapped = FALSE;
  818. masterDevice = AdapterObject == NULL || AdapterObject->MasterDevice ?
  819. TRUE : FALSE;
  820. translationEntry = MapRegisterBase;
  821. //
  822. // Clear the index of used buffers.
  823. //
  824. if (translationEntry) {
  825. translationEntry->Index = 0;
  826. }
  827. //
  828. // Determine if the data needs to be copied to the orginal buffer.
  829. // This only occurs if the data tranfer is from the device, the
  830. // MapReisterBase is not NULL and the transfer spans a page.
  831. //
  832. if (!WriteToDevice && translationEntry) {
  833. //
  834. // If this is not a master device, then just transfer the buffer.
  835. //
  836. if (ADDRESS_AND_SIZE_TO_SPAN_PAGES(CurrentVa, Length) > 1 &&
  837. !masterDevice) {
  838. HalpCopyBufferMap(
  839. Mdl,
  840. translationEntry,
  841. CurrentVa,
  842. Length,
  843. WriteToDevice
  844. );
  845. } else {
  846. //
  847. // Cycle through the pages of the transfer to determine if there
  848. // are any which need to be copied back.
  849. //
  850. transferLength = PAGE_SIZE - BYTE_OFFSET(CurrentVa);
  851. partialLength = transferLength;
  852. pageFrame = (PULONG)(Mdl+1);
  853. pageFrame += ((ULONG) CurrentVa - (ULONG) Mdl->StartVa) / PAGE_SIZE;
  854. while( transferLength <= Length ){
  855. if (*pageFrame >= BYTES_TO_PAGES(MAXIMUM_PHYSICAL_ADDRESS)) {
  856. //
  857. // Check to see that the MDL is mapped in system space.
  858. // If is not mapped, then map it. This ensures that the
  859. // buffer will only have to be mapped at most once per I/O.
  860. //
  861. if ((Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA) == 0) {
  862. Mdl->MappedSystemVa = MmGetMdlVirtualAddress(Mdl);
  863. Mdl->MdlFlags |= MDL_MAPPED_TO_SYSTEM_VA;
  864. mapped = TRUE;
  865. }
  866. HalpCopyBufferMap(
  867. Mdl,
  868. translationEntry,
  869. CurrentVa,
  870. partialLength,
  871. WriteToDevice
  872. );
  873. }
  874. CurrentVa = (PCCHAR) CurrentVa + partialLength;
  875. partialLength = PAGE_SIZE;
  876. //
  877. // Note that transferLength indicates the amount which will be
  878. // transfered after the next loop; thus, it is updated with the
  879. // new partial length.
  880. //
  881. transferLength += partialLength;
  882. pageFrame++;
  883. translationEntry++;
  884. }
  885. //
  886. // Process the any remaining residue.
  887. //
  888. partialLength = Length - transferLength + partialLength;
  889. if (partialLength && *pageFrame >= BYTES_TO_PAGES(MAXIMUM_PHYSICAL_ADDRESS)) {
  890. HalpCopyBufferMap(
  891. Mdl,
  892. translationEntry,
  893. CurrentVa,
  894. partialLength,
  895. WriteToDevice
  896. );
  897. }
  898. }
  899. }
  900. //
  901. // If this is a master device, then there is nothing more to do so return
  902. // TRUE.
  903. //
  904. if (masterDevice) {
  905. return(TRUE);
  906. }
  907. //
  908. // Mask the DMA request line so that DMA requests cannot occur.
  909. //
  910. if (AdapterObject->AdapterNumber == 1) {
  911. //
  912. // This request is for DMA controller 1
  913. //
  914. PDMA1_CONTROL dmaControl;
  915. dmaControl = AdapterObject->AdapterBaseVa;
  916. WRITE_PORT_UCHAR(
  917. &dmaControl->SingleMask,
  918. (UCHAR) (DMA_SETMASK | AdapterObject->ChannelNumber)
  919. );
  920. } else {
  921. //
  922. // This request is for DMA controller 2
  923. //
  924. PDMA2_CONTROL dmaControl;
  925. dmaControl = AdapterObject->AdapterBaseVa;
  926. WRITE_PORT_UCHAR(
  927. &dmaControl->SingleMask,
  928. (UCHAR) (DMA_SETMASK | AdapterObject->ChannelNumber)
  929. );
  930. }
  931. return TRUE;
  932. }
  933. VOID
  934. IoFreeMapRegisters(
  935. PADAPTER_OBJECT AdapterObject,
  936. PVOID MapRegisterBase,
  937. ULONG NumberOfMapRegisters
  938. )
  939. /*++
  940. Routine Description:
  941. This routine deallocates the map registers for the adapter. If there are
  942. any queued adapter waiting for an attempt is made to allocate the next
  943. entry.
  944. Arguments:
  945. AdapterObject - The adapter object to where the map register should be
  946. returned.
  947. MapRegisterBase - The map register base of the registers to be deallocated.
  948. NumberOfMapRegisters - The number of registers to be deallocated.
  949. Return Value:
  950. None
  951. --+*/
  952. {
  953. PTRANSLATION_ENTRY translationEntry;
  954. //
  955. // Determine if this was the last allocation from the adapter. If is was
  956. // then free the map registers by restoring the map register base and the
  957. // channel count; otherwise the registers are lost. This handles the
  958. // normal case.
  959. //
  960. translationEntry = AdapterObject->MapRegisterBase;
  961. translationEntry -= NumberOfMapRegisters;
  962. if (translationEntry == MapRegisterBase) {
  963. //
  964. // The last allocated registers are being freed.
  965. //
  966. AdapterObject->MapRegisterBase = (PVOID) translationEntry;
  967. AdapterObject->MapRegistersPerChannel += NumberOfMapRegisters;
  968. }
  969. }
  970. PHYSICAL_ADDRESS
  971. MmGetPhysicalAddress (
  972. IN PVOID BaseAddress
  973. )
  974. /*++
  975. Routine Description:
  976. This function returns the corresponding physical address for a
  977. valid virtual address.
  978. Arguments:
  979. BaseAddress - Supplies the virtual address for which to return the
  980. physical address.
  981. Return Value:
  982. Returns the corresponding physical address.
  983. Environment:
  984. Kernel mode. Any IRQL level.
  985. --*/
  986. {
  987. PHYSICAL_ADDRESS PhysicalAddress;
  988. ULONG Index;
  989. PhysicalAddress.HighPart = 0;
  990. PhysicalAddress.LowPart = (ULONG)BaseAddress & ~KSEG0_BASE;
  991. //
  992. // If the address is in the hal map range, get the physical
  993. // addressed mapped by the pte
  994. //
  995. if (((ULONG) BaseAddress) >= 0xffc00000) {
  996. Index = (PhysicalAddress.LowPart >> 12) & 0x3ff;
  997. PhysicalAddress.LowPart = HalPT[Index].PageFrameNumber << PAGE_SHIFT;
  998. PhysicalAddress.LowPart |= ((ULONG)BaseAddress) & (PAGE_SIZE-1);
  999. }
  1000. return(PhysicalAddress);
  1001. }
  1002. PVOID
  1003. MmAllocateNonCachedMemory (
  1004. IN ULONG NumberOfBytes
  1005. )
  1006. /*++
  1007. Routine Description:
  1008. This function allocates a range of noncached memory in
  1009. the non-paged portion of the system address space.
  1010. This routine is designed to be used by a driver's initialization
  1011. routine to allocate a noncached block of virtual memory for
  1012. various device specific buffers.
  1013. Arguments:
  1014. NumberOfBytes - Supplies the number of bytes to allocate.
  1015. Return Value:
  1016. NULL - the specified request could not be satisfied.
  1017. NON-NULL - Returns a pointer (virtual address in the nonpaged portion
  1018. of the system) to the allocated phyiscally contiguous
  1019. memory.
  1020. Environment:
  1021. Kernel mode, IRQL of APC_LEVEL or below.
  1022. --*/
  1023. {
  1024. PVOID BaseAddress;
  1025. //
  1026. // Allocated the memory.
  1027. //
  1028. BaseAddress = FwAllocateHeap(NumberOfBytes);
  1029. return BaseAddress;
  1030. }