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