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