-
Notifications
You must be signed in to change notification settings - Fork 7
/
rxqueue.cpp
536 lines (423 loc) · 14.1 KB
/
rxqueue.cpp
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
#include "precomp.h"
#include "device.h"
#include "trace.h"
#include "adapter.h"
#include "rxqueue.h"
#include "interrupt.h"
#include "link.h"
_Use_decl_annotations_
NTSTATUS
EvtAdapterCreateRxQueue(
_In_ NETADAPTER netAdapter,
_Inout_ NETRXQUEUE_INIT* rxQueueInit
)
{
NTSTATUS status = STATUS_SUCCESS;
TraceEntryNetAdapter(netAdapter);
RT_ADAPTER* adapter = RtGetAdapterContext(netAdapter);
WDF_OBJECT_ATTRIBUTES rxAttributes;
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&rxAttributes, RT_RXQUEUE);
rxAttributes.EvtDestroyCallback = EvtRxQueueDestroy;
NET_PACKET_QUEUE_CONFIG rxConfig;
NET_PACKET_QUEUE_CONFIG_INIT(
&rxConfig,
EvtRxQueueAdvance,
EvtRxQueueSetNotificationEnabled,
EvtRxQueueCancel);
rxConfig.EvtStart = EvtRxQueueStart;
rxConfig.EvtStop = EvtRxQueueStop;
const ULONG queueId = NetRxQueueInitGetQueueId(rxQueueInit);
NETPACKETQUEUE rxQueue;
GOTO_IF_NOT_NT_SUCCESS(Exit, status,
NetRxQueueCreate(rxQueueInit, &rxAttributes, &rxConfig, &rxQueue));
RT_RXQUEUE* rx = RtGetRxQueueContext(rxQueue);
NET_EXTENSION_QUERY extension;
NET_EXTENSION_QUERY_INIT(
&extension,
NET_PACKET_EXTENSION_CHECKSUM_NAME,
NET_PACKET_EXTENSION_CHECKSUM_VERSION_1,
NetExtensionTypePacket);
rx->QueueId = queueId;
NetRxQueueGetExtension(rxQueue, &extension, &rx->ChecksumExtension);
NET_EXTENSION_QUERY_INIT(
&extension,
NET_FRAGMENT_EXTENSION_VIRTUAL_ADDRESS_NAME,
NET_FRAGMENT_EXTENSION_VIRTUAL_ADDRESS_VERSION_1,
NetExtensionTypeFragment);
NetRxQueueGetExtension(rxQueue, &extension, &rx->VirtualAddressExtension);
NET_EXTENSION_QUERY_INIT(
&extension,
NET_PACKET_EXTENSION_IEEE8021Q_NAME,
NET_PACKET_EXTENSION_IEEE8021Q_VERSION_1,
NetExtensionTypePacket);
NetRxQueueGetExtension(rxQueue, &extension, &rx->Ieee8021qExtension);
NET_EXTENSION_QUERY_INIT(
&extension,
NET_FRAGMENT_EXTENSION_LOGICAL_ADDRESS_NAME,
NET_FRAGMENT_EXTENSION_LOGICAL_ADDRESS_VERSION_1,
NetExtensionTypeFragment);
NetRxQueueGetExtension(rxQueue, &extension, &rx->LogicalAddressExtension);
NET_EXTENSION_QUERY_INIT(
&extension,
NET_PACKET_EXTENSION_HASH_NAME,
NET_PACKET_EXTENSION_HASH_VERSION_1,
NetExtensionTypePacket);
NetRxQueueGetExtension(rxQueue, &extension, &rx->HashValueExtension);
GOTO_IF_NOT_NT_SUCCESS(Exit, status,
RtRxQueueInitialize(rxQueue, adapter));
Exit:
TraceExitResult(status);
return status;
}
static
void
RtFillRxChecksumInfo(
_In_ RT_RXQUEUE const* rx,
_In_ RxDesc const* rxd,
_In_ UINT32 packetIndex,
_Inout_ NET_PACKET* packet
)
{
const RT_ADAPTER* adapter = rx->Adapter;
const struct re_softc* sc = &adapter->bsdData;
NET_PACKET_CHECKSUM* checksumInfo =
NetExtensionGetPacketChecksum(
&rx->ChecksumExtension,
packetIndex);
#define opts1 rxd->ul[0]
#define opts2 rxd->ul[1]
checksumInfo->Layer2 =
(opts1 & RL_CRC)
? NetPacketRxChecksumEvaluationInvalid
: NetPacketRxChecksumEvaluationValid;
if ((sc->re_if_flags & RL_FLAG_DESCV2) == 0) {
UINT32 proto = opts1 & RL_ProtoMASK;
if (proto != 0) {
packet->Layout.Layer3Type = NetPacketLayer3TypeIPv4UnspecifiedOptions;
if (adapter->RxIpHwChkSum) {
checksumInfo->Layer3 = (opts1 & RL_IPF) ? NetPacketRxChecksumEvaluationInvalid :
NetPacketRxChecksumEvaluationValid;
}
}
else {
return;
}
if (proto == RL_ProtoTCP) {
packet->Layout.Layer4Type = NetPacketLayer4TypeTcp;
if (adapter->TxTcpHwChkSum) {
checksumInfo->Layer4 = (opts1 & RL_TCPF) ? NetPacketRxChecksumEvaluationInvalid :
NetPacketRxChecksumEvaluationValid;
}
}
else if (proto == RL_ProtoUDP) {
packet->Layout.Layer4Type = NetPacketLayer4TypeUdp;
if (adapter->RxUdpHwChkSum) {
checksumInfo->Layer4 = (opts1 & RL_UDPF) ? NetPacketRxChecksumEvaluationInvalid :
NetPacketRxChecksumEvaluationValid;
}
}
}
else {
/*
* RTL8168C/RTL816CP/RTL8111C/RTL8111CP
*/
if (opts2 & RL_V4F) {
packet->Layout.Layer3Type = NetPacketLayer3TypeIPv4UnspecifiedOptions;
if (adapter->RxIpHwChkSum) {
checksumInfo->Layer3 = (opts1 & RL_IPF) ? NetPacketRxChecksumEvaluationInvalid :
NetPacketRxChecksumEvaluationValid;
}
}
else if (opts2 & RL_V6F) {
packet->Layout.Layer3Type = NetPacketLayer3TypeIPv6UnspecifiedExtensions;
}
else {
return;
}
if (opts1 & RL_TCPT) {
packet->Layout.Layer4Type = NetPacketLayer4TypeTcp;
if (adapter->TxTcpHwChkSum) {
checksumInfo->Layer4 = (opts1 & RL_TCPF) ? NetPacketRxChecksumEvaluationInvalid :
NetPacketRxChecksumEvaluationValid;
}
}
else if (opts1 & RL_UDPT) {
packet->Layout.Layer4Type = NetPacketLayer4TypeUdp;
if (adapter->RxUdpHwChkSum) {
checksumInfo->Layer4 = (opts1 & RL_UDPF) ? NetPacketRxChecksumEvaluationInvalid :
NetPacketRxChecksumEvaluationValid;
}
}
}
}
static UINT32 GetRxIndexFromFragment(
_In_ RT_RXQUEUE* rx,
_In_ UINT32 fragmentIdx
) {
return (fragmentIdx + rx->NumRxDesc - rx->DescStartIdx) % rx->NumRxDesc;
}
void
RxIndicateReceives(
_In_ RT_RXQUEUE* rx
)
{
NET_RING* pr = NetRingCollectionGetPacketRing(rx->Rings);
NET_RING* fr = NetRingCollectionGetFragmentRing(rx->Rings);
while (fr->BeginIndex != fr->NextIndex)
{
UINT32 const fragmentIndex = fr->BeginIndex;
//Calculate Desc Index from fragment
UINT32 RxDescIdx = GetRxIndexFromFragment(rx, fragmentIndex);
RxDesc const* rxd = &rx->RxdBase[RxDescIdx];
if (0 != rxd->so0.OWN)
break;
if (pr->BeginIndex == pr->EndIndex)
break;
// If there is a packet available we are guaranteed to have a fragment as well
NT_FRE_ASSERT(fr->BeginIndex != fr->EndIndex);
NET_FRAGMENT* fragment = NetRingGetFragmentAtIndex(fr, fragmentIndex);
fragment->ValidLength = (rxd->so0.Frame_Length - ETHER_CRC_LEN);
fragment->Capacity = fragment->ValidLength;
fragment->Offset = 0;
// Link fragment and packet
UINT32 const packetIndex = pr->BeginIndex;
NET_PACKET* packet = NetRingGetPacketAtIndex(pr, packetIndex);
packet->FragmentIndex = fragmentIndex;
packet->FragmentCount = 1;
#define opts1 rxd->ul[0]
#define opts2 rxd->ul[1]
if (rx->Ieee8021qExtension.Enabled)
{
NET_PACKET_IEEE8021Q* ieee8021q = NetExtensionGetPacketIeee8021Q(&rx->Ieee8021qExtension, packetIndex);
if (opts2 & RL_RDESC_VLANCTL_TAG) {
RT_TAG_802_1Q tag8021q = { 0 };
tag8021q.Value = (opts2 & RL_RDESC_VLANCTL_DATA);
ieee8021q->PriorityCodePoint = tag8021q.TagHeader.Priority;
ieee8021q->VlanIdentifier = (tag8021q.TagHeader.VLanID1 << 8) | tag8021q.TagHeader.VLanID2;
}
}
if (rx->ChecksumExtension.Enabled)
{
RtFillRxChecksumInfo(rx, rxd, packetIndex, packet);
}
//TODO
/*if (rx->HashValueExtension.Enabled && rx->Adapter->RssEnabled)
{
// fill packet hash value
RtFillReceiveScalingInfo(rx, rxd, packetIndex);
}*/
packet->Layout.Layer2Type = NetPacketLayer2TypeEthernet;
pr->BeginIndex = NetRingIncrementIndex(pr, pr->BeginIndex);
fr->BeginIndex = NetRingIncrementIndex(fr, fr->BeginIndex);
}
}
static
void
RtPostRxDescriptor(
_In_ RxDesc* desc,
_In_ NET_FRAGMENT_LOGICAL_ADDRESS const* logicalAddress,
_In_ BOOLEAN ringEnd
)
{
RtlZeroMemory(desc, sizeof(*desc));
desc->so0.RxBuff = logicalAddress->LogicalAddress;
desc->so0.OWN = 1;
if (ringEnd)
desc->so0.EOR = 1;
desc->so0.Frame_Length = RE_BUF_SIZE;
MemoryBarrier();
}
void
RxSlideBuffers(
_In_ RT_RXQUEUE* rx
)
{
NET_RING* fr = NetRingCollectionGetFragmentRing(rx->Rings);
rx->DescStartIdx = fr->BeginIndex;
UINT32 index = 0;
do {
UINT32 RxDescIdx = GetRxIndexFromFragment(rx, index);
RtPostRxDescriptor(&rx->RxdBase[RxDescIdx],
NetExtensionGetFragmentLogicalAddress(&rx->LogicalAddressExtension, index),
RxDescIdx == (rx->NumRxDesc - 1u));
index = NetRingIncrementIndex(fr, index);
} while (index != 0);
MemoryBarrier();
}
void
RxPostBuffers(
_In_ RT_RXQUEUE* rx
)
{
NET_RING* fr = NetRingCollectionGetFragmentRing(rx->Rings);
while (fr->NextIndex != fr->EndIndex)
{
UINT32 const index = fr->NextIndex;
UINT32 RxDescIdx = GetRxIndexFromFragment(rx, index);
RtPostRxDescriptor(&rx->RxdBase[RxDescIdx],
NetExtensionGetFragmentLogicalAddress(&rx->LogicalAddressExtension, index),
RxDescIdx == (rx->NumRxDesc - 1u));
fr->NextIndex = NetRingIncrementIndex(fr, fr->NextIndex);
}
}
NTSTATUS
RtRxQueueInitialize(
_In_ NETPACKETQUEUE rxQueue,
_In_ RT_ADAPTER* adapter
)
{
NTSTATUS status = STATUS_SUCCESS;
RT_RXQUEUE* rx = RtGetRxQueueContext(rxQueue);
rx->Adapter = adapter;
rx->Interrupt = adapter->Interrupt;
rx->Rings = NetRxQueueGetRingCollection(rxQueue);
// allocate descriptors
NET_RING* pr = NetRingCollectionGetPacketRing(rx->Rings);
NET_RING* fr = NetRingCollectionGetFragmentRing(rx->Rings);
rx->NumRxDesc = (USHORT)(fr->NumberOfElements > USHORT_MAX ? USHORT_MAX : fr->NumberOfElements);
ULONG rxSize;
GOTO_IF_NOT_NT_SUCCESS(Exit, status,
RtlULongMult(rx->NumRxDesc, sizeof(RxDesc), &rxSize));
UINT32 const rxdSize = pr->NumberOfElements * sizeof(RxDesc);
GOTO_IF_NOT_NT_SUCCESS(Exit, status,
WdfCommonBufferCreate(
rx->Adapter->DmaEnabler,
rxdSize,
WDF_NO_OBJECT_ATTRIBUTES,
&rx->RxdArray));
rx->RxdBase = static_cast<RxDesc*>(WdfCommonBufferGetAlignedVirtualAddress(rx->RxdArray));
rx->RxdSize = rxdSize;
Exit:
return status;
}
void
RtRxQueueSetInterrupt(
_In_ RT_RXQUEUE* rx,
_In_ BOOLEAN notificationEnabled
)
{
InterlockedExchange(&rx->Interrupt->RxNotifyArmed[rx->QueueId], notificationEnabled);
if (!notificationEnabled)
// block this thread until we're sure any outstanding DPCs are complete.
// This is to guarantee we don't return from this function call until
// any oustanding rx notification is complete.
KeFlushQueuedDpcs();
}
_Use_decl_annotations_
void
EvtRxQueueStart(
NETPACKETQUEUE rxQueue
)
{
RT_RXQUEUE* rx = RtGetRxQueueContext(rxQueue);
RT_ADAPTER* adapter = rx->Adapter;
re_softc* sc = &adapter->bsdData;
RtlZeroMemory(rx->RxdBase, rx->RxdSize);
rx->DescStartIdx = 0;
PHYSICAL_ADDRESS pa = WdfCommonBufferGetAlignedLogicalAddress(rx->RxdArray);
if (rx->QueueId == 0)
{
CSR_WRITE_4(sc, RE_RXADDR0, pa.LowPart);
CSR_WRITE_4(sc, RE_RXADDR1, pa.HighPart);
}
else
{
//GigaMacSetReceiveDescriptorStartAddress(adapter, rx->QueueId, pa);
}
WdfSpinLockAcquire(adapter->Lock);
/*if (!(adapter->CSRAddress->CmdReg & CR_RE))
{
adapter->CSRAddress->CmdReg |= CR_RE;
}*/
adapter->RxQueues[rx->QueueId] = rxQueue;
//RtAdapterUpdateRcr(adapter);
WdfSpinLockRelease(adapter->Lock);
RtlFirstStart(adapter);
}
_Use_decl_annotations_
void
EvtRxQueueStop(
NETPACKETQUEUE rxQueue
)
{
RT_RXQUEUE* rx = RtGetRxQueueContext(rxQueue);
WdfSpinLockAcquire(rx->Adapter->Lock);
size_t count = 0;
for (size_t i = 0; i < ARRAYSIZE(rx->Adapter->RxQueues); i++)
{
if (rx->Adapter->RxQueues[i])
{
count++;
}
}
if (1 == count)
{
//rx->Adapter->CSRAddress->CmdReg &= ~CR_RE;
}
RtRxQueueSetInterrupt(rx, false);
rx->Adapter->RxQueues[rx->QueueId] = WDF_NO_HANDLE;
WdfSpinLockRelease(rx->Adapter->Lock);
}
_Use_decl_annotations_
void
EvtRxQueueDestroy(
_In_ WDFOBJECT rxQueue
)
{
TraceEntry(TraceLoggingPointer(rxQueue, "RxQueue"));
RT_RXQUEUE* rx = RtGetRxQueueContext(rxQueue);
if (rx->RxdArray)
WdfObjectDelete(rx->RxdArray);
rx->RxdArray = NULL;
TraceExit();
}
_Use_decl_annotations_
VOID
EvtRxQueueSetNotificationEnabled(
_In_ NETPACKETQUEUE rxQueue,
_In_ BOOLEAN notificationEnabled
)
{
TraceEntry(TraceLoggingPointer(rxQueue), TraceLoggingBoolean(notificationEnabled));
RT_RXQUEUE* rx = RtGetRxQueueContext(rxQueue);
RtRxQueueSetInterrupt(rx, notificationEnabled);
TraceExit();
}
_Use_decl_annotations_
void
EvtRxQueueAdvance(
_In_ NETPACKETQUEUE rxQueue
)
{
TraceEntry(TraceLoggingPointer(rxQueue, "RxQueue"));
RT_RXQUEUE* rx = RtGetRxQueueContext(rxQueue);
RxIndicateReceives(rx);
RxPostBuffers(rx);
TraceExit();
}
_Use_decl_annotations_
void
EvtRxQueueCancel(
_In_ NETPACKETQUEUE rxQueue
)
{
TraceEntry(TraceLoggingPointer(rxQueue, "RxQueue"));
RT_RXQUEUE* rx = RtGetRxQueueContext(rxQueue);
WdfSpinLockAcquire(rx->Adapter->Lock);
re_stop(&rx->Adapter->bsdData);
WdfSpinLockRelease(rx->Adapter->Lock);
// try (but not very hard) to grab anything that may have been
// indicated during rx disable. advance will continue to be called
// after cancel until all packets are returned to the framework.
RxIndicateReceives(rx);
NET_RING* pr = NetRingCollectionGetPacketRing(rx->Rings);
while (pr->BeginIndex != pr->EndIndex)
{
NET_PACKET* packet = NetRingGetPacketAtIndex(pr, pr->BeginIndex);
packet->Ignore = 1;
pr->BeginIndex = NetRingIncrementIndex(pr, pr->BeginIndex);
}
NET_RING* fr = NetRingCollectionGetFragmentRing(rx->Rings);
fr->BeginIndex = fr->EndIndex;
TraceExit();
}