1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2020 Intel Corporation
4 #include <linux/debugfs.h>
6 #include "ufs-debugfs.h"
9 static struct dentry *ufs_debugfs_root;
11 void __init ufs_debugfs_init(void)
13 ufs_debugfs_root = debugfs_create_dir("ufshcd", NULL);
16 void ufs_debugfs_exit(void)
18 debugfs_remove_recursive(ufs_debugfs_root);
21 static int ufs_debugfs_stats_show(struct seq_file *s, void *data)
23 struct ufs_hba *hba = s->private;
24 struct ufs_event_hist *e = hba->ufs_stats.event;
26 #define PRT(fmt, typ) \
27 seq_printf(s, fmt, e[UFS_EVT_ ## typ].cnt)
29 PRT("PHY Adapter Layer errors (except LINERESET): %llu\n", PA_ERR);
30 PRT("Data Link Layer errors: %llu\n", DL_ERR);
31 PRT("Network Layer errors: %llu\n", NL_ERR);
32 PRT("Transport Layer errors: %llu\n", TL_ERR);
33 PRT("Generic DME errors: %llu\n", DME_ERR);
34 PRT("Auto-hibernate errors: %llu\n", AUTO_HIBERN8_ERR);
35 PRT("IS Fatal errors (CEFES, SBFES, HCFES, DFES): %llu\n", FATAL_ERR);
36 PRT("DME Link Startup errors: %llu\n", LINK_STARTUP_FAIL);
37 PRT("PM Resume errors: %llu\n", RESUME_ERR);
38 PRT("PM Suspend errors : %llu\n", SUSPEND_ERR);
39 PRT("Logical Unit Resets: %llu\n", DEV_RESET);
40 PRT("Host Resets: %llu\n", HOST_RESET);
41 PRT("SCSI command aborts: %llu\n", ABORT);
45 DEFINE_SHOW_ATTRIBUTE(ufs_debugfs_stats);
47 static int ee_usr_mask_get(void *data, u64 *val)
49 struct ufs_hba *hba = data;
51 *val = hba->ee_usr_mask;
55 static int ufs_debugfs_get_user_access(struct ufs_hba *hba)
56 __acquires(&hba->host_sem)
59 if (!ufshcd_is_user_access_allowed(hba)) {
63 ufshcd_rpm_get_sync(hba);
67 static void ufs_debugfs_put_user_access(struct ufs_hba *hba)
68 __releases(&hba->host_sem)
70 ufshcd_rpm_put_sync(hba);
74 static int ee_usr_mask_set(void *data, u64 val)
76 struct ufs_hba *hba = data;
79 if (val & ~(u64)MASK_EE_STATUS)
81 err = ufs_debugfs_get_user_access(hba);
84 err = ufshcd_update_ee_usr_mask(hba, val, MASK_EE_STATUS);
85 ufs_debugfs_put_user_access(hba);
89 DEFINE_DEBUGFS_ATTRIBUTE(ee_usr_mask_fops, ee_usr_mask_get, ee_usr_mask_set, "%#llx\n");
91 void ufs_debugfs_exception_event(struct ufs_hba *hba, u16 status)
97 if (!hba->debugfs_ee_rate_limit_ms || !status)
100 mutex_lock(&hba->ee_ctrl_mutex);
101 ee_ctrl_mask = hba->ee_drv_mask | (hba->ee_usr_mask & ~status);
102 chgd = ee_ctrl_mask != hba->ee_ctrl_mask;
104 err = __ufshcd_write_ee_control(hba, ee_ctrl_mask);
106 dev_err(hba->dev, "%s: failed to write ee control %d\n",
109 mutex_unlock(&hba->ee_ctrl_mutex);
112 unsigned long delay = msecs_to_jiffies(hba->debugfs_ee_rate_limit_ms);
114 queue_delayed_work(system_freezable_wq, &hba->debugfs_ee_work, delay);
118 static void ufs_debugfs_restart_ee(struct work_struct *work)
120 struct ufs_hba *hba = container_of(work, struct ufs_hba, debugfs_ee_work.work);
122 if (!hba->ee_usr_mask || pm_runtime_suspended(hba->dev) ||
123 ufs_debugfs_get_user_access(hba))
125 ufshcd_write_ee_control(hba);
126 ufs_debugfs_put_user_access(hba);
129 void ufs_debugfs_hba_init(struct ufs_hba *hba)
131 /* Set default exception event rate limit period to 20ms */
132 hba->debugfs_ee_rate_limit_ms = 20;
133 INIT_DELAYED_WORK(&hba->debugfs_ee_work, ufs_debugfs_restart_ee);
134 hba->debugfs_root = debugfs_create_dir(dev_name(hba->dev), ufs_debugfs_root);
135 debugfs_create_file("stats", 0400, hba->debugfs_root, hba, &ufs_debugfs_stats_fops);
136 debugfs_create_file("exception_event_mask", 0600, hba->debugfs_root,
137 hba, &ee_usr_mask_fops);
138 debugfs_create_u32("exception_event_rate_limit_ms", 0600, hba->debugfs_root,
139 &hba->debugfs_ee_rate_limit_ms);
142 void ufs_debugfs_hba_exit(struct ufs_hba *hba)
144 debugfs_remove_recursive(hba->debugfs_root);
145 cancel_delayed_work_sync(&hba->debugfs_ee_work);