1 /* $NetBSD: fd.c,v 1.40 1996/11/13 06:13:42 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1993, 1994, 1995 Charles Hannum. 5 * Copyright (c) 1995 Paul Kranenburg. 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 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * @(#)fd.c 7.4 (Berkeley) 5/25/91 41 */ 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/file.h> 47 #include <sys/ioctl.h> 48 #include <sys/device.h> 49 #include <sys/disklabel.h> 50 #include <sys/dkstat.h> 51 #include <sys/disk.h> 52 #include <sys/buf.h> 53 #include <sys/uio.h> 54 #include <sys/stat.h> 55 #include <sys/syslog.h> 56 #include <sys/queue.h> 57 #include <sys/conf.h> 58 59 #include <dev/cons.h> 60 61 #include <machine/cpu.h> 62 #include <machine/autoconf.h> 63 #include <machine/conf.h> 64 65 #include <sparc/sparc/auxreg.h> 66 #include <sparc/dev/fdreg.h> 67 #include <sparc/dev/fdvar.h> 68 69 #define FDUNIT(dev) (minor(dev) / 8) 70 #define FDTYPE(dev) (minor(dev) % 8) 71 72 #define b_cylin b_resid 73 74 #define FD_DEBUG 75 #ifdef FD_DEBUG 76 int fdc_debug = 0; 77 #endif 78 79 enum fdc_state { 80 DEVIDLE = 0, 81 MOTORWAIT, 82 DOSEEK, 83 SEEKWAIT, 84 SEEKTIMEDOUT, 85 SEEKCOMPLETE, 86 DOIO, 87 IOCOMPLETE, 88 IOTIMEDOUT, 89 DORESET, 90 RESETCOMPLETE, 91 RESETTIMEDOUT, 92 DORECAL, 93 RECALWAIT, 94 RECALTIMEDOUT, 95 RECALCOMPLETE, 96 }; 97 98 /* software state, per controller */ 99 struct fdc_softc { 100 struct device sc_dev; /* boilerplate */ 101 struct intrhand sc_sih; 102 struct intrhand sc_hih; 103 caddr_t sc_reg; 104 struct fd_softc *sc_fd[4]; /* pointers to children */ 105 TAILQ_HEAD(drivehead, fd_softc) sc_drives; 106 enum fdc_state sc_state; 107 int sc_flags; 108 #define FDC_82077 0x01 109 #define FDC_NEEDHEADSETTLE 0x02 110 #define FDC_EIS 0x04 111 int sc_errors; /* number of retries so far */ 112 int sc_overruns; /* number of DMA overruns */ 113 int sc_cfg; /* current configuration */ 114 struct fdcio sc_io; 115 #define sc_reg_msr sc_io.fdcio_reg_msr 116 #define sc_reg_fifo sc_io.fdcio_reg_fifo 117 #define sc_reg_dor sc_io.fdcio_reg_dor 118 #define sc_reg_drs sc_io.fdcio_reg_msr 119 #define sc_istate sc_io.fdcio_istate 120 #define sc_data sc_io.fdcio_data 121 #define sc_tc sc_io.fdcio_tc 122 #define sc_nstat sc_io.fdcio_nstat 123 #define sc_status sc_io.fdcio_status 124 #define sc_intrcnt sc_io.fdcio_intrcnt 125 }; 126 127 #ifndef FDC_C_HANDLER 128 extern struct fdcio *fdciop; 129 #endif 130 131 /* controller driver configuration */ 132 int fdcmatch __P((struct device *, void *, void *)); 133 void fdcattach __P((struct device *, struct device *, void *)); 134 135 struct cfattach fdc_ca = { 136 sizeof(struct fdc_softc), fdcmatch, fdcattach 137 }; 138 139 struct cfdriver fdc_cd = { 140 NULL, "fdc", DV_DULL 141 }; 142 143 __inline struct fd_type *fd_dev_to_type __P((struct fd_softc *, dev_t)); 144 145 /* 146 * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how 147 * we tell them apart. 148 */ 149 struct fd_type { 150 int sectrac; /* sectors per track */ 151 int heads; /* number of heads */ 152 int seccyl; /* sectors per cylinder */ 153 int secsize; /* size code for sectors */ 154 int datalen; /* data len when secsize = 0 */ 155 int steprate; /* step rate and head unload time */ 156 int gap1; /* gap len between sectors */ 157 int gap2; /* formatting gap */ 158 int tracks; /* total num of tracks */ 159 int size; /* size of disk in sectors */ 160 int step; /* steps per cylinder */ 161 int rate; /* transfer speed code */ 162 char *name; 163 }; 164 165 /* The order of entries in the following table is important -- BEWARE! */ 166 struct fd_type fd_types[] = { 167 { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB" }, /* 1.44MB diskette */ 168 { 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS,"1.2MB" }, /* 1.2 MB AT-diskettes */ 169 { 9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS,"360KB/AT" }, /* 360kB in 1.2MB drive */ 170 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,"360KB/PC" }, /* 360kB PC diskettes */ 171 { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,"720KB" }, /* 3.5" 720kB diskette */ 172 { 9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS,"720KB/x" }, /* 720kB in 1.2MB drive */ 173 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,"360KB/x" }, /* 360kB in 720kB drive */ 174 }; 175 176 /* software state, per disk (with up to 4 disks per ctlr) */ 177 struct fd_softc { 178 struct device sc_dv; /* generic device info */ 179 struct disk sc_dk; /* generic disk info */ 180 181 struct fd_type *sc_deftype; /* default type descriptor */ 182 struct fd_type *sc_type; /* current type descriptor */ 183 184 daddr_t sc_blkno; /* starting block number */ 185 int sc_bcount; /* byte count left */ 186 int sc_skip; /* bytes already transferred */ 187 int sc_nblks; /* number of blocks currently tranferring */ 188 int sc_nbytes; /* number of bytes currently tranferring */ 189 190 int sc_drive; /* physical unit number */ 191 int sc_flags; 192 #define FD_OPEN 0x01 /* it's open */ 193 #define FD_MOTOR 0x02 /* motor should be on */ 194 #define FD_MOTOR_WAIT 0x04 /* motor coming up */ 195 int sc_cylin; /* where we think the head is */ 196 197 void *sc_sdhook; /* shutdownhook cookie */ 198 199 TAILQ_ENTRY(fd_softc) sc_drivechain; 200 int sc_ops; /* I/O ops since last switch */ 201 struct buf sc_q; /* head of buf chain */ 202 }; 203 204 /* floppy driver configuration */ 205 int fdmatch __P((struct device *, void *, void *)); 206 void fdattach __P((struct device *, struct device *, void *)); 207 208 struct cfattach fd_ca = { 209 sizeof(struct fd_softc), fdmatch, fdattach 210 }; 211 212 struct cfdriver fd_cd = { 213 NULL, "fd", DV_DISK 214 }; 215 216 void fdgetdisklabel __P((dev_t)); 217 int fd_get_parms __P((struct fd_softc *)); 218 void fdstrategy __P((struct buf *)); 219 void fdstart __P((struct fd_softc *)); 220 int fdprint __P((void *, const char *)); 221 222 struct dkdriver fddkdriver = { fdstrategy }; 223 224 struct fd_type *fd_nvtotype __P((char *, int, int)); 225 void fd_set_motor __P((struct fdc_softc *fdc)); 226 void fd_motor_off __P((void *arg)); 227 void fd_motor_on __P((void *arg)); 228 int fdcresult __P((struct fdc_softc *fdc)); 229 int out_fdc __P((struct fdc_softc *fdc, u_char x)); 230 void fdcstart __P((struct fdc_softc *fdc)); 231 void fdcstatus __P((struct device *dv, int n, char *s)); 232 void fdc_reset __P((struct fdc_softc *fdc)); 233 void fdctimeout __P((void *arg)); 234 void fdcpseudointr __P((void *arg)); 235 #ifdef FDC_C_HANDLER 236 int fdchwintr __P((struct fdc_softc *)); 237 #else 238 void fdchwintr __P((void)); 239 #endif 240 int fdcswintr __P((struct fdc_softc *)); 241 void fdcretry __P((struct fdc_softc *fdc)); 242 void fdfinish __P((struct fd_softc *fd, struct buf *bp)); 243 void fd_do_eject __P((void)); 244 void fd_mountroot_hook __P((struct device *)); 245 static void fdconf __P((struct fdc_softc *)); 246 247 #if PIL_FDSOFT == 4 248 #define IE_FDSOFT IE_L4 249 #else 250 #error 4 251 #endif 252 253 #define OBP_FDNAME (CPU_ISSUN4M ? "SUNW,fdtwo" : "fd") 254 255 int 256 fdcmatch(parent, match, aux) 257 struct device *parent; 258 void *match, *aux; 259 { 260 register struct confargs *ca = aux; 261 register struct romaux *ra = &ca->ca_ra; 262 263 /* 264 * Floppy doesn't exist on sun4. 265 */ 266 if (CPU_ISSUN4) 267 return (0); 268 269 /* 270 * Floppy controller is on mainbus on sun4c. 271 */ 272 if ((CPU_ISSUN4C) && (ca->ca_bustype != BUS_MAIN)) 273 return (0); 274 275 /* 276 * Floppy controller is on obio on sun4m. 277 */ 278 if ((CPU_ISSUN4M) && (ca->ca_bustype != BUS_OBIO)) 279 return (0); 280 281 /* Sun PROMs call the controller an "fd" or "SUNW,fdtwo" */ 282 if (strcmp(OBP_FDNAME, ra->ra_name)) 283 return (0); 284 285 if (ca->ca_ra.ra_vaddr && 286 probeget(ca->ca_ra.ra_vaddr, 1) == -1) { 287 return (0); 288 } 289 290 return (1); 291 } 292 293 /* 294 * Arguments passed between fdcattach and fdprobe. 295 */ 296 struct fdc_attach_args { 297 int fa_drive; 298 struct bootpath *fa_bootpath; 299 struct fd_type *fa_deftype; 300 }; 301 302 /* 303 * Print the location of a disk drive (called just before attaching the 304 * the drive). If `fdc' is not NULL, the drive was found but was not 305 * in the system config file; print the drive name as well. 306 * Return QUIET (config_find ignores this if the device was configured) to 307 * avoid printing `fdN not configured' messages. 308 */ 309 int 310 fdprint(aux, fdc) 311 void *aux; 312 const char *fdc; 313 { 314 register struct fdc_attach_args *fa = aux; 315 316 if (!fdc) 317 printf(" drive %d", fa->fa_drive); 318 return QUIET; 319 } 320 321 static void 322 fdconf(fdc) 323 struct fdc_softc *fdc; 324 { 325 int vroom; 326 327 if (out_fdc(fdc, NE7CMD_DUMPREG) || fdcresult(fdc) != 10) 328 return; 329 330 /* 331 * dumpreg[7] seems to be a motor-off timeout; set it to whatever 332 * the PROM thinks is appropriate. 333 */ 334 if ((vroom = fdc->sc_status[7]) == 0) 335 vroom = 0x64; 336 337 /* Configure controller to use FIFO and Implied Seek */ 338 out_fdc(fdc, NE7CMD_CFG); 339 out_fdc(fdc, vroom); 340 out_fdc(fdc, fdc->sc_cfg); 341 out_fdc(fdc, 0); /* PRETRK */ 342 /* No result phase */ 343 } 344 345 void 346 fdcattach(parent, self, aux) 347 struct device *parent, *self; 348 void *aux; 349 { 350 register struct confargs *ca = aux; 351 struct fdc_softc *fdc = (void *)self; 352 struct fdc_attach_args fa; 353 struct bootpath *bp; 354 int pri; 355 char code; 356 357 if (ca->ca_ra.ra_vaddr) 358 fdc->sc_reg = (caddr_t)ca->ca_ra.ra_vaddr; 359 else 360 fdc->sc_reg = (caddr_t)mapiodev(ca->ca_ra.ra_reg, 0, 361 ca->ca_ra.ra_len, 362 ca->ca_bustype); 363 364 fdc->sc_state = DEVIDLE; 365 fdc->sc_istate = ISTATE_IDLE; 366 fdc->sc_flags |= FDC_EIS; 367 TAILQ_INIT(&fdc->sc_drives); 368 369 pri = ca->ca_ra.ra_intr[0].int_pri; 370 #ifdef FDC_C_HANDLER 371 fdc->sc_hih.ih_fun = (void *)fdchwintr; 372 fdc->sc_hih.ih_arg = fdc; 373 intr_establish(pri, &fdc->sc_hih); 374 #else 375 fdciop = &fdc->sc_io; 376 intr_fasttrap(pri, fdchwintr); 377 #endif 378 fdc->sc_sih.ih_fun = (void *)fdcswintr; 379 fdc->sc_sih.ih_arg = fdc; 380 intr_establish(PIL_FDSOFT, &fdc->sc_sih); 381 382 /* Assume a 82077 */ 383 fdc->sc_reg_msr = &((struct fdreg_77 *)fdc->sc_reg)->fd_msr; 384 fdc->sc_reg_fifo = &((struct fdreg_77 *)fdc->sc_reg)->fd_fifo; 385 fdc->sc_reg_dor = &((struct fdreg_77 *)fdc->sc_reg)->fd_dor; 386 387 code = '7'; 388 if (*fdc->sc_reg_dor == NE7_RQM) { 389 /* 390 * This hack from Chris Torek: apparently DOR really 391 * addresses MSR/DRS on a 82072. 392 * We used to rely on the VERSION command to tell the 393 * difference (which did not work). 394 */ 395 *fdc->sc_reg_dor = FDC_250KBPS; 396 if (*fdc->sc_reg_dor == NE7_RQM) 397 code = '2'; 398 } 399 if (code == '7') { 400 fdc->sc_flags |= FDC_82077; 401 } else { 402 fdc->sc_reg_msr = &((struct fdreg_72 *)fdc->sc_reg)->fd_msr; 403 fdc->sc_reg_fifo = &((struct fdreg_72 *)fdc->sc_reg)->fd_fifo; 404 fdc->sc_reg_dor = 0; 405 } 406 407 #ifdef FD_DEBUG 408 if (out_fdc(fdc, NE7CMD_VERSION) == 0 && 409 fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x90) { 410 if (fdc_debug) 411 printf("[version cmd]"); 412 } 413 #endif 414 415 /* 416 * Configure controller; enable FIFO, Implied seek, no POLL mode?. 417 * Note: CFG_EFIFO is active-low, initial threshold value: 8 418 */ 419 fdc->sc_cfg = CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|(8 & CFG_THRHLD_MASK); 420 fdconf(fdc); 421 422 if (fdc->sc_flags & FDC_82077) { 423 /* Lock configuration across soft resets. */ 424 out_fdc(fdc, NE7CMD_LOCK | CFG_LOCK); 425 if (fdcresult(fdc) != 1) 426 printf(" CFGLOCK: unexpected response"); 427 } 428 429 evcnt_attach(&fdc->sc_dev, "intr", &fdc->sc_intrcnt); 430 431 printf(" pri %d, softpri %d: chip 8207%c\n", pri, PIL_FDSOFT, code); 432 433 /* 434 * Controller and drives are represented by one and the same 435 * Openprom node, so we can as well check for the floppy boots here. 436 */ 437 fa.fa_bootpath = 0; 438 if ((bp = ca->ca_ra.ra_bp) && strcmp(bp->name, OBP_FDNAME) == 0) { 439 440 switch (ca->ca_bustype) { 441 case BUS_MAIN: 442 /* 443 * We can get the bootpath in several different 444 * formats! The faked v1 bootpath looks like /fd@0,0. 445 * The v2 bootpath is either just /fd0, in which case 446 * `bp->val[0]' will have been set to -1, or /fd@x,y 447 * where <x,y> is the prom address specifier. 448 */ 449 if (((bp->val[0] == ca->ca_ra.ra_iospace) && 450 (bp->val[1] == (int)ca->ca_ra.ra_paddr)) || 451 452 ((bp->val[0] == -1) && /* v2: /fd0 */ 453 (bp->val[1] == 0)) || 454 455 ((bp->val[0] == 0) && /* v1: /fd@0,0 */ 456 (bp->val[1] == 0)) 457 ) 458 fa.fa_bootpath = bp; 459 break; 460 461 case BUS_OBIO: 462 /* 463 * floppy controller on obio (such as on the sun4m), 464 * e.g.: `/obio0/SUNW,fdtwo@0,700000'. 465 * We use "slot, offset" to determine if this is the 466 * right one. 467 */ 468 if ((bp->val[0] == ca->ca_slot) && 469 (bp->val[1] == ca->ca_offset)) 470 fa.fa_bootpath = bp; 471 break; 472 } 473 474 } 475 476 /* physical limit: four drives per controller. */ 477 for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) { 478 fa.fa_deftype = NULL; /* unknown */ 479 fa.fa_deftype = &fd_types[0]; /* XXX */ 480 (void)config_found(self, (void *)&fa, fdprint); 481 } 482 483 bootpath_store(1, NULL); 484 } 485 486 int 487 fdmatch(parent, match, aux) 488 struct device *parent; 489 void *match, *aux; 490 { 491 struct fdc_softc *fdc = (void *)parent; 492 struct fdc_attach_args *fa = aux; 493 int drive = fa->fa_drive; 494 int n, ok; 495 496 if (drive > 0) 497 /* XXX - for now, punt > 1 drives */ 498 return 0; 499 500 if (fdc->sc_flags & FDC_82077) { 501 /* select drive and turn on motor */ 502 *fdc->sc_reg_dor = drive | FDO_FRST | FDO_MOEN(drive); 503 /* wait for motor to spin up */ 504 delay(250000); 505 } else { 506 auxregbisc(AUXIO_FDS, 0); 507 } 508 fdc->sc_nstat = 0; 509 out_fdc(fdc, NE7CMD_RECAL); 510 out_fdc(fdc, drive); 511 /* wait for recalibrate */ 512 for (n = 0; n < 10000; n++) { 513 delay(1000); 514 if ((*fdc->sc_reg_msr & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) { 515 /* wait a bit longer till device *really* is ready */ 516 delay(100000); 517 if (out_fdc(fdc, NE7CMD_SENSEI)) 518 break; 519 if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80) 520 /* 521 * Got `invalid command'; we interpret it 522 * to mean that the re-calibrate hasn't in 523 * fact finished yet 524 */ 525 continue; 526 break; 527 } 528 } 529 n = fdc->sc_nstat; 530 #ifdef FD_DEBUG 531 if (fdc_debug) { 532 int i; 533 printf("fdprobe: %d stati:", n); 534 for (i = 0; i < n; i++) 535 printf(" %x", fdc->sc_status[i]); 536 printf("\n"); 537 } 538 #endif 539 ok = (n == 2 && (fdc->sc_status[0] & 0xf8) == 0x20) ? 1 : 0; 540 541 /* turn off motor */ 542 if (fdc->sc_flags & FDC_82077) { 543 /* select drive and turn on motor */ 544 *fdc->sc_reg_dor = FDO_FRST; 545 } else { 546 auxregbisc(0, AUXIO_FDS); 547 } 548 549 return ok; 550 } 551 552 /* 553 * Controller is working, and drive responded. Attach it. 554 */ 555 void 556 fdattach(parent, self, aux) 557 struct device *parent, *self; 558 void *aux; 559 { 560 struct fdc_softc *fdc = (void *)parent; 561 struct fd_softc *fd = (void *)self; 562 struct fdc_attach_args *fa = aux; 563 struct fd_type *type = fa->fa_deftype; 564 int drive = fa->fa_drive; 565 566 /* XXX Allow `flags' to override device type? */ 567 568 if (type) 569 printf(": %s %d cyl, %d head, %d sec\n", type->name, 570 type->tracks, type->heads, type->sectrac); 571 else 572 printf(": density unknown\n"); 573 574 fd->sc_cylin = -1; 575 fd->sc_drive = drive; 576 fd->sc_deftype = type; 577 fdc->sc_fd[drive] = fd; 578 579 /* 580 * Initialize and attach the disk structure. 581 */ 582 fd->sc_dk.dk_name = fd->sc_dv.dv_xname; 583 fd->sc_dk.dk_driver = &fddkdriver; 584 disk_attach(&fd->sc_dk); 585 586 /* 587 * We're told if we're the boot device in fdcattach(). 588 */ 589 if (fa->fa_bootpath) 590 fa->fa_bootpath->dev = &fd->sc_dv; 591 592 /* 593 * Establish a mountroot_hook anyway in case we booted 594 * with RB_ASKNAME and get selected as the boot device. 595 */ 596 mountroot_hook_establish(fd_mountroot_hook, &fd->sc_dv); 597 598 /* Make sure the drive motor gets turned off at shutdown time. */ 599 fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd); 600 601 /* XXX Need to do some more fiddling with sc_dk. */ 602 dk_establish(&fd->sc_dk, &fd->sc_dv); 603 } 604 605 __inline struct fd_type * 606 fd_dev_to_type(fd, dev) 607 struct fd_softc *fd; 608 dev_t dev; 609 { 610 int type = FDTYPE(dev); 611 612 if (type > (sizeof(fd_types) / sizeof(fd_types[0]))) 613 return NULL; 614 return type ? &fd_types[type - 1] : fd->sc_deftype; 615 } 616 617 void 618 fdstrategy(bp) 619 register struct buf *bp; /* IO operation to perform */ 620 { 621 struct fd_softc *fd; 622 int unit = FDUNIT(bp->b_dev); 623 int sz; 624 int s; 625 626 /* Valid unit, controller, and request? */ 627 if (unit >= fd_cd.cd_ndevs || 628 (fd = fd_cd.cd_devs[unit]) == 0 || 629 bp->b_blkno < 0 || 630 (bp->b_bcount % FDC_BSIZE) != 0) { 631 bp->b_error = EINVAL; 632 goto bad; 633 } 634 635 /* If it's a null transfer, return immediately. */ 636 if (bp->b_bcount == 0) 637 goto done; 638 639 sz = howmany(bp->b_bcount, FDC_BSIZE); 640 641 if (bp->b_blkno + sz > fd->sc_type->size) { 642 sz = fd->sc_type->size - bp->b_blkno; 643 if (sz == 0) { 644 /* If exactly at end of disk, return EOF. */ 645 bp->b_resid = bp->b_bcount; 646 goto done; 647 } 648 if (sz < 0) { 649 /* If past end of disk, return EINVAL. */ 650 bp->b_error = EINVAL; 651 goto bad; 652 } 653 /* Otherwise, truncate request. */ 654 bp->b_bcount = sz << DEV_BSHIFT; 655 } 656 657 bp->b_cylin = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl; 658 659 #ifdef FD_DEBUG 660 if (fdc_debug > 1) 661 printf("fdstrategy: b_blkno %d b_bcount %ld blkno %d cylin %ld\n", 662 bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylin); 663 #endif 664 665 /* Queue transfer on drive, activate drive and controller if idle. */ 666 s = splbio(); 667 disksort(&fd->sc_q, bp); 668 untimeout(fd_motor_off, fd); /* a good idea */ 669 if (!fd->sc_q.b_active) 670 fdstart(fd); 671 #ifdef DIAGNOSTIC 672 else { 673 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent; 674 if (fdc->sc_state == DEVIDLE) { 675 printf("fdstrategy: controller inactive\n"); 676 fdcstart(fdc); 677 } 678 } 679 #endif 680 splx(s); 681 return; 682 683 bad: 684 bp->b_flags |= B_ERROR; 685 done: 686 /* Toss transfer; we're done early. */ 687 biodone(bp); 688 } 689 690 void 691 fdstart(fd) 692 struct fd_softc *fd; 693 { 694 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent; 695 int active = fdc->sc_drives.tqh_first != 0; 696 697 /* Link into controller queue. */ 698 fd->sc_q.b_active = 1; 699 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain); 700 701 /* If controller not already active, start it. */ 702 if (!active) 703 fdcstart(fdc); 704 } 705 706 void 707 fdfinish(fd, bp) 708 struct fd_softc *fd; 709 struct buf *bp; 710 { 711 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent; 712 713 /* 714 * Move this drive to the end of the queue to give others a `fair' 715 * chance. We only force a switch if N operations are completed while 716 * another drive is waiting to be serviced, since there is a long motor 717 * startup delay whenever we switch. 718 */ 719 if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) { 720 fd->sc_ops = 0; 721 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain); 722 if (bp->b_actf) { 723 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain); 724 } else 725 fd->sc_q.b_active = 0; 726 } 727 bp->b_resid = fd->sc_bcount; 728 fd->sc_skip = 0; 729 fd->sc_q.b_actf = bp->b_actf; 730 731 biodone(bp); 732 /* turn off motor 5s from now */ 733 timeout(fd_motor_off, fd, 5 * hz); 734 fdc->sc_state = DEVIDLE; 735 } 736 737 void 738 fdc_reset(fdc) 739 struct fdc_softc *fdc; 740 { 741 if (fdc->sc_flags & FDC_82077) { 742 *fdc->sc_reg_dor = FDO_MOEN(0); 743 } 744 745 *fdc->sc_reg_drs = DRS_RESET; 746 delay(10); 747 *fdc->sc_reg_drs = 0; 748 #ifdef FD_DEBUG 749 if (fdc_debug) 750 printf("fdc reset\n"); 751 #endif 752 } 753 754 void 755 fd_set_motor(fdc) 756 struct fdc_softc *fdc; 757 { 758 struct fd_softc *fd; 759 u_char status; 760 int n; 761 762 if (fdc->sc_flags & FDC_82077) { 763 status = FDO_FRST | FDO_FDMAEN; 764 if ((fd = fdc->sc_drives.tqh_first) != NULL) 765 status |= fd->sc_drive; 766 767 for (n = 0; n < 4; n++) 768 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR)) 769 status |= FDO_MOEN(n); 770 *fdc->sc_reg_dor = status; 771 } else { 772 int on = 0; 773 774 for (n = 0; n < 4; n++) 775 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR)) 776 on = 1; 777 if (on) { 778 auxregbisc(AUXIO_FDS, 0); 779 } else { 780 auxregbisc(0, AUXIO_FDS); 781 } 782 } 783 } 784 785 void 786 fd_motor_off(arg) 787 void *arg; 788 { 789 struct fd_softc *fd = arg; 790 int s; 791 792 s = splbio(); 793 fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT); 794 fd_set_motor((struct fdc_softc *)fd->sc_dv.dv_parent); 795 splx(s); 796 } 797 798 void 799 fd_motor_on(arg) 800 void *arg; 801 { 802 struct fd_softc *fd = arg; 803 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent; 804 int s; 805 806 s = splbio(); 807 fd->sc_flags &= ~FD_MOTOR_WAIT; 808 if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT)) 809 (void) fdcswintr(fdc); 810 splx(s); 811 } 812 813 int 814 fdcresult(fdc) 815 struct fdc_softc *fdc; 816 { 817 u_char i; 818 int j = 100000, 819 n = 0; 820 821 for (; j; j--) { 822 i = *fdc->sc_reg_msr & (NE7_DIO | NE7_RQM | NE7_CB); 823 if (i == NE7_RQM) 824 return (fdc->sc_nstat = n); 825 if (i == (NE7_DIO | NE7_RQM | NE7_CB)) { 826 if (n >= sizeof(fdc->sc_status)) { 827 log(LOG_ERR, "fdcresult: overrun\n"); 828 return -1; 829 } 830 fdc->sc_status[n++] = *fdc->sc_reg_fifo; 831 } 832 } 833 log(LOG_ERR, "fdcresult: timeout\n"); 834 return (fdc->sc_nstat = -1); 835 } 836 837 int 838 out_fdc(fdc, x) 839 struct fdc_softc *fdc; 840 u_char x; 841 { 842 int i = 100000; 843 844 while (((*fdc->sc_reg_msr & (NE7_DIO|NE7_RQM)) != NE7_RQM) && i-- > 0); 845 if (i <= 0) 846 return -1; 847 848 *fdc->sc_reg_fifo = x; 849 return 0; 850 } 851 852 int 853 fdopen(dev, flags, fmt, p) 854 dev_t dev; 855 int flags, fmt; 856 struct proc *p; 857 { 858 int unit, pmask; 859 struct fd_softc *fd; 860 struct fd_type *type; 861 862 unit = FDUNIT(dev); 863 if (unit >= fd_cd.cd_ndevs) 864 return ENXIO; 865 fd = fd_cd.cd_devs[unit]; 866 if (fd == 0) 867 return ENXIO; 868 type = fd_dev_to_type(fd, dev); 869 if (type == NULL) 870 return ENXIO; 871 872 if ((fd->sc_flags & FD_OPEN) != 0 && 873 fd->sc_type != type) 874 return EBUSY; 875 876 fd->sc_type = type; 877 fd->sc_cylin = -1; 878 fd->sc_flags |= FD_OPEN; 879 880 /* 881 * Only update the disklabel if we're not open anywhere else. 882 */ 883 if (fd->sc_dk.dk_openmask == 0) 884 fdgetdisklabel(dev); 885 886 pmask = (1 << DISKPART(dev)); 887 888 switch (fmt) { 889 case S_IFCHR: 890 fd->sc_dk.dk_copenmask |= pmask; 891 break; 892 893 case S_IFBLK: 894 fd->sc_dk.dk_bopenmask |= pmask; 895 break; 896 } 897 fd->sc_dk.dk_openmask = 898 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask; 899 900 return 0; 901 } 902 903 int 904 fdclose(dev, flags, fmt, p) 905 dev_t dev; 906 int flags, fmt; 907 struct proc *p; 908 { 909 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)]; 910 int pmask = (1 << DISKPART(dev)); 911 912 fd->sc_flags &= ~FD_OPEN; 913 914 switch (fmt) { 915 case S_IFCHR: 916 fd->sc_dk.dk_copenmask &= ~pmask; 917 break; 918 919 case S_IFBLK: 920 fd->sc_dk.dk_bopenmask &= ~pmask; 921 break; 922 } 923 fd->sc_dk.dk_openmask = 924 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask; 925 926 return 0; 927 } 928 929 int 930 fdread(dev, uio, flag) 931 dev_t dev; 932 struct uio *uio; 933 int flag; 934 { 935 936 return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio)); 937 } 938 939 int 940 fdwrite(dev, uio, flag) 941 dev_t dev; 942 struct uio *uio; 943 int flag; 944 { 945 946 return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio)); 947 } 948 949 void 950 fdcstart(fdc) 951 struct fdc_softc *fdc; 952 { 953 954 #ifdef DIAGNOSTIC 955 /* only got here if controller's drive queue was inactive; should 956 be in idle state */ 957 if (fdc->sc_state != DEVIDLE) { 958 printf("fdcstart: not idle\n"); 959 return; 960 } 961 #endif 962 (void) fdcswintr(fdc); 963 } 964 965 void 966 fdcstatus(dv, n, s) 967 struct device *dv; 968 int n; 969 char *s; 970 { 971 struct fdc_softc *fdc = (void *)dv->dv_parent; 972 char bits[64]; 973 #if 0 974 /* 975 * A 82072 seems to return <invalid command> on 976 * gratuitous Sense Interrupt commands. 977 */ 978 if (n == 0 && (fdc->sc_flags & FDC_82077)) { 979 out_fdc(fdc, NE7CMD_SENSEI); 980 (void) fdcresult(fdc); 981 n = 2; 982 } 983 #endif 984 985 /* Just print last status */ 986 n = fdc->sc_nstat; 987 988 printf("%s: %s: state %d", dv->dv_xname, s, fdc->sc_state); 989 990 switch (n) { 991 case 0: 992 printf("\n"); 993 break; 994 case 2: 995 printf(" (st0 %s cyl %d)\n", 996 bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS, 997 bits, sizeof(bits)), fdc->sc_status[1]); 998 break; 999 case 7: 1000 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0], 1001 NE7_ST0BITS, bits, sizeof(bits))); 1002 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1], 1003 NE7_ST1BITS, bits, sizeof(bits))); 1004 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2], 1005 NE7_ST2BITS, bits, sizeof(bits))); 1006 printf(" cyl %d head %d sec %d)\n", 1007 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]); 1008 break; 1009 #ifdef DIAGNOSTIC 1010 default: 1011 printf(" fdcstatus: weird size: %d\n", n); 1012 break; 1013 #endif 1014 } 1015 } 1016 1017 void 1018 fdctimeout(arg) 1019 void *arg; 1020 { 1021 struct fdc_softc *fdc = arg; 1022 struct fd_softc *fd = fdc->sc_drives.tqh_first; 1023 int s; 1024 1025 s = splbio(); 1026 fdcstatus(&fd->sc_dv, 0, "timeout"); 1027 1028 if (fd->sc_q.b_actf) 1029 fdc->sc_state++; 1030 else 1031 fdc->sc_state = DEVIDLE; 1032 1033 (void) fdcswintr(fdc); 1034 splx(s); 1035 } 1036 1037 void 1038 fdcpseudointr(arg) 1039 void *arg; 1040 { 1041 struct fdc_softc *fdc = arg; 1042 int s; 1043 1044 /* Just ensure it has the right spl. */ 1045 s = splbio(); 1046 (void) fdcswintr(fdc); 1047 splx(s); 1048 } 1049 1050 1051 #ifdef FDC_C_HANDLER 1052 /* 1053 * hardware interrupt entry point: must be converted to `fast' 1054 * (in-window) handler. 1055 */ 1056 int 1057 fdchwintr(fdc) 1058 struct fdc_softc *fdc; 1059 { 1060 struct buf *bp; 1061 int read; 1062 1063 switch (fdc->sc_istate) { 1064 case ISTATE_SENSEI: 1065 out_fdc(fdc, NE7CMD_SENSEI); 1066 fdcresult(fdc); 1067 fdc->sc_istate = ISTATE_IDLE; 1068 ienab_bis(IE_FDSOFT); 1069 return 1; 1070 case ISTATE_IDLE: 1071 case ISTATE_SPURIOUS: 1072 auxregbisc(0, AUXIO_FDS); /* Does this help? */ 1073 fdcresult(fdc); 1074 fdc->sc_istate = ISTATE_SPURIOUS; 1075 printf("fdc: stray hard interrupt... "); 1076 ienab_bis(IE_FDSOFT); 1077 return 1; 1078 case ISTATE_DMA: 1079 break; 1080 default: 1081 printf("fdc: goofed ...\n"); 1082 return 1; 1083 } 1084 1085 read = bp->b_flags & B_READ; 1086 for (;;) { 1087 register int msr; 1088 1089 msr = *fdc->sc_reg_msr; 1090 1091 if ((msr & NE7_RQM) == 0) 1092 break; 1093 1094 if ((msr & NE7_NDM) == 0) { 1095 fdcresult(fdc); 1096 fdc->sc_istate = ISTATE_IDLE; 1097 ienab_bis(IE_FDSOFT); 1098 printf("fdc: overrun: tc = %d\n", fdc->sc_tc); 1099 break; 1100 } 1101 1102 if (msr & NE7_DIO) { 1103 #ifdef DIAGNOSTIC 1104 if (!read) 1105 printf("fdxfer: false read\n"); 1106 #endif 1107 *fdc->sc_data++ = *fdc->sc_reg_fifo; 1108 } else { 1109 #ifdef DIAGNOSTIC 1110 if (read) 1111 printf("fdxfer: false write\n"); 1112 #endif 1113 *fdc->sc_reg_fifo = *fdc->sc_data++; 1114 } 1115 if (--fdc->sc_tc == 0) { 1116 auxregbisc(AUXIO_FTC, 0); 1117 fdc->sc_istate = ISTATE_IDLE; 1118 delay(10); 1119 auxregbisc(0, AUXIO_FTC); 1120 fdcresult(fdc); 1121 ienab_bis(IE_FDSOFT); 1122 break; 1123 } 1124 } 1125 return 1; 1126 } 1127 #endif 1128 1129 int 1130 fdcswintr(fdc) 1131 struct fdc_softc *fdc; 1132 { 1133 #define st0 fdc->sc_status[0] 1134 #define st1 fdc->sc_status[1] 1135 #define cyl fdc->sc_status[1] 1136 #define OUT_FDC(fdc, c, s) \ 1137 do { if (out_fdc(fdc, (c))) { (fdc)->sc_state = (s); goto loop; } } while(0) 1138 1139 struct fd_softc *fd; 1140 struct buf *bp; 1141 int read, head, sec, nblks; 1142 struct fd_type *type; 1143 1144 1145 if (fdc->sc_istate != ISTATE_IDLE) { 1146 /* Trouble... */ 1147 printf("fdc: spurious interrupt: state %d, istate=%d\n", 1148 fdc->sc_state, fdc->sc_istate); 1149 fdc->sc_istate = ISTATE_IDLE; 1150 if (fdc->sc_state == RESETCOMPLETE || 1151 fdc->sc_state == RESETTIMEDOUT) { 1152 panic("fdcintr: spurious interrupt can't be cleared"); 1153 } 1154 goto doreset; 1155 } 1156 1157 loop: 1158 /* Is there a drive for the controller to do a transfer with? */ 1159 fd = fdc->sc_drives.tqh_first; 1160 if (fd == NULL) { 1161 fdc->sc_state = DEVIDLE; 1162 return 0; 1163 } 1164 1165 /* Is there a transfer to this drive? If not, deactivate drive. */ 1166 bp = fd->sc_q.b_actf; 1167 if (bp == NULL) { 1168 fd->sc_ops = 0; 1169 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain); 1170 fd->sc_q.b_active = 0; 1171 goto loop; 1172 } 1173 1174 switch (fdc->sc_state) { 1175 case DEVIDLE: 1176 fdc->sc_errors = 0; 1177 fd->sc_skip = 0; 1178 fd->sc_bcount = bp->b_bcount; 1179 fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE); 1180 untimeout(fd_motor_off, fd); 1181 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) { 1182 fdc->sc_state = MOTORWAIT; 1183 return 1; 1184 } 1185 if ((fd->sc_flags & FD_MOTOR) == 0) { 1186 /* Turn on the motor, being careful about pairing. */ 1187 struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1]; 1188 if (ofd && ofd->sc_flags & FD_MOTOR) { 1189 untimeout(fd_motor_off, ofd); 1190 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT); 1191 } 1192 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT; 1193 fd_set_motor(fdc); 1194 fdc->sc_state = MOTORWAIT; 1195 if (fdc->sc_flags & FDC_82077) { /* XXX */ 1196 /* Allow .25s for motor to stabilize. */ 1197 timeout(fd_motor_on, fd, hz / 4); 1198 } else { 1199 fd->sc_flags &= ~FD_MOTOR_WAIT; 1200 goto loop; 1201 } 1202 return 1; 1203 } 1204 /* Make sure the right drive is selected. */ 1205 fd_set_motor(fdc); 1206 1207 /* fall through */ 1208 case DOSEEK: 1209 doseek: 1210 if (fdc->sc_flags & FDC_EIS) { 1211 fd->sc_cylin = bp->b_cylin; 1212 /* We use implied seek */ 1213 goto doio; 1214 } 1215 1216 if (fd->sc_cylin == bp->b_cylin) 1217 goto doio; 1218 1219 /* specify command */ 1220 OUT_FDC(fdc, NE7CMD_SPECIFY, SEEKTIMEDOUT); 1221 OUT_FDC(fdc, fd->sc_type->steprate, SEEKTIMEDOUT); 1222 OUT_FDC(fdc, 6, SEEKTIMEDOUT); /* XXX head load time == 6ms */ 1223 1224 fdc->sc_istate = ISTATE_SENSEI; 1225 /* seek function */ 1226 OUT_FDC(fdc, NE7CMD_SEEK, SEEKTIMEDOUT); 1227 OUT_FDC(fdc, fd->sc_drive, SEEKTIMEDOUT); /* drive number */ 1228 OUT_FDC(fdc, bp->b_cylin * fd->sc_type->step, SEEKTIMEDOUT); 1229 1230 fd->sc_cylin = -1; 1231 fdc->sc_state = SEEKWAIT; 1232 fdc->sc_nstat = 0; 1233 1234 fd->sc_dk.dk_seek++; 1235 disk_busy(&fd->sc_dk); 1236 1237 timeout(fdctimeout, fdc, 4 * hz); 1238 return 1; 1239 1240 case DOIO: 1241 doio: 1242 type = fd->sc_type; 1243 sec = fd->sc_blkno % type->seccyl; 1244 nblks = type->seccyl - sec; 1245 nblks = min(nblks, fd->sc_bcount / FDC_BSIZE); 1246 nblks = min(nblks, FDC_MAXIOSIZE / FDC_BSIZE); 1247 fd->sc_nblks = nblks; 1248 fd->sc_nbytes = nblks * FDC_BSIZE; 1249 head = sec / type->sectrac; 1250 sec -= head * type->sectrac; 1251 #ifdef DIAGNOSTIC 1252 {int block; 1253 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec; 1254 if (block != fd->sc_blkno) { 1255 printf("fdcintr: block %d != blkno %d\n", block, fd->sc_blkno); 1256 #ifdef DDB 1257 Debugger(); 1258 #endif 1259 }} 1260 #endif 1261 read = bp->b_flags & B_READ; 1262 1263 /* Setup for pseudo DMA */ 1264 fdc->sc_data = bp->b_data + fd->sc_skip; 1265 fdc->sc_tc = fd->sc_nbytes; 1266 1267 *fdc->sc_reg_drs = type->rate; 1268 #ifdef FD_DEBUG 1269 if (fdc_debug > 1) 1270 printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n", 1271 read ? "read" : "write", fd->sc_drive, 1272 fd->sc_cylin, head, sec, nblks); 1273 #endif 1274 fdc->sc_state = IOCOMPLETE; 1275 fdc->sc_istate = ISTATE_DMA; 1276 fdc->sc_nstat = 0; 1277 if (read) 1278 OUT_FDC(fdc, NE7CMD_READ, IOTIMEDOUT); /* READ */ 1279 else 1280 OUT_FDC(fdc, NE7CMD_WRITE, IOTIMEDOUT); /* WRITE */ 1281 OUT_FDC(fdc, (head << 2) | fd->sc_drive, IOTIMEDOUT); 1282 OUT_FDC(fdc, fd->sc_cylin, IOTIMEDOUT); /* track */ 1283 OUT_FDC(fdc, head, IOTIMEDOUT); 1284 OUT_FDC(fdc, sec + 1, IOTIMEDOUT); /* sector +1 */ 1285 OUT_FDC(fdc, type->secsize, IOTIMEDOUT);/* sector size */ 1286 OUT_FDC(fdc, type->sectrac, IOTIMEDOUT);/* sectors/track */ 1287 OUT_FDC(fdc, type->gap1, IOTIMEDOUT); /* gap1 size */ 1288 OUT_FDC(fdc, type->datalen, IOTIMEDOUT);/* data length */ 1289 1290 disk_busy(&fd->sc_dk); 1291 1292 /* allow 2 seconds for operation */ 1293 timeout(fdctimeout, fdc, 2 * hz); 1294 return 1; /* will return later */ 1295 1296 case SEEKWAIT: 1297 untimeout(fdctimeout, fdc); 1298 fdc->sc_state = SEEKCOMPLETE; 1299 if (fdc->sc_flags & FDC_NEEDHEADSETTLE) { 1300 /* allow 1/50 second for heads to settle */ 1301 timeout(fdcpseudointr, fdc, hz / 50); 1302 return 1; /* will return later */ 1303 } 1304 1305 case SEEKCOMPLETE: 1306 disk_unbusy(&fd->sc_dk, 0); /* no data on seek */ 1307 1308 /* Make sure seek really happened. */ 1309 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || 1310 cyl != bp->b_cylin * fd->sc_type->step) { 1311 #ifdef FD_DEBUG 1312 if (fdc_debug) 1313 fdcstatus(&fd->sc_dv, 2, "seek failed"); 1314 #endif 1315 fdcretry(fdc); 1316 goto loop; 1317 } 1318 fd->sc_cylin = bp->b_cylin; 1319 goto doio; 1320 1321 case IOTIMEDOUT: 1322 auxregbisc(AUXIO_FTC, 0); 1323 delay(10); 1324 auxregbisc(0, AUXIO_FTC); 1325 (void)fdcresult(fdc); 1326 case SEEKTIMEDOUT: 1327 case RECALTIMEDOUT: 1328 case RESETTIMEDOUT: 1329 fdcretry(fdc); 1330 goto loop; 1331 1332 case IOCOMPLETE: /* IO DONE, post-analyze */ 1333 untimeout(fdctimeout, fdc); 1334 1335 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid)); 1336 1337 if (fdc->sc_nstat != 7 || (st0 & 0xf8) != 0 || st1 != 0) { 1338 #ifdef FD_DEBUG 1339 if (fdc_debug) { 1340 fdcstatus(&fd->sc_dv, 7, 1341 bp->b_flags & B_READ 1342 ? "read failed" : "write failed"); 1343 printf("blkno %d nblks %d tc %d\n", 1344 fd->sc_blkno, fd->sc_nblks, fdc->sc_tc); 1345 } 1346 #endif 1347 if (fdc->sc_nstat == 7 && 1348 (st1 & ST1_OVERRUN) == ST1_OVERRUN) { 1349 1350 /* 1351 * Silently retry overruns if no other 1352 * error bit is set. Adjust threshold. 1353 */ 1354 int thr = fdc->sc_cfg & CFG_THRHLD_MASK; 1355 if (thr < 15) { 1356 thr++; 1357 fdc->sc_cfg &= ~CFG_THRHLD_MASK; 1358 fdc->sc_cfg |= (thr & CFG_THRHLD_MASK); 1359 #ifdef FD_DEBUG 1360 if (fdc_debug) 1361 printf("fdc: %d -> threshold\n", thr); 1362 #endif 1363 fdconf(fdc); 1364 fdc->sc_overruns = 0; 1365 } 1366 if (++fdc->sc_overruns < 3) { 1367 fdc->sc_state = DOIO; 1368 goto loop; 1369 } 1370 } 1371 fdcretry(fdc); 1372 goto loop; 1373 } 1374 if (fdc->sc_errors) { 1375 diskerr(bp, "fd", "soft error", LOG_PRINTF, 1376 fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL); 1377 printf("\n"); 1378 fdc->sc_errors = 0; 1379 } else { 1380 if (--fdc->sc_overruns < -20) { 1381 int thr = fdc->sc_cfg & CFG_THRHLD_MASK; 1382 if (thr > 0) { 1383 thr--; 1384 fdc->sc_cfg &= ~CFG_THRHLD_MASK; 1385 fdc->sc_cfg |= (thr & CFG_THRHLD_MASK); 1386 #ifdef FD_DEBUG 1387 if (fdc_debug) 1388 printf("fdc: %d -> threshold\n", thr); 1389 #endif 1390 fdconf(fdc); 1391 } 1392 fdc->sc_overruns = 0; 1393 } 1394 } 1395 fd->sc_blkno += fd->sc_nblks; 1396 fd->sc_skip += fd->sc_nbytes; 1397 fd->sc_bcount -= fd->sc_nbytes; 1398 if (fd->sc_bcount > 0) { 1399 bp->b_cylin = fd->sc_blkno / fd->sc_type->seccyl; 1400 goto doseek; 1401 } 1402 fdfinish(fd, bp); 1403 goto loop; 1404 1405 case DORESET: 1406 doreset: 1407 /* try a reset, keep motor on */ 1408 fd_set_motor(fdc); 1409 delay(100); 1410 fdc_reset(fdc); 1411 fdc->sc_nstat = 0; 1412 fdc->sc_istate = ISTATE_SENSEI; 1413 fdc->sc_state = RESETCOMPLETE; 1414 timeout(fdctimeout, fdc, hz / 2); 1415 return 1; /* will return later */ 1416 1417 case RESETCOMPLETE: 1418 untimeout(fdctimeout, fdc); 1419 fdconf(fdc); 1420 1421 /* fall through */ 1422 case DORECAL: 1423 fdc->sc_state = RECALWAIT; 1424 fdc->sc_istate = ISTATE_SENSEI; 1425 fdc->sc_nstat = 0; 1426 /* recalibrate function */ 1427 OUT_FDC(fdc, NE7CMD_RECAL, RECALTIMEDOUT); 1428 OUT_FDC(fdc, fd->sc_drive, RECALTIMEDOUT); 1429 timeout(fdctimeout, fdc, 5 * hz); 1430 return 1; /* will return later */ 1431 1432 case RECALWAIT: 1433 untimeout(fdctimeout, fdc); 1434 fdc->sc_state = RECALCOMPLETE; 1435 if (fdc->sc_flags & FDC_NEEDHEADSETTLE) { 1436 /* allow 1/30 second for heads to settle */ 1437 timeout(fdcpseudointr, fdc, hz / 30); 1438 return 1; /* will return later */ 1439 } 1440 1441 case RECALCOMPLETE: 1442 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) { 1443 #ifdef FD_DEBUG 1444 if (fdc_debug) 1445 fdcstatus(&fd->sc_dv, 2, "recalibrate failed"); 1446 #endif 1447 fdcretry(fdc); 1448 goto loop; 1449 } 1450 fd->sc_cylin = 0; 1451 goto doseek; 1452 1453 case MOTORWAIT: 1454 if (fd->sc_flags & FD_MOTOR_WAIT) 1455 return 1; /* time's not up yet */ 1456 goto doseek; 1457 1458 default: 1459 fdcstatus(&fd->sc_dv, 0, "stray interrupt"); 1460 return 1; 1461 } 1462 #ifdef DIAGNOSTIC 1463 panic("fdcintr: impossible"); 1464 #endif 1465 #undef st0 1466 #undef st1 1467 #undef cyl 1468 } 1469 1470 void 1471 fdcretry(fdc) 1472 struct fdc_softc *fdc; 1473 { 1474 char bits[64]; 1475 struct fd_softc *fd; 1476 struct buf *bp; 1477 1478 fd = fdc->sc_drives.tqh_first; 1479 bp = fd->sc_q.b_actf; 1480 1481 fdc->sc_overruns = 0; 1482 1483 switch (fdc->sc_errors) { 1484 case 0: 1485 /* try again */ 1486 fdc->sc_state = 1487 (fdc->sc_flags & FDC_EIS) ? DOIO : DOSEEK; 1488 break; 1489 1490 case 1: case 2: case 3: 1491 /* didn't work; try recalibrating */ 1492 fdc->sc_state = DORECAL; 1493 break; 1494 1495 case 4: 1496 /* still no go; reset the bastard */ 1497 fdc->sc_state = DORESET; 1498 break; 1499 1500 default: 1501 diskerr(bp, "fd", "hard error", LOG_PRINTF, 1502 fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL); 1503 1504 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0], 1505 NE7_ST0BITS, bits, sizeof(bits))); 1506 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1], 1507 NE7_ST1BITS, bits, sizeof(bits))); 1508 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2], 1509 NE7_ST2BITS, bits, sizeof(bits))); 1510 printf(" cyl %d head %d sec %d)\n", 1511 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]); 1512 1513 bp->b_flags |= B_ERROR; 1514 bp->b_error = EIO; 1515 fdfinish(fd, bp); 1516 } 1517 fdc->sc_errors++; 1518 } 1519 1520 int 1521 fdsize(dev) 1522 dev_t dev; 1523 { 1524 1525 /* Swapping to floppies would not make sense. */ 1526 return -1; 1527 } 1528 1529 int 1530 fddump(dev, blkno, va, size) 1531 dev_t dev; 1532 daddr_t blkno; 1533 caddr_t va; 1534 size_t size; 1535 { 1536 1537 /* Not implemented. */ 1538 return EINVAL; 1539 } 1540 1541 int 1542 fdioctl(dev, cmd, addr, flag, p) 1543 dev_t dev; 1544 u_long cmd; 1545 caddr_t addr; 1546 int flag; 1547 struct proc *p; 1548 { 1549 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)]; 1550 int error; 1551 1552 switch (cmd) { 1553 case DIOCGDINFO: 1554 *(struct disklabel *)addr = *(fd->sc_dk.dk_label); 1555 return 0; 1556 1557 case DIOCWLABEL: 1558 if ((flag & FWRITE) == 0) 1559 return EBADF; 1560 /* XXX do something */ 1561 return 0; 1562 1563 case DIOCWDINFO: 1564 if ((flag & FWRITE) == 0) 1565 return EBADF; 1566 1567 error = setdisklabel(fd->sc_dk.dk_label, 1568 (struct disklabel *)addr, 0, 1569 fd->sc_dk.dk_cpulabel); 1570 if (error) 1571 return error; 1572 1573 error = writedisklabel(dev, fdstrategy, 1574 fd->sc_dk.dk_label, 1575 fd->sc_dk.dk_cpulabel); 1576 return error; 1577 1578 case DIOCLOCK: 1579 /* 1580 * Nothing to do here, really. 1581 */ 1582 return 0; 1583 1584 case DIOCEJECT: 1585 fd_do_eject(); 1586 return 0; 1587 1588 #ifdef DEBUG 1589 case _IO('f', 100): 1590 { 1591 int i; 1592 struct fdc_softc *fdc = (struct fdc_softc *) 1593 fd->sc_dv.dv_parent; 1594 1595 out_fdc(fdc, NE7CMD_DUMPREG); 1596 fdcresult(fdc); 1597 printf("dumpreg(%d regs): <", fdc->sc_nstat); 1598 for (i = 0; i < fdc->sc_nstat; i++) 1599 printf(" %x", fdc->sc_status[i]); 1600 printf(">\n"); 1601 } 1602 1603 return 0; 1604 case _IOW('f', 101, int): 1605 ((struct fdc_softc *)fd->sc_dv.dv_parent)->sc_cfg &= 1606 ~CFG_THRHLD_MASK; 1607 ((struct fdc_softc *)fd->sc_dv.dv_parent)->sc_cfg |= 1608 (*(int *)addr & CFG_THRHLD_MASK); 1609 fdconf((struct fdc_softc *) fd->sc_dv.dv_parent); 1610 return 0; 1611 case _IO('f', 102): 1612 { 1613 int i; 1614 struct fdc_softc *fdc = (struct fdc_softc *) 1615 fd->sc_dv.dv_parent; 1616 out_fdc(fdc, NE7CMD_SENSEI); 1617 fdcresult(fdc); 1618 printf("sensei(%d regs): <", fdc->sc_nstat); 1619 for (i=0; i< fdc->sc_nstat; i++) 1620 printf(" 0x%x", fdc->sc_status[i]); 1621 } 1622 printf(">\n"); 1623 return 0; 1624 #endif 1625 default: 1626 return ENOTTY; 1627 } 1628 1629 #ifdef DIAGNOSTIC 1630 panic("fdioctl: impossible"); 1631 #endif 1632 } 1633 1634 void 1635 fdgetdisklabel(dev) 1636 dev_t dev; 1637 { 1638 int unit = FDUNIT(dev), i; 1639 struct fd_softc *fd = fd_cd.cd_devs[unit]; 1640 struct disklabel *lp = fd->sc_dk.dk_label; 1641 struct cpu_disklabel *clp = fd->sc_dk.dk_cpulabel; 1642 1643 bzero(lp, sizeof(struct disklabel)); 1644 bzero(lp, sizeof(struct cpu_disklabel)); 1645 1646 lp->d_type = DTYPE_FLOPPY; 1647 lp->d_secsize = FDC_BSIZE; 1648 lp->d_secpercyl = fd->sc_type->seccyl; 1649 lp->d_nsectors = fd->sc_type->sectrac; 1650 lp->d_ncylinders = fd->sc_type->tracks; 1651 lp->d_ntracks = fd->sc_type->heads; /* Go figure... */ 1652 lp->d_rpm = 3600; /* XXX like it matters... */ 1653 1654 strncpy(lp->d_typename, "floppy", sizeof(lp->d_typename)); 1655 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname)); 1656 lp->d_interleave = 1; 1657 1658 lp->d_partitions[RAW_PART].p_offset = 0; 1659 lp->d_partitions[RAW_PART].p_size = lp->d_secpercyl * lp->d_ncylinders; 1660 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED; 1661 lp->d_npartitions = RAW_PART + 1; 1662 1663 lp->d_magic = DISKMAGIC; 1664 lp->d_magic2 = DISKMAGIC; 1665 lp->d_checksum = dkcksum(lp); 1666 1667 /* 1668 * Call the generic disklabel extraction routine. If there's 1669 * not a label there, fake it. 1670 */ 1671 if (readdisklabel(dev, fdstrategy, lp, clp) != NULL) { 1672 strncpy(lp->d_packname, "default label", 1673 sizeof(lp->d_packname)); 1674 /* 1675 * Reset the partition info; it might have gotten 1676 * trashed in readdisklabel(). 1677 * 1678 * XXX Why do we have to do this? readdisklabel() 1679 * should be safe... 1680 */ 1681 for (i = 0; i < MAXPARTITIONS; ++i) { 1682 lp->d_partitions[i].p_offset = 0; 1683 if (i == RAW_PART) { 1684 lp->d_partitions[i].p_size = 1685 lp->d_secpercyl * lp->d_ncylinders; 1686 lp->d_partitions[i].p_fstype = FS_BSDFFS; 1687 } else { 1688 lp->d_partitions[i].p_size = 0; 1689 lp->d_partitions[i].p_fstype = FS_UNUSED; 1690 } 1691 } 1692 lp->d_npartitions = RAW_PART + 1; 1693 } 1694 } 1695 1696 void 1697 fd_do_eject() 1698 { 1699 1700 auxregbisc(AUXIO_FDS, AUXIO_FEJ); 1701 delay(10); 1702 auxregbisc(AUXIO_FEJ, AUXIO_FDS); 1703 } 1704 1705 #ifdef RAMDISK_HOOKS 1706 int fd_read_rd_image __P((size_t *, caddr_t *)); 1707 #endif 1708 1709 /* ARGSUSED */ 1710 void 1711 fd_mountroot_hook(dev) 1712 struct device *dev; 1713 { 1714 int c; 1715 1716 fd_do_eject(); 1717 printf("Insert filesystem floppy and press return."); 1718 for (;;) { 1719 c = cngetc(); 1720 if ((c == '\r') || (c == '\n')) { 1721 printf("\n"); 1722 break; 1723 } 1724 } 1725 #ifdef RAMDISK_HOOKS 1726 { 1727 extern int (*rd_read_image) __P((size_t *, caddr_t *)); 1728 rd_read_image = fd_read_rd_image; 1729 } 1730 #endif 1731 } 1732 1733 #ifdef RAMDISK_HOOKS 1734 #include <sys/malloc.h> 1735 #include <sys/proc.h> 1736 1737 #define FDMICROROOTSIZE ((2*18*80) << DEV_BSHIFT) 1738 1739 int 1740 fd_read_rd_image(sizep, addrp) 1741 size_t *sizep; 1742 caddr_t *addrp; 1743 { 1744 struct buf buf, *bp = &buf; 1745 dev_t dev; 1746 off_t offset; 1747 caddr_t addr; 1748 1749 dev = makedev(54,0); /* XXX */ 1750 1751 MALLOC(addr, caddr_t, FDMICROROOTSIZE, M_DEVBUF, M_WAITOK); 1752 *addrp = addr; 1753 1754 if (fdopen(dev, 0, S_IFCHR, NULL)) 1755 panic("fd: mountroot: fdopen"); 1756 1757 offset = 0; 1758 1759 for (;;) { 1760 bp->b_dev = dev; 1761 bp->b_error = 0; 1762 bp->b_resid = 0; 1763 bp->b_proc = NULL; 1764 bp->b_flags = B_BUSY | B_PHYS | B_RAW | B_READ; 1765 bp->b_blkno = btodb(offset); 1766 bp->b_bcount = DEV_BSIZE; 1767 bp->b_data = addr; 1768 fdstrategy(bp); 1769 while ((bp->b_flags & B_DONE) == 0) { 1770 tsleep((caddr_t)bp, PRIBIO + 1, "physio", 0); 1771 } 1772 if (bp->b_error) 1773 panic("fd: mountroot: fdread error %d", bp->b_error); 1774 1775 if (bp->b_resid != 0) 1776 break; 1777 1778 addr += DEV_BSIZE; 1779 offset += DEV_BSIZE; 1780 if (offset + DEV_BSIZE > FDMICROROOTSIZE) 1781 break; 1782 } 1783 (void)fdclose(dev, 0, S_IFCHR, NULL); 1784 *sizep = offset; 1785 fd_do_eject(); 1786 return 0; 1787 } 1788 #endif 1789