Merge branch 'next' into for-linus
[linux-2.6-microblaze.git] / drivers / s390 / char / zcore.c
1 // SPDX-License-Identifier: GPL-1.0+
2 /*
3  * zcore module to export memory content and register sets for creating system
4  * dumps on SCSI/NVMe disks (zfcp/nvme dump).
5  *
6  * For more information please refer to Documentation/s390/zfcpdump.rst
7  *
8  * Copyright IBM Corp. 2003, 2008
9  * Author(s): Michael Holzheu
10  */
11
12 #define KMSG_COMPONENT "zdump"
13 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
14
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/debugfs.h>
18 #include <linux/reboot.h>
19
20 #include <asm/asm-offsets.h>
21 #include <asm/ipl.h>
22 #include <asm/sclp.h>
23 #include <asm/setup.h>
24 #include <linux/uaccess.h>
25 #include <asm/debug.h>
26 #include <asm/processor.h>
27 #include <asm/irqflags.h>
28 #include <asm/checksum.h>
29 #include <asm/os_info.h>
30 #include <asm/switch_to.h>
31 #include "sclp.h"
32
33 #define TRACE(x...) debug_sprintf_event(zcore_dbf, 1, x)
34
35 enum arch_id {
36         ARCH_S390       = 0,
37         ARCH_S390X      = 1,
38 };
39
40 struct ipib_info {
41         unsigned long   ipib;
42         u32             checksum;
43 }  __attribute__((packed));
44
45 static struct debug_info *zcore_dbf;
46 static int hsa_available;
47 static struct dentry *zcore_dir;
48 static struct dentry *zcore_reipl_file;
49 static struct dentry *zcore_hsa_file;
50 static struct ipl_parameter_block *zcore_ipl_block;
51
52 static char hsa_buf[PAGE_SIZE] __aligned(PAGE_SIZE);
53
54 /*
55  * Copy memory from HSA to user memory (not reentrant):
56  *
57  * @dest:  User buffer where memory should be copied to
58  * @src:   Start address within HSA where data should be copied
59  * @count: Size of buffer, which should be copied
60  */
61 int memcpy_hsa_user(void __user *dest, unsigned long src, size_t count)
62 {
63         unsigned long offset, bytes;
64
65         if (!hsa_available)
66                 return -ENODATA;
67
68         while (count) {
69                 if (sclp_sdias_copy(hsa_buf, src / PAGE_SIZE + 2, 1)) {
70                         TRACE("sclp_sdias_copy() failed\n");
71                         return -EIO;
72                 }
73                 offset = src % PAGE_SIZE;
74                 bytes = min(PAGE_SIZE - offset, count);
75                 if (copy_to_user(dest, hsa_buf + offset, bytes))
76                         return -EFAULT;
77                 src += bytes;
78                 dest += bytes;
79                 count -= bytes;
80         }
81         return 0;
82 }
83
84 /*
85  * Copy memory from HSA to kernel memory (not reentrant):
86  *
87  * @dest:  Kernel or user buffer where memory should be copied to
88  * @src:   Start address within HSA where data should be copied
89  * @count: Size of buffer, which should be copied
90  */
91 int memcpy_hsa_kernel(void *dest, unsigned long src, size_t count)
92 {
93         unsigned long offset, bytes;
94
95         if (!hsa_available)
96                 return -ENODATA;
97
98         while (count) {
99                 if (sclp_sdias_copy(hsa_buf, src / PAGE_SIZE + 2, 1)) {
100                         TRACE("sclp_sdias_copy() failed\n");
101                         return -EIO;
102                 }
103                 offset = src % PAGE_SIZE;
104                 bytes = min(PAGE_SIZE - offset, count);
105                 memcpy(dest, hsa_buf + offset, bytes);
106                 src += bytes;
107                 dest += bytes;
108                 count -= bytes;
109         }
110         return 0;
111 }
112
113 static int __init init_cpu_info(void)
114 {
115         struct save_area *sa;
116
117         /* get info for boot cpu from lowcore, stored in the HSA */
118         sa = save_area_boot_cpu();
119         if (!sa)
120                 return -ENOMEM;
121         if (memcpy_hsa_kernel(hsa_buf, __LC_FPREGS_SAVE_AREA, 512) < 0) {
122                 TRACE("could not copy from HSA\n");
123                 return -EIO;
124         }
125         save_area_add_regs(sa, hsa_buf); /* vx registers are saved in smp.c */
126         return 0;
127 }
128
129 /*
130  * Release the HSA
131  */
132 static void release_hsa(void)
133 {
134         diag308(DIAG308_REL_HSA, NULL);
135         hsa_available = 0;
136 }
137
138 static ssize_t zcore_reipl_write(struct file *filp, const char __user *buf,
139                                  size_t count, loff_t *ppos)
140 {
141         if (zcore_ipl_block) {
142                 diag308(DIAG308_SET, zcore_ipl_block);
143                 diag308(DIAG308_LOAD_CLEAR, NULL);
144         }
145         return count;
146 }
147
148 static int zcore_reipl_open(struct inode *inode, struct file *filp)
149 {
150         return stream_open(inode, filp);
151 }
152
153 static int zcore_reipl_release(struct inode *inode, struct file *filp)
154 {
155         return 0;
156 }
157
158 static const struct file_operations zcore_reipl_fops = {
159         .owner          = THIS_MODULE,
160         .write          = zcore_reipl_write,
161         .open           = zcore_reipl_open,
162         .release        = zcore_reipl_release,
163         .llseek         = no_llseek,
164 };
165
166 static ssize_t zcore_hsa_read(struct file *filp, char __user *buf,
167                               size_t count, loff_t *ppos)
168 {
169         static char str[18];
170
171         if (hsa_available)
172                 snprintf(str, sizeof(str), "%lx\n", sclp.hsa_size);
173         else
174                 snprintf(str, sizeof(str), "0\n");
175         return simple_read_from_buffer(buf, count, ppos, str, strlen(str));
176 }
177
178 static ssize_t zcore_hsa_write(struct file *filp, const char __user *buf,
179                                size_t count, loff_t *ppos)
180 {
181         char value;
182
183         if (*ppos != 0)
184                 return -EPIPE;
185         if (copy_from_user(&value, buf, 1))
186                 return -EFAULT;
187         if (value != '0')
188                 return -EINVAL;
189         release_hsa();
190         return count;
191 }
192
193 static const struct file_operations zcore_hsa_fops = {
194         .owner          = THIS_MODULE,
195         .write          = zcore_hsa_write,
196         .read           = zcore_hsa_read,
197         .open           = nonseekable_open,
198         .llseek         = no_llseek,
199 };
200
201 static int __init check_sdias(void)
202 {
203         if (!sclp.hsa_size) {
204                 TRACE("Could not determine HSA size\n");
205                 return -ENODEV;
206         }
207         return 0;
208 }
209
210 /*
211  * Provide IPL parameter information block from either HSA or memory
212  * for future reipl
213  */
214 static int __init zcore_reipl_init(void)
215 {
216         struct ipib_info ipib_info;
217         int rc;
218
219         rc = memcpy_hsa_kernel(&ipib_info, __LC_DUMP_REIPL, sizeof(ipib_info));
220         if (rc)
221                 return rc;
222         if (ipib_info.ipib == 0)
223                 return 0;
224         zcore_ipl_block = (void *) __get_free_page(GFP_KERNEL);
225         if (!zcore_ipl_block)
226                 return -ENOMEM;
227         if (ipib_info.ipib < sclp.hsa_size)
228                 rc = memcpy_hsa_kernel(zcore_ipl_block, ipib_info.ipib,
229                                        PAGE_SIZE);
230         else
231                 rc = memcpy_real(zcore_ipl_block, (void *) ipib_info.ipib,
232                                  PAGE_SIZE);
233         if (rc || (__force u32)csum_partial(zcore_ipl_block, zcore_ipl_block->hdr.len, 0) !=
234             ipib_info.checksum) {
235                 TRACE("Checksum does not match\n");
236                 free_page((unsigned long) zcore_ipl_block);
237                 zcore_ipl_block = NULL;
238         }
239         return 0;
240 }
241
242 static int zcore_reboot_and_on_panic_handler(struct notifier_block *self,
243                                              unsigned long         event,
244                                              void                  *data)
245 {
246         if (hsa_available)
247                 release_hsa();
248
249         return NOTIFY_OK;
250 }
251
252 static struct notifier_block zcore_reboot_notifier = {
253         .notifier_call  = zcore_reboot_and_on_panic_handler,
254         /* we need to be notified before reipl and kdump */
255         .priority       = INT_MAX,
256 };
257
258 static struct notifier_block zcore_on_panic_notifier = {
259         .notifier_call  = zcore_reboot_and_on_panic_handler,
260         /* we need to be notified before reipl and kdump */
261         .priority       = INT_MAX,
262 };
263
264 static int __init zcore_init(void)
265 {
266         unsigned char arch;
267         int rc;
268
269         if (!is_ipl_type_dump())
270                 return -ENODATA;
271         if (OLDMEM_BASE)
272                 return -ENODATA;
273
274         zcore_dbf = debug_register("zcore", 4, 1, 4 * sizeof(long));
275         debug_register_view(zcore_dbf, &debug_sprintf_view);
276         debug_set_level(zcore_dbf, 6);
277
278         if (ipl_info.type == IPL_TYPE_FCP_DUMP) {
279                 TRACE("type:   fcp\n");
280                 TRACE("devno:  %x\n", ipl_info.data.fcp.dev_id.devno);
281                 TRACE("wwpn:   %llx\n", (unsigned long long) ipl_info.data.fcp.wwpn);
282                 TRACE("lun:    %llx\n", (unsigned long long) ipl_info.data.fcp.lun);
283         } else if (ipl_info.type == IPL_TYPE_NVME_DUMP) {
284                 TRACE("type:   nvme\n");
285                 TRACE("fid:    %x\n", ipl_info.data.nvme.fid);
286                 TRACE("nsid:   %x\n", ipl_info.data.nvme.nsid);
287         }
288
289         rc = sclp_sdias_init();
290         if (rc)
291                 goto fail;
292
293         rc = check_sdias();
294         if (rc)
295                 goto fail;
296         hsa_available = 1;
297
298         rc = memcpy_hsa_kernel(&arch, __LC_AR_MODE_ID, 1);
299         if (rc)
300                 goto fail;
301
302         if (arch == ARCH_S390) {
303                 pr_alert("The 64-bit dump tool cannot be used for a "
304                          "32-bit system\n");
305                 rc = -EINVAL;
306                 goto fail;
307         }
308
309         pr_alert("The dump process started for a 64-bit operating system\n");
310         rc = init_cpu_info();
311         if (rc)
312                 goto fail;
313
314         rc = zcore_reipl_init();
315         if (rc)
316                 goto fail;
317
318         zcore_dir = debugfs_create_dir("zcore" , NULL);
319         zcore_reipl_file = debugfs_create_file("reipl", S_IRUSR, zcore_dir,
320                                                 NULL, &zcore_reipl_fops);
321         zcore_hsa_file = debugfs_create_file("hsa", S_IRUSR|S_IWUSR, zcore_dir,
322                                              NULL, &zcore_hsa_fops);
323
324         register_reboot_notifier(&zcore_reboot_notifier);
325         atomic_notifier_chain_register(&panic_notifier_list, &zcore_on_panic_notifier);
326
327         return 0;
328 fail:
329         diag308(DIAG308_REL_HSA, NULL);
330         return rc;
331 }
332 subsys_initcall(zcore_init);