blob: f99e55308b32df8625658de3f63c1fddf5010236 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/s390mach.c
3 * S/390 machine check handler
4 *
5 * S390 version
6 * Copyright (C) 2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Ingo Adlung (adlung@de.ibm.com)
8 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 */
10
11#include <linux/config.h>
12#include <linux/init.h>
13#include <linux/sched.h>
14#include <linux/errno.h>
15#include <linux/workqueue.h>
Heiko Carstens022e4fc2006-05-01 12:16:14 -070016#include <linux/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <asm/lowcore.h>
19
20#include "s390mach.h"
21
22#define DBG printk
23// #define DBG(args,...) do {} while (0);
24
25static struct semaphore m_sem;
26
Cornelia Huckfb6958a2006-01-06 00:19:25 -080027extern int css_process_crw(int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028extern int chsc_process_crw(void);
29extern int chp_process_crw(int, int);
30extern void css_reiterate_subchannels(void);
31
32extern struct workqueue_struct *slow_path_wq;
33extern struct work_struct slow_path_work;
34
Heiko Carstens77fa2242005-06-25 14:55:30 -070035static NORET_TYPE void
Linus Torvalds1da177e2005-04-16 15:20:36 -070036s390_handle_damage(char *msg)
37{
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#ifdef CONFIG_SMP
39 smp_send_stop();
40#endif
41 disabled_wait((unsigned long) __builtin_return_address(0));
Heiko Carstens77fa2242005-06-25 14:55:30 -070042 for(;;);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
45/*
46 * Retrieve CRWs and call function to handle event.
47 *
48 * Note : we currently process CRWs for io and chsc subchannels only
49 */
50static int
51s390_collect_crw_info(void *param)
52{
Cornelia Huckfb6958a2006-01-06 00:19:25 -080053 struct crw crw[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 int ccode, ret, slow;
55 struct semaphore *sem;
Cornelia Huckfb6958a2006-01-06 00:19:25 -080056 unsigned int chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 sem = (struct semaphore *)param;
59 /* Set a nice name. */
60 daemonize("kmcheck");
61repeat:
62 down_interruptible(sem);
63 slow = 0;
Cornelia Huckfb6958a2006-01-06 00:19:25 -080064 chain = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 while (1) {
Cornelia Huckfb6958a2006-01-06 00:19:25 -080066 if (unlikely(chain > 1)) {
67 struct crw tmp_crw;
68
69 printk(KERN_WARNING"%s: Code does not support more "
70 "than two chained crws; please report to "
71 "linux390@de.ibm.com!\n", __FUNCTION__);
72 ccode = stcrw(&tmp_crw);
73 printk(KERN_WARNING"%s: crw reports slct=%d, oflw=%d, "
74 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
75 __FUNCTION__, tmp_crw.slct, tmp_crw.oflw,
76 tmp_crw.chn, tmp_crw.rsc, tmp_crw.anc,
77 tmp_crw.erc, tmp_crw.rsid);
78 printk(KERN_WARNING"%s: This was crw number %x in the "
79 "chain\n", __FUNCTION__, chain);
80 if (ccode != 0)
81 break;
82 chain = tmp_crw.chn ? chain + 1 : 0;
83 continue;
84 }
85 ccode = stcrw(&crw[chain]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (ccode != 0)
87 break;
88 DBG(KERN_DEBUG "crw_info : CRW reports slct=%d, oflw=%d, "
89 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
Cornelia Huckfb6958a2006-01-06 00:19:25 -080090 crw[chain].slct, crw[chain].oflw, crw[chain].chn,
91 crw[chain].rsc, crw[chain].anc, crw[chain].erc,
92 crw[chain].rsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 /* Check for overflows. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -080094 if (crw[chain].oflw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 pr_debug("%s: crw overflow detected!\n", __FUNCTION__);
96 css_reiterate_subchannels();
Cornelia Huckfb6958a2006-01-06 00:19:25 -080097 chain = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 slow = 1;
99 continue;
100 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800101 switch (crw[chain].rsc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 case CRW_RSC_SCH:
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800103 if (crw[0].chn && !chain)
104 break;
105 pr_debug("source is subchannel %04X\n", crw[0].rsid);
106 ret = css_process_crw (crw[0].rsid,
107 chain ? crw[1].rsid : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 if (ret == -EAGAIN)
109 slow = 1;
110 break;
111 case CRW_RSC_MONITOR:
112 pr_debug("source is monitoring facility\n");
113 break;
114 case CRW_RSC_CPATH:
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800115 pr_debug("source is channel path %02X\n", crw[0].rsid);
116 switch (crw[0].erc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 case CRW_ERC_IPARM: /* Path has come. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800118 ret = chp_process_crw(crw[0].rsid, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 break;
120 case CRW_ERC_PERRI: /* Path has gone. */
121 case CRW_ERC_PERRN:
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800122 ret = chp_process_crw(crw[0].rsid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 break;
124 default:
125 pr_debug("Don't know how to handle erc=%x\n",
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800126 crw[0].erc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 ret = 0;
128 }
129 if (ret == -EAGAIN)
130 slow = 1;
131 break;
132 case CRW_RSC_CONFIG:
133 pr_debug("source is configuration-alert facility\n");
134 break;
135 case CRW_RSC_CSS:
136 pr_debug("source is channel subsystem\n");
137 ret = chsc_process_crw();
138 if (ret == -EAGAIN)
139 slow = 1;
140 break;
141 default:
142 pr_debug("unknown source\n");
143 break;
144 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800145 /* chain is always 0 or 1 here. */
146 chain = crw[chain].chn ? chain + 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148 if (slow)
149 queue_work(slow_path_wq, &slow_path_work);
150 goto repeat;
151 return 0;
152}
153
Heiko Carstens77fa2242005-06-25 14:55:30 -0700154struct mcck_struct {
155 int kill_task;
156 int channel_report;
157 int warning;
158 unsigned long long mcck_code;
159};
160
161static DEFINE_PER_CPU(struct mcck_struct, cpu_mcck);
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/*
Heiko Carstens77fa2242005-06-25 14:55:30 -0700164 * Main machine check handler function. Will be called with interrupts enabled
165 * or disabled and machine checks enabled or disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 */
167void
Heiko Carstens77fa2242005-06-25 14:55:30 -0700168s390_handle_mcck(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Heiko Carstens77fa2242005-06-25 14:55:30 -0700170 unsigned long flags;
171 struct mcck_struct mcck;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Heiko Carstens77fa2242005-06-25 14:55:30 -0700173 /*
174 * Disable machine checks and get the current state of accumulated
175 * machine checks. Afterwards delete the old state and enable machine
176 * checks again.
177 */
178 local_irq_save(flags);
179 local_mcck_disable();
180 mcck = __get_cpu_var(cpu_mcck);
181 memset(&__get_cpu_var(cpu_mcck), 0, sizeof(struct mcck_struct));
182 clear_thread_flag(TIF_MCCK_PENDING);
183 local_mcck_enable();
184 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Heiko Carstens77fa2242005-06-25 14:55:30 -0700186 if (mcck.channel_report)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 up(&m_sem);
188
189#ifdef CONFIG_MACHCHK_WARNING
190/*
191 * The warning may remain for a prolonged period on the bare iron.
192 * (actually till the machine is powered off, or until the problem is gone)
193 * So we just stop listening for the WARNING MCH and prevent continuously
194 * being interrupted. One caveat is however, that we must do this per
195 * processor and cannot use the smp version of ctl_clear_bit().
196 * On VM we only get one interrupt per virtally presented machinecheck.
197 * Though one suffices, we may get one interrupt per (virtual) processor.
198 */
Heiko Carstens77fa2242005-06-25 14:55:30 -0700199 if (mcck.warning) { /* WARNING pending ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 static int mchchk_wng_posted = 0;
201 /*
202 * Use single machine clear, as we cannot handle smp right now
203 */
204 __ctl_clear_bit(14, 24); /* Disable WARNING MCH */
205 if (xchg(&mchchk_wng_posted, 1) == 0)
206 kill_proc(1, SIGPWR, 1);
207 }
208#endif
Heiko Carstens77fa2242005-06-25 14:55:30 -0700209
210 if (mcck.kill_task) {
211 local_irq_enable();
212 printk(KERN_EMERG "mcck: Terminating task because of machine "
213 "malfunction (code 0x%016llx).\n", mcck.mcck_code);
214 printk(KERN_EMERG "mcck: task: %s, pid: %d.\n",
215 current->comm, current->pid);
216 do_exit(SIGSEGV);
217 }
218}
219
220/*
221 * returns 0 if all registers could be validated
222 * returns 1 otherwise
223 */
224static int
225s390_revalidate_registers(struct mci *mci)
226{
227 int kill_task;
228 u64 tmpclock;
229 u64 zero;
230 void *fpt_save_area, *fpt_creg_save_area;
231
232 kill_task = 0;
233 zero = 0;
234 /* General purpose registers */
235 if (!mci->gr)
236 /*
237 * General purpose registers couldn't be restored and have
238 * unknown contents. Process needs to be terminated.
239 */
240 kill_task = 1;
241
242 /* Revalidate floating point registers */
243 if (!mci->fp)
244 /*
245 * Floating point registers can't be restored and
246 * therefore the process needs to be terminated.
247 */
248 kill_task = 1;
249
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800250#ifndef CONFIG_64BIT
Heiko Carstens77fa2242005-06-25 14:55:30 -0700251 asm volatile("ld 0,0(%0)\n"
252 "ld 2,8(%0)\n"
253 "ld 4,16(%0)\n"
254 "ld 6,24(%0)"
255 : : "a" (&S390_lowcore.floating_pt_save_area));
256#endif
257
258 if (MACHINE_HAS_IEEE) {
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800259#ifdef CONFIG_64BIT
Heiko Carstens77fa2242005-06-25 14:55:30 -0700260 fpt_save_area = &S390_lowcore.floating_pt_save_area;
261 fpt_creg_save_area = &S390_lowcore.fpt_creg_save_area;
262#else
263 fpt_save_area = (void *) S390_lowcore.extended_save_area_addr;
264 fpt_creg_save_area = fpt_save_area+128;
265#endif
266 /* Floating point control register */
267 if (!mci->fc) {
268 /*
269 * Floating point control register can't be restored.
270 * Task will be terminated.
271 */
Martin Schwidefskyae6aa2e2005-09-03 15:57:56 -0700272 asm volatile ("lfpc 0(%0)" : : "a" (&zero), "m" (zero));
Heiko Carstens77fa2242005-06-25 14:55:30 -0700273 kill_task = 1;
274
275 }
276 else
277 asm volatile (
278 "lfpc 0(%0)"
279 : : "a" (fpt_creg_save_area));
280
281 asm volatile("ld 0,0(%0)\n"
282 "ld 1,8(%0)\n"
283 "ld 2,16(%0)\n"
284 "ld 3,24(%0)\n"
285 "ld 4,32(%0)\n"
286 "ld 5,40(%0)\n"
287 "ld 6,48(%0)\n"
288 "ld 7,56(%0)\n"
289 "ld 8,64(%0)\n"
290 "ld 9,72(%0)\n"
291 "ld 10,80(%0)\n"
292 "ld 11,88(%0)\n"
293 "ld 12,96(%0)\n"
294 "ld 13,104(%0)\n"
295 "ld 14,112(%0)\n"
296 "ld 15,120(%0)\n"
297 : : "a" (fpt_save_area));
298 }
299
300 /* Revalidate access registers */
301 asm volatile("lam 0,15,0(%0)"
302 : : "a" (&S390_lowcore.access_regs_save_area));
303 if (!mci->ar)
304 /*
305 * Access registers have unknown contents.
306 * Terminating task.
307 */
308 kill_task = 1;
309
310 /* Revalidate control registers */
311 if (!mci->cr)
312 /*
313 * Control registers have unknown contents.
314 * Can't recover and therefore stopping machine.
315 */
316 s390_handle_damage("invalid control registers.");
317 else
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800318#ifdef CONFIG_64BIT
Heiko Carstens77fa2242005-06-25 14:55:30 -0700319 asm volatile("lctlg 0,15,0(%0)"
320 : : "a" (&S390_lowcore.cregs_save_area));
321#else
322 asm volatile("lctl 0,15,0(%0)"
323 : : "a" (&S390_lowcore.cregs_save_area));
324#endif
325
326 /*
327 * We don't even try to revalidate the TOD register, since we simply
328 * can't write something sensible into that register.
329 */
330
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800331#ifdef CONFIG_64BIT
Heiko Carstens77fa2242005-06-25 14:55:30 -0700332 /*
333 * See if we can revalidate the TOD programmable register with its
334 * old contents (should be zero) otherwise set it to zero.
335 */
336 if (!mci->pr)
337 asm volatile("sr 0,0\n"
338 "sckpf"
339 : : : "0", "cc");
340 else
341 asm volatile(
342 "l 0,0(%0)\n"
343 "sckpf"
344 : : "a" (&S390_lowcore.tod_progreg_save_area) : "0", "cc");
345#endif
346
347 /* Revalidate clock comparator register */
348 asm volatile ("stck 0(%1)\n"
349 "sckc 0(%1)"
350 : "=m" (tmpclock) : "a" (&(tmpclock)) : "cc", "memory");
351
352 /* Check if old PSW is valid */
353 if (!mci->wp)
354 /*
355 * Can't tell if we come from user or kernel mode
356 * -> stopping machine.
357 */
358 s390_handle_damage("old psw invalid.");
359
360 if (!mci->ms || !mci->pm || !mci->ia)
361 kill_task = 1;
362
363 return kill_task;
364}
365
Heiko Carstensb73d40c2006-04-27 18:40:23 -0700366#define MAX_IPD_COUNT 29
Heiko Carstens022e4fc2006-05-01 12:16:14 -0700367#define MAX_IPD_TIME (5 * 60 * USEC_PER_SEC) /* 5 minutes */
Heiko Carstensb73d40c2006-04-27 18:40:23 -0700368
Heiko Carstens77fa2242005-06-25 14:55:30 -0700369/*
370 * machine check handler.
371 */
372void
373s390_do_machine_check(struct pt_regs *regs)
374{
Heiko Carstensb73d40c2006-04-27 18:40:23 -0700375 static DEFINE_SPINLOCK(ipd_lock);
376 static unsigned long long last_ipd;
377 static int ipd_count;
378 unsigned long long tmp;
Heiko Carstens77fa2242005-06-25 14:55:30 -0700379 struct mci *mci;
380 struct mcck_struct *mcck;
381 int umode;
382
383 mci = (struct mci *) &S390_lowcore.mcck_interruption_code;
384 mcck = &__get_cpu_var(cpu_mcck);
385 umode = user_mode(regs);
386
387 if (mci->sd)
388 /* System damage -> stopping machine */
389 s390_handle_damage("received system damage machine check.");
390
391 if (mci->pd) {
392 if (mci->b) {
393 /* Processing backup -> verify if we can survive this */
394 u64 z_mcic, o_mcic, t_mcic;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800395#ifdef CONFIG_64BIT
Heiko Carstens77fa2242005-06-25 14:55:30 -0700396 z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<29);
397 o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
398 1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
399 1ULL<<30 | 1ULL<<21 | 1ULL<<20 | 1ULL<<17 |
400 1ULL<<16);
401#else
402 z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<57 | 1ULL<<50 |
403 1ULL<<29);
404 o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
405 1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
406 1ULL<<30 | 1ULL<<20 | 1ULL<<17 | 1ULL<<16);
407#endif
408 t_mcic = *(u64 *)mci;
409
410 if (((t_mcic & z_mcic) != 0) ||
411 ((t_mcic & o_mcic) != o_mcic)) {
412 s390_handle_damage("processing backup machine "
413 "check with damage.");
414 }
Heiko Carstensb73d40c2006-04-27 18:40:23 -0700415
416 /*
417 * Nullifying exigent condition, therefore we might
418 * retry this instruction.
419 */
420
421 spin_lock(&ipd_lock);
422
423 tmp = get_clock();
424
425 if (((tmp - last_ipd) >> 12) < MAX_IPD_TIME)
426 ipd_count++;
427 else
428 ipd_count = 1;
429
430 last_ipd = tmp;
431
432 if (ipd_count == MAX_IPD_COUNT)
433 s390_handle_damage("too many ipd retries.");
434
435 spin_unlock(&ipd_lock);
Heiko Carstens77fa2242005-06-25 14:55:30 -0700436 }
437 else {
438 /* Processing damage -> stopping machine */
439 s390_handle_damage("received instruction processing "
440 "damage machine check.");
441 }
442 }
443 if (s390_revalidate_registers(mci)) {
444 if (umode) {
445 /*
446 * Couldn't restore all register contents while in
447 * user mode -> mark task for termination.
448 */
449 mcck->kill_task = 1;
450 mcck->mcck_code = *(unsigned long long *) mci;
451 set_thread_flag(TIF_MCCK_PENDING);
452 }
453 else
454 /*
455 * Couldn't restore all register contents while in
456 * kernel mode -> stopping machine.
457 */
458 s390_handle_damage("unable to revalidate registers.");
459 }
460
461 if (mci->se)
462 /* Storage error uncorrected */
463 s390_handle_damage("received storage error uncorrected "
464 "machine check.");
465
466 if (mci->ke)
467 /* Storage key-error uncorrected */
468 s390_handle_damage("received storage key-error uncorrected "
469 "machine check.");
470
471 if (mci->ds && mci->fa)
472 /* Storage degradation */
473 s390_handle_damage("received storage degradation machine "
474 "check.");
475
476 if (mci->cp) {
477 /* Channel report word pending */
478 mcck->channel_report = 1;
479 set_thread_flag(TIF_MCCK_PENDING);
480 }
481
482 if (mci->w) {
483 /* Warning pending */
484 mcck->warning = 1;
485 set_thread_flag(TIF_MCCK_PENDING);
486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
489/*
490 * s390_init_machine_check
491 *
492 * initialize machine check handling
493 */
494static int
495machine_check_init(void)
496{
497 init_MUTEX_LOCKED(&m_sem);
Heiko Carstens77fa2242005-06-25 14:55:30 -0700498 ctl_clear_bit(14, 25); /* disable external damage MCH */
499 ctl_set_bit(14, 27); /* enable system recovery MCH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500#ifdef CONFIG_MACHCHK_WARNING
501 ctl_set_bit(14, 24); /* enable warning MCH */
502#endif
503 return 0;
504}
505
506/*
507 * Initialize the machine check handler really early to be able to
508 * catch all machine checks that happen during boot
509 */
510arch_initcall(machine_check_init);
511
512/*
513 * Machine checks for the channel subsystem must be enabled
514 * after the channel subsystem is initialized
515 */
516static int __init
517machine_check_crw_init (void)
518{
519 kernel_thread(s390_collect_crw_info, &m_sem, CLONE_FS|CLONE_FILES);
520 ctl_set_bit(14, 28); /* enable channel report MCH */
521 return 0;
522}
523
524device_initcall (machine_check_crw_init);