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