2 * drivers/s390/cio/cio.c
3 * S/390 common I/O routines -- low level i/o calls
5 * Copyright (C) 1999-2002 IBM Deutschland Entwicklung GmbH,
7 * Author(s): Ingo Adlung (adlung@de.ibm.com)
8 * Cornelia Huck (cornelia.huck@de.ibm.com)
9 * Arnd Bergmann (arndb@de.ibm.com)
10 * Martin Schwidefsky (schwidefsky@de.ibm.com)
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/device.h>
17 #include <linux/kernel_stat.h>
18 #include <linux/interrupt.h>
19 #include <linux/vs_context.h>
22 #include <asm/delay.h>
30 #include "blacklist.h"
31 #include "cio_debug.h"
33 debug_info_t *cio_debug_msg_id;
34 debug_info_t *cio_debug_trace_id;
35 debug_info_t *cio_debug_crw_id;
40 cio_setup (char *parm)
42 if (!strcmp (parm, "yes"))
44 else if (!strcmp (parm, "no"))
47 printk (KERN_ERR "cio_setup : invalid cio_msg parameter '%s'",
52 __setup ("cio_msg=", cio_setup);
55 * Function: cio_debug_init
56 * Initializes three debug logs (under /proc/s390dbf) for common I/O:
57 * - cio_msg logs the messages which are printk'ed when CONFIG_DEBUG_IO is on
58 * - cio_trace logs the calling of different functions
59 * - cio_crw logs the messages which are printk'ed when CONFIG_DEBUG_CRW is on
60 * debug levels depend on CONFIG_DEBUG_IO resp. CONFIG_DEBUG_CRW
65 cio_debug_msg_id = debug_register ("cio_msg", 16, 4, 16*sizeof (long));
66 if (!cio_debug_msg_id)
68 debug_register_view (cio_debug_msg_id, &debug_sprintf_view);
69 debug_set_level (cio_debug_msg_id, 2);
70 cio_debug_trace_id = debug_register ("cio_trace", 16, 4, 16);
71 if (!cio_debug_trace_id)
73 debug_register_view (cio_debug_trace_id, &debug_hex_ascii_view);
74 debug_set_level (cio_debug_trace_id, 2);
75 cio_debug_crw_id = debug_register ("cio_crw", 4, 4, 16*sizeof (long));
76 if (!cio_debug_crw_id)
78 debug_register_view (cio_debug_crw_id, &debug_sprintf_view);
79 debug_set_level (cio_debug_crw_id, 2);
80 pr_debug("debugging initialized\n");
85 debug_unregister (cio_debug_msg_id);
86 if (cio_debug_trace_id)
87 debug_unregister (cio_debug_trace_id);
89 debug_unregister (cio_debug_crw_id);
90 pr_debug("could not initialize debugging\n");
94 arch_initcall (cio_debug_init);
97 cio_set_options (struct subchannel *sch, int flags)
99 sch->options.suspend = (flags & DOIO_ALLOW_SUSPEND) != 0;
100 sch->options.prefetch = (flags & DOIO_DENY_PREFETCH) != 0;
101 sch->options.inter = (flags & DOIO_SUPPRESS_INTER) != 0;
105 /* FIXME: who wants to use this? */
107 cio_get_options (struct subchannel *sch)
112 if (sch->options.suspend)
113 flags |= DOIO_ALLOW_SUSPEND;
114 if (sch->options.prefetch)
115 flags |= DOIO_DENY_PREFETCH;
116 if (sch->options.inter)
117 flags |= DOIO_SUPPRESS_INTER;
122 * Use tpi to get a pending interrupt, call the interrupt handler and
123 * return a pointer to the subchannel structure.
128 struct tpi_info *tpi_info;
129 struct subchannel *sch;
132 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
135 irb = (struct irb *) __LC_IRB;
136 /* Store interrupt response block to lowcore. */
137 if (tsch (tpi_info->schid, irb) != 0)
138 /* Not status pending or not operational. */
140 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
145 spin_lock(&sch->lock);
146 memcpy (&sch->schib.scsw, &irb->scsw, sizeof (struct scsw));
147 if (sch->driver && sch->driver->irq)
148 sch->driver->irq(&sch->dev);
149 spin_unlock(&sch->lock);
156 cio_start_handle_notoper(struct subchannel *sch, __u8 lpm)
165 stsch (sch->schid, &sch->schib);
167 CIO_MSG_EVENT(0, "cio_start: 'not oper' status for "
168 "subchannel 0.%x.%04x!\n", sch->schid.ssid,
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_key (struct subchannel *sch, /* subchannel structure */
179 struct ccw1 * cpa, /* logical channel prog addr */
180 __u8 lpm, /* logical path mask */
181 __u8 key) /* storage key */
186 CIO_TRACE_EVENT (4, "stIO");
187 CIO_TRACE_EVENT (4, sch->dev.bus_id);
189 /* sch is always under 2G. */
190 sch->orb.intparm = (__u32)(unsigned long)sch;
193 sch->orb.pfch = sch->options.prefetch == 0;
194 sch->orb.spnd = sch->options.suspend;
195 sch->orb.ssic = sch->options.suspend && sch->options.inter;
196 sch->orb.lpm = (lpm != 0) ? (lpm & sch->opm) : sch->lpm;
199 * for 64 bit we always support 64 bit IDAWs with 4k page size only
204 sch->orb.key = key >> 4;
205 /* issue "Start Subchannel" */
206 sch->orb.cpa = (__u32) __pa (cpa);
207 ccode = ssch (sch->schid, &sch->orb);
209 /* process condition code */
210 sprintf (dbf_txt, "ccode:%d", ccode);
211 CIO_TRACE_EVENT (4, dbf_txt);
216 * initialize device status information
218 sch->schib.scsw.actl |= SCSW_ACTL_START_PEND;
220 case 1: /* status pending */
223 default: /* device/path not operational */
224 return cio_start_handle_notoper(sch, lpm);
229 cio_start (struct subchannel *sch, struct ccw1 *cpa, __u8 lpm)
231 return cio_start_key(sch, cpa, lpm, PAGE_DEFAULT_KEY);
235 * resume suspended I/O operation
238 cio_resume (struct subchannel *sch)
243 CIO_TRACE_EVENT (4, "resIO");
244 CIO_TRACE_EVENT (4, sch->dev.bus_id);
246 ccode = rsch (sch->schid);
248 sprintf (dbf_txt, "ccode:%d", ccode);
249 CIO_TRACE_EVENT (4, dbf_txt);
253 sch->schib.scsw.actl |= SCSW_ACTL_RESUME_PEND;
261 * useless to wait for request completion
262 * as device is no longer operational !
272 cio_halt(struct subchannel *sch)
280 CIO_TRACE_EVENT (2, "haltIO");
281 CIO_TRACE_EVENT (2, sch->dev.bus_id);
284 * Issue "Halt subchannel" and process condition code
286 ccode = hsch (sch->schid);
288 sprintf (dbf_txt, "ccode:%d", ccode);
289 CIO_TRACE_EVENT (2, dbf_txt);
293 sch->schib.scsw.actl |= SCSW_ACTL_HALT_PEND;
295 case 1: /* status pending */
298 default: /* device not operational */
304 * Clear I/O operation
307 cio_clear(struct subchannel *sch)
315 CIO_TRACE_EVENT (2, "clearIO");
316 CIO_TRACE_EVENT (2, sch->dev.bus_id);
319 * Issue "Clear subchannel" and process condition code
321 ccode = csch (sch->schid);
323 sprintf (dbf_txt, "ccode:%d", ccode);
324 CIO_TRACE_EVENT (2, dbf_txt);
328 sch->schib.scsw.actl |= SCSW_ACTL_CLEAR_PEND;
330 default: /* device not operational */
336 * Function: cio_cancel
337 * Issues a "Cancel Subchannel" on the specified subchannel
338 * Note: We don't need any fancy intparms and flags here
339 * since xsch is executed synchronously.
340 * Only for common I/O internal use as for now.
343 cio_cancel (struct subchannel *sch)
351 CIO_TRACE_EVENT (2, "cancelIO");
352 CIO_TRACE_EVENT (2, sch->dev.bus_id);
354 ccode = xsch (sch->schid);
356 sprintf (dbf_txt, "ccode:%d", ccode);
357 CIO_TRACE_EVENT (2, dbf_txt);
360 case 0: /* success */
361 /* Update information in scsw. */
362 stsch (sch->schid, &sch->schib);
364 case 1: /* status pending */
366 case 2: /* not applicable */
368 default: /* not oper */
374 * Function: cio_modify
375 * Issues a "Modify Subchannel" on the specified subchannel
378 cio_modify (struct subchannel *sch)
380 int ccode, retry, ret;
383 for (retry = 0; retry < 5; retry++) {
384 ccode = msch_err (sch->schid, &sch->schib);
385 if (ccode < 0) /* -EIO if msch gets a program check. */
388 case 0: /* successfull */
390 case 1: /* status pending */
393 udelay (100); /* allow for recovery */
396 case 3: /* not operational */
407 cio_enable_subchannel (struct subchannel *sch, unsigned int isc)
414 CIO_TRACE_EVENT (2, "ensch");
415 CIO_TRACE_EVENT (2, sch->dev.bus_id);
417 ccode = stsch (sch->schid, &sch->schib);
421 for (retry = 5, ret = 0; retry > 0; retry--) {
422 sch->schib.pmcw.ena = 1;
423 sch->schib.pmcw.isc = isc;
424 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
425 ret = cio_modify(sch);
430 * Got a program check in cio_modify. Try without
431 * the concurrent sense bit the next time.
433 sch->schib.pmcw.csense = 0;
435 stsch (sch->schid, &sch->schib);
436 if (sch->schib.pmcw.ena)
441 if (tsch(sch->schid, &irb) != 0)
445 sprintf (dbf_txt, "ret:%d", ret);
446 CIO_TRACE_EVENT (2, dbf_txt);
451 * Disable subchannel.
454 cio_disable_subchannel (struct subchannel *sch)
461 CIO_TRACE_EVENT (2, "dissch");
462 CIO_TRACE_EVENT (2, sch->dev.bus_id);
464 ccode = stsch (sch->schid, &sch->schib);
465 if (ccode == 3) /* Not operational. */
468 if (sch->schib.scsw.actl != 0)
470 * the disable function must not be called while there are
471 * requests pending for completion !
475 for (retry = 5, ret = 0; retry > 0; retry--) {
476 sch->schib.pmcw.ena = 0;
477 ret = cio_modify(sch);
482 * The subchannel is busy or status pending.
483 * We'll disable when the next interrupt was delivered
484 * via the state machine.
488 stsch (sch->schid, &sch->schib);
489 if (!sch->schib.pmcw.ena)
493 sprintf (dbf_txt, "ret:%d", ret);
494 CIO_TRACE_EVENT (2, dbf_txt);
499 * cio_validate_subchannel()
501 * Find out subchannel type and initialize struct subchannel.
503 * SUBCHANNEL_TYPE_IO for a normal io subchannel
504 * SUBCHANNEL_TYPE_CHSC for a chsc subchannel
505 * SUBCHANNEL_TYPE_MESSAGE for a messaging subchannel
506 * SUBCHANNEL_TYPE_ADM for a adm(?) subchannel
507 * -ENXIO for non-defined subchannels
508 * -ENODEV for subchannels with invalid device number or blacklisted devices
511 cio_validate_subchannel (struct subchannel *sch, struct subchannel_id schid)
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 spin_lock_init(&sch->lock);
523 mutex_init(&sch->reg_mutex);
525 /* Set a name for the subchannel */
526 snprintf (sch->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x", schid.ssid,
530 * The first subchannel that is not-operational (ccode==3)
531 * indicates that there aren't any more devices available.
532 * If stsch gets an exception, it means the current subchannel set
535 ccode = stsch_err (schid, &sch->schib);
537 return (ccode == 3) ? -ENXIO : ccode;
540 /* Copy subchannel type from path management control word. */
541 sch->st = sch->schib.pmcw.st;
544 * ... just being curious we check for non I/O subchannels
547 CIO_DEBUG(KERN_INFO, 0,
548 "Subchannel 0.%x.%04x reports "
549 "non-I/O subchannel type %04X\n",
550 sch->schid.ssid, sch->schid.sch_no, sch->st);
551 /* We stop here for non-io subchannels. */
555 /* Initialization for io subchannels. */
556 if (!sch->schib.pmcw.dnv)
557 /* io subchannel but device number is invalid. */
560 /* Devno is valid. */
561 if (is_blacklisted (sch->schid.ssid, sch->schib.pmcw.dev)) {
563 * This device must not be known to Linux. So we simply
564 * say that there is no device and return ENODEV.
566 CIO_MSG_EVENT(0, "Blacklisted device detected "
567 "at devno %04X, subchannel set %x\n",
568 sch->schib.pmcw.dev, sch->schid.ssid);
572 if (!cio_is_console(sch->schid))
573 chsc_validate_chpids(sch);
574 sch->lpm = sch->schib.pmcw.pim &
575 sch->schib.pmcw.pam &
576 sch->schib.pmcw.pom &
579 CIO_DEBUG(KERN_INFO, 0,
580 "Detected device %04x on subchannel 0.%x.%04X"
581 " - PIM = %02X, PAM = %02X, POM = %02X\n",
582 sch->schib.pmcw.dev, sch->schid.ssid,
583 sch->schid.sch_no, sch->schib.pmcw.pim,
584 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
587 * We now have to initially ...
588 * ... set "interruption subclass"
589 * ... enable "concurrent sense"
590 * ... enable "multipath mode" if more than one
591 * CHPID is available. This is done regardless
592 * whether multiple paths are available for us.
594 sch->schib.pmcw.isc = 3; /* could be smth. else */
595 sch->schib.pmcw.csense = 1; /* concurrent sense */
596 sch->schib.pmcw.ena = 0;
597 if ((sch->lpm & (sch->lpm - 1)) != 0)
598 sch->schib.pmcw.mp = 1; /* multipath mode */
603 * do_IRQ() handles all normal I/O device IRQ's (the special
604 * SMP cross-CPU interrupts have their own specific
609 do_IRQ (struct pt_regs *regs)
611 struct tpi_info *tpi_info;
612 struct subchannel *sch;
616 asm volatile ("mc 0,0");
617 if (S390_lowcore.int_clock >= S390_lowcore.jiffy_timer)
619 * Make sure that the i/o interrupt did not "overtake"
620 * the last HZ timer interrupt.
624 * Get interrupt information from lowcore
626 tpi_info = (struct tpi_info *) __LC_SUBCHANNEL_ID;
627 irb = (struct irb *) __LC_IRB;
629 kstat_cpu(smp_processor_id()).irqs[IO_INTERRUPT]++;
631 * Non I/O-subchannel thin interrupts are processed differently
633 if (tpi_info->adapter_IO == 1 &&
634 tpi_info->int_type == IO_INTERRUPT_TYPE) {
638 sch = (struct subchannel *)(unsigned long)tpi_info->intparm;
640 spin_lock(&sch->lock);
641 /* Store interrupt response block to lowcore. */
642 if (tsch (tpi_info->schid, irb) == 0 && sch) {
643 struct vx_info_save vxis;
645 /* Keep subchannel information word up to date. */
646 memcpy (&sch->schib.scsw, &irb->scsw,
648 /* Call interrupt handler if there is one. */
649 __enter_vx_admin(&vxis);
650 if (sch->driver && sch->driver->irq)
651 sch->driver->irq(&sch->dev);
652 __leave_vx_admin(&vxis);
655 spin_unlock(&sch->lock);
657 * Are more interrupts pending?
658 * If so, the tpi instruction will update the lowcore
659 * to hold the info for the next interrupt.
660 * We don't do this for VM because a tpi drops the cpu
661 * out of the sie which costs more cycles than it saves.
663 } while (!MACHINE_IS_VM && tpi (NULL) != 0);
667 #ifdef CONFIG_CCW_CONSOLE
668 static struct subchannel console_subchannel;
669 static int console_subchannel_in_use;
672 * busy wait for the next interrupt on the console
677 unsigned long cr6 __attribute__ ((aligned (8)));
678 unsigned long save_cr6 __attribute__ ((aligned (8)));
681 * before entering the spinlock we may already have
682 * processed the interrupt on a different CPU...
684 if (!console_subchannel_in_use)
687 /* disable all but isc 7 (console device) */
688 __ctl_store (save_cr6, 6, 6);
690 __ctl_load (cr6, 6, 6);
693 spin_unlock(&console_subchannel.lock);
696 spin_lock(&console_subchannel.lock);
697 } while (console_subchannel.schib.scsw.actl != 0);
699 * restore previous isc value
701 __ctl_load (save_cr6, 6, 6);
705 cio_test_for_console(struct subchannel_id schid, void *data)
707 if (stsch_err(schid, &console_subchannel.schib) != 0)
709 if (console_subchannel.schib.pmcw.dnv &&
710 console_subchannel.schib.pmcw.dev ==
712 console_irq = schid.sch_no;
713 return 1; /* found */
720 cio_get_console_sch_no(void)
722 struct subchannel_id schid;
724 init_subchannel_id(&schid);
725 if (console_irq != -1) {
726 /* VM provided us with the irq number of the console. */
727 schid.sch_no = console_irq;
728 if (stsch(schid, &console_subchannel.schib) != 0 ||
729 !console_subchannel.schib.pmcw.dnv)
731 console_devno = console_subchannel.schib.pmcw.dev;
732 } else if (console_devno != -1) {
733 /* At least the console device number is known. */
734 for_each_subchannel(cio_test_for_console, NULL);
735 if (console_irq == -1)
738 /* unlike in 2.4, we cannot autoprobe here, since
739 * the channel subsystem is not fully initialized.
740 * With some luck, the HWC console can take over */
741 printk(KERN_WARNING "No ccw console found!\n");
748 cio_probe_console(void)
751 struct subchannel_id schid;
753 if (xchg(&console_subchannel_in_use, 1) != 0)
754 return ERR_PTR(-EBUSY);
755 sch_no = cio_get_console_sch_no();
757 console_subchannel_in_use = 0;
758 return ERR_PTR(-ENODEV);
760 memset(&console_subchannel, 0, sizeof(struct subchannel));
761 init_subchannel_id(&schid);
762 schid.sch_no = sch_no;
763 ret = cio_validate_subchannel(&console_subchannel, schid);
765 console_subchannel_in_use = 0;
766 return ERR_PTR(-ENODEV);
770 * enable console I/O-interrupt subclass 7
773 console_subchannel.schib.pmcw.isc = 7;
774 console_subchannel.schib.pmcw.intparm =
775 (__u32)(unsigned long)&console_subchannel;
776 ret = cio_modify(&console_subchannel);
778 console_subchannel_in_use = 0;
781 return &console_subchannel;
785 cio_release_console(void)
787 console_subchannel.schib.pmcw.intparm = 0;
788 cio_modify(&console_subchannel);
789 ctl_clear_bit(6, 24);
790 console_subchannel_in_use = 0;
793 /* Bah... hack to catch console special sausages. */
795 cio_is_console(struct subchannel_id schid)
797 if (!console_subchannel_in_use)
799 return schid_equal(&schid, &console_subchannel.schid);
803 cio_get_console_subchannel(void)
805 if (!console_subchannel_in_use)
807 return &console_subchannel;
812 __disable_subchannel_easy(struct subchannel_id schid, struct schib *schib)
817 for (retry=0;retry<3;retry++) {
819 cc = msch(schid, schib);
821 return (cc==3?-ENODEV:-EBUSY);
823 if (!schib->pmcw.ena)
826 return -EBUSY; /* uhm... */
830 __clear_subchannel_easy(struct subchannel_id schid)
836 for (retry=0;retry<20;retry++) {
840 tsch(ti.schid, (struct irb *)__LC_IRB);
841 if (schid_equal(&ti.schid, &schid))
849 extern void do_reipl(unsigned long devno);
851 __shutdown_subchannel_easy(struct subchannel_id schid, void *data)
855 if (stsch_err(schid, &schib))
859 switch(__disable_subchannel_easy(schid, &schib)) {
863 default: /* -EBUSY */
864 if (__clear_subchannel_easy(schid))
865 break; /* give up... */
866 stsch(schid, &schib);
867 __disable_subchannel_easy(schid, &schib);
873 clear_all_subchannels(void)
876 for_each_subchannel(__shutdown_subchannel_easy, NULL);
879 /* Make sure all subchannels are quiet before we re-ipl an lpar. */
881 reipl(unsigned long devno)
883 clear_all_subchannels();
884 cio_reset_channel_paths();