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

Cache object: 99a8b2a9ca11c5b7b5f594f977488b81


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