Home | History | Annotate | Line # | Download | only in isa
fd.c revision 1.83
      1 /*	$NetBSD: fd.c,v 1.83 2008/03/29 09:01:35 mlelstv Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*-
     40  * Copyright (c) 1990 The Regents of the University of California.
     41  * All rights reserved.
     42  *
     43  * This code is derived from software contributed to Berkeley by
     44  * Don Ahn.
     45  *
     46  * Redistribution and use in source and binary forms, with or without
     47  * modification, are permitted provided that the following conditions
     48  * are met:
     49  * 1. Redistributions of source code must retain the above copyright
     50  *    notice, this list of conditions and the following disclaimer.
     51  * 2. Redistributions in binary form must reproduce the above copyright
     52  *    notice, this list of conditions and the following disclaimer in the
     53  *    documentation and/or other materials provided with the distribution.
     54  * 3. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
     71  */
     72 
     73 /*
     74  * Floppy formatting facilities merged from FreeBSD fd.c driver:
     75  *	Id: fd.c,v 1.53 1995/03/12 22:40:56 joerg Exp
     76  * which carries the same copyright/redistribution notice as shown above with
     77  * the addition of the following statement before the "Redistribution and
     78  * use ..." clause:
     79  *
     80  * Copyright (c) 1993, 1994 by
     81  *  jc (at) irbs.UUCP (John Capo)
     82  *  vak (at) zebub.msk.su (Serge Vakulenko)
     83  *  ache (at) astral.msk.su (Andrew A. Chernov)
     84  *
     85  * Copyright (c) 1993, 1994, 1995 by
     86  *  joerg_wunsch (at) uriah.sax.de (Joerg Wunsch)
     87  *  dufault (at) hda.com (Peter Dufault)
     88  */
     89 
     90 #include <sys/cdefs.h>
     91 __KERNEL_RCSID(0, "$NetBSD: fd.c,v 1.83 2008/03/29 09:01:35 mlelstv Exp $");
     92 
     93 #include "rnd.h"
     94 #include "opt_ddb.h"
     95 
     96 /*
     97  * XXX This driver should be properly MI'd some day, but this allows us
     98  * XXX to eliminate a lot of code duplication for now.
     99  */
    100 #if !defined(alpha) && !defined(algor) && !defined(atari) && \
    101     !defined(bebox) && !defined(evbmips) && !defined(i386) && \
    102     !defined(prep) && !defined(sandpoint) && !defined(x86_64) && \
    103     !defined(mvmeppc)
    104 #error platform not supported by this driver, yet
    105 #endif
    106 
    107 #include <sys/param.h>
    108 #include <sys/systm.h>
    109 #include <sys/callout.h>
    110 #include <sys/kernel.h>
    111 #include <sys/file.h>
    112 #include <sys/ioctl.h>
    113 #include <sys/device.h>
    114 #include <sys/disklabel.h>
    115 #include <sys/disk.h>
    116 #include <sys/buf.h>
    117 #include <sys/bufq.h>
    118 #include <sys/malloc.h>
    119 #include <sys/uio.h>
    120 #include <sys/syslog.h>
    121 #include <sys/queue.h>
    122 #include <sys/proc.h>
    123 #include <sys/fdio.h>
    124 #include <sys/conf.h>
    125 #include <sys/vnode.h>
    126 #if NRND > 0
    127 #include <sys/rnd.h>
    128 #endif
    129 
    130 #include <prop/proplib.h>
    131 
    132 #include <uvm/uvm_extern.h>
    133 
    134 #include <dev/cons.h>
    135 
    136 #include <sys/cpu.h>
    137 #include <sys/bus.h>
    138 
    139 #include "locators.h"
    140 
    141 #if defined(atari)
    142 /*
    143  * On the atari, it is configured as fdcisa
    144  */
    145 #define	FDCCF_DRIVE		FDCISACF_DRIVE
    146 #define	FDCCF_DRIVE_DEFAULT	FDCISACF_DRIVE_DEFAULT
    147 
    148 #define	fd_cd	fdisa_cd
    149 #endif /* atari */
    150 
    151 #include <sys/intr.h>
    152 
    153 #include <dev/isa/isavar.h>
    154 #include <dev/isa/isadmavar.h>
    155 
    156 #include <dev/isa/fdreg.h>
    157 #include <dev/isa/fdcvar.h>
    158 
    159 #if defined(i386)
    160 
    161 #include <dev/ic/mc146818reg.h>			/* for NVRAM access */
    162 #include <i386/isa/nvram.h>
    163 
    164 #include "mca.h"
    165 #if NMCA > 0
    166 #include <machine/mca_machdep.h>		/* for MCA_system */
    167 #endif
    168 
    169 #endif /* i386 */
    170 
    171 #include <dev/isa/fdvar.h>
    172 
    173 #define FDUNIT(dev)	(minor(dev) / 8)
    174 #define FDTYPE(dev)	(minor(dev) % 8)
    175 
    176 /* (mis)use device use flag to identify format operation */
    177 #define B_FORMAT B_DEVPRIVATE
    178 
    179 /* controller driver configuration */
    180 int fdprint(void *, const char *);
    181 
    182 #if NMCA > 0
    183 /* MCA - specific entries */
    184 const struct fd_type mca_fd_types[] = {
    185 	{ 18,2,36,2,0xff,0x0f,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB"    }, /* 1.44MB diskette - XXX try 16ms step rate */
    186 	{  9,2,18,2,0xff,0x4f,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB"    }, /* 3.5 inch 720kB diskette - XXX try 24ms step rate */
    187 };
    188 #endif /* NMCA > 0 */
    189 
    190 /* The order of entries in the following table is important -- BEWARE! */
    191 
    192 #if defined(atari)
    193 const struct fd_type fd_types[] = {
    194 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,0xf6,1, "360KB/PC" }, /* 360kB PC diskettes */
    195 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB"    }, /* 3.5 inch 720kB diskette */
    196 	{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB"   }, /* 1.44MB diskette */
    197 };
    198 #else
    199 const struct fd_type fd_types[] = {
    200 	{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB"   }, /* 1.44MB diskette */
    201 	{ 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS,0xf6,1, "1.2MB"    }, /* 1.2 MB AT-diskettes */
    202 	{  9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS,0xf6,1, "360KB/AT" }, /* 360kB in 1.2MB drive */
    203 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,0xf6,1, "360KB/PC" }, /* 360kB PC diskettes */
    204 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB"    }, /* 3.5 inch 720kB diskette */
    205 	{  9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS,0xf6,1, "720KB/x"  }, /* 720kB in 1.2MB drive */
    206 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,0xf6,1, "360KB/x"  }, /* 360kB in 720kB drive */
    207 };
    208 #endif /* defined(atari) */
    209 
    210 void fdcfinishattach(device_t);
    211 int fdprobe(device_t, struct cfdata *, void *);
    212 void fdattach(device_t, device_t, void *);
    213 static int fddetach(device_t, int);
    214 static int fdcintr1(struct fdc_softc *);
    215 static void fdcintrcb(void *);
    216 static bool fdcsuspend(device_t PMF_FN_PROTO);
    217 static bool fdcresume(device_t PMF_FN_PROTO);
    218 
    219 extern struct cfdriver fd_cd;
    220 
    221 #ifdef atari
    222 CFATTACH_DECL_NEW(fdisa, sizeof(struct fd_softc),
    223     fdprobe, fdattach, fddetach, NULL);
    224 #else
    225 CFATTACH_DECL_NEW(fd, sizeof(struct fd_softc),
    226     fdprobe, fdattach, fddetach, NULL);
    227 #endif
    228 
    229 dev_type_open(fdopen);
    230 dev_type_close(fdclose);
    231 dev_type_read(fdread);
    232 dev_type_write(fdwrite);
    233 dev_type_ioctl(fdioctl);
    234 dev_type_strategy(fdstrategy);
    235 
    236 const struct bdevsw fd_bdevsw = {
    237 	fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK
    238 };
    239 
    240 const struct cdevsw fd_cdevsw = {
    241 	fdopen, fdclose, fdread, fdwrite, fdioctl,
    242 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    243 };
    244 
    245 void fdgetdisklabel(struct fd_softc *);
    246 int fd_get_parms(struct fd_softc *);
    247 void fdstart(struct fd_softc *);
    248 
    249 struct dkdriver fddkdriver = { fdstrategy, NULL };
    250 
    251 #if defined(i386)
    252 const struct fd_type *fd_nvtotype(const char *, int, int);
    253 #endif /* i386 */
    254 void fd_set_motor(struct fdc_softc *fdc, int reset);
    255 void fd_motor_off(void *arg);
    256 void fd_motor_on(void *arg);
    257 int fdcresult(struct fdc_softc *fdc);
    258 void fdcstart(struct fdc_softc *fdc);
    259 void fdcstatus(device_t, int, const char *);
    260 void fdctimeout(void *arg);
    261 void fdcretry(struct fdc_softc *fdc);
    262 void fdfinish(struct fd_softc *fd, struct buf *bp);
    263 static const struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
    264 int fdformat(dev_t, struct ne7_fd_formb *, struct lwp *);
    265 static void fd_set_properties(struct fd_softc *fd);
    266 
    267 void	fd_mountroot_hook(device_t);
    268 
    269 /*
    270  * Arguments passed between fdcattach and fdprobe.
    271  */
    272 struct fdc_attach_args {
    273 	int fa_drive;
    274 	const struct fd_type *fa_deftype;
    275 };
    276 
    277 /*
    278  * Print the location of a disk drive (called just before attaching the
    279  * the drive).  If `fdc' is not NULL, the drive was found but was not
    280  * in the system config file; print the drive name as well.
    281  * Return QUIET (config_find ignores this if the device was configured) to
    282  * avoid printing `fdN not configured' messages.
    283  */
    284 int
    285 fdprint(void *aux, const char *fdc)
    286 {
    287 	struct fdc_attach_args *fa = aux;
    288 
    289 	if (!fdc)
    290 		aprint_normal(" drive %d", fa->fa_drive);
    291 	return QUIET;
    292 }
    293 
    294 static bool
    295 fdcresume(device_t self PMF_FN_ARGS)
    296 {
    297 	struct fdc_softc *fdc = device_private(self);
    298 
    299 	(void)fdcintr1(fdc);
    300 	return true;
    301 }
    302 
    303 static bool
    304 fdcsuspend(device_t self PMF_FN_ARGS)
    305 {
    306 	struct fdc_softc *fdc = device_private(self);
    307 	int drive;
    308 	struct fd_softc *fd;
    309 
    310 	mutex_enter(&fdc->sc_mtx);
    311 	while (fdc->sc_state != DEVIDLE)
    312 		cv_wait(&fdc->sc_cv, &fdc->sc_mtx);
    313 	for (drive = 0; drive < 4; drive++) {
    314 		if ((fd = fdc->sc_fd[drive]) == NULL)
    315 			continue;
    316 		fd->sc_flags &= ~(FD_MOTOR|FD_MOTOR_WAIT);
    317 	}
    318 	fd_set_motor(fdc, 0);
    319 	mutex_exit(&fdc->sc_mtx);
    320 	return true;
    321 }
    322 
    323 void
    324 fdc_childdet(device_t self, device_t child)
    325 {
    326 	struct fdc_softc *fdc = device_private(self);
    327 	struct fd_softc *fd = device_private(child);
    328 	int drive = fd->sc_drive;
    329 
    330 	KASSERT(fdc->sc_fd[drive] == fd); /* but the kid is not my son */
    331 	fdc->sc_fd[drive] = NULL;
    332 }
    333 
    334 int
    335 fdcdetach(device_t self, int flags)
    336 {
    337 	int rc;
    338 	struct fdc_softc *fdc = device_private(self);
    339 
    340 	if ((rc = config_detach_children(self, flags)) != 0)
    341 		return rc;
    342 
    343 	pmf_device_deregister(self);
    344 
    345 	isa_dmamap_destroy(fdc->sc_ic, fdc->sc_drq);
    346 	isa_drq_free(fdc->sc_ic, fdc->sc_drq);
    347 
    348 	callout_destroy(&fdc->sc_intr_ch);
    349 	callout_destroy(&fdc->sc_timo_ch);
    350 
    351 	cv_destroy(&fdc->sc_cv);
    352 	mutex_destroy(&fdc->sc_mtx);
    353 
    354 	return 0;
    355 }
    356 
    357 void
    358 fdcattach(struct fdc_softc *fdc)
    359 {
    360 	mutex_init(&fdc->sc_mtx, MUTEX_DEFAULT, IPL_BIO);
    361 	cv_init(&fdc->sc_cv, "fdcwakeup");
    362 	callout_init(&fdc->sc_timo_ch, 0);
    363 	callout_init(&fdc->sc_intr_ch, 0);
    364 
    365 	fdc->sc_state = DEVIDLE;
    366 	TAILQ_INIT(&fdc->sc_drives);
    367 
    368 	fdc->sc_maxiosize = isa_dmamaxsize(fdc->sc_ic, fdc->sc_drq);
    369 
    370 	if (isa_drq_alloc(fdc->sc_ic, fdc->sc_drq) != 0) {
    371 		aprint_normal_dev(fdc->sc_dev, "can't reserve drq %d\n",
    372 		    fdc->sc_drq);
    373 		return;
    374 	}
    375 
    376 	if (isa_dmamap_create(fdc->sc_ic, fdc->sc_drq, fdc->sc_maxiosize,
    377 	    BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
    378 		aprint_normal_dev(fdc->sc_dev, "can't set up ISA DMA map\n");
    379 		return;
    380 	}
    381 
    382 	config_interrupts(fdc->sc_dev, fdcfinishattach);
    383 
    384 	if (!pmf_device_register(fdc->sc_dev, fdcsuspend, fdcresume)) {
    385 		aprint_error_dev(fdc->sc_dev,
    386 		    "cannot set power mgmt handler\n");
    387 	}
    388 }
    389 
    390 void
    391 fdcfinishattach(device_t self)
    392 {
    393 	struct fdc_softc *fdc = device_private(self);
    394 	bus_space_tag_t iot = fdc->sc_iot;
    395 	bus_space_handle_t ioh = fdc->sc_ioh;
    396 	struct fdc_attach_args fa;
    397 #if defined(i386)
    398 	int type;
    399 #endif
    400 
    401 	/*
    402 	 * Reset the controller to get it into a known state. Not all
    403 	 * probes necessarily need do this to discover the controller up
    404 	 * front, so don't assume anything.
    405 	 */
    406 
    407 	bus_space_write_1(iot, ioh, fdout, 0);
    408 	delay(100);
    409 	bus_space_write_1(iot, ioh, fdout, FDO_FRST);
    410 
    411 	/* see if it can handle a command */
    412 	if (out_fdc(iot, ioh, NE7CMD_SPECIFY) < 0) {
    413 		aprint_normal_dev(fdc->sc_dev, "can't reset controller\n");
    414 		return;
    415 	}
    416 	out_fdc(iot, ioh, 0xdf);
    417 	out_fdc(iot, ioh, 2);
    418 
    419 #if defined(i386)
    420 	/*
    421 	 * The NVRAM info only tells us about the first two disks on the
    422 	 * `primary' floppy controller.
    423 	 */
    424 	/* XXX device_unit() abuse */
    425 	if (device_unit(fdc->sc_dev) == 0)
    426 		type = mc146818_read(NULL, NVRAM_DISKETTE); /* XXX softc */
    427 	else
    428 		type = -1;
    429 #endif /* i386 */
    430 
    431 	/* physical limit: four drives per controller. */
    432 	fdc->sc_state = PROBING;
    433 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
    434 		if (fdc->sc_known) {
    435 			if (fdc->sc_present & (1 << fa.fa_drive)) {
    436 				fa.fa_deftype = fdc->sc_knownfds[fa.fa_drive];
    437 				config_found(fdc->sc_dev, (void *)&fa,
    438 				    fdprint);
    439 			}
    440 		} else {
    441 #if defined(i386)
    442 			if (type >= 0 && fa.fa_drive < 2)
    443 				fa.fa_deftype =
    444 				    fd_nvtotype(device_xname(fdc->sc_dev),
    445 				        type, fa.fa_drive);
    446 			else
    447 				fa.fa_deftype = NULL;		/* unknown */
    448 #elif defined(atari)
    449 			/*
    450 			 * Atari has a different ordening, defaults to 1.44
    451 			 */
    452 			fa.fa_deftype = &fd_types[2];
    453 #else
    454 			/*
    455 			 * Default to 1.44MB on Alpha and BeBox.  How do we tell
    456 			 * on these platforms?
    457 			 */
    458 			fa.fa_deftype = &fd_types[0];
    459 #endif /* i386 */
    460 			(void)config_found_ia(fdc->sc_dev, "fdc", (void *)&fa, fdprint);
    461 		}
    462 	}
    463 	fdc->sc_state = DEVIDLE;
    464 }
    465 
    466 int
    467 fdprobe(device_t parent, cfdata_t match, void *aux)
    468 {
    469 	struct fdc_softc *fdc = device_private(parent);
    470 	struct cfdata *cf = match;
    471 	struct fdc_attach_args *fa = aux;
    472 	int drive = fa->fa_drive;
    473 	bus_space_tag_t iot = fdc->sc_iot;
    474 	bus_space_handle_t ioh = fdc->sc_ioh;
    475 	int n;
    476 
    477 	if (cf->cf_loc[FDCCF_DRIVE] != FDCCF_DRIVE_DEFAULT &&
    478 	    cf->cf_loc[FDCCF_DRIVE] != drive)
    479 		return 0;
    480 	/*
    481 	 * XXX
    482 	 * This is to work around some odd interactions between this driver
    483 	 * and SMC Ethernet cards.
    484 	 */
    485 	if (cf->cf_loc[FDCCF_DRIVE] == FDCCF_DRIVE_DEFAULT && drive >= 2)
    486 		return 0;
    487 
    488 	/* Use PNP information if available */
    489 	if (fdc->sc_known)
    490 		return 1;
    491 
    492 	mutex_enter(&fdc->sc_mtx);
    493 	/* toss any interrupt status */
    494 	for (n = 0; n < 4; n++) {
    495 		out_fdc(iot, ioh, NE7CMD_SENSEI);
    496 		(void) fdcresult(fdc);
    497 	}
    498 	/* select drive and turn on motor */
    499 	bus_space_write_1(iot, ioh, fdout, drive | FDO_FRST | FDO_MOEN(drive));
    500 	/* wait for motor to spin up */
    501 	/* XXX check sc_probe */
    502 	(void) cv_timedwait(&fdc->sc_cv, &fdc->sc_mtx, hz / 4);
    503 	out_fdc(iot, ioh, NE7CMD_RECAL);
    504 	out_fdc(iot, ioh, drive);
    505 	/* wait for recalibrate, up to 2s */
    506 	/* XXX check sc_probe */
    507 	if (cv_timedwait(&fdc->sc_cv, &fdc->sc_mtx, 2 * hz) != EWOULDBLOCK){
    508 #ifdef FD_DEBUG
    509 		/* XXX */
    510 		printf("fdprobe: got intr\n");
    511 #endif
    512 	}
    513 	out_fdc(iot, ioh, NE7CMD_SENSEI);
    514 	n = fdcresult(fdc);
    515 #ifdef FD_DEBUG
    516 	{
    517 		int i;
    518 		printf("fdprobe: status");
    519 		for (i = 0; i < n; i++)
    520 			printf(" %x", fdc->sc_status[i]);
    521 		printf("\n");
    522 	}
    523 #endif
    524 	/* turn off motor */
    525 	bus_space_write_1(iot, ioh, fdout, FDO_FRST);
    526 	mutex_exit(&fdc->sc_mtx);
    527 
    528 #if defined(bebox)	/* XXX What is this about? --thorpej (at) NetBSD.org */
    529 	if (n != 2 || (fdc->sc_status[1] != 0))
    530 		return 0;
    531 #else
    532 	if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
    533 		return 0;
    534 #endif /* bebox */
    535 
    536 	return 1;
    537 }
    538 
    539 /*
    540  * Controller is working, and drive responded.  Attach it.
    541  */
    542 void
    543 fdattach(device_t parent, device_t self, void *aux)
    544 {
    545 	struct fdc_softc *fdc = device_private(parent);
    546 	struct fd_softc *fd = device_private(self);
    547 	struct fdc_attach_args *fa = aux;
    548 	const struct fd_type *type = fa->fa_deftype;
    549 	int drive = fa->fa_drive;
    550 
    551 	fd->sc_dev = self;
    552 
    553 	callout_init(&fd->sc_motoron_ch, 0);
    554 	callout_init(&fd->sc_motoroff_ch, 0);
    555 
    556 	/* XXX Allow `flags' to override device type? */
    557 
    558 	if (type)
    559 		aprint_normal(": %s, %d cyl, %d head, %d sec\n", type->name,
    560 		    type->cyls, type->heads, type->sectrac);
    561 	else
    562 		aprint_normal(": density unknown\n");
    563 
    564 	bufq_alloc(&fd->sc_q, "disksort", BUFQ_SORT_CYLINDER);
    565 	fd->sc_cylin = -1;
    566 	fd->sc_drive = drive;
    567 	fd->sc_deftype = type;
    568 	fdc->sc_fd[drive] = fd;
    569 
    570 	/*
    571 	 * Initialize and attach the disk structure.
    572 	 */
    573 	disk_init(&fd->sc_dk, device_xname(fd->sc_dev), &fddkdriver);
    574 	disk_attach(&fd->sc_dk);
    575 
    576 	/*
    577 	 * Establish a mountroot hook.
    578 	 */
    579 	fd->sc_roothook =
    580 	    mountroothook_establish(fd_mountroot_hook, fd->sc_dev);
    581 
    582 #if NRND > 0
    583 	rnd_attach_source(&fd->rnd_source, device_xname(fd->sc_dev),
    584 			  RND_TYPE_DISK, 0);
    585 #endif
    586 
    587 	fd_set_properties(fd);
    588 
    589 	if (!pmf_device_register(self, NULL, NULL))
    590 		aprint_error_dev(self, "cannot set power mgmt handler\n");
    591 }
    592 
    593 static int
    594 fddetach(device_t self, int flags)
    595 {
    596 	struct fd_softc *fd = device_private(self);
    597 	int bmaj, cmaj, i, mn;
    598 
    599 	fd_motor_off(fd);
    600 
    601 	/* locate the major number */
    602 	bmaj = bdevsw_lookup_major(&fd_bdevsw);
    603 	cmaj = cdevsw_lookup_major(&fd_cdevsw);
    604 
    605 	/* Nuke the vnodes for any open instances. */
    606 	for (i = 0; i < MAXPARTITIONS; i++) {
    607 		mn = DISKMINOR(device_unit(self), i);
    608 		vdevgone(bmaj, mn, mn, VBLK);
    609 		vdevgone(cmaj, mn, mn, VCHR);
    610 	}
    611 
    612 	pmf_device_deregister(self);
    613 
    614 #if 0 /* XXX need to undo at detach? */
    615 	fd_set_properties(fd);
    616 #endif
    617 #if NRND > 0
    618 	rnd_detach_source(&fd->rnd_source);
    619 #endif
    620 
    621 	disk_detach(&fd->sc_dk);
    622 
    623 	/* Kill off any queued buffers. */
    624 	bufq_drain(fd->sc_q);
    625 
    626 	bufq_free(fd->sc_q);
    627 
    628 	callout_destroy(&fd->sc_motoroff_ch);
    629 	callout_destroy(&fd->sc_motoron_ch);
    630 
    631 	return 0;
    632 }
    633 
    634 #if defined(i386)
    635 /*
    636  * Translate nvram type into internal data structure.  Return NULL for
    637  * none/unknown/unusable.
    638  */
    639 const struct fd_type *
    640 fd_nvtotype(const char *fdc, int nvraminfo, int drive)
    641 {
    642 	int type;
    643 
    644 	type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
    645 	switch (type) {
    646 	case NVRAM_DISKETTE_NONE:
    647 		return NULL;
    648 	case NVRAM_DISKETTE_12M:
    649 		return &fd_types[1];
    650 	case NVRAM_DISKETTE_TYPE5:
    651 	case NVRAM_DISKETTE_TYPE6:
    652 		/* XXX We really ought to handle 2.88MB format. */
    653 	case NVRAM_DISKETTE_144M:
    654 #if NMCA > 0
    655 		if (MCA_system)
    656 			return &mca_fd_types[0];
    657 		else
    658 #endif /* NMCA > 0 */
    659 			return &fd_types[0];
    660 	case NVRAM_DISKETTE_360K:
    661 		return &fd_types[3];
    662 	case NVRAM_DISKETTE_720K:
    663 #if NMCA > 0
    664 		if (MCA_system)
    665 			return &mca_fd_types[1];
    666 		else
    667 #endif /* NMCA > 0 */
    668 			return &fd_types[4];
    669 	default:
    670 		printf("%s: drive %d: unknown device type 0x%x\n",
    671 		    fdc, drive, type);
    672 		return NULL;
    673 	}
    674 }
    675 #endif /* i386 */
    676 
    677 static const struct fd_type *
    678 fd_dev_to_type(struct fd_softc *fd, dev_t dev)
    679 {
    680 	u_int type = FDTYPE(dev);
    681 
    682 	if (type > __arraycount(fd_types))
    683 		return NULL;
    684 	return type ? &fd_types[type - 1] : fd->sc_deftype;
    685 }
    686 
    687 void
    688 fdstrategy(struct buf *bp)
    689 {
    690 	struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(bp->b_dev));
    691 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
    692 	int sz;
    693 
    694 	/* Valid unit, controller, and request? */
    695 	if (bp->b_blkno < 0 ||
    696 	    ((bp->b_bcount % FDC_BSIZE) != 0 &&
    697 	     (bp->b_flags & B_FORMAT) == 0)) {
    698 		bp->b_error = EINVAL;
    699 		goto done;
    700 	}
    701 
    702 	/* If it's a null transfer, return immediately. */
    703 	if (bp->b_bcount == 0)
    704 		goto done;
    705 
    706 	sz = howmany(bp->b_bcount, FDC_BSIZE);
    707 
    708 	if (bp->b_blkno + sz > fd->sc_type->size) {
    709 		sz = fd->sc_type->size - bp->b_blkno;
    710 		if (sz == 0) {
    711 			/* If exactly at end of disk, return EOF. */
    712 			goto done;
    713 		}
    714 		if (sz < 0) {
    715 			/* If past end of disk, return EINVAL. */
    716 			bp->b_error = EINVAL;
    717 			goto done;
    718 		}
    719 		/* Otherwise, truncate request. */
    720 		bp->b_bcount = sz << DEV_BSHIFT;
    721 	}
    722 
    723 	bp->b_rawblkno = bp->b_blkno;
    724  	bp->b_cylinder =
    725 	    bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
    726 
    727 #ifdef FD_DEBUG
    728 	printf("fdstrategy: b_blkno %llu b_bcount %d blkno %llu cylin %d "
    729 	    "sz %d\n", (unsigned long long)bp->b_blkno, bp->b_bcount,
    730 	    (unsigned long long)fd->sc_blkno, bp->b_cylinder, sz);
    731 #endif
    732 
    733 	/* Queue transfer on drive, activate drive and controller if idle. */
    734 	mutex_enter(&fdc->sc_mtx);
    735 	BUFQ_PUT(fd->sc_q, bp);
    736 	callout_stop(&fd->sc_motoroff_ch);		/* a good idea */
    737 	if (fd->sc_active == 0)
    738 		fdstart(fd);
    739 #ifdef DIAGNOSTIC
    740 	else {
    741 		if (fdc->sc_state == DEVIDLE) {
    742 			printf("fdstrategy: controller inactive\n");
    743 			fdcstart(fdc);
    744 		}
    745 	}
    746 #endif
    747 	mutex_exit(&fdc->sc_mtx);
    748 	return;
    749 
    750 done:
    751 	/* Toss transfer; we're done early. */
    752 	bp->b_resid = bp->b_bcount;
    753 	biodone(bp);
    754 }
    755 
    756 void
    757 fdstart(struct fd_softc *fd)
    758 {
    759 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
    760 	int active = !TAILQ_EMPTY(&fdc->sc_drives);
    761 
    762 	KASSERT(mutex_owned(&fdc->sc_mtx));
    763 	/* Link into controller queue. */
    764 	fd->sc_active = 1;
    765 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    766 
    767 	/* If controller not already active, start it. */
    768 	if (!active)
    769 		fdcstart(fdc);
    770 }
    771 
    772 void
    773 fdfinish(struct fd_softc *fd, struct buf *bp)
    774 {
    775 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
    776 
    777 	/*
    778 	 * Move this drive to the end of the queue to give others a `fair'
    779 	 * chance.  We only force a switch if N operations are completed while
    780 	 * another drive is waiting to be serviced, since there is a long motor
    781 	 * startup delay whenever we switch.
    782 	 */
    783 	(void)BUFQ_GET(fd->sc_q);
    784 	if (TAILQ_NEXT(fd, sc_drivechain) && ++fd->sc_ops >= 8) {
    785 		fd->sc_ops = 0;
    786 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    787 		if (BUFQ_PEEK(fd->sc_q) != NULL)
    788 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    789 		else
    790 			fd->sc_active = 0;
    791 	}
    792 	bp->b_resid = fd->sc_bcount;
    793 	fd->sc_skip = 0;
    794 
    795 #if NRND > 0
    796 	rnd_add_uint32(&fd->rnd_source, bp->b_blkno);
    797 #endif
    798 
    799 	biodone(bp);
    800 	/* turn off motor 5s from now */
    801 	callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
    802 	fdc->sc_state = DEVIDLE;
    803 }
    804 
    805 int
    806 fdread(dev_t dev, struct uio *uio, int flags)
    807 {
    808 
    809 	return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
    810 }
    811 
    812 int
    813 fdwrite(dev_t dev, struct uio *uio, int flags)
    814 {
    815 
    816 	return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
    817 }
    818 
    819 void
    820 fd_set_motor(struct fdc_softc *fdc, int reset)
    821 {
    822 	struct fd_softc *fd;
    823 	u_char status;
    824 	int n;
    825 
    826 	if ((fd = TAILQ_FIRST(&fdc->sc_drives)) != NULL)
    827 		status = fd->sc_drive;
    828 	else
    829 		status = 0;
    830 	if (!reset)
    831 		status |= FDO_FRST | FDO_FDMAEN;
    832 	for (n = 0; n < 4; n++)
    833 		if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
    834 			status |= FDO_MOEN(n);
    835 	bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout, status);
    836 }
    837 
    838 void
    839 fd_motor_off(void *arg)
    840 {
    841 	struct fd_softc *fd = arg;
    842 	struct fdc_softc *fdc;
    843 
    844 	fdc = device_private(device_parent(fd->sc_dev));
    845 
    846 	mutex_enter(&fdc->sc_mtx);
    847 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
    848 	fd_set_motor(fdc, 0);
    849 	mutex_exit(&fdc->sc_mtx);
    850 }
    851 
    852 void
    853 fd_motor_on(void *arg)
    854 {
    855 	struct fd_softc *fd = arg;
    856 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
    857 
    858 	mutex_enter(&fdc->sc_mtx);
    859 	fd->sc_flags &= ~FD_MOTOR_WAIT;
    860 	if (TAILQ_FIRST(&fdc->sc_drives) == fd && fdc->sc_state == MOTORWAIT)
    861 		(void)fdcintr1(fdc);
    862 	mutex_exit(&fdc->sc_mtx);
    863 }
    864 
    865 int
    866 fdcresult(struct fdc_softc *fdc)
    867 {
    868 	bus_space_tag_t iot = fdc->sc_iot;
    869 	bus_space_handle_t ioh = fdc->sc_ioh;
    870 	u_char i;
    871 	u_int j = 100000,
    872 	      n = 0;
    873 
    874 	for (; j; j--) {
    875 		i = bus_space_read_1(iot, ioh, fdsts) &
    876 		    (NE7_DIO | NE7_RQM | NE7_CB);
    877 		if (i == NE7_RQM)
    878 			return n;
    879 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
    880 			if (n >= sizeof(fdc->sc_status)) {
    881 				log(LOG_ERR, "fdcresult: overrun\n");
    882 				return -1;
    883 			}
    884 			fdc->sc_status[n++] =
    885 			    bus_space_read_1(iot, ioh, fddata);
    886 		}
    887 		delay(10);
    888 	}
    889 	log(LOG_ERR, "fdcresult: timeout\n");
    890 	return -1;
    891 }
    892 
    893 int
    894 out_fdc(bus_space_tag_t iot, bus_space_handle_t ioh, u_char x)
    895 {
    896 	u_char i;
    897 	u_int j = 100000;
    898 
    899 	for (; j; j--) {
    900 		i = bus_space_read_1(iot, ioh, fdsts) &
    901 		    (NE7_DIO | NE7_RQM);
    902 		if (i == NE7_RQM) {
    903 			bus_space_write_1(iot, ioh, fddata, x);
    904 			return 0;
    905 		}
    906 		delay(10);
    907 	}
    908 	return -1;
    909 }
    910 
    911 int
    912 fdopen(dev_t dev, int flags, int mode, struct lwp *l)
    913 {
    914 	struct fd_softc *fd;
    915 	const struct fd_type *type;
    916 
    917 	fd = device_lookup_private(&fd_cd, FDUNIT(dev));
    918 	if (fd == NULL)
    919 		return (ENXIO);
    920 
    921 	type = fd_dev_to_type(fd, dev);
    922 	if (type == NULL)
    923 		return ENXIO;
    924 
    925 	if ((fd->sc_flags & FD_OPEN) != 0 &&
    926 	    memcmp(fd->sc_type, type, sizeof(*type)))
    927 		return EBUSY;
    928 
    929 	fd->sc_type_copy = *type;
    930 	fd->sc_type = &fd->sc_type_copy;
    931 	fd->sc_cylin = -1;
    932 	fd->sc_flags |= FD_OPEN;
    933 
    934 	fd_set_properties(fd);
    935 
    936 	return 0;
    937 }
    938 
    939 int
    940 fdclose(dev_t dev, int flags, int mode, struct lwp *l)
    941 {
    942 	struct fd_softc *fd =
    943 	    device_lookup_private(&fd_cd, FDUNIT(dev));
    944 
    945 	fd->sc_flags &= ~FD_OPEN;
    946 	fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
    947 	return 0;
    948 }
    949 
    950 void
    951 fdcstart(struct fdc_softc *fdc)
    952 {
    953 
    954 	KASSERT(mutex_owned(&fdc->sc_mtx));
    955 
    956 	if (!device_is_active(fdc->sc_dev))
    957 		return;
    958 
    959 #ifdef DIAGNOSTIC
    960 	/* only got here if controller's drive queue was inactive; should
    961 	   be in idle state */
    962 	if (fdc->sc_state != DEVIDLE) {
    963 		printf("fdcstart: not idle\n");
    964 		return;
    965 	}
    966 #endif
    967 	(void)fdcintr1(fdc);
    968 }
    969 
    970 void
    971 fdcstatus(device_t dv, int n, const char *s)
    972 {
    973 	struct fdc_softc *fdc = device_private(device_parent(dv));
    974 	char bits[64];
    975 
    976 	if (n == 0) {
    977 		out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI);
    978 		(void) fdcresult(fdc);
    979 		n = 2;
    980 	}
    981 
    982 	aprint_normal_dev(dv, "%s", s);
    983 
    984 	switch (n) {
    985 	case 0:
    986 		printf("\n");
    987 		break;
    988 	case 2:
    989 		printf(" (st0 %s cyl %d)\n",
    990 		    bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
    991 		    bits, sizeof(bits)), fdc->sc_status[1]);
    992 		break;
    993 	case 7:
    994 		printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
    995 		    NE7_ST0BITS, bits, sizeof(bits)));
    996 		printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
    997 		    NE7_ST1BITS, bits, sizeof(bits)));
    998 		printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
    999 		    NE7_ST2BITS, bits, sizeof(bits)));
   1000 		printf(" cyl %d head %d sec %d)\n",
   1001 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
   1002 		break;
   1003 #ifdef DIAGNOSTIC
   1004 	default:
   1005 		printf("\nfdcstatus: weird size");
   1006 		break;
   1007 #endif
   1008 	}
   1009 }
   1010 
   1011 void
   1012 fdctimeout(void *arg)
   1013 {
   1014 	struct fdc_softc *fdc = arg;
   1015 	struct fd_softc *fd = TAILQ_FIRST(&fdc->sc_drives);
   1016 
   1017 	mutex_enter(&fdc->sc_mtx);
   1018 #ifdef DEBUG
   1019 	log(LOG_ERR, "fdctimeout: state %d\n", fdc->sc_state);
   1020 #endif
   1021 	fdcstatus(fd->sc_dev, 0, "timeout");
   1022 
   1023 	if (BUFQ_PEEK(fd->sc_q) != NULL)
   1024 		fdc->sc_state++;
   1025 	else
   1026 		fdc->sc_state = DEVIDLE;
   1027 
   1028 	(void)fdcintr1(fdc);
   1029 	mutex_exit(&fdc->sc_mtx);
   1030 }
   1031 
   1032 static int
   1033 fdcintr1(struct fdc_softc *fdc)
   1034 {
   1035 #define	st0	fdc->sc_status[0]
   1036 #define	cyl	fdc->sc_status[1]
   1037 	struct fd_softc *fd;
   1038 	struct buf *bp;
   1039 	bus_space_tag_t iot = fdc->sc_iot;
   1040 	bus_space_handle_t ioh = fdc->sc_ioh;
   1041 	int read, head, sec, i, nblks;
   1042 	struct fd_type *type;
   1043 	struct ne7_fd_formb *finfo = NULL;
   1044 
   1045 	KASSERT(mutex_owned(&fdc->sc_mtx));
   1046 	if (fdc->sc_state == PROBING) {
   1047 #ifdef DEBUG
   1048 		printf("fdcintr: got probe interrupt\n");
   1049 #endif
   1050 		fdc->sc_probe++;
   1051 		goto out;
   1052 	}
   1053 
   1054 loop:
   1055 	/* Is there a drive for the controller to do a transfer with? */
   1056 	fd = TAILQ_FIRST(&fdc->sc_drives);
   1057 	if (fd == NULL) {
   1058 		fdc->sc_state = DEVIDLE;
   1059  		goto out;
   1060 	}
   1061 
   1062 	/* Is there a transfer to this drive?  If not, deactivate drive. */
   1063 	bp = BUFQ_PEEK(fd->sc_q);
   1064 	if (bp == NULL) {
   1065 		fd->sc_ops = 0;
   1066 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
   1067 		fd->sc_active = 0;
   1068 		goto loop;
   1069 	}
   1070 
   1071 	if (bp->b_flags & B_FORMAT)
   1072 		finfo = (struct ne7_fd_formb *)bp->b_data;
   1073 
   1074 	switch (fdc->sc_state) {
   1075 	case DEVIDLE:
   1076 		fdc->sc_errors = 0;
   1077 		fd->sc_skip = 0;
   1078 		fd->sc_bcount = bp->b_bcount;
   1079 		fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
   1080 		callout_stop(&fd->sc_motoroff_ch);
   1081 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
   1082 			fdc->sc_state = MOTORWAIT;
   1083 			return 1;
   1084 		}
   1085 		if ((fd->sc_flags & FD_MOTOR) == 0) {
   1086 			/* Turn on the motor, being careful about pairing. */
   1087 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
   1088 			if (ofd && ofd->sc_flags & FD_MOTOR) {
   1089 				callout_stop(&ofd->sc_motoroff_ch);
   1090 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
   1091 			}
   1092 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
   1093 			fd_set_motor(fdc, 0);
   1094 			fdc->sc_state = MOTORWAIT;
   1095 			/* Allow .25s for motor to stabilize. */
   1096 			callout_reset(&fd->sc_motoron_ch, hz / 4,
   1097 			    fd_motor_on, fd);
   1098 			return 1;
   1099 		}
   1100 		/* Make sure the right drive is selected. */
   1101 		fd_set_motor(fdc, 0);
   1102 
   1103 		/* fall through */
   1104 	case DOSEEK:
   1105 	doseek:
   1106 		if (fd->sc_cylin == bp->b_cylinder)
   1107 			goto doio;
   1108 
   1109 		out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
   1110 		out_fdc(iot, ioh, fd->sc_type->steprate);
   1111 		out_fdc(iot, ioh, 6);		/* XXX head load time == 6ms */
   1112 
   1113 		out_fdc(iot, ioh, NE7CMD_SEEK);	/* seek function */
   1114 		out_fdc(iot, ioh, fd->sc_drive); /* drive number */
   1115 		out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step);
   1116 
   1117 		fd->sc_cylin = -1;
   1118 		fdc->sc_state = SEEKWAIT;
   1119 
   1120 		iostat_seek(fd->sc_dk.dk_stats);
   1121 		disk_busy(&fd->sc_dk);
   1122 
   1123 		callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
   1124 		return 1;
   1125 
   1126 	case DOIO:
   1127 	doio:
   1128 		type = fd->sc_type;
   1129 		if (finfo)
   1130 			fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
   1131 				      (char *)finfo;
   1132 		sec = fd->sc_blkno % type->seccyl;
   1133 		nblks = type->seccyl - sec;
   1134 		nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
   1135 		nblks = min(nblks, fdc->sc_maxiosize / FDC_BSIZE);
   1136 		fd->sc_nblks = nblks;
   1137 		fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FDC_BSIZE;
   1138 		head = sec / type->sectrac;
   1139 		sec -= head * type->sectrac;
   1140 #ifdef DIAGNOSTIC
   1141 		{
   1142 			int block;
   1143 			block = (fd->sc_cylin * type->heads + head)
   1144 			    * type->sectrac + sec;
   1145 			if (block != fd->sc_blkno) {
   1146 				printf("fdcintr: block %d != blkno "
   1147 				    "%" PRId64 "\n", block, fd->sc_blkno);
   1148 #ifdef DDB
   1149 				 Debugger();
   1150 #endif
   1151 			}
   1152 		}
   1153 #endif
   1154 		read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE;
   1155 		isa_dmastart(fdc->sc_ic, fdc->sc_drq,
   1156 		    (char *)bp->b_data + fd->sc_skip, fd->sc_nbytes,
   1157 		    NULL, read | DMAMODE_DEMAND, BUS_DMA_NOWAIT);
   1158 		bus_space_write_1(iot, fdc->sc_fdctlioh, 0, type->rate);
   1159 #ifdef FD_DEBUG
   1160 		printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
   1161 			read ? "read" : "write", fd->sc_drive, fd->sc_cylin,
   1162 			head, sec, nblks);
   1163 #endif
   1164 		if (finfo) {
   1165 			/* formatting */
   1166 			if (out_fdc(iot, ioh, NE7CMD_FORMAT) < 0) {
   1167 				fdc->sc_errors = 4;
   1168 				fdcretry(fdc);
   1169 				goto loop;
   1170 			}
   1171 			out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
   1172 			out_fdc(iot, ioh, finfo->fd_formb_secshift);
   1173 			out_fdc(iot, ioh, finfo->fd_formb_nsecs);
   1174 			out_fdc(iot, ioh, finfo->fd_formb_gaplen);
   1175 			out_fdc(iot, ioh, finfo->fd_formb_fillbyte);
   1176 		} else {
   1177 			if (read)
   1178 				out_fdc(iot, ioh, NE7CMD_READ);	/* READ */
   1179 			else
   1180 				out_fdc(iot, ioh, NE7CMD_WRITE); /* WRITE */
   1181 			out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
   1182 			out_fdc(iot, ioh, fd->sc_cylin); /* track */
   1183 			out_fdc(iot, ioh, head);
   1184 			out_fdc(iot, ioh, sec + 1);	 /* sector +1 */
   1185 			out_fdc(iot, ioh, type->secsize);/* sector size */
   1186 			out_fdc(iot, ioh, type->sectrac);/* sectors/track */
   1187 			out_fdc(iot, ioh, type->gap1);	 /* gap1 size */
   1188 			out_fdc(iot, ioh, type->datalen);/* data length */
   1189 		}
   1190 		fdc->sc_state = IOCOMPLETE;
   1191 
   1192 		disk_busy(&fd->sc_dk);
   1193 
   1194 		/* allow 2 seconds for operation */
   1195 		callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
   1196 		return 1;				/* will return later */
   1197 
   1198 	case SEEKWAIT:
   1199 		callout_stop(&fdc->sc_timo_ch);
   1200 		fdc->sc_state = SEEKCOMPLETE;
   1201 		/* allow 1/50 second for heads to settle */
   1202 		callout_reset(&fdc->sc_intr_ch, hz / 50, fdcintrcb, fdc);
   1203 		return 1;
   1204 
   1205 	case SEEKCOMPLETE:
   1206 		/* no data on seek */
   1207 		disk_unbusy(&fd->sc_dk, 0, 0);
   1208 
   1209 		/* Make sure seek really happened. */
   1210 		out_fdc(iot, ioh, NE7CMD_SENSEI);
   1211 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
   1212 		    cyl != bp->b_cylinder * fd->sc_type->step) {
   1213 #ifdef FD_DEBUG
   1214 			fdcstatus(&fd->sc_dev, 2, "seek failed");
   1215 #endif
   1216 			fdcretry(fdc);
   1217 			goto loop;
   1218 		}
   1219 		fd->sc_cylin = bp->b_cylinder;
   1220 		goto doio;
   1221 
   1222 	case IOTIMEDOUT:
   1223 		isa_dmaabort(fdc->sc_ic, fdc->sc_drq);
   1224 	case SEEKTIMEDOUT:
   1225 	case RECALTIMEDOUT:
   1226 	case RESETTIMEDOUT:
   1227 		fdcretry(fdc);
   1228 		goto loop;
   1229 
   1230 	case IOCOMPLETE: /* IO DONE, post-analyze */
   1231 		callout_stop(&fdc->sc_timo_ch);
   1232 
   1233 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
   1234 		    (bp->b_flags & B_READ));
   1235 
   1236 		if (fdcresult(fdc) != 7 || (st0 & 0xf8) != 0) {
   1237 			isa_dmaabort(fdc->sc_ic, fdc->sc_drq);
   1238 #ifdef FD_DEBUG
   1239 			fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
   1240 			    "read failed" : "write failed");
   1241 			printf("blkno %llu nblks %d\n",
   1242 			    (unsigned long long)fd->sc_blkno, fd->sc_nblks);
   1243 #endif
   1244 			fdcretry(fdc);
   1245 			goto loop;
   1246 		}
   1247 		isa_dmadone(fdc->sc_ic, fdc->sc_drq);
   1248 		if (fdc->sc_errors) {
   1249 			diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF,
   1250 			    fd->sc_skip / FDC_BSIZE, NULL);
   1251 			printf("\n");
   1252 			fdc->sc_errors = 0;
   1253 		}
   1254 		fd->sc_blkno += fd->sc_nblks;
   1255 		fd->sc_skip += fd->sc_nbytes;
   1256 		fd->sc_bcount -= fd->sc_nbytes;
   1257 		if (!finfo && fd->sc_bcount > 0) {
   1258 			bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
   1259 			goto doseek;
   1260 		}
   1261 		fdfinish(fd, bp);
   1262 		goto loop;
   1263 
   1264 	case DORESET:
   1265 		/* try a reset, keep motor on */
   1266 		fd_set_motor(fdc, 1);
   1267 		delay(100);
   1268 		fd_set_motor(fdc, 0);
   1269 		fdc->sc_state = RESETCOMPLETE;
   1270 		callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
   1271 		return 1;			/* will return later */
   1272 
   1273 	case RESETCOMPLETE:
   1274 		callout_stop(&fdc->sc_timo_ch);
   1275 		/* clear the controller output buffer */
   1276 		for (i = 0; i < 4; i++) {
   1277 			out_fdc(iot, ioh, NE7CMD_SENSEI);
   1278 			(void) fdcresult(fdc);
   1279 		}
   1280 
   1281 		/* fall through */
   1282 	case DORECAL:
   1283 		out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */
   1284 		out_fdc(iot, ioh, fd->sc_drive);
   1285 		fdc->sc_state = RECALWAIT;
   1286 		callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
   1287 		return 1;			/* will return later */
   1288 
   1289 	case RECALWAIT:
   1290 		callout_stop(&fdc->sc_timo_ch);
   1291 		fdc->sc_state = RECALCOMPLETE;
   1292 		/* allow 1/30 second for heads to settle */
   1293 		callout_reset(&fdc->sc_intr_ch, hz / 30, fdcintrcb, fdc);
   1294 		return 1;			/* will return later */
   1295 
   1296 	case RECALCOMPLETE:
   1297 		out_fdc(iot, ioh, NE7CMD_SENSEI);
   1298 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
   1299 #ifdef FD_DEBUG
   1300 			fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
   1301 #endif
   1302 			fdcretry(fdc);
   1303 			goto loop;
   1304 		}
   1305 		fd->sc_cylin = 0;
   1306 		goto doseek;
   1307 
   1308 	case MOTORWAIT:
   1309 		if (fd->sc_flags & FD_MOTOR_WAIT)
   1310 			return 1;		/* time's not up yet */
   1311 		goto doseek;
   1312 
   1313 	default:
   1314 		fdcstatus(fd->sc_dev, 0, "stray interrupt");
   1315 		return 1;
   1316 	}
   1317 #undef	st0
   1318 #undef	cyl
   1319 
   1320 out:
   1321 	cv_signal(&fdc->sc_cv);
   1322 	return 1;
   1323 }
   1324 
   1325 static void
   1326 fdcintrcb(void *arg)
   1327 {
   1328 	(void)fdcintr(arg);
   1329 }
   1330 
   1331 int
   1332 fdcintr(void *arg)
   1333 {
   1334 	int rc;
   1335 	struct fdc_softc *fdc = arg;
   1336 
   1337 	mutex_enter(&fdc->sc_mtx);
   1338 	rc = fdcintr1(fdc);
   1339 	mutex_exit(&fdc->sc_mtx);
   1340 	return rc;
   1341 }
   1342 
   1343 void
   1344 fdcretry(struct fdc_softc *fdc)
   1345 {
   1346 	char bits[64];
   1347 	struct fd_softc *fd;
   1348 	struct buf *bp;
   1349 
   1350 	fd = TAILQ_FIRST(&fdc->sc_drives);
   1351 	bp = BUFQ_PEEK(fd->sc_q);
   1352 
   1353 	if (fd->sc_opts & FDOPT_NORETRY)
   1354 	    goto fail;
   1355 	switch (fdc->sc_errors) {
   1356 	case 0:
   1357 		/* try again */
   1358 		fdc->sc_state = DOSEEK;
   1359 		break;
   1360 
   1361 	case 1: case 2: case 3:
   1362 		/* didn't work; try recalibrating */
   1363 		fdc->sc_state = DORECAL;
   1364 		break;
   1365 
   1366 	case 4:
   1367 		/* still no go; reset the bastard */
   1368 		fdc->sc_state = DORESET;
   1369 		break;
   1370 
   1371 	default:
   1372 	fail:
   1373 		if ((fd->sc_opts & FDOPT_SILENT) == 0) {
   1374 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
   1375 				fd->sc_skip / FDC_BSIZE, NULL);
   1376 
   1377 			printf(" (st0 %s",
   1378 			       bitmask_snprintf(fdc->sc_status[0],
   1379 						NE7_ST0BITS, bits,
   1380 						sizeof(bits)));
   1381 			printf(" st1 %s",
   1382 			       bitmask_snprintf(fdc->sc_status[1],
   1383 						NE7_ST1BITS, bits,
   1384 						sizeof(bits)));
   1385 			printf(" st2 %s",
   1386 			       bitmask_snprintf(fdc->sc_status[2],
   1387 						NE7_ST2BITS, bits,
   1388 						sizeof(bits)));
   1389 			printf(" cyl %d head %d sec %d)\n",
   1390 			       fdc->sc_status[3],
   1391 			       fdc->sc_status[4],
   1392 			       fdc->sc_status[5]);
   1393 		}
   1394 
   1395 		bp->b_error = EIO;
   1396 		fdfinish(fd, bp);
   1397 	}
   1398 	fdc->sc_errors++;
   1399 }
   1400 
   1401 int
   1402 fdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   1403 {
   1404 	struct fd_softc *fd =
   1405 	    device_lookup_private(&fd_cd, FDUNIT(dev));
   1406 	struct fdformat_parms *form_parms;
   1407 	struct fdformat_cmd *form_cmd;
   1408 	struct ne7_fd_formb *fd_formb;
   1409 	struct disklabel buffer;
   1410 	int error;
   1411 	unsigned int scratch;
   1412 	int il[FD_MAX_NSEC + 1];
   1413 	int i, j;
   1414 #ifdef __HAVE_OLD_DISKLABEL
   1415 	struct disklabel newlabel;
   1416 #endif
   1417 
   1418 	switch (cmd) {
   1419 	case DIOCGDINFO:
   1420 #ifdef __HAVE_OLD_DISKLABEL
   1421 	case ODIOCGDINFO:
   1422 #endif
   1423 		memset(&buffer, 0, sizeof(buffer));
   1424 
   1425 		buffer.d_secpercyl = fd->sc_type->seccyl;
   1426 		buffer.d_type = DTYPE_FLOPPY;
   1427 		buffer.d_secsize = FDC_BSIZE;
   1428 
   1429 		if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
   1430 			return EINVAL;
   1431 
   1432 #ifdef __HAVE_OLD_DISKLABEL
   1433 		if (cmd == ODIOCGDINFO) {
   1434 			if (buffer.d_npartitions > OLDMAXPARTITIONS)
   1435 				return ENOTTY;
   1436 			memcpy(addr, &buffer, sizeof (struct olddisklabel));
   1437 		} else
   1438 #endif
   1439 		*(struct disklabel *)addr = buffer;
   1440 		return 0;
   1441 
   1442 	case DIOCWLABEL:
   1443 		if ((flag & FWRITE) == 0)
   1444 			return EBADF;
   1445 		/* XXX do something */
   1446 		return 0;
   1447 
   1448 	case DIOCWDINFO:
   1449 #ifdef __HAVE_OLD_DISKLABEL
   1450 	case ODIOCWDINFO:
   1451 #endif
   1452 	{
   1453 		struct disklabel *lp;
   1454 
   1455 		if ((flag & FWRITE) == 0)
   1456 			return EBADF;
   1457 #ifdef __HAVE_OLD_DISKLABEL
   1458 		if (cmd == ODIOCWDINFO) {
   1459 			memset(&newlabel, 0, sizeof newlabel);
   1460 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
   1461 			lp = &newlabel;
   1462 		} else
   1463 #endif
   1464 		lp = (struct disklabel *)addr;
   1465 
   1466 		error = setdisklabel(&buffer, lp, 0, NULL);
   1467 		if (error)
   1468 			return error;
   1469 
   1470 		error = writedisklabel(dev, fdstrategy, &buffer, NULL);
   1471 		return error;
   1472 	}
   1473 
   1474 	case FDIOCGETFORMAT:
   1475 		form_parms = (struct fdformat_parms *)addr;
   1476 		form_parms->fdformat_version = FDFORMAT_VERSION;
   1477 		form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
   1478 		form_parms->ncyl = fd->sc_type->cyls;
   1479 		form_parms->nspt = fd->sc_type->sectrac;
   1480 		form_parms->ntrk = fd->sc_type->heads;
   1481 		form_parms->stepspercyl = fd->sc_type->step;
   1482 		form_parms->gaplen = fd->sc_type->gap2;
   1483 		form_parms->fillbyte = fd->sc_type->fillbyte;
   1484 		form_parms->interleave = fd->sc_type->interleave;
   1485 		switch (fd->sc_type->rate) {
   1486 		case FDC_500KBPS:
   1487 			form_parms->xfer_rate = 500 * 1024;
   1488 			break;
   1489 		case FDC_300KBPS:
   1490 			form_parms->xfer_rate = 300 * 1024;
   1491 			break;
   1492 		case FDC_250KBPS:
   1493 			form_parms->xfer_rate = 250 * 1024;
   1494 			break;
   1495 		default:
   1496 			return EINVAL;
   1497 		}
   1498 		return 0;
   1499 
   1500 	case FDIOCSETFORMAT:
   1501 		if((flag & FWRITE) == 0)
   1502 			return EBADF;	/* must be opened for writing */
   1503 		form_parms = (struct fdformat_parms *)addr;
   1504 		if (form_parms->fdformat_version != FDFORMAT_VERSION)
   1505 			return EINVAL;	/* wrong version of formatting prog */
   1506 
   1507 		scratch = form_parms->nbps >> 7;
   1508 		if ((form_parms->nbps & 0x7f) || ffs(scratch) == 0 ||
   1509 		    scratch & ~(1 << (ffs(scratch)-1)))
   1510 			/* not a power-of-two multiple of 128 */
   1511 			return EINVAL;
   1512 
   1513 		switch (form_parms->xfer_rate) {
   1514 		case 500 * 1024:
   1515 			fd->sc_type->rate = FDC_500KBPS;
   1516 			break;
   1517 		case 300 * 1024:
   1518 			fd->sc_type->rate = FDC_300KBPS;
   1519 			break;
   1520 		case 250 * 1024:
   1521 			fd->sc_type->rate = FDC_250KBPS;
   1522 			break;
   1523 		default:
   1524 			return EINVAL;
   1525 		}
   1526 
   1527 		if (form_parms->nspt > FD_MAX_NSEC ||
   1528 		    form_parms->fillbyte > 0xff ||
   1529 		    form_parms->interleave > 0xff)
   1530 			return EINVAL;
   1531 		fd->sc_type->sectrac = form_parms->nspt;
   1532 		if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
   1533 			return EINVAL;
   1534 		fd->sc_type->heads = form_parms->ntrk;
   1535 		fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
   1536 		fd->sc_type->secsize = ffs(scratch)-1;
   1537 		fd->sc_type->gap2 = form_parms->gaplen;
   1538 		fd->sc_type->cyls = form_parms->ncyl;
   1539 		fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
   1540 		    form_parms->nbps / DEV_BSIZE;
   1541 		fd->sc_type->step = form_parms->stepspercyl;
   1542 		fd->sc_type->fillbyte = form_parms->fillbyte;
   1543 		fd->sc_type->interleave = form_parms->interleave;
   1544 		return 0;
   1545 
   1546 	case FDIOCFORMAT_TRACK:
   1547 		if((flag & FWRITE) == 0)
   1548 			return EBADF;	/* must be opened for writing */
   1549 		form_cmd = (struct fdformat_cmd *)addr;
   1550 		if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
   1551 			return EINVAL;	/* wrong version of formatting prog */
   1552 
   1553 		if (form_cmd->head >= fd->sc_type->heads ||
   1554 		    form_cmd->cylinder >= fd->sc_type->cyls) {
   1555 			return EINVAL;
   1556 		}
   1557 
   1558 		fd_formb = malloc(sizeof(struct ne7_fd_formb),
   1559 		    M_TEMP, M_NOWAIT);
   1560 		if (fd_formb == 0)
   1561 			return ENOMEM;
   1562 
   1563 		fd_formb->head = form_cmd->head;
   1564 		fd_formb->cyl = form_cmd->cylinder;
   1565 		fd_formb->transfer_rate = fd->sc_type->rate;
   1566 		fd_formb->fd_formb_secshift = fd->sc_type->secsize;
   1567 		fd_formb->fd_formb_nsecs = fd->sc_type->sectrac;
   1568 		fd_formb->fd_formb_gaplen = fd->sc_type->gap2;
   1569 		fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte;
   1570 
   1571 		memset(il, 0, sizeof il);
   1572 		for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) {
   1573 			while (il[(j%fd_formb->fd_formb_nsecs)+1])
   1574 				j++;
   1575 			il[(j%fd_formb->fd_formb_nsecs)+1] = i;
   1576 			j += fd->sc_type->interleave;
   1577 		}
   1578 		for (i = 0; i < fd_formb->fd_formb_nsecs; i++) {
   1579 			fd_formb->fd_formb_cylno(i) = form_cmd->cylinder;
   1580 			fd_formb->fd_formb_headno(i) = form_cmd->head;
   1581 			fd_formb->fd_formb_secno(i) = il[i+1];
   1582 			fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize;
   1583 		}
   1584 
   1585 		error = fdformat(dev, fd_formb, l);
   1586 		free(fd_formb, M_TEMP);
   1587 		return error;
   1588 
   1589 	case FDIOCGETOPTS:		/* get drive options */
   1590 		*(int *)addr = fd->sc_opts;
   1591 		return 0;
   1592 
   1593 	case FDIOCSETOPTS:		/* set drive options */
   1594 		fd->sc_opts = *(int *)addr;
   1595 		return 0;
   1596 
   1597 	default:
   1598 		return ENOTTY;
   1599 	}
   1600 
   1601 #ifdef DIAGNOSTIC
   1602 	panic("fdioctl: impossible");
   1603 #endif
   1604 }
   1605 
   1606 int
   1607 fdformat(dev_t dev, struct ne7_fd_formb *finfo, struct lwp *l)
   1608 {
   1609 	int rv = 0;
   1610 	struct fd_softc *fd =
   1611 	    device_lookup_private(&fd_cd, FDUNIT(dev));
   1612 	struct fd_type *type = fd->sc_type;
   1613 	struct buf *bp;
   1614 
   1615 	/* set up a buffer header for fdstrategy() */
   1616 	bp = getiobuf(NULL, false);
   1617 	if (bp == NULL)
   1618 		return ENOBUFS;
   1619 
   1620 	bp->b_cflags = BC_BUSY;
   1621 	bp->b_flags = B_PHYS | B_FORMAT;
   1622 	bp->b_proc = l->l_proc;
   1623 	bp->b_dev = dev;
   1624 
   1625 	/*
   1626 	 * calculate a fake blkno, so fdstrategy() would initiate a
   1627 	 * seek to the requested cylinder
   1628 	 */
   1629 	bp->b_blkno = (finfo->cyl * (type->sectrac * type->heads)
   1630 		       + finfo->head * type->sectrac) * FDC_BSIZE / DEV_BSIZE;
   1631 
   1632 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
   1633 	bp->b_data = (void *)finfo;
   1634 
   1635 #ifdef DEBUG
   1636 	printf("fdformat: blkno %" PRIx64 " count %x\n",
   1637 	    bp->b_blkno, bp->b_bcount);
   1638 #endif
   1639 
   1640 	/* now do the format */
   1641 	fdstrategy(bp);
   1642 
   1643 	/* ...and wait for it to complete */
   1644 	rv = biowait(bp);
   1645 	putiobuf(bp);
   1646 	return rv;
   1647 }
   1648 
   1649 /*
   1650  * Mountroot hook: prompt the user to enter the root file system
   1651  * floppy.
   1652  */
   1653 void
   1654 fd_mountroot_hook(device_t dev)
   1655 {
   1656 	int c;
   1657 
   1658 	printf("Insert filesystem floppy and press return.");
   1659 	cnpollc(1);
   1660 	for (;;) {
   1661 		c = cngetc();
   1662 		if ((c == '\r') || (c == '\n')) {
   1663 			printf("\n");
   1664 			break;
   1665 		}
   1666 	}
   1667 	cnpollc(0);
   1668 }
   1669 
   1670 static void
   1671 fd_set_properties(struct fd_softc *fd)
   1672 {
   1673 	prop_dictionary_t disk_info, odisk_info, geom;
   1674 	const struct fd_type *fdt;
   1675 	int secsize;
   1676 
   1677 	fdt = fd->sc_type;
   1678 	if (fdt == NULL) {
   1679 		fdt = fd->sc_deftype;
   1680 		if (fdt == NULL)
   1681 			return;
   1682 	}
   1683 
   1684 	disk_info = prop_dictionary_create();
   1685 
   1686 	geom = prop_dictionary_create();
   1687 
   1688 	prop_dictionary_set_uint64(geom, "sectors-per-unit",
   1689 	    fdt->size);
   1690 
   1691 	switch (fdt->secsize) {
   1692 	case 2:
   1693 		secsize = 512;
   1694 		break;
   1695 	case 3:
   1696 		secsize = 1024;
   1697 		break;
   1698 	default:
   1699 		secsize = 0;
   1700 	}
   1701 
   1702 	prop_dictionary_set_uint32(geom, "sector-size",
   1703 	    secsize);
   1704 
   1705 	prop_dictionary_set_uint16(geom, "sectors-per-track",
   1706 	    fdt->sectrac);
   1707 
   1708 	prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
   1709 	    fdt->heads);
   1710 
   1711 	prop_dictionary_set_uint64(geom, "cylinders-per-unit",
   1712 	    fdt->cyls);
   1713 
   1714 	prop_dictionary_set(disk_info, "geometry", geom);
   1715 	prop_object_release(geom);
   1716 
   1717 	prop_dictionary_set(device_properties(fd->sc_dev),
   1718 	    "disk-info", disk_info);
   1719 
   1720 	/*
   1721 	 * Don't release disk_info here; we keep a reference to it.
   1722 	 * disk_detach() will release it when we go away.
   1723 	 */
   1724 
   1725 	odisk_info = fd->sc_dk.dk_info;
   1726 	fd->sc_dk.dk_info = disk_info;
   1727 	if (odisk_info)
   1728 		prop_object_release(odisk_info);
   1729 }
   1730