ufs-debugfs.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2020 Intel Corporation
  3. #include <linux/debugfs.h>
  4. #include "ufs-debugfs.h"
  5. #include <ufs/ufshcd.h>
  6. #include "ufshcd-priv.h"
  7. static struct dentry *ufs_debugfs_root;
  8. struct ufs_debugfs_attr {
  9. const char *name;
  10. mode_t mode;
  11. const struct file_operations *fops;
  12. };
  13. /* @file corresponds to a debugfs attribute in directory hba->debugfs_root. */
  14. static inline struct ufs_hba *hba_from_file(const struct file *file)
  15. {
  16. return d_inode(file->f_path.dentry->d_parent)->i_private;
  17. }
  18. void __init ufs_debugfs_init(void)
  19. {
  20. ufs_debugfs_root = debugfs_create_dir("ufshcd", NULL);
  21. }
  22. void ufs_debugfs_exit(void)
  23. {
  24. debugfs_remove_recursive(ufs_debugfs_root);
  25. }
  26. static int ufs_debugfs_stats_show(struct seq_file *s, void *data)
  27. {
  28. struct ufs_hba *hba = hba_from_file(s->file);
  29. struct ufs_event_hist *e = hba->ufs_stats.event;
  30. #define PRT(fmt, typ) \
  31. seq_printf(s, fmt, e[UFS_EVT_ ## typ].cnt)
  32. PRT("PHY Adapter Layer errors (except LINERESET): %llu\n", PA_ERR);
  33. PRT("Data Link Layer errors: %llu\n", DL_ERR);
  34. PRT("Network Layer errors: %llu\n", NL_ERR);
  35. PRT("Transport Layer errors: %llu\n", TL_ERR);
  36. PRT("Generic DME errors: %llu\n", DME_ERR);
  37. PRT("Auto-hibernate errors: %llu\n", AUTO_HIBERN8_ERR);
  38. PRT("IS Fatal errors (CEFES, SBFES, HCFES, DFES): %llu\n", FATAL_ERR);
  39. PRT("DME Link Startup errors: %llu\n", LINK_STARTUP_FAIL);
  40. PRT("PM Resume errors: %llu\n", RESUME_ERR);
  41. PRT("PM Suspend errors : %llu\n", SUSPEND_ERR);
  42. PRT("Logical Unit Resets: %llu\n", DEV_RESET);
  43. PRT("Host Resets: %llu\n", HOST_RESET);
  44. PRT("SCSI command aborts: %llu\n", ABORT);
  45. #undef PRT
  46. return 0;
  47. }
  48. DEFINE_SHOW_ATTRIBUTE(ufs_debugfs_stats);
  49. static int ee_usr_mask_get(void *data, u64 *val)
  50. {
  51. struct ufs_hba *hba = data;
  52. *val = hba->ee_usr_mask;
  53. return 0;
  54. }
  55. static int ufs_debugfs_get_user_access(struct ufs_hba *hba)
  56. __acquires(&hba->host_sem)
  57. {
  58. down(&hba->host_sem);
  59. if (!ufshcd_is_user_access_allowed(hba)) {
  60. up(&hba->host_sem);
  61. return -EBUSY;
  62. }
  63. ufshcd_rpm_get_sync(hba);
  64. return 0;
  65. }
  66. static void ufs_debugfs_put_user_access(struct ufs_hba *hba)
  67. __releases(&hba->host_sem)
  68. {
  69. ufshcd_rpm_put_sync(hba);
  70. up(&hba->host_sem);
  71. }
  72. static int ee_usr_mask_set(void *data, u64 val)
  73. {
  74. struct ufs_hba *hba = data;
  75. int err;
  76. if (val & ~(u64)MASK_EE_STATUS)
  77. return -EINVAL;
  78. err = ufs_debugfs_get_user_access(hba);
  79. if (err)
  80. return err;
  81. err = ufshcd_update_ee_usr_mask(hba, val, MASK_EE_STATUS);
  82. ufs_debugfs_put_user_access(hba);
  83. return err;
  84. }
  85. DEFINE_DEBUGFS_ATTRIBUTE(ee_usr_mask_fops, ee_usr_mask_get, ee_usr_mask_set, "%#llx\n");
  86. void ufs_debugfs_exception_event(struct ufs_hba *hba, u16 status)
  87. {
  88. bool chgd = false;
  89. u16 ee_ctrl_mask;
  90. int err = 0;
  91. if (!hba->debugfs_ee_rate_limit_ms || !status)
  92. return;
  93. mutex_lock(&hba->ee_ctrl_mutex);
  94. ee_ctrl_mask = hba->ee_drv_mask | (hba->ee_usr_mask & ~status);
  95. chgd = ee_ctrl_mask != hba->ee_ctrl_mask;
  96. if (chgd) {
  97. err = __ufshcd_write_ee_control(hba, ee_ctrl_mask);
  98. if (err)
  99. dev_err(hba->dev, "%s: failed to write ee control %d\n",
  100. __func__, err);
  101. }
  102. mutex_unlock(&hba->ee_ctrl_mutex);
  103. if (chgd && !err) {
  104. unsigned long delay = msecs_to_jiffies(hba->debugfs_ee_rate_limit_ms);
  105. queue_delayed_work(system_freezable_wq, &hba->debugfs_ee_work, delay);
  106. }
  107. }
  108. static void ufs_debugfs_restart_ee(struct work_struct *work)
  109. {
  110. struct ufs_hba *hba = container_of(work, struct ufs_hba, debugfs_ee_work.work);
  111. if (!hba->ee_usr_mask || pm_runtime_suspended(hba->dev) ||
  112. ufs_debugfs_get_user_access(hba))
  113. return;
  114. ufshcd_write_ee_control(hba);
  115. ufs_debugfs_put_user_access(hba);
  116. }
  117. static int ufs_saved_err_show(struct seq_file *s, void *data)
  118. {
  119. struct ufs_debugfs_attr *attr = s->private;
  120. struct ufs_hba *hba = hba_from_file(s->file);
  121. const int *p;
  122. if (strcmp(attr->name, "saved_err") == 0) {
  123. p = &hba->saved_err;
  124. } else if (strcmp(attr->name, "saved_uic_err") == 0) {
  125. p = &hba->saved_uic_err;
  126. } else {
  127. return -ENOENT;
  128. }
  129. seq_printf(s, "%d\n", *p);
  130. return 0;
  131. }
  132. static ssize_t ufs_saved_err_write(struct file *file, const char __user *buf,
  133. size_t count, loff_t *ppos)
  134. {
  135. struct ufs_debugfs_attr *attr = file->f_inode->i_private;
  136. struct ufs_hba *hba = hba_from_file(file);
  137. char val_str[16] = { };
  138. int val, ret;
  139. if (count > sizeof(val_str))
  140. return -EINVAL;
  141. if (copy_from_user(val_str, buf, count))
  142. return -EFAULT;
  143. ret = kstrtoint(val_str, 0, &val);
  144. if (ret < 0)
  145. return ret;
  146. spin_lock_irq(hba->host->host_lock);
  147. if (strcmp(attr->name, "saved_err") == 0) {
  148. hba->saved_err = val;
  149. } else if (strcmp(attr->name, "saved_uic_err") == 0) {
  150. hba->saved_uic_err = val;
  151. } else {
  152. ret = -ENOENT;
  153. }
  154. if (ret == 0)
  155. ufshcd_schedule_eh_work(hba);
  156. spin_unlock_irq(hba->host->host_lock);
  157. return ret < 0 ? ret : count;
  158. }
  159. static int ufs_saved_err_open(struct inode *inode, struct file *file)
  160. {
  161. return single_open(file, ufs_saved_err_show, inode->i_private);
  162. }
  163. static const struct file_operations ufs_saved_err_fops = {
  164. .owner = THIS_MODULE,
  165. .open = ufs_saved_err_open,
  166. .read = seq_read,
  167. .write = ufs_saved_err_write,
  168. .llseek = seq_lseek,
  169. .release = single_release,
  170. };
  171. static const struct ufs_debugfs_attr ufs_attrs[] = {
  172. { "stats", 0400, &ufs_debugfs_stats_fops },
  173. { "saved_err", 0600, &ufs_saved_err_fops },
  174. { "saved_uic_err", 0600, &ufs_saved_err_fops },
  175. { }
  176. };
  177. void ufs_debugfs_hba_init(struct ufs_hba *hba)
  178. {
  179. const struct ufs_debugfs_attr *attr;
  180. struct dentry *root;
  181. /* Set default exception event rate limit period to 20ms */
  182. hba->debugfs_ee_rate_limit_ms = 20;
  183. INIT_DELAYED_WORK(&hba->debugfs_ee_work, ufs_debugfs_restart_ee);
  184. root = debugfs_create_dir(dev_name(hba->dev), ufs_debugfs_root);
  185. if (IS_ERR_OR_NULL(root))
  186. return;
  187. hba->debugfs_root = root;
  188. d_inode(root)->i_private = hba;
  189. for (attr = ufs_attrs; attr->name; attr++)
  190. debugfs_create_file(attr->name, attr->mode, root, (void *)attr,
  191. attr->fops);
  192. debugfs_create_file("exception_event_mask", 0600, hba->debugfs_root,
  193. hba, &ee_usr_mask_fops);
  194. debugfs_create_u32("exception_event_rate_limit_ms", 0600, hba->debugfs_root,
  195. &hba->debugfs_ee_rate_limit_ms);
  196. }
  197. void ufs_debugfs_hba_exit(struct ufs_hba *hba)
  198. {
  199. debugfs_remove_recursive(hba->debugfs_root);
  200. cancel_delayed_work_sync(&hba->debugfs_ee_work);
  201. }