summaryrefslogtreecommitdiffstats
path: root/private/ntos/ndis/ubnei/receive.c
blob: 6cfb38a299f5f70a0f5f44552ef813745aee3d72 (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
/*++

Copyright (c) 1992  Microsoft Corporation

Module Name:

    receive.c

Abstract:

    This file handles received packets the Ungermann Bass Ethernet Controller.
    This driver conforms to the NDIS 3.0 interface.

    Based on the elnkii drivers for the most part

Author:
    Brian Lieuallen     (BrianLie)      07/02/92

Environment:

    Kernel Mode     Operating Systems        : NT and other lesser OS's

Revision History:

    Brian Lieuallen     BrianLie        12/15/93
        Made it a mini-port


--*/



#include <ndis.h>
#include <efilter.h>

#include "niudata.h"
#include "debug.h"
#include "ubhard.h"
#include "ubsoft.h"
#include "ubnei.h"

#include "map.h"





BOOLEAN
CheckForReceives(
    IN PUBNEI_ADAPTER  pAdapter
    )
/*++

Routine Description:

    This routine is called from the interrupt DPC to check the card for
    received packets. If it finds one then it calls the ethfilter indicate
    it to the correct protocols


Arguments:


Notes:



--*/


{

    PHIGHNIUDATA   pDataWindow  = (PHIGHNIUDATA) pAdapter->pDataWindow;
    PRBD           pRBD;
    UINT           PacketLength,LookAheadSize;
    PUCHAR         pBuffer;
    USHORT         rbdtemp;
    USHORT TmpUshort;
    UCHAR TmpUchar1, TmpUchar2;
    PUCHAR LookaheadBuffer;


    SET_DATAWINDOW(pAdapter,INTERRUPT_ENABLED);

    //    NumberOfPackets=pDataWindow->RcvFrames.SRB_WritePtr[0]-
    //                    pDataWindow->RcvFrames.SRB_ReadPtr[0];

    //
    //  Check to see if the are any recieved frames in the ring buffer
    //

    UBNEI_MOVE_SHARED_RAM_TO_UCHAR(&TmpUchar1, &(pDataWindow->RcvFrames.SRB_ReadPtr[0]));
    UBNEI_MOVE_SHARED_RAM_TO_UCHAR(&TmpUchar2, &(pDataWindow->RcvFrames.SRB_WritePtr[0]));

    if (TmpUchar1 == TmpUchar2) {
        SET_RECDWINDOW(pAdapter,INTERRUPT_ENABLED);
        return FALSE;
    }

    //
    //   There is at least one packet to indicate
    //

    //   SRB- stands for Simple Ring Buffer
    //        The main components of interest are the Read and Write pointers
    //        and the offset array.
    //
    //        The value in the R/W ptr is actually an index into the Offset
    //        array. The values in the offset array are offsets to RBD's
    //
    //   RDB- Receive Buffer Descriptor
    //   The  structure holds the address of the actual receive data, its
    //   its length and perhaps a link to the next RDB that makes up
    //   a given packet
    //
    //   The RDB's and the actual receive buffers are all in the first
    //   half of the 64k data segment. If the window size is only 16k then
    //   only 16k will be used, otherwise the whole 32k will be used
    //


    //
    //  We do receive indications until there are no more or reset starts
    //

    while ((TmpUchar1 != TmpUchar2)) {


        //
        //  Get the offset of the first RBD, used in freeing the RBD chain
        //  when the receive is complete
        //

        UBNEI_MOVE_SHARED_RAM_TO_USHORT(&rbdtemp,
                                        &(pDataWindow->RcvFrames.SRB_Offsets[TmpUchar1])
                                       );

        //
        //  Get a pointer to the RBD the data window
        //

        pRBD=(PRBD)((PUCHAR)pAdapter->pCardRam+rbdtemp);

        SET_RECDWINDOW(pAdapter,INTERRUPT_ENABLED);

        //
        //  Get the address of the first buffer pointed to by this RBD
        //  All of the receive buffers are in the receive segment
        //

        UBNEI_MOVE_SHARED_RAM_TO_USHORT(&TmpUshort, &(pRBD->RBD_Buffer));

        pBuffer=(PUCHAR)pAdapter->pCardRam+TmpUshort;

        UBNEI_MOVE_SHARED_RAM_TO_USHORT(&TmpUshort, &(pRBD->RBD_Frame_Length));

        PacketLength=TmpUshort;

        LookAheadSize=PacketLength < pAdapter->ReceiveBufSize ?
                          PacketLength : pAdapter->ReceiveBufSize;

        IF_LOG('y');

        if (PacketLength >= 14) {

            NdisCreateLookaheadBufferFromSharedMemory(
                pBuffer,
                LookAheadSize,
                &LookaheadBuffer
                );

            if (LookaheadBuffer != NULL) {

                //
                //  We save this information in the Adapter block for use of the
                //  TransferData
                //
                pAdapter->pIndicatedRBD=pRBD;
                pAdapter->PacketLen=PacketLength;

                //
                //  Save this for transfer data, so probably won't have to get this out
                //  of the shared ram again
                //
                pAdapter->FirstCardBuffer=pBuffer;


                NdisMEthIndicateReceive(
                    pAdapter->NdisAdapterHandle,
                    NULL,
                    LookaheadBuffer,
                    14,
                    (PUCHAR)LookaheadBuffer + 14,
                    LookAheadSize -14,
                    PacketLength-14
                    );



                NdisDestroyLookaheadBufferFromSharedMemory(LookaheadBuffer);

            }

        }

        IF_LOG('Y');

        SET_DATAWINDOW(pAdapter,INTERRUPT_ENABLED);

        //
        //  Free the RBDs by placing the the first RBD in the returned RBDs
        //  ring buffer
        //

        TmpUchar1++;
        UBNEI_MOVE_UCHAR_TO_SHARED_RAM(&(pDataWindow->RcvFrames.SRB_ReadPtr[0]), TmpUchar1);

        UBNEI_MOVE_SHARED_RAM_TO_UCHAR(&TmpUchar2,
                                       &(pDataWindow->ReturnedRBDs.SRB_WritePtr[0])
                                      );
        UBNEI_MOVE_USHORT_TO_SHARED_RAM(&(pDataWindow->ReturnedRBDs.SRB_Offsets[TmpUchar2]),
                                        rbdtemp
                                       );
        UBNEI_MOVE_UCHAR_TO_SHARED_RAM(&(pDataWindow->ReturnedRBDs.SRB_WritePtr[0]),
                                       TmpUchar2 + 1
                                      );
        //
        //  read the ring pointers again for the main while loop
        //
//        UBNEI_MOVE_SHARED_RAM_TO_UCHAR(&TmpUchar1, &(pDataWindow->RcvFrames.SRB_ReadPtr[0]));
        UBNEI_MOVE_SHARED_RAM_TO_UCHAR(&TmpUchar2, &(pDataWindow->RcvFrames.SRB_WritePtr[0]));

    }

    SET_RECDWINDOW(pAdapter,INTERRUPT_ENABLED);

    IF_LOG('z');


    //
    //   If we get here we know that at least one packet was indicated
    //

    NdisMEthIndicateReceiveComplete(pAdapter->NdisAdapterHandle);


    IF_LOG('Z');

    return TRUE;
}






NDIS_STATUS
UbneiTransferData(
    OUT PNDIS_PACKET  Packet,
    OUT PUINT         BytesTransferred,
    IN  NDIS_HANDLE   MacBindingHandle,
    IN  NDIS_HANDLE   MacReceiveContext,
    IN  UINT          ByteOffset,
    IN  UINT          BytesToTransfer
    )


/*++

Routine Description:

    NDIS function.

Arguments:

    see NDIS 3.0 spec.

Notes:

    This routine uses two elements in the Adapter structure to pass
    information about the current indication. This will only work because
    there can be only one indication going on at a time.


--*/

{
    UINT BytesLeft, BytesNow, BytesWanted;
    PNDIS_BUFFER CurBuffer;
    PUCHAR  BufStart;
    UINT BufLen;
    UINT BufferIndex, i;
    PUCHAR         pBuffer;
    PUBNEI_ADAPTER pAdapter	  = MacBindingHandle;
    PRBD           pRBD           = pAdapter->pIndicatedRBD;
    UINT           CardBufferSize = pAdapter->ReceiveBufSize;
    USHORT TmpUshort;

    UINT           BytesLeftInCardBuffer;



    ByteOffset += 14;



    //
    // See how much data there is to transfer.
    //


    if (ByteOffset+BytesToTransfer > pAdapter->PacketLen) {

        IF_LOUD(DbgPrint("TD() Protocol asked for too much data\n");)
        BytesWanted = pAdapter->PacketLen - ByteOffset;

    } else {

        BytesWanted = BytesToTransfer;

    }

    BytesLeft = BytesWanted;


    //
    // Determine where the copying should start.
    //


    IF_LOG('t');

    ASSERT_RECEIVE_WINDOW( pAdapter);

//    SET_RECDWINDOW(pAdapter,INTERRUPT_ENABLED);


    ByteOffset=ByteOffset;

    BytesLeftInCardBuffer=CardBufferSize-ByteOffset;

    pBuffer=pAdapter->FirstCardBuffer;

    pBuffer+=ByteOffset;

    if (ByteOffset>CardBufferSize) {
        //
        //  The transfer start point is not in the first card buffer.
        //  Move the correct one.
        //
        //  The is definitly the exception case
        //
        BufferIndex=ByteOffset/CardBufferSize;

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

            UBNEI_MOVE_SHARED_RAM_TO_USHORT(&TmpUshort, &(pRBD->RBD_next_RBD));

            pRBD=(PRBD)((PUCHAR)pAdapter->pCardRam+ TmpUshort);

            ByteOffset-=CardBufferSize;

        }

        UBNEI_MOVE_SHARED_RAM_TO_USHORT(&TmpUshort, &(pRBD->RBD_Buffer));

        BytesLeftInCardBuffer=CardBufferSize-ByteOffset;

        pBuffer=(PUCHAR)pAdapter->pCardRam+TmpUshort;

        pBuffer+=ByteOffset;

    }


    NdisQueryPacket(Packet, NULL, NULL, &CurBuffer, NULL);

    NdisQueryBuffer(CurBuffer, (PVOID *)&BufStart, &BufLen);

    //
    // Loop, filling each buffer in the packet until there
    // are no more buffers or the data has all been copied.
    //

    while (BytesLeft > 0) {

        BytesNow = BytesLeft;

        //
        // See how much room is in the current ndis buffer
        //
        if (BufLen < BytesLeft) {

            BytesNow = BufLen;

        }

        //
        //  See how much room is left in the current card buffer
        //
        if (BytesNow > BytesLeftInCardBuffer) {

            BytesNow = BytesLeftInCardBuffer;

        }

        //
        // Copy up the data.
        //
        UBNEI_MOVE_SHARED_RAM_TO_MEM(BufStart, pBuffer, BytesNow);

        BufStart += BytesNow;

        BufLen   -= BytesNow;


        pBuffer  += BytesNow;

        BytesLeftInCardBuffer -= BytesNow;


        BytesLeft -= BytesNow;


        //
        // Is the transfer done now?
        //

        if (BytesLeft == 0) {

            break;

        }


        //
        // Did we use up the card buffer?
        //
        if (BytesLeftInCardBuffer == 0) {

            UBNEI_MOVE_SHARED_RAM_TO_USHORT(&TmpUshort, &(pRBD->RBD_next_RBD));

            pRBD=(PRBD)((PUCHAR)pAdapter->pCardRam+TmpUshort);

            UBNEI_MOVE_SHARED_RAM_TO_USHORT(&TmpUshort, &(pRBD->RBD_Buffer));


            pBuffer=(PUCHAR)pAdapter->pCardRam+TmpUshort;

            BytesLeftInCardBuffer=CardBufferSize;

        }


        //
        // Was the end of this packet buffer reached?
        //
        if (BufLen==0) {

            NdisGetNextBuffer(CurBuffer, &CurBuffer);

            if (CurBuffer == (PNDIS_BUFFER)NULL) {

                break;

            }

            NdisQueryBuffer(CurBuffer, (PVOID *)&BufStart, &BufLen);

        }

    }


    *BytesTransferred = BytesWanted - BytesLeft;

    IF_LOG('T');


    return NDIS_STATUS_SUCCESS;

}