patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / 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  *   $Revision: 1.123 $
5  *
6  *    Copyright (C) 1999-2002 IBM Deutschland Entwicklung GmbH,
7  *                            IBM Corporation
8  *    Author(s): Ingo Adlung (adlung@de.ibm.com)
9  *               Cornelia Huck (cohuck@de.ibm.com)
10  *               Arnd Bergmann (arndb@de.ibm.com)
11  *               Martin Schwidefsky (schwidefsky@de.ibm.com)
12  */
13
14 #include <linux/module.h>
15 #include <linux/config.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/device.h>
19 #include <linux/kernel_stat.h>
20
21 #include <asm/hardirq.h>
22 #include <asm/cio.h>
23 #include <asm/delay.h>
24 #include <asm/irq.h>
25
26 #include "airq.h"
27 #include "cio.h"
28 #include "css.h"
29 #include "chsc.h"
30 #include "ioasm.h"
31 #include "blacklist.h"
32 #include "cio_debug.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", 4, 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", 4, 4, 8);
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", 2, 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->irq, 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         irq_enter ();
145         spin_lock(&sch->lock);
146         memcpy (&sch->schib.scsw, &irb->scsw, sizeof (struct scsw));
147         if (sch->driver && sch->driver->irq)
148                 sch->driver->irq(&sch->dev);
149         spin_unlock(&sch->lock);
150         irq_exit ();
151         return 1;
152 }
153
154 static inline int
155 cio_start_handle_notoper(struct subchannel *sch, __u8 lpm)
156 {
157         char dbf_text[15];
158
159         if (lpm != 0)
160                 sch->lpm &= ~lpm;
161         else
162                 sch->lpm = 0;
163
164         stsch (sch->irq, &sch->schib);
165
166         CIO_MSG_EVENT(0, "cio_start: 'not oper' status for "
167                       "subchannel %04x!\n", sch->irq);
168         sprintf(dbf_text, "no%s", sch->dev.bus_id);
169         CIO_TRACE_EVENT(0, dbf_text);
170         CIO_HEX_EVENT(0, &sch->schib, sizeof (struct schib));
171
172         return (sch->lpm ? -EACCES : -ENODEV);
173 }
174
175 int
176 cio_start (struct subchannel *sch,      /* subchannel structure */
177            struct ccw1 * cpa,           /* logical channel prog addr */
178            __u8 lpm)                    /* logical path mask */
179 {
180         char dbf_txt[15];
181         int ccode;
182
183         CIO_TRACE_EVENT (4, "stIO");
184         CIO_TRACE_EVENT (4, sch->dev.bus_id);
185
186         /* sch is always under 2G. */
187         sch->orb.intparm = (__u32)(unsigned long)sch;
188         sch->orb.fmt = 1;
189
190         sch->orb.pfch = sch->options.prefetch == 0;
191         sch->orb.spnd = sch->options.suspend;
192         sch->orb.ssic = sch->options.suspend && sch->options.inter;
193         sch->orb.lpm = (lpm != 0) ? (lpm & sch->opm) : sch->lpm;
194 #ifdef CONFIG_ARCH_S390X
195         /*
196          * for 64 bit we always support 64 bit IDAWs with 4k page size only
197          */
198         sch->orb.c64 = 1;
199         sch->orb.i2k = 0;
200 #endif
201         sch->orb.cpa = (__u32) __pa (cpa);
202
203         /*
204          * Issue "Start subchannel" and process condition code
205          */
206         ccode = ssch (sch->irq, &sch->orb);
207         sprintf (dbf_txt, "ccode:%d", ccode);
208         CIO_TRACE_EVENT (4, dbf_txt);
209
210         switch (ccode) {
211         case 0:
212                 /*
213                  * initialize device status information
214                  */
215                 sch->schib.scsw.actl |= SCSW_ACTL_START_PEND;
216                 return 0;
217         case 1:         /* status pending */
218         case 2:         /* busy */
219                 return -EBUSY;
220         default:                /* device/path not operational */
221                 return cio_start_handle_notoper(sch, lpm);
222         }
223 }
224
225 /*
226  * resume suspended I/O operation
227  */
228 int
229 cio_resume (struct subchannel *sch)
230 {
231         char dbf_txt[15];
232         int ccode;
233
234         CIO_TRACE_EVENT (4, "resIO");
235         CIO_TRACE_EVENT (4, sch->dev.bus_id);
236
237         ccode = rsch (sch->irq);
238
239         sprintf (dbf_txt, "ccode:%d", ccode);
240         CIO_TRACE_EVENT (4, dbf_txt);
241
242         switch (ccode) {
243         case 0:
244                 sch->schib.scsw.actl |= SCSW_ACTL_RESUME_PEND;
245                 return 0;
246         case 1:
247                 return -EBUSY;
248         case 2:
249                 return -EINVAL;
250         default:
251                 /*
252                  * useless to wait for request completion
253                  *  as device is no longer operational !
254                  */
255                 return -ENODEV;
256         }
257 }
258
259 /*
260  * halt I/O operation
261  */
262 int
263 cio_halt(struct subchannel *sch)
264 {
265         char dbf_txt[15];
266         int ccode;
267
268         if (!sch)
269                 return -ENODEV;
270
271         CIO_TRACE_EVENT (2, "haltIO");
272         CIO_TRACE_EVENT (2, sch->dev.bus_id);
273
274         /*
275          * Issue "Halt subchannel" and process condition code
276          */
277         ccode = hsch (sch->irq);
278
279         sprintf (dbf_txt, "ccode:%d", ccode);
280         CIO_TRACE_EVENT (2, dbf_txt);
281
282         switch (ccode) {
283         case 0:
284                 sch->schib.scsw.actl |= SCSW_ACTL_HALT_PEND;
285                 return 0;
286         case 1:         /* status pending */
287         case 2:         /* busy */
288                 return -EBUSY;
289         default:                /* device not operational */
290                 return -ENODEV;
291         }
292 }
293
294 /*
295  * Clear I/O operation
296  */
297 int
298 cio_clear(struct subchannel *sch)
299 {
300         char dbf_txt[15];
301         int ccode;
302
303         if (!sch)
304                 return -ENODEV;
305
306         CIO_TRACE_EVENT (2, "clearIO");
307         CIO_TRACE_EVENT (2, sch->dev.bus_id);
308
309         /*
310          * Issue "Clear subchannel" and process condition code
311          */
312         ccode = csch (sch->irq);
313
314         sprintf (dbf_txt, "ccode:%d", ccode);
315         CIO_TRACE_EVENT (2, dbf_txt);
316
317         switch (ccode) {
318         case 0:
319                 sch->schib.scsw.actl |= SCSW_ACTL_CLEAR_PEND;
320                 return 0;
321         default:                /* device not operational */
322                 return -ENODEV;
323         }
324 }
325
326 /*
327  * Function: cio_cancel
328  * Issues a "Cancel Subchannel" on the specified subchannel
329  * Note: We don't need any fancy intparms and flags here
330  *       since xsch is executed synchronously.
331  * Only for common I/O internal use as for now.
332  */
333 int
334 cio_cancel (struct subchannel *sch)
335 {
336         char dbf_txt[15];
337         int ccode;
338
339         if (!sch)
340                 return -ENODEV;
341
342         CIO_TRACE_EVENT (2, "cancelIO");
343         CIO_TRACE_EVENT (2, sch->dev.bus_id);
344
345         ccode = xsch (sch->irq);
346
347         sprintf (dbf_txt, "ccode:%d", ccode);
348         CIO_TRACE_EVENT (2, dbf_txt);
349
350         switch (ccode) {
351         case 0:         /* success */
352                 /* Update information in scsw. */
353                 stsch (sch->irq, &sch->schib);
354                 return 0;
355         case 1:         /* status pending */
356                 return -EBUSY;
357         case 2:         /* not applicable */
358                 return -EINVAL;
359         default:        /* not oper */
360                 return -ENODEV;
361         }
362 }
363
364 /*
365  * Function: cio_modify
366  * Issues a "Modify Subchannel" on the specified subchannel
367  */
368 int
369 cio_modify (struct subchannel *sch)
370 {
371         int ccode, retry, ret;
372
373         ret = 0;
374         for (retry = 0; retry < 5; retry++) {
375                 ccode = msch_err (sch->irq, &sch->schib);
376                 if (ccode < 0)  /* -EIO if msch gets a program check. */
377                         return ccode;
378                 switch (ccode) {
379                 case 0: /* successfull */
380                         return 0;
381                 case 1: /* status pending */
382                         return -EBUSY;
383                 case 2: /* busy */
384                         udelay (100);   /* allow for recovery */
385                         ret = -EBUSY;
386                         break;
387                 case 3: /* not operational */
388                         return -ENODEV;
389                 }
390         }
391         return ret;
392 }
393
394 /*
395  * Enable subchannel.
396  */
397 int
398 cio_enable_subchannel (struct subchannel *sch, unsigned int isc)
399 {
400         char dbf_txt[15];
401         int ccode;
402         int retry;
403         int ret;
404
405         CIO_TRACE_EVENT (2, "ensch");
406         CIO_TRACE_EVENT (2, sch->dev.bus_id);
407
408         ccode = stsch (sch->irq, &sch->schib);
409         if (ccode)
410                 return -ENODEV;
411
412         sch->schib.pmcw.ena = 1;
413         sch->schib.pmcw.isc = isc;
414         sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
415         for (retry = 5, ret = 0; retry > 0; retry--) {
416                 ret = cio_modify(sch);
417                 if (ret == -ENODEV)
418                         break;
419                 if (ret == -EIO)
420                         /*
421                          * Got a program check in cio_modify. Try without
422                          * the concurrent sense bit the next time.
423                          */
424                         sch->schib.pmcw.csense = 0;
425                 if (ret == 0) {
426                         stsch (sch->irq, &sch->schib);
427                         if (sch->schib.pmcw.ena)
428                                 break;
429                 }
430                 if (ret == -EBUSY) {
431                         struct irb irb;
432                         if (tsch(sch->irq, &irb) != 0)
433                                 break;
434                 }
435         }
436         sprintf (dbf_txt, "ret:%d", ret);
437         CIO_TRACE_EVENT (2, dbf_txt);
438         return ret;
439 }
440
441 /*
442  * Disable subchannel.
443  */
444 int
445 cio_disable_subchannel (struct subchannel *sch)
446 {
447         char dbf_txt[15];
448         int ccode;
449         int retry;
450         int ret;
451
452         CIO_TRACE_EVENT (2, "dissch");
453         CIO_TRACE_EVENT (2, sch->dev.bus_id);
454
455         ccode = stsch (sch->irq, &sch->schib);
456         if (ccode == 3)         /* Not operational. */
457                 return -ENODEV;
458
459         if (sch->schib.scsw.actl != 0)
460                 /*
461                  * the disable function must not be called while there are
462                  *  requests pending for completion !
463                  */
464                 return -EBUSY;
465
466
467         sch->schib.pmcw.ena = 0;
468         for (retry = 5, ret = 0; retry > 0; retry--) {
469                 ret = cio_modify(sch);
470                 if (ret == -ENODEV)
471                         break;
472                 if (ret == -EBUSY)
473                         /*
474                          * The subchannel is busy or status pending.
475                          * We'll disable when the next interrupt was delivered
476                          * via the state machine.
477                          */
478                         break;
479                 if (ret == 0) {
480                         stsch (sch->irq, &sch->schib);
481                         if (!sch->schib.pmcw.ena)
482                                 break;
483                 }
484         }
485         sprintf (dbf_txt, "ret:%d", ret);
486         CIO_TRACE_EVENT (2, dbf_txt);
487         return ret;
488 }
489
490 /*
491  * cio_validate_subchannel()
492  *
493  * Find out subchannel type and initialize struct subchannel.
494  * Return codes:
495  *   SUBCHANNEL_TYPE_IO for a normal io subchannel
496  *   SUBCHANNEL_TYPE_CHSC for a chsc subchannel
497  *   SUBCHANNEL_TYPE_MESSAGE for a messaging subchannel
498  *   SUBCHANNEL_TYPE_ADM for a adm(?) subchannel
499  *   -ENXIO for non-defined subchannels
500  *   -ENODEV for subchannels with invalid device number or blacklisted devices
501  */
502 int
503 cio_validate_subchannel (struct subchannel *sch, unsigned int irq)
504 {
505         char dbf_txt[15];
506         int ccode;
507
508         sprintf (dbf_txt, "valsch%x", irq);
509         CIO_TRACE_EVENT (4, dbf_txt);
510
511         /* Nuke all fields. */
512         memset(sch, 0, sizeof(struct subchannel));
513
514         spin_lock_init(&sch->lock);
515
516         /* Set a name for the subchannel */
517         snprintf (sch->dev.bus_id, BUS_ID_SIZE, "0.0.%04x", irq);
518
519         /*
520          * The first subchannel that is not-operational (ccode==3)
521          *  indicates that there aren't any more devices available.
522          */
523         sch->irq = irq;
524         ccode = stsch (irq, &sch->schib);
525         if (ccode)
526                 return -ENXIO;
527
528         /* Copy subchannel type from path management control word. */
529         sch->st = sch->schib.pmcw.st;
530
531         /*
532          * ... just being curious we check for non I/O subchannels
533          */
534         if (sch->st != 0) {
535                 CIO_DEBUG(KERN_INFO, 0,
536                           "Subchannel %04X reports "
537                           "non-I/O subchannel type %04X\n",
538                           sch->irq, sch->st);
539                 /* We stop here for non-io subchannels. */
540                 return sch->st;
541         }
542
543         /* Initialization for io subchannels. */
544         if (!sch->schib.pmcw.dnv)
545                 /* io subchannel but device number is invalid. */
546                 return -ENODEV;
547
548         /* Devno is valid. */
549         if (is_blacklisted (sch->schib.pmcw.dev)) {
550                 /*
551                  * This device must not be known to Linux. So we simply
552                  * say that there is no device and return ENODEV.
553                  */
554                 CIO_MSG_EVENT(0, "Blacklisted device detected "
555                               "at devno %04X\n", sch->schib.pmcw.dev);
556                 return -ENODEV;
557         }
558         sch->opm = 0xff;
559         chsc_validate_chpids(sch);
560         sch->lpm = sch->schib.pmcw.pim &
561                 sch->schib.pmcw.pam &
562                 sch->schib.pmcw.pom &
563                 sch->opm;
564
565         CIO_DEBUG(KERN_INFO, 0,
566                   "Detected device %04X on subchannel %04X"
567                   " - PIM = %02X, PAM = %02X, POM = %02X\n",
568                   sch->schib.pmcw.dev, sch->irq, sch->schib.pmcw.pim,
569                   sch->schib.pmcw.pam, sch->schib.pmcw.pom);
570
571         /*
572          * We now have to initially ...
573          *  ... set "interruption subclass"
574          *  ... enable "concurrent sense"
575          *  ... enable "multipath mode" if more than one
576          *        CHPID is available. This is done regardless
577          *        whether multiple paths are available for us.
578          */
579         sch->schib.pmcw.isc = 3;        /* could be smth. else */
580         sch->schib.pmcw.csense = 1;     /* concurrent sense */
581         sch->schib.pmcw.ena = 0;
582         if ((sch->lpm & (sch->lpm - 1)) != 0)
583                 sch->schib.pmcw.mp = 1; /* multipath mode */
584         return 0;
585 }
586
587 /*
588  * do_IRQ() handles all normal I/O device IRQ's (the special
589  *          SMP cross-CPU interrupts have their own specific
590  *          handlers).
591  *
592  */
593 void
594 do_IRQ (struct pt_regs *regs)
595 {
596         struct tpi_info *tpi_info;
597         struct subchannel *sch;
598         struct irb *irb;
599
600         irq_enter ();
601         asm volatile ("mc 0,0");
602         if (S390_lowcore.int_clock >= S390_lowcore.jiffy_timer)
603                 account_ticks(regs);
604         /*
605          * Get interrupt information from lowcore
606          */
607         tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
608         irb = (struct irb *) __LC_IRB;
609         do {
610                 kstat_cpu(smp_processor_id()).irqs[IO_INTERRUPT]++;
611                 /*
612                  * Non I/O-subchannel thin interrupts are processed differently
613                  */
614                 if (tpi_info->adapter_IO == 1 &&
615                     tpi_info->int_type == IO_INTERRUPT_TYPE) {
616                         do_adapter_IO();
617                         continue;
618                 }
619                 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
620                 if (sch)
621                         spin_lock(&sch->lock);
622                 /* Store interrupt response block to lowcore. */
623                 if (tsch (tpi_info->irq, irb) == 0 && sch) {
624                         /* Keep subchannel information word up to date. */
625                         memcpy (&sch->schib.scsw, &irb->scsw,
626                                 sizeof (irb->scsw));
627                         /* Call interrupt handler if there is one. */
628                         if (sch->driver && sch->driver->irq)
629                                 sch->driver->irq(&sch->dev);
630                 }
631                 if (sch)
632                         spin_unlock(&sch->lock);
633                 /*
634                  * Are more interrupts pending?
635                  * If so, the tpi instruction will update the lowcore
636                  * to hold the info for the next interrupt.
637                  * We don't do this for VM because a tpi drops the cpu
638                  * out of the sie which costs more cycles than it saves.
639                  */
640         } while (!MACHINE_IS_VM && tpi (NULL) != 0);
641         irq_exit ();
642 }
643
644 #ifdef CONFIG_CCW_CONSOLE
645 static struct subchannel console_subchannel;
646 static int console_subchannel_in_use;
647
648 /*
649  * busy wait for the next interrupt on the console
650  */
651 void
652 wait_cons_dev (void)
653 {
654         unsigned long cr6      __attribute__ ((aligned (8)));
655         unsigned long save_cr6 __attribute__ ((aligned (8)));
656
657         /* 
658          * before entering the spinlock we may already have
659          * processed the interrupt on a different CPU...
660          */
661         if (!console_subchannel_in_use)
662                 return;
663
664         /* disable all but isc 7 (console device) */
665         __ctl_store (save_cr6, 6, 6);
666         cr6 = 0x01000000;
667         __ctl_load (cr6, 6, 6);
668
669         do {
670                 spin_unlock(&console_subchannel.lock);
671                 if (!cio_tpi())
672                         cpu_relax();
673                 spin_lock(&console_subchannel.lock);
674         } while (console_subchannel.schib.scsw.actl != 0);
675         /*
676          * restore previous isc value
677          */
678         __ctl_load (save_cr6, 6, 6);
679 }
680
681 static int
682 cio_console_irq(void)
683 {
684         int irq;
685         
686         if (console_irq != -1) {
687                 /* VM provided us with the irq number of the console. */
688                 if (stsch(console_irq, &console_subchannel.schib) != 0 ||
689                     !console_subchannel.schib.pmcw.dnv)
690                         return -1;
691                 console_device = console_subchannel.schib.pmcw.dev;
692         } else if (console_device != -1) {
693                 /* At least the console device number is known. */
694                 for (irq = 0; irq < __MAX_SUBCHANNELS; irq++) {
695                         if (stsch(irq, &console_subchannel.schib) != 0)
696                                 break;
697                         if (console_subchannel.schib.pmcw.dnv &&
698                             console_subchannel.schib.pmcw.dev ==
699                             console_device) {
700                                 console_irq = irq;
701                                 break;
702                         }
703                 }
704                 if (console_irq == -1)
705                         return -1;
706         } else {
707                 /* unlike in 2.4, we cannot autoprobe here, since
708                  * the channel subsystem is not fully initialized.
709                  * With some luck, the HWC console can take over */
710                 printk(KERN_WARNING "No ccw console found!\n");
711                 return -1;
712         }
713         return console_irq;
714 }
715
716 struct subchannel *
717 cio_probe_console(void)
718 {
719         int irq, ret;
720
721         if (xchg(&console_subchannel_in_use, 1) != 0)
722                 return ERR_PTR(-EBUSY);
723         irq = cio_console_irq();
724         if (irq == -1) {
725                 console_subchannel_in_use = 0;
726                 return ERR_PTR(-ENODEV);
727         }
728         memset(&console_subchannel, 0, sizeof(struct subchannel));
729         ret = cio_validate_subchannel(&console_subchannel, irq);
730         if (ret) {
731                 console_subchannel_in_use = 0;
732                 return ERR_PTR(-ENODEV);
733         }
734
735         /*
736          * enable console I/O-interrupt subclass 7
737          */
738         ctl_set_bit(6, 24);
739         console_subchannel.schib.pmcw.isc = 7;
740         console_subchannel.schib.pmcw.intparm =
741                 (__u32)(unsigned long)&console_subchannel;
742         ret = cio_modify(&console_subchannel);
743         if (ret) {
744                 console_subchannel_in_use = 0;
745                 return ERR_PTR(ret);
746         }
747         return &console_subchannel;
748 }
749
750 void
751 cio_release_console(void)
752 {
753         console_subchannel.schib.pmcw.intparm = 0;
754         cio_modify(&console_subchannel);
755         ctl_clear_bit(6, 24);
756         console_subchannel_in_use = 0;
757 }
758
759 /* Bah... hack to catch console special sausages. */
760 int
761 cio_is_console(int irq)
762 {
763         if (!console_subchannel_in_use)
764                 return 0;
765         return (irq == console_subchannel.irq);
766 }
767
768 struct subchannel *
769 cio_get_console_subchannel(void)
770 {
771         if (!console_subchannel_in_use)
772                 return 0;
773         return &console_subchannel;
774 }
775
776 #endif
777 static inline int
778 __disable_subchannel_easy(unsigned int schid, struct schib *schib)
779 {
780         int retry, cc;
781
782         cc = 0;
783         for (retry=0;retry<3;retry++) {
784                 schib->pmcw.ena = 0;
785                 cc = msch(schid, schib);
786                 if (cc)
787                         return (cc==3?-ENODEV:-EBUSY);
788                 stsch(schid, schib);
789                 if (!schib->pmcw.ena)
790                         return 0;
791         }
792         return -EBUSY; /* uhm... */
793 }
794
795 static inline int
796 __clear_subchannel_easy(unsigned int schid)
797 {
798         int retry;
799
800         if (csch(schid))
801                 return -ENODEV;
802         for (retry=0;retry<20;retry++) {
803                 struct tpi_info ti;
804
805                 if (tpi(&ti)) {
806                         tsch(schid, (struct irb *)__LC_IRB);
807                         return 0;
808                 }
809                 udelay(100);
810         }
811         return -EBUSY;
812 }
813
814 extern void do_reipl(unsigned long devno);
815 /* Make sure all subchannels are quiet before we re-ipl an lpar. */
816 void
817 reipl(unsigned long devno)
818 {
819         unsigned int schid;
820
821         local_irq_disable();
822         for (schid=0;schid<=highest_subchannel;schid++) {
823                 struct schib schib;
824                 if (stsch(schid, &schib))
825                         goto out;
826                 if (!schib.pmcw.ena)
827                         continue;
828                 switch(__disable_subchannel_easy(schid, &schib)) {
829                 case 0:
830                 case -ENODEV:
831                         break;
832                 default: /* -EBUSY */
833                         if (__clear_subchannel_easy(schid))
834                                 break; /* give up... */
835                         stsch(schid, &schib);
836                         __disable_subchannel_easy(schid, &schib);
837                 }
838         }
839 out:
840         do_reipl(devno);
841 }