3 Copyright(c) 1992-2000 Microsoft Corporation
\r
11 Ndis Intermediate Miniport driver sample. This is a passthru driver.
\r
24 #include "precomp.h"
\r
27 #define MAX_PACKET_POOL_SIZE 0x0000FFFF
\r
28 #define MIN_PACKET_POOL_SIZE 0x000000FF
\r
31 // NDIS version as 0xMMMMmmmm, where M=Major/m=minor (0x00050001 = 5.1);
\r
32 // initially unknown (0)
\r
34 ULONG NdisDotSysVersion = 0x0;
\r
37 #define NDIS_SYS_VERSION_51 0x00050001
\r
42 OUT PNDIS_STATUS Status,
\r
43 IN NDIS_HANDLE BindContext,
\r
44 IN PNDIS_STRING DeviceName,
\r
45 IN PVOID SystemSpecific1,
\r
46 IN PVOID SystemSpecific2
\r
50 Routine Description:
\r
52 Called by NDIS to bind to a miniport below.
\r
56 Status - Return status of bind here.
\r
57 BindContext - Can be passed to NdisCompleteBindAdapter if this call is pended.
\r
58 DeviceName - Device name to bind to. This is passed to NdisOpenAdapter.
\r
59 SystemSpecific1 - Can be passed to NdisOpenProtocolConfiguration to read per-binding information
\r
60 SystemSpecific2 - Unused
\r
64 NDIS_STATUS_PENDING if this call is pended. In this case call NdisCompleteBindAdapter
\r
66 Anything else Completes this call synchronously
\r
70 NDIS_HANDLE ConfigHandle = NULL;
\r
71 PNDIS_CONFIGURATION_PARAMETER Param;
\r
72 NDIS_STRING DeviceStr = NDIS_STRING_CONST("UpperBindings");
\r
73 NDIS_STRING NdisVersionStr = NDIS_STRING_CONST("NdisVersion");
\r
74 PADAPT pAdapt = NULL;
\r
78 BOOLEAN NoCleanUpNeeded = FALSE;
\r
81 UNREFERENCED_PARAMETER(BindContext);
\r
82 UNREFERENCED_PARAMETER(SystemSpecific2);
\r
84 DBGPRINT(("==> Protocol BindAdapter\n"));
\r
89 // Access the configuration section for our binding-specific
\r
92 NdisOpenProtocolConfiguration(Status,
\r
96 if (*Status != NDIS_STATUS_SUCCESS)
\r
100 if (NdisDotSysVersion == 0)
\r
102 NdisReadConfiguration(Status,
\r
105 &NdisVersionStr, // "NdisVersion"
\r
106 NdisParameterInteger);
\r
107 if (*Status != NDIS_STATUS_SUCCESS)
\r
112 NdisDotSysVersion = Param->ParameterData.IntegerData;
\r
117 // Read the "UpperBindings" reserved key that contains a list
\r
118 // of device names representing our miniport instances corresponding
\r
119 // to this lower binding. Since this is a 1:1 IM driver, this key
\r
120 // contains exactly one name.
\r
122 // If we want to implement a N:1 mux driver (N adapter instances
\r
123 // over a single lower binding), then UpperBindings will be a
\r
124 // MULTI_SZ containing a list of device names - we would loop through
\r
125 // this list, calling NdisIMInitializeDeviceInstanceEx once for
\r
126 // each name in it.
\r
128 NdisReadConfiguration(Status,
\r
132 NdisParameterString);
\r
133 if (*Status != NDIS_STATUS_SUCCESS)
\r
139 // Allocate memory for the Adapter structure. This represents both the
\r
140 // protocol context as well as the adapter structure when the miniport
\r
143 // In addition to the base structure, allocate space for the device
\r
144 // instance string.
\r
146 TotalSize = sizeof(ADAPT) + Param->ParameterData.StringData.MaximumLength;
\r
148 NdisAllocateMemoryWithTag(&pAdapt, TotalSize, TAG);
\r
150 if (pAdapt == NULL)
\r
152 *Status = NDIS_STATUS_RESOURCES;
\r
157 // Initialize the adapter structure. We copy in the IM device
\r
158 // name as well, because we may need to use it in a call to
\r
159 // NdisIMCancelInitializeDeviceInstance. The string returned
\r
160 // by NdisReadConfiguration is active (i.e. available) only
\r
161 // for the duration of this call to our BindAdapter handler.
\r
163 NdisZeroMemory(pAdapt, TotalSize);
\r
164 pAdapt->DeviceName.MaximumLength = Param->ParameterData.StringData.MaximumLength;
\r
165 pAdapt->DeviceName.Length = Param->ParameterData.StringData.Length;
\r
166 pAdapt->DeviceName.Buffer = (PWCHAR)((ULONG_PTR)pAdapt + sizeof(ADAPT));
\r
167 NdisMoveMemory(pAdapt->DeviceName.Buffer,
\r
168 Param->ParameterData.StringData.Buffer,
\r
169 Param->ParameterData.StringData.MaximumLength);
\r
173 NdisInitializeEvent(&pAdapt->Event);
\r
174 NdisAllocateSpinLock(&pAdapt->Lock);
\r
177 // Allocate a packet pool for sends. We need this to pass sends down.
\r
178 // We cannot use the same packet descriptor that came down to our send
\r
179 // handler (see also NDIS 5.1 packet stacking).
\r
181 NdisAllocatePacketPoolEx(Status,
\r
182 &pAdapt->SendPacketPoolHandle,
\r
183 MIN_PACKET_POOL_SIZE,
\r
184 MAX_PACKET_POOL_SIZE - MIN_PACKET_POOL_SIZE,
\r
185 sizeof(SEND_RSVD));
\r
187 if (*Status != NDIS_STATUS_SUCCESS)
\r
193 // Allocate a packet pool for receives. We need this to indicate receives.
\r
194 // Same consideration as sends (see also NDIS 5.1 packet stacking).
\r
196 NdisAllocatePacketPoolEx(Status,
\r
197 &pAdapt->RecvPacketPoolHandle,
\r
198 MIN_PACKET_POOL_SIZE,
\r
199 MAX_PACKET_POOL_SIZE - MIN_PACKET_POOL_SIZE,
\r
200 PROTOCOL_RESERVED_SIZE_IN_PACKET);
\r
202 if (*Status != NDIS_STATUS_SUCCESS)
\r
208 // Now open the adapter below and complete the initialization
\r
210 NdisOpenAdapter(Status,
\r
212 &pAdapt->BindingHandle,
\r
215 sizeof(MediumArray)/sizeof(NDIS_MEDIUM),
\r
222 if (*Status == NDIS_STATUS_PENDING)
\r
224 NdisWaitEvent(&pAdapt->Event, 0);
\r
225 *Status = pAdapt->Status;
\r
228 if (*Status != NDIS_STATUS_SUCCESS)
\r
232 PtReferenceAdapt(pAdapt);
\r
234 #pragma prefast(suppress: __WARNING_POTENTIAL_BUFFER_OVERFLOW, "Ndis guarantees MediumIndex to be within bounds");
\r
235 pAdapt->Medium = MediumArray[MediumIndex];
\r
238 // Now ask NDIS to initialize our miniport (upper) edge.
\r
239 // Set the flag below to synchronize with a possible call
\r
240 // to our protocol Unbind handler that may come in before
\r
241 // our miniport initialization happens.
\r
243 pAdapt->MiniportInitPending = TRUE;
\r
244 NdisInitializeEvent(&pAdapt->MiniportInitEvent);
\r
246 PtReferenceAdapt(pAdapt);
\r
248 *Status = NdisIMInitializeDeviceInstanceEx(DriverHandle,
\r
249 &pAdapt->DeviceName,
\r
252 if (*Status != NDIS_STATUS_SUCCESS)
\r
254 if (pAdapt->MiniportIsHalted == TRUE)
\r
256 NoCleanUpNeeded = TRUE;
\r
259 DBGPRINT(("BindAdapter: Adapt %p, IMInitializeDeviceInstance error %x\n",
\r
262 if (PtDereferenceAdapt(pAdapt))
\r
270 PtDereferenceAdapt(pAdapt);
\r
275 // Close the configuration handle now - see comments above with
\r
276 // the call to NdisIMInitializeDeviceInstanceEx.
\r
278 if (ConfigHandle != NULL)
\r
280 NdisCloseConfiguration(ConfigHandle);
\r
283 if ((*Status != NDIS_STATUS_SUCCESS) && (NoCleanUpNeeded == FALSE))
\r
285 if (pAdapt != NULL)
\r
287 if (pAdapt->BindingHandle != NULL)
\r
289 NDIS_STATUS LocalStatus;
\r
292 // Close the binding we opened above.
\r
295 NdisResetEvent(&pAdapt->Event);
\r
297 NdisCloseAdapter(&LocalStatus, pAdapt->BindingHandle);
\r
298 pAdapt->BindingHandle = NULL;
\r
300 if (LocalStatus == NDIS_STATUS_PENDING)
\r
302 NdisWaitEvent(&pAdapt->Event, 0);
\r
303 LocalStatus = pAdapt->Status;
\r
307 if (PtDereferenceAdapt(pAdapt))
\r
316 DBGPRINT(("<== Protocol BindAdapter: pAdapt %p, Status %x\n", pAdapt, *Status));
\r
321 PtOpenAdapterComplete(
\r
322 IN NDIS_HANDLE ProtocolBindingContext,
\r
323 IN NDIS_STATUS Status,
\r
324 IN NDIS_STATUS OpenErrorStatus
\r
328 Routine Description:
\r
330 Completion routine for NdisOpenAdapter issued from within the PtBindAdapter. Simply
\r
331 unblock the caller.
\r
335 ProtocolBindingContext Pointer to the adapter
\r
336 Status Status of the NdisOpenAdapter call
\r
337 OpenErrorStatus Secondary status(ignored by us).
\r
345 PADAPT pAdapt =(PADAPT)ProtocolBindingContext;
\r
347 UNREFERENCED_PARAMETER(OpenErrorStatus);
\r
349 DBGPRINT(("==> PtOpenAdapterComplete: Adapt %p, Status %x\n", pAdapt, Status));
\r
350 pAdapt->Status = Status;
\r
351 NdisSetEvent(&pAdapt->Event);
\r
357 OUT PNDIS_STATUS Status,
\r
358 IN NDIS_HANDLE ProtocolBindingContext,
\r
359 IN NDIS_HANDLE UnbindContext
\r
363 Routine Description:
\r
365 Called by NDIS when we are required to unbind to the adapter below.
\r
366 This functions shares functionality with the miniport's HaltHandler.
\r
367 The code should ensure that NdisCloseAdapter and NdisFreeMemory is called
\r
368 only once between the two functions
\r
372 Status Placeholder for return status
\r
373 ProtocolBindingContext Pointer to the adapter structure
\r
374 UnbindContext Context for NdisUnbindComplete() if this pends
\r
378 Status for NdisIMDeinitializeDeviceContext
\r
382 PADAPT pAdapt =(PADAPT)ProtocolBindingContext;
\r
383 NDIS_STATUS LocalStatus;
\r
385 UNREFERENCED_PARAMETER(UnbindContext);
\r
387 DBGPRINT(("==> PtUnbindAdapter: Adapt %p\n", pAdapt));
\r
390 // Set the flag that the miniport below is unbinding, so the request handlers will
\r
391 // fail any request comming later
\r
393 NdisAcquireSpinLock(&pAdapt->Lock);
\r
394 pAdapt->UnbindingInProcess = TRUE;
\r
395 if (pAdapt->QueuedRequest == TRUE)
\r
397 pAdapt->QueuedRequest = FALSE;
\r
398 NdisReleaseSpinLock(&pAdapt->Lock);
\r
400 PtRequestComplete(pAdapt,
\r
402 NDIS_STATUS_FAILURE );
\r
407 NdisReleaseSpinLock(&pAdapt->Lock);
\r
411 // Check if we had called NdisIMInitializeDeviceInstanceEx and
\r
412 // we are awaiting a call to MiniportInitialize.
\r
414 if (pAdapt->MiniportInitPending == TRUE)
\r
417 // Try to cancel the pending IMInit process.
\r
419 LocalStatus = NdisIMCancelInitializeDeviceInstance(
\r
421 &pAdapt->DeviceName);
\r
423 if (LocalStatus == NDIS_STATUS_SUCCESS)
\r
426 // Successfully cancelled IM Initialization; our
\r
427 // Miniport Initialize routine will not be called
\r
428 // for this device.
\r
430 pAdapt->MiniportInitPending = FALSE;
\r
431 ASSERT(pAdapt->MiniportHandle == NULL);
\r
436 // Our Miniport Initialize routine will be called
\r
437 // (may be running on another thread at this time).
\r
438 // Wait for it to finish.
\r
440 NdisWaitEvent(&pAdapt->MiniportInitEvent, 0);
\r
441 ASSERT(pAdapt->MiniportInitPending == FALSE);
\r
448 // Call NDIS to remove our device-instance. We do most of the work
\r
449 // inside the HaltHandler.
\r
451 // The Handle will be NULL if our miniport Halt Handler has been called or
\r
452 // if the IM device was never initialized
\r
455 if (pAdapt->MiniportHandle != NULL)
\r
457 *Status = NdisIMDeInitializeDeviceInstance(pAdapt->MiniportHandle);
\r
459 if (*Status != NDIS_STATUS_SUCCESS)
\r
461 *Status = NDIS_STATUS_FAILURE;
\r
467 // We need to do some work here.
\r
468 // Close the binding below us
\r
469 // and release the memory allocated.
\r
472 if(pAdapt->BindingHandle != NULL)
\r
474 NdisResetEvent(&pAdapt->Event);
\r
476 NdisCloseAdapter(Status, pAdapt->BindingHandle);
\r
479 // Wait for it to complete
\r
481 if(*Status == NDIS_STATUS_PENDING)
\r
483 NdisWaitEvent(&pAdapt->Event, 0);
\r
484 *Status = pAdapt->Status;
\r
486 pAdapt->BindingHandle = NULL;
\r
491 // Both Our MiniportHandle and Binding Handle should not be NULL.
\r
493 *Status = NDIS_STATUS_FAILURE;
\r
498 // Free the memory here, if was not released earlier(by calling the HaltHandler)
\r
500 MPFreeAllPacketPools(pAdapt);
\r
501 NdisFreeSpinLock(&pAdapt->Lock);
\r
502 NdisFreeMemory(pAdapt, 0, 0);
\r
505 DBGPRINT(("<== PtUnbindAdapter: Adapt %p\n", pAdapt));
\r
513 NDIS_STATUS Status;
\r
515 if (ProtHandle != NULL)
\r
517 NdisDeregisterProtocol(&Status, ProtHandle);
\r
521 DBGPRINT(("PtUnloadProtocol: done!\n"));
\r
527 PtCloseAdapterComplete(
\r
528 IN NDIS_HANDLE ProtocolBindingContext,
\r
529 IN NDIS_STATUS Status
\r
533 Routine Description:
\r
535 Completion for the CloseAdapter call.
\r
539 ProtocolBindingContext Pointer to the adapter structure
\r
540 Status Completion status
\r
548 PADAPT pAdapt =(PADAPT)ProtocolBindingContext;
\r
550 DBGPRINT(("CloseAdapterComplete: Adapt %p, Status %x\n", pAdapt, Status));
\r
551 pAdapt->Status = Status;
\r
552 NdisSetEvent(&pAdapt->Event);
\r
558 IN NDIS_HANDLE ProtocolBindingContext,
\r
559 IN NDIS_STATUS Status
\r
563 Routine Description:
\r
565 Completion for the reset.
\r
569 ProtocolBindingContext Pointer to the adapter structure
\r
570 Status Completion status
\r
579 UNREFERENCED_PARAMETER(ProtocolBindingContext);
\r
580 UNREFERENCED_PARAMETER(Status);
\r
582 // We never issue a reset, so we should not be here.
\r
590 IN NDIS_HANDLE ProtocolBindingContext,
\r
591 IN PNDIS_REQUEST NdisRequest,
\r
592 IN NDIS_STATUS Status
\r
596 Routine Description:
\r
598 Completion handler for the previously posted request. All OIDS
\r
599 are completed by and sent to the same miniport that they were requested for.
\r
600 If Oid == OID_PNP_QUERY_POWER then the data structure needs to returned with all entries =
\r
601 NdisDeviceStateUnspecified
\r
605 ProtocolBindingContext Pointer to the adapter structure
\r
606 NdisRequest The posted request
\r
607 Status Completion status
\r
615 PADAPT pAdapt = (PADAPT)ProtocolBindingContext;
\r
616 NDIS_OID Oid = pAdapt->Request.DATA.SET_INFORMATION.Oid ;
\r
619 // Since our request is not outstanding anymore
\r
621 ASSERT(pAdapt->OutstandingRequests == TRUE);
\r
623 pAdapt->OutstandingRequests = FALSE;
\r
626 // Complete the Set or Query, and fill in the buffer for OID_PNP_CAPABILITIES, if need be.
\r
628 switch (NdisRequest->RequestType)
\r
630 case NdisRequestQueryInformation:
\r
633 // We never pass OID_PNP_QUERY_POWER down.
\r
635 ASSERT(Oid != OID_PNP_QUERY_POWER);
\r
637 if ((Oid == OID_PNP_CAPABILITIES) && (Status == NDIS_STATUS_SUCCESS))
\r
639 MPQueryPNPCapabilities(pAdapt, &Status);
\r
641 *pAdapt->BytesReadOrWritten = NdisRequest->DATA.QUERY_INFORMATION.BytesWritten;
\r
642 *pAdapt->BytesNeeded = NdisRequest->DATA.QUERY_INFORMATION.BytesNeeded;
\r
644 if (((Oid == OID_GEN_MAC_OPTIONS)
\r
645 && (Status == NDIS_STATUS_SUCCESS))
\r
646 && (NdisDotSysVersion >= NDIS_SYS_VERSION_51))
\r
649 // Only do this on Windows XP or greater (NDIS.SYS v 5.1);
\r
650 // do not do in Windows 2000 (NDIS.SYS v 5.0))
\r
654 // Remove the no-loopback bit from mac-options. In essence we are
\r
655 // telling NDIS that we can handle loopback. We don't, but the
\r
656 // interface below us does. If we do not do this, then loopback
\r
657 // processing happens both below us and above us. This is wasteful
\r
658 // at best and if Netmon is running, it will see multiple copies
\r
659 // of loopback packets when sniffing above us.
\r
661 // Only the lowest miniport is a stack of layered miniports should
\r
662 // ever report this bit set to NDIS.
\r
664 *(PULONG)NdisRequest->DATA.QUERY_INFORMATION.InformationBuffer &= ~NDIS_MAC_OPTION_NO_LOOPBACK;
\r
667 NdisMQueryInformationComplete(pAdapt->MiniportHandle,
\r
671 case NdisRequestSetInformation:
\r
673 ASSERT( Oid != OID_PNP_SET_POWER);
\r
675 *pAdapt->BytesReadOrWritten = NdisRequest->DATA.SET_INFORMATION.BytesRead;
\r
676 *pAdapt->BytesNeeded = NdisRequest->DATA.SET_INFORMATION.BytesNeeded;
\r
677 NdisMSetInformationComplete(pAdapt->MiniportHandle,
\r
691 IN NDIS_HANDLE ProtocolBindingContext,
\r
692 IN NDIS_STATUS GeneralStatus,
\r
693 IN PVOID StatusBuffer,
\r
694 IN UINT StatusBufferSize
\r
698 Routine Description:
\r
700 Status handler for the lower-edge(protocol).
\r
704 ProtocolBindingContext Pointer to the adapter structure
\r
705 GeneralStatus Status code
\r
706 StatusBuffer Status buffer
\r
707 StatusBufferSize Size of the status buffer
\r
715 PADAPT pAdapt = (PADAPT)ProtocolBindingContext;
\r
718 // Pass up this indication only if the upper edge miniport is initialized
\r
719 // and powered on. Also ignore indications that might be sent by the lower
\r
720 // miniport when it isn't at D0.
\r
722 if ((pAdapt->MiniportHandle != NULL) &&
\r
723 (pAdapt->MPDeviceState == NdisDeviceStateD0) &&
\r
724 (pAdapt->PTDeviceState == NdisDeviceStateD0))
\r
726 if ((GeneralStatus == NDIS_STATUS_MEDIA_CONNECT) ||
\r
727 (GeneralStatus == NDIS_STATUS_MEDIA_DISCONNECT))
\r
730 pAdapt->LastIndicatedStatus = GeneralStatus;
\r
732 NdisMIndicateStatus(pAdapt->MiniportHandle,
\r
738 // Save the last indicated media status
\r
742 if ((pAdapt->MiniportHandle != NULL) &&
\r
743 ((GeneralStatus == NDIS_STATUS_MEDIA_CONNECT) ||
\r
744 (GeneralStatus == NDIS_STATUS_MEDIA_DISCONNECT)))
\r
746 pAdapt->LatestUnIndicateStatus = GeneralStatus;
\r
755 IN NDIS_HANDLE ProtocolBindingContext
\r
759 Routine Description:
\r
770 PADAPT pAdapt = (PADAPT)ProtocolBindingContext;
\r
773 // Pass up this indication only if the upper edge miniport is initialized
\r
774 // and powered on. Also ignore indications that might be sent by the lower
\r
775 // miniport when it isn't at D0.
\r
777 if ((pAdapt->MiniportHandle != NULL) &&
\r
778 (pAdapt->MPDeviceState == NdisDeviceStateD0) &&
\r
779 (pAdapt->PTDeviceState == NdisDeviceStateD0))
\r
781 NdisMIndicateStatusComplete(pAdapt->MiniportHandle);
\r
788 IN NDIS_HANDLE ProtocolBindingContext,
\r
789 IN PNDIS_PACKET Packet,
\r
790 IN NDIS_STATUS Status
\r
794 Routine Description:
\r
796 Called by NDIS when the miniport below had completed a send. We should
\r
797 complete the corresponding upper-edge send this represents.
\r
801 ProtocolBindingContext - Points to ADAPT structure
\r
802 Packet - Low level packet being completed
\r
803 Status - status of send
\r
811 PADAPT pAdapt = (PADAPT)ProtocolBindingContext;
\r
813 NDIS_HANDLE PoolHandle;
\r
817 // Packet stacking:
\r
819 // Determine if the packet we are completing is the one we allocated. If so, then
\r
820 // get the original packet from the reserved area and completed it and free the
\r
821 // allocated packet. If this is the packet that was sent down to us, then just
\r
824 PoolHandle = NdisGetPoolFromPacket(Packet);
\r
825 if (PoolHandle != pAdapt->SendPacketPoolHandle)
\r
828 // We had passed down a packet belonging to the protocol above us.
\r
830 // DBGPRINT(("PtSendComp: Adapt %p, Stacked Packet %p\n", pAdapt, Packet));
\r
832 NdisMSendComplete(pAdapt->MiniportHandle,
\r
839 PSEND_RSVD SendRsvd;
\r
841 SendRsvd = (PSEND_RSVD)(Packet->ProtocolReserved);
\r
842 Pkt = SendRsvd->OriginalPkt;
\r
845 NdisIMCopySendCompletePerPacketInfo (Pkt, Packet);
\r
848 NdisDprFreePacket(Packet);
\r
850 NdisMSendComplete(pAdapt->MiniportHandle,
\r
855 // Decrease the outstanding send count
\r
857 ADAPT_DECR_PENDING_SENDS(pAdapt);
\r
862 PtTransferDataComplete(
\r
863 IN NDIS_HANDLE ProtocolBindingContext,
\r
864 IN PNDIS_PACKET Packet,
\r
865 IN NDIS_STATUS Status,
\r
866 IN UINT BytesTransferred
\r
870 Routine Description:
\r
872 Entry point called by NDIS to indicate completion of a call by us
\r
873 to NdisTransferData.
\r
875 See notes under SendComplete.
\r
883 PADAPT pAdapt =(PADAPT)ProtocolBindingContext;
\r
885 if(pAdapt->MiniportHandle)
\r
887 NdisMTransferDataComplete(pAdapt->MiniportHandle,
\r
897 IN NDIS_HANDLE ProtocolBindingContext,
\r
898 IN NDIS_HANDLE MacReceiveContext,
\r
899 IN PVOID HeaderBuffer,
\r
900 IN UINT HeaderBufferSize,
\r
901 IN PVOID LookAheadBuffer,
\r
902 IN UINT LookAheadBufferSize,
\r
907 Routine Description:
\r
909 Handle receive data indicated up by the miniport below. We pass
\r
910 it along to the protocol above us.
\r
912 If the miniport below indicates packets, NDIS would more
\r
913 likely call us at our ReceivePacket handler. However we
\r
914 might be called here in certain situations even though
\r
915 the miniport below has indicated a receive packet, e.g.
\r
916 if the miniport had set packet status to NDIS_STATUS_RESOURCES.
\r
920 <see DDK ref page for ProtocolReceive>
\r
924 NDIS_STATUS_SUCCESS if we processed the receive successfully,
\r
925 NDIS_STATUS_XXX error code if we discarded it.
\r
929 PADAPT pAdapt = (PADAPT)ProtocolBindingContext;
\r
930 PNDIS_PACKET MyPacket, Packet = NULL;
\r
931 NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
\r
932 ULONG Proc = KeGetCurrentProcessorNumber();
\r
934 if ((!pAdapt->MiniportHandle) || (pAdapt->MPDeviceState > NdisDeviceStateD0))
\r
936 Status = NDIS_STATUS_FAILURE;
\r
941 // Get at the packet, if any, indicated up by the miniport below.
\r
943 Packet = NdisGetReceivedPacket(pAdapt->BindingHandle, MacReceiveContext);
\r
944 if (Packet != NULL)
\r
947 // The miniport below did indicate up a packet. Use information
\r
948 // from that packet to construct a new packet to indicate up.
\r
953 // NDIS 5.1 NOTE: Do not reuse the original packet in indicating
\r
954 // up a receive, even if there is sufficient packet stack space.
\r
955 // If we had to do so, we would have had to overwrite the
\r
956 // status field in the original packet to NDIS_STATUS_RESOURCES,
\r
957 // and it is not allowed for protocols to overwrite this field
\r
958 // in received packets.
\r
963 // Get a packet off the pool and indicate that up
\r
965 NdisDprAllocatePacket(&Status,
\r
967 pAdapt->RecvPacketPoolHandle);
\r
969 if (Status == NDIS_STATUS_SUCCESS)
\r
972 // Make our packet point to data from the original
\r
973 // packet. NOTE: this works only because we are
\r
974 // indicating a receive directly from the context of
\r
975 // our receive indication. If we need to queue this
\r
976 // packet and indicate it from another thread context,
\r
977 // we will also have to allocate a new buffer and copy
\r
978 // over the packet contents, OOB data and per-packet
\r
979 // information. This is because the packet data
\r
980 // is available only for the duration of this
\r
981 // receive indication call.
\r
983 NDIS_PACKET_FIRST_NDIS_BUFFER(MyPacket) = NDIS_PACKET_FIRST_NDIS_BUFFER(Packet);
\r
984 NDIS_PACKET_LAST_NDIS_BUFFER(MyPacket) = NDIS_PACKET_LAST_NDIS_BUFFER(Packet);
\r
987 // Get the original packet (it could be the same packet as the
\r
988 // one received or a different one based on the number of layered
\r
989 // miniports below) and set it on the indicated packet so the OOB
\r
990 // data is visible correctly at protocols above. If the IM driver
\r
991 // modifies the packet in any way it should not set the new packet's
\r
992 // original packet equal to the original packet of the packet that
\r
993 // was indicated to it from the underlying driver, in this case, the
\r
994 // IM driver should also ensure that the related per packet info should
\r
995 // be copied to the new packet.
\r
996 // we can set the original packet to the original packet of the packet
\r
997 // indicated from the underlying driver because the driver doesn't modify
\r
998 // the data content in the packet.
\r
1000 NDIS_SET_ORIGINAL_PACKET(MyPacket, NDIS_GET_ORIGINAL_PACKET(Packet));
\r
1001 NDIS_SET_PACKET_HEADER_SIZE(MyPacket, HeaderBufferSize);
\r
1004 // Copy packet flags.
\r
1006 NdisGetPacketFlags(MyPacket) = NdisGetPacketFlags(Packet);
\r
1009 // Force protocols above to make a copy if they want to hang
\r
1010 // on to data in this packet. This is because we are in our
\r
1011 // Receive handler (not ReceivePacket) and we can't return a
\r
1012 // ref count from here.
\r
1014 NDIS_SET_PACKET_STATUS(MyPacket, NDIS_STATUS_RESOURCES);
\r
1017 // By setting NDIS_STATUS_RESOURCES, we also know that we can reclaim
\r
1018 // this packet as soon as the call to NdisMIndicateReceivePacket
\r
1022 if (pAdapt->MiniportHandle != NULL)
\r
1024 NdisMIndicateReceivePacket(pAdapt->MiniportHandle, &MyPacket, 1);
\r
1028 // Reclaim the indicated packet. Since we had set its status
\r
1029 // to NDIS_STATUS_RESOURCES, we are guaranteed that protocols
\r
1030 // above are done with it.
\r
1032 NdisDprFreePacket(MyPacket);
\r
1040 // The miniport below us uses the old-style (not packet)
\r
1041 // receive indication. Fall through.
\r
1046 // Fall through if the miniport below us has either not
\r
1047 // indicated a packet or we could not allocate one
\r
1049 pAdapt->ReceivedIndicationFlags[Proc] = TRUE;
\r
1050 if (pAdapt->MiniportHandle == NULL)
\r
1054 switch (pAdapt->Medium)
\r
1056 case NdisMedium802_3:
\r
1057 case NdisMediumWan:
\r
1058 NdisMEthIndicateReceive(pAdapt->MiniportHandle,
\r
1059 MacReceiveContext,
\r
1063 LookAheadBufferSize,
\r
1067 case NdisMedium802_5:
\r
1068 NdisMTrIndicateReceive(pAdapt->MiniportHandle,
\r
1069 MacReceiveContext,
\r
1073 LookAheadBufferSize,
\r
1078 case NdisMediumFddi:
\r
1079 NdisMFddiIndicateReceive(pAdapt->MiniportHandle,
\r
1080 MacReceiveContext,
\r
1084 LookAheadBufferSize,
\r
1100 PtReceiveComplete(
\r
1101 IN NDIS_HANDLE ProtocolBindingContext
\r
1105 Routine Description:
\r
1107 Called by the adapter below us when it is done indicating a batch of
\r
1112 ProtocolBindingContext Pointer to our adapter structure.
\r
1120 PADAPT pAdapt =(PADAPT)ProtocolBindingContext;
\r
1121 ULONG Proc = KeGetCurrentProcessorNumber();
\r
1123 if (((pAdapt->MiniportHandle != NULL)
\r
1124 && (pAdapt->MPDeviceState == NdisDeviceStateD0))
\r
1125 && (pAdapt->ReceivedIndicationFlags[Proc]))
\r
1127 switch (pAdapt->Medium)
\r
1129 case NdisMedium802_3:
\r
1130 case NdisMediumWan:
\r
1131 NdisMEthIndicateReceiveComplete(pAdapt->MiniportHandle);
\r
1134 case NdisMedium802_5:
\r
1135 NdisMTrIndicateReceiveComplete(pAdapt->MiniportHandle);
\r
1138 case NdisMediumFddi:
\r
1139 NdisMFddiIndicateReceiveComplete(pAdapt->MiniportHandle);
\r
1148 pAdapt->ReceivedIndicationFlags[Proc] = FALSE;
\r
1154 IN NDIS_HANDLE ProtocolBindingContext,
\r
1155 IN PNDIS_PACKET Packet
\r
1159 Routine Description:
\r
1161 ReceivePacket handler. Called by NDIS if the miniport below supports
\r
1162 NDIS 4.0 style receives. Re-package the buffer chain in a new packet
\r
1163 and indicate the new packet to protocols above us. Any context for
\r
1164 packets indicated up must be kept in the MiniportReserved field.
\r
1166 NDIS 5.1 - packet stacking - if there is sufficient "stack space" in
\r
1167 the packet passed to us, we can use the same packet in a receive
\r
1172 ProtocolBindingContext - Pointer to our adapter structure.
\r
1173 Packet - Pointer to the packet
\r
1177 == 0 -> We are done with the packet
\r
1178 != 0 -> We will keep the packet and call NdisReturnPackets() this
\r
1179 many times when done.
\r
1182 PADAPT pAdapt =(PADAPT)ProtocolBindingContext;
\r
1183 NDIS_STATUS Status;
\r
1184 PNDIS_PACKET MyPacket;
\r
1185 BOOLEAN Remaining;
\r
1188 // Drop the packet silently if the upper miniport edge isn't initialized or
\r
1189 // the miniport edge is in low power state
\r
1191 if ((!pAdapt->MiniportHandle) || (pAdapt->MPDeviceState > NdisDeviceStateD0))
\r
1198 // Check if we can reuse the same packet for indicating up.
\r
1199 // See also: PtReceive().
\r
1201 (VOID)NdisIMGetCurrentPacketStack(Packet, &Remaining);
\r
1205 // We can reuse "Packet". Indicate it up and be done with it.
\r
1207 Status = NDIS_GET_PACKET_STATUS(Packet);
\r
1208 NdisMIndicateReceivePacket(pAdapt->MiniportHandle, &Packet, 1);
\r
1209 return((Status != NDIS_STATUS_RESOURCES) ? 1 : 0);
\r
1214 // Get a packet off the pool and indicate that up
\r
1216 NdisDprAllocatePacket(&Status,
\r
1218 pAdapt->RecvPacketPoolHandle);
\r
1220 if (Status == NDIS_STATUS_SUCCESS)
\r
1222 PRECV_RSVD RecvRsvd;
\r
1224 RecvRsvd = (PRECV_RSVD)(MyPacket->MiniportReserved);
\r
1225 RecvRsvd->OriginalPkt = Packet;
\r
1227 NDIS_PACKET_FIRST_NDIS_BUFFER(MyPacket) = NDIS_PACKET_FIRST_NDIS_BUFFER(Packet);
\r
1228 NDIS_PACKET_LAST_NDIS_BUFFER(MyPacket) = NDIS_PACKET_LAST_NDIS_BUFFER(Packet);
\r
1231 // Get the original packet (it could be the same packet as the one
\r
1232 // received or a different one based on the number of layered miniports
\r
1233 // below) and set it on the indicated packet so the OOB data is visible
\r
1234 // correctly to protocols above us.
\r
1236 NDIS_SET_ORIGINAL_PACKET(MyPacket, NDIS_GET_ORIGINAL_PACKET(Packet));
\r
1239 // Set Packet Flags
\r
1241 NdisGetPacketFlags(MyPacket) = NdisGetPacketFlags(Packet);
\r
1243 Status = NDIS_GET_PACKET_STATUS(Packet);
\r
1245 NDIS_SET_PACKET_STATUS(MyPacket, Status);
\r
1246 NDIS_SET_PACKET_HEADER_SIZE(MyPacket, NDIS_GET_PACKET_HEADER_SIZE(Packet));
\r
1248 if (pAdapt->MiniportHandle != NULL)
\r
1250 NdisMIndicateReceivePacket(pAdapt->MiniportHandle, &MyPacket, 1);
\r
1254 // Check if we had indicated up the packet with NDIS_STATUS_RESOURCES
\r
1255 // NOTE -- do not use NDIS_GET_PACKET_STATUS(MyPacket) for this since
\r
1256 // it might have changed! Use the value saved in the local variable.
\r
1258 if (Status == NDIS_STATUS_RESOURCES)
\r
1261 // Our ReturnPackets handler will not be called for this packet.
\r
1262 // We should reclaim it right here.
\r
1264 NdisDprFreePacket(MyPacket);
\r
1267 return((Status != NDIS_STATUS_RESOURCES) ? 1 : 0);
\r
1272 // We are out of packets. Silently drop it.
\r
1281 IN NDIS_HANDLE ProtocolBindingContext,
\r
1282 IN PNET_PNP_EVENT pNetPnPEvent
\r
1286 Routine Description:
\r
1288 This is called by NDIS to notify us of a PNP event related to a lower
\r
1289 binding. Based on the event, this dispatches to other helper routines.
\r
1291 NDIS 5.1: forward this event to the upper protocol(s) by calling
\r
1292 NdisIMNotifyPnPEvent.
\r
1296 ProtocolBindingContext - Pointer to our adapter structure. Can be NULL
\r
1297 for "global" notifications
\r
1299 pNetPnPEvent - Pointer to the PNP event to be processed.
\r
1303 NDIS_STATUS code indicating status of event processing.
\r
1307 PADAPT pAdapt =(PADAPT)ProtocolBindingContext;
\r
1308 NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
\r
1310 DBGPRINT(("PtPnPHandler: Adapt %p, Event %d\n", pAdapt, pNetPnPEvent->NetEvent));
\r
1312 switch (pNetPnPEvent->NetEvent)
\r
1314 case NetEventSetPower:
\r
1315 Status = PtPnPNetEventSetPower(pAdapt, pNetPnPEvent);
\r
1318 case NetEventReconfigure:
\r
1319 Status = PtPnPNetEventReconfigure(pAdapt, pNetPnPEvent);
\r
1325 // Pass on this notification to protocol(s) above, before
\r
1326 // doing anything else with it.
\r
1328 if (pAdapt && pAdapt->MiniportHandle)
\r
1330 Status = NdisIMNotifyPnPEvent(pAdapt->MiniportHandle, pNetPnPEvent);
\r
1333 Status = NDIS_STATUS_SUCCESS;
\r
1345 PtPnPNetEventReconfigure(
\r
1347 IN PNET_PNP_EVENT pNetPnPEvent
\r
1350 Routine Description:
\r
1352 This routine is called from NDIS to notify our protocol edge of a
\r
1353 reconfiguration of parameters for either a specific binding (pAdapt
\r
1354 is not NULL), or global parameters if any (pAdapt is NULL).
\r
1358 pAdapt - Pointer to our adapter structure.
\r
1359 pNetPnPEvent - the reconfigure event
\r
1363 NDIS_STATUS_SUCCESS
\r
1367 NDIS_STATUS ReconfigStatus = NDIS_STATUS_SUCCESS;
\r
1368 NDIS_STATUS ReturnStatus = NDIS_STATUS_SUCCESS;
\r
1373 // Is this is a global reconfiguration notification ?
\r
1375 if (pAdapt == NULL)
\r
1378 // An important event that causes this notification to us is if
\r
1379 // one of our upper-edge miniport instances was enabled after being
\r
1380 // disabled earlier, e.g. from Device Manager in Win2000. Note that
\r
1381 // NDIS calls this because we had set up an association between our
\r
1382 // miniport and protocol entities by calling NdisIMAssociateMiniport.
\r
1384 // Since we would have torn down the lower binding for that miniport,
\r
1385 // we need NDIS' assistance to re-bind to the lower miniport. The
\r
1386 // call to NdisReEnumerateProtocolBindings does exactly that.
\r
1388 NdisReEnumerateProtocolBindings (ProtHandle);
\r
1395 // Pass on this notification to protocol(s) above before doing anything
\r
1398 if (pAdapt->MiniportHandle)
\r
1400 ReturnStatus = NdisIMNotifyPnPEvent(pAdapt->MiniportHandle, pNetPnPEvent);
\r
1404 ReconfigStatus = NDIS_STATUS_SUCCESS;
\r
1408 DBGPRINT(("<==PtPNPNetEventReconfigure: pAdapt %p\n", pAdapt));
\r
1412 // Overwrite status with what upper-layer protocol(s) returned.
\r
1414 ReconfigStatus = ReturnStatus;
\r
1417 return ReconfigStatus;
\r
1422 PtPnPNetEventSetPower(
\r
1424 IN PNET_PNP_EVENT pNetPnPEvent
\r
1427 Routine Description:
\r
1429 This is a notification to our protocol edge of the power state
\r
1430 of the lower miniport. If it is going to a low-power state, we must
\r
1431 wait here for all outstanding sends and requests to complete.
\r
1433 NDIS 5.1: Since we use packet stacking, it is not sufficient to
\r
1434 check usage of our local send packet pool to detect whether or not
\r
1435 all outstanding sends have completed. For this, use the new API
\r
1436 NdisQueryPendingIOCount.
\r
1438 NDIS 5.1: Use the 5.1 API NdisIMNotifyPnPEvent to pass on PnP
\r
1439 notifications to upper protocol(s).
\r
1443 pAdapt - Pointer to the adpater structure
\r
1444 pNetPnPEvent - The Net Pnp Event. this contains the new device state
\r
1448 NDIS_STATUS_SUCCESS or the status returned by upper-layer protocols.
\r
1452 PNDIS_DEVICE_POWER_STATE pDeviceState =(PNDIS_DEVICE_POWER_STATE)(pNetPnPEvent->Buffer);
\r
1453 NDIS_DEVICE_POWER_STATE PrevDeviceState = pAdapt->PTDeviceState;
\r
1454 NDIS_STATUS Status;
\r
1455 NDIS_STATUS ReturnStatus;
\r
1457 ReturnStatus = NDIS_STATUS_SUCCESS;
\r
1460 // Set the Internal Device State, this blocks all new sends or receives
\r
1462 NdisAcquireSpinLock(&pAdapt->Lock);
\r
1463 pAdapt->PTDeviceState = *pDeviceState;
\r
1466 // Check if the miniport below is going to a low power state.
\r
1468 if (pAdapt->PTDeviceState > NdisDeviceStateD0)
\r
1471 // If the miniport below is going to standby, fail all incoming requests
\r
1473 if (PrevDeviceState == NdisDeviceStateD0)
\r
1475 pAdapt->StandingBy = TRUE;
\r
1478 NdisReleaseSpinLock(&pAdapt->Lock);
\r
1482 // Notify upper layer protocol(s) first.
\r
1484 if (pAdapt->MiniportHandle != NULL)
\r
1486 ReturnStatus = NdisIMNotifyPnPEvent(pAdapt->MiniportHandle, pNetPnPEvent);
\r
1491 // Wait for outstanding sends and requests to complete.
\r
1493 while (pAdapt->OutstandingSends != 0)
\r
1498 while (pAdapt->OutstandingRequests == TRUE)
\r
1501 // sleep till outstanding requests complete
\r
1507 // If the below miniport is going to low power state, complete the queued request
\r
1509 NdisAcquireSpinLock(&pAdapt->Lock);
\r
1510 if (pAdapt->QueuedRequest)
\r
1512 pAdapt->QueuedRequest = FALSE;
\r
1513 NdisReleaseSpinLock(&pAdapt->Lock);
\r
1514 PtRequestComplete(pAdapt, &pAdapt->Request, NDIS_STATUS_FAILURE);
\r
1518 NdisReleaseSpinLock(&pAdapt->Lock);
\r
1522 ASSERT(NdisPacketPoolUsage(pAdapt->SendPacketPoolHandle) == 0);
\r
1523 ASSERT(pAdapt->OutstandingRequests == FALSE);
\r
1528 // If the physical miniport is powering up (from Low power state to D0),
\r
1531 if (PrevDeviceState > NdisDeviceStateD0)
\r
1533 pAdapt->StandingBy = FALSE;
\r
1536 // The device below is being turned on. If we had a request
\r
1537 // pending, send it down now.
\r
1539 if (pAdapt->QueuedRequest == TRUE)
\r
1541 pAdapt->QueuedRequest = FALSE;
\r
1543 pAdapt->OutstandingRequests = TRUE;
\r
1544 NdisReleaseSpinLock(&pAdapt->Lock);
\r
1546 NdisRequest(&Status,
\r
1547 pAdapt->BindingHandle,
\r
1548 &pAdapt->Request);
\r
1550 if (Status != NDIS_STATUS_PENDING)
\r
1552 PtRequestComplete(pAdapt,
\r
1560 NdisReleaseSpinLock(&pAdapt->Lock);
\r
1566 // Pass on this notification to protocol(s) above
\r
1568 if (pAdapt->MiniportHandle)
\r
1570 ReturnStatus = NdisIMNotifyPnPEvent(pAdapt->MiniportHandle, pNetPnPEvent);
\r
1576 return ReturnStatus;
\r
1584 NdisAcquireSpinLock(&pAdapt->Lock);
\r
1586 ASSERT(pAdapt->RefCount >= 0);
\r
1588 pAdapt->RefCount ++;
\r
1589 NdisReleaseSpinLock(&pAdapt->Lock);
\r
1594 PtDereferenceAdapt(
\r
1598 NdisAcquireSpinLock(&pAdapt->Lock);
\r
1600 ASSERT(pAdapt->RefCount > 0);
\r
1602 pAdapt->RefCount--;
\r
1604 if (pAdapt->RefCount == 0)
\r
1606 NdisReleaseSpinLock(&pAdapt->Lock);
\r
1609 // Free all resources on this adapter structure.
\r
1611 MPFreeAllPacketPools (pAdapt);;
\r
1612 NdisFreeSpinLock(&pAdapt->Lock);
\r
1613 NdisFreeMemory(pAdapt, 0 , 0);
\r
1620 NdisReleaseSpinLock(&pAdapt->Lock);
\r