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.

netdevice.h 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 32
  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. * Transmit network-layer packet via network device
  72. *
  73. * @v iobuf I/O buffer
  74. * @v netdev Network device
  75. * @v net_protocol Network-layer protocol
  76. * @v ll_dest Link-layer destination address
  77. * @ret rc Return status code
  78. *
  79. * This method should prepend in the link-layer header
  80. * (e.g. the Ethernet DIX header) and transmit the packet.
  81. * This method takes ownership of the I/O buffer.
  82. */
  83. int ( * tx ) ( struct io_buffer *iobuf, struct net_device *netdev,
  84. struct net_protocol *net_protocol,
  85. const void *ll_dest );
  86. /**
  87. * Handle received packet
  88. *
  89. * @v iobuf I/O buffer
  90. * @v netdev Network device
  91. *
  92. * This method should strip off the link-layer header
  93. * (e.g. the Ethernet DIX header) and pass the packet to
  94. * net_rx(). This method takes ownership of the packet
  95. * buffer.
  96. */
  97. int ( * rx ) ( struct io_buffer *iobuf, struct net_device *netdev );
  98. /**
  99. * Transcribe link-layer address
  100. *
  101. * @v ll_addr Link-layer address
  102. * @ret string Human-readable transcription of address
  103. *
  104. * This method should convert the link-layer address into a
  105. * human-readable format.
  106. *
  107. * The buffer used to hold the transcription is statically
  108. * allocated.
  109. */
  110. const char * ( * ntoa ) ( const void * ll_addr );
  111. /** Link-layer protocol
  112. *
  113. * This is an ARPHRD_XXX constant, in network byte order.
  114. */
  115. uint16_t ll_proto;
  116. /** Link-layer address length */
  117. uint8_t ll_addr_len;
  118. /** Link-layer header length */
  119. uint8_t ll_header_len;
  120. /** Link-layer broadcast address */
  121. const uint8_t *ll_broadcast;
  122. };
  123. /** Network device operations */
  124. struct net_device_operations {
  125. /** Open network device
  126. *
  127. * @v netdev Network device
  128. * @ret rc Return status code
  129. *
  130. * This method should allocate RX I/O buffers and enable
  131. * the hardware to start transmitting and receiving packets.
  132. */
  133. int ( * open ) ( struct net_device *netdev );
  134. /** Close network device
  135. *
  136. * @v netdev Network device
  137. *
  138. * This method should stop the flow of packets, and free up
  139. * any packets that are currently in the device's TX queue.
  140. */
  141. void ( * close ) ( struct net_device *netdev );
  142. /** Transmit packet
  143. *
  144. * @v netdev Network device
  145. * @v iobuf I/O buffer
  146. * @ret rc Return status code
  147. *
  148. * This method should cause the hardware to initiate
  149. * transmission of the I/O buffer.
  150. *
  151. * If this method returns success, the I/O buffer remains
  152. * owned by the net device's TX queue, and the net device must
  153. * eventually call netdev_tx_complete() to free the buffer.
  154. * If this method returns failure, the I/O buffer is
  155. * immediately released; the failure is interpreted as
  156. * "failure to enqueue buffer".
  157. *
  158. * This method is guaranteed to be called only when the device
  159. * is open.
  160. */
  161. int ( * transmit ) ( struct net_device *netdev,
  162. struct io_buffer *iobuf );
  163. /** Poll for completed and received packets
  164. *
  165. * @v netdev Network device
  166. *
  167. * This method should cause the hardware to check for
  168. * completed transmissions and received packets. Any received
  169. * packets should be delivered via netdev_rx().
  170. *
  171. * This method is guaranteed to be called only when the device
  172. * is open.
  173. */
  174. void ( * poll ) ( struct net_device *netdev );
  175. /** Enable or disable interrupts
  176. *
  177. * @v netdev Network device
  178. * @v enable Interrupts should be enabled
  179. */
  180. void ( * irq ) ( struct net_device *netdev, int enable );
  181. };
  182. /** Network device statistics */
  183. struct net_device_stats {
  184. /** Count of successfully completed transmissions */
  185. unsigned int tx_ok;
  186. /** Count of transmission errors */
  187. unsigned int tx_err;
  188. /** Count of successfully received packets */
  189. unsigned int rx_ok;
  190. /** Count of reception errors */
  191. unsigned int rx_err;
  192. };
  193. /**
  194. * A network device
  195. *
  196. * This structure represents a piece of networking hardware. It has
  197. * properties such as a link-layer address and methods for
  198. * transmitting and receiving raw packets.
  199. *
  200. * Note that this structure must represent a generic network device,
  201. * not just an Ethernet device.
  202. */
  203. struct net_device {
  204. /** Reference counter */
  205. struct refcnt refcnt;
  206. /** List of network devices */
  207. struct list_head list;
  208. /** Name of this network device */
  209. char name[8];
  210. /** Underlying hardware device */
  211. struct device *dev;
  212. /** Network device operations */
  213. struct net_device_operations *op;
  214. /** Link-layer protocol */
  215. struct ll_protocol *ll_protocol;
  216. /** Link-layer address
  217. *
  218. * For Ethernet, this is the MAC address.
  219. */
  220. uint8_t ll_addr[MAX_LL_ADDR_LEN];
  221. /** Current device state
  222. *
  223. * This is the bitwise-OR of zero or more NETDEV_XXX constants.
  224. */
  225. unsigned int state;
  226. /** TX packet queue */
  227. struct list_head tx_queue;
  228. /** RX packet queue */
  229. struct list_head rx_queue;
  230. /** Device statistics */
  231. struct net_device_stats stats;
  232. /** Configuration settings applicable to this device */
  233. struct simple_settings settings;
  234. /** Driver private data */
  235. void *priv;
  236. };
  237. /** Network device is open */
  238. #define NETDEV_OPEN 0x0001
  239. /** Declare a link-layer protocol */
  240. #define __ll_protocol __table ( struct ll_protocol, ll_protocols, 01 )
  241. /** Declare a network-layer protocol */
  242. #define __net_protocol __table ( struct net_protocol, net_protocols, 01 )
  243. extern struct list_head net_devices;
  244. extern struct net_device_operations null_netdev_operations;
  245. /**
  246. * Initialise a network device
  247. *
  248. * @v netdev Network device
  249. * @v op Network device operations
  250. */
  251. static inline void netdev_init ( struct net_device *netdev,
  252. struct net_device_operations *op ) {
  253. netdev->op = op;
  254. }
  255. /**
  256. * Stop using a network device
  257. *
  258. * @v netdev Network device
  259. *
  260. * Drivers should call this method immediately before the final call
  261. * to netdev_put().
  262. */
  263. static inline void netdev_nullify ( struct net_device *netdev ) {
  264. netdev->op = &null_netdev_operations;
  265. }
  266. /**
  267. * Get printable network device hardware address
  268. *
  269. * @v netdev Network device
  270. * @ret name Hardware address
  271. */
  272. static inline const char * netdev_hwaddr ( struct net_device *netdev ) {
  273. return netdev->ll_protocol->ntoa ( netdev->ll_addr );
  274. }
  275. /** Iterate over all network devices */
  276. #define for_each_netdev( netdev ) \
  277. list_for_each_entry ( (netdev), &net_devices, list )
  278. /** There exist some network devices
  279. *
  280. * @ret existence Existence of network devices
  281. */
  282. static inline int have_netdevs ( void ) {
  283. return ( ! list_empty ( &net_devices ) );
  284. }
  285. /**
  286. * Get reference to network device
  287. *
  288. * @v netdev Network device
  289. * @ret netdev Network device
  290. */
  291. static inline __attribute__ (( always_inline )) struct net_device *
  292. netdev_get ( struct net_device *netdev ) {
  293. ref_get ( &netdev->refcnt );
  294. return netdev;
  295. }
  296. /**
  297. * Drop reference to network device
  298. *
  299. * @v netdev Network device
  300. */
  301. static inline __attribute__ (( always_inline )) void
  302. netdev_put ( struct net_device *netdev ) {
  303. ref_put ( &netdev->refcnt );
  304. }
  305. /**
  306. * Get driver private area for this network device
  307. *
  308. * @v netdev Network device
  309. * @ret priv Driver private area for this network device
  310. */
  311. static inline __attribute__ (( always_inline )) void *
  312. netdev_priv ( struct net_device *netdev ) {
  313. return netdev->priv;
  314. }
  315. /**
  316. * Get per-netdevice configuration settings block
  317. *
  318. * @v netdev Network device
  319. * @ret settings Settings block
  320. */
  321. static inline __attribute__ (( always_inline )) struct settings *
  322. netdev_settings ( struct net_device *netdev ) {
  323. return &netdev->settings.settings;
  324. }
  325. extern int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf );
  326. extern void netdev_tx_complete_err ( struct net_device *netdev,
  327. struct io_buffer *iobuf, int rc );
  328. extern void netdev_tx_complete_next_err ( struct net_device *netdev, int rc );
  329. extern void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf );
  330. extern void netdev_rx_err ( struct net_device *netdev,
  331. struct io_buffer *iobuf, int rc );
  332. extern void netdev_poll ( struct net_device *netdev );
  333. extern struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev );
  334. extern struct net_device * alloc_netdev ( size_t priv_size );
  335. extern int register_netdev ( struct net_device *netdev );
  336. extern int netdev_open ( struct net_device *netdev );
  337. extern void netdev_close ( struct net_device *netdev );
  338. extern void unregister_netdev ( struct net_device *netdev );
  339. extern void netdev_irq ( struct net_device *netdev, int enable );
  340. extern struct net_device * find_netdev ( const char *name );
  341. extern struct net_device * find_netdev_by_location ( unsigned int bus_type,
  342. unsigned int location );
  343. extern int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
  344. struct net_protocol *net_protocol, const void *ll_dest );
  345. extern int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
  346. uint16_t net_proto, const void *ll_source );
  347. extern struct settings_operations netdev_settings_operations;
  348. /**
  349. * Complete network transmission
  350. *
  351. * @v netdev Network device
  352. * @v iobuf I/O buffer
  353. *
  354. * The packet must currently be in the network device's TX queue.
  355. */
  356. static inline void netdev_tx_complete ( struct net_device *netdev,
  357. struct io_buffer *iobuf ) {
  358. netdev_tx_complete_err ( netdev, iobuf, 0 );
  359. }
  360. /**
  361. * Complete network transmission
  362. *
  363. * @v netdev Network device
  364. *
  365. * Completes the oldest outstanding packet in the TX queue.
  366. */
  367. static inline void netdev_tx_complete_next ( struct net_device *netdev ) {
  368. netdev_tx_complete_next_err ( netdev, 0 );
  369. }
  370. #endif /* _GPXE_NETDEVICE_H */