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