changeset 45:9cec88f4c98b

Merged changes in
author Jonathan Pevarnek <pevarnj@gmail.com>
date Fri, 08 Apr 2011 09:56:20 -0400
parents 03c6618b2c24 (current diff) 3d69c66b2610 (diff)
children a6be89bc4b04
files .hgignore Makefile arch/io.c include/system.h src/dynamic.c src/fs.c src/init.c src/sarpn.c
diffstat 14 files changed, 750 insertions(+), 686 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Fri Apr 08 09:46:23 2011 -0400
+++ b/.hgignore	Fri Apr 08 09:56:20 2011 -0400
@@ -1,4 +1,5 @@
 \.o$
+\.a$
 \.rto$
 ^ipl/ipl_ccws\.S$
 ^sarpn$
--- a/Makefile	Fri Apr 08 09:46:23 2011 -0400
+++ b/Makefile	Fri Apr 08 09:56:20 2011 -0400
@@ -3,6 +3,7 @@
 CC=$(CROSS_COMPILE)gcc
 CXX=$(CROSS_COMPILE)g++
 LD=$(CROSS_COMPILE)ld
+AR=$(CROSS_COMPILE)ar
 OBJCOPY=$(CROSS_COMPILE)objcopy
 
 MAKEFLAGS += -rR --no-print-directory
@@ -12,9 +13,11 @@
 
 BINS=sarpn dynamic fs
 
-sarpn_OBJS=src/sarpn.o arch/io.o src/std.o src/stack.o src/operations.o src/math.o
-dynamic_OBJS=src/dynamic.o arch/io.o src/std.o src/stack.o
-fs_OBJS=src/fs.o arch/io.o src/std.o
+sarpn_OBJS=src/sarpn.o arch/arch.a src/std.o src/stack.o src/operations.o src/math.o
+dynamic_OBJS=src/dynamic.o arch/arch.a src/std.o src/stack.o
+fs_OBJS=src/fs.o arch/arch.a src/std.o
+
+ARCH_OBJS=arch/io.o arch/cons.o arch/ebcdic.o arch/fba.o arch/ioint.o
 
 .PHONY: all build clean tags
 
@@ -25,6 +28,8 @@
 clean:
 	rm -f $(sarpn_OBJS)
 	rm -f $(dynamic_OBJS)
+	rm -f $(fs_OBJS)
+	rm -f $(ARCH_OBJS)
 	rm -f $(BINS)
 	rm -f loader.bin ipl/*.o ipl/*.rto ipl/ipl_ccws.S cscope.out
 
@@ -39,6 +44,9 @@
 fs: $(fs_OBJS)
 	$(LD) $(LDFLAGS) -T scripts/linker.script -o $@ $^
 
+arch/arch.a: $(ARCH_OBJS)
+	$(AR) rc $@ $^
+
 %.o: %.S
 	$(AS) -m64 -o $@ $<
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/arch/channel.h	Fri Apr 08 09:56:20 2011 -0400
@@ -0,0 +1,224 @@
+#ifndef __CHANNEL_H
+#define __CHANNEL_H
+
+struct pmcw {
+	/* word 0 */
+	u32 interrupt_param;	/* Interruption Parameter */
+
+	/* word 1*/
+	u8 __zero1:2,
+	   isc:3,		/* I/O-Interruption-Subclass Code */
+	   __zero2:3;
+	u8 e:1,			/* Enabled */
+	   lm:2,		/* Limit Mode */
+	   mm:2,		/* Measurement-Mode Enable */
+	   d:1,			/* Multipath Mode */
+	   t:1,			/* Timing Facility */
+	   v:1;			/* Device Number Valid */
+	u16 dev_num;		/* Device Number */
+
+	/* word 2 */
+	u8 lpm;			/* Logical-Path Mask */
+	u8 pnom;		/* Path-Not-Operational Mask */
+	u8 lpum;		/* Last-Path-Used Mask */
+	u8 pim;			/* Path-Installed Mask */
+
+	/* word 3 */
+	u16 mbi;		/* Measurement-Block Index */
+	u8 pom;			/* Path-Operational Mask */
+	u8 pam;			/* Path-Available Mask */
+
+	/* word 4 & 5 */
+	u8 chpid[8];		/* Channel-Path Identifiers */
+
+	/* word 6 */
+	u16 __zero3;
+	u16 __zero4:13,
+	    f:1,		/* Measurement Block Format Control */
+	    x:1,		/* Extended Measurement Word Mode Enable */
+	    s:1;		/* Concurrent Sense */
+};
+
+struct scsw {
+	/* word 0 */
+	u16 key:4,		/* Subchannel key */
+	    s:1,		/* Suspend control */
+	    l:1,		/* ESW format */
+	    cc:2,		/* Deferred condition code */
+	    f:1,		/* Format */
+	    p:1,		/* Prefetch */
+	    i:1,		/* Initial-status interruption control */
+	    a:1,		/* Address-limit-checking control */
+	    u:1,		/* Supress-suspended interruption */
+	    z:1,		/* Zero condition code */
+	    e:1,		/* Extended control */
+	    n:1;		/* Path no operational */
+	u16 __zero:1,
+	    fc:3,		/* Function control */
+	    ac:7,		/* Activity control */
+	    sc:5;		/* Status control */
+
+	/* word 1 */
+	u32 addr;		/* CCW Address */
+
+	/* word 2 */
+	u8 dev_status;		/* Device status */
+	u8 sch_status;		/* Subchannel status */
+	u16 count;		/* Count */
+} __attribute__((packed));
+
+struct schib {
+	struct pmcw pmcw;               /* Path Management Control Word */
+	struct scsw scsw;               /* Subchannel Status Word */
+	u32 measure_block_1;
+	u32 measure_block_2;
+	u32 model_dep_area;
+} __attribute__((packed,aligned(4)));
+
+struct orb {
+	/* word 0 */
+	u32 param;		/* Interruption Parameter */
+
+	/* word 1 */
+	u8 key:4,		/* Subchannel Key */
+	   s:1,			/* Suspend */
+	   c:1,			/* Streaming-Mode Control */
+	   m:1,			/* Modification Control */
+	   y:1;			/* Synchronization Control */
+	u8 f:1,			/* Format Control */
+	   p:1,			/* Prefetch Control */
+	   i:1,			/* Initial-Status-Interruption Control */
+	   a:1,			/* Address-Limit-Checking control */
+	   u:1,			/* Suppress-Suspend-Interruption Control */
+	   __zero1:1,
+	   h:1,			/* Format-2-IDAW Control */
+	   t:1;			/* 2K-IDAW Control */
+	u8 lpm;			/* Logical-Path Mask */
+	u8 l:1,			/* Incorrect-Length-Suppression Mode */
+	   d:1,			/* Modified-CCW-Indirect-Data-Addressing Control */
+	   __zero2:5,
+	   x:1;			/* ORB-Extension Control */
+
+	/* word 2 */
+	u32 addr;		/* Channel-Program Address */
+
+	/* word 3 */
+	u8 css_prio;		/* Channel-Subsystem Priority */
+	u8 __reserved1;
+	u8 cu_prio;		/* Control-Unit Priority */
+	u8 __reserved2;
+
+	/* word 4 - 7 */
+	u32 __reserved3;
+	u32 __reserved4;
+	u32 __reserved5;
+	u32 __reserved6;
+} __attribute__((packed,aligned(4)));
+
+struct ccw {
+	u8 cmd;			/* Command code */
+	u8 flags;		/* Flags */
+	u16 count;		/* Count */
+	u32 addr;		/* Data Address */
+} __attribute__((packed,aligned(8)));
+
+struct psw {
+	u8 _zero0:1,
+	   r:1,			/* PER Mask (R)			*/
+	   _zero1:3,
+	   t:1,			/* DAT Mode (T)			*/
+	   io:1,		/* I/O Mask (IO)		*/
+	   ex:1;		/* External Mask (EX)		*/
+
+	u8 key:4,		/* Key				*/
+	   _zero2:1,
+	   m:1,			/* Machine-Check Mask (M)	*/
+	   w:1,			/* Wait State (W)		*/
+	   p:1;			/* Problem State (P)		*/
+
+	u8 as:2,		/* Address-Space Control (AS)	*/
+	   cc:2,		/* Condition Code (CC)		*/
+	   prog_mask:4;		/* Program Mask			*/
+
+	u8 _zero3:7,
+	   ea:1;		/* Extended Addressing (EA)	*/
+
+	u32 ba:1,		/* Basic Addressing (BA)	*/
+	    _zero4:31;
+
+	u64 ptr;
+};
+
+struct irb {
+	struct scsw scsw;			/* Subchannel-Status */
+	u32 w0a, w1a, w2a, w3a, w4a;
+	u32 w0b, w1b, w2b, w3b, w4b, w5b, w6b, w7b;
+	u32 w0c, w1c, w2c, w3c, w4c, w5c, w6c, w7c;
+} __attribute__((packed,aligned(4)));
+
+static inline int store_sch(u32 sch, struct schib *schib)
+{
+	int cc;
+
+	asm volatile(
+		"lr	%%r1,%2\n"
+		"stsch	%1\n"
+		"ipm	%0\n"
+		"srl	%0,28\n"
+		: /* output */
+		  "=d" (cc),
+		  "=Q" (*schib)
+		: /* input */
+		  "d" (sch)
+		: /* clobbered */
+		  "cc", "r1", "memory"
+	);
+
+	return cc;
+}
+
+static inline int modify_sch(u32 sch, struct schib *schib)
+{
+	int cc;
+
+	asm volatile(
+		"lr	%%r1,%1\n"
+		"msch	0(%2)\n"
+		"ipm	%0\n"
+		"srl	%0,28\n"
+		: /* output */
+		  "=d" (cc)
+		: /* input */
+		  "d" (sch),
+		  "a" (schib)
+		: /* clobbered */
+		  "cc", "r1"
+	);
+
+	return cc;
+}
+
+static inline int start_sch(u32 sch, struct orb *orb)
+{
+	int cc;
+
+	asm volatile(
+		"	lr	%%r1,%1\n"
+		"	ssch	0(%2)\n"
+		"	ipm	%0\n"
+		"	srl	%0,28\n"
+		: /* output */
+		  "=d" (cc)
+		: /* input */
+		  "d" (sch),
+		  "a" (orb)
+		: /* clobbered */
+		  "cc", "r1"
+	);
+
+	return cc;
+}
+
+extern void wait_for_io_int();
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/arch/cons.c	Fri Apr 08 09:56:20 2011 -0400
@@ -0,0 +1,87 @@
+#include "channel.h"
+#include "ebcdic.h"
+
+#include <die.h>
+
+#define CON_LEN 132
+
+static u32 consch;
+
+int putline(char *buf, int len)
+{
+	char data[CON_LEN];
+	struct orb orb;
+	struct ccw ccw;
+
+	if ((len <= 0) || (len > CON_LEN))
+		return -1;
+
+	__builtin_memcpy(data, buf, len);
+	ascii2ebcdic((u8*) data, len);
+
+	ccw.cmd   = 0x01;
+	ccw.flags = 0;
+	ccw.count = len;
+	ccw.addr  = (u32) (u64) data;
+
+	__builtin_memset(&orb, 0, sizeof(struct orb));
+	orb.lpm   = 0xff;
+	orb.addr  = (u32) (u64) &ccw;
+	orb.f     = 1;
+
+	if (start_sch(consch, &orb))
+		die();
+
+	wait_for_io_int();
+
+	return len;
+}
+
+int getline(char *buf, int len)
+{
+	struct orb orb;
+	struct ccw ccw;
+	int i;
+
+	if ((len <= 0) || (len > CON_LEN))
+		return -1;
+
+	wait_for_io_int();
+
+	__builtin_memset(buf, 0, len);
+	ccw.cmd   = 0x0a;
+	ccw.flags = 0x20;
+	ccw.count = len;
+	ccw.addr  = (u32) (u64) buf;
+
+	__builtin_memset(&orb, 0, sizeof(struct orb));
+	orb.lpm   = 0xff;
+	orb.addr  = (u32) (u64) &ccw;
+	orb.f     = 1;
+
+	if (start_sch(consch, &orb))
+		die();
+
+	wait_for_io_int();
+
+	for(i=0; i<len; i++)
+		if (!buf[i])
+			break;
+
+	if (i)
+		ebcdic2ascii((u8*) buf, i);
+
+	return i;
+}
+
+void init_console()
+{
+	u32 sch;
+
+	sch = find_dev(CON_DEV);
+
+	if (!sch)
+		die();
+
+	consch = sch;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/arch/ebcdic.c	Fri Apr 08 09:56:20 2011 -0400
@@ -0,0 +1,172 @@
+/*
+ * Tables taken from Linux's arch/s390/kernel/ebcdic.c
+ */
+
+/*
+ * ASCII (IBM PC 437)  -> EBCDIC 037
+ */
+u8 ascii2ebcdic_table[256] =
+{
+ /*00 NUL   SOH   STX   ETX   EOT   ENQ   ACK   BEL */
+     0x00, 0x01, 0x02, 0x03, 0x37, 0x2D, 0x2E, 0x2F,
+ /*08  BS    HT    LF    VT    FF    CR    SO    SI */
+ /*              ->NL                               */
+     0x16, 0x05, 0x15, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
+ /*10 DLE   DC1   DC2   DC3   DC4   NAK   SYN   ETB */
+     0x10, 0x11, 0x12, 0x13, 0x3C, 0x3D, 0x32, 0x26,
+ /*18 CAN    EM   SUB   ESC    FS    GS    RS    US */
+ /*                               ->IGS ->IRS ->IUS */
+     0x18, 0x19, 0x3F, 0x27, 0x22, 0x1D, 0x1E, 0x1F,
+ /*20  SP     !     "     #     $     %     &     ' */
+     0x40, 0x5A, 0x7F, 0x7B, 0x5B, 0x6C, 0x50, 0x7D,
+ /*28   (     )     *     +     ,     -    .      / */
+     0x4D, 0x5D, 0x5C, 0x4E, 0x6B, 0x60, 0x4B, 0x61,
+ /*30   0     1     2     3     4     5     6     7 */
+     0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
+ /*38   8     9     :     ;     <     =     >     ? */
+     0xF8, 0xF9, 0x7A, 0x5E, 0x4C, 0x7E, 0x6E, 0x6F,
+ /*40   @     A     B     C     D     E     F     G */
+     0x7C, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
+ /*48   H     I     J     K     L     M     N     O */
+     0xC8, 0xC9, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6,
+ /*50   P     Q     R     S     T     U     V     W */
+     0xD7, 0xD8, 0xD9, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6,
+ /*58   X     Y     Z     [     \     ]     ^     _ */
+     0xE7, 0xE8, 0xE9, 0xBA, 0xE0, 0xBB, 0xB0, 0x6D,
+ /*60   `     a     b     c     d     e     f     g */
+     0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
+ /*68   h     i     j     k     l     m     n     o */
+     0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
+ /*70   p     q     r     s     t     u     v     w */
+     0x97, 0x98, 0x99, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
+ /*78   x     y     z     {     |     }     ~    DL */
+     0xA7, 0xA8, 0xA9, 0xC0, 0x4F, 0xD0, 0xA1, 0x07,
+ /*80*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*88*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*90*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*98*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*A0*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*A8*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*B0*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*B8*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*C0*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*C8*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*D0*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*D8*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*E0        sz						*/
+     0x3F, 0x59, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*E8*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*F0*/
+     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
+ /*F8*/
+     0x90, 0x3F, 0x3F, 0x3F, 0x3F, 0xEA, 0x3F, 0xFF
+};
+
+/*
+ * EBCDIC 037 -> ASCII (IBM PC 437)
+ */
+u8 ebcdic2ascii_table[256] =
+{
+ /* 0x00   NUL   SOH   STX   ETX  *SEL    HT  *RNL   DEL */
+          0x00, 0x01, 0x02, 0x03, 0x07, 0x09, 0x07, 0x7F,
+ /* 0x08   -GE  -SPS  -RPT    VT    FF    CR    SO    SI */
+          0x07, 0x07, 0x07, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
+ /* 0x10   DLE   DC1   DC2   DC3  -RES   -NL    BS  -POC
+                                  -ENP  ->LF             */
+          0x10, 0x11, 0x12, 0x13, 0x07, 0x0A, 0x08, 0x07,
+ /* 0x18   CAN    EM  -UBS  -CU1  -IFS  -IGS  -IRS  -ITB
+                                                    -IUS */
+          0x18, 0x19, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
+ /* 0x20   -DS  -SOS    FS  -WUS  -BYP    LF   ETB   ESC
+                                  -INP                   */
+          0x07, 0x07, 0x1C, 0x07, 0x07, 0x0A, 0x17, 0x1B,
+ /* 0x28   -SA  -SFE   -SM  -CSP  -MFA   ENQ   ACK   BEL
+                       -SW                               */
+          0x07, 0x07, 0x07, 0x07, 0x07, 0x05, 0x06, 0x07,
+ /* 0x30  ----  ----   SYN   -IR   -PP  -TRN  -NBS   EOT */
+          0x07, 0x07, 0x16, 0x07, 0x07, 0x07, 0x07, 0x04,
+ /* 0x38  -SBS   -IT  -RFF  -CU3   DC4   NAK  ----   SUB */
+          0x07, 0x07, 0x07, 0x07, 0x14, 0x15, 0x07, 0x1A,
+ /* 0x40    SP   RSP           ä              ----       */
+          0x20, 0xFF, 0x83, 0x84, 0x85, 0xA0, 0x07, 0x86,
+ /* 0x48                       .     <     (     +     | */
+          0x87, 0xA4, 0x9B, 0x2E, 0x3C, 0x28, 0x2B, 0x7C,
+ /* 0x50     &                                      ---- */
+          0x26, 0x82, 0x88, 0x89, 0x8A, 0xA1, 0x8C, 0x07,
+ /* 0x58           ß     !     $     *     )     ;       */
+          0x8D, 0xE1, 0x21, 0x24, 0x2A, 0x29, 0x3B, 0xAA,
+ /* 0x60     -     /  ----     Ä  ----  ----  ----       */
+          0x2D, 0x2F, 0x07, 0x8E, 0x07, 0x07, 0x07, 0x8F,
+ /* 0x68              ----     ,     %     _     >     ? */
+          0x80, 0xA5, 0x07, 0x2C, 0x25, 0x5F, 0x3E, 0x3F,
+ /* 0x70  ----        ----  ----  ----  ----  ----  ---- */
+          0x07, 0x90, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
+ /* 0x78     *     `     :     #     @     '     =     " */
+          0x70, 0x60, 0x3A, 0x23, 0x40, 0x27, 0x3D, 0x22,
+ /* 0x80     *     a     b     c     d     e     f     g */
+          0x07, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
+ /* 0x88     h     i              ----  ----  ----       */
+          0x68, 0x69, 0xAE, 0xAF, 0x07, 0x07, 0x07, 0xF1,
+ /* 0x90     °     j     k     l     m     n     o     p */
+          0xF8, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70,
+ /* 0x98     q     r                    ----        ---- */
+          0x71, 0x72, 0xA6, 0xA7, 0x91, 0x07, 0x92, 0x07,
+ /* 0xA0           ~     s     t     u     v     w     x */
+          0xE6, 0x7E, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
+ /* 0xA8     y     z              ----  ----  ----  ---- */
+          0x79, 0x7A, 0xAD, 0xAB, 0x07, 0x07, 0x07, 0x07,
+ /* 0xB0     ^                    ----     §  ----       */
+          0x5E, 0x9C, 0x9D, 0xFA, 0x07, 0x07, 0x07, 0xAC,
+ /* 0xB8        ----     [     ]  ----  ----  ----  ---- */
+          0xAB, 0x07, 0x5B, 0x5D, 0x07, 0x07, 0x07, 0x07,
+ /* 0xC0     {     A     B     C     D     E     F     G */
+          0x7B, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
+ /* 0xC8     H     I  ----           ö              ---- */
+          0x48, 0x49, 0x07, 0x93, 0x94, 0x95, 0xA2, 0x07,
+ /* 0xD0     }     J     K     L     M     N     O     P */
+          0x7D, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
+ /* 0xD8     Q     R  ----           ü                   */
+          0x51, 0x52, 0x07, 0x96, 0x81, 0x97, 0xA3, 0x98,
+ /* 0xE0     \           S     T     U     V     W     X */
+          0x5C, 0xF6, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
+ /* 0xE8     Y     Z        ----     Ö  ----  ----  ---- */
+          0x59, 0x5A, 0xFD, 0x07, 0x99, 0x07, 0x07, 0x07,
+ /* 0xF0     0     1     2     3     4     5     6     7 */
+          0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
+ /* 0xF8     8     9  ----  ----     Ü  ----  ----  ---- */
+          0x38, 0x39, 0x07, 0x07, 0x9A, 0x07, 0x07, 0x07
+};
+
+/*
+ * Generic translate buffer function.
+ */
+void __translate(u8 *buf, int len, const u8 *table)
+{
+	asm volatile(
+		"	sr	%%r0,%%r0\n"		/* test byte = 0 */
+		"	la	%%r2,0(%0)\n"		/* buffer */
+		"	lr	%%r3,%2\n"		/* length */
+		"	la	%%r4,0(%1)\n"		/* table */
+		"	tre	%%r2,%%r4\n"
+		: /* output */
+		: /* input */
+		  "a" (buf),
+		  "a" (table),
+		  "d" (len)
+		: /* clobbered */
+		  "cc", "r0", "r2", "r3", "r4"
+	);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/arch/ebcdic.h	Fri Apr 08 09:56:20 2011 -0400
@@ -0,0 +1,14 @@
+#ifndef __EBCDIC_H
+#define __EBCDIC_H
+
+extern u8 ascii2ebcdic_table[256];
+extern u8 ebcdic2ascii_table[256];
+
+extern void __translate(u8 *buf, int len, const u8 *table);
+
+#define ascii2ebcdic(buf, len)	\
+			__translate((buf), (len), ascii2ebcdic_table)
+#define ebcdic2ascii(buf, len)  \
+			__translate((buf), (len), ebcdic2ascii_table)
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/arch/fba.c	Fri Apr 08 09:56:20 2011 -0400
@@ -0,0 +1,157 @@
+#include "channel.h"
+
+#include <die.h>
+
+#define FBA_BLK_SIZE			512
+
+#define DASD_FBA_WRITE			0x41
+#define DASD_FBA_READ			0x42
+#define DASD_FBA_LOCATE_RECORD		0x43
+#define DASD_FBA_DEFINE_EXTENT		0x63
+
+enum op_type {
+	WRITE = 0,
+	READ,
+};
+
+struct fba_DE_data {
+	u8 perm:2,              /* Permissions on this extent */
+	   __zero0:2,
+	   da:1,
+	   diag:1,              /* allow diagnose */
+	   __zero1:2;
+	u8 __zero3;
+	u16 blksize;            /* Blocksize */
+	u32 ext_loc;            /* Extent locator */
+	u32 ext_beg;            /* logical number of block 0 in extent */
+	u32 ext_end;            /* logocal number of last block in extent */
+};
+
+struct fba_LO_data {
+	u8 zero:4,
+	   cmd:4;
+	u8 aux;
+	u16 count;
+	u32 blkno;
+};
+
+/* build a Define-Extent CCW */
+static void fba_define_extent(struct ccw *ccw, struct fba_DE_data *data,
+			      enum op_type rw, u16 blksize, u32 blkno, u32 len)
+{
+	ccw->cmd   = DASD_FBA_DEFINE_EXTENT;
+	ccw->count = 0x10;
+	ccw->addr  = (u32) (u64) data;
+
+	__builtin_memset(data, 0, sizeof(struct fba_DE_data));
+	switch(rw) {
+		case WRITE:
+			data->perm = 0x00;
+			break;
+		case READ:
+			data->perm = 0x01;
+			break;
+		default:
+			die();
+			break;
+	}
+	data->blksize = blksize;
+	data->ext_loc = blkno;
+	data->ext_end = len - 1;
+}
+
+/* build a Locate-Record CCW */
+static void fba_locate_record(struct ccw *ccw, struct fba_LO_data *data,
+			      enum op_type rw, u32 blkno, u32 len)
+{
+	ccw->cmd   = DASD_FBA_LOCATE_RECORD;
+	ccw->count = 0x08;
+	ccw->addr  = (u32) (u64) data;
+
+	__builtin_memset(data, 0, sizeof(struct fba_LO_data));
+	switch(rw) {
+		case WRITE:
+			data->cmd = 0x05;
+			break;
+		case READ:
+			data->cmd = 0x06;
+			break;
+		default:
+			die();
+			break;
+	}
+	data->blkno = blkno;
+	data->count = len;
+}
+
+/* build a Read or Write CCW */
+static void fba_readwrite(struct ccw *ccw, void *ptr, enum op_type rw, u32 len)
+{
+	if(len > 0xffff)
+		die();
+
+	switch(rw) {
+		case 0:
+			ccw->cmd = DASD_FBA_WRITE;
+			break;
+		case 1:
+			ccw->cmd = DASD_FBA_READ;
+			break;
+		default:
+			die();
+			break;
+	}
+
+	ccw->count = len;
+	ccw->addr  = (u32) (u64) ptr;
+}
+
+/**
+ * fba_read_blk - read a block from an FBA device
+ * @dev:	device to read from
+ * @blkno:	block number to read
+ * @ptr:	buffer to fill
+ * @sync:	synchronous I/O
+ */
+static int __fba_readwrite_blk(u32 dev, u32 blkno, void *ptr, enum op_type dir)
+{
+	struct orb orb;
+	struct ccw ccw[3];
+	struct fba_DE_data defext;
+	struct fba_LO_data locate;
+
+	__builtin_memset(&orb, 0, sizeof(struct orb));
+	orb.lpm = 0xff;
+	orb.addr = (u32) (u64) ccw;
+	orb.f = 1;
+
+	__builtin_memset(ccw, 0, sizeof(ccw));
+
+	/* Define-Extent */
+	fba_define_extent(&ccw[0], &defext, dir, FBA_BLK_SIZE, blkno, 1);
+	ccw[0].flags |= 0x40;
+
+	/* Locate */
+	fba_locate_record(&ccw[1], &locate, dir, 0, 1);
+	ccw[1].flags |= 0x40;
+
+	/* Read */
+	fba_readwrite(&ccw[2], ptr, dir, FBA_BLK_SIZE);
+
+	if (start_sch(dev, &orb))
+		die();
+
+	wait_for_io_int();
+
+	return 0;
+}
+
+int fba_read_blk(u32 dev, u32 blkno, void *ptr)
+{
+	return __fba_readwrite_blk(dev, blkno, ptr, READ);
+}
+
+int fba_write_blk(u32 dev, u32 blkno, void *ptr)
+{
+	return __fba_readwrite_blk(dev, blkno, ptr, WRITE);
+}
--- a/arch/io.c	Fri Apr 08 09:46:23 2011 -0400
+++ b/arch/io.c	Fri Apr 08 09:56:20 2011 -0400
@@ -1,418 +1,6 @@
-/*
- * Tables taken from Linux's arch/s390/kernel/ebcdic.c
- */
 
-/*
- * ASCII (IBM PC 437)  -> EBCDIC 037
- */
-static u8 ascii2ebcdic_table[256] =
-{
- /*00 NUL   SOH   STX   ETX   EOT   ENQ   ACK   BEL */
-     0x00, 0x01, 0x02, 0x03, 0x37, 0x2D, 0x2E, 0x2F,
- /*08  BS    HT    LF    VT    FF    CR    SO    SI */
- /*              ->NL                               */
-     0x16, 0x05, 0x15, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
- /*10 DLE   DC1   DC2   DC3   DC4   NAK   SYN   ETB */
-     0x10, 0x11, 0x12, 0x13, 0x3C, 0x3D, 0x32, 0x26,
- /*18 CAN    EM   SUB   ESC    FS    GS    RS    US */
- /*                               ->IGS ->IRS ->IUS */
-     0x18, 0x19, 0x3F, 0x27, 0x22, 0x1D, 0x1E, 0x1F,
- /*20  SP     !     "     #     $     %     &     ' */
-     0x40, 0x5A, 0x7F, 0x7B, 0x5B, 0x6C, 0x50, 0x7D,
- /*28   (     )     *     +     ,     -    .      / */
-     0x4D, 0x5D, 0x5C, 0x4E, 0x6B, 0x60, 0x4B, 0x61,
- /*30   0     1     2     3     4     5     6     7 */
-     0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
- /*38   8     9     :     ;     <     =     >     ? */
-     0xF8, 0xF9, 0x7A, 0x5E, 0x4C, 0x7E, 0x6E, 0x6F,
- /*40   @     A     B     C     D     E     F     G */
-     0x7C, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
- /*48   H     I     J     K     L     M     N     O */
-     0xC8, 0xC9, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6,
- /*50   P     Q     R     S     T     U     V     W */
-     0xD7, 0xD8, 0xD9, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6,
- /*58   X     Y     Z     [     \     ]     ^     _ */
-     0xE7, 0xE8, 0xE9, 0xBA, 0xE0, 0xBB, 0xB0, 0x6D,
- /*60   `     a     b     c     d     e     f     g */
-     0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
- /*68   h     i     j     k     l     m     n     o */
-     0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
- /*70   p     q     r     s     t     u     v     w */
-     0x97, 0x98, 0x99, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
- /*78   x     y     z     {     |     }     ~    DL */
-     0xA7, 0xA8, 0xA9, 0xC0, 0x4F, 0xD0, 0xA1, 0x07,
- /*80*/
-     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
- /*88*/
-     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
- /*90*/
-     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
- /*98*/
-     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
- /*A0*/
-     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
- /*A8*/
-     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
- /*B0*/
-     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
- /*B8*/
-     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
- /*C0*/
-     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
- /*C8*/
-     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
- /*D0*/
-     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
- /*D8*/
-     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
- /*E0        sz						*/
-     0x3F, 0x59, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
- /*E8*/
-     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
- /*F0*/
-     0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
- /*F8*/
-     0x90, 0x3F, 0x3F, 0x3F, 0x3F, 0xEA, 0x3F, 0xFF
-};
-
-/*
- * EBCDIC 037 -> ASCII (IBM PC 437)
- */
-u8 ebcdic2ascii_table[256] =
-{
- /* 0x00   NUL   SOH   STX   ETX  *SEL    HT  *RNL   DEL */
-          0x00, 0x01, 0x02, 0x03, 0x07, 0x09, 0x07, 0x7F,
- /* 0x08   -GE  -SPS  -RPT    VT    FF    CR    SO    SI */
-          0x07, 0x07, 0x07, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
- /* 0x10   DLE   DC1   DC2   DC3  -RES   -NL    BS  -POC
-                                  -ENP  ->LF             */
-          0x10, 0x11, 0x12, 0x13, 0x07, 0x0A, 0x08, 0x07,
- /* 0x18   CAN    EM  -UBS  -CU1  -IFS  -IGS  -IRS  -ITB
-                                                    -IUS */
-          0x18, 0x19, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
- /* 0x20   -DS  -SOS    FS  -WUS  -BYP    LF   ETB   ESC
-                                  -INP                   */
-          0x07, 0x07, 0x1C, 0x07, 0x07, 0x0A, 0x17, 0x1B,
- /* 0x28   -SA  -SFE   -SM  -CSP  -MFA   ENQ   ACK   BEL
-                       -SW                               */
-          0x07, 0x07, 0x07, 0x07, 0x07, 0x05, 0x06, 0x07,
- /* 0x30  ----  ----   SYN   -IR   -PP  -TRN  -NBS   EOT */
-          0x07, 0x07, 0x16, 0x07, 0x07, 0x07, 0x07, 0x04,
- /* 0x38  -SBS   -IT  -RFF  -CU3   DC4   NAK  ----   SUB */
-          0x07, 0x07, 0x07, 0x07, 0x14, 0x15, 0x07, 0x1A,
- /* 0x40    SP   RSP           ä              ----       */
-          0x20, 0xFF, 0x83, 0x84, 0x85, 0xA0, 0x07, 0x86,
- /* 0x48                       .     <     (     +     | */
-          0x87, 0xA4, 0x9B, 0x2E, 0x3C, 0x28, 0x2B, 0x7C,
- /* 0x50     &                                      ---- */
-          0x26, 0x82, 0x88, 0x89, 0x8A, 0xA1, 0x8C, 0x07,
- /* 0x58           ß     !     $     *     )     ;       */
-          0x8D, 0xE1, 0x21, 0x24, 0x2A, 0x29, 0x3B, 0xAA,
- /* 0x60     -     /  ----     Ä  ----  ----  ----       */
-          0x2D, 0x2F, 0x07, 0x8E, 0x07, 0x07, 0x07, 0x8F,
- /* 0x68              ----     ,     %     _     >     ? */
-          0x80, 0xA5, 0x07, 0x2C, 0x25, 0x5F, 0x3E, 0x3F,
- /* 0x70  ----        ----  ----  ----  ----  ----  ---- */
-          0x07, 0x90, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
- /* 0x78     *     `     :     #     @     '     =     " */
-          0x70, 0x60, 0x3A, 0x23, 0x40, 0x27, 0x3D, 0x22,
- /* 0x80     *     a     b     c     d     e     f     g */
-          0x07, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
- /* 0x88     h     i              ----  ----  ----       */
-          0x68, 0x69, 0xAE, 0xAF, 0x07, 0x07, 0x07, 0xF1,
- /* 0x90     °     j     k     l     m     n     o     p */
-          0xF8, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70,
- /* 0x98     q     r                    ----        ---- */
-          0x71, 0x72, 0xA6, 0xA7, 0x91, 0x07, 0x92, 0x07,
- /* 0xA0           ~     s     t     u     v     w     x */
-          0xE6, 0x7E, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
- /* 0xA8     y     z              ----  ----  ----  ---- */
-          0x79, 0x7A, 0xAD, 0xAB, 0x07, 0x07, 0x07, 0x07,
- /* 0xB0     ^                    ----     §  ----       */
-          0x5E, 0x9C, 0x9D, 0xFA, 0x07, 0x07, 0x07, 0xAC,
- /* 0xB8        ----     [     ]  ----  ----  ----  ---- */
-          0xAB, 0x07, 0x5B, 0x5D, 0x07, 0x07, 0x07, 0x07,
- /* 0xC0     {     A     B     C     D     E     F     G */
-          0x7B, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
- /* 0xC8     H     I  ----           ö              ---- */
-          0x48, 0x49, 0x07, 0x93, 0x94, 0x95, 0xA2, 0x07,
- /* 0xD0     }     J     K     L     M     N     O     P */
-          0x7D, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
- /* 0xD8     Q     R  ----           ü                   */
-          0x51, 0x52, 0x07, 0x96, 0x81, 0x97, 0xA3, 0x98,
- /* 0xE0     \           S     T     U     V     W     X */
-          0x5C, 0xF6, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
- /* 0xE8     Y     Z        ----     Ö  ----  ----  ---- */
-          0x59, 0x5A, 0xFD, 0x07, 0x99, 0x07, 0x07, 0x07,
- /* 0xF0     0     1     2     3     4     5     6     7 */
-          0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- /* 0xF8     8     9  ----  ----     Ü  ----  ----  ---- */
-          0x38, 0x39, 0x07, 0x07, 0x9A, 0x07, 0x07, 0x07
-};
-
-/*
- * Generic translate buffer function.
- */
-static inline void __translate(u8 *buf, int len, const u8 *table)
-{
-	asm volatile(
-		"	sr	%%r0,%%r0\n"		/* test byte = 0 */
-		"	la	%%r2,0(%0)\n"		/* buffer */
-		"	lr	%%r3,%2\n"		/* length */
-		"	la	%%r4,0(%1)\n"		/* table */
-		"	tre	%%r2,%%r4\n"
-		: /* output */
-		: /* input */
-		  "a" (buf),
-		  "a" (table),
-		  "d" (len)
-		: /* clobbered */
-		  "cc", "r0", "r2", "r3", "r4"
-	);
-}
-
-#define ascii2ebcdic(buf, len)	\
-			__translate((buf), (len), ascii2ebcdic_table)
-#define ebcdic2ascii(buf, len)  \
-			__translate((buf), (len), ebcdic2ascii_table)
-
-/* console I/O functions */
-
-#define CON_DEV 0x0009
-
-struct pmcw {
-	/* word 0 */
-	u32 interrupt_param;	/* Interruption Parameter */
-
-	/* word 1*/
-	u8 __zero1:2,
-	   isc:3,		/* I/O-Interruption-Subclass Code */
-	   __zero2:3;
-	u8 e:1,			/* Enabled */
-	   lm:2,		/* Limit Mode */
-	   mm:2,		/* Measurement-Mode Enable */
-	   d:1,			/* Multipath Mode */
-	   t:1,			/* Timing Facility */
-	   v:1;			/* Device Number Valid */
-	u16 dev_num;		/* Device Number */
-
-	/* word 2 */
-	u8 lpm;			/* Logical-Path Mask */
-	u8 pnom;		/* Path-Not-Operational Mask */
-	u8 lpum;		/* Last-Path-Used Mask */
-	u8 pim;			/* Path-Installed Mask */
-
-	/* word 3 */
-	u16 mbi;		/* Measurement-Block Index */
-	u8 pom;			/* Path-Operational Mask */
-	u8 pam;			/* Path-Available Mask */
-
-	/* word 4 & 5 */
-	u8 chpid[8];		/* Channel-Path Identifiers */
-
-	/* word 6 */
-	u16 __zero3;
-	u16 __zero4:13,
-	    f:1,		/* Measurement Block Format Control */
-	    x:1,		/* Extended Measurement Word Mode Enable */
-	    s:1;		/* Concurrent Sense */
-};
-
-struct scsw {
-	/* word 0 */
-	u16 key:4,		/* Subchannel key */
-	    s:1,		/* Suspend control */
-	    l:1,		/* ESW format */
-	    cc:2,		/* Deferred condition code */
-	    f:1,		/* Format */
-	    p:1,		/* Prefetch */
-	    i:1,		/* Initial-status interruption control */
-	    a:1,		/* Address-limit-checking control */
-	    u:1,		/* Supress-suspended interruption */
-	    z:1,		/* Zero condition code */
-	    e:1,		/* Extended control */
-	    n:1;		/* Path no operational */
-	u16 __zero:1,
-	    fc:3,		/* Function control */
-	    ac:7,		/* Activity control */
-	    sc:5;		/* Status control */
-
-	/* word 1 */
-	u32 addr;		/* CCW Address */
-
-	/* word 2 */
-	u8 dev_status;		/* Device status */
-	u8 sch_status;		/* Subchannel status */
-	u16 count;		/* Count */
-} __attribute__((packed));
-
-struct schib {
-	struct pmcw pmcw;               /* Path Management Control Word */
-	struct scsw scsw;               /* Subchannel Status Word */
-	u32 measure_block_1;
-	u32 measure_block_2;
-	u32 model_dep_area;
-} __attribute__((packed,aligned(4)));
-
-struct orb {
-	/* word 0 */
-	u32 param;		/* Interruption Parameter */
-
-	/* word 1 */
-	u8 key:4,		/* Subchannel Key */
-	   s:1,			/* Suspend */
-	   c:1,			/* Streaming-Mode Control */
-	   m:1,			/* Modification Control */
-	   y:1;			/* Synchronization Control */
-	u8 f:1,			/* Format Control */
-	   p:1,			/* Prefetch Control */
-	   i:1,			/* Initial-Status-Interruption Control */
-	   a:1,			/* Address-Limit-Checking control */
-	   u:1,			/* Suppress-Suspend-Interruption Control */
-	   __zero1:1,
-	   h:1,			/* Format-2-IDAW Control */
-	   t:1;			/* 2K-IDAW Control */
-	u8 lpm;			/* Logical-Path Mask */
-	u8 l:1,			/* Incorrect-Length-Suppression Mode */
-	   d:1,			/* Modified-CCW-Indirect-Data-Addressing Control */
-	   __zero2:5,
-	   x:1;			/* ORB-Extension Control */
-
-	/* word 2 */
-	u32 addr;		/* Channel-Program Address */
-
-	/* word 3 */
-	u8 css_prio;		/* Channel-Subsystem Priority */
-	u8 __reserved1;
-	u8 cu_prio;		/* Control-Unit Priority */
-	u8 __reserved2;
-
-	/* word 4 - 7 */
-	u32 __reserved3;
-	u32 __reserved4;
-	u32 __reserved5;
-	u32 __reserved6;
-} __attribute__((packed,aligned(4)));
-
-struct ccw {
-	u8 cmd;			/* Command code */
-	u8 flags;		/* Flags */
-	u16 count;		/* Count */
-	u32 addr;		/* Data Address */
-} __attribute__((packed,aligned(8)));
-
-struct psw {
-	u8 _zero0:1,
-	   r:1,			/* PER Mask (R)			*/
-	   _zero1:3,
-	   t:1,			/* DAT Mode (T)			*/
-	   io:1,		/* I/O Mask (IO)		*/
-	   ex:1;		/* External Mask (EX)		*/
-
-	u8 key:4,		/* Key				*/
-	   _zero2:1,
-	   m:1,			/* Machine-Check Mask (M)	*/
-	   w:1,			/* Wait State (W)		*/
-	   p:1;			/* Problem State (P)		*/
-
-	u8 as:2,		/* Address-Space Control (AS)	*/
-	   cc:2,		/* Condition Code (CC)		*/
-	   prog_mask:4;		/* Program Mask			*/
-
-	u8 _zero3:7,
-	   ea:1;		/* Extended Addressing (EA)	*/
-
-	u32 ba:1,		/* Basic Addressing (BA)	*/
-	    _zero4:31;
-
-	u64 ptr;
-};
-
-struct irb {
-	struct scsw scsw;			/* Subchannel-Status */
-	u32 w0a, w1a, w2a, w3a, w4a;
-	u32 w0b, w1b, w2b, w3b, w4b, w5b, w6b, w7b;
-	u32 w0c, w1c, w2c, w3c, w4c, w5c, w6c, w7c;
-} __attribute__((packed,aligned(4)));
-
-static int init_cons;
-static u32 consch;
-
-static struct irb irb;
-static struct orb orb;
-static struct ccw ccw;
-
-static inline int store_sch(u32 sch, struct schib *schib)
-{
-	int cc;
-
-	asm volatile(
-		"lr	%%r1,%2\n"
-		"stsch	%1\n"
-		"ipm	%0\n"
-		"srl	%0,28\n"
-		: /* output */
-		  "=d" (cc),
-		  "=Q" (*schib)
-		: /* input */
-		  "d" (sch)
-		: /* clobbered */
-		  "cc", "r1", "memory"
-	);
-
-	return cc;
-}
-
-static inline int modify_sch(u32 sch, struct schib *schib)
-{
-	int cc;
-
-	asm volatile(
-		"lr	%%r1,%1\n"
-		"msch	0(%2)\n"
-		"ipm	%0\n"
-		"srl	%0,28\n"
-		: /* output */
-		  "=d" (cc)
-		: /* input */
-		  "d" (sch),
-		  "a" (schib)
-		: /* clobbered */
-		  "cc", "r1"
-	);
-
-	return cc;
-}
-
-static inline int start_sch(u32 sch, struct orb *orb)
-{
-	int cc;
-
-	asm volatile(
-		"	lr	%%r1,%1\n"
-		"	ssch	0(%2)\n"
-		"	ipm	%0\n"
-		"	srl	%0,28\n"
-		: /* output */
-		  "=d" (cc)
-		: /* input */
-		  "d" (sch),
-		  "a" (orb)
-		: /* clobbered */
-		  "cc", "r1"
-	);
-
-	return cc;
-}
-
-#define die()	do { \
-			asm volatile( \
-				"SR	%r1, %r1	# not used, but should be zero\n" \
-				"SR	%r3, %r3 	# CPU Address\n" \
-				"SIGP	%r1, %r3, 0x05	# Signal, order 0x05\n" \
-			); \
-			for(;;); \
-		} while(0)
+#include "channel.h"
+#include <die.h>
 
 u32 find_dev(int devnum)
 {
@@ -429,10 +17,12 @@
 		if (schib.pmcw.dev_num != devnum)
 			continue;
 
-		schib.pmcw.e = 1;
+		if (!schib.pmcw.e) {
+			schib.pmcw.e = 1;
 
-		if (modify_sch(sch, &schib))
-			continue;
+			if (modify_sch(sch, &schib))
+				continue;
+		}
 
 		return sch;
 	}
@@ -440,61 +30,21 @@
 	return 0;
 }
 
-static void enable_cons(int devnum)
+extern void IOINT(void);
+
+void init_io_int()
 {
 	struct psw psw;
-	u32 sch;
-
-	sch = find_dev(devnum);
-
-	if (!sch)
-		die();
-
-	// found it
-	init_cons = 1;
-	consch = sch;
 
 	// set up the IO interrupt handler
 	psw.ea  = 1;
 	psw.ba  = 1;
-
-	asm volatile(
-		"       larl    %%r1,0f\n"
-		"       stg     %%r1,%0\n"
-		"	brc	15,1f\n"
-		/* IO handler code begins */
-		"0:\n"
-		"	l	%%r1,0xb8\n"
-		"	larl	%%r2,irb\n"
-		"	tsch	0(%%r2)\n"
-		"	l	%%r1,5(%%r2)\n"
-		"	nill	%%r1,0x04\n"
-		"	brc	8,2f\n" // done?
-		"	lg	%%r1,0x178\n" // yes.
-		"	bcr	15,%%r1\n"
-		"2:\n"
-		"	l	%%r1,5(%%r2)\n"
-		"	nill	%%r1,0x80\n"
-		"	brc	8,3f\n" // attention?
-		"	lg	%%r1,0x178\n" // yes.
-		"	bcr	15,%%r1\n"
-		"3:\n"
-		"	lpswe	0x170\n"
-		/* IO handler code ends */
-		"1:\n"
-	: /* output */
-	  "=m" (psw.ptr),
-	  "=m" (irb)
-	: /* input */
-	  "a" (&irb)
-	: /* clobbered */
-	  "r1", "r2"
-	);
+	psw.ptr = (u64) &IOINT;
 
 	__builtin_memcpy(((void*) 0x1f0), &psw, sizeof(struct psw));
 }
 
-static inline void wait_for_io_int()
+void wait_for_io_int()
 {
 	struct psw psw;
 
@@ -516,226 +66,9 @@
         : /* clobbered */
           "r1", "r2"
         );
-}
-
-int putline(char *buf, int len)
-{
-	char data[CON_LEN];
-
-	if (!init_cons)
-		enable_cons(CON_DEV);
-
-	if ((len <= 0) || (len > CON_LEN))
-		return -1;
-
-	__builtin_memcpy(data, buf, len);
-	ascii2ebcdic((u8*) data, len);
-
-	ccw.cmd   = 0x01;
-	ccw.flags = 0;
-	ccw.count = len;
-	ccw.addr  = (u32) (u64) data;
-
-	__builtin_memset(&orb, 0, sizeof(struct orb));
-	orb.lpm   = 0xff;
-	orb.addr  = (u32) (u64) &ccw;
-	orb.f     = 1;
-
-	if (start_sch(consch, &orb))
-		die();
-
-	wait_for_io_int();
-
-	return len;
-}
-
-int getline(char *buf, int len)
-{
-	int i;
-
-	if (!init_cons)
-		enable_cons(CON_DEV);
-
-	if ((len <= 0) || (len > CON_LEN))
-		return -1;
-
-	wait_for_io_int();
-
-	__builtin_memset(buf, 0, len);
-	ccw.cmd   = 0x0a;
-	ccw.flags = 0x20;
-	ccw.count = len;
-	ccw.addr  = (u32) (u64) buf;
-
-	__builtin_memset(&orb, 0, sizeof(struct orb));
-	orb.lpm   = 0xff;
-	orb.addr  = (u32) (u64) &ccw;
-	orb.f     = 1;
-
-	if (start_sch(consch, &orb))
-		die();
-
-	wait_for_io_int();
-
-	for(i=0; i<len; i++)
-		if (!buf[i])
-			break;
-
-	if (i)
-		ebcdic2ascii((u8*) buf, i);
-
-	return i;
-}
-
-#define FBA_BLK_SIZE			512
-
-#define DASD_FBA_WRITE			0x41
-#define DASD_FBA_READ			0x42
-#define DASD_FBA_LOCATE_RECORD		0x43
-#define DASD_FBA_DEFINE_EXTENT		0x63
-
-enum op_type {
-	WRITE = 0,
-	READ,
-};
-
-struct fba_DE_data {
-	u8 perm:2,              /* Permissions on this extent */
-	   __zero0:2,
-	   da:1,
-	   diag:1,              /* allow diagnose */
-	   __zero1:2;
-	u8 __zero3;
-	u16 blksize;            /* Blocksize */
-	u32 ext_loc;            /* Extent locator */
-	u32 ext_beg;            /* logical number of block 0 in extent */
-	u32 ext_end;            /* logocal number of last block in extent */
-};
-
-struct fba_LO_data {
-	u8 zero:4,
-	   cmd:4;
-	u8 aux;
-	u16 count;
-	u32 blkno;
-};
-
-/* build a Define-Extent CCW */
-static void fba_define_extent(struct ccw *ccw, struct fba_DE_data *data,
-			      enum op_type rw, u16 blksize, u32 blkno, u32 len)
-{
-	ccw->cmd   = DASD_FBA_DEFINE_EXTENT;
-	ccw->count = 0x10;
-	ccw->addr  = (u32) (u64) data;
 
-	__builtin_memset(data, 0, sizeof(struct fba_DE_data));
-	switch(rw) {
-		case WRITE:
-			data->perm = 0x00;
-			break;
-		case READ:
-			data->perm = 0x01;
-			break;
-		default:
-			die();
-			break;
-	}
-	data->blksize = blksize;
-	data->ext_loc = blkno;
-	data->ext_end = len - 1;
-}
-
-/* build a Locate-Record CCW */
-static void fba_locate_record(struct ccw *ccw, struct fba_LO_data *data,
-			      enum op_type rw, u32 blkno, u32 len)
-{
-	ccw->cmd   = DASD_FBA_LOCATE_RECORD;
-	ccw->count = 0x08;
-	ccw->addr  = (u32) (u64) data;
-
-	__builtin_memset(data, 0, sizeof(struct fba_LO_data));
-	switch(rw) {
-		case WRITE:
-			data->cmd = 0x05;
-			break;
-		case READ:
-			data->cmd = 0x06;
-			break;
-		default:
-			die();
-			break;
-	}
-	data->blkno = blkno;
-	data->count = len;
-}
-
-/* build a Read or Write CCW */
-static void fba_readwrite(struct ccw *ccw, void *ptr, enum op_type rw, u32 len)
-{
-	if(len > 0xffff)
-		die();
+	if (*((u8*) 0x210) & 0x04)
+		return;
 
-	switch(rw) {
-		case 0:
-			ccw->cmd = DASD_FBA_WRITE;
-			break;
-		case 1:
-			ccw->cmd = DASD_FBA_READ;
-			break;
-		default:
-			die();
-			break;
-	}
-
-	ccw->count = len;
-	ccw->addr  = (u32) (u64) ptr;
+	die();
 }
-
-/**
- * fba_read_blk - read a block from an FBA device
- * @dev:	device to read from
- * @blkno:	block number to read
- * @ptr:	buffer to fill
- * @sync:	synchronous I/O
- */
-static int __fba_readwrite_blk(u32 dev, u32 blkno, void *ptr, enum op_type dir)
-{
-	struct ccw ccw[3];
-	struct fba_DE_data defext;
-	struct fba_LO_data locate;
-
-	__builtin_memset(&orb, 0, sizeof(struct orb));
-	orb.lpm = 0xff;
-	orb.addr = (u32) (u64) ccw;
-	orb.f = 1;
-
-	__builtin_memset(ccw, 0, sizeof(ccw));
-
-	/* Define-Extent */
-	fba_define_extent(&ccw[0], &defext, dir, FBA_BLK_SIZE, blkno, 1);
-	ccw[0].flags |= 0x40;
-
-	/* Locate */
-	fba_locate_record(&ccw[1], &locate, dir, 0, 1);
-	ccw[1].flags |= 0x40;
-
-	/* Read */
-	fba_readwrite(&ccw[2], ptr, dir, FBA_BLK_SIZE);
-
-	if (start_sch(dev, &orb))
-		die();
-
-	wait_for_io_int();
-
-	return 0;
-}
-
-int fba_read_blk(u32 dev, u32 blkno, void *ptr)
-{
-	return __fba_readwrite_blk(dev, blkno, ptr, READ);
-}
-
-int fba_write_blk(u32 dev, u32 blkno, void *ptr)
-{
-	return __fba_readwrite_blk(dev, blkno, ptr, WRITE);
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/arch/ioint.S	Fri Apr 08 09:56:20 2011 -0400
@@ -0,0 +1,44 @@
+.text
+	.align	4
+.globl IOINT
+	.type	IOINT, @function
+IOINT:
+	stmg	%r1,%r2,512
+
+	l	%r1,0xb8
+	larl	%r2,irb
+	tsch	0(%r2)
+
+	l	%r1,5(%r2)
+	nill	%r1,0x04
+	brc	7,4f		# device end
+
+	l	%r1,5(%r2)
+	nill	%r1,0x80
+	brc	7,4f		# attention
+
+	l	%r1,5(%r2)
+	nill	%r1,0x03
+	brc	7,4f		# ucheck or uexcept
+
+4:
+	la	%r1,1
+	st	%r1,0x170
+3:
+	mvc	0x210(1,%r0),8(%r2)
+	lmg	%r1,%r2,512
+	lpswe	0x170
+
+irb:
+	.8byte 0x00
+	.8byte 0x00
+	.8byte 0x00
+	.8byte 0x00
+	.8byte 0x00
+	.8byte 0x00
+	.8byte 0x00
+	.8byte 0x00
+	.8byte 0x00
+	.8byte 0x00
+	.8byte 0x00
+	.8byte 0x00
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/die.h	Fri Apr 08 09:56:20 2011 -0400
@@ -0,0 +1,13 @@
+#ifndef __DIE_H
+#define __DIE_H
+
+#define die()	do { \
+			asm volatile( \
+				"SR	%r1, %r1	# not used, but should be zero\n" \
+				"SR	%r3, %r3 	# CPU Address\n" \
+				"SIGP	%r1, %r3, 0x05	# Signal, order 0x05\n" \
+			); \
+			for(;;); \
+		} while(0)
+
+#endif
--- a/include/system.h	Fri Apr 08 09:46:23 2011 -0400
+++ b/include/system.h	Fri Apr 08 09:56:20 2011 -0400
@@ -1,6 +1,9 @@
 #ifndef __SYSTEM_H
 #define __SYSTEM_H
 
+/* this is the device number for the console device, do not change */
+#define CON_DEV 0x0009
+
 #define NULL	((void*) 0)
 #define CON_LEN 132
 #define HEAP_START 0x200000
@@ -26,6 +29,9 @@
 #ifdef __cplusplus
 extern "C" {
 #endif
+	extern void init_io_int(void);
+	extern void init_console(void);
+
 	extern int putline(char *buf, int len);
 	extern int getline(char *buf, int len);
 
--- a/src/dynamic.c	Fri Apr 08 09:46:23 2011 -0400
+++ b/src/dynamic.c	Fri Apr 08 09:56:20 2011 -0400
@@ -21,6 +21,8 @@
 
 void start(u64 __memsize)
 {
+	init_io_int();
+	init_console();
 	malloc_init(__memsize - HEAP_START);
 	size_t n;
 	char *buffer;
--- a/src/fs.c	Fri Apr 08 09:46:23 2011 -0400
+++ b/src/fs.c	Fri Apr 08 09:56:20 2011 -0400
@@ -80,7 +80,8 @@
 
 void start(u64 __memsize)
 {
-	sPrint(""); //I have no idea why I would possibly need this...
+	init_io_int();
+	init_console();
 	char buffer[256];
 	int i;
 	
--- a/src/sarpn.c	Fri Apr 08 09:46:23 2011 -0400
+++ b/src/sarpn.c	Fri Apr 08 09:56:20 2011 -0400
@@ -8,6 +8,8 @@
 
 void start(u64 __memsize)
 {
+	init_io_int();
+	init_console();
 	malloc_init(__memsize - HEAP_START);
 
 	Stack *stack = stack_init();