Home | History | Annotate | Line # | Download | only in ic
wdc.c revision 1.204
      1 /*	$NetBSD: wdc.c,v 1.204 2004/08/13 04:10:49 thorpej Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1998, 2001, 2003 Manuel Bouyer.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *  This product includes software developed by Manuel Bouyer.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
     34  * All rights reserved.
     35  *
     36  * This code is derived from software contributed to The NetBSD Foundation
     37  * by Charles M. Hannum, by Onno van der Linden and by Manuel Bouyer.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. All advertising materials mentioning features or use of this software
     48  *    must display the following acknowledgement:
     49  *        This product includes software developed by the NetBSD
     50  *        Foundation, Inc. and its contributors.
     51  * 4. Neither the name of The NetBSD Foundation nor the names of its
     52  *    contributors may be used to endorse or promote products derived
     53  *    from this software without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     56  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     57  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     58  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     59  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     60  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     61  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     62  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     63  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     64  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     65  * POSSIBILITY OF SUCH DAMAGE.
     66  */
     67 
     68 /*
     69  * CODE UNTESTED IN THE CURRENT REVISION:
     70  */
     71 
     72 #include <sys/cdefs.h>
     73 __KERNEL_RCSID(0, "$NetBSD: wdc.c,v 1.204 2004/08/13 04:10:49 thorpej Exp $");
     74 
     75 #ifndef ATADEBUG
     76 #define ATADEBUG
     77 #endif /* ATADEBUG */
     78 
     79 #include <sys/param.h>
     80 #include <sys/systm.h>
     81 #include <sys/kernel.h>
     82 #include <sys/conf.h>
     83 #include <sys/buf.h>
     84 #include <sys/device.h>
     85 #include <sys/malloc.h>
     86 #include <sys/syslog.h>
     87 #include <sys/proc.h>
     88 
     89 #include <machine/intr.h>
     90 #include <machine/bus.h>
     91 
     92 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
     93 #define bus_space_write_multi_stream_2	bus_space_write_multi_2
     94 #define bus_space_write_multi_stream_4	bus_space_write_multi_4
     95 #define bus_space_read_multi_stream_2	bus_space_read_multi_2
     96 #define bus_space_read_multi_stream_4	bus_space_read_multi_4
     97 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
     98 
     99 #include <dev/ata/atavar.h>
    100 #include <dev/ata/atareg.h>
    101 #include <dev/ic/wdcreg.h>
    102 #include <dev/ic/wdcvar.h>
    103 
    104 #include "locators.h"
    105 
    106 #include "ataraid.h"
    107 #include "atapibus.h"
    108 #include "wd.h"
    109 
    110 #if NATARAID > 0
    111 #include <dev/ata/ata_raidvar.h>
    112 #endif
    113 
    114 #define WDCDELAY  100 /* 100 microseconds */
    115 #define WDCNDELAY_RST (WDC_RESET_WAIT * 1000 / WDCDELAY)
    116 #if 0
    117 /* If you enable this, it will report any delays more than WDCDELAY * N long. */
    118 #define WDCNDELAY_DEBUG	50
    119 #endif
    120 
    121 /* When polling wait that much and then tsleep for 1/hz seconds */
    122 #define WDCDELAY_POLL 1 /* ms */
    123 
    124 /* timeout for the control commands */
    125 #define WDC_CTRL_DELAY 10000 /* 10s, for the recall command */
    126 
    127 #if NWD > 0
    128 extern const struct ata_bustype wdc_ata_bustype; /* in ata_wdc.c */
    129 #else
    130 /* A fake one, the autoconfig will print "wd at foo ... not configured */
    131 const struct ata_bustype wdc_ata_bustype = {
    132 	SCSIPI_BUSTYPE_ATA,
    133 	NULL,
    134 	NULL,
    135 	NULL,
    136 	NULL,
    137 	NULL,
    138 	NULL,
    139 	NULL
    140 };
    141 #endif
    142 
    143 static int	wdcprobe1(struct wdc_channel*, int);
    144 static void	__wdcerror(struct wdc_channel*, char *);
    145 static int	__wdcwait_reset(struct wdc_channel *, int, int);
    146 static void	__wdccommand_done(struct wdc_channel *, struct ata_xfer *);
    147 static void	__wdccommand_done_end(struct wdc_channel *, struct ata_xfer *);
    148 static void	__wdccommand_kill_xfer(struct wdc_channel *,
    149 			               struct ata_xfer *, int);
    150 static void	__wdccommand_start(struct wdc_channel *, struct ata_xfer *);
    151 static int	__wdccommand_intr(struct wdc_channel *, struct ata_xfer *, int);
    152 static int	__wdcwait(struct wdc_channel *, int, int, int);
    153 
    154 #define DEBUG_INTR   0x01
    155 #define DEBUG_XFERS  0x02
    156 #define DEBUG_STATUS 0x04
    157 #define DEBUG_FUNCS  0x08
    158 #define DEBUG_PROBE  0x10
    159 #define DEBUG_DETACH 0x20
    160 #define DEBUG_DELAY  0x40
    161 #ifdef ATADEBUG
    162 extern int atadebug_mask; /* init'ed in ata.c */
    163 int wdc_nxfer = 0;
    164 #define ATADEBUG_PRINT(args, level)  if (atadebug_mask & (level)) printf args
    165 #else
    166 #define ATADEBUG_PRINT(args, level)
    167 #endif
    168 
    169 /*
    170  * A queue of atabus instances, used to ensure the same bus probe order
    171  * for a given hardware configuration at each boot.
    172  */
    173 struct atabus_initq_head atabus_initq_head =
    174     TAILQ_HEAD_INITIALIZER(atabus_initq_head);
    175 struct simplelock atabus_interlock = SIMPLELOCK_INITIALIZER;
    176 
    177 /*
    178  * Initialize the "shadow register" handles for a standard wdc controller.
    179  */
    180 void
    181 wdc_init_shadow_regs(struct wdc_channel *chp)
    182 {
    183 
    184 	chp->cmd_iohs[wd_status] = chp->cmd_iohs[wd_command];
    185 	chp->cmd_iohs[wd_features] = chp->cmd_iohs[wd_error];
    186 }
    187 
    188 /* Test to see controller with at last one attached drive is there.
    189  * Returns a bit for each possible drive found (0x01 for drive 0,
    190  * 0x02 for drive 1).
    191  * Logic:
    192  * - If a status register is at 0xff, assume there is no drive here
    193  *   (ISA has pull-up resistors).  Similarly if the status register has
    194  *   the value we last wrote to the bus (for IDE interfaces without pullups).
    195  *   If no drive at all -> return.
    196  * - reset the controller, wait for it to complete (may take up to 31s !).
    197  *   If timeout -> return.
    198  * - test ATA/ATAPI signatures. If at last one drive found -> return.
    199  * - try an ATA command on the master.
    200  */
    201 
    202 static void
    203 wdc_drvprobe(struct wdc_channel *chp)
    204 {
    205 	struct ataparams params;
    206 	struct wdc_softc *wdc = chp->ch_wdc;
    207 	u_int8_t st0 = 0, st1 = 0;
    208 	int i, error;
    209 
    210 	if (wdcprobe1(chp, 0) == 0) {
    211 		/* No drives, abort the attach here. */
    212 		return;
    213 	}
    214 
    215 	/* for ATA/OLD drives, wait for DRDY, 3s timeout */
    216 	for (i = 0; i < mstohz(3000); i++) {
    217 		if (chp->ch_drive[0].drive_flags & (DRIVE_ATA|DRIVE_OLD)) {
    218 			if (wdc != NULL && wdc->select)
    219 				wdc->select(chp,0);
    220 			bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh],
    221 			    0, WDSD_IBM);
    222 			delay(10);	/* 400ns delay */
    223 			st0 = bus_space_read_1(chp->cmd_iot,
    224 			    chp->cmd_iohs[wd_status], 0);
    225 		}
    226 
    227 		if (chp->ch_drive[1].drive_flags & (DRIVE_ATA|DRIVE_OLD)) {
    228 			if (wdc != NULL && wdc->select)
    229 				wdc->select(chp,1);
    230 			bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh],
    231 			    0, WDSD_IBM | 0x10);
    232 			delay(10);	/* 400ns delay */
    233 			st1 = bus_space_read_1(chp->cmd_iot,
    234 			    chp->cmd_iohs[wd_status], 0);
    235 		}
    236 
    237 		if (((chp->ch_drive[0].drive_flags & (DRIVE_ATA|DRIVE_OLD))
    238 			== 0 ||
    239 		    (st0 & WDCS_DRDY)) &&
    240 		    ((chp->ch_drive[1].drive_flags & (DRIVE_ATA|DRIVE_OLD))
    241 			== 0 ||
    242 		    (st1 & WDCS_DRDY)))
    243 			break;
    244 		tsleep(&params, PRIBIO, "atadrdy", 1);
    245 	}
    246 	if ((st0 & WDCS_DRDY) == 0)
    247 		chp->ch_drive[0].drive_flags &= ~(DRIVE_ATA|DRIVE_OLD);
    248 	if ((st1 & WDCS_DRDY) == 0)
    249 		chp->ch_drive[1].drive_flags &= ~(DRIVE_ATA|DRIVE_OLD);
    250 
    251 	ATADEBUG_PRINT(("%s:%d: wait DRDY st0 0x%x st1 0x%x\n",
    252 	    wdc->sc_dev.dv_xname,
    253 	    chp->ch_channel, st0, st1), DEBUG_PROBE);
    254 
    255 	/* Wait a bit, some devices are weird just after a reset. */
    256 	delay(5000);
    257 
    258 	for (i = 0; i < 2; i++) {
    259 		/* XXX This should be done by other code. */
    260 		chp->ch_drive[i].chnl_softc = chp;
    261 		chp->ch_drive[i].drive = i;
    262 
    263 		/*
    264 		 * Init error counter so that an error withing the first xfers
    265 		 * will trigger a downgrade
    266 		 */
    267 		chp->ch_drive[i].n_dmaerrs = NERRS_MAX-1;
    268 
    269 		/* If controller can't do 16bit flag the drives as 32bit */
    270 		if ((wdc->cap &
    271 		    (WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32)) ==
    272 		    WDC_CAPABILITY_DATA32)
    273 			chp->ch_drive[i].drive_flags |= DRIVE_CAP32;
    274 		if ((chp->ch_drive[i].drive_flags & DRIVE) == 0)
    275 			continue;
    276 
    277 		/* Shortcut in case we've been shutdown */
    278 		if (chp->ch_flags & WDCF_SHUTDOWN)
    279 			return;
    280 
    281 		/* issue an identify, to try to detect ghosts */
    282 		error = ata_get_params(&chp->ch_drive[i],
    283 		    AT_WAIT | AT_POLL, &params);
    284 		if (error != CMD_OK) {
    285 			tsleep(&params, PRIBIO, "atacnf", mstohz(1000));
    286 
    287 			/* Shortcut in case we've been shutdown */
    288 			if (chp->ch_flags & WDCF_SHUTDOWN)
    289 				return;
    290 
    291 			error = ata_get_params(&chp->ch_drive[i],
    292 			    AT_WAIT | AT_POLL, &params);
    293 		}
    294 		if (error == CMD_OK) {
    295 			/* If IDENTIFY succeeded, this is not an OLD ctrl */
    296 			chp->ch_drive[0].drive_flags &= ~DRIVE_OLD;
    297 			chp->ch_drive[1].drive_flags &= ~DRIVE_OLD;
    298 		} else {
    299 			chp->ch_drive[i].drive_flags &=
    300 			    ~(DRIVE_ATA | DRIVE_ATAPI);
    301 			ATADEBUG_PRINT(("%s:%d:%d: IDENTIFY failed (%d)\n",
    302 			    wdc->sc_dev.dv_xname,
    303 			    chp->ch_channel, i, error), DEBUG_PROBE);
    304 			if ((chp->ch_drive[i].drive_flags & DRIVE_OLD) == 0)
    305 				continue;
    306 			/*
    307 			 * Pre-ATA drive ?
    308 			 * Test registers writability (Error register not
    309 			 * writable, but cyllo is), then try an ATA command.
    310 			 */
    311 			if (wdc->select)
    312 				wdc->select(chp,i);
    313 			bus_space_write_1(chp->cmd_iot,
    314 			    chp->cmd_iohs[wd_sdh], 0, WDSD_IBM | (i << 4));
    315 			delay(10);	/* 400ns delay */
    316 			bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_error],
    317 			    0, 0x58);
    318 			bus_space_write_1(chp->cmd_iot,
    319 			    chp->cmd_iohs[wd_cyl_lo], 0, 0xa5);
    320 			if (bus_space_read_1(chp->cmd_iot,
    321 				chp->cmd_iohs[wd_error], 0) == 0x58 ||
    322 			    bus_space_read_1(chp->cmd_iot,
    323 				chp->cmd_iohs[wd_cyl_lo], 0) != 0xa5) {
    324 				ATADEBUG_PRINT(("%s:%d:%d: register "
    325 				    "writability failed\n",
    326 				    wdc->sc_dev.dv_xname,
    327 				    chp->ch_channel, i), DEBUG_PROBE);
    328 				    chp->ch_drive[i].drive_flags &= ~DRIVE_OLD;
    329 				    continue;
    330 			}
    331 			if (wdc_wait_for_ready(chp, 10000, 0) == WDCWAIT_TOUT) {
    332 				ATADEBUG_PRINT(("%s:%d:%d: not ready\n",
    333 				    wdc->sc_dev.dv_xname,
    334 				    chp->ch_channel, i), DEBUG_PROBE);
    335 				chp->ch_drive[i].drive_flags &= ~DRIVE_OLD;
    336 				continue;
    337 			}
    338 			bus_space_write_1(chp->cmd_iot,
    339 			    chp->cmd_iohs[wd_command], 0, WDCC_RECAL);
    340 			delay(10);	/* 400ns delay */
    341 			if (wdc_wait_for_ready(chp, 10000, 0) == WDCWAIT_TOUT) {
    342 				ATADEBUG_PRINT(("%s:%d:%d: WDCC_RECAL failed\n",
    343 				    wdc->sc_dev.dv_xname,
    344 				    chp->ch_channel, i), DEBUG_PROBE);
    345 				chp->ch_drive[i].drive_flags &= ~DRIVE_OLD;
    346 			} else {
    347 				chp->ch_drive[0].drive_flags &=
    348 				    ~(DRIVE_ATA | DRIVE_ATAPI);
    349 				chp->ch_drive[1].drive_flags &=
    350 				    ~(DRIVE_ATA | DRIVE_ATAPI);
    351 			}
    352 		}
    353 	}
    354 }
    355 
    356 void
    357 atabusconfig(struct atabus_softc *atabus_sc)
    358 {
    359 	struct wdc_channel *chp = atabus_sc->sc_chan;
    360 	struct wdc_softc *wdc = chp->ch_wdc;
    361 	int i;
    362 	struct atabus_initq *atabus_initq = NULL;
    363 
    364 	/* Probe for the drives. */
    365 	(*wdc->drv_probe)(chp);
    366 
    367 	ATADEBUG_PRINT(("atabusattach: ch_drive_flags 0x%x 0x%x\n",
    368 	    chp->ch_drive[0].drive_flags, chp->ch_drive[1].drive_flags),
    369 	    DEBUG_PROBE);
    370 
    371 	/* If no drives, abort here */
    372 	if ((chp->ch_drive[0].drive_flags & DRIVE) == 0 &&
    373 	    (chp->ch_drive[1].drive_flags & DRIVE) == 0)
    374 		goto out;
    375 
    376 	/* Shortcut in case we've been shutdown */
    377 	if (chp->ch_flags & WDCF_SHUTDOWN)
    378 		goto out;
    379 
    380 	/* Make sure the devices probe in atabus order to avoid jitter. */
    381 	simple_lock(&atabus_interlock);
    382 	while(1) {
    383 		atabus_initq = TAILQ_FIRST(&atabus_initq_head);
    384 		if (atabus_initq->atabus_sc == atabus_sc)
    385 			break;
    386 		ltsleep(&atabus_initq_head, PRIBIO, "ata_initq", 0,
    387 		    &atabus_interlock);
    388 	}
    389 	simple_unlock(&atabus_interlock);
    390 
    391 	/*
    392 	 * Attach an ATAPI bus, if needed.
    393 	 */
    394 	if ((chp->ch_drive[0].drive_flags & DRIVE_ATAPI) ||
    395 	    (chp->ch_drive[1].drive_flags & DRIVE_ATAPI)) {
    396 #if NATAPIBUS > 0
    397 		wdc_atapibus_attach(atabus_sc);
    398 #else
    399 		/*
    400 		 * Fake the autoconfig "not configured" message
    401 		 */
    402 		aprint_normal("atapibus at %s not configured\n",
    403 		    wdc->sc_dev.dv_xname);
    404 		chp->atapibus = NULL;
    405 		chp->ch_drive[0].drive_flags &= ~DRIVE_ATAPI;
    406 		chp->ch_drive[1].drive_flags &= ~DRIVE_ATAPI;
    407 #endif
    408 	}
    409 
    410 	for (i = 0; i < 2; i++) {
    411 		struct ata_device adev;
    412 		if ((chp->ch_drive[i].drive_flags &
    413 		    (DRIVE_ATA | DRIVE_OLD)) == 0) {
    414 			continue;
    415 		}
    416 		memset(&adev, 0, sizeof(struct ata_device));
    417 		adev.adev_bustype = &wdc_ata_bustype;
    418 		adev.adev_channel = chp->ch_channel;
    419 		adev.adev_openings = 1;
    420 		adev.adev_drv_data = &chp->ch_drive[i];
    421 		chp->ata_drives[i] = config_found(&atabus_sc->sc_dev,
    422 		    &adev, ataprint);
    423 		if (chp->ata_drives[i] != NULL)
    424 			ata_probe_caps(&chp->ch_drive[i]);
    425 		else
    426 			chp->ch_drive[i].drive_flags &=
    427 			    ~(DRIVE_ATA | DRIVE_OLD);
    428 	}
    429 
    430 	/* now that we know the drives, the controller can set its modes */
    431 	if (wdc->set_modes) {
    432 		wdc->set_modes(chp);
    433 		ata_print_modes(chp);
    434 	}
    435 #if NATARAID > 0
    436 	if (wdc->cap & WDC_CAPABILITY_RAID)
    437 		for (i = 0; i < 2; i++)
    438 			if (chp->ata_drives[i] != NULL)
    439 				ata_raid_check_component(chp->ata_drives[i]);
    440 #endif /* NATARAID > 0 */
    441 
    442 	/*
    443 	 * reset drive_flags for unattached devices, reset state for attached
    444 	 *  ones
    445 	 */
    446 	for (i = 0; i < 2; i++) {
    447 		if (chp->ch_drive[i].drv_softc == NULL)
    448 			chp->ch_drive[i].drive_flags = 0;
    449 		else
    450 			chp->ch_drive[i].state = 0;
    451 	}
    452 
    453  out:
    454 	if (atabus_initq == NULL) {
    455 		simple_lock(&atabus_interlock);
    456 		while(1) {
    457 			atabus_initq = TAILQ_FIRST(&atabus_initq_head);
    458 			if (atabus_initq->atabus_sc == atabus_sc)
    459 				break;
    460 			ltsleep(&atabus_initq_head, PRIBIO, "ata_initq", 0,
    461 			    &atabus_interlock);
    462 		}
    463 		simple_unlock(&atabus_interlock);
    464 	}
    465         simple_lock(&atabus_interlock);
    466         TAILQ_REMOVE(&atabus_initq_head, atabus_initq, atabus_initq);
    467         simple_unlock(&atabus_interlock);
    468 
    469         free(atabus_initq, M_DEVBUF);
    470         wakeup(&atabus_initq_head);
    471 
    472 	ata_delref(chp);
    473 
    474 	config_pending_decr();
    475 }
    476 
    477 int
    478 wdcprobe(struct wdc_channel *chp)
    479 {
    480 
    481 	return (wdcprobe1(chp, 1));
    482 }
    483 
    484 static int
    485 wdcprobe1(struct wdc_channel *chp, int poll)
    486 {
    487 	struct wdc_softc *wdc = chp->ch_wdc;
    488 	u_int8_t st0, st1, sc, sn, cl, ch;
    489 	u_int8_t ret_value = 0x03;
    490 	u_int8_t drive;
    491 	int s;
    492 
    493 	/*
    494 	 * Sanity check to see if the wdc channel responds at all.
    495 	 */
    496 
    497 	s = splbio();
    498 	if (wdc == NULL ||
    499 	    (wdc->cap & WDC_CAPABILITY_NO_EXTRA_RESETS) == 0) {
    500 
    501 		if (wdc != NULL && wdc->select)
    502 			wdc->select(chp,0);
    503 
    504 		bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0,
    505 		    WDSD_IBM);
    506 		delay(10);	/* 400ns delay */
    507 		st0 = bus_space_read_1(chp->cmd_iot,
    508 		    chp->cmd_iohs[wd_status], 0);
    509 
    510 		if (wdc != NULL && wdc->select)
    511 			wdc->select(chp,1);
    512 
    513 		bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0,
    514 		    WDSD_IBM | 0x10);
    515 		delay(10);	/* 400ns delay */
    516 		st1 = bus_space_read_1(chp->cmd_iot,
    517 		    chp->cmd_iohs[wd_status], 0);
    518 
    519 		ATADEBUG_PRINT(("%s:%d: before reset, st0=0x%x, st1=0x%x\n",
    520 		    wdc != NULL ? wdc->sc_dev.dv_xname : "wdcprobe",
    521 		    chp->ch_channel, st0, st1), DEBUG_PROBE);
    522 
    523 		if (st0 == 0xff || st0 == WDSD_IBM)
    524 			ret_value &= ~0x01;
    525 		if (st1 == 0xff || st1 == (WDSD_IBM | 0x10))
    526 			ret_value &= ~0x02;
    527 		/* Register writability test, drive 0. */
    528 		if (ret_value & 0x01) {
    529 			if (wdc != NULL && wdc->select)
    530 				wdc->select(chp,0);
    531 			bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh],
    532 			    0, WDSD_IBM);
    533 			bus_space_write_1(chp->cmd_iot,
    534 			    chp->cmd_iohs[wd_cyl_lo], 0, 0x02);
    535 			cl = bus_space_read_1(chp->cmd_iot,
    536 			    chp->cmd_iohs[wd_cyl_lo], 0);
    537 			if (cl != 0x02) {
    538 				ATADEBUG_PRINT(("%s:%d drive 0 wd_cyl_lo: "
    539 				    "got 0x%x != 0x02\n",
    540 				    wdc != NULL ?
    541 				    wdc->sc_dev.dv_xname : "wdcprobe",
    542 				    chp->ch_channel, cl),
    543 				    DEBUG_PROBE);
    544 				ret_value &= ~0x01;
    545 			}
    546 			bus_space_write_1(chp->cmd_iot,
    547 			    chp->cmd_iohs[wd_cyl_lo], 0, 0x01);
    548 			cl = bus_space_read_1(chp->cmd_iot,
    549 			    chp->cmd_iohs[wd_cyl_lo], 0);
    550 			if (cl != 0x01) {
    551 				ATADEBUG_PRINT(("%s:%d drive 0 wd_cyl_lo: "
    552 				    "got 0x%x != 0x01\n",
    553 				    wdc != NULL ?
    554 				    wdc->sc_dev.dv_xname : "wdcprobe",
    555 				    chp->ch_channel, cl),
    556 				    DEBUG_PROBE);
    557 				ret_value &= ~0x01;
    558 			}
    559 			bus_space_write_1(chp->cmd_iot,
    560 			    chp->cmd_iohs[wd_sector], 0, 0x01);
    561 			cl = bus_space_read_1(chp->cmd_iot,
    562 			    chp->cmd_iohs[wd_sector], 0);
    563 			if (cl != 0x01) {
    564 				ATADEBUG_PRINT(("%s:%d drive 0 wd_sector: "
    565 				    "got 0x%x != 0x01\n",
    566 				    wdc != NULL ?
    567 				    wdc->sc_dev.dv_xname : "wdcprobe",
    568 				    chp->ch_channel, cl),
    569 				    DEBUG_PROBE);
    570 				ret_value &= ~0x01;
    571 			}
    572 			bus_space_write_1(chp->cmd_iot,
    573 			    chp->cmd_iohs[wd_sector], 0, 0x02);
    574 			cl = bus_space_read_1(chp->cmd_iot,
    575 			    chp->cmd_iohs[wd_sector], 0);
    576 			if (cl != 0x02) {
    577 				ATADEBUG_PRINT(("%s:%d drive 0 wd_sector: "
    578 				    "got 0x%x != 0x02\n",
    579 				    wdc != NULL ?
    580 				    wdc->sc_dev.dv_xname : "wdcprobe",
    581 				    chp->ch_channel, cl),
    582 				    DEBUG_PROBE);
    583 				ret_value &= ~0x01;
    584 			}
    585 			cl = bus_space_read_1(chp->cmd_iot,
    586 			    chp->cmd_iohs[wd_cyl_lo], 0);
    587 			if (cl != 0x01) {
    588 				ATADEBUG_PRINT(("%s:%d drive 0 wd_cyl_lo(2): "
    589 				    "got 0x%x != 0x01\n",
    590 				    wdc != NULL ?
    591 				    wdc->sc_dev.dv_xname : "wdcprobe",
    592 				    chp->ch_channel, cl),
    593 				    DEBUG_PROBE);
    594 				ret_value &= ~0x01;
    595 			}
    596 		}
    597 		/* Register writability test, drive 1. */
    598 		if (ret_value & 0x02) {
    599 			if (wdc != NULL && wdc->select)
    600 			     wdc->select(chp,1);
    601 			bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh],
    602 			     0, WDSD_IBM | 0x10);
    603 			bus_space_write_1(chp->cmd_iot,
    604 			    chp->cmd_iohs[wd_cyl_lo], 0, 0x02);
    605 			cl = bus_space_read_1(chp->cmd_iot,
    606 			    chp->cmd_iohs[wd_cyl_lo], 0);
    607 			if (cl != 0x02) {
    608 				ATADEBUG_PRINT(("%s:%d drive 1 wd_cyl_lo: "
    609 				    "got 0x%x != 0x02\n",
    610 				    wdc != NULL ?
    611 				    wdc->sc_dev.dv_xname : "wdcprobe",
    612 				    chp->ch_channel, cl),
    613 				    DEBUG_PROBE);
    614 				ret_value &= ~0x02;
    615 			}
    616 			bus_space_write_1(chp->cmd_iot,
    617 			    chp->cmd_iohs[wd_cyl_lo], 0, 0x01);
    618 			cl = bus_space_read_1(chp->cmd_iot,
    619 			    chp->cmd_iohs[wd_cyl_lo], 0);
    620 			if (cl != 0x01) {
    621 				ATADEBUG_PRINT(("%s:%d drive 1 wd_cyl_lo: "
    622 				    "got 0x%x != 0x01\n",
    623 				    wdc != NULL ?
    624 				    wdc->sc_dev.dv_xname : "wdcprobe",
    625 				    chp->ch_channel, cl),
    626 				    DEBUG_PROBE);
    627 				ret_value &= ~0x02;
    628 			}
    629 			bus_space_write_1(chp->cmd_iot,
    630 			    chp->cmd_iohs[wd_sector], 0, 0x01);
    631 			cl = bus_space_read_1(chp->cmd_iot,
    632 			    chp->cmd_iohs[wd_sector], 0);
    633 			if (cl != 0x01) {
    634 				ATADEBUG_PRINT(("%s:%d drive 1 wd_sector: "
    635 				    "got 0x%x != 0x01\n",
    636 				    wdc != NULL ?
    637 				    wdc->sc_dev.dv_xname : "wdcprobe",
    638 				    chp->ch_channel, cl),
    639 				    DEBUG_PROBE);
    640 				ret_value &= ~0x02;
    641 			}
    642 			bus_space_write_1(chp->cmd_iot,
    643 			    chp->cmd_iohs[wd_sector], 0, 0x02);
    644 			cl = bus_space_read_1(chp->cmd_iot,
    645 			    chp->cmd_iohs[wd_sector], 0);
    646 			if (cl != 0x02) {
    647 				ATADEBUG_PRINT(("%s:%d drive 1 wd_sector: "
    648 				    "got 0x%x != 0x02\n",
    649 				    wdc != NULL ?
    650 				    wdc->sc_dev.dv_xname : "wdcprobe",
    651 				    chp->ch_channel, cl),
    652 				    DEBUG_PROBE);
    653 				ret_value &= ~0x02;
    654 			}
    655 			cl = bus_space_read_1(chp->cmd_iot,
    656 			    chp->cmd_iohs[wd_cyl_lo], 0);
    657 			if (cl != 0x01) {
    658 				ATADEBUG_PRINT(("%s:%d drive 1 wd_cyl_lo(2): "
    659 				    "got 0x%x != 0x01\n",
    660 				    wdc != NULL ?
    661 				    wdc->sc_dev.dv_xname : "wdcprobe",
    662 				    chp->ch_channel, cl),
    663 				    DEBUG_PROBE);
    664 				ret_value &= ~0x02;
    665 			}
    666 		}
    667 
    668 		if (ret_value == 0) {
    669 			splx(s);
    670 			return 0;
    671 		}
    672 	}
    673 
    674 
    675 #if 0 /* XXX this break some ATA or ATAPI devices */
    676 	/*
    677 	 * reset bus. Also send an ATAPI_RESET to devices, in case there are
    678 	 * ATAPI device out there which don't react to the bus reset
    679 	 */
    680 	if (ret_value & 0x01) {
    681 		if (wdc != NULL && wdc->select)
    682 			wdc->select(chp,0);
    683 		bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh],
    684 		     0, WDSD_IBM);
    685 		bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_command], 0,
    686 		    ATAPI_SOFT_RESET);
    687 	}
    688 	if (ret_value & 0x02) {
    689 		if (wdc != NULL && wdc->select)
    690 			wdc->select(chp,0);
    691 		bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh],
    692 		     0, WDSD_IBM | 0x10);
    693 		bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_command], 0,
    694 		    ATAPI_SOFT_RESET);
    695 	}
    696 
    697 	delay(5000);
    698 #endif
    699 
    700 	if (wdc != NULL && wdc->select)
    701 		wdc->select(chp,0);
    702 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0, WDSD_IBM);
    703 	delay(10);	/* 400ns delay */
    704 	/* assert SRST, wait for reset to complete */
    705 	bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
    706 	    WDCTL_RST | WDCTL_IDS | WDCTL_4BIT);
    707 	DELAY(1000);
    708 	bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
    709 	    WDCTL_IDS | WDCTL_4BIT);
    710 	DELAY(2000);
    711 	(void) bus_space_read_1(chp->cmd_iot, chp->cmd_iohs[wd_error], 0);
    712 	bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT);
    713 	delay(10);	/* 400ns delay */
    714 	/* ACK interrupt in case there is one pending left (Promise ATA100) */
    715 	if (wdc != NULL && wdc->irqack != NULL)
    716 		wdc->irqack(chp);
    717 	splx(s);
    718 
    719 	ret_value = __wdcwait_reset(chp, ret_value, poll);
    720 	ATADEBUG_PRINT(("%s:%d: after reset, ret_value=0x%d\n",
    721 	    wdc != NULL ? wdc->sc_dev.dv_xname : "wdcprobe", chp->ch_channel,
    722 	    ret_value), DEBUG_PROBE);
    723 
    724 	/* if reset failed, there's nothing here */
    725 	if (ret_value == 0)
    726 		return 0;
    727 
    728 	/*
    729 	 * Test presence of drives. First test register signatures looking
    730 	 * for ATAPI devices. If it's not an ATAPI and reset said there may
    731 	 * be something here assume it's ATA or OLD.  Ghost will be killed
    732 	 * later in attach routine.
    733 	 */
    734 	for (drive = 0; drive < 2; drive++) {
    735 		if ((ret_value & (0x01 << drive)) == 0)
    736 			continue;
    737 		if (wdc != NULL && wdc->select)
    738 			wdc->select(chp,drive);
    739 		bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0,
    740 		    WDSD_IBM | (drive << 4));
    741 		delay(10);	/* 400ns delay */
    742 		/* Save registers contents */
    743 		sc = bus_space_read_1(chp->cmd_iot,
    744 		    chp->cmd_iohs[wd_seccnt], 0);
    745 		sn = bus_space_read_1(chp->cmd_iot,
    746 		    chp->cmd_iohs[wd_sector], 0);
    747 		cl = bus_space_read_1(chp->cmd_iot,
    748 		    chp->cmd_iohs[wd_cyl_lo], 0);
    749 		ch = bus_space_read_1(chp->cmd_iot,
    750 		     chp->cmd_iohs[wd_cyl_hi], 0);
    751 
    752 		ATADEBUG_PRINT(("%s:%d:%d: after reset, sc=0x%x sn=0x%x "
    753 		    "cl=0x%x ch=0x%x\n",
    754 		    wdc != NULL ? wdc->sc_dev.dv_xname : "wdcprobe",
    755 	    	    chp->ch_channel, drive, sc, sn, cl, ch), DEBUG_PROBE);
    756 		/*
    757 		 * sc & sn are supposted to be 0x1 for ATAPI but in some cases
    758 		 * we get wrong values here, so ignore it.
    759 		 */
    760 		if (cl == 0x14 && ch == 0xeb) {
    761 			chp->ch_drive[drive].drive_flags |= DRIVE_ATAPI;
    762 		} else {
    763 			chp->ch_drive[drive].drive_flags |= DRIVE_ATA;
    764 			if (wdc == NULL ||
    765 			    (wdc->cap & WDC_CAPABILITY_PREATA) != 0)
    766 				chp->ch_drive[drive].drive_flags |= DRIVE_OLD;
    767 		}
    768 	}
    769 	return (ret_value);
    770 }
    771 
    772 void
    773 wdcattach(struct wdc_channel *chp)
    774 {
    775 	struct wdc_softc *wdc = chp->ch_wdc;
    776 
    777 	if (chp->ch_flags & WDCF_DISABLED)
    778 		return;
    779 
    780 	/* default data transfer methods */
    781 	if (!wdc->datain_pio)
    782 		wdc->datain_pio = wdc_datain_pio;
    783 	if (!wdc->dataout_pio)
    784 		wdc->dataout_pio = wdc_dataout_pio;
    785 
    786 	/* initialise global data */
    787 	callout_init(&chp->ch_callout);
    788 	if (wdc->drv_probe == NULL)
    789 		wdc->drv_probe = wdc_drvprobe;
    790 
    791 	TAILQ_INIT(&chp->ch_queue->queue_xfer);
    792 	chp->ch_queue->queue_freeze = 0;
    793 	chp->ch_queue->active_xfer = NULL;
    794 
    795 	chp->atabus = config_found(&wdc->sc_dev, chp, atabusprint);
    796 }
    797 
    798 int
    799 wdcactivate(struct device *self, enum devact act)
    800 {
    801 	struct wdc_softc *wdc = (struct wdc_softc *)self;
    802 	int s, i, error = 0;
    803 
    804 	s = splbio();
    805 	switch (act) {
    806 	case DVACT_ACTIVATE:
    807 		error = EOPNOTSUPP;
    808 		break;
    809 
    810 	case DVACT_DEACTIVATE:
    811 		for (i = 0; i < wdc->nchannels; i++) {
    812 			error = config_deactivate(wdc->channels[i]->atabus);
    813 			if (error)
    814 				break;
    815 		}
    816 		break;
    817 	}
    818 	splx(s);
    819 	return (error);
    820 }
    821 
    822 int
    823 wdcdetach(struct device *self, int flags)
    824 {
    825 	struct wdc_softc *wdc = (struct wdc_softc *)self;
    826 	struct wdc_channel *chp;
    827 	struct scsipi_adapter *adapt = &wdc->sc_atapi_adapter._generic;
    828 	int i, error = 0;
    829 
    830 	for (i = 0; i < wdc->nchannels; i++) {
    831 		chp = wdc->channels[i];
    832 		ATADEBUG_PRINT(("wdcdetach: %s: detaching %s\n",
    833 		    wdc->sc_dev.dv_xname, chp->atabus->dv_xname), DEBUG_DETACH);
    834 		error = config_detach(chp->atabus, flags);
    835 		if (error)
    836 			break;
    837 	}
    838 	if (adapt->adapt_refcnt != 0) {
    839 #ifdef DIAGNOSTIC
    840 		printf("wdcdetach: refcnt should be 0 here??\n");
    841 #endif
    842 		(void) (*adapt->adapt_enable)(&wdc->sc_dev, 0);
    843 	}
    844 	return (error);
    845 }
    846 
    847 /* restart an interrupted I/O */
    848 void
    849 wdcrestart(void *v)
    850 {
    851 	struct wdc_channel *chp = v;
    852 	int s;
    853 
    854 	s = splbio();
    855 	atastart(chp);
    856 	splx(s);
    857 }
    858 
    859 
    860 /*
    861  * Interrupt routine for the controller.  Acknowledge the interrupt, check for
    862  * errors on the current operation, mark it done if necessary, and start the
    863  * next request.  Also check for a partially done transfer, and continue with
    864  * the next chunk if so.
    865  */
    866 int
    867 wdcintr(void *arg)
    868 {
    869 	struct wdc_channel *chp = arg;
    870 	struct wdc_softc *wdc = chp->ch_wdc;
    871 	struct ata_xfer *xfer;
    872 	int ret;
    873 
    874 	if ((wdc->sc_dev.dv_flags & DVF_ACTIVE) == 0) {
    875 		ATADEBUG_PRINT(("wdcintr: deactivated controller\n"),
    876 		    DEBUG_INTR);
    877 		return (0);
    878 	}
    879 	if ((chp->ch_flags & WDCF_IRQ_WAIT) == 0) {
    880 		ATADEBUG_PRINT(("wdcintr: inactive controller\n"), DEBUG_INTR);
    881 		/* try to clear the pending interrupt anyway */
    882 		(void)bus_space_read_1(chp->cmd_iot,
    883 		    chp->cmd_iohs[wd_status], 0);
    884 		return (0);
    885 	}
    886 
    887 	ATADEBUG_PRINT(("wdcintr\n"), DEBUG_INTR);
    888 	xfer = chp->ch_queue->active_xfer;
    889 #ifdef DIAGNOSTIC
    890 	if (xfer == NULL)
    891 		panic("wdcintr: no xfer");
    892 #endif
    893 	if (chp->ch_flags & WDCF_DMA_WAIT) {
    894 		wdc->dma_status =
    895 		    (*wdc->dma_finish)(wdc->dma_arg, chp->ch_channel,
    896 			xfer->c_drive, WDC_DMAEND_END);
    897 		if (wdc->dma_status & WDC_DMAST_NOIRQ) {
    898 			/* IRQ not for us, not detected by DMA engine */
    899 			return 0;
    900 		}
    901 		chp->ch_flags &= ~WDCF_DMA_WAIT;
    902 	}
    903 	chp->ch_flags &= ~WDCF_IRQ_WAIT;
    904 	ret = xfer->c_intr(chp, xfer, 1);
    905 	if (ret == 0) /* irq was not for us, still waiting for irq */
    906 		chp->ch_flags |= WDCF_IRQ_WAIT;
    907 	return (ret);
    908 }
    909 
    910 /* Put all disk in RESET state */
    911 void
    912 wdc_reset_drive(struct ata_drive_datas *drvp, int flags)
    913 {
    914 	struct wdc_channel *chp = drvp->chnl_softc;
    915 	struct wdc_softc *wdc = chp->ch_wdc;
    916 	ATADEBUG_PRINT(("ata_reset_channel %s:%d for drive %d\n",
    917 	    wdc->sc_dev.dv_xname, chp->ch_channel, drvp->drive),
    918 	    DEBUG_FUNCS);
    919 
    920 
    921 	wdc_reset_channel(chp, flags);
    922 }
    923 
    924 void
    925 wdc_reset_channel(struct wdc_channel *chp, int flags)
    926 {
    927 	TAILQ_HEAD(, ata_xfer) reset_xfer;
    928 	struct ata_xfer *xfer, *next_xfer;
    929 	int drive;
    930 
    931 	chp->ch_queue->queue_freeze++;
    932 	TAILQ_INIT(&reset_xfer);
    933 
    934 	/* if we can poll or wait it's OK, otherwise wake up the kernel
    935 	 * thread
    936 	 */
    937 	if ((flags & (AT_POLL | AT_WAIT)) == 0) {
    938 		if (chp->ch_flags & WDCF_TH_RESET) {
    939 			/* no need to schedule a reset more than one time */
    940 			return;
    941 		}
    942 		chp->ch_flags |= WDCF_TH_RESET;
    943 		chp->ch_reset_flags = flags & (AT_RST_EMERG | AT_RST_NOCMD);
    944 		wakeup(&chp->ch_thread);
    945 		return;
    946 	}
    947 
    948 	chp->ch_flags &= ~WDCF_IRQ_WAIT;
    949 	/*
    950 	 * if the current command if on an ATAPI device, issue a
    951 	 * ATAPI_SOFT_RESET
    952 	 */
    953 	xfer = chp->ch_queue->active_xfer;
    954 	if (xfer && xfer->c_chp == chp && (xfer->c_flags & C_ATAPI)) {
    955 		wdccommandshort(chp, xfer->c_drive, ATAPI_SOFT_RESET);
    956 		if (flags & AT_WAIT)
    957 			tsleep(&flags, PRIBIO, "atardl", mstohz(1) + 1);
    958 		else
    959 			delay(1000);
    960 	}
    961 
    962 	/* reset the channel */
    963 	if (flags & AT_WAIT)
    964 		(void) wdcreset(chp, RESET_SLEEP);
    965 	else
    966 		(void) wdcreset(chp, RESET_POLL);
    967 
    968 	/*
    969 	 * wait a bit after reset; in case the DMA engines needs some time
    970 	 * to recover.
    971 	 */
    972 	if (flags & AT_WAIT)
    973 		tsleep(&flags, PRIBIO, "atardl", mstohz(1) + 1);
    974 	else
    975 		delay(1000);
    976 	/*
    977 	 * look for pending xfers. If we have a shared queue, we'll also reset
    978 	 * the other channel if the current xfer is running on it.
    979 	 * Then we'll dequeue only the xfers for this channel.
    980 	 */
    981 	if ((flags & AT_RST_NOCMD) == 0) {
    982 		/*
    983 		 * move all xfers queued for this channel to the reset queue,
    984 		 * and then process the current xfer and then the reset queue.
    985 		 * We have to use a temporary queue because c_kill_xfer()
    986 		 * may requeue commands.
    987 		 */
    988 		for (xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer);
    989 		    xfer != NULL; xfer = next_xfer) {
    990 			next_xfer = TAILQ_NEXT(xfer, c_xferchain);
    991 			if (xfer->c_chp != chp)
    992 				continue;
    993 			TAILQ_REMOVE(&chp->ch_queue->queue_xfer,
    994 			    xfer, c_xferchain);
    995 			TAILQ_INSERT_TAIL(&reset_xfer, xfer, c_xferchain);
    996 		}
    997 		xfer = chp->ch_queue->active_xfer;
    998 		if (xfer) {
    999 			if (xfer->c_chp != chp)
   1000 				wdc_reset_channel(xfer->c_chp, flags);
   1001 			else {
   1002 				callout_stop(&chp->ch_callout);
   1003 				/*
   1004 				 * If we're waiting for DMA, stop the
   1005 				 * DMA engine
   1006 				 */
   1007 				if (chp->ch_flags & WDCF_DMA_WAIT) {
   1008 					(*chp->ch_wdc->dma_finish)(
   1009 					    chp->ch_wdc->dma_arg,
   1010 					    chp->ch_channel,
   1011 					    xfer->c_drive,
   1012 					    WDC_DMAEND_ABRT_QUIET);
   1013 					chp->ch_flags &= ~WDCF_DMA_WAIT;
   1014 				}
   1015 				chp->ch_queue->active_xfer = NULL;
   1016 				if ((flags & AT_RST_EMERG) == 0)
   1017 					xfer->c_kill_xfer(
   1018 					    chp, xfer, KILL_RESET);
   1019 			}
   1020 		}
   1021 
   1022 		for (xfer = TAILQ_FIRST(&reset_xfer);
   1023 		    xfer != NULL; xfer = next_xfer) {
   1024 			next_xfer = TAILQ_NEXT(xfer, c_xferchain);
   1025 			TAILQ_REMOVE(&reset_xfer, xfer, c_xferchain);
   1026 			if ((flags & AT_RST_EMERG) == 0)
   1027 				xfer->c_kill_xfer(chp, xfer, KILL_RESET);
   1028 		}
   1029 	}
   1030 	for (drive = 0; drive < 2; drive++) {
   1031 		chp->ch_drive[drive].state = 0;
   1032 	}
   1033 	chp->ch_flags &= ~WDCF_TH_RESET;
   1034 	if ((flags & AT_RST_EMERG) == 0)  {
   1035 		chp->ch_queue->queue_freeze--;
   1036 		atastart(chp);
   1037 	} else {
   1038 		/* make sure that we can use polled commands */
   1039 		TAILQ_INIT(&chp->ch_queue->queue_xfer);
   1040 		chp->ch_queue->queue_freeze = 0;
   1041 		chp->ch_queue->active_xfer = NULL;
   1042 	}
   1043 }
   1044 
   1045 int
   1046 wdcreset(struct wdc_channel *chp, int poll)
   1047 {
   1048 	struct wdc_softc *wdc = chp->ch_wdc;
   1049 	int drv_mask1, drv_mask2;
   1050 	int s = 0;
   1051 
   1052 	if (wdc->select)
   1053 		wdc->select(chp,0);
   1054 	if (poll != RESET_SLEEP)
   1055 		s = splbio();
   1056 	/* master */
   1057 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0, WDSD_IBM);
   1058 	delay(10);	/* 400ns delay */
   1059 	bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
   1060 	    WDCTL_RST | WDCTL_IDS | WDCTL_4BIT);
   1061 	delay(2000);
   1062 	(void) bus_space_read_1(chp->cmd_iot, chp->cmd_iohs[wd_error], 0);
   1063 	bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
   1064 	    WDCTL_4BIT | WDCTL_IDS);
   1065 	delay(10);	/* 400ns delay */
   1066 	if (poll != RESET_SLEEP) {
   1067 		if (wdc->irqack)
   1068 			wdc->irqack(chp);
   1069 		splx(s);
   1070 	}
   1071 
   1072 	drv_mask1 = (chp->ch_drive[0].drive_flags & DRIVE) ? 0x01:0x00;
   1073 	drv_mask1 |= (chp->ch_drive[1].drive_flags & DRIVE) ? 0x02:0x00;
   1074 	drv_mask2 = __wdcwait_reset(chp, drv_mask1,
   1075 	    (poll == RESET_SLEEP) ? 0 : 1);
   1076 	if (drv_mask2 != drv_mask1) {
   1077 		printf("%s channel %d: reset failed for",
   1078 		    wdc->sc_dev.dv_xname, chp->ch_channel);
   1079 		if ((drv_mask1 & 0x01) != 0 && (drv_mask2 & 0x01) == 0)
   1080 			printf(" drive 0");
   1081 		if ((drv_mask1 & 0x02) != 0 && (drv_mask2 & 0x02) == 0)
   1082 			printf(" drive 1");
   1083 		printf("\n");
   1084 	}
   1085 	bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT);
   1086 	return  (drv_mask1 != drv_mask2) ? 1 : 0;
   1087 }
   1088 
   1089 static int
   1090 __wdcwait_reset(struct wdc_channel *chp, int drv_mask, int poll)
   1091 {
   1092 	struct wdc_softc *wdc = chp->ch_wdc;
   1093 	int timeout, nloop;
   1094 	u_int8_t st0 = 0, st1 = 0;
   1095 #ifdef ATADEBUG
   1096 	u_int8_t sc0 = 0, sn0 = 0, cl0 = 0, ch0 = 0;
   1097 	u_int8_t sc1 = 0, sn1 = 0, cl1 = 0, ch1 = 0;
   1098 #endif
   1099 
   1100 	if (poll)
   1101 		nloop = WDCNDELAY_RST;
   1102 	else
   1103 		nloop = WDC_RESET_WAIT * hz / 1000;
   1104 	/* wait for BSY to deassert */
   1105 	for (timeout = 0; timeout < nloop; timeout++) {
   1106 		if ((drv_mask & 0x01) != 0) {
   1107 			if (wdc && wdc->select)
   1108 				wdc->select(chp,0);
   1109 			bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh],
   1110 			    0, WDSD_IBM); /* master */
   1111 			delay(10);
   1112 			st0 = bus_space_read_1(chp->cmd_iot,
   1113 			    chp->cmd_iohs[wd_status], 0);
   1114 #ifdef ATADEBUG
   1115 			sc0 = bus_space_read_1(chp->cmd_iot,
   1116 			    chp->cmd_iohs[wd_seccnt], 0);
   1117 			sn0 = bus_space_read_1(chp->cmd_iot,
   1118 			    chp->cmd_iohs[wd_sector], 0);
   1119 			cl0 = bus_space_read_1(chp->cmd_iot,
   1120 			    chp->cmd_iohs[wd_cyl_lo], 0);
   1121 			ch0 = bus_space_read_1(chp->cmd_iot,
   1122 			    chp->cmd_iohs[wd_cyl_hi], 0);
   1123 #endif
   1124 		}
   1125 		if ((drv_mask & 0x02) != 0) {
   1126 			if (wdc && wdc->select)
   1127 				wdc->select(chp,1);
   1128 			bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh],
   1129 			    0, WDSD_IBM | 0x10); /* slave */
   1130 			delay(10);
   1131 			st1 = bus_space_read_1(chp->cmd_iot,
   1132 			    chp->cmd_iohs[wd_status], 0);
   1133 #ifdef ATADEBUG
   1134 			sc1 = bus_space_read_1(chp->cmd_iot,
   1135 			    chp->cmd_iohs[wd_seccnt], 0);
   1136 			sn1 = bus_space_read_1(chp->cmd_iot,
   1137 			    chp->cmd_iohs[wd_sector], 0);
   1138 			cl1 = bus_space_read_1(chp->cmd_iot,
   1139 			    chp->cmd_iohs[wd_cyl_lo], 0);
   1140 			ch1 = bus_space_read_1(chp->cmd_iot,
   1141 			    chp->cmd_iohs[wd_cyl_hi], 0);
   1142 #endif
   1143 		}
   1144 
   1145 		if ((drv_mask & 0x01) == 0) {
   1146 			/* no master */
   1147 			if ((drv_mask & 0x02) != 0 && (st1 & WDCS_BSY) == 0) {
   1148 				/* No master, slave is ready, it's done */
   1149 				goto end;
   1150 			}
   1151 		} else if ((drv_mask & 0x02) == 0) {
   1152 			/* no slave */
   1153 			if ((drv_mask & 0x01) != 0 && (st0 & WDCS_BSY) == 0) {
   1154 				/* No slave, master is ready, it's done */
   1155 				goto end;
   1156 			}
   1157 		} else {
   1158 			/* Wait for both master and slave to be ready */
   1159 			if ((st0 & WDCS_BSY) == 0 && (st1 & WDCS_BSY) == 0) {
   1160 				goto end;
   1161 			}
   1162 		}
   1163 		if (poll)
   1164 			delay(WDCDELAY);
   1165 		else
   1166 			tsleep(&nloop, PRIBIO, "atarst", 1);
   1167 	}
   1168 	/* Reset timed out. Maybe it's because drv_mask was not right */
   1169 	if (st0 & WDCS_BSY)
   1170 		drv_mask &= ~0x01;
   1171 	if (st1 & WDCS_BSY)
   1172 		drv_mask &= ~0x02;
   1173 end:
   1174 	ATADEBUG_PRINT(("%s:%d:0: after reset, sc=0x%x sn=0x%x "
   1175 	    "cl=0x%x ch=0x%x\n",
   1176 	     wdc != NULL ? wdc->sc_dev.dv_xname : "wdcprobe",
   1177 	     chp->ch_channel, sc0, sn0, cl0, ch0), DEBUG_PROBE);
   1178 	ATADEBUG_PRINT(("%s:%d:1: after reset, sc=0x%x sn=0x%x "
   1179 	    "cl=0x%x ch=0x%x\n",
   1180 	     wdc != NULL ? wdc->sc_dev.dv_xname : "wdcprobe",
   1181 	     chp->ch_channel, sc1, sn1, cl1, ch1), DEBUG_PROBE);
   1182 
   1183 	ATADEBUG_PRINT(("%s:%d: wdcwait_reset() end, st0=0x%x st1=0x%x\n",
   1184 	    wdc != NULL ? wdc->sc_dev.dv_xname : "wdcprobe", chp->ch_channel,
   1185 	    st0, st1), DEBUG_PROBE);
   1186 
   1187 	return drv_mask;
   1188 }
   1189 
   1190 /*
   1191  * Wait for a drive to be !BSY, and have mask in its status register.
   1192  * return -1 for a timeout after "timeout" ms.
   1193  */
   1194 static int
   1195 __wdcwait(struct wdc_channel *chp, int mask, int bits, int timeout)
   1196 {
   1197 	struct wdc_softc *wdc = chp->ch_wdc;
   1198 	u_char status;
   1199 	int time = 0;
   1200 
   1201 	ATADEBUG_PRINT(("__wdcwait %s:%d\n", wdc != NULL ?
   1202 			wdc->sc_dev.dv_xname : "none",
   1203 			chp->ch_channel), DEBUG_STATUS);
   1204 	chp->ch_error = 0;
   1205 
   1206 	timeout = timeout * 1000 / WDCDELAY; /* delay uses microseconds */
   1207 
   1208 	for (;;) {
   1209 		chp->ch_status = status =
   1210 		    bus_space_read_1(chp->cmd_iot, chp->cmd_iohs[wd_status], 0);
   1211 		if ((status & (WDCS_BSY | mask)) == bits)
   1212 			break;
   1213 		if (++time > timeout) {
   1214 			ATADEBUG_PRINT(("__wdcwait: timeout (time=%d), "
   1215 			    "status %x error %x (mask 0x%x bits 0x%x)\n",
   1216 			    time, status,
   1217 			    bus_space_read_1(chp->cmd_iot,
   1218 				chp->cmd_iohs[wd_error], 0), mask, bits),
   1219 			    DEBUG_STATUS | DEBUG_PROBE | DEBUG_DELAY);
   1220 			return(WDCWAIT_TOUT);
   1221 		}
   1222 		delay(WDCDELAY);
   1223 	}
   1224 #ifdef ATADEBUG
   1225 	if (time > 0 && (atadebug_mask & DEBUG_DELAY))
   1226 		printf("__wdcwait: did busy-wait, time=%d\n", time);
   1227 #endif
   1228 	if (status & WDCS_ERR)
   1229 		chp->ch_error = bus_space_read_1(chp->cmd_iot,
   1230 		    chp->cmd_iohs[wd_error], 0);
   1231 #ifdef WDCNDELAY_DEBUG
   1232 	/* After autoconfig, there should be no long delays. */
   1233 	if (!cold && time > WDCNDELAY_DEBUG) {
   1234 		struct ata_xfer *xfer = chp->ch_queue->active_xfer;
   1235 		if (xfer == NULL)
   1236 			printf("%s channel %d: warning: busy-wait took %dus\n",
   1237 			    wdc->sc_dev.dv_xname, chp->ch_channel,
   1238 			    WDCDELAY * time);
   1239 		else
   1240 			printf("%s:%d:%d: warning: busy-wait took %dus\n",
   1241 			    wdc->sc_dev.dv_xname, chp->ch_channel,
   1242 			    xfer->drive,
   1243 			    WDCDELAY * time);
   1244 	}
   1245 #endif
   1246 	return(WDCWAIT_OK);
   1247 }
   1248 
   1249 /*
   1250  * Call __wdcwait(), polling using tsleep() or waking up the kernel
   1251  * thread if possible
   1252  */
   1253 int
   1254 wdcwait(struct wdc_channel *chp, int mask, int bits, int timeout, int flags)
   1255 {
   1256 	int error, i, timeout_hz = mstohz(timeout);
   1257 
   1258 	if (timeout_hz == 0 ||
   1259 	    (flags & (AT_WAIT | AT_POLL)) == AT_POLL)
   1260 		error = __wdcwait(chp, mask, bits, timeout);
   1261 	else {
   1262 		error = __wdcwait(chp, mask, bits, WDCDELAY_POLL);
   1263 		if (error != 0) {
   1264 			if ((chp->ch_flags & WDCF_TH_RUN) ||
   1265 			    (flags & AT_WAIT)) {
   1266 				/*
   1267 				 * we're running in the channel thread
   1268 				 * or some userland thread context
   1269 				 */
   1270 				for (i = 0; i < timeout_hz; i++) {
   1271 					if (__wdcwait(chp, mask, bits,
   1272 					    WDCDELAY_POLL) == 0) {
   1273 						error = 0;
   1274 						break;
   1275 					}
   1276 					tsleep(&chp, PRIBIO, "atapoll", 1);
   1277 				}
   1278 			} else {
   1279 				/*
   1280 				 * we're probably in interrupt context,
   1281 				 * ask the thread to come back here
   1282 				 */
   1283 #ifdef DIAGNOSTIC
   1284 				if (chp->ch_queue->queue_freeze > 0)
   1285 					panic("wdcwait: queue_freeze");
   1286 #endif
   1287 				chp->ch_queue->queue_freeze++;
   1288 				wakeup(&chp->ch_thread);
   1289 				return(WDCWAIT_THR);
   1290 			}
   1291 		}
   1292 	}
   1293 	return (error);
   1294 }
   1295 
   1296 
   1297 /*
   1298  * Busy-wait for DMA to complete
   1299  */
   1300 int
   1301 wdc_dmawait(struct wdc_channel *chp, struct ata_xfer *xfer, int timeout)
   1302 {
   1303 	struct wdc_softc *wdc = chp->ch_wdc;
   1304 	int time;
   1305 
   1306 	for (time = 0;  time < timeout * 1000 / WDCDELAY; time++) {
   1307 		wdc->dma_status =
   1308 		    (*wdc->dma_finish)(wdc->dma_arg,
   1309 			chp->ch_channel, xfer->c_drive, WDC_DMAEND_END);
   1310 		if ((wdc->dma_status & WDC_DMAST_NOIRQ) == 0)
   1311 			return 0;
   1312 		delay(WDCDELAY);
   1313 	}
   1314 	/* timeout, force a DMA halt */
   1315 	wdc->dma_status = (*wdc->dma_finish)(wdc->dma_arg,
   1316 	    chp->ch_channel, xfer->c_drive, WDC_DMAEND_ABRT);
   1317 	return 1;
   1318 }
   1319 
   1320 void
   1321 wdctimeout(void *arg)
   1322 {
   1323 	struct wdc_channel *chp = (struct wdc_channel *)arg;
   1324 	struct wdc_softc *wdc = chp->ch_wdc;
   1325 	struct ata_xfer *xfer = chp->ch_queue->active_xfer;
   1326 	int s;
   1327 
   1328 	ATADEBUG_PRINT(("wdctimeout\n"), DEBUG_FUNCS);
   1329 
   1330 	s = splbio();
   1331 	if ((chp->ch_flags & WDCF_IRQ_WAIT) != 0) {
   1332 		__wdcerror(chp, "lost interrupt");
   1333 		printf("\ttype: %s tc_bcount: %d tc_skip: %d\n",
   1334 		    (xfer->c_flags & C_ATAPI) ?  "atapi" : "ata",
   1335 		    xfer->c_bcount,
   1336 		    xfer->c_skip);
   1337 		if (chp->ch_flags & WDCF_DMA_WAIT) {
   1338 			wdc->dma_status =
   1339 			    (*wdc->dma_finish)(wdc->dma_arg,
   1340 				chp->ch_channel, xfer->c_drive,
   1341 				WDC_DMAEND_ABRT);
   1342 			chp->ch_flags &= ~WDCF_DMA_WAIT;
   1343 		}
   1344 		/*
   1345 		 * Call the interrupt routine. If we just missed an interrupt,
   1346 		 * it will do what's needed. Else, it will take the needed
   1347 		 * action (reset the device).
   1348 		 * Before that we need to reinstall the timeout callback,
   1349 		 * in case it will miss another irq while in this transfer
   1350 		 * We arbitray chose it to be 1s
   1351 		 */
   1352 		callout_reset(&chp->ch_callout, hz, wdctimeout, chp);
   1353 		xfer->c_flags |= C_TIMEOU;
   1354 		chp->ch_flags &= ~WDCF_IRQ_WAIT;
   1355 		xfer->c_intr(chp, xfer, 1);
   1356 	} else
   1357 		__wdcerror(chp, "missing untimeout");
   1358 	splx(s);
   1359 }
   1360 
   1361 int
   1362 wdc_exec_command(struct ata_drive_datas *drvp, struct ata_command *ata_c)
   1363 {
   1364 	struct wdc_channel *chp = drvp->chnl_softc;
   1365 	struct wdc_softc *wdc = chp->ch_wdc;
   1366 	struct ata_xfer *xfer;
   1367 	int s, ret;
   1368 
   1369 	ATADEBUG_PRINT(("wdc_exec_command %s:%d:%d\n",
   1370 	    wdc->sc_dev.dv_xname, chp->ch_channel, drvp->drive),
   1371 	    DEBUG_FUNCS);
   1372 
   1373 	/* set up an xfer and queue. Wait for completion */
   1374 	xfer = ata_get_xfer(ata_c->flags & AT_WAIT ? ATAXF_CANSLEEP :
   1375 	    ATAXF_NOSLEEP);
   1376 	if (xfer == NULL) {
   1377 		return ATACMD_TRY_AGAIN;
   1378 	 }
   1379 
   1380 	if (wdc->cap & WDC_CAPABILITY_NOIRQ)
   1381 		ata_c->flags |= AT_POLL;
   1382 	if (ata_c->flags & AT_POLL)
   1383 		xfer->c_flags |= C_POLL;
   1384 	xfer->c_drive = drvp->drive;
   1385 	xfer->c_databuf = ata_c->data;
   1386 	xfer->c_bcount = ata_c->bcount;
   1387 	xfer->c_cmd = ata_c;
   1388 	xfer->c_start = __wdccommand_start;
   1389 	xfer->c_intr = __wdccommand_intr;
   1390 	xfer->c_kill_xfer = __wdccommand_kill_xfer;
   1391 
   1392 	s = splbio();
   1393 	ata_exec_xfer(chp, xfer);
   1394 #ifdef DIAGNOSTIC
   1395 	if ((ata_c->flags & AT_POLL) != 0 &&
   1396 	    (ata_c->flags & AT_DONE) == 0)
   1397 		panic("wdc_exec_command: polled command not done");
   1398 #endif
   1399 	if (ata_c->flags & AT_DONE) {
   1400 		ret = ATACMD_COMPLETE;
   1401 	} else {
   1402 		if (ata_c->flags & AT_WAIT) {
   1403 			while ((ata_c->flags & AT_DONE) == 0) {
   1404 				tsleep(ata_c, PRIBIO, "wdccmd", 0);
   1405 			}
   1406 			ret = ATACMD_COMPLETE;
   1407 		} else {
   1408 			ret = ATACMD_QUEUED;
   1409 		}
   1410 	}
   1411 	splx(s);
   1412 	return ret;
   1413 }
   1414 
   1415 static void
   1416 __wdccommand_start(struct wdc_channel *chp, struct ata_xfer *xfer)
   1417 {
   1418 	struct wdc_softc *wdc = chp->ch_wdc;
   1419 	int drive = xfer->c_drive;
   1420 	struct ata_command *ata_c = xfer->c_cmd;
   1421 
   1422 	ATADEBUG_PRINT(("__wdccommand_start %s:%d:%d\n",
   1423 	    wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive),
   1424 	    DEBUG_FUNCS);
   1425 
   1426 	if (wdc->select)
   1427 		wdc->select(chp,drive);
   1428 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0,
   1429 	    WDSD_IBM | (drive << 4));
   1430 	switch(wdcwait(chp, ata_c->r_st_bmask | WDCS_DRQ,
   1431 	    ata_c->r_st_bmask, ata_c->timeout, ata_c->flags)) {
   1432 	case WDCWAIT_OK:
   1433 		break;
   1434 	case WDCWAIT_TOUT:
   1435 		ata_c->flags |= AT_TIMEOU;
   1436 		__wdccommand_done(chp, xfer);
   1437 		return;
   1438 	case WDCWAIT_THR:
   1439 		return;
   1440 	}
   1441 	if (ata_c->flags & AT_POLL) {
   1442 		/* polled command, disable interrupts */
   1443 		bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
   1444 		    WDCTL_4BIT | WDCTL_IDS);
   1445 	}
   1446 	wdccommand(chp, drive, ata_c->r_command, ata_c->r_cyl, ata_c->r_head,
   1447 	    ata_c->r_sector, ata_c->r_count, ata_c->r_features);
   1448 
   1449 	if ((ata_c->flags & AT_POLL) == 0) {
   1450 		chp->ch_flags |= WDCF_IRQ_WAIT; /* wait for interrupt */
   1451 		callout_reset(&chp->ch_callout, ata_c->timeout / 1000 * hz,
   1452 		    wdctimeout, chp);
   1453 		return;
   1454 	}
   1455 	/*
   1456 	 * Polled command. Wait for drive ready or drq. Done in intr().
   1457 	 * Wait for at last 400ns for status bit to be valid.
   1458 	 */
   1459 	delay(10);	/* 400ns delay */
   1460 	__wdccommand_intr(chp, xfer, 0);
   1461 }
   1462 
   1463 static int
   1464 __wdccommand_intr(struct wdc_channel *chp, struct ata_xfer *xfer, int irq)
   1465 {
   1466 	struct wdc_softc *wdc = chp->ch_wdc;
   1467 	struct ata_command *ata_c = xfer->c_cmd;
   1468 	int bcount = ata_c->bcount;
   1469 	char *data = ata_c->data;
   1470 	int wflags;
   1471 
   1472 	if ((ata_c->flags & (AT_WAIT | AT_POLL)) == (AT_WAIT | AT_POLL)) {
   1473 		/* both wait and poll, we can tsleep here */
   1474 		wflags = AT_WAIT | AT_POLL;
   1475 	} else {
   1476 		wflags = AT_POLL;
   1477 	}
   1478 
   1479  again:
   1480 	ATADEBUG_PRINT(("__wdccommand_intr %s:%d:%d\n",
   1481 	    wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive),
   1482 	    DEBUG_INTR);
   1483 	/*
   1484 	 * after a ATAPI_SOFT_RESET, the device will have released the bus.
   1485 	 * Reselect again, it doesn't hurt for others commands, and the time
   1486 	 * penalty for the extra regiter write is acceptable,
   1487 	 * wdc_exec_command() isn't called often (mosly for autoconfig)
   1488 	 */
   1489 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0,
   1490 	    WDSD_IBM | (xfer->c_drive << 4));
   1491 	if ((ata_c->flags & AT_XFDONE) != 0) {
   1492 		/*
   1493 		 * We have completed a data xfer. The drive should now be
   1494 		 * in its initial state
   1495 		 */
   1496 		if (wdcwait(chp, ata_c->r_st_bmask | WDCS_DRQ,
   1497 		    ata_c->r_st_bmask, (irq == 0)  ? ata_c->timeout : 0,
   1498 		    wflags) ==  WDCWAIT_TOUT) {
   1499 			if (irq && (xfer->c_flags & C_TIMEOU) == 0)
   1500 				return 0; /* IRQ was not for us */
   1501 			ata_c->flags |= AT_TIMEOU;
   1502 		}
   1503 		goto out;
   1504 	}
   1505 	if (wdcwait(chp, ata_c->r_st_pmask, ata_c->r_st_pmask,
   1506 	     (irq == 0)  ? ata_c->timeout : 0, wflags) == WDCWAIT_TOUT) {
   1507 		if (irq && (xfer->c_flags & C_TIMEOU) == 0)
   1508 			return 0; /* IRQ was not for us */
   1509 		ata_c->flags |= AT_TIMEOU;
   1510 		goto out;
   1511 	}
   1512 	if (wdc->irqack)
   1513 		wdc->irqack(chp);
   1514 	if (ata_c->flags & AT_READ) {
   1515 		if ((chp->ch_status & WDCS_DRQ) == 0) {
   1516 			ata_c->flags |= AT_TIMEOU;
   1517 			goto out;
   1518 		}
   1519 		if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_CAP32) {
   1520 			bus_space_read_multi_4(chp->data32iot, chp->data32ioh,
   1521 			    0, (u_int32_t*)data, bcount >> 2);
   1522 			data += bcount & 0xfffffffc;
   1523 			bcount = bcount & 0x03;
   1524 		}
   1525 		if (bcount > 0)
   1526 			wdc->datain_pio(chp, DRIVE_NOSTREAM, data, bcount);
   1527 		/* at this point the drive should be in its initial state */
   1528 		ata_c->flags |= AT_XFDONE;
   1529 		/* XXX should read status register here ? */
   1530 	} else if (ata_c->flags & AT_WRITE) {
   1531 		if ((chp->ch_status & WDCS_DRQ) == 0) {
   1532 			ata_c->flags |= AT_TIMEOU;
   1533 			goto out;
   1534 		}
   1535 		if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_CAP32) {
   1536 			bus_space_write_multi_4(chp->data32iot, chp->data32ioh,
   1537 			    0, (u_int32_t*)data, bcount >> 2);
   1538 			data += bcount & 0xfffffffc;
   1539 			bcount = bcount & 0x03;
   1540 		}
   1541 		if (bcount > 0)
   1542 			wdc->dataout_pio(chp, DRIVE_NOSTREAM, data, bcount);
   1543 		ata_c->flags |= AT_XFDONE;
   1544 		if ((ata_c->flags & AT_POLL) == 0) {
   1545 			chp->ch_flags |= WDCF_IRQ_WAIT; /* wait for interrupt */
   1546 			callout_reset(&chp->ch_callout,
   1547 			    ata_c->timeout / 1000 * hz, wdctimeout, chp);
   1548 			return 1;
   1549 		} else {
   1550 			goto again;
   1551 		}
   1552 	}
   1553  out:
   1554 	__wdccommand_done(chp, xfer);
   1555 	return 1;
   1556 }
   1557 
   1558 static void
   1559 __wdccommand_done(struct wdc_channel *chp, struct ata_xfer *xfer)
   1560 {
   1561 	struct wdc_softc *wdc = chp->ch_wdc;
   1562 	struct ata_command *ata_c = xfer->c_cmd;
   1563 
   1564 	ATADEBUG_PRINT(("__wdccommand_done %s:%d:%d\n",
   1565 	    wdc->sc_dev.dv_xname, chp->ch_channel, xfer->c_drive),
   1566 	    DEBUG_FUNCS);
   1567 
   1568 
   1569 	if (chp->ch_status & WDCS_DWF)
   1570 		ata_c->flags |= AT_DF;
   1571 	if (chp->ch_status & WDCS_ERR) {
   1572 		ata_c->flags |= AT_ERROR;
   1573 		ata_c->r_error = chp->ch_error;
   1574 	}
   1575 	if ((ata_c->flags & AT_READREG) != 0 &&
   1576 	    (wdc->sc_dev.dv_flags & DVF_ACTIVE) != 0 &&
   1577 	    (ata_c->flags & (AT_ERROR | AT_DF)) == 0) {
   1578 		ata_c->r_head = bus_space_read_1(chp->cmd_iot,
   1579 		    chp->cmd_iohs[wd_sdh], 0);
   1580 		ata_c->r_count = bus_space_read_1(chp->cmd_iot,
   1581 		    chp->cmd_iohs[wd_seccnt], 0);
   1582 		ata_c->r_sector = bus_space_read_1(chp->cmd_iot,
   1583 		    chp->cmd_iohs[wd_sector], 0);
   1584 		ata_c->r_cyl |= bus_space_read_1(chp->cmd_iot,
   1585 		    chp->cmd_iohs[wd_cyl_lo], 0);
   1586 		ata_c->r_cyl = bus_space_read_1(chp->cmd_iot,
   1587 		    chp->cmd_iohs[wd_cyl_hi], 0) << 8;
   1588 		ata_c->r_error = bus_space_read_1(chp->cmd_iot,
   1589 		    chp->cmd_iohs[wd_error], 0);
   1590 		ata_c->r_features = bus_space_read_1(chp->cmd_iot,
   1591 		    chp->cmd_iohs[wd_features], 0);
   1592 	}
   1593 	callout_stop(&chp->ch_callout);
   1594 	chp->ch_queue->active_xfer = NULL;
   1595 	if (ata_c->flags & AT_POLL) {
   1596 		/* enable interrupts */
   1597 		bus_space_write_1(chp->ctl_iot, chp->ctl_ioh, wd_aux_ctlr,
   1598 		    WDCTL_4BIT);
   1599 		delay(10); /* some drives need a little delay here */
   1600 	}
   1601 	if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_WAITDRAIN) {
   1602 		__wdccommand_kill_xfer(chp, xfer, KILL_GONE);
   1603 		chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_WAITDRAIN;
   1604 		wakeup(&chp->ch_queue->active_xfer);
   1605 	} else
   1606 		__wdccommand_done_end(chp, xfer);
   1607 }
   1608 
   1609 static void
   1610 __wdccommand_done_end(struct wdc_channel *chp, struct ata_xfer *xfer)
   1611 {
   1612 	struct ata_command *ata_c = xfer->c_cmd;
   1613 
   1614 	ata_c->flags |= AT_DONE;
   1615 	ata_free_xfer(chp, xfer);
   1616 	if (ata_c->flags & AT_WAIT)
   1617 		wakeup(ata_c);
   1618 	else if (ata_c->callback)
   1619 		ata_c->callback(ata_c->callback_arg);
   1620 	atastart(chp);
   1621 	return;
   1622 }
   1623 
   1624 static void
   1625 __wdccommand_kill_xfer(struct wdc_channel *chp, struct ata_xfer *xfer,
   1626     int reason)
   1627 {
   1628 	struct ata_command *ata_c = xfer->c_cmd;
   1629 
   1630 	switch (reason) {
   1631 	case KILL_GONE:
   1632 		ata_c->flags |= AT_GONE;
   1633 		break;
   1634 	case KILL_RESET:
   1635 		ata_c->flags |= AT_RESET;
   1636 		break;
   1637 	default:
   1638 		printf("__wdccommand_kill_xfer: unknown reason %d\n",
   1639 		    reason);
   1640 		panic("__wdccommand_kill_xfer");
   1641 	}
   1642 	__wdccommand_done_end(chp, xfer);
   1643 }
   1644 
   1645 /*
   1646  * Send a command. The drive should be ready.
   1647  * Assumes interrupts are blocked.
   1648  */
   1649 void
   1650 wdccommand(struct wdc_channel *chp, u_int8_t drive, u_int8_t command,
   1651     u_int16_t cylin, u_int8_t head, u_int8_t sector, u_int8_t count,
   1652     u_int8_t features)
   1653 {
   1654 	struct wdc_softc *wdc = chp->ch_wdc;
   1655 
   1656 	ATADEBUG_PRINT(("wdccommand %s:%d:%d: command=0x%x cylin=%d head=%d "
   1657 	    "sector=%d count=%d features=%d\n", wdc->sc_dev.dv_xname,
   1658 	    chp->ch_channel, drive, command, cylin, head, sector, count,
   1659 	    features), DEBUG_FUNCS);
   1660 
   1661 	if (wdc->select)
   1662 		wdc->select(chp,drive);
   1663 
   1664 	/* Select drive, head, and addressing mode. */
   1665 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0,
   1666 	    WDSD_IBM | (drive << 4) | head);
   1667 	/* Load parameters into the wd_features register. */
   1668 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_features], 0,
   1669 	    features);
   1670 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_seccnt], 0, count);
   1671 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sector], 0, sector);
   1672 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_cyl_lo], 0, cylin);
   1673 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_cyl_hi],
   1674 	    0, cylin >> 8);
   1675 
   1676 	/* Send command. */
   1677 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_command], 0, command);
   1678 	return;
   1679 }
   1680 
   1681 /*
   1682  * Send a 48-bit addressing command. The drive should be ready.
   1683  * Assumes interrupts are blocked.
   1684  */
   1685 void
   1686 wdccommandext(struct wdc_channel *chp, u_int8_t drive, u_int8_t command,
   1687     u_int64_t blkno, u_int16_t count)
   1688 {
   1689 	struct wdc_softc *wdc = chp->ch_wdc;
   1690 
   1691 	ATADEBUG_PRINT(("wdccommandext %s:%d:%d: command=0x%x blkno=%d "
   1692 	    "count=%d\n", wdc->sc_dev.dv_xname,
   1693 	    chp->ch_channel, drive, command, (u_int32_t) blkno, count),
   1694 	    DEBUG_FUNCS);
   1695 
   1696 	if (wdc->select)
   1697 		wdc->select(chp,drive);
   1698 
   1699 	/* Select drive, head, and addressing mode. */
   1700 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0,
   1701 	    (drive << 4) | WDSD_LBA);
   1702 
   1703 	/* previous */
   1704 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_features], 0, 0);
   1705 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_seccnt],
   1706 	    0, count >> 8);
   1707 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_lba_lo],
   1708 	    0, blkno >> 24);
   1709 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_lba_mi],
   1710 	    0, blkno >> 32);
   1711 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_lba_hi],
   1712 	    0, blkno >> 40);
   1713 
   1714 	/* current */
   1715 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_features], 0, 0);
   1716 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_seccnt], 0, count);
   1717 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_lba_lo], 0, blkno);
   1718 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_lba_mi],
   1719 	    0, blkno >> 8);
   1720 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_lba_hi],
   1721 	    0, blkno >> 16);
   1722 
   1723 	/* Send command. */
   1724 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_command], 0, command);
   1725 	return;
   1726 }
   1727 
   1728 /*
   1729  * Simplified version of wdccommand().  Unbusy/ready/drq must be
   1730  * tested by the caller.
   1731  */
   1732 void
   1733 wdccommandshort(struct wdc_channel *chp, int drive, int command)
   1734 {
   1735 	struct wdc_softc *wdc = chp->ch_wdc;
   1736 
   1737 	ATADEBUG_PRINT(("wdccommandshort %s:%d:%d command 0x%x\n",
   1738 	    wdc->sc_dev.dv_xname, chp->ch_channel, drive, command),
   1739 	    DEBUG_FUNCS);
   1740 
   1741 	if (wdc->select)
   1742 		wdc->select(chp,drive);
   1743 
   1744 	/* Select drive. */
   1745 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_sdh], 0,
   1746 	    WDSD_IBM | (drive << 4));
   1747 
   1748 	bus_space_write_1(chp->cmd_iot, chp->cmd_iohs[wd_command], 0, command);
   1749 }
   1750 
   1751 static void
   1752 __wdcerror(struct wdc_channel *chp, char *msg)
   1753 {
   1754 	struct wdc_softc *wdc = chp->ch_wdc;
   1755 	struct ata_xfer *xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer);
   1756 
   1757 	if (xfer == NULL)
   1758 		printf("%s:%d: %s\n", wdc->sc_dev.dv_xname, chp->ch_channel,
   1759 		    msg);
   1760 	else
   1761 		printf("%s:%d:%d: %s\n", wdc->sc_dev.dv_xname,
   1762 		    chp->ch_channel, xfer->c_drive, msg);
   1763 }
   1764 
   1765 /*
   1766  * the bit bucket
   1767  */
   1768 void
   1769 wdcbit_bucket(struct wdc_channel *chp, int size)
   1770 {
   1771 
   1772 	for (; size >= 2; size -= 2)
   1773 		(void)bus_space_read_2(chp->cmd_iot, chp->cmd_iohs[wd_data], 0);
   1774 	if (size)
   1775 		(void)bus_space_read_1(chp->cmd_iot, chp->cmd_iohs[wd_data], 0);
   1776 }
   1777 
   1778 void
   1779 wdc_datain_pio(chp, flags, buf, len)
   1780 	struct wdc_channel *chp;
   1781 	int flags;
   1782 	void *buf;
   1783 	size_t len;
   1784 {
   1785 
   1786 	if (flags & DRIVE_NOSTREAM) {
   1787 		if (flags & DRIVE_CAP32) {
   1788 			bus_space_read_multi_4(chp->data32iot,
   1789 			    chp->data32ioh, 0, buf, len >> 2);
   1790 			buf = (char *)buf + (len & ~3);
   1791 			len &= 3;
   1792 		}
   1793 		if (len) {
   1794 			bus_space_read_multi_2(chp->cmd_iot,
   1795 			    chp->cmd_iohs[wd_data], 0, buf, len >> 1);
   1796 		}
   1797 	} else {
   1798 		if (flags & DRIVE_CAP32) {
   1799 			bus_space_read_multi_stream_4(chp->data32iot,
   1800 			    chp->data32ioh, 0, buf, len >> 2);
   1801 			buf = (char *)buf + (len & ~3);
   1802 			len &= 3;
   1803 		}
   1804 		if (len) {
   1805 			bus_space_read_multi_stream_2(chp->cmd_iot,
   1806 			    chp->cmd_iohs[wd_data], 0, buf, len >> 1);
   1807 		}
   1808 	}
   1809 }
   1810 
   1811 void
   1812 wdc_dataout_pio(chp, flags, buf, len)
   1813 	struct wdc_channel *chp;
   1814 	int flags;
   1815 	void *buf;
   1816 	size_t len;
   1817 {
   1818 
   1819 	if (flags & DRIVE_NOSTREAM) {
   1820 		if (flags & DRIVE_CAP32) {
   1821 			bus_space_write_multi_4(chp->data32iot,
   1822 			    chp->data32ioh, 0, buf, len >> 2);
   1823 			buf = (char *)buf + (len & ~3);
   1824 			len &= 3;
   1825 		}
   1826 		if (len) {
   1827 			bus_space_write_multi_2(chp->cmd_iot,
   1828 			    chp->cmd_iohs[wd_data], 0, buf, len >> 1);
   1829 		}
   1830 	} else {
   1831 		if (flags & DRIVE_CAP32) {
   1832 			bus_space_write_multi_stream_4(chp->data32iot,
   1833 			    chp->data32ioh, 0, buf, len >> 2);
   1834 			buf = (char *)buf + (len & ~3);
   1835 			len &= 3;
   1836 		}
   1837 		if (len) {
   1838 			bus_space_write_multi_stream_2(chp->cmd_iot,
   1839 			    chp->cmd_iohs[wd_data], 0, buf, len >> 1);
   1840 		}
   1841 	}
   1842 }
   1843