Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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