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