]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - include/net.h
d4985ca307d798172acd3d6b783576262895933c
[karo-tx-uboot.git] / include / net.h
1 /*
2  *      LiMon Monitor (LiMon) - Network.
3  *
4  *      Copyright 1994 - 2000 Neil Russell.
5  *      (See License)
6  *
7  *
8  * History
9  *      9/16/00   bor  adapted to TQM823L/STK8xxL board, RARP/TFTP boot added
10  */
11
12 #ifndef __NET_H__
13 #define __NET_H__
14
15 #if !defined(CONFIG_NET_MULTI) && defined(CONFIG_8xx)
16 #include <commproc.h>
17 #if defined(FEC_ENET) || defined(SCC_ENET)
18 #define CONFIG_NET_MULTI
19 #endif
20 #endif
21 #include <asm/byteorder.h>      /* for nton* / ntoh* stuff */
22
23
24 /*
25  *      The number of receive packet buffers, and the required packet buffer
26  *      alignment in memory.
27  *
28  */
29
30 #define PKTBUFSRX       4
31 #define PKTALIGN        32
32
33 typedef ulong           IPaddr_t;
34
35
36
37 /*
38  * The current receive packet handler.  Called with a pointer to the
39  * application packet, and a protocol type (PORT_BOOTPC or PORT_TFTP).
40  * All other packets are dealt with without calling the handler.
41  */
42 typedef void    rxhand_f(uchar *, unsigned, unsigned, unsigned);
43
44 /*
45  *      A timeout handler.  Called after time interval has expired.
46  */
47 typedef void    thand_f(void);
48
49 #ifdef CONFIG_NET_MULTI
50
51 #define NAMESIZE 16
52
53 enum eth_state_t {
54         ETH_STATE_INIT,
55         ETH_STATE_PASSIVE,
56         ETH_STATE_ACTIVE
57 };
58
59 struct eth_device {
60         char name[NAMESIZE];
61         unsigned char enetaddr[6];
62         int iobase;
63         int state;
64
65         int  (*init) (struct eth_device*, bd_t*);
66         int  (*send) (struct eth_device*, volatile void* pachet, int length);
67         int  (*recv) (struct eth_device*);
68         void (*halt) (struct eth_device*);
69
70         struct eth_device *next;
71         void *priv;
72 };
73
74 extern int eth_initialize(bd_t *bis);           /* Initialize network subsystem */
75 extern int eth_register(struct eth_device* dev);/* Register network device      */
76 extern void eth_try_another(int first_restart); /* Change the device            */
77 extern struct eth_device *eth_get_dev(void);    /* get the current device MAC   */
78 extern void eth_set_enetaddr(int num, char* a); /* Set new MAC address          */
79 #endif
80
81 extern int eth_init(bd_t *bis);                 /* Initialize the device        */
82 extern int eth_send(volatile void *packet, int length);    /* Send a packet     */
83 extern int eth_rx(void);                        /* Check for received packets   */
84 extern void eth_halt(void);                     /* stop SCC                     */
85
86
87 /**********************************************************************/
88 /*
89  *      Protocol headers.
90  */
91
92 /*
93  *      Ethernet header
94  */
95 typedef struct {
96         uchar           et_dest[6];     /* Destination node             */
97         uchar           et_src[6];      /* Source node                  */
98         ushort          et_protlen;     /* Protocol or length           */
99         uchar           et_dsap;        /* 802 DSAP                     */
100         uchar           et_ssap;        /* 802 SSAP                     */
101         uchar           et_ctl;         /* 802 control                  */
102         uchar           et_snap1;       /* SNAP                         */
103         uchar           et_snap2;
104         uchar           et_snap3;
105         ushort          et_prot;        /* 802 protocol                 */
106 } Ethernet_t;
107
108 #define ETHER_HDR_SIZE  14              /* Ethernet header size         */
109 #define E802_HDR_SIZE   22              /* 802 ethernet header size     */
110 #define PROT_IP         0x0800          /* IP protocol                  */
111 #define PROT_ARP        0x0806          /* IP ARP protocol              */
112 #define PROT_RARP       0x8035          /* IP ARP protocol              */
113
114 #define IPPROTO_ICMP     1      /* Internet Control Message Protocol    */
115 #define IPPROTO_UDP     17      /* User Datagram Protocol               */
116
117 /*
118  *      Internet Protocol (IP) header.
119  */
120 typedef struct {
121         uchar           ip_hl_v;        /* header length and version    */
122         uchar           ip_tos;         /* type of service              */
123         ushort          ip_len;         /* total length                 */
124         ushort          ip_id;          /* identification               */
125         ushort          ip_off;         /* fragment offset field        */
126         uchar           ip_ttl;         /* time to live                 */
127         uchar           ip_p;           /* protocol                     */
128         ushort          ip_sum;         /* checksum                     */
129         IPaddr_t        ip_src;         /* Source IP address            */
130         IPaddr_t        ip_dst;         /* Destination IP address       */
131         ushort          udp_src;        /* UDP source port              */
132         ushort          udp_dst;        /* UDP destination port         */
133         ushort          udp_len;        /* Length of UDP packet         */
134         ushort          udp_xsum;       /* Checksum                     */
135 } IP_t;
136
137 #define IP_HDR_SIZE_NO_UDP      (sizeof (IP_t) - 8)
138 #define IP_HDR_SIZE             (sizeof (IP_t))
139
140
141 /*
142  *      Address Resolution Protocol (ARP) header.
143  */
144 typedef struct
145 {
146         ushort          ar_hrd;         /* Format of hardware address   */
147 #   define ARP_ETHER        1           /* Ethernet  hardware address   */
148         ushort          ar_pro;         /* Format of protocol address   */
149         uchar           ar_hln;         /* Length of hardware address   */
150         uchar           ar_pln;         /* Length of protocol address   */
151         ushort          ar_op;          /* Operation                    */
152 #   define ARPOP_REQUEST    1           /* Request  to resolve  address */
153 #   define ARPOP_REPLY      2           /* Response to previous request */
154
155 #   define RARPOP_REQUEST   3           /* Request  to resolve  address */
156 #   define RARPOP_REPLY     4           /* Response to previous request */
157
158         /*
159          * The remaining fields are variable in size, according to
160          * the sizes above, and are defined as appropriate for
161          * specific hardware/protocol combinations.
162          */
163         uchar           ar_data[0];
164 #if 0
165         uchar           ar_sha[];       /* Sender hardware address      */
166         uchar           ar_spa[];       /* Sender protocol address      */
167         uchar           ar_tha[];       /* Target hardware address      */
168         uchar           ar_tpa[];       /* Target protocol address      */
169 #endif /* 0 */
170 } ARP_t;
171
172 #define ARP_HDR_SIZE    (8+20)          /* Size assuming ethernet       */
173
174 /*
175  * ICMP stuff (just enough to handle (host) redirect messages)
176  */
177 #define ICMP_REDIRECT           5       /* Redirect (change route)      */
178
179 /* Codes for REDIRECT. */
180 #define ICMP_REDIR_NET          0       /* Redirect Net                 */
181 #define ICMP_REDIR_HOST         1       /* Redirect Host                */
182
183 typedef struct icmphdr {
184         uchar           type;
185         uchar           code;
186         ushort          checksum;
187         union {
188                 struct {
189                         ushort  id;
190                         ushort  sequence;
191                 } echo;
192                 ulong   gateway;
193                 struct {
194                         ushort  __unused;
195                         ushort  mtu;
196                 } frag;
197         } un;
198 } ICMP_t;
199
200
201
202 /*
203  * Maximum packet size; used to allocate packet storage.
204  * TFTP packets can be 524 bytes + IP header + ethernet header.
205  * Lets be conservative, and go for 38 * 16.  (Must also be
206  * a multiple of 32 bytes).
207  */
208 /*
209  * AS.HARNOIS : Better to set PKTSIZE to maximum size because
210  * traffic type is not always controlled
211  * maximum packet size =  1518
212  * maximum packet size and multiple of 32 bytes =  1536
213  */
214 #define PKTSIZE                 1518
215 #define PKTSIZE_ALIGN           1536
216 /*#define PKTSIZE               608*/
217
218 /*
219  * Maximum receive ring size; that is, the number of packets
220  * we can buffer before overflow happens. Basically, this just
221  * needs to be enough to prevent a packet being discarded while
222  * we are processing the previous one.
223  */
224 #define RINGSZ          4
225 #define RINGSZ_LOG2     2
226
227 /**********************************************************************/
228 /*
229  *      Globals.
230  *
231  * Note:
232  *
233  * All variables of type IPaddr_t are stored in NETWORK byte order
234  * (big endian).
235  */
236
237 /* net.c */
238 /** BOOTP EXTENTIONS **/
239 extern IPaddr_t         NetOurGatewayIP;        /* Our gateway IP addresse      */
240 extern IPaddr_t         NetOurSubnetMask;       /* Our subnet mask (0 = unknown)*/
241 extern IPaddr_t         NetOurDNSIP;     /* Our Domain Name Server (0 = unknown)*/
242 extern char             NetOurNISDomain[32];    /* Our NIS domain               */
243 extern char             NetOurHostName[32];     /* Our hostname                 */
244 extern char             NetOurRootPath[64];     /* Our root path                */
245 extern ushort           NetBootFileSize;        /* Our boot file size in blocks */
246 /** END OF BOOTP EXTENTIONS **/
247 extern ulong            NetBootFileXferSize;    /* size of bootfile in bytes    */
248 extern uchar            NetOurEther[6];         /* Our ethernet address         */
249 extern uchar            NetServerEther[6];      /* Boot server enet address     */
250 extern IPaddr_t         NetOurIP;               /* Our    IP addr (0 = unknown) */
251 extern IPaddr_t         NetServerIP;            /* Server IP addr (0 = unknown) */
252 extern volatile uchar * NetTxPacket;            /* THE transmit packet          */
253 extern volatile uchar * NetRxPackets[PKTBUFSRX];/* Receive packets              */
254 extern volatile uchar * NetRxPkt;               /* Current receive packet       */
255 extern int              NetRxPktLen;            /* Current rx packet length     */
256 extern unsigned         NetIPID;                /* IP ID (counting)             */
257 extern uchar            NetBcastAddr[6];        /* Ethernet boardcast address   */
258
259 extern int              NetState;               /* Network loop state           */
260 #define NETLOOP_CONTINUE        1
261 #define NETLOOP_RESTART         2
262 #define NETLOOP_SUCCESS         3
263 #define NETLOOP_FAIL            4
264
265 #ifdef CONFIG_NET_MULTI
266 extern int              NetRestartWrap;         /* Tried all network devices    */
267 #endif
268
269 typedef enum { BOOTP, RARP, ARP, TFTP, DHCP } proto_t;
270
271 /* from net/net.c */
272 extern char     BootFile[128];                  /* Boot File name               */
273
274 /* Initialize the network adapter */
275 extern int      NetLoop(proto_t);
276
277 /* Shutdown adapters and cleanup */
278 extern void     NetStop(void);
279
280 /* Load failed.  Start again. */
281 extern void     NetStartAgain(void);
282
283 /* Set ethernet header */
284 extern void     NetSetEther(volatile uchar *, uchar *, uint);
285
286 /* Set IP header */
287 extern void     NetSetIP(volatile uchar *, IPaddr_t, int, int, int);
288
289 /* Checksum */
290 extern int      NetCksumOk(uchar *, int);       /* Return true if cksum OK      */
291 extern uint     NetCksum(uchar *, int);         /* Calculate the checksum       */
292
293 /* Set callbacks */
294 extern void     NetSetHandler(rxhand_f *);      /* Set RX packet handler        */
295 extern void     NetSetTimeout(int, thand_f *);  /* Set timeout handler          */
296
297 /* Transmit "NetTxPacket" */
298 extern void     NetSendPacket(volatile uchar *, int);
299
300 /* Processes a received packet */
301 extern void     NetReceive(volatile uchar *, int);
302
303 /* Print an IP address on the console */
304 extern void     print_IPaddr (IPaddr_t);
305
306 /*
307  * The following functions are a bit ugly, but necessary to deal with
308  * alignment restrictions on ARM.
309  *
310  * We're using inline functions, which had the smallest memory
311  * footprint in our tests.
312  */
313 /* return IP *in network byteorder* */
314 static inline IPaddr_t NetReadIP(void *from)
315 {
316         IPaddr_t ip;
317         memcpy((void*)&ip, from, sizeof(ip));
318         return ip;
319 }
320
321 /* return ulong *in network byteorder* */
322 static inline ulong NetReadLong(ulong *from)
323 {
324         ulong l;
325         memcpy((void*)&l, (void*)from, sizeof(l));
326         return l;
327 }
328
329 /* write IP *in network byteorder* */
330 static inline void NetWriteIP(void *to, IPaddr_t ip)
331 {
332         memcpy(to, (void*)&ip, sizeof(ip));
333 }
334
335 /* copy IP */
336 static inline void NetCopyIP(void *to, void *from)
337 {
338         memcpy(to, from, sizeof(IPaddr_t));
339 }
340
341 /* copy ulong */
342 static inline void NetCopyLong(ulong *to, ulong *from)
343 {
344         memcpy((void*)to, (void*)from, sizeof(ulong));
345 }
346
347 /* Convert an IP address to a string */
348 extern void     ip_to_string (IPaddr_t x, char *s);
349
350 /* read an IP address from a environment variable */
351 extern IPaddr_t getenv_IPaddr (char *);
352
353 /* copy a filename (allow for "..." notation, limit length) */
354 extern void     copy_filename (uchar *dst, uchar *src, int size);
355
356 /**********************************************************************/
357
358 #endif /* __NET_H__ */