blob: 6165e6aae762a044150df20f19c7e1e8900b076d [file] [log] [blame]
Greg Kroah-Hartman6f05e692017-11-14 18:38:03 +01001// SPDX-License-Identifier: GPL-1.0+
Michael Holzheu411ed322007-04-27 16:01:49 +02002/*
3 * zcore module to export memory content and register sets for creating system
Alexander Egorenkovbd37b362020-09-29 20:24:55 +02004 * dumps on SCSI/NVMe disks (zfcp/nvme dump).
Michael Holzheu411ed322007-04-27 16:01:49 +02005 *
Mauro Carvalho Chehab8b4a5032019-06-08 23:27:16 -03006 * For more information please refer to Documentation/s390/zfcpdump.rst
Michael Holzheu411ed322007-04-27 16:01:49 +02007 *
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/debugfs.h>
Andy Shevchenkof39650d2021-06-30 18:54:59 -070018#include <linux/panic_notifier.h>
Alexander Egorenkovdabdfac2021-02-25 14:28:52 +010019#include <linux/reboot.h>
Alexander Gordeevebbc9572022-07-19 07:16:36 +020020#include <linux/uio.h>
Philipp Hachtmann50be6342014-01-29 18:16:01 +010021
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>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080026#include <linux/uaccess.h>
Michael Holzheu411ed322007-04-27 16:01:49 +020027#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>
Alexander Gordeev2f0e8aa2022-07-24 15:02:16 +020033#include <asm/maccess.h>
Heiko Carstens763968e2007-05-10 15:45:46 +020034#include "sclp.h"
Michael Holzheu411ed322007-04-27 16:01:49 +020035
36#define TRACE(x...) debug_sprintf_event(zcore_dbf, 1, x)
Michael Holzheu411ed322007-04-27 16:01:49 +020037
Michael Holzheu411ed322007-04-27 16:01:49 +020038enum arch_id {
39 ARCH_S390 = 0,
40 ARCH_S390X = 1,
41};
42
Frank Munzert099b7652009-03-26 15:23:43 +010043struct ipib_info {
44 unsigned long ipib;
45 u32 checksum;
46} __attribute__((packed));
47
Michael Holzheu411ed322007-04-27 16:01:49 +020048static struct debug_info *zcore_dbf;
49static int hsa_available;
50static struct dentry *zcore_dir;
Frank Munzert099b7652009-03-26 15:23:43 +010051static struct dentry *zcore_reipl_file;
Michael Holzheub4b3d122013-01-21 18:37:41 +010052static struct dentry *zcore_hsa_file;
Philipp Rudof3df44e72019-03-14 13:50:32 +010053static struct ipl_parameter_block *zcore_ipl_block;
Michael Holzheu411ed322007-04-27 16:01:49 +020054
Alexander Gordeev9ffed252022-07-19 07:16:33 +020055static DEFINE_MUTEX(hsa_buf_mutex);
Martin Schwidefsky019d6be2015-10-12 10:51:54 +020056static char hsa_buf[PAGE_SIZE] __aligned(PAGE_SIZE);
Michael Holzheu411ed322007-04-27 16:01:49 +020057
Martin Schwidefskydf9694c2015-10-12 10:43:37 +020058/*
Alexander Gordeevebbc9572022-07-19 07:16:36 +020059 * Copy memory from HSA to iterator (not reentrant):
Martin Schwidefskydf9694c2015-10-12 10:43:37 +020060 *
Alexander Gordeevebbc9572022-07-19 07:16:36 +020061 * @iter: Iterator where memory should be copied to
Martin Schwidefskydf9694c2015-10-12 10:43:37 +020062 * @src: Start address within HSA where data should be copied
63 * @count: Size of buffer, which should be copied
64 */
Alexander Gordeevebbc9572022-07-19 07:16:36 +020065size_t memcpy_hsa_iter(struct iov_iter *iter, unsigned long src, size_t count)
Michael Holzheu411ed322007-04-27 16:01:49 +020066{
Alexander Gordeevebbc9572022-07-19 07:16:36 +020067 size_t bytes, copied, res = 0;
68 unsigned long offset;
Martin Schwidefsky019d6be2015-10-12 10:51:54 +020069
70 if (!hsa_available)
Alexander Gordeevebbc9572022-07-19 07:16:36 +020071 return 0;
Martin Schwidefsky019d6be2015-10-12 10:51:54 +020072
Alexander Gordeev9ffed252022-07-19 07:16:33 +020073 mutex_lock(&hsa_buf_mutex);
Martin Schwidefsky019d6be2015-10-12 10:51:54 +020074 while (count) {
75 if (sclp_sdias_copy(hsa_buf, src / PAGE_SIZE + 2, 1)) {
76 TRACE("sclp_sdias_copy() failed\n");
Alexander Gordeevebbc9572022-07-19 07:16:36 +020077 break;
Martin Schwidefsky019d6be2015-10-12 10:51:54 +020078 }
79 offset = src % PAGE_SIZE;
80 bytes = min(PAGE_SIZE - offset, count);
Alexander Gordeevebbc9572022-07-19 07:16:36 +020081 copied = copy_to_iter(hsa_buf + offset, bytes, iter);
82 count -= copied;
83 src += copied;
84 res += copied;
85 if (copied < bytes)
86 break;
Martin Schwidefsky019d6be2015-10-12 10:51:54 +020087 }
Alexander Gordeev9ffed252022-07-19 07:16:33 +020088 mutex_unlock(&hsa_buf_mutex);
Alexander Gordeevebbc9572022-07-19 07:16:36 +020089 return res;
Michael Holzheu411ed322007-04-27 16:01:49 +020090}
91
Martin Schwidefskydf9694c2015-10-12 10:43:37 +020092/*
93 * Copy memory from HSA to kernel memory (not reentrant):
94 *
95 * @dest: Kernel or user buffer where memory should be copied to
96 * @src: Start address within HSA where data should be copied
97 * @count: Size of buffer, which should be copied
98 */
Alexander Gordeevebbc9572022-07-19 07:16:36 +020099static inline int memcpy_hsa_kernel(void *dst, unsigned long src, size_t count)
Michael Holzheu411ed322007-04-27 16:01:49 +0200100{
Alexander Gordeevebbc9572022-07-19 07:16:36 +0200101 struct iov_iter iter;
102 struct kvec kvec;
Martin Schwidefsky019d6be2015-10-12 10:51:54 +0200103
Alexander Gordeevebbc9572022-07-19 07:16:36 +0200104 kvec.iov_base = dst;
105 kvec.iov_len = count;
106 iov_iter_kvec(&iter, WRITE, &kvec, 1, count);
107 if (memcpy_hsa_iter(&iter, src, count) < count)
108 return -EIO;
Martin Schwidefsky019d6be2015-10-12 10:51:54 +0200109 return 0;
Michael Holzheu411ed322007-04-27 16:01:49 +0200110}
111
Martin Schwidefskyffa52d02015-10-28 09:47:58 +0100112static int __init init_cpu_info(void)
Michael Holzheu411ed322007-04-27 16:01:49 +0200113{
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100114 struct save_area *sa;
Michael Holzheu411ed322007-04-27 16:01:49 +0200115
116 /* get info for boot cpu from lowcore, stored in the HSA */
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100117 sa = save_area_boot_cpu();
118 if (!sa)
Michael Holzheu411ed322007-04-27 16:01:49 +0200119 return -ENOMEM;
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100120 if (memcpy_hsa_kernel(hsa_buf, __LC_FPREGS_SAVE_AREA, 512) < 0) {
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200121 TRACE("could not copy from HSA\n");
Michael Holzheu411ed322007-04-27 16:01:49 +0200122 return -EIO;
123 }
Martin Schwidefsky1a2c5842015-10-29 10:59:15 +0100124 save_area_add_regs(sa, hsa_buf); /* vx registers are saved in smp.c */
Michael Holzheu411ed322007-04-27 16:01:49 +0200125 return 0;
126}
127
128/*
Michael Holzheub4b3d122013-01-21 18:37:41 +0100129 * Release the HSA
130 */
131static void release_hsa(void)
132{
133 diag308(DIAG308_REL_HSA, NULL);
134 hsa_available = 0;
135}
136
Frank Munzert099b7652009-03-26 15:23:43 +0100137static ssize_t zcore_reipl_write(struct file *filp, const char __user *buf,
138 size_t count, loff_t *ppos)
139{
Philipp Rudof3df44e72019-03-14 13:50:32 +0100140 if (zcore_ipl_block) {
141 diag308(DIAG308_SET, zcore_ipl_block);
Heiko Carstens0599eea2016-06-11 11:24:27 +0200142 diag308(DIAG308_LOAD_CLEAR, NULL);
Frank Munzert099b7652009-03-26 15:23:43 +0100143 }
144 return count;
145}
146
147static int zcore_reipl_open(struct inode *inode, struct file *filp)
148{
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300149 return stream_open(inode, filp);
Frank Munzert099b7652009-03-26 15:23:43 +0100150}
151
152static int zcore_reipl_release(struct inode *inode, struct file *filp)
153{
154 return 0;
155}
156
157static const struct file_operations zcore_reipl_fops = {
158 .owner = THIS_MODULE,
159 .write = zcore_reipl_write,
160 .open = zcore_reipl_open,
161 .release = zcore_reipl_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200162 .llseek = no_llseek,
Frank Munzert099b7652009-03-26 15:23:43 +0100163};
164
Michael Holzheub4b3d122013-01-21 18:37:41 +0100165static ssize_t zcore_hsa_read(struct file *filp, char __user *buf,
166 size_t count, loff_t *ppos)
167{
168 static char str[18];
169
170 if (hsa_available)
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200171 snprintf(str, sizeof(str), "%lx\n", sclp.hsa_size);
Michael Holzheub4b3d122013-01-21 18:37:41 +0100172 else
173 snprintf(str, sizeof(str), "0\n");
174 return simple_read_from_buffer(buf, count, ppos, str, strlen(str));
175}
176
177static ssize_t zcore_hsa_write(struct file *filp, const char __user *buf,
178 size_t count, loff_t *ppos)
179{
180 char value;
181
182 if (*ppos != 0)
183 return -EPIPE;
184 if (copy_from_user(&value, buf, 1))
185 return -EFAULT;
186 if (value != '0')
187 return -EINVAL;
188 release_hsa();
189 return count;
190}
191
192static const struct file_operations zcore_hsa_fops = {
193 .owner = THIS_MODULE,
194 .write = zcore_hsa_write,
195 .read = zcore_hsa_read,
196 .open = nonseekable_open,
197 .llseek = no_llseek,
198};
199
Michael Holzheu411ed322007-04-27 16:01:49 +0200200static int __init check_sdias(void)
201{
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200202 if (!sclp.hsa_size) {
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200203 TRACE("Could not determine HSA size\n");
Michael Holzheue657d8f2013-11-13 10:38:27 +0100204 return -ENODEV;
Michael Holzheu411ed322007-04-27 16:01:49 +0200205 }
206 return 0;
207}
208
Frank Munzert099b7652009-03-26 15:23:43 +0100209/*
210 * Provide IPL parameter information block from either HSA or memory
211 * for future reipl
212 */
213static int __init zcore_reipl_init(void)
214{
215 struct ipib_info ipib_info;
216 int rc;
217
218 rc = memcpy_hsa_kernel(&ipib_info, __LC_DUMP_REIPL, sizeof(ipib_info));
219 if (rc)
220 return rc;
221 if (ipib_info.ipib == 0)
222 return 0;
Philipp Rudof3df44e72019-03-14 13:50:32 +0100223 zcore_ipl_block = (void *) __get_free_page(GFP_KERNEL);
224 if (!zcore_ipl_block)
Frank Munzert099b7652009-03-26 15:23:43 +0100225 return -ENOMEM;
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200226 if (ipib_info.ipib < sclp.hsa_size)
Philipp Rudof3df44e72019-03-14 13:50:32 +0100227 rc = memcpy_hsa_kernel(zcore_ipl_block, ipib_info.ipib,
228 PAGE_SIZE);
Frank Munzert099b7652009-03-26 15:23:43 +0100229 else
Alexander Gordeev303fd982022-01-29 09:24:50 +0100230 rc = memcpy_real(zcore_ipl_block, ipib_info.ipib, PAGE_SIZE);
Philipp Rudof3df44e72019-03-14 13:50:32 +0100231 if (rc || (__force u32)csum_partial(zcore_ipl_block, zcore_ipl_block->hdr.len, 0) !=
Frank Munzert159d1ff2009-03-26 15:24:45 +0100232 ipib_info.checksum) {
Frank Munzert099b7652009-03-26 15:23:43 +0100233 TRACE("Checksum does not match\n");
Philipp Rudof3df44e72019-03-14 13:50:32 +0100234 free_page((unsigned long) zcore_ipl_block);
235 zcore_ipl_block = NULL;
Frank Munzert099b7652009-03-26 15:23:43 +0100236 }
237 return 0;
238}
239
Alexander Egorenkovdabdfac2021-02-25 14:28:52 +0100240static int zcore_reboot_and_on_panic_handler(struct notifier_block *self,
241 unsigned long event,
242 void *data)
243{
244 if (hsa_available)
245 release_hsa();
246
247 return NOTIFY_OK;
248}
249
250static struct notifier_block zcore_reboot_notifier = {
251 .notifier_call = zcore_reboot_and_on_panic_handler,
252 /* we need to be notified before reipl and kdump */
253 .priority = INT_MAX,
254};
255
256static struct notifier_block zcore_on_panic_notifier = {
257 .notifier_call = zcore_reboot_and_on_panic_handler,
258 /* we need to be notified before reipl and kdump */
259 .priority = INT_MAX,
260};
261
Michael Holzheu411ed322007-04-27 16:01:49 +0200262static int __init zcore_init(void)
263{
264 unsigned char arch;
265 int rc;
266
Alexander Egorenkovbd37b362020-09-29 20:24:55 +0200267 if (!is_ipl_type_dump())
Michael Holzheu411ed322007-04-27 16:01:49 +0200268 return -ENODATA;
Alexander Egorenkove9e78702021-06-15 14:25:41 +0200269 if (oldmem_data.start)
Michael Holzheu3f25dc42011-11-14 11:19:05 +0100270 return -ENODATA;
Michael Holzheu411ed322007-04-27 16:01:49 +0200271
272 zcore_dbf = debug_register("zcore", 4, 1, 4 * sizeof(long));
273 debug_register_view(zcore_dbf, &debug_sprintf_view);
274 debug_set_level(zcore_dbf, 6);
275
Alexander Egorenkovbd37b362020-09-29 20:24:55 +0200276 if (ipl_info.type == IPL_TYPE_FCP_DUMP) {
277 TRACE("type: fcp\n");
278 TRACE("devno: %x\n", ipl_info.data.fcp.dev_id.devno);
279 TRACE("wwpn: %llx\n", (unsigned long long) ipl_info.data.fcp.wwpn);
280 TRACE("lun: %llx\n", (unsigned long long) ipl_info.data.fcp.lun);
281 } else if (ipl_info.type == IPL_TYPE_NVME_DUMP) {
282 TRACE("type: nvme\n");
283 TRACE("fid: %x\n", ipl_info.data.nvme.fid);
284 TRACE("nsid: %x\n", ipl_info.data.nvme.nsid);
285 }
Michael Holzheu411ed322007-04-27 16:01:49 +0200286
Heiko Carstens763968e2007-05-10 15:45:46 +0200287 rc = sclp_sdias_init();
Michael Holzheu411ed322007-04-27 16:01:49 +0200288 if (rc)
289 goto fail;
290
291 rc = check_sdias();
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200292 if (rc)
Michael Holzheu411ed322007-04-27 16:01:49 +0200293 goto fail;
Michael Holzheub4b3d122013-01-21 18:37:41 +0100294 hsa_available = 1;
Michael Holzheu411ed322007-04-27 16:01:49 +0200295
296 rc = memcpy_hsa_kernel(&arch, __LC_AR_MODE_ID, 1);
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200297 if (rc)
Michael Holzheu411ed322007-04-27 16:01:49 +0200298 goto fail;
Michael Holzheu411ed322007-04-27 16:01:49 +0200299
Heiko Carstensf64ca212010-02-26 22:37:32 +0100300 if (arch == ARCH_S390) {
301 pr_alert("The 64-bit dump tool cannot be used for a "
302 "32-bit system\n");
303 rc = -EINVAL;
304 goto fail;
305 }
Michael Holzheu411ed322007-04-27 16:01:49 +0200306
Michael Holzheu68962262016-11-14 18:51:41 +0100307 pr_alert("The dump process started for a 64-bit operating system\n");
Martin Schwidefskyffa52d02015-10-28 09:47:58 +0100308 rc = init_cpu_info();
Michael Holzheu2a062ab2008-07-14 09:59:38 +0200309 if (rc)
Michael Holzheu411ed322007-04-27 16:01:49 +0200310 goto fail;
Michael Holzheu411ed322007-04-27 16:01:49 +0200311
Frank Munzert099b7652009-03-26 15:23:43 +0100312 rc = zcore_reipl_init();
313 if (rc)
314 goto fail;
315
Michael Holzheu411ed322007-04-27 16:01:49 +0200316 zcore_dir = debugfs_create_dir("zcore" , NULL);
Frank Munzert099b7652009-03-26 15:23:43 +0100317 zcore_reipl_file = debugfs_create_file("reipl", S_IRUSR, zcore_dir,
318 NULL, &zcore_reipl_fops);
Michael Holzheub4b3d122013-01-21 18:37:41 +0100319 zcore_hsa_file = debugfs_create_file("hsa", S_IRUSR|S_IWUSR, zcore_dir,
320 NULL, &zcore_hsa_fops);
Michael Holzheu411ed322007-04-27 16:01:49 +0200321
Alexander Egorenkovdabdfac2021-02-25 14:28:52 +0100322 register_reboot_notifier(&zcore_reboot_notifier);
323 atomic_notifier_chain_register(&panic_notifier_list, &zcore_on_panic_notifier);
324
Alexander Egorenkov7449ca82021-02-26 11:21:05 +0100325 return 0;
Michael Holzheu411ed322007-04-27 16:01:49 +0200326fail:
327 diag308(DIAG308_REL_HSA, NULL);
328 return rc;
329}
Michael Holzheu411ed322007-04-27 16:01:49 +0200330subsys_initcall(zcore_init);