1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Don Ahn. 7 * 8 * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu) 9 * aided by the Linux floppy driver modifications from David Bateman 10 * (dbateman@eng.uts.edu.au). 11 * 12 * Copyright (c) 1993, 1994 by 13 * jc@irbs.UUCP (John Capo) 14 * vak@zebub.msk.su (Serge Vakulenko) 15 * ache@astral.msk.su (Andrew A. Chernov) 16 * 17 * Copyright (c) 1993, 1994, 1995 by 18 * joerg_wunsch@uriah.sax.de (Joerg Wunsch) 19 * dufault@hda.com (Peter Dufault) 20 * 21 * Copyright (c) 2001 Joerg Wunsch, 22 * joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch) 23 * 24 * Redistribution and use in source and binary forms, with or without 25 * modification, are permitted provided that the following conditions 26 * are met: 27 * 1. Redistributions of source code must retain the above copyright 28 * notice, this list of conditions and the following disclaimer. 29 * 2. Redistributions in binary form must reproduce the above copyright 30 * notice, this list of conditions and the following disclaimer in the 31 * documentation and/or other materials provided with the distribution. 32 * 4. Neither the name of the University nor the names of its contributors 33 * may be used to endorse or promote products derived from this software 34 * without specific prior written permission. 35 * 36 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 39 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * 48 * from: @(#)fd.c 7.4 (Berkeley) 5/25/91 49 */ 50 51 #include <sys/cdefs.h> 52 __FBSDID("$FreeBSD$"); 53 54 #include "opt_fdc.h" 55 56 #include <sys/param.h> 57 #include <sys/bio.h> 58 #include <sys/bus.h> 59 #include <sys/devicestat.h> 60 #include <sys/disk.h> 61 #include <sys/fcntl.h> 62 #include <sys/fdcio.h> 63 #include <sys/filio.h> 64 #include <sys/kernel.h> 65 #include <sys/lock.h> 66 #include <sys/malloc.h> 67 #include <sys/module.h> 68 #include <sys/mutex.h> 69 #include <sys/proc.h> 70 #include <sys/rman.h> 71 #include <sys/systm.h> 72 73 #include <machine/bus.h> 74 #include <machine/clock.h> 75 #include <machine/stdarg.h> 76 77 #include <isa/isavar.h> 78 #include <isa/isareg.h> 79 #include <dev/fdc/fdcreg.h> 80 #include <dev/fdc/fdcvar.h> 81 #include <isa/rtc.h> 82 83 #define FDBIO_FORMAT BIO_CMD2 84 85 /* configuration flags for fdc */ 86 #define FDC_NO_FIFO (1 << 2) /* do not enable FIFO */ 87 88 /* 89 * Stop retrying after this many DMA overruns. Since each retry takes 90 * one revolution, with 300 rpm., 25 retries take approximately 5 91 * seconds which the read attempt will block in case the DMA overrun 92 * is persistent. 93 */ 94 #define FDC_DMAOV_MAX 25 95 96 /* 97 * Timeout value for the PIO loops to wait until the FDC main status 98 * register matches our expectations (request for master, direction 99 * bit). This is supposed to be a number of microseconds, although 100 * timing might actually not be very accurate. 101 * 102 * Timeouts of 100 msec are believed to be required for some broken 103 * (old) hardware. 104 */ 105 #define FDSTS_TIMEOUT 100000 106 107 /* 108 * Number of subdevices that can be used for different density types. 109 */ 110 #define NUMDENS 16 111 112 #define FDBIO_RDSECTID BIO_CMD1 113 114 /* 115 * List of native drive densities. Order must match enum fd_drivetype 116 * in <sys/fdcio.h>. Upon attaching the drive, each of the 117 * programmable subdevices is initialized with the native density 118 * definition. 119 */ 120 static struct fd_type fd_native_types[] = 121 { 122 { 0 }, /* FDT_NONE */ 123 { 9,2,0xFF,0x2A,40, 720,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_360K */ 124 { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* FDT_12M */ 125 { 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_720K */ 126 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */ 127 #if 0 /* we currently don't handle 2.88 MB */ 128 { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS, 2,0x4C,1,1,FL_MFM|FL_PERPND } /*FDT_288M*/ 129 #else 130 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */ 131 #endif 132 }; 133 134 /* 135 * 360 KB 5.25" and 720 KB 3.5" drives don't have automatic density 136 * selection, they just start out with their native density (or lose). 137 * So 1.2 MB 5.25", 1.44 MB 3.5", and 2.88 MB 3.5" drives have their 138 * respective lists of densities to search for. 139 */ 140 static struct fd_type fd_searchlist_12m[] = { 141 { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* 1.2M */ 142 { 9,2,0xFF,0x23,40, 720,FDC_300KBPS,2,0x50,1,0,FL_MFM|FL_2STEP }, /* 360K */ 143 { 9,2,0xFF,0x20,80,1440,FDC_300KBPS,2,0x50,1,0,FL_MFM }, /* 720K */ 144 }; 145 146 static struct fd_type fd_searchlist_144m[] = { 147 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */ 148 { 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */ 149 }; 150 151 /* We search for 1.44M first since this is the most common case. */ 152 static struct fd_type fd_searchlist_288m[] = { 153 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */ 154 #if 0 155 { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS, 2,0x4C,1,1,FL_MFM|FL_PERPND } /* 2.88M */ 156 #endif 157 { 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */ 158 }; 159 160 #define MAX_SEC_SIZE (128 << 3) 161 #define MAX_CYLINDER 85 /* some people really stress their drives 162 * up to cyl 82 */ 163 #define MAX_HEAD 1 164 165 devclass_t fdc_devclass; 166 167 /* 168 * Per drive structure (softc). 169 */ 170 struct fd_data { 171 struct fdc_data *fdc; /* pointer to controller structure */ 172 int fdsu; /* this units number on this controller */ 173 enum fd_drivetype type; /* drive type */ 174 struct fd_type *ft; /* pointer to current type descriptor */ 175 struct fd_type fts[NUMDENS]; /* type descriptors */ 176 int flags; 177 #define FD_OPEN 0x01 /* it's open */ 178 #define FD_NONBLOCK 0x02 /* O_NONBLOCK set */ 179 #define FD_ACTIVE 0x04 /* it's active */ 180 #define FD_MOTOR 0x08 /* motor should be on */ 181 #define FD_MOTOR_WAIT 0x10 /* motor coming up */ 182 #define FD_UA 0x20 /* force unit attention */ 183 int skip; 184 int hddrv; 185 #define FD_NO_TRACK -2 186 int track; /* where we think the head is */ 187 int options; /* user configurable options, see fdcio.h */ 188 struct callout_handle toffhandle; 189 struct callout_handle tohandle; 190 struct devstat *device_stats; 191 struct cdev *masterdev; 192 device_t dev; 193 fdu_t fdu; 194 }; 195 196 struct fdc_ivars { 197 int fdunit; 198 int fdtype; 199 }; 200 201 static devclass_t fd_devclass; 202 203 /* configuration flags for fd */ 204 #define FD_TYPEMASK 0x0f /* drive type, matches enum 205 * fd_drivetype; on i386 machines, if 206 * given as 0, use RTC type for fd0 207 * and fd1 */ 208 #define FD_DTYPE(flags) ((flags) & FD_TYPEMASK) 209 #define FD_NO_CHLINE 0x10 /* drive does not support changeline 210 * aka. unit attention */ 211 #define FD_NO_PROBE 0x20 /* don't probe drive (seek test), just 212 * assume it is there */ 213 214 /* 215 * Throughout this file the following conventions will be used: 216 * 217 * fd is a pointer to the fd_data struct for the drive in question 218 * fdc is a pointer to the fdc_data struct for the controller 219 * fdu is the floppy drive unit number 220 * fdcu is the floppy controller unit number 221 * fdsu is the floppy drive unit number on that controller. (sub-unit) 222 */ 223 224 /* 225 * Function declarations, same (chaotic) order as they appear in the 226 * file. Re-ordering is too late now, it would only obfuscate the 227 * diffs against old and offspring versions (like the PC98 one). 228 * 229 * Anyone adding functions here, please keep this sequence the same 230 * as below -- makes locating a particular function in the body much 231 * easier. 232 */ 233 static u_int8_t fdsts_rd(fdc_p); 234 static void fddata_wr(fdc_p, u_int8_t); 235 static u_int8_t fddata_rd(fdc_p); 236 #if 0 237 static u_int8_t fdin_rd(fdc_p); 238 #endif 239 static int fdc_err(struct fdc_data *, const char *); 240 static int enable_fifo(fdc_p fdc); 241 static int fd_sense_drive_status(fdc_p, int *); 242 static int fd_sense_int(fdc_p, int *, int *); 243 static int fd_read_status(fdc_p); 244 static void fdc_add_child(device_t, const char *, int); 245 static int fd_probe(device_t); 246 static int fd_attach(device_t); 247 static int fd_detach(device_t); 248 static void set_motor(struct fdc_data *, int, int); 249 # define TURNON 1 250 # define TURNOFF 0 251 static timeout_t fd_turnoff; 252 static timeout_t fd_motor_on; 253 static void fd_turnon(struct fd_data *); 254 static void fdc_reset(fdc_p); 255 static int fd_in(struct fdc_data *, int *); 256 static int out_fdc(struct fdc_data *, int); 257 /* 258 * The open function is named fdopen() to avoid confusion with fdopen() 259 * in fd(4). The difference is now only meaningful for debuggers. 260 */ 261 static d_open_t fdopen; 262 static d_close_t fdclose; 263 static d_strategy_t fdstrategy; 264 static void fdstart(struct fdc_data *); 265 static timeout_t fd_iotimeout; 266 static timeout_t fd_pseudointr; 267 static driver_intr_t fdc_intr; 268 static int fdcpio(fdc_p, long, caddr_t, u_int); 269 static int fdautoselect(struct cdev *); 270 static int fdstate(struct fdc_data *); 271 static int retrier(struct fdc_data *); 272 static void fdbiodone(struct bio *); 273 static int fdmisccmd(struct cdev *, u_int, void *); 274 static d_ioctl_t fdioctl; 275 276 static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */ 277 278 #ifdef FDC_DEBUG 279 /* CAUTION: fd_debug causes huge amounts of logging output */ 280 static int volatile fd_debug = 0; 281 #define TRACE0(arg) do { if (fd_debug) printf(arg); } while (0) 282 #define TRACE1(arg1, arg2) do { if (fd_debug) printf(arg1, arg2); } while (0) 283 #else /* FDC_DEBUG */ 284 #define TRACE0(arg) do { } while (0) 285 #define TRACE1(arg1, arg2) do { } while (0) 286 #endif /* FDC_DEBUG */ 287 288 /* 289 * Bus space handling (access to low-level IO). 290 */ 291 void 292 fdout_wr(fdc_p fdc, u_int8_t v) 293 { 294 bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v); 295 } 296 297 static u_int8_t 298 fdsts_rd(fdc_p fdc) 299 { 300 return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off); 301 } 302 303 static void 304 fddata_wr(fdc_p fdc, u_int8_t v) 305 { 306 bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v); 307 } 308 309 static u_int8_t 310 fddata_rd(fdc_p fdc) 311 { 312 return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off); 313 } 314 315 static u_int8_t 316 fdin_rd(fdc_p fdc) 317 { 318 return bus_space_read_1(fdc->portt, fdc->porth, FDIN); 319 } 320 321 static struct cdevsw fd_cdevsw = { 322 .d_version = D_VERSION, 323 .d_open = fdopen, 324 .d_close = fdclose, 325 .d_read = physread, 326 .d_write = physwrite, 327 .d_ioctl = fdioctl, 328 .d_strategy = fdstrategy, 329 .d_name = "fd", 330 .d_flags = D_DISK | D_NEEDGIANT, 331 }; 332 333 /* 334 * Auxiliary functions. Well, some only. Others are scattered 335 * throughout the entire file. 336 */ 337 static int 338 fdc_err(struct fdc_data *fdc, const char *s) 339 { 340 fdc->fdc_errs++; 341 if (s) { 342 if (fdc->fdc_errs < FDC_ERRMAX) 343 device_printf(fdc->fdc_dev, "%s", s); 344 else if (fdc->fdc_errs == FDC_ERRMAX) 345 device_printf(fdc->fdc_dev, "too many errors, not " 346 "logging any more\n"); 347 } 348 349 return FD_FAILED; 350 } 351 352 /* 353 * fd_cmd: Send a command to the chip. Takes a varargs with this structure: 354 * Unit number, 355 * # of output bytes, output bytes as ints ..., 356 * # of input bytes, input bytes as ints ... 357 */ 358 int 359 fd_cmd(struct fdc_data *fdc, int n_out, ...) 360 { 361 u_char cmd; 362 int n_in; 363 int n; 364 va_list ap; 365 366 va_start(ap, n_out); 367 cmd = (u_char)(va_arg(ap, int)); 368 va_end(ap); 369 va_start(ap, n_out); 370 for (n = 0; n < n_out; n++) 371 { 372 if (out_fdc(fdc, va_arg(ap, int)) < 0) 373 { 374 char msg[50]; 375 snprintf(msg, sizeof(msg), 376 "cmd %x failed at out byte %d of %d\n", 377 cmd, n + 1, n_out); 378 return fdc_err(fdc, msg); 379 } 380 } 381 n_in = va_arg(ap, int); 382 for (n = 0; n < n_in; n++) 383 { 384 int *ptr = va_arg(ap, int *); 385 if (fd_in(fdc, ptr) < 0) 386 { 387 char msg[50]; 388 snprintf(msg, sizeof(msg), 389 "cmd %02x failed at in byte %d of %d\n", 390 cmd, n + 1, n_in); 391 return fdc_err(fdc, msg); 392 } 393 } 394 395 return 0; 396 } 397 398 static int 399 enable_fifo(fdc_p fdc) 400 { 401 int i, j; 402 403 if ((fdc->flags & FDC_HAS_FIFO) == 0) { 404 405 /* 406 * Cannot use fd_cmd the normal way here, since 407 * this might be an invalid command. Thus we send the 408 * first byte, and check for an early turn of data directon. 409 */ 410 411 if (out_fdc(fdc, I8207X_CONFIGURE) < 0) 412 return fdc_err(fdc, "Enable FIFO failed\n"); 413 414 /* If command is invalid, return */ 415 j = FDSTS_TIMEOUT; 416 while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM)) 417 != NE7_RQM && j-- > 0) { 418 if (i == (NE7_DIO | NE7_RQM)) { 419 fdc_reset(fdc); 420 return FD_FAILED; 421 } 422 DELAY(1); 423 } 424 if (j<0 || 425 fd_cmd(fdc, 3, 426 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) { 427 fdc_reset(fdc); 428 return fdc_err(fdc, "Enable FIFO failed\n"); 429 } 430 fdc->flags |= FDC_HAS_FIFO; 431 return 0; 432 } 433 if (fd_cmd(fdc, 4, 434 I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) 435 return fdc_err(fdc, "Re-enable FIFO failed\n"); 436 return 0; 437 } 438 439 static int 440 fd_sense_drive_status(fdc_p fdc, int *st3p) 441 { 442 int st3; 443 444 if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3)) 445 { 446 return fdc_err(fdc, "Sense Drive Status failed\n"); 447 } 448 if (st3p) 449 *st3p = st3; 450 451 return 0; 452 } 453 454 static int 455 fd_sense_int(fdc_p fdc, int *st0p, int *cylp) 456 { 457 int cyl, st0, ret; 458 459 ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0); 460 if (ret) { 461 (void)fdc_err(fdc, 462 "sense intr err reading stat reg 0\n"); 463 return ret; 464 } 465 466 if (st0p) 467 *st0p = st0; 468 469 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) { 470 /* 471 * There doesn't seem to have been an interrupt. 472 */ 473 return FD_NOT_VALID; 474 } 475 476 if (fd_in(fdc, &cyl) < 0) { 477 return fdc_err(fdc, "can't get cyl num\n"); 478 } 479 480 if (cylp) 481 *cylp = cyl; 482 483 return 0; 484 } 485 486 487 static int 488 fd_read_status(fdc_p fdc) 489 { 490 int i, ret; 491 492 for (i = ret = 0; i < 7; i++) { 493 /* 494 * XXX types are poorly chosen. Only bytes can be read 495 * from the hardware, but fdc->status[] wants u_ints and 496 * fd_in() gives ints. 497 */ 498 int status; 499 500 ret = fd_in(fdc, &status); 501 fdc->status[i] = status; 502 if (ret != 0) 503 break; 504 } 505 506 if (ret == 0) 507 fdc->flags |= FDC_STAT_VALID; 508 else 509 fdc->flags &= ~FDC_STAT_VALID; 510 511 return ret; 512 } 513 514 void 515 fdc_release_resources(struct fdc_data *fdc) 516 { 517 device_t dev; 518 519 dev = fdc->fdc_dev; 520 if (fdc->res_irq != 0) { 521 bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq, 522 fdc->res_irq); 523 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq, 524 fdc->res_irq); 525 } 526 if (fdc->res_ctl != 0) { 527 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl, 528 fdc->res_ctl); 529 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl, 530 fdc->res_ctl); 531 } 532 if (fdc->res_ioport != 0) { 533 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport, 534 fdc->res_ioport); 535 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport, 536 fdc->res_ioport); 537 } 538 if (fdc->res_drq != 0) { 539 bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq, 540 fdc->res_drq); 541 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq, 542 fdc->res_drq); 543 } 544 } 545 546 /* 547 * Configuration/initialization stuff, per controller. 548 */ 549 550 int 551 fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 552 { 553 struct fdc_ivars *ivars = device_get_ivars(child); 554 555 switch (which) { 556 case FDC_IVAR_FDUNIT: 557 *result = ivars->fdunit; 558 break; 559 case FDC_IVAR_FDTYPE: 560 *result = ivars->fdtype; 561 break; 562 default: 563 return (ENOENT); 564 } 565 return (0); 566 } 567 568 int 569 fdc_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 570 { 571 struct fdc_ivars *ivars = device_get_ivars(child); 572 573 switch (which) { 574 case FDC_IVAR_FDUNIT: 575 ivars->fdunit = value; 576 break; 577 case FDC_IVAR_FDTYPE: 578 ivars->fdtype = value; 579 break; 580 default: 581 return (ENOENT); 582 } 583 return (0); 584 } 585 586 int 587 fdc_initial_reset(struct fdc_data *fdc) 588 { 589 /* First, reset the floppy controller. */ 590 fdout_wr(fdc, 0); 591 DELAY(100); 592 fdout_wr(fdc, FDO_FRST); 593 594 /* Then, see if it can handle a command. */ 595 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), 596 NE7_SPEC_2(2, 0), 0)) 597 return (ENXIO); 598 return (0); 599 } 600 601 int 602 fdc_detach(device_t dev) 603 { 604 struct fdc_data *fdc; 605 int error; 606 607 fdc = device_get_softc(dev); 608 609 /* have our children detached first */ 610 if ((error = bus_generic_detach(dev))) 611 return (error); 612 613 /* reset controller, turn motor off */ 614 fdout_wr(fdc, 0); 615 616 if ((fdc->flags & FDC_ATTACHED) == 0) { 617 device_printf(dev, "already unloaded\n"); 618 return (0); 619 } 620 fdc->flags &= ~FDC_ATTACHED; 621 622 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq, 623 fdc->fdc_intr); 624 fdc_release_resources(fdc); 625 return (0); 626 } 627 628 /* 629 * Add a child device to the fdc controller. It will then be probed etc. 630 */ 631 static void 632 fdc_add_child(device_t dev, const char *name, int unit) 633 { 634 int fdu, flags; 635 struct fdc_ivars *ivar; 636 device_t child; 637 638 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO); 639 if (ivar == NULL) 640 return; 641 child = device_add_child(dev, name, unit); 642 if (child == NULL) { 643 free(ivar, M_DEVBUF); 644 return; 645 } 646 device_set_ivars(child, ivar); 647 if (resource_int_value(name, unit, "drive", &fdu) != 0) 648 fdu = 0; 649 fdc_set_fdunit(child, fdu); 650 fdc_set_fdtype(child, FDT_NONE); 651 if (resource_int_value(name, unit, "flags", &flags) == 0) 652 device_set_flags(child, flags); 653 if (resource_disabled(name, unit)) 654 device_disable(child); 655 } 656 657 int 658 fdc_attach(device_t dev) 659 { 660 struct fdc_data *fdc; 661 const char *name, *dname; 662 int i, dunit, error; 663 664 fdc = device_get_softc(dev); 665 fdc->fdc_dev = dev; 666 error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq, 667 INTR_TYPE_BIO | INTR_ENTROPY, fdc_intr, fdc, 668 &fdc->fdc_intr); 669 if (error) { 670 device_printf(dev, "cannot setup interrupt\n"); 671 return error; 672 } 673 fdc->fdcu = device_get_unit(dev); 674 fdc->flags |= FDC_ATTACHED | FDC_NEEDS_RESET; 675 676 fdc->state = DEVIDLE; 677 678 /* reset controller, turn motor off, clear fdout mirror reg */ 679 fdout_wr(fdc, fdc->fdout = 0); 680 bioq_init(&fdc->head); 681 682 /* 683 * Probe and attach any children. We should probably detect 684 * devices from the BIOS unless overridden. 685 */ 686 name = device_get_nameunit(dev); 687 i = 0; 688 while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) 689 fdc_add_child(dev, dname, dunit); 690 691 if ((error = bus_generic_attach(dev)) != 0) 692 return (error); 693 694 return (0); 695 } 696 697 int 698 fdc_print_child(device_t me, device_t child) 699 { 700 int retval = 0, flags; 701 702 retval += bus_print_child_header(me, child); 703 retval += printf(" on %s drive %d", device_get_nameunit(me), 704 fdc_get_fdunit(child)); 705 if ((flags = device_get_flags(me)) != 0) 706 retval += printf(" flags %#x", flags); 707 retval += printf("\n"); 708 709 return (retval); 710 } 711 712 /* 713 * Configuration/initialization, per drive. 714 */ 715 static int 716 fd_probe(device_t dev) 717 { 718 int i; 719 u_int st0, st3; 720 struct fd_data *fd; 721 struct fdc_data *fdc; 722 fdsu_t fdsu; 723 int flags, type; 724 725 fdsu = fdc_get_fdunit(dev); 726 fd = device_get_softc(dev); 727 fdc = device_get_softc(device_get_parent(dev)); 728 flags = device_get_flags(dev); 729 730 fd->dev = dev; 731 fd->fdc = fdc; 732 fd->fdsu = fdsu; 733 fd->fdu = device_get_unit(dev); 734 735 type = FD_DTYPE(flags); 736 737 /* Auto-probe if fdinfo is present, but always allow override. */ 738 if (type == FDT_NONE && (type = fdc_get_fdtype(dev)) != FDT_NONE) { 739 fd->type = type; 740 goto done; 741 } else { 742 /* make sure fdautoselect() will be called */ 743 fd->flags = FD_UA; 744 fd->type = type; 745 } 746 747 /* 748 * XXX I think using __i386__ is wrong here since we actually want to probe 749 * for the machine type, not the CPU type (so non-PC arch's like the PC98 will 750 * fail the probe). However, for whatever reason, testing for _MACHINE_ARCH 751 * == i386 breaks the test on FreeBSD/Alpha. 752 */ 753 #if defined(__i386__) || defined(__amd64__) 754 if (fd->type == FDT_NONE && (fd->fdu == 0 || fd->fdu == 1)) { 755 /* Look up what the BIOS thinks we have. */ 756 if (fd->fdu == 0) { 757 if ((fdc->flags & FDC_ISPCMCIA)) 758 /* 759 * Somewhat special. No need to force the 760 * user to set device flags, since the Y-E 761 * Data PCMCIA floppy is always a 1.44 MB 762 * device. 763 */ 764 fd->type = FDT_144M; 765 else 766 fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4; 767 } else { 768 fd->type = rtcin(RTC_FDISKETTE) & 0x0f; 769 } 770 if (fd->type == FDT_288M_1) 771 fd->type = FDT_288M; 772 } 773 #endif /* __i386__ || __amd64__ */ 774 /* is there a unit? */ 775 if (fd->type == FDT_NONE) 776 return (ENXIO); 777 778 /* select it */ 779 set_motor(fdc, fdsu, TURNON); 780 fdc_reset(fdc); /* XXX reset, then unreset, etc. */ 781 DELAY(1000000); /* 1 sec */ 782 783 if ((flags & FD_NO_PROBE) == 0) { 784 /* If we're at track 0 first seek inwards. */ 785 if ((fd_sense_drive_status(fdc, &st3) == 0) && 786 (st3 & NE7_ST3_T0)) { 787 /* Seek some steps... */ 788 if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) { 789 /* ...wait a moment... */ 790 DELAY(300000); 791 /* make ctrlr happy: */ 792 fd_sense_int(fdc, 0, 0); 793 } 794 } 795 796 for (i = 0; i < 2; i++) { 797 /* 798 * we must recalibrate twice, just in case the 799 * heads have been beyond cylinder 76, since 800 * most FDCs still barf when attempting to 801 * recalibrate more than 77 steps 802 */ 803 /* go back to 0: */ 804 if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) { 805 /* a second being enough for full stroke seek*/ 806 DELAY(i == 0 ? 1000000 : 300000); 807 808 /* anything responding? */ 809 if (fd_sense_int(fdc, &st0, 0) == 0 && 810 (st0 & NE7_ST0_EC) == 0) 811 break; /* already probed succesfully */ 812 } 813 } 814 } 815 816 set_motor(fdc, fdsu, TURNOFF); 817 818 if ((flags & FD_NO_PROBE) == 0 && 819 (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */ 820 return (ENXIO); 821 822 done: 823 /* This doesn't work before the first reset. Or set_motor?? */ 824 if ((fdc->flags & FDC_HAS_FIFO) == 0 && 825 fdc->fdct == FDC_ENHANCED && 826 (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0 && 827 enable_fifo(fdc) == 0) { 828 device_printf(device_get_parent(dev), 829 "FIFO enabled, %d bytes threshold\n", fifo_threshold); 830 } 831 832 switch (fd->type) { 833 case FDT_12M: 834 device_set_desc(dev, "1200-KB 5.25\" drive"); 835 break; 836 case FDT_144M: 837 device_set_desc(dev, "1440-KB 3.5\" drive"); 838 break; 839 case FDT_288M: 840 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)"); 841 break; 842 case FDT_360K: 843 device_set_desc(dev, "360-KB 5.25\" drive"); 844 break; 845 case FDT_720K: 846 device_set_desc(dev, "720-KB 3.5\" drive"); 847 break; 848 default: 849 return (ENXIO); 850 } 851 fd->track = FD_NO_TRACK; 852 fd->fdc = fdc; 853 fd->fdsu = fdsu; 854 fd->options = 0; 855 callout_handle_init(&fd->toffhandle); 856 callout_handle_init(&fd->tohandle); 857 858 /* initialize densities for subdevices */ 859 for (i = 0; i < NUMDENS; i++) 860 memcpy(fd->fts + i, fd_native_types + fd->type, 861 sizeof(struct fd_type)); 862 return (0); 863 } 864 865 static int 866 fd_attach(device_t dev) 867 { 868 struct fd_data *fd; 869 870 fd = device_get_softc(dev); 871 fd->masterdev = make_dev(&fd_cdevsw, fd->fdu, 872 UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu); 873 fd->masterdev->si_drv1 = fd; 874 fd->device_stats = devstat_new_entry(device_get_name(dev), 875 device_get_unit(dev), 0, DEVSTAT_NO_ORDERED_TAGS, 876 DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER, 877 DEVSTAT_PRIORITY_FD); 878 return (0); 879 } 880 881 static int 882 fd_detach(device_t dev) 883 { 884 struct fd_data *fd; 885 886 fd = device_get_softc(dev); 887 untimeout(fd_turnoff, fd, fd->toffhandle); 888 devstat_remove_entry(fd->device_stats); 889 destroy_dev(fd->masterdev); 890 891 return (0); 892 } 893 894 static device_method_t fd_methods[] = { 895 /* Device interface */ 896 DEVMETHOD(device_probe, fd_probe), 897 DEVMETHOD(device_attach, fd_attach), 898 DEVMETHOD(device_detach, fd_detach), 899 DEVMETHOD(device_shutdown, bus_generic_shutdown), 900 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */ 901 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */ 902 903 { 0, 0 } 904 }; 905 906 static driver_t fd_driver = { 907 "fd", 908 fd_methods, 909 sizeof(struct fd_data) 910 }; 911 912 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0); 913 914 /* 915 * More auxiliary functions. 916 */ 917 /* 918 * Motor control stuff. 919 * Remember to not deselect the drive we're working on. 920 */ 921 static void 922 set_motor(struct fdc_data *fdc, int fdsu, int turnon) 923 { 924 int fdout; 925 926 fdout = fdc->fdout; 927 if (turnon) { 928 fdout &= ~FDO_FDSEL; 929 fdout |= (FDO_MOEN0 << fdsu) | FDO_FDMAEN | FDO_FRST | fdsu; 930 } else 931 fdout &= ~(FDO_MOEN0 << fdsu); 932 fdc->fdout = fdout; 933 fdout_wr(fdc, fdout); 934 TRACE1("[0x%x->FDOUT]", fdout); 935 } 936 937 static void 938 fd_turnoff(void *xfd) 939 { 940 int s; 941 fd_p fd = xfd; 942 943 TRACE1("[fd%d: turnoff]", fd->fdu); 944 945 s = splbio(); 946 /* 947 * Don't turn off the motor yet if the drive is active. 948 * 949 * If we got here, this could only mean we missed an interrupt. 950 * This can e. g. happen on the Y-E Date PCMCIA floppy controller 951 * after a controller reset. Just schedule a pseudo-interrupt 952 * so the state machine gets re-entered. 953 */ 954 if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) { 955 fdc_intr(fd->fdc); 956 splx(s); 957 return; 958 } 959 960 fd->flags &= ~FD_MOTOR; 961 set_motor(fd->fdc, fd->fdsu, TURNOFF); 962 splx(s); 963 } 964 965 static void 966 fd_motor_on(void *xfd) 967 { 968 int s; 969 fd_p fd = xfd; 970 971 s = splbio(); 972 fd->flags &= ~FD_MOTOR_WAIT; 973 if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT)) 974 { 975 fdc_intr(fd->fdc); 976 } 977 splx(s); 978 } 979 980 static void 981 fd_turnon(fd_p fd) 982 { 983 if(!(fd->flags & FD_MOTOR)) 984 { 985 fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT); 986 set_motor(fd->fdc, fd->fdsu, TURNON); 987 timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */ 988 } 989 } 990 991 static void 992 fdc_reset(fdc_p fdc) 993 { 994 /* Try a reset, keep motor on */ 995 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 996 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 997 DELAY(100); 998 /* enable FDC, but defer interrupts a moment */ 999 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN); 1000 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN); 1001 DELAY(100); 1002 fdout_wr(fdc, fdc->fdout); 1003 TRACE1("[0x%x->FDOUT]", fdc->fdout); 1004 1005 /* XXX after a reset, silently believe the FDC will accept commands */ 1006 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY, 1007 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 1008 0); 1009 if (fdc->flags & FDC_HAS_FIFO) 1010 (void) enable_fifo(fdc); 1011 } 1012 1013 /* 1014 * FDC IO functions, take care of the main status register, timeout 1015 * in case the desired status bits are never set. 1016 * 1017 * These PIO loops initially start out with short delays between 1018 * each iteration in the expectation that the required condition 1019 * is usually met quickly, so it can be handled immediately. After 1020 * about 1 ms, stepping is increased to achieve a better timing 1021 * accuracy in the calls to DELAY(). 1022 */ 1023 static int 1024 fd_in(struct fdc_data *fdc, int *ptr) 1025 { 1026 int i, j, step; 1027 1028 for (j = 0, step = 1; 1029 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM) && 1030 j < FDSTS_TIMEOUT; 1031 j += step) { 1032 if (i == NE7_RQM) 1033 return (fdc_err(fdc, "ready for output in input\n")); 1034 if (j == 1000) 1035 step = 1000; 1036 DELAY(step); 1037 } 1038 if (j >= FDSTS_TIMEOUT) 1039 return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0)); 1040 #ifdef FDC_DEBUG 1041 i = fddata_rd(fdc); 1042 TRACE1("[FDDATA->0x%x]", (unsigned char)i); 1043 *ptr = i; 1044 return (0); 1045 #else /* !FDC_DEBUG */ 1046 i = fddata_rd(fdc); 1047 if (ptr) 1048 *ptr = i; 1049 return (0); 1050 #endif /* FDC_DEBUG */ 1051 } 1052 1053 static int 1054 out_fdc(struct fdc_data *fdc, int x) 1055 { 1056 int i, j, step; 1057 1058 for (j = 0, step = 1; 1059 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != NE7_RQM && 1060 j < FDSTS_TIMEOUT; 1061 j += step) { 1062 if (i == (NE7_DIO|NE7_RQM)) 1063 return (fdc_err(fdc, "ready for input in output\n")); 1064 if (j == 1000) 1065 step = 1000; 1066 DELAY(step); 1067 } 1068 if (j >= FDSTS_TIMEOUT) 1069 return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0)); 1070 1071 /* Send the command and return */ 1072 fddata_wr(fdc, x); 1073 TRACE1("[0x%x->FDDATA]", x); 1074 return (0); 1075 } 1076 1077 /* 1078 * Block device driver interface functions (interspersed with even more 1079 * auxiliary functions). 1080 */ 1081 static int 1082 fdopen(struct cdev *dev, int flags, int mode, struct thread *td) 1083 { 1084 fd_p fd; 1085 fdc_p fdc; 1086 int rv, unitattn, dflags; 1087 1088 fd = dev->si_drv1; 1089 if (fd == NULL) 1090 return (ENXIO); 1091 fdc = fd->fdc; 1092 if ((fdc == NULL) || (fd->type == FDT_NONE)) 1093 return (ENXIO); 1094 dflags = device_get_flags(fd->dev); 1095 /* 1096 * This is a bit bogus. It's still possible that e. g. a 1097 * descriptor gets inherited to a child, but then it's at 1098 * least for the same subdevice. By checking FD_OPEN here, we 1099 * can ensure that a device isn't attempted to be opened with 1100 * different densities at the same time where the second open 1101 * could clobber the settings from the first one. 1102 */ 1103 if (fd->flags & FD_OPEN) 1104 return (EBUSY); 1105 1106 if (flags & FNONBLOCK) { 1107 /* 1108 * Unfortunately, physio(9) discards its ioflag 1109 * argument, thus preventing us from seeing the 1110 * IO_NDELAY bit. So we need to keep track 1111 * ourselves. 1112 */ 1113 fd->flags |= FD_NONBLOCK; 1114 fd->ft = 0; 1115 } else { 1116 /* 1117 * Figure out a unit attention condition. 1118 * 1119 * If UA has been forced, proceed. 1120 * 1121 * If the drive has no changeline support, 1122 * or if the drive parameters have been lost 1123 * due to previous non-blocking access, 1124 * assume a forced UA condition. 1125 * 1126 * If motor is off, turn it on for a moment 1127 * and select our drive, in order to read the 1128 * UA hardware signal. 1129 * 1130 * If motor is on, and our drive is currently 1131 * selected, just read the hardware bit. 1132 * 1133 * If motor is on, but active for another 1134 * drive on that controller, we are lost. We 1135 * cannot risk to deselect the other drive, so 1136 * we just assume a forced UA condition to be 1137 * on the safe side. 1138 */ 1139 unitattn = 0; 1140 if ((dflags & FD_NO_CHLINE) != 0 || 1141 (fd->flags & FD_UA) != 0 || 1142 fd->ft == 0) { 1143 unitattn = 1; 1144 fd->flags &= ~FD_UA; 1145 } else if (fdc->fdout & (FDO_MOEN0 | FDO_MOEN1 | 1146 FDO_MOEN2 | FDO_MOEN3)) { 1147 if ((fdc->fdout & FDO_FDSEL) == fd->fdsu) 1148 unitattn = fdin_rd(fdc) & FDI_DCHG; 1149 else 1150 unitattn = 1; 1151 } else { 1152 set_motor(fdc, fd->fdsu, TURNON); 1153 unitattn = fdin_rd(fdc) & FDI_DCHG; 1154 set_motor(fdc, fd->fdsu, TURNOFF); 1155 } 1156 if (unitattn && (rv = fdautoselect(dev)) != 0) 1157 return (rv); 1158 } 1159 fd->flags |= FD_OPEN; 1160 1161 if ((fdc->flags & FDC_NODMA) == 0) { 1162 if (fdc->dmacnt++ == 0) { 1163 isa_dma_acquire(fdc->dmachan); 1164 isa_dmainit(fdc->dmachan, MAX_SEC_SIZE); 1165 } 1166 } 1167 1168 /* 1169 * Clearing the DMA overrun counter at open time is a bit messy. 1170 * Since we're only managing one counter per controller, opening 1171 * the second drive could mess it up. Anyway, if the DMA overrun 1172 * condition is really persistent, it will eventually time out 1173 * still. OTOH, clearing it here will ensure we'll at least start 1174 * trying again after a previous (maybe even long ago) failure. 1175 * Also, this is merely a stop-gap measure only that should not 1176 * happen during normal operation, so we can tolerate it to be a 1177 * bit sloppy about this. 1178 */ 1179 fdc->dma_overruns = 0; 1180 1181 return 0; 1182 } 1183 1184 static int 1185 fdclose(struct cdev *dev, int flags, int mode, struct thread *td) 1186 { 1187 struct fd_data *fd; 1188 fdc_p fdc; 1189 1190 fd = dev->si_drv1; 1191 fdc = fd->fdc; 1192 fd->flags &= ~(FD_OPEN | FD_NONBLOCK); 1193 fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR); 1194 1195 if ((fdc->flags & FDC_NODMA) == 0) 1196 if (--fdc->dmacnt == 0) 1197 isa_dma_release(fdc->dmachan); 1198 1199 return (0); 1200 } 1201 1202 static void 1203 fdstrategy(struct bio *bp) 1204 { 1205 long blknum, nblocks; 1206 int s; 1207 fdu_t fdu; 1208 fdc_p fdc; 1209 fd_p fd; 1210 size_t fdblk; 1211 1212 fd = bp->bio_dev->si_drv1; 1213 fdu = fd->fdu; 1214 fdc = fd->fdc; 1215 bp->bio_resid = bp->bio_bcount; 1216 if (fd->type == FDT_NONE || fd->ft == 0) { 1217 if (fd->type != FDT_NONE && (fd->flags & FD_NONBLOCK)) 1218 bp->bio_error = EAGAIN; 1219 else 1220 bp->bio_error = ENXIO; 1221 bp->bio_flags |= BIO_ERROR; 1222 goto bad; 1223 } 1224 fdblk = 128 << (fd->ft->secsize); 1225 if (bp->bio_cmd != FDBIO_FORMAT && bp->bio_cmd != FDBIO_RDSECTID) { 1226 if (fd->flags & FD_NONBLOCK) { 1227 bp->bio_error = EAGAIN; 1228 bp->bio_flags |= BIO_ERROR; 1229 goto bad; 1230 } 1231 if (bp->bio_offset < 0) { 1232 printf( 1233 "fd%d: fdstrat: bad request offset = %ju, bcount = %ld\n", 1234 fdu, (intmax_t)bp->bio_offset, bp->bio_bcount); 1235 bp->bio_error = EINVAL; 1236 bp->bio_flags |= BIO_ERROR; 1237 goto bad; 1238 } 1239 if ((bp->bio_bcount % fdblk) != 0) { 1240 bp->bio_error = EINVAL; 1241 bp->bio_flags |= BIO_ERROR; 1242 goto bad; 1243 } 1244 } 1245 1246 /* 1247 * Set up block calculations. 1248 */ 1249 if (bp->bio_offset >= ((off_t)128 << fd->ft->secsize) * fd->ft->size) { 1250 bp->bio_error = EINVAL; 1251 bp->bio_flags |= BIO_ERROR; 1252 goto bad; 1253 } 1254 blknum = bp->bio_offset / fdblk; 1255 nblocks = fd->ft->size; 1256 if (blknum + bp->bio_bcount / fdblk > nblocks) { 1257 if (blknum >= nblocks) { 1258 if (bp->bio_cmd != BIO_READ) { 1259 bp->bio_error = ENOSPC; 1260 bp->bio_flags |= BIO_ERROR; 1261 } 1262 goto bad; /* not always bad, but EOF */ 1263 } 1264 bp->bio_bcount = (nblocks - blknum) * fdblk; 1265 } 1266 bp->bio_pblkno = blknum; 1267 s = splbio(); 1268 bioq_disksort(&fdc->head, bp); 1269 untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */ 1270 devstat_start_transaction_bio(fd->device_stats, bp); 1271 device_busy(fd->dev); 1272 fdstart(fdc); 1273 splx(s); 1274 return; 1275 1276 bad: 1277 biodone(bp); 1278 } 1279 1280 /* 1281 * fdstart 1282 * 1283 * We have just queued something. If the controller is not busy 1284 * then simulate the case where it has just finished a command 1285 * So that it (the interrupt routine) looks on the queue for more 1286 * work to do and picks up what we just added. 1287 * 1288 * If the controller is already busy, we need do nothing, as it 1289 * will pick up our work when the present work completes. 1290 */ 1291 static void 1292 fdstart(struct fdc_data *fdc) 1293 { 1294 int s; 1295 1296 s = splbio(); 1297 if(fdc->state == DEVIDLE) 1298 { 1299 fdc_intr(fdc); 1300 } 1301 splx(s); 1302 } 1303 1304 static void 1305 fd_iotimeout(void *xfdc) 1306 { 1307 fdc_p fdc; 1308 int s; 1309 1310 fdc = xfdc; 1311 TRACE1("fd%d[fd_iotimeout()]", fdc->fdu); 1312 1313 /* 1314 * Due to IBM's brain-dead design, the FDC has a faked ready 1315 * signal, hardwired to ready == true. Thus, any command 1316 * issued if there's no diskette in the drive will _never_ 1317 * complete, and must be aborted by resetting the FDC. 1318 * Many thanks, Big Blue! 1319 * The FDC must not be reset directly, since that would 1320 * interfere with the state machine. Instead, pretend that 1321 * the command completed but was invalid. The state machine 1322 * will reset the FDC and retry once. 1323 */ 1324 s = splbio(); 1325 fdc->status[0] = NE7_ST0_IC_IV; 1326 fdc->flags &= ~FDC_STAT_VALID; 1327 fdc->state = IOTIMEDOUT; 1328 fdc_intr(fdc); 1329 splx(s); 1330 } 1331 1332 /* Just ensure it has the right spl. */ 1333 static void 1334 fd_pseudointr(void *xfdc) 1335 { 1336 int s; 1337 1338 s = splbio(); 1339 fdc_intr(xfdc); 1340 splx(s); 1341 } 1342 1343 /* 1344 * fdc_intr 1345 * 1346 * Keep calling the state machine until it returns a 0. 1347 * Always called at splbio. 1348 */ 1349 static void 1350 fdc_intr(void *xfdc) 1351 { 1352 fdc_p fdc = xfdc; 1353 while(fdstate(fdc)) 1354 ; 1355 } 1356 1357 /* 1358 * Magic pseudo-DMA initialization for YE FDC. Sets count and 1359 * direction. 1360 */ 1361 #define SET_BCDR(fdc,wr,cnt,port) \ 1362 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port, \ 1363 ((cnt)-1) & 0xff); \ 1364 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \ 1365 ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f))); 1366 1367 /* 1368 * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy. 1369 */ 1370 static int 1371 fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count) 1372 { 1373 u_char *cptr = (u_char *)addr; 1374 1375 if (flags == BIO_READ) { 1376 if (fdc->state != PIOREAD) { 1377 fdc->state = PIOREAD; 1378 return(0); 1379 } 1380 SET_BCDR(fdc, 0, count, 0); 1381 bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off + 1382 FDC_YE_DATAPORT, cptr, count); 1383 } else { 1384 bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off + 1385 FDC_YE_DATAPORT, cptr, count); 1386 SET_BCDR(fdc, 0, count, 0); 1387 } 1388 return(1); 1389 } 1390 1391 /* 1392 * Try figuring out the density of the media present in our device. 1393 */ 1394 static int 1395 fdautoselect(struct cdev *dev) 1396 { 1397 fd_p fd; 1398 struct fd_type *fdtp; 1399 struct fdc_readid id; 1400 int i, n, oopts, rv; 1401 1402 fd = dev->si_drv1; 1403 1404 switch (fd->type) { 1405 default: 1406 return (ENXIO); 1407 1408 case FDT_360K: 1409 case FDT_720K: 1410 /* no autoselection on those drives */ 1411 fd->ft = fd_native_types + fd->type; 1412 return (0); 1413 1414 case FDT_12M: 1415 fdtp = fd_searchlist_12m; 1416 n = sizeof fd_searchlist_12m / sizeof(struct fd_type); 1417 break; 1418 1419 case FDT_144M: 1420 fdtp = fd_searchlist_144m; 1421 n = sizeof fd_searchlist_144m / sizeof(struct fd_type); 1422 break; 1423 1424 case FDT_288M: 1425 fdtp = fd_searchlist_288m; 1426 n = sizeof fd_searchlist_288m / sizeof(struct fd_type); 1427 break; 1428 } 1429 1430 /* 1431 * Try reading sector ID fields, first at cylinder 0, head 0, 1432 * then at cylinder 2, head N. We don't probe cylinder 1, 1433 * since for 5.25in DD media in a HD drive, there are no data 1434 * to read (2 step pulses per media cylinder required). For 1435 * two-sided media, the second probe always goes to head 1, so 1436 * we can tell them apart from single-sided media. As a 1437 * side-effect this means that single-sided media should be 1438 * mentioned in the search list after two-sided media of an 1439 * otherwise identical density. Media with a different number 1440 * of sectors per track but otherwise identical parameters 1441 * cannot be distinguished at all. 1442 * 1443 * If we successfully read an ID field on both cylinders where 1444 * the recorded values match our expectation, we are done. 1445 * Otherwise, we try the next density entry from the table. 1446 * 1447 * Stepping to cylinder 2 has the side-effect of clearing the 1448 * unit attention bit. 1449 */ 1450 oopts = fd->options; 1451 fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY; 1452 for (i = 0; i < n; i++, fdtp++) { 1453 fd->ft = fdtp; 1454 1455 id.cyl = id.head = 0; 1456 rv = fdmisccmd(dev, FDBIO_RDSECTID, &id); 1457 if (rv != 0) 1458 continue; 1459 if (id.cyl != 0 || id.head != 0 || 1460 id.secshift != fdtp->secsize) 1461 continue; 1462 id.cyl = 2; 1463 id.head = fd->ft->heads - 1; 1464 rv = fdmisccmd(dev, FDBIO_RDSECTID, &id); 1465 if (id.cyl != 2 || id.head != fdtp->heads - 1 || 1466 id.secshift != fdtp->secsize) 1467 continue; 1468 if (rv == 0) 1469 break; 1470 } 1471 1472 fd->options = oopts; 1473 if (i == n) { 1474 if (bootverbose) 1475 device_printf(fd->dev, "autoselection failed\n"); 1476 fd->ft = 0; 1477 return (EIO); 1478 } else { 1479 if (bootverbose) 1480 device_printf(fd->dev, "autoselected %d KB medium\n", 1481 fd->ft->size / 2); 1482 return (0); 1483 } 1484 } 1485 1486 1487 /* 1488 * The controller state machine. 1489 * 1490 * If it returns a non zero value, it should be called again immediately. 1491 */ 1492 static int 1493 fdstate(fdc_p fdc) 1494 { 1495 struct fdc_readid *idp; 1496 int read, format, rdsectid, cylinder, head, i, sec = 0, sectrac; 1497 int st0, cyl, st3, idf, ne7cmd, mfm, steptrac; 1498 unsigned long blknum; 1499 fdu_t fdu = fdc->fdu; 1500 fd_p fd; 1501 register struct bio *bp; 1502 struct fd_formb *finfo = NULL; 1503 size_t fdblk; 1504 1505 bp = fdc->bp; 1506 if (bp == NULL) { 1507 bp = bioq_first(&fdc->head); 1508 if (bp != NULL) { 1509 bioq_remove(&fdc->head, bp); 1510 fdc->bp = bp; 1511 } 1512 } 1513 if (bp == NULL) { 1514 /* 1515 * Nothing left for this controller to do, 1516 * force into the IDLE state. 1517 */ 1518 fdc->state = DEVIDLE; 1519 if (fdc->fd) { 1520 device_printf(fdc->fdc_dev, 1521 "unexpected valid fd pointer\n"); 1522 fdc->fd = (fd_p) 0; 1523 fdc->fdu = -1; 1524 } 1525 TRACE1("[fdc%d IDLE]", fdc->fdcu); 1526 return (0); 1527 } 1528 fd = bp->bio_dev->si_drv1; 1529 fdu = fd->fdu; 1530 fdblk = 128 << fd->ft->secsize; 1531 if (fdc->fd && (fd != fdc->fd)) 1532 device_printf(fd->dev, "confused fd pointers\n"); 1533 read = bp->bio_cmd == BIO_READ; 1534 mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0; 1535 steptrac = (fd->ft->flags & FL_2STEP)? 2: 1; 1536 if (read) 1537 idf = ISADMA_READ; 1538 else 1539 idf = ISADMA_WRITE; 1540 format = bp->bio_cmd == FDBIO_FORMAT; 1541 rdsectid = bp->bio_cmd == FDBIO_RDSECTID; 1542 if (format) 1543 finfo = (struct fd_formb *)bp->bio_data; 1544 TRACE1("fd%d", fdu); 1545 TRACE1("[%s]", fdstates[fdc->state]); 1546 TRACE1("(0x%x)", fd->flags); 1547 untimeout(fd_turnoff, fd, fd->toffhandle); 1548 fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz); 1549 switch (fdc->state) 1550 { 1551 case DEVIDLE: 1552 case FINDWORK: /* we have found new work */ 1553 fdc->retry = 0; 1554 fd->skip = 0; 1555 fdc->fd = fd; 1556 fdc->fdu = fdu; 1557 fdc->fdctl_wr(fdc, fd->ft->trans); 1558 TRACE1("[0x%x->FDCTL]", fd->ft->trans); 1559 /* 1560 * If the next drive has a motor startup pending, then 1561 * it will start up in its own good time. 1562 */ 1563 if(fd->flags & FD_MOTOR_WAIT) { 1564 fdc->state = MOTORWAIT; 1565 return (0); /* will return later */ 1566 } 1567 /* 1568 * Maybe if it's not starting, it SHOULD be starting. 1569 */ 1570 if (!(fd->flags & FD_MOTOR)) 1571 { 1572 fdc->state = MOTORWAIT; 1573 fd_turnon(fd); 1574 return (0); /* will return later */ 1575 } 1576 else /* at least make sure we are selected */ 1577 { 1578 set_motor(fdc, fd->fdsu, TURNON); 1579 } 1580 if (fdc->flags & FDC_NEEDS_RESET) { 1581 fdc->state = RESETCTLR; 1582 fdc->flags &= ~FDC_NEEDS_RESET; 1583 } else 1584 fdc->state = DOSEEK; 1585 return (1); /* will return immediately */ 1586 1587 case DOSEEK: 1588 blknum = bp->bio_pblkno + fd->skip / fdblk; 1589 cylinder = blknum / (fd->ft->sectrac * fd->ft->heads); 1590 if (cylinder == fd->track) 1591 { 1592 fdc->state = SEEKCOMPLETE; 1593 return (1); /* will return immediately */ 1594 } 1595 if (fd_cmd(fdc, 3, NE7CMD_SEEK, 1596 fd->fdsu, cylinder * steptrac, 0)) 1597 { 1598 /* 1599 * Seek command not accepted, looks like 1600 * the FDC went off to the Saints... 1601 */ 1602 fdc->retry = 6; /* try a reset */ 1603 return(retrier(fdc)); 1604 } 1605 fd->track = FD_NO_TRACK; 1606 fdc->state = SEEKWAIT; 1607 return(0); /* will return later */ 1608 1609 case SEEKWAIT: 1610 /* allow heads to settle */ 1611 timeout(fd_pseudointr, fdc, hz / 16); 1612 fdc->state = SEEKCOMPLETE; 1613 return(0); /* will return later */ 1614 1615 case SEEKCOMPLETE : /* seek done, start DMA */ 1616 blknum = bp->bio_pblkno + fd->skip / fdblk; 1617 cylinder = blknum / (fd->ft->sectrac * fd->ft->heads); 1618 1619 /* Make sure seek really happened. */ 1620 if(fd->track == FD_NO_TRACK) { 1621 int descyl = cylinder * steptrac; 1622 do { 1623 /* 1624 * This might be a "ready changed" interrupt, 1625 * which cannot really happen since the 1626 * RDY pin is hardwired to + 5 volts. This 1627 * generally indicates a "bouncing" intr 1628 * line, so do one of the following: 1629 * 1630 * When running on an enhanced FDC that is 1631 * known to not go stuck after responding 1632 * with INVALID, fetch all interrupt states 1633 * until seeing either an INVALID or a 1634 * real interrupt condition. 1635 * 1636 * When running on a dumb old NE765, give 1637 * up immediately. The controller will 1638 * provide up to four dummy RC interrupt 1639 * conditions right after reset (for the 1640 * corresponding four drives), so this is 1641 * our only chance to get notice that it 1642 * was not the FDC that caused the interrupt. 1643 */ 1644 if (fd_sense_int(fdc, &st0, &cyl) 1645 == FD_NOT_VALID) 1646 return (0); /* will return later */ 1647 if(fdc->fdct == FDC_NE765 1648 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 1649 return (0); /* hope for a real intr */ 1650 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 1651 1652 if (0 == descyl) { 1653 int failed = 0; 1654 /* 1655 * seek to cyl 0 requested; make sure we are 1656 * really there 1657 */ 1658 if (fd_sense_drive_status(fdc, &st3)) 1659 failed = 1; 1660 if ((st3 & NE7_ST3_T0) == 0) { 1661 printf( 1662 "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n", 1663 fdu, st3, NE7_ST3BITS); 1664 failed = 1; 1665 } 1666 1667 if (failed) { 1668 if(fdc->retry < 3) 1669 fdc->retry = 3; 1670 return (retrier(fdc)); 1671 } 1672 } 1673 1674 if (cyl != descyl) { 1675 printf( 1676 "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n", 1677 fdu, descyl, cyl, st0); 1678 if (fdc->retry < 3) 1679 fdc->retry = 3; 1680 return (retrier(fdc)); 1681 } 1682 } 1683 1684 fd->track = cylinder; 1685 if (format) 1686 fd->skip = (char *)&(finfo->fd_formb_cylno(0)) 1687 - (char *)finfo; 1688 if (!rdsectid && !(fdc->flags & FDC_NODMA)) 1689 isa_dmastart(idf, bp->bio_data+fd->skip, 1690 format ? bp->bio_bcount : fdblk, fdc->dmachan); 1691 blknum = bp->bio_pblkno + fd->skip / fdblk; 1692 sectrac = fd->ft->sectrac; 1693 sec = blknum % (sectrac * fd->ft->heads); 1694 head = sec / sectrac; 1695 sec = sec % sectrac + 1; 1696 if (head != 0 && fd->ft->offset_side2 != 0) 1697 sec += fd->ft->offset_side2; 1698 fd->hddrv = ((head&1)<<2)+fdu; 1699 1700 if(format || !(read || rdsectid)) 1701 { 1702 /* make sure the drive is writable */ 1703 if(fd_sense_drive_status(fdc, &st3) != 0) 1704 { 1705 /* stuck controller? */ 1706 if (!(fdc->flags & FDC_NODMA)) 1707 isa_dmadone(idf, 1708 bp->bio_data + fd->skip, 1709 format ? bp->bio_bcount : fdblk, 1710 fdc->dmachan); 1711 fdc->retry = 6; /* reset the beast */ 1712 return (retrier(fdc)); 1713 } 1714 if(st3 & NE7_ST3_WP) 1715 { 1716 /* 1717 * XXX YES! this is ugly. 1718 * in order to force the current operation 1719 * to fail, we will have to fake an FDC 1720 * error - all error handling is done 1721 * by the retrier() 1722 */ 1723 fdc->status[0] = NE7_ST0_IC_AT; 1724 fdc->status[1] = NE7_ST1_NW; 1725 fdc->status[2] = 0; 1726 fdc->status[3] = fd->track; 1727 fdc->status[4] = head; 1728 fdc->status[5] = sec; 1729 fdc->retry = 8; /* break out immediately */ 1730 fdc->state = IOTIMEDOUT; /* not really... */ 1731 return (1); /* will return immediately */ 1732 } 1733 } 1734 1735 if (format) { 1736 ne7cmd = NE7CMD_FORMAT | mfm; 1737 if (fdc->flags & FDC_NODMA) { 1738 /* 1739 * This seems to be necessary for 1740 * whatever obscure reason; if we omit 1741 * it, we end up filling the sector ID 1742 * fields of the newly formatted track 1743 * entirely with garbage, causing 1744 * `wrong cylinder' errors all over 1745 * the place when trying to read them 1746 * back. 1747 * 1748 * Umpf. 1749 */ 1750 SET_BCDR(fdc, 1, bp->bio_bcount, 0); 1751 1752 (void)fdcpio(fdc,bp->bio_cmd, 1753 bp->bio_data+fd->skip, 1754 bp->bio_bcount); 1755 1756 } 1757 /* formatting */ 1758 if(fd_cmd(fdc, 6, ne7cmd, head << 2 | fdu, 1759 finfo->fd_formb_secshift, 1760 finfo->fd_formb_nsecs, 1761 finfo->fd_formb_gaplen, 1762 finfo->fd_formb_fillbyte, 0)) { 1763 /* controller fell over */ 1764 if (!(fdc->flags & FDC_NODMA)) 1765 isa_dmadone(idf, 1766 bp->bio_data + fd->skip, 1767 format ? bp->bio_bcount : fdblk, 1768 fdc->dmachan); 1769 fdc->retry = 6; 1770 return (retrier(fdc)); 1771 } 1772 } else if (rdsectid) { 1773 ne7cmd = NE7CMD_READID | mfm; 1774 if (fd_cmd(fdc, 2, ne7cmd, head << 2 | fdu, 0)) { 1775 /* controller jamming */ 1776 fdc->retry = 6; 1777 return (retrier(fdc)); 1778 } 1779 } else { 1780 /* read or write operation */ 1781 ne7cmd = (read ? NE7CMD_READ | NE7CMD_SK : NE7CMD_WRITE) | mfm; 1782 if (fdc->flags & FDC_NODMA) { 1783 /* 1784 * This seems to be necessary even when 1785 * reading data. 1786 */ 1787 SET_BCDR(fdc, 1, fdblk, 0); 1788 1789 /* 1790 * Perform the write pseudo-DMA before 1791 * the WRITE command is sent. 1792 */ 1793 if (!read) 1794 (void)fdcpio(fdc,bp->bio_cmd, 1795 bp->bio_data+fd->skip, 1796 fdblk); 1797 } 1798 if (fd_cmd(fdc, 9, 1799 ne7cmd, 1800 head << 2 | fdu, /* head & unit */ 1801 fd->track, /* track */ 1802 head, 1803 sec, /* sector + 1 */ 1804 fd->ft->secsize, /* sector size */ 1805 sectrac, /* sectors/track */ 1806 fd->ft->gap, /* gap size */ 1807 fd->ft->datalen, /* data length */ 1808 0)) { 1809 /* the beast is sleeping again */ 1810 if (!(fdc->flags & FDC_NODMA)) 1811 isa_dmadone(idf, 1812 bp->bio_data + fd->skip, 1813 format ? bp->bio_bcount : fdblk, 1814 fdc->dmachan); 1815 fdc->retry = 6; 1816 return (retrier(fdc)); 1817 } 1818 } 1819 if (!rdsectid && (fdc->flags & FDC_NODMA)) 1820 /* 1821 * If this is a read, then simply await interrupt 1822 * before performing PIO. 1823 */ 1824 if (read && !fdcpio(fdc,bp->bio_cmd, 1825 bp->bio_data+fd->skip,fdblk)) { 1826 fd->tohandle = timeout(fd_iotimeout, fdc, hz); 1827 return(0); /* will return later */ 1828 } 1829 1830 /* 1831 * Write (or format) operation will fall through and 1832 * await completion interrupt. 1833 */ 1834 fdc->state = IOCOMPLETE; 1835 fd->tohandle = timeout(fd_iotimeout, fdc, hz); 1836 return (0); /* will return later */ 1837 1838 case PIOREAD: 1839 /* 1840 * Actually perform the PIO read. The IOCOMPLETE case 1841 * removes the timeout for us. 1842 */ 1843 (void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk); 1844 fdc->state = IOCOMPLETE; 1845 /* FALLTHROUGH */ 1846 case IOCOMPLETE: /* IO done, post-analyze */ 1847 untimeout(fd_iotimeout, fdc, fd->tohandle); 1848 1849 if (fd_read_status(fdc)) { 1850 if (!rdsectid && !(fdc->flags & FDC_NODMA)) 1851 isa_dmadone(idf, bp->bio_data + fd->skip, 1852 format ? bp->bio_bcount : fdblk, 1853 fdc->dmachan); 1854 if (fdc->retry < 6) 1855 fdc->retry = 6; /* force a reset */ 1856 return (retrier(fdc)); 1857 } 1858 1859 fdc->state = IOTIMEDOUT; 1860 1861 /* FALLTHROUGH */ 1862 case IOTIMEDOUT: 1863 if (!rdsectid && !(fdc->flags & FDC_NODMA)) 1864 isa_dmadone(idf, bp->bio_data + fd->skip, 1865 format ? bp->bio_bcount : fdblk, fdc->dmachan); 1866 if (fdc->status[0] & NE7_ST0_IC) { 1867 if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 1868 && fdc->status[1] & NE7_ST1_OR) { 1869 /* 1870 * DMA overrun. Someone hogged the bus and 1871 * didn't release it in time for the next 1872 * FDC transfer. 1873 * 1874 * We normally restart this without bumping 1875 * the retry counter. However, in case 1876 * something is seriously messed up (like 1877 * broken hardware), we rather limit the 1878 * number of retries so the IO operation 1879 * doesn't block indefinately. 1880 */ 1881 if (fdc->dma_overruns++ < FDC_DMAOV_MAX) { 1882 fdc->state = SEEKCOMPLETE; 1883 return (1);/* will return immediately */ 1884 } /* else fall through */ 1885 } 1886 if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV 1887 && fdc->retry < 6) 1888 fdc->retry = 6; /* force a reset */ 1889 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 1890 && fdc->status[2] & NE7_ST2_WC 1891 && fdc->retry < 3) 1892 fdc->retry = 3; /* force recalibrate */ 1893 return (retrier(fdc)); 1894 } 1895 /* All OK */ 1896 if (rdsectid) { 1897 /* copy out ID field contents */ 1898 idp = (struct fdc_readid *)bp->bio_data; 1899 idp->cyl = fdc->status[3]; 1900 idp->head = fdc->status[4]; 1901 idp->sec = fdc->status[5]; 1902 idp->secshift = fdc->status[6]; 1903 } 1904 /* Operation successful, retry DMA overruns again next time. */ 1905 fdc->dma_overruns = 0; 1906 fd->skip += fdblk; 1907 if (!rdsectid && !format && fd->skip < bp->bio_bcount) { 1908 /* set up next transfer */ 1909 fdc->state = DOSEEK; 1910 } else { 1911 /* ALL DONE */ 1912 fd->skip = 0; 1913 bp->bio_resid = 0; 1914 fdc->bp = NULL; 1915 device_unbusy(fd->dev); 1916 biofinish(bp, fd->device_stats, 0); 1917 fdc->fd = (fd_p) 0; 1918 fdc->fdu = -1; 1919 fdc->state = FINDWORK; 1920 } 1921 return (1); /* will return immediately */ 1922 1923 case RESETCTLR: 1924 fdc_reset(fdc); 1925 fdc->retry++; 1926 fdc->state = RESETCOMPLETE; 1927 return (0); /* will return later */ 1928 1929 case RESETCOMPLETE: 1930 /* 1931 * Discard all the results from the reset so that they 1932 * can't cause an unexpected interrupt later. 1933 */ 1934 for (i = 0; i < 4; i++) 1935 (void)fd_sense_int(fdc, &st0, &cyl); 1936 fdc->state = STARTRECAL; 1937 /* FALLTHROUGH */ 1938 case STARTRECAL: 1939 if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) { 1940 /* arrgl */ 1941 fdc->retry = 6; 1942 return (retrier(fdc)); 1943 } 1944 fdc->state = RECALWAIT; 1945 return (0); /* will return later */ 1946 1947 case RECALWAIT: 1948 /* allow heads to settle */ 1949 timeout(fd_pseudointr, fdc, hz / 8); 1950 fdc->state = RECALCOMPLETE; 1951 return (0); /* will return later */ 1952 1953 case RECALCOMPLETE: 1954 do { 1955 /* 1956 * See SEEKCOMPLETE for a comment on this: 1957 */ 1958 if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) 1959 return (0); /* will return later */ 1960 if(fdc->fdct == FDC_NE765 1961 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 1962 return (0); /* hope for a real intr */ 1963 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 1964 if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0) 1965 { 1966 if(fdc->retry > 3) 1967 /* 1968 * A recalibrate from beyond cylinder 77 1969 * will "fail" due to the FDC limitations; 1970 * since people used to complain much about 1971 * the failure message, try not logging 1972 * this one if it seems to be the first 1973 * time in a line. 1974 */ 1975 printf("fd%d: recal failed ST0 %b cyl %d\n", 1976 fdu, st0, NE7_ST0BITS, cyl); 1977 if(fdc->retry < 3) fdc->retry = 3; 1978 return (retrier(fdc)); 1979 } 1980 fd->track = 0; 1981 /* Seek (probably) necessary */ 1982 fdc->state = DOSEEK; 1983 return (1); /* will return immediately */ 1984 1985 case MOTORWAIT: 1986 if(fd->flags & FD_MOTOR_WAIT) 1987 { 1988 return (0); /* time's not up yet */ 1989 } 1990 if (fdc->flags & FDC_NEEDS_RESET) { 1991 fdc->state = RESETCTLR; 1992 fdc->flags &= ~FDC_NEEDS_RESET; 1993 } else 1994 fdc->state = DOSEEK; 1995 return (1); /* will return immediately */ 1996 1997 default: 1998 device_printf(fdc->fdc_dev, "unexpected FD int->"); 1999 if (fd_read_status(fdc) == 0) 2000 printf("FDC status :%x %x %x %x %x %x %x ", 2001 fdc->status[0], 2002 fdc->status[1], 2003 fdc->status[2], 2004 fdc->status[3], 2005 fdc->status[4], 2006 fdc->status[5], 2007 fdc->status[6] ); 2008 else 2009 printf("No status available "); 2010 if (fd_sense_int(fdc, &st0, &cyl) != 0) 2011 { 2012 printf("[controller is dead now]\n"); 2013 return (0); /* will return later */ 2014 } 2015 printf("ST0 = %x, PCN = %x\n", st0, cyl); 2016 return (0); /* will return later */ 2017 } 2018 /* noone should ever get here */ 2019 } 2020 2021 static int 2022 retrier(struct fdc_data *fdc) 2023 { 2024 struct bio *bp; 2025 struct fd_data *fd; 2026 int fdu; 2027 2028 bp = fdc->bp; 2029 2030 /* XXX shouldn't this be cached somewhere? */ 2031 fd = bp->bio_dev->si_drv1; 2032 fdu = fd->fdu; 2033 if (fd->options & FDOPT_NORETRY) 2034 goto fail; 2035 2036 switch (fdc->retry) { 2037 case 0: case 1: case 2: 2038 fdc->state = SEEKCOMPLETE; 2039 break; 2040 case 3: case 4: case 5: 2041 fdc->state = STARTRECAL; 2042 break; 2043 case 6: 2044 fdc->state = RESETCTLR; 2045 break; 2046 case 7: 2047 break; 2048 default: 2049 fail: 2050 if ((fd->options & FDOPT_NOERRLOG) == 0) { 2051 disk_err(bp, "hard error", 2052 fdc->fd->skip / DEV_BSIZE, 0); 2053 if (fdc->flags & FDC_STAT_VALID) { 2054 printf( 2055 " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n", 2056 fdc->status[0], NE7_ST0BITS, 2057 fdc->status[1], NE7_ST1BITS, 2058 fdc->status[2], NE7_ST2BITS, 2059 fdc->status[3], fdc->status[4], 2060 fdc->status[5]); 2061 } 2062 else 2063 printf(" (No status)\n"); 2064 } 2065 if ((fd->options & FDOPT_NOERROR) == 0) { 2066 bp->bio_flags |= BIO_ERROR; 2067 bp->bio_error = EIO; 2068 bp->bio_resid = bp->bio_bcount - fdc->fd->skip; 2069 } else 2070 bp->bio_resid = 0; 2071 fdc->bp = NULL; 2072 fdc->fd->skip = 0; 2073 device_unbusy(fd->dev); 2074 biofinish(bp, fdc->fd->device_stats, 0); 2075 fdc->state = FINDWORK; 2076 fdc->flags |= FDC_NEEDS_RESET; 2077 fdc->fd = (fd_p) 0; 2078 fdc->fdu = -1; 2079 return (1); 2080 } 2081 fdc->retry++; 2082 return (1); 2083 } 2084 2085 static void 2086 fdbiodone(struct bio *bp) 2087 { 2088 wakeup(bp); 2089 } 2090 2091 static int 2092 fdmisccmd(struct cdev *dev, u_int cmd, void *data) 2093 { 2094 fdu_t fdu; 2095 fd_p fd; 2096 struct bio *bp; 2097 struct fd_formb *finfo; 2098 struct fdc_readid *idfield; 2099 size_t fdblk; 2100 int error; 2101 2102 fd = dev->si_drv1; 2103 fdu = fd->fdu; 2104 fdblk = 128 << fd->ft->secsize; 2105 finfo = (struct fd_formb *)data; 2106 idfield = (struct fdc_readid *)data; 2107 2108 bp = malloc(sizeof(struct bio), M_TEMP, M_WAITOK | M_ZERO); 2109 2110 /* 2111 * Set up a bio request for fdstrategy(). bio_offset is faked 2112 * so that fdstrategy() will seek to the the requested 2113 * cylinder, and use the desired head. 2114 */ 2115 bp->bio_cmd = cmd; 2116 if (cmd == FDBIO_FORMAT) { 2117 bp->bio_offset = 2118 (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) + 2119 finfo->head * fd->ft->sectrac) * fdblk; 2120 bp->bio_bcount = sizeof(struct fd_idfield_data) * 2121 finfo->fd_formb_nsecs; 2122 } else if (cmd == FDBIO_RDSECTID) { 2123 bp->bio_offset = 2124 (idfield->cyl * (fd->ft->sectrac * fd->ft->heads) + 2125 idfield->head * fd->ft->sectrac) * fdblk; 2126 bp->bio_bcount = sizeof(struct fdc_readid); 2127 } else 2128 panic("wrong cmd in fdmisccmd()"); 2129 bp->bio_data = data; 2130 bp->bio_dev = dev; 2131 bp->bio_done = fdbiodone; 2132 bp->bio_flags = 0; 2133 2134 /* Now run the command. */ 2135 fdstrategy(bp); 2136 error = biowait(bp, "fdcmd"); 2137 2138 free(bp, M_TEMP); 2139 return (error); 2140 } 2141 2142 static int 2143 fdioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 2144 { 2145 fdu_t fdu; 2146 fd_p fd; 2147 struct fdc_status *fsp; 2148 struct fdc_readid *rid; 2149 int error; 2150 2151 fd = dev->si_drv1; 2152 fdu = fd->fdu; 2153 2154 /* 2155 * First, handle everything that could be done with 2156 * FD_NONBLOCK still being set. 2157 */ 2158 switch (cmd) { 2159 2160 case DIOCGMEDIASIZE: 2161 if (fd->ft == 0) 2162 return ((fd->flags & FD_NONBLOCK) ? EAGAIN : ENXIO); 2163 *(off_t *)addr = (128 << (fd->ft->secsize)) * fd->ft->size; 2164 return (0); 2165 2166 case DIOCGSECTORSIZE: 2167 if (fd->ft == 0) 2168 return ((fd->flags & FD_NONBLOCK) ? EAGAIN : ENXIO); 2169 *(u_int *)addr = 128 << (fd->ft->secsize); 2170 return (0); 2171 2172 case FIONBIO: 2173 if (*(int *)addr != 0) 2174 fd->flags |= FD_NONBLOCK; 2175 else { 2176 if (fd->ft == 0) { 2177 /* 2178 * No drive type has been selected yet, 2179 * cannot turn FNONBLOCK off. 2180 */ 2181 return (EINVAL); 2182 } 2183 fd->flags &= ~FD_NONBLOCK; 2184 } 2185 return (0); 2186 2187 case FIOASYNC: 2188 /* keep the generic fcntl() code happy */ 2189 return (0); 2190 2191 case FD_GTYPE: /* get drive type */ 2192 if (fd->ft == 0) 2193 /* no type known yet, return the native type */ 2194 *(struct fd_type *)addr = fd_native_types[fd->type]; 2195 else 2196 *(struct fd_type *)addr = *fd->ft; 2197 return (0); 2198 2199 case FD_STYPE: /* set drive type */ 2200 /* 2201 * Allow setting drive type temporarily iff 2202 * currently unset. Used for fdformat so any 2203 * user can set it, and then start formatting. 2204 */ 2205 if (fd->ft) 2206 return (EINVAL); /* already set */ 2207 fd->fts[0] = *(struct fd_type *)addr; 2208 fd->ft = &fd->fts[0]; 2209 fd->flags |= FD_UA; 2210 return (0); 2211 2212 case FD_GOPTS: /* get drive options */ 2213 *(int *)addr = fd->options + FDOPT_AUTOSEL; 2214 return (0); 2215 2216 case FD_SOPTS: /* set drive options */ 2217 fd->options = *(int *)addr & ~FDOPT_AUTOSEL; 2218 return (0); 2219 2220 #ifdef FDC_DEBUG 2221 case FD_DEBUG: 2222 if ((fd_debug != 0) != (*(int *)addr != 0)) { 2223 fd_debug = (*(int *)addr != 0); 2224 printf("fd%d: debugging turned %s\n", 2225 fd->fdu, fd_debug ? "on" : "off"); 2226 } 2227 return (0); 2228 #endif 2229 2230 case FD_CLRERR: 2231 if (suser(td) != 0) 2232 return (EPERM); 2233 fd->fdc->fdc_errs = 0; 2234 return (0); 2235 2236 case FD_GSTAT: 2237 fsp = (struct fdc_status *)addr; 2238 if ((fd->fdc->flags & FDC_STAT_VALID) == 0) 2239 return (EINVAL); 2240 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int)); 2241 return (0); 2242 2243 case FD_GDTYPE: 2244 *(enum fd_drivetype *)addr = fd->type; 2245 return (0); 2246 } 2247 2248 /* 2249 * Now handle everything else. Make sure we have a valid 2250 * drive type. 2251 */ 2252 if (fd->flags & FD_NONBLOCK) 2253 return (EAGAIN); 2254 if (fd->ft == 0) 2255 return (ENXIO); 2256 error = 0; 2257 2258 switch (cmd) { 2259 2260 case FD_FORM: 2261 if ((flag & FWRITE) == 0) 2262 return (EBADF); /* must be opened for writing */ 2263 if (((struct fd_formb *)addr)->format_version != 2264 FD_FORMAT_VERSION) 2265 return (EINVAL); /* wrong version of formatting prog */ 2266 error = fdmisccmd(dev, FDBIO_FORMAT, addr); 2267 break; 2268 2269 case FD_GTYPE: /* get drive type */ 2270 *(struct fd_type *)addr = *fd->ft; 2271 break; 2272 2273 case FD_STYPE: /* set drive type */ 2274 /* this is considered harmful; only allow for superuser */ 2275 if (suser(td) != 0) 2276 return (EPERM); 2277 *fd->ft = *(struct fd_type *)addr; 2278 break; 2279 2280 case FD_GOPTS: /* get drive options */ 2281 *(int *)addr = fd->options; 2282 break; 2283 2284 case FD_SOPTS: /* set drive options */ 2285 fd->options = *(int *)addr; 2286 break; 2287 2288 #ifdef FDC_DEBUG 2289 case FD_DEBUG: 2290 if ((fd_debug != 0) != (*(int *)addr != 0)) { 2291 fd_debug = (*(int *)addr != 0); 2292 printf("fd%d: debugging turned %s\n", 2293 fd->fdu, fd_debug ? "on" : "off"); 2294 } 2295 break; 2296 #endif 2297 2298 case FD_CLRERR: 2299 if (suser(td) != 0) 2300 return (EPERM); 2301 fd->fdc->fdc_errs = 0; 2302 break; 2303 2304 case FD_GSTAT: 2305 fsp = (struct fdc_status *)addr; 2306 if ((fd->fdc->flags & FDC_STAT_VALID) == 0) 2307 return (EINVAL); 2308 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int)); 2309 break; 2310 2311 case FD_READID: 2312 rid = (struct fdc_readid *)addr; 2313 if (rid->cyl > MAX_CYLINDER || rid->head > MAX_HEAD) 2314 return (EINVAL); 2315 error = fdmisccmd(dev, FDBIO_RDSECTID, addr); 2316 break; 2317 2318 default: 2319 error = ENOTTY; 2320 break; 2321 } 2322 return (error); 2323 } 2324