summaryrefslogtreecommitdiff
path: root/network/ndis/netvmini/6x/rssv2lib.c
blob: a2bc5550790b13bb0fd2b923ec5eb814d2400d8a (plain)
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
/*++

Copyright (c) Microsoft Corporation.  All rights reserved.

    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
    KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
    PURPOSE.

Module Name:

    RSSv2Lib.c

Abstract:

    This module implements RSSv2 helper library.

--*/
#pragma warning(push)
#define  PRAGMA_ZERO_SIZED_ARRAY        4200
#define  PRAGMA_NAMELESS_STRUCT_UNION   4201
#define  PRAGMA_BIT_FIELD_NOT_INT       4214
#define  PRAGMA_STRUCTURE_PADDED        4324
#define  PRAGMA_NO_RETTYPE_FOR_FUNC     4508
#pragma warning(disable: PRAGMA_NO_RETTYPE_FOR_FUNC)
#pragma warning(disable: PRAGMA_ZERO_SIZED_ARRAY) 
#pragma warning(disable: PRAGMA_NAMELESS_STRUCT_UNION)
#pragma warning(disable: PRAGMA_STRUCTURE_PADDED)
#include <ndis.h>
#pragma warning(pop)

#define RSSV2_MAX_NUMBER_OF_PROCESSORS_IN_RSS_TABLE 64
#include "rssv2lib.h"

extern
FORCEINLINE
VOID
RssV2InitializeParsingContext (
    _Out_ PRSSV2_PARSING_CONTEXT Context,
    _In_ PNDIS_RSS_SET_INDIRECTION_ENTRIES RssV2Oid,
    _In_ BOOLEAN IsNativeRss
    )
/*++
Routine Description:

    This routine initializes RSSv2 command parsing context from the OID.

Arguments:

    Context - parsing context

    RssV2Oid - OID that needs parsing

    IsNativeRSS - Boolean which tells if the miniport is in NativeRSS mode

Return Value:

    None.

--*/
{
    RtlZeroMemory(Context, sizeof(*Context));
    Context->IsNativeRss = IsNativeRss;
    Context->RssV2Oid = RssV2Oid;
    Context->MaxIndex = RssV2Oid->NumberOfRssEntries;
    Context->LimitIndex = 0; 
    Context->StartIndex = 0;
    Context->LastStartIndex = 0;
}


_Success_(return != FALSE)
BOOLEAN
RssV2GetNextCommandRange (
    _Inout_ PRSSV2_PARSING_CONTEXT Context,
    _Out_ ULONG* SwitchId,
    _Out_ ULONG* VPortId
    )
/*++
Routine Description:

    This routine finds a group of move commands in the OID, which target the
    same SwitchID & VPortID. It updates the Context to facilitate iteration
    over the group later.

Arguments:

    Context - parsing context

    SwitchId - pointer to the variable which receives SwitchId

    VPortId - pointer to the variable which receives VPortId

Return Value:

    TRUE if a new group of commands is found, FALSE otherwise.

--*/
{
    PNDIS_RSS_SET_INDIRECTION_ENTRY command;
    ULONG currIndex;
    BOOLEAN isRangePresent;
    ULONG switchId;
    ULONG vPortId;

    isRangePresent = (BOOLEAN)(Context->LimitIndex < Context->MaxIndex);

    if (isRangePresent)
    {
        Context->StartIndex = Context->LimitIndex;
        Context->LastStartIndex = Context->StartIndex;

        if (Context->IsNativeRss)
        {
            switchId = NDIS_INVALID_SWITCH_ID;
            vPortId = NDIS_INVALID_VPORT_ID;
            currIndex = Context->MaxIndex;
        }
        else
        {
            currIndex = Context->LimitIndex;
            command = RSSV2_GET_COMMAND(Context->RssV2Oid, currIndex);
            switchId = command->SwitchId;
            vPortId = command->VPortId;

            for (; currIndex < Context->MaxIndex; currIndex++)
            {
                command = RSSV2_GET_COMMAND(Context->RssV2Oid, currIndex);

                if ((command->SwitchId != switchId) ||
                    (command->VPortId != vPortId))
                {
                    break;
                }
            }
        }

        Context->LimitIndex = currIndex;
        *SwitchId = switchId;
        *VPortId = vPortId;
    }

    return isRangePresent;
}


VOID
RssV2RestartRangeIterator (
    _Inout_ PRSSV2_PARSING_CONTEXT Context
    )
/*++
Routine Description:

    This routine allows to reset the context with respect to iterations
    over the command groups.

    This is used by multi-pass OID handlers.

Arguments:

    Context - parsing context

Return Value:

    None.

--*/
{
    Context->LimitIndex = 0;
    Context->StartIndex = 0;
    Context->LastStartIndex = 0;
}




PNDIS_RSS_SET_INDIRECTION_ENTRY
RssV2GetNextCommand (
    _Inout_ PRSSV2_PARSING_CONTEXT Context,
    _In_ BOOLEAN SkipProcessedCommands
    )
/*++
Routine Description:

    This routine returns next command from the current command group (which 
    target the same SwitchId & VPortId). 

Arguments:

    Context - parsing context

    SkipProcessedCommands - TRUE, if user wants to commands which already 
                            have EntryStatus changed from NDIS_STATUS_PENDING.
                            FALSE, if user wants to iterate over all commands.

Return Value:

    Pointer to the next command, NULL - if there are no more commands left.

--*/
{
    PNDIS_RSS_SET_INDIRECTION_ENTRY command;

    do
    {
        if (Context->StartIndex < Context->LimitIndex)
        {
            command = RSSV2_GET_COMMAND(Context->RssV2Oid, 
                                        Context->StartIndex++);
        }
        else
        {
            command = NULL;
        }
    } while (SkipProcessedCommands && 
             (command != NULL) &&
             (command->EntryStatus != NDIS_STATUS_PENDING));

    return command;
}



VOID
RssV2RestartCommandIterator (
    _Inout_ PRSSV2_PARSING_CONTEXT Context
    )
/*++
Routine Description:

    This routine allows to reset the context with respect to iterations
    over the commands inside the same command group.

    This is used by multi-pass OID handlers.

Arguments:

    Context - parsing context

Return Value:

    None.

--*/
{
    Context->StartIndex = Context->LastStartIndex;
}


VOID
RssV2SetCommandRangeStatus (
    _Inout_ PRSSV2_PARSING_CONTEXT Context,
    _In_ NDIS_STATUS Status
    )
/*++
Routine Description:

    This routine sets EntryStatus for all commands of the current command group

Arguments:

    Context - parsing context

    Status - NDIS_STATUS to set

Return Value:

    None.

--*/
{
    ULONG index;
    PNDIS_RSS_SET_INDIRECTION_ENTRY command;

    for (index = Context->LastStartIndex; index < Context->LimitIndex; index++)
    {
        command = RSSV2_GET_COMMAND(Context->RssV2Oid, index);
        command->EntryStatus = Status;
    }
}


FORCEINLINE
PULONG_PTR
RssV2NQEnforcerGetBitfield (
    _In_ PRSSV2_QUEUE_MAP QueueMap,
    _In_ ULONG LocalCpuIndex
    )
/*++
Routine Description:

    Routine finds a pointer to the bitfield for specified 
    (RSS-)local processor index.

Arguments:

    QueueMap - Pointer to the queue map

    LocalCpuIndex - Local index of the processor

Return Value:

    Pointer to the bitfield.

--*/
{
    ASSERT(LocalCpuIndex < QueueMap->MaxProcessors);

    return (PULONG_PTR)((PUINT8)QueueMap + 
                        sizeof(RSSV2_QUEUE_MAP) + 
                        RSSV2_BITFIELD_OFFSET(LocalCpuIndex));
}

FORCEINLINE
PUINT8
RssV2NQEnforceGetReference (
    _In_ PRSSV2_QUEUE_MAP QueueMap,
    _In_ ULONG LocalCpuIndex
    )
/*++
Routine Description:

    Routine finds a pointer to the reference counter for the specified 
    (RSS-)local processor index.

Arguments:

    QueueMap - Pointer to the queue map

    LocalCpuIndex - Local index of the processor

Return Value:

    Pointer to the reference coutner.

--*/
{
    ASSERT(LocalCpuIndex < QueueMap->MaxProcessors);

    return (PUINT8)((PUINT8)QueueMap + 
                    sizeof(RSSV2_QUEUE_MAP) + 
                    RSSV2_BITFIELD_SIZE(QueueMap->MaxProcessors) +
                    RSSV2_REFERENCE_OFFSET(LocalCpuIndex));
}


VOID
RssV2NQEnforcerReset (
    _Inout_ PRSSV2_QUEUE_MAP QueueMap
    )
/*++
Routine Description:

    Routine resets the already initialized QueueMap.

    Queue Map has zero references by any processors.

Arguments:

    QueueMap - Pointer to the queue map

Return Value:

    None.

--*/
{
    RtlZeroMemory(RssV2NQEnforcerGetBitfield(QueueMap, 0),
                  RSSV2_BITFIELD_SIZE(QueueMap->MaxProcessors));

    RtlZeroMemory(RssV2NQEnforceGetReference(QueueMap, 0),
                  RSSV2_REFERENCE_SIZE(QueueMap->MaxProcessors));
}


VOID
RssV2NQEnforcerInitialize (
    _Inout_ PRSSV2_QUEUE_MAP QueueMap,
    _In_ ULONG MaxNumberOfProcessorsInRssTable
    )
/*++
Routine Description:

    Routine initializes the QueueMap.

Arguments:

    QueueMap - Pointer to the queue map

    MaxNumberOfProcessorsInRssTable - Maximum number of processors which 
                                      RSS table can ever contain.

Return Value:

    None.

--*/
{
    QueueMap->MaxProcessors = MaxNumberOfProcessorsInRssTable;
    KeInitializeSpinLock(&QueueMap->SpinLock);
    RssV2NQEnforcerReset(QueueMap);
}


VOID
RssV2NQEnforcerReference (
    _Inout_ PRSSV2_QUEUE_MAP QueueMap,
    _In_ ULONG LocalCpuIndex
    )
/*++
Routine Description:

    Routine marks processor as holding a reference. Corresponding bit in 
    the bitfiled is set to 1, and reference count is incremented.

Arguments:

    QueueMap - Pointer to the queue map

    LocalCpuIndex - Local index of the processor

Return Value:

    None.

--*/
{
    PUINT8 reference;
    PULONG_PTR bitfield;

    if (LocalCpuIndex < QueueMap->MaxProcessors)
    {
        reference = RssV2NQEnforceGetReference(QueueMap, LocalCpuIndex);
        *reference += 1;

        bitfield =  RssV2NQEnforcerGetBitfield(QueueMap, LocalCpuIndex);
        *bitfield |= 1UI64 << (LocalCpuIndex % BITS_PER_WORD);
    }
}


VOID
RssV2NQEnforcerDereference (
    _Inout_ PRSSV2_QUEUE_MAP QueueMap,
    _In_ ULONG LocalCpuIndex
    )
/*++
Routine Description:

    Routine removes one reference cause by the processor. Reference count
    is decremented, and if it becomes zero, a corresponding bit in 
    the bitfiled is cleared to 0.

Arguments:

    QueueMap - Pointer to the queue map

    LocalCpuIndex - Local index of the processor

Return Value:

    None.

--*/
{
    PUINT8 reference;
    PULONG_PTR bitfield;

    if (LocalCpuIndex < QueueMap->MaxProcessors)
    {
        reference = RssV2NQEnforceGetReference(QueueMap, LocalCpuIndex);
        *reference -= 1;
        if (*reference == 0)
        {
            bitfield =  RssV2NQEnforcerGetBitfield(QueueMap, LocalCpuIndex);
            *bitfield &= ~(1UI64 << (LocalCpuIndex % BITS_PER_WORD));
        }
    }
}


VOID
RssV2NQEnforcerUpdate (
    _Inout_ PRSSV2_QUEUE_MAP QueueMap,
    _In_ UINT8 OldCpuIndex, 
    _In_ UINT8 NewCpuIndex
    )
/*++
Routine Description:

    Routine updates references for steering parameter (when it is re-points
    from one processor to another).

Arguments:

    QueueMap - Pointer to the queue map

    OldCpuIndex - Old local index of the processor used by steering parameter

    NewCpuIndex - New Local index of the processor for the steering parameter

Return Value:

    None.

--*/
{
    if (OldCpuIndex != NewCpuIndex)
    {
        RssV2NQEnforcerReference(QueueMap, NewCpuIndex);
        RssV2NQEnforcerDereference(QueueMap, OldCpuIndex);
    }
}


ULONG
RssV2NQEnforcerGetNumberOfProcs (
    _In_ PRSSV2_QUEUE_MAP QueueMap
    )
/*++
Routine Description:

    Routine counts number of bits set to "1" in the bitfield, this telling
    how many processors are used.

Arguments:

    QueueMap - Pointer to the queue map


Return Value:

    Number of processors used.

--*/
{
    ULONG localCpuIndex;
    ULONG numberOfProcessors;
    PULONG_PTR bitfield;

    numberOfProcessors = 0;

    for (localCpuIndex = 0; 
         localCpuIndex < QueueMap->MaxProcessors; 
         localCpuIndex += BITS_PER_WORD)
    {
        bitfield = RssV2NQEnforcerGetBitfield(QueueMap, localCpuIndex);
        numberOfProcessors += RtlNumberOfSetBitsUlongPtr(*bitfield);
    }

    return numberOfProcessors;
}


VOID
RssV2NQEnforcerEnter(
    _Inout_ PRSSV2_QUEUE_MAP GlobalQueueMap,
    _Inout_ PRSSV2_QUEUE_MAP LocalQueueMap
    )
/*++
Routine Description:

    Routine takes a spinlock and copies Queue Map locally.

Arguments:

    GlobalQueueMap - Pointer to the global queue map (e.g. VPort's) visible by
                     many processors.

    LocalQueueMap - Pointer to the queue map on stack.

Return Value:

    None.

--*/
{
    KeAcquireSpinLockAtDpcLevel(&GlobalQueueMap->SpinLock);
    
    RtlMoveMemory(LocalQueueMap, 
                  GlobalQueueMap, 
                  RssV2NQEnforcerGetQueueMapSize(GlobalQueueMap->MaxProcessors));
}


NDIS_STATUS
RssV2NQEnforcerLeave (
    _Inout_ PRSSV2_QUEUE_MAP GlobalQueueMap,
    _In_ PRSSV2_QUEUE_MAP LocalQueueMap,
    _In_ ULONG QueueLimit
    )
/*++
Routine Description:

    Routine performs "Number of Queues" (NQ-)violation check.

    Usage pattern:

        RssV2NQEnforcerEnter(qm);

            for_all_commands_in_the_current_group()
            {
                RssV2NQEnforcerUpdate(qm, oldCpuIndex, newCpuIndex);
            }

        status = RssV2NQEnforcerLeave(qm);
        if (FAILED(status)
        {
            RssV2SetCommandRangeStatus(&context, status);
        }

        //
        // Proceed to apply to HW.
        //

Arguments:

    GlobalQueueMap - Pointer to the global queue map (e.g. VPort's) visible by
                     many processors. 

    LocalQueueMap - Pointer to the queue map on stack. If NQ-check succeeds, 
                    the global queue map will be updated from the local copy.
                    If NQ-check fails, local copy will be discarded and global
                    copy is unchanged.

    QueueLimit  - Configured maximum number of queues for scaling entity (VPort).

Return Value:

    NDIS_STATUS_SUCCESS - if the accumulated local changes lead to valid 
                         configuration.
    
    NDIS_STATUS_NO_QUEUES - if the accumulated local configuration exceeds 
                            QueueLimit.

--*/
{
    NDIS_STATUS status;

    if (RssV2NQEnforcerGetNumberOfProcs(LocalQueueMap) <= QueueLimit)
    {
        RtlMoveMemory(GlobalQueueMap, 
                      LocalQueueMap, 
                      RssV2NQEnforcerGetQueueMapSize(GlobalQueueMap->MaxProcessors));

        status = NDIS_STATUS_SUCCESS;
    }
    else
    {
        status = NDIS_STATUS_NO_QUEUES;
    }

    KeReleaseSpinLockFromDpcLevel(&GlobalQueueMap->SpinLock);

    return status;
}