Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp2579353rdb; Fri, 22 Sep 2023 02:53:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEddHn37GzeohTJ/RQWW3704DgK62qvB1kfcWGLodf+ftLj4vneJqzMG9dSC6ofNd6k7KZk X-Received: by 2002:a05:6a00:2284:b0:68f:bbc3:df44 with SMTP id f4-20020a056a00228400b0068fbbc3df44mr10066541pfe.0.1695376415693; Fri, 22 Sep 2023 02:53:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695376415; cv=none; d=google.com; s=arc-20160816; b=uaRBWWPpQWgdRx9lEBVRKlXxWXHcGds20b4bFQpTjvDW9zq49US8vzF6l+MryePmOS c/a+xIcv9ex8VCVun3013AkFWnqyLin9VnSonkhxjIq7BU8b8pMiZov5jVaWGnKbyYKd jNQKL+xFSm0uMJXdkmL/GDskLGf8cRfTdM1/UDKRphfnd1a0ndY/zHqGEXSLjjrtdVKP nP3ER3GPRoYwbJYKzaNlZLoOZutqdKYzJ/aeMhYgk1o8eMNwunip1J+KstD7dth8Xav6 QbonDaCiBKM6u6HUOZzh46Jr4Ki9LqiEGVxAhGv6Td/teqU1+HzEAD4T7IQ+1g9GsWBN CFMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=JULaAYT20Ip8VFivaDNlJVIp9U2+m0R6atUprRfo4yQ=; fh=efbNbptNhbD3foczTZf5UTdQGTI/0Ud9virqYphOI9o=; b=aVL/9SPvXebBx61IFvNxDMs4qZSMK66/Vjr8ROcQyVpXEDSsP5UeRUqdV67rKnwhNw atHGsWIOL6AvmjzmIZKk2YEQ76i04fdQiZbIa9EwRxtNvU0MjvyaMEw0Rdk5O8NE4XHM X73VuTKUEed2WxHgxWGHF4YMVMv1kg19dcl/+jwj3Cy5jepdBwSwopAO7oxswptHMAEc DYizumS5ZGkuJ3sOBi00bfiqX95eFIJmi9HJeAklm2QBZdZzRKoS4NGFTn+Hx5GjWWRP 1EVGVl05whsLdxEzPGZ9E0dg+i26V/k/0b4UUY+kABOGmus3Nkx0S9t1vj2z18VcNFos U5qQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id g185-20020a636bc2000000b00565eb0b2e66si3330468pgc.864.2023.09.22.02.53.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 02:53:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 8BAD58096382; Fri, 22 Sep 2023 02:30:40 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233279AbjIVJaj (ORCPT + 99 others); Fri, 22 Sep 2023 05:30:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233025AbjIVJ3x (ORCPT ); Fri, 22 Sep 2023 05:29:53 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B789CE4; Fri, 22 Sep 2023 02:29:38 -0700 (PDT) Received: from kwepemm000012.china.huawei.com (unknown [172.30.72.57]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4RsRhl168XzMlmS; Fri, 22 Sep 2023 17:25:59 +0800 (CST) Received: from build.huawei.com (10.175.101.6) by kwepemm000012.china.huawei.com (7.193.23.142) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.31; Fri, 22 Sep 2023 17:29:35 +0800 From: Wenchao Hao To: "James E . J . Bottomley" , "Martin K . Petersen" , Douglas Gilbert , , CC: , , Wenchao Hao Subject: [PATCH v5 06/10] scsi: scsi_debug: set command's result and sense data if the error is injected Date: Fri, 22 Sep 2023 17:29:02 +0800 Message-ID: <20230922092906.2645265-7-haowenchao2@huawei.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20230922092906.2645265-1-haowenchao2@huawei.com> References: <20230922092906.2645265-1-haowenchao2@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.101.6] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To kwepemm000012.china.huawei.com (7.193.23.142) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Fri, 22 Sep 2023 02:30:40 -0700 (PDT) If a fail commnd error is injected, set the command's status and sense data then finish this scsi command. Set SCSI command's status and sense data format: +--------+------+-------------------------------------------------------+ | Column | Type | Description | +--------+------+-------------------------------------------------------+ | 1 | u8 | Error type, fixed to 0x2 | +--------+------+-------------------------------------------------------+ | 2 | s32 | Error Count | | | | 0: the rule will be ignored | | | | positive: the rule will always take effect | | | | negative: the rule takes effect n times where -n is | | | | the value given. Ignored after n times | +--------+------+-------------------------------------------------------+ | 3 | x8 | SCSI command opcode, 0xff for all commands | +--------+------+-------------------------------------------------------+ | 4 | x8 | Host byte in scsi_cmd::status | | | | [scsi_cmd::status has 32 bits holding these 3 bytes] | +--------+------+-------------------------------------------------------+ | 5 | x8 | Driver byte in scsi_cmd::status | +--------+------+-------------------------------------------------------+ | 6 | x8 | SCSI Status byte in scsi_cmd::status | +--------+------+-------------------------------------------------------+ | 7 | x8 | SCSI Sense Key in scsi_cmnd | +--------+------+-------------------------------------------------------+ | 8 | x8 | SCSI ASC in scsi_cmnd | +--------+------+-------------------------------------------------------+ | 9 | x8 | SCSI ASCQ in scsi_cmnd | +--------+------+-------------------------------------------------------+ Examples: error=/sys/kernel/debug/scsi_debug/0:0:0:1/error echo "2 -10 0x88 0 0 0x2 0x3 0x11 0x0" >${error} will make device's read command return with media error with additional sense of "Unrecovered read error" (UNC): Acked-by: Douglas Gilbert Signed-off-by: Wenchao Hao --- drivers/scsi/scsi_debug.c | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/drivers/scsi/scsi_debug.c b/drivers/scsi/scsi_debug.c index dea40d983155..fe1f7421f617 100644 --- a/drivers/scsi/scsi_debug.c +++ b/drivers/scsi/scsi_debug.c @@ -7787,6 +7787,48 @@ static int sdebug_fail_queue_cmd(struct scsi_cmnd *cmnd) return 0; } +static int sdebug_fail_cmd(struct scsi_cmnd *cmnd, int *retval, + struct sdebug_err_inject *info) +{ + struct scsi_device *sdp = cmnd->device; + struct sdebug_dev_info *devip = (struct sdebug_dev_info *)sdp->hostdata; + struct sdebug_err_inject *err; + unsigned char *cmd = cmnd->cmnd; + int ret = 0; + int result; + + if (devip == NULL) + return 0; + + rcu_read_lock(); + list_for_each_entry_rcu(err, &devip->inject_err_list, list) { + if (err->type == ERR_FAIL_CMD && + (err->cmd == cmd[0] || err->cmd == 0xff)) { + if (!err->cnt) { + rcu_read_unlock(); + return 0; + } + + ret = !!err->cnt; + rcu_read_unlock(); + goto out_handle; + } + } + rcu_read_unlock(); + + return 0; + +out_handle: + if (err->cnt < 0) + err->cnt++; + mk_sense_buffer(cmnd, err->sense_key, err->asc, err->asq); + result = err->status_byte | err->host_byte << 16 | err->driver_byte << 24; + *info = *err; + *retval = schedule_resp(cmnd, devip, result, NULL, 0, 0); + + return ret; +} + static int scsi_debug_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scp) { @@ -7807,6 +7849,7 @@ static int scsi_debug_queuecommand(struct Scsi_Host *shost, bool has_wlun_rl; bool inject_now; int ret = 0; + struct sdebug_err_inject err; scsi_set_resid(scp, 0); if (sdebug_statistics) { @@ -7859,6 +7902,16 @@ static int scsi_debug_queuecommand(struct Scsi_Host *shost, return ret; } + if (sdebug_fail_cmd(scp, &ret, &err)) { + scmd_printk(KERN_INFO, scp, + "fail command 0x%x with hostbyte=0x%x, " + "driverbyte=0x%x, statusbyte=0x%x, " + "sense_key=0x%x, asc=0x%x, asq=0x%x\n", + opcode, err.host_byte, err.driver_byte, + err.status_byte, err.sense_key, err.asc, err.asq); + return ret; + } + if (unlikely(inject_now && !atomic_read(&sdeb_inject_pending))) atomic_set(&sdeb_inject_pending, 1); -- 2.32.0