1 // SPDX-License-Identifier: GPL-2.0-or-later
2
3 #include <linux/kconfig.h>
4 #include <linux/types.h>
5 #include <linux/fault-inject.h>
6 #include <linux/debugfs.h>
7 #include <linux/module.h>
8 #include <ufs/ufshcd.h>
9 #include "ufs-fault-injection.h"
10
11 static int ufs_fault_get(char *buffer, const struct kernel_param *kp);
12 static int ufs_fault_set(const char *val, const struct kernel_param *kp);
13
14 static const struct kernel_param_ops ufs_fault_ops = {
15 .get = ufs_fault_get,
16 .set = ufs_fault_set,
17 };
18
19 enum { FAULT_INJ_STR_SIZE = 80 };
20
21 /*
22 * For more details about fault injection, please refer to
23 * Documentation/fault-injection/fault-injection.rst.
24 */
25 static char g_trigger_eh_str[FAULT_INJ_STR_SIZE];
26 module_param_cb(trigger_eh, &ufs_fault_ops, g_trigger_eh_str, 0644);
27 MODULE_PARM_DESC(trigger_eh,
28 "Fault injection. trigger_eh=<interval>,<probability>,<space>,<times>");
29 static DECLARE_FAULT_ATTR(ufs_trigger_eh_attr);
30
31 static char g_timeout_str[FAULT_INJ_STR_SIZE];
32 module_param_cb(timeout, &ufs_fault_ops, g_timeout_str, 0644);
33 MODULE_PARM_DESC(timeout,
34 "Fault injection. timeout=<interval>,<probability>,<space>,<times>");
35 static DECLARE_FAULT_ATTR(ufs_timeout_attr);
36
ufs_fault_get(char * buffer,const struct kernel_param * kp)37 static int ufs_fault_get(char *buffer, const struct kernel_param *kp)
38 {
39 const char *fault_str = kp->arg;
40
41 return sysfs_emit(buffer, "%s\n", fault_str);
42 }
43
ufs_fault_set(const char * val,const struct kernel_param * kp)44 static int ufs_fault_set(const char *val, const struct kernel_param *kp)
45 {
46 struct fault_attr *attr = NULL;
47
48 if (kp->arg == g_trigger_eh_str)
49 attr = &ufs_trigger_eh_attr;
50 else if (kp->arg == g_timeout_str)
51 attr = &ufs_timeout_attr;
52
53 if (WARN_ON_ONCE(!attr))
54 return -EINVAL;
55
56 if (!setup_fault_attr(attr, (char *)val))
57 return -EINVAL;
58
59 strscpy(kp->arg, val, FAULT_INJ_STR_SIZE);
60
61 return 0;
62 }
63
ufs_fault_inject_hba_init(struct ufs_hba * hba)64 void ufs_fault_inject_hba_init(struct ufs_hba *hba)
65 {
66 hba->trigger_eh_attr = ufs_trigger_eh_attr;
67 hba->timeout_attr = ufs_timeout_attr;
68 #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
69 fault_create_debugfs_attr("trigger_eh_inject", hba->debugfs_root, &hba->trigger_eh_attr);
70 fault_create_debugfs_attr("timeout_inject", hba->debugfs_root, &hba->timeout_attr);
71 #endif
72 }
73
ufs_trigger_eh(struct ufs_hba * hba)74 bool ufs_trigger_eh(struct ufs_hba *hba)
75 {
76 return should_fail(&hba->trigger_eh_attr, 1);
77 }
78
ufs_fail_completion(struct ufs_hba * hba)79 bool ufs_fail_completion(struct ufs_hba *hba)
80 {
81 return should_fail(&hba->timeout_attr, 1);
82 }
83