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
|
/*++
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:
NIC_RECV.C
Abstract:
This module contains miniport receive routines
Environment:
Kernel mode
--*/
#include "precomp.h"
#if defined(EVENT_TRACING)
#include "nic_recv.tmh"
#endif
_IRQL_requires_same_
_IRQL_requires_(DISPATCH_LEVEL)
_Requires_lock_held_(FdoData->RcvLock)
VOID
NICHandleRecvInterrupt(
IN PFDO_DATA FdoData
)
/*++
Routine Description:
Interrupt handler for receive processing. Put the received packets
into an array and call NICServiceReadIrps. If we run low on
RFDs, allocate another one.
Assumption: This function is called with the Rcv SPINLOCK held.
Arguments:
FdoData Pointer to our FdoData
Return Value:
None
--*/
{
PMP_RFD pMpRfd = NULL;
PHW_RFD pHwRfd = NULL;
PMP_RFD PacketArray[NIC_DEF_RFDS];
PMP_RFD PacketFreeArray[NIC_DEF_RFDS];
UINT PacketArrayCount;
UINT PacketFreeCount;
UINT Index;
UINT LoopIndex = 0;
UINT LoopCount = NIC_MAX_RFDS / NIC_DEF_RFDS + 1; // avoid staying here too long
BOOLEAN bContinue = TRUE;
BOOLEAN bAllocNewRfd = FALSE;
USHORT PacketStatus;
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_READ, "---> NICHandleRecvInterrupt\n");
ASSERT(FdoData->nReadyRecv >= NIC_MIN_RFDS);
while (LoopIndex++ < LoopCount && bContinue)
{
PacketArrayCount = 0;
PacketFreeCount = 0;
//
// Process up to the array size RFD's
//
while (PacketArrayCount < NIC_DEF_RFDS)
{
if (IsListEmpty(&FdoData->RecvList))
{
ASSERT(FdoData->nReadyRecv == 0);
bContinue = FALSE;
break;
}
//
// Get the next MP_RFD to process
//
pMpRfd = (PMP_RFD)GetListHeadEntry(&FdoData->RecvList);
//
// Get the associated HW_RFD
//
pHwRfd = pMpRfd->HwRfd;
//
// Is this packet completed?
//
PacketStatus = NIC_RFD_GET_STATUS(pHwRfd);
if (!NIC_RFD_STATUS_COMPLETED(PacketStatus))
{
bContinue = FALSE;
break;
}
//
// HW specific - check if actual count field has been updated
//
if (!NIC_RFD_VALID_ACTUALCOUNT(pHwRfd))
{
bContinue = FALSE;
break;
}
//
// Remove the RFD from the head of the List
//
RemoveEntryList((PLIST_ENTRY)pMpRfd);
FdoData->nReadyRecv--;
ASSERT(MP_TEST_FLAG(pMpRfd, fMP_RFD_RECV_READY));
MP_CLEAR_FLAG(pMpRfd, fMP_RFD_RECV_READY);
//
// A good packet? drop it if not.
//
if (!NIC_RFD_STATUS_SUCCESS(PacketStatus))
{
TraceEvents(TRACE_LEVEL_WARNING, DBG_READ,
"Receive failure = %x\n", PacketStatus);
NICReturnRFD(FdoData, pMpRfd);
continue;
}
//
// Do not receive any packets until a filter has been set
//
if (!FdoData->PacketFilter)
{
NICReturnRFD(FdoData, pMpRfd);
continue;
}
//
// Do not receive any packets until we are at D0
//
if (FdoData->DevicePowerState != PowerDeviceD0)
{
NICReturnRFD(FdoData, pMpRfd);
continue;
}
pMpRfd->PacketSize = NIC_RFD_GET_PACKET_SIZE(pHwRfd);
KeFlushIoBuffers(pMpRfd->Mdl, TRUE, TRUE);
//
// set the status on the packet, either resources or success
//
if (FdoData->nReadyRecv >= MIN_NUM_RFD)
{
MP_SET_FLAG(pMpRfd, fMP_RFD_RECV_PEND);
}
else
{
MP_SET_FLAG(pMpRfd, fMP_RFD_RESOURCES);
_Analysis_assume_(PacketFreeCount <= PacketArrayCount);
PacketFreeArray[PacketFreeCount] = pMpRfd;
PacketFreeCount++;
//
// Reset the RFD shrink count - don't attempt to shrink RFD
//
FdoData->RfdShrinkCount = 0;
//
// Remember to allocate a new RFD later
//
bAllocNewRfd = TRUE;
}
PacketArray[PacketArrayCount] = pMpRfd;
PacketArrayCount++;
}
//
// if we didn't process any receives, just return from here
//
if (PacketArrayCount == 0)
{
break;
}
WdfSpinLockRelease(FdoData->RcvLock);
WdfSpinLockAcquire(FdoData->Lock);
//
// if we have a Recv interrupt and have reported a media disconnect status
// time to indicate the new status
//
if (Disconnected == FdoData->MediaState)
{
TraceEvents(TRACE_LEVEL_WARNING, DBG_READ, "Media state changed to Connected\n");
MP_CLEAR_FLAG(FdoData, fMP_ADAPTER_NO_CABLE);
FdoData->MediaState = Connected;
WdfSpinLockRelease(FdoData->Lock);
//
// Indicate the media event
//
NICServiceIndicateStatusIrp(FdoData);
}
else
{
WdfSpinLockRelease(FdoData->Lock);
}
NICServiceReadIrps(
FdoData,
PacketArray,
PacketArrayCount);
WdfSpinLockAcquire(FdoData->RcvLock);
//
// Return all the RFDs to the pool.
//
for (Index = 0; Index < PacketFreeCount; Index++)
{
//
// Get the MP_RFD saved in this packet, in NICAllocRfd
//
pMpRfd = PacketFreeArray[Index];
ASSERT(MP_TEST_FLAG(pMpRfd, fMP_RFD_RESOURCES));
MP_CLEAR_FLAG(pMpRfd, fMP_RFD_RESOURCES);
NICReturnRFD(FdoData, pMpRfd);
}
}
//
// If we ran low on RFD's, we need to allocate a new RFD
//
if (bAllocNewRfd)
{
//
// Allocate one more RFD only if it doesn't exceed the max RFD limit
//
if (FdoData->CurrNumRfd < FdoData->MaxNumRfd
&& !FdoData->AllocNewRfd)
{
NTSTATUS status;
FdoData->AllocNewRfd = TRUE;
//
// Since we are running at DISPATCH_LEVEL, we will queue a workitem
// to allocate RFD memory at PASSIVE_LEVEL. Note that
// AllocateCommonBuffer and FreeCommonBuffer can be called only at
// PASSIVE_LEVEL.
//
status = PciDrvQueuePassiveLevelCallback(FdoData,
NICAllocRfdWorkItem,
NULL, NULL);
if(!NT_SUCCESS(status)){
FdoData->AllocNewRfd = FALSE;
}
}
}
ASSERT(FdoData->nReadyRecv >= NIC_MIN_RFDS);
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_READ, "<--- NICHandleRecvInterrupt\n");
}
VOID
NICReturnRFD(
IN PFDO_DATA FdoData,
IN PMP_RFD pMpRfd
)
/*++
Routine Description:
Recycle a RFD and put it back onto the receive list
Assumption: This function is called with the Rcv SPINLOCK held.
Arguments:
FdoData Pointer to our FdoData
pMpRfd Pointer to the RFD
Return Value:
None
--*/
{
PMP_RFD pLastMpRfd;
PHW_RFD pHwRfd = pMpRfd->HwRfd;
ASSERT(pMpRfd->Flags == 0);
MP_SET_FLAG(pMpRfd, fMP_RFD_RECV_READY);
//
// HW_SPECIFIC_START
//
pHwRfd->RfdCbHeader.CbStatus = 0;
pHwRfd->RfdActualCount = 0;
pHwRfd->RfdCbHeader.CbCommand = (RFD_EL_BIT);
pHwRfd->RfdCbHeader.CbLinkPointer = DRIVER_NULL;
//
// Append this RFD to the RFD chain
if (!IsListEmpty(&FdoData->RecvList))
{
pLastMpRfd = (PMP_RFD)GetListTailEntry(&FdoData->RecvList);
// Link it onto the end of the chain dynamically
pHwRfd = pLastMpRfd->HwRfd;
pHwRfd->RfdCbHeader.CbLinkPointer = pMpRfd->HwRfdPhys;
pHwRfd->RfdCbHeader.CbCommand = 0;
}
//
// HW_SPECIFIC_END
//
//
// The processing on this RFD is done, so put it back on the tail of
// our list
//
InsertTailList(&FdoData->RecvList, (PLIST_ENTRY)pMpRfd);
FdoData->nReadyRecv++;
ASSERT(FdoData->nReadyRecv <= FdoData->CurrNumRfd);
}
_Requires_lock_held_(FdoData->RcvLock)
NTSTATUS
NICStartRecv(
IN PFDO_DATA FdoData
)
/*++
Routine Description:
Start the receive unit if it's not in a ready state
Assumption: This function is called with the Rcv SPINLOCK held.
Arguments:
FdoData Pointer to our FdoData
Return Value:
NT Status code
--*/
{
PMP_RFD pMpRfd;
NTSTATUS status;
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_READ, "---> NICStartRecv\n");
//
// If the receiver is ready, then don't try to restart.
//
if (NIC_IS_RECV_READY(FdoData))
{
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_READ, "Receive unit already active\n");
return STATUS_SUCCESS;
}
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_READ, "Re-start receive unit...\n");
ASSERT(!IsListEmpty(&FdoData->RecvList));
//
// Get the MP_RFD head
//
pMpRfd = (PMP_RFD)GetListHeadEntry(&FdoData->RecvList);
//
// If more packets are received, clean up RFD chain again
//
if (NIC_RFD_GET_STATUS(pMpRfd->HwRfd))
{
NICHandleRecvInterrupt(FdoData);
ASSERT(!IsListEmpty(&FdoData->RecvList));
//
// Get the new MP_RFD head
//
pMpRfd = (PMP_RFD)GetListHeadEntry(&FdoData->RecvList);
}
//
// Wait for the SCB to clear before we set the general pointer
//
if (!WaitScb(FdoData))
{
status = STATUS_DEVICE_DATA_ERROR;
goto exit;
}
if (FdoData->DevicePowerState > PowerDeviceD0)
{
status = STATUS_DEVICE_DATA_ERROR;
goto exit;
}
//
// Set the SCB General Pointer to point the current Rfd
//
FdoData->CSRAddress->ScbGeneralPointer = pMpRfd->HwRfdPhys;
//
// Issue the SCB RU start command
//
status = D100IssueScbCommand(FdoData, SCB_RUC_START, FALSE);
if (status == STATUS_SUCCESS)
{
// wait for the command to be accepted
if (!WaitScb(FdoData))
{
status = STATUS_DEVICE_DATA_ERROR;
}
}
exit:
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_READ, "<--- NICStartRecv, Status=%x\n", status);
return status;
}
VOID
NICResetRecv(
IN PFDO_DATA FdoData
)
/*++
Routine Description:
Reset the receive list
Assumption: This function is called with the Rcv SPINLOCK held.
Arguments:
FdoData Pointer to our FdoData
Return Value:
None
--*/
{
PMP_RFD pMpRfd;
PHW_RFD pHwRfd;
ULONG RfdCount;
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_READ, "--> NICResetRecv\n");
ASSERT(!IsListEmpty(&FdoData->RecvList));
//
// Get the MP_RFD head
//
pMpRfd = (PMP_RFD)GetListHeadEntry(&FdoData->RecvList);
for (RfdCount = 0; RfdCount < FdoData->nReadyRecv; RfdCount++)
{
pHwRfd = pMpRfd->HwRfd;
pHwRfd->RfdCbHeader.CbStatus = 0;
pMpRfd = (PMP_RFD)GetListFLink(&pMpRfd->List);
}
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_READ, "<-- NICResetRecv\n");
}
VOID
NICServiceReadIrps(
PFDO_DATA FdoData,
PMP_RFD *PacketArray,
ULONG PacketArrayCount
)
/*++
Routine Description:
Copy the data from the recv buffers to pending read IRP buffers
and complete the IRP. When used as network driver, copy operation
can be avoided by devising a private interface between us and the
NDIS-WDM filter and have the NDIS-WDM edge to indicate our buffers
directly to NDIS.
Called at DISPATCH_LEVEL. Take advantage of that fact while
acquiring spinlocks.
Arguments:
FdoData Pointer to our FdoData
Return Value:
None
--*/
{
PMP_RFD pMpRfd = NULL;
ULONG index;
NTSTATUS status;
PVOID buffer;
WDFREQUEST request;
size_t bufLength=0;
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_READ, "--> NICServiceReadIrps\n");
for(index=0; index < PacketArrayCount; index++)
{
pMpRfd = PacketArray[index];
ASSERT(pMpRfd);
status = WdfIoQueueRetrieveNextRequest( FdoData->PendingReadQueue,
&request );
if(NT_SUCCESS(status)){
WDF_REQUEST_PARAMETERS params;
ULONG length = 0;
WDF_REQUEST_PARAMETERS_INIT(¶ms);
WdfRequestGetParameters(
request,
¶ms
);
ASSERT(status == STATUS_SUCCESS);
bufLength = params.Parameters.Read.Length;
status = WdfRequestRetrieveOutputBuffer(request,
bufLength,
&buffer,
&bufLength);
if(NT_SUCCESS(status) ) {
length = min((ULONG)bufLength, pMpRfd->PacketSize);
RtlCopyMemory(buffer, pMpRfd->Buffer, length);
Hexdump((TRACE_LEVEL_VERBOSE, DBG_READ,
"Received Packet Data: %!HEXDUMP!\n",
log_xstr(buffer, (USHORT)length)));
FdoData->BytesReceived += length;
}
WdfRequestCompleteWithInformation(request, status, length);
}else {
ASSERTMSG("WdfIoQueueRetrieveNextRequest failed",
(status == STATUS_NO_MORE_ENTRIES ||
status == STATUS_WDF_PAUSED));
}
WdfSpinLockAcquire(FdoData->RcvLock);
ASSERT(MP_TEST_FLAG(pMpRfd, fMP_RFD_RECV_PEND));
MP_CLEAR_FLAG(pMpRfd, fMP_RFD_RECV_PEND);
if (FdoData->RfdShrinkCount < NIC_RFD_SHRINK_THRESHOLD)
{
NICReturnRFD(FdoData, pMpRfd);
}
else
{
ASSERT(FdoData->CurrNumRfd > FdoData->NumRfd);
status = PciDrvQueuePassiveLevelCallback(FdoData,
NICFreeRfdWorkItem, (PVOID)pMpRfd,
NULL);
if(NT_SUCCESS(status)){
FdoData->RfdShrinkCount = 0;
FdoData->CurrNumRfd--;
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_READ, "Shrink... CurrNumRfd = %d\n",
FdoData->CurrNumRfd);
} else {
//
// We couldn't queue a workitem to free memory, so let us
// put that back in the main pool and try again next time.
//
NICReturnRFD(FdoData, pMpRfd);
}
}
WdfSpinLockRelease(FdoData->RcvLock);
}// end of loop
TraceEvents(TRACE_LEVEL_VERBOSE, DBG_READ, "<-- NICServiceReadIrps\n");
return;
}
|