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