1*0f9e9ec2Sjsg /* $OpenBSD: fd.c,v 1.109 2024/05/13 01:15:50 jsg Exp $ */
24176a241Sderaadt /* $NetBSD: fd.c,v 1.90 1996/05/12 23:12:03 mycroft Exp $ */
3df930be7Sderaadt
4df930be7Sderaadt /*-
53af98d30Sdownsj * Copyright (c) 1993, 1994, 1995, 1996 Charles Hannum.
6df930be7Sderaadt * Copyright (c) 1990 The Regents of the University of California.
7df930be7Sderaadt * All rights reserved.
8df930be7Sderaadt *
9df930be7Sderaadt * This code is derived from software contributed to Berkeley by
10df930be7Sderaadt * Don Ahn.
11df930be7Sderaadt *
121921243fSdownsj * Portions Copyright (c) 1993, 1994 by
131921243fSdownsj * jc@irbs.UUCP (John Capo)
141921243fSdownsj * vak@zebub.msk.su (Serge Vakulenko)
151921243fSdownsj * ache@astral.msk.su (Andrew A. Chernov)
161921243fSdownsj * joerg_wunsch@uriah.sax.de (Joerg Wunsch)
171921243fSdownsj *
18df930be7Sderaadt * Redistribution and use in source and binary forms, with or without
19df930be7Sderaadt * modification, are permitted provided that the following conditions
20df930be7Sderaadt * are met:
21df930be7Sderaadt * 1. Redistributions of source code must retain the above copyright
22df930be7Sderaadt * notice, this list of conditions and the following disclaimer.
23df930be7Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
24df930be7Sderaadt * notice, this list of conditions and the following disclaimer in the
25df930be7Sderaadt * documentation and/or other materials provided with the distribution.
2629295d1cSmillert * 3. Neither the name of the University nor the names of its contributors
27df930be7Sderaadt * may be used to endorse or promote products derived from this software
28df930be7Sderaadt * without specific prior written permission.
29df930be7Sderaadt *
30df930be7Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31df930be7Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32df930be7Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33df930be7Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34df930be7Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35df930be7Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36df930be7Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37df930be7Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38df930be7Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39df930be7Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40df930be7Sderaadt * SUCH DAMAGE.
41df930be7Sderaadt *
42df930be7Sderaadt * @(#)fd.c 7.4 (Berkeley) 5/25/91
43df930be7Sderaadt */
44df930be7Sderaadt
45df930be7Sderaadt #include <sys/param.h>
46df930be7Sderaadt #include <sys/systm.h>
47df930be7Sderaadt #include <sys/kernel.h>
48c0cd3489Sguenther #include <sys/fcntl.h>
49df930be7Sderaadt #include <sys/ioctl.h>
50df930be7Sderaadt #include <sys/device.h>
51df930be7Sderaadt #include <sys/disklabel.h>
52df930be7Sderaadt #include <sys/disk.h>
53df930be7Sderaadt #include <sys/buf.h>
541921243fSdownsj #include <sys/malloc.h>
55df930be7Sderaadt #include <sys/uio.h>
564b64fdbfSderaadt #include <sys/mtio.h>
57df930be7Sderaadt #include <sys/syslog.h>
58df930be7Sderaadt #include <sys/queue.h>
59190f1464Skrw #include <sys/stat.h>
607d33d8deSho #include <sys/timeout.h>
6191f4f7d8Sdlg #include <sys/dkio.h>
62df930be7Sderaadt
63df930be7Sderaadt #include <machine/cpu.h>
64b9a85264Sniklas #include <machine/bus.h>
654176a241Sderaadt #include <machine/intr.h>
661921243fSdownsj #include <machine/ioctl_fd.h>
67df930be7Sderaadt
68df930be7Sderaadt #include <dev/isa/isavar.h>
69df930be7Sderaadt #include <dev/isa/isadmavar.h>
7071e39c02Sdownsj #include <dev/isa/fdreg.h>
71df930be7Sderaadt
72aff9e93dSderaadt #if defined(__i386__) || defined(__amd64__) /* XXX */
73df930be7Sderaadt #include <i386/isa/nvram.h>
7471e39c02Sdownsj #endif
75df930be7Sderaadt
7671e39c02Sdownsj #include <dev/isa/fdlink.h>
773041a0b9Sdownsj
781921243fSdownsj /* XXX misuse a flag to identify format operation */
791921243fSdownsj #define B_FORMAT B_XXX
801921243fSdownsj
811921243fSdownsj /* fd_type struct now in ioctl_fd.h */
82df930be7Sderaadt
83df930be7Sderaadt /* The order of entries in the following table is important -- BEWARE! */
84df930be7Sderaadt struct fd_type fd_types[] = {
85df930be7Sderaadt { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB" }, /* 1.44MB diskette */
86df930be7Sderaadt { 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS, "1.2MB" }, /* 1.2 MB AT-diskettes */
87df930be7Sderaadt { 9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS, "360KB/AT" }, /* 360kB in 1.2MB drive */
88df930be7Sderaadt { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS, "360KB/PC" }, /* 360kB PC diskettes */
89df930be7Sderaadt { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS, "720KB" }, /* 3.5" 720kB diskette */
90df930be7Sderaadt { 9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS, "720KB/x" }, /* 720kB in 1.2MB drive */
91df930be7Sderaadt { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS, "360KB/x" }, /* 360kB in 720kB drive */
9276c8686eSdownsj { 36,2,72,2,0xff,0xaf,0x1b,0x54,80,5760,1,FDC_500KBPS,"2.88MB" }, /* 2.88MB diskette */
9376c8686eSdownsj { 8,2,16,3,0xff,0xdf,0x35,0x74,77,1232,1,FDC_500KBPS,"1.2MB/[1024bytes/sector]" } /* 1.2 MB japanese format */
94df930be7Sderaadt };
95df930be7Sderaadt
96df930be7Sderaadt /* software state, per disk (with up to 4 disks per ctlr) */
97df930be7Sderaadt struct fd_softc {
98df930be7Sderaadt struct device sc_dev;
993dbef52bSderaadt struct disk sc_dk;
100df930be7Sderaadt
101df930be7Sderaadt struct fd_type *sc_deftype; /* default type descriptor */
102df930be7Sderaadt struct fd_type *sc_type; /* current type descriptor */
103df930be7Sderaadt
1041abdbfdeSderaadt daddr_t sc_blkno; /* starting block number */
105df930be7Sderaadt int sc_bcount; /* byte count left */
1061921243fSdownsj int sc_opts; /* user-set options */
107df930be7Sderaadt int sc_skip; /* bytes already transferred */
10884d3ec93Skrw int sc_nblks; /* number of blocks currently transferring */
10984d3ec93Skrw int sc_nbytes; /* number of bytes currently transferring */
110df930be7Sderaadt
111df930be7Sderaadt int sc_drive; /* physical unit number */
112df930be7Sderaadt int sc_flags;
113df930be7Sderaadt #define FD_OPEN 0x01 /* it's open */
114df930be7Sderaadt #define FD_MOTOR 0x02 /* motor should be on */
115df930be7Sderaadt #define FD_MOTOR_WAIT 0x04 /* motor coming up */
116df930be7Sderaadt int sc_cylin; /* where we think the head is */
117df930be7Sderaadt
118df930be7Sderaadt TAILQ_ENTRY(fd_softc) sc_drivechain;
119df930be7Sderaadt int sc_ops; /* I/O ops since last switch */
1208ea83772Sdlg struct bufq sc_bufq; /* pending I/O */
1218ea83772Sdlg struct buf *sc_bp; /* the current I/O */
1227d33d8deSho struct timeout fd_motor_on_to;
1237d33d8deSho struct timeout fd_motor_off_to;
1247d33d8deSho struct timeout fdtimeout_to;
125df930be7Sderaadt };
126df930be7Sderaadt
127df930be7Sderaadt /* floppy driver configuration */
128c4071fd1Smillert int fdprobe(struct device *, void *, void *);
129c4071fd1Smillert void fdattach(struct device *, struct device *, void *);
130c06fda6dSderaadt int fdactivate(struct device *, int);
131df930be7Sderaadt
132471aeecfSnaddy const struct cfattach fd_ca = {
133c06fda6dSderaadt sizeof(struct fd_softc), fdprobe, fdattach, NULL, fdactivate
134d724e01aSderaadt };
135d724e01aSderaadt
136d724e01aSderaadt struct cfdriver fd_cd = {
137d724e01aSderaadt NULL, "fd", DV_DISK
138df930be7Sderaadt };
139df930be7Sderaadt
140df591ed6Sderaadt int fdgetdisklabel(dev_t, struct fd_softc *, struct disklabel *, int);
141c4071fd1Smillert void fdstrategy(struct buf *);
142c4071fd1Smillert void fdstart(struct fd_softc *);
143c4071fd1Smillert int fdintr(struct fdc_softc *);
144df930be7Sderaadt
145c4071fd1Smillert void fd_set_motor(struct fdc_softc *fdc, int reset);
146c4071fd1Smillert void fd_motor_off(void *arg);
147c4071fd1Smillert void fd_motor_on(void *arg);
148c4071fd1Smillert void fdfinish(struct fd_softc *fd, struct buf *bp);
149c4071fd1Smillert int fdformat(dev_t, struct fd_formb *, struct proc *);
1505398df4bSderaadt static __inline struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
151c4071fd1Smillert void fdretry(struct fd_softc *);
152c4071fd1Smillert void fdtimeout(void *);
153df930be7Sderaadt
154df591ed6Sderaadt int
fdgetdisklabel(dev_t dev,struct fd_softc * fd,struct disklabel * lp,int spoofonly)155190f1464Skrw fdgetdisklabel(dev_t dev, struct fd_softc *fd, struct disklabel *lp,
156190f1464Skrw int spoofonly)
157190f1464Skrw {
158190f1464Skrw bzero(lp, sizeof(struct disklabel));
159190f1464Skrw
160190f1464Skrw lp->d_type = DTYPE_FLOPPY;
161190f1464Skrw lp->d_secsize = FD_BSIZE(fd);
162190f1464Skrw lp->d_secpercyl = fd->sc_type->seccyl;
163190f1464Skrw lp->d_nsectors = fd->sc_type->sectrac;
164190f1464Skrw lp->d_ncylinders = fd->sc_type->tracks;
165190f1464Skrw lp->d_ntracks = fd->sc_type->heads; /* Go figure... */
166190f1464Skrw DL_SETDSIZE(lp, fd->sc_type->size);
167190f1464Skrw
168190f1464Skrw strncpy(lp->d_typename, "floppy disk", sizeof(lp->d_typename));
169190f1464Skrw strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
170190f1464Skrw lp->d_version = 1;
171190f1464Skrw
172190f1464Skrw lp->d_magic = DISKMAGIC;
173190f1464Skrw lp->d_magic2 = DISKMAGIC;
174190f1464Skrw lp->d_checksum = dkcksum(lp);
175190f1464Skrw
176190f1464Skrw /*
177190f1464Skrw * Call the generic disklabel extraction routine. If there's
178190f1464Skrw * not a label there, fake it.
179190f1464Skrw */
180df591ed6Sderaadt return readdisklabel(DISKLABELDEV(dev), fdstrategy, lp, spoofonly);
181190f1464Skrw }
182190f1464Skrw
183df930be7Sderaadt int
fdprobe(struct device * parent,void * match,void * aux)1840e9d7eafSmatthew fdprobe(struct device *parent, void *match, void *aux)
185df930be7Sderaadt {
186df930be7Sderaadt struct fdc_softc *fdc = (void *)parent;
187df930be7Sderaadt struct cfdata *cf = match;
188df930be7Sderaadt struct fdc_attach_args *fa = aux;
189df930be7Sderaadt int drive = fa->fa_drive;
190b9a85264Sniklas bus_space_tag_t iot = fdc->sc_iot;
191b9a85264Sniklas bus_space_handle_t ioh = fdc->sc_ioh;
192df930be7Sderaadt int n;
193df930be7Sderaadt
194df930be7Sderaadt if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != drive)
195df930be7Sderaadt return 0;
196df930be7Sderaadt /*
197df930be7Sderaadt * XXX
198df930be7Sderaadt * This is to work around some odd interactions between this driver
199df930be7Sderaadt * and SMC Ethernet cards.
200df930be7Sderaadt */
201df930be7Sderaadt if (cf->cf_loc[0] == -1 && drive >= 2)
202df930be7Sderaadt return 0;
203df930be7Sderaadt
204e9475ca2Sdownsj /*
205e9475ca2Sdownsj * We want to keep the flags config gave us.
206e9475ca2Sdownsj */
207e9475ca2Sdownsj fa->fa_flags = cf->cf_flags;
208e9475ca2Sdownsj
209df930be7Sderaadt /* select drive and turn on motor */
210b9a85264Sniklas bus_space_write_1(iot, ioh, fdout, drive | FDO_FRST | FDO_MOEN(drive));
211df930be7Sderaadt /* wait for motor to spin up */
21211969f56Scheloha tsleep_nsec(fdc, 0, "fdprobe", MSEC_TO_NSEC(250));
213b9a85264Sniklas out_fdc(iot, ioh, NE7CMD_RECAL);
214b9a85264Sniklas out_fdc(iot, ioh, drive);
215df930be7Sderaadt /* wait for recalibrate */
21611969f56Scheloha tsleep_nsec(fdc, 0, "fdprobe", MSEC_TO_NSEC(2000));
217b9a85264Sniklas out_fdc(iot, ioh, NE7CMD_SENSEI);
218df930be7Sderaadt n = fdcresult(fdc);
219df930be7Sderaadt #ifdef FD_DEBUG
220df930be7Sderaadt {
221df930be7Sderaadt int i;
222df930be7Sderaadt printf("fdprobe: status");
223df930be7Sderaadt for (i = 0; i < n; i++)
224df930be7Sderaadt printf(" %x", fdc->sc_status[i]);
225df930be7Sderaadt printf("\n");
226df930be7Sderaadt }
227df930be7Sderaadt #endif
22859107f6cSdownsj
22959107f6cSdownsj /* turn off motor */
23011969f56Scheloha tsleep_nsec(fdc, 0, "fdprobe", MSEC_TO_NSEC(250));
23159107f6cSdownsj bus_space_write_1(iot, ioh, fdout, FDO_FRST);
23259107f6cSdownsj
233768789fcSrees /* flags & 0x20 forces the drive to be found even if it won't probe */
234768789fcSrees if (!(fa->fa_flags & 0x20) && (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20))
235df930be7Sderaadt return 0;
236df930be7Sderaadt
237df930be7Sderaadt return 1;
238df930be7Sderaadt }
239df930be7Sderaadt
240df930be7Sderaadt /*
241df930be7Sderaadt * Controller is working, and drive responded. Attach it.
242df930be7Sderaadt */
243df930be7Sderaadt void
fdattach(struct device * parent,struct device * self,void * aux)2440e9d7eafSmatthew fdattach(struct device *parent, struct device *self, void *aux)
245df930be7Sderaadt {
246df930be7Sderaadt struct fdc_softc *fdc = (void *)parent;
247df930be7Sderaadt struct fd_softc *fd = (void *)self;
248df930be7Sderaadt struct fdc_attach_args *fa = aux;
249df930be7Sderaadt struct fd_type *type = fa->fa_deftype;
250df930be7Sderaadt int drive = fa->fa_drive;
251df930be7Sderaadt
252e9475ca2Sdownsj if (!type || (fa->fa_flags & 0x10)) {
253e9475ca2Sdownsj /* The config has overridden this. */
254e9475ca2Sdownsj switch (fa->fa_flags & 0x07) {
2553af98d30Sdownsj case 1: /* 2.88MB */
2563af98d30Sdownsj type = &fd_types[7];
2573af98d30Sdownsj break;
258e9475ca2Sdownsj case 2: /* 1.44MB */
259e9475ca2Sdownsj type = &fd_types[0];
260e9475ca2Sdownsj break;
261e9475ca2Sdownsj case 3: /* 1.2MB */
262e9475ca2Sdownsj type = &fd_types[1];
263e9475ca2Sdownsj break;
264e9475ca2Sdownsj case 4: /* 720K */
265e9475ca2Sdownsj type = &fd_types[4];
266e9475ca2Sdownsj break;
267e9475ca2Sdownsj case 5: /* 360K */
268e9475ca2Sdownsj type = &fd_types[3];
269e9475ca2Sdownsj break;
27076c8686eSdownsj case 6: /* 1.2 MB japanese format */
27176c8686eSdownsj type = &fd_types[8];
27276c8686eSdownsj break;
273d1688987Snate #ifdef __alpha__
274d1688987Snate default:
275d1688987Snate /* 1.44MB, how to detect others?
276d1688987Snate * idea from NetBSD -- jay@rootaction.net
277d1688987Snate */
278d1688987Snate type = &fd_types[0];
279d1688987Snate #endif
280e9475ca2Sdownsj }
281e9475ca2Sdownsj }
282df930be7Sderaadt
283df930be7Sderaadt if (type)
284df930be7Sderaadt printf(": %s %d cyl, %d head, %d sec\n", type->name,
285df930be7Sderaadt type->tracks, type->heads, type->sectrac);
286df930be7Sderaadt else
287df930be7Sderaadt printf(": density unknown\n");
288df930be7Sderaadt
289df930be7Sderaadt fd->sc_cylin = -1;
290df930be7Sderaadt fd->sc_drive = drive;
291df930be7Sderaadt fd->sc_deftype = type;
2923af98d30Sdownsj fdc->sc_type[drive] = FDC_TYPE_DISK;
2933af98d30Sdownsj fdc->sc_link.fdlink.sc_fd[drive] = fd;
2943dbef52bSderaadt
2953dbef52bSderaadt /*
2963dbef52bSderaadt * Initialize and attach the disk structure.
2973dbef52bSderaadt */
29828696e49Sjsing fd->sc_dk.dk_flags = DKF_NOLABELREAD;
2993dbef52bSderaadt fd->sc_dk.dk_name = fd->sc_dev.dv_xname;
3008ea83772Sdlg bufq_init(&fd->sc_bufq, BUFQ_DEFAULT);
3012690bc4bSjsing disk_attach(&fd->sc_dev, &fd->sc_dk);
3023dbef52bSderaadt
3037d33d8deSho /* Setup timeout structures */
3047d33d8deSho timeout_set(&fd->fd_motor_on_to, fd_motor_on, fd);
3057d33d8deSho timeout_set(&fd->fd_motor_off_to, fd_motor_off, fd);
3067d33d8deSho timeout_set(&fd->fdtimeout_to, fdtimeout, fd);
307df930be7Sderaadt }
308df930be7Sderaadt
309c06fda6dSderaadt int
fdactivate(struct device * self,int act)310c06fda6dSderaadt fdactivate(struct device *self, int act)
311c06fda6dSderaadt {
3126d8f2501Sderaadt struct fd_softc *fd = (void *)self;
3136d8f2501Sderaadt struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
314c06fda6dSderaadt int rv = 0;
315c06fda6dSderaadt
316c06fda6dSderaadt switch (act) {
3176d8f2501Sderaadt case DVACT_SUSPEND:
3186d8f2501Sderaadt if (fdc->sc_state != DEVIDLE) {
3196d8f2501Sderaadt timeout_del(&fd->fd_motor_on_to);
3206d8f2501Sderaadt timeout_del(&fd->fd_motor_off_to);
3216d8f2501Sderaadt timeout_del(&fd->fdtimeout_to);
3226d8f2501Sderaadt fdc->sc_state = IOTIMEDOUT;
3236d8f2501Sderaadt fdc->sc_errors = 4;
3246d8f2501Sderaadt }
3256d8f2501Sderaadt break;
326c06fda6dSderaadt case DVACT_POWERDOWN:
327c06fda6dSderaadt fd_motor_off(self);
328c06fda6dSderaadt break;
329c06fda6dSderaadt }
330c06fda6dSderaadt
331c06fda6dSderaadt return (rv);
332c06fda6dSderaadt }
333c06fda6dSderaadt
334df930be7Sderaadt /*
335df930be7Sderaadt * Translate nvram type into internal data structure. Return NULL for
336df930be7Sderaadt * none/unknown/unusable.
337df930be7Sderaadt */
338df930be7Sderaadt struct fd_type *
fd_nvtotype(char * fdc,int nvraminfo,int drive)3390e9d7eafSmatthew fd_nvtotype(char *fdc, int nvraminfo, int drive)
340df930be7Sderaadt {
341d1688987Snate #ifdef __alpha__
342d1688987Snate /* Alpha: assume 1.44MB, idea from NetBSD sys/dev/isa/fd.c
343d1688987Snate * -- jay@rootaction.net
344d1688987Snate */
345d1688987Snate return &fd_types[0]; /* 1.44MB */
346d1688987Snate #else
347df930be7Sderaadt int type;
348df930be7Sderaadt
349df930be7Sderaadt type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
350df930be7Sderaadt switch (type) {
351df930be7Sderaadt case NVRAM_DISKETTE_NONE:
352df930be7Sderaadt return NULL;
353df930be7Sderaadt case NVRAM_DISKETTE_12M:
354df930be7Sderaadt return &fd_types[1];
355df930be7Sderaadt case NVRAM_DISKETTE_TYPE5:
356df930be7Sderaadt case NVRAM_DISKETTE_TYPE6:
3573af98d30Sdownsj return &fd_types[7];
358df930be7Sderaadt case NVRAM_DISKETTE_144M:
359df930be7Sderaadt return &fd_types[0];
360df930be7Sderaadt case NVRAM_DISKETTE_360K:
361df930be7Sderaadt return &fd_types[3];
362df930be7Sderaadt case NVRAM_DISKETTE_720K:
363df930be7Sderaadt return &fd_types[4];
364df930be7Sderaadt default:
365df930be7Sderaadt printf("%s: drive %d: unknown device type 0x%x\n",
366df930be7Sderaadt fdc, drive, type);
367df930be7Sderaadt return NULL;
368df930be7Sderaadt }
369d1688987Snate #endif
370df930be7Sderaadt }
371df930be7Sderaadt
3725398df4bSderaadt static __inline struct fd_type *
fd_dev_to_type(struct fd_softc * fd,dev_t dev)3730e9d7eafSmatthew fd_dev_to_type(struct fd_softc *fd, dev_t dev)
374df930be7Sderaadt {
375df930be7Sderaadt int type = FDTYPE(dev);
376df930be7Sderaadt
377df930be7Sderaadt if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
378df930be7Sderaadt return NULL;
379df930be7Sderaadt return type ? &fd_types[type - 1] : fd->sc_deftype;
380df930be7Sderaadt }
381df930be7Sderaadt
382df930be7Sderaadt void
fdstrategy(struct buf * bp)3830e9d7eafSmatthew fdstrategy(struct buf *bp)
384df930be7Sderaadt {
3853af98d30Sdownsj struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(bp->b_dev)];
386df930be7Sderaadt int sz;
387df930be7Sderaadt int s;
388cfe658c7Skrw int fd_bsize = FD_BSIZE(fd);
38976c8686eSdownsj int bf = fd_bsize / DEV_BSIZE;
390df930be7Sderaadt
391df930be7Sderaadt /* Valid unit, controller, and request? */
3923af98d30Sdownsj if (bp->b_blkno < 0 ||
39376c8686eSdownsj (((bp->b_blkno % bf) != 0 ||
39476c8686eSdownsj (bp->b_bcount % fd_bsize) != 0) &&
3951921243fSdownsj (bp->b_flags & B_FORMAT) == 0)) {
396df930be7Sderaadt bp->b_error = EINVAL;
397df930be7Sderaadt goto bad;
398df930be7Sderaadt }
399df930be7Sderaadt
400df930be7Sderaadt /* If it's a null transfer, return immediately. */
401df930be7Sderaadt if (bp->b_bcount == 0)
402df930be7Sderaadt goto done;
403df930be7Sderaadt
40476c8686eSdownsj sz = howmany(bp->b_bcount, DEV_BSIZE);
405df930be7Sderaadt
40676c8686eSdownsj if (bp->b_blkno + sz > fd->sc_type->size * bf) {
40776c8686eSdownsj sz = fd->sc_type->size * bf - bp->b_blkno;
4086b174afeSderaadt if (sz == 0)
409df930be7Sderaadt /* If exactly at end of disk, return EOF. */
410df930be7Sderaadt goto done;
411df930be7Sderaadt if (sz < 0) {
412df930be7Sderaadt /* If past end of disk, return EINVAL. */
413df930be7Sderaadt bp->b_error = EINVAL;
414df930be7Sderaadt goto bad;
415df930be7Sderaadt }
416df930be7Sderaadt /* Otherwise, truncate request. */
417df930be7Sderaadt bp->b_bcount = sz << DEV_BSHIFT;
418df930be7Sderaadt }
419df930be7Sderaadt
42027f37992Sdlg bp->b_resid = bp->b_bcount;
421df930be7Sderaadt
422df930be7Sderaadt #ifdef FD_DEBUG
42327f37992Sdlg printf("fdstrategy: b_blkno %lld b_bcount %d blkno %lld sz %d\n",
42427f37992Sdlg (long long)bp->b_blkno, bp->b_bcount,
42527f37992Sdlg (long long)fd->sc_blkno, sz);
426df930be7Sderaadt #endif
427df930be7Sderaadt
4288ea83772Sdlg /* Queue I/O */
4298ea83772Sdlg bufq_queue(&fd->sc_bufq, bp);
4308ea83772Sdlg
431df930be7Sderaadt /* Queue transfer on drive, activate drive and controller if idle. */
432df930be7Sderaadt s = splbio();
4337d33d8deSho timeout_del(&fd->fd_motor_off_to); /* a good idea */
4348ea83772Sdlg if (fd->sc_bp == NULL)
435df930be7Sderaadt fdstart(fd);
436df930be7Sderaadt #ifdef DIAGNOSTIC
437df930be7Sderaadt else {
438df930be7Sderaadt struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
439df930be7Sderaadt if (fdc->sc_state == DEVIDLE) {
440df930be7Sderaadt printf("fdstrategy: controller inactive\n");
441df930be7Sderaadt fdcstart(fdc);
442df930be7Sderaadt }
443df930be7Sderaadt }
444df930be7Sderaadt #endif
445df930be7Sderaadt splx(s);
446df930be7Sderaadt return;
447df930be7Sderaadt
448df930be7Sderaadt bad:
449df930be7Sderaadt bp->b_flags |= B_ERROR;
450df930be7Sderaadt done:
451df930be7Sderaadt /* Toss transfer; we're done early. */
4526b174afeSderaadt bp->b_resid = bp->b_bcount;
453259d6dc6Sart s = splbio();
454df930be7Sderaadt biodone(bp);
455259d6dc6Sart splx(s);
456df930be7Sderaadt }
457df930be7Sderaadt
458df930be7Sderaadt void
fdstart(struct fd_softc * fd)4590e9d7eafSmatthew fdstart(struct fd_softc *fd)
460df930be7Sderaadt {
461df930be7Sderaadt struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
4629d08f8e5Smiod int active = !TAILQ_EMPTY(&fdc->sc_link.fdlink.sc_drives);
463df930be7Sderaadt
464df930be7Sderaadt /* Link into controller queue. */
4658ea83772Sdlg fd->sc_bp = bufq_dequeue(&fd->sc_bufq);
4663af98d30Sdownsj TAILQ_INSERT_TAIL(&fdc->sc_link.fdlink.sc_drives, fd, sc_drivechain);
467df930be7Sderaadt
468df930be7Sderaadt /* If controller not already active, start it. */
469df930be7Sderaadt if (!active)
470df930be7Sderaadt fdcstart(fdc);
471df930be7Sderaadt }
472df930be7Sderaadt
473df930be7Sderaadt void
fdfinish(struct fd_softc * fd,struct buf * bp)4740e9d7eafSmatthew fdfinish(struct fd_softc *fd, struct buf *bp)
475df930be7Sderaadt {
476df930be7Sderaadt struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
477df930be7Sderaadt
478259d6dc6Sart splassert(IPL_BIO);
479259d6dc6Sart
4808ea83772Sdlg fd->sc_skip = 0;
4818ea83772Sdlg fd->sc_bp = bufq_dequeue(&fd->sc_bufq);
4828ea83772Sdlg
483df930be7Sderaadt /*
484df930be7Sderaadt * Move this drive to the end of the queue to give others a `fair'
485df930be7Sderaadt * chance. We only force a switch if N operations are completed while
486df930be7Sderaadt * another drive is waiting to be serviced, since there is a long motor
487df930be7Sderaadt * startup delay whenever we switch.
488df930be7Sderaadt */
4899d08f8e5Smiod if (TAILQ_NEXT(fd, sc_drivechain) != NULL && ++fd->sc_ops >= 8) {
490df930be7Sderaadt fd->sc_ops = 0;
4913af98d30Sdownsj TAILQ_REMOVE(&fdc->sc_link.fdlink.sc_drives, fd, sc_drivechain);
4928ea83772Sdlg if (fd->sc_bp != NULL) {
4933af98d30Sdownsj TAILQ_INSERT_TAIL(&fdc->sc_link.fdlink.sc_drives, fd,
4943af98d30Sdownsj sc_drivechain);
495df930be7Sderaadt }
4968ea83772Sdlg }
4973dbef52bSderaadt
498df930be7Sderaadt biodone(bp);
499df930be7Sderaadt /* turn off motor 5s from now */
50029e86e5eSblambert timeout_add_sec(&fd->fd_motor_off_to, 5);
501df930be7Sderaadt fdc->sc_state = DEVIDLE;
502df930be7Sderaadt }
503df930be7Sderaadt
504df930be7Sderaadt int
fdread(dev_t dev,struct uio * uio,int flags)5050e9d7eafSmatthew fdread(dev_t dev, struct uio *uio, int flags)
506df930be7Sderaadt {
50790f6ca67Smatthew return (physio(fdstrategy, dev, B_READ, minphys, uio));
508df930be7Sderaadt }
509df930be7Sderaadt
510df930be7Sderaadt int
fdwrite(dev_t dev,struct uio * uio,int flags)5110e9d7eafSmatthew fdwrite(dev_t dev, struct uio *uio, int flags)
512df930be7Sderaadt {
51390f6ca67Smatthew return (physio(fdstrategy, dev, B_WRITE, minphys, uio));
514df930be7Sderaadt }
515df930be7Sderaadt
516df930be7Sderaadt void
fd_set_motor(struct fdc_softc * fdc,int reset)5170e9d7eafSmatthew fd_set_motor(struct fdc_softc *fdc, int reset)
518df930be7Sderaadt {
519df930be7Sderaadt struct fd_softc *fd;
520df930be7Sderaadt u_char status;
521df930be7Sderaadt int n;
522df930be7Sderaadt
5239d08f8e5Smiod if ((fd = TAILQ_FIRST(&fdc->sc_link.fdlink.sc_drives)) != NULL)
524df930be7Sderaadt status = fd->sc_drive;
525df930be7Sderaadt else
526df930be7Sderaadt status = 0;
527df930be7Sderaadt if (!reset)
528df930be7Sderaadt status |= FDO_FRST | FDO_FDMAEN;
529df930be7Sderaadt for (n = 0; n < 4; n++)
5303af98d30Sdownsj if ((fd = fdc->sc_link.fdlink.sc_fd[n])
5313af98d30Sdownsj && (fd->sc_flags & FD_MOTOR))
532df930be7Sderaadt status |= FDO_MOEN(n);
533b9a85264Sniklas bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout, status);
534df930be7Sderaadt }
535df930be7Sderaadt
536df930be7Sderaadt void
fd_motor_off(void * arg)5370e9d7eafSmatthew fd_motor_off(void *arg)
538df930be7Sderaadt {
539df930be7Sderaadt struct fd_softc *fd = arg;
540df930be7Sderaadt int s;
541df930be7Sderaadt
542df930be7Sderaadt s = splbio();
543df930be7Sderaadt fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
544df930be7Sderaadt fd_set_motor((struct fdc_softc *)fd->sc_dev.dv_parent, 0);
545df930be7Sderaadt splx(s);
546df930be7Sderaadt }
547df930be7Sderaadt
548df930be7Sderaadt void
fd_motor_on(void * arg)5490e9d7eafSmatthew fd_motor_on(void *arg)
550df930be7Sderaadt {
551df930be7Sderaadt struct fd_softc *fd = arg;
552df930be7Sderaadt struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
553df930be7Sderaadt int s;
554df930be7Sderaadt
555df930be7Sderaadt s = splbio();
556df930be7Sderaadt fd->sc_flags &= ~FD_MOTOR_WAIT;
5579d08f8e5Smiod if ((TAILQ_FIRST(&fdc->sc_link.fdlink.sc_drives) == fd)
5583af98d30Sdownsj && (fdc->sc_state == MOTORWAIT))
5593041a0b9Sdownsj (void) fdintr(fdc);
560df930be7Sderaadt splx(s);
561df930be7Sderaadt }
562df930be7Sderaadt
563df930be7Sderaadt int
fdopen(dev_t dev,int flags,int fmt,struct proc * p)5640e9d7eafSmatthew fdopen(dev_t dev, int flags, int fmt, struct proc *p)
565df930be7Sderaadt {
566190f1464Skrw int unit, pmask;
567df930be7Sderaadt struct fd_softc *fd;
568df930be7Sderaadt struct fd_type *type;
569df930be7Sderaadt
570df930be7Sderaadt unit = FDUNIT(dev);
571d724e01aSderaadt if (unit >= fd_cd.cd_ndevs)
572df930be7Sderaadt return ENXIO;
573d724e01aSderaadt fd = fd_cd.cd_devs[unit];
574df930be7Sderaadt if (fd == 0)
575df930be7Sderaadt return ENXIO;
576df930be7Sderaadt type = fd_dev_to_type(fd, dev);
577df930be7Sderaadt if (type == NULL)
578df930be7Sderaadt return ENXIO;
579df930be7Sderaadt
580df930be7Sderaadt if ((fd->sc_flags & FD_OPEN) != 0 &&
581df930be7Sderaadt fd->sc_type != type)
582df930be7Sderaadt return EBUSY;
583df930be7Sderaadt
584df930be7Sderaadt fd->sc_type = type;
585df930be7Sderaadt fd->sc_cylin = -1;
586df930be7Sderaadt fd->sc_flags |= FD_OPEN;
587df930be7Sderaadt
588190f1464Skrw /*
589190f1464Skrw * Only update the disklabel if we're not open anywhere else.
590190f1464Skrw */
591190f1464Skrw if (fd->sc_dk.dk_openmask == 0)
592190f1464Skrw fdgetdisklabel(dev, fd, fd->sc_dk.dk_label, 0);
593190f1464Skrw
594190f1464Skrw pmask = (1 << FDPART(dev));
595190f1464Skrw
596190f1464Skrw switch (fmt) {
597190f1464Skrw case S_IFCHR:
598190f1464Skrw fd->sc_dk.dk_copenmask |= pmask;
599190f1464Skrw break;
600190f1464Skrw
601190f1464Skrw case S_IFBLK:
602190f1464Skrw fd->sc_dk.dk_bopenmask |= pmask;
603190f1464Skrw break;
604190f1464Skrw }
605190f1464Skrw fd->sc_dk.dk_openmask =
606190f1464Skrw fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
607190f1464Skrw
608df930be7Sderaadt return 0;
609df930be7Sderaadt }
610df930be7Sderaadt
611df930be7Sderaadt int
fdclose(dev_t dev,int flags,int fmt,struct proc * p)6120e9d7eafSmatthew fdclose(dev_t dev, int flags, int fmt, struct proc *p)
613df930be7Sderaadt {
614d724e01aSderaadt struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
615190f1464Skrw int pmask = (1 << FDPART(dev));
616df930be7Sderaadt
617df930be7Sderaadt fd->sc_flags &= ~FD_OPEN;
6181921243fSdownsj fd->sc_opts &= ~FDOPT_NORETRY;
619190f1464Skrw
620190f1464Skrw switch (fmt) {
621190f1464Skrw case S_IFCHR:
622190f1464Skrw fd->sc_dk.dk_copenmask &= ~pmask;
623190f1464Skrw break;
624190f1464Skrw
625190f1464Skrw case S_IFBLK:
626190f1464Skrw fd->sc_dk.dk_bopenmask &= ~pmask;
627190f1464Skrw break;
628190f1464Skrw }
629190f1464Skrw fd->sc_dk.dk_openmask =
630190f1464Skrw fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
631190f1464Skrw
632190f1464Skrw return (0);
633df930be7Sderaadt }
634df930be7Sderaadt
6351abdbfdeSderaadt daddr_t
fdsize(dev_t dev)6360e9d7eafSmatthew fdsize(dev_t dev)
637df930be7Sderaadt {
6383041a0b9Sdownsj /* Swapping to floppies would not make sense. */
6393041a0b9Sdownsj return -1;
640df930be7Sderaadt }
641df930be7Sderaadt
642df930be7Sderaadt int
fddump(dev_t dev,daddr_t blkno,caddr_t va,size_t size)6431abdbfdeSderaadt fddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
644df930be7Sderaadt {
6453041a0b9Sdownsj /* Not implemented. */
6463041a0b9Sdownsj return ENXIO;
6473041a0b9Sdownsj }
6483041a0b9Sdownsj
6493041a0b9Sdownsj /*
6503041a0b9Sdownsj * Called from the controller.
6513041a0b9Sdownsj */
6523041a0b9Sdownsj int
fdintr(struct fdc_softc * fdc)6530e9d7eafSmatthew fdintr(struct fdc_softc *fdc)
6543041a0b9Sdownsj {
655df930be7Sderaadt #define st0 fdc->sc_status[0]
656df930be7Sderaadt #define cyl fdc->sc_status[1]
657df930be7Sderaadt struct fd_softc *fd;
658df930be7Sderaadt struct buf *bp;
659b9a85264Sniklas bus_space_tag_t iot = fdc->sc_iot;
660b9a85264Sniklas bus_space_handle_t ioh = fdc->sc_ioh;
661a06af799Sdownsj bus_space_handle_t ioh_ctl = fdc->sc_ioh_ctl;
66227f37992Sdlg int read, head, sec, i, nblks, cylin;
663df930be7Sderaadt struct fd_type *type;
6641921243fSdownsj struct fd_formb *finfo = NULL;
665cfe658c7Skrw int fd_bsize;
666df930be7Sderaadt
667df930be7Sderaadt loop:
6683041a0b9Sdownsj /* Is there a transfer to this drive? If not, deactivate drive. */
6699d08f8e5Smiod fd = TAILQ_FIRST(&fdc->sc_link.fdlink.sc_drives);
670df930be7Sderaadt if (fd == NULL) {
671df930be7Sderaadt fdc->sc_state = DEVIDLE;
672df930be7Sderaadt return 1;
673df930be7Sderaadt }
674cfe658c7Skrw fd_bsize = FD_BSIZE(fd);
675df930be7Sderaadt
6768ea83772Sdlg bp = fd->sc_bp;
677df930be7Sderaadt if (bp == NULL) {
678df930be7Sderaadt fd->sc_ops = 0;
6793af98d30Sdownsj TAILQ_REMOVE(&fdc->sc_link.fdlink.sc_drives, fd, sc_drivechain);
680df930be7Sderaadt goto loop;
681df930be7Sderaadt }
682df930be7Sderaadt
6831921243fSdownsj if (bp->b_flags & B_FORMAT)
6841921243fSdownsj finfo = (struct fd_formb *)bp->b_data;
6851921243fSdownsj
68627f37992Sdlg cylin = ((bp->b_blkno * DEV_BSIZE) + (bp->b_bcount - bp->b_resid)) /
68727f37992Sdlg (fd_bsize * fd->sc_type->seccyl);
68827f37992Sdlg
689df930be7Sderaadt switch (fdc->sc_state) {
690df930be7Sderaadt case DEVIDLE:
691df930be7Sderaadt fdc->sc_errors = 0;
692df930be7Sderaadt fd->sc_skip = 0;
693df930be7Sderaadt fd->sc_bcount = bp->b_bcount;
69476c8686eSdownsj fd->sc_blkno = bp->b_blkno / (fd_bsize / DEV_BSIZE);
6957d33d8deSho timeout_del(&fd->fd_motor_off_to);
696df930be7Sderaadt if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
697df930be7Sderaadt fdc->sc_state = MOTORWAIT;
698df930be7Sderaadt return 1;
699df930be7Sderaadt }
700df930be7Sderaadt if ((fd->sc_flags & FD_MOTOR) == 0) {
701df930be7Sderaadt /* Turn on the motor, being careful about pairing. */
7023af98d30Sdownsj struct fd_softc *ofd =
7033af98d30Sdownsj fdc->sc_link.fdlink.sc_fd[fd->sc_drive ^ 1];
704df930be7Sderaadt if (ofd && ofd->sc_flags & FD_MOTOR) {
7057d33d8deSho timeout_del(&ofd->fd_motor_off_to);
706df930be7Sderaadt ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
707df930be7Sderaadt }
708df930be7Sderaadt fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
709df930be7Sderaadt fd_set_motor(fdc, 0);
710df930be7Sderaadt fdc->sc_state = MOTORWAIT;
711df930be7Sderaadt /* Allow .25s for motor to stabilize. */
71257f30c4fSblambert timeout_add_msec(&fd->fd_motor_on_to, 250);
713df930be7Sderaadt return 1;
714df930be7Sderaadt }
715df930be7Sderaadt /* Make sure the right drive is selected. */
716df930be7Sderaadt fd_set_motor(fdc, 0);
717df930be7Sderaadt
71883344e41Sjsg /* FALLTHROUGH */
719df930be7Sderaadt case DOSEEK:
720df930be7Sderaadt doseek:
72127f37992Sdlg if (fd->sc_cylin == cylin)
722df930be7Sderaadt goto doio;
723df930be7Sderaadt
724b9a85264Sniklas out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
725b9a85264Sniklas out_fdc(iot, ioh, fd->sc_type->steprate);
726b9a85264Sniklas out_fdc(iot, ioh, 6); /* XXX head load time == 6ms */
727df930be7Sderaadt
728b9a85264Sniklas out_fdc(iot, ioh, NE7CMD_SEEK); /* seek function */
729b9a85264Sniklas out_fdc(iot, ioh, fd->sc_drive); /* drive number */
73027f37992Sdlg out_fdc(iot, ioh, cylin * fd->sc_type->step);
731df930be7Sderaadt
732df930be7Sderaadt fd->sc_cylin = -1;
733df930be7Sderaadt fdc->sc_state = SEEKWAIT;
7341eb11dafSderaadt
7351eb11dafSderaadt fd->sc_dk.dk_seek++;
7361eb11dafSderaadt disk_busy(&fd->sc_dk);
7371eb11dafSderaadt
73829e86e5eSblambert timeout_add_sec(&fd->fdtimeout_to, 4);
739df930be7Sderaadt return 1;
740df930be7Sderaadt
741df930be7Sderaadt case DOIO:
742df930be7Sderaadt doio:
743df930be7Sderaadt type = fd->sc_type;
7441921243fSdownsj if (finfo)
7451921243fSdownsj fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
7461921243fSdownsj (char *)finfo;
747df930be7Sderaadt sec = fd->sc_blkno % type->seccyl;
748df930be7Sderaadt nblks = type->seccyl - sec;
74976c8686eSdownsj nblks = min(nblks, fd->sc_bcount / fd_bsize);
75076c8686eSdownsj nblks = min(nblks, FDC_MAXIOSIZE / fd_bsize);
751df930be7Sderaadt fd->sc_nblks = nblks;
75276c8686eSdownsj fd->sc_nbytes = finfo ? bp->b_bcount : nblks * fd_bsize;
753df930be7Sderaadt head = sec / type->sectrac;
754df930be7Sderaadt sec -= head * type->sectrac;
755df930be7Sderaadt #ifdef DIAGNOSTIC
756df930be7Sderaadt {int block;
757df930be7Sderaadt block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
758df930be7Sderaadt if (block != fd->sc_blkno) {
759bd8c82ffSjasper panic("fdintr: block %d != blkno %llu", block, fd->sc_blkno);
760df930be7Sderaadt }}
761df930be7Sderaadt #endif
762fa7cc844Sniklas read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE;
763aee4fbd6Sniklas isadma_start(bp->b_data + fd->sc_skip, fd->sc_nbytes,
764aee4fbd6Sniklas fdc->sc_drq, read);
765a06af799Sdownsj bus_space_write_1(iot, ioh_ctl, fdctl, type->rate);
766df930be7Sderaadt #ifdef FD_DEBUG
7673041a0b9Sdownsj printf("fdintr: %s drive %d track %d head %d sec %d nblks %d\n",
768df930be7Sderaadt read ? "read" : "write", fd->sc_drive, fd->sc_cylin, head,
769df930be7Sderaadt sec, nblks);
770df930be7Sderaadt #endif
7711921243fSdownsj if (finfo) {
7721921243fSdownsj /* formatting */
773b9a85264Sniklas if (out_fdc(iot, ioh, NE7CMD_FORMAT) < 0) {
7741921243fSdownsj fdc->sc_errors = 4;
7753041a0b9Sdownsj fdretry(fd);
7761921243fSdownsj goto loop;
7771921243fSdownsj }
778b9a85264Sniklas out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
779b9a85264Sniklas out_fdc(iot, ioh, finfo->fd_formb_secshift);
780b9a85264Sniklas out_fdc(iot, ioh, finfo->fd_formb_nsecs);
781b9a85264Sniklas out_fdc(iot, ioh, finfo->fd_formb_gaplen);
782b9a85264Sniklas out_fdc(iot, ioh, finfo->fd_formb_fillbyte);
7831921243fSdownsj } else {
784df930be7Sderaadt if (read)
785b9a85264Sniklas out_fdc(iot, ioh, NE7CMD_READ); /* READ */
786df930be7Sderaadt else
787b9a85264Sniklas out_fdc(iot, ioh, NE7CMD_WRITE);/* WRITE */
788b9a85264Sniklas out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
789b9a85264Sniklas out_fdc(iot, ioh, fd->sc_cylin); /* track */
790b9a85264Sniklas out_fdc(iot, ioh, head);
791b9a85264Sniklas out_fdc(iot, ioh, sec + 1); /* sec +1 */
792b9a85264Sniklas out_fdc(iot, ioh, type->secsize); /* sec size */
793b9a85264Sniklas out_fdc(iot, ioh, type->sectrac); /* secs/track */
794b9a85264Sniklas out_fdc(iot, ioh, type->gap1); /* gap1 size */
795b9a85264Sniklas out_fdc(iot, ioh, type->datalen); /* data len */
7961921243fSdownsj }
797df930be7Sderaadt fdc->sc_state = IOCOMPLETE;
7981eb11dafSderaadt
7991eb11dafSderaadt disk_busy(&fd->sc_dk);
8001eb11dafSderaadt
801df930be7Sderaadt /* allow 2 seconds for operation */
80229e86e5eSblambert timeout_add_sec(&fd->fdtimeout_to, 2);
803df930be7Sderaadt return 1; /* will return later */
804df930be7Sderaadt
805df930be7Sderaadt case SEEKWAIT:
8067d33d8deSho timeout_del(&fd->fdtimeout_to);
807df930be7Sderaadt fdc->sc_state = SEEKCOMPLETE;
808df930be7Sderaadt /* allow 1/50 second for heads to settle */
80957f30c4fSblambert timeout_add_msec(&fdc->fdcpseudointr_to, 20);
810df930be7Sderaadt return 1;
811df930be7Sderaadt
812df930be7Sderaadt case SEEKCOMPLETE:
813d40269afSderaadt disk_unbusy(&fd->sc_dk, 0, 0, 0); /* no data on seek */
8141eb11dafSderaadt
815df930be7Sderaadt /* Make sure seek really happened. */
816b9a85264Sniklas out_fdc(iot, ioh, NE7CMD_SENSEI);
817df930be7Sderaadt if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
81827f37992Sdlg cyl != cylin * fd->sc_type->step) {
819df930be7Sderaadt #ifdef FD_DEBUG
820df930be7Sderaadt fdcstatus(&fd->sc_dev, 2, "seek failed");
821df930be7Sderaadt #endif
8223041a0b9Sdownsj fdretry(fd);
823df930be7Sderaadt goto loop;
824df930be7Sderaadt }
82527f37992Sdlg fd->sc_cylin = cylin;
826df930be7Sderaadt goto doio;
827df930be7Sderaadt
828df930be7Sderaadt case IOTIMEDOUT:
829aee4fbd6Sniklas isadma_abort(fdc->sc_drq);
830df930be7Sderaadt case SEEKTIMEDOUT:
831df930be7Sderaadt case RECALTIMEDOUT:
832df930be7Sderaadt case RESETTIMEDOUT:
8333041a0b9Sdownsj fdretry(fd);
834df930be7Sderaadt goto loop;
835df930be7Sderaadt
836df930be7Sderaadt case IOCOMPLETE: /* IO DONE, post-analyze */
8377d33d8deSho timeout_del(&fd->fdtimeout_to);
8381eb11dafSderaadt
83935fa4c39Stedu disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
840d40269afSderaadt fd->sc_blkno, (bp->b_flags & B_READ));
8411eb11dafSderaadt
842df930be7Sderaadt if (fdcresult(fdc) != 7 || (st0 & 0xf8) != 0) {
843aee4fbd6Sniklas isadma_abort(fdc->sc_drq);
844df930be7Sderaadt #ifdef FD_DEBUG
845df930be7Sderaadt fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
846df930be7Sderaadt "read failed" : "write failed");
8478234ed2eSjasper printf("blkno %lld nblks %d\n",
848bb4f4faeSkrw (long long)fd->sc_blkno, fd->sc_nblks);
849df930be7Sderaadt #endif
8503041a0b9Sdownsj fdretry(fd);
851df930be7Sderaadt goto loop;
852df930be7Sderaadt }
853fa7cc844Sniklas read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE;
854aee4fbd6Sniklas isadma_done(fdc->sc_drq);
855df930be7Sderaadt if (fdc->sc_errors) {
856df930be7Sderaadt diskerr(bp, "fd", "soft error", LOG_PRINTF,
85776c8686eSdownsj fd->sc_skip / fd_bsize, (struct disklabel *)NULL);
858df930be7Sderaadt printf("\n");
859df930be7Sderaadt fdc->sc_errors = 0;
860df930be7Sderaadt }
86127f37992Sdlg
862df930be7Sderaadt fd->sc_blkno += fd->sc_nblks;
863df930be7Sderaadt fd->sc_skip += fd->sc_nbytes;
864df930be7Sderaadt fd->sc_bcount -= fd->sc_nbytes;
86527f37992Sdlg bp->b_resid -= fd->sc_nbytes;
8661921243fSdownsj if (!finfo && fd->sc_bcount > 0) {
86727f37992Sdlg cylin = fd->sc_blkno / fd->sc_type->seccyl;
868df930be7Sderaadt goto doseek;
869df930be7Sderaadt }
870df930be7Sderaadt fdfinish(fd, bp);
871df930be7Sderaadt goto loop;
872df930be7Sderaadt
873df930be7Sderaadt case DORESET:
874df930be7Sderaadt /* try a reset, keep motor on */
875df930be7Sderaadt fd_set_motor(fdc, 1);
876df930be7Sderaadt delay(100);
877df930be7Sderaadt fd_set_motor(fdc, 0);
878df930be7Sderaadt fdc->sc_state = RESETCOMPLETE;
87957f30c4fSblambert timeout_add_msec(&fd->fdtimeout_to, 500);
880df930be7Sderaadt return 1; /* will return later */
881df930be7Sderaadt
882df930be7Sderaadt case RESETCOMPLETE:
8837d33d8deSho timeout_del(&fd->fdtimeout_to);
884df930be7Sderaadt /* clear the controller output buffer */
885df930be7Sderaadt for (i = 0; i < 4; i++) {
886b9a85264Sniklas out_fdc(iot, ioh, NE7CMD_SENSEI);
887df930be7Sderaadt (void) fdcresult(fdc);
888df930be7Sderaadt }
889df930be7Sderaadt
89083344e41Sjsg /* FALLTHROUGH */
891df930be7Sderaadt case DORECAL:
892b9a85264Sniklas out_fdc(iot, ioh, NE7CMD_RECAL); /* recal function */
893b9a85264Sniklas out_fdc(iot, ioh, fd->sc_drive);
894df930be7Sderaadt fdc->sc_state = RECALWAIT;
89529e86e5eSblambert timeout_add_sec(&fd->fdtimeout_to, 5);
896df930be7Sderaadt return 1; /* will return later */
897df930be7Sderaadt
898df930be7Sderaadt case RECALWAIT:
8997d33d8deSho timeout_del(&fd->fdtimeout_to);
900df930be7Sderaadt fdc->sc_state = RECALCOMPLETE;
901df930be7Sderaadt /* allow 1/30 second for heads to settle */
90211969f56Scheloha timeout_add_msec(&fdc->fdcpseudointr_to, 1000 / 30);
903df930be7Sderaadt return 1; /* will return later */
904df930be7Sderaadt
905df930be7Sderaadt case RECALCOMPLETE:
906b9a85264Sniklas out_fdc(iot, ioh, NE7CMD_SENSEI);
907df930be7Sderaadt if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
908df930be7Sderaadt #ifdef FD_DEBUG
909df930be7Sderaadt fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
910df930be7Sderaadt #endif
9113041a0b9Sdownsj fdretry(fd);
912df930be7Sderaadt goto loop;
913df930be7Sderaadt }
914df930be7Sderaadt fd->sc_cylin = 0;
915df930be7Sderaadt goto doseek;
916df930be7Sderaadt
917df930be7Sderaadt case MOTORWAIT:
918df930be7Sderaadt if (fd->sc_flags & FD_MOTOR_WAIT)
919df930be7Sderaadt return 1; /* time's not up yet */
920df930be7Sderaadt goto doseek;
921df930be7Sderaadt
922df930be7Sderaadt default:
923df930be7Sderaadt fdcstatus(&fd->sc_dev, 0, "stray interrupt");
924df930be7Sderaadt return 1;
925df930be7Sderaadt }
926df930be7Sderaadt #ifdef DIAGNOSTIC
9273041a0b9Sdownsj panic("fdintr: impossible");
928df930be7Sderaadt #endif
929df930be7Sderaadt #undef st0
930df930be7Sderaadt #undef cyl
931df930be7Sderaadt }
932df930be7Sderaadt
933df930be7Sderaadt void
fdtimeout(void * arg)9340e9d7eafSmatthew fdtimeout(void *arg)
935df930be7Sderaadt {
9363041a0b9Sdownsj struct fd_softc *fd = arg;
9373041a0b9Sdownsj struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
9383041a0b9Sdownsj int s;
939df930be7Sderaadt
9403041a0b9Sdownsj s = splbio();
9413041a0b9Sdownsj #ifdef DEBUG
9423041a0b9Sdownsj log(LOG_ERR,"fdtimeout: state %d\n", fdc->sc_state);
9433041a0b9Sdownsj #endif
9443041a0b9Sdownsj fdcstatus(&fd->sc_dev, 0, "timeout");
9453041a0b9Sdownsj
9468ea83772Sdlg if (fd->sc_bp != NULL)
9473041a0b9Sdownsj fdc->sc_state++;
9483041a0b9Sdownsj else
9493041a0b9Sdownsj fdc->sc_state = DEVIDLE;
9503041a0b9Sdownsj
9513041a0b9Sdownsj (void) fdintr(fdc);
9523041a0b9Sdownsj splx(s);
9533041a0b9Sdownsj }
9543041a0b9Sdownsj
9553041a0b9Sdownsj void
fdretry(struct fd_softc * fd)9560e9d7eafSmatthew fdretry(struct fd_softc *fd)
9573041a0b9Sdownsj {
9583041a0b9Sdownsj struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
9598ea83772Sdlg struct buf *bp = fd->sc_bp;
960df930be7Sderaadt
9611921243fSdownsj if (fd->sc_opts & FDOPT_NORETRY)
9621921243fSdownsj goto fail;
963df930be7Sderaadt switch (fdc->sc_errors) {
964df930be7Sderaadt case 0:
965df930be7Sderaadt /* try again */
966da3b7780Shannken fdc->sc_state = DOSEEK;
967df930be7Sderaadt break;
968df930be7Sderaadt
969df930be7Sderaadt case 1: case 2: case 3:
970df930be7Sderaadt /* didn't work; try recalibrating */
971df930be7Sderaadt fdc->sc_state = DORECAL;
972df930be7Sderaadt break;
973df930be7Sderaadt
974df930be7Sderaadt case 4:
975df930be7Sderaadt /* still no go; reset the bastard */
976df930be7Sderaadt fdc->sc_state = DORESET;
977df930be7Sderaadt break;
978df930be7Sderaadt
979df930be7Sderaadt default:
9801921243fSdownsj fail:
981df930be7Sderaadt diskerr(bp, "fd", "hard error", LOG_PRINTF,
982cfe658c7Skrw fd->sc_skip / FD_BSIZE(fd), (struct disklabel *)NULL);
983df930be7Sderaadt printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
984df930be7Sderaadt fdc->sc_status[0], NE7_ST0BITS,
985df930be7Sderaadt fdc->sc_status[1], NE7_ST1BITS,
986df930be7Sderaadt fdc->sc_status[2], NE7_ST2BITS,
987df930be7Sderaadt fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
988df930be7Sderaadt
989df930be7Sderaadt bp->b_flags |= B_ERROR;
990df930be7Sderaadt bp->b_error = EIO;
99127f37992Sdlg bp->b_resid = bp->b_bcount;
992df930be7Sderaadt fdfinish(fd, bp);
993df930be7Sderaadt }
994df930be7Sderaadt fdc->sc_errors++;
995df930be7Sderaadt }
996df930be7Sderaadt
997df930be7Sderaadt int
fdioctl(dev_t dev,u_long cmd,caddr_t addr,int flag,struct proc * p)9980e9d7eafSmatthew fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
999df930be7Sderaadt {
1000d724e01aSderaadt struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
1001a08af95aSkrw struct disklabel *lp;
1002df930be7Sderaadt int error;
1003df930be7Sderaadt
1004df930be7Sderaadt switch (cmd) {
10054b64fdbfSderaadt case MTIOCTOP:
10064b64fdbfSderaadt if (((struct mtop *)addr)->mt_op != MTOFFL)
10074b64fdbfSderaadt return EIO;
10084b64fdbfSderaadt return (0);
1009e6586b6fSkrw
1010e6586b6fSkrw case DIOCRLDINFO:
1011e6586b6fSkrw lp = malloc(sizeof(*lp), M_TEMP, M_WAITOK);
1012190f1464Skrw fdgetdisklabel(dev, fd, lp, 0);
1013e6586b6fSkrw bcopy(lp, fd->sc_dk.dk_label, sizeof(*lp));
101497818786Stedu free(lp, M_TEMP, sizeof(*lp));
1015e6586b6fSkrw return 0;
1016e6586b6fSkrw
1017e6586b6fSkrw case DIOCGPDINFO:
1018e6586b6fSkrw fdgetdisklabel(dev, fd, (struct disklabel *)addr, 1);
1019e6586b6fSkrw return 0;
1020e6586b6fSkrw
1021e6586b6fSkrw case DIOCGDINFO:
1022e6586b6fSkrw *(struct disklabel *)addr = *(fd->sc_dk.dk_label);
1023e6586b6fSkrw return 0;
1024e6586b6fSkrw
1025e6586b6fSkrw case DIOCGPART:
1026e6586b6fSkrw ((struct partinfo *)addr)->disklab = fd->sc_dk.dk_label;
1027e6586b6fSkrw ((struct partinfo *)addr)->part =
1028e6586b6fSkrw &fd->sc_dk.dk_label->d_partitions[FDPART(dev)];
1029df930be7Sderaadt return 0;
1030df930be7Sderaadt
1031df930be7Sderaadt case DIOCWDINFO:
10322e4cb731Skrw case DIOCSDINFO:
1033df930be7Sderaadt if ((flag & FWRITE) == 0)
1034df930be7Sderaadt return EBADF;
1035df930be7Sderaadt
10362e4cb731Skrw error = setdisklabel(fd->sc_dk.dk_label,
10372e4cb731Skrw (struct disklabel *)addr, 0);
10382e4cb731Skrw if (error == 0) {
10392e4cb731Skrw if (cmd == DIOCWDINFO)
10402e4cb731Skrw error = writedisklabel(DISKLABELDEV(dev),
10412e4cb731Skrw fdstrategy, fd->sc_dk.dk_label);
10422e4cb731Skrw }
1043df930be7Sderaadt return error;
1044df930be7Sderaadt
10451921243fSdownsj case FD_FORM:
10461921243fSdownsj if((flag & FWRITE) == 0)
10471921243fSdownsj return EBADF; /* must be opened for writing */
10481921243fSdownsj else if(((struct fd_formb *)addr)->format_version !=
10491921243fSdownsj FD_FORMAT_VERSION)
10501921243fSdownsj return EINVAL; /* wrong version of formatting prog */
10511921243fSdownsj else
10521921243fSdownsj return fdformat(dev, (struct fd_formb *)addr, p);
10531921243fSdownsj break;
10541921243fSdownsj
10551921243fSdownsj case FD_GTYPE: /* get drive type */
10561921243fSdownsj *(struct fd_type *)addr = *fd->sc_type;
10571921243fSdownsj return 0;
10581921243fSdownsj
10591921243fSdownsj case FD_GOPTS: /* get drive options */
10601921243fSdownsj *(int *)addr = fd->sc_opts;
10611921243fSdownsj return 0;
10621921243fSdownsj
10631921243fSdownsj case FD_SOPTS: /* set drive options */
10641921243fSdownsj fd->sc_opts = *(int *)addr;
10651921243fSdownsj return 0;
10661921243fSdownsj
1067df930be7Sderaadt default:
1068df930be7Sderaadt return ENOTTY;
1069df930be7Sderaadt }
1070df930be7Sderaadt
1071df930be7Sderaadt #ifdef DIAGNOSTIC
1072df930be7Sderaadt panic("fdioctl: impossible");
1073df930be7Sderaadt #endif
1074df930be7Sderaadt }
10751921243fSdownsj
10761921243fSdownsj int
fdformat(dev_t dev,struct fd_formb * finfo,struct proc * p)10770e9d7eafSmatthew fdformat(dev_t dev, struct fd_formb *finfo, struct proc *p)
10781921243fSdownsj {
1079fed3d83dSmiod int rv = 0;
10801921243fSdownsj struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
10811921243fSdownsj struct fd_type *type = fd->sc_type;
10821921243fSdownsj struct buf *bp;
1083cfe658c7Skrw int fd_bsize = FD_BSIZE(fd);
10841921243fSdownsj
10851921243fSdownsj /* set up a buffer header for fdstrategy() */
10862b585ff0Skrw bp = malloc(sizeof(*bp), M_TEMP, M_NOWAIT | M_ZERO);
1087fed3d83dSmiod if (bp == NULL)
10881921243fSdownsj return ENOBUFS;
1089fed3d83dSmiod
1090c10f2b41Sderaadt bp->b_flags = B_BUSY | B_PHYS | B_FORMAT | B_RAW;
10911921243fSdownsj bp->b_proc = p;
10921921243fSdownsj bp->b_dev = dev;
10931921243fSdownsj
10941921243fSdownsj /*
10951921243fSdownsj * calculate a fake blkno, so fdstrategy() would initiate a
10961921243fSdownsj * seek to the requested cylinder
10971921243fSdownsj */
10981921243fSdownsj bp->b_blkno = (finfo->cyl * (type->sectrac * type->heads)
109976c8686eSdownsj + finfo->head * type->sectrac) * fd_bsize / DEV_BSIZE;
11001921243fSdownsj
11011921243fSdownsj bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
11021921243fSdownsj bp->b_data = (caddr_t)finfo;
11031921243fSdownsj
11041921243fSdownsj #ifdef DEBUG
11052f436acaSsf printf("fdformat: blkno %llx count %lx\n", bp->b_blkno, bp->b_bcount);
11061921243fSdownsj #endif
11071921243fSdownsj
11081921243fSdownsj /* now do the format */
11091921243fSdownsj fdstrategy(bp);
11101921243fSdownsj
11111921243fSdownsj /* ...and wait for it to complete */
1112fed3d83dSmiod rv = biowait(bp);
111397818786Stedu free(bp, M_TEMP, sizeof(*bp));
1114fed3d83dSmiod return (rv);
11151921243fSdownsj }
1116