2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <gpxe/if_ether.h>
26 #include <gpxe/iobuf.h>
27 #include <gpxe/tables.h>
28 #include <gpxe/process.h>
29 #include <gpxe/init.h>
30 #include <gpxe/device.h>
31 #include <gpxe/netdevice.h>
35 * Network device management
39 /** Registered network-layer protocols */
40 static struct net_protocol net_protocols[0]
41 __table_start ( struct net_protocol, net_protocols );
42 static struct net_protocol net_protocols_end[0]
43 __table_end ( struct net_protocol, net_protocols );
45 /** List of network devices */
46 struct list_head net_devices = LIST_HEAD_INIT ( net_devices );
49 * Transmit raw packet via network device
51 * @v netdev Network device
53 * @ret rc Return status code
55 * Transmits the packet via the specified network device. This
56 * function takes ownership of the I/O buffer.
58 int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf ) {
61 DBGC ( netdev, "NETDEV %p transmitting %p (%p+%zx)\n",
62 netdev, iobuf, iobuf->data, iob_len ( iobuf ) );
64 list_add_tail ( &iobuf->list, &netdev->tx_queue );
66 if ( ! ( netdev->state & NETDEV_OPEN ) ) {
71 if ( ( rc = netdev->transmit ( netdev, iobuf ) ) != 0 )
77 DBGC ( netdev, "NETDEV %p transmission %p failed: %s\n",
78 netdev, iobuf, strerror ( rc ) );
79 netdev_tx_complete ( netdev, iobuf );
84 * Complete network transmission
86 * @v netdev Network device
89 * The packet must currently be in the network device's TX queue.
91 void netdev_tx_complete ( struct net_device *netdev, struct io_buffer *iobuf ) {
92 DBGC ( netdev, "NETDEV %p transmission %p complete\n", netdev, iobuf );
94 /* Catch data corruption as early as possible */
95 assert ( iobuf->list.next != NULL );
96 assert ( iobuf->list.prev != NULL );
98 list_del ( &iobuf->list );
103 * Complete network transmission
105 * @v netdev Network device
107 * Completes the oldest outstanding packet in the TX queue.
109 void netdev_tx_complete_next ( struct net_device *netdev ) {
110 struct io_buffer *iobuf;
112 list_for_each_entry ( iobuf, &netdev->tx_queue, list ) {
113 netdev_tx_complete ( netdev, iobuf );
119 * Add packet to receive queue
121 * @v netdev Network device
122 * @v iobuf I/O buffer
124 * The packet is added to the network device's RX queue. This
125 * function takes ownership of the I/O buffer.
127 void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
128 DBGC ( netdev, "NETDEV %p received %p (%p+%zx)\n",
129 netdev, iobuf, iobuf->data, iob_len ( iobuf ) );
130 list_add_tail ( &iobuf->list, &netdev->rx_queue );
134 * Poll for packet on network device
136 * @v netdev Network device
137 * @v rx_quota Maximum number of packets to receive
138 * @ret True There are packets present in the receive queue
139 * @ret False There are no packets present in the receive queue
141 * Polls the network device for received packets. Any received
142 * packets will be added to the RX packet queue via netdev_rx().
144 int netdev_poll ( struct net_device *netdev, unsigned int rx_quota ) {
146 if ( netdev->state & NETDEV_OPEN )
147 netdev->poll ( netdev, rx_quota );
149 return ( ! list_empty ( &netdev->rx_queue ) );
153 * Remove packet from device's receive queue
155 * @v netdev Network device
156 * @ret iobuf I/O buffer, or NULL
158 * Removes the first packet from the device's RX queue and returns it.
159 * Ownership of the packet is transferred to the caller.
161 struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev ) {
162 struct io_buffer *iobuf;
164 list_for_each_entry ( iobuf, &netdev->rx_queue, list ) {
165 list_del ( &iobuf->list );
172 * Allocate network device
174 * @v priv_size Size of private data area (net_device::priv)
175 * @ret netdev Network device, or NULL
177 * Allocates space for a network device and its private data area.
179 struct net_device * alloc_netdev ( size_t priv_size ) {
180 struct net_device *netdev;
183 total_len = ( sizeof ( *netdev ) + priv_size );
184 netdev = malloc ( total_len );
186 memset ( netdev, 0, total_len );
187 INIT_LIST_HEAD ( &netdev->references );
188 INIT_LIST_HEAD ( &netdev->tx_queue );
189 INIT_LIST_HEAD ( &netdev->rx_queue );
190 netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) );
196 * Register network device
198 * @v netdev Network device
199 * @ret rc Return status code
201 * Gives the network device a name and adds it to the list of network
204 int register_netdev ( struct net_device *netdev ) {
205 static unsigned int ifindex = 0;
207 /* Create device name */
208 snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
211 /* Add to device list */
212 list_add_tail ( &netdev->list, &net_devices );
213 DBGC ( netdev, "NETDEV %p registered as %s (phys %s hwaddr %s)\n",
214 netdev, netdev->name, netdev->dev->name,
215 netdev_hwaddr ( netdev ) );
221 * Open network device
223 * @v netdev Network device
224 * @ret rc Return status code
226 int netdev_open ( struct net_device *netdev ) {
229 /* Do nothing if device is already open */
230 if ( netdev->state & NETDEV_OPEN )
233 DBGC ( netdev, "NETDEV %p opening\n", netdev );
235 /* Open the device */
236 if ( ( rc = netdev->open ( netdev ) ) != 0 )
240 netdev->state |= NETDEV_OPEN;
245 * Close network device
247 * @v netdev Network device
249 void netdev_close ( struct net_device *netdev ) {
250 struct io_buffer *iobuf;
252 /* Do nothing if device is already closed */
253 if ( ! ( netdev->state & NETDEV_OPEN ) )
256 DBGC ( netdev, "NETDEV %p closing\n", netdev );
258 /* Close the device */
259 netdev->close ( netdev );
261 /* Discard any packets in the TX queue */
262 while ( ! list_empty ( &netdev->tx_queue ) ) {
263 netdev_tx_complete_next ( netdev );
266 /* Discard any packets in the RX queue */
267 while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
268 DBGC ( netdev, "NETDEV %p discarding received %p\n",
274 netdev->state &= ~NETDEV_OPEN;
278 * Unregister network device
280 * @v netdev Network device
282 * Removes the network device from the list of network devices.
284 void unregister_netdev ( struct net_device *netdev ) {
286 /* Ensure device is closed */
287 netdev_close ( netdev );
289 /* Kill off any persistent references to this device */
290 forget_references ( &netdev->references );
292 /* Remove from device list */
293 list_del ( &netdev->list );
294 DBGC ( netdev, "NETDEV %p unregistered\n", netdev );
298 * Free network device
300 * @v netdev Network device
302 void free_netdev ( struct net_device *netdev ) {
307 * Get network device by name
309 * @v name Network device name
310 * @ret netdev Network device, or NULL
312 struct net_device * find_netdev ( const char *name ) {
313 struct net_device *netdev;
315 list_for_each_entry ( netdev, &net_devices, list ) {
316 if ( strcmp ( netdev->name, name ) == 0 )
324 * Get network device by PCI bus:dev.fn address
326 * @v busdevfn PCI bus:dev.fn address
327 * @ret netdev Network device, or NULL
329 struct net_device * find_pci_netdev ( unsigned int busdevfn ) {
330 struct net_device *netdev;
332 list_for_each_entry ( netdev, &net_devices, list ) {
333 if ( ( netdev->dev->desc.bus_type == BUS_TYPE_PCI ) &&
334 ( netdev->dev->desc.location == busdevfn ) )
342 * Transmit network-layer packet
344 * @v iobuf I/O buffer
345 * @v netdev Network device
346 * @v net_protocol Network-layer protocol
347 * @v ll_dest Destination link-layer address
348 * @ret rc Return status code
350 * Prepends link-layer headers to the I/O buffer and transmits the
351 * packet via the specified network device. This function takes
352 * ownership of the I/O buffer.
354 int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
355 struct net_protocol *net_protocol, const void *ll_dest ) {
356 return netdev->ll_protocol->tx ( iobuf, netdev, net_protocol, ll_dest );
360 * Process received network-layer packet
362 * @v iobuf I/O buffer
363 * @v netdev Network device
364 * @v net_proto Network-layer protocol, in network-byte order
365 * @v ll_source Source link-layer address
366 * @ret rc Return status code
368 int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
369 uint16_t net_proto, const void *ll_source ) {
370 struct net_protocol *net_protocol;
372 /* Hand off to network-layer protocol, if any */
373 for ( net_protocol = net_protocols ; net_protocol < net_protocols_end ;
375 if ( net_protocol->net_proto == net_proto ) {
376 return net_protocol->rx ( iobuf, netdev, ll_source );
384 * Single-step the network stack
386 * @v process Network stack process
388 * This polls all interfaces for received packets, and processes
389 * packets from the RX queue.
391 static void net_step ( struct process *process __unused ) {
392 struct net_device *netdev;
393 struct io_buffer *iobuf;
395 /* Poll and process each network device */
396 list_for_each_entry ( netdev, &net_devices, list ) {
398 /* Poll for new packets */
399 netdev_poll ( netdev, -1U );
401 /* Process at most one received packet. Give priority
402 * to getting packets out of the NIC over processing
403 * the received packets, because we advertise a window
404 * that assumes that we can receive packets from the
405 * NIC faster than they arrive.
407 if ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
408 DBGC ( netdev, "NETDEV %p processing %p\n",
410 netdev->ll_protocol->rx ( iobuf, netdev );
415 /** Networking stack process */
416 static struct process net_process = {
420 /** Initialise the networking stack process */
421 static void init_net ( void ) {
422 process_add ( &net_process );
425 INIT_FN ( INIT_PROCESS, init_net, NULL, NULL );