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.

1511 lines
44 KiB

  1. /*++
  2. Copyright(c) 1992-2000 Microsoft Corporation
  3. Module Name:
  4. protocol.c
  5. Abstract:
  6. Ndis Intermediate Miniport driver sample. This is a passthru driver.
  7. Author:
  8. Environment:
  9. Revision History:
  10. --*/
  11. #include "precomp.h"
  12. #pragma hdrstop
  13. #define MAX_PACKET_POOL_SIZE 0x0000FFFF
  14. #define MIN_PACKET_POOL_SIZE 0x000000FF
  15. VOID
  16. PtBindAdapter(
  17. OUT PNDIS_STATUS Status,
  18. IN NDIS_HANDLE BindContext,
  19. IN PNDIS_STRING DeviceName,
  20. IN PVOID SystemSpecific1,
  21. IN PVOID SystemSpecific2
  22. )
  23. /*++
  24. Routine Description:
  25. Called by NDIS to bind to a miniport below.
  26. Arguments:
  27. Status - Return status of bind here.
  28. BindContext - Can be passed to NdisCompleteBindAdapter if this call is pended.
  29. DeviceName - Device name to bind to. This is passed to NdisOpenAdapter.
  30. SystemSpecific1 - Can be passed to NdisOpenProtocolConfiguration to read per-binding information
  31. SystemSpecific2 - Unused
  32. Return Value:
  33. NDIS_STATUS_PENDING if this call is pended. In this case call NdisCompleteBindAdapter
  34. to complete.
  35. Anything else Completes this call synchronously
  36. --*/
  37. {
  38. NDIS_HANDLE ConfigHandle = NULL;
  39. PNDIS_CONFIGURATION_PARAMETER Param;
  40. NDIS_STRING DeviceStr = NDIS_STRING_CONST("UpperBindings");
  41. PADAPT pAdapt = NULL;
  42. NDIS_STATUS Sts;
  43. UINT MediumIndex;
  44. ULONG TotalSize;
  45. UNREFERENCED_PARAMETER(BindContext);
  46. UNREFERENCED_PARAMETER(SystemSpecific2);
  47. DBGPRINT(("==> Protocol BindAdapter\n"));
  48. do
  49. {
  50. //
  51. // Access the configuration section for our binding-specific
  52. // parameters.
  53. //
  54. NdisOpenProtocolConfiguration(Status,
  55. &ConfigHandle,
  56. SystemSpecific1);
  57. if (*Status != NDIS_STATUS_SUCCESS)
  58. {
  59. break;
  60. }
  61. //
  62. // Read the "UpperBindings" reserved key that contains a list
  63. // of device names representing our miniport instances corresponding
  64. // to this lower binding. Since this is a 1:1 IM driver, this key
  65. // contains exactly one name.
  66. //
  67. // If we want to implement a N:1 mux driver (N adapter instances
  68. // over a single lower binding), then UpperBindings will be a
  69. // MULTI_SZ containing a list of device names - we would loop through
  70. // this list, calling NdisIMInitializeDeviceInstanceEx once for
  71. // each name in it.
  72. //
  73. NdisReadConfiguration(Status,
  74. &Param,
  75. ConfigHandle,
  76. &DeviceStr,
  77. NdisParameterString);
  78. if (*Status != NDIS_STATUS_SUCCESS)
  79. {
  80. break;
  81. }
  82. //
  83. // Allocate memory for the Adapter structure. This represents both the
  84. // protocol context as well as the adapter structure when the miniport
  85. // is initialized.
  86. //
  87. // In addition to the base structure, allocate space for the device
  88. // instance string.
  89. //
  90. TotalSize = sizeof(ADAPT) + Param->ParameterData.StringData.MaximumLength;
  91. NdisAllocateMemoryWithTag(&pAdapt, TotalSize, TAG);
  92. if (pAdapt == NULL)
  93. {
  94. *Status = NDIS_STATUS_RESOURCES;
  95. break;
  96. }
  97. //
  98. // Initialize the adapter structure. We copy in the IM device
  99. // name as well, because we may need to use it in a call to
  100. // NdisIMCancelInitializeDeviceInstance. The string returned
  101. // by NdisReadConfiguration is active (i.e. available) only
  102. // for the duration of this call to our BindAdapter handler.
  103. //
  104. NdisZeroMemory(pAdapt, TotalSize);
  105. pAdapt->DeviceName.MaximumLength = Param->ParameterData.StringData.MaximumLength;
  106. pAdapt->DeviceName.Length = Param->ParameterData.StringData.Length;
  107. pAdapt->DeviceName.Buffer = (PWCHAR)((ULONG_PTR)pAdapt + sizeof(ADAPT));
  108. NdisMoveMemory(pAdapt->DeviceName.Buffer,
  109. Param->ParameterData.StringData.Buffer,
  110. Param->ParameterData.StringData.MaximumLength);
  111. NdisInitializeEvent(&pAdapt->Event);
  112. NdisAllocateSpinLock(&pAdapt->Lock);
  113. //
  114. // Allocate a packet pool for sends. We need this to pass sends down.
  115. // We cannot use the same packet descriptor that came down to our send
  116. // handler (see also NDIS 5.1 packet stacking).
  117. //
  118. NdisAllocatePacketPoolEx(Status,
  119. &pAdapt->SendPacketPoolHandle,
  120. MIN_PACKET_POOL_SIZE,
  121. MAX_PACKET_POOL_SIZE - MIN_PACKET_POOL_SIZE,
  122. sizeof(SEND_RSVD));
  123. if (*Status != NDIS_STATUS_SUCCESS)
  124. {
  125. break;
  126. }
  127. //
  128. // Allocate a packet pool for receives. We need this to indicate receives.
  129. // Same consideration as sends (see also NDIS 5.1 packet stacking).
  130. //
  131. NdisAllocatePacketPoolEx(Status,
  132. &pAdapt->RecvPacketPoolHandle,
  133. MIN_PACKET_POOL_SIZE,
  134. MAX_PACKET_POOL_SIZE - MIN_PACKET_POOL_SIZE,
  135. PROTOCOL_RESERVED_SIZE_IN_PACKET);
  136. if (*Status != NDIS_STATUS_SUCCESS)
  137. {
  138. break;
  139. }
  140. //
  141. // Now open the adapter below and complete the initialization
  142. //
  143. NdisOpenAdapter(Status,
  144. &Sts,
  145. &pAdapt->BindingHandle,
  146. &MediumIndex,
  147. MediumArray,
  148. sizeof(MediumArray)/sizeof(NDIS_MEDIUM),
  149. ProtHandle,
  150. pAdapt,
  151. DeviceName,
  152. 0,
  153. NULL);
  154. if (*Status == NDIS_STATUS_PENDING)
  155. {
  156. NdisWaitEvent(&pAdapt->Event, 0);
  157. *Status = pAdapt->Status;
  158. }
  159. if (*Status != NDIS_STATUS_SUCCESS)
  160. {
  161. break;
  162. }
  163. pAdapt->Medium = MediumArray[MediumIndex];
  164. //
  165. // Now ask NDIS to initialize our miniport (upper) edge.
  166. // Set the flag below to synchronize with a possible call
  167. // to our protocol Unbind handler that may come in before
  168. // our miniport initialization happens.
  169. //
  170. pAdapt->MiniportInitPending = TRUE;
  171. NdisInitializeEvent(&pAdapt->MiniportInitEvent);
  172. *Status = NdisIMInitializeDeviceInstanceEx(DriverHandle,
  173. &pAdapt->DeviceName,
  174. pAdapt);
  175. if (*Status != NDIS_STATUS_SUCCESS)
  176. {
  177. DBGPRINT(("BindAdapter: Adapt %p, IMInitializeDeviceInstance error %x\n",
  178. pAdapt, *Status));
  179. break;
  180. }
  181. } while(FALSE);
  182. //
  183. // Close the configuration handle now - see comments above with
  184. // the call to NdisIMInitializeDeviceInstanceEx.
  185. //
  186. if (ConfigHandle != NULL)
  187. {
  188. NdisCloseConfiguration(ConfigHandle);
  189. }
  190. if (*Status != NDIS_STATUS_SUCCESS)
  191. {
  192. if (pAdapt != NULL)
  193. {
  194. if (pAdapt->BindingHandle != NULL)
  195. {
  196. NDIS_STATUS LocalStatus;
  197. //
  198. // Close the binding we opened above.
  199. //
  200. NdisResetEvent(&pAdapt->Event);
  201. NdisCloseAdapter(&LocalStatus, pAdapt->BindingHandle);
  202. pAdapt->BindingHandle = NULL;
  203. if (LocalStatus == NDIS_STATUS_PENDING)
  204. {
  205. NdisWaitEvent(&pAdapt->Event, 0);
  206. LocalStatus = pAdapt->Status;
  207. }
  208. }
  209. if (pAdapt->SendPacketPoolHandle != NULL)
  210. {
  211. NdisFreePacketPool(pAdapt->SendPacketPoolHandle);
  212. }
  213. if (pAdapt->RecvPacketPoolHandle != NULL)
  214. {
  215. NdisFreePacketPool(pAdapt->RecvPacketPoolHandle);
  216. }
  217. NdisFreeMemory(pAdapt, 0, 0);
  218. pAdapt = NULL;
  219. }
  220. }
  221. DBGPRINT(("<== Protocol BindAdapter: pAdapt %p, Status %x\n", pAdapt, *Status));
  222. }
  223. VOID
  224. PtOpenAdapterComplete(
  225. IN NDIS_HANDLE ProtocolBindingContext,
  226. IN NDIS_STATUS Status,
  227. IN NDIS_STATUS OpenErrorStatus
  228. )
  229. /*++
  230. Routine Description:
  231. Completion routine for NdisOpenAdapter issued from within the PtBindAdapter. Simply
  232. unblock the caller.
  233. Arguments:
  234. ProtocolBindingContext Pointer to the adapter
  235. Status Status of the NdisOpenAdapter call
  236. OpenErrorStatus Secondary status(ignored by us).
  237. Return Value:
  238. None
  239. --*/
  240. {
  241. PADAPT pAdapt =(PADAPT)ProtocolBindingContext;
  242. UNREFERENCED_PARAMETER(OpenErrorStatus);
  243. DBGPRINT(("==> PtOpenAdapterComplete: Adapt %p, Status %x\n", pAdapt, Status));
  244. pAdapt->Status = Status;
  245. NdisSetEvent(&pAdapt->Event);
  246. }
  247. VOID
  248. PtUnbindAdapter(
  249. OUT PNDIS_STATUS Status,
  250. IN NDIS_HANDLE ProtocolBindingContext,
  251. IN NDIS_HANDLE UnbindContext
  252. )
  253. /*++
  254. Routine Description:
  255. Called by NDIS when we are required to unbind to the adapter below.
  256. This functions shares functionality with the miniport's HaltHandler.
  257. The code should ensure that NdisCloseAdapter and NdisFreeMemory is called
  258. only once between the two functions
  259. Arguments:
  260. Status Placeholder for return status
  261. ProtocolBindingContext Pointer to the adapter structure
  262. UnbindContext Context for NdisUnbindComplete() if this pends
  263. Return Value:
  264. Status for NdisIMDeinitializeDeviceContext
  265. --*/
  266. {
  267. PADAPT pAdapt =(PADAPT)ProtocolBindingContext;
  268. NDIS_STATUS LocalStatus;
  269. UNREFERENCED_PARAMETER(UnbindContext);
  270. DBGPRINT(("==> PtUnbindAdapter: Adapt %p\n", pAdapt));
  271. //
  272. // Set the flag that the miniport below is unbinding, so the request handlers will
  273. // fail any request comming later
  274. //
  275. NdisAcquireSpinLock(&pAdapt->Lock);
  276. pAdapt->UnbindingInProcess = TRUE;
  277. if (pAdapt->QueuedRequest == TRUE)
  278. {
  279. pAdapt->QueuedRequest = FALSE;
  280. NdisReleaseSpinLock(&pAdapt->Lock);
  281. PtRequestComplete(pAdapt,
  282. &pAdapt->Request,
  283. NDIS_STATUS_FAILURE );
  284. }
  285. else
  286. {
  287. NdisReleaseSpinLock(&pAdapt->Lock);
  288. }
  289. #ifndef WIN9X
  290. //
  291. // Check if we had called NdisIMInitializeDeviceInstanceEx and
  292. // we are awaiting a call to MiniportInitialize.
  293. //
  294. if (pAdapt->MiniportInitPending == TRUE)
  295. {
  296. //
  297. // Try to cancel the pending IMInit process.
  298. //
  299. LocalStatus = NdisIMCancelInitializeDeviceInstance(
  300. DriverHandle,
  301. &pAdapt->DeviceName);
  302. if (LocalStatus == NDIS_STATUS_SUCCESS)
  303. {
  304. //
  305. // Successfully cancelled IM Initialization; our
  306. // Miniport Initialize routine will not be called
  307. // for this device.
  308. //
  309. pAdapt->MiniportInitPending = FALSE;
  310. ASSERT(pAdapt->MiniportHandle == NULL);
  311. }
  312. else
  313. {
  314. //
  315. // Our Miniport Initialize routine will be called
  316. // (may be running on another thread at this time).
  317. // Wait for it to finish.
  318. //
  319. NdisWaitEvent(&pAdapt->MiniportInitEvent, 0);
  320. ASSERT(pAdapt->MiniportInitPending == FALSE);
  321. }
  322. }
  323. #endif // !WIN9X
  324. //
  325. // Call NDIS to remove our device-instance. We do most of the work
  326. // inside the HaltHandler.
  327. //
  328. // The Handle will be NULL if our miniport Halt Handler has been called or
  329. // if the IM device was never initialized
  330. //
  331. if (pAdapt->MiniportHandle != NULL)
  332. {
  333. *Status = NdisIMDeInitializeDeviceInstance(pAdapt->MiniportHandle);
  334. if (*Status != NDIS_STATUS_SUCCESS)
  335. {
  336. *Status = NDIS_STATUS_FAILURE;
  337. }
  338. }
  339. else
  340. {
  341. //
  342. // We need to do some work here.
  343. // Close the binding below us
  344. // and release the memory allocated.
  345. //
  346. if(pAdapt->BindingHandle != NULL)
  347. {
  348. NdisResetEvent(&pAdapt->Event);
  349. NdisCloseAdapter(Status, pAdapt->BindingHandle);
  350. //
  351. // Wait for it to complete
  352. //
  353. if(*Status == NDIS_STATUS_PENDING)
  354. {
  355. NdisWaitEvent(&pAdapt->Event, 0);
  356. *Status = pAdapt->Status;
  357. }
  358. pAdapt->BindingHandle = NULL;
  359. }
  360. else
  361. {
  362. //
  363. // Both Our MiniportHandle and Binding Handle should not be NULL.
  364. //
  365. *Status = NDIS_STATUS_FAILURE;
  366. ASSERT(0);
  367. }
  368. //
  369. // Free the memory here, if was not released earlier(by calling the HaltHandler)
  370. //
  371. NdisFreeMemory(pAdapt, 0, 0);
  372. }
  373. DBGPRINT(("<== PtUnbindAdapter: Adapt %p\n", pAdapt));
  374. }
  375. VOID
  376. PtUnloadProtocol(
  377. VOID
  378. )
  379. {
  380. NDIS_STATUS Status;
  381. if (ProtHandle != NULL)
  382. {
  383. NdisDeregisterProtocol(&Status, ProtHandle);
  384. ProtHandle = NULL;
  385. }
  386. DBGPRINT(("PtUnloadProtocol: done!\n"));
  387. }
  388. VOID
  389. PtCloseAdapterComplete(
  390. IN NDIS_HANDLE ProtocolBindingContext,
  391. IN NDIS_STATUS Status
  392. )
  393. /*++
  394. Routine Description:
  395. Completion for the CloseAdapter call.
  396. Arguments:
  397. ProtocolBindingContext Pointer to the adapter structure
  398. Status Completion status
  399. Return Value:
  400. None.
  401. --*/
  402. {
  403. PADAPT pAdapt =(PADAPT)ProtocolBindingContext;
  404. DBGPRINT(("CloseAdapterComplete: Adapt %p, Status %x\n", pAdapt, Status));
  405. pAdapt->Status = Status;
  406. NdisSetEvent(&pAdapt->Event);
  407. }
  408. VOID
  409. PtResetComplete(
  410. IN NDIS_HANDLE ProtocolBindingContext,
  411. IN NDIS_STATUS Status
  412. )
  413. /*++
  414. Routine Description:
  415. Completion for the reset.
  416. Arguments:
  417. ProtocolBindingContext Pointer to the adapter structure
  418. Status Completion status
  419. Return Value:
  420. None.
  421. --*/
  422. {
  423. UNREFERENCED_PARAMETER(ProtocolBindingContext);
  424. UNREFERENCED_PARAMETER(Status);
  425. //
  426. // We never issue a reset, so we should not be here.
  427. //
  428. ASSERT(0);
  429. }
  430. VOID
  431. PtRequestComplete(
  432. IN NDIS_HANDLE ProtocolBindingContext,
  433. IN PNDIS_REQUEST NdisRequest,
  434. IN NDIS_STATUS Status
  435. )
  436. /*++
  437. Routine Description:
  438. Completion handler for the previously posted request. All OIDS
  439. are completed by and sent to the same miniport that they were requested for.
  440. If Oid == OID_PNP_QUERY_POWER then the data structure needs to returned with all entries =
  441. NdisDeviceStateUnspecified
  442. Arguments:
  443. ProtocolBindingContext Pointer to the adapter structure
  444. NdisRequest The posted request
  445. Status Completion status
  446. Return Value:
  447. None
  448. --*/
  449. {
  450. PADAPT pAdapt = (PADAPT)ProtocolBindingContext;
  451. NDIS_OID Oid = pAdapt->Request.DATA.SET_INFORMATION.Oid ;
  452. //
  453. // Since our request is not outstanding anymore
  454. //
  455. ASSERT(pAdapt->OutstandingRequests == TRUE);
  456. pAdapt->OutstandingRequests = FALSE;
  457. //
  458. // Complete the Set or Query, and fill in the buffer for OID_PNP_CAPABILITIES, if need be.
  459. //
  460. switch (NdisRequest->RequestType)
  461. {
  462. case NdisRequestQueryInformation:
  463. //
  464. // We never pass OID_PNP_QUERY_POWER down.
  465. //
  466. ASSERT(Oid != OID_PNP_QUERY_POWER);
  467. if ((Oid == OID_PNP_CAPABILITIES) && (Status == NDIS_STATUS_SUCCESS))
  468. {
  469. MPQueryPNPCapabilities(pAdapt, &Status);
  470. }
  471. *pAdapt->BytesReadOrWritten = NdisRequest->DATA.QUERY_INFORMATION.BytesWritten;
  472. *pAdapt->BytesNeeded = NdisRequest->DATA.QUERY_INFORMATION.BytesNeeded;
  473. if ((Oid == OID_GEN_MAC_OPTIONS) && (Status == NDIS_STATUS_SUCCESS))
  474. {
  475. //
  476. // Remove the no-loopback bit from mac-options. In essence we are
  477. // telling NDIS that we can handle loopback. We don't, but the
  478. // interface below us does. If we do not do this, then loopback
  479. // processing happens both below us and above us. This is wasteful
  480. // at best and if Netmon is running, it will see multiple copies
  481. // of loopback packets when sniffing above us.
  482. //
  483. // Only the lowest miniport is a stack of layered miniports should
  484. // ever report this bit set to NDIS.
  485. //
  486. *(PULONG)NdisRequest->DATA.QUERY_INFORMATION.InformationBuffer &= ~NDIS_MAC_OPTION_NO_LOOPBACK;
  487. }
  488. NdisMQueryInformationComplete(pAdapt->MiniportHandle,
  489. Status);
  490. break;
  491. case NdisRequestSetInformation:
  492. ASSERT( Oid != OID_PNP_SET_POWER);
  493. *pAdapt->BytesReadOrWritten = NdisRequest->DATA.SET_INFORMATION.BytesRead;
  494. *pAdapt->BytesNeeded = NdisRequest->DATA.SET_INFORMATION.BytesNeeded;
  495. NdisMSetInformationComplete(pAdapt->MiniportHandle,
  496. Status);
  497. break;
  498. default:
  499. ASSERT(0);
  500. break;
  501. }
  502. }
  503. VOID
  504. PtStatus(
  505. IN NDIS_HANDLE ProtocolBindingContext,
  506. IN NDIS_STATUS GeneralStatus,
  507. IN PVOID StatusBuffer,
  508. IN UINT StatusBufferSize
  509. )
  510. /*++
  511. Routine Description:
  512. Status handler for the lower-edge(protocol).
  513. Arguments:
  514. ProtocolBindingContext Pointer to the adapter structure
  515. GeneralStatus Status code
  516. StatusBuffer Status buffer
  517. StatusBufferSize Size of the status buffer
  518. Return Value:
  519. None
  520. --*/
  521. {
  522. PADAPT pAdapt = (PADAPT)ProtocolBindingContext;
  523. //
  524. // Pass up this indication only if the upper edge miniport is initialized
  525. // and powered on. Also ignore indications that might be sent by the lower
  526. // miniport when it isn't at D0.
  527. //
  528. if ((pAdapt->MiniportHandle != NULL) &&
  529. (pAdapt->MPDeviceState == NdisDeviceStateD0) &&
  530. (pAdapt->PTDeviceState == NdisDeviceStateD0))
  531. {
  532. if ((GeneralStatus == NDIS_STATUS_MEDIA_CONNECT) ||
  533. (GeneralStatus == NDIS_STATUS_MEDIA_DISCONNECT))
  534. {
  535. pAdapt->LastIndicatedStatus = GeneralStatus;
  536. }
  537. NdisMIndicateStatus(pAdapt->MiniportHandle,
  538. GeneralStatus,
  539. StatusBuffer,
  540. StatusBufferSize);
  541. }
  542. //
  543. // Save the last indicated media status
  544. //
  545. else
  546. {
  547. if ((pAdapt->MiniportHandle != NULL) &&
  548. ((GeneralStatus == NDIS_STATUS_MEDIA_CONNECT) ||
  549. (GeneralStatus == NDIS_STATUS_MEDIA_DISCONNECT)))
  550. {
  551. pAdapt->LatestUnIndicateStatus = GeneralStatus;
  552. }
  553. }
  554. }
  555. VOID
  556. PtStatusComplete(
  557. IN NDIS_HANDLE ProtocolBindingContext
  558. )
  559. /*++
  560. Routine Description:
  561. Arguments:
  562. Return Value:
  563. --*/
  564. {
  565. PADAPT pAdapt = (PADAPT)ProtocolBindingContext;
  566. //
  567. // Pass up this indication only if the upper edge miniport is initialized
  568. // and powered on. Also ignore indications that might be sent by the lower
  569. // miniport when it isn't at D0.
  570. //
  571. if ((pAdapt->MiniportHandle != NULL) &&
  572. (pAdapt->MPDeviceState == NdisDeviceStateD0) &&
  573. (pAdapt->PTDeviceState == NdisDeviceStateD0))
  574. {
  575. NdisMIndicateStatusComplete(pAdapt->MiniportHandle);
  576. }
  577. }
  578. VOID
  579. PtSendComplete(
  580. IN NDIS_HANDLE ProtocolBindingContext,
  581. IN PNDIS_PACKET Packet,
  582. IN NDIS_STATUS Status
  583. )
  584. /*++
  585. Routine Description:
  586. Called by NDIS when the miniport below had completed a send. We should
  587. complete the corresponding upper-edge send this represents.
  588. Arguments:
  589. ProtocolBindingContext - Points to ADAPT structure
  590. Packet - Low level packet being completed
  591. Status - status of send
  592. Return Value:
  593. None
  594. --*/
  595. {
  596. PADAPT pAdapt = (PADAPT)ProtocolBindingContext;
  597. PNDIS_PACKET Pkt;
  598. NDIS_HANDLE PoolHandle;
  599. #ifdef NDIS51
  600. //
  601. // Packet stacking:
  602. //
  603. // Determine if the packet we are completing is the one we allocated. If so, then
  604. // get the original packet from the reserved area and completed it and free the
  605. // allocated packet. If this is the packet that was sent down to us, then just
  606. // complete it
  607. //
  608. PoolHandle = NdisGetPoolFromPacket(Packet);
  609. if (PoolHandle != pAdapt->SendPacketPoolHandle)
  610. {
  611. //
  612. // We had passed down a packet belonging to the protocol above us.
  613. //
  614. // DBGPRINT(("PtSendComp: Adapt %p, Stacked Packet %p\n", pAdapt, Packet));
  615. NdisMSendComplete(pAdapt->MiniportHandle,
  616. Packet,
  617. Status);
  618. }
  619. else
  620. #endif // NDIS51
  621. {
  622. PSEND_RSVD SendRsvd;
  623. SendRsvd = (PSEND_RSVD)(Packet->ProtocolReserved);
  624. Pkt = SendRsvd->OriginalPkt;
  625. #ifndef WIN9X
  626. NdisIMCopySendCompletePerPacketInfo (Pkt, Packet);
  627. #endif
  628. NdisDprFreePacket(Packet);
  629. NdisMSendComplete(pAdapt->MiniportHandle,
  630. Pkt,
  631. Status);
  632. }
  633. //
  634. // Decrease the outstanding send count
  635. //
  636. ADAPT_DECR_PENDING_SENDS(pAdapt);
  637. }
  638. VOID
  639. PtTransferDataComplete(
  640. IN NDIS_HANDLE ProtocolBindingContext,
  641. IN PNDIS_PACKET Packet,
  642. IN NDIS_STATUS Status,
  643. IN UINT BytesTransferred
  644. )
  645. /*++
  646. Routine Description:
  647. Entry point called by NDIS to indicate completion of a call by us
  648. to NdisTransferData.
  649. See notes under SendComplete.
  650. Arguments:
  651. Return Value:
  652. --*/
  653. {
  654. PADAPT pAdapt =(PADAPT)ProtocolBindingContext;
  655. if(pAdapt->MiniportHandle)
  656. {
  657. NdisMTransferDataComplete(pAdapt->MiniportHandle,
  658. Packet,
  659. Status,
  660. BytesTransferred);
  661. }
  662. }
  663. NDIS_STATUS
  664. PtReceive(
  665. IN NDIS_HANDLE ProtocolBindingContext,
  666. IN NDIS_HANDLE MacReceiveContext,
  667. IN PVOID HeaderBuffer,
  668. IN UINT HeaderBufferSize,
  669. IN PVOID LookAheadBuffer,
  670. IN UINT LookAheadBufferSize,
  671. IN UINT PacketSize
  672. )
  673. /*++
  674. Routine Description:
  675. Handle receive data indicated up by the miniport below. We pass
  676. it along to the protocol above us.
  677. If the miniport below indicates packets, NDIS would more
  678. likely call us at our ReceivePacket handler. However we
  679. might be called here in certain situations even though
  680. the miniport below has indicated a receive packet, e.g.
  681. if the miniport had set packet status to NDIS_STATUS_RESOURCES.
  682. Arguments:
  683. <see DDK ref page for ProtocolReceive>
  684. Return Value:
  685. NDIS_STATUS_SUCCESS if we processed the receive successfully,
  686. NDIS_STATUS_XXX error code if we discarded it.
  687. --*/
  688. {
  689. PADAPT pAdapt = (PADAPT)ProtocolBindingContext;
  690. PNDIS_PACKET MyPacket, Packet;
  691. NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
  692. if ((!pAdapt->MiniportHandle) || (pAdapt->MPDeviceState > NdisDeviceStateD0))
  693. {
  694. Status = NDIS_STATUS_FAILURE;
  695. }
  696. else do
  697. {
  698. //
  699. // Get at the packet, if any, indicated up by the miniport below.
  700. //
  701. Packet = NdisGetReceivedPacket(pAdapt->BindingHandle, MacReceiveContext);
  702. if (Packet != NULL)
  703. {
  704. //
  705. // The miniport below did indicate up a packet. Use information
  706. // from that packet to construct a new packet to indicate up.
  707. //
  708. #ifdef NDIS51
  709. //
  710. // NDIS 5.1 NOTE: Do not reuse the original packet in indicating
  711. // up a receive, even if there is sufficient packet stack space.
  712. // If we had to do so, we would have had to overwrite the
  713. // status field in the original packet to NDIS_STATUS_RESOURCES,
  714. // and it is not allowed for protocols to overwrite this field
  715. // in received packets.
  716. //
  717. #endif // NDIS51
  718. //
  719. // Get a packet off the pool and indicate that up
  720. //
  721. NdisDprAllocatePacket(&Status,
  722. &MyPacket,
  723. pAdapt->RecvPacketPoolHandle);
  724. if (Status == NDIS_STATUS_SUCCESS)
  725. {
  726. //
  727. // Make our packet point to data from the original
  728. // packet. NOTE: this works only because we are
  729. // indicating a receive directly from the context of
  730. // our receive indication. If we need to queue this
  731. // packet and indicate it from another thread context,
  732. // we will also have to allocate a new buffer and copy
  733. // over the packet contents, OOB data and per-packet
  734. // information. This is because the packet data
  735. // is available only for the duration of this
  736. // receive indication call.
  737. //
  738. MyPacket->Private.Head = Packet->Private.Head;
  739. MyPacket->Private.Tail = Packet->Private.Tail;
  740. //
  741. // Get the original packet (it could be the same packet as the
  742. // one received or a different one based on the number of layered
  743. // miniports below) and set it on the indicated packet so the OOB
  744. // data is visible correctly at protocols above.
  745. //
  746. NDIS_SET_ORIGINAL_PACKET(MyPacket, NDIS_GET_ORIGINAL_PACKET(Packet));
  747. NDIS_SET_PACKET_HEADER_SIZE(MyPacket, HeaderBufferSize);
  748. //
  749. // Copy packet flags.
  750. //
  751. NdisGetPacketFlags(MyPacket) = NdisGetPacketFlags(Packet);
  752. //
  753. // Force protocols above to make a copy if they want to hang
  754. // on to data in this packet. This is because we are in our
  755. // Receive handler (not ReceivePacket) and we can't return a
  756. // ref count from here.
  757. //
  758. NDIS_SET_PACKET_STATUS(MyPacket, NDIS_STATUS_RESOURCES);
  759. //
  760. // By setting NDIS_STATUS_RESOURCES, we also know that we can reclaim
  761. // this packet as soon as the call to NdisMIndicateReceivePacket
  762. // returns.
  763. //
  764. NdisMIndicateReceivePacket(pAdapt->MiniportHandle, &MyPacket, 1);
  765. //
  766. // Reclaim the indicated packet. Since we had set its status
  767. // to NDIS_STATUS_RESOURCES, we are guaranteed that protocols
  768. // above are done with it.
  769. //
  770. NdisDprFreePacket(MyPacket);
  771. break;
  772. }
  773. }
  774. else
  775. {
  776. //
  777. // The miniport below us uses the old-style (not packet)
  778. // receive indication. Fall through.
  779. //
  780. }
  781. //
  782. // Fall through if the miniport below us has either not
  783. // indicated a packet or we could not allocate one
  784. //
  785. pAdapt->IndicateRcvComplete = TRUE;
  786. switch (pAdapt->Medium)
  787. {
  788. case NdisMedium802_3:
  789. case NdisMediumWan:
  790. NdisMEthIndicateReceive(pAdapt->MiniportHandle,
  791. MacReceiveContext,
  792. HeaderBuffer,
  793. HeaderBufferSize,
  794. LookAheadBuffer,
  795. LookAheadBufferSize,
  796. PacketSize);
  797. break;
  798. case NdisMedium802_5:
  799. NdisMTrIndicateReceive(pAdapt->MiniportHandle,
  800. MacReceiveContext,
  801. HeaderBuffer,
  802. HeaderBufferSize,
  803. LookAheadBuffer,
  804. LookAheadBufferSize,
  805. PacketSize);
  806. break;
  807. case NdisMediumFddi:
  808. NdisMFddiIndicateReceive(pAdapt->MiniportHandle,
  809. MacReceiveContext,
  810. HeaderBuffer,
  811. HeaderBufferSize,
  812. LookAheadBuffer,
  813. LookAheadBufferSize,
  814. PacketSize);
  815. break;
  816. default:
  817. ASSERT(FALSE);
  818. break;
  819. }
  820. } while(FALSE);
  821. return Status;
  822. }
  823. VOID
  824. PtReceiveComplete(
  825. IN NDIS_HANDLE ProtocolBindingContext
  826. )
  827. /*++
  828. Routine Description:
  829. Called by the adapter below us when it is done indicating a batch of
  830. received packets.
  831. Arguments:
  832. ProtocolBindingContext Pointer to our adapter structure.
  833. Return Value:
  834. None
  835. --*/
  836. {
  837. PADAPT pAdapt =(PADAPT)ProtocolBindingContext;
  838. if (((pAdapt->MiniportHandle != NULL)
  839. && (pAdapt->MPDeviceState > NdisDeviceStateD0))
  840. && (pAdapt->IndicateRcvComplete))
  841. {
  842. switch (pAdapt->Medium)
  843. {
  844. case NdisMedium802_3:
  845. case NdisMediumWan:
  846. NdisMEthIndicateReceiveComplete(pAdapt->MiniportHandle);
  847. break;
  848. case NdisMedium802_5:
  849. NdisMTrIndicateReceiveComplete(pAdapt->MiniportHandle);
  850. break;
  851. case NdisMediumFddi:
  852. NdisMFddiIndicateReceiveComplete(pAdapt->MiniportHandle);
  853. break;
  854. default:
  855. ASSERT(FALSE);
  856. break;
  857. }
  858. }
  859. pAdapt->IndicateRcvComplete = FALSE;
  860. }
  861. INT
  862. PtReceivePacket(
  863. IN NDIS_HANDLE ProtocolBindingContext,
  864. IN PNDIS_PACKET Packet
  865. )
  866. /*++
  867. Routine Description:
  868. ReceivePacket handler. Called by NDIS if the miniport below supports
  869. NDIS 4.0 style receives. Re-package the buffer chain in a new packet
  870. and indicate the new packet to protocols above us. Any context for
  871. packets indicated up must be kept in the MiniportReserved field.
  872. NDIS 5.1 - packet stacking - if there is sufficient "stack space" in
  873. the packet passed to us, we can use the same packet in a receive
  874. indication.
  875. Arguments:
  876. ProtocolBindingContext - Pointer to our adapter structure.
  877. Packet - Pointer to the packet
  878. Return Value:
  879. == 0 -> We are done with the packet
  880. != 0 -> We will keep the packet and call NdisReturnPackets() this
  881. many times when done.
  882. --*/
  883. {
  884. PADAPT pAdapt =(PADAPT)ProtocolBindingContext;
  885. NDIS_STATUS Status;
  886. PNDIS_PACKET MyPacket;
  887. BOOLEAN Remaining;
  888. //
  889. // Drop the packet silently if the upper miniport edge isn't initialized or
  890. // the miniport edge is in low power state
  891. //
  892. if ((!pAdapt->MiniportHandle) || (pAdapt->MPDeviceState > NdisDeviceStateD0))
  893. {
  894. return 0;
  895. }
  896. #ifdef NDIS51
  897. //
  898. // Check if we can reuse the same packet for indicating up.
  899. // See also: PtReceive().
  900. //
  901. (VOID)NdisIMGetCurrentPacketStack(Packet, &Remaining);
  902. if (Remaining)
  903. {
  904. //
  905. // We can reuse "Packet". Indicate it up and be done with it.
  906. //
  907. Status = NDIS_GET_PACKET_STATUS(Packet);
  908. NdisMIndicateReceivePacket(pAdapt->MiniportHandle, &Packet, 1);
  909. return((Status != NDIS_STATUS_RESOURCES) ? 1 : 0);
  910. }
  911. #endif // NDIS51
  912. //
  913. // Get a packet off the pool and indicate that up
  914. //
  915. NdisDprAllocatePacket(&Status,
  916. &MyPacket,
  917. pAdapt->RecvPacketPoolHandle);
  918. if (Status == NDIS_STATUS_SUCCESS)
  919. {
  920. PRECV_RSVD RecvRsvd;
  921. RecvRsvd = (PRECV_RSVD)(MyPacket->MiniportReserved);
  922. RecvRsvd->OriginalPkt = Packet;
  923. MyPacket->Private.Head = Packet->Private.Head;
  924. MyPacket->Private.Tail = Packet->Private.Tail;
  925. //
  926. // Get the original packet (it could be the same packet as the one
  927. // received or a different one based on the number of layered miniports
  928. // below) and set it on the indicated packet so the OOB data is visible
  929. // correctly to protocols above us.
  930. //
  931. NDIS_SET_ORIGINAL_PACKET(MyPacket, NDIS_GET_ORIGINAL_PACKET(Packet));
  932. //
  933. // Set Packet Flags
  934. //
  935. NdisGetPacketFlags(MyPacket) = NdisGetPacketFlags(Packet);
  936. Status = NDIS_GET_PACKET_STATUS(Packet);
  937. NDIS_SET_PACKET_STATUS(MyPacket, Status);
  938. NDIS_SET_PACKET_HEADER_SIZE(MyPacket, NDIS_GET_PACKET_HEADER_SIZE(Packet));
  939. NdisMIndicateReceivePacket(pAdapt->MiniportHandle, &MyPacket, 1);
  940. //
  941. // Check if we had indicated up the packet with NDIS_STATUS_RESOURCES
  942. // NOTE -- do not use NDIS_GET_PACKET_STATUS(MyPacket) for this since
  943. // it might have changed! Use the value saved in the local variable.
  944. //
  945. if (Status == NDIS_STATUS_RESOURCES)
  946. {
  947. //
  948. // Our ReturnPackets handler will not be called for this packet.
  949. // We should reclaim it right here.
  950. //
  951. NdisDprFreePacket(MyPacket);
  952. }
  953. return((Status != NDIS_STATUS_RESOURCES) ? 1 : 0);
  954. }
  955. else
  956. {
  957. //
  958. // We are out of packets. Silently drop it.
  959. //
  960. return(0);
  961. }
  962. }
  963. NDIS_STATUS
  964. PtPNPHandler(
  965. IN NDIS_HANDLE ProtocolBindingContext,
  966. IN PNET_PNP_EVENT pNetPnPEvent
  967. )
  968. /*++
  969. Routine Description:
  970. This is called by NDIS to notify us of a PNP event related to a lower
  971. binding. Based on the event, this dispatches to other helper routines.
  972. NDIS 5.1: forward this event to the upper protocol(s) by calling
  973. NdisIMNotifyPnPEvent.
  974. Arguments:
  975. ProtocolBindingContext - Pointer to our adapter structure. Can be NULL
  976. for "global" notifications
  977. pNetPnPEvent - Pointer to the PNP event to be processed.
  978. Return Value:
  979. NDIS_STATUS code indicating status of event processing.
  980. --*/
  981. {
  982. PADAPT pAdapt =(PADAPT)ProtocolBindingContext;
  983. NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
  984. DBGPRINT(("PtPnPHandler: Adapt %p, Event %d\n", pAdapt, pNetPnPEvent->NetEvent));
  985. switch (pNetPnPEvent->NetEvent)
  986. {
  987. case NetEventSetPower:
  988. Status = PtPnPNetEventSetPower(pAdapt, pNetPnPEvent);
  989. break;
  990. case NetEventReconfigure:
  991. Status = PtPnPNetEventReconfigure(pAdapt, pNetPnPEvent);
  992. break;
  993. default:
  994. #ifdef NDIS51
  995. //
  996. // Pass on this notification to protocol(s) above, before
  997. // doing anything else with it.
  998. //
  999. if (pAdapt && pAdapt->MiniportHandle)
  1000. {
  1001. Status = NdisIMNotifyPnPEvent(pAdapt->MiniportHandle, pNetPnPEvent);
  1002. }
  1003. #else
  1004. Status = NDIS_STATUS_SUCCESS;
  1005. #endif // NDIS51
  1006. break;
  1007. }
  1008. return Status;
  1009. }
  1010. NDIS_STATUS
  1011. PtPnPNetEventReconfigure(
  1012. IN PADAPT pAdapt,
  1013. IN PNET_PNP_EVENT pNetPnPEvent
  1014. )
  1015. /*++
  1016. Routine Description:
  1017. This routine is called from NDIS to notify our protocol edge of a
  1018. reconfiguration of parameters for either a specific binding (pAdapt
  1019. is not NULL), or global parameters if any (pAdapt is NULL).
  1020. Arguments:
  1021. pAdapt - Pointer to our adapter structure.
  1022. pNetPnPEvent - the reconfigure event
  1023. Return Value:
  1024. NDIS_STATUS_SUCCESS
  1025. --*/
  1026. {
  1027. NDIS_STATUS ReconfigStatus = NDIS_STATUS_SUCCESS;
  1028. NDIS_STATUS ReturnStatus = NDIS_STATUS_SUCCESS;
  1029. do
  1030. {
  1031. //
  1032. // Is this is a global reconfiguration notification ?
  1033. //
  1034. if (pAdapt == NULL)
  1035. {
  1036. //
  1037. // An important event that causes this notification to us is if
  1038. // one of our upper-edge miniport instances was enabled after being
  1039. // disabled earlier, e.g. from Device Manager in Win2000. Note that
  1040. // NDIS calls this because we had set up an association between our
  1041. // miniport and protocol entities by calling NdisIMAssociateMiniport.
  1042. //
  1043. // Since we would have torn down the lower binding for that miniport,
  1044. // we need NDIS' assistance to re-bind to the lower miniport. The
  1045. // call to NdisReEnumerateProtocolBindings does exactly that.
  1046. //
  1047. NdisReEnumerateProtocolBindings (ProtHandle);
  1048. break;
  1049. }
  1050. #ifdef NDIS51
  1051. //
  1052. // Pass on this notification to protocol(s) above before doing anything
  1053. // with it.
  1054. //
  1055. if (pAdapt->MiniportHandle)
  1056. {
  1057. ReturnStatus = NdisIMNotifyPnPEvent(pAdapt->MiniportHandle, pNetPnPEvent);
  1058. }
  1059. #endif // NDIS51
  1060. ReconfigStatus = NDIS_STATUS_SUCCESS;
  1061. } while(FALSE);
  1062. DBGPRINT(("<==PtPNPNetEventReconfigure: pAdapt %p\n", pAdapt));
  1063. #ifdef NDIS51
  1064. //
  1065. // Overwrite status with what upper-layer protocol(s) returned.
  1066. //
  1067. ReconfigStatus = ReturnStatus;
  1068. #endif
  1069. return ReconfigStatus;
  1070. }
  1071. NDIS_STATUS
  1072. PtPnPNetEventSetPower(
  1073. IN PADAPT pAdapt,
  1074. IN PNET_PNP_EVENT pNetPnPEvent
  1075. )
  1076. /*++
  1077. Routine Description:
  1078. This is a notification to our protocol edge of the power state
  1079. of the lower miniport. If it is going to a low-power state, we must
  1080. wait here for all outstanding sends and requests to complete.
  1081. NDIS 5.1: Since we use packet stacking, it is not sufficient to
  1082. check usage of our local send packet pool to detect whether or not
  1083. all outstanding sends have completed. For this, use the new API
  1084. NdisQueryPendingIOCount.
  1085. NDIS 5.1: Use the 5.1 API NdisIMNotifyPnPEvent to pass on PnP
  1086. notifications to upper protocol(s).
  1087. Arguments:
  1088. pAdapt - Pointer to the adpater structure
  1089. pNetPnPEvent - The Net Pnp Event. this contains the new device state
  1090. Return Value:
  1091. NDIS_STATUS_SUCCESS or the status returned by upper-layer protocols.
  1092. --*/
  1093. {
  1094. PNDIS_DEVICE_POWER_STATE pDeviceState =(PNDIS_DEVICE_POWER_STATE)(pNetPnPEvent->Buffer);
  1095. NDIS_DEVICE_POWER_STATE PrevDeviceState = pAdapt->PTDeviceState;
  1096. NDIS_STATUS Status;
  1097. NDIS_STATUS ReturnStatus;
  1098. #ifdef NDIS51
  1099. ULONG PendingIoCount = 0;
  1100. #endif // NDIS51
  1101. ReturnStatus = NDIS_STATUS_SUCCESS;
  1102. //
  1103. // Set the Internal Device State, this blocks all new sends or receives
  1104. //
  1105. NdisAcquireSpinLock(&pAdapt->Lock);
  1106. pAdapt->PTDeviceState = *pDeviceState;
  1107. //
  1108. // Check if the miniport below is going to a low power state.
  1109. //
  1110. if (pAdapt->PTDeviceState > NdisDeviceStateD0)
  1111. {
  1112. //
  1113. // If the miniport below is going to standby, fail all incoming requests
  1114. //
  1115. if (PrevDeviceState == NdisDeviceStateD0)
  1116. {
  1117. pAdapt->StandingBy = TRUE;
  1118. }
  1119. NdisReleaseSpinLock(&pAdapt->Lock);
  1120. #ifdef NDIS51
  1121. //
  1122. // Notify upper layer protocol(s) first.
  1123. //
  1124. if (pAdapt->MiniportHandle != NULL)
  1125. {
  1126. ReturnStatus = NdisIMNotifyPnPEvent(pAdapt->MiniportHandle, pNetPnPEvent);
  1127. }
  1128. #endif // NDIS51
  1129. //
  1130. // Wait for outstanding sends and requests to complete.
  1131. //
  1132. while (pAdapt->OutstandingSends != 0)
  1133. {
  1134. NdisMSleep(2);
  1135. }
  1136. while (pAdapt->OutstandingRequests == TRUE)
  1137. {
  1138. //
  1139. // sleep till outstanding requests complete
  1140. //
  1141. NdisMSleep(2);
  1142. }
  1143. //
  1144. // If the below miniport is going to low power state, complete the queued request
  1145. //
  1146. NdisAcquireSpinLock(&pAdapt->Lock);
  1147. if (pAdapt->QueuedRequest)
  1148. {
  1149. pAdapt->QueuedRequest = FALSE;
  1150. NdisReleaseSpinLock(&pAdapt->Lock);
  1151. PtRequestComplete(pAdapt, &pAdapt->Request, NDIS_STATUS_FAILURE);
  1152. }
  1153. else
  1154. {
  1155. NdisReleaseSpinLock(&pAdapt->Lock);
  1156. }
  1157. ASSERT(NdisPacketPoolUsage(pAdapt->SendPacketPoolHandle) == 0);
  1158. ASSERT(pAdapt->OutstandingRequests == FALSE);
  1159. }
  1160. else
  1161. {
  1162. //
  1163. // If the physical miniport is powering up (from Low power state to D0),
  1164. // clear the flag
  1165. //
  1166. if (PrevDeviceState > NdisDeviceStateD0)
  1167. {
  1168. pAdapt->StandingBy = FALSE;
  1169. }
  1170. //
  1171. // The device below is being turned on. If we had a request
  1172. // pending, send it down now.
  1173. //
  1174. if (pAdapt->QueuedRequest == TRUE)
  1175. {
  1176. pAdapt->QueuedRequest = FALSE;
  1177. pAdapt->OutstandingRequests = TRUE;
  1178. NdisReleaseSpinLock(&pAdapt->Lock);
  1179. NdisRequest(&Status,
  1180. pAdapt->BindingHandle,
  1181. &pAdapt->Request);
  1182. if (Status != NDIS_STATUS_PENDING)
  1183. {
  1184. PtRequestComplete(pAdapt,
  1185. &pAdapt->Request,
  1186. Status);
  1187. }
  1188. }
  1189. else
  1190. {
  1191. NdisReleaseSpinLock(&pAdapt->Lock);
  1192. }
  1193. #ifdef NDIS51
  1194. //
  1195. // Pass on this notification to protocol(s) above
  1196. //
  1197. if (pAdapt->MiniportHandle)
  1198. {
  1199. ReturnStatus = NdisIMNotifyPnPEvent(pAdapt->MiniportHandle, pNetPnPEvent);
  1200. }
  1201. #endif // NDIS51
  1202. }
  1203. return ReturnStatus;
  1204. }