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