You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ibft.c 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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. FILE_LICENCE ( BSD2 );
  28. #include <stdint.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. #include <byteswap.h>
  34. #include <ipxe/pci.h>
  35. #include <ipxe/acpi.h>
  36. #include <ipxe/in.h>
  37. #include <ipxe/netdevice.h>
  38. #include <ipxe/ethernet.h>
  39. #include <ipxe/vlan.h>
  40. #include <ipxe/tcpip.h>
  41. #include <ipxe/dhcp.h>
  42. #include <ipxe/iscsi.h>
  43. #include <ipxe/ibft.h>
  44. /** @file
  45. *
  46. * iSCSI boot firmware table
  47. *
  48. * The information in this file is derived from the document "iSCSI
  49. * Boot Firmware Table (iBFT)" as published by IBM at
  50. *
  51. * ftp://ftp.software.ibm.com/systems/support/system_x_pdf/ibm_iscsi_boot_firmware_table_v1.02.pdf
  52. *
  53. */
  54. /**
  55. * iSCSI string buffer
  56. *
  57. * This is an internal structure that we use to keep track of the
  58. * allocation of string data.
  59. */
  60. struct ibft_strings {
  61. /** Strings data */
  62. char *data;
  63. /** Starting offset of strings */
  64. size_t start;
  65. /** Total length */
  66. size_t len;
  67. };
  68. /**
  69. * Align structure within iBFT
  70. *
  71. * @v len Unaligned length (or offset)
  72. * @ret len Aligned length (or offset)
  73. */
  74. static inline size_t ibft_align ( size_t len ) {
  75. return ( ( len + IBFT_ALIGN - 1 ) & ~( IBFT_ALIGN - 1 ) );
  76. }
  77. /**
  78. * Fill in an IP address field within iBFT
  79. *
  80. * @v ipaddr IP address field
  81. * @v in IPv4 address
  82. */
  83. static void ibft_set_ipaddr ( struct ibft_ipaddr *ipaddr, struct in_addr in ) {
  84. memset ( ipaddr, 0, sizeof ( *ipaddr ) );
  85. if ( in.s_addr ) {
  86. ipaddr->in = in;
  87. ipaddr->ones = 0xffff;
  88. }
  89. }
  90. /**
  91. * Fill in an IP address within iBFT from configuration setting
  92. *
  93. * @v settings Parent settings block, or NULL
  94. * @v ipaddr IP address field
  95. * @v setting Configuration setting
  96. * @v count Maximum number of IP addresses
  97. */
  98. static void ibft_set_ipaddr_setting ( struct settings *settings,
  99. struct ibft_ipaddr *ipaddr,
  100. const struct setting *setting,
  101. unsigned int count ) {
  102. struct in_addr in[count];
  103. unsigned int i;
  104. fetch_ipv4_array_setting ( settings, setting, in, count );
  105. for ( i = 0 ; i < count ; i++ ) {
  106. ibft_set_ipaddr ( &ipaddr[i], in[i] );
  107. }
  108. }
  109. /**
  110. * Read IP address from iBFT (for debugging)
  111. *
  112. * @v strings iBFT string block descriptor
  113. * @v string String field
  114. * @ret ipaddr IP address string
  115. */
  116. static const char * ibft_ipaddr ( struct ibft_ipaddr *ipaddr ) {
  117. return inet_ntoa ( ipaddr->in );
  118. }
  119. /**
  120. * Allocate a string within iBFT
  121. *
  122. * @v strings iBFT string block descriptor
  123. * @v string String field to fill in
  124. * @v len Length of string to allocate (excluding NUL)
  125. * @ret dest String destination, or NULL
  126. */
  127. static char * ibft_alloc_string ( struct ibft_strings *strings,
  128. struct ibft_string *string, size_t len ) {
  129. size_t new_len;
  130. char *new_data;
  131. char *dest;
  132. /* Extend string data buffer */
  133. new_len = ( strings->len + len + 1 /* NUL */ );
  134. new_data = realloc ( strings->data, new_len );
  135. if ( ! new_data )
  136. return NULL;
  137. strings->data = new_data;
  138. /* Fill in string field */
  139. string->offset = cpu_to_le16 ( strings->start + strings->len );
  140. string->len = cpu_to_le16 ( len );
  141. /* Zero string */
  142. dest = ( strings->data + strings->len );
  143. memset ( dest, 0, ( len + 1 /* NUL */ ) );
  144. /* Update allocated length */
  145. strings->len = new_len;
  146. return dest;
  147. }
  148. /**
  149. * Fill in a string field within iBFT
  150. *
  151. * @v strings iBFT string block descriptor
  152. * @v string String field
  153. * @v data String to fill in, or NULL
  154. * @ret rc Return status code
  155. */
  156. static int ibft_set_string ( struct ibft_strings *strings,
  157. struct ibft_string *string, const char *data ) {
  158. char *dest;
  159. if ( ! data )
  160. return 0;
  161. dest = ibft_alloc_string ( strings, string, strlen ( data ) );
  162. if ( ! dest )
  163. return -ENOBUFS;
  164. strcpy ( dest, data );
  165. return 0;
  166. }
  167. /**
  168. * Fill in a string field within iBFT from configuration setting
  169. *
  170. * @v settings Parent settings block, or NULL
  171. * @v strings iBFT string block descriptor
  172. * @v string String field
  173. * @v setting Configuration setting
  174. * @ret rc Return status code
  175. */
  176. static int ibft_set_string_setting ( struct settings *settings,
  177. struct ibft_strings *strings,
  178. struct ibft_string *string,
  179. const struct setting *setting ) {
  180. struct settings *origin;
  181. struct setting fetched;
  182. int len;
  183. char *dest;
  184. len = fetch_setting ( settings, setting, &origin, &fetched, NULL, 0 );
  185. if ( len < 0 ) {
  186. string->offset = 0;
  187. string->len = 0;
  188. return 0;
  189. }
  190. dest = ibft_alloc_string ( strings, string, len );
  191. if ( ! dest )
  192. return -ENOBUFS;
  193. fetch_string_setting ( origin, &fetched, dest, ( len + 1 ));
  194. return 0;
  195. }
  196. /**
  197. * Read string from iBFT (for debugging)
  198. *
  199. * @v strings iBFT string block descriptor
  200. * @v string String field
  201. * @ret data String content (or "<empty>")
  202. */
  203. static const char * ibft_string ( struct ibft_strings *strings,
  204. struct ibft_string *string ) {
  205. size_t offset = le16_to_cpu ( string->offset );
  206. return ( offset ? ( strings->data + offset - strings->start ) : NULL );
  207. }
  208. /**
  209. * Check if network device is required for the iBFT
  210. *
  211. * @v netdev Network device
  212. * @ret is_required Network device is required
  213. */
  214. static int ibft_netdev_is_required ( struct net_device *netdev ) {
  215. struct iscsi_session *iscsi;
  216. struct sockaddr_tcpip *st_target;
  217. list_for_each_entry ( iscsi, &ibft_model.descs, desc.list ) {
  218. st_target = ( struct sockaddr_tcpip * ) &iscsi->target_sockaddr;
  219. if ( tcpip_netdev ( st_target ) == netdev )
  220. return 1;
  221. }
  222. return 0;
  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. /* Fill in common header */
  242. nic->header.structure_id = IBFT_STRUCTURE_ID_NIC;
  243. nic->header.version = 1;
  244. nic->header.length = cpu_to_le16 ( sizeof ( *nic ) );
  245. nic->header.flags = ( IBFT_FL_NIC_BLOCK_VALID |
  246. IBFT_FL_NIC_FIRMWARE_BOOT_SELECTED );
  247. DBG ( "iBFT NIC %d is %s\n", nic->header.index, netdev->name );
  248. /* Determine origin of IP address */
  249. fetch_setting ( parent, &ip_setting, &origin, NULL, NULL, 0 );
  250. nic->origin = ( ( origin == parent ) ?
  251. IBFT_NIC_ORIGIN_MANUAL : IBFT_NIC_ORIGIN_DHCP );
  252. DBG ( "iBFT NIC %d origin = %d\n", nic->header.index, nic->origin );
  253. /* Extract values from configuration settings */
  254. ibft_set_ipaddr_setting ( parent, &nic->ip_address, &ip_setting, 1 );
  255. DBG ( "iBFT NIC %d IP = %s\n",
  256. nic->header.index, ibft_ipaddr ( &nic->ip_address ) );
  257. ibft_set_ipaddr_setting ( parent, &nic->gateway, &gateway_setting, 1 );
  258. DBG ( "iBFT NIC %d gateway = %s\n",
  259. nic->header.index, 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. ibft_set_ipaddr_setting ( parent, &nic->dhcp, &dhcp_server_setting, 1 );
  264. DBG ( "iBFT NIC %d DNS = %s",
  265. nic->header.index, ibft_ipaddr ( &nic->dns[0] ) );
  266. DBG ( ", %s\n", ibft_ipaddr ( &nic->dns[1] ) );
  267. if ( ( rc = ibft_set_string_setting ( NULL, strings, &nic->hostname,
  268. &hostname_setting ) ) != 0 )
  269. return rc;
  270. DBG ( "iBFT NIC %d hostname = %s\n",
  271. nic->header.index, ibft_string ( strings, &nic->hostname ) );
  272. /* Derive subnet mask prefix from subnet mask */
  273. fetch_ipv4_setting ( parent, &netmask_setting, &netmask_addr );
  274. while ( netmask_addr.s_addr ) {
  275. if ( netmask_addr.s_addr & 0x1 )
  276. netmask_count++;
  277. netmask_addr.s_addr >>= 1;
  278. }
  279. nic->subnet_mask_prefix = netmask_count;
  280. DBG ( "iBFT NIC %d subnet = /%d\n",
  281. nic->header.index, nic->subnet_mask_prefix );
  282. /* Extract values from net-device configuration */
  283. nic->vlan = cpu_to_le16 ( vlan_tag ( netdev ) );
  284. DBG ( "iBFT NIC %d VLAN = %02x\n",
  285. nic->header.index, le16_to_cpu ( nic->vlan ) );
  286. if ( ( rc = ll_protocol->eth_addr ( netdev->ll_addr,
  287. nic->mac_address ) ) != 0 ) {
  288. DBG ( "Could not determine %s MAC: %s\n",
  289. netdev->name, strerror ( rc ) );
  290. return rc;
  291. }
  292. DBG ( "iBFT NIC %d MAC = %s\n",
  293. nic->header.index, eth_ntoa ( nic->mac_address ) );
  294. nic->pci_bus_dev_func = cpu_to_le16 ( netdev->dev->desc.location );
  295. DBG ( "iBFT NIC %d PCI = %04x\n",
  296. nic->header.index, le16_to_cpu ( nic->pci_bus_dev_func ) );
  297. return 0;
  298. }
  299. /**
  300. * Fill in Initiator portion of iBFT
  301. *
  302. * @v initiator Initiator portion of iBFT
  303. * @v strings iBFT string block descriptor
  304. * @ret rc Return status code
  305. */
  306. static int ibft_fill_initiator ( struct ibft_initiator *initiator,
  307. struct ibft_strings *strings ) {
  308. int rc;
  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. /* Fill in initiator name */
  316. if ( ( rc = ibft_set_string_setting ( NULL, strings,
  317. &initiator->initiator_name,
  318. &initiator_iqn_setting ) ) != 0 )
  319. return rc;
  320. DBG ( "iBFT initiator name = %s\n",
  321. ibft_string ( strings, &initiator->initiator_name ) );
  322. return 0;
  323. }
  324. /**
  325. * Fill in Target NIC association
  326. *
  327. * @v target Target portion of iBFT
  328. * @v iscsi iSCSI session
  329. * @ret rc Return status code
  330. */
  331. static int ibft_fill_target_nic_association ( struct ibft_target *target,
  332. struct iscsi_session *iscsi ) {
  333. struct sockaddr_tcpip *st_target =
  334. ( struct sockaddr_tcpip * ) &iscsi->target_sockaddr;
  335. struct net_device *associated;
  336. struct net_device *netdev;
  337. /* Find network device used to reach target */
  338. associated = tcpip_netdev ( st_target );
  339. if ( ! associated ) {
  340. DBG ( "iBFT target %d has no net device\n",
  341. target->header.index );
  342. return -EHOSTUNREACH;
  343. }
  344. /* Calculate association */
  345. for_each_netdev ( netdev ) {
  346. if ( netdev == associated ) {
  347. DBG ( "iBFT target %d uses NIC %d (%s)\n",
  348. target->header.index, target->nic_association,
  349. netdev->name );
  350. return 0;
  351. }
  352. if ( ! ibft_netdev_is_required ( netdev ) )
  353. continue;
  354. target->nic_association++;
  355. }
  356. DBG ( "iBFT target %d has impossible NIC %s\n",
  357. target->header.index, netdev->name );
  358. return -EINVAL;
  359. }
  360. /**
  361. * Fill in Target CHAP portion of iBFT
  362. *
  363. * @v target Target portion of iBFT
  364. * @v strings iBFT string block descriptor
  365. * @v iscsi iSCSI session
  366. * @ret rc Return status code
  367. */
  368. static int ibft_fill_target_chap ( struct ibft_target *target,
  369. struct ibft_strings *strings,
  370. struct iscsi_session *iscsi ) {
  371. int rc;
  372. if ( ! ( iscsi->status & ISCSI_STATUS_AUTH_FORWARD_REQUIRED ) )
  373. return 0;
  374. assert ( iscsi->initiator_username );
  375. assert ( iscsi->initiator_password );
  376. target->chap_type = IBFT_CHAP_ONE_WAY;
  377. if ( ( rc = ibft_set_string ( strings, &target->chap_name,
  378. iscsi->initiator_username ) ) != 0 )
  379. return rc;
  380. DBG ( "iBFT target %d username = %s\n", target->header.index,
  381. ibft_string ( strings, &target->chap_name ) );
  382. if ( ( rc = ibft_set_string ( strings, &target->chap_secret,
  383. iscsi->initiator_password ) ) != 0 )
  384. return rc;
  385. DBG ( "iBFT target %d password = <redacted>\n", target->header.index );
  386. return 0;
  387. }
  388. /**
  389. * Fill in Target Reverse CHAP portion of iBFT
  390. *
  391. * @v target Target portion of iBFT
  392. * @v strings iBFT string block descriptor
  393. * @v iscsi iSCSI session
  394. * @ret rc Return status code
  395. */
  396. static int ibft_fill_target_reverse_chap ( struct ibft_target *target,
  397. struct ibft_strings *strings,
  398. struct iscsi_session *iscsi ) {
  399. int rc;
  400. if ( ! ( iscsi->status & ISCSI_STATUS_AUTH_REVERSE_REQUIRED ) )
  401. return 0;
  402. assert ( iscsi->initiator_username );
  403. assert ( iscsi->initiator_password );
  404. assert ( iscsi->target_username );
  405. assert ( iscsi->target_password );
  406. target->chap_type = IBFT_CHAP_MUTUAL;
  407. if ( ( rc = ibft_set_string ( strings, &target->reverse_chap_name,
  408. iscsi->target_username ) ) != 0 )
  409. return rc;
  410. DBG ( "iBFT target %d reverse username = %s\n", target->header.index,
  411. ibft_string ( strings, &target->chap_name ) );
  412. if ( ( rc = ibft_set_string ( strings, &target->reverse_chap_secret,
  413. iscsi->target_password ) ) != 0 )
  414. return rc;
  415. DBG ( "iBFT target %d reverse password = <redacted>\n",
  416. target->header.index );
  417. return 0;
  418. }
  419. /**
  420. * Fill in Target portion of iBFT
  421. *
  422. * @v target Target portion of iBFT
  423. * @v strings iBFT string block descriptor
  424. * @v iscsi iSCSI session
  425. * @ret rc Return status code
  426. */
  427. static int ibft_fill_target ( struct ibft_target *target,
  428. struct ibft_strings *strings,
  429. struct iscsi_session *iscsi ) {
  430. struct sockaddr_tcpip *st_target =
  431. ( struct sockaddr_tcpip * ) &iscsi->target_sockaddr;
  432. struct sockaddr_in *sin_target =
  433. ( struct sockaddr_in * ) &iscsi->target_sockaddr;
  434. int rc;
  435. /* Fill in common header */
  436. target->header.structure_id = IBFT_STRUCTURE_ID_TARGET;
  437. target->header.version = 1;
  438. target->header.length = cpu_to_le16 ( sizeof ( *target ) );
  439. target->header.flags = ( IBFT_FL_TARGET_BLOCK_VALID |
  440. IBFT_FL_TARGET_FIRMWARE_BOOT_SELECTED );
  441. /* Fill in Target values */
  442. ibft_set_ipaddr ( &target->ip_address, sin_target->sin_addr );
  443. DBG ( "iBFT target %d IP = %s\n",
  444. target->header.index, ibft_ipaddr ( &target->ip_address ) );
  445. target->socket = cpu_to_le16 ( ntohs ( st_target->st_port ) );
  446. DBG ( "iBFT target %d port = %d\n",
  447. target->header.index, target->socket );
  448. memcpy ( &target->boot_lun, &iscsi->lun, sizeof ( target->boot_lun ) );
  449. DBG ( "iBFT target %d boot LUN = " SCSI_LUN_FORMAT "\n",
  450. target->header.index, SCSI_LUN_DATA ( target->boot_lun ) );
  451. if ( ( rc = ibft_set_string ( strings, &target->target_name,
  452. iscsi->target_iqn ) ) != 0 )
  453. return rc;
  454. DBG ( "iBFT target %d name = %s\n", target->header.index,
  455. ibft_string ( strings, &target->target_name ) );
  456. if ( ( rc = ibft_fill_target_nic_association ( target, iscsi ) ) != 0 )
  457. return rc;
  458. if ( ( rc = ibft_fill_target_chap ( target, strings, iscsi ) ) != 0 )
  459. return rc;
  460. if ( ( rc = ibft_fill_target_reverse_chap ( target, strings,
  461. iscsi ) ) != 0 )
  462. return rc;
  463. return 0;
  464. }
  465. /**
  466. * Check if iBFT descriptor is complete
  467. *
  468. * @v desc ACPI descriptor
  469. * @ret rc Return status code
  470. */
  471. static int ibft_complete ( struct acpi_descriptor *desc ) {
  472. struct iscsi_session *iscsi =
  473. container_of ( desc, struct iscsi_session, desc );
  474. /* Fail if we do not yet have the target address */
  475. if ( ! iscsi->target_sockaddr.sa_family )
  476. return -EAGAIN;
  477. return 0;
  478. }
  479. /**
  480. * Install iBFT
  481. *
  482. * @v install Installation method
  483. * @ret rc Return status code
  484. */
  485. static int ibft_install ( int ( * install ) ( struct acpi_header *acpi ) ) {
  486. struct net_device *netdev;
  487. struct iscsi_session *iscsi;
  488. struct ibft_table *table;
  489. struct ibft_initiator *initiator;
  490. struct ibft_nic *nic;
  491. struct ibft_target *target;
  492. struct ibft_strings strings;
  493. struct acpi_header *acpi;
  494. void *data;
  495. unsigned int targets = 0;
  496. unsigned int pairs = 0;
  497. size_t offset = 0;
  498. size_t table_len;
  499. size_t control_len;
  500. size_t initiator_offset;
  501. size_t nic_offset;
  502. size_t target_offset;
  503. size_t strings_offset;
  504. size_t len;
  505. unsigned int i;
  506. int rc;
  507. /* Calculate table sizes and offsets */
  508. list_for_each_entry ( iscsi, &ibft_model.descs, desc.list )
  509. targets++;
  510. pairs = ( sizeof ( table->control.pair ) /
  511. sizeof ( table->control.pair[0] ) );
  512. if ( pairs < targets )
  513. pairs = targets;
  514. offset = offsetof ( typeof ( *table ), control.pair );
  515. offset += ( pairs * sizeof ( table->control.pair[0] ) );
  516. table_len = offset;
  517. control_len = ( table_len - offsetof ( typeof ( *table ), control ) );
  518. offset = ibft_align ( offset );
  519. initiator_offset = offset;
  520. offset += ibft_align ( sizeof ( *initiator ) );
  521. nic_offset = offset;
  522. offset += ( pairs * ibft_align ( sizeof ( *nic ) ) );
  523. target_offset = offset;
  524. offset += ( pairs * ibft_align ( sizeof ( *target ) ) );
  525. strings_offset = offset;
  526. strings.data = NULL;
  527. strings.start = strings_offset;
  528. strings.len = 0;
  529. len = offset;
  530. /* Allocate table */
  531. data = zalloc ( len );
  532. if ( ! data ) {
  533. rc = -ENOMEM;
  534. goto err_alloc;
  535. }
  536. /* Fill in Control block */
  537. table = data;
  538. table->control.header.structure_id = IBFT_STRUCTURE_ID_CONTROL;
  539. table->control.header.version = 1;
  540. table->control.header.length = cpu_to_le16 ( control_len );
  541. /* Fill in Initiator block */
  542. initiator = ( data + initiator_offset );
  543. table->control.initiator = cpu_to_le16 ( initiator_offset );
  544. if ( ( rc = ibft_fill_initiator ( initiator, &strings ) ) != 0 )
  545. goto err_initiator;
  546. /* Fill in NIC blocks */
  547. i = 0;
  548. for_each_netdev ( netdev ) {
  549. if ( ! ibft_netdev_is_required ( netdev ) )
  550. continue;
  551. assert ( i < pairs );
  552. table->control.pair[i].nic = nic_offset;
  553. nic = ( data + nic_offset );
  554. nic->header.index = i;
  555. if ( ( rc = ibft_fill_nic ( nic, &strings, netdev ) ) != 0 )
  556. goto err_nic;
  557. i++;
  558. nic_offset += ibft_align ( sizeof ( *nic ) );
  559. }
  560. /* Fill in Target blocks */
  561. i = 0;
  562. list_for_each_entry ( iscsi, &ibft_model.descs, desc.list ) {
  563. assert ( i < pairs );
  564. table->control.pair[i].target = target_offset;
  565. target = ( data + target_offset );
  566. target->header.index = i;
  567. if ( ( rc = ibft_fill_target ( target, &strings, iscsi ) ) != 0)
  568. goto err_target;
  569. i++;
  570. target_offset += ibft_align ( sizeof ( *target ) );
  571. }
  572. /* Reallocate table to include space for strings */
  573. len += strings.len;
  574. acpi = realloc ( data, len );
  575. if ( ! acpi )
  576. goto err_realloc;
  577. data = NULL;
  578. /* Fill in ACPI header */
  579. acpi->signature = cpu_to_le32 ( IBFT_SIG );
  580. acpi->length = cpu_to_le32 ( len );
  581. acpi->revision = 1;
  582. /* Append strings */
  583. memcpy ( ( ( ( void * ) acpi ) + strings_offset ), strings.data,
  584. strings.len );
  585. /* Install ACPI table */
  586. if ( ( rc = install ( acpi ) ) != 0 ) {
  587. DBG ( "iBFT could not install: %s\n", strerror ( rc ) );
  588. goto err_install;
  589. }
  590. err_install:
  591. free ( acpi );
  592. err_realloc:
  593. err_target:
  594. err_nic:
  595. err_initiator:
  596. free ( data );
  597. err_alloc:
  598. free ( strings.data );
  599. return rc;
  600. }
  601. /** iBFT model */
  602. struct acpi_model ibft_model __acpi_model = {
  603. .descs = LIST_HEAD_INIT ( ibft_model.descs ),
  604. .complete = ibft_complete,
  605. .install = ibft_install,
  606. };