[PATCH v3 2/3] scsi: ufs: core: Add sysfs attribute to control WB buffer resize function

From: Lu Hongfei
Date: Mon Sep 11 2023 - 02:00:24 EST


Add wb_buf_resize_control sysfs node, which allows the host to control the
WB buffer resize function.

Add ufshcd_configure_wb_buf_resize() function which will write the
bWriteBoosterBufferResizeEn attribute to enable WB buffer resize.

The detailed definition of the this node can be found in the sysfs
documentation.

Signed-off-by: Lu Hongfei <luhongfei@xxxxxxxx>
---
Documentation/ABI/testing/sysfs-driver-ufs | 17 ++++++++++++
drivers/ufs/core/ufs-sysfs.c | 32 ++++++++++++++++++++++
drivers/ufs/core/ufshcd.c | 15 ++++++++++
include/ufs/ufshcd.h | 1 +
4 files changed, 65 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-driver-ufs b/Documentation/ABI/testing/sysfs-driver-ufs
index 0c7efaf62de0..b8bd7e844cb0 100644
--- a/Documentation/ABI/testing/sysfs-driver-ufs
+++ b/Documentation/ABI/testing/sysfs-driver-ufs
@@ -1437,6 +1437,23 @@ Description:
If avail_wb_buff < wb_flush_threshold, it indicates that WriteBooster buffer needs to
be flushed, otherwise it is not necessary.

+What: /sys/bus/platform/drivers/ufshcd/*/wb_buf_resize_control
+What: /sys/bus/platform/devices/*.ufs/wb_buf_resize_control
+Date: Sept 2023
+Contact: Lu Hongfei <luhongfei@xxxxxxxx>
+Description:
+ The host can decrease or increase the WriteBooster Buffer size by setting
+ this file.
+
+ ====== ======================================
+ 00h Idle (There is no resize operation)
+ 01h Decrease WriteBooster Buffer Size
+ 02h Increase WriteBooster Buffer Size
+ Others Reserved
+ ====== ======================================
+
+ The file is write only.
+
Contact: Daniil Lunev <dlunev@xxxxxxxxxxxx>
What: /sys/bus/platform/drivers/ufshcd/*/capabilities/
What: /sys/bus/platform/devices/*.ufs/capabilities/
diff --git a/drivers/ufs/core/ufs-sysfs.c b/drivers/ufs/core/ufs-sysfs.c
index c95906443d5f..c14da6770316 100644
--- a/drivers/ufs/core/ufs-sysfs.c
+++ b/drivers/ufs/core/ufs-sysfs.c
@@ -329,6 +329,36 @@ static ssize_t wb_flush_threshold_store(struct device *dev,
return count;
}

+static ssize_t wb_buf_resize_control_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct ufs_hba *hba = dev_get_drvdata(dev);
+ unsigned int wb_buf_resize_op;
+
+ if (!ufshcd_is_wb_allowed(hba) || !hba->dev_info.wb_enabled) {
+ dev_err(dev, "The WB is not allowed or disabled!\n");
+ return -EINVAL;
+ }
+
+ if (!hba->dev_info.b_presrv_uspc_en) {
+ dev_err(dev, "The WB buf resize is not allowed!\n");
+ return -EINVAL;
+ }
+
+ if (kstrtouint(buf, 0, &wb_buf_resize_op))
+ return -EINVAL;
+
+ if (wb_buf_resize_op != 0x01 && wb_buf_resize_op != 0x02) {
+ dev_err(dev, "The operation %u is invalid!\n", wb_buf_resize_op);
+ return -EINVAL;
+ }
+
+ ufshcd_configure_wb_buf_resize(hba, wb_buf_resize_op);
+
+ return count;
+}
+
static DEVICE_ATTR_RW(rpm_lvl);
static DEVICE_ATTR_RO(rpm_target_dev_state);
static DEVICE_ATTR_RO(rpm_target_link_state);
@@ -339,6 +369,7 @@ static DEVICE_ATTR_RW(auto_hibern8);
static DEVICE_ATTR_RW(wb_on);
static DEVICE_ATTR_RW(enable_wb_buf_flush);
static DEVICE_ATTR_RW(wb_flush_threshold);
+static DEVICE_ATTR_WO(wb_buf_resize_control);

static struct attribute *ufs_sysfs_ufshcd_attrs[] = {
&dev_attr_rpm_lvl.attr,
@@ -351,6 +382,7 @@ static struct attribute *ufs_sysfs_ufshcd_attrs[] = {
&dev_attr_wb_on.attr,
&dev_attr_enable_wb_buf_flush.attr,
&dev_attr_wb_flush_threshold.attr,
+ &dev_attr_wb_buf_resize_control.attr,
NULL
};

diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index c2df07545f96..7a9b9d941155 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -6046,6 +6046,21 @@ static bool ufshcd_wb_need_flush(struct ufs_hba *hba)
return ufshcd_wb_presrv_usrspc_keep_vcc_on(hba, avail_buf);
}

+int ufshcd_configure_wb_buf_resize(struct ufs_hba *hba, u32 resize_op)
+{
+ int ret;
+ u8 index;
+
+ index = ufshcd_wb_get_query_index(hba);
+ ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
+ QUERY_ATTR_IDN_WB_BUF_RESIZE_EN, index, 0, &resize_op);
+ if (ret)
+ dev_err(hba->dev,
+ "%s: Enable WB buf resize operation failed %d\n",
+ __func__, ret);
+ return ret;
+}
+
static void ufshcd_rpm_dev_flush_recheck_work(struct work_struct *work)
{
struct ufs_hba *hba = container_of(to_delayed_work(work),
diff --git a/include/ufs/ufshcd.h b/include/ufs/ufshcd.h
index 7d07b256e906..a2e5fddfc245 100644
--- a/include/ufs/ufshcd.h
+++ b/include/ufs/ufshcd.h
@@ -1381,6 +1381,7 @@ int ufshcd_advanced_rpmb_req_handler(struct ufs_hba *hba, struct utp_upiu_req *r
struct ufs_ehs *ehs_rsp, int sg_cnt,
struct scatterlist *sg_list, enum dma_data_direction dir);
int ufshcd_wb_toggle(struct ufs_hba *hba, bool enable);
+int ufshcd_configure_wb_buf_resize(struct ufs_hba *hba, u32 resize_op);
int ufshcd_wb_toggle_buf_flush(struct ufs_hba *hba, bool enable);
int ufshcd_suspend_prepare(struct device *dev);
int __ufshcd_suspend_prepare(struct device *dev, bool rpm_ok_for_spm);
--
2.39.0