1 /* $NetBSD: fd.c,v 1.69 2006/10/12 01:31:16 christos 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.69 2006/10/12 01:31:16 christos 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, NULL }; 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_t dev, struct uio *uio, int flags __unused) 701 { 702 703 return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio)); 704 } 705 706 int 707 fdwrite(dev_t dev, struct uio *uio, int flags __unused) 708 { 709 710 return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio)); 711 } 712 713 void 714 fd_set_motor(fdc, reset) 715 struct fdc_softc *fdc; 716 int reset; 717 { 718 struct fd_softc *fd; 719 u_char status; 720 int n; 721 722 if ((fd = TAILQ_FIRST(&fdc->sc_drives)) != NULL) 723 status = fd->sc_drive; 724 else 725 status = 0; 726 if (!reset) 727 status |= FDO_FRST | FDO_FDMAEN; 728 for (n = 0; n < 4; n++) 729 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR)) 730 status |= FDO_MOEN(n); 731 bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout, status); 732 } 733 734 void 735 fd_motor_off(arg) 736 void *arg; 737 { 738 struct fd_softc *fd = arg; 739 int s; 740 741 s = splbio(); 742 fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT); 743 fd_set_motor((struct fdc_softc *)device_parent(&fd->sc_dev), 0); 744 splx(s); 745 } 746 747 void 748 fd_motor_on(arg) 749 void *arg; 750 { 751 struct fd_softc *fd = arg; 752 struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dev); 753 int s; 754 755 s = splbio(); 756 fd->sc_flags &= ~FD_MOTOR_WAIT; 757 if ((TAILQ_FIRST(&fdc->sc_drives) == fd) &&(fdc->sc_state == MOTORWAIT)) 758 (void) fdcintr(fdc); 759 splx(s); 760 } 761 762 int 763 fdcresult(fdc) 764 struct fdc_softc *fdc; 765 { 766 bus_space_tag_t iot = fdc->sc_iot; 767 bus_space_handle_t ioh = fdc->sc_ioh; 768 u_char i; 769 u_int j = 100000, 770 n = 0; 771 772 for (; j; j--) { 773 i = bus_space_read_1(iot, ioh, fdsts) & 774 (NE7_DIO | NE7_RQM | NE7_CB); 775 if (i == NE7_RQM) 776 return n; 777 if (i == (NE7_DIO | NE7_RQM | NE7_CB)) { 778 if (n >= sizeof(fdc->sc_status)) { 779 log(LOG_ERR, "fdcresult: overrun\n"); 780 return -1; 781 } 782 fdc->sc_status[n++] = 783 bus_space_read_1(iot, ioh, fddata); 784 } 785 delay(10); 786 } 787 log(LOG_ERR, "fdcresult: timeout\n"); 788 return -1; 789 } 790 791 int 792 out_fdc(iot, ioh, x) 793 bus_space_tag_t iot; 794 bus_space_handle_t ioh; 795 u_char x; 796 { 797 u_char i; 798 u_int j = 100000; 799 800 for (; j; j--) { 801 i = bus_space_read_1(iot, ioh, fdsts) & 802 (NE7_DIO | NE7_RQM); 803 if (i == NE7_RQM) { 804 bus_space_write_1(iot, ioh, fddata, x); 805 return 0; 806 } 807 delay(10); 808 } 809 return -1; 810 } 811 812 int 813 fdopen(dev_t dev, int flags __unused, int mode __unused, struct lwp *l __unused) 814 { 815 struct fd_softc *fd; 816 const struct fd_type *type; 817 818 fd = device_lookup(&fd_cd, FDUNIT(dev)); 819 if (fd == NULL) 820 return (ENXIO); 821 822 type = fd_dev_to_type(fd, dev); 823 if (type == NULL) 824 return ENXIO; 825 826 if ((fd->sc_flags & FD_OPEN) != 0 && 827 memcmp(fd->sc_type, type, sizeof(*type))) 828 return EBUSY; 829 830 fd->sc_type_copy = *type; 831 fd->sc_type = &fd->sc_type_copy; 832 fd->sc_cylin = -1; 833 fd->sc_flags |= FD_OPEN; 834 835 return 0; 836 } 837 838 int 839 fdclose(dev_t dev, int flags __unused, int mode __unused, struct lwp *l __unused) 840 { 841 struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev)); 842 843 fd->sc_flags &= ~FD_OPEN; 844 fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT); 845 return 0; 846 } 847 848 void 849 fdcstart(fdc) 850 struct fdc_softc *fdc; 851 { 852 853 #ifdef DIAGNOSTIC 854 /* only got here if controller's drive queue was inactive; should 855 be in idle state */ 856 if (fdc->sc_state != DEVIDLE) { 857 printf("fdcstart: not idle\n"); 858 return; 859 } 860 #endif 861 (void) fdcintr(fdc); 862 } 863 864 void 865 fdcstatus(dv, n, s) 866 struct device *dv; 867 int n; 868 const char *s; 869 { 870 struct fdc_softc *fdc = (void *)device_parent(dv); 871 char bits[64]; 872 873 if (n == 0) { 874 out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI); 875 (void) fdcresult(fdc); 876 n = 2; 877 } 878 879 printf("%s: %s", dv->dv_xname, s); 880 881 switch (n) { 882 case 0: 883 printf("\n"); 884 break; 885 case 2: 886 printf(" (st0 %s cyl %d)\n", 887 bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS, 888 bits, sizeof(bits)), fdc->sc_status[1]); 889 break; 890 case 7: 891 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0], 892 NE7_ST0BITS, bits, sizeof(bits))); 893 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1], 894 NE7_ST1BITS, bits, sizeof(bits))); 895 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2], 896 NE7_ST2BITS, bits, sizeof(bits))); 897 printf(" cyl %d head %d sec %d)\n", 898 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]); 899 break; 900 #ifdef DIAGNOSTIC 901 default: 902 printf("\nfdcstatus: weird size"); 903 break; 904 #endif 905 } 906 } 907 908 void 909 fdctimeout(arg) 910 void *arg; 911 { 912 struct fdc_softc *fdc = arg; 913 struct fd_softc *fd = TAILQ_FIRST(&fdc->sc_drives); 914 int s; 915 916 s = splbio(); 917 #ifdef DEBUG 918 log(LOG_ERR, "fdctimeout: state %d\n", fdc->sc_state); 919 #endif 920 fdcstatus(&fd->sc_dev, 0, "timeout"); 921 922 if (BUFQ_PEEK(fd->sc_q) != NULL) 923 fdc->sc_state++; 924 else 925 fdc->sc_state = DEVIDLE; 926 927 (void) fdcintr(fdc); 928 splx(s); 929 } 930 931 void 932 fdcpseudointr(arg) 933 void *arg; 934 { 935 int s; 936 937 /* Just ensure it has the right spl. */ 938 s = splbio(); 939 (void) fdcintr(arg); 940 splx(s); 941 } 942 943 int 944 fdcintr(arg) 945 void *arg; 946 { 947 struct fdc_softc *fdc = arg; 948 #define st0 fdc->sc_status[0] 949 #define cyl fdc->sc_status[1] 950 struct fd_softc *fd; 951 struct buf *bp; 952 bus_space_tag_t iot = fdc->sc_iot; 953 bus_space_handle_t ioh = fdc->sc_ioh; 954 int read, head, sec, i, nblks; 955 struct fd_type *type; 956 struct ne7_fd_formb *finfo = NULL; 957 958 if (fdc->sc_state == PROBING) { 959 #ifdef DEBUG 960 printf("fdcintr: got probe interrupt\n"); 961 #endif 962 wakeup(fdc); 963 return 1; 964 } 965 966 loop: 967 /* Is there a drive for the controller to do a transfer with? */ 968 fd = TAILQ_FIRST(&fdc->sc_drives); 969 if (fd == NULL) { 970 fdc->sc_state = DEVIDLE; 971 return 1; 972 } 973 974 /* Is there a transfer to this drive? If not, deactivate drive. */ 975 bp = BUFQ_PEEK(fd->sc_q); 976 if (bp == NULL) { 977 fd->sc_ops = 0; 978 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain); 979 fd->sc_active = 0; 980 goto loop; 981 } 982 983 if (bp->b_flags & B_FORMAT) 984 finfo = (struct ne7_fd_formb *)bp->b_data; 985 986 switch (fdc->sc_state) { 987 case DEVIDLE: 988 fdc->sc_errors = 0; 989 fd->sc_skip = 0; 990 fd->sc_bcount = bp->b_bcount; 991 fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE); 992 callout_stop(&fd->sc_motoroff_ch); 993 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) { 994 fdc->sc_state = MOTORWAIT; 995 return 1; 996 } 997 if ((fd->sc_flags & FD_MOTOR) == 0) { 998 /* Turn on the motor, being careful about pairing. */ 999 struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1]; 1000 if (ofd && ofd->sc_flags & FD_MOTOR) { 1001 callout_stop(&ofd->sc_motoroff_ch); 1002 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT); 1003 } 1004 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT; 1005 fd_set_motor(fdc, 0); 1006 fdc->sc_state = MOTORWAIT; 1007 /* Allow .25s for motor to stabilize. */ 1008 callout_reset(&fd->sc_motoron_ch, hz / 4, 1009 fd_motor_on, fd); 1010 return 1; 1011 } 1012 /* Make sure the right drive is selected. */ 1013 fd_set_motor(fdc, 0); 1014 1015 /* fall through */ 1016 case DOSEEK: 1017 doseek: 1018 if (fd->sc_cylin == bp->b_cylinder) 1019 goto doio; 1020 1021 out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */ 1022 out_fdc(iot, ioh, fd->sc_type->steprate); 1023 out_fdc(iot, ioh, 6); /* XXX head load time == 6ms */ 1024 1025 out_fdc(iot, ioh, NE7CMD_SEEK); /* seek function */ 1026 out_fdc(iot, ioh, fd->sc_drive); /* drive number */ 1027 out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step); 1028 1029 fd->sc_cylin = -1; 1030 fdc->sc_state = SEEKWAIT; 1031 1032 iostat_seek(fd->sc_dk.dk_stats); 1033 disk_busy(&fd->sc_dk); 1034 1035 callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc); 1036 return 1; 1037 1038 case DOIO: 1039 doio: 1040 type = fd->sc_type; 1041 if (finfo) 1042 fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) - 1043 (char *)finfo; 1044 sec = fd->sc_blkno % type->seccyl; 1045 nblks = type->seccyl - sec; 1046 nblks = min(nblks, fd->sc_bcount / FDC_BSIZE); 1047 nblks = min(nblks, fdc->sc_maxiosize / FDC_BSIZE); 1048 fd->sc_nblks = nblks; 1049 fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FDC_BSIZE; 1050 head = sec / type->sectrac; 1051 sec -= head * type->sectrac; 1052 #ifdef DIAGNOSTIC 1053 { 1054 int block; 1055 block = (fd->sc_cylin * type->heads + head) 1056 * type->sectrac + sec; 1057 if (block != fd->sc_blkno) { 1058 printf("fdcintr: block %d != blkno " 1059 "%" PRId64 "\n", block, fd->sc_blkno); 1060 #ifdef DDB 1061 Debugger(); 1062 #endif 1063 } 1064 } 1065 #endif 1066 read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE; 1067 isa_dmastart(fdc->sc_ic, fdc->sc_drq, 1068 bp->b_data + fd->sc_skip, fd->sc_nbytes, 1069 NULL, read | DMAMODE_DEMAND, BUS_DMA_NOWAIT); 1070 bus_space_write_1(iot, fdc->sc_fdctlioh, 0, type->rate); 1071 #ifdef FD_DEBUG 1072 printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n", 1073 read ? "read" : "write", fd->sc_drive, fd->sc_cylin, 1074 head, sec, nblks); 1075 #endif 1076 if (finfo) { 1077 /* formatting */ 1078 if (out_fdc(iot, ioh, NE7CMD_FORMAT) < 0) { 1079 fdc->sc_errors = 4; 1080 fdcretry(fdc); 1081 goto loop; 1082 } 1083 out_fdc(iot, ioh, (head << 2) | fd->sc_drive); 1084 out_fdc(iot, ioh, finfo->fd_formb_secshift); 1085 out_fdc(iot, ioh, finfo->fd_formb_nsecs); 1086 out_fdc(iot, ioh, finfo->fd_formb_gaplen); 1087 out_fdc(iot, ioh, finfo->fd_formb_fillbyte); 1088 } else { 1089 if (read) 1090 out_fdc(iot, ioh, NE7CMD_READ); /* READ */ 1091 else 1092 out_fdc(iot, ioh, NE7CMD_WRITE); /* WRITE */ 1093 out_fdc(iot, ioh, (head << 2) | fd->sc_drive); 1094 out_fdc(iot, ioh, fd->sc_cylin); /* track */ 1095 out_fdc(iot, ioh, head); 1096 out_fdc(iot, ioh, sec + 1); /* sector +1 */ 1097 out_fdc(iot, ioh, type->secsize);/* sector size */ 1098 out_fdc(iot, ioh, type->sectrac);/* sectors/track */ 1099 out_fdc(iot, ioh, type->gap1); /* gap1 size */ 1100 out_fdc(iot, ioh, type->datalen);/* data length */ 1101 } 1102 fdc->sc_state = IOCOMPLETE; 1103 1104 disk_busy(&fd->sc_dk); 1105 1106 /* allow 2 seconds for operation */ 1107 callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc); 1108 return 1; /* will return later */ 1109 1110 case SEEKWAIT: 1111 callout_stop(&fdc->sc_timo_ch); 1112 fdc->sc_state = SEEKCOMPLETE; 1113 /* allow 1/50 second for heads to settle */ 1114 callout_reset(&fdc->sc_intr_ch, hz / 50, fdcpseudointr, fdc); 1115 return 1; 1116 1117 case SEEKCOMPLETE: 1118 /* no data on seek */ 1119 disk_unbusy(&fd->sc_dk, 0, 0); 1120 1121 /* Make sure seek really happened. */ 1122 out_fdc(iot, ioh, NE7CMD_SENSEI); 1123 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || 1124 cyl != bp->b_cylinder * fd->sc_type->step) { 1125 #ifdef FD_DEBUG 1126 fdcstatus(&fd->sc_dev, 2, "seek failed"); 1127 #endif 1128 fdcretry(fdc); 1129 goto loop; 1130 } 1131 fd->sc_cylin = bp->b_cylinder; 1132 goto doio; 1133 1134 case IOTIMEDOUT: 1135 isa_dmaabort(fdc->sc_ic, fdc->sc_drq); 1136 case SEEKTIMEDOUT: 1137 case RECALTIMEDOUT: 1138 case RESETTIMEDOUT: 1139 fdcretry(fdc); 1140 goto loop; 1141 1142 case IOCOMPLETE: /* IO DONE, post-analyze */ 1143 callout_stop(&fdc->sc_timo_ch); 1144 1145 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid), 1146 (bp->b_flags & B_READ)); 1147 1148 if (fdcresult(fdc) != 7 || (st0 & 0xf8) != 0) { 1149 isa_dmaabort(fdc->sc_ic, fdc->sc_drq); 1150 #ifdef FD_DEBUG 1151 fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ? 1152 "read failed" : "write failed"); 1153 printf("blkno %llu nblks %d\n", 1154 (unsigned long long)fd->sc_blkno, fd->sc_nblks); 1155 #endif 1156 fdcretry(fdc); 1157 goto loop; 1158 } 1159 isa_dmadone(fdc->sc_ic, fdc->sc_drq); 1160 if (fdc->sc_errors) { 1161 diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF, 1162 fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL); 1163 printf("\n"); 1164 fdc->sc_errors = 0; 1165 } 1166 fd->sc_blkno += fd->sc_nblks; 1167 fd->sc_skip += fd->sc_nbytes; 1168 fd->sc_bcount -= fd->sc_nbytes; 1169 if (!finfo && fd->sc_bcount > 0) { 1170 bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl; 1171 goto doseek; 1172 } 1173 fdfinish(fd, bp); 1174 goto loop; 1175 1176 case DORESET: 1177 /* try a reset, keep motor on */ 1178 fd_set_motor(fdc, 1); 1179 delay(100); 1180 fd_set_motor(fdc, 0); 1181 fdc->sc_state = RESETCOMPLETE; 1182 callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc); 1183 return 1; /* will return later */ 1184 1185 case RESETCOMPLETE: 1186 callout_stop(&fdc->sc_timo_ch); 1187 /* clear the controller output buffer */ 1188 for (i = 0; i < 4; i++) { 1189 out_fdc(iot, ioh, NE7CMD_SENSEI); 1190 (void) fdcresult(fdc); 1191 } 1192 1193 /* fall through */ 1194 case DORECAL: 1195 out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */ 1196 out_fdc(iot, ioh, fd->sc_drive); 1197 fdc->sc_state = RECALWAIT; 1198 callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc); 1199 return 1; /* will return later */ 1200 1201 case RECALWAIT: 1202 callout_stop(&fdc->sc_timo_ch); 1203 fdc->sc_state = RECALCOMPLETE; 1204 /* allow 1/30 second for heads to settle */ 1205 callout_reset(&fdc->sc_intr_ch, hz / 30, fdcpseudointr, fdc); 1206 return 1; /* will return later */ 1207 1208 case RECALCOMPLETE: 1209 out_fdc(iot, ioh, NE7CMD_SENSEI); 1210 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) { 1211 #ifdef FD_DEBUG 1212 fdcstatus(&fd->sc_dev, 2, "recalibrate failed"); 1213 #endif 1214 fdcretry(fdc); 1215 goto loop; 1216 } 1217 fd->sc_cylin = 0; 1218 goto doseek; 1219 1220 case MOTORWAIT: 1221 if (fd->sc_flags & FD_MOTOR_WAIT) 1222 return 1; /* time's not up yet */ 1223 goto doseek; 1224 1225 default: 1226 fdcstatus(&fd->sc_dev, 0, "stray interrupt"); 1227 return 1; 1228 } 1229 #ifdef DIAGNOSTIC 1230 panic("fdcintr: impossible"); 1231 #endif 1232 #undef st0 1233 #undef cyl 1234 } 1235 1236 void 1237 fdcretry(fdc) 1238 struct fdc_softc *fdc; 1239 { 1240 char bits[64]; 1241 struct fd_softc *fd; 1242 struct buf *bp; 1243 1244 fd = TAILQ_FIRST(&fdc->sc_drives); 1245 bp = BUFQ_PEEK(fd->sc_q); 1246 1247 if (fd->sc_opts & FDOPT_NORETRY) 1248 goto fail; 1249 switch (fdc->sc_errors) { 1250 case 0: 1251 /* try again */ 1252 fdc->sc_state = DOSEEK; 1253 break; 1254 1255 case 1: case 2: case 3: 1256 /* didn't work; try recalibrating */ 1257 fdc->sc_state = DORECAL; 1258 break; 1259 1260 case 4: 1261 /* still no go; reset the bastard */ 1262 fdc->sc_state = DORESET; 1263 break; 1264 1265 default: 1266 fail: 1267 if ((fd->sc_opts & FDOPT_SILENT) == 0) { 1268 diskerr(bp, "fd", "hard error", LOG_PRINTF, 1269 fd->sc_skip / FDC_BSIZE, 1270 (struct disklabel *)NULL); 1271 1272 printf(" (st0 %s", 1273 bitmask_snprintf(fdc->sc_status[0], 1274 NE7_ST0BITS, bits, 1275 sizeof(bits))); 1276 printf(" st1 %s", 1277 bitmask_snprintf(fdc->sc_status[1], 1278 NE7_ST1BITS, bits, 1279 sizeof(bits))); 1280 printf(" st2 %s", 1281 bitmask_snprintf(fdc->sc_status[2], 1282 NE7_ST2BITS, bits, 1283 sizeof(bits))); 1284 printf(" cyl %d head %d sec %d)\n", 1285 fdc->sc_status[3], 1286 fdc->sc_status[4], 1287 fdc->sc_status[5]); 1288 } 1289 1290 bp->b_flags |= B_ERROR; 1291 bp->b_error = EIO; 1292 fdfinish(fd, bp); 1293 } 1294 fdc->sc_errors++; 1295 } 1296 1297 int 1298 fdioctl(dev, cmd, addr, flag, l) 1299 dev_t dev; 1300 u_long cmd; 1301 caddr_t addr; 1302 int flag; 1303 struct lwp *l; 1304 { 1305 struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev)); 1306 struct fdformat_parms *form_parms; 1307 struct fdformat_cmd *form_cmd; 1308 struct ne7_fd_formb *fd_formb; 1309 struct disklabel buffer; 1310 int error; 1311 unsigned int scratch; 1312 int il[FD_MAX_NSEC + 1]; 1313 register int i, j; 1314 #ifdef __HAVE_OLD_DISKLABEL 1315 struct disklabel newlabel; 1316 #endif 1317 1318 switch (cmd) { 1319 case DIOCGDINFO: 1320 #ifdef __HAVE_OLD_DISKLABEL 1321 case ODIOCGDINFO: 1322 #endif 1323 memset(&buffer, 0, sizeof(buffer)); 1324 1325 buffer.d_secpercyl = fd->sc_type->seccyl; 1326 buffer.d_type = DTYPE_FLOPPY; 1327 buffer.d_secsize = FDC_BSIZE; 1328 1329 if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL) 1330 return EINVAL; 1331 1332 #ifdef __HAVE_OLD_DISKLABEL 1333 if (cmd == ODIOCGDINFO) { 1334 if (buffer.d_npartitions > OLDMAXPARTITIONS) 1335 return ENOTTY; 1336 memcpy(addr, &buffer, sizeof (struct olddisklabel)); 1337 } else 1338 #endif 1339 *(struct disklabel *)addr = buffer; 1340 return 0; 1341 1342 case DIOCWLABEL: 1343 if ((flag & FWRITE) == 0) 1344 return EBADF; 1345 /* XXX do something */ 1346 return 0; 1347 1348 case DIOCWDINFO: 1349 #ifdef __HAVE_OLD_DISKLABEL 1350 case ODIOCWDINFO: 1351 #endif 1352 { 1353 struct disklabel *lp; 1354 1355 if ((flag & FWRITE) == 0) 1356 return EBADF; 1357 #ifdef __HAVE_OLD_DISKLABEL 1358 if (cmd == ODIOCWDINFO) { 1359 memset(&newlabel, 0, sizeof newlabel); 1360 memcpy(&newlabel, addr, sizeof (struct olddisklabel)); 1361 lp = &newlabel; 1362 } else 1363 #endif 1364 lp = (struct disklabel *)addr; 1365 1366 error = setdisklabel(&buffer, lp, 0, NULL); 1367 if (error) 1368 return error; 1369 1370 error = writedisklabel(dev, fdstrategy, &buffer, NULL); 1371 return error; 1372 } 1373 1374 case FDIOCGETFORMAT: 1375 form_parms = (struct fdformat_parms *)addr; 1376 form_parms->fdformat_version = FDFORMAT_VERSION; 1377 form_parms->nbps = 128 * (1 << fd->sc_type->secsize); 1378 form_parms->ncyl = fd->sc_type->cyls; 1379 form_parms->nspt = fd->sc_type->sectrac; 1380 form_parms->ntrk = fd->sc_type->heads; 1381 form_parms->stepspercyl = fd->sc_type->step; 1382 form_parms->gaplen = fd->sc_type->gap2; 1383 form_parms->fillbyte = fd->sc_type->fillbyte; 1384 form_parms->interleave = fd->sc_type->interleave; 1385 switch (fd->sc_type->rate) { 1386 case FDC_500KBPS: 1387 form_parms->xfer_rate = 500 * 1024; 1388 break; 1389 case FDC_300KBPS: 1390 form_parms->xfer_rate = 300 * 1024; 1391 break; 1392 case FDC_250KBPS: 1393 form_parms->xfer_rate = 250 * 1024; 1394 break; 1395 default: 1396 return EINVAL; 1397 } 1398 return 0; 1399 1400 case FDIOCSETFORMAT: 1401 if((flag & FWRITE) == 0) 1402 return EBADF; /* must be opened for writing */ 1403 form_parms = (struct fdformat_parms *)addr; 1404 if (form_parms->fdformat_version != FDFORMAT_VERSION) 1405 return EINVAL; /* wrong version of formatting prog */ 1406 1407 scratch = form_parms->nbps >> 7; 1408 if ((form_parms->nbps & 0x7f) || ffs(scratch) == 0 || 1409 scratch & ~(1 << (ffs(scratch)-1))) 1410 /* not a power-of-two multiple of 128 */ 1411 return EINVAL; 1412 1413 switch (form_parms->xfer_rate) { 1414 case 500 * 1024: 1415 fd->sc_type->rate = FDC_500KBPS; 1416 break; 1417 case 300 * 1024: 1418 fd->sc_type->rate = FDC_300KBPS; 1419 break; 1420 case 250 * 1024: 1421 fd->sc_type->rate = FDC_250KBPS; 1422 break; 1423 default: 1424 return EINVAL; 1425 } 1426 1427 if (form_parms->nspt > FD_MAX_NSEC || 1428 form_parms->fillbyte > 0xff || 1429 form_parms->interleave > 0xff) 1430 return EINVAL; 1431 fd->sc_type->sectrac = form_parms->nspt; 1432 if (form_parms->ntrk != 2 && form_parms->ntrk != 1) 1433 return EINVAL; 1434 fd->sc_type->heads = form_parms->ntrk; 1435 fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk; 1436 fd->sc_type->secsize = ffs(scratch)-1; 1437 fd->sc_type->gap2 = form_parms->gaplen; 1438 fd->sc_type->cyls = form_parms->ncyl; 1439 fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl * 1440 form_parms->nbps / DEV_BSIZE; 1441 fd->sc_type->step = form_parms->stepspercyl; 1442 fd->sc_type->fillbyte = form_parms->fillbyte; 1443 fd->sc_type->interleave = form_parms->interleave; 1444 return 0; 1445 1446 case FDIOCFORMAT_TRACK: 1447 if((flag & FWRITE) == 0) 1448 return EBADF; /* must be opened for writing */ 1449 form_cmd = (struct fdformat_cmd *)addr; 1450 if (form_cmd->formatcmd_version != FDFORMAT_VERSION) 1451 return EINVAL; /* wrong version of formatting prog */ 1452 1453 if (form_cmd->head >= fd->sc_type->heads || 1454 form_cmd->cylinder >= fd->sc_type->cyls) { 1455 return EINVAL; 1456 } 1457 1458 fd_formb = malloc(sizeof(struct ne7_fd_formb), 1459 M_TEMP, M_NOWAIT); 1460 if (fd_formb == 0) 1461 return ENOMEM; 1462 1463 fd_formb->head = form_cmd->head; 1464 fd_formb->cyl = form_cmd->cylinder; 1465 fd_formb->transfer_rate = fd->sc_type->rate; 1466 fd_formb->fd_formb_secshift = fd->sc_type->secsize; 1467 fd_formb->fd_formb_nsecs = fd->sc_type->sectrac; 1468 fd_formb->fd_formb_gaplen = fd->sc_type->gap2; 1469 fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte; 1470 1471 memset(il, 0, sizeof il); 1472 for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) { 1473 while (il[(j%fd_formb->fd_formb_nsecs)+1]) 1474 j++; 1475 il[(j%fd_formb->fd_formb_nsecs)+1] = i; 1476 j += fd->sc_type->interleave; 1477 } 1478 for (i = 0; i < fd_formb->fd_formb_nsecs; i++) { 1479 fd_formb->fd_formb_cylno(i) = form_cmd->cylinder; 1480 fd_formb->fd_formb_headno(i) = form_cmd->head; 1481 fd_formb->fd_formb_secno(i) = il[i+1]; 1482 fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize; 1483 } 1484 1485 error = fdformat(dev, fd_formb, l); 1486 free(fd_formb, M_TEMP); 1487 return error; 1488 1489 case FDIOCGETOPTS: /* get drive options */ 1490 *(int *)addr = fd->sc_opts; 1491 return 0; 1492 1493 case FDIOCSETOPTS: /* set drive options */ 1494 fd->sc_opts = *(int *)addr; 1495 return 0; 1496 1497 default: 1498 return ENOTTY; 1499 } 1500 1501 #ifdef DIAGNOSTIC 1502 panic("fdioctl: impossible"); 1503 #endif 1504 } 1505 1506 int 1507 fdformat(dev, finfo, l) 1508 dev_t dev; 1509 struct ne7_fd_formb *finfo; 1510 struct lwp *l; 1511 { 1512 int rv = 0; 1513 struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev)); 1514 struct fd_type *type = fd->sc_type; 1515 struct buf *bp; 1516 1517 /* set up a buffer header for fdstrategy() */ 1518 bp = getiobuf_nowait(); 1519 if (bp == NULL) 1520 return ENOBUFS; 1521 1522 bp->b_vp = NULL; 1523 bp->b_flags = B_BUSY | B_PHYS | B_FORMAT; 1524 bp->b_proc = l->l_proc; 1525 bp->b_dev = dev; 1526 1527 /* 1528 * calculate a fake blkno, so fdstrategy() would initiate a 1529 * seek to the requested cylinder 1530 */ 1531 bp->b_blkno = (finfo->cyl * (type->sectrac * type->heads) 1532 + finfo->head * type->sectrac) * FDC_BSIZE / DEV_BSIZE; 1533 1534 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs; 1535 bp->b_data = (caddr_t)finfo; 1536 1537 #ifdef DEBUG 1538 printf("fdformat: blkno %" PRIx64 " count %x\n", 1539 bp->b_blkno, bp->b_bcount); 1540 #endif 1541 1542 /* now do the format */ 1543 fdstrategy(bp); 1544 1545 /* ...and wait for it to complete */ 1546 rv = biowait(bp); 1547 putiobuf(bp); 1548 return rv; 1549 } 1550 1551 /* 1552 * Mountroot hook: prompt the user to enter the root file system 1553 * floppy. 1554 */ 1555 void 1556 fd_mountroot_hook(struct device *dev __unused) 1557 { 1558 int c; 1559 1560 printf("Insert filesystem floppy and press return."); 1561 cnpollc(1); 1562 for (;;) { 1563 c = cngetc(); 1564 if ((c == '\r') || (c == '\n')) { 1565 printf("\n"); 1566 break; 1567 } 1568 } 1569 cnpollc(0); 1570 } 1571