Line data Source code
1 : /* $OpenBSD: fd.c,v 1.106 2017/12/30 23:08:29 guenther Exp $ */
2 : /* $NetBSD: fd.c,v 1.90 1996/05/12 23:12:03 mycroft Exp $ */
3 :
4 : /*-
5 : * Copyright (c) 1993, 1994, 1995, 1996 Charles Hannum.
6 : * Copyright (c) 1990 The Regents of the University of California.
7 : * All rights reserved.
8 : *
9 : * This code is derived from software contributed to Berkeley by
10 : * Don Ahn.
11 : *
12 : * Portions Copyright (c) 1993, 1994 by
13 : * jc@irbs.UUCP (John Capo)
14 : * vak@zebub.msk.su (Serge Vakulenko)
15 : * ache@astral.msk.su (Andrew A. Chernov)
16 : * joerg_wunsch@uriah.sax.de (Joerg Wunsch)
17 : *
18 : * Redistribution and use in source and binary forms, with or without
19 : * modification, are permitted provided that the following conditions
20 : * are met:
21 : * 1. Redistributions of source code must retain the above copyright
22 : * notice, this list of conditions and the following disclaimer.
23 : * 2. Redistributions in binary form must reproduce the above copyright
24 : * notice, this list of conditions and the following disclaimer in the
25 : * documentation and/or other materials provided with the distribution.
26 : * 3. Neither the name of the University nor the names of its contributors
27 : * may be used to endorse or promote products derived from this software
28 : * without specific prior written permission.
29 : *
30 : * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 : * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 : * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 : * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 : * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 : * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 : * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 : * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 : * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 : * SUCH DAMAGE.
41 : *
42 : * @(#)fd.c 7.4 (Berkeley) 5/25/91
43 : */
44 :
45 : #include <sys/param.h>
46 : #include <sys/systm.h>
47 : #include <sys/kernel.h>
48 : #include <sys/fcntl.h>
49 : #include <sys/ioctl.h>
50 : #include <sys/device.h>
51 : #include <sys/disklabel.h>
52 : #include <sys/disk.h>
53 : #include <sys/buf.h>
54 : #include <sys/malloc.h>
55 : #include <sys/uio.h>
56 : #include <sys/mtio.h>
57 : #include <sys/syslog.h>
58 : #include <sys/queue.h>
59 : #include <sys/stat.h>
60 : #include <sys/timeout.h>
61 : #include <sys/dkio.h>
62 :
63 : #include <machine/cpu.h>
64 : #include <machine/bus.h>
65 : #include <machine/intr.h>
66 : #include <machine/ioctl_fd.h>
67 :
68 : #include <dev/isa/isavar.h>
69 : #include <dev/isa/isadmavar.h>
70 : #include <dev/isa/fdreg.h>
71 :
72 : #if defined(__i386__) || defined(__amd64__) /* XXX */
73 : #include <i386/isa/nvram.h>
74 : #endif
75 :
76 : #include <dev/isa/fdlink.h>
77 :
78 : /* XXX misuse a flag to identify format operation */
79 : #define B_FORMAT B_XXX
80 :
81 : /* fd_type struct now in ioctl_fd.h */
82 :
83 : /* The order of entries in the following table is important -- BEWARE! */
84 : struct fd_type fd_types[] = {
85 : { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB" }, /* 1.44MB diskette */
86 : { 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS, "1.2MB" }, /* 1.2 MB AT-diskettes */
87 : { 9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS, "360KB/AT" }, /* 360kB in 1.2MB drive */
88 : { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS, "360KB/PC" }, /* 360kB PC diskettes */
89 : { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS, "720KB" }, /* 3.5" 720kB diskette */
90 : { 9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS, "720KB/x" }, /* 720kB in 1.2MB drive */
91 : { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS, "360KB/x" }, /* 360kB in 720kB drive */
92 : { 36,2,72,2,0xff,0xaf,0x1b,0x54,80,5760,1,FDC_500KBPS,"2.88MB" }, /* 2.88MB diskette */
93 : { 8,2,16,3,0xff,0xdf,0x35,0x74,77,1232,1,FDC_500KBPS,"1.2MB/[1024bytes/sector]" } /* 1.2 MB japanese format */
94 : };
95 :
96 : /* software state, per disk (with up to 4 disks per ctlr) */
97 : struct fd_softc {
98 : struct device sc_dev;
99 : struct disk sc_dk;
100 :
101 : struct fd_type *sc_deftype; /* default type descriptor */
102 : struct fd_type *sc_type; /* current type descriptor */
103 :
104 : daddr_t sc_blkno; /* starting block number */
105 : int sc_bcount; /* byte count left */
106 : int sc_opts; /* user-set options */
107 : int sc_skip; /* bytes already transferred */
108 : int sc_nblks; /* number of blocks currently transferring */
109 : int sc_nbytes; /* number of bytes currently transferring */
110 :
111 : int sc_drive; /* physical unit number */
112 : int sc_flags;
113 : #define FD_OPEN 0x01 /* it's open */
114 : #define FD_MOTOR 0x02 /* motor should be on */
115 : #define FD_MOTOR_WAIT 0x04 /* motor coming up */
116 : int sc_cylin; /* where we think the head is */
117 :
118 : TAILQ_ENTRY(fd_softc) sc_drivechain;
119 : int sc_ops; /* I/O ops since last switch */
120 : struct bufq sc_bufq; /* pending I/O */
121 : struct buf *sc_bp; /* the current I/O */
122 : struct timeout fd_motor_on_to;
123 : struct timeout fd_motor_off_to;
124 : struct timeout fdtimeout_to;
125 : };
126 :
127 : /* floppy driver configuration */
128 : int fdprobe(struct device *, void *, void *);
129 : void fdattach(struct device *, struct device *, void *);
130 : int fdactivate(struct device *, int);
131 :
132 : struct cfattach fd_ca = {
133 : sizeof(struct fd_softc), fdprobe, fdattach, NULL, fdactivate
134 : };
135 :
136 : struct cfdriver fd_cd = {
137 : NULL, "fd", DV_DISK
138 : };
139 :
140 : int fdgetdisklabel(dev_t, struct fd_softc *, struct disklabel *, int);
141 : int fd_get_parms(struct fd_softc *);
142 : void fdstrategy(struct buf *);
143 : void fdstart(struct fd_softc *);
144 : int fdintr(struct fdc_softc *);
145 :
146 : void fd_set_motor(struct fdc_softc *fdc, int reset);
147 : void fd_motor_off(void *arg);
148 : void fd_motor_on(void *arg);
149 : void fdfinish(struct fd_softc *fd, struct buf *bp);
150 : int fdformat(dev_t, struct fd_formb *, struct proc *);
151 : static __inline struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
152 : void fdretry(struct fd_softc *);
153 : void fdtimeout(void *);
154 :
155 : int
156 0 : fdgetdisklabel(dev_t dev, struct fd_softc *fd, struct disklabel *lp,
157 : int spoofonly)
158 : {
159 0 : bzero(lp, sizeof(struct disklabel));
160 :
161 0 : lp->d_type = DTYPE_FLOPPY;
162 0 : lp->d_secsize = FD_BSIZE(fd);
163 0 : lp->d_secpercyl = fd->sc_type->seccyl;
164 0 : lp->d_nsectors = fd->sc_type->sectrac;
165 0 : lp->d_ncylinders = fd->sc_type->tracks;
166 0 : lp->d_ntracks = fd->sc_type->heads; /* Go figure... */
167 0 : DL_SETDSIZE(lp, fd->sc_type->size);
168 :
169 0 : strncpy(lp->d_typename, "floppy disk", sizeof(lp->d_typename));
170 0 : strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
171 0 : lp->d_version = 1;
172 :
173 0 : lp->d_magic = DISKMAGIC;
174 0 : lp->d_magic2 = DISKMAGIC;
175 0 : lp->d_checksum = dkcksum(lp);
176 :
177 : /*
178 : * Call the generic disklabel extraction routine. If there's
179 : * not a label there, fake it.
180 : */
181 0 : return readdisklabel(DISKLABELDEV(dev), fdstrategy, lp, spoofonly);
182 : }
183 :
184 : int
185 0 : fdprobe(struct device *parent, void *match, void *aux)
186 : {
187 0 : struct fdc_softc *fdc = (void *)parent;
188 0 : struct cfdata *cf = match;
189 0 : struct fdc_attach_args *fa = aux;
190 0 : int drive = fa->fa_drive;
191 0 : bus_space_tag_t iot = fdc->sc_iot;
192 0 : bus_space_handle_t ioh = fdc->sc_ioh;
193 : int n;
194 :
195 0 : if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != drive)
196 0 : return 0;
197 : /*
198 : * XXX
199 : * This is to work around some odd interactions between this driver
200 : * and SMC Ethernet cards.
201 : */
202 0 : if (cf->cf_loc[0] == -1 && drive >= 2)
203 0 : return 0;
204 :
205 : /*
206 : * We want to keep the flags config gave us.
207 : */
208 0 : fa->fa_flags = cf->cf_flags;
209 :
210 : /* select drive and turn on motor */
211 0 : bus_space_write_1(iot, ioh, fdout, drive | FDO_FRST | FDO_MOEN(drive));
212 : /* wait for motor to spin up */
213 0 : tsleep(fdc, 0, "fdprobe", 250 * hz / 1000);
214 0 : out_fdc(iot, ioh, NE7CMD_RECAL);
215 0 : out_fdc(iot, ioh, drive);
216 : /* wait for recalibrate */
217 0 : tsleep(fdc, 0, "fdprobe", 2000 * hz / 1000);
218 0 : out_fdc(iot, ioh, NE7CMD_SENSEI);
219 0 : n = fdcresult(fdc);
220 : #ifdef FD_DEBUG
221 : {
222 : int i;
223 : printf("fdprobe: status");
224 : for (i = 0; i < n; i++)
225 : printf(" %x", fdc->sc_status[i]);
226 : printf("\n");
227 : }
228 : #endif
229 :
230 : /* turn off motor */
231 0 : tsleep(fdc, 0, "fdprobe", 250 * hz / 1000);
232 0 : bus_space_write_1(iot, ioh, fdout, FDO_FRST);
233 :
234 : /* flags & 0x20 forces the drive to be found even if it won't probe */
235 0 : if (!(fa->fa_flags & 0x20) && (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20))
236 0 : return 0;
237 :
238 0 : return 1;
239 0 : }
240 :
241 : /*
242 : * Controller is working, and drive responded. Attach it.
243 : */
244 : void
245 0 : fdattach(struct device *parent, struct device *self, void *aux)
246 : {
247 0 : struct fdc_softc *fdc = (void *)parent;
248 0 : struct fd_softc *fd = (void *)self;
249 0 : struct fdc_attach_args *fa = aux;
250 0 : struct fd_type *type = fa->fa_deftype;
251 0 : int drive = fa->fa_drive;
252 :
253 0 : if (!type || (fa->fa_flags & 0x10)) {
254 : /* The config has overridden this. */
255 0 : switch (fa->fa_flags & 0x07) {
256 : case 1: /* 2.88MB */
257 : type = &fd_types[7];
258 0 : break;
259 : case 2: /* 1.44MB */
260 : type = &fd_types[0];
261 0 : break;
262 : case 3: /* 1.2MB */
263 : type = &fd_types[1];
264 0 : break;
265 : case 4: /* 720K */
266 : type = &fd_types[4];
267 0 : break;
268 : case 5: /* 360K */
269 : type = &fd_types[3];
270 0 : break;
271 : case 6: /* 1.2 MB japanese format */
272 : type = &fd_types[8];
273 0 : break;
274 : #ifdef __alpha__
275 : default:
276 : /* 1.44MB, how to detect others?
277 : * idea from NetBSD -- jay@rootaction.net
278 : */
279 : type = &fd_types[0];
280 : #endif
281 : }
282 : }
283 :
284 0 : if (type)
285 0 : printf(": %s %d cyl, %d head, %d sec\n", type->name,
286 0 : type->tracks, type->heads, type->sectrac);
287 : else
288 0 : printf(": density unknown\n");
289 :
290 0 : fd->sc_cylin = -1;
291 0 : fd->sc_drive = drive;
292 0 : fd->sc_deftype = type;
293 0 : fdc->sc_type[drive] = FDC_TYPE_DISK;
294 0 : fdc->sc_link.fdlink.sc_fd[drive] = fd;
295 :
296 : /*
297 : * Initialize and attach the disk structure.
298 : */
299 0 : fd->sc_dk.dk_flags = DKF_NOLABELREAD;
300 0 : fd->sc_dk.dk_name = fd->sc_dev.dv_xname;
301 0 : bufq_init(&fd->sc_bufq, BUFQ_DEFAULT);
302 0 : disk_attach(&fd->sc_dev, &fd->sc_dk);
303 :
304 : /* Setup timeout structures */
305 0 : timeout_set(&fd->fd_motor_on_to, fd_motor_on, fd);
306 0 : timeout_set(&fd->fd_motor_off_to, fd_motor_off, fd);
307 0 : timeout_set(&fd->fdtimeout_to, fdtimeout, fd);
308 0 : }
309 :
310 : int
311 0 : fdactivate(struct device *self, int act)
312 : {
313 0 : struct fd_softc *fd = (void *)self;
314 0 : struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
315 : int rv = 0;
316 :
317 0 : switch (act) {
318 : case DVACT_SUSPEND:
319 0 : if (fdc->sc_state != DEVIDLE) {
320 0 : timeout_del(&fd->fd_motor_on_to);
321 0 : timeout_del(&fd->fd_motor_off_to);
322 0 : timeout_del(&fd->fdtimeout_to);
323 0 : fdc->sc_state = IOTIMEDOUT;
324 0 : fdc->sc_errors = 4;
325 0 : }
326 : break;
327 : case DVACT_POWERDOWN:
328 0 : fd_motor_off(self);
329 0 : break;
330 : }
331 :
332 0 : return (rv);
333 : }
334 :
335 : /*
336 : * Translate nvram type into internal data structure. Return NULL for
337 : * none/unknown/unusable.
338 : */
339 : struct fd_type *
340 0 : fd_nvtotype(char *fdc, int nvraminfo, int drive)
341 : {
342 : #ifdef __alpha__
343 : /* Alpha: assume 1.44MB, idea from NetBSD sys/dev/isa/fd.c
344 : * -- jay@rootaction.net
345 : */
346 : return &fd_types[0]; /* 1.44MB */
347 : #else
348 : int type;
349 :
350 0 : type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
351 0 : switch (type) {
352 : case NVRAM_DISKETTE_NONE:
353 0 : return NULL;
354 : case NVRAM_DISKETTE_12M:
355 0 : return &fd_types[1];
356 : case NVRAM_DISKETTE_TYPE5:
357 : case NVRAM_DISKETTE_TYPE6:
358 0 : return &fd_types[7];
359 : case NVRAM_DISKETTE_144M:
360 0 : return &fd_types[0];
361 : case NVRAM_DISKETTE_360K:
362 0 : return &fd_types[3];
363 : case NVRAM_DISKETTE_720K:
364 0 : return &fd_types[4];
365 : default:
366 0 : printf("%s: drive %d: unknown device type 0x%x\n",
367 : fdc, drive, type);
368 0 : return NULL;
369 : }
370 : #endif
371 0 : }
372 :
373 : static __inline struct fd_type *
374 0 : fd_dev_to_type(struct fd_softc *fd, dev_t dev)
375 : {
376 0 : int type = FDTYPE(dev);
377 :
378 0 : if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
379 0 : return NULL;
380 0 : return type ? &fd_types[type - 1] : fd->sc_deftype;
381 0 : }
382 :
383 : void
384 0 : fdstrategy(struct buf *bp)
385 : {
386 0 : struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(bp->b_dev)];
387 : int sz;
388 : int s;
389 0 : int fd_bsize = FD_BSIZE(fd);
390 0 : int bf = fd_bsize / DEV_BSIZE;
391 :
392 : /* Valid unit, controller, and request? */
393 0 : if (bp->b_blkno < 0 ||
394 0 : (((bp->b_blkno % bf) != 0 ||
395 0 : (bp->b_bcount % fd_bsize) != 0) &&
396 0 : (bp->b_flags & B_FORMAT) == 0)) {
397 0 : bp->b_error = EINVAL;
398 0 : goto bad;
399 : }
400 :
401 : /* If it's a null transfer, return immediately. */
402 0 : if (bp->b_bcount == 0)
403 : goto done;
404 :
405 0 : sz = howmany(bp->b_bcount, DEV_BSIZE);
406 :
407 0 : if (bp->b_blkno + sz > fd->sc_type->size * bf) {
408 0 : sz = fd->sc_type->size * bf - bp->b_blkno;
409 0 : if (sz == 0)
410 : /* If exactly at end of disk, return EOF. */
411 : goto done;
412 0 : if (sz < 0) {
413 : /* If past end of disk, return EINVAL. */
414 0 : bp->b_error = EINVAL;
415 0 : goto bad;
416 : }
417 : /* Otherwise, truncate request. */
418 0 : bp->b_bcount = sz << DEV_BSHIFT;
419 0 : }
420 :
421 0 : bp->b_resid = bp->b_bcount;
422 :
423 : #ifdef FD_DEBUG
424 : printf("fdstrategy: b_blkno %lld b_bcount %d blkno %lld sz %d\n",
425 : (long long)bp->b_blkno, bp->b_bcount,
426 : (long long)fd->sc_blkno, sz);
427 : #endif
428 :
429 : /* Queue I/O */
430 0 : bufq_queue(&fd->sc_bufq, bp);
431 :
432 : /* Queue transfer on drive, activate drive and controller if idle. */
433 0 : s = splbio();
434 0 : timeout_del(&fd->fd_motor_off_to); /* a good idea */
435 0 : if (fd->sc_bp == NULL)
436 0 : fdstart(fd);
437 : #ifdef DIAGNOSTIC
438 : else {
439 0 : struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
440 0 : if (fdc->sc_state == DEVIDLE) {
441 0 : printf("fdstrategy: controller inactive\n");
442 0 : fdcstart(fdc);
443 0 : }
444 : }
445 : #endif
446 0 : splx(s);
447 0 : return;
448 :
449 : bad:
450 0 : bp->b_flags |= B_ERROR;
451 : done:
452 : /* Toss transfer; we're done early. */
453 0 : bp->b_resid = bp->b_bcount;
454 0 : s = splbio();
455 0 : biodone(bp);
456 0 : splx(s);
457 0 : }
458 :
459 : void
460 0 : fdstart(struct fd_softc *fd)
461 : {
462 0 : struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
463 0 : int active = !TAILQ_EMPTY(&fdc->sc_link.fdlink.sc_drives);
464 :
465 : /* Link into controller queue. */
466 0 : fd->sc_bp = bufq_dequeue(&fd->sc_bufq);
467 0 : TAILQ_INSERT_TAIL(&fdc->sc_link.fdlink.sc_drives, fd, sc_drivechain);
468 :
469 : /* If controller not already active, start it. */
470 0 : if (!active)
471 0 : fdcstart(fdc);
472 0 : }
473 :
474 : void
475 0 : fdfinish(struct fd_softc *fd, struct buf *bp)
476 : {
477 0 : struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
478 :
479 0 : splassert(IPL_BIO);
480 :
481 0 : fd->sc_skip = 0;
482 0 : fd->sc_bp = bufq_dequeue(&fd->sc_bufq);
483 :
484 : /*
485 : * Move this drive to the end of the queue to give others a `fair'
486 : * chance. We only force a switch if N operations are completed while
487 : * another drive is waiting to be serviced, since there is a long motor
488 : * startup delay whenever we switch.
489 : */
490 0 : if (TAILQ_NEXT(fd, sc_drivechain) != NULL && ++fd->sc_ops >= 8) {
491 0 : fd->sc_ops = 0;
492 0 : TAILQ_REMOVE(&fdc->sc_link.fdlink.sc_drives, fd, sc_drivechain);
493 0 : if (fd->sc_bp != NULL) {
494 0 : TAILQ_INSERT_TAIL(&fdc->sc_link.fdlink.sc_drives, fd,
495 : sc_drivechain);
496 0 : }
497 : }
498 :
499 0 : biodone(bp);
500 : /* turn off motor 5s from now */
501 0 : timeout_add_sec(&fd->fd_motor_off_to, 5);
502 0 : fdc->sc_state = DEVIDLE;
503 0 : }
504 :
505 : int
506 0 : fdread(dev_t dev, struct uio *uio, int flags)
507 : {
508 0 : return (physio(fdstrategy, dev, B_READ, minphys, uio));
509 : }
510 :
511 : int
512 0 : fdwrite(dev_t dev, struct uio *uio, int flags)
513 : {
514 0 : return (physio(fdstrategy, dev, B_WRITE, minphys, uio));
515 : }
516 :
517 : void
518 0 : fd_set_motor(struct fdc_softc *fdc, int reset)
519 : {
520 : struct fd_softc *fd;
521 : u_char status;
522 : int n;
523 :
524 0 : if ((fd = TAILQ_FIRST(&fdc->sc_link.fdlink.sc_drives)) != NULL)
525 0 : status = fd->sc_drive;
526 : else
527 : status = 0;
528 0 : if (!reset)
529 0 : status |= FDO_FRST | FDO_FDMAEN;
530 0 : for (n = 0; n < 4; n++)
531 0 : if ((fd = fdc->sc_link.fdlink.sc_fd[n])
532 0 : && (fd->sc_flags & FD_MOTOR))
533 0 : status |= FDO_MOEN(n);
534 0 : bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout, status);
535 0 : }
536 :
537 : void
538 0 : fd_motor_off(void *arg)
539 : {
540 0 : struct fd_softc *fd = arg;
541 : int s;
542 :
543 0 : s = splbio();
544 0 : fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
545 0 : fd_set_motor((struct fdc_softc *)fd->sc_dev.dv_parent, 0);
546 0 : splx(s);
547 0 : }
548 :
549 : void
550 0 : fd_motor_on(void *arg)
551 : {
552 0 : struct fd_softc *fd = arg;
553 0 : struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
554 : int s;
555 :
556 0 : s = splbio();
557 0 : fd->sc_flags &= ~FD_MOTOR_WAIT;
558 0 : if ((TAILQ_FIRST(&fdc->sc_link.fdlink.sc_drives) == fd)
559 0 : && (fdc->sc_state == MOTORWAIT))
560 0 : (void) fdintr(fdc);
561 0 : splx(s);
562 0 : }
563 :
564 : int
565 0 : fdopen(dev_t dev, int flags, int fmt, struct proc *p)
566 : {
567 : int unit, pmask;
568 : struct fd_softc *fd;
569 : struct fd_type *type;
570 :
571 0 : unit = FDUNIT(dev);
572 0 : if (unit >= fd_cd.cd_ndevs)
573 0 : return ENXIO;
574 0 : fd = fd_cd.cd_devs[unit];
575 0 : if (fd == 0)
576 0 : return ENXIO;
577 0 : type = fd_dev_to_type(fd, dev);
578 0 : if (type == NULL)
579 0 : return ENXIO;
580 :
581 0 : if ((fd->sc_flags & FD_OPEN) != 0 &&
582 0 : fd->sc_type != type)
583 0 : return EBUSY;
584 :
585 0 : fd->sc_type = type;
586 0 : fd->sc_cylin = -1;
587 0 : fd->sc_flags |= FD_OPEN;
588 :
589 : /*
590 : * Only update the disklabel if we're not open anywhere else.
591 : */
592 0 : if (fd->sc_dk.dk_openmask == 0)
593 0 : fdgetdisklabel(dev, fd, fd->sc_dk.dk_label, 0);
594 :
595 0 : pmask = (1 << FDPART(dev));
596 :
597 0 : switch (fmt) {
598 : case S_IFCHR:
599 0 : fd->sc_dk.dk_copenmask |= pmask;
600 0 : break;
601 :
602 : case S_IFBLK:
603 0 : fd->sc_dk.dk_bopenmask |= pmask;
604 0 : break;
605 : }
606 0 : fd->sc_dk.dk_openmask =
607 0 : fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
608 :
609 0 : return 0;
610 0 : }
611 :
612 : int
613 0 : fdclose(dev_t dev, int flags, int fmt, struct proc *p)
614 : {
615 0 : struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
616 0 : int pmask = (1 << FDPART(dev));
617 :
618 0 : fd->sc_flags &= ~FD_OPEN;
619 0 : fd->sc_opts &= ~FDOPT_NORETRY;
620 :
621 0 : switch (fmt) {
622 : case S_IFCHR:
623 0 : fd->sc_dk.dk_copenmask &= ~pmask;
624 0 : break;
625 :
626 : case S_IFBLK:
627 0 : fd->sc_dk.dk_bopenmask &= ~pmask;
628 0 : break;
629 : }
630 0 : fd->sc_dk.dk_openmask =
631 0 : fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
632 :
633 0 : return (0);
634 : }
635 :
636 : daddr_t
637 0 : fdsize(dev_t dev)
638 : {
639 : /* Swapping to floppies would not make sense. */
640 0 : return -1;
641 : }
642 :
643 : int
644 0 : fddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
645 : {
646 : /* Not implemented. */
647 0 : return ENXIO;
648 : }
649 :
650 : /*
651 : * Called from the controller.
652 : */
653 : int
654 0 : fdintr(struct fdc_softc *fdc)
655 : {
656 : #define st0 fdc->sc_status[0]
657 : #define cyl fdc->sc_status[1]
658 : struct fd_softc *fd;
659 : struct buf *bp;
660 0 : bus_space_tag_t iot = fdc->sc_iot;
661 0 : bus_space_handle_t ioh = fdc->sc_ioh;
662 0 : bus_space_handle_t ioh_ctl = fdc->sc_ioh_ctl;
663 : int read, head, sec, i, nblks, cylin;
664 : struct fd_type *type;
665 : struct fd_formb *finfo = NULL;
666 0 : int fd_bsize;
667 :
668 : loop:
669 : /* Is there a transfer to this drive? If not, deactivate drive. */
670 0 : fd = TAILQ_FIRST(&fdc->sc_link.fdlink.sc_drives);
671 0 : if (fd == NULL) {
672 0 : fdc->sc_state = DEVIDLE;
673 0 : return 1;
674 : }
675 0 : fd_bsize = FD_BSIZE(fd);
676 :
677 0 : bp = fd->sc_bp;
678 0 : if (bp == NULL) {
679 0 : fd->sc_ops = 0;
680 0 : TAILQ_REMOVE(&fdc->sc_link.fdlink.sc_drives, fd, sc_drivechain);
681 0 : goto loop;
682 : }
683 :
684 0 : if (bp->b_flags & B_FORMAT)
685 0 : finfo = (struct fd_formb *)bp->b_data;
686 :
687 0 : cylin = ((bp->b_blkno * DEV_BSIZE) + (bp->b_bcount - bp->b_resid)) /
688 0 : (fd_bsize * fd->sc_type->seccyl);
689 :
690 0 : switch (fdc->sc_state) {
691 : case DEVIDLE:
692 0 : fdc->sc_errors = 0;
693 0 : fd->sc_skip = 0;
694 0 : fd->sc_bcount = bp->b_bcount;
695 0 : fd->sc_blkno = bp->b_blkno / (fd_bsize / DEV_BSIZE);
696 0 : timeout_del(&fd->fd_motor_off_to);
697 0 : if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
698 0 : fdc->sc_state = MOTORWAIT;
699 0 : return 1;
700 : }
701 0 : if ((fd->sc_flags & FD_MOTOR) == 0) {
702 : /* Turn on the motor, being careful about pairing. */
703 : struct fd_softc *ofd =
704 0 : fdc->sc_link.fdlink.sc_fd[fd->sc_drive ^ 1];
705 0 : if (ofd && ofd->sc_flags & FD_MOTOR) {
706 0 : timeout_del(&ofd->fd_motor_off_to);
707 0 : ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
708 0 : }
709 0 : fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
710 0 : fd_set_motor(fdc, 0);
711 0 : fdc->sc_state = MOTORWAIT;
712 : /* Allow .25s for motor to stabilize. */
713 0 : timeout_add_msec(&fd->fd_motor_on_to, 250);
714 : return 1;
715 : }
716 : /* Make sure the right drive is selected. */
717 0 : fd_set_motor(fdc, 0);
718 :
719 : /* FALLTHROUGH */
720 : case DOSEEK:
721 : doseek:
722 0 : if (fd->sc_cylin == cylin)
723 : goto doio;
724 :
725 0 : out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
726 0 : out_fdc(iot, ioh, fd->sc_type->steprate);
727 0 : out_fdc(iot, ioh, 6); /* XXX head load time == 6ms */
728 :
729 0 : out_fdc(iot, ioh, NE7CMD_SEEK); /* seek function */
730 0 : out_fdc(iot, ioh, fd->sc_drive); /* drive number */
731 0 : out_fdc(iot, ioh, cylin * fd->sc_type->step);
732 :
733 0 : fd->sc_cylin = -1;
734 0 : fdc->sc_state = SEEKWAIT;
735 :
736 0 : fd->sc_dk.dk_seek++;
737 0 : disk_busy(&fd->sc_dk);
738 :
739 0 : timeout_add_sec(&fd->fdtimeout_to, 4);
740 0 : return 1;
741 :
742 : case DOIO:
743 : doio:
744 0 : type = fd->sc_type;
745 0 : if (finfo)
746 0 : fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
747 0 : (char *)finfo;
748 0 : sec = fd->sc_blkno % type->seccyl;
749 0 : nblks = type->seccyl - sec;
750 0 : nblks = min(nblks, fd->sc_bcount / fd_bsize);
751 0 : nblks = min(nblks, FDC_MAXIOSIZE / fd_bsize);
752 0 : fd->sc_nblks = nblks;
753 0 : fd->sc_nbytes = finfo ? bp->b_bcount : nblks * fd_bsize;
754 0 : head = sec / type->sectrac;
755 0 : sec -= head * type->sectrac;
756 : #ifdef DIAGNOSTIC
757 : {int block;
758 0 : block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
759 0 : if (block != fd->sc_blkno) {
760 0 : panic("fdintr: block %d != blkno %llu", block, fd->sc_blkno);
761 : }}
762 : #endif
763 0 : read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE;
764 0 : isadma_start(bp->b_data + fd->sc_skip, fd->sc_nbytes,
765 : fdc->sc_drq, read);
766 0 : bus_space_write_1(iot, ioh_ctl, fdctl, type->rate);
767 : #ifdef FD_DEBUG
768 : printf("fdintr: %s drive %d track %d head %d sec %d nblks %d\n",
769 : read ? "read" : "write", fd->sc_drive, fd->sc_cylin, head,
770 : sec, nblks);
771 : #endif
772 0 : if (finfo) {
773 : /* formatting */
774 0 : if (out_fdc(iot, ioh, NE7CMD_FORMAT) < 0) {
775 0 : fdc->sc_errors = 4;
776 0 : fdretry(fd);
777 0 : goto loop;
778 : }
779 0 : out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
780 0 : out_fdc(iot, ioh, finfo->fd_formb_secshift);
781 0 : out_fdc(iot, ioh, finfo->fd_formb_nsecs);
782 0 : out_fdc(iot, ioh, finfo->fd_formb_gaplen);
783 0 : out_fdc(iot, ioh, finfo->fd_formb_fillbyte);
784 0 : } else {
785 0 : if (read)
786 0 : out_fdc(iot, ioh, NE7CMD_READ); /* READ */
787 : else
788 0 : out_fdc(iot, ioh, NE7CMD_WRITE);/* WRITE */
789 0 : out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
790 0 : out_fdc(iot, ioh, fd->sc_cylin); /* track */
791 0 : out_fdc(iot, ioh, head);
792 0 : out_fdc(iot, ioh, sec + 1); /* sec +1 */
793 0 : out_fdc(iot, ioh, type->secsize); /* sec size */
794 0 : out_fdc(iot, ioh, type->sectrac); /* secs/track */
795 0 : out_fdc(iot, ioh, type->gap1); /* gap1 size */
796 0 : out_fdc(iot, ioh, type->datalen); /* data len */
797 : }
798 0 : fdc->sc_state = IOCOMPLETE;
799 :
800 0 : disk_busy(&fd->sc_dk);
801 :
802 : /* allow 2 seconds for operation */
803 0 : timeout_add_sec(&fd->fdtimeout_to, 2);
804 0 : return 1; /* will return later */
805 :
806 : case SEEKWAIT:
807 0 : timeout_del(&fd->fdtimeout_to);
808 0 : fdc->sc_state = SEEKCOMPLETE;
809 : /* allow 1/50 second for heads to settle */
810 0 : timeout_add_msec(&fdc->fdcpseudointr_to, 20);
811 0 : return 1;
812 :
813 : case SEEKCOMPLETE:
814 0 : disk_unbusy(&fd->sc_dk, 0, 0, 0); /* no data on seek */
815 :
816 : /* Make sure seek really happened. */
817 0 : out_fdc(iot, ioh, NE7CMD_SENSEI);
818 0 : if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
819 0 : cyl != cylin * fd->sc_type->step) {
820 : #ifdef FD_DEBUG
821 : fdcstatus(&fd->sc_dev, 2, "seek failed");
822 : #endif
823 0 : fdretry(fd);
824 0 : goto loop;
825 : }
826 0 : fd->sc_cylin = cylin;
827 0 : goto doio;
828 :
829 : case IOTIMEDOUT:
830 0 : isadma_abort(fdc->sc_drq);
831 : case SEEKTIMEDOUT:
832 : case RECALTIMEDOUT:
833 : case RESETTIMEDOUT:
834 0 : fdretry(fd);
835 0 : goto loop;
836 :
837 : case IOCOMPLETE: /* IO DONE, post-analyze */
838 0 : timeout_del(&fd->fdtimeout_to);
839 :
840 0 : disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
841 0 : fd->sc_blkno, (bp->b_flags & B_READ));
842 :
843 0 : if (fdcresult(fdc) != 7 || (st0 & 0xf8) != 0) {
844 0 : isadma_abort(fdc->sc_drq);
845 : #ifdef FD_DEBUG
846 : fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
847 : "read failed" : "write failed");
848 : printf("blkno %lld nblks %d\n",
849 : (long long)fd->sc_blkno, fd->sc_nblks);
850 : #endif
851 0 : fdretry(fd);
852 0 : goto loop;
853 : }
854 0 : read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE;
855 0 : isadma_done(fdc->sc_drq);
856 0 : if (fdc->sc_errors) {
857 0 : diskerr(bp, "fd", "soft error", LOG_PRINTF,
858 0 : fd->sc_skip / fd_bsize, (struct disklabel *)NULL);
859 0 : printf("\n");
860 0 : fdc->sc_errors = 0;
861 0 : }
862 :
863 0 : fd->sc_blkno += fd->sc_nblks;
864 0 : fd->sc_skip += fd->sc_nbytes;
865 0 : fd->sc_bcount -= fd->sc_nbytes;
866 0 : bp->b_resid -= fd->sc_nbytes;
867 0 : if (!finfo && fd->sc_bcount > 0) {
868 0 : cylin = fd->sc_blkno / fd->sc_type->seccyl;
869 0 : goto doseek;
870 : }
871 0 : fdfinish(fd, bp);
872 0 : goto loop;
873 :
874 : case DORESET:
875 : /* try a reset, keep motor on */
876 0 : fd_set_motor(fdc, 1);
877 0 : delay(100);
878 0 : fd_set_motor(fdc, 0);
879 0 : fdc->sc_state = RESETCOMPLETE;
880 0 : timeout_add_msec(&fd->fdtimeout_to, 500);
881 0 : return 1; /* will return later */
882 :
883 : case RESETCOMPLETE:
884 0 : timeout_del(&fd->fdtimeout_to);
885 : /* clear the controller output buffer */
886 0 : for (i = 0; i < 4; i++) {
887 0 : out_fdc(iot, ioh, NE7CMD_SENSEI);
888 0 : (void) fdcresult(fdc);
889 : }
890 :
891 : /* FALLTHROUGH */
892 : case DORECAL:
893 0 : out_fdc(iot, ioh, NE7CMD_RECAL); /* recal function */
894 0 : out_fdc(iot, ioh, fd->sc_drive);
895 0 : fdc->sc_state = RECALWAIT;
896 0 : timeout_add_sec(&fd->fdtimeout_to, 5);
897 0 : return 1; /* will return later */
898 :
899 : case RECALWAIT:
900 0 : timeout_del(&fd->fdtimeout_to);
901 0 : fdc->sc_state = RECALCOMPLETE;
902 : /* allow 1/30 second for heads to settle */
903 0 : timeout_add(&fdc->fdcpseudointr_to, hz / 30);
904 0 : return 1; /* will return later */
905 :
906 : case RECALCOMPLETE:
907 0 : out_fdc(iot, ioh, NE7CMD_SENSEI);
908 0 : if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
909 : #ifdef FD_DEBUG
910 : fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
911 : #endif
912 0 : fdretry(fd);
913 0 : goto loop;
914 : }
915 0 : fd->sc_cylin = 0;
916 0 : goto doseek;
917 :
918 : case MOTORWAIT:
919 0 : if (fd->sc_flags & FD_MOTOR_WAIT)
920 0 : return 1; /* time's not up yet */
921 : goto doseek;
922 :
923 : default:
924 0 : fdcstatus(&fd->sc_dev, 0, "stray interrupt");
925 0 : return 1;
926 : }
927 : #ifdef DIAGNOSTIC
928 : panic("fdintr: impossible");
929 : #endif
930 : #undef st0
931 : #undef cyl
932 0 : }
933 :
934 : void
935 0 : fdtimeout(void *arg)
936 : {
937 0 : struct fd_softc *fd = arg;
938 0 : struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
939 : int s;
940 :
941 0 : s = splbio();
942 : #ifdef DEBUG
943 : log(LOG_ERR,"fdtimeout: state %d\n", fdc->sc_state);
944 : #endif
945 0 : fdcstatus(&fd->sc_dev, 0, "timeout");
946 :
947 0 : if (fd->sc_bp != NULL)
948 0 : fdc->sc_state++;
949 : else
950 0 : fdc->sc_state = DEVIDLE;
951 :
952 0 : (void) fdintr(fdc);
953 0 : splx(s);
954 0 : }
955 :
956 : void
957 0 : fdretry(struct fd_softc *fd)
958 : {
959 0 : struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
960 0 : struct buf *bp = fd->sc_bp;
961 :
962 0 : if (fd->sc_opts & FDOPT_NORETRY)
963 : goto fail;
964 0 : switch (fdc->sc_errors) {
965 : case 0:
966 : /* try again */
967 0 : fdc->sc_state = DOSEEK;
968 0 : break;
969 :
970 : case 1: case 2: case 3:
971 : /* didn't work; try recalibrating */
972 0 : fdc->sc_state = DORECAL;
973 0 : break;
974 :
975 : case 4:
976 : /* still no go; reset the bastard */
977 0 : fdc->sc_state = DORESET;
978 0 : break;
979 :
980 : default:
981 : fail:
982 0 : diskerr(bp, "fd", "hard error", LOG_PRINTF,
983 0 : fd->sc_skip / FD_BSIZE(fd), (struct disklabel *)NULL);
984 0 : printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n",
985 0 : fdc->sc_status[0], NE7_ST0BITS,
986 0 : fdc->sc_status[1], NE7_ST1BITS,
987 0 : fdc->sc_status[2], NE7_ST2BITS,
988 0 : fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
989 :
990 0 : bp->b_flags |= B_ERROR;
991 0 : bp->b_error = EIO;
992 0 : bp->b_resid = bp->b_bcount;
993 0 : fdfinish(fd, bp);
994 0 : }
995 0 : fdc->sc_errors++;
996 0 : }
997 :
998 : int
999 0 : fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
1000 : {
1001 0 : struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
1002 : struct disklabel *lp;
1003 : int error;
1004 :
1005 0 : switch (cmd) {
1006 : case MTIOCTOP:
1007 0 : if (((struct mtop *)addr)->mt_op != MTOFFL)
1008 0 : return EIO;
1009 0 : return (0);
1010 :
1011 : case DIOCRLDINFO:
1012 0 : lp = malloc(sizeof(*lp), M_TEMP, M_WAITOK);
1013 0 : fdgetdisklabel(dev, fd, lp, 0);
1014 0 : bcopy(lp, fd->sc_dk.dk_label, sizeof(*lp));
1015 0 : free(lp, M_TEMP, sizeof(*lp));
1016 0 : return 0;
1017 :
1018 : case DIOCGPDINFO:
1019 0 : fdgetdisklabel(dev, fd, (struct disklabel *)addr, 1);
1020 0 : return 0;
1021 :
1022 : case DIOCGDINFO:
1023 0 : *(struct disklabel *)addr = *(fd->sc_dk.dk_label);
1024 0 : return 0;
1025 :
1026 : case DIOCGPART:
1027 0 : ((struct partinfo *)addr)->disklab = fd->sc_dk.dk_label;
1028 0 : ((struct partinfo *)addr)->part =
1029 0 : &fd->sc_dk.dk_label->d_partitions[FDPART(dev)];
1030 0 : return 0;
1031 :
1032 : case DIOCWDINFO:
1033 : case DIOCSDINFO:
1034 0 : if ((flag & FWRITE) == 0)
1035 0 : return EBADF;
1036 :
1037 0 : error = setdisklabel(fd->sc_dk.dk_label,
1038 0 : (struct disklabel *)addr, 0);
1039 0 : if (error == 0) {
1040 0 : if (cmd == DIOCWDINFO)
1041 0 : error = writedisklabel(DISKLABELDEV(dev),
1042 0 : fdstrategy, fd->sc_dk.dk_label);
1043 : }
1044 0 : return error;
1045 :
1046 : case FD_FORM:
1047 0 : if((flag & FWRITE) == 0)
1048 0 : return EBADF; /* must be opened for writing */
1049 0 : else if(((struct fd_formb *)addr)->format_version !=
1050 : FD_FORMAT_VERSION)
1051 0 : return EINVAL; /* wrong version of formatting prog */
1052 : else
1053 0 : return fdformat(dev, (struct fd_formb *)addr, p);
1054 : break;
1055 :
1056 : case FD_GTYPE: /* get drive type */
1057 0 : *(struct fd_type *)addr = *fd->sc_type;
1058 0 : return 0;
1059 :
1060 : case FD_GOPTS: /* get drive options */
1061 0 : *(int *)addr = fd->sc_opts;
1062 0 : return 0;
1063 :
1064 : case FD_SOPTS: /* set drive options */
1065 0 : fd->sc_opts = *(int *)addr;
1066 0 : return 0;
1067 :
1068 : default:
1069 0 : return ENOTTY;
1070 : }
1071 :
1072 : #ifdef DIAGNOSTIC
1073 : panic("fdioctl: impossible");
1074 : #endif
1075 0 : }
1076 :
1077 : int
1078 0 : fdformat(dev_t dev, struct fd_formb *finfo, struct proc *p)
1079 : {
1080 : int rv = 0;
1081 0 : struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
1082 0 : struct fd_type *type = fd->sc_type;
1083 : struct buf *bp;
1084 0 : int fd_bsize = FD_BSIZE(fd);
1085 :
1086 : /* set up a buffer header for fdstrategy() */
1087 0 : bp = malloc(sizeof(*bp), M_TEMP, M_NOWAIT | M_ZERO);
1088 0 : if (bp == NULL)
1089 0 : return ENOBUFS;
1090 :
1091 0 : bp->b_flags = B_BUSY | B_PHYS | B_FORMAT | B_RAW;
1092 0 : bp->b_proc = p;
1093 0 : bp->b_dev = dev;
1094 :
1095 : /*
1096 : * calculate a fake blkno, so fdstrategy() would initiate a
1097 : * seek to the requested cylinder
1098 : */
1099 0 : bp->b_blkno = (finfo->cyl * (type->sectrac * type->heads)
1100 0 : + finfo->head * type->sectrac) * fd_bsize / DEV_BSIZE;
1101 :
1102 0 : bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
1103 0 : bp->b_data = (caddr_t)finfo;
1104 :
1105 : #ifdef DEBUG
1106 : printf("fdformat: blkno %llx count %lx\n", bp->b_blkno, bp->b_bcount);
1107 : #endif
1108 :
1109 : /* now do the format */
1110 0 : fdstrategy(bp);
1111 :
1112 : /* ...and wait for it to complete */
1113 0 : rv = biowait(bp);
1114 0 : free(bp, M_TEMP, sizeof(*bp));
1115 0 : return (rv);
1116 0 : }
|