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