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

Cache object: 47050fe4ed5a6a4f2b86f454ba2a8751


[ 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.