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