xref: /linux/drivers/ufs/core/ufs-sysfs.c (revision 7ec462100ef9142344ddbf86f2c3008b97acddbe)
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 	return sysfs_emit(buf, "%llu\n", div_u64(ktime_to_us(m->lat_sum[READ]),
674 						 m->nr_req[READ]));
675 }
676 
read_req_latency_max_show(struct device * dev,struct device_attribute * attr,char * buf)677 static ssize_t read_req_latency_max_show(struct device *dev,
678 					 struct device_attribute *attr,
679 					 char *buf)
680 {
681 	struct ufs_hba *hba = dev_get_drvdata(dev);
682 
683 	return sysfs_emit(buf, "%llu\n",
684 			  ktime_to_us(hba->monitor.lat_max[READ]));
685 }
686 
read_req_latency_min_show(struct device * dev,struct device_attribute * attr,char * buf)687 static ssize_t read_req_latency_min_show(struct device *dev,
688 					 struct device_attribute *attr,
689 					 char *buf)
690 {
691 	struct ufs_hba *hba = dev_get_drvdata(dev);
692 
693 	return sysfs_emit(buf, "%llu\n",
694 			  ktime_to_us(hba->monitor.lat_min[READ]));
695 }
696 
read_req_latency_sum_show(struct device * dev,struct device_attribute * attr,char * buf)697 static ssize_t read_req_latency_sum_show(struct device *dev,
698 					 struct device_attribute *attr,
699 					 char *buf)
700 {
701 	struct ufs_hba *hba = dev_get_drvdata(dev);
702 
703 	return sysfs_emit(buf, "%llu\n",
704 			  ktime_to_us(hba->monitor.lat_sum[READ]));
705 }
706 
write_total_sectors_show(struct device * dev,struct device_attribute * attr,char * buf)707 static ssize_t write_total_sectors_show(struct device *dev,
708 					struct device_attribute *attr,
709 					char *buf)
710 {
711 	struct ufs_hba *hba = dev_get_drvdata(dev);
712 
713 	return sysfs_emit(buf, "%lu\n", hba->monitor.nr_sec_rw[WRITE]);
714 }
715 
write_total_busy_show(struct device * dev,struct device_attribute * attr,char * buf)716 static ssize_t write_total_busy_show(struct device *dev,
717 				     struct device_attribute *attr, char *buf)
718 {
719 	struct ufs_hba *hba = dev_get_drvdata(dev);
720 
721 	return sysfs_emit(buf, "%llu\n",
722 			  ktime_to_us(hba->monitor.total_busy[WRITE]));
723 }
724 
write_nr_requests_show(struct device * dev,struct device_attribute * attr,char * buf)725 static ssize_t write_nr_requests_show(struct device *dev,
726 				      struct device_attribute *attr, char *buf)
727 {
728 	struct ufs_hba *hba = dev_get_drvdata(dev);
729 
730 	return sysfs_emit(buf, "%lu\n", hba->monitor.nr_req[WRITE]);
731 }
732 
write_req_latency_avg_show(struct device * dev,struct device_attribute * attr,char * buf)733 static ssize_t write_req_latency_avg_show(struct device *dev,
734 					  struct device_attribute *attr,
735 					  char *buf)
736 {
737 	struct ufs_hba *hba = dev_get_drvdata(dev);
738 	struct ufs_hba_monitor *m = &hba->monitor;
739 
740 	return sysfs_emit(buf, "%llu\n", div_u64(ktime_to_us(m->lat_sum[WRITE]),
741 						 m->nr_req[WRITE]));
742 }
743 
write_req_latency_max_show(struct device * dev,struct device_attribute * attr,char * buf)744 static ssize_t write_req_latency_max_show(struct device *dev,
745 					  struct device_attribute *attr,
746 					  char *buf)
747 {
748 	struct ufs_hba *hba = dev_get_drvdata(dev);
749 
750 	return sysfs_emit(buf, "%llu\n",
751 			  ktime_to_us(hba->monitor.lat_max[WRITE]));
752 }
753 
write_req_latency_min_show(struct device * dev,struct device_attribute * attr,char * buf)754 static ssize_t write_req_latency_min_show(struct device *dev,
755 					  struct device_attribute *attr,
756 					  char *buf)
757 {
758 	struct ufs_hba *hba = dev_get_drvdata(dev);
759 
760 	return sysfs_emit(buf, "%llu\n",
761 			  ktime_to_us(hba->monitor.lat_min[WRITE]));
762 }
763 
write_req_latency_sum_show(struct device * dev,struct device_attribute * attr,char * buf)764 static ssize_t write_req_latency_sum_show(struct device *dev,
765 					  struct device_attribute *attr,
766 					  char *buf)
767 {
768 	struct ufs_hba *hba = dev_get_drvdata(dev);
769 
770 	return sysfs_emit(buf, "%llu\n",
771 			  ktime_to_us(hba->monitor.lat_sum[WRITE]));
772 }
773 
774 static DEVICE_ATTR_RW(monitor_enable);
775 static DEVICE_ATTR_RW(monitor_chunk_size);
776 static DEVICE_ATTR_RO(read_total_sectors);
777 static DEVICE_ATTR_RO(read_total_busy);
778 static DEVICE_ATTR_RO(read_nr_requests);
779 static DEVICE_ATTR_RO(read_req_latency_avg);
780 static DEVICE_ATTR_RO(read_req_latency_max);
781 static DEVICE_ATTR_RO(read_req_latency_min);
782 static DEVICE_ATTR_RO(read_req_latency_sum);
783 static DEVICE_ATTR_RO(write_total_sectors);
784 static DEVICE_ATTR_RO(write_total_busy);
785 static DEVICE_ATTR_RO(write_nr_requests);
786 static DEVICE_ATTR_RO(write_req_latency_avg);
787 static DEVICE_ATTR_RO(write_req_latency_max);
788 static DEVICE_ATTR_RO(write_req_latency_min);
789 static DEVICE_ATTR_RO(write_req_latency_sum);
790 
791 static struct attribute *ufs_sysfs_monitor_attrs[] = {
792 	&dev_attr_monitor_enable.attr,
793 	&dev_attr_monitor_chunk_size.attr,
794 	&dev_attr_read_total_sectors.attr,
795 	&dev_attr_read_total_busy.attr,
796 	&dev_attr_read_nr_requests.attr,
797 	&dev_attr_read_req_latency_avg.attr,
798 	&dev_attr_read_req_latency_max.attr,
799 	&dev_attr_read_req_latency_min.attr,
800 	&dev_attr_read_req_latency_sum.attr,
801 	&dev_attr_write_total_sectors.attr,
802 	&dev_attr_write_total_busy.attr,
803 	&dev_attr_write_nr_requests.attr,
804 	&dev_attr_write_req_latency_avg.attr,
805 	&dev_attr_write_req_latency_max.attr,
806 	&dev_attr_write_req_latency_min.attr,
807 	&dev_attr_write_req_latency_sum.attr,
808 	NULL
809 };
810 
811 static const struct attribute_group ufs_sysfs_monitor_group = {
812 	.name = "monitor",
813 	.attrs = ufs_sysfs_monitor_attrs,
814 };
815 
lane_show(struct device * dev,struct device_attribute * attr,char * buf)816 static ssize_t lane_show(struct device *dev, struct device_attribute *attr,
817 			 char *buf)
818 {
819 	struct ufs_hba *hba = dev_get_drvdata(dev);
820 
821 	return sysfs_emit(buf, "%u\n", hba->pwr_info.lane_rx);
822 }
823 
mode_show(struct device * dev,struct device_attribute * attr,char * buf)824 static ssize_t mode_show(struct device *dev, struct device_attribute *attr,
825 			 char *buf)
826 {
827 	struct ufs_hba *hba = dev_get_drvdata(dev);
828 
829 	return sysfs_emit(buf, "%s\n", ufs_pa_pwr_mode_to_string(hba->pwr_info.pwr_rx));
830 }
831 
rate_show(struct device * dev,struct device_attribute * attr,char * buf)832 static ssize_t rate_show(struct device *dev, struct device_attribute *attr,
833 			 char *buf)
834 {
835 	struct ufs_hba *hba = dev_get_drvdata(dev);
836 
837 	return sysfs_emit(buf, "%s\n", ufs_hs_gear_rate_to_string(hba->pwr_info.hs_rate));
838 }
839 
gear_show(struct device * dev,struct device_attribute * attr,char * buf)840 static ssize_t gear_show(struct device *dev, struct device_attribute *attr,
841 			 char *buf)
842 {
843 	struct ufs_hba *hba = dev_get_drvdata(dev);
844 
845 	return sysfs_emit(buf, "%s\n", hba->pwr_info.hs_rate ?
846 			  ufs_hs_gear_to_string(hba->pwr_info.gear_rx) :
847 			  ufs_pwm_gear_to_string(hba->pwr_info.gear_rx));
848 }
849 
dev_pm_show(struct device * dev,struct device_attribute * attr,char * buf)850 static ssize_t dev_pm_show(struct device *dev, struct device_attribute *attr,
851 			   char *buf)
852 {
853 	struct ufs_hba *hba = dev_get_drvdata(dev);
854 
855 	return sysfs_emit(buf, "%s\n", ufshcd_ufs_dev_pwr_mode_to_string(hba->curr_dev_pwr_mode));
856 }
857 
link_state_show(struct device * dev,struct device_attribute * attr,char * buf)858 static ssize_t link_state_show(struct device *dev,
859 			       struct device_attribute *attr, char *buf)
860 {
861 	struct ufs_hba *hba = dev_get_drvdata(dev);
862 
863 	return sysfs_emit(buf, "%s\n", ufshcd_uic_link_state_to_string(hba->uic_link_state));
864 }
865 
866 static DEVICE_ATTR_RO(lane);
867 static DEVICE_ATTR_RO(mode);
868 static DEVICE_ATTR_RO(rate);
869 static DEVICE_ATTR_RO(gear);
870 static DEVICE_ATTR_RO(dev_pm);
871 static DEVICE_ATTR_RO(link_state);
872 
873 static struct attribute *ufs_power_info_attrs[] = {
874 	&dev_attr_lane.attr,
875 	&dev_attr_mode.attr,
876 	&dev_attr_rate.attr,
877 	&dev_attr_gear.attr,
878 	&dev_attr_dev_pm.attr,
879 	&dev_attr_link_state.attr,
880 	NULL
881 };
882 
883 static const struct attribute_group ufs_sysfs_power_info_group = {
884 	.name = "power_info",
885 	.attrs = ufs_power_info_attrs,
886 };
887 
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)888 static ssize_t ufs_sysfs_read_desc_param(struct ufs_hba *hba,
889 				  enum desc_idn desc_id,
890 				  u8 desc_index,
891 				  u8 param_offset,
892 				  u8 *sysfs_buf,
893 				  u8 param_size)
894 {
895 	u8 desc_buf[8] = {0};
896 	int ret;
897 
898 	if (param_size > 8)
899 		return -EINVAL;
900 
901 	down(&hba->host_sem);
902 	if (!ufshcd_is_user_access_allowed(hba)) {
903 		ret = -EBUSY;
904 		goto out;
905 	}
906 
907 	ufshcd_rpm_get_sync(hba);
908 	ret = ufshcd_read_desc_param(hba, desc_id, desc_index,
909 				param_offset, desc_buf, param_size);
910 	ufshcd_rpm_put_sync(hba);
911 	if (ret) {
912 		ret = -EINVAL;
913 		goto out;
914 	}
915 
916 	switch (param_size) {
917 	case 1:
918 		ret = sysfs_emit(sysfs_buf, "0x%02X\n", *desc_buf);
919 		break;
920 	case 2:
921 		ret = sysfs_emit(sysfs_buf, "0x%04X\n",
922 			get_unaligned_be16(desc_buf));
923 		break;
924 	case 4:
925 		ret = sysfs_emit(sysfs_buf, "0x%08X\n",
926 			get_unaligned_be32(desc_buf));
927 		break;
928 	case 8:
929 		ret = sysfs_emit(sysfs_buf, "0x%016llX\n",
930 			get_unaligned_be64(desc_buf));
931 		break;
932 	}
933 
934 out:
935 	up(&hba->host_sem);
936 	return ret;
937 }
938 
939 #define UFS_DESC_PARAM(_name, _puname, _duname, _size)			\
940 static ssize_t _name##_show(struct device *dev,				\
941 	struct device_attribute *attr, char *buf)			\
942 {									\
943 	struct ufs_hba *hba = dev_get_drvdata(dev);			\
944 	return ufs_sysfs_read_desc_param(hba, QUERY_DESC_IDN_##_duname,	\
945 		0, _duname##_DESC_PARAM##_puname, buf, _size);		\
946 }									\
947 static DEVICE_ATTR_RO(_name)
948 
949 #define UFS_DEVICE_DESC_PARAM(_name, _uname, _size)			\
950 	UFS_DESC_PARAM(_name, _uname, DEVICE, _size)
951 
952 UFS_DEVICE_DESC_PARAM(device_type, _DEVICE_TYPE, 1);
953 UFS_DEVICE_DESC_PARAM(device_class, _DEVICE_CLASS, 1);
954 UFS_DEVICE_DESC_PARAM(device_sub_class, _DEVICE_SUB_CLASS, 1);
955 UFS_DEVICE_DESC_PARAM(protocol, _PRTCL, 1);
956 UFS_DEVICE_DESC_PARAM(number_of_luns, _NUM_LU, 1);
957 UFS_DEVICE_DESC_PARAM(number_of_wluns, _NUM_WLU, 1);
958 UFS_DEVICE_DESC_PARAM(boot_enable, _BOOT_ENBL, 1);
959 UFS_DEVICE_DESC_PARAM(descriptor_access_enable, _DESC_ACCSS_ENBL, 1);
960 UFS_DEVICE_DESC_PARAM(initial_power_mode, _INIT_PWR_MODE, 1);
961 UFS_DEVICE_DESC_PARAM(high_priority_lun, _HIGH_PR_LUN, 1);
962 UFS_DEVICE_DESC_PARAM(secure_removal_type, _SEC_RMV_TYPE, 1);
963 UFS_DEVICE_DESC_PARAM(support_security_lun, _SEC_LU, 1);
964 UFS_DEVICE_DESC_PARAM(bkops_termination_latency, _BKOP_TERM_LT, 1);
965 UFS_DEVICE_DESC_PARAM(initial_active_icc_level, _ACTVE_ICC_LVL, 1);
966 UFS_DEVICE_DESC_PARAM(specification_version, _SPEC_VER, 2);
967 UFS_DEVICE_DESC_PARAM(manufacturing_date, _MANF_DATE, 2);
968 UFS_DEVICE_DESC_PARAM(manufacturer_id, _MANF_ID, 2);
969 UFS_DEVICE_DESC_PARAM(rtt_capability, _RTT_CAP, 1);
970 UFS_DEVICE_DESC_PARAM(rtc_update, _FRQ_RTC, 2);
971 UFS_DEVICE_DESC_PARAM(ufs_features, _UFS_FEAT, 1);
972 UFS_DEVICE_DESC_PARAM(ffu_timeout, _FFU_TMT, 1);
973 UFS_DEVICE_DESC_PARAM(queue_depth, _Q_DPTH, 1);
974 UFS_DEVICE_DESC_PARAM(device_version, _DEV_VER, 2);
975 UFS_DEVICE_DESC_PARAM(number_of_secure_wpa, _NUM_SEC_WPA, 1);
976 UFS_DEVICE_DESC_PARAM(psa_max_data_size, _PSA_MAX_DATA, 4);
977 UFS_DEVICE_DESC_PARAM(psa_state_timeout, _PSA_TMT, 1);
978 UFS_DEVICE_DESC_PARAM(ext_feature_sup, _EXT_UFS_FEATURE_SUP, 4);
979 UFS_DEVICE_DESC_PARAM(wb_presv_us_en, _WB_PRESRV_USRSPC_EN, 1);
980 UFS_DEVICE_DESC_PARAM(wb_type, _WB_TYPE, 1);
981 UFS_DEVICE_DESC_PARAM(wb_shared_alloc_units, _WB_SHARED_ALLOC_UNITS, 4);
982 
983 static struct attribute *ufs_sysfs_device_descriptor[] = {
984 	&dev_attr_device_type.attr,
985 	&dev_attr_device_class.attr,
986 	&dev_attr_device_sub_class.attr,
987 	&dev_attr_protocol.attr,
988 	&dev_attr_number_of_luns.attr,
989 	&dev_attr_number_of_wluns.attr,
990 	&dev_attr_boot_enable.attr,
991 	&dev_attr_descriptor_access_enable.attr,
992 	&dev_attr_initial_power_mode.attr,
993 	&dev_attr_high_priority_lun.attr,
994 	&dev_attr_secure_removal_type.attr,
995 	&dev_attr_support_security_lun.attr,
996 	&dev_attr_bkops_termination_latency.attr,
997 	&dev_attr_initial_active_icc_level.attr,
998 	&dev_attr_specification_version.attr,
999 	&dev_attr_manufacturing_date.attr,
1000 	&dev_attr_manufacturer_id.attr,
1001 	&dev_attr_rtt_capability.attr,
1002 	&dev_attr_rtc_update.attr,
1003 	&dev_attr_ufs_features.attr,
1004 	&dev_attr_ffu_timeout.attr,
1005 	&dev_attr_queue_depth.attr,
1006 	&dev_attr_device_version.attr,
1007 	&dev_attr_number_of_secure_wpa.attr,
1008 	&dev_attr_psa_max_data_size.attr,
1009 	&dev_attr_psa_state_timeout.attr,
1010 	&dev_attr_ext_feature_sup.attr,
1011 	&dev_attr_wb_presv_us_en.attr,
1012 	&dev_attr_wb_type.attr,
1013 	&dev_attr_wb_shared_alloc_units.attr,
1014 	NULL,
1015 };
1016 
1017 static const struct attribute_group ufs_sysfs_device_descriptor_group = {
1018 	.name = "device_descriptor",
1019 	.attrs = ufs_sysfs_device_descriptor,
1020 };
1021 
1022 #define UFS_INTERCONNECT_DESC_PARAM(_name, _uname, _size)		\
1023 	UFS_DESC_PARAM(_name, _uname, INTERCONNECT, _size)
1024 
1025 UFS_INTERCONNECT_DESC_PARAM(unipro_version, _UNIPRO_VER, 2);
1026 UFS_INTERCONNECT_DESC_PARAM(mphy_version, _MPHY_VER, 2);
1027 
1028 static struct attribute *ufs_sysfs_interconnect_descriptor[] = {
1029 	&dev_attr_unipro_version.attr,
1030 	&dev_attr_mphy_version.attr,
1031 	NULL,
1032 };
1033 
1034 static const struct attribute_group ufs_sysfs_interconnect_descriptor_group = {
1035 	.name = "interconnect_descriptor",
1036 	.attrs = ufs_sysfs_interconnect_descriptor,
1037 };
1038 
1039 #define UFS_GEOMETRY_DESC_PARAM(_name, _uname, _size)			\
1040 	UFS_DESC_PARAM(_name, _uname, GEOMETRY, _size)
1041 
1042 UFS_GEOMETRY_DESC_PARAM(raw_device_capacity, _DEV_CAP, 8);
1043 UFS_GEOMETRY_DESC_PARAM(max_number_of_luns, _MAX_NUM_LUN, 1);
1044 UFS_GEOMETRY_DESC_PARAM(segment_size, _SEG_SIZE, 4);
1045 UFS_GEOMETRY_DESC_PARAM(allocation_unit_size, _ALLOC_UNIT_SIZE, 1);
1046 UFS_GEOMETRY_DESC_PARAM(min_addressable_block_size, _MIN_BLK_SIZE, 1);
1047 UFS_GEOMETRY_DESC_PARAM(optimal_read_block_size, _OPT_RD_BLK_SIZE, 1);
1048 UFS_GEOMETRY_DESC_PARAM(optimal_write_block_size, _OPT_WR_BLK_SIZE, 1);
1049 UFS_GEOMETRY_DESC_PARAM(max_in_buffer_size, _MAX_IN_BUF_SIZE, 1);
1050 UFS_GEOMETRY_DESC_PARAM(max_out_buffer_size, _MAX_OUT_BUF_SIZE, 1);
1051 UFS_GEOMETRY_DESC_PARAM(rpmb_rw_size, _RPMB_RW_SIZE, 1);
1052 UFS_GEOMETRY_DESC_PARAM(dyn_capacity_resource_policy, _DYN_CAP_RSRC_PLC, 1);
1053 UFS_GEOMETRY_DESC_PARAM(data_ordering, _DATA_ORDER, 1);
1054 UFS_GEOMETRY_DESC_PARAM(max_number_of_contexts, _MAX_NUM_CTX, 1);
1055 UFS_GEOMETRY_DESC_PARAM(sys_data_tag_unit_size, _TAG_UNIT_SIZE, 1);
1056 UFS_GEOMETRY_DESC_PARAM(sys_data_tag_resource_size, _TAG_RSRC_SIZE, 1);
1057 UFS_GEOMETRY_DESC_PARAM(secure_removal_types, _SEC_RM_TYPES, 1);
1058 UFS_GEOMETRY_DESC_PARAM(memory_types, _MEM_TYPES, 2);
1059 UFS_GEOMETRY_DESC_PARAM(sys_code_memory_max_alloc_units,
1060 	_SCM_MAX_NUM_UNITS, 4);
1061 UFS_GEOMETRY_DESC_PARAM(sys_code_memory_capacity_adjustment_factor,
1062 	_SCM_CAP_ADJ_FCTR, 2);
1063 UFS_GEOMETRY_DESC_PARAM(non_persist_memory_max_alloc_units,
1064 	_NPM_MAX_NUM_UNITS, 4);
1065 UFS_GEOMETRY_DESC_PARAM(non_persist_memory_capacity_adjustment_factor,
1066 	_NPM_CAP_ADJ_FCTR, 2);
1067 UFS_GEOMETRY_DESC_PARAM(enh1_memory_max_alloc_units,
1068 	_ENM1_MAX_NUM_UNITS, 4);
1069 UFS_GEOMETRY_DESC_PARAM(enh1_memory_capacity_adjustment_factor,
1070 	_ENM1_CAP_ADJ_FCTR, 2);
1071 UFS_GEOMETRY_DESC_PARAM(enh2_memory_max_alloc_units,
1072 	_ENM2_MAX_NUM_UNITS, 4);
1073 UFS_GEOMETRY_DESC_PARAM(enh2_memory_capacity_adjustment_factor,
1074 	_ENM2_CAP_ADJ_FCTR, 2);
1075 UFS_GEOMETRY_DESC_PARAM(enh3_memory_max_alloc_units,
1076 	_ENM3_MAX_NUM_UNITS, 4);
1077 UFS_GEOMETRY_DESC_PARAM(enh3_memory_capacity_adjustment_factor,
1078 	_ENM3_CAP_ADJ_FCTR, 2);
1079 UFS_GEOMETRY_DESC_PARAM(enh4_memory_max_alloc_units,
1080 	_ENM4_MAX_NUM_UNITS, 4);
1081 UFS_GEOMETRY_DESC_PARAM(enh4_memory_capacity_adjustment_factor,
1082 	_ENM4_CAP_ADJ_FCTR, 2);
1083 UFS_GEOMETRY_DESC_PARAM(wb_max_alloc_units, _WB_MAX_ALLOC_UNITS, 4);
1084 UFS_GEOMETRY_DESC_PARAM(wb_max_wb_luns, _WB_MAX_WB_LUNS, 1);
1085 UFS_GEOMETRY_DESC_PARAM(wb_buff_cap_adj, _WB_BUFF_CAP_ADJ, 1);
1086 UFS_GEOMETRY_DESC_PARAM(wb_sup_red_type, _WB_SUP_RED_TYPE, 1);
1087 UFS_GEOMETRY_DESC_PARAM(wb_sup_wb_type, _WB_SUP_WB_TYPE, 1);
1088 
1089 
1090 static struct attribute *ufs_sysfs_geometry_descriptor[] = {
1091 	&dev_attr_raw_device_capacity.attr,
1092 	&dev_attr_max_number_of_luns.attr,
1093 	&dev_attr_segment_size.attr,
1094 	&dev_attr_allocation_unit_size.attr,
1095 	&dev_attr_min_addressable_block_size.attr,
1096 	&dev_attr_optimal_read_block_size.attr,
1097 	&dev_attr_optimal_write_block_size.attr,
1098 	&dev_attr_max_in_buffer_size.attr,
1099 	&dev_attr_max_out_buffer_size.attr,
1100 	&dev_attr_rpmb_rw_size.attr,
1101 	&dev_attr_dyn_capacity_resource_policy.attr,
1102 	&dev_attr_data_ordering.attr,
1103 	&dev_attr_max_number_of_contexts.attr,
1104 	&dev_attr_sys_data_tag_unit_size.attr,
1105 	&dev_attr_sys_data_tag_resource_size.attr,
1106 	&dev_attr_secure_removal_types.attr,
1107 	&dev_attr_memory_types.attr,
1108 	&dev_attr_sys_code_memory_max_alloc_units.attr,
1109 	&dev_attr_sys_code_memory_capacity_adjustment_factor.attr,
1110 	&dev_attr_non_persist_memory_max_alloc_units.attr,
1111 	&dev_attr_non_persist_memory_capacity_adjustment_factor.attr,
1112 	&dev_attr_enh1_memory_max_alloc_units.attr,
1113 	&dev_attr_enh1_memory_capacity_adjustment_factor.attr,
1114 	&dev_attr_enh2_memory_max_alloc_units.attr,
1115 	&dev_attr_enh2_memory_capacity_adjustment_factor.attr,
1116 	&dev_attr_enh3_memory_max_alloc_units.attr,
1117 	&dev_attr_enh3_memory_capacity_adjustment_factor.attr,
1118 	&dev_attr_enh4_memory_max_alloc_units.attr,
1119 	&dev_attr_enh4_memory_capacity_adjustment_factor.attr,
1120 	&dev_attr_wb_max_alloc_units.attr,
1121 	&dev_attr_wb_max_wb_luns.attr,
1122 	&dev_attr_wb_buff_cap_adj.attr,
1123 	&dev_attr_wb_sup_red_type.attr,
1124 	&dev_attr_wb_sup_wb_type.attr,
1125 	NULL,
1126 };
1127 
1128 static const struct attribute_group ufs_sysfs_geometry_descriptor_group = {
1129 	.name = "geometry_descriptor",
1130 	.attrs = ufs_sysfs_geometry_descriptor,
1131 };
1132 
1133 #define UFS_HEALTH_DESC_PARAM(_name, _uname, _size)			\
1134 	UFS_DESC_PARAM(_name, _uname, HEALTH, _size)
1135 
1136 UFS_HEALTH_DESC_PARAM(eol_info, _EOL_INFO, 1);
1137 UFS_HEALTH_DESC_PARAM(life_time_estimation_a, _LIFE_TIME_EST_A, 1);
1138 UFS_HEALTH_DESC_PARAM(life_time_estimation_b, _LIFE_TIME_EST_B, 1);
1139 
1140 static struct attribute *ufs_sysfs_health_descriptor[] = {
1141 	&dev_attr_eol_info.attr,
1142 	&dev_attr_life_time_estimation_a.attr,
1143 	&dev_attr_life_time_estimation_b.attr,
1144 	NULL,
1145 };
1146 
1147 static const struct attribute_group ufs_sysfs_health_descriptor_group = {
1148 	.name = "health_descriptor",
1149 	.attrs = ufs_sysfs_health_descriptor,
1150 };
1151 
1152 #define UFS_POWER_DESC_PARAM(_name, _uname, _index)			\
1153 static ssize_t _name##_index##_show(struct device *dev,			\
1154 	struct device_attribute *attr, char *buf)			\
1155 {									\
1156 	struct ufs_hba *hba = dev_get_drvdata(dev);			\
1157 	return ufs_sysfs_read_desc_param(hba, QUERY_DESC_IDN_POWER, 0,	\
1158 		PWR_DESC##_uname##_0 + _index * 2, buf, 2);		\
1159 }									\
1160 static DEVICE_ATTR_RO(_name##_index)
1161 
1162 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 0);
1163 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 1);
1164 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 2);
1165 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 3);
1166 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 4);
1167 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 5);
1168 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 6);
1169 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 7);
1170 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 8);
1171 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 9);
1172 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 10);
1173 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 11);
1174 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 12);
1175 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 13);
1176 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 14);
1177 UFS_POWER_DESC_PARAM(active_icc_levels_vcc, _ACTIVE_LVLS_VCC, 15);
1178 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 0);
1179 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 1);
1180 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 2);
1181 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 3);
1182 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 4);
1183 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 5);
1184 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 6);
1185 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 7);
1186 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 8);
1187 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 9);
1188 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 10);
1189 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 11);
1190 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 12);
1191 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 13);
1192 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 14);
1193 UFS_POWER_DESC_PARAM(active_icc_levels_vccq, _ACTIVE_LVLS_VCCQ, 15);
1194 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 0);
1195 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 1);
1196 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 2);
1197 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 3);
1198 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 4);
1199 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 5);
1200 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 6);
1201 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 7);
1202 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 8);
1203 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 9);
1204 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 10);
1205 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 11);
1206 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 12);
1207 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 13);
1208 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 14);
1209 UFS_POWER_DESC_PARAM(active_icc_levels_vccq2, _ACTIVE_LVLS_VCCQ2, 15);
1210 
1211 static struct attribute *ufs_sysfs_power_descriptor[] = {
1212 	&dev_attr_active_icc_levels_vcc0.attr,
1213 	&dev_attr_active_icc_levels_vcc1.attr,
1214 	&dev_attr_active_icc_levels_vcc2.attr,
1215 	&dev_attr_active_icc_levels_vcc3.attr,
1216 	&dev_attr_active_icc_levels_vcc4.attr,
1217 	&dev_attr_active_icc_levels_vcc5.attr,
1218 	&dev_attr_active_icc_levels_vcc6.attr,
1219 	&dev_attr_active_icc_levels_vcc7.attr,
1220 	&dev_attr_active_icc_levels_vcc8.attr,
1221 	&dev_attr_active_icc_levels_vcc9.attr,
1222 	&dev_attr_active_icc_levels_vcc10.attr,
1223 	&dev_attr_active_icc_levels_vcc11.attr,
1224 	&dev_attr_active_icc_levels_vcc12.attr,
1225 	&dev_attr_active_icc_levels_vcc13.attr,
1226 	&dev_attr_active_icc_levels_vcc14.attr,
1227 	&dev_attr_active_icc_levels_vcc15.attr,
1228 	&dev_attr_active_icc_levels_vccq0.attr,
1229 	&dev_attr_active_icc_levels_vccq1.attr,
1230 	&dev_attr_active_icc_levels_vccq2.attr,
1231 	&dev_attr_active_icc_levels_vccq3.attr,
1232 	&dev_attr_active_icc_levels_vccq4.attr,
1233 	&dev_attr_active_icc_levels_vccq5.attr,
1234 	&dev_attr_active_icc_levels_vccq6.attr,
1235 	&dev_attr_active_icc_levels_vccq7.attr,
1236 	&dev_attr_active_icc_levels_vccq8.attr,
1237 	&dev_attr_active_icc_levels_vccq9.attr,
1238 	&dev_attr_active_icc_levels_vccq10.attr,
1239 	&dev_attr_active_icc_levels_vccq11.attr,
1240 	&dev_attr_active_icc_levels_vccq12.attr,
1241 	&dev_attr_active_icc_levels_vccq13.attr,
1242 	&dev_attr_active_icc_levels_vccq14.attr,
1243 	&dev_attr_active_icc_levels_vccq15.attr,
1244 	&dev_attr_active_icc_levels_vccq20.attr,
1245 	&dev_attr_active_icc_levels_vccq21.attr,
1246 	&dev_attr_active_icc_levels_vccq22.attr,
1247 	&dev_attr_active_icc_levels_vccq23.attr,
1248 	&dev_attr_active_icc_levels_vccq24.attr,
1249 	&dev_attr_active_icc_levels_vccq25.attr,
1250 	&dev_attr_active_icc_levels_vccq26.attr,
1251 	&dev_attr_active_icc_levels_vccq27.attr,
1252 	&dev_attr_active_icc_levels_vccq28.attr,
1253 	&dev_attr_active_icc_levels_vccq29.attr,
1254 	&dev_attr_active_icc_levels_vccq210.attr,
1255 	&dev_attr_active_icc_levels_vccq211.attr,
1256 	&dev_attr_active_icc_levels_vccq212.attr,
1257 	&dev_attr_active_icc_levels_vccq213.attr,
1258 	&dev_attr_active_icc_levels_vccq214.attr,
1259 	&dev_attr_active_icc_levels_vccq215.attr,
1260 	NULL,
1261 };
1262 
1263 static const struct attribute_group ufs_sysfs_power_descriptor_group = {
1264 	.name = "power_descriptor",
1265 	.attrs = ufs_sysfs_power_descriptor,
1266 };
1267 
1268 #define UFS_STRING_DESCRIPTOR(_name, _pname)				\
1269 static ssize_t _name##_show(struct device *dev,				\
1270 	struct device_attribute *attr, char *buf)			\
1271 {									\
1272 	u8 index;							\
1273 	struct ufs_hba *hba = dev_get_drvdata(dev);			\
1274 	int ret;							\
1275 	int desc_len = QUERY_DESC_MAX_SIZE;				\
1276 	u8 *desc_buf;							\
1277 									\
1278 	down(&hba->host_sem);						\
1279 	if (!ufshcd_is_user_access_allowed(hba)) {			\
1280 		up(&hba->host_sem);					\
1281 		return -EBUSY;						\
1282 	}								\
1283 	desc_buf = kzalloc(QUERY_DESC_MAX_SIZE, GFP_ATOMIC);		\
1284 	if (!desc_buf) {						\
1285 		up(&hba->host_sem);					\
1286 		return -ENOMEM;						\
1287 	}								\
1288 	ufshcd_rpm_get_sync(hba);					\
1289 	ret = ufshcd_query_descriptor_retry(hba,			\
1290 		UPIU_QUERY_OPCODE_READ_DESC, QUERY_DESC_IDN_DEVICE,	\
1291 		0, 0, desc_buf, &desc_len);				\
1292 	if (ret) {							\
1293 		ret = -EINVAL;						\
1294 		goto out;						\
1295 	}								\
1296 	index = desc_buf[DEVICE_DESC_PARAM##_pname];			\
1297 	kfree(desc_buf);						\
1298 	desc_buf = NULL;						\
1299 	ret = ufshcd_read_string_desc(hba, index, &desc_buf,		\
1300 				      SD_ASCII_STD);			\
1301 	if (ret < 0)							\
1302 		goto out;						\
1303 	ret = sysfs_emit(buf, "%s\n", desc_buf);			\
1304 out:									\
1305 	ufshcd_rpm_put_sync(hba);					\
1306 	kfree(desc_buf);						\
1307 	up(&hba->host_sem);						\
1308 	return ret;							\
1309 }									\
1310 static DEVICE_ATTR_RO(_name)
1311 
1312 UFS_STRING_DESCRIPTOR(manufacturer_name, _MANF_NAME);
1313 UFS_STRING_DESCRIPTOR(product_name, _PRDCT_NAME);
1314 UFS_STRING_DESCRIPTOR(oem_id, _OEM_ID);
1315 UFS_STRING_DESCRIPTOR(serial_number, _SN);
1316 UFS_STRING_DESCRIPTOR(product_revision, _PRDCT_REV);
1317 
1318 static struct attribute *ufs_sysfs_string_descriptors[] = {
1319 	&dev_attr_manufacturer_name.attr,
1320 	&dev_attr_product_name.attr,
1321 	&dev_attr_oem_id.attr,
1322 	&dev_attr_serial_number.attr,
1323 	&dev_attr_product_revision.attr,
1324 	NULL,
1325 };
1326 
1327 static const struct attribute_group ufs_sysfs_string_descriptors_group = {
1328 	.name = "string_descriptors",
1329 	.attrs = ufs_sysfs_string_descriptors,
1330 };
1331 
ufshcd_is_wb_flags(enum flag_idn idn)1332 static inline bool ufshcd_is_wb_flags(enum flag_idn idn)
1333 {
1334 	return idn >= QUERY_FLAG_IDN_WB_EN &&
1335 		idn <= QUERY_FLAG_IDN_WB_BUFF_FLUSH_DURING_HIBERN8;
1336 }
1337 
1338 #define UFS_FLAG(_name, _uname)						\
1339 static ssize_t _name##_show(struct device *dev,				\
1340 	struct device_attribute *attr, char *buf)			\
1341 {									\
1342 	bool flag;							\
1343 	u8 index = 0;							\
1344 	int ret;							\
1345 	struct ufs_hba *hba = dev_get_drvdata(dev);			\
1346 									\
1347 	down(&hba->host_sem);						\
1348 	if (!ufshcd_is_user_access_allowed(hba)) {			\
1349 		up(&hba->host_sem);					\
1350 		return -EBUSY;						\
1351 	}								\
1352 	if (ufshcd_is_wb_flags(QUERY_FLAG_IDN##_uname))			\
1353 		index = ufshcd_wb_get_query_index(hba);			\
1354 	ufshcd_rpm_get_sync(hba);					\
1355 	ret = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_READ_FLAG,	\
1356 		QUERY_FLAG_IDN##_uname, index, &flag);			\
1357 	ufshcd_rpm_put_sync(hba);					\
1358 	if (ret) {							\
1359 		ret = -EINVAL;						\
1360 		goto out;						\
1361 	}								\
1362 	ret = sysfs_emit(buf, "%s\n", flag ? "true" : "false");		\
1363 out:									\
1364 	up(&hba->host_sem);						\
1365 	return ret;							\
1366 }									\
1367 static DEVICE_ATTR_RO(_name)
1368 
1369 UFS_FLAG(device_init, _FDEVICEINIT);
1370 UFS_FLAG(permanent_wpe, _PERMANENT_WPE);
1371 UFS_FLAG(power_on_wpe, _PWR_ON_WPE);
1372 UFS_FLAG(bkops_enable, _BKOPS_EN);
1373 UFS_FLAG(life_span_mode_enable, _LIFE_SPAN_MODE_ENABLE);
1374 UFS_FLAG(phy_resource_removal, _FPHYRESOURCEREMOVAL);
1375 UFS_FLAG(busy_rtc, _BUSY_RTC);
1376 UFS_FLAG(disable_fw_update, _PERMANENTLY_DISABLE_FW_UPDATE);
1377 UFS_FLAG(wb_enable, _WB_EN);
1378 UFS_FLAG(wb_flush_en, _WB_BUFF_FLUSH_EN);
1379 UFS_FLAG(wb_flush_during_h8, _WB_BUFF_FLUSH_DURING_HIBERN8);
1380 
1381 static struct attribute *ufs_sysfs_device_flags[] = {
1382 	&dev_attr_device_init.attr,
1383 	&dev_attr_permanent_wpe.attr,
1384 	&dev_attr_power_on_wpe.attr,
1385 	&dev_attr_bkops_enable.attr,
1386 	&dev_attr_life_span_mode_enable.attr,
1387 	&dev_attr_phy_resource_removal.attr,
1388 	&dev_attr_busy_rtc.attr,
1389 	&dev_attr_disable_fw_update.attr,
1390 	&dev_attr_wb_enable.attr,
1391 	&dev_attr_wb_flush_en.attr,
1392 	&dev_attr_wb_flush_during_h8.attr,
1393 	NULL,
1394 };
1395 
1396 static const struct attribute_group ufs_sysfs_flags_group = {
1397 	.name = "flags",
1398 	.attrs = ufs_sysfs_device_flags,
1399 };
1400 
max_number_of_rtt_show(struct device * dev,struct device_attribute * attr,char * buf)1401 static ssize_t max_number_of_rtt_show(struct device *dev,
1402 				      struct device_attribute *attr, char *buf)
1403 {
1404 	struct ufs_hba *hba = dev_get_drvdata(dev);
1405 	u32 rtt;
1406 	int ret;
1407 
1408 	down(&hba->host_sem);
1409 	if (!ufshcd_is_user_access_allowed(hba)) {
1410 		up(&hba->host_sem);
1411 		return -EBUSY;
1412 	}
1413 
1414 	ufshcd_rpm_get_sync(hba);
1415 	ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
1416 		QUERY_ATTR_IDN_MAX_NUM_OF_RTT, 0, 0, &rtt);
1417 	ufshcd_rpm_put_sync(hba);
1418 
1419 	if (ret)
1420 		goto out;
1421 
1422 	ret = sysfs_emit(buf, "0x%08X\n", rtt);
1423 
1424 out:
1425 	up(&hba->host_sem);
1426 	return ret;
1427 }
1428 
max_number_of_rtt_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1429 static ssize_t max_number_of_rtt_store(struct device *dev,
1430 				       struct device_attribute *attr,
1431 				       const char *buf, size_t count)
1432 {
1433 	struct ufs_hba *hba = dev_get_drvdata(dev);
1434 	struct ufs_dev_info *dev_info = &hba->dev_info;
1435 	struct scsi_device *sdev;
1436 	unsigned int rtt;
1437 	int ret;
1438 
1439 	if (kstrtouint(buf, 0, &rtt))
1440 		return -EINVAL;
1441 
1442 	if (rtt > dev_info->rtt_cap) {
1443 		dev_err(dev, "rtt can be at most bDeviceRTTCap\n");
1444 		return -EINVAL;
1445 	}
1446 
1447 	down(&hba->host_sem);
1448 	if (!ufshcd_is_user_access_allowed(hba)) {
1449 		ret = -EBUSY;
1450 		goto out;
1451 	}
1452 
1453 	ufshcd_rpm_get_sync(hba);
1454 
1455 	shost_for_each_device(sdev, hba->host)
1456 		blk_mq_freeze_queue(sdev->request_queue);
1457 
1458 	ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
1459 		QUERY_ATTR_IDN_MAX_NUM_OF_RTT, 0, 0, &rtt);
1460 
1461 	shost_for_each_device(sdev, hba->host)
1462 		blk_mq_unfreeze_queue(sdev->request_queue);
1463 
1464 	ufshcd_rpm_put_sync(hba);
1465 
1466 out:
1467 	up(&hba->host_sem);
1468 	return ret < 0 ? ret : count;
1469 }
1470 
1471 static DEVICE_ATTR_RW(max_number_of_rtt);
1472 
ufshcd_is_wb_attrs(enum attr_idn idn)1473 static inline bool ufshcd_is_wb_attrs(enum attr_idn idn)
1474 {
1475 	return idn >= QUERY_ATTR_IDN_WB_FLUSH_STATUS &&
1476 		idn <= QUERY_ATTR_IDN_CURR_WB_BUFF_SIZE;
1477 }
1478 
1479 #define UFS_ATTRIBUTE(_name, _uname)					\
1480 static ssize_t _name##_show(struct device *dev,				\
1481 	struct device_attribute *attr, char *buf)			\
1482 {									\
1483 	struct ufs_hba *hba = dev_get_drvdata(dev);			\
1484 	u32 value;							\
1485 	int ret;							\
1486 	u8 index = 0;							\
1487 									\
1488 	down(&hba->host_sem);						\
1489 	if (!ufshcd_is_user_access_allowed(hba)) {			\
1490 		up(&hba->host_sem);					\
1491 		return -EBUSY;						\
1492 	}								\
1493 	if (ufshcd_is_wb_attrs(QUERY_ATTR_IDN##_uname))			\
1494 		index = ufshcd_wb_get_query_index(hba);			\
1495 	ufshcd_rpm_get_sync(hba);					\
1496 	ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,	\
1497 		QUERY_ATTR_IDN##_uname, index, 0, &value);		\
1498 	ufshcd_rpm_put_sync(hba);					\
1499 	if (ret) {							\
1500 		ret = -EINVAL;						\
1501 		goto out;						\
1502 	}								\
1503 	ret = sysfs_emit(buf, "0x%08X\n", value);			\
1504 out:									\
1505 	up(&hba->host_sem);						\
1506 	return ret;							\
1507 }									\
1508 static DEVICE_ATTR_RO(_name)
1509 
1510 UFS_ATTRIBUTE(boot_lun_enabled, _BOOT_LU_EN);
1511 UFS_ATTRIBUTE(current_power_mode, _POWER_MODE);
1512 UFS_ATTRIBUTE(active_icc_level, _ACTIVE_ICC_LVL);
1513 UFS_ATTRIBUTE(ooo_data_enabled, _OOO_DATA_EN);
1514 UFS_ATTRIBUTE(bkops_status, _BKOPS_STATUS);
1515 UFS_ATTRIBUTE(purge_status, _PURGE_STATUS);
1516 UFS_ATTRIBUTE(max_data_in_size, _MAX_DATA_IN);
1517 UFS_ATTRIBUTE(max_data_out_size, _MAX_DATA_OUT);
1518 UFS_ATTRIBUTE(reference_clock_frequency, _REF_CLK_FREQ);
1519 UFS_ATTRIBUTE(configuration_descriptor_lock, _CONF_DESC_LOCK);
1520 UFS_ATTRIBUTE(exception_event_control, _EE_CONTROL);
1521 UFS_ATTRIBUTE(exception_event_status, _EE_STATUS);
1522 UFS_ATTRIBUTE(ffu_status, _FFU_STATUS);
1523 UFS_ATTRIBUTE(psa_state, _PSA_STATE);
1524 UFS_ATTRIBUTE(psa_data_size, _PSA_DATA_SIZE);
1525 UFS_ATTRIBUTE(wb_flush_status, _WB_FLUSH_STATUS);
1526 UFS_ATTRIBUTE(wb_avail_buf, _AVAIL_WB_BUFF_SIZE);
1527 UFS_ATTRIBUTE(wb_life_time_est, _WB_BUFF_LIFE_TIME_EST);
1528 UFS_ATTRIBUTE(wb_cur_buf, _CURR_WB_BUFF_SIZE);
1529 
1530 
1531 static struct attribute *ufs_sysfs_attributes[] = {
1532 	&dev_attr_boot_lun_enabled.attr,
1533 	&dev_attr_current_power_mode.attr,
1534 	&dev_attr_active_icc_level.attr,
1535 	&dev_attr_ooo_data_enabled.attr,
1536 	&dev_attr_bkops_status.attr,
1537 	&dev_attr_purge_status.attr,
1538 	&dev_attr_max_data_in_size.attr,
1539 	&dev_attr_max_data_out_size.attr,
1540 	&dev_attr_reference_clock_frequency.attr,
1541 	&dev_attr_configuration_descriptor_lock.attr,
1542 	&dev_attr_max_number_of_rtt.attr,
1543 	&dev_attr_exception_event_control.attr,
1544 	&dev_attr_exception_event_status.attr,
1545 	&dev_attr_ffu_status.attr,
1546 	&dev_attr_psa_state.attr,
1547 	&dev_attr_psa_data_size.attr,
1548 	&dev_attr_wb_flush_status.attr,
1549 	&dev_attr_wb_avail_buf.attr,
1550 	&dev_attr_wb_life_time_est.attr,
1551 	&dev_attr_wb_cur_buf.attr,
1552 	NULL,
1553 };
1554 
1555 static const struct attribute_group ufs_sysfs_attributes_group = {
1556 	.name = "attributes",
1557 	.attrs = ufs_sysfs_attributes,
1558 };
1559 
1560 static const struct attribute_group *ufs_sysfs_groups[] = {
1561 	&ufs_sysfs_default_group,
1562 	&ufs_sysfs_capabilities_group,
1563 	&ufs_sysfs_ufshci_group,
1564 	&ufs_sysfs_monitor_group,
1565 	&ufs_sysfs_power_info_group,
1566 	&ufs_sysfs_device_descriptor_group,
1567 	&ufs_sysfs_interconnect_descriptor_group,
1568 	&ufs_sysfs_geometry_descriptor_group,
1569 	&ufs_sysfs_health_descriptor_group,
1570 	&ufs_sysfs_power_descriptor_group,
1571 	&ufs_sysfs_string_descriptors_group,
1572 	&ufs_sysfs_flags_group,
1573 	&ufs_sysfs_attributes_group,
1574 	NULL,
1575 };
1576 
1577 #define UFS_LUN_DESC_PARAM(_pname, _puname, _duname, _size)		\
1578 static ssize_t _pname##_show(struct device *dev,			\
1579 	struct device_attribute *attr, char *buf)			\
1580 {									\
1581 	struct scsi_device *sdev = to_scsi_device(dev);			\
1582 	struct ufs_hba *hba = shost_priv(sdev->host);			\
1583 	u8 lun = ufshcd_scsi_to_upiu_lun(sdev->lun);			\
1584 	if (!ufs_is_valid_unit_desc_lun(&hba->dev_info, lun))		\
1585 		return -EINVAL;						\
1586 	return ufs_sysfs_read_desc_param(hba, QUERY_DESC_IDN_##_duname,	\
1587 		lun, _duname##_DESC_PARAM##_puname, buf, _size);	\
1588 }									\
1589 static DEVICE_ATTR_RO(_pname)
1590 
1591 #define UFS_UNIT_DESC_PARAM(_name, _uname, _size)			\
1592 	UFS_LUN_DESC_PARAM(_name, _uname, UNIT, _size)
1593 
1594 UFS_UNIT_DESC_PARAM(lu_enable, _LU_ENABLE, 1);
1595 UFS_UNIT_DESC_PARAM(boot_lun_id, _BOOT_LUN_ID, 1);
1596 UFS_UNIT_DESC_PARAM(lun_write_protect, _LU_WR_PROTECT, 1);
1597 UFS_UNIT_DESC_PARAM(lun_queue_depth, _LU_Q_DEPTH, 1);
1598 UFS_UNIT_DESC_PARAM(psa_sensitive, _PSA_SENSITIVE, 1);
1599 UFS_UNIT_DESC_PARAM(lun_memory_type, _MEM_TYPE, 1);
1600 UFS_UNIT_DESC_PARAM(data_reliability, _DATA_RELIABILITY, 1);
1601 UFS_UNIT_DESC_PARAM(logical_block_size, _LOGICAL_BLK_SIZE, 1);
1602 UFS_UNIT_DESC_PARAM(logical_block_count, _LOGICAL_BLK_COUNT, 8);
1603 UFS_UNIT_DESC_PARAM(erase_block_size, _ERASE_BLK_SIZE, 4);
1604 UFS_UNIT_DESC_PARAM(provisioning_type, _PROVISIONING_TYPE, 1);
1605 UFS_UNIT_DESC_PARAM(physical_memory_resourse_count, _PHY_MEM_RSRC_CNT, 8);
1606 UFS_UNIT_DESC_PARAM(context_capabilities, _CTX_CAPABILITIES, 2);
1607 UFS_UNIT_DESC_PARAM(large_unit_granularity, _LARGE_UNIT_SIZE_M1, 1);
1608 UFS_UNIT_DESC_PARAM(wb_buf_alloc_units, _WB_BUF_ALLOC_UNITS, 4);
1609 
1610 static struct attribute *ufs_sysfs_unit_descriptor[] = {
1611 	&dev_attr_lu_enable.attr,
1612 	&dev_attr_boot_lun_id.attr,
1613 	&dev_attr_lun_write_protect.attr,
1614 	&dev_attr_lun_queue_depth.attr,
1615 	&dev_attr_psa_sensitive.attr,
1616 	&dev_attr_lun_memory_type.attr,
1617 	&dev_attr_data_reliability.attr,
1618 	&dev_attr_logical_block_size.attr,
1619 	&dev_attr_logical_block_count.attr,
1620 	&dev_attr_erase_block_size.attr,
1621 	&dev_attr_provisioning_type.attr,
1622 	&dev_attr_physical_memory_resourse_count.attr,
1623 	&dev_attr_context_capabilities.attr,
1624 	&dev_attr_large_unit_granularity.attr,
1625 	&dev_attr_wb_buf_alloc_units.attr,
1626 	NULL,
1627 };
1628 
ufs_unit_descriptor_is_visible(struct kobject * kobj,struct attribute * attr,int n)1629 static umode_t ufs_unit_descriptor_is_visible(struct kobject *kobj, struct attribute *attr, int n)
1630 {
1631 	struct device *dev = container_of(kobj, struct device, kobj);
1632 	struct scsi_device *sdev = to_scsi_device(dev);
1633 	u8 lun = ufshcd_scsi_to_upiu_lun(sdev->lun);
1634 	umode_t mode = attr->mode;
1635 
1636 	if (lun == UFS_UPIU_BOOT_WLUN || lun == UFS_UPIU_UFS_DEVICE_WLUN)
1637 		/* Boot and device WLUN have no unit descriptors */
1638 		mode = 0;
1639 	if (lun == UFS_UPIU_RPMB_WLUN && attr == &dev_attr_wb_buf_alloc_units.attr)
1640 		mode = 0;
1641 
1642 	return mode;
1643 }
1644 
1645 
1646 const struct attribute_group ufs_sysfs_unit_descriptor_group = {
1647 	.name = "unit_descriptor",
1648 	.attrs = ufs_sysfs_unit_descriptor,
1649 	.is_visible = ufs_unit_descriptor_is_visible,
1650 };
1651 
dyn_cap_needed_attribute_show(struct device * dev,struct device_attribute * attr,char * buf)1652 static ssize_t dyn_cap_needed_attribute_show(struct device *dev,
1653 	struct device_attribute *attr, char *buf)
1654 {
1655 	u32 value;
1656 	struct scsi_device *sdev = to_scsi_device(dev);
1657 	struct ufs_hba *hba = shost_priv(sdev->host);
1658 	u8 lun = ufshcd_scsi_to_upiu_lun(sdev->lun);
1659 	int ret;
1660 
1661 	down(&hba->host_sem);
1662 	if (!ufshcd_is_user_access_allowed(hba)) {
1663 		ret = -EBUSY;
1664 		goto out;
1665 	}
1666 
1667 	ufshcd_rpm_get_sync(hba);
1668 	ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
1669 		QUERY_ATTR_IDN_DYN_CAP_NEEDED, lun, 0, &value);
1670 	ufshcd_rpm_put_sync(hba);
1671 	if (ret) {
1672 		ret = -EINVAL;
1673 		goto out;
1674 	}
1675 
1676 	ret = sysfs_emit(buf, "0x%08X\n", value);
1677 
1678 out:
1679 	up(&hba->host_sem);
1680 	return ret;
1681 }
1682 static DEVICE_ATTR_RO(dyn_cap_needed_attribute);
1683 
1684 static struct attribute *ufs_sysfs_lun_attributes[] = {
1685 	&dev_attr_dyn_cap_needed_attribute.attr,
1686 	NULL,
1687 };
1688 
1689 const struct attribute_group ufs_sysfs_lun_attributes_group = {
1690 	.attrs = ufs_sysfs_lun_attributes,
1691 };
1692 
ufs_sysfs_add_nodes(struct device * dev)1693 void ufs_sysfs_add_nodes(struct device *dev)
1694 {
1695 	int ret;
1696 
1697 	ret = sysfs_create_groups(&dev->kobj, ufs_sysfs_groups);
1698 	if (ret)
1699 		dev_err(dev,
1700 			"%s: sysfs groups creation failed (err = %d)\n",
1701 			__func__, ret);
1702 }
1703 
ufs_sysfs_remove_nodes(struct device * dev)1704 void ufs_sysfs_remove_nodes(struct device *dev)
1705 {
1706 	sysfs_remove_groups(&dev->kobj, ufs_sysfs_groups);
1707 }
1708