net.h 11 KB

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