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