summaryrefslogtreecommitdiffstats
path: root/private/ntos/dlc/dlcque.c
blob: 6359c0b6b8f88e3e9dde37b130eb209d3077dbed (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
/*++

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

Module Name:

    dlcque.c

Abstract:

    This module provides primitives to manage the dlc command and
    event queues.

    Contents:
        QueueDlcEvent
        MakeDlcEvent
        IsCommandOnList
        SearchAndRemoveCommand
        SearchAndRemoveAnyCommand
        SearchAndRemoveCommandByHandle
        SearchAndRemoveSpecificCommand
        QueueDlcCommand
        AbortCommand
        CancelDlcCommand
        PurgeDlcEventQueue
        PurgeDlcFlowControlQueue

Author:

    Antti Saarenheimo 29-Aug-1991

Environment:

    Kernel mode

Revision History:

--*/

#include <dlc.h>
#include "dlcdebug.h"

/*++

Design notes about the DLC event and command queue management
-------------------------------------------------------------

In DLC API the READ command may be given before or after the actual
event has happened.  This means, that all DLC events of the READ command
must be queued and also the READ command must be queued to wait for
the set of events it was designated.

For each new DLC (READ) command the driver searches first the event queue
and then queues the command, if the desired event was not found.
The same thing is made also for the events:  the dlc command queue
is checked first and then the event is queued (if it was a read event)
or it is dropped away (if the event was not meant for READ and there
was no command waiting for it).

The events are implemented by the event masks.  The event is executed
if the result of bit-OR operation for the event masks in the command
and in the event is not zero.

All commands and receive events of a dlc station (direct, sap or link)
are returned as a DLC completion event when the station is closed.
The same operation is done also by the total reset or all sap stations
(and the only direct station).
A READ command may be used to read that command completion from
the event list.  The READ command may have been give before, in the
same time linked to the next CCB field of the close/reset command or
after the close/reset command has completed.
DirOpenAdapter command deletes all events (and commands) from the
event queue.  The received data and CCBs are not returned back, if
there is not given any READ command for that purpose.

(This has been fixed:
 There could be a minor incompatibility with IBM OS/2 DLC API,
 the NT DLC driver may not always complete a dlc command with
 the READ command linked to commmand's CCB,  if there is another
 matching DLC command pending)

    Here is the solution:

    Actually we could make a special
    READ command, that is chained to the very end of the command queue
    and that can be matched only with a CCB pointer of the completed
    DLC command.  We could modify the command aborting to support also
    this case, and there should be a special CCB input field in the
    NT READ for the completed command (do we also need to return
    the READ flag or command completion flag?).

----

We need at least these procedures:

MakeDlcEvent(
    pFileContext, Event, StationId, pOwnerObject, pEventInformation, SecInfo);

    - scans the command queues
    - saves the event if it can't find a matching command and
      if the command's event masks defines, that the event should be saved

QueueDlcCommand(
    pFileContext, Event, StationId, StationIdMask, pIrp, AbortHandle, pfComp );
    - scans the event queue for a matching event
    - saves the event if can't find a match

AbortCommand(
    pFileContext, Event, StationId, StationIdMask, AbortHandle, ppCcbLink );
    - aborts a command in the command queue

****** Subprocedures (used by the api functions)

PDLC_COMMAND
SearchPrevCommand(
    pQueue, EventMask, StationId, StationIdMask, SearchHandle, pPrevCommand
    - returns pointer to the previous element before the matching
      dlc command in a queue,

(Macro: SearchPrevEvent
            - searches and removes the given event from the event queue and
            returns its pointer)
--*/


VOID
QueueDlcEvent(
    IN PDLC_FILE_CONTEXT pFileContext,
    IN PDLC_PACKET pPacket
    )

/*++

Routine Description:

    The routine tries first to find a matching event in the command
    queues and queues the DLC event if it can't find anything and if
    the event belongs to the mask of the queued commands.
    There are two event queues, both having a mask for the checked
    events.  The queue is checked only if the event bits are found
    in the mask of the queue.

Arguments:

    pFileContext    - process specific adapter context
    pPacket         - Event packet

Return Value:

    None

--*/

{
    PDLC_COMMAND pDlcCommand;

    DIAG_FUNCTION("QueueDlcEvent");

    //
    // get search mask
    //

    pPacket->Event.Overlay.StationIdMask = (USHORT)((pPacket->Event.StationId == -1) ? 0 : -1);

    //
    // DLC commands can be completed with a special READ command,
    // that is linked to the CCB pointer of the command.
    // NT DLC must queue that special read command before the
    // command that it was linked.  We must check here
    // if there is a special READ command just for this command
    // completion.
    // **************  HACK-HACK-HACK   **************
    //     Close/Reset command completions use different
    //     EventInformation from the other command completions
    //     and they search the read command by themself =>
    //     we don't need to care about it.
    //     If SeconadryInfo == 0
    //     then this is a Close/Reset command completion
    //     and we don't search the special read command.
    //
    // **************  HACK-HACK-HACK   **************
    //

    if (!IsListEmpty(&pFileContext->CommandQueue)) {

        pDlcCommand = NULL;

        if (pPacket->Event.Event == DLC_COMMAND_COMPLETION
        && pPacket->Event.SecondaryInfo != 0) {

            pDlcCommand = SearchAndRemoveCommandByHandle(
                                &pFileContext->CommandQueue,
                                (ULONG)-1,              // mask for all events
                                (USHORT)DLC_IGNORE_STATION_ID,
                                (USHORT)DLC_STATION_MASK_SPECIFIC,
                                pPacket->Event.pEventInformation
                                );
        }

        if (pDlcCommand == NULL) {
            pDlcCommand = SearchAndRemoveCommand(&pFileContext->CommandQueue,
                                                 pPacket->Event.Event,
                                                 pPacket->Event.StationId,
                                                 pPacket->Event.Overlay.StationIdMask
                                                 );
        }

        if (pDlcCommand != NULL) {

            BOOLEAN DeallocateEvent;

            DeallocateEvent = pDlcCommand->Overlay.pfCompletionHandler(
                                pFileContext,
                                pPacket->Event.pOwnerObject,
                                pDlcCommand->pIrp,
                                (UINT)pPacket->Event.Event,
                                pPacket->Event.pEventInformation,
                                pPacket->Event.SecondaryInfo
                                );

            if (DeallocateEvent) {

                DEALLOCATE_PACKET_DLC_PKT(pFileContext->hPacketPool, pPacket);

            }

            DEALLOCATE_PACKET_DLC_PKT(pFileContext->hPacketPool, pDlcCommand);

            return;
        }
    }

    //
    // queue this event packet if it is to be picked up by a READ
    //

    if (pPacket->Event.Event & DLC_READ_FLAGS) {
        LlcInsertTailList(&pFileContext->EventQueue, pPacket);
    }
}


NTSTATUS
MakeDlcEvent(
    IN PDLC_FILE_CONTEXT pFileContext,
    IN ULONG Event,
    IN USHORT StationId,
    IN PDLC_OBJECT pDlcObject,
    IN PVOID pEventInformation,
    IN ULONG SecondaryInfo,
    IN BOOLEAN FreeEventInfo
    )

/*++

Routine Description:

    The routine allocates a event packet, saves the event information
    into it and queues (or completes) the event packet.

Arguments:

    pFileContext        - process specific adapter context
    Event               - event code
    StationId           - station id the event is destined
    pDlcObject          - the optional dlc object used in the event completion
    pEventInformation   - generic event information
    SecondaryInfo       - optional misc. data
    FreeEventInfo       - TRUE if pEventInformation should be deallocated

Return Value:

    NTSTATUS:
        STATUS_SUCCESS
        DLC_STATUS_NO_MEMORY

--*/

{
    PDLC_EVENT pDlcEvent;

    DIAG_FUNCTION("MakeDlcEvent");

    //
    // We couldn't find any matching commands for this event and
    // this event is a queued event => allocate a packet and
    // queue the event.
    //

    pDlcEvent = ALLOCATE_PACKET_DLC_PKT(pFileContext->hPacketPool);

    if (pDlcEvent ==  NULL) {
        return DLC_STATUS_NO_MEMORY;
    }
    pDlcEvent->Event = Event;
    pDlcEvent->StationId = StationId;
    pDlcEvent->pOwnerObject = pDlcObject;
    pDlcEvent->SecondaryInfo = SecondaryInfo;
    pDlcEvent->pEventInformation = pEventInformation;
    pDlcEvent->bFreeEventInfo = FreeEventInfo;
    QueueDlcEvent(pFileContext, (PDLC_PACKET)pDlcEvent);
    return STATUS_SUCCESS;
}


PDLC_COMMAND
IsCommandOnList(
    IN PVOID RequestHandle,
    IN PLIST_ENTRY List
    )

/*++

Routine Description:

    Searches the command queue of a DLC file context for a 'request handle'
    which is the address (in user space) of a command CCB, such as a READ

    If RequestHandle is located, a pointer to the DLC_COMMAND containing
    it is returned, else NULL

    Note: Assumes that handles are not shared between processes (it looks
    as though the entire driver assumes this) and this function is called
    within the context of the process to which the searched handle belongs

Arguments:

    RequestHandle   - address of CCB to look for
    List            - address of a list of DLC_COMMAND structures

Return Value:

    PDLC_COMMAND
        Success - address of located DLC_COMMAND structure containing
                  RequestHandle (in AbortHandle field)
        Failure - NULL

--*/

{
    PLIST_ENTRY entry;

    if (!IsListEmpty(List)) {
        for (entry = List->Flink; entry != List; entry = entry->Flink) {
            if (((PDLC_COMMAND)entry)->AbortHandle == RequestHandle) {
                return (PDLC_COMMAND)entry;
            }
        }
    }
    return NULL;
}


PDLC_COMMAND
SearchAndRemoveCommand(
    IN PLIST_ENTRY pQueueBase,
    IN ULONG Event,
    IN USHORT StationId,
    IN USHORT StationIdMask
    )

/*++

Routine Description:

    The routine searches and removes the given command or event from
    command, event or receive command queue.
    The station id, its mask, event mask and search handle are used
    to define the search.

Arguments:

    pQueueBase - address of queue's base pointer

    Event - event code

    StationId - station id of this command

    StationIdMask - station id mask for the event station id

    pSearchHandle - additional search key,  this is actually an
        orginal user mode ccb pointer (vdm or Windows/Nt)

Return Value:

    PDLC_COMMAND

--*/

{
    PDLC_COMMAND pCmd;

    DIAG_FUNCTION("SearchAndRemoveCommand");

    //
    // Events and commands are both saved to entry lists and this
    // procedure is used to search a macthing event for a command
    // or vice verse.  Commands has a masks, that may defines
    // the search for a specific station id, all stations on a sap
    // or all station ids.
    // the newest element in the list and the next element is the oldest
    // The commands are always scanned from the oldest to the newest.
    //

    if (!IsListEmpty(pQueueBase)) {

        for (pCmd = (PDLC_COMMAND)pQueueBase->Flink;
             pCmd != (PDLC_COMMAND)pQueueBase;
             pCmd = (PDLC_COMMAND)pCmd->LlcPacket.pNext) {

            if ((pCmd->Event & Event)
            && (pCmd->StationId & pCmd->StationIdMask & StationIdMask)
                == (StationId & pCmd->StationIdMask & StationIdMask)) {

                LlcRemoveEntryList(pCmd);
                return pCmd;
            }
        }
    }
    return NULL;
}


PDLC_COMMAND
SearchAndRemoveAnyCommand(
    IN PDLC_FILE_CONTEXT pFileContext,
    IN ULONG EventMask,
    IN USHORT StationId,
    IN USHORT StationIdMask,
    IN PVOID pSearchHandle
    )

/*++

Routine Description:

    The routine searches a dlc command from the normal read command queue
    for events and the special receive command queue.

Arguments:

    pQueueBase - address of queue's base pointer

    Event - event code

    StationId - station id of this command

    StationIdMask - station id mask for the event station id

    pSearchHandle - additional search key,  this is actually an
        orginal user mode ccb pointer (vdm or Windows/Nt)

Return Value:

    PDLC_COMMAND

--*/

{
    PDLC_COMMAND pDlcCommand;

    DIAG_FUNCTION("SearchAndRemoveAnyCommand");

    pDlcCommand = SearchAndRemoveCommandByHandle(&pFileContext->CommandQueue,
                                                 EventMask,
                                                 StationId,
                                                 StationIdMask,
                                                 pSearchHandle
                                                 );
    if (pDlcCommand == NULL) {
        pDlcCommand = SearchAndRemoveCommandByHandle(&pFileContext->ReceiveQueue,
                                                     EventMask,
                                                     StationId,
                                                     StationIdMask,
                                                     pSearchHandle
                                                     );
    }
    return pDlcCommand;
}


PDLC_COMMAND
SearchAndRemoveCommandByHandle(
    IN PLIST_ENTRY pQueueBase,
    IN ULONG Event,
    IN USHORT StationId,
    IN USHORT StationIdMask,
    IN PVOID pSearchHandle
    )

/*++

Routine Description:

    The routine searches and removes the given command or event from
    command, event or receive command queue using a search handle.
    This search routine is tailored to find the commands belonging
    only to the deleted object (this searches only the exact macthes).
    The other search routine supports wild cards only for the read
    commands and thus it cannot be used here.  We just want to remove
    only those commands, that read events from the deleted object but not
    from elsewhere.

Arguments:

    pQueueBase      - address of queue's base pointer
    Event           - event code or mask for the searched events
    StationId       - station id of this command
    StationIdMask   - station id mask for the event station id
    pSearchHandle   - additional search key,  this is actually an orginal user
                      mode ccb pointer (vdm or Windows/Nt)

Return Value:

    PDLC_COMMAND

--*/

{
    PDLC_COMMAND pCmd;

    DIAG_FUNCTION("SearchAndRemoveCommandByHandle");

    if (!IsListEmpty(pQueueBase)) {

        for (pCmd = (PDLC_COMMAND)pQueueBase->Flink;
             pCmd != (PDLC_COMMAND)pQueueBase;
             pCmd = (PDLC_COMMAND)pCmd->LlcPacket.pNext) {

            //
            // The event mask match always!
            //

            if ((pCmd->Event & Event)
            && (pSearchHandle == DLC_MATCH_ANY_COMMAND
            || pSearchHandle == pCmd->AbortHandle
            || (pCmd->StationId & StationIdMask) == (StationId & StationIdMask))) {

                LlcRemoveEntryList(pCmd);
                return pCmd;
            }
        }
    }
    return NULL;
}


PDLC_COMMAND
SearchAndRemoveSpecificCommand(
    IN PLIST_ENTRY pQueueBase,
    IN PVOID pSearchHandle
    )

/*++

Routine Description:

    Searches for a DLC_COMMAND structure having a specific search handle (ie
    abort handle or application CCB address). If found, removes the DLC_COMMAND
    from the queue, else returns NULL

Arguments:

    pQueueBase      - address of queue's base pointer
    pSearchHandle   - additional search key,  this is actually an orginal user
                      mode ccb pointer (vdm or Windows/Nt)

Return Value:

    PDLC_COMMAND

--*/

{
    DIAG_FUNCTION("SearchAndRemoveSpecificCommand");

    if (!IsListEmpty(pQueueBase)) {

        PDLC_COMMAND pCmd;

        for (pCmd = (PDLC_COMMAND)pQueueBase->Flink;
             pCmd != (PDLC_COMMAND)pQueueBase;
             pCmd = (PDLC_COMMAND)pCmd->LlcPacket.pNext) {

            //
            // The event mask match always!
            //

            if (pSearchHandle == pCmd->AbortHandle) {
                LlcRemoveEntryList(pCmd);
                return pCmd;
            }
        }
    }
    return NULL;
}


NTSTATUS
QueueDlcCommand(
    IN PDLC_FILE_CONTEXT pFileContext,
    IN ULONG Event,
    IN USHORT StationId,
    IN USHORT StationIdMask,
    IN PIRP pIrp,
    IN PVOID AbortHandle,
    IN PFCOMPLETION_HANDLER pfCompletionHandler
    )

/*++

Routine Description:

    The routine tries first to find a matching event in the event
    queue and  queues the DLC command if it can't find an event.

Arguments:

    pFileContext        - process specific adapter context
    Event               - event code
    StationId           - station id the event is destined
    StationIdMask       - mask used to define the destination station group
    pIrp                - the i/o request packet of the related DLC command,
                          link to the input and output parameters.
    AbortHandle         - handle used to cancel the command from the queue
    pfCompletionHandler - completion handler of the command, called when a
                          matching event has been found.

Return Value:

    NTSTATUS

--*/

{
    PDLC_COMMAND pDlcCommand;
    PDLC_EVENT pEvent;

    DIAG_FUNCTION("QueueDlcCommand");

    pEvent = SearchAndRemoveEvent(&pFileContext->EventQueue,
                                  Event,
                                  StationId,
                                  StationIdMask
                                  );
    if (pEvent != NULL) {

        BOOLEAN DeallocateEvent;

        DeallocateEvent = pfCompletionHandler(pFileContext,
                                              pEvent->pOwnerObject,
                                              pIrp,
                                              (UINT)pEvent->Event,
                                              pEvent->pEventInformation,
                                              pEvent->SecondaryInfo
                                              );
        if (DeallocateEvent) {

            DEALLOCATE_PACKET_DLC_PKT(pFileContext->hPacketPool, pEvent);

        }
    } else {

        //
        // We couldn't find any matching command for this event and
        // this event is a queued event => allocate a packet and
        // queue the event.
        //

        pDlcCommand = (PDLC_COMMAND)ALLOCATE_PACKET_DLC_PKT(pFileContext->hPacketPool);

        if (pDlcCommand ==  NULL) {
            return DLC_STATUS_NO_MEMORY;
        }
        pDlcCommand->Event = Event;
        pDlcCommand->pIrp = pIrp;
        pDlcCommand->StationId = StationId;
        pDlcCommand->StationIdMask = StationIdMask;
        pDlcCommand->AbortHandle = AbortHandle;
        pDlcCommand->Overlay.pfCompletionHandler = pfCompletionHandler;

        //
        // The permanent receive commands, that do not actually read
        // anuting (just enable the data receiving) are put to another
        // queue to speed up the search of the read commands.
        //

        if (Event == LLC_RECEIVE_COMMAND_FLAG) {
            LlcInsertTailList(&pFileContext->ReceiveQueue, pDlcCommand);
        } else {
            LlcInsertTailList(&pFileContext->CommandQueue, pDlcCommand);
        }

        //
        // Asynchronous commands returns ALWAYS the pending status.
        //
    }
    return STATUS_PENDING;
}


NTSTATUS
AbortCommand(
    IN PDLC_FILE_CONTEXT pFileContext,
    IN USHORT StationId,
    IN USHORT StationIdMask,
    IN PVOID AbortHandle,
    IN OUT PVOID *ppCcbLink,
    IN UINT CancelStatus,
    IN BOOLEAN SuppressCommandCompletion
    )

/*++

Routine Description:

    The routine searches and cancels a command from a command queue.
    The commands must always belong to the defined DLC object.
    A NULL value in abort handle selects all matching commands
    found in the queue.

Arguments:

    pFileContext                -
    StationId                   - station id the searched command is destined for
    StationIdMask               - station id mask used in the search
    AbortHandle                 - handle used to cancel the command from the
                                  queue. The whole command queue will be scanned
                                  if this handle is NULL
    ppCcbLink                   - the canceled commands are linked by their next
                                  CCB pointer fieldsr. The caller must provide
                                  the next CCB address in this parameter
                                  (usually *ppCcbLink == NULL) and the function
                                  will return the address of the last cancelled
                                  CCB field.
    CancelStatus                - Status for the command to be canceled
    SuppressCommandCompletion   - the flag is set, if the normal command
                                  completion is suppressed.

Return Value:

     - no mathing command was found
    STATUS_SUCCESS - the command was canceled

--*/

{
    PDLC_COMMAND pDlcCommand;

    DIAG_FUNCTION("AbortCommand");

    pDlcCommand = SearchAndRemoveAnyCommand(pFileContext,
                                            (ULONG)(-1), // search all commands
                                            StationId,
                                            StationIdMask,
                                            AbortHandle
                                            );
    if (pDlcCommand == NULL && AbortHandle == DLC_MATCH_ANY_COMMAND) {
        pDlcCommand = pFileContext->pTimerQueue;
        if (pDlcCommand != NULL) {
            pFileContext->pTimerQueue = (PDLC_COMMAND)pDlcCommand->LlcPacket.pNext;
        }
    }
    if (pDlcCommand != NULL) {
        CancelDlcCommand(pFileContext,
                         pDlcCommand,
                         ppCcbLink,
                         CancelStatus,
                         SuppressCommandCompletion
                         );
        return STATUS_SUCCESS;
    } else {
        return DLC_STATUS_INVALID_CCB_POINTER;
    }
}


VOID
CancelDlcCommand(
    IN PDLC_FILE_CONTEXT pFileContext,
    IN PDLC_COMMAND pDlcCommand,
    IN OUT PVOID *ppCcbLink,
    IN UINT CancelStatus,
    IN BOOLEAN SuppressCommandCompletion
    )

/*++

Routine Description:

    The cancels and optionally completes the given DLC command. Called when one
    DLC I/O request is used to kill another (e.g. READ.CANCEL, DIR.TIMER.CANCEL)

Arguments:

    pFileContext                -
    pDlcCommand                 -
    ppCcbLink                   - the canceled commands are linked by their next
                                  CCB pointer fields. The caller must provide
                                  the next CCB address in this parameter
                                  (usually *ppCcbLink == NULL) and the function
                                  will return the address of the last cancelled
                                  CCB field
    CancelStatus                - Status for the command to be canceled
    SuppressCommandCompletion   - if set, normal command completion is suppressed

Return Value:

    None

--*/

{
    PVOID pOldCcbLink;

    DIAG_FUNCTION("CancelDlcCommand");

    //
    // We must return the current CCB link to be linked to the next cancelled
    // CCB command (or to the CCB pointer of cancelling command). But first
    // save the previous CCB link before we read a new one
    //

    pOldCcbLink = *ppCcbLink;
    *ppCcbLink = ((PNT_DLC_PARMS)pDlcCommand->pIrp->AssociatedIrp.SystemBuffer)->Async.Ccb.pCcbAddress;

    //
    // Check if we must suppress any kind of command completion indications to
    // the applications. I/O system should not care, if its event handle is
    // removed
    //

    if (SuppressCommandCompletion) {
        pDlcCommand->pIrp->UserEvent = NULL;
    }
    CompleteAsyncCommand(pFileContext, CancelStatus, pDlcCommand->pIrp, pOldCcbLink, FALSE);

    DEALLOCATE_PACKET_DLC_PKT(pFileContext->hPacketPool, pDlcCommand);

}


VOID
PurgeDlcEventQueue(
    IN PDLC_FILE_CONTEXT pFileContext
    )

/*++

Routine Description:

    Deletes all events from a FILE_CONTEXT event queue. Called when the
    FILE_CONTEXT is being deleted and before we deallocate the packet pool from
    which the events were allocated

Arguments:

    pFileContext    - pointer to FILE_CONTEXT owning the queue

Return Value:

    None.

--*/

{
    PDLC_EVENT p;

    while (!IsListEmpty(&pFileContext->EventQueue)) {
        p = (PDLC_EVENT)RemoveHeadList(&pFileContext->EventQueue);
        if (p->bFreeEventInfo && p->pEventInformation) {

#if DBG
            DbgPrint("PurgeDlcEventQueue: deallocating pEventInformation: %x\n", p->pEventInformation);
#endif

            DEALLOCATE_PACKET_DLC_PKT(pFileContext->hPacketPool, p->pEventInformation);

        }

        DEALLOCATE_PACKET_DLC_PKT(pFileContext->hPacketPool, p);

    }
}


VOID
PurgeDlcFlowControlQueue(
    IN PDLC_FILE_CONTEXT pFileContext
    )

/*++

Routine Description:

    Deletes all packets from the flow control queue. Called when the FILE_CONTEXT
    is being deleted and before we deallocate the packet pool from which flow
    control packets were allocated

Arguments:

    pFileContext    - pointer to FILE_CONTEXT owning the queue

Return Value:

    None.

--*/

{
    PDLC_RESET_LOCAL_BUSY_CMD p;

    while (!IsListEmpty(&pFileContext->FlowControlQueue)) {
        p = (PDLC_RESET_LOCAL_BUSY_CMD)RemoveHeadList(&pFileContext->FlowControlQueue);

        DEALLOCATE_PACKET_DLC_PKT(pFileContext->hPacketPool, p);

    }
}


/*
//
//  Internal consistency check to hunt a bougus event in the event queue.
//
//extern BOOLEAN EventCheckDisabled;
//
int
CheckEventQueue(
    PDLC_FILE_CONTEXT   pFileContext
    )
{
    static PDLC_FILE_CONTEXT   pOldFileContext = NULL;

    if (pFileContext == NULL)
    {
        pFileContext = pOldFileContext;
    }
    else
    {
        pOldFileContext = pFileContext;
    }
    if (pFileContext == NULL)
        return 0;

    if (!IsListEmpty( &pFileContext->EventQueue ) &&
        pFileContext->EventQueue.Flink == pFileContext->EventQueue.Blink &&
        &pFileContext->EventQueue != pFileContext->EventQueue.Flink->Flink)
    {
        FooDebugBreak();
    }
    return 0;
}

int
FooDebugBreak()
{
    INT i;

    return i++;
}
*/
//    PDLC_EVENT  pEvent;
//
//    if (EventCheckDisabled || pFileContext->AdapterNumber != 0 ||
//        pFileContext->EventQueue == NULL)
//        return;
//
//    pEvent = (PDLC_EVENT)pFileContext->pEventQueue->LlcPacket.pNext;
//    for (;;)
//    {
//        if (pEvent->Event == LLC_STATUS_CHANGE &&
//            pEvent->pOwnerObject == NULL)
//            DebugBreak();
//        if (pEvent == pFileContext->pEventQueue)
//            break;
//        pEvent = (PDLC_EVENT)pEvent->LlcPacket.pNext;
//    }
//}
//