summaryrefslogtreecommitdiff
path: root/network/ndis/netvmini/6x/mphal.c
blob: 63aff8341897a938ca127a99d371a5221ec829f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
/*++

Copyright (c) Microsoft Corporation.  All rights reserved.

    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
    KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
    PURPOSE.

Module Name:

   MpHAL.C

Abstract:

    This module implements the adapter's hardware.

--*/


#include "netvmin6.h"
#include "mphal.tmh"

//
// This registry value saves the permanent MAC address of a netvmini NIC.  It is
// only needed because there's no hardware that keeps track of the permanent
// address across reboots.
//
// It is saved as a NdisParameterBinary configuration value (REG_BINARY) with
// length NIC_MACADDR_LEN (6 bytes).
//
#define NETVMINI_MAC_ADDRESS_KEY L"NetvminiMacAddress"


static
NDIS_STATUS
HWCopyBytesFromNetBuffer(
    _In_     PNET_BUFFER        NetBuffer,
    _Inout_  PULONG             cbDest,
    _Out_writes_bytes_to_(*cbDest, *cbDest) PVOID Dest);


#pragma NDIS_PAGEABLE_FUNCTION(HWInitialize)
#pragma NDIS_PAGEABLE_FUNCTION(HWReadPermanentMacAddress)




NDIS_STATUS
HWInitialize(
    _In_  PMP_ADAPTER                     Adapter,
    _In_  PNDIS_MINIPORT_INIT_PARAMETERS  InitParameters)
/*++
Routine Description:

    Query assigned resources and initialize the adapter.

Arguments:

    Adapter                     Pointer to our adapter
    InitParameters              Parameters to MiniportInitializeEx

Return Value:

    NDIS_STATUS_SUCCESS
    NDIS_STATUS_ADAPTER_NOT_FOUND

--*/
{
    NDIS_STATUS                     Status = NDIS_STATUS_ADAPTER_NOT_FOUND;
    PCM_PARTIAL_RESOURCE_DESCRIPTOR pResDesc;
    ULONG                           index;

    UNREFERENCED_PARAMETER(Adapter);

    DEBUGP(MP_TRACE, "[%p] ---> HWInitialize\n", Adapter);
    PAGED_CODE();


    do
    {
        if (InitParameters->AllocatedResources)
        {
            for (index=0; index < InitParameters->AllocatedResources->Count; index++)
            {
                pResDesc = &InitParameters->AllocatedResources->PartialDescriptors[index];

                switch (pResDesc->Type)
                {
                    case CmResourceTypePort:
                        DEBUGP(MP_INFO, "[%p] IoBaseAddress = 0x%x\n", Adapter,
                                NdisGetPhysicalAddressLow(pResDesc->u.Port.Start));
                        DEBUGP(MP_INFO, "[%p] IoRange = x%x\n", Adapter,
                                pResDesc->u.Port.Length);
                        break;

                    case CmResourceTypeInterrupt:
                        DEBUGP(MP_INFO, "[%p] InterruptLevel = x%x\n", Adapter,
                                pResDesc->u.Interrupt.Level);
                        break;

                    case CmResourceTypeMemory:
                        DEBUGP(MP_INFO, "[%p] MemPhysAddress(Low) = 0x%0x\n", Adapter,
                                NdisGetPhysicalAddressLow(pResDesc->u.Memory.Start));
                        DEBUGP(MP_INFO, "[%p] MemPhysAddress(High) = 0x%0x\n", Adapter,
                                NdisGetPhysicalAddressHigh(pResDesc->u.Memory.Start));
                        break;
                }
            }
        }

        Status = NDIS_STATUS_SUCCESS;

        //
        // Map bus-relative IO range to system IO space using
        // NdisMRegisterIoPortRange
        //

        //
        // Map bus-relative registers to virtual system-space
        // using NdisMMapIoSpace
        //


        //
        // Disable interrupts here as soon as possible
        //

        //
        // Register the interrupt using NdisMRegisterInterruptEx
        //

        //
        // Initialize the hardware with mapped resources
        //

        //
        // Enable the interrupt
        //

    } while (FALSE);


    DEBUGP(MP_TRACE, "[%p] <--- HWInitialize Status = 0x%x\n", Adapter, Status);
    return Status;
}

VOID
HWReadPermanentMacAddress(
    _In_  PMP_ADAPTER  Adapter,
    _In_  NDIS_HANDLE  ConfigurationHandle,
    _Out_writes_bytes_(NIC_MACADDR_SIZE)  PUCHAR  PermanentMacAddress)
/*++

Routine Description:

    Loads the permanent MAC address that is burnt into the NIC.

    IRQL = PASSIVE_LEVEL

Arguments:

    Adapter                     Pointer to our adapter
    ConfigurationHandle         NIC configuration from NdisOpenConfigurationEx
    PermanentMacAddress         On return, receives the NIC's MAC address

Return Value:

    None.

--*/
{
    NDIS_STATUS                   Status;
    PNDIS_CONFIGURATION_PARAMETER Parameter = NULL;
    NDIS_STRING                   PermanentAddressKey = RTL_CONSTANT_STRING(NETVMINI_MAC_ADDRESS_KEY);

    UNREFERENCED_PARAMETER(Adapter);
    PAGED_CODE();

    //
    // We want to figure out what the NIC's physical address is.
    // If we had a hardware NIC, we would query the physical address from it.
    // Instead, for the purposes of this sample, we'll read it from the
    // registry.  This will help us keep the permanent address constant, even
    // if the adapter is disabled/enabled.
    //
    // Note that the registry value that saves our permanent MAC address isn't
    // the one that end-users can configure through the NIC management GUI,
    // nor is it expected that other miniports would need to use a parameter
    // like this.  We only have it to work around the lack of physical hardware.
    //
    NdisReadConfiguration(
            &Status,
            &Parameter,
            ConfigurationHandle,
            &PermanentAddressKey,
            NdisParameterBinary);
    if (Status == NDIS_STATUS_SUCCESS
            && Parameter->ParameterType == NdisParameterBinary
            && Parameter->ParameterData.BinaryData.Length == NIC_MACADDR_SIZE)
    {
        //
        // There is a permanent address stashed in the special netvmini
        // parameter.
        //
        NIC_COPY_ADDRESS(PermanentMacAddress, Parameter->ParameterData.BinaryData.Buffer);
    }
    else
    {
        NDIS_CONFIGURATION_PARAMETER NewPhysicalAddress;
        LARGE_INTEGER TickCountValue;
        UCHAR CurrentMacIndex = 3;

        //
        // There is no (valid) address stashed in the netvmini parameter, so
        // this is probably the first time we've loaded this adapter before.
        //
        // Just for testing purposes, let us make up a dummy mac address.
        // In order to avoid conflicts with MAC addresses, it is usually a good
        // idea to check the IEEE OUI list (e.g. at
        // http://standards.ieee.org/regauth/oui/oui.txt). According to that
        // list 00-50-F2 is owned by Microsoft.
        //
        // An important rule to "generating" MAC addresses is to have the
        // "locally administered bit" set in the address, which is bit 0x02 for
        // LSB-type networks like Ethernet. Also make sure to never set the
        // multicast bit in any MAC address: bit 0x01 in LSB networks.
        //

        {C_ASSERT(NIC_MACADDR_SIZE > 3);}
        NdisZeroMemory(PermanentMacAddress, NIC_MACADDR_SIZE);
        PermanentMacAddress[0] = 0x02;
        PermanentMacAddress[1] = 0x50;
        PermanentMacAddress[2] = 0xF2;

        //
        // Generated value based on the current tick count value.
        //
        KeQueryTickCount(&TickCountValue);
        do
        {
            //
            // Pick up the value in groups of 8 bits to populate the rest of the MAC address.
            //
            PermanentMacAddress[CurrentMacIndex] = (UCHAR)(TickCountValue.LowPart>>((CurrentMacIndex-3)*8));
        } while(++CurrentMacIndex < NIC_MACADDR_SIZE);

        //
        // Finally, we should make a best-effort attempt to save this address
        // to our configuration, so the NIC will always come up with this
        // permanent address.
        //

        NewPhysicalAddress.ParameterType = NdisParameterBinary;
        NewPhysicalAddress.ParameterData.BinaryData.Length = NIC_MACADDR_SIZE;
        NewPhysicalAddress.ParameterData.BinaryData.Buffer = PermanentMacAddress;

        NdisWriteConfiguration(
                &Status,
                ConfigurationHandle,
                &PermanentAddressKey,
                &NewPhysicalAddress);
        if (NDIS_STATUS_SUCCESS != Status)
        {
            DEBUGP(MP_WARNING, "[%p] NdisWriteConfiguration failed to save the permanent MAC address", Adapter);
            // No other handling -- this isn't a fatal error
        }
    }
}



_IRQL_requires_same_
_Function_class_(ALLOCATE_FUNCTION)
PVOID
HWFrameAllocate (
    _In_ POOL_TYPE PoolType,
    _In_ SIZE_T NumberOfBytes,
    _In_ ULONG Tag)
/*++

Routine Description:

    This routine allocates memory for a FRAME.  It is an ALLOCATE_FUNCTION, so
    its parameters and usage are the same as ExAllocatePoolWithTag.

    This allocator is meant to be used with the send NPAGED_LOOKASIDE_LIST.
    While you do not normally need to provide your own allocator (and in fact
    there is a performance penalty for doing so), we use the allocator to
    initialize the FRAME's MDL.  This saves us the effort of (re)initializing
    the same MDL every time we reuse the FRAME.

    Runs at IRQL <= DISPATCH_LEVEL

Arguments:

    PoolType                    Must be NonPagedPool, or NonPagedPoolNx for Win8
                                    and later
    NumberOfBytes               Must be sizeof(FRAME)
    Tag                         The pool allocation tag

Return Value:

    NULL if there are insufficient resources to allocate a FRAME.
    Else, a pointer to a newly-allocated FRAME.  Free it with HWFrameFree.

--*/
{
    PFRAME Frame = NULL;

    DEBUGP(MP_TRACE, "---> HWFrameAllocate\n");

    UNREFERENCED_PARAMETER(PoolType);
    ASSERT(NumberOfBytes == sizeof(FRAME));

    Frame = (PFRAME) NdisAllocateMemoryWithTagPriority(
            NdisDriverHandle,
            (UINT)NumberOfBytes,
            Tag,
            NormalPoolPriority);
    if (!Frame)
    {
        DEBUGP(MP_ERROR, "NdisAllocateMemoryWithTagPriority failed");
        return NULL;
    }
    NdisZeroMemory(Frame, NumberOfBytes);

    Frame->Mdl = NdisAllocateMdl(
            NdisDriverHandle,
            (PVOID)&Frame->Data[0],
            sizeof(Frame->Data));
    if (Frame->Mdl == NULL)
    {
        DEBUGP(MP_ERROR, "NdisAllocateMdl failed\n");
        NdisFreeMemory(Frame, (UINT)NumberOfBytes, 0);
        return NULL;
    }

    DEBUGP(MP_TRACE, "<--- HWFrameAllocate. Frame: %p\n", Frame);

    return Frame;
}


_Use_decl_annotations_
VOID
HWFrameFree (
    PVOID  Memory)
/*++

Routine Description:

    This routine frees memory for a FRAME.  It is a FREE_FUNCTION, the
    reciprocal of HWFrameAllocate.

    Runs at IRQL <= DISPATCH_LEVEL

Arguments:

    Memory                      A buffer allocated with HWFrameAllocate

Return Value:

    None.

--*/
{
    PFRAME Frame = (PFRAME) Memory;

    DEBUGP(MP_TRACE, "---> HWFrameFree. Frame: %p\n", Frame);

    ASSERT(Frame && Frame->Mdl);

    NdisFreeMdl(Frame->Mdl);
    NdisFreeMemory(Frame, sizeof(FRAME), 0);

    DEBUGP(MP_TRACE, "<--- HWFrameFree\n");

}


VOID
HWFrameReference(
    _In_  PFRAME  Frame)
/*++

Routine Description:

    This routine increments the reference count of a FRAME.

    Runs at IRQL <= DISPATCH_LEVEL

Arguments:

    Frame                       Frame to reference

Return Value:

    None.

--*/
{
    DEBUGP(MP_TRACE, "---> HWFrameReference. Frame: %p\n", Frame);

    NdisInterlockedIncrement(&Frame->Ref);

    DEBUGP(MP_TRACE, "<--- HWFrameReference. Frame: %p\n", Frame);

}


VOID
HWFrameRelease(
    _In_  PFRAME  Frame)
/*++

Routine Description:

    This routine decrements the reference count of a FRAME.  If the last
    refernce was released, the FRAME is freed back to the unused pool.

    Runs at IRQL <= DISPATCH_LEVEL

Arguments:

    Frame                       Frame to release

Return Value:

    None.

--*/
{
    DEBUGP(MP_TRACE, "---> HWFrameRelease. Frame: %p\n", Frame);

    if (0 == NdisInterlockedDecrement(&Frame->Ref))
    {
        DEBUGP(MP_TRACE, "---> Freeing Frame: %p\n", Frame);

        NdisFreeToNPagedLookasideList(&GlobalData.FrameDataLookaside, Frame);
        Frame = NULL;
    }

    DEBUGP(MP_TRACE, "<--- HWFrameRelease. Frame: %p\n", Frame);

}


NDIS_STATUS
HWCopyBytesFromNetBuffer(
    _In_     PNET_BUFFER        NetBuffer,
    _Inout_  PULONG             cbDest,
    _Out_writes_bytes_to_(*cbDest, *cbDest) PVOID Dest)
/*++

Routine Description:

    Copies the first cbDest bytes from a NET_BUFFER. In order to show how the various data structures fit together, this
    implementation copies the data by iterating through the MDLs for the NET_BUFFER. The NdisGetDataBuffer API also allows you
    to copy a contiguous block of data from a NET_BUFFER.

    Runs at IRQL <= DISPATCH_LEVEL.

Arguments:

    NetBuffer                   The NB to read
    cbDest                      On input, the number of bytes in the buffer Dest
                                On return, the number of bytes actually copied
    Dest                        On return, receives the first cbDest bytes of
                                the network frame in NetBuffer

Return Value:

    None.

Notes:

    If the output buffer is larger than the NB's frame size, *cbDest will
    contain the number of bytes in the frame size.

    If the output buffer is smaller than the NB's frame size, only the first
    *cbDest bytes will be copied (the buffer will receive a truncated copy of
    the frame).

--*/
{
    NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
    //
    // Start copy from current MDL
    //
    PMDL CurrentMdl = NET_BUFFER_CURRENT_MDL(NetBuffer);
    //
    // Data on current MDL may be offset from start of MDL
    //
    ULONG DestOffset = 0;
    while (DestOffset < *cbDest && CurrentMdl)
    {
        //
        // Map MDL memory to System Address Space. LowPagePriority means mapping may fail if
        // system is low on memory resources.
        //
        PUCHAR SrcMemory = MmGetSystemAddressForMdlSafe(CurrentMdl, LowPagePriority | MdlMappingNoExecute);
        ULONG Length = MmGetMdlByteCount(CurrentMdl);
        if (!SrcMemory)
        {
            Status = NDIS_STATUS_RESOURCES;
            break;
        }

        if(DestOffset==0)
        {
            //
            // The first MDL segment should be accessed from the current MDL offset
            //
            ULONG MdlOffset = NET_BUFFER_CURRENT_MDL_OFFSET(NetBuffer);
            SrcMemory += MdlOffset;
            Length -= MdlOffset;
        }

        Length = min(Length, *cbDest-DestOffset);

        //
        // Copy Memory
        //
        NdisMoveMemory((PUCHAR)Dest+DestOffset, SrcMemory, Length);
        DestOffset += Length;

        //
        // Get next MDL (if any available)
        //
        CurrentMdl = NDIS_MDL_LINKAGE(CurrentMdl);
    }

    if(Status == NDIS_STATUS_SUCCESS)
    {
        *cbDest = DestOffset;
    }

    return Status;
}


NDIS_STATUS
HWGetDestinationAddress(
    _In_  PNET_BUFFER  NetBuffer,
    _Out_writes_bytes_(NIC_MACADDR_SIZE) PUCHAR DestAddress)
/*++

Routine Description:

    Returns the destination address of a NET_BUFFER that is to be sent.

    Runs at IRQL <= DISPATCH_LEVEL.

Arguments:

    NetBuffer                   The NB containing the frame that is being sent
    DestAddress                 On return, receives the frame's destination

Return Value:

    NDIS_STATUS_FAILURE         The frame is too short
    NDIS_STATUS_SUCCESS         Else

--*/
{
    NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
    NIC_FRAME_HEADER Header;
    ULONG cbHeader = sizeof(Header);

    Status = HWCopyBytesFromNetBuffer(NetBuffer, &cbHeader, &Header);
    if(Status == NDIS_STATUS_SUCCESS)
    {
        if (cbHeader < sizeof(Header))
        {
            NdisZeroMemory(DestAddress, NIC_MACADDR_SIZE);
            Status = NDIS_STATUS_FAILURE;
        }
        else
        {
            GET_DESTINATION_OF_FRAME(DestAddress, &Header);
        }
    }
	else
	{
		NdisZeroMemory(DestAddress, NIC_MACADDR_SIZE);
	}

    return Status;
}

BOOLEAN
HWIsFrameAcceptedByPacketFilter(
    _In_  PMP_ADAPTER  Adapter,
    _In_reads_bytes_(NIC_MACADDR_SIZE) PUCHAR  DestAddress,
    _In_  ULONG        FrameType)
/*++

Routine Description:

    This routines checks to see whether the packet can be accepted
    for transmission based on the currently programmed filter type
    of the NIC and the mac address of the packet.

    With real adapter, this routine would be implemented in hardware.  However,
    since we don't have any hardware to do the matching for us, we'll do it in
    the driver.

Arguments:

    Adapter                     Our adapter that is receiving a frame
    FrameData                   The raw frame, starting at the frame header
    cbFrameData                 Number of bytes in the FrameData buffer

Return Value:

    TRUE if the frame is accepted by the packet filter, and should be indicated
    up to the higher levels of the stack.

    FALSE if the frame doesn't match the filter, and should just be dropped.

--*/
{
    BOOLEAN     result = FALSE;

    DEBUGP(MP_LOUD, "[%p] ---> HWIsFrameAcceptedByPacketFilter PacketFilter = 0x%08x, FrameType = 0x%08x\n",
            Adapter,
            Adapter->PacketFilter,
            FrameType);

    do
    {
        //
        // If the NIC is in promiscuous mode, we will accept anything
        // and everything.
        //
        if (Adapter->PacketFilter & NDIS_PACKET_TYPE_PROMISCUOUS)
        {
            result = TRUE;
            break;
        }


        switch (FrameType)
        {
            case NDIS_PACKET_TYPE_BROADCAST:
                if (Adapter->PacketFilter & NDIS_PACKET_TYPE_BROADCAST)
                {
                    //
                    // If it's a broadcast packet and broadcast is enabled,
                    // we can accept that.
                    //
                    result = TRUE;
                }
                break;


            case NDIS_PACKET_TYPE_MULTICAST:
                //
                // If it's a multicast packet and multicast is enabled,
                // we can accept that.
                //
                if (Adapter->PacketFilter & NDIS_PACKET_TYPE_ALL_MULTICAST)
                {
                    result = TRUE;
                    break;
                }
                else if (Adapter->PacketFilter & NDIS_PACKET_TYPE_MULTICAST)
                {
                    ULONG index;

                    //
                    // Check to see if the multicast address is in our list
                    //
                    ASSERT(Adapter->ulMCListSize <= NIC_MAX_MCAST_LIST);
                    for (index=0; index < Adapter->ulMCListSize && index < NIC_MAX_MCAST_LIST; index++)
                    {
                        if (NIC_ADDR_EQUAL(DestAddress, Adapter->MCList[index]))
                        {
                            result = TRUE;
                            break;
                        }
                    }
                }
                break;


            case NDIS_PACKET_TYPE_DIRECTED:

                if (Adapter->PacketFilter & NDIS_PACKET_TYPE_DIRECTED)
                {
                    //
                    // This has to be a directed packet. If so, does packet dest
                    // address match with the mac address of the NIC.
                    //
                    if (NIC_ADDR_EQUAL(DestAddress, Adapter->CurrentAddress))
                    {
                        result = TRUE;
                        break;
                    }
                }

                break;
        }

    } while(FALSE);


    DEBUGP(MP_LOUD, "[%p] <--- HWIsFrameAcceptedByPacketFilter Result = %u\n", Adapter, result);
    return result;
}


NDIS_MEDIA_CONNECT_STATE
HWGetMediaConnectStatus(
    _In_  PMP_ADAPTER Adapter)
/*++

Routine Description:

    This routine will query the hardware and return
    the media status.

Arguments:

    Adapter                     Our Adapter

Return Value:

    NdisMediaStateDisconnected or
    NdisMediaStateConnected

--*/
{
    if (MP_TEST_FLAG(Adapter, fMP_DISCONNECTED))
    {
        return MediaConnectStateDisconnected;
    }
    else
    {
        return MediaConnectStateConnected;
    }
}

VOID
HWProgramDmaForSend(
    _In_  PMP_ADAPTER   Adapter,
    _In_  PTCB          Tcb,
    _In_  PNET_BUFFER   NetBuffer,
    _In_  BOOLEAN       fAtDispatch)
/*++

Routine Description:

    Program the hardware to read the data payload from the NET_BUFFER's MDL
    and queue it for transmission.  When the hardware has finished reading the
    MDL, it will fire an interrupt to indicate that it no longer needs the MDL
    anymore.

    Our hardware, of course, doesn't have any DMA, so it just copies the data
    to a FRAME structure and transmits that.


    Runs at IRQL <= DISPATCH_LEVEL

Arguments:

    Adapter                     Our adapter that will send a frame
    Tcb                         The TCB that tracks the transmit status
    NetBuffer                   Contains the data to send
    fAtDispatch                 TRUE if the current IRQL is DISPATCH_LEVEL

Return Value:

    None.

--*/
{
    PFRAME Frame;
    NDIS_NET_BUFFER_LIST_8021Q_INFO Nbl1QInfo = {0};
    PNET_BUFFER_LIST Nbl = NULL;
    NDIS_STATUS Status = NDIS_STATUS_SUCCESS;

    DEBUGP(MP_TRACE, "[%p] ---> HWProgramDmaForSend. NB: 0x%p\n", Adapter, NetBuffer);

    do
    {
        //
        // Program the hardware to begin reading the data from NetBuffer's MDL and
        // queue it for transmission.
        //

        Tcb->NetBuffer = NetBuffer;
        Tcb->BytesActuallySent = 0;

        Frame = (PFRAME)NdisAllocateFromNPagedLookasideList(&GlobalData.FrameDataLookaside);

        if (!Frame)
        {
            DEBUGP(MP_TRACE, "[%p] ---> No frames available for send.\n", Adapter);
            //
            // Oops, we couldn't get any more FRAMEs to send.
            //
            // When the SendComplete fires, we'll tell the driver that zero bytes
            // were sent successfully.  It will update the bookkeeping and inform
            // the protocol that the NBL wasn't completely sent.
            //
            break;
        }

        DEBUGP(MP_TRACE, "[%p] Send Frame: 0x%p\n", Adapter, Frame);
        ASSERT(NET_BUFFER_DATA_LENGTH(NetBuffer) <= NIC_BUFFER_SIZE);

        Frame->Ref = 1;

        //
        // Copy the data from the NB to the FRAME's data region.  This step roughly
        // corresponds to a hardware DMA.
        //
        Frame->ulSize = min(NET_BUFFER_DATA_LENGTH(NetBuffer), NIC_BUFFER_SIZE);
        Status = HWCopyBytesFromNetBuffer(NetBuffer, &Frame->ulSize, Frame->Data);
        if(Status != NDIS_STATUS_SUCCESS)
        {
            DEBUGP(MP_TRACE, "[%p] ---> Failed to copy frame buffer. Result = %u\n", Adapter, Status);
            break;
        }

        if (Frame->ulSize < HW_MIN_FRAME_SIZE)
        {
            // Don't leak the contents of kernel memory!  Zero out padding bytes.
            ULONG cbPaddingNeeded = HW_MIN_FRAME_SIZE - Frame->ulSize;
            NdisZeroMemory(Frame->Data + Frame->ulSize, cbPaddingNeeded);
            Frame->ulSize += cbPaddingNeeded;
        }

        ASSERT(Frame->ulSize >= HW_MIN_FRAME_SIZE && Frame->ulSize <= NIC_BUFFER_SIZE);


        //
        // For simplicity in the sample in order to support VLAN we extract the information from the NBL or frame and pass the
        // NDIS_NET_BUFFER_LIST_8021Q_INFO structure to the code that simulates the send/receive. The code does nothing to convert
        // modify the frame format.
        // In real HW, on send the code should extract the information from the NBL and covert it to 802.1Q format for transmission, and
        // on receive the adapter should detect if the packet is in 802.1Q format and if so convert it back to 802.3 before indicating it up to NDIS
        // (populating the 8021Q info in the NBL being indicated).
        //
        Nbl = NBL_FROM_SEND_NB(NetBuffer);
        Nbl1QInfo.Value = NET_BUFFER_LIST_INFO(Nbl, Ieee8021QNetBufferListInfo);

        if(Nbl1QInfo.Value)
        {
            DEBUGP(MP_TRACE, "[%p] Send NBL (%p) OOB Vlan ID: %i\n", Adapter, Nbl, Nbl1QInfo.TagHeader.VlanId);
        }
        else
        {
            DEBUGP(MP_TRACE, "[%p] Send NBL (%p) has no OOB VLAN tag, checking frame header.\n", Adapter, Nbl);
            if(IS_FRAME_8021Q(Frame))
            {
                //
                // The frame has type of 802.1Q. Retrieve the VLAN information
                //
                COPY_TAG_INFO_FROM_HEADER_TO_PACKET_INFO(Nbl1QInfo, GET_FRAME_VLAN_TAG_HEADER(Frame));
                DEBUGP(MP_TRACE, "[%p] Send NBL (%p) frame Vlan ID: %i\n", Adapter, Nbl, Nbl1QInfo.TagHeader.VlanId);
            }
            else
            {
                DEBUGP(MP_TRACE, "[%p] Send NBL (%p) has no VLAN information in its frame header.\n", Adapter, Nbl);
            }
        }

        RXDeliverFrameToEveryAdapter(Adapter, &Nbl1QInfo, Frame, fAtDispatch);

        Tcb->BytesActuallySent = Frame->ulSize;

    } while(FALSE);

    if(Frame)
    {
        HWFrameRelease(Frame);
    }

    DEBUGP(MP_TRACE, "[%p] <--- HWProgramDmaForSend\n", Adapter);

}


_IRQL_requires_(DISPATCH_LEVEL)
ULONG
HWGetBytesSent(
    _In_  PMP_ADAPTER  Adapter,
    _In_  PTCB         Tcb)
/*++

Routine Description:

    When the adapter indicates it has completed the send operation, call this
    routine to determine how many bytes were successfully sent (if any).

Arguments:

    Adapter                     Our adapter that will send a frame
    Tcb                         The TCB that tracks the transmit status

Return Value:

    0                           There was an error sending this frame
    >0                          Number of bytes actually sent

--*/
{
    UNREFERENCED_PARAMETER(Adapter);

    return Tcb->BytesActuallySent;
}

NDIS_STATUS
HWBeginReceiveDma(
    _In_  PMP_ADAPTER  Adapter,
    _In_  PNDIS_NET_BUFFER_LIST_8021Q_INFO Nbl1QInfo,
    _In_  PRCB         Rcb,
    _In_  PFRAME       Frame)
/*++

Routine Description:

    Simulate the hardware deciding to receive a FRAME into one of its RCBs. In VMQ enabled scenarios, it will
    find the matching queue and if matched retrieve the shared memory for the queue for the NBL. Otherwise, it
    uses the existing Frame for the NBL.

Arguments:

    Adapter                     Pointer to our adapter
    Nbl1QInfo                   8021Q Tag information for the FRAME being received
    Rcb                         The RCB that tracks this receive operation
    Frame                       The FRAME that to receive

Return Value:

    NDIS_STATUS

--*/
{

    NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
    PNET_BUFFER NetBuffer = NET_BUFFER_LIST_FIRST_NB(Rcb->Nbl);

    do
    {
        DEBUGP(MP_TRACE, "[%p] ---> HWBeginReceiveDma. Frame: 0x%p\n", Adapter, Frame);

        //
        // Preserve 802.1Q information, if specified.
        //
        if(Nbl1QInfo->Value)
        {
            DEBUGP(MP_TRACE, "[%p] Preserved VLAN Id=%i\n", Adapter, Nbl1QInfo->TagHeader.VlanId);
            NET_BUFFER_LIST_INFO(Rcb->Nbl, Ieee8021QNetBufferListInfo) = Nbl1QInfo->Value;
        }
        else
        {
            DEBUGP(MP_TRACE, "[%p] No VLAN tag to preserve.\n", Adapter);
            NET_BUFFER_LIST_INFO(Rcb->Nbl, Ieee8021QNetBufferListInfo) = 0;
        }

        //
        // If VMQ is enabled, and we're not using the default queue,
        // we need to copy the FRAME to the NBL's shared memory area
        //
        if(VMQ_ENABLED(Adapter))
        {
            BOOLEAN Copied;
            Status = CopyFrameToRxQueueRcb(Adapter, Frame, Nbl1QInfo, Rcb, &Copied);
            if(Copied || Status != NDIS_STATUS_SUCCESS)
            {
                break;
            }
        }
        else
        {
            UNREFERENCED_PARAMETER(Adapter);
        }

        //
        // Either 6.20 is not supported, VMQ is disabled, or matched default queue, so use existing frame memory for indication
        //
        HWFrameReference(Frame);
        Rcb->Data = Frame;

        NET_BUFFER_FIRST_MDL(NetBuffer) = Frame->Mdl;
        NET_BUFFER_DATA_LENGTH(NetBuffer) = Frame->ulSize;
        NET_BUFFER_DATA_OFFSET(NetBuffer) = 0;
        NET_BUFFER_CURRENT_MDL(NetBuffer) = NET_BUFFER_FIRST_MDL(NetBuffer);
        NET_BUFFER_CURRENT_MDL_OFFSET(NetBuffer) = 0;

    }
    while(FALSE);

    DEBUGP(MP_TRACE, "[%p] <--- HWBeginReceiveDma Status 0x%08x\n", Adapter, Status);
    return Status;

}