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