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