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