From daef8fbf0cead350e637088cdd6b8dbaace17f4f Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 4 Oct 2011 16:23:59 +0200 Subject: [PATCH 24/76] ide/atapi: Introduce CHECK_READY flag for commands RH-Author: Markus Armbruster Message-id: <1317745491-18401-17-git-send-email-armbru@redhat.com> Patchwork-id: 33666 O-Subject: [PATCH RHEL-6.2 qemu-kvm 16/68] ide/atapi: Introduce CHECK_READY flag for commands Bugzilla: 742458 RH-Acked-by: Paolo Bonzini RH-Acked-by: Amit Shah RH-Acked-by: Juan Quintela From: Kevin Wolf Some commands are supposed to report a Not Ready Condition (i.e. they require a medium to be present in order to execute successfully). Instead of duplicating the check in each command implementation, let's add a flag and check it before calling the command. This patch only converts existing checks, it does not introduce new checks for any of the other commands that can/should report a Not Ready Condition. Signed-off-by: Kevin Wolf (cherry picked from commit 7a2c4b82340d621bff462672b29c88d2020d68c1) Signed-off-by: Markus Armbruster --- hw/ide/atapi.c | 48 +++++++++++++++++++++++------------------------- 1 files changed, 23 insertions(+), 25 deletions(-) Signed-off-by: Michal Novotny --- hw/ide/atapi.c | 48 +++++++++++++++++++++++------------------------- 1 files changed, 23 insertions(+), 25 deletions(-) diff --git a/hw/ide/atapi.c b/hw/ide/atapi.c index 9dbd4bf..a09299e 100644 --- a/hw/ide/atapi.c +++ b/hw/ide/atapi.c @@ -821,11 +821,9 @@ error_cmd: static void cmd_test_unit_ready(IDEState *s, uint8_t *buf) { - if (bdrv_is_inserted(s->bs)) { - ide_atapi_cmd_ok(s); - } else { - ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT); - } + /* Not Ready Conditions are already handled in ide_atapi_cmd(), so if we + * come here, we know that it's ready. */ + ide_atapi_cmd_ok(s); } static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf) @@ -891,11 +889,6 @@ static void cmd_seek(IDEState *s, uint8_t* buf) unsigned int lba; uint64_t total_sectors = s->nb_sectors >> 2; - if (total_sectors == 0) { - ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT); - return; - } - lba = ube32_to_cpu(buf + 2); if (lba >= total_sectors) { ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR); @@ -949,13 +942,8 @@ static void cmd_mechanism_status(IDEState *s, uint8_t* buf) static void cmd_read_toc_pma_atip(IDEState *s, uint8_t* buf) { int format, msf, start_track, len; - uint64_t total_sectors = s->nb_sectors >> 2; int max_len; - - if (total_sectors == 0) { - ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT); - return; - } + uint64_t total_sectors = s->nb_sectors >> 2; max_len = ube16_to_cpu(buf + 7); format = buf[9] >> 6; @@ -994,11 +982,6 @@ static void cmd_read_cdvd_capacity(IDEState *s, uint8_t* buf) { uint64_t total_sectors = s->nb_sectors >> 2; - if (total_sectors == 0) { - ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT); - return; - } - /* NOTE: it is really the number of sectors minus 1 */ cpu_to_ube32(buf, total_sectors - 1); cpu_to_ube32(buf + 4, 2048); @@ -1070,22 +1053,29 @@ enum { * unit attention condition. (See MMC-5, section 4.1.6.1) */ ALLOW_UA = 0x01, + + /* + * Commands flagged with CHECK_READY can only execute if a medium is present. + * Otherwise they report the Not Ready Condition. (See MMC-5, section + * 4.1.8) + */ + CHECK_READY = 0x02, }; static const struct { void (*handler)(IDEState *s, uint8_t *buf); int flags; } atapi_cmd_table[0x100] = { - [ 0x00 ] = { cmd_test_unit_ready, 0 }, + [ 0x00 ] = { cmd_test_unit_ready, CHECK_READY }, [ 0x03 ] = { cmd_request_sense, ALLOW_UA }, [ 0x12 ] = { cmd_inquiry, ALLOW_UA }, [ 0x1a ] = { cmd_mode_sense, /* (6) */ 0 }, [ 0x1b ] = { cmd_start_stop_unit, 0 }, [ 0x1e ] = { cmd_prevent_allow_medium_removal, 0 }, - [ 0x25 ] = { cmd_read_cdvd_capacity, 0 }, + [ 0x25 ] = { cmd_read_cdvd_capacity, CHECK_READY }, [ 0x28 ] = { cmd_read, /* (10) */ 0 }, - [ 0x2b ] = { cmd_seek, 0 }, - [ 0x43 ] = { cmd_read_toc_pma_atip, 0 }, + [ 0x2b ] = { cmd_seek, CHECK_READY }, + [ 0x43 ] = { cmd_read_toc_pma_atip, CHECK_READY }, [ 0x46 ] = { cmd_get_configuration, ALLOW_UA }, [ 0x4a ] = { cmd_get_event_status_notification, ALLOW_UA }, [ 0x5a ] = { cmd_mode_sense, /* (10) */ 0 }, @@ -1134,6 +1124,14 @@ void ide_atapi_cmd(IDEState *s) return; } + /* Report a Not Ready condition if appropriate for the command */ + if ((atapi_cmd_table[s->io_buffer[0]].flags & CHECK_READY) && + (!media_present(s) || !bdrv_is_inserted(s->bs))) + { + ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT); + return; + } + /* Execute the command */ if (atapi_cmd_table[s->io_buffer[0]].handler) { atapi_cmd_table[s->io_buffer[0]].handler(s, buf); -- 1.7.4.4