187f6c662SJulian Elischer /* 25b81b6b3SRodney W. Grimes * Copyright (c) 1990 The Regents of the University of California. 35b81b6b3SRodney W. Grimes * All rights reserved. 45b81b6b3SRodney W. Grimes * 55b81b6b3SRodney W. Grimes * This code is derived from software contributed to Berkeley by 65b81b6b3SRodney W. Grimes * Don Ahn. 75b81b6b3SRodney W. Grimes * 8dc16046fSJoerg Wunsch * Copyright (c) 1993, 1994 by 93a2f7427SDavid Greenman * jc@irbs.UUCP (John Capo) 103a2f7427SDavid Greenman * vak@zebub.msk.su (Serge Vakulenko) 113a2f7427SDavid Greenman * ache@astral.msk.su (Andrew A. Chernov) 12dc16046fSJoerg Wunsch * 13dc16046fSJoerg Wunsch * Copyright (c) 1993, 1994, 1995 by 143a2f7427SDavid Greenman * joerg_wunsch@uriah.sax.de (Joerg Wunsch) 15dc5df763SJoerg Wunsch * dufault@hda.com (Peter Dufault) 163a2f7427SDavid Greenman * 175b81b6b3SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 185b81b6b3SRodney W. Grimes * modification, are permitted provided that the following conditions 195b81b6b3SRodney W. Grimes * are met: 205b81b6b3SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 215b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 225b81b6b3SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 235b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 245b81b6b3SRodney W. Grimes * documentation and/or other materials provided with the distribution. 255b81b6b3SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 265b81b6b3SRodney W. Grimes * must display the following acknowledgement: 275b81b6b3SRodney W. Grimes * This product includes software developed by the University of 285b81b6b3SRodney W. Grimes * California, Berkeley and its contributors. 295b81b6b3SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 305b81b6b3SRodney W. Grimes * may be used to endorse or promote products derived from this software 315b81b6b3SRodney W. Grimes * without specific prior written permission. 325b81b6b3SRodney W. Grimes * 335b81b6b3SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 345b81b6b3SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 355b81b6b3SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 365b81b6b3SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 375b81b6b3SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 385b81b6b3SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 395b81b6b3SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 405b81b6b3SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 415b81b6b3SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 425b81b6b3SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 435b81b6b3SRodney W. Grimes * SUCH DAMAGE. 445b81b6b3SRodney W. Grimes * 45dc4ff321SRodney W. Grimes * from: @(#)fd.c 7.4 (Berkeley) 5/25/91 46cba8a5ddSPoul-Henning Kamp * $Id: fd.c,v 1.90 1996/07/12 07:40:59 bde Exp $ 475b81b6b3SRodney W. Grimes * 485b81b6b3SRodney W. Grimes */ 495b81b6b3SRodney W. Grimes 50b99f0a4aSAndrew Moore #include "ft.h" 51b99f0a4aSAndrew Moore #if NFT < 1 52b99f0a4aSAndrew Moore #undef NFDC 53b99f0a4aSAndrew Moore #endif 545b81b6b3SRodney W. Grimes #include "fd.h" 555b81b6b3SRodney W. Grimes 56b99f0a4aSAndrew Moore #if NFDC > 0 57b99f0a4aSAndrew Moore 58b99f0a4aSAndrew Moore #include <sys/param.h> 59b99f0a4aSAndrew Moore #include <sys/systm.h> 60b99f0a4aSAndrew Moore #include <sys/kernel.h> 61b99f0a4aSAndrew Moore #include <sys/conf.h> 62b99f0a4aSAndrew Moore #include <sys/file.h> 63b99f0a4aSAndrew Moore #include <sys/ioctl.h> 64671e2ceeSBruce Evans #include <machine/clock.h> 65b99f0a4aSAndrew Moore #include <machine/ioctl_fd.h> 66b99f0a4aSAndrew Moore #include <sys/disklabel.h> 67b99f0a4aSAndrew Moore #include <sys/buf.h> 68b99f0a4aSAndrew Moore #include <sys/uio.h> 69b99f0a4aSAndrew Moore #include <sys/malloc.h> 703a2f7427SDavid Greenman #include <sys/proc.h> 71b99f0a4aSAndrew Moore #include <sys/syslog.h> 7292200632SGarrett Wollman #include <sys/devconf.h> 7392200632SGarrett Wollman #include <sys/dkstat.h> 74f540b106SGarrett Wollman #include <i386/isa/isa.h> 75f540b106SGarrett Wollman #include <i386/isa/isa_device.h> 76f540b106SGarrett Wollman #include <i386/isa/fdreg.h> 77f540b106SGarrett Wollman #include <i386/isa/fdc.h> 78f540b106SGarrett Wollman #include <i386/isa/rtc.h> 79dc5df763SJoerg Wunsch #include <machine/stdarg.h> 8087eafbcaSPoul-Henning Kamp #if NFT > 0 8187eafbcaSPoul-Henning Kamp #include <sys/ftape.h> 8287eafbcaSPoul-Henning Kamp #include <i386/isa/ftreg.h> 8387eafbcaSPoul-Henning Kamp #endif 848af5d536SJulian Elischer #ifdef DEVFS 858af5d536SJulian Elischer #include <sys/devfsext.h> 868af5d536SJulian Elischer #endif 875b81b6b3SRodney W. Grimes 8887f6c662SJulian Elischer 8992200632SGarrett Wollman static int fd_goaway(struct kern_devconf *, int); 9092200632SGarrett Wollman static int fdc_goaway(struct kern_devconf *, int); 914b2af45fSPoul-Henning Kamp static int fd_externalize(struct kern_devconf *, struct sysctl_req *); 925e235068SJordan K. Hubbard 935e235068SJordan K. Hubbard /* 945e235068SJordan K. Hubbard * Templates for the kern_devconf structures used when we attach. 955e235068SJordan K. Hubbard */ 965e235068SJordan K. Hubbard static struct kern_devconf kdc_fd[NFD] = { { 975e235068SJordan K. Hubbard 0, 0, 0, /* filled in by kern_devconf.c */ 985e235068SJordan K. Hubbard "fd", 0, { MDDT_DISK, 0 }, 995e235068SJordan K. Hubbard fd_externalize, 0, fd_goaway, DISK_EXTERNALLEN, 1005e235068SJordan K. Hubbard 0, /* parent */ 1015e235068SJordan K. Hubbard 0, /* parentdata */ 102dc16046fSJoerg Wunsch DC_UNCONFIGURED, /* state */ 1036c0081e9SGarrett Wollman "floppy disk", 1046c0081e9SGarrett Wollman DC_CLS_DISK /* class */ 1055e235068SJordan K. Hubbard } }; 1065e235068SJordan K. Hubbard 1075e235068SJordan K. Hubbard struct kern_devconf kdc_fdc[NFDC] = { { 1085e235068SJordan K. Hubbard 0, 0, 0, /* filled in by kern_devconf.c */ 1095e235068SJordan K. Hubbard "fdc", 0, { MDDT_ISA, 0, "bio" }, 1105e235068SJordan K. Hubbard isa_generic_externalize, 0, fdc_goaway, ISA_EXTERNALLEN, 1115e235068SJordan K. Hubbard 0, /* parent */ 1125e235068SJordan K. Hubbard 0, /* parentdata */ 113dc16046fSJoerg Wunsch DC_UNCONFIGURED, /* state */ 1146c0081e9SGarrett Wollman "floppy disk/tape controller", 1156c0081e9SGarrett Wollman DC_CLS_MISC /* class */ 1165e235068SJordan K. Hubbard } }; 1175e235068SJordan K. Hubbard 1185e235068SJordan K. Hubbard static inline void 1195e235068SJordan K. Hubbard fd_registerdev(int ctlr, int unit) 1205e235068SJordan K. Hubbard { 1215e235068SJordan K. Hubbard if(unit != 0) 1225e235068SJordan K. Hubbard kdc_fd[unit] = kdc_fd[0]; 1235e235068SJordan K. Hubbard 1245e235068SJordan K. Hubbard kdc_fd[unit].kdc_unit = unit; 1255e235068SJordan K. Hubbard kdc_fd[unit].kdc_parent = &kdc_fdc[ctlr]; 1265e235068SJordan K. Hubbard kdc_fd[unit].kdc_parentdata = 0; 1275e235068SJordan K. Hubbard dev_attach(&kdc_fd[unit]); 1285e235068SJordan K. Hubbard } 1295e235068SJordan K. Hubbard 1305e235068SJordan K. Hubbard static inline void 1315e235068SJordan K. Hubbard fdc_registerdev(struct isa_device *dvp) 1325e235068SJordan K. Hubbard { 1335e235068SJordan K. Hubbard int unit = dvp->id_unit; 1345e235068SJordan K. Hubbard 1355e235068SJordan K. Hubbard if(unit != 0) 1365e235068SJordan K. Hubbard kdc_fdc[unit] = kdc_fdc[0]; 1375e235068SJordan K. Hubbard 1385e235068SJordan K. Hubbard kdc_fdc[unit].kdc_unit = unit; 1395e235068SJordan K. Hubbard kdc_fdc[unit].kdc_parent = &kdc_isa0; 1405e235068SJordan K. Hubbard kdc_fdc[unit].kdc_parentdata = dvp; 1415e235068SJordan K. Hubbard dev_attach(&kdc_fdc[unit]); 1425e235068SJordan K. Hubbard } 1435e235068SJordan K. Hubbard 1445e235068SJordan K. Hubbard static int 1455e235068SJordan K. Hubbard fdc_goaway(struct kern_devconf *kdc, int force) 1465e235068SJordan K. Hubbard { 1475e235068SJordan K. Hubbard if(force) { 1485e235068SJordan K. Hubbard dev_detach(kdc); 1495e235068SJordan K. Hubbard return 0; 1505e235068SJordan K. Hubbard } else { 1515e235068SJordan K. Hubbard return EBUSY; /* XXX fix */ 1525e235068SJordan K. Hubbard } 1535e235068SJordan K. Hubbard } 1545e235068SJordan K. Hubbard 1555e235068SJordan K. Hubbard static int 1565e235068SJordan K. Hubbard fd_goaway(struct kern_devconf *kdc, int force) 1575e235068SJordan K. Hubbard { 1585e235068SJordan K. Hubbard dev_detach(kdc); 1595e235068SJordan K. Hubbard return 0; 1605e235068SJordan K. Hubbard } 16192200632SGarrett Wollman 162671e2ceeSBruce Evans #define b_cylin b_resid /* XXX now spelled b_cylinder elsewhere */ 1635b81b6b3SRodney W. Grimes 164b39c878eSAndrey A. Chernov /* misuse a flag to identify format operation */ 165b39c878eSAndrey A. Chernov #define B_FORMAT B_XXX 1665b81b6b3SRodney W. Grimes 1673a2f7427SDavid Greenman /* 1683a2f7427SDavid Greenman * this biotab field doubles as a field for the physical unit number 1693a2f7427SDavid Greenman * on the controller 1703a2f7427SDavid Greenman */ 1713a2f7427SDavid Greenman #define id_physid id_scsiid 1723a2f7427SDavid Greenman 173dc5df763SJoerg Wunsch /* error returns for fd_cmd() */ 174dc5df763SJoerg Wunsch #define FD_FAILED -1 175dc5df763SJoerg Wunsch #define FD_NOT_VALID -2 176dc5df763SJoerg Wunsch #define FDC_ERRMAX 100 /* do not log more */ 177dc5df763SJoerg Wunsch 178b39c878eSAndrey A. Chernov #define NUMTYPES 14 179b39c878eSAndrey A. Chernov #define NUMDENS (NUMTYPES - 6) 1807ca0641bSAndrey A. Chernov 1813a2f7427SDavid Greenman /* These defines (-1) must match index for fd_types */ 182b99f0a4aSAndrew Moore #define F_TAPE_TYPE 0x020 /* bit for fd_types to indicate tape */ 183b99f0a4aSAndrew Moore #define NO_TYPE 0 /* must match NO_TYPE in ft.c */ 184b99f0a4aSAndrew Moore #define FD_1720 1 185b99f0a4aSAndrew Moore #define FD_1480 2 186b99f0a4aSAndrew Moore #define FD_1440 3 187b99f0a4aSAndrew Moore #define FD_1200 4 188b99f0a4aSAndrew Moore #define FD_820 5 189b99f0a4aSAndrew Moore #define FD_800 6 190b99f0a4aSAndrew Moore #define FD_720 7 191b99f0a4aSAndrew Moore #define FD_360 8 192ed2fa05eSAndrey A. Chernov 193b99f0a4aSAndrew Moore #define FD_1480in5_25 9 194b99f0a4aSAndrew Moore #define FD_1440in5_25 10 195b99f0a4aSAndrew Moore #define FD_820in5_25 11 196b99f0a4aSAndrew Moore #define FD_800in5_25 12 197b99f0a4aSAndrew Moore #define FD_720in5_25 13 198b99f0a4aSAndrew Moore #define FD_360in5_25 14 199b99f0a4aSAndrew Moore 2007ca0641bSAndrey A. Chernov 2016f4e0bebSPoul-Henning Kamp static struct fd_type fd_types[NUMTYPES] = 2025b81b6b3SRodney W. Grimes { 203126518a1SAndrey A. Chernov { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */ 204126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */ 205126518a1SAndrey A. Chernov { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */ 206126518a1SAndrey A. Chernov { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /* 1.2M in HD 5.25/3.5 */ 207126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /* 820K in HD 3.5in */ 208126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /* 800K in HD 3.5in */ 209126518a1SAndrey A. Chernov { 9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /* 720K in HD 3.5in */ 210b0568305SAndrey A. Chernov { 9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /* 360K in DD 5.25in */ 211ed2fa05eSAndrey A. Chernov 212126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */ 213126518a1SAndrey A. Chernov { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */ 214126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /* 820K in HD 5.25in */ 215126518a1SAndrey A. Chernov { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /* 800K in HD 5.25in */ 216126518a1SAndrey A. Chernov { 9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /* 720K in HD 5.25in */ 217126518a1SAndrey A. Chernov { 9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /* 360K in HD 5.25in */ 2185b81b6b3SRodney W. Grimes }; 2195b81b6b3SRodney W. Grimes 220b99f0a4aSAndrew Moore #define DRVS_PER_CTLR 2 /* 2 floppies */ 221dc16046fSJoerg Wunsch 2225b81b6b3SRodney W. Grimes /***********************************************************************\ 2235b81b6b3SRodney W. Grimes * Per controller structure. * 2245b81b6b3SRodney W. Grimes \***********************************************************************/ 225b99f0a4aSAndrew Moore struct fdc_data fdc_data[NFDC]; 2265b81b6b3SRodney W. Grimes 2275b81b6b3SRodney W. Grimes /***********************************************************************\ 2285b81b6b3SRodney W. Grimes * Per drive structure. * 229b99f0a4aSAndrew Moore * N per controller (DRVS_PER_CTLR) * 2305b81b6b3SRodney W. Grimes \***********************************************************************/ 2316f4e0bebSPoul-Henning Kamp static struct fd_data { 232b99f0a4aSAndrew Moore struct fdc_data *fdc; /* pointer to controller structure */ 2335b81b6b3SRodney W. Grimes int fdsu; /* this units number on this controller */ 2343a2f7427SDavid Greenman int type; /* Drive type (FD_1440...) */ 2355b81b6b3SRodney W. Grimes struct fd_type *ft; /* pointer to the type descriptor */ 2365b81b6b3SRodney W. Grimes int flags; 2375b81b6b3SRodney W. Grimes #define FD_OPEN 0x01 /* it's open */ 2385b81b6b3SRodney W. Grimes #define FD_ACTIVE 0x02 /* it's active */ 2395b81b6b3SRodney W. Grimes #define FD_MOTOR 0x04 /* motor should be on */ 2405b81b6b3SRodney W. Grimes #define FD_MOTOR_WAIT 0x08 /* motor coming up */ 2415b81b6b3SRodney W. Grimes int skip; 2425b81b6b3SRodney W. Grimes int hddrv; 243dc5df763SJoerg Wunsch #define FD_NO_TRACK -2 2445b81b6b3SRodney W. Grimes int track; /* where we think the head is */ 2453a2f7427SDavid Greenman int options; /* user configurable options, see ioctl_fd.h */ 24692200632SGarrett Wollman int dkunit; /* disk stats unit number */ 24787f6c662SJulian Elischer #ifdef DEVFS 24821519754SBruce Evans void *bdevs[1 + NUMDENS + MAXPARTITIONS]; 24921519754SBruce Evans void *cdevs[1 + NUMDENS + MAXPARTITIONS]; 25087f6c662SJulian Elischer #endif 2515b81b6b3SRodney W. Grimes } fd_data[NFD]; 2525b81b6b3SRodney W. Grimes 2535b81b6b3SRodney W. Grimes /***********************************************************************\ 2545b81b6b3SRodney W. Grimes * Throughout this file the following conventions will be used: * 2555b81b6b3SRodney W. Grimes * fd is a pointer to the fd_data struct for the drive in question * 2565b81b6b3SRodney W. Grimes * fdc is a pointer to the fdc_data struct for the controller * 2575b81b6b3SRodney W. Grimes * fdu is the floppy drive unit number * 2585b81b6b3SRodney W. Grimes * fdcu is the floppy controller unit number * 2595b81b6b3SRodney W. Grimes * fdsu is the floppy drive unit number on that controller. (sub-unit) * 2605b81b6b3SRodney W. Grimes \***********************************************************************/ 261b99f0a4aSAndrew Moore 2623a2f7427SDavid Greenman #if NFT > 0 2633a2f7427SDavid Greenman int ftopen(dev_t, int); 2643a2f7427SDavid Greenman int ftintr(ftu_t ftu); 2653a2f7427SDavid Greenman int ftclose(dev_t, int); 2663a2f7427SDavid Greenman void ftstrategy(struct buf *); 2673a2f7427SDavid Greenman int ftioctl(dev_t, int, caddr_t, int, struct proc *); 2683a2f7427SDavid Greenman int ftdump(dev_t); 2693a2f7427SDavid Greenman int ftsize(dev_t); 27074fa89f4SRodney W. Grimes int ftattach(struct isa_device *, struct isa_device *, int); 2713a2f7427SDavid Greenman #endif 2725b81b6b3SRodney W. Grimes 2733a2f7427SDavid Greenman /* autoconfig functions */ 2743a2f7427SDavid Greenman static int fdprobe(struct isa_device *); 2753a2f7427SDavid Greenman static int fdattach(struct isa_device *); 2763a2f7427SDavid Greenman 2773a2f7427SDavid Greenman /* needed for ft driver, thus exported */ 2783a2f7427SDavid Greenman int in_fdc(fdcu_t); 2793a2f7427SDavid Greenman int out_fdc(fdcu_t, int); 2803a2f7427SDavid Greenman 2813a2f7427SDavid Greenman /* internal functions */ 2823a2f7427SDavid Greenman static void set_motor(fdcu_t, int, int); 2833a2f7427SDavid Greenman # define TURNON 1 2843a2f7427SDavid Greenman # define TURNOFF 0 2853a2f7427SDavid Greenman static timeout_t fd_turnoff; 2863a2f7427SDavid Greenman static timeout_t fd_motor_on; 2873a2f7427SDavid Greenman static void fd_turnon(fdu_t); 2883a2f7427SDavid Greenman static void fdc_reset(fdc_p); 289b5e8ce9fSBruce Evans static int fd_in(fdcu_t, int *); 2903a2f7427SDavid Greenman static void fdstart(fdcu_t); 2913a2f7427SDavid Greenman static timeout_t fd_timeout; 2923a2f7427SDavid Greenman static timeout_t fd_pseudointr; 2933a2f7427SDavid Greenman static int fdstate(fdcu_t, fdc_p); 294aaf08d94SGarrett Wollman static int retrier(fdcu_t); 2953a2f7427SDavid Greenman static int fdformat(dev_t, struct fd_formb *, struct proc *); 2963a2f7427SDavid Greenman 297aaf08d94SGarrett Wollman 2985b81b6b3SRodney W. Grimes #define DEVIDLE 0 2995b81b6b3SRodney W. Grimes #define FINDWORK 1 3005b81b6b3SRodney W. Grimes #define DOSEEK 2 3015b81b6b3SRodney W. Grimes #define SEEKCOMPLETE 3 3025b81b6b3SRodney W. Grimes #define IOCOMPLETE 4 3035b81b6b3SRodney W. Grimes #define RECALCOMPLETE 5 3045b81b6b3SRodney W. Grimes #define STARTRECAL 6 3055b81b6b3SRodney W. Grimes #define RESETCTLR 7 3065b81b6b3SRodney W. Grimes #define SEEKWAIT 8 3075b81b6b3SRodney W. Grimes #define RECALWAIT 9 3085b81b6b3SRodney W. Grimes #define MOTORWAIT 10 3095b81b6b3SRodney W. Grimes #define IOTIMEDOUT 11 3105b81b6b3SRodney W. Grimes 3115b81b6b3SRodney W. Grimes #ifdef DEBUG 312cba2a7c6SBruce Evans static char const * const fdstates[] = 3135b81b6b3SRodney W. Grimes { 3145b81b6b3SRodney W. Grimes "DEVIDLE", 3155b81b6b3SRodney W. Grimes "FINDWORK", 3165b81b6b3SRodney W. Grimes "DOSEEK", 3175b81b6b3SRodney W. Grimes "SEEKCOMPLETE", 3185b81b6b3SRodney W. Grimes "IOCOMPLETE", 3195b81b6b3SRodney W. Grimes "RECALCOMPLETE", 3205b81b6b3SRodney W. Grimes "STARTRECAL", 3215b81b6b3SRodney W. Grimes "RESETCTLR", 3225b81b6b3SRodney W. Grimes "SEEKWAIT", 3235b81b6b3SRodney W. Grimes "RECALWAIT", 3245b81b6b3SRodney W. Grimes "MOTORWAIT", 3255b81b6b3SRodney W. Grimes "IOTIMEDOUT" 3265b81b6b3SRodney W. Grimes }; 3275b81b6b3SRodney W. Grimes 3283a2f7427SDavid Greenman /* CAUTION: fd_debug causes huge amounts of logging output */ 329cba2a7c6SBruce Evans static int volatile fd_debug = 0; 3305b81b6b3SRodney W. Grimes #define TRACE0(arg) if(fd_debug) printf(arg) 3315b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2) 332381fe1aaSGarrett Wollman #else /* DEBUG */ 3335b81b6b3SRodney W. Grimes #define TRACE0(arg) 3345b81b6b3SRodney W. Grimes #define TRACE1(arg1, arg2) 335381fe1aaSGarrett Wollman #endif /* DEBUG */ 3365b81b6b3SRodney W. Grimes 337dc16046fSJoerg Wunsch /* autoconfig structure */ 338dc16046fSJoerg Wunsch 339dc16046fSJoerg Wunsch struct isa_driver fdcdriver = { 340dc16046fSJoerg Wunsch fdprobe, fdattach, "fdc", 341dc16046fSJoerg Wunsch }; 342dc16046fSJoerg Wunsch 34387f6c662SJulian Elischer static d_open_t Fdopen; /* NOTE, not fdopen */ 34487f6c662SJulian Elischer static d_close_t fdclose; 34587f6c662SJulian Elischer static d_ioctl_t fdioctl; 34687f6c662SJulian Elischer static d_strategy_t fdstrategy; 34787f6c662SJulian Elischer 34887f6c662SJulian Elischer #define CDEV_MAJOR 9 34987f6c662SJulian Elischer #define BDEV_MAJOR 2 350cba8a5ddSPoul-Henning Kamp static struct cdevsw fd_cdevsw; 351d2f265faSPoul-Henning Kamp static struct bdevsw fd_bdevsw = 35287f6c662SJulian Elischer { Fdopen, fdclose, fdstrategy, fdioctl, /*2*/ 353f332b8a1SBruce Evans nodump, nopsize, 0, "fd", &fd_cdevsw, -1 }; 35487f6c662SJulian Elischer 35587f6c662SJulian Elischer 3566f4e0bebSPoul-Henning Kamp static struct isa_device *fdcdevs[NFDC]; 35792200632SGarrett Wollman 35892200632SGarrett Wollman /* 35992200632SGarrett Wollman * Provide hw.devconf information. 36092200632SGarrett Wollman */ 36192200632SGarrett Wollman static int 3624b2af45fSPoul-Henning Kamp fd_externalize(struct kern_devconf *kdc, struct sysctl_req *req) 36392200632SGarrett Wollman { 3644b2af45fSPoul-Henning Kamp return disk_externalize(fd_data[kdc->kdc_unit].fdsu, req); 36592200632SGarrett Wollman } 36692200632SGarrett Wollman 367dc5df763SJoerg Wunsch static int 368dc5df763SJoerg Wunsch fdc_err(fdcu_t fdcu, const char *s) 369dc5df763SJoerg Wunsch { 370dc5df763SJoerg Wunsch fdc_data[fdcu].fdc_errs++; 37116b04b6aSJoerg Wunsch if(s) { 372dc5df763SJoerg Wunsch if(fdc_data[fdcu].fdc_errs < FDC_ERRMAX) 3736a0e6f42SRodney W. Grimes printf("fdc%d: %s", fdcu, s); 374dc5df763SJoerg Wunsch else if(fdc_data[fdcu].fdc_errs == FDC_ERRMAX) 375dc5df763SJoerg Wunsch printf("fdc%d: too many errors, not logging any more\n", 376dc5df763SJoerg Wunsch fdcu); 37716b04b6aSJoerg Wunsch } 378dc5df763SJoerg Wunsch 379dc5df763SJoerg Wunsch return FD_FAILED; 380dc5df763SJoerg Wunsch } 381dc5df763SJoerg Wunsch 382dc5df763SJoerg Wunsch /* 383dc5df763SJoerg Wunsch * fd_cmd: Send a command to the chip. Takes a varargs with this structure: 384dc5df763SJoerg Wunsch * Unit number, 385dc5df763SJoerg Wunsch * # of output bytes, output bytes as ints ..., 386dc5df763SJoerg Wunsch * # of input bytes, input bytes as ints ... 387dc5df763SJoerg Wunsch */ 388dc5df763SJoerg Wunsch 3896f4e0bebSPoul-Henning Kamp static int 390dc5df763SJoerg Wunsch fd_cmd(fdcu_t fdcu, int n_out, ...) 391dc5df763SJoerg Wunsch { 392dc5df763SJoerg Wunsch u_char cmd; 393dc5df763SJoerg Wunsch int n_in; 394dc5df763SJoerg Wunsch int n; 395dc5df763SJoerg Wunsch va_list ap; 396dc5df763SJoerg Wunsch 397dc5df763SJoerg Wunsch va_start(ap, n_out); 398dc5df763SJoerg Wunsch cmd = (u_char)(va_arg(ap, int)); 399dc5df763SJoerg Wunsch va_end(ap); 400dc5df763SJoerg Wunsch va_start(ap, n_out); 401dc5df763SJoerg Wunsch for (n = 0; n < n_out; n++) 402dc5df763SJoerg Wunsch { 403dc5df763SJoerg Wunsch if (out_fdc(fdcu, va_arg(ap, int)) < 0) 404dc5df763SJoerg Wunsch { 405dc5df763SJoerg Wunsch char msg[50]; 406dc5df763SJoerg Wunsch sprintf(msg, 407dc5df763SJoerg Wunsch "cmd %x failed at out byte %d of %d\n", 408dc5df763SJoerg Wunsch cmd, n + 1, n_out); 409dc5df763SJoerg Wunsch return fdc_err(fdcu, msg); 410dc5df763SJoerg Wunsch } 411dc5df763SJoerg Wunsch } 412dc5df763SJoerg Wunsch n_in = va_arg(ap, int); 413dc5df763SJoerg Wunsch for (n = 0; n < n_in; n++) 414dc5df763SJoerg Wunsch { 415dc5df763SJoerg Wunsch int *ptr = va_arg(ap, int *); 416dc5df763SJoerg Wunsch if (fd_in(fdcu, ptr) < 0) 417dc5df763SJoerg Wunsch { 418dc5df763SJoerg Wunsch char msg[50]; 419dc5df763SJoerg Wunsch sprintf(msg, 420dc5df763SJoerg Wunsch "cmd %02x failed at in byte %d of %d\n", 421dc5df763SJoerg Wunsch cmd, n + 1, n_in); 422dc5df763SJoerg Wunsch return fdc_err(fdcu, msg); 423dc5df763SJoerg Wunsch } 424dc5df763SJoerg Wunsch } 425dc5df763SJoerg Wunsch 426dc5df763SJoerg Wunsch return 0; 427dc5df763SJoerg Wunsch } 428dc5df763SJoerg Wunsch 4296f4e0bebSPoul-Henning Kamp static int 430dc5df763SJoerg Wunsch fd_sense_drive_status(fdc_p fdc, int *st3p) 431dc5df763SJoerg Wunsch { 432dc5df763SJoerg Wunsch int st3; 433dc5df763SJoerg Wunsch 434dc5df763SJoerg Wunsch if (fd_cmd(fdc->fdcu, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3)) 435dc5df763SJoerg Wunsch { 4366a0e6f42SRodney W. Grimes return fdc_err(fdc->fdcu, "Sense Drive Status failed\n"); 437dc5df763SJoerg Wunsch } 438dc5df763SJoerg Wunsch if (st3p) 439dc5df763SJoerg Wunsch *st3p = st3; 440dc5df763SJoerg Wunsch 441dc5df763SJoerg Wunsch return 0; 442dc5df763SJoerg Wunsch } 443dc5df763SJoerg Wunsch 4446f4e0bebSPoul-Henning Kamp static int 445dc5df763SJoerg Wunsch fd_sense_int(fdc_p fdc, int *st0p, int *cylp) 446dc5df763SJoerg Wunsch { 447dc5df763SJoerg Wunsch int st0, cyl; 448dc5df763SJoerg Wunsch 449dc5df763SJoerg Wunsch int ret = fd_cmd(fdc->fdcu, 1, NE7CMD_SENSEI, 1, &st0); 450dc5df763SJoerg Wunsch 451dc5df763SJoerg Wunsch if (ret) 452dc5df763SJoerg Wunsch { 453dc5df763SJoerg Wunsch (void)fdc_err(fdc->fdcu, 454dc5df763SJoerg Wunsch "sense intr err reading stat reg 0\n"); 455dc5df763SJoerg Wunsch return ret; 456dc5df763SJoerg Wunsch } 457dc5df763SJoerg Wunsch 458dc5df763SJoerg Wunsch if (st0p) 459dc5df763SJoerg Wunsch *st0p = st0; 460dc5df763SJoerg Wunsch 461dc5df763SJoerg Wunsch if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) 462dc5df763SJoerg Wunsch { 463dc5df763SJoerg Wunsch /* 464dc5df763SJoerg Wunsch * There doesn't seem to have been an interrupt. 465dc5df763SJoerg Wunsch */ 466dc5df763SJoerg Wunsch return FD_NOT_VALID; 467dc5df763SJoerg Wunsch } 468dc5df763SJoerg Wunsch 469dc5df763SJoerg Wunsch if (fd_in(fdc->fdcu, &cyl) < 0) 470dc5df763SJoerg Wunsch { 471dc5df763SJoerg Wunsch return fdc_err(fdc->fdcu, "can't get cyl num\n"); 472dc5df763SJoerg Wunsch } 473dc5df763SJoerg Wunsch 474dc5df763SJoerg Wunsch if (cylp) 475dc5df763SJoerg Wunsch *cylp = cyl; 476dc5df763SJoerg Wunsch 477dc5df763SJoerg Wunsch return 0; 478dc5df763SJoerg Wunsch } 479dc5df763SJoerg Wunsch 480dc5df763SJoerg Wunsch 4816f4e0bebSPoul-Henning Kamp static int 482dc5df763SJoerg Wunsch fd_read_status(fdc_p fdc, int fdsu) 483dc5df763SJoerg Wunsch { 484dc5df763SJoerg Wunsch int i, ret; 485b5e8ce9fSBruce Evans 486dc5df763SJoerg Wunsch for (i = 0; i < 7; i++) 487dc5df763SJoerg Wunsch { 488b5e8ce9fSBruce Evans /* 489b5e8ce9fSBruce Evans * XXX types are poorly chosen. Only bytes can by read 490b5e8ce9fSBruce Evans * from the hardware, but fdc_status wants u_longs and 491b5e8ce9fSBruce Evans * fd_in() gives ints. 492b5e8ce9fSBruce Evans */ 493b5e8ce9fSBruce Evans int status; 494b5e8ce9fSBruce Evans 495b5e8ce9fSBruce Evans ret = fd_in(fdc->fdcu, &status); 496b5e8ce9fSBruce Evans fdc->status[i] = status; 497b5e8ce9fSBruce Evans if (ret != 0) 498dc5df763SJoerg Wunsch break; 499dc5df763SJoerg Wunsch } 500dc5df763SJoerg Wunsch 501dc5df763SJoerg Wunsch if (ret == 0) 502dc5df763SJoerg Wunsch fdc->flags |= FDC_STAT_VALID; 503dc5df763SJoerg Wunsch else 504dc5df763SJoerg Wunsch fdc->flags &= ~FDC_STAT_VALID; 505dc5df763SJoerg Wunsch 506dc5df763SJoerg Wunsch return ret; 507dc5df763SJoerg Wunsch } 508dc5df763SJoerg Wunsch 5095b81b6b3SRodney W. Grimes /****************************************************************************/ 5105b81b6b3SRodney W. Grimes /* autoconfiguration stuff */ 5115b81b6b3SRodney W. Grimes /****************************************************************************/ 512dc5df763SJoerg Wunsch 5135b81b6b3SRodney W. Grimes /* 5145b81b6b3SRodney W. Grimes * probe for existance of controller 5155b81b6b3SRodney W. Grimes */ 5163a2f7427SDavid Greenman static int 517dc5df763SJoerg Wunsch fdprobe(struct isa_device *dev) 5185b81b6b3SRodney W. Grimes { 5195b81b6b3SRodney W. Grimes fdcu_t fdcu = dev->id_unit; 5205b81b6b3SRodney W. Grimes if(fdc_data[fdcu].flags & FDC_ATTACHED) 5215b81b6b3SRodney W. Grimes { 5226a0e6f42SRodney W. Grimes printf("fdc%d: unit used multiple times\n", fdcu); 5235b81b6b3SRodney W. Grimes return 0; 5245b81b6b3SRodney W. Grimes } 5255b81b6b3SRodney W. Grimes 52692200632SGarrett Wollman fdcdevs[fdcu] = dev; 5275b81b6b3SRodney W. Grimes fdc_data[fdcu].baseport = dev->id_iobase; 5285b81b6b3SRodney W. Grimes 5296c0081e9SGarrett Wollman #ifndef DEV_LKM 5306c0081e9SGarrett Wollman fdc_registerdev(dev); 5316c0081e9SGarrett Wollman #endif 5326c0081e9SGarrett Wollman 53316111cedSAndrew Moore /* First - lets reset the floppy controller */ 5343a2f7427SDavid Greenman outb(dev->id_iobase+FDOUT, 0); 53516111cedSAndrew Moore DELAY(100); 5363a2f7427SDavid Greenman outb(dev->id_iobase+FDOUT, FDO_FRST); 53716111cedSAndrew Moore 5385b81b6b3SRodney W. Grimes /* see if it can handle a command */ 539dc5df763SJoerg Wunsch if (fd_cmd(fdcu, 540dc5df763SJoerg Wunsch 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 541dc5df763SJoerg Wunsch 0)) 5425b81b6b3SRodney W. Grimes { 5435b81b6b3SRodney W. Grimes return(0); 5445b81b6b3SRodney W. Grimes } 545dc16046fSJoerg Wunsch kdc_fdc[fdcu].kdc_state = DC_IDLE; 5465b81b6b3SRodney W. Grimes return (IO_FDCSIZE); 5475b81b6b3SRodney W. Grimes } 5485b81b6b3SRodney W. Grimes 5495b81b6b3SRodney W. Grimes /* 5505b81b6b3SRodney W. Grimes * wire controller into system, look for floppy units 5515b81b6b3SRodney W. Grimes */ 5523a2f7427SDavid Greenman static int 553dc5df763SJoerg Wunsch fdattach(struct isa_device *dev) 5545b81b6b3SRodney W. Grimes { 5553a2f7427SDavid Greenman unsigned fdt; 5565b81b6b3SRodney W. Grimes fdu_t fdu; 5575b81b6b3SRodney W. Grimes fdcu_t fdcu = dev->id_unit; 5585b81b6b3SRodney W. Grimes fdc_p fdc = fdc_data + fdcu; 5595b81b6b3SRodney W. Grimes fd_p fd; 560cba2a7c6SBruce Evans int fdsu, st0, st3, i; 561cba2a7c6SBruce Evans #if NFT > 0 562cba2a7c6SBruce Evans int unithasfd; 563cba2a7c6SBruce Evans #endif 564b99f0a4aSAndrew Moore struct isa_device *fdup; 565dc5df763SJoerg Wunsch int ic_type = 0; 566999422d7SJulian Elischer #ifdef DEVFS 567c8f2fe8dSBruce Evans int mynor; 56821519754SBruce Evans int typemynor; 56921519754SBruce Evans int typesize; 57021519754SBruce Evans #endif 57192200632SGarrett Wollman 5725b81b6b3SRodney W. Grimes fdc->fdcu = fdcu; 5735b81b6b3SRodney W. Grimes fdc->flags |= FDC_ATTACHED; 5745b81b6b3SRodney W. Grimes fdc->dmachan = dev->id_drq; 575100f78bbSSujal Patel /* Acquire the DMA channel forever, The driver will do the rest */ 576100f78bbSSujal Patel isa_dma_acquire(fdc->dmachan); 577dd87702aSBruce Evans isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */); 5785b81b6b3SRodney W. Grimes fdc->state = DEVIDLE; 5793a2f7427SDavid Greenman /* reset controller, turn motor off, clear fdout mirror reg */ 5803a2f7427SDavid Greenman outb(fdc->baseport + FDOUT, ((fdc->fdout = 0))); 58117542807SPoul-Henning Kamp TAILQ_INIT(&fdc->head); 5825b81b6b3SRodney W. Grimes 5835b81b6b3SRodney W. Grimes /* check for each floppy drive */ 584b99f0a4aSAndrew Moore for (fdup = isa_biotab_fdc; fdup->id_driver != 0; fdup++) { 585b99f0a4aSAndrew Moore if (fdup->id_iobase != dev->id_iobase) 586b99f0a4aSAndrew Moore continue; 587b99f0a4aSAndrew Moore fdu = fdup->id_unit; 588b99f0a4aSAndrew Moore fd = &fd_data[fdu]; 589b99f0a4aSAndrew Moore if (fdu >= (NFD+NFT)) 590b99f0a4aSAndrew Moore continue; 591b99f0a4aSAndrew Moore fdsu = fdup->id_physid; 592b99f0a4aSAndrew Moore /* look up what bios thinks we have */ 593b99f0a4aSAndrew Moore switch (fdu) { 594b99f0a4aSAndrew Moore case 0: fdt = (rtcin(RTC_FDISKETTE) & 0xf0); 595b99f0a4aSAndrew Moore break; 596b99f0a4aSAndrew Moore case 1: fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0); 597b99f0a4aSAndrew Moore break; 598b99f0a4aSAndrew Moore default: fdt = RTCFDT_NONE; 599b99f0a4aSAndrew Moore break; 600b99f0a4aSAndrew Moore } 6015b81b6b3SRodney W. Grimes /* is there a unit? */ 602b99f0a4aSAndrew Moore if ((fdt == RTCFDT_NONE) 603b99f0a4aSAndrew Moore #if NFT > 0 604b99f0a4aSAndrew Moore || (fdsu >= DRVS_PER_CTLR)) { 605b99f0a4aSAndrew Moore #else 606b99f0a4aSAndrew Moore ) { 60756ef0285SAndrew Moore fd->type = NO_TYPE; 608b99f0a4aSAndrew Moore #endif 609b99f0a4aSAndrew Moore #if NFT > 0 610b99f0a4aSAndrew Moore /* If BIOS says no floppy, or > 2nd device */ 611b99f0a4aSAndrew Moore /* Probe for and attach a floppy tape. */ 61274fa89f4SRodney W. Grimes /* Tell FT if there was already a disk */ 61374fa89f4SRodney W. Grimes /* with this unit number found. */ 61474fa89f4SRodney W. Grimes 61574fa89f4SRodney W. Grimes unithasfd = 0; 61674fa89f4SRodney W. Grimes if (fdu < NFD && fd->type != NO_TYPE) 61774fa89f4SRodney W. Grimes unithasfd = 1; 61885827d9cSJoerg Wunsch if (ftattach(dev, fdup, unithasfd)) 619b99f0a4aSAndrew Moore continue; 62056ef0285SAndrew Moore if (fdsu < DRVS_PER_CTLR) 621b99f0a4aSAndrew Moore fd->type = NO_TYPE; 62256ef0285SAndrew Moore #endif 6235b81b6b3SRodney W. Grimes continue; 624f5f7ba03SJordan K. Hubbard } 6255b81b6b3SRodney W. Grimes 6265b81b6b3SRodney W. Grimes /* select it */ 6273a2f7427SDavid Greenman set_motor(fdcu, fdsu, TURNON); 6286b7bd95bSJoerg Wunsch DELAY(1000000); /* 1 sec */ 629dc5df763SJoerg Wunsch 630dc5df763SJoerg Wunsch if (ic_type == 0 && 631dc5df763SJoerg Wunsch fd_cmd(fdcu, 1, NE7CMD_VERSION, 1, &ic_type) == 0) 632dc5df763SJoerg Wunsch { 6336a0e6f42SRodney W. Grimes printf("fdc%d: ", fdcu); 634dc5df763SJoerg Wunsch ic_type = (u_char)ic_type; 635dc5df763SJoerg Wunsch switch( ic_type ) { 636dc5df763SJoerg Wunsch case 0x80: 6376a0e6f42SRodney W. Grimes printf("NEC 765\n"); 638dc5df763SJoerg Wunsch fdc->fdct = FDC_NE765; 6396c0081e9SGarrett Wollman kdc_fdc[fdcu].kdc_description = 6406c0081e9SGarrett Wollman "NEC 765 floppy disk/tape controller"; 641dc5df763SJoerg Wunsch break; 642dc5df763SJoerg Wunsch case 0x81: 6436a0e6f42SRodney W. Grimes printf("Intel 82077\n"); 644dc5df763SJoerg Wunsch fdc->fdct = FDC_I82077; 6456c0081e9SGarrett Wollman kdc_fdc[fdcu].kdc_description = 6466c0081e9SGarrett Wollman "Intel 82077 floppy disk/tape controller"; 647dc5df763SJoerg Wunsch break; 648dc5df763SJoerg Wunsch case 0x90: 6496a0e6f42SRodney W. Grimes printf("NEC 72065B\n"); 650dc5df763SJoerg Wunsch fdc->fdct = FDC_NE72065; 6516c0081e9SGarrett Wollman kdc_fdc[fdcu].kdc_description = 6526c0081e9SGarrett Wollman "NEC 72065B floppy disk/tape controller"; 653dc5df763SJoerg Wunsch break; 654dc5df763SJoerg Wunsch default: 6556a0e6f42SRodney W. Grimes printf("unknown IC type %02x\n", ic_type); 656dc5df763SJoerg Wunsch fdc->fdct = FDC_UNKNOWN; 657dc5df763SJoerg Wunsch break; 6586b7bd95bSJoerg Wunsch } 659dc5df763SJoerg Wunsch } 660dc5df763SJoerg Wunsch if ((fd_cmd(fdcu, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) && 661dc5df763SJoerg Wunsch (st3 & NE7_ST3_T0)) { 662dc5df763SJoerg Wunsch /* if at track 0, first seek inwards */ 663dc5df763SJoerg Wunsch /* seek some steps: */ 664dc5df763SJoerg Wunsch (void)fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0); 665dc5df763SJoerg Wunsch DELAY(300000); /* ...wait a moment... */ 666dc5df763SJoerg Wunsch (void)fd_sense_int(fdc, 0, 0); /* make ctrlr happy */ 667dc5df763SJoerg Wunsch } 668dc5df763SJoerg Wunsch 669dc5df763SJoerg Wunsch /* If we're at track 0 first seek inwards. */ 670dc5df763SJoerg Wunsch if ((fd_sense_drive_status(fdc, &st3) == 0) && 671dc5df763SJoerg Wunsch (st3 & NE7_ST3_T0)) { 672dc5df763SJoerg Wunsch /* Seek some steps... */ 673dc5df763SJoerg Wunsch if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) { 674dc5df763SJoerg Wunsch /* ...wait a moment... */ 675dc5df763SJoerg Wunsch DELAY(300000); 676dc5df763SJoerg Wunsch /* make ctrlr happy: */ 677dc5df763SJoerg Wunsch (void)fd_sense_int(fdc, 0, 0); 678dc5df763SJoerg Wunsch } 679dc5df763SJoerg Wunsch } 680dc5df763SJoerg Wunsch 6816b7bd95bSJoerg Wunsch for(i = 0; i < 2; i++) { 6826b7bd95bSJoerg Wunsch /* 6836b7bd95bSJoerg Wunsch * we must recalibrate twice, just in case the 6846b7bd95bSJoerg Wunsch * heads have been beyond cylinder 76, since most 6856b7bd95bSJoerg Wunsch * FDCs still barf when attempting to recalibrate 6866b7bd95bSJoerg Wunsch * more than 77 steps 6876b7bd95bSJoerg Wunsch */ 688dc5df763SJoerg Wunsch /* go back to 0: */ 689dc5df763SJoerg Wunsch if (fd_cmd(fdcu, 2, NE7CMD_RECAL, fdsu, 0) == 0) { 6906b7bd95bSJoerg Wunsch /* a second being enough for full stroke seek*/ 6916b7bd95bSJoerg Wunsch DELAY(i == 0? 1000000: 300000); 6925b81b6b3SRodney W. Grimes 6936b7bd95bSJoerg Wunsch /* anything responding? */ 694dc5df763SJoerg Wunsch if (fd_sense_int(fdc, &st0, 0) == 0 && 695dc5df763SJoerg Wunsch (st0 & NE7_ST0_EC) == 0) 6966b7bd95bSJoerg Wunsch break; /* already probed succesfully */ 6976b7bd95bSJoerg Wunsch } 698dc5df763SJoerg Wunsch } 6996b7bd95bSJoerg Wunsch 7003a2f7427SDavid Greenman set_motor(fdcu, fdsu, TURNOFF); 7013a2f7427SDavid Greenman 7023a2f7427SDavid Greenman if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */ 7035b81b6b3SRodney W. Grimes continue; 7045b81b6b3SRodney W. Grimes 705dc5df763SJoerg Wunsch fd->track = FD_NO_TRACK; 706b99f0a4aSAndrew Moore fd->fdc = fdc; 707b99f0a4aSAndrew Moore fd->fdsu = fdsu; 7083a2f7427SDavid Greenman fd->options = 0; 7092d9d0204SRodney W. Grimes printf("fd%d: ", fdu); 7105b81b6b3SRodney W. Grimes 7116c0081e9SGarrett Wollman fd_registerdev(fdcu, fdu); 712b99f0a4aSAndrew Moore switch (fdt) { 7137ca0641bSAndrey A. Chernov case RTCFDT_12M: 7146a0e6f42SRodney W. Grimes printf("1.2MB 5.25in\n"); 715b99f0a4aSAndrew Moore fd->type = FD_1200; 7166c0081e9SGarrett Wollman kdc_fd[fdu].kdc_description = 7176c0081e9SGarrett Wollman "1.2MB (1200K) 5.25in floppy disk drive"; 7187ca0641bSAndrey A. Chernov break; 7197ca0641bSAndrey A. Chernov case RTCFDT_144M: 7206a0e6f42SRodney W. Grimes printf("1.44MB 3.5in\n"); 721b99f0a4aSAndrew Moore fd->type = FD_1440; 7226c0081e9SGarrett Wollman kdc_fd[fdu].kdc_description = 7236c0081e9SGarrett Wollman "1.44MB (1440K) 3.5in floppy disk drive"; 7247ca0641bSAndrey A. Chernov break; 725290dd077SJoerg Wunsch case RTCFDT_288M: 72686a727d9SJoerg Wunsch case RTCFDT_288M_1: 7276a0e6f42SRodney W. Grimes printf("2.88MB 3.5in - 1.44MB mode\n"); 728290dd077SJoerg Wunsch fd->type = FD_1440; 7296c0081e9SGarrett Wollman kdc_fd[fdu].kdc_description = 7306c0081e9SGarrett Wollman "2.88MB (2880K) 3.5in floppy disk drive in 1.44 mode"; 731290dd077SJoerg Wunsch break; 7327ca0641bSAndrey A. Chernov case RTCFDT_360K: 7336a0e6f42SRodney W. Grimes printf("360KB 5.25in\n"); 734b99f0a4aSAndrew Moore fd->type = FD_360; 7356c0081e9SGarrett Wollman kdc_fd[fdu].kdc_description = 7366c0081e9SGarrett Wollman "360KB 5.25in floppy disk drive"; 7377ca0641bSAndrey A. Chernov break; 738ed2fa05eSAndrey A. Chernov case RTCFDT_720K: 7396a0e6f42SRodney W. Grimes printf("720KB 3.5in\n"); 740b99f0a4aSAndrew Moore fd->type = FD_720; 7416c0081e9SGarrett Wollman kdc_fd[fdu].kdc_description = 7426c0081e9SGarrett Wollman "720KB 3.5in floppy disk drive"; 743ed2fa05eSAndrey A. Chernov break; 7447ca0641bSAndrey A. Chernov default: 7456a0e6f42SRodney W. Grimes printf("unknown\n"); 746b99f0a4aSAndrew Moore fd->type = NO_TYPE; 74721519754SBruce Evans dev_detach(&kdc_fd[fdu]); 74821519754SBruce Evans continue; 7495b81b6b3SRodney W. Grimes } 750dc16046fSJoerg Wunsch kdc_fd[fdu].kdc_state = DC_IDLE; 751999422d7SJulian Elischer #ifdef DEVFS 75221519754SBruce Evans mynor = fdu << 6; 75321519754SBruce Evans fd->bdevs[0] = devfs_add_devswf(&fd_bdevsw, mynor, DV_BLK, 754f85120acSBruce Evans UID_ROOT, GID_OPERATOR, 0640, 75521519754SBruce Evans "fd%d", fdu); 75621519754SBruce Evans fd->cdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_CHR, 757f85120acSBruce Evans UID_ROOT, GID_OPERATOR, 0640, 75821519754SBruce Evans "rfd%d", fdu); 75921519754SBruce Evans for (i = 1; i < 1 + NUMDENS; i++) { 76021519754SBruce Evans /* 76121519754SBruce Evans * XXX this and the lookup in Fdopen() should be 76221519754SBruce Evans * data driven. 76321519754SBruce Evans */ 76421519754SBruce Evans switch (fd->type) { 76521519754SBruce Evans case FD_360: 76621519754SBruce Evans if (i != FD_360) 76721519754SBruce Evans continue; 76821519754SBruce Evans break; 76921519754SBruce Evans case FD_720: 77021519754SBruce Evans if (i != FD_720 && i != FD_800 && i != FD_820) 77121519754SBruce Evans continue; 77221519754SBruce Evans break; 77321519754SBruce Evans case FD_1200: 77421519754SBruce Evans if (i != FD_360 && i != FD_720 && i != FD_800 77521519754SBruce Evans && i != FD_820 && i != FD_1200 77621519754SBruce Evans && i != FD_1440 && i != FD_1480) 77721519754SBruce Evans continue; 77821519754SBruce Evans break; 77921519754SBruce Evans case FD_1440: 78021519754SBruce Evans if (i != FD_720 && i != FD_800 && i != FD_820 78121519754SBruce Evans && i != FD_1200 && i != FD_1440 78221519754SBruce Evans && i != FD_1480 && i != FD_1720) 78321519754SBruce Evans continue; 78421519754SBruce Evans break; 78521519754SBruce Evans } 78621519754SBruce Evans typemynor = mynor | i; 78721519754SBruce Evans typesize = fd_types[i - 1].size / 2; 78821519754SBruce Evans /* 78921519754SBruce Evans * XXX all these conversions give bloated code and 79021519754SBruce Evans * confusing names. 79121519754SBruce Evans */ 79221519754SBruce Evans if (typesize == 1476) 79321519754SBruce Evans typesize = 1480; 79421519754SBruce Evans if (typesize == 1722) 79521519754SBruce Evans typesize = 1720; 79621519754SBruce Evans fd->bdevs[i] = 79721519754SBruce Evans devfs_add_devswf(&fd_bdevsw, typemynor, DV_BLK, 79821519754SBruce Evans UID_ROOT, GID_OPERATOR, 0640, 79921519754SBruce Evans "fd%d.%d", fdu, typesize); 80021519754SBruce Evans fd->cdevs[i] = 80121519754SBruce Evans devfs_add_devswf(&fd_cdevsw, typemynor, DV_CHR, 80221519754SBruce Evans UID_ROOT, GID_OPERATOR, 0640, 80321519754SBruce Evans "rfd%d.%d", fdu, typesize); 80421519754SBruce Evans } 80521519754SBruce Evans for (i = 0; i < MAXPARTITIONS; i++) { 80621519754SBruce Evans fd->bdevs[1 + NUMDENS + i] = 80721519754SBruce Evans devfs_link(fd->bdevs[0], 80821519754SBruce Evans "fd%d%c", fdu, 'a' + i); 80921519754SBruce Evans fd->cdevs[1 + NUMDENS + i] = 81021519754SBruce Evans devfs_link(fd->cdevs[0], 81121519754SBruce Evans "rfd%d%c", fdu, 'a' + i); 81221519754SBruce Evans } 813999422d7SJulian Elischer #endif /* DEVFS */ 81492200632SGarrett Wollman if (dk_ndrive < DK_NDRIVE) { 81592200632SGarrett Wollman sprintf(dk_names[dk_ndrive], "fd%d", fdu); 8166a0e6f42SRodney W. Grimes fd->dkunit = dk_ndrive++; 817671e2ceeSBruce Evans /* 818671e2ceeSBruce Evans * XXX assume rate is FDC_500KBPS. 819671e2ceeSBruce Evans */ 820671e2ceeSBruce Evans dk_wpms[dk_ndrive] = 500000 / 8 / 2; 82192200632SGarrett Wollman } else { 82292200632SGarrett Wollman fd->dkunit = -1; 82392200632SGarrett Wollman } 8245b81b6b3SRodney W. Grimes } 8255b81b6b3SRodney W. Grimes 8263a2f7427SDavid Greenman return (1); 8275b81b6b3SRodney W. Grimes } 8285b81b6b3SRodney W. Grimes 8295b81b6b3SRodney W. Grimes /****************************************************************************/ 8305b81b6b3SRodney W. Grimes /* motor control stuff */ 8315e235068SJordan K. Hubbard /* remember to not deselect the drive we're working on */ 8325b81b6b3SRodney W. Grimes /****************************************************************************/ 8333a2f7427SDavid Greenman static void 834dc5df763SJoerg Wunsch set_motor(fdcu_t fdcu, int fdsu, int turnon) 8355b81b6b3SRodney W. Grimes { 8363a2f7427SDavid Greenman int fdout = fdc_data[fdcu].fdout; 8373a2f7427SDavid Greenman int needspecify = 0; 8383a2f7427SDavid Greenman 8393a2f7427SDavid Greenman if(turnon) { 8403a2f7427SDavid Greenman fdout &= ~FDO_FDSEL; 8413a2f7427SDavid Greenman fdout |= (FDO_MOEN0 << fdsu) + fdsu; 8423a2f7427SDavid Greenman } else 8433a2f7427SDavid Greenman fdout &= ~(FDO_MOEN0 << fdsu); 8443a2f7427SDavid Greenman 8455e235068SJordan K. Hubbard if(!turnon 8465e235068SJordan K. Hubbard && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0) 8475e235068SJordan K. Hubbard /* gonna turn off the last drive, put FDC to bed */ 8485e235068SJordan K. Hubbard fdout &= ~ (FDO_FRST|FDO_FDMAEN); 8495e235068SJordan K. Hubbard else { 8503a2f7427SDavid Greenman /* make sure controller is selected and specified */ 8513a2f7427SDavid Greenman if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0) 8523a2f7427SDavid Greenman needspecify = 1; 8533a2f7427SDavid Greenman fdout |= (FDO_FRST|FDO_FDMAEN); 8545b81b6b3SRodney W. Grimes } 8555b81b6b3SRodney W. Grimes 8563a2f7427SDavid Greenman outb(fdc_data[fdcu].baseport+FDOUT, fdout); 8573a2f7427SDavid Greenman fdc_data[fdcu].fdout = fdout; 858dc16046fSJoerg Wunsch kdc_fdc[fdcu].kdc_state = (fdout & FDO_FRST)? DC_BUSY: DC_IDLE; 8593a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdout); 8603a2f7427SDavid Greenman 8613a2f7427SDavid Greenman if(needspecify) { 862dc8603e3SJoerg Wunsch /* 863dc5df763SJoerg Wunsch * XXX 864dc8603e3SJoerg Wunsch * special case: since we have just woken up the FDC 865dc8603e3SJoerg Wunsch * from its sleep, we silently assume the command will 866dc8603e3SJoerg Wunsch * be accepted, and do not test for a timeout 867dc8603e3SJoerg Wunsch */ 868dc5df763SJoerg Wunsch (void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY, 869dc5df763SJoerg Wunsch NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 870dc5df763SJoerg Wunsch 0); 8713a2f7427SDavid Greenman } 8723a2f7427SDavid Greenman } 8733a2f7427SDavid Greenman 874381fe1aaSGarrett Wollman static void 875d0917939SPaul Richards fd_turnoff(void *arg1) 8765b81b6b3SRodney W. Grimes { 877381fe1aaSGarrett Wollman fdu_t fdu = (fdu_t)arg1; 878f5f7ba03SJordan K. Hubbard int s; 8795b81b6b3SRodney W. Grimes fd_p fd = fd_data + fdu; 880dc16046fSJoerg Wunsch 881dc16046fSJoerg Wunsch TRACE1("[fd%d: turnoff]", fdu); 8828335c1b8SBruce Evans 8838335c1b8SBruce Evans /* 8848335c1b8SBruce Evans * Don't turn off the motor yet if the drive is active. 8858335c1b8SBruce Evans * XXX shouldn't even schedule turnoff until drive is inactive 8868335c1b8SBruce Evans * and nothing is queued on it. 8878335c1b8SBruce Evans */ 8888335c1b8SBruce Evans if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fdu) { 8898335c1b8SBruce Evans timeout(fd_turnoff, arg1, 4 * hz); 8908335c1b8SBruce Evans return; 8918335c1b8SBruce Evans } 8928335c1b8SBruce Evans 893f5f7ba03SJordan K. Hubbard s = splbio(); 8945b81b6b3SRodney W. Grimes fd->flags &= ~FD_MOTOR; 8953a2f7427SDavid Greenman set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF); 896f5f7ba03SJordan K. Hubbard splx(s); 8975b81b6b3SRodney W. Grimes } 8985b81b6b3SRodney W. Grimes 8993a2f7427SDavid Greenman static void 900d0917939SPaul Richards fd_motor_on(void *arg1) 9015b81b6b3SRodney W. Grimes { 902381fe1aaSGarrett Wollman fdu_t fdu = (fdu_t)arg1; 903f5f7ba03SJordan K. Hubbard int s; 904f5f7ba03SJordan K. Hubbard 9055b81b6b3SRodney W. Grimes fd_p fd = fd_data + fdu; 906f5f7ba03SJordan K. Hubbard s = splbio(); 9075b81b6b3SRodney W. Grimes fd->flags &= ~FD_MOTOR_WAIT; 9085b81b6b3SRodney W. Grimes if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT)) 9095b81b6b3SRodney W. Grimes { 910f5f7ba03SJordan K. Hubbard fdintr(fd->fdc->fdcu); 9115b81b6b3SRodney W. Grimes } 912f5f7ba03SJordan K. Hubbard splx(s); 9135b81b6b3SRodney W. Grimes } 9145b81b6b3SRodney W. Grimes 9153a2f7427SDavid Greenman static void 916dc5df763SJoerg Wunsch fd_turnon(fdu_t fdu) 9175b81b6b3SRodney W. Grimes { 9185b81b6b3SRodney W. Grimes fd_p fd = fd_data + fdu; 9195b81b6b3SRodney W. Grimes if(!(fd->flags & FD_MOTOR)) 9205b81b6b3SRodney W. Grimes { 9213a2f7427SDavid Greenman fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT); 9223a2f7427SDavid Greenman set_motor(fd->fdc->fdcu, fd->fdsu, TURNON); 9235e235068SJordan K. Hubbard timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */ 9245b81b6b3SRodney W. Grimes } 9255b81b6b3SRodney W. Grimes } 9265b81b6b3SRodney W. Grimes 927381fe1aaSGarrett Wollman static void 928dc5df763SJoerg Wunsch fdc_reset(fdc_p fdc) 9295b81b6b3SRodney W. Grimes { 9303a2f7427SDavid Greenman fdcu_t fdcu = fdc->fdcu; 9313a2f7427SDavid Greenman 9323a2f7427SDavid Greenman /* Try a reset, keep motor on */ 9333a2f7427SDavid Greenman outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 9343a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 9353a2f7427SDavid Greenman DELAY(100); 9363a2f7427SDavid Greenman /* enable FDC, but defer interrupts a moment */ 9373a2f7427SDavid Greenman outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN); 9383a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN); 9393a2f7427SDavid Greenman DELAY(100); 9403a2f7427SDavid Greenman outb(fdc->baseport + FDOUT, fdc->fdout); 9413a2f7427SDavid Greenman TRACE1("[0x%x->FDOUT]", fdc->fdout); 9423a2f7427SDavid Greenman 943dc5df763SJoerg Wunsch /* XXX after a reset, silently believe the FDC will accept commands */ 944dc5df763SJoerg Wunsch (void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY, 945dc5df763SJoerg Wunsch NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 946dc5df763SJoerg Wunsch 0); 9475b81b6b3SRodney W. Grimes } 9485b81b6b3SRodney W. Grimes 9495b81b6b3SRodney W. Grimes /****************************************************************************/ 9505b81b6b3SRodney W. Grimes /* fdc in/out */ 9515b81b6b3SRodney W. Grimes /****************************************************************************/ 9525b81b6b3SRodney W. Grimes int 953dc5df763SJoerg Wunsch in_fdc(fdcu_t fdcu) 9545b81b6b3SRodney W. Grimes { 9555b81b6b3SRodney W. Grimes int baseport = fdc_data[fdcu].baseport; 9565b81b6b3SRodney W. Grimes int i, j = 100000; 9573a2f7427SDavid Greenman while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM)) 9585b81b6b3SRodney W. Grimes != (NE7_DIO|NE7_RQM) && j-- > 0) 959dc5df763SJoerg Wunsch if (i == NE7_RQM) 9606a0e6f42SRodney W. Grimes return fdc_err(fdcu, "ready for output in input\n"); 9615b81b6b3SRodney W. Grimes if (j <= 0) 96216b04b6aSJoerg Wunsch return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0); 9635b81b6b3SRodney W. Grimes #ifdef DEBUG 9643a2f7427SDavid Greenman i = inb(baseport+FDDATA); 9653a2f7427SDavid Greenman TRACE1("[FDDATA->0x%x]", (unsigned char)i); 9665b81b6b3SRodney W. Grimes return(i); 9675b81b6b3SRodney W. Grimes #else 9683a2f7427SDavid Greenman return inb(baseport+FDDATA); 9695b81b6b3SRodney W. Grimes #endif 9705b81b6b3SRodney W. Grimes } 9715b81b6b3SRodney W. Grimes 972dc5df763SJoerg Wunsch /* 973dc5df763SJoerg Wunsch * fd_in: Like in_fdc, but allows you to see if it worked. 974dc5df763SJoerg Wunsch */ 975b5e8ce9fSBruce Evans static int 976dc5df763SJoerg Wunsch fd_in(fdcu_t fdcu, int *ptr) 977dc5df763SJoerg Wunsch { 978dc5df763SJoerg Wunsch int baseport = fdc_data[fdcu].baseport; 979dc5df763SJoerg Wunsch int i, j = 100000; 980dc5df763SJoerg Wunsch while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM)) 981dc5df763SJoerg Wunsch != (NE7_DIO|NE7_RQM) && j-- > 0) 982dc5df763SJoerg Wunsch if (i == NE7_RQM) 9836a0e6f42SRodney W. Grimes return fdc_err(fdcu, "ready for output in input\n"); 984dc5df763SJoerg Wunsch if (j <= 0) 98516b04b6aSJoerg Wunsch return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0); 986dc5df763SJoerg Wunsch #ifdef DEBUG 987dc5df763SJoerg Wunsch i = inb(baseport+FDDATA); 988dc5df763SJoerg Wunsch TRACE1("[FDDATA->0x%x]", (unsigned char)i); 989dc5df763SJoerg Wunsch *ptr = i; 990dc5df763SJoerg Wunsch return 0; 991dc5df763SJoerg Wunsch #else 992dc5df763SJoerg Wunsch i = inb(baseport+FDDATA); 993dc5df763SJoerg Wunsch if (ptr) 994dc5df763SJoerg Wunsch *ptr = i; 995dc5df763SJoerg Wunsch return 0; 996dc5df763SJoerg Wunsch #endif 997dc5df763SJoerg Wunsch } 998dc5df763SJoerg Wunsch 999dc5df763SJoerg Wunsch int 1000dc5df763SJoerg Wunsch out_fdc(fdcu_t fdcu, int x) 10015b81b6b3SRodney W. Grimes { 10025b81b6b3SRodney W. Grimes int baseport = fdc_data[fdcu].baseport; 10033b3837dbSRodney W. Grimes int i; 10045b81b6b3SRodney W. Grimes 10053b3837dbSRodney W. Grimes /* Check that the direction bit is set */ 10063b3837dbSRodney W. Grimes i = 100000; 10073a2f7427SDavid Greenman while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0); 10086a0e6f42SRodney W. Grimes if (i <= 0) return fdc_err(fdcu, "direction bit not set\n"); 10093b3837dbSRodney W. Grimes 10103b3837dbSRodney W. Grimes /* Check that the floppy controller is ready for a command */ 10113b3837dbSRodney W. Grimes i = 100000; 10123a2f7427SDavid Greenman while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0); 101316b04b6aSJoerg Wunsch if (i <= 0) 101416b04b6aSJoerg Wunsch return fdc_err(fdcu, bootverbose? "output ready timeout\n": 0); 10153b3837dbSRodney W. Grimes 10163b3837dbSRodney W. Grimes /* Send the command and return */ 10173a2f7427SDavid Greenman outb(baseport+FDDATA, x); 10183a2f7427SDavid Greenman TRACE1("[0x%x->FDDATA]", x); 10195b81b6b3SRodney W. Grimes return (0); 10205b81b6b3SRodney W. Grimes } 10215b81b6b3SRodney W. Grimes 10225b81b6b3SRodney W. Grimes /****************************************************************************/ 10235b81b6b3SRodney W. Grimes /* fdopen/fdclose */ 10245b81b6b3SRodney W. Grimes /****************************************************************************/ 1025381fe1aaSGarrett Wollman int 1026671e2ceeSBruce Evans Fdopen(dev_t dev, int flags, int mode, struct proc *p) 10275b81b6b3SRodney W. Grimes { 10285b81b6b3SRodney W. Grimes fdu_t fdu = FDUNIT(minor(dev)); 102920a29168SAndrey A. Chernov int type = FDTYPE(minor(dev)); 1030b99f0a4aSAndrew Moore fdc_p fdc; 10315b81b6b3SRodney W. Grimes 1032b99f0a4aSAndrew Moore #if NFT > 0 1033b99f0a4aSAndrew Moore /* check for a tape open */ 1034b99f0a4aSAndrew Moore if (type & F_TAPE_TYPE) 1035b99f0a4aSAndrew Moore return(ftopen(dev, flags)); 1036b99f0a4aSAndrew Moore #endif 10375b81b6b3SRodney W. Grimes /* check bounds */ 1038b99f0a4aSAndrew Moore if (fdu >= NFD) 1039b99f0a4aSAndrew Moore return(ENXIO); 1040b99f0a4aSAndrew Moore fdc = fd_data[fdu].fdc; 1041b99f0a4aSAndrew Moore if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE)) 1042b99f0a4aSAndrew Moore return(ENXIO); 1043b99f0a4aSAndrew Moore if (type > NUMDENS) 1044b99f0a4aSAndrew Moore return(ENXIO); 10457ca0641bSAndrey A. Chernov if (type == 0) 10467ca0641bSAndrey A. Chernov type = fd_data[fdu].type; 10477ca0641bSAndrey A. Chernov else { 10487ca0641bSAndrey A. Chernov if (type != fd_data[fdu].type) { 1049fa4700b4SAndrey A. Chernov switch (fd_data[fdu].type) { 10507ca0641bSAndrey A. Chernov case FD_360: 10517ca0641bSAndrey A. Chernov return(ENXIO); 1052ed2fa05eSAndrey A. Chernov case FD_720: 1053b39c878eSAndrey A. Chernov if ( type != FD_820 1054b39c878eSAndrey A. Chernov && type != FD_800 1055ed2fa05eSAndrey A. Chernov ) 1056ed2fa05eSAndrey A. Chernov return(ENXIO); 1057ed2fa05eSAndrey A. Chernov break; 10587ca0641bSAndrey A. Chernov case FD_1200: 1059b39c878eSAndrey A. Chernov switch (type) { 1060b39c878eSAndrey A. Chernov case FD_1480: 1061b39c878eSAndrey A. Chernov type = FD_1480in5_25; 1062fa4700b4SAndrey A. Chernov break; 10637ca0641bSAndrey A. Chernov case FD_1440: 1064b39c878eSAndrey A. Chernov type = FD_1440in5_25; 1065b39c878eSAndrey A. Chernov break; 1066b39c878eSAndrey A. Chernov case FD_820: 1067b39c878eSAndrey A. Chernov type = FD_820in5_25; 1068b39c878eSAndrey A. Chernov break; 1069b39c878eSAndrey A. Chernov case FD_800: 1070b39c878eSAndrey A. Chernov type = FD_800in5_25; 1071b39c878eSAndrey A. Chernov break; 1072b39c878eSAndrey A. Chernov case FD_720: 1073b39c878eSAndrey A. Chernov type = FD_720in5_25; 1074b39c878eSAndrey A. Chernov break; 1075b39c878eSAndrey A. Chernov case FD_360: 1076b39c878eSAndrey A. Chernov type = FD_360in5_25; 1077b39c878eSAndrey A. Chernov break; 1078b39c878eSAndrey A. Chernov default: 1079b39c878eSAndrey A. Chernov return(ENXIO); 1080b39c878eSAndrey A. Chernov } 1081b39c878eSAndrey A. Chernov break; 1082b39c878eSAndrey A. Chernov case FD_1440: 1083b39c878eSAndrey A. Chernov if ( type != FD_1720 1084b39c878eSAndrey A. Chernov && type != FD_1480 1085ed2fa05eSAndrey A. Chernov && type != FD_1200 1086b39c878eSAndrey A. Chernov && type != FD_820 1087b39c878eSAndrey A. Chernov && type != FD_800 1088b39c878eSAndrey A. Chernov && type != FD_720 10897ca0641bSAndrey A. Chernov ) 1090dffff499SAndrey A. Chernov return(ENXIO); 1091fa4700b4SAndrey A. Chernov break; 10927ca0641bSAndrey A. Chernov } 10937ca0641bSAndrey A. Chernov } 1094fa4700b4SAndrey A. Chernov } 1095b99f0a4aSAndrew Moore fd_data[fdu].ft = fd_types + type - 1; 10965b81b6b3SRodney W. Grimes fd_data[fdu].flags |= FD_OPEN; 1097dc16046fSJoerg Wunsch kdc_fd[fdu].kdc_state = DC_BUSY; 10985b81b6b3SRodney W. Grimes 10995b81b6b3SRodney W. Grimes return 0; 11005b81b6b3SRodney W. Grimes } 11015b81b6b3SRodney W. Grimes 1102381fe1aaSGarrett Wollman int 1103671e2ceeSBruce Evans fdclose(dev_t dev, int flags, int mode, struct proc *p) 11045b81b6b3SRodney W. Grimes { 11055b81b6b3SRodney W. Grimes fdu_t fdu = FDUNIT(minor(dev)); 1106b99f0a4aSAndrew Moore 1107b99f0a4aSAndrew Moore #if NFT > 0 11083a2f7427SDavid Greenman int type = FDTYPE(minor(dev)); 11093a2f7427SDavid Greenman 1110b99f0a4aSAndrew Moore if (type & F_TAPE_TYPE) 11113a2f7427SDavid Greenman return ftclose(dev, flags); 1112b99f0a4aSAndrew Moore #endif 11135b81b6b3SRodney W. Grimes fd_data[fdu].flags &= ~FD_OPEN; 11143a2f7427SDavid Greenman fd_data[fdu].options &= ~FDOPT_NORETRY; 1115dc16046fSJoerg Wunsch kdc_fd[fdu].kdc_state = DC_IDLE; 1116dc16046fSJoerg Wunsch 11175b81b6b3SRodney W. Grimes return(0); 11185b81b6b3SRodney W. Grimes } 11195b81b6b3SRodney W. Grimes 11205b81b6b3SRodney W. Grimes 11213a2f7427SDavid Greenman /****************************************************************************/ 11223a2f7427SDavid Greenman /* fdstrategy */ 11233a2f7427SDavid Greenman /****************************************************************************/ 11243a2f7427SDavid Greenman void 11253a2f7427SDavid Greenman fdstrategy(struct buf *bp) 11263a2f7427SDavid Greenman { 11273a2f7427SDavid Greenman long nblocks, blknum; 11283a2f7427SDavid Greenman int s; 11293a2f7427SDavid Greenman fdcu_t fdcu; 11303a2f7427SDavid Greenman fdu_t fdu; 11313a2f7427SDavid Greenman fdc_p fdc; 11323a2f7427SDavid Greenman fd_p fd; 11333a2f7427SDavid Greenman size_t fdblk; 11343a2f7427SDavid Greenman 11353a2f7427SDavid Greenman fdu = FDUNIT(minor(bp->b_dev)); 11363a2f7427SDavid Greenman fd = &fd_data[fdu]; 11373a2f7427SDavid Greenman fdc = fd->fdc; 11383a2f7427SDavid Greenman fdcu = fdc->fdcu; 11393a2f7427SDavid Greenman 11403a2f7427SDavid Greenman #if NFT > 0 11413a2f7427SDavid Greenman if (FDTYPE(minor(bp->b_dev)) & F_TAPE_TYPE) { 11423a2f7427SDavid Greenman /* ft tapes do not (yet) support strategy i/o */ 1143d3628763SRodney W. Grimes bp->b_error = ENODEV; 11443a2f7427SDavid Greenman bp->b_flags |= B_ERROR; 11453a2f7427SDavid Greenman goto bad; 11463a2f7427SDavid Greenman } 11473a2f7427SDavid Greenman /* check for controller already busy with tape */ 11483a2f7427SDavid Greenman if (fdc->flags & FDC_TAPE_BUSY) { 11493a2f7427SDavid Greenman bp->b_error = EBUSY; 11503a2f7427SDavid Greenman bp->b_flags |= B_ERROR; 11513a2f7427SDavid Greenman goto bad; 11523a2f7427SDavid Greenman } 11533a2f7427SDavid Greenman #endif 1154d3628763SRodney W. Grimes fdblk = 128 << (fd->ft->secsize); 11553a2f7427SDavid Greenman if (!(bp->b_flags & B_FORMAT)) { 11563a2f7427SDavid Greenman if ((fdu >= NFD) || (bp->b_blkno < 0)) { 1157dc5df763SJoerg Wunsch printf( 11586a0e6f42SRodney W. Grimes "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n", 1159702c623aSPoul-Henning Kamp fdu, (u_long)bp->b_blkno, bp->b_bcount); 11603a2f7427SDavid Greenman bp->b_error = EINVAL; 11613a2f7427SDavid Greenman bp->b_flags |= B_ERROR; 11623a2f7427SDavid Greenman goto bad; 11633a2f7427SDavid Greenman } 11643a2f7427SDavid Greenman if ((bp->b_bcount % fdblk) != 0) { 11653a2f7427SDavid Greenman bp->b_error = EINVAL; 11663a2f7427SDavid Greenman bp->b_flags |= B_ERROR; 11673a2f7427SDavid Greenman goto bad; 11683a2f7427SDavid Greenman } 11693a2f7427SDavid Greenman } 11703a2f7427SDavid Greenman 11713a2f7427SDavid Greenman /* 11723a2f7427SDavid Greenman * Set up block calculations. 11733a2f7427SDavid Greenman */ 11743a2f7427SDavid Greenman blknum = (unsigned long) bp->b_blkno * DEV_BSIZE/fdblk; 11753a2f7427SDavid Greenman nblocks = fd->ft->size; 11763a2f7427SDavid Greenman if (blknum + (bp->b_bcount / fdblk) > nblocks) { 11773a2f7427SDavid Greenman if (blknum == nblocks) { 11783a2f7427SDavid Greenman bp->b_resid = bp->b_bcount; 11793a2f7427SDavid Greenman } else { 11803a2f7427SDavid Greenman bp->b_error = ENOSPC; 11813a2f7427SDavid Greenman bp->b_flags |= B_ERROR; 11823a2f7427SDavid Greenman } 11833a2f7427SDavid Greenman goto bad; 11843a2f7427SDavid Greenman } 11853a2f7427SDavid Greenman bp->b_cylin = blknum / (fd->ft->sectrac * fd->ft->heads); 118627513ca7SBruce Evans bp->b_pblkno = bp->b_blkno; 11873a2f7427SDavid Greenman s = splbio(); 118817542807SPoul-Henning Kamp tqdisksort(&fdc->head, bp); 11895e235068SJordan K. Hubbard untimeout(fd_turnoff, (caddr_t)fdu); /* a good idea */ 11903a2f7427SDavid Greenman fdstart(fdcu); 11913a2f7427SDavid Greenman splx(s); 11923a2f7427SDavid Greenman return; 11933a2f7427SDavid Greenman 11943a2f7427SDavid Greenman bad: 11953a2f7427SDavid Greenman biodone(bp); 11963a2f7427SDavid Greenman } 11973a2f7427SDavid Greenman 11985b81b6b3SRodney W. Grimes /***************************************************************\ 11995b81b6b3SRodney W. Grimes * fdstart * 12005b81b6b3SRodney W. Grimes * We have just queued something.. if the controller is not busy * 12015b81b6b3SRodney W. Grimes * then simulate the case where it has just finished a command * 12025b81b6b3SRodney W. Grimes * So that it (the interrupt routine) looks on the queue for more* 12035b81b6b3SRodney W. Grimes * work to do and picks up what we just added. * 12045b81b6b3SRodney W. Grimes * If the controller is already busy, we need do nothing, as it * 12055b81b6b3SRodney W. Grimes * will pick up our work when the present work completes * 12065b81b6b3SRodney W. Grimes \***************************************************************/ 1207381fe1aaSGarrett Wollman static void 1208dc5df763SJoerg Wunsch fdstart(fdcu_t fdcu) 12095b81b6b3SRodney W. Grimes { 12105b81b6b3SRodney W. Grimes int s; 12115b81b6b3SRodney W. Grimes 12125b81b6b3SRodney W. Grimes s = splbio(); 12135b81b6b3SRodney W. Grimes if(fdc_data[fdcu].state == DEVIDLE) 12145b81b6b3SRodney W. Grimes { 12155b81b6b3SRodney W. Grimes fdintr(fdcu); 12165b81b6b3SRodney W. Grimes } 12175b81b6b3SRodney W. Grimes splx(s); 12185b81b6b3SRodney W. Grimes } 12195b81b6b3SRodney W. Grimes 1220381fe1aaSGarrett Wollman static void 1221d0917939SPaul Richards fd_timeout(void *arg1) 12225b81b6b3SRodney W. Grimes { 1223381fe1aaSGarrett Wollman fdcu_t fdcu = (fdcu_t)arg1; 12245b81b6b3SRodney W. Grimes fdu_t fdu = fdc_data[fdcu].fdu; 12253a2f7427SDavid Greenman int baseport = fdc_data[fdcu].baseport; 122617542807SPoul-Henning Kamp struct buf *bp; 1227f5f7ba03SJordan K. Hubbard int s; 12285b81b6b3SRodney W. Grimes 122917542807SPoul-Henning Kamp bp = TAILQ_FIRST(&fdc_data[fdcu].head); 12305b81b6b3SRodney W. Grimes 12313a2f7427SDavid Greenman /* 12323a2f7427SDavid Greenman * Due to IBM's brain-dead design, the FDC has a faked ready 12333a2f7427SDavid Greenman * signal, hardwired to ready == true. Thus, any command 12343a2f7427SDavid Greenman * issued if there's no diskette in the drive will _never_ 12353a2f7427SDavid Greenman * complete, and must be aborted by resetting the FDC. 12363a2f7427SDavid Greenman * Many thanks, Big Blue! 12373a2f7427SDavid Greenman */ 12385b81b6b3SRodney W. Grimes 12393a2f7427SDavid Greenman s = splbio(); 12403a2f7427SDavid Greenman 12413a2f7427SDavid Greenman TRACE1("fd%d[fd_timeout()]", fdu); 12423a2f7427SDavid Greenman /* See if the controller is still busy (patiently awaiting data) */ 12433a2f7427SDavid Greenman if(((inb(baseport + FDSTS)) & (NE7_CB|NE7_RQM)) == NE7_CB) 12443a2f7427SDavid Greenman { 12453a2f7427SDavid Greenman TRACE1("[FDSTS->0x%x]", inb(baseport + FDSTS)); 12463a2f7427SDavid Greenman /* yup, it is; kill it now */ 12473a2f7427SDavid Greenman fdc_reset(&fdc_data[fdcu]); 12483a2f7427SDavid Greenman printf("fd%d: Operation timeout\n", fdu); 12493a2f7427SDavid Greenman } 12505b81b6b3SRodney W. Grimes 12515b81b6b3SRodney W. Grimes if (bp) 12525b81b6b3SRodney W. Grimes { 12535b81b6b3SRodney W. Grimes retrier(fdcu); 12543a2f7427SDavid Greenman fdc_data[fdcu].status[0] = NE7_ST0_IC_RC; 12555b81b6b3SRodney W. Grimes fdc_data[fdcu].state = IOTIMEDOUT; 12565b81b6b3SRodney W. Grimes if( fdc_data[fdcu].retry < 6) 12575b81b6b3SRodney W. Grimes fdc_data[fdcu].retry = 6; 12585b81b6b3SRodney W. Grimes } 12595b81b6b3SRodney W. Grimes else 12605b81b6b3SRodney W. Grimes { 12615b81b6b3SRodney W. Grimes fdc_data[fdcu].fd = (fd_p) 0; 12625b81b6b3SRodney W. Grimes fdc_data[fdcu].fdu = -1; 12635b81b6b3SRodney W. Grimes fdc_data[fdcu].state = DEVIDLE; 12645b81b6b3SRodney W. Grimes } 1265f5f7ba03SJordan K. Hubbard fdintr(fdcu); 1266f5f7ba03SJordan K. Hubbard splx(s); 12675b81b6b3SRodney W. Grimes } 12685b81b6b3SRodney W. Grimes 12695b81b6b3SRodney W. Grimes /* just ensure it has the right spl */ 1270381fe1aaSGarrett Wollman static void 1271d0917939SPaul Richards fd_pseudointr(void *arg1) 12725b81b6b3SRodney W. Grimes { 1273381fe1aaSGarrett Wollman fdcu_t fdcu = (fdcu_t)arg1; 12745b81b6b3SRodney W. Grimes int s; 12753a2f7427SDavid Greenman 12765b81b6b3SRodney W. Grimes s = splbio(); 12775b81b6b3SRodney W. Grimes fdintr(fdcu); 12785b81b6b3SRodney W. Grimes splx(s); 12795b81b6b3SRodney W. Grimes } 12805b81b6b3SRodney W. Grimes 12815b81b6b3SRodney W. Grimes /***********************************************************************\ 12825b81b6b3SRodney W. Grimes * fdintr * 12835b81b6b3SRodney W. Grimes * keep calling the state machine until it returns a 0 * 12845b81b6b3SRodney W. Grimes * ALWAYS called at SPLBIO * 12855b81b6b3SRodney W. Grimes \***********************************************************************/ 1286381fe1aaSGarrett Wollman void 1287381fe1aaSGarrett Wollman fdintr(fdcu_t fdcu) 12885b81b6b3SRodney W. Grimes { 12895b81b6b3SRodney W. Grimes fdc_p fdc = fdc_data + fdcu; 1290b99f0a4aSAndrew Moore #if NFT > 0 1291b99f0a4aSAndrew Moore fdu_t fdu = fdc->fdu; 1292b99f0a4aSAndrew Moore 1293b99f0a4aSAndrew Moore if (fdc->flags & FDC_TAPE_BUSY) 1294b99f0a4aSAndrew Moore (ftintr(fdu)); 1295b99f0a4aSAndrew Moore else 1296b99f0a4aSAndrew Moore #endif 1297381fe1aaSGarrett Wollman while(fdstate(fdcu, fdc)) 1298381fe1aaSGarrett Wollman ; 12995b81b6b3SRodney W. Grimes } 13005b81b6b3SRodney W. Grimes 13015b81b6b3SRodney W. Grimes /***********************************************************************\ 13025b81b6b3SRodney W. Grimes * The controller state machine. * 13035b81b6b3SRodney W. Grimes * if it returns a non zero value, it should be called again immediatly * 13045b81b6b3SRodney W. Grimes \***********************************************************************/ 13053a2f7427SDavid Greenman static int 1306dc5df763SJoerg Wunsch fdstate(fdcu_t fdcu, fdc_p fdc) 13075b81b6b3SRodney W. Grimes { 13084ccc87c5SPoul-Henning Kamp int read, format, head, sec = 0, sectrac, st0, cyl, st3; 13095b81b6b3SRodney W. Grimes unsigned long blknum; 13105b81b6b3SRodney W. Grimes fdu_t fdu = fdc->fdu; 13115b81b6b3SRodney W. Grimes fd_p fd; 131217542807SPoul-Henning Kamp register struct buf *bp; 1313b39c878eSAndrey A. Chernov struct fd_formb *finfo = NULL; 13143a2f7427SDavid Greenman size_t fdblk; 13155b81b6b3SRodney W. Grimes 131614212c9dSPoul-Henning Kamp bp = TAILQ_FIRST(&fdc->head); 131717542807SPoul-Henning Kamp if(!bp) { 13185b81b6b3SRodney W. Grimes /***********************************************\ 13195b81b6b3SRodney W. Grimes * nothing left for this controller to do * 13205b81b6b3SRodney W. Grimes * Force into the IDLE state, * 13215b81b6b3SRodney W. Grimes \***********************************************/ 13225b81b6b3SRodney W. Grimes fdc->state = DEVIDLE; 13235b81b6b3SRodney W. Grimes if(fdc->fd) 13245b81b6b3SRodney W. Grimes { 13256a0e6f42SRodney W. Grimes printf("fd%d: unexpected valid fd pointer\n", 13263a2f7427SDavid Greenman fdc->fdu); 13275b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 13285b81b6b3SRodney W. Grimes fdc->fdu = -1; 13295b81b6b3SRodney W. Grimes } 13305b81b6b3SRodney W. Grimes TRACE1("[fdc%d IDLE]", fdcu); 13315b81b6b3SRodney W. Grimes return(0); 13325b81b6b3SRodney W. Grimes } 13335b81b6b3SRodney W. Grimes fdu = FDUNIT(minor(bp->b_dev)); 13345b81b6b3SRodney W. Grimes fd = fd_data + fdu; 13353a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 13365b81b6b3SRodney W. Grimes if (fdc->fd && (fd != fdc->fd)) 13375b81b6b3SRodney W. Grimes { 13386a0e6f42SRodney W. Grimes printf("fd%d: confused fd pointers\n", fdu); 13395b81b6b3SRodney W. Grimes } 13405b81b6b3SRodney W. Grimes read = bp->b_flags & B_READ; 1341b39c878eSAndrey A. Chernov format = bp->b_flags & B_FORMAT; 1342b39c878eSAndrey A. Chernov if(format) 1343b39c878eSAndrey A. Chernov finfo = (struct fd_formb *)bp->b_un.b_addr; 13445b81b6b3SRodney W. Grimes TRACE1("fd%d", fdu); 13455b81b6b3SRodney W. Grimes TRACE1("[%s]", fdstates[fdc->state]); 13465b81b6b3SRodney W. Grimes TRACE1("(0x%x)", fd->flags); 13475e235068SJordan K. Hubbard untimeout(fd_turnoff, (caddr_t)fdu); 13485e235068SJordan K. Hubbard timeout(fd_turnoff, (caddr_t)fdu, 4 * hz); 13495b81b6b3SRodney W. Grimes switch (fdc->state) 13505b81b6b3SRodney W. Grimes { 13515b81b6b3SRodney W. Grimes case DEVIDLE: 13525b81b6b3SRodney W. Grimes case FINDWORK: /* we have found new work */ 13535b81b6b3SRodney W. Grimes fdc->retry = 0; 13545b81b6b3SRodney W. Grimes fd->skip = 0; 13555b81b6b3SRodney W. Grimes fdc->fd = fd; 13565b81b6b3SRodney W. Grimes fdc->fdu = fdu; 13573a2f7427SDavid Greenman outb(fdc->baseport+FDCTL, fd->ft->trans); 13583a2f7427SDavid Greenman TRACE1("[0x%x->FDCTL]", fd->ft->trans); 13595b81b6b3SRodney W. Grimes /*******************************************************\ 13605b81b6b3SRodney W. Grimes * If the next drive has a motor startup pending, then * 13615b81b6b3SRodney W. Grimes * it will start up in it's own good time * 13625b81b6b3SRodney W. Grimes \*******************************************************/ 13635b81b6b3SRodney W. Grimes if(fd->flags & FD_MOTOR_WAIT) 13645b81b6b3SRodney W. Grimes { 13655b81b6b3SRodney W. Grimes fdc->state = MOTORWAIT; 13665b81b6b3SRodney W. Grimes return(0); /* come back later */ 13675b81b6b3SRodney W. Grimes } 13685b81b6b3SRodney W. Grimes /*******************************************************\ 13695b81b6b3SRodney W. Grimes * Maybe if it's not starting, it SHOULD be starting * 13705b81b6b3SRodney W. Grimes \*******************************************************/ 13715b81b6b3SRodney W. Grimes if (!(fd->flags & FD_MOTOR)) 13725b81b6b3SRodney W. Grimes { 13735b81b6b3SRodney W. Grimes fdc->state = MOTORWAIT; 13745b81b6b3SRodney W. Grimes fd_turnon(fdu); 13755b81b6b3SRodney W. Grimes return(0); 13765b81b6b3SRodney W. Grimes } 13775b81b6b3SRodney W. Grimes else /* at least make sure we are selected */ 13785b81b6b3SRodney W. Grimes { 13793a2f7427SDavid Greenman set_motor(fdcu, fd->fdsu, TURNON); 13805b81b6b3SRodney W. Grimes } 13815e235068SJordan K. Hubbard fdc->state = DOSEEK; 13825b81b6b3SRodney W. Grimes break; 13835b81b6b3SRodney W. Grimes case DOSEEK: 13845b81b6b3SRodney W. Grimes if (bp->b_cylin == fd->track) 13855b81b6b3SRodney W. Grimes { 13865b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 13875b81b6b3SRodney W. Grimes break; 13885b81b6b3SRodney W. Grimes } 1389dc5df763SJoerg Wunsch if (fd_cmd(fdcu, 3, NE7CMD_SEEK, 1390dc5df763SJoerg Wunsch fd->fdsu, bp->b_cylin * fd->ft->steptrac, 1391dc5df763SJoerg Wunsch 0)) 1392dc8603e3SJoerg Wunsch { 1393dc8603e3SJoerg Wunsch /* 1394dc8603e3SJoerg Wunsch * seek command not accepted, looks like 1395dc8603e3SJoerg Wunsch * the FDC went off to the Saints... 1396dc8603e3SJoerg Wunsch */ 1397dc8603e3SJoerg Wunsch fdc->retry = 6; /* try a reset */ 1398dc8603e3SJoerg Wunsch return(retrier(fdcu)); 1399dc8603e3SJoerg Wunsch } 1400dc5df763SJoerg Wunsch fd->track = FD_NO_TRACK; 14015b81b6b3SRodney W. Grimes fdc->state = SEEKWAIT; 14025b81b6b3SRodney W. Grimes return(0); /* will return later */ 14035b81b6b3SRodney W. Grimes case SEEKWAIT: 14045b81b6b3SRodney W. Grimes /* allow heads to settle */ 140504b734cfSPoul-Henning Kamp timeout(fd_pseudointr, (caddr_t)fdcu, hz / 16); 14065b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 14075b81b6b3SRodney W. Grimes return(0); /* will return later */ 14085b81b6b3SRodney W. Grimes case SEEKCOMPLETE : /* SEEK DONE, START DMA */ 14095b81b6b3SRodney W. Grimes /* Make sure seek really happened*/ 1410dc5df763SJoerg Wunsch if(fd->track == FD_NO_TRACK) 14115b81b6b3SRodney W. Grimes { 14125b81b6b3SRodney W. Grimes int descyl = bp->b_cylin * fd->ft->steptrac; 14133a2f7427SDavid Greenman do { 14143a2f7427SDavid Greenman /* 1415dc5df763SJoerg Wunsch * This might be a "ready changed" interrupt, 1416dc5df763SJoerg Wunsch * which cannot really happen since the 1417dc5df763SJoerg Wunsch * RDY pin is hardwired to + 5 volts. This 1418dc5df763SJoerg Wunsch * generally indicates a "bouncing" intr 1419dc5df763SJoerg Wunsch * line, so do one of the following: 1420dc5df763SJoerg Wunsch * 1421dc5df763SJoerg Wunsch * When running on an enhanced FDC that is 1422dc5df763SJoerg Wunsch * known to not go stuck after responding 1423dc5df763SJoerg Wunsch * with INVALID, fetch all interrupt states 1424dc5df763SJoerg Wunsch * until seeing either an INVALID or a 1425dc5df763SJoerg Wunsch * real interrupt condition. 1426dc5df763SJoerg Wunsch * 1427dc5df763SJoerg Wunsch * When running on a dumb old NE765, give 1428dc5df763SJoerg Wunsch * up immediately. The controller will 1429dc5df763SJoerg Wunsch * provide up to four dummy RC interrupt 1430dc5df763SJoerg Wunsch * conditions right after reset (for the 1431dc5df763SJoerg Wunsch * corresponding four drives), so this is 1432dc5df763SJoerg Wunsch * our only chance to get notice that it 1433dc5df763SJoerg Wunsch * was not the FDC that caused the interrupt. 14343a2f7427SDavid Greenman */ 1435dc5df763SJoerg Wunsch if (fd_sense_int(fdc, &st0, &cyl) 1436dc5df763SJoerg Wunsch == FD_NOT_VALID) 1437dc5df763SJoerg Wunsch return 0; 1438dc5df763SJoerg Wunsch if(fdc->fdct == FDC_NE765 1439dc5df763SJoerg Wunsch && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 1440dc5df763SJoerg Wunsch return 0; /* hope for a real intr */ 14413a2f7427SDavid Greenman } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 1442dc5df763SJoerg Wunsch 14433a2f7427SDavid Greenman if (0 == descyl) 14443a2f7427SDavid Greenman { 1445dc5df763SJoerg Wunsch int failed = 0; 14463a2f7427SDavid Greenman /* 14473a2f7427SDavid Greenman * seek to cyl 0 requested; make sure we are 14483a2f7427SDavid Greenman * really there 14493a2f7427SDavid Greenman */ 1450dc5df763SJoerg Wunsch if (fd_sense_drive_status(fdc, &st3)) 1451dc5df763SJoerg Wunsch failed = 1; 14523a2f7427SDavid Greenman if ((st3 & NE7_ST3_T0) == 0) { 14533a2f7427SDavid Greenman printf( 14543a2f7427SDavid Greenman "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n", 14553a2f7427SDavid Greenman fdu, st3, NE7_ST3BITS); 1456dc5df763SJoerg Wunsch failed = 1; 1457dc5df763SJoerg Wunsch } 1458dc5df763SJoerg Wunsch 1459dc5df763SJoerg Wunsch if (failed) 1460dc5df763SJoerg Wunsch { 14613a2f7427SDavid Greenman if(fdc->retry < 3) 14623a2f7427SDavid Greenman fdc->retry = 3; 14633a2f7427SDavid Greenman return(retrier(fdcu)); 14643a2f7427SDavid Greenman } 14653a2f7427SDavid Greenman } 1466dc5df763SJoerg Wunsch 14675b81b6b3SRodney W. Grimes if (cyl != descyl) 14685b81b6b3SRodney W. Grimes { 14693a2f7427SDavid Greenman printf( 14703a2f7427SDavid Greenman "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n", 14712d9d0204SRodney W. Grimes fdu, descyl, cyl, st0); 14725b81b6b3SRodney W. Grimes return(retrier(fdcu)); 14735b81b6b3SRodney W. Grimes } 14745b81b6b3SRodney W. Grimes } 14755b81b6b3SRodney W. Grimes 14765b81b6b3SRodney W. Grimes fd->track = bp->b_cylin; 1477b39c878eSAndrey A. Chernov if(format) 1478b39c878eSAndrey A. Chernov fd->skip = (char *)&(finfo->fd_formb_cylno(0)) 1479b39c878eSAndrey A. Chernov - (char *)finfo; 14805b81b6b3SRodney W. Grimes isa_dmastart(bp->b_flags, bp->b_un.b_addr+fd->skip, 14813a2f7427SDavid Greenman format ? bp->b_bcount : fdblk, fdc->dmachan); 14823a2f7427SDavid Greenman blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk 14833a2f7427SDavid Greenman + fd->skip/fdblk; 14845b81b6b3SRodney W. Grimes sectrac = fd->ft->sectrac; 14855b81b6b3SRodney W. Grimes sec = blknum % (sectrac * fd->ft->heads); 14865b81b6b3SRodney W. Grimes head = sec / sectrac; 14875b81b6b3SRodney W. Grimes sec = sec % sectrac + 1; 14883a2f7427SDavid Greenman fd->hddrv = ((head&1)<<2)+fdu; 14893a2f7427SDavid Greenman 14903a2f7427SDavid Greenman if(format || !read) 14913a2f7427SDavid Greenman { 14923a2f7427SDavid Greenman /* make sure the drive is writable */ 1493dc5df763SJoerg Wunsch if(fd_sense_drive_status(fdc, &st3) != 0) 1494dc8603e3SJoerg Wunsch { 1495dc8603e3SJoerg Wunsch /* stuck controller? */ 1496dc8603e3SJoerg Wunsch fdc->retry = 6; /* reset the beast */ 1497dc8603e3SJoerg Wunsch return(retrier(fdcu)); 1498dc8603e3SJoerg Wunsch } 14993a2f7427SDavid Greenman if(st3 & NE7_ST3_WP) 15003a2f7427SDavid Greenman { 15013a2f7427SDavid Greenman /* 15023a2f7427SDavid Greenman * XXX YES! this is ugly. 15033a2f7427SDavid Greenman * in order to force the current operation 15043a2f7427SDavid Greenman * to fail, we will have to fake an FDC 15053a2f7427SDavid Greenman * error - all error handling is done 15063a2f7427SDavid Greenman * by the retrier() 15073a2f7427SDavid Greenman */ 15083a2f7427SDavid Greenman fdc->status[0] = NE7_ST0_IC_AT; 15093a2f7427SDavid Greenman fdc->status[1] = NE7_ST1_NW; 15103a2f7427SDavid Greenman fdc->status[2] = 0; 15113a2f7427SDavid Greenman fdc->status[3] = fd->track; 15123a2f7427SDavid Greenman fdc->status[4] = head; 15133a2f7427SDavid Greenman fdc->status[5] = sec; 15143a2f7427SDavid Greenman fdc->retry = 8; /* break out immediately */ 15153a2f7427SDavid Greenman fdc->state = IOTIMEDOUT; /* not really... */ 15163a2f7427SDavid Greenman return (1); 15173a2f7427SDavid Greenman } 15183a2f7427SDavid Greenman } 15195b81b6b3SRodney W. Grimes 1520b39c878eSAndrey A. Chernov if(format) 1521b39c878eSAndrey A. Chernov { 1522b39c878eSAndrey A. Chernov /* formatting */ 1523dc5df763SJoerg Wunsch if(fd_cmd(fdcu, 6, 1524dc5df763SJoerg Wunsch NE7CMD_FORMAT, 1525dc5df763SJoerg Wunsch head << 2 | fdu, 1526dc5df763SJoerg Wunsch finfo->fd_formb_secshift, 1527dc5df763SJoerg Wunsch finfo->fd_formb_nsecs, 1528dc5df763SJoerg Wunsch finfo->fd_formb_gaplen, 1529dc5df763SJoerg Wunsch finfo->fd_formb_fillbyte, 1530dc5df763SJoerg Wunsch 0)) 1531dc8603e3SJoerg Wunsch { 1532dc8603e3SJoerg Wunsch /* controller fell over */ 1533dc8603e3SJoerg Wunsch fdc->retry = 6; 1534dc8603e3SJoerg Wunsch return(retrier(fdcu)); 1535dc8603e3SJoerg Wunsch } 1536b39c878eSAndrey A. Chernov } 1537b39c878eSAndrey A. Chernov else 1538b39c878eSAndrey A. Chernov { 1539dc5df763SJoerg Wunsch if (fd_cmd(fdcu, 9, 1540dc5df763SJoerg Wunsch (read ? NE7CMD_READ : NE7CMD_WRITE), 1541dc5df763SJoerg Wunsch head << 2 | fdu, /* head & unit */ 1542dc5df763SJoerg Wunsch fd->track, /* track */ 1543dc5df763SJoerg Wunsch head, 1544dc5df763SJoerg Wunsch sec, /* sector + 1 */ 1545dc5df763SJoerg Wunsch fd->ft->secsize, /* sector size */ 1546dc5df763SJoerg Wunsch sectrac, /* sectors/track */ 1547dc5df763SJoerg Wunsch fd->ft->gap, /* gap size */ 1548dc5df763SJoerg Wunsch fd->ft->datalen, /* data length */ 1549dc5df763SJoerg Wunsch 0)) 15505b81b6b3SRodney W. Grimes { 1551dc8603e3SJoerg Wunsch /* the beast is sleeping again */ 1552dc8603e3SJoerg Wunsch fdc->retry = 6; 1553dc8603e3SJoerg Wunsch return(retrier(fdcu)); 15545b81b6b3SRodney W. Grimes } 1555b39c878eSAndrey A. Chernov } 15565b81b6b3SRodney W. Grimes fdc->state = IOCOMPLETE; 15575e235068SJordan K. Hubbard timeout(fd_timeout, (caddr_t)fdcu, hz); 15585b81b6b3SRodney W. Grimes return(0); /* will return later */ 15595b81b6b3SRodney W. Grimes case IOCOMPLETE: /* IO DONE, post-analyze */ 15605e235068SJordan K. Hubbard untimeout(fd_timeout, (caddr_t)fdcu); 1561dc5df763SJoerg Wunsch 1562dc5df763SJoerg Wunsch if (fd_read_status(fdc, fd->fdsu)) 15635b81b6b3SRodney W. Grimes { 1564dc5df763SJoerg Wunsch if (fdc->retry < 6) 1565dc5df763SJoerg Wunsch fdc->retry = 6; /* force a reset */ 1566dc5df763SJoerg Wunsch return retrier(fdcu); 15675b81b6b3SRodney W. Grimes } 1568dc5df763SJoerg Wunsch 15693a2f7427SDavid Greenman fdc->state = IOTIMEDOUT; 1570dc5df763SJoerg Wunsch 15713a2f7427SDavid Greenman /* FALLTHROUGH */ 1572dc5df763SJoerg Wunsch 15733a2f7427SDavid Greenman case IOTIMEDOUT: 15745b81b6b3SRodney W. Grimes isa_dmadone(bp->b_flags, bp->b_un.b_addr+fd->skip, 15753a2f7427SDavid Greenman format ? bp->b_bcount : fdblk, fdc->dmachan); 15763a2f7427SDavid Greenman if (fdc->status[0] & NE7_ST0_IC) 15775b81b6b3SRodney W. Grimes { 15783a2f7427SDavid Greenman if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 15793a2f7427SDavid Greenman && fdc->status[1] & NE7_ST1_OR) { 1580b39c878eSAndrey A. Chernov /* 15813a2f7427SDavid Greenman * DMA overrun. Someone hogged the bus 15823a2f7427SDavid Greenman * and didn't release it in time for the 15833a2f7427SDavid Greenman * next FDC transfer. 15843a2f7427SDavid Greenman * Just restart it, don't increment retry 15853a2f7427SDavid Greenman * count. (vak) 1586b39c878eSAndrey A. Chernov */ 1587b39c878eSAndrey A. Chernov fdc->state = SEEKCOMPLETE; 1588b39c878eSAndrey A. Chernov return (1); 1589b39c878eSAndrey A. Chernov } 15903a2f7427SDavid Greenman else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV 15913a2f7427SDavid Greenman && fdc->retry < 6) 15923a2f7427SDavid Greenman fdc->retry = 6; /* force a reset */ 15933a2f7427SDavid Greenman else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 15943a2f7427SDavid Greenman && fdc->status[2] & NE7_ST2_WC 15953a2f7427SDavid Greenman && fdc->retry < 3) 15963a2f7427SDavid Greenman fdc->retry = 3; /* force recalibrate */ 15975b81b6b3SRodney W. Grimes return(retrier(fdcu)); 15985b81b6b3SRodney W. Grimes } 15995b81b6b3SRodney W. Grimes /* All OK */ 16003a2f7427SDavid Greenman fd->skip += fdblk; 1601b39c878eSAndrey A. Chernov if (!format && fd->skip < bp->b_bcount) 16025b81b6b3SRodney W. Grimes { 16035b81b6b3SRodney W. Grimes /* set up next transfer */ 16043a2f7427SDavid Greenman blknum = (unsigned long)bp->b_blkno*DEV_BSIZE/fdblk 16053a2f7427SDavid Greenman + fd->skip/fdblk; 16063a2f7427SDavid Greenman bp->b_cylin = 16073a2f7427SDavid Greenman (blknum / (fd->ft->sectrac * fd->ft->heads)); 16085b81b6b3SRodney W. Grimes fdc->state = DOSEEK; 16095b81b6b3SRodney W. Grimes } 16105b81b6b3SRodney W. Grimes else 16115b81b6b3SRodney W. Grimes { 16125b81b6b3SRodney W. Grimes /* ALL DONE */ 16135b81b6b3SRodney W. Grimes fd->skip = 0; 16145b81b6b3SRodney W. Grimes bp->b_resid = 0; 161517542807SPoul-Henning Kamp TAILQ_REMOVE(&fdc->head, bp, b_act); 16165b81b6b3SRodney W. Grimes biodone(bp); 16175b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 16185b81b6b3SRodney W. Grimes fdc->fdu = -1; 16195b81b6b3SRodney W. Grimes fdc->state = FINDWORK; 16205b81b6b3SRodney W. Grimes } 16215b81b6b3SRodney W. Grimes return(1); 16225b81b6b3SRodney W. Grimes case RESETCTLR: 16233a2f7427SDavid Greenman fdc_reset(fdc); 16245b81b6b3SRodney W. Grimes fdc->retry++; 16255b81b6b3SRodney W. Grimes fdc->state = STARTRECAL; 16265b81b6b3SRodney W. Grimes break; 16275b81b6b3SRodney W. Grimes case STARTRECAL: 16280e317d05SJoerg Wunsch /* XXX clear the fdc results from the last reset, if any. */ 16290e317d05SJoerg Wunsch { 16300e317d05SJoerg Wunsch int i; 16310e317d05SJoerg Wunsch for (i = 0; i < 4; i++) 16320e317d05SJoerg Wunsch (void)fd_sense_int(fdc, &st0, &cyl); 16330e317d05SJoerg Wunsch } 16340e317d05SJoerg Wunsch 1635dc5df763SJoerg Wunsch if(fd_cmd(fdcu, 1636dc5df763SJoerg Wunsch 2, NE7CMD_RECAL, fdu, 1637dc5df763SJoerg Wunsch 0)) /* Recalibrate Function */ 1638dc8603e3SJoerg Wunsch { 1639dc8603e3SJoerg Wunsch /* arrgl */ 1640dc8603e3SJoerg Wunsch fdc->retry = 6; 1641dc8603e3SJoerg Wunsch return(retrier(fdcu)); 1642dc8603e3SJoerg Wunsch } 16435b81b6b3SRodney W. Grimes fdc->state = RECALWAIT; 16445b81b6b3SRodney W. Grimes return(0); /* will return later */ 16455b81b6b3SRodney W. Grimes case RECALWAIT: 16465b81b6b3SRodney W. Grimes /* allow heads to settle */ 164704b734cfSPoul-Henning Kamp timeout(fd_pseudointr, (caddr_t)fdcu, hz / 8); 16485b81b6b3SRodney W. Grimes fdc->state = RECALCOMPLETE; 16495b81b6b3SRodney W. Grimes return(0); /* will return later */ 16505b81b6b3SRodney W. Grimes case RECALCOMPLETE: 16513a2f7427SDavid Greenman do { 16523a2f7427SDavid Greenman /* 1653dc5df763SJoerg Wunsch * See SEEKCOMPLETE for a comment on this: 16543a2f7427SDavid Greenman */ 1655dc5df763SJoerg Wunsch if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) 1656dc5df763SJoerg Wunsch return 0; 1657dc5df763SJoerg Wunsch if(fdc->fdct == FDC_NE765 1658dc5df763SJoerg Wunsch && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 1659dc5df763SJoerg Wunsch return 0; /* hope for a real intr */ 16603a2f7427SDavid Greenman } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 16613a2f7427SDavid Greenman if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0) 16625b81b6b3SRodney W. Grimes { 1663dc8603e3SJoerg Wunsch if(fdc->retry > 3) 1664dc8603e3SJoerg Wunsch /* 1665dc8603e3SJoerg Wunsch * a recalibrate from beyond cylinder 77 1666dc8603e3SJoerg Wunsch * will "fail" due to the FDC limitations; 1667dc8603e3SJoerg Wunsch * since people used to complain much about 1668dc8603e3SJoerg Wunsch * the failure message, try not logging 1669dc8603e3SJoerg Wunsch * this one if it seems to be the first 1670dc8603e3SJoerg Wunsch * time in a line 1671dc8603e3SJoerg Wunsch */ 1672dc8603e3SJoerg Wunsch printf("fd%d: recal failed ST0 %b cyl %d\n", 1673dc8603e3SJoerg Wunsch fdu, st0, NE7_ST0BITS, cyl); 16743a2f7427SDavid Greenman if(fdc->retry < 3) fdc->retry = 3; 16755b81b6b3SRodney W. Grimes return(retrier(fdcu)); 16765b81b6b3SRodney W. Grimes } 16775b81b6b3SRodney W. Grimes fd->track = 0; 16785b81b6b3SRodney W. Grimes /* Seek (probably) necessary */ 16795b81b6b3SRodney W. Grimes fdc->state = DOSEEK; 16805b81b6b3SRodney W. Grimes return(1); /* will return immediatly */ 16815b81b6b3SRodney W. Grimes case MOTORWAIT: 16825b81b6b3SRodney W. Grimes if(fd->flags & FD_MOTOR_WAIT) 16835b81b6b3SRodney W. Grimes { 16845b81b6b3SRodney W. Grimes return(0); /* time's not up yet */ 16855b81b6b3SRodney W. Grimes } 16865e235068SJordan K. Hubbard /* 16875e235068SJordan K. Hubbard * since the controller was off, it has lost its 16885e235068SJordan K. Hubbard * idea about the current track it were; thus, 16895e235068SJordan K. Hubbard * recalibrate the bastard 16905e235068SJordan K. Hubbard */ 16915e235068SJordan K. Hubbard fdc->state = STARTRECAL; 16925b81b6b3SRodney W. Grimes return(1); /* will return immediatly */ 16935b81b6b3SRodney W. Grimes default: 1694dc5df763SJoerg Wunsch printf("fdc%d: Unexpected FD int->", fdcu); 1695dc5df763SJoerg Wunsch if (fd_read_status(fdc, fd->fdsu) == 0) 1696dac0f2dbSJoerg Wunsch printf("FDC status :%lx %lx %lx %lx %lx %lx %lx ", 16975b81b6b3SRodney W. Grimes fdc->status[0], 16985b81b6b3SRodney W. Grimes fdc->status[1], 16995b81b6b3SRodney W. Grimes fdc->status[2], 17005b81b6b3SRodney W. Grimes fdc->status[3], 17015b81b6b3SRodney W. Grimes fdc->status[4], 17025b81b6b3SRodney W. Grimes fdc->status[5], 17035b81b6b3SRodney W. Grimes fdc->status[6] ); 17043a2f7427SDavid Greenman else 1705dac0f2dbSJoerg Wunsch printf("No status available "); 1706dac0f2dbSJoerg Wunsch if (fd_sense_int(fdc, &st0, &cyl) != 0) 1707dac0f2dbSJoerg Wunsch { 1708dac0f2dbSJoerg Wunsch printf("[controller is dead now]\n"); 17095b81b6b3SRodney W. Grimes return(0); 17105b81b6b3SRodney W. Grimes } 1711dac0f2dbSJoerg Wunsch printf("ST0 = %x, PCN = %x\n", st0, cyl); 1712dac0f2dbSJoerg Wunsch return(0); 1713dac0f2dbSJoerg Wunsch } 1714dac0f2dbSJoerg Wunsch /*XXX confusing: some branches return immediately, others end up here*/ 17155b81b6b3SRodney W. Grimes return(1); /* Come back immediatly to new state */ 17165b81b6b3SRodney W. Grimes } 17175b81b6b3SRodney W. Grimes 1718aaf08d94SGarrett Wollman static int 1719f5f7ba03SJordan K. Hubbard retrier(fdcu) 1720f5f7ba03SJordan K. Hubbard fdcu_t fdcu; 17215b81b6b3SRodney W. Grimes { 17225b81b6b3SRodney W. Grimes fdc_p fdc = fdc_data + fdcu; 172317542807SPoul-Henning Kamp register struct buf *bp; 17245b81b6b3SRodney W. Grimes 172517542807SPoul-Henning Kamp bp = TAILQ_FIRST(&fdc->head); 17265b81b6b3SRodney W. Grimes 17273a2f7427SDavid Greenman if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY) 17283a2f7427SDavid Greenman goto fail; 17295b81b6b3SRodney W. Grimes switch(fdc->retry) 17305b81b6b3SRodney W. Grimes { 17315b81b6b3SRodney W. Grimes case 0: case 1: case 2: 17325b81b6b3SRodney W. Grimes fdc->state = SEEKCOMPLETE; 17335b81b6b3SRodney W. Grimes break; 17345b81b6b3SRodney W. Grimes case 3: case 4: case 5: 17355b81b6b3SRodney W. Grimes fdc->state = STARTRECAL; 17365b81b6b3SRodney W. Grimes break; 17375b81b6b3SRodney W. Grimes case 6: 17385b81b6b3SRodney W. Grimes fdc->state = RESETCTLR; 17395b81b6b3SRodney W. Grimes break; 17405b81b6b3SRodney W. Grimes case 7: 17415b81b6b3SRodney W. Grimes break; 17425b81b6b3SRodney W. Grimes default: 17433a2f7427SDavid Greenman fail: 17445b81b6b3SRodney W. Grimes { 17457ca0641bSAndrey A. Chernov dev_t sav_b_dev = bp->b_dev; 17467ca0641bSAndrey A. Chernov /* Trick diskerr */ 17473a2f7427SDavid Greenman bp->b_dev = makedev(major(bp->b_dev), 17483a2f7427SDavid Greenman (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART); 174992ed385aSRodney W. Grimes diskerr(bp, "fd", "hard error", LOG_PRINTF, 17503a2f7427SDavid Greenman fdc->fd->skip / DEV_BSIZE, 17513a2f7427SDavid Greenman (struct disklabel *)NULL); 17527ca0641bSAndrey A. Chernov bp->b_dev = sav_b_dev; 1753dc5df763SJoerg Wunsch if (fdc->flags & FDC_STAT_VALID) 1754dc5df763SJoerg Wunsch { 1755dc5df763SJoerg Wunsch printf( 1756dc5df763SJoerg Wunsch " (ST0 %b ST1 %b ST2 %b cyl %ld hd %ld sec %ld)\n", 1757dc5df763SJoerg Wunsch fdc->status[0], NE7_ST0BITS, 1758dc5df763SJoerg Wunsch fdc->status[1], NE7_ST1BITS, 1759dc5df763SJoerg Wunsch fdc->status[2], NE7_ST2BITS, 1760dc5df763SJoerg Wunsch fdc->status[3], fdc->status[4], 1761dc5df763SJoerg Wunsch fdc->status[5]); 1762dc5df763SJoerg Wunsch } 1763dc5df763SJoerg Wunsch else 1764dc5df763SJoerg Wunsch printf(" (No status)\n"); 17655b81b6b3SRodney W. Grimes } 17665b81b6b3SRodney W. Grimes bp->b_flags |= B_ERROR; 17675b81b6b3SRodney W. Grimes bp->b_error = EIO; 17685b81b6b3SRodney W. Grimes bp->b_resid = bp->b_bcount - fdc->fd->skip; 176917542807SPoul-Henning Kamp TAILQ_REMOVE(&fdc->head, bp, b_act); 17705b81b6b3SRodney W. Grimes fdc->fd->skip = 0; 17715b81b6b3SRodney W. Grimes biodone(bp); 177292ed385aSRodney W. Grimes fdc->state = FINDWORK; 17735b81b6b3SRodney W. Grimes fdc->fd = (fd_p) 0; 17745b81b6b3SRodney W. Grimes fdc->fdu = -1; 1775f5f7ba03SJordan K. Hubbard /* XXX abort current command, if any. */ 177692ed385aSRodney W. Grimes return(1); 17775b81b6b3SRodney W. Grimes } 17785b81b6b3SRodney W. Grimes fdc->retry++; 17795b81b6b3SRodney W. Grimes return(1); 17805b81b6b3SRodney W. Grimes } 17815b81b6b3SRodney W. Grimes 1782b39c878eSAndrey A. Chernov static int 1783b39c878eSAndrey A. Chernov fdformat(dev, finfo, p) 1784b39c878eSAndrey A. Chernov dev_t dev; 1785b39c878eSAndrey A. Chernov struct fd_formb *finfo; 1786b39c878eSAndrey A. Chernov struct proc *p; 1787b39c878eSAndrey A. Chernov { 1788b39c878eSAndrey A. Chernov fdu_t fdu; 1789b39c878eSAndrey A. Chernov fd_p fd; 1790b39c878eSAndrey A. Chernov 1791b39c878eSAndrey A. Chernov struct buf *bp; 1792b39c878eSAndrey A. Chernov int rv = 0, s; 17933a2f7427SDavid Greenman size_t fdblk; 1794b39c878eSAndrey A. Chernov 1795b39c878eSAndrey A. Chernov fdu = FDUNIT(minor(dev)); 1796b39c878eSAndrey A. Chernov fd = &fd_data[fdu]; 17973a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 1798b39c878eSAndrey A. Chernov 1799b39c878eSAndrey A. Chernov /* set up a buffer header for fdstrategy() */ 1800b39c878eSAndrey A. Chernov bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT); 1801b39c878eSAndrey A. Chernov if(bp == 0) 1802b39c878eSAndrey A. Chernov return ENOBUFS; 180382f5379bSJoerg Wunsch /* 180482f5379bSJoerg Wunsch * keep the process from being swapped 180582f5379bSJoerg Wunsch */ 180682f5379bSJoerg Wunsch p->p_flag |= P_PHYSIO; 1807b39c878eSAndrey A. Chernov bzero((void *)bp, sizeof(struct buf)); 1808b39c878eSAndrey A. Chernov bp->b_flags = B_BUSY | B_PHYS | B_FORMAT; 1809b39c878eSAndrey A. Chernov bp->b_proc = p; 1810b39c878eSAndrey A. Chernov bp->b_dev = dev; 1811b39c878eSAndrey A. Chernov 1812b39c878eSAndrey A. Chernov /* 1813b39c878eSAndrey A. Chernov * calculate a fake blkno, so fdstrategy() would initiate a 1814b39c878eSAndrey A. Chernov * seek to the requested cylinder 1815b39c878eSAndrey A. Chernov */ 1816b39c878eSAndrey A. Chernov bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) 18173a2f7427SDavid Greenman + finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE; 1818b39c878eSAndrey A. Chernov 1819b39c878eSAndrey A. Chernov bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs; 18205e235068SJordan K. Hubbard bp->b_un.b_addr = (caddr_t)finfo; 1821b39c878eSAndrey A. Chernov 1822b39c878eSAndrey A. Chernov /* now do the format */ 1823b39c878eSAndrey A. Chernov fdstrategy(bp); 1824b39c878eSAndrey A. Chernov 1825b39c878eSAndrey A. Chernov /* ...and wait for it to complete */ 1826b39c878eSAndrey A. Chernov s = splbio(); 1827b39c878eSAndrey A. Chernov while(!(bp->b_flags & B_DONE)) 1828b39c878eSAndrey A. Chernov { 18295e235068SJordan K. Hubbard rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz); 1830b39c878eSAndrey A. Chernov if(rv == EWOULDBLOCK) 1831b39c878eSAndrey A. Chernov break; 1832b39c878eSAndrey A. Chernov } 1833b39c878eSAndrey A. Chernov splx(s); 1834b39c878eSAndrey A. Chernov 183582f5379bSJoerg Wunsch if(rv == EWOULDBLOCK) { 1836b39c878eSAndrey A. Chernov /* timed out */ 1837b39c878eSAndrey A. Chernov rv = EIO; 183882f5379bSJoerg Wunsch biodone(bp); 183982f5379bSJoerg Wunsch } 18403a2f7427SDavid Greenman if(bp->b_flags & B_ERROR) 18413a2f7427SDavid Greenman rv = bp->b_error; 184282f5379bSJoerg Wunsch /* 184382f5379bSJoerg Wunsch * allow the process to be swapped 184482f5379bSJoerg Wunsch */ 184582f5379bSJoerg Wunsch p->p_flag &= ~P_PHYSIO; 1846b39c878eSAndrey A. Chernov free(bp, M_TEMP); 1847b39c878eSAndrey A. Chernov return rv; 1848b39c878eSAndrey A. Chernov } 1849b39c878eSAndrey A. Chernov 1850f5f7ba03SJordan K. Hubbard /* 1851671e2ceeSBruce Evans * TODO: don't allocate buffer on stack. 1852f5f7ba03SJordan K. Hubbard */ 18535b81b6b3SRodney W. Grimes 1854f5f7ba03SJordan K. Hubbard int 1855b39c878eSAndrey A. Chernov fdioctl(dev, cmd, addr, flag, p) 1856f5f7ba03SJordan K. Hubbard dev_t dev; 1857f5f7ba03SJordan K. Hubbard int cmd; 1858f5f7ba03SJordan K. Hubbard caddr_t addr; 1859f5f7ba03SJordan K. Hubbard int flag; 1860b39c878eSAndrey A. Chernov struct proc *p; 1861f5f7ba03SJordan K. Hubbard { 18623a2f7427SDavid Greenman fdu_t fdu = FDUNIT(minor(dev)); 18633a2f7427SDavid Greenman fd_p fd = &fd_data[fdu]; 18643a2f7427SDavid Greenman size_t fdblk; 18653a2f7427SDavid Greenman 1866f5f7ba03SJordan K. Hubbard struct fd_type *fdt; 1867f5f7ba03SJordan K. Hubbard struct disklabel *dl; 1868f5f7ba03SJordan K. Hubbard char buffer[DEV_BSIZE]; 18693a2f7427SDavid Greenman int error = 0; 1870f5f7ba03SJordan K. Hubbard 1871b99f0a4aSAndrew Moore #if NFT > 0 1872a60eff27SNate Williams int type = FDTYPE(minor(dev)); 1873a60eff27SNate Williams 1874a60eff27SNate Williams /* check for a tape ioctl */ 1875a60eff27SNate Williams if (type & F_TAPE_TYPE) 1876b99f0a4aSAndrew Moore return ftioctl(dev, cmd, addr, flag, p); 1877b99f0a4aSAndrew Moore #endif 1878b99f0a4aSAndrew Moore 18793a2f7427SDavid Greenman fdblk = 128 << fd->ft->secsize; 1880f5f7ba03SJordan K. Hubbard 1881f5f7ba03SJordan K. Hubbard switch (cmd) 1882f5f7ba03SJordan K. Hubbard { 1883f5f7ba03SJordan K. Hubbard case DIOCGDINFO: 1884f5f7ba03SJordan K. Hubbard bzero(buffer, sizeof (buffer)); 1885f5f7ba03SJordan K. Hubbard dl = (struct disklabel *)buffer; 18863a2f7427SDavid Greenman dl->d_secsize = fdblk; 188792ed385aSRodney W. Grimes fdt = fd_data[FDUNIT(minor(dev))].ft; 1888f5f7ba03SJordan K. Hubbard dl->d_secpercyl = fdt->size / fdt->tracks; 1889f5f7ba03SJordan K. Hubbard dl->d_type = DTYPE_FLOPPY; 1890f5f7ba03SJordan K. Hubbard 1891191e1a59SBruce Evans if (readdisklabel(dkmodpart(dev, RAW_PART), fdstrategy, dl) 1892191e1a59SBruce Evans == NULL) 1893f5f7ba03SJordan K. Hubbard error = 0; 1894f5f7ba03SJordan K. Hubbard else 1895f5f7ba03SJordan K. Hubbard error = EINVAL; 1896f5f7ba03SJordan K. Hubbard 1897f5f7ba03SJordan K. Hubbard *(struct disklabel *)addr = *dl; 1898f5f7ba03SJordan K. Hubbard break; 1899f5f7ba03SJordan K. Hubbard 1900f5f7ba03SJordan K. Hubbard case DIOCSDINFO: 1901f5f7ba03SJordan K. Hubbard if ((flag & FWRITE) == 0) 1902f5f7ba03SJordan K. Hubbard error = EBADF; 1903f5f7ba03SJordan K. Hubbard break; 1904f5f7ba03SJordan K. Hubbard 1905f5f7ba03SJordan K. Hubbard case DIOCWLABEL: 1906f5f7ba03SJordan K. Hubbard if ((flag & FWRITE) == 0) 1907f5f7ba03SJordan K. Hubbard error = EBADF; 1908f5f7ba03SJordan K. Hubbard break; 1909f5f7ba03SJordan K. Hubbard 1910f5f7ba03SJordan K. Hubbard case DIOCWDINFO: 1911f5f7ba03SJordan K. Hubbard if ((flag & FWRITE) == 0) 1912f5f7ba03SJordan K. Hubbard { 1913f5f7ba03SJordan K. Hubbard error = EBADF; 1914f5f7ba03SJordan K. Hubbard break; 1915f5f7ba03SJordan K. Hubbard } 1916f5f7ba03SJordan K. Hubbard 1917f5f7ba03SJordan K. Hubbard dl = (struct disklabel *)addr; 1918f5f7ba03SJordan K. Hubbard 1919191e1a59SBruce Evans if ((error = setdisklabel((struct disklabel *)buffer, dl, 1920191e1a59SBruce Evans (u_long)0)) != 0) 1921f5f7ba03SJordan K. Hubbard break; 1922f5f7ba03SJordan K. Hubbard 1923b39c878eSAndrey A. Chernov error = writedisklabel(dev, fdstrategy, 192454c7241bSJordan K. Hubbard (struct disklabel *)buffer); 1925b39c878eSAndrey A. Chernov break; 1926b39c878eSAndrey A. Chernov 1927b39c878eSAndrey A. Chernov case FD_FORM: 1928b39c878eSAndrey A. Chernov if((flag & FWRITE) == 0) 1929b39c878eSAndrey A. Chernov error = EBADF; /* must be opened for writing */ 1930b39c878eSAndrey A. Chernov else if(((struct fd_formb *)addr)->format_version != 1931b39c878eSAndrey A. Chernov FD_FORMAT_VERSION) 1932b39c878eSAndrey A. Chernov error = EINVAL; /* wrong version of formatting prog */ 1933b39c878eSAndrey A. Chernov else 1934b39c878eSAndrey A. Chernov error = fdformat(dev, (struct fd_formb *)addr, p); 1935b39c878eSAndrey A. Chernov break; 1936b39c878eSAndrey A. Chernov 1937b39c878eSAndrey A. Chernov case FD_GTYPE: /* get drive type */ 1938b39c878eSAndrey A. Chernov *(struct fd_type *)addr = *fd_data[FDUNIT(minor(dev))].ft; 1939f5f7ba03SJordan K. Hubbard break; 1940f5f7ba03SJordan K. Hubbard 19413a2f7427SDavid Greenman case FD_STYPE: /* set drive type */ 19423a2f7427SDavid Greenman /* this is considered harmful; only allow for superuser */ 19433a2f7427SDavid Greenman if(suser(p->p_ucred, &p->p_acflag) != 0) 19443a2f7427SDavid Greenman return EPERM; 19453a2f7427SDavid Greenman *fd_data[FDUNIT(minor(dev))].ft = *(struct fd_type *)addr; 19463a2f7427SDavid Greenman break; 19473a2f7427SDavid Greenman 19483a2f7427SDavid Greenman case FD_GOPTS: /* get drive options */ 19493a2f7427SDavid Greenman *(int *)addr = fd_data[FDUNIT(minor(dev))].options; 19503a2f7427SDavid Greenman break; 19513a2f7427SDavid Greenman 19523a2f7427SDavid Greenman case FD_SOPTS: /* set drive options */ 19533a2f7427SDavid Greenman fd_data[FDUNIT(minor(dev))].options = *(int *)addr; 19543a2f7427SDavid Greenman break; 19553a2f7427SDavid Greenman 1956f5f7ba03SJordan K. Hubbard default: 19573a2f7427SDavid Greenman error = ENOTTY; 1958f5f7ba03SJordan K. Hubbard break; 1959f5f7ba03SJordan K. Hubbard } 1960f5f7ba03SJordan K. Hubbard return (error); 1961f5f7ba03SJordan K. Hubbard } 1962f5f7ba03SJordan K. Hubbard 19637146c13eSJulian Elischer 19647146c13eSJulian Elischer static fd_devsw_installed = 0; 19657146c13eSJulian Elischer 196687f6c662SJulian Elischer static void fd_drvinit(void *notused ) 19677146c13eSJulian Elischer { 196887f6c662SJulian Elischer 19697146c13eSJulian Elischer if( ! fd_devsw_installed ) { 1970cba8a5ddSPoul-Henning Kamp bdevsw_add_generic(BDEV_MAJOR,CDEV_MAJOR, &fd_bdevsw); 19717146c13eSJulian Elischer fd_devsw_installed = 1; 19727146c13eSJulian Elischer } 19737146c13eSJulian Elischer } 197487f6c662SJulian Elischer 197587f6c662SJulian Elischer SYSINIT(fddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,fd_drvinit,NULL) 197687f6c662SJulian Elischer 1977f5f7ba03SJordan K. Hubbard #endif 19783a2f7427SDavid Greenman /* 19793a2f7427SDavid Greenman * Hello emacs, these are the 19803a2f7427SDavid Greenman * Local Variables: 19813a2f7427SDavid Greenman * c-indent-level: 8 19823a2f7427SDavid Greenman * c-continued-statement-offset: 8 19833a2f7427SDavid Greenman * c-continued-brace-offset: 0 19843a2f7427SDavid Greenman * c-brace-offset: -8 19853a2f7427SDavid Greenman * c-brace-imaginary-offset: 0 19863a2f7427SDavid Greenman * c-argdecl-indent: 8 19873a2f7427SDavid Greenman * c-label-offset: -8 19883a2f7427SDavid Greenman * c++-hanging-braces: 1 19893a2f7427SDavid Greenman * c++-access-specifier-offset: -8 19903a2f7427SDavid Greenman * c++-empty-arglist-indent: 8 19913a2f7427SDavid Greenman * c++-friend-offset: 0 19923a2f7427SDavid Greenman * End: 19933a2f7427SDavid Greenman */ 1994