You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. #ifndef _GPXE_NETDEVICE_H
  2. #define _GPXE_NETDEVICE_H
  3. /** @file
  4. *
  5. * Network device management
  6. *
  7. */
  8. #include <stdint.h>
  9. #include <gpxe/list.h>
  10. #include <gpxe/tables.h>
  11. #include <gpxe/refcnt.h>
  12. #include <gpxe/settings.h>
  13. struct io_buffer;
  14. struct net_device;
  15. struct net_protocol;
  16. struct ll_protocol;
  17. struct device;
  18. /** Maximum length of a link-layer address */
  19. #define MAX_LL_ADDR_LEN 20
  20. /** Maximum length of a link-layer header */
  21. #define MAX_LL_HEADER_LEN 6
  22. /** Maximum length of a network-layer address */
  23. #define MAX_NET_ADDR_LEN 4
  24. /**
  25. * A network-layer protocol
  26. *
  27. */
  28. struct net_protocol {
  29. /** Protocol name */
  30. const char *name;
  31. /**
  32. * Process received packet
  33. *
  34. * @v iobuf I/O buffer
  35. * @v netdev Network device
  36. * @v ll_source Link-layer source address
  37. *
  38. * This method takes ownership of the I/O buffer.
  39. */
  40. int ( * rx ) ( struct io_buffer *iobuf, struct net_device *netdev,
  41. const void *ll_source );
  42. /**
  43. * Transcribe network-layer address
  44. *
  45. * @v net_addr Network-layer address
  46. * @ret string Human-readable transcription of address
  47. *
  48. * This method should convert the network-layer address into a
  49. * human-readable format (e.g. dotted quad notation for IPv4).
  50. *
  51. * The buffer used to hold the transcription is statically
  52. * allocated.
  53. */
  54. const char * ( *ntoa ) ( const void * net_addr );
  55. /** Network-layer protocol
  56. *
  57. * This is an ETH_P_XXX constant, in network-byte order
  58. */
  59. uint16_t net_proto;
  60. /** Network-layer address length */
  61. uint8_t net_addr_len;
  62. };
  63. /**
  64. * A link-layer protocol
  65. *
  66. */
  67. struct ll_protocol {
  68. /** Protocol name */
  69. const char *name;
  70. /**
  71. * Add link-layer header
  72. *
  73. * @v iobuf I/O buffer
  74. * @v ll_dest Link-layer destination address
  75. * @v ll_source Source link-layer address
  76. * @v net_proto Network-layer protocol, in network-byte order
  77. * @ret rc Return status code
  78. */
  79. int ( * push ) ( struct io_buffer *iobuf, const void *ll_dest,
  80. const void *ll_source, uint16_t net_proto );
  81. /**
  82. * Remove link-layer header
  83. *
  84. * @v iobuf I/O buffer
  85. * @ret ll_dest Link-layer destination address
  86. * @ret ll_source Source link-layer address
  87. * @ret net_proto Network-layer protocol, in network-byte order
  88. * @ret rc Return status code
  89. */
  90. int ( * pull ) ( struct io_buffer *iobuf, const void **ll_dest,
  91. const void **ll_source, uint16_t *net_proto );
  92. /**
  93. * Transcribe link-layer address
  94. *
  95. * @v ll_addr Link-layer address
  96. * @ret string Human-readable transcription of address
  97. *
  98. * This method should convert the link-layer address into a
  99. * human-readable format.
  100. *
  101. * The buffer used to hold the transcription is statically
  102. * allocated.
  103. */
  104. const char * ( * ntoa ) ( const void * ll_addr );
  105. /**
  106. * Hash multicast address
  107. *
  108. * @v af Address family
  109. * @v net_addr Network-layer address
  110. * @v ll_addr Link-layer address to fill in
  111. * @ret rc Return status code
  112. */
  113. int ( * mc_hash ) ( unsigned int af, const void *net_addr,
  114. void *ll_addr );
  115. /** Link-layer protocol
  116. *
  117. * This is an ARPHRD_XXX constant, in network byte order.
  118. */
  119. uint16_t ll_proto;
  120. /** Link-layer address length */
  121. uint8_t ll_addr_len;
  122. /** Link-layer header length */
  123. uint8_t ll_header_len;
  124. /** Link-layer broadcast address */
  125. const uint8_t *ll_broadcast;
  126. };
  127. /** Network device operations */
  128. struct net_device_operations {
  129. /** Open network device
  130. *
  131. * @v netdev Network device
  132. * @ret rc Return status code
  133. *
  134. * This method should allocate RX I/O buffers and enable
  135. * the hardware to start transmitting and receiving packets.
  136. */
  137. int ( * open ) ( struct net_device *netdev );
  138. /** Close network device
  139. *
  140. * @v netdev Network device
  141. *
  142. * This method should stop the flow of packets, and free up
  143. * any packets that are currently in the device's TX queue.
  144. */
  145. void ( * close ) ( struct net_device *netdev );
  146. /** Transmit packet
  147. *
  148. * @v netdev Network device
  149. * @v iobuf I/O buffer
  150. * @ret rc Return status code
  151. *
  152. * This method should cause the hardware to initiate
  153. * transmission of the I/O buffer.
  154. *
  155. * If this method returns success, the I/O buffer remains
  156. * owned by the net device's TX queue, and the net device must
  157. * eventually call netdev_tx_complete() to free the buffer.
  158. * If this method returns failure, the I/O buffer is
  159. * immediately released; the failure is interpreted as
  160. * "failure to enqueue buffer".
  161. *
  162. * This method is guaranteed to be called only when the device
  163. * is open.
  164. */
  165. int ( * transmit ) ( struct net_device *netdev,
  166. struct io_buffer *iobuf );
  167. /** Poll for completed and received packets
  168. *
  169. * @v netdev Network device
  170. *
  171. * This method should cause the hardware to check for
  172. * completed transmissions and received packets. Any received
  173. * packets should be delivered via netdev_rx().
  174. *
  175. * This method is guaranteed to be called only when the device
  176. * is open.
  177. */
  178. void ( * poll ) ( struct net_device *netdev );
  179. /** Enable or disable interrupts
  180. *
  181. * @v netdev Network device
  182. * @v enable Interrupts should be enabled
  183. */
  184. void ( * irq ) ( struct net_device *netdev, int enable );
  185. };
  186. /** Network device error */
  187. struct net_device_error {
  188. /** Error status code */
  189. int rc;
  190. /** Error count */
  191. unsigned int count;
  192. };
  193. /** Maximum number of unique errors that we will keep track of */
  194. #define NETDEV_MAX_UNIQUE_ERRORS 4
  195. /** Network device statistics */
  196. struct net_device_stats {
  197. /** Count of successful completions */
  198. unsigned int good;
  199. /** Count of error completions */
  200. unsigned int bad;
  201. /** Error breakdowns */
  202. struct net_device_error errors[NETDEV_MAX_UNIQUE_ERRORS];
  203. };
  204. /**
  205. * A network device
  206. *
  207. * This structure represents a piece of networking hardware. It has
  208. * properties such as a link-layer address and methods for
  209. * transmitting and receiving raw packets.
  210. *
  211. * Note that this structure must represent a generic network device,
  212. * not just an Ethernet device.
  213. */
  214. struct net_device {
  215. /** Reference counter */
  216. struct refcnt refcnt;
  217. /** List of network devices */
  218. struct list_head list;
  219. /** List of open network devices */
  220. struct list_head open_list;
  221. /** Name of this network device */
  222. char name[8];
  223. /** Underlying hardware device */
  224. struct device *dev;
  225. /** Network device operations */
  226. struct net_device_operations *op;
  227. /** Link-layer protocol */
  228. struct ll_protocol *ll_protocol;
  229. /** Link-layer address
  230. *
  231. * For Ethernet, this is the MAC address.
  232. */
  233. uint8_t ll_addr[MAX_LL_ADDR_LEN];
  234. /** Current device state
  235. *
  236. * This is the bitwise-OR of zero or more NETDEV_XXX constants.
  237. */
  238. unsigned int state;
  239. /** Maximum packet length
  240. *
  241. * This length includes any link-layer headers.
  242. */
  243. size_t max_pkt_len;
  244. /** TX packet queue */
  245. struct list_head tx_queue;
  246. /** RX packet queue */
  247. struct list_head rx_queue;
  248. /** TX statistics */
  249. struct net_device_stats tx_stats;
  250. /** RX statistics */
  251. struct net_device_stats rx_stats;
  252. /** Configuration settings applicable to this device */
  253. struct simple_settings settings;
  254. /** Driver private data */
  255. void *priv;
  256. };
  257. /** Network device is open */
  258. #define NETDEV_OPEN 0x0001
  259. /** Network device has link */
  260. #define NETDEV_LINK_UP 0x0002
  261. /** Declare a link-layer protocol */
  262. #define __ll_protocol __table ( struct ll_protocol, ll_protocols, 01 )
  263. /** Declare a network-layer protocol */
  264. #define __net_protocol __table ( struct net_protocol, net_protocols, 01 )
  265. extern struct list_head net_devices;
  266. extern struct net_device_operations null_netdev_operations;
  267. /**
  268. * Initialise a network device
  269. *
  270. * @v netdev Network device
  271. * @v op Network device operations
  272. */
  273. static inline void netdev_init ( struct net_device *netdev,
  274. struct net_device_operations *op ) {
  275. netdev->op = op;
  276. }
  277. /**
  278. * Stop using a network device
  279. *
  280. * @v netdev Network device
  281. *
  282. * Drivers should call this method immediately before the final call
  283. * to netdev_put().
  284. */
  285. static inline void netdev_nullify ( struct net_device *netdev ) {
  286. netdev->op = &null_netdev_operations;
  287. }
  288. /**
  289. * Get printable network device hardware address
  290. *
  291. * @v netdev Network device
  292. * @ret name Hardware address
  293. */
  294. static inline const char * netdev_hwaddr ( struct net_device *netdev ) {
  295. return netdev->ll_protocol->ntoa ( netdev->ll_addr );
  296. }
  297. /** Iterate over all network devices */
  298. #define for_each_netdev( netdev ) \
  299. list_for_each_entry ( (netdev), &net_devices, list )
  300. /** There exist some network devices
  301. *
  302. * @ret existence Existence of network devices
  303. */
  304. static inline int have_netdevs ( void ) {
  305. return ( ! list_empty ( &net_devices ) );
  306. }
  307. /**
  308. * Get reference to network device
  309. *
  310. * @v netdev Network device
  311. * @ret netdev Network device
  312. */
  313. static inline __attribute__ (( always_inline )) struct net_device *
  314. netdev_get ( struct net_device *netdev ) {
  315. ref_get ( &netdev->refcnt );
  316. return netdev;
  317. }
  318. /**
  319. * Drop reference to network device
  320. *
  321. * @v netdev Network device
  322. */
  323. static inline __attribute__ (( always_inline )) void
  324. netdev_put ( struct net_device *netdev ) {
  325. ref_put ( &netdev->refcnt );
  326. }
  327. /**
  328. * Get driver private area for this network device
  329. *
  330. * @v netdev Network device
  331. * @ret priv Driver private area for this network device
  332. */
  333. static inline __attribute__ (( always_inline )) void *
  334. netdev_priv ( struct net_device *netdev ) {
  335. return netdev->priv;
  336. }
  337. /**
  338. * Get per-netdevice configuration settings block
  339. *
  340. * @v netdev Network device
  341. * @ret settings Settings block
  342. */
  343. static inline __attribute__ (( always_inline )) struct settings *
  344. netdev_settings ( struct net_device *netdev ) {
  345. return &netdev->settings.settings;
  346. }
  347. /**
  348. * Mark network device as having link up
  349. *
  350. * @v netdev Network device
  351. */
  352. static inline __attribute__ (( always_inline )) void
  353. netdev_link_up ( struct net_device *netdev ) {
  354. netdev->state |= NETDEV_LINK_UP;
  355. }
  356. /**
  357. * Mark network device as having link down
  358. *
  359. * @v netdev Network device
  360. */
  361. static inline __attribute__ (( always_inline )) void
  362. netdev_link_down ( struct net_device *netdev ) {
  363. netdev->state &= ~NETDEV_LINK_UP;
  364. }
  365. /**
  366. * Check link state of network device
  367. *
  368. * @v netdev Network device
  369. * @ret link_up Link is up
  370. */
  371. static inline __attribute__ (( always_inline )) int
  372. netdev_link_ok ( struct net_device *netdev ) {
  373. return ( netdev->state & NETDEV_LINK_UP );
  374. }
  375. extern int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf );
  376. extern void netdev_tx_complete_err ( struct net_device *netdev,
  377. struct io_buffer *iobuf, int rc );
  378. extern void netdev_tx_complete_next_err ( struct net_device *netdev, int rc );
  379. extern void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf );
  380. extern void netdev_rx_err ( struct net_device *netdev,
  381. struct io_buffer *iobuf, int rc );
  382. extern void netdev_poll ( struct net_device *netdev );
  383. extern struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev );
  384. extern struct net_device * alloc_netdev ( size_t priv_size );
  385. extern int register_netdev ( struct net_device *netdev );
  386. extern int netdev_open ( struct net_device *netdev );
  387. extern void netdev_close ( struct net_device *netdev );
  388. extern void unregister_netdev ( struct net_device *netdev );
  389. extern void netdev_irq ( struct net_device *netdev, int enable );
  390. extern struct net_device * find_netdev ( const char *name );
  391. extern struct net_device * find_netdev_by_location ( unsigned int bus_type,
  392. unsigned int location );
  393. extern struct net_device * last_opened_netdev ( void );
  394. extern int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
  395. struct net_protocol *net_protocol, const void *ll_dest );
  396. extern int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  397. uint16_t net_proto, const void *ll_source );
  398. extern struct settings_operations netdev_settings_operations;
  399. /**
  400. * Complete network transmission
  401. *
  402. * @v netdev Network device
  403. * @v iobuf I/O buffer
  404. *
  405. * The packet must currently be in the network device's TX queue.
  406. */
  407. static inline void netdev_tx_complete ( struct net_device *netdev,
  408. struct io_buffer *iobuf ) {
  409. netdev_tx_complete_err ( netdev, iobuf, 0 );
  410. }
  411. /**
  412. * Complete network transmission
  413. *
  414. * @v netdev Network device
  415. *
  416. * Completes the oldest outstanding packet in the TX queue.
  417. */
  418. static inline void netdev_tx_complete_next ( struct net_device *netdev ) {
  419. netdev_tx_complete_next_err ( netdev, 0 );
  420. }
  421. #endif /* _GPXE_NETDEVICE_H */