cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

ufs-sysfs.c (41375B)


      1// SPDX-License-Identifier: GPL-2.0
      2// Copyright (C) 2018 Western Digital Corporation
      3
      4#include <linux/err.h>
      5#include <linux/string.h>
      6#include <linux/bitfield.h>
      7#include <asm/unaligned.h>
      8
      9#include <ufs/ufs.h>
     10#include "ufs-sysfs.h"
     11#include "ufshcd-priv.h"
     12
     13static const char *ufshcd_uic_link_state_to_string(
     14			enum uic_link_state state)
     15{
     16	switch (state) {
     17	case UIC_LINK_OFF_STATE:	return "OFF";
     18	case UIC_LINK_ACTIVE_STATE:	return "ACTIVE";
     19	case UIC_LINK_HIBERN8_STATE:	return "HIBERN8";
     20	case UIC_LINK_BROKEN_STATE:	return "BROKEN";
     21	default:			return "UNKNOWN";
     22	}
     23}
     24
     25static const char *ufshcd_ufs_dev_pwr_mode_to_string(
     26			enum ufs_dev_pwr_mode state)
     27{
     28	switch (state) {
     29	case UFS_ACTIVE_PWR_MODE:	return "ACTIVE";
     30	case UFS_SLEEP_PWR_MODE:	return "SLEEP";
     31	case UFS_POWERDOWN_PWR_MODE:	return "POWERDOWN";
     32	case UFS_DEEPSLEEP_PWR_MODE:	return "DEEPSLEEP";
     33	default:			return "UNKNOWN";
     34	}
     35}
     36
     37static inline ssize_t ufs_sysfs_pm_lvl_store(struct device *dev,
     38					     struct device_attribute *attr,
     39					     const char *buf, size_t count,
     40					     bool rpm)
     41{
     42	struct ufs_hba *hba = dev_get_drvdata(dev);
     43	struct ufs_dev_info *dev_info = &hba->dev_info;
     44	unsigned long flags, value;
     45
     46	if (kstrtoul(buf, 0, &value))
     47		return -EINVAL;
     48
     49	if (value >= UFS_PM_LVL_MAX)
     50		return -EINVAL;
     51
     52	if (ufs_pm_lvl_states[value].dev_state == UFS_DEEPSLEEP_PWR_MODE &&
     53	    (!(hba->caps & UFSHCD_CAP_DEEPSLEEP) ||
     54	     !(dev_info->wspecversion >= 0x310)))
     55		return -EINVAL;
     56
     57	spin_lock_irqsave(hba->host->host_lock, flags);
     58	if (rpm)
     59		hba->rpm_lvl = value;
     60	else
     61		hba->spm_lvl = value;
     62	spin_unlock_irqrestore(hba->host->host_lock, flags);
     63	return count;
     64}
     65
     66static ssize_t rpm_lvl_show(struct device *dev,
     67		struct device_attribute *attr, char *buf)
     68{
     69	struct ufs_hba *hba = dev_get_drvdata(dev);
     70
     71	return sysfs_emit(buf, "%d\n", hba->rpm_lvl);
     72}
     73
     74static ssize_t rpm_lvl_store(struct device *dev,
     75		struct device_attribute *attr, const char *buf, size_t count)
     76{
     77	return ufs_sysfs_pm_lvl_store(dev, attr, buf, count, true);
     78}
     79
     80static ssize_t rpm_target_dev_state_show(struct device *dev,
     81		struct device_attribute *attr, char *buf)
     82{
     83	struct ufs_hba *hba = dev_get_drvdata(dev);
     84
     85	return sysfs_emit(buf, "%s\n", ufshcd_ufs_dev_pwr_mode_to_string(
     86			ufs_pm_lvl_states[hba->rpm_lvl].dev_state));
     87}
     88
     89static ssize_t rpm_target_link_state_show(struct device *dev,
     90		struct device_attribute *attr, char *buf)
     91{
     92	struct ufs_hba *hba = dev_get_drvdata(dev);
     93
     94	return sysfs_emit(buf, "%s\n", ufshcd_uic_link_state_to_string(
     95			ufs_pm_lvl_states[hba->rpm_lvl].link_state));
     96}
     97
     98static ssize_t spm_lvl_show(struct device *dev,
     99		struct device_attribute *attr, char *buf)
    100{
    101	struct ufs_hba *hba = dev_get_drvdata(dev);
    102
    103	return sysfs_emit(buf, "%d\n", hba->spm_lvl);
    104}
    105
    106static ssize_t spm_lvl_store(struct device *dev,
    107		struct device_attribute *attr, const char *buf, size_t count)
    108{
    109	return ufs_sysfs_pm_lvl_store(dev, attr, buf, count, false);
    110}
    111
    112static ssize_t spm_target_dev_state_show(struct device *dev,
    113		struct device_attribute *attr, char *buf)
    114{
    115	struct ufs_hba *hba = dev_get_drvdata(dev);
    116
    117	return sysfs_emit(buf, "%s\n", ufshcd_ufs_dev_pwr_mode_to_string(
    118				ufs_pm_lvl_states[hba->spm_lvl].dev_state));
    119}
    120
    121static ssize_t spm_target_link_state_show(struct device *dev,
    122		struct device_attribute *attr, char *buf)
    123{
    124	struct ufs_hba *hba = dev_get_drvdata(dev);
    125
    126	return sysfs_emit(buf, "%s\n", ufshcd_uic_link_state_to_string(
    127				ufs_pm_lvl_states[hba->spm_lvl].link_state));
    128}
    129
    130/* Convert Auto-Hibernate Idle Timer register value to microseconds */
    131static int ufshcd_ahit_to_us(u32 ahit)
    132{
    133	int timer = FIELD_GET(UFSHCI_AHIBERN8_TIMER_MASK, ahit);
    134	int scale = FIELD_GET(UFSHCI_AHIBERN8_SCALE_MASK, ahit);
    135
    136	for (; scale > 0; --scale)
    137		timer *= UFSHCI_AHIBERN8_SCALE_FACTOR;
    138
    139	return timer;
    140}
    141
    142/* Convert microseconds to Auto-Hibernate Idle Timer register value */
    143static u32 ufshcd_us_to_ahit(unsigned int timer)
    144{
    145	unsigned int scale;
    146
    147	for (scale = 0; timer > UFSHCI_AHIBERN8_TIMER_MASK; ++scale)
    148		timer /= UFSHCI_AHIBERN8_SCALE_FACTOR;
    149
    150	return FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, timer) |
    151	       FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, scale);
    152}
    153
    154static ssize_t auto_hibern8_show(struct device *dev,
    155				 struct device_attribute *attr, char *buf)
    156{
    157	u32 ahit;
    158	int ret;
    159	struct ufs_hba *hba = dev_get_drvdata(dev);
    160
    161	if (!ufshcd_is_auto_hibern8_supported(hba))
    162		return -EOPNOTSUPP;
    163
    164	down(&hba->host_sem);
    165	if (!ufshcd_is_user_access_allowed(hba)) {
    166		ret = -EBUSY;
    167		goto out;
    168	}
    169
    170	pm_runtime_get_sync(hba->dev);
    171	ufshcd_hold(hba, false);
    172	ahit = ufshcd_readl(hba, REG_AUTO_HIBERNATE_IDLE_TIMER);
    173	ufshcd_release(hba);
    174	pm_runtime_put_sync(hba->dev);
    175
    176	ret = sysfs_emit(buf, "%d\n", ufshcd_ahit_to_us(ahit));
    177
    178out:
    179	up(&hba->host_sem);
    180	return ret;
    181}
    182
    183static ssize_t auto_hibern8_store(struct device *dev,
    184				  struct device_attribute *attr,
    185				  const char *buf, size_t count)
    186{
    187	struct ufs_hba *hba = dev_get_drvdata(dev);
    188	unsigned int timer;
    189	int ret = 0;
    190
    191	if (!ufshcd_is_auto_hibern8_supported(hba))
    192		return -EOPNOTSUPP;
    193
    194	if (kstrtouint(buf, 0, &timer))
    195		return -EINVAL;
    196
    197	if (timer > UFSHCI_AHIBERN8_MAX)
    198		return -EINVAL;
    199
    200	down(&hba->host_sem);
    201	if (!ufshcd_is_user_access_allowed(hba)) {
    202		ret = -EBUSY;
    203		goto out;
    204	}
    205
    206	ufshcd_auto_hibern8_update(hba, ufshcd_us_to_ahit(timer));
    207
    208out:
    209	up(&hba->host_sem);
    210	return ret ? ret : count;
    211}
    212
    213static ssize_t wb_on_show(struct device *dev, struct device_attribute *attr,
    214			  char *buf)
    215{
    216	struct ufs_hba *hba = dev_get_drvdata(dev);
    217
    218	return sysfs_emit(buf, "%d\n", hba->dev_info.wb_enabled);
    219}
    220
    221static ssize_t wb_on_store(struct device *dev, struct device_attribute *attr,
    222			   const char *buf, size_t count)
    223{
    224	struct ufs_hba *hba = dev_get_drvdata(dev);
    225	unsigned int wb_enable;
    226	ssize_t res;
    227
    228	if (!ufshcd_is_wb_allowed(hba) || ufshcd_is_clkscaling_supported(hba)) {
    229		/*
    230		 * If the platform supports UFSHCD_CAP_CLK_SCALING, turn WB
    231		 * on/off will be done while clock scaling up/down.
    232		 */
    233		dev_warn(dev, "To control WB through wb_on is not allowed!\n");
    234		return -EOPNOTSUPP;
    235	}
    236
    237	if (kstrtouint(buf, 0, &wb_enable))
    238		return -EINVAL;
    239
    240	if (wb_enable != 0 && wb_enable != 1)
    241		return -EINVAL;
    242
    243	down(&hba->host_sem);
    244	if (!ufshcd_is_user_access_allowed(hba)) {
    245		res = -EBUSY;
    246		goto out;
    247	}
    248
    249	ufshcd_rpm_get_sync(hba);
    250	res = ufshcd_wb_toggle(hba, wb_enable);
    251	ufshcd_rpm_put_sync(hba);
    252out:
    253	up(&hba->host_sem);
    254	return res < 0 ? res : count;
    255}
    256
    257static DEVICE_ATTR_RW(rpm_lvl);
    258static DEVICE_ATTR_RO(rpm_target_dev_state);
    259static DEVICE_ATTR_RO(rpm_target_link_state);
    260static DEVICE_ATTR_RW(spm_lvl);
    261static DEVICE_ATTR_RO(spm_target_dev_state);
    262static DEVICE_ATTR_RO(spm_target_link_state);
    263static DEVICE_ATTR_RW(auto_hibern8);
    264static DEVICE_ATTR_RW(wb_on);
    265
    266static struct attribute *ufs_sysfs_ufshcd_attrs[] = {
    267	&dev_attr_rpm_lvl.attr,
    268	&dev_attr_rpm_target_dev_state.attr,
    269	&dev_attr_rpm_target_link_state.attr,
    270	&dev_attr_spm_lvl.attr,
    271	&dev_attr_spm_target_dev_state.attr,
    272	&dev_attr_spm_target_link_state.attr,
    273	&dev_attr_auto_hibern8.attr,
    274	&dev_attr_wb_on.attr,
    275	NULL
    276};
    277
    278static const struct attribute_group ufs_sysfs_default_group = {
    279	.attrs = ufs_sysfs_ufshcd_attrs,
    280};
    281
    282static ssize_t monitor_enable_show(struct device *dev,
    283				   struct device_attribute *attr, char *buf)
    284{
    285	struct ufs_hba *hba = dev_get_drvdata(dev);
    286
    287	return sysfs_emit(buf, "%d\n", hba->monitor.enabled);
    288}
    289
    290static ssize_t monitor_enable_store(struct device *dev,
    291				    struct device_attribute *attr,
    292				    const char *buf, size_t count)
    293{
    294	struct ufs_hba *hba = dev_get_drvdata(dev);
    295	unsigned long value, flags;
    296
    297	if (kstrtoul(buf, 0, &value))
    298		return -EINVAL;
    299
    300	value = !!value;
    301	spin_lock_irqsave(hba->host->host_lock, flags);
    302	if (value == hba->monitor.enabled)
    303		goto out_unlock;
    304
    305	if (!value) {
    306		memset(&hba->monitor, 0, sizeof(hba->monitor));
    307	} else {
    308		hba->monitor.enabled = true;
    309		hba->monitor.enabled_ts = ktime_get();
    310	}
    311
    312out_unlock:
    313	spin_unlock_irqrestore(hba->host->host_lock, flags);
    314	return count;
    315}
    316
    317static ssize_t monitor_chunk_size_show(struct device *dev,
    318				   struct device_attribute *attr, char *buf)
    319{
    320	struct ufs_hba *hba = dev_get_drvdata(dev);
    321
    322	return sysfs_emit(buf, "%lu\n", hba->monitor.chunk_size);
    323}
    324
    325static ssize_t monitor_chunk_size_store(struct device *dev,
    326				    struct device_attribute *attr,
    327				    const char *buf, size_t count)
    328{
    329	struct ufs_hba *hba = dev_get_drvdata(dev);
    330	unsigned long value, flags;
    331
    332	if (kstrtoul(buf, 0, &value))
    333		return -EINVAL;
    334
    335	spin_lock_irqsave(hba->host->host_lock, flags);
    336	/* Only allow chunk size change when monitor is disabled */
    337	if (!hba->monitor.enabled)
    338		hba->monitor.chunk_size = value;
    339	spin_unlock_irqrestore(hba->host->host_lock, flags);
    340	return count;
    341}
    342
    343static ssize_t read_total_sectors_show(struct device *dev,
    344				       struct device_attribute *attr, char *buf)
    345{
    346	struct ufs_hba *hba = dev_get_drvdata(dev);
    347
    348	return sysfs_emit(buf, "%lu\n", hba->monitor.nr_sec_rw[READ]);
    349}
    350
    351static ssize_t read_total_busy_show(struct device *dev,
    352				    struct device_attribute *attr, char *buf)
    353{
    354	struct ufs_hba *hba = dev_get_drvdata(dev);
    355
    356	return sysfs_emit(buf, "%llu\n",
    357			  ktime_to_us(hba->monitor.total_busy[READ]));
    358}
    359
    360static ssize_t read_nr_requests_show(struct device *dev,
    361				     struct device_attribute *attr, char *buf)
    362{
    363	struct ufs_hba *hba = dev_get_drvdata(dev);
    364
    365	return sysfs_emit(buf, "%lu\n", hba->monitor.nr_req[READ]);
    366}
    367
    368static ssize_t read_req_latency_avg_show(struct device *dev,
    369					 struct device_attribute *attr,
    370					 char *buf)
    371{
    372	struct ufs_hba *hba = dev_get_drvdata(dev);
    373	struct ufs_hba_monitor *m = &hba->monitor;
    374
    375	return sysfs_emit(buf, "%llu\n", div_u64(ktime_to_us(m->lat_sum[READ]),
    376						 m->nr_req[READ]));
    377}
    378
    379static ssize_t read_req_latency_max_show(struct device *dev,
    380					 struct device_attribute *attr,
    381					 char *buf)
    382{
    383	struct ufs_hba *hba = dev_get_drvdata(dev);
    384
    385	return sysfs_emit(buf, "%llu\n",
    386			  ktime_to_us(hba->monitor.lat_max[READ]));
    387}
    388
    389static ssize_t read_req_latency_min_show(struct device *dev,
    390					 struct device_attribute *attr,
    391					 char *buf)
    392{
    393	struct ufs_hba *hba = dev_get_drvdata(dev);
    394
    395	return sysfs_emit(buf, "%llu\n",
    396			  ktime_to_us(hba->monitor.lat_min[READ]));
    397}
    398
    399static ssize_t read_req_latency_sum_show(struct device *dev,
    400					 struct device_attribute *attr,
    401					 char *buf)
    402{
    403	struct ufs_hba *hba = dev_get_drvdata(dev);
    404
    405	return sysfs_emit(buf, "%llu\n",
    406			  ktime_to_us(hba->monitor.lat_sum[READ]));
    407}
    408
    409static ssize_t write_total_sectors_show(struct device *dev,
    410					struct device_attribute *attr,
    411					char *buf)
    412{
    413	struct ufs_hba *hba = dev_get_drvdata(dev);
    414
    415	return sysfs_emit(buf, "%lu\n", hba->monitor.nr_sec_rw[WRITE]);
    416}
    417
    418static ssize_t write_total_busy_show(struct device *dev,
    419				     struct device_attribute *attr, char *buf)
    420{
    421	struct ufs_hba *hba = dev_get_drvdata(dev);
    422
    423	return sysfs_emit(buf, "%llu\n",
    424			  ktime_to_us(hba->monitor.total_busy[WRITE]));
    425}
    426
    427static ssize_t write_nr_requests_show(struct device *dev,
    428				      struct device_attribute *attr, char *buf)
    429{
    430	struct ufs_hba *hba = dev_get_drvdata(dev);
    431
    432	return sysfs_emit(buf, "%lu\n", hba->monitor.nr_req[WRITE]);
    433}
    434
    435static ssize_t write_req_latency_avg_show(struct device *dev,
    436					  struct device_attribute *attr,
    437					  char *buf)
    438{
    439	struct ufs_hba *hba = dev_get_drvdata(dev);
    440	struct ufs_hba_monitor *m = &hba->monitor;
    441
    442	return sysfs_emit(buf, "%llu\n", div_u64(ktime_to_us(m->lat_sum[WRITE]),
    443						 m->nr_req[WRITE]));
    444}
    445
    446static ssize_t write_req_latency_max_show(struct device *dev,
    447					  struct device_attribute *attr,
    448					  char *buf)
    449{
    450	struct ufs_hba *hba = dev_get_drvdata(dev);
    451
    452	return sysfs_emit(buf, "%llu\n",
    453			  ktime_to_us(hba->monitor.lat_max[WRITE]));
    454}
    455
    456static ssize_t write_req_latency_min_show(struct device *dev,
    457					  struct device_attribute *attr,
    458					  char *buf)
    459{
    460	struct ufs_hba *hba = dev_get_drvdata(dev);
    461
    462	return sysfs_emit(buf, "%llu\n",
    463			  ktime_to_us(hba->monitor.lat_min[WRITE]));
    464}
    465
    466static ssize_t write_req_latency_sum_show(struct device *dev,
    467					  struct device_attribute *attr,
    468					  char *buf)
    469{
    470	struct ufs_hba *hba = dev_get_drvdata(dev);
    471
    472	return sysfs_emit(buf, "%llu\n",
    473			  ktime_to_us(hba->monitor.lat_sum[WRITE]));
    474}
    475
    476static DEVICE_ATTR_RW(monitor_enable);
    477static DEVICE_ATTR_RW(monitor_chunk_size);
    478static DEVICE_ATTR_RO(read_total_sectors);
    479static DEVICE_ATTR_RO(read_total_busy);
    480static DEVICE_ATTR_RO(read_nr_requests);
    481static DEVICE_ATTR_RO(read_req_latency_avg);
    482static DEVICE_ATTR_RO(read_req_latency_max);
    483static DEVICE_ATTR_RO(read_req_latency_min);
    484static DEVICE_ATTR_RO(read_req_latency_sum);
    485static DEVICE_ATTR_RO(write_total_sectors);
    486static DEVICE_ATTR_RO(write_total_busy);
    487static DEVICE_ATTR_RO(write_nr_requests);
    488static DEVICE_ATTR_RO(write_req_latency_avg);
    489static DEVICE_ATTR_RO(write_req_latency_max);
    490static DEVICE_ATTR_RO(write_req_latency_min);
    491static DEVICE_ATTR_RO(write_req_latency_sum);
    492
    493static struct attribute *ufs_sysfs_monitor_attrs[] = {
    494	&dev_attr_monitor_enable.attr,
    495	&dev_attr_monitor_chunk_size.attr,
    496	&dev_attr_read_total_sectors.attr,
    497	&dev_attr_read_total_busy.attr,
    498	&dev_attr_read_nr_requests.attr,
    499	&dev_attr_read_req_latency_avg.attr,
    500	&dev_attr_read_req_latency_max.attr,
    501	&dev_attr_read_req_latency_min.attr,
    502	&dev_attr_read_req_latency_sum.attr,
    503	&dev_attr_write_total_sectors.attr,
    504	&dev_attr_write_total_busy.attr,
    505	&dev_attr_write_nr_requests.attr,
    506	&dev_attr_write_req_latency_avg.attr,
    507	&dev_attr_write_req_latency_max.attr,
    508	&dev_attr_write_req_latency_min.attr,
    509	&dev_attr_write_req_latency_sum.attr,
    510	NULL
    511};
    512
    513static const struct attribute_group ufs_sysfs_monitor_group = {
    514	.name = "monitor",
    515	.attrs = ufs_sysfs_monitor_attrs,
    516};
    517
    518static ssize_t ufs_sysfs_read_desc_param(struct ufs_hba *hba,
    519				  enum desc_idn desc_id,
    520				  u8 desc_index,
    521				  u8 param_offset,
    522				  u8 *sysfs_buf,
    523				  u8 param_size)
    524{
    525	u8 desc_buf[8] = {0};
    526	int ret;
    527
    528	if (param_size > 8)
    529		return -EINVAL;
    530
    531	down(&hba->host_sem);
    532	if (!ufshcd_is_user_access_allowed(hba)) {
    533		ret = -EBUSY;
    534		goto out;
    535	}
    536
    537	ufshcd_rpm_get_sync(hba);
    538	ret = ufshcd_read_desc_param(hba, desc_id, desc_index,
    539				param_offset, desc_buf, param_size);
    540	ufshcd_rpm_put_sync(hba);
    541	if (ret) {
    542		ret = -EINVAL;
    543		goto out;
    544	}
    545
    546	switch (param_size) {
    547	case 1:
    548		ret = sysfs_emit(sysfs_buf, "0x%02X\n", *desc_buf);
    549		break;
    550	case 2:
    551		ret = sysfs_emit(sysfs_buf, "0x%04X\n",
    552			get_unaligned_be16(desc_buf));
    553		break;
    554	case 4:
    555		ret = sysfs_emit(sysfs_buf, "0x%08X\n",
    556			get_unaligned_be32(desc_buf));
    557		break;
    558	case 8:
    559		ret = sysfs_emit(sysfs_buf, "0x%016llX\n",
    560			get_unaligned_be64(desc_buf));
    561		break;
    562	}
    563
    564out:
    565	up(&hba->host_sem);
    566	return ret;
    567}
    568
    569#define UFS_DESC_PARAM(_name, _puname, _duname, _size)			\
    570static ssize_t _name##_show(struct device *dev,				\
    571	struct device_attribute *attr, char *buf)			\
    572{									\
    573	struct ufs_hba *hba = dev_get_drvdata(dev);			\
    574	return ufs_sysfs_read_desc_param(hba, QUERY_DESC_IDN_##_duname,	\
    575		0, _duname##_DESC_PARAM##_puname, buf, _size);		\
    576}									\
    577static DEVICE_ATTR_RO(_name)
    578
    579#define UFS_DEVICE_DESC_PARAM(_name, _uname, _size)			\
    580	UFS_DESC_PARAM(_name, _uname, DEVICE, _size)
    581
    582UFS_DEVICE_DESC_PARAM(device_type, _DEVICE_TYPE, 1);
    583UFS_DEVICE_DESC_PARAM(device_class, _DEVICE_CLASS, 1);
    584UFS_DEVICE_DESC_PARAM(device_sub_class, _DEVICE_SUB_CLASS, 1);
    585UFS_DEVICE_DESC_PARAM(protocol, _PRTCL, 1);
    586UFS_DEVICE_DESC_PARAM(number_of_luns, _NUM_LU, 1);
    587UFS_DEVICE_DESC_PARAM(number_of_wluns, _NUM_WLU, 1);
    588UFS_DEVICE_DESC_PARAM(boot_enable, _BOOT_ENBL, 1);
    589UFS_DEVICE_DESC_PARAM(descriptor_access_enable, _DESC_ACCSS_ENBL, 1);
    590UFS_DEVICE_DESC_PARAM(initial_power_mode, _INIT_PWR_MODE, 1);
    591UFS_DEVICE_DESC_PARAM(high_priority_lun, _HIGH_PR_LUN, 1);
    592UFS_DEVICE_DESC_PARAM(secure_removal_type, _SEC_RMV_TYPE, 1);
    593UFS_DEVICE_DESC_PARAM(support_security_lun, _SEC_LU, 1);
    594UFS_DEVICE_DESC_PARAM(bkops_termination_latency, _BKOP_TERM_LT, 1);
    595UFS_DEVICE_DESC_PARAM(initial_active_icc_level, _ACTVE_ICC_LVL, 1);
    596UFS_DEVICE_DESC_PARAM(specification_version, _SPEC_VER, 2);
    597UFS_DEVICE_DESC_PARAM(manufacturing_date, _MANF_DATE, 2);
    598UFS_DEVICE_DESC_PARAM(manufacturer_id, _MANF_ID, 2);
    599UFS_DEVICE_DESC_PARAM(rtt_capability, _RTT_CAP, 1);
    600UFS_DEVICE_DESC_PARAM(rtc_update, _FRQ_RTC, 2);
    601UFS_DEVICE_DESC_PARAM(ufs_features, _UFS_FEAT, 1);
    602UFS_DEVICE_DESC_PARAM(ffu_timeout, _FFU_TMT, 1);
    603UFS_DEVICE_DESC_PARAM(queue_depth, _Q_DPTH, 1);
    604UFS_DEVICE_DESC_PARAM(device_version, _DEV_VER, 2);
    605UFS_DEVICE_DESC_PARAM(number_of_secure_wpa, _NUM_SEC_WPA, 1);
    606UFS_DEVICE_DESC_PARAM(psa_max_data_size, _PSA_MAX_DATA, 4);
    607UFS_DEVICE_DESC_PARAM(psa_state_timeout, _PSA_TMT, 1);
    608UFS_DEVICE_DESC_PARAM(hpb_version, _HPB_VER, 2);
    609UFS_DEVICE_DESC_PARAM(hpb_control, _HPB_CONTROL, 1);
    610UFS_DEVICE_DESC_PARAM(ext_feature_sup, _EXT_UFS_FEATURE_SUP, 4);
    611UFS_DEVICE_DESC_PARAM(wb_presv_us_en, _WB_PRESRV_USRSPC_EN, 1);
    612UFS_DEVICE_DESC_PARAM(wb_type, _WB_TYPE, 1);
    613UFS_DEVICE_DESC_PARAM(wb_shared_alloc_units, _WB_SHARED_ALLOC_UNITS, 4);
    614
    615static struct attribute *ufs_sysfs_device_descriptor[] = {
    616	&dev_attr_device_type.attr,
    617	&dev_attr_device_class.attr,
    618	&dev_attr_device_sub_class.attr,
    619	&dev_attr_protocol.attr,
    620	&dev_attr_number_of_luns.attr,
    621	&dev_attr_number_of_wluns.attr,
    622	&dev_attr_boot_enable.attr,
    623	&dev_attr_descriptor_access_enable.attr,
    624	&dev_attr_initial_power_mode.attr,
    625	&dev_attr_high_priority_lun.attr,
    626	&dev_attr_secure_removal_type.attr,
    627	&dev_attr_support_security_lun.attr,
    628	&dev_attr_bkops_termination_latency.attr,
    629	&dev_attr_initial_active_icc_level.attr,
    630	&dev_attr_specification_version.attr,
    631	&dev_attr_manufacturing_date.attr,
    632	&dev_attr_manufacturer_id.attr,
    633	&dev_attr_rtt_capability.attr,
    634	&dev_attr_rtc_update.attr,
    635	&dev_attr_ufs_features.attr,
    636	&dev_attr_ffu_timeout.attr,
    637	&dev_attr_queue_depth.attr,
    638	&dev_attr_device_version.attr,
    639	&dev_attr_number_of_secure_wpa.attr,
    640	&dev_attr_psa_max_data_size.attr,
    641	&dev_attr_psa_state_timeout.attr,
    642	&dev_attr_hpb_version.attr,
    643	&dev_attr_hpb_control.attr,
    644	&dev_attr_ext_feature_sup.attr,
    645	&dev_attr_wb_presv_us_en.attr,
    646	&dev_attr_wb_type.attr,
    647	&dev_attr_wb_shared_alloc_units.attr,
    648	NULL,
    649};
    650
    651static const struct attribute_group ufs_sysfs_device_descriptor_group = {
    652	.name = "device_descriptor",
    653	.attrs = ufs_sysfs_device_descriptor,
    654};
    655
    656#define UFS_INTERCONNECT_DESC_PARAM(_name, _uname, _size)		\
    657	UFS_DESC_PARAM(_name, _uname, INTERCONNECT, _size)
    658
    659UFS_INTERCONNECT_DESC_PARAM(unipro_version, _UNIPRO_VER, 2);
    660UFS_INTERCONNECT_DESC_PARAM(mphy_version, _MPHY_VER, 2);
    661
    662static struct attribute *ufs_sysfs_interconnect_descriptor[] = {
    663	&dev_attr_unipro_version.attr,
    664	&dev_attr_mphy_version.attr,
    665	NULL,
    666};
    667
    668static const struct attribute_group ufs_sysfs_interconnect_descriptor_group = {
    669	.name = "interconnect_descriptor",
    670	.attrs = ufs_sysfs_interconnect_descriptor,
    671};
    672
    673#define UFS_GEOMETRY_DESC_PARAM(_name, _uname, _size)			\
    674	UFS_DESC_PARAM(_name, _uname, GEOMETRY, _size)
    675
    676UFS_GEOMETRY_DESC_PARAM(raw_device_capacity, _DEV_CAP, 8);
    677UFS_GEOMETRY_DESC_PARAM(max_number_of_luns, _MAX_NUM_LUN, 1);
    678UFS_GEOMETRY_DESC_PARAM(segment_size, _SEG_SIZE, 4);
    679UFS_GEOMETRY_DESC_PARAM(allocation_unit_size, _ALLOC_UNIT_SIZE, 1);
    680UFS_GEOMETRY_DESC_PARAM(min_addressable_block_size, _MIN_BLK_SIZE, 1);
    681UFS_GEOMETRY_DESC_PARAM(optimal_read_block_size, _OPT_RD_BLK_SIZE, 1);
    682UFS_GEOMETRY_DESC_PARAM(optimal_write_block_size, _OPT_WR_BLK_SIZE, 1);
    683UFS_GEOMETRY_DESC_PARAM(max_in_buffer_size, _MAX_IN_BUF_SIZE, 1);
    684UFS_GEOMETRY_DESC_PARAM(max_out_buffer_size, _MAX_OUT_BUF_SIZE, 1);
    685UFS_GEOMETRY_DESC_PARAM(rpmb_rw_size, _RPMB_RW_SIZE, 1);
    686UFS_GEOMETRY_DESC_PARAM(dyn_capacity_resource_policy, _DYN_CAP_RSRC_PLC, 1);
    687UFS_GEOMETRY_DESC_PARAM(data_ordering, _DATA_ORDER, 1);
    688UFS_GEOMETRY_DESC_PARAM(max_number_of_contexts, _MAX_NUM_CTX, 1);
    689UFS_GEOMETRY_DESC_PARAM(sys_data_tag_unit_size, _TAG_UNIT_SIZE, 1);
    690UFS_GEOMETRY_DESC_PARAM(sys_data_tag_resource_size, _TAG_RSRC_SIZE, 1);
    691UFS_GEOMETRY_DESC_PARAM(secure_removal_types, _SEC_RM_TYPES, 1);
    692UFS_GEOMETRY_DESC_PARAM(memory_types, _MEM_TYPES, 2);
    693UFS_GEOMETRY_DESC_PARAM(sys_code_memory_max_alloc_units,
    694	_SCM_MAX_NUM_UNITS, 4);
    695UFS_GEOMETRY_DESC_PARAM(sys_code_memory_capacity_adjustment_factor,
    696	_SCM_CAP_ADJ_FCTR, 2);
    697UFS_GEOMETRY_DESC_PARAM(non_persist_memory_max_alloc_units,
    698	_NPM_MAX_NUM_UNITS, 4);
    699UFS_GEOMETRY_DESC_PARAM(non_persist_memory_capacity_adjustment_factor,
    700	_NPM_CAP_ADJ_FCTR, 2);
    701UFS_GEOMETRY_DESC_PARAM(enh1_memory_max_alloc_units,
    702	_ENM1_MAX_NUM_UNITS, 4);
    703UFS_GEOMETRY_DESC_PARAM(enh1_memory_capacity_adjustment_factor,
    704	_ENM1_CAP_ADJ_FCTR, 2);
    705UFS_GEOMETRY_DESC_PARAM(enh2_memory_max_alloc_units,
    706	_ENM2_MAX_NUM_UNITS, 4);
    707UFS_GEOMETRY_DESC_PARAM(enh2_memory_capacity_adjustment_factor,
    708	_ENM2_CAP_ADJ_FCTR, 2);
    709UFS_GEOMETRY_DESC_PARAM(enh3_memory_max_alloc_units,
    710	_ENM3_MAX_NUM_UNITS, 4);
    711UFS_GEOMETRY_DESC_PARAM(enh3_memory_capacity_adjustment_factor,
    712	_ENM3_CAP_ADJ_FCTR, 2);
    713UFS_GEOMETRY_DESC_PARAM(enh4_memory_max_alloc_units,
    714	_ENM4_MAX_NUM_UNITS, 4);
    715UFS_GEOMETRY_DESC_PARAM(enh4_memory_capacity_adjustment_factor,
    716	_ENM4_CAP_ADJ_FCTR, 2);
    717UFS_GEOMETRY_DESC_PARAM(hpb_region_size, _HPB_REGION_SIZE, 1);
    718UFS_GEOMETRY_DESC_PARAM(hpb_number_lu, _HPB_NUMBER_LU, 1);
    719UFS_GEOMETRY_DESC_PARAM(hpb_subregion_size, _HPB_SUBREGION_SIZE, 1);
    720UFS_GEOMETRY_DESC_PARAM(hpb_max_active_regions, _HPB_MAX_ACTIVE_REGS, 2);
    721UFS_GEOMETRY_DESC_PARAM(wb_max_alloc_units, _WB_MAX_ALLOC_UNITS, 4);
    722UFS_GEOMETRY_DESC_PARAM(wb_max_wb_luns, _WB_MAX_WB_LUNS, 1);
    723UFS_GEOMETRY_DESC_PARAM(wb_buff_cap_adj, _WB_BUFF_CAP_ADJ, 1);
    724UFS_GEOMETRY_DESC_PARAM(wb_sup_red_type, _WB_SUP_RED_TYPE, 1);
    725UFS_GEOMETRY_DESC_PARAM(wb_sup_wb_type, _WB_SUP_WB_TYPE, 1);
    726
    727
    728static struct attribute *ufs_sysfs_geometry_descriptor[] = {
    729	&dev_attr_raw_device_capacity.attr,
    730	&dev_attr_max_number_of_luns.attr,
    731	&dev_attr_segment_size.attr,
    732	&dev_attr_allocation_unit_size.attr,
    733	&dev_attr_min_addressable_block_size.attr,
    734	&dev_attr_optimal_read_block_size.attr,
    735	&dev_attr_optimal_write_block_size.attr,
    736	&dev_attr_max_in_buffer_size.attr,
    737	&dev_attr_max_out_buffer_size.attr,
    738	&dev_attr_rpmb_rw_size.attr,
    739	&dev_attr_dyn_capacity_resource_policy.attr,
    740	&dev_attr_data_ordering.attr,
    741	&dev_attr_max_number_of_contexts.attr,
    742	&dev_attr_sys_data_tag_unit_size.attr,
    743	&dev_attr_sys_data_tag_resource_size.attr,
    744	&dev_attr_secure_removal_types.attr,
    745	&dev_attr_memory_types.attr,
    746	&dev_attr_sys_code_memory_max_alloc_units.attr,
    747	&dev_attr_sys_code_memory_capacity_adjustment_factor.attr,
    748	&dev_attr_non_persist_memory_max_alloc_units.attr,
    749	&dev_attr_non_persist_memory_capacity_adjustment_factor.attr,
    750	&dev_attr_enh1_memory_max_alloc_units.attr,
    751	&dev_attr_enh1_memory_capacity_adjustment_factor.attr,
    752	&dev_attr_enh2_memory_max_alloc_units.attr,
    753	&dev_attr_enh2_memory_capacity_adjustment_factor.attr,
    754	&dev_attr_enh3_memory_max_alloc_units.attr,
    755	&dev_attr_enh3_memory_capacity_adjustment_factor.attr,
    756	&dev_attr_enh4_memory_max_alloc_units.attr,
    757	&dev_attr_enh4_memory_capacity_adjustment_factor.attr,
    758	&dev_attr_hpb_region_size.attr,
    759	&dev_attr_hpb_number_lu.attr,
    760	&dev_attr_hpb_subregion_size.attr,
    761	&dev_attr_hpb_max_active_regions.attr,
    762	&dev_attr_wb_max_alloc_units.attr,
    763	&dev_attr_wb_max_wb_luns.attr,
    764	&dev_attr_wb_buff_cap_adj.attr,
    765	&dev_attr_wb_sup_red_type.attr,
    766	&dev_attr_wb_sup_wb_type.attr,
    767	NULL,
    768};
    769
    770static const struct attribute_group ufs_sysfs_geometry_descriptor_group = {
    771	.name = "geometry_descriptor",
    772	.attrs = ufs_sysfs_geometry_descriptor,
    773};
    774
    775#define UFS_HEALTH_DESC_PARAM(_name, _uname, _size)			\
    776	UFS_DESC_PARAM(_name, _uname, HEALTH, _size)
    777
    778UFS_HEALTH_DESC_PARAM(eol_info, _EOL_INFO, 1);
    779UFS_HEALTH_DESC_PARAM(life_time_estimation_a, _LIFE_TIME_EST_A, 1);
    780UFS_HEALTH_DESC_PARAM(life_time_estimation_b, _LIFE_TIME_EST_B, 1);
    781
    782static struct attribute *ufs_sysfs_health_descriptor[] = {
    783	&dev_attr_eol_info.attr,
    784	&dev_attr_life_time_estimation_a.attr,
    785	&dev_attr_life_time_estimation_b.attr,
    786	NULL,
    787};
    788
    789static const struct attribute_group ufs_sysfs_health_descriptor_group = {
    790	.name = "health_descriptor",
    791	.attrs = ufs_sysfs_health_descriptor,
    792};
    793
    794#define UFS_POWER_DESC_PARAM(_name, _uname, _index)			\
    795static ssize_t _name##_index##_show(struct device *dev,			\
    796	struct device_attribute *attr, char *buf)			\
    797{									\
    798	struct ufs_hba *hba = dev_get_drvdata(dev);			\
    799	return ufs_sysfs_read_desc_param(hba, QUERY_DESC_IDN_POWER, 0,	\
    800		PWR_DESC##_uname##_0 + _index * 2, buf, 2);		\
    801}									\
    802static DEVICE_ATTR_RO(_name##_index)
    803
    804UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 0);
    805UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 1);
    806UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 2);
    807UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 3);
    808UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 4);
    809UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 5);
    810UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 6);
    811UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 7);
    812UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 8);
    813UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 9);
    814UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 10);
    815UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 11);
    816UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 12);
    817UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 13);
    818UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 14);
    819UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 15);
    820UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 0);
    821UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 1);
    822UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 2);
    823UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 3);
    824UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 4);
    825UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 5);
    826UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 6);
    827UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 7);
    828UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 8);
    829UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 9);
    830UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 10);
    831UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 11);
    832UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 12);
    833UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 13);
    834UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 14);
    835UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 15);
    836UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 0);
    837UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 1);
    838UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 2);
    839UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 3);
    840UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 4);
    841UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 5);
    842UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 6);
    843UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 7);
    844UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 8);
    845UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 9);
    846UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 10);
    847UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 11);
    848UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 12);
    849UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 13);
    850UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 14);
    851UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 15);
    852
    853static struct attribute *ufs_sysfs_power_descriptor[] = {
    854	&dev_attr_active_icc_levels_vcc0.attr,
    855	&dev_attr_active_icc_levels_vcc1.attr,
    856	&dev_attr_active_icc_levels_vcc2.attr,
    857	&dev_attr_active_icc_levels_vcc3.attr,
    858	&dev_attr_active_icc_levels_vcc4.attr,
    859	&dev_attr_active_icc_levels_vcc5.attr,
    860	&dev_attr_active_icc_levels_vcc6.attr,
    861	&dev_attr_active_icc_levels_vcc7.attr,
    862	&dev_attr_active_icc_levels_vcc8.attr,
    863	&dev_attr_active_icc_levels_vcc9.attr,
    864	&dev_attr_active_icc_levels_vcc10.attr,
    865	&dev_attr_active_icc_levels_vcc11.attr,
    866	&dev_attr_active_icc_levels_vcc12.attr,
    867	&dev_attr_active_icc_levels_vcc13.attr,
    868	&dev_attr_active_icc_levels_vcc14.attr,
    869	&dev_attr_active_icc_levels_vcc15.attr,
    870	&dev_attr_active_icc_levels_vccq0.attr,
    871	&dev_attr_active_icc_levels_vccq1.attr,
    872	&dev_attr_active_icc_levels_vccq2.attr,
    873	&dev_attr_active_icc_levels_vccq3.attr,
    874	&dev_attr_active_icc_levels_vccq4.attr,
    875	&dev_attr_active_icc_levels_vccq5.attr,
    876	&dev_attr_active_icc_levels_vccq6.attr,
    877	&dev_attr_active_icc_levels_vccq7.attr,
    878	&dev_attr_active_icc_levels_vccq8.attr,
    879	&dev_attr_active_icc_levels_vccq9.attr,
    880	&dev_attr_active_icc_levels_vccq10.attr,
    881	&dev_attr_active_icc_levels_vccq11.attr,
    882	&dev_attr_active_icc_levels_vccq12.attr,
    883	&dev_attr_active_icc_levels_vccq13.attr,
    884	&dev_attr_active_icc_levels_vccq14.attr,
    885	&dev_attr_active_icc_levels_vccq15.attr,
    886	&dev_attr_active_icc_levels_vccq20.attr,
    887	&dev_attr_active_icc_levels_vccq21.attr,
    888	&dev_attr_active_icc_levels_vccq22.attr,
    889	&dev_attr_active_icc_levels_vccq23.attr,
    890	&dev_attr_active_icc_levels_vccq24.attr,
    891	&dev_attr_active_icc_levels_vccq25.attr,
    892	&dev_attr_active_icc_levels_vccq26.attr,
    893	&dev_attr_active_icc_levels_vccq27.attr,
    894	&dev_attr_active_icc_levels_vccq28.attr,
    895	&dev_attr_active_icc_levels_vccq29.attr,
    896	&dev_attr_active_icc_levels_vccq210.attr,
    897	&dev_attr_active_icc_levels_vccq211.attr,
    898	&dev_attr_active_icc_levels_vccq212.attr,
    899	&dev_attr_active_icc_levels_vccq213.attr,
    900	&dev_attr_active_icc_levels_vccq214.attr,
    901	&dev_attr_active_icc_levels_vccq215.attr,
    902	NULL,
    903};
    904
    905static const struct attribute_group ufs_sysfs_power_descriptor_group = {
    906	.name = "power_descriptor",
    907	.attrs = ufs_sysfs_power_descriptor,
    908};
    909
    910#define UFS_STRING_DESCRIPTOR(_name, _pname)				\
    911static ssize_t _name##_show(struct device *dev,				\
    912	struct device_attribute *attr, char *buf)			\
    913{									\
    914	u8 index;							\
    915	struct ufs_hba *hba = dev_get_drvdata(dev);			\
    916	int ret;							\
    917	int desc_len = QUERY_DESC_MAX_SIZE;				\
    918	u8 *desc_buf;							\
    919									\
    920	down(&hba->host_sem);						\
    921	if (!ufshcd_is_user_access_allowed(hba)) {			\
    922		up(&hba->host_sem);					\
    923		return -EBUSY;						\
    924	}								\
    925	desc_buf = kzalloc(QUERY_DESC_MAX_SIZE, GFP_ATOMIC);		\
    926	if (!desc_buf) {						\
    927		up(&hba->host_sem);					\
    928		return -ENOMEM;						\
    929	}								\
    930	ufshcd_rpm_get_sync(hba);					\
    931	ret = ufshcd_query_descriptor_retry(hba,			\
    932		UPIU_QUERY_OPCODE_READ_DESC, QUERY_DESC_IDN_DEVICE,	\
    933		0, 0, desc_buf, &desc_len);				\
    934	if (ret) {							\
    935		ret = -EINVAL;						\
    936		goto out;						\
    937	}								\
    938	index = desc_buf[DEVICE_DESC_PARAM##_pname];			\
    939	kfree(desc_buf);						\
    940	desc_buf = NULL;						\
    941	ret = ufshcd_read_string_desc(hba, index, &desc_buf,		\
    942				      SD_ASCII_STD);			\
    943	if (ret < 0)							\
    944		goto out;						\
    945	ret = sysfs_emit(buf, "%s\n", desc_buf);			\
    946out:									\
    947	ufshcd_rpm_put_sync(hba);					\
    948	kfree(desc_buf);						\
    949	up(&hba->host_sem);						\
    950	return ret;							\
    951}									\
    952static DEVICE_ATTR_RO(_name)
    953
    954UFS_STRING_DESCRIPTOR(manufacturer_name, _MANF_NAME);
    955UFS_STRING_DESCRIPTOR(product_name, _PRDCT_NAME);
    956UFS_STRING_DESCRIPTOR(oem_id, _OEM_ID);
    957UFS_STRING_DESCRIPTOR(serial_number, _SN);
    958UFS_STRING_DESCRIPTOR(product_revision, _PRDCT_REV);
    959
    960static struct attribute *ufs_sysfs_string_descriptors[] = {
    961	&dev_attr_manufacturer_name.attr,
    962	&dev_attr_product_name.attr,
    963	&dev_attr_oem_id.attr,
    964	&dev_attr_serial_number.attr,
    965	&dev_attr_product_revision.attr,
    966	NULL,
    967};
    968
    969static const struct attribute_group ufs_sysfs_string_descriptors_group = {
    970	.name = "string_descriptors",
    971	.attrs = ufs_sysfs_string_descriptors,
    972};
    973
    974static inline bool ufshcd_is_wb_flags(enum flag_idn idn)
    975{
    976	return idn >= QUERY_FLAG_IDN_WB_EN &&
    977		idn <= QUERY_FLAG_IDN_WB_BUFF_FLUSH_DURING_HIBERN8;
    978}
    979
    980#define UFS_FLAG(_name, _uname)						\
    981static ssize_t _name##_show(struct device *dev,				\
    982	struct device_attribute *attr, char *buf)			\
    983{									\
    984	bool flag;							\
    985	u8 index = 0;							\
    986	int ret;							\
    987	struct ufs_hba *hba = dev_get_drvdata(dev);			\
    988									\
    989	down(&hba->host_sem);						\
    990	if (!ufshcd_is_user_access_allowed(hba)) {			\
    991		up(&hba->host_sem);					\
    992		return -EBUSY;						\
    993	}								\
    994	if (ufshcd_is_wb_flags(QUERY_FLAG_IDN##_uname))			\
    995		index = ufshcd_wb_get_query_index(hba);			\
    996	ufshcd_rpm_get_sync(hba);					\
    997	ret = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_READ_FLAG,	\
    998		QUERY_FLAG_IDN##_uname, index, &flag);			\
    999	ufshcd_rpm_put_sync(hba);					\
   1000	if (ret) {							\
   1001		ret = -EINVAL;						\
   1002		goto out;						\
   1003	}								\
   1004	ret = sysfs_emit(buf, "%s\n", flag ? "true" : "false");		\
   1005out:									\
   1006	up(&hba->host_sem);						\
   1007	return ret;							\
   1008}									\
   1009static DEVICE_ATTR_RO(_name)
   1010
   1011UFS_FLAG(device_init, _FDEVICEINIT);
   1012UFS_FLAG(permanent_wpe, _PERMANENT_WPE);
   1013UFS_FLAG(power_on_wpe, _PWR_ON_WPE);
   1014UFS_FLAG(bkops_enable, _BKOPS_EN);
   1015UFS_FLAG(life_span_mode_enable, _LIFE_SPAN_MODE_ENABLE);
   1016UFS_FLAG(phy_resource_removal, _FPHYRESOURCEREMOVAL);
   1017UFS_FLAG(busy_rtc, _BUSY_RTC);
   1018UFS_FLAG(disable_fw_update, _PERMANENTLY_DISABLE_FW_UPDATE);
   1019UFS_FLAG(wb_enable, _WB_EN);
   1020UFS_FLAG(wb_flush_en, _WB_BUFF_FLUSH_EN);
   1021UFS_FLAG(wb_flush_during_h8, _WB_BUFF_FLUSH_DURING_HIBERN8);
   1022UFS_FLAG(hpb_enable, _HPB_EN);
   1023
   1024static struct attribute *ufs_sysfs_device_flags[] = {
   1025	&dev_attr_device_init.attr,
   1026	&dev_attr_permanent_wpe.attr,
   1027	&dev_attr_power_on_wpe.attr,
   1028	&dev_attr_bkops_enable.attr,
   1029	&dev_attr_life_span_mode_enable.attr,
   1030	&dev_attr_phy_resource_removal.attr,
   1031	&dev_attr_busy_rtc.attr,
   1032	&dev_attr_disable_fw_update.attr,
   1033	&dev_attr_wb_enable.attr,
   1034	&dev_attr_wb_flush_en.attr,
   1035	&dev_attr_wb_flush_during_h8.attr,
   1036	&dev_attr_hpb_enable.attr,
   1037	NULL,
   1038};
   1039
   1040static const struct attribute_group ufs_sysfs_flags_group = {
   1041	.name = "flags",
   1042	.attrs = ufs_sysfs_device_flags,
   1043};
   1044
   1045static inline bool ufshcd_is_wb_attrs(enum attr_idn idn)
   1046{
   1047	return idn >= QUERY_ATTR_IDN_WB_FLUSH_STATUS &&
   1048		idn <= QUERY_ATTR_IDN_CURR_WB_BUFF_SIZE;
   1049}
   1050
   1051#define UFS_ATTRIBUTE(_name, _uname)					\
   1052static ssize_t _name##_show(struct device *dev,				\
   1053	struct device_attribute *attr, char *buf)			\
   1054{									\
   1055	struct ufs_hba *hba = dev_get_drvdata(dev);			\
   1056	u32 value;							\
   1057	int ret;							\
   1058	u8 index = 0;							\
   1059									\
   1060	down(&hba->host_sem);						\
   1061	if (!ufshcd_is_user_access_allowed(hba)) {			\
   1062		up(&hba->host_sem);					\
   1063		return -EBUSY;						\
   1064	}								\
   1065	if (ufshcd_is_wb_attrs(QUERY_ATTR_IDN##_uname))			\
   1066		index = ufshcd_wb_get_query_index(hba);			\
   1067	ufshcd_rpm_get_sync(hba);					\
   1068	ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,	\
   1069		QUERY_ATTR_IDN##_uname, index, 0, &value);		\
   1070	ufshcd_rpm_put_sync(hba);					\
   1071	if (ret) {							\
   1072		ret = -EINVAL;						\
   1073		goto out;						\
   1074	}								\
   1075	ret = sysfs_emit(buf, "0x%08X\n", value);			\
   1076out:									\
   1077	up(&hba->host_sem);						\
   1078	return ret;							\
   1079}									\
   1080static DEVICE_ATTR_RO(_name)
   1081
   1082UFS_ATTRIBUTE(boot_lun_enabled, _BOOT_LU_EN);
   1083UFS_ATTRIBUTE(max_data_size_hpb_single_cmd, _MAX_HPB_SINGLE_CMD);
   1084UFS_ATTRIBUTE(current_power_mode, _POWER_MODE);
   1085UFS_ATTRIBUTE(active_icc_level, _ACTIVE_ICC_LVL);
   1086UFS_ATTRIBUTE(ooo_data_enabled, _OOO_DATA_EN);
   1087UFS_ATTRIBUTE(bkops_status, _BKOPS_STATUS);
   1088UFS_ATTRIBUTE(purge_status, _PURGE_STATUS);
   1089UFS_ATTRIBUTE(max_data_in_size, _MAX_DATA_IN);
   1090UFS_ATTRIBUTE(max_data_out_size, _MAX_DATA_OUT);
   1091UFS_ATTRIBUTE(reference_clock_frequency, _REF_CLK_FREQ);
   1092UFS_ATTRIBUTE(configuration_descriptor_lock, _CONF_DESC_LOCK);
   1093UFS_ATTRIBUTE(max_number_of_rtt, _MAX_NUM_OF_RTT);
   1094UFS_ATTRIBUTE(exception_event_control, _EE_CONTROL);
   1095UFS_ATTRIBUTE(exception_event_status, _EE_STATUS);
   1096UFS_ATTRIBUTE(ffu_status, _FFU_STATUS);
   1097UFS_ATTRIBUTE(psa_state, _PSA_STATE);
   1098UFS_ATTRIBUTE(psa_data_size, _PSA_DATA_SIZE);
   1099UFS_ATTRIBUTE(wb_flush_status, _WB_FLUSH_STATUS);
   1100UFS_ATTRIBUTE(wb_avail_buf, _AVAIL_WB_BUFF_SIZE);
   1101UFS_ATTRIBUTE(wb_life_time_est, _WB_BUFF_LIFE_TIME_EST);
   1102UFS_ATTRIBUTE(wb_cur_buf, _CURR_WB_BUFF_SIZE);
   1103
   1104
   1105static struct attribute *ufs_sysfs_attributes[] = {
   1106	&dev_attr_boot_lun_enabled.attr,
   1107	&dev_attr_max_data_size_hpb_single_cmd.attr,
   1108	&dev_attr_current_power_mode.attr,
   1109	&dev_attr_active_icc_level.attr,
   1110	&dev_attr_ooo_data_enabled.attr,
   1111	&dev_attr_bkops_status.attr,
   1112	&dev_attr_purge_status.attr,
   1113	&dev_attr_max_data_in_size.attr,
   1114	&dev_attr_max_data_out_size.attr,
   1115	&dev_attr_reference_clock_frequency.attr,
   1116	&dev_attr_configuration_descriptor_lock.attr,
   1117	&dev_attr_max_number_of_rtt.attr,
   1118	&dev_attr_exception_event_control.attr,
   1119	&dev_attr_exception_event_status.attr,
   1120	&dev_attr_ffu_status.attr,
   1121	&dev_attr_psa_state.attr,
   1122	&dev_attr_psa_data_size.attr,
   1123	&dev_attr_wb_flush_status.attr,
   1124	&dev_attr_wb_avail_buf.attr,
   1125	&dev_attr_wb_life_time_est.attr,
   1126	&dev_attr_wb_cur_buf.attr,
   1127	NULL,
   1128};
   1129
   1130static const struct attribute_group ufs_sysfs_attributes_group = {
   1131	.name = "attributes",
   1132	.attrs = ufs_sysfs_attributes,
   1133};
   1134
   1135static const struct attribute_group *ufs_sysfs_groups[] = {
   1136	&ufs_sysfs_default_group,
   1137	&ufs_sysfs_monitor_group,
   1138	&ufs_sysfs_device_descriptor_group,
   1139	&ufs_sysfs_interconnect_descriptor_group,
   1140	&ufs_sysfs_geometry_descriptor_group,
   1141	&ufs_sysfs_health_descriptor_group,
   1142	&ufs_sysfs_power_descriptor_group,
   1143	&ufs_sysfs_string_descriptors_group,
   1144	&ufs_sysfs_flags_group,
   1145	&ufs_sysfs_attributes_group,
   1146	NULL,
   1147};
   1148
   1149#define UFS_LUN_DESC_PARAM(_pname, _puname, _duname, _size)		\
   1150static ssize_t _pname##_show(struct device *dev,			\
   1151	struct device_attribute *attr, char *buf)			\
   1152{									\
   1153	struct scsi_device *sdev = to_scsi_device(dev);			\
   1154	struct ufs_hba *hba = shost_priv(sdev->host);			\
   1155	u8 lun = ufshcd_scsi_to_upiu_lun(sdev->lun);			\
   1156	if (!ufs_is_valid_unit_desc_lun(&hba->dev_info, lun,		\
   1157				_duname##_DESC_PARAM##_puname))		\
   1158		return -EINVAL;						\
   1159	return ufs_sysfs_read_desc_param(hba, QUERY_DESC_IDN_##_duname,	\
   1160		lun, _duname##_DESC_PARAM##_puname, buf, _size);	\
   1161}									\
   1162static DEVICE_ATTR_RO(_pname)
   1163
   1164#define UFS_UNIT_DESC_PARAM(_name, _uname, _size)			\
   1165	UFS_LUN_DESC_PARAM(_name, _uname, UNIT, _size)
   1166
   1167UFS_UNIT_DESC_PARAM(lu_enable, _LU_ENABLE, 1);
   1168UFS_UNIT_DESC_PARAM(boot_lun_id, _BOOT_LUN_ID, 1);
   1169UFS_UNIT_DESC_PARAM(lun_write_protect, _LU_WR_PROTECT, 1);
   1170UFS_UNIT_DESC_PARAM(lun_queue_depth, _LU_Q_DEPTH, 1);
   1171UFS_UNIT_DESC_PARAM(psa_sensitive, _PSA_SENSITIVE, 1);
   1172UFS_UNIT_DESC_PARAM(lun_memory_type, _MEM_TYPE, 1);
   1173UFS_UNIT_DESC_PARAM(data_reliability, _DATA_RELIABILITY, 1);
   1174UFS_UNIT_DESC_PARAM(logical_block_size, _LOGICAL_BLK_SIZE, 1);
   1175UFS_UNIT_DESC_PARAM(logical_block_count, _LOGICAL_BLK_COUNT, 8);
   1176UFS_UNIT_DESC_PARAM(erase_block_size, _ERASE_BLK_SIZE, 4);
   1177UFS_UNIT_DESC_PARAM(provisioning_type, _PROVISIONING_TYPE, 1);
   1178UFS_UNIT_DESC_PARAM(physical_memory_resourse_count, _PHY_MEM_RSRC_CNT, 8);
   1179UFS_UNIT_DESC_PARAM(context_capabilities, _CTX_CAPABILITIES, 2);
   1180UFS_UNIT_DESC_PARAM(large_unit_granularity, _LARGE_UNIT_SIZE_M1, 1);
   1181UFS_UNIT_DESC_PARAM(hpb_lu_max_active_regions, _HPB_LU_MAX_ACTIVE_RGNS, 2);
   1182UFS_UNIT_DESC_PARAM(hpb_pinned_region_start_offset, _HPB_PIN_RGN_START_OFF, 2);
   1183UFS_UNIT_DESC_PARAM(hpb_number_pinned_regions, _HPB_NUM_PIN_RGNS, 2);
   1184UFS_UNIT_DESC_PARAM(wb_buf_alloc_units, _WB_BUF_ALLOC_UNITS, 4);
   1185
   1186static struct attribute *ufs_sysfs_unit_descriptor[] = {
   1187	&dev_attr_lu_enable.attr,
   1188	&dev_attr_boot_lun_id.attr,
   1189	&dev_attr_lun_write_protect.attr,
   1190	&dev_attr_lun_queue_depth.attr,
   1191	&dev_attr_psa_sensitive.attr,
   1192	&dev_attr_lun_memory_type.attr,
   1193	&dev_attr_data_reliability.attr,
   1194	&dev_attr_logical_block_size.attr,
   1195	&dev_attr_logical_block_count.attr,
   1196	&dev_attr_erase_block_size.attr,
   1197	&dev_attr_provisioning_type.attr,
   1198	&dev_attr_physical_memory_resourse_count.attr,
   1199	&dev_attr_context_capabilities.attr,
   1200	&dev_attr_large_unit_granularity.attr,
   1201	&dev_attr_hpb_lu_max_active_regions.attr,
   1202	&dev_attr_hpb_pinned_region_start_offset.attr,
   1203	&dev_attr_hpb_number_pinned_regions.attr,
   1204	&dev_attr_wb_buf_alloc_units.attr,
   1205	NULL,
   1206};
   1207
   1208const struct attribute_group ufs_sysfs_unit_descriptor_group = {
   1209	.name = "unit_descriptor",
   1210	.attrs = ufs_sysfs_unit_descriptor,
   1211};
   1212
   1213static ssize_t dyn_cap_needed_attribute_show(struct device *dev,
   1214	struct device_attribute *attr, char *buf)
   1215{
   1216	u32 value;
   1217	struct scsi_device *sdev = to_scsi_device(dev);
   1218	struct ufs_hba *hba = shost_priv(sdev->host);
   1219	u8 lun = ufshcd_scsi_to_upiu_lun(sdev->lun);
   1220	int ret;
   1221
   1222	down(&hba->host_sem);
   1223	if (!ufshcd_is_user_access_allowed(hba)) {
   1224		ret = -EBUSY;
   1225		goto out;
   1226	}
   1227
   1228	ufshcd_rpm_get_sync(hba);
   1229	ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
   1230		QUERY_ATTR_IDN_DYN_CAP_NEEDED, lun, 0, &value);
   1231	ufshcd_rpm_put_sync(hba);
   1232	if (ret) {
   1233		ret = -EINVAL;
   1234		goto out;
   1235	}
   1236
   1237	ret = sysfs_emit(buf, "0x%08X\n", value);
   1238
   1239out:
   1240	up(&hba->host_sem);
   1241	return ret;
   1242}
   1243static DEVICE_ATTR_RO(dyn_cap_needed_attribute);
   1244
   1245static struct attribute *ufs_sysfs_lun_attributes[] = {
   1246	&dev_attr_dyn_cap_needed_attribute.attr,
   1247	NULL,
   1248};
   1249
   1250const struct attribute_group ufs_sysfs_lun_attributes_group = {
   1251	.attrs = ufs_sysfs_lun_attributes,
   1252};
   1253
   1254void ufs_sysfs_add_nodes(struct device *dev)
   1255{
   1256	int ret;
   1257
   1258	ret = sysfs_create_groups(&dev->kobj, ufs_sysfs_groups);
   1259	if (ret)
   1260		dev_err(dev,
   1261			"%s: sysfs groups creation failed (err = %d)\n",
   1262			__func__, ret);
   1263}
   1264
   1265void ufs_sysfs_remove_nodes(struct device *dev)
   1266{
   1267	sysfs_remove_groups(&dev->kobj, ufs_sysfs_groups);
   1268}