Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / interface / efi / efi_driver.c
1 /*
2  * Copyright (C) 2011 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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <ipxe/version.h>
28 #include <ipxe/efi/efi.h>
29 #include <ipxe/efi/Protocol/DriverBinding.h>
30 #include <ipxe/efi/Protocol/ComponentName2.h>
31 #include <ipxe/efi/Protocol/DevicePath.h>
32 #include <ipxe/efi/efi_strings.h>
33 #include <ipxe/efi/efi_driver.h>
34
35 /** @file
36  *
37  * EFI driver interface
38  *
39  */
40
41 static EFI_DRIVER_BINDING_PROTOCOL efi_driver_binding;
42
43 /** List of controlled EFI devices */
44 static LIST_HEAD ( efi_devices );
45
46 /**
47  * Find EFI device
48  *
49  * @v device            EFI device handle
50  * @ret efidev          EFI device, or NULL if not found
51  */
52 static struct efi_device * efidev_find ( EFI_HANDLE device ) {
53         struct efi_device *efidev;
54
55         /* Look for an existing EFI device */
56         list_for_each_entry ( efidev, &efi_devices, dev.siblings ) {
57                 if ( efidev->device == device )
58                         return efidev;
59         }
60
61         return NULL;
62 }
63
64 /**
65  * Get parent EFI device
66  *
67  * @v dev               Generic device
68  * @ret efidev          Parent EFI device, or NULL
69  */
70 struct efi_device * efidev_parent ( struct device *dev ) {
71         struct device *parent = dev->parent;
72         struct efi_device *efidev;
73
74         /* Check that parent exists and is an EFI device */
75         if ( ! parent )
76                 return NULL;
77         if ( parent->desc.bus_type != BUS_TYPE_EFI )
78                 return NULL;
79
80         /* Get containing EFI device */
81         efidev = container_of ( parent, struct efi_device, dev );
82         return efidev;
83 }
84
85 /**
86  * Check to see if driver supports a device
87  *
88  * @v driver            EFI driver
89  * @v device            EFI device
90  * @v child             Path to child device, if any
91  * @ret efirc           EFI status code
92  */
93 static EFI_STATUS EFIAPI
94 efi_driver_supported ( EFI_DRIVER_BINDING_PROTOCOL *driver __unused,
95                        EFI_HANDLE device, EFI_DEVICE_PATH_PROTOCOL *child ) {
96         struct efi_driver *efidrv;
97         int rc;
98
99         DBGCP ( device, "EFIDRV %p %s DRIVER_SUPPORTED",
100                 device, efi_handle_name ( device ) );
101         if ( child )
102                 DBGCP ( device, " (child %s)", efi_devpath_text ( child ) );
103         DBGCP ( device, "\n" );
104
105         /* Do nothing if we are already driving this device */
106         if ( efidev_find ( device ) != NULL ) {
107                 DBGCP ( device, "EFIDRV %p %s is already started\n",
108                         device, efi_handle_name ( device ) );
109                 return EFI_ALREADY_STARTED;
110         }
111
112         /* Look for a driver claiming to support this device */
113         for_each_table_entry ( efidrv, EFI_DRIVERS ) {
114                 if ( ( rc = efidrv->supported ( device ) ) == 0 ) {
115                         DBGC ( device, "EFIDRV %p %s has driver \"%s\"\n",
116                                device, efi_handle_name ( device ),
117                                efidrv->name );
118                         return 0;
119                 }
120         }
121         DBGCP ( device, "EFIDRV %p %s has no driver\n",
122                 device, efi_handle_name ( device ) );
123
124         return EFI_UNSUPPORTED;
125 }
126
127 /**
128  * Attach driver to device
129  *
130  * @v driver            EFI driver
131  * @v device            EFI device
132  * @v child             Path to child device, if any
133  * @ret efirc           EFI status code
134  */
135 static EFI_STATUS EFIAPI
136 efi_driver_start ( EFI_DRIVER_BINDING_PROTOCOL *driver __unused,
137                    EFI_HANDLE device, EFI_DEVICE_PATH_PROTOCOL *child ) {
138         struct efi_driver *efidrv;
139         struct efi_device *efidev;
140         EFI_STATUS efirc;
141         int rc;
142
143         DBGC ( device, "EFIDRV %p %s DRIVER_START",
144                device, efi_handle_name ( device ) );
145         if ( child )
146                 DBGC ( device, " (child %s)", efi_devpath_text ( child ) );
147         DBGC ( device, "\n" );
148
149         /* Do nothing if we are already driving this device */
150         efidev = efidev_find ( device );
151         if ( efidev ) {
152                 DBGCP ( device, "EFIDRV %p %s is already started\n",
153                         device, efi_handle_name ( device ) );
154                 efirc = EFI_ALREADY_STARTED;
155                 goto err_already_started;
156         }
157
158         /* Allocate and initialise structure */
159         efidev = zalloc ( sizeof ( *efidev ) );
160         if ( ! efidev ) {
161                 efirc = EFI_OUT_OF_RESOURCES;
162                 goto err_alloc;
163         }
164         efidev->device = device;
165         efidev->dev.desc.bus_type = BUS_TYPE_EFI;
166         INIT_LIST_HEAD ( &efidev->dev.children );
167         list_add ( &efidev->dev.siblings, &efi_devices );
168
169         /* Try to start this device */
170         for_each_table_entry ( efidrv, EFI_DRIVERS ) {
171                 if ( ( rc = efidrv->supported ( device ) ) != 0 ) {
172                         DBGC ( device, "EFIDRV %p %s is not supported by "
173                                "driver \"%s\": %s\n", device,
174                                efi_handle_name ( device ), efidrv->name,
175                                strerror ( rc ) );
176                         continue;
177                 }
178                 if ( ( rc = efidrv->start ( efidev ) ) == 0 ) {
179                         efidev->driver = efidrv;
180                         DBGC ( device, "EFIDRV %p %s using driver \"%s\"\n",
181                                device, efi_handle_name ( device ),
182                                efidev->driver->name );
183                         return 0;
184                 }
185                 DBGC ( device, "EFIDRV %p %s could not start driver \"%s\": "
186                        "%s\n", device, efi_handle_name ( device ),
187                        efidrv->name, strerror ( rc ) );
188         }
189         efirc = EFI_UNSUPPORTED;
190
191         list_del ( &efidev->dev.siblings );
192         free ( efidev );
193  err_alloc:
194  err_already_started:
195         return efirc;
196 }
197
198 /**
199  * Detach driver from device
200  *
201  * @v driver            EFI driver
202  * @v device            EFI device
203  * @v pci               PCI device
204  * @v num_children      Number of child devices
205  * @v children          List of child devices
206  * @ret efirc           EFI status code
207  */
208 static EFI_STATUS EFIAPI
209 efi_driver_stop ( EFI_DRIVER_BINDING_PROTOCOL *driver __unused,
210                   EFI_HANDLE device, UINTN num_children,
211                   EFI_HANDLE *children ) {
212         struct efi_driver *efidrv;
213         struct efi_device *efidev;
214         UINTN i;
215
216         DBGC ( device, "EFIDRV %p %s DRIVER_STOP",
217                device, efi_handle_name ( device ) );
218         for ( i = 0 ; i < num_children ; i++ ) {
219                 DBGC ( device, "%s%p %s", ( i ? ", " : " child " ),
220                        children[i], efi_handle_name ( children[i] ) );
221         }
222         DBGC ( device, "\n" );
223
224         /* Do nothing unless we are driving this device */
225         efidev = efidev_find ( device );
226         if ( ! efidev ) {
227                 DBGCP ( device, "EFIDRV %p %s is not started\n",
228                         device, efi_handle_name ( device ) );
229                 return 0;
230         }
231
232         /* Stop this device */
233         efidrv = efidev->driver;
234         assert ( efidrv != NULL );
235         efidrv->stop ( efidev );
236         list_del ( &efidev->dev.siblings );
237         free ( efidev );
238
239         return 0;
240 }
241
242 /** EFI driver binding protocol */
243 static EFI_DRIVER_BINDING_PROTOCOL efi_driver_binding = {
244         .Supported = efi_driver_supported,
245         .Start = efi_driver_start,
246         .Stop = efi_driver_stop,
247 };
248
249 /**
250  * Look up driver name
251  *
252  * @v wtf               Component name protocol
253  * @v language          Language to use
254  * @v driver_name       Driver name to fill in
255  * @ret efirc           EFI status code
256  */
257 static EFI_STATUS EFIAPI
258 efi_driver_name ( EFI_COMPONENT_NAME2_PROTOCOL *wtf __unused,
259                   CHAR8 *language __unused, CHAR16 **driver_name ) {
260         const wchar_t *name;
261
262         name = ( product_wname[0] ? product_wname : build_wname );
263         *driver_name = ( ( wchar_t * ) name );
264         return 0;
265 }
266
267 /**
268  * Look up controller name
269  *
270  * @v wtf               Component name protocol
271  * @v device            Device
272  * @v child             Child device, or NULL
273  * @v language          Language to use
274  * @v driver_name       Device name to fill in
275  * @ret efirc           EFI status code
276  */
277 static EFI_STATUS EFIAPI
278 efi_driver_controller_name ( EFI_COMPONENT_NAME2_PROTOCOL *wtf __unused,
279                              EFI_HANDLE device, EFI_HANDLE child,
280                              CHAR8 *language, CHAR16 **controller_name ) {
281         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
282         union {
283                 EFI_COMPONENT_NAME2_PROTOCOL *name2;
284                 void *interface;
285         } name2;
286         EFI_STATUS efirc;
287
288         /* Delegate to the EFI_COMPONENT_NAME2_PROTOCOL instance
289          * installed on child handle, if present.
290          */
291         if ( ( child != NULL ) &&
292              ( ( efirc = bs->OpenProtocol (
293                           child, &efi_component_name2_protocol_guid,
294                           &name2.interface, NULL, NULL,
295                           EFI_OPEN_PROTOCOL_GET_PROTOCOL ) ) == 0 ) ) {
296                 return name2.name2->GetControllerName ( name2.name2, device,
297                                                         child, language,
298                                                         controller_name );
299         }
300
301         /* Otherwise, let EFI use the default Device Path Name */
302         return EFI_UNSUPPORTED;
303 }
304
305 /** EFI component name protocol */
306 static EFI_COMPONENT_NAME2_PROTOCOL efi_wtf = {
307         .GetDriverName = efi_driver_name,
308         .GetControllerName = efi_driver_controller_name,
309         .SupportedLanguages = "en",
310 };
311
312 /**
313  * Install EFI driver
314  *
315  * @ret rc              Return status code
316  */
317 int efi_driver_install ( void ) {
318         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
319         EFI_STATUS efirc;
320         int rc;
321
322         /* Calculate driver version number.  We use the build
323          * timestamp (in seconds since the Epoch) shifted right by six
324          * bits: this gives us an approximately one-minute resolution
325          * and a scheme which will last until the year 10680.
326          */
327         efi_driver_binding.Version = ( build_timestamp >> 6 );
328
329         /* Install protocols on image handle */
330         efi_driver_binding.ImageHandle = efi_image_handle;
331         efi_driver_binding.DriverBindingHandle = efi_image_handle;
332         if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
333                         &efi_image_handle,
334                         &efi_driver_binding_protocol_guid, &efi_driver_binding,
335                         &efi_component_name2_protocol_guid, &efi_wtf,
336                         NULL ) ) != 0 ) {
337                 rc = -EEFI ( efirc );
338                 DBGC ( &efi_driver_binding, "EFIDRV could not install "
339                        "protocols: %s\n", strerror ( rc ) );
340                 return rc;
341         }
342
343         return 0;
344 }
345
346 /**
347  * Uninstall EFI driver
348  *
349  */
350 void efi_driver_uninstall ( void ) {
351         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
352
353         /* Uninstall protocols */
354         bs->UninstallMultipleProtocolInterfaces (
355                 efi_image_handle,
356                 &efi_driver_binding_protocol_guid, &efi_driver_binding,
357                 &efi_component_name2_protocol_guid, &efi_wtf, NULL );
358 }
359
360 /**
361  * Try to connect EFI driver
362  *
363  * @v device            EFI device
364  * @ret rc              Return status code
365  */
366 static int efi_driver_connect ( EFI_HANDLE device ) {
367         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
368         EFI_HANDLE drivers[2] =
369                 { efi_driver_binding.DriverBindingHandle, NULL };
370         EFI_STATUS efirc;
371         int rc;
372
373         /* Check if we want to drive this device */
374         if ( ( efirc = efi_driver_supported ( &efi_driver_binding, device,
375                                               NULL ) ) != 0 ) {
376                 /* Not supported; not an error */
377                 return 0;
378         }
379
380         /* Disconnect any existing drivers */
381         DBGC2 ( device, "EFIDRV %p %s before disconnecting:\n",
382                 device, efi_handle_name ( device ) );
383         DBGC2_EFI_PROTOCOLS ( device, device );
384         DBGC ( device, "EFIDRV %p %s disconnecting existing drivers\n",
385                device, efi_handle_name ( device ) );
386         if ( ( efirc = bs->DisconnectController ( device, NULL,
387                                                   NULL ) ) != 0 ) {
388                 rc = -EEFI ( efirc );
389                 DBGC ( device, "EFIDRV %p %s could not disconnect existing "
390                        "drivers: %s\n", device, efi_handle_name ( device ),
391                        strerror ( rc ) );
392                 /* Ignore the error and attempt to connect our drivers */
393         }
394         DBGC2 ( device, "EFIDRV %p %s after disconnecting:\n",
395                 device, efi_handle_name ( device ) );
396         DBGC2_EFI_PROTOCOLS ( device, device );
397
398         /* Connect our driver */
399         DBGC ( device, "EFIDRV %p %s connecting new drivers\n",
400                device, efi_handle_name ( device ) );
401         if ( ( efirc = bs->ConnectController ( device, drivers, NULL,
402                                                FALSE ) ) != 0 ) {
403                 rc = -EEFI ( efirc );
404                 DBGC ( device, "EFIDRV %p %s could not connect new drivers: "
405                        "%s\n", device, efi_handle_name ( device ),
406                        strerror ( rc ) );
407                 return rc;
408         }
409         DBGC2 ( device, "EFIDRV %p %s after connecting:\n",
410                 device, efi_handle_name ( device ) );
411         DBGC2_EFI_PROTOCOLS ( device, device );
412
413         return 0;
414 }
415
416 /**
417  * Try to disconnect EFI driver
418  *
419  * @v device            EFI device
420  * @ret rc              Return status code
421  */
422 static int efi_driver_disconnect ( EFI_HANDLE device ) {
423         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
424
425         /* Disconnect our driver */
426         bs->DisconnectController ( device,
427                                    efi_driver_binding.DriverBindingHandle,
428                                    NULL );
429         return 0;
430 }
431
432 /**
433  * Reconnect original EFI driver
434  *
435  * @v device            EFI device
436  * @ret rc              Return status code
437  */
438 static int efi_driver_reconnect ( EFI_HANDLE device ) {
439         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
440
441         /* Reconnect any available driver */
442         bs->ConnectController ( device, NULL, NULL, FALSE );
443
444         return 0;
445 }
446
447 /**
448  * Connect/disconnect EFI driver from all handles
449  *
450  * @v method            Connect/disconnect method
451  * @ret rc              Return status code
452  */
453 static int efi_driver_handles ( int ( * method ) ( EFI_HANDLE handle ) ) {
454         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
455         EFI_HANDLE *handles;
456         UINTN num_handles;
457         EFI_STATUS efirc;
458         UINTN i;
459         int rc;
460
461         /* Enumerate all handles */
462         if ( ( efirc = bs->LocateHandleBuffer ( AllHandles, NULL, NULL,
463                                                 &num_handles,
464                                                 &handles ) ) != 0 ) {
465                 rc = -EEFI ( efirc );
466                 DBGC ( &efi_driver_binding, "EFIDRV could not list handles: "
467                        "%s\n", strerror ( rc ) );
468                 goto err_locate;
469         }
470
471         /* Connect/disconnect driver from all handles */
472         for ( i = 0 ; i < num_handles ; i++ ) {
473                 if ( ( rc = method ( handles[i] ) ) != 0 )
474                         goto err_method;
475         }
476
477         /* Success */
478         rc = 0;
479
480  err_method:
481         bs->FreePool ( handles );
482  err_locate:
483         return rc;
484 }
485
486 /**
487  * Connect EFI driver to all possible devices
488  *
489  * @ret rc              Return status code
490  */
491 int efi_driver_connect_all ( void ) {
492
493         DBGC ( &efi_driver_binding, "EFIDRV connecting our drivers\n" );
494         return efi_driver_handles ( efi_driver_connect );
495 }
496
497 /**
498  * Disconnect EFI driver from all possible devices
499  *
500  * @ret rc              Return status code
501  */
502 void efi_driver_disconnect_all ( void ) {
503
504         DBGC ( &efi_driver_binding, "EFIDRV disconnecting our drivers\n" );
505         efi_driver_handles ( efi_driver_disconnect );
506 }
507
508 /**
509  * Reconnect original EFI drivers to all possible devices
510  *
511  * @ret rc              Return status code
512  */
513 void efi_driver_reconnect_all ( void ) {
514
515         DBGC ( &efi_driver_binding, "EFIDRV reconnecting old drivers\n" );
516         efi_driver_handles ( efi_driver_reconnect );
517 }