Home | History | Annotate | Line # | Download | only in ata
wd.c revision 1.220.8.1
      1 /*	$NetBSD: wd.c,v 1.220.8.1 2002/05/16 12:14:04 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.1 2002/05/16 12:14:04 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 buf_queue sc_q;
    138 	struct callout sc_restart_ch;
    139 	/* IDE disk soft states */
    140 	struct ata_bio sc_wdc_bio; /* current transfer */
    141 	struct buf *sc_bp; /* buf being transfered */
    142 	void *wdc_softc;   /* pointer to our parent */
    143 	struct ata_drive_datas *drvp; /* Our controller's infos */
    144 	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((struct ata_drive_datas *, int, char *));
    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);
    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_FIRST(&sc->sc_q)) != NULL) {
    424 		BUFQ_REMOVE(&sc->sc_q, bp);
    425 		bp->b_error = EIO;
    426 		bp->b_flags |= B_ERROR;
    427 		bp->b_resid = bp->b_bcount;
    428 		biodone(bp);
    429 	}
    430 
    431 	splx(s);
    432 
    433 	/* Nuke the vnodes for any open instances. */
    434 	for (i = 0; i < MAXPARTITIONS; i++) {
    435 		mn = WDMINOR(self->dv_unit, i);
    436 		vdevgone(bmaj, mn, mn, VBLK);
    437 		vdevgone(cmaj, mn, mn, VCHR);
    438 	}
    439 
    440 	/* Detach disk. */
    441 	disk_detach(&sc->sc_dk);
    442 
    443 	/* Get rid of the shutdown hook. */
    444 	if (sc->sc_sdhook != NULL)
    445 		shutdownhook_disestablish(sc->sc_sdhook);
    446 
    447 #if NRND > 0
    448 	/* Unhook the entropy source. */
    449 	rnd_detach_source(&sc->rnd_source);
    450 #endif
    451 
    452 	return (0);
    453 }
    454 
    455 /*
    456  * Read/write routine for a buffer.  Validates the arguments and schedules the
    457  * transfer.  Does not wait for the transfer to complete.
    458  */
    459 void
    460 wdstrategy(bp)
    461 	struct buf *bp;
    462 {
    463 	struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(bp->b_dev));
    464 	struct disklabel *lp = wd->sc_dk.dk_label;
    465 	daddr_t blkno;
    466 	int s;
    467 
    468 	WDCDEBUG_PRINT(("wdstrategy (%s)\n", wd->sc_dev.dv_xname),
    469 	    DEBUG_XFERS);
    470 
    471 	/* Valid request?  */
    472 	if (bp->b_blkno < 0 ||
    473 	    (bp->b_bcount % lp->d_secsize) != 0 ||
    474 	    (bp->b_bcount / lp->d_secsize) >= (1 << NBBY)) {
    475 		bp->b_error = EINVAL;
    476 		goto bad;
    477 	}
    478 
    479 	/* If device invalidated (e.g. media change, door open), error. */
    480 	if ((wd->sc_flags & WDF_LOADED) == 0) {
    481 		bp->b_error = EIO;
    482 		goto bad;
    483 	}
    484 
    485 	/* If it's a null transfer, return immediately. */
    486 	if (bp->b_bcount == 0)
    487 		goto done;
    488 
    489 	/*
    490 	 * Do bounds checking, adjust transfer. if error, process.
    491 	 * If end of partition, just return.
    492 	 */
    493 	if (WDPART(bp->b_dev) != RAW_PART &&
    494 	    bounds_check_with_label(bp, wd->sc_dk.dk_label,
    495 	    (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
    496 		goto done;
    497 
    498 	/*
    499 	 * Now convert the block number to absolute and put it in
    500 	 * terms of the device's logical block size.
    501 	 */
    502 	if (lp->d_secsize >= DEV_BSIZE)
    503 		blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
    504 	else
    505 		blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
    506 
    507 	if (WDPART(bp->b_dev) != RAW_PART)
    508 		blkno += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
    509 
    510 	bp->b_rawblkno = blkno;
    511 
    512 	/* Queue transfer on drive, activate drive and controller if idle. */
    513 	s = splbio();
    514 	disksort_blkno(&wd->sc_q, bp);
    515 	wdstart(wd);
    516 	splx(s);
    517 	return;
    518 bad:
    519 	bp->b_flags |= B_ERROR;
    520 done:
    521 	/* Toss transfer; we're done early. */
    522 	bp->b_resid = bp->b_bcount;
    523 	biodone(bp);
    524 }
    525 
    526 /*
    527  * Queue a drive for I/O.
    528  */
    529 void
    530 wdstart(arg)
    531 	void *arg;
    532 {
    533 	struct wd_softc *wd = arg;
    534 	struct buf *bp = NULL;
    535 
    536 	WDCDEBUG_PRINT(("wdstart %s\n", wd->sc_dev.dv_xname),
    537 	    DEBUG_XFERS);
    538 	while (wd->openings > 0) {
    539 
    540 		/* Is there a buf for us ? */
    541 		if ((bp = BUFQ_FIRST(&wd->sc_q)) == NULL)
    542 			return;
    543 		BUFQ_REMOVE(&wd->sc_q, bp);
    544 
    545 		/*
    546 		 * Make the command. First lock the device
    547 		 */
    548 		wd->openings--;
    549 
    550 		wd->retries = 0;
    551 		__wdstart(wd, bp);
    552 	}
    553 }
    554 
    555 void
    556 __wdstart(wd, bp)
    557 	struct wd_softc *wd;
    558 	struct buf *bp;
    559 {
    560 
    561 	wd->sc_wdc_bio.blkno = bp->b_rawblkno;
    562 	wd->sc_wdc_bio.blkdone =0;
    563 	wd->sc_bp = bp;
    564 	/*
    565 	 * If we're retrying, retry in single-sector mode. This will give us
    566 	 * the sector number of the problem, and will eventually allow the
    567 	 * transfer to succeed.
    568 	 */
    569 	if (wd->sc_multi == 1 || wd->retries >= WDIORETRIES_SINGLE)
    570 		wd->sc_wdc_bio.flags = ATA_SINGLE;
    571 	else
    572 		wd->sc_wdc_bio.flags = 0;
    573 	if (wd->sc_flags & WDF_LBA48)
    574 		wd->sc_wdc_bio.flags |= ATA_LBA48;
    575 	if (wd->sc_flags & WDF_LBA)
    576 		wd->sc_wdc_bio.flags |= ATA_LBA;
    577 	if (bp->b_flags & B_READ)
    578 		wd->sc_wdc_bio.flags |= ATA_READ;
    579 	wd->sc_wdc_bio.bcount = bp->b_bcount;
    580 	wd->sc_wdc_bio.databuf = bp->b_data;
    581 	/* Instrumentation. */
    582 	disk_busy(&wd->sc_dk);
    583 	switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
    584 	case WDC_TRY_AGAIN:
    585 		callout_reset(&wd->sc_restart_ch, hz, wdrestart, wd);
    586 		break;
    587 	case WDC_QUEUED:
    588 	case WDC_COMPLETE:
    589 		break;
    590 	default:
    591 		panic("__wdstart: bad return code from ata_bio()");
    592 	}
    593 }
    594 
    595 void
    596 wddone(v)
    597 	void *v;
    598 {
    599 	struct wd_softc *wd = v;
    600 	struct buf *bp = wd->sc_bp;
    601 	char buf[256], *errbuf = buf;
    602 	WDCDEBUG_PRINT(("wddone %s\n", wd->sc_dev.dv_xname),
    603 	    DEBUG_XFERS);
    604 
    605 	if (bp == NULL)
    606 		return;
    607 	bp->b_resid = wd->sc_wdc_bio.bcount;
    608 	errbuf[0] = '\0';
    609 	switch (wd->sc_wdc_bio.error) {
    610 	case ERR_DMA:
    611 		errbuf = "DMA error";
    612 		goto retry;
    613 	case ERR_DF:
    614 		errbuf = "device fault";
    615 		goto retry;
    616 	case TIMEOUT:
    617 		errbuf = "device timeout";
    618 		goto retry;
    619 	case ERROR:
    620 		/* Don't care about media change bits */
    621 		if (wd->sc_wdc_bio.r_error != 0 &&
    622 		    (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
    623 			goto noerror;
    624 		wdperror(wd->drvp, wd->sc_wdc_bio.r_error, errbuf);
    625 retry:		/* Just reset and retry. Can we do more ? */
    626 		wd->atabus->ata_reset_channel(wd->drvp);
    627 		diskerr(bp, "wd", errbuf, LOG_PRINTF,
    628 		    wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
    629 		if (wd->retries++ < WDIORETRIES) {
    630 			printf(", retrying\n");
    631 			callout_reset(&wd->sc_restart_ch, RECOVERYTIME,
    632 			    wdrestart, wd);
    633 			return;
    634 		}
    635 		printf("\n");
    636 		bp->b_flags |= B_ERROR;
    637 		bp->b_error = EIO;
    638 		break;
    639 	case NOERROR:
    640 noerror:	if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
    641 			printf("%s: soft error (corrected)\n",
    642 			    wd->sc_dev.dv_xname);
    643 		break;
    644 	case ERR_NODEV:
    645 		bp->b_flags |= B_ERROR;
    646 		bp->b_error = EIO;
    647 		break;
    648 	}
    649 	disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid));
    650 #if NRND > 0
    651 	rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
    652 #endif
    653 	biodone(bp);
    654 	wd->openings++;
    655 	wdstart(wd);
    656 }
    657 
    658 void
    659 wdrestart(v)
    660 	void *v;
    661 {
    662 	struct wd_softc *wd = v;
    663 	struct buf *bp = wd->sc_bp;
    664 	int s;
    665 	WDCDEBUG_PRINT(("wdrestart %s\n", wd->sc_dev.dv_xname),
    666 	    DEBUG_XFERS);
    667 
    668 	s = splbio();
    669 	__wdstart(v, bp);
    670 	splx(s);
    671 }
    672 
    673 int
    674 wdread(dev, uio, flags)
    675 	dev_t dev;
    676 	struct uio *uio;
    677 	int flags;
    678 {
    679 
    680 	WDCDEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
    681 	return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
    682 }
    683 
    684 int
    685 wdwrite(dev, uio, flags)
    686 	dev_t dev;
    687 	struct uio *uio;
    688 	int flags;
    689 {
    690 
    691 	WDCDEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
    692 	return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
    693 }
    694 
    695 /*
    696  * Wait interruptibly for an exclusive lock.
    697  *
    698  * XXX
    699  * Several drivers do this; it should be abstracted and made MP-safe.
    700  */
    701 int
    702 wdlock(wd)
    703 	struct wd_softc *wd;
    704 {
    705 	int error;
    706 	int s;
    707 
    708 	WDCDEBUG_PRINT(("wdlock\n"), DEBUG_FUNCS);
    709 
    710 	s = splbio();
    711 
    712 	while ((wd->sc_flags & WDF_LOCKED) != 0) {
    713 		wd->sc_flags |= WDF_WANTED;
    714 		if ((error = tsleep(wd, PRIBIO | PCATCH,
    715 		    "wdlck", 0)) != 0) {
    716 			splx(s);
    717 			return error;
    718 		}
    719 	}
    720 	wd->sc_flags |= WDF_LOCKED;
    721 	splx(s);
    722 	return 0;
    723 }
    724 
    725 /*
    726  * Unlock and wake up any waiters.
    727  */
    728 void
    729 wdunlock(wd)
    730 	struct wd_softc *wd;
    731 {
    732 
    733 	WDCDEBUG_PRINT(("wdunlock\n"), DEBUG_FUNCS);
    734 
    735 	wd->sc_flags &= ~WDF_LOCKED;
    736 	if ((wd->sc_flags & WDF_WANTED) != 0) {
    737 		wd->sc_flags &= ~WDF_WANTED;
    738 		wakeup(wd);
    739 	}
    740 }
    741 
    742 int
    743 wdopen(dev, flag, fmt, p)
    744 	dev_t dev;
    745 	int flag, fmt;
    746 	struct proc *p;
    747 {
    748 	struct wd_softc *wd;
    749 	int part, error;
    750 
    751 	WDCDEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
    752 	wd = device_lookup(&wd_cd, WDUNIT(dev));
    753 	if (wd == NULL)
    754 		return (ENXIO);
    755 
    756 	/*
    757 	 * If this is the first open of this device, add a reference
    758 	 * to the adapter.
    759 	 */
    760 	if (wd->sc_dk.dk_openmask == 0 &&
    761 	    (error = wd->atabus->ata_addref(wd->drvp)) != 0)
    762 		return (error);
    763 
    764 	if ((error = wdlock(wd)) != 0)
    765 		goto bad4;
    766 
    767 	if (wd->sc_dk.dk_openmask != 0) {
    768 		/*
    769 		 * If any partition is open, but the disk has been invalidated,
    770 		 * disallow further opens.
    771 		 */
    772 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    773 			error = EIO;
    774 			goto bad3;
    775 		}
    776 	} else {
    777 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    778 			wd->sc_flags |= WDF_LOADED;
    779 
    780 			/* Load the physical device parameters. */
    781 			wd_get_params(wd, AT_WAIT, &wd->sc_params);
    782 
    783 			/* Load the partition info if not already loaded. */
    784 			wdgetdisklabel(wd);
    785 		}
    786 	}
    787 
    788 	part = WDPART(dev);
    789 
    790 	/* Check that the partition exists. */
    791 	if (part != RAW_PART &&
    792 	    (part >= wd->sc_dk.dk_label->d_npartitions ||
    793 	     wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    794 		error = ENXIO;
    795 		goto bad;
    796 	}
    797 
    798 	/* Insure only one open at a time. */
    799 	switch (fmt) {
    800 	case S_IFCHR:
    801 		wd->sc_dk.dk_copenmask |= (1 << part);
    802 		break;
    803 	case S_IFBLK:
    804 		wd->sc_dk.dk_bopenmask |= (1 << part);
    805 		break;
    806 	}
    807 	wd->sc_dk.dk_openmask =
    808 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    809 
    810 	wdunlock(wd);
    811 	return 0;
    812 
    813 bad:
    814 	if (wd->sc_dk.dk_openmask == 0) {
    815 	}
    816 
    817 bad3:
    818 	wdunlock(wd);
    819 bad4:
    820 	if (wd->sc_dk.dk_openmask == 0)
    821 		wd->atabus->ata_delref(wd->drvp);
    822 	return error;
    823 }
    824 
    825 int
    826 wdclose(dev, flag, fmt, p)
    827 	dev_t dev;
    828 	int flag, fmt;
    829 	struct proc *p;
    830 {
    831 	struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
    832 	int part = WDPART(dev);
    833 	int error;
    834 
    835 	WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
    836 	if ((error = wdlock(wd)) != 0)
    837 		return error;
    838 
    839 	switch (fmt) {
    840 	case S_IFCHR:
    841 		wd->sc_dk.dk_copenmask &= ~(1 << part);
    842 		break;
    843 	case S_IFBLK:
    844 		wd->sc_dk.dk_bopenmask &= ~(1 << part);
    845 		break;
    846 	}
    847 	wd->sc_dk.dk_openmask =
    848 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    849 
    850 	if (wd->sc_dk.dk_openmask == 0) {
    851 		wd_flushcache(wd, AT_WAIT);
    852 		/* XXXX Must wait for I/O to complete! */
    853 
    854 		if (! (wd->sc_flags & WDF_KLABEL))
    855 			wd->sc_flags &= ~WDF_LOADED;
    856 
    857 		wd->atabus->ata_delref(wd->drvp);
    858 	}
    859 
    860 	wdunlock(wd);
    861 	return 0;
    862 }
    863 
    864 void
    865 wdgetdefaultlabel(wd, lp)
    866 	struct wd_softc *wd;
    867 	struct disklabel *lp;
    868 {
    869 
    870 	WDCDEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
    871 	memset(lp, 0, sizeof(struct disklabel));
    872 
    873 	lp->d_secsize = DEV_BSIZE;
    874 	lp->d_ntracks = wd->sc_params.atap_heads;
    875 	lp->d_nsectors = wd->sc_params.atap_sectors;
    876 	lp->d_ncylinders = wd->sc_params.atap_cylinders;
    877 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    878 
    879 	if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
    880 		lp->d_type = DTYPE_ST506;
    881 	else
    882 		lp->d_type = DTYPE_ESDI;
    883 
    884 	strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
    885 	strncpy(lp->d_packname, "fictitious", 16);
    886 	lp->d_secperunit = wd->sc_capacity;
    887 	lp->d_rpm = 3600;
    888 	lp->d_interleave = 1;
    889 	lp->d_flags = 0;
    890 
    891 	lp->d_partitions[RAW_PART].p_offset = 0;
    892 	lp->d_partitions[RAW_PART].p_size =
    893 	lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
    894 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    895 	lp->d_npartitions = RAW_PART + 1;
    896 
    897 	lp->d_magic = DISKMAGIC;
    898 	lp->d_magic2 = DISKMAGIC;
    899 	lp->d_checksum = dkcksum(lp);
    900 }
    901 
    902 /*
    903  * Fabricate a default disk label, and try to read the correct one.
    904  */
    905 void
    906 wdgetdisklabel(wd)
    907 	struct wd_softc *wd;
    908 {
    909 	struct disklabel *lp = wd->sc_dk.dk_label;
    910 	char *errstring;
    911 
    912 	WDCDEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
    913 
    914 	memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
    915 
    916 	wdgetdefaultlabel(wd, lp);
    917 
    918 	wd->sc_badsect[0] = -1;
    919 
    920 	if (wd->drvp->state > RECAL)
    921 		wd->drvp->drive_flags |= DRIVE_RESET;
    922 	errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
    923 	    wdstrategy, lp, wd->sc_dk.dk_cpulabel);
    924 	if (errstring) {
    925 		/*
    926 		 * This probably happened because the drive's default
    927 		 * geometry doesn't match the DOS geometry.  We
    928 		 * assume the DOS geometry is now in the label and try
    929 		 * again.  XXX This is a kluge.
    930 		 */
    931 		if (wd->drvp->state > RECAL)
    932 			wd->drvp->drive_flags |= DRIVE_RESET;
    933 		errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit,
    934 		    RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
    935 	}
    936 	if (errstring) {
    937 		printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
    938 		return;
    939 	}
    940 
    941 	if (wd->drvp->state > RECAL)
    942 		wd->drvp->drive_flags |= DRIVE_RESET;
    943 #ifdef HAS_BAD144_HANDLING
    944 	if ((lp->d_flags & D_BADSECT) != 0)
    945 		bad144intern(wd);
    946 #endif
    947 }
    948 
    949 void
    950 wdperror(drvp, errno, buf)
    951 	struct ata_drive_datas *drvp;
    952 	int errno;
    953 	char *buf;
    954 {
    955 	static char *errstr0_3[] = {"address mark not found",
    956 	    "track 0 not found", "aborted command", "media change requested",
    957 	    "id not found", "media changed", "uncorrectable data error",
    958 	    "bad block detected"};
    959 	static char *errstr4_5[] = {"obsolete (address mark not found)",
    960 	    "no media/write protected", "aborted command",
    961 	    "media change requested", "id not found", "media changed",
    962 	    "uncorrectable data error", "interface CRC error"};
    963 	char **errstr;
    964 	int i;
    965 	char *sep = "";
    966 
    967 	if (drvp->ata_vers >= 4)
    968 		errstr = errstr4_5;
    969 	else
    970 		errstr = errstr0_3;
    971 
    972 	if (errno == 0)
    973 		sprintf(buf, "error not notified");
    974 
    975 	for (i = 0; i < 8; i++) {
    976 		if (errno & (1 << i)) {
    977 			buf += sprintf(buf, "%s%s", sep, errstr[i]);
    978 			sep = ", ";
    979 		}
    980 	}
    981 }
    982 
    983 int
    984 wdioctl(dev, xfer, addr, flag, p)
    985 	dev_t dev;
    986 	u_long xfer;
    987 	caddr_t addr;
    988 	int flag;
    989 	struct proc *p;
    990 {
    991 	struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
    992 	int error;
    993 #ifdef __HAVE_OLD_DISKLABEL
    994 	struct disklabel newlabel;
    995 #endif
    996 
    997 	WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
    998 
    999 	if ((wd->sc_flags & WDF_LOADED) == 0)
   1000 		return EIO;
   1001 
   1002 	switch (xfer) {
   1003 #ifdef HAS_BAD144_HANDLING
   1004 	case DIOCSBAD:
   1005 		if ((flag & FWRITE) == 0)
   1006 			return EBADF;
   1007 		wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
   1008 		wd->sc_dk.dk_label->d_flags |= D_BADSECT;
   1009 		bad144intern(wd);
   1010 		return 0;
   1011 #endif
   1012 
   1013 	case DIOCGDINFO:
   1014 		*(struct disklabel *)addr = *(wd->sc_dk.dk_label);
   1015 		return 0;
   1016 #ifdef __HAVE_OLD_DISKLABEL
   1017 	case ODIOCGDINFO:
   1018 		newlabel = *(wd->sc_dk.dk_label);
   1019 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
   1020 			return ENOTTY;
   1021 		memcpy(addr, &newlabel, sizeof (struct olddisklabel));
   1022 		return 0;
   1023 #endif
   1024 
   1025 	case DIOCGPART:
   1026 		((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
   1027 		((struct partinfo *)addr)->part =
   1028 		    &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
   1029 		return 0;
   1030 
   1031 	case DIOCWDINFO:
   1032 	case DIOCSDINFO:
   1033 #ifdef __HAVE_OLD_DISKLABEL
   1034 	case ODIOCWDINFO:
   1035 	case ODIOCSDINFO:
   1036 #endif
   1037 	{
   1038 		struct disklabel *lp;
   1039 
   1040 #ifdef __HAVE_OLD_DISKLABEL
   1041 		if (xfer == ODIOCSDINFO || xfer == ODIOCWDINFO) {
   1042 			memset(&newlabel, 0, sizeof newlabel);
   1043 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
   1044 			lp = &newlabel;
   1045 		} else
   1046 #endif
   1047 		lp = (struct disklabel *)addr;
   1048 
   1049 		if ((flag & FWRITE) == 0)
   1050 			return EBADF;
   1051 
   1052 		if ((error = wdlock(wd)) != 0)
   1053 			return error;
   1054 		wd->sc_flags |= WDF_LABELLING;
   1055 
   1056 		error = setdisklabel(wd->sc_dk.dk_label,
   1057 		    lp, /*wd->sc_dk.dk_openmask : */0,
   1058 		    wd->sc_dk.dk_cpulabel);
   1059 		if (error == 0) {
   1060 			if (wd->drvp->state > RECAL)
   1061 				wd->drvp->drive_flags |= DRIVE_RESET;
   1062 			if (xfer == DIOCWDINFO
   1063 #ifdef __HAVE_OLD_DISKLABEL
   1064 			    || xfer == ODIOCWDINFO
   1065 #endif
   1066 			    )
   1067 				error = writedisklabel(WDLABELDEV(dev),
   1068 				    wdstrategy, wd->sc_dk.dk_label,
   1069 				    wd->sc_dk.dk_cpulabel);
   1070 		}
   1071 
   1072 		wd->sc_flags &= ~WDF_LABELLING;
   1073 		wdunlock(wd);
   1074 		return error;
   1075 	}
   1076 
   1077 	case DIOCKLABEL:
   1078 		if (*(int *)addr)
   1079 			wd->sc_flags |= WDF_KLABEL;
   1080 		else
   1081 			wd->sc_flags &= ~WDF_KLABEL;
   1082 		return 0;
   1083 
   1084 	case DIOCWLABEL:
   1085 		if ((flag & FWRITE) == 0)
   1086 			return EBADF;
   1087 		if (*(int *)addr)
   1088 			wd->sc_flags |= WDF_WLABEL;
   1089 		else
   1090 			wd->sc_flags &= ~WDF_WLABEL;
   1091 		return 0;
   1092 
   1093 	case DIOCGDEFLABEL:
   1094 		wdgetdefaultlabel(wd, (struct disklabel *)addr);
   1095 		return 0;
   1096 #ifdef __HAVE_OLD_DISKLABEL
   1097 	case ODIOCGDEFLABEL:
   1098 		wdgetdefaultlabel(wd, &newlabel);
   1099 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
   1100 			return ENOTTY;
   1101 		memcpy(addr, &newlabel, sizeof (struct olddisklabel));
   1102 		return 0;
   1103 #endif
   1104 
   1105 #ifdef notyet
   1106 	case DIOCWFORMAT:
   1107 		if ((flag & FWRITE) == 0)
   1108 			return EBADF;
   1109 		{
   1110 		register struct format_op *fop;
   1111 		struct iovec aiov;
   1112 		struct uio auio;
   1113 
   1114 		fop = (struct format_op *)addr;
   1115 		aiov.iov_base = fop->df_buf;
   1116 		aiov.iov_len = fop->df_count;
   1117 		auio.uio_iov = &aiov;
   1118 		auio.uio_iovcnt = 1;
   1119 		auio.uio_resid = fop->df_count;
   1120 		auio.uio_segflg = 0;
   1121 		auio.uio_offset =
   1122 			fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
   1123 		auio.uio_procp = p;
   1124 		error = physio(wdformat, NULL, dev, B_WRITE, minphys,
   1125 		    &auio);
   1126 		fop->df_count -= auio.uio_resid;
   1127 		fop->df_reg[0] = wdc->sc_status;
   1128 		fop->df_reg[1] = wdc->sc_error;
   1129 		return error;
   1130 		}
   1131 #endif
   1132 
   1133 	case ATAIOCCOMMAND:
   1134 		/*
   1135 		 * Make sure this command is (relatively) safe first
   1136 		 */
   1137 		if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
   1138 		    (flag & FWRITE) == 0)
   1139 			return (EBADF);
   1140 		{
   1141 		struct wd_ioctl *wi;
   1142 		atareq_t *atareq = (atareq_t *) addr;
   1143 		int error;
   1144 
   1145 		wi = wi_get();
   1146 		wi->wi_softc = wd;
   1147 		wi->wi_atareq = *atareq;
   1148 
   1149 		if (atareq->datalen && atareq->flags &
   1150 		    (ATACMD_READ | ATACMD_WRITE)) {
   1151 			wi->wi_iov.iov_base = atareq->databuf;
   1152 			wi->wi_iov.iov_len = atareq->datalen;
   1153 			wi->wi_uio.uio_iov = &wi->wi_iov;
   1154 			wi->wi_uio.uio_iovcnt = 1;
   1155 			wi->wi_uio.uio_resid = atareq->datalen;
   1156 			wi->wi_uio.uio_offset = 0;
   1157 			wi->wi_uio.uio_segflg = UIO_USERSPACE;
   1158 			wi->wi_uio.uio_rw =
   1159 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
   1160 			wi->wi_uio.uio_procp = p;
   1161 			error = physio(wdioctlstrategy, &wi->wi_bp, dev,
   1162 			    (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
   1163 			    minphys, &wi->wi_uio);
   1164 		} else {
   1165 			/* No need to call physio if we don't have any
   1166 			   user data */
   1167 			wi->wi_bp.b_flags = 0;
   1168 			wi->wi_bp.b_data = 0;
   1169 			wi->wi_bp.b_bcount = 0;
   1170 			wi->wi_bp.b_dev = 0;
   1171 			wi->wi_bp.b_proc = p;
   1172 			wdioctlstrategy(&wi->wi_bp);
   1173 			error = wi->wi_bp.b_error;
   1174 		}
   1175 		*atareq = wi->wi_atareq;
   1176 		wi_free(wi);
   1177 		return(error);
   1178 		}
   1179 
   1180 	default:
   1181 		return ENOTTY;
   1182 	}
   1183 
   1184 #ifdef DIAGNOSTIC
   1185 	panic("wdioctl: impossible");
   1186 #endif
   1187 }
   1188 
   1189 #ifdef B_FORMAT
   1190 int
   1191 wdformat(struct buf *bp)
   1192 {
   1193 
   1194 	bp->b_flags |= B_FORMAT;
   1195 	return wdstrategy(bp);
   1196 }
   1197 #endif
   1198 
   1199 int
   1200 wdsize(dev)
   1201 	dev_t dev;
   1202 {
   1203 	struct wd_softc *wd;
   1204 	int part, omask;
   1205 	int size;
   1206 
   1207 	WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
   1208 
   1209 	wd = device_lookup(&wd_cd, WDUNIT(dev));
   1210 	if (wd == NULL)
   1211 		return (-1);
   1212 
   1213 	part = WDPART(dev);
   1214 	omask = wd->sc_dk.dk_openmask & (1 << part);
   1215 
   1216 	if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
   1217 		return (-1);
   1218 	if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
   1219 		size = -1;
   1220 	else
   1221 		size = wd->sc_dk.dk_label->d_partitions[part].p_size *
   1222 		    (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
   1223 	if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
   1224 		return (-1);
   1225 	return (size);
   1226 }
   1227 
   1228 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
   1229 static int wddoingadump = 0;
   1230 static int wddumprecalibrated = 0;
   1231 static int wddumpmulti = 1;
   1232 
   1233 /*
   1234  * Dump core after a system crash.
   1235  */
   1236 int
   1237 wddump(dev, blkno, va, size)
   1238 	dev_t dev;
   1239 	daddr_t blkno;
   1240 	caddr_t va;
   1241 	size_t size;
   1242 {
   1243 	struct wd_softc *wd;	/* disk unit to do the I/O */
   1244 	struct disklabel *lp;   /* disk's disklabel */
   1245 	int part, err;
   1246 	int nblks;	/* total number of sectors left to write */
   1247 	char errbuf[256];
   1248 
   1249 	/* Check if recursive dump; if so, punt. */
   1250 	if (wddoingadump)
   1251 		return EFAULT;
   1252 	wddoingadump = 1;
   1253 
   1254 	wd = device_lookup(&wd_cd, WDUNIT(dev));
   1255 	if (wd == NULL)
   1256 		return (ENXIO);
   1257 
   1258 	part = WDPART(dev);
   1259 
   1260 	/* Make sure it was initialized. */
   1261 	if (wd->drvp->state < READY)
   1262 		return ENXIO;
   1263 
   1264 	/* Convert to disk sectors.  Request must be a multiple of size. */
   1265 	lp = wd->sc_dk.dk_label;
   1266 	if ((size % lp->d_secsize) != 0)
   1267 		return EFAULT;
   1268 	nblks = size / lp->d_secsize;
   1269 	blkno = blkno / (lp->d_secsize / DEV_BSIZE);
   1270 
   1271 	/* Check transfer bounds against partition size. */
   1272 	if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
   1273 		return EINVAL;
   1274 
   1275 	/* Offset block number to start of partition. */
   1276 	blkno += lp->d_partitions[part].p_offset;
   1277 
   1278 	/* Recalibrate, if first dump transfer. */
   1279 	if (wddumprecalibrated == 0) {
   1280 		wddumpmulti = wd->sc_multi;
   1281 		wddumprecalibrated = 1;
   1282 		wd->drvp->state = RESET;
   1283 	}
   1284 
   1285 	while (nblks > 0) {
   1286 again:
   1287 		wd->sc_bp = NULL;
   1288 		wd->sc_wdc_bio.blkno = blkno;
   1289 		wd->sc_wdc_bio.flags = ATA_POLL;
   1290 		if (wddumpmulti == 1)
   1291 			wd->sc_wdc_bio.flags |= ATA_SINGLE;
   1292 		if (wd->sc_flags & WDF_LBA48)
   1293 			wd->sc_wdc_bio.flags |= ATA_LBA48;
   1294 		if (wd->sc_flags & WDF_LBA)
   1295 			wd->sc_wdc_bio.flags |= ATA_LBA;
   1296 		wd->sc_wdc_bio.bcount =
   1297 			min(nblks, wddumpmulti) * lp->d_secsize;
   1298 		wd->sc_wdc_bio.databuf = va;
   1299 #ifndef WD_DUMP_NOT_TRUSTED
   1300 		switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
   1301 		case WDC_TRY_AGAIN:
   1302 			panic("wddump: try again");
   1303 			break;
   1304 		case WDC_QUEUED:
   1305 			panic("wddump: polled command has been queued");
   1306 			break;
   1307 		case WDC_COMPLETE:
   1308 			break;
   1309 		}
   1310 		switch(wd->sc_wdc_bio.error) {
   1311 		case TIMEOUT:
   1312 			printf("wddump: device timed out");
   1313 			err = EIO;
   1314 			break;
   1315 		case ERR_DF:
   1316 			printf("wddump: drive fault");
   1317 			err = EIO;
   1318 			break;
   1319 		case ERR_DMA:
   1320 			printf("wddump: DMA error");
   1321 			err = EIO;
   1322 			break;
   1323 		case ERROR:
   1324 			errbuf[0] = '\0';
   1325 			wdperror(wd->drvp, wd->sc_wdc_bio.r_error, errbuf);
   1326 			printf("wddump: %s", errbuf);
   1327 			err = EIO;
   1328 			break;
   1329 		case NOERROR:
   1330 			err = 0;
   1331 			break;
   1332 		default:
   1333 			panic("wddump: unknown error type");
   1334 		}
   1335 		if (err != 0) {
   1336 			if (wddumpmulti != 1) {
   1337 				wddumpmulti = 1; /* retry in single-sector */
   1338 				printf(", retrying\n");
   1339 				goto again;
   1340 			}
   1341 			printf("\n");
   1342 			return err;
   1343 		}
   1344 #else	/* WD_DUMP_NOT_TRUSTED */
   1345 		/* Let's just talk about this first... */
   1346 		printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
   1347 		    unit, va, cylin, head, sector);
   1348 		delay(500 * 1000);	/* half a second */
   1349 #endif
   1350 
   1351 		/* update block count */
   1352 		nblks -= min(nblks, wddumpmulti);
   1353 		blkno += min(nblks, wddumpmulti);
   1354 		va += min(nblks, wddumpmulti) * lp->d_secsize;
   1355 	}
   1356 
   1357 	wddoingadump = 0;
   1358 	return 0;
   1359 }
   1360 
   1361 #ifdef HAS_BAD144_HANDLING
   1362 /*
   1363  * Internalize the bad sector table.
   1364  */
   1365 void
   1366 bad144intern(wd)
   1367 	struct wd_softc *wd;
   1368 {
   1369 	struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
   1370 	struct disklabel *lp = wd->sc_dk.dk_label;
   1371 	int i = 0;
   1372 
   1373 	WDCDEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
   1374 
   1375 	for (; i < NBT_BAD; i++) {
   1376 		if (bt->bt_bad[i].bt_cyl == 0xffff)
   1377 			break;
   1378 		wd->sc_badsect[i] =
   1379 		    bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
   1380 		    (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
   1381 		    (bt->bt_bad[i].bt_trksec & 0xff);
   1382 	}
   1383 	for (; i < NBT_BAD+1; i++)
   1384 		wd->sc_badsect[i] = -1;
   1385 }
   1386 #endif
   1387 
   1388 int
   1389 wd_get_params(wd, flags, params)
   1390 	struct wd_softc *wd;
   1391 	u_int8_t flags;
   1392 	struct ataparams *params;
   1393 {
   1394 	switch (wd->atabus->ata_get_params(wd->drvp, flags, params)) {
   1395 	case CMD_AGAIN:
   1396 		return 1;
   1397 	case CMD_ERR:
   1398 		/*
   1399 		 * We `know' there's a drive here; just assume it's old.
   1400 		 * This geometry is only used to read the MBR and print a
   1401 		 * (false) attach message.
   1402 		 */
   1403 		strncpy(params->atap_model, "ST506",
   1404 		    sizeof params->atap_model);
   1405 		params->atap_config = ATA_CFG_FIXED;
   1406 		params->atap_cylinders = 1024;
   1407 		params->atap_heads = 8;
   1408 		params->atap_sectors = 17;
   1409 		params->atap_multi = 1;
   1410 		params->atap_capabilities1 = params->atap_capabilities2 = 0;
   1411 		wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
   1412 		return 0;
   1413 	case CMD_OK:
   1414 		return 0;
   1415 	default:
   1416 		panic("wd_get_params: bad return code from ata_get_params");
   1417 		/* NOTREACHED */
   1418 	}
   1419 }
   1420 
   1421 void
   1422 wd_flushcache(wd, flags)
   1423 	struct wd_softc *wd;
   1424 	int flags;
   1425 {
   1426 	struct wdc_command wdc_c;
   1427 
   1428 	if (wd->drvp->ata_vers < 4) /* WDCC_FLUSHCACHE is here since ATA-4 */
   1429 		return;
   1430 	memset(&wdc_c, 0, sizeof(struct wdc_command));
   1431 	wdc_c.r_command = WDCC_FLUSHCACHE;
   1432 	wdc_c.r_st_bmask = WDCS_DRDY;
   1433 	wdc_c.r_st_pmask = WDCS_DRDY;
   1434 	wdc_c.flags = flags;
   1435 	wdc_c.timeout = 30000; /* 30s timeout */
   1436 	if (wd->atabus->ata_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
   1437 		printf("%s: flush cache command didn't complete\n",
   1438 		    wd->sc_dev.dv_xname);
   1439 	}
   1440 	if (wdc_c.flags & AT_TIMEOU) {
   1441 		printf("%s: flush cache command timeout\n",
   1442 		    wd->sc_dev.dv_xname);
   1443 	}
   1444 	if (wdc_c.flags & AT_DF) {
   1445 		printf("%s: flush cache command: drive fault\n",
   1446 		    wd->sc_dev.dv_xname);
   1447 	}
   1448 	/*
   1449 	 * Ignore error register, it shouldn't report anything else
   1450 	 * than COMMAND ABORTED, which means the device doesn't support
   1451 	 * flush cache
   1452 	 */
   1453 }
   1454 
   1455 void
   1456 wd_shutdown(arg)
   1457 	void *arg;
   1458 {
   1459 	struct wd_softc *wd = arg;
   1460 	wd_flushcache(wd, AT_POLL);
   1461 }
   1462 
   1463 /*
   1464  * Allocate space for a ioctl queue structure.  Mostly taken from
   1465  * scsipi_ioctl.c
   1466  */
   1467 struct wd_ioctl *
   1468 wi_get()
   1469 {
   1470 	struct wd_ioctl *wi;
   1471 	int s;
   1472 
   1473 	wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK|M_ZERO);
   1474 	s = splbio();
   1475 	LIST_INSERT_HEAD(&wi_head, wi, wi_list);
   1476 	splx(s);
   1477 	return (wi);
   1478 }
   1479 
   1480 /*
   1481  * Free an ioctl structure and remove it from our list
   1482  */
   1483 
   1484 void
   1485 wi_free(wi)
   1486 	struct wd_ioctl *wi;
   1487 {
   1488 	int s;
   1489 
   1490 	s = splbio();
   1491 	LIST_REMOVE(wi, wi_list);
   1492 	splx(s);
   1493 	free(wi, M_TEMP);
   1494 }
   1495 
   1496 /*
   1497  * Find a wd_ioctl structure based on the struct buf.
   1498  */
   1499 
   1500 struct wd_ioctl *
   1501 wi_find(bp)
   1502 	struct buf *bp;
   1503 {
   1504 	struct wd_ioctl *wi;
   1505 	int s;
   1506 
   1507 	s = splbio();
   1508 	for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
   1509 		if (bp == &wi->wi_bp)
   1510 			break;
   1511 	splx(s);
   1512 	return (wi);
   1513 }
   1514 
   1515 /*
   1516  * Ioctl pseudo strategy routine
   1517  *
   1518  * This is mostly stolen from scsipi_ioctl.c:scsistrategy().  What
   1519  * happens here is:
   1520  *
   1521  * - wdioctl() queues a wd_ioctl structure.
   1522  *
   1523  * - wdioctl() calls physio/wdioctlstrategy based on whether or not
   1524  *   user space I/O is required.  If physio() is called, physio() eventually
   1525  *   calls wdioctlstrategy().
   1526  *
   1527  * - In either case, wdioctlstrategy() calls wd->atabus->ata_exec_command()
   1528  *   to perform the actual command
   1529  *
   1530  * The reason for the use of the pseudo strategy routine is because
   1531  * when doing I/O to/from user space, physio _really_ wants to be in
   1532  * the loop.  We could put the entire buffer into the ioctl request
   1533  * structure, but that won't scale if we want to do things like download
   1534  * microcode.
   1535  */
   1536 
   1537 void
   1538 wdioctlstrategy(bp)
   1539 	struct buf *bp;
   1540 {
   1541 	struct wd_ioctl *wi;
   1542 	struct wdc_command wdc_c;
   1543 	int error = 0;
   1544 
   1545 	wi = wi_find(bp);
   1546 	if (wi == NULL) {
   1547 		printf("user_strat: No ioctl\n");
   1548 		error = EINVAL;
   1549 		goto bad;
   1550 	}
   1551 
   1552 	memset(&wdc_c, 0, sizeof(wdc_c));
   1553 
   1554 	/*
   1555 	 * Abort if physio broke up the transfer
   1556 	 */
   1557 
   1558 	if (bp->b_bcount != wi->wi_atareq.datalen) {
   1559 		printf("physio split wd ioctl request... cannot proceed\n");
   1560 		error = EIO;
   1561 		goto bad;
   1562 	}
   1563 
   1564 	/*
   1565 	 * Abort if we didn't get a buffer size that was a multiple of
   1566 	 * our sector size (or was larger than NBBY)
   1567 	 */
   1568 
   1569 	if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
   1570 	    (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
   1571 	     (1 << NBBY)) {
   1572 		error = EINVAL;
   1573 		goto bad;
   1574 	}
   1575 
   1576 	/*
   1577 	 * Make sure a timeout was supplied in the ioctl request
   1578 	 */
   1579 
   1580 	if (wi->wi_atareq.timeout == 0) {
   1581 		error = EINVAL;
   1582 		goto bad;
   1583 	}
   1584 
   1585 	if (wi->wi_atareq.flags & ATACMD_READ)
   1586 		wdc_c.flags |= AT_READ;
   1587 	else if (wi->wi_atareq.flags & ATACMD_WRITE)
   1588 		wdc_c.flags |= AT_WRITE;
   1589 
   1590 	if (wi->wi_atareq.flags & ATACMD_READREG)
   1591 		wdc_c.flags |= AT_READREG;
   1592 
   1593 	wdc_c.flags |= AT_WAIT;
   1594 
   1595 	wdc_c.timeout = wi->wi_atareq.timeout;
   1596 	wdc_c.r_command = wi->wi_atareq.command;
   1597 	wdc_c.r_head = wi->wi_atareq.head & 0x0f;
   1598 	wdc_c.r_cyl = wi->wi_atareq.cylinder;
   1599 	wdc_c.r_sector = wi->wi_atareq.sec_num;
   1600 	wdc_c.r_count = wi->wi_atareq.sec_count;
   1601 	wdc_c.r_precomp = wi->wi_atareq.features;
   1602 	wdc_c.r_st_bmask = WDCS_DRDY;
   1603 	wdc_c.r_st_pmask = WDCS_DRDY;
   1604 	wdc_c.data = wi->wi_bp.b_data;
   1605 	wdc_c.bcount = wi->wi_bp.b_bcount;
   1606 
   1607 	if (wi->wi_softc->atabus->ata_exec_command(wi->wi_softc->drvp, &wdc_c)
   1608 	    != WDC_COMPLETE) {
   1609 		wi->wi_atareq.retsts = ATACMD_ERROR;
   1610 		goto bad;
   1611 	}
   1612 
   1613 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
   1614 		if (wdc_c.flags & AT_ERROR) {
   1615 			wi->wi_atareq.retsts = ATACMD_ERROR;
   1616 			wi->wi_atareq.error = wdc_c.r_error;
   1617 		} else if (wdc_c.flags & AT_DF)
   1618 			wi->wi_atareq.retsts = ATACMD_DF;
   1619 		else
   1620 			wi->wi_atareq.retsts = ATACMD_TIMEOUT;
   1621 	} else {
   1622 		wi->wi_atareq.retsts = ATACMD_OK;
   1623 		if (wi->wi_atareq.flags & ATACMD_READREG) {
   1624 			wi->wi_atareq.head = wdc_c.r_head ;
   1625 			wi->wi_atareq.cylinder = wdc_c.r_cyl;
   1626 			wi->wi_atareq.sec_num = wdc_c.r_sector;
   1627 			wi->wi_atareq.sec_count = wdc_c.r_count;
   1628 			wi->wi_atareq.features = wdc_c.r_precomp;
   1629 			wi->wi_atareq.error = wdc_c.r_error;
   1630 		}
   1631 	}
   1632 
   1633 	bp->b_error = 0;
   1634 	biodone(bp);
   1635 	return;
   1636 bad:
   1637 	bp->b_flags |= B_ERROR;
   1638 	bp->b_error = error;
   1639 	biodone(bp);
   1640 }
   1641