forked from coolstar/wificx-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.cpp
138 lines (101 loc) · 3.49 KB
/
adapter.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
#include "precomp.h"
#include <netadaptercx.h>
#include "trace.h"
#include "device.h"
#include "adapter.h"
NTSTATUS
MtkInitializeAdapterContext(
_In_ MTK_ADAPTER* adapter,
_In_ WDFDEVICE device,
_In_ NETADAPTER netAdapter
)
/*++
Routine Description:
Allocate MTK_ADAPTER data block and do some initialization
Arguments:
adapter Pointer to receive pointer to our adapter
Return Value:
NTSTATUS failure code, or STATUS_SUCCESS
--*/
{
TraceEntry();
NTSTATUS status = STATUS_SUCCESS;
adapter->NetAdapter = netAdapter;
adapter->WdfDevice = device;
//
// Get WDF miniport device context.
//
MtkGetDeviceContext(adapter->WdfDevice)->Adapter = adapter;
//spinlock
WDF_OBJECT_ATTRIBUTES attributes;
WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
attributes.ParentObject = adapter->WdfDevice;
GOTO_IF_NOT_NT_SUCCESS(Exit, status,
WdfSpinLockCreate(&attributes, &adapter->Lock));
Exit:
TraceExitResult(status);
return status;
}
static
void
MtkAdapterSetDatapathCapabilities(
_In_ MTK_ADAPTER const* adapter
)
{
NET_ADAPTER_DMA_CAPABILITIES txDmaCapabilities;
NET_ADAPTER_DMA_CAPABILITIES_INIT(&txDmaCapabilities, adapter->DmaEnabler);
NET_ADAPTER_TX_CAPABILITIES txCapabilities;
NET_ADAPTER_TX_CAPABILITIES_INIT_FOR_DMA(
&txCapabilities,
&txDmaCapabilities,
1);
txCapabilities.FragmentRingNumberOfElementsHint = MTK_MIN_TCB * MTK_MAX_PHYS_BUF_COUNT;
txCapabilities.MaximumNumberOfFragments = MTK_MAX_PHYS_BUF_COUNT;
NET_ADAPTER_DMA_CAPABILITIES rxDmaCapabilities;
NET_ADAPTER_DMA_CAPABILITIES_INIT(&rxDmaCapabilities, adapter->DmaEnabler);
NET_ADAPTER_RX_CAPABILITIES rxCapabilities;
NET_ADAPTER_RX_CAPABILITIES_INIT_SYSTEM_MANAGED_DMA(
&rxCapabilities,
&rxDmaCapabilities,
MTK_MAX_PACKET_SIZE + FRAME_CRC_SIZE + RSVD_BUF_SIZE,
1);
rxCapabilities.FragmentBufferAlignment = 64;
rxCapabilities.FragmentRingNumberOfElementsHint = 32;
NetAdapterSetDataPathCapabilities(adapter->NetAdapter, &txCapabilities, &rxCapabilities);
}
_Use_decl_annotations_
NTSTATUS
MtkAdapterStart(
MTK_ADAPTER* adapter
)
{
TraceEntryNetAdapter(adapter->NetAdapter);
NTSTATUS status = STATUS_SUCCESS;
{
//barebones init
ULONG64 maxXmitLinkSpeed = MTK_MEDIA_MAX_SPEED;
ULONG64 maxRcvLinkSpeed = MTK_MEDIA_MAX_SPEED;
NET_ADAPTER_LINK_LAYER_CAPABILITIES linkLayerCapabilities;
NET_ADAPTER_LINK_LAYER_CAPABILITIES_INIT(
&linkLayerCapabilities,
maxXmitLinkSpeed,
maxRcvLinkSpeed);
NetAdapterSetLinkLayerCapabilities(adapter->NetAdapter, &linkLayerCapabilities);
NetAdapterSetLinkLayerMtuSize(adapter->NetAdapter, MTK_MAX_PACKET_SIZE - ETH_LENGTH_OF_HEADER);
for (ULONG i = 0; i < ETHERNET_ADDRESS_LENGTH; i++)
{
adapter->PermanentAddress.Address[i] = i;
}
adapter->PermanentAddress.Length = ETHERNET_ADDRESS_LENGTH;
RtlCopyMemory(&adapter->CurrentAddress, &adapter->PermanentAddress, sizeof(adapter->PermanentAddress));
NetAdapterSetPermanentLinkLayerAddress(adapter->NetAdapter, &adapter->PermanentAddress);
NetAdapterSetCurrentLinkLayerAddress(adapter->NetAdapter, &adapter->CurrentAddress);
}
MtkAdapterSetDatapathCapabilities(adapter);
GOTO_IF_NOT_NT_SUCCESS(
Exit, status,
NetAdapterStart(adapter->NetAdapter));
Exit:
TraceExitResult(status);
return status;
}