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