These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / net / efi / snponly.c
1 /*
2  * Copyright (C) 2014 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 <errno.h>
28 #include <ipxe/init.h>
29 #include <ipxe/efi/efi.h>
30 #include <ipxe/efi/efi_driver.h>
31 #include <ipxe/efi/efi_utils.h>
32 #include <ipxe/efi/Protocol/SimpleNetwork.h>
33 #include <ipxe/efi/Protocol/NetworkInterfaceIdentifier.h>
34 #include "snpnet.h"
35 #include "nii.h"
36
37 /** @file
38  *
39  * EFI chainloaded-device-only driver
40  *
41  */
42
43 /** A chainloaded protocol */
44 struct chained_protocol {
45         /** Protocol GUID */
46         EFI_GUID *protocol;
47         /**
48          * Protocol instance installed on the loaded image's device handle
49          *
50          * We match against the protocol instance (rather than simply
51          * matching against the device handle itself) because some
52          * systems load us via a child of the underlying device, with
53          * a duplicate protocol installed on the child handle.
54          */
55         void *interface;
56 };
57
58 /** Chainloaded SNP protocol */
59 static struct chained_protocol chained_snp = {
60         .protocol = &efi_simple_network_protocol_guid,
61 };
62
63 /** Chainloaded NII protocol */
64 static struct chained_protocol chained_nii = {
65         .protocol = &efi_nii31_protocol_guid,
66 };
67
68 /**
69  * Locate chainloaded protocol instance
70  *
71  * @v chained           Chainloaded protocol
72  * @ret rc              Return status code
73  */
74 static int chained_locate ( struct chained_protocol *chained ) {
75         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
76         EFI_HANDLE device = efi_loaded_image->DeviceHandle;
77         EFI_HANDLE parent;
78         EFI_STATUS efirc;
79         int rc;
80
81         /* Locate handle supporting this protocol */
82         if ( ( rc = efi_locate_device ( device, chained->protocol,
83                                         &parent ) ) != 0 ) {
84                 DBGC ( device, "CHAINED %p %s does not support %s: %s\n",
85                        device, efi_handle_name ( device ),
86                        efi_guid_ntoa ( chained->protocol ), strerror ( rc ) );
87                 goto err_locate_device;
88         }
89         DBGC ( device, "CHAINED %p %s found %s on ", device,
90                efi_handle_name ( device ), efi_guid_ntoa ( chained->protocol ));
91         DBGC ( device, "%p %s\n", parent, efi_handle_name ( parent ) );
92
93         /* Get protocol instance */
94         if ( ( efirc = bs->OpenProtocol ( parent, chained->protocol,
95                                           &chained->interface, efi_image_handle,
96                                           device,
97                                           EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
98                 rc = -EEFI ( efirc );
99                 DBGC ( device, "CHAINED %p %s could not open %s on ",
100                        device, efi_handle_name ( device ),
101                        efi_guid_ntoa ( chained->protocol ) );
102                 DBGC ( device, "%p %s: %s\n",
103                        parent, efi_handle_name ( parent ), strerror ( rc ) );
104                 goto err_open_protocol;
105         }
106
107  err_locate_device:
108         bs->CloseProtocol ( parent, chained->protocol, efi_image_handle,
109                             device );
110  err_open_protocol:
111         return rc;
112 }
113
114 /**
115  * Check to see if driver supports a device
116  *
117  * @v device            EFI device handle
118  * @v chained           Chainloaded protocol
119  * @ret rc              Return status code
120  */
121 static int chained_supported ( EFI_HANDLE device,
122                                struct chained_protocol *chained ) {
123         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
124         EFI_STATUS efirc;
125         void *interface;
126         int rc;
127
128         /* Get protocol */
129         if ( ( efirc = bs->OpenProtocol ( device, chained->protocol, &interface,
130                                           efi_image_handle, device,
131                                           EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
132                 rc = -EEFI ( efirc );
133                 DBGCP ( device, "CHAINED %p %s is not a %s device\n",
134                         device, efi_handle_name ( device ),
135                         efi_guid_ntoa ( chained->protocol ) );
136                 goto err_open_protocol;
137         }
138
139         /* Test for a match against the chainloading device */
140         if ( interface != chained->interface ) {
141                 DBGC ( device, "CHAINED %p %s %p is not the chainloaded "
142                        "%s\n", device, efi_handle_name ( device ),
143                        interface, efi_guid_ntoa ( chained->protocol ) );
144                 rc = -ENOTTY;
145                 goto err_no_match;
146         }
147
148         /* Success */
149         rc = 0;
150         DBGC ( device, "CHAINED %p %s %p is the chainloaded %s\n",
151                device, efi_handle_name ( device ), interface,
152                efi_guid_ntoa ( chained->protocol ) );
153
154  err_no_match:
155         bs->CloseProtocol ( device, chained->protocol, efi_image_handle,
156                             device );
157  err_open_protocol:
158         return rc;
159 }
160
161 /**
162  * Check to see if driver supports a device
163  *
164  * @v device            EFI device handle
165  * @ret rc              Return status code
166  */
167 static int snponly_supported ( EFI_HANDLE device ) {
168
169         return chained_supported ( device, &chained_snp );
170 }
171
172 /**
173  * Check to see if driver supports a device
174  *
175  * @v device            EFI device handle
176  * @ret rc              Return status code
177  */
178 static int niionly_supported ( EFI_HANDLE device ) {
179
180         return chained_supported ( device, &chained_nii );
181 }
182
183 /** EFI SNP chainloading-device-only driver */
184 struct efi_driver snponly_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
185         .name = "SNPONLY",
186         .supported = snponly_supported,
187         .start = snpnet_start,
188         .stop = snpnet_stop,
189 };
190
191 /** EFI NII chainloading-device-only driver */
192 struct efi_driver niionly_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
193         .name = "NIIONLY",
194         .supported = niionly_supported,
195         .start = nii_start,
196         .stop = nii_stop,
197 };
198
199 /**
200  * Initialise EFI chainloaded-device-only driver
201  *
202  */
203 static void chained_init ( void ) {
204
205         chained_locate ( &chained_snp );
206         chained_locate ( &chained_nii );
207 }
208
209 /** EFI chainloaded-device-only initialisation function */
210 struct init_fn chained_init_fn __init_fn ( INIT_LATE ) = {
211         .initialise = chained_init,
212 };