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