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