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.

333 lines
9.5 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. PNDIS_WRAPPER_HANDLE WrapperHandle;
  45. ULONG cbSize;
  46. UNREFERENCED_PARAMETER (SystemSpecific3);
  47. #if TRACK_UNLOAD
  48. DbgPrint("NdisInitializeWrapper: DriverObject %p\n",SystemSpecific1);
  49. #endif
  50. DBGPRINT_RAW(DBG_COMP_INIT, DBG_LEVEL_INFO,
  51. ("==>NdisInitializeWrapper\n"));
  52. *NdisWrapperHandle = NULL;
  53. cbSize = sizeof(NDIS_WRAPPER_HANDLE) + ((PUNICODE_STRING)SystemSpecific2)->Length + sizeof(WCHAR);
  54. WrapperHandle = (PNDIS_WRAPPER_HANDLE)ALLOC_FROM_POOL(cbSize, NDIS_TAG_WRAPPER_HANDLE);
  55. if (WrapperHandle != NULL)
  56. {
  57. *NdisWrapperHandle = WrapperHandle;
  58. NdisZeroMemory(WrapperHandle, cbSize);
  59. WrapperHandle->DriverObject = (PDRIVER_OBJECT)SystemSpecific1;
  60. WrapperHandle->ServiceRegPath.Buffer = (PWSTR)((PUCHAR)WrapperHandle + sizeof(NDIS_WRAPPER_HANDLE));
  61. WrapperHandle->ServiceRegPath.Length = ((PUNICODE_STRING)SystemSpecific2)->Length;
  62. WrapperHandle->ServiceRegPath.MaximumLength = WrapperHandle->ServiceRegPath.Length + sizeof(WCHAR);
  63. NdisMoveMemory(WrapperHandle->ServiceRegPath.Buffer,
  64. ((PUNICODE_STRING)SystemSpecific2)->Buffer,
  65. WrapperHandle->ServiceRegPath.Length);
  66. }
  67. DBGPRINT_RAW(DBG_COMP_INIT, DBG_LEVEL_INFO,
  68. ("<==NdisInitializeWrapper\n"));
  69. }
  70. VOID
  71. NdisTerminateWrapper(
  72. IN NDIS_HANDLE NdisWrapperHandle,
  73. IN PVOID SystemSpecific
  74. )
  75. /*++
  76. Routine Description:
  77. Called at the end of every MAC's termination routine.
  78. Arguments:
  79. NdisWrapperHandle - The handle returned from NdisInitializeWrapper.
  80. SystemSpecific - No defined value.
  81. Return Value:
  82. None.
  83. --*/
  84. {
  85. PNDIS_WRAPPER_HANDLE WrapperHandle = (PNDIS_WRAPPER_HANDLE)NdisWrapperHandle;
  86. PNDIS_M_DRIVER_BLOCK MiniBlock;
  87. #if TRACK_UNLOAD
  88. DbgPrint("NdisTerminateWrapper: WrapperHandle %p\n",WrapperHandle);
  89. #endif
  90. DBGPRINT_RAW(DBG_COMP_INIT, DBG_LEVEL_INFO,
  91. ("==>NdisTerminateWrapper: NdisWrapperHandle %p\n", NdisWrapperHandle));
  92. UNREFERENCED_PARAMETER(SystemSpecific);
  93. if ((WrapperHandle != NULL) && (WrapperHandle->DriverObject != NULL))
  94. {
  95. #if TRACK_UNLOAD
  96. DbgPrint("NdisTerminateWrapper: DriverObject %p\n",WrapperHandle->DriverObject);
  97. #endif
  98. MiniBlock = (PNDIS_M_DRIVER_BLOCK)IoGetDriverObjectExtension(WrapperHandle->DriverObject,
  99. (PVOID)NDIS_PNP_MINIPORT_DRIVER_ID);
  100. if (MiniBlock != NULL)
  101. {
  102. #if TRACK_UNLOAD
  103. DbgPrint("NdisTerminateWrapper: MiniBlock %p\n",MiniBlock);
  104. #endif
  105. MiniBlock->Flags |= fMINIBLOCK_RECEIVED_TERMINATE_WRAPPER;
  106. //
  107. // Miniports should not be terminating the wrapper unless they are failing DriverEntry
  108. //
  109. if ((MiniBlock->MiniportQueue != NULL) || (MiniBlock->Flags & fMINIBLOCK_UNLOADING))
  110. {
  111. DBGPRINT_RAW(DBG_COMP_INIT, DBG_LEVEL_INFO,
  112. ("<==NdisTerminateWrapper\n"));
  113. return;
  114. }
  115. DBGPRINT_RAW(DBG_COMP_INIT, DBG_LEVEL_INFO,
  116. ("NdisTerminateWrapper: MiniBlock %p\n", MiniBlock));
  117. //
  118. // if the driver is going to fail DriverEntry, we expect it to have enough sense
  119. // to undo what it is done so far and not to wait for UnloadHandler
  120. //
  121. MiniBlock->UnloadHandler = NULL;
  122. MiniBlock->Flags |= fMINIBLOCK_TERMINATE_WRAPPER_UNLOAD;
  123. //
  124. // call unload entry point since PnP is not going to do it
  125. //
  126. ndisMUnload(WrapperHandle->DriverObject);
  127. }
  128. else
  129. {
  130. FREE_POOL(WrapperHandle);
  131. }
  132. }
  133. DBGPRINT_RAW(DBG_COMP_INIT, DBG_LEVEL_INFO,
  134. ("<==NdisTerminateWrapper\n"));
  135. }
  136. VOID
  137. NdisSetupDmaTransfer(
  138. OUT PNDIS_STATUS Status,
  139. IN NDIS_HANDLE NdisDmaHandle,
  140. IN PNDIS_BUFFER Buffer,
  141. IN ULONG Offset,
  142. IN ULONG Length,
  143. IN BOOLEAN WriteToDevice
  144. )
  145. /*++
  146. Routine Description:
  147. Sets up the host DMA controller for a DMA transfer. The
  148. DMA controller is set up to transfer the specified MDL.
  149. Since we register all DMA channels as non-scatter/gather,
  150. IoMapTransfer will ensure that the entire MDL is
  151. in a single logical piece for transfer.
  152. Arguments:
  153. Status - Returns the status of the request.
  154. NdisDmaHandle - Handle returned by NdisAllocateDmaChannel.
  155. Buffer - An NDIS_BUFFER which describes the host memory involved in the
  156. transfer.
  157. Offset - An offset within buffer where the transfer should
  158. start.
  159. Length - The length of the transfer. VirtualAddress plus Length must not
  160. extend beyond the end of the buffer.
  161. WriteToDevice - TRUE for a download operation (host to adapter); FALSE
  162. for an upload operation (adapter to host).
  163. Return Value:
  164. None.
  165. --*/
  166. {
  167. PNDIS_DMA_BLOCK DmaBlock = (PNDIS_DMA_BLOCK)NdisDmaHandle;
  168. PMAP_TRANSFER mapTransfer = *((PDMA_ADAPTER)DmaBlock->SystemAdapterObject)->DmaOperations->MapTransfer;
  169. PFLUSH_ADAPTER_BUFFERS flushAdapterBuffers = *((PDMA_ADAPTER)DmaBlock->SystemAdapterObject)->DmaOperations->FlushAdapterBuffers;
  170. ULONG LengthMapped;
  171. //
  172. // Make sure another request is not in progress.
  173. //
  174. if (DmaBlock->InProgress)
  175. {
  176. *Status = NDIS_STATUS_RESOURCES;
  177. return;
  178. }
  179. DmaBlock->InProgress = TRUE;
  180. //
  181. // Use IoMapTransfer to set up the transfer.
  182. //
  183. LengthMapped = Length;
  184. mapTransfer(DmaBlock->SystemAdapterObject,
  185. (PMDL)Buffer,
  186. DmaBlock->MapRegisterBase,
  187. (PUCHAR)(MDL_VA(Buffer)) + Offset,
  188. &LengthMapped,
  189. WriteToDevice);
  190. if (LengthMapped != Length)
  191. {
  192. //
  193. // Somehow the request could not be mapped competely,
  194. // this should not happen for a non-scatter/gather adapter.
  195. //
  196. flushAdapterBuffers(DmaBlock->SystemAdapterObject,
  197. (PMDL)Buffer,
  198. DmaBlock->MapRegisterBase,
  199. (PUCHAR)(MDL_VA(Buffer)) + Offset,
  200. LengthMapped,
  201. WriteToDevice);
  202. DmaBlock->InProgress = FALSE;
  203. *Status = NDIS_STATUS_RESOURCES;
  204. }
  205. else *Status = NDIS_STATUS_SUCCESS;
  206. }
  207. VOID
  208. NdisCompleteDmaTransfer(
  209. OUT PNDIS_STATUS Status,
  210. IN NDIS_HANDLE NdisDmaHandle,
  211. IN PNDIS_BUFFER Buffer,
  212. IN ULONG Offset,
  213. IN ULONG Length,
  214. IN BOOLEAN WriteToDevice
  215. )
  216. /*++
  217. Routine Description:
  218. Completes a previously started DMA transfer.
  219. Arguments:
  220. Status - Returns the status of the transfer.
  221. NdisDmaHandle - Handle returned by NdisAllocateDmaChannel.
  222. Buffer - An NDIS_BUFFER which was passed to NdisSetupDmaTransfer.
  223. Offset - the offset passed to NdisSetupDmaTransfer.
  224. Length - The length passed to NdisSetupDmaTransfer.
  225. WriteToDevice - TRUE for a download operation (host to adapter); FALSE
  226. for an upload operation (adapter to host).
  227. Return Value:
  228. None.
  229. --*/
  230. {
  231. PNDIS_DMA_BLOCK DmaBlock = (PNDIS_DMA_BLOCK)NdisDmaHandle;
  232. PFLUSH_ADAPTER_BUFFERS flushAdapterBuffers = *((PDMA_ADAPTER)DmaBlock->SystemAdapterObject)->DmaOperations->FlushAdapterBuffers;
  233. BOOLEAN Successful;
  234. Successful = flushAdapterBuffers(DmaBlock->SystemAdapterObject,
  235. (PMDL)Buffer,
  236. DmaBlock->MapRegisterBase,
  237. (PUCHAR)(MDL_VA(Buffer)) + Offset,
  238. Length,
  239. WriteToDevice);
  240. *Status = (Successful ? NDIS_STATUS_SUCCESS : NDIS_STATUS_RESOURCES);
  241. DmaBlock->InProgress = FALSE;
  242. }
  243. #pragma hdrstop