Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp1911239imm; Tue, 22 May 2018 11:18:38 -0700 (PDT) X-Google-Smtp-Source: AB8JxZo94oQePfmsDdADvfPcp5g6rscNvZ3Cjxdqy37Kfbu7oYoOvgG0YXdLSNF0vinI4UWOqSV0 X-Received: by 2002:a17:902:31a4:: with SMTP id x33-v6mr25991077plb.355.1527013118048; Tue, 22 May 2018 11:18:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527013118; cv=none; d=google.com; s=arc-20160816; b=ULvuRrkf+KkK+gUeMPbYQZFnnczz3Fg+bscGh1nW38zEl97WxHu+H6uEQgjY1xoaoL LshdwBbSjAYkIxsBxRjKzDULzZ6Vk7A+mSSGZGzjqV4wWDfB6vm1MwxEtTdC+gVWtscX l7PohazgXRxib5KXcNDPBBiytrDJj4dUX9OdrteDfB4L9p+y3RBZbQICLVGY4gBwjyDo pPIxpoX9Nrol2x2wXDDU2/zn7OQlYOEN15kZ8PKFTZ0RC6aKN3z5d/pHmIH3c2/kU3QE zad5I6WKfdA3iJaJM0QZTL1MUM7Y5y+2rHjoaaTfIc6GmSYIpfkci/hIyS0JxdW3EwE9 bslg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=QHMcuxNQ9B/+A5bK5ntp1pFCctmjKymmOHnR3K+A2EY=; b=S2llhJtYNQu2PgR/tk1kkXDnHhY5F8fIXxuvWA5RUF9ECIlXTCgdmNEl8de2NpiBl5 7FMA5EBfaVOwRPmWHFh5yDXEekGcTHQmptX6awJwKjsp7349mrgiLuXCDZsvoq8wL2is YqeOm4aDs54jLGB2+eeIvAdLwdTU6N3tASFtI6zS3RiUljSJ8C6lf5Z43Xed7Y3+Cbqe im9kpVubzUCT7J1GpT4FM7hOVa0qfkxVxW3MvymUDYCojwY2LBE647RydLyTJcRM2fop mEu7Kg0isvTG1wk6dAMd+B/JQsv77+Gmm8X+TDsV2ObvGfK66nbtPMBgWYscx4F1nbeY TY6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=KMu4yJ9z; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i15-v6si611042pgr.566.2018.05.22.11.18.23; Tue, 22 May 2018 11:18:37 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=KMu4yJ9z; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752278AbeEVSQk (ORCPT + 99 others); Tue, 22 May 2018 14:16:40 -0400 Received: from mail-pg0-f65.google.com ([74.125.83.65]:43039 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751447AbeEVSPX (ORCPT ); Tue, 22 May 2018 14:15:23 -0400 Received: by mail-pg0-f65.google.com with SMTP id p8-v6so8206691pgq.10 for ; Tue, 22 May 2018 11:15:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=QHMcuxNQ9B/+A5bK5ntp1pFCctmjKymmOHnR3K+A2EY=; b=KMu4yJ9zmL9YdpgJX/lFYOFcHpi+cbGBlXCq0zWe2S10+/lEO0nc9LRfbQcLdfuSaQ u+n3JWC5DBLlZVG7GzQIhHqkEmZGMxvVAMl/TOjcxIMAFvK8YJPoFJfrwjLi8dTdPfZ5 FF9bvpNi2gVKd2JDL71SByqretqEnvoYu74rk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=QHMcuxNQ9B/+A5bK5ntp1pFCctmjKymmOHnR3K+A2EY=; b=YtDeANWdXWJdRxQtlJCsulk3J2m9R0aj8/asvguQ43DKY+XiKKzGENZS6na0tkIUd6 Zgq7m5uXCCtcteMjYfkwdZu6ZeBE4Cc1/Vx642B0JEr4+7PwhdgN7l+cKCPncSwH+kT4 CbAJXbKBKsxH9f+gpHmrLs2G1/ZQUBNrdfXqTUQZaIr7XSJ9lInmaiTneDO7cw78cLM3 RgQOSrjzg6C+viOhaccfD6fhAR3qhbDav/F5HGfnC8V4IfwLPgHpFD7ysH0ci5o7PuUK eipdMOEveLKRj69uGl6JXh9ZGhwnz1neovu3htlQwSD6CfOBQpjScinR60j0sgByJt57 NcPw== X-Gm-Message-State: ALKqPwf7e1ht1HApCv7PgJpgE66jufltl04Wg8Z1U5rJlYytJDyUHij/ MDEfbU8rXLKywooxWCptVgo9xQ== X-Received: by 2002:a62:f20d:: with SMTP id m13-v6mr25479849pfh.170.1527012923140; Tue, 22 May 2018 11:15:23 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id v15-v6sm33175218pfa.107.2018.05.22.11.15.18 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 22 May 2018 11:15:18 -0700 (PDT) From: Kees Cook To: Jens Axboe Cc: Kees Cook , "Martin K. Petersen" , James Bottomley , Tejun Heo , Borislav Petkov , "David S. Miller" , "Manoj N. Kumar" , "Matthew R. Ochs" , Uma Krishnan , linux-block@vger.kernel.org, linux-ide@vger.kernel.org, linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/6] block: Create scsi_sense.h for SCSI and ATAPI Date: Tue, 22 May 2018 11:15:09 -0700 Message-Id: <20180522181512.39316-4-keescook@chromium.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180522181512.39316-1-keescook@chromium.org> References: <20180522181512.39316-1-keescook@chromium.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Both SCSI and ATAPI share the sense header. In preparation for using the struct scsi_sense_hdr more widely, move this into a separate header and move the helper function to scsi_ioctl.c which is linked with CONFIG_IDE by way of CONFIG_BLK_SCSI_REQUEST. Signed-off-by: Kees Cook --- block/scsi_ioctl.c | 64 ++++++++++++++++++++++++++++++++++++++ drivers/scsi/scsi_common.c | 64 -------------------------------------- include/scsi/scsi_cmnd.h | 1 - include/scsi/scsi_common.h | 32 +------------------ include/scsi/scsi_sense.h | 44 ++++++++++++++++++++++++++ 5 files changed, 109 insertions(+), 96 deletions(-) create mode 100644 include/scsi/scsi_sense.h diff --git a/block/scsi_ioctl.c b/block/scsi_ioctl.c index 60b471f8621b..87ff3cc7a364 100644 --- a/block/scsi_ioctl.c +++ b/block/scsi_ioctl.c @@ -728,6 +728,70 @@ void scsi_req_init(struct scsi_request *req) } EXPORT_SYMBOL(scsi_req_init); +/** + * scsi_normalize_sense - normalize main elements from either fixed or + * descriptor sense data format into a common format. + * + * @sense_buffer: byte array containing sense data returned by device + * @sb_len: number of valid bytes in sense_buffer + * @sshdr: pointer to instance of structure that common + * elements are written to. + * + * Notes: + * The "main elements" from sense data are: response_code, sense_key, + * asc, ascq and additional_length (only for descriptor format). + * + * Typically this function can be called after a device has + * responded to a SCSI command with the CHECK_CONDITION status. + * + * Return value: + * true if valid sense data information found, else false; + */ +bool scsi_normalize_sense(const u8 *sense_buffer, int sb_len, + struct scsi_sense_hdr *sshdr) +{ + memset(sshdr, 0, sizeof(struct scsi_sense_hdr)); + + if (!sense_buffer || !sb_len) + return false; + + sshdr->response_code = (sense_buffer[0] & 0x7f); + + if (!scsi_sense_valid(sshdr)) + return false; + + if (sshdr->response_code >= 0x72) { + /* + * descriptor format + */ + if (sb_len > 1) + sshdr->sense_key = (sense_buffer[1] & 0xf); + if (sb_len > 2) + sshdr->asc = sense_buffer[2]; + if (sb_len > 3) + sshdr->ascq = sense_buffer[3]; + if (sb_len > 7) + sshdr->additional_length = sense_buffer[7]; + } else { + /* + * fixed format + */ + if (sb_len > 2) + sshdr->sense_key = (sense_buffer[2] & 0xf); + if (sb_len > 7) { + sb_len = (sb_len < (sense_buffer[7] + 8)) ? + sb_len : (sense_buffer[7] + 8); + if (sb_len > 12) + sshdr->asc = sense_buffer[12]; + if (sb_len > 13) + sshdr->ascq = sense_buffer[13]; + } + } + + return true; +} +EXPORT_SYMBOL(scsi_normalize_sense); + static int __init blk_scsi_ioctl_init(void) { blk_set_cmd_filter_defaults(&blk_default_cmd_filter); diff --git a/drivers/scsi/scsi_common.c b/drivers/scsi/scsi_common.c index 90349498f686..8621a07cb967 100644 --- a/drivers/scsi/scsi_common.c +++ b/drivers/scsi/scsi_common.c @@ -116,70 +116,6 @@ void int_to_scsilun(u64 lun, struct scsi_lun *scsilun) } EXPORT_SYMBOL(int_to_scsilun); -/** - * scsi_normalize_sense - normalize main elements from either fixed or - * descriptor sense data format into a common format. - * - * @sense_buffer: byte array containing sense data returned by device - * @sb_len: number of valid bytes in sense_buffer - * @sshdr: pointer to instance of structure that common - * elements are written to. - * - * Notes: - * The "main elements" from sense data are: response_code, sense_key, - * asc, ascq and additional_length (only for descriptor format). - * - * Typically this function can be called after a device has - * responded to a SCSI command with the CHECK_CONDITION status. - * - * Return value: - * true if valid sense data information found, else false; - */ -bool scsi_normalize_sense(const u8 *sense_buffer, int sb_len, - struct scsi_sense_hdr *sshdr) -{ - memset(sshdr, 0, sizeof(struct scsi_sense_hdr)); - - if (!sense_buffer || !sb_len) - return false; - - sshdr->response_code = (sense_buffer[0] & 0x7f); - - if (!scsi_sense_valid(sshdr)) - return false; - - if (sshdr->response_code >= 0x72) { - /* - * descriptor format - */ - if (sb_len > 1) - sshdr->sense_key = (sense_buffer[1] & 0xf); - if (sb_len > 2) - sshdr->asc = sense_buffer[2]; - if (sb_len > 3) - sshdr->ascq = sense_buffer[3]; - if (sb_len > 7) - sshdr->additional_length = sense_buffer[7]; - } else { - /* - * fixed format - */ - if (sb_len > 2) - sshdr->sense_key = (sense_buffer[2] & 0xf); - if (sb_len > 7) { - sb_len = (sb_len < (sense_buffer[7] + 8)) ? - sb_len : (sense_buffer[7] + 8); - if (sb_len > 12) - sshdr->asc = sense_buffer[12]; - if (sb_len > 13) - sshdr->ascq = sense_buffer[13]; - } - } - - return true; -} -EXPORT_SYMBOL(scsi_normalize_sense); - /** * scsi_sense_desc_find - search for a given descriptor type in descriptor sense data format. * @sense_buffer: byte array of descriptor format sense data diff --git a/include/scsi/scsi_cmnd.h b/include/scsi/scsi_cmnd.h index aaf1e971c6a3..3b9f42f6615c 100644 --- a/include/scsi/scsi_cmnd.h +++ b/include/scsi/scsi_cmnd.h @@ -120,7 +120,6 @@ struct scsi_cmnd { struct request *request; /* The command we are working on */ -#define SCSI_SENSE_BUFFERSIZE 96 unsigned char *sense_buffer; /* obtained by REQUEST SENSE when * CHECK CONDITION is received on original diff --git a/include/scsi/scsi_common.h b/include/scsi/scsi_common.h index 731ac09ed231..82d6209f2e10 100644 --- a/include/scsi/scsi_common.h +++ b/include/scsi/scsi_common.h @@ -8,6 +8,7 @@ #include #include +#include static inline unsigned scsi_varlen_cdb_length(const void *hdr) @@ -31,37 +32,6 @@ extern const char *scsi_device_type(unsigned type); extern void int_to_scsilun(u64, struct scsi_lun *); extern u64 scsilun_to_int(struct scsi_lun *); -/* - * This is a slightly modified SCSI sense "descriptor" format header. - * The addition is to allow the 0x70 and 0x71 response codes. The idea - * is to place the salient data from either "fixed" or "descriptor" sense - * format into one structure to ease application processing. - * - * The original sense buffer should be kept around for those cases - * in which more information is required (e.g. the LBA of a MEDIUM ERROR). - */ -struct scsi_sense_hdr { /* See SPC-3 section 4.5 */ - u8 response_code; /* permit: 0x0, 0x70, 0x71, 0x72, 0x73 */ - u8 sense_key; - u8 asc; - u8 ascq; - u8 byte4; - u8 byte5; - u8 byte6; - u8 additional_length; /* always 0 for fixed sense format */ -}; - -static inline bool scsi_sense_valid(const struct scsi_sense_hdr *sshdr) -{ - if (!sshdr) - return false; - - return (sshdr->response_code & 0x70) == 0x70; -} - -extern bool scsi_normalize_sense(const u8 *sense_buffer, int sb_len, - struct scsi_sense_hdr *sshdr); - extern void scsi_build_sense_buffer(int desc, u8 *buf, u8 key, u8 asc, u8 ascq); int scsi_set_sense_information(u8 *buf, int buf_len, u64 info); int scsi_set_sense_field_pointer(u8 *buf, int buf_len, u16 fp, u8 bp, bool cd); diff --git a/include/scsi/scsi_sense.h b/include/scsi/scsi_sense.h new file mode 100644 index 000000000000..be923c67b93e --- /dev/null +++ b/include/scsi/scsi_sense.h @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Structures and functions used by both SCSI and ATAPI sense code. + */ + +#ifndef _SCSI_SENSE_H_ +#define _SCSI_SENSE_H_ + +#include + +/* + * This is a slightly modified SCSI sense "descriptor" format header. + * The addition is to allow the 0x70 and 0x71 response codes. The idea + * is to place the salient data from either "fixed" or "descriptor" sense + * format into one structure to ease application processing. + * + * The original sense buffer should be kept around for those cases + * in which more information is required (e.g. the LBA of a MEDIUM ERROR). + */ +struct scsi_sense_hdr { /* See SPC-3 section 4.5 */ + u8 response_code; /* permit: 0x0, 0x70, 0x71, 0x72, 0x73 */ + u8 sense_key; + u8 asc; + u8 ascq; + u8 byte4; + u8 byte5; + u8 byte6; + u8 additional_length; /* always 0 for fixed sense format */ +}; + +static inline bool scsi_sense_valid(const struct scsi_sense_hdr *sshdr) +{ + if (!sshdr) + return false; + + return (sshdr->response_code & 0x70) == 0x70; +} + +#define SCSI_SENSE_BUFFERSIZE 96 + +extern bool scsi_normalize_sense(const u8 *sense_buffer, int sb_len, + struct scsi_sense_hdr *sshdr); + +#endif /* _SCSI_SENSE_H_ */ -- 2.17.0