Home | History | Annotate | Line # | Download | only in ata
wd.c revision 1.153
      1 /*	$NetBSD: wd.c,v 1.153 1996/10/13 01:38:04 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
      5  *
      6  * DMA and multi-sector PIO handling are derived from code contributed by
      7  * Onno van der Linden.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by Charles M. Hannum.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/kernel.h>
     38 #include <sys/conf.h>
     39 #include <sys/file.h>
     40 #include <sys/stat.h>
     41 #include <sys/ioctl.h>
     42 #include <sys/buf.h>
     43 #include <sys/uio.h>
     44 #include <sys/malloc.h>
     45 #include <sys/device.h>
     46 #include <sys/disklabel.h>
     47 #include <sys/disk.h>
     48 #include <sys/syslog.h>
     49 #include <sys/proc.h>
     50 
     51 #include <vm/vm.h>
     52 
     53 #include <machine/cpu.h>
     54 #include <machine/intr.h>
     55 #include <machine/pio.h>
     56 
     57 #include <dev/isa/isavar.h>
     58 #include <dev/isa/isadmavar.h>
     59 #include <dev/isa/wdreg.h>
     60 
     61 #define	WAITTIME	(4 * hz)	/* time to wait for a completion */
     62 #define	RECOVERYTIME	(hz / 2)	/* time to recover from an error */
     63 
     64 #define WDCDELAY	100
     65 #define WDCNDELAY	100000		/* delay = 100us; so 10s for a controller state change */
     66 #if 0
     67 /* If you enable this, it will report any delays more than 100us * N long. */
     68 #define WDCNDELAY_DEBUG	10
     69 #endif
     70 
     71 #define	WDIORETRIES	5		/* number of retries before giving up */
     72 
     73 #define	WDUNIT(dev)			DISKUNIT(dev)
     74 #define	WDPART(dev)			DISKPART(dev)
     75 #define	MAKEWDDEV(maj, unit, part)	MAKEDISKDEV(maj, unit, part)
     76 
     77 #define	WDLABELDEV(dev)	(MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART))
     78 
     79 struct wd_softc {
     80 	struct device sc_dev;
     81 	struct disk sc_dk;
     82 
     83 	/* Information about the current transfer: */
     84 	daddr_t sc_blkno;	/* starting block number */
     85 	int sc_bcount;		/* byte count left */
     86 	int sc_skip;		/* bytes already transferred */
     87 	int sc_nblks;		/* number of blocks currently transferring */
     88 	int sc_nbytes;		/* number of bytes currently transferring */
     89 
     90 	/* Long-term state: */
     91 	int sc_drive;			/* physical unit number */
     92 	int sc_state;			/* control state */
     93 #define	RECAL		0		/* recalibrate */
     94 #define	RECAL_WAIT	1		/* done recalibrating */
     95 #define	GEOMETRY	2		/* upload geometry */
     96 #define	GEOMETRY_WAIT	3		/* done uploading geometry */
     97 #define	MULTIMODE	4		/* set multiple mode */
     98 #define	MULTIMODE_WAIT	5		/* done setting multiple mode */
     99 #define	OPEN		6		/* done with open */
    100 	int sc_mode;			/* transfer mode */
    101 #define	WDM_PIOSINGLE	0		/* single-sector PIO */
    102 #define	WDM_PIOMULTI	1		/* multi-sector PIO */
    103 #define	WDM_DMA		2		/* DMA */
    104 	int sc_multiple;		/* multiple for WDM_PIOMULTI */
    105 	int sc_flags;			/* drive characteistics found */
    106 #define	WDF_LOCKED	0x01
    107 #define	WDF_WANTED	0x02
    108 #define	WDF_WLABEL	0x04		/* label is writable */
    109 #define	WDF_LABELLING	0x08		/* writing label */
    110 /* XXX Nothing resets this yet, but disk change sensing will when ATAPI is
    111    implemented. */
    112 #define	WDF_LOADED	0x10		/* parameters loaded */
    113 #define	WDF_32BIT	0x20		/* can do 32-bit transfer */
    114 
    115 	struct wdparams sc_params;	/* ESDI/ATA drive parameters */
    116 	daddr_t	sc_badsect[127];	/* 126 plus trailing -1 marker */
    117 
    118 	TAILQ_ENTRY(wd_softc) sc_drivechain;
    119 	struct buf sc_q;
    120 };
    121 
    122 struct wdc_softc {
    123 	struct device sc_dev;
    124 	void *sc_ih;
    125 
    126 	int sc_iobase;			/* I/O port base */
    127 	int sc_drq;			/* DMA channel */
    128 
    129 	TAILQ_HEAD(drivehead, wd_softc) sc_drives;
    130 	int sc_flags;
    131 #define	WDCF_ACTIVE	0x01		/* controller is active */
    132 #define	WDCF_SINGLE	0x02		/* sector at a time mode */
    133 #define	WDCF_ERROR	0x04		/* processing a disk error */
    134 #define	WDCF_WANTED	0x08		/* XXX locking for wd_get_parms() */
    135 	int sc_errors;			/* errors during current transfer */
    136 	u_char sc_status;		/* copy of status register */
    137 	u_char sc_error;		/* copy of error register */
    138 };
    139 
    140 int	wdcprobe 	__P((struct device *, void *, void *));
    141 void	wdcattach 	__P((struct device *, struct device *, void *));
    142 int	wdcintr		__P((void *));
    143 
    144 struct cfattach wdc_ca = {
    145 	sizeof(struct wdc_softc), wdcprobe, wdcattach
    146 };
    147 
    148 struct cfdriver wdc_cd = {
    149 	NULL, "wdc", DV_DULL
    150 };
    151 
    152 int wdprobe __P((struct device *, void *, void *));
    153 void wdattach __P((struct device *, struct device *, void *));
    154 int wdprint __P((void *, const char *));
    155 
    156 struct cfattach wd_ca = {
    157 	sizeof(struct wd_softc), wdprobe, wdattach
    158 };
    159 
    160 struct cfdriver wd_cd = {
    161 	NULL, "wd", DV_DISK
    162 };
    163 
    164 void	wdgetdisklabel	__P((struct wd_softc *));
    165 int	wd_get_parms	__P((struct wd_softc *));
    166 void	wdstrategy	__P((struct buf *));
    167 void	wdstart		__P((struct wd_softc *));
    168 
    169 struct dkdriver wddkdriver = { wdstrategy };
    170 
    171 /* XXX: these should go elsewhere */
    172 cdev_decl(wd);
    173 bdev_decl(wd);
    174 
    175 void	wdfinish	__P((struct wd_softc *, struct buf *));
    176 int 	dcintr		__P((void *));
    177 void	wdcstart	__P((struct wdc_softc *));
    178 int	wdcommand	__P((struct wd_softc *, int, int, int, int, int));
    179 int	wdcommandshort	__P((struct wdc_softc *, int, int));
    180 int	wdcontrol	__P((struct wd_softc *));
    181 int	wdsetctlr	__P((struct wd_softc *));
    182 static void bad144intern __P((struct wd_softc *));
    183 int	wdcreset	__P((struct wdc_softc *));
    184 void	wdcrestart	__P((void *arg));
    185 void	wdcunwedge	__P((struct wdc_softc *));
    186 void	wdctimeout	__P((void *arg));
    187 void	wderror		__P((void *, struct buf *, char *));
    188 int	wdcwait		__P((struct wdc_softc *, int));
    189 int	wdlock		__P((struct wd_softc *));
    190 void	wdunlock	__P((struct wd_softc *));
    191 
    192 /* ST506 spec says that if READY or SEEKCMPLT go off, then the read or write
    193    command is aborted. */
    194 #define	wait_for_drq(d)		wdcwait(d, WDCS_DRDY | WDCS_DSC | WDCS_DRQ)
    195 #define	wait_for_ready(d)	wdcwait(d, WDCS_DRDY | WDCS_DSC)
    196 #define	wait_for_unbusy(d)	wdcwait(d, 0)
    197 
    198 int
    199 wdcprobe(parent, match, aux)
    200 	struct device *parent;
    201 	void *match, *aux;
    202 {
    203 	struct wdc_softc *wdc = match;
    204 	struct isa_attach_args *ia = aux;
    205 	int iobase;
    206 
    207 	wdc->sc_iobase = iobase = ia->ia_iobase;
    208 
    209 	/* Check if we have registers that work. */
    210 	outb(iobase+wd_error, 0x5a);	/* Error register not writable, */
    211 	outb(iobase+wd_cyl_lo, 0xa5);	/* but all of cyllo are. */
    212 	if (inb(iobase+wd_error) == 0x5a || inb(iobase+wd_cyl_lo) != 0xa5)
    213 		return 0;
    214 
    215 	if (wdcreset(wdc) != 0) {
    216 		delay(500000);
    217 		if (wdcreset(wdc) != 0)
    218 			return 0;
    219 	}
    220 
    221 	/* Select drive 0. */
    222 	outb(iobase+wd_sdh, WDSD_IBM | 0);
    223 
    224 	/* Wait for controller to become ready. */
    225 	if (wait_for_unbusy(wdc) < 0)
    226 		return 0;
    227 
    228 	/* Start drive diagnostics. */
    229 	outb(iobase+wd_command, WDCC_DIAGNOSE);
    230 
    231 	/* Wait for command to complete. */
    232 	if (wait_for_unbusy(wdc) < 0)
    233 		return 0;
    234 
    235 	ia->ia_iosize = 8;
    236 	ia->ia_msize = 0;
    237 	return 1;
    238 }
    239 
    240 struct wdc_attach_args {
    241 	int wa_drive;
    242 };
    243 
    244 int
    245 wdprint(aux, wdc)
    246 	void *aux;
    247 	const char *wdc;
    248 {
    249 	struct wdc_attach_args *wa = aux;
    250 
    251 	if (!wdc)
    252 		printf(" drive %d", wa->wa_drive);
    253 	return QUIET;
    254 }
    255 
    256 void
    257 wdcattach(parent, self, aux)
    258 	struct device *parent, *self;
    259 	void *aux;
    260 {
    261 	struct wdc_softc *wdc = (void *)self;
    262 	struct isa_attach_args *ia = aux;
    263 	struct wdc_attach_args wa;
    264 
    265 	TAILQ_INIT(&wdc->sc_drives);
    266 	wdc->sc_drq = ia->ia_drq;
    267 
    268 	printf("\n");
    269 
    270 	wdc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    271 	    IPL_BIO, wdcintr, wdc);
    272 
    273 	for (wa.wa_drive = 0; wa.wa_drive < 2; wa.wa_drive++)
    274 		(void)config_found(self, (void *)&wa, wdprint);
    275 }
    276 
    277 int
    278 wdprobe(parent, match, aux)
    279 	struct device *parent;
    280 	void *match, *aux;
    281 {
    282 	struct wdc_softc *wdc = (void *)parent;
    283 	struct cfdata *cf = match;
    284 	struct wdc_attach_args *wa = aux;
    285 	int drive = wa->wa_drive;
    286 
    287 	if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != drive)
    288 		return 0;
    289 
    290 	if (wdcommandshort(wdc, drive, WDCC_RECAL) != 0 ||
    291 	    wait_for_ready(wdc) != 0)
    292 		return 0;
    293 
    294 	return 1;
    295 }
    296 
    297 void
    298 wdattach(parent, self, aux)
    299 	struct device *parent, *self;
    300 	void *aux;
    301 {
    302 	struct wd_softc *wd = (void *)self;
    303 	struct wdc_softc *wdc = (void *)parent;
    304 	struct wdc_attach_args *wa = aux;
    305 	int i, blank;
    306 	char buf[41], c, *p, *q;
    307 
    308 	wd->sc_drive = wa->wa_drive;
    309 
    310 	/*
    311 	 * Initialize and attach the disk structure.
    312 	 */
    313 	wd->sc_dk.dk_driver = &wddkdriver;
    314 	wd->sc_dk.dk_name = wd->sc_dev.dv_xname;
    315 	disk_attach(&wd->sc_dk);
    316 
    317 	wd_get_parms(wd);
    318 	for (blank = 0, p = wd->sc_params.wdp_model, q = buf, i = 0;
    319 	     i < sizeof(wd->sc_params.wdp_model); i++) {
    320 		c = *p++;
    321 		if (c == '\0')
    322 			break;
    323 		if (c != ' ') {
    324 			if (blank) {
    325 				*q++ = ' ';
    326 				blank = 0;
    327 			}
    328 			*q++ = c;
    329 		} else
    330 			blank = 1;
    331 	}
    332 	*q++ = '\0';
    333 
    334 	printf(": %dMB, %d cyl, %d head, %d sec, %d bytes/sec <%s>\n",
    335 	    wd->sc_params.wdp_cylinders *
    336 	    (wd->sc_params.wdp_heads * wd->sc_params.wdp_sectors) /
    337 	    (1048576 / DEV_BSIZE),
    338 	    wd->sc_params.wdp_cylinders,
    339 	    wd->sc_params.wdp_heads,
    340 	    wd->sc_params.wdp_sectors,
    341 	    DEV_BSIZE,
    342 	    buf);
    343 
    344 	if ((wd->sc_params.wdp_capabilities & WD_CAP_DMA) != 0 &&
    345 	    wdc->sc_drq != DRQUNK) {
    346 		wd->sc_mode = WDM_DMA;
    347 	} else if (wd->sc_params.wdp_maxmulti > 1) {
    348 		wd->sc_mode = WDM_PIOMULTI;
    349 		wd->sc_multiple = min(wd->sc_params.wdp_maxmulti, 16);
    350 	} else {
    351 		wd->sc_mode = WDM_PIOSINGLE;
    352 		wd->sc_multiple = 1;
    353 	}
    354 
    355 	printf("%s: using", wd->sc_dev.dv_xname);
    356 	if (wd->sc_mode == WDM_DMA)
    357 		printf(" dma transfers,");
    358 	else
    359 		printf(" %d-sector %d-bit pio transfers,",
    360 		    wd->sc_multiple, (wd->sc_flags & WDF_32BIT) == 0 ? 16 : 32);
    361 	if ((wd->sc_params.wdp_capabilities & WD_CAP_LBA) != 0)
    362 		printf(" lba addressing\n");
    363 	else
    364 		printf(" chs addressing\n");
    365 }
    366 
    367 /*
    368  * Read/write routine for a buffer.  Validates the arguments and schedules the
    369  * transfer.  Does not wait for the transfer to complete.
    370  */
    371 void
    372 wdstrategy(bp)
    373 	struct buf *bp;
    374 {
    375 	struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(bp->b_dev)];
    376 	int s;
    377 
    378 	/* Valid request?  */
    379 	if (bp->b_blkno < 0 ||
    380 	    (bp->b_bcount % wd->sc_dk.dk_label->d_secsize) != 0 ||
    381 	    (bp->b_bcount / wd->sc_dk.dk_label->d_secsize) >= (1 << NBBY)) {
    382 		bp->b_error = EINVAL;
    383 		goto bad;
    384 	}
    385 
    386 	/* If device invalidated (e.g. media change, door open), error. */
    387 	if ((wd->sc_flags & WDF_LOADED) == 0) {
    388 		bp->b_error = EIO;
    389 		goto bad;
    390 	}
    391 
    392 	/* If it's a null transfer, return immediately. */
    393 	if (bp->b_bcount == 0)
    394 		goto done;
    395 
    396 	/*
    397 	 * Do bounds checking, adjust transfer. if error, process.
    398 	 * If end of partition, just return.
    399 	 */
    400 	if (WDPART(bp->b_dev) != RAW_PART &&
    401 	    bounds_check_with_label(bp, wd->sc_dk.dk_label,
    402 	    (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
    403 		goto done;
    404 
    405 	/* Queue transfer on drive, activate drive and controller if idle. */
    406 	s = splbio();
    407 	disksort(&wd->sc_q, bp);
    408 	if (!wd->sc_q.b_active)
    409 		wdstart(wd);
    410 #if 0
    411 	else {
    412 		struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
    413 		if ((wdc->sc_flags & (WDCF_ACTIVE|WDCF_ERROR)) == 0) {
    414 			printf("wdstrategy: controller inactive\n");
    415 			wdcstart(wdc);
    416 		}
    417 	}
    418 #endif
    419 	splx(s);
    420 	return;
    421 
    422 bad:
    423 	bp->b_flags |= B_ERROR;
    424 done:
    425 	/* Toss transfer; we're done early. */
    426 	bp->b_resid = bp->b_bcount;
    427 	biodone(bp);
    428 }
    429 
    430 /*
    431  * Queue a drive for I/O.
    432  */
    433 void
    434 wdstart(wd)
    435 	struct wd_softc *wd;
    436 {
    437 	struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
    438 	int active = wdc->sc_drives.tqh_first != 0;
    439 
    440 	/* Link onto controller queue. */
    441 	wd->sc_q.b_active = 1;
    442 	TAILQ_INSERT_TAIL(&wdc->sc_drives, wd, sc_drivechain);
    443 
    444 	disk_busy(&wd->sc_dk);
    445 
    446 	/* If controller not already active, start it. */
    447 	if (!active)
    448 		wdcstart(wdc);
    449 }
    450 
    451 /*
    452  * Finish an I/O operation.  Clean up the drive and controller state, set the
    453  * residual count, and inform the upper layers that the operation is complete.
    454  */
    455 void
    456 wdfinish(wd, bp)
    457 	struct wd_softc *wd;
    458 	struct buf *bp;
    459 {
    460 	struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
    461 
    462 	wdc->sc_flags &= ~(WDCF_SINGLE | WDCF_ERROR);
    463 	wdc->sc_errors = 0;
    464 	/*
    465 	 * Move this drive to the end of the queue to give others a `fair'
    466 	 * chance.
    467 	 */
    468 	if (wd->sc_drivechain.tqe_next) {
    469 		TAILQ_REMOVE(&wdc->sc_drives, wd, sc_drivechain);
    470 		if (bp->b_actf) {
    471 			TAILQ_INSERT_TAIL(&wdc->sc_drives, wd, sc_drivechain);
    472 		} else
    473 			wd->sc_q.b_active = 0;
    474 	}
    475 	bp->b_resid = wd->sc_bcount;
    476 	wd->sc_skip = 0;
    477 	wd->sc_q.b_actf = bp->b_actf;
    478 
    479 	disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid));
    480 
    481 	if (!wd->sc_q.b_actf) {
    482 		TAILQ_REMOVE(&wdc->sc_drives, wd, sc_drivechain);
    483 		wd->sc_q.b_active = 0;
    484 	} else
    485 		disk_busy(&wd->sc_dk);
    486 
    487 	biodone(bp);
    488 }
    489 
    490 int
    491 wdread(dev, uio, flags)
    492 	dev_t dev;
    493 	struct uio *uio;
    494 	int flags;
    495 {
    496 
    497 	return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
    498 }
    499 
    500 int
    501 wdwrite(dev, uio, flags)
    502 	dev_t dev;
    503 	struct uio *uio;
    504 	int flags;
    505 {
    506 
    507 	return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
    508 }
    509 
    510 /*
    511  * Start I/O on a controller.  This does the calculation, and starts a read or
    512  * write operation.  Called to from wdstart() to start a transfer, from
    513  * wdcintr() to continue a multi-sector transfer or start the next transfer, or
    514  * wdcrestart() after recovering from an error.
    515  */
    516 void
    517 wdcstart(wdc)
    518 	struct wdc_softc *wdc;
    519 {
    520 	struct wd_softc *wd;
    521 	struct buf *bp;
    522 	struct disklabel *lp;
    523 	int nblks;
    524 
    525 #ifdef DIAGNOSTIC
    526 	if ((wdc->sc_flags & WDCF_ACTIVE) != 0)
    527 		panic("wdcstart: controller still active");
    528 #endif
    529 
    530 	/*
    531 	 * XXX
    532 	 * This is a kluge.  See comments in wd_get_parms().
    533 	 */
    534 	if ((wdc->sc_flags & WDCF_WANTED) != 0) {
    535 		wdc->sc_flags &= ~WDCF_WANTED;
    536 		wakeup(wdc);
    537 		return;
    538 	}
    539 
    540 loop:
    541 	/* Is there a drive for the controller to do a transfer with? */
    542 	wd = wdc->sc_drives.tqh_first;
    543 	if (wd == NULL)
    544 		return;
    545 
    546 	/* Is there a transfer to this drive?  If not, deactivate drive. */
    547 	bp = wd->sc_q.b_actf;
    548 
    549 	if (wdc->sc_errors >= WDIORETRIES) {
    550 		wderror(wd, bp, "hard error");
    551 		bp->b_error = EIO;
    552 		bp->b_flags |= B_ERROR;
    553 		wdfinish(wd, bp);
    554 		goto loop;
    555 	}
    556 
    557 	/* Do control operations specially. */
    558 	if (wd->sc_state < OPEN) {
    559 		/*
    560 		 * Actually, we want to be careful not to mess with the control
    561 		 * state if the device is currently busy, but we can assume
    562 		 * that we never get to this point if that's the case.
    563 		 */
    564 		if (wdcontrol(wd) == 0) {
    565 			/* The drive is busy.  Wait. */
    566 			return;
    567 		}
    568 	}
    569 
    570 	/*
    571 	 * WDCF_ERROR is set by wdcunwedge() and wdcintr() when an error is
    572 	 * encountered.  If we are in multi-sector mode, then we switch to
    573 	 * single-sector mode and retry the operation from the start.
    574 	 */
    575 	if (wdc->sc_flags & WDCF_ERROR) {
    576 		wdc->sc_flags &= ~WDCF_ERROR;
    577 		if ((wdc->sc_flags & WDCF_SINGLE) == 0) {
    578 			wdc->sc_flags |= WDCF_SINGLE;
    579 			wd->sc_skip = 0;
    580 		}
    581 	}
    582 
    583 	lp = wd->sc_dk.dk_label;
    584 
    585 	/* When starting a transfer... */
    586 	if (wd->sc_skip == 0) {
    587 		int part = WDPART(bp->b_dev);
    588 		daddr_t blkno;
    589 
    590 #ifdef WDDEBUG
    591 		printf("\n%s: wdcstart %s %d@%d; map ", wd->sc_dev.dv_xname,
    592 		    (bp->b_flags & B_READ) ? "read" : "write", bp->b_bcount,
    593 		    bp->b_blkno);
    594 #endif
    595 		wd->sc_bcount = bp->b_bcount;
    596 		blkno = bp->b_blkno;
    597 		if (part != RAW_PART)
    598 			blkno += lp->d_partitions[part].p_offset;
    599 		wd->sc_blkno = blkno / (lp->d_secsize / DEV_BSIZE);
    600 	} else {
    601 #ifdef WDDEBUG
    602 		printf(" %d)%x", wd->sc_skip, inb(wd->sc_iobase+wd_altsts));
    603 #endif
    604 	}
    605 
    606 	/* When starting a multi-sector transfer, or doing single-sector
    607 	    transfers... */
    608 	if (wd->sc_skip == 0 || (wdc->sc_flags & WDCF_SINGLE) != 0 ||
    609 	    wd->sc_mode == WDM_DMA) {
    610 		daddr_t blkno = wd->sc_blkno;
    611 		long cylin, head, sector;
    612 		int command;
    613 
    614 		if ((wdc->sc_flags & WDCF_SINGLE) != 0)
    615 			nblks = 1;
    616 		else if (wd->sc_mode != WDM_DMA)
    617 			nblks = wd->sc_bcount / lp->d_secsize;
    618 		else
    619 			nblks = min(wd->sc_bcount / lp->d_secsize, 8);
    620 
    621 		/* Check for bad sectors and adjust transfer, if necessary. */
    622 		if ((lp->d_flags & D_BADSECT) != 0
    623 #ifdef B_FORMAT
    624 		    && (bp->b_flags & B_FORMAT) == 0
    625 #endif
    626 		    ) {
    627 			long blkdiff;
    628 			int i;
    629 
    630 			for (i = 0; (blkdiff = wd->sc_badsect[i]) != -1; i++) {
    631 				blkdiff -= blkno;
    632 				if (blkdiff < 0)
    633 					continue;
    634 				if (blkdiff == 0) {
    635 					/* Replace current block of transfer. */
    636 					blkno =
    637 					    lp->d_secperunit - lp->d_nsectors - i - 1;
    638 				}
    639 				if (blkdiff < nblks) {
    640 					/* Bad block inside transfer. */
    641 					wdc->sc_flags |= WDCF_SINGLE;
    642 					nblks = 1;
    643 				}
    644 				break;
    645 			}
    646 			/* Tranfer is okay now. */
    647 		}
    648 
    649 		if ((wd->sc_params.wdp_capabilities & WD_CAP_LBA) != 0) {
    650 			sector = (blkno >> 0) & 0xff;
    651 			cylin = (blkno >> 8) & 0xffff;
    652 			head = (blkno >> 24) & 0xf;
    653 			head |= WDSD_LBA;
    654 		} else {
    655 			sector = blkno % lp->d_nsectors;
    656 			sector++;	/* Sectors begin with 1, not 0. */
    657 			blkno /= lp->d_nsectors;
    658 			head = blkno % lp->d_ntracks;
    659 			blkno /= lp->d_ntracks;
    660 			cylin = blkno;
    661 			head |= WDSD_CHS;
    662 		}
    663 
    664 		if (wd->sc_mode == WDM_PIOSINGLE ||
    665 		    (wdc->sc_flags & WDCF_SINGLE) != 0)
    666 			wd->sc_nblks = 1;
    667 		else if (wd->sc_mode == WDM_PIOMULTI)
    668 			wd->sc_nblks = min(nblks, wd->sc_multiple);
    669 		else
    670 			wd->sc_nblks = nblks;
    671 		wd->sc_nbytes = wd->sc_nblks * lp->d_secsize;
    672 
    673 #ifdef B_FORMAT
    674 		if (bp->b_flags & B_FORMAT) {
    675 			sector = lp->d_gap3;
    676 			nblks = lp->d_nsectors;
    677 			command = WDCC_FORMAT;
    678 		} else
    679 #endif
    680 		switch (wd->sc_mode) {
    681 		case WDM_DMA:
    682 			command = (bp->b_flags & B_READ) ?
    683 			    WDCC_READDMA : WDCC_WRITEDMA;
    684 			/* Start the DMA channel and bounce the buffer if
    685 			   necessary. */
    686 			isa_dmastart(
    687 			    bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE,
    688 			    bp->b_data + wd->sc_skip,
    689 			    wd->sc_nbytes, wdc->sc_drq);
    690 			break;
    691 		case WDM_PIOMULTI:
    692 			command = (bp->b_flags & B_READ) ?
    693 			    WDCC_READMULTI : WDCC_WRITEMULTI;
    694 			break;
    695 		case WDM_PIOSINGLE:
    696 			command = (bp->b_flags & B_READ) ?
    697 			    WDCC_READ : WDCC_WRITE;
    698 			break;
    699 		default:
    700 #ifdef DIAGNOSTIC
    701 			panic("bad wd mode");
    702 #endif
    703 			return;
    704 		}
    705 
    706 		/* Initiate command! */
    707 		if (wdcommand(wd, command, cylin, head, sector, nblks) != 0) {
    708 			wderror(wd, NULL,
    709 			    "wdcstart: timeout waiting for unbusy");
    710 			wdcunwedge(wdc);
    711 			return;
    712 		}
    713 
    714 #ifdef WDDEBUG
    715 		printf("sector %d cylin %d head %d addr %x sts %x\n", sector,
    716 		    cylin, head, bp->b_data, inb(wd->sc_iobase+wd_altsts));
    717 #endif
    718 	} else if (wd->sc_nblks > 1) {
    719 		/* The number of blocks in the last stretch may be smaller. */
    720 		nblks = wd->sc_bcount / lp->d_secsize;
    721 		if (wd->sc_nblks > nblks) {
    722 			wd->sc_nblks = nblks;
    723 			wd->sc_nbytes = wd->sc_bcount;
    724 		}
    725 	}
    726 
    727 	/* If this was a write and not using DMA, push the data. */
    728 	if (wd->sc_mode != WDM_DMA &&
    729 	    (bp->b_flags & (B_READ|B_WRITE)) == B_WRITE) {
    730 		if (wait_for_drq(wdc) < 0) {
    731 			wderror(wd, NULL, "wdcstart: timeout waiting for drq");
    732 			wdcunwedge(wdc);
    733 			return;
    734 		}
    735 
    736 		/* Push out data. */
    737 		if ((wd->sc_flags & WDF_32BIT) == 0)
    738 			outsw(wdc->sc_iobase+wd_data, bp->b_data + wd->sc_skip,
    739 			    wd->sc_nbytes >> 1);
    740 		else
    741 			outsl(wdc->sc_iobase+wd_data, bp->b_data + wd->sc_skip,
    742 			    wd->sc_nbytes >> 2);
    743 	}
    744 
    745 	wdc->sc_flags |= WDCF_ACTIVE;
    746 	timeout(wdctimeout, wdc, WAITTIME);
    747 }
    748 
    749 /*
    750  * Interrupt routine for the controller.  Acknowledge the interrupt, check for
    751  * errors on the current operation, mark it done if necessary, and start the
    752  * next request.  Also check for a partially done transfer, and continue with
    753  * the next chunk if so.
    754  */
    755 int
    756 wdcintr(arg)
    757 	void *arg;
    758 {
    759 	struct wdc_softc *wdc = arg;
    760 	struct wd_softc *wd;
    761 	struct buf *bp;
    762 
    763 	if ((wdc->sc_flags & WDCF_ACTIVE) == 0) {
    764 		/* Clear the pending interrupt and abort. */
    765 		(void) inb(wdc->sc_iobase+wd_status);
    766 		return 0;
    767 	}
    768 
    769 	wdc->sc_flags &= ~WDCF_ACTIVE;
    770 	untimeout(wdctimeout, wdc);
    771 
    772 	wd = wdc->sc_drives.tqh_first;
    773 	bp = wd->sc_q.b_actf;
    774 
    775 #ifdef WDDEBUG
    776 	printf("I%d ", ctrlr);
    777 #endif
    778 
    779 	if (wait_for_unbusy(wdc) < 0) {
    780 		wderror(wd, NULL, "wdcintr: timeout waiting for unbusy");
    781 		wdc->sc_status |= WDCS_ERR;	/* XXX */
    782 	}
    783 
    784 	/* Is it not a transfer, but a control operation? */
    785 	if (wd->sc_state < OPEN) {
    786 		if (wdcontrol(wd) == 0) {
    787 			/* The drive is busy.  Wait. */
    788 			return 1;
    789 		}
    790 		wdcstart(wdc);
    791 		return 1;
    792 	}
    793 
    794 	/* Turn off the DMA channel and unbounce the buffer. */
    795 	if (wd->sc_mode == WDM_DMA)
    796 		isa_dmadone(bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE,
    797 		    bp->b_data + wd->sc_skip, wd->sc_nbytes, wdc->sc_drq);
    798 
    799 	/* Have we an error? */
    800 	if (wdc->sc_status & WDCS_ERR) {
    801 #ifdef WDDEBUG
    802 		wderror(wd, NULL, "wdcintr");
    803 #endif
    804 		if ((wdc->sc_flags & WDCF_SINGLE) == 0) {
    805 			wdc->sc_flags |= WDCF_ERROR;
    806 			goto restart;
    807 		}
    808 
    809 #ifdef B_FORMAT
    810 		if (bp->b_flags & B_FORMAT)
    811 			goto bad;
    812 #endif
    813 
    814 		if (++wdc->sc_errors < WDIORETRIES)
    815 			goto restart;
    816 		wderror(wd, bp, "hard error");
    817 
    818 #ifdef B_FORMAT
    819 	bad:
    820 #endif
    821 		bp->b_error = EIO;
    822 		bp->b_flags |= B_ERROR;
    823 		goto done;
    824 	}
    825 
    826 	/* If this was a read and not using DMA, fetch the data. */
    827 	if (wd->sc_mode != WDM_DMA &&
    828 	    (bp->b_flags & (B_READ|B_WRITE)) == B_READ) {
    829 		if ((wdc->sc_status & (WDCS_DRDY | WDCS_DSC | WDCS_DRQ))
    830 		    != (WDCS_DRDY | WDCS_DSC | WDCS_DRQ)) {
    831 			wderror(wd, NULL, "wdcintr: read intr before drq");
    832 			wdcunwedge(wdc);
    833 			return 1;
    834 		}
    835 
    836 		/* Pull in data. */
    837 		if ((wd->sc_flags & WDF_32BIT) == 0)
    838 			insw(wdc->sc_iobase+wd_data, bp->b_data + wd->sc_skip,
    839 			    wd->sc_nbytes >> 1);
    840 		else
    841 			insl(wdc->sc_iobase+wd_data, bp->b_data + wd->sc_skip,
    842 			    wd->sc_nbytes >> 2);
    843 	}
    844 
    845 	/* If we encountered any abnormalities, flag it as a soft error. */
    846 	if (wdc->sc_errors > 0 ||
    847 	    (wdc->sc_status & WDCS_CORR) != 0) {
    848 		wderror(wd, bp, "soft error (corrected)");
    849 		wdc->sc_errors = 0;
    850 	}
    851 
    852 	/* Adjust pointers for the next block, if any. */
    853 	wd->sc_blkno += wd->sc_nblks;
    854 	wd->sc_skip += wd->sc_nbytes;
    855 	wd->sc_bcount -= wd->sc_nbytes;
    856 
    857 	/* See if this transfer is complete. */
    858 	if (wd->sc_bcount > 0)
    859 		goto restart;
    860 
    861 done:
    862 	/* Done with this transfer, with or without error. */
    863 	wdfinish(wd, bp);
    864 
    865 restart:
    866 	/* Start the next operation, if any. */
    867 	wdcstart(wdc);
    868 
    869 	return 1;
    870 }
    871 
    872 /*
    873  * Wait interruptibly for an exclusive lock.
    874  *
    875  * XXX
    876  * Several drivers do this; it should be abstracted and made MP-safe.
    877  */
    878 int
    879 wdlock(wd)
    880 	struct wd_softc *wd;
    881 {
    882 	int error;
    883 
    884 	while ((wd->sc_flags & WDF_LOCKED) != 0) {
    885 		wd->sc_flags |= WDF_WANTED;
    886 		if ((error = tsleep(wd, PRIBIO | PCATCH, "wdlck", 0)) != 0)
    887 			return error;
    888 	}
    889 	wd->sc_flags |= WDF_LOCKED;
    890 	return 0;
    891 }
    892 
    893 /*
    894  * Unlock and wake up any waiters.
    895  */
    896 void
    897 wdunlock(wd)
    898 	struct wd_softc *wd;
    899 {
    900 
    901 	wd->sc_flags &= ~WDF_LOCKED;
    902 	if ((wd->sc_flags & WDF_WANTED) != 0) {
    903 		wd->sc_flags &= ~WDF_WANTED;
    904 		wakeup(wd);
    905 	}
    906 }
    907 
    908 int
    909 wdopen(dev, flag, fmt, p)
    910 	dev_t dev;
    911 	int flag, fmt;
    912 	struct proc *p;
    913 {
    914 	struct wd_softc *wd;
    915 	int unit, part;
    916 	int error;
    917 
    918 	unit = WDUNIT(dev);
    919 	if (unit >= wd_cd.cd_ndevs)
    920 		return ENXIO;
    921 	wd = wd_cd.cd_devs[unit];
    922 	if (wd == 0)
    923 		return ENXIO;
    924 
    925 	if ((error = wdlock(wd)) != 0)
    926 		return error;
    927 
    928 	if (wd->sc_dk.dk_openmask != 0) {
    929 		/*
    930 		 * If any partition is open, but the disk has been invalidated,
    931 		 * disallow further opens.
    932 		 */
    933 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    934 			error = EIO;
    935 			goto bad3;
    936 		}
    937 	} else {
    938 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    939 			wd->sc_flags |= WDF_LOADED;
    940 
    941 			/* Load the physical device parameters. */
    942 			if (wd_get_parms(wd) != 0) {
    943 				error = ENXIO;
    944 				goto bad2;
    945 			}
    946 
    947 			/* Load the partition info if not already loaded. */
    948 			wdgetdisklabel(wd);
    949 		}
    950 	}
    951 
    952 	part = WDPART(dev);
    953 
    954 	/* Check that the partition exists. */
    955 	if (part != RAW_PART &&
    956 	    (part >= wd->sc_dk.dk_label->d_npartitions ||
    957 	     wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    958 		error = ENXIO;
    959 		goto bad;
    960 	}
    961 
    962 	/* Insure only one open at a time. */
    963 	switch (fmt) {
    964 	case S_IFCHR:
    965 		wd->sc_dk.dk_copenmask |= (1 << part);
    966 		break;
    967 	case S_IFBLK:
    968 		wd->sc_dk.dk_bopenmask |= (1 << part);
    969 		break;
    970 	}
    971 	wd->sc_dk.dk_openmask = wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    972 
    973 	wdunlock(wd);
    974 	return 0;
    975 
    976 bad2:
    977 	wd->sc_flags &= ~WDF_LOADED;
    978 
    979 bad:
    980 	if (wd->sc_dk.dk_openmask == 0) {
    981 	}
    982 
    983 bad3:
    984 	wdunlock(wd);
    985 	return error;
    986 }
    987 
    988 int
    989 wdclose(dev, flag, fmt, p)
    990 	dev_t dev;
    991 	int flag, fmt;
    992 	struct proc *p;
    993 {
    994 	struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
    995 	int part = WDPART(dev);
    996 	int error;
    997 
    998 	if ((error = wdlock(wd)) != 0)
    999 		return error;
   1000 
   1001 	switch (fmt) {
   1002 	case S_IFCHR:
   1003 		wd->sc_dk.dk_copenmask &= ~(1 << part);
   1004 		break;
   1005 	case S_IFBLK:
   1006 		wd->sc_dk.dk_bopenmask &= ~(1 << part);
   1007 		break;
   1008 	}
   1009 	wd->sc_dk.dk_openmask = wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
   1010 
   1011 	if (wd->sc_dk.dk_openmask == 0) {
   1012 		/* XXXX Must wait for I/O to complete! */
   1013 	}
   1014 
   1015 	wdunlock(wd);
   1016 	return 0;
   1017 }
   1018 
   1019 /*
   1020  * Fabricate a default disk label, and try to read the correct one.
   1021  */
   1022 void
   1023 wdgetdisklabel(wd)
   1024 	struct wd_softc *wd;
   1025 {
   1026 	struct disklabel *lp = wd->sc_dk.dk_label;
   1027 	char *errstring;
   1028 
   1029 	bzero(lp, sizeof(struct disklabel));
   1030 	bzero(wd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
   1031 
   1032 	lp->d_secsize = DEV_BSIZE;
   1033 	lp->d_ntracks = wd->sc_params.wdp_heads;
   1034 	lp->d_nsectors = wd->sc_params.wdp_sectors;
   1035 	lp->d_ncylinders = wd->sc_params.wdp_cylinders;
   1036 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   1037 
   1038 #if 0
   1039 	strncpy(lp->d_typename, "ST506 disk", 16);
   1040 	lp->d_type = DTYPE_ST506;
   1041 #endif
   1042 	strncpy(lp->d_packname, wd->sc_params.wdp_model, 16);
   1043 	lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
   1044 	lp->d_rpm = 3600;
   1045 	lp->d_interleave = 1;
   1046 	lp->d_flags = 0;
   1047 
   1048 	lp->d_partitions[RAW_PART].p_offset = 0;
   1049 	lp->d_partitions[RAW_PART].p_size =
   1050 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
   1051 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   1052 	lp->d_npartitions = RAW_PART + 1;
   1053 
   1054 	lp->d_magic = DISKMAGIC;
   1055 	lp->d_magic2 = DISKMAGIC;
   1056 	lp->d_checksum = dkcksum(lp);
   1057 
   1058 	wd->sc_badsect[0] = -1;
   1059 
   1060 	if (wd->sc_state > RECAL)
   1061 		wd->sc_state = RECAL;
   1062 	errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
   1063 	    wdstrategy, lp, wd->sc_dk.dk_cpulabel);
   1064 	if (errstring) {
   1065 		/*
   1066 		 * This probably happened because the drive's default
   1067 		 * geometry doesn't match the DOS geometry.  We
   1068 		 * assume the DOS geometry is now in the label and try
   1069 		 * again.  XXX This is a kluge.
   1070 		 */
   1071 		if (wd->sc_state > GEOMETRY)
   1072 			wd->sc_state = GEOMETRY;
   1073 		errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
   1074 		    wdstrategy, lp, wd->sc_dk.dk_cpulabel);
   1075 	}
   1076 	if (errstring) {
   1077 		printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
   1078 		return;
   1079 	}
   1080 
   1081 	if (wd->sc_state > GEOMETRY)
   1082 		wd->sc_state = GEOMETRY;
   1083 	if ((lp->d_flags & D_BADSECT) != 0)
   1084 		bad144intern(wd);
   1085 }
   1086 
   1087 /*
   1088  * Implement operations needed before read/write.
   1089  * Returns 0 if operation still in progress, 1 if completed.
   1090  */
   1091 int
   1092 wdcontrol(wd)
   1093 	struct wd_softc *wd;
   1094 {
   1095 	struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
   1096 
   1097 	switch (wd->sc_state) {
   1098 	case RECAL:			/* Set SDH, step rate, do recal. */
   1099 		if (wdcommandshort(wdc, wd->sc_drive, WDCC_RECAL) != 0) {
   1100 			wderror(wd, NULL, "wdcontrol: recal failed (1)");
   1101 			goto bad;
   1102 		}
   1103 		wd->sc_state = RECAL_WAIT;
   1104 		break;
   1105 
   1106 	case RECAL_WAIT:
   1107 		if (wdc->sc_status & WDCS_ERR) {
   1108 			wderror(wd, NULL, "wdcontrol: recal failed (2)");
   1109 			goto bad;
   1110 		}
   1111 		/* fall through */
   1112 	case GEOMETRY:
   1113 		if ((wd->sc_params.wdp_capabilities & WD_CAP_LBA) != 0)
   1114 			goto multimode;
   1115 		if (wdsetctlr(wd) != 0) {
   1116 			/* Already printed a message. */
   1117 			goto bad;
   1118 		}
   1119 		wd->sc_state = GEOMETRY_WAIT;
   1120 		break;
   1121 
   1122 	case GEOMETRY_WAIT:
   1123 		if (wdc->sc_status & WDCS_ERR) {
   1124 			wderror(wd, NULL, "wdcontrol: geometry failed");
   1125 			goto bad;
   1126 		}
   1127 		/* fall through */
   1128 	case MULTIMODE:
   1129 	multimode:
   1130 		if (wd->sc_mode != WDM_PIOMULTI)
   1131 			goto open;
   1132 		outb(wdc->sc_iobase+wd_seccnt, wd->sc_multiple);
   1133 		if (wdcommandshort(wdc, wd->sc_drive, WDCC_SETMULTI) != 0) {
   1134 			wderror(wd, NULL, "wdcontrol: setmulti failed (1)");
   1135 			goto bad;
   1136 		}
   1137 		wd->sc_state = MULTIMODE_WAIT;
   1138 		break;
   1139 
   1140 	case MULTIMODE_WAIT:
   1141 		if (wdc->sc_status & WDCS_ERR) {
   1142 			wderror(wd, NULL, "wdcontrol: setmulti failed (2)");
   1143 			goto bad;
   1144 		}
   1145 		/* fall through */
   1146 	case OPEN:
   1147 	open:
   1148 		wdc->sc_errors = 0;
   1149 		wd->sc_state = OPEN;
   1150 		/*
   1151 		 * The rest of the initialization can be done by normal means.
   1152 		 */
   1153 		return 1;
   1154 
   1155 	bad:
   1156 		wdcunwedge(wdc);
   1157 		return 0;
   1158 	}
   1159 
   1160 	wdc->sc_flags |= WDCF_ACTIVE;
   1161 	timeout(wdctimeout, wdc, WAITTIME);
   1162 	return 0;
   1163 }
   1164 
   1165 /*
   1166  * Wait for the drive to become ready and send a command.
   1167  * Return -1 if busy for too long or 0 otherwise.
   1168  * Assumes interrupts are blocked.
   1169  */
   1170 int
   1171 wdcommand(wd, command, cylin, head, sector, count)
   1172 	struct wd_softc *wd;
   1173 	int command;
   1174 	int cylin, head, sector, count;
   1175 {
   1176 	struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
   1177 	int iobase = wdc->sc_iobase;
   1178 	int stat;
   1179 
   1180 	/* Select drive, head, and addressing mode. */
   1181 	outb(iobase+wd_sdh, WDSD_IBM | (wd->sc_drive << 4) | head);
   1182 
   1183 	/* Wait for it to become ready to accept a command. */
   1184 	if (command == WDCC_IDP)
   1185 		stat = wait_for_unbusy(wdc);
   1186 	else
   1187 		stat = wdcwait(wdc, WDCS_DRDY);
   1188 	if (stat < 0)
   1189 		return -1;
   1190 
   1191 	/* Load parameters. */
   1192 	if (wd->sc_dk.dk_label->d_type == DTYPE_ST506)
   1193 		outb(iobase+wd_precomp, wd->sc_dk.dk_label->d_precompcyl / 4);
   1194 	else
   1195 		outb(iobase+wd_features, 0);
   1196 	outb(iobase+wd_cyl_lo, cylin);
   1197 	outb(iobase+wd_cyl_hi, cylin >> 8);
   1198 	outb(iobase+wd_sector, sector);
   1199 	outb(iobase+wd_seccnt, count);
   1200 
   1201 	/* Send command. */
   1202 	outb(iobase+wd_command, command);
   1203 
   1204 	return 0;
   1205 }
   1206 
   1207 /*
   1208  * Simplified version of wdcommand().
   1209  */
   1210 int
   1211 wdcommandshort(wdc, drive, command)
   1212 	struct wdc_softc *wdc;
   1213 	int drive;
   1214 	int command;
   1215 {
   1216 	int iobase = wdc->sc_iobase;
   1217 
   1218 	/* Select drive. */
   1219 	outb(iobase+wd_sdh, WDSD_IBM | (drive << 4));
   1220 
   1221 	if (wdcwait(wdc, WDCS_DRDY) < 0)
   1222 		return -1;
   1223 
   1224 	outb(iobase+wd_command, command);
   1225 
   1226 	return 0;
   1227 }
   1228 
   1229 /*
   1230  * Tell the drive what geometry to use.
   1231  */
   1232 int
   1233 wdsetctlr(wd)
   1234 	struct wd_softc *wd;
   1235 {
   1236 
   1237 #ifdef WDDEBUG
   1238 	printf("wd(%d,%d) C%dH%dS%d\n", wd->sc_dev.dv_unit, wd->sc_drive,
   1239 	    wd->sc_dk.dk_label->d_ncylinders, wd->sc_dk.dk_label->d_ntracks,
   1240 	    wd->sc_dk.dk_label->d_nsectors);
   1241 #endif
   1242 
   1243 	if (wdcommand(wd, WDCC_IDP, wd->sc_dk.dk_label->d_ncylinders,
   1244 	    wd->sc_dk.dk_label->d_ntracks - 1, 0,
   1245 	    wd->sc_dk.dk_label->d_nsectors) != 0) {
   1246 		wderror(wd, NULL, "wdsetctlr: geometry upload failed");
   1247 		return -1;
   1248 	}
   1249 
   1250 	return 0;
   1251 }
   1252 
   1253 /*
   1254  * Get the drive parameters, if ESDI or ATA, or create fake ones for ST506.
   1255  */
   1256 int
   1257 wd_get_parms(wd)
   1258 	struct wd_softc *wd;
   1259 {
   1260 	struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
   1261 	int i;
   1262 	char tb[DEV_BSIZE];
   1263 	int s, error;
   1264 
   1265 	/*
   1266 	 * XXX
   1267 	 * The locking done here, and the length of time this may keep the rest
   1268 	 * of the system suspended, is a kluge.  This should be rewritten to
   1269 	 * set up a transfer and queue it through wdstart(), but it's called
   1270 	 * infrequently enough that this isn't a pressing matter.
   1271 	 */
   1272 
   1273 	s = splbio();
   1274 
   1275 	while ((wdc->sc_flags & WDCF_ACTIVE) != 0) {
   1276 		wdc->sc_flags |= WDCF_WANTED;
   1277 		if ((error = tsleep(wdc, PRIBIO | PCATCH, "wdprm", 0)) != 0) {
   1278 			splx(s);
   1279 			return error;
   1280 		}
   1281 	}
   1282 
   1283 	if (wdcommandshort(wdc, wd->sc_drive, WDCC_IDENTIFY) != 0 ||
   1284 	    wait_for_drq(wdc) != 0) {
   1285 		/*
   1286 		 * We `know' there's a drive here; just assume it's old.
   1287 		 * This geometry is only used to read the MBR and print a
   1288 		 * (false) attach message.
   1289 		 */
   1290 		strncpy(wd->sc_dk.dk_label->d_typename, "ST506",
   1291 		    sizeof wd->sc_dk.dk_label->d_typename);
   1292 		wd->sc_dk.dk_label->d_type = DTYPE_ST506;
   1293 
   1294 		strncpy(wd->sc_params.wdp_model, "unknown",
   1295 		    sizeof wd->sc_params.wdp_model);
   1296 		wd->sc_params.wdp_config = WD_CFG_FIXED;
   1297 		wd->sc_params.wdp_cylinders = 1024;
   1298 		wd->sc_params.wdp_heads = 8;
   1299 		wd->sc_params.wdp_sectors = 17;
   1300 		wd->sc_params.wdp_maxmulti = 0;
   1301 		wd->sc_params.wdp_usedmovsd = 0;
   1302 		wd->sc_params.wdp_capabilities = 0;
   1303 	} else {
   1304 		strncpy(wd->sc_dk.dk_label->d_typename, "ESDI/IDE",
   1305 		    sizeof wd->sc_dk.dk_label->d_typename);
   1306 		wd->sc_dk.dk_label->d_type = DTYPE_ESDI;
   1307 
   1308 		/* Read in parameter block. */
   1309 		insw(wdc->sc_iobase+wd_data, tb, sizeof(tb) / sizeof(short));
   1310 		bcopy(tb, &wd->sc_params, sizeof(struct wdparams));
   1311 
   1312 		/* Shuffle string byte order. */
   1313 		for (i = 0; i < sizeof(wd->sc_params.wdp_model); i += 2) {
   1314 			u_short *p;
   1315 			p = (u_short *)(wd->sc_params.wdp_model + i);
   1316 			*p = ntohs(*p);
   1317 		}
   1318 	}
   1319 
   1320 	/* Clear any leftover interrupt. */
   1321 	(void) inb(wdc->sc_iobase+wd_status);
   1322 
   1323 	/* Restart the queue. */
   1324 	wdcstart(wdc);
   1325 
   1326 	splx(s);
   1327 	return 0;
   1328 }
   1329 
   1330 int
   1331 wdioctl(dev, cmd, addr, flag, p)
   1332 	dev_t dev;
   1333 	u_long cmd;
   1334 	caddr_t addr;
   1335 	int flag;
   1336 	struct proc *p;
   1337 {
   1338 	struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
   1339 	int error;
   1340 
   1341 	if ((wd->sc_flags & WDF_LOADED) == 0)
   1342 		return EIO;
   1343 
   1344 	switch (cmd) {
   1345 	case DIOCSBAD:
   1346 		if ((flag & FWRITE) == 0)
   1347 			return EBADF;
   1348 		wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
   1349 		wd->sc_dk.dk_label->d_flags |= D_BADSECT;
   1350 		bad144intern(wd);
   1351 		return 0;
   1352 
   1353 	case DIOCGDINFO:
   1354 		*(struct disklabel *)addr = *(wd->sc_dk.dk_label);
   1355 		return 0;
   1356 
   1357 	case DIOCGPART:
   1358 		((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
   1359 		((struct partinfo *)addr)->part =
   1360 		    &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
   1361 		return 0;
   1362 
   1363 	case DIOCWDINFO:
   1364 	case DIOCSDINFO:
   1365 		if ((flag & FWRITE) == 0)
   1366 			return EBADF;
   1367 
   1368 		if ((error = wdlock(wd)) != 0)
   1369 			return error;
   1370 		wd->sc_flags |= WDF_LABELLING;
   1371 
   1372 		error = setdisklabel(wd->sc_dk.dk_label,
   1373 		    (struct disklabel *)addr, /*wd->sc_dk.dk_openmask : */0,
   1374 		    wd->sc_dk.dk_cpulabel);
   1375 		if (error == 0) {
   1376 			if (wd->sc_state > GEOMETRY)
   1377 				wd->sc_state = GEOMETRY;
   1378 			if (cmd == DIOCWDINFO)
   1379 				error = writedisklabel(WDLABELDEV(dev),
   1380 				    wdstrategy, wd->sc_dk.dk_label,
   1381 				    wd->sc_dk.dk_cpulabel);
   1382 		}
   1383 
   1384 		wd->sc_flags &= ~WDF_LABELLING;
   1385 		wdunlock(wd);
   1386 		return error;
   1387 
   1388 	case DIOCWLABEL:
   1389 		if ((flag & FWRITE) == 0)
   1390 			return EBADF;
   1391 		if (*(int *)addr)
   1392 			wd->sc_flags |= WDF_WLABEL;
   1393 		else
   1394 			wd->sc_flags &= ~WDF_WLABEL;
   1395 		return 0;
   1396 
   1397 #ifdef notyet
   1398 	case DIOCWFORMAT:
   1399 		if ((flag & FWRITE) == 0)
   1400 			return EBADF;
   1401 	{
   1402 		register struct format_op *fop;
   1403 		struct iovec aiov;
   1404 		struct uio auio;
   1405 
   1406 		fop = (struct format_op *)addr;
   1407 		aiov.iov_base = fop->df_buf;
   1408 		aiov.iov_len = fop->df_count;
   1409 		auio.uio_iov = &aiov;
   1410 		auio.uio_iovcnt = 1;
   1411 		auio.uio_resid = fop->df_count;
   1412 		auio.uio_segflg = 0;
   1413 		auio.uio_offset =
   1414 		    fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
   1415 		auio.uio_procp = p;
   1416 		error = physio(wdformat, NULL, dev, B_WRITE, minphys,
   1417 		    &auio);
   1418 		fop->df_count -= auio.uio_resid;
   1419 		fop->df_reg[0] = wdc->sc_status;
   1420 		fop->df_reg[1] = wdc->sc_error;
   1421 		return error;
   1422 	}
   1423 #endif
   1424 
   1425 	default:
   1426 		return ENOTTY;
   1427 	}
   1428 
   1429 #ifdef DIAGNOSTIC
   1430 	panic("wdioctl: impossible");
   1431 #endif
   1432 }
   1433 
   1434 #ifdef B_FORMAT
   1435 int
   1436 wdformat(struct buf *bp)
   1437 {
   1438 
   1439 	bp->b_flags |= B_FORMAT;
   1440 	return wdstrategy(bp);
   1441 }
   1442 #endif
   1443 
   1444 int
   1445 wdsize(dev)
   1446 	dev_t dev;
   1447 {
   1448 	struct wd_softc *wd;
   1449 	int part;
   1450 	int size;
   1451 
   1452 	if (wdopen(dev, 0, S_IFBLK, NULL) != 0)
   1453 		return -1;
   1454 	wd = wd_cd.cd_devs[WDUNIT(dev)];
   1455 	part = WDPART(dev);
   1456 	if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
   1457 		size = -1;
   1458 	else
   1459 		size = wd->sc_dk.dk_label->d_partitions[part].p_size;
   1460 	if (wdclose(dev, 0, S_IFBLK, NULL) != 0)
   1461 		return -1;
   1462 	return size;
   1463 }
   1464 
   1465 
   1466 #ifndef __BDEVSW_DUMP_OLD_TYPE
   1467 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
   1468 static int wddoingadump;
   1469 static int wddumprecalibrated;
   1470 
   1471 /*
   1472  * Dump core after a system crash.
   1473  */
   1474 int
   1475 wddump(dev, blkno, va, size)
   1476         dev_t dev;
   1477         daddr_t blkno;
   1478         caddr_t va;
   1479         size_t size;
   1480 {
   1481 	struct wd_softc *wd;	/* disk unit to do the I/O */
   1482 	struct wdc_softc *wdc;	/* disk controller to do the I/O */
   1483 	struct disklabel *lp;	/* disk's disklabel */
   1484 	int	unit, part;
   1485 	int	nblks;		/* total number of sectors left to write */
   1486 
   1487 	/* Check if recursive dump; if so, punt. */
   1488 	if (wddoingadump)
   1489 		return EFAULT;
   1490 	wddoingadump = 1;
   1491 
   1492 	unit = WDUNIT(dev);
   1493 	if (unit >= wd_cd.cd_ndevs)
   1494 		return ENXIO;
   1495 	wd = wd_cd.cd_devs[unit];
   1496 	if (wd == 0)
   1497 		return ENXIO;
   1498 
   1499 	part = WDPART(dev);
   1500 
   1501 	/* Make sure it was initialized. */
   1502 	if (wd->sc_state < OPEN)
   1503 		return ENXIO;
   1504 
   1505 	wdc = (void *)wd->sc_dev.dv_parent;
   1506 
   1507         /* Convert to disk sectors.  Request must be a multiple of size. */
   1508 	lp = wd->sc_dk.dk_label;
   1509 	if ((size % lp->d_secsize) != 0)
   1510 		return EFAULT;
   1511 	nblks = size / lp->d_secsize;
   1512 	blkno = blkno / (lp->d_secsize / DEV_BSIZE);
   1513 
   1514 	/* Check transfer bounds against partition size. */
   1515 	if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
   1516 		return EINVAL;
   1517 
   1518 	/* Offset block number to start of partition. */
   1519 	blkno += lp->d_partitions[part].p_offset;
   1520 
   1521 	/* Recalibrate, if first dump transfer. */
   1522 	if (wddumprecalibrated == 0) {
   1523 		wddumprecalibrated = 1;
   1524 		if (wdcommandshort(wdc, wd->sc_drive, WDCC_RECAL) != 0 ||
   1525 		    wait_for_ready(wdc) != 0 || wdsetctlr(wd) != 0 ||
   1526 		    wait_for_ready(wdc) != 0) {
   1527 			wderror(wd, NULL, "wddump: recal failed");
   1528 			return EIO;
   1529 		}
   1530 	}
   1531 
   1532 	while (nblks > 0) {
   1533 		daddr_t xlt_blkno = blkno;
   1534 		long cylin, head, sector;
   1535 
   1536 		if ((lp->d_flags & D_BADSECT) != 0) {
   1537 			long blkdiff;
   1538 			int i;
   1539 
   1540 			for (i = 0; (blkdiff = wd->sc_badsect[i]) != -1; i++) {
   1541 				blkdiff -= xlt_blkno;
   1542 				if (blkdiff < 0)
   1543 					continue;
   1544 				if (blkdiff == 0) {
   1545 					/* Replace current block of transfer. */
   1546 					xlt_blkno = lp->d_secperunit -
   1547 					    lp->d_nsectors - i - 1;
   1548 				}
   1549 				break;
   1550 			}
   1551 			/* Tranfer is okay now. */
   1552 		}
   1553 
   1554 		if ((wd->sc_params.wdp_capabilities & WD_CAP_LBA) != 0) {
   1555 			sector = (xlt_blkno >> 0) & 0xff;
   1556 			cylin = (xlt_blkno >> 8) & 0xffff;
   1557 			head = (xlt_blkno >> 24) & 0xf;
   1558 			head |= WDSD_LBA;
   1559 		} else {
   1560 			sector = xlt_blkno % lp->d_nsectors;
   1561 			sector++;	/* Sectors begin with 1, not 0. */
   1562 			xlt_blkno /= lp->d_nsectors;
   1563 			head = xlt_blkno % lp->d_ntracks;
   1564 			xlt_blkno /= lp->d_ntracks;
   1565 			cylin = xlt_blkno;
   1566 			head |= WDSD_CHS;
   1567 		}
   1568 
   1569 #ifndef WD_DUMP_NOT_TRUSTED
   1570 		if (wdcommand(wd, WDCC_WRITE, cylin, head, sector, 1) != 0 ||
   1571 		    wait_for_drq(wdc) != 0) {
   1572 			wderror(wd, NULL, "wddump: write failed");
   1573 			return EIO;
   1574 		}
   1575 
   1576 		outsw(wdc->sc_iobase+wd_data, va, lp->d_secsize >> 1);
   1577 
   1578 		/* Check data request (should be done). */
   1579 		if (wait_for_ready(wdc) != 0) {
   1580 			wderror(wd, NULL, "wddump: timeout waiting for ready");
   1581 			return EIO;
   1582 		}
   1583 #else	/* WD_DUMP_NOT_TRUSTED */
   1584 		/* Let's just talk about this first... */
   1585 		printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
   1586 		    unit, va, cylin, head, sector);
   1587 		delay(500 * 1000);	/* half a second */
   1588 #endif
   1589 
   1590 		/* update block count */
   1591 		nblks -= 1;
   1592 		blkno += 1;
   1593 		va += lp->d_secsize;
   1594 	}
   1595 
   1596 	wddoingadump = 0;
   1597 	return 0;
   1598 }
   1599 #else /* __BDEVSW_DUMP_NEW_TYPE */
   1600 int
   1601 wddump(dev, blkno, va, size)
   1602         dev_t dev;
   1603         daddr_t blkno;
   1604         caddr_t va;
   1605         size_t size;
   1606 {
   1607 
   1608 	/* Not implemented. */
   1609 	return ENXIO;
   1610 }
   1611 #endif /* __BDEVSW_DUMP_NEW_TYPE */
   1612 
   1613 /*
   1614  * Internalize the bad sector table.
   1615  */
   1616 void
   1617 bad144intern(wd)
   1618 	struct wd_softc *wd;
   1619 {
   1620 	struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
   1621 	struct disklabel *lp = wd->sc_dk.dk_label;
   1622 	int i = 0;
   1623 
   1624 	for (; i < 126; i++) {
   1625 		if (bt->bt_bad[i].bt_cyl == 0xffff)
   1626 			break;
   1627 		wd->sc_badsect[i] =
   1628 		    bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
   1629 		    (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
   1630 		    (bt->bt_bad[i].bt_trksec & 0xff);
   1631 	}
   1632 	for (; i < 127; i++)
   1633 		wd->sc_badsect[i] = -1;
   1634 }
   1635 
   1636 int
   1637 wdcreset(wdc)
   1638 	struct wdc_softc *wdc;
   1639 {
   1640 	int iobase = wdc->sc_iobase;
   1641 
   1642 	/* Reset the device. */
   1643 	outb(iobase+wd_ctlr, WDCTL_RST | WDCTL_IDS);
   1644 	delay(1000);
   1645 	outb(iobase+wd_ctlr, WDCTL_IDS);
   1646 	delay(1000);
   1647 	(void) inb(iobase+wd_error);
   1648 	outb(iobase+wd_ctlr, WDCTL_4BIT);
   1649 
   1650 	if (wait_for_unbusy(wdc) < 0) {
   1651 		printf("%s: reset failed\n", wdc->sc_dev.dv_xname);
   1652 		return 1;
   1653 	}
   1654 
   1655 	return 0;
   1656 }
   1657 
   1658 void
   1659 wdcrestart(arg)
   1660 	void *arg;
   1661 {
   1662 	struct wdc_softc *wdc = arg;
   1663 	int s;
   1664 
   1665 	s = splbio();
   1666 	wdcstart(wdc);
   1667 	splx(s);
   1668 }
   1669 
   1670 /*
   1671  * Unwedge the controller after an unexpected error.  We do this by resetting
   1672  * it, marking all drives for recalibration, and stalling the queue for a short
   1673  * period to give the reset time to finish.
   1674  * NOTE: We use a timeout here, so this routine must not be called during
   1675  * autoconfig or dump.
   1676  */
   1677 void
   1678 wdcunwedge(wdc)
   1679 	struct wdc_softc *wdc;
   1680 {
   1681 	int unit;
   1682 
   1683 	untimeout(wdctimeout, wdc);
   1684 	(void) wdcreset(wdc);
   1685 
   1686 	/* Schedule recalibrate for all drives on this controller. */
   1687 	for (unit = 0; unit < wd_cd.cd_ndevs; unit++) {
   1688 		struct wd_softc *wd = wd_cd.cd_devs[unit];
   1689 		if (!wd || (void *)wd->sc_dev.dv_parent != wdc)
   1690 			continue;
   1691 		if (wd->sc_state > RECAL)
   1692 			wd->sc_state = RECAL;
   1693 	}
   1694 
   1695 	wdc->sc_flags |= WDCF_ERROR;
   1696 	++wdc->sc_errors;
   1697 
   1698 	/* Wake up in a little bit and restart the operation. */
   1699 	timeout(wdcrestart, wdc, RECOVERYTIME);
   1700 }
   1701 
   1702 int
   1703 wdcwait(wdc, mask)
   1704 	struct wdc_softc *wdc;
   1705 	int mask;
   1706 {
   1707 	int iobase = wdc->sc_iobase;
   1708 	int timeout = 0;
   1709 	u_char status;
   1710 #ifdef WDCNDELAY_DEBUG
   1711 	extern int cold;
   1712 #endif
   1713 
   1714 	for (;;) {
   1715 		wdc->sc_status = status = inb(iobase+wd_status);
   1716 		if ((status & WDCS_BSY) == 0 && (status & mask) == mask)
   1717 			break;
   1718 		if (++timeout > WDCNDELAY)
   1719 			return -1;
   1720 		delay(WDCDELAY);
   1721 	}
   1722 	if (status & WDCS_ERR) {
   1723 		wdc->sc_error = inb(iobase+wd_error);
   1724 		return WDCS_ERR;
   1725 	}
   1726 #ifdef WDCNDELAY_DEBUG
   1727 	/* After autoconfig, there should be no long delays. */
   1728 	if (!cold && timeout > WDCNDELAY_DEBUG)
   1729 		printf("%s: warning: busy-wait took %dus\n",
   1730 		    wdc->sc_dev.dv_xname, WDCDELAY * timeout);
   1731 #endif
   1732 	return 0;
   1733 }
   1734 
   1735 void
   1736 wdctimeout(arg)
   1737 	void *arg;
   1738 {
   1739 	struct wdc_softc *wdc = (struct wdc_softc *)arg;
   1740 	int s;
   1741 
   1742 	s = splbio();
   1743 	if ((wdc->sc_flags & WDCF_ACTIVE) != 0) {
   1744 		struct wd_softc *wd = wdc->sc_drives.tqh_first;
   1745 		struct buf *bp = wd->sc_q.b_actf;
   1746 
   1747 		wdc->sc_flags &= ~WDCF_ACTIVE;
   1748 		wderror(wdc, NULL, "lost interrupt");
   1749 		printf("%s: lost interrupt: %sing %d@%s:%d\n",
   1750 		    wdc->sc_dev.dv_xname,
   1751 		    (bp->b_flags & B_READ) ? "read" : "writ",
   1752 		    wd->sc_nblks, wd->sc_dev.dv_xname, wd->sc_blkno);
   1753 		wdcunwedge(wdc);
   1754 	} else
   1755 		wderror(wdc, NULL, "missing untimeout");
   1756 	splx(s);
   1757 }
   1758 
   1759 void
   1760 wderror(dev, bp, msg)
   1761 	void *dev;
   1762 	struct buf *bp;
   1763 	char *msg;
   1764 {
   1765 	struct wd_softc *wd = dev;
   1766 	struct wdc_softc *wdc = dev;
   1767 
   1768 	if (bp) {
   1769 		diskerr(bp, "wd", msg, LOG_PRINTF, wd->sc_skip / DEV_BSIZE,
   1770 		    wd->sc_dk.dk_label);
   1771 		printf("\n");
   1772 	} else
   1773 		printf("%s: %s: status %b error %b\n", wdc->sc_dev.dv_xname,
   1774 		    msg, wdc->sc_status, WDCS_BITS, wdc->sc_error, WDERR_BITS);
   1775 }
   1776