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