summaryrefslogtreecommitdiffstats
path: root/private/ntos/ndis/htdsu/link.c
blob: 1983c07dd7f8789def25a78c238b55b2400ce648 (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
/***************************************************************************\
|* Copyright (c) 1994  Microsoft Corporation                               *|
|* Developed for Microsoft by TriplePoint, Inc. Beaverton, Oregon          *|
|*                                                                         *|
|* This file is part of the HT Communications DSU41 WAN Miniport Driver.   *|
\***************************************************************************/
#include "version.h"
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Module Name:

    link.c

Abstract:

    This module contains the WAN Miniport link management routines.  All
    information about the state of the link is stored in the Link structure.
        LinkInitialize()
        LinkAllocate()
        LinkRelease()
        LinkLineUp()
        LinkLineDown()
        LinkLineError()

    This driver conforms to the NDIS 3.0 Miniport interface.

Author:

    Larry Hattery - TriplePoint, Inc. (larryh@tpi.com) Jun-94

Environment:

    Windows NT 3.5 kernel mode Miniport driver or equivalent.

Revision History:

---------------------------------------------------------------------------*/

#define  __FILEID__     4       // Unique file ID for error logging

#include "htdsu.h"


VOID
LinkInitialize(
    IN PHTDSU_ADAPTER   Adapter,
    IN PSTRING          AddressList
    )

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Functional Description:

    This routine initializes the Link structures within the Adapter
    structure.  We use the link structure to hold all the information about
    a connection, whether the connection is made via TAPI calls or is a
    leased line WAN connection.

Parameters:

    Adapter _ A pointer ot our adapter information structure.

    AddressList _ This is a list of MULTI_SZ strings which are to be assigned
                  to each link for use by TAPI HtTapiGetAddressCaps.

Return Values:

    None.

---------------------------------------------------------------------------*/

{
    DBG_FUNC("LinkInitialize")

    /*
    // A pointer to our link information structure for the selected line device.
    */
    PHTDSU_LINK Link;

    /*
    // Index into the link array.
    */
    USHORT LinkIndex;

    /*
    // A pointer to the RAS/TAPI line address assigned to each link.
    */
    PUCHAR LineAddress = AddressList->Buffer;

    DBG_ENTER(Adapter);

    ASSERT(Adapter->NumLineDevs <= HTDSU_NUM_LINKS);

    /*
    // Go through and initialize each link.
    */
    for (LinkIndex = 0; LinkIndex < HTDSU_NUM_LINKS; ++LinkIndex)
    {
        Link = GET_LINK_FROM_LINKINDEX(Adapter, LinkIndex);
        
        /*
        // Initially, the link is not allocated to anyone and these fields
        // must be reset.
        // We can assume the entire Adapter structure is zeroed to begin with.
        */
        ASSERT(Link->Adapter == (PHTDSU_ADAPTER)0);
        ASSERT(Link->htLine == (HTAPI_LINE)0);
        ASSERT(Link->htCall == (HTAPI_CALL)0);
        ASSERT(Link->NdisLinkContext == NULL);

        /*
        // Setup the static features of the link.
        */
        Link->CardLine          = LinkIndex + HTDSU_CMD_LINE1;
        Link->LinkIndex         = LinkIndex;
        Link->LinkSpeed         = _56KBPS;
        Link->LineMode          = HTDSU_LINEMODE_DIALUP;
        Link->MediaModesCaps    = LINEMEDIAMODE_DIGITALDATA;
        Link->Quality           = NdisWanErrorControl;
        
        /*
        // If we run off the end of the address list, we just point at the
        // null terminator for the other addresses.  This might happen if
        // some of the lines were not configured for use with RAS/TAPI.
        */
        strcpy(Link->LineAddress, LineAddress);
        LineAddress += strlen(LineAddress) + 1;
        if ((LineAddress - AddressList->Buffer) >= AddressList->Length)
        {
            --LineAddress;
        }

        DBG_NOTICE(Adapter,("LineAddress=<%s>\n",Link->LineAddress));

        /*
        // Initialize the TAPI event capabilities supported by the link.
        */
        Link->DevStatesCaps     = LINEDEVSTATE_RINGING |
                                  LINEDEVSTATE_CONNECTED |
                                  LINEDEVSTATE_DISCONNECTED |
                                  LINEDEVSTATE_OUTOFSERVICE |
                                  LINEDEVSTATE_OPEN |
                                  LINEDEVSTATE_CLOSE;
        Link->AddressStatesCaps = 0;
        Link->CallStatesCaps    = LINECALLSTATE_IDLE |
                                  LINECALLSTATE_OFFERING |
                                  LINECALLSTATE_ACCEPTED |
                                  LINECALLSTATE_DIALING |
                                  LINECALLSTATE_BUSY |
                                  LINECALLSTATE_CONNECTED |
                                  LINECALLSTATE_PROCEEDING |
                                  LINECALLSTATE_DISCONNECTED;
        /*
        // We use this timer to keep track of incoming and outgoing call 
        // status, and to provide timeouts for certain call states.
        */
        NdisMInitializeTimer(
            &Link->CallTimer,
            Adapter->MiniportAdapterHandle,
            HtTapiCallTimerHandler,
            Link
            );
    }
    DBG_LEAVE(Adapter);
}


PHTDSU_LINK
LinkAllocate(
    IN PHTDSU_ADAPTER Adapter,
    IN HTAPI_LINE htLine,
    IN USHORT LinkIndex
    )

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Functional Description:

    This routine allocates a specific Link structure and passes back a
    pointer which can be used by the driver to access the link.

Parameters:

    Adapter _ A pointer ot our adapter information structure.

    Connection _ The TAPI connection handle to be associated with the link.

    LinkIndex _ The specific link structure being allocated.

Return Values:

    A pointer to allocated link information structure, NULL if not allocated.

---------------------------------------------------------------------------*/

{
    DBG_FUNC("LinkAllocate")

    /*
    // A pointer to our link information structure for the selected line device.
    */
    PHTDSU_LINK Link;

    DBG_ENTER(Adapter);

    Link = GET_LINK_FROM_LINKINDEX(Adapter, LinkIndex);

    if (Link->Adapter == (PHTDSU_ADAPTER) 0)
    {
        /*
        // We use the Adapter field to flag whether the link has been allocated.
        // The htLine field is used to associate this link with the TAPI
        // connection wrapper.  Reset all the state information for this link.
        */
        Link->Adapter           = Adapter;
        Link->htLine            = htLine;
        Link->RingCount         = 0;
        Link->DevState          = 0;
        Link->DevStatesMask     = 0;    // Default to indicate no line events
        Link->AddressState      = 0;
        Link->AddressStatesMask = 0;    // Default to indicate no address events
        Link->CallState         = 0;
        Link->CallStatesMask    = Link->CallStatesCaps;
        Link->MediaMode         = Link->MediaModesCaps;
        Link->MediaModesMask    = 0;
        Link->Closing           = FALSE;
        Link->CallClosing       = FALSE;

        /*
        // Initialize the default link information structure.  It may be
        // changed later by MiniportSetInformation.
        */
        Link->WanLinkInfo.NdisLinkHandle   = Link;
        Link->WanLinkInfo.MaxSendFrameSize = Adapter->WanInfo.MaxFrameSize;
        Link->WanLinkInfo.MaxRecvFrameSize = Adapter->WanInfo.MaxFrameSize;
        Link->WanLinkInfo.SendFramingBits  = Adapter->WanInfo.FramingBits;
        Link->WanLinkInfo.RecvFramingBits  = Adapter->WanInfo.FramingBits;
        Link->WanLinkInfo.SendACCM         = Adapter->WanInfo.DesiredACCM;
        Link->WanLinkInfo.RecvACCM         = Adapter->WanInfo.DesiredACCM;
    }
    else
    {
        /*
        // The requested link has already been allocated.
        */
        Link = NULL;
    }

    DBG_LEAVE(Adapter);

    return (Link);
}


VOID
LinkRelease(
    IN PHTDSU_LINK Link
    )

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Functional Description:

    This routine releases a specific Link structure and makes it available
    for future allocation.  It is assumed that the caller has closed any
    associated connection and notified TAPI and WAN with a LINE_DOWN.

Parameters:

    Link _ A pointer to the link information structure to be released.

Return Values:

    None.

---------------------------------------------------------------------------*/

{
    DBG_FUNC("LinkRelease")
    DBG_ENTER(Link->Adapter);

    Link->Adapter         = (PHTDSU_ADAPTER)0;
    Link->htLine          = (HTAPI_LINE)0;
    Link->htCall          = (HTAPI_CALL)0;
    Link->NdisLinkContext = NULL;
}


VOID
LinkLineUp(
    IN PHTDSU_LINK Link
    )

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Functional Description:

    This routine marks a link as connected and sends a LINE_UP indication to 
    the WAN wrapper.

    A line up indication is generated when a new link becomes active. Prior 
    to this the MAC will accept frames and may let them succeed or fail, but 
    it is unlikely that they will actually be received by any remote. During 
    this state protocols are encouraged to reduce their timers and retry 
    counts so as to quickly fail any outgoing connection attempts.  

    NOTE: This indication must be sent to the WAN wrapper prior to returning 
    from the OID_TAPI_ANSWER request, and prior to indicating the 
    LINECALLSTATE_CONNECTED to the connection wrapper.  Otherwise, the 
    connection wrapper client might attempt to send data to the WAN wrapper 
    before it is aware of the line.

    The status code for the line up indication is NDIS_STATUS_WAN_LINE_UP.  
    The format of the StatusBuffer for this code is:

    typedef struct _NDIS_MAC_LINE_UP
    {
        IN  ULONG               LinkSpeed;
        IN  NDIS_WAN_QUALITY    Quality;
        IN  USHORT              SendWindow;
        IN  NDIS_HANDLE         ConnectionWrapperID;
        IN  NDIS_HANDLE         NdisLinkHandle;
        OUT NDIS_HANDLE         NdisLinkContext;
        
    } NDIS_MAC_LINE_UP, *PNDIS_MAC_LINE_UP;

    LinkSpeed _ The speed of the link, in 100 bps units.
    
    Quality _ The quality of the new line.
    
    SendWindow _ The recommended send window, i.e., the number of packets 
                 that should be given to the adapter before pausing to wait 
                 for an acknowledgement. Some devices achieve higher 
                 throughput if they have several packets to send at once; 
                 others are especially unreliable. A value of 0 indicates 
                 no recommendation.
    
    ConnectionWrapperID _ The MAC supplied handle by which this line will be 
                          known to the connection wrapper clients.  This must 
                          be a unique handle across all drivers using the 
                          connection wrapper, so typically htCall should be 
                          used to gaurantee it is unique.  This must be the 
                          same value returned from the OID_TAPI_GETID request 
                          for the “ndis” device class.  Refer to the 
                          Connection Wrapper Interface Specification for 
                          further details.  If not using the connection 
                          wrapper, the value is 0.
    
    NdisLinkHandle _ The MAC supplied handle passed down in future Miniport 
                     calls (such as MiniportSend) for this link.  Typically, 
                     the MAC will provide a pointer to its control block for 
                     that link.  The value must be unique, for the first 
                     LINE_UP for that link.  Subsequent LINE_UPs may be 
                     called if line characteristics change.  When subsequent 
                     LINE_UP calls are made, the MiniportLinkHandle must be 
                     filled with the value returned on the first LINE_UP call.
    
    NdisLinkContext _ The WAN wrapper supplied handle to be used in future 
                      Miniport calls (such as MiniportReceive) to the wrapper.
                      The WAN wrapper will provide a unique handle for every 
                      unique LINE_UP.  The MiniportLinkHandle must be 0 if 
                      this is the first LINE_UP.  It must contain the value 
                      returned on the first LINE_UP for subsequent LINE_UP 
                      calls.

Parameters:

    Link _ A pointer to our link information structure, on which this LINE_UP
           indication is being made.

Return Values:

    None.

---------------------------------------------------------------------------*/

{
    DBG_FUNC("LinkLineUp")

    NDIS_MAC_LINE_UP LineUpInfo;

    DBG_ENTER(Link->Adapter);

    /*
    // Initialize the LINE_UP event packet.
    */
    LineUpInfo.LinkSpeed           = Link->LinkSpeed / 100;
    LineUpInfo.Quality             = Link->Quality;
    LineUpInfo.SendWindow          = Link->SendWindow;
    LineUpInfo.ConnectionWrapperID = (NDIS_HANDLE) Link->htCall;
    LineUpInfo.NdisLinkHandle      = Link;
    LineUpInfo.NdisLinkContext     = Link->NdisLinkContext;

    /*
    // Indicate the event to the WAN wrapper.
    */
    NdisMIndicateStatus(Link->Adapter->MiniportAdapterHandle,
                        NDIS_STATUS_WAN_LINE_UP,
                        &LineUpInfo,
                        sizeof(LineUpInfo)
                        );

    /*
    // Save the WAN wrapper link context for use when indicating received
    // packets and errors.
    */
    Link->NdisLinkContext = LineUpInfo.NdisLinkContext;

    DBG_NOTICE(Link->Adapter,
              ("MAC_LINE_UP: LinkHandle=%Xh NdisHandle=%Xh WrapperID=%Xh\n",
               LineUpInfo.NdisLinkHandle,
               LineUpInfo.NdisLinkContext,
               LineUpInfo.ConnectionWrapperID
              ));

    DBG_LEAVE(Link->Adapter);
}


VOID
LinkLineDown(
    IN PHTDSU_LINK Link
    )

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Functional Description:

    This routine marks a link as disconnected and sends a LINE_DOWN
    indication to the WAN wrapper.

    A line down indication is generated when a link goes down. Protocols 
    should again reduce their timers and retry counts until the next line 
    up indication.

    The status code for the line down indication is NDIS_STATUS_WAN_LINE_DOWN.
    The format of the StatusBuffer for this code is:

    typedef struct _NDIS_MAC_LINE_DOWN
    {
        IN  NDIS_HANDLE         NdisLinkContext;
        
    } NDIS_MAC_LINE_DOWN, *PNDIS_MAC_LINE_DOWN;

    MiniportLinkContext _ Value returned in NDIS_WAN_LINE_UP.

Parameters:

    Link _ A pointer to our link information structure, on which this LINE_DOWN
           indication is being made.

Return Values:

    None.

---------------------------------------------------------------------------*/

{
    DBG_FUNC("LinkLineDown")

    NDIS_MAC_LINE_DOWN LineDownInfo;

    DBG_ENTER(Link->Adapter);

    /*
    // We can't allow indications to NULL...
    */
    if (Link->NdisLinkContext)
    {
        DBG_NOTICE(Link->Adapter,
                  ("MAC_LINE_DOWN: NdisHandle=%Xh\n",
                   Link->NdisLinkContext
                  ));

        /*
        // Setup the LINE_DOWN event packet and indicate the event to the 
        // WAN wrapper.
        */
        LineDownInfo.NdisLinkContext = Link->NdisLinkContext;

        NdisMIndicateStatus(Link->Adapter->MiniportAdapterHandle,
                            NDIS_STATUS_WAN_LINE_DOWN,
                            &LineDownInfo,
                            sizeof(LineDownInfo)
                            );
        /*
        // The line is down, so there's no more context for receives.
        */
        Link->NdisLinkContext = NULL;
        Link->CallClosing     = FALSE;
    }

    DBG_LEAVE(Link->Adapter);
}


VOID
LinkLineError(
    IN PHTDSU_LINK Link,
    IN ULONG Errors
    )

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Functional Description:

    This routine is used to indicate to the WAN wrapper that a partial
    packet was received from the remote end.  The NDIS_STATUS_WAN_FRAGMENT
    indication is used to notify WAN wrapper.

    A fragment indication indicates that a partial packet was received from 
    the remote. The protocol is encouraged to send frames to the remote that 
    will notify it of this situation, rather than waiting for a timeout to 
    occur.

    The status code for the fragment indication is NDIS_STATUS_WAN_FRAGMENT.  
    The format of the StatusBuffer for this code is:

    typedef struct _NDIS_MAC_FRAGMENT
    {
        IN  NDIS_HANDLE         NdisLinkContext;
        IN  ULONG               Errors;
        
    } NDIS_MAC_FRAGMENT, *PNDIS_MAC_FRAGMENT;

    NdisLinkContext _ Value returned in NDIS_WAN_LINE_UP.
    
    Errors _ A bit field set to one or more bits indicating the reason the 
             fragment was received.  If no direct mapping from the WAN medium 
             error to one of the six errors listed below exists, choose the 
             most apropriate error:
             
        	 WAN_ERROR_CRC
        	 WAN_ERROR_FRAMING
        	 WAN_ERROR_HARDWAREOVERRUN
        	 WAN_ERROR_BUFFEROVERRUN
        	 WAN_ERROR_TIMEOUT
             WAN_ERROR_ALIGNMENT
        
    NOTE: The WAN wrapper keeps track of dropped packets by counting the 
          number of fragment indications on the link.

Parameters:

    Link _ A pointer to our link information structure, on which this error
           was encountered.

Return Values:

    None.

---------------------------------------------------------------------------*/

{
    DBG_FUNC("LinkLineError")

    NDIS_MAC_FRAGMENT FragmentInfo;

    /*
    // We can't allow indications to NULL...
    */
    if (Link->NdisLinkContext)
    {
        DBG_ENTER(Link->Adapter);

        DBG_NOTICE(Link->Adapter,
                  ("MAC_LINE_ERROR: NdisHandle=%Xh Errors=%Xh\n",
                   Link->NdisLinkContext,
                   Errors
                  ));

        /*
        // Setup the FRAGMENT event packet and indicate it to the WAN wrapper.
        */
        FragmentInfo.NdisLinkContext = Link->NdisLinkContext;
        FragmentInfo.Errors = Errors;

        NdisMIndicateStatus(Link->Adapter->MiniportAdapterHandle,
                            NDIS_STATUS_WAN_FRAGMENT,
                            &FragmentInfo,
                            sizeof(FragmentInfo)
                            );

        DBG_LEAVE(Link->Adapter);
    }
}