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