atapi: Some code restructuring
v2 cover atapi: Some code restructuring
v2:
- Fixed commit messages
- Added a comment in cmd_test_unit_ready
Kevin Wolf (5):
ide: Split atapi.c out
ide/atapi: Factor commands out
ide/atapi: Use table instead of switch for commands ide/atapi: Replace bdrv_get_geometry calls by s->nb_sectors ide/atapi: Introduce CHECK_READY flag for commands
Makefile.objs | 2 +- hw/ide/atapi.c | 1134 +++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/ide/core.c | 1065 +------------------------------------------------- hw/ide/internal.h | 14 +- 4 files changed, 1149 insertions(+), 1066 deletions(-) create mode 100644 hw/ide/atapi.c
Signature · 1 line
1.7.2.3
PATCH v2 1/5 ide: Split atapi.c out
Besides moving code, this patch only fixes some whitespace issues in the moved code and makes all functions in atapi.c static which can be static.
--- Makefile.objs | 2 +- hw/ide/atapi.c | 1083 +++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/ide/core.c | 1065 +--------------------------------------------------- hw/ide/internal.h | 14 +- 4 files changed, 1098 insertions(+), 1066 deletions(-) create mode 100644 hw/ide/atapi.c
Makefile.objs +1 -1
@@ -242,7 +242,7 @@ hw-obj-$(CONFIG_LAN9118) += lan9118.o hw-obj-$(CONFIG_NE2000_ISA) += ne2000-isa.o # IDE -hw-obj-$(CONFIG_IDE_CORE) += ide/core.o +hw-obj-$(CONFIG_IDE_CORE) += ide/core.o ide/atapi.o hw-obj-$(CONFIG_IDE_QDEV) += ide/qdev.o hw-obj-$(CONFIG_IDE_PCI) += ide/pci.o hw-obj-$(CONFIG_IDE_ISA) += ide/isa.o
hw/ide/atapi.c new file +1083 -0
1,083 lines not on this page · raw message
hw/ide/core.c +4 -1061
1,113 lines not on this page · raw message
hw/ide/internal.h +10 -4
@@ -9,6 +9,7 @@#include <hw/ide.h> #include "block_int.h" #include "iorange.h" +#include "dma.h" /* debug IDE devices */ //#define DEBUG_IDE
@@ -544,10 +545,6 @@ void ide_set_sector(IDEState *s, int64_t sector_num);void ide_dma_error (IDEState *s); -void ide_atapi_cmd_ok (IDEState *s); -void ide_atapi_cmd_error (IDEState *s,int sense_key,int asc); -void ide_atapi_io_error (IDEState *s,int ret); -void ide_ioport_write (void *opaque,uint32_t addr,uint32_t val);uint32_t ide_ioport_read (void *opaque,uint32_t addr1);uint32_t ide_status_read (void *opaque,uint32_t addr);
@@ -570,6 +567,15 @@ void ide_sector_write(IDEState *s);void ide_sector_read (IDEState *s);void ide_flush_cache (IDEState *s); +void ide_transfer_start (IDEState *s,uint8_t *buf,int size, +EndTransferFunc *end_transfer_func); +void ide_transfer_stop (IDEState *s); +void ide_set_inactive (IDEState *s); + +/* hw/ide/atapi.c */ +void ide_atapi_cmd (IDEState *s); +void ide_atapi_cmd_reply_end (IDEState *s); +/* hw/ide/qdev.c */ void ide_bus_new (IDEBus *idebus,DeviceState *dev,int bus_id);IDEDevice *ide_create_drive (IDEBus *bus,int unit,DriveInfo *drive);
Signature · 1 line
1.7.2.3
Re: [Qemu-devel] [PATCH v2 1/5] ide: Split atapi.c out
Quoted 11 lines · Kevin Wolf
Besides moving code, this patch only fixes some whitespace issues in the moved code and makes all functions in
atapi.cstatic which can be static.Signed-off-by Kevin Wolf <[email protected]>--- Makefile.objs | 2 +- hw/ide/atapi.c | 1083 +++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/ide/core.c | 1065 +--------------------------------------------------- hw/ide/internal.h | 14 +- 4 files changed, 1098 insertions(+), 1066 deletions(-) create mode 100644 hw/ide/atapi.c
<snip>
hw/ide/atapi.c line 2
Quoted 5 lines
hw/ide/atapi.c new file +2
-0@@ -0,0 +1,1083 @@ +/* +* QEMU ATAPI Emulatoin
typo here.
hw/ide/atapi.c line 5
Quoted 2 lines
hw/ide/atapi.c new file +2
-0+* Copyright (c) 2003 Fabrice Bellard +* Copyright (c) 2006 Openedhand Ltd.
Could you also add
Copyright (C) 2011 Red Hat Inc.
Copyright (C) 2011 Amit Shah <[email protected]>
<snip>
hw/ide/atapi.c line 74
Quoted 10 lines
hw/ide/atapi.c new file +10
-0+ +static void lba_to_msf (uint8_t *buf,int lba) +{ + lba +=150 ; + buf[0 ] = (lba /75 ) /60 ; + buf[1 ] = (lba /75 ) %60 ; + buf[2 ] = lba %75 ; +} + +/* XXX: DVDs that could fit on a CD will be reported as a CD */
This comment belongs to the function below.
hw/ide/atapi.c line 84
Quoted 10 lines
hw/ide/atapi.c new file +10
-0+static inline int media_present (IDEState *s) +{ +return (s->nb_sectors >0 ); +} + +static inline int media_is_dvd (IDEState *s) +{ +return (media_present (s) && s->nb_sectors >CD_MAX_SECTORS ); +} +
Amit
PATCH v2 2/5 ide/atapi: Factor commands out
In reply to Kevin WolfIn preparation for a table of function pointers, factor each command out from ide_atapi_cmd() into its own function.
--- hw/ide/atapi.c | 837 +++++++++++++++++++++++++++++++------------------------- 1 files changed, 459 insertions(+), 378 deletions(-)
hw/ide/atapi.c +459 -378
@@ -621,11 +621,453 @@ static void handle_get_event_status_notification(IDEState *s,ide_atapi_cmd_reply (s, used_len, max_len); } +static void cmd_request_sense (IDEState *s,uint8_t *buf) +{ +int max_len = buf[4 ]; + +memset (buf,0 ,18 ); + buf[0 ] =0x70 | (1 <<7 ); + buf[2 ] = s->sense_key; + buf[7 ] =10 ; + buf[12 ] = s->asc; + +if (s->sense_key ==SENSE_UNIT_ATTENTION ) { + s->sense_key =SENSE_NONE ; + } + +ide_atapi_cmd_reply (s,18 , max_len); +} + +static void cmd_inquiry (IDEState *s,uint8_t *buf) +{ +int max_len = buf[4 ]; + + buf[0 ] =0x05 ;/* CD-ROM */ + buf[1 ] =0x80 ;/* removable */ + buf[2 ] =0x00 ;/* ISO */ + buf[3 ] =0x21 ;/* ATAPI-2 (XXX: put ATAPI-4 ?) */ + buf[4 ] =31 ;/* additional length */ + buf[5 ] =0 ;/* reserved */ + buf[6 ] =0 ;/* reserved */ + buf[7 ] =0 ;/* reserved */ +padstr8 (buf +8 ,8 ,"QEMU" ); +padstr8 (buf +16 ,16 ,"QEMU DVD-ROM" ); +padstr8 (buf +32 ,4 , s->version); +ide_atapi_cmd_reply (s,36 , max_len); +} + +static void cmd_get_configuration (IDEState *s,uint8_t *buf) +{ +uint32_t len; +uint8_t index =0 ; +int max_len; + +/* only feature 0 is supported */ +if (buf[2 ] !=0 || buf[3 ] !=0 ) { +ide_atapi_cmd_error (s,SENSE_ILLEGAL_REQUEST , +ASC_INV_FIELD_IN_CMD_PACKET ); +return ; + } + +/* XXX: could result in alignment problems in some architectures */ + max_len =ube16_to_cpu (buf +7 ); + +/* +* XXX: avoid overflow for io_buffer if max_len is bigger than +* the size of that buffer (dimensioned to max number of +* sectors to transfer at once) +* +* Only a problem if the feature/profiles grow. +*/ +if (max_len >512 ) { +/* XXX: assume 1 sector */ + max_len =512 ; + } + +memset (buf,0 , max_len); +/* +* the number of sectors from the media tells us which profile +* to use as current. 0 means there is no media +*/ +if (media_is_dvd (s)) { +cpu_to_ube16 (buf +6 ,MMC_PROFILE_DVD_ROM ); + }else if (media_is_cd (s)) { +cpu_to_ube16 (buf +6 ,MMC_PROFILE_CD_ROM ); + } + + buf[10 ] =0x02 |0x01 ;/* persistent and current */ + len =12 ;/* headers: 8 + 4 */ + len +=ide_atapi_set_profile (buf, &index,MMC_PROFILE_DVD_ROM ); + len +=ide_atapi_set_profile (buf, &index,MMC_PROFILE_CD_ROM ); +cpu_to_ube32 (buf, len -4 );/* data length */ + +ide_atapi_cmd_reply (s, len, max_len); +} + +static void cmd_mode_sense (IDEState *s,uint8_t *buf) +{ +int action, code; +int max_len; + +if (buf[0 ] ==GPCMD_MODE_SENSE_10 ) { + max_len =ube16_to_cpu (buf +7 ); + }else { + max_len = buf[4 ]; + } + + action = buf[2 ] >>6 ; + code = buf[2 ] &0x3f ; + +switch (action) { +case 0 :/* current values */ +switch (code) { +case GPMODE_R_W_ERROR_PAGE :/* error recovery */ +cpu_to_ube16 (&buf[0 ],16 +6 ); + buf[2 ] =0x70 ; + buf[3 ] =0 ; + buf[4 ] =0 ; + buf[5 ] =0 ; + buf[6 ] =0 ; + buf[7 ] =0 ; + + buf[8 ] =0x01 ; + buf[9 ] =0x06 ; + buf[10 ] =0x00 ; + buf[11 ] =0x05 ; + buf[12 ] =0x00 ; + buf[13 ] =0x00 ; + buf[14 ] =0x00 ; + buf[15 ] =0x00 ; +ide_atapi_cmd_reply (s,16 , max_len); +break ; +case GPMODE_AUDIO_CTL_PAGE : +cpu_to_ube16 (&buf[0 ],24 +6 ); + buf[2 ] =0x70 ; + buf[3 ] =0 ; + buf[4 ] =0 ; + buf[5 ] =0 ; + buf[6 ] =0 ; + buf[7 ] =0 ; + +/* Fill with CDROM audio volume */ + buf[17 ] =0 ; + buf[19 ] =0 ; + buf[21 ] =0 ; + buf[23 ] =0 ; + +ide_atapi_cmd_reply (s,24 , max_len); +break ; +case GPMODE_CAPABILITIES_PAGE : +cpu_to_ube16 (&buf[0 ],28 +6 ); + buf[2 ] =0x70 ; + buf[3 ] =0 ; + buf[4 ] =0 ; + buf[5 ] =0 ; + buf[6 ] =0 ; + buf[7 ] =0 ; + + buf[8 ] =0x2a ; + buf[9 ] =0x12 ; + buf[10 ] =0x00 ; + buf[11 ] =0x00 ; + +/* Claim PLAY_AUDIO capability (0x01) since some Linux +code checks for this to automount media. */ + buf[12 ] =0x71 ; + buf[13 ] =3 <<5 ; + buf[14 ] = (1 <<0 ) | (1 <<3 ) | (1 <<5 ); +if (bdrv_is_locked (s->bs)) + buf[6 ] |=1 <<1 ; + buf[15 ] =0x00 ; +cpu_to_ube16 (&buf[16 ],706 ); + buf[18 ] =0 ; + buf[19 ] =2 ; +cpu_to_ube16 (&buf[20 ],512 ); +cpu_to_ube16 (&buf[22 ],706 ); + buf[24 ] =0 ; + buf[25 ] =0 ; + buf[26 ] =0 ; + buf[27 ] =0 ; +ide_atapi_cmd_reply (s,28 , max_len); +break ; +default : + goto error_cmd; + } +break ; +case 1 :/* changeable values */ + goto error_cmd; +case 2 :/* default values */ + goto error_cmd; +default : +case 3 :/* saved values */ +ide_atapi_cmd_error (s,SENSE_ILLEGAL_REQUEST , +ASC_SAVING_PARAMETERS_NOT_SUPPORTED ); +break ; + } +return ; + +error_cmd: +ide_atapi_cmd_error (s,SENSE_ILLEGAL_REQUEST ,ASC_INV_FIELD_IN_CMD_PACKET ); +} + +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 ); + } +} + +static void cmd_prevent_allow_medium_removal (IDEState *s,uint8_t * buf) +{ +bdrv_set_locked (s->bs, buf[4 ] &1 ); +ide_atapi_cmd_ok (s); +} + +static void cmd_read (IDEState *s,uint8_t * buf) +{ +int nb_sectors, lba; + +if (buf[0 ] ==GPCMD_READ_10 ) { + nb_sectors =ube16_to_cpu (buf +7 ); + }else { + nb_sectors =ube32_to_cpu (buf +6 ); + } + + lba =ube32_to_cpu (buf +2 ); +if (nb_sectors ==0 ) { +ide_atapi_cmd_ok (s); +return ; + } + +ide_atapi_cmd_read (s, lba, nb_sectors,2048 ); +} + +static void cmd_read_cd (IDEState *s,uint8_t * buf) +{ +int nb_sectors, lba, transfer_request; + + nb_sectors = (buf[6 ] <<16 ) | (buf[7 ] <<8 ) | buf[8 ]; + lba =ube32_to_cpu (buf +2 ); + +if (nb_sectors ==0 ) { +ide_atapi_cmd_ok (s); +return ; + } + + transfer_request = buf[9 ]; +switch (transfer_request &0xf8 ) { +case 0x00 : +/* nothing */ +ide_atapi_cmd_ok (s); +break ; +case 0x10 : +/* normal read */ +ide_atapi_cmd_read (s, lba, nb_sectors,2048 ); +break ; +case 0xf8 : +/* read all data */ +ide_atapi_cmd_read (s, lba, nb_sectors,2352 ); +break ; +default : +ide_atapi_cmd_error (s,SENSE_ILLEGAL_REQUEST , +ASC_INV_FIELD_IN_CMD_PACKET ); +break ; + } +} + +static void cmd_seek (IDEState *s,uint8_t * buf) +{ +unsigned int lba; +uint64_t total_sectors; + +bdrv_get_geometry (s->bs, &total_sectors); + + total_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 ); +return ; + } + +ide_atapi_cmd_ok (s); +} + +static void cmd_start_stop_unit (IDEState *s,uint8_t * buf) +{ +int start, eject, sense, err =0 ; + start = buf[4 ] &1 ; + eject = (buf[4 ] >>1 ) &1 ; + +if (eject) { + err =bdrv_eject (s->bs, !start); + } + +switch (err) { +case 0 : +ide_atapi_cmd_ok (s); +break ; +case -EBUSY : + sense =SENSE_NOT_READY ; +if (bdrv_is_inserted (s->bs)) { + sense =SENSE_ILLEGAL_REQUEST ; + } +ide_atapi_cmd_error (s, sense,ASC_MEDIA_REMOVAL_PREVENTED ); +break ; +default : +ide_atapi_cmd_error (s,SENSE_NOT_READY ,ASC_MEDIUM_NOT_PRESENT ); +break ; + } +} + +static void cmd_mechanism_status (IDEState *s,uint8_t * buf) +{ +int max_len =ube16_to_cpu (buf +8 ); + +cpu_to_ube16 (buf,0 ); +/* no current LBA */ + buf[2 ] =0 ; + buf[3 ] =0 ; + buf[4 ] =0 ; + buf[5 ] =1 ; +cpu_to_ube16 (buf +6 ,0 ); +ide_atapi_cmd_reply (s,8 , max_len); +} + +static void cmd_read_toc_pma_atip (IDEState *s,uint8_t * buf) +{ +int format, msf, start_track, len; +uint64_t total_sectors; +int max_len; + +bdrv_get_geometry (s->bs, &total_sectors); + + total_sectors >>=2 ; +if (total_sectors ==0 ) { +ide_atapi_cmd_error (s,SENSE_NOT_READY ,ASC_MEDIUM_NOT_PRESENT ); +return ; + } + + max_len =ube16_to_cpu (buf +7 ); + format = buf[9 ] >>6 ; + msf = (buf[1 ] >>1 ) &1 ; + start_track = buf[6 ]; + +switch (format) { +case 0 : + len =cdrom_read_toc (total_sectors, buf, msf, start_track); +if (len <0 ) + goto error_cmd; +ide_atapi_cmd_reply (s, len, max_len); +break ; +case 1 : +/* multi session : only a single session defined */ +memset (buf,0 ,12 ); + buf[1 ] =0x0a ; + buf[2 ] =0x01 ; + buf[3 ] =0x01 ; +ide_atapi_cmd_reply (s,12 , max_len); +break ; +case 2 : + len =cdrom_read_toc_raw (total_sectors, buf, msf, start_track); +if (len <0 ) + goto error_cmd; +ide_atapi_cmd_reply (s, len, max_len); +break ; +default : + error_cmd: +ide_atapi_cmd_error (s,SENSE_ILLEGAL_REQUEST , +ASC_INV_FIELD_IN_CMD_PACKET ); + } +} + +static void cmd_read_cdvd_capacity (IDEState *s,uint8_t * buf) +{ +uint64_t total_sectors; + +bdrv_get_geometry (s->bs, &total_sectors); + + total_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 ); +ide_atapi_cmd_reply (s,8 ,8 ); +} + +static void cmd_read_dvd_structure (IDEState *s,uint8_t * buf) +{ +int max_len; +int media = buf[1 ]; +int format = buf[7 ]; +int ret; + + max_len =ube16_to_cpu (buf +8 ); + +if (format <0xff ) { +if (media_is_cd (s)) { +ide_atapi_cmd_error (s,SENSE_ILLEGAL_REQUEST , +ASC_INCOMPATIBLE_FORMAT ); +return ; + }else if (!media_present (s)) { +ide_atapi_cmd_error (s,SENSE_ILLEGAL_REQUEST , +ASC_INV_FIELD_IN_CMD_PACKET ); +return ; + } + } + +memset (buf,0 , max_len >IDE_DMA_BUF_SECTORS *512 +4 ? +IDE_DMA_BUF_SECTORS *512 +4 : max_len); + +switch (format) { + case 0x00 ... 0x7f: +case 0xff : +if (media ==0 ) { + ret =ide_dvd_read_structure (s, format, buf, buf); + +if (ret <0 ) { +ide_atapi_cmd_error (s,SENSE_ILLEGAL_REQUEST , -ret); + }else { +ide_atapi_cmd_reply (s, ret, max_len); + } + +break ; + } +/* TODO: BD support, fall through for now */ + +/* Generic disk structures */ +case 0x80 :/* TODO: AACS volume identifier */ +case 0x81 :/* TODO: AACS media serial number */ +case 0x82 :/* TODO: AACS media identifier */ +case 0x83 :/* TODO: AACS media key block */ +case 0x90 :/* TODO: List of recognized format layers */ +case 0xc0 :/* TODO: Write protection status */ +default : +ide_atapi_cmd_error (s,SENSE_ILLEGAL_REQUEST , +ASC_INV_FIELD_IN_CMD_PACKET ); +break ; + } +} + +static void cmd_set_speed (IDEState *s,uint8_t * buf) +{ +ide_atapi_cmd_ok (s); +} +void ide_atapi_cmd (IDEState *s) {const uint8_t *packet;uint8_t *buf; -int max_len; packet = s->io_buffer; buf = s->io_buffer;
@@ -665,413 +1107,52 @@ void ide_atapi_cmd(IDEState *s) }switch (s->io_buffer[0 ]) {case GPCMD_TEST_UNIT_READY : -if (bdrv_is_inserted (s->bs)) { -ide_atapi_cmd_ok (s); - }else { -ide_atapi_cmd_error (s,SENSE_NOT_READY , -ASC_MEDIUM_NOT_PRESENT ); - } +cmd_test_unit_ready (s, buf);break ;case GPCMD_MODE_SENSE_6 :case GPCMD_MODE_SENSE_10 : - { -int action, code; -if (packet[0 ] ==GPCMD_MODE_SENSE_10 ) - max_len =ube16_to_cpu (packet +7 ); -else - max_len = packet[4 ]; - action = packet[2 ] >>6 ; - code = packet[2 ] &0x3f ; -switch (action) { -case 0 :/* current values */ -switch (code) { -case GPMODE_R_W_ERROR_PAGE :/* error recovery */ -cpu_to_ube16 (&buf[0 ],16 +6 ); - buf[2 ] =0x70 ; - buf[3 ] =0 ; - buf[4 ] =0 ; - buf[5 ] =0 ; - buf[6 ] =0 ; - buf[7 ] =0 ; - - buf[8 ] =0x01 ; - buf[9 ] =0x06 ; - buf[10 ] =0x00 ; - buf[11 ] =0x05 ; - buf[12 ] =0x00 ; - buf[13 ] =0x00 ; - buf[14 ] =0x00 ; - buf[15 ] =0x00 ; -ide_atapi_cmd_reply (s,16 , max_len); -break ; -case GPMODE_AUDIO_CTL_PAGE : -cpu_to_ube16 (&buf[0 ],24 +6 ); - buf[2 ] =0x70 ; - buf[3 ] =0 ; - buf[4 ] =0 ; - buf[5 ] =0 ; - buf[6 ] =0 ; - buf[7 ] =0 ; - -/* Fill with CDROM audio volume */ - buf[17 ] =0 ; - buf[19 ] =0 ; - buf[21 ] =0 ; - buf[23 ] =0 ; - -ide_atapi_cmd_reply (s,24 , max_len); -break ; -case GPMODE_CAPABILITIES_PAGE : -cpu_to_ube16 (&buf[0 ],28 +6 ); - buf[2 ] =0x70 ; - buf[3 ] =0 ; - buf[4 ] =0 ; - buf[5 ] =0 ; - buf[6 ] =0 ; - buf[7 ] =0 ; - - buf[8 ] =0x2a ; - buf[9 ] =0x12 ; - buf[10 ] =0x00 ; - buf[11 ] =0x00 ; - -/* Claim PLAY_AUDIO capability (0x01) since some Linux -code checks for this to automount media. */ - buf[12 ] =0x71 ; - buf[13 ] =3 <<5 ; - buf[14 ] = (1 <<0 ) | (1 <<3 ) | (1 <<5 ); -if (bdrv_is_locked (s->bs)) - buf[6 ] |=1 <<1 ; - buf[15 ] =0x00 ; -cpu_to_ube16 (&buf[16 ],706 ); - buf[18 ] =0 ; - buf[19 ] =2 ; -cpu_to_ube16 (&buf[20 ],512 ); -cpu_to_ube16 (&buf[22 ],706 ); - buf[24 ] =0 ; - buf[25 ] =0 ; - buf[26 ] =0 ; - buf[27 ] =0 ; -ide_atapi_cmd_reply (s,28 , max_len); -break ; -default : - goto error_cmd; - } -break ; -case 1 :/* changeable values */ - goto error_cmd; -case 2 :/* default values */ - goto error_cmd; -default : -case 3 :/* saved values */ -ide_atapi_cmd_error (s,SENSE_ILLEGAL_REQUEST , -ASC_SAVING_PARAMETERS_NOT_SUPPORTED ); -break ; - } - } +cmd_mode_sense (s, buf);break ;case GPCMD_REQUEST_SENSE : - max_len = packet[4 ]; -memset (buf,0 ,18 ); - buf[0 ] =0x70 | (1 <<7 ); - buf[2 ] = s->sense_key; - buf[7 ] =10 ; - buf[12 ] = s->asc; -if (s->sense_key ==SENSE_UNIT_ATTENTION ) - s->sense_key =SENSE_NONE ; -ide_atapi_cmd_reply (s,18 , max_len); +cmd_request_sense (s, buf);break ;case GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL : -bdrv_set_locked (s->bs, packet[4 ] &1 ); -ide_atapi_cmd_ok (s); +cmd_prevent_allow_medium_removal (s, buf);break ;case GPCMD_READ_10 :case GPCMD_READ_12 : - { -int nb_sectors, lba; - -if (packet[0 ] ==GPCMD_READ_10 ) - nb_sectors =ube16_to_cpu (packet +7 ); -else - nb_sectors =ube32_to_cpu (packet +6 ); - lba =ube32_to_cpu (packet +2 ); -if (nb_sectors ==0 ) { -ide_atapi_cmd_ok (s); -break ; - } -ide_atapi_cmd_read (s, lba, nb_sectors,2048 ); - } +cmd_read (s, buf);break ;case GPCMD_READ_CD : - { -int nb_sectors, lba, transfer_request; - - nb_sectors = (packet[6 ] <<16 ) | (packet[7 ] <<8 ) | packet[8 ]; - lba =ube32_to_cpu (packet +2 ); -if (nb_sectors ==0 ) { -ide_atapi_cmd_ok (s); -break ; - } - transfer_request = packet[9 ]; -switch (transfer_request &0xf8 ) { -case 0x00 : -/* nothing */ -ide_atapi_cmd_ok (s); -break ; -case 0x10 : -/* normal read */ -ide_atapi_cmd_read (s, lba, nb_sectors,2048 ); -break ; -case 0xf8 : -/* read all data */ -ide_atapi_cmd_read (s, lba, nb_sectors,2352 ); -break ; -default : -ide_atapi_cmd_error (s,SENSE_ILLEGAL_REQUEST , -ASC_INV_FIELD_IN_CMD_PACKET ); -break ; - } - } +cmd_read_cd (s, buf);break ;case GPCMD_SEEK : - { -unsigned int lba; -uint64_t total_sectors; - -bdrv_get_geometry (s->bs, &total_sectors); - total_sectors >>=2 ; -if (total_sectors ==0 ) { -ide_atapi_cmd_error (s,SENSE_NOT_READY , -ASC_MEDIUM_NOT_PRESENT ); -break ; - } - lba =ube32_to_cpu (packet +2 ); -if (lba >= total_sectors) { -ide_atapi_cmd_error (s,SENSE_ILLEGAL_REQUEST , -ASC_LOGICAL_BLOCK_OOR ); -break ; - } -ide_atapi_cmd_ok (s); - } +cmd_seek (s, buf);break ;case GPCMD_START_STOP_UNIT : - { -int start, eject, sense, err =0 ; - start = packet[4 ] &1 ; - eject = (packet[4 ] >>1 ) &1 ; - -if (eject) { - err =bdrv_eject (s->bs, !start); - } - -switch (err) { -case 0 : -ide_atapi_cmd_ok (s); -break ; -case -EBUSY : - sense =SENSE_NOT_READY ; -if (bdrv_is_inserted (s->bs)) { - sense =SENSE_ILLEGAL_REQUEST ; - } -ide_atapi_cmd_error (s, sense, -ASC_MEDIA_REMOVAL_PREVENTED ); -break ; -default : -ide_atapi_cmd_error (s,SENSE_NOT_READY , -ASC_MEDIUM_NOT_PRESENT ); -break ; - } - } +cmd_start_stop_unit (s, buf);break ;case GPCMD_MECHANISM_STATUS : - { - max_len =ube16_to_cpu (packet +8 ); -cpu_to_ube16 (buf,0 ); -/* no current LBA */ - buf[2 ] =0 ; - buf[3 ] =0 ; - buf[4 ] =0 ; - buf[5 ] =1 ; -cpu_to_ube16 (buf +6 ,0 ); -ide_atapi_cmd_reply (s,8 , max_len); - } +cmd_mechanism_status (s, buf);break ;case GPCMD_READ_TOC_PMA_ATIP : - { -int format, msf, start_track, len; -uint64_t total_sectors; - -bdrv_get_geometry (s->bs, &total_sectors); - total_sectors >>=2 ; -if (total_sectors ==0 ) { -ide_atapi_cmd_error (s,SENSE_NOT_READY , -ASC_MEDIUM_NOT_PRESENT ); -break ; - } - max_len =ube16_to_cpu (packet +7 ); - format = packet[9 ] >>6 ; - msf = (packet[1 ] >>1 ) &1 ; - start_track = packet[6 ]; -switch (format) { -case 0 : - len =cdrom_read_toc (total_sectors, buf, msf, start_track); -if (len <0 ) - goto error_cmd; -ide_atapi_cmd_reply (s, len, max_len); -break ; -case 1 : -/* multi session : only a single session defined */ -memset (buf,0 ,12 ); - buf[1 ] =0x0a ; - buf[2 ] =0x01 ; - buf[3 ] =0x01 ; -ide_atapi_cmd_reply (s,12 , max_len); -break ; -case 2 : - len =cdrom_read_toc_raw (total_sectors, buf, msf, start_track); -if (len <0 ) - goto error_cmd; -ide_atapi_cmd_reply (s, len, max_len); -break ; -default : - error_cmd: -ide_atapi_cmd_error (s,SENSE_ILLEGAL_REQUEST , -ASC_INV_FIELD_IN_CMD_PACKET ); -break ; - } - } +cmd_read_toc_pma_atip (s, buf);break ;case GPCMD_READ_CDVD_CAPACITY : - { -uint64_t total_sectors; - -bdrv_get_geometry (s->bs, &total_sectors); - total_sectors >>=2 ; -if (total_sectors ==0 ) { -ide_atapi_cmd_error (s,SENSE_NOT_READY , -ASC_MEDIUM_NOT_PRESENT ); -break ; - } -/* NOTE: it is really the number of sectors minus 1 */ -cpu_to_ube32 (buf, total_sectors -1 ); -cpu_to_ube32 (buf +4 ,2048 ); -ide_atapi_cmd_reply (s,8 ,8 ); - } +cmd_read_cdvd_capacity (s, buf);break ;case GPCMD_READ_DVD_STRUCTURE : - { -int media = packet[1 ]; -int format = packet[7 ]; -int ret; - - max_len =ube16_to_cpu (packet +8 ); - -if (format <0xff ) { -if (media_is_cd (s)) { -ide_atapi_cmd_error (s,SENSE_ILLEGAL_REQUEST , -ASC_INCOMPATIBLE_FORMAT ); -break ; - }else if (!media_present (s)) { -ide_atapi_cmd_error (s,SENSE_ILLEGAL_REQUEST , -ASC_INV_FIELD_IN_CMD_PACKET ); -break ; - } - } - -memset (buf,0 , max_len >IDE_DMA_BUF_SECTORS *512 +4 ? -IDE_DMA_BUF_SECTORS *512 +4 : max_len); - -switch (format) { - case 0x00 ... 0x7f: -case 0xff : -if (media ==0 ) { - ret =ide_dvd_read_structure (s, format, packet, buf); - -if (ret <0 ) -ide_atapi_cmd_error (s,SENSE_ILLEGAL_REQUEST , -ret); -else -ide_atapi_cmd_reply (s, ret, max_len); - -break ; - } -/* TODO: BD support, fall through for now */ - -/* Generic disk structures */ -case 0x80 :/* TODO: AACS volume identifier */ -case 0x81 :/* TODO: AACS media serial number */ -case 0x82 :/* TODO: AACS media identifier */ -case 0x83 :/* TODO: AACS media key block */ -case 0x90 :/* TODO: List of recognized format layers */ -case 0xc0 :/* TODO: Write protection status */ -default : -ide_atapi_cmd_error (s,SENSE_ILLEGAL_REQUEST , -ASC_INV_FIELD_IN_CMD_PACKET ); -break ; - } - } +cmd_read_dvd_structure (s, buf);break ;case GPCMD_SET_SPEED : -ide_atapi_cmd_ok (s); +cmd_set_speed (s, buf);break ;case GPCMD_INQUIRY : - max_len = packet[4 ]; - buf[0 ] =0x05 ;/* CD-ROM */ - buf[1 ] =0x80 ;/* removable */ - buf[2 ] =0x00 ;/* ISO */ - buf[3 ] =0x21 ;/* ATAPI-2 (XXX: put ATAPI-4 ?) */ - buf[4 ] =31 ;/* additional length */ - buf[5 ] =0 ;/* reserved */ - buf[6 ] =0 ;/* reserved */ - buf[7 ] =0 ;/* reserved */ -padstr8 (buf +8 ,8 ,"QEMU" ); -padstr8 (buf +16 ,16 ,"QEMU DVD-ROM" ); -padstr8 (buf +32 ,4 , s->version); -ide_atapi_cmd_reply (s,36 , max_len); +cmd_inquiry (s, buf);break ;case GPCMD_GET_CONFIGURATION : - { -uint32_t len; -uint8_t index =0 ; - -/* only feature 0 is supported */ -if (packet[2 ] !=0 || packet[3 ] !=0 ) { -ide_atapi_cmd_error (s,SENSE_ILLEGAL_REQUEST , -ASC_INV_FIELD_IN_CMD_PACKET ); -break ; - } - -/* XXX: could result in alignment problems in some architectures */ - max_len =ube16_to_cpu (packet +7 ); - -/* -* XXX: avoid overflow for io_buffer if max_len is bigger than -* the size of that buffer (dimensioned to max number of -* sectors to transfer at once) -* -* Only a problem if the feature/profiles grow. -*/ -if (max_len >512 )/* XXX: assume 1 sector */ - max_len =512 ; - -memset (buf,0 , max_len); -/* -* the number of sectors from the media tells us which profile -* to use as current. 0 means there is no media -*/ -if (media_is_dvd (s)) -cpu_to_ube16 (buf +6 ,MMC_PROFILE_DVD_ROM ); -else if (media_is_cd (s)) -cpu_to_ube16 (buf +6 ,MMC_PROFILE_CD_ROM ); - - buf[10 ] =0x02 |0x01 ;/* persistent and current */ - len =12 ;/* headers: 8 + 4 */ - len +=ide_atapi_set_profile (buf, &index,MMC_PROFILE_DVD_ROM ); - len +=ide_atapi_set_profile (buf, &index,MMC_PROFILE_CD_ROM ); -cpu_to_ube32 (buf, len -4 );/* data length */ - -ide_atapi_cmd_reply (s, len, max_len); -break ; - } +cmd_get_configuration (s, buf); +break ;case GPCMD_GET_EVENT_STATUS_NOTIFICATION :handle_get_event_status_notification (s, buf, packet);break ;
Signature · 1 line
1.7.2.3
PATCH v2 3/5 ide/atapi: Use table instead of switch for commands
In reply to Kevin Wolf--- hw/ide/atapi.c | 115 +++++++++++++++++++++++-------------------------------- 1 files changed, 48 insertions(+), 67 deletions(-)
hw/ide/atapi.c +48 -67
@@ -533,10 +533,11 @@ static unsigned int event_status_media(IDEState *s,return 8 ;/* We wrote to 4 extra bytes from the header */ } -static void handle_get_event_status_notification (IDEState *s, -uint8_t *buf, -const uint8_t *packet) +static void cmd_get_event_status_notification (IDEState *s, +uint8_t *buf) { +const uint8_t *packet = buf; +struct {uint8_t opcode;uint8_t polled;/* lsb bit is polled; others are reserved */
@@ -1064,6 +1065,38 @@ static void cmd_set_speed(IDEState *s, uint8_t* buf)ide_atapi_cmd_ok (s); } +enum { +/* +* Only commands flagged as ALLOW_UA are allowed to run under a +* unit attention condition. (See MMC-5, section 4.1.6.1) +*/ +ALLOW_UA =0x01 , +}; + +struct { +void (*handler)(IDEState *s,uint8_t *buf); +int flags; +} atapi_cmd_table[0x100 ] = { + [0x00 ] = { cmd_test_unit_ready,0 }, + [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 }, + [0x28 ] = { cmd_read,/* (10) */ 0 }, + [0x2b ] = { cmd_seek,0 }, + [0x43 ] = { cmd_read_toc_pma_atip,0 }, + [0x46 ] = { cmd_get_configuration,ALLOW_UA }, + [0x4a ] = { cmd_get_event_status_notification,ALLOW_UA }, + [0x5a ] = { cmd_mode_sense,/* (10) */ 0 }, + [0xa8 ] = { cmd_read,/* (12) */ 0 }, + [0xad ] = { cmd_read_dvd_structure,0 }, + [0xbb ] = { cmd_set_speed,0 }, + [0xbd ] = { cmd_mechanism_status,0 }, + [0xbe ] = { cmd_read_cd,0 }, +}; +void ide_atapi_cmd (IDEState *s) {const uint8_t *packet;
@@ -1082,21 +1115,17 @@ void ide_atapi_cmd(IDEState *s) }#endif /* -* If there's a UNIT_ATTENTION condition pending, only -* REQUEST_SENSE, INQUIRY, GET_CONFIGURATION and -* GET_EVENT_STATUS_NOTIFICATION commands are allowed to complete. -* MMC-5, section 4.1.6.1 lists only these commands being allowed -* to complete, with other commands getting a CHECK condition -* response unless a higher priority status, defined by the drive +* If there's a UNIT_ATTENTION condition pending, only command flagged with +* ALLOW_UA are allowed to complete. with other commands getting a CHECK +* condition response unless a higher priority status, defined by the drive * here, is pending. */ if (s->sense_key ==SENSE_UNIT_ATTENTION && - s->io_buffer[0 ] !=GPCMD_REQUEST_SENSE && - s->io_buffer[0 ] !=GPCMD_INQUIRY && - s->io_buffer[0 ] !=GPCMD_GET_EVENT_STATUS_NOTIFICATION ) { + !(atapi_cmd_table[s->io_buffer[0 ]].flags &ALLOW_UA )) {ide_atapi_cmd_check_status (s);return ; } +if (bdrv_is_inserted (s->bs) && s->cdrom_changed) {ide_atapi_cmd_error (s,SENSE_NOT_READY ,ASC_MEDIUM_NOT_PRESENT );
@@ -1105,60 +1134,12 @@ void ide_atapi_cmd(IDEState *s) s->asc =ASC_MEDIUM_MAY_HAVE_CHANGED ;return ; } -switch (s->io_buffer[0 ]) { -case GPCMD_TEST_UNIT_READY : -cmd_test_unit_ready (s, buf); -break ; -case GPCMD_MODE_SENSE_6 : -case GPCMD_MODE_SENSE_10 : -cmd_mode_sense (s, buf); -break ; -case GPCMD_REQUEST_SENSE : -cmd_request_sense (s, buf); -break ; -case GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL : -cmd_prevent_allow_medium_removal (s, buf); -break ; -case GPCMD_READ_10 : -case GPCMD_READ_12 : -cmd_read (s, buf); -break ; -case GPCMD_READ_CD : -cmd_read_cd (s, buf); -break ; -case GPCMD_SEEK : -cmd_seek (s, buf); -break ; -case GPCMD_START_STOP_UNIT : -cmd_start_stop_unit (s, buf); -break ; -case GPCMD_MECHANISM_STATUS : -cmd_mechanism_status (s, buf); -break ; -case GPCMD_READ_TOC_PMA_ATIP : -cmd_read_toc_pma_atip (s, buf); -break ; -case GPCMD_READ_CDVD_CAPACITY : -cmd_read_cdvd_capacity (s, buf); -break ; -case GPCMD_READ_DVD_STRUCTURE : -cmd_read_dvd_structure (s, buf); -break ; -case GPCMD_SET_SPEED : -cmd_set_speed (s, buf); -break ; -case GPCMD_INQUIRY : -cmd_inquiry (s, buf); -break ; -case GPCMD_GET_CONFIGURATION : -cmd_get_configuration (s, buf); -break ; -case GPCMD_GET_EVENT_STATUS_NOTIFICATION : -handle_get_event_status_notification (s, buf, packet); -break ; -default : -ide_atapi_cmd_error (s,SENSE_ILLEGAL_REQUEST , -ASC_ILLEGAL_OPCODE ); -break ; + +/* Execute the command */ +if (atapi_cmd_table[s->io_buffer[0 ]].handler) { + atapi_cmd_table[s->io_buffer[0 ]].handler (s, buf); +return ; } + +ide_atapi_cmd_error (s,SENSE_ILLEGAL_REQUEST ,ASC_ILLEGAL_OPCODE ); }
Signature · 1 line
1.7.2.3
Re: [Qemu-devel] [PATCH v2 3/5] ide/atapi: Use table instead of switch for commands
Quoted 31 lines · Kevin Wolf
Signed-off-by Kevin Wolf <[email protected]>---
hw/ide/atapi.c | 115 +++++++++++++++++++++++--------------------------------1 files changed, 48 insertions(+), 67 deletions(-)hw/ide/atapi.c +0
-0@@ -533,10 +533,11 @@ static unsigned int event_status_media(IDEState *s,return 8; /* We wrote to 4 extra bytes from the header */ }
hw/ide/atapi.c +2
-3-static void handle_get_event_status_notification (IDEState *s, - uint8_t *buf, - const uint8_t *packet) +static void cmd_get_event_status_notification (IDEState *s, + uint8_t *buf){
+ constuint8_t*packet = buf;
+
struct {
uint8_topcode;
uint8_tpolled; /* lsb bit is polled; others are reserved */hw/ide/atapi.c +3
-0@@ -1064,6 +1065,38 @@ static void cmd_set_speed(IDEState *s, uint8_t* buf) ide_atapi_cmd_ok(s); } +enum { + /* +* Only commands flagged as ALLOW_UA are allowed to run under a
hw/ide/atapi.c static void cmd_set_speed(IDEState *s, uint8_t* buf) · line 1080
Quoted 10 lines
hw/ide/atapi.c +10
-0+* unit attention condition. (See MMC-5, section 4.1.6.1) +*/ + ALLOW_UA = 0x01, +}; + +struct { + void (*handler)(IDEState *s, uint8_t *buf); + int flags; +} atapi_cmd_table[0x100 ] = { + [ 0x00 ] = { cmd_test_unit_ready, 0 },
How about using symbols here, like
[ GPCMD_TEST_UNIT_READY ] = { cmd_test_unit_ready, 0 },?
The table can probably be static const.
Quoted 122 lines
hw/ide/atapi.c +22
-6+ [ 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 }, + [ 0x28 ] = { cmd_read, /* (10) */ 0 }, + [ 0x2b ] = { cmd_seek, 0 }, + [ 0x43 ] = { cmd_read_toc_pma_atip, 0 }, + [ 0x46 ] = { cmd_get_configuration, ALLOW_UA }, + [ 0x4a ] = { cmd_get_event_status_notification, ALLOW_UA }, + [ 0x5a ] = { cmd_mode_sense, /* (10) */ 0 }, + [ 0xa8 ] = { cmd_read, /* (12) */ 0 }, + [ 0xad ] = { cmd_read_dvd_structure, 0 }, + [ 0xbb ] = { cmd_set_speed, 0 }, + [ 0xbd ] = { cmd_mechanism_status, 0 }, + [ 0xbe ] = { cmd_read_cd, 0 }, +}; + void ide_atapi_cmd(IDEState *s) { const uint8_t *packet;@@ -1082,21 +1115,17 @@ void ide_atapi_cmd(IDEState *s) } #endif /* -* If there's a UNIT_ATTENTION condition pending, only -* REQUEST_SENSE, INQUIRY, GET_CONFIGURATION and -* GET_EVENT_STATUS_NOTIFICATION commands are allowed to complete. -* MMC-5, section 4.1.6.1 lists only these commands being allowed -* to complete, with other commands getting a CHECK condition -* response unless a higher priority status, defined by the drive +* If there's a UNIT_ATTENTION condition pending, only command flagged with +* ALLOW_UA are allowed to complete. with other commands getting a CHECK +* condition response unless a higher priority status, defined by the drive * here, is pending. */ if (
s->sense_key==SENSE_UNIT_ATTENTION&&
- s->io_buffer[0] !=GPCMD_REQUEST_SENSE&&
- s->io_buffer[0] !=GPCMD_INQUIRY&&
- s->io_buffer[0] !=GPCMD_GET_EVENT_STATUS_NOTIFICATION) {
+ !(atapi_cmd_table[s->io_buffer[0]].flags &ALLOW_UA)) {ide_atapi_cmd_check_status(s);
return;
}
+
if (bdrv_is_inserted(s->bs)&&s->cdrom_changed) {ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);hw/ide/atapi.c +7
-56@@ -1105,60 +1134,12 @@ void ide_atapi_cmd(IDEState *s) s->asc = ASC_MEDIUM_MAY_HAVE_CHANGED; return; } - switch(s->io_buffer[0]) { - case GPCMD_TEST_UNIT_READY: - cmd_test_unit_ready(s, buf); - break; - case GPCMD_MODE_SENSE_6: - case GPCMD_MODE_SENSE_10: - cmd_mode_sense(s, buf); - break; - case GPCMD_REQUEST_SENSE: - cmd_request_sense(s, buf); - break; - case GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL: - cmd_prevent_allow_medium_removal(s, buf); - break; - case GPCMD_READ_10: - case GPCMD_READ_12: - cmd_read(s, buf); - break; - case GPCMD_READ_CD: - cmd_read_cd(s, buf); - break; - case GPCMD_SEEK: - cmd_seek(s, buf); - break; - case GPCMD_START_STOP_UNIT: - cmd_start_stop_unit(s, buf); - break; - case GPCMD_MECHANISM_STATUS: - cmd_mechanism_status(s, buf); - break; - case GPCMD_READ_TOC_PMA_ATIP: - cmd_read_toc_pma_atip(s, buf); - break; - case GPCMD_READ_CDVD_CAPACITY: - cmd_read_cdvd_capacity(s, buf); - break; - case GPCMD_READ_DVD_STRUCTURE: - cmd_read_dvd_structure(s, buf); - break; - case GPCMD_SET_SPEED: - cmd_set_speed(s, buf); - break; - case GPCMD_INQUIRY: - cmd_inquiry(s, buf); - break; - case GPCMD_GET_CONFIGURATION: - cmd_get_configuration(s, buf); - break; - case GPCMD_GET_EVENT_STATUS_NOTIFICATION: - handle_get_event_status_notification(s, buf, packet); - break; - default: - ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, - ASC_ILLEGAL_OPCODE); - break; + + /* Execute the command */ + if (atapi_cmd_table[s->io_buffer[0]].handler) { + atapi_cmd_table[s->io_buffer[0]].handler(s, buf); + return; } + + ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, ASC_ILLEGAL_OPCODE); } --1.7.2.3
Re: [Qemu-devel] [PATCH v2 3/5] ide/atapi: Use table instead of switch for commands
Am 20.04.2011 20:13, schrieb Blue Swirl:
Quoted 44 lines · Kevin Wolf
Quoted 31 lines
Signed-off-by Kevin Wolf <[email protected]>--- hw/ide/atapi.c | 115 +++++++++++++++++++++++-------------------------------- 1 files changed, 48 insertions(+), 67 deletions(-)hw/ide/atapi.c +7
-3@@ -533,10 +533,11 @@ static unsigned int event_status_media(IDEState *s,return 8 ;/* We wrote to 4 extra bytes from the header */ } -static void handle_get_event_status_notification (IDEState *s, -uint8_t *buf, -const uint8_t *packet) +static void cmd_get_event_status_notification (IDEState *s, +uint8_t *buf) { +const uint8_t *packet = buf; +struct {uint8_t opcode;uint8_t polled;/* lsb bit is polled; others are reserved */ @@ -1064,6 +1065,38 @@ static void cmd_set_speed(IDEState *s, uint8_t* buf)ide_atapi_cmd_ok (s); } +enum { +/* +* Only commands flagged as ALLOW_UA are allowed to run under a hw/ide/atapi.c static void cmd_set_speed(IDEState *s, uint8_t* buf) · line 1080
Quoted 10 lines
hw/ide/atapi.c +10
-0+* unit attention condition. (See MMC-5, section 4.1.6.1) +*/ + ALLOW_UA = 0x01, +}; + +struct { +void (*handler)(IDEState *s,uint8_t *buf); +int flags; +} atapi_cmd_table[0x100 ] = { + [0x00 ] = { cmd_test_unit_ready,0 },How about using symbols here, like
Quoted 2 lines
[ GPCMD_TEST_UNIT_READY ] = { cmd_test_unit_ready, 0 },?
I avoided symbols intentionally. In fact, I'm even considering to remove the symbols from the header file because most of them are unused now (and it should be easy enough to get rid of the rest of them so that the only occurrence of the literal opcode is in the table).
The point here is that this table contains all the information about a command in one place. If you used symbols, you would have the command name twice, and the opcode would be missing. So not using symbolic constants but rather literal opcodes improves readability in this case.
Quoted 1 line
The table can probably be static const.
Right, will fix.
Kevin
Re: [Qemu-devel] [PATCH v2 3/5] ide/atapi: Use table instead of switch for commands
In reply to Kevin WolfQuoted 11 lines · Kevin Wolf
Signed-off-by Kevin Wolf <[email protected]>--- hw/ide/atapi.c | 115 +++++++++++++++++++++++-------------------------------- 1 files changed, 48 insertions(+), 67 deletions(-)hw/ide/atapi.c +0
-0@@ -533,10 +533,11 @@ static unsigned int event_status_media(IDEState *s,return 8 ;/* We wrote to 4 extra bytes from the header */
hw/ide/atapi.c static unsigned int event_status_media(IDEState *s, · line 540
Quoted 10 lines
hw/ide/atapi.c +4
-3} -static void handle_get_event_status_notification (IDEState *s, -uint8_t *buf, -const uint8_t *packet) +static void cmd_get_event_status_notification (IDEState *s, +uint8_t *buf) { +const uint8_t *packet = buf; +
Exploiting packet == buf. But do we want to go away from that?
Amit
PATCH v2 4/5 ide/atapi: Replace bdrv_get_geometry calls by s->nb_sectors
In reply to Kevin WolfThe disk size can only change when the medium is changed, and the change callback takes care of updating s->nb_sectors in this case.
--- hw/ide/atapi.c | 21 ++++++--------------- 1 files changed, 6 insertions(+), 15 deletions(-)
hw/ide/atapi.c +6 -15
@@ -416,10 +416,10 @@ static int ide_dvd_read_structure(IDEState *s, int format,if (layer !=0 )return -ASC_INV_FIELD_IN_CMD_PACKET ; -bdrv_get_geometry (s->bs, &total_sectors); - total_sectors >>=2 ; -if (total_sectors ==0 ) + total_sectors = s->nb_sectors >>2 ; +if (total_sectors ==0 ) {return -ASC_MEDIUM_NOT_PRESENT ; + } buf[4 ] =1 ;/* DVD-ROM, part version 1 */ buf[5 ] =0xf ;/* 120mm disc, minimum rate unspecified */
@@ -881,11 +881,8 @@ static void cmd_read_cd(IDEState *s, uint8_t* buf)static void cmd_seek (IDEState *s,uint8_t * buf) {unsigned int lba; -uint64_t total_sectors; - -bdrv_get_geometry (s->bs, &total_sectors); +uint64_t total_sectors = s->nb_sectors >>2 ; - total_sectors >>=2 ;if (total_sectors ==0 ) {ide_atapi_cmd_error (s,SENSE_NOT_READY ,ASC_MEDIUM_NOT_PRESENT );return ;
@@ -944,12 +941,9 @@ 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; +uint64_t total_sectors = s->nb_sectors >>2 ;int max_len; -bdrv_get_geometry (s->bs, &total_sectors); - - total_sectors >>=2 ;if (total_sectors ==0 ) {ide_atapi_cmd_error (s,SENSE_NOT_READY ,ASC_MEDIUM_NOT_PRESENT );return ;
@@ -990,11 +984,8 @@ static void cmd_read_toc_pma_atip(IDEState *s, uint8_t* buf)static void cmd_read_cdvd_capacity (IDEState *s,uint8_t * buf) { -uint64_t total_sectors; - -bdrv_get_geometry (s->bs, &total_sectors); +uint64_t total_sectors = s->nb_sectors >>2 ; - total_sectors >>=2 ;if (total_sectors ==0 ) {ide_atapi_cmd_error (s,SENSE_NOT_READY ,ASC_MEDIUM_NOT_PRESENT );return ;
Signature · 1 line
1.7.2.3
PATCH v2 5/5 ide/atapi: Introduce CHECK_READY flag for commands
In reply to Kevin WolfSome commands are supposed to report a Not Ready Conmdition (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.
--- hw/ide/atapi.c | 48 +++++++++++++++++++++++------------------------- 1 files changed, 23 insertions(+), 25 deletions(-)
hw/ide/atapi.c +23 -25
@@ -813,11 +813,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)
@@ -883,11 +881,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 );
@@ -941,13 +934,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 ;
@@ -986,11 +974,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 );
@@ -1062,22 +1045,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, };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 },
@@ -1126,6 +1116,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);
Signature · 1 line
1.7.2.3
Re: [Qemu-devel] [PATCH v2 5/5] ide/atapi: Introduce CHECK_READY flag for commands
Quoted 1 line · Kevin Wolf
Some commands are supposed to report a Not Ready Conmdition (i.e. they require
typo in Condition
Amit