提交 afa46c46 编写于 作者: P Paolo Bonzini 提交者: Anthony Liguori

scsi: move request parsing to common code

Also introduce the first occurrence of "independent" SCSIReqOps,
to handle invalid commands in common code.
Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
Signed-off-by: NAnthony Liguori <aliguori@us.ibm.com>
上级 87dcd1b2
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "trace.h" #include "trace.h"
static char *scsibus_get_fw_dev_path(DeviceState *dev); static char *scsibus_get_fw_dev_path(DeviceState *dev);
static int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf);
static int scsi_build_sense(uint8_t *in_buf, int in_len, static int scsi_build_sense(uint8_t *in_buf, int in_len,
uint8_t *buf, int len, bool fixed); uint8_t *buf, int len, bool fixed);
...@@ -134,6 +135,20 @@ int scsi_bus_legacy_handle_cmdline(SCSIBus *bus) ...@@ -134,6 +135,20 @@ int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
return res; return res;
} }
/* SCSIReqOps implementation for invalid commands. */
static int32_t scsi_invalid_command(SCSIRequest *req, uint8_t *buf)
{
scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
scsi_req_complete(req, CHECK_CONDITION);
return 0;
}
struct SCSIReqOps reqops_invalid_opcode = {
.size = sizeof(SCSIRequest),
.send_command = scsi_invalid_command
};
SCSIRequest *scsi_req_alloc(SCSIReqOps *reqops, SCSIDevice *d, uint32_t tag, SCSIRequest *scsi_req_alloc(SCSIReqOps *reqops, SCSIDevice *d, uint32_t tag,
uint32_t lun, void *hba_private) uint32_t lun, void *hba_private)
{ {
...@@ -157,8 +172,22 @@ SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun, ...@@ -157,8 +172,22 @@ SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
uint8_t *buf, void *hba_private) uint8_t *buf, void *hba_private)
{ {
SCSIRequest *req; SCSIRequest *req;
req = d->info->alloc_req(d, tag, lun, hba_private); SCSICommand cmd;
memcpy(req->cmd.buf, buf, 16);
if (scsi_req_parse(&cmd, d, buf) != 0) {
trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]);
req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private);
} else {
trace_scsi_req_parsed(d->id, lun, tag, buf[0],
cmd.mode, cmd.xfer);
if (req->cmd.lba != -1) {
trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0],
cmd.lba);
}
req = d->info->alloc_req(d, tag, lun, hba_private);
}
req->cmd = cmd;
return req; return req;
} }
...@@ -424,27 +453,21 @@ static uint64_t scsi_cmd_lba(SCSICommand *cmd) ...@@ -424,27 +453,21 @@ static uint64_t scsi_cmd_lba(SCSICommand *cmd)
return lba; return lba;
} }
int scsi_req_parse(SCSIRequest *req, uint8_t *buf) int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
{ {
int rc; int rc;
if (req->dev->type == TYPE_TAPE) { if (dev->type == TYPE_TAPE) {
rc = scsi_req_stream_length(&req->cmd, req->dev, buf); rc = scsi_req_stream_length(cmd, dev, buf);
} else { } else {
rc = scsi_req_length(&req->cmd, req->dev, buf); rc = scsi_req_length(cmd, dev, buf);
} }
if (rc != 0) if (rc != 0)
return rc; return rc;
assert(buf == req->cmd.buf); memcpy(cmd->buf, buf, cmd->len);
scsi_cmd_xfer_mode(&req->cmd); scsi_cmd_xfer_mode(cmd);
req->cmd.lba = scsi_cmd_lba(&req->cmd); cmd->lba = scsi_cmd_lba(cmd);
trace_scsi_req_parsed(req->dev->id, req->lun, req->tag, buf[0],
req->cmd.mode, req->cmd.xfer);
if (req->cmd.lba != -1) {
trace_scsi_req_parsed_lba(req->dev->id, req->lun, req->tag, buf[0],
req->cmd.lba);
}
return 0; return 0;
} }
......
...@@ -964,11 +964,6 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf) ...@@ -964,11 +964,6 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *buf)
outbuf = (uint8_t *)r->iov.iov_base; outbuf = (uint8_t *)r->iov.iov_base;
DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", req->lun, req->tag, buf[0]); DPRINTF("Command: lun=%d tag=0x%x data=0x%02x", req->lun, req->tag, buf[0]);
if (scsi_req_parse(&r->req, buf) != 0) {
BADF("Unsupported command length, command %x\n", command);
scsi_check_condition(r, SENSE_CODE(INVALID_OPCODE));
return 0;
}
#ifdef DEBUG_SCSI #ifdef DEBUG_SCSI
{ {
int i; int i;
......
...@@ -84,10 +84,6 @@ static void scsi_command_complete(void *opaque, int ret) ...@@ -84,10 +84,6 @@ static void scsi_command_complete(void *opaque, int ret)
case -EDOM: case -EDOM:
status = TASK_SET_FULL; status = TASK_SET_FULL;
break; break;
case -EINVAL:
status = CHECK_CONDITION;
scsi_req_build_sense(&r->req, SENSE_CODE(INVALID_FIELD));
break;
case -ENOMEM: case -ENOMEM:
status = CHECK_CONDITION; status = CHECK_CONDITION;
scsi_req_build_sense(&r->req, SENSE_CODE(TARGET_FAILURE)); scsi_req_build_sense(&r->req, SENSE_CODE(TARGET_FAILURE));
...@@ -298,11 +294,6 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *cmd) ...@@ -298,11 +294,6 @@ static int32_t scsi_send_command(SCSIRequest *req, uint8_t *cmd)
return 0; return 0;
} }
if (-1 == scsi_req_parse(&r->req, cmd)) {
BADF("Unsupported command length, command %x\n", cmd[0]);
scsi_command_complete(r, -EINVAL);
return 0;
}
scsi_req_fixup(&r->req); scsi_req_fixup(&r->req);
DPRINTF("Command: lun=%d tag=0x%x len %zd data=0x%02x", lun, tag, DPRINTF("Command: lun=%d tag=0x%x len %zd data=0x%02x", lun, tag,
......
...@@ -165,7 +165,6 @@ SCSIRequest *scsi_req_ref(SCSIRequest *req); ...@@ -165,7 +165,6 @@ SCSIRequest *scsi_req_ref(SCSIRequest *req);
void scsi_req_unref(SCSIRequest *req); void scsi_req_unref(SCSIRequest *req);
void scsi_req_build_sense(SCSIRequest *req, SCSISense sense); void scsi_req_build_sense(SCSIRequest *req, SCSISense sense);
int scsi_req_parse(SCSIRequest *req, uint8_t *buf);
void scsi_req_print(SCSIRequest *req); void scsi_req_print(SCSIRequest *req);
void scsi_req_continue(SCSIRequest *req); void scsi_req_continue(SCSIRequest *req);
void scsi_req_data(SCSIRequest *req, int len); void scsi_req_data(SCSIRequest *req, int len);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册