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