BSD 4_4 release
[unix-history] / usr / src / sys / i386 / isa / fd.c
index 8407b8a..b02e778 100644 (file)
@@ -1,38 +1,66 @@
 /*-
 /*-
- * Copyright (c) 1990 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1990, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  * This code is derived from software contributed to Berkeley by
  * Don Ahn.
  *
  *
  * This code is derived from software contributed to Berkeley by
  * Don Ahn.
  *
- * %sccs.include.386.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
  *
  *
- *     @(#)fd.c        5.1 (Berkeley) %G%
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *     @(#)fd.c        8.1 (Berkeley) 6/11/93
  */
 
  */
 
-/****************************************************************************/
-/*                               fd driver                                  */
-/****************************************************************************/
-#include "param.h"
-#include "dkbad.h"
-#include "systm.h"
-#include "conf.h"
-#include "file.h"
-#include "dir.h"
-#include "user.h"
-#include "ioctl.h"
-#include "disk.h"
-#include "buf.h"
-#include "vm.h"
-#include "uio.h"
-#include "machine/pte.h"
-#include "machine/device.h"
-#include "icu.h"
-
-#define NFD 2
+#include "fd.h"
+#if NFD > 0
+/*
+ * This driver assumed that NFD == 2. Now it works for NFD == 1 or NFD == 2
+ * It will probably not work for NFD > 2.
+ */
+#include <sys/param.h>
+#include <sys/dkbad.h>
+#include <sys/systm.h>
+#include <sys/conf.h>
+#include <sys/file.h>
+#include <sys/ioctl.h>
+#include <sys/buf.h>
+#include <sys/uio.h>
+
+#include <i386/isa/isa_device.h>
+#include <i386/isa/fdreg.h>
+#include <i386/isa/icu.h>
+
 #define        FDUNIT(s)       ((s)&1)
 #define        FDTYPE(s)       (((s)>>1)&7)
 #define        FDUNIT(s)       ((s)&1)
 #define        FDTYPE(s)       (((s)>>1)&7)
+#define FDMOTOR(u)     (fd_unit[(u)].motor ? (1 << (4 + (u))) : 0)
+
 #define b_cylin b_resid
 #define b_cylin b_resid
+#define b_step b_resid
 #define FDBLK 512
 #define NUMTYPES 4
 
 #define FDBLK 512
 #define NUMTYPES 4
 
@@ -48,7 +76,7 @@ struct fd_type {
 };
 
 struct fd_type fd_types[NUMTYPES] = {
 };
 
 struct fd_type fd_types[NUMTYPES] = {
-       { 18,2,0xFF,0x1B,80,2880,1,0 }, /* 1.44 meg HD 3.5in floppy    */
+       { 18,2,0xFF,0x1B,80,2880,1,0 }, /* 1.44 meg HD 3.5in floppy    */
        { 15,2,0xFF,0x1B,80,2400,1,0 }, /* 1.2 meg HD floppy           */
        { 9,2,0xFF,0x23,40,720,2,1 },   /* 360k floppy in 1.2meg drive */
        { 9,2,0xFF,0x2A,40,720,1,1 },   /* 360k floppy in DD drive     */
        { 15,2,0xFF,0x1B,80,2400,1,0 }, /* 1.2 meg HD floppy           */
        { 9,2,0xFF,0x23,40,720,2,1 },   /* 360k floppy in 1.2meg drive */
        { 9,2,0xFF,0x2A,40,720,1,1 },   /* 360k floppy in DD drive     */
@@ -58,89 +86,120 @@ struct fd_u {
        int type;               /* Drive type (HD, DD     */
        int active;             /* Drive activity boolean */
        int motor;              /* Motor on flag          */
        int type;               /* Drive type (HD, DD     */
        int active;             /* Drive activity boolean */
        int motor;              /* Motor on flag          */
-       int opencnt;            /* Num times open         */
        struct buf head;        /* Head of buf chain      */
        struct buf rhead;       /* Raw head of buf chain  */
        struct buf head;        /* Head of buf chain      */
        struct buf rhead;       /* Raw head of buf chain  */
+       int reset;
 } fd_unit[NFD];
 
 } fd_unit[NFD];
 
+
 extern int hz;
 
 /* state needed for current transfer */
 extern int hz;
 
 /* state needed for current transfer */
+static fdc;    /* floppy disk controller io base register */
+int    fd_dmachan = 2;
 static int fd_skip;
 static int fd_state;
 static int fd_retry;
 static int fd_drive;
 static int fd_skip;
 static int fd_state;
 static int fd_retry;
 static int fd_drive;
+static int fd_track = -1;
 static int fd_status[7];
 static int fd_status[7];
-static char fdrawbuf[FDBLK];
 
 
-/* stuff needed for virtual to physical calculations */
-extern char Sysbase;
-static unsigned long sbase = (unsigned long) &Sysbase;
+/*
+       make sure bounce buffer for DMA is aligned since the DMA chip
+       doesn't roll over properly over a 64k boundary
+*/
+extern struct buf *dma_bounce[];
 
 /****************************************************************************/
 /*                      autoconfiguration stuff                             */
 /****************************************************************************/
 int fdprobe(), fdattach(), fd_turnoff();
 
 
 /****************************************************************************/
 /*                      autoconfiguration stuff                             */
 /****************************************************************************/
 int fdprobe(), fdattach(), fd_turnoff();
 
-struct driver fddriver = {
+struct isa_driver fddriver = {
        fdprobe, fdattach, "fd",
 };
 
 fdprobe(dev)
        fdprobe, fdattach, "fd",
 };
 
 fdprobe(dev)
-struct device *dev;
+struct isa_device *dev;
 {
        return 1;
 }
 
 fdattach(dev)
 {
        return 1;
 }
 
 fdattach(dev)
-struct device *dev;
+struct isa_device *dev;
+{      int     s;
+
+       fdc = dev->id_iobase;
+       /* Set transfer to 500kbps */
+       outb(fdc+fdctl,0);
+       fd_turnoff(0);
+}
+
+int
+Fdsize(dev)
+dev_t  dev;
 {
 {
-       INTREN(IRQ6);
+       return(2400);
 }
 
 /****************************************************************************/
 /*                               fdstrategy                                 */
 /****************************************************************************/
 }
 
 /****************************************************************************/
 /*                               fdstrategy                                 */
 /****************************************************************************/
-fdstrategy(bp)
+Fdstrategy(bp)
        register struct buf *bp;        /* IO operation to perform */
 {
        register struct buf *bp;        /* IO operation to perform */
 {
-       register struct buf *dp,*dp0,*dp1;
+       register struct buf *dp;
        long nblocks,blknum;
        long nblocks,blknum;
-       int     unit, type, s;
+       int     unit, type, s;
 
 
-       unit = FDUNIT(minor(bp->b_dev));
-       type = FDTYPE(minor(bp->b_dev));
-#ifdef FDOTHER
-       printf("fdstrat: unit = %d, blkno = %d, bcount = %d\n",
-               unit, bp->b_blkno, bp->b_bcount);
+       unit = FDUNIT(minor(bp->b_dev));
+       type = FDTYPE(minor(bp->b_dev));
+
+#ifdef FDTEST
+printf("fdstrat%d, blk = %d, bcount = %d, addr = %x|",
+       unit, bp->b_blkno, bp->b_bcount,bp->b_un.b_addr);
 #endif
        if ((unit >= NFD) || (bp->b_blkno < 0)) {
                printf("fdstrat: unit = %d, blkno = %d, bcount = %d\n",
                        unit, bp->b_blkno, bp->b_bcount);
                pg("fd:error in fdstrategy");
                bp->b_error = EINVAL;
 #endif
        if ((unit >= NFD) || (bp->b_blkno < 0)) {
                printf("fdstrat: unit = %d, blkno = %d, bcount = %d\n",
                        unit, bp->b_blkno, bp->b_bcount);
                pg("fd:error in fdstrategy");
                bp->b_error = EINVAL;
+               bp->b_flags |= B_ERROR;
                goto bad;
        }
        /*
         * Set up block calculations.
         */
        blknum = (unsigned long) bp->b_blkno * DEV_BSIZE/FDBLK;
                goto bad;
        }
        /*
         * Set up block calculations.
         */
        blknum = (unsigned long) bp->b_blkno * DEV_BSIZE/FDBLK;
-       nblocks = fd_types[type].size;
+       nblocks = fd_types[type].size;
        if (blknum + (bp->b_bcount / FDBLK) > nblocks) {
        if (blknum + (bp->b_bcount / FDBLK) > nblocks) {
-               if (blknum == nblocks) bp->b_resid = bp->b_bcount;
-               else bp->b_error = ENOSPC;
+               if (blknum == nblocks) {
+                       bp->b_resid = bp->b_bcount;
+               } else {
+                       bp->b_error = ENOSPC;
+                       bp->b_flags |= B_ERROR;
+               }
+#ifdef FDTEST
+printf("fdstrat%d, too big\n");
+#endif 
                goto bad;
        }
                goto bad;
        }
-       bp->b_cylin = blknum / (fd_types[type].sectrac * 2);
-       bp->b_cylin *= (fd_types[type].steptrac);
+       bp->b_cylin = blknum / (fd_types[type].sectrac * 2);
        dp = &fd_unit[unit].head;
        dp = &fd_unit[unit].head;
-       dp0 = &fd_unit[0].head;
-       dp1 = &fd_unit[1].head;
+       dp->b_step = (fd_types[fd_unit[unit].type].steptrac);
        s = splbio();
        disksort(dp, bp);
        s = splbio();
        disksort(dp, bp);
-       if ((dp0->b_active == 0)&&(dp1->b_active == 0)) {
+       if ((fd_unit[0].head.b_active == 0)
+#if NFD > 1
+           && (fd_unit[1].head.b_active == 0)
+#endif
+           ) {
+#ifdef FDDEBUG
+printf("T|");
+#endif
                dp->b_active = 1;
                fd_drive = unit;
                dp->b_active = 1;
                fd_drive = unit;
+               fd_track = -1;  /* force seek on first xfer */
                untimeout(fd_turnoff,unit);
                fdstart(unit);          /* start drive if idle */
        }
                untimeout(fd_turnoff,unit);
                fdstart(unit);          /* start drive if idle */
        }
@@ -148,7 +207,6 @@ fdstrategy(bp)
        return;
 
 bad:
        return;
 
 bad:
-       bp->b_flags |= B_ERROR;
        biodone(bp);
 }
 
        biodone(bp);
 }
 
@@ -158,10 +216,11 @@ bad:
 set_motor(unit,reset)
 int unit,reset;
 {
 set_motor(unit,reset)
 int unit,reset;
 {
-       int m0,m1;
-       m0 = fd_unit[0].motor;
-       m1 = fd_unit[1].motor;
-       outb(0x3f2,unit | (reset ? 0 : 0xC)  | (m0 ? 16 : 0) | (m1 ? 32 : 0));
+       outb(fdc+fdout,unit | (reset ? 0 : 0xC)  | FDMOTOR(0) 
+#if NFD > 1
+            | FDMOTOR(1)
+#endif
+            );
 }
 
 fd_turnoff(unit)
 }
 
 fd_turnoff(unit)
@@ -186,8 +245,9 @@ int
 in_fdc()
 {
        int i;
 in_fdc()
 {
        int i;
-       while ((i = inb(0x3f4) & 192) != 192) if (i == 128) return -1;
-       return inb(0x3f5);
+       while ((i = inb(fdc+fdsts) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM))
+               if (i == NE7_RQM) return -1;
+       return inb(fdc+fddata);
 }
 
 dump_stat()
 }
 
 dump_stat()
@@ -197,9 +257,9 @@ dump_stat()
                fd_status[i] = in_fdc();
                if (fd_status[i] < 0) break;
        }
                fd_status[i] = in_fdc();
                if (fd_status[i] < 0) break;
        }
-       printf("FD bad status :%X %X %X %X %X %X %X\n",
-               fd_status[0], fd_status[1], fd_status[2], fd_status[3],
-               fd_status[4], fd_status[5], fd_status[6] );
+printf("FD bad status :%lx %lx %lx %lx %lx %lx %lx\n",
+       fd_status[0], fd_status[1], fd_status[2], fd_status[3],
+       fd_status[4], fd_status[5], fd_status[6] );
 }
 
 out_fdc(x)
 }
 
 out_fdc(x)
@@ -209,12 +269,12 @@ int x;
        static int maxcnt = 0;
        errcnt = 0;
        do {
        static int maxcnt = 0;
        errcnt = 0;
        do {
-               r = (inb(0x3f4) & 192);
-               if (r==128) break;
-               if (r==192) {
+               r = (inb(fdc+fdsts) & (NE7_DIO|NE7_RQM));
+               if (r== NE7_RQM) break;
+               if (r==(NE7_DIO|NE7_RQM)) {
                        dump_stat(); /* error: direction. eat up output */
 #ifdef FDOTHER
                        dump_stat(); /* error: direction. eat up output */
 #ifdef FDOTHER
-                       printf("%X\n",x);
+printf("%lx\n",x);
 #endif
                }
                /* printf("Error r = %d:",r); */
 #endif
                }
                /* printf("Error r = %d:",r); */
@@ -223,10 +283,10 @@ int x;
        if (errcnt > maxcnt) {
                maxcnt = errcnt;
 #ifdef FDOTHER
        if (errcnt > maxcnt) {
                maxcnt = errcnt;
 #ifdef FDOTHER
-               printf("New MAX = %d\n",maxcnt);
+printf("New MAX = %d\n",maxcnt);
 #endif
        }
 #endif
        }
-       outb(0x3f5,x&0xFF);
+       outb(fdc+fddata,x);
 }
 
 /* see if fdc responding */
 }
 
 /* see if fdc responding */
@@ -235,7 +295,7 @@ check_fdc()
 {
        int i;
        for(i=0;i<100;i++) {
 {
        int i;
        for(i=0;i<100;i++) {
-               if (inb(0x3f4)&128) return 0;
+               if (inb(fdc+fdsts)& NE7_RQM) return 0;
        }
        return 1;
 }
        }
        return 1;
 }
@@ -243,29 +303,30 @@ check_fdc()
 /****************************************************************************/
 /*                           fdopen/fdclose                                 */
 /****************************************************************************/
 /****************************************************************************/
 /*                           fdopen/fdclose                                 */
 /****************************************************************************/
-fdopen(dev, flags)
+Fdopen(dev, flags)
        dev_t   dev;
        int     flags;
 {
        dev_t   dev;
        int     flags;
 {
-       int unit = FDUNIT(minor(dev));
-       int type = FDTYPE(minor(dev));
+       int unit = FDUNIT(minor(dev));
+       int type = FDTYPE(minor(dev));
        int s;
 
        int s;
 
+       printf("fdopen %x %d %d\n", minor(dev), unit, type);
        /* check bounds */
        if (unit >= NFD) return(ENXIO);
        if (type >= NUMTYPES) return(ENXIO);
        /* check bounds */
        if (unit >= NFD) return(ENXIO);
        if (type >= NUMTYPES) return(ENXIO);
+/*
        if (check_fdc()) return(EBUSY);
        if (check_fdc()) return(EBUSY);
+*/
 
        /* Set proper disk type, only allow one type */
 
        /* Set proper disk type, only allow one type */
-       s = splbio();
-       splx(s);
-
        return 0;
 }
 
        return 0;
 }
 
-fdclose(dev)
+Fdclose(dev, flags)
        dev_t dev;
 {
        dev_t dev;
 {
+       return (0);
 }
 
 /****************************************************************************/
 }
 
 /****************************************************************************/
@@ -274,22 +335,22 @@ fdclose(dev)
 /*
  * Routines to do raw IO for a unit.
  */
 /*
  * Routines to do raw IO for a unit.
  */
-fdread(dev, uio)                       /* character read routine */
+Fdread(dev, uio)                       /* character read routine */
 dev_t dev;
 struct uio *uio;
 {
 dev_t dev;
 struct uio *uio;
 {
-       int unit = FDUNIT(minor(dev)) ;
+       int unit = FDUNIT(minor(dev)) ;
        if (unit >= NFD) return(ENXIO);
        if (unit >= NFD) return(ENXIO);
-       return(physio(fdstrategy,&fd_unit[unit].rhead,dev,B_READ,minphys,uio));
+       return(physio(Fdstrategy,&fd_unit[unit].rhead,dev,B_READ,minphys,uio));
 }
 
 }
 
-fdwrite(dev, uio)                      /* character write routine */
+Fdwrite(dev, uio)                      /* character write routine */
 dev_t dev;
 struct uio *uio;
 {
 dev_t dev;
 struct uio *uio;
 {
-       int unit = FDUNIT(minor(dev));
+       int unit = FDUNIT(minor(dev)) ;
        if (unit >= NFD) return(ENXIO);
        if (unit >= NFD) return(ENXIO);
-       return(physio(fdstrategy,&fd_unit[unit].rhead,dev,B_WRITE,minphys,uio));
+       return(physio(Fdstrategy,&fd_unit[unit].rhead,dev,B_WRITE,minphys,uio));
 }
 
 /****************************************************************************/
 }
 
 /****************************************************************************/
@@ -301,72 +362,41 @@ int unit;
        register struct buf *dp,*bp;
        int s;
 
        register struct buf *dp,*bp;
        int s;
 
+#ifdef FDTEST
+printf("fd%d|",unit);
+#endif 
+       s = splbio();
        if (!fd_unit[unit].motor) {
                fd_turnon(unit);
                /* Wait for 1 sec */
                timeout(fdstart,unit,hz);
        if (!fd_unit[unit].motor) {
                fd_turnon(unit);
                /* Wait for 1 sec */
                timeout(fdstart,unit,hz);
-       } else {
-               s = splbio();
+               /*DELAY(1000000);*/
+       }else
+                {
+               /* make sure drive is selected as well as on */
+               /*set_motor(unit,0);*/
+
                dp = &fd_unit[unit].head;
                bp = dp->b_actf;
                fd_retry = 0;
                dp = &fd_unit[unit].head;
                bp = dp->b_actf;
                fd_retry = 0;
-               fd_state = 1;
+               if (fd_unit[unit].reset) fd_state = 1;
+               else {
+                       /* DO a RESET */
+                       fd_unit[unit].reset = 1;
+                       fd_state = 5;
+               }
                fd_skip = 0;
                fd_skip = 0;
+#ifdef FDDEBUG
+printf("Seek %d %d\n", bp->b_cylin, dp->b_step);
+#endif
+               if (bp->b_cylin != fd_track) {
                /* Seek necessary, never quite sure where head is at! */
                out_fdc(15);    /* Seek function */
                out_fdc(unit);  /* Drive number */
                /* Seek necessary, never quite sure where head is at! */
                out_fdc(15);    /* Seek function */
                out_fdc(unit);  /* Drive number */
-               out_fdc(bp->b_cylin);
-               splx(s);
+               out_fdc(bp->b_cylin * dp->b_step);
+               } else fdintr(0xff);
        }
        }
-}
-
-/* XXX temporary */
-kernel_space(x)
-unsigned long x;
-{
-       if ((x >= sbase) & (x < sbase + 0x800000)) return 1;
-       else return 0;
-}
-
-
-/****************************************************************************/
-/*                                 fd_dma                                   */
-/* set up DMA read/write operation and virtual address addr for nbytes      */
-/****************************************************************************/
-fd_dma(read,addr,nbytes)
-int read;
-unsigned long addr;
-int nbytes;
-{
-       unsigned long phys;
-       int s,raw;
-
-       if (kernel_space(addr)) raw = 0;
-       else raw = 1;
-
-       /* copy bounce buffer on write */
-       if (raw && !read) bcopy(addr,fdrawbuf,FDBLK);
-
-       /* Set read/write bytes */
-       if (read) {
-               outb(0xC,0x46); outb(0xB,0x46);
-       } else {
-               outb(0xC,0x4A); outb(0xB,0x4A);
-       }
-       /* Send start address */
-       if (raw) phys = (unsigned long) &fdrawbuf[0];
-       else phys = addr;
-       /* translate to physical */
-       phys = phys - sbase;
-       outb(0x4,phys & 0xFF);
-       outb(0x4,(phys>>8) & 0xFF);
-       outb(0x81,(phys>>16) & 0xFF);
-       /* Send count */
-       nbytes--;
-       outb(0x5,nbytes & 0xFF);
-       outb(0x5,(nbytes>>8) & 0xFF);
-       /* set channel 2 */
-       outb(0x0A,2);
+       splx(s);
 }
 
 fd_timeout(x)
 }
 
 fd_timeout(x)
@@ -381,12 +411,12 @@ int x;
        out_fdc(0x4);
        out_fdc(fd_drive);
        i = in_fdc();
        out_fdc(0x4);
        out_fdc(fd_drive);
        i = in_fdc();
-       printf("Timeout drive status %X\n",i);
+       printf("Timeout drive status %lx\n",i);
 
        out_fdc(0x8);
        i = in_fdc();
        j = in_fdc();
 
        out_fdc(0x8);
        i = in_fdc();
        j = in_fdc();
-       printf("ST0 = %X, PCN = %X\n",i,j);
+       printf("ST0 = %lx, PCN = %lx\n",i,j);
 
        if (bp) badtrans(dp,bp);
 }
 
        if (bp) badtrans(dp,bp);
 }
@@ -394,31 +424,38 @@ int x;
 /****************************************************************************/
 /*                                 fdintr                                   */
 /****************************************************************************/
 /****************************************************************************/
 /*                                 fdintr                                   */
 /****************************************************************************/
-fdintr(vec)
-int vec;
+fdintr(unit)
 {
        register struct buf *dp,*bp;
        struct buf *dpother;
 {
        register struct buf *dp,*bp;
        struct buf *dpother;
-       int read,head,trac,sec,i,s,sectrac;
+       int read,head,trac,sec,i,s,sectrac,cyl;
        unsigned long blknum;
        struct fd_type *ft;
        unsigned long blknum;
        struct fd_type *ft;
-       static int fd_track;
+
+#ifdef FDTEST
+       printf("state %d, unit %d, dr %d|",fd_state,unit,fd_drive);
+#endif
 
        dp = &fd_unit[fd_drive].head;
        bp = dp->b_actf;
        read = bp->b_flags & B_READ;
 
        dp = &fd_unit[fd_drive].head;
        bp = dp->b_actf;
        read = bp->b_flags & B_READ;
-       ft = &fd_types[FDTYPE(bp->b_dev)];
+       ft = &fd_types[FDTYPE(bp->b_dev)];
 
        switch (fd_state) {
        case 1 : /* SEEK DONE, START DMA */
 
        switch (fd_state) {
        case 1 : /* SEEK DONE, START DMA */
-#ifdef FDOTHER
-               out_fdc(0x8);
-               i = in_fdc();
-               sec = in_fdc();
-               printf("ST0 = %X, PCN = %X:",i,sec);
-#endif
+               /* Make sure seek really happened*/
+               if (unit != 0xff) {
+                       out_fdc(0x8);
+                       i = in_fdc();
+                       cyl = in_fdc();
+                       if (!(i&0x20) || (cyl != bp->b_cylin*dp->b_step)) {
+printf("Stray int ST0 = %lx, PCN = %lx:",i,cyl);
+                               return;
+                       }
+               }
+
                fd_track = bp->b_cylin;
                fd_track = bp->b_cylin;
-               fd_dma(read,bp->b_un.b_addr+fd_skip,FDBLK);
+               at_dma(read,bp->b_un.b_addr+fd_skip,FDBLK, fd_dmachan);
                blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/FDBLK
                        + fd_skip/FDBLK;
                sectrac = ft->sectrac;
                blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/FDBLK
                        + fd_skip/FDBLK;
                sectrac = ft->sectrac;
@@ -448,10 +485,11 @@ int vec;
                }
                if (fd_status[0]&0xF8) {
 #ifdef FDOTHER
                }
                if (fd_status[0]&0xF8) {
 #ifdef FDOTHER
-                       printf("status0 err %d:",fd_status[0]);
+printf("status0 err %d:",fd_status[0]);
 #endif
                        goto retry;
                }
 #endif
                        goto retry;
                }
+/*
                if (fd_status[1]){
                        printf("status1 err %d:",fd_status[0]);
                        goto retry;
                if (fd_status[1]){
                        printf("status1 err %d:",fd_status[0]);
                        goto retry;
@@ -460,14 +498,18 @@ int vec;
                        printf("status2 err %d:",fd_status[0]);
                        goto retry;
                }
                        printf("status2 err %d:",fd_status[0]);
                        goto retry;
                }
+*/
                /* All OK */
                if (!kernel_space(bp->b_un.b_addr+fd_skip)) {
                        /* RAW transfer */
                /* All OK */
                if (!kernel_space(bp->b_un.b_addr+fd_skip)) {
                        /* RAW transfer */
-                       if (read) bcopy(fdrawbuf,bp->b_un.b_addr+fd_skip,
-                                       DEV_BSIZE);
+                       if (read) bcopy(dma_bounce[fd_dmachan]->b_un.b_addr,
+                               bp->b_un.b_addr+fd_skip, FDBLK);
                }
                fd_skip += FDBLK;
                if (fd_skip >= bp->b_bcount) {
                }
                fd_skip += FDBLK;
                if (fd_skip >= bp->b_bcount) {
+#ifdef FDTEST
+printf("DONE %d|", bp->b_blkno);
+#endif
                        /* ALL DONE */
                        fd_skip = 0;
                        bp->b_resid = 0;
                        /* ALL DONE */
                        fd_skip = 0;
                        bp->b_resid = 0;
@@ -476,26 +518,34 @@ int vec;
                        nextstate(dp);
 
                } else {
                        nextstate(dp);
 
                } else {
+#ifdef FDDEBUG
+printf("next|");
+#endif
                        /* set up next transfer */
                        blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/FDBLK
                                + fd_skip/FDBLK;
                        fd_state = 1;
                        bp->b_cylin = (blknum / (ft->sectrac * 2));
                        /* set up next transfer */
                        blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/FDBLK
                                + fd_skip/FDBLK;
                        fd_state = 1;
                        bp->b_cylin = (blknum / (ft->sectrac * 2));
-                       bp->b_cylin *= ft->steptrac;
                        if (bp->b_cylin != fd_track) {
                        if (bp->b_cylin != fd_track) {
+#ifdef FDTEST
+printf("Seek|");
+#endif
                                /* SEEK Necessary */
                                out_fdc(15);    /* Seek function */
                                out_fdc(fd_drive);/* Drive number */
                                /* SEEK Necessary */
                                out_fdc(15);    /* Seek function */
                                out_fdc(fd_drive);/* Drive number */
-                               out_fdc(bp->b_cylin);
+                               out_fdc(bp->b_cylin * dp->b_step);
                                break;
                                break;
-                       } else fdintr();
+                       } else fdintr(0xff);
                }
                break;
        case 3:
                }
                break;
        case 3:
+#ifdef FDOTHER
+printf("Seek %d %d\n", bp->b_cylin, dp->b_step);
+#endif
                /* Seek necessary */
                out_fdc(15);    /* Seek function */
                out_fdc(fd_drive);/* Drive number */
                /* Seek necessary */
                out_fdc(15);    /* Seek function */
                out_fdc(fd_drive);/* Drive number */
-               out_fdc(bp->b_cylin);
+               out_fdc(bp->b_cylin * dp->b_step);
                fd_state = 1;
                break;
        case 4:
                fd_state = 1;
                break;
        case 4:
@@ -506,45 +556,48 @@ int vec;
                out_fdc(fd_drive);
                fd_state = 3;
                break;
                out_fdc(fd_drive);
                fd_state = 3;
                break;
+       case 5:
+#ifdef FDOTHER
+               printf("**RESET**\n");
+#endif
+               /* Try a reset, keep motor on */
+               set_motor(fd_drive,1);
+               set_motor(fd_drive,0);
+               outb(fdc+fdctl,ft->trans);
+               fd_retry++;
+               fd_state = 4;
+               break;
        default:
        default:
-#ifdef FDDEBUG
                printf("Unexpected FD int->");
                out_fdc(0x8);
                i = in_fdc();
                sec = in_fdc();
                printf("Unexpected FD int->");
                out_fdc(0x8);
                i = in_fdc();
                sec = in_fdc();
-               printf("ST0 = %X, PCN = %X\n",i,sec);
+               printf("ST0 = %lx, PCN = %lx\n",i,sec);
                out_fdc(0x4A); 
                out_fdc(fd_drive);
                for(i=0;i<7;i++) {
                        fd_status[i] = in_fdc();
                }
                out_fdc(0x4A); 
                out_fdc(fd_drive);
                for(i=0;i<7;i++) {
                        fd_status[i] = in_fdc();
                }
-       printf("intr status :%X %X %X %X %X %X %X ",
+       printf("intr status :%lx %lx %lx %lx %lx %lx %lx ",
                fd_status[0], fd_status[1], fd_status[2], fd_status[3],
                fd_status[4], fd_status[5], fd_status[6] );
                fd_status[0], fd_status[1], fd_status[2], fd_status[3],
                fd_status[4], fd_status[5], fd_status[6] );
-#endif
                break;
        }
        return;
 retry:
        switch(fd_retry) {
        case 0: case 1:
                break;
        }
        return;
 retry:
        switch(fd_retry) {
        case 0: case 1:
+       case 2: case 3:
                break;
                break;
-       case 2:
-#ifdef FDDEBUG
-               printf("**RESET**\n");
-#endif
-               /* Try a reset, keep motor on */
-               set_motor(fd_drive,1);
-               set_motor(fd_drive,0);
-               outb(0x3f7,ft->trans);
+       case 4:
                fd_retry++;
                fd_retry++;
-               fd_state = 4;
+               fd_state = 5;
+               fdintr(0xff);
                return;
                return;
-       case 3: case 4:
-       case 5: case 6:
+       case 5: case 6: case 7:
                break;
        default:
                break;
        default:
-               printf("FD err %X %X %X %X %X %X %X\n",
+               printf("FD err %lx %lx %lx %lx %lx %lx %lx\n",
                fd_status[0], fd_status[1], fd_status[2], fd_status[3],
                fd_status[4], fd_status[5], fd_status[6] );
                badtrans(dp,bp);
                fd_status[0], fd_status[1], fd_status[2], fd_status[3],
                fd_status[4], fd_status[5], fd_status[6] );
                badtrans(dp,bp);
@@ -552,7 +605,7 @@ retry:
        }
        fd_state = 1;
        fd_retry++;
        }
        fd_state = 1;
        fd_retry++;
-       fdintr();
+       fdintr(0xff);
 }
 
 badtrans(dp,bp)
 }
 
 badtrans(dp,bp)
@@ -580,17 +633,39 @@ struct buf *dp,*bp;
 nextstate(dp)
 struct buf *dp;
 {
 nextstate(dp)
 struct buf *dp;
 {
-       struct buf *dpother;
        
        
-       dpother = &fd_unit[fd_drive ? 0 : 1].head;
-       if (dp->b_actf) fdstart(fd_drive);
-       else if (dpother->b_actf) {
-               dp->b_active = 0;
-               fdstart(fd_drive ? 0 : 1);
-       } else {
-               untimeout(fd_turnoff,fd_drive);
-               timeout(fd_turnoff,fd_drive,5*hz);
-               fd_state = 0;
-               dp->b_active = 0;
+       if (dp->b_actf) 
+               fdstart(fd_drive);
+       else {
+#if NFD > 1
+               struct buf *dpother;
+
+               dpother = &fd_unit[fd_drive ? 0 : 1].head;
+
+               if (dpother->b_actf) {
+#ifdef FDTEST
+printf("switch|");
+#endif
+                       untimeout(fd_turnoff,fd_drive);
+                       timeout(fd_turnoff,fd_drive,5*hz);
+                       fd_drive = 1 - fd_drive;
+                       dp->b_active = 0;
+                       dpother->b_active = 1;
+                       fdstart(fd_drive);
+               } else 
+#endif
+               {
+#ifdef FDTEST
+printf("off|");
+#endif
+                       untimeout(fd_turnoff,fd_drive);
+                       timeout(fd_turnoff,fd_drive,5*hz);
+                       fd_state = 0;
+                       dp->b_active = 0;
+               }
        }
 }
        }
 }
+
+Fdioctl() {}
+Fddump() {}
+#endif