summaryrefslogblamecommitdiffstats
path: root/private/ntos/ndis/ne3200/interrup.c
blob: d1fef19b0b63ec9bbb64468f294074dda4024e21 (plain) (tree)
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


































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                               
/*++

Copyright (c) 1990  Microsoft Corporation

Module Name:

    interrup.c

Abstract:

    This module contains the interrupt-processing code for the Novell
    NE3200 NDIS 3.0 miniport driver.

Author:

    Keith Moore (KeithMo) 04-Feb-1991

Environment:

Revision History:

--*/

#include <ne3200sw.h>

//
// Forward declarations of functions in this file
//
STATIC
BOOLEAN
FASTCALL
NE3200ProcessReceiveInterrupts(
    IN PNE3200_ADAPTER Adapter
    );

STATIC
BOOLEAN
FASTCALL
NE3200ProcessCommandInterrupts(
    IN PNE3200_ADAPTER Adapter
    );

VOID
NE3200Isr(
    OUT PBOOLEAN InterruptRecognized,
    OUT PBOOLEAN QueueDpc,
    IN PVOID Context
    )

/*++

Routine Description:

    Interrupt service routine for the NE3200.  It's main job is
    to get the value of the System Doorbell Register and record the
    changes in the adapters own list of interrupt reasons.

Arguments:

    Interrupt - Interrupt object for the NE3200.

    Context - Really a pointer to the adapter.

Return Value:

    Returns true if the interrupt really was from our NE3200.

--*/

{

    //
    // Will hold the value from the System Doorbell Register.
    //
    UCHAR SystemDoorbell;

    //
    // Holds the pointer to the adapter.
    //
    PNE3200_ADAPTER Adapter = Context;

    IF_LOG('i');

    //
    // Get the interrupt status
    //
    NE3200_READ_SYSTEM_DOORBELL_INTERRUPT(Adapter, &SystemDoorbell);

    //
    // Are any of the bits expected?
    //
    if (SystemDoorbell & NE3200_SYSTEM_DOORBELL_MASK) {

        IF_LOG(SystemDoorbell);

        //
        // It's our interrupt.  Disable further interrupts.
        //
        NE3200_WRITE_SYSTEM_DOORBELL_MASK(
            Adapter,
            0
            );

        IF_LOG('I');

        //
        // Return that we recognize it
        //
        *InterruptRecognized = TRUE;

    } else {

        IF_LOG('I');

        //
        // Return that we don't recognize it
        //
        *InterruptRecognized = FALSE;

    }

    //
    // No Dpc call is needed for initialization
    //
    *QueueDpc = FALSE;

}


STATIC
VOID
NE3200HandleInterrupt(
    IN NDIS_HANDLE MiniportAdapterContext
    )

/*++

Routine Description:

    Main routine for processing interrupts.

Arguments:

    Adapter - The Adapter to process interrupts for.

Return Value:

    None.

--*/

{
    //
    // The adapter for which to handle interrupts.
    //
    PNE3200_ADAPTER Adapter = ((PNE3200_ADAPTER)MiniportAdapterContext);

    //
    // Holds a value of SystemDoorbellInterrupt.
    //
    USHORT SystemDoorbell = 0;

    //
    // Should NdisMEthIndicateReceiveComplete() be called?
    //
    BOOLEAN IndicateReceiveComplete = FALSE;

    IF_LOG('p');

    //
    // Get the current reason for interrupts
    //
    NE3200_READ_SYSTEM_DOORBELL_INTERRUPT(Adapter, &SystemDoorbell);

    //
    // Acknowledge those interrupts.
    //
    NE3200_WRITE_SYSTEM_DOORBELL_INTERRUPT(
        Adapter,
        SystemDoorbell
        );

    //
    // Get just the important ones.
    //
    SystemDoorbell &= NE3200_SYSTEM_DOORBELL_MASK;

    while (TRUE) {

        //
        // If we have a reset in progress then start the reset.
        //

        if (Adapter->ResetInProgress) goto check_reset;

not_reset:

        //
        // Check the interrupt source and other reasons
        // for processing.  If there are no reasons to
        // process then exit this loop.
        //

        //
        // Check the interrupt vector and see if there are any
        // more receives to process.  After we process any
        // other interrupt source we always come back to the top
        // of the loop to check if any more receive packets have
        // come in.  This is to lessen the probability that we
        // drop a receive.
        //
        if (SystemDoorbell & NE3200_SYSTEM_DOORBELL_PACKET_RECEIVED) {

            IF_LOG('r');

            //
            // Process receive interrupts.
            //
            if (NE3200ProcessReceiveInterrupts(Adapter)) {

                //
                // If done with all receives, then clear the interrupt
                // from our status.
                //
                SystemDoorbell &= ~NE3200_SYSTEM_DOORBELL_PACKET_RECEIVED;

            }

            //
            // Note that we got a receive.
            //
            Adapter->ReceiveInterrupt = TRUE;
            IndicateReceiveComplete = TRUE;

            IF_LOG('R');

        } else if ((SystemDoorbell &
                 NE3200_SYSTEM_DOORBELL_COMMAND_COMPLETE) == 0 ) {

            //
            // If the command is not completed, and no receives, then
            // exit the loop.
            //
            break;
        }

        //
        // First we check that this is a packet that was transmitted
        // but not already processed.  Recall that this routine
        // will be called repeatedly until this tests false, Or we
        // hit a packet that we don't completely own.
        //
        if ((Adapter->FirstCommandOnCard == NULL) ||
            (Adapter->FirstCommandOnCard->Hardware.State != NE3200_STATE_EXECUTION_COMPLETE)) {

            //
            // No more work to do, clear the interrupt status bit.
            //
            IF_LOG('V');
            SystemDoorbell &= ~NE3200_SYSTEM_DOORBELL_COMMAND_COMPLETE;

        } else {

            IF_LOG('c');

            //
            // Complete this transmit.
            //
            if ( NE3200ProcessCommandInterrupts(Adapter) ) {
                SystemDoorbell &= ~NE3200_SYSTEM_DOORBELL_COMMAND_COMPLETE;
            }

            IF_LOG('C');

        }

        //
        // Get more interrupt bits for processing
        //
        if (SystemDoorbell == 0) {

            //
            // Get the current reason for interrupts
            //
            NE3200_READ_SYSTEM_DOORBELL_INTERRUPT(Adapter, &SystemDoorbell);

            //
            // Acknowledge those interrupts.
            //
            NE3200_WRITE_SYSTEM_DOORBELL_INTERRUPT(
                Adapter,
                SystemDoorbell
                );

            //
            // Get just the important ones.
            //
            SystemDoorbell &= NE3200_SYSTEM_DOORBELL_MASK;

        }

    }

done:

    IF_LOG('P');

    if (IndicateReceiveComplete) {

        NdisMEthIndicateReceiveComplete(Adapter->MiniportAdapterHandle);

    }

    return;

check_reset:

    if (Adapter->ResetState != NE3200ResetStateComplete) {

        //
        // The adapter is not in a state where it can process a reset.
        //
        goto not_reset;

    }

    //
    // Start the reset
    //
    NE3200DoAdapterReset(Adapter);

    goto done;

}

STATIC
BOOLEAN
FASTCALL
NE3200ProcessReceiveInterrupts(
    IN PNE3200_ADAPTER Adapter
    )

/*++

Routine Description:

    Process the packets that have the adapter has finished receiving.

Arguments:

    Adapter - The adapter to indicate to.

Return Value:

    Whether to clear interrupt bit or not.

--*/

{

    //
    // We don't get here unless there was a receive.  Loop through
    // the receive blocks starting at the last known block owned by
    // the hardware.
    //
    // Examine each receive block for errors.
    //
    // We keep an array whose elements are indexed by the block
    // index of the receive blocks.  The arrays elements are the
    // virtual addresses of the buffers pointed to by each block.
    //
    // After we find a packet we give the routine that process the
    // packet through the filter, the buffers virtual address (which
    // is always the lookahead size) and as the MAC Context the
    // index to the receive block.
    //

    //
    // Pointer to the receive block being examined.
    //
    PNE3200_SUPER_RECEIVE_ENTRY CurrentEntry = Adapter->ReceiveQueueHead;

    //
    // Pointer to last receive block in the queue.
    //
    PNE3200_SUPER_RECEIVE_ENTRY LastEntry;

    //
    // Limit the number of consecutive receives we will do.  This way
    // we do not starve transmit interrupts when processing many, many
    // receives
    //
#define MAX_RECEIVES_PROCESSED      10
    ULONG ReceivePacketCount = 0;


    //
    // Loop forever
    //
    while (TRUE) {

        //
        // Ensure that our Receive Entry is on an even boundary.
        //
        ASSERT(!(NdisGetPhysicalAddressLow(CurrentEntry->Self) & 1));

        //
        // Check to see whether we own the packet.  If
        // we don't then simply return to the caller.
        //
        if (CurrentEntry->Hardware.State != NE3200_STATE_FREE) {

            //
            // We've found a packet.  Prepare the parameters
            // for indication, then indicate.
            //
            if (ReceivePacketCount < MAX_RECEIVES_PROCESSED) {

                //
                // Increment the limit.
                //
                ReceivePacketCount++;

                //
                // Flush the receive buffer
                //
                NdisFlushBuffer(CurrentEntry->FlushBuffer, FALSE);

                //
                // Check the packet for a runt
                //
                if ((UINT)(CurrentEntry->Hardware.FrameSize) <
                    NE3200_HEADER_SIZE) {

                    if ((UINT)(CurrentEntry->Hardware.FrameSize) >=
                        NE3200_LENGTH_OF_ADDRESS) {

                        //
                        // Runt Packet, indicate it.
                        //
                        NdisMEthIndicateReceive(
                            Adapter->MiniportAdapterHandle,
                            (NDIS_HANDLE)(CurrentEntry->ReceiveBuffer),
                            CurrentEntry->ReceiveBuffer,
                            (UINT)CurrentEntry->Hardware.FrameSize,
                            NULL,
                            0,
                            0
                            );

                    }

                } else {

                    //
                    // Good frame, indicate it
                    //
                    NdisMEthIndicateReceive(
                        Adapter->MiniportAdapterHandle,
                        (NDIS_HANDLE)(CurrentEntry->ReceiveBuffer),
                        CurrentEntry->ReceiveBuffer,
                        NE3200_HEADER_SIZE,
                        ((PUCHAR)CurrentEntry->ReceiveBuffer) + NE3200_HEADER_SIZE,
                        (UINT)CurrentEntry->Hardware.FrameSize - NE3200_HEADER_SIZE,
                        (UINT)CurrentEntry->Hardware.FrameSize - NE3200_HEADER_SIZE
                        );

                }

                //
                // Give the packet back to the hardware.
                //
                // Chain the current block onto the tail of the Receive Queue.
                //
                CurrentEntry->Hardware.NextPending = NE3200_NULL;
                CurrentEntry->Hardware.State = NE3200_STATE_FREE;

                //
                // Update receive ring
                //
                LastEntry = Adapter->ReceiveQueueTail;
                LastEntry->Hardware.NextPending =
                    NdisGetPhysicalAddressLow(CurrentEntry->Self);

                //
                // Update the queue tail.
                //
                Adapter->ReceiveQueueTail = LastEntry->NextEntry;

                //
                // Advance to the next block.
                //
                CurrentEntry = CurrentEntry->NextEntry;

                //
                // See if the adapter needs to be restarted.  The NE3200
                // stops if it runs out receive buffers.  Since we just
                // released one, we restart the adapter.
                //
                if (LastEntry->Hardware.State != NE3200_STATE_FREE) {

                    //
                    // We've exhausted all Receive Blocks.  Now we
                    // must restart the adapter.
                    //
                    IF_LOG('O');
                    NE3200StartChipAndDisableInterrupts(Adapter, Adapter->ReceiveQueueTail);

                }

            } else {

                //
                // Update statistics, we are exiting to check for
                // transmit interrupts.
                //
                Adapter->ReceiveQueueHead = CurrentEntry;
                Adapter->GoodReceives += MAX_RECEIVES_PROCESSED+1;

                IF_LOG('o');

                return FALSE;

            }

        } else {

            //
            // All done, update statistics and exit.
            //
            Adapter->ReceiveQueueHead = CurrentEntry;
            Adapter->GoodReceives += ReceivePacketCount;
            return TRUE;

        }

    }

}


STATIC
BOOLEAN
FASTCALL
NE3200ProcessCommandInterrupts(
    IN PNE3200_ADAPTER Adapter
    )

/*++

Routine Description:

    Process the Command Complete interrupts.

Arguments:

    Adapter - The adapter that was sent from.

Return Value:

    None.

--*/

{

    //
    // Pointer to command block being processed.
    //
    PNE3200_SUPER_COMMAND_BLOCK CurrentCommandBlock = Adapter->FirstCommandOnCard;

    //
    // Holds whether the packet successfully transmitted or not.
    //
    NDIS_STATUS StatusToReturn;

    //
    // Pointer to the packet that started this transmission.
    //
    PNDIS_PACKET OwningPacket;

    //
    // Points to the reserved part of the OwningPacket.
    //
    PNE3200_RESERVED Reserved;

    //
    // Ensure that the Command Block is on an even boundary.
    //
    ASSERT(!(NdisGetPhysicalAddressLow(CurrentCommandBlock->Self) & 1));

    IF_LOG('t');

    if (CurrentCommandBlock->Hardware.CommandCode == NE3200_COMMAND_TRANSMIT) {

        //
        // The current command block is from a transmit.
        //
        Adapter->SendInterrupt = TRUE;

        //
        // Get a pointer to the owning packet and the reserved part of
        // the packet.
        //
        OwningPacket = CurrentCommandBlock->OwningPacket;
        Reserved = PNE3200_RESERVED_FROM_PACKET(OwningPacket);

        if (CurrentCommandBlock->UsedNE3200Buffer) {

            //
            // This packet used adapter buffers.  We can
            // now return these buffers to the adapter.
            //

            //
            // The adapter buffer descriptor that was allocated to this packet.
            //
            PNE3200_BUFFER_DESCRIPTOR BufferDescriptor = Adapter->NE3200Buffers +
                                                  CurrentCommandBlock->NE3200BuffersIndex;

            //
            // Put the adapter buffer back on the free list.
            //
            BufferDescriptor->Next = Adapter->NE3200BufferListHead;
            Adapter->NE3200BufferListHead = CurrentCommandBlock->NE3200BuffersIndex;

        } else {

            //
            // Ndis buffer mapped
            //
            PNDIS_BUFFER CurrentBuffer;

            //
            // Map register that was used
            //
            UINT CurMapRegister;

            //
            // The transmit is finished, so we can release
            // the physical mapping used for it.
            //
            NdisQueryPacket(
                OwningPacket,
                NULL,
                NULL,
                &CurrentBuffer,
                NULL
                );

            //
            // Get starting map register
            //
            CurMapRegister = CurrentCommandBlock->CommandBlockIndex *
                        NE3200_MAXIMUM_BLOCKS_PER_PACKET;

            //
            // For each buffer
            //
            while (CurrentBuffer) {

                //
                // Finish the mapping
                //
                NdisMCompleteBufferPhysicalMapping(
                    Adapter->MiniportAdapterHandle,
                    CurrentBuffer,
                    CurMapRegister
                    );

                ++CurMapRegister;

                NdisGetNextBuffer(
                    CurrentBuffer,
                    &CurrentBuffer
                    );

            }

        }

        //
        // If there was an error transmitting this
        // packet, update our error counters.
        //
        if (CurrentCommandBlock->Hardware.Status & NE3200_STATUS_FATALERROR_MASK) {

            if (CurrentCommandBlock->Hardware.Status &
                NE3200_STATUS_MAXIMUM_COLLISIONS) {

                Adapter->RetryFailure++;

            } else if (CurrentCommandBlock->Hardware.Status &
                NE3200_STATUS_NO_CARRIER) {

                Adapter->LostCarrier++;

            } else if (CurrentCommandBlock->Hardware.Status &
                NE3200_STATUS_HEART_BEAT) {

                Adapter->NoClearToSend++;

            } else if (CurrentCommandBlock->Hardware.Status &
                NE3200_STATUS_DMA_UNDERRUN) {

                Adapter->UnderFlow++;

            }

            StatusToReturn = NDIS_STATUS_FAILURE;

        } else {

            //
            // Update good transmit counter
            //
            StatusToReturn = NDIS_STATUS_SUCCESS;
            Adapter->GoodTransmits++;
        }

        ASSERT(sizeof(UINT) == sizeof(PNDIS_PACKET));

        //
        // Release the command block.
        //
        NE3200RelinquishCommandBlock(Adapter, CurrentCommandBlock);

        //
        // The transmit is now complete
        //
        NdisMSendComplete(
            Adapter->MiniportAdapterHandle,
            OwningPacket,
            StatusToReturn
            );

    } else if (CurrentCommandBlock->Hardware.CommandCode ==
        NE3200_COMMAND_READ_ADAPTER_STATISTICS) {

        //
        // Release the command block.
        //
        Adapter->OutOfResources =
            CurrentCommandBlock->Hardware.PARAMETERS.STATISTICS.ResourceErrors;

        Adapter->CrcErrors =
            CurrentCommandBlock->Hardware.PARAMETERS.STATISTICS.CrcErrors;

        Adapter->AlignmentErrors =
            CurrentCommandBlock->Hardware.PARAMETERS.STATISTICS.AlignmentErrors;

        Adapter->DmaOverruns =
            CurrentCommandBlock->Hardware.PARAMETERS.STATISTICS.OverrunErrors;


        //
        // If this was from a request, complete it
        //
        if (Adapter->RequestInProgress) {

            NE3200FinishQueryInformation(Adapter);

        }

        //
        // Release the command block
        //
        NE3200RelinquishCommandBlock(Adapter, CurrentCommandBlock);

    } else if (CurrentCommandBlock->Hardware.CommandCode ==
        NE3200_COMMAND_CLEAR_ADAPTER_STATISTICS) {

        //
        // Release the command block.
        //
        NE3200RelinquishCommandBlock(Adapter, CurrentCommandBlock);

    } else if (CurrentCommandBlock->Hardware.CommandCode ==
        NE3200_COMMAND_SET_STATION_ADDRESS) {

        //
        // Ignore
        //

    } else {

        //
        // The current command block is not from a transmit.
        //
        // Complete the request.
        //
        // if the CurrentCommandBlock->Set is FALSE,
        // it means this multicast operation was not caused by
        // a SetInformation request.
        //

        if (CurrentCommandBlock->Set) {

            //
            // Release the command block.
            //
            NE3200RelinquishCommandBlock(Adapter, CurrentCommandBlock);

            if (!Adapter->RequestInProgress) {

                //
                // Bogus interrupt.  Ignore it
                //

            } else {

                IF_LOG(']');

                Adapter->RequestInProgress = FALSE;

                //
                // Complete the request
                //
                NdisMSetInformationComplete(
                    Adapter->MiniportAdapterHandle,
                    NDIS_STATUS_SUCCESS);

            }

        } else {
            IF_LOG('T');
            return TRUE;
        }

    }

    return FALSE;

}