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.

1444 lines
42 KiB

  1. /*++
  2. Copyright (c) 1992-2000 Microsoft Corporation
  3. Module Name:
  4. miniport.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. NDIS_STATUS
  14. MPInitialize(
  15. OUT PNDIS_STATUS OpenErrorStatus,
  16. OUT PUINT SelectedMediumIndex,
  17. IN PNDIS_MEDIUM MediumArray,
  18. IN UINT MediumArraySize,
  19. IN NDIS_HANDLE MiniportAdapterHandle,
  20. IN NDIS_HANDLE WrapperConfigurationContext
  21. )
  22. /*++
  23. Routine Description:
  24. This is the initialize handler which gets called as a result of
  25. the BindAdapter handler calling NdisIMInitializeDeviceInstanceEx.
  26. The context parameter which we pass there is the adapter structure
  27. which we retrieve here.
  28. Arguments:
  29. OpenErrorStatus Not used by us.
  30. SelectedMediumIndex Place-holder for what media we are using
  31. MediumArray Array of ndis media passed down to us to pick from
  32. MediumArraySize Size of the array
  33. MiniportAdapterHandle The handle NDIS uses to refer to us
  34. WrapperConfigurationContext For use by NdisOpenConfiguration
  35. Return Value:
  36. NDIS_STATUS_SUCCESS unless something goes wrong
  37. --*/
  38. {
  39. UINT i;
  40. PADAPT pAdapt;
  41. NDIS_STATUS Status = NDIS_STATUS_FAILURE;
  42. NDIS_MEDIUM Medium;
  43. UNREFERENCED_PARAMETER(WrapperConfigurationContext);
  44. do
  45. {
  46. //
  47. // Start off by retrieving our adapter context and storing
  48. // the Miniport handle in it.
  49. //
  50. pAdapt = NdisIMGetDeviceContext(MiniportAdapterHandle);
  51. pAdapt->MiniportHandle = MiniportAdapterHandle;
  52. DBGPRINT(("==> Miniport Initialize: Adapt %p\n", pAdapt));
  53. //
  54. // Usually we export the medium type of the adapter below as our
  55. // virtual miniport's medium type. However if the adapter below us
  56. // is a WAN device, then we claim to be of medium type 802.3.
  57. //
  58. Medium = pAdapt->Medium;
  59. if (Medium == NdisMediumWan)
  60. {
  61. Medium = NdisMedium802_3;
  62. }
  63. for (i = 0; i < MediumArraySize; i++)
  64. {
  65. if (MediumArray[i] == Medium)
  66. {
  67. *SelectedMediumIndex = i;
  68. break;
  69. }
  70. }
  71. if (i == MediumArraySize)
  72. {
  73. Status = NDIS_STATUS_UNSUPPORTED_MEDIA;
  74. break;
  75. }
  76. //
  77. // Set the attributes now. NDIS_ATTRIBUTE_DESERIALIZE enables us
  78. // to make up-calls to NDIS without having to call NdisIMSwitchToMiniport
  79. // or NdisIMQueueCallBack. This also forces us to protect our data using
  80. // spinlocks where appropriate. Also in this case NDIS does not queue
  81. // packets on our behalf. Since this is a very simple pass-thru
  82. // miniport, we do not have a need to protect anything. However in
  83. // a general case there will be a need to use per-adapter spin-locks
  84. // for the packet queues at the very least.
  85. //
  86. NdisMSetAttributesEx(MiniportAdapterHandle,
  87. pAdapt,
  88. 0, // CheckForHangTimeInSeconds
  89. NDIS_ATTRIBUTE_IGNORE_PACKET_TIMEOUT |
  90. NDIS_ATTRIBUTE_IGNORE_REQUEST_TIMEOUT|
  91. NDIS_ATTRIBUTE_INTERMEDIATE_DRIVER |
  92. NDIS_ATTRIBUTE_DESERIALIZE |
  93. NDIS_ATTRIBUTE_NO_HALT_ON_SUSPEND,
  94. 0);
  95. //
  96. // Initialize LastIndicatedStatus to be NDIS_STATUS_MEDIA_CONNECT
  97. //
  98. pAdapt->LastIndicatedStatus = NDIS_STATUS_MEDIA_CONNECT;
  99. //
  100. // Initialize the power states for both the lower binding (PTDeviceState)
  101. // and our miniport edge to Powered On.
  102. //
  103. pAdapt->MPDeviceState = NdisDeviceStateD0;
  104. pAdapt->PTDeviceState = NdisDeviceStateD0;
  105. //
  106. // Add this adapter to the global pAdapt List
  107. //
  108. NdisAcquireSpinLock(&GlobalLock);
  109. pAdapt->Next = pAdaptList;
  110. pAdaptList = pAdapt;
  111. NdisReleaseSpinLock(&GlobalLock);
  112. //
  113. // Create an ioctl interface
  114. //
  115. (VOID)PtRegisterDevice();
  116. Status = NDIS_STATUS_SUCCESS;
  117. }
  118. while (FALSE);
  119. //
  120. // If we had received an UnbindAdapter notification on the underlying
  121. // adapter, we would have blocked that thread waiting for the IM Init
  122. // process to complete. Wake up any such thread.
  123. //
  124. ASSERT(pAdapt->MiniportInitPending == TRUE);
  125. pAdapt->MiniportInitPending = FALSE;
  126. NdisSetEvent(&pAdapt->MiniportInitEvent);
  127. DBGPRINT(("<== Miniport Initialize: Adapt %p, Status %x\n", pAdapt, Status));
  128. *OpenErrorStatus = Status;
  129. return Status;
  130. }
  131. NDIS_STATUS
  132. MPSend(
  133. IN NDIS_HANDLE MiniportAdapterContext,
  134. IN PNDIS_PACKET Packet,
  135. IN UINT Flags
  136. )
  137. /*++
  138. Routine Description:
  139. Send Packet handler. Either this or our SendPackets (array) handler is called
  140. based on which one is enabled in our Miniport Characteristics.
  141. Arguments:
  142. MiniportAdapterContext Pointer to the adapter
  143. Packet Packet to send
  144. Flags Unused, passed down below
  145. Return Value:
  146. Return code from NdisSend
  147. --*/
  148. {
  149. PADAPT pAdapt = (PADAPT)MiniportAdapterContext;
  150. NDIS_STATUS Status;
  151. PNDIS_PACKET MyPacket;
  152. PVOID MediaSpecificInfo = NULL;
  153. ULONG MediaSpecificInfoSize = 0;
  154. //
  155. // The driver should fail the send if the virtual miniport is in low
  156. // power state
  157. //
  158. if (pAdapt->MPDeviceState > NdisDeviceStateD0)
  159. {
  160. return NDIS_STATUS_FAILURE;
  161. }
  162. #ifdef NDIS51
  163. //
  164. // Use NDIS 5.1 packet stacking:
  165. //
  166. {
  167. PNDIS_PACKET_STACK pStack;
  168. BOOLEAN Remaining;
  169. //
  170. // Packet stacks: Check if we can use the same packet for sending down.
  171. //
  172. pStack = NdisIMGetCurrentPacketStack(Packet, &Remaining);
  173. if (Remaining)
  174. {
  175. //
  176. // We can reuse "Packet".
  177. //
  178. // NOTE: if we needed to keep per-packet information in packets
  179. // sent down, we can use pStack->IMReserved[].
  180. //
  181. ASSERT(pStack);
  182. //
  183. // If the below miniport is going to low power state, stop sending down any packet.
  184. //
  185. NdisAcquireSpinLock(&pAdapt->Lock);
  186. if (pAdapt->PTDeviceState > NdisDeviceStateD0)
  187. {
  188. NdisReleaseSpinLock(&pAdapt->Lock);
  189. return NDIS_STATUS_FAILURE;
  190. }
  191. pAdapt->OutstandingSends++;
  192. NdisReleaseSpinLock(&pAdapt->Lock);
  193. NdisSend(&Status,
  194. pAdapt->BindingHandle,
  195. Packet);
  196. if (Status != NDIS_STATUS_PENDING)
  197. {
  198. ADAPT_DECR_PENDING_SENDS(pAdapt);
  199. }
  200. return(Status);
  201. }
  202. }
  203. #endif // NDIS51
  204. //
  205. // We are either not using packet stacks, or there isn't stack space
  206. // in the original packet passed down to us. Allocate a new packet
  207. // to wrap the data with.
  208. //
  209. //
  210. // If the below miniport is going to low power state, stop sending down any packet.
  211. //
  212. NdisAcquireSpinLock(&pAdapt->Lock);
  213. if (pAdapt->PTDeviceState > NdisDeviceStateD0)
  214. {
  215. NdisReleaseSpinLock(&pAdapt->Lock);
  216. return NDIS_STATUS_FAILURE;
  217. }
  218. pAdapt->OutstandingSends++;
  219. NdisReleaseSpinLock(&pAdapt->Lock);
  220. NdisAllocatePacket(&Status,
  221. &MyPacket,
  222. pAdapt->SendPacketPoolHandle);
  223. if (Status == NDIS_STATUS_SUCCESS)
  224. {
  225. PSEND_RSVD SendRsvd;
  226. //
  227. // Save a pointer to the original packet in our reserved
  228. // area in the new packet. This is needed so that we can
  229. // get back to the original packet when the new packet's send
  230. // is completed.
  231. //
  232. SendRsvd = (PSEND_RSVD)(MyPacket->ProtocolReserved);
  233. SendRsvd->OriginalPkt = Packet;
  234. MyPacket->Private.Flags = Flags;
  235. //
  236. // Set up the new packet so that it describes the same
  237. // data as the original packet.
  238. //
  239. MyPacket->Private.Head = Packet->Private.Head;
  240. MyPacket->Private.Tail = Packet->Private.Tail;
  241. #ifdef WIN9X
  242. //
  243. // Work around the fact that NDIS does not initialize this
  244. // to FALSE on Win9x.
  245. //
  246. MyPacket->Private.ValidCounts = FALSE;
  247. #endif
  248. //
  249. // Copy the OOB Offset from the original packet to the new
  250. // packet.
  251. //
  252. NdisMoveMemory(NDIS_OOB_DATA_FROM_PACKET(MyPacket),
  253. NDIS_OOB_DATA_FROM_PACKET(Packet),
  254. sizeof(NDIS_PACKET_OOB_DATA));
  255. #ifndef WIN9X
  256. //
  257. // Copy the right parts of per packet info into the new packet.
  258. // This API is not available on Win9x since task offload is
  259. // not supported on that platform.
  260. //
  261. NdisIMCopySendPerPacketInfo(MyPacket, Packet);
  262. #endif
  263. //
  264. // Copy the Media specific information
  265. //
  266. NDIS_GET_PACKET_MEDIA_SPECIFIC_INFO(Packet,
  267. &MediaSpecificInfo,
  268. &MediaSpecificInfoSize);
  269. if (MediaSpecificInfo || MediaSpecificInfoSize)
  270. {
  271. NDIS_SET_PACKET_MEDIA_SPECIFIC_INFO(MyPacket,
  272. MediaSpecificInfo,
  273. MediaSpecificInfoSize);
  274. }
  275. NdisSend(&Status,
  276. pAdapt->BindingHandle,
  277. MyPacket);
  278. if (Status != NDIS_STATUS_PENDING)
  279. {
  280. #ifndef WIN9X
  281. NdisIMCopySendCompletePerPacketInfo (Packet, MyPacket);
  282. #endif
  283. NdisFreePacket(MyPacket);
  284. ADAPT_DECR_PENDING_SENDS(pAdapt);
  285. }
  286. }
  287. else
  288. {
  289. ADAPT_DECR_PENDING_SENDS(pAdapt);
  290. //
  291. // We are out of packets. Silently drop it. Alternatively we can deal with it:
  292. // - By keeping separate send and receive pools
  293. // - Dynamically allocate more pools as needed and free them when not needed
  294. //
  295. }
  296. return(Status);
  297. }
  298. VOID
  299. MPSendPackets(
  300. IN NDIS_HANDLE MiniportAdapterContext,
  301. IN PPNDIS_PACKET PacketArray,
  302. IN UINT NumberOfPackets
  303. )
  304. /*++
  305. Routine Description:
  306. Send Packet Array handler. Either this or our SendPacket handler is called
  307. based on which one is enabled in our Miniport Characteristics.
  308. Arguments:
  309. MiniportAdapterContext Pointer to our adapter
  310. PacketArray Set of packets to send
  311. NumberOfPackets Self-explanatory
  312. Return Value:
  313. None
  314. --*/
  315. {
  316. PADAPT pAdapt = (PADAPT)MiniportAdapterContext;
  317. NDIS_STATUS Status;
  318. UINT i;
  319. PVOID MediaSpecificInfo = NULL;
  320. UINT MediaSpecificInfoSize = 0;
  321. for (i = 0; i < NumberOfPackets; i++)
  322. {
  323. PNDIS_PACKET Packet, MyPacket;
  324. Packet = PacketArray[i];
  325. //
  326. // The driver should fail the send if the virtual miniport is in low
  327. // power state
  328. //
  329. if (pAdapt->MPDeviceState > NdisDeviceStateD0)
  330. {
  331. NdisMSendComplete(ADAPT_MINIPORT_HANDLE(pAdapt),
  332. Packet,
  333. NDIS_STATUS_FAILURE);
  334. continue;
  335. }
  336. #ifdef NDIS51
  337. //
  338. // Use NDIS 5.1 packet stacking:
  339. //
  340. {
  341. PNDIS_PACKET_STACK pStack;
  342. BOOLEAN Remaining;
  343. //
  344. // Packet stacks: Check if we can use the same packet for sending down.
  345. //
  346. pStack = NdisIMGetCurrentPacketStack(Packet, &Remaining);
  347. if (Remaining)
  348. {
  349. //
  350. // We can reuse "Packet".
  351. //
  352. // NOTE: if we needed to keep per-packet information in packets
  353. // sent down, we can use pStack->IMReserved[].
  354. //
  355. ASSERT(pStack);
  356. //
  357. // If the below miniport is going to low power state, stop sending down any packet.
  358. //
  359. NdisAcquireSpinLock(&pAdapt->Lock);
  360. if (pAdapt->PTDeviceState > NdisDeviceStateD0)
  361. {
  362. NdisReleaseSpinLock(&pAdapt->Lock);
  363. NdisMSendComplete(ADAPT_MINIPORT_HANDLE(pAdapt),
  364. Packet,
  365. NDIS_STATUS_FAILURE);
  366. }
  367. else
  368. {
  369. pAdapt->OutstandingSends++;
  370. NdisReleaseSpinLock(&pAdapt->Lock);
  371. NdisSend(&Status,
  372. pAdapt->BindingHandle,
  373. Packet);
  374. if (Status != NDIS_STATUS_PENDING)
  375. {
  376. NdisMSendComplete(ADAPT_MINIPORT_HANDLE(pAdapt),
  377. Packet,
  378. Status);
  379. ADAPT_DECR_PENDING_SENDS(pAdapt);
  380. }
  381. }
  382. continue;
  383. }
  384. }
  385. #endif
  386. do
  387. {
  388. NdisAcquireSpinLock(&pAdapt->Lock);
  389. //
  390. // If the below miniport is going to low power state, stop sending down any packet.
  391. //
  392. if (pAdapt->PTDeviceState > NdisDeviceStateD0)
  393. {
  394. NdisReleaseSpinLock(&pAdapt->Lock);
  395. Status = NDIS_STATUS_FAILURE;
  396. break;
  397. }
  398. pAdapt->OutstandingSends++;
  399. NdisReleaseSpinLock(&pAdapt->Lock);
  400. NdisAllocatePacket(&Status,
  401. &MyPacket,
  402. pAdapt->SendPacketPoolHandle);
  403. if (Status == NDIS_STATUS_SUCCESS)
  404. {
  405. PSEND_RSVD SendRsvd;
  406. SendRsvd = (PSEND_RSVD)(MyPacket->ProtocolReserved);
  407. SendRsvd->OriginalPkt = Packet;
  408. MyPacket->Private.Flags = NdisGetPacketFlags(Packet);
  409. MyPacket->Private.Head = Packet->Private.Head;
  410. MyPacket->Private.Tail = Packet->Private.Tail;
  411. #ifdef WIN9X
  412. //
  413. // Work around the fact that NDIS does not initialize this
  414. // to FALSE on Win9x.
  415. //
  416. MyPacket->Private.ValidCounts = FALSE;
  417. #endif // WIN9X
  418. //
  419. // Copy the OOB data from the original packet to the new
  420. // packet.
  421. //
  422. NdisMoveMemory(NDIS_OOB_DATA_FROM_PACKET(MyPacket),
  423. NDIS_OOB_DATA_FROM_PACKET(Packet),
  424. sizeof(NDIS_PACKET_OOB_DATA));
  425. //
  426. // Copy relevant parts of the per packet info into the new packet
  427. //
  428. #ifndef WIN9X
  429. NdisIMCopySendPerPacketInfo(MyPacket, Packet);
  430. #endif
  431. //
  432. // Copy the Media specific information
  433. //
  434. NDIS_GET_PACKET_MEDIA_SPECIFIC_INFO(Packet,
  435. &MediaSpecificInfo,
  436. &MediaSpecificInfoSize);
  437. if (MediaSpecificInfo || MediaSpecificInfoSize)
  438. {
  439. NDIS_SET_PACKET_MEDIA_SPECIFIC_INFO(MyPacket,
  440. MediaSpecificInfo,
  441. MediaSpecificInfoSize);
  442. }
  443. NdisSend(&Status,
  444. pAdapt->BindingHandle,
  445. MyPacket);
  446. if (Status != NDIS_STATUS_PENDING)
  447. {
  448. #ifndef WIN9X
  449. NdisIMCopySendCompletePerPacketInfo (Packet, MyPacket);
  450. #endif
  451. NdisFreePacket(MyPacket);
  452. ADAPT_DECR_PENDING_SENDS(pAdapt);
  453. }
  454. }
  455. else
  456. {
  457. //
  458. // The driver cannot allocate a packet.
  459. //
  460. ADAPT_DECR_PENDING_SENDS(pAdapt);
  461. }
  462. }
  463. while (FALSE);
  464. if (Status != NDIS_STATUS_PENDING)
  465. {
  466. NdisMSendComplete(ADAPT_MINIPORT_HANDLE(pAdapt),
  467. Packet,
  468. Status);
  469. }
  470. }
  471. }
  472. NDIS_STATUS
  473. MPQueryInformation(
  474. IN NDIS_HANDLE MiniportAdapterContext,
  475. IN NDIS_OID Oid,
  476. IN PVOID InformationBuffer,
  477. IN ULONG InformationBufferLength,
  478. OUT PULONG BytesWritten,
  479. OUT PULONG BytesNeeded
  480. )
  481. /*++
  482. Routine Description:
  483. Entry point called by NDIS to query for the value of the specified OID.
  484. Typical processing is to forward the query down to the underlying miniport.
  485. The following OIDs are filtered here:
  486. OID_PNP_QUERY_POWER - return success right here
  487. OID_GEN_SUPPORTED_GUIDS - do not forward, otherwise we will show up
  488. multiple instances of private GUIDs supported by the underlying miniport.
  489. OID_PNP_CAPABILITIES - we do send this down to the lower miniport, but
  490. the values returned are postprocessed before we complete this request;
  491. see PtRequestComplete.
  492. NOTE on OID_TCP_TASK_OFFLOAD - if this IM driver modifies the contents
  493. of data it passes through such that a lower miniport may not be able
  494. to perform TCP task offload, then it should not forward this OID down,
  495. but fail it here with the status NDIS_STATUS_NOT_SUPPORTED. This is to
  496. avoid performing incorrect transformations on data.
  497. If our miniport edge (upper edge) is at a low-power state, fail the request.
  498. If our protocol edge (lower edge) has been notified of a low-power state,
  499. we pend this request until the miniport below has been set to D0. Since
  500. requests to miniports are serialized always, at most a single request will
  501. be pended.
  502. Arguments:
  503. MiniportAdapterContext Pointer to the adapter structure
  504. Oid Oid for this query
  505. InformationBuffer Buffer for information
  506. InformationBufferLength Size of this buffer
  507. BytesWritten Specifies how much info is written
  508. BytesNeeded In case the buffer is smaller than what we need, tell them how much is needed
  509. Return Value:
  510. Return code from the NdisRequest below.
  511. --*/
  512. {
  513. PADAPT pAdapt = (PADAPT)MiniportAdapterContext;
  514. NDIS_STATUS Status = NDIS_STATUS_FAILURE;
  515. do
  516. {
  517. if (Oid == OID_PNP_QUERY_POWER)
  518. {
  519. //
  520. // Do not forward this.
  521. //
  522. Status = NDIS_STATUS_SUCCESS;
  523. break;
  524. }
  525. if (Oid == OID_GEN_SUPPORTED_GUIDS)
  526. {
  527. //
  528. // Do not forward this, otherwise we will end up with multiple
  529. // instances of private GUIDs that the underlying miniport
  530. // supports.
  531. //
  532. Status = NDIS_STATUS_NOT_SUPPORTED;
  533. break;
  534. }
  535. if (Oid == OID_TCP_TASK_OFFLOAD)
  536. {
  537. //
  538. // Fail this -if- this driver performs data transformations
  539. // that can interfere with a lower driver's ability to offload
  540. // TCP tasks.
  541. //
  542. // Status = NDIS_STATUS_NOT_SUPPORTED;
  543. // break;
  544. //
  545. }
  546. //
  547. // If the miniport below is unbinding, just fail any request
  548. //
  549. NdisAcquireSpinLock(&pAdapt->Lock);
  550. if (pAdapt->UnbindingInProcess == TRUE)
  551. {
  552. NdisReleaseSpinLock(&pAdapt->Lock);
  553. Status = NDIS_STATUS_FAILURE;
  554. break;
  555. }
  556. NdisReleaseSpinLock(&pAdapt->Lock);
  557. //
  558. // All other queries are failed, if the miniport is not at D0,
  559. //
  560. if (pAdapt->MPDeviceState > NdisDeviceStateD0)
  561. {
  562. Status = NDIS_STATUS_FAILURE;
  563. break;
  564. }
  565. pAdapt->Request.RequestType = NdisRequestQueryInformation;
  566. pAdapt->Request.DATA.QUERY_INFORMATION.Oid = Oid;
  567. pAdapt->Request.DATA.QUERY_INFORMATION.InformationBuffer = InformationBuffer;
  568. pAdapt->Request.DATA.QUERY_INFORMATION.InformationBufferLength = InformationBufferLength;
  569. pAdapt->BytesNeeded = BytesNeeded;
  570. pAdapt->BytesReadOrWritten = BytesWritten;
  571. //
  572. // If the miniport below is binding, fail the request
  573. //
  574. NdisAcquireSpinLock(&pAdapt->Lock);
  575. if (pAdapt->UnbindingInProcess == TRUE)
  576. {
  577. NdisReleaseSpinLock(&pAdapt->Lock);
  578. Status = NDIS_STATUS_FAILURE;
  579. break;
  580. }
  581. //
  582. // If the Protocol device state is OFF, mark this request as being
  583. // pended. We queue this until the device state is back to D0.
  584. //
  585. if ((pAdapt->PTDeviceState > NdisDeviceStateD0)
  586. && (pAdapt->StandingBy == FALSE))
  587. {
  588. pAdapt->QueuedRequest = TRUE;
  589. NdisReleaseSpinLock(&pAdapt->Lock);
  590. Status = NDIS_STATUS_PENDING;
  591. break;
  592. }
  593. //
  594. // This is in the process of powering down the system, always fail the request
  595. //
  596. if (pAdapt->StandingBy == TRUE)
  597. {
  598. NdisReleaseSpinLock(&pAdapt->Lock);
  599. Status = NDIS_STATUS_FAILURE;
  600. break;
  601. }
  602. pAdapt->OutstandingRequests = TRUE;
  603. NdisReleaseSpinLock(&pAdapt->Lock);
  604. //
  605. // default case, most requests will be passed to the miniport below
  606. //
  607. NdisRequest(&Status,
  608. pAdapt->BindingHandle,
  609. &pAdapt->Request);
  610. if (Status != NDIS_STATUS_PENDING)
  611. {
  612. PtRequestComplete(pAdapt, &pAdapt->Request, Status);
  613. Status = NDIS_STATUS_PENDING;
  614. }
  615. } while (FALSE);
  616. return(Status);
  617. }
  618. VOID
  619. MPQueryPNPCapabilities(
  620. IN OUT PADAPT pAdapt,
  621. OUT PNDIS_STATUS pStatus
  622. )
  623. /*++
  624. Routine Description:
  625. Postprocess a request for OID_PNP_CAPABILITIES that was forwarded
  626. down to the underlying miniport, and has been completed by it.
  627. Arguments:
  628. pAdapt - Pointer to the adapter structure
  629. pStatus - Place to return final status
  630. Return Value:
  631. None.
  632. --*/
  633. {
  634. PNDIS_PNP_CAPABILITIES pPNPCapabilities;
  635. PNDIS_PM_WAKE_UP_CAPABILITIES pPMstruct;
  636. if (pAdapt->Request.DATA.QUERY_INFORMATION.InformationBufferLength >= sizeof(NDIS_PNP_CAPABILITIES))
  637. {
  638. pPNPCapabilities = (PNDIS_PNP_CAPABILITIES)(pAdapt->Request.DATA.QUERY_INFORMATION.InformationBuffer);
  639. //
  640. // The following fields must be overwritten by an IM driver.
  641. //
  642. pPMstruct= & pPNPCapabilities->WakeUpCapabilities;
  643. pPMstruct->MinMagicPacketWakeUp = NdisDeviceStateUnspecified;
  644. pPMstruct->MinPatternWakeUp = NdisDeviceStateUnspecified;
  645. pPMstruct->MinLinkChangeWakeUp = NdisDeviceStateUnspecified;
  646. *pAdapt->BytesReadOrWritten = sizeof(NDIS_PNP_CAPABILITIES);
  647. *pAdapt->BytesNeeded = 0;
  648. //
  649. // Setting our internal flags
  650. // Default, device is ON
  651. //
  652. pAdapt->MPDeviceState = NdisDeviceStateD0;
  653. pAdapt->PTDeviceState = NdisDeviceStateD0;
  654. *pStatus = NDIS_STATUS_SUCCESS;
  655. }
  656. else
  657. {
  658. *pAdapt->BytesNeeded= sizeof(NDIS_PNP_CAPABILITIES);
  659. *pStatus = NDIS_STATUS_RESOURCES;
  660. }
  661. }
  662. NDIS_STATUS
  663. MPSetInformation(
  664. IN NDIS_HANDLE MiniportAdapterContext,
  665. IN NDIS_OID Oid,
  666. IN PVOID InformationBuffer,
  667. IN ULONG InformationBufferLength,
  668. OUT PULONG BytesRead,
  669. OUT PULONG BytesNeeded
  670. )
  671. /*++
  672. Routine Description:
  673. Miniport SetInfo handler.
  674. In the case of OID_PNP_SET_POWER, record the power state and return the OID.
  675. Do not pass below
  676. If the device is suspended, do not block the SET_POWER_OID
  677. as it is used to reactivate the Passthru miniport
  678. PM- If the MP is not ON (DeviceState > D0) return immediately (except for 'query power' and 'set power')
  679. If MP is ON, but the PT is not at D0, then queue the queue the request for later processing
  680. Requests to miniports are always serialized
  681. Arguments:
  682. MiniportAdapterContext Pointer to the adapter structure
  683. Oid Oid for this query
  684. InformationBuffer Buffer for information
  685. InformationBufferLength Size of this buffer
  686. BytesRead Specifies how much info is read
  687. BytesNeeded In case the buffer is smaller than what we need, tell them how much is needed
  688. Return Value:
  689. Return code from the NdisRequest below.
  690. --*/
  691. {
  692. PADAPT pAdapt = (PADAPT)MiniportAdapterContext;
  693. NDIS_STATUS Status;
  694. Status = NDIS_STATUS_FAILURE;
  695. do
  696. {
  697. //
  698. // The Set Power should not be sent to the miniport below the Passthru, but is handled internally
  699. //
  700. if (Oid == OID_PNP_SET_POWER)
  701. {
  702. MPProcessSetPowerOid(&Status,
  703. pAdapt,
  704. InformationBuffer,
  705. InformationBufferLength,
  706. BytesRead,
  707. BytesNeeded);
  708. break;
  709. }
  710. //
  711. // If the miniport below is unbinding, fail the request
  712. //
  713. NdisAcquireSpinLock(&pAdapt->Lock);
  714. if (pAdapt->UnbindingInProcess == TRUE)
  715. {
  716. NdisReleaseSpinLock(&pAdapt->Lock);
  717. Status = NDIS_STATUS_FAILURE;
  718. break;
  719. }
  720. NdisReleaseSpinLock(&pAdapt->Lock);
  721. //
  722. // All other Set Information requests are failed, if the miniport is
  723. // not at D0 or is transitioning to a device state greater than D0.
  724. //
  725. if (pAdapt->MPDeviceState > NdisDeviceStateD0)
  726. {
  727. Status = NDIS_STATUS_FAILURE;
  728. break;
  729. }
  730. // Set up the Request and return the result
  731. pAdapt->Request.RequestType = NdisRequestSetInformation;
  732. pAdapt->Request.DATA.SET_INFORMATION.Oid = Oid;
  733. pAdapt->Request.DATA.SET_INFORMATION.InformationBuffer = InformationBuffer;
  734. pAdapt->Request.DATA.SET_INFORMATION.InformationBufferLength = InformationBufferLength;
  735. pAdapt->BytesNeeded = BytesNeeded;
  736. pAdapt->BytesReadOrWritten = BytesRead;
  737. //
  738. // If the miniport below is unbinding, fail the request
  739. //
  740. NdisAcquireSpinLock(&pAdapt->Lock);
  741. if (pAdapt->UnbindingInProcess == TRUE)
  742. {
  743. NdisReleaseSpinLock(&pAdapt->Lock);
  744. Status = NDIS_STATUS_FAILURE;
  745. break;
  746. }
  747. //
  748. // If the device below is at a low power state, we cannot send it the
  749. // request now, and must pend it.
  750. //
  751. if ((pAdapt->PTDeviceState > NdisDeviceStateD0)
  752. && (pAdapt->StandingBy == FALSE))
  753. {
  754. pAdapt->QueuedRequest = TRUE;
  755. NdisReleaseSpinLock(&pAdapt->Lock);
  756. Status = NDIS_STATUS_PENDING;
  757. break;
  758. }
  759. //
  760. // This is in the process of powering down the system, always fail the request
  761. //
  762. if (pAdapt->StandingBy == TRUE)
  763. {
  764. NdisReleaseSpinLock(&pAdapt->Lock);
  765. Status = NDIS_STATUS_FAILURE;
  766. break;
  767. }
  768. pAdapt->OutstandingRequests = TRUE;
  769. NdisReleaseSpinLock(&pAdapt->Lock);
  770. //
  771. // Forward the request to the device below.
  772. //
  773. NdisRequest(&Status,
  774. pAdapt->BindingHandle,
  775. &pAdapt->Request);
  776. if (Status != NDIS_STATUS_PENDING)
  777. {
  778. *BytesRead = pAdapt->Request.DATA.SET_INFORMATION.BytesRead;
  779. *BytesNeeded = pAdapt->Request.DATA.SET_INFORMATION.BytesNeeded;
  780. pAdapt->OutstandingRequests = FALSE;
  781. }
  782. } while (FALSE);
  783. return(Status);
  784. }
  785. VOID
  786. MPProcessSetPowerOid(
  787. IN OUT PNDIS_STATUS pNdisStatus,
  788. IN PADAPT pAdapt,
  789. IN PVOID InformationBuffer,
  790. IN ULONG InformationBufferLength,
  791. OUT PULONG BytesRead,
  792. OUT PULONG BytesNeeded
  793. )
  794. /*++
  795. Routine Description:
  796. This routine does all the procssing for a request with a SetPower Oid
  797. The miniport shoud accept the Set Power and transition to the new state
  798. The Set Power should not be passed to the miniport below
  799. If the IM miniport is going into a low power state, then there is no guarantee if it will ever
  800. be asked go back to D0, before getting halted. No requests should be pended or queued.
  801. Arguments:
  802. pNdisStatus - Status of the operation
  803. pAdapt - The Adapter structure
  804. InformationBuffer - The New DeviceState
  805. InformationBufferLength
  806. BytesRead - No of bytes read
  807. BytesNeeded - No of bytes needed
  808. Return Value:
  809. Status - NDIS_STATUS_SUCCESS if all the wait events succeed.
  810. --*/
  811. {
  812. NDIS_DEVICE_POWER_STATE NewDeviceState;
  813. DBGPRINT(("==>MPProcessSetPowerOid: Adapt %p\n", pAdapt));
  814. ASSERT (InformationBuffer != NULL);
  815. *pNdisStatus = NDIS_STATUS_FAILURE;
  816. do
  817. {
  818. //
  819. // Check for invalid length
  820. //
  821. if (InformationBufferLength < sizeof(NDIS_DEVICE_POWER_STATE))
  822. {
  823. *pNdisStatus = NDIS_STATUS_INVALID_LENGTH;
  824. break;
  825. }
  826. NewDeviceState = (*(PNDIS_DEVICE_POWER_STATE)InformationBuffer);
  827. //
  828. // Check for invalid device state
  829. //
  830. if ((pAdapt->MPDeviceState > NdisDeviceStateD0) && (NewDeviceState != NdisDeviceStateD0))
  831. {
  832. //
  833. // If the miniport is in a non-D0 state, the miniport can only receive a Set Power to D0
  834. //
  835. ASSERT (!(pAdapt->MPDeviceState > NdisDeviceStateD0) && (NewDeviceState != NdisDeviceStateD0));
  836. *pNdisStatus = NDIS_STATUS_FAILURE;
  837. break;
  838. }
  839. //
  840. // Is the miniport transitioning from an On (D0) state to an Low Power State (>D0)
  841. // If so, then set the StandingBy Flag - (Block all incoming requests)
  842. //
  843. if (pAdapt->MPDeviceState == NdisDeviceStateD0 && NewDeviceState > NdisDeviceStateD0)
  844. {
  845. pAdapt->StandingBy = TRUE;
  846. }
  847. //
  848. // If the miniport is transitioning from a low power state to ON (D0), then clear the StandingBy flag
  849. // All incoming requests will be pended until the physical miniport turns ON.
  850. //
  851. if (pAdapt->MPDeviceState > NdisDeviceStateD0 && NewDeviceState == NdisDeviceStateD0)
  852. {
  853. pAdapt->StandingBy = FALSE;
  854. }
  855. //
  856. // Now update the state in the pAdapt structure;
  857. //
  858. pAdapt->MPDeviceState = NewDeviceState;
  859. *pNdisStatus = NDIS_STATUS_SUCCESS;
  860. } while (FALSE);
  861. if (*pNdisStatus == NDIS_STATUS_SUCCESS)
  862. {
  863. //
  864. // The miniport resume from low power state
  865. //
  866. if (pAdapt->StandingBy == FALSE)
  867. {
  868. //
  869. // If we need to indicate the media connect state
  870. //
  871. if (pAdapt->LastIndicatedStatus != pAdapt->LatestUnIndicateStatus)
  872. {
  873. NdisMIndicateStatus(pAdapt->MiniportHandle,
  874. pAdapt->LatestUnIndicateStatus,
  875. (PVOID)NULL,
  876. 0);
  877. NdisMIndicateStatusComplete(pAdapt->MiniportHandle);
  878. pAdapt->LastIndicatedStatus = pAdapt->LatestUnIndicateStatus;
  879. }
  880. }
  881. else
  882. {
  883. //
  884. // Initialize LatestUnIndicatedStatus
  885. //
  886. pAdapt->LatestUnIndicateStatus = pAdapt->LastIndicatedStatus;
  887. }
  888. *BytesRead = sizeof(NDIS_DEVICE_POWER_STATE);
  889. *BytesNeeded = 0;
  890. }
  891. else
  892. {
  893. *BytesRead = 0;
  894. *BytesNeeded = sizeof (NDIS_DEVICE_POWER_STATE);
  895. }
  896. DBGPRINT(("<==MPProcessSetPowerOid: Adapt %p\n", pAdapt));
  897. }
  898. VOID
  899. MPReturnPacket(
  900. IN NDIS_HANDLE MiniportAdapterContext,
  901. IN PNDIS_PACKET Packet
  902. )
  903. /*++
  904. Routine Description:
  905. NDIS Miniport entry point called whenever protocols are done with
  906. a packet that we had indicated up and they had queued up for returning
  907. later.
  908. Arguments:
  909. MiniportAdapterContext - pointer to ADAPT structure
  910. Packet - packet being returned.
  911. Return Value:
  912. None.
  913. --*/
  914. {
  915. PADAPT pAdapt = (PADAPT)MiniportAdapterContext;
  916. #ifdef NDIS51
  917. //
  918. // Packet stacking: Check if this packet belongs to us.
  919. //
  920. if (NdisGetPoolFromPacket(Packet) != pAdapt->RecvPacketPoolHandle)
  921. {
  922. //
  923. // We reused the original packet in a receive indication.
  924. // Simply return it to the miniport below us.
  925. //
  926. NdisReturnPackets(&Packet, 1);
  927. }
  928. else
  929. #endif // NDIS51
  930. {
  931. //
  932. // This is a packet allocated from this IM's receive packet pool.
  933. // Reclaim our packet, and return the original to the driver below.
  934. //
  935. PNDIS_PACKET MyPacket;
  936. PRECV_RSVD RecvRsvd;
  937. RecvRsvd = (PRECV_RSVD)(Packet->MiniportReserved);
  938. MyPacket = RecvRsvd->OriginalPkt;
  939. NdisFreePacket(Packet);
  940. NdisReturnPackets(&MyPacket, 1);
  941. }
  942. }
  943. NDIS_STATUS
  944. MPTransferData(
  945. OUT PNDIS_PACKET Packet,
  946. OUT PUINT BytesTransferred,
  947. IN NDIS_HANDLE MiniportAdapterContext,
  948. IN NDIS_HANDLE MiniportReceiveContext,
  949. IN UINT ByteOffset,
  950. IN UINT BytesToTransfer
  951. )
  952. /*++
  953. Routine Description:
  954. Miniport's transfer data handler.
  955. Arguments:
  956. Packet Destination packet
  957. BytesTransferred Place-holder for how much data was copied
  958. MiniportAdapterContext Pointer to the adapter structure
  959. MiniportReceiveContext Context
  960. ByteOffset Offset into the packet for copying data
  961. BytesToTransfer How much to copy.
  962. Return Value:
  963. Status of transfer
  964. --*/
  965. {
  966. PADAPT pAdapt = (PADAPT)MiniportAdapterContext;
  967. NDIS_STATUS Status;
  968. //
  969. // Return, if the device is OFF
  970. //
  971. if (IsIMDeviceStateOn(pAdapt) == FALSE)
  972. {
  973. return NDIS_STATUS_FAILURE;
  974. }
  975. NdisTransferData(&Status,
  976. pAdapt->BindingHandle,
  977. MiniportReceiveContext,
  978. ByteOffset,
  979. BytesToTransfer,
  980. Packet,
  981. BytesTransferred);
  982. return(Status);
  983. }
  984. VOID
  985. MPHalt(
  986. IN NDIS_HANDLE MiniportAdapterContext
  987. )
  988. /*++
  989. Routine Description:
  990. Halt handler. All the hard-work for clean-up is done here.
  991. Arguments:
  992. MiniportAdapterContext Pointer to the Adapter
  993. Return Value:
  994. None.
  995. --*/
  996. {
  997. PADAPT pAdapt = (PADAPT)MiniportAdapterContext;
  998. NDIS_STATUS Status;
  999. PADAPT *ppCursor;
  1000. DBGPRINT(("==>MiniportHalt: Adapt %p\n", pAdapt));
  1001. //
  1002. // Remove this adapter from the global list
  1003. //
  1004. NdisAcquireSpinLock(&GlobalLock);
  1005. for (ppCursor = &pAdaptList; *ppCursor != NULL; ppCursor = &(*ppCursor)->Next)
  1006. {
  1007. if (*ppCursor == pAdapt)
  1008. {
  1009. *ppCursor = pAdapt->Next;
  1010. break;
  1011. }
  1012. }
  1013. NdisReleaseSpinLock(&GlobalLock);
  1014. //
  1015. // Delete the ioctl interface that was created when the miniport
  1016. // was created.
  1017. //
  1018. (VOID)PtDeregisterDevice();
  1019. //
  1020. // If we have a valid bind, close the miniport below the protocol
  1021. //
  1022. if (pAdapt->BindingHandle != NULL)
  1023. {
  1024. //
  1025. // Close the binding below. and wait for it to complete
  1026. //
  1027. NdisResetEvent(&pAdapt->Event);
  1028. NdisCloseAdapter(&Status, pAdapt->BindingHandle);
  1029. if (Status == NDIS_STATUS_PENDING)
  1030. {
  1031. NdisWaitEvent(&pAdapt->Event, 0);
  1032. Status = pAdapt->Status;
  1033. }
  1034. ASSERT (Status == NDIS_STATUS_SUCCESS);
  1035. pAdapt->BindingHandle = NULL;
  1036. }
  1037. //
  1038. // Free all resources on this adapter structure.
  1039. //
  1040. MPFreeAllPacketPools (pAdapt);
  1041. NdisFreeMemory(pAdapt, 0, 0);
  1042. DBGPRINT(("<== MiniportHalt: pAdapt %p\n", pAdapt));
  1043. }
  1044. #ifdef NDIS51_MINIPORT
  1045. VOID
  1046. MPCancelSendPackets(
  1047. IN NDIS_HANDLE MiniportAdapterContext,
  1048. IN PVOID CancelId
  1049. )
  1050. /*++
  1051. Routine Description:
  1052. The miniport entry point to handle cancellation of all send packets
  1053. that match the given CancelId. If we have queued any packets that match
  1054. this, then we should dequeue them and call NdisMSendComplete for all
  1055. such packets, with a status of NDIS_STATUS_REQUEST_ABORTED.
  1056. We should also call NdisCancelSendPackets in turn, on each lower binding
  1057. that this adapter corresponds to. This is to let miniports below cancel
  1058. any matching packets.
  1059. Arguments:
  1060. MiniportAdapterContext - pointer to ADAPT structure
  1061. CancelId - ID of packets to be cancelled.
  1062. Return Value:
  1063. None
  1064. --*/
  1065. {
  1066. PADAPT pAdapt = (PADAPT)MiniportAdapterContext;
  1067. //
  1068. // If we queue packets on our adapter structure, this would be
  1069. // the place to acquire a spinlock to it, unlink any packets whose
  1070. // Id matches CancelId, release the spinlock and call NdisMSendComplete
  1071. // with NDIS_STATUS_REQUEST_ABORTED for all unlinked packets.
  1072. //
  1073. //
  1074. // Next, pass this down so that we let the miniport(s) below cancel
  1075. // any packets that they might have queued.
  1076. //
  1077. NdisCancelSendPackets(pAdapt->BindingHandle, CancelId);
  1078. return;
  1079. }
  1080. VOID
  1081. MPDevicePnPEvent(
  1082. IN NDIS_HANDLE MiniportAdapterContext,
  1083. IN NDIS_DEVICE_PNP_EVENT DevicePnPEvent,
  1084. IN PVOID InformationBuffer,
  1085. IN ULONG InformationBufferLength
  1086. )
  1087. /*++
  1088. Routine Description:
  1089. This handler is called to notify us of PnP events directed to
  1090. our miniport device object.
  1091. Arguments:
  1092. MiniportAdapterContext - pointer to ADAPT structure
  1093. DevicePnPEvent - the event
  1094. InformationBuffer - Points to additional event-specific information
  1095. InformationBufferLength - length of above
  1096. Return Value:
  1097. None
  1098. --*/
  1099. {
  1100. // TBD - add code/comments about processing this.
  1101. UNREFERENCED_PARAMETER(MiniportAdapterContext);
  1102. UNREFERENCED_PARAMETER(DevicePnPEvent);
  1103. UNREFERENCED_PARAMETER(InformationBuffer);
  1104. UNREFERENCED_PARAMETER(InformationBufferLength);
  1105. return;
  1106. }
  1107. VOID
  1108. MPAdapterShutdown(
  1109. IN NDIS_HANDLE MiniportAdapterContext
  1110. )
  1111. /*++
  1112. Routine Description:
  1113. This handler is called to notify us of an impending system shutdown.
  1114. Arguments:
  1115. MiniportAdapterContext - pointer to ADAPT structure
  1116. Return Value:
  1117. None
  1118. --*/
  1119. {
  1120. UNREFERENCED_PARAMETER(MiniportAdapterContext);
  1121. return;
  1122. }
  1123. #endif
  1124. VOID
  1125. MPFreeAllPacketPools(
  1126. IN PADAPT pAdapt
  1127. )
  1128. /*++
  1129. Routine Description:
  1130. Free all packet pools on the specified adapter.
  1131. Arguments:
  1132. pAdapt - pointer to ADAPT structure
  1133. Return Value:
  1134. None
  1135. --*/
  1136. {
  1137. if (pAdapt->RecvPacketPoolHandle != NULL)
  1138. {
  1139. //
  1140. // Free the packet pool that is used to indicate receives
  1141. //
  1142. NdisFreePacketPool(pAdapt->RecvPacketPoolHandle);
  1143. pAdapt->RecvPacketPoolHandle = NULL;
  1144. }
  1145. if (pAdapt->SendPacketPoolHandle != NULL)
  1146. {
  1147. //
  1148. // Free the packet pool that is used to send packets below
  1149. //
  1150. NdisFreePacketPool(pAdapt->SendPacketPoolHandle);
  1151. pAdapt->SendPacketPoolHandle = NULL;
  1152. }
  1153. }