blob: c839a1593b8fe6e33c1393c49e25d1ab6e08958a [file] [log] [blame]
Michael Holzheu411ed322007-04-27 16:01:49 +02001/*
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 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02008 * Copyright IBM Corp. 2003, 2008
Michael Holzheu411ed322007-04-27 16:01:49 +02009 * Author(s): Michael Holzheu
10 */
11
Michael Holzheu17159dc62008-12-25 13:39:51 +010012#define KMSG_COMPONENT "zdump"
13#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14
Michael Holzheu411ed322007-04-27 16:01:49 +020015#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Michael Holzheu411ed322007-04-27 16:01:49 +020017#include <linux/miscdevice.h>
Michael Holzheu411ed322007-04-27 16:01:49 +020018#include <linux/debugfs.h>
Heiko Carstens3948a102011-10-30 15:16:55 +010019#include <linux/module.h>
Philipp Hachtmann50be6342014-01-29 18:16:01 +010020#include <linux/memblock.h>
21
Heiko Carstenscbb870c2010-02-26 22:37:43 +010022#include <asm/asm-offsets.h>
Michael Holzheu411ed322007-04-27 16:01:49 +020023#include <asm/ipl.h>
24#include <asm/sclp.h>
25#include <asm/setup.h>
Michael Holzheu411ed322007-04-27 16:01:49 +020026#include <asm/uaccess.h>
27#include <asm/debug.h>
28#include <asm/processor.h>
29#include <asm/irqflags.h>
Frank Munzert159d1ff2009-03-26 15:24:45 +010030#include <asm/checksum.h>
Michael Holzheua62bc072014-10-06 17:57:43 +020031#include <asm/switch_to.h>
Heiko Carstens763968e2007-05-10 15:45:46 +020032#include "sclp.h"
Michael Holzheu411ed322007-04-27 16:01:49 +020033
34#define TRACE(x...) debug_sprintf_event(zcore_dbf, 1, x)
Michael Holzheu411ed322007-04-27 16:01:49 +020035
Michael Holzheu6f79d332013-09-11 14:24:54 -070036#define TO_USER 1
37#define TO_KERNEL 0
Frank Munzert12e0c952008-07-17 17:16:40 +020038#define CHUNK_INFO_SIZE 34 /* 2 16-byte char, each followed by blank */
Michael Holzheu411ed322007-04-27 16:01:49 +020039
40enum arch_id {
41 ARCH_S390 = 0,
42 ARCH_S390X = 1,
43};
44
45/* dump system info */
46
47struct sys_info {
Heiko Carstensf64ca212010-02-26 22:37:32 +010048 enum arch_id arch;
49 unsigned long sa_base;
50 u32 sa_size;
51 int cpu_map[NR_CPUS];
52 unsigned long mem_size;
53 struct save_area lc_mask;
Michael Holzheu411ed322007-04-27 16:01:49 +020054};
55
Frank Munzert099b7652009-03-26 15:23:43 +010056struct ipib_info {
57 unsigned long ipib;
58 u32 checksum;
59} __attribute__((packed));
60
Michael Holzheu411ed322007-04-27 16:01:49 +020061static struct sys_info sys_info;
62static struct debug_info *zcore_dbf;
63static int hsa_available;
64static struct dentry *zcore_dir;
65static struct dentry *zcore_file;
Frank Munzert12e0c952008-07-17 17:16:40 +020066static struct dentry *zcore_memmap_file;
Frank Munzert099b7652009-03-26 15:23:43 +010067static struct dentry *zcore_reipl_file;
Michael Holzheub4b3d122013-01-21 18:37:41 +010068static struct dentry *zcore_hsa_file;
Frank Munzert099b7652009-03-26 15:23:43 +010069static struct ipl_parameter_block *ipl_block;
Michael Holzheu411ed322007-04-27 16:01:49 +020070
71/*
72 * Copy memory from HSA to kernel or user memory (not reentrant):
73 *
74 * @dest: Kernel or user buffer where memory should be copied to
75 * @src: Start address within HSA where data should be copied
76 * @count: Size of buffer, which should be copied
77 * @mode: Either TO_KERNEL or TO_USER
78 */
Michael Holzheu6f79d332013-09-11 14:24:54 -070079int memcpy_hsa(void *dest, unsigned long src, size_t count, int mode)
Michael Holzheu411ed322007-04-27 16:01:49 +020080{
81 int offs, blk_num;
82 static char buf[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
83
Michael Holzheub4b3d122013-01-21 18:37:41 +010084 if (!hsa_available)
85 return -ENODATA;
Michael Holzheu411ed322007-04-27 16:01:49 +020086 if (count == 0)
87 return 0;
88
89 /* copy first block */
90 offs = 0;
91 if ((src % PAGE_SIZE) != 0) {
92 blk_num = src / PAGE_SIZE + 2;
93 if (sclp_sdias_copy(buf, blk_num, 1)) {
94 TRACE("sclp_sdias_copy() failed\n");
95 return -EIO;
96 }
97 offs = min((PAGE_SIZE - (src % PAGE_SIZE)), count);
98 if (mode == TO_USER) {
99 if (copy_to_user((__force __user void*) dest,
100 buf + (src % PAGE_SIZE), offs))
101 return -EFAULT;
102 } else
103 memcpy(dest, buf + (src % PAGE_SIZE), offs);
104 }
105 if (offs == count)
106 goto out;
107
108 /* copy middle */
109 for (; (offs + PAGE_SIZE) <= count; offs += PAGE_SIZE) {
110 blk_num = (src + offs) / PAGE_SIZE + 2;
111 if (sclp_sdias_copy(buf, blk_num, 1)) {
112 TRACE("sclp_sdias_copy() failed\n");
113 return -EIO;
114 }
115 if (mode == TO_USER) {
116 if (copy_to_user((__force __user void*) dest + offs,
117 buf, PAGE_SIZE))
118 return -EFAULT;
119 } else
120 memcpy(dest + offs, buf, PAGE_SIZE);
121 }
122 if (offs == count)
123 goto out;
124
125 /* copy last block */
126 blk_num = (src + offs) / PAGE_SIZE + 2;
127 if (sclp_sdias_copy(buf, blk_num, 1)) {
128 TRACE("sclp_sdias_copy() failed\n");
129 return -EIO;
130 }
131 if (mode == TO_USER) {
132 if (copy_to_user((__force __user void*) dest + offs, buf,
Michael Holzheu241fd9b2013-04-19 18:03:02 +0200133 count - offs))
Michael Holzheu411ed322007-04-27 16:01:49 +0200134 return -EFAULT;
135 } else
136 memcpy(dest + offs, buf, count - offs);
137out:
138 return 0;
139}
140
141static int memcpy_hsa_user(void __user *dest, unsigned long src, size_t count)
142{
143 return memcpy_hsa((void __force *) dest, src, count, TO_USER);
144}
145
146static int memcpy_hsa_kernel(void *dest, unsigned long src, size_t count)
147{
148 return memcpy_hsa(dest, src, count, TO_KERNEL);
149}
150
Michael Holzheu411ed322007-04-27 16:01:49 +0200151static int __init init_cpu_info(enum arch_id arch)
152{
Michael Holzheua62bc072014-10-06 17:57:43 +0200153 struct save_area_ext *sa_ext;
Michael Holzheu411ed322007-04-27 16:01:49 +0200154
155 /* get info for boot cpu from lowcore, stored in the HSA */
156
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200157 sa_ext = dump_save_areas.areas[0];
Michael Holzheua62bc072014-10-06 17:57:43 +0200158 if (!sa_ext)
Michael Holzheu411ed322007-04-27 16:01:49 +0200159 return -ENOMEM;
Michael Holzheua62bc072014-10-06 17:57:43 +0200160 if (memcpy_hsa_kernel(&sa_ext->sa, sys_info.sa_base,
161 sys_info.sa_size) < 0) {
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200162 TRACE("could not copy from HSA\n");
Michael Holzheu411ed322007-04-27 16:01:49 +0200163 return -EIO;
164 }
Michael Holzheua62bc072014-10-06 17:57:43 +0200165 if (MACHINE_HAS_VX)
166 save_vx_regs_safe(sa_ext->vx_regs);
Michael Holzheu411ed322007-04-27 16:01:49 +0200167 return 0;
168}
169
170static DEFINE_MUTEX(zcore_mutex);
171
Michael Holzheu0cbde8e2010-02-26 22:37:55 +0100172#define DUMP_VERSION 0x5
Michael Holzheu411ed322007-04-27 16:01:49 +0200173#define DUMP_MAGIC 0xa8190173618f23fdULL
174#define DUMP_ARCH_S390X 2
175#define DUMP_ARCH_S390 1
176#define HEADER_SIZE 4096
177
178/* dump header dumped according to s390 crash dump format */
179
180struct zcore_header {
181 u64 magic;
182 u32 version;
183 u32 header_size;
184 u32 dump_level;
185 u32 page_size;
186 u64 mem_size;
187 u64 mem_start;
188 u64 mem_end;
189 u32 num_pages;
190 u32 pad1;
191 u64 tod;
Heiko Carstense86a6ed2009-09-11 10:29:04 +0200192 struct cpuid cpu_id;
Michael Holzheu411ed322007-04-27 16:01:49 +0200193 u32 arch_id;
Michael Holzheuce444822007-06-19 13:10:02 +0200194 u32 volnr;
Michael Holzheu411ed322007-04-27 16:01:49 +0200195 u32 build_arch;
Michael Holzheuce444822007-06-19 13:10:02 +0200196 u64 rmem_size;
Michael Holzheu0cbde8e2010-02-26 22:37:55 +0100197 u8 mvdump;
198 u16 cpu_cnt;
199 u16 real_cpu_cnt;
200 u8 end_pad1[0x200-0x061];
201 u64 mvdump_sign;
202 u64 mvdump_zipl_time;
203 u8 end_pad2[0x800-0x210];
204 u32 lc_vec[512];
Michael Holzheu411ed322007-04-27 16:01:49 +0200205} __attribute__((packed,__aligned__(16)));
206
207static struct zcore_header zcore_header = {
208 .magic = DUMP_MAGIC,
209 .version = DUMP_VERSION,
210 .header_size = 4096,
211 .dump_level = 0,
212 .page_size = PAGE_SIZE,
213 .mem_start = 0,
Michael Holzheu411ed322007-04-27 16:01:49 +0200214 .build_arch = DUMP_ARCH_S390X,
Michael Holzheu411ed322007-04-27 16:01:49 +0200215};
216
217/*
218 * Copy lowcore info to buffer. Use map in order to copy only register parts.
219 *
220 * @buf: User buffer
221 * @sa: Pointer to save area
222 * @sa_off: Offset in save area to copy
223 * @len: Number of bytes to copy
224 */
225static int copy_lc(void __user *buf, void *sa, int sa_off, int len)
226{
227 int i;
228 char *lc_mask = (char*)&sys_info.lc_mask;
229
230 for (i = 0; i < len; i++) {
231 if (!lc_mask[i + sa_off])
232 continue;
233 if (copy_to_user(buf + i, sa + sa_off + i, 1))
234 return -EFAULT;
235 }
236 return 0;
237}
238
239/*
240 * Copy lowcores info to memory, if necessary
241 *
242 * @buf: User buffer
243 * @addr: Start address of buffer in dump memory
244 * @count: Size of buffer
245 */
246static int zcore_add_lc(char __user *buf, unsigned long start, size_t count)
247{
248 unsigned long end;
Michael Holzheu58952942013-10-11 10:29:23 +0200249 int i;
Michael Holzheu411ed322007-04-27 16:01:49 +0200250
251 if (count == 0)
252 return 0;
253
254 end = start + count;
Michael Holzheu58952942013-10-11 10:29:23 +0200255 for (i = 0; i < dump_save_areas.count; i++) {
Michael Holzheu411ed322007-04-27 16:01:49 +0200256 unsigned long cp_start, cp_end; /* copy range */
257 unsigned long sa_start, sa_end; /* save area range */
258 unsigned long prefix;
259 unsigned long sa_off, len, buf_off;
Michael Holzheua62bc072014-10-06 17:57:43 +0200260 struct save_area *save_area = &dump_save_areas.areas[i]->sa;
Michael Holzheu411ed322007-04-27 16:01:49 +0200261
Michael Holzheu58952942013-10-11 10:29:23 +0200262 prefix = save_area->pref_reg;
Michael Holzheu411ed322007-04-27 16:01:49 +0200263 sa_start = prefix + sys_info.sa_base;
264 sa_end = prefix + sys_info.sa_base + sys_info.sa_size;
265
266 if ((end < sa_start) || (start > sa_end))
Michael Holzheu58952942013-10-11 10:29:23 +0200267 continue;
Michael Holzheu411ed322007-04-27 16:01:49 +0200268 cp_start = max(start, sa_start);
269 cp_end = min(end, sa_end);
270
271 buf_off = cp_start - start;
272 sa_off = cp_start - sa_start;
273 len = cp_end - cp_start;
274
275 TRACE("copy_lc for: %lx\n", start);
Michael Holzheu58952942013-10-11 10:29:23 +0200276 if (copy_lc(buf + buf_off, save_area, sa_off, len))
Michael Holzheu411ed322007-04-27 16:01:49 +0200277 return -EFAULT;
Michael Holzheu411ed322007-04-27 16:01:49 +0200278 }
279 return 0;
280}
281
282/*
Michael Holzheub4b3d122013-01-21 18:37:41 +0100283 * Release the HSA
284 */
285static void release_hsa(void)
286{
287 diag308(DIAG308_REL_HSA, NULL);
288 hsa_available = 0;
289}
290
291/*
Michael Holzheu411ed322007-04-27 16:01:49 +0200292 * Read routine for zcore character device
293 * First 4K are dump header
294 * Next 32MB are HSA Memory
295 * Rest is read from absolute Memory
296 */
297static ssize_t zcore_read(struct file *file, char __user *buf, size_t count,
298 loff_t *ppos)
299{
300 unsigned long mem_start; /* Start address in memory */
301 size_t mem_offs; /* Offset in dump memory */
302 size_t hdr_count; /* Size of header part of output buffer */
303 size_t size;
304 int rc;
305
306 mutex_lock(&zcore_mutex);
307
308 if (*ppos > (sys_info.mem_size + HEADER_SIZE)) {
309 rc = -EINVAL;
310 goto fail;
311 }
312
313 count = min(count, (size_t) (sys_info.mem_size + HEADER_SIZE - *ppos));
314
315 /* Copy dump header */
316 if (*ppos < HEADER_SIZE) {
317 size = min(count, (size_t) (HEADER_SIZE - *ppos));
318 if (copy_to_user(buf, &zcore_header + *ppos, size)) {
319 rc = -EFAULT;
320 goto fail;
321 }
322 hdr_count = size;
323 mem_start = 0;
324 } else {
325 hdr_count = 0;
326 mem_start = *ppos - HEADER_SIZE;
327 }
328
329 mem_offs = 0;
330
331 /* Copy from HSA data */
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200332 if (*ppos < sclp.hsa_size + HEADER_SIZE) {
Michael Holzheue657d8f2013-11-13 10:38:27 +0100333 size = min((count - hdr_count),
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200334 (size_t) (sclp.hsa_size - mem_start));
Michael Holzheu411ed322007-04-27 16:01:49 +0200335 rc = memcpy_hsa_user(buf + hdr_count, mem_start, size);
336 if (rc)
337 goto fail;
338
339 mem_offs += size;
340 }
341
342 /* Copy from real mem */
343 size = count - mem_offs - hdr_count;
Michael Holzheu7f0bf652011-10-30 15:16:39 +0100344 rc = copy_to_user_real(buf + hdr_count + mem_offs,
345 (void *) mem_start + mem_offs, size);
Michael Holzheu411ed322007-04-27 16:01:49 +0200346 if (rc)
347 goto fail;
348
349 /*
350 * Since s390 dump analysis tools like lcrash or crash
351 * expect register sets in the prefix pages of the cpus,
352 * we copy them into the read buffer, if necessary.
353 * buf + hdr_count: Start of memory part of output buffer
354 * mem_start: Start memory address to copy from
355 * count - hdr_count: Size of memory area to copy
356 */
357 if (zcore_add_lc(buf + hdr_count, mem_start, count - hdr_count)) {
358 rc = -EFAULT;
359 goto fail;
360 }
361 *ppos += count;
362fail:
363 mutex_unlock(&zcore_mutex);
364 return (rc < 0) ? rc : count;
365}
366
367static int zcore_open(struct inode *inode, struct file *filp)
368{
369 if (!hsa_available)
370 return -ENODATA;
371 else
372 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
373}
374
375static int zcore_release(struct inode *inode, struct file *filep)
376{
Michael Holzheub4b3d122013-01-21 18:37:41 +0100377 if (hsa_available)
378 release_hsa();
Michael Holzheu411ed322007-04-27 16:01:49 +0200379 return 0;
380}
381
382static loff_t zcore_lseek(struct file *file, loff_t offset, int orig)
383{
384 loff_t rc;
385
386 mutex_lock(&zcore_mutex);
387 switch (orig) {
388 case 0:
389 file->f_pos = offset;
390 rc = file->f_pos;
391 break;
392 case 1:
393 file->f_pos += offset;
394 rc = file->f_pos;
395 break;
396 default:
397 rc = -EINVAL;
398 }
399 mutex_unlock(&zcore_mutex);
400 return rc;
401}
402
Jan Engelhardt5c81cdb2008-01-26 14:11:29 +0100403static const struct file_operations zcore_fops = {
Michael Holzheu411ed322007-04-27 16:01:49 +0200404 .owner = THIS_MODULE,
405 .llseek = zcore_lseek,
406 .read = zcore_read,
407 .open = zcore_open,
408 .release = zcore_release,
409};
410
Frank Munzert12e0c952008-07-17 17:16:40 +0200411static ssize_t zcore_memmap_read(struct file *filp, char __user *buf,
412 size_t count, loff_t *ppos)
413{
414 return simple_read_from_buffer(buf, count, ppos, filp->private_data,
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100415 memblock.memory.cnt * CHUNK_INFO_SIZE);
Frank Munzert12e0c952008-07-17 17:16:40 +0200416}
417
418static int zcore_memmap_open(struct inode *inode, struct file *filp)
419{
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100420 struct memblock_region *reg;
Frank Munzert12e0c952008-07-17 17:16:40 +0200421 char *buf;
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100422 int i = 0;
Frank Munzert12e0c952008-07-17 17:16:40 +0200423
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100424 buf = kzalloc(memblock.memory.cnt * CHUNK_INFO_SIZE, GFP_KERNEL);
Frank Munzert12e0c952008-07-17 17:16:40 +0200425 if (!buf) {
Frank Munzert12e0c952008-07-17 17:16:40 +0200426 return -ENOMEM;
427 }
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100428 for_each_memblock(memory, reg) {
429 sprintf(buf + (i++ * CHUNK_INFO_SIZE), "%016llx %016llx ",
430 (unsigned long long) reg->base,
431 (unsigned long long) reg->size);
Frank Munzert12e0c952008-07-17 17:16:40 +0200432 }
Frank Munzert12e0c952008-07-17 17:16:40 +0200433 filp->private_data = buf;
Martin Schwidefsky58ea91c2010-05-17 10:00:07 +0200434 return nonseekable_open(inode, filp);
Frank Munzert12e0c952008-07-17 17:16:40 +0200435}
436
437static int zcore_memmap_release(struct inode *inode, struct file *filp)
438{
439 kfree(filp->private_data);
440 return 0;
441}
442
443static const struct file_operations zcore_memmap_fops = {
444 .owner = THIS_MODULE,
445 .read = zcore_memmap_read,
446 .open = zcore_memmap_open,
447 .release = zcore_memmap_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200448 .llseek = no_llseek,
Frank Munzert12e0c952008-07-17 17:16:40 +0200449};
450
Frank Munzert099b7652009-03-26 15:23:43 +0100451static ssize_t zcore_reipl_write(struct file *filp, const char __user *buf,
452 size_t count, loff_t *ppos)
453{
454 if (ipl_block) {
455 diag308(DIAG308_SET, ipl_block);
456 diag308(DIAG308_IPL, NULL);
457 }
458 return count;
459}
460
461static int zcore_reipl_open(struct inode *inode, struct file *filp)
462{
Martin Schwidefsky58ea91c2010-05-17 10:00:07 +0200463 return nonseekable_open(inode, filp);
Frank Munzert099b7652009-03-26 15:23:43 +0100464}
465
466static int zcore_reipl_release(struct inode *inode, struct file *filp)
467{
468 return 0;
469}
470
471static const struct file_operations zcore_reipl_fops = {
472 .owner = THIS_MODULE,
473 .write = zcore_reipl_write,
474 .open = zcore_reipl_open,
475 .release = zcore_reipl_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200476 .llseek = no_llseek,
Frank Munzert099b7652009-03-26 15:23:43 +0100477};
478
Michael Holzheub4b3d122013-01-21 18:37:41 +0100479static ssize_t zcore_hsa_read(struct file *filp, char __user *buf,
480 size_t count, loff_t *ppos)
481{
482 static char str[18];
483
484 if (hsa_available)
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200485 snprintf(str, sizeof(str), "%lx\n", sclp.hsa_size);
Michael Holzheub4b3d122013-01-21 18:37:41 +0100486 else
487 snprintf(str, sizeof(str), "0\n");
488 return simple_read_from_buffer(buf, count, ppos, str, strlen(str));
489}
490
491static ssize_t zcore_hsa_write(struct file *filp, const char __user *buf,
492 size_t count, loff_t *ppos)
493{
494 char value;
495
496 if (*ppos != 0)
497 return -EPIPE;
498 if (copy_from_user(&value, buf, 1))
499 return -EFAULT;
500 if (value != '0')
501 return -EINVAL;
502 release_hsa();
503 return count;
504}
505
506static const struct file_operations zcore_hsa_fops = {
507 .owner = THIS_MODULE,
508 .write = zcore_hsa_write,
509 .read = zcore_hsa_read,
510 .open = nonseekable_open,
511 .llseek = no_llseek,
512};
513
Heiko Carstensf64ca212010-02-26 22:37:32 +0100514static void __init set_lc_mask(struct save_area *map)
Michael Holzheu411ed322007-04-27 16:01:49 +0200515{
Heiko Carstensf64ca212010-02-26 22:37:32 +0100516 memset(&map->fp_regs, 0xff, sizeof(map->fp_regs));
517 memset(&map->gp_regs, 0xff, sizeof(map->gp_regs));
518 memset(&map->psw, 0xff, sizeof(map->psw));
519 memset(&map->pref_reg, 0xff, sizeof(map->pref_reg));
520 memset(&map->fp_ctrl_reg, 0xff, sizeof(map->fp_ctrl_reg));
521 memset(&map->tod_reg, 0xff, sizeof(map->tod_reg));
522 memset(&map->timer, 0xff, sizeof(map->timer));
523 memset(&map->clk_cmp, 0xff, sizeof(map->clk_cmp));
524 memset(&map->acc_regs, 0xff, sizeof(map->acc_regs));
525 memset(&map->ctrl_regs, 0xff, sizeof(map->ctrl_regs));
Michael Holzheu411ed322007-04-27 16:01:49 +0200526}
527
528/*
529 * Initialize dump globals for a given architecture
530 */
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200531static int __init sys_info_init(enum arch_id arch, unsigned long mem_end)
Michael Holzheu411ed322007-04-27 16:01:49 +0200532{
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200533 int rc;
534
Michael Holzheu411ed322007-04-27 16:01:49 +0200535 switch (arch) {
536 case ARCH_S390X:
Michael Holzheu17159dc62008-12-25 13:39:51 +0100537 pr_alert("DETECTED 'S390X (64 bit) OS'\n");
Michael Holzheu411ed322007-04-27 16:01:49 +0200538 break;
539 case ARCH_S390:
Michael Holzheu17159dc62008-12-25 13:39:51 +0100540 pr_alert("DETECTED 'S390 (32 bit) OS'\n");
Michael Holzheu411ed322007-04-27 16:01:49 +0200541 break;
542 default:
Michael Holzheu17159dc62008-12-25 13:39:51 +0100543 pr_alert("0x%x is an unknown architecture.\n",arch);
Michael Holzheu411ed322007-04-27 16:01:49 +0200544 return -EINVAL;
545 }
Heiko Carstensf64ca212010-02-26 22:37:32 +0100546 sys_info.sa_base = SAVE_AREA_BASE;
547 sys_info.sa_size = sizeof(struct save_area);
Michael Holzheu411ed322007-04-27 16:01:49 +0200548 sys_info.arch = arch;
Heiko Carstensf64ca212010-02-26 22:37:32 +0100549 set_lc_mask(&sys_info.lc_mask);
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200550 rc = init_cpu_info(arch);
551 if (rc)
552 return rc;
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200553 sys_info.mem_size = mem_end;
Michael Holzheu411ed322007-04-27 16:01:49 +0200554
555 return 0;
556}
557
558static int __init check_sdias(void)
559{
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200560 if (!sclp.hsa_size) {
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200561 TRACE("Could not determine HSA size\n");
Michael Holzheue657d8f2013-11-13 10:38:27 +0100562 return -ENODEV;
Michael Holzheu411ed322007-04-27 16:01:49 +0200563 }
564 return 0;
565}
566
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200567static int __init get_mem_info(unsigned long *mem, unsigned long *end)
Michael Holzheu411ed322007-04-27 16:01:49 +0200568{
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100569 struct memblock_region *reg;
Frank Munzert12e0c952008-07-17 17:16:40 +0200570
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100571 for_each_memblock(memory, reg) {
572 *mem += reg->size;
573 *end = max_t(unsigned long, *end, reg->base + reg->size);
Frank Munzert12e0c952008-07-17 17:16:40 +0200574 }
Frank Munzert12e0c952008-07-17 17:16:40 +0200575 return 0;
576}
577
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200578static void __init zcore_header_init(int arch, struct zcore_header *hdr,
579 unsigned long mem_size)
Frank Munzert12e0c952008-07-17 17:16:40 +0200580{
Michael Holzheu0cbde8e2010-02-26 22:37:55 +0100581 u32 prefix;
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200582 int i;
Frank Munzert12e0c952008-07-17 17:16:40 +0200583
Michael Holzheu411ed322007-04-27 16:01:49 +0200584 if (arch == ARCH_S390X)
585 hdr->arch_id = DUMP_ARCH_S390X;
586 else
587 hdr->arch_id = DUMP_ARCH_S390;
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200588 hdr->mem_size = mem_size;
589 hdr->rmem_size = mem_size;
Michael Holzheu411ed322007-04-27 16:01:49 +0200590 hdr->mem_end = sys_info.mem_size;
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200591 hdr->num_pages = mem_size / PAGE_SIZE;
Heiko Carstens1aae0562013-01-30 09:49:40 +0100592 hdr->tod = get_tod_clock();
Michael Holzheu411ed322007-04-27 16:01:49 +0200593 get_cpu_id(&hdr->cpu_id);
Michael Holzheu58952942013-10-11 10:29:23 +0200594 for (i = 0; i < dump_save_areas.count; i++) {
Michael Holzheua62bc072014-10-06 17:57:43 +0200595 prefix = dump_save_areas.areas[i]->sa.pref_reg;
Michael Holzheu0cbde8e2010-02-26 22:37:55 +0100596 hdr->real_cpu_cnt++;
597 if (!prefix)
598 continue;
599 hdr->lc_vec[hdr->cpu_cnt] = prefix;
600 hdr->cpu_cnt++;
601 }
Michael Holzheu411ed322007-04-27 16:01:49 +0200602}
603
Frank Munzert099b7652009-03-26 15:23:43 +0100604/*
605 * Provide IPL parameter information block from either HSA or memory
606 * for future reipl
607 */
608static int __init zcore_reipl_init(void)
609{
610 struct ipib_info ipib_info;
611 int rc;
612
613 rc = memcpy_hsa_kernel(&ipib_info, __LC_DUMP_REIPL, sizeof(ipib_info));
614 if (rc)
615 return rc;
616 if (ipib_info.ipib == 0)
617 return 0;
618 ipl_block = (void *) __get_free_page(GFP_KERNEL);
619 if (!ipl_block)
620 return -ENOMEM;
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200621 if (ipib_info.ipib < sclp.hsa_size)
Frank Munzert099b7652009-03-26 15:23:43 +0100622 rc = memcpy_hsa_kernel(ipl_block, ipib_info.ipib, PAGE_SIZE);
623 else
Michael Holzheu92fe3132010-03-24 11:49:50 +0100624 rc = memcpy_real(ipl_block, (void *) ipib_info.ipib, PAGE_SIZE);
Michael Holzheu76ef9642010-04-22 17:17:07 +0200625 if (rc || csum_partial(ipl_block, ipl_block->hdr.len, 0) !=
Frank Munzert159d1ff2009-03-26 15:24:45 +0100626 ipib_info.checksum) {
Frank Munzert099b7652009-03-26 15:23:43 +0100627 TRACE("Checksum does not match\n");
628 free_page((unsigned long) ipl_block);
629 ipl_block = NULL;
630 }
631 return 0;
632}
633
Michael Holzheu411ed322007-04-27 16:01:49 +0200634static int __init zcore_init(void)
635{
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200636 unsigned long mem_size, mem_end;
Michael Holzheu411ed322007-04-27 16:01:49 +0200637 unsigned char arch;
638 int rc;
639
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200640 mem_size = mem_end = 0;
Michael Holzheu411ed322007-04-27 16:01:49 +0200641 if (ipl_info.type != IPL_TYPE_FCP_DUMP)
642 return -ENODATA;
Michael Holzheu3f25dc42011-11-14 11:19:05 +0100643 if (OLDMEM_BASE)
644 return -ENODATA;
Michael Holzheu411ed322007-04-27 16:01:49 +0200645
646 zcore_dbf = debug_register("zcore", 4, 1, 4 * sizeof(long));
647 debug_register_view(zcore_dbf, &debug_sprintf_view);
648 debug_set_level(zcore_dbf, 6);
649
650 TRACE("devno: %x\n", ipl_info.data.fcp.dev_id.devno);
651 TRACE("wwpn: %llx\n", (unsigned long long) ipl_info.data.fcp.wwpn);
652 TRACE("lun: %llx\n", (unsigned long long) ipl_info.data.fcp.lun);
653
Heiko Carstens763968e2007-05-10 15:45:46 +0200654 rc = sclp_sdias_init();
Michael Holzheu411ed322007-04-27 16:01:49 +0200655 if (rc)
656 goto fail;
657
658 rc = check_sdias();
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200659 if (rc)
Michael Holzheu411ed322007-04-27 16:01:49 +0200660 goto fail;
Michael Holzheub4b3d122013-01-21 18:37:41 +0100661 hsa_available = 1;
Michael Holzheu411ed322007-04-27 16:01:49 +0200662
663 rc = memcpy_hsa_kernel(&arch, __LC_AR_MODE_ID, 1);
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200664 if (rc)
Michael Holzheu411ed322007-04-27 16:01:49 +0200665 goto fail;
Michael Holzheu411ed322007-04-27 16:01:49 +0200666
Heiko Carstensf64ca212010-02-26 22:37:32 +0100667 if (arch == ARCH_S390) {
668 pr_alert("The 64-bit dump tool cannot be used for a "
669 "32-bit system\n");
670 rc = -EINVAL;
671 goto fail;
672 }
Michael Holzheu411ed322007-04-27 16:01:49 +0200673
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200674 rc = get_mem_info(&mem_size, &mem_end);
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200675 if (rc)
Michael Holzheu411ed322007-04-27 16:01:49 +0200676 goto fail;
Michael Holzheu411ed322007-04-27 16:01:49 +0200677
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200678 rc = sys_info_init(arch, mem_end);
Frank Munzert12e0c952008-07-17 17:16:40 +0200679 if (rc)
680 goto fail;
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200681 zcore_header_init(arch, &zcore_header, mem_size);
Michael Holzheu411ed322007-04-27 16:01:49 +0200682
Frank Munzert099b7652009-03-26 15:23:43 +0100683 rc = zcore_reipl_init();
684 if (rc)
685 goto fail;
686
Michael Holzheu411ed322007-04-27 16:01:49 +0200687 zcore_dir = debugfs_create_dir("zcore" , NULL);
688 if (!zcore_dir) {
689 rc = -ENOMEM;
690 goto fail;
691 }
692 zcore_file = debugfs_create_file("mem", S_IRUSR, zcore_dir, NULL,
693 &zcore_fops);
694 if (!zcore_file) {
Michael Holzheu411ed322007-04-27 16:01:49 +0200695 rc = -ENOMEM;
Frank Munzert12e0c952008-07-17 17:16:40 +0200696 goto fail_dir;
697 }
698 zcore_memmap_file = debugfs_create_file("memmap", S_IRUSR, zcore_dir,
699 NULL, &zcore_memmap_fops);
700 if (!zcore_memmap_file) {
701 rc = -ENOMEM;
702 goto fail_file;
Michael Holzheu411ed322007-04-27 16:01:49 +0200703 }
Frank Munzert099b7652009-03-26 15:23:43 +0100704 zcore_reipl_file = debugfs_create_file("reipl", S_IRUSR, zcore_dir,
705 NULL, &zcore_reipl_fops);
706 if (!zcore_reipl_file) {
707 rc = -ENOMEM;
708 goto fail_memmap_file;
709 }
Michael Holzheub4b3d122013-01-21 18:37:41 +0100710 zcore_hsa_file = debugfs_create_file("hsa", S_IRUSR|S_IWUSR, zcore_dir,
711 NULL, &zcore_hsa_fops);
712 if (!zcore_hsa_file) {
713 rc = -ENOMEM;
714 goto fail_reipl_file;
715 }
Michael Holzheu411ed322007-04-27 16:01:49 +0200716 return 0;
717
Michael Holzheub4b3d122013-01-21 18:37:41 +0100718fail_reipl_file:
719 debugfs_remove(zcore_reipl_file);
Frank Munzert099b7652009-03-26 15:23:43 +0100720fail_memmap_file:
721 debugfs_remove(zcore_memmap_file);
Frank Munzert12e0c952008-07-17 17:16:40 +0200722fail_file:
723 debugfs_remove(zcore_file);
724fail_dir:
725 debugfs_remove(zcore_dir);
Michael Holzheu411ed322007-04-27 16:01:49 +0200726fail:
727 diag308(DIAG308_REL_HSA, NULL);
728 return rc;
729}
730
Michael Holzheu411ed322007-04-27 16:01:49 +0200731static void __exit zcore_exit(void)
732{
733 debug_unregister(zcore_dbf);
Heiko Carstens763968e2007-05-10 15:45:46 +0200734 sclp_sdias_exit();
Frank Munzert099b7652009-03-26 15:23:43 +0100735 free_page((unsigned long) ipl_block);
Michael Holzheub4b3d122013-01-21 18:37:41 +0100736 debugfs_remove(zcore_hsa_file);
Frank Munzert099b7652009-03-26 15:23:43 +0100737 debugfs_remove(zcore_reipl_file);
738 debugfs_remove(zcore_memmap_file);
739 debugfs_remove(zcore_file);
740 debugfs_remove(zcore_dir);
Michael Holzheu411ed322007-04-27 16:01:49 +0200741 diag308(DIAG308_REL_HSA, NULL);
742}
743
Frank Munzert099b7652009-03-26 15:23:43 +0100744MODULE_AUTHOR("Copyright IBM Corp. 2003,2008");
Michael Holzheu411ed322007-04-27 16:01:49 +0200745MODULE_DESCRIPTION("zcore module for zfcpdump support");
746MODULE_LICENSE("GPL");
747
748subsys_initcall(zcore_init);
749module_exit(zcore_exit);