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.

445 lines
12 KiB

  1. /*++
  2. Copyright (c) 1989, 1990, 1991 Microsoft Corporation
  3. Module Name:
  4. devctx.c
  5. Abstract:
  6. This module contains code which implements the DEVICE_CONTEXT object.
  7. Routines are provided to reference, and dereference transport device
  8. context objects. Currently, there is no need to create them or destroy
  9. them, as this is handled at configuration time. If it is later required
  10. to dynamically load/unload the transport provider's device object and
  11. associated context, then we can add the create and destroy functions.
  12. The transport device context object is a structure which contains a
  13. system-defined DEVICE_OBJECT followed by information which is maintained
  14. by the transport provider, called the context.
  15. Author:
  16. David Beaver (dbeaver) 1 -July 1991
  17. Environment:
  18. Kernel mode
  19. Revision History:
  20. --*/
  21. #include "precomp.h"
  22. #pragma hdrstop
  23. #ifdef ALLOC_PRAGMA
  24. #pragma alloc_text(PAGE,NbfCreateDeviceContext)
  25. #endif
  26. VOID
  27. NbfRefDeviceContext(
  28. IN PDEVICE_CONTEXT DeviceContext
  29. )
  30. /*++
  31. Routine Description:
  32. This routine increments the reference count on a device context.
  33. Arguments:
  34. DeviceContext - Pointer to a transport device context object.
  35. Return Value:
  36. none.
  37. --*/
  38. {
  39. IF_NBFDBG (NBF_DEBUG_DEVCTX) {
  40. NbfPrint0 ("NbfRefDeviceContext: Entered.\n");
  41. }
  42. ASSERT (DeviceContext->ReferenceCount >= 0); // not perfect, but...
  43. (VOID)InterlockedIncrement (&DeviceContext->ReferenceCount);
  44. } /* NbfRefDeviceContext */
  45. VOID
  46. NbfDerefDeviceContext(
  47. IN PDEVICE_CONTEXT DeviceContext
  48. )
  49. /*++
  50. Routine Description:
  51. This routine dereferences a device context by decrementing the
  52. reference count contained in the structure. Currently, we don't
  53. do anything special when the reference count drops to zero, but
  54. we could dynamically unload stuff then.
  55. Arguments:
  56. DeviceContext - Pointer to a transport device context object.
  57. Return Value:
  58. none.
  59. --*/
  60. {
  61. LONG result;
  62. IF_NBFDBG (NBF_DEBUG_DEVCTX) {
  63. NbfPrint0 ("NbfDerefDeviceContext: Entered.\n");
  64. }
  65. result = InterlockedDecrement (&DeviceContext->ReferenceCount);
  66. ASSERT (result >= 0);
  67. if (result == 0) {
  68. NbfDestroyDeviceContext (DeviceContext);
  69. }
  70. } /* NbfDerefDeviceContext */
  71. NTSTATUS
  72. NbfCreateDeviceContext(
  73. IN PDRIVER_OBJECT DriverObject,
  74. IN PUNICODE_STRING DeviceName,
  75. IN OUT PDEVICE_CONTEXT *DeviceContext
  76. )
  77. /*++
  78. Routine Description:
  79. This routine creates and initializes a device context structure.
  80. Arguments:
  81. DriverObject - pointer to the IO subsystem supplied driver object.
  82. DeviceContext - Pointer to a pointer to a transport device context object.
  83. DeviceName - pointer to the name of the device this device object points to.
  84. Return Value:
  85. STATUS_SUCCESS if all is well; STATUS_INSUFFICIENT_RESOURCES otherwise.
  86. --*/
  87. {
  88. NTSTATUS status;
  89. PDEVICE_OBJECT deviceObject;
  90. PDEVICE_CONTEXT deviceContext;
  91. USHORT i;
  92. //
  93. // Create the device object for NETBEUI.
  94. //
  95. status = IoCreateDevice(
  96. DriverObject,
  97. sizeof (DEVICE_CONTEXT) - sizeof (DEVICE_OBJECT) +
  98. (DeviceName->Length + sizeof(UNICODE_NULL)),
  99. DeviceName,
  100. FILE_DEVICE_TRANSPORT,
  101. FILE_DEVICE_SECURE_OPEN,
  102. FALSE,
  103. &deviceObject);
  104. if (!NT_SUCCESS(status)) {
  105. return status;
  106. }
  107. deviceObject->Flags |= DO_DIRECT_IO;
  108. deviceContext = (PDEVICE_CONTEXT)deviceObject;
  109. //
  110. // Initialize our part of the device context.
  111. //
  112. RtlZeroMemory(
  113. ((PUCHAR)deviceContext) + sizeof(DEVICE_OBJECT),
  114. sizeof(DEVICE_CONTEXT) - sizeof(DEVICE_OBJECT));
  115. //
  116. // Copy over the device name.
  117. //
  118. deviceContext->DeviceNameLength = DeviceName->Length + sizeof(WCHAR);
  119. deviceContext->DeviceName = (PWCHAR)(deviceContext+1);
  120. RtlCopyMemory(
  121. deviceContext->DeviceName,
  122. DeviceName->Buffer,
  123. DeviceName->Length);
  124. deviceContext->DeviceName[DeviceName->Length/sizeof(WCHAR)] = UNICODE_NULL;
  125. //
  126. // Initialize device context fields.
  127. //
  128. deviceContext->NetmanVariables = NULL; // no variables yet.
  129. //
  130. // Initialize the reference count.
  131. //
  132. deviceContext->ReferenceCount = 1;
  133. #if DBG
  134. {
  135. UINT Counter;
  136. for (Counter = 0; Counter < NUMBER_OF_DCREFS; Counter++) {
  137. deviceContext->RefTypes[Counter] = 0;
  138. }
  139. // This reference is removed by the caller.
  140. deviceContext->RefTypes[DCREF_CREATION] = 1;
  141. }
  142. #endif
  143. deviceContext->CreateRefRemoved = FALSE;
  144. //
  145. // initialize the various fields in the device context
  146. //
  147. InitializeListHead(&deviceContext->Linkage);
  148. KeInitializeSpinLock (&deviceContext->Interlock);
  149. KeInitializeSpinLock (&deviceContext->SpinLock);
  150. KeInitializeSpinLock (&deviceContext->LinkSpinLock);
  151. KeInitializeSpinLock (&deviceContext->TimerSpinLock);
  152. KeInitializeSpinLock (&deviceContext->LoopbackSpinLock);
  153. KeInitializeSpinLock (&deviceContext->SendPoolListLock);
  154. KeInitializeSpinLock (&deviceContext->RcvPoolListLock);
  155. deviceContext->LinkTreeRoot = NULL;
  156. deviceContext->LastLink = NULL;
  157. deviceContext->LinkTreeElements = 0;
  158. deviceContext->LoopbackLinks[0] = NULL;
  159. deviceContext->LoopbackLinks[1] = NULL;
  160. deviceContext->LoopbackInProgress = FALSE;
  161. KeInitializeDpc(
  162. &deviceContext->LoopbackDpc,
  163. NbfProcessLoopbackQueue,
  164. (PVOID)deviceContext
  165. );
  166. deviceContext->WanThreadQueued = FALSE;
  167. ExInitializeWorkItem(
  168. &deviceContext->WanDelayedQueueItem,
  169. NbfProcessWanDelayedQueue,
  170. (PVOID)deviceContext);
  171. deviceContext->UniqueIdentifier = 1;
  172. deviceContext->ControlChannelIdentifier = 1;
  173. InitializeListHead (&deviceContext->ConnectionPool);
  174. InitializeListHead (&deviceContext->AddressPool);
  175. InitializeListHead (&deviceContext->AddressFilePool);
  176. InitializeListHead (&deviceContext->AddressDatabase);
  177. InitializeListHead (&deviceContext->LinkPool);
  178. InitializeListHead (&deviceContext->LinkDeferred);
  179. InitializeListHead (&deviceContext->PacketWaitQueue);
  180. InitializeListHead (&deviceContext->PacketizeQueue);
  181. InitializeListHead (&deviceContext->DataAckQueue);
  182. InitializeListHead (&deviceContext->DeferredRrQueue);
  183. InitializeListHead (&deviceContext->RequestPool);
  184. InitializeListHead (&deviceContext->UIFramePool);
  185. deviceContext->PacketPool.Next = NULL;
  186. deviceContext->RrPacketPool.Next = NULL;
  187. deviceContext->ReceivePacketPool.Next = NULL;
  188. deviceContext->ReceiveBufferPool.Next = NULL;
  189. InitializeListHead (&deviceContext->ReceiveInProgress);
  190. InitializeListHead (&deviceContext->ShortList);
  191. InitializeListHead (&deviceContext->LongList);
  192. InitializeListHead (&deviceContext->PurgeList);
  193. InitializeListHead (&deviceContext->LoopbackQueue);
  194. InitializeListHead (&deviceContext->FindNameQueue);
  195. InitializeListHead (&deviceContext->StatusQueryQueue);
  196. InitializeListHead (&deviceContext->IrpCompletionQueue);
  197. InitializeListHead (&deviceContext->QueryIndicationQueue);
  198. InitializeListHead (&deviceContext->DatagramIndicationQueue);
  199. deviceContext->IndicationQueuesInUse = FALSE;
  200. //
  201. // Equivalent to setting ShortListActive, DataAckQueueActive,
  202. // and LinkDeferredActive to FALSE.
  203. //
  204. deviceContext->a.AnyActive = 0;
  205. deviceContext->ProcessingShortTimer = FALSE;
  206. deviceContext->DataAckQueueChanged = FALSE;
  207. deviceContext->StalledConnectionCount = (USHORT)0;
  208. deviceContext->EasilyDisconnected = FALSE;
  209. //
  210. // Initialize provider statistics.
  211. //
  212. deviceContext->Statistics.Version = 0x0100;
  213. #if 0
  214. deviceContext->Information.Version = 2;
  215. deviceContext->Information.MaxTsduSize = NBF_MAX_TSDU_SIZE;
  216. deviceContext->Information.MaxDatagramSize = NBF_MAX_DATAGRAM_SIZE;
  217. deviceContext->Information.MaxConnectionUserData = NBF_MAX_CONNECTION_USER_DATA;
  218. deviceContext->Information.ServiceFlags = NBF_SERVICE_FLAGS;
  219. deviceContext->Information.TransmittedTsdus = 0;
  220. deviceContext->Information.ReceivedTsdus = 0;
  221. deviceContext->Information.TransmissionErrors = 0;
  222. deviceContext->Information.ReceiveErrors = 0;
  223. deviceContext->Information.MinimumLookaheadData = NBF_MIN_LOOKAHEAD_DATA;
  224. deviceContext->Information.MaximumLookaheadData = NBF_MAX_LOOKAHEAD_DATA;
  225. deviceContext->Information.DiscardedFrames = 0;
  226. deviceContext->Information.OversizeTsdusReceived = 0;
  227. deviceContext->Information.UndersizeTsdusReceived = 0;
  228. deviceContext->Information.MulticastTsdusReceived = 0;
  229. deviceContext->Information.BroadcastTsdusReceived = 0;
  230. deviceContext->Information.MulticastTsdusTransmitted = 0;
  231. deviceContext->Information.BroadcastTsdusTransmitted = 0;
  232. deviceContext->Information.SendTimeouts = 0;
  233. deviceContext->Information.ReceiveTimeouts = 0;
  234. deviceContext->Information.ConnectionIndicationsReceived = 0;
  235. deviceContext->Information.ConnectionIndicationsAccepted = 0;
  236. deviceContext->Information.ConnectionsInitiated = 0;
  237. deviceContext->Information.ConnectionsAccepted = 0;
  238. #endif
  239. deviceContext->State = DEVICECONTEXT_STATE_OPENING;
  240. //
  241. // Loopback buffer is not allocated.
  242. //
  243. deviceContext->LookaheadContiguous = NULL;
  244. //
  245. // Initialize the resource that guards address ACLs.
  246. //
  247. ExInitializeResourceLite (&deviceContext->AddressResource);
  248. //
  249. // No LSNs are in use.
  250. //
  251. for (i=0; i<(NETBIOS_SESSION_LIMIT+1); i++) {
  252. deviceContext->LsnTable[i] = 0;
  253. }
  254. deviceContext->NextLsnStart = 1;
  255. //
  256. // No addresses are in use.
  257. //
  258. for (i=0; i<256; i++) {
  259. deviceContext->AddressCounts[i] = 0;
  260. }
  261. //
  262. // No timers in use at present
  263. //
  264. INITIALIZE_TIMER_STATE(deviceContext);
  265. //
  266. // set the netbios multicast address for this network type
  267. //
  268. for (i=0; i<HARDWARE_ADDRESS_LENGTH; i++) {
  269. deviceContext->LocalAddress.Address [i] = 0; // set later
  270. deviceContext->NetBIOSAddress.Address [i] = 0;
  271. }
  272. deviceContext->Type = NBF_DEVICE_CONTEXT_SIGNATURE;
  273. deviceContext->Size = sizeof (DEVICE_CONTEXT);
  274. *DeviceContext = deviceContext;
  275. return STATUS_SUCCESS;
  276. }
  277. VOID
  278. NbfDestroyDeviceContext(
  279. IN PDEVICE_CONTEXT DeviceContext
  280. )
  281. /*++
  282. Routine Description:
  283. This routine destroys a device context structure.
  284. Arguments:
  285. DeviceContext - Pointer to a pointer to a transport device context object.
  286. Return Value:
  287. None.
  288. --*/
  289. {
  290. KIRQL oldIrql;
  291. ACQUIRE_DEVICES_LIST_LOCK();
  292. // Is ref count zero - or did a new rebind happen now
  293. // See rebind happen in NbfReInitializeDeviceContext
  294. if (DeviceContext->ReferenceCount != 0)
  295. {
  296. // A rebind happened while we waited for the lock
  297. RELEASE_DEVICES_LIST_LOCK();
  298. return;
  299. }
  300. // Splice this adapter of the list of device contexts
  301. RemoveEntryList (&DeviceContext->Linkage);
  302. RELEASE_DEVICES_LIST_LOCK();
  303. // Mark the adapter as going away to prevent activity
  304. DeviceContext->State = DEVICECONTEXT_STATE_STOPPING;
  305. // Free the packet pools, etc. and close the adapter.
  306. NbfCloseNdis (DeviceContext);
  307. // Remove all the storage associated with the device.
  308. NbfFreeResources (DeviceContext);
  309. // Cleanup any kernel resources
  310. ExDeleteResourceLite (&DeviceContext->AddressResource);
  311. // Delete device from IO space
  312. IoDeleteDevice ((PDEVICE_OBJECT)DeviceContext);
  313. return;
  314. }