xref: /OK3568_Linux_fs/kernel/drivers/hv/hv_debugfs.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Authors:
4*4882a593Smuzhiyun  *   Branden Bonaby <brandonbonaby94@gmail.com>
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/hyperv.h>
8*4882a593Smuzhiyun #include <linux/debugfs.h>
9*4882a593Smuzhiyun #include <linux/delay.h>
10*4882a593Smuzhiyun #include <linux/err.h>
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include "hyperv_vmbus.h"
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun static struct dentry *hv_debug_root;
15*4882a593Smuzhiyun 
hv_debugfs_delay_get(void * data,u64 * val)16*4882a593Smuzhiyun static int hv_debugfs_delay_get(void *data, u64 *val)
17*4882a593Smuzhiyun {
18*4882a593Smuzhiyun 	*val = *(u32 *)data;
19*4882a593Smuzhiyun 	return 0;
20*4882a593Smuzhiyun }
21*4882a593Smuzhiyun 
hv_debugfs_delay_set(void * data,u64 val)22*4882a593Smuzhiyun static int hv_debugfs_delay_set(void *data, u64 val)
23*4882a593Smuzhiyun {
24*4882a593Smuzhiyun 	if (val > 1000)
25*4882a593Smuzhiyun 		return -EINVAL;
26*4882a593Smuzhiyun 	*(u32 *)data = val;
27*4882a593Smuzhiyun 	return 0;
28*4882a593Smuzhiyun }
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun DEFINE_DEBUGFS_ATTRIBUTE(hv_debugfs_delay_fops, hv_debugfs_delay_get,
31*4882a593Smuzhiyun 			 hv_debugfs_delay_set, "%llu\n");
32*4882a593Smuzhiyun 
hv_debugfs_state_get(void * data,u64 * val)33*4882a593Smuzhiyun static int hv_debugfs_state_get(void *data, u64 *val)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun 	*val = *(bool *)data;
36*4882a593Smuzhiyun 	return 0;
37*4882a593Smuzhiyun }
38*4882a593Smuzhiyun 
hv_debugfs_state_set(void * data,u64 val)39*4882a593Smuzhiyun static int hv_debugfs_state_set(void *data, u64 val)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun 	if (val == 1)
42*4882a593Smuzhiyun 		*(bool *)data = true;
43*4882a593Smuzhiyun 	else if (val == 0)
44*4882a593Smuzhiyun 		*(bool *)data = false;
45*4882a593Smuzhiyun 	else
46*4882a593Smuzhiyun 		return -EINVAL;
47*4882a593Smuzhiyun 	return 0;
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun DEFINE_DEBUGFS_ATTRIBUTE(hv_debugfs_state_fops, hv_debugfs_state_get,
51*4882a593Smuzhiyun 			 hv_debugfs_state_set, "%llu\n");
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun /* Setup delay files to store test values */
hv_debug_delay_files(struct hv_device * dev,struct dentry * root)54*4882a593Smuzhiyun static int hv_debug_delay_files(struct hv_device *dev, struct dentry *root)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun 	struct vmbus_channel *channel = dev->channel;
57*4882a593Smuzhiyun 	char *buffer = "fuzz_test_buffer_interrupt_delay";
58*4882a593Smuzhiyun 	char *message = "fuzz_test_message_delay";
59*4882a593Smuzhiyun 	int *buffer_val = &channel->fuzz_testing_interrupt_delay;
60*4882a593Smuzhiyun 	int *message_val = &channel->fuzz_testing_message_delay;
61*4882a593Smuzhiyun 	struct dentry *buffer_file, *message_file;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	buffer_file = debugfs_create_file(buffer, 0644, root,
64*4882a593Smuzhiyun 					  buffer_val,
65*4882a593Smuzhiyun 					  &hv_debugfs_delay_fops);
66*4882a593Smuzhiyun 	if (IS_ERR(buffer_file)) {
67*4882a593Smuzhiyun 		pr_debug("debugfs_hyperv: file %s not created\n", buffer);
68*4882a593Smuzhiyun 		return PTR_ERR(buffer_file);
69*4882a593Smuzhiyun 	}
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	message_file = debugfs_create_file(message, 0644, root,
72*4882a593Smuzhiyun 					   message_val,
73*4882a593Smuzhiyun 					   &hv_debugfs_delay_fops);
74*4882a593Smuzhiyun 	if (IS_ERR(message_file)) {
75*4882a593Smuzhiyun 		pr_debug("debugfs_hyperv: file %s not created\n", message);
76*4882a593Smuzhiyun 		return PTR_ERR(message_file);
77*4882a593Smuzhiyun 	}
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	return 0;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun /* Setup test state value for vmbus device */
hv_debug_set_test_state(struct hv_device * dev,struct dentry * root)83*4882a593Smuzhiyun static int hv_debug_set_test_state(struct hv_device *dev, struct dentry *root)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun 	struct vmbus_channel *channel = dev->channel;
86*4882a593Smuzhiyun 	bool *state = &channel->fuzz_testing_state;
87*4882a593Smuzhiyun 	char *status = "fuzz_test_state";
88*4882a593Smuzhiyun 	struct dentry *test_state;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	test_state = debugfs_create_file(status, 0644, root,
91*4882a593Smuzhiyun 					 state,
92*4882a593Smuzhiyun 					 &hv_debugfs_state_fops);
93*4882a593Smuzhiyun 	if (IS_ERR(test_state)) {
94*4882a593Smuzhiyun 		pr_debug("debugfs_hyperv: file %s not created\n", status);
95*4882a593Smuzhiyun 		return PTR_ERR(test_state);
96*4882a593Smuzhiyun 	}
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	return 0;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun /* Bind hv device to a dentry for debugfs */
hv_debug_set_dir_dentry(struct hv_device * dev,struct dentry * root)102*4882a593Smuzhiyun static void hv_debug_set_dir_dentry(struct hv_device *dev, struct dentry *root)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun 	if (hv_debug_root)
105*4882a593Smuzhiyun 		dev->debug_dir = root;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun /* Create all test dentry's and names for fuzz testing */
hv_debug_add_dev_dir(struct hv_device * dev)109*4882a593Smuzhiyun int hv_debug_add_dev_dir(struct hv_device *dev)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun 	const char *device = dev_name(&dev->device);
112*4882a593Smuzhiyun 	char *delay_name = "delay";
113*4882a593Smuzhiyun 	struct dentry *delay, *dev_root;
114*4882a593Smuzhiyun 	int ret;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	if (!IS_ERR(hv_debug_root)) {
117*4882a593Smuzhiyun 		dev_root = debugfs_create_dir(device, hv_debug_root);
118*4882a593Smuzhiyun 		if (IS_ERR(dev_root)) {
119*4882a593Smuzhiyun 			pr_debug("debugfs_hyperv: hyperv/%s/ not created\n",
120*4882a593Smuzhiyun 				 device);
121*4882a593Smuzhiyun 			return PTR_ERR(dev_root);
122*4882a593Smuzhiyun 		}
123*4882a593Smuzhiyun 		hv_debug_set_test_state(dev, dev_root);
124*4882a593Smuzhiyun 		hv_debug_set_dir_dentry(dev, dev_root);
125*4882a593Smuzhiyun 		delay = debugfs_create_dir(delay_name, dev_root);
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 		if (IS_ERR(delay)) {
128*4882a593Smuzhiyun 			pr_debug("debugfs_hyperv: hyperv/%s/%s/ not created\n",
129*4882a593Smuzhiyun 				 device, delay_name);
130*4882a593Smuzhiyun 			return PTR_ERR(delay);
131*4882a593Smuzhiyun 		}
132*4882a593Smuzhiyun 		ret = hv_debug_delay_files(dev, delay);
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 		return ret;
135*4882a593Smuzhiyun 	}
136*4882a593Smuzhiyun 	pr_debug("debugfs_hyperv: hyperv/ not in root debugfs path\n");
137*4882a593Smuzhiyun 	return PTR_ERR(hv_debug_root);
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun /* Remove dentry associated with released hv device */
hv_debug_rm_dev_dir(struct hv_device * dev)141*4882a593Smuzhiyun void hv_debug_rm_dev_dir(struct hv_device *dev)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun 	if (!IS_ERR(hv_debug_root))
144*4882a593Smuzhiyun 		debugfs_remove_recursive(dev->debug_dir);
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun /* Remove all dentrys associated with vmbus testing */
hv_debug_rm_all_dir(void)148*4882a593Smuzhiyun void hv_debug_rm_all_dir(void)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun 	debugfs_remove_recursive(hv_debug_root);
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun /* Delay buffer/message reads on a vmbus channel */
hv_debug_delay_test(struct vmbus_channel * channel,enum delay delay_type)154*4882a593Smuzhiyun void hv_debug_delay_test(struct vmbus_channel *channel, enum delay delay_type)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun 	struct vmbus_channel *test_channel =    channel->primary_channel ?
157*4882a593Smuzhiyun 						channel->primary_channel :
158*4882a593Smuzhiyun 						channel;
159*4882a593Smuzhiyun 	bool state = test_channel->fuzz_testing_state;
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	if (state) {
162*4882a593Smuzhiyun 		if (delay_type == 0)
163*4882a593Smuzhiyun 			udelay(test_channel->fuzz_testing_interrupt_delay);
164*4882a593Smuzhiyun 		else
165*4882a593Smuzhiyun 			udelay(test_channel->fuzz_testing_message_delay);
166*4882a593Smuzhiyun 	}
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun /* Initialize top dentry for vmbus testing */
hv_debug_init(void)170*4882a593Smuzhiyun int hv_debug_init(void)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun 	hv_debug_root = debugfs_create_dir("hyperv", NULL);
173*4882a593Smuzhiyun 	if (IS_ERR(hv_debug_root)) {
174*4882a593Smuzhiyun 		pr_debug("debugfs_hyperv: hyperv/ not created\n");
175*4882a593Smuzhiyun 		return PTR_ERR(hv_debug_root);
176*4882a593Smuzhiyun 	}
177*4882a593Smuzhiyun 	return 0;
178*4882a593Smuzhiyun }
179