summaryrefslogtreecommitdiffstats
path: root/private/ntos/ndis/dc21x4/filter.c
blob: d358f5d35e8799c0b272c8d3fb81b9e711d5808f (plain) (blame)
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
/*+
 * file:        filter.c
 *
 * Copyright (C) 1992-1995 by
 * Digital Equipment Corporation, Maynard, Massachusetts.
 * All rights reserved.
 *
 * This software is furnished under a license and may be used and copied
 * only  in  accordance  of  the  terms  of  such  license  and with the
 * inclusion of the above copyright notice. This software or  any  other
 * copies thereof may not be provided or otherwise made available to any
 * other person.  No title to and  ownership of the  software is  hereby
 * transferred.
 *
 * The information in this software is  subject to change without notice
 * and  should  not  be  construed  as a commitment by digital equipment
 * corporation.
 *
 * Digital assumes no responsibility for the use  or  reliability of its
 * software on equipment which is not supplied by digital.
 *
 *
 * Abstract:    This file is part of the NDIS 4.0 miniport driver for DEC's
 *              DC21X4 Ethernet adapter family.
 *
 * Author:      Philippe Klein
 *
 * Revision History:
 *
 *      phk     31-Jul-94   Creation date
 *
 *      phk     18-dec-94   Add a dummy descriptor in front of the setup
 *                          descriptor to cover the underrun before
 *                          setup descriptor case.
 *
-*/

#include <precomp.h>




#pragma NDIS_PAGABLE_FUNCTION(DC21X4InitializeCam)

/*+
 *
 * DC21X4InitializeCam
 *
 * Routine Description:
 *
 *    Initialize the DC21X4 CAM
 *
-*/
extern
VOID
DC21X4InitializeCam (
    IN PDC21X4_ADAPTER Adapter,
    IN PUSHORT Address
    )
{

   UINT i;
   PDC21X4_SETUP_BUFFER SetupBuffer;

   SetupBuffer = (PDC21X4_SETUP_BUFFER)Adapter->SetupBufferVa;

   // Perfect Filtering Setup Buffer

   Adapter->FilteringMode = DC21X4_PERFECT_FILTERING;

   // Copy Address into the first entry

   for (i=0; i<3; i++) {

      (USHORT)(SetupBuffer->Perfect.PhysicalAddress[0][i]) = *Address++;
   }

   // Duplicate the first entry in all the remaining
   // entries of the filter

   for (i = 1; i < DC21X4_SETUP_PERFECT_ENTRIES; i++) {

      MOVE_MEMORY(
         SetupBuffer->Perfect.PhysicalAddress[i],
         SetupBuffer->Perfect.PhysicalAddress[0],
         sizeof(ULONG)*3
         );
   }

   return;
}










/*+
 *
 * DC21X4LoadCam
 *
 * Routine Description:
 *
 *    Load the Setup Buffer into DC21X4's CAM
 *
 * Arguments:
 *
 *    Adapter       - the Adapter for the hardware
 *    InterruptMode - Synchronize the Setup Buffer completion on interrupt
 *
-*/
extern
BOOLEAN
DC21X4LoadCam (
    IN PDC21X4_ADAPTER Adapter,
    IN BOOLEAN InterruptMode
    )
{
   PDC21X4_TRANSMIT_DESCRIPTOR SetupDescriptor;
   PDC21X4_TRANSMIT_DESCRIPTOR DummyDescriptor;
   UINT Time = DC21X4_SETUP_TIMEOUT;
   ULONG DC21X4Status;
   ULONG DC21X4Command;

#if _DBG
   PULONG t;
   UINT i;
   DbgPrint("DC21X4LoadCam\n");
#endif


   if (!InterruptMode) {

      // Mask the interrupts
      DC21X4_WRITE_PORT(
         DC21X4_INTERRUPT_MASK,
         0
         );
   }


   if (Adapter->FullDuplex) {
      NdisDprAcquireSpinLock(&Adapter->EnqueueSpinLock);
   }

   DummyDescriptor = Adapter->EnqueueTransmitDescriptor;
   Adapter->EnqueueTransmitDescriptor = (Adapter->EnqueueTransmitDescriptor)->Next;

   SetupDescriptor = Adapter->EnqueueTransmitDescriptor;
   Adapter->EnqueueTransmitDescriptor = (Adapter->EnqueueTransmitDescriptor)->Next;

   if (Adapter->FullDuplex) {
       NdisDprReleaseSpinLock(&Adapter->EnqueueSpinLock);
       NdisDprAcquireSpinLock(&Adapter->FullDuplexSpinLock);
   }

   Adapter->FreeTransmitDescriptorCount -= 2;

   if (Adapter->FullDuplex) {
       NdisDprReleaseSpinLock(&Adapter->FullDuplexSpinLock);
   }

   // Initialize the Dummy descriptor

   DummyDescriptor->Control &= DC21X4_TDES_SECOND_ADDR_CHAINED;
   DummyDescriptor->FirstBufferAddress = 0;
   DummyDescriptor->Status = DESC_OWNED_BY_DC21X4;

   // Initialize the Setup descriptor

   SetupDescriptor->Control &= DC21X4_TDES_SECOND_ADDR_CHAINED;
   SetupDescriptor->Control |=
      (DC21X4_TDES_SETUP_PACKET | DC21X4_TDES_INTERRUPT_ON_COMPLETION
      | DC21X4_SETUP_BUFFER_SIZE);

   if (Adapter->FilteringMode != DC21X4_PERFECT_FILTERING) {
      SetupDescriptor->Control |= DC21X4_TDES_HASH_FILTERING;
   }

   SetupDescriptor->FirstBufferAddress = Adapter->SetupBufferPa;
   SetupDescriptor->Status = DESC_OWNED_BY_DC21X4;

#if _DBG
   DbgPrint("Setup Descriptor: %08x\n %08x\n %08x\n %08x\n %08x\n",
   SetupDescriptor,
   SetupDescriptor->Status,
      SetupDescriptor->Control,
      SetupDescriptor->FirstBufferAddress,
      SetupDescriptor->SecondBufferAddress);

   t = (PULONG)Adapter->SetupBufferVa;
   DbgPrint("Setup buffer:\n");
   for (i = 0; i < 16; i++, t+=3) {
      DbgPrint("  [%08x] %08x %08x %08x\n",t,*t,*(t+1),*(t+2));
   }
#endif


   // Start or Poll the Transmitter to process the Setup Frame

   if ((Adapter->DeviceId == DC21040_CFID) &&
      (Adapter->RevisionNumber == DC21040_REV1)) {

      // Txm hang workaround : Disable the SIA before
      // writing the Operation Mode register
      // SFD workaround : Disable the Receiver before processing
      // the setup packet

      DC21X4_WRITE_PORT(
         DC21X4_SIA_MODE_0,
         DC21X4_RESET_SIA
         );

      NdisStallExecution(1*MILLISECOND);      // Wait 1 ms

      DC21X4_WRITE_PORT(
         DC21X4_OPERATION_MODE,
         (Adapter->OperationMode & ~DC21X4_RCV_START)
         );
   }

   DC21X4_READ_PORT(
      DC21X4_OPERATION_MODE,
      &DC21X4Command
      );

   if (DC21X4Command & DC21X4_TXM_START) {

      DC21X4_WRITE_PORT(
         DC21X4_OPERATION_MODE,
         Adapter->OperationMode
         );

      if (!Adapter->DisableTransmitPolling) {

#if _DBG
         DbgPrint("  Txm Poll_demand\n");
#endif
         DC21X4_WRITE_PORT(
             DC21X4_TXM_POLL_DEMAND,
             1
             );
      }

   }
   else {
#if _DBG
      DbgPrint("  Start Txm\n");
#endif
      Adapter->OperationMode |= DC21X4_TXM_START;
      DC21X4_WRITE_PORT(
         DC21X4_OPERATION_MODE,
         Adapter->OperationMode
         );
   }

   if (InterruptMode) {
      return TRUE;
   }
   else {

      // Poll for completion

      while (Time--) {

         NdisStallExecution(5*MILLISECOND);  //wait 5 ms

            DC21X4_READ_PORT(
            DC21X4_STATUS,
            &DC21X4Status
            );

         if (DC21X4Status & DC21X4_TXM_BUFFER_UNAVAILABLE) {

            DC21X4_WRITE_PORT(
               DC21X4_STATUS,
               DC21X4_TXM_INTERRUPTS
               );
            break;
         }

      }

      Adapter->DequeueTransmitDescriptor =
         ((Adapter->DequeueTransmitDescriptor)->Next)->Next;

      if (Adapter->FullDuplex) {
         NdisDprAcquireSpinLock(&Adapter->FullDuplexSpinLock);
      }

      Adapter->FreeTransmitDescriptorCount += 2;

      if (Adapter->FullDuplex) {
         NdisDprReleaseSpinLock(&Adapter->FullDuplexSpinLock);
      }

      if ((Adapter->DeviceId == DC21040_CFID) &&
          (Adapter->RevisionNumber == DC21040_REV1)) {

         //Restart the Receiver and Enable the SIA

         DC21X4_WRITE_PORT(
            DC21X4_OPERATION_MODE,
            Adapter->OperationMode
            );

         DC21X4_WRITE_PORT(
            DC21X4_SIA_MODE_0,
            Adapter->Media[Adapter->SelectedMedium].SiaRegister[0]
            );
      }

      if (Time > 0) {

         //Restore the interrupt mask
         DC21X4_WRITE_PORT(
            DC21X4_INTERRUPT_MASK,
            Adapter->InterruptMask
            );

         return TRUE;
      }
      else {
         return FALSE;
      }

   }

}












/*++
 *
 * DC21X4ChangeFilter
 *
 * Routine Description:
 *
 *    Action routine called when a filter class is modified
 *
-*/
extern
NDIS_STATUS
DC21X4ChangeFilter (
    IN PDC21X4_ADAPTER Adapter,
    IN ULONG NewFilterClass
    )
{

   ULONG Command;
   ULONG PrevFilterClass;

#if _DBG
   DbgPrint("DC21X4ChangeClasses NewClass= %x\n",NewFilterClass);
#endif

   PrevFilterClass = Adapter->FilterClass;
   Adapter->FilterClass = NewFilterClass;

   Command = Adapter->OperationMode &
      ~(DC21X4_PROMISCUOUS_MODE | DC21X4_PASS_ALL_MULTICAST);

   if (NewFilterClass & NDIS_PACKET_TYPE_PROMISCUOUS) {
#if _DBG
      DbgPrint("  Promiscuous Mode\n");
#endif
      Command |= DC21X4_PROMISCUOUS_MODE;
   }

   else if (NewFilterClass & NDIS_PACKET_TYPE_ALL_MULTICAST) {
#if _DBG
      DbgPrint("  All_Multicast Mode\n");
#endif
      Command |= DC21X4_PASS_ALL_MULTICAST;
   }

   if (NewFilterClass & NDIS_PACKET_TYPE_BROADCAST) {

#if _DBG
      DbgPrint("  Broadcast Mode\n");
#endif
      // If broadcast was not previously enabled an entry for the
      // broadcast address should be added into the CAM

      if (!(PrevFilterClass & NDIS_PACKET_TYPE_BROADCAST)) {

         AddBroadcastToSetup (
            Adapter
            );
         Adapter->OperationMode = Command;

         DC21X4LoadCam(
            Adapter,
            TRUE
            );

         return NDIS_STATUS_PENDING;
      }
   }

   else if (PrevFilterClass & NDIS_PACKET_TYPE_BROADCAST) {

      // Broadcast was previously enabled and should be
      // disabled now by removing the braodcast address entry
      // from to CAM

      RemoveBroadcastFromSetup (
         Adapter
         );
      Adapter->OperationMode = Command;

      DC21X4LoadCam(
         Adapter,
         TRUE
         );

      return NDIS_STATUS_PENDING;
   }

   if (Command != Adapter->OperationMode) {

      // Modify the DC21X4 Serial Command Register

      Adapter->OperationMode = Command;

      switch (Adapter->DeviceId) {

         case DC21040_CFID:

            if (Adapter->RevisionNumber == DC21040_REV1) {

            // Txm hang workaround : Disable the SIA before
            //    writing the Operation Mode register

            DC21X4_WRITE_PORT(
               DC21X4_SIA_MODE_0,
               DC21X4_RESET_SIA
               );

            NdisStallExecution(1*MILLISECOND);      // Wait 1 ms


               DC21X4_WRITE_PORT(
               DC21X4_OPERATION_MODE,
               Adapter->OperationMode
               );

            DC21X4_WRITE_PORT(
               DC21X4_SIA_MODE_0,
               Adapter->Media[Adapter->SelectedMedium].SiaRegister[0]
               );
            break;
         }

         default:

            DC21X4_WRITE_PORT(
            DC21X4_OPERATION_MODE,
            Adapter->OperationMode
            );
      }

   }

   return NDIS_STATUS_SUCCESS;
}










/*+
 *
 * AddBroadcastToSetup (Adapter);
 *
 * Routine Description:
 *
 *    Add the Broadcast address to the DC21X4 Setup Buffer
 *
-*/

VOID
AddBroadcastToSetup (
    IN PDC21X4_ADAPTER Adapter
    )

{

   PDC21X4_SETUP_BUFFER SetupBuffer;
   UINT i;

#if _DBG
   PULONG t;
   DbgPrint("AddBroadcastToSetup\n");
#endif

   SetupBuffer = (PDC21X4_SETUP_BUFFER)Adapter->SetupBufferVa;

   if (Adapter->FilteringMode == DC21X4_PERFECT_FILTERING) {

      // Load the broadcast address in the second entry
      // of the Setup Buffer

      for (i=0; i<3; i++) {

         SetupBuffer->Perfect.PhysicalAddress[1][i] = 0x0000ffff;
      }

   }
   else {

      // Add the Broadcast hit to the Hash Filter
      // (HashIndex(Broadcast_address) = 255)

      SetupBuffer->Hash.Filter[15] |= 0x8000;
   }

}










/*+
 *
 * RemoveBroadcastFromSetup (Adapter);
 *
 * Routine Description:
 *
 *    Remove the Broadcast address from the DC21X4 Setup Buffer
 *
-*/

VOID
RemoveBroadcastFromSetup (
    IN PDC21X4_ADAPTER Adapter
    )
{

   PDC21X4_SETUP_BUFFER SetupBuffer;

#if _DBG
   DbgPrint("RemoveBroadcastFromSetup\n");
#endif

   SetupBuffer = (PDC21X4_SETUP_BUFFER)Adapter->SetupBufferVa;

   if (Adapter->FilteringMode == DC21X4_PERFECT_FILTERING) {

      // Duplicate the Adapter entry (1st entry) into the
      // broadcast entry (2nd entry)

      MOVE_MEMORY (
         SetupBuffer->Perfect.PhysicalAddress[1],
         SetupBuffer->Perfect.PhysicalAddress[0],
         sizeof(ULONG)*3
         );
   }
   else {

      // Remove the Broadcast hit from the Hash Filter
      // (HashIndex(Broadcast_address) = 255)

      SetupBuffer->Hash.Filter[15] &= ~0x8000;
   }
}










/*+
 *
 * DC21X4ChangeAddresses
 *
 * Routine Description:
 *
 *    Load a new Multicast Addresse list into the adapter's CAM
 *
-*/
extern
NDIS_STATUS
DC21X4ChangeMulticastAddresses(
    IN PDC21X4_ADAPTER Adapter,
    IN PVOID MulticastAddresses,
    IN UINT AddressCount
    )

{
   PDC21X4_SETUP_BUFFER SetupBuffer;

   PUSHORT AsUShort;
   PUCHAR  MultAddr;
   PULONG Buffer;
   UINT HashIndex;
   UINT i;

#if _DBG
   DbgPrint("DC21X4ChangeMulticastAddresses\n");
#endif

   if (AddressCount > Adapter->MaxMulticastAddresses) {
      return NDIS_STATUS_MULTICAST_FULL;
   }

   SetupBuffer = (PDC21X4_SETUP_BUFFER)Adapter->SetupBufferVa;

   // Reinitialize the Setup Buffer and load the DC21X4's CAM

   if (AddressCount <= DC21X4_MAX_MULTICAST_PERFECT)  {

#if _DBG
      DbgPrint("Perfect Filtering\n");
#endif
      // Perfect Filtering

      //The first entry is the Adapter address

      AsUShort = (PUSHORT)Adapter->CurrentNetworkAddress;

      for (i=0; i<3; i++,AsUShort++) {

         (USHORT)(SetupBuffer->Perfect.PhysicalAddress[0][i]) = *AsUShort;
      }

      //The second entry is a broadcast address if Broadcast_Enable
      //otherwise it is a duplicate of the first entry

      if ( Adapter->FilterClass & NDIS_PACKET_TYPE_BROADCAST) {

         for (i=0; i<3; i++) {

            SetupBuffer->Perfect.PhysicalAddress[1][i] = 0x0000ffff;
         }
      }
      else {
         MOVE_MEMORY (
            SetupBuffer->Perfect.PhysicalAddress[1],
            SetupBuffer->Perfect.PhysicalAddress[0],
            sizeof(ULONG)*3
            );
      }

      // Load the new multicast list;

      AsUShort = (PUSHORT)MulticastAddresses;
      Buffer =  (PULONG)SetupBuffer->Perfect.PhysicalAddress[2];

      for (i=0; i< AddressCount * 3; i++,AsUShort++) {
         *(PUSHORT)Buffer = *AsUShort;
         Buffer++;
      }

      // Duplicate the Adapter adress in the remaining entries
      // of the buffer

      for (i=AddressCount+2;  i< DC21X4_SETUP_PERFECT_ENTRIES; i++ ) {

         MOVE_MEMORY(
            SetupBuffer->Perfect.PhysicalAddress[i],
            SetupBuffer->Perfect.PhysicalAddress[0],
            sizeof(ULONG)*3
            );
      }

      Adapter->FilteringMode = DC21X4_PERFECT_FILTERING;

   }

   else {


#if _DBG
      DbgPrint("Hashing\n");
#endif
      //Hashing

      // Initialize the SetupBuffer

      NdisZeroMemory (
         (PVOID)(SetupBuffer->Hash.Filter),
         (ULONG)(sizeof(SetupBuffer->Hash))
         );

      // Load the hash filter

      MultAddr = (PUCHAR)MulticastAddresses;
      for (i=0; i < AddressCount; i++, MultAddr += ETH_LENGTH_OF_ADDRESS) {

         if (IS_MULTICAST(MultAddr)) {

            HashIndex = ~CRC32(MultAddr,ETH_LENGTH_OF_ADDRESS) & 0x1FF;
            SetupBuffer->Hash.Filter[HashIndex/16] |= (1 << (HashIndex % 16));
#if _DBG
            DbgPrint("   >>HashIndex = %d  [%d]bit=%d\n",HashIndex,(HashIndex/16),(HashIndex%16));
#endif
         }

      }


      if (Adapter->FilterClass & NDIS_PACKET_TYPE_BROADCAST) {

         // Add the Broadcast entry to the Hash Filter
         // (Hash_Index(Broadcast_address) = 255)

         SetupBuffer->Hash.Filter[15] |= 0x80000;
      }

      // Load the Adapter Address

      AsUShort = (PUSHORT)Adapter->CurrentNetworkAddress;
      for (i=0; i<3; i++, AsUShort++) {

         (USHORT)(SetupBuffer->Hash.PhysicalAddress[i]) = *AsUShort;
      }

      Adapter->FilteringMode = DC21X4_HASH_FILTERING;
   }

   DC21X4LoadCam(
      Adapter,
      TRUE
      );

   return NDIS_STATUS_PENDING;

}