Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Ki86K-0005m7-Uv for qemu-devel@nongnu.org; Tue, 23 Sep 2008 09:38:49 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Ki86J-0005lN-6g for qemu-devel@nongnu.org; Tue, 23 Sep 2008 09:38:48 -0400 Received: from [199.232.76.173] (port=43234 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Ki86J-0005lJ-0i for qemu-devel@nongnu.org; Tue, 23 Sep 2008 09:38:47 -0400 Received: from savannah.gnu.org ([199.232.41.3]:46005 helo=sv.gnu.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1Ki86I-0004X4-9a for qemu-devel@nongnu.org; Tue, 23 Sep 2008 09:38:46 -0400 Received: from cvs.savannah.gnu.org ([199.232.41.69]) by sv.gnu.org with esmtp (Exim 4.63) (envelope-from ) id 1Ki86G-0002n5-Qj for qemu-devel@nongnu.org; Tue, 23 Sep 2008 13:38:44 +0000 Received: from aliguori by cvs.savannah.gnu.org with local (Exim 4.63) (envelope-from ) id 1Ki86G-0002n1-Ix for qemu-devel@nongnu.org; Tue, 23 Sep 2008 13:38:44 +0000 MIME-Version: 1.0 Errors-To: aliguori Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Anthony Liguori Message-Id: Date: Tue, 23 Sep 2008 13:38:44 +0000 Subject: [Qemu-devel] [5305] scsi-generic: decode correctly SCSI tape commands (Laurent Vivier) Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Revision: 5305 http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5305 Author: aliguori Date: 2008-09-23 13:38:44 +0000 (Tue, 23 Sep 2008) Log Message: ----------- scsi-generic: decode correctly SCSI tape commands (Laurent Vivier) This patch allows to use a "real" SCSI tape with qemu using "-drive /dev/sgX,if=scsi". It allows to decode correctly transfer length when the type of the device is a tape. Some issues remain when the application reading the tape tries to go beyond the end of the stream (but they must be corrected at the SCSI controller level). Signed-off-by: Laurent Vivier Signed-off-by: Anthony Liguori Modified Paths: -------------- trunk/hw/scsi-generic.c Modified: trunk/hw/scsi-generic.c =================================================================== --- trunk/hw/scsi-generic.c 2008-09-23 02:57:00 UTC (rev 5304) +++ trunk/hw/scsi-generic.c 2008-09-23 13:38:44 UTC (rev 5305) @@ -44,12 +44,14 @@ #include #include +#define REWIND 0x01 +#define REPORT_DENSITY_SUPPORT 0x44 #define LOAD_UNLOAD 0xa6 #define SET_CD_SPEED 0xbb #define BLANK 0xa1 #define SCSI_CMD_BUF_SIZE 16 -#define SCSI_SENSE_BUF_SIZE 32 +#define SCSI_SENSE_BUF_SIZE 96 #define SG_ERR_DRIVER_TIMEOUT 0x06 #define SG_ERR_DRIVER_SENSE 0x08 @@ -75,6 +77,7 @@ { SCSIRequest *requests; BlockDriverState *bdrv; + int type; int blocksize; int lun; scsi_completionfn completion; @@ -163,7 +166,7 @@ } else if ((s->driver_status & SG_ERR_DRIVER_SENSE) == 0) sense = NO_SENSE; else - sense = s->sensebuf[2] & 0x0f; + sense = s->sensebuf[2]; } DPRINTF("Command complete 0x%p tag=0x%x sense=%d\n", r, r->tag, sense); @@ -273,10 +276,14 @@ if (r->cmd[0] == REQUEST_SENSE && s->driver_status & SG_ERR_DRIVER_SENSE) { - memcpy(r->buf, s->sensebuf, 16); + int len = MIN(r->len, SCSI_SENSE_BUF_SIZE); + memcpy(r->buf, s->sensebuf, len); r->io_header.driver_status = 0; r->len = -1; - s->completion(s->opaque, SCSI_REASON_DATA, r->tag, 16); + DPRINTF("Sense: %d %d %d %d %d %d %d %d\n", + r->buf[0], r->buf[1], r->buf[2], r->buf[3], + r->buf[4], r->buf[5], r->buf[6], r->buf[7]); + s->completion(s->opaque, SCSI_REASON_DATA, r->tag, len); return; } @@ -434,6 +441,32 @@ return 0; } +static int scsi_stream_length(uint8_t *cmd, int blocksize, int *cmdlen, uint32_t *len) +{ + switch(cmd[0]) { + /* stream commands */ + case READ_6: + case READ_REVERSE: + case RECOVER_BUFFERED_DATA: + case WRITE_6: + *cmdlen = 6; + *len = cmd[4] | (cmd[3] << 8) | (cmd[2] << 16); + if (cmd[1] & 0x01) /* fixed */ + *len *= blocksize; + break; + case REWIND: + case START_STOP: + *cmdlen = 6; + *len = 0; + cmd[1] = 0x01; /* force IMMED, otherwise qemu waits end of command */ + break; + /* generic commands */ + default: + return scsi_length(cmd, blocksize, cmdlen, len); + } + return 0; +} + static int is_write(int command) { switch (command) { @@ -495,9 +528,16 @@ return 0; } - if (scsi_length(cmd, s->blocksize, &cmdlen, &len) == -1) { - BADF("Unsupported command length, command %x\n", cmd[0]); - return 0; + if (s->type == TYPE_TAPE) { + if (scsi_stream_length(cmd, s->blocksize, &cmdlen, &len) == -1) { + BADF("Unsupported command length, command %x\n", cmd[0]); + return 0; + } + } else { + if (scsi_length(cmd, s->blocksize, &cmdlen, &len) == -1) { + BADF("Unsupported command length, command %x\n", cmd[0]); + return 0; + } } DPRINTF("Command: lun=%d tag=0x%x data=0x%02x len %d\n", lun, tag, @@ -633,12 +673,17 @@ s->completion = completion; s->opaque = opaque; s->lun = scsiid.lun; + s->type = scsiid.scsi_type; s->blocksize = get_blocksize(s->bdrv); s->driver_status = 0; memset(s->sensebuf, 0, sizeof(s->sensebuf)); /* removable media returns 0 if not present */ - if (s->blocksize <= 0) - s->blocksize = 2048; + if (s->blocksize <= 0) { + if (s->type == TYPE_ROM || s->type == TYPE_WORM) + s->blocksize = 2048; + else + s->blocksize = 512; + } /* define function to manage device */