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