1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2020 Intel Corporation
3
4 #include <linux/debugfs.h>
5
6 #include "ufs-debugfs.h"
7 #include <ufs/ufshcd.h>
8 #include "ufshcd-priv.h"
9
10 static struct dentry *ufs_debugfs_root;
11
12 struct ufs_debugfs_attr {
13 const char *name;
14 mode_t mode;
15 const struct file_operations *fops;
16 };
17
18 /* @file corresponds to a debugfs attribute in directory hba->debugfs_root. */
hba_from_file(const struct file * file)19 static inline struct ufs_hba *hba_from_file(const struct file *file)
20 {
21 return d_inode(file->f_path.dentry->d_parent)->i_private;
22 }
23
ufs_debugfs_init(void)24 void __init ufs_debugfs_init(void)
25 {
26 ufs_debugfs_root = debugfs_create_dir("ufshcd", NULL);
27 }
28
ufs_debugfs_exit(void)29 void ufs_debugfs_exit(void)
30 {
31 debugfs_remove_recursive(ufs_debugfs_root);
32 }
33
ufs_debugfs_stats_show(struct seq_file * s,void * data)34 static int ufs_debugfs_stats_show(struct seq_file *s, void *data)
35 {
36 struct ufs_hba *hba = hba_from_file(s->file);
37 struct ufs_event_hist *e = hba->ufs_stats.event;
38
39 #define PRT(fmt, typ) \
40 seq_printf(s, fmt, e[UFS_EVT_ ## typ].cnt)
41
42 PRT("PHY Adapter Layer errors (except LINERESET): %llu\n", PA_ERR);
43 PRT("Data Link Layer errors: %llu\n", DL_ERR);
44 PRT("Network Layer errors: %llu\n", NL_ERR);
45 PRT("Transport Layer errors: %llu\n", TL_ERR);
46 PRT("Generic DME errors: %llu\n", DME_ERR);
47 PRT("Auto-hibernate errors: %llu\n", AUTO_HIBERN8_ERR);
48 PRT("IS Fatal errors (CEFES, SBFES, HCFES, DFES): %llu\n", FATAL_ERR);
49 PRT("DME Link Startup errors: %llu\n", LINK_STARTUP_FAIL);
50 PRT("PM Resume errors: %llu\n", RESUME_ERR);
51 PRT("PM Suspend errors : %llu\n", SUSPEND_ERR);
52 PRT("Logical Unit Resets: %llu\n", DEV_RESET);
53 PRT("Host Resets: %llu\n", HOST_RESET);
54 PRT("SCSI command aborts: %llu\n", ABORT);
55 #undef PRT
56 return 0;
57 }
58 DEFINE_SHOW_ATTRIBUTE(ufs_debugfs_stats);
59
ee_usr_mask_get(void * data,u64 * val)60 static int ee_usr_mask_get(void *data, u64 *val)
61 {
62 struct ufs_hba *hba = data;
63
64 *val = hba->ee_usr_mask;
65 return 0;
66 }
67
68 token_context_lock(ufs_debugfs);
69
ufs_debugfs_get_user_access(struct ufs_hba * hba)70 static int ufs_debugfs_get_user_access(struct ufs_hba *hba)
71 __cond_acquires(0, ufs_debugfs)
72 {
73 down(&hba->host_sem);
74 if (!ufshcd_is_user_access_allowed(hba)) {
75 up(&hba->host_sem);
76 return -EBUSY;
77 }
78 ufshcd_rpm_get_sync(hba);
79 __acquire(ufs_debugfs);
80 return 0;
81 }
82
ufs_debugfs_put_user_access(struct ufs_hba * hba)83 static void ufs_debugfs_put_user_access(struct ufs_hba *hba)
84 __releases(ufs_debugfs)
85 {
86 ufshcd_rpm_put_sync(hba);
87 up(&hba->host_sem);
88 __release(ufs_debugfs);
89 }
90
ee_usr_mask_set(void * data,u64 val)91 static int ee_usr_mask_set(void *data, u64 val)
92 {
93 struct ufs_hba *hba = data;
94 int err;
95
96 if (val & ~(u64)MASK_EE_STATUS)
97 return -EINVAL;
98 err = ufs_debugfs_get_user_access(hba);
99 if (err)
100 return err;
101 err = ufshcd_update_ee_usr_mask(hba, val, MASK_EE_STATUS);
102 ufs_debugfs_put_user_access(hba);
103 return err;
104 }
105
106 DEFINE_DEBUGFS_ATTRIBUTE(ee_usr_mask_fops, ee_usr_mask_get, ee_usr_mask_set, "%#llx\n");
107
ufs_debugfs_exception_event(struct ufs_hba * hba,u16 status)108 void ufs_debugfs_exception_event(struct ufs_hba *hba, u16 status)
109 {
110 bool chgd = false;
111 u16 ee_ctrl_mask;
112 int err = 0;
113
114 if (!hba->debugfs_ee_rate_limit_ms || !status)
115 return;
116
117 mutex_lock(&hba->ee_ctrl_mutex);
118 ee_ctrl_mask = hba->ee_drv_mask | (hba->ee_usr_mask & ~status);
119 chgd = ee_ctrl_mask != hba->ee_ctrl_mask;
120 if (chgd) {
121 err = __ufshcd_write_ee_control(hba, ee_ctrl_mask);
122 if (err)
123 dev_err(hba->dev, "%s: failed to write ee control %d\n",
124 __func__, err);
125 }
126 mutex_unlock(&hba->ee_ctrl_mutex);
127
128 if (chgd && !err) {
129 unsigned long delay = msecs_to_jiffies(hba->debugfs_ee_rate_limit_ms);
130
131 queue_delayed_work(system_freezable_wq, &hba->debugfs_ee_work, delay);
132 }
133 }
134
ufs_debugfs_restart_ee(struct work_struct * work)135 static void ufs_debugfs_restart_ee(struct work_struct *work)
136 {
137 struct ufs_hba *hba = container_of(work, struct ufs_hba, debugfs_ee_work.work);
138
139 if (!hba->ee_usr_mask || pm_runtime_suspended(hba->dev) ||
140 ufs_debugfs_get_user_access(hba))
141 return;
142 ufshcd_write_ee_control(hba);
143 ufs_debugfs_put_user_access(hba);
144 }
145
ufs_saved_err_show(struct seq_file * s,void * data)146 static int ufs_saved_err_show(struct seq_file *s, void *data)
147 {
148 struct ufs_debugfs_attr *attr = s->private;
149 struct ufs_hba *hba = hba_from_file(s->file);
150 const int *p;
151
152 if (strcmp(attr->name, "saved_err") == 0) {
153 p = &hba->saved_err;
154 } else if (strcmp(attr->name, "saved_uic_err") == 0) {
155 p = &hba->saved_uic_err;
156 } else {
157 return -ENOENT;
158 }
159
160 seq_printf(s, "%d\n", *p);
161 return 0;
162 }
163
ufs_saved_err_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)164 static ssize_t ufs_saved_err_write(struct file *file, const char __user *buf,
165 size_t count, loff_t *ppos)
166 {
167 struct ufs_debugfs_attr *attr = file->f_inode->i_private;
168 struct ufs_hba *hba = hba_from_file(file);
169 char val_str[16] = { };
170 int val, ret;
171
172 if (count >= sizeof(val_str))
173 return -EINVAL;
174 if (copy_from_user(val_str, buf, count))
175 return -EFAULT;
176 ret = kstrtoint(val_str, 0, &val);
177 if (ret < 0)
178 return ret;
179
180 spin_lock_irq(hba->host->host_lock);
181 if (strcmp(attr->name, "saved_err") == 0) {
182 hba->saved_err = val;
183 } else if (strcmp(attr->name, "saved_uic_err") == 0) {
184 hba->saved_uic_err = val;
185 } else {
186 ret = -ENOENT;
187 }
188 if (ret == 0)
189 ufshcd_schedule_eh_work(hba);
190 spin_unlock_irq(hba->host->host_lock);
191
192 return ret < 0 ? ret : count;
193 }
194
ufs_saved_err_open(struct inode * inode,struct file * file)195 static int ufs_saved_err_open(struct inode *inode, struct file *file)
196 {
197 return single_open(file, ufs_saved_err_show, inode->i_private);
198 }
199
200 static const struct file_operations ufs_saved_err_fops = {
201 .owner = THIS_MODULE,
202 .open = ufs_saved_err_open,
203 .read = seq_read,
204 .write = ufs_saved_err_write,
205 .llseek = seq_lseek,
206 .release = single_release,
207 };
208
209 static const struct ufs_debugfs_attr ufs_attrs[] = {
210 { "stats", 0400, &ufs_debugfs_stats_fops },
211 { "saved_err", 0600, &ufs_saved_err_fops },
212 { "saved_uic_err", 0600, &ufs_saved_err_fops },
213 { }
214 };
215
ufs_tx_eq_params_show(struct seq_file * s,void * data)216 static int ufs_tx_eq_params_show(struct seq_file *s, void *data)
217 {
218 const char *file_name = s->file->f_path.dentry->d_name.name;
219 u32 gear = (u32)(uintptr_t)s->file->f_inode->i_private;
220 struct ufs_hba *hba = hba_from_file(s->file);
221 struct ufshcd_tx_eq_settings *settings;
222 struct ufs_pa_layer_attr *pwr_info;
223 struct ufshcd_tx_eq_params *params;
224 u32 rate = hba->pwr_info.hs_rate;
225 u32 num_lanes;
226 int lane;
227
228 if (!ufshcd_is_tx_eq_supported(hba))
229 return -EOPNOTSUPP;
230
231 if (gear < UFS_HS_G1 || gear > UFS_HS_GEAR_MAX) {
232 seq_printf(s, "Invalid gear selected: %u\n", gear);
233 return 0;
234 }
235
236 if (!hba->max_pwr_info.is_valid) {
237 seq_puts(s, "Max power info is invalid\n");
238 return 0;
239 }
240
241 pwr_info = &hba->max_pwr_info.info;
242 params = &hba->tx_eq_params[gear - 1];
243 if (!params->is_valid) {
244 seq_printf(s, "TX EQ params are invalid for HS-G%u, Rate-%s\n",
245 gear, ufs_hs_rate_to_str(rate));
246 return 0;
247 }
248
249 if (strcmp(file_name, "host_tx_eq_params") == 0) {
250 settings = params->host;
251 num_lanes = pwr_info->lane_tx;
252 seq_printf(s, "Host TX EQ PreShoot Cap: 0x%02x, DeEmphasis Cap: 0x%02x\n",
253 hba->host_preshoot_cap, hba->host_deemphasis_cap);
254 } else if (strcmp(file_name, "device_tx_eq_params") == 0) {
255 settings = params->device;
256 num_lanes = pwr_info->lane_rx;
257 seq_printf(s, "Device TX EQ PreShoot Cap: 0x%02x, DeEmphasis Cap: 0x%02x\n",
258 hba->device_preshoot_cap, hba->device_deemphasis_cap);
259 } else {
260 return -ENOENT;
261 }
262
263 seq_printf(s, "TX EQ setting for HS-G%u, Rate-%s:\n", gear,
264 ufs_hs_rate_to_str(rate));
265 for (lane = 0; lane < num_lanes; lane++)
266 seq_printf(s, "TX Lane %d - PreShoot: %d, DeEmphasis: %d, Pre-Coding %senabled\n",
267 lane, settings[lane].preshoot,
268 settings[lane].deemphasis,
269 settings[lane].precode_en ? "" : "not ");
270
271 return 0;
272 }
273
ufs_tx_eq_params_open(struct inode * inode,struct file * file)274 static int ufs_tx_eq_params_open(struct inode *inode, struct file *file)
275 {
276 return single_open(file, ufs_tx_eq_params_show, inode->i_private);
277 }
278
279 static const struct file_operations ufs_tx_eq_params_fops = {
280 .owner = THIS_MODULE,
281 .open = ufs_tx_eq_params_open,
282 .read = seq_read,
283 .llseek = seq_lseek,
284 .release = single_release,
285 };
286
287 static const struct ufs_debugfs_attr ufs_tx_eq_attrs[] = {
288 { "host_tx_eq_params", 0400, &ufs_tx_eq_params_fops },
289 { "device_tx_eq_params", 0400, &ufs_tx_eq_params_fops },
290 { }
291 };
292
ufs_tx_eqtr_record_show(struct seq_file * s,void * data)293 static int ufs_tx_eqtr_record_show(struct seq_file *s, void *data)
294 {
295 const char *file_name = s->file->f_path.dentry->d_name.name;
296 u8 (*fom_array)[TX_HS_NUM_PRESHOOT][TX_HS_NUM_DEEMPHASIS];
297 u32 gear = (u32)(uintptr_t)s->file->f_inode->i_private;
298 unsigned long preshoot_bitmap, deemphasis_bitmap;
299 struct ufs_hba *hba = hba_from_file(s->file);
300 struct ufs_pa_layer_attr *pwr_info;
301 struct ufshcd_tx_eq_params *params;
302 struct ufshcd_tx_eqtr_record *rec;
303 u32 rate = hba->pwr_info.hs_rate;
304 u8 preshoot, deemphasis;
305 u32 num_lanes;
306 char name[32];
307 int lane;
308
309 if (!ufshcd_is_tx_eq_supported(hba))
310 return -EOPNOTSUPP;
311
312 if (gear < UFS_HS_G1 || gear > UFS_HS_GEAR_MAX) {
313 seq_printf(s, "Invalid gear selected: %u\n", gear);
314 return 0;
315 }
316
317 if (!hba->max_pwr_info.is_valid) {
318 seq_puts(s, "Max power info is invalid\n");
319 return 0;
320 }
321
322 pwr_info = &hba->max_pwr_info.info;
323 params = &hba->tx_eq_params[gear - 1];
324 if (!params->is_valid) {
325 seq_printf(s, "TX EQ params are invalid for HS-G%u, Rate-%s\n",
326 gear, ufs_hs_rate_to_str(rate));
327 return 0;
328 }
329
330 rec = params->eqtr_record;
331 if (!rec || !rec->last_record_index) {
332 seq_printf(s, "No TX EQTR records found for HS-G%u, Rate-%s.\n",
333 gear, ufs_hs_rate_to_str(rate));
334 return 0;
335 }
336
337 if (strcmp(file_name, "host_tx_eqtr_record") == 0) {
338 preshoot_bitmap = (hba->host_preshoot_cap << 0x1) | 0x1;
339 deemphasis_bitmap = (hba->host_deemphasis_cap << 0x1) | 0x1;
340 num_lanes = pwr_info->lane_tx;
341 fom_array = rec->host_fom;
342 snprintf(name, sizeof(name), "%s", "Host");
343 } else if (strcmp(file_name, "device_tx_eqtr_record") == 0) {
344 preshoot_bitmap = (hba->device_preshoot_cap << 0x1) | 0x1;
345 deemphasis_bitmap = (hba->device_deemphasis_cap << 0x1) | 0x1;
346 num_lanes = pwr_info->lane_rx;
347 fom_array = rec->device_fom;
348 snprintf(name, sizeof(name), "%s", "Device");
349 } else {
350 return -ENOENT;
351 }
352
353 seq_printf(s, "%s TX EQTR record summary -\n", name);
354 seq_printf(s, "Target Power Mode: HS-G%u, Rate-%s\n", gear,
355 ufs_hs_rate_to_str(rate));
356 seq_printf(s, "Most recent record index: %d\n",
357 rec->last_record_index);
358 seq_printf(s, "Most recent record timestamp: %llu us\n",
359 ktime_to_us(rec->last_record_ts));
360
361 for (lane = 0; lane < num_lanes; lane++) {
362 seq_printf(s, "\nTX Lane %d FOM - %s\n", lane, "PreShoot\\DeEmphasis");
363 seq_puts(s, "\\");
364 /* Print DeEmphasis header as X-axis. */
365 for (deemphasis = 0; deemphasis < TX_HS_NUM_DEEMPHASIS; deemphasis++)
366 seq_printf(s, "%8d%s", deemphasis, " ");
367 seq_puts(s, "\n");
368 /* Print matrix rows with PreShoot as Y-axis. */
369 for (preshoot = 0; preshoot < TX_HS_NUM_PRESHOOT; preshoot++) {
370 seq_printf(s, "%d", preshoot);
371 for (deemphasis = 0; deemphasis < TX_HS_NUM_DEEMPHASIS; deemphasis++) {
372 if (test_bit(preshoot, &preshoot_bitmap) &&
373 test_bit(deemphasis, &deemphasis_bitmap)) {
374 u8 fom = fom_array[lane][preshoot][deemphasis];
375 u8 fom_val = fom & RX_FOM_VALUE_MASK;
376 bool precode_en = fom & RX_FOM_PRECODING_EN_BIT;
377
378 if (ufshcd_is_txeq_presets_used(hba) &&
379 !ufshcd_is_txeq_preset_selected(preshoot, deemphasis))
380 seq_printf(s, "%8s%s", "-", " ");
381 else
382 seq_printf(s, "%8u%s", fom_val,
383 precode_en ? "*" : " ");
384 } else {
385 seq_printf(s, "%8s%s", "x", " ");
386 }
387 }
388 seq_puts(s, "\n");
389 }
390 }
391
392 return 0;
393 }
394
ufs_tx_eqtr_record_open(struct inode * inode,struct file * file)395 static int ufs_tx_eqtr_record_open(struct inode *inode, struct file *file)
396 {
397 return single_open(file, ufs_tx_eqtr_record_show, inode->i_private);
398 }
399
400 static const struct file_operations ufs_tx_eqtr_record_fops = {
401 .owner = THIS_MODULE,
402 .open = ufs_tx_eqtr_record_open,
403 .read = seq_read,
404 .llseek = seq_lseek,
405 .release = single_release,
406 };
407
ufs_tx_eq_ctrl_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)408 static ssize_t ufs_tx_eq_ctrl_write(struct file *file, const char __user *buf,
409 size_t count, loff_t *ppos)
410 {
411 u32 gear = (u32)(uintptr_t)file->f_inode->i_private;
412 struct ufs_hba *hba = hba_from_file(file);
413 char kbuf[32];
414 int ret;
415
416 if (count >= sizeof(kbuf))
417 return -EINVAL;
418
419 if (copy_from_user(kbuf, buf, count))
420 return -EFAULT;
421
422 if (!ufshcd_is_tx_eq_supported(hba))
423 return -EOPNOTSUPP;
424
425 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL ||
426 !hba->max_pwr_info.is_valid)
427 return -EBUSY;
428
429 if (!hba->ufs_device_wlun)
430 return -ENODEV;
431
432 kbuf[count] = '\0';
433
434 if (sysfs_streq(kbuf, "retrain")) {
435 ret = ufs_debugfs_get_user_access(hba);
436 if (ret)
437 return ret;
438 ret = ufshcd_retrain_tx_eq(hba, gear);
439 ufs_debugfs_put_user_access(hba);
440 } else {
441 /* Unknown operation */
442 return -EINVAL;
443 }
444
445 return ret ? ret : count;
446 }
447
ufs_tx_eq_ctrl_show(struct seq_file * s,void * data)448 static int ufs_tx_eq_ctrl_show(struct seq_file *s, void *data)
449 {
450 seq_puts(s, "write 'retrain' to retrain TX Equalization settings\n");
451 return 0;
452 }
453
ufs_tx_eq_ctrl_open(struct inode * inode,struct file * file)454 static int ufs_tx_eq_ctrl_open(struct inode *inode, struct file *file)
455 {
456 return single_open(file, ufs_tx_eq_ctrl_show, inode->i_private);
457 }
458
459 static const struct file_operations ufs_tx_eq_ctrl_fops = {
460 .owner = THIS_MODULE,
461 .open = ufs_tx_eq_ctrl_open,
462 .read = seq_read,
463 .llseek = seq_lseek,
464 .write = ufs_tx_eq_ctrl_write,
465 .release = single_release,
466 };
467
468 static const struct ufs_debugfs_attr ufs_tx_eqtr_attrs[] = {
469 { "host_tx_eqtr_record", 0400, &ufs_tx_eqtr_record_fops },
470 { "device_tx_eqtr_record", 0400, &ufs_tx_eqtr_record_fops },
471 { "tx_eq_ctrl", 0600, &ufs_tx_eq_ctrl_fops },
472 { }
473 };
474
ufs_debugfs_hba_init(struct ufs_hba * hba)475 void ufs_debugfs_hba_init(struct ufs_hba *hba)
476 {
477 const struct ufs_debugfs_attr *attr;
478 struct dentry *root;
479
480 /* Set default exception event rate limit period to 20ms */
481 hba->debugfs_ee_rate_limit_ms = 20;
482 INIT_DELAYED_WORK(&hba->debugfs_ee_work, ufs_debugfs_restart_ee);
483
484 root = debugfs_create_dir(dev_name(hba->dev), ufs_debugfs_root);
485 if (IS_ERR_OR_NULL(root))
486 return;
487 hba->debugfs_root = root;
488 d_inode(root)->i_private = hba;
489 for (attr = ufs_attrs; attr->name; attr++)
490 debugfs_create_file(attr->name, attr->mode, root, (void *)attr,
491 attr->fops);
492 debugfs_create_file("exception_event_mask", 0600, hba->debugfs_root,
493 hba, &ee_usr_mask_fops);
494 debugfs_create_u32("exception_event_rate_limit_ms", 0600, hba->debugfs_root,
495 &hba->debugfs_ee_rate_limit_ms);
496
497 if (!(hba->caps & UFSHCD_CAP_TX_EQUALIZATION))
498 return;
499
500 for (u32 gear = UFS_HS_G1; gear <= UFS_HS_GEAR_MAX; gear++) {
501 struct dentry *txeq_dir;
502 char name[32];
503
504 snprintf(name, sizeof(name), "tx_eq_hs_gear%d", gear);
505 txeq_dir = debugfs_create_dir(name, hba->debugfs_root);
506 if (IS_ERR_OR_NULL(txeq_dir))
507 return;
508
509 d_inode(txeq_dir)->i_private = hba;
510
511 /* Create files for TX Equalization parameters */
512 for (attr = ufs_tx_eq_attrs; attr->name; attr++)
513 debugfs_create_file(attr->name, attr->mode, txeq_dir,
514 (void *)(uintptr_t)gear,
515 attr->fops);
516
517 /* TX EQTR is supported for HS-G4 and higher Gears */
518 if (gear < UFS_HS_G4)
519 continue;
520
521 /* Create files for TX EQTR related attributes */
522 for (attr = ufs_tx_eqtr_attrs; attr->name; attr++)
523 debugfs_create_file(attr->name, attr->mode, txeq_dir,
524 (void *)(uintptr_t)gear,
525 attr->fops);
526 }
527 }
528
ufs_debugfs_hba_exit(struct ufs_hba * hba)529 void ufs_debugfs_hba_exit(struct ufs_hba *hba)
530 {
531 debugfs_remove_recursive(hba->debugfs_root);
532 cancel_delayed_work_sync(&hba->debugfs_ee_work);
533 }
534