These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / interface / efi / efi_bofm.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  * 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 <errno.h>
27 #include <ipxe/bofm.h>
28 #include <ipxe/efi/efi.h>
29 #include <ipxe/efi/efi_pci.h>
30 #include <ipxe/efi/efi_driver.h>
31
32 /** @file
33  *
34  * IBM BladeCenter Open Fabric Manager (BOFM) EFI interface
35  *
36  */
37
38 /***************************************************************************
39  *
40  * EFI BOFM definitions
41  *
42  ***************************************************************************
43  *
44  * Taken from the BOFM UEFI Vendor Specification document
45  *
46  */
47
48 #define IBM_BOFM_DRIVER_CONFIGURATION_PROTOCOL_GUID                     \
49         { 0x03207ce2, 0xd9c7, 0x11dc,                                   \
50           { 0xa9, 0x4d, 0x00, 0x19, 0x7d, 0x89, 0x02, 0x38 } }
51
52 #define IBM_BOFM_DRIVER_CONFIGURATION2_PROTOCOL_GUID                    \
53         { 0xe82a9763, 0x0584, 0x4e41,                                   \
54           { 0xbb, 0x39, 0xe0, 0xcd, 0xb8, 0xc1, 0xf0, 0xfc } }
55
56 typedef struct {
57         UINT8 Id;
58         UINT8 ResultByte;
59 } __attribute__ (( packed )) BOFM_EPID_Results_t;
60
61 typedef struct {
62         UINT8 Version;
63         UINT8 Level;
64         UINT16 Length;
65         UINT8 Checksum;
66         UINT8 Profile[32];
67         UINT8 GlobalOption0;
68         UINT8 GlobalOption1;
69         UINT8 GlobalOption2;
70         UINT8 GlobalOption3;
71         UINT32 SequenceStamp;
72         UINT8 Regions[911]; // For use by BOFM Driver
73         UINT32 Reserved1;
74 } __attribute__ (( packed )) BOFM_Parameters_t;
75
76 typedef struct {
77         UINT32 Reserved1;
78         UINT8 Version;
79         UINT8 Level;
80         UINT8 Checksum;
81         UINT32 SequenceStamp;
82         UINT8 SUIDResults;
83         UINT8 EntryResults[32];
84         UINT8 Reserved2;
85         UINT8 Reserved3;
86         UINT8 FCTgtResults[2];
87         UINT8 SASTgtResults[2];
88         BOFM_EPID_Results_t EPIDResults[2];
89         UINT8 Results4[10];
90 } __attribute__ (( packed )) BOFM_Results_t;
91
92 typedef struct {
93         UINT32 Signature;
94         UINT32 SubSignature;
95         BOFM_Parameters_t Parameters;
96         BOFM_Results_t Results;
97 } __attribute__ (( packed )) BOFM_DataStructure_t;
98
99 #define IBM_BOFM_TABLE BOFM_DataStructure_t
100
101 typedef struct _IBM_BOFM_DRIVER_CONFIGURATION_PROTOCOL
102         IBM_BOFM_DRIVER_CONFIGURATION_PROTOCOL;
103
104 typedef struct _IBM_BOFM_DRIVER_CONFIGURATION_PROTOCOL2
105         IBM_BOFM_DRIVER_CONFIGURATION_PROTOCOL2;
106
107 typedef EFI_STATUS ( EFIAPI *IBM_BOFM_DRIVER_CONFIGURATION_SUPPORT ) (
108         IN IBM_BOFM_DRIVER_CONFIGURATION_PROTOCOL *This,
109         EFI_HANDLE ControllerHandle,
110         UINT8 SupporttedOptions,
111         UINT8 iSCSI_Parameter_Version,
112         UINT8 BOFM_Parameter_Version
113 );
114
115 typedef EFI_STATUS ( EFIAPI *IBM_BOFM_DRIVER_CONFIGURATION_STATUS ) (
116         IN IBM_BOFM_DRIVER_CONFIGURATION_PROTOCOL *This,
117         EFI_HANDLE ControllerHandle,
118         BOOLEAN ResetRequired,
119         UINT8 BOFMReturnCode
120 );
121
122 typedef EFI_STATUS ( EFIAPI *IBM_BOFM_DRIVER_CONFIGURATION_STATUS2 ) (
123         IN IBM_BOFM_DRIVER_CONFIGURATION_PROTOCOL2 *This,
124         EFI_HANDLE ControllerHandle,
125         BOOLEAN ResetRequired,
126         UINT8 BOFMReturnCode
127 );
128
129 struct _IBM_BOFM_DRIVER_CONFIGURATION_PROTOCOL {
130         IBM_BOFM_TABLE BofmTable;
131         IBM_BOFM_DRIVER_CONFIGURATION_STATUS SetStatus;
132         IBM_BOFM_DRIVER_CONFIGURATION_SUPPORT RegisterSupport;
133 };
134
135 struct _IBM_BOFM_DRIVER_CONFIGURATION_PROTOCOL2 {
136         UINT32 Signature;
137         UINT32 Reserved1;
138         UINT64 Reserved2;
139         IBM_BOFM_DRIVER_CONFIGURATION_STATUS2 SetStatus;
140         IBM_BOFM_DRIVER_CONFIGURATION_SUPPORT RegisterSupport;
141         IBM_BOFM_TABLE BofmTable;
142 };
143
144 /***************************************************************************
145  *
146  * EFI BOFM interface
147  *
148  ***************************************************************************
149  */
150
151 /** BOFM1 protocol GUID */
152 static EFI_GUID bofm1_protocol_guid =
153         IBM_BOFM_DRIVER_CONFIGURATION_PROTOCOL_GUID;
154
155 /** BOFM2 protocol GUID */
156 static EFI_GUID bofm2_protocol_guid =
157         IBM_BOFM_DRIVER_CONFIGURATION2_PROTOCOL_GUID;
158
159 /**
160  * Check if device is supported
161  *
162  * @v device            EFI device handle
163  * @ret rc              Return status code
164  */
165 static int efi_bofm_supported ( EFI_HANDLE device ) {
166         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
167         struct pci_device pci;
168         union {
169                 IBM_BOFM_DRIVER_CONFIGURATION_PROTOCOL *bofm1;
170                 void *interface;
171         } bofm1;
172         EFI_STATUS efirc;
173         int rc;
174
175         /* Get PCI device information */
176         if ( ( rc = efipci_info ( device, &pci ) ) != 0 )
177                 return rc;
178
179         /* Look for a BOFM driver */
180         if ( ( rc = bofm_find_driver ( &pci ) ) != 0 ) {
181                 DBGCP ( device, "EFIBOFM %p %s has no driver\n",
182                         device, efi_handle_name ( device ) );
183                 return rc;
184         }
185
186         /* Locate BOFM protocol */
187         if ( ( efirc = bs->LocateProtocol ( &bofm1_protocol_guid, NULL,
188                                             &bofm1.interface ) ) != 0 ) {
189                 rc = -EEFI ( efirc );
190                 DBGC ( device, "EFIBOFM %p %s cannot find BOFM protocol\n",
191                        device, efi_handle_name ( device ) );
192                 return rc;
193         }
194
195         /* Register support for this device */
196         if ( ( efirc = bofm1.bofm1->RegisterSupport ( bofm1.bofm1, device,
197                                                       0x04 /* Can change MAC */,
198                                                       0x00 /* No iSCSI */,
199                                                       0x02 /* Version */ ))!=0){
200                 rc = -EEFI ( efirc );
201                 DBGC ( device, "EFIBOFM %p %s could not register support: %s\n",
202                        device, efi_handle_name ( device ), strerror ( rc ) );
203                 return rc;
204         }
205
206         DBGC ( device, "EFIBOFM %p %s has driver \"%s\"\n",
207                device, efi_handle_name ( device ), pci.id->name );
208         return 0;
209 }
210
211 /**
212  * Attach driver to device
213  *
214  * @v efidev            EFI device
215  * @ret rc              Return status code
216  */
217 static int efi_bofm_start ( struct efi_device *efidev ) {
218         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
219         EFI_HANDLE device = efidev->device;
220         union {
221                 IBM_BOFM_DRIVER_CONFIGURATION_PROTOCOL *bofm1;
222                 void *interface;
223         } bofm1;
224         union {
225                 IBM_BOFM_DRIVER_CONFIGURATION_PROTOCOL2 *bofm2;
226                 void *interface;
227         } bofm2;
228         struct pci_device pci;
229         IBM_BOFM_TABLE *bofmtab;
230         IBM_BOFM_TABLE *bofmtab2;
231         int bofmrc;
232         EFI_STATUS efirc;
233         int rc;
234
235         /* Open PCI device, if possible */
236         if ( ( rc = efipci_open ( device, EFI_OPEN_PROTOCOL_GET_PROTOCOL,
237                                   &pci ) ) != 0 )
238                 goto err_open;
239
240         /* Locate BOFM protocol */
241         if ( ( efirc = bs->LocateProtocol ( &bofm1_protocol_guid, NULL,
242                                             &bofm1.interface ) ) != 0 ) {
243                 rc = -EEFI ( efirc );
244                 DBGC ( device, "EFIBOFM %p %s cannot find BOFM protocol\n",
245                        device, efi_handle_name ( device ) );
246                 goto err_locate_bofm;
247         }
248         bofmtab = &bofm1.bofm1->BofmTable;
249         DBGC ( device, "EFIBOFM %p %s found version 1 BOFM table at %p+%04x\n",
250                device, efi_handle_name ( device ), bofmtab,
251                bofmtab->Parameters.Length );
252
253         /* Locate BOFM2 protocol, if available */
254         if ( ( efirc = bs->LocateProtocol ( &bofm2_protocol_guid, NULL,
255                                             &bofm2.interface ) ) == 0 ) {
256                 bofmtab2 = &bofm2.bofm2->BofmTable;
257                 DBGC ( device, "EFIBOFM %p %s found version 2 BOFM table at "
258                        "%p+%04x\n", device, efi_handle_name ( device ),
259                        bofmtab2, bofmtab2->Parameters.Length );
260                 assert ( bofm2.bofm2->RegisterSupport ==
261                          bofm1.bofm1->RegisterSupport );
262         } else {
263                 DBGC ( device, "EFIBOFM %p %s cannot find BOFM2 protocol\n",
264                        device, efi_handle_name ( device ) );
265                 /* Not a fatal error; may be a BOFM1-only system */
266                 bofmtab2 = NULL;
267         }
268
269         /* Process BOFM table */
270         DBGC2 ( device, "EFIBOFM %p %s version 1 before processing:\n",
271                 device, efi_handle_name ( device ) );
272         DBGC2_HD ( device, bofmtab, bofmtab->Parameters.Length );
273         if ( bofmtab2 ) {
274                 DBGC2 ( device, "EFIBOFM %p %s version 2 before processing:\n",
275                         device, efi_handle_name ( device ) );
276                 DBGC2_HD ( device, bofmtab2, bofmtab2->Parameters.Length );
277         }
278         bofmrc = bofm ( virt_to_user ( bofmtab2 ? bofmtab2 : bofmtab ), &pci );
279         DBGC ( device, "EFIBOFM %p %s status %08x\n",
280                device, efi_handle_name ( device ), bofmrc );
281         DBGC2 ( device, "EFIBOFM %p %s version 1 after processing:\n",
282                 device, efi_handle_name ( device ) );
283         DBGC2_HD ( device, bofmtab, bofmtab->Parameters.Length );
284         if ( bofmtab2 ) {
285                 DBGC2 ( device, "EFIBOFM %p %s version 2 after processing:\n",
286                         device, efi_handle_name ( device ) );
287                 DBGC2_HD ( device, bofmtab2, bofmtab2->Parameters.Length );
288         }
289
290         /* Return BOFM status */
291         if ( bofmtab2 ) {
292                 if ( ( efirc = bofm2.bofm2->SetStatus ( bofm2.bofm2, device,
293                                                         FALSE, bofmrc ) ) != 0){
294                         rc = -EEFI ( efirc );
295                         DBGC ( device, "EFIBOFM %p %s could not set BOFM2 "
296                                "status: %s\n", device,
297                                efi_handle_name ( device ), strerror ( rc ) );
298                         goto err_set_status;
299                 }
300         } else {
301                 if ( ( efirc = bofm1.bofm1->SetStatus ( bofm1.bofm1, device,
302                                                         FALSE, bofmrc ) ) != 0){
303                         rc = -EEFI ( efirc );
304                         DBGC ( device, "EFIBOFM %p %s could not set BOFM "
305                                "status: %s\n", device,
306                                efi_handle_name ( device ), strerror ( rc ) );
307                         goto err_set_status;
308                 }
309         }
310
311         /* BOFM (ab)uses the "start" method to mean "process and exit" */
312         rc = -EAGAIN;
313
314  err_set_status:
315  err_locate_bofm:
316         efipci_close ( device );
317  err_open:
318         return rc;
319 }
320
321 /**
322  * Detach driver from device
323  *
324  * @v device            EFI device
325  */
326 static void efi_bofm_stop ( struct efi_device *efidev __unused ) {
327
328         /* Should never happen */
329         assert ( 0 );
330 }
331
332 /** EFI BOFM driver */
333 struct efi_driver efi_bofm_driver __efi_driver ( EFI_DRIVER_EARLY ) = {
334         .name = "BOFM",
335         .supported = efi_bofm_supported,
336         .start = efi_bofm_start,
337         .stop = efi_bofm_stop,
338 };