Home | History | Annotate | Line # | Download | only in ata
wd.c revision 1.205.2.1
      1 /*	$NetBSD: wd.c,v 1.205.2.1 2001/05/01 12:27:11 he 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 #ifdef __HAVE_OLD_DISKLABEL
    933 	struct disklabel newlabel;
    934 #endif
    935 
    936 	WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
    937 
    938 	if ((wd->sc_flags & WDF_LOADED) == 0)
    939 		return EIO;
    940 
    941 	switch (xfer) {
    942 #ifdef HAS_BAD144_HANDLING
    943 	case DIOCSBAD:
    944 		if ((flag & FWRITE) == 0)
    945 			return EBADF;
    946 		wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
    947 		wd->sc_dk.dk_label->d_flags |= D_BADSECT;
    948 		bad144intern(wd);
    949 		return 0;
    950 #endif
    951 
    952 	case DIOCGDINFO:
    953 		*(struct disklabel *)addr = *(wd->sc_dk.dk_label);
    954 		return 0;
    955 #ifdef __HAVE_OLD_DISKLABEL
    956 	case ODIOCGDINFO:
    957 		newlabel = *(wd->sc_dk.dk_label);
    958 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    959 			return ENOTTY;
    960 		memcpy(addr, &newlabel, sizeof (struct olddisklabel));
    961 		return 0;
    962 #endif
    963 
    964 	case DIOCGPART:
    965 		((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
    966 		((struct partinfo *)addr)->part =
    967 		    &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
    968 		return 0;
    969 
    970 	case DIOCWDINFO:
    971 	case DIOCSDINFO:
    972 #ifdef __HAVE_OLD_DISKLABEL
    973 	case ODIOCWDINFO:
    974 	case ODIOCSDINFO:
    975 #endif
    976 	{
    977 		struct disklabel *lp;
    978 
    979 #ifdef __HAVE_OLD_DISKLABEL
    980 		if (xfer == ODIOCSDINFO || xfer == ODIOCWDINFO) {
    981 			memset(&newlabel, 0, sizeof newlabel);
    982 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
    983 			lp = &newlabel;
    984 		} else
    985 #endif
    986 		lp = (struct disklabel *)addr;
    987 
    988 		if ((flag & FWRITE) == 0)
    989 			return EBADF;
    990 
    991 		if ((error = wdlock(wd)) != 0)
    992 			return error;
    993 		wd->sc_flags |= WDF_LABELLING;
    994 
    995 		error = setdisklabel(wd->sc_dk.dk_label,
    996 		    lp, /*wd->sc_dk.dk_openmask : */0,
    997 		    wd->sc_dk.dk_cpulabel);
    998 		if (error == 0) {
    999 			if (wd->drvp->state > RECAL)
   1000 				wd->drvp->drive_flags |= DRIVE_RESET;
   1001 			if (xfer == DIOCWDINFO
   1002 #ifdef __HAVE_OLD_DISKLABEL
   1003 			    || xfer == ODIOCWDINFO
   1004 #endif
   1005 			    )
   1006 				error = writedisklabel(WDLABELDEV(dev),
   1007 				    wdstrategy, wd->sc_dk.dk_label,
   1008 				    wd->sc_dk.dk_cpulabel);
   1009 		}
   1010 
   1011 		wd->sc_flags &= ~WDF_LABELLING;
   1012 		wdunlock(wd);
   1013 		return error;
   1014 	}
   1015 
   1016 	case DIOCKLABEL:
   1017 		if (*(int *)addr)
   1018 			wd->sc_flags |= WDF_KLABEL;
   1019 		else
   1020 			wd->sc_flags &= ~WDF_KLABEL;
   1021 		return 0;
   1022 
   1023 	case DIOCWLABEL:
   1024 		if ((flag & FWRITE) == 0)
   1025 			return EBADF;
   1026 		if (*(int *)addr)
   1027 			wd->sc_flags |= WDF_WLABEL;
   1028 		else
   1029 			wd->sc_flags &= ~WDF_WLABEL;
   1030 		return 0;
   1031 
   1032 	case DIOCGDEFLABEL:
   1033 		wdgetdefaultlabel(wd, (struct disklabel *)addr);
   1034 		return 0;
   1035 #ifdef __HAVE_OLD_DISKLABEL
   1036 	case ODIOCGDEFLABEL:
   1037 		wdgetdefaultlabel(wd, &newlabel);
   1038 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
   1039 			return ENOTTY;
   1040 		memcpy(addr, &newlabel, sizeof (struct olddisklabel));
   1041 		return 0;
   1042 #endif
   1043 
   1044 #ifdef notyet
   1045 	case DIOCWFORMAT:
   1046 		if ((flag & FWRITE) == 0)
   1047 			return EBADF;
   1048 		{
   1049 		register struct format_op *fop;
   1050 		struct iovec aiov;
   1051 		struct uio auio;
   1052 
   1053 		fop = (struct format_op *)addr;
   1054 		aiov.iov_base = fop->df_buf;
   1055 		aiov.iov_len = fop->df_count;
   1056 		auio.uio_iov = &aiov;
   1057 		auio.uio_iovcnt = 1;
   1058 		auio.uio_resid = fop->df_count;
   1059 		auio.uio_segflg = 0;
   1060 		auio.uio_offset =
   1061 			fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
   1062 		auio.uio_procp = p;
   1063 		error = physio(wdformat, NULL, dev, B_WRITE, minphys,
   1064 		    &auio);
   1065 		fop->df_count -= auio.uio_resid;
   1066 		fop->df_reg[0] = wdc->sc_status;
   1067 		fop->df_reg[1] = wdc->sc_error;
   1068 		return error;
   1069 		}
   1070 #endif
   1071 
   1072 	case ATAIOCCOMMAND:
   1073 		/*
   1074 		 * Make sure this command is (relatively) safe first
   1075 		 */
   1076 		if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
   1077 		    (flag & FWRITE) == 0)
   1078 			return (EBADF);
   1079 		{
   1080 		struct wd_ioctl *wi;
   1081 		atareq_t *atareq = (atareq_t *) addr;
   1082 		int error;
   1083 
   1084 		wi = wi_get();
   1085 		wi->wi_softc = wd;
   1086 		wi->wi_atareq = *atareq;
   1087 
   1088 		if (atareq->datalen && atareq->flags &
   1089 		    (ATACMD_READ | ATACMD_WRITE)) {
   1090 			wi->wi_iov.iov_base = atareq->databuf;
   1091 			wi->wi_iov.iov_len = atareq->datalen;
   1092 			wi->wi_uio.uio_iov = &wi->wi_iov;
   1093 			wi->wi_uio.uio_iovcnt = 1;
   1094 			wi->wi_uio.uio_resid = atareq->datalen;
   1095 			wi->wi_uio.uio_offset = 0;
   1096 			wi->wi_uio.uio_segflg = UIO_USERSPACE;
   1097 			wi->wi_uio.uio_rw =
   1098 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
   1099 			wi->wi_uio.uio_procp = p;
   1100 			error = physio(wdioctlstrategy, &wi->wi_bp, dev,
   1101 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
   1102 			    minphys, &wi->wi_uio);
   1103 		} else {
   1104 			/* No need to call physio if we don't have any
   1105 			   user data */
   1106 			wi->wi_bp.b_flags = 0;
   1107 			wi->wi_bp.b_data = 0;
   1108 			wi->wi_bp.b_bcount = 0;
   1109 			wi->wi_bp.b_dev = 0;
   1110 			wi->wi_bp.b_proc = p;
   1111 			wdioctlstrategy(&wi->wi_bp);
   1112 			error = wi->wi_bp.b_error;
   1113 		}
   1114 		*atareq = wi->wi_atareq;
   1115 		wi_free(wi);
   1116 		return(error);
   1117 		}
   1118 
   1119 	default:
   1120 		return ENOTTY;
   1121 	}
   1122 
   1123 #ifdef DIAGNOSTIC
   1124 	panic("wdioctl: impossible");
   1125 #endif
   1126 }
   1127 
   1128 #ifdef B_FORMAT
   1129 int
   1130 wdformat(struct buf *bp)
   1131 {
   1132 
   1133 	bp->b_flags |= B_FORMAT;
   1134 	return wdstrategy(bp);
   1135 }
   1136 #endif
   1137 
   1138 int
   1139 wdsize(dev)
   1140 	dev_t dev;
   1141 {
   1142 	struct wd_softc *wd;
   1143 	int part, unit, omask;
   1144 	int size;
   1145 
   1146 	WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
   1147 
   1148 	unit = WDUNIT(dev);
   1149 	if (unit >= wd_cd.cd_ndevs)
   1150 		return (-1);
   1151 	wd = wd_cd.cd_devs[unit];
   1152 	if (wd == NULL)
   1153 		return (-1);
   1154 
   1155 	part = WDPART(dev);
   1156 	omask = wd->sc_dk.dk_openmask & (1 << part);
   1157 
   1158 	if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
   1159 		return (-1);
   1160 	if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
   1161 		size = -1;
   1162 	else
   1163 		size = wd->sc_dk.dk_label->d_partitions[part].p_size *
   1164 		    (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
   1165 	if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
   1166 		return (-1);
   1167 	return (size);
   1168 }
   1169 
   1170 #ifndef __BDEVSW_DUMP_OLD_TYPE
   1171 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
   1172 static int wddoingadump = 0;
   1173 static int wddumprecalibrated = 0;
   1174 static int wddumpmulti = 1;
   1175 
   1176 /*
   1177  * Dump core after a system crash.
   1178  */
   1179 int
   1180 wddump(dev, blkno, va, size)
   1181 	dev_t dev;
   1182 	daddr_t blkno;
   1183 	caddr_t va;
   1184 	size_t size;
   1185 {
   1186 	struct wd_softc *wd;	/* disk unit to do the I/O */
   1187 	struct disklabel *lp;   /* disk's disklabel */
   1188 	int unit, part;
   1189 	int nblks;	/* total number of sectors left to write */
   1190 	int err;
   1191 	char errbuf[256];
   1192 
   1193 	/* Check if recursive dump; if so, punt. */
   1194 	if (wddoingadump)
   1195 		return EFAULT;
   1196 	wddoingadump = 1;
   1197 
   1198 	unit = WDUNIT(dev);
   1199 	if (unit >= wd_cd.cd_ndevs)
   1200 		return ENXIO;
   1201 	wd = wd_cd.cd_devs[unit];
   1202 	if (wd == (struct wd_softc *)0)
   1203 		return ENXIO;
   1204 
   1205 	part = WDPART(dev);
   1206 
   1207 	/* Make sure it was initialized. */
   1208 	if (wd->drvp->state < READY)
   1209 		return ENXIO;
   1210 
   1211 	/* Convert to disk sectors.  Request must be a multiple of size. */
   1212 	lp = wd->sc_dk.dk_label;
   1213 	if ((size % lp->d_secsize) != 0)
   1214 		return EFAULT;
   1215 	nblks = size / lp->d_secsize;
   1216 	blkno = blkno / (lp->d_secsize / DEV_BSIZE);
   1217 
   1218 	/* Check transfer bounds against partition size. */
   1219 	if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
   1220 		return EINVAL;
   1221 
   1222 	/* Offset block number to start of partition. */
   1223 	blkno += lp->d_partitions[part].p_offset;
   1224 
   1225 	/* Recalibrate, if first dump transfer. */
   1226 	if (wddumprecalibrated == 0) {
   1227 		wddumpmulti = wd->sc_multi;
   1228 		wddumprecalibrated = 1;
   1229 		wd->drvp->state = RESET;
   1230 	}
   1231 
   1232 	while (nblks > 0) {
   1233 again:
   1234 		wd->sc_wdc_bio.blkno = blkno;
   1235 		wd->sc_wdc_bio.flags = ATA_POLL;
   1236 		if (wddumpmulti == 1)
   1237 			wd->sc_wdc_bio.flags |= ATA_SINGLE;
   1238 		if (wd->sc_flags & WDF_LBA)
   1239 			wd->sc_wdc_bio.flags |= ATA_LBA;
   1240 		wd->sc_wdc_bio.bcount =
   1241 			min(nblks, wddumpmulti) * lp->d_secsize;
   1242 		wd->sc_wdc_bio.databuf = va;
   1243 #ifndef WD_DUMP_NOT_TRUSTED
   1244 		switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
   1245 		case WDC_TRY_AGAIN:
   1246 			panic("wddump: try again");
   1247 			break;
   1248 		case WDC_QUEUED:
   1249 			panic("wddump: polled command has been queued");
   1250 			break;
   1251 		case WDC_COMPLETE:
   1252 			break;
   1253 		}
   1254 		switch(wd->sc_wdc_bio.error) {
   1255 		case TIMEOUT:
   1256 			printf("wddump: device timed out");
   1257 			err = EIO;
   1258 			break;
   1259 		case ERR_DF:
   1260 			printf("wddump: drive fault");
   1261 			err = EIO;
   1262 			break;
   1263 		case ERR_DMA:
   1264 			printf("wddump: DMA error");
   1265 			err = EIO;
   1266 			break;
   1267 		case ERROR:
   1268 			errbuf[0] = '\0';
   1269 			ata_perror(wd->drvp, wd->sc_wdc_bio.r_error, errbuf);
   1270 			printf("wddump: %s", errbuf);
   1271 			err = EIO;
   1272 			break;
   1273 		case NOERROR:
   1274 			err = 0;
   1275 			break;
   1276 		default:
   1277 			panic("wddump: unknown error type");
   1278 		}
   1279 		if (err != 0) {
   1280 			if (wddumpmulti != 1) {
   1281 				wddumpmulti = 1; /* retry in single-sector */
   1282 				printf(", retrying\n");
   1283 				goto again;
   1284 			}
   1285 			printf("\n");
   1286 			return err;
   1287 		}
   1288 #else	/* WD_DUMP_NOT_TRUSTED */
   1289 		/* Let's just talk about this first... */
   1290 		printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
   1291 		    unit, va, cylin, head, sector);
   1292 		delay(500 * 1000);	/* half a second */
   1293 #endif
   1294 
   1295 		/* update block count */
   1296 		nblks -= min(nblks, wddumpmulti);
   1297 		blkno += min(nblks, wddumpmulti);
   1298 		va += min(nblks, wddumpmulti) * lp->d_secsize;
   1299 	}
   1300 
   1301 	wddoingadump = 0;
   1302 	return 0;
   1303 }
   1304 #else /* __BDEVSW_DUMP_NEW_TYPE */
   1305 
   1306 
   1307 int
   1308 wddump(dev, blkno, va, size)
   1309 	dev_t dev;
   1310 	daddr_t blkno;
   1311 	caddr_t va;
   1312 	size_t size;
   1313 {
   1314 
   1315 	/* Not implemented. */
   1316 	return ENXIO;
   1317 }
   1318 #endif /* __BDEVSW_DUMP_NEW_TYPE */
   1319 
   1320 #ifdef HAS_BAD144_HANDLING
   1321 /*
   1322  * Internalize the bad sector table.
   1323  */
   1324 void
   1325 bad144intern(wd)
   1326 	struct wd_softc *wd;
   1327 {
   1328 	struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
   1329 	struct disklabel *lp = wd->sc_dk.dk_label;
   1330 	int i = 0;
   1331 
   1332 	WDCDEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
   1333 
   1334 	for (; i < NBT_BAD; i++) {
   1335 		if (bt->bt_bad[i].bt_cyl == 0xffff)
   1336 			break;
   1337 		wd->sc_badsect[i] =
   1338 		    bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
   1339 		    (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
   1340 		    (bt->bt_bad[i].bt_trksec & 0xff);
   1341 	}
   1342 	for (; i < NBT_BAD+1; i++)
   1343 		wd->sc_badsect[i] = -1;
   1344 }
   1345 #endif
   1346 
   1347 int
   1348 wd_get_params(wd, flags, params)
   1349 	struct wd_softc *wd;
   1350 	u_int8_t flags;
   1351 	struct ataparams *params;
   1352 {
   1353 	switch (ata_get_params(wd->drvp, flags, params)) {
   1354 	case CMD_AGAIN:
   1355 		return 1;
   1356 	case CMD_ERR:
   1357 		/*
   1358 		 * We `know' there's a drive here; just assume it's old.
   1359 		 * This geometry is only used to read the MBR and print a
   1360 		 * (false) attach message.
   1361 		 */
   1362 		strncpy(params->atap_model, "ST506",
   1363 		    sizeof params->atap_model);
   1364 		params->atap_config = ATA_CFG_FIXED;
   1365 		params->atap_cylinders = 1024;
   1366 		params->atap_heads = 8;
   1367 		params->atap_sectors = 17;
   1368 		params->atap_multi = 1;
   1369 		params->atap_capabilities1 = params->atap_capabilities2 = 0;
   1370 		wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
   1371 		return 0;
   1372 	case CMD_OK:
   1373 		return 0;
   1374 	default:
   1375 		panic("wd_get_params: bad return code from ata_get_params");
   1376 		/* NOTREACHED */
   1377 	}
   1378 }
   1379 
   1380 void
   1381 wd_flushcache(wd, flags)
   1382 	struct wd_softc *wd;
   1383 	int flags;
   1384 {
   1385 	struct wdc_command wdc_c;
   1386 
   1387 	if (wd->drvp->ata_vers < 4) /* WDCC_FLUSHCACHE is here since ATA-4 */
   1388 		return;
   1389 	memset(&wdc_c, 0, sizeof(struct wdc_command));
   1390 	wdc_c.r_command = WDCC_FLUSHCACHE;
   1391 	wdc_c.r_st_bmask = WDCS_DRDY;
   1392 	wdc_c.r_st_pmask = WDCS_DRDY;
   1393 	wdc_c.flags = flags;
   1394 	wdc_c.timeout = 30000; /* 30s timeout */
   1395 	if (wdc_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
   1396 		printf("%s: flush cache command didn't complete\n",
   1397 		    wd->sc_dev.dv_xname);
   1398 	}
   1399 	if (wdc_c.flags & AT_TIMEOU) {
   1400 		printf("%s: flush cache command timeout\n",
   1401 		    wd->sc_dev.dv_xname);
   1402 	}
   1403 	if (wdc_c.flags & AT_DF) {
   1404 		printf("%s: flush cache command: drive fault\n",
   1405 		    wd->sc_dev.dv_xname);
   1406 	}
   1407 	/*
   1408 	 * Ignore error register, it shouldn't report anything else
   1409 	 * than COMMAND ABORTED, which means the device doesn't support
   1410 	 * flush cache
   1411 	 */
   1412 }
   1413 
   1414 void
   1415 wd_shutdown(arg)
   1416 	void *arg;
   1417 {
   1418 	struct wd_softc *wd = arg;
   1419 	wd_flushcache(wd, AT_POLL);
   1420 }
   1421 
   1422 /*
   1423  * Allocate space for a ioctl queue structure.  Mostly taken from
   1424  * scsipi_ioctl.c
   1425  */
   1426 struct wd_ioctl *
   1427 wi_get()
   1428 {
   1429 	struct wd_ioctl *wi;
   1430 	int s;
   1431 
   1432 	wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK);
   1433 	memset(wi, 0, sizeof (struct wd_ioctl));
   1434 	s = splbio();
   1435 	LIST_INSERT_HEAD(&wi_head, wi, wi_list);
   1436 	splx(s);
   1437 	return (wi);
   1438 }
   1439 
   1440 /*
   1441  * Free an ioctl structure and remove it from our list
   1442  */
   1443 
   1444 void
   1445 wi_free(wi)
   1446 	struct wd_ioctl *wi;
   1447 {
   1448 	int s;
   1449 
   1450 	s = splbio();
   1451 	LIST_REMOVE(wi, wi_list);
   1452 	splx(s);
   1453 	free(wi, M_TEMP);
   1454 }
   1455 
   1456 /*
   1457  * Find a wd_ioctl structure based on the struct buf.
   1458  */
   1459 
   1460 struct wd_ioctl *
   1461 wi_find(bp)
   1462 	struct buf *bp;
   1463 {
   1464 	struct wd_ioctl *wi;
   1465 	int s;
   1466 
   1467 	s = splbio();
   1468 	for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
   1469 		if (bp == &wi->wi_bp)
   1470 			break;
   1471 	splx(s);
   1472 	return (wi);
   1473 }
   1474 
   1475 /*
   1476  * Ioctl pseudo strategy routine
   1477  *
   1478  * This is mostly stolen from scsipi_ioctl.c:scsistrategy().  What
   1479  * happens here is:
   1480  *
   1481  * - wdioctl() queues a wd_ioctl structure.
   1482  *
   1483  * - wdioctl() calls physio/wdioctlstrategy based on whether or not
   1484  *   user space I/O is required.  If physio() is called, physio() eventually
   1485  *   calls wdioctlstrategy().
   1486  *
   1487  * - In either case, wdioctlstrategy() calls wdc_exec_command()
   1488  *   to perform the actual command
   1489  *
   1490  * The reason for the use of the pseudo strategy routine is because
   1491  * when doing I/O to/from user space, physio _really_ wants to be in
   1492  * the loop.  We could put the entire buffer into the ioctl request
   1493  * structure, but that won't scale if we want to do things like download
   1494  * microcode.
   1495  */
   1496 
   1497 void
   1498 wdioctlstrategy(bp)
   1499 	struct buf *bp;
   1500 {
   1501 	struct wd_ioctl *wi;
   1502 	struct wdc_command wdc_c;
   1503 	int error = 0;
   1504 
   1505 	wi = wi_find(bp);
   1506 	if (wi == NULL) {
   1507 		printf("user_strat: No ioctl\n");
   1508 		error = EINVAL;
   1509 		goto bad;
   1510 	}
   1511 
   1512 	memset(&wdc_c, 0, sizeof(wdc_c));
   1513 
   1514 	/*
   1515 	 * Abort if physio broke up the transfer
   1516 	 */
   1517 
   1518 	if (bp->b_bcount != wi->wi_atareq.datalen) {
   1519 		printf("physio split wd ioctl request... cannot proceed\n");
   1520 		error = EIO;
   1521 		goto bad;
   1522 	}
   1523 
   1524 	/*
   1525 	 * Abort if we didn't get a buffer size that was a multiple of
   1526 	 * our sector size (or was larger than NBBY)
   1527 	 */
   1528 
   1529 	if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
   1530 	    (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
   1531 	     (1 << NBBY)) {
   1532 		error = EINVAL;
   1533 		goto bad;
   1534 	}
   1535 
   1536 	/*
   1537 	 * Make sure a timeout was supplied in the ioctl request
   1538 	 */
   1539 
   1540 	if (wi->wi_atareq.timeout == 0) {
   1541 		error = EINVAL;
   1542 		goto bad;
   1543 	}
   1544 
   1545 	if (wi->wi_atareq.flags & ATACMD_READ)
   1546 		wdc_c.flags |= AT_READ;
   1547 	else if (wi->wi_atareq.flags & ATACMD_WRITE)
   1548 		wdc_c.flags |= AT_WRITE;
   1549 
   1550 	if (wi->wi_atareq.flags & ATACMD_READREG)
   1551 		wdc_c.flags |= AT_READREG;
   1552 
   1553 	wdc_c.flags |= AT_WAIT;
   1554 
   1555 	wdc_c.timeout = wi->wi_atareq.timeout;
   1556 	wdc_c.r_command = wi->wi_atareq.command;
   1557 	wdc_c.r_head = wi->wi_atareq.head & 0x0f;
   1558 	wdc_c.r_cyl = wi->wi_atareq.cylinder;
   1559 	wdc_c.r_sector = wi->wi_atareq.sec_num;
   1560 	wdc_c.r_count = wi->wi_atareq.sec_count;
   1561 	wdc_c.r_precomp = wi->wi_atareq.features;
   1562 	wdc_c.r_st_bmask = WDCS_DRDY;
   1563 	wdc_c.r_st_pmask = WDCS_DRDY;
   1564 	wdc_c.data = wi->wi_bp.b_data;
   1565 	wdc_c.bcount = wi->wi_bp.b_bcount;
   1566 
   1567 	if (wdc_exec_command(wi->wi_softc->drvp, &wdc_c) != WDC_COMPLETE) {
   1568 		wi->wi_atareq.retsts = ATACMD_ERROR;
   1569 		goto bad;
   1570 	}
   1571 
   1572 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
   1573 		if (wdc_c.flags & AT_ERROR) {
   1574 			wi->wi_atareq.retsts = ATACMD_ERROR;
   1575 			wi->wi_atareq.error = wdc_c.r_error;
   1576 		} else if (wdc_c.flags & AT_DF)
   1577 			wi->wi_atareq.retsts = ATACMD_DF;
   1578 		else
   1579 			wi->wi_atareq.retsts = ATACMD_TIMEOUT;
   1580 	} else {
   1581 		wi->wi_atareq.retsts = ATACMD_OK;
   1582 		if (wi->wi_atareq.flags & ATACMD_READREG) {
   1583 			wi->wi_atareq.head = wdc_c.r_head ;
   1584 			wi->wi_atareq.cylinder = wdc_c.r_cyl;
   1585 			wi->wi_atareq.sec_num = wdc_c.r_sector;
   1586 			wi->wi_atareq.sec_count = wdc_c.r_count;
   1587 			wi->wi_atareq.features = wdc_c.r_precomp;
   1588 			wi->wi_atareq.error = wdc_c.r_error;
   1589 		}
   1590 	}
   1591 
   1592 	bp->b_error = 0;
   1593 	biodone(bp);
   1594 	return;
   1595 bad:
   1596 	bp->b_flags |= B_ERROR;
   1597 	bp->b_error = error;
   1598 	biodone(bp);
   1599 }
   1600