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