Merge git://git.infradead.org/mtd-2.6
[pandora-kernel.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, u32 intparm)
411 {
412         char dbf_txt[15];
413         int ccode;
414         int retry;
415         int ret;
416
417         CIO_TRACE_EVENT (2, "ensch");
418         CIO_TRACE_EVENT (2, sch->dev.bus_id);
419
420         if (sch_is_pseudo_sch(sch))
421                 return -EINVAL;
422         ccode = stsch (sch->schid, &sch->schib);
423         if (ccode)
424                 return -ENODEV;
425
426         for (retry = 5, ret = 0; retry > 0; retry--) {
427                 sch->schib.pmcw.ena = 1;
428                 sch->schib.pmcw.isc = sch->isc;
429                 sch->schib.pmcw.intparm = intparm;
430                 ret = cio_modify(sch);
431                 if (ret == -ENODEV)
432                         break;
433                 if (ret == -EIO)
434                         /*
435                          * Got a program check in cio_modify. Try without
436                          * the concurrent sense bit the next time.
437                          */
438                         sch->schib.pmcw.csense = 0;
439                 if (ret == 0) {
440                         stsch (sch->schid, &sch->schib);
441                         if (sch->schib.pmcw.ena)
442                                 break;
443                 }
444                 if (ret == -EBUSY) {
445                         struct irb irb;
446                         if (tsch(sch->schid, &irb) != 0)
447                                 break;
448                 }
449         }
450         sprintf (dbf_txt, "ret:%d", ret);
451         CIO_TRACE_EVENT (2, dbf_txt);
452         return ret;
453 }
454
455 /*
456  * Disable subchannel.
457  */
458 int
459 cio_disable_subchannel (struct subchannel *sch)
460 {
461         char dbf_txt[15];
462         int ccode;
463         int retry;
464         int ret;
465
466         CIO_TRACE_EVENT (2, "dissch");
467         CIO_TRACE_EVENT (2, sch->dev.bus_id);
468
469         if (sch_is_pseudo_sch(sch))
470                 return 0;
471         ccode = stsch (sch->schid, &sch->schib);
472         if (ccode == 3)         /* Not operational. */
473                 return -ENODEV;
474
475         if (sch->schib.scsw.actl != 0)
476                 /*
477                  * the disable function must not be called while there are
478                  *  requests pending for completion !
479                  */
480                 return -EBUSY;
481
482         for (retry = 5, ret = 0; retry > 0; retry--) {
483                 sch->schib.pmcw.ena = 0;
484                 ret = cio_modify(sch);
485                 if (ret == -ENODEV)
486                         break;
487                 if (ret == -EBUSY)
488                         /*
489                          * The subchannel is busy or status pending.
490                          * We'll disable when the next interrupt was delivered
491                          * via the state machine.
492                          */
493                         break;
494                 if (ret == 0) {
495                         stsch (sch->schid, &sch->schib);
496                         if (!sch->schib.pmcw.ena)
497                                 break;
498                 }
499         }
500         sprintf (dbf_txt, "ret:%d", ret);
501         CIO_TRACE_EVENT (2, dbf_txt);
502         return ret;
503 }
504
505 int cio_create_sch_lock(struct subchannel *sch)
506 {
507         sch->lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
508         if (!sch->lock)
509                 return -ENOMEM;
510         spin_lock_init(sch->lock);
511         return 0;
512 }
513
514 /*
515  * cio_validate_subchannel()
516  *
517  * Find out subchannel type and initialize struct subchannel.
518  * Return codes:
519  *   SUBCHANNEL_TYPE_IO for a normal io subchannel
520  *   SUBCHANNEL_TYPE_CHSC for a chsc subchannel
521  *   SUBCHANNEL_TYPE_MESSAGE for a messaging subchannel
522  *   SUBCHANNEL_TYPE_ADM for a adm(?) subchannel
523  *   -ENXIO for non-defined subchannels
524  *   -ENODEV for subchannels with invalid device number or blacklisted devices
525  */
526 int
527 cio_validate_subchannel (struct subchannel *sch, struct subchannel_id schid)
528 {
529         char dbf_txt[15];
530         int ccode;
531         int err;
532
533         sprintf (dbf_txt, "valsch%x", schid.sch_no);
534         CIO_TRACE_EVENT (4, dbf_txt);
535
536         /* Nuke all fields. */
537         memset(sch, 0, sizeof(struct subchannel));
538
539         sch->schid = schid;
540         if (cio_is_console(schid)) {
541                 sch->lock = cio_get_console_lock();
542         } else {
543                 err = cio_create_sch_lock(sch);
544                 if (err)
545                         goto out;
546         }
547         mutex_init(&sch->reg_mutex);
548         /* Set a name for the subchannel */
549         snprintf (sch->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x", schid.ssid,
550                   schid.sch_no);
551
552         /*
553          * The first subchannel that is not-operational (ccode==3)
554          *  indicates that there aren't any more devices available.
555          * If stsch gets an exception, it means the current subchannel set
556          *  is not valid.
557          */
558         ccode = stsch_err (schid, &sch->schib);
559         if (ccode) {
560                 err = (ccode == 3) ? -ENXIO : ccode;
561                 goto out;
562         }
563         /* Copy subchannel type from path management control word. */
564         sch->st = sch->schib.pmcw.st;
565
566         /*
567          * ... just being curious we check for non I/O subchannels
568          */
569         if (sch->st != 0) {
570                 CIO_DEBUG(KERN_INFO, 0,
571                           "Subchannel 0.%x.%04x reports "
572                           "non-I/O subchannel type %04X\n",
573                           sch->schid.ssid, sch->schid.sch_no, sch->st);
574                 /* We stop here for non-io subchannels. */
575                 err = sch->st;
576                 goto out;
577         }
578
579         /* Initialization for io subchannels. */
580         if (!css_sch_is_valid(&sch->schib)) {
581                 err = -ENODEV;
582                 goto out;
583         }
584
585         /* Devno is valid. */
586         if (is_blacklisted (sch->schid.ssid, sch->schib.pmcw.dev)) {
587                 /*
588                  * This device must not be known to Linux. So we simply
589                  * say that there is no device and return ENODEV.
590                  */
591                 CIO_MSG_EVENT(4, "Blacklisted device detected "
592                               "at devno %04X, subchannel set %x\n",
593                               sch->schib.pmcw.dev, sch->schid.ssid);
594                 err = -ENODEV;
595                 goto out;
596         }
597         if (cio_is_console(sch->schid))
598                 sch->opm = 0xff;
599         else
600                 sch->opm = chp_get_sch_opm(sch);
601         sch->lpm = sch->schib.pmcw.pam & sch->opm;
602         sch->isc = 3;
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          *  ... enable "concurrent sense"
614          *  ... enable "multipath mode" if more than one
615          *        CHPID is available. This is done regardless
616          *        whether multiple paths are available for us.
617          */
618         sch->schib.pmcw.csense = 1;     /* concurrent sense */
619         sch->schib.pmcw.ena = 0;
620         if ((sch->lpm & (sch->lpm - 1)) != 0)
621                 sch->schib.pmcw.mp = 1; /* multipath mode */
622         /* clean up possible residual cmf stuff */
623         sch->schib.pmcw.mme = 0;
624         sch->schib.pmcw.mbfc = 0;
625         sch->schib.pmcw.mbi = 0;
626         sch->schib.mba = 0;
627         return 0;
628 out:
629         if (!cio_is_console(schid))
630                 kfree(sch->lock);
631         sch->lock = NULL;
632         return err;
633 }
634
635 /*
636  * do_IRQ() handles all normal I/O device IRQ's (the special
637  *          SMP cross-CPU interrupts have their own specific
638  *          handlers).
639  *
640  */
641 void
642 do_IRQ (struct pt_regs *regs)
643 {
644         struct tpi_info *tpi_info;
645         struct subchannel *sch;
646         struct irb *irb;
647         struct pt_regs *old_regs;
648
649         old_regs = set_irq_regs(regs);
650         irq_enter();
651         s390_idle_check();
652         if (S390_lowcore.int_clock >= S390_lowcore.clock_comparator)
653                 /* Serve timer interrupts first. */
654                 clock_comparator_work();
655         /*
656          * Get interrupt information from lowcore
657          */
658         tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
659         irb = (struct irb *) __LC_IRB;
660         do {
661                 kstat_cpu(smp_processor_id()).irqs[IO_INTERRUPT]++;
662                 /*
663                  * Non I/O-subchannel thin interrupts are processed differently
664                  */
665                 if (tpi_info->adapter_IO == 1 &&
666                     tpi_info->int_type == IO_INTERRUPT_TYPE) {
667                         do_adapter_IO();
668                         continue;
669                 }
670                 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
671                 if (!sch) {
672                         /* Clear pending interrupt condition. */
673                         tsch(tpi_info->schid, irb);
674                         continue;
675                 }
676                 spin_lock(sch->lock);
677                 /* Store interrupt response block to lowcore. */
678                 if (tsch(tpi_info->schid, irb) == 0) {
679                         /* Keep subchannel information word up to date. */
680                         memcpy (&sch->schib.scsw, &irb->scsw,
681                                 sizeof (irb->scsw));
682                         /* Call interrupt handler if there is one. */
683                         if (sch->driver && sch->driver->irq)
684                                 sch->driver->irq(sch);
685                 }
686                 spin_unlock(sch->lock);
687                 /*
688                  * Are more interrupts pending?
689                  * If so, the tpi instruction will update the lowcore
690                  * to hold the info for the next interrupt.
691                  * We don't do this for VM because a tpi drops the cpu
692                  * out of the sie which costs more cycles than it saves.
693                  */
694         } while (!MACHINE_IS_VM && tpi (NULL) != 0);
695         irq_exit();
696         set_irq_regs(old_regs);
697 }
698
699 #ifdef CONFIG_CCW_CONSOLE
700 static struct subchannel console_subchannel;
701 static struct io_subchannel_private console_priv;
702 static int console_subchannel_in_use;
703
704 void *cio_get_console_priv(void)
705 {
706         return &console_priv;
707 }
708
709 /*
710  * busy wait for the next interrupt on the console
711  */
712 void wait_cons_dev(void)
713         __releases(console_subchannel.lock)
714         __acquires(console_subchannel.lock)
715 {
716         unsigned long cr6      __attribute__ ((aligned (8)));
717         unsigned long save_cr6 __attribute__ ((aligned (8)));
718
719         /* 
720          * before entering the spinlock we may already have
721          * processed the interrupt on a different CPU...
722          */
723         if (!console_subchannel_in_use)
724                 return;
725
726         /* disable all but isc 7 (console device) */
727         __ctl_store (save_cr6, 6, 6);
728         cr6 = 0x01000000;
729         __ctl_load (cr6, 6, 6);
730
731         do {
732                 spin_unlock(console_subchannel.lock);
733                 if (!cio_tpi())
734                         cpu_relax();
735                 spin_lock(console_subchannel.lock);
736         } while (console_subchannel.schib.scsw.actl != 0);
737         /*
738          * restore previous isc value
739          */
740         __ctl_load (save_cr6, 6, 6);
741 }
742
743 static int
744 cio_test_for_console(struct subchannel_id schid, void *data)
745 {
746         if (stsch_err(schid, &console_subchannel.schib) != 0)
747                 return -ENXIO;
748         if ((console_subchannel.schib.pmcw.st == SUBCHANNEL_TYPE_IO) &&
749             console_subchannel.schib.pmcw.dnv &&
750             (console_subchannel.schib.pmcw.dev == console_devno)) {
751                 console_irq = schid.sch_no;
752                 return 1; /* found */
753         }
754         return 0;
755 }
756
757
758 static int
759 cio_get_console_sch_no(void)
760 {
761         struct subchannel_id schid;
762         
763         init_subchannel_id(&schid);
764         if (console_irq != -1) {
765                 /* VM provided us with the irq number of the console. */
766                 schid.sch_no = console_irq;
767                 if (stsch(schid, &console_subchannel.schib) != 0 ||
768                     (console_subchannel.schib.pmcw.st != SUBCHANNEL_TYPE_IO) ||
769                     !console_subchannel.schib.pmcw.dnv)
770                         return -1;
771                 console_devno = console_subchannel.schib.pmcw.dev;
772         } else if (console_devno != -1) {
773                 /* At least the console device number is known. */
774                 for_each_subchannel(cio_test_for_console, NULL);
775                 if (console_irq == -1)
776                         return -1;
777         } else {
778                 /* unlike in 2.4, we cannot autoprobe here, since
779                  * the channel subsystem is not fully initialized.
780                  * With some luck, the HWC console can take over */
781                 printk(KERN_WARNING "cio: No ccw console found!\n");
782                 return -1;
783         }
784         return console_irq;
785 }
786
787 struct subchannel *
788 cio_probe_console(void)
789 {
790         int sch_no, ret;
791         struct subchannel_id schid;
792
793         if (xchg(&console_subchannel_in_use, 1) != 0)
794                 return ERR_PTR(-EBUSY);
795         sch_no = cio_get_console_sch_no();
796         if (sch_no == -1) {
797                 console_subchannel_in_use = 0;
798                 return ERR_PTR(-ENODEV);
799         }
800         memset(&console_subchannel, 0, sizeof(struct subchannel));
801         init_subchannel_id(&schid);
802         schid.sch_no = sch_no;
803         ret = cio_validate_subchannel(&console_subchannel, schid);
804         if (ret) {
805                 console_subchannel_in_use = 0;
806                 return ERR_PTR(-ENODEV);
807         }
808
809         /*
810          * enable console I/O-interrupt subclass 7
811          */
812         ctl_set_bit(6, 24);
813         console_subchannel.isc = 7;
814         console_subchannel.schib.pmcw.isc = 7;
815         console_subchannel.schib.pmcw.intparm =
816                 (u32)(addr_t)&console_subchannel;
817         ret = cio_modify(&console_subchannel);
818         if (ret) {
819                 console_subchannel_in_use = 0;
820                 return ERR_PTR(ret);
821         }
822         return &console_subchannel;
823 }
824
825 void
826 cio_release_console(void)
827 {
828         console_subchannel.schib.pmcw.intparm = 0;
829         cio_modify(&console_subchannel);
830         ctl_clear_bit(6, 24);
831         console_subchannel_in_use = 0;
832 }
833
834 /* Bah... hack to catch console special sausages. */
835 int
836 cio_is_console(struct subchannel_id schid)
837 {
838         if (!console_subchannel_in_use)
839                 return 0;
840         return schid_equal(&schid, &console_subchannel.schid);
841 }
842
843 struct subchannel *
844 cio_get_console_subchannel(void)
845 {
846         if (!console_subchannel_in_use)
847                 return NULL;
848         return &console_subchannel;
849 }
850
851 #endif
852 static int
853 __disable_subchannel_easy(struct subchannel_id schid, struct schib *schib)
854 {
855         int retry, cc;
856
857         cc = 0;
858         for (retry=0;retry<3;retry++) {
859                 schib->pmcw.ena = 0;
860                 cc = msch(schid, schib);
861                 if (cc)
862                         return (cc==3?-ENODEV:-EBUSY);
863                 stsch(schid, schib);
864                 if (!schib->pmcw.ena)
865                         return 0;
866         }
867         return -EBUSY; /* uhm... */
868 }
869
870 /* we can't use the normal udelay here, since it enables external interrupts */
871
872 static void udelay_reset(unsigned long usecs)
873 {
874         uint64_t start_cc, end_cc;
875
876         asm volatile ("STCK %0" : "=m" (start_cc));
877         do {
878                 cpu_relax();
879                 asm volatile ("STCK %0" : "=m" (end_cc));
880         } while (((end_cc - start_cc)/4096) < usecs);
881 }
882
883 static int
884 __clear_subchannel_easy(struct subchannel_id schid)
885 {
886         int retry;
887
888         if (csch(schid))
889                 return -ENODEV;
890         for (retry=0;retry<20;retry++) {
891                 struct tpi_info ti;
892
893                 if (tpi(&ti)) {
894                         tsch(ti.schid, (struct irb *)__LC_IRB);
895                         if (schid_equal(&ti.schid, &schid))
896                                 return 0;
897                 }
898                 udelay_reset(100);
899         }
900         return -EBUSY;
901 }
902
903 static int pgm_check_occured;
904
905 static void cio_reset_pgm_check_handler(void)
906 {
907         pgm_check_occured = 1;
908 }
909
910 static int stsch_reset(struct subchannel_id schid, volatile struct schib *addr)
911 {
912         int rc;
913
914         pgm_check_occured = 0;
915         s390_base_pgm_handler_fn = cio_reset_pgm_check_handler;
916         rc = stsch(schid, addr);
917         s390_base_pgm_handler_fn = NULL;
918
919         /* The program check handler could have changed pgm_check_occured. */
920         barrier();
921
922         if (pgm_check_occured)
923                 return -EIO;
924         else
925                 return rc;
926 }
927
928 static int __shutdown_subchannel_easy(struct subchannel_id schid, void *data)
929 {
930         struct schib schib;
931
932         if (stsch_reset(schid, &schib))
933                 return -ENXIO;
934         if (!schib.pmcw.ena)
935                 return 0;
936         switch(__disable_subchannel_easy(schid, &schib)) {
937         case 0:
938         case -ENODEV:
939                 break;
940         default: /* -EBUSY */
941                 if (__clear_subchannel_easy(schid))
942                         break; /* give up... */
943                 stsch(schid, &schib);
944                 __disable_subchannel_easy(schid, &schib);
945         }
946         return 0;
947 }
948
949 static atomic_t chpid_reset_count;
950
951 static void s390_reset_chpids_mcck_handler(void)
952 {
953         struct crw crw;
954         struct mci *mci;
955
956         /* Check for pending channel report word. */
957         mci = (struct mci *)&S390_lowcore.mcck_interruption_code;
958         if (!mci->cp)
959                 return;
960         /* Process channel report words. */
961         while (stcrw(&crw) == 0) {
962                 /* Check for responses to RCHP. */
963                 if (crw.slct && crw.rsc == CRW_RSC_CPATH)
964                         atomic_dec(&chpid_reset_count);
965         }
966 }
967
968 #define RCHP_TIMEOUT (30 * USEC_PER_SEC)
969 static void css_reset(void)
970 {
971         int i, ret;
972         unsigned long long timeout;
973         struct chp_id chpid;
974
975         /* Reset subchannels. */
976         for_each_subchannel(__shutdown_subchannel_easy,  NULL);
977         /* Reset channel paths. */
978         s390_base_mcck_handler_fn = s390_reset_chpids_mcck_handler;
979         /* Enable channel report machine checks. */
980         __ctl_set_bit(14, 28);
981         /* Temporarily reenable machine checks. */
982         local_mcck_enable();
983         chp_id_init(&chpid);
984         for (i = 0; i <= __MAX_CHPID; i++) {
985                 chpid.id = i;
986                 ret = rchp(chpid);
987                 if ((ret == 0) || (ret == 2))
988                         /*
989                          * rchp either succeeded, or another rchp is already
990                          * in progress. In either case, we'll get a crw.
991                          */
992                         atomic_inc(&chpid_reset_count);
993         }
994         /* Wait for machine check for all channel paths. */
995         timeout = get_clock() + (RCHP_TIMEOUT << 12);
996         while (atomic_read(&chpid_reset_count) != 0) {
997                 if (get_clock() > timeout)
998                         break;
999                 cpu_relax();
1000         }
1001         /* Disable machine checks again. */
1002         local_mcck_disable();
1003         /* Disable channel report machine checks. */
1004         __ctl_clear_bit(14, 28);
1005         s390_base_mcck_handler_fn = NULL;
1006 }
1007
1008 static struct reset_call css_reset_call = {
1009         .fn = css_reset,
1010 };
1011
1012 static int __init init_css_reset_call(void)
1013 {
1014         atomic_set(&chpid_reset_count, 0);
1015         register_reset_call(&css_reset_call);
1016         return 0;
1017 }
1018
1019 arch_initcall(init_css_reset_call);
1020
1021 struct sch_match_id {
1022         struct subchannel_id schid;
1023         struct ccw_dev_id devid;
1024         int rc;
1025 };
1026
1027 static int __reipl_subchannel_match(struct subchannel_id schid, void *data)
1028 {
1029         struct schib schib;
1030         struct sch_match_id *match_id = data;
1031
1032         if (stsch_reset(schid, &schib))
1033                 return -ENXIO;
1034         if ((schib.pmcw.st == SUBCHANNEL_TYPE_IO) && schib.pmcw.dnv &&
1035             (schib.pmcw.dev == match_id->devid.devno) &&
1036             (schid.ssid == match_id->devid.ssid)) {
1037                 match_id->schid = schid;
1038                 match_id->rc = 0;
1039                 return 1;
1040         }
1041         return 0;
1042 }
1043
1044 static int reipl_find_schid(struct ccw_dev_id *devid,
1045                             struct subchannel_id *schid)
1046 {
1047         struct sch_match_id match_id;
1048
1049         match_id.devid = *devid;
1050         match_id.rc = -ENODEV;
1051         for_each_subchannel(__reipl_subchannel_match, &match_id);
1052         if (match_id.rc == 0)
1053                 *schid = match_id.schid;
1054         return match_id.rc;
1055 }
1056
1057 extern void do_reipl_asm(__u32 schid);
1058
1059 /* Make sure all subchannels are quiet before we re-ipl an lpar. */
1060 void reipl_ccw_dev(struct ccw_dev_id *devid)
1061 {
1062         struct subchannel_id schid;
1063
1064         s390_reset_system();
1065         if (reipl_find_schid(devid, &schid) != 0)
1066                 panic("IPL Device not found\n");
1067         do_reipl_asm(*((__u32*)&schid));
1068 }
1069
1070 int __init cio_get_iplinfo(struct cio_iplinfo *iplinfo)
1071 {
1072         struct subchannel_id schid;
1073         struct schib schib;
1074
1075         schid = *(struct subchannel_id *)__LC_SUBCHANNEL_ID;
1076         if (!schid.one)
1077                 return -ENODEV;
1078         if (stsch(schid, &schib))
1079                 return -ENODEV;
1080         if (schib.pmcw.st != SUBCHANNEL_TYPE_IO)
1081                 return -ENODEV;
1082         if (!schib.pmcw.dnv)
1083                 return -ENODEV;
1084         iplinfo->devno = schib.pmcw.dev;
1085         iplinfo->is_qdio = schib.pmcw.qf;
1086         return 0;
1087 }