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
|
////////////////////////////////////////////////////////////////////////////////
///
/// @file Ethernet.h
///
/// @project
///
/// @brief Ethernet Support Routines
///
////////////////////////////////////////////////////////////////////////////////
///
////////////////////////////////////////////////////////////////////////////////
///
/// @copyright Copyright (c) 2018, Evan Lojewski
/// @cond
///
/// All rights reserved.
///
/// Redistribution and use in source and binary forms, with or without
/// modification, are permitted provided that the following conditions are met:
/// 1. Redistributions of source code must retain the above copyright notice,
/// this list of conditions and the following disclaimer.
/// 2. Redistributions in binary form must reproduce the above copyright notice,
/// this list of conditions and the following disclaimer in the documentation
/// and/or other materials provided with the distribution.
/// 3. Neither the name of the copyright holder nor the
/// names of its contributors may be used to endorse or promote products
/// derived from this software without specific prior written permission.
///
////////////////////////////////////////////////////////////////////////////////
///
/// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
/// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
/// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
/// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
/// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
/// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
/// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
/// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
/// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
/// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
/// POSSIBILITY OF SUCH DAMAGE.
/// @endcond
////////////////////////////////////////////////////////////////////////////////
#ifndef ETHERNET_H
#define ETHERNET_H
#include <stdint.h>
#define ETHERNET_FRAME_MIN 64
typedef struct
{
#ifdef __LITTLE_ENDIAN__
uint8_t DestinationAddress[6];
uint8_t SourceAddress[6];
uint16_t reserved; // part of payload
uint16_t EtherType;
#else
#error Not tested
#endif
} EthernetHeader_t;
_Static_assert(sizeof(EthernetHeader_t) == 16, "sizeof(EthernetHeader_t) must be 16.");
#define ETHER_TYPE_NCSI 0x88f8
#define ETHERNET_HEADER_OFFSET 0
#define PACKET_OFFSET (12)
typedef struct
{
// Bytes 0 - 11 - Ethernet Header
uint8_t DestinationAddress[6];
uint8_t SourceAddress[6];
#ifdef __LITTLE_ENDIAN__
// Bytes 12 - 15
uint32_t HeaderRevision:8; /* Should be 1 */
uint32_t ManagmentControllerID:8; /* Should be 0 */
uint32_t EtherType:16; // part of Ethernet header.
// Bytes 16 - 19
uint32_t ChannelID:8;
uint32_t ControlPacketType:8;
uint32_t InstanceID:8;
uint32_t reserved_0:8;
// Bytes 20 - 23
uint32_t reserved_2:16;
uint32_t PayloadLength:12;
uint32_t reserved_1:4;
// Bytes 24 - 27
uint32_t reserved_3;
#else
#error Not tested
#endif
} ControlPacketHeader_t;
_Static_assert(sizeof(ControlPacketHeader_t) == 16 + PACKET_OFFSET, "sizeof(ControlPacketHeader_t) must be 16.");
#define CONTROL_PACKET_PAYLOAD_OFFSET (PACKET_OFFSET + 16)
#define CONTROL_PACKET_TYPE_RESPONSE (0x80)
#define CONTROL_PACKET_TYPE_CLEAR_INITIAL_STATE (0x00)
#define CONTROL_PACKET_TYPE_SELECT_PACKAGE (0x01)
#define CONTROL_PACKET_TYPE_DESELECT_PACKAGE (0x02)
#define CONTROL_PACKET_TYPE_ENABLE_CHANNEL (0x03)
#define CONTROL_PACKET_TYPE_DISABLE_CHANNEL (0x04)
#define CONTROL_PACKET_TYPE_RESET_CHANNEL (0x05)
#define CONTROL_PACKET_TYPE_ENABLE_CHANNEL_NETWORK_TX (0x06)
#define CONTROL_PACKET_TYPE_DISABLE_CHANNEL_NETWORK_TX (0x07)
#define CONTROL_PACKET_TYPE_AEN_ENABLE (0x08)
#define CONTROL_PACKET_TYPE_SET_LINK (0x09)
#define CONTROL_PACKET_TYPE_GET_LINK_STATUS (0x0A)
#define CONTROL_PACKET_TYPE_SET_VLAN_FILTER (0x0B)
#define CONTROL_PACKET_TYPE_ENABLE_VLAN (0x0C)
#define CONTROL_PACKET_TYPE_DISABLE_VLAN (0x0D)
#define CONTROL_PACKET_TYPE_SET_MAC_ADDRESS (0x0E)
// 0x0F
#define CONTROL_PACKET_TYPE_ENABLE_BROADCAST_FILTERING (0x10)
#define CONTROL_PACKET_TYPE_DISABLE_BROADCAST_FILTERING (0x11)
#define CONTROL_PACKET_TYPE_ENABLE_GLOBAL_MULTICAST_FILTERING (0x12)
#define CONTROL_PACKET_TYPE_DISABLE_GLOBAL_MULTICAST_FILTERING (0x13)
#define CONTROL_PACKET_TYPE_SET_NCSI_FLOW_CONTROL (0x14)
#define CONTROL_PACKET_TYPE_GET_VERSION_ID (0x15)
#define CONTROL_PACKET_TYPE_GET_CAPABILITIES (0x16)
#define CONTROL_PACKET_TYPE_GET_PARAMETERS (0x17)
#define CONTROL_PACKET_TYPE_GET_CONTROLLER_PACKET_STATS (0x18)
#define CONTROL_PACKET_TYPE_GET_NCSI_STATS (0x19)
#define CONTROL_PACKET_TYPE_GET_NCSI_PASSTHRU_STATS (0x1A)
#define CONTROL_PACKET_OEM_COMMAND (0x50)
typedef struct {
// Bytes 0 - 27
ControlPacketHeader_t header;
// Byte 28 - 31
uint32_t headerPadding:16;
uint32_t checksumHigh:16;
// Bytes 32 - 35
uint32_t checksumLow;
uint32_t pad:16;
} ClearInitialState_t;
typedef struct {
// Bytes 0 - 27
ControlPacketHeader_t header;
// Byte 28 - 31
uint32_t HardwareArbitartionDisabled:8;
uint32_t reserved_0:8;
uint32_t headerPadding:16;
// Bytes 32 - 35
uint32_t checksumLow;
uint32_t pad:16;
} SelectPackage_t;
typedef struct {
// Bytes 0 - 27
ControlPacketHeader_t header;
// Byte 28 - 31
uint32_t AEN_MC_ID:8;
uint32_t reserved_0:8;
uint32_t headerPadding:16;
// Bytes 32 - 35
uint32_t AENControl_High:16;
uint32_t reserved_1:16;
// Bytes 36 - 39
uint32_t pad:16;
uint32_t AENControl_Low:16;
} AENEnable_t;
typedef struct
{
// Bytes 0 - 11
uint8_t DestinationAddress[6];
uint8_t SourceAddress[6];
#ifdef __LITTLE_ENDIAN__
// Bytes 12 - 15
uint32_t HeaderRevision:8; /* Should be 1 */
uint32_t ManagmentControllerID:8; /* Should be 0 */
uint32_t EtherType:16; // part of Ethernet header.
// Bytes 16 - 19
uint32_t ChannelID:8;
uint32_t ControlPacketType:8;
uint32_t InstanceID:8;
uint32_t reserved_0:8;
// Bytes 20 - 23
uint32_t reserved_2:16;
uint32_t PayloadLength:12;
uint32_t reserved_1:4;
// Bytes 24 - 27
uint32_t reserved_3;
// Bytes 28 - 31
uint16_t ResponseCode;
uint16_t reserved_4;
// Bytes 32 - 35
uint16_t reserved_5;
uint16_t ReasonCode;
#else
#error Not tested
#endif
} ResponsePacketHeader_t;
// typedef union {
// struct {
// EthernetHeader_t header;
// } header;
// struct {
// uint8_t padding[CONTROL_PACKET_OFFSET];
// ControlPacketHeader_t header;
// } controlPacket;
// struct {
// uint8_t padding[CONTROL_PACKET_PAYLOAD_OFFSET];
// uint32_t checksumHigh:16;
// uint32_t reserved:16;
// uint32_t pad:16
// uint32_t checksumLow:16;
// } selectPackage
// } SelectPackage_t;
typedef union {
// Ethernet frame must be at least 64 bytes.
uint32_t words[ETHERNET_FRAME_MIN/4];
EthernetHeader_t header;
/* Control Packets */
ControlPacketHeader_t controlPacket;
ClearInitialState_t clearInitialState;
SelectPackage_t selectPackage;
AENEnable_t AENEnable;
/* Response Packets */
ResponsePacketHeader_t responsePacket;
} NetworkFrame_t;
#endif /* ETHERNET_H */
|