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