blob: e0c87a83eb34b6b0147411d324d635e6e97e0a6e [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>
Martin Schwidefskybbfed512015-10-15 11:14:19 +020031#include <asm/os_info.h>
Michael Holzheua62bc072014-10-06 17:57:43 +020032#include <asm/switch_to.h>
Heiko Carstens763968e2007-05-10 15:45:46 +020033#include "sclp.h"
Michael Holzheu411ed322007-04-27 16:01:49 +020034
35#define TRACE(x...) debug_sprintf_event(zcore_dbf, 1, x)
Michael Holzheu411ed322007-04-27 16:01:49 +020036
Michael Holzheu6f79d332013-09-11 14:24:54 -070037#define TO_USER 1
38#define TO_KERNEL 0
Frank Munzert12e0c952008-07-17 17:16:40 +020039#define CHUNK_INFO_SIZE 34 /* 2 16-byte char, each followed by blank */
Michael Holzheu411ed322007-04-27 16:01:49 +020040
41enum arch_id {
42 ARCH_S390 = 0,
43 ARCH_S390X = 1,
44};
45
46/* dump system info */
47
48struct sys_info {
Heiko Carstensf64ca212010-02-26 22:37:32 +010049 enum arch_id arch;
50 unsigned long sa_base;
51 u32 sa_size;
52 int cpu_map[NR_CPUS];
53 unsigned long mem_size;
54 struct save_area lc_mask;
Michael Holzheu411ed322007-04-27 16:01:49 +020055};
56
Frank Munzert099b7652009-03-26 15:23:43 +010057struct ipib_info {
58 unsigned long ipib;
59 u32 checksum;
60} __attribute__((packed));
61
Michael Holzheu411ed322007-04-27 16:01:49 +020062static struct sys_info sys_info;
63static struct debug_info *zcore_dbf;
64static int hsa_available;
65static struct dentry *zcore_dir;
66static struct dentry *zcore_file;
Frank Munzert12e0c952008-07-17 17:16:40 +020067static struct dentry *zcore_memmap_file;
Frank Munzert099b7652009-03-26 15:23:43 +010068static struct dentry *zcore_reipl_file;
Michael Holzheub4b3d122013-01-21 18:37:41 +010069static struct dentry *zcore_hsa_file;
Frank Munzert099b7652009-03-26 15:23:43 +010070static struct ipl_parameter_block *ipl_block;
Michael Holzheu411ed322007-04-27 16:01:49 +020071
72/*
73 * Copy memory from HSA to kernel or user memory (not reentrant):
74 *
75 * @dest: Kernel or user buffer where memory should be copied to
76 * @src: Start address within HSA where data should be copied
77 * @count: Size of buffer, which should be copied
78 * @mode: Either TO_KERNEL or TO_USER
79 */
Michael Holzheu6f79d332013-09-11 14:24:54 -070080int memcpy_hsa(void *dest, unsigned long src, size_t count, int mode)
Michael Holzheu411ed322007-04-27 16:01:49 +020081{
82 int offs, blk_num;
83 static char buf[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
84
Michael Holzheub4b3d122013-01-21 18:37:41 +010085 if (!hsa_available)
86 return -ENODATA;
Michael Holzheu411ed322007-04-27 16:01:49 +020087 if (count == 0)
88 return 0;
89
90 /* copy first block */
91 offs = 0;
92 if ((src % PAGE_SIZE) != 0) {
93 blk_num = src / PAGE_SIZE + 2;
94 if (sclp_sdias_copy(buf, blk_num, 1)) {
95 TRACE("sclp_sdias_copy() failed\n");
96 return -EIO;
97 }
98 offs = min((PAGE_SIZE - (src % PAGE_SIZE)), count);
99 if (mode == TO_USER) {
100 if (copy_to_user((__force __user void*) dest,
101 buf + (src % PAGE_SIZE), offs))
102 return -EFAULT;
103 } else
104 memcpy(dest, buf + (src % PAGE_SIZE), offs);
105 }
106 if (offs == count)
107 goto out;
108
109 /* copy middle */
110 for (; (offs + PAGE_SIZE) <= count; offs += PAGE_SIZE) {
111 blk_num = (src + offs) / PAGE_SIZE + 2;
112 if (sclp_sdias_copy(buf, blk_num, 1)) {
113 TRACE("sclp_sdias_copy() failed\n");
114 return -EIO;
115 }
116 if (mode == TO_USER) {
117 if (copy_to_user((__force __user void*) dest + offs,
118 buf, PAGE_SIZE))
119 return -EFAULT;
120 } else
121 memcpy(dest + offs, buf, PAGE_SIZE);
122 }
123 if (offs == count)
124 goto out;
125
126 /* copy last block */
127 blk_num = (src + offs) / PAGE_SIZE + 2;
128 if (sclp_sdias_copy(buf, blk_num, 1)) {
129 TRACE("sclp_sdias_copy() failed\n");
130 return -EIO;
131 }
132 if (mode == TO_USER) {
133 if (copy_to_user((__force __user void*) dest + offs, buf,
Michael Holzheu241fd9b2013-04-19 18:03:02 +0200134 count - offs))
Michael Holzheu411ed322007-04-27 16:01:49 +0200135 return -EFAULT;
136 } else
137 memcpy(dest + offs, buf, count - offs);
138out:
139 return 0;
140}
141
142static int memcpy_hsa_user(void __user *dest, unsigned long src, size_t count)
143{
144 return memcpy_hsa((void __force *) dest, src, count, TO_USER);
145}
146
147static int memcpy_hsa_kernel(void *dest, unsigned long src, size_t count)
148{
149 return memcpy_hsa(dest, src, count, TO_KERNEL);
150}
151
Michael Holzheu411ed322007-04-27 16:01:49 +0200152static int __init init_cpu_info(enum arch_id arch)
153{
Michael Holzheua62bc072014-10-06 17:57:43 +0200154 struct save_area_ext *sa_ext;
Martin Schwidefskybbfed512015-10-15 11:14:19 +0200155 struct _lowcore *lc;
156 void *ptr;
157 int i;
Michael Holzheu411ed322007-04-27 16:01:49 +0200158
159 /* get info for boot cpu from lowcore, stored in the HSA */
160
Michael Holzheu1592a8e2015-05-26 19:05:23 +0200161 sa_ext = dump_save_areas.areas[0];
Michael Holzheua62bc072014-10-06 17:57:43 +0200162 if (!sa_ext)
Michael Holzheu411ed322007-04-27 16:01:49 +0200163 return -ENOMEM;
Michael Holzheua62bc072014-10-06 17:57:43 +0200164 if (memcpy_hsa_kernel(&sa_ext->sa, sys_info.sa_base,
165 sys_info.sa_size) < 0) {
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200166 TRACE("could not copy from HSA\n");
Michael Holzheu411ed322007-04-27 16:01:49 +0200167 return -EIO;
168 }
Martin Schwidefskybbfed512015-10-15 11:14:19 +0200169 if (!MACHINE_HAS_VX)
170 return 0;
171
172 save_vx_regs_safe(sa_ext->vx_regs);
173 /* Get address of the vector register save area for each CPU */
174 for (i = 0; i < dump_save_areas.count; i++) {
175 sa_ext = dump_save_areas.areas[i];
176 lc = (struct _lowcore *)(unsigned long) sa_ext->sa.pref_reg;
177 ptr = &lc->vector_save_area_addr;
178 copy_from_oldmem(&sa_ext->vx_sa_addr, ptr,
179 sizeof(sa_ext->vx_sa_addr));
180 }
Michael Holzheu411ed322007-04-27 16:01:49 +0200181 return 0;
182}
183
184static DEFINE_MUTEX(zcore_mutex);
185
Michael Holzheu0cbde8e2010-02-26 22:37:55 +0100186#define DUMP_VERSION 0x5
Michael Holzheu411ed322007-04-27 16:01:49 +0200187#define DUMP_MAGIC 0xa8190173618f23fdULL
188#define DUMP_ARCH_S390X 2
189#define DUMP_ARCH_S390 1
190#define HEADER_SIZE 4096
191
192/* dump header dumped according to s390 crash dump format */
193
194struct zcore_header {
195 u64 magic;
196 u32 version;
197 u32 header_size;
198 u32 dump_level;
199 u32 page_size;
200 u64 mem_size;
201 u64 mem_start;
202 u64 mem_end;
203 u32 num_pages;
204 u32 pad1;
205 u64 tod;
Heiko Carstense86a6ed2009-09-11 10:29:04 +0200206 struct cpuid cpu_id;
Michael Holzheu411ed322007-04-27 16:01:49 +0200207 u32 arch_id;
Michael Holzheuce444822007-06-19 13:10:02 +0200208 u32 volnr;
Michael Holzheu411ed322007-04-27 16:01:49 +0200209 u32 build_arch;
Michael Holzheuce444822007-06-19 13:10:02 +0200210 u64 rmem_size;
Michael Holzheu0cbde8e2010-02-26 22:37:55 +0100211 u8 mvdump;
212 u16 cpu_cnt;
213 u16 real_cpu_cnt;
214 u8 end_pad1[0x200-0x061];
215 u64 mvdump_sign;
216 u64 mvdump_zipl_time;
217 u8 end_pad2[0x800-0x210];
218 u32 lc_vec[512];
Michael Holzheu411ed322007-04-27 16:01:49 +0200219} __attribute__((packed,__aligned__(16)));
220
221static struct zcore_header zcore_header = {
222 .magic = DUMP_MAGIC,
223 .version = DUMP_VERSION,
224 .header_size = 4096,
225 .dump_level = 0,
226 .page_size = PAGE_SIZE,
227 .mem_start = 0,
Michael Holzheu411ed322007-04-27 16:01:49 +0200228 .build_arch = DUMP_ARCH_S390X,
Michael Holzheu411ed322007-04-27 16:01:49 +0200229};
230
231/*
232 * Copy lowcore info to buffer. Use map in order to copy only register parts.
233 *
234 * @buf: User buffer
235 * @sa: Pointer to save area
236 * @sa_off: Offset in save area to copy
237 * @len: Number of bytes to copy
238 */
239static int copy_lc(void __user *buf, void *sa, int sa_off, int len)
240{
241 int i;
242 char *lc_mask = (char*)&sys_info.lc_mask;
243
244 for (i = 0; i < len; i++) {
245 if (!lc_mask[i + sa_off])
246 continue;
247 if (copy_to_user(buf + i, sa + sa_off + i, 1))
248 return -EFAULT;
249 }
250 return 0;
251}
252
253/*
254 * Copy lowcores info to memory, if necessary
255 *
256 * @buf: User buffer
257 * @addr: Start address of buffer in dump memory
258 * @count: Size of buffer
259 */
260static int zcore_add_lc(char __user *buf, unsigned long start, size_t count)
261{
Martin Schwidefskybbfed512015-10-15 11:14:19 +0200262 struct save_area_ext *sa_ext;
263 struct save_area *sa;
Michael Holzheu411ed322007-04-27 16:01:49 +0200264 unsigned long end;
Michael Holzheu58952942013-10-11 10:29:23 +0200265 int i;
Michael Holzheu411ed322007-04-27 16:01:49 +0200266
267 if (count == 0)
268 return 0;
269
270 end = start + count;
Michael Holzheu58952942013-10-11 10:29:23 +0200271 for (i = 0; i < dump_save_areas.count; i++) {
Michael Holzheu411ed322007-04-27 16:01:49 +0200272 unsigned long cp_start, cp_end; /* copy range */
273 unsigned long sa_start, sa_end; /* save area range */
Michael Holzheu411ed322007-04-27 16:01:49 +0200274 unsigned long sa_off, len, buf_off;
275
Martin Schwidefskybbfed512015-10-15 11:14:19 +0200276 sa_ext = dump_save_areas.areas[i];
277 sa = &sa_ext->sa;
Michael Holzheu411ed322007-04-27 16:01:49 +0200278
Martin Schwidefskybbfed512015-10-15 11:14:19 +0200279 /* Copy the 512 bytes lowcore save area 0x1200 - 0x1400 */
280 sa_start = sa->pref_reg + sys_info.sa_base;
281 sa_end = sa_start + sys_info.sa_size;
282
283 if (end >= sa_start && start < sa_end) {
284 cp_start = max(start, sa_start);
285 cp_end = min(end, sa_end);
286 buf_off = cp_start - start;
287 sa_off = cp_start - sa_start;
288 len = cp_end - cp_start;
289
290 TRACE("copy_lc: %lx-%lx\n", cp_start, cp_end);
291 if (copy_lc(buf + buf_off, sa, sa_off, len))
292 return -EFAULT;
293 }
294
295 if (!MACHINE_HAS_VX)
Michael Holzheu58952942013-10-11 10:29:23 +0200296 continue;
Michael Holzheu411ed322007-04-27 16:01:49 +0200297
Martin Schwidefskybbfed512015-10-15 11:14:19 +0200298 /* Copy the 512 bytes vector save area */
299 sa_start = sa_ext->vx_sa_addr & -1024UL;
300 sa_end = sa_start + 512;
Michael Holzheu411ed322007-04-27 16:01:49 +0200301
Martin Schwidefskybbfed512015-10-15 11:14:19 +0200302 if (end >= sa_start && start < sa_end) {
303 cp_start = max(start, sa_start);
304 cp_end = min(end, sa_end);
305
306 buf_off = cp_start - start;
307 sa_off = cp_start - sa_start;
308 len = cp_end - cp_start;
309
310 TRACE("copy vxrs: %lx-%lx\n", cp_start, cp_end);
311 if (copy_to_user(buf + buf_off,
312 (void *) &sa_ext->vx_regs + sa_off,
313 len))
314 return -EFAULT;
315 }
Michael Holzheu411ed322007-04-27 16:01:49 +0200316 }
317 return 0;
318}
319
320/*
Michael Holzheub4b3d122013-01-21 18:37:41 +0100321 * Release the HSA
322 */
323static void release_hsa(void)
324{
325 diag308(DIAG308_REL_HSA, NULL);
326 hsa_available = 0;
327}
328
329/*
Michael Holzheu411ed322007-04-27 16:01:49 +0200330 * Read routine for zcore character device
331 * First 4K are dump header
332 * Next 32MB are HSA Memory
333 * Rest is read from absolute Memory
334 */
335static ssize_t zcore_read(struct file *file, char __user *buf, size_t count,
336 loff_t *ppos)
337{
338 unsigned long mem_start; /* Start address in memory */
339 size_t mem_offs; /* Offset in dump memory */
340 size_t hdr_count; /* Size of header part of output buffer */
341 size_t size;
342 int rc;
343
344 mutex_lock(&zcore_mutex);
345
346 if (*ppos > (sys_info.mem_size + HEADER_SIZE)) {
347 rc = -EINVAL;
348 goto fail;
349 }
350
351 count = min(count, (size_t) (sys_info.mem_size + HEADER_SIZE - *ppos));
352
353 /* Copy dump header */
354 if (*ppos < HEADER_SIZE) {
355 size = min(count, (size_t) (HEADER_SIZE - *ppos));
356 if (copy_to_user(buf, &zcore_header + *ppos, size)) {
357 rc = -EFAULT;
358 goto fail;
359 }
360 hdr_count = size;
361 mem_start = 0;
362 } else {
363 hdr_count = 0;
364 mem_start = *ppos - HEADER_SIZE;
365 }
366
367 mem_offs = 0;
368
369 /* Copy from HSA data */
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200370 if (*ppos < sclp.hsa_size + HEADER_SIZE) {
Michael Holzheue657d8f2013-11-13 10:38:27 +0100371 size = min((count - hdr_count),
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200372 (size_t) (sclp.hsa_size - mem_start));
Michael Holzheu411ed322007-04-27 16:01:49 +0200373 rc = memcpy_hsa_user(buf + hdr_count, mem_start, size);
374 if (rc)
375 goto fail;
376
377 mem_offs += size;
378 }
379
380 /* Copy from real mem */
381 size = count - mem_offs - hdr_count;
Michael Holzheu7f0bf652011-10-30 15:16:39 +0100382 rc = copy_to_user_real(buf + hdr_count + mem_offs,
383 (void *) mem_start + mem_offs, size);
Michael Holzheu411ed322007-04-27 16:01:49 +0200384 if (rc)
385 goto fail;
386
387 /*
388 * Since s390 dump analysis tools like lcrash or crash
389 * expect register sets in the prefix pages of the cpus,
390 * we copy them into the read buffer, if necessary.
391 * buf + hdr_count: Start of memory part of output buffer
392 * mem_start: Start memory address to copy from
393 * count - hdr_count: Size of memory area to copy
394 */
395 if (zcore_add_lc(buf + hdr_count, mem_start, count - hdr_count)) {
396 rc = -EFAULT;
397 goto fail;
398 }
399 *ppos += count;
400fail:
401 mutex_unlock(&zcore_mutex);
402 return (rc < 0) ? rc : count;
403}
404
405static int zcore_open(struct inode *inode, struct file *filp)
406{
407 if (!hsa_available)
408 return -ENODATA;
409 else
410 return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
411}
412
413static int zcore_release(struct inode *inode, struct file *filep)
414{
Michael Holzheub4b3d122013-01-21 18:37:41 +0100415 if (hsa_available)
416 release_hsa();
Michael Holzheu411ed322007-04-27 16:01:49 +0200417 return 0;
418}
419
420static loff_t zcore_lseek(struct file *file, loff_t offset, int orig)
421{
422 loff_t rc;
423
424 mutex_lock(&zcore_mutex);
425 switch (orig) {
426 case 0:
427 file->f_pos = offset;
428 rc = file->f_pos;
429 break;
430 case 1:
431 file->f_pos += offset;
432 rc = file->f_pos;
433 break;
434 default:
435 rc = -EINVAL;
436 }
437 mutex_unlock(&zcore_mutex);
438 return rc;
439}
440
Jan Engelhardt5c81cdb2008-01-26 14:11:29 +0100441static const struct file_operations zcore_fops = {
Michael Holzheu411ed322007-04-27 16:01:49 +0200442 .owner = THIS_MODULE,
443 .llseek = zcore_lseek,
444 .read = zcore_read,
445 .open = zcore_open,
446 .release = zcore_release,
447};
448
Frank Munzert12e0c952008-07-17 17:16:40 +0200449static ssize_t zcore_memmap_read(struct file *filp, char __user *buf,
450 size_t count, loff_t *ppos)
451{
452 return simple_read_from_buffer(buf, count, ppos, filp->private_data,
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100453 memblock.memory.cnt * CHUNK_INFO_SIZE);
Frank Munzert12e0c952008-07-17 17:16:40 +0200454}
455
456static int zcore_memmap_open(struct inode *inode, struct file *filp)
457{
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100458 struct memblock_region *reg;
Frank Munzert12e0c952008-07-17 17:16:40 +0200459 char *buf;
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100460 int i = 0;
Frank Munzert12e0c952008-07-17 17:16:40 +0200461
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100462 buf = kzalloc(memblock.memory.cnt * CHUNK_INFO_SIZE, GFP_KERNEL);
Frank Munzert12e0c952008-07-17 17:16:40 +0200463 if (!buf) {
Frank Munzert12e0c952008-07-17 17:16:40 +0200464 return -ENOMEM;
465 }
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100466 for_each_memblock(memory, reg) {
467 sprintf(buf + (i++ * CHUNK_INFO_SIZE), "%016llx %016llx ",
468 (unsigned long long) reg->base,
469 (unsigned long long) reg->size);
Frank Munzert12e0c952008-07-17 17:16:40 +0200470 }
Frank Munzert12e0c952008-07-17 17:16:40 +0200471 filp->private_data = buf;
Martin Schwidefsky58ea91c2010-05-17 10:00:07 +0200472 return nonseekable_open(inode, filp);
Frank Munzert12e0c952008-07-17 17:16:40 +0200473}
474
475static int zcore_memmap_release(struct inode *inode, struct file *filp)
476{
477 kfree(filp->private_data);
478 return 0;
479}
480
481static const struct file_operations zcore_memmap_fops = {
482 .owner = THIS_MODULE,
483 .read = zcore_memmap_read,
484 .open = zcore_memmap_open,
485 .release = zcore_memmap_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200486 .llseek = no_llseek,
Frank Munzert12e0c952008-07-17 17:16:40 +0200487};
488
Frank Munzert099b7652009-03-26 15:23:43 +0100489static ssize_t zcore_reipl_write(struct file *filp, const char __user *buf,
490 size_t count, loff_t *ppos)
491{
492 if (ipl_block) {
493 diag308(DIAG308_SET, ipl_block);
494 diag308(DIAG308_IPL, NULL);
495 }
496 return count;
497}
498
499static int zcore_reipl_open(struct inode *inode, struct file *filp)
500{
Martin Schwidefsky58ea91c2010-05-17 10:00:07 +0200501 return nonseekable_open(inode, filp);
Frank Munzert099b7652009-03-26 15:23:43 +0100502}
503
504static int zcore_reipl_release(struct inode *inode, struct file *filp)
505{
506 return 0;
507}
508
509static const struct file_operations zcore_reipl_fops = {
510 .owner = THIS_MODULE,
511 .write = zcore_reipl_write,
512 .open = zcore_reipl_open,
513 .release = zcore_reipl_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200514 .llseek = no_llseek,
Frank Munzert099b7652009-03-26 15:23:43 +0100515};
516
Michael Holzheub4b3d122013-01-21 18:37:41 +0100517static ssize_t zcore_hsa_read(struct file *filp, char __user *buf,
518 size_t count, loff_t *ppos)
519{
520 static char str[18];
521
522 if (hsa_available)
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200523 snprintf(str, sizeof(str), "%lx\n", sclp.hsa_size);
Michael Holzheub4b3d122013-01-21 18:37:41 +0100524 else
525 snprintf(str, sizeof(str), "0\n");
526 return simple_read_from_buffer(buf, count, ppos, str, strlen(str));
527}
528
529static ssize_t zcore_hsa_write(struct file *filp, const char __user *buf,
530 size_t count, loff_t *ppos)
531{
532 char value;
533
534 if (*ppos != 0)
535 return -EPIPE;
536 if (copy_from_user(&value, buf, 1))
537 return -EFAULT;
538 if (value != '0')
539 return -EINVAL;
540 release_hsa();
541 return count;
542}
543
544static const struct file_operations zcore_hsa_fops = {
545 .owner = THIS_MODULE,
546 .write = zcore_hsa_write,
547 .read = zcore_hsa_read,
548 .open = nonseekable_open,
549 .llseek = no_llseek,
550};
551
Heiko Carstensf64ca212010-02-26 22:37:32 +0100552static void __init set_lc_mask(struct save_area *map)
Michael Holzheu411ed322007-04-27 16:01:49 +0200553{
Heiko Carstensf64ca212010-02-26 22:37:32 +0100554 memset(&map->fp_regs, 0xff, sizeof(map->fp_regs));
555 memset(&map->gp_regs, 0xff, sizeof(map->gp_regs));
556 memset(&map->psw, 0xff, sizeof(map->psw));
557 memset(&map->pref_reg, 0xff, sizeof(map->pref_reg));
558 memset(&map->fp_ctrl_reg, 0xff, sizeof(map->fp_ctrl_reg));
559 memset(&map->tod_reg, 0xff, sizeof(map->tod_reg));
560 memset(&map->timer, 0xff, sizeof(map->timer));
561 memset(&map->clk_cmp, 0xff, sizeof(map->clk_cmp));
562 memset(&map->acc_regs, 0xff, sizeof(map->acc_regs));
563 memset(&map->ctrl_regs, 0xff, sizeof(map->ctrl_regs));
Michael Holzheu411ed322007-04-27 16:01:49 +0200564}
565
566/*
567 * Initialize dump globals for a given architecture
568 */
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200569static int __init sys_info_init(enum arch_id arch, unsigned long mem_end)
Michael Holzheu411ed322007-04-27 16:01:49 +0200570{
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200571 int rc;
572
Michael Holzheu411ed322007-04-27 16:01:49 +0200573 switch (arch) {
574 case ARCH_S390X:
Michael Holzheu17159dc62008-12-25 13:39:51 +0100575 pr_alert("DETECTED 'S390X (64 bit) OS'\n");
Michael Holzheu411ed322007-04-27 16:01:49 +0200576 break;
577 case ARCH_S390:
Michael Holzheu17159dc62008-12-25 13:39:51 +0100578 pr_alert("DETECTED 'S390 (32 bit) OS'\n");
Michael Holzheu411ed322007-04-27 16:01:49 +0200579 break;
580 default:
Michael Holzheu17159dc62008-12-25 13:39:51 +0100581 pr_alert("0x%x is an unknown architecture.\n",arch);
Michael Holzheu411ed322007-04-27 16:01:49 +0200582 return -EINVAL;
583 }
Heiko Carstensf64ca212010-02-26 22:37:32 +0100584 sys_info.sa_base = SAVE_AREA_BASE;
585 sys_info.sa_size = sizeof(struct save_area);
Michael Holzheu411ed322007-04-27 16:01:49 +0200586 sys_info.arch = arch;
Heiko Carstensf64ca212010-02-26 22:37:32 +0100587 set_lc_mask(&sys_info.lc_mask);
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200588 rc = init_cpu_info(arch);
589 if (rc)
590 return rc;
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200591 sys_info.mem_size = mem_end;
Michael Holzheu411ed322007-04-27 16:01:49 +0200592
593 return 0;
594}
595
596static int __init check_sdias(void)
597{
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200598 if (!sclp.hsa_size) {
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200599 TRACE("Could not determine HSA size\n");
Michael Holzheue657d8f2013-11-13 10:38:27 +0100600 return -ENODEV;
Michael Holzheu411ed322007-04-27 16:01:49 +0200601 }
602 return 0;
603}
604
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200605static int __init get_mem_info(unsigned long *mem, unsigned long *end)
Michael Holzheu411ed322007-04-27 16:01:49 +0200606{
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100607 struct memblock_region *reg;
Frank Munzert12e0c952008-07-17 17:16:40 +0200608
Philipp Hachtmann50be6342014-01-29 18:16:01 +0100609 for_each_memblock(memory, reg) {
610 *mem += reg->size;
611 *end = max_t(unsigned long, *end, reg->base + reg->size);
Frank Munzert12e0c952008-07-17 17:16:40 +0200612 }
Frank Munzert12e0c952008-07-17 17:16:40 +0200613 return 0;
614}
615
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200616static void __init zcore_header_init(int arch, struct zcore_header *hdr,
617 unsigned long mem_size)
Frank Munzert12e0c952008-07-17 17:16:40 +0200618{
Michael Holzheu0cbde8e2010-02-26 22:37:55 +0100619 u32 prefix;
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200620 int i;
Frank Munzert12e0c952008-07-17 17:16:40 +0200621
Michael Holzheu411ed322007-04-27 16:01:49 +0200622 if (arch == ARCH_S390X)
623 hdr->arch_id = DUMP_ARCH_S390X;
624 else
625 hdr->arch_id = DUMP_ARCH_S390;
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200626 hdr->mem_size = mem_size;
627 hdr->rmem_size = mem_size;
Michael Holzheu411ed322007-04-27 16:01:49 +0200628 hdr->mem_end = sys_info.mem_size;
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200629 hdr->num_pages = mem_size / PAGE_SIZE;
Heiko Carstens1aae0562013-01-30 09:49:40 +0100630 hdr->tod = get_tod_clock();
Michael Holzheu411ed322007-04-27 16:01:49 +0200631 get_cpu_id(&hdr->cpu_id);
Michael Holzheu58952942013-10-11 10:29:23 +0200632 for (i = 0; i < dump_save_areas.count; i++) {
Michael Holzheua62bc072014-10-06 17:57:43 +0200633 prefix = dump_save_areas.areas[i]->sa.pref_reg;
Michael Holzheu0cbde8e2010-02-26 22:37:55 +0100634 hdr->real_cpu_cnt++;
635 if (!prefix)
636 continue;
637 hdr->lc_vec[hdr->cpu_cnt] = prefix;
638 hdr->cpu_cnt++;
639 }
Michael Holzheu411ed322007-04-27 16:01:49 +0200640}
641
Frank Munzert099b7652009-03-26 15:23:43 +0100642/*
643 * Provide IPL parameter information block from either HSA or memory
644 * for future reipl
645 */
646static int __init zcore_reipl_init(void)
647{
648 struct ipib_info ipib_info;
649 int rc;
650
651 rc = memcpy_hsa_kernel(&ipib_info, __LC_DUMP_REIPL, sizeof(ipib_info));
652 if (rc)
653 return rc;
654 if (ipib_info.ipib == 0)
655 return 0;
656 ipl_block = (void *) __get_free_page(GFP_KERNEL);
657 if (!ipl_block)
658 return -ENOMEM;
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200659 if (ipib_info.ipib < sclp.hsa_size)
Frank Munzert099b7652009-03-26 15:23:43 +0100660 rc = memcpy_hsa_kernel(ipl_block, ipib_info.ipib, PAGE_SIZE);
661 else
Michael Holzheu92fe3132010-03-24 11:49:50 +0100662 rc = memcpy_real(ipl_block, (void *) ipib_info.ipib, PAGE_SIZE);
Michael Holzheu76ef9642010-04-22 17:17:07 +0200663 if (rc || csum_partial(ipl_block, ipl_block->hdr.len, 0) !=
Frank Munzert159d1ff2009-03-26 15:24:45 +0100664 ipib_info.checksum) {
Frank Munzert099b7652009-03-26 15:23:43 +0100665 TRACE("Checksum does not match\n");
666 free_page((unsigned long) ipl_block);
667 ipl_block = NULL;
668 }
669 return 0;
670}
671
Michael Holzheu411ed322007-04-27 16:01:49 +0200672static int __init zcore_init(void)
673{
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200674 unsigned long mem_size, mem_end;
Michael Holzheu411ed322007-04-27 16:01:49 +0200675 unsigned char arch;
676 int rc;
677
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200678 mem_size = mem_end = 0;
Michael Holzheu411ed322007-04-27 16:01:49 +0200679 if (ipl_info.type != IPL_TYPE_FCP_DUMP)
680 return -ENODATA;
Michael Holzheu3f25dc42011-11-14 11:19:05 +0100681 if (OLDMEM_BASE)
682 return -ENODATA;
Michael Holzheu411ed322007-04-27 16:01:49 +0200683
684 zcore_dbf = debug_register("zcore", 4, 1, 4 * sizeof(long));
685 debug_register_view(zcore_dbf, &debug_sprintf_view);
686 debug_set_level(zcore_dbf, 6);
687
688 TRACE("devno: %x\n", ipl_info.data.fcp.dev_id.devno);
689 TRACE("wwpn: %llx\n", (unsigned long long) ipl_info.data.fcp.wwpn);
690 TRACE("lun: %llx\n", (unsigned long long) ipl_info.data.fcp.lun);
691
Heiko Carstens763968e2007-05-10 15:45:46 +0200692 rc = sclp_sdias_init();
Michael Holzheu411ed322007-04-27 16:01:49 +0200693 if (rc)
694 goto fail;
695
696 rc = check_sdias();
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200697 if (rc)
Michael Holzheu411ed322007-04-27 16:01:49 +0200698 goto fail;
Michael Holzheub4b3d122013-01-21 18:37:41 +0100699 hsa_available = 1;
Michael Holzheu411ed322007-04-27 16:01:49 +0200700
701 rc = memcpy_hsa_kernel(&arch, __LC_AR_MODE_ID, 1);
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200702 if (rc)
Michael Holzheu411ed322007-04-27 16:01:49 +0200703 goto fail;
Michael Holzheu411ed322007-04-27 16:01:49 +0200704
Heiko Carstensf64ca212010-02-26 22:37:32 +0100705 if (arch == ARCH_S390) {
706 pr_alert("The 64-bit dump tool cannot be used for a "
707 "32-bit system\n");
708 rc = -EINVAL;
709 goto fail;
710 }
Michael Holzheu411ed322007-04-27 16:01:49 +0200711
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200712 rc = get_mem_info(&mem_size, &mem_end);
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200713 if (rc)
Michael Holzheu411ed322007-04-27 16:01:49 +0200714 goto fail;
Michael Holzheu411ed322007-04-27 16:01:49 +0200715
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200716 rc = sys_info_init(arch, mem_end);
Frank Munzert12e0c952008-07-17 17:16:40 +0200717 if (rc)
718 goto fail;
Heiko Carstens7b1e4272013-04-27 13:43:34 +0200719 zcore_header_init(arch, &zcore_header, mem_size);
Michael Holzheu411ed322007-04-27 16:01:49 +0200720
Frank Munzert099b7652009-03-26 15:23:43 +0100721 rc = zcore_reipl_init();
722 if (rc)
723 goto fail;
724
Michael Holzheu411ed322007-04-27 16:01:49 +0200725 zcore_dir = debugfs_create_dir("zcore" , NULL);
726 if (!zcore_dir) {
727 rc = -ENOMEM;
728 goto fail;
729 }
730 zcore_file = debugfs_create_file("mem", S_IRUSR, zcore_dir, NULL,
731 &zcore_fops);
732 if (!zcore_file) {
Michael Holzheu411ed322007-04-27 16:01:49 +0200733 rc = -ENOMEM;
Frank Munzert12e0c952008-07-17 17:16:40 +0200734 goto fail_dir;
735 }
736 zcore_memmap_file = debugfs_create_file("memmap", S_IRUSR, zcore_dir,
737 NULL, &zcore_memmap_fops);
738 if (!zcore_memmap_file) {
739 rc = -ENOMEM;
740 goto fail_file;
Michael Holzheu411ed322007-04-27 16:01:49 +0200741 }
Frank Munzert099b7652009-03-26 15:23:43 +0100742 zcore_reipl_file = debugfs_create_file("reipl", S_IRUSR, zcore_dir,
743 NULL, &zcore_reipl_fops);
744 if (!zcore_reipl_file) {
745 rc = -ENOMEM;
746 goto fail_memmap_file;
747 }
Michael Holzheub4b3d122013-01-21 18:37:41 +0100748 zcore_hsa_file = debugfs_create_file("hsa", S_IRUSR|S_IWUSR, zcore_dir,
749 NULL, &zcore_hsa_fops);
750 if (!zcore_hsa_file) {
751 rc = -ENOMEM;
752 goto fail_reipl_file;
753 }
Michael Holzheu411ed322007-04-27 16:01:49 +0200754 return 0;
755
Michael Holzheub4b3d122013-01-21 18:37:41 +0100756fail_reipl_file:
757 debugfs_remove(zcore_reipl_file);
Frank Munzert099b7652009-03-26 15:23:43 +0100758fail_memmap_file:
759 debugfs_remove(zcore_memmap_file);
Frank Munzert12e0c952008-07-17 17:16:40 +0200760fail_file:
761 debugfs_remove(zcore_file);
762fail_dir:
763 debugfs_remove(zcore_dir);
Michael Holzheu411ed322007-04-27 16:01:49 +0200764fail:
765 diag308(DIAG308_REL_HSA, NULL);
766 return rc;
767}
768
Michael Holzheu411ed322007-04-27 16:01:49 +0200769static void __exit zcore_exit(void)
770{
771 debug_unregister(zcore_dbf);
Heiko Carstens763968e2007-05-10 15:45:46 +0200772 sclp_sdias_exit();
Frank Munzert099b7652009-03-26 15:23:43 +0100773 free_page((unsigned long) ipl_block);
Michael Holzheub4b3d122013-01-21 18:37:41 +0100774 debugfs_remove(zcore_hsa_file);
Frank Munzert099b7652009-03-26 15:23:43 +0100775 debugfs_remove(zcore_reipl_file);
776 debugfs_remove(zcore_memmap_file);
777 debugfs_remove(zcore_file);
778 debugfs_remove(zcore_dir);
Michael Holzheu411ed322007-04-27 16:01:49 +0200779 diag308(DIAG308_REL_HSA, NULL);
780}
781
Frank Munzert099b7652009-03-26 15:23:43 +0100782MODULE_AUTHOR("Copyright IBM Corp. 2003,2008");
Michael Holzheu411ed322007-04-27 16:01:49 +0200783MODULE_DESCRIPTION("zcore module for zfcpdump support");
784MODULE_LICENSE("GPL");
785
786subsys_initcall(zcore_init);
787module_exit(zcore_exit);