net.h 11 KB

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