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