From patchwork Sun Dec 17 05:29:44 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 849591 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="rXEkQrk7"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yzt6Y2W7zz9sBW for ; Sun, 17 Dec 2017 16:30:27 +1100 (AEDT) Received: from localhost ([::1]:53172 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eQRWq-00076z-B3 for incoming@patchwork.ozlabs.org; Sun, 17 Dec 2017 00:30:24 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40297) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eQRWN-00076n-Fg for qemu-devel@nongnu.org; Sun, 17 Dec 2017 00:29:56 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eQRWK-0004vT-BS for qemu-devel@nongnu.org; Sun, 17 Dec 2017 00:29:55 -0500 Received: from mail-wr0-x242.google.com ([2a00:1450:400c:c0c::242]:38703) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eQRWK-0004v2-0M for qemu-devel@nongnu.org; Sun, 17 Dec 2017 00:29:52 -0500 Received: by mail-wr0-x242.google.com with SMTP id o2so11275883wro.5 for ; Sat, 16 Dec 2017 21:29:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id; bh=wqiroYIzc326aVx3AUaevLaTJaex+XqNbefRYKNqo/Y=; b=rXEkQrk7fMvGC7HO0yWF0pvb8jwoaw2atoSdWSWMYbo2E994m1MH4kUsCvWtY3eBpz 8npCHzs88DqcLXIZbxbOmuRceDgq2Uf4Q8iGYKIr+CfiIaYuD+Qp9as1Kyby2mm8Cx71 CDWv8N8HRZp6rfQaDJvlQC30uQiw/OwB6oJsgMapLrmkjEPA6np75MYZ4kfCe7E4GjQO pBBeX0+kG9GQqU4rF27zlWdD79EozJX+w61CoPh7203w9msQ1PFGJnL1ye8bw4/064tX zeUmOF+ydWxDE4AwQA0pCp/Mijay77SgyrUA3ELFqyxqDwCNhkHMj2JzeyHE6UmCv4z5 fmRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id; bh=wqiroYIzc326aVx3AUaevLaTJaex+XqNbefRYKNqo/Y=; b=XZfag492Gw1dgMFG+Ljh6k3VGMOxnCvTOIAGZLOqwRX3BO/qnovrMOd8nMm6CI7KWN g7uJYY5/sGW8DJ2Fnjvj0n7tB+VU1fPj+kx2WdjMufMfOgJ+C3dFMExxzmmD6iHixC/0 mhhQyT3UEgzEEr1OioXXuI+yO7MsryDtrpe6vFD8C6fUj74LiBVVgeRVwVTqzAQZUWJe HlHa4bFlMixgVKN7gPHbk32NLEFWFU9zZ4fRRsfTH1IN9NkcN2YOUfGcefDv9neGN1vF jlusI+oXL9c6JXCGI2G/t5vUh++6T1EworzPSqcHsCoVyRpCHOvcXnQa6c5rgrwCAuNv ik8w== X-Gm-Message-State: AKGB3mKf9got0g6UZ1Jmu4WwkQO3xEZdB8EgIzn3XvTqREMM1azSJOVj 3cQ6yFMB0lo5Ou7nqkVGn0QKc8PZ X-Google-Smtp-Source: ACJfBovofe6dusqhtyqk3mst0frZjcgQMi1OiufNbBNZpf/LVEyP/ZhloNs8hF32NN6sDSWEWfLRyQ== X-Received: by 10.223.152.12 with SMTP id v12mr131198wrb.234.1513488590393; Sat, 16 Dec 2017 21:29:50 -0800 (PST) Received: from donizetti.lan (dynamic-adsl-78-12-251-125.clienti.tiscali.it. [78.12.251.125]) by smtp.gmail.com with ESMTPSA id v18sm6169790wrv.37.2017.12.16.21.29.48 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 16 Dec 2017 21:29:49 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Sun, 17 Dec 2017 06:29:44 +0100 Message-Id: <20171217052945.12782-1-pbonzini@redhat.com> X-Mailer: git-send-email 2.14.3 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c0c::242 Subject: [Qemu-devel] [PATCH] scsi: provide general-purpose functions to manage sense data X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: dgilbert@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Extract the common parts of scsi_sense_buf_to_errno, scsi_convert_sense and scsi_target_send_command's REQUEST SENSE handling into two new functions scsi_parse_sense_buf and scsi_build_sense_buf. Fix a bug in scsi_target_send_command along the way; the length was written in buf[10] rather than buf[7]. Reported-by: Dr. David Alan Gilbert Fixes: b07fbce634 ("scsi-bus: correct responses for INQUIRY and REQUEST SENSE") Signed-off-by: Paolo Bonzini Reviewed-by: Dr. David Alan Gilbert --- hw/scsi/scsi-bus.c | 16 +----- include/scsi/utils.h | 3 ++ scsi/utils.c | 139 +++++++++++++++++++++++++-------------------------- 3 files changed, 72 insertions(+), 86 deletions(-) diff --git a/hw/scsi/scsi-bus.c b/hw/scsi/scsi-bus.c index 977f7bce1f..965becf31f 100644 --- a/hw/scsi/scsi-bus.c +++ b/hw/scsi/scsi-bus.c @@ -540,20 +540,8 @@ static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf) if (req->lun != 0) { const struct SCSISense sense = SENSE_CODE(LUN_NOT_SUPPORTED); - if (fixed_sense) { - r->buf[0] = 0x70; - r->buf[2] = sense.key; - r->buf[10] = 10; - r->buf[12] = sense.asc; - r->buf[13] = sense.ascq; - r->len = MIN(req->cmd.xfer, SCSI_SENSE_LEN); - } else { - r->buf[0] = 0x72; - r->buf[1] = sense.key; - r->buf[2] = sense.asc; - r->buf[3] = sense.ascq; - r->len = 8; - } + r->len = scsi_build_sense_buf(r->buf, req->cmd.xfer, + sense, fixed_sense); } else { r->len = scsi_device_get_sense(r->req.dev, r->buf, MIN(req->cmd.xfer, r->buf_len), diff --git a/include/scsi/utils.h b/include/scsi/utils.h index eb07e474ee..4b705f5e0f 100644 --- a/include/scsi/utils.h +++ b/include/scsi/utils.h @@ -31,6 +31,9 @@ typedef struct SCSISense { } SCSISense; int scsi_build_sense(uint8_t *buf, SCSISense sense); +SCSISense scsi_parse_sense_buf(const uint8_t *in_buf, int in_len); +int scsi_build_sense_buf(uint8_t *buf, size_t max_size, SCSISense sense, + bool fixed_sense); /* * Predefined sense codes diff --git a/scsi/utils.c b/scsi/utils.c index e4182a9b09..61bc1a8e2b 100644 --- a/scsi/utils.c +++ b/scsi/utils.c @@ -96,15 +96,60 @@ int scsi_cdb_length(uint8_t *buf) return cdb_len; } +SCSISense scsi_parse_sense_buf(const uint8_t *in_buf, int in_len) +{ + bool fixed_in; + SCSISense sense; + + assert(in_len > 0); + fixed_in = (in_buf[0] & 2) == 0; + if (fixed_in) { + if (in_len < 14) { + return SENSE_CODE(IO_ERROR); + } + sense.key = in_buf[2]; + sense.asc = in_buf[12]; + sense.ascq = in_buf[13]; + } else { + if (in_len < 4) { + return SENSE_CODE(IO_ERROR); + } + sense.key = in_buf[1]; + sense.asc = in_buf[2]; + sense.ascq = in_buf[3]; + } + + return sense; +} + +int scsi_build_sense_buf(uint8_t *out_buf, size_t size, SCSISense sense, + bool fixed_sense) +{ + int len; + uint8_t buf[SCSI_SENSE_LEN] = { 0 }; + + if (fixed_sense) { + buf[0] = 0x70; + buf[2] = sense.key; + buf[7] = 10; + buf[12] = sense.asc; + buf[13] = sense.ascq; + len = 18; + } else { + buf[0] = 0x72; + buf[1] = sense.key; + buf[2] = sense.asc; + buf[3] = sense.ascq; + len = 8; + } + len = MIN(len, size); + memcpy(out_buf, buf, len); + return len; +} + int scsi_build_sense(uint8_t *buf, SCSISense sense) { - memset(buf, 0, 18); - buf[0] = 0x70; - buf[2] = sense.key; - buf[7] = 10; - buf[12] = sense.asc; - buf[13] = sense.ascq; - return 18; + return scsi_build_sense_buf(buf, SCSI_SENSE_LEN, sense, true); } /* @@ -274,52 +319,21 @@ const struct SCSISense sense_code_SPACE_ALLOC_FAILED = { int scsi_convert_sense(uint8_t *in_buf, int in_len, uint8_t *buf, int len, bool fixed) { - bool fixed_in; SCSISense sense; - if (!fixed && len < 8) { - return 0; - } - - if (in_len == 0) { - sense.key = NO_SENSE; - sense.asc = 0; - sense.ascq = 0; - } else { - fixed_in = (in_buf[0] & 2) == 0; - - if (fixed == fixed_in) { - memcpy(buf, in_buf, MIN(len, in_len)); - return MIN(len, in_len); - } + bool fixed_in; - if (fixed_in) { - sense.key = in_buf[2]; - sense.asc = in_buf[12]; - sense.ascq = in_buf[13]; - } else { - sense.key = in_buf[1]; - sense.asc = in_buf[2]; - sense.ascq = in_buf[3]; - } + fixed_in = (in_buf[0] & 2) == 0; + if (in_len && fixed == fixed_in) { + memcpy(buf, in_buf, MIN(len, in_len)); + return MIN(len, in_len); } - memset(buf, 0, len); - if (fixed) { - /* Return fixed format sense buffer */ - buf[0] = 0x70; - buf[2] = sense.key; - buf[7] = 10; - buf[12] = sense.asc; - buf[13] = sense.ascq; - return MIN(len, SCSI_SENSE_LEN); + if (in_len == 0) { + sense = SENSE_CODE(NO_SENSE); } else { - /* Return descriptor format sense buffer */ - buf[0] = 0x72; - buf[1] = sense.key; - buf[2] = sense.asc; - buf[3] = sense.ascq; - return 8; + sense = scsi_parse_sense_buf(in_buf, in_len); } + return scsi_build_sense_buf(buf, len, sense, fixed); } int scsi_sense_to_errno(int key, int asc, int ascq) @@ -366,34 +380,15 @@ int scsi_sense_to_errno(int key, int asc, int ascq) } } -int scsi_sense_buf_to_errno(const uint8_t *sense, size_t sense_size) +int scsi_sense_buf_to_errno(const uint8_t *in_buf, size_t in_len) { - int key, asc, ascq; - if (sense_size < 1) { - return EIO; - } - switch (sense[0]) { - case 0x70: /* Fixed format sense data. */ - if (sense_size < 14) { - return EIO; - } - key = sense[2] & 0xF; - asc = sense[12]; - ascq = sense[13]; - break; - case 0x72: /* Descriptor format sense data. */ - if (sense_size < 4) { - return EIO; - } - key = sense[1] & 0xF; - asc = sense[2]; - ascq = sense[3]; - break; - default: + SCSISense sense; + if (in_len < 1) { return EIO; - break; } - return scsi_sense_to_errno(key, asc, ascq); + + sense = scsi_parse_sense_buf(in_buf, in_len); + return scsi_sense_to_errno(sense.key, sense.asc, sense.ascq); } const char *scsi_command_name(uint8_t cmd)