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