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 * Flush device's transmit queue
121 * @v netdev Network device
123 static void netdev_tx_flush ( struct net_device *netdev ) {
125 /* Discard any packets in the TX queue */
126 while ( ! list_empty ( &netdev->tx_queue ) ) {
127 netdev_tx_complete_next ( netdev );
132 * Add packet to receive queue
134 * @v netdev Network device
135 * @v iobuf I/O buffer
137 * The packet is added to the network device's RX queue. This
138 * function takes ownership of the I/O buffer.
140 void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
141 DBGC ( netdev, "NETDEV %p received %p (%p+%zx)\n",
142 netdev, iobuf, iobuf->data, iob_len ( iobuf ) );
143 list_add_tail ( &iobuf->list, &netdev->rx_queue );
147 * Poll for packet on network device
149 * @v netdev Network device
150 * @v rx_quota Maximum number of packets to receive
151 * @ret True There are packets present in the receive queue
152 * @ret False There are no packets present in the receive queue
154 * Polls the network device for received packets. Any received
155 * packets will be added to the RX packet queue via netdev_rx().
157 int netdev_poll ( struct net_device *netdev, unsigned int rx_quota ) {
159 if ( netdev->state & NETDEV_OPEN )
160 netdev->poll ( netdev, rx_quota );
162 return ( ! list_empty ( &netdev->rx_queue ) );
166 * Remove packet from device's receive queue
168 * @v netdev Network device
169 * @ret iobuf I/O buffer, or NULL
171 * Removes the first packet from the device's RX queue and returns it.
172 * Ownership of the packet is transferred to the caller.
174 struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev ) {
175 struct io_buffer *iobuf;
177 list_for_each_entry ( iobuf, &netdev->rx_queue, list ) {
178 list_del ( &iobuf->list );
185 * Flush device's receive queue
187 * @v netdev Network device
189 static void netdev_rx_flush ( struct net_device *netdev ) {
190 struct io_buffer *iobuf;
192 /* Discard any packets in the RX queue */
193 while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
194 DBGC ( netdev, "NETDEV %p discarding received %p\n",
201 * Free network device
203 * @v refcnt Network device reference counter
205 static void free_netdev ( struct refcnt *refcnt ) {
206 struct net_device *netdev =
207 container_of ( refcnt, struct net_device, refcnt );
209 netdev_tx_flush ( netdev );
210 netdev_rx_flush ( netdev );
215 * Allocate network device
217 * @v priv_size Size of private data area (net_device::priv)
218 * @ret netdev Network device, or NULL
220 * Allocates space for a network device and its private data area.
222 struct net_device * alloc_netdev ( size_t priv_size ) {
223 struct net_device *netdev;
226 total_len = ( sizeof ( *netdev ) + priv_size );
227 netdev = malloc ( total_len );
229 memset ( netdev, 0, total_len );
230 netdev->refcnt.free = free_netdev;
231 INIT_LIST_HEAD ( &netdev->tx_queue );
232 INIT_LIST_HEAD ( &netdev->rx_queue );
233 netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) );
239 * Register network device
241 * @v netdev Network device
242 * @ret rc Return status code
244 * Gives the network device a name and adds it to the list of network
247 int register_netdev ( struct net_device *netdev ) {
248 static unsigned int ifindex = 0;
250 /* Create device name */
251 snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
254 /* Add to device list */
255 netdev_get ( netdev );
256 list_add_tail ( &netdev->list, &net_devices );
257 DBGC ( netdev, "NETDEV %p registered as %s (phys %s hwaddr %s)\n",
258 netdev, netdev->name, netdev->dev->name,
259 netdev_hwaddr ( netdev ) );
265 * Open network device
267 * @v netdev Network device
268 * @ret rc Return status code
270 int netdev_open ( struct net_device *netdev ) {
273 /* Do nothing if device is already open */
274 if ( netdev->state & NETDEV_OPEN )
277 DBGC ( netdev, "NETDEV %p opening\n", netdev );
279 /* Open the device */
280 if ( ( rc = netdev->open ( netdev ) ) != 0 )
284 netdev->state |= NETDEV_OPEN;
289 * Close network device
291 * @v netdev Network device
293 void netdev_close ( struct net_device *netdev ) {
295 /* Do nothing if device is already closed */
296 if ( ! ( netdev->state & NETDEV_OPEN ) )
299 DBGC ( netdev, "NETDEV %p closing\n", netdev );
301 /* Close the device */
302 netdev->close ( netdev );
304 /* Flush TX and RX queues */
305 netdev_tx_flush ( netdev );
306 netdev_rx_flush ( netdev );
309 netdev->state &= ~NETDEV_OPEN;
313 * Unregister network device
315 * @v netdev Network device
317 * Removes the network device from the list of network devices.
319 void unregister_netdev ( struct net_device *netdev ) {
321 /* Ensure device is closed */
322 netdev_close ( netdev );
324 /* Remove from device list */
325 list_del ( &netdev->list );
326 netdev_put ( netdev );
327 DBGC ( netdev, "NETDEV %p unregistered\n", netdev );
331 * Get network device by name
333 * @v name Network device name
334 * @ret netdev Network device, or NULL
336 struct net_device * find_netdev ( const char *name ) {
337 struct net_device *netdev;
339 list_for_each_entry ( netdev, &net_devices, list ) {
340 if ( strcmp ( netdev->name, name ) == 0 )
348 * Get network device by PCI bus:dev.fn address
350 * @v busdevfn PCI bus:dev.fn address
351 * @ret netdev Network device, or NULL
353 struct net_device * find_pci_netdev ( unsigned int busdevfn ) {
354 struct net_device *netdev;
356 list_for_each_entry ( netdev, &net_devices, list ) {
357 if ( ( netdev->dev->desc.bus_type == BUS_TYPE_PCI ) &&
358 ( netdev->dev->desc.location == busdevfn ) )
366 * Transmit network-layer packet
368 * @v iobuf I/O buffer
369 * @v netdev Network device
370 * @v net_protocol Network-layer protocol
371 * @v ll_dest Destination link-layer address
372 * @ret rc Return status code
374 * Prepends link-layer headers to the I/O buffer and transmits the
375 * packet via the specified network device. This function takes
376 * ownership of the I/O buffer.
378 int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
379 struct net_protocol *net_protocol, const void *ll_dest ) {
380 return netdev->ll_protocol->tx ( iobuf, netdev, net_protocol, ll_dest );
384 * Process received network-layer packet
386 * @v iobuf I/O buffer
387 * @v netdev Network device
388 * @v net_proto Network-layer protocol, in network-byte order
389 * @v ll_source Source link-layer address
390 * @ret rc Return status code
392 int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
393 uint16_t net_proto, const void *ll_source ) {
394 struct net_protocol *net_protocol;
396 /* Hand off to network-layer protocol, if any */
397 for ( net_protocol = net_protocols ; net_protocol < net_protocols_end ;
399 if ( net_protocol->net_proto == net_proto ) {
400 return net_protocol->rx ( iobuf, netdev, ll_source );
408 * Single-step the network stack
410 * @v process Network stack process
412 * This polls all interfaces for received packets, and processes
413 * packets from the RX queue.
415 static void net_step ( struct process *process __unused ) {
416 struct net_device *netdev;
417 struct io_buffer *iobuf;
419 /* Poll and process each network device */
420 list_for_each_entry ( netdev, &net_devices, list ) {
422 /* Poll for new packets */
423 netdev_poll ( netdev, -1U );
425 /* Process at most one received packet. Give priority
426 * to getting packets out of the NIC over processing
427 * the received packets, because we advertise a window
428 * that assumes that we can receive packets from the
429 * NIC faster than they arrive.
431 if ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
432 DBGC ( netdev, "NETDEV %p processing %p\n",
434 netdev->ll_protocol->rx ( iobuf, netdev );
439 /** Networking stack process */
440 static struct process net_process = {
444 /** Initialise the networking stack process */
445 static void init_net ( void ) {
446 process_add ( &net_process );
449 INIT_FN ( INIT_PROCESS, init_net, NULL, NULL );