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>
14 #include <gpxe/settings.h>
22 /** Maximum length of a link-layer address */
23 #define MAX_LL_ADDR_LEN 20
25 /** Maximum length of a link-layer header */
26 #define MAX_LL_HEADER_LEN 32
28 /** Maximum length of a network-layer address */
29 #define MAX_NET_ADDR_LEN 4
32 * A network-layer protocol
39 * Process received packet
42 * @v netdev Network device
43 * @v ll_source Link-layer source address
45 * This method takes ownership of the I/O buffer.
47 int ( * rx ) ( struct io_buffer *iobuf, struct net_device *netdev,
48 const void *ll_source );
50 * Transcribe network-layer address
52 * @v net_addr Network-layer address
53 * @ret string Human-readable transcription of address
55 * This method should convert the network-layer address into a
56 * human-readable format (e.g. dotted quad notation for IPv4).
58 * The buffer used to hold the transcription is statically
61 const char * ( *ntoa ) ( const void * net_addr );
62 /** Network-layer protocol
64 * This is an ETH_P_XXX constant, in network-byte order
67 /** Network-layer address length */
72 * A link-layer protocol
79 * Transmit network-layer packet via network device
82 * @v netdev Network device
83 * @v net_protocol Network-layer protocol
84 * @v ll_dest Link-layer destination address
85 * @ret rc Return status code
87 * This method should prepend in the link-layer header
88 * (e.g. the Ethernet DIX header) and transmit the packet.
89 * This method takes ownership of the I/O buffer.
91 int ( * tx ) ( struct io_buffer *iobuf, struct net_device *netdev,
92 struct net_protocol *net_protocol,
93 const void *ll_dest );
95 * Handle received packet
98 * @v netdev Network device
100 * This method should strip off the link-layer header
101 * (e.g. the Ethernet DIX header) and pass the packet to
102 * net_rx(). This method takes ownership of the packet
105 int ( * rx ) ( struct io_buffer *iobuf, struct net_device *netdev );
107 * Transcribe link-layer address
109 * @v ll_addr Link-layer address
110 * @ret string Human-readable transcription of address
112 * This method should convert the link-layer address into a
113 * human-readable format.
115 * The buffer used to hold the transcription is statically
118 const char * ( * ntoa ) ( const void * ll_addr );
119 /** Link-layer protocol
121 * This is an ARPHRD_XXX constant, in network byte order.
124 /** Link-layer address length */
126 /** Link-layer header length */
127 uint8_t ll_header_len;
128 /** Link-layer broadcast address */
129 const uint8_t *ll_broadcast;
132 /** Network device operations */
133 struct net_device_operations {
134 /** Open network device
136 * @v netdev Network device
137 * @ret rc Return status code
139 * This method should allocate RX I/O buffers and enable
140 * the hardware to start transmitting and receiving packets.
142 int ( * open ) ( struct net_device *netdev );
143 /** Close network device
145 * @v netdev Network device
147 * This method should stop the flow of packets, and free up
148 * any packets that are currently in the device's TX queue.
150 void ( * close ) ( struct net_device *netdev );
153 * @v netdev Network device
154 * @v iobuf I/O buffer
155 * @ret rc Return status code
157 * This method should cause the hardware to initiate
158 * transmission of the I/O buffer.
160 * If this method returns success, the I/O buffer remains
161 * owned by the net device's TX queue, and the net device must
162 * eventually call netdev_tx_complete() to free the buffer.
163 * If this method returns failure, the I/O buffer is
164 * immediately released; the failure is interpreted as
165 * "failure to enqueue buffer".
167 * This method is guaranteed to be called only when the device
170 int ( * transmit ) ( struct net_device *netdev,
171 struct io_buffer *iobuf );
172 /** Poll for completed and received packets
174 * @v netdev Network device
176 * This method should cause the hardware to check for
177 * completed transmissions and received packets. Any received
178 * packets should be delivered via netdev_rx().
180 * This method is guaranteed to be called only when the device
183 void ( * poll ) ( struct net_device *netdev );
184 /** Enable or disable interrupts
186 * @v netdev Network device
187 * @v enable Interrupts should be enabled
189 void ( * irq ) ( struct net_device *netdev, int enable );
192 /** Network device statistics */
193 struct net_device_stats {
194 /** Count of successfully completed transmissions */
196 /** Count of transmission errors */
198 /** Count of successfully received packets */
200 /** Count of reception errors */
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.
211 * Note that this structure must represent a generic network device,
212 * not just an Ethernet device.
215 /** Reference counter */
216 struct refcnt refcnt;
217 /** List of network devices */
218 struct list_head list;
219 /** Name of this network device */
221 /** Underlying hardware device */
224 /** Network device operations */
225 struct net_device_operations *op;
227 /** Link-layer protocol */
228 struct ll_protocol *ll_protocol;
229 /** Link-layer address
231 * For Ethernet, this is the MAC address.
233 uint8_t ll_addr[MAX_LL_ADDR_LEN];
235 /** Current device state
237 * This is the bitwise-OR of zero or more NETDEV_XXX constants.
240 /** TX packet queue */
241 struct list_head tx_queue;
242 /** RX packet queue */
243 struct list_head rx_queue;
244 /** Device statistics */
245 struct net_device_stats stats;
247 /** Configuration settings applicable to this device */
248 struct settings settings;
250 /** Driver private data */
254 /** Network device is open */
255 #define NETDEV_OPEN 0x0001
257 /** Declare a link-layer protocol */
258 #define __ll_protocol __table ( struct ll_protocol, ll_protocols, 01 )
260 /** Declare a network-layer protocol */
261 #define __net_protocol __table ( struct net_protocol, net_protocols, 01 )
263 extern struct list_head net_devices;
264 extern struct net_device_operations null_netdev_operations;
267 * Initialise a network device
269 * @v netdev Network device
270 * @v op Network device operations
272 static inline void netdev_init ( struct net_device *netdev,
273 struct net_device_operations *op ) {
278 * Stop using a network device
280 * @v netdev Network device
282 * Drivers should call this method immediately before the final call
285 static inline void netdev_nullify ( struct net_device *netdev ) {
286 netdev->op = &null_netdev_operations;
290 * Get printable network device hardware address
292 * @v netdev Network device
293 * @ret name Hardware address
295 static inline const char * netdev_hwaddr ( struct net_device *netdev ) {
296 return netdev->ll_protocol->ntoa ( netdev->ll_addr );
299 /** Iterate over all network devices */
300 #define for_each_netdev( netdev ) \
301 list_for_each_entry ( (netdev), &net_devices, list )
303 /** There exist some network devices
305 * @ret existence Existence of network devices
307 static inline int have_netdevs ( void ) {
308 return ( ! list_empty ( &net_devices ) );
312 * Get reference to network device
314 * @v netdev Network device
315 * @ret netdev Network device
317 static inline __attribute__ (( always_inline )) struct net_device *
318 netdev_get ( struct net_device *netdev ) {
319 ref_get ( &netdev->refcnt );
324 * Drop reference to network device
326 * @v netdev Network device
328 static inline __attribute__ (( always_inline )) void
329 netdev_put ( struct net_device *netdev ) {
330 ref_put ( &netdev->refcnt );
334 * Get driver private area for this network device
336 * @v netdev Network device
337 * @ret priv Driver private area for this network device
339 static inline __attribute__ (( always_inline )) void *
340 netdev_priv ( struct net_device *netdev ) {
344 extern int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf );
345 extern void netdev_tx_complete_err ( struct net_device *netdev,
346 struct io_buffer *iobuf, int rc );
347 extern void netdev_tx_complete_next_err ( struct net_device *netdev, int rc );
348 extern void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf );
349 extern void netdev_rx_err ( struct net_device *netdev,
350 struct io_buffer *iobuf, int rc );
351 extern void netdev_poll ( struct net_device *netdev );
352 extern struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev );
353 extern struct net_device * alloc_netdev ( size_t priv_size );
354 extern int register_netdev ( struct net_device *netdev );
355 extern int netdev_open ( struct net_device *netdev );
356 extern void netdev_close ( struct net_device *netdev );
357 extern void unregister_netdev ( struct net_device *netdev );
358 extern void netdev_irq ( struct net_device *netdev, int enable );
359 extern struct net_device * find_netdev ( const char *name );
360 extern struct net_device * find_netdev_by_location ( unsigned int bus_type,
361 unsigned int location );
362 extern int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
363 struct net_protocol *net_protocol, const void *ll_dest );
364 extern int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
365 uint16_t net_proto, const void *ll_source );
367 extern struct settings_operations netdev_settings_operations;
370 * Complete network transmission
372 * @v netdev Network device
373 * @v iobuf I/O buffer
375 * The packet must currently be in the network device's TX queue.
377 static inline void netdev_tx_complete ( struct net_device *netdev,
378 struct io_buffer *iobuf ) {
379 netdev_tx_complete_err ( netdev, iobuf, 0 );
383 * Complete network transmission
385 * @v netdev Network device
387 * Completes the oldest outstanding packet in the TX queue.
389 static inline void netdev_tx_complete_next ( struct net_device *netdev ) {
390 netdev_tx_complete_next_err ( netdev, 0 );
393 #endif /* _GPXE_NETDEVICE_H */