1 /* $NetBSD: fd.c,v 1.68 1999/11/21 15:23:01 pk Exp $ */ 2 3 /*- 4 * Copyright (c) 1993, 1994, 1995 Charles M. 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 "opt_ddb.h" 44 #include "opt_md.h" 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/kernel.h> 49 #include <sys/file.h> 50 #include <sys/ioctl.h> 51 #include <sys/device.h> 52 #include <sys/disklabel.h> 53 #include <sys/dkstat.h> 54 #include <sys/disk.h> 55 #include <sys/fdio.h> 56 #include <sys/buf.h> 57 #include <sys/malloc.h> 58 #include <sys/proc.h> 59 #include <sys/uio.h> 60 #include <sys/stat.h> 61 #include <sys/syslog.h> 62 #include <sys/queue.h> 63 #include <sys/conf.h> 64 65 #include <dev/cons.h> 66 67 #include <vm/vm.h> 68 69 #include <uvm/uvm_extern.h> 70 71 #include <machine/cpu.h> 72 #include <machine/autoconf.h> 73 #include <machine/conf.h> 74 75 #include <sparc/sparc/auxreg.h> 76 #include <sparc/dev/fdreg.h> 77 #include <sparc/dev/fdvar.h> 78 79 #define FDUNIT(dev) (minor(dev) / 8) 80 #define FDTYPE(dev) (minor(dev) % 8) 81 82 /* XXX misuse a flag to identify format operation */ 83 #define B_FORMAT B_XXX 84 #define b_cylin b_resid 85 86 #define FD_DEBUG 87 #ifdef FD_DEBUG 88 int fdc_debug = 0; 89 #endif 90 91 enum fdc_state { 92 DEVIDLE = 0, 93 MOTORWAIT, 94 DOSEEK, 95 SEEKWAIT, 96 SEEKTIMEDOUT, 97 SEEKCOMPLETE, 98 DOIO, 99 IOCOMPLETE, 100 IOTIMEDOUT, 101 DORESET, 102 RESETCOMPLETE, 103 RESETTIMEDOUT, 104 DORECAL, 105 RECALWAIT, 106 RECALTIMEDOUT, 107 RECALCOMPLETE, 108 }; 109 110 /* software state, per controller */ 111 struct fdc_softc { 112 struct device sc_dev; /* boilerplate */ 113 bus_space_tag_t sc_bustag; 114 caddr_t sc_reg; 115 struct fd_softc *sc_fd[4]; /* pointers to children */ 116 TAILQ_HEAD(drivehead, fd_softc) sc_drives; 117 enum fdc_state sc_state; 118 int sc_flags; 119 #define FDC_82077 0x01 120 #define FDC_NEEDHEADSETTLE 0x02 121 #define FDC_EIS 0x04 122 int sc_errors; /* number of retries so far */ 123 int sc_overruns; /* number of DMA overruns */ 124 int sc_cfg; /* current configuration */ 125 struct fdcio sc_io; 126 #define sc_reg_msr sc_io.fdcio_reg_msr 127 #define sc_reg_fifo sc_io.fdcio_reg_fifo 128 #define sc_reg_dor sc_io.fdcio_reg_dor 129 #define sc_reg_drs sc_io.fdcio_reg_msr 130 #define sc_istate sc_io.fdcio_istate 131 #define sc_data sc_io.fdcio_data 132 #define sc_tc sc_io.fdcio_tc 133 #define sc_nstat sc_io.fdcio_nstat 134 #define sc_status sc_io.fdcio_status 135 #define sc_intrcnt sc_io.fdcio_intrcnt 136 }; 137 138 #ifndef FDC_C_HANDLER 139 extern struct fdcio *fdciop; 140 #endif 141 142 /* controller driver configuration */ 143 int fdcmatch_mainbus __P((struct device *, struct cfdata *, void *)); 144 int fdcmatch_obio __P((struct device *, struct cfdata *, void *)); 145 void fdcattach_mainbus __P((struct device *, struct device *, void *)); 146 void fdcattach_obio __P((struct device *, struct device *, void *)); 147 148 void fdcattach __P((struct fdc_softc *, int, struct bootpath *)); 149 150 struct cfattach fdc_mainbus_ca = { 151 sizeof(struct fdc_softc), fdcmatch_mainbus, fdcattach_mainbus 152 }; 153 struct cfattach fdc_obio_ca = { 154 sizeof(struct fdc_softc), fdcmatch_obio, fdcattach_obio 155 }; 156 157 __inline struct fd_type *fd_dev_to_type __P((struct fd_softc *, dev_t)); 158 159 /* 160 * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how 161 * we tell them apart. 162 */ 163 struct fd_type { 164 int sectrac; /* sectors per track */ 165 int heads; /* number of heads */ 166 int seccyl; /* sectors per cylinder */ 167 int secsize; /* size code for sectors */ 168 int datalen; /* data len when secsize = 0 */ 169 int steprate; /* step rate and head unload time */ 170 int gap1; /* gap len between sectors */ 171 int gap2; /* formatting gap */ 172 int cylinders; /* total num of cylinders */ 173 int size; /* size of disk in sectors */ 174 int step; /* steps per cylinder */ 175 int rate; /* transfer speed code */ 176 int fillbyte; /* format fill byte */ 177 int interleave; /* interleave factor (formatting) */ 178 char *name; 179 }; 180 181 /* The order of entries in the following table is important -- BEWARE! */ 182 struct fd_type fd_types[] = { 183 { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB" }, /* 1.44MB diskette */ 184 { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB" }, /* 3.5" 720kB diskette */ 185 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,0xf6,1, "360KB/x" }, /* 360kB in 720kB drive */ 186 { 8,2,16,3,0xff,0xdf,0x35,0x74,77,1232,1,FDC_500KBPS,0xf6,1, "1.2MB/NEC" } /* 1.2 MB japanese format */ 187 }; 188 189 /* software state, per disk (with up to 4 disks per ctlr) */ 190 struct fd_softc { 191 struct device sc_dv; /* generic device info */ 192 struct disk sc_dk; /* generic disk info */ 193 194 struct fd_type *sc_deftype; /* default type descriptor */ 195 struct fd_type *sc_type; /* current type descriptor */ 196 197 daddr_t sc_blkno; /* starting block number */ 198 int sc_bcount; /* byte count left */ 199 int sc_skip; /* bytes already transferred */ 200 int sc_nblks; /* number of blocks currently tranferring */ 201 int sc_nbytes; /* number of bytes currently tranferring */ 202 203 int sc_drive; /* physical unit number */ 204 int sc_flags; 205 #define FD_OPEN 0x01 /* it's open */ 206 #define FD_MOTOR 0x02 /* motor should be on */ 207 #define FD_MOTOR_WAIT 0x04 /* motor coming up */ 208 int sc_cylin; /* where we think the head is */ 209 int sc_opts; /* user-set options */ 210 211 void *sc_sdhook; /* shutdownhook cookie */ 212 213 TAILQ_ENTRY(fd_softc) sc_drivechain; 214 int sc_ops; /* I/O ops since last switch */ 215 struct buf sc_q; /* head of buf chain */ 216 }; 217 218 /* floppy driver configuration */ 219 int fdmatch __P((struct device *, struct cfdata *, void *)); 220 void fdattach __P((struct device *, struct device *, void *)); 221 222 struct cfattach fd_ca = { 223 sizeof(struct fd_softc), fdmatch, fdattach 224 }; 225 226 extern struct cfdriver fd_cd; 227 228 void fdgetdisklabel __P((dev_t)); 229 int fd_get_parms __P((struct fd_softc *)); 230 void fdstrategy __P((struct buf *)); 231 void fdstart __P((struct fd_softc *)); 232 int fdprint __P((void *, const char *)); 233 234 struct dkdriver fddkdriver = { fdstrategy }; 235 236 struct fd_type *fd_nvtotype __P((char *, int, int)); 237 void fd_set_motor __P((struct fdc_softc *fdc)); 238 void fd_motor_off __P((void *arg)); 239 void fd_motor_on __P((void *arg)); 240 int fdcresult __P((struct fdc_softc *fdc)); 241 int out_fdc __P((struct fdc_softc *fdc, u_char x)); 242 void fdcstart __P((struct fdc_softc *fdc)); 243 void fdcstatus __P((struct device *dv, int n, char *s)); 244 void fdc_reset __P((struct fdc_softc *fdc)); 245 void fdctimeout __P((void *arg)); 246 void fdcpseudointr __P((void *arg)); 247 #ifdef FDC_C_HANDLER 248 int fdchwintr __P((void *)); 249 #else 250 void fdchwintr __P((void)); 251 #endif 252 int fdcswintr __P((void *)); 253 int fdcstate __P((struct fdc_softc *)); 254 void fdcretry __P((struct fdc_softc *fdc)); 255 void fdfinish __P((struct fd_softc *fd, struct buf *bp)); 256 int fdformat __P((dev_t, struct ne7_fd_formb *, struct proc *)); 257 void fd_do_eject __P((struct fd_softc *)); 258 void fd_mountroot_hook __P((struct device *)); 259 static void fdconf __P((struct fdc_softc *)); 260 261 #if PIL_FDSOFT == 4 262 #define IE_FDSOFT IE_L4 263 #else 264 #error 4 265 #endif 266 267 #ifdef FDC_C_HANDLER 268 #if defined(SUN4M) 269 #define FD_SET_SWINTR do { \ 270 if (CPU_ISSUN4M) \ 271 raise(0, PIL_FDSOFT); \ 272 else \ 273 ienab_bis(IE_L4); \ 274 } while(0) 275 #else 276 #define AUDIO_SET_SWINTR ienab_bis(IE_FDSOFT) 277 #endif /* defined(SUN4M) */ 278 #endif /* FDC_C_HANDLER */ 279 280 #define OBP_FDNAME (CPU_ISSUN4M ? "SUNW,fdtwo" : "fd") 281 282 int 283 fdcmatch_mainbus(parent, match, aux) 284 struct device *parent; 285 struct cfdata *match; 286 void *aux; 287 { 288 struct mainbus_attach_args *ma = aux; 289 290 /* 291 * Floppy controller is on mainbus on sun4c. 292 */ 293 if (!CPU_ISSUN4C) 294 return (0); 295 296 /* sun4c PROMs call the controller "fd" */ 297 if (strcmp("fd", ma->ma_name) != 0) 298 return (0); 299 300 return (bus_space_probe(ma->ma_bustag, 301 ma->ma_iospace, 302 ma->ma_paddr, 303 1, /* probe size */ 304 0, /* offset */ 305 0, /* flags */ 306 NULL, NULL)); 307 } 308 309 int 310 fdcmatch_obio(parent, match, aux) 311 struct device *parent; 312 struct cfdata *match; 313 void *aux; 314 { 315 union obio_attach_args *uoba = aux; 316 struct sbus_attach_args *sa; 317 318 /* 319 * Floppy controller is on obio on sun4m. 320 */ 321 if (uoba->uoba_isobio4 != 0) 322 return (0); 323 324 sa = &uoba->uoba_sbus; 325 326 /* sun4m PROMs call the controller "SUNW,fdtwo" */ 327 if (strcmp("SUNW,fdtwo", sa->sa_name) != 0) 328 return (0); 329 330 return (bus_space_probe(sa->sa_bustag, sa->sa_slot, sa->sa_offset, 331 1, /* probe size */ 332 0, /* offset */ 333 0, /* flags */ 334 NULL, NULL)); 335 } 336 337 /* 338 * Arguments passed between fdcattach and fdprobe. 339 */ 340 struct fdc_attach_args { 341 int fa_drive; 342 struct bootpath *fa_bootpath; 343 struct fd_type *fa_deftype; 344 }; 345 346 /* 347 * Print the location of a disk drive (called just before attaching the 348 * the drive). If `fdc' is not NULL, the drive was found but was not 349 * in the system config file; print the drive name as well. 350 * Return QUIET (config_find ignores this if the device was configured) to 351 * avoid printing `fdN not configured' messages. 352 */ 353 int 354 fdprint(aux, fdc) 355 void *aux; 356 const char *fdc; 357 { 358 register struct fdc_attach_args *fa = aux; 359 360 if (!fdc) 361 printf(" drive %d", fa->fa_drive); 362 return (QUIET); 363 } 364 365 static void 366 fdconf(fdc) 367 struct fdc_softc *fdc; 368 { 369 int vroom; 370 371 if (out_fdc(fdc, NE7CMD_DUMPREG) || fdcresult(fdc) != 10) 372 return; 373 374 /* 375 * dumpreg[7] seems to be a motor-off timeout; set it to whatever 376 * the PROM thinks is appropriate. 377 */ 378 if ((vroom = fdc->sc_status[7]) == 0) 379 vroom = 0x64; 380 381 /* Configure controller to use FIFO and Implied Seek */ 382 out_fdc(fdc, NE7CMD_CFG); 383 out_fdc(fdc, vroom); 384 out_fdc(fdc, fdc->sc_cfg); 385 out_fdc(fdc, 0); /* PRETRK */ 386 /* No result phase */ 387 } 388 389 /* 390 * Controller and drives are represented by one and the same 391 * Openprom node, so we can as well check for the floppy boots here. 392 */ 393 394 void 395 fdcattach_mainbus(parent, self, aux) 396 struct device *parent, *self; 397 void *aux; 398 { 399 struct fdc_softc *fdc = (void *)self; 400 struct mainbus_attach_args *ma = aux; 401 struct bootpath *bp; 402 403 fdc->sc_bustag = ma->ma_bustag; 404 405 if (ma->ma_promvaddr != 0) 406 fdc->sc_reg = (caddr_t)ma->ma_promvaddr; 407 else { 408 bus_space_handle_t bh; 409 if (bus_space_map2( 410 ma->ma_bustag, 411 ma->ma_iospace, 412 ma->ma_paddr, 413 ma->ma_size, 414 BUS_SPACE_MAP_LINEAR, 415 0, 416 &bh) != 0) { 417 printf("%s: cannot map registers\n", self->dv_xname); 418 return; 419 } 420 fdc->sc_reg = (caddr_t)bh; 421 } 422 423 bp = NULL; 424 if (ma->ma_bp != NULL && strcmp(ma->ma_bp->name, OBP_FDNAME) == 0) { 425 int v0 = ma->ma_bp->val[0]; 426 int v1 = ma->ma_bp->val[1]; 427 /* 428 * We can get the bootpath in several different 429 * formats! The faked v1 bootpath looks like /fd@0,0. 430 * The v2 bootpath is either just /fd0, in which case 431 * `bp->val[0]' will have been set to -1, or /fd@x,y 432 * where <x,y> is the prom address specifier. 433 */ 434 if (((v0 == ma->ma_iospace) && (v1 == (int)ma->ma_paddr)) || 435 ((v0 == -1) && (v1 == 0)) || /* v2: /fd0 */ 436 ((v0 == 0) && (v1 == 0)) /* v1: /fd@0,0 */ ) 437 bp = ma->ma_bp; 438 } 439 440 fdcattach(fdc, ma->ma_pri, bp); 441 } 442 443 void 444 fdcattach_obio(parent, self, aux) 445 struct device *parent, *self; 446 void *aux; 447 { 448 struct fdc_softc *fdc = (void *)self; 449 union obio_attach_args *uoba = aux; 450 struct sbus_attach_args *sa = &uoba->uoba_sbus; 451 struct bootpath *bp; 452 453 fdc->sc_bustag = sa->sa_bustag; 454 455 if (sa->sa_npromvaddrs != 0) 456 fdc->sc_reg = (caddr_t)sa->sa_promvaddrs[0]; 457 else { 458 bus_space_handle_t bh; 459 460 if (sbus_bus_map(sa->sa_bustag, sa->sa_slot, 461 sa->sa_offset, 462 sa->sa_size, 463 BUS_SPACE_MAP_LINEAR, 464 0, 465 &bh) != 0) { 466 printf("%s: cannot map control registers\n", 467 self->dv_xname); 468 return; 469 } 470 fdc->sc_reg = (caddr_t)bh; 471 } 472 473 bp = NULL; 474 if (sa->sa_bp != NULL && strcmp(sa->sa_bp->name, OBP_FDNAME) == 0) { 475 int v0 = sa->sa_bp->val[0]; 476 int v1 = sa->sa_bp->val[1]; 477 /* 478 * floppy controller on obio (such as on the sun4m), 479 * e.g.: `/obio0/SUNW,fdtwo@0,700000'. 480 * We use "slot, offset" to determine if this is the 481 * right one. 482 */ 483 if ((v0 != sa->sa_slot) || (v1 != sa->sa_offset)) 484 bp = sa->sa_bp; 485 } 486 487 if (sa->sa_nintr != 0) 488 fdcattach(fdc, sa->sa_pri, bp); 489 } 490 491 void 492 fdcattach(fdc, pri, bp) 493 struct fdc_softc *fdc; 494 int pri; 495 struct bootpath *bp; 496 { 497 struct fdc_attach_args fa; 498 char code; 499 500 fdc->sc_state = DEVIDLE; 501 fdc->sc_istate = ISTATE_IDLE; 502 fdc->sc_flags |= FDC_EIS; 503 TAILQ_INIT(&fdc->sc_drives); 504 505 #ifdef FDC_C_HANDLER 506 (void)bus_intr_establish(fdc->sc_bustag, pri, 0, 507 fdchwintr, fdc); 508 #else 509 fdciop = &fdc->sc_io; 510 (void)bus_intr_establish(fdc->sc_bustag, pri, 511 BUS_INTR_ESTABLISH_FASTTRAP, 512 (int (*) __P((void *)))fdchwintr, NULL); 513 #endif 514 (void)bus_intr_establish(fdc->sc_bustag, PIL_FDSOFT, 515 BUS_INTR_ESTABLISH_SOFTINTR, 516 fdcswintr, fdc); 517 518 /* Assume a 82077 */ 519 fdc->sc_reg_msr = &((struct fdreg_77 *)fdc->sc_reg)->fd_msr; 520 fdc->sc_reg_fifo = &((struct fdreg_77 *)fdc->sc_reg)->fd_fifo; 521 fdc->sc_reg_dor = &((struct fdreg_77 *)fdc->sc_reg)->fd_dor; 522 523 code = '7'; 524 if (*fdc->sc_reg_dor == NE7_RQM) { 525 /* 526 * This hack from Chris Torek: apparently DOR really 527 * addresses MSR/DRS on a 82072. 528 * We used to rely on the VERSION command to tell the 529 * difference (which did not work). 530 */ 531 *fdc->sc_reg_dor = FDC_250KBPS; 532 if (*fdc->sc_reg_dor == NE7_RQM) 533 code = '2'; 534 } 535 if (code == '7') { 536 fdc->sc_flags |= FDC_82077; 537 } else { 538 fdc->sc_reg_msr = &((struct fdreg_72 *)fdc->sc_reg)->fd_msr; 539 fdc->sc_reg_fifo = &((struct fdreg_72 *)fdc->sc_reg)->fd_fifo; 540 fdc->sc_reg_dor = 0; 541 } 542 543 #ifdef FD_DEBUG 544 if (out_fdc(fdc, NE7CMD_VERSION) == 0 && 545 fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x90) { 546 if (fdc_debug) 547 printf("[version cmd]"); 548 } 549 #endif 550 551 /* 552 * Configure controller; enable FIFO, Implied seek, no POLL mode?. 553 * Note: CFG_EFIFO is active-low, initial threshold value: 8 554 */ 555 fdc->sc_cfg = CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|(8 & CFG_THRHLD_MASK); 556 fdconf(fdc); 557 558 if (fdc->sc_flags & FDC_82077) { 559 /* Lock configuration across soft resets. */ 560 out_fdc(fdc, NE7CMD_LOCK | CFG_LOCK); 561 if (fdcresult(fdc) != 1) 562 printf(" CFGLOCK: unexpected response"); 563 } 564 565 evcnt_attach(&fdc->sc_dev, "intr", &fdc->sc_intrcnt); 566 567 printf(" softpri %d: chip 8207%c\n", PIL_FDSOFT, code); 568 569 fa.fa_bootpath = bp; 570 571 /* physical limit: four drives per controller. */ 572 for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) { 573 fa.fa_deftype = NULL; /* unknown */ 574 fa.fa_deftype = &fd_types[0]; /* XXX */ 575 (void)config_found(&fdc->sc_dev, (void *)&fa, fdprint); 576 } 577 578 bootpath_store(1, NULL); 579 } 580 581 int 582 fdmatch(parent, match, aux) 583 struct device *parent; 584 struct cfdata *match; 585 void *aux; 586 { 587 struct fdc_softc *fdc = (void *)parent; 588 struct fdc_attach_args *fa = aux; 589 int drive = fa->fa_drive; 590 int n, ok; 591 592 if (drive > 0) 593 /* XXX - for now, punt on more than one drive */ 594 return (0); 595 596 if (fdc->sc_flags & FDC_82077) { 597 /* select drive and turn on motor */ 598 *fdc->sc_reg_dor = drive | FDO_FRST | FDO_MOEN(drive); 599 /* wait for motor to spin up */ 600 delay(250000); 601 } else { 602 auxregbisc(AUXIO4C_FDS, 0); 603 } 604 fdc->sc_nstat = 0; 605 out_fdc(fdc, NE7CMD_RECAL); 606 out_fdc(fdc, drive); 607 /* wait for recalibrate */ 608 for (n = 0; n < 10000; n++) { 609 delay(1000); 610 if ((*fdc->sc_reg_msr & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) { 611 /* wait a bit longer till device *really* is ready */ 612 delay(100000); 613 if (out_fdc(fdc, NE7CMD_SENSEI)) 614 break; 615 if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80) 616 /* 617 * Got `invalid command'; we interpret it 618 * to mean that the re-calibrate hasn't in 619 * fact finished yet 620 */ 621 continue; 622 break; 623 } 624 } 625 n = fdc->sc_nstat; 626 #ifdef FD_DEBUG 627 if (fdc_debug) { 628 int i; 629 printf("fdprobe: %d stati:", n); 630 for (i = 0; i < n; i++) 631 printf(" 0x%x", fdc->sc_status[i]); 632 printf("\n"); 633 } 634 #endif 635 ok = (n == 2 && (fdc->sc_status[0] & 0xf8) == 0x20) ? 1 : 0; 636 637 /* turn off motor */ 638 if (fdc->sc_flags & FDC_82077) { 639 /* deselect drive and turn motor off */ 640 *fdc->sc_reg_dor = FDO_FRST | FDO_DS; 641 } else { 642 auxregbisc(0, AUXIO4C_FDS); 643 } 644 645 return (ok); 646 } 647 648 /* 649 * Controller is working, and drive responded. Attach it. 650 */ 651 void 652 fdattach(parent, self, aux) 653 struct device *parent, *self; 654 void *aux; 655 { 656 struct fdc_softc *fdc = (void *)parent; 657 struct fd_softc *fd = (void *)self; 658 struct fdc_attach_args *fa = aux; 659 struct fd_type *type = fa->fa_deftype; 660 int drive = fa->fa_drive; 661 662 /* XXX Allow `flags' to override device type? */ 663 664 if (type) 665 printf(": %s %d cyl, %d head, %d sec\n", type->name, 666 type->cylinders, type->heads, type->sectrac); 667 else 668 printf(": density unknown\n"); 669 670 fd->sc_cylin = -1; 671 fd->sc_drive = drive; 672 fd->sc_deftype = type; 673 fdc->sc_fd[drive] = fd; 674 675 out_fdc(fdc, NE7CMD_SPECIFY); 676 out_fdc(fdc, type->steprate); 677 out_fdc(fdc, 6 | NE7_SPECIFY_NODMA); 678 679 /* 680 * Initialize and attach the disk structure. 681 */ 682 fd->sc_dk.dk_name = fd->sc_dv.dv_xname; 683 fd->sc_dk.dk_driver = &fddkdriver; 684 disk_attach(&fd->sc_dk); 685 686 /* 687 * We're told if we're the boot device in fdcattach(). 688 */ 689 if (fa->fa_bootpath) 690 fa->fa_bootpath->dev = &fd->sc_dv; 691 692 /* 693 * Establish a mountroot_hook anyway in case we booted 694 * with RB_ASKNAME and get selected as the boot device. 695 */ 696 mountroothook_establish(fd_mountroot_hook, &fd->sc_dv); 697 698 /* Make sure the drive motor gets turned off at shutdown time. */ 699 fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd); 700 701 /* XXX Need to do some more fiddling with sc_dk. */ 702 dk_establish(&fd->sc_dk, &fd->sc_dv); 703 } 704 705 __inline struct fd_type * 706 fd_dev_to_type(fd, dev) 707 struct fd_softc *fd; 708 dev_t dev; 709 { 710 int type = FDTYPE(dev); 711 712 if (type > (sizeof(fd_types) / sizeof(fd_types[0]))) 713 return (NULL); 714 return (type ? &fd_types[type - 1] : fd->sc_deftype); 715 } 716 717 void 718 fdstrategy(bp) 719 register struct buf *bp; /* IO operation to perform */ 720 { 721 struct fd_softc *fd; 722 int unit = FDUNIT(bp->b_dev); 723 int sz; 724 int s; 725 726 /* Valid unit, controller, and request? */ 727 if (unit >= fd_cd.cd_ndevs || 728 (fd = fd_cd.cd_devs[unit]) == 0 || 729 bp->b_blkno < 0 || 730 (((bp->b_bcount % FD_BSIZE(fd)) != 0 || 731 (bp->b_blkno * DEV_BSIZE) % FD_BSIZE(fd) != 0) && 732 (bp->b_flags & B_FORMAT) == 0)) { 733 bp->b_error = EINVAL; 734 goto bad; 735 } 736 737 /* If it's a null transfer, return immediately. */ 738 if (bp->b_bcount == 0) 739 goto done; 740 741 sz = howmany(bp->b_bcount, DEV_BSIZE); 742 743 if (bp->b_blkno + sz > (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)) { 744 sz = (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd) 745 - bp->b_blkno; 746 if (sz == 0) { 747 /* If exactly at end of disk, return EOF. */ 748 bp->b_resid = bp->b_bcount; 749 goto done; 750 } 751 if (sz < 0) { 752 /* If past end of disk, return EINVAL. */ 753 bp->b_error = EINVAL; 754 goto bad; 755 } 756 /* Otherwise, truncate request. */ 757 bp->b_bcount = sz << DEV_BSHIFT; 758 } 759 760 bp->b_cylin = (bp->b_blkno * DEV_BSIZE) / 761 (FD_BSIZE(fd) * fd->sc_type->seccyl); 762 763 #ifdef FD_DEBUG 764 if (fdc_debug > 1) 765 printf("fdstrategy: b_blkno %d b_bcount %ld blkno %d cylin %ld\n", 766 bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylin); 767 #endif 768 769 /* Queue transfer on drive, activate drive and controller if idle. */ 770 s = splbio(); 771 disksort(&fd->sc_q, bp); 772 untimeout(fd_motor_off, fd); /* a good idea */ 773 if (!fd->sc_q.b_active) 774 fdstart(fd); 775 #ifdef DIAGNOSTIC 776 else { 777 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent; 778 if (fdc->sc_state == DEVIDLE) { 779 printf("fdstrategy: controller inactive\n"); 780 fdcstart(fdc); 781 } 782 } 783 #endif 784 splx(s); 785 return; 786 787 bad: 788 bp->b_flags |= B_ERROR; 789 done: 790 /* Toss transfer; we're done early. */ 791 biodone(bp); 792 } 793 794 void 795 fdstart(fd) 796 struct fd_softc *fd; 797 { 798 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent; 799 int active = fdc->sc_drives.tqh_first != 0; 800 801 /* Link into controller queue. */ 802 fd->sc_q.b_active = 1; 803 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain); 804 805 /* If controller not already active, start it. */ 806 if (!active) 807 fdcstart(fdc); 808 } 809 810 void 811 fdfinish(fd, bp) 812 struct fd_softc *fd; 813 struct buf *bp; 814 { 815 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent; 816 817 /* 818 * Move this drive to the end of the queue to give others a `fair' 819 * chance. We only force a switch if N operations are completed while 820 * another drive is waiting to be serviced, since there is a long motor 821 * startup delay whenever we switch. 822 */ 823 if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) { 824 fd->sc_ops = 0; 825 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain); 826 if (bp->b_actf) { 827 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain); 828 } else 829 fd->sc_q.b_active = 0; 830 } 831 bp->b_resid = fd->sc_bcount; 832 fd->sc_skip = 0; 833 fd->sc_q.b_actf = bp->b_actf; 834 835 biodone(bp); 836 /* turn off motor 5s from now */ 837 timeout(fd_motor_off, fd, 5 * hz); 838 fdc->sc_state = DEVIDLE; 839 } 840 841 void 842 fdc_reset(fdc) 843 struct fdc_softc *fdc; 844 { 845 if (fdc->sc_flags & FDC_82077) { 846 *fdc->sc_reg_dor = FDO_FDMAEN | FDO_MOEN(0); 847 } 848 849 *fdc->sc_reg_drs = DRS_RESET; 850 delay(10); 851 *fdc->sc_reg_drs = 0; 852 853 if (fdc->sc_flags & FDC_82077) { 854 *fdc->sc_reg_dor = FDO_FRST | FDO_FDMAEN | FDO_DS; 855 } 856 #ifdef FD_DEBUG 857 if (fdc_debug) 858 printf("fdc reset\n"); 859 #endif 860 } 861 862 void 863 fd_set_motor(fdc) 864 struct fdc_softc *fdc; 865 { 866 struct fd_softc *fd; 867 u_char status; 868 int n; 869 870 if (fdc->sc_flags & FDC_82077) { 871 status = FDO_FRST | FDO_FDMAEN; 872 if ((fd = fdc->sc_drives.tqh_first) != NULL) 873 status |= fd->sc_drive; 874 875 for (n = 0; n < 4; n++) 876 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR)) 877 status |= FDO_MOEN(n); 878 *fdc->sc_reg_dor = status; 879 } else { 880 int on = 0; 881 882 for (n = 0; n < 4; n++) 883 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR)) 884 on = 1; 885 if (on) { 886 auxregbisc(AUXIO4C_FDS, 0); 887 } else { 888 auxregbisc(0, AUXIO4C_FDS); 889 } 890 } 891 } 892 893 void 894 fd_motor_off(arg) 895 void *arg; 896 { 897 struct fd_softc *fd = arg; 898 int s; 899 900 s = splbio(); 901 fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT); 902 fd_set_motor((struct fdc_softc *)fd->sc_dv.dv_parent); 903 splx(s); 904 } 905 906 void 907 fd_motor_on(arg) 908 void *arg; 909 { 910 struct fd_softc *fd = arg; 911 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent; 912 int s; 913 914 s = splbio(); 915 fd->sc_flags &= ~FD_MOTOR_WAIT; 916 if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT)) 917 (void) fdcstate(fdc); 918 splx(s); 919 } 920 921 int 922 fdcresult(fdc) 923 struct fdc_softc *fdc; 924 { 925 u_char i; 926 int j = 100000, 927 n = 0; 928 929 for (; j; j--) { 930 i = *fdc->sc_reg_msr & (NE7_DIO | NE7_RQM | NE7_CB); 931 if (i == NE7_RQM) 932 return (fdc->sc_nstat = n); 933 if (i == (NE7_DIO | NE7_RQM | NE7_CB)) { 934 if (n >= sizeof(fdc->sc_status)) { 935 log(LOG_ERR, "fdcresult: overrun\n"); 936 return (-1); 937 } 938 fdc->sc_status[n++] = *fdc->sc_reg_fifo; 939 } else 940 delay(10); 941 } 942 log(LOG_ERR, "fdcresult: timeout\n"); 943 return (fdc->sc_nstat = -1); 944 } 945 946 int 947 out_fdc(fdc, x) 948 struct fdc_softc *fdc; 949 u_char x; 950 { 951 int i = 100000; 952 953 while (((*fdc->sc_reg_msr & (NE7_DIO|NE7_RQM)) != NE7_RQM) && i-- > 0) 954 delay(1); 955 if (i <= 0) 956 return (-1); 957 958 *fdc->sc_reg_fifo = x; 959 return (0); 960 } 961 962 int 963 fdopen(dev, flags, fmt, p) 964 dev_t dev; 965 int flags, fmt; 966 struct proc *p; 967 { 968 int unit, pmask; 969 struct fd_softc *fd; 970 struct fd_type *type; 971 972 unit = FDUNIT(dev); 973 if (unit >= fd_cd.cd_ndevs) 974 return (ENXIO); 975 fd = fd_cd.cd_devs[unit]; 976 if (fd == 0) 977 return (ENXIO); 978 type = fd_dev_to_type(fd, dev); 979 if (type == NULL) 980 return (ENXIO); 981 982 if ((fd->sc_flags & FD_OPEN) != 0 && 983 fd->sc_type != type) 984 return (EBUSY); 985 986 fd->sc_type = type; 987 fd->sc_cylin = -1; 988 fd->sc_flags |= FD_OPEN; 989 990 /* 991 * Only update the disklabel if we're not open anywhere else. 992 */ 993 if (fd->sc_dk.dk_openmask == 0) 994 fdgetdisklabel(dev); 995 996 pmask = (1 << DISKPART(dev)); 997 998 switch (fmt) { 999 case S_IFCHR: 1000 fd->sc_dk.dk_copenmask |= pmask; 1001 break; 1002 1003 case S_IFBLK: 1004 fd->sc_dk.dk_bopenmask |= pmask; 1005 break; 1006 } 1007 fd->sc_dk.dk_openmask = 1008 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask; 1009 1010 return (0); 1011 } 1012 1013 int 1014 fdclose(dev, flags, fmt, p) 1015 dev_t dev; 1016 int flags, fmt; 1017 struct proc *p; 1018 { 1019 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)]; 1020 int pmask = (1 << DISKPART(dev)); 1021 1022 fd->sc_flags &= ~FD_OPEN; 1023 fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT); 1024 1025 switch (fmt) { 1026 case S_IFCHR: 1027 fd->sc_dk.dk_copenmask &= ~pmask; 1028 break; 1029 1030 case S_IFBLK: 1031 fd->sc_dk.dk_bopenmask &= ~pmask; 1032 break; 1033 } 1034 fd->sc_dk.dk_openmask = 1035 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask; 1036 1037 return (0); 1038 } 1039 1040 int 1041 fdread(dev, uio, flag) 1042 dev_t dev; 1043 struct uio *uio; 1044 int flag; 1045 { 1046 1047 return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio)); 1048 } 1049 1050 int 1051 fdwrite(dev, uio, flag) 1052 dev_t dev; 1053 struct uio *uio; 1054 int flag; 1055 { 1056 1057 return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio)); 1058 } 1059 1060 void 1061 fdcstart(fdc) 1062 struct fdc_softc *fdc; 1063 { 1064 1065 #ifdef DIAGNOSTIC 1066 /* only got here if controller's drive queue was inactive; should 1067 be in idle state */ 1068 if (fdc->sc_state != DEVIDLE) { 1069 printf("fdcstart: not idle\n"); 1070 return; 1071 } 1072 #endif 1073 (void) fdcstate(fdc); 1074 } 1075 1076 void 1077 fdcstatus(dv, n, s) 1078 struct device *dv; 1079 int n; 1080 char *s; 1081 { 1082 struct fdc_softc *fdc = (void *)dv->dv_parent; 1083 char bits[64]; 1084 #if 0 1085 /* 1086 * A 82072 seems to return <invalid command> on 1087 * gratuitous Sense Interrupt commands. 1088 */ 1089 if (n == 0 && (fdc->sc_flags & FDC_82077)) { 1090 out_fdc(fdc, NE7CMD_SENSEI); 1091 (void) fdcresult(fdc); 1092 n = 2; 1093 } 1094 #endif 1095 1096 /* Just print last status */ 1097 n = fdc->sc_nstat; 1098 1099 printf("%s: %s: state %d", dv->dv_xname, s, fdc->sc_state); 1100 1101 switch (n) { 1102 case 0: 1103 printf("\n"); 1104 break; 1105 case 2: 1106 printf(" (st0 %s cyl %d)\n", 1107 bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS, 1108 bits, sizeof(bits)), fdc->sc_status[1]); 1109 break; 1110 case 7: 1111 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0], 1112 NE7_ST0BITS, bits, sizeof(bits))); 1113 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1], 1114 NE7_ST1BITS, bits, sizeof(bits))); 1115 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2], 1116 NE7_ST2BITS, bits, sizeof(bits))); 1117 printf(" cyl %d head %d sec %d)\n", 1118 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]); 1119 break; 1120 #ifdef DIAGNOSTIC 1121 default: 1122 printf(" fdcstatus: weird size: %d\n", n); 1123 break; 1124 #endif 1125 } 1126 } 1127 1128 void 1129 fdctimeout(arg) 1130 void *arg; 1131 { 1132 struct fdc_softc *fdc = arg; 1133 struct fd_softc *fd = fdc->sc_drives.tqh_first; 1134 int s; 1135 1136 s = splbio(); 1137 fdcstatus(&fd->sc_dv, 0, "timeout"); 1138 1139 if (fd->sc_q.b_actf) 1140 fdc->sc_state++; 1141 else 1142 fdc->sc_state = DEVIDLE; 1143 1144 (void) fdcstate(fdc); 1145 splx(s); 1146 } 1147 1148 void 1149 fdcpseudointr(arg) 1150 void *arg; 1151 { 1152 struct fdc_softc *fdc = arg; 1153 int s; 1154 1155 /* Just ensure it has the right spl. */ 1156 s = splbio(); 1157 (void) fdcstate(fdc); 1158 splx(s); 1159 } 1160 1161 1162 #ifdef FDC_C_HANDLER 1163 /* 1164 * hardware interrupt entry point: must be converted to `fast' 1165 * (in-window) handler. 1166 */ 1167 int 1168 fdchwintr(arg) 1169 void *arg; 1170 { 1171 struct fdc_softc *fdc = arg; 1172 1173 switch (fdc->sc_istate) { 1174 case ISTATE_IDLE: 1175 return (0); 1176 case ISTATE_SENSEI: 1177 out_fdc(fdc, NE7CMD_SENSEI); 1178 fdcresult(fdc); 1179 fdc->sc_istate = ISTATE_IDLE; 1180 FD_SET_SWINTR; 1181 return (1); 1182 case ISTATE_SPURIOUS: 1183 fdcresult(fdc); 1184 fdc->sc_istate = ISTATE_SPURIOUS; 1185 printf("fdc: stray hard interrupt... "); 1186 FD_SET_SWINTR; 1187 return (1); 1188 case ISTATE_DMA: 1189 break; 1190 default: 1191 printf("fdc: goofed ...\n"); 1192 return (1); 1193 } 1194 1195 for (;;) { 1196 register int msr; 1197 1198 msr = *fdc->sc_reg_msr; 1199 1200 if ((msr & NE7_RQM) == 0) 1201 break; 1202 1203 if ((msr & NE7_NDM) == 0) { 1204 fdcresult(fdc); 1205 fdc->sc_istate = ISTATE_IDLE; 1206 ienab_bis(IE_FDSOFT); 1207 printf("fdc: overrun: tc = %d\n", fdc->sc_tc); 1208 break; 1209 } 1210 1211 if (msr & NE7_DIO) { 1212 *fdc->sc_data++ = *fdc->sc_reg_fifo; 1213 } else { 1214 *fdc->sc_reg_fifo = *fdc->sc_data++; 1215 } 1216 if (--fdc->sc_tc == 0) { 1217 fdc->sc_istate = ISTATE_DONE; 1218 FTC_FLIP; 1219 fdcresult(fdc); 1220 FD_SET_SWINTR; 1221 break; 1222 } 1223 } 1224 return (1); 1225 } 1226 #endif 1227 1228 int 1229 fdcswintr(arg) 1230 void *arg; 1231 { 1232 struct fdc_softc *fdc = arg; 1233 int s; 1234 1235 if (fdc->sc_istate != ISTATE_DONE) 1236 return (0); 1237 1238 fdc->sc_istate = ISTATE_IDLE; 1239 s = splbio(); 1240 fdcstate(fdc); 1241 splx(s); 1242 return (1); 1243 } 1244 1245 int 1246 fdcstate(fdc) 1247 struct fdc_softc *fdc; 1248 { 1249 #define st0 fdc->sc_status[0] 1250 #define st1 fdc->sc_status[1] 1251 #define cyl fdc->sc_status[1] 1252 #define OUT_FDC(fdc, c, s) \ 1253 do { if (out_fdc(fdc, (c))) { (fdc)->sc_state = (s); goto loop; } } while(0) 1254 1255 struct fd_softc *fd; 1256 struct buf *bp; 1257 int read, head, sec, nblks; 1258 struct fd_type *type; 1259 struct ne7_fd_formb *finfo = NULL; 1260 1261 1262 if (fdc->sc_istate != ISTATE_IDLE) { 1263 /* Trouble... */ 1264 printf("fdc: spurious interrupt: state %d, istate=%d\n", 1265 fdc->sc_state, fdc->sc_istate); 1266 fdc->sc_istate = ISTATE_IDLE; 1267 if (fdc->sc_state == RESETCOMPLETE || 1268 fdc->sc_state == RESETTIMEDOUT) { 1269 panic("fdcintr: spurious interrupt can't be cleared"); 1270 } 1271 goto doreset; 1272 } 1273 1274 loop: 1275 /* Is there a drive for the controller to do a transfer with? */ 1276 fd = fdc->sc_drives.tqh_first; 1277 if (fd == NULL) { 1278 fdc->sc_state = DEVIDLE; 1279 return (0); 1280 } 1281 1282 /* Is there a transfer to this drive? If not, deactivate drive. */ 1283 bp = fd->sc_q.b_actf; 1284 if (bp == NULL) { 1285 fd->sc_ops = 0; 1286 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain); 1287 fd->sc_q.b_active = 0; 1288 goto loop; 1289 } 1290 1291 if (bp->b_flags & B_FORMAT) 1292 finfo = (struct ne7_fd_formb *)bp->b_data; 1293 1294 switch (fdc->sc_state) { 1295 case DEVIDLE: 1296 fdc->sc_errors = 0; 1297 fd->sc_skip = 0; 1298 fd->sc_bcount = bp->b_bcount; 1299 fd->sc_blkno = (bp->b_blkno * DEV_BSIZE) / FD_BSIZE(fd); 1300 untimeout(fd_motor_off, fd); 1301 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) { 1302 fdc->sc_state = MOTORWAIT; 1303 return (1); 1304 } 1305 if ((fd->sc_flags & FD_MOTOR) == 0) { 1306 /* Turn on the motor, being careful about pairing. */ 1307 struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1]; 1308 if (ofd && ofd->sc_flags & FD_MOTOR) { 1309 untimeout(fd_motor_off, ofd); 1310 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT); 1311 } 1312 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT; 1313 fd_set_motor(fdc); 1314 fdc->sc_state = MOTORWAIT; 1315 if (fdc->sc_flags & FDC_82077) { /* XXX */ 1316 /* Allow .25s for motor to stabilize. */ 1317 timeout(fd_motor_on, fd, hz / 4); 1318 } else { 1319 fd->sc_flags &= ~FD_MOTOR_WAIT; 1320 goto loop; 1321 } 1322 return (1); 1323 } 1324 /* Make sure the right drive is selected. */ 1325 fd_set_motor(fdc); 1326 1327 /*FALLTHROUGH*/ 1328 case DOSEEK: 1329 doseek: 1330 if ((fdc->sc_flags & FDC_EIS) && 1331 (bp->b_flags & B_FORMAT) == 0) { 1332 fd->sc_cylin = bp->b_cylin; 1333 /* We use implied seek */ 1334 goto doio; 1335 } 1336 1337 if (fd->sc_cylin == bp->b_cylin) 1338 goto doio; 1339 1340 /* specify command */ 1341 OUT_FDC(fdc, NE7CMD_SPECIFY, SEEKTIMEDOUT); 1342 OUT_FDC(fdc, fd->sc_type->steprate, SEEKTIMEDOUT); 1343 /* XXX head load time == 6ms */ 1344 OUT_FDC(fdc, 6 | NE7_SPECIFY_NODMA, SEEKTIMEDOUT); 1345 1346 fdc->sc_istate = ISTATE_SENSEI; 1347 /* seek function */ 1348 OUT_FDC(fdc, NE7CMD_SEEK, SEEKTIMEDOUT); 1349 OUT_FDC(fdc, fd->sc_drive, SEEKTIMEDOUT); /* drive number */ 1350 OUT_FDC(fdc, bp->b_cylin * fd->sc_type->step, SEEKTIMEDOUT); 1351 1352 fd->sc_cylin = -1; 1353 fdc->sc_state = SEEKWAIT; 1354 fdc->sc_nstat = 0; 1355 1356 fd->sc_dk.dk_seek++; 1357 disk_busy(&fd->sc_dk); 1358 1359 timeout(fdctimeout, fdc, 4 * hz); 1360 return (1); 1361 1362 case DOIO: 1363 doio: 1364 if (finfo != NULL) 1365 fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) - 1366 (char *)finfo; 1367 type = fd->sc_type; 1368 sec = fd->sc_blkno % type->seccyl; 1369 nblks = type->seccyl - sec; 1370 nblks = min(nblks, fd->sc_bcount / FD_BSIZE(fd)); 1371 nblks = min(nblks, FDC_MAXIOSIZE / FD_BSIZE(fd)); 1372 fd->sc_nblks = nblks; 1373 fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FD_BSIZE(fd); 1374 head = sec / type->sectrac; 1375 sec -= head * type->sectrac; 1376 #ifdef DIAGNOSTIC 1377 {int block; 1378 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec; 1379 if (block != fd->sc_blkno) { 1380 printf("fdcintr: block %d != blkno %d\n", block, fd->sc_blkno); 1381 #ifdef DDB 1382 Debugger(); 1383 #endif 1384 }} 1385 #endif 1386 read = bp->b_flags & B_READ; 1387 1388 /* Setup for pseudo DMA */ 1389 fdc->sc_data = bp->b_data + fd->sc_skip; 1390 fdc->sc_tc = fd->sc_nbytes; 1391 1392 *fdc->sc_reg_drs = type->rate; 1393 #ifdef FD_DEBUG 1394 if (fdc_debug > 1) 1395 printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n", 1396 read ? "read" : "write", fd->sc_drive, 1397 fd->sc_cylin, head, sec, nblks); 1398 #endif 1399 fdc->sc_state = IOCOMPLETE; 1400 fdc->sc_istate = ISTATE_DMA; 1401 fdc->sc_nstat = 0; 1402 if (finfo != NULL) { 1403 /* formatting */ 1404 OUT_FDC(fdc, NE7CMD_FORMAT, IOTIMEDOUT); 1405 OUT_FDC(fdc, (head << 2) | fd->sc_drive, IOTIMEDOUT); 1406 OUT_FDC(fdc, finfo->fd_formb_secshift, IOTIMEDOUT); 1407 OUT_FDC(fdc, finfo->fd_formb_nsecs, IOTIMEDOUT); 1408 OUT_FDC(fdc, finfo->fd_formb_gaplen, IOTIMEDOUT); 1409 OUT_FDC(fdc, finfo->fd_formb_fillbyte, IOTIMEDOUT); 1410 } else { 1411 if (read) 1412 OUT_FDC(fdc, NE7CMD_READ, IOTIMEDOUT); 1413 else 1414 OUT_FDC(fdc, NE7CMD_WRITE, IOTIMEDOUT); 1415 OUT_FDC(fdc, (head << 2) | fd->sc_drive, IOTIMEDOUT); 1416 OUT_FDC(fdc, fd->sc_cylin, IOTIMEDOUT); /*track*/ 1417 OUT_FDC(fdc, head, IOTIMEDOUT); 1418 OUT_FDC(fdc, sec + 1, IOTIMEDOUT); /*sector+1*/ 1419 OUT_FDC(fdc, type->secsize, IOTIMEDOUT);/*sector size*/ 1420 OUT_FDC(fdc, type->sectrac, IOTIMEDOUT);/*secs/track*/ 1421 OUT_FDC(fdc, type->gap1, IOTIMEDOUT); /*gap1 size*/ 1422 OUT_FDC(fdc, type->datalen, IOTIMEDOUT);/*data length*/ 1423 } 1424 1425 disk_busy(&fd->sc_dk); 1426 1427 /* allow 2 seconds for operation */ 1428 timeout(fdctimeout, fdc, 2 * hz); 1429 return (1); /* will return later */ 1430 1431 case SEEKWAIT: 1432 untimeout(fdctimeout, fdc); 1433 fdc->sc_state = SEEKCOMPLETE; 1434 if (fdc->sc_flags & FDC_NEEDHEADSETTLE) { 1435 /* allow 1/50 second for heads to settle */ 1436 timeout(fdcpseudointr, fdc, hz / 50); 1437 return (1); /* will return later */ 1438 } 1439 /*FALLTHROUGH*/ 1440 case SEEKCOMPLETE: 1441 disk_unbusy(&fd->sc_dk, 0); /* no data on seek */ 1442 1443 /* Make sure seek really happened. */ 1444 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || 1445 cyl != bp->b_cylin * fd->sc_type->step) { 1446 #ifdef FD_DEBUG 1447 if (fdc_debug) 1448 fdcstatus(&fd->sc_dv, 2, "seek failed"); 1449 #endif 1450 fdcretry(fdc); 1451 goto loop; 1452 } 1453 fd->sc_cylin = bp->b_cylin; 1454 goto doio; 1455 1456 case IOTIMEDOUT: 1457 FTC_FLIP; 1458 (void)fdcresult(fdc); 1459 /*FALLTHROUGH*/ 1460 case SEEKTIMEDOUT: 1461 case RECALTIMEDOUT: 1462 case RESETTIMEDOUT: 1463 fdcretry(fdc); 1464 goto loop; 1465 1466 case IOCOMPLETE: /* IO DONE, post-analyze */ 1467 untimeout(fdctimeout, fdc); 1468 1469 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid)); 1470 1471 if (fdc->sc_nstat != 7 || st1 != 0 || 1472 ((st0 & 0xf8) != 0 && 1473 ((st0 & 0xf8) != 0x20 || (fdc->sc_cfg & CFG_EIS) == 0))) { 1474 #ifdef FD_DEBUG 1475 if (fdc_debug) { 1476 fdcstatus(&fd->sc_dv, 7, 1477 bp->b_flags & B_READ 1478 ? "read failed" : "write failed"); 1479 printf("blkno %d nblks %d nstat %d tc %d\n", 1480 fd->sc_blkno, fd->sc_nblks, 1481 fdc->sc_nstat, fdc->sc_tc); 1482 } 1483 #endif 1484 if (fdc->sc_nstat == 7 && 1485 (st1 & ST1_OVERRUN) == ST1_OVERRUN) { 1486 1487 /* 1488 * Silently retry overruns if no other 1489 * error bit is set. Adjust threshold. 1490 */ 1491 int thr = fdc->sc_cfg & CFG_THRHLD_MASK; 1492 if (thr < 15) { 1493 thr++; 1494 fdc->sc_cfg &= ~CFG_THRHLD_MASK; 1495 fdc->sc_cfg |= (thr & CFG_THRHLD_MASK); 1496 #ifdef FD_DEBUG 1497 if (fdc_debug) 1498 printf("fdc: %d -> threshold\n", thr); 1499 #endif 1500 fdconf(fdc); 1501 fdc->sc_overruns = 0; 1502 } 1503 if (++fdc->sc_overruns < 3) { 1504 fdc->sc_state = DOIO; 1505 goto loop; 1506 } 1507 } 1508 fdcretry(fdc); 1509 goto loop; 1510 } 1511 if (fdc->sc_errors) { 1512 diskerr(bp, "fd", "soft error", LOG_PRINTF, 1513 fd->sc_skip / FD_BSIZE(fd), 1514 (struct disklabel *)NULL); 1515 printf("\n"); 1516 fdc->sc_errors = 0; 1517 } else { 1518 if (--fdc->sc_overruns < -20) { 1519 int thr = fdc->sc_cfg & CFG_THRHLD_MASK; 1520 if (thr > 0) { 1521 thr--; 1522 fdc->sc_cfg &= ~CFG_THRHLD_MASK; 1523 fdc->sc_cfg |= (thr & CFG_THRHLD_MASK); 1524 #ifdef FD_DEBUG 1525 if (fdc_debug) 1526 printf("fdc: %d -> threshold\n", thr); 1527 #endif 1528 fdconf(fdc); 1529 } 1530 fdc->sc_overruns = 0; 1531 } 1532 } 1533 fd->sc_blkno += fd->sc_nblks; 1534 fd->sc_skip += fd->sc_nbytes; 1535 fd->sc_bcount -= fd->sc_nbytes; 1536 if (finfo == NULL && fd->sc_bcount > 0) { 1537 bp->b_cylin = fd->sc_blkno / fd->sc_type->seccyl; 1538 goto doseek; 1539 } 1540 fdfinish(fd, bp); 1541 goto loop; 1542 1543 case DORESET: 1544 doreset: 1545 /* try a reset, keep motor on */ 1546 fd_set_motor(fdc); 1547 delay(100); 1548 fdc_reset(fdc); 1549 fdc->sc_nstat = 0; 1550 fdc->sc_istate = ISTATE_SENSEI; 1551 fdc->sc_state = RESETCOMPLETE; 1552 timeout(fdctimeout, fdc, hz / 2); 1553 return (1); /* will return later */ 1554 1555 case RESETCOMPLETE: 1556 untimeout(fdctimeout, fdc); 1557 fdconf(fdc); 1558 1559 /* fall through */ 1560 case DORECAL: 1561 fdc->sc_state = RECALWAIT; 1562 fdc->sc_istate = ISTATE_SENSEI; 1563 fdc->sc_nstat = 0; 1564 /* recalibrate function */ 1565 OUT_FDC(fdc, NE7CMD_RECAL, RECALTIMEDOUT); 1566 OUT_FDC(fdc, fd->sc_drive, RECALTIMEDOUT); 1567 timeout(fdctimeout, fdc, 5 * hz); 1568 return (1); /* will return later */ 1569 1570 case RECALWAIT: 1571 untimeout(fdctimeout, fdc); 1572 fdc->sc_state = RECALCOMPLETE; 1573 if (fdc->sc_flags & FDC_NEEDHEADSETTLE) { 1574 /* allow 1/30 second for heads to settle */ 1575 timeout(fdcpseudointr, fdc, hz / 30); 1576 return (1); /* will return later */ 1577 } 1578 1579 case RECALCOMPLETE: 1580 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) { 1581 #ifdef FD_DEBUG 1582 if (fdc_debug) 1583 fdcstatus(&fd->sc_dv, 2, "recalibrate failed"); 1584 #endif 1585 fdcretry(fdc); 1586 goto loop; 1587 } 1588 fd->sc_cylin = 0; 1589 goto doseek; 1590 1591 case MOTORWAIT: 1592 if (fd->sc_flags & FD_MOTOR_WAIT) 1593 return (1); /* time's not up yet */ 1594 goto doseek; 1595 1596 default: 1597 fdcstatus(&fd->sc_dv, 0, "stray interrupt"); 1598 return (1); 1599 } 1600 #ifdef DIAGNOSTIC 1601 panic("fdcintr: impossible"); 1602 #endif 1603 #undef st0 1604 #undef st1 1605 #undef cyl 1606 } 1607 1608 void 1609 fdcretry(fdc) 1610 struct fdc_softc *fdc; 1611 { 1612 char bits[64]; 1613 struct fd_softc *fd; 1614 struct buf *bp; 1615 1616 fd = fdc->sc_drives.tqh_first; 1617 bp = fd->sc_q.b_actf; 1618 1619 fdc->sc_overruns = 0; 1620 if (fd->sc_opts & FDOPT_NORETRY) 1621 goto fail; 1622 1623 switch (fdc->sc_errors) { 1624 case 0: 1625 /* try again */ 1626 fdc->sc_state = 1627 (fdc->sc_flags & FDC_EIS) ? DOIO : DOSEEK; 1628 break; 1629 1630 case 1: case 2: case 3: 1631 /* didn't work; try recalibrating */ 1632 fdc->sc_state = DORECAL; 1633 break; 1634 1635 case 4: 1636 /* still no go; reset the bastard */ 1637 fdc->sc_state = DORESET; 1638 break; 1639 1640 default: 1641 fail: 1642 if ((fd->sc_opts & FDOPT_SILENT) == 0) { 1643 diskerr(bp, "fd", "hard error", LOG_PRINTF, 1644 fd->sc_skip / FD_BSIZE(fd), 1645 (struct disklabel *)NULL); 1646 1647 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0], 1648 NE7_ST0BITS, bits, sizeof(bits))); 1649 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1], 1650 NE7_ST1BITS, bits, sizeof(bits))); 1651 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2], 1652 NE7_ST2BITS, bits, sizeof(bits))); 1653 printf(" cyl %d head %d sec %d)\n", 1654 fdc->sc_status[3], fdc->sc_status[4], 1655 fdc->sc_status[5]); 1656 } 1657 1658 bp->b_flags |= B_ERROR; 1659 bp->b_error = EIO; 1660 fdfinish(fd, bp); 1661 } 1662 fdc->sc_errors++; 1663 } 1664 1665 int 1666 fdsize(dev) 1667 dev_t dev; 1668 { 1669 1670 /* Swapping to floppies would not make sense. */ 1671 return (-1); 1672 } 1673 1674 int 1675 fddump(dev, blkno, va, size) 1676 dev_t dev; 1677 daddr_t blkno; 1678 caddr_t va; 1679 size_t size; 1680 { 1681 1682 /* Not implemented. */ 1683 return (EINVAL); 1684 } 1685 1686 int 1687 fdioctl(dev, cmd, addr, flag, p) 1688 dev_t dev; 1689 u_long cmd; 1690 caddr_t addr; 1691 int flag; 1692 struct proc *p; 1693 { 1694 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)]; 1695 struct fdformat_parms *form_parms; 1696 struct fdformat_cmd *form_cmd; 1697 struct ne7_fd_formb fd_formb; 1698 int il[FD_MAX_NSEC + 1]; 1699 int i, j; 1700 int error; 1701 1702 switch (cmd) { 1703 case DIOCGDINFO: 1704 *(struct disklabel *)addr = *(fd->sc_dk.dk_label); 1705 return 0; 1706 1707 case DIOCWLABEL: 1708 if ((flag & FWRITE) == 0) 1709 return EBADF; 1710 /* XXX do something */ 1711 return (0); 1712 1713 case DIOCWDINFO: 1714 if ((flag & FWRITE) == 0) 1715 return (EBADF); 1716 1717 error = setdisklabel(fd->sc_dk.dk_label, 1718 (struct disklabel *)addr, 0, 1719 fd->sc_dk.dk_cpulabel); 1720 if (error) 1721 return (error); 1722 1723 error = writedisklabel(dev, fdstrategy, 1724 fd->sc_dk.dk_label, 1725 fd->sc_dk.dk_cpulabel); 1726 return (error); 1727 1728 case DIOCLOCK: 1729 /* 1730 * Nothing to do here, really. 1731 */ 1732 return (0); 1733 1734 case DIOCEJECT: 1735 if (*(int *)addr == 0) { 1736 int part = DISKPART(dev); 1737 /* 1738 * Don't force eject: check that we are the only 1739 * partition open. If so, unlock it. 1740 */ 1741 if ((fd->sc_dk.dk_openmask & ~(1 << part)) != 0 || 1742 fd->sc_dk.dk_bopenmask + fd->sc_dk.dk_copenmask != 1743 fd->sc_dk.dk_openmask) { 1744 return (EBUSY); 1745 } 1746 } 1747 /* FALLTHROUGH */ 1748 case ODIOCEJECT: 1749 fd_do_eject(fd); 1750 return (0); 1751 1752 case FDIOCGETFORMAT: 1753 form_parms = (struct fdformat_parms *)addr; 1754 form_parms->fdformat_version = FDFORMAT_VERSION; 1755 form_parms->nbps = 128 * (1 << fd->sc_type->secsize); 1756 form_parms->ncyl = fd->sc_type->cylinders; 1757 form_parms->nspt = fd->sc_type->sectrac; 1758 form_parms->ntrk = fd->sc_type->heads; 1759 form_parms->stepspercyl = fd->sc_type->step; 1760 form_parms->gaplen = fd->sc_type->gap2; 1761 form_parms->fillbyte = fd->sc_type->fillbyte; 1762 form_parms->interleave = fd->sc_type->interleave; 1763 switch (fd->sc_type->rate) { 1764 case FDC_500KBPS: 1765 form_parms->xfer_rate = 500 * 1024; 1766 break; 1767 case FDC_300KBPS: 1768 form_parms->xfer_rate = 300 * 1024; 1769 break; 1770 case FDC_250KBPS: 1771 form_parms->xfer_rate = 250 * 1024; 1772 break; 1773 default: 1774 return (EINVAL); 1775 } 1776 return (0); 1777 1778 case FDIOCSETFORMAT: 1779 if ((flag & FWRITE) == 0) 1780 return (EBADF); /* must be opened for writing */ 1781 1782 form_parms = (struct fdformat_parms *)addr; 1783 if (form_parms->fdformat_version != FDFORMAT_VERSION) 1784 return (EINVAL);/* wrong version of formatting prog */ 1785 1786 i = form_parms->nbps >> 7; 1787 if ((form_parms->nbps & 0x7f) || ffs(i) == 0 || 1788 i & ~(1 << (ffs(i)-1))) 1789 /* not a power-of-two multiple of 128 */ 1790 return (EINVAL); 1791 1792 switch (form_parms->xfer_rate) { 1793 case 500 * 1024: 1794 fd->sc_type->rate = FDC_500KBPS; 1795 break; 1796 case 300 * 1024: 1797 fd->sc_type->rate = FDC_300KBPS; 1798 break; 1799 case 250 * 1024: 1800 fd->sc_type->rate = FDC_250KBPS; 1801 break; 1802 default: 1803 return (EINVAL); 1804 } 1805 1806 if (form_parms->nspt > FD_MAX_NSEC || 1807 form_parms->fillbyte > 0xff || 1808 form_parms->interleave > 0xff) 1809 return EINVAL; 1810 fd->sc_type->sectrac = form_parms->nspt; 1811 if (form_parms->ntrk != 2 && form_parms->ntrk != 1) 1812 return EINVAL; 1813 fd->sc_type->heads = form_parms->ntrk; 1814 fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk; 1815 fd->sc_type->secsize = ffs(i)-1; 1816 fd->sc_type->gap2 = form_parms->gaplen; 1817 fd->sc_type->cylinders = form_parms->ncyl; 1818 fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl * 1819 form_parms->nbps / DEV_BSIZE; 1820 fd->sc_type->step = form_parms->stepspercyl; 1821 fd->sc_type->fillbyte = form_parms->fillbyte; 1822 fd->sc_type->interleave = form_parms->interleave; 1823 return (0); 1824 1825 case FDIOCFORMAT_TRACK: 1826 if((flag & FWRITE) == 0) 1827 /* must be opened for writing */ 1828 return (EBADF); 1829 form_cmd = (struct fdformat_cmd *)addr; 1830 if (form_cmd->formatcmd_version != FDFORMAT_VERSION) 1831 /* wrong version of formatting prog */ 1832 return (EINVAL); 1833 1834 if (form_cmd->head >= fd->sc_type->heads || 1835 form_cmd->cylinder >= fd->sc_type->cylinders) { 1836 return (EINVAL); 1837 } 1838 1839 fd_formb.head = form_cmd->head; 1840 fd_formb.cyl = form_cmd->cylinder; 1841 fd_formb.transfer_rate = fd->sc_type->rate; 1842 fd_formb.fd_formb_secshift = fd->sc_type->secsize; 1843 fd_formb.fd_formb_nsecs = fd->sc_type->sectrac; 1844 fd_formb.fd_formb_gaplen = fd->sc_type->gap2; 1845 fd_formb.fd_formb_fillbyte = fd->sc_type->fillbyte; 1846 1847 bzero(il, sizeof il); 1848 for (j = 0, i = 1; i <= fd_formb.fd_formb_nsecs; i++) { 1849 while (il[(j%fd_formb.fd_formb_nsecs) + 1]) 1850 j++; 1851 il[(j%fd_formb.fd_formb_nsecs) + 1] = i; 1852 j += fd->sc_type->interleave; 1853 } 1854 for (i = 0; i < fd_formb.fd_formb_nsecs; i++) { 1855 fd_formb.fd_formb_cylno(i) = form_cmd->cylinder; 1856 fd_formb.fd_formb_headno(i) = form_cmd->head; 1857 fd_formb.fd_formb_secno(i) = il[i+1]; 1858 fd_formb.fd_formb_secsize(i) = fd->sc_type->secsize; 1859 } 1860 1861 return fdformat(dev, &fd_formb, p); 1862 1863 case FDIOCGETOPTS: /* get drive options */ 1864 *(int *)addr = fd->sc_opts; 1865 return (0); 1866 1867 case FDIOCSETOPTS: /* set drive options */ 1868 fd->sc_opts = *(int *)addr; 1869 return (0); 1870 1871 #ifdef DEBUG 1872 case _IO('f', 100): 1873 { 1874 int i; 1875 struct fdc_softc *fdc = (struct fdc_softc *) 1876 fd->sc_dv.dv_parent; 1877 1878 out_fdc(fdc, NE7CMD_DUMPREG); 1879 fdcresult(fdc); 1880 printf("dumpreg(%d regs): <", fdc->sc_nstat); 1881 for (i = 0; i < fdc->sc_nstat; i++) 1882 printf(" 0x%x", fdc->sc_status[i]); 1883 printf(">\n"); 1884 } 1885 1886 return (0); 1887 case _IOW('f', 101, int): 1888 ((struct fdc_softc *)fd->sc_dv.dv_parent)->sc_cfg &= 1889 ~CFG_THRHLD_MASK; 1890 ((struct fdc_softc *)fd->sc_dv.dv_parent)->sc_cfg |= 1891 (*(int *)addr & CFG_THRHLD_MASK); 1892 fdconf((struct fdc_softc *) fd->sc_dv.dv_parent); 1893 return (0); 1894 case _IO('f', 102): 1895 { 1896 int i; 1897 struct fdc_softc *fdc = (struct fdc_softc *) 1898 fd->sc_dv.dv_parent; 1899 out_fdc(fdc, NE7CMD_SENSEI); 1900 fdcresult(fdc); 1901 printf("sensei(%d regs): <", fdc->sc_nstat); 1902 for (i=0; i< fdc->sc_nstat; i++) 1903 printf(" 0x%x", fdc->sc_status[i]); 1904 } 1905 printf(">\n"); 1906 return (0); 1907 #endif 1908 default: 1909 return (ENOTTY); 1910 } 1911 1912 #ifdef DIAGNOSTIC 1913 panic("fdioctl: impossible"); 1914 #endif 1915 } 1916 1917 int 1918 fdformat(dev, finfo, p) 1919 dev_t dev; 1920 struct ne7_fd_formb *finfo; 1921 struct proc *p; 1922 { 1923 int rv = 0, s; 1924 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)]; 1925 struct fd_type *type = fd->sc_type; 1926 struct buf *bp; 1927 1928 /* set up a buffer header for fdstrategy() */ 1929 bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT); 1930 if (bp == 0) 1931 return (ENOBUFS); 1932 1933 PHOLD(p); 1934 bzero((void *)bp, sizeof(struct buf)); 1935 bp->b_flags = B_BUSY | B_PHYS | B_FORMAT; 1936 bp->b_proc = p; 1937 bp->b_dev = dev; 1938 1939 /* 1940 * Calculate a fake blkno, so fdstrategy() would initiate a 1941 * seek to the requested cylinder. 1942 */ 1943 bp->b_blkno = ((finfo->cyl * (type->sectrac * type->heads) 1944 + finfo->head * type->sectrac) * FD_BSIZE(fd)) 1945 / DEV_BSIZE; 1946 1947 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs; 1948 bp->b_data = (caddr_t)finfo; 1949 1950 #ifdef FD_DEBUG 1951 if (fdc_debug) 1952 printf("fdformat: blkno 0x%x count %ld\n", 1953 bp->b_blkno, bp->b_bcount); 1954 #endif 1955 1956 /* now do the format */ 1957 fdstrategy(bp); 1958 1959 /* ...and wait for it to complete */ 1960 s = splbio(); 1961 while (!(bp->b_flags & B_DONE)) { 1962 rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz); 1963 if (rv == EWOULDBLOCK) 1964 break; 1965 } 1966 splx(s); 1967 1968 if (rv == EWOULDBLOCK) { 1969 /* timed out */ 1970 rv = EIO; 1971 biodone(bp); 1972 } 1973 if (bp->b_flags & B_ERROR) { 1974 rv = bp->b_error; 1975 } 1976 PRELE(p); 1977 free(bp, M_TEMP); 1978 return (rv); 1979 } 1980 1981 void 1982 fdgetdisklabel(dev) 1983 dev_t dev; 1984 { 1985 int unit = FDUNIT(dev), i; 1986 struct fd_softc *fd = fd_cd.cd_devs[unit]; 1987 struct disklabel *lp = fd->sc_dk.dk_label; 1988 struct cpu_disklabel *clp = fd->sc_dk.dk_cpulabel; 1989 1990 bzero(lp, sizeof(struct disklabel)); 1991 bzero(lp, sizeof(struct cpu_disklabel)); 1992 1993 lp->d_type = DTYPE_FLOPPY; 1994 lp->d_secsize = FD_BSIZE(fd); 1995 lp->d_secpercyl = fd->sc_type->seccyl; 1996 lp->d_nsectors = fd->sc_type->sectrac; 1997 lp->d_ncylinders = fd->sc_type->cylinders; 1998 lp->d_ntracks = fd->sc_type->heads; /* Go figure... */ 1999 lp->d_rpm = 3600; /* XXX like it matters... */ 2000 2001 strncpy(lp->d_typename, "floppy", sizeof(lp->d_typename)); 2002 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname)); 2003 lp->d_interleave = 1; 2004 2005 lp->d_partitions[RAW_PART].p_offset = 0; 2006 lp->d_partitions[RAW_PART].p_size = lp->d_secpercyl * lp->d_ncylinders; 2007 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED; 2008 lp->d_npartitions = RAW_PART + 1; 2009 2010 lp->d_magic = DISKMAGIC; 2011 lp->d_magic2 = DISKMAGIC; 2012 lp->d_checksum = dkcksum(lp); 2013 2014 /* 2015 * Call the generic disklabel extraction routine. If there's 2016 * not a label there, fake it. 2017 */ 2018 if (readdisklabel(dev, fdstrategy, lp, clp) != NULL) { 2019 strncpy(lp->d_packname, "default label", 2020 sizeof(lp->d_packname)); 2021 /* 2022 * Reset the partition info; it might have gotten 2023 * trashed in readdisklabel(). 2024 * 2025 * XXX Why do we have to do this? readdisklabel() 2026 * should be safe... 2027 */ 2028 for (i = 0; i < MAXPARTITIONS; ++i) { 2029 lp->d_partitions[i].p_offset = 0; 2030 if (i == RAW_PART) { 2031 lp->d_partitions[i].p_size = 2032 lp->d_secpercyl * lp->d_ncylinders; 2033 lp->d_partitions[i].p_fstype = FS_BSDFFS; 2034 } else { 2035 lp->d_partitions[i].p_size = 0; 2036 lp->d_partitions[i].p_fstype = FS_UNUSED; 2037 } 2038 } 2039 lp->d_npartitions = RAW_PART + 1; 2040 } 2041 } 2042 2043 void 2044 fd_do_eject(fd) 2045 struct fd_softc *fd; 2046 { 2047 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent; 2048 2049 if (CPU_ISSUN4C) { 2050 auxregbisc(AUXIO4C_FDS, AUXIO4C_FEJ); 2051 delay(10); 2052 auxregbisc(AUXIO4C_FEJ, AUXIO4C_FDS); 2053 return; 2054 } 2055 if (CPU_ISSUN4M && (fdc->sc_flags & FDC_82077)) { 2056 int dor = FDO_FRST | FDO_FDMAEN | FDO_MOEN(0); 2057 *fdc->sc_reg_dor = dor | FDO_EJ; 2058 delay(10); 2059 *fdc->sc_reg_dor = FDO_FRST | FDO_DS; 2060 return; 2061 } 2062 } 2063 2064 #ifdef MEMORY_DISK_HOOKS 2065 int fd_read_md_image __P((size_t *, caddr_t *)); 2066 #endif 2067 2068 /* ARGSUSED */ 2069 void 2070 fd_mountroot_hook(dev) 2071 struct device *dev; 2072 { 2073 int c; 2074 2075 fd_do_eject((struct fd_softc *)dev); 2076 printf("Insert filesystem floppy and press return."); 2077 for (;;) { 2078 c = cngetc(); 2079 if ((c == '\r') || (c == '\n')) { 2080 printf("\n"); 2081 break; 2082 } 2083 } 2084 } 2085 2086 #ifdef MEMORY_DISK_HOOKS 2087 2088 #define FDMICROROOTSIZE ((2*18*80) << DEV_BSHIFT) 2089 2090 int 2091 fd_read_md_image(sizep, addrp) 2092 size_t *sizep; 2093 caddr_t *addrp; 2094 { 2095 struct buf buf, *bp = &buf; 2096 dev_t dev; 2097 off_t offset; 2098 caddr_t addr; 2099 2100 dev = makedev(54,0); /* XXX */ 2101 2102 MALLOC(addr, caddr_t, FDMICROROOTSIZE, M_DEVBUF, M_WAITOK); 2103 *addrp = addr; 2104 2105 if (fdopen(dev, 0, S_IFCHR, NULL)) 2106 panic("fd: mountroot: fdopen"); 2107 2108 offset = 0; 2109 2110 for (;;) { 2111 bp->b_dev = dev; 2112 bp->b_error = 0; 2113 bp->b_resid = 0; 2114 bp->b_proc = NULL; 2115 bp->b_flags = B_BUSY | B_PHYS | B_RAW | B_READ; 2116 bp->b_blkno = btodb(offset); 2117 bp->b_bcount = DEV_BSIZE; 2118 bp->b_data = addr; 2119 fdstrategy(bp); 2120 while ((bp->b_flags & B_DONE) == 0) { 2121 tsleep((caddr_t)bp, PRIBIO + 1, "physio", 0); 2122 } 2123 if (bp->b_error) 2124 panic("fd: mountroot: fdread error %d", bp->b_error); 2125 2126 if (bp->b_resid != 0) 2127 break; 2128 2129 addr += DEV_BSIZE; 2130 offset += DEV_BSIZE; 2131 if (offset + DEV_BSIZE > FDMICROROOTSIZE) 2132 break; 2133 } 2134 (void)fdclose(dev, 0, S_IFCHR, NULL); 2135 *sizep = offset; 2136 fd_do_eject(fd_cd.cd_devs[FDUNIT(dev)]); 2137 return (0); 2138 } 2139 #endif 2140