xref: /linux/drivers/nvme/host/fault_inject.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1bc50ad75SChristoph Hellwig // SPDX-License-Identifier: GPL-2.0
2b9e03857SThomas Tai /*
3b9e03857SThomas Tai  * fault injection support for nvme.
4b9e03857SThomas Tai  *
5b9e03857SThomas Tai  * Copyright (c) 2018, Oracle and/or its affiliates
6b9e03857SThomas Tai  */
7b9e03857SThomas Tai 
8b9e03857SThomas Tai #include <linux/moduleparam.h>
9*6ce2082fSJani Nikula #include <linux/debugfs.h>
10b9e03857SThomas Tai #include "nvme.h"
11b9e03857SThomas Tai 
12b9e03857SThomas Tai static DECLARE_FAULT_ATTR(fail_default_attr);
13b9e03857SThomas Tai /* optional fault injection attributes boot time option:
14b9e03857SThomas Tai  * nvme_core.fail_request=<interval>,<probability>,<space>,<times>
15b9e03857SThomas Tai  */
16b9e03857SThomas Tai static char *fail_request;
17b9e03857SThomas Tai module_param(fail_request, charp, 0000);
18b9e03857SThomas Tai 
nvme_fault_inject_init(struct nvme_fault_inject * fault_inj,const char * dev_name)19a3646451SAkinobu Mita void nvme_fault_inject_init(struct nvme_fault_inject *fault_inj,
20a3646451SAkinobu Mita 			    const char *dev_name)
21b9e03857SThomas Tai {
22b9e03857SThomas Tai 	struct dentry *dir, *parent;
23b9e03857SThomas Tai 	struct fault_attr *attr = &fault_inj->attr;
24b9e03857SThomas Tai 
25b9e03857SThomas Tai 	/* set default fault injection attribute */
26b9e03857SThomas Tai 	if (fail_request)
27b9e03857SThomas Tai 		setup_fault_attr(&fail_default_attr, fail_request);
28b9e03857SThomas Tai 
29b9e03857SThomas Tai 	/* create debugfs directory and attribute */
30a3646451SAkinobu Mita 	parent = debugfs_create_dir(dev_name, NULL);
31733ba346SMinjie Du 	if (IS_ERR(parent)) {
32a3646451SAkinobu Mita 		pr_warn("%s: failed to create debugfs directory\n", dev_name);
33b9e03857SThomas Tai 		return;
34b9e03857SThomas Tai 	}
35b9e03857SThomas Tai 
36b9e03857SThomas Tai 	*attr = fail_default_attr;
37b9e03857SThomas Tai 	dir = fault_create_debugfs_attr("fault_inject", parent, attr);
38b9e03857SThomas Tai 	if (IS_ERR(dir)) {
39a3646451SAkinobu Mita 		pr_warn("%s: failed to create debugfs attr\n", dev_name);
40b9e03857SThomas Tai 		debugfs_remove_recursive(parent);
41b9e03857SThomas Tai 		return;
42b9e03857SThomas Tai 	}
43a3646451SAkinobu Mita 	fault_inj->parent = parent;
44b9e03857SThomas Tai 
45b9e03857SThomas Tai 	/* create debugfs for status code and dont_retry */
46b9e03857SThomas Tai 	fault_inj->status = NVME_SC_INVALID_OPCODE;
47b9e03857SThomas Tai 	fault_inj->dont_retry = true;
48b9e03857SThomas Tai 	debugfs_create_x16("status", 0600, dir,	&fault_inj->status);
49b9e03857SThomas Tai 	debugfs_create_bool("dont_retry", 0600, dir, &fault_inj->dont_retry);
50b9e03857SThomas Tai }
51b9e03857SThomas Tai 
nvme_fault_inject_fini(struct nvme_fault_inject * fault_inject)52a3646451SAkinobu Mita void nvme_fault_inject_fini(struct nvme_fault_inject *fault_inject)
53b9e03857SThomas Tai {
54b9e03857SThomas Tai 	/* remove debugfs directories */
55a3646451SAkinobu Mita 	debugfs_remove_recursive(fault_inject->parent);
56b9e03857SThomas Tai }
57b9e03857SThomas Tai 
nvme_should_fail(struct request * req)58b9e03857SThomas Tai void nvme_should_fail(struct request *req)
59b9e03857SThomas Tai {
60f3fa33acSChristoph Hellwig 	struct gendisk *disk = req->q->disk;
61a3646451SAkinobu Mita 	struct nvme_fault_inject *fault_inject = NULL;
62b9e03857SThomas Tai 	u16 status;
63b9e03857SThomas Tai 
64a3646451SAkinobu Mita 	if (disk) {
65a3646451SAkinobu Mita 		struct nvme_ns *ns = disk->private_data;
66b9e03857SThomas Tai 
67a3646451SAkinobu Mita 		if (ns)
68a3646451SAkinobu Mita 			fault_inject = &ns->fault_inject;
69a3646451SAkinobu Mita 		else
70a3646451SAkinobu Mita 			WARN_ONCE(1, "No namespace found for request\n");
71f79d5fdaSAkinobu Mita 	} else {
72f79d5fdaSAkinobu Mita 		fault_inject = &nvme_req(req)->ctrl->fault_inject;
73a3646451SAkinobu Mita 	}
74a3646451SAkinobu Mita 
75a3646451SAkinobu Mita 	if (fault_inject && should_fail(&fault_inject->attr, 1)) {
76b9e03857SThomas Tai 		/* inject status code and DNR bit */
77a3646451SAkinobu Mita 		status = fault_inject->status;
78a3646451SAkinobu Mita 		if (fault_inject->dont_retry)
79dd0b0a4aSWeiwen Hu 			status |= NVME_STATUS_DNR;
80b9e03857SThomas Tai 		nvme_req(req)->status =	status;
81b9e03857SThomas Tai 	}
82b9e03857SThomas Tai }
83b9e03857SThomas Tai EXPORT_SYMBOL_GPL(nvme_should_fail);
84