Home | History | Annotate | Line # | Download | only in ata
wd.c revision 1.226
      1 /*	$NetBSD: wd.c,v 1.226 2002/09/27 17:17:53 drochner Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1998, 2001 Manuel Bouyer.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *	notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *	notice, this list of conditions and the following disclaimer in the
     13  *	documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *	must display the following acknowledgement:
     16  *  This product includes software developed by Manuel Bouyer.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *	derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1998 The NetBSD Foundation, Inc.
     34  * All rights reserved.
     35  *
     36  * This code is derived from software contributed to The NetBSD Foundation
     37  * by Charles M. Hannum and by Onno van der Linden.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. All advertising materials mentioning features or use of this software
     48  *    must display the following acknowledgement:
     49  *        This product includes software developed by the NetBSD
     50  *        Foundation, Inc. and its contributors.
     51  * 4. Neither the name of The NetBSD Foundation nor the names of its
     52  *    contributors may be used to endorse or promote products derived
     53  *    from this software without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     56  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     57  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     58  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     59  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     60  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     61  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     62  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     63  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     64  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     65  * POSSIBILITY OF SUCH DAMAGE.
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: wd.c,v 1.226 2002/09/27 17:17:53 drochner Exp $");
     70 
     71 #ifndef WDCDEBUG
     72 #define WDCDEBUG
     73 #endif /* WDCDEBUG */
     74 
     75 #include "rnd.h"
     76 
     77 #include <sys/param.h>
     78 #include <sys/systm.h>
     79 #include <sys/kernel.h>
     80 #include <sys/conf.h>
     81 #include <sys/file.h>
     82 #include <sys/stat.h>
     83 #include <sys/ioctl.h>
     84 #include <sys/buf.h>
     85 #include <sys/uio.h>
     86 #include <sys/malloc.h>
     87 #include <sys/device.h>
     88 #include <sys/disklabel.h>
     89 #include <sys/disk.h>
     90 #include <sys/syslog.h>
     91 #include <sys/proc.h>
     92 #include <sys/vnode.h>
     93 #if NRND > 0
     94 #include <sys/rnd.h>
     95 #endif
     96 
     97 #include <machine/intr.h>
     98 #include <machine/bus.h>
     99 
    100 #include <dev/ata/atareg.h>
    101 #include <dev/ata/atavar.h>
    102 #include <dev/ata/wdvar.h>
    103 #include <dev/ic/wdcreg.h>
    104 #include <sys/ataio.h>
    105 #include "locators.h"
    106 
    107 #define	WAITTIME	(4 * hz)	/* time to wait for a completion */
    108 #define	WDIORETRIES_SINGLE 4	/* number of retries before single-sector */
    109 #define	WDIORETRIES	5	/* number of retries before giving up */
    110 #define	RECOVERYTIME hz/2	/* time to wait before retrying a cmd */
    111 
    112 #define	WDUNIT(dev)		DISKUNIT(dev)
    113 #define	WDPART(dev)		DISKPART(dev)
    114 #define	WDMINOR(unit, part)	DISKMINOR(unit, part)
    115 #define	MAKEWDDEV(maj, unit, part)	MAKEDISKDEV(maj, unit, part)
    116 
    117 #define	WDLABELDEV(dev)	(MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART))
    118 
    119 #define DEBUG_INTR   0x01
    120 #define DEBUG_XFERS  0x02
    121 #define DEBUG_STATUS 0x04
    122 #define DEBUG_FUNCS  0x08
    123 #define DEBUG_PROBE  0x10
    124 #ifdef WDCDEBUG
    125 extern int wdcdebug_wd_mask; /* init'ed in ata_wdc.c */
    126 #define WDCDEBUG_PRINT(args, level) \
    127 	if (wdcdebug_wd_mask & (level)) \
    128 		printf args
    129 #else
    130 #define WDCDEBUG_PRINT(args, level)
    131 #endif
    132 
    133 struct wd_softc {
    134 	/* General disk infos */
    135 	struct device sc_dev;
    136 	struct disk sc_dk;
    137 	struct bufq_state sc_q;
    138 	struct callout sc_restart_ch;
    139 	/* IDE disk soft states */
    140 	struct ata_bio sc_wdc_bio; /* current transfer */
    141 	struct buf *sc_bp; /* buf being transfered */
    142 	void *wdc_softc;   /* pointer to our parent */
    143 	struct ata_drive_datas *drvp; /* Our controller's infos */
    144 	const struct ata_bustype *atabus;
    145 	int openings;
    146 	struct ataparams sc_params;/* drive characteistics found */
    147 	int sc_flags;
    148 #define	WDF_LOCKED	0x001
    149 #define	WDF_WANTED	0x002
    150 #define	WDF_WLABEL	0x004 /* label is writable */
    151 #define	WDF_LABELLING	0x008 /* writing label */
    152 /*
    153  * XXX Nothing resets this yet, but disk change sensing will when ATA-4 is
    154  * more fully implemented.
    155  */
    156 #define WDF_LOADED	0x010 /* parameters loaded */
    157 #define WDF_WAIT	0x020 /* waiting for resources */
    158 #define WDF_LBA		0x040 /* using LBA mode */
    159 #define WDF_KLABEL	0x080 /* retain label after 'full' close */
    160 #define WDF_LBA48	0x100 /* using 48-bit LBA mode */
    161 	int sc_capacity;
    162 	int cyl; /* actual drive parameters */
    163 	int heads;
    164 	int sectors;
    165 	int retries; /* number of xfer retry */
    166 
    167 	void *sc_sdhook;		/* our shutdown hook */
    168 
    169 #if NRND > 0
    170 	rndsource_element_t	rnd_source;
    171 #endif
    172 };
    173 
    174 #define sc_drive sc_wdc_bio.drive
    175 #define sc_mode sc_wdc_bio.mode
    176 #define sc_multi sc_wdc_bio.multi
    177 #define sc_badsect sc_wdc_bio.badsect
    178 
    179 int	wdprobe		__P((struct device *, struct cfdata *, void *));
    180 void	wdattach	__P((struct device *, struct device *, void *));
    181 int	wddetach __P((struct device *, int));
    182 int	wdactivate __P((struct device *, enum devact));
    183 int	wdprint	__P((void *, char *));
    184 void	wdperror __P((const struct wd_softc *));
    185 
    186 struct cfattach wd_ca = {
    187 	sizeof(struct wd_softc), wdprobe, wdattach, wddetach, wdactivate
    188 };
    189 
    190 extern struct cfdriver wd_cd;
    191 
    192 dev_type_open(wdopen);
    193 dev_type_close(wdclose);
    194 dev_type_read(wdread);
    195 dev_type_write(wdwrite);
    196 dev_type_ioctl(wdioctl);
    197 dev_type_strategy(wdstrategy);
    198 dev_type_dump(wddump);
    199 dev_type_size(wdsize);
    200 
    201 const struct bdevsw wd_bdevsw = {
    202 	wdopen, wdclose, wdstrategy, wdioctl, wddump, wdsize, D_DISK
    203 };
    204 
    205 const struct cdevsw wd_cdevsw = {
    206 	wdopen, wdclose, wdread, wdwrite, wdioctl,
    207 	nostop, notty, nopoll, nommap, D_DISK
    208 };
    209 
    210 /*
    211  * Glue necessary to hook WDCIOCCOMMAND into physio
    212  */
    213 
    214 struct wd_ioctl {
    215 	LIST_ENTRY(wd_ioctl) wi_list;
    216 	struct buf wi_bp;
    217 	struct uio wi_uio;
    218 	struct iovec wi_iov;
    219 	atareq_t wi_atareq;
    220 	struct wd_softc *wi_softc;
    221 };
    222 
    223 LIST_HEAD(, wd_ioctl) wi_head;
    224 
    225 struct	wd_ioctl *wi_find __P((struct buf *));
    226 void	wi_free __P((struct wd_ioctl *));
    227 struct	wd_ioctl *wi_get __P((void));
    228 void	wdioctlstrategy __P((struct buf *));
    229 
    230 void  wdgetdefaultlabel __P((struct wd_softc *, struct disklabel *));
    231 void  wdgetdisklabel	__P((struct wd_softc *));
    232 void  wdstart	__P((void *));
    233 void  __wdstart	__P((struct wd_softc*, struct buf *));
    234 void  wdrestart __P((void*));
    235 int   wd_get_params __P((struct wd_softc *, u_int8_t, struct ataparams *));
    236 void  wd_flushcache __P((struct wd_softc *, int));
    237 void  wd_shutdown __P((void*));
    238 
    239 struct dkdriver wddkdriver = { wdstrategy };
    240 
    241 #ifdef HAS_BAD144_HANDLING
    242 static void bad144intern __P((struct wd_softc *));
    243 #endif
    244 int	wdlock	__P((struct wd_softc *));
    245 void	wdunlock	__P((struct wd_softc *));
    246 
    247 int
    248 wdprobe(parent, match, aux)
    249 	struct device *parent;
    250 	struct cfdata *match;
    251 
    252 	void *aux;
    253 {
    254 	struct ata_device *adev = aux;
    255 
    256 	if (adev == NULL)
    257 		return 0;
    258 	if (adev->adev_bustype->bustype_type != SCSIPI_BUSTYPE_ATA)
    259 		return 0;
    260 
    261 	if (match->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT &&
    262 	    match->cf_loc[ATACF_CHANNEL] != adev->adev_channel)
    263 		return 0;
    264 
    265 	if (match->cf_loc[ATACF_DRIVE] != ATACF_DRIVE_DEFAULT &&
    266 	    match->cf_loc[ATACF_DRIVE] != adev->adev_drv_data->drive)
    267 		return 0;
    268 	return 1;
    269 }
    270 
    271 void
    272 wdattach(parent, self, aux)
    273 	struct device *parent, *self;
    274 	void *aux;
    275 {
    276 	struct wd_softc *wd = (void *)self;
    277 	struct ata_device *adev= aux;
    278 	int i, blank;
    279 	char buf[41], pbuf[9], c, *p, *q;
    280 	WDCDEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
    281 
    282 	callout_init(&wd->sc_restart_ch);
    283 	bufq_alloc(&wd->sc_q, BUFQ_DISKSORT|BUFQ_SORT_RAWBLOCK);
    284 
    285 	wd->atabus = adev->adev_bustype;
    286 	wd->openings = adev->adev_openings;
    287 	wd->drvp = adev->adev_drv_data;;
    288 	wd->wdc_softc = parent;
    289 	/* give back our softc to our caller */
    290 	wd->drvp->drv_softc = &wd->sc_dev;
    291 
    292 	/* read our drive info */
    293 	if (wd_get_params(wd, AT_POLL, &wd->sc_params) != 0) {
    294 		printf("%s: IDENTIFY failed\n", wd->sc_dev.dv_xname);
    295 		return;
    296 	}
    297 
    298 	for (blank = 0, p = wd->sc_params.atap_model, q = buf, i = 0;
    299 	    i < sizeof(wd->sc_params.atap_model); i++) {
    300 		c = *p++;
    301 		if (c == '\0')
    302 			break;
    303 		if (c != ' ') {
    304 			if (blank) {
    305 				*q++ = ' ';
    306 				blank = 0;
    307 			}
    308 			*q++ = c;
    309 		} else
    310 			blank = 1;
    311 	}
    312 	*q++ = '\0';
    313 
    314 	printf(": <%s>\n", buf);
    315 
    316 	if ((wd->sc_params.atap_multi & 0xff) > 1) {
    317 		wd->sc_multi = wd->sc_params.atap_multi & 0xff;
    318 	} else {
    319 		wd->sc_multi = 1;
    320 	}
    321 
    322 	printf("%s: drive supports %d-sector PIO transfers,",
    323 	    wd->sc_dev.dv_xname, wd->sc_multi);
    324 
    325 	/* 48-bit LBA addressing */
    326 	if ((wd->sc_params.atap_cmd2_en & WDC_CAP_LBA48) != 0)
    327 		wd->sc_flags |= WDF_LBA48;
    328 
    329 	/* Prior to ATA-4, LBA was optional. */
    330 	if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0)
    331 		wd->sc_flags |= WDF_LBA;
    332 #if 0
    333 	/* ATA-4 requires LBA. */
    334 	if (wd->sc_params.atap_ataversion != 0xffff &&
    335 	    wd->sc_params.atap_ataversion >= WDC_VER_ATA4)
    336 		wd->sc_flags |= WDF_LBA;
    337 #endif
    338 
    339 	if ((wd->sc_flags & WDF_LBA48) != 0) {
    340 		printf(" LBA48 addressing\n");
    341 		wd->sc_capacity =
    342 		    ((u_int64_t) wd->sc_params.__reserved6[11] << 48) |
    343 		    ((u_int64_t) wd->sc_params.__reserved6[10] << 32) |
    344 		    ((u_int64_t) wd->sc_params.__reserved6[9]  << 16) |
    345 		    ((u_int64_t) wd->sc_params.__reserved6[8]  << 0);
    346 	} else if ((wd->sc_flags & WDF_LBA) != 0) {
    347 		printf(" LBA addressing\n");
    348 		wd->sc_capacity =
    349 		    (wd->sc_params.atap_capacity[1] << 16) |
    350 		    wd->sc_params.atap_capacity[0];
    351 	} else {
    352 		printf(" chs addressing\n");
    353 		wd->sc_capacity =
    354 		    wd->sc_params.atap_cylinders *
    355 		    wd->sc_params.atap_heads *
    356 		    wd->sc_params.atap_sectors;
    357 	}
    358 	format_bytes(pbuf, sizeof(pbuf),
    359 	    (u_int64_t)wd->sc_capacity * DEV_BSIZE);
    360 	printf("%s: %s, %d cyl, %d head, %d sec, "
    361 	    "%d bytes/sect x %d sectors\n",
    362 	    self->dv_xname, pbuf, wd->sc_params.atap_cylinders,
    363 	    wd->sc_params.atap_heads, wd->sc_params.atap_sectors,
    364 	    DEV_BSIZE, wd->sc_capacity);
    365 
    366 	WDCDEBUG_PRINT(("%s: atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n",
    367 	    self->dv_xname, wd->sc_params.atap_dmatiming_mimi,
    368 	    wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE);
    369 	/*
    370 	 * Initialize and attach the disk structure.
    371 	 */
    372 	wd->sc_dk.dk_driver = &wddkdriver;
    373 	wd->sc_dk.dk_name = wd->sc_dev.dv_xname;
    374 	disk_attach(&wd->sc_dk);
    375 	wd->sc_wdc_bio.lp = wd->sc_dk.dk_label;
    376 	wd->sc_sdhook = shutdownhook_establish(wd_shutdown, wd);
    377 	if (wd->sc_sdhook == NULL)
    378 		printf("%s: WARNING: unable to establish shutdown hook\n",
    379 		    wd->sc_dev.dv_xname);
    380 #if NRND > 0
    381 	rnd_attach_source(&wd->rnd_source, wd->sc_dev.dv_xname,
    382 			  RND_TYPE_DISK, 0);
    383 #endif
    384 }
    385 
    386 int
    387 wdactivate(self, act)
    388 	struct device *self;
    389 	enum devact act;
    390 {
    391 	int rv = 0;
    392 
    393 	switch (act) {
    394 	case DVACT_ACTIVATE:
    395 		rv = EOPNOTSUPP;
    396 		break;
    397 
    398 	case DVACT_DEACTIVATE:
    399 		/*
    400 		 * Nothing to do; we key off the device's DVF_ACTIVATE.
    401 		 */
    402 		break;
    403 	}
    404 	return (rv);
    405 }
    406 
    407 int
    408 wddetach(self, flags)
    409 	struct device *self;
    410 	int flags;
    411 {
    412 	struct wd_softc *sc = (struct wd_softc *)self;
    413 	struct buf *bp;
    414 	int s, bmaj, cmaj, i, mn;
    415 
    416 	/* locate the major number */
    417 	bmaj = bdevsw_lookup_major(&wd_bdevsw);
    418 	cmaj = cdevsw_lookup_major(&wd_cdevsw);
    419 
    420 	s = splbio();
    421 
    422 	/* Kill off any queued buffers. */
    423 	while ((bp = BUFQ_GET(&sc->sc_q)) != NULL) {
    424 		bp->b_error = EIO;
    425 		bp->b_flags |= B_ERROR;
    426 		bp->b_resid = bp->b_bcount;
    427 		biodone(bp);
    428 	}
    429 
    430 	bufq_free(&sc->sc_q);
    431 
    432 	splx(s);
    433 
    434 	/* Nuke the vnodes for any open instances. */
    435 	for (i = 0; i < MAXPARTITIONS; i++) {
    436 		mn = WDMINOR(self->dv_unit, i);
    437 		vdevgone(bmaj, mn, mn, VBLK);
    438 		vdevgone(cmaj, mn, mn, VCHR);
    439 	}
    440 
    441 	/* Detach disk. */
    442 	disk_detach(&sc->sc_dk);
    443 
    444 	/* Get rid of the shutdown hook. */
    445 	if (sc->sc_sdhook != NULL)
    446 		shutdownhook_disestablish(sc->sc_sdhook);
    447 
    448 #if NRND > 0
    449 	/* Unhook the entropy source. */
    450 	rnd_detach_source(&sc->rnd_source);
    451 #endif
    452 
    453 	return (0);
    454 }
    455 
    456 /*
    457  * Read/write routine for a buffer.  Validates the arguments and schedules the
    458  * transfer.  Does not wait for the transfer to complete.
    459  */
    460 void
    461 wdstrategy(bp)
    462 	struct buf *bp;
    463 {
    464 	struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(bp->b_dev));
    465 	struct disklabel *lp = wd->sc_dk.dk_label;
    466 	daddr_t blkno;
    467 	int s;
    468 
    469 	WDCDEBUG_PRINT(("wdstrategy (%s)\n", wd->sc_dev.dv_xname),
    470 	    DEBUG_XFERS);
    471 
    472 	/* Valid request?  */
    473 	if (bp->b_blkno < 0 ||
    474 	    (bp->b_bcount % lp->d_secsize) != 0 ||
    475 	    (bp->b_bcount / lp->d_secsize) >= (1 << NBBY)) {
    476 		bp->b_error = EINVAL;
    477 		goto bad;
    478 	}
    479 
    480 	/* If device invalidated (e.g. media change, door open), error. */
    481 	if ((wd->sc_flags & WDF_LOADED) == 0) {
    482 		bp->b_error = EIO;
    483 		goto bad;
    484 	}
    485 
    486 	/* If it's a null transfer, return immediately. */
    487 	if (bp->b_bcount == 0)
    488 		goto done;
    489 
    490 	/*
    491 	 * Do bounds checking, adjust transfer. if error, process.
    492 	 * If end of partition, just return.
    493 	 */
    494 	if (WDPART(bp->b_dev) != RAW_PART &&
    495 	    bounds_check_with_label(bp, wd->sc_dk.dk_label,
    496 	    (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
    497 		goto done;
    498 
    499 	/*
    500 	 * Now convert the block number to absolute and put it in
    501 	 * terms of the device's logical block size.
    502 	 */
    503 	if (lp->d_secsize >= DEV_BSIZE)
    504 		blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
    505 	else
    506 		blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
    507 
    508 	if (WDPART(bp->b_dev) != RAW_PART)
    509 		blkno += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
    510 
    511 	bp->b_rawblkno = blkno;
    512 
    513 	/* Queue transfer on drive, activate drive and controller if idle. */
    514 	s = splbio();
    515 	BUFQ_PUT(&wd->sc_q, bp);
    516 	wdstart(wd);
    517 	splx(s);
    518 	return;
    519 bad:
    520 	bp->b_flags |= B_ERROR;
    521 done:
    522 	/* Toss transfer; we're done early. */
    523 	bp->b_resid = bp->b_bcount;
    524 	biodone(bp);
    525 }
    526 
    527 /*
    528  * Queue a drive for I/O.
    529  */
    530 void
    531 wdstart(arg)
    532 	void *arg;
    533 {
    534 	struct wd_softc *wd = arg;
    535 	struct buf *bp = NULL;
    536 
    537 	WDCDEBUG_PRINT(("wdstart %s\n", wd->sc_dev.dv_xname),
    538 	    DEBUG_XFERS);
    539 	while (wd->openings > 0) {
    540 
    541 		/* Is there a buf for us ? */
    542 		if ((bp = BUFQ_GET(&wd->sc_q)) == NULL)
    543 			return;
    544 
    545 		/*
    546 		 * Make the command. First lock the device
    547 		 */
    548 		wd->openings--;
    549 
    550 		wd->retries = 0;
    551 		__wdstart(wd, bp);
    552 	}
    553 }
    554 
    555 void
    556 __wdstart(wd, bp)
    557 	struct wd_softc *wd;
    558 	struct buf *bp;
    559 {
    560 
    561 	wd->sc_wdc_bio.blkno = bp->b_rawblkno;
    562 	wd->sc_wdc_bio.blkdone =0;
    563 	wd->sc_bp = bp;
    564 	/*
    565 	 * If we're retrying, retry in single-sector mode. This will give us
    566 	 * the sector number of the problem, and will eventually allow the
    567 	 * transfer to succeed.
    568 	 */
    569 	if (wd->sc_multi == 1 || wd->retries >= WDIORETRIES_SINGLE)
    570 		wd->sc_wdc_bio.flags = ATA_SINGLE;
    571 	else
    572 		wd->sc_wdc_bio.flags = 0;
    573 	if (wd->sc_flags & WDF_LBA48)
    574 		wd->sc_wdc_bio.flags |= ATA_LBA48;
    575 	if (wd->sc_flags & WDF_LBA)
    576 		wd->sc_wdc_bio.flags |= ATA_LBA;
    577 	if (bp->b_flags & B_READ)
    578 		wd->sc_wdc_bio.flags |= ATA_READ;
    579 	wd->sc_wdc_bio.bcount = bp->b_bcount;
    580 	wd->sc_wdc_bio.databuf = bp->b_data;
    581 	/* Instrumentation. */
    582 	disk_busy(&wd->sc_dk);
    583 	switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
    584 	case WDC_TRY_AGAIN:
    585 		callout_reset(&wd->sc_restart_ch, hz, wdrestart, wd);
    586 		break;
    587 	case WDC_QUEUED:
    588 	case WDC_COMPLETE:
    589 		break;
    590 	default:
    591 		panic("__wdstart: bad return code from ata_bio()");
    592 	}
    593 }
    594 
    595 void
    596 wddone(v)
    597 	void *v;
    598 {
    599 	struct wd_softc *wd = v;
    600 	struct buf *bp = wd->sc_bp;
    601 	const char *errmsg;
    602 	int do_perror = 0;
    603 	WDCDEBUG_PRINT(("wddone %s\n", wd->sc_dev.dv_xname),
    604 	    DEBUG_XFERS);
    605 
    606 	if (bp == NULL)
    607 		return;
    608 	bp->b_resid = wd->sc_wdc_bio.bcount;
    609 	switch (wd->sc_wdc_bio.error) {
    610 	case ERR_DMA:
    611 		errmsg = "DMA error";
    612 		goto retry;
    613 	case ERR_DF:
    614 		errmsg = "device fault";
    615 		goto retry;
    616 	case TIMEOUT:
    617 		errmsg = "device timeout";
    618 		goto retry;
    619 	case ERROR:
    620 		/* Don't care about media change bits */
    621 		if (wd->sc_wdc_bio.r_error != 0 &&
    622 		    (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
    623 			goto noerror;
    624 		errmsg = "error";
    625 		do_perror = 1;
    626 retry:		/* Just reset and retry. Can we do more ? */
    627 		wd->atabus->ata_reset_channel(wd->drvp);
    628 		diskerr(bp, "wd", errmsg, LOG_PRINTF,
    629 		    wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
    630 		if (wd->retries < WDIORETRIES)
    631 			printf(", retrying\n");
    632 		if (do_perror)
    633 			wdperror(wd);
    634 		if (wd->retries < WDIORETRIES) {
    635 			wd->retries++;
    636 			callout_reset(&wd->sc_restart_ch, RECOVERYTIME,
    637 			    wdrestart, wd);
    638 			return;
    639 		}
    640 		printf("\n");
    641 		bp->b_flags |= B_ERROR;
    642 		bp->b_error = EIO;
    643 		break;
    644 	case NOERROR:
    645 noerror:	if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
    646 			printf("%s: soft error (corrected)\n",
    647 			    wd->sc_dev.dv_xname);
    648 		break;
    649 	case ERR_NODEV:
    650 		bp->b_flags |= B_ERROR;
    651 		bp->b_error = EIO;
    652 		break;
    653 	}
    654 	disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid));
    655 #if NRND > 0
    656 	rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
    657 #endif
    658 	biodone(bp);
    659 	wd->openings++;
    660 	wdstart(wd);
    661 }
    662 
    663 void
    664 wdrestart(v)
    665 	void *v;
    666 {
    667 	struct wd_softc *wd = v;
    668 	struct buf *bp = wd->sc_bp;
    669 	int s;
    670 	WDCDEBUG_PRINT(("wdrestart %s\n", wd->sc_dev.dv_xname),
    671 	    DEBUG_XFERS);
    672 
    673 	s = splbio();
    674 	__wdstart(v, bp);
    675 	splx(s);
    676 }
    677 
    678 int
    679 wdread(dev, uio, flags)
    680 	dev_t dev;
    681 	struct uio *uio;
    682 	int flags;
    683 {
    684 
    685 	WDCDEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
    686 	return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
    687 }
    688 
    689 int
    690 wdwrite(dev, uio, flags)
    691 	dev_t dev;
    692 	struct uio *uio;
    693 	int flags;
    694 {
    695 
    696 	WDCDEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
    697 	return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
    698 }
    699 
    700 /*
    701  * Wait interruptibly for an exclusive lock.
    702  *
    703  * XXX
    704  * Several drivers do this; it should be abstracted and made MP-safe.
    705  */
    706 int
    707 wdlock(wd)
    708 	struct wd_softc *wd;
    709 {
    710 	int error;
    711 	int s;
    712 
    713 	WDCDEBUG_PRINT(("wdlock\n"), DEBUG_FUNCS);
    714 
    715 	s = splbio();
    716 
    717 	while ((wd->sc_flags & WDF_LOCKED) != 0) {
    718 		wd->sc_flags |= WDF_WANTED;
    719 		if ((error = tsleep(wd, PRIBIO | PCATCH,
    720 		    "wdlck", 0)) != 0) {
    721 			splx(s);
    722 			return error;
    723 		}
    724 	}
    725 	wd->sc_flags |= WDF_LOCKED;
    726 	splx(s);
    727 	return 0;
    728 }
    729 
    730 /*
    731  * Unlock and wake up any waiters.
    732  */
    733 void
    734 wdunlock(wd)
    735 	struct wd_softc *wd;
    736 {
    737 
    738 	WDCDEBUG_PRINT(("wdunlock\n"), DEBUG_FUNCS);
    739 
    740 	wd->sc_flags &= ~WDF_LOCKED;
    741 	if ((wd->sc_flags & WDF_WANTED) != 0) {
    742 		wd->sc_flags &= ~WDF_WANTED;
    743 		wakeup(wd);
    744 	}
    745 }
    746 
    747 int
    748 wdopen(dev, flag, fmt, p)
    749 	dev_t dev;
    750 	int flag, fmt;
    751 	struct proc *p;
    752 {
    753 	struct wd_softc *wd;
    754 	int part, error;
    755 
    756 	WDCDEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
    757 	wd = device_lookup(&wd_cd, WDUNIT(dev));
    758 	if (wd == NULL)
    759 		return (ENXIO);
    760 
    761 	/*
    762 	 * If this is the first open of this device, add a reference
    763 	 * to the adapter.
    764 	 */
    765 	if (wd->sc_dk.dk_openmask == 0 &&
    766 	    (error = wd->atabus->ata_addref(wd->drvp)) != 0)
    767 		return (error);
    768 
    769 	if ((error = wdlock(wd)) != 0)
    770 		goto bad4;
    771 
    772 	if (wd->sc_dk.dk_openmask != 0) {
    773 		/*
    774 		 * If any partition is open, but the disk has been invalidated,
    775 		 * disallow further opens.
    776 		 */
    777 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    778 			error = EIO;
    779 			goto bad3;
    780 		}
    781 	} else {
    782 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    783 			wd->sc_flags |= WDF_LOADED;
    784 
    785 			/* Load the physical device parameters. */
    786 			wd_get_params(wd, AT_WAIT, &wd->sc_params);
    787 
    788 			/* Load the partition info if not already loaded. */
    789 			wdgetdisklabel(wd);
    790 		}
    791 	}
    792 
    793 	part = WDPART(dev);
    794 
    795 	/* Check that the partition exists. */
    796 	if (part != RAW_PART &&
    797 	    (part >= wd->sc_dk.dk_label->d_npartitions ||
    798 	     wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    799 		error = ENXIO;
    800 		goto bad;
    801 	}
    802 
    803 	/* Insure only one open at a time. */
    804 	switch (fmt) {
    805 	case S_IFCHR:
    806 		wd->sc_dk.dk_copenmask |= (1 << part);
    807 		break;
    808 	case S_IFBLK:
    809 		wd->sc_dk.dk_bopenmask |= (1 << part);
    810 		break;
    811 	}
    812 	wd->sc_dk.dk_openmask =
    813 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    814 
    815 	wdunlock(wd);
    816 	return 0;
    817 
    818 bad:
    819 	if (wd->sc_dk.dk_openmask == 0) {
    820 	}
    821 
    822 bad3:
    823 	wdunlock(wd);
    824 bad4:
    825 	if (wd->sc_dk.dk_openmask == 0)
    826 		wd->atabus->ata_delref(wd->drvp);
    827 	return error;
    828 }
    829 
    830 int
    831 wdclose(dev, flag, fmt, p)
    832 	dev_t dev;
    833 	int flag, fmt;
    834 	struct proc *p;
    835 {
    836 	struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
    837 	int part = WDPART(dev);
    838 	int error;
    839 
    840 	WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
    841 	if ((error = wdlock(wd)) != 0)
    842 		return error;
    843 
    844 	switch (fmt) {
    845 	case S_IFCHR:
    846 		wd->sc_dk.dk_copenmask &= ~(1 << part);
    847 		break;
    848 	case S_IFBLK:
    849 		wd->sc_dk.dk_bopenmask &= ~(1 << part);
    850 		break;
    851 	}
    852 	wd->sc_dk.dk_openmask =
    853 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    854 
    855 	if (wd->sc_dk.dk_openmask == 0) {
    856 		wd_flushcache(wd, AT_WAIT);
    857 		/* XXXX Must wait for I/O to complete! */
    858 
    859 		if (! (wd->sc_flags & WDF_KLABEL))
    860 			wd->sc_flags &= ~WDF_LOADED;
    861 
    862 		wd->atabus->ata_delref(wd->drvp);
    863 	}
    864 
    865 	wdunlock(wd);
    866 	return 0;
    867 }
    868 
    869 void
    870 wdgetdefaultlabel(wd, lp)
    871 	struct wd_softc *wd;
    872 	struct disklabel *lp;
    873 {
    874 
    875 	WDCDEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
    876 	memset(lp, 0, sizeof(struct disklabel));
    877 
    878 	lp->d_secsize = DEV_BSIZE;
    879 	lp->d_ntracks = wd->sc_params.atap_heads;
    880 	lp->d_nsectors = wd->sc_params.atap_sectors;
    881 	lp->d_ncylinders = wd->sc_params.atap_cylinders;
    882 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    883 
    884 	if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
    885 		lp->d_type = DTYPE_ST506;
    886 	else
    887 		lp->d_type = DTYPE_ESDI;
    888 
    889 	strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
    890 	strncpy(lp->d_packname, "fictitious", 16);
    891 	lp->d_secperunit = wd->sc_capacity;
    892 	lp->d_rpm = 3600;
    893 	lp->d_interleave = 1;
    894 	lp->d_flags = 0;
    895 
    896 	lp->d_partitions[RAW_PART].p_offset = 0;
    897 	lp->d_partitions[RAW_PART].p_size =
    898 	lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
    899 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    900 	lp->d_npartitions = RAW_PART + 1;
    901 
    902 	lp->d_magic = DISKMAGIC;
    903 	lp->d_magic2 = DISKMAGIC;
    904 	lp->d_checksum = dkcksum(lp);
    905 }
    906 
    907 /*
    908  * Fabricate a default disk label, and try to read the correct one.
    909  */
    910 void
    911 wdgetdisklabel(wd)
    912 	struct wd_softc *wd;
    913 {
    914 	struct disklabel *lp = wd->sc_dk.dk_label;
    915 	char *errstring;
    916 
    917 	WDCDEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
    918 
    919 	memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
    920 
    921 	wdgetdefaultlabel(wd, lp);
    922 
    923 	wd->sc_badsect[0] = -1;
    924 
    925 	if (wd->drvp->state > RECAL)
    926 		wd->drvp->drive_flags |= DRIVE_RESET;
    927 	errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
    928 	    wdstrategy, lp, wd->sc_dk.dk_cpulabel);
    929 	if (errstring) {
    930 		/*
    931 		 * This probably happened because the drive's default
    932 		 * geometry doesn't match the DOS geometry.  We
    933 		 * assume the DOS geometry is now in the label and try
    934 		 * again.  XXX This is a kluge.
    935 		 */
    936 		if (wd->drvp->state > RECAL)
    937 			wd->drvp->drive_flags |= DRIVE_RESET;
    938 		errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit,
    939 		    RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
    940 	}
    941 	if (errstring) {
    942 		printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
    943 		return;
    944 	}
    945 
    946 	if (wd->drvp->state > RECAL)
    947 		wd->drvp->drive_flags |= DRIVE_RESET;
    948 #ifdef HAS_BAD144_HANDLING
    949 	if ((lp->d_flags & D_BADSECT) != 0)
    950 		bad144intern(wd);
    951 #endif
    952 }
    953 
    954 void
    955 wdperror(wd)
    956 	const struct wd_softc *wd;
    957 {
    958 	static const char *const errstr0_3[] = {"address mark not found",
    959 	    "track 0 not found", "aborted command", "media change requested",
    960 	    "id not found", "media changed", "uncorrectable data error",
    961 	    "bad block detected"};
    962 	static const char *const errstr4_5[] = {
    963 	    "obsolete (address mark not found)",
    964 	    "no media/write protected", "aborted command",
    965 	    "media change requested", "id not found", "media changed",
    966 	    "uncorrectable data error", "interface CRC error"};
    967 	const char *const *errstr;
    968 	int i;
    969 	char *sep = "";
    970 
    971 	const char *devname = wd->sc_dev.dv_xname;
    972 	struct ata_drive_datas *drvp = wd->drvp;
    973 	int errno = wd->sc_wdc_bio.r_error;
    974 
    975 	if (drvp->ata_vers >= 4)
    976 		errstr = errstr4_5;
    977 	else
    978 		errstr = errstr0_3;
    979 
    980 	printf("%s: (", devname);
    981 
    982 	if (errno == 0)
    983 		printf("error not notified");
    984 
    985 	for (i = 0; i < 8; i++) {
    986 		if (errno & (1 << i)) {
    987 			printf("%s%s", sep, errstr[i]);
    988 			sep = ", ";
    989 		}
    990 	}
    991 	printf(")\n");
    992 }
    993 
    994 int
    995 wdioctl(dev, xfer, addr, flag, p)
    996 	dev_t dev;
    997 	u_long xfer;
    998 	caddr_t addr;
    999 	int flag;
   1000 	struct proc *p;
   1001 {
   1002 	struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
   1003 	int error;
   1004 #ifdef __HAVE_OLD_DISKLABEL
   1005 	struct disklabel newlabel;
   1006 #endif
   1007 
   1008 	WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
   1009 
   1010 	if ((wd->sc_flags & WDF_LOADED) == 0)
   1011 		return EIO;
   1012 
   1013 	switch (xfer) {
   1014 #ifdef HAS_BAD144_HANDLING
   1015 	case DIOCSBAD:
   1016 		if ((flag & FWRITE) == 0)
   1017 			return EBADF;
   1018 		wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
   1019 		wd->sc_dk.dk_label->d_flags |= D_BADSECT;
   1020 		bad144intern(wd);
   1021 		return 0;
   1022 #endif
   1023 
   1024 	case DIOCGDINFO:
   1025 		*(struct disklabel *)addr = *(wd->sc_dk.dk_label);
   1026 		return 0;
   1027 #ifdef __HAVE_OLD_DISKLABEL
   1028 	case ODIOCGDINFO:
   1029 		newlabel = *(wd->sc_dk.dk_label);
   1030 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
   1031 			return ENOTTY;
   1032 		memcpy(addr, &newlabel, sizeof (struct olddisklabel));
   1033 		return 0;
   1034 #endif
   1035 
   1036 	case DIOCGPART:
   1037 		((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
   1038 		((struct partinfo *)addr)->part =
   1039 		    &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
   1040 		return 0;
   1041 
   1042 	case DIOCWDINFO:
   1043 	case DIOCSDINFO:
   1044 #ifdef __HAVE_OLD_DISKLABEL
   1045 	case ODIOCWDINFO:
   1046 	case ODIOCSDINFO:
   1047 #endif
   1048 	{
   1049 		struct disklabel *lp;
   1050 
   1051 #ifdef __HAVE_OLD_DISKLABEL
   1052 		if (xfer == ODIOCSDINFO || xfer == ODIOCWDINFO) {
   1053 			memset(&newlabel, 0, sizeof newlabel);
   1054 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
   1055 			lp = &newlabel;
   1056 		} else
   1057 #endif
   1058 		lp = (struct disklabel *)addr;
   1059 
   1060 		if ((flag & FWRITE) == 0)
   1061 			return EBADF;
   1062 
   1063 		if ((error = wdlock(wd)) != 0)
   1064 			return error;
   1065 		wd->sc_flags |= WDF_LABELLING;
   1066 
   1067 		error = setdisklabel(wd->sc_dk.dk_label,
   1068 		    lp, /*wd->sc_dk.dk_openmask : */0,
   1069 		    wd->sc_dk.dk_cpulabel);
   1070 		if (error == 0) {
   1071 			if (wd->drvp->state > RECAL)
   1072 				wd->drvp->drive_flags |= DRIVE_RESET;
   1073 			if (xfer == DIOCWDINFO
   1074 #ifdef __HAVE_OLD_DISKLABEL
   1075 			    || xfer == ODIOCWDINFO
   1076 #endif
   1077 			    )
   1078 				error = writedisklabel(WDLABELDEV(dev),
   1079 				    wdstrategy, wd->sc_dk.dk_label,
   1080 				    wd->sc_dk.dk_cpulabel);
   1081 		}
   1082 
   1083 		wd->sc_flags &= ~WDF_LABELLING;
   1084 		wdunlock(wd);
   1085 		return error;
   1086 	}
   1087 
   1088 	case DIOCKLABEL:
   1089 		if (*(int *)addr)
   1090 			wd->sc_flags |= WDF_KLABEL;
   1091 		else
   1092 			wd->sc_flags &= ~WDF_KLABEL;
   1093 		return 0;
   1094 
   1095 	case DIOCWLABEL:
   1096 		if ((flag & FWRITE) == 0)
   1097 			return EBADF;
   1098 		if (*(int *)addr)
   1099 			wd->sc_flags |= WDF_WLABEL;
   1100 		else
   1101 			wd->sc_flags &= ~WDF_WLABEL;
   1102 		return 0;
   1103 
   1104 	case DIOCGDEFLABEL:
   1105 		wdgetdefaultlabel(wd, (struct disklabel *)addr);
   1106 		return 0;
   1107 #ifdef __HAVE_OLD_DISKLABEL
   1108 	case ODIOCGDEFLABEL:
   1109 		wdgetdefaultlabel(wd, &newlabel);
   1110 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
   1111 			return ENOTTY;
   1112 		memcpy(addr, &newlabel, sizeof (struct olddisklabel));
   1113 		return 0;
   1114 #endif
   1115 
   1116 #ifdef notyet
   1117 	case DIOCWFORMAT:
   1118 		if ((flag & FWRITE) == 0)
   1119 			return EBADF;
   1120 		{
   1121 		register struct format_op *fop;
   1122 		struct iovec aiov;
   1123 		struct uio auio;
   1124 
   1125 		fop = (struct format_op *)addr;
   1126 		aiov.iov_base = fop->df_buf;
   1127 		aiov.iov_len = fop->df_count;
   1128 		auio.uio_iov = &aiov;
   1129 		auio.uio_iovcnt = 1;
   1130 		auio.uio_resid = fop->df_count;
   1131 		auio.uio_segflg = 0;
   1132 		auio.uio_offset =
   1133 			fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
   1134 		auio.uio_procp = p;
   1135 		error = physio(wdformat, NULL, dev, B_WRITE, minphys,
   1136 		    &auio);
   1137 		fop->df_count -= auio.uio_resid;
   1138 		fop->df_reg[0] = wdc->sc_status;
   1139 		fop->df_reg[1] = wdc->sc_error;
   1140 		return error;
   1141 		}
   1142 #endif
   1143 
   1144 	case ATAIOCCOMMAND:
   1145 		/*
   1146 		 * Make sure this command is (relatively) safe first
   1147 		 */
   1148 		if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
   1149 		    (flag & FWRITE) == 0)
   1150 			return (EBADF);
   1151 		{
   1152 		struct wd_ioctl *wi;
   1153 		atareq_t *atareq = (atareq_t *) addr;
   1154 		int error;
   1155 
   1156 		wi = wi_get();
   1157 		wi->wi_softc = wd;
   1158 		wi->wi_atareq = *atareq;
   1159 
   1160 		if (atareq->datalen && atareq->flags &
   1161 		    (ATACMD_READ | ATACMD_WRITE)) {
   1162 			wi->wi_iov.iov_base = atareq->databuf;
   1163 			wi->wi_iov.iov_len = atareq->datalen;
   1164 			wi->wi_uio.uio_iov = &wi->wi_iov;
   1165 			wi->wi_uio.uio_iovcnt = 1;
   1166 			wi->wi_uio.uio_resid = atareq->datalen;
   1167 			wi->wi_uio.uio_offset = 0;
   1168 			wi->wi_uio.uio_segflg = UIO_USERSPACE;
   1169 			wi->wi_uio.uio_rw =
   1170 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
   1171 			wi->wi_uio.uio_procp = p;
   1172 			error = physio(wdioctlstrategy, &wi->wi_bp, dev,
   1173 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
   1174 			    minphys, &wi->wi_uio);
   1175 		} else {
   1176 			/* No need to call physio if we don't have any
   1177 			   user data */
   1178 			wi->wi_bp.b_flags = 0;
   1179 			wi->wi_bp.b_data = 0;
   1180 			wi->wi_bp.b_bcount = 0;
   1181 			wi->wi_bp.b_dev = 0;
   1182 			wi->wi_bp.b_proc = p;
   1183 			wdioctlstrategy(&wi->wi_bp);
   1184 			error = wi->wi_bp.b_error;
   1185 		}
   1186 		*atareq = wi->wi_atareq;
   1187 		wi_free(wi);
   1188 		return(error);
   1189 		}
   1190 
   1191 	default:
   1192 		return ENOTTY;
   1193 	}
   1194 
   1195 #ifdef DIAGNOSTIC
   1196 	panic("wdioctl: impossible");
   1197 #endif
   1198 }
   1199 
   1200 #ifdef B_FORMAT
   1201 int
   1202 wdformat(struct buf *bp)
   1203 {
   1204 
   1205 	bp->b_flags |= B_FORMAT;
   1206 	return wdstrategy(bp);
   1207 }
   1208 #endif
   1209 
   1210 int
   1211 wdsize(dev)
   1212 	dev_t dev;
   1213 {
   1214 	struct wd_softc *wd;
   1215 	int part, omask;
   1216 	int size;
   1217 
   1218 	WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
   1219 
   1220 	wd = device_lookup(&wd_cd, WDUNIT(dev));
   1221 	if (wd == NULL)
   1222 		return (-1);
   1223 
   1224 	part = WDPART(dev);
   1225 	omask = wd->sc_dk.dk_openmask & (1 << part);
   1226 
   1227 	if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
   1228 		return (-1);
   1229 	if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
   1230 		size = -1;
   1231 	else
   1232 		size = wd->sc_dk.dk_label->d_partitions[part].p_size *
   1233 		    (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
   1234 	if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
   1235 		return (-1);
   1236 	return (size);
   1237 }
   1238 
   1239 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
   1240 static int wddoingadump = 0;
   1241 static int wddumprecalibrated = 0;
   1242 static int wddumpmulti = 1;
   1243 
   1244 /*
   1245  * Dump core after a system crash.
   1246  */
   1247 int
   1248 wddump(dev, blkno, va, size)
   1249 	dev_t dev;
   1250 	daddr_t blkno;
   1251 	caddr_t va;
   1252 	size_t size;
   1253 {
   1254 	struct wd_softc *wd;	/* disk unit to do the I/O */
   1255 	struct disklabel *lp;   /* disk's disklabel */
   1256 	int part, err;
   1257 	int nblks;	/* total number of sectors left to write */
   1258 
   1259 	/* Check if recursive dump; if so, punt. */
   1260 	if (wddoingadump)
   1261 		return EFAULT;
   1262 	wddoingadump = 1;
   1263 
   1264 	wd = device_lookup(&wd_cd, WDUNIT(dev));
   1265 	if (wd == NULL)
   1266 		return (ENXIO);
   1267 
   1268 	part = WDPART(dev);
   1269 
   1270 	/* Convert to disk sectors.  Request must be a multiple of size. */
   1271 	lp = wd->sc_dk.dk_label;
   1272 	if ((size % lp->d_secsize) != 0)
   1273 		return EFAULT;
   1274 	nblks = size / lp->d_secsize;
   1275 	blkno = blkno / (lp->d_secsize / DEV_BSIZE);
   1276 
   1277 	/* Check transfer bounds against partition size. */
   1278 	if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
   1279 		return EINVAL;
   1280 
   1281 	/* Offset block number to start of partition. */
   1282 	blkno += lp->d_partitions[part].p_offset;
   1283 
   1284 	/* Recalibrate, if first dump transfer. */
   1285 	if (wddumprecalibrated == 0) {
   1286 		wddumpmulti = wd->sc_multi;
   1287 		wddumprecalibrated = 1;
   1288 		wd->drvp->state = RESET;
   1289 	}
   1290 
   1291 	while (nblks > 0) {
   1292 again:
   1293 		wd->sc_bp = NULL;
   1294 		wd->sc_wdc_bio.blkno = blkno;
   1295 		wd->sc_wdc_bio.flags = ATA_POLL;
   1296 		if (wddumpmulti == 1)
   1297 			wd->sc_wdc_bio.flags |= ATA_SINGLE;
   1298 		if (wd->sc_flags & WDF_LBA48)
   1299 			wd->sc_wdc_bio.flags |= ATA_LBA48;
   1300 		if (wd->sc_flags & WDF_LBA)
   1301 			wd->sc_wdc_bio.flags |= ATA_LBA;
   1302 		wd->sc_wdc_bio.bcount =
   1303 			min(nblks, wddumpmulti) * lp->d_secsize;
   1304 		wd->sc_wdc_bio.databuf = va;
   1305 #ifndef WD_DUMP_NOT_TRUSTED
   1306 		switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
   1307 		case WDC_TRY_AGAIN:
   1308 			panic("wddump: try again");
   1309 			break;
   1310 		case WDC_QUEUED:
   1311 			panic("wddump: polled command has been queued");
   1312 			break;
   1313 		case WDC_COMPLETE:
   1314 			break;
   1315 		}
   1316 		switch(wd->sc_wdc_bio.error) {
   1317 		case TIMEOUT:
   1318 			printf("wddump: device timed out");
   1319 			err = EIO;
   1320 			break;
   1321 		case ERR_DF:
   1322 			printf("wddump: drive fault");
   1323 			err = EIO;
   1324 			break;
   1325 		case ERR_DMA:
   1326 			printf("wddump: DMA error");
   1327 			err = EIO;
   1328 			break;
   1329 		case ERROR:
   1330 			printf("wddump: ");
   1331 			wdperror(wd);
   1332 			err = EIO;
   1333 			break;
   1334 		case NOERROR:
   1335 			err = 0;
   1336 			break;
   1337 		default:
   1338 			panic("wddump: unknown error type");
   1339 		}
   1340 		if (err != 0) {
   1341 			if (wddumpmulti != 1) {
   1342 				wddumpmulti = 1; /* retry in single-sector */
   1343 				printf(", retrying\n");
   1344 				goto again;
   1345 			}
   1346 			printf("\n");
   1347 			return err;
   1348 		}
   1349 #else	/* WD_DUMP_NOT_TRUSTED */
   1350 		/* Let's just talk about this first... */
   1351 		printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
   1352 		    unit, va, cylin, head, sector);
   1353 		delay(500 * 1000);	/* half a second */
   1354 #endif
   1355 
   1356 		/* update block count */
   1357 		nblks -= min(nblks, wddumpmulti);
   1358 		blkno += min(nblks, wddumpmulti);
   1359 		va += min(nblks, wddumpmulti) * lp->d_secsize;
   1360 	}
   1361 
   1362 	wddoingadump = 0;
   1363 	return 0;
   1364 }
   1365 
   1366 #ifdef HAS_BAD144_HANDLING
   1367 /*
   1368  * Internalize the bad sector table.
   1369  */
   1370 void
   1371 bad144intern(wd)
   1372 	struct wd_softc *wd;
   1373 {
   1374 	struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
   1375 	struct disklabel *lp = wd->sc_dk.dk_label;
   1376 	int i = 0;
   1377 
   1378 	WDCDEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
   1379 
   1380 	for (; i < NBT_BAD; i++) {
   1381 		if (bt->bt_bad[i].bt_cyl == 0xffff)
   1382 			break;
   1383 		wd->sc_badsect[i] =
   1384 		    bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
   1385 		    (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
   1386 		    (bt->bt_bad[i].bt_trksec & 0xff);
   1387 	}
   1388 	for (; i < NBT_BAD+1; i++)
   1389 		wd->sc_badsect[i] = -1;
   1390 }
   1391 #endif
   1392 
   1393 int
   1394 wd_get_params(wd, flags, params)
   1395 	struct wd_softc *wd;
   1396 	u_int8_t flags;
   1397 	struct ataparams *params;
   1398 {
   1399 	switch (wd->atabus->ata_get_params(wd->drvp, flags, params)) {
   1400 	case CMD_AGAIN:
   1401 		return 1;
   1402 	case CMD_ERR:
   1403 		/*
   1404 		 * We `know' there's a drive here; just assume it's old.
   1405 		 * This geometry is only used to read the MBR and print a
   1406 		 * (false) attach message.
   1407 		 */
   1408 		strncpy(params->atap_model, "ST506",
   1409 		    sizeof params->atap_model);
   1410 		params->atap_config = ATA_CFG_FIXED;
   1411 		params->atap_cylinders = 1024;
   1412 		params->atap_heads = 8;
   1413 		params->atap_sectors = 17;
   1414 		params->atap_multi = 1;
   1415 		params->atap_capabilities1 = params->atap_capabilities2 = 0;
   1416 		wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
   1417 		return 0;
   1418 	case CMD_OK:
   1419 		return 0;
   1420 	default:
   1421 		panic("wd_get_params: bad return code from ata_get_params");
   1422 		/* NOTREACHED */
   1423 	}
   1424 }
   1425 
   1426 void
   1427 wd_flushcache(wd, flags)
   1428 	struct wd_softc *wd;
   1429 	int flags;
   1430 {
   1431 	struct wdc_command wdc_c;
   1432 
   1433 	if (wd->drvp->ata_vers < 4) /* WDCC_FLUSHCACHE is here since ATA-4 */
   1434 		return;
   1435 	memset(&wdc_c, 0, sizeof(struct wdc_command));
   1436 	wdc_c.r_command = WDCC_FLUSHCACHE;
   1437 	wdc_c.r_st_bmask = WDCS_DRDY;
   1438 	wdc_c.r_st_pmask = WDCS_DRDY;
   1439 	wdc_c.flags = flags;
   1440 	wdc_c.timeout = 30000; /* 30s timeout */
   1441 	if (wd->atabus->ata_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
   1442 		printf("%s: flush cache command didn't complete\n",
   1443 		    wd->sc_dev.dv_xname);
   1444 	}
   1445 	if (wdc_c.flags & AT_TIMEOU) {
   1446 		printf("%s: flush cache command timeout\n",
   1447 		    wd->sc_dev.dv_xname);
   1448 	}
   1449 	if (wdc_c.flags & AT_DF) {
   1450 		printf("%s: flush cache command: drive fault\n",
   1451 		    wd->sc_dev.dv_xname);
   1452 	}
   1453 	/*
   1454 	 * Ignore error register, it shouldn't report anything else
   1455 	 * than COMMAND ABORTED, which means the device doesn't support
   1456 	 * flush cache
   1457 	 */
   1458 }
   1459 
   1460 void
   1461 wd_shutdown(arg)
   1462 	void *arg;
   1463 {
   1464 	struct wd_softc *wd = arg;
   1465 	wd_flushcache(wd, AT_POLL);
   1466 }
   1467 
   1468 /*
   1469  * Allocate space for a ioctl queue structure.  Mostly taken from
   1470  * scsipi_ioctl.c
   1471  */
   1472 struct wd_ioctl *
   1473 wi_get()
   1474 {
   1475 	struct wd_ioctl *wi;
   1476 	int s;
   1477 
   1478 	wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK|M_ZERO);
   1479 	s = splbio();
   1480 	LIST_INSERT_HEAD(&wi_head, wi, wi_list);
   1481 	splx(s);
   1482 	return (wi);
   1483 }
   1484 
   1485 /*
   1486  * Free an ioctl structure and remove it from our list
   1487  */
   1488 
   1489 void
   1490 wi_free(wi)
   1491 	struct wd_ioctl *wi;
   1492 {
   1493 	int s;
   1494 
   1495 	s = splbio();
   1496 	LIST_REMOVE(wi, wi_list);
   1497 	splx(s);
   1498 	free(wi, M_TEMP);
   1499 }
   1500 
   1501 /*
   1502  * Find a wd_ioctl structure based on the struct buf.
   1503  */
   1504 
   1505 struct wd_ioctl *
   1506 wi_find(bp)
   1507 	struct buf *bp;
   1508 {
   1509 	struct wd_ioctl *wi;
   1510 	int s;
   1511 
   1512 	s = splbio();
   1513 	for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
   1514 		if (bp == &wi->wi_bp)
   1515 			break;
   1516 	splx(s);
   1517 	return (wi);
   1518 }
   1519 
   1520 /*
   1521  * Ioctl pseudo strategy routine
   1522  *
   1523  * This is mostly stolen from scsipi_ioctl.c:scsistrategy().  What
   1524  * happens here is:
   1525  *
   1526  * - wdioctl() queues a wd_ioctl structure.
   1527  *
   1528  * - wdioctl() calls physio/wdioctlstrategy based on whether or not
   1529  *   user space I/O is required.  If physio() is called, physio() eventually
   1530  *   calls wdioctlstrategy().
   1531  *
   1532  * - In either case, wdioctlstrategy() calls wd->atabus->ata_exec_command()
   1533  *   to perform the actual command
   1534  *
   1535  * The reason for the use of the pseudo strategy routine is because
   1536  * when doing I/O to/from user space, physio _really_ wants to be in
   1537  * the loop.  We could put the entire buffer into the ioctl request
   1538  * structure, but that won't scale if we want to do things like download
   1539  * microcode.
   1540  */
   1541 
   1542 void
   1543 wdioctlstrategy(bp)
   1544 	struct buf *bp;
   1545 {
   1546 	struct wd_ioctl *wi;
   1547 	struct wdc_command wdc_c;
   1548 	int error = 0;
   1549 
   1550 	wi = wi_find(bp);
   1551 	if (wi == NULL) {
   1552 		printf("user_strat: No ioctl\n");
   1553 		error = EINVAL;
   1554 		goto bad;
   1555 	}
   1556 
   1557 	memset(&wdc_c, 0, sizeof(wdc_c));
   1558 
   1559 	/*
   1560 	 * Abort if physio broke up the transfer
   1561 	 */
   1562 
   1563 	if (bp->b_bcount != wi->wi_atareq.datalen) {
   1564 		printf("physio split wd ioctl request... cannot proceed\n");
   1565 		error = EIO;
   1566 		goto bad;
   1567 	}
   1568 
   1569 	/*
   1570 	 * Abort if we didn't get a buffer size that was a multiple of
   1571 	 * our sector size (or was larger than NBBY)
   1572 	 */
   1573 
   1574 	if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
   1575 	    (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
   1576 	     (1 << NBBY)) {
   1577 		error = EINVAL;
   1578 		goto bad;
   1579 	}
   1580 
   1581 	/*
   1582 	 * Make sure a timeout was supplied in the ioctl request
   1583 	 */
   1584 
   1585 	if (wi->wi_atareq.timeout == 0) {
   1586 		error = EINVAL;
   1587 		goto bad;
   1588 	}
   1589 
   1590 	if (wi->wi_atareq.flags & ATACMD_READ)
   1591 		wdc_c.flags |= AT_READ;
   1592 	else if (wi->wi_atareq.flags & ATACMD_WRITE)
   1593 		wdc_c.flags |= AT_WRITE;
   1594 
   1595 	if (wi->wi_atareq.flags & ATACMD_READREG)
   1596 		wdc_c.flags |= AT_READREG;
   1597 
   1598 	wdc_c.flags |= AT_WAIT;
   1599 
   1600 	wdc_c.timeout = wi->wi_atareq.timeout;
   1601 	wdc_c.r_command = wi->wi_atareq.command;
   1602 	wdc_c.r_head = wi->wi_atareq.head & 0x0f;
   1603 	wdc_c.r_cyl = wi->wi_atareq.cylinder;
   1604 	wdc_c.r_sector = wi->wi_atareq.sec_num;
   1605 	wdc_c.r_count = wi->wi_atareq.sec_count;
   1606 	wdc_c.r_precomp = wi->wi_atareq.features;
   1607 	wdc_c.r_st_bmask = WDCS_DRDY;
   1608 	wdc_c.r_st_pmask = WDCS_DRDY;
   1609 	wdc_c.data = wi->wi_bp.b_data;
   1610 	wdc_c.bcount = wi->wi_bp.b_bcount;
   1611 
   1612 	if (wi->wi_softc->atabus->ata_exec_command(wi->wi_softc->drvp, &wdc_c)
   1613 	    != WDC_COMPLETE) {
   1614 		wi->wi_atareq.retsts = ATACMD_ERROR;
   1615 		goto bad;
   1616 	}
   1617 
   1618 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
   1619 		if (wdc_c.flags & AT_ERROR) {
   1620 			wi->wi_atareq.retsts = ATACMD_ERROR;
   1621 			wi->wi_atareq.error = wdc_c.r_error;
   1622 		} else if (wdc_c.flags & AT_DF)
   1623 			wi->wi_atareq.retsts = ATACMD_DF;
   1624 		else
   1625 			wi->wi_atareq.retsts = ATACMD_TIMEOUT;
   1626 	} else {
   1627 		wi->wi_atareq.retsts = ATACMD_OK;
   1628 		if (wi->wi_atareq.flags & ATACMD_READREG) {
   1629 			wi->wi_atareq.head = wdc_c.r_head ;
   1630 			wi->wi_atareq.cylinder = wdc_c.r_cyl;
   1631 			wi->wi_atareq.sec_num = wdc_c.r_sector;
   1632 			wi->wi_atareq.sec_count = wdc_c.r_count;
   1633 			wi->wi_atareq.features = wdc_c.r_precomp;
   1634 			wi->wi_atareq.error = wdc_c.r_error;
   1635 		}
   1636 	}
   1637 
   1638 	bp->b_error = 0;
   1639 	biodone(bp);
   1640 	return;
   1641 bad:
   1642 	bp->b_flags |= B_ERROR;
   1643 	bp->b_error = error;
   1644 	biodone(bp);
   1645 }
   1646