Skip to contentSkip to conversation

Check for supported SCSI commands

PATCH Applied Hannes Reinecke 4 participants 20 messages Jul 22, 2011 – Jul 27, 2011 KVM

lore
Share & tools
lore
#0 lore raw

v2 cover Check for supported SCSI commands

Markus Armbruster pointed out that not every SCSI command is supported for a given device type. Based on his patch and suggestiongs this series cleans up the SCSI device type and adds a check for supported commands.

Hannes Reinecke (6):
scsi-disk: Codingstyle fixes
scsi: Remove references to SET_WINDOW
scsi: Remove REZERO_UNIT emulation
scsi: Sanitize command definitions
scsi-disk: Remove 'drive_kind'
scsi-disk: Check for supported commands

 hw/scsi-bus.c     |   74 ++++++++++++---------
 hw/scsi-defs.h    |   62 +++++++++++-------
 hw/scsi-disk.c    |  185 ++++++++++++++++++++++++++++++++++++++++-------------
 hw/scsi-generic.c |    2 +-
 4 files changed, 221 insertions(+), 102 deletions(-)
Signature · 1 line
1.7.3.4
#1 lore raw

PATCH v2 1/6 scsi-disk: Codingstyle fixes

Replace tabs with spaces.

---
 hw/scsi-disk.c |   16 ++++++++--------
 1 files changed, 8 insertions(+), 8 deletions(-)
hw/scsi-disk.c +8 -8
index 05d14ab..910d3b5 100644
@@ -526,7 +526,7 @@ static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
     memset(outbuf, 0, buflen);
 
     if (req->lun) {
-        outbuf[0] = 0x7f;	/* LUN not supported */
+        outbuf[0] = 0x7f;       /* LUN not supported */
         return buflen;
     }
 
@@ -836,7 +836,7 @@ static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
     case TEST_UNIT_READY:
         if (!bdrv_is_inserted(s->bs))
             goto not_ready;
-	break;
+        break;
     case REQUEST_SENSE:
         if (req->cmd.xfer < 4)
             goto illegal_request;
@@ -848,7 +848,7 @@ static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
         buflen = scsi_disk_emulate_inquiry(req, outbuf);
         if (buflen < 0)
             goto illegal_request;
-	break;
+        break;
     case MODE_SENSE:
     case MODE_SENSE_10:
         buflen = scsi_disk_emulate_mode_sense(req, outbuf);
@@ -881,14 +881,14 @@ static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
             /* load/eject medium */
             bdrv_eject(s->bs, !(req->cmd.buf[4] & 1));
         }
-	break;
+        break;
     case ALLOW_MEDIUM_REMOVAL:
         bdrv_set_locked(s->bs, req->cmd.buf[4] & 1);
-	break;
+        break;
     case READ_CAPACITY:
         /* The normal LEN field for this command is zero.  */
-	memset(outbuf, 0, 8);
-	bdrv_get_geometry(s->bs, &nb_sectors);
+        memset(outbuf, 0, 8);
+        bdrv_get_geometry(s->bs, &nb_sectors);
         if (!nb_sectors)
             goto not_ready;
         nb_sectors /= s->cluster_size;
@@ -908,7 +908,7 @@ static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
         outbuf[6] = s->cluster_size * 2;
         outbuf[7] = 0;
         buflen = 8;
-	break;
+        break;
     case SYNCHRONIZE_CACHE:
         ret = bdrv_flush(s->bs);
         if (ret < 0) {
Signature · 1 line
1.7.3.4
#2 lore raw

PATCH v2 2/6 scsi: Remove references to SET_WINDOW

In reply to Hannes Reinecke

SET_WINDOW command is vendor-specific only.
So we shouldn't try to emulate it.

---
 hw/scsi-bus.c  |    2 --
 hw/scsi-defs.h |    1 -
 2 files changed, 0 insertions(+), 3 deletions(-)
hw/scsi-bus.c +0 -2
index 8b1a412..facc98d 100644
@@ -350,7 +350,6 @@ static void scsi_req_xfer_mode(SCSIRequest *req)
     case SEARCH_HIGH_12:
     case SEARCH_EQUAL_12:
     case SEARCH_LOW_12:
-    case SET_WINDOW:
     case MEDIUM_SCAN:
     case SEND_VOLUME_TAG:
     case WRITE_LONG_2:
@@ -544,7 +543,6 @@ static const char *scsi_command_name(uint8_t cmd)
         [ SEND_DIAGNOSTIC          ] = "SEND_DIAGNOSTIC",
         [ ALLOW_MEDIUM_REMOVAL     ] = "ALLOW_MEDIUM_REMOVAL",
 
-        [ SET_WINDOW               ] = "SET_WINDOW",
         [ READ_CAPACITY            ] = "READ_CAPACITY",
         [ READ_10                  ] = "READ_10",
         [ WRITE_10                 ] = "WRITE_10",
hw/scsi-defs.h +0 -1
index 413cce0..8513983 100644
@@ -49,7 +49,6 @@
 #define SEND_DIAGNOSTIC       0x1d
 #define ALLOW_MEDIUM_REMOVAL  0x1e
 
-#define SET_WINDOW            0x24
 #define READ_CAPACITY         0x25
 #define READ_10               0x28
 #define WRITE_10              0x2a
Signature · 1 line
1.7.3.4
#3 lore raw

PATCH v2 3/6 scsi: Remove REZERO_UNIT emulation

In reply to Hannes Reinecke

REZERO_UNIT command is obsolete. Remove support for it.

---
 hw/scsi-bus.c  |    3 ---
 hw/scsi-defs.h |    1 -
 hw/scsi-disk.c |    7 -------
 3 files changed, 0 insertions(+), 11 deletions(-)
hw/scsi-bus.c +0 -3
index facc98d..52a6784 100644
@@ -223,7 +223,6 @@ static int scsi_req_length(SCSIRequest *req, uint8_t *cmd)
 
     switch(cmd[0]) {
     case TEST_UNIT_READY:
-    case REZERO_UNIT:
     case START_STOP:
     case SEEK_6:
     case WRITE_FILEMARKS:
@@ -516,8 +515,6 @@ static const char *scsi_command_name(uint8_t cmd)
 {
     static const char *names[] = {
         [ TEST_UNIT_READY          ] = "TEST_UNIT_READY",
-        [ REZERO_UNIT              ] = "REZERO_UNIT",
-        /* REWIND and REZERO_UNIT use the same operation code */
         [ REQUEST_SENSE            ] = "REQUEST_SENSE",
         [ FORMAT_UNIT              ] = "FORMAT_UNIT",
         [ READ_BLOCK_LIMITS        ] = "READ_BLOCK_LIMITS",
hw/scsi-defs.h +0 -1
index 8513983..1f40c5c 100644
@@ -25,7 +25,6 @@
  */
 
 #define TEST_UNIT_READY       0x00
-#define REZERO_UNIT           0x01
 #define REQUEST_SENSE         0x03
 #define FORMAT_UNIT           0x04
 #define READ_BLOCK_LIMITS     0x05
hw/scsi-disk.c +0 -7
index 910d3b5..323368a 100644
@@ -972,12 +972,6 @@ static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
         break;
     case VERIFY:
         break;
-    case REZERO_UNIT:
-        DPRINTF("Rezero Unit\n");
-        if (!bdrv_is_inserted(s->bs)) {
-            goto not_ready;
-        }
-        break;
     default:
         scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(INVALID_OPCODE));
         return -1;
@@ -1059,7 +1053,6 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
     case SERVICE_ACTION_IN:
     case REPORT_LUNS:
     case VERIFY:
-    case REZERO_UNIT:
         rc = scsi_disk_emulate_command(r, outbuf);
         if (rc < 0) {
             return 0;
Signature · 1 line
1.7.3.4
#4 lore raw

PATCH v2 4/6 scsi: Sanitize command definitions

In reply to Hannes Reinecke

Sanitize SCSI command definitions.
Add _10 suffix to READ_CAPACITY, WRITE_VERIFY, VERIFY, READ_LONG, WRITE_LONG, and WRITE_SAME.
Add new command definitions for LOCATE_10, UNMAP, VARLENGTH_CDB, WRITE_FILEMARKS_16, EXTENDED_COPY, ATA_PASSTHROUGH, ACCESS_CONTROL_IN, ACCESS_CONTROL_OUT, COMPARE_AND_WRITE, VERIFY_16, SYNCHRONIZE_CACHE_16, LOCATE_16, ERASE_16, WRITE_LONG_16, LOAD_UNLOAD, VERIFY_12. Remove invalid definition of WRITE_LONG_2.

---
 hw/scsi-bus.c     |   69 ++++++++++++++++++++++++++++++++--------------------
 hw/scsi-defs.h    |   54 +++++++++++++++++++++++++----------------
 hw/scsi-disk.c    |   10 ++++----
 hw/scsi-generic.c |    2 +-
 4 files changed, 81 insertions(+), 54 deletions(-)
hw/scsi-bus.c +42 -27
index 52a6784..0b0344c 100644
@@ -223,6 +223,7 @@ static int scsi_req_length(SCSIRequest *req, uint8_t *cmd)
 
     switch(cmd[0]) {
     case TEST_UNIT_READY:
+    case REWIND:
     case START_STOP:
     case SEEK_6:
     case WRITE_FILEMARKS:
@@ -231,24 +232,24 @@ static int scsi_req_length(SCSIRequest *req, uint8_t *cmd)
     case RELEASE:
     case ERASE:
     case ALLOW_MEDIUM_REMOVAL:
-    case VERIFY:
+    case VERIFY_10:
     case SEEK_10:
     case SYNCHRONIZE_CACHE:
     case LOCK_UNLOCK_CACHE:
     case LOAD_UNLOAD:
     case SET_CD_SPEED:
     case SET_LIMITS:
-    case WRITE_LONG:
+    case WRITE_LONG_10:
     case MOVE_MEDIUM:
     case UPDATE_BLOCK:
         req->cmd.xfer = 0;
         break;
     case MODE_SENSE:
         break;
-    case WRITE_SAME:
+    case WRITE_SAME_10:
         req->cmd.xfer = 1;
         break;
-    case READ_CAPACITY:
+    case READ_CAPACITY_10:
         req->cmd.xfer = 8;
         break;
     case READ_BLOCK_LIMITS:
@@ -264,7 +265,7 @@ static int scsi_req_length(SCSIRequest *req, uint8_t *cmd)
         req->cmd.xfer *= 8;
         break;
     case WRITE_10:
-    case WRITE_VERIFY:
+    case WRITE_VERIFY_10:
     case WRITE_6:
     case WRITE_12:
     case WRITE_VERIFY_12:
@@ -324,7 +325,7 @@ static void scsi_req_xfer_mode(SCSIRequest *req)
     switch (req->cmd.buf[0]) {
     case WRITE_6:
     case WRITE_10:
-    case WRITE_VERIFY:
+    case WRITE_VERIFY_10:
     case WRITE_12:
     case WRITE_VERIFY_12:
     case WRITE_16:
@@ -344,14 +345,13 @@ static void scsi_req_xfer_mode(SCSIRequest *req)
     case SEARCH_HIGH:
     case SEARCH_LOW:
     case UPDATE_BLOCK:
-    case WRITE_LONG:
-    case WRITE_SAME:
+    case WRITE_LONG_10:
+    case WRITE_SAME_10:
     case SEARCH_HIGH_12:
     case SEARCH_EQUAL_12:
     case SEARCH_LOW_12:
     case MEDIUM_SCAN:
     case SEND_VOLUME_TAG:
-    case WRITE_LONG_2:
     case PERSISTENT_RESERVE_OUT:
     case MAINTENANCE_OUT:
         req->cmd.mode = SCSI_XFER_TO_DEV;
@@ -515,6 +515,7 @@ static const char *scsi_command_name(uint8_t cmd)
 {
     static const char *names[] = {
         [ TEST_UNIT_READY          ] = "TEST_UNIT_READY",
+        [ REWIND                   ] = "REWIND",
         [ REQUEST_SENSE            ] = "REQUEST_SENSE",
         [ FORMAT_UNIT              ] = "FORMAT_UNIT",
         [ READ_BLOCK_LIMITS        ] = "READ_BLOCK_LIMITS",
@@ -539,13 +540,12 @@ static const char *scsi_command_name(uint8_t cmd)
         [ RECEIVE_DIAGNOSTIC       ] = "RECEIVE_DIAGNOSTIC",
         [ SEND_DIAGNOSTIC          ] = "SEND_DIAGNOSTIC",
         [ ALLOW_MEDIUM_REMOVAL     ] = "ALLOW_MEDIUM_REMOVAL",
-
-        [ READ_CAPACITY            ] = "READ_CAPACITY",
+        [ READ_CAPACITY_10         ] = "READ_CAPACITY_10",
         [ READ_10                  ] = "READ_10",
         [ WRITE_10                 ] = "WRITE_10",
         [ SEEK_10                  ] = "SEEK_10",
-        [ WRITE_VERIFY             ] = "WRITE_VERIFY",
-        [ VERIFY                   ] = "VERIFY",
+        [ WRITE_VERIFY_10          ] = "WRITE_VERIFY_10",
+        [ VERIFY_10                ] = "VERIFY_10",
         [ SEARCH_HIGH              ] = "SEARCH_HIGH",
         [ SEARCH_EQUAL             ] = "SEARCH_EQUAL",
         [ SEARCH_LOW               ] = "SEARCH_LOW",
@@ -561,11 +561,14 @@ static const char *scsi_command_name(uint8_t cmd)
         [ WRITE_BUFFER             ] = "WRITE_BUFFER",
         [ READ_BUFFER              ] = "READ_BUFFER",
         [ UPDATE_BLOCK             ] = "UPDATE_BLOCK",
-        [ READ_LONG                ] = "READ_LONG",
-        [ WRITE_LONG               ] = "WRITE_LONG",
+        [ READ_LONG_10             ] = "READ_LONG_10",
+        [ WRITE_LONG_10            ] = "WRITE_LONG_10",
         [ CHANGE_DEFINITION        ] = "CHANGE_DEFINITION",
-        [ WRITE_SAME               ] = "WRITE_SAME",
+        [ WRITE_SAME_10            ] = "WRITE_SAME_10",
+        [ UNMAP                    ] = "UNMAP",
         [ READ_TOC                 ] = "READ_TOC",
+        [ REPORT_DENSITY_SUPPORT   ] = "REPORT_DENSITY_SUPPORT",
+        [ GET_CONFIGURATION        ] = "GET_CONFIGURATION",
         [ LOG_SELECT               ] = "LOG_SELECT",
         [ LOG_SENSE                ] = "LOG_SENSE",
         [ MODE_SELECT_10           ] = "MODE_SELECT_10",
@@ -574,27 +577,39 @@ static const char *scsi_command_name(uint8_t cmd)
         [ MODE_SENSE_10            ] = "MODE_SENSE_10",
         [ PERSISTENT_RESERVE_IN    ] = "PERSISTENT_RESERVE_IN",
         [ PERSISTENT_RESERVE_OUT   ] = "PERSISTENT_RESERVE_OUT",
+        [ WRITE_FILEMARKS_16       ] = "WRITE_FILEMARKS_16",
+        [ EXTENDED_COPY            ] = "EXTENDED_COPY",
+        [ ATA_PASSTHROUGH          ] = "ATA_PASSTHROUGH",
+        [ ACCESS_CONTROL_IN        ] = "ACCESS_CONTROL_IN",
+        [ ACCESS_CONTROL_OUT       ] = "ACCESS_CONTROL_OUT",
+        [ READ_16                  ] = "READ_16",
+        [ COMPARE_AND_WRITE        ] = "COMPARE_AND_WRITE",
+        [ WRITE_16                 ] = "WRITE_16",
+        [ WRITE_VERIFY_16          ] = "WRITE_VERIFY_16",
+        [ VERIFY_16                ] = "VERIFY_16",
+        [ SYNCHRONIZE_CACHE_16     ] = "SYNCHRONIZE_CACHE_16",
+        [ LOCATE_16                ] = "LOCATE_16",
+        [ WRITE_SAME_16            ] = "WRITE_SAME_16",
+        [ ERASE_16                 ] = "ERASE_16",
+        [ SERVICE_ACTION_IN        ] = "SERVICE_ACTION_IN",
+        [ WRITE_LONG_16            ] = "WRITE_LONG_16",
+        [ REPORT_LUNS              ] = "REPORT_LUNS",
+        [ BLANK                    ] = "BLANK",
+        [ MAINTENANCE_IN           ] = "MAINTENANCE_IN",
+        [ MAINTENANCE_OUT          ] = "MAINTENANCE_OUT",
         [ MOVE_MEDIUM              ] = "MOVE_MEDIUM",
+        [ LOAD_UNLOAD              ] = "LOAD_UNLOAD",
         [ READ_12                  ] = "READ_12",
         [ WRITE_12                 ] = "WRITE_12",
         [ WRITE_VERIFY_12          ] = "WRITE_VERIFY_12",
+        [ VERIFY_12                ] = "VERIFY_12",
         [ SEARCH_HIGH_12           ] = "SEARCH_HIGH_12",
         [ SEARCH_EQUAL_12          ] = "SEARCH_EQUAL_12",
         [ SEARCH_LOW_12            ] = "SEARCH_LOW_12",
         [ READ_ELEMENT_STATUS      ] = "READ_ELEMENT_STATUS",
         [ SEND_VOLUME_TAG          ] = "SEND_VOLUME_TAG",
-        [ WRITE_LONG_2             ] = "WRITE_LONG_2",
-
-        [ REPORT_DENSITY_SUPPORT   ] = "REPORT_DENSITY_SUPPORT",
-        [ GET_CONFIGURATION        ] = "GET_CONFIGURATION",
-        [ READ_16                  ] = "READ_16",
-        [ WRITE_16                 ] = "WRITE_16",
-        [ WRITE_VERIFY_16          ] = "WRITE_VERIFY_16",
-        [ SERVICE_ACTION_IN        ] = "SERVICE_ACTION_IN",
-        [ REPORT_LUNS              ] = "REPORT_LUNS",
-        [ LOAD_UNLOAD              ] = "LOAD_UNLOAD",
+        [ READ_DEFECT_DATA_12      ] = "READ_DEFECT_DATA_12",
         [ SET_CD_SPEED             ] = "SET_CD_SPEED",
-        [ BLANK                    ] = "BLANK",
     };
 
     if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
hw/scsi-defs.h +33 -21

95 lines not on this page · raw message

hw/scsi-disk.c +5 -5
index 323368a..535fa11 100644
@@ -885,7 +885,7 @@ static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
     case ALLOW_MEDIUM_REMOVAL:
         bdrv_set_locked(s->bs, req->cmd.buf[4] & 1);
         break;
-    case READ_CAPACITY:
+    case READ_CAPACITY_10:
         /* The normal LEN field for this command is zero.  */
         memset(outbuf, 0, 8);
         bdrv_get_geometry(s->bs, &nb_sectors);
@@ -970,7 +970,7 @@ static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
         outbuf[3] = 8;
         buflen = 16;
         break;
-    case VERIFY:
+    case VERIFY_10:
         break;
     default:
         scsi_command_complete(r, CHECK_CONDITION, SENSE_CODE(INVALID_OPCODE));
@@ -1046,13 +1046,13 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
     case RELEASE_10:
     case START_STOP:
     case ALLOW_MEDIUM_REMOVAL:
-    case READ_CAPACITY:
+    case READ_CAPACITY_10:
     case SYNCHRONIZE_CACHE:
     case READ_TOC:
     case GET_CONFIGURATION:
     case SERVICE_ACTION_IN:
     case REPORT_LUNS:
-    case VERIFY:
+    case VERIFY_10:
         rc = scsi_disk_emulate_command(r, outbuf);
         if (rc < 0) {
             return 0;
@@ -1075,7 +1075,7 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
     case WRITE_10:
     case WRITE_12:
     case WRITE_16:
-    case WRITE_VERIFY:
+    case WRITE_VERIFY_10:
     case WRITE_VERIFY_12:
     case WRITE_VERIFY_16:
         len = r->req.cmd.xfer / s->qdev.blocksize;
hw/scsi-generic.c +1 -1
index 90345a7..17e83c7 100644
@@ -406,7 +406,7 @@ static int get_blocksize(BlockDriverState *bdrv)
 
     memset(cmd, 0, sizeof(cmd));
     memset(buf, 0, sizeof(buf));
-    cmd[0] = READ_CAPACITY;
+    cmd[0] = READ_CAPACITY_10;
 
     memset(&io_header, 0, sizeof(io_header));
     io_header.interface_id = 'S';
Signature · 1 line
1.7.3.4
#5 lore raw

PATCH v2 5/6 scsi-disk: Remove 'drive_kind'

In reply to Hannes Reinecke

Instead of using its own definitions scsi-disk should be using the device type of the parent device.

---
 hw/scsi-defs.h |    6 +++++-
 hw/scsi-disk.c |   48 ++++++++++++++++++++++++------------------------
 2 files changed, 29 insertions(+), 25 deletions(-)
hw/scsi-defs.h +5 -1
index f644860..27010b7 100644
@@ -164,6 +164,7 @@
 
 #define TYPE_DISK           0x00
 #define TYPE_TAPE           0x01
+#define TYPE_PRINTER        0x02
 #define TYPE_PROCESSOR      0x03    /* HP scanners use this */
 #define TYPE_WORM           0x04    /* Treated as ROM by our system */
 #define TYPE_ROM            0x05
@@ -171,6 +172,9 @@
 #define TYPE_MOD            0x07    /* Magneto-optical disk -
 				     * - treated as TYPE_DISK */
 #define TYPE_MEDIUM_CHANGER 0x08
-#define TYPE_ENCLOSURE	    0x0d    /* Enclosure Services Device */
+#define TYPE_STORAGE_ARRAY  0x0c    /* Storage array device */
+#define TYPE_ENCLOSURE      0x0d    /* Enclosure Services Device */
+#define TYPE_RBC            0x0e    /* Simplified Direct-Access Device */
+#define TYPE_OSD            0x11    /* Object-storage Device */
 #define TYPE_NO_LUN         0x7f
 
hw/scsi-disk.c +24 -24

138 lines not on this page · raw message

Signature · 1 line
1.7.3.4
#6 lore raw

Re: [Qemu-devel] [PATCH 5/6] scsi-disk: Remove 'drive_kind'

Quoted 133 lines · Hannes Reinecke

Instead of using its own definitions scsi-disk should be using the device type of the parent device.

---
 hw/scsi-defs.h |    6 +++++-
 hw/scsi-disk.c |   48 ++++++++++++++++++++++++------------------------
 2 files changed, 29 insertions(+), 25 deletions(-)
hw/scsi-defs.h +5 -1
index f644860..27010b7 100644
@@ -164,6 +164,7 @@
 
 #define TYPE_DISK           0x00
 #define TYPE_TAPE           0x01
+#define TYPE_PRINTER        0x02
 #define TYPE_PROCESSOR      0x03    /* HP scanners use this */
 #define TYPE_WORM           0x04    /* Treated as ROM by our system */
 #define TYPE_ROM            0x05
@@ -171,6 +172,9 @@
 #define TYPE_MOD            0x07    /* Magneto-optical disk -
 				     * - treated as TYPE_DISK */
 #define TYPE_MEDIUM_CHANGER 0x08
-#define TYPE_ENCLOSURE	    0x0d    /* Enclosure Services Device */
+#define TYPE_STORAGE_ARRAY  0x0c    /* Storage array device */
+#define TYPE_ENCLOSURE      0x0d    /* Enclosure Services Device */
+#define TYPE_RBC            0x0e    /* Simplified Direct-Access Device */
+#define TYPE_OSD            0x11    /* Object-storage Device */
 #define TYPE_NO_LUN         0x7f
 
hw/scsi-disk.c +11 -14
index 535fa11..ae2c157 100644
@@ -74,7 +74,6 @@ struct SCSIDiskState
     char *version;
     char *serial;
     SCSISense sense;
-    SCSIDriveKind drive_kind;
 };
 
 static int scsi_handle_rw_error(SCSIDiskReq *r, int error, int type);
@@ -382,7 +381,7 @@ static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
             return -1;
         }
 
-        if (s->drive_kind == SCSI_CD) {
+        if (s->qdev.type == TYPE_ROM) {
             outbuf[buflen++] = 5;
         } else {
             outbuf[buflen++] = 0;
@@ -401,7 +400,7 @@ static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
             if (s->serial)
                 outbuf[buflen++] = 0x80; // unit serial number
             outbuf[buflen++] = 0x83; // device identification
-            if (s->drive_kind == SCSI_HD) {
+            if (s->qdev.type == TYPE_DISK) {
                 outbuf[buflen++] = 0xb0; // block limits
                 outbuf[buflen++] = 0xb2; // thin provisioning
             }
@@ -460,7 +459,7 @@ static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
             unsigned int opt_io_size =
                     s->qdev.conf.opt_io_size / s->qdev.blocksize;
 
-            if (s->drive_kind == SCSI_CD) {
+            if (s->qdev.type == TYPE_ROM) {
                 DPRINTF("Inquiry (EVPD[%02X] not supported for CDROM\n",
                         page_code);
                 return -1;
@@ -530,12 +529,11 @@ static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
         return buflen;
     }
 
-    if (s->drive_kind == SCSI_CD) {
-        outbuf[0] = 5;
+    outbuf[0] = s->qdev.type & 0x1f;
+    if (s->qdev.type == TYPE_ROM) {
         outbuf[1] = 0x80;
         memcpy(&outbuf[16], "QEMU CD-ROM     ", 16);
     } else {
-        outbuf[0] = 0;
         outbuf[1] = s->removable ? 0x80 : 0;
         memcpy(&outbuf[16], "QEMU HARDDISK   ", 16);
     }
@@ -661,7 +659,7 @@ static int mode_sense_page(SCSIRequest *req, int page, uint8_t *p,
         return p[1] + 2;
 
     case 0x2a: /* CD Capabilities and Mechanical Status page. */
-        if (s->drive_kind != SCSI_CD)
+        if (s->qdev.type != TYPE_ROM)
             return 0;
         p[0] = 0x2a;
         p[1] = 0x14;
@@ -877,7 +875,7 @@ static int scsi_disk_emulate_command(SCSIDiskReq *r, uint8_t *outbuf)
             goto illegal_request;
         break;
     case START_STOP:
-        if (s->drive_kind == SCSI_CD && (req->cmd.buf[4] & 2)) {
+        if (s->qdev.type == TYPE_ROM && (req->cmd.buf[4] & 2)) {
             /* load/eject medium */
             bdrv_eject(s->bs, !(req->cmd.buf[4] & 1));
         }
@@ -1183,7 +1181,7 @@ static void scsi_destroy(SCSIDevice *dev)
     blockdev_mark_auto_del(s->qdev.conf.bs);
 }
 
-static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind)
+static int scsi_initfn(SCSIDevice *dev, uint8_t scsi_type)
 {
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, dev);
     DriveInfo *dinfo;
@@ -1193,9 +1191,8 @@ static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind)
         return -1;
     }
     s->bs = s->qdev.conf.bs;
-    s->drive_kind = kind;
 
-    if (kind == SCSI_HD && !bdrv_is_inserted(s->bs)) {
+    if (scsi_type == TYPE_DISK && !bdrv_is_inserted(s->bs)) {
         error_report("Device needs media, but drive is empty");
         return -1;
     }
@@ -1217,44 +1214,47 @@ static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind)
         return -1;
     }
 
-    if (kind == SCSI_CD) {
+    if (scsi_type == TYPE_ROM) {
         s->qdev.blocksize = 2048;
-    } else {
+    } else if (scsi_type == TYPE_DISK) {

hw/scsi-disk.c static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind) · line 1228

Quoted 10 lines
hw/scsi-disk.c +4 -1
         s->qdev.blocksize = s->qdev.conf.logical_block_size;
+    } else {
+        error_report("scsi-disk: Unhandled SCSI type %02x", scsi_type);
+        return -1;
     }
     s->cluster_size = s->qdev.blocksize / 512;
     s->bs->buffer_alignment = s->qdev.blocksize;
 
-    s->qdev.type = TYPE_DISK;
+    s->qdev.type = scsi_type;

Is this a bug fix?

Quoted 17 lines
hw/scsi-disk.c +3 -3
     qemu_add_vm_change_state_handler(scsi_dma_restart_cb, s);
-    bdrv_set_removable(s->bs, kind == SCSI_CD);
+    bdrv_set_removable(s->bs, scsi_type == TYPE_ROM);
     add_boot_device_path(s->qdev.conf.bootindex, &dev->qdev, ",0");
     return 0;
 }
 
 static int scsi_hd_initfn(SCSIDevice *dev)
 {
-    return scsi_initfn(dev, SCSI_HD);
+    return scsi_initfn(dev, TYPE_DISK);
 }
 
 static int scsi_cd_initfn(SCSIDevice *dev)
 {
-    return scsi_initfn(dev, SCSI_CD);
+    return scsi_initfn(dev, TYPE_ROM);

hw/scsi-disk.c static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind) · line 1251

Quoted 10 lines
hw/scsi-disk.c +1 -3
 }
 
 static int scsi_disk_initfn(SCSIDevice *dev)
 {
-    SCSIDriveKind kind;
     DriveInfo *dinfo;
+    uint8_t scsi_type = TYPE_DISK;
 
-    if (!dev->conf.bs) {
-        kind = SCSI_HD;         /* will die in scsi_initfn() */

The comment explains why we don't explicitly fail when !dev->conf.bs, like all the other block device models. I'd rather keep it.

Quoted 14 lines
hw/scsi-disk.c +5 -3
-    } else {
+    if (dev->conf.bs) {
         dinfo = drive_get_by_blockdev(dev->conf.bs);
-        kind = dinfo->media_cd ? SCSI_CD : SCSI_HD;
+        if (dinfo->media_cd) {
+            scsi_type = TYPE_ROM;
+        }
     }
 
-    return scsi_initfn(dev, kind);
+    return scsi_initfn(dev, scsi_type);
 }
 
 #define DEFINE_SCSI_DISK_PROPERTIES()                           \
#7 lore raw

Re: [Qemu-devel] [PATCH 5/6] scsi-disk: Remove 'drive_kind'

Quoted 11 lines · Markus Armbruster
Quoted 9 lines · Hannes Reinecke

Instead of using its own definitions scsi-disk should be using the device type of the parent device.

---
  hw/scsi-defs.h |    6 +++++-
  hw/scsi-disk.c |   48 ++++++++++++++++++++++++------------------------
  2 files changed, 29 insertions(+), 25 deletions(-)

[ .. ]

Quoted 17 lines
Quoted 17 lines
hw/scsi-disk.c +5 -2
@@ -1217,44 +1214,47 @@ static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind)
          return -1;
      }
 
-    if (kind == SCSI_CD) {
+    if (scsi_type == TYPE_ROM) {
          s->qdev.blocksize = 2048;
-    } else {
+    } else if (scsi_type == TYPE_DISK) {
          s->qdev.blocksize = s->qdev.conf.logical_block_size;
+    } else {
+        error_report("scsi-disk: Unhandled SCSI type %02x", scsi_type);
+        return -1;
      }
      s->cluster_size = s->qdev.blocksize / 512;
      s->bs->buffer_alignment = s->qdev.blocksize;
 
Quoted 5 lines

hw/scsi-disk.c static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind) · line 1228

Quoted 2 lines
hw/scsi-disk.c +1 -1
-    s->qdev.type = TYPE_DISK;
+    s->qdev.type = scsi_type;

Is this a bug fix?

No, proper initialisation.
s->qdev.type is the SCSI type we're told to emulate. So we have to set it to the correct value otherwise the emulation will return wrong values.

Quoted 25 lines
Quoted 25 lines
hw/scsi-disk.c +4 -4
      qemu_add_vm_change_state_handler(scsi_dma_restart_cb, s);
-    bdrv_set_removable(s->bs, kind == SCSI_CD);
+    bdrv_set_removable(s->bs, scsi_type == TYPE_ROM);
      add_boot_device_path(s->qdev.conf.bootindex,&dev->qdev, ",0");
      return 0;
  }
 
  static int scsi_hd_initfn(SCSIDevice *dev)
  {
-    return scsi_initfn(dev, SCSI_HD);
+    return scsi_initfn(dev, TYPE_DISK);
  }
 
  static int scsi_cd_initfn(SCSIDevice *dev)
  {
-    return scsi_initfn(dev, SCSI_CD);
+    return scsi_initfn(dev, TYPE_ROM);
  }
 
  static int scsi_disk_initfn(SCSIDevice *dev)
  {
-    SCSIDriveKind kind;
      DriveInfo *dinfo;
+    uint8_t scsi_type = TYPE_DISK;
 
Quoted 6 lines

hw/scsi-disk.c static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind) · line 1251

Quoted 2 lines
hw/scsi-disk.c +0 -2
-    if (!dev->conf.bs) {
-        kind = SCSI_HD;         /* will die in scsi_initfn() */

The comment explains why we don't explicitly fail when !dev->conf.bs, like all the other block device models. I'd rather keep it.

Ah. The magic of block devices. By all means, keep it.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		      zSeries & Storage
[email protected]			      +49 911 74053 688
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: J. Hawn, J. Guild, F. Imendörffer, HRB 16746 (AG Nürnberg)
#8 lore raw

Re: [Qemu-devel] [PATCH 5/6] scsi-disk: Remove 'drive_kind'

Quoted 35 lines · Hannes Reinecke
Quoted 11 lines · Markus Armbruster
Quoted 9 lines · Hannes Reinecke

Instead of using its own definitions scsi-disk should be using the device type of the parent device.

---
  hw/scsi-defs.h |    6 +++++-
  hw/scsi-disk.c |   48 ++++++++++++++++++++++++------------------------
  2 files changed, 29 insertions(+), 25 deletions(-)

[ .. ]

Quoted 17 lines
Quoted 17 lines
(quoted diff) +5 -2
@@ -1217,44 +1214,47 @@ static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind)
          return -1;
      }
 
-    if (kind == SCSI_CD) {
+    if (scsi_type == TYPE_ROM) {
          s->qdev.blocksize = 2048;
-    } else {
+    } else if (scsi_type == TYPE_DISK) {
          s->qdev.blocksize = s->qdev.conf.logical_block_size;
+    } else {
+        error_report("scsi-disk: Unhandled SCSI type %02x", scsi_type);
+        return -1;
      }
      s->cluster_size = s->qdev.blocksize / 512;
      s->bs->buffer_alignment = s->qdev.blocksize;
 
Quoted 5 lines

Review static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind) · line 1228

Quoted 2 lines
(quoted diff) +1 -1
-    s->qdev.type = TYPE_DISK;
+    s->qdev.type = scsi_type;

Is this a bug fix?

Quoted 4 lines

No, proper initialisation.
s->qdev.type is the SCSI type we're told to emulate. So we have to set it to the correct value otherwise the emulation will return wrong values.

Well, it changes .type from TYPE_DISK to TYPE_ROM for scsi-cd. I understand how that is required for your patch to work. I wonder whether it has an impact beyond that, given that the old value is plainly wrong.

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
(quoted diff) +4 -4
      qemu_add_vm_change_state_handler(scsi_dma_restart_cb, s);
-    bdrv_set_removable(s->bs, kind == SCSI_CD);
+    bdrv_set_removable(s->bs, scsi_type == TYPE_ROM);
      add_boot_device_path(s->qdev.conf.bootindex,&dev->qdev, ",0");
      return 0;
  }
 
  static int scsi_hd_initfn(SCSIDevice *dev)
  {
-    return scsi_initfn(dev, SCSI_HD);
+    return scsi_initfn(dev, TYPE_DISK);
  }
 
  static int scsi_cd_initfn(SCSIDevice *dev)
  {
-    return scsi_initfn(dev, SCSI_CD);
+    return scsi_initfn(dev, TYPE_ROM);
  }
 
  static int scsi_disk_initfn(SCSIDevice *dev)
  {
-    SCSIDriveKind kind;
      DriveInfo *dinfo;
+    uint8_t scsi_type = TYPE_DISK;
 
Quoted 7 lines
Quoted 6 lines

Review static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind) · line 1251

Quoted 2 lines
(quoted diff) +0 -2
-    if (!dev->conf.bs) {
-        kind = SCSI_HD;         /* will die in scsi_initfn() */

The comment explains why we don't explicitly fail when !dev->conf.bs, like all the other block device models. I'd rather keep it.

Ah. The magic of block devices. By all means, keep it.

Like this?

hw/scsi-disk.c +4 -4
index f42a5d1..0925944 100644
@@ -1251,17 +1251,17 @@ static int scsi_cd_initfn(SCSIDevice *dev)
 
 static int scsi_disk_initfn(SCSIDevice *dev)
 {
-    SCSIDriveKind kind;
+    uint8_t scsi_type;
     DriveInfo *dinfo;
 
     if (!dev->conf.bs) {
-        kind = SCSI_HD;         /* will die in scsi_initfn() */
+        scsi_type = TYPE_DISK;  /* will die in scsi_initfn() */
     } else {
         dinfo = drive_get_by_blockdev(dev->conf.bs);
-        kind = dinfo->media_cd ? SCSI_CD : SCSI_HD;
+        scsi_type = dinfo->media_cd ? TYPE_ROM : TYPE_DISK;
     }
 
-    return scsi_initfn(dev, kind);
+    return scsi_initfn(dev, scsi_type);
 }
 
 #define DEFINE_SCSI_DISK_PROPERTIES()                           \

By the way, I'm afraid you forgot to remove typedef SCSIDriveKind. Care to respin this one?

#9 lore raw

Re: [Qemu-devel] [PATCH 5/6] scsi-disk: Remove 'drive_kind'

Quoted 109 lines · Markus Armbruster
Quoted 35 lines · Hannes Reinecke
Quoted 11 lines · Markus Armbruster
Quoted 9 lines · Hannes Reinecke

Instead of using its own definitions scsi-disk should be using the device type of the parent device.

---
   hw/scsi-defs.h |    6 +++++-
   hw/scsi-disk.c |   48 ++++++++++++++++++++++++------------------------
   2 files changed, 29 insertions(+), 25 deletions(-)

[ .. ]

Quoted 17 lines
Quoted 17 lines
(quoted diff) +5 -2
@@ -1217,44 +1214,47 @@ static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind)
           return -1;
       }
 
-    if (kind == SCSI_CD) {
+    if (scsi_type == TYPE_ROM) {
           s->qdev.blocksize = 2048;
-    } else {
+    } else if (scsi_type == TYPE_DISK) {
           s->qdev.blocksize = s->qdev.conf.logical_block_size;
+    } else {
+        error_report("scsi-disk: Unhandled SCSI type %02x", scsi_type);
+        return -1;
       }
       s->cluster_size = s->qdev.blocksize / 512;
       s->bs->buffer_alignment = s->qdev.blocksize;
 
Quoted 5 lines

Review static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind) · line 1228

Quoted 2 lines
(quoted diff) +1 -1
-    s->qdev.type = TYPE_DISK;
+    s->qdev.type = scsi_type;

Is this a bug fix?

Quoted 4 lines

No, proper initialisation.
s->qdev.type is the SCSI type we're told to emulate. So we have to set it to the correct value otherwise the emulation will return wrong values.

Well, it changes .type from TYPE_DISK to TYPE_ROM for scsi-cd. I understand how that is required for your patch to work. I wonder whether it has an impact beyond that, given that the old value is plainly wrong.

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
(quoted diff) +4 -4
       qemu_add_vm_change_state_handler(scsi_dma_restart_cb, s);
-    bdrv_set_removable(s->bs, kind == SCSI_CD);
+    bdrv_set_removable(s->bs, scsi_type == TYPE_ROM);
       add_boot_device_path(s->qdev.conf.bootindex,&dev->qdev, ",0");
       return 0;
   }
 
   static int scsi_hd_initfn(SCSIDevice *dev)
   {
-    return scsi_initfn(dev, SCSI_HD);
+    return scsi_initfn(dev, TYPE_DISK);
   }
 
   static int scsi_cd_initfn(SCSIDevice *dev)
   {
-    return scsi_initfn(dev, SCSI_CD);
+    return scsi_initfn(dev, TYPE_ROM);
   }
 
   static int scsi_disk_initfn(SCSIDevice *dev)
   {
-    SCSIDriveKind kind;
       DriveInfo *dinfo;
+    uint8_t scsi_type = TYPE_DISK;
 
Quoted 7 lines
Quoted 6 lines

Review static int scsi_initfn(SCSIDevice *dev, SCSIDriveKind kind) · line 1251

Quoted 2 lines
(quoted diff) +0 -2
-    if (!dev->conf.bs) {
-        kind = SCSI_HD;         /* will die in scsi_initfn() */

The comment explains why we don't explicitly fail when !dev->conf.bs, like all the other block device models. I'd rather keep it.

Ah. The magic of block devices. By all means, keep it.

Like this?

hw/scsi-disk.c +4 -4
index f42a5d1..0925944 100644
@@ -1251,17 +1251,17 @@ static int scsi_cd_initfn(SCSIDevice *dev)
 
  static int scsi_disk_initfn(SCSIDevice *dev)
  {
-    SCSIDriveKind kind;
+    uint8_t scsi_type;
      DriveInfo *dinfo;
 
      if (!dev->conf.bs) {
-        kind = SCSI_HD;         /* will die in scsi_initfn() */
+        scsi_type = TYPE_DISK;  /* will die in scsi_initfn() */
      } else {
          dinfo = drive_get_by_blockdev(dev->conf.bs);
-        kind = dinfo->media_cd ? SCSI_CD : SCSI_HD;
+        scsi_type = dinfo->media_cd ? TYPE_ROM : TYPE_DISK;
      }
 
-    return scsi_initfn(dev, kind);
+    return scsi_initfn(dev, scsi_type);
  }
 
  #define DEFINE_SCSI_DISK_PROPERTIES()                           \
Quoted 2 lines

By the way, I'm afraid you forgot to remove typedef SCSIDriveKind. Care to respin this one?

Here you go.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		      zSeries & Storage
[email protected]			      +49 911 74053 688
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: J. Hawn, J. Guild, F. Imendörffer, HRB 16746 (AG Nürnberg)
Quoted 1 line

From f6e40a484dbcfdd4f8434ae3427c75a56581d659 Mon Sep 17 00:00:00 2001

From: Hannes Reinecke <[email protected]>
Date: Fri, 22 Jul 2011 16:44:46 +0200
Subject: [PATCH] scsi-disk: Remove 'drive_kind'

Instead of using its own definitions scsi-disk should be using the device type of the parent device.

hw/scsi-defs.h +5 -1
index f644860..27010b7 100644
@@ -164,6 +164,7 @@
 
 #define TYPE_DISK           0x00
 #define TYPE_TAPE           0x01
+#define TYPE_PRINTER        0x02
 #define TYPE_PROCESSOR      0x03    /* HP scanners use this */
 #define TYPE_WORM           0x04    /* Treated as ROM by our system */
 #define TYPE_ROM            0x05
@@ -171,6 +172,9 @@
 #define TYPE_MOD            0x07    /* Magneto-optical disk -
 				     * - treated as TYPE_DISK */
 #define TYPE_MEDIUM_CHANGER 0x08
-#define TYPE_ENCLOSURE	    0x0d    /* Enclosure Services Device */
+#define TYPE_STORAGE_ARRAY  0x0c    /* Storage array device */
+#define TYPE_ENCLOSURE      0x0d    /* Enclosure Services Device */
+#define TYPE_RBC            0x0e    /* Simplified Direct-Access Device */
+#define TYPE_OSD            0x11    /* Object-storage Device */
 #define TYPE_NO_LUN         0x7f
 
hw/scsi-disk.c +22 -24

144 lines not on this page · raw message

#10 lore raw

PATCH v2 6/6 scsi-disk: Check for supported commands

In reply to Hannes Reinecke

Not every command is support for any device type. This patch adds a check for rejecting unsupported commands.

---
 hw/scsi-disk.c |  104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 103 insertions(+), 1 deletions(-)
hw/scsi-disk.c +103 -1
index ae2c157..8ad90c0 100644
@@ -361,13 +361,107 @@ static int scsi_get_sense(SCSIRequest *req, uint8_t *outbuf, int len)
     return scsi_build_sense(s->sense, outbuf, len, len > 14);
 }
 
+#define GENERIC_CMD (uint32_t)0xFFFFFFFF
+#define DISK_CMD (1u << TYPE_DISK)
+#define TAPE_CMD (1u << TYPE_TAPE)
+#define PRINTER_CMD (1u << TYPE_PRINTER)
+#define PROCESSOR_CMD (1u << TYPE_PROCESSOR)
+#define WORM_CMD (1u << TYPE_WORM)
+#define ROM_CMD (1u << TYPE_ROM)
+#define SCANNER_CMD (1u << TYPE_SCANNER)
+#define MOD_CMD (1u << TYPE_MOD)
+#define MEDIUM_CHANGER_CMD (1u << TYPE_MEDIUM_CHANGER)
+#define ARRAY_CMD (1u << TYPE_STORAGE_ARRAY)
+#define ENCLOSURE_CMD (1u << TYPE_ENCLOSURE)
+#define RBC_CMD (1u << TYPE_RBC)
+#define OSD_CMD (1u << TYPE_OSD)
+
+#define NO_ROM_CMD (GENERIC_CMD | ~ROM_CMD)
+
+uint32_t scsi_cmd_table[0x100] = {
+    [TEST_UNIT_READY]           = GENERIC_CMD,
+    [REWIND]                    = TAPE_CMD,
+    [REQUEST_SENSE]             = GENERIC_CMD,
+    [FORMAT_UNIT]               = DISK_CMD|ROM_CMD,
+    [READ_BLOCK_LIMITS]         = TAPE_CMD,
+    [REASSIGN_BLOCKS]           = DISK_CMD|WORM_CMD|MOD_CMD,
+    [READ_6]                    = DISK_CMD|TAPE_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [WRITE_6]                   = DISK_CMD|TAPE_CMD|WORM_CMD|MOD_CMD,
+    [READ_REVERSE]              = TAPE_CMD,
+    [WRITE_FILEMARKS]           = TAPE_CMD,
+    [SPACE]                     = TAPE_CMD,
+    [INQUIRY]                   = GENERIC_CMD,
+    [MODE_SELECT]               = GENERIC_CMD,
+    [RESERVE]                   = TAPE_CMD|PRINTER_CMD,
+    [RELEASE]                   = TAPE_CMD|PRINTER_CMD,
+    [ERASE]                     = TAPE_CMD,
+    [MODE_SENSE]                = GENERIC_CMD,
+    [START_STOP]                = GENERIC_CMD,
+    [RECEIVE_DIAGNOSTIC]        = GENERIC_CMD,
+    [SEND_DIAGNOSTIC]           = GENERIC_CMD,
+    [ALLOW_MEDIUM_REMOVAL]      = GENERIC_CMD,
+    [READ_CAPACITY_10]          = DISK_CMD|WORM_CMD|MOD_CMD,
+    [READ_10]                   = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [WRITE_10]                  = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [SEEK_10]                   = TAPE_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [WRITE_VERIFY_10]           = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [VERIFY_10]                 = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [READ_POSITION]             = TAPE_CMD,
+    [SYNCHRONIZE_CACHE]         = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD|RBC_CMD,
+    [WRITE_BUFFER]              = GENERIC_CMD,
+    [READ_BUFFER]               = GENERIC_CMD,
+    [READ_LONG_10]              = DISK_CMD|WORM_CMD|MOD_CMD,
+    [WRITE_LONG_10]             = DISK_CMD|WORM_CMD|MOD_CMD,
+    [WRITE_SAME_10]             = DISK_CMD,
+    [UNMAP]                     = DISK_CMD,
+    [READ_TOC]                  = ROM_CMD,
+    [REPORT_DENSITY_SUPPORT]    = TAPE_CMD,
+    [GET_CONFIGURATION]         = ROM_CMD,
+    [LOG_SELECT]                = GENERIC_CMD,
+    [LOG_SENSE]                 = GENERIC_CMD,
+    [MODE_SELECT_10]            = GENERIC_CMD,
+    [RESERVE_10]                = PRINTER_CMD,
+    [RELEASE_10]                = PRINTER_CMD,
+    [MODE_SENSE_10]             = GENERIC_CMD,
+    [PERSISTENT_RESERVE_IN]     = GENERIC_CMD,
+    [PERSISTENT_RESERVE_OUT]    = GENERIC_CMD,
+    [VARLENGTH_CDB]             = OSD_CMD,
+    [WRITE_FILEMARKS_16]        = TAPE_CMD,
+    [ATA_PASSTHROUGH]           = DISK_CMD|ROM_CMD|RBC_CMD,
+    [READ_16]                   = DISK_CMD|TAPE_CMD|WORM_CMD|MOD_CMD|RBC_CMD,
+    [WRITE_16]                  = DISK_CMD|TAPE_CMD|WORM_CMD|MOD_CMD|RBC_CMD,
+    [WRITE_VERIFY_16]           = DISK_CMD|WORM_CMD|MOD_CMD|RBC_CMD,
+    [SYNCHRONIZE_CACHE_16]      = DISK_CMD|TAPE_CMD|WORM_CMD|MOD_CMD|RBC_CMD,
+    [LOCATE_16]                 = TAPE_CMD,
+    [WRITE_SAME_16]             = DISK_CMD|TAPE_CMD,
+    [SERVICE_ACTION_IN]         = GENERIC_CMD,
+    [REPORT_LUNS]               = NO_ROM_CMD,
+    [BLANK]                     = ROM_CMD,
+    [MAINTENANCE_IN]            = NO_ROM_CMD,
+    [MAINTENANCE_OUT]           = NO_ROM_CMD,
+    [MOVE_MEDIUM]               = MEDIUM_CHANGER_CMD,
+    [LOAD_UNLOAD]               = ROM_CMD|MEDIUM_CHANGER_CMD,
+    [READ_12]                   = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [WRITE_12]                  = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [WRITE_VERIFY_12]           = DISK_CMD|WORM_CMD|MOD_CMD,
+    [VERIFY_12]                 = DISK_CMD|WORM_CMD|MOD_CMD,
+    [READ_ELEMENT_STATUS]       = WORM_CMD|MOD_CMD,
+    [SET_CD_SPEED]              = ROM_CMD
+};
+
+static bool scsi_command_supported(uint8_t scsi_type, uint8_t cmd)
+{
+    uint32_t mask = (1u << scsi_type);
+    return scsi_cmd_table[cmd] & mask;
+}
+
 static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
 {
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
     int buflen = 0;
 
     if (req->cmd.buf[1] & 0x2) {
-        /* Command support data - optional, not implemented */
+        /* Command support data - obsolete */
         BADF("optional INQUIRY command support request not implemented\n");
         return -1;
     }
@@ -1032,6 +1126,14 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
             return 0;
         }
     }
+    if (!scsi_command_supported(command, s->qdev.type)) {
+        DPRINTF("Command %02x not supported for type %02x\n",
+                command, s->qdev.type);
+        scsi_command_complete(r, CHECK_CONDITION,
+                              SENSE_CODE(INVALID_OPCODE));
+        return 0;
+    }
+
     switch (command) {
     case TEST_UNIT_READY:
     case REQUEST_SENSE:
Signature · 1 line
1.7.3.4
#11 lore raw

Re: [PATCH 6/6] scsi-disk: Check for supported commands

Am 22.07.2011 16:51, schrieb Hannes Reinecke:

Quoted 122 lines

Not every command is support for any device type. This patch adds a check for rejecting unsupported commands.

---
 hw/scsi-disk.c |  104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 103 insertions(+), 1 deletions(-)
hw/scsi-disk.c +95 -1
index ae2c157..8ad90c0 100644
@@ -361,13 +361,107 @@ static int scsi_get_sense(SCSIRequest *req, uint8_t *outbuf, int len)
     return scsi_build_sense(s->sense, outbuf, len, len > 14);
 }
 
+#define GENERIC_CMD (uint32_t)0xFFFFFFFF
+#define DISK_CMD (1u << TYPE_DISK)
+#define TAPE_CMD (1u << TYPE_TAPE)
+#define PRINTER_CMD (1u << TYPE_PRINTER)
+#define PROCESSOR_CMD (1u << TYPE_PROCESSOR)
+#define WORM_CMD (1u << TYPE_WORM)
+#define ROM_CMD (1u << TYPE_ROM)
+#define SCANNER_CMD (1u << TYPE_SCANNER)
+#define MOD_CMD (1u << TYPE_MOD)
+#define MEDIUM_CHANGER_CMD (1u << TYPE_MEDIUM_CHANGER)
+#define ARRAY_CMD (1u << TYPE_STORAGE_ARRAY)
+#define ENCLOSURE_CMD (1u << TYPE_ENCLOSURE)
+#define RBC_CMD (1u << TYPE_RBC)
+#define OSD_CMD (1u << TYPE_OSD)
+
+#define NO_ROM_CMD (GENERIC_CMD | ~ROM_CMD)
+
+uint32_t scsi_cmd_table[0x100] = {
+    [TEST_UNIT_READY]           = GENERIC_CMD,
+    [REWIND]                    = TAPE_CMD,
+    [REQUEST_SENSE]             = GENERIC_CMD,
+    [FORMAT_UNIT]               = DISK_CMD|ROM_CMD,
+    [READ_BLOCK_LIMITS]         = TAPE_CMD,
+    [REASSIGN_BLOCKS]           = DISK_CMD|WORM_CMD|MOD_CMD,
+    [READ_6]                    = DISK_CMD|TAPE_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [WRITE_6]                   = DISK_CMD|TAPE_CMD|WORM_CMD|MOD_CMD,
+    [READ_REVERSE]              = TAPE_CMD,
+    [WRITE_FILEMARKS]           = TAPE_CMD,
+    [SPACE]                     = TAPE_CMD,
+    [INQUIRY]                   = GENERIC_CMD,
+    [MODE_SELECT]               = GENERIC_CMD,
+    [RESERVE]                   = TAPE_CMD|PRINTER_CMD,
+    [RELEASE]                   = TAPE_CMD|PRINTER_CMD,
+    [ERASE]                     = TAPE_CMD,
+    [MODE_SENSE]                = GENERIC_CMD,
+    [START_STOP]                = GENERIC_CMD,
+    [RECEIVE_DIAGNOSTIC]        = GENERIC_CMD,
+    [SEND_DIAGNOSTIC]           = GENERIC_CMD,
+    [ALLOW_MEDIUM_REMOVAL]      = GENERIC_CMD,
+    [READ_CAPACITY_10]          = DISK_CMD|WORM_CMD|MOD_CMD,
+    [READ_10]                   = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [WRITE_10]                  = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [SEEK_10]                   = TAPE_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [WRITE_VERIFY_10]           = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [VERIFY_10]                 = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [READ_POSITION]             = TAPE_CMD,
+    [SYNCHRONIZE_CACHE]         = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD|RBC_CMD,
+    [WRITE_BUFFER]              = GENERIC_CMD,
+    [READ_BUFFER]               = GENERIC_CMD,
+    [READ_LONG_10]              = DISK_CMD|WORM_CMD|MOD_CMD,
+    [WRITE_LONG_10]             = DISK_CMD|WORM_CMD|MOD_CMD,
+    [WRITE_SAME_10]             = DISK_CMD,
+    [UNMAP]                     = DISK_CMD,
+    [READ_TOC]                  = ROM_CMD,
+    [REPORT_DENSITY_SUPPORT]    = TAPE_CMD,
+    [GET_CONFIGURATION]         = ROM_CMD,
+    [LOG_SELECT]                = GENERIC_CMD,
+    [LOG_SENSE]                 = GENERIC_CMD,
+    [MODE_SELECT_10]            = GENERIC_CMD,
+    [RESERVE_10]                = PRINTER_CMD,
+    [RELEASE_10]                = PRINTER_CMD,
+    [MODE_SENSE_10]             = GENERIC_CMD,
+    [PERSISTENT_RESERVE_IN]     = GENERIC_CMD,
+    [PERSISTENT_RESERVE_OUT]    = GENERIC_CMD,
+    [VARLENGTH_CDB]             = OSD_CMD,
+    [WRITE_FILEMARKS_16]        = TAPE_CMD,
+    [ATA_PASSTHROUGH]           = DISK_CMD|ROM_CMD|RBC_CMD,
+    [READ_16]                   = DISK_CMD|TAPE_CMD|WORM_CMD|MOD_CMD|RBC_CMD,
+    [WRITE_16]                  = DISK_CMD|TAPE_CMD|WORM_CMD|MOD_CMD|RBC_CMD,
+    [WRITE_VERIFY_16]           = DISK_CMD|WORM_CMD|MOD_CMD|RBC_CMD,
+    [SYNCHRONIZE_CACHE_16]      = DISK_CMD|TAPE_CMD|WORM_CMD|MOD_CMD|RBC_CMD,
+    [LOCATE_16]                 = TAPE_CMD,
+    [WRITE_SAME_16]             = DISK_CMD|TAPE_CMD,
+    [SERVICE_ACTION_IN]         = GENERIC_CMD,
+    [REPORT_LUNS]               = NO_ROM_CMD,
+    [BLANK]                     = ROM_CMD,
+    [MAINTENANCE_IN]            = NO_ROM_CMD,
+    [MAINTENANCE_OUT]           = NO_ROM_CMD,
+    [MOVE_MEDIUM]               = MEDIUM_CHANGER_CMD,
+    [LOAD_UNLOAD]               = ROM_CMD|MEDIUM_CHANGER_CMD,
+    [READ_12]                   = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [WRITE_12]                  = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [WRITE_VERIFY_12]           = DISK_CMD|WORM_CMD|MOD_CMD,
+    [VERIFY_12]                 = DISK_CMD|WORM_CMD|MOD_CMD,
+    [READ_ELEMENT_STATUS]       = WORM_CMD|MOD_CMD,
+    [SET_CD_SPEED]              = ROM_CMD
+};
+
+static bool scsi_command_supported(uint8_t scsi_type, uint8_t cmd)
+{
+    uint32_t mask = (1u << scsi_type);
+    return scsi_cmd_table[cmd] & mask;
+}
+
 static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
 {
     SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
     int buflen = 0;
 
     if (req->cmd.buf[1] & 0x2) {
-        /* Command support data - optional, not implemented */
+        /* Command support data - obsolete */
         BADF("optional INQUIRY command support request not implemented\n");
         return -1;
     }

hw/scsi-disk.c static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf) · line 1129

Quoted 4 lines
hw/scsi-disk.c +1 -0
@@ -1032,6 +1126,14 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
             return 0;
         }
     }
+    if (!scsi_command_supported(command, s->qdev.type)) {

Wrong parameter order?

Kevin

#12 lore raw

Re: [PATCH 6/6] scsi-disk: Check for supported commands

Quoted 116 lines · Kevin Wolf

Am 22.07.2011 16:51, schrieb Hannes Reinecke:

Quoted 115 lines

Not every command is support for any device type. This patch adds a check for rejecting unsupported commands.

---
  hw/scsi-disk.c |  104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
  1 files changed, 103 insertions(+), 1 deletions(-)
hw/scsi-disk.c +94 -0
index ae2c157..8ad90c0 100644
@@ -361,13 +361,107 @@ static int scsi_get_sense(SCSIRequest *req, uint8_t *outbuf, int len)
      return scsi_build_sense(s->sense, outbuf, len, len>  14);
  }
 
+#define GENERIC_CMD (uint32_t)0xFFFFFFFF
+#define DISK_CMD (1u<<  TYPE_DISK)
+#define TAPE_CMD (1u<<  TYPE_TAPE)
+#define PRINTER_CMD (1u<<  TYPE_PRINTER)
+#define PROCESSOR_CMD (1u<<  TYPE_PROCESSOR)
+#define WORM_CMD (1u<<  TYPE_WORM)
+#define ROM_CMD (1u<<  TYPE_ROM)
+#define SCANNER_CMD (1u<<  TYPE_SCANNER)
+#define MOD_CMD (1u<<  TYPE_MOD)
+#define MEDIUM_CHANGER_CMD (1u<<  TYPE_MEDIUM_CHANGER)
+#define ARRAY_CMD (1u<<  TYPE_STORAGE_ARRAY)
+#define ENCLOSURE_CMD (1u<<  TYPE_ENCLOSURE)
+#define RBC_CMD (1u<<  TYPE_RBC)
+#define OSD_CMD (1u<<  TYPE_OSD)
+
+#define NO_ROM_CMD (GENERIC_CMD | ~ROM_CMD)
+
+uint32_t scsi_cmd_table[0x100] = {
+    [TEST_UNIT_READY]           = GENERIC_CMD,
+    [REWIND]                    = TAPE_CMD,
+    [REQUEST_SENSE]             = GENERIC_CMD,
+    [FORMAT_UNIT]               = DISK_CMD|ROM_CMD,
+    [READ_BLOCK_LIMITS]         = TAPE_CMD,
+    [REASSIGN_BLOCKS]           = DISK_CMD|WORM_CMD|MOD_CMD,
+    [READ_6]                    = DISK_CMD|TAPE_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [WRITE_6]                   = DISK_CMD|TAPE_CMD|WORM_CMD|MOD_CMD,
+    [READ_REVERSE]              = TAPE_CMD,
+    [WRITE_FILEMARKS]           = TAPE_CMD,
+    [SPACE]                     = TAPE_CMD,
+    [INQUIRY]                   = GENERIC_CMD,
+    [MODE_SELECT]               = GENERIC_CMD,
+    [RESERVE]                   = TAPE_CMD|PRINTER_CMD,
+    [RELEASE]                   = TAPE_CMD|PRINTER_CMD,
+    [ERASE]                     = TAPE_CMD,
+    [MODE_SENSE]                = GENERIC_CMD,
+    [START_STOP]                = GENERIC_CMD,
+    [RECEIVE_DIAGNOSTIC]        = GENERIC_CMD,
+    [SEND_DIAGNOSTIC]           = GENERIC_CMD,
+    [ALLOW_MEDIUM_REMOVAL]      = GENERIC_CMD,
+    [READ_CAPACITY_10]          = DISK_CMD|WORM_CMD|MOD_CMD,
+    [READ_10]                   = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [WRITE_10]                  = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [SEEK_10]                   = TAPE_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [WRITE_VERIFY_10]           = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [VERIFY_10]                 = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [READ_POSITION]             = TAPE_CMD,
+    [SYNCHRONIZE_CACHE]         = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD|RBC_CMD,
+    [WRITE_BUFFER]              = GENERIC_CMD,
+    [READ_BUFFER]               = GENERIC_CMD,
+    [READ_LONG_10]              = DISK_CMD|WORM_CMD|MOD_CMD,
+    [WRITE_LONG_10]             = DISK_CMD|WORM_CMD|MOD_CMD,
+    [WRITE_SAME_10]             = DISK_CMD,
+    [UNMAP]                     = DISK_CMD,
+    [READ_TOC]                  = ROM_CMD,
+    [REPORT_DENSITY_SUPPORT]    = TAPE_CMD,
+    [GET_CONFIGURATION]         = ROM_CMD,
+    [LOG_SELECT]                = GENERIC_CMD,
+    [LOG_SENSE]                 = GENERIC_CMD,
+    [MODE_SELECT_10]            = GENERIC_CMD,
+    [RESERVE_10]                = PRINTER_CMD,
+    [RELEASE_10]                = PRINTER_CMD,
+    [MODE_SENSE_10]             = GENERIC_CMD,
+    [PERSISTENT_RESERVE_IN]     = GENERIC_CMD,
+    [PERSISTENT_RESERVE_OUT]    = GENERIC_CMD,
+    [VARLENGTH_CDB]             = OSD_CMD,
+    [WRITE_FILEMARKS_16]        = TAPE_CMD,
+    [ATA_PASSTHROUGH]           = DISK_CMD|ROM_CMD|RBC_CMD,
+    [READ_16]                   = DISK_CMD|TAPE_CMD|WORM_CMD|MOD_CMD|RBC_CMD,
+    [WRITE_16]                  = DISK_CMD|TAPE_CMD|WORM_CMD|MOD_CMD|RBC_CMD,
+    [WRITE_VERIFY_16]           = DISK_CMD|WORM_CMD|MOD_CMD|RBC_CMD,
+    [SYNCHRONIZE_CACHE_16]      = DISK_CMD|TAPE_CMD|WORM_CMD|MOD_CMD|RBC_CMD,
+    [LOCATE_16]                 = TAPE_CMD,
+    [WRITE_SAME_16]             = DISK_CMD|TAPE_CMD,
+    [SERVICE_ACTION_IN]         = GENERIC_CMD,
+    [REPORT_LUNS]               = NO_ROM_CMD,
+    [BLANK]                     = ROM_CMD,
+    [MAINTENANCE_IN]            = NO_ROM_CMD,
+    [MAINTENANCE_OUT]           = NO_ROM_CMD,
+    [MOVE_MEDIUM]               = MEDIUM_CHANGER_CMD,
+    [LOAD_UNLOAD]               = ROM_CMD|MEDIUM_CHANGER_CMD,
+    [READ_12]                   = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [WRITE_12]                  = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [WRITE_VERIFY_12]           = DISK_CMD|WORM_CMD|MOD_CMD,
+    [VERIFY_12]                 = DISK_CMD|WORM_CMD|MOD_CMD,
+    [READ_ELEMENT_STATUS]       = WORM_CMD|MOD_CMD,
+    [SET_CD_SPEED]              = ROM_CMD
+};
+
+static bool scsi_command_supported(uint8_t scsi_type, uint8_t cmd)
+{
+    uint32_t mask = (1u<<  scsi_type);
+    return scsi_cmd_table[cmd]&  mask;
+}
+
  static int scsi_disk_emulate_inquiry(SCSIRequest *req, uint8_t *outbuf)
  {
      SCSIDiskState *s = DO_UPCAST(SCSIDiskState, qdev, req->dev);
      int buflen = 0;
 
Quoted 14 lines

hw/scsi-disk.c static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf) · line 1129

Quoted 11 lines
hw/scsi-disk.c +2 -1
      if (req->cmd.buf[1]&  0x2) {
-        /* Command support data - optional, not implemented */
+        /* Command support data - obsolete */
          BADF("optional INQUIRY command support request not implemented\n");
          return -1;
      }
@@ -1032,6 +1126,14 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
              return 0;
          }
      }
+    if (!scsi_command_supported(command, s->qdev.type)) {

Wrong parameter order?

Don't be picky :-)

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		      zSeries & Storage
[email protected]			      +49 911 74053 688
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: J. Hawn, J. Guild, F. Imendörffer, HRB 16746 (AG Nürnberg)
#13 lore raw

Re: [Qemu-devel] [PATCH 6/6] scsi-disk: Check for supported commands

In reply to Hannes Reinecke
Quoted 4 lines · Hannes Reinecke

Not every command is support for any device type. This patch adds a check for rejecting unsupported commands.

This seems to conflic with Markus' series. But if we want to invest any major effort into it, we really need to different dispatch tables for different device types. There's two sane ways to do it:

one top-level handler with a switch per device type, or tables with a handler pointer with a device type. I'm fine with either one.

What I really don't get with this patch is the listing of all the different SCSI device types. It's already a mistake that we tried to handle disks and CDROMs with the same driver, but adding even more just makes it worth.

IMHO we should simply have one file per SCSI spec, e.g. an spc.c for the common bits, then an sbc.c for disks, and mmc.c for cdroms. Maybe more the day we add more emulated device types.

#14 lore raw

Re: [PATCH 6/6] scsi-disk: Check for supported commands

Quoted 10 lines · Christoph Hellwig
Quoted 4 lines · Hannes Reinecke

Not every command is support for any device type. This patch adds a check for rejecting unsupported commands.

This seems to conflic with Markus' series. But if we want to invest any major effort into it, we really need to different dispatch tables for different device types. There's two sane ways to do it:

Quoted 6 lines

one top-level handler with a switch per device type, or tables with a handler pointer with a device type. I'm fine with either one.

What I really don't get with this patch is the listing of all the different SCSI device types. It's already a mistake that we tried to handle disks and CDROMs with the same driver, but adding even more just makes it worth.

It fits into what we have...

Quoted 3 lines

IMHO we should simply have one file per SCSI spec, e.g. an spc.c for the common bits, then an sbc.c for disks, and mmc.c for cdroms. Maybe more the day we add more emulated device types.

Commit b443ae67 `scsi: Split qdev "scsi-disk" into "scsi-hd" and "scsi-cd"' was a first baby step towards that goal.

Munging everything together may look like an easy way to avoid code duplication initially, but leads to "common" code riddled with special cases.

Proper code reuse among the separate drivers will have to be enforced.

Anyway, back to the patch's topic: ideas on how to reject SCSI commands invalid for the device type given the current state of affairs, i.e. disks and CD-ROMs munged together in scsi-disk.c? "Don't, change state of affairs first" is a valid answer, it just means we probably won't get them rejected any time soon.

#15 lore raw

Re: [PATCH 6/6] scsi-disk: Check for supported commands

In reply to Hannes Reinecke

Am 22.07.2011 16:51, schrieb Hannes Reinecke:

Quoted 4 lines

Not every command is support for any device type. This patch adds a check for rejecting unsupported commands.

We do emulate SEEK (6), but it's not in your scsi_cmd_table at all.

Quoted 27 lines
---
 hw/scsi-disk.c |  104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 103 insertions(+), 1 deletions(-)
hw/scsi-disk.c +15 -0
index ae2c157..8ad90c0 100644
@@ -361,13 +361,107 @@ static int scsi_get_sense(SCSIRequest *req, uint8_t *outbuf, int len)
     return scsi_build_sense(s->sense, outbuf, len, len > 14);
 }
 
+#define GENERIC_CMD (uint32_t)0xFFFFFFFF
+#define DISK_CMD (1u << TYPE_DISK)
+#define TAPE_CMD (1u << TYPE_TAPE)
+#define PRINTER_CMD (1u << TYPE_PRINTER)
+#define PROCESSOR_CMD (1u << TYPE_PROCESSOR)
+#define WORM_CMD (1u << TYPE_WORM)
+#define ROM_CMD (1u << TYPE_ROM)
+#define SCANNER_CMD (1u << TYPE_SCANNER)
+#define MOD_CMD (1u << TYPE_MOD)
+#define MEDIUM_CHANGER_CMD (1u << TYPE_MEDIUM_CHANGER)
+#define ARRAY_CMD (1u << TYPE_STORAGE_ARRAY)
+#define ENCLOSURE_CMD (1u << TYPE_ENCLOSURE)
+#define RBC_CMD (1u << TYPE_RBC)
+#define OSD_CMD (1u << TYPE_OSD)
+

hw/scsi-disk.c static int scsi_get_sense(SCSIRequest *req, uint8_t *outbuf, int len) · line 388

Quoted 10 lines
hw/scsi-disk.c +10 -0
+#define NO_ROM_CMD (GENERIC_CMD | ~ROM_CMD)
+
+uint32_t scsi_cmd_table[0x100] = {
+    [TEST_UNIT_READY]           = GENERIC_CMD,
+    [REWIND]                    = TAPE_CMD,
+    [REQUEST_SENSE]             = GENERIC_CMD,
+    [FORMAT_UNIT]               = DISK_CMD|ROM_CMD,
+    [READ_BLOCK_LIMITS]         = TAPE_CMD,
+    [REASSIGN_BLOCKS]           = DISK_CMD|WORM_CMD|MOD_CMD,
+    [READ_6]                    = DISK_CMD|TAPE_CMD|WORM_CMD|ROM_CMD|MOD_CMD,

My spec says that MMC doesn't support READ(6)

hw/scsi-disk.c static int scsi_get_sense(SCSIRequest *req, uint8_t *outbuf, int len) · line 396

Quoted 8 lines
hw/scsi-disk.c +8 -0
+    [WRITE_6]                   = DISK_CMD|TAPE_CMD|WORM_CMD|MOD_CMD,
+    [READ_REVERSE]              = TAPE_CMD,
+    [WRITE_FILEMARKS]           = TAPE_CMD,
+    [SPACE]                     = TAPE_CMD,
+    [INQUIRY]                   = GENERIC_CMD,
+    [MODE_SELECT]               = GENERIC_CMD,
+    [RESERVE]                   = TAPE_CMD|PRINTER_CMD,
+    [RELEASE]                   = TAPE_CMD|PRINTER_CMD,

What's the reason for allowing this for tape, but not e.g. for disks? It's marked as obsolete for both (and optional for quite a few other types)

hw/scsi-disk.c static int scsi_get_sense(SCSIRequest *req, uint8_t *outbuf, int len) · line 403

Quoted 7 lines
hw/scsi-disk.c +7 -0
+    [ERASE]                     = TAPE_CMD,
+    [MODE_SENSE]                = GENERIC_CMD,
+    [START_STOP]                = GENERIC_CMD,
+    [RECEIVE_DIAGNOSTIC]        = GENERIC_CMD,
+    [SEND_DIAGNOSTIC]           = GENERIC_CMD,
+    [ALLOW_MEDIUM_REMOVAL]      = GENERIC_CMD,
+    [READ_CAPACITY_10]          = DISK_CMD|WORM_CMD|MOD_CMD,

ROM_CMD, too

hw/scsi-disk.c static int scsi_get_sense(SCSIRequest *req, uint8_t *outbuf, int len) · line 406

Quoted 3 lines
hw/scsi-disk.c +3 -0
+    [READ_10]                   = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [WRITE_10]                  = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [SEEK_10]                   = TAPE_CMD|WORM_CMD|ROM_CMD|MOD_CMD,

Tape doesn't have SEEK(10) in the spec.

Quoted 18 lines
hw/scsi-disk.c +18 -0
+    [WRITE_VERIFY_10]           = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [VERIFY_10]                 = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [READ_POSITION]             = TAPE_CMD,
+    [SYNCHRONIZE_CACHE]         = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD|RBC_CMD,
+    [WRITE_BUFFER]              = GENERIC_CMD,
+    [READ_BUFFER]               = GENERIC_CMD,
+    [READ_LONG_10]              = DISK_CMD|WORM_CMD|MOD_CMD,
+    [WRITE_LONG_10]             = DISK_CMD|WORM_CMD|MOD_CMD,
+    [WRITE_SAME_10]             = DISK_CMD,
+    [UNMAP]                     = DISK_CMD,
+    [READ_TOC]                  = ROM_CMD,
+    [REPORT_DENSITY_SUPPORT]    = TAPE_CMD,
+    [GET_CONFIGURATION]         = ROM_CMD,
+    [LOG_SELECT]                = GENERIC_CMD,
+    [LOG_SENSE]                 = GENERIC_CMD,
+    [MODE_SELECT_10]            = GENERIC_CMD,
+    [RESERVE_10]                = PRINTER_CMD,
+    [RELEASE_10]                = PRINTER_CMD,

hw/scsi-disk.c static int scsi_get_sense(SCSIRequest *req, uint8_t *outbuf, int len) · line 434

Quoted 10 lines
hw/scsi-disk.c +10 -0
+    [MODE_SENSE_10]             = GENERIC_CMD,
+    [PERSISTENT_RESERVE_IN]     = GENERIC_CMD,
+    [PERSISTENT_RESERVE_OUT]    = GENERIC_CMD,
+    [VARLENGTH_CDB]             = OSD_CMD,
+    [WRITE_FILEMARKS_16]        = TAPE_CMD,
+    [ATA_PASSTHROUGH]           = DISK_CMD|ROM_CMD|RBC_CMD,
+    [READ_16]                   = DISK_CMD|TAPE_CMD|WORM_CMD|MOD_CMD|RBC_CMD,
+    [WRITE_16]                  = DISK_CMD|TAPE_CMD|WORM_CMD|MOD_CMD|RBC_CMD,
+    [WRITE_VERIFY_16]           = DISK_CMD|WORM_CMD|MOD_CMD|RBC_CMD,
+    [SYNCHRONIZE_CACHE_16]      = DISK_CMD|TAPE_CMD|WORM_CMD|MOD_CMD|RBC_CMD,

Tape doesn't have this.

hw/scsi-disk.c static int scsi_get_sense(SCSIRequest *req, uint8_t *outbuf, int len) · line 436

Quoted 2 lines
hw/scsi-disk.c +2 -0
+    [LOCATE_16]                 = TAPE_CMD,
+    [WRITE_SAME_16]             = DISK_CMD|TAPE_CMD,

Again not for tape in my spec.

Kevin

#16 lore raw

Re: [PATCH 6/6] scsi-disk: Check for supported commands

Quoted 8 lines · Kevin Wolf

Am 22.07.2011 16:51, schrieb Hannes Reinecke:

Quoted 4 lines

Not every command is support for any device type. This patch adds a check for rejecting unsupported commands.

We do emulate SEEK (6), but it's not in your scsi_cmd_table at all.

Hmm.

Quoted 31 lines
Quoted 31 lines
---
  hw/scsi-disk.c |  104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
  1 files changed, 103 insertions(+), 1 deletions(-)
hw/scsi-disk.c +19 -0
index ae2c157..8ad90c0 100644
@@ -361,13 +361,107 @@ static int scsi_get_sense(SCSIRequest *req, uint8_t *outbuf, int len)
      return scsi_build_sense(s->sense, outbuf, len, len>  14);
  }
 
+#define GENERIC_CMD (uint32_t)0xFFFFFFFF
+#define DISK_CMD (1u<<  TYPE_DISK)
+#define TAPE_CMD (1u<<  TYPE_TAPE)
+#define PRINTER_CMD (1u<<  TYPE_PRINTER)
+#define PROCESSOR_CMD (1u<<  TYPE_PROCESSOR)
+#define WORM_CMD (1u<<  TYPE_WORM)
+#define ROM_CMD (1u<<  TYPE_ROM)
+#define SCANNER_CMD (1u<<  TYPE_SCANNER)
+#define MOD_CMD (1u<<  TYPE_MOD)
+#define MEDIUM_CHANGER_CMD (1u<<  TYPE_MEDIUM_CHANGER)
+#define ARRAY_CMD (1u<<  TYPE_STORAGE_ARRAY)
+#define ENCLOSURE_CMD (1u<<  TYPE_ENCLOSURE)
+#define RBC_CMD (1u<<  TYPE_RBC)
+#define OSD_CMD (1u<<  TYPE_OSD)
+
+#define NO_ROM_CMD (GENERIC_CMD | ~ROM_CMD)
+
+uint32_t scsi_cmd_table[0x100] = {
+    [TEST_UNIT_READY]           = GENERIC_CMD,
Quoted 9 lines

hw/scsi-disk.c static int scsi_get_sense(SCSIRequest *req, uint8_t *outbuf, int len) · line 388

Quoted 6 lines
hw/scsi-disk.c +6 -0
+    [REWIND]                    = TAPE_CMD,
+    [REQUEST_SENSE]             = GENERIC_CMD,
+    [FORMAT_UNIT]               = DISK_CMD|ROM_CMD,
+    [READ_BLOCK_LIMITS]         = TAPE_CMD,
+    [REASSIGN_BLOCKS]           = DISK_CMD|WORM_CMD|MOD_CMD,
+    [READ_6]                    = DISK_CMD|TAPE_CMD|WORM_CMD|ROM_CMD|MOD_CMD,

My spec says that MMC doesn't support READ(6)

But it does support 'RECEIVE(6)', with the same opcode.

Quoted 9 lines

hw/scsi-disk.c static int scsi_get_sense(SCSIRequest *req, uint8_t *outbuf, int len) · line 396

Quoted 8 lines
hw/scsi-disk.c +8 -0
+    [WRITE_6]                   = DISK_CMD|TAPE_CMD|WORM_CMD|MOD_CMD,
+    [READ_REVERSE]              = TAPE_CMD,
+    [WRITE_FILEMARKS]           = TAPE_CMD,
+    [SPACE]                     = TAPE_CMD,
+    [INQUIRY]                   = GENERIC_CMD,
+    [MODE_SELECT]               = GENERIC_CMD,
+    [RESERVE]                   = TAPE_CMD|PRINTER_CMD,
+    [RELEASE]                   = TAPE_CMD|PRINTER_CMD,
Quoted 3 lines

What's the reason for allowing this for tape, but not e.g. for disks? It's marked as obsolete for both (and optional for quite a few other types)

Because it's mandatory for TAPE and PRINTER. But the implementation details are horrible and we're not doing anything here (which in itself is a violation of the spec), so I think it's better to not support it if we don't have to.

Quoted 10 lines

hw/scsi-disk.c static int scsi_get_sense(SCSIRequest *req, uint8_t *outbuf, int len) · line 403

Quoted 7 lines
hw/scsi-disk.c +7 -0
+    [ERASE]                     = TAPE_CMD,
+    [MODE_SENSE]                = GENERIC_CMD,
+    [START_STOP]                = GENERIC_CMD,
+    [RECEIVE_DIAGNOSTIC]        = GENERIC_CMD,
+    [SEND_DIAGNOSTIC]           = GENERIC_CMD,
+    [ALLOW_MEDIUM_REMOVAL]      = GENERIC_CMD,
+    [READ_CAPACITY_10]          = DISK_CMD|WORM_CMD|MOD_CMD,

ROM_CMD, too

Ok.

Quoted 6 lines

hw/scsi-disk.c static int scsi_get_sense(SCSIRequest *req, uint8_t *outbuf, int len) · line 406

Quoted 3 lines
hw/scsi-disk.c +3 -0
+    [READ_10]                   = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [WRITE_10]                  = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [SEEK_10]                   = TAPE_CMD|WORM_CMD|ROM_CMD|MOD_CMD,

Tape doesn't have SEEK(10) in the spec.

But it does have 'LOCATE_10', which shares the same opcode.

Quoted 29 lines
Quoted 18 lines
hw/scsi-disk.c +18 -0
+    [WRITE_VERIFY_10]           = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [VERIFY_10]                 = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD,
+    [READ_POSITION]             = TAPE_CMD,
+    [SYNCHRONIZE_CACHE]         = DISK_CMD|WORM_CMD|ROM_CMD|MOD_CMD|RBC_CMD,
+    [WRITE_BUFFER]              = GENERIC_CMD,
+    [READ_BUFFER]               = GENERIC_CMD,
+    [READ_LONG_10]              = DISK_CMD|WORM_CMD|MOD_CMD,
+    [WRITE_LONG_10]             = DISK_CMD|WORM_CMD|MOD_CMD,
+    [WRITE_SAME_10]             = DISK_CMD,
+    [UNMAP]                     = DISK_CMD,
+    [READ_TOC]                  = ROM_CMD,
+    [REPORT_DENSITY_SUPPORT]    = TAPE_CMD,
+    [GET_CONFIGURATION]         = ROM_CMD,
+    [LOG_SELECT]                = GENERIC_CMD,
+    [LOG_SENSE]                 = GENERIC_CMD,
+    [MODE_SELECT_10]            = GENERIC_CMD,
+    [RESERVE_10]                = PRINTER_CMD,
+    [RELEASE_10]                = PRINTER_CMD,

hw/scsi-disk.c static int scsi_get_sense(SCSIRequest *req, uint8_t *outbuf, int len) · line 434

Quoted 10 lines
hw/scsi-disk.c +10 -0
+    [MODE_SENSE_10]             = GENERIC_CMD,
+    [PERSISTENT_RESERVE_IN]     = GENERIC_CMD,
+    [PERSISTENT_RESERVE_OUT]    = GENERIC_CMD,
+    [VARLENGTH_CDB]             = OSD_CMD,
+    [WRITE_FILEMARKS_16]        = TAPE_CMD,
+    [ATA_PASSTHROUGH]           = DISK_CMD|ROM_CMD|RBC_CMD,
+    [READ_16]                   = DISK_CMD|TAPE_CMD|WORM_CMD|MOD_CMD|RBC_CMD,
+    [WRITE_16]                  = DISK_CMD|TAPE_CMD|WORM_CMD|MOD_CMD|RBC_CMD,
+    [WRITE_VERIFY_16]           = DISK_CMD|WORM_CMD|MOD_CMD|RBC_CMD,
+    [SYNCHRONIZE_CACHE_16]      = DISK_CMD|TAPE_CMD|WORM_CMD|MOD_CMD|RBC_CMD,
Quoted 2 lines

Tape doesn't have this.

It's called 'SPACE(16)' for tapes.

Quoted 5 lines

hw/scsi-disk.c static int scsi_get_sense(SCSIRequest *req, uint8_t *outbuf, int len) · line 436

Quoted 2 lines
hw/scsi-disk.c +2 -0
+    [LOCATE_16]                 = TAPE_CMD,
+    [WRITE_SAME_16]             = DISK_CMD|TAPE_CMD,

Again not for tape in my spec.

But tape has 'ERASE(16)', again with the same opcode.

However, these duplicate opcodes are really awkward. Especially with debugging enabled one wouldn't be able to print out the correct name for an opcode.

I guess I take the approach suggested by hch and have different tables for the individual device-types.

Let's see ...

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		      zSeries & Storage
[email protected]			      +49 911 74053 688
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: J. Hawn, J. Guild, F. Imendörffer, HRB 16746 (AG Nürnberg)
#17 lore raw
In reply to Hannes Reinecke
Quoted 3 lines · Hannes Reinecke

Markus Armbruster pointed out that not every SCSI command is supported for a given device type. Based on his patch and suggestiongs this series cleans up the SCSI device type and adds a check for supported commands.

I like this series. It conflicts with mine. I can work with Kevin to resolve the conflict.

#18 lore raw
Quoted 8 lines · Markus Armbruster
Quoted 3 lines · Hannes Reinecke

Markus Armbruster pointed out that not every SCSI command is supported for a given device type. Based on his patch and suggestiongs this series cleans up the SCSI device type and adds a check for supported commands.

I like this series. It conflicts with mine. I can work with Kevin to resolve the conflict.

Yes please. I'm happy to assist.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		      zSeries & Storage
[email protected]			      +49 911 74053 688
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: J. Hawn, J. Guild, F. Imendörffer, HRB 16746 (AG Nürnberg)
Applied
#19 lore raw
In reply to Hannes Reinecke

Am 22.07.2011 16:51, schrieb Hannes Reinecke:

Quoted 12 lines

Markus Armbruster pointed out that not every SCSI command is supported for a given device type. Based on his patch and suggestiongs this series cleans up the SCSI device type and adds a check for supported commands.

Hannes Reinecke (6):
scsi-disk: Codingstyle fixes
scsi: Remove references to SET_WINDOW
scsi: Remove REZERO_UNIT emulation
scsi: Sanitize command definitions
scsi-disk: Remove 'drive_kind'
scsi-disk: Check for supported commands

Quoted 5 lines
 hw/scsi-bus.c     |   74 ++++++++++++---------
 hw/scsi-defs.h    |   62 +++++++++++-------
 hw/scsi-disk.c    |  185 ++++++++++++++++++++++++++++++++++++++++-------------
 hw/scsi-generic.c |    2 +-
 4 files changed, 221 insertions(+), 102 deletions(-)

Thanks, applied patches 1-5 to the block branch.

Please send v3 of patch 6 on top.

Kevin