xref: /linux/drivers/ufs/core/ufs-sysfs.c (revision c94cd0248ced494e0bcf82b21380e8037c4dd26b)
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 <linux/unaligned.h>
8 
9 #include <ufs/ufs.h>
10 #include <ufs/unipro.h>
11 #include "ufs-sysfs.h"
12 #include "ufshcd-priv.h"
13 
ufs_pa_pwr_mode_to_string(enum ufs_pa_pwr_mode mode)14 static const char *ufs_pa_pwr_mode_to_string(enum ufs_pa_pwr_mode mode)
15 {
16 	switch (mode) {
17 	case FAST_MODE:		return "FAST_MODE";
18 	case SLOW_MODE:		return "SLOW_MODE";
19 	case FASTAUTO_MODE:	return "FASTAUTO_MODE";
20 	case SLOWAUTO_MODE:	return "SLOWAUTO_MODE";
21 	default:		return "UNKNOWN";
22 	}
23 }
24 
ufs_hs_gear_rate_to_string(enum ufs_hs_gear_rate rate)25 static const char *ufs_hs_gear_rate_to_string(enum ufs_hs_gear_rate rate)
26 {
27 	switch (rate) {
28 	case PA_HS_MODE_A:	return "HS_RATE_A";
29 	case PA_HS_MODE_B:	return "HS_RATE_B";
30 	default:		return "UNKNOWN";
31 	}
32 }
33 
ufs_pwm_gear_to_string(enum ufs_pwm_gear_tag gear)34 static const char *ufs_pwm_gear_to_string(enum ufs_pwm_gear_tag gear)
35 {
36 	switch (gear) {
37 	case UFS_PWM_G1:	return "PWM_GEAR1";
38 	case UFS_PWM_G2:	return "PWM_GEAR2";
39 	case UFS_PWM_G3:	return "PWM_GEAR3";
40 	case UFS_PWM_G4:	return "PWM_GEAR4";
41 	case UFS_PWM_G5:	return "PWM_GEAR5";
42 	case UFS_PWM_G6:	return "PWM_GEAR6";
43 	case UFS_PWM_G7:	return "PWM_GEAR7";
44 	default:		return "UNKNOWN";
45 	}
46 }
47 
ufs_hs_gear_to_string(enum ufs_hs_gear_tag gear)48 static const char *ufs_hs_gear_to_string(enum ufs_hs_gear_tag gear)
49 {
50 	switch (gear) {
51 	case UFS_HS_G1:	return "HS_GEAR1";
52 	case UFS_HS_G2:	return "HS_GEAR2";
53 	case UFS_HS_G3:	return "HS_GEAR3";
54 	case UFS_HS_G4:	return "HS_GEAR4";
55 	case UFS_HS_G5:	return "HS_GEAR5";
56 	default:	return "UNKNOWN";
57 	}
58 }
59 
ufshcd_uic_link_state_to_string(enum uic_link_state state)60 static const char *ufshcd_uic_link_state_to_string(
61 			enum uic_link_state state)
62 {
63 	switch (state) {
64 	case UIC_LINK_OFF_STATE:	return "OFF";
65 	case UIC_LINK_ACTIVE_STATE:	return "ACTIVE";
66 	case UIC_LINK_HIBERN8_STATE:	return "HIBERN8";
67 	case UIC_LINK_BROKEN_STATE:	return "BROKEN";
68 	default:			return "UNKNOWN";
69 	}
70 }
71 
ufshcd_ufs_dev_pwr_mode_to_string(enum ufs_dev_pwr_mode state)72 static const char *ufshcd_ufs_dev_pwr_mode_to_string(
73 			enum ufs_dev_pwr_mode state)
74 {
75 	switch (state) {
76 	case UFS_ACTIVE_PWR_MODE:	return "ACTIVE";
77 	case UFS_SLEEP_PWR_MODE:	return "SLEEP";
78 	case UFS_POWERDOWN_PWR_MODE:	return "POWERDOWN";
79 	case UFS_DEEPSLEEP_PWR_MODE:	return "DEEPSLEEP";
80 	default:			return "UNKNOWN";
81 	}
82 }
83 
ufs_sysfs_pm_lvl_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count,bool rpm)84 static inline ssize_t ufs_sysfs_pm_lvl_store(struct device *dev,
85 					     struct device_attribute *attr,
86 					     const char *buf, size_t count,
87 					     bool rpm)
88 {
89 	struct ufs_hba *hba = dev_get_drvdata(dev);
90 	struct ufs_dev_info *dev_info = &hba->dev_info;
91 	unsigned long flags, value;
92 
93 	if (kstrtoul(buf, 0, &value))
94 		return -EINVAL;
95 
96 	if (value >= UFS_PM_LVL_MAX)
97 		return -EINVAL;
98 
99 	if (ufs_pm_lvl_states[value].dev_state == UFS_DEEPSLEEP_PWR_MODE &&
100 	    (!(hba->caps & UFSHCD_CAP_DEEPSLEEP) ||
101 	     !(dev_info->wspecversion >= 0x310)))
102 		return -EINVAL;
103 
104 	spin_lock_irqsave(hba->host->host_lock, flags);
105 	if (rpm)
106 		hba->rpm_lvl = value;
107 	else
108 		hba->spm_lvl = value;
109 	spin_unlock_irqrestore(hba->host->host_lock, flags);
110 	return count;
111 }
112 
rpm_lvl_show(struct device * dev,struct device_attribute * attr,char * buf)113 static ssize_t rpm_lvl_show(struct device *dev,
114 		struct device_attribute *attr, char *buf)
115 {
116 	struct ufs_hba *hba = dev_get_drvdata(dev);
117 
118 	return sysfs_emit(buf, "%d\n", hba->rpm_lvl);
119 }
120 
rpm_lvl_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)121 static ssize_t rpm_lvl_store(struct device *dev,
122 		struct device_attribute *attr, const char *buf, size_t count)
123 {
124 	return ufs_sysfs_pm_lvl_store(dev, attr, buf, count, true);
125 }
126 
rpm_target_dev_state_show(struct device * dev,struct device_attribute * attr,char * buf)127 static ssize_t rpm_target_dev_state_show(struct device *dev,
128 		struct device_attribute *attr, char *buf)
129 {
130 	struct ufs_hba *hba = dev_get_drvdata(dev);
131 
132 	return sysfs_emit(buf, "%s\n", ufshcd_ufs_dev_pwr_mode_to_string(
133 			ufs_pm_lvl_states[hba->rpm_lvl].dev_state));
134 }
135 
rpm_target_link_state_show(struct device * dev,struct device_attribute * attr,char * buf)136 static ssize_t rpm_target_link_state_show(struct device *dev,
137 		struct device_attribute *attr, char *buf)
138 {
139 	struct ufs_hba *hba = dev_get_drvdata(dev);
140 
141 	return sysfs_emit(buf, "%s\n", ufshcd_uic_link_state_to_string(
142 			ufs_pm_lvl_states[hba->rpm_lvl].link_state));
143 }
144 
spm_lvl_show(struct device * dev,struct device_attribute * attr,char * buf)145 static ssize_t spm_lvl_show(struct device *dev,
146 		struct device_attribute *attr, char *buf)
147 {
148 	struct ufs_hba *hba = dev_get_drvdata(dev);
149 
150 	return sysfs_emit(buf, "%d\n", hba->spm_lvl);
151 }
152 
spm_lvl_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)153 static ssize_t spm_lvl_store(struct device *dev,
154 		struct device_attribute *attr, const char *buf, size_t count)
155 {
156 	return ufs_sysfs_pm_lvl_store(dev, attr, buf, count, false);
157 }
158 
spm_target_dev_state_show(struct device * dev,struct device_attribute * attr,char * buf)159 static ssize_t spm_target_dev_state_show(struct device *dev,
160 		struct device_attribute *attr, char *buf)
161 {
162 	struct ufs_hba *hba = dev_get_drvdata(dev);
163 
164 	return sysfs_emit(buf, "%s\n", ufshcd_ufs_dev_pwr_mode_to_string(
165 				ufs_pm_lvl_states[hba->spm_lvl].dev_state));
166 }
167 
spm_target_link_state_show(struct device * dev,struct device_attribute * attr,char * buf)168 static ssize_t spm_target_link_state_show(struct device *dev,
169 		struct device_attribute *attr, char *buf)
170 {
171 	struct ufs_hba *hba = dev_get_drvdata(dev);
172 
173 	return sysfs_emit(buf, "%s\n", ufshcd_uic_link_state_to_string(
174 				ufs_pm_lvl_states[hba->spm_lvl].link_state));
175 }
176 
177 /* Convert Auto-Hibernate Idle Timer register value to microseconds */
ufshcd_ahit_to_us(u32 ahit)178 static int ufshcd_ahit_to_us(u32 ahit)
179 {
180 	int timer = FIELD_GET(UFSHCI_AHIBERN8_TIMER_MASK, ahit);
181 	int scale = FIELD_GET(UFSHCI_AHIBERN8_SCALE_MASK, ahit);
182 
183 	for (; scale > 0; --scale)
184 		timer *= UFSHCI_AHIBERN8_SCALE_FACTOR;
185 
186 	return timer;
187 }
188 
189 /* Convert microseconds to Auto-Hibernate Idle Timer register value */
ufshcd_us_to_ahit(unsigned int timer)190 static u32 ufshcd_us_to_ahit(unsigned int timer)
191 {
192 	unsigned int scale;
193 
194 	for (scale = 0; timer > UFSHCI_AHIBERN8_TIMER_MASK; ++scale)
195 		timer /= UFSHCI_AHIBERN8_SCALE_FACTOR;
196 
197 	return FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, timer) |
198 	       FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, scale);
199 }
200 
ufshcd_read_hci_reg(struct ufs_hba * hba,u32 * val,unsigned int reg)201 static int ufshcd_read_hci_reg(struct ufs_hba *hba, u32 *val, unsigned int reg)
202 {
203 	down(&hba->host_sem);
204 	if (!ufshcd_is_user_access_allowed(hba)) {
205 		up(&hba->host_sem);
206 		return -EBUSY;
207 	}
208 
209 	ufshcd_rpm_get_sync(hba);
210 	ufshcd_hold(hba);
211 	*val = ufshcd_readl(hba, reg);
212 	ufshcd_release(hba);
213 	ufshcd_rpm_put_sync(hba);
214 
215 	up(&hba->host_sem);
216 	return 0;
217 }
218 
auto_hibern8_show(struct device * dev,struct device_attribute * attr,char * buf)219 static ssize_t auto_hibern8_show(struct device *dev,
220 				 struct device_attribute *attr, char *buf)
221 {
222 	u32 ahit;
223 	int ret;
224 	struct ufs_hba *hba = dev_get_drvdata(dev);
225 
226 	if (!ufshcd_is_auto_hibern8_supported(hba))
227 		return -EOPNOTSUPP;
228 
229 	ret = ufshcd_read_hci_reg(hba, &ahit, REG_AUTO_HIBERNATE_IDLE_TIMER);
230 	if (ret)
231 		return ret;
232 
233 	return sysfs_emit(buf, "%d\n", ufshcd_ahit_to_us(ahit));
234 }
235 
auto_hibern8_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)236 static ssize_t auto_hibern8_store(struct device *dev,
237 				  struct device_attribute *attr,
238 				  const char *buf, size_t count)
239 {
240 	struct ufs_hba *hba = dev_get_drvdata(dev);
241 	unsigned int timer;
242 	int ret = 0;
243 
244 	if (!ufshcd_is_auto_hibern8_supported(hba))
245 		return -EOPNOTSUPP;
246 
247 	if (kstrtouint(buf, 0, &timer))
248 		return -EINVAL;
249 
250 	if (timer > UFSHCI_AHIBERN8_MAX)
251 		return -EINVAL;
252 
253 	down(&hba->host_sem);
254 	if (!ufshcd_is_user_access_allowed(hba)) {
255 		ret = -EBUSY;
256 		goto out;
257 	}
258 
259 	ufshcd_auto_hibern8_update(hba, ufshcd_us_to_ahit(timer));
260 
261 out:
262 	up(&hba->host_sem);
263 	return ret ? ret : count;
264 }
265 
wb_on_show(struct device * dev,struct device_attribute * attr,char * buf)266 static ssize_t wb_on_show(struct device *dev, struct device_attribute *attr,
267 			  char *buf)
268 {
269 	struct ufs_hba *hba = dev_get_drvdata(dev);
270 
271 	return sysfs_emit(buf, "%d\n", hba->dev_info.wb_enabled);
272 }
273 
wb_on_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)274 static ssize_t wb_on_store(struct device *dev, struct device_attribute *attr,
275 			   const char *buf, size_t count)
276 {
277 	struct ufs_hba *hba = dev_get_drvdata(dev);
278 	unsigned int wb_enable;
279 	ssize_t res;
280 
281 	if (!ufshcd_is_wb_allowed(hba) || (ufshcd_is_clkscaling_supported(hba)
282 		&& ufshcd_enable_wb_if_scaling_up(hba))) {
283 		/*
284 		 * If the platform supports UFSHCD_CAP_CLK_SCALING, turn WB
285 		 * on/off will be done while clock scaling up/down.
286 		 */
287 		dev_warn(dev, "It is not allowed to configure WB!\n");
288 		return -EOPNOTSUPP;
289 	}
290 
291 	if (kstrtouint(buf, 0, &wb_enable))
292 		return -EINVAL;
293 
294 	if (wb_enable != 0 && wb_enable != 1)
295 		return -EINVAL;
296 
297 	down(&hba->host_sem);
298 	if (!ufshcd_is_user_access_allowed(hba)) {
299 		res = -EBUSY;
300 		goto out;
301 	}
302 
303 	ufshcd_rpm_get_sync(hba);
304 	res = ufshcd_wb_toggle(hba, wb_enable);
305 	ufshcd_rpm_put_sync(hba);
306 out:
307 	up(&hba->host_sem);
308 	return res < 0 ? res : count;
309 }
310 
rtc_update_ms_show(struct device * dev,struct device_attribute * attr,char * buf)311 static ssize_t rtc_update_ms_show(struct device *dev, struct device_attribute *attr,
312 				  char *buf)
313 {
314 	struct ufs_hba *hba = dev_get_drvdata(dev);
315 
316 	return sysfs_emit(buf, "%d\n", hba->dev_info.rtc_update_period);
317 }
318 
rtc_update_ms_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)319 static ssize_t rtc_update_ms_store(struct device *dev, struct device_attribute *attr,
320 				   const char *buf, size_t count)
321 {
322 	struct ufs_hba *hba = dev_get_drvdata(dev);
323 	unsigned int ms;
324 	bool resume_period_update = false;
325 
326 	if (kstrtouint(buf, 0, &ms))
327 		return -EINVAL;
328 
329 	if (!hba->dev_info.rtc_update_period && ms > 0)
330 		resume_period_update =  true;
331 	/* Minimum and maximum update frequency should be synchronized with all UFS vendors */
332 	hba->dev_info.rtc_update_period = ms;
333 
334 	if (resume_period_update)
335 		schedule_delayed_work(&hba->ufs_rtc_update_work,
336 				      msecs_to_jiffies(hba->dev_info.rtc_update_period));
337 	return count;
338 }
339 
enable_wb_buf_flush_show(struct device * dev,struct device_attribute * attr,char * buf)340 static ssize_t enable_wb_buf_flush_show(struct device *dev,
341 				    struct device_attribute *attr,
342 				    char *buf)
343 {
344 	struct ufs_hba *hba = dev_get_drvdata(dev);
345 
346 	return sysfs_emit(buf, "%d\n", hba->dev_info.wb_buf_flush_enabled);
347 }
348 
enable_wb_buf_flush_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)349 static ssize_t enable_wb_buf_flush_store(struct device *dev,
350 				     struct device_attribute *attr,
351 				     const char *buf, size_t count)
352 {
353 	struct ufs_hba *hba = dev_get_drvdata(dev);
354 	unsigned int enable_wb_buf_flush;
355 	ssize_t res;
356 
357 	if (!ufshcd_is_wb_buf_flush_allowed(hba)) {
358 		dev_warn(dev, "It is not allowed to configure WB buf flushing!\n");
359 		return -EOPNOTSUPP;
360 	}
361 
362 	if (kstrtouint(buf, 0, &enable_wb_buf_flush))
363 		return -EINVAL;
364 
365 	if (enable_wb_buf_flush != 0 && enable_wb_buf_flush != 1)
366 		return -EINVAL;
367 
368 	down(&hba->host_sem);
369 	if (!ufshcd_is_user_access_allowed(hba)) {
370 		res = -EBUSY;
371 		goto out;
372 	}
373 
374 	ufshcd_rpm_get_sync(hba);
375 	res = ufshcd_wb_toggle_buf_flush(hba, enable_wb_buf_flush);
376 	ufshcd_rpm_put_sync(hba);
377 
378 out:
379 	up(&hba->host_sem);
380 	return res < 0 ? res : count;
381 }
382 
wb_flush_threshold_show(struct device * dev,struct device_attribute * attr,char * buf)383 static ssize_t wb_flush_threshold_show(struct device *dev,
384 					 struct device_attribute *attr,
385 					 char *buf)
386 {
387 	struct ufs_hba *hba = dev_get_drvdata(dev);
388 
389 	return sysfs_emit(buf, "%u\n", hba->vps->wb_flush_threshold);
390 }
391 
wb_flush_threshold_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)392 static ssize_t wb_flush_threshold_store(struct device *dev,
393 					  struct device_attribute *attr,
394 					  const char *buf, size_t count)
395 {
396 	struct ufs_hba *hba = dev_get_drvdata(dev);
397 	unsigned int wb_flush_threshold;
398 
399 	if (kstrtouint(buf, 0, &wb_flush_threshold))
400 		return -EINVAL;
401 
402 	/* The range of values for wb_flush_threshold is (0,10] */
403 	if (wb_flush_threshold > UFS_WB_BUF_REMAIN_PERCENT(100) ||
404 	    wb_flush_threshold == 0) {
405 		dev_err(dev, "The value of wb_flush_threshold is invalid!\n");
406 		return -EINVAL;
407 	}
408 
409 	hba->vps->wb_flush_threshold = wb_flush_threshold;
410 
411 	return count;
412 }
413 
414 /**
415  * pm_qos_enable_show - sysfs handler to show pm qos enable value
416  * @dev: device associated with the UFS controller
417  * @attr: sysfs attribute handle
418  * @buf: buffer for sysfs file
419  *
420  * Print 1 if PM QoS feature is enabled, 0 if disabled.
421  *
422  * Returns number of characters written to @buf.
423  */
pm_qos_enable_show(struct device * dev,struct device_attribute * attr,char * buf)424 static ssize_t pm_qos_enable_show(struct device *dev,
425 		struct device_attribute *attr, char *buf)
426 {
427 	struct ufs_hba *hba = dev_get_drvdata(dev);
428 
429 	return sysfs_emit(buf, "%d\n", hba->pm_qos_enabled);
430 }
431 
432 /**
433  * pm_qos_enable_store - sysfs handler to store value
434  * @dev: device associated with the UFS controller
435  * @attr: sysfs attribute handle
436  * @buf: buffer for sysfs file
437  * @count: stores buffer characters count
438  *
439  * Input 0 to disable PM QoS and 1 value to enable.
440  * Default state: 1
441  *
442  * Return: number of characters written to @buf on success, < 0 upon failure.
443  */
pm_qos_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)444 static ssize_t pm_qos_enable_store(struct device *dev,
445 		struct device_attribute *attr, const char *buf, size_t count)
446 {
447 	struct ufs_hba *hba = dev_get_drvdata(dev);
448 	bool value;
449 
450 	if (kstrtobool(buf, &value))
451 		return -EINVAL;
452 
453 	if (value)
454 		ufshcd_pm_qos_init(hba);
455 	else
456 		ufshcd_pm_qos_exit(hba);
457 
458 	return count;
459 }
460 
461 static DEVICE_ATTR_RW(rpm_lvl);
462 static DEVICE_ATTR_RO(rpm_target_dev_state);
463 static DEVICE_ATTR_RO(rpm_target_link_state);
464 static DEVICE_ATTR_RW(spm_lvl);
465 static DEVICE_ATTR_RO(spm_target_dev_state);
466 static DEVICE_ATTR_RO(spm_target_link_state);
467 static DEVICE_ATTR_RW(auto_hibern8);
468 static DEVICE_ATTR_RW(wb_on);
469 static DEVICE_ATTR_RW(enable_wb_buf_flush);
470 static DEVICE_ATTR_RW(wb_flush_threshold);
471 static DEVICE_ATTR_RW(rtc_update_ms);
472 static DEVICE_ATTR_RW(pm_qos_enable);
473 
474 static struct attribute *ufs_sysfs_ufshcd_attrs[] = {
475 	&dev_attr_rpm_lvl.attr,
476 	&dev_attr_rpm_target_dev_state.attr,
477 	&dev_attr_rpm_target_link_state.attr,
478 	&dev_attr_spm_lvl.attr,
479 	&dev_attr_spm_target_dev_state.attr,
480 	&dev_attr_spm_target_link_state.attr,
481 	&dev_attr_auto_hibern8.attr,
482 	&dev_attr_wb_on.attr,
483 	&dev_attr_enable_wb_buf_flush.attr,
484 	&dev_attr_wb_flush_threshold.attr,
485 	&dev_attr_rtc_update_ms.attr,
486 	&dev_attr_pm_qos_enable.attr,
487 	NULL
488 };
489 
490 static const struct attribute_group ufs_sysfs_default_group = {
491 	.attrs = ufs_sysfs_ufshcd_attrs,
492 };
493 
clock_scaling_show(struct device * dev,struct device_attribute * attr,char * buf)494 static ssize_t clock_scaling_show(struct device *dev, struct device_attribute *attr,
495 				  char *buf)
496 {
497 	struct ufs_hba *hba = dev_get_drvdata(dev);
498 
499 	return sysfs_emit(buf, "%d\n", ufshcd_is_clkscaling_supported(hba));
500 }
501 
write_booster_show(struct device * dev,struct device_attribute * attr,char * buf)502 static ssize_t write_booster_show(struct device *dev, struct device_attribute *attr,
503 				  char *buf)
504 {
505 	struct ufs_hba *hba = dev_get_drvdata(dev);
506 
507 	return sysfs_emit(buf, "%d\n", ufshcd_is_wb_allowed(hba));
508 }
509 
510 static DEVICE_ATTR_RO(clock_scaling);
511 static DEVICE_ATTR_RO(write_booster);
512 
513 /*
514  * See Documentation/ABI/testing/sysfs-driver-ufs for the semantics of this
515  * group.
516  */
517 static struct attribute *ufs_sysfs_capabilities_attrs[] = {
518 	&dev_attr_clock_scaling.attr,
519 	&dev_attr_write_booster.attr,
520 	NULL
521 };
522 
523 static const struct attribute_group ufs_sysfs_capabilities_group = {
524 	.name = "capabilities",
525 	.attrs = ufs_sysfs_capabilities_attrs,
526 };
527 
version_show(struct device * dev,struct device_attribute * attr,char * buf)528 static ssize_t version_show(struct device *dev,
529 		struct device_attribute *attr, char *buf)
530 {
531 	struct ufs_hba *hba = dev_get_drvdata(dev);
532 
533 	return sysfs_emit(buf, "0x%x\n", hba->ufs_version);
534 }
535 
product_id_show(struct device * dev,struct device_attribute * attr,char * buf)536 static ssize_t product_id_show(struct device *dev,
537 		struct device_attribute *attr, char *buf)
538 {
539 	int ret;
540 	u32 val;
541 	struct ufs_hba *hba = dev_get_drvdata(dev);
542 
543 	ret = ufshcd_read_hci_reg(hba, &val, REG_CONTROLLER_PID);
544 	if (ret)
545 		return ret;
546 
547 	return sysfs_emit(buf, "0x%x\n", val);
548 }
549 
man_id_show(struct device * dev,struct device_attribute * attr,char * buf)550 static ssize_t man_id_show(struct device *dev,
551 		struct device_attribute *attr, char *buf)
552 {
553 	int ret;
554 	u32 val;
555 	struct ufs_hba *hba = dev_get_drvdata(dev);
556 
557 	ret = ufshcd_read_hci_reg(hba, &val, REG_CONTROLLER_MID);
558 	if (ret)
559 		return ret;
560 
561 	return sysfs_emit(buf, "0x%x\n", val);
562 }
563 
564 static DEVICE_ATTR_RO(version);
565 static DEVICE_ATTR_RO(product_id);
566 static DEVICE_ATTR_RO(man_id);
567 
568 static struct attribute *ufs_sysfs_ufshci_cap_attrs[] = {
569 	&dev_attr_version.attr,
570 	&dev_attr_product_id.attr,
571 	&dev_attr_man_id.attr,
572 	NULL
573 };
574 
575 static const struct attribute_group ufs_sysfs_ufshci_group = {
576 	.name = "ufshci_capabilities",
577 	.attrs = ufs_sysfs_ufshci_cap_attrs,
578 };
579 
monitor_enable_show(struct device * dev,struct device_attribute * attr,char * buf)580 static ssize_t monitor_enable_show(struct device *dev,
581 				   struct device_attribute *attr, char *buf)
582 {
583 	struct ufs_hba *hba = dev_get_drvdata(dev);
584 
585 	return sysfs_emit(buf, "%d\n", hba->monitor.enabled);
586 }
587 
monitor_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)588 static ssize_t monitor_enable_store(struct device *dev,
589 				    struct device_attribute *attr,
590 				    const char *buf, size_t count)
591 {
592 	struct ufs_hba *hba = dev_get_drvdata(dev);
593 	unsigned long value, flags;
594 
595 	if (kstrtoul(buf, 0, &value))
596 		return -EINVAL;
597 
598 	value = !!value;
599 	spin_lock_irqsave(hba->host->host_lock, flags);
600 	if (value == hba->monitor.enabled)
601 		goto out_unlock;
602 
603 	if (!value) {
604 		memset(&hba->monitor, 0, sizeof(hba->monitor));
605 	} else {
606 		hba->monitor.enabled = true;
607 		hba->monitor.enabled_ts = ktime_get();
608 	}
609 
610 out_unlock:
611 	spin_unlock_irqrestore(hba->host->host_lock, flags);
612 	return count;
613 }
614 
monitor_chunk_size_show(struct device * dev,struct device_attribute * attr,char * buf)615 static ssize_t monitor_chunk_size_show(struct device *dev,
616 				   struct device_attribute *attr, char *buf)
617 {
618 	struct ufs_hba *hba = dev_get_drvdata(dev);
619 
620 	return sysfs_emit(buf, "%lu\n", hba->monitor.chunk_size);
621 }
622 
monitor_chunk_size_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)623 static ssize_t monitor_chunk_size_store(struct device *dev,
624 				    struct device_attribute *attr,
625 				    const char *buf, size_t count)
626 {
627 	struct ufs_hba *hba = dev_get_drvdata(dev);
628 	unsigned long value, flags;
629 
630 	if (kstrtoul(buf, 0, &value))
631 		return -EINVAL;
632 
633 	spin_lock_irqsave(hba->host->host_lock, flags);
634 	/* Only allow chunk size change when monitor is disabled */
635 	if (!hba->monitor.enabled)
636 		hba->monitor.chunk_size = value;
637 	spin_unlock_irqrestore(hba->host->host_lock, flags);
638 	return count;
639 }
640 
read_total_sectors_show(struct device * dev,struct device_attribute * attr,char * buf)641 static ssize_t read_total_sectors_show(struct device *dev,
642 				       struct device_attribute *attr, char *buf)
643 {
644 	struct ufs_hba *hba = dev_get_drvdata(dev);
645 
646 	return sysfs_emit(buf, "%lu\n", hba->monitor.nr_sec_rw[READ]);
647 }
648 
read_total_busy_show(struct device * dev,struct device_attribute * attr,char * buf)649 static ssize_t read_total_busy_show(struct device *dev,
650 				    struct device_attribute *attr, char *buf)
651 {
652 	struct ufs_hba *hba = dev_get_drvdata(dev);
653 
654 	return sysfs_emit(buf, "%llu\n",
655 			  ktime_to_us(hba->monitor.total_busy[READ]));
656 }
657 
read_nr_requests_show(struct device * dev,struct device_attribute * attr,char * buf)658 static ssize_t read_nr_requests_show(struct device *dev,
659 				     struct device_attribute *attr, char *buf)
660 {
661 	struct ufs_hba *hba = dev_get_drvdata(dev);
662 
663 	return sysfs_emit(buf, "%lu\n", hba->monitor.nr_req[READ]);
664 }
665 
read_req_latency_avg_show(struct device * dev,struct device_attribute * attr,char * buf)666 static ssize_t read_req_latency_avg_show(struct device *dev,
667 					 struct device_attribute *attr,
668 					 char *buf)
669 {
670 	struct ufs_hba *hba = dev_get_drvdata(dev);
671 	struct ufs_hba_monitor *m = &hba->monitor;
672 
673 	if (!m->nr_req[READ])
674 		return sysfs_emit(buf, "0\n");
675 
676 	return sysfs_emit(buf, "%llu\n", div_u64(ktime_to_us(m->lat_sum[READ]),
677 						 m->nr_req[READ]));
678 }
679 
read_req_latency_max_show(struct device * dev,struct device_attribute * attr,char * buf)680 static ssize_t read_req_latency_max_show(struct device *dev,
681 					 struct device_attribute *attr,
682 					 char *buf)
683 {
684 	struct ufs_hba *hba = dev_get_drvdata(dev);
685 
686 	return sysfs_emit(buf, "%llu\n",
687 			  ktime_to_us(hba->monitor.lat_max[READ]));
688 }
689 
read_req_latency_min_show(struct device * dev,struct device_attribute * attr,char * buf)690 static ssize_t read_req_latency_min_show(struct device *dev,
691 					 struct device_attribute *attr,
692 					 char *buf)
693 {
694 	struct ufs_hba *hba = dev_get_drvdata(dev);
695 
696 	return sysfs_emit(buf, "%llu\n",
697 			  ktime_to_us(hba->monitor.lat_min[READ]));
698 }
699 
read_req_latency_sum_show(struct device * dev,struct device_attribute * attr,char * buf)700 static ssize_t read_req_latency_sum_show(struct device *dev,
701 					 struct device_attribute *attr,
702 					 char *buf)
703 {
704 	struct ufs_hba *hba = dev_get_drvdata(dev);
705 
706 	return sysfs_emit(buf, "%llu\n",
707 			  ktime_to_us(hba->monitor.lat_sum[READ]));
708 }
709 
write_total_sectors_show(struct device * dev,struct device_attribute * attr,char * buf)710 static ssize_t write_total_sectors_show(struct device *dev,
711 					struct device_attribute *attr,
712 					char *buf)
713 {
714 	struct ufs_hba *hba = dev_get_drvdata(dev);
715 
716 	return sysfs_emit(buf, "%lu\n", hba->monitor.nr_sec_rw[WRITE]);
717 }
718 
write_total_busy_show(struct device * dev,struct device_attribute * attr,char * buf)719 static ssize_t write_total_busy_show(struct device *dev,
720 				     struct device_attribute *attr, char *buf)
721 {
722 	struct ufs_hba *hba = dev_get_drvdata(dev);
723 
724 	return sysfs_emit(buf, "%llu\n",
725 			  ktime_to_us(hba->monitor.total_busy[WRITE]));
726 }
727 
write_nr_requests_show(struct device * dev,struct device_attribute * attr,char * buf)728 static ssize_t write_nr_requests_show(struct device *dev,
729 				      struct device_attribute *attr, char *buf)
730 {
731 	struct ufs_hba *hba = dev_get_drvdata(dev);
732 
733 	return sysfs_emit(buf, "%lu\n", hba->monitor.nr_req[WRITE]);
734 }
735 
write_req_latency_avg_show(struct device * dev,struct device_attribute * attr,char * buf)736 static ssize_t write_req_latency_avg_show(struct device *dev,
737 					  struct device_attribute *attr,
738 					  char *buf)
739 {
740 	struct ufs_hba *hba = dev_get_drvdata(dev);
741 	struct ufs_hba_monitor *m = &hba->monitor;
742 
743 	if (!m->nr_req[WRITE])
744 		return sysfs_emit(buf, "0\n");
745 
746 	return sysfs_emit(buf, "%llu\n", div_u64(ktime_to_us(m->lat_sum[WRITE]),
747 						 m->nr_req[WRITE]));
748 }
749 
write_req_latency_max_show(struct device * dev,struct device_attribute * attr,char * buf)750 static ssize_t write_req_latency_max_show(struct device *dev,
751 					  struct device_attribute *attr,
752 					  char *buf)
753 {
754 	struct ufs_hba *hba = dev_get_drvdata(dev);
755 
756 	return sysfs_emit(buf, "%llu\n",
757 			  ktime_to_us(hba->monitor.lat_max[WRITE]));
758 }
759 
write_req_latency_min_show(struct device * dev,struct device_attribute * attr,char * buf)760 static ssize_t write_req_latency_min_show(struct device *dev,
761 					  struct device_attribute *attr,
762 					  char *buf)
763 {
764 	struct ufs_hba *hba = dev_get_drvdata(dev);
765 
766 	return sysfs_emit(buf, "%llu\n",
767 			  ktime_to_us(hba->monitor.lat_min[WRITE]));
768 }
769 
write_req_latency_sum_show(struct device * dev,struct device_attribute * attr,char * buf)770 static ssize_t write_req_latency_sum_show(struct device *dev,
771 					  struct device_attribute *attr,
772 					  char *buf)
773 {
774 	struct ufs_hba *hba = dev_get_drvdata(dev);
775 
776 	return sysfs_emit(buf, "%llu\n",
777 			  ktime_to_us(hba->monitor.lat_sum[WRITE]));
778 }
779 
780 static DEVICE_ATTR_RW(monitor_enable);
781 static DEVICE_ATTR_RW(monitor_chunk_size);
782 static DEVICE_ATTR_RO(read_total_sectors);
783 static DEVICE_ATTR_RO(read_total_busy);
784 static DEVICE_ATTR_RO(read_nr_requests);
785 static DEVICE_ATTR_RO(read_req_latency_avg);
786 static DEVICE_ATTR_RO(read_req_latency_max);
787 static DEVICE_ATTR_RO(read_req_latency_min);
788 static DEVICE_ATTR_RO(read_req_latency_sum);
789 static DEVICE_ATTR_RO(write_total_sectors);
790 static DEVICE_ATTR_RO(write_total_busy);
791 static DEVICE_ATTR_RO(write_nr_requests);
792 static DEVICE_ATTR_RO(write_req_latency_avg);
793 static DEVICE_ATTR_RO(write_req_latency_max);
794 static DEVICE_ATTR_RO(write_req_latency_min);
795 static DEVICE_ATTR_RO(write_req_latency_sum);
796 
797 static struct attribute *ufs_sysfs_monitor_attrs[] = {
798 	&dev_attr_monitor_enable.attr,
799 	&dev_attr_monitor_chunk_size.attr,
800 	&dev_attr_read_total_sectors.attr,
801 	&dev_attr_read_total_busy.attr,
802 	&dev_attr_read_nr_requests.attr,
803 	&dev_attr_read_req_latency_avg.attr,
804 	&dev_attr_read_req_latency_max.attr,
805 	&dev_attr_read_req_latency_min.attr,
806 	&dev_attr_read_req_latency_sum.attr,
807 	&dev_attr_write_total_sectors.attr,
808 	&dev_attr_write_total_busy.attr,
809 	&dev_attr_write_nr_requests.attr,
810 	&dev_attr_write_req_latency_avg.attr,
811 	&dev_attr_write_req_latency_max.attr,
812 	&dev_attr_write_req_latency_min.attr,
813 	&dev_attr_write_req_latency_sum.attr,
814 	NULL
815 };
816 
817 static const struct attribute_group ufs_sysfs_monitor_group = {
818 	.name = "monitor",
819 	.attrs = ufs_sysfs_monitor_attrs,
820 };
821 
lane_show(struct device * dev,struct device_attribute * attr,char * buf)822 static ssize_t lane_show(struct device *dev, struct device_attribute *attr,
823 			 char *buf)
824 {
825 	struct ufs_hba *hba = dev_get_drvdata(dev);
826 
827 	return sysfs_emit(buf, "%u\n", hba->pwr_info.lane_rx);
828 }
829 
mode_show(struct device * dev,struct device_attribute * attr,char * buf)830 static ssize_t mode_show(struct device *dev, struct device_attribute *attr,
831 			 char *buf)
832 {
833 	struct ufs_hba *hba = dev_get_drvdata(dev);
834 
835 	return sysfs_emit(buf, "%s\n", ufs_pa_pwr_mode_to_string(hba->pwr_info.pwr_rx));
836 }
837 
rate_show(struct device * dev,struct device_attribute * attr,char * buf)838 static ssize_t rate_show(struct device *dev, struct device_attribute *attr,
839 			 char *buf)
840 {
841 	struct ufs_hba *hba = dev_get_drvdata(dev);
842 
843 	return sysfs_emit(buf, "%s\n", ufs_hs_gear_rate_to_string(hba->pwr_info.hs_rate));
844 }
845 
gear_show(struct device * dev,struct device_attribute * attr,char * buf)846 static ssize_t gear_show(struct device *dev, struct device_attribute *attr,
847 			 char *buf)
848 {
849 	struct ufs_hba *hba = dev_get_drvdata(dev);
850 
851 	return sysfs_emit(buf, "%s\n", hba->pwr_info.hs_rate ?
852 			  ufs_hs_gear_to_string(hba->pwr_info.gear_rx) :
853 			  ufs_pwm_gear_to_string(hba->pwr_info.gear_rx));
854 }
855 
dev_pm_show(struct device * dev,struct device_attribute * attr,char * buf)856 static ssize_t dev_pm_show(struct device *dev, struct device_attribute *attr,
857 			   char *buf)
858 {
859 	struct ufs_hba *hba = dev_get_drvdata(dev);
860 
861 	return sysfs_emit(buf, "%s\n", ufshcd_ufs_dev_pwr_mode_to_string(hba->curr_dev_pwr_mode));
862 }
863 
link_state_show(struct device * dev,struct device_attribute * attr,char * buf)864 static ssize_t link_state_show(struct device *dev,
865 			       struct device_attribute *attr, char *buf)
866 {
867 	struct ufs_hba *hba = dev_get_drvdata(dev);
868 
869 	return sysfs_emit(buf, "%s\n", ufshcd_uic_link_state_to_string(hba->uic_link_state));
870 }
871 
872 static DEVICE_ATTR_RO(lane);
873 static DEVICE_ATTR_RO(mode);
874 static DEVICE_ATTR_RO(rate);
875 static DEVICE_ATTR_RO(gear);
876 static DEVICE_ATTR_RO(dev_pm);
877 static DEVICE_ATTR_RO(link_state);
878 
879 static struct attribute *ufs_power_info_attrs[] = {
880 	&dev_attr_lane.attr,
881 	&dev_attr_mode.attr,
882 	&dev_attr_rate.attr,
883 	&dev_attr_gear.attr,
884 	&dev_attr_dev_pm.attr,
885 	&dev_attr_link_state.attr,
886 	NULL
887 };
888 
889 static const struct attribute_group ufs_sysfs_power_info_group = {
890 	.name = "power_info",
891 	.attrs = ufs_power_info_attrs,
892 };
893 
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)894 static ssize_t ufs_sysfs_read_desc_param(struct ufs_hba *hba,
895 				  enum desc_idn desc_id,
896 				  u8 desc_index,
897 				  u8 param_offset,
898 				  u8 *sysfs_buf,
899 				  u8 param_size)
900 {
901 	u8 desc_buf[8] = {0};
902 	int ret;
903 
904 	if (param_size > 8)
905 		return -EINVAL;
906 
907 	down(&hba->host_sem);
908 	if (!ufshcd_is_user_access_allowed(hba)) {
909 		ret = -EBUSY;
910 		goto out;
911 	}
912 
913 	ufshcd_rpm_get_sync(hba);
914 	ret = ufshcd_read_desc_param(hba, desc_id, desc_index,
915 				param_offset, desc_buf, param_size);
916 	ufshcd_rpm_put_sync(hba);
917 	if (ret) {
918 		ret = -EINVAL;
919 		goto out;
920 	}
921 
922 	switch (param_size) {
923 	case 1:
924 		ret = sysfs_emit(sysfs_buf, "0x%02X\n", *desc_buf);
925 		break;
926 	case 2:
927 		ret = sysfs_emit(sysfs_buf, "0x%04X\n",
928 			get_unaligned_be16(desc_buf));
929 		break;
930 	case 4:
931 		ret = sysfs_emit(sysfs_buf, "0x%08X\n",
932 			get_unaligned_be32(desc_buf));
933 		break;
934 	case 8:
935 		ret = sysfs_emit(sysfs_buf, "0x%016llX\n",
936 			get_unaligned_be64(desc_buf));
937 		break;
938 	}
939 
940 out:
941 	up(&hba->host_sem);
942 	return ret;
943 }
944 
945 #define UFS_DESC_PARAM(_name, _puname, _duname, _size)			\
946 static ssize_t _name##_show(struct device *dev,				\
947 	struct device_attribute *attr, char *buf)			\
948 {									\
949 	struct ufs_hba *hba = dev_get_drvdata(dev);			\
950 	return ufs_sysfs_read_desc_param(hba, QUERY_DESC_IDN_##_duname,	\
951 		0, _duname##_DESC_PARAM##_puname, buf, _size);		\
952 }									\
953 static DEVICE_ATTR_RO(_name)
954 
955 #define UFS_DEVICE_DESC_PARAM(_name, _uname, _size)			\
956 	UFS_DESC_PARAM(_name, _uname, DEVICE, _size)
957 
958 UFS_DEVICE_DESC_PARAM(device_type, _DEVICE_TYPE, 1);
959 UFS_DEVICE_DESC_PARAM(device_class, _DEVICE_CLASS, 1);
960 UFS_DEVICE_DESC_PARAM(device_sub_class, _DEVICE_SUB_CLASS, 1);
961 UFS_DEVICE_DESC_PARAM(protocol, _PRTCL, 1);
962 UFS_DEVICE_DESC_PARAM(number_of_luns, _NUM_LU, 1);
963 UFS_DEVICE_DESC_PARAM(number_of_wluns, _NUM_WLU, 1);
964 UFS_DEVICE_DESC_PARAM(boot_enable, _BOOT_ENBL, 1);
965 UFS_DEVICE_DESC_PARAM(descriptor_access_enable, _DESC_ACCSS_ENBL, 1);
966 UFS_DEVICE_DESC_PARAM(initial_power_mode, _INIT_PWR_MODE, 1);
967 UFS_DEVICE_DESC_PARAM(high_priority_lun, _HIGH_PR_LUN, 1);
968 UFS_DEVICE_DESC_PARAM(secure_removal_type, _SEC_RMV_TYPE, 1);
969 UFS_DEVICE_DESC_PARAM(support_security_lun, _SEC_LU, 1);
970 UFS_DEVICE_DESC_PARAM(bkops_termination_latency, _BKOP_TERM_LT, 1);
971 UFS_DEVICE_DESC_PARAM(initial_active_icc_level, _ACTVE_ICC_LVL, 1);
972 UFS_DEVICE_DESC_PARAM(specification_version, _SPEC_VER, 2);
973 UFS_DEVICE_DESC_PARAM(manufacturing_date, _MANF_DATE, 2);
974 UFS_DEVICE_DESC_PARAM(manufacturer_id, _MANF_ID, 2);
975 UFS_DEVICE_DESC_PARAM(rtt_capability, _RTT_CAP, 1);
976 UFS_DEVICE_DESC_PARAM(rtc_update, _FRQ_RTC, 2);
977 UFS_DEVICE_DESC_PARAM(ufs_features, _UFS_FEAT, 1);
978 UFS_DEVICE_DESC_PARAM(ffu_timeout, _FFU_TMT, 1);
979 UFS_DEVICE_DESC_PARAM(queue_depth, _Q_DPTH, 1);
980 UFS_DEVICE_DESC_PARAM(device_version, _DEV_VER, 2);
981 UFS_DEVICE_DESC_PARAM(number_of_secure_wpa, _NUM_SEC_WPA, 1);
982 UFS_DEVICE_DESC_PARAM(psa_max_data_size, _PSA_MAX_DATA, 4);
983 UFS_DEVICE_DESC_PARAM(psa_state_timeout, _PSA_TMT, 1);
984 UFS_DEVICE_DESC_PARAM(ext_feature_sup, _EXT_UFS_FEATURE_SUP, 4);
985 UFS_DEVICE_DESC_PARAM(wb_presv_us_en, _WB_PRESRV_USRSPC_EN, 1);
986 UFS_DEVICE_DESC_PARAM(wb_type, _WB_TYPE, 1);
987 UFS_DEVICE_DESC_PARAM(wb_shared_alloc_units, _WB_SHARED_ALLOC_UNITS, 4);
988 
989 static struct attribute *ufs_sysfs_device_descriptor[] = {
990 	&dev_attr_device_type.attr,
991 	&dev_attr_device_class.attr,
992 	&dev_attr_device_sub_class.attr,
993 	&dev_attr_protocol.attr,
994 	&dev_attr_number_of_luns.attr,
995 	&dev_attr_number_of_wluns.attr,
996 	&dev_attr_boot_enable.attr,
997 	&dev_attr_descriptor_access_enable.attr,
998 	&dev_attr_initial_power_mode.attr,
999 	&dev_attr_high_priority_lun.attr,
1000 	&dev_attr_secure_removal_type.attr,
1001 	&dev_attr_support_security_lun.attr,
1002 	&dev_attr_bkops_termination_latency.attr,
1003 	&dev_attr_initial_active_icc_level.attr,
1004 	&dev_attr_specification_version.attr,
1005 	&dev_attr_manufacturing_date.attr,
1006 	&dev_attr_manufacturer_id.attr,
1007 	&dev_attr_rtt_capability.attr,
1008 	&dev_attr_rtc_update.attr,
1009 	&dev_attr_ufs_features.attr,
1010 	&dev_attr_ffu_timeout.attr,
1011 	&dev_attr_queue_depth.attr,
1012 	&dev_attr_device_version.attr,
1013 	&dev_attr_number_of_secure_wpa.attr,
1014 	&dev_attr_psa_max_data_size.attr,
1015 	&dev_attr_psa_state_timeout.attr,
1016 	&dev_attr_ext_feature_sup.attr,
1017 	&dev_attr_wb_presv_us_en.attr,
1018 	&dev_attr_wb_type.attr,
1019 	&dev_attr_wb_shared_alloc_units.attr,
1020 	NULL,
1021 };
1022 
1023 static const struct attribute_group ufs_sysfs_device_descriptor_group = {
1024 	.name = "device_descriptor",
1025 	.attrs = ufs_sysfs_device_descriptor,
1026 };
1027 
1028 #define UFS_INTERCONNECT_DESC_PARAM(_name, _uname, _size)		\
1029 	UFS_DESC_PARAM(_name, _uname, INTERCONNECT, _size)
1030 
1031 UFS_INTERCONNECT_DESC_PARAM(unipro_version, _UNIPRO_VER, 2);
1032 UFS_INTERCONNECT_DESC_PARAM(mphy_version, _MPHY_VER, 2);
1033 
1034 static struct attribute *ufs_sysfs_interconnect_descriptor[] = {
1035 	&dev_attr_unipro_version.attr,
1036 	&dev_attr_mphy_version.attr,
1037 	NULL,
1038 };
1039 
1040 static const struct attribute_group ufs_sysfs_interconnect_descriptor_group = {
1041 	.name = "interconnect_descriptor",
1042 	.attrs = ufs_sysfs_interconnect_descriptor,
1043 };
1044 
1045 #define UFS_GEOMETRY_DESC_PARAM(_name, _uname, _size)			\
1046 	UFS_DESC_PARAM(_name, _uname, GEOMETRY, _size)
1047 
1048 UFS_GEOMETRY_DESC_PARAM(raw_device_capacity, _DEV_CAP, 8);
1049 UFS_GEOMETRY_DESC_PARAM(max_number_of_luns, _MAX_NUM_LUN, 1);
1050 UFS_GEOMETRY_DESC_PARAM(segment_size, _SEG_SIZE, 4);
1051 UFS_GEOMETRY_DESC_PARAM(allocation_unit_size, _ALLOC_UNIT_SIZE, 1);
1052 UFS_GEOMETRY_DESC_PARAM(min_addressable_block_size, _MIN_BLK_SIZE, 1);
1053 UFS_GEOMETRY_DESC_PARAM(optimal_read_block_size, _OPT_RD_BLK_SIZE, 1);
1054 UFS_GEOMETRY_DESC_PARAM(optimal_write_block_size, _OPT_WR_BLK_SIZE, 1);
1055 UFS_GEOMETRY_DESC_PARAM(max_in_buffer_size, _MAX_IN_BUF_SIZE, 1);
1056 UFS_GEOMETRY_DESC_PARAM(max_out_buffer_size, _MAX_OUT_BUF_SIZE, 1);
1057 UFS_GEOMETRY_DESC_PARAM(rpmb_rw_size, _RPMB_RW_SIZE, 1);
1058 UFS_GEOMETRY_DESC_PARAM(dyn_capacity_resource_policy, _DYN_CAP_RSRC_PLC, 1);
1059 UFS_GEOMETRY_DESC_PARAM(data_ordering, _DATA_ORDER, 1);
1060 UFS_GEOMETRY_DESC_PARAM(max_number_of_contexts, _MAX_NUM_CTX, 1);
1061 UFS_GEOMETRY_DESC_PARAM(sys_data_tag_unit_size, _TAG_UNIT_SIZE, 1);
1062 UFS_GEOMETRY_DESC_PARAM(sys_data_tag_resource_size, _TAG_RSRC_SIZE, 1);
1063 UFS_GEOMETRY_DESC_PARAM(secure_removal_types, _SEC_RM_TYPES, 1);
1064 UFS_GEOMETRY_DESC_PARAM(memory_types, _MEM_TYPES, 2);
1065 UFS_GEOMETRY_DESC_PARAM(sys_code_memory_max_alloc_units,
1066 	_SCM_MAX_NUM_UNITS, 4);
1067 UFS_GEOMETRY_DESC_PARAM(sys_code_memory_capacity_adjustment_factor,
1068 	_SCM_CAP_ADJ_FCTR, 2);
1069 UFS_GEOMETRY_DESC_PARAM(non_persist_memory_max_alloc_units,
1070 	_NPM_MAX_NUM_UNITS, 4);
1071 UFS_GEOMETRY_DESC_PARAM(non_persist_memory_capacity_adjustment_factor,
1072 	_NPM_CAP_ADJ_FCTR, 2);
1073 UFS_GEOMETRY_DESC_PARAM(enh1_memory_max_alloc_units,
1074 	_ENM1_MAX_NUM_UNITS, 4);
1075 UFS_GEOMETRY_DESC_PARAM(enh1_memory_capacity_adjustment_factor,
1076 	_ENM1_CAP_ADJ_FCTR, 2);
1077 UFS_GEOMETRY_DESC_PARAM(enh2_memory_max_alloc_units,
1078 	_ENM2_MAX_NUM_UNITS, 4);
1079 UFS_GEOMETRY_DESC_PARAM(enh2_memory_capacity_adjustment_factor,
1080 	_ENM2_CAP_ADJ_FCTR, 2);
1081 UFS_GEOMETRY_DESC_PARAM(enh3_memory_max_alloc_units,
1082 	_ENM3_MAX_NUM_UNITS, 4);
1083 UFS_GEOMETRY_DESC_PARAM(enh3_memory_capacity_adjustment_factor,
1084 	_ENM3_CAP_ADJ_FCTR, 2);
1085 UFS_GEOMETRY_DESC_PARAM(enh4_memory_max_alloc_units,
1086 	_ENM4_MAX_NUM_UNITS, 4);
1087 UFS_GEOMETRY_DESC_PARAM(enh4_memory_capacity_adjustment_factor,
1088 	_ENM4_CAP_ADJ_FCTR, 2);
1089 UFS_GEOMETRY_DESC_PARAM(wb_max_alloc_units, _WB_MAX_ALLOC_UNITS, 4);
1090 UFS_GEOMETRY_DESC_PARAM(wb_max_wb_luns, _WB_MAX_WB_LUNS, 1);
1091 UFS_GEOMETRY_DESC_PARAM(wb_buff_cap_adj, _WB_BUFF_CAP_ADJ, 1);
1092 UFS_GEOMETRY_DESC_PARAM(wb_sup_red_type, _WB_SUP_RED_TYPE, 1);
1093 UFS_GEOMETRY_DESC_PARAM(wb_sup_wb_type, _WB_SUP_WB_TYPE, 1);
1094 
1095 
1096 static struct attribute *ufs_sysfs_geometry_descriptor[] = {
1097 	&dev_attr_raw_device_capacity.attr,
1098 	&dev_attr_max_number_of_luns.attr,
1099 	&dev_attr_segment_size.attr,
1100 	&dev_attr_allocation_unit_size.attr,
1101 	&dev_attr_min_addressable_block_size.attr,
1102 	&dev_attr_optimal_read_block_size.attr,
1103 	&dev_attr_optimal_write_block_size.attr,
1104 	&dev_attr_max_in_buffer_size.attr,
1105 	&dev_attr_max_out_buffer_size.attr,
1106 	&dev_attr_rpmb_rw_size.attr,
1107 	&dev_attr_dyn_capacity_resource_policy.attr,
1108 	&dev_attr_data_ordering.attr,
1109 	&dev_attr_max_number_of_contexts.attr,
1110 	&dev_attr_sys_data_tag_unit_size.attr,
1111 	&dev_attr_sys_data_tag_resource_size.attr,
1112 	&dev_attr_secure_removal_types.attr,
1113 	&dev_attr_memory_types.attr,
1114 	&dev_attr_sys_code_memory_max_alloc_units.attr,
1115 	&dev_attr_sys_code_memory_capacity_adjustment_factor.attr,
1116 	&dev_attr_non_persist_memory_max_alloc_units.attr,
1117 	&dev_attr_non_persist_memory_capacity_adjustment_factor.attr,
1118 	&dev_attr_enh1_memory_max_alloc_units.attr,
1119 	&dev_attr_enh1_memory_capacity_adjustment_factor.attr,
1120 	&dev_attr_enh2_memory_max_alloc_units.attr,
1121 	&dev_attr_enh2_memory_capacity_adjustment_factor.attr,
1122 	&dev_attr_enh3_memory_max_alloc_units.attr,
1123 	&dev_attr_enh3_memory_capacity_adjustment_factor.attr,
1124 	&dev_attr_enh4_memory_max_alloc_units.attr,
1125 	&dev_attr_enh4_memory_capacity_adjustment_factor.attr,
1126 	&dev_attr_wb_max_alloc_units.attr,
1127 	&dev_attr_wb_max_wb_luns.attr,
1128 	&dev_attr_wb_buff_cap_adj.attr,
1129 	&dev_attr_wb_sup_red_type.attr,
1130 	&dev_attr_wb_sup_wb_type.attr,
1131 	NULL,
1132 };
1133 
1134 static const struct attribute_group ufs_sysfs_geometry_descriptor_group = {
1135 	.name = "geometry_descriptor",
1136 	.attrs = ufs_sysfs_geometry_descriptor,
1137 };
1138 
1139 #define UFS_HEALTH_DESC_PARAM(_name, _uname, _size)			\
1140 	UFS_DESC_PARAM(_name, _uname, HEALTH, _size)
1141 
1142 UFS_HEALTH_DESC_PARAM(eol_info, _EOL_INFO, 1);
1143 UFS_HEALTH_DESC_PARAM(life_time_estimation_a, _LIFE_TIME_EST_A, 1);
1144 UFS_HEALTH_DESC_PARAM(life_time_estimation_b, _LIFE_TIME_EST_B, 1);
1145 
1146 static struct attribute *ufs_sysfs_health_descriptor[] = {
1147 	&dev_attr_eol_info.attr,
1148 	&dev_attr_life_time_estimation_a.attr,
1149 	&dev_attr_life_time_estimation_b.attr,
1150 	NULL,
1151 };
1152 
1153 static const struct attribute_group ufs_sysfs_health_descriptor_group = {
1154 	.name = "health_descriptor",
1155 	.attrs = ufs_sysfs_health_descriptor,
1156 };
1157 
1158 #define UFS_POWER_DESC_PARAM(_name, _uname, _index)			\
1159 static ssize_t _name##_index##_show(struct device *dev,			\
1160 	struct device_attribute *attr, char *buf)			\
1161 {									\
1162 	struct ufs_hba *hba = dev_get_drvdata(dev);			\
1163 	return ufs_sysfs_read_desc_param(hba, QUERY_DESC_IDN_POWER, 0,	\
1164 		PWR_DESC##_uname##_0 + _index * 2, buf, 2);		\
1165 }									\
1166 static DEVICE_ATTR_RO(_name##_index)
1167 
1168 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 0);
1169 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 1);
1170 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 2);
1171 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 3);
1172 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 4);
1173 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 5);
1174 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 6);
1175 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 7);
1176 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 8);
1177 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 9);
1178 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 10);
1179 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 11);
1180 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 12);
1181 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 13);
1182 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 14);
1183 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 15);
1184 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 0);
1185 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 1);
1186 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 2);
1187 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 3);
1188 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 4);
1189 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 5);
1190 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 6);
1191 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 7);
1192 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 8);
1193 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 9);
1194 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 10);
1195 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 11);
1196 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 12);
1197 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 13);
1198 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 14);
1199 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 15);
1200 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 0);
1201 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 1);
1202 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 2);
1203 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 3);
1204 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 4);
1205 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 5);
1206 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 6);
1207 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 7);
1208 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 8);
1209 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 9);
1210 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 10);
1211 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 11);
1212 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 12);
1213 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 13);
1214 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 14);
1215 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 15);
1216 
1217 static struct attribute *ufs_sysfs_power_descriptor[] = {
1218 	&dev_attr_active_icc_levels_vcc0.attr,
1219 	&dev_attr_active_icc_levels_vcc1.attr,
1220 	&dev_attr_active_icc_levels_vcc2.attr,
1221 	&dev_attr_active_icc_levels_vcc3.attr,
1222 	&dev_attr_active_icc_levels_vcc4.attr,
1223 	&dev_attr_active_icc_levels_vcc5.attr,
1224 	&dev_attr_active_icc_levels_vcc6.attr,
1225 	&dev_attr_active_icc_levels_vcc7.attr,
1226 	&dev_attr_active_icc_levels_vcc8.attr,
1227 	&dev_attr_active_icc_levels_vcc9.attr,
1228 	&dev_attr_active_icc_levels_vcc10.attr,
1229 	&dev_attr_active_icc_levels_vcc11.attr,
1230 	&dev_attr_active_icc_levels_vcc12.attr,
1231 	&dev_attr_active_icc_levels_vcc13.attr,
1232 	&dev_attr_active_icc_levels_vcc14.attr,
1233 	&dev_attr_active_icc_levels_vcc15.attr,
1234 	&dev_attr_active_icc_levels_vccq0.attr,
1235 	&dev_attr_active_icc_levels_vccq1.attr,
1236 	&dev_attr_active_icc_levels_vccq2.attr,
1237 	&dev_attr_active_icc_levels_vccq3.attr,
1238 	&dev_attr_active_icc_levels_vccq4.attr,
1239 	&dev_attr_active_icc_levels_vccq5.attr,
1240 	&dev_attr_active_icc_levels_vccq6.attr,
1241 	&dev_attr_active_icc_levels_vccq7.attr,
1242 	&dev_attr_active_icc_levels_vccq8.attr,
1243 	&dev_attr_active_icc_levels_vccq9.attr,
1244 	&dev_attr_active_icc_levels_vccq10.attr,
1245 	&dev_attr_active_icc_levels_vccq11.attr,
1246 	&dev_attr_active_icc_levels_vccq12.attr,
1247 	&dev_attr_active_icc_levels_vccq13.attr,
1248 	&dev_attr_active_icc_levels_vccq14.attr,
1249 	&dev_attr_active_icc_levels_vccq15.attr,
1250 	&dev_attr_active_icc_levels_vccq20.attr,
1251 	&dev_attr_active_icc_levels_vccq21.attr,
1252 	&dev_attr_active_icc_levels_vccq22.attr,
1253 	&dev_attr_active_icc_levels_vccq23.attr,
1254 	&dev_attr_active_icc_levels_vccq24.attr,
1255 	&dev_attr_active_icc_levels_vccq25.attr,
1256 	&dev_attr_active_icc_levels_vccq26.attr,
1257 	&dev_attr_active_icc_levels_vccq27.attr,
1258 	&dev_attr_active_icc_levels_vccq28.attr,
1259 	&dev_attr_active_icc_levels_vccq29.attr,
1260 	&dev_attr_active_icc_levels_vccq210.attr,
1261 	&dev_attr_active_icc_levels_vccq211.attr,
1262 	&dev_attr_active_icc_levels_vccq212.attr,
1263 	&dev_attr_active_icc_levels_vccq213.attr,
1264 	&dev_attr_active_icc_levels_vccq214.attr,
1265 	&dev_attr_active_icc_levels_vccq215.attr,
1266 	NULL,
1267 };
1268 
1269 static const struct attribute_group ufs_sysfs_power_descriptor_group = {
1270 	.name = "power_descriptor",
1271 	.attrs = ufs_sysfs_power_descriptor,
1272 };
1273 
1274 #define UFS_STRING_DESCRIPTOR(_name, _pname)				\
1275 static ssize_t _name##_show(struct device *dev,				\
1276 	struct device_attribute *attr, char *buf)			\
1277 {									\
1278 	u8 index;							\
1279 	struct ufs_hba *hba = dev_get_drvdata(dev);			\
1280 	int ret;							\
1281 	int desc_len = QUERY_DESC_MAX_SIZE;				\
1282 	u8 *desc_buf;							\
1283 									\
1284 	down(&hba->host_sem);						\
1285 	if (!ufshcd_is_user_access_allowed(hba)) {			\
1286 		up(&hba->host_sem);					\
1287 		return -EBUSY;						\
1288 	}								\
1289 	desc_buf = kzalloc(QUERY_DESC_MAX_SIZE, GFP_ATOMIC);		\
1290 	if (!desc_buf) {						\
1291 		up(&hba->host_sem);					\
1292 		return -ENOMEM;						\
1293 	}								\
1294 	ufshcd_rpm_get_sync(hba);					\
1295 	ret = ufshcd_query_descriptor_retry(hba,			\
1296 		UPIU_QUERY_OPCODE_READ_DESC, QUERY_DESC_IDN_DEVICE,	\
1297 		0, 0, desc_buf, &desc_len);				\
1298 	if (ret) {							\
1299 		ret = -EINVAL;						\
1300 		goto out;						\
1301 	}								\
1302 	index = desc_buf[DEVICE_DESC_PARAM##_pname];			\
1303 	kfree(desc_buf);						\
1304 	desc_buf = NULL;						\
1305 	ret = ufshcd_read_string_desc(hba, index, &desc_buf,		\
1306 				      SD_ASCII_STD);			\
1307 	if (ret < 0)							\
1308 		goto out;						\
1309 	ret = sysfs_emit(buf, "%s\n", desc_buf);			\
1310 out:									\
1311 	ufshcd_rpm_put_sync(hba);					\
1312 	kfree(desc_buf);						\
1313 	up(&hba->host_sem);						\
1314 	return ret;							\
1315 }									\
1316 static DEVICE_ATTR_RO(_name)
1317 
1318 UFS_STRING_DESCRIPTOR(manufacturer_name, _MANF_NAME);
1319 UFS_STRING_DESCRIPTOR(product_name, _PRDCT_NAME);
1320 UFS_STRING_DESCRIPTOR(oem_id, _OEM_ID);
1321 UFS_STRING_DESCRIPTOR(serial_number, _SN);
1322 UFS_STRING_DESCRIPTOR(product_revision, _PRDCT_REV);
1323 
1324 static struct attribute *ufs_sysfs_string_descriptors[] = {
1325 	&dev_attr_manufacturer_name.attr,
1326 	&dev_attr_product_name.attr,
1327 	&dev_attr_oem_id.attr,
1328 	&dev_attr_serial_number.attr,
1329 	&dev_attr_product_revision.attr,
1330 	NULL,
1331 };
1332 
1333 static const struct attribute_group ufs_sysfs_string_descriptors_group = {
1334 	.name = "string_descriptors",
1335 	.attrs = ufs_sysfs_string_descriptors,
1336 };
1337 
ufshcd_is_wb_flags(enum flag_idn idn)1338 static inline bool ufshcd_is_wb_flags(enum flag_idn idn)
1339 {
1340 	return idn >= QUERY_FLAG_IDN_WB_EN &&
1341 		idn <= QUERY_FLAG_IDN_WB_BUFF_FLUSH_DURING_HIBERN8;
1342 }
1343 
1344 #define UFS_FLAG(_name, _uname)						\
1345 static ssize_t _name##_show(struct device *dev,				\
1346 	struct device_attribute *attr, char *buf)			\
1347 {									\
1348 	bool flag;							\
1349 	u8 index = 0;							\
1350 	int ret;							\
1351 	struct ufs_hba *hba = dev_get_drvdata(dev);			\
1352 									\
1353 	down(&hba->host_sem);						\
1354 	if (!ufshcd_is_user_access_allowed(hba)) {			\
1355 		up(&hba->host_sem);					\
1356 		return -EBUSY;						\
1357 	}								\
1358 	if (ufshcd_is_wb_flags(QUERY_FLAG_IDN##_uname))			\
1359 		index = ufshcd_wb_get_query_index(hba);			\
1360 	ufshcd_rpm_get_sync(hba);					\
1361 	ret = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_READ_FLAG,	\
1362 		QUERY_FLAG_IDN##_uname, index, &flag);			\
1363 	ufshcd_rpm_put_sync(hba);					\
1364 	if (ret) {							\
1365 		ret = -EINVAL;						\
1366 		goto out;						\
1367 	}								\
1368 	ret = sysfs_emit(buf, "%s\n", flag ? "true" : "false");		\
1369 out:									\
1370 	up(&hba->host_sem);						\
1371 	return ret;							\
1372 }									\
1373 static DEVICE_ATTR_RO(_name)
1374 
1375 UFS_FLAG(device_init, _FDEVICEINIT);
1376 UFS_FLAG(permanent_wpe, _PERMANENT_WPE);
1377 UFS_FLAG(power_on_wpe, _PWR_ON_WPE);
1378 UFS_FLAG(bkops_enable, _BKOPS_EN);
1379 UFS_FLAG(life_span_mode_enable, _LIFE_SPAN_MODE_ENABLE);
1380 UFS_FLAG(phy_resource_removal, _FPHYRESOURCEREMOVAL);
1381 UFS_FLAG(busy_rtc, _BUSY_RTC);
1382 UFS_FLAG(disable_fw_update, _PERMANENTLY_DISABLE_FW_UPDATE);
1383 UFS_FLAG(wb_enable, _WB_EN);
1384 UFS_FLAG(wb_flush_en, _WB_BUFF_FLUSH_EN);
1385 UFS_FLAG(wb_flush_during_h8, _WB_BUFF_FLUSH_DURING_HIBERN8);
1386 
1387 static struct attribute *ufs_sysfs_device_flags[] = {
1388 	&dev_attr_device_init.attr,
1389 	&dev_attr_permanent_wpe.attr,
1390 	&dev_attr_power_on_wpe.attr,
1391 	&dev_attr_bkops_enable.attr,
1392 	&dev_attr_life_span_mode_enable.attr,
1393 	&dev_attr_phy_resource_removal.attr,
1394 	&dev_attr_busy_rtc.attr,
1395 	&dev_attr_disable_fw_update.attr,
1396 	&dev_attr_wb_enable.attr,
1397 	&dev_attr_wb_flush_en.attr,
1398 	&dev_attr_wb_flush_during_h8.attr,
1399 	NULL,
1400 };
1401 
1402 static const struct attribute_group ufs_sysfs_flags_group = {
1403 	.name = "flags",
1404 	.attrs = ufs_sysfs_device_flags,
1405 };
1406 
max_number_of_rtt_show(struct device * dev,struct device_attribute * attr,char * buf)1407 static ssize_t max_number_of_rtt_show(struct device *dev,
1408 				      struct device_attribute *attr, char *buf)
1409 {
1410 	struct ufs_hba *hba = dev_get_drvdata(dev);
1411 	u32 rtt;
1412 	int ret;
1413 
1414 	down(&hba->host_sem);
1415 	if (!ufshcd_is_user_access_allowed(hba)) {
1416 		up(&hba->host_sem);
1417 		return -EBUSY;
1418 	}
1419 
1420 	ufshcd_rpm_get_sync(hba);
1421 	ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
1422 		QUERY_ATTR_IDN_MAX_NUM_OF_RTT, 0, 0, &rtt);
1423 	ufshcd_rpm_put_sync(hba);
1424 
1425 	if (ret)
1426 		goto out;
1427 
1428 	ret = sysfs_emit(buf, "0x%08X\n", rtt);
1429 
1430 out:
1431 	up(&hba->host_sem);
1432 	return ret;
1433 }
1434 
max_number_of_rtt_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1435 static ssize_t max_number_of_rtt_store(struct device *dev,
1436 				       struct device_attribute *attr,
1437 				       const char *buf, size_t count)
1438 {
1439 	struct ufs_hba *hba = dev_get_drvdata(dev);
1440 	struct ufs_dev_info *dev_info = &hba->dev_info;
1441 	struct scsi_device *sdev;
1442 	unsigned int rtt;
1443 	int ret;
1444 
1445 	if (kstrtouint(buf, 0, &rtt))
1446 		return -EINVAL;
1447 
1448 	if (rtt > dev_info->rtt_cap) {
1449 		dev_err(dev, "rtt can be at most bDeviceRTTCap\n");
1450 		return -EINVAL;
1451 	}
1452 
1453 	down(&hba->host_sem);
1454 	if (!ufshcd_is_user_access_allowed(hba)) {
1455 		ret = -EBUSY;
1456 		goto out;
1457 	}
1458 
1459 	ufshcd_rpm_get_sync(hba);
1460 
1461 	shost_for_each_device(sdev, hba->host)
1462 		blk_mq_freeze_queue(sdev->request_queue);
1463 
1464 	ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
1465 		QUERY_ATTR_IDN_MAX_NUM_OF_RTT, 0, 0, &rtt);
1466 
1467 	shost_for_each_device(sdev, hba->host)
1468 		blk_mq_unfreeze_queue(sdev->request_queue);
1469 
1470 	ufshcd_rpm_put_sync(hba);
1471 
1472 out:
1473 	up(&hba->host_sem);
1474 	return ret < 0 ? ret : count;
1475 }
1476 
1477 static DEVICE_ATTR_RW(max_number_of_rtt);
1478 
ufshcd_is_wb_attrs(enum attr_idn idn)1479 static inline bool ufshcd_is_wb_attrs(enum attr_idn idn)
1480 {
1481 	return idn >= QUERY_ATTR_IDN_WB_FLUSH_STATUS &&
1482 		idn <= QUERY_ATTR_IDN_CURR_WB_BUFF_SIZE;
1483 }
1484 
1485 #define UFS_ATTRIBUTE(_name, _uname)					\
1486 static ssize_t _name##_show(struct device *dev,				\
1487 	struct device_attribute *attr, char *buf)			\
1488 {									\
1489 	struct ufs_hba *hba = dev_get_drvdata(dev);			\
1490 	u32 value;							\
1491 	int ret;							\
1492 	u8 index = 0;							\
1493 									\
1494 	down(&hba->host_sem);						\
1495 	if (!ufshcd_is_user_access_allowed(hba)) {			\
1496 		up(&hba->host_sem);					\
1497 		return -EBUSY;						\
1498 	}								\
1499 	if (ufshcd_is_wb_attrs(QUERY_ATTR_IDN##_uname))			\
1500 		index = ufshcd_wb_get_query_index(hba);			\
1501 	ufshcd_rpm_get_sync(hba);					\
1502 	ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,	\
1503 		QUERY_ATTR_IDN##_uname, index, 0, &value);		\
1504 	ufshcd_rpm_put_sync(hba);					\
1505 	if (ret) {							\
1506 		ret = -EINVAL;						\
1507 		goto out;						\
1508 	}								\
1509 	ret = sysfs_emit(buf, "0x%08X\n", value);			\
1510 out:									\
1511 	up(&hba->host_sem);						\
1512 	return ret;							\
1513 }									\
1514 static DEVICE_ATTR_RO(_name)
1515 
1516 UFS_ATTRIBUTE(boot_lun_enabled, _BOOT_LU_EN);
1517 UFS_ATTRIBUTE(current_power_mode, _POWER_MODE);
1518 UFS_ATTRIBUTE(active_icc_level, _ACTIVE_ICC_LVL);
1519 UFS_ATTRIBUTE(ooo_data_enabled, _OOO_DATA_EN);
1520 UFS_ATTRIBUTE(bkops_status, _BKOPS_STATUS);
1521 UFS_ATTRIBUTE(purge_status, _PURGE_STATUS);
1522 UFS_ATTRIBUTE(max_data_in_size, _MAX_DATA_IN);
1523 UFS_ATTRIBUTE(max_data_out_size, _MAX_DATA_OUT);
1524 UFS_ATTRIBUTE(reference_clock_frequency, _REF_CLK_FREQ);
1525 UFS_ATTRIBUTE(configuration_descriptor_lock, _CONF_DESC_LOCK);
1526 UFS_ATTRIBUTE(exception_event_control, _EE_CONTROL);
1527 UFS_ATTRIBUTE(exception_event_status, _EE_STATUS);
1528 UFS_ATTRIBUTE(ffu_status, _FFU_STATUS);
1529 UFS_ATTRIBUTE(psa_state, _PSA_STATE);
1530 UFS_ATTRIBUTE(psa_data_size, _PSA_DATA_SIZE);
1531 UFS_ATTRIBUTE(wb_flush_status, _WB_FLUSH_STATUS);
1532 UFS_ATTRIBUTE(wb_avail_buf, _AVAIL_WB_BUFF_SIZE);
1533 UFS_ATTRIBUTE(wb_life_time_est, _WB_BUFF_LIFE_TIME_EST);
1534 UFS_ATTRIBUTE(wb_cur_buf, _CURR_WB_BUFF_SIZE);
1535 
1536 
1537 static struct attribute *ufs_sysfs_attributes[] = {
1538 	&dev_attr_boot_lun_enabled.attr,
1539 	&dev_attr_current_power_mode.attr,
1540 	&dev_attr_active_icc_level.attr,
1541 	&dev_attr_ooo_data_enabled.attr,
1542 	&dev_attr_bkops_status.attr,
1543 	&dev_attr_purge_status.attr,
1544 	&dev_attr_max_data_in_size.attr,
1545 	&dev_attr_max_data_out_size.attr,
1546 	&dev_attr_reference_clock_frequency.attr,
1547 	&dev_attr_configuration_descriptor_lock.attr,
1548 	&dev_attr_max_number_of_rtt.attr,
1549 	&dev_attr_exception_event_control.attr,
1550 	&dev_attr_exception_event_status.attr,
1551 	&dev_attr_ffu_status.attr,
1552 	&dev_attr_psa_state.attr,
1553 	&dev_attr_psa_data_size.attr,
1554 	&dev_attr_wb_flush_status.attr,
1555 	&dev_attr_wb_avail_buf.attr,
1556 	&dev_attr_wb_life_time_est.attr,
1557 	&dev_attr_wb_cur_buf.attr,
1558 	NULL,
1559 };
1560 
1561 static const struct attribute_group ufs_sysfs_attributes_group = {
1562 	.name = "attributes",
1563 	.attrs = ufs_sysfs_attributes,
1564 };
1565 
1566 static const struct attribute_group *ufs_sysfs_groups[] = {
1567 	&ufs_sysfs_default_group,
1568 	&ufs_sysfs_capabilities_group,
1569 	&ufs_sysfs_ufshci_group,
1570 	&ufs_sysfs_monitor_group,
1571 	&ufs_sysfs_power_info_group,
1572 	&ufs_sysfs_device_descriptor_group,
1573 	&ufs_sysfs_interconnect_descriptor_group,
1574 	&ufs_sysfs_geometry_descriptor_group,
1575 	&ufs_sysfs_health_descriptor_group,
1576 	&ufs_sysfs_power_descriptor_group,
1577 	&ufs_sysfs_string_descriptors_group,
1578 	&ufs_sysfs_flags_group,
1579 	&ufs_sysfs_attributes_group,
1580 	NULL,
1581 };
1582 
1583 #define UFS_LUN_DESC_PARAM(_pname, _puname, _duname, _size)		\
1584 static ssize_t _pname##_show(struct device *dev,			\
1585 	struct device_attribute *attr, char *buf)			\
1586 {									\
1587 	struct scsi_device *sdev = to_scsi_device(dev);			\
1588 	struct ufs_hba *hba = shost_priv(sdev->host);			\
1589 	u8 lun = ufshcd_scsi_to_upiu_lun(sdev->lun);			\
1590 	if (!ufs_is_valid_unit_desc_lun(&hba->dev_info, lun))		\
1591 		return -EINVAL;						\
1592 	return ufs_sysfs_read_desc_param(hba, QUERY_DESC_IDN_##_duname,	\
1593 		lun, _duname##_DESC_PARAM##_puname, buf, _size);	\
1594 }									\
1595 static DEVICE_ATTR_RO(_pname)
1596 
1597 #define UFS_UNIT_DESC_PARAM(_name, _uname, _size)			\
1598 	UFS_LUN_DESC_PARAM(_name, _uname, UNIT, _size)
1599 
1600 UFS_UNIT_DESC_PARAM(lu_enable, _LU_ENABLE, 1);
1601 UFS_UNIT_DESC_PARAM(boot_lun_id, _BOOT_LUN_ID, 1);
1602 UFS_UNIT_DESC_PARAM(lun_write_protect, _LU_WR_PROTECT, 1);
1603 UFS_UNIT_DESC_PARAM(lun_queue_depth, _LU_Q_DEPTH, 1);
1604 UFS_UNIT_DESC_PARAM(psa_sensitive, _PSA_SENSITIVE, 1);
1605 UFS_UNIT_DESC_PARAM(lun_memory_type, _MEM_TYPE, 1);
1606 UFS_UNIT_DESC_PARAM(data_reliability, _DATA_RELIABILITY, 1);
1607 UFS_UNIT_DESC_PARAM(logical_block_size, _LOGICAL_BLK_SIZE, 1);
1608 UFS_UNIT_DESC_PARAM(logical_block_count, _LOGICAL_BLK_COUNT, 8);
1609 UFS_UNIT_DESC_PARAM(erase_block_size, _ERASE_BLK_SIZE, 4);
1610 UFS_UNIT_DESC_PARAM(provisioning_type, _PROVISIONING_TYPE, 1);
1611 UFS_UNIT_DESC_PARAM(physical_memory_resourse_count, _PHY_MEM_RSRC_CNT, 8);
1612 UFS_UNIT_DESC_PARAM(context_capabilities, _CTX_CAPABILITIES, 2);
1613 UFS_UNIT_DESC_PARAM(large_unit_granularity, _LARGE_UNIT_SIZE_M1, 1);
1614 UFS_UNIT_DESC_PARAM(wb_buf_alloc_units, _WB_BUF_ALLOC_UNITS, 4);
1615 
1616 static struct attribute *ufs_sysfs_unit_descriptor[] = {
1617 	&dev_attr_lu_enable.attr,
1618 	&dev_attr_boot_lun_id.attr,
1619 	&dev_attr_lun_write_protect.attr,
1620 	&dev_attr_lun_queue_depth.attr,
1621 	&dev_attr_psa_sensitive.attr,
1622 	&dev_attr_lun_memory_type.attr,
1623 	&dev_attr_data_reliability.attr,
1624 	&dev_attr_logical_block_size.attr,
1625 	&dev_attr_logical_block_count.attr,
1626 	&dev_attr_erase_block_size.attr,
1627 	&dev_attr_provisioning_type.attr,
1628 	&dev_attr_physical_memory_resourse_count.attr,
1629 	&dev_attr_context_capabilities.attr,
1630 	&dev_attr_large_unit_granularity.attr,
1631 	&dev_attr_wb_buf_alloc_units.attr,
1632 	NULL,
1633 };
1634 
ufs_unit_descriptor_is_visible(struct kobject * kobj,struct attribute * attr,int n)1635 static umode_t ufs_unit_descriptor_is_visible(struct kobject *kobj, struct attribute *attr, int n)
1636 {
1637 	struct device *dev = container_of(kobj, struct device, kobj);
1638 	struct scsi_device *sdev = to_scsi_device(dev);
1639 	u8 lun = ufshcd_scsi_to_upiu_lun(sdev->lun);
1640 	umode_t mode = attr->mode;
1641 
1642 	if (lun == UFS_UPIU_BOOT_WLUN || lun == UFS_UPIU_UFS_DEVICE_WLUN)
1643 		/* Boot and device WLUN have no unit descriptors */
1644 		mode = 0;
1645 	if (lun == UFS_UPIU_RPMB_WLUN && attr == &dev_attr_wb_buf_alloc_units.attr)
1646 		mode = 0;
1647 
1648 	return mode;
1649 }
1650 
1651 
1652 const struct attribute_group ufs_sysfs_unit_descriptor_group = {
1653 	.name = "unit_descriptor",
1654 	.attrs = ufs_sysfs_unit_descriptor,
1655 	.is_visible = ufs_unit_descriptor_is_visible,
1656 };
1657 
dyn_cap_needed_attribute_show(struct device * dev,struct device_attribute * attr,char * buf)1658 static ssize_t dyn_cap_needed_attribute_show(struct device *dev,
1659 	struct device_attribute *attr, char *buf)
1660 {
1661 	u32 value;
1662 	struct scsi_device *sdev = to_scsi_device(dev);
1663 	struct ufs_hba *hba = shost_priv(sdev->host);
1664 	u8 lun = ufshcd_scsi_to_upiu_lun(sdev->lun);
1665 	int ret;
1666 
1667 	down(&hba->host_sem);
1668 	if (!ufshcd_is_user_access_allowed(hba)) {
1669 		ret = -EBUSY;
1670 		goto out;
1671 	}
1672 
1673 	ufshcd_rpm_get_sync(hba);
1674 	ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
1675 		QUERY_ATTR_IDN_DYN_CAP_NEEDED, lun, 0, &value);
1676 	ufshcd_rpm_put_sync(hba);
1677 	if (ret) {
1678 		ret = -EINVAL;
1679 		goto out;
1680 	}
1681 
1682 	ret = sysfs_emit(buf, "0x%08X\n", value);
1683 
1684 out:
1685 	up(&hba->host_sem);
1686 	return ret;
1687 }
1688 static DEVICE_ATTR_RO(dyn_cap_needed_attribute);
1689 
1690 static struct attribute *ufs_sysfs_lun_attributes[] = {
1691 	&dev_attr_dyn_cap_needed_attribute.attr,
1692 	NULL,
1693 };
1694 
1695 const struct attribute_group ufs_sysfs_lun_attributes_group = {
1696 	.attrs = ufs_sysfs_lun_attributes,
1697 };
1698 
ufs_sysfs_add_nodes(struct device * dev)1699 void ufs_sysfs_add_nodes(struct device *dev)
1700 {
1701 	int ret;
1702 
1703 	ret = sysfs_create_groups(&dev->kobj, ufs_sysfs_groups);
1704 	if (ret)
1705 		dev_err(dev,
1706 			"%s: sysfs groups creation failed (err = %d)\n",
1707 			__func__, ret);
1708 }
1709 
ufs_sysfs_remove_nodes(struct device * dev)1710 void ufs_sysfs_remove_nodes(struct device *dev)
1711 {
1712 	sysfs_remove_groups(&dev->kobj, ufs_sysfs_groups);
1713 }
1714