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