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