1 /* $NetBSD: fd.c,v 1.86 2008/04/30 23:02:43 ad Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 2003, 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Charles M. Hannum. 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 * Floppy formatting facilities merged from FreeBSD fd.c driver: 68 * Id: fd.c,v 1.53 1995/03/12 22:40:56 joerg Exp 69 * which carries the same copyright/redistribution notice as shown above with 70 * the addition of the following statement before the "Redistribution and 71 * use ..." clause: 72 * 73 * Copyright (c) 1993, 1994 by 74 * jc@irbs.UUCP (John Capo) 75 * vak@zebub.msk.su (Serge Vakulenko) 76 * ache@astral.msk.su (Andrew A. Chernov) 77 * 78 * Copyright (c) 1993, 1994, 1995 by 79 * joerg_wunsch@uriah.sax.de (Joerg Wunsch) 80 * dufault@hda.com (Peter Dufault) 81 */ 82 83 #include <sys/cdefs.h> 84 __KERNEL_RCSID(0, "$NetBSD: fd.c,v 1.86 2008/04/30 23:02:43 ad Exp $"); 85 86 #include "rnd.h" 87 #include "opt_ddb.h" 88 89 /* 90 * XXX This driver should be properly MI'd some day, but this allows us 91 * XXX to eliminate a lot of code duplication for now. 92 */ 93 #if !defined(alpha) && !defined(algor) && !defined(atari) && \ 94 !defined(bebox) && !defined(evbmips) && !defined(i386) && \ 95 !defined(prep) && !defined(sandpoint) && !defined(x86_64) && \ 96 !defined(mvmeppc) 97 #error platform not supported by this driver, yet 98 #endif 99 100 #include <sys/param.h> 101 #include <sys/systm.h> 102 #include <sys/callout.h> 103 #include <sys/kernel.h> 104 #include <sys/file.h> 105 #include <sys/ioctl.h> 106 #include <sys/device.h> 107 #include <sys/disklabel.h> 108 #include <sys/disk.h> 109 #include <sys/buf.h> 110 #include <sys/bufq.h> 111 #include <sys/malloc.h> 112 #include <sys/uio.h> 113 #include <sys/syslog.h> 114 #include <sys/queue.h> 115 #include <sys/proc.h> 116 #include <sys/fdio.h> 117 #include <sys/conf.h> 118 #include <sys/vnode.h> 119 #if NRND > 0 120 #include <sys/rnd.h> 121 #endif 122 123 #include <prop/proplib.h> 124 125 #include <uvm/uvm_extern.h> 126 127 #include <dev/cons.h> 128 129 #include <sys/cpu.h> 130 #include <sys/bus.h> 131 132 #include "locators.h" 133 134 #if defined(atari) 135 /* 136 * On the atari, it is configured as fdcisa 137 */ 138 #define FDCCF_DRIVE FDCISACF_DRIVE 139 #define FDCCF_DRIVE_DEFAULT FDCISACF_DRIVE_DEFAULT 140 141 #define fd_cd fdisa_cd 142 #endif /* atari */ 143 144 #include <sys/intr.h> 145 146 #include <dev/isa/isavar.h> 147 #include <dev/isa/isadmavar.h> 148 149 #include <dev/isa/fdreg.h> 150 #include <dev/isa/fdcvar.h> 151 152 #if defined(i386) || defined(x86_64) 153 154 #include <dev/ic/mc146818reg.h> /* for NVRAM access */ 155 #include <i386/isa/nvram.h> 156 157 #if defined(i386) 158 #include "mca.h" 159 #if NMCA > 0 160 #include <machine/mca_machdep.h> /* for MCA_system */ 161 #endif 162 #endif 163 164 #endif /* i386 || x86_64 */ 165 166 #include <dev/isa/fdvar.h> 167 168 #define FDUNIT(dev) (minor(dev) / 8) 169 #define FDTYPE(dev) (minor(dev) % 8) 170 171 /* (mis)use device use flag to identify format operation */ 172 #define B_FORMAT B_DEVPRIVATE 173 174 /* controller driver configuration */ 175 int fdprint(void *, const char *); 176 177 #if NMCA > 0 178 /* MCA - specific entries */ 179 const struct fd_type mca_fd_types[] = { 180 { 18,2,36,2,0xff,0x0f,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB" }, /* 1.44MB diskette - XXX try 16ms step rate */ 181 { 9,2,18,2,0xff,0x4f,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB" }, /* 3.5 inch 720kB diskette - XXX try 24ms step rate */ 182 }; 183 #endif /* NMCA > 0 */ 184 185 /* The order of entries in the following table is important -- BEWARE! */ 186 187 #if defined(atari) 188 const struct fd_type fd_types[] = { 189 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,0xf6,1, "360KB/PC" }, /* 360kB PC diskettes */ 190 { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB" }, /* 3.5 inch 720kB diskette */ 191 { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB" }, /* 1.44MB diskette */ 192 }; 193 #else 194 const struct fd_type fd_types[] = { 195 { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB" }, /* 1.44MB diskette */ 196 { 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS,0xf6,1, "1.2MB" }, /* 1.2 MB AT-diskettes */ 197 { 9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS,0xf6,1, "360KB/AT" }, /* 360kB in 1.2MB drive */ 198 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,0xf6,1, "360KB/PC" }, /* 360kB PC diskettes */ 199 { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB" }, /* 3.5 inch 720kB diskette */ 200 { 9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS,0xf6,1, "720KB/x" }, /* 720kB in 1.2MB drive */ 201 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,0xf6,1, "360KB/x" }, /* 360kB in 720kB drive */ 202 }; 203 #endif /* defined(atari) */ 204 205 void fdcfinishattach(device_t); 206 int fdprobe(device_t, struct cfdata *, void *); 207 void fdattach(device_t, device_t, void *); 208 static int fddetach(device_t, int); 209 static int fdcintr1(struct fdc_softc *); 210 static void fdcintrcb(void *); 211 static bool fdcsuspend(device_t PMF_FN_PROTO); 212 static bool fdcresume(device_t PMF_FN_PROTO); 213 214 extern struct cfdriver fd_cd; 215 216 #ifdef atari 217 CFATTACH_DECL_NEW(fdisa, sizeof(struct fd_softc), 218 fdprobe, fdattach, fddetach, NULL); 219 #else 220 CFATTACH_DECL_NEW(fd, sizeof(struct fd_softc), 221 fdprobe, fdattach, fddetach, NULL); 222 #endif 223 224 dev_type_open(fdopen); 225 dev_type_close(fdclose); 226 dev_type_read(fdread); 227 dev_type_write(fdwrite); 228 dev_type_ioctl(fdioctl); 229 dev_type_strategy(fdstrategy); 230 231 const struct bdevsw fd_bdevsw = { 232 fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK 233 }; 234 235 const struct cdevsw fd_cdevsw = { 236 fdopen, fdclose, fdread, fdwrite, fdioctl, 237 nostop, notty, nopoll, nommap, nokqfilter, D_DISK 238 }; 239 240 void fdgetdisklabel(struct fd_softc *); 241 int fd_get_parms(struct fd_softc *); 242 void fdstart(struct fd_softc *); 243 244 struct dkdriver fddkdriver = { fdstrategy, NULL }; 245 246 #if defined(i386) || defined(x86_64) 247 const struct fd_type *fd_nvtotype(const char *, int, int); 248 #endif /* i386 || x86_64 */ 249 void fd_set_motor(struct fdc_softc *fdc, int reset); 250 void fd_motor_off(void *arg); 251 void fd_motor_on(void *arg); 252 int fdcresult(struct fdc_softc *fdc); 253 void fdcstart(struct fdc_softc *fdc); 254 void fdcstatus(device_t, int, const char *); 255 void fdctimeout(void *arg); 256 void fdcretry(struct fdc_softc *fdc); 257 void fdfinish(struct fd_softc *fd, struct buf *bp); 258 static const struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t); 259 int fdformat(dev_t, struct ne7_fd_formb *, struct lwp *); 260 static void fd_set_properties(struct fd_softc *fd); 261 262 void fd_mountroot_hook(device_t); 263 264 /* 265 * Arguments passed between fdcattach and fdprobe. 266 */ 267 struct fdc_attach_args { 268 int fa_drive; 269 const struct fd_type *fa_deftype; 270 }; 271 272 /* 273 * Print the location of a disk drive (called just before attaching the 274 * the drive). If `fdc' is not NULL, the drive was found but was not 275 * in the system config file; print the drive name as well. 276 * Return QUIET (config_find ignores this if the device was configured) to 277 * avoid printing `fdN not configured' messages. 278 */ 279 int 280 fdprint(void *aux, const char *fdc) 281 { 282 struct fdc_attach_args *fa = aux; 283 284 if (!fdc) 285 aprint_normal(" drive %d", fa->fa_drive); 286 return QUIET; 287 } 288 289 static bool 290 fdcresume(device_t self PMF_FN_ARGS) 291 { 292 struct fdc_softc *fdc = device_private(self); 293 294 (void)fdcintr1(fdc); 295 return true; 296 } 297 298 static bool 299 fdcsuspend(device_t self PMF_FN_ARGS) 300 { 301 struct fdc_softc *fdc = device_private(self); 302 int drive; 303 struct fd_softc *fd; 304 305 mutex_enter(&fdc->sc_mtx); 306 while (fdc->sc_state != DEVIDLE) 307 cv_wait(&fdc->sc_cv, &fdc->sc_mtx); 308 for (drive = 0; drive < 4; drive++) { 309 if ((fd = fdc->sc_fd[drive]) == NULL) 310 continue; 311 fd->sc_flags &= ~(FD_MOTOR|FD_MOTOR_WAIT); 312 } 313 fd_set_motor(fdc, 0); 314 mutex_exit(&fdc->sc_mtx); 315 return true; 316 } 317 318 void 319 fdc_childdet(device_t self, device_t child) 320 { 321 struct fdc_softc *fdc = device_private(self); 322 struct fd_softc *fd = device_private(child); 323 int drive = fd->sc_drive; 324 325 KASSERT(fdc->sc_fd[drive] == fd); /* but the kid is not my son */ 326 fdc->sc_fd[drive] = NULL; 327 } 328 329 int 330 fdcdetach(device_t self, int flags) 331 { 332 int rc; 333 struct fdc_softc *fdc = device_private(self); 334 335 if ((rc = config_detach_children(self, flags)) != 0) 336 return rc; 337 338 pmf_device_deregister(self); 339 340 isa_dmamap_destroy(fdc->sc_ic, fdc->sc_drq); 341 isa_drq_free(fdc->sc_ic, fdc->sc_drq); 342 343 callout_destroy(&fdc->sc_intr_ch); 344 callout_destroy(&fdc->sc_timo_ch); 345 346 cv_destroy(&fdc->sc_cv); 347 mutex_destroy(&fdc->sc_mtx); 348 349 return 0; 350 } 351 352 void 353 fdcattach(struct fdc_softc *fdc) 354 { 355 mutex_init(&fdc->sc_mtx, MUTEX_DEFAULT, IPL_BIO); 356 cv_init(&fdc->sc_cv, "fdcwakeup"); 357 callout_init(&fdc->sc_timo_ch, 0); 358 callout_init(&fdc->sc_intr_ch, 0); 359 360 fdc->sc_state = DEVIDLE; 361 TAILQ_INIT(&fdc->sc_drives); 362 363 fdc->sc_maxiosize = isa_dmamaxsize(fdc->sc_ic, fdc->sc_drq); 364 365 if (isa_drq_alloc(fdc->sc_ic, fdc->sc_drq) != 0) { 366 aprint_normal_dev(fdc->sc_dev, "can't reserve drq %d\n", 367 fdc->sc_drq); 368 return; 369 } 370 371 if (isa_dmamap_create(fdc->sc_ic, fdc->sc_drq, fdc->sc_maxiosize, 372 BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) { 373 aprint_normal_dev(fdc->sc_dev, "can't set up ISA DMA map\n"); 374 return; 375 } 376 377 config_interrupts(fdc->sc_dev, fdcfinishattach); 378 379 if (!pmf_device_register(fdc->sc_dev, fdcsuspend, fdcresume)) { 380 aprint_error_dev(fdc->sc_dev, 381 "cannot set power mgmt handler\n"); 382 } 383 } 384 385 void 386 fdcfinishattach(device_t self) 387 { 388 struct fdc_softc *fdc = device_private(self); 389 bus_space_tag_t iot = fdc->sc_iot; 390 bus_space_handle_t ioh = fdc->sc_ioh; 391 struct fdc_attach_args fa; 392 393 /* 394 * Reset the controller to get it into a known state. Not all 395 * probes necessarily need do this to discover the controller up 396 * front, so don't assume anything. 397 */ 398 399 bus_space_write_1(iot, ioh, fdout, 0); 400 delay(100); 401 bus_space_write_1(iot, ioh, fdout, FDO_FRST); 402 403 /* see if it can handle a command */ 404 if (out_fdc(iot, ioh, NE7CMD_SPECIFY) < 0) { 405 aprint_normal_dev(fdc->sc_dev, "can't reset controller\n"); 406 return; 407 } 408 out_fdc(iot, ioh, 0xdf); 409 out_fdc(iot, ioh, 2); 410 411 #if defined(i386) || defined(x86_64) 412 /* 413 * The NVRAM info only tells us about the first two disks on the 414 * `primary' floppy controller. 415 */ 416 /* XXX device_unit() abuse */ 417 if (device_unit(fdc->sc_dev) == 0) { 418 int type = mc146818_read(NULL, NVRAM_DISKETTE); /* XXX softc */ 419 fdc->sc_known = 1; 420 fdc->sc_knownfds[0] = fd_nvtotype(device_xname(fdc->sc_dev), 421 type, 0); 422 if (fdc->sc_knownfds[0] != NULL) 423 fdc->sc_present |= 1; 424 fdc->sc_knownfds[1] = fd_nvtotype(device_xname(fdc->sc_dev), 425 type, 1); 426 if (fdc->sc_knownfds[1] != NULL) 427 fdc->sc_present |= 2; 428 } 429 #endif /* i386 || x86_64 */ 430 431 /* physical limit: four drives per controller. */ 432 fdc->sc_state = PROBING; 433 for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) { 434 if (fdc->sc_known) { 435 if (fdc->sc_present & (1 << fa.fa_drive)) { 436 fa.fa_deftype = fdc->sc_knownfds[fa.fa_drive]; 437 config_found(fdc->sc_dev, (void *)&fa, 438 fdprint); 439 } 440 } else { 441 #if defined(atari) 442 /* 443 * Atari has a different ordening, defaults to 1.44 444 */ 445 fa.fa_deftype = &fd_types[2]; 446 #else 447 /* 448 * Default to 1.44MB on Alpha and BeBox. How do we tell 449 * on these platforms? 450 */ 451 fa.fa_deftype = &fd_types[0]; 452 #endif 453 (void)config_found_ia(fdc->sc_dev, "fdc", (void *)&fa, fdprint); 454 } 455 } 456 fdc->sc_state = DEVIDLE; 457 } 458 459 int 460 fdprobe(device_t parent, cfdata_t match, void *aux) 461 { 462 struct fdc_softc *fdc = device_private(parent); 463 struct cfdata *cf = match; 464 struct fdc_attach_args *fa = aux; 465 int drive = fa->fa_drive; 466 bus_space_tag_t iot = fdc->sc_iot; 467 bus_space_handle_t ioh = fdc->sc_ioh; 468 int n; 469 470 if (cf->cf_loc[FDCCF_DRIVE] != FDCCF_DRIVE_DEFAULT && 471 cf->cf_loc[FDCCF_DRIVE] != drive) 472 return 0; 473 /* 474 * XXX 475 * This is to work around some odd interactions between this driver 476 * and SMC Ethernet cards. 477 */ 478 if (cf->cf_loc[FDCCF_DRIVE] == FDCCF_DRIVE_DEFAULT && drive >= 2) 479 return 0; 480 481 /* Use PNP information if available */ 482 if (fdc->sc_known) 483 return 1; 484 485 mutex_enter(&fdc->sc_mtx); 486 /* toss any interrupt status */ 487 for (n = 0; n < 4; n++) { 488 out_fdc(iot, ioh, NE7CMD_SENSEI); 489 (void) fdcresult(fdc); 490 } 491 /* select drive and turn on motor */ 492 bus_space_write_1(iot, ioh, fdout, drive | FDO_FRST | FDO_MOEN(drive)); 493 /* wait for motor to spin up */ 494 /* XXX check sc_probe */ 495 (void) cv_timedwait(&fdc->sc_cv, &fdc->sc_mtx, hz / 4); 496 out_fdc(iot, ioh, NE7CMD_RECAL); 497 out_fdc(iot, ioh, drive); 498 /* wait for recalibrate, up to 2s */ 499 /* XXX check sc_probe */ 500 if (cv_timedwait(&fdc->sc_cv, &fdc->sc_mtx, 2 * hz) != EWOULDBLOCK){ 501 #ifdef FD_DEBUG 502 /* XXX */ 503 printf("fdprobe: got intr\n"); 504 #endif 505 } 506 out_fdc(iot, ioh, NE7CMD_SENSEI); 507 n = fdcresult(fdc); 508 #ifdef FD_DEBUG 509 { 510 int i; 511 printf("fdprobe: status"); 512 for (i = 0; i < n; i++) 513 printf(" %x", fdc->sc_status[i]); 514 printf("\n"); 515 } 516 #endif 517 /* turn off motor */ 518 bus_space_write_1(iot, ioh, fdout, FDO_FRST); 519 mutex_exit(&fdc->sc_mtx); 520 521 #if defined(bebox) /* XXX What is this about? --thorpej@NetBSD.org */ 522 if (n != 2 || (fdc->sc_status[1] != 0)) 523 return 0; 524 #else 525 if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20) 526 return 0; 527 #endif /* bebox */ 528 529 return 1; 530 } 531 532 /* 533 * Controller is working, and drive responded. Attach it. 534 */ 535 void 536 fdattach(device_t parent, device_t self, void *aux) 537 { 538 struct fdc_softc *fdc = device_private(parent); 539 struct fd_softc *fd = device_private(self); 540 struct fdc_attach_args *fa = aux; 541 const struct fd_type *type = fa->fa_deftype; 542 int drive = fa->fa_drive; 543 544 fd->sc_dev = self; 545 546 callout_init(&fd->sc_motoron_ch, 0); 547 callout_init(&fd->sc_motoroff_ch, 0); 548 549 /* XXX Allow `flags' to override device type? */ 550 551 if (type) 552 aprint_normal(": %s, %d cyl, %d head, %d sec\n", type->name, 553 type->cyls, type->heads, type->sectrac); 554 else 555 aprint_normal(": density unknown\n"); 556 557 bufq_alloc(&fd->sc_q, "disksort", BUFQ_SORT_CYLINDER); 558 fd->sc_cylin = -1; 559 fd->sc_drive = drive; 560 fd->sc_deftype = type; 561 fdc->sc_fd[drive] = fd; 562 563 /* 564 * Initialize and attach the disk structure. 565 */ 566 disk_init(&fd->sc_dk, device_xname(fd->sc_dev), &fddkdriver); 567 disk_attach(&fd->sc_dk); 568 569 /* 570 * Establish a mountroot hook. 571 */ 572 fd->sc_roothook = 573 mountroothook_establish(fd_mountroot_hook, fd->sc_dev); 574 575 #if NRND > 0 576 rnd_attach_source(&fd->rnd_source, device_xname(fd->sc_dev), 577 RND_TYPE_DISK, 0); 578 #endif 579 580 fd_set_properties(fd); 581 582 if (!pmf_device_register(self, NULL, NULL)) 583 aprint_error_dev(self, "cannot set power mgmt handler\n"); 584 } 585 586 static int 587 fddetach(device_t self, int flags) 588 { 589 struct fd_softc *fd = device_private(self); 590 int bmaj, cmaj, i, mn; 591 592 fd_motor_off(fd); 593 594 /* locate the major number */ 595 bmaj = bdevsw_lookup_major(&fd_bdevsw); 596 cmaj = cdevsw_lookup_major(&fd_cdevsw); 597 598 /* Nuke the vnodes for any open instances. */ 599 for (i = 0; i < MAXPARTITIONS; i++) { 600 mn = DISKMINOR(device_unit(self), i); 601 vdevgone(bmaj, mn, mn, VBLK); 602 vdevgone(cmaj, mn, mn, VCHR); 603 } 604 605 pmf_device_deregister(self); 606 607 #if 0 /* XXX need to undo at detach? */ 608 fd_set_properties(fd); 609 #endif 610 #if NRND > 0 611 rnd_detach_source(&fd->rnd_source); 612 #endif 613 614 disk_detach(&fd->sc_dk); 615 616 /* Kill off any queued buffers. */ 617 bufq_drain(fd->sc_q); 618 619 bufq_free(fd->sc_q); 620 621 callout_destroy(&fd->sc_motoroff_ch); 622 callout_destroy(&fd->sc_motoron_ch); 623 624 return 0; 625 } 626 627 #if defined(i386) || defined(x86_64) 628 /* 629 * Translate nvram type into internal data structure. Return NULL for 630 * none/unknown/unusable. 631 */ 632 const struct fd_type * 633 fd_nvtotype(const char *fdc, int nvraminfo, int drive) 634 { 635 int type; 636 637 type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0; 638 switch (type) { 639 case NVRAM_DISKETTE_NONE: 640 return NULL; 641 case NVRAM_DISKETTE_12M: 642 return &fd_types[1]; 643 case NVRAM_DISKETTE_TYPE5: 644 case NVRAM_DISKETTE_TYPE6: 645 /* XXX We really ought to handle 2.88MB format. */ 646 case NVRAM_DISKETTE_144M: 647 #if NMCA > 0 648 if (MCA_system) 649 return &mca_fd_types[0]; 650 else 651 #endif /* NMCA > 0 */ 652 return &fd_types[0]; 653 case NVRAM_DISKETTE_360K: 654 return &fd_types[3]; 655 case NVRAM_DISKETTE_720K: 656 #if NMCA > 0 657 if (MCA_system) 658 return &mca_fd_types[1]; 659 else 660 #endif /* NMCA > 0 */ 661 return &fd_types[4]; 662 default: 663 printf("%s: drive %d: unknown device type 0x%x\n", 664 fdc, drive, type); 665 return NULL; 666 } 667 } 668 #endif /* i386 || x86_64 */ 669 670 static const struct fd_type * 671 fd_dev_to_type(struct fd_softc *fd, dev_t dev) 672 { 673 u_int type = FDTYPE(dev); 674 675 if (type > __arraycount(fd_types)) 676 return NULL; 677 return type ? &fd_types[type - 1] : fd->sc_deftype; 678 } 679 680 void 681 fdstrategy(struct buf *bp) 682 { 683 struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(bp->b_dev)); 684 struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev)); 685 int sz; 686 687 /* Valid unit, controller, and request? */ 688 if (bp->b_blkno < 0 || 689 ((bp->b_bcount % FDC_BSIZE) != 0 && 690 (bp->b_flags & B_FORMAT) == 0)) { 691 bp->b_error = EINVAL; 692 goto done; 693 } 694 695 /* If it's a null transfer, return immediately. */ 696 if (bp->b_bcount == 0) 697 goto done; 698 699 sz = howmany(bp->b_bcount, FDC_BSIZE); 700 701 if (bp->b_blkno + sz > fd->sc_type->size) { 702 sz = fd->sc_type->size - bp->b_blkno; 703 if (sz == 0) { 704 /* If exactly at end of disk, return EOF. */ 705 goto done; 706 } 707 if (sz < 0) { 708 /* If past end of disk, return EINVAL. */ 709 bp->b_error = EINVAL; 710 goto done; 711 } 712 /* Otherwise, truncate request. */ 713 bp->b_bcount = sz << DEV_BSHIFT; 714 } 715 716 bp->b_rawblkno = bp->b_blkno; 717 bp->b_cylinder = 718 bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl; 719 720 #ifdef FD_DEBUG 721 printf("fdstrategy: b_blkno %llu b_bcount %d blkno %llu cylin %d " 722 "sz %d\n", (unsigned long long)bp->b_blkno, bp->b_bcount, 723 (unsigned long long)fd->sc_blkno, bp->b_cylinder, sz); 724 #endif 725 726 /* Queue transfer on drive, activate drive and controller if idle. */ 727 mutex_enter(&fdc->sc_mtx); 728 BUFQ_PUT(fd->sc_q, bp); 729 callout_stop(&fd->sc_motoroff_ch); /* a good idea */ 730 if (fd->sc_active == 0) 731 fdstart(fd); 732 #ifdef DIAGNOSTIC 733 else { 734 if (fdc->sc_state == DEVIDLE) { 735 printf("fdstrategy: controller inactive\n"); 736 fdcstart(fdc); 737 } 738 } 739 #endif 740 mutex_exit(&fdc->sc_mtx); 741 return; 742 743 done: 744 /* Toss transfer; we're done early. */ 745 bp->b_resid = bp->b_bcount; 746 biodone(bp); 747 } 748 749 void 750 fdstart(struct fd_softc *fd) 751 { 752 struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev)); 753 int active = !TAILQ_EMPTY(&fdc->sc_drives); 754 755 KASSERT(mutex_owned(&fdc->sc_mtx)); 756 /* Link into controller queue. */ 757 fd->sc_active = 1; 758 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain); 759 760 /* If controller not already active, start it. */ 761 if (!active) 762 fdcstart(fdc); 763 } 764 765 void 766 fdfinish(struct fd_softc *fd, struct buf *bp) 767 { 768 struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev)); 769 770 /* 771 * Move this drive to the end of the queue to give others a `fair' 772 * chance. We only force a switch if N operations are completed while 773 * another drive is waiting to be serviced, since there is a long motor 774 * startup delay whenever we switch. 775 */ 776 (void)BUFQ_GET(fd->sc_q); 777 if (TAILQ_NEXT(fd, sc_drivechain) && ++fd->sc_ops >= 8) { 778 fd->sc_ops = 0; 779 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain); 780 if (BUFQ_PEEK(fd->sc_q) != NULL) 781 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain); 782 else 783 fd->sc_active = 0; 784 } 785 bp->b_resid = fd->sc_bcount; 786 fd->sc_skip = 0; 787 788 #if NRND > 0 789 rnd_add_uint32(&fd->rnd_source, bp->b_blkno); 790 #endif 791 792 biodone(bp); 793 /* turn off motor 5s from now */ 794 callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd); 795 fdc->sc_state = DEVIDLE; 796 } 797 798 int 799 fdread(dev_t dev, struct uio *uio, int flags) 800 { 801 802 return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio)); 803 } 804 805 int 806 fdwrite(dev_t dev, struct uio *uio, int flags) 807 { 808 809 return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio)); 810 } 811 812 void 813 fd_set_motor(struct fdc_softc *fdc, int reset) 814 { 815 struct fd_softc *fd; 816 u_char status; 817 int n; 818 819 if ((fd = TAILQ_FIRST(&fdc->sc_drives)) != NULL) 820 status = fd->sc_drive; 821 else 822 status = 0; 823 if (!reset) 824 status |= FDO_FRST | FDO_FDMAEN; 825 for (n = 0; n < 4; n++) 826 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR)) 827 status |= FDO_MOEN(n); 828 bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout, status); 829 } 830 831 void 832 fd_motor_off(void *arg) 833 { 834 struct fd_softc *fd = arg; 835 struct fdc_softc *fdc; 836 837 fdc = device_private(device_parent(fd->sc_dev)); 838 839 mutex_enter(&fdc->sc_mtx); 840 fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT); 841 fd_set_motor(fdc, 0); 842 mutex_exit(&fdc->sc_mtx); 843 } 844 845 void 846 fd_motor_on(void *arg) 847 { 848 struct fd_softc *fd = arg; 849 struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev)); 850 851 mutex_enter(&fdc->sc_mtx); 852 fd->sc_flags &= ~FD_MOTOR_WAIT; 853 if (TAILQ_FIRST(&fdc->sc_drives) == fd && fdc->sc_state == MOTORWAIT) 854 (void)fdcintr1(fdc); 855 mutex_exit(&fdc->sc_mtx); 856 } 857 858 int 859 fdcresult(struct fdc_softc *fdc) 860 { 861 bus_space_tag_t iot = fdc->sc_iot; 862 bus_space_handle_t ioh = fdc->sc_ioh; 863 u_char i; 864 u_int j = 100000, 865 n = 0; 866 867 for (; j; j--) { 868 i = bus_space_read_1(iot, ioh, fdsts) & 869 (NE7_DIO | NE7_RQM | NE7_CB); 870 if (i == NE7_RQM) 871 return n; 872 if (i == (NE7_DIO | NE7_RQM | NE7_CB)) { 873 if (n >= sizeof(fdc->sc_status)) { 874 log(LOG_ERR, "fdcresult: overrun\n"); 875 return -1; 876 } 877 fdc->sc_status[n++] = 878 bus_space_read_1(iot, ioh, fddata); 879 } 880 delay(10); 881 } 882 log(LOG_ERR, "fdcresult: timeout\n"); 883 return -1; 884 } 885 886 int 887 out_fdc(bus_space_tag_t iot, bus_space_handle_t ioh, u_char x) 888 { 889 u_char i; 890 u_int j = 100000; 891 892 for (; j; j--) { 893 i = bus_space_read_1(iot, ioh, fdsts) & 894 (NE7_DIO | NE7_RQM); 895 if (i == NE7_RQM) { 896 bus_space_write_1(iot, ioh, fddata, x); 897 return 0; 898 } 899 delay(10); 900 } 901 return -1; 902 } 903 904 int 905 fdopen(dev_t dev, int flags, int mode, struct lwp *l) 906 { 907 struct fd_softc *fd; 908 const struct fd_type *type; 909 910 fd = device_lookup_private(&fd_cd, FDUNIT(dev)); 911 if (fd == NULL) 912 return (ENXIO); 913 914 type = fd_dev_to_type(fd, dev); 915 if (type == NULL) 916 return ENXIO; 917 918 if ((fd->sc_flags & FD_OPEN) != 0 && 919 memcmp(fd->sc_type, type, sizeof(*type))) 920 return EBUSY; 921 922 fd->sc_type_copy = *type; 923 fd->sc_type = &fd->sc_type_copy; 924 fd->sc_cylin = -1; 925 fd->sc_flags |= FD_OPEN; 926 927 fd_set_properties(fd); 928 929 return 0; 930 } 931 932 int 933 fdclose(dev_t dev, int flags, int mode, struct lwp *l) 934 { 935 struct fd_softc *fd = 936 device_lookup_private(&fd_cd, FDUNIT(dev)); 937 938 fd->sc_flags &= ~FD_OPEN; 939 fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT); 940 return 0; 941 } 942 943 void 944 fdcstart(struct fdc_softc *fdc) 945 { 946 947 KASSERT(mutex_owned(&fdc->sc_mtx)); 948 949 if (!device_is_active(fdc->sc_dev)) 950 return; 951 952 #ifdef DIAGNOSTIC 953 /* only got here if controller's drive queue was inactive; should 954 be in idle state */ 955 if (fdc->sc_state != DEVIDLE) { 956 printf("fdcstart: not idle\n"); 957 return; 958 } 959 #endif 960 (void)fdcintr1(fdc); 961 } 962 963 void 964 fdcstatus(device_t dv, int n, const char *s) 965 { 966 struct fdc_softc *fdc = device_private(device_parent(dv)); 967 char bits[64]; 968 969 if (n == 0) { 970 out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI); 971 (void) fdcresult(fdc); 972 n = 2; 973 } 974 975 aprint_normal_dev(dv, "%s", s); 976 977 switch (n) { 978 case 0: 979 printf("\n"); 980 break; 981 case 2: 982 printf(" (st0 %s cyl %d)\n", 983 bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS, 984 bits, sizeof(bits)), fdc->sc_status[1]); 985 break; 986 case 7: 987 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0], 988 NE7_ST0BITS, bits, sizeof(bits))); 989 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1], 990 NE7_ST1BITS, bits, sizeof(bits))); 991 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2], 992 NE7_ST2BITS, bits, sizeof(bits))); 993 printf(" cyl %d head %d sec %d)\n", 994 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]); 995 break; 996 #ifdef DIAGNOSTIC 997 default: 998 printf("\nfdcstatus: weird size"); 999 break; 1000 #endif 1001 } 1002 } 1003 1004 void 1005 fdctimeout(void *arg) 1006 { 1007 struct fdc_softc *fdc = arg; 1008 struct fd_softc *fd = TAILQ_FIRST(&fdc->sc_drives); 1009 1010 mutex_enter(&fdc->sc_mtx); 1011 #ifdef DEBUG 1012 log(LOG_ERR, "fdctimeout: state %d\n", fdc->sc_state); 1013 #endif 1014 fdcstatus(fd->sc_dev, 0, "timeout"); 1015 1016 if (BUFQ_PEEK(fd->sc_q) != NULL) 1017 fdc->sc_state++; 1018 else 1019 fdc->sc_state = DEVIDLE; 1020 1021 (void)fdcintr1(fdc); 1022 mutex_exit(&fdc->sc_mtx); 1023 } 1024 1025 static int 1026 fdcintr1(struct fdc_softc *fdc) 1027 { 1028 #define st0 fdc->sc_status[0] 1029 #define cyl fdc->sc_status[1] 1030 struct fd_softc *fd; 1031 struct buf *bp; 1032 bus_space_tag_t iot = fdc->sc_iot; 1033 bus_space_handle_t ioh = fdc->sc_ioh; 1034 int read, head, sec, i, nblks; 1035 struct fd_type *type; 1036 struct ne7_fd_formb *finfo = NULL; 1037 1038 KASSERT(mutex_owned(&fdc->sc_mtx)); 1039 if (fdc->sc_state == PROBING) { 1040 #ifdef DEBUG 1041 printf("fdcintr: got probe interrupt\n"); 1042 #endif 1043 fdc->sc_probe++; 1044 goto out; 1045 } 1046 1047 loop: 1048 /* Is there a drive for the controller to do a transfer with? */ 1049 fd = TAILQ_FIRST(&fdc->sc_drives); 1050 if (fd == NULL) { 1051 fdc->sc_state = DEVIDLE; 1052 goto out; 1053 } 1054 1055 /* Is there a transfer to this drive? If not, deactivate drive. */ 1056 bp = BUFQ_PEEK(fd->sc_q); 1057 if (bp == NULL) { 1058 fd->sc_ops = 0; 1059 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain); 1060 fd->sc_active = 0; 1061 goto loop; 1062 } 1063 1064 if (bp->b_flags & B_FORMAT) 1065 finfo = (struct ne7_fd_formb *)bp->b_data; 1066 1067 switch (fdc->sc_state) { 1068 case DEVIDLE: 1069 fdc->sc_errors = 0; 1070 fd->sc_skip = 0; 1071 fd->sc_bcount = bp->b_bcount; 1072 fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE); 1073 callout_stop(&fd->sc_motoroff_ch); 1074 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) { 1075 fdc->sc_state = MOTORWAIT; 1076 return 1; 1077 } 1078 if ((fd->sc_flags & FD_MOTOR) == 0) { 1079 /* Turn on the motor, being careful about pairing. */ 1080 struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1]; 1081 if (ofd && ofd->sc_flags & FD_MOTOR) { 1082 callout_stop(&ofd->sc_motoroff_ch); 1083 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT); 1084 } 1085 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT; 1086 fd_set_motor(fdc, 0); 1087 fdc->sc_state = MOTORWAIT; 1088 /* Allow .25s for motor to stabilize. */ 1089 callout_reset(&fd->sc_motoron_ch, hz / 4, 1090 fd_motor_on, fd); 1091 return 1; 1092 } 1093 /* Make sure the right drive is selected. */ 1094 fd_set_motor(fdc, 0); 1095 1096 /* fall through */ 1097 case DOSEEK: 1098 doseek: 1099 if (fd->sc_cylin == bp->b_cylinder) 1100 goto doio; 1101 1102 out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */ 1103 out_fdc(iot, ioh, fd->sc_type->steprate); 1104 out_fdc(iot, ioh, 6); /* XXX head load time == 6ms */ 1105 1106 out_fdc(iot, ioh, NE7CMD_SEEK); /* seek function */ 1107 out_fdc(iot, ioh, fd->sc_drive); /* drive number */ 1108 out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step); 1109 1110 fd->sc_cylin = -1; 1111 fdc->sc_state = SEEKWAIT; 1112 1113 iostat_seek(fd->sc_dk.dk_stats); 1114 disk_busy(&fd->sc_dk); 1115 1116 callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc); 1117 return 1; 1118 1119 case DOIO: 1120 doio: 1121 type = fd->sc_type; 1122 if (finfo) 1123 fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) - 1124 (char *)finfo; 1125 sec = fd->sc_blkno % type->seccyl; 1126 nblks = type->seccyl - sec; 1127 nblks = min(nblks, fd->sc_bcount / FDC_BSIZE); 1128 nblks = min(nblks, fdc->sc_maxiosize / FDC_BSIZE); 1129 fd->sc_nblks = nblks; 1130 fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FDC_BSIZE; 1131 head = sec / type->sectrac; 1132 sec -= head * type->sectrac; 1133 #ifdef DIAGNOSTIC 1134 { 1135 int block; 1136 block = (fd->sc_cylin * type->heads + head) 1137 * type->sectrac + sec; 1138 if (block != fd->sc_blkno) { 1139 printf("fdcintr: block %d != blkno " 1140 "%" PRId64 "\n", block, fd->sc_blkno); 1141 #ifdef DDB 1142 Debugger(); 1143 #endif 1144 } 1145 } 1146 #endif 1147 read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE; 1148 isa_dmastart(fdc->sc_ic, fdc->sc_drq, 1149 (char *)bp->b_data + fd->sc_skip, fd->sc_nbytes, 1150 NULL, read | DMAMODE_DEMAND, BUS_DMA_NOWAIT); 1151 bus_space_write_1(iot, fdc->sc_fdctlioh, 0, type->rate); 1152 #ifdef FD_DEBUG 1153 printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n", 1154 read ? "read" : "write", fd->sc_drive, fd->sc_cylin, 1155 head, sec, nblks); 1156 #endif 1157 if (finfo) { 1158 /* formatting */ 1159 if (out_fdc(iot, ioh, NE7CMD_FORMAT) < 0) { 1160 fdc->sc_errors = 4; 1161 fdcretry(fdc); 1162 goto loop; 1163 } 1164 out_fdc(iot, ioh, (head << 2) | fd->sc_drive); 1165 out_fdc(iot, ioh, finfo->fd_formb_secshift); 1166 out_fdc(iot, ioh, finfo->fd_formb_nsecs); 1167 out_fdc(iot, ioh, finfo->fd_formb_gaplen); 1168 out_fdc(iot, ioh, finfo->fd_formb_fillbyte); 1169 } else { 1170 if (read) 1171 out_fdc(iot, ioh, NE7CMD_READ); /* READ */ 1172 else 1173 out_fdc(iot, ioh, NE7CMD_WRITE); /* WRITE */ 1174 out_fdc(iot, ioh, (head << 2) | fd->sc_drive); 1175 out_fdc(iot, ioh, fd->sc_cylin); /* track */ 1176 out_fdc(iot, ioh, head); 1177 out_fdc(iot, ioh, sec + 1); /* sector +1 */ 1178 out_fdc(iot, ioh, type->secsize);/* sector size */ 1179 out_fdc(iot, ioh, type->sectrac);/* sectors/track */ 1180 out_fdc(iot, ioh, type->gap1); /* gap1 size */ 1181 out_fdc(iot, ioh, type->datalen);/* data length */ 1182 } 1183 fdc->sc_state = IOCOMPLETE; 1184 1185 disk_busy(&fd->sc_dk); 1186 1187 /* allow 2 seconds for operation */ 1188 callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc); 1189 return 1; /* will return later */ 1190 1191 case SEEKWAIT: 1192 callout_stop(&fdc->sc_timo_ch); 1193 fdc->sc_state = SEEKCOMPLETE; 1194 /* allow 1/50 second for heads to settle */ 1195 callout_reset(&fdc->sc_intr_ch, hz / 50, fdcintrcb, fdc); 1196 return 1; 1197 1198 case SEEKCOMPLETE: 1199 /* no data on seek */ 1200 disk_unbusy(&fd->sc_dk, 0, 0); 1201 1202 /* Make sure seek really happened. */ 1203 out_fdc(iot, ioh, NE7CMD_SENSEI); 1204 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || 1205 cyl != bp->b_cylinder * fd->sc_type->step) { 1206 #ifdef FD_DEBUG 1207 fdcstatus(fd->sc_dev, 2, "seek failed"); 1208 #endif 1209 fdcretry(fdc); 1210 goto loop; 1211 } 1212 fd->sc_cylin = bp->b_cylinder; 1213 goto doio; 1214 1215 case IOTIMEDOUT: 1216 isa_dmaabort(fdc->sc_ic, fdc->sc_drq); 1217 case SEEKTIMEDOUT: 1218 case RECALTIMEDOUT: 1219 case RESETTIMEDOUT: 1220 fdcretry(fdc); 1221 goto loop; 1222 1223 case IOCOMPLETE: /* IO DONE, post-analyze */ 1224 callout_stop(&fdc->sc_timo_ch); 1225 1226 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid), 1227 (bp->b_flags & B_READ)); 1228 1229 if (fdcresult(fdc) != 7 || (st0 & 0xf8) != 0) { 1230 isa_dmaabort(fdc->sc_ic, fdc->sc_drq); 1231 #ifdef FD_DEBUG 1232 fdcstatus(fd->sc_dev, 7, bp->b_flags & B_READ ? 1233 "read failed" : "write failed"); 1234 printf("blkno %llu nblks %d\n", 1235 (unsigned long long)fd->sc_blkno, fd->sc_nblks); 1236 #endif 1237 fdcretry(fdc); 1238 goto loop; 1239 } 1240 isa_dmadone(fdc->sc_ic, fdc->sc_drq); 1241 if (fdc->sc_errors) { 1242 diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF, 1243 fd->sc_skip / FDC_BSIZE, NULL); 1244 printf("\n"); 1245 fdc->sc_errors = 0; 1246 } 1247 fd->sc_blkno += fd->sc_nblks; 1248 fd->sc_skip += fd->sc_nbytes; 1249 fd->sc_bcount -= fd->sc_nbytes; 1250 if (!finfo && fd->sc_bcount > 0) { 1251 bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl; 1252 goto doseek; 1253 } 1254 fdfinish(fd, bp); 1255 goto loop; 1256 1257 case DORESET: 1258 /* try a reset, keep motor on */ 1259 fd_set_motor(fdc, 1); 1260 delay(100); 1261 fd_set_motor(fdc, 0); 1262 fdc->sc_state = RESETCOMPLETE; 1263 callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc); 1264 return 1; /* will return later */ 1265 1266 case RESETCOMPLETE: 1267 callout_stop(&fdc->sc_timo_ch); 1268 /* clear the controller output buffer */ 1269 for (i = 0; i < 4; i++) { 1270 out_fdc(iot, ioh, NE7CMD_SENSEI); 1271 (void) fdcresult(fdc); 1272 } 1273 1274 /* fall through */ 1275 case DORECAL: 1276 out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */ 1277 out_fdc(iot, ioh, fd->sc_drive); 1278 fdc->sc_state = RECALWAIT; 1279 callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc); 1280 return 1; /* will return later */ 1281 1282 case RECALWAIT: 1283 callout_stop(&fdc->sc_timo_ch); 1284 fdc->sc_state = RECALCOMPLETE; 1285 /* allow 1/30 second for heads to settle */ 1286 callout_reset(&fdc->sc_intr_ch, hz / 30, fdcintrcb, fdc); 1287 return 1; /* will return later */ 1288 1289 case RECALCOMPLETE: 1290 out_fdc(iot, ioh, NE7CMD_SENSEI); 1291 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) { 1292 #ifdef FD_DEBUG 1293 fdcstatus(fd->sc_dev, 2, "recalibrate failed"); 1294 #endif 1295 fdcretry(fdc); 1296 goto loop; 1297 } 1298 fd->sc_cylin = 0; 1299 goto doseek; 1300 1301 case MOTORWAIT: 1302 if (fd->sc_flags & FD_MOTOR_WAIT) 1303 return 1; /* time's not up yet */ 1304 goto doseek; 1305 1306 default: 1307 fdcstatus(fd->sc_dev, 0, "stray interrupt"); 1308 return 1; 1309 } 1310 #undef st0 1311 #undef cyl 1312 1313 out: 1314 cv_signal(&fdc->sc_cv); 1315 return 1; 1316 } 1317 1318 static void 1319 fdcintrcb(void *arg) 1320 { 1321 (void)fdcintr(arg); 1322 } 1323 1324 int 1325 fdcintr(void *arg) 1326 { 1327 int rc; 1328 struct fdc_softc *fdc = arg; 1329 1330 mutex_enter(&fdc->sc_mtx); 1331 rc = fdcintr1(fdc); 1332 mutex_exit(&fdc->sc_mtx); 1333 return rc; 1334 } 1335 1336 void 1337 fdcretry(struct fdc_softc *fdc) 1338 { 1339 char bits[64]; 1340 struct fd_softc *fd; 1341 struct buf *bp; 1342 1343 fd = TAILQ_FIRST(&fdc->sc_drives); 1344 bp = BUFQ_PEEK(fd->sc_q); 1345 1346 if (fd->sc_opts & FDOPT_NORETRY) 1347 goto fail; 1348 switch (fdc->sc_errors) { 1349 case 0: 1350 /* try again */ 1351 fdc->sc_state = DOSEEK; 1352 break; 1353 1354 case 1: case 2: case 3: 1355 /* didn't work; try recalibrating */ 1356 fdc->sc_state = DORECAL; 1357 break; 1358 1359 case 4: 1360 /* still no go; reset the bastard */ 1361 fdc->sc_state = DORESET; 1362 break; 1363 1364 default: 1365 fail: 1366 if ((fd->sc_opts & FDOPT_SILENT) == 0) { 1367 diskerr(bp, "fd", "hard error", LOG_PRINTF, 1368 fd->sc_skip / FDC_BSIZE, NULL); 1369 1370 printf(" (st0 %s", 1371 bitmask_snprintf(fdc->sc_status[0], 1372 NE7_ST0BITS, bits, 1373 sizeof(bits))); 1374 printf(" st1 %s", 1375 bitmask_snprintf(fdc->sc_status[1], 1376 NE7_ST1BITS, bits, 1377 sizeof(bits))); 1378 printf(" st2 %s", 1379 bitmask_snprintf(fdc->sc_status[2], 1380 NE7_ST2BITS, bits, 1381 sizeof(bits))); 1382 printf(" cyl %d head %d sec %d)\n", 1383 fdc->sc_status[3], 1384 fdc->sc_status[4], 1385 fdc->sc_status[5]); 1386 } 1387 1388 bp->b_error = EIO; 1389 fdfinish(fd, bp); 1390 } 1391 fdc->sc_errors++; 1392 } 1393 1394 int 1395 fdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) 1396 { 1397 struct fd_softc *fd = 1398 device_lookup_private(&fd_cd, FDUNIT(dev)); 1399 struct fdformat_parms *form_parms; 1400 struct fdformat_cmd *form_cmd; 1401 struct ne7_fd_formb *fd_formb; 1402 struct disklabel buffer; 1403 int error; 1404 unsigned int scratch; 1405 int il[FD_MAX_NSEC + 1]; 1406 int i, j; 1407 #ifdef __HAVE_OLD_DISKLABEL 1408 struct disklabel newlabel; 1409 #endif 1410 1411 switch (cmd) { 1412 case DIOCGDINFO: 1413 #ifdef __HAVE_OLD_DISKLABEL 1414 case ODIOCGDINFO: 1415 #endif 1416 memset(&buffer, 0, sizeof(buffer)); 1417 1418 buffer.d_secpercyl = fd->sc_type->seccyl; 1419 buffer.d_type = DTYPE_FLOPPY; 1420 buffer.d_secsize = FDC_BSIZE; 1421 1422 if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL) 1423 return EINVAL; 1424 1425 #ifdef __HAVE_OLD_DISKLABEL 1426 if (cmd == ODIOCGDINFO) { 1427 if (buffer.d_npartitions > OLDMAXPARTITIONS) 1428 return ENOTTY; 1429 memcpy(addr, &buffer, sizeof (struct olddisklabel)); 1430 } else 1431 #endif 1432 *(struct disklabel *)addr = buffer; 1433 return 0; 1434 1435 case DIOCWLABEL: 1436 if ((flag & FWRITE) == 0) 1437 return EBADF; 1438 /* XXX do something */ 1439 return 0; 1440 1441 case DIOCWDINFO: 1442 #ifdef __HAVE_OLD_DISKLABEL 1443 case ODIOCWDINFO: 1444 #endif 1445 { 1446 struct disklabel *lp; 1447 1448 if ((flag & FWRITE) == 0) 1449 return EBADF; 1450 #ifdef __HAVE_OLD_DISKLABEL 1451 if (cmd == ODIOCWDINFO) { 1452 memset(&newlabel, 0, sizeof newlabel); 1453 memcpy(&newlabel, addr, sizeof (struct olddisklabel)); 1454 lp = &newlabel; 1455 } else 1456 #endif 1457 lp = (struct disklabel *)addr; 1458 1459 error = setdisklabel(&buffer, lp, 0, NULL); 1460 if (error) 1461 return error; 1462 1463 error = writedisklabel(dev, fdstrategy, &buffer, NULL); 1464 return error; 1465 } 1466 1467 case FDIOCGETFORMAT: 1468 form_parms = (struct fdformat_parms *)addr; 1469 form_parms->fdformat_version = FDFORMAT_VERSION; 1470 form_parms->nbps = 128 * (1 << fd->sc_type->secsize); 1471 form_parms->ncyl = fd->sc_type->cyls; 1472 form_parms->nspt = fd->sc_type->sectrac; 1473 form_parms->ntrk = fd->sc_type->heads; 1474 form_parms->stepspercyl = fd->sc_type->step; 1475 form_parms->gaplen = fd->sc_type->gap2; 1476 form_parms->fillbyte = fd->sc_type->fillbyte; 1477 form_parms->interleave = fd->sc_type->interleave; 1478 switch (fd->sc_type->rate) { 1479 case FDC_500KBPS: 1480 form_parms->xfer_rate = 500 * 1024; 1481 break; 1482 case FDC_300KBPS: 1483 form_parms->xfer_rate = 300 * 1024; 1484 break; 1485 case FDC_250KBPS: 1486 form_parms->xfer_rate = 250 * 1024; 1487 break; 1488 default: 1489 return EINVAL; 1490 } 1491 return 0; 1492 1493 case FDIOCSETFORMAT: 1494 if((flag & FWRITE) == 0) 1495 return EBADF; /* must be opened for writing */ 1496 form_parms = (struct fdformat_parms *)addr; 1497 if (form_parms->fdformat_version != FDFORMAT_VERSION) 1498 return EINVAL; /* wrong version of formatting prog */ 1499 1500 scratch = form_parms->nbps >> 7; 1501 if ((form_parms->nbps & 0x7f) || ffs(scratch) == 0 || 1502 scratch & ~(1 << (ffs(scratch)-1))) 1503 /* not a power-of-two multiple of 128 */ 1504 return EINVAL; 1505 1506 switch (form_parms->xfer_rate) { 1507 case 500 * 1024: 1508 fd->sc_type->rate = FDC_500KBPS; 1509 break; 1510 case 300 * 1024: 1511 fd->sc_type->rate = FDC_300KBPS; 1512 break; 1513 case 250 * 1024: 1514 fd->sc_type->rate = FDC_250KBPS; 1515 break; 1516 default: 1517 return EINVAL; 1518 } 1519 1520 if (form_parms->nspt > FD_MAX_NSEC || 1521 form_parms->fillbyte > 0xff || 1522 form_parms->interleave > 0xff) 1523 return EINVAL; 1524 fd->sc_type->sectrac = form_parms->nspt; 1525 if (form_parms->ntrk != 2 && form_parms->ntrk != 1) 1526 return EINVAL; 1527 fd->sc_type->heads = form_parms->ntrk; 1528 fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk; 1529 fd->sc_type->secsize = ffs(scratch)-1; 1530 fd->sc_type->gap2 = form_parms->gaplen; 1531 fd->sc_type->cyls = form_parms->ncyl; 1532 fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl * 1533 form_parms->nbps / DEV_BSIZE; 1534 fd->sc_type->step = form_parms->stepspercyl; 1535 fd->sc_type->fillbyte = form_parms->fillbyte; 1536 fd->sc_type->interleave = form_parms->interleave; 1537 return 0; 1538 1539 case FDIOCFORMAT_TRACK: 1540 if((flag & FWRITE) == 0) 1541 return EBADF; /* must be opened for writing */ 1542 form_cmd = (struct fdformat_cmd *)addr; 1543 if (form_cmd->formatcmd_version != FDFORMAT_VERSION) 1544 return EINVAL; /* wrong version of formatting prog */ 1545 1546 if (form_cmd->head >= fd->sc_type->heads || 1547 form_cmd->cylinder >= fd->sc_type->cyls) { 1548 return EINVAL; 1549 } 1550 1551 fd_formb = malloc(sizeof(struct ne7_fd_formb), 1552 M_TEMP, M_NOWAIT); 1553 if (fd_formb == 0) 1554 return ENOMEM; 1555 1556 fd_formb->head = form_cmd->head; 1557 fd_formb->cyl = form_cmd->cylinder; 1558 fd_formb->transfer_rate = fd->sc_type->rate; 1559 fd_formb->fd_formb_secshift = fd->sc_type->secsize; 1560 fd_formb->fd_formb_nsecs = fd->sc_type->sectrac; 1561 fd_formb->fd_formb_gaplen = fd->sc_type->gap2; 1562 fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte; 1563 1564 memset(il, 0, sizeof il); 1565 for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) { 1566 while (il[(j%fd_formb->fd_formb_nsecs)+1]) 1567 j++; 1568 il[(j%fd_formb->fd_formb_nsecs)+1] = i; 1569 j += fd->sc_type->interleave; 1570 } 1571 for (i = 0; i < fd_formb->fd_formb_nsecs; i++) { 1572 fd_formb->fd_formb_cylno(i) = form_cmd->cylinder; 1573 fd_formb->fd_formb_headno(i) = form_cmd->head; 1574 fd_formb->fd_formb_secno(i) = il[i+1]; 1575 fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize; 1576 } 1577 1578 error = fdformat(dev, fd_formb, l); 1579 free(fd_formb, M_TEMP); 1580 return error; 1581 1582 case FDIOCGETOPTS: /* get drive options */ 1583 *(int *)addr = fd->sc_opts; 1584 return 0; 1585 1586 case FDIOCSETOPTS: /* set drive options */ 1587 fd->sc_opts = *(int *)addr; 1588 return 0; 1589 1590 default: 1591 return ENOTTY; 1592 } 1593 1594 #ifdef DIAGNOSTIC 1595 panic("fdioctl: impossible"); 1596 #endif 1597 } 1598 1599 int 1600 fdformat(dev_t dev, struct ne7_fd_formb *finfo, struct lwp *l) 1601 { 1602 int rv = 0; 1603 struct fd_softc *fd = 1604 device_lookup_private(&fd_cd, FDUNIT(dev)); 1605 struct fd_type *type = fd->sc_type; 1606 struct buf *bp; 1607 1608 /* set up a buffer header for fdstrategy() */ 1609 bp = getiobuf(NULL, false); 1610 if (bp == NULL) 1611 return ENOBUFS; 1612 1613 bp->b_cflags = BC_BUSY; 1614 bp->b_flags = B_PHYS | B_FORMAT; 1615 bp->b_proc = l->l_proc; 1616 bp->b_dev = dev; 1617 1618 /* 1619 * calculate a fake blkno, so fdstrategy() would initiate a 1620 * seek to the requested cylinder 1621 */ 1622 bp->b_blkno = (finfo->cyl * (type->sectrac * type->heads) 1623 + finfo->head * type->sectrac) * FDC_BSIZE / DEV_BSIZE; 1624 1625 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs; 1626 bp->b_data = (void *)finfo; 1627 1628 #ifdef DEBUG 1629 printf("fdformat: blkno %" PRIx64 " count %x\n", 1630 bp->b_blkno, bp->b_bcount); 1631 #endif 1632 1633 /* now do the format */ 1634 fdstrategy(bp); 1635 1636 /* ...and wait for it to complete */ 1637 rv = biowait(bp); 1638 putiobuf(bp); 1639 return rv; 1640 } 1641 1642 /* 1643 * Mountroot hook: prompt the user to enter the root file system 1644 * floppy. 1645 */ 1646 void 1647 fd_mountroot_hook(device_t dev) 1648 { 1649 int c; 1650 1651 printf("Insert filesystem floppy and press return."); 1652 cnpollc(1); 1653 for (;;) { 1654 c = cngetc(); 1655 if ((c == '\r') || (c == '\n')) { 1656 printf("\n"); 1657 break; 1658 } 1659 } 1660 cnpollc(0); 1661 } 1662 1663 static void 1664 fd_set_properties(struct fd_softc *fd) 1665 { 1666 prop_dictionary_t disk_info, odisk_info, geom; 1667 const struct fd_type *fdt; 1668 int secsize; 1669 1670 fdt = fd->sc_type; 1671 if (fdt == NULL) { 1672 fdt = fd->sc_deftype; 1673 if (fdt == NULL) 1674 return; 1675 } 1676 1677 disk_info = prop_dictionary_create(); 1678 1679 geom = prop_dictionary_create(); 1680 1681 prop_dictionary_set_uint64(geom, "sectors-per-unit", 1682 fdt->size); 1683 1684 switch (fdt->secsize) { 1685 case 2: 1686 secsize = 512; 1687 break; 1688 case 3: 1689 secsize = 1024; 1690 break; 1691 default: 1692 secsize = 0; 1693 } 1694 1695 prop_dictionary_set_uint32(geom, "sector-size", 1696 secsize); 1697 1698 prop_dictionary_set_uint16(geom, "sectors-per-track", 1699 fdt->sectrac); 1700 1701 prop_dictionary_set_uint16(geom, "tracks-per-cylinder", 1702 fdt->heads); 1703 1704 prop_dictionary_set_uint64(geom, "cylinders-per-unit", 1705 fdt->cyls); 1706 1707 prop_dictionary_set(disk_info, "geometry", geom); 1708 prop_object_release(geom); 1709 1710 prop_dictionary_set(device_properties(fd->sc_dev), 1711 "disk-info", disk_info); 1712 1713 /* 1714 * Don't release disk_info here; we keep a reference to it. 1715 * disk_detach() will release it when we go away. 1716 */ 1717 1718 odisk_info = fd->sc_dk.dk_info; 1719 fd->sc_dk.dk_info = disk_info; 1720 if (odisk_info) 1721 prop_object_release(odisk_info); 1722 } 1723