summaryrefslogtreecommitdiffstats
path: root/private/ntos/dlc/dlcindc.c
blob: 11f26e1b9e520a1cfca6e15008bf31c8848f4a26 (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
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
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
/*++

Copyright (c) 1991  Microsoft Corporation
Copyright (c) 1991  Nokia Data Systems AB

Module Name:

    dlcindc.c

Abstract:

    This module includes primitives to handle all events
    and indications from the LLC (802.2 data link) module.

    Contents:
        LlcReceiveIndication
        LlcEventIndication
        LlcCommandCompletion
        CompleteTransmitCommand
        CompleteDlcCommand

Author:

    Antti Saarenheimo 01-Sep-1991

Environment:

    Kernel mode

Revision History:

--*/

//
// This define enables the private DLC function prototypes
// We don't want to export our data types to the llc layer.
// MIPS compiler doesn't accept hiding of the internal data
// structures by a PVOID in the function prototype.
// i386 will check the type defines
//

#ifndef i386
#define DLC_PRIVATE_PROTOTYPES
#endif
#include <dlc.h>
#include <smbgtpt.h>

#if 0

//
// if DLC and LLC share the same driver then we can use macros to access fields
// in the BINDING_CONTEXT and ADAPTER_CONTEXT structures
//

#if DLC_AND_LLC
#ifndef i386
#define LLC_PRIVATE_PROTOTYPES
#endif
#include "llcdef.h"
#include "llctyp.h"
#include "llcapi.h"
#endif
#endif

//
// Table includes all llc header length of different frame types
//

static UCHAR aDlcHeaderLengths[LLC_LAST_FRAME_TYPE / 2] = {
    0,  // DLC_UNDEFINED_FRAME_TYPE = 0,
    0,  // DLC_MAC_FRAME = 0x02,
    4,  // DLC_I_FRAME = 0x04,
    3,  // DLC_UI_FRAME = 0x06,
    3,  // DLC_XID_COMMAND_POLL = 0x08,
    3,  // DLC_XID_COMMAND_NOT_POLL = 0x0a,
    3,  // DLC_XID_RESPONSE_FINAL = 0x0c,
    3,  // DLC_XID_RESPONSE_NOT_FINAL = 0x0e,
    3,  // DLC_TEST_RESPONSE_FINAL = 0x10,
    3,  // DLC_TEST_RESPONSE_NOT_FINAL = 0x12,
    0,  // DLC_DIRECT_8022 = 0x14,
    3,  // DLC_TEST_COMMAND = 0x16,
    0   // DLC_DIRECT_ETHERNET_TYPE = 0x18
};


DLC_STATUS
LlcReceiveIndication(
    IN PDLC_FILE_CONTEXT pFileContext,
    IN PDLC_OBJECT pDlcObject,
    IN USHORT FrameType,
    IN PUCHAR pLookBuf,
    IN UINT cbPacketSize
    )

/*++

Routine Description:

    The primitive handles the receive data indication from
    the lower level
    the returned parameter block of the read command.

    IBM has successfully made the receive extremly complicated.
    We can distinguish at least four different ways to gather the
    receive information to the frame header, when we have received
    an I- frame for a link station:

    1. Rcv command for link station, frames linked in link basis
        - user length and read options from link object
        - receive buffer base in link object
        - station information from link object

    2. Rcv command for link station, frames linked in sap bases
        - user length and read options from link object
        - receive buffer base in sap object
        - station information from link object

    3. Rcv command only for sap station, frames linked in link basis
        - user length and read options from sap object
        - receive buffer base in link object
        - station information from link object

    4. Rcv command only for sap station, frames linked in sap basis
        - user length and read options from sap object
        - receive buffer base in sap object
        - station information from link object

    =>  We have three different DLC objects in receive:

    1. The orginal destination of the frame, we will read station id
       from that object.
    2. The owner of the receive, the read command must match to the
       station id of the events owner.  The owner also chains the
       the received data in its frame list.
    3. Receive object: the receive object defines the recieve options
       saved to the frame header and the read flag saved to the read
       parameters.

    At least two objects are same (the different ones in different cases),
    and in most cases all objects are the same dlc object.

    We will need to save these in rcv event:
    -  The owner dlc object
    -  Pointer to linked frame header list or to a single frame
       (defined by the receive read option in the next object).
       We can directly use the owner object, because the frames
       need to be chained.  In that case we must save directly the
       reference of the buffer header.
    -  The receive object,  the dlc object having a pending receive,
       that was used to received this event.

Arguments:

    pFileContext    - the device context of this DLC client
    pDlcObject      - the DLC client, that received the event.
    FrameType       - current frame type
    pLookBuf        - points to the data from the LLC header (ie. excludes the
                      LAN header). MAY NOT CONTAIN ALL DATA
    cbPacketSize    - amount of data to copy, including DLC header, but not
                      including LLC header

Return Value:

    DLC_STATUS:

--*/

{
    PDLC_OBJECT pRcvObject = pDlcObject;
    PDLC_OBJECT pOwnerObject;
    PDLC_BUFFER_HEADER pBufferHeader;
    DLC_STATUS Status = STATUS_SUCCESS;
    NTSTATUS NtStatus;
    UINT uiLlcOffset;
    UINT FrameHeaderSize;
    UINT LlcLength;
    PDLC_EVENT pRcvEvent;
    UINT DataSize;
    PFIRST_DLC_SEGMENT pFirstBuffer;
    PDLC_COMMAND pDlcCommand;
    UINT BufferSizeLeft;

    //
    // this function is called in the context of a DPC: it is the receive data
    // indication from NDIS
    //

    ASSUME_IRQL(DISPATCH_LEVEL);

    DLC_TRACE('D');

    if (pFileContext->State != DLC_FILE_CONTEXT_OPEN) {
        return DLC_STATUS_ADAPTER_CLOSED;
    }

    ENTER_DLC(pFileContext);

#if LLC_DBG

    if (pDlcObject->State > DLC_OBJECT_CLOSED) {
       DbgPrint("Invalid object type!");
       DbgBreakPoint();
    }

#endif

    //
    // Search the first object having a pending receive, loop
    // the link, the sap and the direct station until we find one.
    //

    while (pRcvObject != NULL && pRcvObject->pRcvParms == NULL) {
        if (pRcvObject->Type == DLC_LINK_OBJECT) {
            pRcvObject = pRcvObject->u.Link.pSap;
        } else if (pRcvObject->Type == DLC_SAP_OBJECT) {
            pRcvObject = pFileContext->SapStationTable[0];
        } else if (pRcvObject->Type == DLC_DIRECT_OBJECT) {
            pRcvObject = NULL;
        }
    }

    //
    // return error status if we cannot find any receive command.
    //

    if (pRcvObject == NULL) {
        Status = DLC_STATUS_NO_RECEIVE_COMMAND;

//#if DBG
//        DbgPrint("DLC.LlcReceiveIndication.%d: Error: No receive command\n", __LINE__);
//#endif

        goto ErrorExit;
    }

    //
    // Now we must figure out the actual owner of the received frame.
    // There are actually only two special cases:
    //

    if (pRcvObject->pRcvParms->Async.Parms.Receive.uchRcvReadOption == LLC_RCV_CHAIN_FRAMES_ON_LINK
    && pRcvObject->Type == DLC_SAP_OBJECT) {
        pOwnerObject = pDlcObject;
    } else if (pRcvObject->pRcvParms->Async.Parms.Receive.uchRcvReadOption == LLC_RCV_CHAIN_FRAMES_ON_SAP
    && pRcvObject->Type == DLC_LINK_OBJECT) {
        pOwnerObject = pRcvObject->u.Link.pSap;
    } else {

        //
        // In all other cases we chain the frames to the receive object
        // (actually IBM has not defined the case), the frames are
        // chained for direct if the rcv read option is set chaining for
        // sap or link station
        //

        pOwnerObject = pRcvObject;

        //
        // direct station can recieve only the frame
        // types defined by station id of the receive command
        // There are three types, we need to check the one, that does not work:
        //
        //      DLC_DIRECT_ALL_FRAMES       0
        //      DLC_DIRECT_MAC_FRAMES       1
        //      DLC_DIRECT_NON_MAC_FRAMES   2
        //

        if (pRcvObject->Type == DLC_DIRECT_OBJECT) {
            if (FrameType == LLC_DIRECT_MAC) {
                if (pRcvObject->pRcvParms->Async.Parms.Receive.usStationId == LLC_DIRECT_8022) {
                    Status = DLC_STATUS_NO_RECEIVE_COMMAND;
                    goto ErrorExit;
                }
            } else {

                //
                // It must be a non-MAC frame
                //

                if (pRcvObject->pRcvParms->Async.Parms.Receive.usStationId == LLC_DIRECT_MAC) {
                    Status = DLC_STATUS_NO_RECEIVE_COMMAND;
                    goto ErrorExit;
                }
            }
        }
    }

    //
    // The frame length must be known when the buffers are allocated,
    // This may not be the same as the actual length of the received
    // LAN header (if we received a DIX frame)
    //

    uiLlcOffset = LlcGetReceivedLanHeaderLength(pFileContext->pBindingContext);

    //
    // Check first the buffer type (contiguous or non contiguous),
    // and then allocate it.
    // Note: WE DO NOT SUPPORT THE BREAK OPTION (because it would make
    // the current buffer management even more complicated)
    //

    LlcLength = aDlcHeaderLengths[FrameType / 2];

    //
    // DIX frames are a special case: they must be filtered
    // (DIX Llc header == ethernet type word is always 2 bytes,
    // nobody else use this llc type size).
    // A DIX application may define an offset, mask and match to
    // filter only those frames it is really needing.
    // This method works very well with XNS and TCP socket types
    //

	if ( LlcLength > cbPacketSize ) {
		Status = DLC_STATUS_INVALID_FRAME_LENGTH;
        goto ErrorExit;
	}

    if ((FrameType == LLC_DIRECT_ETHERNET_TYPE)
    && (pDlcObject->u.Direct.ProtocolTypeMask != 0)) {

        ULONG ProtocolType;

        //
        // there's a real good possibility here that if the app supplies a very
        // large value for the protocol offset, we will blow up - there's no
        // range checking performed!
        //

        ASSERT(pDlcObject->u.Direct.ProtocolTypeOffset >= 14);

        //
        // let's add that range check: if the protocol offset is before the
        // data part of the frame or past the end of this particular frame
        // then we say there's no receive defined for this frame
        //

        if ((pDlcObject->u.Direct.ProtocolTypeOffset < 14)
        || (pDlcObject->u.Direct.ProtocolTypeOffset > cbPacketSize + 10)) {
            return DLC_STATUS_NO_RECEIVE_COMMAND;
        }

        //
        // the offset to the protocol type field is given as offset from the
        // start of the frame: we only get to look in the lookahead buffer,
        // but we know that since this is an ethernet frame, the lookahead
        // buffer starts 14 bytes into the frame, so remove this length from
        // the protocol offset
        //

        ProtocolType = SmbGetUlong(&pLookBuf[pDlcObject->u.Direct.ProtocolTypeOffset - 14]);

        if ((ProtocolType & pDlcObject->u.Direct.ProtocolTypeMask) != pDlcObject->u.Direct.ProtocolTypeMatch) {
            return DLC_STATUS_NO_RECEIVE_COMMAND;
        }
    }

    //
    // The created MDL must not include the LAN header because it is not copied
    // by LlcTransferData. We use a temporary frame header size to allocate space
    // for the LAN header. The LLC header will be copied just as any other data
    //

    if (FrameType == LLC_DIRECT_MAC) {
        if (pRcvObject->pRcvParms->Async.Parms.Receive.uchOptions & DLC_CONTIGUOUS_MAC) {
            FrameHeaderSize = sizeof(DLC_CONTIGUOUS_RECEIVE) + uiLlcOffset;
            DataSize = cbPacketSize;
        } else {
            FrameHeaderSize = sizeof(DLC_NOT_CONTIGUOUS_RECEIVE);
            DataSize = cbPacketSize - LlcLength;
        }
    } else {
        if (pRcvObject->pRcvParms->Async.Parms.Receive.uchOptions & DLC_CONTIGUOUS_DATA) {
            FrameHeaderSize = sizeof(DLC_CONTIGUOUS_RECEIVE) + uiLlcOffset;
            DataSize = cbPacketSize;
        } else {
            FrameHeaderSize = sizeof(DLC_NOT_CONTIGUOUS_RECEIVE);
            DataSize = cbPacketSize - LlcLength;
        }
    }

    pBufferHeader = NULL;
    NtStatus = BufferPoolAllocate(
#if DBG
                pFileContext,
#endif
                pFileContext->hBufferPool,
                DataSize,                       // size of actual MDL buffers
                FrameHeaderSize,                // frame hdr (and possibly lan hdr)
                pRcvObject->pRcvParms->Async.Parms.Receive.usUserLength,
                cbPacketSize + uiLlcOffset,     // size of the packet
                (UINT)(-1),                     // any size is OK.
                &pBufferHeader,                 // returned buffer pointer
                &BufferSizeLeft
                );
    if (NtStatus != STATUS_SUCCESS) {
        if (FrameType != LLC_I_FRAME) {

            //
            // We must complete the receive with the given error status,
            // if this frame is not a I- frame.  (I-frames can be dropped
            // to the floor, the other frames completes the receive with
            // an error status)
            // -----------------------------------------------
            // We should not complete commands in receive lookahead.
            // The correct way could be to queue this somehow in
            // data link and process this, when the command is completed
            // in the command completion indication.
            // On the other hand, NBF DOES THIS FOR EVERY IRP!
            //

            pDlcCommand = SearchAndRemoveCommandByHandle(
                            &pFileContext->ReceiveQueue,
                            (ULONG)-1,
                            (USHORT)DLC_IGNORE_STATION_ID,
                            (USHORT)DLC_STATION_MASK_SPECIFIC,
                            pRcvObject->pRcvParms->Async.Ccb.pCcbAddress
                            );

            //
            // RLF 11/24/92
            //
            // if pDlcCommand is NULL then check the command queue - this may
            // be a receive without a RECEIVE_FLAG parameter
            //

            if (!pDlcCommand) {
                pDlcCommand = SearchAndRemoveCommandByHandle(
                                &pFileContext->CommandQueue,
                                (ULONG)-1,
                                (USHORT)DLC_IGNORE_STATION_ID,
                                (USHORT)DLC_STATION_MASK_SPECIFIC,
                                pRcvObject->pRcvParms->Async.Ccb.pCcbAddress
                                );
                ASSERT(pDlcCommand);
            }

            pRcvObject->pRcvParms = NULL;

#if LLC_DBG

            DbgPrint("cFramesReceived: %x\n", cFramesReceived);
            DbgPrint("cFramesIndicated: %x\n", cFramesIndicated);
            DbgPrint("cFramesReleased: %x\n", cFramesReleased);

            if (pDlcCommand == NULL) {
                DbgPrint("Lost receive command???");
            } else

#endif

            CompleteDlcCommand(pFileContext,
                               pRcvObject->StationId,
                               pDlcCommand,
                               DLC_STATUS_LOST_DATA_NO_BUFFERS
                               );
        }

        //
        // Free the partial buffer
        //

        BufferPoolDeallocateList(pFileContext->hBufferPool, pBufferHeader);

//#if DBG
//        DbgPrint("DLC.LlcReceiveIndication.%d: Error: Out of receive buffers\n", __LINE__);
//#endif

        Status = DLC_STATUS_OUT_OF_RCV_BUFFERS;
        goto ErrorExit;
    }

    //
    // A link station may have committed memory from the buffer pool
    // when it local busy state was enabled after a local busy state
    // because of 'out of receive buffers'.  We must uncommit all
    // packets received by that link station until the size of
    // the commited buffer space is zero
    //

    if (pDlcObject->CommittedBufferSpace != 0) {

        ULONG UncommittedBufferSpace;

        //
        // get the smaller
        //

        UncommittedBufferSpace = (pDlcObject->CommittedBufferSpace < BufGetPacketSize(cbPacketSize)
                               ? pDlcObject->CommittedBufferSpace
                               : BufGetPacketSize(cbPacketSize));

        pDlcObject->CommittedBufferSpace -= UncommittedBufferSpace;
        BufUncommitBuffers(pFileContext->hBufferPool, UncommittedBufferSpace);
    }

    //
    // By default this is linked only to itself.
    // We must create a event information every time,
    // because app might read the old chained frames
    // just between TransferData and its confirmation.
    // => we cannot chain frames before TransmitData is confirmed.
    // We should not either save any pointers to other objects,
    // because they might disappear before the confirm
    // (we use the pending transmit count to prevent OwnerObject
    // to disappear before the confirm)
    //

    pBufferHeader->FrameBuffer.pNextFrame = pBufferHeader;

    pRcvEvent = ALLOCATE_PACKET_DLC_PKT(pFileContext->hPacketPool);

    if (pRcvEvent == NULL) {
        Status = DLC_STATUS_NO_MEMORY;
        BufferPoolDeallocateList(pFileContext->hBufferPool, pBufferHeader);

#if DBG
        DbgPrint("DLC.LlcReceiveIndication.%d: Error: Out of memory\n", __LINE__);
#endif

        goto ErrorExit;
    }

    pRcvEvent->Event = LLC_RECEIVE_DATA;
    pRcvEvent->StationId = pOwnerObject->StationId;
    pRcvEvent->pOwnerObject = pOwnerObject;
    pRcvEvent->Overlay.RcvReadOption = pRcvObject->pRcvParms->Async.Parms.Receive.uchRcvReadOption;
    pRcvEvent->SecondaryInfo = pRcvObject->pRcvParms->Async.Parms.Receive.ulReceiveFlag;
    pRcvEvent->pEventInformation = pBufferHeader;
    pOwnerObject->PendingLlcRequests++;

    pFirstBuffer = (PFIRST_DLC_SEGMENT)
        ((PUCHAR)pBufferHeader->FrameBuffer.pParent->Header.pGlobalVa
        + MIN_DLC_BUFFER_SEGMENT * pBufferHeader->FrameBuffer.Index);

    pFirstBuffer->Cont.Options = pRcvObject->pRcvParms->Async.Parms.Receive.uchOptions;
    pFirstBuffer->Cont.MessageType = (UCHAR)FrameType;
    pFirstBuffer->Cont.BuffersLeft = (USHORT)(BufferPoolCount(pFileContext->hBufferPool));
    pFirstBuffer->Cont.RcvFs = 0xCC;
    pFirstBuffer->Cont.AdapterNumber = pFileContext->AdapterNumber;
    pFirstBuffer->Cont.pNextFrame = NULL;
    pFirstBuffer->Cont.StationId = pDlcObject->StationId;

    //
    // A receive command without read flag is used only once.
    // The receive completion will complete also the receive command
    //

    if (pRcvObject->pRcvParms->Async.Parms.Receive.ulReceiveFlag == 0) {
        pRcvObject->pRcvParms = NULL;
    }

    //
    // copy NOT_CONTIGUOUS or CONTIGUOUS frame header to beginning of buffer
    //

    if (FrameHeaderSize == sizeof(DLC_NOT_CONTIGUOUS_RECEIVE)) {
        pFirstBuffer->Cont.UserOffset = sizeof(DLC_NOT_CONTIGUOUS_RECEIVE);
        LlcCopyReceivedLanHeader(pFileContext->pBindingContext,
                                 pFirstBuffer->NotCont.LanHeader,
                                 NULL
                                 );
        pFirstBuffer->NotCont.LanHeaderLength = (UCHAR)uiLlcOffset;
        if (FrameType != LLC_DIRECT_ETHERNET_TYPE) {
            pFirstBuffer->NotCont.DlcHeaderLength = (UCHAR)LlcLength;
            LlcMemCpy((PCHAR)pFirstBuffer->NotCont.DlcHeader,
                      (PCHAR)pLookBuf,
                      LlcLength
                      );
        } else {

            USHORT ethernetType = LlcGetEthernetType(pFileContext->pBindingContext);
            UCHAR byte = ethernetType & 0xff;

            pFirstBuffer->NotCont.DlcHeaderLength = 2;
            ethernetType >>= 8;
            ethernetType |= ((USHORT)byte) << 8;
            *(PUSHORT)&pFirstBuffer->NotCont.DlcHeader = ethernetType;
            LlcLength = 0;
        }
    } else {

        //
        // We have included the LAN header size in the frame header size to
        // make room for this copy, but now we fix the UserOffset and everything
        // should be OK
        //

        LlcLength = 0;
        pFirstBuffer->Cont.UserOffset = sizeof(DLC_CONTIGUOUS_RECEIVE);
        LlcCopyReceivedLanHeader(pFileContext->pBindingContext,
                                 (PCHAR)pFirstBuffer
                                    + sizeof(DLC_CONTIGUOUS_RECEIVE)
                                    + pFirstBuffer->Cont.UserLength,
                                 NULL
                                 );
    }

#if LLC_DBG
    cFramesReceived++;
#endif

    //
    // Save the event only if this is the first chained frame.
    // The sequential received frames will be queued behind it.
    //

    LEAVE_DLC(pFileContext);

    RELEASE_DRIVER_LOCK();

    LlcTransferData(
        pFileContext->pBindingContext,      // data link adapter context
        &(pRcvEvent->LlcPacket),            // receive packet
        pBufferHeader->FrameBuffer.pMdl,    // destination mdl
        LlcLength,                          // offset in LookBuf to copy from
        cbPacketSize - LlcLength            // length of copied data
        );

    ACQUIRE_DRIVER_LOCK();

    //
    // Transfer data returns always a pending status,
    // the success/error status is returned asynchronously
    // in the the receive indication completion (really?)
    // We should copy the whole frame here, if is visiable
    // in the receive lookahead buffer.
    //

    return STATUS_SUCCESS;

ErrorExit:

    LEAVE_DLC(pFileContext);

    //
    // The receive status is very important for I- frames,
    // because  the llc driver set the link busy when we return
    // DLC_STATUS_NO_RECEIVE_COMMAND or DLC_STATUS_OUT_OF_RCV_BUFFERS.
    //

    if (Status == DLC_STATUS_NO_MEMORY) {
        Status = DLC_STATUS_OUT_OF_RCV_BUFFERS;
    }

    return Status;
}


VOID
LlcEventIndication(
    IN PDLC_FILE_CONTEXT pFileContext,
    IN PVOID hEventObject,
    IN UINT Event,
    IN PVOID pEventInformation,
    IN ULONG SecondaryInfo
    )

/*++

Routine Description:

    The primitive handles all LLC and NDIS events
    and translates them to DLC events, that are either immediately
    executed by a pending (and matching) read command or
    they are queued to the event queue.
    LLC cannot provide any packet with these events, beacuse they
    were not initiated by the protocol, but they just happened
    asynchronously in the data link driver.

    Special:

    This routine must not call back the data link driver, if has
    gon any other DLC status indication except INDICATE_CONNECT_REQUEST
    (that may be closed by DLC, if there are no available station ids
    on the sap).

Arguments:

    pFileContext            - DLC object handle or a file context of the event
    hEventObject            - DLC object handle or a file context of the event
    Event                   - LLC event code. Usually it can be used directly as
                              a DLC event code
    pEventInformation       - information to DLC status change block
                              (or another pointer to some misc information)
    SecondaryInformation    - dword information used by some NDIS errors

Return Value:

    None.

--*/

{
    PDLC_OBJECT pDlcObject;

    ASSUME_IRQL(DISPATCH_LEVEL);

    DLC_TRACE('E');

    if (pFileContext->State != DLC_FILE_CONTEXT_OPEN) {
        return;
    }

    ENTER_DLC(pFileContext);

    //
    // DLC status and NDIS adapter status events have different parameters,
    // => we cannot do any common preprocessing for them.
    //

    switch (Event) {
    case LLC_STATUS_CHANGE_ON_SAP:
        Event = LLC_STATUS_CHANGE;

    case LLC_STATUS_CHANGE:
        pDlcObject = (PDLC_OBJECT)hEventObject;

#if LLC_DBG

        if (pDlcObject != NULL && pDlcObject->State > DLC_OBJECT_CLOSED) {
            DbgPrint("Invalid object type!");
            DbgBreakPoint();
        }

#endif

        //
        // We must create a DLC driver object, if a
        // connect request has created a new link station
        // in the data link driver.
        //

        if (SecondaryInfo & INDICATE_CONNECT_REQUEST) {

            //
            // Create the DLC driver object, if remote connection
            // request created a new link station on LLC.
            // The connect request may be done as well for
            // a disconnected station.
            //

            if (pDlcObject->Type == DLC_SAP_OBJECT) {

                NTSTATUS Status;

                Status = InitializeLinkStation(
                            pFileContext,
                            pDlcObject,     // Sap station id
                            NULL,
                            ((PDLC_STATUS_TABLE)pEventInformation)->hLlcLinkStation,
                            &pDlcObject     // NEW Link station id
                            );
                if (Status != STATUS_SUCCESS) {

                    //
                    // This client has all its available link stations
                    // reserved or we are simply run out of memory.
                    // Several LLC clients may share the same SAP.
                    // All remote connections are handled by the
                    // first client registered on the sap
                    // until it runs out of available link stations.
                    // LlcCloseStation for a link indicating connect request
                    // will redirect the connection request to the next
                    // possible LLC client having opened the same sap or
                    // deletes the link station, if there are no clients left
                    //

                    LEAVE_DLC(pFileContext);

                    LlcCloseStation(
                        ((PDLC_STATUS_TABLE)pEventInformation)->hLlcLinkStation,
                        NULL
                        );

                    ENTER_DLC(pFileContext);

                    break;          // We have done it
                }
            }
        }

        //
        // The remotely created link station may send also other indications
        // than Connect, even if there is not yet a link station object
        // created in DLC driver. We must skip all those events.
        //

        if (pDlcObject->Type == DLC_LINK_OBJECT) {

            PDLC_EVENT pDlcEvent = pDlcObject->u.Link.pStatusEvent;

            pDlcEvent->Event = Event;
            pDlcEvent->StationId = pDlcObject->StationId;
            pDlcEvent->pOwnerObject = pDlcObject;
            pDlcEvent->pEventInformation = pEventInformation;
            pDlcEvent->SecondaryInfo |= SecondaryInfo;

            //
            // The next pointer is reset whenever the status event
            // packet is read and disconnected from the event queue.
            //

            if (pDlcEvent->LlcPacket.pNext == NULL) {
                QueueDlcEvent(pFileContext, (PDLC_PACKET)pDlcEvent);
            }
        }
        break;

    case NDIS_STATUS_RING_STATUS:

		ASSERT ( IS_NDIS_RING_STATUS(SecondaryInfo) );

        //
        // The secondary information is directly the
        // the network statys code as defined for
        // ibm token-ring and dlc api!
        //

        Event = LLC_NETWORK_STATUS;

        //
        // This event should go to all READ having defined the
        // the network status flag!
        //

        MakeDlcEvent(pFileContext,
                     Event,
                     (USHORT)(-1),
                     NULL,
                     pEventInformation,
                     NDIS_RING_STATUS_TO_DLC_RING_STATUS(SecondaryInfo),
                     FALSE
                     );
        break;

    case NDIS_STATUS_CLOSED:

        //
        // NDIS status closed is given only when the network
        // administrator is for some reason unloading NDIS.
        // Thus we must always return the 'System Action' error
        // code ('04') with LLC_CRITICAL_ERROR, but
        // we will add it later when all stations has been closed.
        //

        if (pFileContext->State != DLC_FILE_CONTEXT_CLOSED) {
            pFileContext->State = DLC_FILE_CONTEXT_CLOSED;
            CloseAllStations(
                pFileContext,
                NULL,                  // we don't have any command to complete
                LLC_CRITICAL_EXCEPTION,
                NULL,
                NULL,
                &pFileContext->ClosingPacket
                );
        }
        break;

    case LLC_TIMER_TICK_EVENT:

        //
        // This flag is used to limit the number of the failing expand
        // operations for the buffer pool.  We don't try to do it again
        // for a while, if we cannot lock memory.
        //

        MemoryLockFailed = FALSE;

        //
        // We free the extra locked pages in the buffer pool once
        // in five seconds. The unlocking takes some time, and we
        // don't want to do it whenever a read command is executed
        // (as we do with the expanding)
        //

        pFileContext->TimerTickCounter++;
        if ((pFileContext->TimerTickCounter % 10) == 0 && pFileContext->hBufferPool != NULL) {
            BufferPoolFreeExtraPages(
#if DBG
                                     pFileContext,
#endif
                                     (PDLC_BUFFER_POOL)pFileContext->hBufferPool
                                     );
        }

        //
        // Decrement the tick count of the first object in the timer queue
        // (if there is any) and complete its all sequential commands
        // having zero tickout.
        //

        if (pFileContext->pTimerQueue != NULL) {
            pFileContext->pTimerQueue->Overlay.TimerTicks--;

            while (pFileContext->pTimerQueue != NULL
            && pFileContext->pTimerQueue->Overlay.TimerTicks == 0) {

                PDLC_COMMAND pCommand;

                pCommand = pFileContext->pTimerQueue;
                pFileContext->pTimerQueue = (PDLC_COMMAND)pCommand->LlcPacket.pNext;

#if LLC_DBG

                pCommand->LlcPacket.pNext = NULL;

#endif

                CompleteDlcCommand(pFileContext, 0, pCommand, STATUS_SUCCESS);
            }
        }
        break;

#if LLC_DBG

    default:
        LlcInvalidObjectType();
        break;

#endif

    }

    LEAVE_DLC(pFileContext);
}


VOID
LlcCommandCompletion(
    IN PDLC_FILE_CONTEXT pFileContext,
    IN PDLC_OBJECT pDlcObject,
    IN PDLC_PACKET pPacket
    )

/*++

Routine Description:

    The routine completes the asynchronous DLC operations: Transmit,
    TransferData (for receive), LlcConnect and LlcDisconnect.

Arguments:

    pFileContext    - DLC process and adapter specific file context
    pDlcObject      - the object, that was assosiated with the command
    pPacket         - packet assosiated with the command

Return Value:

    None

--*/

{
    PDLC_PACKET pRootNode;
    UINT Status;

    ASSUME_IRQL(DISPATCH_LEVEL);

    DLC_TRACE('B');

    Status = (UINT)pPacket->LlcPacket.Data.Completion.Status;

    ENTER_DLC(pFileContext);

#if LLC_DBG

    if (pDlcObject != NULL && pDlcObject->State > DLC_OBJECT_CLOSED) {
        DbgPrint("Invalid object type!");
        DbgBreakPoint();
    }

#endif

    switch (pPacket->LlcPacket.Data.Completion.CompletedCommand) {
    case LLC_RECEIVE_COMPLETION:

        //
        // The receiving object may be different from the
        // actual object given by data link.
        // (this case should be moved to a subprocedure, that would
        // be called directly from the receive data handler, if
        // TransferData is executed synchronously (it always does it).
        // That would save at least 100 instructions.)
        //

        DLC_TRACE('h');

        pDlcObject = pPacket->Event.pOwnerObject;
        pDlcObject->PendingLlcRequests--;

        if (Status != STATUS_SUCCESS || pDlcObject->State != DLC_OBJECT_OPEN) {

            //
            // We must free the receive buffers, the packet
            // will be deallocated in the end of this procedure.
            //

            BufferPoolDeallocateList(pFileContext->hBufferPool,
                                     pPacket->Event.pEventInformation
                                     );

            DEALLOCATE_PACKET_DLC_PKT(pFileContext->hPacketPool, pPacket);

        } else {
            if (pPacket->Event.Overlay.RcvReadOption != LLC_RCV_READ_INDIVIDUAL_FRAMES) {

                //
                // The received frames must be chained,
                // add the new buffer header to the old
                // link list if there is any,  The buffers
                // are saved to a circular link list to make
                // possible to build easily the final link list in
                // application address space.
                //

                if (pDlcObject->pReceiveEvent != NULL) {

                    //
                    // new: pPacket->Event.pEventInformation
                    // base: pDlcObject->pReceiveEvent->pEventInformation
                    // Operations when a new element is added to base:
                    //

                    //
                    // 1. new->next = base->next
                    //

                    ((PDLC_BUFFER_HEADER)pPacket->Event.pEventInformation)->FrameBuffer.pNextFrame
                        = ((PDLC_BUFFER_HEADER)pDlcObject->pReceiveEvent->pEventInformation)->FrameBuffer.pNextFrame;

                    //
                    // 2. base->next = new
                    //

                    ((PDLC_BUFFER_HEADER)pDlcObject->pReceiveEvent->pEventInformation)->FrameBuffer.pNextFrame
                        = (PDLC_BUFFER_HEADER)pPacket->Event.pEventInformation;

                    //
                    // 3. base = new
                    //

                    pDlcObject->pReceiveEvent->pEventInformation = pPacket->Event.pEventInformation;

                    DEALLOCATE_PACKET_DLC_PKT(pFileContext->hPacketPool, pPacket);

                    //
                    // This event is already queued =>
                    // we may leave this procedure
                    //

                    break;          // **********  EXIT ************
                } else {
                    pDlcObject->pReceiveEvent = &pPacket->Event;
                }
            }

            //
            // All receives are events.  The event is handled immediately
            // if there is a pending command in the command queue,
            // otherwise the command is queued to the event queue to be
            // read by a command issued later.
            //

            pPacket->Event.Overlay.StationIdMask = (USHORT)(-1);
            QueueDlcEvent(pFileContext, pPacket);
        }
        break;

    case LLC_SEND_COMPLETION:

        //
        // We first free or/and unlock all buffers, that
        // were used in the transmit of this frame.
        //

        DLC_TRACE('i');

        BufferPoolFreeXmitBuffers(pFileContext->hBufferPool, pPacket);

        //
        // Reset the local busy states, if there is now enough
        // buffers the receive the expected stuff for a link station.
        //

        if (!IsListEmpty(&pFileContext->FlowControlQueue)
        && pFileContext->hBufferPool != NULL
        && BufGetUncommittedSpace(pFileContext->hBufferPool) >= 0) {
            ResetLocalBusyBufferStates(pFileContext);
        }

        //
        // This code completes a transmit command.
        // It releases all resources allocated for the transmit
        // and completes the command, if this was the last
        // transmit associated with it.
        // Note:
        // Single transmit command may consists of several frames.
        // We must wait until all NDIS send requests have been completed
        // before we can complete the command. That's why the first transmit
        // node is also a root node.  All transmit nodes have a reference
        // to the root node.
        // (why we incrment/decrement the object reference count separately
        // for each frame,  we could do it only once for a transmit command).
        //

        pDlcObject->PendingLlcRequests--;
        pRootNode = pPacket->Node.pTransmitNode;

        //
        // Don't delete root node packet, we will need it to queue the
        // command completion (if the command completion flag is used)
        //

        if (pPacket != pRootNode) {

            DEALLOCATE_PACKET_DLC_PKT(pFileContext->hPacketPool, pPacket);

        }

        //
        // We will save and keep the first asynchronous error status
        //

        if (Status != STATUS_SUCCESS && pRootNode->Node.pIrp->IoStatus.Status == STATUS_SUCCESS) {
            pRootNode->Node.pIrp->IoStatus.Status = Status;
        }

        pRootNode->Node.FrameCount--;
        if (pRootNode->Node.FrameCount == 0) {
            CompleteTransmitCommand(pFileContext,
                                    pRootNode->Node.pIrp,
                                    pDlcObject,
                                    pRootNode
                                    );
        }
        break;

    case LLC_RESET_COMPLETION:
        pPacket->ResetPacket.pClosingInfo->CloseCounter--;
        if (pPacket->ResetPacket.pClosingInfo->CloseCounter == 0) {
            CompleteCloseReset(pFileContext, pPacket->ResetPacket.pClosingInfo);
        }

        DEALLOCATE_PACKET_DLC_PKT(pFileContext->hPacketPool, pPacket);

        break;

    case LLC_CONNECT_COMPLETION:

        DLC_TRACE('e');

        CompleteDlcCommand(pFileContext,
                           pDlcObject->StationId,
                           &pPacket->DlcCommand,
                           Status
                           );
        pDlcObject->PendingLlcRequests--;
        break;

    case LLC_DISCONNECT_COMPLETION:

        //
        // The disconnect is in dlc driver always connected to
        // the closing of the link station.  We just continue
        // the asynchronous command.  Still this process (waiting
        // the other side to ack to disconnect packet DISC)
        // may be interrupted by an immediate close command
        // (DLC.RESET or DIR.CLOSE.ADAPTER).
        //

        DLC_TRACE('g');

        if (pDlcObject->LlcObjectExists == TRUE) {
            pDlcObject->LlcObjectExists = FALSE;

            LEAVE_DLC(pFileContext);

            LlcCloseStation(pDlcObject->hLlcObject, (PLLC_PACKET)pPacket);

            ENTER_DLC(pFileContext);

            DereferenceLlcObject(pDlcObject);

            //
            // We don't want to complete a dlc object twice.
            //

            LEAVE_DLC(pFileContext);

            return;
        }

    case LLC_CLOSE_COMPLETION:

        //
        // Just free the command packet and update the reference counter.
        // The end of this procedure takes care of the rest.
        //

        DLC_TRACE('f');

        pDlcObject->PendingLlcRequests--;

        DEALLOCATE_PACKET_DLC_PKT(pFileContext->hPacketPool, pPacket);

        break;

#if LLC_DBG

    default:
        LlcInvalidObjectType();
        break;

#endif

    };

#if LLC_DBG

    if (pDlcObject != NULL && pDlcObject->PendingLlcRequests < 0) {
        DbgPrint("Error: PendingLlcRequests < 0!!!\n");
        DbgBreakPoint();
    }

#endif

    //
    // we can try to complete the close/reset only when there are no
    // pending commands issued to LLC (and NDIS).
    // The procedure will check, if there is still pending commands.
    //

    if (pDlcObject != NULL && pDlcObject->State != DLC_OBJECT_OPEN) {
        CompleteCloseStation(pFileContext, pDlcObject);
    }

    LEAVE_DLC(pFileContext);
}


VOID
CompleteTransmitCommand(
    IN PDLC_FILE_CONTEXT pFileContext,
    IN PIRP pIrp,
    IN PDLC_OBJECT pChainObject,
    IN PDLC_PACKET pPacket
    )

/*++

Routine Description:

    The routine completes the DLC transmit command and optionally
    chains its CCB(s) to the completion list.
    The transmit read option defines, if the transmit commands
    are chained or if each command is completed with a separate
    READ- command.

Arguments:

    pFileContext    - DLC process and adapter specific file context
    pIrp            - Io- request packet of the completed command
    pChainObject    - the DLC object the packet(s) was transmitted from
    pPacket         - the orginal packet of the transmit command

Return Value:

    None

--*/

{
    PVOID pUserCcbPointer = NULL;
    PNT_DLC_PARMS pDlcParms = (PNT_DLC_PARMS)pIrp->AssociatedIrp.SystemBuffer;

    //
    // MODMOD RLF 01/19/93
    //

    BOOLEAN queuePacket = FALSE;
    PVOID pCcb;
    ULONG eventFlags;

    ASSUME_IRQL(DISPATCH_LEVEL);

    //
    // MODMOD ends
    //

    pDlcParms->Async.Parms.Transmit.FrameStatus = 0xCC;

    //
    // Check if the transmit commands should be linked to the completion list
    //

    if (pDlcParms->Async.Ccb.CommandCompletionFlag != 0) {

        //
        // Are they linked together in the same completion event?
        //

        if (pDlcParms->Async.Parms.Transmit.XmitReadOption != LLC_COMPLETE_SINGLE_XMIT_FRAME) {
            if (pDlcParms->Async.Parms.Transmit.XmitReadOption == LLC_CHAIN_XMIT_COMMANDS_ON_SAP
            && pChainObject->Type == DLC_LINK_OBJECT) {
                pChainObject = pChainObject->u.Link.pSap;
            }
            pChainObject->ChainedTransmitCount++;
            pUserCcbPointer = pChainObject->pPrevXmitCcbAddress;
            pChainObject->pPrevXmitCcbAddress = pDlcParms->Async.Ccb.pCcbAddress;

            //
            // Make new event only for the first transmit completion
            //

            if (pChainObject->ChainedTransmitCount == 1) {
                pChainObject->pFirstChainedCcbAddress = pDlcParms->Async.Ccb.pCcbAddress;
                pPacket->Event.pOwnerObject = pChainObject;
            } else {

                //
                // There is already a pending event for the
                // this transmit command, we may free this one.
                // The space & speed optimal code execution requires
                // a shameful jump.
                //

                DEALLOCATE_PACKET_DLC_PKT(pFileContext->hPacketPool, pPacket);

                //
                // MODMOD RLF 01/21/93
                //

                pCcb = pDlcParms->Async.Ccb.pCcbAddress;

                //
                // MODMOD ends
                //

                //
                // ***** G-O-T-O ********
                //

                goto ThisIsA_SHAME;
            }
        } else {
            pPacket->Event.pOwnerObject = NULL;
        }

        //
        // MODMOD RLF 01/19/93
        //

        ////
        //// We translate the orginal transit packet to a new event packet
        ////
        //
        //pPacket->Event.Event = LLC_TRANSMIT_COMPLETION;
        //pPacket->Event.StationId = (USHORT)pChainObject->StationId;
        //pPacket->Event.pEventInformation = pDlcParms->Async.Ccb.pCcbAddress;
        //pPacket->Event.SecondaryInfo = pDlcParms->Async.Ccb.CommandCompletionFlag;
        //QueueDlcEvent(pFileContext, pPacket);

        queuePacket = TRUE;
        pCcb = pDlcParms->Async.Ccb.pCcbAddress;
        eventFlags = pDlcParms->Async.Ccb.CommandCompletionFlag;

        //
        // MODMOD ends
        //

    } else {

        DEALLOCATE_PACKET_DLC_PKT(pFileContext->hPacketPool, pPacket);

    }

ThisIsA_SHAME:

    //
    // Set the default value to the returned frame status
    //

    if (pIrp->IoStatus.Status != STATUS_SUCCESS) {

        //
        // Set the FS (frame status) error code, NDIS has
        // returned an FS-related error code.
        // Note: This error status is never returned in the
        // case of I- frames (or should it be?).
        //

        if (pIrp->IoStatus.Status == NDIS_STATUS_NOT_RECOGNIZED) {
            pDlcParms->Async.Parms.Transmit.FrameStatus = 0;
            pDlcParms->Async.Ccb.uchDlcStatus = LLC_STATUS_TRANSMIT_ERROR_FS;
        } else if (pIrp->IoStatus.Status == NDIS_STATUS_NOT_COPIED) {
            pDlcParms->Async.Parms.Transmit.FrameStatus = 0x44;
            pDlcParms->Async.Ccb.uchDlcStatus = LLC_STATUS_TRANSMIT_ERROR_FS;
        } else if (pIrp->IoStatus.Status == NDIS_STATUS_INVALID_PACKET) {
            pDlcParms->Async.Parms.Transmit.FrameStatus = 0;
            pDlcParms->Async.Ccb.uchDlcStatus = LLC_STATUS_INVALID_FRAME_LENGTH;
        } else {

            //
            // Don't overwrite the existing DLC error codes!
            //

            if (pIrp->IoStatus.Status < DLC_STATUS_ERROR_BASE
            || pIrp->IoStatus.Status > DLC_STATUS_MAX_ERROR) {
                pDlcParms->Async.Ccb.uchDlcStatus = (UCHAR)LLC_STATUS_TRANSMIT_ERROR;
            } else {
                pDlcParms->Async.Ccb.uchDlcStatus = (UCHAR)(pIrp->IoStatus.Status - DLC_STATUS_ERROR_BASE);
            }
        }
        pIrp->IoStatus.Status = STATUS_SUCCESS;
    } else {
        pDlcParms->Async.Ccb.uchDlcStatus = (UCHAR)STATUS_SUCCESS;
    }

    pDlcParms->Async.Ccb.pCcbAddress = pUserCcbPointer;

    //
    // Copy the optional second output buffer to user memory.
    //

    if (IoGetCurrentIrpStackLocation(pIrp)->Parameters.DeviceIoControl.IoControlCode == IOCTL_DLC_TRANSMIT) {

        //
        // MODMOD RLF 01/21/93
        //
        // Transmit now uses METHOD_OUT_DIRECT which means we update the CCB
        // with the pNext and uchDlcStatus fields
        //

        PLLC_CCB pInputCcb;
        PUCHAR pFrameStatus;

        pInputCcb = (PLLC_CCB)MmGetSystemAddressForMdl(pIrp->MdlAddress);

        //
        // the pointer may be an unaligned VDM pointer!
        //

        SmbPutUlong((PULONG)(&pInputCcb->pNext), (ULONG)pUserCcbPointer);
        pInputCcb->uchDlcStatus = pDlcParms->Async.Ccb.uchDlcStatus;

        //
        // MODMOD ends
        //

        //
        // performance (slight) improvement. The following copies A SINGLE BYTE
        // (the frame status field). Replace call to copy routine with single
        // byte move
        //

        //LlcMemCpy(MmGetSystemAddressForMdl((PMDL)pDlcParms->Async.Ccb.u.pMdl),
        //          &pDlcParms->Async.Parms.Transmit.FrameStatus,
        //          aSpecialOutputBuffers[IOCTL_DLC_TRANSMIT_INDEX]
        //          );

        pFrameStatus = (PUCHAR)MmGetSystemAddressForMdl((PMDL)pDlcParms->Async.Ccb.u.pMdl);
        *pFrameStatus = pDlcParms->Async.Parms.Transmit.FrameStatus;

        UnlockAndFreeMdl(pDlcParms->Async.Ccb.u.pMdl);
    }

    //
    // we are about to complete this IRP - remove the cancel routine
    //

//    RELEASE_DRIVER_LOCK();

    SetIrpCancelRoutine(pIrp, FALSE);
    IoCompleteRequest(pIrp, (CCHAR)IO_NETWORK_INCREMENT);

//    ACQUIRE_DRIVER_LOCK();

    //
    // MODMOD RLF 01/19/93
    //
    // Moved queueing of the event until after the IoCompleteRequest because it
    // was possible for the following to occur:
    //
    //  Thread A:
    //
    //  1.  app allocates transmit CCB & submits it. Transmit completion is to
    //      be picked up on a READ CCB
    //  2.  transmit completes
    //  3.  DLC queues event for transmit completion
    //
    //  Thread B:
    //
    //  4.  app submits READ CCB
    //  5.  READ finds completed transmit event on DLC event queue & removes it
    //  6.  READ IRP is completed (IoCompleteRequest)
    //  7.  app checks READ CCB and deallocates transmit CCB
    //  8.  app reallocates memory previously used for transmit CCB
    //
    //  Thread A:
    //
    //  9.  transmit IRP is completed (IoCompleteRequest)
    //
    // At this point, the IoCompleteRequest for the transmit copies some
    // completion info over the area which used to be the original transmit CCB
    // but has since been reallocated, causing lachrymae maximus
    //
    // This is safe because in this case we know we have a transmit which is
    // destined to be picked up by a READ (its ulCompletionFlag parameter is
    // non-zero), so it doesn't do any harm if we complete the IRP before
    // queueing the event for a READ
    //

    if (queuePacket) {
        pPacket->Event.Event = LLC_TRANSMIT_COMPLETION;
        pPacket->Event.StationId = (USHORT)pChainObject->StationId;
        pPacket->Event.pEventInformation = pCcb;
        pPacket->Event.SecondaryInfo = eventFlags;
        QueueDlcEvent(pFileContext, pPacket);
    }

    //
    // MODMOD ends
    //

    DereferenceFileContext(pFileContext);
}


VOID
CompleteDlcCommand(
    IN PDLC_FILE_CONTEXT pFileContext,
    IN USHORT StationId,
    IN PDLC_COMMAND pDlcCommand,
    IN UINT Status
    )

/*++

Routine Description:

    The routine completes the DLC command and optionally
    saves its CCB(s) to the completion list, if the command
    has a command completion flag.

Arguments:

    pFileContext    - DLC process and adapter specific file context
    StationId       - the station id of the completed command (0 for non station
                      based commands)
    pDlcCommand     - the caller must provide either command completion packet
                      or IRP
    Status          - command completion status

Return Value:

    None

--*/

{
    PVOID pCcbAddress;
    ULONG CommandCompletionFlag;
    PIRP pIrp;

    pIrp = pDlcCommand->pIrp;

    DEALLOCATE_PACKET_DLC_PKT(pFileContext->hPacketPool, pDlcCommand);

    pCcbAddress = ((PNT_DLC_PARMS)pIrp->AssociatedIrp.SystemBuffer)->Async.Ccb.pCcbAddress;
    CommandCompletionFlag = ((PNT_DLC_PARMS)pIrp->AssociatedIrp.SystemBuffer)->Async.Ccb.CommandCompletionFlag;
    CompleteAsyncCommand(pFileContext, Status, pIrp, NULL, FALSE);

    //
    // Queue command completion event, if the command completion flag was set
    //

    if (CommandCompletionFlag != 0) {
        MakeDlcEvent(pFileContext,
                     DLC_COMMAND_COMPLETION,
                     StationId,
                     NULL,
                     pCcbAddress,
                     CommandCompletionFlag,
                     FALSE
                     );
    }
}