1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <linux/device.h>
28#include <linux/init.h>
29#include <linux/mm.h>
30#include <linux/cpu.h>
31#include <linux/module.h>
32
33#define ERR_INJ_DEBUG
34
35#define ERR_DATA_BUFFER_SIZE 3
36
37#define define_one_ro(name) \
38static DEVICE_ATTR(name, 0444, show_##name, NULL)
39
40#define define_one_rw(name) \
41static DEVICE_ATTR(name, 0644, show_##name, store_##name)
42
43static u64 call_start[NR_CPUS];
44static u64 phys_addr[NR_CPUS];
45static u64 err_type_info[NR_CPUS];
46static u64 err_struct_info[NR_CPUS];
47static struct {
48 u64 data1;
49 u64 data2;
50 u64 data3;
51} __attribute__((__aligned__(16))) err_data_buffer[NR_CPUS];
52static s64 status[NR_CPUS];
53static u64 capabilities[NR_CPUS];
54static u64 resources[NR_CPUS];
55
56#define show(name) \
57static ssize_t \
58show_##name(struct device *dev, struct device_attribute *attr, \
59 char *buf) \
60{ \
61 u32 cpu=dev->id; \
62 return sprintf(buf, "%lx\n", name[cpu]); \
63}
64
65#define store(name) \
66static ssize_t \
67store_##name(struct device *dev, struct device_attribute *attr, \
68 const char *buf, size_t size) \
69{ \
70 unsigned int cpu=dev->id; \
71 name[cpu] = simple_strtoull(buf, NULL, 16); \
72 return size; \
73}
74
75show(call_start)
76
77
78
79
80static ssize_t
81store_call_start(struct device *dev, struct device_attribute *attr,
82 const char *buf, size_t size)
83{
84 unsigned int cpu=dev->id;
85 unsigned long call_start = simple_strtoull(buf, NULL, 16);
86
87#ifdef ERR_INJ_DEBUG
88 printk(KERN_DEBUG "pal_mc_err_inject for cpu%d:\n", cpu);
89 printk(KERN_DEBUG "err_type_info=%lx,\n", err_type_info[cpu]);
90 printk(KERN_DEBUG "err_struct_info=%lx,\n", err_struct_info[cpu]);
91 printk(KERN_DEBUG "err_data_buffer=%lx, %lx, %lx.\n",
92 err_data_buffer[cpu].data1,
93 err_data_buffer[cpu].data2,
94 err_data_buffer[cpu].data3);
95#endif
96 switch (call_start) {
97 case 0:
98 break;
99 case 1:
100 status[cpu]=ia64_pal_mc_error_inject_phys(err_type_info[cpu],
101 err_struct_info[cpu],
102 ia64_tpa(&err_data_buffer[cpu]),
103 &capabilities[cpu],
104 &resources[cpu]);
105 break;
106 case 2:
107 status[cpu]=ia64_pal_mc_error_inject_virt(err_type_info[cpu],
108 err_struct_info[cpu],
109 ia64_tpa(&err_data_buffer[cpu]),
110 &capabilities[cpu],
111 &resources[cpu]);
112 break;
113 default:
114 status[cpu] = -EINVAL;
115 break;
116 }
117
118#ifdef ERR_INJ_DEBUG
119 printk(KERN_DEBUG "Returns: status=%d,\n", (int)status[cpu]);
120 printk(KERN_DEBUG "capapbilities=%lx,\n", capabilities[cpu]);
121 printk(KERN_DEBUG "resources=%lx\n", resources[cpu]);
122#endif
123 return size;
124}
125
126show(err_type_info)
127store(err_type_info)
128
129static ssize_t
130show_virtual_to_phys(struct device *dev, struct device_attribute *attr,
131 char *buf)
132{
133 unsigned int cpu=dev->id;
134 return sprintf(buf, "%lx\n", phys_addr[cpu]);
135}
136
137static ssize_t
138store_virtual_to_phys(struct device *dev, struct device_attribute *attr,
139 const char *buf, size_t size)
140{
141 unsigned int cpu=dev->id;
142 u64 virt_addr=simple_strtoull(buf, NULL, 16);
143 int ret;
144
145 ret = get_user_pages(current, current->mm, virt_addr,
146 1, VM_READ, 0, NULL, NULL);
147 if (ret<=0) {
148#ifdef ERR_INJ_DEBUG
149 printk("Virtual address %lx is not existing.\n",virt_addr);
150#endif
151 return -EINVAL;
152 }
153
154 phys_addr[cpu] = ia64_tpa(virt_addr);
155 return size;
156}
157
158show(err_struct_info)
159store(err_struct_info)
160
161static ssize_t
162show_err_data_buffer(struct device *dev,
163 struct device_attribute *attr, char *buf)
164{
165 unsigned int cpu=dev->id;
166
167 return sprintf(buf, "%lx, %lx, %lx\n",
168 err_data_buffer[cpu].data1,
169 err_data_buffer[cpu].data2,
170 err_data_buffer[cpu].data3);
171}
172
173static ssize_t
174store_err_data_buffer(struct device *dev,
175 struct device_attribute *attr,
176 const char *buf, size_t size)
177{
178 unsigned int cpu=dev->id;
179 int ret;
180
181#ifdef ERR_INJ_DEBUG
182 printk("write err_data_buffer=[%lx,%lx,%lx] on cpu%d\n",
183 err_data_buffer[cpu].data1,
184 err_data_buffer[cpu].data2,
185 err_data_buffer[cpu].data3,
186 cpu);
187#endif
188 ret=sscanf(buf, "%lx, %lx, %lx",
189 &err_data_buffer[cpu].data1,
190 &err_data_buffer[cpu].data2,
191 &err_data_buffer[cpu].data3);
192 if (ret!=ERR_DATA_BUFFER_SIZE)
193 return -EINVAL;
194
195 return size;
196}
197
198show(status)
199show(capabilities)
200show(resources)
201
202define_one_rw(call_start);
203define_one_rw(err_type_info);
204define_one_rw(err_struct_info);
205define_one_rw(err_data_buffer);
206define_one_rw(virtual_to_phys);
207define_one_ro(status);
208define_one_ro(capabilities);
209define_one_ro(resources);
210
211static struct attribute *default_attrs[] = {
212 &dev_attr_call_start.attr,
213 &dev_attr_virtual_to_phys.attr,
214 &dev_attr_err_type_info.attr,
215 &dev_attr_err_struct_info.attr,
216 &dev_attr_err_data_buffer.attr,
217 &dev_attr_status.attr,
218 &dev_attr_capabilities.attr,
219 &dev_attr_resources.attr,
220 NULL
221};
222
223static struct attribute_group err_inject_attr_group = {
224 .attrs = default_attrs,
225 .name = "err_inject"
226};
227
228static int err_inject_add_dev(struct device *sys_dev)
229{
230 return sysfs_create_group(&sys_dev->kobj, &err_inject_attr_group);
231}
232
233static int err_inject_remove_dev(struct device *sys_dev)
234{
235 sysfs_remove_group(&sys_dev->kobj, &err_inject_attr_group);
236 return 0;
237}
238static int err_inject_cpu_callback(struct notifier_block *nfb,
239 unsigned long action, void *hcpu)
240{
241 unsigned int cpu = (unsigned long)hcpu;
242 struct device *sys_dev;
243
244 sys_dev = get_cpu_device(cpu);
245 switch (action) {
246 case CPU_ONLINE:
247 case CPU_ONLINE_FROZEN:
248 err_inject_add_dev(sys_dev);
249 break;
250 case CPU_DEAD:
251 case CPU_DEAD_FROZEN:
252 err_inject_remove_dev(sys_dev);
253 break;
254 }
255
256 return NOTIFY_OK;
257}
258
259static struct notifier_block err_inject_cpu_notifier =
260{
261 .notifier_call = err_inject_cpu_callback,
262};
263
264static int __init
265err_inject_init(void)
266{
267 int i;
268
269#ifdef ERR_INJ_DEBUG
270 printk(KERN_INFO "Enter error injection driver.\n");
271#endif
272 for_each_online_cpu(i) {
273 err_inject_cpu_callback(&err_inject_cpu_notifier, CPU_ONLINE,
274 (void *)(long)i);
275 }
276
277 register_hotcpu_notifier(&err_inject_cpu_notifier);
278
279 return 0;
280}
281
282static void __exit
283err_inject_exit(void)
284{
285 int i;
286 struct device *sys_dev;
287
288#ifdef ERR_INJ_DEBUG
289 printk(KERN_INFO "Exit error injection driver.\n");
290#endif
291 for_each_online_cpu(i) {
292 sys_dev = get_cpu_device(i);
293 sysfs_remove_group(&sys_dev->kobj, &err_inject_attr_group);
294 }
295 unregister_hotcpu_notifier(&err_inject_cpu_notifier);
296}
297
298module_init(err_inject_init);
299module_exit(err_inject_exit);
300
301MODULE_AUTHOR("Fenghua Yu <fenghua.yu@intel.com>");
302MODULE_DESCRIPTION("MC error injection kernel sysfs interface");
303MODULE_LICENSE("GPL");
304