2 * drivers/s390/cio/cio.c
3 * S/390 common I/O routines -- low level i/o calls
6 * Copyright (C) 1999-2002 IBM Deutschland Entwicklung GmbH,
8 * Author(s): Ingo Adlung (adlung@de.ibm.com)
9 * Cornelia Huck (cohuck@de.ibm.com)
10 * Arnd Bergmann (arndb@de.ibm.com)
11 * Martin Schwidefsky (schwidefsky@de.ibm.com)
14 #include <linux/module.h>
15 #include <linux/config.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/device.h>
19 #include <linux/kernel_stat.h>
20 #include <linux/interrupt.h>
23 #include <asm/delay.h>
31 #include "blacklist.h"
32 #include "cio_debug.h"
34 debug_info_t *cio_debug_msg_id;
35 debug_info_t *cio_debug_trace_id;
36 debug_info_t *cio_debug_crw_id;
41 cio_setup (char *parm)
43 if (!strcmp (parm, "yes"))
45 else if (!strcmp (parm, "no"))
48 printk (KERN_ERR "cio_setup : invalid cio_msg parameter '%s'",
53 __setup ("cio_msg=", cio_setup);
56 * Function: cio_debug_init
57 * Initializes three debug logs (under /proc/s390dbf) for common I/O:
58 * - cio_msg logs the messages which are printk'ed when CONFIG_DEBUG_IO is on
59 * - cio_trace logs the calling of different functions
60 * - cio_crw logs the messages which are printk'ed when CONFIG_DEBUG_CRW is on
61 * debug levels depend on CONFIG_DEBUG_IO resp. CONFIG_DEBUG_CRW
66 cio_debug_msg_id = debug_register ("cio_msg", 4, 4, 16*sizeof (long));
67 if (!cio_debug_msg_id)
69 debug_register_view (cio_debug_msg_id, &debug_sprintf_view);
70 debug_set_level (cio_debug_msg_id, 2);
71 cio_debug_trace_id = debug_register ("cio_trace", 4, 4, 8);
72 if (!cio_debug_trace_id)
74 debug_register_view (cio_debug_trace_id, &debug_hex_ascii_view);
75 debug_set_level (cio_debug_trace_id, 2);
76 cio_debug_crw_id = debug_register ("cio_crw", 2, 4, 16*sizeof (long));
77 if (!cio_debug_crw_id)
79 debug_register_view (cio_debug_crw_id, &debug_sprintf_view);
80 debug_set_level (cio_debug_crw_id, 2);
81 pr_debug("debugging initialized\n");
86 debug_unregister (cio_debug_msg_id);
87 if (cio_debug_trace_id)
88 debug_unregister (cio_debug_trace_id);
90 debug_unregister (cio_debug_crw_id);
91 pr_debug("could not initialize debugging\n");
95 arch_initcall (cio_debug_init);
98 cio_set_options (struct subchannel *sch, int flags)
100 sch->options.suspend = (flags & DOIO_ALLOW_SUSPEND) != 0;
101 sch->options.prefetch = (flags & DOIO_DENY_PREFETCH) != 0;
102 sch->options.inter = (flags & DOIO_SUPPRESS_INTER) != 0;
106 /* FIXME: who wants to use this? */
108 cio_get_options (struct subchannel *sch)
113 if (sch->options.suspend)
114 flags |= DOIO_ALLOW_SUSPEND;
115 if (sch->options.prefetch)
116 flags |= DOIO_DENY_PREFETCH;
117 if (sch->options.inter)
118 flags |= DOIO_SUPPRESS_INTER;
123 * Use tpi to get a pending interrupt, call the interrupt handler and
124 * return a pointer to the subchannel structure.
129 struct tpi_info *tpi_info;
130 struct subchannel *sch;
133 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
136 irb = (struct irb *) __LC_IRB;
137 /* Store interrupt response block to lowcore. */
138 if (tsch (tpi_info->irq, irb) != 0)
139 /* Not status pending or not operational. */
141 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
146 spin_lock(&sch->lock);
147 memcpy (&sch->schib.scsw, &irb->scsw, sizeof (struct scsw));
148 if (sch->driver && sch->driver->irq)
149 sch->driver->irq(&sch->dev);
150 spin_unlock(&sch->lock);
157 cio_start_handle_notoper(struct subchannel *sch, __u8 lpm)
166 stsch (sch->irq, &sch->schib);
168 CIO_MSG_EVENT(0, "cio_start: 'not oper' status for "
169 "subchannel %04x!\n", sch->irq);
170 sprintf(dbf_text, "no%s", sch->dev.bus_id);
171 CIO_TRACE_EVENT(0, dbf_text);
172 CIO_HEX_EVENT(0, &sch->schib, sizeof (struct schib));
174 return (sch->lpm ? -EACCES : -ENODEV);
178 cio_start (struct subchannel *sch, /* subchannel structure */
179 struct ccw1 * cpa, /* logical channel prog addr */
180 __u8 lpm) /* logical path mask */
185 CIO_TRACE_EVENT (4, "stIO");
186 CIO_TRACE_EVENT (4, sch->dev.bus_id);
188 /* sch is always under 2G. */
189 sch->orb.intparm = (__u32)(unsigned long)sch;
192 sch->orb.pfch = sch->options.prefetch == 0;
193 sch->orb.spnd = sch->options.suspend;
194 sch->orb.ssic = sch->options.suspend && sch->options.inter;
195 sch->orb.lpm = (lpm != 0) ? (lpm & sch->opm) : sch->lpm;
196 #ifdef CONFIG_ARCH_S390X
198 * for 64 bit we always support 64 bit IDAWs with 4k page size only
203 sch->orb.cpa = (__u32) __pa (cpa);
206 * Issue "Start subchannel" and process condition code
208 ccode = ssch (sch->irq, &sch->orb);
209 sprintf (dbf_txt, "ccode:%d", ccode);
210 CIO_TRACE_EVENT (4, dbf_txt);
215 * initialize device status information
217 sch->schib.scsw.actl |= SCSW_ACTL_START_PEND;
219 case 1: /* status pending */
222 default: /* device/path not operational */
223 return cio_start_handle_notoper(sch, lpm);
228 * resume suspended I/O operation
231 cio_resume (struct subchannel *sch)
236 CIO_TRACE_EVENT (4, "resIO");
237 CIO_TRACE_EVENT (4, sch->dev.bus_id);
239 ccode = rsch (sch->irq);
241 sprintf (dbf_txt, "ccode:%d", ccode);
242 CIO_TRACE_EVENT (4, dbf_txt);
246 sch->schib.scsw.actl |= SCSW_ACTL_RESUME_PEND;
254 * useless to wait for request completion
255 * as device is no longer operational !
265 cio_halt(struct subchannel *sch)
273 CIO_TRACE_EVENT (2, "haltIO");
274 CIO_TRACE_EVENT (2, sch->dev.bus_id);
277 * Issue "Halt subchannel" and process condition code
279 ccode = hsch (sch->irq);
281 sprintf (dbf_txt, "ccode:%d", ccode);
282 CIO_TRACE_EVENT (2, dbf_txt);
286 sch->schib.scsw.actl |= SCSW_ACTL_HALT_PEND;
288 case 1: /* status pending */
291 default: /* device not operational */
297 * Clear I/O operation
300 cio_clear(struct subchannel *sch)
308 CIO_TRACE_EVENT (2, "clearIO");
309 CIO_TRACE_EVENT (2, sch->dev.bus_id);
312 * Issue "Clear subchannel" and process condition code
314 ccode = csch (sch->irq);
316 sprintf (dbf_txt, "ccode:%d", ccode);
317 CIO_TRACE_EVENT (2, dbf_txt);
321 sch->schib.scsw.actl |= SCSW_ACTL_CLEAR_PEND;
323 default: /* device not operational */
329 * Function: cio_cancel
330 * Issues a "Cancel Subchannel" on the specified subchannel
331 * Note: We don't need any fancy intparms and flags here
332 * since xsch is executed synchronously.
333 * Only for common I/O internal use as for now.
336 cio_cancel (struct subchannel *sch)
344 CIO_TRACE_EVENT (2, "cancelIO");
345 CIO_TRACE_EVENT (2, sch->dev.bus_id);
347 ccode = xsch (sch->irq);
349 sprintf (dbf_txt, "ccode:%d", ccode);
350 CIO_TRACE_EVENT (2, dbf_txt);
353 case 0: /* success */
354 /* Update information in scsw. */
355 stsch (sch->irq, &sch->schib);
357 case 1: /* status pending */
359 case 2: /* not applicable */
361 default: /* not oper */
367 * Function: cio_modify
368 * Issues a "Modify Subchannel" on the specified subchannel
371 cio_modify (struct subchannel *sch)
373 int ccode, retry, ret;
376 for (retry = 0; retry < 5; retry++) {
377 ccode = msch_err (sch->irq, &sch->schib);
378 if (ccode < 0) /* -EIO if msch gets a program check. */
381 case 0: /* successfull */
383 case 1: /* status pending */
386 udelay (100); /* allow for recovery */
389 case 3: /* not operational */
400 cio_enable_subchannel (struct subchannel *sch, unsigned int isc)
407 CIO_TRACE_EVENT (2, "ensch");
408 CIO_TRACE_EVENT (2, sch->dev.bus_id);
410 ccode = stsch (sch->irq, &sch->schib);
414 for (retry = 5, ret = 0; retry > 0; retry--) {
415 sch->schib.pmcw.ena = 1;
416 sch->schib.pmcw.isc = isc;
417 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
418 ret = cio_modify(sch);
423 * Got a program check in cio_modify. Try without
424 * the concurrent sense bit the next time.
426 sch->schib.pmcw.csense = 0;
428 stsch (sch->irq, &sch->schib);
429 if (sch->schib.pmcw.ena)
434 if (tsch(sch->irq, &irb) != 0)
438 sprintf (dbf_txt, "ret:%d", ret);
439 CIO_TRACE_EVENT (2, dbf_txt);
444 * Disable subchannel.
447 cio_disable_subchannel (struct subchannel *sch)
454 CIO_TRACE_EVENT (2, "dissch");
455 CIO_TRACE_EVENT (2, sch->dev.bus_id);
457 ccode = stsch (sch->irq, &sch->schib);
458 if (ccode == 3) /* Not operational. */
461 if (sch->schib.scsw.actl != 0)
463 * the disable function must not be called while there are
464 * requests pending for completion !
468 for (retry = 5, ret = 0; retry > 0; retry--) {
469 sch->schib.pmcw.ena = 0;
470 ret = cio_modify(sch);
475 * The subchannel is busy or status pending.
476 * We'll disable when the next interrupt was delivered
477 * via the state machine.
481 stsch (sch->irq, &sch->schib);
482 if (!sch->schib.pmcw.ena)
486 sprintf (dbf_txt, "ret:%d", ret);
487 CIO_TRACE_EVENT (2, dbf_txt);
492 * cio_validate_subchannel()
494 * Find out subchannel type and initialize struct subchannel.
496 * SUBCHANNEL_TYPE_IO for a normal io subchannel
497 * SUBCHANNEL_TYPE_CHSC for a chsc subchannel
498 * SUBCHANNEL_TYPE_MESSAGE for a messaging subchannel
499 * SUBCHANNEL_TYPE_ADM for a adm(?) subchannel
500 * -ENXIO for non-defined subchannels
501 * -ENODEV for subchannels with invalid device number or blacklisted devices
504 cio_validate_subchannel (struct subchannel *sch, unsigned int irq)
509 sprintf (dbf_txt, "valsch%x", irq);
510 CIO_TRACE_EVENT (4, dbf_txt);
512 /* Nuke all fields. */
513 memset(sch, 0, sizeof(struct subchannel));
515 spin_lock_init(&sch->lock);
517 /* Set a name for the subchannel */
518 snprintf (sch->dev.bus_id, BUS_ID_SIZE, "0.0.%04x", irq);
521 * The first subchannel that is not-operational (ccode==3)
522 * indicates that there aren't any more devices available.
525 ccode = stsch (irq, &sch->schib);
529 /* Copy subchannel type from path management control word. */
530 sch->st = sch->schib.pmcw.st;
533 * ... just being curious we check for non I/O subchannels
536 CIO_DEBUG(KERN_INFO, 0,
537 "Subchannel %04X reports "
538 "non-I/O subchannel type %04X\n",
540 /* We stop here for non-io subchannels. */
544 /* Initialization for io subchannels. */
545 if (!sch->schib.pmcw.dnv)
546 /* io subchannel but device number is invalid. */
549 /* Devno is valid. */
550 if (is_blacklisted (sch->schib.pmcw.dev)) {
552 * This device must not be known to Linux. So we simply
553 * say that there is no device and return ENODEV.
555 CIO_MSG_EVENT(0, "Blacklisted device detected "
556 "at devno %04X\n", sch->schib.pmcw.dev);
560 chsc_validate_chpids(sch);
561 sch->lpm = sch->schib.pmcw.pim &
562 sch->schib.pmcw.pam &
563 sch->schib.pmcw.pom &
566 CIO_DEBUG(KERN_INFO, 0,
567 "Detected device %04X on subchannel %04X"
568 " - PIM = %02X, PAM = %02X, POM = %02X\n",
569 sch->schib.pmcw.dev, sch->irq, sch->schib.pmcw.pim,
570 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
573 * We now have to initially ...
574 * ... set "interruption subclass"
575 * ... enable "concurrent sense"
576 * ... enable "multipath mode" if more than one
577 * CHPID is available. This is done regardless
578 * whether multiple paths are available for us.
580 sch->schib.pmcw.isc = 3; /* could be smth. else */
581 sch->schib.pmcw.csense = 1; /* concurrent sense */
582 sch->schib.pmcw.ena = 0;
583 if ((sch->lpm & (sch->lpm - 1)) != 0)
584 sch->schib.pmcw.mp = 1; /* multipath mode */
589 * do_IRQ() handles all normal I/O device IRQ's (the special
590 * SMP cross-CPU interrupts have their own specific
595 do_IRQ (struct pt_regs *regs)
597 struct tpi_info *tpi_info;
598 struct subchannel *sch;
602 asm volatile ("mc 0,0");
603 if (S390_lowcore.int_clock >= S390_lowcore.jiffy_timer)
606 * Get interrupt information from lowcore
608 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
609 irb = (struct irb *) __LC_IRB;
611 kstat_cpu(smp_processor_id()).irqs[IO_INTERRUPT]++;
613 * Non I/O-subchannel thin interrupts are processed differently
615 if (tpi_info->adapter_IO == 1 &&
616 tpi_info->int_type == IO_INTERRUPT_TYPE) {
620 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
622 spin_lock(&sch->lock);
623 /* Store interrupt response block to lowcore. */
624 if (tsch (tpi_info->irq, irb) == 0 && sch) {
625 /* Keep subchannel information word up to date. */
626 memcpy (&sch->schib.scsw, &irb->scsw,
628 /* Call interrupt handler if there is one. */
629 if (sch->driver && sch->driver->irq)
630 sch->driver->irq(&sch->dev);
633 spin_unlock(&sch->lock);
635 * Are more interrupts pending?
636 * If so, the tpi instruction will update the lowcore
637 * to hold the info for the next interrupt.
638 * We don't do this for VM because a tpi drops the cpu
639 * out of the sie which costs more cycles than it saves.
641 } while (!MACHINE_IS_VM && tpi (NULL) != 0);
645 #ifdef CONFIG_CCW_CONSOLE
646 static struct subchannel console_subchannel;
647 static int console_subchannel_in_use;
650 * busy wait for the next interrupt on the console
655 unsigned long cr6 __attribute__ ((aligned (8)));
656 unsigned long save_cr6 __attribute__ ((aligned (8)));
659 * before entering the spinlock we may already have
660 * processed the interrupt on a different CPU...
662 if (!console_subchannel_in_use)
665 /* disable all but isc 7 (console device) */
666 __ctl_store (save_cr6, 6, 6);
668 __ctl_load (cr6, 6, 6);
671 spin_unlock(&console_subchannel.lock);
674 spin_lock(&console_subchannel.lock);
675 } while (console_subchannel.schib.scsw.actl != 0);
677 * restore previous isc value
679 __ctl_load (save_cr6, 6, 6);
683 cio_console_irq(void)
687 if (console_irq != -1) {
688 /* VM provided us with the irq number of the console. */
689 if (stsch(console_irq, &console_subchannel.schib) != 0 ||
690 !console_subchannel.schib.pmcw.dnv)
692 console_devno = console_subchannel.schib.pmcw.dev;
693 } else if (console_devno != -1) {
694 /* At least the console device number is known. */
695 for (irq = 0; irq < __MAX_SUBCHANNELS; irq++) {
696 if (stsch(irq, &console_subchannel.schib) != 0)
698 if (console_subchannel.schib.pmcw.dnv &&
699 console_subchannel.schib.pmcw.dev ==
705 if (console_irq == -1)
708 /* unlike in 2.4, we cannot autoprobe here, since
709 * the channel subsystem is not fully initialized.
710 * With some luck, the HWC console can take over */
711 printk(KERN_WARNING "No ccw console found!\n");
718 cio_probe_console(void)
722 if (xchg(&console_subchannel_in_use, 1) != 0)
723 return ERR_PTR(-EBUSY);
724 irq = cio_console_irq();
726 console_subchannel_in_use = 0;
727 return ERR_PTR(-ENODEV);
729 memset(&console_subchannel, 0, sizeof(struct subchannel));
730 ret = cio_validate_subchannel(&console_subchannel, irq);
732 console_subchannel_in_use = 0;
733 return ERR_PTR(-ENODEV);
737 * enable console I/O-interrupt subclass 7
740 console_subchannel.schib.pmcw.isc = 7;
741 console_subchannel.schib.pmcw.intparm =
742 (__u32)(unsigned long)&console_subchannel;
743 ret = cio_modify(&console_subchannel);
745 console_subchannel_in_use = 0;
748 return &console_subchannel;
752 cio_release_console(void)
754 console_subchannel.schib.pmcw.intparm = 0;
755 cio_modify(&console_subchannel);
756 ctl_clear_bit(6, 24);
757 console_subchannel_in_use = 0;
760 /* Bah... hack to catch console special sausages. */
762 cio_is_console(int irq)
764 if (!console_subchannel_in_use)
766 return (irq == console_subchannel.irq);
770 cio_get_console_subchannel(void)
772 if (!console_subchannel_in_use)
774 return &console_subchannel;
779 __disable_subchannel_easy(unsigned int schid, struct schib *schib)
784 for (retry=0;retry<3;retry++) {
786 cc = msch(schid, schib);
788 return (cc==3?-ENODEV:-EBUSY);
790 if (!schib->pmcw.ena)
793 return -EBUSY; /* uhm... */
797 __clear_subchannel_easy(unsigned int schid)
803 for (retry=0;retry<20;retry++) {
807 tsch(schid, (struct irb *)__LC_IRB);
815 extern void do_reipl(unsigned long devno);
816 /* Make sure all subchannels are quiet before we re-ipl an lpar. */
818 reipl(unsigned long devno)
823 for (schid=0;schid<=highest_subchannel;schid++) {
825 if (stsch(schid, &schib))
829 switch(__disable_subchannel_easy(schid, &schib)) {
833 default: /* -EBUSY */
834 if (__clear_subchannel_easy(schid))
835 break; /* give up... */
836 stsch(schid, &schib);
837 __disable_subchannel_easy(schid, &schib);