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.

334 lines
9.2 KiB

  1. /*++
  2. Copyright (c) 1990-1995 Microsoft Corporation
  3. Module Name:
  4. config.c
  5. Abstract:
  6. NDIS wrapper functions for full mac drivers configuration/initialization
  7. Author:
  8. Sean Selitrennikoff (SeanSe) 05-Oct-93
  9. Jameel Hyder (JameelH) 01-Jun-95 Re-organization/optimization
  10. Environment:
  11. Kernel mode, FSD
  12. Revision History:
  13. --*/
  14. #include <precomp.h>
  15. #include <stdarg.h>
  16. //
  17. // Define the module number for debug code.
  18. //
  19. #define MODULE_NUMBER MODULE_CONFIG
  20. //
  21. // Requests Used by MAC Drivers
  22. //
  23. //
  24. VOID
  25. NdisInitializeWrapper(
  26. OUT PNDIS_HANDLE NdisWrapperHandle,
  27. IN PVOID SystemSpecific1,
  28. IN PVOID SystemSpecific2,
  29. IN PVOID SystemSpecific3
  30. )
  31. /*++
  32. Routine Description:
  33. Called at the beginning of every MAC's initialization routine.
  34. Arguments:
  35. NdisWrapperHandle - A MAC specific handle for the wrapper.
  36. SystemSpecific1, a pointer to the driver object for the MAC.
  37. SystemSpecific2, a PUNICODE_STRING containing the location of
  38. the registry subtree for this driver.
  39. SystemSpecific3, unused on NT.
  40. Return Value:
  41. None.
  42. --*/
  43. {
  44. NDIS_STATUS Status;
  45. PNDIS_WRAPPER_HANDLE WrapperHandle;
  46. ULONG cbSize;
  47. UNREFERENCED_PARAMETER (SystemSpecific3);
  48. #if TRACK_UNLOAD
  49. DbgPrint("NdisInitializeWrapper: DriverObject %p\n",SystemSpecific1);
  50. #endif
  51. DBGPRINT_RAW(DBG_COMP_INIT, DBG_LEVEL_INFO,
  52. ("==>NdisInitializeWrapper\n"));
  53. *NdisWrapperHandle = NULL;
  54. cbSize = sizeof(NDIS_WRAPPER_HANDLE) + ((PUNICODE_STRING)SystemSpecific2)->Length + sizeof(WCHAR);
  55. WrapperHandle = (PNDIS_WRAPPER_HANDLE)ALLOC_FROM_POOL(cbSize, NDIS_TAG_WRAPPER_HANDLE);
  56. if (WrapperHandle != NULL)
  57. {
  58. *NdisWrapperHandle = WrapperHandle;
  59. NdisZeroMemory(WrapperHandle, cbSize);
  60. WrapperHandle->DriverObject = (PDRIVER_OBJECT)SystemSpecific1;
  61. WrapperHandle->ServiceRegPath.Buffer = (PWSTR)((PUCHAR)WrapperHandle + sizeof(NDIS_WRAPPER_HANDLE));
  62. WrapperHandle->ServiceRegPath.Length = ((PUNICODE_STRING)SystemSpecific2)->Length;
  63. WrapperHandle->ServiceRegPath.MaximumLength = WrapperHandle->ServiceRegPath.Length + sizeof(WCHAR);
  64. NdisMoveMemory(WrapperHandle->ServiceRegPath.Buffer,
  65. ((PUNICODE_STRING)SystemSpecific2)->Buffer,
  66. WrapperHandle->ServiceRegPath.Length);
  67. }
  68. DBGPRINT_RAW(DBG_COMP_INIT, DBG_LEVEL_INFO,
  69. ("<==NdisInitializeWrapper\n"));
  70. }
  71. VOID
  72. NdisTerminateWrapper(
  73. IN NDIS_HANDLE NdisWrapperHandle,
  74. IN PVOID SystemSpecific
  75. )
  76. /*++
  77. Routine Description:
  78. Called at the end of every MAC's termination routine.
  79. Arguments:
  80. NdisWrapperHandle - The handle returned from NdisInitializeWrapper.
  81. SystemSpecific - No defined value.
  82. Return Value:
  83. None.
  84. --*/
  85. {
  86. PNDIS_WRAPPER_HANDLE WrapperHandle = (PNDIS_WRAPPER_HANDLE)NdisWrapperHandle;
  87. PNDIS_M_DRIVER_BLOCK MiniBlock;
  88. #if TRACK_UNLOAD
  89. DbgPrint("NdisTerminateWrapper: WrapperHandle %p\n",WrapperHandle);
  90. #endif
  91. DBGPRINT_RAW(DBG_COMP_INIT, DBG_LEVEL_INFO,
  92. ("==>NdisTerminateWrapper: NdisWrapperHandle %p\n", NdisWrapperHandle));
  93. UNREFERENCED_PARAMETER(SystemSpecific);
  94. if ((WrapperHandle != NULL) && (WrapperHandle->DriverObject != NULL))
  95. {
  96. #if TRACK_UNLOAD
  97. DbgPrint("NdisTerminateWrapper: DriverObject %p\n",WrapperHandle->DriverObject);
  98. #endif
  99. MiniBlock = (PNDIS_M_DRIVER_BLOCK)IoGetDriverObjectExtension(WrapperHandle->DriverObject,
  100. (PVOID)NDIS_PNP_MINIPORT_DRIVER_ID);
  101. if (MiniBlock != NULL)
  102. {
  103. #if TRACK_UNLOAD
  104. DbgPrint("NdisTerminateWrapper: MiniBlock %p\n",MiniBlock);
  105. #endif
  106. MiniBlock->Flags |= fMINIBLOCK_RECEIVED_TERMINATE_WRAPPER;
  107. //
  108. // Miniports should not be terminating the wrapper unless they are failing DriverEntry
  109. //
  110. if ((MiniBlock->MiniportQueue != NULL) || (MiniBlock->Flags & fMINIBLOCK_UNLOADING))
  111. {
  112. DBGPRINT_RAW(DBG_COMP_INIT, DBG_LEVEL_INFO,
  113. ("<==NdisTerminateWrapper\n"));
  114. return;
  115. }
  116. DBGPRINT_RAW(DBG_COMP_INIT, DBG_LEVEL_INFO,
  117. ("NdisTerminateWrapper: MiniBlock %p\n", MiniBlock));
  118. //
  119. // if the driver is going to fail DriverEntry, we expect it to have enough sense
  120. // to undo what it is done so far and not to wait for UnloadHandler
  121. //
  122. MiniBlock->UnloadHandler = NULL;
  123. MiniBlock->Flags |= fMINIBLOCK_TERMINATE_WRAPPER_UNLOAD;
  124. //
  125. // call unload entry point since PnP is not going to do it
  126. //
  127. ndisMUnload(WrapperHandle->DriverObject);
  128. }
  129. else
  130. {
  131. FREE_POOL(WrapperHandle);
  132. }
  133. }
  134. DBGPRINT_RAW(DBG_COMP_INIT, DBG_LEVEL_INFO,
  135. ("<==NdisTerminateWrapper\n"));
  136. }
  137. VOID
  138. NdisSetupDmaTransfer(
  139. OUT PNDIS_STATUS Status,
  140. IN NDIS_HANDLE NdisDmaHandle,
  141. IN PNDIS_BUFFER Buffer,
  142. IN ULONG Offset,
  143. IN ULONG Length,
  144. IN BOOLEAN WriteToDevice
  145. )
  146. /*++
  147. Routine Description:
  148. Sets up the host DMA controller for a DMA transfer. The
  149. DMA controller is set up to transfer the specified MDL.
  150. Since we register all DMA channels as non-scatter/gather,
  151. IoMapTransfer will ensure that the entire MDL is
  152. in a single logical piece for transfer.
  153. Arguments:
  154. Status - Returns the status of the request.
  155. NdisDmaHandle - Handle returned by NdisAllocateDmaChannel.
  156. Buffer - An NDIS_BUFFER which describes the host memory involved in the
  157. transfer.
  158. Offset - An offset within buffer where the transfer should
  159. start.
  160. Length - The length of the transfer. VirtualAddress plus Length must not
  161. extend beyond the end of the buffer.
  162. WriteToDevice - TRUE for a download operation (host to adapter); FALSE
  163. for an upload operation (adapter to host).
  164. Return Value:
  165. None.
  166. --*/
  167. {
  168. PNDIS_DMA_BLOCK DmaBlock = (PNDIS_DMA_BLOCK)NdisDmaHandle;
  169. PMAP_TRANSFER mapTransfer = *((PDMA_ADAPTER)DmaBlock->SystemAdapterObject)->DmaOperations->MapTransfer;
  170. PFLUSH_ADAPTER_BUFFERS flushAdapterBuffers = *((PDMA_ADAPTER)DmaBlock->SystemAdapterObject)->DmaOperations->FlushAdapterBuffers;
  171. ULONG LengthMapped;
  172. //
  173. // Make sure another request is not in progress.
  174. //
  175. if (DmaBlock->InProgress)
  176. {
  177. *Status = NDIS_STATUS_RESOURCES;
  178. return;
  179. }
  180. DmaBlock->InProgress = TRUE;
  181. //
  182. // Use IoMapTransfer to set up the transfer.
  183. //
  184. LengthMapped = Length;
  185. mapTransfer(DmaBlock->SystemAdapterObject,
  186. (PMDL)Buffer,
  187. DmaBlock->MapRegisterBase,
  188. (PUCHAR)(MDL_VA(Buffer)) + Offset,
  189. &LengthMapped,
  190. WriteToDevice);
  191. if (LengthMapped != Length)
  192. {
  193. //
  194. // Somehow the request could not be mapped competely,
  195. // this should not happen for a non-scatter/gather adapter.
  196. //
  197. flushAdapterBuffers(DmaBlock->SystemAdapterObject,
  198. (PMDL)Buffer,
  199. DmaBlock->MapRegisterBase,
  200. (PUCHAR)(MDL_VA(Buffer)) + Offset,
  201. LengthMapped,
  202. WriteToDevice);
  203. DmaBlock->InProgress = FALSE;
  204. *Status = NDIS_STATUS_RESOURCES;
  205. }
  206. else *Status = NDIS_STATUS_SUCCESS;
  207. }
  208. VOID
  209. NdisCompleteDmaTransfer(
  210. OUT PNDIS_STATUS Status,
  211. IN NDIS_HANDLE NdisDmaHandle,
  212. IN PNDIS_BUFFER Buffer,
  213. IN ULONG Offset,
  214. IN ULONG Length,
  215. IN BOOLEAN WriteToDevice
  216. )
  217. /*++
  218. Routine Description:
  219. Completes a previously started DMA transfer.
  220. Arguments:
  221. Status - Returns the status of the transfer.
  222. NdisDmaHandle - Handle returned by NdisAllocateDmaChannel.
  223. Buffer - An NDIS_BUFFER which was passed to NdisSetupDmaTransfer.
  224. Offset - the offset passed to NdisSetupDmaTransfer.
  225. Length - The length passed to NdisSetupDmaTransfer.
  226. WriteToDevice - TRUE for a download operation (host to adapter); FALSE
  227. for an upload operation (adapter to host).
  228. Return Value:
  229. None.
  230. --*/
  231. {
  232. PNDIS_DMA_BLOCK DmaBlock = (PNDIS_DMA_BLOCK)NdisDmaHandle;
  233. PFLUSH_ADAPTER_BUFFERS flushAdapterBuffers = *((PDMA_ADAPTER)DmaBlock->SystemAdapterObject)->DmaOperations->FlushAdapterBuffers;
  234. BOOLEAN Successful;
  235. Successful = flushAdapterBuffers(DmaBlock->SystemAdapterObject,
  236. (PMDL)Buffer,
  237. DmaBlock->MapRegisterBase,
  238. (PUCHAR)(MDL_VA(Buffer)) + Offset,
  239. Length,
  240. WriteToDevice);
  241. *Status = (Successful ? NDIS_STATUS_SUCCESS : NDIS_STATUS_RESOURCES);
  242. DmaBlock->InProgress = FALSE;
  243. }
  244. #pragma hdrstop