Home | History | Annotate | Line # | Download | only in ic
wdc.c revision 1.26
      1 /*	$NetBSD: wdc.c,v 1.26 1998/08/14 20:39:04 drochner Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1994, 1995, 1998 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  * Atapi support added by Manuel Bouyer.
     10  *
     11  * Bus_space-ified by Christopher G. Demetriou.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  * 3. All advertising materials mentioning features or use of this software
     22  *    must display the following acknowledgement:
     23  *	This product includes software developed by Charles M. Hannum.
     24  * 4. The name of the author may not be used to endorse or promote products
     25  *    derived from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     28  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     29  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     30  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     32  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     33  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     34  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     36  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * CODE UNTESTED IN THE CURRENT REVISION:
     41  *	* DMA
     42  *	* 32-bit data port access.
     43  */
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/conf.h>
     49 #include <sys/file.h>
     50 #include <sys/stat.h>
     51 #include <sys/ioctl.h>
     52 #include <sys/buf.h>
     53 #include <sys/uio.h>
     54 #include <sys/malloc.h>
     55 #include <sys/device.h>
     56 #include <sys/disklabel.h>
     57 #include <sys/disk.h>
     58 #include <sys/syslog.h>
     59 #include <sys/proc.h>
     60 
     61 #include <vm/vm.h>
     62 
     63 #include <machine/intr.h>
     64 #include <machine/bus.h>
     65 
     66 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
     67 #define	bus_space_write_multi_stream_2	bus_space_write_multi_2
     68 #define	bus_space_write_multi_stream_4	bus_space_write_multi_4
     69 #define	bus_space_read_multi_stream_2	bus_space_read_multi_2
     70 #define	bus_space_read_multi_stream_4	bus_space_read_multi_4
     71 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
     72 
     73 #include <dev/ic/wdcreg.h>
     74 #include <dev/ic/wdcvar.h>
     75 #include <dev/ata/wdlink.h>
     76 #include "atapibus.h"
     77 #include "wdc.h"
     78 
     79 #if NATAPIBUS > 0
     80 #include <dev/scsipi/scsipi_all.h>
     81 #include <dev/scsipi/atapi_all.h>
     82 #include <dev/scsipi/atapiconf.h>
     83 #endif
     84 
     85 #define	WAITTIME	(10 * hz)	/* time to wait for a completion */
     86 	/* this is a lot for hard drives, but not for cdroms */
     87 #define RECOVERYTIME hz/2
     88 #define WDCDELAY	100
     89 #define WDCNDELAY	100000	/* delay = 100us; so 10s for a controller state change */
     90 #if 0
     91 /* If you enable this, it will report any delays more than 100us * N long. */
     92 #define WDCNDELAY_DEBUG	50
     93 #endif
     94 
     95 #define	WDIORETRIES	5		/* number of retries before giving up */
     96 
     97 #define	WDPART(dev)			DISKPART(dev)
     98 
     99 LIST_HEAD(xfer_free_list, wdc_xfer) xfer_free_list;
    100 
    101 int	wdc_init_controller __P((struct wdc_softc *,
    102 	    const struct wdc_attachment_data *));
    103 void	wdcstart	__P((struct wdc_softc *));
    104 int	wdcreset	__P((struct wdc_softc *, int));
    105 #define VERBOSE 1
    106 #define SILENT 0
    107 void	wdcrestart	__P((void *arg));
    108 void	wdcunwedge	__P((struct wdc_softc *));
    109 void	wdctimeout	__P((void *arg));
    110 int	wdccontrol	__P((struct wdc_softc*, struct wd_link *));
    111 void	wdc_free_xfer	__P((struct wdc_xfer *));
    112 void	wdcerror	__P((struct wdc_softc*, char *));
    113 void	wdcbit_bucket	__P(( struct wdc_softc *, int));
    114 #if NWD > 0
    115 int	wdprint			__P((void *, const char *));
    116 int	wdsetctlr		__P((struct wd_link *));
    117 int	wdc_ata_intr	__P((struct wdc_softc *,struct wdc_xfer *));
    118 void	wdc_ata_start	__P((struct wdc_softc *,struct wdc_xfer *));
    119 void	wdc_ata_done	__P((struct wdc_softc *, struct wdc_xfer *));
    120 #endif /* NWD > 0 */
    121 #if NATAPIBUS > 0
    122 void	wdc_atapi_minphys __P((struct buf *bp));
    123 void	wdc_atapi_start	__P((struct wdc_softc *,struct wdc_xfer *));
    124 int	wdc_atapi_intr	__P((struct wdc_softc *, struct wdc_xfer *));
    125 void	wdc_atapi_done	__P((struct wdc_softc *, struct wdc_xfer *));
    126 int	wdc_atapi_send_command_packet __P((struct scsipi_xfer *sc_xfer));
    127 #define MAX_SIZE MAXPHYS /* XXX */
    128 #endif
    129 
    130 #ifdef ATAPI_DEBUG2
    131 static int wdc_nxfer;
    132 #endif
    133 
    134 #ifdef WDDEBUG
    135 #define WDDEBUG_PRINT(args)	printf args
    136 #else
    137 #define WDDEBUG_PRINT(args)
    138 #endif
    139 
    140 #if NATAPIBUS > 0
    141 static struct scsipi_adapter wdc_switch  = {
    142 	wdc_atapi_send_command_packet,
    143 	wdc_atapi_minphys,
    144 	0,
    145 	0
    146 };
    147 #endif
    148 
    149 /*
    150  * wdc_init_controller: Does a quick probe/init of the controller.
    151  *
    152  * Return values:
    153  *	0	No controller present (as far as it can tell).
    154  *	>0	Controller present and seemingly functional.
    155  *	<0	Controller present, but not working correctly.
    156  */
    157 int
    158 wdc_init_controller(wdc, adp)
    159 	struct wdc_softc *wdc;
    160 	const struct wdc_attachment_data *adp;
    161 {
    162 	bus_space_tag_t iot;
    163 	bus_space_handle_t ioh;
    164 
    165 	iot = wdc->sc_iot;
    166 	ioh = wdc->sc_ioh;
    167 
    168 	bus_space_write_1(iot, ioh, wd_sdh, WDSD_IBM); /* master */
    169 
    170 	if (wdcreset(wdc, SILENT) != 0) {
    171 		/*
    172 		 * If the reset failed, there is no master. test for
    173 		 * ATAPI signature on the slave device. If no ATAPI
    174 		 * slave, wait 5s and retry a reset.
    175 		 */
    176 		bus_space_write_1(iot, ioh, wd_sdh, WDSD_IBM | 0x10); /*slave*/
    177 		if (bus_space_read_1(iot, ioh, wd_cyl_lo) == 0x14 &&
    178 		    bus_space_read_1(iot, ioh, wd_cyl_hi) == 0xeb) {
    179 			wdc->sc_flags |= WDCF_ONESLAVE;
    180 			goto drivefound;
    181 		} else {
    182 			delay(500000);
    183 			if (wdcreset(wdc, SILENT) != 0)
    184 				return (0);
    185 		}
    186 	}
    187 	delay(1000);
    188 
    189 	/*
    190 	 * Reset succeeded.  Test for ATAPI signature on both master
    191 	 * and slave.
    192 	 */
    193 	if (bus_space_read_1(iot, ioh, wd_cyl_lo) == 0x14 &&
    194 		bus_space_read_1(iot, ioh, wd_cyl_hi) == 0xeb)
    195 		goto drivefound;
    196 	bus_space_write_1(iot, ioh, wd_sdh, WDSD_IBM | 0x10);
    197 	if (bus_space_read_1(iot, ioh, wd_cyl_lo) == 0x14 &&
    198 		bus_space_read_1(iot, ioh, wd_cyl_hi) == 0xeb) {
    199 		wdc->sc_flags |= WDCF_ONESLAVE;
    200 		goto drivefound;
    201 	}
    202 
    203 	/*
    204 	 * Test non-ATAPI registers.  Error register not writable,
    205 	 * but all of cyllo is.
    206 	 */
    207 	bus_space_write_1(iot, ioh, wd_sdh, WDSD_IBM);
    208 	bus_space_write_1(iot, ioh, wd_error, 0x58);
    209 	bus_space_write_1(iot, ioh, wd_cyl_lo, 0xa5);
    210 	if (bus_space_read_1(iot, ioh, wd_error) != 0x58 &&
    211 	    bus_space_read_1(iot, ioh, wd_cyl_lo) == 0xa5)
    212 		goto drivefound;
    213 
    214 	/*
    215 	 * If no drives found, but the resets succeeded, we claim to
    216 	 * have the controller, at least.
    217 	 */
    218 	return (1);
    219 
    220 drivefound:
    221 	/* Select drive 0 or ATAPI slave device */
    222 	if (wdc->sc_flags & WDCF_ONESLAVE)
    223 		bus_space_write_1(iot, ioh, wd_sdh, WDSD_IBM | 0x10);
    224 	else
    225 		bus_space_write_1(iot, ioh, wd_sdh, WDSD_IBM);
    226 
    227 	/* Wait for controller to become ready. */
    228 	if (wait_for_unbusy(wdc) < 0)
    229 		return (-1);
    230 
    231 	/* Start drive diagnostics. */
    232 	bus_space_write_1(iot, ioh, wd_command, WDCC_DIAGNOSE);
    233 
    234 	/* Wait for command to complete. */
    235 	if (wait_for_unbusy(wdc) < 0)
    236 		return (-1);
    237 
    238 	return 1;
    239 }
    240 
    241 int
    242 wdcprobe(adp)
    243 	const struct wdc_attachment_data *adp;
    244 {
    245 	struct wdc_softc _wdc, *wdc = &_wdc;		/* XXX EWWWWW! */
    246 	int rv;
    247 
    248 	bzero(wdc, sizeof *wdc);
    249 	strcpy(wdc->sc_dev.dv_xname, "wdcprobe");
    250 	wdc->sc_adp = adp;
    251 
    252 	rv = wdc_init_controller(wdc, adp);
    253 
    254 	if (rv < 0)
    255 		rv = 1;
    256 	return (rv);
    257 }
    258 
    259 void
    260 wdcattach(wdc, adp)
    261 	struct wdc_softc *wdc;
    262 	const struct wdc_attachment_data *adp;
    263 {
    264 #if NWD > 0
    265 	int drive;
    266 #endif
    267 
    268 	wdc->sc_adp = adp;
    269 	if (wdc_init_controller(wdc, adp) <= 0) {
    270 		printf("%s: controller wouldn't initialize properly\n",
    271 		    wdc->sc_dev.dv_xname);
    272 		return;
    273 	}
    274 
    275 	TAILQ_INIT(&wdc->sc_xfer);
    276 
    277 	if (wdc->sc_cap & WDC_CAPABILITY_DMA)
    278 		(*wdc->sc_dma_setup)(wdc->sc_dma_arg);
    279 
    280 #ifdef ATAPI_DEBUG2
    281 	wdc_nxfer = 0;
    282 #endif
    283 
    284 #if NATAPIBUS > 0
    285 	/*
    286 	 * Attach an ATAPI bus, if configured.
    287 	 */
    288 	wdc->ab_link = malloc(sizeof(struct scsipi_link), M_DEVBUF, M_NOWAIT);
    289 	if (wdc->ab_link == NULL) {
    290 		printf("%s: can't allocate ATAPI link\n",
    291 		    wdc->sc_dev.dv_xname);
    292 		return;
    293 	}
    294 	bzero(wdc->ab_link,sizeof(struct scsipi_link));
    295 	wdc->ab_link->type = BUS_ATAPI;
    296 	wdc->ab_link->openings = 1;
    297 	wdc->ab_link->scsipi_atapi.type = ATAPI;
    298 	wdc->ab_link->scsipi_atapi.channel = 0;
    299 	wdc->ab_link->adapter_softc = (caddr_t)wdc;
    300 	wdc->ab_link->adapter = &wdc_switch;
    301 	(void)config_found(&wdc->sc_dev, (void *)wdc->ab_link, NULL);
    302 #endif /* NATAPIBUS > 0 */
    303 #if NWD > 0
    304 	/*
    305 	 * Attach standard IDE/ESDI/etc. disks to the controller.
    306 	 */
    307 	for (drive = 0; drive < 2; drive++) {
    308 		/* if a disk is already present, skip */
    309 		if ((wdc->sc_drives_mask & (1 << drive)) != 0) {
    310 			continue;
    311 		}
    312 		/* controller active while autoconf */
    313 		wdc->sc_flags |= WDCF_ACTIVE;
    314 
    315 		if (wdccommandshort(wdc, drive, WDCC_RECAL) != 0 ||
    316             	    wait_for_ready(wdc) != 0) {
    317 			wdc->d_link[drive] = NULL;
    318 			wdc->sc_flags &= ~WDCF_ACTIVE;
    319 		} else {
    320 			wdc->sc_flags &= ~WDCF_ACTIVE;
    321 			wdc->d_link[drive] = malloc(sizeof(struct wd_link),
    322 			    M_DEVBUF, M_NOWAIT);
    323 			if (wdc->d_link[drive] == NULL) {
    324 				printf("%s: can't allocate link for drive %d\n",
    325 				    wdc->sc_dev.dv_xname, drive);
    326 				continue;
    327 			}
    328 			bzero(wdc->d_link[drive],sizeof(struct wd_link));
    329 			wdc->d_link[drive]->type = ATA;
    330 			wdc->d_link[drive]->wdc_softc =(caddr_t) wdc;
    331 			wdc->d_link[drive]->drive = drive;
    332 			if (wdc->sc_cap & WDC_CAPABILITY_DMA)
    333 				wdc->d_link[drive]->sc_mode = WDM_DMA;
    334 			else
    335 				wdc->d_link[drive]->sc_mode = 0;
    336 
    337 			wdc->sc_drives_mask |= (1 << drive);
    338 			(void)config_found(&wdc->sc_dev,
    339 			    (void *)wdc->d_link[drive], wdprint);
    340 		}
    341 	}
    342 #endif /* NWD > 0 */
    343 	/* explicitly select an existing drive, to avoid spurious interrupts */
    344 	if (wdc->sc_flags & WDCF_ONESLAVE)
    345 		bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_sdh, WDSD_IBM | 0x10); /* slave */
    346 	else
    347 		bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_sdh, WDSD_IBM); /* master */
    348 	/*
    349 	 * Reset controller. The probe, with some combinations of ATA/ATAPI
    350 	 * devices keep it in a mostly working, but strange state (with busy
    351 	 * led on)
    352 	 */
    353 	wdcreset(wdc, VERBOSE);
    354 }
    355 
    356 /*
    357  * Start I/O on a controller.  This does the calculation, and starts a read or
    358  * write operation.  Called to from wdstart() to start a transfer, from
    359  * wdcintr() to continue a multi-sector transfer or start the next transfer, or
    360  * wdcrestart() after recovering from an error.
    361  */
    362 void
    363 wdcstart(wdc)
    364 	struct wdc_softc *wdc;
    365 {
    366 	struct wdc_xfer *xfer;
    367 
    368 	if ((wdc->sc_flags & WDCF_ACTIVE) != 0 ) {
    369 		WDDEBUG_PRINT(("wdcstart: already active\n"));
    370 		return; /* controller aleady active */
    371 	}
    372 #ifdef DIAGNOSTIC
    373 	if ((wdc->sc_flags & WDCF_IRQ_WAIT) != 0)
    374 		panic("wdcstart: controller waiting for irq\n");
    375 #endif
    376 	/* is there a xfer ? */
    377 	xfer = wdc->sc_xfer.tqh_first;
    378 	if (xfer == NULL) {
    379 #ifdef ATAPI_DEBUG2
    380 		printf("wdcstart: null xfer\n");
    381 #endif
    382 		/*
    383 		 * XXX
    384 		 * This is a kluge.  See comments in wd_get_parms().
    385 		 */
    386 		if ((wdc->sc_flags & WDCF_WANTED) != 0) {
    387 #ifdef ATAPI_DEBUG2
    388 			printf("WDCF_WANTED\n");
    389 #endif
    390 			wdc->sc_flags &= ~WDCF_WANTED;
    391 			wakeup(wdc);
    392 		}
    393 		return;
    394 	}
    395 
    396 	if (wdc->sc_cap & WDC_CAPABILITY_HWLOCK)
    397 		if (!(*wdc->sc_claim_hw)(wdc, 0))
    398 			return;
    399 
    400 	wdc->sc_flags |= WDCF_ACTIVE;
    401 #ifdef ATAPI_DEBUG2
    402 		printf("wdcstart: drive %d\n", (int)xfer->d_link->drive);
    403 #endif
    404     bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_sdh, WDSD_IBM | xfer->d_link->drive << 4);
    405 #if NATAPIBUS > 0 && NWD > 0
    406 	if (xfer->c_flags & C_ATAPI) {
    407 #ifdef ATAPI_DEBUG_WDC
    408 		printf("wdcstart: atapi\n");
    409 #endif
    410 		wdc_atapi_start(wdc,xfer);
    411 	} else
    412 		wdc_ata_start(wdc,xfer);
    413 #else /* NATAPIBUS > 0 && NWD > 0 */
    414 #if NATAPIBUS > 0
    415 #ifdef ATAPI_DEBUG_WDC
    416 	printf("wdcstart: atapi\n");
    417 #endif
    418 	wdc_atapi_start(wdc,xfer);
    419 #endif /* NATAPIBUS > */
    420 #if NWD > 0
    421 	wdc_ata_start(wdc,xfer);
    422 #endif /* NWD > 0 */
    423 #endif /* NATAPIBUS > 0 && NWD > 0 */
    424 }
    425 
    426 #if NWD > 0
    427 int
    428 wdprint(aux, wdc)
    429 	void *aux;
    430 	const char *wdc;
    431 {
    432 	struct wd_link *d_link = aux;
    433 
    434 	if (!wdc)
    435 		printf(" drive %d", d_link->drive);
    436 	return QUIET;
    437 }
    438 
    439 void
    440 wdc_ata_start(wdc, xfer)
    441 	struct wdc_softc *wdc;
    442 	struct wdc_xfer *xfer;
    443 {
    444 	struct wd_link *d_link;
    445 	struct buf *bp = xfer->c_bp;
    446 	int nblks;
    447 
    448 	d_link=xfer->d_link;
    449 
    450 	if (wdc->sc_errors >= WDIORETRIES) {
    451 		wderror(d_link, bp, "wdc_ata_start hard error");
    452 		xfer->c_flags |= C_ERROR;
    453 		wdc_ata_done(wdc, xfer);
    454 		return;
    455 	}
    456 
    457 	/* Do control operations specially. */
    458 	if (d_link->sc_state < READY) {
    459 		/*
    460 		 * Actually, we want to be careful not to mess with the control
    461 		 * state if the device is currently busy, but we can assume
    462 		 * that we never get to this point if that's the case.
    463 		 */
    464 		if (wdccontrol(wdc, d_link) == 0) {
    465 			/* The drive is busy.  Wait. */
    466 			return;
    467 		}
    468 	}
    469 
    470 	/*
    471 	 * WDCF_ERROR is set by wdcunwedge() and wdcintr() when an error is
    472 	 * encountered.  If we are in multi-sector mode, then we switch to
    473 	 * single-sector mode and retry the operation from the start.
    474 	 */
    475 	if (wdc->sc_flags & WDCF_ERROR) {
    476 		wdc->sc_flags &= ~WDCF_ERROR;
    477 		if ((wdc->sc_flags & WDCF_SINGLE) == 0) {
    478 			wdc->sc_flags |= WDCF_SINGLE;
    479 			xfer->c_skip = 0;
    480 		}
    481 	}
    482 
    483 
    484 	/* When starting a transfer... */
    485 	if (xfer->c_skip == 0) {
    486 		WDDEBUG_PRINT(("\n%s: wdc_ata_start %s %d@%d; map ",
    487 		    wdc->sc_dev.dv_xname,
    488 		    (xfer->c_flags & B_READ) ? "read" : "write",
    489 		    xfer->c_bcount, xfer->c_blkno));
    490 	} else {
    491 		WDDEBUG_PRINT((" %d)%x", xfer->c_skip,
    492 		    bus_space_read_1(wdc->sc_auxiot, wdc->sc_auxioh,
    493 			wd_aux_altsts)));
    494 	}
    495 
    496 	/*
    497 	 * When starting a multi-sector transfer, or doing single-sector
    498 	 * transfers...
    499 	 */
    500 	if (xfer->c_skip == 0 || (wdc->sc_flags & WDCF_SINGLE) != 0 ||
    501 	    d_link->sc_mode == WDM_DMA) {
    502 		daddr_t blkno = xfer->c_blkno;
    503 		long cylin, head, sector;
    504 		int command;
    505 
    506 		if ((wdc->sc_flags & WDCF_SINGLE) != 0)
    507 			nblks = 1;
    508 		else if (d_link->sc_mode != WDM_DMA)
    509 			nblks = xfer->c_bcount / d_link->sc_lp->d_secsize;
    510 		else
    511 			nblks =
    512 			    min(xfer->c_bcount / d_link->sc_lp->d_secsize, 8);
    513 
    514 		/* Check for bad sectors and adjust transfer, if necessary. */
    515 		if ((d_link->sc_lp->d_flags & D_BADSECT) != 0
    516 #ifdef B_FORMAT
    517 		    && (bp->b_flags & B_FORMAT) == 0
    518 #endif
    519 		    ) {
    520 			long blkdiff;
    521 			int i;
    522 
    523 			for (i = 0;
    524 			    (blkdiff = d_link->sc_badsect[i]) != -1; i++) {
    525 				blkdiff -= blkno;
    526 				if (blkdiff < 0)
    527 					continue;
    528 				if (blkdiff == 0) {
    529 					/* Replace current block of transfer. */
    530 					blkno =
    531 					    d_link->sc_lp->d_secperunit -
    532 					    d_link->sc_lp->d_nsectors - i - 1;
    533 				}
    534 				if (blkdiff < nblks) {
    535 					/* Bad block inside transfer. */
    536 					wdc->sc_flags |= WDCF_SINGLE;
    537 					nblks = 1;
    538 				}
    539 				break;
    540 			}
    541 			/* Transfer is okay now. */
    542 		}
    543 
    544 		if ((d_link->sc_flags & WDF_LBA) != 0) {
    545 			sector = (blkno >> 0) & 0xff;
    546 			cylin = (blkno >> 8) & 0xffff;
    547 			head = (blkno >> 24) & 0xf;
    548 			head |= WDSD_LBA;
    549 		} else {
    550 			sector = blkno % d_link->sc_lp->d_nsectors;
    551 			sector++;	/* Sectors begin with 1, not 0. */
    552 			blkno /= d_link->sc_lp->d_nsectors;
    553 			head = blkno % d_link->sc_lp->d_ntracks;
    554 			blkno /= d_link->sc_lp->d_ntracks;
    555 			cylin = blkno;
    556 			head |= WDSD_CHS;
    557 		}
    558 
    559 		if (d_link->sc_mode == WDM_PIOSINGLE ||
    560 		    (wdc->sc_flags & WDCF_SINGLE) != 0)
    561 			xfer->c_nblks = 1;
    562 		else if (d_link->sc_mode == WDM_PIOMULTI)
    563 			xfer->c_nblks = min(nblks, d_link->sc_multiple);
    564 		else
    565 			xfer->c_nblks = nblks;
    566 		xfer->c_nbytes = xfer->c_nblks * d_link->sc_lp->d_secsize;
    567 
    568 #ifdef B_FORMAT
    569 		if (bp->b_flags & B_FORMAT) {
    570 			sector = d_link->sc_lp->d_gap3;
    571 			nblks = d_link->sc_lp->d_nsectors;
    572 			command = WDCC_FORMAT;
    573 		} else
    574 #endif
    575 		switch (d_link->sc_mode) {
    576 		case WDM_DMA:
    577 			command = (xfer->c_flags & B_READ) ?
    578 			    WDCC_READDMA : WDCC_WRITEDMA;
    579 			/* Start the DMA channel. */
    580 			(*wdc->sc_dma_start)(wdc->sc_dma_arg,
    581 			    xfer->databuf + xfer->c_skip, xfer->c_nbytes,
    582 			    xfer->c_flags & B_READ);
    583 			break;
    584 
    585 		case WDM_PIOMULTI:
    586 			command = (xfer->c_flags & B_READ) ?
    587 			    WDCC_READMULTI : WDCC_WRITEMULTI;
    588 			break;
    589 
    590 		case WDM_PIOSINGLE:
    591 			command = (xfer->c_flags & B_READ) ?
    592 			    WDCC_READ : WDCC_WRITE;
    593 			break;
    594 
    595 		default:
    596 #ifdef DIAGNOSTIC
    597 			panic("bad wd mode");
    598 #endif
    599 			return;
    600 		}
    601 
    602 		/* Initiate command! */
    603 		if (wdccommand(wdc, d_link, command, d_link->drive,
    604 		    cylin, head, sector, nblks) != 0) {
    605 			wderror(d_link, NULL,
    606 			    "wdc_ata_start: timeout waiting for unbusy");
    607 			wdcunwedge(wdc);
    608 			return;
    609 		}
    610 
    611 		WDDEBUG_PRINT(("sector %lu cylin %lu head %lu addr %p sts %x\n",
    612 		    sector, cylin, head, xfer->databuf,
    613 		    bus_space_read_1(wdc->sc_auxiot, wdc->sc_auxioh,
    614 			wd_aux_altsts)));
    615 
    616 	} else if (xfer->c_nblks > 1) {
    617 		/* The number of blocks in the last stretch may be smaller. */
    618 		nblks = xfer->c_bcount / d_link->sc_lp->d_secsize;
    619 		if (xfer->c_nblks > nblks) {
    620 			xfer->c_nblks = nblks;
    621 			xfer->c_nbytes = xfer->c_bcount;
    622 		}
    623 	}
    624 
    625 	/* If this was a write and not using DMA, push the data. */
    626 	if (d_link->sc_mode != WDM_DMA &&
    627 	    (xfer->c_flags & (B_READ|B_WRITE)) == B_WRITE) {
    628 		if (wait_for_drq(wdc) < 0) {
    629 			wderror(d_link, NULL,
    630 			    "wdc_ata_start: timeout waiting for drq");
    631 			wdcunwedge(wdc);
    632 			return;
    633 		}
    634 
    635 		if ((wdc->sc_cap & WDC_CAPABILITY_DATA32) == 0)
    636 			bus_space_write_multi_stream_2(wdc->sc_iot, wdc->sc_ioh,
    637 			    wd_data, xfer->databuf + xfer->c_skip,
    638 			    xfer->c_nbytes >> 1);
    639 		else
    640 			bus_space_write_multi_stream_4(wdc->sc_iot, wdc->sc_ioh,
    641 			    wd_data, xfer->databuf + xfer->c_skip,
    642 			    xfer->c_nbytes >> 2);
    643 	}
    644 
    645 	wdc->sc_flags |= WDCF_IRQ_WAIT;
    646 	WDDEBUG_PRINT(("wdc_ata_start: timeout "));
    647 	timeout(wdctimeout, wdc, WAITTIME);
    648 	WDDEBUG_PRINT(("done\n"));
    649 }
    650 
    651 int
    652 wdc_ata_intr(wdc,xfer)
    653 	struct wdc_softc *wdc;
    654 	struct wdc_xfer *xfer;
    655 {
    656 	struct wd_link *d_link;
    657 
    658 	d_link = xfer->d_link;
    659 
    660 	if (wait_for_unbusy(wdc) < 0) {
    661 		wdcerror(wdc, "wdcintr: timeout waiting for unbusy");
    662 		return 0;
    663 	}
    664 
    665 	wdc->sc_flags &= ~WDCF_IRQ_WAIT;
    666 	untimeout(wdctimeout, wdc);
    667 
    668 	/* Is it not a transfer, but a control operation? */
    669 	if (d_link->sc_state < READY) {
    670 		if (wdccontrol(wdc, d_link) == 0) {
    671 			/* The drive is busy.  Wait. */
    672 			return 1;
    673 		}
    674 		WDDEBUG_PRINT(("wdc_ata_start from wdc_ata_intr(open) flags 0x%x\n",
    675 		    wdc->sc_flags));
    676 		wdc_ata_start(wdc,xfer);
    677 		return 1;
    678 	}
    679 
    680 	/* Turn off the DMA channel. */
    681 	if (d_link->sc_mode == WDM_DMA)
    682 		(*wdc->sc_dma_finish)(wdc->sc_dma_arg);
    683 
    684 	/* Have we an error? */
    685 	if (wdc->sc_status & WDCS_ERR) {
    686 #ifdef WDDEBUG
    687 		wderror(d_link, NULL, "wdc_ata_start");
    688 #endif
    689 		if ((wdc->sc_flags & WDCF_SINGLE) == 0) {
    690 			wdc->sc_flags |= WDCF_ERROR;
    691 			goto restart;
    692 		}
    693 
    694 #ifdef B_FORMAT
    695 		if (bp->b_flags & B_FORMAT)
    696 			goto bad;
    697 #endif
    698 
    699 		if (++wdc->sc_errors < WDIORETRIES) {
    700 			if (wdc->sc_errors == (WDIORETRIES + 1) / 2) {
    701 #if 0
    702 				wderror(wd, NULL, "wedgie");
    703 #endif
    704 				wdcunwedge(wdc);
    705 				return 1;
    706 			}
    707 			goto restart;
    708 		}
    709 		wderror(d_link, xfer->c_bp, "wdc_ata_intr hard error");
    710 
    711 #ifdef B_FORMAT
    712 	bad:
    713 #endif
    714 		xfer->c_flags |= C_ERROR;
    715 		goto done;
    716 	}
    717 
    718 	/* If this was a read and not using DMA, fetch the data. */
    719 	if (d_link->sc_mode != WDM_DMA &&
    720 	    (xfer->c_flags & (B_READ|B_WRITE)) == B_READ) {
    721 		if ((wdc->sc_status & (WDCS_DRDY | WDCS_DSC | WDCS_DRQ))
    722 		    != (WDCS_DRDY | WDCS_DSC | WDCS_DRQ)) {
    723 			wderror(d_link, NULL, "wdcintr: read intr before drq");
    724 			wdcunwedge(wdc);
    725 			return 1;
    726 		}
    727 
    728 		/* Pull in data. */
    729 		if ((wdc->sc_cap & WDC_CAPABILITY_DATA32) == 0)
    730 			bus_space_read_multi_stream_2(wdc->sc_iot, wdc->sc_ioh,
    731 			    wd_data, xfer->databuf + xfer->c_skip,
    732 			    xfer->c_nbytes >> 1);
    733 		else
    734 			bus_space_read_multi_stream_4(wdc->sc_iot, wdc->sc_ioh,
    735 			    wd_data, xfer->databuf + xfer->c_skip,
    736 			    xfer->c_nbytes >> 2);
    737 	}
    738 
    739 	/* If we encountered any abnormalities, flag it as a soft error. */
    740 	if (wdc->sc_errors > 0 ||
    741 	    (wdc->sc_status & WDCS_CORR) != 0) {
    742 		wderror(d_link, xfer->c_bp, "soft error (corrected)");
    743 		wdc->sc_errors = 0;
    744 	}
    745 
    746 	/* Adjust pointers for the next block, if any. */
    747 	xfer->c_blkno += xfer->c_nblks;
    748 	xfer->c_skip += xfer->c_nbytes;
    749 	xfer->c_bcount -= xfer->c_nbytes;
    750 
    751 	/* See if this transfer is complete. */
    752 	if (xfer->c_bcount > 0)
    753 		goto restart;
    754 
    755 done:
    756 	/* Done with this transfer, with or without error. */
    757 	wdc_ata_done(wdc, xfer);
    758 	return 1;
    759 
    760 restart:
    761 	/* Start the next operation */
    762 	WDDEBUG_PRINT(("wdc_ata_start from wdcintr flags 0x%x\n",
    763 	    wdc->sc_flags));
    764 	wdc_ata_start(wdc, xfer);
    765 
    766 	return 1;
    767 }
    768 
    769 void
    770 wdc_ata_done(wdc, xfer)
    771 	struct wdc_softc *wdc;
    772 	struct wdc_xfer *xfer;
    773 {
    774 	struct buf *bp = xfer->c_bp;
    775 	struct wd_link *d_link = xfer->d_link;
    776 	int s;
    777 
    778 	WDDEBUG_PRINT(("wdc_ata_done\n"));
    779 
    780 	if (wdc->sc_cap & WDC_CAPABILITY_HWLOCK)
    781 		(*wdc->sc_free_hw)(wdc);
    782 
    783 	/* remove this command from xfer queue */
    784 	s = splbio();
    785 	TAILQ_REMOVE(&wdc->sc_xfer, xfer, c_xferchain);
    786 	wdc->sc_flags &= ~(WDCF_SINGLE | WDCF_ERROR | WDCF_ACTIVE);
    787 	wdc->sc_errors = 0;
    788 	if (bp) {
    789 		if (xfer->c_flags & C_ERROR) {
    790 			bp->b_flags |= B_ERROR;
    791 			bp->b_error = EIO;
    792 		}
    793 		bp->b_resid = xfer->c_bcount;
    794 		wddone(d_link, bp);
    795 		biodone(bp);
    796 	} else {
    797 		wakeup(xfer->databuf);
    798 	}
    799 	xfer->c_skip = 0;
    800 	wdc_free_xfer(xfer);
    801 	d_link->openings++;
    802 	wdstart((void*)d_link->wd_softc);
    803 	WDDEBUG_PRINT(("wdcstart from wdc_ata_done, flags 0x%x\n",
    804 	    wdc->sc_flags));
    805 	wdcstart(wdc);
    806 	splx(s);
    807 }
    808 
    809 /*
    810  * Get the drive parameters, if ESDI or ATA, or create fake ones for ST506.
    811  */
    812 int
    813 wdc_get_parms(wdc, d_link)
    814 	struct wdc_softc * wdc;
    815 	struct wd_link *d_link;
    816 {
    817 	int i;
    818 	char tb[DEV_BSIZE];
    819 	int s, error;
    820 
    821 	/*
    822 	 * XXX
    823 	 * The locking done here, and the length of time this may keep the rest
    824 	 * of the system suspended, is a kluge.  This should be rewritten to
    825 	 * set up a transfer and queue it through wdstart(), but it's called
    826 	 * infrequently enough that this isn't a pressing matter.
    827 	 */
    828 
    829 	s = splbio();
    830 
    831 	while ((wdc->sc_flags & WDCF_ACTIVE) != 0) {
    832 		wdc->sc_flags |= WDCF_WANTED;
    833 		if ((error = tsleep(wdc, PRIBIO | PCATCH, "wdprm", 0)) != 0) {
    834 			splx(s);
    835 			return error;
    836 		}
    837 	}
    838 	if (wdc->sc_cap & WDC_CAPABILITY_HWLOCK)
    839 		if (!(*wdc->sc_claim_hw)(wdc, 1))
    840 			panic("wdc_get_parms: Cannot claim wd-hardware");
    841 
    842 	wdc->sc_flags |= WDCF_ACTIVE;
    843 
    844 	if (wdccommandshort(wdc, d_link->drive, WDCC_IDENTIFY) != 0 ||
    845 	    wait_for_drq(wdc) != 0) {
    846 		/*
    847 		 * We `know' there's a drive here; just assume it's old.
    848 		 * This geometry is only used to read the MBR and print a
    849 		 * (false) attach message.
    850 		 */
    851 		strncpy(d_link->sc_lp->d_typename, "ST506",
    852 		    sizeof d_link->sc_lp->d_typename);
    853 		d_link->sc_lp->d_type = DTYPE_ST506;
    854 
    855 		strncpy(d_link->sc_params.wdp_model, "unknown",
    856 		    sizeof d_link->sc_params.wdp_model);
    857 		d_link->sc_params.wdp_config = WD_CFG_FIXED;
    858 		d_link->sc_params.wdp_cylinders = 1024;
    859 		d_link->sc_params.wdp_heads = 8;
    860 		d_link->sc_params.wdp_sectors = 17;
    861 		d_link->sc_params.wdp_multi = 0x0000;
    862 		d_link->sc_params.wdp_capabilities1 = 0x0000;
    863 		d_link->sc_params.wdp_capabilities2 = 0x0000;
    864 		d_link->sc_params.wdp_ataversion = 0x0000;
    865 	} else {
    866 		strncpy(d_link->sc_lp->d_typename, "ESDI/IDE",
    867 		    sizeof d_link->sc_lp->d_typename);
    868 		d_link->sc_lp->d_type = DTYPE_ESDI;
    869 
    870 		/* Read in parameter block. */
    871 		bus_space_read_multi_2(wdc->sc_iot, wdc->sc_ioh, wd_data,
    872 		    (u_int16_t *)tb, sizeof(tb) >> 1);
    873 		bcopy(tb, &d_link->sc_params, sizeof(struct wdparams));
    874 
    875 		/* Shuffle string byte order. */
    876 		for (i = 0; i < sizeof(d_link->sc_params.wdp_model); i += 2) {
    877 			u_short *p;
    878 			p = (u_short *)(d_link->sc_params.wdp_model + i);
    879 			*p = ntohs(*p);
    880 		}
    881 	}
    882 
    883 	/* Clear any leftover interrupt. */
    884 	(void) bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status);
    885 
    886 	if (wdc->sc_cap & WDC_CAPABILITY_HWLOCK)
    887 		(*wdc->sc_free_hw)(wdc);
    888 
    889 	/* Restart the queue. */
    890 	WDDEBUG_PRINT(("wdcstart from wdc_get_parms flags 0x%x\n",
    891 	    wdc->sc_flags));
    892 	wdc->sc_flags &= ~WDCF_ACTIVE;
    893 	wdcstart(wdc);
    894 
    895 	splx(s);
    896 	return 0;
    897 }
    898 
    899 /*
    900  * Implement operations needed before read/write.
    901  * Returns 0 if operation still in progress, 1 if completed.
    902  */
    903 int
    904 wdccontrol(wdc, d_link)
    905 	struct wdc_softc *wdc;
    906 	struct wd_link *d_link;
    907 {
    908 	WDDEBUG_PRINT(("wdccontrol\n"));
    909 
    910 	switch (d_link->sc_state) {
    911 	case RECAL:	/* Set SDH, step rate, do recal. */
    912 		if (wdccommandshort(wdc, d_link->drive, WDCC_RECAL) != 0) {
    913 			wderror(d_link, NULL, "wdccontrol: recal failed (1)");
    914 			goto bad;
    915 		}
    916 		d_link->sc_state = RECAL_WAIT;
    917 		break;
    918 
    919 	case RECAL_WAIT:
    920 		if (wdc->sc_status & WDCS_ERR) {
    921 			wderror(d_link, NULL, "wdccontrol: recal failed (2)");
    922 			goto bad;
    923 		}
    924 		/* fall through */
    925 
    926 	case GEOMETRY:
    927 		if ((d_link->sc_flags & WDF_LBA) != 0)
    928 			goto multimode;
    929 		if (wdsetctlr(d_link) != 0) {
    930 			/* Already printed a message. */
    931 			goto bad;
    932 		}
    933 		d_link->sc_state = GEOMETRY_WAIT;
    934 		break;
    935 
    936 	case GEOMETRY_WAIT:
    937 		if (wdc->sc_status & WDCS_ERR) {
    938 			wderror(d_link, NULL, "wdccontrol: geometry failed");
    939 			goto bad;
    940 		}
    941 		/* fall through */
    942 
    943 	case MULTIMODE:
    944 	multimode:
    945 		if (d_link->sc_mode != WDM_PIOMULTI)
    946 			goto ready;
    947 		bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_seccnt, d_link->sc_multiple);
    948 		if (wdccommandshort(wdc, d_link->drive,
    949 		    WDCC_SETMULTI) != 0) {
    950 			wderror(d_link, NULL,
    951 			    "wdccontrol: setmulti failed (1)");
    952 			goto bad;
    953 		}
    954 		d_link->sc_state = MULTIMODE_WAIT;
    955 		break;
    956 
    957 	case MULTIMODE_WAIT:
    958 		if (wdc->sc_status & WDCS_ERR) {
    959 			wderror(d_link, NULL,
    960 			    "wdccontrol: setmulti failed (2)");
    961 			goto bad;
    962 		}
    963 		/* fall through */
    964 
    965 	case READY:
    966 	ready:
    967 		wdc->sc_errors = 0;
    968 		d_link->sc_state = READY;
    969 		/*
    970 		 * The rest of the initialization can be done by normal means.
    971 		 */
    972 		return 1;
    973 
    974 	bad:
    975 		wdcunwedge(wdc);
    976 		return 0;
    977 	}
    978 
    979 	wdc->sc_flags |= WDCF_IRQ_WAIT;
    980 	timeout(wdctimeout, wdc, WAITTIME);
    981 	return 0;
    982 }
    983 
    984 #endif /* NWD > 0 */
    985 
    986 
    987 /*
    988  * Interrupt routine for the controller.  Acknowledge the interrupt, check for
    989  * errors on the current operation, mark it done if necessary, and start the
    990  * next request.  Also check for a partially done transfer, and continue with
    991  * the next chunk if so.
    992  */
    993 int
    994 wdcintr(arg)
    995 	void *arg;
    996 {
    997 	struct wdc_softc *wdc = arg;
    998 	struct wdc_xfer *xfer;
    999 
   1000 	if ((wdc->sc_flags & WDCF_IRQ_WAIT) == 0) {
   1001 		/* Clear the pending interrupt and abort. */
   1002 		u_char s = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status);
   1003 
   1004 #ifdef ATAPI_DEBUG_WDC
   1005 		u_char e = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
   1006 		u_char i = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_seccnt);
   1007 		printf("wdcintr: inactive controller, "
   1008 		    "punting st=%02x er=%02x irr=%02x\n", s, e, i);
   1009 #else
   1010 		bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
   1011 		bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_seccnt);
   1012 #endif
   1013 
   1014 		if (s & WDCS_DRQ) {
   1015 			int len = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_lo) +
   1016 			    256 * bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_hi);
   1017 #ifdef ATAPI_DEBUG_WDC
   1018 			printf ("wdcintr: clearing up %d bytes\n", len);
   1019 #endif
   1020 			wdcbit_bucket (wdc, len);
   1021 		}
   1022 		return 0;
   1023 	}
   1024 
   1025 	WDDEBUG_PRINT(("wdcintr\n"));
   1026 
   1027 	xfer = wdc->sc_xfer.tqh_first;
   1028 #if NATAPIBUS > 0 && NWD > 0
   1029 	if (xfer->c_flags & C_ATAPI) {
   1030 		return wdc_atapi_intr(wdc,xfer);
   1031 	} else
   1032 		return wdc_ata_intr(wdc,xfer);
   1033 #else /* NATAPIBUS > 0  && NWD > 0 */
   1034 #if NATAPIBUS > 0
   1035 	return wdc_atapi_intr(wdc,xfer);
   1036 #endif /* NATAPIBUS > 0 */
   1037 #if NWD > 0
   1038 	return wdc_ata_intr(wdc,xfer);
   1039 #endif /* NWD > 0 */
   1040 #endif /* NATAPIBUS > 0  && NWD > 0 */
   1041 
   1042 	/*
   1043 	 * Catch-all; if no children were configured, the interrupt
   1044 	 * could not have been for us.
   1045 	 */
   1046 	return (0);
   1047 }
   1048 
   1049 int
   1050 wdcreset(wdc, verb)
   1051 	struct wdc_softc *wdc;
   1052 	int verb;
   1053 {
   1054 
   1055 	/* Reset the device. */
   1056 	bus_space_write_1(wdc->sc_auxiot, wdc->sc_auxioh, wd_aux_ctlr,
   1057 	    WDCTL_RST | WDCTL_IDS);
   1058 	delay(1000);
   1059 	bus_space_write_1(wdc->sc_auxiot, wdc->sc_auxioh, wd_aux_ctlr,
   1060 	    WDCTL_IDS);
   1061 	delay(1000);
   1062 	(void) bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
   1063 	bus_space_write_1(wdc->sc_auxiot, wdc->sc_auxioh, wd_aux_ctlr,
   1064 	    WDCTL_4BIT);
   1065 
   1066 	if (wait_for_unbusy(wdc) < 0) {
   1067 		if (verb)
   1068 			printf("%s: reset failed\n", wdc->sc_dev.dv_xname);
   1069 		return 1;
   1070 	}
   1071 
   1072 	return 0;
   1073 }
   1074 
   1075 void
   1076 wdcrestart(arg)
   1077 	void *arg;
   1078 {
   1079 	struct wdc_softc *wdc = arg;
   1080 	int s;
   1081 
   1082 	s = splbio();
   1083 	wdcstart(wdc);
   1084 	splx(s);
   1085 }
   1086 
   1087 /*
   1088  * Unwedge the controller after an unexpected error.  We do this by resetting
   1089  * it, marking all drives for recalibration, and stalling the queue for a short
   1090  * period to give the reset time to finish.
   1091  * NOTE: We use a timeout here, so this routine must not be called during
   1092  * autoconfig or dump.
   1093  */
   1094 void
   1095 wdcunwedge(wdc)
   1096 	struct wdc_softc *wdc;
   1097 {
   1098 	int unit;
   1099 
   1100 #ifdef ATAPI_DEBUG
   1101 	printf("wdcunwedge\n");
   1102 #endif
   1103 
   1104 	untimeout(wdctimeout, wdc);
   1105 	wdc->sc_flags &= ~WDCF_IRQ_WAIT;
   1106 	(void) wdcreset(wdc, VERBOSE);
   1107 
   1108 	/* Schedule recalibrate for all drives on this controller. */
   1109 	for (unit = 0; unit < 2; unit++) {
   1110 		if (!wdc->d_link[unit])
   1111 			wdccommandshort(wdc, unit, ATAPI_SOFT_RESET);
   1112 		else if (wdc->d_link[unit]->sc_state > RECAL)
   1113 			wdc->d_link[unit]->sc_state = RECAL;
   1114 	}
   1115 
   1116 	wdc->sc_flags |= WDCF_ERROR;
   1117 	++wdc->sc_errors;
   1118 
   1119 	/* Wake up in a little bit and restart the operation. */
   1120 	WDDEBUG_PRINT(("wdcrestart from wdcunwedge\n"));
   1121 	wdc->sc_flags &= ~WDCF_ACTIVE;
   1122 	timeout(wdcrestart, wdc, RECOVERYTIME);
   1123 }
   1124 
   1125 int
   1126 wdcwait(wdc, mask)
   1127 	struct wdc_softc *wdc;
   1128 	int mask;
   1129 {
   1130 	int timeout = 0;
   1131 	u_char status;
   1132 #ifdef WDCNDELAY_DEBUG
   1133 	extern int cold;
   1134 #endif
   1135 
   1136 	WDDEBUG_PRINT(("wdcwait\n"));
   1137 
   1138 	for (;;) {
   1139 		wdc->sc_status = status = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status);
   1140 		/*
   1141 		 * XXX
   1142 		 * If a single slave ATAPI device is attached, it may
   1143 		 * have released the bus. Select it and try again.
   1144 		 */
   1145 		if (status == 0xff && wdc->sc_flags & WDCF_ONESLAVE) {
   1146 			bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_sdh, WDSD_IBM | 0x10);
   1147 			wdc->sc_status = status = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status);
   1148 		}
   1149 		if ((status & WDCS_BSY) == 0 && (status & mask) == mask)
   1150 			break;
   1151 		if (++timeout > WDCNDELAY) {
   1152 #ifdef ATAPI_DEBUG
   1153 			printf("wdcwait: timeout, status %x error %x\n", status, bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error));
   1154 #endif
   1155 			return -1;
   1156 		}
   1157 		delay(WDCDELAY);
   1158 	}
   1159 	if (status & WDCS_ERR) {
   1160 		wdc->sc_error = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
   1161 		return WDCS_ERR;
   1162 	}
   1163 #ifdef WDCNDELAY_DEBUG
   1164 	/* After autoconfig, there should be no long delays. */
   1165 	if (!cold && timeout > WDCNDELAY_DEBUG) {
   1166 		struct wdc_xfer *xfer = wdc->sc_xfer.tqh_first;
   1167 		if (xfer == NULL)
   1168 			printf("%s: warning: busy-wait took %dus\n",
   1169 	    		wdc->sc_dev.dv_xname, WDCDELAY * timeout);
   1170 		else
   1171 			printf("%s(%s): warning: busy-wait took %dus\n",
   1172 				wdc->sc_dev.dv_xname,
   1173 			    ((struct device*)xfer->d_link->wd_softc)->dv_xname,
   1174 				WDCDELAY * timeout);
   1175 	}
   1176 #endif
   1177 	return 0;
   1178 }
   1179 
   1180 void
   1181 wdctimeout(arg)
   1182 	void *arg;
   1183 {
   1184 	struct wdc_softc *wdc = (struct wdc_softc *)arg;
   1185 	struct wdc_xfer *xfer = wdc->sc_xfer.tqh_first;
   1186 	int s;
   1187 
   1188 	WDDEBUG_PRINT(("wdctimeout\n"));
   1189 
   1190 	s = splbio();
   1191 	if ((wdc->sc_flags & WDCF_IRQ_WAIT) != 0) {
   1192 		wdcerror(wdc, "lost interrupt");
   1193 		printf("\ttype: %s\n", (xfer->c_flags & C_ATAPI) ? "atapi":"ata");
   1194 		printf("\tc_bcount: %d\n", xfer->c_bcount);
   1195 		printf("\tc_skip: %d\n", xfer->c_skip);
   1196 		wdcintr(wdc);
   1197 		wdc->sc_flags &= ~WDCF_IRQ_WAIT;
   1198 		wdcunwedge(wdc);
   1199 	} else
   1200 		wdcerror(wdc, "missing untimeout");
   1201 	splx(s);
   1202 }
   1203 
   1204 /*
   1205  * Wait for the drive to become ready and send a command.
   1206  * Return -1 if busy for too long or 0 otherwise.
   1207  * Assumes interrupts are blocked.
   1208  */
   1209 int
   1210 wdccommand(wdc, d_link, command, drive, cylin, head, sector, count)
   1211 		struct wdc_softc *wdc;
   1212         struct wd_link *d_link;
   1213         int command;
   1214         int drive, cylin, head, sector, count;
   1215 {
   1216         int stat;
   1217 
   1218 	WDDEBUG_PRINT(("wdccommand drive %d\n", drive));
   1219 
   1220 #if defined(DIAGNOSTIC) && defined(WDCDEBUG)
   1221 	if ((wdc->sc_flags & WDCF_ACTIVE) == 0)
   1222 		printf("wdccommand: controler not active (drive %d)\n", drive);
   1223 #endif
   1224 
   1225         /* Select drive, head, and addressing mode. */
   1226         bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_sdh, WDSD_IBM | (drive << 4) | head);
   1227 
   1228         /* Wait for it to become ready to accept a command. */
   1229         if (command == WDCC_IDP || d_link->type == ATAPI)
   1230                 stat = wait_for_unbusy(wdc);
   1231         else
   1232                 stat = wdcwait(wdc, WDCS_DRDY);
   1233 
   1234         if (stat < 0) {
   1235 #ifdef ATAPI_DEBUG
   1236 		printf("wdcommand: xfer failed (wait_for_unbusy) status %d\n",
   1237 		    stat);
   1238 #endif
   1239                 return -1;
   1240 	}
   1241 
   1242         /* Load parameters. */
   1243         if (d_link->type == ATA && d_link->sc_lp->d_type == DTYPE_ST506)
   1244                 bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_precomp, d_link->sc_lp->d_precompcyl / 4);
   1245         else
   1246                 bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_features, 0);
   1247         bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_lo, cylin);
   1248         bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_hi, cylin >> 8);
   1249         bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_sector, sector);
   1250         bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_seccnt, count);
   1251 
   1252         /* Send command. */
   1253         bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_command, command);
   1254 
   1255         return 0;
   1256 }
   1257 
   1258 /*
   1259  * Simplified version of wdccommand().
   1260  */
   1261 int
   1262 wdccommandshort(wdc, drive, command)
   1263 	struct wdc_softc *wdc;
   1264         int drive;
   1265         int command;
   1266 {
   1267 
   1268 	WDDEBUG_PRINT(("wdccommandshort\n"));
   1269 
   1270 #if defined(DIAGNOSTIC) && defined(WDCDEBUG)
   1271 	if ((wdc->sc_flags & WDCF_ACTIVE) == 0)
   1272 		printf("wdccommandshort: controller not active (drive %d)\n",
   1273 		    drive);
   1274 #endif
   1275 
   1276         /* Select drive. */
   1277         bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_sdh, WDSD_IBM | (drive << 4));
   1278 
   1279         if (wdcwait(wdc, WDCS_DRDY) < 0)
   1280                 return -1;
   1281 
   1282         bus_space_write_1(wdc->sc_iot, wdc->sc_ioh, wd_command, command);
   1283 
   1284 	return 0;
   1285 }
   1286 
   1287 void
   1288 wdc_exec_xfer(wdc, d_link, xfer)
   1289 	struct wdc_softc *wdc;
   1290 	struct wd_link *d_link;
   1291 	struct wdc_xfer *xfer;
   1292 {
   1293 	int s;
   1294 
   1295 	WDDEBUG_PRINT(("wdc_exec_xfer\n"));
   1296 
   1297 	s = splbio();
   1298 
   1299 	/* insert at the end of command list */
   1300 	TAILQ_INSERT_TAIL(&wdc->sc_xfer,xfer , c_xferchain);
   1301 	WDDEBUG_PRINT(("wdcstart from wdc_exec_xfer, flags 0x%x\n",
   1302 	    wdc->sc_flags));
   1303 	wdcstart(wdc);
   1304 	xfer->c_flags |= C_NEEDDONE; /* we can now call upper level done() */
   1305 	splx(s);
   1306 }
   1307 
   1308 struct wdc_xfer *
   1309 wdc_get_xfer(flags)
   1310 	int flags;
   1311 {
   1312 	struct wdc_xfer *xfer;
   1313 	int s;
   1314 
   1315 	s = splbio();
   1316 	if ((xfer = xfer_free_list.lh_first) != NULL) {
   1317 		LIST_REMOVE(xfer, free_list);
   1318 		splx(s);
   1319 #ifdef DIAGNOSTIC
   1320 		if ((xfer->c_flags & C_INUSE) != 0)
   1321 			panic("wdc_get_xfer: xfer already in use\n");
   1322 #endif
   1323 	} else {
   1324 		splx(s);
   1325 #ifdef ATAPI_DEBUG2
   1326 		printf("wdc:making xfer %d\n",wdc_nxfer);
   1327 #endif
   1328 		xfer = malloc(sizeof(*xfer), M_DEVBUF,
   1329 		    ((flags & IDE_NOSLEEP) != 0 ? M_NOWAIT : M_WAITOK));
   1330 		if (xfer == NULL)
   1331 			return 0;
   1332 
   1333 #ifdef DIAGNOSTIC
   1334 		xfer->c_flags &= ~C_INUSE;
   1335 #endif
   1336 #ifdef ATAPI_DEBUG2
   1337 		wdc_nxfer++;
   1338 #endif
   1339 	}
   1340 #ifdef DIAGNOSTIC
   1341 	if ((xfer->c_flags & C_INUSE) != 0)
   1342 		panic("wdc_get_xfer: xfer already in use\n");
   1343 #endif
   1344 	bzero(xfer,sizeof(struct wdc_xfer));
   1345 	xfer->c_flags = C_INUSE;
   1346 	return xfer;
   1347 }
   1348 
   1349 void
   1350 wdc_free_xfer(xfer)
   1351 	struct wdc_xfer *xfer;
   1352 {
   1353 	int s;
   1354 
   1355 	s = splbio();
   1356 	xfer->c_flags &= ~C_INUSE;
   1357 	LIST_INSERT_HEAD(&xfer_free_list, xfer, free_list);
   1358 	splx(s);
   1359 }
   1360 
   1361 void
   1362 wdcerror(wdc, msg)
   1363 	struct wdc_softc *wdc;
   1364 	char *msg;
   1365 {
   1366 	struct wdc_xfer *xfer = wdc->sc_xfer.tqh_first;
   1367 	if (xfer == NULL)
   1368 		printf("%s: %s\n", wdc->sc_dev.dv_xname, msg);
   1369 	else
   1370 		printf("%s(%d): %s\n", wdc->sc_dev.dv_xname,
   1371 		    xfer->d_link->drive, msg);
   1372 }
   1373 
   1374 /*
   1375  * the bit bucket
   1376  */
   1377 void
   1378 wdcbit_bucket(wdc, size)
   1379 	struct wdc_softc *wdc;
   1380 	int size;
   1381 {
   1382 
   1383 	for (; size >= 2; size -= 2)
   1384 		(void)bus_space_read_2(wdc->sc_iot, wdc->sc_ioh, wd_data);
   1385 	if (size)
   1386 		(void)bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_data);
   1387 }
   1388 
   1389 
   1390 #if NATAPIBUS > 0
   1391 
   1392 void
   1393 wdc_atapi_minphys (struct buf *bp)
   1394 {
   1395     if(bp->b_bcount > MAX_SIZE)
   1396 		bp->b_bcount = MAX_SIZE;
   1397 	minphys(bp);
   1398 }
   1399 
   1400 
   1401 void
   1402 wdc_atapi_start(wdc, xfer)
   1403 	struct wdc_softc *wdc;
   1404 	struct wdc_xfer *xfer;
   1405 {
   1406 	struct scsipi_xfer *sc_xfer = xfer->atapi_cmd;
   1407 
   1408 #ifdef ATAPI_DEBUG_WDC
   1409 	printf("wdc_atapi_start, acp flags %x \n",sc_xfer->flags);
   1410 #endif
   1411 	if (wdc->sc_errors >= WDIORETRIES) {
   1412 		if ((wdc->sc_status & WDCS_ERR) == 0) {
   1413 			sc_xfer->error = XS_DRIVER_STUFFUP; /* XXX do we know more ? */
   1414 		} else {
   1415 			sc_xfer->error = XS_SENSE;
   1416 			sc_xfer->sense.atapi_sense = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
   1417 		}
   1418 		wdc_atapi_done(wdc, xfer);
   1419 		return;
   1420 	}
   1421 	if (wait_for_unbusy(wdc) != 0) {
   1422 		if ((wdc->sc_status & WDCS_ERR) == 0) {
   1423 			printf("wdc_atapi_start: not ready, st = %02x\n",
   1424 			    wdc->sc_status);
   1425 			sc_xfer->error = XS_SELTIMEOUT;
   1426 		}
   1427 #if 0 /* don't get the sense yet, as this may be just UNIT ATTENTION */
   1428 		else {
   1429 #ifdef ATAPI_DEBUG_WDC
   1430 			printf("wdc_atapi_start: sense %02x\n", wdc->sc_error);
   1431 #endif
   1432 			sc_xfer->error = XS_SENSE;
   1433 			sc_xfer->sense.atapi_sense = wdc->sc_error;
   1434 		}
   1435 		wdc_atapi_done(wdc, xfer);
   1436 		return;
   1437 #endif
   1438 	}
   1439 
   1440 	/*
   1441 	 * Limit length to what can be stuffed into the cylinder register
   1442 	 * (16 bits).  Some CD-ROMs seem to interpret '0' as 65536,
   1443 	 * but not all devices do that and it's not obvious from the
   1444 	 * ATAPI spec that that behaviour should be expected.  If more
   1445 	 * data is necessary, multiple data transfer phases will be done.
   1446 	 */
   1447 	if (wdccommand(wdc, (struct wd_link*)xfer->d_link, ATAPI_PACKET_COMMAND,
   1448 	    sc_xfer->sc_link->scsipi_atapi.drive,
   1449 	    sc_xfer->datalen <= 0xffff ? sc_xfer->datalen : 0xffff,
   1450 		0, 0, 0) != 0) {
   1451 		printf("wdc_atapi_start: can't send atapi packet command\n");
   1452 		sc_xfer->error = XS_DRIVER_STUFFUP;
   1453 		wdc_atapi_done(wdc, xfer);
   1454 		return;
   1455 	}
   1456 	if ((sc_xfer->sc_link->scsipi_atapi.cap  & 0x0300) != ACAP_DRQ_INTR) {
   1457 		int i, phase;
   1458 		for (i=20000; i>0; --i) {
   1459 			phase = (bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_ireason) &
   1460 			    (WDCI_CMD | WDCI_IN)) |
   1461 			    (bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status) & WDCS_DRQ);
   1462 			if (phase == PHASE_CMDOUT)
   1463 				break;
   1464 			delay(10);
   1465 		}
   1466 		if (phase != PHASE_CMDOUT ) {
   1467 			printf("wdc_atapi_start: timeout waiting PHASE_CMDOUT");
   1468 			sc_xfer->error = XS_SELTIMEOUT;
   1469 			wdc_atapi_done(wdc, xfer);
   1470 			return;
   1471 		}
   1472 		bus_space_write_multi_2(wdc->sc_iot, wdc->sc_ioh, wd_data,
   1473 		    (u_int16_t *)sc_xfer->cmd, sc_xfer->cmdlen >> 1);
   1474 	}
   1475 	wdc->sc_flags |= WDCF_IRQ_WAIT;
   1476 
   1477 #ifdef ATAPI_DEBUG2
   1478 	printf("wdc_atapi_start: timeout\n");
   1479 #endif
   1480 	timeout(wdctimeout, wdc, WAITTIME);
   1481 	return;
   1482 }
   1483 
   1484 
   1485 int
   1486 wdc_atapi_get_params(ab_link, drive, id)
   1487 	struct scsipi_link *ab_link;
   1488 	u_int8_t drive;
   1489 	struct atapi_identify *id;
   1490 {
   1491 	struct wdc_softc *wdc = (void*)ab_link->adapter_softc;
   1492 	int status, len, excess = 0;
   1493 	int s, error;
   1494 
   1495 	/* if a disk is already present, skip */
   1496 	if ((wdc->sc_drives_mask & (1 << drive)) != 0) {
   1497 #ifdef ATAPI_DEBUG_PROBE
   1498 		printf("wdc_atapi_get_params: drive %d present\n", drive);
   1499 #endif
   1500 		return 0;
   1501 	}
   1502 
   1503 	/*
   1504 	 * If there is only one ATAPI slave on the bus,don't probe
   1505 	 * drive 0 (master)
   1506 	 */
   1507 
   1508 	if (wdc->sc_flags & WDCF_ONESLAVE && drive != 1)
   1509 		return 0;
   1510 
   1511 #ifdef ATAPI_DEBUG_PROBE
   1512 	printf("wdc_atapi_get_params: probing drive %d\n", drive);
   1513 #endif
   1514 
   1515 	/*
   1516 	 * XXX
   1517 	 * The locking done here, and the length of time this may keep the rest
   1518 	 * of the system suspended, is a kluge.  This should be rewritten to
   1519 	 * set up a transfer and queue it through wdstart(), but it's called
   1520 	 * infrequently enough that this isn't a pressing matter.
   1521 	 */
   1522 
   1523 	s = splbio();
   1524 
   1525 	while ((wdc->sc_flags & WDCF_ACTIVE) != 0) {
   1526 		wdc->sc_flags |= WDCF_WANTED;
   1527 		if ((error = tsleep(wdc, PRIBIO | PCATCH, "atprm", 0)) != 0) {
   1528 			splx(s);
   1529 			return error;
   1530 		}
   1531 	}
   1532 
   1533 	wdc->sc_flags |= WDCF_ACTIVE;
   1534 	error = 1;
   1535 	(void)wdcreset(wdc, VERBOSE);
   1536 	if ((status = wdccommand(wdc, (struct wd_link*)(&(ab_link->scsipi_atapi)),
   1537 	    ATAPI_SOFT_RESET, drive, 0, 0, 0, 0)) != 0) {
   1538 #ifdef ATAPI_DEBUG
   1539 		printf("wdc_atapi_get_params: ATAPI_SOFT_RESET"
   1540 		    "failed for drive %d: status %d error %d\n",
   1541 		    drive, status, wdc->sc_error);
   1542 #endif
   1543 		error = 0;
   1544 		goto end;
   1545 	}
   1546 	if ((status = wait_for_unbusy(wdc)) != 0) {
   1547 #ifdef ATAPI_DEBUG
   1548 	printf("wdc_atapi_get_params: wait_for_unbusy failed "
   1549 	    "for drive %d: status %d error %d\n",
   1550 	    drive, status, wdc->sc_error);
   1551 #endif
   1552 		error = 0;
   1553 		goto end;
   1554 	}
   1555 
   1556 	/* Some ATAPI devices seem a bit more time after software reset. */
   1557 	delay(5000);
   1558 
   1559 	if (wdccommand(wdc, (struct wd_link*)(&(ab_link->scsipi_atapi)),
   1560 		ATAPI_IDENTIFY_DEVICE, drive, sizeof(struct atapi_identify),
   1561 		0, 0, 0) != 0 ||
   1562 	    atapi_ready(wdc) != 0) {
   1563 #ifdef ATAPI_DEBUG_PROBE
   1564 		printf("ATAPI_IDENTIFY_DEVICE failed for drive %d\n", drive);
   1565 #endif
   1566 		error = 0;
   1567 		goto end;
   1568 	}
   1569 	len = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_lo) + 256 *
   1570 	    bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_hi);
   1571 	if (len != sizeof(struct atapi_identify)) {
   1572 		printf("Warning drive %d returned %d/%d of "
   1573 		    "identify device data\n", drive, len,
   1574 		    sizeof(struct atapi_identify));
   1575 		excess = len - sizeof(struct atapi_identify);
   1576 		if (excess < 0)
   1577 			excess = 0;
   1578 	}
   1579 	bus_space_read_multi_2(wdc->sc_iot, wdc->sc_ioh, wd_data,
   1580 	    (u_int16_t *)id, sizeof(struct atapi_identify) >> 1);
   1581 	wdcbit_bucket(wdc, excess);
   1582 	wdc->sc_drives_mask |= (1 << drive);
   1583 
   1584  end:	/* Restart the queue. */
   1585 	WDDEBUG_PRINT(("wdcstart from wdc_atapi_get_parms flags 0x%x\n",
   1586 	    wdc->sc_flags));
   1587 	wdc->sc_flags &= ~WDCF_ACTIVE;
   1588 	wdcstart(wdc);
   1589 	splx(s);
   1590 	return error;
   1591 }
   1592 
   1593 int
   1594 wdc_atapi_send_command_packet(sc_xfer)
   1595 	struct scsipi_xfer *sc_xfer;
   1596 {
   1597 	struct scsipi_link *sc_link = sc_xfer->sc_link;
   1598 	struct wdc_softc *wdc = (void*)sc_link->adapter_softc;
   1599 	struct wdc_xfer *xfer;
   1600 	int flags = sc_xfer->flags;
   1601 
   1602 	if (flags & SCSI_POLL) {   /* should use the queue and wdc_atapi_start */
   1603 		struct wdc_xfer xfer_s;
   1604 		int i, s;
   1605 
   1606 		s = splbio();
   1607 #ifdef ATAPI_DEBUG_WDC
   1608 		printf("wdc_atapi_send_cmd: "
   1609 		    "flags 0x%x drive %d cmdlen %d datalen %d",
   1610 		    sc_xfer->flags, sc_link->scsipi_atapi.drive, sc_xfer->cmdlen,
   1611 			sc_xfer->datalen);
   1612 #endif
   1613 		xfer = &xfer_s;
   1614 		bzero(xfer, sizeof(xfer_s));
   1615 		xfer->c_flags = C_INUSE|C_ATAPI|flags;
   1616 		xfer->d_link = (struct wd_link *)(&sc_link->scsipi_atapi);
   1617 		xfer->c_bp = sc_xfer->bp;
   1618 		xfer->atapi_cmd = sc_xfer;
   1619 		xfer->c_blkno = 0;
   1620 		xfer->databuf = sc_xfer->data;
   1621 		xfer->c_bcount = sc_xfer->datalen;
   1622 
   1623 		if (wait_for_unbusy (wdc) != 0)  {
   1624 			if ((wdc->sc_status & WDCS_ERR) == 0) {
   1625 				printf("wdc_atapi_send_command: not ready, "
   1626 				    "st = %02x\n", wdc->sc_status);
   1627 				sc_xfer->error = XS_SELTIMEOUT;
   1628 			} else {
   1629 				sc_xfer->error = XS_SENSE;
   1630 				sc_xfer->sense.atapi_sense = wdc->sc_error;
   1631 			}
   1632 			splx(s);
   1633 			return COMPLETE;
   1634 		}
   1635 
   1636 		/*
   1637 		 * Limit length to what can be stuffed into the cylinder
   1638 		 * register (16 bits).  Some CD-ROMs seem to interpret '0'
   1639 		 * as 65536, but not all devices do that and it's not
   1640 		 * obvious from the ATAPI spec that that behaviour should
   1641 		 * be expected.  If more data is necessary, multiple data
   1642 		 * transfer phases will be done.
   1643 		 */
   1644 		if (wdccommand(wdc, (struct wd_link*)(&sc_link->scsipi_atapi),
   1645 		    ATAPI_PACKET_COMMAND, sc_link->scsipi_atapi.drive,
   1646 		    sc_xfer->datalen <= 0xffff ? sc_xfer->datalen : 0xffff,
   1647 		    0, 0, 0) != 0) {
   1648 			printf("can't send atapi packet command\n");
   1649 			sc_xfer->error = XS_DRIVER_STUFFUP;
   1650 			splx(s);
   1651 			return COMPLETE;
   1652 		}
   1653 
   1654 		/* Wait for cmd i/o phase. */
   1655 		for (i = 20000; i > 0; --i) {
   1656 			int phase;
   1657 			phase = (bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_ireason) &
   1658 			    (WDCI_CMD | WDCI_IN)) |
   1659 			    (bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status) & WDCS_DRQ);
   1660 			if (phase == PHASE_CMDOUT)
   1661 				break;
   1662 			delay(10);
   1663 		}
   1664 #ifdef ATAPI_DEBUG_WDC
   1665 		printf("Wait for cmd i/o phase: i = %d\n", i);
   1666 #endif
   1667 
   1668 		bus_space_write_multi_2(wdc->sc_iot, wdc->sc_ioh, wd_data,
   1669 		    (u_int16_t *)sc_xfer->cmd, sc_xfer->cmdlen >> 1);
   1670 
   1671 		/* Wait for data i/o phase. */
   1672 		for ( i= 20000; i > 0; --i) {
   1673 			int phase;
   1674 			phase = (bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_ireason) &
   1675 			    (WDCI_CMD | WDCI_IN)) |
   1676 			    (bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status) & WDCS_DRQ);
   1677 			if (phase != PHASE_CMDOUT)
   1678 				break;
   1679 			delay(10);
   1680 		}
   1681 
   1682 #ifdef ATAPI_DEBUG_WDC
   1683 		printf("Wait for data i/o phase: i = %d\n", i);
   1684 #endif
   1685 		wdc->sc_flags |= WDCF_IRQ_WAIT;
   1686 		while ((sc_xfer->flags & ITSDONE) == 0) {
   1687 			wdc_atapi_intr(wdc, xfer);
   1688 			for (i = 2000; i > 0; --i)
   1689 				if ((bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status)
   1690 				    & WDCS_DRQ) == 0)
   1691 					break;
   1692 #ifdef ATAPI_DEBUG_WDC
   1693 			printf("wdc_atapi_intr: i = %d\n", i);
   1694 #endif
   1695 		}
   1696 		wdc->sc_flags &= ~(WDCF_IRQ_WAIT | WDCF_SINGLE | WDCF_ERROR);
   1697 		wdc->sc_errors = 0;
   1698 		xfer->c_skip = 0;
   1699 		splx(s);
   1700 		return COMPLETE;
   1701 	} else {	/* POLLED */
   1702 		xfer = wdc_get_xfer(flags & SCSI_NOSLEEP ? IDE_NOSLEEP : 0);
   1703 		if (xfer == NULL) {
   1704 			return TRY_AGAIN_LATER;
   1705 		}
   1706 		xfer->c_flags |= C_ATAPI|sc_xfer->flags;
   1707 		xfer->d_link = (struct wd_link*)(&sc_link->scsipi_atapi);
   1708 		xfer->c_bp = sc_xfer->bp;
   1709 		xfer->atapi_cmd = sc_xfer;
   1710 		xfer->c_blkno = 0;
   1711 		xfer->databuf = sc_xfer->data;
   1712 		xfer->c_bcount = sc_xfer->datalen;
   1713 		wdc_exec_xfer(wdc, xfer->d_link, xfer);
   1714 #ifdef ATAPI_DEBUG_WDC
   1715 		printf("wdc_atapi_send_command_packet: wdc_exec_xfer, flags 0x%x\n",
   1716 			sc_xfer->flags);
   1717 #endif
   1718 		return (sc_xfer->flags & ITSDONE) ? COMPLETE : SUCCESSFULLY_QUEUED;
   1719 	}
   1720 }
   1721 
   1722 int
   1723 wdc_atapi_intr(wdc, xfer)
   1724 	struct wdc_softc *wdc;
   1725 	struct wdc_xfer *xfer;
   1726 {
   1727 	struct scsipi_xfer *sc_xfer = xfer->atapi_cmd;
   1728 	int len, phase, i, retries=0;
   1729 	int err, st, ire;
   1730 
   1731 #ifdef ATAPI_DEBUG2
   1732 	printf("wdc_atapi_intr: %s\n", wdc->sc_dev.dv_xname);
   1733 #endif
   1734 
   1735 	if (wait_for_unbusy(wdc) < 0) {
   1736 		if ((wdc->sc_status & WDCS_ERR) == 0) {
   1737 			printf("wdc_atapi_intr: controller busy\n");
   1738 			return 0;
   1739 		} else {
   1740 			sc_xfer->error = XS_SENSE;
   1741 			sc_xfer->sense.atapi_sense = wdc->sc_error;
   1742 		}
   1743 #ifdef ATAPI_DEBUG_WDC
   1744 		printf("wdc_atapi_intr: wdc_atapi_done(), error %d\n",
   1745 			sc_xfer->error);
   1746 #endif
   1747 		wdc_atapi_done(wdc, xfer);
   1748 		return 0;
   1749 	}
   1750 
   1751 
   1752 again:
   1753 	len = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_lo) +
   1754 	    256 * bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_cyl_hi);
   1755 
   1756 	st = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_status);
   1757 	err = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
   1758 	ire = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_ireason);
   1759 
   1760 	phase = (ire & (WDCI_CMD | WDCI_IN)) | (st & WDCS_DRQ);
   1761 #ifdef ATAPI_DEBUG_WDC
   1762 	printf("wdc_atapi_intr: len %d st %d err %d ire %d :",
   1763 	    len, st, err, ire);
   1764 #endif
   1765 	switch (phase) {
   1766 	case PHASE_CMDOUT:
   1767 		/* send packet command */
   1768 #ifdef ATAPI_DEBUG_WDC
   1769 		printf("PHASE_CMDOUT\n");
   1770 #endif
   1771 
   1772 #ifdef ATAPI_DEBUG_WDC
   1773 		{
   1774 			int i;
   1775 			char *c = (char *)sc_xfer->cmd;
   1776 			printf("wdc_atapi_intr: cmd ");
   1777 			for (i = 0; i < sc_xfer->cmdlen; i++)
   1778 				printf("%x ", c[i]);
   1779 			printf("\n");
   1780 		}
   1781 #endif
   1782 
   1783 		bus_space_write_multi_2(wdc->sc_iot, wdc->sc_ioh, wd_data,
   1784 		    (u_int16_t *)sc_xfer->cmd, sc_xfer->cmdlen >> 1);
   1785 		return 1;
   1786 
   1787 	case PHASE_DATAOUT:
   1788 		/* write data */
   1789 #ifdef ATAPI_DEBUG_WDC
   1790 		printf("PHASE_DATAOUT\n");
   1791 #endif
   1792 		if ((sc_xfer->flags & SCSI_DATA_OUT) == 0) {
   1793 			printf("wdc_atapi_intr: bad data phase\n");
   1794 			sc_xfer->error = XS_DRIVER_STUFFUP;
   1795 			return 0;
   1796 		}
   1797 		if (xfer->c_bcount < len) {
   1798 			printf("wdc_atapi_intr: warning: write only "
   1799 			    "%d of %d requested bytes\n", xfer->c_bcount, len);
   1800 			bus_space_write_multi_2(wdc->sc_iot, wdc->sc_ioh,
   1801 			    wd_data, xfer->databuf + xfer->c_skip,
   1802 			    xfer->c_bcount >> 1);
   1803 			for (i = xfer->c_bcount; i < len; i += 2)
   1804 				bus_space_write_2(wdc->sc_iot, wdc->sc_ioh,
   1805 				    wd_data, 0);
   1806 			xfer->c_skip += xfer->c_bcount;
   1807 			xfer->c_bcount = 0;
   1808 		} else {
   1809 			bus_space_write_multi_2(wdc->sc_iot, wdc->sc_ioh,
   1810 			    wd_data, xfer->databuf + xfer->c_skip, len >> 1);
   1811 			xfer->c_skip += len;
   1812 			xfer->c_bcount -= len;
   1813 		}
   1814 		return 1;
   1815 
   1816 	case PHASE_DATAIN:
   1817 		/* Read data */
   1818 #ifdef ATAPI_DEBUG_WDC
   1819 		printf("PHASE_DATAIN\n");
   1820 #endif
   1821 		if ((sc_xfer->flags & SCSI_DATA_IN) == 0) {
   1822 			printf("wdc_atapi_intr: bad data phase\n");
   1823 			sc_xfer->error = XS_DRIVER_STUFFUP;
   1824 			return 0;
   1825 		}
   1826 		if (xfer->c_bcount < len) {
   1827 			printf("wdc_atapi_intr: warning: reading only "
   1828 			    "%d of %d bytes\n", xfer->c_bcount, len);
   1829 			bus_space_read_multi_2(wdc->sc_iot, wdc->sc_ioh,
   1830 			    wd_data, xfer->databuf + xfer->c_skip,
   1831 			    xfer->c_bcount >> 1);
   1832 			wdcbit_bucket(wdc, len - xfer->c_bcount);
   1833 			xfer->c_skip += xfer->c_bcount;
   1834 			xfer->c_bcount = 0;
   1835 		} else {
   1836 			bus_space_read_multi_2(wdc->sc_iot, wdc->sc_ioh,
   1837 			    wd_data, xfer->databuf + xfer->c_skip, len >> 1);
   1838 			xfer->c_skip += len;
   1839 			xfer->c_bcount -=len;
   1840 		}
   1841 		return 1;
   1842 
   1843 	case PHASE_ABORTED:
   1844 	case PHASE_COMPLETED:
   1845 #ifdef ATAPI_DEBUG_WDC
   1846 		printf("PHASE_COMPLETED\n");
   1847 #endif
   1848 		if (st & WDCS_ERR) {
   1849 			sc_xfer->error = XS_SENSE;
   1850 			sc_xfer->sense.atapi_sense = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
   1851 		}
   1852 #ifdef ATAPI_DEBUG_WDC
   1853 		if (xfer->c_bcount != 0) {
   1854 			printf("wdc_atapi_intr warning: bcount value "
   1855 			    "is %d after io\n", xfer->c_bcount);
   1856 		}
   1857 #endif
   1858 		break;
   1859 
   1860 	default:
   1861         if (++retries<500) {
   1862             DELAY(100);
   1863             goto again;
   1864         }
   1865 		printf("wdc_atapi_intr: unknown phase %d\n", phase);
   1866 		if (st & WDCS_ERR) {
   1867 			sc_xfer->error = XS_SENSE;
   1868 			sc_xfer->sense.atapi_sense = bus_space_read_1(wdc->sc_iot, wdc->sc_ioh, wd_error);
   1869 		} else {
   1870 			sc_xfer->error = XS_DRIVER_STUFFUP;
   1871 		}
   1872 	}
   1873 
   1874 #ifdef ATAPI_DEBUG_WDC
   1875 		printf("wdc_atapi_intr: wdc_atapi_done() (end), error %d\n",
   1876 			sc_xfer->error);
   1877 #endif
   1878 	wdc_atapi_done(wdc, xfer);
   1879 	return (1);
   1880 }
   1881 
   1882 
   1883 void
   1884 wdc_atapi_done(wdc, xfer)
   1885 	struct wdc_softc *wdc;
   1886 	struct wdc_xfer *xfer;
   1887 {
   1888 	struct scsipi_xfer *sc_xfer = xfer->atapi_cmd;
   1889 	int s;
   1890 	int need_done =  xfer->c_flags & C_NEEDDONE;
   1891 
   1892 	if (wdc->sc_cap & WDC_CAPABILITY_HWLOCK)
   1893 		(*wdc->sc_free_hw)(wdc);
   1894 
   1895 #ifdef ATAPI_DEBUG
   1896 	printf("wdc_atapi_done: flags 0x%x\n", (u_int)xfer->c_flags);
   1897 #endif
   1898 	sc_xfer->resid = xfer->c_bcount;
   1899 	wdc->sc_flags &= ~WDCF_IRQ_WAIT;
   1900 
   1901 	/* remove this command from xfer queue */
   1902 	wdc->sc_errors = 0;
   1903 	xfer->c_skip = 0;
   1904 	if ((xfer->c_flags & SCSI_POLL) == 0) {
   1905 		s = splbio();
   1906 		untimeout(wdctimeout, wdc);
   1907 		TAILQ_REMOVE(&wdc->sc_xfer, xfer, c_xferchain);
   1908 		wdc->sc_flags &= ~(WDCF_SINGLE | WDCF_ERROR | WDCF_ACTIVE);
   1909 		wdc_free_xfer(xfer);
   1910 		sc_xfer->flags |= ITSDONE;
   1911 		if (need_done) {
   1912 #ifdef ATAPI_DEBUG
   1913 		printf("wdc_atapi_done: scsipi_done\n");
   1914 #endif
   1915 			scsipi_done(sc_xfer);
   1916 		}
   1917 #ifdef WDDEBUG
   1918 		printf("wdcstart from wdc_atapi_intr, flags 0x%x\n",
   1919 		    wdc->sc_flags);
   1920 #endif
   1921 		wdcstart(wdc);
   1922 	    splx(s);
   1923 	} else {
   1924 		wdc->sc_flags &= ~(WDCF_SINGLE | WDCF_ERROR | WDCF_ACTIVE);
   1925 		sc_xfer->flags |= ITSDONE;
   1926 	}
   1927 }
   1928 
   1929 #endif /* NATAPIBUS > 0 */
   1930