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