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