• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.h"
10 #include "ufs-sysfs.h"
11 
ufschd_uic_link_state_to_string(enum uic_link_state state)12 static const char *ufschd_uic_link_state_to_string(
13 			enum uic_link_state state)
14 {
15 	switch (state) {
16 	case UIC_LINK_OFF_STATE:	return "OFF";
17 	case UIC_LINK_ACTIVE_STATE:	return "ACTIVE";
18 	case UIC_LINK_HIBERN8_STATE:	return "HIBERN8";
19 	case UIC_LINK_BROKEN_STATE:	return "BROKEN";
20 	default:			return "UNKNOWN";
21 	}
22 }
23 
ufschd_ufs_dev_pwr_mode_to_string(enum ufs_dev_pwr_mode state)24 static const char *ufschd_ufs_dev_pwr_mode_to_string(
25 			enum ufs_dev_pwr_mode state)
26 {
27 	switch (state) {
28 	case UFS_ACTIVE_PWR_MODE:	return "ACTIVE";
29 	case UFS_SLEEP_PWR_MODE:	return "SLEEP";
30 	case UFS_POWERDOWN_PWR_MODE:	return "POWERDOWN";
31 	default:			return "UNKNOWN";
32 	}
33 }
34 
ufs_sysfs_pm_lvl_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count,bool rpm)35 static inline ssize_t ufs_sysfs_pm_lvl_store(struct device *dev,
36 					     struct device_attribute *attr,
37 					     const char *buf, size_t count,
38 					     bool rpm)
39 {
40 	struct ufs_hba *hba = dev_get_drvdata(dev);
41 	unsigned long flags, value;
42 
43 	if (kstrtoul(buf, 0, &value))
44 		return -EINVAL;
45 
46 	if (value >= UFS_PM_LVL_MAX)
47 		return -EINVAL;
48 
49 	spin_lock_irqsave(hba->host->host_lock, flags);
50 	if (rpm)
51 		hba->rpm_lvl = value;
52 	else
53 		hba->spm_lvl = value;
54 	spin_unlock_irqrestore(hba->host->host_lock, flags);
55 	return count;
56 }
57 
rpm_lvl_show(struct device * dev,struct device_attribute * attr,char * buf)58 static ssize_t rpm_lvl_show(struct device *dev,
59 		struct device_attribute *attr, char *buf)
60 {
61 	struct ufs_hba *hba = dev_get_drvdata(dev);
62 
63 	return sprintf(buf, "%d\n", hba->rpm_lvl);
64 }
65 
rpm_lvl_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)66 static ssize_t rpm_lvl_store(struct device *dev,
67 		struct device_attribute *attr, const char *buf, size_t count)
68 {
69 	return ufs_sysfs_pm_lvl_store(dev, attr, buf, count, true);
70 }
71 
rpm_target_dev_state_show(struct device * dev,struct device_attribute * attr,char * buf)72 static ssize_t rpm_target_dev_state_show(struct device *dev,
73 		struct device_attribute *attr, char *buf)
74 {
75 	struct ufs_hba *hba = dev_get_drvdata(dev);
76 
77 	return sprintf(buf, "%s\n", ufschd_ufs_dev_pwr_mode_to_string(
78 			ufs_pm_lvl_states[hba->rpm_lvl].dev_state));
79 }
80 
rpm_target_link_state_show(struct device * dev,struct device_attribute * attr,char * buf)81 static ssize_t rpm_target_link_state_show(struct device *dev,
82 		struct device_attribute *attr, char *buf)
83 {
84 	struct ufs_hba *hba = dev_get_drvdata(dev);
85 
86 	return sprintf(buf, "%s\n", ufschd_uic_link_state_to_string(
87 			ufs_pm_lvl_states[hba->rpm_lvl].link_state));
88 }
89 
spm_lvl_show(struct device * dev,struct device_attribute * attr,char * buf)90 static ssize_t spm_lvl_show(struct device *dev,
91 		struct device_attribute *attr, char *buf)
92 {
93 	struct ufs_hba *hba = dev_get_drvdata(dev);
94 
95 	return sprintf(buf, "%d\n", hba->spm_lvl);
96 }
97 
spm_lvl_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)98 static ssize_t spm_lvl_store(struct device *dev,
99 		struct device_attribute *attr, const char *buf, size_t count)
100 {
101 	return ufs_sysfs_pm_lvl_store(dev, attr, buf, count, false);
102 }
103 
spm_target_dev_state_show(struct device * dev,struct device_attribute * attr,char * buf)104 static ssize_t spm_target_dev_state_show(struct device *dev,
105 		struct device_attribute *attr, char *buf)
106 {
107 	struct ufs_hba *hba = dev_get_drvdata(dev);
108 
109 	return sprintf(buf, "%s\n", ufschd_ufs_dev_pwr_mode_to_string(
110 				ufs_pm_lvl_states[hba->spm_lvl].dev_state));
111 }
112 
spm_target_link_state_show(struct device * dev,struct device_attribute * attr,char * buf)113 static ssize_t spm_target_link_state_show(struct device *dev,
114 		struct device_attribute *attr, char *buf)
115 {
116 	struct ufs_hba *hba = dev_get_drvdata(dev);
117 
118 	return sprintf(buf, "%s\n", ufschd_uic_link_state_to_string(
119 				ufs_pm_lvl_states[hba->spm_lvl].link_state));
120 }
121 
122 /* Convert Auto-Hibernate Idle Timer register value to microseconds */
ufshcd_ahit_to_us(u32 ahit)123 static int ufshcd_ahit_to_us(u32 ahit)
124 {
125 	int timer = FIELD_GET(UFSHCI_AHIBERN8_TIMER_MASK, ahit);
126 	int scale = FIELD_GET(UFSHCI_AHIBERN8_SCALE_MASK, ahit);
127 
128 	for (; scale > 0; --scale)
129 		timer *= UFSHCI_AHIBERN8_SCALE_FACTOR;
130 
131 	return timer;
132 }
133 
134 /* Convert microseconds to Auto-Hibernate Idle Timer register value */
ufshcd_us_to_ahit(unsigned int timer)135 static u32 ufshcd_us_to_ahit(unsigned int timer)
136 {
137 	unsigned int scale;
138 
139 	for (scale = 0; timer > UFSHCI_AHIBERN8_TIMER_MASK; ++scale)
140 		timer /= UFSHCI_AHIBERN8_SCALE_FACTOR;
141 
142 	return FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, timer) |
143 	       FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, scale);
144 }
145 
auto_hibern8_show(struct device * dev,struct device_attribute * attr,char * buf)146 static ssize_t auto_hibern8_show(struct device *dev,
147 				 struct device_attribute *attr, char *buf)
148 {
149 	u32 ahit;
150 	struct ufs_hba *hba = dev_get_drvdata(dev);
151 
152 	if (!ufshcd_is_auto_hibern8_supported(hba))
153 		return -EOPNOTSUPP;
154 
155 	pm_runtime_get_sync(hba->dev);
156 	ufshcd_hold(hba, false);
157 	ahit = ufshcd_readl(hba, REG_AUTO_HIBERNATE_IDLE_TIMER);
158 	ufshcd_release(hba);
159 	pm_runtime_put_sync(hba->dev);
160 
161 	return scnprintf(buf, PAGE_SIZE, "%d\n", ufshcd_ahit_to_us(ahit));
162 }
163 
auto_hibern8_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)164 static ssize_t auto_hibern8_store(struct device *dev,
165 				  struct device_attribute *attr,
166 				  const char *buf, size_t count)
167 {
168 	struct ufs_hba *hba = dev_get_drvdata(dev);
169 	unsigned int timer;
170 
171 	if (!ufshcd_is_auto_hibern8_supported(hba))
172 		return -EOPNOTSUPP;
173 
174 	if (kstrtouint(buf, 0, &timer))
175 		return -EINVAL;
176 
177 	if (timer > UFSHCI_AHIBERN8_MAX)
178 		return -EINVAL;
179 
180 	ufshcd_auto_hibern8_update(hba, ufshcd_us_to_ahit(timer));
181 
182 	return count;
183 }
184 
185 static DEVICE_ATTR_RW(rpm_lvl);
186 static DEVICE_ATTR_RO(rpm_target_dev_state);
187 static DEVICE_ATTR_RO(rpm_target_link_state);
188 static DEVICE_ATTR_RW(spm_lvl);
189 static DEVICE_ATTR_RO(spm_target_dev_state);
190 static DEVICE_ATTR_RO(spm_target_link_state);
191 static DEVICE_ATTR_RW(auto_hibern8);
192 
193 static struct attribute *ufs_sysfs_ufshcd_attrs[] = {
194 	&dev_attr_rpm_lvl.attr,
195 	&dev_attr_rpm_target_dev_state.attr,
196 	&dev_attr_rpm_target_link_state.attr,
197 	&dev_attr_spm_lvl.attr,
198 	&dev_attr_spm_target_dev_state.attr,
199 	&dev_attr_spm_target_link_state.attr,
200 	&dev_attr_auto_hibern8.attr,
201 	NULL
202 };
203 
204 static const struct attribute_group ufs_sysfs_default_group = {
205 	.attrs = ufs_sysfs_ufshcd_attrs,
206 };
207 
ufs_sysfs_read_desc_param(struct ufs_hba * hba,enum desc_idn desc_id,u8 desc_index,u8 param_offset,u8 * sysfs_buf,u8 param_size)208 static ssize_t ufs_sysfs_read_desc_param(struct ufs_hba *hba,
209 				  enum desc_idn desc_id,
210 				  u8 desc_index,
211 				  u8 param_offset,
212 				  u8 *sysfs_buf,
213 				  u8 param_size)
214 {
215 	u8 desc_buf[8] = {0};
216 	int ret;
217 
218 	if (param_size > 8)
219 		return -EINVAL;
220 
221 	pm_runtime_get_sync(hba->dev);
222 	ret = ufshcd_read_desc_param(hba, desc_id, desc_index,
223 				param_offset, desc_buf, param_size);
224 	pm_runtime_put_sync(hba->dev);
225 	if (ret)
226 		return -EINVAL;
227 	switch (param_size) {
228 	case 1:
229 		ret = sprintf(sysfs_buf, "0x%02X\n", *desc_buf);
230 		break;
231 	case 2:
232 		ret = sprintf(sysfs_buf, "0x%04X\n",
233 			get_unaligned_be16(desc_buf));
234 		break;
235 	case 4:
236 		ret = sprintf(sysfs_buf, "0x%08X\n",
237 			get_unaligned_be32(desc_buf));
238 		break;
239 	case 8:
240 		ret = sprintf(sysfs_buf, "0x%016llX\n",
241 			get_unaligned_be64(desc_buf));
242 		break;
243 	}
244 
245 	return ret;
246 }
247 
248 #define UFS_DESC_PARAM(_name, _puname, _duname, _size)			\
249 static ssize_t _name##_show(struct device *dev,				\
250 	struct device_attribute *attr, char *buf)			\
251 {									\
252 	struct ufs_hba *hba = dev_get_drvdata(dev);			\
253 	return ufs_sysfs_read_desc_param(hba, QUERY_DESC_IDN_##_duname,	\
254 		0, _duname##_DESC_PARAM##_puname, buf, _size);		\
255 }									\
256 static DEVICE_ATTR_RO(_name)
257 
258 #define UFS_DEVICE_DESC_PARAM(_name, _uname, _size)			\
259 	UFS_DESC_PARAM(_name, _uname, DEVICE, _size)
260 
261 UFS_DEVICE_DESC_PARAM(device_type, _DEVICE_TYPE, 1);
262 UFS_DEVICE_DESC_PARAM(device_class, _DEVICE_CLASS, 1);
263 UFS_DEVICE_DESC_PARAM(device_sub_class, _DEVICE_SUB_CLASS, 1);
264 UFS_DEVICE_DESC_PARAM(protocol, _PRTCL, 1);
265 UFS_DEVICE_DESC_PARAM(number_of_luns, _NUM_LU, 1);
266 UFS_DEVICE_DESC_PARAM(number_of_wluns, _NUM_WLU, 1);
267 UFS_DEVICE_DESC_PARAM(boot_enable, _BOOT_ENBL, 1);
268 UFS_DEVICE_DESC_PARAM(descriptor_access_enable, _DESC_ACCSS_ENBL, 1);
269 UFS_DEVICE_DESC_PARAM(initial_power_mode, _INIT_PWR_MODE, 1);
270 UFS_DEVICE_DESC_PARAM(high_priority_lun, _HIGH_PR_LUN, 1);
271 UFS_DEVICE_DESC_PARAM(secure_removal_type, _SEC_RMV_TYPE, 1);
272 UFS_DEVICE_DESC_PARAM(support_security_lun, _SEC_LU, 1);
273 UFS_DEVICE_DESC_PARAM(bkops_termination_latency, _BKOP_TERM_LT, 1);
274 UFS_DEVICE_DESC_PARAM(initial_active_icc_level, _ACTVE_ICC_LVL, 1);
275 UFS_DEVICE_DESC_PARAM(specification_version, _SPEC_VER, 2);
276 UFS_DEVICE_DESC_PARAM(manufacturing_date, _MANF_DATE, 2);
277 UFS_DEVICE_DESC_PARAM(manufacturer_id, _MANF_ID, 2);
278 UFS_DEVICE_DESC_PARAM(rtt_capability, _RTT_CAP, 1);
279 UFS_DEVICE_DESC_PARAM(rtc_update, _FRQ_RTC, 2);
280 UFS_DEVICE_DESC_PARAM(ufs_features, _UFS_FEAT, 1);
281 UFS_DEVICE_DESC_PARAM(ffu_timeout, _FFU_TMT, 1);
282 UFS_DEVICE_DESC_PARAM(queue_depth, _Q_DPTH, 1);
283 UFS_DEVICE_DESC_PARAM(device_version, _DEV_VER, 2);
284 UFS_DEVICE_DESC_PARAM(number_of_secure_wpa, _NUM_SEC_WPA, 1);
285 UFS_DEVICE_DESC_PARAM(psa_max_data_size, _PSA_MAX_DATA, 4);
286 UFS_DEVICE_DESC_PARAM(psa_state_timeout, _PSA_TMT, 1);
287 UFS_DEVICE_DESC_PARAM(ext_feature_sup, _EXT_UFS_FEATURE_SUP, 4);
288 UFS_DEVICE_DESC_PARAM(wb_presv_us_en, _WB_PRESRV_USRSPC_EN, 1);
289 UFS_DEVICE_DESC_PARAM(wb_type, _WB_TYPE, 1);
290 UFS_DEVICE_DESC_PARAM(wb_shared_alloc_units, _WB_SHARED_ALLOC_UNITS, 4);
291 
292 static struct attribute *ufs_sysfs_device_descriptor[] = {
293 	&dev_attr_device_type.attr,
294 	&dev_attr_device_class.attr,
295 	&dev_attr_device_sub_class.attr,
296 	&dev_attr_protocol.attr,
297 	&dev_attr_number_of_luns.attr,
298 	&dev_attr_number_of_wluns.attr,
299 	&dev_attr_boot_enable.attr,
300 	&dev_attr_descriptor_access_enable.attr,
301 	&dev_attr_initial_power_mode.attr,
302 	&dev_attr_high_priority_lun.attr,
303 	&dev_attr_secure_removal_type.attr,
304 	&dev_attr_support_security_lun.attr,
305 	&dev_attr_bkops_termination_latency.attr,
306 	&dev_attr_initial_active_icc_level.attr,
307 	&dev_attr_specification_version.attr,
308 	&dev_attr_manufacturing_date.attr,
309 	&dev_attr_manufacturer_id.attr,
310 	&dev_attr_rtt_capability.attr,
311 	&dev_attr_rtc_update.attr,
312 	&dev_attr_ufs_features.attr,
313 	&dev_attr_ffu_timeout.attr,
314 	&dev_attr_queue_depth.attr,
315 	&dev_attr_device_version.attr,
316 	&dev_attr_number_of_secure_wpa.attr,
317 	&dev_attr_psa_max_data_size.attr,
318 	&dev_attr_psa_state_timeout.attr,
319 	&dev_attr_ext_feature_sup.attr,
320 	&dev_attr_wb_presv_us_en.attr,
321 	&dev_attr_wb_type.attr,
322 	&dev_attr_wb_shared_alloc_units.attr,
323 	NULL,
324 };
325 
326 static const struct attribute_group ufs_sysfs_device_descriptor_group = {
327 	.name = "device_descriptor",
328 	.attrs = ufs_sysfs_device_descriptor,
329 };
330 
331 #define UFS_INTERCONNECT_DESC_PARAM(_name, _uname, _size)		\
332 	UFS_DESC_PARAM(_name, _uname, INTERCONNECT, _size)
333 
334 UFS_INTERCONNECT_DESC_PARAM(unipro_version, _UNIPRO_VER, 2);
335 UFS_INTERCONNECT_DESC_PARAM(mphy_version, _MPHY_VER, 2);
336 
337 static struct attribute *ufs_sysfs_interconnect_descriptor[] = {
338 	&dev_attr_unipro_version.attr,
339 	&dev_attr_mphy_version.attr,
340 	NULL,
341 };
342 
343 static const struct attribute_group ufs_sysfs_interconnect_descriptor_group = {
344 	.name = "interconnect_descriptor",
345 	.attrs = ufs_sysfs_interconnect_descriptor,
346 };
347 
348 #define UFS_GEOMETRY_DESC_PARAM(_name, _uname, _size)			\
349 	UFS_DESC_PARAM(_name, _uname, GEOMETRY, _size)
350 
351 UFS_GEOMETRY_DESC_PARAM(raw_device_capacity, _DEV_CAP, 8);
352 UFS_GEOMETRY_DESC_PARAM(max_number_of_luns, _MAX_NUM_LUN, 1);
353 UFS_GEOMETRY_DESC_PARAM(segment_size, _SEG_SIZE, 4);
354 UFS_GEOMETRY_DESC_PARAM(allocation_unit_size, _ALLOC_UNIT_SIZE, 1);
355 UFS_GEOMETRY_DESC_PARAM(min_addressable_block_size, _MIN_BLK_SIZE, 1);
356 UFS_GEOMETRY_DESC_PARAM(optimal_read_block_size, _OPT_RD_BLK_SIZE, 1);
357 UFS_GEOMETRY_DESC_PARAM(optimal_write_block_size, _OPT_WR_BLK_SIZE, 1);
358 UFS_GEOMETRY_DESC_PARAM(max_in_buffer_size, _MAX_IN_BUF_SIZE, 1);
359 UFS_GEOMETRY_DESC_PARAM(max_out_buffer_size, _MAX_OUT_BUF_SIZE, 1);
360 UFS_GEOMETRY_DESC_PARAM(rpmb_rw_size, _RPMB_RW_SIZE, 1);
361 UFS_GEOMETRY_DESC_PARAM(dyn_capacity_resource_policy, _DYN_CAP_RSRC_PLC, 1);
362 UFS_GEOMETRY_DESC_PARAM(data_ordering, _DATA_ORDER, 1);
363 UFS_GEOMETRY_DESC_PARAM(max_number_of_contexts, _MAX_NUM_CTX, 1);
364 UFS_GEOMETRY_DESC_PARAM(sys_data_tag_unit_size, _TAG_UNIT_SIZE, 1);
365 UFS_GEOMETRY_DESC_PARAM(sys_data_tag_resource_size, _TAG_RSRC_SIZE, 1);
366 UFS_GEOMETRY_DESC_PARAM(secure_removal_types, _SEC_RM_TYPES, 1);
367 UFS_GEOMETRY_DESC_PARAM(memory_types, _MEM_TYPES, 2);
368 UFS_GEOMETRY_DESC_PARAM(sys_code_memory_max_alloc_units,
369 	_SCM_MAX_NUM_UNITS, 4);
370 UFS_GEOMETRY_DESC_PARAM(sys_code_memory_capacity_adjustment_factor,
371 	_SCM_CAP_ADJ_FCTR, 2);
372 UFS_GEOMETRY_DESC_PARAM(non_persist_memory_max_alloc_units,
373 	_NPM_MAX_NUM_UNITS, 4);
374 UFS_GEOMETRY_DESC_PARAM(non_persist_memory_capacity_adjustment_factor,
375 	_NPM_CAP_ADJ_FCTR, 2);
376 UFS_GEOMETRY_DESC_PARAM(enh1_memory_max_alloc_units,
377 	_ENM1_MAX_NUM_UNITS, 4);
378 UFS_GEOMETRY_DESC_PARAM(enh1_memory_capacity_adjustment_factor,
379 	_ENM1_CAP_ADJ_FCTR, 2);
380 UFS_GEOMETRY_DESC_PARAM(enh2_memory_max_alloc_units,
381 	_ENM2_MAX_NUM_UNITS, 4);
382 UFS_GEOMETRY_DESC_PARAM(enh2_memory_capacity_adjustment_factor,
383 	_ENM2_CAP_ADJ_FCTR, 2);
384 UFS_GEOMETRY_DESC_PARAM(enh3_memory_max_alloc_units,
385 	_ENM3_MAX_NUM_UNITS, 4);
386 UFS_GEOMETRY_DESC_PARAM(enh3_memory_capacity_adjustment_factor,
387 	_ENM3_CAP_ADJ_FCTR, 2);
388 UFS_GEOMETRY_DESC_PARAM(enh4_memory_max_alloc_units,
389 	_ENM4_MAX_NUM_UNITS, 4);
390 UFS_GEOMETRY_DESC_PARAM(enh4_memory_capacity_adjustment_factor,
391 	_ENM4_CAP_ADJ_FCTR, 2);
392 UFS_GEOMETRY_DESC_PARAM(wb_max_alloc_units, _WB_MAX_ALLOC_UNITS, 4);
393 UFS_GEOMETRY_DESC_PARAM(wb_max_wb_luns, _WB_MAX_WB_LUNS, 1);
394 UFS_GEOMETRY_DESC_PARAM(wb_buff_cap_adj, _WB_BUFF_CAP_ADJ, 1);
395 UFS_GEOMETRY_DESC_PARAM(wb_sup_red_type, _WB_SUP_RED_TYPE, 1);
396 UFS_GEOMETRY_DESC_PARAM(wb_sup_wb_type, _WB_SUP_WB_TYPE, 1);
397 
398 
399 static struct attribute *ufs_sysfs_geometry_descriptor[] = {
400 	&dev_attr_raw_device_capacity.attr,
401 	&dev_attr_max_number_of_luns.attr,
402 	&dev_attr_segment_size.attr,
403 	&dev_attr_allocation_unit_size.attr,
404 	&dev_attr_min_addressable_block_size.attr,
405 	&dev_attr_optimal_read_block_size.attr,
406 	&dev_attr_optimal_write_block_size.attr,
407 	&dev_attr_max_in_buffer_size.attr,
408 	&dev_attr_max_out_buffer_size.attr,
409 	&dev_attr_rpmb_rw_size.attr,
410 	&dev_attr_dyn_capacity_resource_policy.attr,
411 	&dev_attr_data_ordering.attr,
412 	&dev_attr_max_number_of_contexts.attr,
413 	&dev_attr_sys_data_tag_unit_size.attr,
414 	&dev_attr_sys_data_tag_resource_size.attr,
415 	&dev_attr_secure_removal_types.attr,
416 	&dev_attr_memory_types.attr,
417 	&dev_attr_sys_code_memory_max_alloc_units.attr,
418 	&dev_attr_sys_code_memory_capacity_adjustment_factor.attr,
419 	&dev_attr_non_persist_memory_max_alloc_units.attr,
420 	&dev_attr_non_persist_memory_capacity_adjustment_factor.attr,
421 	&dev_attr_enh1_memory_max_alloc_units.attr,
422 	&dev_attr_enh1_memory_capacity_adjustment_factor.attr,
423 	&dev_attr_enh2_memory_max_alloc_units.attr,
424 	&dev_attr_enh2_memory_capacity_adjustment_factor.attr,
425 	&dev_attr_enh3_memory_max_alloc_units.attr,
426 	&dev_attr_enh3_memory_capacity_adjustment_factor.attr,
427 	&dev_attr_enh4_memory_max_alloc_units.attr,
428 	&dev_attr_enh4_memory_capacity_adjustment_factor.attr,
429 	&dev_attr_wb_max_alloc_units.attr,
430 	&dev_attr_wb_max_wb_luns.attr,
431 	&dev_attr_wb_buff_cap_adj.attr,
432 	&dev_attr_wb_sup_red_type.attr,
433 	&dev_attr_wb_sup_wb_type.attr,
434 	NULL,
435 };
436 
437 static const struct attribute_group ufs_sysfs_geometry_descriptor_group = {
438 	.name = "geometry_descriptor",
439 	.attrs = ufs_sysfs_geometry_descriptor,
440 };
441 
442 #define UFS_HEALTH_DESC_PARAM(_name, _uname, _size)			\
443 	UFS_DESC_PARAM(_name, _uname, HEALTH, _size)
444 
445 UFS_HEALTH_DESC_PARAM(eol_info, _EOL_INFO, 1);
446 UFS_HEALTH_DESC_PARAM(life_time_estimation_a, _LIFE_TIME_EST_A, 1);
447 UFS_HEALTH_DESC_PARAM(life_time_estimation_b, _LIFE_TIME_EST_B, 1);
448 
449 static struct attribute *ufs_sysfs_health_descriptor[] = {
450 	&dev_attr_eol_info.attr,
451 	&dev_attr_life_time_estimation_a.attr,
452 	&dev_attr_life_time_estimation_b.attr,
453 	NULL,
454 };
455 
456 static const struct attribute_group ufs_sysfs_health_descriptor_group = {
457 	.name = "health_descriptor",
458 	.attrs = ufs_sysfs_health_descriptor,
459 };
460 
461 #define UFS_POWER_DESC_PARAM(_name, _uname, _index)			\
462 static ssize_t _name##_index##_show(struct device *dev,			\
463 	struct device_attribute *attr, char *buf)			\
464 {									\
465 	struct ufs_hba *hba = dev_get_drvdata(dev);			\
466 	return ufs_sysfs_read_desc_param(hba, QUERY_DESC_IDN_POWER, 0,	\
467 		PWR_DESC##_uname##_0 + _index * 2, buf, 2);		\
468 }									\
469 static DEVICE_ATTR_RO(_name##_index)
470 
471 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 0);
472 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 1);
473 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 2);
474 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 3);
475 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 4);
476 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 5);
477 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 6);
478 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 7);
479 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 8);
480 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 9);
481 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 10);
482 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 11);
483 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 12);
484 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 13);
485 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 14);
486 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 15);
487 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 0);
488 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 1);
489 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 2);
490 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 3);
491 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 4);
492 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 5);
493 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 6);
494 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 7);
495 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 8);
496 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 9);
497 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 10);
498 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 11);
499 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 12);
500 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 13);
501 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 14);
502 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 15);
503 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 0);
504 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 1);
505 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 2);
506 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 3);
507 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 4);
508 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 5);
509 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 6);
510 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 7);
511 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 8);
512 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 9);
513 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 10);
514 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 11);
515 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 12);
516 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 13);
517 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 14);
518 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 15);
519 
520 static struct attribute *ufs_sysfs_power_descriptor[] = {
521 	&dev_attr_active_icc_levels_vcc0.attr,
522 	&dev_attr_active_icc_levels_vcc1.attr,
523 	&dev_attr_active_icc_levels_vcc2.attr,
524 	&dev_attr_active_icc_levels_vcc3.attr,
525 	&dev_attr_active_icc_levels_vcc4.attr,
526 	&dev_attr_active_icc_levels_vcc5.attr,
527 	&dev_attr_active_icc_levels_vcc6.attr,
528 	&dev_attr_active_icc_levels_vcc7.attr,
529 	&dev_attr_active_icc_levels_vcc8.attr,
530 	&dev_attr_active_icc_levels_vcc9.attr,
531 	&dev_attr_active_icc_levels_vcc10.attr,
532 	&dev_attr_active_icc_levels_vcc11.attr,
533 	&dev_attr_active_icc_levels_vcc12.attr,
534 	&dev_attr_active_icc_levels_vcc13.attr,
535 	&dev_attr_active_icc_levels_vcc14.attr,
536 	&dev_attr_active_icc_levels_vcc15.attr,
537 	&dev_attr_active_icc_levels_vccq0.attr,
538 	&dev_attr_active_icc_levels_vccq1.attr,
539 	&dev_attr_active_icc_levels_vccq2.attr,
540 	&dev_attr_active_icc_levels_vccq3.attr,
541 	&dev_attr_active_icc_levels_vccq4.attr,
542 	&dev_attr_active_icc_levels_vccq5.attr,
543 	&dev_attr_active_icc_levels_vccq6.attr,
544 	&dev_attr_active_icc_levels_vccq7.attr,
545 	&dev_attr_active_icc_levels_vccq8.attr,
546 	&dev_attr_active_icc_levels_vccq9.attr,
547 	&dev_attr_active_icc_levels_vccq10.attr,
548 	&dev_attr_active_icc_levels_vccq11.attr,
549 	&dev_attr_active_icc_levels_vccq12.attr,
550 	&dev_attr_active_icc_levels_vccq13.attr,
551 	&dev_attr_active_icc_levels_vccq14.attr,
552 	&dev_attr_active_icc_levels_vccq15.attr,
553 	&dev_attr_active_icc_levels_vccq20.attr,
554 	&dev_attr_active_icc_levels_vccq21.attr,
555 	&dev_attr_active_icc_levels_vccq22.attr,
556 	&dev_attr_active_icc_levels_vccq23.attr,
557 	&dev_attr_active_icc_levels_vccq24.attr,
558 	&dev_attr_active_icc_levels_vccq25.attr,
559 	&dev_attr_active_icc_levels_vccq26.attr,
560 	&dev_attr_active_icc_levels_vccq27.attr,
561 	&dev_attr_active_icc_levels_vccq28.attr,
562 	&dev_attr_active_icc_levels_vccq29.attr,
563 	&dev_attr_active_icc_levels_vccq210.attr,
564 	&dev_attr_active_icc_levels_vccq211.attr,
565 	&dev_attr_active_icc_levels_vccq212.attr,
566 	&dev_attr_active_icc_levels_vccq213.attr,
567 	&dev_attr_active_icc_levels_vccq214.attr,
568 	&dev_attr_active_icc_levels_vccq215.attr,
569 	NULL,
570 };
571 
572 static const struct attribute_group ufs_sysfs_power_descriptor_group = {
573 	.name = "power_descriptor",
574 	.attrs = ufs_sysfs_power_descriptor,
575 };
576 
577 #define UFS_STRING_DESCRIPTOR(_name, _pname)				\
578 static ssize_t _name##_show(struct device *dev,				\
579 	struct device_attribute *attr, char *buf)			\
580 {									\
581 	u8 index;							\
582 	struct ufs_hba *hba = dev_get_drvdata(dev);			\
583 	int ret;							\
584 	int desc_len = QUERY_DESC_MAX_SIZE;				\
585 	u8 *desc_buf;							\
586 									\
587 	desc_buf = kzalloc(QUERY_DESC_MAX_SIZE, GFP_ATOMIC);		\
588 	if (!desc_buf)                                                  \
589 		return -ENOMEM;                                         \
590 	pm_runtime_get_sync(hba->dev);					\
591 	ret = ufshcd_query_descriptor_retry(hba,			\
592 		UPIU_QUERY_OPCODE_READ_DESC, QUERY_DESC_IDN_DEVICE,	\
593 		0, 0, desc_buf, &desc_len);				\
594 	if (ret) {							\
595 		ret = -EINVAL;						\
596 		goto out;						\
597 	}								\
598 	index = desc_buf[DEVICE_DESC_PARAM##_pname];			\
599 	kfree(desc_buf);						\
600 	desc_buf = NULL;						\
601 	ret = ufshcd_read_string_desc(hba, index, &desc_buf,		\
602 				      SD_ASCII_STD);			\
603 	if (ret < 0)							\
604 		goto out;						\
605 	ret = snprintf(buf, PAGE_SIZE, "%s\n", desc_buf);		\
606 out:									\
607 	pm_runtime_put_sync(hba->dev);					\
608 	kfree(desc_buf);						\
609 	return ret;							\
610 }									\
611 static DEVICE_ATTR_RO(_name)
612 
613 UFS_STRING_DESCRIPTOR(manufacturer_name, _MANF_NAME);
614 UFS_STRING_DESCRIPTOR(product_name, _PRDCT_NAME);
615 UFS_STRING_DESCRIPTOR(oem_id, _OEM_ID);
616 UFS_STRING_DESCRIPTOR(serial_number, _SN);
617 UFS_STRING_DESCRIPTOR(product_revision, _PRDCT_REV);
618 
619 static struct attribute *ufs_sysfs_string_descriptors[] = {
620 	&dev_attr_manufacturer_name.attr,
621 	&dev_attr_product_name.attr,
622 	&dev_attr_oem_id.attr,
623 	&dev_attr_serial_number.attr,
624 	&dev_attr_product_revision.attr,
625 	NULL,
626 };
627 
628 static const struct attribute_group ufs_sysfs_string_descriptors_group = {
629 	.name = "string_descriptors",
630 	.attrs = ufs_sysfs_string_descriptors,
631 };
632 
ufshcd_is_wb_flags(enum flag_idn idn)633 static inline bool ufshcd_is_wb_flags(enum flag_idn idn)
634 {
635 	return ((idn >= QUERY_FLAG_IDN_WB_EN) &&
636 		(idn <= QUERY_FLAG_IDN_WB_BUFF_FLUSH_DURING_HIBERN8));
637 }
638 
639 #define UFS_FLAG(_name, _uname)						\
640 static ssize_t _name##_show(struct device *dev,				\
641 	struct device_attribute *attr, char *buf)			\
642 {									\
643 	bool flag;							\
644 	u8 index = 0;							\
645 	int ret;							\
646 	struct ufs_hba *hba = dev_get_drvdata(dev);			\
647 	if (ufshcd_is_wb_flags(QUERY_FLAG_IDN##_uname))			\
648 		index = ufshcd_wb_get_query_index(hba);			\
649 	pm_runtime_get_sync(hba->dev);					\
650 	ret = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_READ_FLAG,	\
651 		QUERY_FLAG_IDN##_uname, index, &flag);			\
652 	pm_runtime_put_sync(hba->dev);					\
653 	if (ret)							\
654 		return -EINVAL;						\
655 	return sprintf(buf, "%s\n", flag ? "true" : "false"); \
656 }									\
657 static DEVICE_ATTR_RO(_name)
658 
659 UFS_FLAG(device_init, _FDEVICEINIT);
660 UFS_FLAG(permanent_wpe, _PERMANENT_WPE);
661 UFS_FLAG(power_on_wpe, _PWR_ON_WPE);
662 UFS_FLAG(bkops_enable, _BKOPS_EN);
663 UFS_FLAG(life_span_mode_enable, _LIFE_SPAN_MODE_ENABLE);
664 UFS_FLAG(phy_resource_removal, _FPHYRESOURCEREMOVAL);
665 UFS_FLAG(busy_rtc, _BUSY_RTC);
666 UFS_FLAG(disable_fw_update, _PERMANENTLY_DISABLE_FW_UPDATE);
667 UFS_FLAG(wb_enable, _WB_EN);
668 UFS_FLAG(wb_flush_en, _WB_BUFF_FLUSH_EN);
669 UFS_FLAG(wb_flush_during_h8, _WB_BUFF_FLUSH_DURING_HIBERN8);
670 
671 static struct attribute *ufs_sysfs_device_flags[] = {
672 	&dev_attr_device_init.attr,
673 	&dev_attr_permanent_wpe.attr,
674 	&dev_attr_power_on_wpe.attr,
675 	&dev_attr_bkops_enable.attr,
676 	&dev_attr_life_span_mode_enable.attr,
677 	&dev_attr_phy_resource_removal.attr,
678 	&dev_attr_busy_rtc.attr,
679 	&dev_attr_disable_fw_update.attr,
680 	&dev_attr_wb_enable.attr,
681 	&dev_attr_wb_flush_en.attr,
682 	&dev_attr_wb_flush_during_h8.attr,
683 	NULL,
684 };
685 
686 static const struct attribute_group ufs_sysfs_flags_group = {
687 	.name = "flags",
688 	.attrs = ufs_sysfs_device_flags,
689 };
690 
ufshcd_is_wb_attrs(enum attr_idn idn)691 static inline bool ufshcd_is_wb_attrs(enum attr_idn idn)
692 {
693 	return ((idn >= QUERY_ATTR_IDN_WB_FLUSH_STATUS) &&
694 		(idn <= QUERY_ATTR_IDN_CURR_WB_BUFF_SIZE));
695 }
696 
697 #define UFS_ATTRIBUTE(_name, _uname)					\
698 static ssize_t _name##_show(struct device *dev,				\
699 	struct device_attribute *attr, char *buf)			\
700 {									\
701 	struct ufs_hba *hba = dev_get_drvdata(dev);			\
702 	u32 value;							\
703 	int ret;							\
704 	u8 index = 0;							\
705 	if (ufshcd_is_wb_attrs(QUERY_ATTR_IDN##_uname))			\
706 		index = ufshcd_wb_get_query_index(hba);			\
707 	pm_runtime_get_sync(hba->dev);					\
708 	ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,	\
709 		QUERY_ATTR_IDN##_uname, index, 0, &value);		\
710 	pm_runtime_put_sync(hba->dev);					\
711 	if (ret)							\
712 		return -EINVAL;						\
713 	return sprintf(buf, "0x%08X\n", value);				\
714 }									\
715 static DEVICE_ATTR_RO(_name)
716 
717 UFS_ATTRIBUTE(boot_lun_enabled, _BOOT_LU_EN);
718 UFS_ATTRIBUTE(current_power_mode, _POWER_MODE);
719 UFS_ATTRIBUTE(active_icc_level, _ACTIVE_ICC_LVL);
720 UFS_ATTRIBUTE(ooo_data_enabled, _OOO_DATA_EN);
721 UFS_ATTRIBUTE(bkops_status, _BKOPS_STATUS);
722 UFS_ATTRIBUTE(purge_status, _PURGE_STATUS);
723 UFS_ATTRIBUTE(max_data_in_size, _MAX_DATA_IN);
724 UFS_ATTRIBUTE(max_data_out_size, _MAX_DATA_OUT);
725 UFS_ATTRIBUTE(reference_clock_frequency, _REF_CLK_FREQ);
726 UFS_ATTRIBUTE(configuration_descriptor_lock, _CONF_DESC_LOCK);
727 UFS_ATTRIBUTE(max_number_of_rtt, _MAX_NUM_OF_RTT);
728 UFS_ATTRIBUTE(exception_event_control, _EE_CONTROL);
729 UFS_ATTRIBUTE(exception_event_status, _EE_STATUS);
730 UFS_ATTRIBUTE(ffu_status, _FFU_STATUS);
731 UFS_ATTRIBUTE(psa_state, _PSA_STATE);
732 UFS_ATTRIBUTE(psa_data_size, _PSA_DATA_SIZE);
733 UFS_ATTRIBUTE(wb_flush_status, _WB_FLUSH_STATUS);
734 UFS_ATTRIBUTE(wb_avail_buf, _AVAIL_WB_BUFF_SIZE);
735 UFS_ATTRIBUTE(wb_life_time_est, _WB_BUFF_LIFE_TIME_EST);
736 UFS_ATTRIBUTE(wb_cur_buf, _CURR_WB_BUFF_SIZE);
737 
738 
739 static struct attribute *ufs_sysfs_attributes[] = {
740 	&dev_attr_boot_lun_enabled.attr,
741 	&dev_attr_current_power_mode.attr,
742 	&dev_attr_active_icc_level.attr,
743 	&dev_attr_ooo_data_enabled.attr,
744 	&dev_attr_bkops_status.attr,
745 	&dev_attr_purge_status.attr,
746 	&dev_attr_max_data_in_size.attr,
747 	&dev_attr_max_data_out_size.attr,
748 	&dev_attr_reference_clock_frequency.attr,
749 	&dev_attr_configuration_descriptor_lock.attr,
750 	&dev_attr_max_number_of_rtt.attr,
751 	&dev_attr_exception_event_control.attr,
752 	&dev_attr_exception_event_status.attr,
753 	&dev_attr_ffu_status.attr,
754 	&dev_attr_psa_state.attr,
755 	&dev_attr_psa_data_size.attr,
756 	&dev_attr_wb_flush_status.attr,
757 	&dev_attr_wb_avail_buf.attr,
758 	&dev_attr_wb_life_time_est.attr,
759 	&dev_attr_wb_cur_buf.attr,
760 	NULL,
761 };
762 
763 static const struct attribute_group ufs_sysfs_attributes_group = {
764 	.name = "attributes",
765 	.attrs = ufs_sysfs_attributes,
766 };
767 
768 static const struct attribute_group *ufs_sysfs_groups[] = {
769 	&ufs_sysfs_default_group,
770 	&ufs_sysfs_device_descriptor_group,
771 	&ufs_sysfs_interconnect_descriptor_group,
772 	&ufs_sysfs_geometry_descriptor_group,
773 	&ufs_sysfs_health_descriptor_group,
774 	&ufs_sysfs_power_descriptor_group,
775 	&ufs_sysfs_string_descriptors_group,
776 	&ufs_sysfs_flags_group,
777 	&ufs_sysfs_attributes_group,
778 	NULL,
779 };
780 
781 #define UFS_LUN_DESC_PARAM(_pname, _puname, _duname, _size)		\
782 static ssize_t _pname##_show(struct device *dev,			\
783 	struct device_attribute *attr, char *buf)			\
784 {									\
785 	struct scsi_device *sdev = to_scsi_device(dev);			\
786 	struct ufs_hba *hba = shost_priv(sdev->host);			\
787 	u8 lun = ufshcd_scsi_to_upiu_lun(sdev->lun);			\
788 	if (!ufs_is_valid_unit_desc_lun(&hba->dev_info, lun,		\
789 				_duname##_DESC_PARAM##_puname))		\
790 		return -EINVAL;						\
791 	return ufs_sysfs_read_desc_param(hba, QUERY_DESC_IDN_##_duname,	\
792 		lun, _duname##_DESC_PARAM##_puname, buf, _size);	\
793 }									\
794 static DEVICE_ATTR_RO(_pname)
795 
796 #define UFS_UNIT_DESC_PARAM(_name, _uname, _size)			\
797 	UFS_LUN_DESC_PARAM(_name, _uname, UNIT, _size)
798 
799 UFS_UNIT_DESC_PARAM(boot_lun_id, _BOOT_LUN_ID, 1);
800 UFS_UNIT_DESC_PARAM(lun_write_protect, _LU_WR_PROTECT, 1);
801 UFS_UNIT_DESC_PARAM(lun_queue_depth, _LU_Q_DEPTH, 1);
802 UFS_UNIT_DESC_PARAM(psa_sensitive, _PSA_SENSITIVE, 1);
803 UFS_UNIT_DESC_PARAM(lun_memory_type, _MEM_TYPE, 1);
804 UFS_UNIT_DESC_PARAM(data_reliability, _DATA_RELIABILITY, 1);
805 UFS_UNIT_DESC_PARAM(logical_block_size, _LOGICAL_BLK_SIZE, 1);
806 UFS_UNIT_DESC_PARAM(logical_block_count, _LOGICAL_BLK_COUNT, 8);
807 UFS_UNIT_DESC_PARAM(erase_block_size, _ERASE_BLK_SIZE, 4);
808 UFS_UNIT_DESC_PARAM(provisioning_type, _PROVISIONING_TYPE, 1);
809 UFS_UNIT_DESC_PARAM(physical_memory_resourse_count, _PHY_MEM_RSRC_CNT, 8);
810 UFS_UNIT_DESC_PARAM(context_capabilities, _CTX_CAPABILITIES, 2);
811 UFS_UNIT_DESC_PARAM(large_unit_granularity, _LARGE_UNIT_SIZE_M1, 1);
812 UFS_UNIT_DESC_PARAM(wb_buf_alloc_units, _WB_BUF_ALLOC_UNITS, 4);
813 
814 
815 static struct attribute *ufs_sysfs_unit_descriptor[] = {
816 	&dev_attr_boot_lun_id.attr,
817 	&dev_attr_lun_write_protect.attr,
818 	&dev_attr_lun_queue_depth.attr,
819 	&dev_attr_psa_sensitive.attr,
820 	&dev_attr_lun_memory_type.attr,
821 	&dev_attr_data_reliability.attr,
822 	&dev_attr_logical_block_size.attr,
823 	&dev_attr_logical_block_count.attr,
824 	&dev_attr_erase_block_size.attr,
825 	&dev_attr_provisioning_type.attr,
826 	&dev_attr_physical_memory_resourse_count.attr,
827 	&dev_attr_context_capabilities.attr,
828 	&dev_attr_large_unit_granularity.attr,
829 	&dev_attr_wb_buf_alloc_units.attr,
830 	NULL,
831 };
832 
833 const struct attribute_group ufs_sysfs_unit_descriptor_group = {
834 	.name = "unit_descriptor",
835 	.attrs = ufs_sysfs_unit_descriptor,
836 };
837 
dyn_cap_needed_attribute_show(struct device * dev,struct device_attribute * attr,char * buf)838 static ssize_t dyn_cap_needed_attribute_show(struct device *dev,
839 	struct device_attribute *attr, char *buf)
840 {
841 	u32 value;
842 	struct scsi_device *sdev = to_scsi_device(dev);
843 	struct ufs_hba *hba = shost_priv(sdev->host);
844 	u8 lun = ufshcd_scsi_to_upiu_lun(sdev->lun);
845 	int ret;
846 
847 	pm_runtime_get_sync(hba->dev);
848 	ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
849 		QUERY_ATTR_IDN_DYN_CAP_NEEDED, lun, 0, &value);
850 	pm_runtime_put_sync(hba->dev);
851 	if (ret)
852 		return -EINVAL;
853 	return sprintf(buf, "0x%08X\n", value);
854 }
855 static DEVICE_ATTR_RO(dyn_cap_needed_attribute);
856 
857 static struct attribute *ufs_sysfs_lun_attributes[] = {
858 	&dev_attr_dyn_cap_needed_attribute.attr,
859 	NULL,
860 };
861 
862 const struct attribute_group ufs_sysfs_lun_attributes_group = {
863 	.attrs = ufs_sysfs_lun_attributes,
864 };
865 
ufs_sysfs_add_nodes(struct device * dev)866 void ufs_sysfs_add_nodes(struct device *dev)
867 {
868 	int ret;
869 
870 	ret = sysfs_create_groups(&dev->kobj, ufs_sysfs_groups);
871 	if (ret)
872 		dev_err(dev,
873 			"%s: sysfs groups creation failed (err = %d)\n",
874 			__func__, ret);
875 }
876 
ufs_sysfs_remove_nodes(struct device * dev)877 void ufs_sysfs_remove_nodes(struct device *dev)
878 {
879 	sysfs_remove_groups(&dev->kobj, ufs_sysfs_groups);
880 }
881