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