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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
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/6.3/sys/dev/fdc/fdc.c 172172 2007-09-14 23:44:11Z jkim $");
   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 
 1377         fd = pp->geom->softc;
 1378         fdc = fd->fdc;
 1379 
 1380         /*
 1381          * If our provider is withering, we can only get negative requests
 1382          * and we don't want to even see them
 1383          */
 1384         if (pp->flags & G_PF_WITHER)
 1385                 return (0);
 1386 
 1387         ar = r + pp->acr;
 1388         aw = w + pp->acw;
 1389         ae = e + pp->ace;
 1390 
 1391         if (ar == 0 && aw == 0 && ae == 0) {
 1392                 device_unbusy(fd->dev);
 1393                 return (0);
 1394         }
 1395 
 1396         if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) {
 1397                 if (fdmisccmd(fd, BIO_PROBE, NULL))
 1398                         return (ENXIO);
 1399                 if (fd->flags & FD_EMPTY)
 1400                         return (ENXIO);
 1401                 if (fd->flags & FD_NEWDISK) {
 1402                         fdautoselect(fd);
 1403                         mtx_lock(&fdc->fdc_mtx);
 1404                         fd->flags &= ~FD_NEWDISK;
 1405                         mtx_unlock(&fdc->fdc_mtx);
 1406                 }
 1407                 device_busy(fd->dev);
 1408         }
 1409 
 1410         if (w > 0 && (fd->flags & FD_WP))
 1411                 return (EROFS);
 1412 
 1413         pp->sectorsize = fd->sectorsize;
 1414         pp->stripesize = fd->ft->heads * fd->ft->sectrac * fd->sectorsize;
 1415         pp->mediasize = pp->stripesize * fd->ft->tracks;
 1416         return (0);
 1417 }
 1418 
 1419 static void
 1420 fd_start(struct bio *bp)
 1421 {
 1422         struct fdc_data *       fdc;
 1423         struct fd_data *        fd;
 1424 
 1425         fd = bp->bio_to->geom->softc;
 1426         fdc = fd->fdc;
 1427         bp->bio_driver1 = fd;
 1428         if (bp->bio_cmd & BIO_GETATTR) {
 1429                 if (g_handleattr_int(bp, "GEOM::fwsectors", fd->ft->sectrac))
 1430                         return;
 1431                 if (g_handleattr_int(bp, "GEOM::fwheads", fd->ft->heads))
 1432                         return;
 1433                 g_io_deliver(bp, ENOIOCTL);
 1434                 return;
 1435         }
 1436         if (!(bp->bio_cmd & (BIO_READ|BIO_WRITE))) {
 1437                 g_io_deliver(bp, EOPNOTSUPP);
 1438                 return;
 1439         }
 1440         bp->bio_pblkno = bp->bio_offset / fd->sectorsize;
 1441         bp->bio_resid = bp->bio_length;
 1442         fd_enqueue(fd, bp);
 1443         return;
 1444 }
 1445 
 1446 static int
 1447 fd_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
 1448 {
 1449         struct fd_data *fd;
 1450         struct fdc_status *fsp;
 1451         struct fdc_readid *rid;
 1452         int error;
 1453 
 1454         fd = pp->geom->softc;
 1455 
 1456         switch (cmd) {
 1457         case FD_GTYPE:                  /* get drive type */
 1458                 *(struct fd_type *)data = *fd->ft;
 1459                 return (0);
 1460 
 1461         case FD_STYPE:                  /* set drive type */
 1462                 if (!(fflag & FWRITE))
 1463                         return (EPERM);
 1464                 /*
 1465                  * Allow setting drive type temporarily iff
 1466                  * currently unset.  Used for fdformat so any
 1467                  * user can set it, and then start formatting.
 1468                  */
 1469                 fd->fts = *(struct fd_type *)data;
 1470                 if (fd->fts.sectrac) {
 1471                         /* XXX: check for rubbish */
 1472                         fdsettype(fd, &fd->fts);
 1473                 } else {
 1474                         fdsettype(fd, fd_native_types[fd->type]);
 1475                 }
 1476                 if (debugflags & 0x40)
 1477                         fdprinttype(fd->ft);
 1478                 return (0);
 1479 
 1480         case FD_GOPTS:                  /* get drive options */
 1481                 *(int *)data = fd->options;
 1482                 return (0);
 1483 
 1484         case FD_SOPTS:                  /* set drive options */
 1485                 if (!(fflag & FWRITE))
 1486                         return (EPERM);
 1487                 fd->options = *(int *)data;
 1488                 return (0);
 1489 
 1490         case FD_CLRERR:
 1491                 if (suser(td) != 0)
 1492                         return (EPERM);
 1493                 fd->fdc->fdc_errs = 0;
 1494                 return (0);
 1495 
 1496         case FD_GSTAT:
 1497                 fsp = (struct fdc_status *)data;
 1498                 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
 1499                         return (EINVAL);
 1500                 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
 1501                 return (0);
 1502 
 1503         case FD_GDTYPE:
 1504                 *(enum fd_drivetype *)data = fd->type;
 1505                 return (0);
 1506 
 1507         case FD_FORM:
 1508                 if (!(fflag & FWRITE))
 1509                         return (EPERM);
 1510                 if (((struct fd_formb *)data)->format_version !=
 1511                     FD_FORMAT_VERSION)
 1512                         return (EINVAL); /* wrong version of formatting prog */
 1513                 error = fdmisccmd(fd, BIO_FMT, data);
 1514                 mtx_lock(&fd->fdc->fdc_mtx);
 1515                 fd->flags |= FD_NEWDISK;
 1516                 mtx_unlock(&fd->fdc->fdc_mtx);
 1517                 break;
 1518 
 1519         case FD_READID:
 1520                 rid = (struct fdc_readid *)data;
 1521                 if (rid->cyl > 85 || rid->head > 1)
 1522                         return (EINVAL);
 1523                 error = fdmisccmd(fd, BIO_RDID, data);
 1524                 break;
 1525 
 1526         case FIONBIO:
 1527         case FIOASYNC:
 1528                 /* For backwards compat with old fd*(8) tools */
 1529                 error = 0;
 1530                 break;
 1531 
 1532         default:
 1533                 if (debugflags & 0x80)
 1534                         printf("Unknown ioctl %lx\n", cmd);
 1535                 error = ENOIOCTL;
 1536                 break;
 1537         }
 1538         return (error);
 1539 };
 1540 
 1541 
 1542 
 1543 /*
 1544  * Configuration/initialization stuff, per controller.
 1545  */
 1546 
 1547 devclass_t fdc_devclass;
 1548 static devclass_t fd_devclass;
 1549 
 1550 struct fdc_ivars {
 1551         int     fdunit;
 1552         int     fdtype;
 1553 };
 1554 
 1555 void
 1556 fdc_release_resources(struct fdc_data *fdc)
 1557 {
 1558         device_t dev;
 1559         struct resource *last;
 1560         int i;
 1561 
 1562         dev = fdc->fdc_dev;
 1563         if (fdc->fdc_intr)
 1564                 bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
 1565         fdc->fdc_intr = NULL;
 1566         if (fdc->res_irq != NULL)
 1567                 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
 1568                     fdc->res_irq);
 1569         fdc->res_irq = NULL;
 1570         last = NULL;
 1571         for (i = 0; i < FDC_MAXREG; i++) {
 1572                 if (fdc->resio[i] != NULL && fdc->resio[i] != last) {
 1573                         bus_release_resource(dev, SYS_RES_IOPORT,
 1574                             fdc->ridio[i], fdc->resio[i]);
 1575                         last = fdc->resio[i];
 1576                         fdc->resio[i] = NULL;
 1577                 }
 1578         }
 1579         if (fdc->res_drq != NULL)
 1580                 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
 1581                     fdc->res_drq);
 1582         fdc->res_drq = NULL;
 1583 }
 1584 
 1585 int
 1586 fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
 1587 {
 1588         struct fdc_ivars *ivars = device_get_ivars(child);
 1589 
 1590         switch (which) {
 1591         case FDC_IVAR_FDUNIT:
 1592                 *result = ivars->fdunit;
 1593                 break;
 1594         case FDC_IVAR_FDTYPE:
 1595                 *result = ivars->fdtype;
 1596                 break;
 1597         default:
 1598                 return (ENOENT);
 1599         }
 1600         return (0);
 1601 }
 1602 
 1603 int
 1604 fdc_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
 1605 {
 1606         struct fdc_ivars *ivars = device_get_ivars(child);
 1607 
 1608         switch (which) {
 1609         case FDC_IVAR_FDUNIT:
 1610                 ivars->fdunit = value;
 1611                 break;
 1612         case FDC_IVAR_FDTYPE:
 1613                 ivars->fdtype = value;
 1614                 break;
 1615         default:
 1616                 return (ENOENT);
 1617         }
 1618         return (0);
 1619 }
 1620 
 1621 int
 1622 fdc_initial_reset(device_t dev, struct fdc_data *fdc)
 1623 {
 1624         int ic_type, part_id;
 1625 
 1626         /*
 1627          * A status value of 0xff is very unlikely, but not theoretically
 1628          * impossible, but it is far more likely to indicate an empty bus.
 1629          */
 1630         if (fdsts_rd(fdc) == 0xff)
 1631                 return (ENXIO);
 1632 
 1633         /*
 1634          * Assert a reset to the floppy controller and check that the status
 1635          * register goes to zero.
 1636          */
 1637         fdout_wr(fdc, 0);
 1638         fdout_wr(fdc, 0);
 1639         if (fdsts_rd(fdc) != 0)
 1640                 return (ENXIO);
 1641 
 1642         /*
 1643          * Clear the reset and see it come ready.
 1644          */
 1645         fdout_wr(fdc, FDO_FRST);
 1646         DELAY(100);
 1647         if (fdsts_rd(fdc) != 0x80)
 1648                 return (ENXIO);
 1649 
 1650         /* Then, see if it can handle a command. */
 1651         if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, 0xaf, 0x1e, 0))
 1652                 return (ENXIO);
 1653 
 1654         /*
 1655          * Try to identify the chip.
 1656          *
 1657          * The i8272 datasheet documents that unknown commands
 1658          * will return ST0 as 0x80.  The i8272 is supposedly identical
 1659          * to the NEC765.
 1660          * The i82077SL datasheet says 0x90 for the VERSION command,
 1661          * and several "superio" chips emulate this.
 1662          */
 1663         if (fdc_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type))
 1664                 return (ENXIO);
 1665         if (fdc_cmd(fdc, 1, 0x18, 1, &part_id))
 1666                 return (ENXIO);
 1667         if (bootverbose)
 1668                 device_printf(dev,
 1669                     "ic_type %02x part_id %02x\n", ic_type, part_id);
 1670         switch (ic_type & 0xff) {
 1671         case 0x80:
 1672                 device_set_desc(dev, "NEC 765 or clone");
 1673                 fdc->fdct = FDC_NE765;
 1674                 break;
 1675         case 0x81:
 1676         case 0x90:
 1677                 device_set_desc(dev,
 1678                     "Enhanced floppy controller");
 1679                 fdc->fdct = FDC_ENHANCED;
 1680                 break;
 1681         default:
 1682                 device_set_desc(dev, "Generic floppy controller");
 1683                 fdc->fdct = FDC_UNKNOWN;
 1684                 break;
 1685         }
 1686         return (0);
 1687 }
 1688 
 1689 int
 1690 fdc_detach(device_t dev)
 1691 {
 1692         struct  fdc_data *fdc;
 1693         int     error;
 1694 
 1695         fdc = device_get_softc(dev);
 1696 
 1697         /* have our children detached first */
 1698         if ((error = bus_generic_detach(dev)))
 1699                 return (error);
 1700 
 1701         /* kill worker thread */
 1702         mtx_lock(&fdc->fdc_mtx);
 1703         fdc->flags |= FDC_KTHREAD_EXIT;
 1704         wakeup(&fdc->head);
 1705         while ((fdc->flags & FDC_KTHREAD_ALIVE) != 0)
 1706                 msleep(&fdc->fdc_thread, &fdc->fdc_mtx, PRIBIO, "fdcdet", 0);
 1707         mtx_unlock(&fdc->fdc_mtx);
 1708 
 1709         /* reset controller, turn motor off */
 1710         fdout_wr(fdc, 0);
 1711 
 1712         if (!(fdc->flags & FDC_NODMA))
 1713                 isa_dma_release(fdc->dmachan);
 1714         fdc_release_resources(fdc);
 1715         mtx_destroy(&fdc->fdc_mtx);
 1716         return (0);
 1717 }
 1718 
 1719 /*
 1720  * Add a child device to the fdc controller.  It will then be probed etc.
 1721  */
 1722 device_t
 1723 fdc_add_child(device_t dev, const char *name, int unit)
 1724 {
 1725         struct fdc_ivars *ivar;
 1726         device_t child;
 1727 
 1728         ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
 1729         if (ivar == NULL)
 1730                 return (NULL);
 1731         child = device_add_child(dev, name, unit);
 1732         if (child == NULL) {
 1733                 free(ivar, M_DEVBUF);
 1734                 return (NULL);
 1735         }
 1736         device_set_ivars(child, ivar);
 1737         ivar->fdunit = unit;
 1738         ivar->fdtype = FDT_NONE;
 1739         if (resource_disabled(name, unit))
 1740                 device_disable(child);
 1741         return (child);
 1742 }
 1743 
 1744 int
 1745 fdc_attach(device_t dev)
 1746 {
 1747         struct  fdc_data *fdc;
 1748         int     error;
 1749 
 1750         fdc = device_get_softc(dev);
 1751         fdc->fdc_dev = dev;
 1752         error = fdc_initial_reset(dev, fdc);
 1753         if (error) {
 1754                 device_printf(dev, "does not respond\n");
 1755                 return (error);
 1756         }
 1757         error = bus_setup_intr(dev, fdc->res_irq,
 1758             INTR_TYPE_BIO | INTR_ENTROPY | INTR_MPSAFE |
 1759             ((fdc->flags & FDC_NOFAST) ? 0 : INTR_FAST),
 1760             fdc_intr, fdc, &fdc->fdc_intr);
 1761         if (error) {
 1762                 device_printf(dev, "cannot setup interrupt\n");
 1763                 return (error);
 1764         }
 1765         if (!(fdc->flags & FDC_NODMA)) {
 1766                 error = isa_dma_acquire(fdc->dmachan);
 1767                 if (!error) {
 1768                         error = isa_dma_init(fdc->dmachan,
 1769                             MAX_BYTES_PER_CYL, M_WAITOK);
 1770                         if (error)
 1771                                 isa_dma_release(fdc->dmachan);
 1772                 }
 1773                 if (error)
 1774                         return (error);
 1775         }
 1776         fdc->fdcu = device_get_unit(dev);
 1777         fdc->flags |= FDC_NEEDS_RESET;
 1778 
 1779         mtx_init(&fdc->fdc_mtx, "fdc lock", NULL, MTX_DEF);
 1780 
 1781         /* reset controller, turn motor off, clear fdout mirror reg */
 1782         fdout_wr(fdc, fdc->fdout = 0);
 1783         bioq_init(&fdc->head);
 1784 
 1785         kthread_create(fdc_thread, fdc, &fdc->fdc_thread, 0, 0,
 1786             "fdc%d", device_get_unit(dev));
 1787 
 1788         settle = hz / 8;
 1789 
 1790         return (0);
 1791 }
 1792 
 1793 int
 1794 fdc_hints_probe(device_t dev)
 1795 {
 1796         const char *name, *dname;
 1797         int i, error, dunit;
 1798 
 1799         /*
 1800          * Probe and attach any children.  We should probably detect
 1801          * devices from the BIOS unless overridden.
 1802          */
 1803         name = device_get_nameunit(dev);
 1804         i = 0;
 1805         while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) {
 1806                 resource_int_value(dname, dunit, "drive", &dunit);
 1807                 fdc_add_child(dev, dname, dunit);
 1808         }
 1809 
 1810         if ((error = bus_generic_attach(dev)) != 0)
 1811                 return (error);
 1812         return (0);
 1813 }
 1814 
 1815 int
 1816 fdc_print_child(device_t me, device_t child)
 1817 {
 1818         int retval = 0, flags;
 1819 
 1820         retval += bus_print_child_header(me, child);
 1821         retval += printf(" on %s drive %d", device_get_nameunit(me),
 1822                fdc_get_fdunit(child));
 1823         if ((flags = device_get_flags(me)) != 0)
 1824                 retval += printf(" flags %#x", flags);
 1825         retval += printf("\n");
 1826 
 1827         return (retval);
 1828 }
 1829 
 1830 /*
 1831  * Configuration/initialization, per drive.
 1832  */
 1833 static int
 1834 fd_probe(device_t dev)
 1835 {
 1836         int     i, unit;
 1837         u_int   st0, st3;
 1838         struct  fd_data *fd;
 1839         struct  fdc_data *fdc;
 1840         int     fdsu;
 1841         int     flags, type;
 1842 
 1843         fdsu = fdc_get_fdunit(dev);
 1844         fd = device_get_softc(dev);
 1845         fdc = device_get_softc(device_get_parent(dev));
 1846         flags = device_get_flags(dev);
 1847 
 1848         fd->dev = dev;
 1849         fd->fdc = fdc;
 1850         fd->fdsu = fdsu;
 1851         unit = device_get_unit(dev);
 1852 
 1853         /* Auto-probe if fdinfo is present, but always allow override. */
 1854         type = flags & FD_TYPEMASK;
 1855         if (type == FDT_NONE && (type = fdc_get_fdtype(dev)) != FDT_NONE) {
 1856                 fd->type = type;
 1857                 goto done;
 1858         } else {
 1859                 /* make sure fdautoselect() will be called */
 1860                 fd->flags = FD_EMPTY;
 1861                 fd->type = type;
 1862         }
 1863 
 1864 #if (defined(__i386__) && !defined(PC98)) || defined(__amd64__)
 1865         if (fd->type == FDT_NONE && (unit == 0 || unit == 1)) {
 1866                 /* Look up what the BIOS thinks we have. */
 1867                 if (unit == 0)
 1868                         fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
 1869                 else
 1870                         fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
 1871                 if (fd->type == FDT_288M_1)
 1872                         fd->type = FDT_288M;
 1873         }
 1874 #endif /* __i386__ || __amd64__ */
 1875         /* is there a unit? */
 1876         if (fd->type == FDT_NONE)
 1877                 return (ENXIO);
 1878 
 1879 /*
 1880         mtx_lock(&fdc->fdc_mtx);
 1881 */
 1882         /* select it */
 1883         fd_select(fd);
 1884         fd_motor(fd, 1);
 1885         fdc->fd = fd;
 1886         fdc_reset(fdc);         /* XXX reset, then unreset, etc. */
 1887         DELAY(1000000); /* 1 sec */
 1888 
 1889         if ((flags & FD_NO_PROBE) == 0) {
 1890                 /* If we're at track 0 first seek inwards. */
 1891                 if ((fdc_sense_drive(fdc, &st3) == 0) &&
 1892                     (st3 & NE7_ST3_T0)) {
 1893                         /* Seek some steps... */
 1894                         if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
 1895                                 /* ...wait a moment... */
 1896                                 DELAY(300000);
 1897                                 /* make ctrlr happy: */
 1898                                 fdc_sense_int(fdc, NULL, NULL);
 1899                         }
 1900                 }
 1901 
 1902                 for (i = 0; i < 2; i++) {
 1903                         /*
 1904                          * we must recalibrate twice, just in case the
 1905                          * heads have been beyond cylinder 76, since
 1906                          * most FDCs still barf when attempting to
 1907                          * recalibrate more than 77 steps
 1908                          */
 1909                         /* go back to 0: */
 1910                         if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
 1911                                 /* a second being enough for full stroke seek*/
 1912                                 DELAY(i == 0 ? 1000000 : 300000);
 1913 
 1914                                 /* anything responding? */
 1915                                 if (fdc_sense_int(fdc, &st0, NULL) == 0 &&
 1916                                     (st0 & NE7_ST0_EC) == 0)
 1917                                         break; /* already probed succesfully */
 1918                         }
 1919                 }
 1920         }
 1921 
 1922         fd_motor(fd, 0);
 1923         fdc->fd = NULL;
 1924 /*
 1925         mtx_unlock(&fdc->fdc_mtx);
 1926 */
 1927 
 1928         if ((flags & FD_NO_PROBE) == 0 &&
 1929             (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
 1930                 return (ENXIO);
 1931 
 1932 done:
 1933 
 1934         switch (fd->type) {
 1935         case FDT_12M:
 1936                 device_set_desc(dev, "1200-KB 5.25\" drive");
 1937                 break;
 1938         case FDT_144M:
 1939                 device_set_desc(dev, "1440-KB 3.5\" drive");
 1940                 break;
 1941         case FDT_288M:
 1942                 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
 1943                 break;
 1944         case FDT_360K:
 1945                 device_set_desc(dev, "360-KB 5.25\" drive");
 1946                 break;
 1947         case FDT_720K:
 1948                 device_set_desc(dev, "720-KB 3.5\" drive");
 1949                 break;
 1950         default:
 1951                 return (ENXIO);
 1952         }
 1953         fd->track = FD_NO_TRACK;
 1954         fd->fdc = fdc;
 1955         fd->fdsu = fdsu;
 1956         fd->options = 0;
 1957         callout_init_mtx(&fd->toffhandle, &fd->fdc->fdc_mtx, 0);
 1958 
 1959         /* initialize densities for subdevices */
 1960         fdsettype(fd, fd_native_types[fd->type]);
 1961         return (0);
 1962 }
 1963 
 1964 /*
 1965  * We have to do this in a geom event because GEOM is not running
 1966  * when fd_attach() is.
 1967  * XXX: move fd_attach after geom like ata/scsi disks
 1968  */
 1969 static void
 1970 fd_attach2(void *arg, int flag)
 1971 {
 1972         struct  fd_data *fd;
 1973 
 1974         fd = arg;
 1975 
 1976         fd->fd_geom = g_new_geomf(&g_fd_class,
 1977             "fd%d", device_get_unit(fd->dev));
 1978         fd->fd_provider = g_new_providerf(fd->fd_geom, fd->fd_geom->name);
 1979         fd->fd_geom->softc = fd;
 1980         g_error_provider(fd->fd_provider, 0);
 1981 }
 1982 
 1983 static int
 1984 fd_attach(device_t dev)
 1985 {
 1986         struct  fd_data *fd;
 1987 
 1988         fd = device_get_softc(dev);
 1989         g_post_event(fd_attach2, fd, M_WAITOK, NULL);
 1990         fd->flags |= FD_EMPTY;
 1991         bioq_init(&fd->fd_bq);
 1992 
 1993         return (0);
 1994 }
 1995 
 1996 static int
 1997 fd_detach(device_t dev)
 1998 {
 1999         struct  fd_data *fd;
 2000 
 2001         fd = device_get_softc(dev);
 2002         g_topology_lock();
 2003         g_wither_geom(fd->fd_geom, ENXIO);
 2004         g_topology_unlock();
 2005         while (device_get_state(dev) == DS_BUSY)
 2006                 tsleep(fd, PZERO, "fdd", hz/10);
 2007         callout_drain(&fd->toffhandle);
 2008 
 2009         return (0);
 2010 }
 2011 
 2012 static device_method_t fd_methods[] = {
 2013         /* Device interface */
 2014         DEVMETHOD(device_probe,         fd_probe),
 2015         DEVMETHOD(device_attach,        fd_attach),
 2016         DEVMETHOD(device_detach,        fd_detach),
 2017         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
 2018         DEVMETHOD(device_suspend,       bus_generic_suspend), /* XXX */
 2019         DEVMETHOD(device_resume,        bus_generic_resume), /* XXX */
 2020         { 0, 0 }
 2021 };
 2022 
 2023 static driver_t fd_driver = {
 2024         "fd",
 2025         fd_methods,
 2026         sizeof(struct fd_data)
 2027 };
 2028 
 2029 static int
 2030 fdc_modevent(module_t mod, int type, void *data)
 2031 {
 2032 
 2033         g_modevent(NULL, type, &g_fd_class);
 2034         return (0);
 2035 }
 2036 
 2037 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, fdc_modevent, 0);

Cache object: 58cef27bc0c6c96285f5559ac3895083


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