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