Merge tag 'iommu-updates-v4.6' of git://git.kernel.org/pub/scm/linux/kernel/git/joro...
[deliverable/linux.git] / drivers / s390 / char / zcore.c
CommitLineData
411ed322
MH
1/*
2 * zcore module to export memory content and register sets for creating system
3 * dumps on SCSI disks (zfcpdump). The "zcore/mem" debugfs file shows the same
4 * dump format as s390 standalone dumps.
5 *
6 * For more information please refer to Documentation/s390/zfcpdump.txt
7 *
a53c8fab 8 * Copyright IBM Corp. 2003, 2008
411ed322
MH
9 * Author(s): Michael Holzheu
10 */
11
17159dc6
MH
12#define KMSG_COMPONENT "zdump"
13#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14
411ed322 15#include <linux/init.h>
5a0e3ad6 16#include <linux/slab.h>
411ed322 17#include <linux/miscdevice.h>
411ed322 18#include <linux/debugfs.h>
3948a102 19#include <linux/module.h>
50be6345
PH
20#include <linux/memblock.h>
21
cbb870c8 22#include <asm/asm-offsets.h>
411ed322
MH
23#include <asm/ipl.h>
24#include <asm/sclp.h>
25#include <asm/setup.h>
411ed322
MH
26#include <asm/uaccess.h>
27#include <asm/debug.h>
28#include <asm/processor.h>
29#include <asm/irqflags.h>
159d1ff8 30#include <asm/checksum.h>
bbfed511 31#include <asm/os_info.h>
a62bc073 32#include <asm/switch_to.h>
763968e2 33#include "sclp.h"
411ed322
MH
34
35#define TRACE(x...) debug_sprintf_event(zcore_dbf, 1, x)
411ed322 36
12e0c95e 37#define CHUNK_INFO_SIZE 34 /* 2 16-byte char, each followed by blank */
411ed322
MH
38
39enum arch_id {
40 ARCH_S390 = 0,
41 ARCH_S390X = 1,
42};
43
099b7651
FM
44struct ipib_info {
45 unsigned long ipib;
46 u32 checksum;
47} __attribute__((packed));
48
411ed322
MH
49static struct debug_info *zcore_dbf;
50static int hsa_available;
51static struct dentry *zcore_dir;
12e0c95e 52static struct dentry *zcore_memmap_file;
099b7651 53static struct dentry *zcore_reipl_file;
b4b3d128 54static struct dentry *zcore_hsa_file;
099b7651 55static struct ipl_parameter_block *ipl_block;
411ed322 56
019d6bec
MS
57static char hsa_buf[PAGE_SIZE] __aligned(PAGE_SIZE);
58
411ed322 59/*
019d6bec 60 * Copy memory from HSA to user memory (not reentrant):
411ed322 61 *
019d6bec 62 * @dest: User buffer where memory should be copied to
411ed322
MH
63 * @src: Start address within HSA where data should be copied
64 * @count: Size of buffer, which should be copied
411ed322 65 */
019d6bec 66int memcpy_hsa_user(void __user *dest, unsigned long src, size_t count)
411ed322 67{
019d6bec 68 unsigned long offset, bytes;
411ed322 69
b4b3d128
MH
70 if (!hsa_available)
71 return -ENODATA;
411ed322 72
019d6bec
MS
73 while (count) {
74 if (sclp_sdias_copy(hsa_buf, src / PAGE_SIZE + 2, 1)) {
411ed322
MH
75 TRACE("sclp_sdias_copy() failed\n");
76 return -EIO;
77 }
019d6bec
MS
78 offset = src % PAGE_SIZE;
79 bytes = min(PAGE_SIZE - offset, count);
80 if (copy_to_user(dest, hsa_buf + offset, bytes))
411ed322 81 return -EFAULT;
019d6bec
MS
82 src += bytes;
83 dest += bytes;
84 count -= bytes;
85 }
411ed322
MH
86 return 0;
87}
88
df9694c7
MS
89/*
90 * Copy memory from HSA to kernel memory (not reentrant):
91 *
92 * @dest: Kernel or user buffer where memory should be copied to
93 * @src: Start address within HSA where data should be copied
94 * @count: Size of buffer, which should be copied
95 */
96int memcpy_hsa_kernel(void *dest, unsigned long src, size_t count)
411ed322 97{
019d6bec
MS
98 unsigned long offset, bytes;
99
100 if (!hsa_available)
101 return -ENODATA;
102
103 while (count) {
104 if (sclp_sdias_copy(hsa_buf, src / PAGE_SIZE + 2, 1)) {
105 TRACE("sclp_sdias_copy() failed\n");
106 return -EIO;
107 }
108 offset = src % PAGE_SIZE;
109 bytes = min(PAGE_SIZE - offset, count);
110 memcpy(dest, hsa_buf + offset, bytes);
111 src += bytes;
112 dest += bytes;
113 count -= bytes;
114 }
115 return 0;
411ed322
MH
116}
117
ffa52d02 118static int __init init_cpu_info(void)
411ed322 119{
1a2c5840 120 struct save_area *sa;
411ed322
MH
121
122 /* get info for boot cpu from lowcore, stored in the HSA */
1a2c5840
MS
123 sa = save_area_boot_cpu();
124 if (!sa)
411ed322 125 return -ENOMEM;
1a2c5840 126 if (memcpy_hsa_kernel(hsa_buf, __LC_FPREGS_SAVE_AREA, 512) < 0) {
2a062ab4 127 TRACE("could not copy from HSA\n");
411ed322
MH
128 return -EIO;
129 }
1a2c5840 130 save_area_add_regs(sa, hsa_buf); /* vx registers are saved in smp.c */
411ed322
MH
131 return 0;
132}
133
b4b3d128
MH
134/*
135 * Release the HSA
136 */
137static void release_hsa(void)
138{
139 diag308(DIAG308_REL_HSA, NULL);
140 hsa_available = 0;
141}
142
12e0c95e
FM
143static ssize_t zcore_memmap_read(struct file *filp, char __user *buf,
144 size_t count, loff_t *ppos)
145{
146 return simple_read_from_buffer(buf, count, ppos, filp->private_data,
50be6345 147 memblock.memory.cnt * CHUNK_INFO_SIZE);
12e0c95e
FM
148}
149
150static int zcore_memmap_open(struct inode *inode, struct file *filp)
151{
50be6345 152 struct memblock_region *reg;
12e0c95e 153 char *buf;
50be6345 154 int i = 0;
12e0c95e 155
50be6345 156 buf = kzalloc(memblock.memory.cnt * CHUNK_INFO_SIZE, GFP_KERNEL);
12e0c95e 157 if (!buf) {
12e0c95e
FM
158 return -ENOMEM;
159 }
50be6345
PH
160 for_each_memblock(memory, reg) {
161 sprintf(buf + (i++ * CHUNK_INFO_SIZE), "%016llx %016llx ",
162 (unsigned long long) reg->base,
163 (unsigned long long) reg->size);
12e0c95e 164 }
12e0c95e 165 filp->private_data = buf;
58ea91c0 166 return nonseekable_open(inode, filp);
12e0c95e
FM
167}
168
169static int zcore_memmap_release(struct inode *inode, struct file *filp)
170{
171 kfree(filp->private_data);
172 return 0;
173}
174
175static const struct file_operations zcore_memmap_fops = {
176 .owner = THIS_MODULE,
177 .read = zcore_memmap_read,
178 .open = zcore_memmap_open,
179 .release = zcore_memmap_release,
6038f373 180 .llseek = no_llseek,
12e0c95e
FM
181};
182
099b7651
FM
183static ssize_t zcore_reipl_write(struct file *filp, const char __user *buf,
184 size_t count, loff_t *ppos)
185{
186 if (ipl_block) {
187 diag308(DIAG308_SET, ipl_block);
188 diag308(DIAG308_IPL, NULL);
189 }
190 return count;
191}
192
193static int zcore_reipl_open(struct inode *inode, struct file *filp)
194{
58ea91c0 195 return nonseekable_open(inode, filp);
099b7651
FM
196}
197
198static int zcore_reipl_release(struct inode *inode, struct file *filp)
199{
200 return 0;
201}
202
203static const struct file_operations zcore_reipl_fops = {
204 .owner = THIS_MODULE,
205 .write = zcore_reipl_write,
206 .open = zcore_reipl_open,
207 .release = zcore_reipl_release,
6038f373 208 .llseek = no_llseek,
099b7651
FM
209};
210
b4b3d128
MH
211static ssize_t zcore_hsa_read(struct file *filp, char __user *buf,
212 size_t count, loff_t *ppos)
213{
214 static char str[18];
215
216 if (hsa_available)
37c5f6c8 217 snprintf(str, sizeof(str), "%lx\n", sclp.hsa_size);
b4b3d128
MH
218 else
219 snprintf(str, sizeof(str), "0\n");
220 return simple_read_from_buffer(buf, count, ppos, str, strlen(str));
221}
222
223static ssize_t zcore_hsa_write(struct file *filp, const char __user *buf,
224 size_t count, loff_t *ppos)
225{
226 char value;
227
228 if (*ppos != 0)
229 return -EPIPE;
230 if (copy_from_user(&value, buf, 1))
231 return -EFAULT;
232 if (value != '0')
233 return -EINVAL;
234 release_hsa();
235 return count;
236}
237
238static const struct file_operations zcore_hsa_fops = {
239 .owner = THIS_MODULE,
240 .write = zcore_hsa_write,
241 .read = zcore_hsa_read,
242 .open = nonseekable_open,
243 .llseek = no_llseek,
244};
245
411ed322
MH
246static int __init check_sdias(void)
247{
37c5f6c8 248 if (!sclp.hsa_size) {
2a062ab4 249 TRACE("Could not determine HSA size\n");
e657d8fe 250 return -ENODEV;
411ed322
MH
251 }
252 return 0;
253}
254
099b7651
FM
255/*
256 * Provide IPL parameter information block from either HSA or memory
257 * for future reipl
258 */
259static int __init zcore_reipl_init(void)
260{
261 struct ipib_info ipib_info;
262 int rc;
263
264 rc = memcpy_hsa_kernel(&ipib_info, __LC_DUMP_REIPL, sizeof(ipib_info));
265 if (rc)
266 return rc;
267 if (ipib_info.ipib == 0)
268 return 0;
269 ipl_block = (void *) __get_free_page(GFP_KERNEL);
270 if (!ipl_block)
271 return -ENOMEM;
37c5f6c8 272 if (ipib_info.ipib < sclp.hsa_size)
099b7651
FM
273 rc = memcpy_hsa_kernel(ipl_block, ipib_info.ipib, PAGE_SIZE);
274 else
92fe3132 275 rc = memcpy_real(ipl_block, (void *) ipib_info.ipib, PAGE_SIZE);
76ef964c 276 if (rc || csum_partial(ipl_block, ipl_block->hdr.len, 0) !=
159d1ff8 277 ipib_info.checksum) {
099b7651
FM
278 TRACE("Checksum does not match\n");
279 free_page((unsigned long) ipl_block);
280 ipl_block = NULL;
281 }
282 return 0;
283}
284
411ed322
MH
285static int __init zcore_init(void)
286{
287 unsigned char arch;
288 int rc;
289
290 if (ipl_info.type != IPL_TYPE_FCP_DUMP)
291 return -ENODATA;
3f25dc4f
MH
292 if (OLDMEM_BASE)
293 return -ENODATA;
411ed322
MH
294
295 zcore_dbf = debug_register("zcore", 4, 1, 4 * sizeof(long));
296 debug_register_view(zcore_dbf, &debug_sprintf_view);
297 debug_set_level(zcore_dbf, 6);
298
299 TRACE("devno: %x\n", ipl_info.data.fcp.dev_id.devno);
300 TRACE("wwpn: %llx\n", (unsigned long long) ipl_info.data.fcp.wwpn);
301 TRACE("lun: %llx\n", (unsigned long long) ipl_info.data.fcp.lun);
302
763968e2 303 rc = sclp_sdias_init();
411ed322
MH
304 if (rc)
305 goto fail;
306
307 rc = check_sdias();
2a062ab4 308 if (rc)
411ed322 309 goto fail;
b4b3d128 310 hsa_available = 1;
411ed322
MH
311
312 rc = memcpy_hsa_kernel(&arch, __LC_AR_MODE_ID, 1);
2a062ab4 313 if (rc)
411ed322 314 goto fail;
411ed322 315
f64ca217
HC
316 if (arch == ARCH_S390) {
317 pr_alert("The 64-bit dump tool cannot be used for a "
318 "32-bit system\n");
319 rc = -EINVAL;
320 goto fail;
321 }
411ed322 322
ffa52d02
MS
323 pr_alert("DETECTED 'S390X (64 bit) OS'\n");
324 rc = init_cpu_info();
2a062ab4 325 if (rc)
411ed322 326 goto fail;
411ed322 327
099b7651
FM
328 rc = zcore_reipl_init();
329 if (rc)
330 goto fail;
331
411ed322
MH
332 zcore_dir = debugfs_create_dir("zcore" , NULL);
333 if (!zcore_dir) {
334 rc = -ENOMEM;
335 goto fail;
336 }
12e0c95e
FM
337 zcore_memmap_file = debugfs_create_file("memmap", S_IRUSR, zcore_dir,
338 NULL, &zcore_memmap_fops);
339 if (!zcore_memmap_file) {
340 rc = -ENOMEM;
ffa52d02 341 goto fail_dir;
411ed322 342 }
099b7651
FM
343 zcore_reipl_file = debugfs_create_file("reipl", S_IRUSR, zcore_dir,
344 NULL, &zcore_reipl_fops);
345 if (!zcore_reipl_file) {
346 rc = -ENOMEM;
347 goto fail_memmap_file;
348 }
b4b3d128
MH
349 zcore_hsa_file = debugfs_create_file("hsa", S_IRUSR|S_IWUSR, zcore_dir,
350 NULL, &zcore_hsa_fops);
351 if (!zcore_hsa_file) {
352 rc = -ENOMEM;
353 goto fail_reipl_file;
354 }
411ed322
MH
355 return 0;
356
b4b3d128
MH
357fail_reipl_file:
358 debugfs_remove(zcore_reipl_file);
099b7651
FM
359fail_memmap_file:
360 debugfs_remove(zcore_memmap_file);
12e0c95e
FM
361fail_dir:
362 debugfs_remove(zcore_dir);
411ed322
MH
363fail:
364 diag308(DIAG308_REL_HSA, NULL);
365 return rc;
366}
367
411ed322
MH
368static void __exit zcore_exit(void)
369{
370 debug_unregister(zcore_dbf);
763968e2 371 sclp_sdias_exit();
099b7651 372 free_page((unsigned long) ipl_block);
b4b3d128 373 debugfs_remove(zcore_hsa_file);
099b7651
FM
374 debugfs_remove(zcore_reipl_file);
375 debugfs_remove(zcore_memmap_file);
099b7651 376 debugfs_remove(zcore_dir);
411ed322
MH
377 diag308(DIAG308_REL_HSA, NULL);
378}
379
099b7651 380MODULE_AUTHOR("Copyright IBM Corp. 2003,2008");
411ed322
MH
381MODULE_DESCRIPTION("zcore module for zfcpdump support");
382MODULE_LICENSE("GPL");
383
384subsys_initcall(zcore_init);
385module_exit(zcore_exit);
This page took 0.713364 seconds and 5 git commands to generate.