1 #ifndef _GPXE_NETDEVICE_H
2 #define _GPXE_NETDEVICE_H
6 * Network device management
11 #include <gpxe/list.h>
12 #include <gpxe/tables.h>
13 #include <gpxe/refcnt.h>
21 /** Maximum length of a link-layer address */
22 #define MAX_LL_ADDR_LEN 20
24 /** Maximum length of a link-layer header */
25 #define MAX_LL_HEADER_LEN 32
27 /** Maximum length of a network-layer address */
28 #define MAX_NET_ADDR_LEN 4
31 * A network-layer protocol
38 * Process received packet
41 * @v netdev Network device
42 * @v ll_source Link-layer source address
44 * This method takes ownership of the I/O buffer.
46 int ( * rx ) ( struct io_buffer *iobuf, struct net_device *netdev,
47 const void *ll_source );
49 * Transcribe network-layer address
51 * @v net_addr Network-layer address
52 * @ret string Human-readable transcription of address
54 * This method should convert the network-layer address into a
55 * human-readable format (e.g. dotted quad notation for IPv4).
57 * The buffer used to hold the transcription is statically
60 const char * ( *ntoa ) ( const void * net_addr );
61 /** Network-layer protocol
63 * This is an ETH_P_XXX constant, in network-byte order
66 /** Network-layer address length */
71 * A link-layer protocol
78 * Transmit network-layer packet via network device
81 * @v netdev Network device
82 * @v net_protocol Network-layer protocol
83 * @v ll_dest Link-layer destination address
84 * @ret rc Return status code
86 * This method should prepend in the link-layer header
87 * (e.g. the Ethernet DIX header) and transmit the packet.
88 * This method takes ownership of the I/O buffer.
90 int ( * tx ) ( struct io_buffer *iobuf, struct net_device *netdev,
91 struct net_protocol *net_protocol,
92 const void *ll_dest );
94 * Handle received packet
97 * @v netdev Network device
99 * This method should strip off the link-layer header
100 * (e.g. the Ethernet DIX header) and pass the packet to
101 * net_rx(). This method takes ownership of the packet
104 int ( * rx ) ( struct io_buffer *iobuf, struct net_device *netdev );
106 * Transcribe link-layer address
108 * @v ll_addr Link-layer address
109 * @ret string Human-readable transcription of address
111 * This method should convert the link-layer address into a
112 * human-readable format.
114 * The buffer used to hold the transcription is statically
117 const char * ( * ntoa ) ( const void * ll_addr );
118 /** Link-layer protocol
120 * This is an ARPHRD_XXX constant, in network byte order.
123 /** Link-layer address length */
125 /** Link-layer header length */
126 uint8_t ll_header_len;
127 /** Link-layer broadcast address */
128 const uint8_t *ll_broadcast;
131 /** Network device operations */
132 struct net_device_operations {
133 /** Open network device
135 * @v netdev Network device
136 * @ret rc Return status code
138 * This method should allocate RX I/O buffers and enable
139 * the hardware to start transmitting and receiving packets.
141 int ( * open ) ( struct net_device *netdev );
142 /** Close network device
144 * @v netdev Network device
146 * This method should stop the flow of packets, and free up
147 * any packets that are currently in the device's TX queue.
149 void ( * close ) ( struct net_device *netdev );
152 * @v netdev Network device
153 * @v iobuf I/O buffer
154 * @ret rc Return status code
156 * This method should cause the hardware to initiate
157 * transmission of the I/O buffer.
159 * If this method returns success, the I/O buffer remains
160 * owned by the net device's TX queue, and the net device must
161 * eventually call netdev_tx_complete() to free the buffer.
162 * If this method returns failure, the I/O buffer is
163 * immediately released; the failure is interpreted as
164 * "failure to enqueue buffer".
166 * This method is guaranteed to be called only when the device
169 int ( * transmit ) ( struct net_device *netdev,
170 struct io_buffer *iobuf );
171 /** Poll for completed and received packets
173 * @v netdev Network device
175 * This method should cause the hardware to check for
176 * completed transmissions and received packets. Any received
177 * packets should be delivered via netdev_rx().
179 * This method is guaranteed to be called only when the device
182 void ( * poll ) ( struct net_device *netdev );
183 /** Enable or disable interrupts
185 * @v netdev Network device
186 * @v enable Interrupts should be enabled
188 void ( * irq ) ( struct net_device *netdev, int enable );
191 /** Network device statistics */
192 struct net_device_stats {
193 /** Count of successfully completed transmissions */
195 /** Count of transmission errors */
197 /** Count of successfully received packets */
199 /** Count of reception errors */
206 * This structure represents a piece of networking hardware. It has
207 * properties such as a link-layer address and methods for
208 * transmitting and receiving raw packets.
210 * Note that this structure must represent a generic network device,
211 * not just an Ethernet device.
214 /** Reference counter */
215 struct refcnt refcnt;
216 /** List of network devices */
217 struct list_head list;
218 /** Name of this network device */
220 /** Underlying hardware device */
223 /** Network device operations */
224 struct net_device_operations *op;
226 /** Link-layer protocol */
227 struct ll_protocol *ll_protocol;
228 /** Link-layer address
230 * For Ethernet, this is the MAC address.
232 uint8_t ll_addr[MAX_LL_ADDR_LEN];
234 /** Current device state
236 * This is the bitwise-OR of zero or more NETDEV_XXX constants.
239 /** TX packet queue */
240 struct list_head tx_queue;
241 /** RX packet queue */
242 struct list_head rx_queue;
243 /** Device statistics */
244 struct net_device_stats stats;
246 /** Driver private data */
250 /** Network device is open */
251 #define NETDEV_OPEN 0x0001
253 /** Declare a link-layer protocol */
254 #define __ll_protocol __table ( struct ll_protocol, ll_protocols, 01 )
256 /** Declare a network-layer protocol */
257 #define __net_protocol __table ( struct net_protocol, net_protocols, 01 )
259 extern struct list_head net_devices;
260 extern struct net_device_operations null_netdev_operations;
263 * Initialise a network device
265 * @v netdev Network device
266 * @v op Network device operations
268 static inline void netdev_init ( struct net_device *netdev,
269 struct net_device_operations *op ) {
274 * Stop using a network device
276 * @v netdev Network device
278 * Drivers should call this method immediately before the final call
281 static inline void netdev_nullify ( struct net_device *netdev ) {
282 netdev->op = &null_netdev_operations;
286 * Get printable network device hardware address
288 * @v netdev Network device
289 * @ret name Hardware address
291 static inline const char * netdev_hwaddr ( struct net_device *netdev ) {
292 return netdev->ll_protocol->ntoa ( netdev->ll_addr );
295 /** Iterate over all network devices */
296 #define for_each_netdev( netdev ) \
297 list_for_each_entry ( (netdev), &net_devices, list )
299 /** There exist some network devices
301 * @ret existence Existence of network devices
303 static inline int have_netdevs ( void ) {
304 return ( ! list_empty ( &net_devices ) );
308 * Get reference to network device
310 * @v netdev Network device
311 * @ret netdev Network device
313 static inline __attribute__ (( always_inline )) struct net_device *
314 netdev_get ( struct net_device *netdev ) {
315 ref_get ( &netdev->refcnt );
320 * Drop reference to network device
322 * @v netdev Network device
324 static inline __attribute__ (( always_inline )) void
325 netdev_put ( struct net_device *netdev ) {
326 ref_put ( &netdev->refcnt );
330 * Get driver private area for this network device
332 * @v netdev Network device
333 * @ret priv Driver private area for this network device
335 static inline __attribute__ (( always_inline )) void *
336 netdev_priv ( struct net_device *netdev ) {
340 extern int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf );
341 extern void netdev_tx_complete_err ( struct net_device *netdev,
342 struct io_buffer *iobuf, int rc );
343 extern void netdev_tx_complete_next_err ( struct net_device *netdev, int rc );
344 extern void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf );
345 extern void netdev_rx_err ( struct net_device *netdev,
346 struct io_buffer *iobuf, int rc );
347 extern void netdev_poll ( struct net_device *netdev );
348 extern struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev );
349 extern struct net_device * alloc_netdev ( size_t priv_size );
350 extern int register_netdev ( struct net_device *netdev );
351 extern int netdev_open ( struct net_device *netdev );
352 extern void netdev_close ( struct net_device *netdev );
353 extern void unregister_netdev ( struct net_device *netdev );
354 extern void netdev_irq ( struct net_device *netdev, int enable );
355 extern struct net_device * find_netdev ( const char *name );
356 extern struct net_device * find_netdev_by_location ( unsigned int bus_type,
357 unsigned int location );
358 extern int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
359 struct net_protocol *net_protocol, const void *ll_dest );
360 extern int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
361 uint16_t net_proto, const void *ll_source );
364 * Complete network transmission
366 * @v netdev Network device
367 * @v iobuf I/O buffer
369 * The packet must currently be in the network device's TX queue.
371 static inline void netdev_tx_complete ( struct net_device *netdev,
372 struct io_buffer *iobuf ) {
373 netdev_tx_complete_err ( netdev, iobuf, 0 );
377 * Complete network transmission
379 * @v netdev Network device
381 * Completes the oldest outstanding packet in the TX queue.
383 static inline void netdev_tx_complete_next ( struct net_device *netdev ) {
384 netdev_tx_complete_next_err ( netdev, 0 );
387 #endif /* _GPXE_NETDEVICE_H */