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