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