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