summaryrefslogtreecommitdiffstats
path: root/private/ntos/ndis/netflex/receive.c
blob: 1219cb0337eabd5a03ae7b55bb8b3497290b2032 (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
//**********************************************************************
//**********************************************************************
//
// File Name:       RECEIVE.C
//
// Program Name:    NetFlex NDIS 3.0 Miniport Driver
//
// Companion Files: None
//
// Function:        This module contains the NetFlex Miniport Driver
//                  interface routines called by the Wrapper and the
//                  configuration manager.
//
// (c) Compaq Computer Corporation, 1992,1993,1994
//
// This file is licensed by Compaq Computer Corporation to Microsoft
// Corporation pursuant to the letter of August 20, 1992 from
// Gary Stimac to Mark Baber.
//
// History:
//
//     04/15/94  Robert Van Cleve - Converted from NDIS Mac Driver
//
//**********************************************************************
//**********************************************************************


//-------------------------------------
// Include all general companion files
//-------------------------------------

#include <ndis.h>
#include "tmsstrct.h"
#include "macstrct.h"
#include "adapter.h"
#include "protos.h"

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
//  Routine Name:   NetFlexProcessRcv
//
//  Description:    This routine looks through the receive lists
//                  looking for received packets.  A receive
//                  indication is given for each packet received
//
//  Input:          acb          - Pointer to the Adapter's acb
//
//  Output:         true if we should indicaterecievecomplete
//
//  Calls:          NdisIndicateReceive
//
//  Called_By:      NetflxHandleInterrupt
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
USHORT
FASTCALL
NetFlexProcessEthRcv(
    PACB acb
    )
{
    PRCV    rcvptr;
    USHORT  FrameSize;
    USHORT  ReceiveCount = 0;
    PUCHAR  Temp;

#if (DBG || DBGPRINT)
    BOOLEAN IsBroadcast;
    PUCHAR  SourceAddress;
#endif

    //
    // While there is recieves to process...
    //
    rcvptr = acb->acb_rcv_head;

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

    do
    {
        //
        // See if the recieve is on one list...
        //
        if ((rcvptr->RCV_CSTAT & (RCSTAT_EOF | RCSTAT_SOF)) == (RCSTAT_EOF | RCSTAT_SOF))
        {
            // Frame is on one list.
            //
            FrameSize  = (USHORT)(SWAPS(rcvptr->RCV_Fsize));
            rcvptr->RCV_HeaderLen = HDR_SIZE;

            //
            // Flush the receive buffer
            //
            NdisFlushBuffer(rcvptr->RCV_FlushBuffer, FALSE);

#if (DBG || DBGPRINT)
            SourceAddress = (PVOID)((PUCHAR)&(rcvptr->RCV_Buf) + 2);
            IsBroadcast = ETH_IS_BROADCAST(SourceAddress);  // works for eth & tr
            if (IsBroadcast)
            {
                DebugPrint(3,("NF(%d): Recieved broadcast!\n",acb->anum));
            }
            else if (ETH_IS_MULTICAST(SourceAddress))
            {
                DebugPrint(3,("NF(%d): Recieved multicast!\n",acb->anum));
            }
#endif
            //
            // For speed...
            //
            Temp = (PUCHAR) rcvptr->RCV_Buf;

            //
            //  Check for Runt or Normal Packet
            //
            if (FrameSize >= HDR_SIZE)
            {
                // Normal Packet
                //
                ReceiveCount++;
                NdisMEthIndicateReceive(acb->acb_handle,
                                        (NDIS_HANDLE)(((PUCHAR) Temp) + HDR_SIZE),
                                        Temp,
                                        (UINT)HDR_SIZE,
                                        (((PUCHAR) Temp) + HDR_SIZE),
                                        (UINT)(FrameSize - HDR_SIZE),
                                        (UINT)(FrameSize - HDR_SIZE));

            }
            else if (FrameSize >= NET_ADDR_SIZE)
            {
                ReceiveCount++;
                // Runt Packet
                //
                DebugPrint(1,("NF(%d) - Got Runt! len = %d\n",acb->anum,FrameSize));
                NdisMEthIndicateReceive(acb->acb_handle,
                                        (NDIS_HANDLE)(((PUCHAR) Temp) + HDR_SIZE),
                                        Temp,
                                        (UINT)FrameSize,
                                        NULL,
                                        0,
                                        0);
            }
#if DBG
            else
            {
                DebugPrint(1,("NF(%d) - Rec - Packetlen = %d",acb->anum,FrameSize));
            }
#endif

            rcvptr->RCV_CSTAT =
                     ((rcvptr->RCV_Number % acb->RcvIntRatio) == 0) ? RCSTAT_GO_INT : RCSTAT_GO;
            //
            // Get next receive list
            //
            rcvptr = rcvptr->RCV_Next;
        }
        else
        {
            //
            // Frame is too large.  Release the frame.
            //
            acb->acb_gen_objs.frames_rcvd_err++;

            DebugPrint(0,("Netflx: Receive Not on one list.\n"));

            //
            // Clean up the list making up this packet.
            //
            while (((rcvptr->RCV_CSTAT & (RCSTAT_EOF | RCSTAT_SOF)) != (RCSTAT_EOF | RCSTAT_SOF)) &&
                   ((rcvptr->RCV_CSTAT & RCSTAT_COMPLETE) != 0)
            )
            {
                //
                // Clean the list and set the FINT based on ratio.
                //

                rcvptr->RCV_CSTAT =
                         ((rcvptr->RCV_Number % acb->RcvIntRatio) == 0) ? RCSTAT_GO_INT : RCSTAT_GO;

                rcvptr = rcvptr->RCV_Next;
            }
        }

        //
        // If we're processing too many, get out
        //
        if (ReceiveCount >= acb->acb_maxrcvs)
            break;

    } while (rcvptr->RCV_CSTAT & RCSTAT_COMPLETE);

    //
    // Update head pointer
    //
    acb->acb_rcv_head = rcvptr;

    //
    // Tell Adapter that there are more receives available
    //
    NdisRawWritePortUshort( acb->SifIntPort, (USHORT) SIFINT_RCVVALID);

    //
    // Update number of received frames
    //
    acb->acb_gen_objs.frames_rcvd_ok += ReceiveCount;

    return(ReceiveCount);
}


//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
//  Routine Name:   NetFlexProcessTrRcv
//
//  Description:    This routine looks through the receive lists
//                  looking for received packets.  A receive
//                  indication is given for each packet received.
//
//  Input:          acb          - Pointer to the Adapter's acb
//
//  Output:         true if we should indicaterecievecomplete
//
//  Calls:          NdisIndicateReceive
//
//  Called_By:      NetflxHandleInterrupt
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
USHORT
FASTCALL
NetFlexProcessTrRcv(
    PACB acb
    )
{
    PRCV    rcvptr;
    USHORT  FrameSize;
    USHORT  HeaderSize;
    USHORT  ReceiveCount = 0;
    PUCHAR  Temp;

#if (DBG || DBGPRINT)
    BOOLEAN IsBroadcast;
    PUCHAR  SourceAddress;
#endif

    //
    // While there is recieves to process...
    //
    rcvptr = acb->acb_rcv_head;

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

    do
    {
        // See if the recieve is on one list...
        //
        if ((rcvptr->RCV_CSTAT & (RCSTAT_EOF | RCSTAT_SOF)) == (RCSTAT_EOF | RCSTAT_SOF))
        {
            // Frame is on one list.
            //
            FrameSize  = (USHORT)(SWAPS(rcvptr->RCV_Fsize));

            HeaderSize = HDR_SIZE;

            //
            // Flush the receive buffer
            //
            NdisFlushBuffer(rcvptr->RCV_FlushBuffer, FALSE);

#if (DBG || DBGPRINT)
            SourceAddress = (PVOID)((PUCHAR)&(rcvptr->RCV_Buf) + 2);

            IsBroadcast = ETH_IS_BROADCAST(SourceAddress);  // works for eth & tr
            if (IsBroadcast)
            {
                DebugPrint(3,("NF(%d): Recieved broadcast!\n",acb->anum));
            }
            else
            {
                TR_IS_GROUP(SourceAddress,&IsBroadcast);
                if (IsBroadcast)
                {
                    DebugPrint(3,("NF(%d): Recieved TR Group!\n",acb->anum));
                }
            }

            TR_IS_FUNCTIONAL(SourceAddress,&IsBroadcast);
            if (IsBroadcast)
                DebugPrint(2,("NF(%d): Recieved TR Fuctional!\n",acb->anum));
#endif
            //
            // For speed...
            //
            Temp = (PUCHAR) rcvptr->RCV_Buf;

            //
            // Make sure we have at least the AC, FS, SRC & DST fields before
            // looking at the source routing info.
            //
            if (FrameSize >= HeaderSize)
            {
                // Is the source routing bit is on?
                //
                if (Temp[8] & 0x80)
                {
                    // Yes, figure out the size of the MAC Frame Header.
                    //
                    HeaderSize = (Temp[HDR_SIZE] & 0x1f) + HDR_SIZE;
                    rcvptr->RCV_HeaderLen = HeaderSize;
                    //
                    //  Check for Runt or Normal Packet again...
                    //
                    if (FrameSize >= HeaderSize)
                    {
                        // Normal Packet
                        //
                        ReceiveCount++;

                        NdisMTrIndicateReceive(
                            acb->acb_handle,
                            (NDIS_HANDLE)(((PUCHAR) Temp) + HeaderSize),
                            Temp,
                            (UINT)HeaderSize,
                            (((PUCHAR) Temp) + HeaderSize),
                            (UINT)(FrameSize - HeaderSize),
                            (UINT)(FrameSize - HeaderSize));
                    }
                    else if (FrameSize >= NET_ADDR_SIZE)
                    {
                        // Runt Packet
                        //
                        ReceiveCount++;

                        DebugPrint(1,("NF(%d) - Got Runt - len = %d!\n",acb->anum,FrameSize));

                        NdisMTrIndicateReceive(
                            acb->acb_handle,
                            (NDIS_HANDLE)(((PUCHAR) Temp) + HeaderSize),
                            Temp,
                            (UINT)FrameSize,
                            NULL,
                            0,
                            0);
                    }
                }
                else
                {
                    // No Source Routing info, but has Normal Packet Length
                    //
                    rcvptr->RCV_HeaderLen = HeaderSize;

                    ReceiveCount++;

                    NdisMTrIndicateReceive(
                        acb->acb_handle,
                        (NDIS_HANDLE)(((PUCHAR) Temp) + HeaderSize),
                        Temp,
                        (UINT)HeaderSize,
                        (((PUCHAR) Temp) + HeaderSize),
                        (UINT)(FrameSize - HeaderSize),
                        (UINT)(FrameSize - HeaderSize));
                }
            }
            else
            {
                // No, Frame doesn't have AC, FC, SRC & DST.
                // Is it bigger than net_addr_size?
                //
                if (FrameSize >= NET_ADDR_SIZE)
                {
                    // Yes, so indicate Runt Packet
                    //
                    ReceiveCount++;

                    DebugPrint(1,("NF(%d) - Got Runt - len = %d!\n",acb->anum,FrameSize));

                    NdisMTrIndicateReceive(
                        acb->acb_handle,
                        (NDIS_HANDLE)(((PUCHAR) Temp) + HeaderSize),
                        Temp,
                        (UINT)FrameSize,
                        NULL,
                        0,
                        0);
                }
            }

            rcvptr->RCV_CSTAT =
                 ((rcvptr->RCV_Number % acb->RcvIntRatio) == 0) ? RCSTAT_GO_INT : RCSTAT_GO;

            //
            // Get next receive list
            //
            rcvptr = rcvptr->RCV_Next;

        }
        else
        {
            // Frame is too large.  Release the frame.
            //
            acb->acb_gen_objs.frames_rcvd_err++;
            DebugPrint(0,("Netflx: Receive Not on one list.\n"));
            //
            // Clean up the list making up this packet.
            //
            while (((rcvptr->RCV_CSTAT & (RCSTAT_EOF | RCSTAT_SOF)) != (RCSTAT_EOF | RCSTAT_SOF)) &&
                   ((rcvptr->RCV_CSTAT & RCSTAT_COMPLETE) != 0))
            {
                // Clean the list and set the FINT based on ratio.
                //
                rcvptr->RCV_CSTAT =
                     ((rcvptr->RCV_Number % acb->RcvIntRatio) == 0) ? RCSTAT_GO_INT : RCSTAT_GO;

                rcvptr = rcvptr->RCV_Next;
            }
        }

        //
        // If we're processing too many, get out
        //
        if (ReceiveCount >= acb->acb_maxrcvs)
            break;

    } while (rcvptr->RCV_CSTAT & RCSTAT_COMPLETE);

    //
    // Update head pointer
    //
    acb->acb_rcv_head = rcvptr;

    //
    // Tell Adapter that there are more receives available
    //
    NdisRawWritePortUshort( acb->SifIntPort, (USHORT) SIFINT_RCVVALID);

    //
    // Update number of recieved frames
    //
    acb->acb_gen_objs.frames_rcvd_ok += ReceiveCount;

    return ReceiveCount;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
//  Routine Name:   NetFlexTransferData
//
//  Description:    This routine copies the received data into
//                  a packet structure provided by the caller.
//
//  Input:
//
//  MiniportAdapterContext - The context value returned by the driver when the
//  adapter was initialized.  In reality this is a pointer to NE3200_ADAPTER.
//
//  MiniportReceiveContext - The context value passed by the driver on its call
//  to NdisMIndicateReceive.  The driver can use this value to determine
//  which packet, on which adapter, is being received.
//
//  ByteOffset - An unsigned integer specifying the offset within the
//  received packet at which the copy is to begin.  If the entire packet
//  is to be copied, ByteOffset must be zero.
//
//  BytesToTransfer - An unsigned integer specifying the number of bytes
//  to copy.  It is legal to transfer zero bytes; this has no effect.  If
//  the sum of ByteOffset and BytesToTransfer is greater than the size
//  of the received packet, then the remainder of the packet (starting from
//  ByteOffset) is transferred, and the trailing portion of the receive
//  buffer is not modified.
//
//  Packet - A pointer to a descriptor for the packet storage into which
//  the MAC is to copy the received packet.
//
//  BytesTransfered - A pointer to an unsigned integer.  The MAC writes
//  the actual number of bytes transferred into this location.  This value
//  is not valid if the return Status is STATUS_PENDING.
//
//  Output:
//      Packet - Place to copy data.
//      BytesTransferred - Number of bytes copied.
//      Returns NDIS_STATUS_SUCCESS for a successful
//      completion. Otherwise, an error code is returned.
//
//  Called By:
//      Miniport Wrapper
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
NDIS_STATUS
NetFlexTransferData(
    OUT PNDIS_PACKET Packet,
    OUT PUINT BytesTransferred,
    IN NDIS_HANDLE MiniportAdapterContext,
    IN NDIS_HANDLE MiniportReceiveContext,
    IN UINT ByteOffset,
    IN UINT BytesToTransfer
    )
{
    PACB acb = (PACB) MiniportAdapterContext;
    PNDIS_BUFFER    DestinationCurrentBuffer;
    UINT            DestinationBufferCount;
    PUCHAR          SourceCurrentAddress;
    PVOID           DestinationVirtualAddress;
    UINT            DestinationCurrentLength;
    UINT            LocalBytesTransferred = 0;
    UINT            AmountToMove;
    UINT            Remaining;

    //
    // Display number of bytes to transfer on the debugger
    //
    DebugPrint(2,("NF(%d) - Copying %u bytes\n",acb->anum,BytesToTransfer));

    //
    // Initialize the number of bytes transferred to 0
    //
    *BytesTransferred = 0;

    //
    // If we don't have any more to transfer, we're done
    //
    if (BytesToTransfer == 0)
    {
        return NDIS_STATUS_SUCCESS;
    }

    //
    // Get the first buffer of the destination.
    //
    NdisQueryPacket(
        Packet,
        NULL,
        &DestinationBufferCount,
        &DestinationCurrentBuffer,
        NULL
        );

    //
    // Could have a null packet. If so, we are done.
    //
    if (DestinationBufferCount == 0)
    {
        return NDIS_STATUS_SUCCESS;
    }

    //
    // Get information on the buffer.
    //
    NdisQueryBuffer(
        DestinationCurrentBuffer,
        &DestinationVirtualAddress,
        &DestinationCurrentLength
        );

    //
    // Set up the source address.
    //
    SourceCurrentAddress = (PCHAR)(MiniportReceiveContext) + ByteOffset;

    //
    // Do the actual transfer from source to destination
    //
    while (LocalBytesTransferred < BytesToTransfer)
    {
        // Check to see whether we've exhausted the current destination
        // buffer.  If so, move onto the next one.
        //
        if (DestinationCurrentLength == 0)
        {
            NdisGetNextBuffer(
                DestinationCurrentBuffer,
                &DestinationCurrentBuffer
                );

            if (DestinationCurrentBuffer == NULL)
            {
                // We've reached the end of the packet.  We return
                // with what we've done so far. (Which must be shorter
                // than requested.)
                //
                break;
            }

            NdisQueryBuffer(
                DestinationCurrentBuffer,
                &DestinationVirtualAddress,
                &DestinationCurrentLength
                );

            continue;
        }

        //
        // Copy the data.
        //

        Remaining = BytesToTransfer - LocalBytesTransferred;

        AmountToMove = DestinationCurrentLength;

        AmountToMove = ((Remaining < AmountToMove)?
                        (Remaining):(AmountToMove));

        NdisMoveMemory(
            DestinationVirtualAddress,
            SourceCurrentAddress,
            AmountToMove
            );

        //
        // Update pointers and counters
        //
        SourceCurrentAddress += AmountToMove;
        LocalBytesTransferred += AmountToMove;
        DestinationCurrentLength -= AmountToMove;
    }

    //
    // Indicate how many bytes were transferred
    //
    *BytesTransferred = LocalBytesTransferred;

    //
    // Display total bytes transferred on debugger
    //
    DebugPrint(2,("NF(%d) - Total bytes transferred = %x\n",acb->anum,*BytesTransferred));

    return NDIS_STATUS_SUCCESS;
}