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.3/sys/dev/fdc/fdc.c 136361 2004-10-10 23:49:38Z peadar $");
   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                         return fdc_err(fdc, msg);
  470                 }
  471         }
  472         n_in = va_arg(ap, int);
  473         for (n = 0; n < n_in; n++) {
  474                 int *ptr = va_arg(ap, int *);
  475                 if (fdc_in(fdc, ptr) < 0) {
  476                         char msg[50];
  477                         snprintf(msg, sizeof(msg),
  478                                 "cmd %02x failed at in byte %d of %d\n",
  479                                 cmd, n + 1, n_in);
  480                         fdc->flags |= FDC_NEEDS_RESET;
  481                         return fdc_err(fdc, msg);
  482                 }
  483         }
  484         return (0);
  485 }
  486 
  487 static void
  488 fdc_reset(struct fdc_data *fdc)
  489 {
  490         int i, r[10];
  491 
  492         /* Try a reset, keep motor on */
  493         fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
  494         DELAY(100);
  495         /* enable FDC, but defer interrupts a moment */
  496         fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
  497         DELAY(100);
  498         fdout_wr(fdc, fdc->fdout);
  499 
  500         /* XXX after a reset, silently believe the FDC will accept commands */
  501         if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, spec1, spec2, 0))
  502                 device_printf(fdc->fdc_dev, " SPECIFY failed in reset\n");
  503 
  504         if (fdc->fdct == FDC_ENHANCED) {
  505                 if (fdc_cmd(fdc, 4,
  506                     I8207X_CONFIGURE,
  507                     0,
  508                     0x40 |                      /* Enable Implied Seek */
  509                     0x10 |                      /* Polling disabled */
  510                     (fifo_threshold - 1),       /* Fifo threshold */
  511                     0x00,                       /* Precomp track */
  512                     0))
  513                         device_printf(fdc->fdc_dev,
  514                             " CONFIGURE failed in reset\n");
  515                 if (debugflags & 1) {
  516                         if (fdc_cmd(fdc, 1,
  517                             0x0e,                       /* DUMPREG */
  518                             10, &r[0], &r[1], &r[2], &r[3], &r[4],
  519                             &r[5], &r[6], &r[7], &r[8], &r[9]))
  520                                 device_printf(fdc->fdc_dev,
  521                                     " DUMPREG failed in reset\n");
  522                         for (i = 0; i < 10; i++)
  523                                 printf(" %02x", r[i]);
  524                         printf("\n");
  525                 }
  526         }
  527 }
  528 
  529 static int
  530 fdc_sense_drive(struct fdc_data *fdc, int *st3p)
  531 {
  532         int st3;
  533 
  534         if (fdc_cmd(fdc, 2, NE7CMD_SENSED, fdc->fd->fdsu, 1, &st3))
  535                 return (fdc_err(fdc, "Sense Drive Status failed\n"));
  536         if (st3p)
  537                 *st3p = st3;
  538         return (0);
  539 }
  540 
  541 static int
  542 fdc_sense_int(struct fdc_data *fdc, int *st0p, int *cylp)
  543 {
  544         int cyl, st0, ret;
  545 
  546         ret = fdc_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
  547         if (ret) {
  548                 (void)fdc_err(fdc, "sense intr err reading stat reg 0\n");
  549                 return (ret);
  550         }
  551 
  552         if (st0p)
  553                 *st0p = st0;
  554 
  555         if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
  556                 /*
  557                  * There doesn't seem to have been an interrupt.
  558                  */
  559                 return (FD_NOT_VALID);
  560         }
  561 
  562         if (fdc_in(fdc, &cyl) < 0)
  563                 return fdc_err(fdc, "can't get cyl num\n");
  564 
  565         if (cylp)
  566                 *cylp = cyl;
  567 
  568         return (0);
  569 }
  570 
  571 static int
  572 fdc_read_status(struct fdc_data *fdc)
  573 {
  574         int i, ret, status;
  575 
  576         for (i = ret = 0; i < 7; i++) {
  577                 ret = fdc_in(fdc, &status);
  578                 fdc->status[i] = status;
  579                 if (ret != 0)
  580                         break;
  581         }
  582 
  583         if (ret == 0)
  584                 fdc->flags |= FDC_STAT_VALID;
  585         else
  586                 fdc->flags &= ~FDC_STAT_VALID;
  587 
  588         return ret;
  589 }
  590 
  591 /*
  592  * Select this drive
  593  */
  594 static void
  595 fd_select(struct fd_data *fd)
  596 {
  597         struct fdc_data *fdc;
  598 
  599         /* XXX: lock controller */
  600         fdc = fd->fdc;
  601         fdc->fdout &= ~FDO_FDSEL;
  602         fdc->fdout |= FDO_FDMAEN | FDO_FRST | fd->fdsu;
  603         fdout_wr(fdc, fdc->fdout);
  604 }
  605 
  606 static void
  607 fd_turnon(void *arg)
  608 {
  609         struct fd_data *fd;
  610         struct bio *bp;
  611         int once;
  612 
  613         fd = arg;
  614         mtx_lock(&fd->fdc->fdc_mtx);
  615         fd->flags &= ~FD_MOTORWAIT;
  616         fd->flags |= FD_MOTOR;
  617         once = 0;
  618         for (;;) {
  619                 bp = bioq_takefirst(&fd->fd_bq);
  620                 if (bp == NULL)
  621                         break;
  622                 bioq_disksort(&fd->fdc->head, bp);
  623                 once = 1;
  624         }
  625         mtx_unlock(&fd->fdc->fdc_mtx);
  626         if (once)
  627                 wakeup(&fd->fdc->head);
  628 }
  629 
  630 static void
  631 fd_motor(struct fd_data *fd, int turnon)
  632 {
  633         struct fdc_data *fdc;
  634 
  635         fdc = fd->fdc;
  636 /*
  637         mtx_assert(&fdc->fdc_mtx, MA_OWNED);
  638 */
  639         if (turnon) {
  640                 fd->flags |= FD_MOTORWAIT;
  641                 fdc->fdout |= (FDO_MOEN0 << fd->fdsu);
  642                 callout_reset(&fd->toffhandle, hz, fd_turnon, fd);
  643         } else {
  644                 callout_drain(&fd->toffhandle);
  645                 fd->flags &= ~(FD_MOTOR|FD_MOTORWAIT);
  646                 fdc->fdout &= ~(FDO_MOEN0 << fd->fdsu);
  647         }
  648         fdout_wr(fdc, fdc->fdout);
  649 }
  650 
  651 static void
  652 fd_turnoff(void *xfd)
  653 {
  654         struct fd_data *fd = xfd;
  655 
  656         mtx_lock(&fd->fdc->fdc_mtx);
  657         fd_motor(fd, 0);
  658         mtx_unlock(&fd->fdc->fdc_mtx);
  659 }
  660 
  661 /*
  662  * fdc_intr
  663  *
  664  * Keep calling the state machine until it returns a 0.
  665  */
  666 static void
  667 fdc_intr(void *arg)
  668 {
  669 
  670         wakeup(arg);
  671 }
  672 
  673 /*
  674  * fdc_pio(): perform programmed IO read/write for YE PCMCIA floppy.
  675  */
  676 static void
  677 fdc_pio(struct fdc_data *fdc)
  678 {
  679         u_char *cptr;
  680         struct bio *bp;
  681         u_int count;
  682 
  683         bp = fdc->bp;
  684         cptr = fdc->fd->fd_ioptr;
  685         count = fdc->fd->fd_iosize;
  686 
  687         if (bp->bio_cmd == BIO_READ) {
  688                 fdbcdr_wr(fdc, 0, count);
  689                 bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off +
  690                     FD_YE_DATAPORT, cptr, count);
  691         } else {
  692                 bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off +
  693                     FD_YE_DATAPORT, cptr, count);
  694                 fdbcdr_wr(fdc, 0, count);       /* needed? */
  695         }
  696 }
  697 
  698 static int
  699 fdc_biodone(struct fdc_data *fdc, int error)
  700 {
  701         struct fd_data *fd;
  702         struct bio *bp;
  703 
  704         fd = fdc->fd;
  705         bp = fdc->bp;
  706 
  707         mtx_lock(&fdc->fdc_mtx);
  708         if (--fd->fd_iocount == 0)
  709                 callout_reset(&fd->toffhandle, 4 * hz, fd_turnoff, fd);
  710         fdc->bp = NULL;
  711         fdc->fd = NULL;
  712         mtx_unlock(&fdc->fdc_mtx);
  713         if (bp->bio_to != NULL) {
  714                 if ((debugflags & 2) && fd->fdc->retry > 0)
  715                         printf("retries: %d\n", fd->fdc->retry);
  716                 g_io_deliver(bp, error);
  717                 return (0);
  718         }
  719         bp->bio_error = error;
  720         bp->bio_flags |= BIO_DONE;
  721         wakeup(bp);
  722         return (0);
  723 }
  724 
  725 static int retry_line;
  726 
  727 static int
  728 fdc_worker(struct fdc_data *fdc)
  729 {
  730         struct fd_data *fd;
  731         struct bio *bp;
  732         int i, nsect;
  733         int st0, st3, cyl, mfm, steptrac, cylinder, descyl, sec;
  734         int head;
  735         static int need_recal;
  736         struct fdc_readid *idp;
  737         struct fd_formb *finfo;
  738 
  739         /* Have we exhausted our retries ? */
  740         bp = fdc->bp;
  741         fd = fdc->fd;
  742         if (bp != NULL &&
  743                 (fdc->retry >= retries || (fd->flags & FDOPT_NORETRY))) {
  744                 if ((debugflags & 4))
  745                         printf("Too many retries (EIO)\n");
  746                 return (fdc_biodone(fdc, EIO));
  747         }
  748 
  749         /* Disable ISADMA if we bailed while it was active */
  750         if (fd != NULL && (fd->flags & FD_ISADMA)) {
  751                 mtx_lock(&Giant);
  752                 isa_dmadone(
  753                     bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
  754                     fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
  755                 mtx_unlock(&Giant);
  756                 fd->flags &= ~FD_ISADMA;
  757         }
  758                 
  759         /* Unwedge the controller ? */
  760         if (fdc->flags & FDC_NEEDS_RESET) {
  761                 fdc->flags &= ~FDC_NEEDS_RESET;
  762                 fdc_reset(fdc);
  763                 msleep(fdc, NULL, PRIBIO, "fdcrst", hz);
  764                 /* Discard results */
  765                 for (i = 0; i < 4; i++)
  766                         fdc_sense_int(fdc, &st0, &cyl);
  767                 /* All drives must recal */
  768                 need_recal = 0xf;
  769         }
  770 
  771         /* Pick up a request, if need be wait for it */
  772         if (fdc->bp == NULL) {
  773                 mtx_lock(&fdc->fdc_mtx);
  774                 do {
  775                         fdc->bp = bioq_takefirst(&fdc->head);
  776                         if (fdc->bp == NULL)
  777                                 msleep(&fdc->head, &fdc->fdc_mtx,
  778                                     PRIBIO, "-", hz);
  779                 } while (fdc->bp == NULL);
  780                 mtx_unlock(&fdc->fdc_mtx);
  781                 bp = fdc->bp;
  782                 fd = fdc->fd = bp->bio_driver1;
  783                 fdc->retry = 0;
  784                 fd->fd_ioptr = bp->bio_data;
  785                 if (bp->bio_cmd & BIO_FMT) {
  786                         i = offsetof(struct fd_formb, fd_formb_cylno(0));
  787                         fd->fd_ioptr += i;
  788                         fd->fd_iosize = bp->bio_length - i;
  789                 }
  790         }
  791 
  792         /* Select drive, setup params */
  793         fd_select(fd);
  794         fdctl_wr(fdc, fd->ft->trans);
  795 
  796         if (bp->bio_cmd & BIO_PROBE) {
  797 
  798                 if (!(fdin_rd(fdc) & FDI_DCHG) && !(fd->flags & FD_EMPTY))
  799                         return (fdc_biodone(fdc, 0));
  800 
  801                 /*
  802                  * Try to find out if we have a disk in the drive
  803                  *
  804                  * First recal, then seek to cyl#1, this clears the
  805                  * old condition on the disk change line so we can
  806                  * examine it for current status
  807                  */
  808                 if (debugflags & 0x40)
  809                         printf("New disk in probe\n");
  810                 fd->flags |= FD_NEWDISK;
  811                 retry_line = __LINE__;
  812                 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
  813                         return (1);
  814                 msleep(fdc, NULL, PRIBIO, "fdrecal", hz);
  815                 retry_line = __LINE__;
  816                 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
  817                         return (1); /* XXX */
  818                 retry_line = __LINE__;
  819                 if ((st0 & 0xc0) || cyl != 0)
  820                         return (1);
  821 
  822                 /* Seek to track 1 */
  823                 retry_line = __LINE__;
  824                 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, 1, 0))
  825                         return (1);
  826                 msleep(fdc, NULL, PRIBIO, "fdseek", hz);
  827                 retry_line = __LINE__;
  828                 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
  829                         return (1); /* XXX */
  830                 need_recal |= (1 << fd->fdsu);
  831                 if (fdin_rd(fdc) & FDI_DCHG) {
  832                         if (debugflags & 0x40)
  833                                 printf("Empty in probe\n");
  834                         fd->flags |= FD_EMPTY;
  835                 } else {
  836                         if (debugflags & 0x40)
  837                                 printf("Got disk in probe\n");
  838                         fd->flags &= ~FD_EMPTY;
  839                         retry_line = __LINE__;
  840                         if(fdc_sense_drive(fdc, &st3) != 0)
  841                                 return (1);
  842                         if(st3 & NE7_ST3_WP)
  843                                 fd->flags |= FD_WP;
  844                         else
  845                                 fd->flags &= ~FD_WP;
  846                 }
  847                 return (fdc_biodone(fdc, 0));
  848         }
  849 
  850         /*
  851          * If we are dead just flush the requests
  852          */
  853         if (fd->flags & FD_EMPTY)
  854                 return (fdc_biodone(fdc, ENXIO));
  855 
  856         /* Check if we lost our media */
  857         if (fdin_rd(fdc) & FDI_DCHG) {
  858                 if (debugflags & 0x40)
  859                         printf("Lost disk\n");
  860                 fd->flags |= FD_EMPTY;
  861                 fd->flags |= FD_NEWDISK;
  862                 g_topology_lock();
  863                 g_orphan_provider(fd->fd_provider, EXDEV);
  864                 fd->fd_provider->flags |= G_PF_WITHER;
  865                 fd->fd_provider =
  866                     g_new_providerf(fd->fd_geom, fd->fd_geom->name);
  867                 g_error_provider(fd->fd_provider, 0);
  868                 g_topology_unlock();
  869                 return (fdc_biodone(fdc, ENXIO));
  870         }
  871 
  872         /* Check if the floppy is write-protected */
  873         if(bp->bio_cmd & (BIO_FMT | BIO_WRITE)) {
  874                 retry_line = __LINE__;
  875                 if(fdc_sense_drive(fdc, &st3) != 0)
  876                         return (1);
  877                 if(st3 & NE7_ST3_WP)
  878                         return (fdc_biodone(fdc, EROFS));
  879         }
  880 
  881         mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
  882         steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
  883         i = fd->ft->sectrac * fd->ft->heads;
  884         cylinder = bp->bio_pblkno / i;
  885         descyl = cylinder * steptrac;
  886         sec = bp->bio_pblkno % i;
  887         nsect = i - sec;
  888         head = sec / fd->ft->sectrac;
  889         sec = sec % fd->ft->sectrac + 1;
  890 
  891         /* If everything is going swimmingly, use multisector xfer */
  892         if (fdc->retry == 0 && bp->bio_cmd & (BIO_READ|BIO_WRITE)) {
  893                 fd->fd_iosize = imin(nsect * fd->sectorsize, bp->bio_resid);
  894                 nsect = fd->fd_iosize / fd->sectorsize;
  895         } else if (bp->bio_cmd & (BIO_READ|BIO_WRITE)) {
  896                 fd->fd_iosize = fd->sectorsize;
  897                 nsect = 1;
  898         }
  899 
  900         /* Do RECAL if we need to or are going to track zero anyway */
  901         if ((need_recal & (1 << fd->fdsu)) ||
  902             (cylinder == 0 && fd->track != 0) ||
  903             fdc->retry > 2) {
  904                 retry_line = __LINE__;
  905                 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
  906                         return (1);
  907                 msleep(fdc, NULL, PRIBIO, "fdrecal", hz);
  908                 retry_line = __LINE__;
  909                 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
  910                         return (1); /* XXX */
  911                 retry_line = __LINE__;
  912                 if ((st0 & 0xc0) || cyl != 0)
  913                         return (1);
  914                 need_recal &= ~(1 << fd->fdsu);
  915                 fd->track = 0;
  916                 /* let the heads settle */
  917                 if (settle)
  918                         msleep(fdc->fd, NULL, PRIBIO, "fdhdstl", settle);
  919         }
  920 
  921         /* 
  922          * SEEK to where we want to be
  923          *
  924          * Enhanced controllers do implied seeks for read&write as long as
  925          * we do not need multiple steps per track.
  926          */
  927         if (cylinder != fd->track && (
  928             fdc->fdct != FDC_ENHANCED ||
  929             descyl != cylinder ||
  930             (bp->bio_cmd & (BIO_RDID|BIO_FMT)))) {
  931                 retry_line = __LINE__;
  932                 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, descyl, 0))
  933                         return (1);
  934                 msleep(fdc, NULL, PRIBIO, "fdseek", hz);
  935                 retry_line = __LINE__;
  936                 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
  937                         return (1); /* XXX */
  938                 retry_line = __LINE__;
  939                 if ((st0 & 0xc0) || cyl != descyl) {
  940                         need_recal |= (1 << fd->fdsu);
  941                         return (1);
  942                 }
  943                 /* let the heads settle */
  944                 if (settle)
  945                         msleep(fdc->fd, NULL, PRIBIO, "fdhdstl", settle);
  946         }
  947         fd->track = cylinder;
  948 
  949         if (debugflags & 8) 
  950                 printf("op %x bn %ju siz %u ptr %p retry %d\n",
  951                     bp->bio_cmd, bp->bio_pblkno, fd->fd_iosize,
  952                     fd->fd_ioptr, fdc->retry);
  953 
  954         /* Setup ISADMA if we need it and have it */
  955         if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT))
  956              && !(fdc->flags & FDC_NODMA)) {
  957                 mtx_lock(&Giant);
  958                 isa_dmastart(
  959                     bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
  960                     fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
  961                 mtx_unlock(&Giant);
  962                 fd->flags |= FD_ISADMA;
  963         }
  964 
  965         /* Do PIO if we have to */
  966         if (fdc->flags & FDC_NODMA) {
  967                 if (bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT))
  968                         fdbcdr_wr(fdc, 1, fd->fd_iosize);
  969                 if (bp->bio_cmd & (BIO_WRITE|BIO_FMT))
  970                         fdc_pio(fdc);
  971         }
  972 
  973         switch(bp->bio_cmd) {
  974         case BIO_FMT:
  975                 /* formatting */
  976                 finfo = (struct fd_formb *)bp->bio_data;
  977                 retry_line = __LINE__;
  978                 if (fdc_cmd(fdc, 6,
  979                     NE7CMD_FORMAT | mfm,
  980                     head << 2 | fd->fdsu,
  981                     finfo->fd_formb_secshift,
  982                     finfo->fd_formb_nsecs,
  983                     finfo->fd_formb_gaplen,
  984                     finfo->fd_formb_fillbyte, 0))
  985                         return (1);
  986                 break;
  987         case BIO_RDID:
  988                 retry_line = __LINE__;
  989                 if (fdc_cmd(fdc, 2,
  990                     NE7CMD_READID | mfm,
  991                     head << 2 | fd->fdsu, 0))
  992                         return (1);
  993                 break;
  994         case BIO_READ:
  995                 retry_line = __LINE__;
  996                 if (fdc_cmd(fdc, 9,
  997                     NE7CMD_READ | NE7CMD_SK | mfm | NE7CMD_MT,
  998                     head << 2 | fd->fdsu,       /* head & unit */
  999                     fd->track,                  /* track */
 1000                     head,                       /* head */
 1001                     sec,                        /* sector + 1 */
 1002                     fd->ft->secsize,            /* sector size */
 1003                     fd->ft->sectrac,            /* sectors/track */
 1004                     fd->ft->gap,                /* gap size */
 1005                     fd->ft->datalen,            /* data length */
 1006                     0))
 1007                         return (1);
 1008                 break;
 1009         case BIO_WRITE:
 1010                 retry_line = __LINE__;
 1011                 if (fdc_cmd(fdc, 9,
 1012                     NE7CMD_WRITE | mfm | NE7CMD_MT,
 1013                     head << 2 | fd->fdsu,       /* head & unit */
 1014                     fd->track,                  /* track */
 1015                     head,                       /* head */
 1016                     sec,                        /* sector + 1 */
 1017                     fd->ft->secsize,            /* sector size */
 1018                     fd->ft->sectrac,            /* sectors/track */
 1019                     fd->ft->gap,                /* gap size */
 1020                     fd->ft->datalen,            /* data length */
 1021                     0))
 1022                         return (1);
 1023                 break;
 1024         default:
 1025                 KASSERT(0 == 1, ("Wrong bio_cmd %x\n", bp->bio_cmd));
 1026         }
 1027 
 1028         /* Wait for interrupt */
 1029         i = msleep(fdc, NULL, PRIBIO, "fddata", hz);
 1030 
 1031         /* PIO if the read looks good */
 1032         if (i == 0 && (fdc->flags & FDC_NODMA) && (bp->bio_cmd & BIO_READ))
 1033                 fdc_pio(fdc);
 1034 
 1035         /* Finish DMA */
 1036         if (fd->flags & FD_ISADMA) {
 1037                 mtx_lock(&Giant);
 1038                 isa_dmadone(
 1039                     bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
 1040                     fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
 1041                 mtx_unlock(&Giant);
 1042                 fd->flags &= ~FD_ISADMA;
 1043         }
 1044 
 1045         if (i != 0) {
 1046                 /*
 1047                  * Timeout.
 1048                  *
 1049                  * Due to IBM's brain-dead design, the FDC has a faked ready
 1050                  * signal, hardwired to ready == true. Thus, any command
 1051                  * issued if there's no diskette in the drive will _never_
 1052                  * complete, and must be aborted by resetting the FDC.
 1053                  * Many thanks, Big Blue!
 1054                  */
 1055                 retry_line = __LINE__;
 1056                 fdc->flags |= FDC_NEEDS_RESET;
 1057                 return (1);
 1058         }
 1059 
 1060         retry_line = __LINE__;
 1061         if (fdc_read_status(fdc))
 1062                 return (1);
 1063 
 1064         if (debugflags & 0x10)
 1065                 printf("  -> %x %x %x %x\n",
 1066                     fdc->status[0], fdc->status[1],
 1067                     fdc->status[2], fdc->status[3]);
 1068 
 1069         st0 = fdc->status[0] & NE7_ST0_IC;
 1070         if (st0 != 0) {
 1071                 retry_line = __LINE__;
 1072                 if (st0 == NE7_ST0_IC_AT && fdc->status[1] & NE7_ST1_OR) {
 1073                         /*
 1074                          * DMA overrun. Someone hogged the bus and
 1075                          * didn't release it in time for the next
 1076                          * FDC transfer.
 1077                          */
 1078                         return (1);
 1079                 }
 1080                 retry_line = __LINE__;
 1081                 if(st0 == NE7_ST0_IC_IV) {
 1082                         fdc->flags |= FDC_NEEDS_RESET;
 1083                         return (1);
 1084                 }
 1085                 retry_line = __LINE__;
 1086                 if(st0 == NE7_ST0_IC_AT && fdc->status[2] & NE7_ST2_WC) {
 1087                         need_recal |= (1 << fd->fdsu);
 1088                         return (1);
 1089                 }
 1090                 if (debugflags & 0x20) {
 1091                         printf("status %02x %02x %02x %02x %02x %02x\n",
 1092                             fdc->status[0], fdc->status[1], fdc->status[2],
 1093                             fdc->status[3], fdc->status[4], fdc->status[5]);
 1094                 }
 1095                 retry_line = __LINE__;
 1096                 return (1);
 1097         }
 1098         /* All OK */
 1099         switch(bp->bio_cmd) {
 1100         case BIO_RDID:
 1101                 /* copy out ID field contents */
 1102                 idp = (struct fdc_readid *)bp->bio_data;
 1103                 idp->cyl = fdc->status[3];
 1104                 idp->head = fdc->status[4];
 1105                 idp->sec = fdc->status[5];
 1106                 idp->secshift = fdc->status[6];
 1107                 if (debugflags & 0x40)
 1108                         printf("c %d h %d s %d z %d\n",
 1109                             idp->cyl, idp->head, idp->sec, idp->secshift);
 1110                 break;
 1111         case BIO_READ:
 1112         case BIO_WRITE:
 1113                 bp->bio_pblkno += nsect;
 1114                 bp->bio_resid -= fd->fd_iosize;
 1115                 bp->bio_completed += fd->fd_iosize;
 1116                 fd->fd_ioptr += fd->fd_iosize;
 1117                 /* Since we managed to get something done, reset the retry */
 1118                 fdc->retry = 0;
 1119                 if (bp->bio_resid > 0)
 1120                         return (0);
 1121                 break;
 1122         case BIO_FMT:
 1123                 break;
 1124         }
 1125         return (fdc_biodone(fdc, 0));
 1126 }
 1127 
 1128 static void
 1129 fdc_thread(void *arg)
 1130 {
 1131         struct fdc_data *fdc;
 1132 
 1133         fdc = arg;
 1134         int i;
 1135 
 1136         for (;;) {
 1137                 i = fdc_worker(fdc);
 1138                 if (i && debugflags & 0x20) {
 1139                         if (fdc->bp != NULL) {
 1140                                 g_print_bio(fdc->bp);
 1141                                 printf("\n");
 1142                         }
 1143                         printf("Retry line %d\n", retry_line);
 1144                 }
 1145                 fdc->retry += i;
 1146         }
 1147 }
 1148 
 1149 /*
 1150  * Enqueue a requst.
 1151  */
 1152 static void
 1153 fd_enqueue(struct fd_data *fd, struct bio *bp)
 1154 {
 1155         struct fdc_data *fdc;
 1156         int call;
 1157 
 1158         call = 0;
 1159         fdc = fd->fdc;
 1160         mtx_lock(&fdc->fdc_mtx);
 1161         /* If we go from idle, cancel motor turnoff */
 1162         if (fd->fd_iocount++ == 0)
 1163                 callout_drain(&fd->toffhandle);
 1164         if (fd->flags & FD_MOTOR) {
 1165                 /* The motor is on, send it directly to the controller */
 1166                 bioq_disksort(&fdc->head, bp);
 1167                 wakeup(&fdc->head);
 1168         } else {
 1169                 /* Queue it on the drive until the motor has started */
 1170                 bioq_insert_tail(&fd->fd_bq, bp);
 1171                 if (!(fd->flags & FD_MOTORWAIT))
 1172                         fd_motor(fd, 1);
 1173         }
 1174         mtx_unlock(&fdc->fdc_mtx);
 1175 }
 1176 
 1177 static int
 1178 fdmisccmd(struct fd_data *fd, u_int cmd, void *data)
 1179 {
 1180         struct bio *bp;
 1181         struct fd_formb *finfo;
 1182         struct fdc_readid *idfield;
 1183         int error;
 1184 
 1185         bp = malloc(sizeof(struct bio), M_TEMP, M_WAITOK | M_ZERO);
 1186 
 1187         /*
 1188          * Set up a bio request for fdstrategy().  bio_offset is faked
 1189          * so that fdstrategy() will seek to the the requested
 1190          * cylinder, and use the desired head.
 1191          */
 1192         bp->bio_cmd = cmd;
 1193         if (cmd == BIO_FMT) {
 1194                 finfo = (struct fd_formb *)data;
 1195                 bp->bio_pblkno =
 1196                     (finfo->cyl * fd->ft->heads + finfo->head) *
 1197                     fd->ft->sectrac;
 1198                 bp->bio_length = sizeof *finfo;
 1199         } else if (cmd == BIO_RDID) {
 1200                 idfield = (struct fdc_readid *)data;
 1201                 bp->bio_pblkno =
 1202                     (idfield->cyl * fd->ft->heads + idfield->head) *
 1203                     fd->ft->sectrac;
 1204                 bp->bio_length = sizeof(struct fdc_readid);
 1205         } else if (cmd == BIO_PROBE) {
 1206                 /* nothing */
 1207         } else
 1208                 panic("wrong cmd in fdmisccmd()");
 1209         bp->bio_offset = bp->bio_pblkno * fd->sectorsize;
 1210         bp->bio_data = data;
 1211         bp->bio_driver1 = fd;
 1212         bp->bio_flags = 0;
 1213 
 1214         fd_enqueue(fd, bp);
 1215 
 1216         do {
 1217                 msleep(bp, NULL, PRIBIO, "fdwait", hz);
 1218         } while (!(bp->bio_flags & BIO_DONE));
 1219         error = bp->bio_error;
 1220 
 1221         free(bp, M_TEMP);
 1222         return (error);
 1223 }
 1224 
 1225 /*
 1226  * Try figuring out the density of the media present in our device.
 1227  */
 1228 static int
 1229 fdautoselect(struct fd_data *fd)
 1230 {
 1231         struct fd_type *fdtp;
 1232         struct fdc_readid id;
 1233         int oopts, rv;
 1234 
 1235         if (!(fd->ft->flags & FL_AUTO))
 1236                 return (0);
 1237 
 1238         fdtp = fd_native_types[fd->type];
 1239         fdsettype(fd, fdtp);
 1240         if (!(fd->ft->flags & FL_AUTO))
 1241                 return (0);
 1242 
 1243         /*
 1244          * Try reading sector ID fields, first at cylinder 0, head 0,
 1245          * then at cylinder 2, head N.  We don't probe cylinder 1,
 1246          * since for 5.25in DD media in a HD drive, there are no data
 1247          * to read (2 step pulses per media cylinder required).  For
 1248          * two-sided media, the second probe always goes to head 1, so
 1249          * we can tell them apart from single-sided media.  As a
 1250          * side-effect this means that single-sided media should be
 1251          * mentioned in the search list after two-sided media of an
 1252          * otherwise identical density.  Media with a different number
 1253          * of sectors per track but otherwise identical parameters
 1254          * cannot be distinguished at all.
 1255          *
 1256          * If we successfully read an ID field on both cylinders where
 1257          * the recorded values match our expectation, we are done.
 1258          * Otherwise, we try the next density entry from the table.
 1259          *
 1260          * Stepping to cylinder 2 has the side-effect of clearing the
 1261          * unit attention bit.
 1262          */
 1263         oopts = fd->options;
 1264         fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
 1265         for (; fdtp->heads; fdtp++) {
 1266                 fdsettype(fd, fdtp);
 1267 
 1268                 id.cyl = id.head = 0;
 1269                 rv = fdmisccmd(fd, BIO_RDID, &id);
 1270                 if (rv != 0)
 1271                         continue;
 1272                 if (id.cyl != 0 || id.head != 0 || id.secshift != fdtp->secsize)
 1273                         continue;
 1274                 id.cyl = 2;
 1275                 id.head = fd->ft->heads - 1;
 1276                 rv = fdmisccmd(fd, BIO_RDID, &id);
 1277                 if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
 1278                     id.secshift != fdtp->secsize)
 1279                         continue;
 1280                 if (rv == 0)
 1281                         break;
 1282         }
 1283 
 1284         fd->options = oopts;
 1285         if (fdtp->heads == 0) {
 1286                 if (debugflags & 0x40)
 1287                         device_printf(fd->dev, "autoselection failed\n");
 1288                 fdsettype(fd, fd_native_types[fd->type]);
 1289                 return (0);
 1290         } else {
 1291                 if (debugflags & 0x40) {
 1292                         device_printf(fd->dev,
 1293                             "autoselected %d KB medium\n", fd->ft->size / 2);
 1294                         fdprinttype(fd->ft);
 1295                 }
 1296                 return (0);
 1297         }
 1298 }
 1299 
 1300 /*
 1301  * GEOM class implementation
 1302  */
 1303 
 1304 static g_access_t       fd_access;
 1305 static g_start_t        fd_start;
 1306 static g_ioctl_t        fd_ioctl;
 1307 
 1308 struct g_class g_fd_class = {
 1309         .name =         "FD",
 1310         .version =      G_VERSION,
 1311         .start =        fd_start,
 1312         .access =       fd_access,
 1313         .ioctl =        fd_ioctl,
 1314 };
 1315 
 1316 DECLARE_GEOM_CLASS(g_fd_class, g_fd);
 1317 
 1318 static int
 1319 fd_access(struct g_provider *pp, int r, int w, int e)
 1320 {
 1321         struct fd_data *fd;
 1322         struct fdc_data *fdc;
 1323         int ar, aw, ae;
 1324 
 1325         fd = pp->geom->softc;
 1326         fdc = fd->fdc;
 1327 
 1328         /*
 1329          * If our provider is withering, we can only get negative requests
 1330          * and we don't want to even see them
 1331          */
 1332         if (pp->flags & G_PF_WITHER)
 1333                 return (0);
 1334 
 1335         ar = r + pp->acr;
 1336         aw = w + pp->acw;
 1337         ae = e + pp->ace;
 1338 
 1339         if (ar == 0 && aw == 0 && ae == 0) {
 1340                 device_unbusy(fd->dev);
 1341                 return (0);
 1342         }
 1343 
 1344         if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) {
 1345                 if (fdmisccmd(fd, BIO_PROBE, NULL))
 1346                         return (ENXIO);
 1347                 if (fd->flags & FD_EMPTY)
 1348                         return (ENXIO);
 1349                 if (fd->flags & FD_NEWDISK) {
 1350                         fdautoselect(fd);
 1351                         fd->flags &= ~FD_NEWDISK;
 1352                 }
 1353                 device_busy(fd->dev);
 1354         }
 1355 
 1356 #ifdef notyet
 1357         if (w > 0 && (fd->flags & FD_WP))
 1358                 return (EROFS);
 1359 #endif
 1360 
 1361         pp->sectorsize = fd->sectorsize;
 1362         pp->stripesize = fd->ft->heads * fd->ft->sectrac * fd->sectorsize;
 1363         pp->mediasize = pp->stripesize * fd->ft->tracks;
 1364         return (0);
 1365 }
 1366 
 1367 static void
 1368 fd_start(struct bio *bp)
 1369 {
 1370         struct fdc_data *       fdc;
 1371         struct fd_data *        fd;
 1372 
 1373         fd = bp->bio_to->geom->softc;
 1374         fdc = fd->fdc;
 1375         bp->bio_driver1 = fd;
 1376         if (bp->bio_cmd & BIO_GETATTR) {
 1377                 if (g_handleattr_int(bp, "GEOM::fwsectors", fd->ft->sectrac))
 1378                         return;
 1379                 if (g_handleattr_int(bp, "GEOM::fwheads", fd->ft->heads))
 1380                         return;
 1381                 g_io_deliver(bp, ENOIOCTL);
 1382                 return;
 1383         }
 1384         if (!(bp->bio_cmd & (BIO_READ|BIO_WRITE))) {
 1385                 g_io_deliver(bp, EOPNOTSUPP);
 1386                 return;
 1387         }
 1388         bp->bio_pblkno = bp->bio_offset / fd->sectorsize;
 1389         bp->bio_resid = bp->bio_length;
 1390         fd_enqueue(fd, bp);
 1391         return;
 1392 }
 1393 
 1394 static int
 1395 fd_ioctl(struct g_provider *pp, u_long cmd, void *data, struct thread *td)
 1396 {
 1397         struct fd_data *fd;
 1398         struct fdc_status *fsp;
 1399         struct fdc_readid *rid;
 1400         int error;
 1401 
 1402         fd = pp->geom->softc;
 1403 
 1404         switch (cmd) {
 1405         case FD_GTYPE:                  /* get drive type */
 1406                 *(struct fd_type *)data = *fd->ft;
 1407                 return (0);
 1408 
 1409         case FD_STYPE:                  /* set drive type */
 1410                 /*
 1411                  * Allow setting drive type temporarily iff
 1412                  * currently unset.  Used for fdformat so any
 1413                  * user can set it, and then start formatting.
 1414                  */
 1415                 fd->fts = *(struct fd_type *)data;
 1416                 if (fd->fts.sectrac) {
 1417                         /* XXX: check for rubbish */
 1418                         fdsettype(fd, &fd->fts);
 1419                 } else {
 1420                         fdsettype(fd, fd_native_types[fd->type]);
 1421                 }
 1422                 if (debugflags & 0x40)
 1423                         fdprinttype(fd->ft);
 1424                 return (0);
 1425 
 1426         case FD_GOPTS:                  /* get drive options */
 1427                 *(int *)data = fd->options;
 1428                 return (0);
 1429 
 1430         case FD_SOPTS:                  /* set drive options */
 1431                 fd->options = *(int *)data;
 1432                 return (0);
 1433 
 1434         case FD_CLRERR:
 1435                 if (suser(td) != 0)
 1436                         return (EPERM);
 1437                 fd->fdc->fdc_errs = 0;
 1438                 return (0);
 1439 
 1440         case FD_GSTAT:
 1441                 fsp = (struct fdc_status *)data;
 1442                 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
 1443                         return (EINVAL);
 1444                 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
 1445                 return (0);
 1446 
 1447         case FD_GDTYPE:
 1448                 *(enum fd_drivetype *)data = fd->type;
 1449                 return (0);
 1450 
 1451         case FD_FORM:
 1452                 if (((struct fd_formb *)data)->format_version !=
 1453                     FD_FORMAT_VERSION)
 1454                         return (EINVAL); /* wrong version of formatting prog */
 1455                 error = fdmisccmd(fd, BIO_FMT, data);
 1456                 fd->flags |= FD_NEWDISK;
 1457                 break;
 1458 
 1459         case FD_READID:
 1460                 rid = (struct fdc_readid *)data;
 1461                 if (rid->cyl > 85 || rid->head > 1)
 1462                         return (EINVAL);
 1463                 error = fdmisccmd(fd, BIO_RDID, data);
 1464                 break;
 1465 
 1466         case FIONBIO:
 1467         case FIOASYNC:
 1468                 /* For backwards compat with old fd*(8) tools */
 1469                 error = 0;
 1470                 break;
 1471 
 1472         default:
 1473                 if (debugflags & 0x80)
 1474                         printf("Unknown ioctl %lx\n", cmd);
 1475                 error = ENOIOCTL;
 1476                 break;
 1477         }
 1478         return (error);
 1479 };
 1480 
 1481 
 1482 
 1483 /*
 1484  * Configuration/initialization stuff, per controller.
 1485  */
 1486 
 1487 devclass_t fdc_devclass;
 1488 static devclass_t fd_devclass;
 1489 
 1490 struct fdc_ivars {
 1491         int     fdunit;
 1492         int     fdtype;
 1493 };
 1494 
 1495 void
 1496 fdc_release_resources(struct fdc_data *fdc)
 1497 {
 1498         device_t dev;
 1499 
 1500         dev = fdc->fdc_dev;
 1501         if (fdc->fdc_intr)
 1502                 bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
 1503         fdc->fdc_intr = NULL;
 1504         if (fdc->res_irq != NULL)
 1505                 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
 1506                     fdc->res_irq);
 1507         fdc->res_irq = NULL;
 1508         if (fdc->res_ctl != NULL)
 1509                 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl,
 1510                     fdc->res_ctl);
 1511         fdc->res_ctl = NULL;
 1512         if (fdc->res_sts != NULL)
 1513                 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_sts,
 1514                     fdc->res_sts);
 1515         fdc->res_sts = NULL;
 1516         if (fdc->res_ioport != NULL)
 1517                 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport,
 1518                     fdc->res_ioport);
 1519         fdc->res_ioport = NULL;
 1520         if (fdc->res_drq != NULL)
 1521                 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
 1522                     fdc->res_drq);
 1523         fdc->res_drq = NULL;
 1524 }
 1525 
 1526 int
 1527 fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
 1528 {
 1529         struct fdc_ivars *ivars = device_get_ivars(child);
 1530 
 1531         switch (which) {
 1532         case FDC_IVAR_FDUNIT:
 1533                 *result = ivars->fdunit;
 1534                 break;
 1535         case FDC_IVAR_FDTYPE:
 1536                 *result = ivars->fdtype;
 1537                 break;
 1538         default:
 1539                 return (ENOENT);
 1540         }
 1541         return (0);
 1542 }
 1543 
 1544 int
 1545 fdc_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
 1546 {
 1547         struct fdc_ivars *ivars = device_get_ivars(child);
 1548 
 1549         switch (which) {
 1550         case FDC_IVAR_FDUNIT:
 1551                 ivars->fdunit = value;
 1552                 break;
 1553         case FDC_IVAR_FDTYPE:
 1554                 ivars->fdtype = value;
 1555                 break;
 1556         default:
 1557                 return (ENOENT);
 1558         }
 1559         return (0);
 1560 }
 1561 
 1562 int
 1563 fdc_initial_reset(device_t dev, struct fdc_data *fdc)
 1564 {
 1565         int ic_type, part_id;
 1566 
 1567         /*
 1568          * A status value of 0xff is very unlikely, but not theoretically
 1569          * impossible, but it is far more likely to indicate an empty bus.
 1570          */
 1571         if (fdsts_rd(fdc) == 0xff)
 1572                 return (ENXIO);
 1573 
 1574         /*
 1575          * Assert a reset to the floppy controller and check that the status
 1576          * register goes to zero.
 1577          */
 1578         fdout_wr(fdc, 0);
 1579         fdout_wr(fdc, 0);
 1580         if (fdsts_rd(fdc) != 0)
 1581                 return (ENXIO);
 1582 
 1583         /*
 1584          * Clear the reset and see it come ready.
 1585          */
 1586         fdout_wr(fdc, FDO_FRST);
 1587         DELAY(100);
 1588         if (fdsts_rd(fdc) != 0x80)
 1589                 return (ENXIO);
 1590 
 1591         /* Then, see if it can handle a command. */
 1592         if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, 0xaf, 0x1e, 0))
 1593                 return (ENXIO);
 1594 
 1595         /*
 1596          * Try to identify the chip.
 1597          *
 1598          * The i8272 datasheet documents that unknown commands
 1599          * will return ST0 as 0x80.  The i8272 is supposedly identical
 1600          * to the NEC765.
 1601          * The i82077SL datasheet says 0x90 for the VERSION command,
 1602          * and several "superio" chips emulate this.
 1603          */
 1604         if (fdc_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type))
 1605                 return (ENXIO);
 1606         if (fdc_cmd(fdc, 1, 0x18, 1, &part_id))
 1607                 return (ENXIO);
 1608         if (bootverbose)
 1609                 device_printf(dev,
 1610                     "ic_type %02x part_id %02x\n", ic_type, part_id);
 1611         switch (ic_type & 0xff) {
 1612         case 0x80:
 1613                 device_set_desc(dev, "NEC 765 or clone");
 1614                 fdc->fdct = FDC_NE765;
 1615                 break;
 1616         case 0x81:
 1617         case 0x90:
 1618                 device_set_desc(dev,
 1619                     "Enhanced floppy controller");
 1620                 fdc->fdct = FDC_ENHANCED;
 1621                 break;
 1622         default:
 1623                 device_set_desc(dev, "Generic floppy controller");
 1624                 fdc->fdct = FDC_UNKNOWN;
 1625                 break;
 1626         }
 1627         return (0);
 1628 }
 1629 
 1630 int
 1631 fdc_detach(device_t dev)
 1632 {
 1633         struct  fdc_data *fdc;
 1634         int     error;
 1635 
 1636         fdc = device_get_softc(dev);
 1637 
 1638         /* have our children detached first */
 1639         if ((error = bus_generic_detach(dev)))
 1640                 return (error);
 1641 
 1642         /* XXX: kill thread */
 1643         /* reset controller, turn motor off */
 1644         fdout_wr(fdc, 0);
 1645 
 1646         if (!(fdc->flags & FDC_NODMA))
 1647                 isa_dma_release(fdc->dmachan);
 1648         fdc_release_resources(fdc);
 1649         mtx_destroy(&fdc->fdc_mtx);
 1650         return (0);
 1651 }
 1652 
 1653 /*
 1654  * Add a child device to the fdc controller.  It will then be probed etc.
 1655  */
 1656 device_t
 1657 fdc_add_child(device_t dev, const char *name, int unit)
 1658 {
 1659         int flags;
 1660         struct fdc_ivars *ivar;
 1661         device_t child;
 1662 
 1663         ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
 1664         if (ivar == NULL)
 1665                 return (NULL);
 1666         child = device_add_child(dev, name, unit);
 1667         if (child == NULL) {
 1668                 free(ivar, M_DEVBUF);
 1669                 return (NULL);
 1670         }
 1671         device_set_ivars(child, ivar);
 1672         ivar->fdunit = unit;
 1673         ivar->fdtype = FDT_NONE;
 1674         if (resource_int_value(name, unit, "flags", &flags) == 0)
 1675                 device_set_flags(child, flags);
 1676         if (resource_disabled(name, unit))
 1677                 device_disable(child);
 1678         return (child);
 1679 }
 1680 
 1681 int
 1682 fdc_attach(device_t dev)
 1683 {
 1684         struct  fdc_data *fdc;
 1685         int     error;
 1686 
 1687         fdc = device_get_softc(dev);
 1688         fdc->fdc_dev = dev;
 1689         error = fdc_initial_reset(dev, fdc);
 1690         if (error) {
 1691                 device_printf(dev, "does not respond\n");
 1692                 return (error);
 1693         }
 1694         error = bus_setup_intr(dev, fdc->res_irq,
 1695             INTR_TYPE_BIO | INTR_ENTROPY | INTR_FAST | INTR_MPSAFE,
 1696             fdc_intr, fdc, &fdc->fdc_intr);
 1697         if (error) {
 1698                 device_printf(dev, "cannot setup interrupt\n");
 1699                 return (error);
 1700         }
 1701         if (!(fdc->flags & FDC_NODMA)) {
 1702                 error = isa_dma_acquire(fdc->dmachan);
 1703                 if (error)
 1704                         return (error);
 1705                 /* XXX no error return */
 1706                 isa_dmainit(fdc->dmachan, MAX_BYTES_PER_CYL);
 1707         }
 1708         fdc->fdcu = device_get_unit(dev);
 1709         fdc->flags |= FDC_NEEDS_RESET;
 1710 
 1711         mtx_init(&fdc->fdc_mtx, "fdc lock", NULL, MTX_DEF);
 1712 
 1713         /* reset controller, turn motor off, clear fdout mirror reg */
 1714         fdout_wr(fdc, fdc->fdout = 0);
 1715         bioq_init(&fdc->head);
 1716 
 1717         kthread_create(fdc_thread, fdc, &fdc->fdc_thread, 0, 0,
 1718             "fdc%d", device_get_unit(dev));
 1719 
 1720         settle = hz / 8;
 1721 
 1722         return (0);
 1723 }
 1724 
 1725 int
 1726 fdc_hints_probe(device_t dev)
 1727 {
 1728         const char *name, *dname;
 1729         int i, error, dunit;
 1730 
 1731         /*
 1732          * Probe and attach any children.  We should probably detect
 1733          * devices from the BIOS unless overridden.
 1734          */
 1735         name = device_get_nameunit(dev);
 1736         i = 0;
 1737         while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) {
 1738                 resource_int_value(dname, dunit, "drive", &dunit);
 1739                 fdc_add_child(dev, dname, dunit);
 1740         }
 1741 
 1742         if ((error = bus_generic_attach(dev)) != 0)
 1743                 return (error);
 1744         return (0);
 1745 }
 1746 
 1747 int
 1748 fdc_print_child(device_t me, device_t child)
 1749 {
 1750         int retval = 0, flags;
 1751 
 1752         retval += bus_print_child_header(me, child);
 1753         retval += printf(" on %s drive %d", device_get_nameunit(me),
 1754                fdc_get_fdunit(child));
 1755         if ((flags = device_get_flags(me)) != 0)
 1756                 retval += printf(" flags %#x", flags);
 1757         retval += printf("\n");
 1758 
 1759         return (retval);
 1760 }
 1761 
 1762 /*
 1763  * Configuration/initialization, per drive.
 1764  */
 1765 static int
 1766 fd_probe(device_t dev)
 1767 {
 1768         int     i, unit;
 1769         u_int   st0, st3;
 1770         struct  fd_data *fd;
 1771         struct  fdc_data *fdc;
 1772         int     fdsu;
 1773         int     flags, type;
 1774 
 1775         fdsu = fdc_get_fdunit(dev);
 1776         fd = device_get_softc(dev);
 1777         fdc = device_get_softc(device_get_parent(dev));
 1778         flags = device_get_flags(dev);
 1779 
 1780         fd->dev = dev;
 1781         fd->fdc = fdc;
 1782         fd->fdsu = fdsu;
 1783         unit = device_get_unit(dev);
 1784 
 1785         /* Auto-probe if fdinfo is present, but always allow override. */
 1786         type = flags & FD_TYPEMASK;
 1787         if (type == FDT_NONE && (type = fdc_get_fdtype(dev)) != FDT_NONE) {
 1788                 fd->type = type;
 1789                 goto done;
 1790         } else {
 1791                 /* make sure fdautoselect() will be called */
 1792                 fd->flags = FD_EMPTY;
 1793                 fd->type = type;
 1794         }
 1795 
 1796 #if (defined(__i386__) && !defined(PC98)) || defined(__amd64__)
 1797         if (fd->type == FDT_NONE && (unit == 0 || unit == 1)) {
 1798                 /* Look up what the BIOS thinks we have. */
 1799                 if (unit == 0)
 1800                         fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
 1801                 else
 1802                         fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
 1803                 if (fd->type == FDT_288M_1)
 1804                         fd->type = FDT_288M;
 1805         }
 1806 #endif /* __i386__ || __amd64__ */
 1807         /* is there a unit? */
 1808         if (fd->type == FDT_NONE)
 1809                 return (ENXIO);
 1810 
 1811 /*
 1812         mtx_lock(&fdc->fdc_mtx);
 1813 */
 1814         /* select it */
 1815         fd_select(fd);
 1816         fd_motor(fd, 1);
 1817         fdc->fd = fd;
 1818         fdc_reset(fdc);         /* XXX reset, then unreset, etc. */
 1819         DELAY(1000000); /* 1 sec */
 1820 
 1821         if ((flags & FD_NO_PROBE) == 0) {
 1822                 /* If we're at track 0 first seek inwards. */
 1823                 if ((fdc_sense_drive(fdc, &st3) == 0) &&
 1824                     (st3 & NE7_ST3_T0)) {
 1825                         /* Seek some steps... */
 1826                         if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
 1827                                 /* ...wait a moment... */
 1828                                 DELAY(300000);
 1829                                 /* make ctrlr happy: */
 1830                                 fdc_sense_int(fdc, 0, 0);
 1831                         }
 1832                 }
 1833 
 1834                 for (i = 0; i < 2; i++) {
 1835                         /*
 1836                          * we must recalibrate twice, just in case the
 1837                          * heads have been beyond cylinder 76, since
 1838                          * most FDCs still barf when attempting to
 1839                          * recalibrate more than 77 steps
 1840                          */
 1841                         /* go back to 0: */
 1842                         if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
 1843                                 /* a second being enough for full stroke seek*/
 1844                                 DELAY(i == 0 ? 1000000 : 300000);
 1845 
 1846                                 /* anything responding? */
 1847                                 if (fdc_sense_int(fdc, &st0, 0) == 0 &&
 1848                                     (st0 & NE7_ST0_EC) == 0)
 1849                                         break; /* already probed succesfully */
 1850                         }
 1851                 }
 1852         }
 1853 
 1854         fd_motor(fd, 0);
 1855         fdc->fd = NULL;
 1856 /*
 1857         mtx_unlock(&fdc->fdc_mtx);
 1858 */
 1859 
 1860         if ((flags & FD_NO_PROBE) == 0 &&
 1861             (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
 1862                 return (ENXIO);
 1863 
 1864 done:
 1865 
 1866         switch (fd->type) {
 1867         case FDT_12M:
 1868                 device_set_desc(dev, "1200-KB 5.25\" drive");
 1869                 break;
 1870         case FDT_144M:
 1871                 device_set_desc(dev, "1440-KB 3.5\" drive");
 1872                 break;
 1873         case FDT_288M:
 1874                 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
 1875                 break;
 1876         case FDT_360K:
 1877                 device_set_desc(dev, "360-KB 5.25\" drive");
 1878                 break;
 1879         case FDT_720K:
 1880                 device_set_desc(dev, "720-KB 3.5\" drive");
 1881                 break;
 1882         default:
 1883                 return (ENXIO);
 1884         }
 1885         fd->track = FD_NO_TRACK;
 1886         fd->fdc = fdc;
 1887         fd->fdsu = fdsu;
 1888         fd->options = 0;
 1889         callout_init(&fd->toffhandle, 1);
 1890         callout_init(&fd->tohandle, 1);
 1891 
 1892         /* initialize densities for subdevices */
 1893         fdsettype(fd, fd_native_types[fd->type]);
 1894         return (0);
 1895 }
 1896 
 1897 /*
 1898  * We have to do this in a geom event because GEOM is not running
 1899  * when fd_attach() is.
 1900  * XXX: move fd_attach after geom like ata/scsi disks
 1901  */
 1902 static void
 1903 fd_attach2(void *arg, int flag)
 1904 {
 1905         struct  fd_data *fd;
 1906 
 1907         fd = arg;
 1908         
 1909         fd->fd_geom = g_new_geomf(&g_fd_class,
 1910             "fd%d", device_get_unit(fd->dev));
 1911         fd->fd_provider = g_new_providerf(fd->fd_geom, fd->fd_geom->name);
 1912         fd->fd_geom->softc = fd;
 1913         g_error_provider(fd->fd_provider, 0);
 1914 }
 1915 
 1916 static int
 1917 fd_attach(device_t dev)
 1918 {
 1919         struct  fd_data *fd;
 1920 
 1921         fd = device_get_softc(dev);
 1922         g_post_event(fd_attach2, fd, M_WAITOK, NULL);
 1923         fd->flags |= FD_EMPTY;
 1924         bioq_init(&fd->fd_bq);
 1925         return (0);
 1926 
 1927         return (0);
 1928 }
 1929 
 1930 static int
 1931 fd_detach(device_t dev)
 1932 {
 1933         struct  fd_data *fd;
 1934 
 1935         fd = device_get_softc(dev);
 1936         callout_drain(&fd->toffhandle);
 1937 
 1938         return (0);
 1939 }
 1940 
 1941 static device_method_t fd_methods[] = {
 1942         /* Device interface */
 1943         DEVMETHOD(device_probe,         fd_probe),
 1944         DEVMETHOD(device_attach,        fd_attach),
 1945         DEVMETHOD(device_detach,        fd_detach),
 1946         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
 1947         DEVMETHOD(device_suspend,       bus_generic_suspend), /* XXX */
 1948         DEVMETHOD(device_resume,        bus_generic_resume), /* XXX */
 1949         { 0, 0 }
 1950 };
 1951 
 1952 static driver_t fd_driver = {
 1953         "fd",
 1954         fd_methods,
 1955         sizeof(struct fd_data)
 1956 };
 1957 
 1958 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0);

Cache object: 9c4292e5a5b1ec9b70caffd1bfac7aa1


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