Skip to contentSkip to conversation

scsi-bus: Add support for SCSI scanners

Qemu-devel Jarkko Lavinen 2 participants 5 messages Jun 28, 2016 – Jun 30, 2016

lore
Share & tools
lore
#0 lore raw

I have been trying to get Minolta Multi Pro SCSI film scanner working with Qemu in Linux and WinXP guests. With Linux guest and virtio interface the scanner works out of the box. Unfortunately I haven't been able to find virtio SCSI driver for Win XP.

With Linux guest and pvscsi driver additional patches are needed to add support for some missing commands. The same patches are also needed with WinXP guest with virtual LSI SCSI adapter drivers.

With these patches I am able to do index scan, frame selection, prescan, focus, scanner calibration,main scan (35mm and medium format) and do the transport lockup. What is still missing is the real implementation of REQUEST SENSE command with extra bytes which the scanner application use with focusing.

Few times the scans start return nothing but black in XP but native Vuescan in Linux works just right and I have to restart the scanner to fix this.

I've also seen soft lockups while scanning with Test Unit Ready / Mode Sense in loop with kernel watchdog nagging. Could be that too verbose SCSI debugging is choking the system. I haven't touched the kernel (4.4.14) only written 022000" or 022022 (not the octal number but decimal value) to /proc/sys/dev/scsi/logging_level.

But these patches are still a proof of concept. I am able to use the SCSI scanner with virtual LSI adapter from Qemu with XP or Linux guest and and almost everything works. With VMware and VirtualBox not much or almost nothing works.

Jarkko Lavinen

Quoted 1 line

From 9358393e19dc1fcf85e9371cb50b6dfb9c640d42 Mon Sep 17 00:00:00 2001

From: Jarkko Lavinen <[email protected]>
Date: Tue, 28 Jun 2016 17:26:58 +0300
Subject: [PATCH 1/2] scsi-bus: Use correct transfer lengths for common SCSI
commands

Fix the transfer lenghts as per ANSI spec section 8. The change for INQUIRY and REQUEST_SENSE are cosmetic and have no effect when commands are emulated.

---
 hw/scsi/scsi-bus.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
hw/scsi/scsi-bus.c +8 -2
index ad6f398..045594a 100644
@@ -949,7 +949,15 @@ static int scsi_req_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
         }
         cmd->xfer *= dev->blocksize;
         break;
+    case INQUIRY:
     case MODE_SENSE:
+    case MODE_SELECT:
+    case REQUEST_SENSE:
+        cmd->xfer = buf[4];
+        break;
+    case MODE_SENSE_10:
+    case MODE_SELECT_10:
+        cmd->xfer = (buf[7] << 8) | buf[8];
         break;
     case WRITE_SAME_10:
     case WRITE_SAME_16:
@@ -1004,7 +1012,6 @@ static int scsi_req_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
             cmd->xfer = (buf[1] & 16) == 0 ? 0 : (buf[1] & 32 ? 8 : 4);
         }
         break;
-    case INQUIRY:
     case RECEIVE_DIAGNOSTIC:
     case SEND_DIAGNOSTIC:
         cmd->xfer = buf[4] | (buf[3] << 8);
@@ -1132,7 +1139,6 @@ static int scsi_req_medium_changer_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8
     return 0;
 }
 
-
 static void scsi_cmd_xfer_mode(SCSICommand *cmd)
 {
     if (!cmd->xfer) {
Signature · 1 line
2.1.4
Quoted 1 line

From e78b81f02cb53369499a0cc70495f1b84f6dbfc8 Mon Sep 17 00:00:00 2001

From: Jarkko Lavinen <[email protected]>
Date: Tue, 28 Jun 2016 17:49:41 +0300
Subject: [PATCH 2/2] scsi-bus: Add SCSI scanner support

Add support for missing scanner specific SCSI commands and their xfer lenghts as per ANSI spec section 15.

---
 hw/scsi/scsi-bus.c   | 31 +++++++++++++++++++++++++++++++
 include/block/scsi.h |  6 ++++++
 2 files changed, 37 insertions(+)
hw/scsi/scsi-bus.c +31 -0
index 045594a..2f8da49 100644
@@ -1139,6 +1139,32 @@ static int scsi_req_medium_changer_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8
     return 0;
 }
 
+static int scsi_req_scanner_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
+{
+    switch (buf[0]) {
+    /* Scanner commands */
+    case OBJECT_POSITION:
+        cmd->xfer = 0;
+        break;
+    case GET_DATA_BUFFER_STATUS:
+        cmd->xfer = buf[8] | (buf[7] << 8);
+        break;
+    case SCAN:
+        cmd->xfer = buf[4];
+        break;
+    case READ_10:
+    case SEND:
+    case GET_WINDOW:
+    case SET_WINDOW:
+        cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
+        break;
+    default:
+        return scsi_req_xfer(cmd, dev, buf);
+    }
+
+    return 0;
+}
+
 static void scsi_cmd_xfer_mode(SCSICommand *cmd)
 {
     if (!cmd->xfer) {
@@ -1184,6 +1210,8 @@ static void scsi_cmd_xfer_mode(SCSICommand *cmd)
     case SEND_DVD_STRUCTURE:
     case PERSISTENT_RESERVE_OUT:
     case MAINTENANCE_OUT:
+    case SET_WINDOW:
+    case SCAN:
         cmd->mode = SCSI_XFER_TO_DEV;
         break;
     case ATA_PASSTHROUGH_12:
@@ -1264,6 +1292,9 @@ int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf)
     case TYPE_MEDIUM_CHANGER:
         rc = scsi_req_medium_changer_xfer(cmd, dev, buf);
         break;
+    case TYPE_SCANNER:
+        rc = scsi_req_scanner_length(cmd, dev, buf);
+        break;
     default:
         rc = scsi_req_xfer(cmd, dev, buf);
         break;
include/block/scsi.h +6 -0
index a311341..8b966d7 100644
@@ -48,13 +48,17 @@
 #define ERASE                 0x19
 #define MODE_SENSE            0x1a
 #define LOAD_UNLOAD           0x1b
+#define SCAN                  0x1b
 #define START_STOP            0x1b
 #define RECEIVE_DIAGNOSTIC    0x1c
 #define SEND_DIAGNOSTIC       0x1d
 #define ALLOW_MEDIUM_REMOVAL  0x1e
+#define SET_WINDOW            0x24
 #define READ_CAPACITY_10      0x25
+#define GET_WINDOW            0x25
 #define READ_10               0x28
 #define WRITE_10              0x2a
+#define SEND                  0x2a
 #define SEEK_10               0x2b
 #define LOCATE_10             0x2b
 #define POSITION_TO_ELEMENT   0x2b
@@ -62,10 +66,12 @@
 #define VERIFY_10             0x2f
 #define SEARCH_HIGH           0x30
 #define SEARCH_EQUAL          0x31
+#define OBJECT_POSITION       0x31
 #define SEARCH_LOW            0x32
 #define SET_LIMITS            0x33
 #define PRE_FETCH             0x34
 #define READ_POSITION         0x34
+#define GET_DATA_BUFFER_STATUS 0x34
 #define SYNCHRONIZE_CACHE     0x35
 #define LOCK_UNLOCK_CACHE     0x36
 #define INITIALIZE_ELEMENT_STATUS_WITH_RANGE 0x37
Signature · 1 line
2.1.4
#1 lore raw
Quoted 1 line

+ case INQUIRY:

This is wrong, because INQUIRY's byte 3 is defined to be part of the length in modern SCSI standards. Perhaps you can move it to scsi_req_scanner_length if really necessary?

hw/scsi/scsi-bus.c static int scsi_req_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) · line 957

Quoted 5 lines

case MODE_SENSE:

hw/scsi/scsi-bus.c +4 -0
+    case MODE_SELECT:
+    case REQUEST_SENSE:
+        cmd->xfer = buf[4];
+        break;

This is unnecessary, because scsi_cdb_xfer should get it right (the commands are respectively 15h for MODE SELECT and 03h for REQUEST SENSE; the existing entry for MODE SENSE is also unnecessary).

hw/scsi/scsi-bus.c static int scsi_req_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf) · line 961

Quoted 4 lines
hw/scsi/scsi-bus.c +3 -0
+    case MODE_SENSE_10:
+    case MODE_SELECT_10:
+        cmd->xfer = (buf[7] << 8) | buf[8];
         break;

Same here, scsi_cdb_xfer should handle it.

Quoted 21 lines
hw/scsi/scsi-bus.c +12 -0
index 045594a..2f8da49 100644
@@ -1139,6 +1139,32 @@ static int scsi_req_medium_changer_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8
     return 0;
 }
 
+static int scsi_req_scanner_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
+{
+    switch (buf[0]) {
+    /* Scanner commands */
+    case OBJECT_POSITION:
+        cmd->xfer = 0;
+        break;
+    case GET_DATA_BUFFER_STATUS:
+        cmd->xfer = buf[8] | (buf[7] << 8);
+        break;
+    case SCAN:
+        cmd->xfer = buf[4];

hw/scsi/scsi-bus.c static int scsi_req_medium_changer_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8 · line 1163

Quoted 10 lines
hw/scsi/scsi-bus.c +10 -0
+        break;
+    case READ_10:
+    case SEND:
+    case GET_WINDOW:
+    case SET_WINDOW:
+        cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
+        break;
+    default:
+        return scsi_req_xfer(cmd, dev, buf);
+    }

Looks good.

hw/scsi/scsi-bus.c static void scsi_cmd_xfer_mode(SCSICommand *cmd) · line 1214

Quoted 12 lines
hw/scsi/scsi-bus.c +5 -0
+    return 0;
+}
+
 static void scsi_cmd_xfer_mode(SCSICommand *cmd)
 {
     if (!cmd->xfer) {
@@ -1184,6 +1210,8 @@ static void scsi_cmd_xfer_mode(SCSICommand *cmd)
     case SEND_DVD_STRUCTURE:
     case PERSISTENT_RESERVE_OUT:
     case MAINTENANCE_OUT:
+    case SET_WINDOW:
+    case SCAN:

SCAN conflicts with START_STOP. Add a comment please saying that START_STOP has cmd->xfer set to 0 in scsi_req_xfer for non-scanner devices.

Everything else looks good.

Thanks,

Paolo

Quoted 51 lines
hw/scsi/scsi-bus.c +3 -0
         cmd->mode = SCSI_XFER_TO_DEV;
         break;
     case ATA_PASSTHROUGH_12:
@@ -1264,6 +1292,9 @@ int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf)
     case TYPE_MEDIUM_CHANGER:
         rc = scsi_req_medium_changer_xfer(cmd, dev, buf);
         break;
+    case TYPE_SCANNER:
+        rc = scsi_req_scanner_length(cmd, dev, buf);
+        break;
     default:
         rc = scsi_req_xfer(cmd, dev, buf);
         break;
include/block/scsi.h +6 -0
index a311341..8b966d7 100644
@@ -48,13 +48,17 @@
 #define ERASE                 0x19
 #define MODE_SENSE            0x1a
 #define LOAD_UNLOAD           0x1b
+#define SCAN                  0x1b
 #define START_STOP            0x1b
 #define RECEIVE_DIAGNOSTIC    0x1c
 #define SEND_DIAGNOSTIC       0x1d
 #define ALLOW_MEDIUM_REMOVAL  0x1e
+#define SET_WINDOW            0x24
 #define READ_CAPACITY_10      0x25
+#define GET_WINDOW            0x25
 #define READ_10               0x28
 #define WRITE_10              0x2a
+#define SEND                  0x2a
 #define SEEK_10               0x2b
 #define LOCATE_10             0x2b
 #define POSITION_TO_ELEMENT   0x2b
@@ -62,10 +66,12 @@
 #define VERIFY_10             0x2f
 #define SEARCH_HIGH           0x30
 #define SEARCH_EQUAL          0x31
+#define OBJECT_POSITION       0x31
 #define SEARCH_LOW            0x32
 #define SET_LIMITS            0x33
 #define PRE_FETCH             0x34
 #define READ_POSITION         0x34
+#define GET_DATA_BUFFER_STATUS 0x34
 #define SYNCHRONIZE_CACHE     0x35
 #define LOCK_UNLOCK_CACHE     0x36
 #define INITIALIZE_ELEMENT_STATUS_WITH_RANGE 0x37
Signature · 1 line
2.1.4
#2 lore raw
Quoted 5 lines · Paolo Bonzini

This is wrong, because INQUIRY's byte 3 is defined to be part of the length in modern SCSI standards.

This is wrong, because INQUIRY's byte 3 is defined to be part of the length in modern SCSI standards.

Ok. I was using outdated ANSI spec too pedantically. The first patch is not needed at all.

Quoted 3 lines

SCAN conflicts with START_STOP. Add a comment please saying that START_STOP has cmd->xfer set to 0 in scsi_req_xfer for non-scanner devices.

Done.

I also removed this:

    case GET_DATA_BUFFER_STATUS:
        cmd->xfer = buf[8] | (buf[7] << 8);

Since it is again unneedes and handled by scsi_cdb_xfer.

I added longer sense buffer in Request Sense command for scanners. Multi Pro has at least one sense bit in byte 18 and Minolta's own SW uses 20 bytes as an allocation size in Request Sense command.

The extra sense bit seems to be a kind of internal busy/film holder moving status. After having this sense bit available scanning programs have been very stable and every feature seems to be working.

Jarkko

Quoted 1 line

From 0eb0f4ef920973f62ad3029f5b9ac726cfe610cf Mon Sep 17 00:00:00 2001

From: Jarkko Lavinen <[email protected]>
Date: Tue, 28 Jun 2016 21:51:52 +0300
Subject: [PATCH 1/2] scsi-bus: Add SCSI scanner support

Add support for missing scanner specific SCSI commands and their xfer lenghts as per ANSI spec section 15.

---
 hw/scsi/scsi-bus.c   | 30 ++++++++++++++++++++++++++++++
 include/block/scsi.h |  6 ++++++
 2 files changed, 36 insertions(+)
hw/scsi/scsi-bus.c +30 -0
index ad6f398..e0fbaee 100644
@@ -1132,6 +1132,31 @@ static int scsi_req_medium_changer_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8
     return 0;
 }
 
+static int scsi_req_scanner_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
+{
+    switch (buf[0]) {
+    /* Scanner commands */
+    case OBJECT_POSITION:
+        cmd->xfer = 0;
+        break;
+    case SCAN:
+        /* SCAN conflicts with START_STOP which has cmd->xfer set to 0 for
+           non-scanner devices */
+        cmd->xfer = buf[4];
+        break;
+    case READ_10:
+    case SEND:
+    case GET_WINDOW:
+    case SET_WINDOW:
+        cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
+        break;
+    default:
+        /* GET_DATA_BUFFER_STATUS xfer handled by scsi_req_xfer */
+        return scsi_req_xfer(cmd, dev, buf);
+    }
+
+    return 0;
+}
 
 static void scsi_cmd_xfer_mode(SCSICommand *cmd)
 {
@@ -1178,6 +1203,8 @@ static void scsi_cmd_xfer_mode(SCSICommand *cmd)
     case SEND_DVD_STRUCTURE:
     case PERSISTENT_RESERVE_OUT:
     case MAINTENANCE_OUT:
+    case SET_WINDOW:
+    case SCAN:
         cmd->mode = SCSI_XFER_TO_DEV;
         break;
     case ATA_PASSTHROUGH_12:
@@ -1258,6 +1285,9 @@ int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf)
     case TYPE_MEDIUM_CHANGER:
         rc = scsi_req_medium_changer_xfer(cmd, dev, buf);
         break;
+    case TYPE_SCANNER:
+        rc = scsi_req_scanner_length(cmd, dev, buf);
+        break;
     default:
         rc = scsi_req_xfer(cmd, dev, buf);
         break;
include/block/scsi.h +6 -0
index a311341..8b966d7 100644
@@ -48,13 +48,17 @@
 #define ERASE                 0x19
 #define MODE_SENSE            0x1a
 #define LOAD_UNLOAD           0x1b
+#define SCAN                  0x1b
 #define START_STOP            0x1b
 #define RECEIVE_DIAGNOSTIC    0x1c
 #define SEND_DIAGNOSTIC       0x1d
 #define ALLOW_MEDIUM_REMOVAL  0x1e
+#define SET_WINDOW            0x24
 #define READ_CAPACITY_10      0x25
+#define GET_WINDOW            0x25
 #define READ_10               0x28
 #define WRITE_10              0x2a
+#define SEND                  0x2a
 #define SEEK_10               0x2b
 #define LOCATE_10             0x2b
 #define POSITION_TO_ELEMENT   0x2b
@@ -62,10 +66,12 @@
 #define VERIFY_10             0x2f
 #define SEARCH_HIGH           0x30
 #define SEARCH_EQUAL          0x31
+#define OBJECT_POSITION       0x31
 #define SEARCH_LOW            0x32
 #define SET_LIMITS            0x33
 #define PRE_FETCH             0x34
 #define READ_POSITION         0x34
+#define GET_DATA_BUFFER_STATUS 0x34
 #define SYNCHRONIZE_CACHE     0x35
 #define LOCK_UNLOCK_CACHE     0x36
 #define INITIALIZE_ELEMENT_STATUS_WITH_RANGE 0x37
Signature · 1 line
2.1.4
Quoted 1 line

From dd85d2aac375db97e860a0908e5bfd5192d0dc61 Mon Sep 17 00:00:00 2001

From: Jarkko Lavinen <[email protected]>
Date: Wed, 29 Jun 2016 03:11:46 +0300
Subject: [PATCH 2/2] scsi-bus: Use longer sense buffer with scanners

Scanners can provide additional sense bytes beyond 18 bytes. VueScan uses 32 bytes alloc length with Request Sense command.

---
 hw/scsi/scsi-bus.c     | 10 +++++++++-
 include/hw/scsi/scsi.h |  1 +
 2 files changed, 10 insertions(+), 1 deletion(-)
hw/scsi/scsi-bus.c +9 -1
index e0fbaee..367c5c2 100644
@@ -461,6 +461,14 @@ static bool scsi_target_emulate_inquiry(SCSITargetReq *r)
     return true;
 }
 
+static size_t scsi_sense_len(SCSIRequest *req)
+{
+    if (req->dev->type == TYPE_SCANNER)
+        return SCSI_SENSE_LEN_XTRA;
+    else
+        return SCSI_SENSE_LEN;
+}
+
 static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf)
 {
     SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
@@ -477,7 +485,7 @@ static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf)
         }
         break;
     case REQUEST_SENSE:
-        scsi_target_alloc_buf(&r->req, SCSI_SENSE_LEN);
+        scsi_target_alloc_buf(&r->req, scsi_sense_len(req));
         r->len = scsi_device_get_sense(r->req.dev, r->buf,
                                        MIN(req->cmd.xfer, r->buf_len),
                                        (req->cmd.buf[1] & 1) == 0);
include/hw/scsi/scsi.h +1 -0
index 8acd3fa..76d121d 100644
@@ -10,6 +10,7 @@
 
 #define SCSI_CMD_BUF_SIZE     16
 #define SCSI_SENSE_LEN      18
+#define SCSI_SENSE_LEN_XTRA 32
 #define SCSI_INQUIRY_LEN    36
 
 typedef struct SCSIBus SCSIBus;
Signature · 1 line
2.1.4
#3 lore raw
Quoted 21 lines · Jarkko Lavinen
Quoted 5 lines · Paolo Bonzini

This is wrong, because INQUIRY's byte 3 is defined to be part of the length in modern SCSI standards.

This is wrong, because INQUIRY's byte 3 is defined to be part of the length in modern SCSI standards.

Ok. I was using outdated ANSI spec too pedantically. The first patch is not needed at all.

Quoted 3 lines

SCAN conflicts with START_STOP. Add a comment please saying that START_STOP has cmd->xfer set to 0 in scsi_req_xfer for non-scanner devices.

Done.

I also removed this:

    case GET_DATA_BUFFER_STATUS:
        cmd->xfer = buf[8] | (buf[7] << 8);

Since it is again unneedes and handled by scsi_cdb_xfer.

Quoted 7 lines

I added longer sense buffer in Request Sense command for scanners. Multi Pro has at least one sense bit in byte 18 and Minolta's own SW uses 20 bytes as an allocation size in Request Sense command.

The extra sense bit seems to be a kind of internal busy/film holder moving status. After having this sense bit available scanning programs have been very stable and every feature seems to be working.

Thanks! Out of curiosity what HBA are you using on the host?

Paolo

#4 lore raw
Quoted 5 lines · Paolo Bonzini
Quoted 3 lines

The extra sense bit seems to be a kind of internal busy/film holder moving status. After having this sense bit available scanning programs have been very stable and every feature seems to be working.

Thanks! Out of curiosity what HBA are you using on the host?

I did the testing with Adaptec 29320LPE. I have also 29160N on a PCI slot, but it does not support MSI. Initially I was trying to use PCI pass-through with IOMMU which requires MSI. I have also seen one kernel crash when probing / initializing 29160N but that was with Xen and IOMMU enabled.

Multi Pros have also a Firewire port. These are known issue over time when scanner refuses to initialize if set to use Firewire port but still work with SCSI port.

Jarkko