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