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.

1927 lines
53 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. ndisbind.c
  5. Abstract:
  6. NDIS protocol entry points and utility routines to handle binding
  7. and unbinding from adapters.
  8. Environment:
  9. Kernel mode only.
  10. Revision History:
  11. arvindm 4/5/2000 Created
  12. --*/
  13. #include "precomp.h"
  14. #define __FILENUMBER 'DNIB'
  15. VOID
  16. NdisProtBindAdapter(
  17. OUT PNDIS_STATUS pStatus,
  18. IN NDIS_HANDLE BindContext,
  19. IN PNDIS_STRING pDeviceName,
  20. IN PVOID SystemSpecific1,
  21. IN PVOID SystemSpecific2
  22. )
  23. /*++
  24. Routine Description:
  25. Protocol Bind Handler entry point called when NDIS wants us
  26. to bind to an adapter. We go ahead and set up a binding.
  27. An OPEN_CONTEXT structure is allocated to keep state about
  28. this binding.
  29. Arguments:
  30. pStatus - place to return bind status
  31. BindContext - handle to use with NdisCompleteBindAdapter
  32. DeviceName - adapter to bind to
  33. SystemSpecific1 - used to access protocol-specific registry
  34. key for this binding
  35. SystemSpecific2 - unused
  36. Return Value:
  37. None
  38. --*/
  39. {
  40. PNDISPROT_OPEN_CONTEXT pOpenContext;
  41. NDIS_STATUS Status, ConfigStatus;
  42. NDIS_HANDLE ConfigHandle;
  43. UNREFERENCED_PARAMETER(BindContext);
  44. UNREFERENCED_PARAMETER(SystemSpecific2);
  45. do
  46. {
  47. //
  48. // Allocate our context for this open.
  49. //
  50. NPROT_ALLOC_MEM(pOpenContext, sizeof(NDISPROT_OPEN_CONTEXT));
  51. if (pOpenContext == NULL)
  52. {
  53. Status = NDIS_STATUS_RESOURCES;
  54. break;
  55. }
  56. //
  57. // Initialize it.
  58. //
  59. NPROT_ZERO_MEM(pOpenContext, sizeof(NDISPROT_OPEN_CONTEXT));
  60. NPROT_SET_SIGNATURE(pOpenContext, oc);
  61. NPROT_INIT_LOCK(&pOpenContext->Lock);
  62. NPROT_INIT_LIST_HEAD(&pOpenContext->PendedReads);
  63. NPROT_INIT_LIST_HEAD(&pOpenContext->PendedWrites);
  64. NPROT_INIT_LIST_HEAD(&pOpenContext->RecvPktQueue);
  65. NPROT_INIT_EVENT(&pOpenContext->PoweredUpEvent);
  66. //
  67. // Start off by assuming that the device below is powered up.
  68. //
  69. NPROT_SIGNAL_EVENT(&pOpenContext->PoweredUpEvent);
  70. //
  71. // Determine the platform we are running on.
  72. //
  73. pOpenContext->bRunningOnWin9x = TRUE;
  74. NdisOpenProtocolConfiguration(
  75. &ConfigStatus,
  76. &ConfigHandle,
  77. (PNDIS_STRING)SystemSpecific1);
  78. if (ConfigStatus == NDIS_STATUS_SUCCESS)
  79. {
  80. PNDIS_CONFIGURATION_PARAMETER pParameter;
  81. NDIS_STRING VersionKey = NDIS_STRING_CONST("Environment");
  82. NdisReadConfiguration(
  83. &ConfigStatus,
  84. &pParameter,
  85. ConfigHandle,
  86. &VersionKey,
  87. NdisParameterInteger);
  88. if ((ConfigStatus == NDIS_STATUS_SUCCESS) &&
  89. ((pParameter->ParameterType == NdisParameterInteger) ||
  90. (pParameter->ParameterType == NdisParameterHexInteger)))
  91. {
  92. pOpenContext->bRunningOnWin9x =
  93. (pParameter->ParameterData.IntegerData == NdisEnvironmentWindows);
  94. }
  95. NdisCloseConfiguration(ConfigHandle);
  96. }
  97. NPROT_REF_OPEN(pOpenContext); // Bind
  98. //
  99. // Add it to the global list.
  100. //
  101. NPROT_ACQUIRE_LOCK(&Globals.GlobalLock);
  102. NPROT_INSERT_TAIL_LIST(&Globals.OpenList,
  103. &pOpenContext->Link);
  104. NPROT_RELEASE_LOCK(&Globals.GlobalLock);
  105. //
  106. // Set up the NDIS binding.
  107. //
  108. Status = ndisprotCreateBinding(
  109. pOpenContext,
  110. (PUCHAR)pDeviceName->Buffer,
  111. pDeviceName->Length);
  112. if (Status != NDIS_STATUS_SUCCESS)
  113. {
  114. break;
  115. }
  116. }
  117. while (FALSE);
  118. *pStatus = Status;
  119. return;
  120. }
  121. VOID
  122. NdisProtOpenAdapterComplete(
  123. IN NDIS_HANDLE ProtocolBindingContext,
  124. IN NDIS_STATUS Status,
  125. IN NDIS_STATUS OpenErrorCode
  126. )
  127. /*++
  128. Routine Description:
  129. Completion routine called by NDIS if our call to NdisOpenAdapter
  130. pends. Wake up the thread that called NdisOpenAdapter.
  131. Arguments:
  132. ProtocolBindingContext - pointer to open context structure
  133. Status - status of the open
  134. OpenErrorCode - if unsuccessful, additional information
  135. Return Value:
  136. None
  137. --*/
  138. {
  139. PNDISPROT_OPEN_CONTEXT pOpenContext;
  140. UNREFERENCED_PARAMETER(OpenErrorCode);
  141. pOpenContext = (PNDISPROT_OPEN_CONTEXT)ProtocolBindingContext;
  142. NPROT_STRUCT_ASSERT(pOpenContext, oc);
  143. pOpenContext->BindStatus = Status;
  144. NPROT_SIGNAL_EVENT(&pOpenContext->BindEvent);
  145. }
  146. VOID
  147. NdisProtUnbindAdapter(
  148. OUT PNDIS_STATUS pStatus,
  149. IN NDIS_HANDLE ProtocolBindingContext,
  150. IN NDIS_HANDLE UnbindContext
  151. )
  152. /*++
  153. Routine Description:
  154. NDIS calls this when it wants us to close the binding to an adapter.
  155. Arguments:
  156. pStatus - place to return status of Unbind
  157. ProtocolBindingContext - pointer to open context structure
  158. UnbindContext - to use in NdisCompleteUnbindAdapter if we return pending
  159. Return Value:
  160. None
  161. --*/
  162. {
  163. PNDISPROT_OPEN_CONTEXT pOpenContext;
  164. UNREFERENCED_PARAMETER(UnbindContext);
  165. pOpenContext = (PNDISPROT_OPEN_CONTEXT)ProtocolBindingContext;
  166. NPROT_STRUCT_ASSERT(pOpenContext, oc);
  167. //
  168. // Mark this open as having seen an Unbind.
  169. //
  170. NPROT_ACQUIRE_LOCK(&pOpenContext->Lock);
  171. NPROT_SET_FLAGS(pOpenContext->Flags, NUIOO_UNBIND_FLAGS, NUIOO_UNBIND_RECEIVED);
  172. //
  173. // In case we had threads blocked for the device below to be powered
  174. // up, wake them up.
  175. //
  176. NPROT_SIGNAL_EVENT(&pOpenContext->PoweredUpEvent);
  177. NPROT_RELEASE_LOCK(&pOpenContext->Lock);
  178. ndisprotShutdownBinding(pOpenContext);
  179. *pStatus = NDIS_STATUS_SUCCESS;
  180. return;
  181. }
  182. VOID
  183. NdisProtCloseAdapterComplete(
  184. IN NDIS_HANDLE ProtocolBindingContext,
  185. IN NDIS_STATUS Status
  186. )
  187. /*++
  188. Routine Description:
  189. Called by NDIS to complete a pended call to NdisCloseAdapter.
  190. We wake up the thread waiting for this completion.
  191. Arguments:
  192. ProtocolBindingContext - pointer to open context structure
  193. Status - Completion status of NdisCloseAdapter
  194. Return Value:
  195. None
  196. --*/
  197. {
  198. PNDISPROT_OPEN_CONTEXT pOpenContext;
  199. pOpenContext = (PNDISPROT_OPEN_CONTEXT)ProtocolBindingContext;
  200. NPROT_STRUCT_ASSERT(pOpenContext, oc);
  201. pOpenContext->BindStatus = Status;
  202. NPROT_SIGNAL_EVENT(&pOpenContext->BindEvent);
  203. }
  204. NDIS_STATUS
  205. NdisProtPnPEventHandler(
  206. IN NDIS_HANDLE ProtocolBindingContext,
  207. IN PNET_PNP_EVENT pNetPnPEvent
  208. )
  209. /*++
  210. Routine Description:
  211. Called by NDIS to notify us of a PNP event. The most significant
  212. one for us is power state change.
  213. Arguments:
  214. ProtocolBindingContext - pointer to open context structure
  215. this is NULL for global reconfig events.
  216. pNetPnPEvent - pointer to the PNP event
  217. Return Value:
  218. Our processing status for the PNP event.
  219. --*/
  220. {
  221. PNDISPROT_OPEN_CONTEXT pOpenContext;
  222. NDIS_STATUS Status;
  223. pOpenContext = (PNDISPROT_OPEN_CONTEXT)ProtocolBindingContext;
  224. switch (pNetPnPEvent->NetEvent)
  225. {
  226. case NetEventSetPower:
  227. NPROT_STRUCT_ASSERT(pOpenContext, oc);
  228. pOpenContext->PowerState = *(PNET_DEVICE_POWER_STATE)pNetPnPEvent->Buffer;
  229. if (pOpenContext->PowerState > NetDeviceStateD0)
  230. {
  231. //
  232. // The device below is transitioning to a low power state.
  233. // Block any threads attempting to query the device while
  234. // in this state.
  235. //
  236. NPROT_INIT_EVENT(&pOpenContext->PoweredUpEvent);
  237. //
  238. // Wait for any I/O in progress to complete.
  239. //
  240. ndisprotWaitForPendingIO(pOpenContext, FALSE);
  241. //
  242. // Return any receives that we had queued up.
  243. //
  244. ndisprotFlushReceiveQueue(pOpenContext);
  245. DEBUGP(DL_INFO, ("PnPEvent: Open %p, SetPower to %d\n",
  246. pOpenContext, pOpenContext->PowerState));
  247. }
  248. else
  249. {
  250. //
  251. // The device below is powered up.
  252. //
  253. DEBUGP(DL_INFO, ("PnPEvent: Open %p, SetPower ON: %d\n",
  254. pOpenContext, pOpenContext->PowerState));
  255. NPROT_SIGNAL_EVENT(&pOpenContext->PoweredUpEvent);
  256. }
  257. Status = NDIS_STATUS_SUCCESS;
  258. break;
  259. case NetEventQueryPower:
  260. Status = NDIS_STATUS_SUCCESS;
  261. break;
  262. case NetEventBindsComplete:
  263. NPROT_SIGNAL_EVENT(&Globals.BindsComplete);
  264. if(!ndisprotRegisterExCallBack()){
  265. DEBUGP(DL_ERROR, ("DriverEntry: ndisprotRegisterExCallBack failed\n"));
  266. }
  267. Status = NDIS_STATUS_SUCCESS;
  268. break;
  269. case NetEventQueryRemoveDevice:
  270. case NetEventCancelRemoveDevice:
  271. case NetEventReconfigure:
  272. case NetEventBindList:
  273. case NetEventPnPCapabilities:
  274. Status = NDIS_STATUS_SUCCESS;
  275. break;
  276. default:
  277. Status = NDIS_STATUS_NOT_SUPPORTED;
  278. break;
  279. }
  280. DEBUGP(DL_INFO, ("PnPEvent: Open %p, Event %d, Status %x\n",
  281. pOpenContext, pNetPnPEvent->NetEvent, Status));
  282. return (Status);
  283. }
  284. VOID
  285. NdisProtProtocolUnloadHandler(
  286. VOID
  287. )
  288. /*++
  289. Routine Description:
  290. NDIS calls this on a usermode request to uninstall us.
  291. Arguments:
  292. None
  293. Return Value:
  294. None
  295. --*/
  296. {
  297. ndisprotDoProtocolUnload();
  298. }
  299. NDIS_STATUS
  300. ndisprotCreateBinding(
  301. IN PNDISPROT_OPEN_CONTEXT pOpenContext,
  302. IN PUCHAR pBindingInfo,
  303. IN ULONG BindingInfoLength
  304. )
  305. /*++
  306. Routine Description:
  307. Utility function to create an NDIS binding to the indicated device,
  308. if no such binding exists.
  309. Here is where we also allocate additional resources (e.g. packet pool)
  310. for the binding.
  311. Things to take care of:
  312. 1. Is another thread doing this (or has finished binding) already?
  313. 2. Is the binding being closed at this time?
  314. 3. NDIS calls our Unbind handler while we are doing this.
  315. These precautions are not needed if this routine is only called from
  316. the context of our BindAdapter handler, but they are here in case
  317. we initiate binding from elsewhere (e.g. on processing a user command).
  318. NOTE: this function blocks and finishes synchronously.
  319. Arguments:
  320. pOpenContext - pointer to open context block
  321. pBindingInfo - pointer to unicode device name string
  322. BindingInfoLength - length in bytes of the above.
  323. Return Value:
  324. NDIS_STATUS_SUCCESS if a binding was successfully set up.
  325. NDIS_STATUS_XXX error code on any failure.
  326. --*/
  327. {
  328. NDIS_STATUS Status;
  329. NDIS_STATUS OpenErrorCode;
  330. NDIS_MEDIUM MediumArray[1] = {NdisMedium802_3};
  331. UINT SelectedMediumIndex;
  332. PNDISPROT_OPEN_CONTEXT pTmpOpenContext;
  333. BOOLEAN fDoNotDisturb = FALSE;
  334. BOOLEAN fOpenComplete = FALSE;
  335. ULONG BytesProcessed;
  336. ULONG GenericUlong = 0;
  337. DEBUGP(DL_LOUD, ("CreateBinding: open %p/%x, device [%ws]\n",
  338. pOpenContext, pOpenContext->Flags, pBindingInfo));
  339. Status = NDIS_STATUS_SUCCESS;
  340. do
  341. {
  342. //
  343. // Check if we already have a binding to this device.
  344. //
  345. pTmpOpenContext = ndisprotLookupDevice(pBindingInfo, BindingInfoLength);
  346. if (pTmpOpenContext != NULL)
  347. {
  348. DEBUGP(DL_WARN,
  349. ("CreateBinding: Binding to device %ws already exists on open %p\n",
  350. pTmpOpenContext->DeviceName.Buffer, pTmpOpenContext));
  351. NPROT_DEREF_OPEN(pTmpOpenContext); // temp ref added by Lookup
  352. Status = NDIS_STATUS_FAILURE;
  353. break;
  354. }
  355. NPROT_ACQUIRE_LOCK(&pOpenContext->Lock);
  356. //
  357. // Check if this open context is already bound/binding/closing.
  358. //
  359. if (!NPROT_TEST_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_IDLE) ||
  360. NPROT_TEST_FLAGS(pOpenContext->Flags, NUIOO_UNBIND_FLAGS, NUIOO_UNBIND_RECEIVED))
  361. {
  362. NPROT_RELEASE_LOCK(&pOpenContext->Lock);
  363. Status = NDIS_STATUS_NOT_ACCEPTED;
  364. //
  365. // Make sure we don't abort this binding on failure cleanup.
  366. //
  367. fDoNotDisturb = TRUE;
  368. break;
  369. }
  370. NPROT_SET_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_OPENING);
  371. NPROT_RELEASE_LOCK(&pOpenContext->Lock);
  372. //
  373. // Copy in the device name. Add room for a NULL terminator.
  374. //
  375. NPROT_ALLOC_MEM(pOpenContext->DeviceName.Buffer, BindingInfoLength + sizeof(WCHAR));
  376. if (pOpenContext->DeviceName.Buffer == NULL)
  377. {
  378. DEBUGP(DL_WARN, ("CreateBinding: failed to alloc device name buf (%d bytes)\n",
  379. BindingInfoLength + sizeof(WCHAR)));
  380. Status = NDIS_STATUS_RESOURCES;
  381. break;
  382. }
  383. NPROT_COPY_MEM(pOpenContext->DeviceName.Buffer, pBindingInfo, BindingInfoLength);
  384. *(PWCHAR)((PUCHAR)pOpenContext->DeviceName.Buffer + BindingInfoLength) = L'\0';
  385. NdisInitUnicodeString(&pOpenContext->DeviceName, pOpenContext->DeviceName.Buffer);
  386. //
  387. // Allocate packet pools.
  388. //
  389. NdisAllocatePacketPoolEx(
  390. &Status,
  391. &pOpenContext->SendPacketPool,
  392. MIN_SEND_PACKET_POOL_SIZE,
  393. MAX_SEND_PACKET_POOL_SIZE - MIN_SEND_PACKET_POOL_SIZE,
  394. sizeof(NPROT_SEND_PACKET_RSVD));
  395. if (Status != NDIS_STATUS_SUCCESS)
  396. {
  397. DEBUGP(DL_WARN, ("CreateBinding: failed to alloc"
  398. " send packet pool: %x\n", Status));
  399. break;
  400. }
  401. NdisAllocatePacketPoolEx(
  402. &Status,
  403. &pOpenContext->RecvPacketPool,
  404. MIN_RECV_PACKET_POOL_SIZE,
  405. MAX_RECV_PACKET_POOL_SIZE - MIN_RECV_PACKET_POOL_SIZE,
  406. sizeof(NPROT_RECV_PACKET_RSVD));
  407. if (Status != NDIS_STATUS_SUCCESS)
  408. {
  409. DEBUGP(DL_WARN, ("CreateBinding: failed to alloc"
  410. " recv packet pool: %x\n", Status));
  411. break;
  412. }
  413. //
  414. // Buffer pool for receives.
  415. //
  416. NdisAllocateBufferPool(
  417. &Status,
  418. &pOpenContext->RecvBufferPool,
  419. MAX_RECV_PACKET_POOL_SIZE);
  420. if (Status != NDIS_STATUS_SUCCESS)
  421. {
  422. DEBUGP(DL_WARN, ("CreateBinding: failed to alloc"
  423. " recv buffer pool: %x\n", Status));
  424. break;
  425. }
  426. //
  427. // If we are running on Win9X, allocate a buffer pool for sends
  428. // as well, since we can't simply cast MDLs to NDIS_BUFFERs.
  429. //
  430. if (pOpenContext->bRunningOnWin9x)
  431. {
  432. NdisAllocateBufferPool(
  433. &Status,
  434. &pOpenContext->SendBufferPool,
  435. MAX_SEND_PACKET_POOL_SIZE);
  436. if (Status != NDIS_STATUS_SUCCESS)
  437. {
  438. DEBUGP(DL_WARN, ("CreateBinding: failed to alloc"
  439. " send buffer pool: %x\n", Status));
  440. break;
  441. }
  442. }
  443. //
  444. // Assume that the device is powered up.
  445. //
  446. pOpenContext->PowerState = NetDeviceStateD0;
  447. //
  448. // Open the adapter.
  449. //
  450. NPROT_INIT_EVENT(&pOpenContext->BindEvent);
  451. NdisOpenAdapter(
  452. &Status,
  453. &OpenErrorCode,
  454. &pOpenContext->BindingHandle,
  455. &SelectedMediumIndex,
  456. &MediumArray[0],
  457. sizeof(MediumArray) / sizeof(NDIS_MEDIUM),
  458. Globals.NdisProtocolHandle,
  459. (NDIS_HANDLE)pOpenContext,
  460. &pOpenContext->DeviceName,
  461. 0,
  462. NULL);
  463. if (Status == NDIS_STATUS_PENDING)
  464. {
  465. NPROT_WAIT_EVENT(&pOpenContext->BindEvent, 0);
  466. Status = pOpenContext->BindStatus;
  467. }
  468. if (Status != NDIS_STATUS_SUCCESS)
  469. {
  470. DEBUGP(DL_WARN, ("CreateBinding: NdisOpenAdapter (%ws) failed: %x\n",
  471. pOpenContext->DeviceName.Buffer, Status));
  472. break;
  473. }
  474. //
  475. // Note down the fact that we have successfully bound.
  476. // We don't update the state on the open just yet - this
  477. // is to prevent other threads from shutting down the binding.
  478. //
  479. fOpenComplete = TRUE;
  480. //
  481. // Get the friendly name for the adapter. It is not fatal for this
  482. // to fail.
  483. //
  484. (VOID)NdisQueryAdapterInstanceName(
  485. &pOpenContext->DeviceDescr,
  486. pOpenContext->BindingHandle
  487. );
  488. //
  489. // Get Current address
  490. //
  491. Status = ndisprotDoRequest(
  492. pOpenContext,
  493. NdisRequestQueryInformation,
  494. OID_802_3_CURRENT_ADDRESS,
  495. &pOpenContext->CurrentAddress[0],
  496. NPROT_MAC_ADDR_LEN,
  497. &BytesProcessed
  498. );
  499. if (Status != NDIS_STATUS_SUCCESS)
  500. {
  501. DEBUGP(DL_WARN, ("CreateBinding: qry current address failed: %x\n",
  502. Status));
  503. break;
  504. }
  505. //
  506. // Get MAC options.
  507. //
  508. Status = ndisprotDoRequest(
  509. pOpenContext,
  510. NdisRequestQueryInformation,
  511. OID_GEN_MAC_OPTIONS,
  512. &pOpenContext->MacOptions,
  513. sizeof(pOpenContext->MacOptions),
  514. &BytesProcessed
  515. );
  516. if (Status != NDIS_STATUS_SUCCESS)
  517. {
  518. DEBUGP(DL_WARN, ("CreateBinding: qry MAC options failed: %x\n",
  519. Status));
  520. break;
  521. }
  522. //
  523. // Get the max frame size.
  524. //
  525. Status = ndisprotDoRequest(
  526. pOpenContext,
  527. NdisRequestQueryInformation,
  528. OID_GEN_MAXIMUM_FRAME_SIZE,
  529. &pOpenContext->MaxFrameSize,
  530. sizeof(pOpenContext->MaxFrameSize),
  531. &BytesProcessed
  532. );
  533. if (Status != NDIS_STATUS_SUCCESS)
  534. {
  535. DEBUGP(DL_WARN, ("CreateBinding: qry max frame failed: %x\n",
  536. Status));
  537. break;
  538. }
  539. //
  540. // Get the media connect status.
  541. //
  542. Status = ndisprotDoRequest(
  543. pOpenContext,
  544. NdisRequestQueryInformation,
  545. OID_GEN_MEDIA_CONNECT_STATUS,
  546. &GenericUlong,
  547. sizeof(GenericUlong),
  548. &BytesProcessed
  549. );
  550. if (Status != NDIS_STATUS_SUCCESS)
  551. {
  552. DEBUGP(DL_WARN, ("CreateBinding: qry media connect status failed: %x\n",
  553. Status));
  554. break;
  555. }
  556. if (GenericUlong == NdisMediaStateConnected)
  557. {
  558. NPROT_SET_FLAGS(pOpenContext->Flags, NUIOO_MEDIA_FLAGS, NUIOO_MEDIA_CONNECTED);
  559. }
  560. else
  561. {
  562. NPROT_SET_FLAGS(pOpenContext->Flags, NUIOO_MEDIA_FLAGS, NUIOO_MEDIA_DISCONNECTED);
  563. }
  564. //
  565. // Mark this open. Also check if we received an Unbind while
  566. // we were setting this up.
  567. //
  568. NPROT_ACQUIRE_LOCK(&pOpenContext->Lock);
  569. NPROT_SET_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_ACTIVE);
  570. //
  571. // Did an unbind happen in the meantime?
  572. //
  573. if (NPROT_TEST_FLAGS(pOpenContext->Flags, NUIOO_UNBIND_FLAGS, NUIOO_UNBIND_RECEIVED))
  574. {
  575. Status = NDIS_STATUS_FAILURE;
  576. }
  577. NPROT_RELEASE_LOCK(&pOpenContext->Lock);
  578. }
  579. while (FALSE);
  580. if ((Status != NDIS_STATUS_SUCCESS) && !fDoNotDisturb)
  581. {
  582. NPROT_ACQUIRE_LOCK(&pOpenContext->Lock);
  583. //
  584. // Check if we had actually finished opening the adapter.
  585. //
  586. if (fOpenComplete)
  587. {
  588. NPROT_SET_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_ACTIVE);
  589. }
  590. else if (NPROT_TEST_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_OPENING))
  591. {
  592. NPROT_SET_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_FAILED);
  593. }
  594. NPROT_RELEASE_LOCK(&pOpenContext->Lock);
  595. ndisprotShutdownBinding(pOpenContext);
  596. }
  597. DEBUGP(DL_INFO, ("CreateBinding: OpenContext %p, Status %x\n",
  598. pOpenContext, Status));
  599. return (Status);
  600. }
  601. VOID
  602. ndisprotShutdownBinding(
  603. IN PNDISPROT_OPEN_CONTEXT pOpenContext
  604. )
  605. /*++
  606. Routine Description:
  607. Utility function to shut down the NDIS binding, if one exists, on
  608. the specified open. This is written to be called from:
  609. ndisprotCreateBinding - on failure
  610. NdisProtUnbindAdapter
  611. We handle the case where a binding is in the process of being set up.
  612. This precaution is not needed if this routine is only called from
  613. the context of our UnbindAdapter handler, but they are here in case
  614. we initiate unbinding from elsewhere (e.g. on processing a user command).
  615. NOTE: this blocks and finishes synchronously.
  616. Arguments:
  617. pOpenContext - pointer to open context block
  618. Return Value:
  619. None
  620. --*/
  621. {
  622. NDIS_STATUS Status;
  623. BOOLEAN DoCloseBinding = FALSE;
  624. do
  625. {
  626. NPROT_ACQUIRE_LOCK(&pOpenContext->Lock);
  627. if (NPROT_TEST_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_OPENING))
  628. {
  629. //
  630. // We are still in the process of setting up this binding.
  631. //
  632. NPROT_RELEASE_LOCK(&pOpenContext->Lock);
  633. break;
  634. }
  635. if (NPROT_TEST_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_ACTIVE))
  636. {
  637. NPROT_SET_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_CLOSING);
  638. DoCloseBinding = TRUE;
  639. }
  640. NPROT_RELEASE_LOCK(&pOpenContext->Lock);
  641. if (DoCloseBinding)
  642. {
  643. ULONG PacketFilter = 0;
  644. ULONG BytesRead = 0;
  645. //
  646. // Set Packet filter to 0 before closing the binding
  647. //
  648. Status = ndisprotDoRequest(
  649. pOpenContext,
  650. NdisRequestSetInformation,
  651. OID_GEN_CURRENT_PACKET_FILTER,
  652. &PacketFilter,
  653. sizeof(PacketFilter),
  654. &BytesRead);
  655. if (Status != NDIS_STATUS_SUCCESS)
  656. {
  657. DEBUGP(DL_WARN, ("ShutDownBinding: set packet filter failed: %x\n", Status));
  658. }
  659. //
  660. // Set multicast list to null before closing the binding
  661. //
  662. Status = ndisprotDoRequest(
  663. pOpenContext,
  664. NdisRequestSetInformation,
  665. OID_802_3_MULTICAST_LIST,
  666. NULL,
  667. 0,
  668. &BytesRead);
  669. if (Status != NDIS_STATUS_SUCCESS)
  670. {
  671. DEBUGP(DL_WARN, ("ShutDownBinding: set multicast list failed: %x\n", Status));
  672. }
  673. //
  674. // Wait for any pending sends or requests on
  675. // the binding to complete.
  676. //
  677. ndisprotWaitForPendingIO(pOpenContext, TRUE);
  678. //
  679. // Discard any queued receives.
  680. //
  681. ndisprotFlushReceiveQueue(pOpenContext);
  682. //
  683. // Close the binding now.
  684. //
  685. NPROT_INIT_EVENT(&pOpenContext->BindEvent);
  686. DEBUGP(DL_INFO, ("ShutdownBinding: Closing OpenContext %p,"
  687. " BindingHandle %p\n",
  688. pOpenContext, pOpenContext->BindingHandle));
  689. NdisCloseAdapter(&Status, pOpenContext->BindingHandle);
  690. if (Status == NDIS_STATUS_PENDING)
  691. {
  692. NPROT_WAIT_EVENT(&pOpenContext->BindEvent, 0);
  693. Status = pOpenContext->BindStatus;
  694. }
  695. NPROT_ASSERT(Status == NDIS_STATUS_SUCCESS);
  696. pOpenContext->BindingHandle = NULL;
  697. }
  698. if (DoCloseBinding)
  699. {
  700. NPROT_ACQUIRE_LOCK(&pOpenContext->Lock);
  701. NPROT_SET_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_IDLE);
  702. NPROT_SET_FLAGS(pOpenContext->Flags, NUIOO_UNBIND_FLAGS, 0);
  703. NPROT_RELEASE_LOCK(&pOpenContext->Lock);
  704. }
  705. //
  706. // Remove it from the global list.
  707. //
  708. NPROT_ACQUIRE_LOCK(&Globals.GlobalLock);
  709. NPROT_REMOVE_ENTRY_LIST(&pOpenContext->Link);
  710. NPROT_RELEASE_LOCK(&Globals.GlobalLock);
  711. //
  712. // Free any other resources allocated for this bind.
  713. //
  714. ndisprotFreeBindResources(pOpenContext);
  715. NPROT_DEREF_OPEN(pOpenContext); // Shutdown binding
  716. }
  717. while (FALSE);
  718. }
  719. VOID
  720. ndisprotFreeBindResources(
  721. IN PNDISPROT_OPEN_CONTEXT pOpenContext
  722. )
  723. /*++
  724. Routine Description:
  725. Free any resources set up for an NDIS binding.
  726. Arguments:
  727. pOpenContext - pointer to open context block
  728. Return Value:
  729. None
  730. --*/
  731. {
  732. if (pOpenContext->SendPacketPool != NULL)
  733. {
  734. NdisFreePacketPool(pOpenContext->SendPacketPool);
  735. pOpenContext->SendPacketPool = NULL;
  736. }
  737. if (pOpenContext->RecvPacketPool != NULL)
  738. {
  739. NdisFreePacketPool(pOpenContext->RecvPacketPool);
  740. pOpenContext->RecvPacketPool = NULL;
  741. }
  742. if (pOpenContext->RecvBufferPool != NULL)
  743. {
  744. NdisFreeBufferPool(pOpenContext->RecvBufferPool);
  745. pOpenContext->RecvBufferPool = NULL;
  746. }
  747. if (pOpenContext->SendBufferPool != NULL)
  748. {
  749. NdisFreeBufferPool(pOpenContext->SendBufferPool);
  750. pOpenContext->SendBufferPool = NULL;
  751. }
  752. if (pOpenContext->DeviceName.Buffer != NULL)
  753. {
  754. NPROT_FREE_MEM(pOpenContext->DeviceName.Buffer);
  755. pOpenContext->DeviceName.Buffer = NULL;
  756. pOpenContext->DeviceName.Length =
  757. pOpenContext->DeviceName.MaximumLength = 0;
  758. }
  759. if (pOpenContext->DeviceDescr.Buffer != NULL)
  760. {
  761. //
  762. // this would have been allocated by NdisQueryAdpaterInstanceName.
  763. //
  764. NdisFreeMemory(pOpenContext->DeviceDescr.Buffer, 0, 0);
  765. pOpenContext->DeviceDescr.Buffer = NULL;
  766. }
  767. }
  768. VOID
  769. ndisprotWaitForPendingIO(
  770. IN PNDISPROT_OPEN_CONTEXT pOpenContext,
  771. IN BOOLEAN DoCancelReads
  772. )
  773. /*++
  774. Routine Description:
  775. Utility function to wait for all outstanding I/O to complete
  776. on an open context. It is assumed that the open context
  777. won't go away while we are in this routine.
  778. Arguments:
  779. pOpenContext - pointer to open context structure
  780. DoCancelReads - do we wait for pending reads to go away (and cancel them)?
  781. Return Value:
  782. None
  783. --*/
  784. {
  785. NDIS_STATUS Status;
  786. ULONG LoopCount;
  787. ULONG PendingCount;
  788. #ifdef NDIS51
  789. //
  790. // Wait for any pending sends or requests on the binding to complete.
  791. //
  792. for (LoopCount = 0; LoopCount < 60; LoopCount++)
  793. {
  794. Status = NdisQueryPendingIOCount(
  795. pOpenContext->BindingHandle,
  796. &PendingCount);
  797. if ((Status != NDIS_STATUS_SUCCESS) ||
  798. (PendingCount == 0))
  799. {
  800. break;
  801. }
  802. DEBUGP(DL_INFO, ("WaitForPendingIO: Open %p, %d pending I/O at NDIS\n",
  803. pOpenContext, PendingCount));
  804. NPROT_SLEEP(2);
  805. }
  806. NPROT_ASSERT(LoopCount < 60);
  807. #endif // NDIS51
  808. //
  809. // Make sure any threads trying to send have finished.
  810. //
  811. for (LoopCount = 0; LoopCount < 60; LoopCount++)
  812. {
  813. if (pOpenContext->PendedSendCount == 0)
  814. {
  815. break;
  816. }
  817. DEBUGP(DL_WARN, ("WaitForPendingIO: Open %p, %d pended sends\n",
  818. pOpenContext, pOpenContext->PendedSendCount));
  819. NPROT_SLEEP(1);
  820. }
  821. NPROT_ASSERT(LoopCount < 60);
  822. if (DoCancelReads)
  823. {
  824. //
  825. // Wait for any pended reads to complete/cancel.
  826. //
  827. while (pOpenContext->PendedReadCount != 0)
  828. {
  829. DEBUGP(DL_INFO, ("WaitForPendingIO: Open %p, %d pended reads\n",
  830. pOpenContext, pOpenContext->PendedReadCount));
  831. //
  832. // Cancel any pending reads.
  833. //
  834. ndisprotCancelPendingReads(pOpenContext);
  835. NPROT_SLEEP(1);
  836. }
  837. }
  838. }
  839. VOID
  840. ndisprotDoProtocolUnload(
  841. VOID
  842. )
  843. /*++
  844. Routine Description:
  845. Utility routine to handle unload from the NDIS protocol side.
  846. Arguments:
  847. None
  848. Return Value:
  849. None
  850. --*/
  851. {
  852. NDIS_HANDLE ProtocolHandle;
  853. NDIS_STATUS Status;
  854. DEBUGP(DL_INFO, ("ProtocolUnload: ProtocolHandle %lx\n",
  855. Globals.NdisProtocolHandle));
  856. if (Globals.NdisProtocolHandle != NULL)
  857. {
  858. ProtocolHandle = Globals.NdisProtocolHandle;
  859. Globals.NdisProtocolHandle = NULL;
  860. NdisDeregisterProtocol(
  861. &Status,
  862. ProtocolHandle
  863. );
  864. }
  865. }
  866. NDIS_STATUS
  867. ndisprotDoRequest(
  868. IN PNDISPROT_OPEN_CONTEXT pOpenContext,
  869. IN NDIS_REQUEST_TYPE RequestType,
  870. IN NDIS_OID Oid,
  871. IN PVOID InformationBuffer,
  872. IN ULONG InformationBufferLength,
  873. OUT PULONG pBytesProcessed
  874. )
  875. /*++
  876. Routine Description:
  877. Utility routine that forms and sends an NDIS_REQUEST to the
  878. miniport, waits for it to complete, and returns status
  879. to the caller.
  880. NOTE: this assumes that the calling routine ensures validity
  881. of the binding handle until this returns.
  882. Arguments:
  883. pOpenContext - pointer to our open context
  884. RequestType - NdisRequest[Set|Query]Information
  885. Oid - the object being set/queried
  886. InformationBuffer - data for the request
  887. InformationBufferLength - length of the above
  888. pBytesProcessed - place to return bytes read/written
  889. Return Value:
  890. Status of the set/query request
  891. --*/
  892. {
  893. NDISPROT_REQUEST ReqContext;
  894. PNDIS_REQUEST pNdisRequest = &ReqContext.Request;
  895. NDIS_STATUS Status;
  896. NPROT_INIT_EVENT(&ReqContext.ReqEvent);
  897. pNdisRequest->RequestType = RequestType;
  898. switch (RequestType)
  899. {
  900. case NdisRequestQueryInformation:
  901. pNdisRequest->DATA.QUERY_INFORMATION.Oid = Oid;
  902. pNdisRequest->DATA.QUERY_INFORMATION.InformationBuffer =
  903. InformationBuffer;
  904. pNdisRequest->DATA.QUERY_INFORMATION.InformationBufferLength =
  905. InformationBufferLength;
  906. break;
  907. case NdisRequestSetInformation:
  908. pNdisRequest->DATA.SET_INFORMATION.Oid = Oid;
  909. pNdisRequest->DATA.SET_INFORMATION.InformationBuffer =
  910. InformationBuffer;
  911. pNdisRequest->DATA.SET_INFORMATION.InformationBufferLength =
  912. InformationBufferLength;
  913. break;
  914. default:
  915. NPROT_ASSERT(FALSE);
  916. break;
  917. }
  918. NdisRequest(&Status,
  919. pOpenContext->BindingHandle,
  920. pNdisRequest);
  921. if (Status == NDIS_STATUS_PENDING)
  922. {
  923. NPROT_WAIT_EVENT(&ReqContext.ReqEvent, 0);
  924. Status = ReqContext.Status;
  925. }
  926. if (Status == NDIS_STATUS_SUCCESS)
  927. {
  928. *pBytesProcessed = (RequestType == NdisRequestQueryInformation)?
  929. pNdisRequest->DATA.QUERY_INFORMATION.BytesWritten:
  930. pNdisRequest->DATA.SET_INFORMATION.BytesRead;
  931. //
  932. // The driver below should set the correct value to BytesWritten
  933. // or BytesRead. But now, we just truncate the value to InformationBufferLength
  934. //
  935. if (*pBytesProcessed > InformationBufferLength)
  936. {
  937. *pBytesProcessed = InformationBufferLength;
  938. }
  939. }
  940. return (Status);
  941. }
  942. NDIS_STATUS
  943. ndisprotValidateOpenAndDoRequest(
  944. IN PNDISPROT_OPEN_CONTEXT pOpenContext,
  945. IN NDIS_REQUEST_TYPE RequestType,
  946. IN NDIS_OID Oid,
  947. IN PVOID InformationBuffer,
  948. IN ULONG InformationBufferLength,
  949. OUT PULONG pBytesProcessed,
  950. IN BOOLEAN bWaitForPowerOn
  951. )
  952. /*++
  953. Routine Description:
  954. Utility routine to prevalidate and reference an open context
  955. before calling ndisprotDoRequest. This routine makes sure
  956. we have a valid binding.
  957. Arguments:
  958. pOpenContext - pointer to our open context
  959. RequestType - NdisRequest[Set|Query]Information
  960. Oid - the object being set/queried
  961. InformationBuffer - data for the request
  962. InformationBufferLength - length of the above
  963. pBytesProcessed - place to return bytes read/written
  964. bWaitForPowerOn - Wait for the device to be powered on if it isn't already.
  965. Return Value:
  966. Status of the set/query request
  967. --*/
  968. {
  969. NDIS_STATUS Status;
  970. do
  971. {
  972. if (pOpenContext == NULL)
  973. {
  974. DEBUGP(DL_WARN, ("ValidateOpenAndDoRequest: request on unassociated file object!\n"));
  975. Status = NDIS_STATUS_INVALID_DATA;
  976. break;
  977. }
  978. NPROT_STRUCT_ASSERT(pOpenContext, oc);
  979. NPROT_ACQUIRE_LOCK(&pOpenContext->Lock);
  980. //
  981. // Proceed only if we have a binding.
  982. //
  983. if (!NPROT_TEST_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_ACTIVE))
  984. {
  985. NPROT_RELEASE_LOCK(&pOpenContext->Lock);
  986. Status = NDIS_STATUS_INVALID_DATA;
  987. break;
  988. }
  989. NPROT_ASSERT(pOpenContext->BindingHandle != NULL);
  990. //
  991. // Make sure that the binding does not go away until we
  992. // are finished with the request.
  993. //
  994. NdisInterlockedIncrement((PLONG)&pOpenContext->PendedSendCount);
  995. NPROT_RELEASE_LOCK(&pOpenContext->Lock);
  996. if (bWaitForPowerOn)
  997. {
  998. //
  999. // Wait for the device below to be powered up.
  1000. // We don't wait indefinitely here - this is to avoid
  1001. // a PROCESS_HAS_LOCKED_PAGES bugcheck that could happen
  1002. // if the calling process terminates, and this IRP doesn't
  1003. // complete within a reasonable time. An alternative would
  1004. // be to explicitly handle cancellation of this IRP.
  1005. //
  1006. NPROT_WAIT_EVENT(&pOpenContext->PoweredUpEvent, 4500);
  1007. }
  1008. Status = ndisprotDoRequest(
  1009. pOpenContext,
  1010. RequestType,
  1011. Oid,
  1012. InformationBuffer,
  1013. InformationBufferLength,
  1014. pBytesProcessed);
  1015. //
  1016. // Let go of the binding.
  1017. //
  1018. NdisInterlockedDecrement((PLONG)&pOpenContext->PendedSendCount);
  1019. }
  1020. while (FALSE);
  1021. DEBUGP(DL_LOUD, ("ValidateOpenAndDoReq: Open %p/%x, OID %x, Status %x\n",
  1022. pOpenContext, pOpenContext->Flags, Oid, Status));
  1023. return (Status);
  1024. }
  1025. VOID
  1026. NdisProtResetComplete(
  1027. IN NDIS_HANDLE ProtocolBindingContext,
  1028. IN NDIS_STATUS Status
  1029. )
  1030. /*++
  1031. Routine Description:
  1032. NDIS entry point indicating that a protocol initiated reset
  1033. has completed. Since we never call NdisReset(), this should
  1034. never be called.
  1035. Arguments:
  1036. ProtocolBindingContext - pointer to open context
  1037. Status - status of reset completion
  1038. Return Value:
  1039. None
  1040. --*/
  1041. {
  1042. UNREFERENCED_PARAMETER(ProtocolBindingContext);
  1043. UNREFERENCED_PARAMETER(Status);
  1044. ASSERT(FALSE);
  1045. return;
  1046. }
  1047. VOID
  1048. NdisProtRequestComplete(
  1049. IN NDIS_HANDLE ProtocolBindingContext,
  1050. IN PNDIS_REQUEST pNdisRequest,
  1051. IN NDIS_STATUS Status
  1052. )
  1053. /*++
  1054. Routine Description:
  1055. NDIS entry point indicating completion of a pended NDIS_REQUEST.
  1056. Arguments:
  1057. ProtocolBindingContext - pointer to open context
  1058. pNdisRequest - pointer to NDIS request
  1059. Status - status of reset completion
  1060. Return Value:
  1061. None
  1062. --*/
  1063. {
  1064. PNDISPROT_OPEN_CONTEXT pOpenContext;
  1065. PNDISPROT_REQUEST pReqContext;
  1066. pOpenContext = (PNDISPROT_OPEN_CONTEXT)ProtocolBindingContext;
  1067. NPROT_STRUCT_ASSERT(pOpenContext, oc);
  1068. //
  1069. // Get at the request context.
  1070. //
  1071. pReqContext = CONTAINING_RECORD(pNdisRequest, NDISPROT_REQUEST, Request);
  1072. //
  1073. // Save away the completion status.
  1074. //
  1075. pReqContext->Status = Status;
  1076. //
  1077. // Wake up the thread blocked for this request to complete.
  1078. //
  1079. NPROT_SIGNAL_EVENT(&pReqContext->ReqEvent);
  1080. }
  1081. VOID
  1082. NdisProtStatus(
  1083. IN NDIS_HANDLE ProtocolBindingContext,
  1084. IN NDIS_STATUS GeneralStatus,
  1085. IN PVOID StatusBuffer,
  1086. IN UINT StatusBufferSize
  1087. )
  1088. /*++
  1089. Routine Description:
  1090. Protocol entry point called by NDIS to indicate a change
  1091. in status at the miniport.
  1092. We make note of reset and media connect status indications.
  1093. Arguments:
  1094. ProtocolBindingContext - pointer to open context
  1095. GeneralStatus - status code
  1096. StatusBuffer - status-specific additional information
  1097. StatusBufferSize - size of the above
  1098. Return Value:
  1099. None
  1100. --*/
  1101. {
  1102. PNDISPROT_OPEN_CONTEXT pOpenContext;
  1103. UNREFERENCED_PARAMETER(StatusBuffer);
  1104. UNREFERENCED_PARAMETER(StatusBufferSize);
  1105. pOpenContext = (PNDISPROT_OPEN_CONTEXT)ProtocolBindingContext;
  1106. NPROT_STRUCT_ASSERT(pOpenContext, oc);
  1107. DEBUGP(DL_INFO, ("Status: Open %p, Status %x\n",
  1108. pOpenContext, GeneralStatus));
  1109. NPROT_ACQUIRE_LOCK(&pOpenContext->Lock);
  1110. do
  1111. {
  1112. if (pOpenContext->PowerState != NetDeviceStateD0)
  1113. {
  1114. //
  1115. // The device is in a low power state.
  1116. //
  1117. DEBUGP(DL_INFO, ("Status: Open %p in power state %d,"
  1118. " Status %x ignored\n", pOpenContext,
  1119. pOpenContext->PowerState, GeneralStatus));
  1120. //
  1121. // We continue and make note of status indications
  1122. //
  1123. // break;
  1124. //
  1125. //
  1126. // NOTE that any actions we take based on these
  1127. // status indications should take into account
  1128. // the current device power state.
  1129. //
  1130. }
  1131. switch(GeneralStatus)
  1132. {
  1133. case NDIS_STATUS_RESET_START:
  1134. NPROT_ASSERT(!NPROT_TEST_FLAGS(pOpenContext->Flags,
  1135. NUIOO_RESET_FLAGS,
  1136. NUIOO_RESET_IN_PROGRESS));
  1137. NPROT_SET_FLAGS(pOpenContext->Flags,
  1138. NUIOO_RESET_FLAGS,
  1139. NUIOO_RESET_IN_PROGRESS);
  1140. break;
  1141. case NDIS_STATUS_RESET_END:
  1142. NPROT_ASSERT(NPROT_TEST_FLAGS(pOpenContext->Flags,
  1143. NUIOO_RESET_FLAGS,
  1144. NUIOO_RESET_IN_PROGRESS));
  1145. NPROT_SET_FLAGS(pOpenContext->Flags,
  1146. NUIOO_RESET_FLAGS,
  1147. NUIOO_NOT_RESETTING);
  1148. break;
  1149. case NDIS_STATUS_MEDIA_CONNECT:
  1150. NPROT_SET_FLAGS(pOpenContext->Flags,
  1151. NUIOO_MEDIA_FLAGS,
  1152. NUIOO_MEDIA_CONNECTED);
  1153. break;
  1154. case NDIS_STATUS_MEDIA_DISCONNECT:
  1155. NPROT_SET_FLAGS(pOpenContext->Flags,
  1156. NUIOO_MEDIA_FLAGS,
  1157. NUIOO_MEDIA_DISCONNECTED);
  1158. break;
  1159. default:
  1160. break;
  1161. }
  1162. }
  1163. while (FALSE);
  1164. NPROT_RELEASE_LOCK(&pOpenContext->Lock);
  1165. }
  1166. VOID
  1167. NdisProtStatusComplete(
  1168. IN NDIS_HANDLE ProtocolBindingContext
  1169. )
  1170. /*++
  1171. Routine Description:
  1172. Protocol entry point called by NDIS. We ignore this.
  1173. Arguments:
  1174. ProtocolBindingContext - pointer to open context
  1175. Return Value:
  1176. None
  1177. --*/
  1178. {
  1179. PNDISPROT_OPEN_CONTEXT pOpenContext;
  1180. pOpenContext = (PNDISPROT_OPEN_CONTEXT)ProtocolBindingContext;
  1181. NPROT_STRUCT_ASSERT(pOpenContext, oc);
  1182. return;
  1183. }
  1184. NDIS_STATUS
  1185. ndisprotQueryBinding(
  1186. IN PUCHAR pBuffer,
  1187. IN ULONG InputLength,
  1188. IN ULONG OutputLength,
  1189. OUT PULONG pBytesReturned
  1190. )
  1191. /*++
  1192. Routine Description:
  1193. Return information about the specified binding.
  1194. Arguments:
  1195. pBuffer - pointer to NDISPROT_QUERY_BINDING
  1196. InputLength - input buffer size
  1197. OutputLength - output buffer size
  1198. pBytesReturned - place to return copied byte count.
  1199. Return Value:
  1200. NDIS_STATUS_SUCCESS if successful, failure code otherwise.
  1201. --*/
  1202. {
  1203. PNDISPROT_QUERY_BINDING pQueryBinding;
  1204. PNDISPROT_OPEN_CONTEXT pOpenContext;
  1205. PLIST_ENTRY pEnt;
  1206. ULONG Remaining;
  1207. ULONG BindingIndex;
  1208. NDIS_STATUS Status;
  1209. do
  1210. {
  1211. if (InputLength < sizeof(NDISPROT_QUERY_BINDING))
  1212. {
  1213. Status = NDIS_STATUS_RESOURCES;
  1214. break;
  1215. }
  1216. if (OutputLength < sizeof(NDISPROT_QUERY_BINDING))
  1217. {
  1218. Status = NDIS_STATUS_BUFFER_OVERFLOW;
  1219. break;
  1220. }
  1221. Remaining = OutputLength - sizeof(NDISPROT_QUERY_BINDING);
  1222. pQueryBinding = (PNDISPROT_QUERY_BINDING)pBuffer;
  1223. BindingIndex = pQueryBinding->BindingIndex;
  1224. Status = NDIS_STATUS_ADAPTER_NOT_FOUND;
  1225. pOpenContext = NULL;
  1226. NPROT_ACQUIRE_LOCK(&Globals.GlobalLock);
  1227. for (pEnt = Globals.OpenList.Flink;
  1228. pEnt != &Globals.OpenList;
  1229. pEnt = pEnt->Flink)
  1230. {
  1231. pOpenContext = CONTAINING_RECORD(pEnt, NDISPROT_OPEN_CONTEXT, Link);
  1232. NPROT_STRUCT_ASSERT(pOpenContext, oc);
  1233. NPROT_ACQUIRE_LOCK(&pOpenContext->Lock);
  1234. //
  1235. // Skip if not bound.
  1236. //
  1237. if (!NPROT_TEST_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_ACTIVE))
  1238. {
  1239. NPROT_RELEASE_LOCK(&pOpenContext->Lock);
  1240. continue;
  1241. }
  1242. if (BindingIndex == 0)
  1243. {
  1244. //
  1245. // Got the binding we are looking for. Copy the device
  1246. // name and description strings to the output buffer.
  1247. //
  1248. DEBUGP(DL_INFO,
  1249. ("QueryBinding: found open %p\n", pOpenContext));
  1250. pQueryBinding->DeviceNameLength = pOpenContext->DeviceName.Length + sizeof(WCHAR);
  1251. pQueryBinding->DeviceDescrLength = pOpenContext->DeviceDescr.Length + sizeof(WCHAR);
  1252. if (Remaining < pQueryBinding->DeviceNameLength +
  1253. pQueryBinding->DeviceDescrLength)
  1254. {
  1255. NPROT_RELEASE_LOCK(&pOpenContext->Lock);
  1256. Status = NDIS_STATUS_BUFFER_OVERFLOW;
  1257. break;
  1258. }
  1259. NPROT_ZERO_MEM((PUCHAR)pBuffer + sizeof(NDISPROT_QUERY_BINDING),
  1260. pQueryBinding->DeviceNameLength +
  1261. pQueryBinding->DeviceDescrLength);
  1262. pQueryBinding->DeviceNameOffset = sizeof(NDISPROT_QUERY_BINDING);
  1263. NPROT_COPY_MEM((PUCHAR)pBuffer + pQueryBinding->DeviceNameOffset,
  1264. pOpenContext->DeviceName.Buffer,
  1265. pOpenContext->DeviceName.Length);
  1266. pQueryBinding->DeviceDescrOffset = pQueryBinding->DeviceNameOffset +
  1267. pQueryBinding->DeviceNameLength;
  1268. NPROT_COPY_MEM((PUCHAR)pBuffer + pQueryBinding->DeviceDescrOffset,
  1269. pOpenContext->DeviceDescr.Buffer,
  1270. pOpenContext->DeviceDescr.Length);
  1271. NPROT_RELEASE_LOCK(&pOpenContext->Lock);
  1272. *pBytesReturned = pQueryBinding->DeviceDescrOffset + pQueryBinding->DeviceDescrLength;
  1273. Status = NDIS_STATUS_SUCCESS;
  1274. break;
  1275. }
  1276. NPROT_RELEASE_LOCK(&pOpenContext->Lock);
  1277. BindingIndex--;
  1278. }
  1279. NPROT_RELEASE_LOCK(&Globals.GlobalLock);
  1280. }
  1281. while (FALSE);
  1282. return (Status);
  1283. }
  1284. PNDISPROT_OPEN_CONTEXT
  1285. ndisprotLookupDevice(
  1286. IN PUCHAR pBindingInfo,
  1287. IN ULONG BindingInfoLength
  1288. )
  1289. /*++
  1290. Routine Description:
  1291. Search our global list for an open context structure that
  1292. has a binding to the specified device, and return a pointer
  1293. to it.
  1294. NOTE: we reference the open that we return.
  1295. Arguments:
  1296. pBindingInfo - pointer to unicode device name string
  1297. BindingInfoLength - length in bytes of the above.
  1298. Return Value:
  1299. Pointer to the matching open context if found, else NULL
  1300. --*/
  1301. {
  1302. PNDISPROT_OPEN_CONTEXT pOpenContext;
  1303. PLIST_ENTRY pEnt;
  1304. pOpenContext = NULL;
  1305. NPROT_ACQUIRE_LOCK(&Globals.GlobalLock);
  1306. for (pEnt = Globals.OpenList.Flink;
  1307. pEnt != &Globals.OpenList;
  1308. pEnt = pEnt->Flink)
  1309. {
  1310. pOpenContext = CONTAINING_RECORD(pEnt, NDISPROT_OPEN_CONTEXT, Link);
  1311. NPROT_STRUCT_ASSERT(pOpenContext, oc);
  1312. //
  1313. // Check if this has the name we are looking for.
  1314. //
  1315. if ((pOpenContext->DeviceName.Length == BindingInfoLength) &&
  1316. NPROT_MEM_CMP(pOpenContext->DeviceName.Buffer, pBindingInfo, BindingInfoLength))
  1317. {
  1318. NPROT_REF_OPEN(pOpenContext); // ref added by LookupDevice
  1319. break;
  1320. }
  1321. pOpenContext = NULL;
  1322. }
  1323. NPROT_RELEASE_LOCK(&Globals.GlobalLock);
  1324. return (pOpenContext);
  1325. }
  1326. NDIS_STATUS
  1327. ndisprotQueryOidValue(
  1328. IN PNDISPROT_OPEN_CONTEXT pOpenContext,
  1329. OUT PVOID pDataBuffer,
  1330. IN ULONG BufferLength,
  1331. OUT PULONG pBytesWritten
  1332. )
  1333. /*++
  1334. Routine Description:
  1335. Query an arbitrary OID value from the miniport.
  1336. Arguments:
  1337. pOpenContext - pointer to open context representing our binding to the miniport
  1338. pDataBuffer - place to store the returned value
  1339. BufferLength - length of the above
  1340. pBytesWritten - place to return length returned
  1341. Return Value:
  1342. NDIS_STATUS_SUCCESS if we successfully queried the OID.
  1343. NDIS_STATUS_XXX error code otherwise.
  1344. --*/
  1345. {
  1346. NDIS_STATUS Status;
  1347. PNDISPROT_QUERY_OID pQuery;
  1348. NDIS_OID Oid;
  1349. Oid = 0;
  1350. do
  1351. {
  1352. if (BufferLength < sizeof(NDISPROT_QUERY_OID))
  1353. {
  1354. Status = NDIS_STATUS_BUFFER_TOO_SHORT;
  1355. break;
  1356. }
  1357. pQuery = (PNDISPROT_QUERY_OID)pDataBuffer;
  1358. Oid = pQuery->Oid;
  1359. NPROT_ACQUIRE_LOCK(&pOpenContext->Lock);
  1360. if (!NPROT_TEST_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_ACTIVE))
  1361. {
  1362. DEBUGP(DL_WARN,
  1363. ("QueryOid: Open %p/%x is in invalid state\n",
  1364. pOpenContext, pOpenContext->Flags));
  1365. NPROT_RELEASE_LOCK(&pOpenContext->Lock);
  1366. Status = NDIS_STATUS_FAILURE;
  1367. break;
  1368. }
  1369. //
  1370. // Make sure the binding doesn't go away.
  1371. //
  1372. NdisInterlockedIncrement((PLONG)&pOpenContext->PendedSendCount);
  1373. NPROT_RELEASE_LOCK(&pOpenContext->Lock);
  1374. Status = ndisprotDoRequest(
  1375. pOpenContext,
  1376. NdisRequestQueryInformation,
  1377. Oid,
  1378. &pQuery->Data[0],
  1379. BufferLength - FIELD_OFFSET(NDISPROT_QUERY_OID, Data),
  1380. pBytesWritten);
  1381. NPROT_ACQUIRE_LOCK(&pOpenContext->Lock);
  1382. NdisInterlockedDecrement((PLONG)&pOpenContext->PendedSendCount);
  1383. NPROT_RELEASE_LOCK(&pOpenContext->Lock);
  1384. if (Status == NDIS_STATUS_SUCCESS)
  1385. {
  1386. *pBytesWritten += FIELD_OFFSET(NDISPROT_QUERY_OID, Data);
  1387. }
  1388. }
  1389. while (FALSE);
  1390. DEBUGP(DL_LOUD, ("QueryOid: Open %p/%x, OID %x, Status %x\n",
  1391. pOpenContext, pOpenContext->Flags, Oid, Status));
  1392. return (Status);
  1393. }
  1394. NDIS_STATUS
  1395. ndisprotSetOidValue(
  1396. IN PNDISPROT_OPEN_CONTEXT pOpenContext,
  1397. OUT PVOID pDataBuffer,
  1398. IN ULONG BufferLength
  1399. )
  1400. /*++
  1401. Routine Description:
  1402. Set an arbitrary OID value to the miniport.
  1403. Arguments:
  1404. pOpenContext - pointer to open context representing our binding to the miniport
  1405. pDataBuffer - buffer that contains the value to be set
  1406. BufferLength - length of the above
  1407. Return Value:
  1408. NDIS_STATUS_SUCCESS if we successfully set the OID
  1409. NDIS_STATUS_XXX error code otherwise.
  1410. --*/
  1411. {
  1412. NDIS_STATUS Status;
  1413. PNDISPROT_SET_OID pSet;
  1414. NDIS_OID Oid;
  1415. ULONG BytesWritten;
  1416. Oid = 0;
  1417. do
  1418. {
  1419. if (BufferLength < sizeof(NDISPROT_SET_OID))
  1420. {
  1421. Status = NDIS_STATUS_BUFFER_TOO_SHORT;
  1422. break;
  1423. }
  1424. pSet = (PNDISPROT_SET_OID)pDataBuffer;
  1425. Oid = pSet->Oid;
  1426. NPROT_ACQUIRE_LOCK(&pOpenContext->Lock);
  1427. if (!NPROT_TEST_FLAGS(pOpenContext->Flags, NUIOO_BIND_FLAGS, NUIOO_BIND_ACTIVE))
  1428. {
  1429. DEBUGP(DL_WARN,
  1430. ("SetOid: Open %p/%x is in invalid state\n",
  1431. pOpenContext, pOpenContext->Flags));
  1432. NPROT_RELEASE_LOCK(&pOpenContext->Lock);
  1433. Status = NDIS_STATUS_FAILURE;
  1434. break;
  1435. }
  1436. //
  1437. // Make sure the binding doesn't go away.
  1438. //
  1439. NdisInterlockedIncrement((PLONG)&pOpenContext->PendedSendCount);
  1440. NPROT_RELEASE_LOCK(&pOpenContext->Lock);
  1441. Status = ndisprotDoRequest(
  1442. pOpenContext,
  1443. NdisRequestSetInformation,
  1444. Oid,
  1445. &pSet->Data[0],
  1446. BufferLength - FIELD_OFFSET(NDISPROT_SET_OID, Data),
  1447. &BytesWritten);
  1448. NPROT_ACQUIRE_LOCK(&pOpenContext->Lock);
  1449. NdisInterlockedDecrement((PLONG)&pOpenContext->PendedSendCount);
  1450. NPROT_RELEASE_LOCK(&pOpenContext->Lock);
  1451. }
  1452. while (FALSE);
  1453. DEBUGP(DL_LOUD, ("SetOid: Open %p/%x, OID %x, Status %x\n",
  1454. pOpenContext, pOpenContext->Flags, Oid, Status));
  1455. return (Status);
  1456. }