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