1 /* $NetBSD: fd.c,v 1.98 2010/08/11 11:35:08 pgoyette 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.98 2010/08/11 11:35:08 pgoyette 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) && !defined(ofppc) 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, cfdata_t, 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, const pmf_qual_t *); 212 static bool fdcresume(device_t, const pmf_qual_t *); 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, const pmf_qual_t *qual) 291 { 292 struct fdc_softc *fdc = device_private(self); 293 294 mutex_enter(&fdc->sc_mtx); 295 (void)fdcintr1(fdc); 296 mutex_exit(&fdc->sc_mtx); 297 return true; 298 } 299 300 static bool 301 fdcsuspend(device_t self, const pmf_qual_t *qual) 302 { 303 struct fdc_softc *fdc = device_private(self); 304 int drive; 305 struct fd_softc *fd; 306 307 mutex_enter(&fdc->sc_mtx); 308 while (fdc->sc_state != DEVIDLE) 309 cv_wait(&fdc->sc_cv, &fdc->sc_mtx); 310 for (drive = 0; drive < 4; drive++) { 311 if ((fd = fdc->sc_fd[drive]) == NULL) 312 continue; 313 fd->sc_flags &= ~(FD_MOTOR|FD_MOTOR_WAIT); 314 } 315 fd_set_motor(fdc, 0); 316 mutex_exit(&fdc->sc_mtx); 317 return true; 318 } 319 320 void 321 fdc_childdet(device_t self, device_t child) 322 { 323 struct fdc_softc *fdc = device_private(self); 324 struct fd_softc *fd = device_private(child); 325 int drive = fd->sc_drive; 326 327 KASSERT(fdc->sc_fd[drive] == fd); /* but the kid is not my son */ 328 fdc->sc_fd[drive] = NULL; 329 } 330 331 int 332 fdcdetach(device_t self, int flags) 333 { 334 int rc; 335 struct fdc_softc *fdc = device_private(self); 336 337 if ((rc = config_detach_children(self, flags)) != 0) 338 return rc; 339 340 pmf_device_deregister(self); 341 342 isa_dmamap_destroy(fdc->sc_ic, fdc->sc_drq); 343 isa_drq_free(fdc->sc_ic, fdc->sc_drq); 344 345 callout_destroy(&fdc->sc_intr_ch); 346 callout_destroy(&fdc->sc_timo_ch); 347 348 cv_destroy(&fdc->sc_cv); 349 mutex_destroy(&fdc->sc_mtx); 350 351 return 0; 352 } 353 354 void 355 fdcattach(struct fdc_softc *fdc) 356 { 357 mutex_init(&fdc->sc_mtx, MUTEX_DEFAULT, IPL_BIO); 358 cv_init(&fdc->sc_cv, "fdcwake"); 359 callout_init(&fdc->sc_timo_ch, 0); 360 callout_init(&fdc->sc_intr_ch, 0); 361 362 fdc->sc_state = DEVIDLE; 363 TAILQ_INIT(&fdc->sc_drives); 364 365 fdc->sc_maxiosize = isa_dmamaxsize(fdc->sc_ic, fdc->sc_drq); 366 367 if (isa_drq_alloc(fdc->sc_ic, fdc->sc_drq) != 0) { 368 aprint_normal_dev(fdc->sc_dev, "can't reserve drq %d\n", 369 fdc->sc_drq); 370 return; 371 } 372 373 if (isa_dmamap_create(fdc->sc_ic, fdc->sc_drq, fdc->sc_maxiosize, 374 BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) { 375 aprint_normal_dev(fdc->sc_dev, "can't set up ISA DMA map\n"); 376 return; 377 } 378 379 config_interrupts(fdc->sc_dev, fdcfinishattach); 380 381 if (!pmf_device_register(fdc->sc_dev, fdcsuspend, fdcresume)) { 382 aprint_error_dev(fdc->sc_dev, 383 "cannot set power mgmt handler\n"); 384 } 385 } 386 387 void 388 fdcfinishattach(device_t self) 389 { 390 struct fdc_softc *fdc = device_private(self); 391 bus_space_tag_t iot = fdc->sc_iot; 392 bus_space_handle_t ioh = fdc->sc_ioh; 393 struct fdc_attach_args fa; 394 395 /* 396 * Reset the controller to get it into a known state. Not all 397 * probes necessarily need do this to discover the controller up 398 * front, so don't assume anything. 399 */ 400 401 bus_space_write_1(iot, ioh, fdout, 0); 402 delay(100); 403 bus_space_write_1(iot, ioh, fdout, FDO_FRST); 404 405 /* see if it can handle a command */ 406 if (out_fdc(iot, ioh, NE7CMD_SPECIFY) < 0) { 407 aprint_normal_dev(fdc->sc_dev, "can't reset controller\n"); 408 return; 409 } 410 out_fdc(iot, ioh, 0xdf); 411 out_fdc(iot, ioh, 2); 412 413 #if defined(i386) || defined(x86_64) 414 /* 415 * The NVRAM info only tells us about the first two disks on the 416 * `primary' floppy controller. 417 */ 418 /* XXX device_unit() abuse */ 419 if (device_unit(fdc->sc_dev) == 0) { 420 int type = mc146818_read(NULL, NVRAM_DISKETTE); /* XXX softc */ 421 fdc->sc_known = 1; 422 fdc->sc_knownfds[0] = fd_nvtotype(device_xname(fdc->sc_dev), 423 type, 0); 424 if (fdc->sc_knownfds[0] != NULL) 425 fdc->sc_present |= 1; 426 fdc->sc_knownfds[1] = fd_nvtotype(device_xname(fdc->sc_dev), 427 type, 1); 428 if (fdc->sc_knownfds[1] != NULL) 429 fdc->sc_present |= 2; 430 } 431 #endif /* i386 || x86_64 */ 432 433 /* physical limit: four drives per controller. */ 434 fdc->sc_state = PROBING; 435 for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) { 436 if (fdc->sc_known) { 437 if (fdc->sc_present & (1 << fa.fa_drive)) { 438 fa.fa_deftype = fdc->sc_knownfds[fa.fa_drive]; 439 config_found(fdc->sc_dev, (void *)&fa, 440 fdprint); 441 } 442 } else { 443 #if defined(atari) 444 /* 445 * Atari has a different ordening, defaults to 1.44 446 */ 447 fa.fa_deftype = &fd_types[2]; 448 #else 449 /* 450 * Default to 1.44MB on Alpha and BeBox. How do we tell 451 * on these platforms? 452 */ 453 fa.fa_deftype = &fd_types[0]; 454 #endif 455 (void)config_found_ia(fdc->sc_dev, "fdc", (void *)&fa, fdprint); 456 } 457 } 458 fdc->sc_state = DEVIDLE; 459 } 460 461 int 462 fdprobe(device_t parent, cfdata_t match, void *aux) 463 { 464 struct fdc_softc *fdc = device_private(parent); 465 cfdata_t cf = match; 466 struct fdc_attach_args *fa = aux; 467 int drive = fa->fa_drive; 468 bus_space_tag_t iot = fdc->sc_iot; 469 bus_space_handle_t ioh = fdc->sc_ioh; 470 int n; 471 472 if (cf->cf_loc[FDCCF_DRIVE] != FDCCF_DRIVE_DEFAULT && 473 cf->cf_loc[FDCCF_DRIVE] != drive) 474 return 0; 475 /* 476 * XXX 477 * This is to work around some odd interactions between this driver 478 * and SMC Ethernet cards. 479 */ 480 if (cf->cf_loc[FDCCF_DRIVE] == FDCCF_DRIVE_DEFAULT && drive >= 2) 481 return 0; 482 483 /* Use PNP information if available */ 484 if (fdc->sc_known) 485 return 1; 486 487 mutex_enter(&fdc->sc_mtx); 488 /* toss any interrupt status */ 489 for (n = 0; n < 4; n++) { 490 out_fdc(iot, ioh, NE7CMD_SENSEI); 491 (void) fdcresult(fdc); 492 } 493 /* select drive and turn on motor */ 494 bus_space_write_1(iot, ioh, fdout, drive | FDO_FRST | FDO_MOEN(drive)); 495 /* wait for motor to spin up */ 496 /* XXX check sc_probe */ 497 (void) cv_timedwait(&fdc->sc_cv, &fdc->sc_mtx, hz / 4); 498 out_fdc(iot, ioh, NE7CMD_RECAL); 499 out_fdc(iot, ioh, drive); 500 /* wait for recalibrate, up to 2s */ 501 /* XXX check sc_probe */ 502 if (cv_timedwait(&fdc->sc_cv, &fdc->sc_mtx, 2 * hz) != EWOULDBLOCK){ 503 #ifdef FD_DEBUG 504 /* XXX */ 505 printf("fdprobe: got intr\n"); 506 #endif 507 } 508 out_fdc(iot, ioh, NE7CMD_SENSEI); 509 n = fdcresult(fdc); 510 #ifdef FD_DEBUG 511 { 512 int i; 513 printf("fdprobe: status"); 514 for (i = 0; i < n; i++) 515 printf(" %x", fdc->sc_status[i]); 516 printf("\n"); 517 } 518 #endif 519 /* turn off motor */ 520 bus_space_write_1(iot, ioh, fdout, FDO_FRST); 521 mutex_exit(&fdc->sc_mtx); 522 523 #if defined(bebox) /* XXX What is this about? --thorpej@NetBSD.org */ 524 if (n != 2 || (fdc->sc_status[1] != 0)) 525 return 0; 526 #else 527 if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20) 528 return 0; 529 #endif /* bebox */ 530 531 return 1; 532 } 533 534 /* 535 * Controller is working, and drive responded. Attach it. 536 */ 537 void 538 fdattach(device_t parent, device_t self, void *aux) 539 { 540 struct fdc_softc *fdc = device_private(parent); 541 struct fd_softc *fd = device_private(self); 542 struct fdc_attach_args *fa = aux; 543 const struct fd_type *type = fa->fa_deftype; 544 int drive = fa->fa_drive; 545 546 fd->sc_dev = self; 547 548 callout_init(&fd->sc_motoron_ch, 0); 549 callout_init(&fd->sc_motoroff_ch, 0); 550 551 /* XXX Allow `flags' to override device type? */ 552 553 if (type) 554 aprint_normal(": %s, %d cyl, %d head, %d sec\n", type->name, 555 type->cyls, type->heads, type->sectrac); 556 else 557 aprint_normal(": density unknown\n"); 558 559 bufq_alloc(&fd->sc_q, "disksort", BUFQ_SORT_CYLINDER); 560 fd->sc_cylin = -1; 561 fd->sc_drive = drive; 562 fd->sc_deftype = type; 563 fdc->sc_fd[drive] = fd; 564 565 /* 566 * Initialize and attach the disk structure. 567 */ 568 disk_init(&fd->sc_dk, device_xname(fd->sc_dev), &fddkdriver); 569 disk_attach(&fd->sc_dk); 570 571 /* 572 * Establish a mountroot hook. 573 */ 574 fd->sc_roothook = 575 mountroothook_establish(fd_mountroot_hook, fd->sc_dev); 576 577 #if NRND > 0 578 rnd_attach_source(&fd->rnd_source, device_xname(fd->sc_dev), 579 RND_TYPE_DISK, 0); 580 #endif 581 582 fd_set_properties(fd); 583 584 if (!pmf_device_register(self, NULL, NULL)) 585 aprint_error_dev(self, "cannot set power mgmt handler\n"); 586 } 587 588 static int 589 fddetach(device_t self, int flags) 590 { 591 struct fd_softc *fd = device_private(self); 592 int bmaj, cmaj, i, mn; 593 594 fd_motor_off(fd); 595 596 /* locate the major number */ 597 bmaj = bdevsw_lookup_major(&fd_bdevsw); 598 cmaj = cdevsw_lookup_major(&fd_cdevsw); 599 600 /* Nuke the vnodes for any open instances. */ 601 for (i = 0; i < MAXPARTITIONS; i++) { 602 mn = DISKMINOR(device_unit(self), i); 603 vdevgone(bmaj, mn, mn, VBLK); 604 vdevgone(cmaj, mn, mn, VCHR); 605 } 606 607 pmf_device_deregister(self); 608 609 #if 0 /* XXX need to undo at detach? */ 610 fd_set_properties(fd); 611 #endif 612 #if NRND > 0 613 rnd_detach_source(&fd->rnd_source); 614 #endif 615 616 disk_detach(&fd->sc_dk); 617 disk_destroy(&fd->sc_dk); 618 619 /* Kill off any queued buffers. */ 620 bufq_drain(fd->sc_q); 621 622 bufq_free(fd->sc_q); 623 624 callout_destroy(&fd->sc_motoroff_ch); 625 callout_destroy(&fd->sc_motoron_ch); 626 627 return 0; 628 } 629 630 #if defined(i386) || defined(x86_64) 631 /* 632 * Translate nvram type into internal data structure. Return NULL for 633 * none/unknown/unusable. 634 */ 635 const struct fd_type * 636 fd_nvtotype(const char *fdc, int nvraminfo, int drive) 637 { 638 int type; 639 640 type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0; 641 switch (type) { 642 case NVRAM_DISKETTE_NONE: 643 return NULL; 644 case NVRAM_DISKETTE_12M: 645 return &fd_types[1]; 646 case NVRAM_DISKETTE_TYPE5: 647 case NVRAM_DISKETTE_TYPE6: 648 /* XXX We really ought to handle 2.88MB format. */ 649 case NVRAM_DISKETTE_144M: 650 #if NMCA > 0 651 if (MCA_system) 652 return &mca_fd_types[0]; 653 else 654 #endif /* NMCA > 0 */ 655 return &fd_types[0]; 656 case NVRAM_DISKETTE_360K: 657 return &fd_types[3]; 658 case NVRAM_DISKETTE_720K: 659 #if NMCA > 0 660 if (MCA_system) 661 return &mca_fd_types[1]; 662 else 663 #endif /* NMCA > 0 */ 664 return &fd_types[4]; 665 default: 666 printf("%s: drive %d: unknown device type 0x%x\n", 667 fdc, drive, type); 668 return NULL; 669 } 670 } 671 #endif /* i386 || x86_64 */ 672 673 static const struct fd_type * 674 fd_dev_to_type(struct fd_softc *fd, dev_t dev) 675 { 676 u_int type = FDTYPE(dev); 677 678 if (type > __arraycount(fd_types)) 679 return NULL; 680 return type ? &fd_types[type - 1] : fd->sc_deftype; 681 } 682 683 void 684 fdstrategy(struct buf *bp) 685 { 686 struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(bp->b_dev)); 687 struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev)); 688 int sz; 689 690 /* Valid unit, controller, and request? */ 691 if (bp->b_blkno < 0 || 692 ((bp->b_bcount % FDC_BSIZE) != 0 && 693 (bp->b_flags & B_FORMAT) == 0)) { 694 bp->b_error = EINVAL; 695 goto done; 696 } 697 698 /* If it's a null transfer, return immediately. */ 699 if (bp->b_bcount == 0) 700 goto done; 701 702 sz = howmany(bp->b_bcount, FDC_BSIZE); 703 704 if (bp->b_blkno + sz > fd->sc_type->size) { 705 sz = fd->sc_type->size - bp->b_blkno; 706 if (sz == 0) { 707 /* If exactly at end of disk, return EOF. */ 708 goto done; 709 } 710 if (sz < 0) { 711 /* If past end of disk, return EINVAL. */ 712 bp->b_error = EINVAL; 713 goto done; 714 } 715 /* Otherwise, truncate request. */ 716 bp->b_bcount = sz << DEV_BSHIFT; 717 } 718 719 bp->b_rawblkno = bp->b_blkno; 720 bp->b_cylinder = 721 bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl; 722 723 #ifdef FD_DEBUG 724 printf("fdstrategy: b_blkno %llu b_bcount %d blkno %llu cylin %d " 725 "sz %d\n", (unsigned long long)bp->b_blkno, bp->b_bcount, 726 (unsigned long long)fd->sc_blkno, bp->b_cylinder, sz); 727 #endif 728 729 /* Queue transfer on drive, activate drive and controller if idle. */ 730 mutex_enter(&fdc->sc_mtx); 731 bufq_put(fd->sc_q, bp); 732 callout_stop(&fd->sc_motoroff_ch); /* a good idea */ 733 if (fd->sc_active == 0) 734 fdstart(fd); 735 #ifdef DIAGNOSTIC 736 else { 737 if (fdc->sc_state == DEVIDLE) { 738 printf("fdstrategy: controller inactive\n"); 739 fdcstart(fdc); 740 } 741 } 742 #endif 743 mutex_exit(&fdc->sc_mtx); 744 return; 745 746 done: 747 /* Toss transfer; we're done early. */ 748 bp->b_resid = bp->b_bcount; 749 biodone(bp); 750 } 751 752 void 753 fdstart(struct fd_softc *fd) 754 { 755 struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev)); 756 int active = !TAILQ_EMPTY(&fdc->sc_drives); 757 758 KASSERT(mutex_owned(&fdc->sc_mtx)); 759 /* Link into controller queue. */ 760 fd->sc_active = 1; 761 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain); 762 763 /* If controller not already active, start it. */ 764 if (!active) 765 fdcstart(fdc); 766 } 767 768 void 769 fdfinish(struct fd_softc *fd, struct buf *bp) 770 { 771 struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev)); 772 773 /* 774 * Move this drive to the end of the queue to give others a `fair' 775 * chance. We only force a switch if N operations are completed while 776 * another drive is waiting to be serviced, since there is a long motor 777 * startup delay whenever we switch. 778 */ 779 (void)bufq_get(fd->sc_q); 780 if (TAILQ_NEXT(fd, sc_drivechain) && ++fd->sc_ops >= 8) { 781 fd->sc_ops = 0; 782 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain); 783 if (bufq_peek(fd->sc_q) != NULL) 784 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain); 785 else 786 fd->sc_active = 0; 787 } 788 bp->b_resid = fd->sc_bcount; 789 fd->sc_skip = 0; 790 791 #if NRND > 0 792 rnd_add_uint32(&fd->rnd_source, bp->b_blkno); 793 #endif 794 795 biodone(bp); 796 /* turn off motor 5s from now */ 797 callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd); 798 fdc->sc_state = DEVIDLE; 799 } 800 801 int 802 fdread(dev_t dev, struct uio *uio, int flags) 803 { 804 805 return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio)); 806 } 807 808 int 809 fdwrite(dev_t dev, struct uio *uio, int flags) 810 { 811 812 return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio)); 813 } 814 815 void 816 fd_set_motor(struct fdc_softc *fdc, int reset) 817 { 818 struct fd_softc *fd; 819 u_char status; 820 int n; 821 822 if ((fd = TAILQ_FIRST(&fdc->sc_drives)) != NULL) 823 status = fd->sc_drive; 824 else 825 status = 0; 826 if (!reset) 827 status |= FDO_FRST | FDO_FDMAEN; 828 for (n = 0; n < 4; n++) 829 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR)) 830 status |= FDO_MOEN(n); 831 bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout, status); 832 } 833 834 void 835 fd_motor_off(void *arg) 836 { 837 struct fd_softc *fd = arg; 838 struct fdc_softc *fdc; 839 840 fdc = device_private(device_parent(fd->sc_dev)); 841 842 mutex_enter(&fdc->sc_mtx); 843 fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT); 844 fd_set_motor(fdc, 0); 845 mutex_exit(&fdc->sc_mtx); 846 } 847 848 void 849 fd_motor_on(void *arg) 850 { 851 struct fd_softc *fd = arg; 852 struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev)); 853 854 mutex_enter(&fdc->sc_mtx); 855 fd->sc_flags &= ~FD_MOTOR_WAIT; 856 if (TAILQ_FIRST(&fdc->sc_drives) == fd && fdc->sc_state == MOTORWAIT) 857 (void)fdcintr1(fdc); 858 mutex_exit(&fdc->sc_mtx); 859 } 860 861 int 862 fdcresult(struct fdc_softc *fdc) 863 { 864 bus_space_tag_t iot = fdc->sc_iot; 865 bus_space_handle_t ioh = fdc->sc_ioh; 866 u_char i; 867 u_int j = 100000, 868 n = 0; 869 870 for (; j; j--) { 871 i = bus_space_read_1(iot, ioh, fdsts) & 872 (NE7_DIO | NE7_RQM | NE7_CB); 873 if (i == NE7_RQM) 874 return n; 875 if (i == (NE7_DIO | NE7_RQM | NE7_CB)) { 876 if (n >= sizeof(fdc->sc_status)) { 877 log(LOG_ERR, "fdcresult: overrun\n"); 878 return -1; 879 } 880 fdc->sc_status[n++] = 881 bus_space_read_1(iot, ioh, fddata); 882 } 883 delay(10); 884 } 885 log(LOG_ERR, "fdcresult: timeout\n"); 886 return -1; 887 } 888 889 int 890 out_fdc(bus_space_tag_t iot, bus_space_handle_t ioh, u_char x) 891 { 892 u_char i; 893 u_int j = 100000; 894 895 for (; j; j--) { 896 i = bus_space_read_1(iot, ioh, fdsts) & 897 (NE7_DIO | NE7_RQM); 898 if (i == NE7_RQM) { 899 bus_space_write_1(iot, ioh, fddata, x); 900 return 0; 901 } 902 delay(10); 903 } 904 return -1; 905 } 906 907 int 908 fdopen(dev_t dev, int flags, int mode, struct lwp *l) 909 { 910 struct fd_softc *fd; 911 const struct fd_type *type; 912 913 fd = device_lookup_private(&fd_cd, FDUNIT(dev)); 914 if (fd == NULL) 915 return (ENXIO); 916 917 type = fd_dev_to_type(fd, dev); 918 if (type == NULL) 919 return ENXIO; 920 921 if ((fd->sc_flags & FD_OPEN) != 0 && 922 memcmp(fd->sc_type, type, sizeof(*type))) 923 return EBUSY; 924 925 fd->sc_type_copy = *type; 926 fd->sc_type = &fd->sc_type_copy; 927 fd->sc_cylin = -1; 928 fd->sc_flags |= FD_OPEN; 929 930 fd_set_properties(fd); 931 932 return 0; 933 } 934 935 int 936 fdclose(dev_t dev, int flags, int mode, struct lwp *l) 937 { 938 struct fd_softc *fd = 939 device_lookup_private(&fd_cd, FDUNIT(dev)); 940 941 fd->sc_flags &= ~FD_OPEN; 942 fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT); 943 return 0; 944 } 945 946 void 947 fdcstart(struct fdc_softc *fdc) 948 { 949 950 KASSERT(mutex_owned(&fdc->sc_mtx)); 951 952 if (!device_is_active(fdc->sc_dev)) 953 return; 954 955 #ifdef DIAGNOSTIC 956 /* only got here if controller's drive queue was inactive; should 957 be in idle state */ 958 if (fdc->sc_state != DEVIDLE) { 959 printf("fdcstart: not idle\n"); 960 return; 961 } 962 #endif 963 (void)fdcintr1(fdc); 964 } 965 966 static void 967 fdcpstatus(int n, struct fdc_softc *fdc) 968 { 969 char bits[64]; 970 971 switch (n) { 972 case 0: 973 printf("\n"); 974 break; 975 case 2: 976 snprintb(bits, sizeof(bits), NE7_ST0BITS, fdc->sc_status[0]); 977 printf(" (st0 %s cyl %d)\n", bits, fdc->sc_status[1]); 978 break; 979 case 7: 980 snprintb(bits, sizeof(bits), NE7_ST0BITS, fdc->sc_status[0]); 981 printf(" (st0 %s", bits); 982 snprintb(bits, sizeof(bits), NE7_ST1BITS, fdc->sc_status[1]); 983 printf(" st1 %s", bits); 984 snprintb(bits, sizeof(bits), NE7_ST2BITS, fdc->sc_status[2]); 985 printf(" st2 %s", bits); 986 printf(" cyl %d head %d sec %d)\n", 987 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]); 988 break; 989 #ifdef DIAGNOSTIC 990 default: 991 printf("\nfdcstatus: weird size"); 992 break; 993 #endif 994 } 995 } 996 997 void 998 fdcstatus(device_t dv, int n, const char *s) 999 { 1000 struct fdc_softc *fdc = device_private(device_parent(dv)); 1001 1002 if (n == 0) { 1003 out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI); 1004 (void) fdcresult(fdc); 1005 n = 2; 1006 } 1007 fdcpstatus(n, fdc); 1008 1009 aprint_normal_dev(dv, "%s", s); 1010 1011 } 1012 1013 void 1014 fdctimeout(void *arg) 1015 { 1016 struct fdc_softc *fdc = arg; 1017 struct fd_softc *fd = TAILQ_FIRST(&fdc->sc_drives); 1018 1019 mutex_enter(&fdc->sc_mtx); 1020 #ifdef DEBUG 1021 log(LOG_ERR, "fdctimeout: state %d\n", fdc->sc_state); 1022 #endif 1023 fdcstatus(fd->sc_dev, 0, "timeout"); 1024 1025 if (bufq_peek(fd->sc_q) != NULL) 1026 fdc->sc_state++; 1027 else 1028 fdc->sc_state = DEVIDLE; 1029 1030 (void)fdcintr1(fdc); 1031 mutex_exit(&fdc->sc_mtx); 1032 } 1033 1034 static int 1035 fdcintr1(struct fdc_softc *fdc) 1036 { 1037 #define st0 fdc->sc_status[0] 1038 #define cyl fdc->sc_status[1] 1039 struct fd_softc *fd; 1040 struct buf *bp; 1041 bus_space_tag_t iot = fdc->sc_iot; 1042 bus_space_handle_t ioh = fdc->sc_ioh; 1043 int read, head, sec, i, nblks; 1044 struct fd_type *type; 1045 struct ne7_fd_formb *finfo = NULL; 1046 1047 KASSERT(mutex_owned(&fdc->sc_mtx)); 1048 if (fdc->sc_state == PROBING) { 1049 #ifdef DEBUG 1050 printf("fdcintr: got probe interrupt\n"); 1051 #endif 1052 fdc->sc_probe++; 1053 goto out; 1054 } 1055 1056 loop: 1057 /* Is there a drive for the controller to do a transfer with? */ 1058 fd = TAILQ_FIRST(&fdc->sc_drives); 1059 if (fd == NULL) { 1060 fdc->sc_state = DEVIDLE; 1061 goto out; 1062 } 1063 1064 /* Is there a transfer to this drive? If not, deactivate drive. */ 1065 bp = bufq_peek(fd->sc_q); 1066 if (bp == NULL) { 1067 fd->sc_ops = 0; 1068 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain); 1069 fd->sc_active = 0; 1070 goto loop; 1071 } 1072 1073 if (bp->b_flags & B_FORMAT) 1074 finfo = (struct ne7_fd_formb *)bp->b_data; 1075 1076 switch (fdc->sc_state) { 1077 case DEVIDLE: 1078 fdc->sc_errors = 0; 1079 fd->sc_skip = 0; 1080 fd->sc_bcount = bp->b_bcount; 1081 fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE); 1082 callout_stop(&fd->sc_motoroff_ch); 1083 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) { 1084 fdc->sc_state = MOTORWAIT; 1085 return 1; 1086 } 1087 if ((fd->sc_flags & FD_MOTOR) == 0) { 1088 /* Turn on the motor, being careful about pairing. */ 1089 struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1]; 1090 if (ofd && ofd->sc_flags & FD_MOTOR) { 1091 callout_stop(&ofd->sc_motoroff_ch); 1092 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT); 1093 } 1094 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT; 1095 fd_set_motor(fdc, 0); 1096 fdc->sc_state = MOTORWAIT; 1097 /* Allow .25s for motor to stabilize. */ 1098 callout_reset(&fd->sc_motoron_ch, hz / 4, 1099 fd_motor_on, fd); 1100 return 1; 1101 } 1102 /* Make sure the right drive is selected. */ 1103 fd_set_motor(fdc, 0); 1104 1105 /* fall through */ 1106 case DOSEEK: 1107 doseek: 1108 if (fd->sc_cylin == bp->b_cylinder) 1109 goto doio; 1110 1111 out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */ 1112 out_fdc(iot, ioh, fd->sc_type->steprate); 1113 out_fdc(iot, ioh, 6); /* XXX head load time == 6ms */ 1114 1115 out_fdc(iot, ioh, NE7CMD_SEEK); /* seek function */ 1116 out_fdc(iot, ioh, fd->sc_drive); /* drive number */ 1117 out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step); 1118 1119 fd->sc_cylin = -1; 1120 fdc->sc_state = SEEKWAIT; 1121 1122 iostat_seek(fd->sc_dk.dk_stats); 1123 disk_busy(&fd->sc_dk); 1124 1125 callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc); 1126 return 1; 1127 1128 case DOIO: 1129 doio: 1130 type = fd->sc_type; 1131 if (finfo) 1132 fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) - 1133 (char *)finfo; 1134 sec = fd->sc_blkno % type->seccyl; 1135 nblks = type->seccyl - sec; 1136 nblks = min(nblks, fd->sc_bcount / FDC_BSIZE); 1137 nblks = min(nblks, fdc->sc_maxiosize / FDC_BSIZE); 1138 fd->sc_nblks = nblks; 1139 fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FDC_BSIZE; 1140 head = sec / type->sectrac; 1141 sec -= head * type->sectrac; 1142 #ifdef DIAGNOSTIC 1143 { 1144 int block; 1145 block = (fd->sc_cylin * type->heads + head) 1146 * type->sectrac + sec; 1147 if (block != fd->sc_blkno) { 1148 printf("fdcintr: block %d != blkno " 1149 "%" PRId64 "\n", block, fd->sc_blkno); 1150 #ifdef DDB 1151 Debugger(); 1152 #endif 1153 } 1154 } 1155 #endif 1156 read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE; 1157 isa_dmastart(fdc->sc_ic, fdc->sc_drq, 1158 (char *)bp->b_data + fd->sc_skip, fd->sc_nbytes, 1159 NULL, read | DMAMODE_DEMAND, BUS_DMA_NOWAIT); 1160 bus_space_write_1(iot, fdc->sc_fdctlioh, 0, type->rate); 1161 #ifdef FD_DEBUG 1162 printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n", 1163 read ? "read" : "write", fd->sc_drive, fd->sc_cylin, 1164 head, sec, nblks); 1165 #endif 1166 if (finfo) { 1167 /* formatting */ 1168 if (out_fdc(iot, ioh, NE7CMD_FORMAT) < 0) { 1169 fdc->sc_errors = 4; 1170 fdcretry(fdc); 1171 goto loop; 1172 } 1173 out_fdc(iot, ioh, (head << 2) | fd->sc_drive); 1174 out_fdc(iot, ioh, finfo->fd_formb_secshift); 1175 out_fdc(iot, ioh, finfo->fd_formb_nsecs); 1176 out_fdc(iot, ioh, finfo->fd_formb_gaplen); 1177 out_fdc(iot, ioh, finfo->fd_formb_fillbyte); 1178 } else { 1179 if (read) 1180 out_fdc(iot, ioh, NE7CMD_READ); /* READ */ 1181 else 1182 out_fdc(iot, ioh, NE7CMD_WRITE); /* WRITE */ 1183 out_fdc(iot, ioh, (head << 2) | fd->sc_drive); 1184 out_fdc(iot, ioh, fd->sc_cylin); /* track */ 1185 out_fdc(iot, ioh, head); 1186 out_fdc(iot, ioh, sec + 1); /* sector +1 */ 1187 out_fdc(iot, ioh, type->secsize);/* sector size */ 1188 out_fdc(iot, ioh, type->sectrac);/* sectors/track */ 1189 out_fdc(iot, ioh, type->gap1); /* gap1 size */ 1190 out_fdc(iot, ioh, type->datalen);/* data length */ 1191 } 1192 fdc->sc_state = IOCOMPLETE; 1193 1194 disk_busy(&fd->sc_dk); 1195 1196 /* allow 2 seconds for operation */ 1197 callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc); 1198 return 1; /* will return later */ 1199 1200 case SEEKWAIT: 1201 callout_stop(&fdc->sc_timo_ch); 1202 fdc->sc_state = SEEKCOMPLETE; 1203 /* allow 1/50 second for heads to settle */ 1204 callout_reset(&fdc->sc_intr_ch, hz / 50, fdcintrcb, fdc); 1205 return 1; 1206 1207 case SEEKCOMPLETE: 1208 /* no data on seek */ 1209 disk_unbusy(&fd->sc_dk, 0, 0); 1210 1211 /* Make sure seek really happened. */ 1212 out_fdc(iot, ioh, NE7CMD_SENSEI); 1213 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || 1214 cyl != bp->b_cylinder * fd->sc_type->step) { 1215 #ifdef FD_DEBUG 1216 fdcstatus(fd->sc_dev, 2, "seek failed"); 1217 #endif 1218 fdcretry(fdc); 1219 goto loop; 1220 } 1221 fd->sc_cylin = bp->b_cylinder; 1222 goto doio; 1223 1224 case IOTIMEDOUT: 1225 isa_dmaabort(fdc->sc_ic, fdc->sc_drq); 1226 case SEEKTIMEDOUT: 1227 case RECALTIMEDOUT: 1228 case RESETTIMEDOUT: 1229 fdcretry(fdc); 1230 goto loop; 1231 1232 case IOCOMPLETE: /* IO DONE, post-analyze */ 1233 callout_stop(&fdc->sc_timo_ch); 1234 1235 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid), 1236 (bp->b_flags & B_READ)); 1237 1238 if (fdcresult(fdc) != 7 || (st0 & 0xf8) != 0) { 1239 isa_dmaabort(fdc->sc_ic, fdc->sc_drq); 1240 #ifdef FD_DEBUG 1241 fdcstatus(fd->sc_dev, 7, bp->b_flags & B_READ ? 1242 "read failed" : "write failed"); 1243 printf("blkno %llu nblks %d\n", 1244 (unsigned long long)fd->sc_blkno, fd->sc_nblks); 1245 #endif 1246 fdcretry(fdc); 1247 goto loop; 1248 } 1249 isa_dmadone(fdc->sc_ic, fdc->sc_drq); 1250 if (fdc->sc_errors) { 1251 diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF, 1252 fd->sc_skip / FDC_BSIZE, NULL); 1253 printf("\n"); 1254 fdc->sc_errors = 0; 1255 } 1256 fd->sc_blkno += fd->sc_nblks; 1257 fd->sc_skip += fd->sc_nbytes; 1258 fd->sc_bcount -= fd->sc_nbytes; 1259 if (!finfo && fd->sc_bcount > 0) { 1260 bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl; 1261 goto doseek; 1262 } 1263 fdfinish(fd, bp); 1264 goto loop; 1265 1266 case DORESET: 1267 /* try a reset, keep motor on */ 1268 fd_set_motor(fdc, 1); 1269 delay(100); 1270 fd_set_motor(fdc, 0); 1271 fdc->sc_state = RESETCOMPLETE; 1272 callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc); 1273 return 1; /* will return later */ 1274 1275 case RESETCOMPLETE: 1276 callout_stop(&fdc->sc_timo_ch); 1277 /* clear the controller output buffer */ 1278 for (i = 0; i < 4; i++) { 1279 out_fdc(iot, ioh, NE7CMD_SENSEI); 1280 (void) fdcresult(fdc); 1281 } 1282 1283 /* fall through */ 1284 case DORECAL: 1285 out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */ 1286 out_fdc(iot, ioh, fd->sc_drive); 1287 fdc->sc_state = RECALWAIT; 1288 callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc); 1289 return 1; /* will return later */ 1290 1291 case RECALWAIT: 1292 callout_stop(&fdc->sc_timo_ch); 1293 fdc->sc_state = RECALCOMPLETE; 1294 /* allow 1/30 second for heads to settle */ 1295 callout_reset(&fdc->sc_intr_ch, hz / 30, fdcintrcb, fdc); 1296 return 1; /* will return later */ 1297 1298 case RECALCOMPLETE: 1299 out_fdc(iot, ioh, NE7CMD_SENSEI); 1300 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) { 1301 #ifdef FD_DEBUG 1302 fdcstatus(fd->sc_dev, 2, "recalibrate failed"); 1303 #endif 1304 fdcretry(fdc); 1305 goto loop; 1306 } 1307 fd->sc_cylin = 0; 1308 goto doseek; 1309 1310 case MOTORWAIT: 1311 if (fd->sc_flags & FD_MOTOR_WAIT) 1312 return 1; /* time's not up yet */ 1313 goto doseek; 1314 1315 default: 1316 fdcstatus(fd->sc_dev, 0, "stray interrupt"); 1317 return 1; 1318 } 1319 #undef st0 1320 #undef cyl 1321 1322 out: 1323 cv_signal(&fdc->sc_cv); 1324 return 1; 1325 } 1326 1327 static void 1328 fdcintrcb(void *arg) 1329 { 1330 (void)fdcintr(arg); 1331 } 1332 1333 int 1334 fdcintr(void *arg) 1335 { 1336 int rc; 1337 struct fdc_softc *fdc = arg; 1338 1339 mutex_enter(&fdc->sc_mtx); 1340 rc = fdcintr1(fdc); 1341 mutex_exit(&fdc->sc_mtx); 1342 return rc; 1343 } 1344 1345 void 1346 fdcretry(struct fdc_softc *fdc) 1347 { 1348 struct fd_softc *fd; 1349 struct buf *bp; 1350 1351 fd = TAILQ_FIRST(&fdc->sc_drives); 1352 bp = bufq_peek(fd->sc_q); 1353 1354 if (fd->sc_opts & FDOPT_NORETRY) 1355 goto fail; 1356 switch (fdc->sc_errors) { 1357 case 0: 1358 /* try again */ 1359 fdc->sc_state = DOSEEK; 1360 break; 1361 1362 case 1: case 2: case 3: 1363 /* didn't work; try recalibrating */ 1364 fdc->sc_state = DORECAL; 1365 break; 1366 1367 case 4: 1368 /* still no go; reset the bastard */ 1369 fdc->sc_state = DORESET; 1370 break; 1371 1372 default: 1373 fail: 1374 if ((fd->sc_opts & FDOPT_SILENT) == 0) { 1375 diskerr(bp, "fd", "hard error", LOG_PRINTF, 1376 fd->sc_skip / FDC_BSIZE, NULL); 1377 fdcpstatus(7, fdc); 1378 } 1379 1380 bp->b_error = EIO; 1381 fdfinish(fd, bp); 1382 } 1383 fdc->sc_errors++; 1384 } 1385 1386 int 1387 fdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) 1388 { 1389 struct fd_softc *fd = 1390 device_lookup_private(&fd_cd, FDUNIT(dev)); 1391 struct fdformat_parms *form_parms; 1392 struct fdformat_cmd *form_cmd; 1393 struct ne7_fd_formb *fd_formb; 1394 struct disklabel buffer; 1395 int error; 1396 unsigned int scratch; 1397 int il[FD_MAX_NSEC + 1]; 1398 int i, j; 1399 #ifdef __HAVE_OLD_DISKLABEL 1400 struct disklabel newlabel; 1401 #endif 1402 1403 error = disk_ioctl(&fd->sc_dk, cmd, addr, flag, l); 1404 if (error != EPASSTHROUGH) 1405 return (error); 1406 1407 switch (cmd) { 1408 case DIOCGDINFO: 1409 #ifdef __HAVE_OLD_DISKLABEL 1410 case ODIOCGDINFO: 1411 #endif 1412 memset(&buffer, 0, sizeof(buffer)); 1413 1414 buffer.d_type = DTYPE_FLOPPY; 1415 buffer.d_secsize = FDC_BSIZE; 1416 buffer.d_nsectors = fd->sc_type->sectrac; 1417 buffer.d_ntracks = fd->sc_type->heads; 1418 buffer.d_ncylinders = fd->sc_type->cyls; 1419 buffer.d_secpercyl = fd->sc_type->seccyl; 1420 buffer.d_secperunit = fd->sc_type->size; 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 FD_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