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