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