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