-
Notifications
You must be signed in to change notification settings - Fork 7
/
device.cpp
294 lines (240 loc) · 7.97 KB
/
device.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
#include "precomp.h"
#include <initguid.h>
#include <wdmguid.h>
#include "trace.h"
#include "device.h"
#include "adapter.h"
#include "configuration.h"
#include "link.h"
NTSTATUS
RtGetResources(
_In_ RT_ADAPTER* adapter,
_In_ WDFCMRESLIST resourcesRaw,
_In_ WDFCMRESLIST resourcesTranslated)
{
TraceEntry();
NTSTATUS status = STATUS_SUCCESS;
ULONG errorCode = 0;
ULONG errorValue = 0;
ULONG memRegCnt = 0;
// According to https://msdn.microsoft.com/windows/hardware/drivers/wdf/raw-and-translated-resources
// "Both versions represent the same set of hardware resources, in the same order."
ULONG rawCount = WdfCmResourceListGetCount(resourcesRaw);
NT_ASSERT(rawCount == WdfCmResourceListGetCount(resourcesTranslated));
for (ULONG i = 0; i < rawCount; i++)
{
PCM_PARTIAL_RESOURCE_DESCRIPTOR rawDescriptor = WdfCmResourceListGetDescriptor(resourcesRaw, i);
PCM_PARTIAL_RESOURCE_DESCRIPTOR translatedDescriptor = WdfCmResourceListGetDescriptor(resourcesTranslated, i);
if (rawDescriptor->Type == CmResourceTypeMemory)
{
// RTL8125 has 2 memory IO regions, first region is MAC regs, second region is MSI-X
if (memRegCnt == 0)
{
adapter->MMIOAddress = MmMapIoSpaceEx(
translatedDescriptor->u.Memory.Start,
translatedDescriptor->u.Memory.Length,
PAGE_READWRITE | PAGE_NOCACHE);
adapter->MMIOSize = translatedDescriptor->u.Memory.Length;
}
memRegCnt++;
}
}
if (!adapter->MMIOAddress)
{
status = STATUS_RESOURCE_TYPE_NOT_FOUND;
errorCode = NDIS_ERROR_CODE_RESOURCE_CONFLICT;
errorValue = ERRLOG_NO_MEMORY_RESOURCE;
GOTO_IF_NOT_NT_SUCCESS(Exit, status, STATUS_NDIS_RESOURCE_CONFLICT);
}
status = WdfFdoQueryForInterface(adapter->WdfDevice,
&GUID_BUS_INTERFACE_STANDARD,
(PINTERFACE)&adapter->PciConfig,
sizeof(BUS_INTERFACE_STANDARD),
1, // Version
NULL); //InterfaceSpecificData
if (!NT_SUCCESS(status))
goto Exit;
Exit:
TraceExitResult(status);
return status;
}
NTSTATUS
RtRegisterScatterGatherDma(
_In_ RT_ADAPTER* adapter)
{
TraceEntryRtAdapter(adapter);
WDF_DMA_ENABLER_CONFIG dmaEnablerConfig;
WDF_DMA_ENABLER_CONFIG_INIT(&dmaEnablerConfig, WdfDmaProfileScatterGather64, adapter->bsdData.max_jumbo_frame_size);
dmaEnablerConfig.Flags |= WDF_DMA_ENABLER_CONFIG_REQUIRE_SINGLE_TRANSFER;
dmaEnablerConfig.WdmDmaVersionOverride = 3;
NTSTATUS status = STATUS_SUCCESS;
GOTO_IF_NOT_NT_SUCCESS(Exit, status,
WdfDmaEnablerCreate(
adapter->WdfDevice,
&dmaEnablerConfig,
WDF_NO_OBJECT_ATTRIBUTES,
&adapter->DmaEnabler),
TraceLoggingRtAdapter(adapter));
Exit:
TraceExitResult(status);
return status;
}
void
RtReleaseHardware(
_In_ RT_ADAPTER* adapter)
{
if (adapter->MMIOAddress)
{
MmUnmapIoSpace(
adapter->MMIOAddress,
adapter->MMIOSize);
adapter->MMIOAddress = NULL;
adapter->MMIOSize = 0;
}
}
NTSTATUS
RtInitializeHardware(
_In_ RT_ADAPTER* adapter,
_In_ WDFCMRESLIST resourcesRaw,
_In_ WDFCMRESLIST resourcesTranslated)
{
TraceEntryRtAdapter(adapter);
//
// Read the registry parameters
//
NTSTATUS status = STATUS_SUCCESS;
re_softc* sc = &adapter->bsdData;
GOTO_IF_NOT_NT_SUCCESS(Exit, status,
RtAdapterReadConfiguration(adapter));
GOTO_IF_NOT_NT_SUCCESS(Exit, status,
RtGetResources(adapter, resourcesRaw, resourcesTranslated));
RtlZeroMemory(&adapter->bsdData, sizeof(adapter->bsdData));
adapter->bsdData.dev = adapter;
adapter->bsdData.if_net.if_mtu = ETHERMTU;
adapter->bsdData.eee_enable = adapter->EEEEnable ? 1 : 0;
UINT16 devID = ConfigRead16(adapter, 2);
adapter->bsdData.re_device_id = devID;
GOTO_IF_NOT_NT_SUCCESS(Exit, status,
BSD_NT_WRAP(re_check_mac_version(&adapter->bsdData)),
TraceLoggingRtAdapter(adapter));
re_init_software_variable(&adapter->bsdData);
if ((sc->re_type == MACFG_24) || (sc->re_type == MACFG_25) || (sc->re_type == MACFG_26))
sc->if_net.if_hwassist |= CSUM_TCP | CSUM_UDP;
else
sc->if_net.if_hwassist |= RE_CSUM_FEATURES;
sc->if_net.if_capenable = IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6;
/* TSO capability setup */
if (sc->re_if_flags & RL_FLAG_8168G_PLUS) {
sc->if_net.if_hwassist |= CSUM_TSO;
sc->if_net.if_capenable |= IFCAP_TSO;
}
/* RTL8169/RTL8101E/RTL8168B not support TSO v6 */
if (!(sc->re_if_flags & RL_FLAG_DESCV2)) {
sc->if_net.if_hwassist &= ~(CSUM_IP6_TSO |
CSUM_TCP_IPV6 |
CSUM_UDP_IPV6);
sc->if_net.if_capenable &= ~(IFCAP_TSO6 | IFCAP_HWCSUM_IPV6);
}
sc->if_net.if_capenable |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
adapter->bsdData.re_rx_cstag = 1;
adapter->bsdData.re_tx_cstag = 1;
GOTO_IF_NOT_NT_SUCCESS(Exit, status,
RtRegisterScatterGatherDma(adapter),
TraceLoggingRtAdapter(adapter));
re_exit_oob(&adapter->bsdData);
re_hw_init(&adapter->bsdData);
/*
* Reset the adapter. Don't need to lock during init
*/
re_reset(&adapter->bsdData);
if (adapter->bsdData.re_type == MACFG_3) { /* Change PCI Latency time */
ConfigWrite8(adapter, RE_PCI_LATENCY_TIMER, 0x40);
}
re_get_hw_mac_address(&adapter->bsdData, adapter->PermanentAddress.Address);
adapter->PermanentAddress.Length = ETHERNET_LENGTH_OF_ADDRESS;
if (adapter->OverrideAddress) {
RtlCopyMemory(&adapter->CurrentAddress, &adapter->PermanentAddress, sizeof(adapter->PermanentAddress));
}
re_phy_power_up(sc);
re_hw_phy_config(sc);
switch (adapter->bsdData.re_device_id) {
case RT_DEVICEID_8126:
adapter->MaxSpeed = 5'000'000'000;
break;
case RT_DEVICEID_8125:
case RT_DEVICEID_3000:
adapter->MaxSpeed = 2'500'000'000;
break;
case RT_DEVICEID_8169:
case RT_DEVICEID_8169SC:
case RT_DEVICEID_8168:
case RT_DEVICEID_8161:
case RT_DEVICEID_8162:
adapter->MaxSpeed = 1'000'000'000;
break;
default:
adapter->MaxSpeed = 100'000'000;
break;
}
switch (sc->re_type) {
case MACFG_80:
case MACFG_81:
case MACFG_82:
case MACFG_83:
case MACFG_90:
case MACFG_91:
case MACFG_92:
adapter->isRTL8125 = TRUE;
break;
default:
adapter->isRTL8125 = FALSE;
break;
}
{
//Temp for barebones init
NET_ADAPTER_LINK_STATE linkState;
NET_ADAPTER_LINK_STATE_INIT(
&linkState,
NDIS_LINK_SPEED_UNKNOWN,
MediaConnectStateDisconnected,
MediaDuplexStateUnknown,
NetAdapterPauseFunctionTypeUnknown,
NetAdapterAutoNegotiationFlagNone);
NetAdapterSetLinkState(adapter->NetAdapter, &linkState);
}
GOTO_IF_NOT_NT_SUCCESS(Exit, status,
RtAdapterStart(adapter));
RtlFirstStart(adapter);
Exit:
TraceExitResult(status);
return status;
}
_Use_decl_annotations_
NTSTATUS
EvtDevicePrepareHardware(
_In_ WDFDEVICE device,
_In_ WDFCMRESLIST resourcesRaw,
_In_ WDFCMRESLIST resourcesTranslated)
{
RT_ADAPTER* adapter = RtGetDeviceContext(device)->Adapter;
TraceEntryRtAdapter(adapter);
NTSTATUS status = STATUS_SUCCESS;
GOTO_IF_NOT_NT_SUCCESS(Exit, status, RtInitializeHardware(adapter, resourcesRaw, resourcesTranslated));
Exit:
TraceExitResult(status);
return status;
}
_Use_decl_annotations_
NTSTATUS
EvtDeviceReleaseHardware(
_In_ WDFDEVICE device,
_In_ WDFCMRESLIST resourcesTranslated)
{
UNREFERENCED_PARAMETER(resourcesTranslated);
RT_ADAPTER* adapter = RtGetDeviceContext(device)->Adapter;
TraceEntryRtAdapter(adapter);
RtReleaseHardware(adapter);
NTSTATUS status = STATUS_SUCCESS;
TraceExitResult(status);
return status;
}