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