[S390] Reset infrastructure for re-IPL.
[safe/jmp/linux-2.6] / drivers / s390 / cio / cio.c
1 /*
2  *  drivers/s390/cio/cio.c
3  *   S/390 common I/O routines -- low level i/o calls
4  *
5  *    Copyright (C) 1999-2002 IBM Deutschland Entwicklung GmbH,
6  *                            IBM Corporation
7  *    Author(s): Ingo Adlung (adlung@de.ibm.com)
8  *               Cornelia Huck (cornelia.huck@de.ibm.com)
9  *               Arnd Bergmann (arndb@de.ibm.com)
10  *               Martin Schwidefsky (schwidefsky@de.ibm.com)
11  */
12
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/device.h>
17 #include <linux/kernel_stat.h>
18 #include <linux/interrupt.h>
19 #include <asm/cio.h>
20 #include <asm/delay.h>
21 #include <asm/irq.h>
22 #include <asm/irq_regs.h>
23 #include <asm/setup.h>
24 #include <asm/reset.h>
25 #include "airq.h"
26 #include "cio.h"
27 #include "css.h"
28 #include "chsc.h"
29 #include "ioasm.h"
30 #include "blacklist.h"
31 #include "cio_debug.h"
32 #include "../s390mach.h"
33
34 debug_info_t *cio_debug_msg_id;
35 debug_info_t *cio_debug_trace_id;
36 debug_info_t *cio_debug_crw_id;
37
38 int cio_show_msg;
39
40 static int __init
41 cio_setup (char *parm)
42 {
43         if (!strcmp (parm, "yes"))
44                 cio_show_msg = 1;
45         else if (!strcmp (parm, "no"))
46                 cio_show_msg = 0;
47         else
48                 printk (KERN_ERR "cio_setup : invalid cio_msg parameter '%s'",
49                         parm);
50         return 1;
51 }
52
53 __setup ("cio_msg=", cio_setup);
54
55 /*
56  * Function: cio_debug_init
57  * Initializes three debug logs (under /proc/s390dbf) for common I/O:
58  * - cio_msg logs the messages which are printk'ed when CONFIG_DEBUG_IO is on
59  * - cio_trace logs the calling of different functions
60  * - cio_crw logs the messages which are printk'ed when CONFIG_DEBUG_CRW is on
61  * debug levels depend on CONFIG_DEBUG_IO resp. CONFIG_DEBUG_CRW
62  */
63 static int __init
64 cio_debug_init (void)
65 {
66         cio_debug_msg_id = debug_register ("cio_msg", 16, 4, 16*sizeof (long));
67         if (!cio_debug_msg_id)
68                 goto out_unregister;
69         debug_register_view (cio_debug_msg_id, &debug_sprintf_view);
70         debug_set_level (cio_debug_msg_id, 2);
71         cio_debug_trace_id = debug_register ("cio_trace", 16, 4, 16);
72         if (!cio_debug_trace_id)
73                 goto out_unregister;
74         debug_register_view (cio_debug_trace_id, &debug_hex_ascii_view);
75         debug_set_level (cio_debug_trace_id, 2);
76         cio_debug_crw_id = debug_register ("cio_crw", 4, 4, 16*sizeof (long));
77         if (!cio_debug_crw_id)
78                 goto out_unregister;
79         debug_register_view (cio_debug_crw_id, &debug_sprintf_view);
80         debug_set_level (cio_debug_crw_id, 2);
81         pr_debug("debugging initialized\n");
82         return 0;
83
84 out_unregister:
85         if (cio_debug_msg_id)
86                 debug_unregister (cio_debug_msg_id);
87         if (cio_debug_trace_id)
88                 debug_unregister (cio_debug_trace_id);
89         if (cio_debug_crw_id)
90                 debug_unregister (cio_debug_crw_id);
91         pr_debug("could not initialize debugging\n");
92         return -1;
93 }
94
95 arch_initcall (cio_debug_init);
96
97 int
98 cio_set_options (struct subchannel *sch, int flags)
99 {
100        sch->options.suspend = (flags & DOIO_ALLOW_SUSPEND) != 0;
101        sch->options.prefetch = (flags & DOIO_DENY_PREFETCH) != 0;
102        sch->options.inter = (flags & DOIO_SUPPRESS_INTER) != 0;
103        return 0;
104 }
105
106 /* FIXME: who wants to use this? */
107 int
108 cio_get_options (struct subchannel *sch)
109 {
110        int flags;
111
112        flags = 0;
113        if (sch->options.suspend)
114                 flags |= DOIO_ALLOW_SUSPEND;
115        if (sch->options.prefetch)
116                 flags |= DOIO_DENY_PREFETCH;
117        if (sch->options.inter)
118                 flags |= DOIO_SUPPRESS_INTER;
119        return flags;
120 }
121
122 /*
123  * Use tpi to get a pending interrupt, call the interrupt handler and
124  * return a pointer to the subchannel structure.
125  */
126 static inline int
127 cio_tpi(void)
128 {
129         struct tpi_info *tpi_info;
130         struct subchannel *sch;
131         struct irb *irb;
132
133         tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
134         if (tpi (NULL) != 1)
135                 return 0;
136         irb = (struct irb *) __LC_IRB;
137         /* Store interrupt response block to lowcore. */
138         if (tsch (tpi_info->schid, irb) != 0)
139                 /* Not status pending or not operational. */
140                 return 1;
141         sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
142         if (!sch)
143                 return 1;
144         local_bh_disable();
145         irq_enter ();
146         spin_lock(&sch->lock);
147         memcpy (&sch->schib.scsw, &irb->scsw, sizeof (struct scsw));
148         if (sch->driver && sch->driver->irq)
149                 sch->driver->irq(&sch->dev);
150         spin_unlock(&sch->lock);
151         irq_exit ();
152         _local_bh_enable();
153         return 1;
154 }
155
156 static inline int
157 cio_start_handle_notoper(struct subchannel *sch, __u8 lpm)
158 {
159         char dbf_text[15];
160
161         if (lpm != 0)
162                 sch->lpm &= ~lpm;
163         else
164                 sch->lpm = 0;
165
166         stsch (sch->schid, &sch->schib);
167
168         CIO_MSG_EVENT(0, "cio_start: 'not oper' status for "
169                       "subchannel 0.%x.%04x!\n", sch->schid.ssid,
170                       sch->schid.sch_no);
171         sprintf(dbf_text, "no%s", sch->dev.bus_id);
172         CIO_TRACE_EVENT(0, dbf_text);
173         CIO_HEX_EVENT(0, &sch->schib, sizeof (struct schib));
174
175         return (sch->lpm ? -EACCES : -ENODEV);
176 }
177
178 int
179 cio_start_key (struct subchannel *sch,  /* subchannel structure */
180                struct ccw1 * cpa,       /* logical channel prog addr */
181                __u8 lpm,                /* logical path mask */
182                __u8 key)                /* storage key */
183 {
184         char dbf_txt[15];
185         int ccode;
186
187         CIO_TRACE_EVENT (4, "stIO");
188         CIO_TRACE_EVENT (4, sch->dev.bus_id);
189
190         /* sch is always under 2G. */
191         sch->orb.intparm = (__u32)(unsigned long)sch;
192         sch->orb.fmt = 1;
193
194         sch->orb.pfch = sch->options.prefetch == 0;
195         sch->orb.spnd = sch->options.suspend;
196         sch->orb.ssic = sch->options.suspend && sch->options.inter;
197         sch->orb.lpm = (lpm != 0) ? lpm : sch->lpm;
198 #ifdef CONFIG_64BIT
199         /*
200          * for 64 bit we always support 64 bit IDAWs with 4k page size only
201          */
202         sch->orb.c64 = 1;
203         sch->orb.i2k = 0;
204 #endif
205         sch->orb.key = key >> 4;
206         /* issue "Start Subchannel" */
207         sch->orb.cpa = (__u32) __pa (cpa);
208         ccode = ssch (sch->schid, &sch->orb);
209
210         /* process condition code */
211         sprintf (dbf_txt, "ccode:%d", ccode);
212         CIO_TRACE_EVENT (4, dbf_txt);
213
214         switch (ccode) {
215         case 0:
216                 /*
217                  * initialize device status information
218                  */
219                 sch->schib.scsw.actl |= SCSW_ACTL_START_PEND;
220                 return 0;
221         case 1:         /* status pending */
222         case 2:         /* busy */
223                 return -EBUSY;
224         default:                /* device/path not operational */
225                 return cio_start_handle_notoper(sch, lpm);
226         }
227 }
228
229 int
230 cio_start (struct subchannel *sch, struct ccw1 *cpa, __u8 lpm)
231 {
232         return cio_start_key(sch, cpa, lpm, PAGE_DEFAULT_KEY);
233 }
234
235 /*
236  * resume suspended I/O operation
237  */
238 int
239 cio_resume (struct subchannel *sch)
240 {
241         char dbf_txt[15];
242         int ccode;
243
244         CIO_TRACE_EVENT (4, "resIO");
245         CIO_TRACE_EVENT (4, sch->dev.bus_id);
246
247         ccode = rsch (sch->schid);
248
249         sprintf (dbf_txt, "ccode:%d", ccode);
250         CIO_TRACE_EVENT (4, dbf_txt);
251
252         switch (ccode) {
253         case 0:
254                 sch->schib.scsw.actl |= SCSW_ACTL_RESUME_PEND;
255                 return 0;
256         case 1:
257                 return -EBUSY;
258         case 2:
259                 return -EINVAL;
260         default:
261                 /*
262                  * useless to wait for request completion
263                  *  as device is no longer operational !
264                  */
265                 return -ENODEV;
266         }
267 }
268
269 /*
270  * halt I/O operation
271  */
272 int
273 cio_halt(struct subchannel *sch)
274 {
275         char dbf_txt[15];
276         int ccode;
277
278         if (!sch)
279                 return -ENODEV;
280
281         CIO_TRACE_EVENT (2, "haltIO");
282         CIO_TRACE_EVENT (2, sch->dev.bus_id);
283
284         /*
285          * Issue "Halt subchannel" and process condition code
286          */
287         ccode = hsch (sch->schid);
288
289         sprintf (dbf_txt, "ccode:%d", ccode);
290         CIO_TRACE_EVENT (2, dbf_txt);
291
292         switch (ccode) {
293         case 0:
294                 sch->schib.scsw.actl |= SCSW_ACTL_HALT_PEND;
295                 return 0;
296         case 1:         /* status pending */
297         case 2:         /* busy */
298                 return -EBUSY;
299         default:                /* device not operational */
300                 return -ENODEV;
301         }
302 }
303
304 /*
305  * Clear I/O operation
306  */
307 int
308 cio_clear(struct subchannel *sch)
309 {
310         char dbf_txt[15];
311         int ccode;
312
313         if (!sch)
314                 return -ENODEV;
315
316         CIO_TRACE_EVENT (2, "clearIO");
317         CIO_TRACE_EVENT (2, sch->dev.bus_id);
318
319         /*
320          * Issue "Clear subchannel" and process condition code
321          */
322         ccode = csch (sch->schid);
323
324         sprintf (dbf_txt, "ccode:%d", ccode);
325         CIO_TRACE_EVENT (2, dbf_txt);
326
327         switch (ccode) {
328         case 0:
329                 sch->schib.scsw.actl |= SCSW_ACTL_CLEAR_PEND;
330                 return 0;
331         default:                /* device not operational */
332                 return -ENODEV;
333         }
334 }
335
336 /*
337  * Function: cio_cancel
338  * Issues a "Cancel Subchannel" on the specified subchannel
339  * Note: We don't need any fancy intparms and flags here
340  *       since xsch is executed synchronously.
341  * Only for common I/O internal use as for now.
342  */
343 int
344 cio_cancel (struct subchannel *sch)
345 {
346         char dbf_txt[15];
347         int ccode;
348
349         if (!sch)
350                 return -ENODEV;
351
352         CIO_TRACE_EVENT (2, "cancelIO");
353         CIO_TRACE_EVENT (2, sch->dev.bus_id);
354
355         ccode = xsch (sch->schid);
356
357         sprintf (dbf_txt, "ccode:%d", ccode);
358         CIO_TRACE_EVENT (2, dbf_txt);
359
360         switch (ccode) {
361         case 0:         /* success */
362                 /* Update information in scsw. */
363                 stsch (sch->schid, &sch->schib);
364                 return 0;
365         case 1:         /* status pending */
366                 return -EBUSY;
367         case 2:         /* not applicable */
368                 return -EINVAL;
369         default:        /* not oper */
370                 return -ENODEV;
371         }
372 }
373
374 /*
375  * Function: cio_modify
376  * Issues a "Modify Subchannel" on the specified subchannel
377  */
378 int
379 cio_modify (struct subchannel *sch)
380 {
381         int ccode, retry, ret;
382
383         ret = 0;
384         for (retry = 0; retry < 5; retry++) {
385                 ccode = msch_err (sch->schid, &sch->schib);
386                 if (ccode < 0)  /* -EIO if msch gets a program check. */
387                         return ccode;
388                 switch (ccode) {
389                 case 0: /* successfull */
390                         return 0;
391                 case 1: /* status pending */
392                         return -EBUSY;
393                 case 2: /* busy */
394                         udelay (100);   /* allow for recovery */
395                         ret = -EBUSY;
396                         break;
397                 case 3: /* not operational */
398                         return -ENODEV;
399                 }
400         }
401         return ret;
402 }
403
404 /*
405  * Enable subchannel.
406  */
407 int
408 cio_enable_subchannel (struct subchannel *sch, unsigned int isc)
409 {
410         char dbf_txt[15];
411         int ccode;
412         int retry;
413         int ret;
414
415         CIO_TRACE_EVENT (2, "ensch");
416         CIO_TRACE_EVENT (2, sch->dev.bus_id);
417
418         ccode = stsch (sch->schid, &sch->schib);
419         if (ccode)
420                 return -ENODEV;
421
422         for (retry = 5, ret = 0; retry > 0; retry--) {
423                 sch->schib.pmcw.ena = 1;
424                 sch->schib.pmcw.isc = isc;
425                 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
426                 ret = cio_modify(sch);
427                 if (ret == -ENODEV)
428                         break;
429                 if (ret == -EIO)
430                         /*
431                          * Got a program check in cio_modify. Try without
432                          * the concurrent sense bit the next time.
433                          */
434                         sch->schib.pmcw.csense = 0;
435                 if (ret == 0) {
436                         stsch (sch->schid, &sch->schib);
437                         if (sch->schib.pmcw.ena)
438                                 break;
439                 }
440                 if (ret == -EBUSY) {
441                         struct irb irb;
442                         if (tsch(sch->schid, &irb) != 0)
443                                 break;
444                 }
445         }
446         sprintf (dbf_txt, "ret:%d", ret);
447         CIO_TRACE_EVENT (2, dbf_txt);
448         return ret;
449 }
450
451 /*
452  * Disable subchannel.
453  */
454 int
455 cio_disable_subchannel (struct subchannel *sch)
456 {
457         char dbf_txt[15];
458         int ccode;
459         int retry;
460         int ret;
461
462         CIO_TRACE_EVENT (2, "dissch");
463         CIO_TRACE_EVENT (2, sch->dev.bus_id);
464
465         ccode = stsch (sch->schid, &sch->schib);
466         if (ccode == 3)         /* Not operational. */
467                 return -ENODEV;
468
469         if (sch->schib.scsw.actl != 0)
470                 /*
471                  * the disable function must not be called while there are
472                  *  requests pending for completion !
473                  */
474                 return -EBUSY;
475
476         for (retry = 5, ret = 0; retry > 0; retry--) {
477                 sch->schib.pmcw.ena = 0;
478                 ret = cio_modify(sch);
479                 if (ret == -ENODEV)
480                         break;
481                 if (ret == -EBUSY)
482                         /*
483                          * The subchannel is busy or status pending.
484                          * We'll disable when the next interrupt was delivered
485                          * via the state machine.
486                          */
487                         break;
488                 if (ret == 0) {
489                         stsch (sch->schid, &sch->schib);
490                         if (!sch->schib.pmcw.ena)
491                                 break;
492                 }
493         }
494         sprintf (dbf_txt, "ret:%d", ret);
495         CIO_TRACE_EVENT (2, dbf_txt);
496         return ret;
497 }
498
499 /*
500  * cio_validate_subchannel()
501  *
502  * Find out subchannel type and initialize struct subchannel.
503  * Return codes:
504  *   SUBCHANNEL_TYPE_IO for a normal io subchannel
505  *   SUBCHANNEL_TYPE_CHSC for a chsc subchannel
506  *   SUBCHANNEL_TYPE_MESSAGE for a messaging subchannel
507  *   SUBCHANNEL_TYPE_ADM for a adm(?) subchannel
508  *   -ENXIO for non-defined subchannels
509  *   -ENODEV for subchannels with invalid device number or blacklisted devices
510  */
511 int
512 cio_validate_subchannel (struct subchannel *sch, struct subchannel_id schid)
513 {
514         char dbf_txt[15];
515         int ccode;
516
517         sprintf (dbf_txt, "valsch%x", schid.sch_no);
518         CIO_TRACE_EVENT (4, dbf_txt);
519
520         /* Nuke all fields. */
521         memset(sch, 0, sizeof(struct subchannel));
522
523         spin_lock_init(&sch->lock);
524         mutex_init(&sch->reg_mutex);
525
526         /* Set a name for the subchannel */
527         snprintf (sch->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x", schid.ssid,
528                   schid.sch_no);
529
530         /*
531          * The first subchannel that is not-operational (ccode==3)
532          *  indicates that there aren't any more devices available.
533          * If stsch gets an exception, it means the current subchannel set
534          *  is not valid.
535          */
536         ccode = stsch_err (schid, &sch->schib);
537         if (ccode)
538                 return (ccode == 3) ? -ENXIO : ccode;
539
540         sch->schid = schid;
541         /* Copy subchannel type from path management control word. */
542         sch->st = sch->schib.pmcw.st;
543
544         /*
545          * ... just being curious we check for non I/O subchannels
546          */
547         if (sch->st != 0) {
548                 CIO_DEBUG(KERN_INFO, 0,
549                           "Subchannel 0.%x.%04x reports "
550                           "non-I/O subchannel type %04X\n",
551                           sch->schid.ssid, sch->schid.sch_no, sch->st);
552                 /* We stop here for non-io subchannels. */
553                 return sch->st;
554         }
555
556         /* Initialization for io subchannels. */
557         if (!sch->schib.pmcw.dnv)
558                 /* io subchannel but device number is invalid. */
559                 return -ENODEV;
560
561         /* Devno is valid. */
562         if (is_blacklisted (sch->schid.ssid, sch->schib.pmcw.dev)) {
563                 /*
564                  * This device must not be known to Linux. So we simply
565                  * say that there is no device and return ENODEV.
566                  */
567                 CIO_MSG_EVENT(0, "Blacklisted device detected "
568                               "at devno %04X, subchannel set %x\n",
569                               sch->schib.pmcw.dev, sch->schid.ssid);
570                 return -ENODEV;
571         }
572         sch->opm = 0xff;
573         if (!cio_is_console(sch->schid))
574                 chsc_validate_chpids(sch);
575         sch->lpm = sch->schib.pmcw.pam & sch->opm;
576
577         CIO_DEBUG(KERN_INFO, 0,
578                   "Detected device %04x on subchannel 0.%x.%04X"
579                   " - PIM = %02X, PAM = %02X, POM = %02X\n",
580                   sch->schib.pmcw.dev, sch->schid.ssid,
581                   sch->schid.sch_no, sch->schib.pmcw.pim,
582                   sch->schib.pmcw.pam, sch->schib.pmcw.pom);
583
584         /*
585          * We now have to initially ...
586          *  ... set "interruption subclass"
587          *  ... enable "concurrent sense"
588          *  ... enable "multipath mode" if more than one
589          *        CHPID is available. This is done regardless
590          *        whether multiple paths are available for us.
591          */
592         sch->schib.pmcw.isc = 3;        /* could be smth. else */
593         sch->schib.pmcw.csense = 1;     /* concurrent sense */
594         sch->schib.pmcw.ena = 0;
595         if ((sch->lpm & (sch->lpm - 1)) != 0)
596                 sch->schib.pmcw.mp = 1; /* multipath mode */
597         return 0;
598 }
599
600 /*
601  * do_IRQ() handles all normal I/O device IRQ's (the special
602  *          SMP cross-CPU interrupts have their own specific
603  *          handlers).
604  *
605  */
606 void
607 do_IRQ (struct pt_regs *regs)
608 {
609         struct tpi_info *tpi_info;
610         struct subchannel *sch;
611         struct irb *irb;
612         struct pt_regs *old_regs;
613
614         old_regs = set_irq_regs(regs);
615         irq_enter();
616         asm volatile ("mc 0,0");
617         if (S390_lowcore.int_clock >= S390_lowcore.jiffy_timer)
618                 /**
619                  * Make sure that the i/o interrupt did not "overtake"
620                  * the last HZ timer interrupt.
621                  */
622                 account_ticks();
623         /*
624          * Get interrupt information from lowcore
625          */
626         tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
627         irb = (struct irb *) __LC_IRB;
628         do {
629                 kstat_cpu(smp_processor_id()).irqs[IO_INTERRUPT]++;
630                 /*
631                  * Non I/O-subchannel thin interrupts are processed differently
632                  */
633                 if (tpi_info->adapter_IO == 1 &&
634                     tpi_info->int_type == IO_INTERRUPT_TYPE) {
635                         do_adapter_IO();
636                         continue;
637                 }
638                 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
639                 if (sch)
640                         spin_lock(&sch->lock);
641                 /* Store interrupt response block to lowcore. */
642                 if (tsch (tpi_info->schid, irb) == 0 && sch) {
643                         /* Keep subchannel information word up to date. */
644                         memcpy (&sch->schib.scsw, &irb->scsw,
645                                 sizeof (irb->scsw));
646                         /* Call interrupt handler if there is one. */
647                         if (sch->driver && sch->driver->irq)
648                                 sch->driver->irq(&sch->dev);
649                 }
650                 if (sch)
651                         spin_unlock(&sch->lock);
652                 /*
653                  * Are more interrupts pending?
654                  * If so, the tpi instruction will update the lowcore
655                  * to hold the info for the next interrupt.
656                  * We don't do this for VM because a tpi drops the cpu
657                  * out of the sie which costs more cycles than it saves.
658                  */
659         } while (!MACHINE_IS_VM && tpi (NULL) != 0);
660         irq_exit();
661         set_irq_regs(old_regs);
662 }
663
664 #ifdef CONFIG_CCW_CONSOLE
665 static struct subchannel console_subchannel;
666 static int console_subchannel_in_use;
667
668 /*
669  * busy wait for the next interrupt on the console
670  */
671 void
672 wait_cons_dev (void)
673 {
674         unsigned long cr6      __attribute__ ((aligned (8)));
675         unsigned long save_cr6 __attribute__ ((aligned (8)));
676
677         /* 
678          * before entering the spinlock we may already have
679          * processed the interrupt on a different CPU...
680          */
681         if (!console_subchannel_in_use)
682                 return;
683
684         /* disable all but isc 7 (console device) */
685         __ctl_store (save_cr6, 6, 6);
686         cr6 = 0x01000000;
687         __ctl_load (cr6, 6, 6);
688
689         do {
690                 spin_unlock(&console_subchannel.lock);
691                 if (!cio_tpi())
692                         cpu_relax();
693                 spin_lock(&console_subchannel.lock);
694         } while (console_subchannel.schib.scsw.actl != 0);
695         /*
696          * restore previous isc value
697          */
698         __ctl_load (save_cr6, 6, 6);
699 }
700
701 static int
702 cio_test_for_console(struct subchannel_id schid, void *data)
703 {
704         if (stsch_err(schid, &console_subchannel.schib) != 0)
705                 return -ENXIO;
706         if (console_subchannel.schib.pmcw.dnv &&
707             console_subchannel.schib.pmcw.dev ==
708             console_devno) {
709                 console_irq = schid.sch_no;
710                 return 1; /* found */
711         }
712         return 0;
713 }
714
715
716 static int
717 cio_get_console_sch_no(void)
718 {
719         struct subchannel_id schid;
720         
721         init_subchannel_id(&schid);
722         if (console_irq != -1) {
723                 /* VM provided us with the irq number of the console. */
724                 schid.sch_no = console_irq;
725                 if (stsch(schid, &console_subchannel.schib) != 0 ||
726                     !console_subchannel.schib.pmcw.dnv)
727                         return -1;
728                 console_devno = console_subchannel.schib.pmcw.dev;
729         } else if (console_devno != -1) {
730                 /* At least the console device number is known. */
731                 for_each_subchannel(cio_test_for_console, NULL);
732                 if (console_irq == -1)
733                         return -1;
734         } else {
735                 /* unlike in 2.4, we cannot autoprobe here, since
736                  * the channel subsystem is not fully initialized.
737                  * With some luck, the HWC console can take over */
738                 printk(KERN_WARNING "No ccw console found!\n");
739                 return -1;
740         }
741         return console_irq;
742 }
743
744 struct subchannel *
745 cio_probe_console(void)
746 {
747         int sch_no, ret;
748         struct subchannel_id schid;
749
750         if (xchg(&console_subchannel_in_use, 1) != 0)
751                 return ERR_PTR(-EBUSY);
752         sch_no = cio_get_console_sch_no();
753         if (sch_no == -1) {
754                 console_subchannel_in_use = 0;
755                 return ERR_PTR(-ENODEV);
756         }
757         memset(&console_subchannel, 0, sizeof(struct subchannel));
758         init_subchannel_id(&schid);
759         schid.sch_no = sch_no;
760         ret = cio_validate_subchannel(&console_subchannel, schid);
761         if (ret) {
762                 console_subchannel_in_use = 0;
763                 return ERR_PTR(-ENODEV);
764         }
765
766         /*
767          * enable console I/O-interrupt subclass 7
768          */
769         ctl_set_bit(6, 24);
770         console_subchannel.schib.pmcw.isc = 7;
771         console_subchannel.schib.pmcw.intparm =
772                 (__u32)(unsigned long)&console_subchannel;
773         ret = cio_modify(&console_subchannel);
774         if (ret) {
775                 console_subchannel_in_use = 0;
776                 return ERR_PTR(ret);
777         }
778         return &console_subchannel;
779 }
780
781 void
782 cio_release_console(void)
783 {
784         console_subchannel.schib.pmcw.intparm = 0;
785         cio_modify(&console_subchannel);
786         ctl_clear_bit(6, 24);
787         console_subchannel_in_use = 0;
788 }
789
790 /* Bah... hack to catch console special sausages. */
791 int
792 cio_is_console(struct subchannel_id schid)
793 {
794         if (!console_subchannel_in_use)
795                 return 0;
796         return schid_equal(&schid, &console_subchannel.schid);
797 }
798
799 struct subchannel *
800 cio_get_console_subchannel(void)
801 {
802         if (!console_subchannel_in_use)
803                 return NULL;
804         return &console_subchannel;
805 }
806
807 #endif
808 static inline int
809 __disable_subchannel_easy(struct subchannel_id schid, struct schib *schib)
810 {
811         int retry, cc;
812
813         cc = 0;
814         for (retry=0;retry<3;retry++) {
815                 schib->pmcw.ena = 0;
816                 cc = msch(schid, schib);
817                 if (cc)
818                         return (cc==3?-ENODEV:-EBUSY);
819                 stsch(schid, schib);
820                 if (!schib->pmcw.ena)
821                         return 0;
822         }
823         return -EBUSY; /* uhm... */
824 }
825
826 static inline int
827 __clear_subchannel_easy(struct subchannel_id schid)
828 {
829         int retry;
830
831         if (csch(schid))
832                 return -ENODEV;
833         for (retry=0;retry<20;retry++) {
834                 struct tpi_info ti;
835
836                 if (tpi(&ti)) {
837                         tsch(ti.schid, (struct irb *)__LC_IRB);
838                         if (schid_equal(&ti.schid, &schid))
839                                 return 0;
840                 }
841                 udelay(100);
842         }
843         return -EBUSY;
844 }
845
846 static int __shutdown_subchannel_easy(struct subchannel_id schid, void *data)
847 {
848         struct schib schib;
849
850         if (stsch_err(schid, &schib))
851                 return -ENXIO;
852         if (!schib.pmcw.ena)
853                 return 0;
854         switch(__disable_subchannel_easy(schid, &schib)) {
855         case 0:
856         case -ENODEV:
857                 break;
858         default: /* -EBUSY */
859                 if (__clear_subchannel_easy(schid))
860                         break; /* give up... */
861                 stsch(schid, &schib);
862                 __disable_subchannel_easy(schid, &schib);
863         }
864         return 0;
865 }
866
867 static atomic_t chpid_reset_count;
868
869 static void s390_reset_chpids_mcck_handler(void)
870 {
871         struct crw crw;
872         struct mci *mci;
873
874         /* Check for pending channel report word. */
875         mci = (struct mci *)&S390_lowcore.mcck_interruption_code;
876         if (!mci->cp)
877                 return;
878         /* Process channel report words. */
879         while (stcrw(&crw) == 0) {
880                 /* Check for responses to RCHP. */
881                 if (crw.slct && crw.rsc == CRW_RSC_CPATH)
882                         atomic_dec(&chpid_reset_count);
883         }
884 }
885
886 #define RCHP_TIMEOUT (30 * USEC_PER_SEC)
887 static void css_reset(void)
888 {
889         int i, ret;
890         unsigned long long timeout;
891
892         /* Reset subchannels. */
893         for_each_subchannel(__shutdown_subchannel_easy,  NULL);
894         /* Reset channel paths. */
895         s390_reset_mcck_handler = s390_reset_chpids_mcck_handler;
896         /* Enable channel report machine checks. */
897         __ctl_set_bit(14, 28);
898         /* Temporarily reenable machine checks. */
899         local_mcck_enable();
900         for (i = 0; i <= __MAX_CHPID; i++) {
901                 ret = rchp(i);
902                 if ((ret == 0) || (ret == 2))
903                         /*
904                          * rchp either succeeded, or another rchp is already
905                          * in progress. In either case, we'll get a crw.
906                          */
907                         atomic_inc(&chpid_reset_count);
908         }
909         /* Wait for machine check for all channel paths. */
910         timeout = get_clock() + (RCHP_TIMEOUT << 12);
911         while (atomic_read(&chpid_reset_count) != 0) {
912                 if (get_clock() > timeout)
913                         break;
914                 cpu_relax();
915         }
916         /* Disable machine checks again. */
917         local_mcck_disable();
918         /* Disable channel report machine checks. */
919         __ctl_clear_bit(14, 28);
920         s390_reset_mcck_handler = NULL;
921 }
922
923 static struct reset_call css_reset_call = {
924         .fn = css_reset,
925 };
926
927 static int __init init_css_reset_call(void)
928 {
929         atomic_set(&chpid_reset_count, 0);
930         register_reset_call(&css_reset_call);
931         return 0;
932 }
933
934 arch_initcall(init_css_reset_call);
935
936 struct sch_match_id {
937         struct subchannel_id schid;
938         struct ccw_dev_id devid;
939         int rc;
940 };
941
942 static int __reipl_subchannel_match(struct subchannel_id schid, void *data)
943 {
944         struct schib schib;
945         struct sch_match_id *match_id = data;
946
947         if (stsch_err(schid, &schib))
948                 return -ENXIO;
949         if (schib.pmcw.dnv &&
950             (schib.pmcw.dev == match_id->devid.devno) &&
951             (schid.ssid == match_id->devid.ssid)) {
952                 match_id->schid = schid;
953                 match_id->rc = 0;
954                 return 1;
955         }
956         return 0;
957 }
958
959 static int reipl_find_schid(struct ccw_dev_id *devid,
960                             struct subchannel_id *schid)
961 {
962         struct sch_match_id match_id;
963
964         match_id.devid = *devid;
965         match_id.rc = -ENODEV;
966         for_each_subchannel(__reipl_subchannel_match, &match_id);
967         if (match_id.rc == 0)
968                 *schid = match_id.schid;
969         return match_id.rc;
970 }
971
972 extern void do_reipl_asm(__u32 schid);
973
974 /* Make sure all subchannels are quiet before we re-ipl an lpar. */
975 void reipl_ccw_dev(struct ccw_dev_id *devid)
976 {
977         struct subchannel_id schid;
978
979         s390_reset_system();
980         if (reipl_find_schid(devid, &schid) != 0)
981                 panic("IPL Device not found\n");
982         do_reipl_asm(*((__u32*)&schid));
983 }
984
985 extern struct schib ipl_schib;
986
987 /*
988  * ipl_save_parameters gets called very early. It is not allowed to access
989  * anything in the bss section at all. The bss section is not cleared yet,
990  * but may contain some ipl parameters written by the firmware.
991  * These parameters (if present) are copied to 0x2000.
992  * To avoid corruption of the ipl parameters, all variables used by this
993  * function must reside on the stack or in the data section.
994  */
995 void ipl_save_parameters(void)
996 {
997         struct subchannel_id schid;
998         unsigned int *ipl_ptr;
999         void *src, *dst;
1000
1001         schid = *(struct subchannel_id *)__LC_SUBCHANNEL_ID;
1002         if (!schid.one)
1003                 return;
1004         if (stsch(schid, &ipl_schib))
1005                 return;
1006         if (!ipl_schib.pmcw.dnv)
1007                 return;
1008         ipl_devno = ipl_schib.pmcw.dev;
1009         ipl_flags |= IPL_DEVNO_VALID;
1010         if (!ipl_schib.pmcw.qf)
1011                 return;
1012         ipl_flags |= IPL_PARMBLOCK_VALID;
1013         ipl_ptr = (unsigned int *)__LC_IPL_PARMBLOCK_PTR;
1014         src = (void *)(unsigned long)*ipl_ptr;
1015         dst = (void *)IPL_PARMBLOCK_ORIGIN;
1016         memmove(dst, src, PAGE_SIZE);
1017         *ipl_ptr = IPL_PARMBLOCK_ORIGIN;
1018 }