From patchwork Wed Jan 17 12:49:52 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: weiping zhang X-Patchwork-Id: 10169243 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 CC525601D3 for ; Wed, 17 Jan 2018 12:50:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B814E2858C for ; Wed, 17 Jan 2018 12:50:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ACF74285A8; Wed, 17 Jan 2018 12:50:16 +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 vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CB299285A6 for ; Wed, 17 Jan 2018 12:50:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752705AbeAQMuL (ORCPT ); Wed, 17 Jan 2018 07:50:11 -0500 Received: from mx2.didichuxing.com ([36.110.17.22]:2582 "EHLO BJEXCAS004.didichuxing.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752675AbeAQMuL (ORCPT ); Wed, 17 Jan 2018 07:50:11 -0500 Received: from bogon.didichuxing.com (172.22.50.20) by BJSGEXMBX03.didichuxing.com (172.20.15.133) with Microsoft SMTP Server (TLS) id 15.0.1263.5; Wed, 17 Jan 2018 20:49:56 +0800 Date: Wed, 17 Jan 2018 20:49:52 +0800 From: weiping zhang To: , CC: Subject: [RFC PATCH] scsi: sd: add wce_rcd sysfs interface Message-ID: <20180117124945.GA7787@bogon.didichuxing.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-Originating-IP: [172.22.50.20] X-ClientProxiedBy: BJEXCAS004.didichuxing.com (172.20.1.44) To BJSGEXMBX03.didichuxing.com (172.20.15.133) Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP add user friendly interface wce_rcd to enable/disable write&read cache. The code base on cache_type, but need short input. enable both write and read cache: echo "10" > wce_rcd wce rcd write_cache read_cache 0 0 off on 0 1 off off 1 0 on on 1 1 on off Signed-off-by: weiping zhang --- drivers/scsi/sd.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index a028ab3..9ab210c 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -139,6 +139,15 @@ static const char *sd_cache_types[] = { "write back, no read (daft)" }; +/* + * wce rcd write_cache read_cache + * 0 0 off on + * 0 1 off off + * 1 0 on on + * 1 1 on off + */ +static const char * const sd_wce_rcd[] = {"00", "01", "10", "11"}; + static void sd_set_flush_flag(struct scsi_disk *sdkp) { bool wc = false, fua = false; @@ -287,6 +296,80 @@ cache_type_show(struct device *dev, struct device_attribute *attr, char *buf) static DEVICE_ATTR_RW(cache_type); static ssize_t +wce_rcd_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + int ct, rcd, wce, sp; + struct scsi_disk *sdkp = to_scsi_disk(dev); + struct scsi_device *sdp = sdkp->device; + char buffer[64]; + char *buffer_data; + struct scsi_mode_data data; + struct scsi_sense_hdr sshdr; + static const char temp[] = "temp "; + int len; + + if (sdp->type != TYPE_DISK && sdp->type != TYPE_ZBC) + /* no cache control on RBC devices; theoretically they + * can do it, but there's probably so many exceptions + * it's not worth the risk + */ + return -EINVAL; + + if (strncmp(buf, temp, sizeof(temp) - 1) == 0) { + buf += sizeof(temp) - 1; + sdkp->cache_override = 1; + } else { + sdkp->cache_override = 0; + } + + ct = sysfs_match_string(sd_wce_rcd, buf); + if (ct < 0) + return -EINVAL; + + rcd = ct & 0x01 ? 1 : 0; + wce = (ct & 0x02) && !sdkp->write_prot ? 1 : 0; + + if (sdkp->cache_override) { + sdkp->WCE = wce; + sdkp->RCD = rcd; + sd_set_flush_flag(sdkp); + return count; + } + + if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT, + SD_MAX_RETRIES, &data, NULL)) + return -EINVAL; + len = min_t(size_t, sizeof(buffer), data.length - data.header_length - + data.block_descriptor_length); + buffer_data = buffer + data.header_length + + data.block_descriptor_length; + buffer_data[2] &= ~0x05; + buffer_data[2] |= wce << 2 | rcd; + sp = buffer_data[0] & 0x80 ? 1 : 0; + buffer_data[0] &= ~0x80; + + if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT, + SD_MAX_RETRIES, &data, &sshdr)) { + if (scsi_sense_valid(&sshdr)) + sd_print_sense_hdr(sdkp, &sshdr); + return -EINVAL; + } + revalidate_disk(sdkp->disk); + return count; +} + +static ssize_t +wce_rcd_show(struct device *dev, struct device_attribute *attr, char *buf) +{ + struct scsi_disk *sdkp = to_scsi_disk(dev); + int ct = sdkp->RCD + 2*sdkp->WCE; + + return sprintf(buf, "%s\n", sd_wce_rcd[ct]); +} +static DEVICE_ATTR_RW(wce_rcd); + +static ssize_t FUA_show(struct device *dev, struct device_attribute *attr, char *buf) { struct scsi_disk *sdkp = to_scsi_disk(dev); @@ -524,6 +607,7 @@ static DEVICE_ATTR_RW(max_write_same_blocks); static struct attribute *sd_disk_attrs[] = { &dev_attr_cache_type.attr, + &dev_attr_wce_rcd.attr, &dev_attr_FUA.attr, &dev_attr_allow_restart.attr, &dev_attr_manage_start_stop.attr,