summaryrefslogtreecommitdiffstats
path: root/private/ntos/tdi/acd/request.c
blob: 7976e733d3612d2c03f75835e5afca96db288129 (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
/*++

Copyright (c) 1995 Microsoft Corporation

Module Name:

    request.c

Abstract:

    Worker thread for the automatic connection driver.

Author:

    Anthony Discolo (adiscolo)  17-Apr-1995

Environment:

    Kernel Mode

Revision History:

--*/

#include <ndis.h>
#include <cxport.h>
#include <tdi.h>
#include <tdikrnl.h>
#include <tdistat.h>
#include <tdiinfo.h>
#include <acd.h>

#include "acdapi.h"
#include "acddefs.h"
#include "mem.h"
#include "debug.h"

//
// External declarations
//
VOID AcdPrintAddress(
    IN PACD_ADDR pAddr
    );



VOID
ProcessCompletion(
    IN PACD_COMPLETION pCompletion,
    IN KIRQL irqlCancel,
    IN KIRQL irqlLock
    )
{
    PLIST_ENTRY pHead;
    KIRQL irql;
    PIRP pIrp;
    PIO_STACK_LOCATION pIrpSp;
    PACD_NOTIFICATION pNotification;

    ASSERT(!IsListEmpty(&AcdNotificationQueueG));
    //
    // Complete the next irp in the
    // AcdNotificationQueueG queue.  These
    // represent the ioctl completions the
    // system service has posted.  Completing
    // this request will start the system service
    // to create a new RAS connection.
    // Logically, there is always just one.
    //
    pHead = RemoveHeadList(&AcdNotificationQueueG);
    pIrp = CONTAINING_RECORD(pHead, IRP, Tail.Overlay.ListEntry);
    pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
    //
    // Disable the irp's cancel routine.
    //
    IoSetCancelRoutine(pIrp, NULL);
    //
    // Copy the success flag and the address into the
    // system buffer.  This will get copied into the
    // user's buffer on return.
    //
    pNotification = (PACD_NOTIFICATION)pIrp->AssociatedIrp.SystemBuffer;
    RtlCopyMemory(
      pNotification,
      &pCompletion->notif,
      sizeof (ACD_NOTIFICATION));
    IF_ACDDBG(ACD_DEBUG_WORKER) {
        AcdPrint(("AcdNotificationRequestThread: "));
        AcdPrintAddress(&pCompletion->notif.addr);
        AcdPrint((", ulFlags=0x%x\n", pCompletion->notif.ulFlags));
    }
    //
    // We can release both the cancel lock
    // and our lock now.
    //
    KeReleaseSpinLock(&AcdSpinLockG, irqlLock);
    IoReleaseCancelSpinLock(irqlCancel);
    //
    // Set the status code and the number
    // of bytes to be copied back to the user
    // buffer.
    //
    pIrp->IoStatus.Status = STATUS_SUCCESS;
    pIrp->IoStatus.Information = sizeof (ACD_NOTIFICATION);
    //
    // Complete the irp.
    //
    IoCompleteRequest(pIrp, IO_NO_INCREMENT);
} // ProcessCompletion



VOID
AcdNotificationRequestThread(
    PVOID context
    )

/*++

DESCRIPTION
    This thread handles the notification that an automatic
    connection may need to be initiated.  This needs to
    happen in a separate thread, because the notification
    may occur at DPC irql.

ARGUMENTS
    None.

RETURN VALUE
    None.

--*/

{
    KIRQL irql, irql2;
    PLIST_ENTRY pEntry, pEntry2;
    PACD_CONNECTION pConnection;
    PACD_COMPLETION pCompletion;
    BOOLEAN bStartTimer, bStopTimer;

    UNREFERENCED_PARAMETER(context);

    IoStartTimer(pAcdDeviceObjectG);

    for (;;) {
        bStartTimer = bStopTimer = FALSE;
        //
        // Acquire our lock.
        //
        IoAcquireCancelSpinLock(&irql);
        KeAcquireSpinLock(&AcdSpinLockG, &irql2);
        //
        // If there are no irps to complete,
        // then go back to sleep.
        //
        if (IsListEmpty(&AcdNotificationQueueG)) {
            IF_ACDDBG(ACD_DEBUG_WORKER) {
                AcdPrint(("AcdNotificationRequestThread: no ioctl to complete\n"));
            }
            KeReleaseSpinLock(&AcdSpinLockG, irql2);
            IoReleaseCancelSpinLock(irql);
            goto again;
        }
        //
        // Search for connections that haven't
        // been processed yet.
        //
        for (pEntry = AcdConnectionQueueG.Flink;
             pEntry != &AcdConnectionQueueG;
             pEntry = pEntry->Flink)
        {
            pConnection = CONTAINING_RECORD(pEntry, ACD_CONNECTION, ListEntry);

            //
            // Don't issue a request to the service
            // for more than one simultaneous connection.
            //
            IF_ACDDBG(ACD_DEBUG_WORKER) {
                AcdPrint((
                  "AcdNotificationRequestThread: pConnection=0x%x, fNotif=%d, fCompleting=%d\n",
                  pConnection,
                  pConnection->fNotif,
                  pConnection->fCompleting));
            }
            if (pConnection->fNotif)
                break;
            //
            // Skip all connections that are in
            // the process of being completed.
            //
            if (pConnection->fCompleting)
                continue;
            //
            // Make sure there is at least one
            // request in this connection that
            // hasn't been canceled.
            //
            for (pEntry2 = pConnection->CompletionList.Flink;
                 pEntry2 != &pConnection->CompletionList;
                 pEntry2 = pEntry2->Flink)
            {
                pCompletion = CONTAINING_RECORD(pEntry2, ACD_COMPLETION, ListEntry);

                if (!pCompletion->fCanceled) {
                    IF_ACDDBG(ACD_DEBUG_WORKER) {
                        AcdPrint((
                          "AcdNotificationRequestThread: starting pConnection=0x%x, pCompletion=0x%x\n",
                          pConnection,
                          pCompletion));
                    }
                    pConnection->fNotif = TRUE;
                    //
                    // This call releases both the cancel lock
                    // and our lock.
                    //
                    ProcessCompletion(pCompletion, irql, irql2);
                    //
                    // Start the connection timer.
                    //
                    bStartTimer = TRUE;
                    //
                    // We can only process one completion
                    // at a time.
                    //
                    goto again;
                }
            }
        }
        //
        // Complete other requests.
        //
        if (!IsListEmpty(&AcdCompletionQueueG)) {
            pEntry = RemoveHeadList(&AcdCompletionQueueG);
            pCompletion = CONTAINING_RECORD(pEntry, ACD_COMPLETION, ListEntry);

            IF_ACDDBG(ACD_DEBUG_WORKER) {
                AcdPrint((
                  "AcdNotificationRequestThread: starting pCompletion=0x%x\n",
                  pCompletion));
            }
            //
            // This call releases both the cancel lock
            // and our lock.
            //
            ProcessCompletion(pCompletion, irql, irql2);
            //
            // We are done with the completion,
            // so we can free the memory now.
            //
            FREE_MEMORY(pCompletion);
            //
            // We can only process one completion
            // at a time.
            //
            goto again;

        }
        //
        // If there are no connections pending,
        // then stop the connection timer.
        //
        if (IsListEmpty(&AcdConnectionQueueG))
            bStopTimer = TRUE;
        //
        // Release our lock.
        //
        KeReleaseSpinLock(&AcdSpinLockG, irql2);
        IoReleaseCancelSpinLock(irql);
again:
        //
        // Start or stop the timer, depending
        // on what we found while we had the
        // spinlock.  We can't hold our spin
        // lock when we call the Io*Timer
        // routines.
        //
#ifdef notdef
        if (bStopTimer)
            IoStopTimer(pAcdDeviceObjectG);
        else if (bStartTimer)
            IoStartTimer(pAcdDeviceObjectG);
#endif
        //
        // Wait for something to do.  This event
        // will be signaled by AcdSignalNotification().
        //
        IF_ACDDBG(ACD_DEBUG_WORKER) {
            AcdPrint(("AcdNotificationRequestThread: waiting on AcdPendingCompletionEventG\n"));
        }
        KeWaitForSingleObject(
          &AcdRequestThreadEventG,
          Executive,
          KernelMode,
          FALSE,
          NULL);
        KeClearEvent(&AcdRequestThreadEventG);
        IF_ACDDBG(ACD_DEBUG_WORKER) {
            AcdPrint(("AcdNotificationRequestThread: AcdPendingCompletionEventG signalled\n"));
        }
    }
} // AcdNotificationRequestThread