ia64/kvm: compilation fix. export account_system_vtime.
[pv_ops_mirror.git] / drivers / s390 / cio / cio.c
blob82c6a2d451284424c40fc14791736f642ef80934
1 /*
2 * drivers/s390/cio/cio.c
3 * S/390 common I/O routines -- low level i/o calls
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)
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"
38 debug_info_t *cio_debug_msg_id;
39 debug_info_t *cio_debug_trace_id;
40 debug_info_t *cio_debug_crw_id;
43 * Function: cio_debug_init
44 * Initializes three debug logs for common I/O:
45 * - cio_msg logs generic cio messages
46 * - cio_trace logs the calling of different functions
47 * - cio_crw logs machine check related cio messages
49 static int __init cio_debug_init(void)
51 cio_debug_msg_id = debug_register("cio_msg", 16, 1, 16 * sizeof(long));
52 if (!cio_debug_msg_id)
53 goto out_unregister;
54 debug_register_view(cio_debug_msg_id, &debug_sprintf_view);
55 debug_set_level(cio_debug_msg_id, 2);
56 cio_debug_trace_id = debug_register("cio_trace", 16, 1, 16);
57 if (!cio_debug_trace_id)
58 goto out_unregister;
59 debug_register_view(cio_debug_trace_id, &debug_hex_ascii_view);
60 debug_set_level(cio_debug_trace_id, 2);
61 cio_debug_crw_id = debug_register("cio_crw", 16, 1, 16 * sizeof(long));
62 if (!cio_debug_crw_id)
63 goto out_unregister;
64 debug_register_view(cio_debug_crw_id, &debug_sprintf_view);
65 debug_set_level(cio_debug_crw_id, 4);
66 return 0;
68 out_unregister:
69 if (cio_debug_msg_id)
70 debug_unregister(cio_debug_msg_id);
71 if (cio_debug_trace_id)
72 debug_unregister(cio_debug_trace_id);
73 if (cio_debug_crw_id)
74 debug_unregister(cio_debug_crw_id);
75 printk(KERN_WARNING"cio: could not initialize debugging\n");
76 return -1;
79 arch_initcall (cio_debug_init);
81 int
82 cio_set_options (struct subchannel *sch, int flags)
84 sch->options.suspend = (flags & DOIO_ALLOW_SUSPEND) != 0;
85 sch->options.prefetch = (flags & DOIO_DENY_PREFETCH) != 0;
86 sch->options.inter = (flags & DOIO_SUPPRESS_INTER) != 0;
87 return 0;
90 /* FIXME: who wants to use this? */
91 int
92 cio_get_options (struct subchannel *sch)
94 int flags;
96 flags = 0;
97 if (sch->options.suspend)
98 flags |= DOIO_ALLOW_SUSPEND;
99 if (sch->options.prefetch)
100 flags |= DOIO_DENY_PREFETCH;
101 if (sch->options.inter)
102 flags |= DOIO_SUPPRESS_INTER;
103 return flags;
107 * Use tpi to get a pending interrupt, call the interrupt handler and
108 * return a pointer to the subchannel structure.
110 static int
111 cio_tpi(void)
113 struct tpi_info *tpi_info;
114 struct subchannel *sch;
115 struct irb *irb;
117 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
118 if (tpi (NULL) != 1)
119 return 0;
120 irb = (struct irb *) __LC_IRB;
121 /* Store interrupt response block to lowcore. */
122 if (tsch (tpi_info->schid, irb) != 0)
123 /* Not status pending or not operational. */
124 return 1;
125 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
126 if (!sch)
127 return 1;
128 local_bh_disable();
129 irq_enter ();
130 spin_lock(sch->lock);
131 memcpy (&sch->schib.scsw, &irb->scsw, sizeof (struct scsw));
132 if (sch->driver && sch->driver->irq)
133 sch->driver->irq(sch);
134 spin_unlock(sch->lock);
135 irq_exit ();
136 _local_bh_enable();
137 return 1;
140 static int
141 cio_start_handle_notoper(struct subchannel *sch, __u8 lpm)
143 char dbf_text[15];
145 if (lpm != 0)
146 sch->lpm &= ~lpm;
147 else
148 sch->lpm = 0;
150 stsch (sch->schid, &sch->schib);
152 CIO_MSG_EVENT(2, "cio_start: 'not oper' status for "
153 "subchannel 0.%x.%04x!\n", sch->schid.ssid,
154 sch->schid.sch_no);
155 sprintf(dbf_text, "no%s", sch->dev.bus_id);
156 CIO_TRACE_EVENT(0, dbf_text);
157 CIO_HEX_EVENT(0, &sch->schib, sizeof (struct schib));
159 return (sch->lpm ? -EACCES : -ENODEV);
163 cio_start_key (struct subchannel *sch, /* subchannel structure */
164 struct ccw1 * cpa, /* logical channel prog addr */
165 __u8 lpm, /* logical path mask */
166 __u8 key) /* storage key */
168 char dbf_txt[15];
169 int ccode;
170 struct orb *orb;
172 CIO_TRACE_EVENT(4, "stIO");
173 CIO_TRACE_EVENT(4, sch->dev.bus_id);
175 orb = &to_io_private(sch)->orb;
176 /* sch is always under 2G. */
177 orb->intparm = (u32)(addr_t)sch;
178 orb->fmt = 1;
180 orb->pfch = sch->options.prefetch == 0;
181 orb->spnd = sch->options.suspend;
182 orb->ssic = sch->options.suspend && sch->options.inter;
183 orb->lpm = (lpm != 0) ? lpm : sch->lpm;
184 #ifdef CONFIG_64BIT
186 * for 64 bit we always support 64 bit IDAWs with 4k page size only
188 orb->c64 = 1;
189 orb->i2k = 0;
190 #endif
191 orb->key = key >> 4;
192 /* issue "Start Subchannel" */
193 orb->cpa = (__u32) __pa(cpa);
194 ccode = ssch(sch->schid, orb);
196 /* process condition code */
197 sprintf(dbf_txt, "ccode:%d", ccode);
198 CIO_TRACE_EVENT(4, dbf_txt);
200 switch (ccode) {
201 case 0:
203 * initialize device status information
205 sch->schib.scsw.actl |= SCSW_ACTL_START_PEND;
206 return 0;
207 case 1: /* status pending */
208 case 2: /* busy */
209 return -EBUSY;
210 default: /* device/path not operational */
211 return cio_start_handle_notoper(sch, lpm);
216 cio_start (struct subchannel *sch, struct ccw1 *cpa, __u8 lpm)
218 return cio_start_key(sch, cpa, lpm, PAGE_DEFAULT_KEY);
222 * resume suspended I/O operation
225 cio_resume (struct subchannel *sch)
227 char dbf_txt[15];
228 int ccode;
230 CIO_TRACE_EVENT (4, "resIO");
231 CIO_TRACE_EVENT (4, sch->dev.bus_id);
233 ccode = rsch (sch->schid);
235 sprintf (dbf_txt, "ccode:%d", ccode);
236 CIO_TRACE_EVENT (4, dbf_txt);
238 switch (ccode) {
239 case 0:
240 sch->schib.scsw.actl |= SCSW_ACTL_RESUME_PEND;
241 return 0;
242 case 1:
243 return -EBUSY;
244 case 2:
245 return -EINVAL;
246 default:
248 * useless to wait for request completion
249 * as device is no longer operational !
251 return -ENODEV;
256 * halt I/O operation
259 cio_halt(struct subchannel *sch)
261 char dbf_txt[15];
262 int ccode;
264 if (!sch)
265 return -ENODEV;
267 CIO_TRACE_EVENT (2, "haltIO");
268 CIO_TRACE_EVENT (2, sch->dev.bus_id);
271 * Issue "Halt subchannel" and process condition code
273 ccode = hsch (sch->schid);
275 sprintf (dbf_txt, "ccode:%d", ccode);
276 CIO_TRACE_EVENT (2, dbf_txt);
278 switch (ccode) {
279 case 0:
280 sch->schib.scsw.actl |= SCSW_ACTL_HALT_PEND;
281 return 0;
282 case 1: /* status pending */
283 case 2: /* busy */
284 return -EBUSY;
285 default: /* device not operational */
286 return -ENODEV;
291 * Clear I/O operation
294 cio_clear(struct subchannel *sch)
296 char dbf_txt[15];
297 int ccode;
299 if (!sch)
300 return -ENODEV;
302 CIO_TRACE_EVENT (2, "clearIO");
303 CIO_TRACE_EVENT (2, sch->dev.bus_id);
306 * Issue "Clear subchannel" and process condition code
308 ccode = csch (sch->schid);
310 sprintf (dbf_txt, "ccode:%d", ccode);
311 CIO_TRACE_EVENT (2, dbf_txt);
313 switch (ccode) {
314 case 0:
315 sch->schib.scsw.actl |= SCSW_ACTL_CLEAR_PEND;
316 return 0;
317 default: /* device not operational */
318 return -ENODEV;
323 * Function: cio_cancel
324 * Issues a "Cancel Subchannel" on the specified subchannel
325 * Note: We don't need any fancy intparms and flags here
326 * since xsch is executed synchronously.
327 * Only for common I/O internal use as for now.
330 cio_cancel (struct subchannel *sch)
332 char dbf_txt[15];
333 int ccode;
335 if (!sch)
336 return -ENODEV;
338 CIO_TRACE_EVENT (2, "cancelIO");
339 CIO_TRACE_EVENT (2, sch->dev.bus_id);
341 ccode = xsch (sch->schid);
343 sprintf (dbf_txt, "ccode:%d", ccode);
344 CIO_TRACE_EVENT (2, dbf_txt);
346 switch (ccode) {
347 case 0: /* success */
348 /* Update information in scsw. */
349 stsch (sch->schid, &sch->schib);
350 return 0;
351 case 1: /* status pending */
352 return -EBUSY;
353 case 2: /* not applicable */
354 return -EINVAL;
355 default: /* not oper */
356 return -ENODEV;
361 * Function: cio_modify
362 * Issues a "Modify Subchannel" on the specified subchannel
365 cio_modify (struct subchannel *sch)
367 int ccode, retry, ret;
369 ret = 0;
370 for (retry = 0; retry < 5; retry++) {
371 ccode = msch_err (sch->schid, &sch->schib);
372 if (ccode < 0) /* -EIO if msch gets a program check. */
373 return ccode;
374 switch (ccode) {
375 case 0: /* successfull */
376 return 0;
377 case 1: /* status pending */
378 return -EBUSY;
379 case 2: /* busy */
380 udelay (100); /* allow for recovery */
381 ret = -EBUSY;
382 break;
383 case 3: /* not operational */
384 return -ENODEV;
387 return ret;
391 * Enable subchannel.
393 int cio_enable_subchannel(struct subchannel *sch, u32 intparm)
395 char dbf_txt[15];
396 int ccode;
397 int retry;
398 int ret;
400 CIO_TRACE_EVENT (2, "ensch");
401 CIO_TRACE_EVENT (2, sch->dev.bus_id);
403 if (sch_is_pseudo_sch(sch))
404 return -EINVAL;
405 ccode = stsch (sch->schid, &sch->schib);
406 if (ccode)
407 return -ENODEV;
409 for (retry = 5, ret = 0; retry > 0; retry--) {
410 sch->schib.pmcw.ena = 1;
411 sch->schib.pmcw.isc = sch->isc;
412 sch->schib.pmcw.intparm = intparm;
413 ret = cio_modify(sch);
414 if (ret == -ENODEV)
415 break;
416 if (ret == -EIO)
418 * Got a program check in cio_modify. Try without
419 * the concurrent sense bit the next time.
421 sch->schib.pmcw.csense = 0;
422 if (ret == 0) {
423 stsch (sch->schid, &sch->schib);
424 if (sch->schib.pmcw.ena)
425 break;
427 if (ret == -EBUSY) {
428 struct irb irb;
429 if (tsch(sch->schid, &irb) != 0)
430 break;
433 sprintf (dbf_txt, "ret:%d", ret);
434 CIO_TRACE_EVENT (2, dbf_txt);
435 return ret;
439 * Disable subchannel.
442 cio_disable_subchannel (struct subchannel *sch)
444 char dbf_txt[15];
445 int ccode;
446 int retry;
447 int ret;
449 CIO_TRACE_EVENT (2, "dissch");
450 CIO_TRACE_EVENT (2, sch->dev.bus_id);
452 if (sch_is_pseudo_sch(sch))
453 return 0;
454 ccode = stsch (sch->schid, &sch->schib);
455 if (ccode == 3) /* Not operational. */
456 return -ENODEV;
458 if (sch->schib.scsw.actl != 0)
460 * the disable function must not be called while there are
461 * requests pending for completion !
463 return -EBUSY;
465 for (retry = 5, ret = 0; retry > 0; retry--) {
466 sch->schib.pmcw.ena = 0;
467 ret = cio_modify(sch);
468 if (ret == -ENODEV)
469 break;
470 if (ret == -EBUSY)
472 * The subchannel is busy or status pending.
473 * We'll disable when the next interrupt was delivered
474 * via the state machine.
476 break;
477 if (ret == 0) {
478 stsch (sch->schid, &sch->schib);
479 if (!sch->schib.pmcw.ena)
480 break;
483 sprintf (dbf_txt, "ret:%d", ret);
484 CIO_TRACE_EVENT (2, dbf_txt);
485 return ret;
488 int cio_create_sch_lock(struct subchannel *sch)
490 sch->lock = kmalloc(sizeof(spinlock_t), GFP_KERNEL);
491 if (!sch->lock)
492 return -ENOMEM;
493 spin_lock_init(sch->lock);
494 return 0;
498 * cio_validate_subchannel()
500 * Find out subchannel type and initialize struct subchannel.
501 * Return codes:
502 * SUBCHANNEL_TYPE_IO for a normal io subchannel
503 * SUBCHANNEL_TYPE_CHSC for a chsc subchannel
504 * SUBCHANNEL_TYPE_MESSAGE for a messaging subchannel
505 * SUBCHANNEL_TYPE_ADM for a adm(?) subchannel
506 * -ENXIO for non-defined subchannels
507 * -ENODEV for subchannels with invalid device number or blacklisted devices
510 cio_validate_subchannel (struct subchannel *sch, struct subchannel_id schid)
512 char dbf_txt[15];
513 int ccode;
514 int err;
516 sprintf (dbf_txt, "valsch%x", schid.sch_no);
517 CIO_TRACE_EVENT (4, dbf_txt);
519 /* Nuke all fields. */
520 memset(sch, 0, sizeof(struct subchannel));
522 sch->schid = schid;
523 if (cio_is_console(schid)) {
524 sch->lock = cio_get_console_lock();
525 } else {
526 err = cio_create_sch_lock(sch);
527 if (err)
528 goto out;
530 mutex_init(&sch->reg_mutex);
531 /* Set a name for the subchannel */
532 snprintf (sch->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x", schid.ssid,
533 schid.sch_no);
536 * The first subchannel that is not-operational (ccode==3)
537 * indicates that there aren't any more devices available.
538 * If stsch gets an exception, it means the current subchannel set
539 * is not valid.
541 ccode = stsch_err (schid, &sch->schib);
542 if (ccode) {
543 err = (ccode == 3) ? -ENXIO : ccode;
544 goto out;
546 /* Copy subchannel type from path management control word. */
547 sch->st = sch->schib.pmcw.st;
550 * ... just being curious we check for non I/O subchannels
552 if (sch->st != 0) {
553 CIO_MSG_EVENT(4, "Subchannel 0.%x.%04x reports "
554 "non-I/O subchannel type %04X\n",
555 sch->schid.ssid, sch->schid.sch_no, sch->st);
556 /* We stop here for non-io subchannels. */
557 err = sch->st;
558 goto out;
561 /* Initialization for io subchannels. */
562 if (!css_sch_is_valid(&sch->schib)) {
563 err = -ENODEV;
564 goto out;
567 /* Devno is valid. */
568 if (is_blacklisted (sch->schid.ssid, sch->schib.pmcw.dev)) {
570 * This device must not be known to Linux. So we simply
571 * say that there is no device and return ENODEV.
573 CIO_MSG_EVENT(6, "Blacklisted device detected "
574 "at devno %04X, subchannel set %x\n",
575 sch->schib.pmcw.dev, sch->schid.ssid);
576 err = -ENODEV;
577 goto out;
579 if (cio_is_console(sch->schid))
580 sch->opm = 0xff;
581 else
582 sch->opm = chp_get_sch_opm(sch);
583 sch->lpm = sch->schib.pmcw.pam & sch->opm;
584 sch->isc = 3;
586 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X "
587 "- PIM = %02X, PAM = %02X, POM = %02X\n",
588 sch->schib.pmcw.dev, sch->schid.ssid,
589 sch->schid.sch_no, sch->schib.pmcw.pim,
590 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
593 * We now have to initially ...
594 * ... enable "concurrent sense"
595 * ... enable "multipath mode" if more than one
596 * CHPID is available. This is done regardless
597 * whether multiple paths are available for us.
599 sch->schib.pmcw.csense = 1; /* concurrent sense */
600 sch->schib.pmcw.ena = 0;
601 if ((sch->lpm & (sch->lpm - 1)) != 0)
602 sch->schib.pmcw.mp = 1; /* multipath mode */
603 /* clean up possible residual cmf stuff */
604 sch->schib.pmcw.mme = 0;
605 sch->schib.pmcw.mbfc = 0;
606 sch->schib.pmcw.mbi = 0;
607 sch->schib.mba = 0;
608 return 0;
609 out:
610 if (!cio_is_console(schid))
611 kfree(sch->lock);
612 sch->lock = NULL;
613 return err;
617 * do_IRQ() handles all normal I/O device IRQ's (the special
618 * SMP cross-CPU interrupts have their own specific
619 * handlers).
622 void
623 do_IRQ (struct pt_regs *regs)
625 struct tpi_info *tpi_info;
626 struct subchannel *sch;
627 struct irb *irb;
628 struct pt_regs *old_regs;
630 old_regs = set_irq_regs(regs);
631 irq_enter();
632 s390_idle_check();
633 if (S390_lowcore.int_clock >= S390_lowcore.clock_comparator)
634 /* Serve timer interrupts first. */
635 clock_comparator_work();
637 * Get interrupt information from lowcore
639 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
640 irb = (struct irb *) __LC_IRB;
641 do {
642 kstat_cpu(smp_processor_id()).irqs[IO_INTERRUPT]++;
644 * Non I/O-subchannel thin interrupts are processed differently
646 if (tpi_info->adapter_IO == 1 &&
647 tpi_info->int_type == IO_INTERRUPT_TYPE) {
648 do_adapter_IO();
649 continue;
651 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
652 if (!sch) {
653 /* Clear pending interrupt condition. */
654 tsch(tpi_info->schid, irb);
655 continue;
657 spin_lock(sch->lock);
658 /* Store interrupt response block to lowcore. */
659 if (tsch(tpi_info->schid, irb) == 0) {
660 /* Keep subchannel information word up to date. */
661 memcpy (&sch->schib.scsw, &irb->scsw,
662 sizeof (irb->scsw));
663 /* Call interrupt handler if there is one. */
664 if (sch->driver && sch->driver->irq)
665 sch->driver->irq(sch);
667 spin_unlock(sch->lock);
669 * Are more interrupts pending?
670 * If so, the tpi instruction will update the lowcore
671 * to hold the info for the next interrupt.
672 * We don't do this for VM because a tpi drops the cpu
673 * out of the sie which costs more cycles than it saves.
675 } while (!MACHINE_IS_VM && tpi (NULL) != 0);
676 irq_exit();
677 set_irq_regs(old_regs);
680 #ifdef CONFIG_CCW_CONSOLE
681 static struct subchannel console_subchannel;
682 static struct io_subchannel_private console_priv;
683 static int console_subchannel_in_use;
685 void *cio_get_console_priv(void)
687 return &console_priv;
691 * busy wait for the next interrupt on the console
693 void wait_cons_dev(void)
694 __releases(console_subchannel.lock)
695 __acquires(console_subchannel.lock)
697 unsigned long cr6 __attribute__ ((aligned (8)));
698 unsigned long save_cr6 __attribute__ ((aligned (8)));
701 * before entering the spinlock we may already have
702 * processed the interrupt on a different CPU...
704 if (!console_subchannel_in_use)
705 return;
707 /* disable all but isc 7 (console device) */
708 __ctl_store (save_cr6, 6, 6);
709 cr6 = 0x01000000;
710 __ctl_load (cr6, 6, 6);
712 do {
713 spin_unlock(console_subchannel.lock);
714 if (!cio_tpi())
715 cpu_relax();
716 spin_lock(console_subchannel.lock);
717 } while (console_subchannel.schib.scsw.actl != 0);
719 * restore previous isc value
721 __ctl_load (save_cr6, 6, 6);
724 static int
725 cio_test_for_console(struct subchannel_id schid, void *data)
727 if (stsch_err(schid, &console_subchannel.schib) != 0)
728 return -ENXIO;
729 if ((console_subchannel.schib.pmcw.st == SUBCHANNEL_TYPE_IO) &&
730 console_subchannel.schib.pmcw.dnv &&
731 (console_subchannel.schib.pmcw.dev == console_devno)) {
732 console_irq = schid.sch_no;
733 return 1; /* found */
735 return 0;
739 static int
740 cio_get_console_sch_no(void)
742 struct subchannel_id schid;
744 init_subchannel_id(&schid);
745 if (console_irq != -1) {
746 /* VM provided us with the irq number of the console. */
747 schid.sch_no = console_irq;
748 if (stsch(schid, &console_subchannel.schib) != 0 ||
749 (console_subchannel.schib.pmcw.st != SUBCHANNEL_TYPE_IO) ||
750 !console_subchannel.schib.pmcw.dnv)
751 return -1;
752 console_devno = console_subchannel.schib.pmcw.dev;
753 } else if (console_devno != -1) {
754 /* At least the console device number is known. */
755 for_each_subchannel(cio_test_for_console, NULL);
756 if (console_irq == -1)
757 return -1;
758 } else {
759 /* unlike in 2.4, we cannot autoprobe here, since
760 * the channel subsystem is not fully initialized.
761 * With some luck, the HWC console can take over */
762 printk(KERN_WARNING "cio: No ccw console found!\n");
763 return -1;
765 return console_irq;
768 struct subchannel *
769 cio_probe_console(void)
771 int sch_no, ret;
772 struct subchannel_id schid;
774 if (xchg(&console_subchannel_in_use, 1) != 0)
775 return ERR_PTR(-EBUSY);
776 sch_no = cio_get_console_sch_no();
777 if (sch_no == -1) {
778 console_subchannel_in_use = 0;
779 return ERR_PTR(-ENODEV);
781 memset(&console_subchannel, 0, sizeof(struct subchannel));
782 init_subchannel_id(&schid);
783 schid.sch_no = sch_no;
784 ret = cio_validate_subchannel(&console_subchannel, schid);
785 if (ret) {
786 console_subchannel_in_use = 0;
787 return ERR_PTR(-ENODEV);
791 * enable console I/O-interrupt subclass 7
793 ctl_set_bit(6, 24);
794 console_subchannel.isc = 7;
795 console_subchannel.schib.pmcw.isc = 7;
796 console_subchannel.schib.pmcw.intparm =
797 (u32)(addr_t)&console_subchannel;
798 ret = cio_modify(&console_subchannel);
799 if (ret) {
800 console_subchannel_in_use = 0;
801 return ERR_PTR(ret);
803 return &console_subchannel;
806 void
807 cio_release_console(void)
809 console_subchannel.schib.pmcw.intparm = 0;
810 cio_modify(&console_subchannel);
811 ctl_clear_bit(6, 24);
812 console_subchannel_in_use = 0;
815 /* Bah... hack to catch console special sausages. */
817 cio_is_console(struct subchannel_id schid)
819 if (!console_subchannel_in_use)
820 return 0;
821 return schid_equal(&schid, &console_subchannel.schid);
824 struct subchannel *
825 cio_get_console_subchannel(void)
827 if (!console_subchannel_in_use)
828 return NULL;
829 return &console_subchannel;
832 #endif
833 static int
834 __disable_subchannel_easy(struct subchannel_id schid, struct schib *schib)
836 int retry, cc;
838 cc = 0;
839 for (retry=0;retry<3;retry++) {
840 schib->pmcw.ena = 0;
841 cc = msch(schid, schib);
842 if (cc)
843 return (cc==3?-ENODEV:-EBUSY);
844 stsch(schid, schib);
845 if (!schib->pmcw.ena)
846 return 0;
848 return -EBUSY; /* uhm... */
851 /* we can't use the normal udelay here, since it enables external interrupts */
853 static void udelay_reset(unsigned long usecs)
855 uint64_t start_cc, end_cc;
857 asm volatile ("STCK %0" : "=m" (start_cc));
858 do {
859 cpu_relax();
860 asm volatile ("STCK %0" : "=m" (end_cc));
861 } while (((end_cc - start_cc)/4096) < usecs);
864 static int
865 __clear_subchannel_easy(struct subchannel_id schid)
867 int retry;
869 if (csch(schid))
870 return -ENODEV;
871 for (retry=0;retry<20;retry++) {
872 struct tpi_info ti;
874 if (tpi(&ti)) {
875 tsch(ti.schid, (struct irb *)__LC_IRB);
876 if (schid_equal(&ti.schid, &schid))
877 return 0;
879 udelay_reset(100);
881 return -EBUSY;
884 static int pgm_check_occured;
886 static void cio_reset_pgm_check_handler(void)
888 pgm_check_occured = 1;
891 static int stsch_reset(struct subchannel_id schid, volatile struct schib *addr)
893 int rc;
895 pgm_check_occured = 0;
896 s390_base_pgm_handler_fn = cio_reset_pgm_check_handler;
897 rc = stsch(schid, addr);
898 s390_base_pgm_handler_fn = NULL;
900 /* The program check handler could have changed pgm_check_occured. */
901 barrier();
903 if (pgm_check_occured)
904 return -EIO;
905 else
906 return rc;
909 static int __shutdown_subchannel_easy(struct subchannel_id schid, void *data)
911 struct schib schib;
913 if (stsch_reset(schid, &schib))
914 return -ENXIO;
915 if (!schib.pmcw.ena)
916 return 0;
917 switch(__disable_subchannel_easy(schid, &schib)) {
918 case 0:
919 case -ENODEV:
920 break;
921 default: /* -EBUSY */
922 if (__clear_subchannel_easy(schid))
923 break; /* give up... */
924 stsch(schid, &schib);
925 __disable_subchannel_easy(schid, &schib);
927 return 0;
930 static atomic_t chpid_reset_count;
932 static void s390_reset_chpids_mcck_handler(void)
934 struct crw crw;
935 struct mci *mci;
937 /* Check for pending channel report word. */
938 mci = (struct mci *)&S390_lowcore.mcck_interruption_code;
939 if (!mci->cp)
940 return;
941 /* Process channel report words. */
942 while (stcrw(&crw) == 0) {
943 /* Check for responses to RCHP. */
944 if (crw.slct && crw.rsc == CRW_RSC_CPATH)
945 atomic_dec(&chpid_reset_count);
949 #define RCHP_TIMEOUT (30 * USEC_PER_SEC)
950 static void css_reset(void)
952 int i, ret;
953 unsigned long long timeout;
954 struct chp_id chpid;
956 /* Reset subchannels. */
957 for_each_subchannel(__shutdown_subchannel_easy, NULL);
958 /* Reset channel paths. */
959 s390_base_mcck_handler_fn = s390_reset_chpids_mcck_handler;
960 /* Enable channel report machine checks. */
961 __ctl_set_bit(14, 28);
962 /* Temporarily reenable machine checks. */
963 local_mcck_enable();
964 chp_id_init(&chpid);
965 for (i = 0; i <= __MAX_CHPID; i++) {
966 chpid.id = i;
967 ret = rchp(chpid);
968 if ((ret == 0) || (ret == 2))
970 * rchp either succeeded, or another rchp is already
971 * in progress. In either case, we'll get a crw.
973 atomic_inc(&chpid_reset_count);
975 /* Wait for machine check for all channel paths. */
976 timeout = get_clock() + (RCHP_TIMEOUT << 12);
977 while (atomic_read(&chpid_reset_count) != 0) {
978 if (get_clock() > timeout)
979 break;
980 cpu_relax();
982 /* Disable machine checks again. */
983 local_mcck_disable();
984 /* Disable channel report machine checks. */
985 __ctl_clear_bit(14, 28);
986 s390_base_mcck_handler_fn = NULL;
989 static struct reset_call css_reset_call = {
990 .fn = css_reset,
993 static int __init init_css_reset_call(void)
995 atomic_set(&chpid_reset_count, 0);
996 register_reset_call(&css_reset_call);
997 return 0;
1000 arch_initcall(init_css_reset_call);
1002 struct sch_match_id {
1003 struct subchannel_id schid;
1004 struct ccw_dev_id devid;
1005 int rc;
1008 static int __reipl_subchannel_match(struct subchannel_id schid, void *data)
1010 struct schib schib;
1011 struct sch_match_id *match_id = data;
1013 if (stsch_reset(schid, &schib))
1014 return -ENXIO;
1015 if ((schib.pmcw.st == SUBCHANNEL_TYPE_IO) && schib.pmcw.dnv &&
1016 (schib.pmcw.dev == match_id->devid.devno) &&
1017 (schid.ssid == match_id->devid.ssid)) {
1018 match_id->schid = schid;
1019 match_id->rc = 0;
1020 return 1;
1022 return 0;
1025 static int reipl_find_schid(struct ccw_dev_id *devid,
1026 struct subchannel_id *schid)
1028 struct sch_match_id match_id;
1030 match_id.devid = *devid;
1031 match_id.rc = -ENODEV;
1032 for_each_subchannel(__reipl_subchannel_match, &match_id);
1033 if (match_id.rc == 0)
1034 *schid = match_id.schid;
1035 return match_id.rc;
1038 extern void do_reipl_asm(__u32 schid);
1040 /* Make sure all subchannels are quiet before we re-ipl an lpar. */
1041 void reipl_ccw_dev(struct ccw_dev_id *devid)
1043 struct subchannel_id schid;
1045 s390_reset_system();
1046 if (reipl_find_schid(devid, &schid) != 0)
1047 panic("IPL Device not found\n");
1048 do_reipl_asm(*((__u32*)&schid));
1051 int __init cio_get_iplinfo(struct cio_iplinfo *iplinfo)
1053 struct subchannel_id schid;
1054 struct schib schib;
1056 schid = *(struct subchannel_id *)__LC_SUBCHANNEL_ID;
1057 if (!schid.one)
1058 return -ENODEV;
1059 if (stsch(schid, &schib))
1060 return -ENODEV;
1061 if (schib.pmcw.st != SUBCHANNEL_TYPE_IO)
1062 return -ENODEV;
1063 if (!schib.pmcw.dnv)
1064 return -ENODEV;
1065 iplinfo->devno = schib.pmcw.dev;
1066 iplinfo->is_qdio = schib.pmcw.qf;
1067 return 0;