Add memtest support.
[syslinux-debian/hramrach.git] / gpxe / src / usr / ifmgmt.c
blobd4cc5a5b60e76918937c8098d3182b58528c8196
1 /*
2 * Copyright (C) 2007 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.
19 FILE_LICENCE ( GPL2_OR_LATER );
21 #include <string.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <console.h>
26 #include <gpxe/netdevice.h>
27 #include <gpxe/device.h>
28 #include <gpxe/process.h>
29 #include <gpxe/keys.h>
30 #include <usr/ifmgmt.h>
32 /** @file
34 * Network interface management
38 /**
39 * Open network device
41 * @v netdev Network device
42 * @ret rc Return status code
44 int ifopen ( struct net_device *netdev ) {
45 int rc;
47 if ( ( rc = netdev_open ( netdev ) ) != 0 ) {
48 printf ( "Could not open %s: %s\n",
49 netdev->name, strerror ( rc ) );
50 return rc;
53 return 0;
56 /**
57 * Close network device
59 * @v netdev Network device
61 void ifclose ( struct net_device *netdev ) {
62 netdev_close ( netdev );
65 /**
66 * Print network device error breakdown
68 * @v stats Network device statistics
69 * @v prefix Message prefix
71 static void ifstat_errors ( struct net_device_stats *stats,
72 const char *prefix ) {
73 unsigned int i;
75 for ( i = 0 ; i < ( sizeof ( stats->errors ) /
76 sizeof ( stats->errors[0] ) ) ; i++ ) {
77 if ( stats->errors[i].count )
78 printf ( " [%s: %d x \"%s\"]\n", prefix,
79 stats->errors[i].count,
80 strerror ( stats->errors[i].rc ) );
84 /**
85 * Print status of network device
87 * @v netdev Network device
89 void ifstat ( struct net_device *netdev ) {
90 printf ( "%s: %s on %s (%s)\n"
91 " [Link:%s, TX:%d TXE:%d RX:%d RXE:%d]\n",
92 netdev->name, netdev_addr ( netdev ), netdev->dev->name,
93 ( ( netdev->state & NETDEV_OPEN ) ? "open" : "closed" ),
94 ( netdev_link_ok ( netdev ) ? "up" : "down" ),
95 netdev->tx_stats.good, netdev->tx_stats.bad,
96 netdev->rx_stats.good, netdev->rx_stats.bad );
97 if ( ! netdev_link_ok ( netdev ) ) {
98 printf ( " [Link status: %s]\n",
99 strerror ( netdev->link_rc ) );
101 ifstat_errors ( &netdev->tx_stats, "TXE" );
102 ifstat_errors ( &netdev->rx_stats, "RXE" );
106 * Wait for link-up, with status indication
108 * @v netdev Network device
109 * @v max_wait_ms Maximum time to wait, in ms
111 int iflinkwait ( struct net_device *netdev, unsigned int max_wait_ms ) {
112 int key;
113 int rc;
115 if ( netdev_link_ok ( netdev ) )
116 return 0;
118 printf ( "Waiting for link-up on %s...", netdev->name );
120 while ( 1 ) {
121 if ( netdev_link_ok ( netdev ) ) {
122 rc = 0;
123 break;
125 if ( max_wait_ms-- == 0 ) {
126 rc = netdev->link_rc;
127 break;
129 step();
130 if ( iskey() ) {
131 key = getchar();
132 if ( key == CTRL_C ) {
133 rc = -ECANCELED;
134 break;
137 mdelay ( 1 );
140 if ( rc == 0 ) {
141 printf ( " ok\n" );
142 } else {
143 printf ( " failed: %s\n", strerror ( rc ) );
146 return rc;