From patchwork Fri Aug 18 14:15:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fam Zheng X-Patchwork-Id: 9909137 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D14BC602C8 for ; Fri, 18 Aug 2017 14:16:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C22FF28C3B for ; Fri, 18 Aug 2017 14:16:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B708E28C40; Fri, 18 Aug 2017 14:16:42 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 1936F28C3E for ; Fri, 18 Aug 2017 14:16:42 +0000 (UTC) Received: from localhost ([::1]:57018 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dii4n-0006AB-9d for patchwork-qemu-devel@patchwork.kernel.org; Fri, 18 Aug 2017 10:16:41 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54730) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dii3W-00066l-8e for qemu-devel@nongnu.org; Fri, 18 Aug 2017 10:15:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dii3U-0005kg-GR for qemu-devel@nongnu.org; Fri, 18 Aug 2017 10:15:22 -0400 Received: from mx1.redhat.com ([209.132.183.28]:46358) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dii3U-0005kG-75 for qemu-devel@nongnu.org; Fri, 18 Aug 2017 10:15:20 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4E63E267D5 for ; Fri, 18 Aug 2017 14:15:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 4E63E267D5 Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=famz@redhat.com Received: from lemon.redhat.com (ovpn-12-120.pek2.redhat.com [10.72.12.120]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8C3056F9F9; Fri, 18 Aug 2017 14:15:17 +0000 (UTC) From: Fam Zheng To: qemu-devel@nongnu.org Date: Fri, 18 Aug 2017 22:15:10 +0800 Message-Id: <20170818141512.15205-2-famz@redhat.com> In-Reply-To: <20170818141512.15205-1-famz@redhat.com> References: <20170818141512.15205-1-famz@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Fri, 18 Aug 2017 14:15:19 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 1/3] scsi: Refactor scsi sense interpreting code 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: Paolo Bonzini Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP So that it can be reused outside of iscsi.c. Also update MAINTAINERS to include the new files in SCSI section. Signed-off-by: Fam Zheng --- MAINTAINERS | 2 ++ block/iscsi.c | 45 ++++----------------------------------------- include/scsi/scsi.h | 19 +++++++++++++++++++ util/Makefile.objs | 1 + util/scsi.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 41 deletions(-) create mode 100644 include/scsi/scsi.h create mode 100644 util/scsi.c diff --git a/MAINTAINERS b/MAINTAINERS index ccee28b12d..2a4e5036ae 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -969,7 +969,9 @@ SCSI M: Paolo Bonzini S: Supported F: include/hw/scsi/* +F: include/scsi/* F: hw/scsi/* +F: util/scsi* F: tests/virtio-scsi-test.c T: git git://github.com/bonzini/qemu.git scsi-next diff --git a/block/iscsi.c b/block/iscsi.c index d557c99668..4bed63cd6d 100644 --- a/block/iscsi.c +++ b/block/iscsi.c @@ -40,6 +40,7 @@ #include "qmp-commands.h" #include "qapi/qmp/qstring.h" #include "crypto/secret.h" +#include "scsi/scsi.h" #include #include @@ -209,47 +210,9 @@ static inline unsigned exp_random(double mean) static int iscsi_translate_sense(struct scsi_sense *sense) { - int ret; - - switch (sense->key) { - case SCSI_SENSE_NOT_READY: - return -EBUSY; - case SCSI_SENSE_DATA_PROTECTION: - return -EACCES; - case SCSI_SENSE_COMMAND_ABORTED: - return -ECANCELED; - case SCSI_SENSE_ILLEGAL_REQUEST: - /* Parse ASCQ */ - break; - default: - return -EIO; - } - switch (sense->ascq) { - case SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR: - case SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE: - case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB: - case SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST: - ret = -EINVAL; - break; - case SCSI_SENSE_ASCQ_LBA_OUT_OF_RANGE: - ret = -ENOSPC; - break; - case SCSI_SENSE_ASCQ_LOGICAL_UNIT_NOT_SUPPORTED: - ret = -ENOTSUP; - break; - case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT: - case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_CLOSED: - case SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_OPEN: - ret = -ENOMEDIUM; - break; - case SCSI_SENSE_ASCQ_WRITE_PROTECTED: - ret = -EACCES; - break; - default: - ret = -EIO; - break; - } - return ret; + return - scsi_sense_to_errno(sense->key, + (sense->ascq & 0xFF00) >> 8, + sense->ascq & 0xFF); } /* Called (via iscsi_service) with QemuMutex held. */ diff --git a/include/scsi/scsi.h b/include/scsi/scsi.h new file mode 100644 index 0000000000..f894ace4bf --- /dev/null +++ b/include/scsi/scsi.h @@ -0,0 +1,19 @@ +/* + * SCSI helpers + * + * Copyright 2017 Red Hat, Inc. + * + * Authors: + * Fam Zheng + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + */ +#ifndef QEMU_SCSI_H +#define QEMU_SCSI_H + +int scsi_sense_to_errno(int key, int asc, int ascq); + +#endif diff --git a/util/Makefile.objs b/util/Makefile.objs index 50a55ecc75..c9e6c493d3 100644 --- a/util/Makefile.objs +++ b/util/Makefile.objs @@ -45,3 +45,4 @@ util-obj-y += qht.o util-obj-y += range.o util-obj-y += stats64.o util-obj-y += systemd.o +util-obj-y += scsi.o diff --git a/util/scsi.c b/util/scsi.c new file mode 100644 index 0000000000..92ca436cd0 --- /dev/null +++ b/util/scsi.c @@ -0,0 +1,52 @@ +/* + * SCSI helpers + * + * Copyright 2017 Red Hat, Inc. + * + * Authors: + * Fam Zheng + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + */ + +#include "qemu/osdep.h" +#include "scsi/scsi.h" + +int scsi_sense_to_errno(int key, int asc, int ascq) +{ + switch (key) { + case 0x02: /* SCSI_SENSE_NOT_READY */ + return EBUSY; + case 0x07: /* SCSI_SENSE_DATA_PROTECTION */ + return EACCES; + case 0x0b: /* SCSI_SENSE_COMMAND_ABORTED */ + return ECANCELED; + case 0x05: /* SCSI_SENSE_ILLEGAL_REQUEST */ + /* Parse ASCQ */ + break; + default: + return EIO; + } + switch ((asc << 8) | ascq) { + case 0x1a00: /* SCSI_SENSE_ASCQ_PARAMETER_LIST_LENGTH_ERROR */ + case 0x2000: /* SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE */ + case 0x2400: /* SCSI_SENSE_ASCQ_INVALID_FIELD_IN_CDB */ + case 0x2600: /* SCSI_SENSE_ASCQ_INVALID_FIELD_IN_PARAMETER_LIST */ + return EINVAL; + case 0x2100: /* SCSI_SENSE_ASCQ_LBA_OUT_OF_RANGE */ + return ENOSPC; + case 0x2500: /* SCSI_SENSE_ASCQ_LOGICAL_UNIT_NOT_SUPPORTED */ + return ENOTSUP; + case 0x3a00: /* SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT */ + case 0x3a01: /* SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_CLOSED */ + case 0x3a02: /* SCSI_SENSE_ASCQ_MEDIUM_NOT_PRESENT_TRAY_OPEN */ + return ENOMEDIUM; + case 0x2700: /* SCSI_SENSE_ASCQ_WRITE_PROTECTED */ + return EACCES; + default: + return EIO; + } +}