From nobody Mon Feb 9 13:59:09 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1513792402866612.8480265144309; Wed, 20 Dec 2017 09:53:22 -0800 (PST) Received: from localhost ([::1]:60768 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eRiYO-0002VK-CO for importer@patchew.org; Wed, 20 Dec 2017 12:53:16 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56550) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eRhyW-0004o9-8z for qemu-devel@nongnu.org; Wed, 20 Dec 2017 12:16:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eRhyU-0007Jz-RY for qemu-devel@nongnu.org; Wed, 20 Dec 2017 12:16:12 -0500 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]:40601) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eRhyU-0007Je-GW for qemu-devel@nongnu.org; Wed, 20 Dec 2017 12:16:10 -0500 Received: by mail-wm0-x243.google.com with SMTP id f206so11070121wmf.5 for ; Wed, 20 Dec 2017 09:16:10 -0800 (PST) Received: from 640k.lan (dynamic-adsl-78-12-251-125.clienti.tiscali.it. [78.12.251.125]) by smtp.gmail.com with ESMTPSA id f125sm2751101wme.45.2017.12.20.09.16.07 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 20 Dec 2017 09:16:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:subject:date:message-id:in-reply-to:references; bh=BQHpWvPRfo1mCA/LAkjZXKOCPOk8gcn6d+p/ZGAU/vI=; b=QqgKy2eh7bJEDnyRkYzicfgg/dWyzYqFSCdhVnxuZQcK4VHGUILu1o2l7tLfwuG/1B +1qZoWyjQvpjDnda2G74WdNoOyB7jeZYZD2ehwDAcU/rEKPb4UIN2zK8ITgzr9ZZTYyW 5/N4/vaRkXb4xTSA3y9FsqSd9a60yaTtcny1VK6Yr/TkZtdsz2nqnY42ceR0itEi5Jc/ V86d3PZdVGP29fYNyMqCKcf5fnEyGsd3AUWbHSJ9bL6HmlOckUZiKXUTC/pm/OyUXDpj B1k/Kd2mbRzl7cASLCwMHVNQfNQyJHFP8ilOjp6YBOau7HQtvcoOhkfkVUT2jU/9OSB7 ZJOg== 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:subject:date:message-id :in-reply-to:references; bh=BQHpWvPRfo1mCA/LAkjZXKOCPOk8gcn6d+p/ZGAU/vI=; b=EV4kr4ruG8ycQFuF6b68megsCATXaIziTUddGAd0fD7k7yZ7tij2YJcDtWpTCgmauc Mt1PCBG66eH56WSy1ncKyaf5jp0Bd/73aND1QYkz8kV1lr4cMdv4BDeGX2RP0ywqmYVo xwwwCWMkuE6TMJmN0oYbZgPIZ2FnqrknU8oQVM8+G9M8Wcl+VRYlu3m4ho08lsgYy6Uw qp6/h0I6rUugh6OoUkg9b+u1bOC4jACx1GKQy9+P+2HBvNeD2nNUtbboRyl9t8y0kogp Rak79oT9ZKSvci/Y6SRMMm89dWtNX4Dwn3L5a81OaJFmx2UdFFoy2jDZ6uPwLqLNoyEa wayw== X-Gm-Message-State: AKGB3mKSpqbgvQZWi6yz9QMXKDzKVWOxfyX2HqnI5GkcDsU/eCM1VoLh tG6Yd97aVx9g1kxJmIbxKTkBJ4/O X-Google-Smtp-Source: ACJfBov8XJlfjmoSNCKBizrTjEA8O10iJTtOdJe/B5lvrkgYO9CMJENP4Myp/D6q1eOgQvroGtRpgw== X-Received: by 10.28.1.14 with SMTP id 14mr7641132wmb.51.1513790169112; Wed, 20 Dec 2017 09:16:09 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Wed, 20 Dec 2017 18:14:44 +0100 Message-Id: <1513790098-9815-33-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1513790098-9815-1-git-send-email-pbonzini@redhat.com> References: <1513790098-9815-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::243 Subject: [Qemu-devel] [PULL 32/46] 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Dr. David Alan Gilbert Fixes: b07fbce634 ("scsi-bus: correct responses for INQUIRY and REQUEST SEN= SE") Signed-off-by: Paolo Bonzini --- 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 977f7bc..965becf 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 *r= eq, uint8_t *buf) if (req->lun !=3D 0) { const struct SCSISense sense =3D SENSE_CODE(LUN_NOT_SUPPORTED); =20 - if (fixed_sense) { - r->buf[0] =3D 0x70; - r->buf[2] =3D sense.key; - r->buf[10] =3D 10; - r->buf[12] =3D sense.asc; - r->buf[13] =3D sense.ascq; - r->len =3D MIN(req->cmd.xfer, SCSI_SENSE_LEN); - } else { - r->buf[0] =3D 0x72; - r->buf[1] =3D sense.key; - r->buf[2] =3D sense.asc; - r->buf[3] =3D sense.ascq; - r->len =3D 8; - } + r->len =3D scsi_build_sense_buf(r->buf, req->cmd.xfer, + sense, fixed_sense); } else { r->len =3D 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 eb07e47..4b705f5 100644 --- a/include/scsi/utils.h +++ b/include/scsi/utils.h @@ -31,6 +31,9 @@ typedef struct SCSISense { } SCSISense; =20 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); =20 /* * Predefined sense codes diff --git a/scsi/utils.c b/scsi/utils.c index e4182a9..61bc1a8 100644 --- a/scsi/utils.c +++ b/scsi/utils.c @@ -96,15 +96,60 @@ int scsi_cdb_length(uint8_t *buf) return cdb_len; } =20 +SCSISense scsi_parse_sense_buf(const uint8_t *in_buf, int in_len) +{ + bool fixed_in; + SCSISense sense; + + assert(in_len > 0); + fixed_in =3D (in_buf[0] & 2) =3D=3D 0; + if (fixed_in) { + if (in_len < 14) { + return SENSE_CODE(IO_ERROR); + } + sense.key =3D in_buf[2]; + sense.asc =3D in_buf[12]; + sense.ascq =3D in_buf[13]; + } else { + if (in_len < 4) { + return SENSE_CODE(IO_ERROR); + } + sense.key =3D in_buf[1]; + sense.asc =3D in_buf[2]; + sense.ascq =3D 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] =3D { 0 }; + + if (fixed_sense) { + buf[0] =3D 0x70; + buf[2] =3D sense.key; + buf[7] =3D 10; + buf[12] =3D sense.asc; + buf[13] =3D sense.ascq; + len =3D 18; + } else { + buf[0] =3D 0x72; + buf[1] =3D sense.key; + buf[2] =3D sense.asc; + buf[3] =3D sense.ascq; + len =3D 8; + } + len =3D 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] =3D 0x70; - buf[2] =3D sense.key; - buf[7] =3D 10; - buf[12] =3D sense.asc; - buf[13] =3D sense.ascq; - return 18; + return scsi_build_sense_buf(buf, SCSI_SENSE_LEN, sense, true); } =20 /* @@ -274,52 +319,21 @@ const struct SCSISense sense_code_SPACE_ALLOC_FAILED = =3D { 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 =3D=3D 0) { - sense.key =3D NO_SENSE; - sense.asc =3D 0; - sense.ascq =3D 0; - } else { - fixed_in =3D (in_buf[0] & 2) =3D=3D 0; - - if (fixed =3D=3D fixed_in) { - memcpy(buf, in_buf, MIN(len, in_len)); - return MIN(len, in_len); - } + bool fixed_in; =20 - if (fixed_in) { - sense.key =3D in_buf[2]; - sense.asc =3D in_buf[12]; - sense.ascq =3D in_buf[13]; - } else { - sense.key =3D in_buf[1]; - sense.asc =3D in_buf[2]; - sense.ascq =3D in_buf[3]; - } + fixed_in =3D (in_buf[0] & 2) =3D=3D 0; + if (in_len && fixed =3D=3D fixed_in) { + memcpy(buf, in_buf, MIN(len, in_len)); + return MIN(len, in_len); } =20 - memset(buf, 0, len); - if (fixed) { - /* Return fixed format sense buffer */ - buf[0] =3D 0x70; - buf[2] =3D sense.key; - buf[7] =3D 10; - buf[12] =3D sense.asc; - buf[13] =3D sense.ascq; - return MIN(len, SCSI_SENSE_LEN); + if (in_len =3D=3D 0) { + sense =3D SENSE_CODE(NO_SENSE); } else { - /* Return descriptor format sense buffer */ - buf[0] =3D 0x72; - buf[1] =3D sense.key; - buf[2] =3D sense.asc; - buf[3] =3D sense.ascq; - return 8; + sense =3D scsi_parse_sense_buf(in_buf, in_len); } + return scsi_build_sense_buf(buf, len, sense, fixed); } =20 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) } } =20 -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 =3D sense[2] & 0xF; - asc =3D sense[12]; - ascq =3D sense[13]; - break; - case 0x72: /* Descriptor format sense data. */ - if (sense_size < 4) { - return EIO; - } - key =3D sense[1] & 0xF; - asc =3D sense[2]; - ascq =3D sense[3]; - break; - default: + SCSISense sense; + if (in_len < 1) { return EIO; - break; } - return scsi_sense_to_errno(key, asc, ascq); + + sense =3D scsi_parse_sense_buf(in_buf, in_len); + return scsi_sense_to_errno(sense.key, sense.asc, sense.ascq); } =20 const char *scsi_command_name(uint8_t cmd) --=20 1.8.3.1