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.

983 lines
21 KiB

  1. /*++
  2. Copyright (c) 1989-1993 Microsoft Corporation
  3. Module Name:
  4. adapter.c
  5. Abstract:
  6. This module contains code which implements the ADAPTER object.
  7. Routines are provided to reference, and dereference transport
  8. adapter objects.
  9. Environment:
  10. Kernel mode
  11. Revision History:
  12. --*/
  13. #include "precomp.h"
  14. #pragma hdrstop
  15. //
  16. // local function prototypes
  17. //
  18. VOID
  19. IpxDelayedFreeAdapter(
  20. IN PVOID Param
  21. );
  22. //********** Pageable Routine Declarations *****
  23. //************************* PAGEIPX **********************************
  24. #ifdef ALLOC_PRAGMA
  25. #pragma alloc_text(PAGEIPX, IpxDelayedFreeAdapter)
  26. #endif
  27. //********** Pageable Routine Declarations *****
  28. //
  29. // These are init only until binding is really dynamic.
  30. //
  31. //
  32. // [FW] So, later we can change this to pnp-compatible value
  33. //
  34. //
  35. // ULONG
  36. // ADAPTER_INDEX_TO_FWCONTEXT(
  37. // IN ULONG _adapterindex;
  38. // );
  39. //
  40. #define ADAPTER_INDEX_TO_FWCONTEXT(_adapterindex) _adapterindex
  41. VOID
  42. IpxRefBinding(
  43. IN PBINDING Binding
  44. )
  45. /*++
  46. Routine Description:
  47. This routine increments the reference count on a device context.
  48. Arguments:
  49. Binding - Pointer to a transport device context object.
  50. Return Value:
  51. none.
  52. --*/
  53. {
  54. CTEAssert (Binding->ReferenceCount > 0); // not perfect, but...
  55. (VOID)InterlockedIncrement (&Binding->ReferenceCount);
  56. } /* IpxRefBinding */
  57. VOID
  58. IpxDerefBinding(
  59. IN PBINDING Binding
  60. )
  61. /*++
  62. Routine Description:
  63. This routine dereferences a device context by decrementing the
  64. reference count contained in the structure. Currently, we don't
  65. do anything special when the reference count drops to zero, but
  66. we could dynamically unload stuff then.
  67. Arguments:
  68. Binding - Pointer to a transport device context object.
  69. Return Value:
  70. none.
  71. --*/
  72. {
  73. LONG result;
  74. result = InterlockedDecrement (&Binding->ReferenceCount);
  75. CTEAssert (result >= 0);
  76. if (result == 0) {
  77. IpxDestroyBinding (Binding);
  78. }
  79. } /* IpxDerefBinding */
  80. NTSTATUS
  81. IpxCreateAdapter(
  82. IN PDEVICE Device,
  83. IN PUNICODE_STRING AdapterName,
  84. IN OUT PADAPTER *AdapterPtr
  85. )
  86. /*++
  87. Routine Description:
  88. This routine creates and initializes a device context structure.
  89. Arguments:
  90. DriverObject - pointer to the IO subsystem supplied driver object.
  91. Adapter - Pointer to a pointer to a transport device context object.
  92. AdapterName - pointer to the name of the device this device object points to.
  93. Return Value:
  94. STATUS_SUCCESS if all is well; STATUS_INSUFFICIENT_RESOURCES otherwise.
  95. --*/
  96. {
  97. PADAPTER Adapter;
  98. #if 0
  99. UINT i, j;
  100. #endif
  101. Adapter = (PADAPTER)IpxAllocateMemory (sizeof(ADAPTER) + AdapterName->Length + sizeof(WCHAR), MEMORY_ADAPTER, "Adapter");
  102. if (Adapter == NULL) {
  103. if (KeGetCurrentIrql() == 0) {
  104. IPX_DEBUG (ADAPTER, ("Create adapter %ws failed\n", AdapterName));
  105. } else {
  106. IPX_DEBUG (ADAPTER, ("Create adapter %lx failed\n", AdapterName));
  107. }
  108. return STATUS_INSUFFICIENT_RESOURCES;
  109. }
  110. IPX_DEBUG (ADAPTER, ("Create adapter %lx %lx succeeded\n", Adapter, AdapterName));
  111. RtlZeroMemory(Adapter, sizeof(ADAPTER));
  112. //
  113. // Copy over the adapter name.
  114. //
  115. Adapter->AdapterNameLength = AdapterName->Length + sizeof(WCHAR);
  116. Adapter->AdapterName = (PWCHAR)(Adapter+1);
  117. RtlCopyMemory(
  118. Adapter->AdapterName,
  119. AdapterName->Buffer,
  120. AdapterName->Length);
  121. Adapter->AdapterName[AdapterName->Length/sizeof(WCHAR)] = UNICODE_NULL;
  122. #if DBG
  123. RtlCopyMemory(Adapter->Signature1, "IAD1", 4);
  124. #endif
  125. Adapter->Type = IPX_ADAPTER_SIGNATURE;
  126. Adapter->Size = sizeof(ADAPTER);
  127. CTEInitLock (&Adapter->Lock);
  128. InitializeListHead (&Adapter->RequestCompletionQueue);
  129. InitializeListHead (&Adapter->ReceiveBufferPoolList);
  130. ExInitializeSListHead (&Adapter->ReceiveBufferList);
  131. Adapter->Device = Device;
  132. Adapter->DeviceLock = &Device->Lock;
  133. IpxReferenceDevice (Device, DREF_ADAPTER);
  134. Adapter->Disabled = ENABLED; // used iu NDIS_MEDIA_SENSE ...
  135. #if 0
  136. Adapter->ReceiveBufferPool.Next = NULL;
  137. for (i = 0; i < ISN_FRAME_TYPE_MAX; i++) {
  138. Adapter->Bindings[i] = NULL;
  139. }
  140. Adapter->BindingCount = 0;
  141. for (i = 0; i < IDENTIFIER_TOTAL; i++) {
  142. for (j = 0; j < SOURCE_ROUTE_HASH_SIZE; j++) {
  143. Adapter->SourceRoutingHeads[i][j] = (PSOURCE_ROUTE)NULL;
  144. }
  145. }
  146. #endif
  147. //
  148. // For the moment, we have to do the source
  149. // routing operation on any type where broadcast
  150. // may not be used for discovery -- improve this
  151. // hopefully.
  152. //
  153. Adapter->SourceRoutingEmpty[IDENTIFIER_RIP] = FALSE;
  154. Adapter->SourceRoutingEmpty[IDENTIFIER_IPX] = FALSE;
  155. Adapter->SourceRoutingEmpty[IDENTIFIER_SPX] = FALSE;
  156. Adapter->SourceRoutingEmpty[IDENTIFIER_NB] = TRUE;
  157. //
  158. // Lock here? Added lock. [TC]
  159. //
  160. KeInitializeEvent(
  161. &Adapter->NdisEvent,
  162. NotificationEvent,
  163. FALSE
  164. );
  165. InterlockedExchange(&(Adapter->ReferenceCount),1);
  166. #if DBG
  167. InterlockedExchange(&(Adapter->RefTypes[ADAP_REF_CREATE]),1);
  168. #endif
  169. *AdapterPtr = Adapter;
  170. return STATUS_SUCCESS;
  171. } /* IpxCreateAdapter */
  172. VOID
  173. IpxDestroyAdapter(
  174. IN PADAPTER Adapter
  175. )
  176. /*++
  177. Routine Description:
  178. This routine destroys a device context structure.
  179. Arguments:
  180. Adapter - Pointer to a pointer to a transport device context object.
  181. Return Value:
  182. None.
  183. --*/
  184. {
  185. ULONG Database, Hash;
  186. PSOURCE_ROUTE Current;
  187. ULONG ReceiveBufferPoolSize;
  188. PIPX_RECEIVE_BUFFER ReceiveBuffer;
  189. PIPX_RECEIVE_BUFFER_POOL ReceiveBufferPool;
  190. PDEVICE Device = Adapter->Device;
  191. PLIST_ENTRY p;
  192. UINT i;
  193. PIPX_DELAYED_FREE_ITEM DelayedFreeItem;
  194. IPX_DEBUG (ADAPTER, ("Destroy adapter %lx\n", Adapter));
  195. //
  196. // Free any receive buffer pools this adapter has.
  197. //
  198. ReceiveBufferPoolSize = FIELD_OFFSET (IPX_RECEIVE_BUFFER_POOL, Buffers[0]) +
  199. (sizeof(IPX_RECEIVE_BUFFER) * Device->InitReceiveBuffers) +
  200. (Adapter->MaxReceivePacketSize * Device->InitReceiveBuffers);
  201. while (!IsListEmpty (&Adapter->ReceiveBufferPoolList)) {
  202. p = RemoveHeadList (&Adapter->ReceiveBufferPoolList);
  203. ReceiveBufferPool = CONTAINING_RECORD (p, IPX_RECEIVE_BUFFER_POOL, Linkage);
  204. for (i = 0; i < ReceiveBufferPool->BufferCount; i++) {
  205. ReceiveBuffer = &ReceiveBufferPool->Buffers[i];
  206. IpxDeinitializeReceiveBuffer (Adapter, ReceiveBuffer, Adapter->MaxReceivePacketSize);
  207. }
  208. IPX_DEBUG (PACKET, ("Free buffer pool %lx\n", ReceiveBufferPool));
  209. IpxFreeMemory (ReceiveBufferPool, ReceiveBufferPoolSize, MEMORY_PACKET, "ReceiveBufferPool");
  210. }
  211. //
  212. // Free all the source routing information for this adapter.
  213. //
  214. for (Database = 0; Database < IDENTIFIER_TOTAL; Database++) {
  215. for (Hash = 0; Hash < SOURCE_ROUTE_HASH_SIZE; Hash++) {
  216. while (Adapter->SourceRoutingHeads[Database][Hash]) {
  217. Current = Adapter->SourceRoutingHeads[Database][Hash];
  218. Adapter->SourceRoutingHeads[Database][Hash] = Current->Next;
  219. IpxFreeMemory (Current, SOURCE_ROUTE_SIZE (Current->SourceRoutingLength), MEMORY_SOURCE_ROUTE, "SourceRouting");
  220. }
  221. }
  222. }
  223. //
  224. // I am moving the following line to the workerthread, so that
  225. // the device can go away only after the worker thread completes. [MS]
  226. //
  227. // IpxDereferenceDevice (Adapter->Device, DREF_ADAPTER);
  228. //
  229. // Free the adapter on a delayed queue so that all
  230. // the threads inside this would have come out of it.
  231. // allocate a work item and queue it on a delayed queue.
  232. //
  233. DelayedFreeItem = (PIPX_DELAYED_FREE_ITEM)IpxAllocateMemory (
  234. sizeof(IPX_DELAYED_FREE_ITEM),
  235. MEMORY_WORK_ITEM,
  236. "Work Item");
  237. if ( DelayedFreeItem ) {
  238. DelayedFreeItem->Context = (PVOID)Adapter;
  239. DelayedFreeItem->ContextSize = sizeof(ADAPTER) + Adapter->AdapterNameLength;
  240. ExInitializeWorkItem(
  241. &DelayedFreeItem->WorkItem,
  242. IpxDelayedFreeAdapter,
  243. (PVOID)DelayedFreeItem);
  244. ExQueueWorkItem(
  245. &DelayedFreeItem->WorkItem,
  246. DelayedWorkQueue);
  247. } else {
  248. //
  249. // oh well, tough luck. Just delay this thread and then
  250. // destroy the adapter.
  251. //
  252. LARGE_INTEGER Delay;
  253. PDEVICE Device;
  254. Delay.QuadPart = -10*10000; // Ten second.
  255. Device = Adapter->Device;
  256. KeDelayExecutionThread(
  257. KernelMode,
  258. FALSE,
  259. &Delay);
  260. IpxFreeMemory (Adapter, sizeof(ADAPTER) + Adapter->AdapterNameLength, MEMORY_ADAPTER, "Adapter");
  261. // We need to dereference the adapter in the failure case as well. [TingCai]
  262. IpxDereferenceDevice (Device, DREF_ADAPTER);
  263. }
  264. } /* IpxDestroyAdapter */
  265. VOID
  266. IpxDelayedFreeAdapter(
  267. IN PVOID Param
  268. )
  269. /*++
  270. Routine Description:
  271. This routine frees an adapter on the delayed queue. We wait long enough
  272. before freeing an adapter to make sure that no threads are accessing it
  273. This allows us to access the Adapter without the use of spinlocks.
  274. Arguments:
  275. Param - pointer to the work item.
  276. Return Value:
  277. None.
  278. --*/
  279. {
  280. LARGE_INTEGER Delay;
  281. PIPX_DELAYED_FREE_ITEM DelayedFreeItem = (PIPX_DELAYED_FREE_ITEM) Param;
  282. PADAPTER Adapter;
  283. PDEVICE Device;
  284. Adapter = (PADAPTER) DelayedFreeItem->Context;
  285. // Keep a pointer, as we need it in IpxDereferenceDevice after we free the memory.[TC]
  286. Device = Adapter->Device;
  287. Delay.QuadPart = -10*10000; // Ten second.
  288. KeDelayExecutionThread(
  289. KernelMode,
  290. FALSE,
  291. &Delay);
  292. // IpxFreeMemory needs to access Device structure, so delay the following line
  293. // to the end of this function.
  294. // IpxDereferenceDevice (Adapter->Device, DREF_ADAPTER);
  295. IpxFreeMemory (
  296. DelayedFreeItem->Context,
  297. DelayedFreeItem->ContextSize,
  298. MEMORY_ADAPTER,
  299. "Adapter");
  300. IpxFreeMemory (
  301. DelayedFreeItem,
  302. sizeof (IPX_DELAYED_FREE_ITEM),
  303. MEMORY_WORK_ITEM,
  304. "Work Item");
  305. IpxDereferenceDevice (Device, DREF_ADAPTER);
  306. } /* IpxDelayedFreeAdapter */
  307. NTSTATUS
  308. IpxCreateBinding(
  309. IN PDEVICE Device,
  310. IN PBINDING_CONFIG ConfigBinding OPTIONAL,
  311. IN ULONG NetworkNumberIndex,
  312. IN PWCHAR AdapterName,
  313. IN OUT PBINDING *BindingPtr
  314. )
  315. /*++
  316. Routine Description:
  317. This routine creates and initializes a binding structure.
  318. Arguments:
  319. Device - The device.
  320. ConfigBinding - Information about this binding. If this is
  321. NULL then this is a WAN binding and all the relevant
  322. information will be filled in by the caller.
  323. NetworkNumberIndex - The index in the frame type array for
  324. ConfigBinding indicating which frame type this binding is for.
  325. Not used if ConfigBinding is not provided.
  326. AdapterName - Used for error logging.
  327. BindingPtr - Returns the allocated binding structure.
  328. Return Value:
  329. STATUS_SUCCESS if all is well; STATUS_INSUFFICIENT_RESOURCES otherwise.
  330. --*/
  331. {
  332. PBINDING Binding;
  333. PSINGLE_LIST_ENTRY s;
  334. s = IPX_POP_ENTRY_LIST(
  335. &Device->BindingList,
  336. &Device->SListsLock);
  337. if (s != NULL) {
  338. goto GotBinding;
  339. }
  340. //
  341. // This function tries to allocate another packet pool.
  342. //
  343. s = IpxPopBinding(Device);
  344. //
  345. // Possibly we should queue the packet up to wait
  346. // for one to become free.
  347. //
  348. if (s == NULL) {
  349. #if DBG
  350. if (KeGetCurrentIrql() == 0) {
  351. IPX_DEBUG (ADAPTER, ("Create binding %ws failed\n", AdapterName));
  352. } else {
  353. IPX_DEBUG (ADAPTER, ("Create binding WAN failed\n"));
  354. }
  355. #endif
  356. return STATUS_INSUFFICIENT_RESOURCES;
  357. }
  358. GotBinding:
  359. Binding = CONTAINING_RECORD (s, BINDING, PoolLinkage);
  360. #if DBG
  361. if (KeGetCurrentIrql() == 0) {
  362. IPX_DEBUG (ADAPTER, ("Create binding %ws succeeded, %lx\n", AdapterName, Binding));
  363. } else {
  364. IPX_DEBUG (ADAPTER, ("Create binding WAN succeeded\n"));
  365. }
  366. #endif
  367. RtlZeroMemory(Binding, sizeof(BINDING));
  368. //
  369. // Initialize the reference count.
  370. //
  371. Binding->ReferenceCount = 1;
  372. #if DBG
  373. Binding->RefTypes[BREF_BOUND] = 1;
  374. #endif
  375. #if DBG
  376. RtlCopyMemory(Binding->Signature1, "IBI1", 4);
  377. #endif
  378. Binding->Type = IPX_BINDING_SIGNATURE;
  379. Binding->Size = sizeof(BINDING);
  380. Binding->Device = Device;
  381. Binding->DeviceLock = &Device->Lock;
  382. if (ConfigBinding != NULL) {
  383. ULONG Temp = ConfigBinding->NetworkNumber[NetworkNumberIndex];
  384. Binding->ConfiguredNetworkNumber = REORDER_ULONG (Temp);
  385. Binding->AutoDetect = ConfigBinding->AutoDetect[NetworkNumberIndex];
  386. Binding->DefaultAutoDetect = ConfigBinding->DefaultAutoDetect[NetworkNumberIndex];
  387. Binding->AllRouteDirected = (BOOLEAN)ConfigBinding->Parameters[BINDING_ALL_ROUTE_DEF];
  388. Binding->AllRouteBroadcast = (BOOLEAN)ConfigBinding->Parameters[BINDING_ALL_ROUTE_BC];
  389. Binding->AllRouteMulticast = (BOOLEAN)ConfigBinding->Parameters[BINDING_ALL_ROUTE_MC];
  390. }
  391. Binding->ReceiveBroadcast = TRUE;
  392. IPX_DEBUG(ADAPTER, (" %x set to TRUE\n", Binding));
  393. #if 0
  394. Binding->BindingSetMember = FALSE;
  395. Binding->NextBinding = (PBINDING)NULL;
  396. Binding->DialOutAsync = FALSE;
  397. #endif
  398. Binding->TdiRegistrationHandle = NULL;
  399. Binding->fInfoIndicated = FALSE;
  400. Binding->PastAutoDetection = FALSE;
  401. //
  402. // We set Binding->FrameType later, after we can map it based on the
  403. // media type of the adapter we bind to.
  404. //
  405. *BindingPtr = Binding;
  406. return STATUS_SUCCESS;
  407. } /* IpxCreateBinding */
  408. VOID
  409. IpxDestroyBinding(
  410. IN PBINDING Binding
  411. )
  412. /*++
  413. Routine Description:
  414. This routine destroys a binding structure.
  415. Arguments:
  416. Binding - Pointer to a transport binding structure.
  417. Return Value:
  418. None.
  419. --*/
  420. {
  421. IPX_DEBUG (ADAPTER, ("Destroy binding %lx\n", Binding));
  422. IPX_PUSH_ENTRY_LIST(
  423. &IpxDevice->BindingList,
  424. &Binding->PoolLinkage,
  425. &IpxDevice->SListsLock);
  426. } /* IpxDestroyBinding */
  427. VOID
  428. IpxAllocateBindingPool(
  429. IN PDEVICE Device
  430. )
  431. /*++
  432. Routine Description:
  433. This routine adds 10 bindings to the pool for this device.
  434. Arguments:
  435. Device - The device.
  436. Return Value:
  437. None.
  438. --*/
  439. {
  440. PIPX_BINDING_POOL BindingPool;
  441. UINT BindingPoolSize;
  442. UINT BindingNum;
  443. PBINDING Binding;
  444. CTELockHandle LockHandle;
  445. BindingPoolSize = FIELD_OFFSET (IPX_BINDING_POOL, Bindings[0]) +
  446. (sizeof(BINDING) * Device->InitBindings);
  447. BindingPool = (PIPX_BINDING_POOL)IpxAllocateMemory (BindingPoolSize, MEMORY_PACKET, "BindingPool");
  448. if (BindingPool == NULL) {
  449. IPX_DEBUG (PNP, ("Could not allocate binding pool memory\n"));
  450. return;
  451. }
  452. IPX_DEBUG (PNP, ("Initializing Binding pool %lx, %d bindings\n",
  453. BindingPool, Device->InitBindings));
  454. BindingPool->BindingCount = Device->InitBindings;
  455. CTEGetLock (&Device->Lock, &LockHandle);
  456. for (BindingNum = 0; BindingNum < BindingPool->BindingCount; BindingNum++) {
  457. Binding = &BindingPool->Bindings[BindingNum];
  458. IPX_PUSH_ENTRY_LIST (&Device->BindingList, &Binding->PoolLinkage, &Device->SListsLock);
  459. #ifdef IPX_TRACK_POOL
  460. Binding->Pool = BindingPool;
  461. #endif
  462. }
  463. InsertTailList (&Device->BindingPoolList, &BindingPool->Linkage);
  464. Device->AllocatedBindings += BindingPool->BindingCount;
  465. CTEFreeLock (&Device->Lock, LockHandle);
  466. } /* IpxAllocateBindingPool */
  467. PSINGLE_LIST_ENTRY
  468. IpxPopBinding(
  469. PDEVICE Device
  470. )
  471. /*++
  472. Routine Description:
  473. This routine allocates a binding from the device context's pool.
  474. If there are no bindings in the pool, it allocates one up to
  475. the configured limit.
  476. Arguments:
  477. Device - Pointer to our device to charge the packet to.
  478. Return Value:
  479. The pointer to the Linkage field in the allocated binding.
  480. --*/
  481. {
  482. PSINGLE_LIST_ENTRY s;
  483. s = IPX_POP_ENTRY_LIST(
  484. &Device->BindingList,
  485. &Device->SListsLock);
  486. if (s != NULL) {
  487. return s;
  488. }
  489. //
  490. // No packets in the pool, see if we can allocate more.
  491. //
  492. if (Device->AllocatedBindings < Device->MaxPoolBindings) {
  493. //
  494. // Allocate a pool and try again.
  495. //
  496. IpxAllocateBindingPool (Device);
  497. s = IPX_POP_ENTRY_LIST(
  498. &Device->BindingList,
  499. &Device->SListsLock);
  500. return s;
  501. } else {
  502. return NULL;
  503. }
  504. } /* IpxPopBinding */
  505. //
  506. // [FW]
  507. //
  508. #ifdef SUNDOWN
  509. NTSTATUS
  510. IpxOpenAdapter(
  511. IN NIC_HANDLE AdapterIndex1,
  512. IN ULONG_PTR FwdAdapterContext,
  513. OUT PNIC_HANDLE IpxAdapterContext
  514. )
  515. #else
  516. NTSTATUS
  517. IpxOpenAdapter(
  518. IN NIC_HANDLE AdapterIndex1,
  519. IN ULONG FwdAdapterContext,
  520. OUT PNIC_HANDLE IpxAdapterContext
  521. )
  522. #endif
  523. /*++
  524. Routine Description:
  525. This routine is called by the Kernel Forwarder to open an adapter
  526. Arguments:
  527. AdapterIndex - index of the adapter to open (NICid for now - will change to a struct
  528. with a version number, signature and the NicId
  529. FwdAdapterContext - Forwarder's context
  530. IpxAdapterContext - our context (for now we use the NICid - for pnp will change
  531. this to contain a signature and version #)
  532. Return Value:
  533. STATUS_INVALID_HANDLE if the AdapterIndex handle was invalid
  534. STATUS_ADAPTER_ALREADY_OPENED if the Adapter is being opened a second time
  535. STATUS_SUCCESS
  536. --*/
  537. {
  538. PBINDING Binding;
  539. PDEVICE Device = IpxDevice;
  540. USHORT AdapterIndex = AdapterIndex1.NicId;
  541. IPX_DEBUG(ADAPTER, ("IPX: Entered IpxOpenAdapter\n"));
  542. //
  543. // Return error if the AdapterIndex is out of range.
  544. // We do indicate the slave bindings to NB/SPX (but not to RIP)
  545. // Hence, the index should be less than HighestExternalNicId (not ValidBindings)
  546. //
  547. if (AdapterIndex > Device->HighestExternalNicId) {
  548. return STATUS_INVALID_HANDLE;
  549. }
  550. //
  551. // Fill up our context to be returned to the Forwarder
  552. //
  553. NIC_HANDLE_FROM_NIC((*IpxAdapterContext), AdapterIndex);
  554. //
  555. // If AdapterIndex is 0, it is for the virtual net
  556. // Will the forwarder open this at all?
  557. //
  558. if (AdapterIndex == 0) {
  559. return STATUS_SUCCESS;
  560. }
  561. //
  562. // Get the binding pointer
  563. //
  564. Binding = NIC_ID_TO_BINDING(IpxDevice, AdapterIndex);
  565. if (Binding == NULL) {
  566. return STATUS_INVALID_HANDLE;
  567. }
  568. //
  569. // Return error if adapter is being opened a second time (or more times)
  570. //
  571. if (GET_LONG_VALUE(Binding->ReferenceCount) >= 2) {
  572. return STATUS_ADAPTER_ALREADY_OPENED;
  573. }
  574. //
  575. // Store the Forwarder's Adapter Context in the binding
  576. //
  577. Binding->FwdAdapterContext = FwdAdapterContext;
  578. //
  579. // Reference the Binding
  580. //
  581. IpxReferenceBinding(Binding, BREF_FWDOPEN);
  582. return STATUS_SUCCESS;
  583. }
  584. NTSTATUS
  585. IpxCloseAdapter(
  586. IN NIC_HANDLE IpxAdapterContext
  587. )
  588. /*++
  589. Routine Description:
  590. This routine is called by the Kernel Forwarder to close an adapter
  591. Arguments:
  592. IpxAdapterContext - our context (for now we use the NICid - for pnp will change
  593. this to contain a signature and version#)
  594. Return Value:
  595. STATUS_ADAPTER_ALREADY_CLOSED - if the adapter is being closed a second time
  596. STATUS_SUCCESS
  597. --*/
  598. {
  599. PBINDING Binding;
  600. IPX_DEBUG(ADAPTER, ("IPX: Entered IpxCloseAdapter\n"));
  601. Binding = NIC_ID_TO_BINDING(IpxDevice, IpxAdapterContext.NicId);
  602. if (Binding == NULL) {
  603. ASSERT(FALSE);
  604. return STATUS_UNSUCCESSFUL;
  605. }
  606. //
  607. // Either the adapter is around (count = 2)
  608. // or it went away (count = 1). The latter cannot happen now.
  609. //
  610. if (GET_LONG_VALUE(Binding->ReferenceCount) <= 1) {
  611. return STATUS_ADAPTER_ALREADY_CLOSED;
  612. }
  613. //
  614. // Dereference the Binding so it can be deleted
  615. //
  616. IpxDereferenceBinding(Binding, BREF_FWDOPEN);
  617. //
  618. // Clear the Forwarder's Adapter Context in the binding
  619. //
  620. Binding->FwdAdapterContext = 0;
  621. return STATUS_SUCCESS;
  622. }
  623. VOID
  624. IpxRefAdapter(
  625. IN PADAPTER Adapter
  626. )
  627. /*++
  628. Routine Description:
  629. This routine increments the reference count on a adapter context.
  630. Arguments:
  631. Adapter - Pointer to a transport adapter context object.
  632. Return Value:
  633. none.
  634. --*/
  635. {
  636. CTEAssert (Adapter->ReferenceCount > 0); // not perfect, but...
  637. (VOID)InterlockedIncrement(&Adapter->ReferenceCount);
  638. } /* IpxRefAdapter */
  639. VOID
  640. IpxDerefAdapter(
  641. IN PADAPTER Adapter
  642. )
  643. /*++
  644. Routine Description:
  645. This routine dereferences a adapter context by decrementing the
  646. reference count contained in the structure.
  647. Arguments:
  648. Adapter - Pointer to a transport adapter context object.
  649. Return Value:
  650. none.
  651. --*/
  652. {
  653. LONG result;
  654. result = InterlockedDecrement (&Adapter->ReferenceCount);
  655. CTEAssert (result >= 0);
  656. if (result == 0) {
  657. KeSetEvent(&Adapter->NdisEvent, 0L, FALSE);
  658. }
  659. } /* IpxDerefAdapter */