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