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