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