1 /*- 2 * Copyright (c) 2004 Poul-Henning Kamp 3 * Copyright (c) 1990 The Regents of the University of California. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * Don Ahn. 8 * 9 * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu) 10 * aided by the Linux floppy driver modifications from David Bateman 11 * (dbateman@eng.uts.edu.au). 12 * 13 * Copyright (c) 1993, 1994 by 14 * jc@irbs.UUCP (John Capo) 15 * vak@zebub.msk.su (Serge Vakulenko) 16 * ache@astral.msk.su (Andrew A. Chernov) 17 * 18 * Copyright (c) 1993, 1994, 1995 by 19 * joerg_wunsch@uriah.sax.de (Joerg Wunsch) 20 * dufault@hda.com (Peter Dufault) 21 * 22 * Copyright (c) 2001 Joerg Wunsch, 23 * joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch) 24 * 25 * Redistribution and use in source and binary forms, with or without 26 * modification, are permitted provided that the following conditions 27 * are met: 28 * 1. Redistributions of source code must retain the above copyright 29 * notice, this list of conditions and the following disclaimer. 30 * 2. Redistributions in binary form must reproduce the above copyright 31 * notice, this list of conditions and the following disclaimer in the 32 * documentation and/or other materials provided with the distribution. 33 * 4. Neither the name of the University nor the names of its contributors 34 * may be used to endorse or promote products derived from this software 35 * without specific prior written permission. 36 * 37 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 40 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 47 * SUCH DAMAGE. 48 * 49 * from: @(#)fd.c 7.4 (Berkeley) 5/25/91 50 * 51 */ 52 53 #include <sys/cdefs.h> 54 __FBSDID("$FreeBSD$"); 55 56 #include "opt_fdc.h" 57 58 #include <sys/param.h> 59 #include <sys/bio.h> 60 #include <sys/bus.h> 61 #include <sys/devicestat.h> 62 #include <sys/disk.h> 63 #include <sys/fcntl.h> 64 #include <sys/fdcio.h> 65 #include <sys/filio.h> 66 #include <sys/kernel.h> 67 #include <sys/kthread.h> 68 #include <sys/lock.h> 69 #include <sys/malloc.h> 70 #include <sys/module.h> 71 #include <sys/mutex.h> 72 #include <sys/proc.h> 73 #include <sys/rman.h> 74 #include <sys/sysctl.h> 75 #include <sys/systm.h> 76 77 #include <geom/geom.h> 78 79 #include <machine/bus.h> 80 #include <machine/clock.h> 81 #include <machine/stdarg.h> 82 83 #include <isa/isavar.h> 84 #include <isa/isareg.h> 85 #include <dev/fdc/fdcvar.h> 86 #include <isa/rtc.h> 87 88 #include <dev/ic/nec765.h> 89 90 /* 91 * Runtime configuration hints/flags 92 */ 93 94 /* configuration flags for fd */ 95 #define FD_TYPEMASK 0x0f /* drive type, matches enum 96 * fd_drivetype; on i386 machines, if 97 * given as 0, use RTC type for fd0 98 * and fd1 */ 99 #define FD_NO_CHLINE 0x10 /* drive does not support changeline 100 * aka. unit attention */ 101 #define FD_NO_PROBE 0x20 /* don't probe drive (seek test), just 102 * assume it is there */ 103 104 /* 105 * Things that could conceiveably considered parameters or tweakables 106 */ 107 108 /* 109 * Maximal number of bytes in a cylinder. 110 * This is used for ISADMA bouncebuffer allocation and sets the max 111 * xfersize we support. 112 * 113 * 2.88M format has 2 x 36 x 512. 114 */ 115 116 #define MAX_BYTES_PER_CYL (2 * 40 * 512) 117 118 /* 119 * Timeout value for the PIO loops to wait until the FDC main status 120 * register matches our expectations (request for master, direction 121 * bit). This is supposed to be a number of microseconds, although 122 * timing might actually not be very accurate. 123 * 124 * Timeouts of 100 msec are believed to be required for some broken 125 * (old) hardware. 126 */ 127 #define FDSTS_TIMEOUT 100000 128 129 /* 130 * After this many errors, stop whining. Close will reset this count. 131 */ 132 #define FDC_ERRMAX 100 /* do not log more */ 133 134 /* 135 * AutoDensity search lists for each drive type. 136 */ 137 138 static struct fd_type fd_searchlist_360k[] = { 139 { FDF_5_360 }, 140 { 0 } 141 }; 142 143 static struct fd_type fd_searchlist_12m[] = { 144 { FDF_5_1200 | FL_AUTO }, 145 { FDF_5_360 | FL_2STEP | FL_AUTO}, 146 { 0 } 147 }; 148 149 static struct fd_type fd_searchlist_720k[] = { 150 { FDF_3_720 }, 151 { 0 } 152 }; 153 154 static struct fd_type fd_searchlist_144m[] = { 155 { FDF_3_1440 | FL_AUTO}, 156 { FDF_3_720 | FL_AUTO}, 157 { 0 } 158 }; 159 160 static struct fd_type fd_searchlist_288m[] = { 161 { FDF_3_1440 | FL_AUTO }, 162 #if 0 163 { FDF_3_2880 | FL_AUTO }, /* XXX: probably doesn't work */ 164 #endif 165 { FDF_3_720 | FL_AUTO}, 166 { 0 } 167 }; 168 169 /* 170 * Order must match enum fd_drivetype in <sys/fdcio.h>. 171 */ 172 static struct fd_type *fd_native_types[] = { 173 NULL, /* FDT_NONE */ 174 fd_searchlist_360k, /* FDT_360K */ 175 fd_searchlist_12m, /* FDT_12M */ 176 fd_searchlist_720k, /* FDT_720K */ 177 fd_searchlist_144m, /* FDT_144M */ 178 fd_searchlist_288m, /* FDT_288M */ 179 }; 180 181 /* 182 * Internals start here 183 */ 184 185 /* registers */ 186 #define FDOUT 2 /* Digital Output Register (W) */ 187 #define FDO_FDSEL 0x03 /* floppy device select */ 188 #define FDO_FRST 0x04 /* floppy controller reset */ 189 #define FDO_FDMAEN 0x08 /* enable floppy DMA and Interrupt */ 190 #define FDO_MOEN0 0x10 /* motor enable drive 0 */ 191 #define FDO_MOEN1 0x20 /* motor enable drive 1 */ 192 #define FDO_MOEN2 0x40 /* motor enable drive 2 */ 193 #define FDO_MOEN3 0x80 /* motor enable drive 3 */ 194 195 #define FDSTS 4 /* NEC 765 Main Status Register (R) */ 196 #define FDDATA 5 /* NEC 765 Data Register (R/W) */ 197 #define FDCTL 7 /* Control Register (W) */ 198 199 /* 200 * The YE-DATA PC Card floppies use PIO to read in the data rather than 201 * DMA due to the wild variability of DMA for the PC Card devices. In 202 * addition, if we cannot setup the DMA resources for the ISA attachment, 203 * we'll use this same offset for data transfer. 204 * 205 * For this mode, offset 0 and 1 must be used to setup the transfer 206 * for this floppy. This means they are only available on those systems 207 * that map them to the floppy drive. Newer systems do not do this, and 208 * we should likely prohibit access to them (or disallow NODMA to be set). 209 */ 210 #define FDBCDR 0 /* And 1 */ 211 #define FD_YE_DATAPORT 6 /* Drive Data port */ 212 213 #define FDI_DCHG 0x80 /* diskette has been changed */ 214 /* requires drive and motor being selected */ 215 /* is cleared by any step pulse to drive */ 216 217 /* 218 * We have two private BIO commands for formatting and sector-id reading 219 */ 220 #define BIO_PROBE BIO_CMD0 221 #define BIO_RDID BIO_CMD1 222 #define BIO_FMT BIO_CMD2 223 224 /* 225 * Per drive structure (softc). 226 */ 227 struct fd_data { 228 u_char *fd_ioptr; /* IO pointer */ 229 u_int fd_iosize; /* Size of IO chunks */ 230 u_int fd_iocount; /* Outstanding requests */ 231 struct fdc_data *fdc; /* pointer to controller structure */ 232 int fdsu; /* this units number on this controller */ 233 enum fd_drivetype type; /* drive type */ 234 struct fd_type *ft; /* pointer to current type descriptor */ 235 struct fd_type fts; /* type descriptors */ 236 int sectorsize; 237 int flags; 238 #define FD_WP (1<<0) /* Write protected */ 239 #define FD_MOTOR (1<<1) /* motor should be on */ 240 #define FD_MOTORWAIT (1<<2) /* motor should be on */ 241 #define FD_EMPTY (1<<3) /* no media */ 242 #define FD_NEWDISK (1<<4) /* media changed */ 243 #define FD_ISADMA (1<<5) /* isa dma started */ 244 int track; /* where we think the head is */ 245 #define FD_NO_TRACK -2 246 int options; /* FDOPT_* */ 247 struct callout toffhandle; 248 struct callout tohandle; 249 struct g_geom *fd_geom; 250 struct g_provider *fd_provider; 251 device_t dev; 252 struct bio_queue_head fd_bq; 253 }; 254 255 #define FD_NOT_VALID -2 256 257 static driver_intr_t fdc_intr; 258 static void fdc_reset(struct fdc_data *); 259 260 SYSCTL_NODE(_debug, OID_AUTO, fdc, CTLFLAG_RW, 0, "fdc driver"); 261 262 static int fifo_threshold = 8; 263 SYSCTL_INT(_debug_fdc, OID_AUTO, fifo, CTLFLAG_RW, &fifo_threshold, 0, 264 "FIFO threshold setting"); 265 266 static int debugflags = 0; 267 SYSCTL_INT(_debug_fdc, OID_AUTO, debugflags, CTLFLAG_RW, &debugflags, 0, 268 "Debug flags"); 269 270 static int retries = 10; 271 SYSCTL_INT(_debug_fdc, OID_AUTO, retries, CTLFLAG_RW, &retries, 0, 272 "Number of retries to attempt"); 273 274 static int spec1 = 0xaf; 275 SYSCTL_INT(_debug_fdc, OID_AUTO, spec1, CTLFLAG_RW, &spec1, 0, 276 "Specification byte one (step-rate + head unload)"); 277 278 static int spec2 = 0x10; 279 SYSCTL_INT(_debug_fdc, OID_AUTO, spec2, CTLFLAG_RW, &spec2, 0, 280 "Specification byte two (head load time + no-dma)"); 281 282 static int settle; 283 SYSCTL_INT(_debug_fdc, OID_AUTO, settle, CTLFLAG_RW, &settle, 0, 284 "Head settling time in sec/hz"); 285 286 static void 287 fdprinttype(struct fd_type *ft) 288 { 289 290 printf("(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,0x%x)", 291 ft->sectrac, ft->secsize, ft->datalen, ft->gap, ft->tracks, 292 ft->size, ft->trans, ft->heads, ft->f_gap, ft->f_inter, 293 ft->offset_side2, ft->flags); 294 } 295 296 static void 297 fdsettype(struct fd_data *fd, struct fd_type *ft) 298 { 299 fd->ft = ft; 300 ft->size = ft->sectrac * ft->heads * ft->tracks; 301 fd->sectorsize = 128 << fd->ft->secsize; 302 } 303 304 /* 305 * Bus space handling (access to low-level IO). 306 */ 307 static void 308 fdctl_wr(struct fdc_data *fdc, u_int8_t v) 309 { 310 311 bus_space_write_1(fdc->ctlt, fdc->ctlh, fdc->ctl_off, v); 312 } 313 314 static void 315 fdout_wr(struct fdc_data *fdc, u_int8_t v) 316 { 317 318 bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v); 319 } 320 321 static u_int8_t 322 fdsts_rd(struct fdc_data *fdc) 323 { 324 325 return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off); 326 } 327 328 static void 329 fddata_wr(struct fdc_data *fdc, u_int8_t v) 330 { 331 332 bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v); 333 } 334 335 static u_int8_t 336 fddata_rd(struct fdc_data *fdc) 337 { 338 339 return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off); 340 } 341 342 static u_int8_t 343 fdin_rd(struct fdc_data *fdc) 344 { 345 346 return bus_space_read_1(fdc->ctlt, fdc->ctlh, fdc->ctl_off); 347 } 348 349 /* 350 * Magic pseudo-DMA initialization for YE FDC. Sets count and 351 * direction. 352 */ 353 static void 354 fdbcdr_wr(struct fdc_data *fdc, int iswrite, uint16_t count) 355 { 356 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + FDBCDR, 357 (count - 1) & 0xff); 358 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + FDBCDR + 1, 359 ((iswrite ? 0x80 : 0) | (((count - 1) >> 8) & 0x7f))); 360 } 361 362 static int 363 fdc_err(struct fdc_data *fdc, const char *s) 364 { 365 fdc->fdc_errs++; 366 if (s) { 367 if (fdc->fdc_errs < FDC_ERRMAX) 368 device_printf(fdc->fdc_dev, "%s", s); 369 else if (fdc->fdc_errs == FDC_ERRMAX) 370 device_printf(fdc->fdc_dev, "too many errors, not " 371 "logging any more\n"); 372 } 373 374 return (1); 375 } 376 377 /* 378 * FDC IO functions, take care of the main status register, timeout 379 * in case the desired status bits are never set. 380 * 381 * These PIO loops initially start out with short delays between 382 * each iteration in the expectation that the required condition 383 * is usually met quickly, so it can be handled immediately. 384 */ 385 static int 386 fdc_in(struct fdc_data *fdc, int *ptr) 387 { 388 int i, j, step; 389 390 step = 1; 391 for (j = 0; j < FDSTS_TIMEOUT; j += step) { 392 i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM); 393 if (i == (NE7_DIO|NE7_RQM)) { 394 i = fddata_rd(fdc); 395 if (ptr) 396 *ptr = i; 397 return (0); 398 } 399 if (i == NE7_RQM) 400 return (fdc_err(fdc, "ready for output in input\n")); 401 step += step; 402 DELAY(step); 403 } 404 return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0)); 405 } 406 407 static int 408 fdc_out(struct fdc_data *fdc, int x) 409 { 410 int i, j, step; 411 412 step = 1; 413 for (j = 0; j < FDSTS_TIMEOUT; j += step) { 414 i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM); 415 if (i == NE7_RQM) { 416 fddata_wr(fdc, x); 417 return (0); 418 } 419 if (i == (NE7_DIO|NE7_RQM)) 420 return (fdc_err(fdc, "ready for input in output\n")); 421 step += step; 422 DELAY(step); 423 } 424 return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0)); 425 } 426 427 /* 428 * fdc_cmd: Send a command to the chip. 429 * Takes a varargs with this structure: 430 * # of output bytes 431 * output bytes as int [...] 432 * # of input bytes 433 * input bytes as int* [...] 434 */ 435 static int 436 fdc_cmd(struct fdc_data *fdc, int n_out, ...) 437 { 438 u_char cmd = 0; 439 int n_in; 440 int n, i; 441 va_list ap; 442 443 va_start(ap, n_out); 444 for (n = 0; n < n_out; n++) { 445 i = va_arg(ap, int); 446 if (n == 0) 447 cmd = i; 448 if (fdc_out(fdc, i) < 0) { 449 char msg[50]; 450 snprintf(msg, sizeof(msg), 451 "cmd %x failed at out byte %d of %d\n", 452 cmd, n + 1, n_out); 453 fdc->flags |= FDC_NEEDS_RESET; 454 return fdc_err(fdc, msg); 455 } 456 } 457 n_in = va_arg(ap, int); 458 for (n = 0; n < n_in; n++) { 459 int *ptr = va_arg(ap, int *); 460 if (fdc_in(fdc, ptr) < 0) { 461 char msg[50]; 462 snprintf(msg, sizeof(msg), 463 "cmd %02x failed at in byte %d of %d\n", 464 cmd, n + 1, n_in); 465 fdc->flags |= FDC_NEEDS_RESET; 466 return fdc_err(fdc, msg); 467 } 468 } 469 return (0); 470 } 471 472 static void 473 fdc_reset(struct fdc_data *fdc) 474 { 475 int i, r[10]; 476 477 /* Try a reset, keep motor on */ 478 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 479 DELAY(100); 480 /* enable FDC, but defer interrupts a moment */ 481 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN); 482 DELAY(100); 483 fdout_wr(fdc, fdc->fdout); 484 485 /* XXX after a reset, silently believe the FDC will accept commands */ 486 if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, spec1, spec2, 0)) 487 device_printf(fdc->fdc_dev, " SPECIFY failed in reset\n"); 488 489 if (fdc->fdct == FDC_ENHANCED) { 490 if (fdc_cmd(fdc, 4, 491 I8207X_CONFIGURE, 492 0, 493 0x40 | /* Enable Implied Seek */ 494 0x10 | /* Polling disabled */ 495 (fifo_threshold - 1), /* Fifo threshold */ 496 0x00, /* Precomp track */ 497 0)) 498 device_printf(fdc->fdc_dev, 499 " CONFIGURE failed in reset\n"); 500 if (debugflags & 1) { 501 if (fdc_cmd(fdc, 1, 502 0x0e, /* DUMPREG */ 503 10, &r[0], &r[1], &r[2], &r[3], &r[4], 504 &r[5], &r[6], &r[7], &r[8], &r[9])) 505 device_printf(fdc->fdc_dev, 506 " DUMPREG failed in reset\n"); 507 for (i = 0; i < 10; i++) 508 printf(" %02x", r[i]); 509 printf("\n"); 510 } 511 } 512 } 513 514 static int 515 fdc_sense_drive(struct fdc_data *fdc, int *st3p) 516 { 517 int st3; 518 519 if (fdc_cmd(fdc, 2, NE7CMD_SENSED, fdc->fd->fdsu, 1, &st3)) 520 return (fdc_err(fdc, "Sense Drive Status failed\n")); 521 if (st3p) 522 *st3p = st3; 523 return (0); 524 } 525 526 static int 527 fdc_sense_int(struct fdc_data *fdc, int *st0p, int *cylp) 528 { 529 int cyl, st0, ret; 530 531 ret = fdc_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0); 532 if (ret) { 533 (void)fdc_err(fdc, "sense intr err reading stat reg 0\n"); 534 return (ret); 535 } 536 537 if (st0p) 538 *st0p = st0; 539 540 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) { 541 /* 542 * There doesn't seem to have been an interrupt. 543 */ 544 return (FD_NOT_VALID); 545 } 546 547 if (fdc_in(fdc, &cyl) < 0) 548 return fdc_err(fdc, "can't get cyl num\n"); 549 550 if (cylp) 551 *cylp = cyl; 552 553 return (0); 554 } 555 556 static int 557 fdc_read_status(struct fdc_data *fdc) 558 { 559 int i, ret, status; 560 561 for (i = ret = 0; i < 7; i++) { 562 ret = fdc_in(fdc, &status); 563 fdc->status[i] = status; 564 if (ret != 0) 565 break; 566 } 567 568 if (ret == 0) 569 fdc->flags |= FDC_STAT_VALID; 570 else 571 fdc->flags &= ~FDC_STAT_VALID; 572 573 return ret; 574 } 575 576 /* 577 * Select this drive 578 */ 579 static void 580 fd_select(struct fd_data *fd) 581 { 582 struct fdc_data *fdc; 583 584 /* XXX: lock controller */ 585 fdc = fd->fdc; 586 fdc->fdout &= ~FDO_FDSEL; 587 fdc->fdout |= FDO_FDMAEN | FDO_FRST | fd->fdsu; 588 fdout_wr(fdc, fdc->fdout); 589 } 590 591 static void 592 fd_turnon(void *arg) 593 { 594 struct fd_data *fd; 595 struct bio *bp; 596 int once; 597 598 fd = arg; 599 mtx_lock(&fd->fdc->fdc_mtx); 600 fd->flags &= ~FD_MOTORWAIT; 601 fd->flags |= FD_MOTOR; 602 once = 0; 603 for (;;) { 604 bp = bioq_takefirst(&fd->fd_bq); 605 if (bp == NULL) 606 break; 607 bioq_disksort(&fd->fdc->head, bp); 608 once = 1; 609 } 610 mtx_unlock(&fd->fdc->fdc_mtx); 611 if (once) 612 wakeup(&fd->fdc->head); 613 } 614 615 static void 616 fd_motor(struct fd_data *fd, int turnon) 617 { 618 struct fdc_data *fdc; 619 620 fdc = fd->fdc; 621 /* 622 mtx_assert(&fdc->fdc_mtx, MA_OWNED); 623 */ 624 if (turnon) { 625 fd->flags |= FD_MOTORWAIT; 626 fdc->fdout |= (FDO_MOEN0 << fd->fdsu); 627 callout_reset(&fd->toffhandle, hz, fd_turnon, fd); 628 } else { 629 callout_drain(&fd->toffhandle); 630 fd->flags &= ~(FD_MOTOR|FD_MOTORWAIT); 631 fdc->fdout &= ~(FDO_MOEN0 << fd->fdsu); 632 } 633 fdout_wr(fdc, fdc->fdout); 634 } 635 636 static void 637 fd_turnoff(void *xfd) 638 { 639 struct fd_data *fd = xfd; 640 641 mtx_lock(&fd->fdc->fdc_mtx); 642 fd_motor(fd, 0); 643 mtx_unlock(&fd->fdc->fdc_mtx); 644 } 645 646 /* 647 * fdc_intr 648 * 649 * Keep calling the state machine until it returns a 0. 650 */ 651 static void 652 fdc_intr(void *arg) 653 { 654 655 wakeup(arg); 656 } 657 658 /* 659 * fdc_pio(): perform programmed IO read/write for YE PCMCIA floppy. 660 */ 661 static void 662 fdc_pio(struct fdc_data *fdc) 663 { 664 u_char *cptr; 665 struct bio *bp; 666 u_int count; 667 668 bp = fdc->bp; 669 cptr = fdc->fd->fd_ioptr; 670 count = fdc->fd->fd_iosize; 671 672 if (bp->bio_cmd == BIO_READ) { 673 fdbcdr_wr(fdc, 0, count); 674 bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off + 675 FD_YE_DATAPORT, cptr, count); 676 } else { 677 bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off + 678 FD_YE_DATAPORT, cptr, count); 679 fdbcdr_wr(fdc, 0, count); /* needed? */ 680 } 681 } 682 683 static int 684 fdc_biodone(struct fdc_data *fdc, int error) 685 { 686 struct fd_data *fd; 687 struct bio *bp; 688 689 fd = fdc->fd; 690 bp = fdc->bp; 691 692 mtx_lock(&fdc->fdc_mtx); 693 if (--fd->fd_iocount == 0) 694 callout_reset(&fd->toffhandle, 4 * hz, fd_turnoff, fd); 695 fdc->bp = NULL; 696 fdc->fd = NULL; 697 mtx_unlock(&fdc->fdc_mtx); 698 if (bp->bio_to != NULL) { 699 if ((debugflags & 2) && fd->fdc->retry > 0) 700 printf("retries: %d\n", fd->fdc->retry); 701 g_io_deliver(bp, error); 702 return (0); 703 } 704 bp->bio_error = error; 705 bp->bio_flags |= BIO_DONE; 706 wakeup(bp); 707 return (0); 708 } 709 710 static int retry_line; 711 712 static int 713 fdc_worker(struct fdc_data *fdc) 714 { 715 struct fd_data *fd; 716 struct bio *bp; 717 int i, nsect; 718 int st0, st3, cyl, mfm, steptrac, cylinder, descyl, sec; 719 int head; 720 static int need_recal; 721 struct fdc_readid *idp; 722 struct fd_formb *finfo; 723 724 /* Have we exhausted our retries ? */ 725 bp = fdc->bp; 726 fd = fdc->fd; 727 if (bp != NULL && 728 (fdc->retry >= retries || (fd->flags & FDOPT_NORETRY))) { 729 if ((debugflags & 4)) 730 printf("Too many retries (EIO)\n"); 731 return (fdc_biodone(fdc, EIO)); 732 } 733 734 /* Disable ISADMA if we bailed while it was active */ 735 if (fd != NULL && (fd->flags & FD_ISADMA)) { 736 mtx_lock(&Giant); 737 isa_dmadone( 738 bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE, 739 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan); 740 mtx_unlock(&Giant); 741 fd->flags &= ~FD_ISADMA; 742 } 743 744 /* Unwedge the controller ? */ 745 if (fdc->flags & FDC_NEEDS_RESET) { 746 fdc->flags &= ~FDC_NEEDS_RESET; 747 fdc_reset(fdc); 748 msleep(fdc, NULL, PRIBIO, "fdcrst", hz); 749 /* Discard results */ 750 for (i = 0; i < 4; i++) 751 fdc_sense_int(fdc, &st0, &cyl); 752 /* All drives must recal */ 753 need_recal = 0xf; 754 } 755 756 /* Pick up a request, if need be wait for it */ 757 if (fdc->bp == NULL) { 758 mtx_lock(&fdc->fdc_mtx); 759 do { 760 fdc->bp = bioq_takefirst(&fdc->head); 761 if (fdc->bp == NULL) 762 msleep(&fdc->head, &fdc->fdc_mtx, 763 PRIBIO, "-", hz); 764 } while (fdc->bp == NULL); 765 mtx_unlock(&fdc->fdc_mtx); 766 bp = fdc->bp; 767 fd = fdc->fd = bp->bio_driver1; 768 fdc->retry = 0; 769 fd->fd_ioptr = bp->bio_data; 770 if (bp->bio_cmd & BIO_FMT) { 771 i = offsetof(struct fd_formb, fd_formb_cylno(0)); 772 fd->fd_ioptr += i; 773 fd->fd_iosize = bp->bio_length - i; 774 } 775 } 776 777 /* Select drive, setup params */ 778 fd_select(fd); 779 fdctl_wr(fdc, fd->ft->trans); 780 781 if (bp->bio_cmd & BIO_PROBE) { 782 783 if (!(fdin_rd(fdc) & FDI_DCHG) && !(fd->flags & FD_EMPTY)) 784 return (fdc_biodone(fdc, 0)); 785 786 /* 787 * Try to find out if we have a disk in the drive 788 * 789 * First recal, then seek to cyl#1, this clears the 790 * old condition on the disk change line so we can 791 * examine it for current status 792 */ 793 if (debugflags & 0x40) 794 printf("New disk in probe\n"); 795 fd->flags |= FD_NEWDISK; 796 retry_line = __LINE__; 797 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0)) 798 return (1); 799 msleep(fdc, NULL, PRIBIO, "fdrecal", hz); 800 retry_line = __LINE__; 801 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) 802 return (1); /* XXX */ 803 retry_line = __LINE__; 804 if ((st0 & 0xc0) || cyl != 0) 805 return (1); 806 807 /* Seek to track 1 */ 808 retry_line = __LINE__; 809 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, 1, 0)) 810 return (1); 811 msleep(fdc, NULL, PRIBIO, "fdseek", hz); 812 retry_line = __LINE__; 813 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) 814 return (1); /* XXX */ 815 need_recal |= (1 << fd->fdsu); 816 if (fdin_rd(fdc) & FDI_DCHG) { 817 if (debugflags & 0x40) 818 printf("Empty in probe\n"); 819 fd->flags |= FD_EMPTY; 820 } else { 821 if (debugflags & 0x40) 822 printf("Got disk in probe\n"); 823 fd->flags &= ~FD_EMPTY; 824 retry_line = __LINE__; 825 if(fdc_sense_drive(fdc, &st3) != 0) 826 return (1); 827 if(st3 & NE7_ST3_WP) 828 fd->flags |= FD_WP; 829 else 830 fd->flags &= ~FD_WP; 831 } 832 return (fdc_biodone(fdc, 0)); 833 } 834 835 /* 836 * If we are dead just flush the requests 837 */ 838 if (fd->flags & FD_EMPTY) 839 return (fdc_biodone(fdc, ENXIO)); 840 841 /* Check if we lost our media */ 842 if (fdin_rd(fdc) & FDI_DCHG) { 843 if (debugflags & 0x40) 844 printf("Lost disk\n"); 845 fd->flags |= FD_EMPTY; 846 fd->flags |= FD_NEWDISK; 847 g_topology_lock(); 848 g_orphan_provider(fd->fd_provider, EXDEV); 849 fd->fd_provider->flags |= G_PF_WITHER; 850 fd->fd_provider = 851 g_new_providerf(fd->fd_geom, fd->fd_geom->name); 852 g_error_provider(fd->fd_provider, 0); 853 g_topology_unlock(); 854 return (fdc_biodone(fdc, ENXIO)); 855 } 856 857 /* Check if the floppy is write-protected */ 858 if(bp->bio_cmd & (BIO_FMT | BIO_WRITE)) { 859 retry_line = __LINE__; 860 if(fdc_sense_drive(fdc, &st3) != 0) 861 return (1); 862 if(st3 & NE7_ST3_WP) 863 return (fdc_biodone(fdc, EROFS)); 864 } 865 866 mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0; 867 steptrac = (fd->ft->flags & FL_2STEP)? 2: 1; 868 i = fd->ft->sectrac * fd->ft->heads; 869 cylinder = bp->bio_pblkno / i; 870 descyl = cylinder * steptrac; 871 sec = bp->bio_pblkno % i; 872 nsect = i - sec; 873 head = sec / fd->ft->sectrac; 874 sec = sec % fd->ft->sectrac + 1; 875 876 /* If everything is going swimmingly, use multisector xfer */ 877 if (fdc->retry == 0 && bp->bio_cmd & (BIO_READ|BIO_WRITE)) { 878 fd->fd_iosize = imin(nsect * fd->sectorsize, bp->bio_resid); 879 nsect = fd->fd_iosize / fd->sectorsize; 880 } else if (bp->bio_cmd & (BIO_READ|BIO_WRITE)) { 881 fd->fd_iosize = fd->sectorsize; 882 nsect = 1; 883 } 884 885 /* Do RECAL if we need to or are going to track zero anyway */ 886 if ((need_recal & (1 << fd->fdsu)) || 887 (cylinder == 0 && fd->track != 0) || 888 fdc->retry > 2) { 889 retry_line = __LINE__; 890 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0)) 891 return (1); 892 msleep(fdc, NULL, PRIBIO, "fdrecal", hz); 893 retry_line = __LINE__; 894 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) 895 return (1); /* XXX */ 896 retry_line = __LINE__; 897 if ((st0 & 0xc0) || cyl != 0) 898 return (1); 899 need_recal &= ~(1 << fd->fdsu); 900 fd->track = 0; 901 /* let the heads settle */ 902 if (settle) 903 msleep(fdc->fd, NULL, PRIBIO, "fdhdstl", settle); 904 } 905 906 /* 907 * SEEK to where we want to be 908 * 909 * Enhanced controllers do implied seeks for read&write as long as 910 * we do not need multiple steps per track. 911 */ 912 if (cylinder != fd->track && ( 913 fdc->fdct != FDC_ENHANCED || 914 descyl != cylinder || 915 (bp->bio_cmd & (BIO_RDID|BIO_FMT)))) { 916 retry_line = __LINE__; 917 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, descyl, 0)) 918 return (1); 919 msleep(fdc, NULL, PRIBIO, "fdseek", hz); 920 retry_line = __LINE__; 921 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) 922 return (1); /* XXX */ 923 retry_line = __LINE__; 924 if ((st0 & 0xc0) || cyl != descyl) { 925 need_recal |= (1 << fd->fdsu); 926 return (1); 927 } 928 /* let the heads settle */ 929 if (settle) 930 msleep(fdc->fd, NULL, PRIBIO, "fdhdstl", settle); 931 } 932 fd->track = cylinder; 933 934 if (debugflags & 8) 935 printf("op %x bn %ju siz %u ptr %p retry %d\n", 936 bp->bio_cmd, bp->bio_pblkno, fd->fd_iosize, 937 fd->fd_ioptr, fdc->retry); 938 939 /* Setup ISADMA if we need it and have it */ 940 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT)) 941 && !(fdc->flags & FDC_NODMA)) { 942 mtx_lock(&Giant); 943 isa_dmastart( 944 bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE, 945 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan); 946 mtx_unlock(&Giant); 947 fd->flags |= FD_ISADMA; 948 } 949 950 /* Do PIO if we have to */ 951 if (fdc->flags & FDC_NODMA) { 952 if (bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT)) 953 fdbcdr_wr(fdc, 1, fd->fd_iosize); 954 if (bp->bio_cmd & (BIO_WRITE|BIO_FMT)) 955 fdc_pio(fdc); 956 } 957 958 switch(bp->bio_cmd) { 959 case BIO_FMT: 960 /* formatting */ 961 finfo = (struct fd_formb *)bp->bio_data; 962 retry_line = __LINE__; 963 if (fdc_cmd(fdc, 6, 964 NE7CMD_FORMAT | mfm, 965 head << 2 | fd->fdsu, 966 finfo->fd_formb_secshift, 967 finfo->fd_formb_nsecs, 968 finfo->fd_formb_gaplen, 969 finfo->fd_formb_fillbyte, 0)) 970 return (1); 971 break; 972 case BIO_RDID: 973 retry_line = __LINE__; 974 if (fdc_cmd(fdc, 2, 975 NE7CMD_READID | mfm, 976 head << 2 | fd->fdsu, 0)) 977 return (1); 978 break; 979 case BIO_READ: 980 retry_line = __LINE__; 981 if (fdc_cmd(fdc, 9, 982 NE7CMD_READ | NE7CMD_SK | mfm | NE7CMD_MT, 983 head << 2 | fd->fdsu, /* head & unit */ 984 fd->track, /* track */ 985 head, /* head */ 986 sec, /* sector + 1 */ 987 fd->ft->secsize, /* sector size */ 988 fd->ft->sectrac, /* sectors/track */ 989 fd->ft->gap, /* gap size */ 990 fd->ft->datalen, /* data length */ 991 0)) 992 return (1); 993 break; 994 case BIO_WRITE: 995 retry_line = __LINE__; 996 if (fdc_cmd(fdc, 9, 997 NE7CMD_WRITE | mfm | NE7CMD_MT, 998 head << 2 | fd->fdsu, /* head & unit */ 999 fd->track, /* track */ 1000 head, /* head */ 1001 sec, /* sector + 1 */ 1002 fd->ft->secsize, /* sector size */ 1003 fd->ft->sectrac, /* sectors/track */ 1004 fd->ft->gap, /* gap size */ 1005 fd->ft->datalen, /* data length */ 1006 0)) 1007 return (1); 1008 break; 1009 default: 1010 KASSERT(0 == 1, ("Wrong bio_cmd %x\n", bp->bio_cmd)); 1011 } 1012 1013 /* Wait for interrupt */ 1014 i = msleep(fdc, NULL, PRIBIO, "fddata", hz); 1015 1016 /* PIO if the read looks good */ 1017 if (i == 0 && (fdc->flags & FDC_NODMA) && (bp->bio_cmd & BIO_READ)) 1018 fdc_pio(fdc); 1019 1020 /* Finish DMA */ 1021 if (fd->flags & FD_ISADMA) { 1022 mtx_lock(&Giant); 1023 isa_dmadone( 1024 bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE, 1025 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan); 1026 mtx_unlock(&Giant); 1027 fd->flags &= ~FD_ISADMA; 1028 } 1029 1030 if (i != 0) { 1031 /* 1032 * Timeout. 1033 * 1034 * Due to IBM's brain-dead design, the FDC has a faked ready 1035 * signal, hardwired to ready == true. Thus, any command 1036 * issued if there's no diskette in the drive will _never_ 1037 * complete, and must be aborted by resetting the FDC. 1038 * Many thanks, Big Blue! 1039 */ 1040 retry_line = __LINE__; 1041 fdc->flags |= FDC_NEEDS_RESET; 1042 return (1); 1043 } 1044 1045 retry_line = __LINE__; 1046 if (fdc_read_status(fdc)) 1047 return (1); 1048 1049 if (debugflags & 0x10) 1050 printf(" -> %x %x %x %x\n", 1051 fdc->status[0], fdc->status[1], 1052 fdc->status[2], fdc->status[3]); 1053 1054 st0 = fdc->status[0] & NE7_ST0_IC; 1055 if (st0 != 0) { 1056 retry_line = __LINE__; 1057 if (st0 == NE7_ST0_IC_AT && fdc->status[1] & NE7_ST1_OR) { 1058 /* 1059 * DMA overrun. Someone hogged the bus and 1060 * didn't release it in time for the next 1061 * FDC transfer. 1062 */ 1063 return (1); 1064 } 1065 retry_line = __LINE__; 1066 if(st0 == NE7_ST0_IC_IV) { 1067 fdc->flags |= FDC_NEEDS_RESET; 1068 return (1); 1069 } 1070 retry_line = __LINE__; 1071 if(st0 == NE7_ST0_IC_AT && fdc->status[2] & NE7_ST2_WC) { 1072 need_recal |= (1 << fd->fdsu); 1073 return (1); 1074 } 1075 if (debugflags & 0x20) { 1076 printf("status %02x %02x %02x %02x %02x %02x\n", 1077 fdc->status[0], fdc->status[1], fdc->status[2], 1078 fdc->status[3], fdc->status[4], fdc->status[5]); 1079 } 1080 retry_line = __LINE__; 1081 return (1); 1082 } 1083 /* All OK */ 1084 switch(bp->bio_cmd) { 1085 case BIO_RDID: 1086 /* copy out ID field contents */ 1087 idp = (struct fdc_readid *)bp->bio_data; 1088 idp->cyl = fdc->status[3]; 1089 idp->head = fdc->status[4]; 1090 idp->sec = fdc->status[5]; 1091 idp->secshift = fdc->status[6]; 1092 if (debugflags & 0x40) 1093 printf("c %d h %d s %d z %d\n", 1094 idp->cyl, idp->head, idp->sec, idp->secshift); 1095 break; 1096 case BIO_READ: 1097 case BIO_WRITE: 1098 bp->bio_pblkno += nsect; 1099 bp->bio_resid -= fd->fd_iosize; 1100 bp->bio_completed += fd->fd_iosize; 1101 fd->fd_ioptr += fd->fd_iosize; 1102 /* Since we managed to get something done, reset the retry */ 1103 fdc->retry = 0; 1104 if (bp->bio_resid > 0) 1105 return (0); 1106 break; 1107 case BIO_FMT: 1108 break; 1109 } 1110 return (fdc_biodone(fdc, 0)); 1111 } 1112 1113 static void 1114 fdc_thread(void *arg) 1115 { 1116 struct fdc_data *fdc; 1117 1118 fdc = arg; 1119 int i; 1120 1121 for (;;) { 1122 i = fdc_worker(fdc); 1123 if (i && debugflags & 0x20) { 1124 if (fdc->bp != NULL) { 1125 g_print_bio(fdc->bp); 1126 printf("\n"); 1127 } 1128 printf("Retry line %d\n", retry_line); 1129 } 1130 fdc->retry += i; 1131 } 1132 } 1133 1134 /* 1135 * Enqueue a requst. 1136 */ 1137 static void 1138 fd_enqueue(struct fd_data *fd, struct bio *bp) 1139 { 1140 struct fdc_data *fdc; 1141 int call; 1142 1143 call = 0; 1144 fdc = fd->fdc; 1145 mtx_lock(&fdc->fdc_mtx); 1146 /* If we go from idle, cancel motor turnoff */ 1147 if (fd->fd_iocount++ == 0) 1148 callout_drain(&fd->toffhandle); 1149 if (fd->flags & FD_MOTOR) { 1150 /* The motor is on, send it directly to the controller */ 1151 bioq_disksort(&fdc->head, bp); 1152 wakeup(&fdc->head); 1153 } else { 1154 /* Queue it on the drive until the motor has started */ 1155 bioq_insert_tail(&fd->fd_bq, bp); 1156 if (!(fd->flags & FD_MOTORWAIT)) 1157 fd_motor(fd, 1); 1158 } 1159 mtx_unlock(&fdc->fdc_mtx); 1160 } 1161 1162 static int 1163 fdmisccmd(struct fd_data *fd, u_int cmd, void *data) 1164 { 1165 struct bio *bp; 1166 struct fd_formb *finfo; 1167 struct fdc_readid *idfield; 1168 int error; 1169 1170 bp = malloc(sizeof(struct bio), M_TEMP, M_WAITOK | M_ZERO); 1171 1172 /* 1173 * Set up a bio request for fdstrategy(). bio_offset is faked 1174 * so that fdstrategy() will seek to the the requested 1175 * cylinder, and use the desired head. 1176 */ 1177 bp->bio_cmd = cmd; 1178 if (cmd == BIO_FMT) { 1179 finfo = (struct fd_formb *)data; 1180 bp->bio_pblkno = 1181 (finfo->cyl * fd->ft->heads + finfo->head) * 1182 fd->ft->sectrac; 1183 bp->bio_length = sizeof *finfo; 1184 } else if (cmd == BIO_RDID) { 1185 idfield = (struct fdc_readid *)data; 1186 bp->bio_pblkno = 1187 (idfield->cyl * fd->ft->heads + idfield->head) * 1188 fd->ft->sectrac; 1189 bp->bio_length = sizeof(struct fdc_readid); 1190 } else if (cmd == BIO_PROBE) { 1191 /* nothing */ 1192 } else 1193 panic("wrong cmd in fdmisccmd()"); 1194 bp->bio_offset = bp->bio_pblkno * fd->sectorsize; 1195 bp->bio_data = data; 1196 bp->bio_driver1 = fd; 1197 bp->bio_flags = 0; 1198 1199 fd_enqueue(fd, bp); 1200 1201 do { 1202 msleep(bp, NULL, PRIBIO, "fdwait", hz); 1203 } while (!(bp->bio_flags & BIO_DONE)); 1204 error = bp->bio_error; 1205 1206 free(bp, M_TEMP); 1207 return (error); 1208 } 1209 1210 /* 1211 * Try figuring out the density of the media present in our device. 1212 */ 1213 static int 1214 fdautoselect(struct fd_data *fd) 1215 { 1216 struct fd_type *fdtp; 1217 struct fdc_readid id; 1218 int oopts, rv; 1219 1220 if (!(fd->ft->flags & FL_AUTO)) 1221 return (0); 1222 1223 fdtp = fd_native_types[fd->type]; 1224 fdsettype(fd, fdtp); 1225 if (!(fd->ft->flags & FL_AUTO)) 1226 return (0); 1227 1228 /* 1229 * Try reading sector ID fields, first at cylinder 0, head 0, 1230 * then at cylinder 2, head N. We don't probe cylinder 1, 1231 * since for 5.25in DD media in a HD drive, there are no data 1232 * to read (2 step pulses per media cylinder required). For 1233 * two-sided media, the second probe always goes to head 1, so 1234 * we can tell them apart from single-sided media. As a 1235 * side-effect this means that single-sided media should be 1236 * mentioned in the search list after two-sided media of an 1237 * otherwise identical density. Media with a different number 1238 * of sectors per track but otherwise identical parameters 1239 * cannot be distinguished at all. 1240 * 1241 * If we successfully read an ID field on both cylinders where 1242 * the recorded values match our expectation, we are done. 1243 * Otherwise, we try the next density entry from the table. 1244 * 1245 * Stepping to cylinder 2 has the side-effect of clearing the 1246 * unit attention bit. 1247 */ 1248 oopts = fd->options; 1249 fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY; 1250 for (; fdtp->heads; fdtp++) { 1251 fdsettype(fd, fdtp); 1252 1253 id.cyl = id.head = 0; 1254 rv = fdmisccmd(fd, BIO_RDID, &id); 1255 if (rv != 0) 1256 continue; 1257 if (id.cyl != 0 || id.head != 0 || id.secshift != fdtp->secsize) 1258 continue; 1259 id.cyl = 2; 1260 id.head = fd->ft->heads - 1; 1261 rv = fdmisccmd(fd, BIO_RDID, &id); 1262 if (id.cyl != 2 || id.head != fdtp->heads - 1 || 1263 id.secshift != fdtp->secsize) 1264 continue; 1265 if (rv == 0) 1266 break; 1267 } 1268 1269 fd->options = oopts; 1270 if (fdtp->heads == 0) { 1271 if (debugflags & 0x40) 1272 device_printf(fd->dev, "autoselection failed\n"); 1273 fdsettype(fd, fd_native_types[fd->type]); 1274 return (0); 1275 } else { 1276 if (debugflags & 0x40) { 1277 device_printf(fd->dev, 1278 "autoselected %d KB medium\n", fd->ft->size / 2); 1279 fdprinttype(fd->ft); 1280 } 1281 return (0); 1282 } 1283 } 1284 1285 /* 1286 * GEOM class implementation 1287 */ 1288 1289 static g_access_t fd_access; 1290 static g_start_t fd_start; 1291 static g_ioctl_t fd_ioctl; 1292 1293 struct g_class g_fd_class = { 1294 .name = "FD", 1295 .version = G_VERSION, 1296 .start = fd_start, 1297 .access = fd_access, 1298 .ioctl = fd_ioctl, 1299 }; 1300 1301 DECLARE_GEOM_CLASS(g_fd_class, g_fd); 1302 1303 static int 1304 fd_access(struct g_provider *pp, int r, int w, int e) 1305 { 1306 struct fd_data *fd; 1307 struct fdc_data *fdc; 1308 int ar, aw, ae; 1309 1310 fd = pp->geom->softc; 1311 fdc = fd->fdc; 1312 1313 /* 1314 * If our provider is withering, we can only get negative requests 1315 * and we don't want to even see them 1316 */ 1317 if (pp->flags & G_PF_WITHER) 1318 return (0); 1319 1320 ar = r + pp->acr; 1321 aw = w + pp->acw; 1322 ae = e + pp->ace; 1323 1324 if (ar == 0 && aw == 0 && ae == 0) { 1325 device_unbusy(fd->dev); 1326 return (0); 1327 } 1328 1329 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) { 1330 if (fdmisccmd(fd, BIO_PROBE, NULL)) 1331 return (ENXIO); 1332 if (fd->flags & FD_EMPTY) 1333 return (ENXIO); 1334 if (fd->flags & FD_NEWDISK) { 1335 fdautoselect(fd); 1336 fd->flags &= ~FD_NEWDISK; 1337 } 1338 device_busy(fd->dev); 1339 } 1340 1341 #ifdef notyet 1342 if (w > 0 && (fd->flags & FD_WP)) 1343 return (EROFS); 1344 #endif 1345 1346 pp->sectorsize = fd->sectorsize; 1347 pp->stripesize = fd->ft->heads * fd->ft->sectrac * fd->sectorsize; 1348 pp->mediasize = pp->stripesize * fd->ft->tracks; 1349 return (0); 1350 } 1351 1352 static void 1353 fd_start(struct bio *bp) 1354 { 1355 struct fdc_data * fdc; 1356 struct fd_data * fd; 1357 1358 fd = bp->bio_to->geom->softc; 1359 fdc = fd->fdc; 1360 bp->bio_driver1 = fd; 1361 if (bp->bio_cmd & BIO_GETATTR) { 1362 if (g_handleattr_int(bp, "GEOM::fwsectors", fd->ft->sectrac)) 1363 return; 1364 if (g_handleattr_int(bp, "GEOM::fwheads", fd->ft->heads)) 1365 return; 1366 g_io_deliver(bp, ENOIOCTL); 1367 return; 1368 } 1369 if (!(bp->bio_cmd & (BIO_READ|BIO_WRITE))) { 1370 g_io_deliver(bp, EOPNOTSUPP); 1371 return; 1372 } 1373 bp->bio_pblkno = bp->bio_offset / fd->sectorsize; 1374 bp->bio_resid = bp->bio_length; 1375 fd_enqueue(fd, bp); 1376 return; 1377 } 1378 1379 static int 1380 fd_ioctl(struct g_provider *pp, u_long cmd, void *data, struct thread *td) 1381 { 1382 struct fd_data *fd; 1383 struct fdc_status *fsp; 1384 struct fdc_readid *rid; 1385 int error; 1386 1387 fd = pp->geom->softc; 1388 1389 switch (cmd) { 1390 case FD_GTYPE: /* get drive type */ 1391 *(struct fd_type *)data = *fd->ft; 1392 return (0); 1393 1394 case FD_STYPE: /* set drive type */ 1395 /* 1396 * Allow setting drive type temporarily iff 1397 * currently unset. Used for fdformat so any 1398 * user can set it, and then start formatting. 1399 */ 1400 fd->fts = *(struct fd_type *)data; 1401 if (fd->fts.sectrac) { 1402 /* XXX: check for rubbish */ 1403 fdsettype(fd, &fd->fts); 1404 } else { 1405 fdsettype(fd, fd_native_types[fd->type]); 1406 } 1407 if (debugflags & 0x40) 1408 fdprinttype(fd->ft); 1409 return (0); 1410 1411 case FD_GOPTS: /* get drive options */ 1412 *(int *)data = fd->options; 1413 return (0); 1414 1415 case FD_SOPTS: /* set drive options */ 1416 fd->options = *(int *)data; 1417 return (0); 1418 1419 case FD_CLRERR: 1420 if (suser(td) != 0) 1421 return (EPERM); 1422 fd->fdc->fdc_errs = 0; 1423 return (0); 1424 1425 case FD_GSTAT: 1426 fsp = (struct fdc_status *)data; 1427 if ((fd->fdc->flags & FDC_STAT_VALID) == 0) 1428 return (EINVAL); 1429 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int)); 1430 return (0); 1431 1432 case FD_GDTYPE: 1433 *(enum fd_drivetype *)data = fd->type; 1434 return (0); 1435 1436 case FD_FORM: 1437 if (((struct fd_formb *)data)->format_version != 1438 FD_FORMAT_VERSION) 1439 return (EINVAL); /* wrong version of formatting prog */ 1440 error = fdmisccmd(fd, BIO_FMT, data); 1441 fd->flags |= FD_NEWDISK; 1442 break; 1443 1444 case FD_READID: 1445 rid = (struct fdc_readid *)data; 1446 if (rid->cyl > 85 || rid->head > 1) 1447 return (EINVAL); 1448 error = fdmisccmd(fd, BIO_RDID, data); 1449 break; 1450 1451 case FIONBIO: 1452 case FIOASYNC: 1453 /* For backwards compat with old fd*(8) tools */ 1454 error = 0; 1455 break; 1456 1457 default: 1458 if (debugflags & 0x80) 1459 printf("Unknown ioctl %lx\n", cmd); 1460 error = ENOIOCTL; 1461 break; 1462 } 1463 return (error); 1464 }; 1465 1466 1467 1468 /* 1469 * Configuration/initialization stuff, per controller. 1470 */ 1471 1472 devclass_t fdc_devclass; 1473 static devclass_t fd_devclass; 1474 1475 struct fdc_ivars { 1476 int fdunit; 1477 int fdtype; 1478 }; 1479 1480 void 1481 fdc_release_resources(struct fdc_data *fdc) 1482 { 1483 device_t dev; 1484 1485 dev = fdc->fdc_dev; 1486 if (fdc->fdc_intr) 1487 bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr); 1488 fdc->fdc_intr = NULL; 1489 if (fdc->res_irq != NULL) 1490 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq, 1491 fdc->res_irq); 1492 fdc->res_irq = NULL; 1493 if (fdc->res_ctl != NULL) 1494 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl, 1495 fdc->res_ctl); 1496 fdc->res_ctl = NULL; 1497 if (fdc->res_sts != NULL) 1498 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_sts, 1499 fdc->res_sts); 1500 fdc->res_sts = NULL; 1501 if (fdc->res_ioport != NULL) 1502 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport, 1503 fdc->res_ioport); 1504 fdc->res_ioport = NULL; 1505 if (fdc->res_drq != NULL) 1506 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq, 1507 fdc->res_drq); 1508 fdc->res_drq = NULL; 1509 } 1510 1511 int 1512 fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 1513 { 1514 struct fdc_ivars *ivars = device_get_ivars(child); 1515 1516 switch (which) { 1517 case FDC_IVAR_FDUNIT: 1518 *result = ivars->fdunit; 1519 break; 1520 case FDC_IVAR_FDTYPE: 1521 *result = ivars->fdtype; 1522 break; 1523 default: 1524 return (ENOENT); 1525 } 1526 return (0); 1527 } 1528 1529 int 1530 fdc_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 1531 { 1532 struct fdc_ivars *ivars = device_get_ivars(child); 1533 1534 switch (which) { 1535 case FDC_IVAR_FDUNIT: 1536 ivars->fdunit = value; 1537 break; 1538 case FDC_IVAR_FDTYPE: 1539 ivars->fdtype = value; 1540 break; 1541 default: 1542 return (ENOENT); 1543 } 1544 return (0); 1545 } 1546 1547 int 1548 fdc_initial_reset(device_t dev, struct fdc_data *fdc) 1549 { 1550 int ic_type, part_id; 1551 1552 /* 1553 * A status value of 0xff is very unlikely, but not theoretically 1554 * impossible, but it is far more likely to indicate an empty bus. 1555 */ 1556 if (fdsts_rd(fdc) == 0xff) 1557 return (ENXIO); 1558 1559 /* 1560 * Assert a reset to the floppy controller and check that the status 1561 * register goes to zero. 1562 */ 1563 fdout_wr(fdc, 0); 1564 fdout_wr(fdc, 0); 1565 if (fdsts_rd(fdc) != 0) 1566 return (ENXIO); 1567 1568 /* 1569 * Clear the reset and see it come ready. 1570 */ 1571 fdout_wr(fdc, FDO_FRST); 1572 DELAY(100); 1573 if (fdsts_rd(fdc) != 0x80) 1574 return (ENXIO); 1575 1576 /* Then, see if it can handle a command. */ 1577 if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, 0xaf, 0x1e, 0)) 1578 return (ENXIO); 1579 1580 /* 1581 * Try to identify the chip. 1582 * 1583 * The i8272 datasheet documents that unknown commands 1584 * will return ST0 as 0x80. The i8272 is supposedly identical 1585 * to the NEC765. 1586 * The i82077SL datasheet says 0x90 for the VERSION command, 1587 * and several "superio" chips emulate this. 1588 */ 1589 if (fdc_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type)) 1590 return (ENXIO); 1591 if (fdc_cmd(fdc, 1, 0x18, 1, &part_id)) 1592 return (ENXIO); 1593 if (bootverbose) 1594 device_printf(dev, 1595 "ic_type %02x part_id %02x\n", ic_type, part_id); 1596 switch (ic_type & 0xff) { 1597 case 0x80: 1598 device_set_desc(dev, "NEC 765 or clone"); 1599 fdc->fdct = FDC_NE765; 1600 break; 1601 case 0x81: 1602 case 0x90: 1603 device_set_desc(dev, 1604 "Enhanced floppy controller"); 1605 fdc->fdct = FDC_ENHANCED; 1606 break; 1607 default: 1608 device_set_desc(dev, "Generic floppy controller"); 1609 fdc->fdct = FDC_UNKNOWN; 1610 break; 1611 } 1612 return (0); 1613 } 1614 1615 int 1616 fdc_detach(device_t dev) 1617 { 1618 struct fdc_data *fdc; 1619 int error; 1620 1621 fdc = device_get_softc(dev); 1622 1623 /* have our children detached first */ 1624 if ((error = bus_generic_detach(dev))) 1625 return (error); 1626 1627 /* XXX: kill thread */ 1628 /* reset controller, turn motor off */ 1629 fdout_wr(fdc, 0); 1630 1631 if (!(fdc->flags & FDC_NODMA)) 1632 isa_dma_release(fdc->dmachan); 1633 fdc_release_resources(fdc); 1634 mtx_destroy(&fdc->fdc_mtx); 1635 return (0); 1636 } 1637 1638 /* 1639 * Add a child device to the fdc controller. It will then be probed etc. 1640 */ 1641 device_t 1642 fdc_add_child(device_t dev, const char *name, int unit) 1643 { 1644 int flags; 1645 struct fdc_ivars *ivar; 1646 device_t child; 1647 1648 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO); 1649 if (ivar == NULL) 1650 return (NULL); 1651 child = device_add_child(dev, name, unit); 1652 if (child == NULL) { 1653 free(ivar, M_DEVBUF); 1654 return (NULL); 1655 } 1656 device_set_ivars(child, ivar); 1657 ivar->fdunit = unit; 1658 ivar->fdtype = FDT_NONE; 1659 if (resource_int_value(name, unit, "flags", &flags) == 0) 1660 device_set_flags(child, flags); 1661 if (resource_disabled(name, unit)) 1662 device_disable(child); 1663 return (child); 1664 } 1665 1666 int 1667 fdc_attach(device_t dev) 1668 { 1669 struct fdc_data *fdc; 1670 int error; 1671 1672 fdc = device_get_softc(dev); 1673 fdc->fdc_dev = dev; 1674 error = fdc_initial_reset(dev, fdc); 1675 if (error) { 1676 device_printf(dev, "does not respond\n"); 1677 return (error); 1678 } 1679 error = bus_setup_intr(dev, fdc->res_irq, 1680 INTR_TYPE_BIO | INTR_ENTROPY | INTR_FAST | INTR_MPSAFE, 1681 fdc_intr, fdc, &fdc->fdc_intr); 1682 if (error) { 1683 device_printf(dev, "cannot setup interrupt\n"); 1684 return (error); 1685 } 1686 if (!(fdc->flags & FDC_NODMA)) { 1687 error = isa_dma_acquire(fdc->dmachan); 1688 if (error) 1689 return (error); 1690 /* XXX no error return */ 1691 isa_dmainit(fdc->dmachan, MAX_BYTES_PER_CYL); 1692 } 1693 fdc->fdcu = device_get_unit(dev); 1694 fdc->flags |= FDC_NEEDS_RESET; 1695 1696 mtx_init(&fdc->fdc_mtx, "fdc lock", NULL, MTX_DEF); 1697 1698 /* reset controller, turn motor off, clear fdout mirror reg */ 1699 fdout_wr(fdc, fdc->fdout = 0); 1700 bioq_init(&fdc->head); 1701 1702 kthread_create(fdc_thread, fdc, &fdc->fdc_thread, 0, 0, 1703 "fdc%d", device_get_unit(dev)); 1704 1705 settle = hz / 8; 1706 1707 return (0); 1708 } 1709 1710 int 1711 fdc_hints_probe(device_t dev) 1712 { 1713 const char *name, *dname; 1714 int i, error, dunit; 1715 1716 /* 1717 * Probe and attach any children. We should probably detect 1718 * devices from the BIOS unless overridden. 1719 */ 1720 name = device_get_nameunit(dev); 1721 i = 0; 1722 while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) { 1723 resource_int_value(dname, dunit, "drive", &dunit); 1724 fdc_add_child(dev, dname, dunit); 1725 } 1726 1727 if ((error = bus_generic_attach(dev)) != 0) 1728 return (error); 1729 return (0); 1730 } 1731 1732 int 1733 fdc_print_child(device_t me, device_t child) 1734 { 1735 int retval = 0, flags; 1736 1737 retval += bus_print_child_header(me, child); 1738 retval += printf(" on %s drive %d", device_get_nameunit(me), 1739 fdc_get_fdunit(child)); 1740 if ((flags = device_get_flags(me)) != 0) 1741 retval += printf(" flags %#x", flags); 1742 retval += printf("\n"); 1743 1744 return (retval); 1745 } 1746 1747 /* 1748 * Configuration/initialization, per drive. 1749 */ 1750 static int 1751 fd_probe(device_t dev) 1752 { 1753 int i, unit; 1754 u_int st0, st3; 1755 struct fd_data *fd; 1756 struct fdc_data *fdc; 1757 int fdsu; 1758 int flags, type; 1759 1760 fdsu = fdc_get_fdunit(dev); 1761 fd = device_get_softc(dev); 1762 fdc = device_get_softc(device_get_parent(dev)); 1763 flags = device_get_flags(dev); 1764 1765 fd->dev = dev; 1766 fd->fdc = fdc; 1767 fd->fdsu = fdsu; 1768 unit = device_get_unit(dev); 1769 1770 /* Auto-probe if fdinfo is present, but always allow override. */ 1771 type = flags & FD_TYPEMASK; 1772 if (type == FDT_NONE && (type = fdc_get_fdtype(dev)) != FDT_NONE) { 1773 fd->type = type; 1774 goto done; 1775 } else { 1776 /* make sure fdautoselect() will be called */ 1777 fd->flags = FD_EMPTY; 1778 fd->type = type; 1779 } 1780 1781 /* 1782 * XXX I think using __i386__ is wrong here since we actually want to probe 1783 * for the machine type, not the CPU type (so non-PC arch's like the PC98 will 1784 * fail the probe). However, for whatever reason, testing for _MACHINE_ARCH 1785 * == i386 breaks the test on FreeBSD/Alpha. 1786 */ 1787 #if defined(__i386__) || defined(__amd64__) 1788 if (fd->type == FDT_NONE && (unit == 0 || unit == 1)) { 1789 /* Look up what the BIOS thinks we have. */ 1790 if (unit == 0) { 1791 if ((fdc->flags & FDC_ISPCMCIA)) 1792 /* 1793 * Somewhat special. No need to force the 1794 * user to set device flags, since the Y-E 1795 * Data PCMCIA floppy is always a 1.44 MB 1796 * device. 1797 */ 1798 fd->type = FDT_144M; 1799 else 1800 fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4; 1801 } else { 1802 fd->type = rtcin(RTC_FDISKETTE) & 0x0f; 1803 } 1804 if (fd->type == FDT_288M_1) 1805 fd->type = FDT_288M; 1806 } 1807 #endif /* __i386__ || __amd64__ */ 1808 /* is there a unit? */ 1809 if (fd->type == FDT_NONE) 1810 return (ENXIO); 1811 1812 /* 1813 mtx_lock(&fdc->fdc_mtx); 1814 */ 1815 /* select it */ 1816 fd_select(fd); 1817 fd_motor(fd, 1); 1818 fdc->fd = fd; 1819 fdc_reset(fdc); /* XXX reset, then unreset, etc. */ 1820 DELAY(1000000); /* 1 sec */ 1821 1822 if ((flags & FD_NO_PROBE) == 0) { 1823 /* If we're at track 0 first seek inwards. */ 1824 if ((fdc_sense_drive(fdc, &st3) == 0) && 1825 (st3 & NE7_ST3_T0)) { 1826 /* Seek some steps... */ 1827 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) { 1828 /* ...wait a moment... */ 1829 DELAY(300000); 1830 /* make ctrlr happy: */ 1831 fdc_sense_int(fdc, 0, 0); 1832 } 1833 } 1834 1835 for (i = 0; i < 2; i++) { 1836 /* 1837 * we must recalibrate twice, just in case the 1838 * heads have been beyond cylinder 76, since 1839 * most FDCs still barf when attempting to 1840 * recalibrate more than 77 steps 1841 */ 1842 /* go back to 0: */ 1843 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) { 1844 /* a second being enough for full stroke seek*/ 1845 DELAY(i == 0 ? 1000000 : 300000); 1846 1847 /* anything responding? */ 1848 if (fdc_sense_int(fdc, &st0, 0) == 0 && 1849 (st0 & NE7_ST0_EC) == 0) 1850 break; /* already probed succesfully */ 1851 } 1852 } 1853 } 1854 1855 fd_motor(fd, 0); 1856 fdc->fd = NULL; 1857 /* 1858 mtx_unlock(&fdc->fdc_mtx); 1859 */ 1860 1861 if ((flags & FD_NO_PROBE) == 0 && 1862 (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */ 1863 return (ENXIO); 1864 1865 done: 1866 1867 switch (fd->type) { 1868 case FDT_12M: 1869 device_set_desc(dev, "1200-KB 5.25\" drive"); 1870 break; 1871 case FDT_144M: 1872 device_set_desc(dev, "1440-KB 3.5\" drive"); 1873 break; 1874 case FDT_288M: 1875 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)"); 1876 break; 1877 case FDT_360K: 1878 device_set_desc(dev, "360-KB 5.25\" drive"); 1879 break; 1880 case FDT_720K: 1881 device_set_desc(dev, "720-KB 3.5\" drive"); 1882 break; 1883 default: 1884 return (ENXIO); 1885 } 1886 fd->track = FD_NO_TRACK; 1887 fd->fdc = fdc; 1888 fd->fdsu = fdsu; 1889 fd->options = 0; 1890 callout_init(&fd->toffhandle, 1); 1891 callout_init(&fd->tohandle, 1); 1892 1893 /* initialize densities for subdevices */ 1894 fdsettype(fd, fd_native_types[fd->type]); 1895 return (0); 1896 } 1897 1898 /* 1899 * We have to do this in a geom event because GEOM is not running 1900 * when fd_attach() is. 1901 * XXX: move fd_attach after geom like ata/scsi disks 1902 */ 1903 static void 1904 fd_attach2(void *arg, int flag) 1905 { 1906 struct fd_data *fd; 1907 1908 fd = arg; 1909 1910 fd->fd_geom = g_new_geomf(&g_fd_class, 1911 "fd%d", device_get_unit(fd->fdc->fdc_dev)); 1912 fd->fd_provider = g_new_providerf(fd->fd_geom, fd->fd_geom->name); 1913 fd->fd_geom->softc = fd; 1914 g_error_provider(fd->fd_provider, 0); 1915 } 1916 1917 static int 1918 fd_attach(device_t dev) 1919 { 1920 struct fd_data *fd; 1921 1922 fd = device_get_softc(dev); 1923 g_post_event(fd_attach2, fd, M_WAITOK, NULL); 1924 fd->flags |= FD_EMPTY; 1925 bioq_init(&fd->fd_bq); 1926 return (0); 1927 1928 return (0); 1929 } 1930 1931 static int 1932 fd_detach(device_t dev) 1933 { 1934 struct fd_data *fd; 1935 1936 fd = device_get_softc(dev); 1937 callout_drain(&fd->toffhandle); 1938 1939 return (0); 1940 } 1941 1942 static device_method_t fd_methods[] = { 1943 /* Device interface */ 1944 DEVMETHOD(device_probe, fd_probe), 1945 DEVMETHOD(device_attach, fd_attach), 1946 DEVMETHOD(device_detach, fd_detach), 1947 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1948 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */ 1949 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */ 1950 { 0, 0 } 1951 }; 1952 1953 static driver_t fd_driver = { 1954 "fd", 1955 fd_methods, 1956 sizeof(struct fd_data) 1957 }; 1958 1959 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0); 1960