From e790366c7c74a4e49a29374a5fb97778d5c8a08b Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Sat, 4 Mar 2017 21:02:31 +0000 Subject: [PATCH] [int13] Refactor to use centralised SAN device abstraction Signed-off-by: Michael Brown --- src/arch/x86/interface/pcbios/int13.c | 949 +++++++------------------- 1 file changed, 264 insertions(+), 685 deletions(-) diff --git a/src/arch/x86/interface/pcbios/int13.c b/src/arch/x86/interface/pcbios/int13.c index 283e5be7..23cfefca 100644 --- a/src/arch/x86/interface/pcbios/int13.c +++ b/src/arch/x86/interface/pcbios/int13.c @@ -29,20 +29,12 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); #include #include #include -#include #include #include -#include -#include -#include -#include -#include -#include #include #include #include #include -#include #include #include #include @@ -59,31 +51,8 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); * */ -/** - * Overall timeout for INT 13 commands (independent of underlying device - * - * Underlying devices should ideally never become totally stuck. - * However, if they do, then the INT 13 mechanism provides no means - * for the caller to cancel the operation, and the machine appears to - * hang. Use an overall timeout for all commands to avoid this - * problem and bounce timeout failures to the caller. - */ -#define INT13_COMMAND_TIMEOUT ( 15 * TICKS_PER_SEC ) - -/** An INT 13 emulated drive */ -struct int13_drive { - /** Reference count */ - struct refcnt refcnt; - /** List of all registered drives */ - struct list_head list; - - /** Block device URI */ - struct uri *uri; - /** Underlying block device interface */ - struct interface block; - - /** BIOS in-use drive number (0x00-0xff) */ - unsigned int drive; +/** INT 13 SAN device private data */ +struct int13_data { /** BIOS natural drive number (0x00-0xff) * * This is the drive number that would have been assigned by @@ -96,16 +65,6 @@ struct int13_drive { */ unsigned int natural_drive; - /** Block device capacity */ - struct block_device_capacity capacity; - /** INT 13 emulated blocksize shift - * - * To allow for emulation of CD-ROM access, this represents - * the left-shift required to translate from INT 13 blocks to - * underlying blocks. - */ - unsigned int blksize_shift; - /** Number of cylinders * * The cylinder number field in an INT 13 call is ten bits @@ -131,13 +90,8 @@ struct int13_drive { */ unsigned int sectors_per_track; - /** Drive is a CD-ROM */ - int is_cdrom; /** Address of El Torito boot catalog (if any) */ unsigned int boot_catalog; - - /** Underlying device status, if in error */ - int block_rc; /** Status of last operation */ int last_status; }; @@ -158,9 +112,6 @@ static struct int13_fdd_parameters __data16 ( int13_fdd_params ) = { }; #define int13_fdd_params __use_data16 ( int13_fdd_params ) -/** List of registered emulated drives */ -static LIST_HEAD ( int13s ); - /** * Equipment word * @@ -189,293 +140,37 @@ static uint8_t __text16 ( num_drives ); #define num_drives __use_text16 ( num_drives ) /** - * Calculate INT 13 drive sector size + * Calculate SAN device capacity (limited to 32 bits) * - * @v int13 Emulated drive - * @ret blksize Sector size - */ -static inline size_t int13_blksize ( struct int13_drive *int13 ) { - return ( int13->capacity.blksize << int13->blksize_shift ); -} - -/** - * Calculate INT 13 drive capacity - * - * @v int13 Emulated drive + * @v sandev SAN device * @ret blocks Number of blocks */ -static inline uint64_t int13_capacity ( struct int13_drive *int13 ) { - return ( int13->capacity.blocks >> int13->blksize_shift ); -} - -/** - * Calculate INT 13 drive capacity (limited to 32 bits) - * - * @v int13 Emulated drive - * @ret blocks Number of blocks - */ -static inline uint32_t int13_capacity32 ( struct int13_drive *int13 ) { - uint64_t capacity = int13_capacity ( int13 ); +static inline uint32_t int13_capacity32 ( struct san_device *sandev ) { + uint64_t capacity = sandev_capacity ( sandev ); return ( ( capacity <= 0xffffffffUL ) ? capacity : 0xffffffff ); } /** - * Test if INT 13 drive is a floppy disk drive + * Test if SAN device is a floppy disk drive * - * @v int13 Emulated drive - * @ret is_fdd Emulated drive is a floppy disk + * @v sandev SAN device + * @ret is_fdd SAN device is a floppy disk drive */ -static inline int int13_is_fdd ( struct int13_drive *int13 ) { - return ( ! ( int13->drive & 0x80 ) ); -} - -/** An INT 13 command */ -struct int13_command { - /** Status */ - int rc; - /** INT 13 drive */ - struct int13_drive *int13; - /** Underlying block device interface */ - struct interface block; - /** Command timeout timer */ - struct retry_timer timer; -}; - -/** - * Record INT 13 drive capacity - * - * @v command INT 13 command - * @v capacity Block device capacity - */ -static void int13_command_capacity ( struct int13_command *command, - struct block_device_capacity *capacity ) { - memcpy ( &command->int13->capacity, capacity, - sizeof ( command->int13->capacity ) ); +static inline int int13_is_fdd ( struct san_device *sandev ) { + return ( ! ( sandev->drive & 0x80 ) ); } /** - * Close INT 13 command + * Parse El Torito parameters * - * @v command INT 13 command - * @v rc Reason for close - */ -static void int13_command_close ( struct int13_command *command, int rc ) { - intf_restart ( &command->block, rc ); - stop_timer ( &command->timer ); - command->rc = rc; -} - -/** - * Handle INT 13 command timer expiry - * - * @v timer Timer - */ -static void int13_command_expired ( struct retry_timer *timer, - int over __unused ) { - struct int13_command *command = - container_of ( timer, struct int13_command, timer ); - - int13_command_close ( command, -ETIMEDOUT ); -} - -/** INT 13 command interface operations */ -static struct interface_operation int13_command_op[] = { - INTF_OP ( intf_close, struct int13_command *, int13_command_close ), - INTF_OP ( block_capacity, struct int13_command *, - int13_command_capacity ), -}; - -/** INT 13 command interface descriptor */ -static struct interface_descriptor int13_command_desc = - INTF_DESC ( struct int13_command, block, int13_command_op ); - -/** - * Open (or reopen) INT 13 emulated drive underlying block device - * - * @v int13 Emulated drive - * @ret rc Return status code - */ -static int int13_reopen_block ( struct int13_drive *int13 ) { - int rc; - - /* Close any existing block device */ - intf_restart ( &int13->block, -ECONNRESET ); - - /* Open block device */ - if ( ( rc = xfer_open_uri ( &int13->block, int13->uri ) ) != 0 ) { - DBGC ( int13, "INT13 drive %02x could not reopen block " - "device: %s\n", int13->drive, strerror ( rc ) ); - int13->block_rc = rc; - return rc; - } - - /* Clear block device error status */ - int13->block_rc = 0; - - return 0; -} - -/** - * Prepare to issue INT 13 command - * - * @v command INT 13 command - * @v int13 Emulated drive - * @ret rc Return status code - */ -static int int13_command_start ( struct int13_command *command, - struct int13_drive *int13 ) { - int rc; - - /* Sanity check */ - assert ( command->int13 == NULL ); - assert ( ! timer_running ( &command->timer ) ); - - /* Reopen block device if necessary */ - if ( ( int13->block_rc != 0 ) && - ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) ) - return rc; - - /* Initialise command */ - command->rc = -EINPROGRESS; - command->int13 = int13; - start_timer_fixed ( &command->timer, INT13_COMMAND_TIMEOUT ); - - /* Wait for block control interface to become ready */ - while ( ( command->rc == -EINPROGRESS ) && - ( xfer_window ( &int13->block ) == 0 ) ) { - step(); - } - - return ( ( command->rc == -EINPROGRESS ) ? - int13->block_rc : command->rc ); -} - -/** - * Wait for INT 13 command to complete - * - * @v command INT 13 command - * @ret rc Return status code - */ -static int int13_command_wait ( struct int13_command *command ) { - - /* Sanity check */ - assert ( timer_running ( &command->timer ) ); - - /* Wait for command to complete */ - while ( command->rc == -EINPROGRESS ) - step(); - - assert ( ! timer_running ( &command->timer ) ); - return command->rc; -} - -/** - * Terminate INT 13 command - * - * @v command INT 13 command - */ -static void int13_command_stop ( struct int13_command *command ) { - stop_timer ( &command->timer ); - command->int13 = NULL; -} - -/** The single active INT 13 command */ -static struct int13_command int13_command = { - .block = INTF_INIT ( int13_command_desc ), - .timer = TIMER_INIT ( int13_command_expired ), -}; - -/** - * Read from or write to INT 13 drive - * - * @v int13 Emulated drive - * @v lba Starting logical block address - * @v count Number of logical blocks - * @v buffer Data buffer - * @v block_rw Block read/write method - * @ret rc Return status code - */ -static int int13_rw ( struct int13_drive *int13, 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 ) ) { - struct int13_command *command = &int13_command; - unsigned int frag_count; - size_t frag_len; - int rc; - - /* Translate to underlying blocksize */ - lba <<= int13->blksize_shift; - count <<= int13->blksize_shift; - - while ( count ) { - - /* Determine fragment length */ - frag_count = count; - if ( frag_count > int13->capacity.max_count ) - frag_count = int13->capacity.max_count; - frag_len = ( int13->capacity.blksize * frag_count ); - - /* Issue command */ - if ( ( ( rc = int13_command_start ( command, int13 ) ) != 0 ) || - ( ( rc = block_rw ( &int13->block, &command->block, lba, - frag_count, buffer, - frag_len ) ) != 0 ) || - ( ( rc = int13_command_wait ( command ) ) != 0 ) ) { - int13_command_stop ( command ); - return rc; - } - int13_command_stop ( command ); - - /* Move to next fragment */ - lba += frag_count; - count -= frag_count; - buffer = userptr_add ( buffer, frag_len ); - } - - return 0; -} - -/** - * Read INT 13 drive capacity - * - * @v int13 Emulated drive - * @ret rc Return status code - */ -static int int13_read_capacity ( struct int13_drive *int13 ) { - struct int13_command *command = &int13_command; - int rc; - - /* Issue command */ - if ( ( ( rc = int13_command_start ( command, int13 ) ) != 0 ) || - ( ( rc = block_read_capacity ( &int13->block, - &command->block ) ) != 0 ) || - ( ( rc = int13_command_wait ( command ) ) != 0 ) ) { - int13_command_stop ( command ); - return rc; - } - - int13_command_stop ( command ); - return 0; -} - -/** - * Parse ISO9660 parameters - * - * @v int13 Emulated drive + * @v sandev SAN device * @v scratch Scratch area for single-sector reads * @ret rc Return status code * - * Reads and parses ISO9660 parameters, if present. + * Reads and parses El Torito parameters, if present. */ -static int int13_parse_iso9660 ( struct int13_drive *int13, void *scratch ) { - static const struct iso9660_primary_descriptor_fixed primary_check = { - .type = ISO9660_TYPE_PRIMARY, - .id = ISO9660_ID, - }; - struct iso9660_primary_descriptor *primary = scratch; +static int int13_parse_eltorito ( struct san_device *sandev, void *scratch ) { + struct int13_data *int13 = sandev->priv; static const struct eltorito_descriptor_fixed boot_check = { .type = ISO9660_TYPE_BOOT, .id = ISO9660_ID, @@ -483,69 +178,34 @@ static int int13_parse_iso9660 ( struct int13_drive *int13, void *scratch ) { .system_id = "EL TORITO SPECIFICATION", }; struct eltorito_descriptor *boot = scratch; - unsigned int blksize; - unsigned int blksize_shift; int rc; - /* Calculate required blocksize shift */ - blksize = int13_blksize ( int13 ); - blksize_shift = 0; - while ( blksize < ISO9660_BLKSIZE ) { - blksize <<= 1; - blksize_shift++; - } - if ( blksize > ISO9660_BLKSIZE ) { - /* Do nothing if the blksize is invalid for CD-ROM access */ - return 0; - } - - /* Read primary volume descriptor */ - if ( ( rc = int13_rw ( int13, - ( ISO9660_PRIMARY_LBA << blksize_shift ), 1, - virt_to_user ( primary ), block_read ) ) != 0 ){ - DBGC ( int13, "INT13 drive %02x could not read ISO9660 " - "primary volume descriptor: %s\n", - int13->drive, strerror ( rc ) ); - return rc; - } - - /* Do nothing unless this is an ISO image */ - if ( memcmp ( primary, &primary_check, sizeof ( primary_check ) ) != 0 ) - return 0; - DBGC ( int13, "INT13 drive %02x contains an ISO9660 filesystem; " - "treating as CD-ROM\n", int13->drive ); - int13->is_cdrom = 1; - /* Read boot record volume descriptor */ - if ( ( rc = int13_rw ( int13, - ( ELTORITO_LBA << blksize_shift ), 1, - virt_to_user ( boot ), block_read ) ) != 0 ) { - DBGC ( int13, "INT13 drive %02x could not read El Torito boot " + if ( ( rc = sandev_rw ( sandev, ELTORITO_LBA, 1, + virt_to_user ( boot ), block_read ) ) != 0 ) { + DBGC ( sandev, "INT13 drive %02x could not read El Torito boot " "record volume descriptor: %s\n", - int13->drive, strerror ( rc ) ); + sandev->drive, strerror ( rc ) ); return rc; } /* Check for an El Torito boot catalog */ if ( memcmp ( boot, &boot_check, sizeof ( boot_check ) ) == 0 ) { int13->boot_catalog = boot->sector; - DBGC ( int13, "INT13 drive %02x has an El Torito boot catalog " - "at LBA %08x\n", int13->drive, int13->boot_catalog ); + DBGC ( sandev, "INT13 drive %02x has an El Torito boot catalog " + "at LBA %08x\n", sandev->drive, int13->boot_catalog ); } else { - DBGC ( int13, "INT13 drive %02x has no El Torito boot " - "catalog\n", int13->drive ); + DBGC ( sandev, "INT13 drive %02x has no El Torito boot " + "catalog\n", sandev->drive ); } - /* Configure drive for no-emulation CD-ROM access */ - int13->blksize_shift += blksize_shift; - return 0; } /** * Guess INT 13 hard disk drive geometry * - * @v int13 Emulated drive + * @v sandev SAN device * @v scratch Scratch area for single-sector reads * @ret heads Guessed number of heads * @ret sectors Guessed number of sectors per track @@ -553,7 +213,7 @@ static int int13_parse_iso9660 ( struct int13_drive *int13, void *scratch ) { * * Guesses the drive geometry by inspecting the partition table. */ -static int int13_guess_geometry_hdd ( struct int13_drive *int13, void *scratch, +static int int13_guess_geometry_hdd ( struct san_device *sandev, void *scratch, unsigned int *heads, unsigned int *sectors ) { struct master_boot_record *mbr = scratch; @@ -568,17 +228,17 @@ static int int13_guess_geometry_hdd ( struct int13_drive *int13, void *scratch, *sectors = 63; /* Read partition table */ - if ( ( rc = int13_rw ( int13, 0, 1, virt_to_user ( mbr ), - block_read ) ) != 0 ) { - DBGC ( int13, "INT13 drive %02x could not read " + if ( ( rc = sandev_rw ( sandev, 0, 1, virt_to_user ( mbr ), + block_read ) ) != 0 ) { + DBGC ( sandev, "INT13 drive %02x could not read " "partition table to guess geometry: %s\n", - int13->drive, strerror ( rc ) ); + sandev->drive, strerror ( rc ) ); return rc; } - DBGC2 ( int13, "INT13 drive %02x has MBR:\n", int13->drive ); - DBGC2_HDA ( int13, 0, mbr, sizeof ( *mbr ) ); - DBGC ( int13, "INT13 drive %02x has signature %08x\n", - int13->drive, mbr->signature ); + DBGC2 ( sandev, "INT13 drive %02x has MBR:\n", sandev->drive ); + DBGC2_HDA ( sandev, 0, mbr, sizeof ( *mbr ) ); + DBGC ( sandev, "INT13 drive %02x has signature %08x\n", + sandev->drive, mbr->signature ); /* Scan through partition table and modify guesses for * heads and sectors_per_track if we find any used @@ -592,9 +252,9 @@ static int int13_guess_geometry_hdd ( struct int13_drive *int13, void *scratch, continue; *heads = ( end_head + 1 ); *sectors = end_sector; - DBGC ( int13, "INT13 drive %02x guessing C/H/S xx/%d/%d based " + DBGC ( sandev, "INT13 drive %02x guessing C/H/S xx/%d/%d based " "on partition %d\n", - int13->drive, *heads, *sectors, ( i + 1 ) ); + sandev->drive, *heads, *sectors, ( i + 1 ) ); } return 0; @@ -627,17 +287,17 @@ static const struct int13_fdd_geometry int13_fdd_geometries[] = { /** * Guess INT 13 floppy disk drive geometry * - * @v int13 Emulated drive + * @v sandev SAN device * @ret heads Guessed number of heads * @ret sectors Guessed number of sectors per track * @ret rc Return status code * * Guesses the drive geometry by inspecting the disk size. */ -static int int13_guess_geometry_fdd ( struct int13_drive *int13, +static int int13_guess_geometry_fdd ( struct san_device *sandev, unsigned int *heads, unsigned int *sectors ) { - unsigned int blocks = int13_capacity ( int13 ); + unsigned int blocks = sandev_capacity ( sandev ); const struct int13_fdd_geometry *geometry; unsigned int cylinders; unsigned int i; @@ -650,8 +310,8 @@ static int int13_guess_geometry_fdd ( struct int13_drive *int13, *heads = INT13_FDD_HEADS ( geometry ); *sectors = INT13_FDD_SECTORS ( geometry ); if ( ( cylinders * (*heads) * (*sectors) ) == blocks ) { - DBGC ( int13, "INT13 drive %02x guessing C/H/S " - "%d/%d/%d based on size %dK\n", int13->drive, + DBGC ( sandev, "INT13 drive %02x guessing C/H/S " + "%d/%d/%d based on size %dK\n", sandev->drive, cylinders, *heads, *sectors, ( blocks / 2 ) ); return 0; } @@ -662,36 +322,33 @@ static int int13_guess_geometry_fdd ( struct int13_drive *int13, */ *heads = 2; *sectors = 18; - DBGC ( int13, "INT13 drive %02x guessing C/H/S xx/%d/%d based on size " - "%dK\n", int13->drive, *heads, *sectors, ( blocks / 2 ) ); + DBGC ( sandev, "INT13 drive %02x guessing C/H/S xx/%d/%d based on size " + "%dK\n", sandev->drive, *heads, *sectors, ( blocks / 2 ) ); return 0; } /** * Guess INT 13 drive geometry * - * @v int13 Emulated drive + * @v sandev SAN device * @v scratch Scratch area for single-sector reads * @ret rc Return status code */ -static int int13_guess_geometry ( struct int13_drive *int13, void *scratch ) { +static int int13_guess_geometry ( struct san_device *sandev, void *scratch ) { + struct int13_data *int13 = sandev->priv; unsigned int guessed_heads; unsigned int guessed_sectors; unsigned int blocks; unsigned int blocks_per_cyl; int rc; - /* Don't even try when the blksize is invalid for C/H/S access */ - if ( int13_blksize ( int13 ) != INT13_BLKSIZE ) - return 0; - /* Guess geometry according to drive type */ - if ( int13_is_fdd ( int13 ) ) { - if ( ( rc = int13_guess_geometry_fdd ( int13, &guessed_heads, + if ( int13_is_fdd ( sandev ) ) { + if ( ( rc = int13_guess_geometry_fdd ( sandev, &guessed_heads, &guessed_sectors )) != 0) return rc; } else { - if ( ( rc = int13_guess_geometry_hdd ( int13, scratch, + if ( ( rc = int13_guess_geometry_hdd ( sandev, scratch, &guessed_heads, &guessed_sectors )) != 0) return rc; @@ -704,7 +361,7 @@ static int int13_guess_geometry ( struct int13_drive *int13, void *scratch ) { int13->sectors_per_track = guessed_sectors; if ( ! int13->cylinders ) { /* Avoid attempting a 64-bit divide on a 32-bit system */ - blocks = int13_capacity32 ( int13 ); + blocks = int13_capacity32 ( sandev ); blocks_per_cyl = ( int13->heads * int13->sectors_per_track ); assert ( blocks_per_cyl != 0 ); int13->cylinders = ( blocks / blocks_per_cyl ); @@ -719,7 +376,8 @@ static int int13_guess_geometry ( struct int13_drive *int13, void *scratch ) { * Update BIOS drive count */ static void int13_sync_num_drives ( void ) { - struct int13_drive *int13; + struct san_device *sandev; + struct int13_data *int13; uint8_t *counter; uint8_t max_drive; uint8_t required; @@ -730,18 +388,19 @@ static void int13_sync_num_drives ( void ) { num_fdds = ( ( equipment_word & 0x0001 ) ? ( ( ( equipment_word >> 6 ) & 0x3 ) + 1 ) : 0 ); - /* Ensure count is large enough to cover all of our emulated drives */ - list_for_each_entry ( int13, &int13s, list ) { - counter = ( int13_is_fdd ( int13 ) ? &num_fdds : &num_drives ); - max_drive = int13->drive; + /* Ensure count is large enough to cover all of our SAN devices */ + for_each_sandev ( sandev ) { + int13 = sandev->priv; + counter = ( int13_is_fdd ( sandev ) ? &num_fdds : &num_drives ); + max_drive = sandev->drive; if ( max_drive < int13->natural_drive ) max_drive = int13->natural_drive; required = ( ( max_drive & 0x7f ) + 1 ); if ( *counter < required ) { *counter = required; - DBGC ( int13, "INT13 drive %02x added to drive count: " + DBGC ( sandev, "INT13 drive %02x added to drive count: " "%d HDDs, %d FDDs\n", - int13->drive, num_drives, num_fdds ); + sandev->drive, num_drives, num_fdds ); } } @@ -773,21 +432,17 @@ static void int13_check_num_drives ( void ) { /** * INT 13, 00 - Reset disk system * - * @v int13 Emulated drive + * @v sandev SAN device * @ret status Status code */ -static int int13_reset ( struct int13_drive *int13, +static int int13_reset ( struct san_device *sandev, struct i386_all_regs *ix86 __unused ) { int rc; - DBGC2 ( int13, "Reset drive\n" ); + DBGC2 ( sandev, "Reset drive\n" ); - /* Reopen underlying block device */ - if ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) - return -INT13_STATUS_RESET_FAILED; - - /* Check that block device is functional */ - if ( ( rc = int13_read_capacity ( int13 ) ) != 0 ) + /* Reset SAN device */ + if ( ( rc = sandev_reset ( sandev ) ) != 0 ) return -INT13_STATUS_RESET_FAILED; return 0; @@ -796,19 +451,21 @@ static int int13_reset ( struct int13_drive *int13, /** * INT 13, 01 - Get status of last operation * - * @v int13 Emulated drive + * @v sandev SAN device * @ret status Status code */ -static int int13_get_last_status ( struct int13_drive *int13, +static int int13_get_last_status ( struct san_device *sandev, struct i386_all_regs *ix86 __unused ) { - DBGC2 ( int13, "Get status of last operation\n" ); + struct int13_data *int13 = sandev->priv; + + DBGC2 ( sandev, "Get status of last operation\n" ); return int13->last_status; } /** * Read / write sectors * - * @v int13 Emulated drive + * @v sandev SAN device * @v al Number of sectors to read or write (must be nonzero) * @v ch Low bits of cylinder number * @v cl (bits 7:6) High bits of cylinder number @@ -819,7 +476,7 @@ static int int13_get_last_status ( struct int13_drive *int13, * @ret status Status code * @ret al Number of sectors read or written */ -static int int13_rw_sectors ( struct int13_drive *int13, +static int int13_rw_sectors ( struct san_device *sandev, struct i386_all_regs *ix86, int ( * block_rw ) ( struct interface *control, struct interface *data, @@ -827,6 +484,7 @@ static int int13_rw_sectors ( struct int13_drive *int13, unsigned int count, userptr_t buffer, size_t len ) ) { + struct int13_data *int13 = sandev->priv; unsigned int cylinder, head, sector; unsigned long lba; unsigned int count; @@ -834,10 +492,10 @@ static int int13_rw_sectors ( struct int13_drive *int13, int rc; /* Validate blocksize */ - if ( int13_blksize ( int13 ) != INT13_BLKSIZE ) { - DBGC ( int13, "\nINT 13 drive %02x invalid blocksize (%zd) " + if ( sandev_blksize ( sandev ) != INT13_BLKSIZE ) { + DBGC ( sandev, "\nINT 13 drive %02x invalid blocksize (%zd) " "for non-extended read/write\n", - int13->drive, int13_blksize ( int13 ) ); + sandev->drive, sandev_blksize ( sandev ) ); return -INT13_STATUS_INVALID; } @@ -848,7 +506,7 @@ static int int13_rw_sectors ( struct int13_drive *int13, if ( ( cylinder >= int13->cylinders ) || ( head >= int13->heads ) || ( sector < 1 ) || ( sector > int13->sectors_per_track ) ) { - DBGC ( int13, "C/H/S %d/%d/%d out of range for geometry " + DBGC ( sandev, "C/H/S %d/%d/%d out of range for geometry " "%d/%d/%d\n", cylinder, head, sector, int13->cylinders, int13->heads, int13->sectors_per_track ); return -INT13_STATUS_INVALID; @@ -858,14 +516,14 @@ static int int13_rw_sectors ( struct int13_drive *int13, count = ix86->regs.al; buffer = real_to_user ( ix86->segs.es, ix86->regs.bx ); - DBGC2 ( int13, "C/H/S %d/%d/%d = LBA %08lx <-> %04x:%04x (count %d)\n", + DBGC2 ( sandev, "C/H/S %d/%d/%d = LBA %08lx <-> %04x:%04x (count %d)\n", cylinder, head, sector, lba, ix86->segs.es, ix86->regs.bx, count ); /* Read from / write to block device */ - if ( ( rc = int13_rw ( int13, lba, count, buffer, block_rw ) ) != 0 ) { - DBGC ( int13, "INT13 drive %02x I/O failed: %s\n", - int13->drive, strerror ( rc ) ); + if ( ( rc = sandev_rw ( sandev, lba, count, buffer, block_rw ) ) != 0 ){ + DBGC ( sandev, "INT13 drive %02x I/O failed: %s\n", + sandev->drive, strerror ( rc ) ); return -INT13_STATUS_READ_ERROR; } @@ -875,7 +533,7 @@ static int int13_rw_sectors ( struct int13_drive *int13, /** * INT 13, 02 - Read sectors * - * @v int13 Emulated drive + * @v sandev SAN device * @v al Number of sectors to read (must be nonzero) * @v ch Low bits of cylinder number * @v cl (bits 7:6) High bits of cylinder number @@ -885,16 +543,17 @@ static int int13_rw_sectors ( struct int13_drive *int13, * @ret status Status code * @ret al Number of sectors read */ -static int int13_read_sectors ( struct int13_drive *int13, +static int int13_read_sectors ( struct san_device *sandev, struct i386_all_regs *ix86 ) { - DBGC2 ( int13, "Read: " ); - return int13_rw_sectors ( int13, ix86, block_read ); + + DBGC2 ( sandev, "Read: " ); + return int13_rw_sectors ( sandev, ix86, block_read ); } /** * INT 13, 03 - Write sectors * - * @v int13 Emulated drive + * @v sandev SAN device * @v al Number of sectors to write (must be nonzero) * @v ch Low bits of cylinder number * @v cl (bits 7:6) High bits of cylinder number @@ -904,16 +563,17 @@ static int int13_read_sectors ( struct int13_drive *int13, * @ret status Status code * @ret al Number of sectors written */ -static int int13_write_sectors ( struct int13_drive *int13, +static int int13_write_sectors ( struct san_device *sandev, struct i386_all_regs *ix86 ) { - DBGC2 ( int13, "Write: " ); - return int13_rw_sectors ( int13, ix86, block_write ); + + DBGC2 ( sandev, "Write: " ); + return int13_rw_sectors ( sandev, ix86, block_write ); } /** * INT 13, 08 - Get drive parameters * - * @v int13 Emulated drive + * @v sandev SAN device * @ret status Status code * @ret ch Low bits of maximum cylinder number * @ret cl (bits 7:6) High bits of maximum cylinder number @@ -921,19 +581,20 @@ static int int13_write_sectors ( struct int13_drive *int13, * @ret dh Maximum head number * @ret dl Number of drives */ -static int int13_get_parameters ( struct int13_drive *int13, +static int int13_get_parameters ( struct san_device *sandev, struct i386_all_regs *ix86 ) { + struct int13_data *int13 = sandev->priv; unsigned int max_cylinder = int13->cylinders - 1; unsigned int max_head = int13->heads - 1; unsigned int max_sector = int13->sectors_per_track; /* sic */ - DBGC2 ( int13, "Get drive parameters\n" ); + DBGC2 ( sandev, "Get drive parameters\n" ); /* Validate blocksize */ - if ( int13_blksize ( int13 ) != INT13_BLKSIZE ) { - DBGC ( int13, "\nINT 13 drive %02x invalid blocksize (%zd) " + if ( sandev_blksize ( sandev ) != INT13_BLKSIZE ) { + DBGC ( sandev, "\nINT 13 drive %02x invalid blocksize (%zd) " "for non-extended parameters\n", - int13->drive, int13_blksize ( int13 ) ); + sandev->drive, sandev_blksize ( sandev ) ); return -INT13_STATUS_INVALID; } @@ -941,10 +602,10 @@ static int int13_get_parameters ( struct int13_drive *int13, ix86->regs.ch = ( max_cylinder & 0xff ); ix86->regs.cl = ( ( ( max_cylinder >> 8 ) << 6 ) | max_sector ); ix86->regs.dh = max_head; - ix86->regs.dl = ( int13_is_fdd ( int13 ) ? num_fdds : num_drives ); + ix86->regs.dl = ( int13_is_fdd ( sandev ) ? num_fdds : num_drives ); /* Floppy-specific parameters */ - if ( int13_is_fdd ( int13 ) ) { + if ( int13_is_fdd ( sandev ) ) { ix86->regs.bl = INT13_FDD_TYPE_1M44; ix86->segs.es = rm_ds; ix86->regs.di = __from_data16 ( &int13_fdd_params ); @@ -956,21 +617,21 @@ static int int13_get_parameters ( struct int13_drive *int13, /** * INT 13, 15 - Get disk type * - * @v int13 Emulated drive + * @v sandev SAN device * @ret ah Type code * @ret cx:dx Sector count * @ret status Status code / disk type */ -static int int13_get_disk_type ( struct int13_drive *int13, +static int int13_get_disk_type ( struct san_device *sandev, struct i386_all_regs *ix86 ) { uint32_t blocks; - DBGC2 ( int13, "Get disk type\n" ); + DBGC2 ( sandev, "Get disk type\n" ); - if ( int13_is_fdd ( int13 ) ) { + if ( int13_is_fdd ( sandev ) ) { return INT13_DISK_TYPE_FDD; } else { - blocks = int13_capacity32 ( int13 ); + blocks = int13_capacity32 ( sandev ); ix86->regs.cx = ( blocks >> 16 ); ix86->regs.dx = ( blocks & 0xffff ); return INT13_DISK_TYPE_HDD; @@ -980,16 +641,17 @@ static int int13_get_disk_type ( struct int13_drive *int13, /** * INT 13, 41 - Extensions installation check * - * @v int13 Emulated drive + * @v sandev SAN device * @v bx 0x55aa * @ret bx 0xaa55 * @ret cx Extensions API support bitmap * @ret status Status code / API version */ -static int int13_extension_check ( struct int13_drive *int13 __unused, +static int int13_extension_check ( struct san_device *sandev __unused, struct i386_all_regs *ix86 ) { + if ( ix86->regs.bx == 0x55aa ) { - DBGC2 ( int13, "INT13 extensions installation check\n" ); + DBGC2 ( sandev, "INT13 extensions installation check\n" ); ix86->regs.bx = 0xaa55; ix86->regs.cx = ( INT13_EXTENSION_LINEAR | INT13_EXTENSION_EDD | @@ -1003,12 +665,12 @@ static int int13_extension_check ( struct int13_drive *int13 __unused, /** * Extended read / write * - * @v int13 Emulated drive + * @v sandev SAN device * @v ds:si Disk address packet * @v block_rw Block read/write method * @ret status Status code */ -static int int13_extended_rw ( struct int13_drive *int13, +static int int13_extended_rw ( struct san_device *sandev, struct i386_all_regs *ix86, int ( * block_rw ) ( struct interface *control, struct interface *data, @@ -1027,14 +689,14 @@ static int int13_extended_rw ( struct int13_drive *int13, * ELTORITO.SYS seems to assume that we are really a CD-ROM if * we support extended reads for a floppy drive. */ - if ( int13_is_fdd ( int13 ) ) + if ( int13_is_fdd ( sandev ) ) return -INT13_STATUS_INVALID; /* Get buffer size */ get_real ( bufsize, ix86->segs.ds, ( ix86->regs.si + offsetof ( typeof ( addr ), bufsize ) ) ); if ( bufsize < offsetof ( typeof ( addr ), buffer_phys ) ) { - DBGC2 ( int13, "\n", bufsize ); + DBGC2 ( sandev, "\n", bufsize ); return -INT13_STATUS_INVALID; } @@ -1042,17 +704,17 @@ static int int13_extended_rw ( struct int13_drive *int13, memset ( &addr, 0, sizeof ( addr ) ); copy_from_real ( &addr, ix86->segs.ds, ix86->regs.si, bufsize ); lba = addr.lba; - DBGC2 ( int13, "LBA %08llx <-> ", ( ( unsigned long long ) lba ) ); + DBGC2 ( sandev, "LBA %08llx <-> ", ( ( unsigned long long ) lba ) ); if ( ( addr.count == 0xff ) || ( ( addr.buffer.segment == 0xffff ) && ( addr.buffer.offset == 0xffff ) ) ) { buffer = phys_to_user ( addr.buffer_phys ); - DBGC2 ( int13, "%08llx", + DBGC2 ( sandev, "%08llx", ( ( unsigned long long ) addr.buffer_phys ) ); } else { buffer = real_to_user ( addr.buffer.segment, addr.buffer.offset ); - DBGC2 ( int13, "%04x:%04x", addr.buffer.segment, + DBGC2 ( sandev, "%04x:%04x", addr.buffer.segment, addr.buffer.offset ); } if ( addr.count <= 0x7f ) { @@ -1060,15 +722,15 @@ static int int13_extended_rw ( struct int13_drive *int13, } else if ( addr.count == 0xff ) { count = addr.long_count; } else { - DBGC2 ( int13, " \n", addr.count ); + DBGC2 ( sandev, " \n", addr.count ); return -INT13_STATUS_INVALID; } - DBGC2 ( int13, " (count %ld)\n", count ); + DBGC2 ( sandev, " (count %ld)\n", count ); /* Read from / write to block device */ - if ( ( rc = int13_rw ( int13, lba, count, buffer, block_rw ) ) != 0 ) { - DBGC ( int13, "INT13 drive %02x extended I/O failed: %s\n", - int13->drive, strerror ( rc ) ); + if ( ( rc = sandev_rw ( sandev, lba, count, buffer, block_rw ) ) != 0 ){ + DBGC ( sandev, "INT13 drive %02x extended I/O failed: %s\n", + sandev->drive, strerror ( rc ) ); /* Record that no blocks were transferred successfully */ addr.count = 0; put_real ( addr.count, ix86->segs.ds, @@ -1083,37 +745,39 @@ static int int13_extended_rw ( struct int13_drive *int13, /** * INT 13, 42 - Extended read * - * @v int13 Emulated drive + * @v sandev SAN device * @v ds:si Disk address packet * @ret status Status code */ -static int int13_extended_read ( struct int13_drive *int13, +static int int13_extended_read ( struct san_device *sandev, struct i386_all_regs *ix86 ) { - DBGC2 ( int13, "Extended read: " ); - return int13_extended_rw ( int13, ix86, block_read ); + + DBGC2 ( sandev, "Extended read: " ); + return int13_extended_rw ( sandev, ix86, block_read ); } /** * INT 13, 43 - Extended write * - * @v int13 Emulated drive + * @v sandev SAN device * @v ds:si Disk address packet * @ret status Status code */ -static int int13_extended_write ( struct int13_drive *int13, +static int int13_extended_write ( struct san_device *sandev, struct i386_all_regs *ix86 ) { - DBGC2 ( int13, "Extended write: " ); - return int13_extended_rw ( int13, ix86, block_write ); + + DBGC2 ( sandev, "Extended write: " ); + return int13_extended_rw ( sandev, ix86, block_write ); } /** * INT 13, 44 - Verify sectors * - * @v int13 Emulated drive + * @v sandev SAN device * @v ds:si Disk address packet * @ret status Status code */ -static int int13_extended_verify ( struct int13_drive *int13, +static int int13_extended_verify ( struct san_device *sandev, struct i386_all_regs *ix86 ) { struct int13_disk_address addr; uint64_t lba; @@ -1125,7 +789,7 @@ static int int13_extended_verify ( struct int13_drive *int13, sizeof ( addr )); lba = addr.lba; count = addr.count; - DBGC2 ( int13, "Verify: LBA %08llx (count %ld)\n", + DBGC2 ( sandev, "Verify: LBA %08llx (count %ld)\n", ( ( unsigned long long ) lba ), count ); } @@ -1136,11 +800,11 @@ static int int13_extended_verify ( struct int13_drive *int13, /** * INT 13, 44 - Extended seek * - * @v int13 Emulated drive + * @v sandev SAN device * @v ds:si Disk address packet * @ret status Status code */ -static int int13_extended_seek ( struct int13_drive *int13, +static int int13_extended_seek ( struct san_device *sandev, struct i386_all_regs *ix86 ) { struct int13_disk_address addr; uint64_t lba; @@ -1152,7 +816,7 @@ static int int13_extended_seek ( struct int13_drive *int13, sizeof ( addr )); lba = addr.lba; count = addr.count; - DBGC2 ( int13, "Seek: LBA %08llx (count %ld)\n", + DBGC2 ( sandev, "Seek: LBA %08llx (count %ld)\n", ( ( unsigned long long ) lba ), count ); } @@ -1163,11 +827,11 @@ static int int13_extended_seek ( struct int13_drive *int13, /** * Build device path information * - * @v int13 Emulated drive + * @v sandev SAN device * @v dpi Device path information * @ret rc Return status code */ -static int int13_device_path_info ( struct int13_drive *int13, +static int int13_device_path_info ( struct san_device *sandev, struct edd_device_path_information *dpi ) { struct device *device; struct device_description *desc; @@ -1176,15 +840,15 @@ static int int13_device_path_info ( struct int13_drive *int13, int rc; /* Reopen block device if necessary */ - if ( ( int13->block_rc != 0 ) && - ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) ) + if ( sandev_needs_reopen ( sandev ) && + ( ( rc = sandev_reopen ( sandev ) ) != 0 ) ) return rc; /* Get underlying hardware device */ - device = identify_device ( &int13->block ); + device = identify_device ( &sandev->block ); if ( ! device ) { - DBGC ( int13, "INT13 drive %02x cannot identify hardware " - "device\n", int13->drive ); + DBGC ( sandev, "INT13 drive %02x cannot identify hardware " + "device\n", sandev->drive ); return -ENODEV; } @@ -1199,16 +863,16 @@ static int int13_device_path_info ( struct int13_drive *int13, dpi->interface_path.pci.channel = 0xff; /* unused */ break; default: - DBGC ( int13, "INT13 drive %02x unrecognised bus type %d\n", - int13->drive, desc->bus_type ); + DBGC ( sandev, "INT13 drive %02x unrecognised bus type %d\n", + sandev->drive, desc->bus_type ); return -ENOTSUP; } /* Get EDD block device description */ - if ( ( rc = edd_describe ( &int13->block, &dpi->interface_type, + if ( ( rc = edd_describe ( &sandev->block, &dpi->interface_type, &dpi->device_path ) ) != 0 ) { - DBGC ( int13, "INT13 drive %02x cannot identify block device: " - "%s\n", int13->drive, strerror ( rc ) ); + DBGC ( sandev, "INT13 drive %02x cannot identify block device: " + "%s\n", sandev->drive, strerror ( rc ) ); return rc; } @@ -1225,12 +889,13 @@ static int int13_device_path_info ( struct int13_drive *int13, /** * INT 13, 48 - Get extended parameters * - * @v int13 Emulated drive + * @v sandev SAN device * @v ds:si Drive parameter table * @ret status Status code */ -static int int13_get_extended_parameters ( struct int13_drive *int13, +static int int13_get_extended_parameters ( struct san_device *sandev, struct i386_all_regs *ix86 ) { + struct int13_data *int13 = sandev->priv; struct int13_disk_parameters params; struct segoff address; size_t len = sizeof ( params ); @@ -1241,26 +906,26 @@ static int int13_get_extended_parameters ( struct int13_drive *int13, get_real ( bufsize, ix86->segs.ds, ( ix86->regs.si + offsetof ( typeof ( params ), bufsize ))); - DBGC2 ( int13, "Get extended drive parameters to %04x:%04x+%02x\n", + DBGC2 ( sandev, "Get extended drive parameters to %04x:%04x+%02x\n", ix86->segs.ds, ix86->regs.si, bufsize ); /* Build drive parameters */ memset ( ¶ms, 0, sizeof ( params ) ); params.flags = INT13_FL_DMA_TRANSPARENT; if ( ( int13->cylinders < 1024 ) && - ( int13_capacity ( int13 ) <= INT13_MAX_CHS_SECTORS ) ) { + ( sandev_capacity ( sandev ) <= INT13_MAX_CHS_SECTORS ) ) { params.flags |= INT13_FL_CHS_VALID; } params.cylinders = int13->cylinders; params.heads = int13->heads; params.sectors_per_track = int13->sectors_per_track; - params.sectors = int13_capacity ( int13 ); - params.sector_size = int13_blksize ( int13 ); + params.sectors = sandev_capacity ( sandev ); + params.sector_size = sandev_blksize ( sandev ); memset ( ¶ms.dpte, 0xff, sizeof ( params.dpte ) ); - if ( ( rc = int13_device_path_info ( int13, ¶ms.dpi ) ) != 0 ) { - DBGC ( int13, "INT13 drive %02x could not provide device " + if ( ( rc = int13_device_path_info ( sandev, ¶ms.dpi ) ) != 0 ) { + DBGC ( sandev, "INT13 drive %02x could not provide device " "path information: %s\n", - int13->drive, strerror ( rc ) ); + sandev->drive, strerror ( rc ) ); len = offsetof ( typeof ( params ), dpi ); } @@ -1276,11 +941,11 @@ static int int13_get_extended_parameters ( struct int13_drive *int13, params.bufsize = offsetof ( typeof ( params ), dpi ); } - DBGC ( int13, "INT 13 drive %02x described using extended " - "parameters:\n", int13->drive ); + DBGC ( sandev, "INT 13 drive %02x described using extended " + "parameters:\n", sandev->drive ); address.segment = ix86->segs.ds; address.offset = ix86->regs.si; - DBGC_HDA ( int13, address, ¶ms, len ); + DBGC_HDA ( sandev, address, ¶ms, len ); /* Return drive parameters */ if ( len > bufsize ) @@ -1293,29 +958,29 @@ static int int13_get_extended_parameters ( struct int13_drive *int13, /** * INT 13, 4b - Get status or terminate CD-ROM emulation * - * @v int13 Emulated drive + * @v sandev SAN device * @v ds:si Specification packet * @ret status Status code */ -static int int13_cdrom_status_terminate ( struct int13_drive *int13, +static int int13_cdrom_status_terminate ( struct san_device *sandev, struct i386_all_regs *ix86 ) { struct int13_cdrom_specification specification; - DBGC2 ( int13, "Get CD-ROM emulation status to %04x:%04x%s\n", + DBGC2 ( sandev, "Get CD-ROM emulation status to %04x:%04x%s\n", ix86->segs.ds, ix86->regs.si, ( ix86->regs.al ? "" : " and terminate" ) ); /* Fail if we are not a CD-ROM */ - if ( ! int13->is_cdrom ) { - DBGC ( int13, "INT13 drive %02x is not a CD-ROM\n", - int13->drive ); + if ( ! sandev->is_cdrom ) { + DBGC ( sandev, "INT13 drive %02x is not a CD-ROM\n", + sandev->drive ); return -INT13_STATUS_INVALID; } /* Build specification packet */ memset ( &specification, 0, sizeof ( specification ) ); specification.size = sizeof ( specification ); - specification.drive = int13->drive; + specification.drive = sandev->drive; /* Return specification packet */ copy_to_real ( ix86->segs.ds, ix86->regs.si, &specification, @@ -1328,33 +993,34 @@ static int int13_cdrom_status_terminate ( struct int13_drive *int13, /** * INT 13, 4d - Read CD-ROM boot catalog * - * @v int13 Emulated drive + * @v sandev SAN device * @v ds:si Command packet * @ret status Status code */ -static int int13_cdrom_read_boot_catalog ( struct int13_drive *int13, +static int int13_cdrom_read_boot_catalog ( struct san_device *sandev, struct i386_all_regs *ix86 ) { + struct int13_data *int13 = sandev->priv; struct int13_cdrom_boot_catalog_command command; int rc; /* Read parameters from command packet */ copy_from_real ( &command, ix86->segs.ds, ix86->regs.si, sizeof ( command ) ); - DBGC2 ( int13, "Read CD-ROM boot catalog to %08x\n", command.buffer ); + DBGC2 ( sandev, "Read CD-ROM boot catalog to %08x\n", command.buffer ); /* Fail if we have no boot catalog */ if ( ! int13->boot_catalog ) { - DBGC ( int13, "INT13 drive %02x has no boot catalog\n", - int13->drive ); + DBGC ( sandev, "INT13 drive %02x has no boot catalog\n", + sandev->drive ); return -INT13_STATUS_INVALID; } /* Read from boot catalog */ - if ( ( rc = int13_rw ( int13, ( int13->boot_catalog + command.start ), - command.count, phys_to_user ( command.buffer ), - block_read ) ) != 0 ) { - DBGC ( int13, "INT13 drive %02x could not read boot catalog: " - "%s\n", int13->drive, strerror ( rc ) ); + if ( ( rc = sandev_rw ( sandev, ( int13->boot_catalog + command.start ), + command.count, phys_to_user ( command.buffer ), + block_read ) ) != 0 ) { + DBGC ( sandev, "INT13 drive %02x could not read boot catalog: " + "%s\n", sandev->drive, strerror ( rc ) ); return -INT13_STATUS_READ_ERROR; } @@ -1368,79 +1034,81 @@ static int int13_cdrom_read_boot_catalog ( struct int13_drive *int13, static __asmcall void int13 ( struct i386_all_regs *ix86 ) { int command = ix86->regs.ah; unsigned int bios_drive = ix86->regs.dl; - struct int13_drive *int13; + struct san_device *sandev; + struct int13_data *int13; int status; /* Check BIOS hasn't killed off our drive */ int13_check_num_drives(); - list_for_each_entry ( int13, &int13s, list ) { + for_each_sandev ( sandev ) { - if ( bios_drive != int13->drive ) { + int13 = sandev->priv; + if ( bios_drive != sandev->drive ) { /* Remap any accesses to this drive's natural number */ if ( bios_drive == int13->natural_drive ) { - DBGC2 ( int13, "INT13,%02x (%02x) remapped to " + DBGC2 ( sandev, "INT13,%02x (%02x) remapped to " "(%02x)\n", ix86->regs.ah, - bios_drive, int13->drive ); - ix86->regs.dl = int13->drive; + bios_drive, sandev->drive ); + ix86->regs.dl = sandev->drive; return; } else if ( ( ( bios_drive & 0x7f ) == 0x7f ) && ( command == INT13_CDROM_STATUS_TERMINATE ) - && int13->is_cdrom ) { + && sandev->is_cdrom ) { /* Catch non-drive-specific CD-ROM calls */ } else { continue; } } - DBGC2 ( int13, "INT13,%02x (%02x): ", + DBGC2 ( sandev, "INT13,%02x (%02x): ", ix86->regs.ah, bios_drive ); switch ( command ) { case INT13_RESET: - status = int13_reset ( int13, ix86 ); + status = int13_reset ( sandev, ix86 ); break; case INT13_GET_LAST_STATUS: - status = int13_get_last_status ( int13, ix86 ); + status = int13_get_last_status ( sandev, ix86 ); break; case INT13_READ_SECTORS: - status = int13_read_sectors ( int13, ix86 ); + status = int13_read_sectors ( sandev, ix86 ); break; case INT13_WRITE_SECTORS: - status = int13_write_sectors ( int13, ix86 ); + status = int13_write_sectors ( sandev, ix86 ); break; case INT13_GET_PARAMETERS: - status = int13_get_parameters ( int13, ix86 ); + status = int13_get_parameters ( sandev, ix86 ); break; case INT13_GET_DISK_TYPE: - status = int13_get_disk_type ( int13, ix86 ); + status = int13_get_disk_type ( sandev, ix86 ); break; case INT13_EXTENSION_CHECK: - status = int13_extension_check ( int13, ix86 ); + status = int13_extension_check ( sandev, ix86 ); break; case INT13_EXTENDED_READ: - status = int13_extended_read ( int13, ix86 ); + status = int13_extended_read ( sandev, ix86 ); break; case INT13_EXTENDED_WRITE: - status = int13_extended_write ( int13, ix86 ); + status = int13_extended_write ( sandev, ix86 ); break; case INT13_EXTENDED_VERIFY: - status = int13_extended_verify ( int13, ix86 ); + status = int13_extended_verify ( sandev, ix86 ); break; case INT13_EXTENDED_SEEK: - status = int13_extended_seek ( int13, ix86 ); + status = int13_extended_seek ( sandev, ix86 ); break; case INT13_GET_EXTENDED_PARAMETERS: - status = int13_get_extended_parameters ( int13, ix86 ); + status = int13_get_extended_parameters ( sandev, ix86 ); break; case INT13_CDROM_STATUS_TERMINATE: - status = int13_cdrom_status_terminate ( int13, ix86 ); + status = int13_cdrom_status_terminate ( sandev, ix86 ); break; case INT13_CDROM_READ_BOOT_CATALOG: - status = int13_cdrom_read_boot_catalog ( int13, ix86 ); + status = int13_cdrom_read_boot_catalog ( sandev, ix86 ); break; default: - DBGC2 ( int13, "*** Unrecognised INT13 ***\n" ); + DBGC2 ( sandev, "*** Unrecognised INT13 ***\n" ); status = -INT13_STATUS_INVALID; break; } @@ -1451,8 +1119,8 @@ static __asmcall void int13 ( struct i386_all_regs *ix86 ) { /* Negative status indicates an error */ if ( status < 0 ) { status = -status; - DBGC ( int13, "INT13,%02x (%02x) failed with status " - "%02x\n", ix86->regs.ah, int13->drive, status ); + DBGC ( sandev, "INT13,%02x (%02x) failed with status " + "%02x\n", ix86->regs.ah, sandev->drive, status ); } else { ix86->flags &= ~CF; } @@ -1529,66 +1197,7 @@ static void int13_unhook_vector ( void ) { } /** - * Check INT13 emulated drive flow control window - * - * @v int13 Emulated drive - */ -static size_t int13_block_window ( struct int13_drive *int13 __unused ) { - - /* We are never ready to receive data via this interface. - * This prevents objects that support both block and stream - * interfaces from attempting to send us stream data. - */ - return 0; -} - -/** - * Handle INT 13 emulated drive underlying block device closing - * - * @v int13 Emulated drive - * @v rc Reason for close - */ -static void int13_block_close ( struct int13_drive *int13, int rc ) { - - /* Any closing is an error from our point of view */ - if ( rc == 0 ) - rc = -ENOTCONN; - - DBGC ( int13, "INT13 drive %02x went away: %s\n", - int13->drive, strerror ( rc ) ); - - /* Record block device error code */ - int13->block_rc = rc; - - /* Shut down interfaces */ - intf_restart ( &int13->block, rc ); -} - -/** INT 13 drive interface operations */ -static struct interface_operation int13_block_op[] = { - INTF_OP ( xfer_window, struct int13_drive *, int13_block_window ), - INTF_OP ( intf_close, struct int13_drive *, int13_block_close ), -}; - -/** INT 13 drive interface descriptor */ -static struct interface_descriptor int13_block_desc = - INTF_DESC ( struct int13_drive, block, int13_block_op ); - -/** - * Free INT 13 emulated drive - * - * @v refcnt Reference count - */ -static void int13_free ( struct refcnt *refcnt ) { - struct int13_drive *int13 = - container_of ( refcnt, struct int13_drive, refcnt ); - - uri_put ( int13->uri ); - free ( int13 ); -} - -/** - * Hook INT 13 emulated drive + * Hook INT 13 SAN device * * @v uri URI * @v drive Drive number @@ -1598,9 +1207,11 @@ static void int13_free ( struct refcnt *refcnt ) { * the INT 13 interrupt vector (if not already hooked). */ static int int13_hook ( struct uri *uri, unsigned int drive ) { - struct int13_drive *int13; + struct san_device *sandev; + struct int13_data *int13; unsigned int natural_drive; void *scratch; + int need_hook = ( ! have_sandevs() ); int rc; /* Calculate natural drive number */ @@ -1611,60 +1222,49 @@ static int int13_hook ( struct uri *uri, unsigned int drive ) { if ( ( drive & 0x7f ) == 0x7f ) drive = natural_drive; - /* Check that drive number is not in use */ - list_for_each_entry ( int13, &int13s, list ) { - if ( int13->drive == drive ) { - rc = -EADDRINUSE; - goto err_in_use; - } - } - - /* Allocate and initialise structure */ - int13 = zalloc ( sizeof ( *int13 ) ); - if ( ! int13 ) { + /* Allocate SAN device */ + sandev = alloc_sandev ( uri, sizeof ( *int13 ) ); + if ( ! sandev ) { rc = -ENOMEM; - goto err_zalloc; + goto err_alloc; } - ref_init ( &int13->refcnt, int13_free ); - intf_init ( &int13->block, &int13_block_desc, &int13->refcnt ); - int13->uri = uri_get ( uri ); - int13->drive = drive; + sandev->drive = drive; + int13 = sandev->priv; int13->natural_drive = natural_drive; - /* Open block device interface */ - if ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) - goto err_reopen_block; - - /* Read device capacity */ - if ( ( rc = int13_read_capacity ( int13 ) ) != 0 ) - goto err_read_capacity; + /* Register SAN device */ + if ( ( rc = register_sandev ( sandev ) ) != 0 ) { + DBGC ( sandev, "INT13 drive %02x could not register: %s\n", + sandev->drive, strerror ( rc ) ); + goto err_register; + } /* Allocate scratch area */ - scratch = malloc ( int13_blksize ( int13 ) ); + scratch = malloc ( sandev_blksize ( sandev ) ); if ( ! scratch ) goto err_alloc_scratch; /* Parse parameters, if present */ - if ( ( rc = int13_parse_iso9660 ( int13, scratch ) ) != 0 ) - goto err_parse_iso9660; + if ( sandev->is_cdrom && + ( ( rc = int13_parse_eltorito ( sandev, scratch ) ) != 0 ) ) + goto err_parse_eltorito; - /* Give drive a default geometry */ - if ( ( rc = int13_guess_geometry ( int13, scratch ) ) != 0 ) + /* Give drive a default geometry, if applicable */ + if ( ( sandev_blksize ( sandev ) == INT13_BLKSIZE ) && + ( ( rc = int13_guess_geometry ( sandev, scratch ) ) != 0 ) ) goto err_guess_geometry; - DBGC ( int13, "INT13 drive %02x (naturally %02x) registered with C/H/S " - "geometry %d/%d/%d\n", int13->drive, int13->natural_drive, - int13->cylinders, int13->heads, int13->sectors_per_track ); + DBGC ( sandev, "INT13 drive %02x (naturally %02x) registered with " + "C/H/S geometry %d/%d/%d\n", + sandev->drive, int13->natural_drive, int13->cylinders, + int13->heads, int13->sectors_per_track ); /* Hook INT 13 vector if not already hooked */ - if ( list_empty ( &int13s ) ) { + if ( need_hook ) { int13_hook_vector(); devices_get(); } - /* Add to list of emulated drives */ - list_add ( &int13->list, &int13s ); - /* Update BIOS drive count */ int13_sync_num_drives(); @@ -1672,73 +1272,52 @@ static int int13_hook ( struct uri *uri, unsigned int drive ) { return drive; err_guess_geometry: - err_parse_iso9660: + err_parse_eltorito: free ( scratch ); err_alloc_scratch: - err_read_capacity: - err_reopen_block: - intf_shutdown ( &int13->block, rc ); - ref_put ( &int13->refcnt ); - err_zalloc: - err_in_use: + unregister_sandev ( sandev ); + err_register: + sandev_put ( sandev ); + err_alloc: return rc; } /** - * Find INT 13 emulated drive by drive number - * - * @v drive Drive number - * @ret int13 Emulated drive, or NULL - */ -static struct int13_drive * int13_find ( unsigned int drive ) { - struct int13_drive *int13; - - list_for_each_entry ( int13, &int13s, list ) { - if ( int13->drive == drive ) - return int13; - } - return NULL; -} - -/** - * Unhook INT 13 emulated drive + * Unhook INT 13 SAN device * * @v drive Drive number * * Unregisters the drive from the INT 13 emulation subsystem. If this - * is the last emulated drive, the INT 13 vector is unhooked (if + * is the last SAN device, the INT 13 vector is unhooked (if * possible). */ static void int13_unhook ( unsigned int drive ) { - struct int13_drive *int13; + struct san_device *sandev; /* Find drive */ - int13 = int13_find ( drive ); - if ( ! int13 ) { - DBG ( "INT13 cannot find emulated drive %02x\n", drive ); + sandev = sandev_find ( drive ); + if ( ! sandev ) { + DBG ( "INT13 cannot find drive %02x\n", drive ); return; } - /* Shut down interfaces */ - intf_shutdown ( &int13->block, 0 ); - - /* Remove from list of emulated drives */ - list_del ( &int13->list ); + /* Unregister SAN device */ + unregister_sandev ( sandev ); /* Should adjust BIOS drive count, but it's difficult * to do so reliably. */ - DBGC ( int13, "INT13 drive %02x unregistered\n", int13->drive ); + DBGC ( sandev, "INT13 drive %02x unregistered\n", sandev->drive ); /* Unhook INT 13 vector if no more drives */ - if ( list_empty ( &int13s ) ) { + if ( ! have_sandevs() ) { devices_put(); int13_unhook_vector(); } - /* Drop list's reference to drive */ - ref_put ( &int13->refcnt ); + /* Drop reference to drive */ + sandev_put ( sandev ); } /** @@ -1939,26 +1518,26 @@ static union xbft_table __bss16 ( xbftab ) __attribute__ (( aligned ( 16 ) )); #define xbftab __use_data16 ( xbftab ) /** - * Describe INT 13 emulated drive for SAN-booted operating system + * Describe SAN device for SAN-booted operating system * * @v drive Drive number * @ret rc Return status code */ static int int13_describe ( unsigned int drive ) { - struct int13_drive *int13; + struct san_device *sandev; struct segoff xbft_address; int rc; /* Find drive */ - int13 = int13_find ( drive ); - if ( ! int13 ) { - DBG ( "INT13 cannot find emulated drive %02x\n", drive ); + sandev = sandev_find ( drive ); + if ( ! sandev ) { + DBG ( "INT13 cannot find drive %02x\n", drive ); return -ENODEV; } /* Reopen block device if necessary */ - if ( ( int13->block_rc != 0 ) && - ( ( rc = int13_reopen_block ( int13 ) ) != 0 ) ) + if ( sandev_needs_reopen ( sandev ) && + ( ( rc = sandev_reopen ( sandev ) ) != 0 ) ) return rc; /* Clear table */ @@ -1971,10 +1550,10 @@ static int int13_describe ( unsigned int drive ) { sizeof ( xbftab.acpi.oem_table_id ) ); /* Fill in remaining parameters */ - if ( ( rc = acpi_describe ( &int13->block, &xbftab.acpi, + if ( ( rc = acpi_describe ( &sandev->block, &xbftab.acpi, sizeof ( xbftab ) ) ) != 0 ) { - DBGC ( int13, "INT13 drive %02x could not create ACPI " - "description: %s\n", int13->drive, strerror ( rc ) ); + DBGC ( sandev, "INT13 drive %02x could not create ACPI " + "description: %s\n", sandev->drive, strerror ( rc ) ); return rc; } @@ -1982,9 +1561,9 @@ static int int13_describe ( unsigned int drive ) { acpi_fix_checksum ( &xbftab.acpi ); xbft_address.segment = rm_ds; xbft_address.offset = __from_data16 ( &xbftab ); - DBGC ( int13, "INT13 drive %02x described using boot firmware " - "table:\n", int13->drive ); - DBGC_HDA ( int13, xbft_address, &xbftab, + DBGC ( sandev, "INT13 drive %02x described using boot firmware " + "table:\n", sandev->drive ); + DBGC_HDA ( sandev, xbft_address, &xbftab, le32_to_cpu ( xbftab.acpi.length ) ); return 0;