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 /** List of network devices */
40 struct list_head net_devices = LIST_HEAD_INIT ( net_devices );
42 /** List of open network devices, in reverse order of opening */
43 struct list_head open_net_devices = LIST_HEAD_INIT ( open_net_devices );
46 * Record network device statistic
48 * @v stats Network device statistics
51 static void netdev_record_stat ( struct net_device_stats *stats, int rc ) {
52 struct net_device_error *error;
53 struct net_device_error *least_common_error;
56 /* If this is not an error, just update the good counter */
62 /* Update the bad counter */
65 /* Locate the appropriate error record */
66 least_common_error = &stats->errors[0];
67 for ( i = 0 ; i < ( sizeof ( stats->errors ) /
68 sizeof ( stats->errors[0] ) ) ; i++ ) {
69 error = &stats->errors[i];
70 /* Update matching record, if found */
71 if ( error->rc == rc ) {
75 if ( error->count < least_common_error->count )
76 least_common_error = error;
79 /* Overwrite the least common error record */
80 least_common_error->rc = rc;
81 least_common_error->count = 1;
85 * Transmit raw packet via network device
87 * @v netdev Network device
89 * @ret rc Return status code
91 * Transmits the packet via the specified network device. This
92 * function takes ownership of the I/O buffer.
94 int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf ) {
97 DBGC ( netdev, "NETDEV %p transmitting %p (%p+%zx)\n",
98 netdev, iobuf, iobuf->data, iob_len ( iobuf ) );
100 list_add_tail ( &iobuf->list, &netdev->tx_queue );
102 if ( ! ( netdev->state & NETDEV_OPEN ) ) {
107 if ( ( rc = netdev->op->transmit ( netdev, iobuf ) ) != 0 )
113 netdev_tx_complete_err ( netdev, iobuf, rc );
118 * Complete network transmission
120 * @v netdev Network device
121 * @v iobuf I/O buffer
122 * @v rc Packet status code
124 * The packet must currently be in the network device's TX queue.
126 void netdev_tx_complete_err ( struct net_device *netdev,
127 struct io_buffer *iobuf, int rc ) {
129 /* Update statistics counter */
130 netdev_record_stat ( &netdev->tx_stats, rc );
132 DBGC ( netdev, "NETDEV %p transmission %p complete\n",
135 DBGC ( netdev, "NETDEV %p transmission %p failed: %s\n",
136 netdev, iobuf, strerror ( rc ) );
139 /* Catch data corruption as early as possible */
140 assert ( iobuf->list.next != NULL );
141 assert ( iobuf->list.prev != NULL );
143 /* Dequeue and free I/O buffer */
144 list_del ( &iobuf->list );
149 * Complete network transmission
151 * @v netdev Network device
152 * @v rc Packet status code
154 * Completes the oldest outstanding packet in the TX queue.
156 void netdev_tx_complete_next_err ( struct net_device *netdev, int rc ) {
157 struct io_buffer *iobuf;
159 list_for_each_entry ( iobuf, &netdev->tx_queue, list ) {
160 netdev_tx_complete_err ( netdev, iobuf, rc );
166 * Flush device's transmit queue
168 * @v netdev Network device
170 static void netdev_tx_flush ( struct net_device *netdev ) {
172 /* Discard any packets in the TX queue */
173 while ( ! list_empty ( &netdev->tx_queue ) ) {
174 netdev_tx_complete_next_err ( netdev, -ECANCELED );
179 * Add packet to receive queue
181 * @v netdev Network device
182 * @v iobuf I/O buffer, or NULL
184 * The packet is added to the network device's RX queue. This
185 * function takes ownership of the I/O buffer.
187 void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
189 DBGC ( netdev, "NETDEV %p received %p (%p+%zx)\n",
190 netdev, iobuf, iobuf->data, iob_len ( iobuf ) );
193 list_add_tail ( &iobuf->list, &netdev->rx_queue );
195 /* Update statistics counter */
196 netdev_record_stat ( &netdev->rx_stats, 0 );
200 * Discard received packet
202 * @v netdev Network device
203 * @v iobuf I/O buffer, or NULL
204 * @v rc Packet status code
206 * The packet is discarded and an RX error is recorded. This function
207 * takes ownership of the I/O buffer. @c iobuf may be NULL if, for
208 * example, the net device wishes to report an error due to being
209 * unable to allocate an I/O buffer.
211 void netdev_rx_err ( struct net_device *netdev,
212 struct io_buffer *iobuf, int rc ) {
214 DBGC ( netdev, "NETDEV %p failed to receive %p: %s\n",
215 netdev, iobuf, strerror ( rc ) );
220 /* Update statistics counter */
221 netdev_record_stat ( &netdev->rx_stats, rc );
225 * Poll for completed and received packets on network device
227 * @v netdev Network device
229 * Polls the network device for completed transmissions and received
230 * packets. Any received packets will be added to the RX packet queue
233 void netdev_poll ( struct net_device *netdev ) {
235 if ( netdev->state & NETDEV_OPEN )
236 netdev->op->poll ( netdev );
240 * Remove packet from device's receive queue
242 * @v netdev Network device
243 * @ret iobuf I/O buffer, or NULL
245 * Removes the first packet from the device's RX queue and returns it.
246 * Ownership of the packet is transferred to the caller.
248 struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev ) {
249 struct io_buffer *iobuf;
251 list_for_each_entry ( iobuf, &netdev->rx_queue, list ) {
252 list_del ( &iobuf->list );
259 * Flush device's receive queue
261 * @v netdev Network device
263 static void netdev_rx_flush ( struct net_device *netdev ) {
264 struct io_buffer *iobuf;
266 /* Discard any packets in the RX queue */
267 while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
268 netdev_rx_err ( netdev, iobuf, -ECANCELED );
273 * Free network device
275 * @v refcnt Network device reference counter
277 static void free_netdev ( struct refcnt *refcnt ) {
278 struct net_device *netdev =
279 container_of ( refcnt, struct net_device, refcnt );
281 netdev_tx_flush ( netdev );
282 netdev_rx_flush ( netdev );
287 * Allocate network device
289 * @v priv_size Size of private data area (net_device::priv)
290 * @ret netdev Network device, or NULL
292 * Allocates space for a network device and its private data area.
294 struct net_device * alloc_netdev ( size_t priv_size ) {
295 struct net_device *netdev;
298 total_len = ( sizeof ( *netdev ) + priv_size );
299 netdev = zalloc ( total_len );
301 netdev->refcnt.free = free_netdev;
302 INIT_LIST_HEAD ( &netdev->tx_queue );
303 INIT_LIST_HEAD ( &netdev->rx_queue );
304 settings_init ( netdev_settings ( netdev ),
305 &netdev_settings_operations, &netdev->refcnt,
307 netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) );
313 * Register network device
315 * @v netdev Network device
316 * @ret rc Return status code
318 * Gives the network device a name and adds it to the list of network
321 int register_netdev ( struct net_device *netdev ) {
322 static unsigned int ifindex = 0;
325 /* Create device name */
326 snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
329 /* Register per-netdev configuration settings */
330 if ( ( rc = register_settings ( netdev_settings ( netdev ),
332 DBGC ( netdev, "NETDEV %p could not register settings: %s\n",
333 netdev, strerror ( rc ) );
337 /* Add to device list */
338 netdev_get ( netdev );
339 list_add_tail ( &netdev->list, &net_devices );
340 DBGC ( netdev, "NETDEV %p registered as %s (phys %s hwaddr %s)\n",
341 netdev, netdev->name, netdev->dev->name,
342 netdev_hwaddr ( netdev ) );
348 * Open network device
350 * @v netdev Network device
351 * @ret rc Return status code
353 int netdev_open ( struct net_device *netdev ) {
356 /* Do nothing if device is already open */
357 if ( netdev->state & NETDEV_OPEN )
360 DBGC ( netdev, "NETDEV %p opening\n", netdev );
362 /* Open the device */
363 if ( ( rc = netdev->op->open ( netdev ) ) != 0 )
367 netdev->state |= NETDEV_OPEN;
369 /* Add to head of open devices list */
370 list_add ( &netdev->open_list, &open_net_devices );
376 * Close network device
378 * @v netdev Network device
380 void netdev_close ( struct net_device *netdev ) {
382 /* Do nothing if device is already closed */
383 if ( ! ( netdev->state & NETDEV_OPEN ) )
386 DBGC ( netdev, "NETDEV %p closing\n", netdev );
388 /* Close the device */
389 netdev->op->close ( netdev );
391 /* Flush TX and RX queues */
392 netdev_tx_flush ( netdev );
393 netdev_rx_flush ( netdev );
396 netdev->state &= ~NETDEV_OPEN;
398 /* Remove from open devices list */
399 list_del ( &netdev->open_list );
403 * Unregister network device
405 * @v netdev Network device
407 * Removes the network device from the list of network devices.
409 void unregister_netdev ( struct net_device *netdev ) {
411 /* Ensure device is closed */
412 netdev_close ( netdev );
414 /* Unregister per-netdev configuration settings */
415 unregister_settings ( netdev_settings ( netdev ) );
417 /* Remove from device list */
418 list_del ( &netdev->list );
419 netdev_put ( netdev );
420 DBGC ( netdev, "NETDEV %p unregistered\n", netdev );
423 /** Enable or disable interrupts
425 * @v netdev Network device
426 * @v enable Interrupts should be enabled
428 void netdev_irq ( struct net_device *netdev, int enable ) {
429 netdev->op->irq ( netdev, enable );
433 * Get network device by name
435 * @v name Network device name
436 * @ret netdev Network device, or NULL
438 struct net_device * find_netdev ( const char *name ) {
439 struct net_device *netdev;
441 list_for_each_entry ( netdev, &net_devices, list ) {
442 if ( strcmp ( netdev->name, name ) == 0 )
450 * Get network device by PCI bus:dev.fn address
452 * @v bus_type Bus type
453 * @v location Bus location
454 * @ret netdev Network device, or NULL
456 struct net_device * find_netdev_by_location ( unsigned int bus_type,
457 unsigned int location ) {
458 struct net_device *netdev;
460 list_for_each_entry ( netdev, &net_devices, list ) {
461 if ( ( netdev->dev->desc.bus_type == bus_type ) &&
462 ( netdev->dev->desc.location == location ) )
470 * Get most recently opened network device
472 * @ret netdev Most recently opened network device, or NULL
474 struct net_device * last_opened_netdev ( void ) {
475 struct net_device *netdev;
477 list_for_each_entry ( netdev, &open_net_devices, open_list ) {
478 assert ( netdev->state & NETDEV_OPEN );
486 * Transmit network-layer packet
488 * @v iobuf I/O buffer
489 * @v netdev Network device
490 * @v net_protocol Network-layer protocol
491 * @v ll_dest Destination link-layer address
492 * @ret rc Return status code
494 * Prepends link-layer headers to the I/O buffer and transmits the
495 * packet via the specified network device. This function takes
496 * ownership of the I/O buffer.
498 int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
499 struct net_protocol *net_protocol, const void *ll_dest ) {
500 struct ll_protocol *ll_protocol = netdev->ll_protocol;
503 /* Force a poll on the netdevice to (potentially) clear any
504 * backed-up TX completions. This is needed on some network
505 * devices to avoid excessive losses due to small TX ring
508 netdev_poll ( netdev );
510 /* Add link-layer header */
511 if ( ( rc = ll_protocol->push ( iobuf, ll_dest, netdev->ll_addr,
512 net_protocol->net_proto ) ) != 0 ) {
517 /* Transmit packet */
518 return netdev_tx ( netdev, iobuf );
522 * Process received network-layer packet
524 * @v iobuf I/O buffer
525 * @v netdev Network device
526 * @v net_proto Network-layer protocol, in network-byte order
527 * @v ll_source Source link-layer address
528 * @ret rc Return status code
530 int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
531 uint16_t net_proto, const void *ll_source ) {
532 struct net_protocol *net_protocol;
534 /* Hand off to network-layer protocol, if any */
535 for_each_table_entry ( net_protocol, NET_PROTOCOLS ) {
536 if ( net_protocol->net_proto == net_proto )
537 return net_protocol->rx ( iobuf, netdev, ll_source );
540 DBGC ( netdev, "NETDEV %p unknown network protocol %04x\n",
541 netdev, ntohs ( net_proto ) );
547 * Single-step the network stack
549 * @v process Network stack process
551 * This polls all interfaces for received packets, and processes
552 * packets from the RX queue.
554 static void net_step ( struct process *process __unused ) {
555 struct net_device *netdev;
556 struct io_buffer *iobuf;
557 struct ll_protocol *ll_protocol;
559 const void *ll_source;
563 /* Poll and process each network device */
564 list_for_each_entry ( netdev, &net_devices, list ) {
566 /* Poll for new packets */
567 netdev_poll ( netdev );
569 /* Process at most one received packet. Give priority
570 * to getting packets out of the NIC over processing
571 * the received packets, because we advertise a window
572 * that assumes that we can receive packets from the
573 * NIC faster than they arrive.
575 if ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
577 DBGC ( netdev, "NETDEV %p processing %p (%p+%zx)\n",
578 netdev, iobuf, iobuf->data,
581 /* Remove link-layer header */
582 ll_protocol = netdev->ll_protocol;
583 if ( ( rc = ll_protocol->pull ( iobuf, &ll_dest,
585 &net_proto ) ) != 0 ) {
590 net_rx ( iobuf, netdev, net_proto, ll_source );
595 /** Networking stack process */
596 struct process net_process __permanent_process = {