1 /* $OpenBSD: fd.c,v 1.107 2020/02/07 13:35:08 cheloha 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 fdgetdisklabel(dev_t dev, struct fd_softc *fd, struct disklabel *lp, 157 int spoofonly) 158 { 159 bzero(lp, sizeof(struct disklabel)); 160 161 lp->d_type = DTYPE_FLOPPY; 162 lp->d_secsize = FD_BSIZE(fd); 163 lp->d_secpercyl = fd->sc_type->seccyl; 164 lp->d_nsectors = fd->sc_type->sectrac; 165 lp->d_ncylinders = fd->sc_type->tracks; 166 lp->d_ntracks = fd->sc_type->heads; /* Go figure... */ 167 DL_SETDSIZE(lp, fd->sc_type->size); 168 169 strncpy(lp->d_typename, "floppy disk", sizeof(lp->d_typename)); 170 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname)); 171 lp->d_version = 1; 172 173 lp->d_magic = DISKMAGIC; 174 lp->d_magic2 = DISKMAGIC; 175 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 return readdisklabel(DISKLABELDEV(dev), fdstrategy, lp, spoofonly); 182 } 183 184 int 185 fdprobe(struct device *parent, void *match, void *aux) 186 { 187 struct fdc_softc *fdc = (void *)parent; 188 struct cfdata *cf = match; 189 struct fdc_attach_args *fa = aux; 190 int drive = fa->fa_drive; 191 bus_space_tag_t iot = fdc->sc_iot; 192 bus_space_handle_t ioh = fdc->sc_ioh; 193 int n; 194 195 if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != drive) 196 return 0; 197 /* 198 * XXX 199 * This is to work around some odd interactions between this driver 200 * and SMC Ethernet cards. 201 */ 202 if (cf->cf_loc[0] == -1 && drive >= 2) 203 return 0; 204 205 /* 206 * We want to keep the flags config gave us. 207 */ 208 fa->fa_flags = cf->cf_flags; 209 210 /* select drive and turn on motor */ 211 bus_space_write_1(iot, ioh, fdout, drive | FDO_FRST | FDO_MOEN(drive)); 212 /* wait for motor to spin up */ 213 tsleep_nsec(fdc, 0, "fdprobe", MSEC_TO_NSEC(250)); 214 out_fdc(iot, ioh, NE7CMD_RECAL); 215 out_fdc(iot, ioh, drive); 216 /* wait for recalibrate */ 217 tsleep_nsec(fdc, 0, "fdprobe", MSEC_TO_NSEC(2000)); 218 out_fdc(iot, ioh, NE7CMD_SENSEI); 219 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 tsleep_nsec(fdc, 0, "fdprobe", MSEC_TO_NSEC(250)); 232 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 if (!(fa->fa_flags & 0x20) && (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)) 236 return 0; 237 238 return 1; 239 } 240 241 /* 242 * Controller is working, and drive responded. Attach it. 243 */ 244 void 245 fdattach(struct device *parent, struct device *self, void *aux) 246 { 247 struct fdc_softc *fdc = (void *)parent; 248 struct fd_softc *fd = (void *)self; 249 struct fdc_attach_args *fa = aux; 250 struct fd_type *type = fa->fa_deftype; 251 int drive = fa->fa_drive; 252 253 if (!type || (fa->fa_flags & 0x10)) { 254 /* The config has overridden this. */ 255 switch (fa->fa_flags & 0x07) { 256 case 1: /* 2.88MB */ 257 type = &fd_types[7]; 258 break; 259 case 2: /* 1.44MB */ 260 type = &fd_types[0]; 261 break; 262 case 3: /* 1.2MB */ 263 type = &fd_types[1]; 264 break; 265 case 4: /* 720K */ 266 type = &fd_types[4]; 267 break; 268 case 5: /* 360K */ 269 type = &fd_types[3]; 270 break; 271 case 6: /* 1.2 MB japanese format */ 272 type = &fd_types[8]; 273 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 if (type) 285 printf(": %s %d cyl, %d head, %d sec\n", type->name, 286 type->tracks, type->heads, type->sectrac); 287 else 288 printf(": density unknown\n"); 289 290 fd->sc_cylin = -1; 291 fd->sc_drive = drive; 292 fd->sc_deftype = type; 293 fdc->sc_type[drive] = FDC_TYPE_DISK; 294 fdc->sc_link.fdlink.sc_fd[drive] = fd; 295 296 /* 297 * Initialize and attach the disk structure. 298 */ 299 fd->sc_dk.dk_flags = DKF_NOLABELREAD; 300 fd->sc_dk.dk_name = fd->sc_dev.dv_xname; 301 bufq_init(&fd->sc_bufq, BUFQ_DEFAULT); 302 disk_attach(&fd->sc_dev, &fd->sc_dk); 303 304 /* Setup timeout structures */ 305 timeout_set(&fd->fd_motor_on_to, fd_motor_on, fd); 306 timeout_set(&fd->fd_motor_off_to, fd_motor_off, fd); 307 timeout_set(&fd->fdtimeout_to, fdtimeout, fd); 308 } 309 310 int 311 fdactivate(struct device *self, int act) 312 { 313 struct fd_softc *fd = (void *)self; 314 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent; 315 int rv = 0; 316 317 switch (act) { 318 case DVACT_SUSPEND: 319 if (fdc->sc_state != DEVIDLE) { 320 timeout_del(&fd->fd_motor_on_to); 321 timeout_del(&fd->fd_motor_off_to); 322 timeout_del(&fd->fdtimeout_to); 323 fdc->sc_state = IOTIMEDOUT; 324 fdc->sc_errors = 4; 325 } 326 break; 327 case DVACT_POWERDOWN: 328 fd_motor_off(self); 329 break; 330 } 331 332 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 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 type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0; 351 switch (type) { 352 case NVRAM_DISKETTE_NONE: 353 return NULL; 354 case NVRAM_DISKETTE_12M: 355 return &fd_types[1]; 356 case NVRAM_DISKETTE_TYPE5: 357 case NVRAM_DISKETTE_TYPE6: 358 return &fd_types[7]; 359 case NVRAM_DISKETTE_144M: 360 return &fd_types[0]; 361 case NVRAM_DISKETTE_360K: 362 return &fd_types[3]; 363 case NVRAM_DISKETTE_720K: 364 return &fd_types[4]; 365 default: 366 printf("%s: drive %d: unknown device type 0x%x\n", 367 fdc, drive, type); 368 return NULL; 369 } 370 #endif 371 } 372 373 static __inline struct fd_type * 374 fd_dev_to_type(struct fd_softc *fd, dev_t dev) 375 { 376 int type = FDTYPE(dev); 377 378 if (type > (sizeof(fd_types) / sizeof(fd_types[0]))) 379 return NULL; 380 return type ? &fd_types[type - 1] : fd->sc_deftype; 381 } 382 383 void 384 fdstrategy(struct buf *bp) 385 { 386 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(bp->b_dev)]; 387 int sz; 388 int s; 389 int fd_bsize = FD_BSIZE(fd); 390 int bf = fd_bsize / DEV_BSIZE; 391 392 /* Valid unit, controller, and request? */ 393 if (bp->b_blkno < 0 || 394 (((bp->b_blkno % bf) != 0 || 395 (bp->b_bcount % fd_bsize) != 0) && 396 (bp->b_flags & B_FORMAT) == 0)) { 397 bp->b_error = EINVAL; 398 goto bad; 399 } 400 401 /* If it's a null transfer, return immediately. */ 402 if (bp->b_bcount == 0) 403 goto done; 404 405 sz = howmany(bp->b_bcount, DEV_BSIZE); 406 407 if (bp->b_blkno + sz > fd->sc_type->size * bf) { 408 sz = fd->sc_type->size * bf - bp->b_blkno; 409 if (sz == 0) 410 /* If exactly at end of disk, return EOF. */ 411 goto done; 412 if (sz < 0) { 413 /* If past end of disk, return EINVAL. */ 414 bp->b_error = EINVAL; 415 goto bad; 416 } 417 /* Otherwise, truncate request. */ 418 bp->b_bcount = sz << DEV_BSHIFT; 419 } 420 421 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 bufq_queue(&fd->sc_bufq, bp); 431 432 /* Queue transfer on drive, activate drive and controller if idle. */ 433 s = splbio(); 434 timeout_del(&fd->fd_motor_off_to); /* a good idea */ 435 if (fd->sc_bp == NULL) 436 fdstart(fd); 437 #ifdef DIAGNOSTIC 438 else { 439 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent; 440 if (fdc->sc_state == DEVIDLE) { 441 printf("fdstrategy: controller inactive\n"); 442 fdcstart(fdc); 443 } 444 } 445 #endif 446 splx(s); 447 return; 448 449 bad: 450 bp->b_flags |= B_ERROR; 451 done: 452 /* Toss transfer; we're done early. */ 453 bp->b_resid = bp->b_bcount; 454 s = splbio(); 455 biodone(bp); 456 splx(s); 457 } 458 459 void 460 fdstart(struct fd_softc *fd) 461 { 462 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent; 463 int active = !TAILQ_EMPTY(&fdc->sc_link.fdlink.sc_drives); 464 465 /* Link into controller queue. */ 466 fd->sc_bp = bufq_dequeue(&fd->sc_bufq); 467 TAILQ_INSERT_TAIL(&fdc->sc_link.fdlink.sc_drives, fd, sc_drivechain); 468 469 /* If controller not already active, start it. */ 470 if (!active) 471 fdcstart(fdc); 472 } 473 474 void 475 fdfinish(struct fd_softc *fd, struct buf *bp) 476 { 477 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent; 478 479 splassert(IPL_BIO); 480 481 fd->sc_skip = 0; 482 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 if (TAILQ_NEXT(fd, sc_drivechain) != NULL && ++fd->sc_ops >= 8) { 491 fd->sc_ops = 0; 492 TAILQ_REMOVE(&fdc->sc_link.fdlink.sc_drives, fd, sc_drivechain); 493 if (fd->sc_bp != NULL) { 494 TAILQ_INSERT_TAIL(&fdc->sc_link.fdlink.sc_drives, fd, 495 sc_drivechain); 496 } 497 } 498 499 biodone(bp); 500 /* turn off motor 5s from now */ 501 timeout_add_sec(&fd->fd_motor_off_to, 5); 502 fdc->sc_state = DEVIDLE; 503 } 504 505 int 506 fdread(dev_t dev, struct uio *uio, int flags) 507 { 508 return (physio(fdstrategy, dev, B_READ, minphys, uio)); 509 } 510 511 int 512 fdwrite(dev_t dev, struct uio *uio, int flags) 513 { 514 return (physio(fdstrategy, dev, B_WRITE, minphys, uio)); 515 } 516 517 void 518 fd_set_motor(struct fdc_softc *fdc, int reset) 519 { 520 struct fd_softc *fd; 521 u_char status; 522 int n; 523 524 if ((fd = TAILQ_FIRST(&fdc->sc_link.fdlink.sc_drives)) != NULL) 525 status = fd->sc_drive; 526 else 527 status = 0; 528 if (!reset) 529 status |= FDO_FRST | FDO_FDMAEN; 530 for (n = 0; n < 4; n++) 531 if ((fd = fdc->sc_link.fdlink.sc_fd[n]) 532 && (fd->sc_flags & FD_MOTOR)) 533 status |= FDO_MOEN(n); 534 bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout, status); 535 } 536 537 void 538 fd_motor_off(void *arg) 539 { 540 struct fd_softc *fd = arg; 541 int s; 542 543 s = splbio(); 544 fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT); 545 fd_set_motor((struct fdc_softc *)fd->sc_dev.dv_parent, 0); 546 splx(s); 547 } 548 549 void 550 fd_motor_on(void *arg) 551 { 552 struct fd_softc *fd = arg; 553 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent; 554 int s; 555 556 s = splbio(); 557 fd->sc_flags &= ~FD_MOTOR_WAIT; 558 if ((TAILQ_FIRST(&fdc->sc_link.fdlink.sc_drives) == fd) 559 && (fdc->sc_state == MOTORWAIT)) 560 (void) fdintr(fdc); 561 splx(s); 562 } 563 564 int 565 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 unit = FDUNIT(dev); 572 if (unit >= fd_cd.cd_ndevs) 573 return ENXIO; 574 fd = fd_cd.cd_devs[unit]; 575 if (fd == 0) 576 return ENXIO; 577 type = fd_dev_to_type(fd, dev); 578 if (type == NULL) 579 return ENXIO; 580 581 if ((fd->sc_flags & FD_OPEN) != 0 && 582 fd->sc_type != type) 583 return EBUSY; 584 585 fd->sc_type = type; 586 fd->sc_cylin = -1; 587 fd->sc_flags |= FD_OPEN; 588 589 /* 590 * Only update the disklabel if we're not open anywhere else. 591 */ 592 if (fd->sc_dk.dk_openmask == 0) 593 fdgetdisklabel(dev, fd, fd->sc_dk.dk_label, 0); 594 595 pmask = (1 << FDPART(dev)); 596 597 switch (fmt) { 598 case S_IFCHR: 599 fd->sc_dk.dk_copenmask |= pmask; 600 break; 601 602 case S_IFBLK: 603 fd->sc_dk.dk_bopenmask |= pmask; 604 break; 605 } 606 fd->sc_dk.dk_openmask = 607 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask; 608 609 return 0; 610 } 611 612 int 613 fdclose(dev_t dev, int flags, int fmt, struct proc *p) 614 { 615 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)]; 616 int pmask = (1 << FDPART(dev)); 617 618 fd->sc_flags &= ~FD_OPEN; 619 fd->sc_opts &= ~FDOPT_NORETRY; 620 621 switch (fmt) { 622 case S_IFCHR: 623 fd->sc_dk.dk_copenmask &= ~pmask; 624 break; 625 626 case S_IFBLK: 627 fd->sc_dk.dk_bopenmask &= ~pmask; 628 break; 629 } 630 fd->sc_dk.dk_openmask = 631 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask; 632 633 return (0); 634 } 635 636 daddr_t 637 fdsize(dev_t dev) 638 { 639 /* Swapping to floppies would not make sense. */ 640 return -1; 641 } 642 643 int 644 fddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size) 645 { 646 /* Not implemented. */ 647 return ENXIO; 648 } 649 650 /* 651 * Called from the controller. 652 */ 653 int 654 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 bus_space_tag_t iot = fdc->sc_iot; 661 bus_space_handle_t ioh = fdc->sc_ioh; 662 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 int fd_bsize; 667 668 loop: 669 /* Is there a transfer to this drive? If not, deactivate drive. */ 670 fd = TAILQ_FIRST(&fdc->sc_link.fdlink.sc_drives); 671 if (fd == NULL) { 672 fdc->sc_state = DEVIDLE; 673 return 1; 674 } 675 fd_bsize = FD_BSIZE(fd); 676 677 bp = fd->sc_bp; 678 if (bp == NULL) { 679 fd->sc_ops = 0; 680 TAILQ_REMOVE(&fdc->sc_link.fdlink.sc_drives, fd, sc_drivechain); 681 goto loop; 682 } 683 684 if (bp->b_flags & B_FORMAT) 685 finfo = (struct fd_formb *)bp->b_data; 686 687 cylin = ((bp->b_blkno * DEV_BSIZE) + (bp->b_bcount - bp->b_resid)) / 688 (fd_bsize * fd->sc_type->seccyl); 689 690 switch (fdc->sc_state) { 691 case DEVIDLE: 692 fdc->sc_errors = 0; 693 fd->sc_skip = 0; 694 fd->sc_bcount = bp->b_bcount; 695 fd->sc_blkno = bp->b_blkno / (fd_bsize / DEV_BSIZE); 696 timeout_del(&fd->fd_motor_off_to); 697 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) { 698 fdc->sc_state = MOTORWAIT; 699 return 1; 700 } 701 if ((fd->sc_flags & FD_MOTOR) == 0) { 702 /* Turn on the motor, being careful about pairing. */ 703 struct fd_softc *ofd = 704 fdc->sc_link.fdlink.sc_fd[fd->sc_drive ^ 1]; 705 if (ofd && ofd->sc_flags & FD_MOTOR) { 706 timeout_del(&ofd->fd_motor_off_to); 707 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT); 708 } 709 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT; 710 fd_set_motor(fdc, 0); 711 fdc->sc_state = MOTORWAIT; 712 /* Allow .25s for motor to stabilize. */ 713 timeout_add_msec(&fd->fd_motor_on_to, 250); 714 return 1; 715 } 716 /* Make sure the right drive is selected. */ 717 fd_set_motor(fdc, 0); 718 719 /* FALLTHROUGH */ 720 case DOSEEK: 721 doseek: 722 if (fd->sc_cylin == cylin) 723 goto doio; 724 725 out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */ 726 out_fdc(iot, ioh, fd->sc_type->steprate); 727 out_fdc(iot, ioh, 6); /* XXX head load time == 6ms */ 728 729 out_fdc(iot, ioh, NE7CMD_SEEK); /* seek function */ 730 out_fdc(iot, ioh, fd->sc_drive); /* drive number */ 731 out_fdc(iot, ioh, cylin * fd->sc_type->step); 732 733 fd->sc_cylin = -1; 734 fdc->sc_state = SEEKWAIT; 735 736 fd->sc_dk.dk_seek++; 737 disk_busy(&fd->sc_dk); 738 739 timeout_add_sec(&fd->fdtimeout_to, 4); 740 return 1; 741 742 case DOIO: 743 doio: 744 type = fd->sc_type; 745 if (finfo) 746 fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) - 747 (char *)finfo; 748 sec = fd->sc_blkno % type->seccyl; 749 nblks = type->seccyl - sec; 750 nblks = min(nblks, fd->sc_bcount / fd_bsize); 751 nblks = min(nblks, FDC_MAXIOSIZE / fd_bsize); 752 fd->sc_nblks = nblks; 753 fd->sc_nbytes = finfo ? bp->b_bcount : nblks * fd_bsize; 754 head = sec / type->sectrac; 755 sec -= head * type->sectrac; 756 #ifdef DIAGNOSTIC 757 {int block; 758 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec; 759 if (block != fd->sc_blkno) { 760 panic("fdintr: block %d != blkno %llu", block, fd->sc_blkno); 761 }} 762 #endif 763 read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE; 764 isadma_start(bp->b_data + fd->sc_skip, fd->sc_nbytes, 765 fdc->sc_drq, read); 766 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 if (finfo) { 773 /* formatting */ 774 if (out_fdc(iot, ioh, NE7CMD_FORMAT) < 0) { 775 fdc->sc_errors = 4; 776 fdretry(fd); 777 goto loop; 778 } 779 out_fdc(iot, ioh, (head << 2) | fd->sc_drive); 780 out_fdc(iot, ioh, finfo->fd_formb_secshift); 781 out_fdc(iot, ioh, finfo->fd_formb_nsecs); 782 out_fdc(iot, ioh, finfo->fd_formb_gaplen); 783 out_fdc(iot, ioh, finfo->fd_formb_fillbyte); 784 } else { 785 if (read) 786 out_fdc(iot, ioh, NE7CMD_READ); /* READ */ 787 else 788 out_fdc(iot, ioh, NE7CMD_WRITE);/* WRITE */ 789 out_fdc(iot, ioh, (head << 2) | fd->sc_drive); 790 out_fdc(iot, ioh, fd->sc_cylin); /* track */ 791 out_fdc(iot, ioh, head); 792 out_fdc(iot, ioh, sec + 1); /* sec +1 */ 793 out_fdc(iot, ioh, type->secsize); /* sec size */ 794 out_fdc(iot, ioh, type->sectrac); /* secs/track */ 795 out_fdc(iot, ioh, type->gap1); /* gap1 size */ 796 out_fdc(iot, ioh, type->datalen); /* data len */ 797 } 798 fdc->sc_state = IOCOMPLETE; 799 800 disk_busy(&fd->sc_dk); 801 802 /* allow 2 seconds for operation */ 803 timeout_add_sec(&fd->fdtimeout_to, 2); 804 return 1; /* will return later */ 805 806 case SEEKWAIT: 807 timeout_del(&fd->fdtimeout_to); 808 fdc->sc_state = SEEKCOMPLETE; 809 /* allow 1/50 second for heads to settle */ 810 timeout_add_msec(&fdc->fdcpseudointr_to, 20); 811 return 1; 812 813 case SEEKCOMPLETE: 814 disk_unbusy(&fd->sc_dk, 0, 0, 0); /* no data on seek */ 815 816 /* Make sure seek really happened. */ 817 out_fdc(iot, ioh, NE7CMD_SENSEI); 818 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || 819 cyl != cylin * fd->sc_type->step) { 820 #ifdef FD_DEBUG 821 fdcstatus(&fd->sc_dev, 2, "seek failed"); 822 #endif 823 fdretry(fd); 824 goto loop; 825 } 826 fd->sc_cylin = cylin; 827 goto doio; 828 829 case IOTIMEDOUT: 830 isadma_abort(fdc->sc_drq); 831 case SEEKTIMEDOUT: 832 case RECALTIMEDOUT: 833 case RESETTIMEDOUT: 834 fdretry(fd); 835 goto loop; 836 837 case IOCOMPLETE: /* IO DONE, post-analyze */ 838 timeout_del(&fd->fdtimeout_to); 839 840 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid), 841 fd->sc_blkno, (bp->b_flags & B_READ)); 842 843 if (fdcresult(fdc) != 7 || (st0 & 0xf8) != 0) { 844 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 fdretry(fd); 852 goto loop; 853 } 854 read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE; 855 isadma_done(fdc->sc_drq); 856 if (fdc->sc_errors) { 857 diskerr(bp, "fd", "soft error", LOG_PRINTF, 858 fd->sc_skip / fd_bsize, (struct disklabel *)NULL); 859 printf("\n"); 860 fdc->sc_errors = 0; 861 } 862 863 fd->sc_blkno += fd->sc_nblks; 864 fd->sc_skip += fd->sc_nbytes; 865 fd->sc_bcount -= fd->sc_nbytes; 866 bp->b_resid -= fd->sc_nbytes; 867 if (!finfo && fd->sc_bcount > 0) { 868 cylin = fd->sc_blkno / fd->sc_type->seccyl; 869 goto doseek; 870 } 871 fdfinish(fd, bp); 872 goto loop; 873 874 case DORESET: 875 /* try a reset, keep motor on */ 876 fd_set_motor(fdc, 1); 877 delay(100); 878 fd_set_motor(fdc, 0); 879 fdc->sc_state = RESETCOMPLETE; 880 timeout_add_msec(&fd->fdtimeout_to, 500); 881 return 1; /* will return later */ 882 883 case RESETCOMPLETE: 884 timeout_del(&fd->fdtimeout_to); 885 /* clear the controller output buffer */ 886 for (i = 0; i < 4; i++) { 887 out_fdc(iot, ioh, NE7CMD_SENSEI); 888 (void) fdcresult(fdc); 889 } 890 891 /* FALLTHROUGH */ 892 case DORECAL: 893 out_fdc(iot, ioh, NE7CMD_RECAL); /* recal function */ 894 out_fdc(iot, ioh, fd->sc_drive); 895 fdc->sc_state = RECALWAIT; 896 timeout_add_sec(&fd->fdtimeout_to, 5); 897 return 1; /* will return later */ 898 899 case RECALWAIT: 900 timeout_del(&fd->fdtimeout_to); 901 fdc->sc_state = RECALCOMPLETE; 902 /* allow 1/30 second for heads to settle */ 903 timeout_add_msec(&fdc->fdcpseudointr_to, 1000 / 30); 904 return 1; /* will return later */ 905 906 case RECALCOMPLETE: 907 out_fdc(iot, ioh, NE7CMD_SENSEI); 908 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) { 909 #ifdef FD_DEBUG 910 fdcstatus(&fd->sc_dev, 2, "recalibrate failed"); 911 #endif 912 fdretry(fd); 913 goto loop; 914 } 915 fd->sc_cylin = 0; 916 goto doseek; 917 918 case MOTORWAIT: 919 if (fd->sc_flags & FD_MOTOR_WAIT) 920 return 1; /* time's not up yet */ 921 goto doseek; 922 923 default: 924 fdcstatus(&fd->sc_dev, 0, "stray interrupt"); 925 return 1; 926 } 927 #ifdef DIAGNOSTIC 928 panic("fdintr: impossible"); 929 #endif 930 #undef st0 931 #undef cyl 932 } 933 934 void 935 fdtimeout(void *arg) 936 { 937 struct fd_softc *fd = arg; 938 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent; 939 int s; 940 941 s = splbio(); 942 #ifdef DEBUG 943 log(LOG_ERR,"fdtimeout: state %d\n", fdc->sc_state); 944 #endif 945 fdcstatus(&fd->sc_dev, 0, "timeout"); 946 947 if (fd->sc_bp != NULL) 948 fdc->sc_state++; 949 else 950 fdc->sc_state = DEVIDLE; 951 952 (void) fdintr(fdc); 953 splx(s); 954 } 955 956 void 957 fdretry(struct fd_softc *fd) 958 { 959 struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent; 960 struct buf *bp = fd->sc_bp; 961 962 if (fd->sc_opts & FDOPT_NORETRY) 963 goto fail; 964 switch (fdc->sc_errors) { 965 case 0: 966 /* try again */ 967 fdc->sc_state = DOSEEK; 968 break; 969 970 case 1: case 2: case 3: 971 /* didn't work; try recalibrating */ 972 fdc->sc_state = DORECAL; 973 break; 974 975 case 4: 976 /* still no go; reset the bastard */ 977 fdc->sc_state = DORESET; 978 break; 979 980 default: 981 fail: 982 diskerr(bp, "fd", "hard error", LOG_PRINTF, 983 fd->sc_skip / FD_BSIZE(fd), (struct disklabel *)NULL); 984 printf(" (st0 %b st1 %b st2 %b cyl %d head %d sec %d)\n", 985 fdc->sc_status[0], NE7_ST0BITS, 986 fdc->sc_status[1], NE7_ST1BITS, 987 fdc->sc_status[2], NE7_ST2BITS, 988 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]); 989 990 bp->b_flags |= B_ERROR; 991 bp->b_error = EIO; 992 bp->b_resid = bp->b_bcount; 993 fdfinish(fd, bp); 994 } 995 fdc->sc_errors++; 996 } 997 998 int 999 fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 1000 { 1001 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)]; 1002 struct disklabel *lp; 1003 int error; 1004 1005 switch (cmd) { 1006 case MTIOCTOP: 1007 if (((struct mtop *)addr)->mt_op != MTOFFL) 1008 return EIO; 1009 return (0); 1010 1011 case DIOCRLDINFO: 1012 lp = malloc(sizeof(*lp), M_TEMP, M_WAITOK); 1013 fdgetdisklabel(dev, fd, lp, 0); 1014 bcopy(lp, fd->sc_dk.dk_label, sizeof(*lp)); 1015 free(lp, M_TEMP, sizeof(*lp)); 1016 return 0; 1017 1018 case DIOCGPDINFO: 1019 fdgetdisklabel(dev, fd, (struct disklabel *)addr, 1); 1020 return 0; 1021 1022 case DIOCGDINFO: 1023 *(struct disklabel *)addr = *(fd->sc_dk.dk_label); 1024 return 0; 1025 1026 case DIOCGPART: 1027 ((struct partinfo *)addr)->disklab = fd->sc_dk.dk_label; 1028 ((struct partinfo *)addr)->part = 1029 &fd->sc_dk.dk_label->d_partitions[FDPART(dev)]; 1030 return 0; 1031 1032 case DIOCWDINFO: 1033 case DIOCSDINFO: 1034 if ((flag & FWRITE) == 0) 1035 return EBADF; 1036 1037 error = setdisklabel(fd->sc_dk.dk_label, 1038 (struct disklabel *)addr, 0); 1039 if (error == 0) { 1040 if (cmd == DIOCWDINFO) 1041 error = writedisklabel(DISKLABELDEV(dev), 1042 fdstrategy, fd->sc_dk.dk_label); 1043 } 1044 return error; 1045 1046 case FD_FORM: 1047 if((flag & FWRITE) == 0) 1048 return EBADF; /* must be opened for writing */ 1049 else if(((struct fd_formb *)addr)->format_version != 1050 FD_FORMAT_VERSION) 1051 return EINVAL; /* wrong version of formatting prog */ 1052 else 1053 return fdformat(dev, (struct fd_formb *)addr, p); 1054 break; 1055 1056 case FD_GTYPE: /* get drive type */ 1057 *(struct fd_type *)addr = *fd->sc_type; 1058 return 0; 1059 1060 case FD_GOPTS: /* get drive options */ 1061 *(int *)addr = fd->sc_opts; 1062 return 0; 1063 1064 case FD_SOPTS: /* set drive options */ 1065 fd->sc_opts = *(int *)addr; 1066 return 0; 1067 1068 default: 1069 return ENOTTY; 1070 } 1071 1072 #ifdef DIAGNOSTIC 1073 panic("fdioctl: impossible"); 1074 #endif 1075 } 1076 1077 int 1078 fdformat(dev_t dev, struct fd_formb *finfo, struct proc *p) 1079 { 1080 int rv = 0; 1081 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)]; 1082 struct fd_type *type = fd->sc_type; 1083 struct buf *bp; 1084 int fd_bsize = FD_BSIZE(fd); 1085 1086 /* set up a buffer header for fdstrategy() */ 1087 bp = malloc(sizeof(*bp), M_TEMP, M_NOWAIT | M_ZERO); 1088 if (bp == NULL) 1089 return ENOBUFS; 1090 1091 bp->b_flags = B_BUSY | B_PHYS | B_FORMAT | B_RAW; 1092 bp->b_proc = p; 1093 bp->b_dev = dev; 1094 1095 /* 1096 * calculate a fake blkno, so fdstrategy() would initiate a 1097 * seek to the requested cylinder 1098 */ 1099 bp->b_blkno = (finfo->cyl * (type->sectrac * type->heads) 1100 + finfo->head * type->sectrac) * fd_bsize / DEV_BSIZE; 1101 1102 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs; 1103 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 fdstrategy(bp); 1111 1112 /* ...and wait for it to complete */ 1113 rv = biowait(bp); 1114 free(bp, M_TEMP, sizeof(*bp)); 1115 return (rv); 1116 } 1117