nic.h
Go to the documentation of this file.
1 /**
2  * @file nic.h
3  * @brief Network interface controller abstraction layer
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2019 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 1.9.6
29  **/
30 
31 #ifndef _NIC_H
32 #define _NIC_H
33 
34 //Dependencies
35 #include "core/net.h"
36 
37 //Tick interval to handle NIC periodic operations
38 #ifndef NIC_TICK_INTERVAL
39  #define NIC_TICK_INTERVAL 1000
40 #elif (NIC_TICK_INTERVAL < 10)
41  #error NIC_TICK_INTERVAL parameter is not valid
42 #endif
43 
44 //Maximum duration a write operation may block
45 #ifndef NIC_MAX_BLOCKING_TIME
46  #define NIC_MAX_BLOCKING_TIME INFINITE_DELAY
47 #elif (NIC_MAX_BLOCKING_TIME < 0)
48  #error NIC_MAX_BLOCKING_TIME parameter is not valid
49 #endif
50 
51 //Size of the NIC driver context
52 #ifndef NIC_CONTEXT_SIZE
53  #define NIC_CONTEXT_SIZE 16
54 #elif (NIC_CONTEXT_SIZE < 1)
55  #error NIC_CONTEXT_SIZE parameter is not valid
56 #endif
57 
58 //Serial Management Interface
59 #define SMI_SYNC 0xFFFFFFFF
60 #define SMI_START 1
61 #define SMI_OPCODE_0 0
62 #define SMI_OPCODE_WRITE 1
63 #define SMI_OPCODE_READ 2
64 #define SMI_TA 2
65 
66 //C++ guard
67 #ifdef __cplusplus
68 extern "C" {
69 #endif
70 
71 
72 /**
73  * @brief NIC types
74  **/
75 
76 typedef enum
77 {
78  NIC_TYPE_UNKNOWN = 0, ///<Unknown interface type
79  NIC_TYPE_ETHERNET = 1, ///<Ethernet interface
80  NIC_TYPE_PPP = 2, ///<PPP interface
81  NIC_TYPE_6LOWPAN = 3, ///<6LoWPAN interface
82  NIC_TYPE_LOOPBACK = 4 ///<Loopback interface
83 } NicType;
84 
85 
86 /**
87  * @brief Link state
88  **/
89 
90 typedef enum
91 {
95 } NicLinkState;
96 
97 
98 /**
99  * @brief Link speed
100  **/
101 
102 typedef enum
103 {
107  NIC_LINK_SPEED_1GBPS = 1000000000
108 } NicLinkSpeed;
109 
110 
111 /**
112  * @brief Duplex mode
113  **/
114 
115 typedef enum
116 {
120 } NicDuplexMode;
121 
122 
123 //NIC abstraction layer
124 typedef error_t (*NicInit)(NetInterface *interface);
125 typedef void (*NicTick)(NetInterface *interface);
126 typedef void (*NicEnableIrq)(NetInterface *interface);
127 typedef void (*NicDisableIrq)(NetInterface *interface);
128 typedef void (*NicEventHandler)(NetInterface *interface);
129 
130 typedef error_t (*NicSendPacket)(NetInterface *interface,
131  const NetBuffer *buffer, size_t offset);
132 
134 typedef error_t (*NicUpdateMacConfig)(NetInterface *interface);
135 
136 typedef void (*NicWritePhyReg)(uint8_t opcode, uint8_t phyAddr,
137  uint8_t regAddr, uint16_t data);
138 
139 typedef uint16_t (*NicReadPhyReg)(uint8_t opcode, uint8_t phyAddr,
140  uint8_t regAddr);
141 
142 //PHY abstraction layer
143 typedef error_t (*PhyInit)(NetInterface *interface);
144 typedef void (*PhyTick)(NetInterface *interface);
145 typedef void (*PhyEnableIrq)(NetInterface *interface);
146 typedef void (*PhyDisableIrq)(NetInterface *interface);
147 typedef void (*PhyEventHandler)(NetInterface *interface);
148 
149 typedef error_t (*PhyTagFrame)(NetInterface *interface, NetBuffer *buffer,
150  size_t *offset, uint8_t port, uint16_t *type);
151 
152 typedef error_t (*PhyUntagFrame)(NetInterface *interface, uint8_t **frame,
153  size_t *length, uint8_t *port);
154 
155 //SPI abstraction layer
156 typedef error_t (*SpiInit)(void);
158 typedef error_t (*SpiSetBitrate)(uint_t bitrate);
159 typedef void (*SpiAssertCs)(void);
160 typedef void (*SpiDeassertCs)(void);
161 typedef uint8_t (*SpiTransfer)(uint8_t data);
162 
163 //UART abstraction layer
164 typedef error_t (*UartInit)(void);
165 typedef void (*UartEnableIrq)(void);
166 typedef void (*UartDisableIrq)(void);
167 typedef void (*UartStartTx)(void);
168 
169 //External interrupt line abstraction layer
170 typedef error_t (*ExtIntInit)(void);
171 typedef void (*ExtIntEnableIrq)(void);
172 typedef void (*ExtIntDisableIrq)(void);
173 
174 
175 /**
176  * @brief NIC driver
177  **/
178 
179 typedef struct
180 {
182  size_t mtu;
197  //bool_t autoIpv4ChecksumCalc;
198  //bool_t autoIpv4ChecksumVerif;
199  //bool_t autoIpv6ChecksumCalc;
200  //bool_t autoIpv6ChecksumVerif;
201  //bool_t autoIcmpChecksumCalc;
202  //bool_t autoIcmpChecksumVerif;
203  //bool_t autoTcpChecksumCalc;
204  //bool_t autoTcpChecksumVerif;
205  //bool_t autoUdpChecksumCalc;
206  //bool_t autoUdpChecksumVerif;
207 } NicDriver;
208 
209 
210 /**
211  * @brief PHY driver
212  **/
213 
214 typedef struct
215 {
223 } PhyDriver;
224 
225 
226 /**
227  * @brief SPI driver
228  **/
229 
230 typedef struct
231 {
238 } SpiDriver;
239 
240 
241 /**
242  * @brief UART driver
243  **/
244 
245 typedef struct
246 {
251 } UartDriver;
252 
253 
254 /**
255  * @brief External interrupt line driver
256  **/
257 
258 typedef struct
259 {
263 } ExtIntDriver;
264 
265 
266 //Tick counter to handle periodic operations
268 
269 //NIC abstraction layer
272 uint8_t nicGetSwitchPort(NetInterface *interface);
273 uint16_t nicGetVlanId(NetInterface *interface);
274 uint16_t nicGetVmanId(NetInterface *interface);
275 
277 
278 void nicTick(NetInterface *interface);
279 
280 error_t nicSendPacket(NetInterface *interface, const NetBuffer *buffer,
281  size_t offset);
282 
284 void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length);
285 void nicNotifyLinkChange(NetInterface *interface);
286 
287 //C++ guard
288 #ifdef __cplusplus
289 }
290 #endif
291 
292 #endif
error_t(* NicSendPacket)(NetInterface *interface, const NetBuffer *buffer, size_t offset)
Definition: nic.h:130
uint8_t(* SpiTransfer)(uint8_t data)
Definition: nic.h:161
@ NIC_LINK_SPEED_1GBPS
Definition: nic.h:107
void nicTick(NetInterface *interface)
Network controller timer handler.
Definition: nic.c:250
uint8_t length
Definition: dtls_misc.h:149
ExtIntEnableIrq enableIrq
Definition: nic.h:261
uint8_t opcode
Definition: dns_common.h:172
void(* UartDisableIrq)(void)
Definition: nic.h:166
int bool_t
Definition: compiler_port.h:49
@ NIC_LINK_SPEED_UNKNOWN
Definition: nic.h:104
size_t mtu
Definition: nic.h:182
NicWritePhyReg writePhyReg
Definition: nic.h:191
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:119
NicType type
Definition: nic.h:181
NicEnableIrq enableIrq
Definition: nic.h:185
SpiAssertCs assertCs
Definition: nic.h:235
ExtIntInit init
Definition: nic.h:260
NicDisableIrq disableIrq
Definition: nic.h:186
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:88
NicUpdateMacConfig updateMacConfig
Definition: nic.h:190
PHY driver.
Definition: nic.h:214
UART driver.
Definition: nic.h:245
error_t nicSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset)
Send a packet to the network controller.
Definition: nic.c:278
External interrupt line driver.
Definition: nic.h:258
error_t(* SpiSetBitrate)(uint_t bitrate)
Definition: nic.h:158
void(* PhyDisableIrq)(NetInterface *interface)
Definition: nic.h:146
uint16_t(* NicReadPhyReg)(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Definition: nic.h:139
error_t(* NicInit)(NetInterface *interface)
Definition: nic.h:124
void(* UartStartTx)(void)
Definition: nic.h:167
PhyInit init
Definition: nic.h:216
SPI driver.
Definition: nic.h:230
void(* PhyTick)(NetInterface *interface)
Definition: nic.h:144
bool_t autoCrcStrip
Definition: nic.h:196
void(* PhyEnableIrq)(NetInterface *interface)
Definition: nic.h:145
NicInit init
Definition: nic.h:183
error_t(* NicUpdateMacConfig)(NetInterface *interface)
Definition: nic.h:134
@ NIC_LINK_STATE_UP
Definition: nic.h:93
void(* SpiDeassertCs)(void)
Definition: nic.h:160
UartDisableIrq disableIrq
Definition: nic.h:249
NetInterface * nicGetLogicalInterface(NetInterface *interface)
Retrieve logical interface.
Definition: nic.c:52
@ NIC_TYPE_LOOPBACK
Loopback interface.
Definition: nic.h:82
PhyUntagFrame untagFrame
Definition: nic.h:222
systime_t nicTickCounter
Definition: nic.c:43
void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length)
Handle a packet received by the network controller.
Definition: nic.c:383
void(* NicEventHandler)(NetInterface *interface)
Definition: nic.h:128
error_t(* PhyTagFrame)(NetInterface *interface, NetBuffer *buffer, size_t *offset, uint8_t port, uint16_t *type)
Definition: nic.h:149
SpiDeassertCs deassertCs
Definition: nic.h:236
char_t type
NicLinkSpeed
Link speed.
Definition: nic.h:102
error_t
Error codes.
Definition: error.h:42
SpiTransfer transfer
Definition: nic.h:237
NicType
NIC types.
Definition: nic.h:76
PhyDisableIrq disableIrq
Definition: nic.h:219
NicLinkState
Link state.
Definition: nic.h:90
#define NetInterface
Definition: net.h:36
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:105
NicTick tick
Definition: nic.h:184
NicSendPacket sendPacket
Definition: nic.h:188
void(* ExtIntEnableIrq)(void)
Definition: nic.h:171
SpiSetBitrate setBitrate
Definition: nic.h:234
NicReadPhyReg readPhyReg
Definition: nic.h:192
error_t(* UartInit)(void)
Definition: nic.h:164
@ NIC_TYPE_UNKNOWN
Unknown interface type.
Definition: nic.h:78
void(* NicDisableIrq)(NetInterface *interface)
Definition: nic.h:127
NicEventHandler eventHandler
Definition: nic.h:187
PhyEventHandler eventHandler
Definition: nic.h:220
error_t nicUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
Definition: nic.c:346
error_t(* ExtIntInit)(void)
Definition: nic.h:170
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:525
bool_t autoCrcCalc
Definition: nic.h:194
@ NIC_LINK_STATE_DOWN
Definition: nic.h:92
UartInit init
Definition: nic.h:247
uint16_t port
Definition: dns_common.h:223
UartStartTx startTx
Definition: nic.h:250
@ NIC_TYPE_PPP
PPP interface.
Definition: nic.h:80
SpiInit init
Definition: nic.h:232
void(* NicWritePhyReg)(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Definition: nic.h:136
error_t(* SpiInit)(void)
Definition: nic.h:156
uint16_t regAddr
bool_t autoPadding
Definition: nic.h:193
uint16_t nicGetVmanId(NetInterface *interface)
Retrieve VMAN identifier.
Definition: nic.c:175
void(* NicEnableIrq)(NetInterface *interface)
Definition: nic.h:126
void(* ExtIntDisableIrq)(void)
Definition: nic.h:172
void(* PhyEventHandler)(NetInterface *interface)
Definition: nic.h:147
bool_t nicIsParentInterface(NetInterface *interface, NetInterface *parent)
Test parent/child relationship between 2 interfaces.
Definition: nic.c:207
NicDuplexMode
Duplex mode.
Definition: nic.h:115
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:118
NicUpdateMacAddrFilter updateMacAddrFilter
Definition: nic.h:189
bool_t autoCrcVerif
Definition: nic.h:195
NetInterface * nicGetPhysicalInterface(NetInterface *interface)
Retrieve physical interface.
Definition: nic.c:84
UartEnableIrq enableIrq
Definition: nic.h:248
void(* SpiAssertCs)(void)
Definition: nic.h:159
@ NIC_UNKNOWN_DUPLEX_MODE
Definition: nic.h:117
PhyTick tick
Definition: nic.h:217
uint8_t mode
Definition: ntp_common.h:149
error_t(* PhyUntagFrame)(NetInterface *interface, uint8_t **frame, size_t *length, uint8_t *port)
Definition: nic.h:152
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:106
SpiSetMode setMode
Definition: nic.h:233
void(* UartEnableIrq)(void)
Definition: nic.h:165
ExtIntDisableIrq disableIrq
Definition: nic.h:262
PhyEnableIrq enableIrq
Definition: nic.h:218
uint16_t nicGetVlanId(NetInterface *interface)
Retrieve VLAN identifier.
Definition: nic.c:144
@ NIC_TYPE_6LOWPAN
6LoWPAN interface
Definition: nic.h:81
unsigned int uint_t
Definition: compiler_port.h:45
TCP/IP stack core.
uint8_t data[]
Definition: dtls_misc.h:176
@ NIC_LINK_STATE_AUTO
Definition: nic.h:94
NIC driver.
Definition: nic.h:179
void(* NicTick)(NetInterface *interface)
Definition: nic.h:125
uint8_t nicGetSwitchPort(NetInterface *interface)
Retrieve switch port identifier.
Definition: nic.c:113
error_t(* PhyInit)(NetInterface *interface)
Definition: nic.h:143
error_t(* SpiSetMode)(uint_t mode)
Definition: nic.h:157
uint32_t systime_t
Definition: compiler_port.h:46
PhyTagFrame tagFrame
Definition: nic.h:221
error_t(* NicUpdateMacAddrFilter)(NetInterface *interface)
Definition: nic.h:133
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition: nic.h:79