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