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