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