[efi] Disable EFI watchdog timer when shutting down to boot an OS
[ipxe.git] / src / usr / ifmgmt.c
1 /*
2 * Copyright (C) 2007 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 <string.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <ipxe/console.h>
31 #include <ipxe/netdevice.h>
32 #include <ipxe/device.h>
33 #include <ipxe/job.h>
34 #include <ipxe/monojob.h>
35 #include <ipxe/timer.h>
36 #include <ipxe/errortab.h>
37 #include <usr/ifmgmt.h>
38
39 /** @file
40 *
41 * Network interface management
42 *
43 */
44
45 /** Default time to wait for link-up */
46 #define LINK_WAIT_TIMEOUT ( 15 * TICKS_PER_SEC )
47
48 /** Default unsuccessful configuration status code */
49 #define EADDRNOTAVAIL_CONFIG __einfo_error ( EINFO_EADDRNOTAVAIL_CONFIG )
50 #define EINFO_EADDRNOTAVAIL_CONFIG \
51 __einfo_uniqify ( EINFO_EADDRNOTAVAIL, 0x01, \
52 "No configuration methods succeeded" )
53
54 /** Human-readable error message */
55 struct errortab ifmgmt_errors[] __errortab = {
56 __einfo_errortab ( EINFO_EADDRNOTAVAIL_CONFIG ),
57 };
58
59 /**
60 * Open network device
61 *
62 * @v netdev Network device
63 * @ret rc Return status code
64 */
65 int ifopen ( struct net_device *netdev ) {
66 int rc;
67
68 if ( ( rc = netdev_open ( netdev ) ) != 0 ) {
69 printf ( "Could not open %s: %s\n",
70 netdev->name, strerror ( rc ) );
71 return rc;
72 }
73
74 return 0;
75 }
76
77 /**
78 * Close network device
79 *
80 * @v netdev Network device
81 */
82 void ifclose ( struct net_device *netdev ) {
83 netdev_close ( netdev );
84 }
85
86 /**
87 * Print network device error breakdown
88 *
89 * @v stats Network device statistics
90 * @v prefix Message prefix
91 */
92 static void ifstat_errors ( struct net_device_stats *stats,
93 const char *prefix ) {
94 unsigned int i;
95
96 for ( i = 0 ; i < ( sizeof ( stats->errors ) /
97 sizeof ( stats->errors[0] ) ) ; i++ ) {
98 if ( stats->errors[i].count )
99 printf ( " [%s: %d x \"%s\"]\n", prefix,
100 stats->errors[i].count,
101 strerror ( stats->errors[i].rc ) );
102 }
103 }
104
105 /**
106 * Print status of network device
107 *
108 * @v netdev Network device
109 */
110 void ifstat ( struct net_device *netdev ) {
111 printf ( "%s: %s using %s on %s (%s) [%s]\n"
112 " [Link:%s%s, TX:%d TXE:%d RX:%d RXE:%d]\n",
113 netdev->name, netdev_addr ( netdev ),
114 netdev->dev->driver_name, netdev->dev->name,
115 netdev->ll_protocol->name,
116 ( netdev_is_open ( netdev ) ? "open" : "closed" ),
117 ( netdev_link_ok ( netdev ) ? "up" : "down" ),
118 ( netdev_link_blocked ( netdev ) ? " (blocked)" : "" ),
119 netdev->tx_stats.good, netdev->tx_stats.bad,
120 netdev->rx_stats.good, netdev->rx_stats.bad );
121 if ( ! netdev_link_ok ( netdev ) ) {
122 printf ( " [Link status: %s]\n",
123 strerror ( netdev->link_rc ) );
124 }
125 ifstat_errors ( &netdev->tx_stats, "TXE" );
126 ifstat_errors ( &netdev->rx_stats, "RXE" );
127 }
128
129 /** Network device poller */
130 struct ifpoller {
131 /** Job control interface */
132 struct interface job;
133 /** Network device */
134 struct net_device *netdev;
135 /** Network device configurator (if applicable) */
136 struct net_device_configurator *configurator;
137 /**
138 * Check progress
139 *
140 * @v ifpoller Network device poller
141 * @ret ongoing_rc Ongoing job status code (if known)
142 */
143 int ( * progress ) ( struct ifpoller *ifpoller );
144 };
145
146 /**
147 * Report network device poller progress
148 *
149 * @v ifpoller Network device poller
150 * @v progress Progress report to fill in
151 * @ret ongoing_rc Ongoing job status code (if known)
152 */
153 static int ifpoller_progress ( struct ifpoller *ifpoller,
154 struct job_progress *progress __unused ) {
155
156 /* Hand off to current progress checker */
157 return ifpoller->progress ( ifpoller );
158 }
159
160 /** Network device poller operations */
161 static struct interface_operation ifpoller_job_op[] = {
162 INTF_OP ( job_progress, struct ifpoller *, ifpoller_progress ),
163 };
164
165 /** Network device poller descriptor */
166 static struct interface_descriptor ifpoller_job_desc =
167 INTF_DESC ( struct ifpoller, job, ifpoller_job_op );
168
169 /**
170 * Poll network device until completion
171 *
172 * @v netdev Network device
173 * @v configurator Network device configurator (if applicable)
174 * @v timeout Timeout period, in ticks
175 * @v progress Method to check progress
176 * @ret rc Return status code
177 */
178 static int ifpoller_wait ( struct net_device *netdev,
179 struct net_device_configurator *configurator,
180 unsigned long timeout,
181 int ( * progress ) ( struct ifpoller *ifpoller ) ) {
182 static struct ifpoller ifpoller = {
183 .job = INTF_INIT ( ifpoller_job_desc ),
184 };
185
186 ifpoller.netdev = netdev;
187 ifpoller.configurator = configurator;
188 ifpoller.progress = progress;
189 intf_plug_plug ( &monojob, &ifpoller.job );
190 return monojob_wait ( "", timeout );
191 }
192
193 /**
194 * Check link-up progress
195 *
196 * @v ifpoller Network device poller
197 * @ret ongoing_rc Ongoing job status code (if known)
198 */
199 static int iflinkwait_progress ( struct ifpoller *ifpoller ) {
200 struct net_device *netdev = ifpoller->netdev;
201 int ongoing_rc = netdev->link_rc;
202
203 /* Terminate successfully if link is up */
204 if ( ongoing_rc == 0 )
205 intf_close ( &ifpoller->job, 0 );
206
207 /* Otherwise, report link status as ongoing job status */
208 return ongoing_rc;
209 }
210
211 /**
212 * Wait for link-up, with status indication
213 *
214 * @v netdev Network device
215 * @v timeout Timeout period, in ticks
216 * @v verbose Always display progress message
217 * @ret rc Return status code
218 */
219 int iflinkwait ( struct net_device *netdev, unsigned long timeout,
220 int verbose ) {
221 int rc;
222
223 /* Ensure device is open */
224 if ( ( rc = ifopen ( netdev ) ) != 0 )
225 return rc;
226
227 /* Return immediately if link is already up, unless being verbose */
228 netdev_poll ( netdev );
229 if ( netdev_link_ok ( netdev ) && ( ! verbose ) )
230 return 0;
231
232 /* Wait for link-up */
233 printf ( "Waiting for link-up on %s", netdev->name );
234 return ifpoller_wait ( netdev, NULL, timeout, iflinkwait_progress );
235 }
236
237 /**
238 * Check configuration progress
239 *
240 * @v ifpoller Network device poller
241 * @ret ongoing_rc Ongoing job status code (if known)
242 */
243 static int ifconf_progress ( struct ifpoller *ifpoller ) {
244 struct net_device *netdev = ifpoller->netdev;
245 struct net_device_configurator *configurator = ifpoller->configurator;
246 struct net_device_configuration *config;
247 int rc;
248
249 /* Do nothing unless configuration has completed */
250 if ( netdev_configuration_in_progress ( netdev ) )
251 return 0;
252
253 /* Terminate with appropriate overall return status code */
254 if ( configurator ) {
255 config = netdev_configuration ( netdev, configurator );
256 rc = config->rc;
257 } else {
258 rc = ( netdev_configuration_ok ( netdev ) ?
259 0 : -EADDRNOTAVAIL_CONFIG );
260 }
261 intf_close ( &ifpoller->job, rc );
262
263 return rc;
264 }
265
266 /**
267 * Perform network device configuration
268 *
269 * @v netdev Network device
270 * @v configurator Network device configurator, or NULL to use all
271 * @v timeout Timeout period, in ticks
272 * @ret rc Return status code
273 */
274 int ifconf ( struct net_device *netdev,
275 struct net_device_configurator *configurator,
276 unsigned long timeout ) {
277 int rc;
278
279 /* Ensure device is open and link is up */
280 if ( ( rc = iflinkwait ( netdev, LINK_WAIT_TIMEOUT, 0 ) ) != 0 )
281 return rc;
282
283 /* Start configuration */
284 if ( configurator ) {
285 if ( ( rc = netdev_configure ( netdev, configurator ) ) != 0 ) {
286 printf ( "Could not configure %s via %s: %s\n",
287 netdev->name, configurator->name,
288 strerror ( rc ) );
289 return rc;
290 }
291 } else {
292 if ( ( rc = netdev_configure_all ( netdev ) ) != 0 ) {
293 printf ( "Could not configure %s: %s\n",
294 netdev->name, strerror ( rc ) );
295 return rc;
296 }
297 }
298
299 /* Wait for configuration to complete */
300 printf ( "Configuring %s%s%s(%s %s)",
301 ( configurator ? "[" : "" ),
302 ( configurator ? configurator->name : "" ),
303 ( configurator ? "] " : "" ),
304 netdev->name, netdev->ll_protocol->ntoa ( netdev->ll_addr ) );
305 return ifpoller_wait ( netdev, configurator, timeout, ifconf_progress );
306 }