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