blob: 5399c5d99b81e0b5bc8f56949d4ec937711b6e67 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/init.h>
12#include <linux/sched.h>
13#include <linux/errno.h>
14#include <linux/workqueue.h>
Heiko Carstens022e4fc2006-05-01 12:16:14 -070015#include <linux/time.h>
Serge E. Hallyn84d11c52006-06-29 15:03:17 +020016#include <linux/kthread.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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059repeat:
60 down_interruptible(sem);
61 slow = 0;
Cornelia Huckfb6958a2006-01-06 00:19:25 -080062 chain = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 while (1) {
Cornelia Huckfb6958a2006-01-06 00:19:25 -080064 if (unlikely(chain > 1)) {
65 struct crw tmp_crw;
66
67 printk(KERN_WARNING"%s: Code does not support more "
68 "than two chained crws; please report to "
69 "linux390@de.ibm.com!\n", __FUNCTION__);
70 ccode = stcrw(&tmp_crw);
71 printk(KERN_WARNING"%s: crw reports slct=%d, oflw=%d, "
72 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
73 __FUNCTION__, tmp_crw.slct, tmp_crw.oflw,
74 tmp_crw.chn, tmp_crw.rsc, tmp_crw.anc,
75 tmp_crw.erc, tmp_crw.rsid);
76 printk(KERN_WARNING"%s: This was crw number %x in the "
77 "chain\n", __FUNCTION__, chain);
78 if (ccode != 0)
79 break;
80 chain = tmp_crw.chn ? chain + 1 : 0;
81 continue;
82 }
83 ccode = stcrw(&crw[chain]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (ccode != 0)
85 break;
86 DBG(KERN_DEBUG "crw_info : CRW reports slct=%d, oflw=%d, "
87 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
Cornelia Huckfb6958a2006-01-06 00:19:25 -080088 crw[chain].slct, crw[chain].oflw, crw[chain].chn,
89 crw[chain].rsc, crw[chain].anc, crw[chain].erc,
90 crw[chain].rsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 /* Check for overflows. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -080092 if (crw[chain].oflw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 pr_debug("%s: crw overflow detected!\n", __FUNCTION__);
94 css_reiterate_subchannels();
Cornelia Huckfb6958a2006-01-06 00:19:25 -080095 chain = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 slow = 1;
97 continue;
98 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -080099 switch (crw[chain].rsc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 case CRW_RSC_SCH:
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800101 if (crw[0].chn && !chain)
102 break;
103 pr_debug("source is subchannel %04X\n", crw[0].rsid);
104 ret = css_process_crw (crw[0].rsid,
105 chain ? crw[1].rsid : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (ret == -EAGAIN)
107 slow = 1;
108 break;
109 case CRW_RSC_MONITOR:
110 pr_debug("source is monitoring facility\n");
111 break;
112 case CRW_RSC_CPATH:
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800113 pr_debug("source is channel path %02X\n", crw[0].rsid);
Cornelia Huck7e560812006-07-12 16:40:19 +0200114 /*
115 * Check for solicited machine checks. These are
116 * created by reset channel path and need not be
117 * reported to the common I/O layer.
118 */
119 if (crw[chain].slct) {
120 DBG(KERN_INFO"solicited machine check for "
121 "channel path %02X\n", crw[0].rsid);
122 break;
123 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800124 switch (crw[0].erc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 case CRW_ERC_IPARM: /* Path has come. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800126 ret = chp_process_crw(crw[0].rsid, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 break;
128 case CRW_ERC_PERRI: /* Path has gone. */
129 case CRW_ERC_PERRN:
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800130 ret = chp_process_crw(crw[0].rsid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 break;
132 default:
133 pr_debug("Don't know how to handle erc=%x\n",
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800134 crw[0].erc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 ret = 0;
136 }
137 if (ret == -EAGAIN)
138 slow = 1;
139 break;
140 case CRW_RSC_CONFIG:
141 pr_debug("source is configuration-alert facility\n");
142 break;
143 case CRW_RSC_CSS:
144 pr_debug("source is channel subsystem\n");
145 ret = chsc_process_crw();
146 if (ret == -EAGAIN)
147 slow = 1;
148 break;
149 default:
150 pr_debug("unknown source\n");
151 break;
152 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800153 /* chain is always 0 or 1 here. */
154 chain = crw[chain].chn ? chain + 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156 if (slow)
157 queue_work(slow_path_wq, &slow_path_work);
158 goto repeat;
159 return 0;
160}
161
Heiko Carstens77fa2242005-06-25 14:55:30 -0700162struct mcck_struct {
163 int kill_task;
164 int channel_report;
165 int warning;
166 unsigned long long mcck_code;
167};
168
169static DEFINE_PER_CPU(struct mcck_struct, cpu_mcck);
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/*
Heiko Carstens77fa2242005-06-25 14:55:30 -0700172 * Main machine check handler function. Will be called with interrupts enabled
173 * or disabled and machine checks enabled or disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 */
175void
Heiko Carstens77fa2242005-06-25 14:55:30 -0700176s390_handle_mcck(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Heiko Carstens77fa2242005-06-25 14:55:30 -0700178 unsigned long flags;
179 struct mcck_struct mcck;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Heiko Carstens77fa2242005-06-25 14:55:30 -0700181 /*
182 * Disable machine checks and get the current state of accumulated
183 * machine checks. Afterwards delete the old state and enable machine
184 * checks again.
185 */
186 local_irq_save(flags);
187 local_mcck_disable();
188 mcck = __get_cpu_var(cpu_mcck);
189 memset(&__get_cpu_var(cpu_mcck), 0, sizeof(struct mcck_struct));
190 clear_thread_flag(TIF_MCCK_PENDING);
191 local_mcck_enable();
192 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Heiko Carstens77fa2242005-06-25 14:55:30 -0700194 if (mcck.channel_report)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 up(&m_sem);
196
197#ifdef CONFIG_MACHCHK_WARNING
198/*
199 * The warning may remain for a prolonged period on the bare iron.
200 * (actually till the machine is powered off, or until the problem is gone)
201 * So we just stop listening for the WARNING MCH and prevent continuously
202 * being interrupted. One caveat is however, that we must do this per
203 * processor and cannot use the smp version of ctl_clear_bit().
204 * On VM we only get one interrupt per virtally presented machinecheck.
205 * Though one suffices, we may get one interrupt per (virtual) processor.
206 */
Heiko Carstens77fa2242005-06-25 14:55:30 -0700207 if (mcck.warning) { /* WARNING pending ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 static int mchchk_wng_posted = 0;
209 /*
210 * Use single machine clear, as we cannot handle smp right now
211 */
212 __ctl_clear_bit(14, 24); /* Disable WARNING MCH */
213 if (xchg(&mchchk_wng_posted, 1) == 0)
214 kill_proc(1, SIGPWR, 1);
215 }
216#endif
Heiko Carstens77fa2242005-06-25 14:55:30 -0700217
218 if (mcck.kill_task) {
219 local_irq_enable();
220 printk(KERN_EMERG "mcck: Terminating task because of machine "
221 "malfunction (code 0x%016llx).\n", mcck.mcck_code);
222 printk(KERN_EMERG "mcck: task: %s, pid: %d.\n",
223 current->comm, current->pid);
224 do_exit(SIGSEGV);
225 }
226}
227
228/*
229 * returns 0 if all registers could be validated
230 * returns 1 otherwise
231 */
232static int
233s390_revalidate_registers(struct mci *mci)
234{
235 int kill_task;
236 u64 tmpclock;
237 u64 zero;
238 void *fpt_save_area, *fpt_creg_save_area;
239
240 kill_task = 0;
241 zero = 0;
242 /* General purpose registers */
243 if (!mci->gr)
244 /*
245 * General purpose registers couldn't be restored and have
246 * unknown contents. Process needs to be terminated.
247 */
248 kill_task = 1;
249
250 /* Revalidate floating point registers */
251 if (!mci->fp)
252 /*
253 * Floating point registers can't be restored and
254 * therefore the process needs to be terminated.
255 */
256 kill_task = 1;
257
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800258#ifndef CONFIG_64BIT
Heiko Carstens77fa2242005-06-25 14:55:30 -0700259 asm volatile("ld 0,0(%0)\n"
260 "ld 2,8(%0)\n"
261 "ld 4,16(%0)\n"
262 "ld 6,24(%0)"
263 : : "a" (&S390_lowcore.floating_pt_save_area));
264#endif
265
266 if (MACHINE_HAS_IEEE) {
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800267#ifdef CONFIG_64BIT
Heiko Carstens77fa2242005-06-25 14:55:30 -0700268 fpt_save_area = &S390_lowcore.floating_pt_save_area;
269 fpt_creg_save_area = &S390_lowcore.fpt_creg_save_area;
270#else
271 fpt_save_area = (void *) S390_lowcore.extended_save_area_addr;
272 fpt_creg_save_area = fpt_save_area+128;
273#endif
274 /* Floating point control register */
275 if (!mci->fc) {
276 /*
277 * Floating point control register can't be restored.
278 * Task will be terminated.
279 */
Martin Schwidefskyae6aa2e2005-09-03 15:57:56 -0700280 asm volatile ("lfpc 0(%0)" : : "a" (&zero), "m" (zero));
Heiko Carstens77fa2242005-06-25 14:55:30 -0700281 kill_task = 1;
282
283 }
284 else
285 asm volatile (
286 "lfpc 0(%0)"
287 : : "a" (fpt_creg_save_area));
288
289 asm volatile("ld 0,0(%0)\n"
290 "ld 1,8(%0)\n"
291 "ld 2,16(%0)\n"
292 "ld 3,24(%0)\n"
293 "ld 4,32(%0)\n"
294 "ld 5,40(%0)\n"
295 "ld 6,48(%0)\n"
296 "ld 7,56(%0)\n"
297 "ld 8,64(%0)\n"
298 "ld 9,72(%0)\n"
299 "ld 10,80(%0)\n"
300 "ld 11,88(%0)\n"
301 "ld 12,96(%0)\n"
302 "ld 13,104(%0)\n"
303 "ld 14,112(%0)\n"
304 "ld 15,120(%0)\n"
305 : : "a" (fpt_save_area));
306 }
307
308 /* Revalidate access registers */
309 asm volatile("lam 0,15,0(%0)"
310 : : "a" (&S390_lowcore.access_regs_save_area));
311 if (!mci->ar)
312 /*
313 * Access registers have unknown contents.
314 * Terminating task.
315 */
316 kill_task = 1;
317
318 /* Revalidate control registers */
319 if (!mci->cr)
320 /*
321 * Control registers have unknown contents.
322 * Can't recover and therefore stopping machine.
323 */
324 s390_handle_damage("invalid control registers.");
325 else
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800326#ifdef CONFIG_64BIT
Heiko Carstens77fa2242005-06-25 14:55:30 -0700327 asm volatile("lctlg 0,15,0(%0)"
328 : : "a" (&S390_lowcore.cregs_save_area));
329#else
330 asm volatile("lctl 0,15,0(%0)"
331 : : "a" (&S390_lowcore.cregs_save_area));
332#endif
333
334 /*
335 * We don't even try to revalidate the TOD register, since we simply
336 * can't write something sensible into that register.
337 */
338
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800339#ifdef CONFIG_64BIT
Heiko Carstens77fa2242005-06-25 14:55:30 -0700340 /*
341 * See if we can revalidate the TOD programmable register with its
342 * old contents (should be zero) otherwise set it to zero.
343 */
344 if (!mci->pr)
345 asm volatile("sr 0,0\n"
346 "sckpf"
347 : : : "0", "cc");
348 else
349 asm volatile(
350 "l 0,0(%0)\n"
351 "sckpf"
352 : : "a" (&S390_lowcore.tod_progreg_save_area) : "0", "cc");
353#endif
354
355 /* Revalidate clock comparator register */
356 asm volatile ("stck 0(%1)\n"
357 "sckc 0(%1)"
358 : "=m" (tmpclock) : "a" (&(tmpclock)) : "cc", "memory");
359
360 /* Check if old PSW is valid */
361 if (!mci->wp)
362 /*
363 * Can't tell if we come from user or kernel mode
364 * -> stopping machine.
365 */
366 s390_handle_damage("old psw invalid.");
367
368 if (!mci->ms || !mci->pm || !mci->ia)
369 kill_task = 1;
370
371 return kill_task;
372}
373
Heiko Carstensb73d40c2006-04-27 18:40:23 -0700374#define MAX_IPD_COUNT 29
Heiko Carstens022e4fc2006-05-01 12:16:14 -0700375#define MAX_IPD_TIME (5 * 60 * USEC_PER_SEC) /* 5 minutes */
Heiko Carstensb73d40c2006-04-27 18:40:23 -0700376
Heiko Carstens77fa2242005-06-25 14:55:30 -0700377/*
378 * machine check handler.
379 */
380void
381s390_do_machine_check(struct pt_regs *regs)
382{
Heiko Carstensb73d40c2006-04-27 18:40:23 -0700383 static DEFINE_SPINLOCK(ipd_lock);
384 static unsigned long long last_ipd;
385 static int ipd_count;
386 unsigned long long tmp;
Heiko Carstens77fa2242005-06-25 14:55:30 -0700387 struct mci *mci;
388 struct mcck_struct *mcck;
389 int umode;
390
Heiko Carstens8e9ccae2006-07-03 00:25:00 -0700391 lockdep_off();
392
Heiko Carstens77fa2242005-06-25 14:55:30 -0700393 mci = (struct mci *) &S390_lowcore.mcck_interruption_code;
394 mcck = &__get_cpu_var(cpu_mcck);
395 umode = user_mode(regs);
396
397 if (mci->sd)
398 /* System damage -> stopping machine */
399 s390_handle_damage("received system damage machine check.");
400
401 if (mci->pd) {
402 if (mci->b) {
403 /* Processing backup -> verify if we can survive this */
404 u64 z_mcic, o_mcic, t_mcic;
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800405#ifdef CONFIG_64BIT
Heiko Carstens77fa2242005-06-25 14:55:30 -0700406 z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<29);
407 o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
408 1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
409 1ULL<<30 | 1ULL<<21 | 1ULL<<20 | 1ULL<<17 |
410 1ULL<<16);
411#else
412 z_mcic = (1ULL<<63 | 1ULL<<59 | 1ULL<<57 | 1ULL<<50 |
413 1ULL<<29);
414 o_mcic = (1ULL<<43 | 1ULL<<42 | 1ULL<<41 | 1ULL<<40 |
415 1ULL<<36 | 1ULL<<35 | 1ULL<<34 | 1ULL<<32 |
416 1ULL<<30 | 1ULL<<20 | 1ULL<<17 | 1ULL<<16);
417#endif
418 t_mcic = *(u64 *)mci;
419
420 if (((t_mcic & z_mcic) != 0) ||
421 ((t_mcic & o_mcic) != o_mcic)) {
422 s390_handle_damage("processing backup machine "
423 "check with damage.");
424 }
Heiko Carstensb73d40c2006-04-27 18:40:23 -0700425
426 /*
427 * Nullifying exigent condition, therefore we might
428 * retry this instruction.
429 */
430
431 spin_lock(&ipd_lock);
432
433 tmp = get_clock();
434
435 if (((tmp - last_ipd) >> 12) < MAX_IPD_TIME)
436 ipd_count++;
437 else
438 ipd_count = 1;
439
440 last_ipd = tmp;
441
442 if (ipd_count == MAX_IPD_COUNT)
443 s390_handle_damage("too many ipd retries.");
444
445 spin_unlock(&ipd_lock);
Heiko Carstens77fa2242005-06-25 14:55:30 -0700446 }
447 else {
448 /* Processing damage -> stopping machine */
449 s390_handle_damage("received instruction processing "
450 "damage machine check.");
451 }
452 }
453 if (s390_revalidate_registers(mci)) {
454 if (umode) {
455 /*
456 * Couldn't restore all register contents while in
457 * user mode -> mark task for termination.
458 */
459 mcck->kill_task = 1;
460 mcck->mcck_code = *(unsigned long long *) mci;
461 set_thread_flag(TIF_MCCK_PENDING);
462 }
463 else
464 /*
465 * Couldn't restore all register contents while in
466 * kernel mode -> stopping machine.
467 */
468 s390_handle_damage("unable to revalidate registers.");
469 }
470
471 if (mci->se)
472 /* Storage error uncorrected */
473 s390_handle_damage("received storage error uncorrected "
474 "machine check.");
475
476 if (mci->ke)
477 /* Storage key-error uncorrected */
478 s390_handle_damage("received storage key-error uncorrected "
479 "machine check.");
480
481 if (mci->ds && mci->fa)
482 /* Storage degradation */
483 s390_handle_damage("received storage degradation machine "
484 "check.");
485
486 if (mci->cp) {
487 /* Channel report word pending */
488 mcck->channel_report = 1;
489 set_thread_flag(TIF_MCCK_PENDING);
490 }
491
492 if (mci->w) {
493 /* Warning pending */
494 mcck->warning = 1;
495 set_thread_flag(TIF_MCCK_PENDING);
496 }
Heiko Carstens8e9ccae2006-07-03 00:25:00 -0700497 lockdep_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500/*
501 * s390_init_machine_check
502 *
503 * initialize machine check handling
504 */
505static int
506machine_check_init(void)
507{
508 init_MUTEX_LOCKED(&m_sem);
Heiko Carstens77fa2242005-06-25 14:55:30 -0700509 ctl_clear_bit(14, 25); /* disable external damage MCH */
510 ctl_set_bit(14, 27); /* enable system recovery MCH */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511#ifdef CONFIG_MACHCHK_WARNING
512 ctl_set_bit(14, 24); /* enable warning MCH */
513#endif
514 return 0;
515}
516
517/*
518 * Initialize the machine check handler really early to be able to
519 * catch all machine checks that happen during boot
520 */
521arch_initcall(machine_check_init);
522
523/*
524 * Machine checks for the channel subsystem must be enabled
525 * after the channel subsystem is initialized
526 */
527static int __init
528machine_check_crw_init (void)
529{
Serge E. Hallyn84d11c52006-06-29 15:03:17 +0200530 kthread_run(s390_collect_crw_info, &m_sem, "kmcheck");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 ctl_set_bit(14, 28); /* enable channel report MCH */
532 return 0;
533}
534
535device_initcall (machine_check_crw_init);