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.

sanboot.c 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*
  2. * Copyright (C) 2017 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. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /**
  25. * @file
  26. *
  27. * SAN booting
  28. *
  29. */
  30. #include <stdint.h>
  31. #include <stdlib.h>
  32. #include <errno.h>
  33. #include <assert.h>
  34. #include <ipxe/xfer.h>
  35. #include <ipxe/open.h>
  36. #include <ipxe/timer.h>
  37. #include <ipxe/process.h>
  38. #include <ipxe/iso9660.h>
  39. #include <ipxe/dhcp.h>
  40. #include <ipxe/settings.h>
  41. #include <ipxe/quiesce.h>
  42. #include <ipxe/sanboot.h>
  43. /**
  44. * Default SAN drive number
  45. *
  46. * The drive number is a meaningful concept only in a BIOS
  47. * environment, where it represents the INT13 drive number (0x80 for
  48. * the first hard disk). We retain it in other environments to allow
  49. * for a simple way for iPXE commands to refer to SAN drives.
  50. */
  51. #define SAN_DEFAULT_DRIVE 0x80
  52. /**
  53. * Timeout for block device commands (in ticks)
  54. *
  55. * Underlying devices should ideally never become totally stuck.
  56. * However, if they do, then the blocking SAN APIs provide no means
  57. * for the caller to cancel the operation, and the machine appears to
  58. * hang. Use an overall timeout for all commands to avoid this
  59. * problem and bounce timeout failures to the caller.
  60. */
  61. #define SAN_COMMAND_TIMEOUT ( 15 * TICKS_PER_SEC )
  62. /**
  63. * Default number of times to retry commands
  64. *
  65. * We may need to retry commands. For example, the underlying
  66. * connection may be closed by the SAN target due to an inactivity
  67. * timeout, or the SAN target may return pointless "error" messages
  68. * such as "SCSI power-on occurred".
  69. */
  70. #define SAN_DEFAULT_RETRIES 10
  71. /**
  72. * Delay between reopening attempts
  73. *
  74. * Some SAN targets will always accept connections instantly and
  75. * report a temporary unavailability by e.g. failing the TEST UNIT
  76. * READY command. Avoid bombarding such targets by introducing a
  77. * small delay between attempts.
  78. */
  79. #define SAN_REOPEN_DELAY_SECS 5
  80. /** List of SAN devices */
  81. LIST_HEAD ( san_devices );
  82. /** Number of times to retry commands */
  83. static unsigned long san_retries = SAN_DEFAULT_RETRIES;
  84. /**
  85. * Find SAN device by drive number
  86. *
  87. * @v drive Drive number
  88. * @ret sandev SAN device, or NULL
  89. */
  90. struct san_device * sandev_find ( unsigned int drive ) {
  91. struct san_device *sandev;
  92. list_for_each_entry ( sandev, &san_devices, list ) {
  93. if ( sandev->drive == drive )
  94. return sandev;
  95. }
  96. return NULL;
  97. }
  98. /**
  99. * Free SAN device
  100. *
  101. * @v refcnt Reference count
  102. */
  103. static void sandev_free ( struct refcnt *refcnt ) {
  104. struct san_device *sandev =
  105. container_of ( refcnt, struct san_device, refcnt );
  106. unsigned int i;
  107. assert ( ! timer_running ( &sandev->timer ) );
  108. assert ( ! sandev->active );
  109. assert ( list_empty ( &sandev->opened ) );
  110. for ( i = 0 ; i < sandev->paths ; i++ ) {
  111. uri_put ( sandev->path[i].uri );
  112. assert ( sandev->path[i].desc == NULL );
  113. }
  114. free ( sandev );
  115. }
  116. /**
  117. * Close SAN device command
  118. *
  119. * @v sandev SAN device
  120. * @v rc Reason for close
  121. */
  122. static void sandev_command_close ( struct san_device *sandev, int rc ) {
  123. /* Stop timer */
  124. stop_timer ( &sandev->timer );
  125. /* Restart interface */
  126. intf_restart ( &sandev->command, rc );
  127. /* Record command status */
  128. sandev->command_rc = rc;
  129. }
  130. /**
  131. * Record SAN device capacity
  132. *
  133. * @v sandev SAN device
  134. * @v capacity SAN device capacity
  135. */
  136. static void sandev_command_capacity ( struct san_device *sandev,
  137. struct block_device_capacity *capacity ) {
  138. /* Record raw capacity information */
  139. memcpy ( &sandev->capacity, capacity, sizeof ( sandev->capacity ) );
  140. }
  141. /** SAN device command interface operations */
  142. static struct interface_operation sandev_command_op[] = {
  143. INTF_OP ( intf_close, struct san_device *, sandev_command_close ),
  144. INTF_OP ( block_capacity, struct san_device *,
  145. sandev_command_capacity ),
  146. };
  147. /** SAN device command interface descriptor */
  148. static struct interface_descriptor sandev_command_desc =
  149. INTF_DESC ( struct san_device, command, sandev_command_op );
  150. /**
  151. * Handle SAN device command timeout
  152. *
  153. * @v retry Retry timer
  154. */
  155. static void sandev_command_expired ( struct retry_timer *timer,
  156. int over __unused ) {
  157. struct san_device *sandev =
  158. container_of ( timer, struct san_device, timer );
  159. sandev_command_close ( sandev, -ETIMEDOUT );
  160. }
  161. /**
  162. * Open SAN path
  163. *
  164. * @v sanpath SAN path
  165. * @ret rc Return status code
  166. */
  167. static int sanpath_open ( struct san_path *sanpath ) {
  168. struct san_device *sandev = sanpath->sandev;
  169. int rc;
  170. /* Sanity check */
  171. list_check_contains_entry ( sanpath, &sandev->closed, list );
  172. /* Open interface */
  173. if ( ( rc = xfer_open_uri ( &sanpath->block, sanpath->uri ) ) != 0 ) {
  174. DBGC ( sandev, "SAN %#02x.%d could not (re)open URI: "
  175. "%s\n", sandev->drive, sanpath->index, strerror ( rc ) );
  176. return rc;
  177. }
  178. /* Update ACPI descriptor, if applicable */
  179. if ( ! ( sandev->flags & SAN_NO_DESCRIBE ) ) {
  180. if ( sanpath->desc )
  181. acpi_del ( sanpath->desc );
  182. sanpath->desc = acpi_describe ( &sanpath->block );
  183. if ( sanpath->desc )
  184. acpi_add ( sanpath->desc );
  185. }
  186. /* Start process */
  187. process_add ( &sanpath->process );
  188. /* Mark as opened */
  189. list_del ( &sanpath->list );
  190. list_add_tail ( &sanpath->list, &sandev->opened );
  191. /* Record as in progress */
  192. sanpath->path_rc = -EINPROGRESS;
  193. return 0;
  194. }
  195. /**
  196. * Close SAN path
  197. *
  198. * @v sanpath SAN path
  199. * @v rc Reason for close
  200. */
  201. static void sanpath_close ( struct san_path *sanpath, int rc ) {
  202. struct san_device *sandev = sanpath->sandev;
  203. /* Record status */
  204. sanpath->path_rc = rc;
  205. /* Mark as closed */
  206. list_del ( &sanpath->list );
  207. list_add_tail ( &sanpath->list, &sandev->closed );
  208. /* Stop process */
  209. process_del ( &sanpath->process );
  210. /* Restart interfaces, avoiding potential loops */
  211. if ( sanpath == sandev->active ) {
  212. intfs_restart ( rc, &sandev->command, &sanpath->block, NULL );
  213. sandev->active = NULL;
  214. sandev_command_close ( sandev, rc );
  215. } else {
  216. intf_restart ( &sanpath->block, rc );
  217. }
  218. }
  219. /**
  220. * Handle closure of underlying block device interface
  221. *
  222. * @v sanpath SAN path
  223. * @v rc Reason for close
  224. */
  225. static void sanpath_block_close ( struct san_path *sanpath, int rc ) {
  226. struct san_device *sandev = sanpath->sandev;
  227. /* Any closure is an error from our point of view */
  228. if ( rc == 0 )
  229. rc = -ENOTCONN;
  230. DBGC ( sandev, "SAN %#02x.%d closed: %s\n",
  231. sandev->drive, sanpath->index, strerror ( rc ) );
  232. /* Close path */
  233. sanpath_close ( sanpath, rc );
  234. }
  235. /**
  236. * Check flow control window
  237. *
  238. * @v sanpath SAN path
  239. */
  240. static size_t sanpath_block_window ( struct san_path *sanpath __unused ) {
  241. /* We are never ready to receive data via this interface.
  242. * This prevents objects that support both block and stream
  243. * interfaces from attempting to send us stream data.
  244. */
  245. return 0;
  246. }
  247. /**
  248. * SAN path process
  249. *
  250. * @v sanpath SAN path
  251. */
  252. static void sanpath_step ( struct san_path *sanpath ) {
  253. struct san_device *sandev = sanpath->sandev;
  254. /* Ignore if we are already the active device */
  255. if ( sanpath == sandev->active )
  256. return;
  257. /* Wait until path has become available */
  258. if ( ! xfer_window ( &sanpath->block ) )
  259. return;
  260. /* Record status */
  261. sanpath->path_rc = 0;
  262. /* Mark as active path or close as applicable */
  263. if ( ! sandev->active ) {
  264. DBGC ( sandev, "SAN %#02x.%d is active\n",
  265. sandev->drive, sanpath->index );
  266. sandev->active = sanpath;
  267. } else {
  268. DBGC ( sandev, "SAN %#02x.%d is available\n",
  269. sandev->drive, sanpath->index );
  270. sanpath_close ( sanpath, 0 );
  271. }
  272. }
  273. /** SAN path block interface operations */
  274. static struct interface_operation sanpath_block_op[] = {
  275. INTF_OP ( intf_close, struct san_path *, sanpath_block_close ),
  276. INTF_OP ( xfer_window, struct san_path *, sanpath_block_window ),
  277. INTF_OP ( xfer_window_changed, struct san_path *, sanpath_step ),
  278. };
  279. /** SAN path block interface descriptor */
  280. static struct interface_descriptor sanpath_block_desc =
  281. INTF_DESC ( struct san_path, block, sanpath_block_op );
  282. /** SAN path process descriptor */
  283. static struct process_descriptor sanpath_process_desc =
  284. PROC_DESC_ONCE ( struct san_path, process, sanpath_step );
  285. /**
  286. * Restart SAN device interface
  287. *
  288. * @v sandev SAN device
  289. * @v rc Reason for restart
  290. */
  291. static void sandev_restart ( struct san_device *sandev, int rc ) {
  292. struct san_path *sanpath;
  293. /* Restart all block device interfaces */
  294. while ( ( sanpath = list_first_entry ( &sandev->opened,
  295. struct san_path, list ) ) ) {
  296. sanpath_close ( sanpath, rc );
  297. }
  298. /* Clear active path */
  299. sandev->active = NULL;
  300. /* Close any outstanding command */
  301. sandev_command_close ( sandev, rc );
  302. }
  303. /**
  304. * (Re)open SAN device
  305. *
  306. * @v sandev SAN device
  307. * @ret rc Return status code
  308. *
  309. * This function will block until the device is available.
  310. */
  311. int sandev_reopen ( struct san_device *sandev ) {
  312. struct san_path *sanpath;
  313. int rc;
  314. /* Unquiesce system */
  315. unquiesce();
  316. /* Close any outstanding command and restart interfaces */
  317. sandev_restart ( sandev, -ECONNRESET );
  318. assert ( sandev->active == NULL );
  319. assert ( list_empty ( &sandev->opened ) );
  320. /* Open all paths */
  321. while ( ( sanpath = list_first_entry ( &sandev->closed,
  322. struct san_path, list ) ) ) {
  323. if ( ( rc = sanpath_open ( sanpath ) ) != 0 )
  324. goto err_open;
  325. }
  326. /* Wait for any device to become available, or for all devices
  327. * to fail.
  328. */
  329. while ( sandev->active == NULL ) {
  330. step();
  331. if ( list_empty ( &sandev->opened ) ) {
  332. /* Get status of the first device to be
  333. * closed. Do this on the basis that earlier
  334. * errors (e.g. "invalid IQN") are probably
  335. * more interesting than later errors
  336. * (e.g. "TCP timeout").
  337. */
  338. rc = -ENODEV;
  339. list_for_each_entry ( sanpath, &sandev->closed, list ) {
  340. rc = sanpath->path_rc;
  341. break;
  342. }
  343. DBGC ( sandev, "SAN %#02x never became available: %s\n",
  344. sandev->drive, strerror ( rc ) );
  345. goto err_none;
  346. }
  347. }
  348. assert ( ! list_empty ( &sandev->opened ) );
  349. return 0;
  350. err_none:
  351. err_open:
  352. sandev_restart ( sandev, rc );
  353. return rc;
  354. }
  355. /** SAN device read/write command parameters */
  356. struct san_command_rw_params {
  357. /** SAN device read/write operation */
  358. int ( * block_rw ) ( struct interface *control, struct interface *data,
  359. uint64_t lba, unsigned int count,
  360. userptr_t buffer, size_t len );
  361. /** Data buffer */
  362. userptr_t buffer;
  363. /** Starting LBA */
  364. uint64_t lba;
  365. /** Block count */
  366. unsigned int count;
  367. };
  368. /** SAN device command parameters */
  369. union san_command_params {
  370. /** Read/write command parameters */
  371. struct san_command_rw_params rw;
  372. };
  373. /**
  374. * Initiate SAN device read/write command
  375. *
  376. * @v sandev SAN device
  377. * @v params Command parameters
  378. * @ret rc Return status code
  379. */
  380. static int sandev_command_rw ( struct san_device *sandev,
  381. const union san_command_params *params ) {
  382. struct san_path *sanpath = sandev->active;
  383. size_t len = ( params->rw.count * sandev->capacity.blksize );
  384. int rc;
  385. /* Sanity check */
  386. assert ( sanpath != NULL );
  387. /* Initiate read/write command */
  388. if ( ( rc = params->rw.block_rw ( &sanpath->block, &sandev->command,
  389. params->rw.lba, params->rw.count,
  390. params->rw.buffer, len ) ) != 0 ) {
  391. DBGC ( sandev, "SAN %#02x.%d could not initiate read/write: "
  392. "%s\n", sandev->drive, sanpath->index, strerror ( rc ) );
  393. return rc;
  394. }
  395. return 0;
  396. }
  397. /**
  398. * Initiate SAN device read capacity command
  399. *
  400. * @v sandev SAN device
  401. * @v params Command parameters
  402. * @ret rc Return status code
  403. */
  404. static int
  405. sandev_command_read_capacity ( struct san_device *sandev,
  406. const union san_command_params *params __unused){
  407. struct san_path *sanpath = sandev->active;
  408. int rc;
  409. /* Sanity check */
  410. assert ( sanpath != NULL );
  411. /* Initiate read capacity command */
  412. if ( ( rc = block_read_capacity ( &sanpath->block,
  413. &sandev->command ) ) != 0 ) {
  414. DBGC ( sandev, "SAN %#02x.%d could not initiate read capacity: "
  415. "%s\n", sandev->drive, sanpath->index, strerror ( rc ) );
  416. return rc;
  417. }
  418. return 0;
  419. }
  420. /**
  421. * Execute a single SAN device command and wait for completion
  422. *
  423. * @v sandev SAN device
  424. * @v command Command
  425. * @v params Command parameters (if required)
  426. * @ret rc Return status code
  427. */
  428. static int
  429. sandev_command ( struct san_device *sandev,
  430. int ( * command ) ( struct san_device *sandev,
  431. const union san_command_params *params ),
  432. const union san_command_params *params ) {
  433. unsigned int retries = 0;
  434. int rc;
  435. /* Sanity check */
  436. assert ( ! timer_running ( &sandev->timer ) );
  437. /* Unquiesce system */
  438. unquiesce();
  439. /* (Re)try command */
  440. do {
  441. /* Reopen block device if applicable */
  442. if ( sandev_needs_reopen ( sandev ) &&
  443. ( ( rc = sandev_reopen ( sandev ) ) != 0 ) ) {
  444. /* Delay reopening attempts */
  445. sleep_fixed ( SAN_REOPEN_DELAY_SECS );
  446. /* Retry opening indefinitely for multipath devices */
  447. if ( sandev->paths <= 1 )
  448. retries++;
  449. continue;
  450. }
  451. /* Initiate command */
  452. if ( ( rc = command ( sandev, params ) ) != 0 ) {
  453. retries++;
  454. continue;
  455. }
  456. /* Start expiry timer */
  457. start_timer_fixed ( &sandev->timer, SAN_COMMAND_TIMEOUT );
  458. /* Wait for command to complete */
  459. while ( timer_running ( &sandev->timer ) )
  460. step();
  461. /* Check command status */
  462. if ( ( rc = sandev->command_rc ) != 0 ) {
  463. retries++;
  464. continue;
  465. }
  466. return 0;
  467. } while ( retries <= san_retries );
  468. /* Sanity check */
  469. assert ( ! timer_running ( &sandev->timer ) );
  470. return rc;
  471. }
  472. /**
  473. * Reset SAN device
  474. *
  475. * @v sandev SAN device
  476. * @ret rc Return status code
  477. */
  478. int sandev_reset ( struct san_device *sandev ) {
  479. int rc;
  480. DBGC ( sandev, "SAN %#02x reset\n", sandev->drive );
  481. /* Close and reopen underlying block device */
  482. if ( ( rc = sandev_reopen ( sandev ) ) != 0 )
  483. return rc;
  484. return 0;
  485. }
  486. /**
  487. * Read from or write to SAN device
  488. *
  489. * @v sandev SAN device
  490. * @v lba Starting logical block address
  491. * @v count Number of logical blocks
  492. * @v buffer Data buffer
  493. * @v block_rw Block read/write method
  494. * @ret rc Return status code
  495. */
  496. static int sandev_rw ( struct san_device *sandev, uint64_t lba,
  497. unsigned int count, userptr_t buffer,
  498. int ( * block_rw ) ( struct interface *control,
  499. struct interface *data,
  500. uint64_t lba, unsigned int count,
  501. userptr_t buffer, size_t len ) ) {
  502. union san_command_params params;
  503. unsigned int remaining;
  504. size_t frag_len;
  505. int rc;
  506. /* Initialise command parameters */
  507. params.rw.block_rw = block_rw;
  508. params.rw.buffer = buffer;
  509. params.rw.lba = ( lba << sandev->blksize_shift );
  510. params.rw.count = sandev->capacity.max_count;
  511. remaining = ( count << sandev->blksize_shift );
  512. /* Read/write fragments */
  513. while ( remaining ) {
  514. /* Determine fragment length */
  515. if ( params.rw.count > remaining )
  516. params.rw.count = remaining;
  517. /* Execute command */
  518. if ( ( rc = sandev_command ( sandev, sandev_command_rw,
  519. &params ) ) != 0 )
  520. return rc;
  521. /* Move to next fragment */
  522. frag_len = ( sandev->capacity.blksize * params.rw.count );
  523. params.rw.buffer = userptr_add ( params.rw.buffer, frag_len );
  524. params.rw.lba += params.rw.count;
  525. remaining -= params.rw.count;
  526. }
  527. return 0;
  528. }
  529. /**
  530. * Read from SAN device
  531. *
  532. * @v sandev SAN device
  533. * @v lba Starting logical block address
  534. * @v count Number of logical blocks
  535. * @v buffer Data buffer
  536. * @ret rc Return status code
  537. */
  538. int sandev_read ( struct san_device *sandev, uint64_t lba,
  539. unsigned int count, userptr_t buffer ) {
  540. int rc;
  541. /* Read from device */
  542. if ( ( rc = sandev_rw ( sandev, lba, count, buffer, block_read ) ) != 0 )
  543. return rc;
  544. return 0;
  545. }
  546. /**
  547. * Write to SAN device
  548. *
  549. * @v sandev SAN device
  550. * @v lba Starting logical block address
  551. * @v count Number of logical blocks
  552. * @v buffer Data buffer
  553. * @ret rc Return status code
  554. */
  555. int sandev_write ( struct san_device *sandev, uint64_t lba,
  556. unsigned int count, userptr_t buffer ) {
  557. int rc;
  558. /* Write to device */
  559. if ( ( rc = sandev_rw ( sandev, lba, count, buffer, block_write ) ) != 0 )
  560. return rc;
  561. /* Quiesce system. This is a heuristic designed to ensure
  562. * that the system is quiesced before Windows starts up, since
  563. * a Windows SAN boot will typically write a status flag to
  564. * the disk as its last action before transferring control to
  565. * the native drivers.
  566. */
  567. quiesce();
  568. return 0;
  569. }
  570. /**
  571. * Describe SAN device
  572. *
  573. * @v sandev SAN device
  574. * @ret rc Return status code
  575. *
  576. * Allow connections to progress until all existent path descriptors
  577. * are complete.
  578. */
  579. static int sandev_describe ( struct san_device *sandev ) {
  580. struct san_path *sanpath;
  581. struct acpi_descriptor *desc;
  582. int rc;
  583. /* Wait for all paths to be either described or closed */
  584. while ( 1 ) {
  585. /* Allow connections to progress */
  586. step();
  587. /* Fail if any closed path has an incomplete descriptor */
  588. list_for_each_entry ( sanpath, &sandev->closed, list ) {
  589. desc = sanpath->desc;
  590. if ( ! desc )
  591. continue;
  592. if ( ( rc = desc->model->complete ( desc ) ) != 0 ) {
  593. DBGC ( sandev, "SAN %#02x.%d could not be "
  594. "described: %s\n", sandev->drive,
  595. sanpath->index, strerror ( rc ) );
  596. return rc;
  597. }
  598. }
  599. /* Succeed if no paths have an incomplete descriptor */
  600. rc = 0;
  601. list_for_each_entry ( sanpath, &sandev->opened, list ) {
  602. desc = sanpath->desc;
  603. if ( ! desc )
  604. continue;
  605. if ( ( rc = desc->model->complete ( desc ) ) != 0 )
  606. break;
  607. }
  608. if ( rc == 0 )
  609. return 0;
  610. }
  611. }
  612. /**
  613. * Remove SAN device descriptors
  614. *
  615. * @v sandev SAN device
  616. */
  617. static void sandev_undescribe ( struct san_device *sandev ) {
  618. struct san_path *sanpath;
  619. unsigned int i;
  620. /* Remove all ACPI descriptors */
  621. for ( i = 0 ; i < sandev->paths ; i++ ) {
  622. sanpath = &sandev->path[i];
  623. if ( sanpath->desc ) {
  624. acpi_del ( sanpath->desc );
  625. sanpath->desc = NULL;
  626. }
  627. }
  628. }
  629. /**
  630. * Configure SAN device as a CD-ROM, if applicable
  631. *
  632. * @v sandev SAN device
  633. * @ret rc Return status code
  634. *
  635. * Both BIOS and UEFI require SAN devices to be accessed with a block
  636. * size of 2048. While we could require the user to configure the
  637. * block size appropriately, this is non-trivial and would impose a
  638. * substantial learning effort on the user. Instead, we check for the
  639. * presence of the ISO9660 primary volume descriptor and, if found,
  640. * then we force a block size of 2048 and map read/write requests
  641. * appropriately.
  642. */
  643. static int sandev_parse_iso9660 ( struct san_device *sandev ) {
  644. static const struct iso9660_primary_descriptor_fixed primary_check = {
  645. .type = ISO9660_TYPE_PRIMARY,
  646. .id = ISO9660_ID,
  647. };
  648. union {
  649. struct iso9660_primary_descriptor primary;
  650. char bytes[ISO9660_BLKSIZE];
  651. } *scratch;
  652. unsigned int blksize;
  653. unsigned int blksize_shift;
  654. unsigned int lba;
  655. unsigned int count;
  656. int rc;
  657. /* Calculate required blocksize shift for potential CD-ROM access */
  658. blksize = sandev->capacity.blksize;
  659. blksize_shift = 0;
  660. while ( blksize < ISO9660_BLKSIZE ) {
  661. blksize <<= 1;
  662. blksize_shift++;
  663. }
  664. if ( blksize > ISO9660_BLKSIZE ) {
  665. /* Cannot be a CD-ROM. This is not an error. */
  666. rc = 0;
  667. goto invalid_blksize;
  668. }
  669. lba = ( ISO9660_PRIMARY_LBA << blksize_shift );
  670. count = ( 1 << blksize_shift );
  671. /* Allocate scratch area */
  672. scratch = malloc ( ISO9660_BLKSIZE );
  673. if ( ! scratch ) {
  674. rc = -ENOMEM;
  675. goto err_alloc;
  676. }
  677. /* Read primary volume descriptor */
  678. if ( ( rc = sandev_read ( sandev, lba, count,
  679. virt_to_user ( scratch ) ) ) != 0 ) {
  680. DBGC ( sandev, "SAN %#02x could not read ISO9660 primary"
  681. "volume descriptor: %s\n",
  682. sandev->drive, strerror ( rc ) );
  683. goto err_rw;
  684. }
  685. /* Configure as CD-ROM if applicable */
  686. if ( memcmp ( &scratch->primary.fixed, &primary_check,
  687. sizeof ( primary_check ) ) == 0 ) {
  688. DBGC ( sandev, "SAN %#02x contains an ISO9660 filesystem; "
  689. "treating as CD-ROM\n", sandev->drive );
  690. sandev->blksize_shift = blksize_shift;
  691. sandev->is_cdrom = 1;
  692. }
  693. err_rw:
  694. free ( scratch );
  695. err_alloc:
  696. invalid_blksize:
  697. return rc;
  698. }
  699. /**
  700. * Allocate SAN device
  701. *
  702. * @v uris List of URIs
  703. * @v count Number of URIs
  704. * @v priv_size Size of private data
  705. * @ret sandev SAN device, or NULL
  706. */
  707. struct san_device * alloc_sandev ( struct uri **uris, unsigned int count,
  708. size_t priv_size ) {
  709. struct san_device *sandev;
  710. struct san_path *sanpath;
  711. size_t size;
  712. unsigned int i;
  713. /* Allocate and initialise structure */
  714. size = ( sizeof ( *sandev ) + ( count * sizeof ( sandev->path[0] ) ) );
  715. sandev = zalloc ( size + priv_size );
  716. if ( ! sandev )
  717. return NULL;
  718. ref_init ( &sandev->refcnt, sandev_free );
  719. intf_init ( &sandev->command, &sandev_command_desc, &sandev->refcnt );
  720. timer_init ( &sandev->timer, sandev_command_expired, &sandev->refcnt );
  721. sandev->priv = ( ( ( void * ) sandev ) + size );
  722. sandev->paths = count;
  723. INIT_LIST_HEAD ( &sandev->opened );
  724. INIT_LIST_HEAD ( &sandev->closed );
  725. for ( i = 0 ; i < count ; i++ ) {
  726. sanpath = &sandev->path[i];
  727. sanpath->sandev = sandev;
  728. sanpath->index = i;
  729. sanpath->uri = uri_get ( uris[i] );
  730. list_add_tail ( &sanpath->list, &sandev->closed );
  731. intf_init ( &sanpath->block, &sanpath_block_desc,
  732. &sandev->refcnt );
  733. process_init_stopped ( &sanpath->process, &sanpath_process_desc,
  734. &sandev->refcnt );
  735. sanpath->path_rc = -EINPROGRESS;
  736. }
  737. return sandev;
  738. }
  739. /**
  740. * Register SAN device
  741. *
  742. * @v sandev SAN device
  743. * @v drive Drive number
  744. * @v flags Flags
  745. * @ret rc Return status code
  746. */
  747. int register_sandev ( struct san_device *sandev, unsigned int drive,
  748. unsigned int flags ) {
  749. int rc;
  750. /* Check that drive number is not in use */
  751. if ( sandev_find ( drive ) != NULL ) {
  752. DBGC ( sandev, "SAN %#02x is already in use\n", drive );
  753. rc = -EADDRINUSE;
  754. goto err_in_use;
  755. }
  756. /* Record drive number and flags */
  757. sandev->drive = drive;
  758. sandev->flags = flags;
  759. /* Check that device is capable of being opened (i.e. that all
  760. * URIs are well-formed and that at least one path is
  761. * working).
  762. */
  763. if ( ( rc = sandev_reopen ( sandev ) ) != 0 )
  764. goto err_reopen;
  765. /* Describe device */
  766. if ( ( rc = sandev_describe ( sandev ) ) != 0 )
  767. goto err_describe;
  768. /* Read device capacity */
  769. if ( ( rc = sandev_command ( sandev, sandev_command_read_capacity,
  770. NULL ) ) != 0 )
  771. goto err_capacity;
  772. /* Configure as a CD-ROM, if applicable */
  773. if ( ( rc = sandev_parse_iso9660 ( sandev ) ) != 0 )
  774. goto err_iso9660;
  775. /* Add to list of SAN devices */
  776. list_add_tail ( &sandev->list, &san_devices );
  777. DBGC ( sandev, "SAN %#02x registered\n", sandev->drive );
  778. return 0;
  779. list_del ( &sandev->list );
  780. err_iso9660:
  781. err_capacity:
  782. err_describe:
  783. err_reopen:
  784. sandev_restart ( sandev, rc );
  785. sandev_undescribe ( sandev );
  786. err_in_use:
  787. return rc;
  788. }
  789. /**
  790. * Unregister SAN device
  791. *
  792. * @v sandev SAN device
  793. */
  794. void unregister_sandev ( struct san_device *sandev ) {
  795. /* Sanity check */
  796. assert ( ! timer_running ( &sandev->timer ) );
  797. /* Remove from list of SAN devices */
  798. list_del ( &sandev->list );
  799. /* Shut down interfaces */
  800. sandev_restart ( sandev, 0 );
  801. /* Remove ACPI descriptors */
  802. sandev_undescribe ( sandev );
  803. DBGC ( sandev, "SAN %#02x unregistered\n", sandev->drive );
  804. }
  805. /** The "san-drive" setting */
  806. const struct setting san_drive_setting __setting ( SETTING_SANBOOT_EXTRA,
  807. san-drive ) = {
  808. .name = "san-drive",
  809. .description = "SAN drive number",
  810. .tag = DHCP_EB_SAN_DRIVE,
  811. .type = &setting_type_uint8,
  812. };
  813. /**
  814. * Get default SAN drive number
  815. *
  816. * @ret drive Default drive number
  817. */
  818. unsigned int san_default_drive ( void ) {
  819. unsigned long drive;
  820. /* Use "san-drive" setting, if specified */
  821. if ( fetch_uint_setting ( NULL, &san_drive_setting, &drive ) >= 0 )
  822. return drive;
  823. /* Otherwise, default to booting from first hard disk */
  824. return SAN_DEFAULT_DRIVE;
  825. }
  826. /** The "san-retries" setting */
  827. const struct setting san_retries_setting __setting ( SETTING_SANBOOT_EXTRA,
  828. san-retries ) = {
  829. .name = "san-retries",
  830. .description = "SAN retry count",
  831. .tag = DHCP_EB_SAN_RETRY,
  832. .type = &setting_type_int8,
  833. };
  834. /**
  835. * Apply SAN boot settings
  836. *
  837. * @ret rc Return status code
  838. */
  839. static int sandev_apply ( void ) {
  840. /* Apply "san-retries" setting */
  841. if ( fetch_uint_setting ( NULL, &san_retries_setting,
  842. &san_retries ) < 0 ) {
  843. san_retries = SAN_DEFAULT_RETRIES;
  844. }
  845. return 0;
  846. }
  847. /** Settings applicator */
  848. struct settings_applicator sandev_applicator __settings_applicator = {
  849. .apply = sandev_apply,
  850. };