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