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