Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / block / ibft.c
1 /*
2  * Copyright Fen Systems Ltd. 2007.  Portions of this code are derived
3  * from IBM Corporation Sample Programs.  Copyright IBM Corporation
4  * 2004, 2007.  All rights reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use, copy,
10  * modify, merge, publish, distribute, sublicense, and/or sell copies
11  * of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  *
26  */
27
28 FILE_LICENCE ( BSD2 );
29
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <byteswap.h>
35 #include <ipxe/pci.h>
36 #include <ipxe/acpi.h>
37 #include <ipxe/in.h>
38 #include <ipxe/netdevice.h>
39 #include <ipxe/ethernet.h>
40 #include <ipxe/vlan.h>
41 #include <ipxe/dhcp.h>
42 #include <ipxe/iscsi.h>
43 #include <ipxe/ibft.h>
44
45 /** @file
46  *
47  * iSCSI boot firmware table
48  *
49  * The information in this file is derived from the document "iSCSI
50  * Boot Firmware Table (iBFT)" as published by IBM at
51  *
52  * ftp://ftp.software.ibm.com/systems/support/system_x_pdf/ibm_iscsi_boot_firmware_table_v1.02.pdf
53  *
54  */
55
56 /**
57  * An iBFT created by iPXE
58  *
59  */
60 struct ipxe_ibft {
61         /** The fixed section */
62         struct ibft_table table;
63         /** The Initiator section */
64         struct ibft_initiator initiator __attribute__ (( aligned ( 16 ) ));
65         /** The NIC section */
66         struct ibft_nic nic __attribute__ (( aligned ( 16 ) ));
67         /** The Target section */
68         struct ibft_target target __attribute__ (( aligned ( 16 ) ));
69         /** Strings block */
70         char strings[0];
71 } __attribute__ (( packed, aligned ( 16 ) ));
72
73 /**
74  * iSCSI string block descriptor
75  *
76  * This is an internal structure that we use to keep track of the
77  * allocation of string data.
78  */
79 struct ibft_strings {
80         /** The iBFT containing these strings */
81         struct ibft_table *table;
82         /** Offset of first free byte within iBFT */
83         size_t offset;
84         /** Total length of the iBFT */
85         size_t len;
86 };
87
88 /**
89  * Fill in an IP address field within iBFT
90  *
91  * @v ipaddr            IP address field
92  * @v in                IPv4 address
93  */
94 static void ibft_set_ipaddr ( struct ibft_ipaddr *ipaddr, struct in_addr in ) {
95         memset ( ipaddr, 0, sizeof ( *ipaddr ) );
96         if ( in.s_addr ) {
97                 ipaddr->in = in;
98                 ipaddr->ones = 0xffff;
99         }
100 }
101
102 /**
103  * Fill in an IP address within iBFT from configuration setting
104  *
105  * @v settings          Parent settings block, or NULL
106  * @v ipaddr            IP address field
107  * @v setting           Configuration setting
108  * @v count             Maximum number of IP addresses
109  */
110 static void ibft_set_ipaddr_setting ( struct settings *settings,
111                                       struct ibft_ipaddr *ipaddr,
112                                       const struct setting *setting,
113                                       unsigned int count ) {
114         struct in_addr in[count];
115         unsigned int i;
116
117         fetch_ipv4_array_setting ( settings, setting, in, count );
118         for ( i = 0 ; i < count ; i++ ) {
119                 ibft_set_ipaddr ( &ipaddr[i], in[i] );
120         }
121 }
122
123 /**
124  * Read IP address from iBFT (for debugging)
125  *
126  * @v strings           iBFT string block descriptor
127  * @v string            String field
128  * @ret ipaddr          IP address string
129  */
130 static const char * ibft_ipaddr ( struct ibft_ipaddr *ipaddr ) {
131         return inet_ntoa ( ipaddr->in );
132 }
133
134 /**
135  * Allocate a string within iBFT
136  *
137  * @v strings           iBFT string block descriptor
138  * @v string            String field to fill in
139  * @v len               Length of string to allocate (excluding NUL)
140  * @ret dest            String destination, or NULL
141  */
142 static char * ibft_alloc_string ( struct ibft_strings *strings,
143                                   struct ibft_string *string, size_t len ) {
144
145         if ( ( strings->offset + len ) >= strings->len )
146                 return NULL;
147
148         string->offset = cpu_to_le16 ( strings->offset );
149         string->len = cpu_to_le16 ( len );
150         strings->offset += ( len + 1 );
151
152         return ( ( ( char * ) strings->table ) + string->offset );
153 }
154
155 /**
156  * Fill in a string field within iBFT
157  *
158  * @v strings           iBFT string block descriptor
159  * @v string            String field
160  * @v data              String to fill in, or NULL
161  * @ret rc              Return status code
162  */
163 static int ibft_set_string ( struct ibft_strings *strings,
164                              struct ibft_string *string, const char *data ) {
165         char *dest;
166
167         if ( ! data )
168                 return 0;
169
170         dest = ibft_alloc_string ( strings, string, strlen ( data ) );
171         if ( ! dest )
172                 return -ENOBUFS;
173         strcpy ( dest, data );
174
175         return 0;
176 }
177
178 /**
179  * Fill in a string field within iBFT from configuration setting
180  *
181  * @v settings          Parent settings block, or NULL
182  * @v strings           iBFT string block descriptor
183  * @v string            String field
184  * @v setting           Configuration setting
185  * @ret rc              Return status code
186  */
187 static int ibft_set_string_setting ( struct settings *settings,
188                                      struct ibft_strings *strings,
189                                      struct ibft_string *string,
190                                      const struct setting *setting ) {
191         struct settings *origin;
192         struct setting fetched;
193         int len;
194         char *dest;
195
196         len = fetch_setting ( settings, setting, &origin, &fetched, NULL, 0 );
197         if ( len < 0 ) {
198                 string->offset = 0;
199                 string->len = 0;
200                 return 0;
201         }
202
203         dest = ibft_alloc_string ( strings, string, len );
204         if ( ! dest )
205                 return -ENOBUFS;
206         fetch_string_setting ( origin, &fetched, dest, ( len + 1 ));
207
208         return 0;
209 }
210
211 /**
212  * Read string from iBFT (for debugging)
213  *
214  * @v strings           iBFT string block descriptor
215  * @v string            String field
216  * @ret data            String content (or "<empty>")
217  */
218 static const char * ibft_string ( struct ibft_strings *strings,
219                                   struct ibft_string *string ) {
220         return ( string->offset ?
221                  ( ( ( char * ) strings->table ) + string->offset ) : NULL );
222 }
223
224 /**
225  * Fill in NIC portion of iBFT
226  *
227  * @v nic               NIC portion of iBFT
228  * @v strings           iBFT string block descriptor
229  * @v netdev            Network device
230  * @ret rc              Return status code
231  */
232 static int ibft_fill_nic ( struct ibft_nic *nic,
233                            struct ibft_strings *strings,
234                            struct net_device *netdev ) {
235         struct ll_protocol *ll_protocol = netdev->ll_protocol;
236         struct in_addr netmask_addr = { 0 };
237         unsigned int netmask_count = 0;
238         struct settings *parent = netdev_settings ( netdev );
239         struct settings *origin;
240         int rc;
241
242         /* Fill in common header */
243         nic->header.structure_id = IBFT_STRUCTURE_ID_NIC;
244         nic->header.version = 1;
245         nic->header.length = cpu_to_le16 ( sizeof ( *nic ) );
246         nic->header.flags = ( IBFT_FL_NIC_BLOCK_VALID |
247                               IBFT_FL_NIC_FIRMWARE_BOOT_SELECTED );
248
249         /* Determine origin of IP address */
250         fetch_setting ( parent, &ip_setting, &origin, NULL, NULL, 0 );
251         nic->origin = ( ( origin == parent ) ?
252                         IBFT_NIC_ORIGIN_MANUAL : IBFT_NIC_ORIGIN_DHCP );
253         DBG ( "iBFT NIC origin = %d\n", nic->origin );
254
255         /* Extract values from configuration settings */
256         ibft_set_ipaddr_setting ( parent, &nic->ip_address, &ip_setting, 1 );
257         DBG ( "iBFT NIC IP = %s\n", ibft_ipaddr ( &nic->ip_address ) );
258         ibft_set_ipaddr_setting ( parent, &nic->gateway, &gateway_setting, 1 );
259         DBG ( "iBFT NIC gateway = %s\n", ibft_ipaddr ( &nic->gateway ) );
260         ibft_set_ipaddr_setting ( NULL, &nic->dns[0], &dns_setting,
261                                   ( sizeof ( nic->dns ) /
262                                     sizeof ( nic->dns[0] ) ) );
263         DBG ( "iBFT NIC DNS = %s", ibft_ipaddr ( &nic->dns[0] ) );
264         DBG ( ", %s\n", ibft_ipaddr ( &nic->dns[1] ) );
265         if ( ( rc = ibft_set_string_setting ( NULL, strings, &nic->hostname,
266                                               &hostname_setting ) ) != 0 )
267                 return rc;
268         DBG ( "iBFT NIC hostname = %s\n",
269               ibft_string ( strings, &nic->hostname ) );
270
271         /* Derive subnet mask prefix from subnet mask */
272         fetch_ipv4_setting ( parent, &netmask_setting, &netmask_addr );
273         while ( netmask_addr.s_addr ) {
274                 if ( netmask_addr.s_addr & 0x1 )
275                         netmask_count++;
276                 netmask_addr.s_addr >>= 1;
277         }
278         nic->subnet_mask_prefix = netmask_count;
279         DBG ( "iBFT NIC subnet = /%d\n", nic->subnet_mask_prefix );
280
281         /* Extract values from net-device configuration */
282         nic->vlan = cpu_to_le16 ( vlan_tag ( netdev ) );
283         DBG ( "iBFT NIC VLAN = %02x\n", le16_to_cpu ( nic->vlan ) );
284         if ( ( rc = ll_protocol->eth_addr ( netdev->ll_addr,
285                                             nic->mac_address ) ) != 0 ) {
286                 DBG ( "Could not determine iBFT MAC: %s\n", strerror ( rc ) );
287                 return rc;
288         }
289         DBG ( "iBFT NIC MAC = %s\n", eth_ntoa ( nic->mac_address ) );
290         nic->pci_bus_dev_func = cpu_to_le16 ( netdev->dev->desc.location );
291         DBG ( "iBFT NIC PCI = %04x\n", le16_to_cpu ( nic->pci_bus_dev_func ) );
292
293         return 0;
294 }
295
296 /**
297  * Fill in Initiator portion of iBFT
298  *
299  * @v initiator         Initiator portion of iBFT
300  * @v strings           iBFT string block descriptor
301  * @v iscsi             iSCSI session
302  * @ret rc              Return status code
303  */
304 static int ibft_fill_initiator ( struct ibft_initiator *initiator,
305                                  struct ibft_strings *strings,
306                                  struct iscsi_session *iscsi ) {
307         int rc;
308
309         /* Fill in common header */
310         initiator->header.structure_id = IBFT_STRUCTURE_ID_INITIATOR;
311         initiator->header.version = 1;
312         initiator->header.length = cpu_to_le16 ( sizeof ( *initiator ) );
313         initiator->header.flags = ( IBFT_FL_INITIATOR_BLOCK_VALID |
314                                     IBFT_FL_INITIATOR_FIRMWARE_BOOT_SELECTED );
315
316         /* Fill in hostname */
317         if ( ( rc = ibft_set_string ( strings, &initiator->initiator_name,
318                                       iscsi->initiator_iqn ) ) != 0 )
319                 return rc;
320         DBG ( "iBFT initiator hostname = %s\n",
321               ibft_string ( strings, &initiator->initiator_name ) );
322
323         return 0;
324 }
325
326 /**
327  * Fill in Target CHAP portion of iBFT
328  *
329  * @v target            Target portion of iBFT
330  * @v strings           iBFT string block descriptor
331  * @v iscsi             iSCSI session
332  * @ret rc              Return status code
333  */
334 static int ibft_fill_target_chap ( struct ibft_target *target,
335                                    struct ibft_strings *strings,
336                                    struct iscsi_session *iscsi ) {
337         int rc;
338
339         if ( ! ( iscsi->status & ISCSI_STATUS_AUTH_FORWARD_REQUIRED ) )
340                 return 0;
341
342         assert ( iscsi->initiator_username );
343         assert ( iscsi->initiator_password );
344
345         target->chap_type = IBFT_CHAP_ONE_WAY;
346         if ( ( rc = ibft_set_string ( strings, &target->chap_name,
347                                       iscsi->initiator_username ) ) != 0 )
348                 return rc;
349         DBG ( "iBFT target username = %s\n",
350               ibft_string ( strings, &target->chap_name ) );
351         if ( ( rc = ibft_set_string ( strings, &target->chap_secret,
352                                       iscsi->initiator_password ) ) != 0 )
353                 return rc;
354         DBG ( "iBFT target password = <redacted>\n" );
355
356         return 0;
357 }
358
359 /**
360  * Fill in Target Reverse CHAP portion of iBFT
361  *
362  * @v target            Target portion of iBFT
363  * @v strings           iBFT string block descriptor
364  * @v iscsi             iSCSI session
365  * @ret rc              Return status code
366  */
367 static int ibft_fill_target_reverse_chap ( struct ibft_target *target,
368                                            struct ibft_strings *strings,
369                                            struct iscsi_session *iscsi ) {
370         int rc;
371
372         if ( ! ( iscsi->status & ISCSI_STATUS_AUTH_REVERSE_REQUIRED ) )
373                 return 0;
374
375         assert ( iscsi->initiator_username );
376         assert ( iscsi->initiator_password );
377         assert ( iscsi->target_username );
378         assert ( iscsi->target_password );
379
380         target->chap_type = IBFT_CHAP_MUTUAL;
381         if ( ( rc = ibft_set_string ( strings, &target->reverse_chap_name,
382                                       iscsi->target_username ) ) != 0 )
383                 return rc;
384         DBG ( "iBFT target reverse username = %s\n",
385               ibft_string ( strings, &target->chap_name ) );
386         if ( ( rc = ibft_set_string ( strings, &target->reverse_chap_secret,
387                                       iscsi->target_password ) ) != 0 )
388                 return rc;
389         DBG ( "iBFT target reverse password = <redacted>\n" );
390
391         return 0;
392 }
393
394 /**
395  * Fill in Target portion of iBFT
396  *
397  * @v target            Target portion of iBFT
398  * @v strings           iBFT string block descriptor
399  * @v iscsi             iSCSI session
400  * @ret rc              Return status code
401  */
402 static int ibft_fill_target ( struct ibft_target *target,
403                               struct ibft_strings *strings,
404                               struct iscsi_session *iscsi ) {
405         struct sockaddr_in *sin_target =
406                 ( struct sockaddr_in * ) &iscsi->target_sockaddr;
407         int rc;
408
409         /* Fill in common header */
410         target->header.structure_id = IBFT_STRUCTURE_ID_TARGET;
411         target->header.version = 1;
412         target->header.length = cpu_to_le16 ( sizeof ( *target ) );
413         target->header.flags = ( IBFT_FL_TARGET_BLOCK_VALID |
414                                  IBFT_FL_TARGET_FIRMWARE_BOOT_SELECTED );
415
416         /* Fill in Target values */
417         ibft_set_ipaddr ( &target->ip_address, sin_target->sin_addr );
418         DBG ( "iBFT target IP = %s\n", ibft_ipaddr ( &target->ip_address ) );
419         target->socket = cpu_to_le16 ( ntohs ( sin_target->sin_port ) );
420         DBG ( "iBFT target port = %d\n", target->socket );
421         memcpy ( &target->boot_lun, &iscsi->lun, sizeof ( target->boot_lun ) );
422         DBG ( "iBFT target boot LUN = " SCSI_LUN_FORMAT "\n",
423               SCSI_LUN_DATA ( target->boot_lun ) );
424         if ( ( rc = ibft_set_string ( strings, &target->target_name,
425                                       iscsi->target_iqn ) ) != 0 )
426                 return rc;
427         DBG ( "iBFT target name = %s\n",
428               ibft_string ( strings, &target->target_name ) );
429         if ( ( rc = ibft_fill_target_chap ( target, strings, iscsi ) ) != 0 )
430                 return rc;
431         if ( ( rc = ibft_fill_target_reverse_chap ( target, strings,
432                                                     iscsi ) ) != 0 )
433                 return rc;
434
435         return 0;
436 }
437
438 /**
439  * Fill in iBFT
440  *
441  * @v iscsi             iSCSI session
442  * @v acpi              ACPI table
443  * @v len               Length of ACPI table
444  * @ret rc              Return status code
445  */
446 int ibft_describe ( struct iscsi_session *iscsi,
447                     struct acpi_description_header *acpi,
448                     size_t len ) {
449         struct ipxe_ibft *ibft =
450                 container_of ( acpi, struct ipxe_ibft, table.acpi );
451         struct ibft_strings strings = {
452                 .table = &ibft->table,
453                 .offset = offsetof ( typeof ( *ibft ), strings ),
454                 .len = len,
455         };
456         struct net_device *netdev;
457         int rc;
458
459         /* Ugly hack.  Now that we have a generic interface mechanism
460          * that can support ioctls, we can potentially eliminate this.
461          */
462         netdev = last_opened_netdev();
463         if ( ! netdev ) {
464                 DBGC ( iscsi, "iSCSI %p cannot guess network device\n",
465                        iscsi );
466                 return -ENODEV;
467         }
468
469         /* Fill in ACPI header */
470         ibft->table.acpi.signature = cpu_to_le32 ( IBFT_SIG );
471         ibft->table.acpi.length = cpu_to_le32 ( len );
472         ibft->table.acpi.revision = 1;
473
474         /* Fill in Control block */
475         ibft->table.control.header.structure_id = IBFT_STRUCTURE_ID_CONTROL;
476         ibft->table.control.header.version = 1;
477         ibft->table.control.header.length =
478                 cpu_to_le16 ( sizeof ( ibft->table.control ) );
479         ibft->table.control.initiator =
480                 cpu_to_le16 ( offsetof ( typeof ( *ibft ), initiator ) );
481         ibft->table.control.nic_0 =
482                 cpu_to_le16 ( offsetof ( typeof ( *ibft ), nic ) );
483         ibft->table.control.target_0 =
484                 cpu_to_le16 ( offsetof ( typeof ( *ibft ), target ) );
485
486         /* Fill in NIC, Initiator and Target blocks */
487         if ( ( rc = ibft_fill_nic ( &ibft->nic, &strings, netdev ) ) != 0 )
488                 return rc;
489         if ( ( rc = ibft_fill_initiator ( &ibft->initiator, &strings,
490                                           iscsi ) ) != 0 )
491                 return rc;
492         if ( ( rc = ibft_fill_target ( &ibft->target, &strings,
493                                        iscsi ) ) != 0 )
494                 return rc;
495
496         return 0;
497 }