The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/fdc/fdc.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 8edd65535e26dd4e5f6eb32bf114374b


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.