These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / net / netdevice.c
1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <byteswap.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <config/general.h>
33 #include <ipxe/if_ether.h>
34 #include <ipxe/iobuf.h>
35 #include <ipxe/tables.h>
36 #include <ipxe/process.h>
37 #include <ipxe/init.h>
38 #include <ipxe/malloc.h>
39 #include <ipxe/device.h>
40 #include <ipxe/errortab.h>
41 #include <ipxe/profile.h>
42 #include <ipxe/fault.h>
43 #include <ipxe/vlan.h>
44 #include <ipxe/netdevice.h>
45
46 /** @file
47  *
48  * Network device management
49  *
50  */
51
52 /** List of network devices */
53 struct list_head net_devices = LIST_HEAD_INIT ( net_devices );
54
55 /** List of open network devices, in reverse order of opening */
56 static struct list_head open_net_devices = LIST_HEAD_INIT ( open_net_devices );
57
58 /** Network device index */
59 static unsigned int netdev_index = 0;
60
61 /** Network polling profiler */
62 static struct profiler net_poll_profiler __profiler = { .name = "net.poll" };
63
64 /** Network receive profiler */
65 static struct profiler net_rx_profiler __profiler = { .name = "net.rx" };
66
67 /** Network transmit profiler */
68 static struct profiler net_tx_profiler __profiler = { .name = "net.tx" };
69
70 /** Default unknown link status code */
71 #define EUNKNOWN_LINK_STATUS __einfo_error ( EINFO_EUNKNOWN_LINK_STATUS )
72 #define EINFO_EUNKNOWN_LINK_STATUS \
73         __einfo_uniqify ( EINFO_EINPROGRESS, 0x01, "Unknown" )
74
75 /** Default not-yet-attempted-configuration status code */
76 #define EUNUSED_CONFIG __einfo_error ( EINFO_EUNUSED_CONFIG )
77 #define EINFO_EUNUSED_CONFIG \
78         __einfo_uniqify ( EINFO_EINPROGRESS, 0x02, "Unused" )
79
80 /** Default configuration-in-progress status code */
81 #define EINPROGRESS_CONFIG __einfo_error ( EINFO_EINPROGRESS_CONFIG )
82 #define EINFO_EINPROGRESS_CONFIG \
83         __einfo_uniqify ( EINFO_EINPROGRESS, 0x03, "Incomplete" )
84
85 /** Default link-down status code */
86 #define ENOTCONN_LINK_DOWN __einfo_error ( EINFO_ENOTCONN_LINK_DOWN )
87 #define EINFO_ENOTCONN_LINK_DOWN \
88         __einfo_uniqify ( EINFO_ENOTCONN, 0x01, "Down" )
89
90 /** Human-readable message for the default link statuses */
91 struct errortab netdev_errors[] __errortab = {
92         __einfo_errortab ( EINFO_EUNKNOWN_LINK_STATUS ),
93         __einfo_errortab ( EINFO_ENOTCONN_LINK_DOWN ),
94         __einfo_errortab ( EINFO_EUNUSED_CONFIG ),
95         __einfo_errortab ( EINFO_EINPROGRESS_CONFIG ),
96 };
97
98 /**
99  * Check whether or not network device has a link-layer address
100  *
101  * @v netdev            Network device
102  * @ret has_ll_addr     Network device has a link-layer address
103  */
104 static int netdev_has_ll_addr ( struct net_device *netdev ) {
105         uint8_t *ll_addr = netdev->ll_addr;
106         size_t remaining = sizeof ( netdev->ll_addr );
107
108         while ( remaining-- ) {
109                 if ( *(ll_addr++) != 0 )
110                         return 1;
111         }
112         return 0;
113 }
114
115 /**
116  * Notify drivers of network device or link state change
117  *
118  * @v netdev            Network device
119  */
120 static void netdev_notify ( struct net_device *netdev ) {
121         struct net_driver *driver;
122
123         for_each_table_entry ( driver, NET_DRIVERS ) {
124                 if ( driver->notify )
125                         driver->notify ( netdev );
126         }
127 }
128
129 /**
130  * Freeze network device receive queue processing
131  *
132  * @v netdev            Network device
133  */
134 void netdev_rx_freeze ( struct net_device *netdev ) {
135
136         /* Mark receive queue processing as frozen */
137         netdev->state |= NETDEV_RX_FROZEN;
138
139         /* Notify drivers of change */
140         netdev_notify ( netdev );
141 }
142
143 /**
144  * Unfreeze network device receive queue processing
145  *
146  * @v netdev            Network device
147  */
148 void netdev_rx_unfreeze ( struct net_device *netdev ) {
149
150         /* Mark receive queue processing as not frozen */
151         netdev->state &= ~NETDEV_RX_FROZEN;
152
153         /* Notify drivers of change */
154         netdev_notify ( netdev );
155 }
156
157 /**
158  * Mark network device as having a specific link state
159  *
160  * @v netdev            Network device
161  * @v rc                Link status code
162  */
163 void netdev_link_err ( struct net_device *netdev, int rc ) {
164
165         /* Stop link block timer */
166         stop_timer ( &netdev->link_block );
167
168         /* Record link state */
169         netdev->link_rc = rc;
170         if ( netdev->link_rc == 0 ) {
171                 DBGC ( netdev, "NETDEV %s link is up\n", netdev->name );
172         } else {
173                 DBGC ( netdev, "NETDEV %s link is down: %s\n",
174                        netdev->name, strerror ( netdev->link_rc ) );
175         }
176
177         /* Notify drivers of link state change */
178         netdev_notify ( netdev );
179 }
180
181 /**
182  * Mark network device as having link down
183  *
184  * @v netdev            Network device
185  */
186 void netdev_link_down ( struct net_device *netdev ) {
187
188         /* Avoid clobbering a more detailed link status code, if one
189          * is already set.
190          */
191         if ( ( netdev->link_rc == 0 ) ||
192              ( netdev->link_rc == -EUNKNOWN_LINK_STATUS ) ) {
193                 netdev_link_err ( netdev, -ENOTCONN_LINK_DOWN );
194         }
195 }
196
197 /**
198  * Mark network device link as being blocked
199  *
200  * @v netdev            Network device
201  * @v timeout           Timeout (in ticks)
202  */
203 void netdev_link_block ( struct net_device *netdev, unsigned long timeout ) {
204
205         /* Start link block timer */
206         if ( ! netdev_link_blocked ( netdev ) ) {
207                 DBGC ( netdev, "NETDEV %s link blocked for %ld ticks\n",
208                        netdev->name, timeout );
209         }
210         start_timer_fixed ( &netdev->link_block, timeout );
211 }
212
213 /**
214  * Mark network device link as being unblocked
215  *
216  * @v netdev            Network device
217  */
218 void netdev_link_unblock ( struct net_device *netdev ) {
219
220         /* Stop link block timer */
221         if ( netdev_link_blocked ( netdev ) )
222                 DBGC ( netdev, "NETDEV %s link unblocked\n", netdev->name );
223         stop_timer ( &netdev->link_block );
224 }
225
226 /**
227  * Handle network device link block timer expiry
228  *
229  * @v timer             Link block timer
230  * @v fail              Failure indicator
231  */
232 static void netdev_link_block_expired ( struct retry_timer *timer,
233                                         int fail __unused ) {
234         struct net_device *netdev =
235                 container_of ( timer, struct net_device, link_block );
236
237         /* Assume link is no longer blocked */
238         DBGC ( netdev, "NETDEV %s link block expired\n", netdev->name );
239 }
240
241 /**
242  * Record network device statistic
243  *
244  * @v stats             Network device statistics
245  * @v rc                Status code
246  */
247 static void netdev_record_stat ( struct net_device_stats *stats, int rc ) {
248         struct net_device_error *error;
249         struct net_device_error *least_common_error;
250         unsigned int i;
251
252         /* If this is not an error, just update the good counter */
253         if ( rc == 0 ) {
254                 stats->good++;
255                 return;
256         }
257
258         /* Update the bad counter */
259         stats->bad++;
260
261         /* Locate the appropriate error record */
262         least_common_error = &stats->errors[0];
263         for ( i = 0 ; i < ( sizeof ( stats->errors ) /
264                             sizeof ( stats->errors[0] ) ) ; i++ ) {
265                 error = &stats->errors[i];
266                 /* Update matching record, if found */
267                 if ( error->rc == rc ) {
268                         error->count++;
269                         return;
270                 }
271                 if ( error->count < least_common_error->count )
272                         least_common_error = error;
273         }
274
275         /* Overwrite the least common error record */
276         least_common_error->rc = rc;
277         least_common_error->count = 1;
278 }
279
280 /**
281  * Transmit raw packet via network device
282  *
283  * @v netdev            Network device
284  * @v iobuf             I/O buffer
285  * @ret rc              Return status code
286  *
287  * Transmits the packet via the specified network device.  This
288  * function takes ownership of the I/O buffer.
289  */
290 int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf ) {
291         int rc;
292
293         DBGC2 ( netdev, "NETDEV %s transmitting %p (%p+%zx)\n",
294                 netdev->name, iobuf, iobuf->data, iob_len ( iobuf ) );
295         profile_start ( &net_tx_profiler );
296
297         /* Enqueue packet */
298         list_add_tail ( &iobuf->list, &netdev->tx_queue );
299
300         /* Avoid calling transmit() on unopened network devices */
301         if ( ! netdev_is_open ( netdev ) ) {
302                 rc = -ENETUNREACH;
303                 goto err;
304         }
305
306         /* Discard packet (for test purposes) if applicable */
307         if ( ( rc = inject_fault ( NETDEV_DISCARD_RATE ) ) != 0 )
308                 goto err;
309
310         /* Transmit packet */
311         if ( ( rc = netdev->op->transmit ( netdev, iobuf ) ) != 0 )
312                 goto err;
313
314         profile_stop ( &net_tx_profiler );
315         return 0;
316
317  err:
318         netdev_tx_complete_err ( netdev, iobuf, rc );
319         return rc;
320 }
321
322 /**
323  * Defer transmitted packet
324  *
325  * @v netdev            Network device
326  * @v iobuf             I/O buffer
327  *
328  * Drivers may call netdev_tx_defer() if there is insufficient space
329  * in the transmit descriptor ring.  Any packets deferred in this way
330  * will be automatically retransmitted as soon as space becomes
331  * available (i.e. as soon as the driver calls netdev_tx_complete()).
332  *
333  * The packet must currently be in the network device's TX queue.
334  *
335  * Drivers utilising netdev_tx_defer() must ensure that space in the
336  * transmit descriptor ring is freed up @b before calling
337  * netdev_tx_complete().  For example, if the ring is modelled using a
338  * producer counter and a consumer counter, then the consumer counter
339  * must be incremented before the call to netdev_tx_complete().
340  * Failure to do this will cause the retransmitted packet to be
341  * immediately redeferred (which will result in out-of-order
342  * transmissions and other nastiness).
343  */
344 void netdev_tx_defer ( struct net_device *netdev, struct io_buffer *iobuf ) {
345
346         /* Catch data corruption as early as possible */
347         list_check_contains_entry ( iobuf, &netdev->tx_queue, list );
348
349         /* Remove from transmit queue */
350         list_del ( &iobuf->list );
351
352         /* Add to deferred transmit queue */
353         list_add_tail ( &iobuf->list, &netdev->tx_deferred );
354
355         /* Record "out of space" statistic */
356         netdev_tx_err ( netdev, NULL, -ENOBUFS );
357 }
358
359 /**
360  * Discard transmitted packet
361  *
362  * @v netdev            Network device
363  * @v iobuf             I/O buffer, or NULL
364  * @v rc                Packet status code
365  *
366  * The packet is discarded and a TX error is recorded.  This function
367  * takes ownership of the I/O buffer.
368  */
369 void netdev_tx_err ( struct net_device *netdev,
370                      struct io_buffer *iobuf, int rc ) {
371
372         /* Update statistics counter */
373         netdev_record_stat ( &netdev->tx_stats, rc );
374         if ( rc == 0 ) {
375                 DBGC2 ( netdev, "NETDEV %s transmission %p complete\n",
376                         netdev->name, iobuf );
377         } else {
378                 DBGC ( netdev, "NETDEV %s transmission %p failed: %s\n",
379                        netdev->name, iobuf, strerror ( rc ) );
380         }
381
382         /* Discard packet */
383         free_iob ( iobuf );
384 }
385
386 /**
387  * Complete network transmission
388  *
389  * @v netdev            Network device
390  * @v iobuf             I/O buffer
391  * @v rc                Packet status code
392  *
393  * The packet must currently be in the network device's TX queue.
394  */
395 void netdev_tx_complete_err ( struct net_device *netdev,
396                               struct io_buffer *iobuf, int rc ) {
397
398         /* Catch data corruption as early as possible */
399         list_check_contains_entry ( iobuf, &netdev->tx_queue, list );
400
401         /* Dequeue and free I/O buffer */
402         list_del ( &iobuf->list );
403         netdev_tx_err ( netdev, iobuf, rc );
404
405         /* Transmit first pending packet, if any */
406         if ( ( iobuf = list_first_entry ( &netdev->tx_deferred,
407                                           struct io_buffer, list ) ) != NULL ) {
408                 list_del ( &iobuf->list );
409                 netdev_tx ( netdev, iobuf );
410         }
411 }
412
413 /**
414  * Complete network transmission
415  *
416  * @v netdev            Network device
417  * @v rc                Packet status code
418  *
419  * Completes the oldest outstanding packet in the TX queue.
420  */
421 void netdev_tx_complete_next_err ( struct net_device *netdev, int rc ) {
422         struct io_buffer *iobuf;
423
424         if ( ( iobuf = list_first_entry ( &netdev->tx_queue, struct io_buffer,
425                                           list ) ) != NULL ) {
426                 netdev_tx_complete_err ( netdev, iobuf, rc );
427         }
428 }
429
430 /**
431  * Flush device's transmit queue
432  *
433  * @v netdev            Network device
434  */
435 static void netdev_tx_flush ( struct net_device *netdev ) {
436
437         /* Discard any packets in the TX queue.  This will also cause
438          * any packets in the deferred TX queue to be discarded
439          * automatically.
440          */
441         while ( ! list_empty ( &netdev->tx_queue ) ) {
442                 netdev_tx_complete_next_err ( netdev, -ECANCELED );
443         }
444         assert ( list_empty ( &netdev->tx_queue ) );
445         assert ( list_empty ( &netdev->tx_deferred ) );
446 }
447
448 /**
449  * Add packet to receive queue
450  *
451  * @v netdev            Network device
452  * @v iobuf             I/O buffer, or NULL
453  *
454  * The packet is added to the network device's RX queue.  This
455  * function takes ownership of the I/O buffer.
456  */
457 void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
458         int rc;
459
460         DBGC2 ( netdev, "NETDEV %s received %p (%p+%zx)\n",
461                 netdev->name, iobuf, iobuf->data, iob_len ( iobuf ) );
462
463         /* Discard packet (for test purposes) if applicable */
464         if ( ( rc = inject_fault ( NETDEV_DISCARD_RATE ) ) != 0 ) {
465                 netdev_rx_err ( netdev, iobuf, rc );
466                 return;
467         }
468
469         /* Enqueue packet */
470         list_add_tail ( &iobuf->list, &netdev->rx_queue );
471
472         /* Update statistics counter */
473         netdev_record_stat ( &netdev->rx_stats, 0 );
474 }
475
476 /**
477  * Discard received packet
478  *
479  * @v netdev            Network device
480  * @v iobuf             I/O buffer, or NULL
481  * @v rc                Packet status code
482  *
483  * The packet is discarded and an RX error is recorded.  This function
484  * takes ownership of the I/O buffer.  @c iobuf may be NULL if, for
485  * example, the net device wishes to report an error due to being
486  * unable to allocate an I/O buffer.
487  */
488 void netdev_rx_err ( struct net_device *netdev,
489                      struct io_buffer *iobuf, int rc ) {
490
491         DBGC ( netdev, "NETDEV %s failed to receive %p: %s\n",
492                netdev->name, iobuf, strerror ( rc ) );
493
494         /* Discard packet */
495         free_iob ( iobuf );
496
497         /* Update statistics counter */
498         netdev_record_stat ( &netdev->rx_stats, rc );
499 }
500
501 /**
502  * Poll for completed and received packets on network device
503  *
504  * @v netdev            Network device
505  *
506  * Polls the network device for completed transmissions and received
507  * packets.  Any received packets will be added to the RX packet queue
508  * via netdev_rx().
509  */
510 void netdev_poll ( struct net_device *netdev ) {
511
512         if ( netdev_is_open ( netdev ) )
513                 netdev->op->poll ( netdev );
514 }
515
516 /**
517  * Remove packet from device's receive queue
518  *
519  * @v netdev            Network device
520  * @ret iobuf           I/O buffer, or NULL
521  *
522  * Removes the first packet from the device's RX queue and returns it.
523  * Ownership of the packet is transferred to the caller.
524  */
525 struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev ) {
526         struct io_buffer *iobuf;
527
528         iobuf = list_first_entry ( &netdev->rx_queue, struct io_buffer, list );
529         if ( ! iobuf )
530                 return NULL;
531
532         list_del ( &iobuf->list );
533         return iobuf;
534 }
535
536 /**
537  * Flush device's receive queue
538  *
539  * @v netdev            Network device
540  */
541 static void netdev_rx_flush ( struct net_device *netdev ) {
542         struct io_buffer *iobuf;
543
544         /* Discard any packets in the RX queue */
545         while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
546                 netdev_rx_err ( netdev, iobuf, -ECANCELED );
547         }
548 }
549
550 /**
551  * Finish network device configuration
552  *
553  * @v config            Network device configuration
554  * @v rc                Reason for completion
555  */
556 static void netdev_config_close ( struct net_device_configuration *config,
557                                   int rc ) {
558         struct net_device_configurator *configurator = config->configurator;
559         struct net_device *netdev = config->netdev;
560
561         /* Restart interface */
562         intf_restart ( &config->job, rc );
563
564         /* Record configuration result */
565         config->rc = rc;
566         if ( rc == 0 ) {
567                 DBGC ( netdev, "NETDEV %s configured via %s\n",
568                        netdev->name, configurator->name );
569         } else {
570                 DBGC ( netdev, "NETDEV %s configuration via %s failed: %s\n",
571                        netdev->name, configurator->name, strerror ( rc ) );
572         }
573 }
574
575 /** Network device configuration interface operations */
576 static struct interface_operation netdev_config_ops[] = {
577         INTF_OP ( intf_close, struct net_device_configuration *,
578                   netdev_config_close ),
579 };
580
581 /** Network device configuration interface descriptor */
582 static struct interface_descriptor netdev_config_desc =
583         INTF_DESC ( struct net_device_configuration, job, netdev_config_ops );
584
585 /**
586  * Free network device
587  *
588  * @v refcnt            Network device reference counter
589  */
590 static void free_netdev ( struct refcnt *refcnt ) {
591         struct net_device *netdev =
592                 container_of ( refcnt, struct net_device, refcnt );
593
594         stop_timer ( &netdev->link_block );
595         netdev_tx_flush ( netdev );
596         netdev_rx_flush ( netdev );
597         clear_settings ( netdev_settings ( netdev ) );
598         free ( netdev );
599 }
600
601 /**
602  * Allocate network device
603  *
604  * @v priv_len          Length of private data area (net_device::priv)
605  * @ret netdev          Network device, or NULL
606  *
607  * Allocates space for a network device and its private data area.
608  */
609 struct net_device * alloc_netdev ( size_t priv_len ) {
610         struct net_device *netdev;
611         struct net_device_configurator *configurator;
612         struct net_device_configuration *config;
613         unsigned int num_configs;
614         size_t confs_len;
615         size_t total_len;
616
617         num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
618         confs_len = ( num_configs * sizeof ( netdev->configs[0] ) );
619         total_len = ( sizeof ( *netdev ) + confs_len + priv_len );
620         netdev = zalloc ( total_len );
621         if ( netdev ) {
622                 ref_init ( &netdev->refcnt, free_netdev );
623                 netdev->link_rc = -EUNKNOWN_LINK_STATUS;
624                 timer_init ( &netdev->link_block, netdev_link_block_expired,
625                              &netdev->refcnt );
626                 INIT_LIST_HEAD ( &netdev->tx_queue );
627                 INIT_LIST_HEAD ( &netdev->tx_deferred );
628                 INIT_LIST_HEAD ( &netdev->rx_queue );
629                 netdev_settings_init ( netdev );
630                 config = netdev->configs;
631                 for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ){
632                         config->netdev = netdev;
633                         config->configurator = configurator;
634                         config->rc = -EUNUSED_CONFIG;
635                         intf_init ( &config->job, &netdev_config_desc,
636                                     &netdev->refcnt );
637                         config++;
638                 }
639                 netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) +
640                                  confs_len );
641         }
642         return netdev;
643 }
644
645 /**
646  * Register network device
647  *
648  * @v netdev            Network device
649  * @ret rc              Return status code
650  *
651  * Gives the network device a name and adds it to the list of network
652  * devices.
653  */
654 int register_netdev ( struct net_device *netdev ) {
655         struct ll_protocol *ll_protocol = netdev->ll_protocol;
656         struct net_driver *driver;
657         struct net_device *duplicate;
658         uint32_t seed;
659         int rc;
660
661         /* Set initial link-layer address, if not already set */
662         if ( ! netdev_has_ll_addr ( netdev ) ) {
663                 ll_protocol->init_addr ( netdev->hw_addr, netdev->ll_addr );
664         }
665
666         /* Reject network devices that are already available via a
667          * different hardware device.
668          */
669         duplicate = find_netdev_by_ll_addr ( ll_protocol, netdev->ll_addr );
670         if ( duplicate && ( duplicate->dev != netdev->dev ) ) {
671                 DBGC ( netdev, "NETDEV rejecting duplicate (phys %s) of %s "
672                        "(phys %s)\n", netdev->dev->name, duplicate->name,
673                        duplicate->dev->name );
674                 rc = -EEXIST;
675                 goto err_duplicate;
676         }
677
678         /* Record device index and create device name */
679         if ( netdev->name[0] == '\0' ) {
680                 snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
681                            netdev_index );
682         }
683         netdev->index = ++netdev_index;
684
685         /* Use least significant bits of the link-layer address to
686          * improve the randomness of the (non-cryptographic) random
687          * number generator.
688          */
689         memcpy ( &seed, ( netdev->ll_addr + ll_protocol->ll_addr_len
690                           - sizeof ( seed ) ), sizeof ( seed ) );
691         srand ( rand() ^ seed );
692
693         /* Add to device list */
694         netdev_get ( netdev );
695         list_add_tail ( &netdev->list, &net_devices );
696         DBGC ( netdev, "NETDEV %s registered (phys %s hwaddr %s)\n",
697                netdev->name, netdev->dev->name,
698                netdev_addr ( netdev ) );
699
700         /* Register per-netdev configuration settings */
701         if ( ( rc = register_settings ( netdev_settings ( netdev ),
702                                         NULL, netdev->name ) ) != 0 ) {
703                 DBGC ( netdev, "NETDEV %s could not register settings: %s\n",
704                        netdev->name, strerror ( rc ) );
705                 goto err_register_settings;
706         }
707
708         /* Probe device */
709         for_each_table_entry ( driver, NET_DRIVERS ) {
710                 if ( driver->probe && ( rc = driver->probe ( netdev ) ) != 0 ) {
711                         DBGC ( netdev, "NETDEV %s could not add %s device: "
712                                "%s\n", netdev->name, driver->name,
713                                strerror ( rc ) );
714                         goto err_probe;
715                 }
716         }
717
718         return 0;
719
720  err_probe:
721         for_each_table_entry_continue_reverse ( driver, NET_DRIVERS ) {
722                 if ( driver->remove )
723                         driver->remove ( netdev );
724         }
725         clear_settings ( netdev_settings ( netdev ) );
726         unregister_settings ( netdev_settings ( netdev ) );
727  err_register_settings:
728  err_duplicate:
729         return rc;
730 }
731
732 /**
733  * Open network device
734  *
735  * @v netdev            Network device
736  * @ret rc              Return status code
737  */
738 int netdev_open ( struct net_device *netdev ) {
739         int rc;
740
741         /* Do nothing if device is already open */
742         if ( netdev->state & NETDEV_OPEN )
743                 return 0;
744
745         DBGC ( netdev, "NETDEV %s opening\n", netdev->name );
746
747         /* Mark as opened */
748         netdev->state |= NETDEV_OPEN;
749
750         /* Open the device */
751         if ( ( rc = netdev->op->open ( netdev ) ) != 0 )
752                 goto err;
753
754         /* Add to head of open devices list */
755         list_add ( &netdev->open_list, &open_net_devices );
756
757         /* Notify drivers of device state change */
758         netdev_notify ( netdev );
759
760         return 0;
761
762  err:
763         netdev->state &= ~NETDEV_OPEN;
764         return rc;
765 }
766
767 /**
768  * Close network device
769  *
770  * @v netdev            Network device
771  */
772 void netdev_close ( struct net_device *netdev ) {
773         unsigned int num_configs;
774         unsigned int i;
775
776         /* Do nothing if device is already closed */
777         if ( ! ( netdev->state & NETDEV_OPEN ) )
778                 return;
779
780         DBGC ( netdev, "NETDEV %s closing\n", netdev->name );
781
782         /* Terminate any ongoing configurations.  Use intf_close()
783          * rather than intf_restart() to allow the cancellation to be
784          * reported back to us if a configuration is actually in
785          * progress.
786          */
787         num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
788         for ( i = 0 ; i < num_configs ; i++ )
789                 intf_close ( &netdev->configs[i].job, -ECANCELED );
790
791         /* Remove from open devices list */
792         list_del ( &netdev->open_list );
793
794         /* Mark as closed */
795         netdev->state &= ~NETDEV_OPEN;
796
797         /* Notify drivers of device state change */
798         netdev_notify ( netdev );
799
800         /* Close the device */
801         netdev->op->close ( netdev );
802
803         /* Flush TX and RX queues */
804         netdev_tx_flush ( netdev );
805         netdev_rx_flush ( netdev );
806 }
807
808 /**
809  * Unregister network device
810  *
811  * @v netdev            Network device
812  *
813  * Removes the network device from the list of network devices.
814  */
815 void unregister_netdev ( struct net_device *netdev ) {
816         struct net_driver *driver;
817
818         /* Ensure device is closed */
819         netdev_close ( netdev );
820
821         /* Remove device */
822         for_each_table_entry_reverse ( driver, NET_DRIVERS ) {
823                 if ( driver->remove )
824                         driver->remove ( netdev );
825         }
826
827         /* Unregister per-netdev configuration settings */
828         clear_settings ( netdev_settings ( netdev ) );
829         unregister_settings ( netdev_settings ( netdev ) );
830
831         /* Remove from device list */
832         DBGC ( netdev, "NETDEV %s unregistered\n", netdev->name );
833         list_del ( &netdev->list );
834         netdev_put ( netdev );
835
836         /* Reset network device index if no devices remain */
837         if ( list_empty ( &net_devices ) )
838                 netdev_index = 0;
839 }
840
841 /** Enable or disable interrupts
842  *
843  * @v netdev            Network device
844  * @v enable            Interrupts should be enabled
845  */
846 void netdev_irq ( struct net_device *netdev, int enable ) {
847
848         /* Do nothing if device does not support interrupts */
849         if ( ! netdev_irq_supported ( netdev ) )
850                 return;
851
852         /* Enable or disable device interrupts */
853         netdev->op->irq ( netdev, enable );
854
855         /* Record interrupt enabled state */
856         netdev->state &= ~NETDEV_IRQ_ENABLED;
857         if ( enable )
858                 netdev->state |= NETDEV_IRQ_ENABLED;
859 }
860
861 /**
862  * Get network device by name
863  *
864  * @v name              Network device name
865  * @ret netdev          Network device, or NULL
866  */
867 struct net_device * find_netdev ( const char *name ) {
868         struct net_device *netdev;
869
870         /* Allow "netX" shortcut */
871         if ( strcmp ( name, "netX" ) == 0 )
872                 return last_opened_netdev();
873
874         /* Identify network device by name */
875         list_for_each_entry ( netdev, &net_devices, list ) {
876                 if ( strcmp ( netdev->name, name ) == 0 )
877                         return netdev;
878         }
879
880         return NULL;
881 }
882
883 /**
884  * Get network device by index
885  *
886  * @v index             Network device index
887  * @ret netdev          Network device, or NULL
888  */
889 struct net_device * find_netdev_by_index ( unsigned int index ) {
890         struct net_device *netdev;
891
892         /* Identify network device by index */
893         list_for_each_entry ( netdev, &net_devices, list ) {
894                 if ( netdev->index == index )
895                         return netdev;
896         }
897
898         return NULL;
899 }
900
901 /**
902  * Get network device by PCI bus:dev.fn address
903  *
904  * @v bus_type          Bus type
905  * @v location          Bus location
906  * @ret netdev          Network device, or NULL
907  */
908 struct net_device * find_netdev_by_location ( unsigned int bus_type,
909                                               unsigned int location ) {
910         struct net_device *netdev;
911
912         list_for_each_entry ( netdev, &net_devices, list ) {
913                 if ( ( netdev->dev->desc.bus_type == bus_type ) &&
914                      ( netdev->dev->desc.location == location ) )
915                         return netdev;
916         }
917
918         return NULL;    
919 }
920
921 /**
922  * Get network device by link-layer address
923  *
924  * @v ll_protocol       Link-layer protocol
925  * @v ll_addr           Link-layer address
926  * @ret netdev          Network device, or NULL
927  */
928 struct net_device * find_netdev_by_ll_addr ( struct ll_protocol *ll_protocol,
929                                              const void *ll_addr ) {
930         struct net_device *netdev;
931
932         list_for_each_entry ( netdev, &net_devices, list ) {
933                 if ( ( netdev->ll_protocol == ll_protocol ) &&
934                      ( memcmp ( netdev->ll_addr, ll_addr,
935                                 ll_protocol->ll_addr_len ) == 0 ) )
936                         return netdev;
937         }
938
939         return NULL;
940 }
941
942 /**
943  * Get most recently opened network device
944  *
945  * @ret netdev          Most recently opened network device, or NULL
946  */
947 struct net_device * last_opened_netdev ( void ) {
948         struct net_device *netdev;
949
950         netdev = list_first_entry ( &open_net_devices, struct net_device,
951                                     open_list );
952         if ( ! netdev )
953                 return NULL;
954
955         assert ( netdev_is_open ( netdev ) );
956         return netdev;
957 }
958
959 /**
960  * Transmit network-layer packet
961  *
962  * @v iobuf             I/O buffer
963  * @v netdev            Network device
964  * @v net_protocol      Network-layer protocol
965  * @v ll_dest           Destination link-layer address
966  * @v ll_source         Source link-layer address
967  * @ret rc              Return status code
968  *
969  * Prepends link-layer headers to the I/O buffer and transmits the
970  * packet via the specified network device.  This function takes
971  * ownership of the I/O buffer.
972  */
973 int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
974              struct net_protocol *net_protocol, const void *ll_dest,
975              const void *ll_source ) {
976         struct ll_protocol *ll_protocol = netdev->ll_protocol;
977         int rc;
978
979         /* Add link-layer header */
980         if ( ( rc = ll_protocol->push ( netdev, iobuf, ll_dest, ll_source,
981                                         net_protocol->net_proto ) ) != 0 ) {
982                 /* Record error for diagnosis */
983                 netdev_tx_err ( netdev, iobuf, rc );
984                 return rc;
985         }
986
987         /* Transmit packet */
988         return netdev_tx ( netdev, iobuf );
989 }
990
991 /**
992  * Process received network-layer packet
993  *
994  * @v iobuf             I/O buffer
995  * @v netdev            Network device
996  * @v net_proto         Network-layer protocol, in network-byte order
997  * @v ll_dest           Destination link-layer address
998  * @v ll_source         Source link-layer address
999  * @v flags             Packet flags
1000  * @ret rc              Return status code
1001  */
1002 int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
1003              uint16_t net_proto, const void *ll_dest, const void *ll_source,
1004              unsigned int flags ) {
1005         struct net_protocol *net_protocol;
1006
1007         /* Hand off to network-layer protocol, if any */
1008         for_each_table_entry ( net_protocol, NET_PROTOCOLS ) {
1009                 if ( net_protocol->net_proto == net_proto )
1010                         return net_protocol->rx ( iobuf, netdev, ll_dest,
1011                                                   ll_source, flags );
1012         }
1013
1014         DBGC ( netdev, "NETDEV %s unknown network protocol %04x\n",
1015                netdev->name, ntohs ( net_proto ) );
1016         free_iob ( iobuf );
1017         return -ENOTSUP;
1018 }
1019
1020 /**
1021  * Poll the network stack
1022  *
1023  * This polls all interfaces for received packets, and processes
1024  * packets from the RX queue.
1025  */
1026 void net_poll ( void ) {
1027         struct net_device *netdev;
1028         struct io_buffer *iobuf;
1029         struct ll_protocol *ll_protocol;
1030         const void *ll_dest;
1031         const void *ll_source;
1032         uint16_t net_proto;
1033         unsigned int flags;
1034         int rc;
1035
1036         /* Poll and process each network device */
1037         list_for_each_entry ( netdev, &net_devices, list ) {
1038
1039                 /* Poll for new packets */
1040                 profile_start ( &net_poll_profiler );
1041                 netdev_poll ( netdev );
1042                 profile_stop ( &net_poll_profiler );
1043
1044                 /* Leave received packets on the queue if receive
1045                  * queue processing is currently frozen.  This will
1046                  * happen when the raw packets are to be manually
1047                  * dequeued using netdev_rx_dequeue(), rather than
1048                  * processed via the usual networking stack.
1049                  */
1050                 if ( netdev_rx_frozen ( netdev ) )
1051                         continue;
1052
1053                 /* Process all received packets */
1054                 while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
1055
1056                         DBGC2 ( netdev, "NETDEV %s processing %p (%p+%zx)\n",
1057                                 netdev->name, iobuf, iobuf->data,
1058                                 iob_len ( iobuf ) );
1059                         profile_start ( &net_rx_profiler );
1060
1061                         /* Remove link-layer header */
1062                         ll_protocol = netdev->ll_protocol;
1063                         if ( ( rc = ll_protocol->pull ( netdev, iobuf,
1064                                                         &ll_dest, &ll_source,
1065                                                         &net_proto,
1066                                                         &flags ) ) != 0 ) {
1067                                 free_iob ( iobuf );
1068                                 continue;
1069                         }
1070
1071                         /* Hand packet to network layer */
1072                         if ( ( rc = net_rx ( iob_disown ( iobuf ), netdev,
1073                                              net_proto, ll_dest,
1074                                              ll_source, flags ) ) != 0 ) {
1075                                 /* Record error for diagnosis */
1076                                 netdev_rx_err ( netdev, NULL, rc );
1077                         }
1078                         profile_stop ( &net_rx_profiler );
1079                 }
1080         }
1081 }
1082
1083 /**
1084  * Single-step the network stack
1085  *
1086  * @v process           Network stack process
1087  */
1088 static void net_step ( struct process *process __unused ) {
1089         net_poll();
1090 }
1091
1092 /**
1093  * Get the VLAN tag (when VLAN support is not present)
1094  *
1095  * @v netdev            Network device
1096  * @ret tag             0, indicating that device is not a VLAN device
1097  */
1098 __weak unsigned int vlan_tag ( struct net_device *netdev __unused ) {
1099         return 0;
1100 }
1101
1102 /**
1103  * Identify VLAN device (when VLAN support is not present)
1104  *
1105  * @v trunk             Trunk network device
1106  * @v tag               VLAN tag
1107  * @ret netdev          VLAN device, if any
1108  */
1109 __weak struct net_device * vlan_find ( struct net_device *trunk __unused,
1110                                        unsigned int tag __unused ) {
1111         return NULL;
1112 }
1113
1114 /** Networking stack process */
1115 PERMANENT_PROCESS ( net_process, net_step );
1116
1117 /**
1118  * Discard some cached network device data
1119  *
1120  * @ret discarded       Number of cached items discarded
1121  */
1122 static unsigned int net_discard ( void ) {
1123         struct net_device *netdev;
1124         struct io_buffer *iobuf;
1125         unsigned int discarded = 0;
1126
1127         /* Try to drop one deferred TX packet from each network device */
1128         for_each_netdev ( netdev ) {
1129                 if ( ( iobuf = list_first_entry ( &netdev->tx_deferred,
1130                                                   struct io_buffer,
1131                                                   list ) ) != NULL ) {
1132
1133                         /* Discard first deferred packet */
1134                         list_del ( &iobuf->list );
1135                         free_iob ( iobuf );
1136
1137                         /* Report discard */
1138                         discarded++;
1139                 }
1140         }
1141
1142         return discarded;
1143 }
1144
1145 /** Network device cache discarder */
1146 struct cache_discarder net_discarder __cache_discarder ( CACHE_NORMAL ) = {
1147         .discard = net_discard,
1148 };
1149
1150 /**
1151  * Find network device configurator
1152  *
1153  * @v name              Name
1154  * @ret configurator    Network device configurator, or NULL
1155  */
1156 struct net_device_configurator * find_netdev_configurator ( const char *name ) {
1157         struct net_device_configurator *configurator;
1158
1159         for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ) {
1160                 if ( strcmp ( configurator->name, name ) == 0 )
1161                         return configurator;
1162         }
1163         return NULL;
1164 }
1165
1166 /**
1167  * Start network device configuration
1168  *
1169  * @v netdev            Network device
1170  * @v configurator      Network device configurator
1171  * @ret rc              Return status code
1172  */
1173 int netdev_configure ( struct net_device *netdev,
1174                        struct net_device_configurator *configurator ) {
1175         struct net_device_configuration *config =
1176                 netdev_configuration ( netdev, configurator );
1177         int rc;
1178
1179         /* Check applicability of configurator */
1180         if ( ! netdev_configurator_applies ( netdev, configurator ) ) {
1181                 DBGC ( netdev, "NETDEV %s does not support configuration via "
1182                        "%s\n", netdev->name, configurator->name );
1183                 return -ENOTSUP;
1184         }
1185
1186         /* Terminate any ongoing configuration */
1187         intf_restart ( &config->job, -ECANCELED );
1188
1189         /* Mark configuration as being in progress */
1190         config->rc = -EINPROGRESS_CONFIG;
1191
1192         DBGC ( netdev, "NETDEV %s starting configuration via %s\n",
1193                netdev->name, configurator->name );
1194
1195         /* Start configuration */
1196         if ( ( rc = configurator->start ( &config->job, netdev ) ) != 0 ) {
1197                 DBGC ( netdev, "NETDEV %s could not start configuration via "
1198                        "%s: %s\n", netdev->name, configurator->name,
1199                        strerror ( rc ) );
1200                 config->rc = rc;
1201                 return rc;
1202         }
1203
1204         return 0;
1205 }
1206
1207 /**
1208  * Start network device configuration via all supported configurators
1209  *
1210  * @v netdev            Network device
1211  * @ret rc              Return status code
1212  */
1213 int netdev_configure_all ( struct net_device *netdev ) {
1214         struct net_device_configurator *configurator;
1215         int rc;
1216
1217         /* Start configuration for each configurator */
1218         for_each_table_entry ( configurator, NET_DEVICE_CONFIGURATORS ) {
1219
1220                 /* Skip any inapplicable configurators */
1221                 if ( ! netdev_configurator_applies ( netdev, configurator ) )
1222                         continue;
1223
1224                 /* Start configuration */
1225                 if ( ( rc = netdev_configure ( netdev, configurator ) ) != 0 )
1226                         return rc;
1227         }
1228
1229         return 0;
1230 }
1231
1232 /**
1233  * Check if network device has a configuration with a specified status code
1234  *
1235  * @v netdev            Network device
1236  * @v rc                Status code
1237  * @ret has_rc          Network device has a configuration with this status code
1238  */
1239 static int netdev_has_configuration_rc ( struct net_device *netdev, int rc ) {
1240         unsigned int num_configs;
1241         unsigned int i;
1242
1243         num_configs = table_num_entries ( NET_DEVICE_CONFIGURATORS );
1244         for ( i = 0 ; i < num_configs ; i++ ) {
1245                 if ( netdev->configs[i].rc == rc )
1246                         return 1;
1247         }
1248         return 0;
1249 }
1250
1251 /**
1252  * Check if network device configuration is in progress
1253  *
1254  * @v netdev            Network device
1255  * @ret is_in_progress  Network device configuration is in progress
1256  */
1257 int netdev_configuration_in_progress ( struct net_device *netdev ) {
1258
1259         return netdev_has_configuration_rc ( netdev, -EINPROGRESS_CONFIG );
1260 }
1261
1262 /**
1263  * Check if network device has at least one successful configuration
1264  *
1265  * @v netdev            Network device
1266  * @v configurator      Configurator
1267  * @ret rc              Return status code
1268  */
1269 int netdev_configuration_ok ( struct net_device *netdev ) {
1270
1271         return netdev_has_configuration_rc ( netdev, 0 );
1272 }