Home | History | Annotate | Line # | Download | only in ata
ata.c revision 1.46
      1  1.46  thorpej /*      $NetBSD: ata.c,v 1.46 2004/08/13 03:12:59 thorpej Exp $      */
      2  1.21  thorpej 
      3   1.2   bouyer /*
      4  1.16   bouyer  * Copyright (c) 1998, 2001 Manuel Bouyer.  All rights reserved.
      5   1.2   bouyer  *
      6   1.2   bouyer  * Redistribution and use in source and binary forms, with or without
      7   1.2   bouyer  * modification, are permitted provided that the following conditions
      8   1.2   bouyer  * are met:
      9   1.2   bouyer  * 1. Redistributions of source code must retain the above copyright
     10   1.2   bouyer  *    notice, this list of conditions and the following disclaimer.
     11   1.2   bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.2   bouyer  *    notice, this list of conditions and the following disclaimer in the
     13   1.2   bouyer  *    documentation and/or other materials provided with the distribution.
     14   1.2   bouyer  * 3. All advertising materials mentioning features or use of this software
     15   1.2   bouyer  *    must display the following acknowledgement:
     16   1.2   bouyer  *  This product includes software developed by Manuel Bouyer.
     17   1.2   bouyer  * 4. The name of the author may not be used to endorse or promote products
     18   1.2   bouyer  *    derived from this software without specific prior written permission.
     19   1.2   bouyer  *
     20   1.2   bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21   1.2   bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22   1.2   bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.13   bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24   1.2   bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25   1.2   bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26   1.2   bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27   1.2   bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28   1.2   bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29   1.2   bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30   1.2   bouyer  */
     31  1.14    lukem 
     32  1.14    lukem #include <sys/cdefs.h>
     33  1.46  thorpej __KERNEL_RCSID(0, "$NetBSD: ata.c,v 1.46 2004/08/13 03:12:59 thorpej Exp $");
     34   1.2   bouyer 
     35   1.6  hubertf #ifndef WDCDEBUG
     36   1.2   bouyer #define WDCDEBUG
     37   1.6  hubertf #endif /* WDCDEBUG */
     38   1.2   bouyer 
     39   1.2   bouyer #include <sys/param.h>
     40   1.2   bouyer #include <sys/systm.h>
     41   1.2   bouyer #include <sys/kernel.h>
     42   1.2   bouyer #include <sys/malloc.h>
     43   1.2   bouyer #include <sys/device.h>
     44  1.30   bouyer #include <sys/conf.h>
     45  1.30   bouyer #include <sys/fcntl.h>
     46  1.23  thorpej #include <sys/proc.h>
     47  1.41  thorpej #include <sys/pool.h>
     48  1.23  thorpej #include <sys/kthread.h>
     49  1.23  thorpej #include <sys/errno.h>
     50  1.30   bouyer #include <sys/ataio.h>
     51   1.2   bouyer 
     52  1.15   bouyer #include <machine/intr.h>
     53  1.15   bouyer #include <machine/bus.h>
     54  1.15   bouyer 
     55   1.2   bouyer #include <dev/ata/atareg.h>
     56   1.2   bouyer #include <dev/ata/atavar.h>
     57  1.15   bouyer #include <dev/ic/wdcreg.h>
     58  1.15   bouyer #include <dev/ic/wdcvar.h>
     59   1.2   bouyer 
     60  1.23  thorpej #include "locators.h"
     61  1.23  thorpej 
     62   1.2   bouyer #define DEBUG_FUNCS  0x08
     63   1.2   bouyer #define DEBUG_PROBE  0x10
     64  1.23  thorpej #define DEBUG_DETACH 0x20
     65  1.44  thorpej #define	DEBUG_XFERS  0x40
     66   1.2   bouyer #ifdef WDCDEBUG
     67   1.2   bouyer extern int wdcdebug_mask; /* init'ed in wdc.c */
     68   1.2   bouyer #define WDCDEBUG_PRINT(args, level) \
     69   1.2   bouyer         if (wdcdebug_mask & (level)) \
     70   1.2   bouyer 		printf args
     71   1.2   bouyer #else
     72   1.2   bouyer #define WDCDEBUG_PRINT(args, level)
     73   1.2   bouyer #endif
     74   1.2   bouyer 
     75  1.41  thorpej POOL_INIT(ata_xfer_pool, sizeof(struct ata_xfer), 0, 0, 0, "ataspl", NULL);
     76  1.41  thorpej 
     77  1.23  thorpej /*****************************************************************************
     78  1.23  thorpej  * ATA bus layer.
     79  1.23  thorpej  *
     80  1.23  thorpej  * ATA controllers attach an atabus instance, which handles probing the bus
     81  1.23  thorpej  * for drives, etc.
     82  1.23  thorpej  *****************************************************************************/
     83  1.23  thorpej 
     84  1.30   bouyer dev_type_open(atabusopen);
     85  1.30   bouyer dev_type_close(atabusclose);
     86  1.30   bouyer dev_type_ioctl(atabusioctl);
     87  1.30   bouyer 
     88  1.30   bouyer const struct cdevsw atabus_cdevsw = {
     89  1.30   bouyer 	atabusopen, atabusclose, noread, nowrite, atabusioctl,
     90  1.30   bouyer 	nostop, notty, nopoll, nommap, nokqfilter,
     91  1.30   bouyer };
     92  1.30   bouyer 
     93  1.30   bouyer extern struct cfdriver atabus_cd;
     94  1.30   bouyer 
     95  1.30   bouyer 
     96  1.23  thorpej /*
     97  1.23  thorpej  * atabusprint:
     98  1.23  thorpej  *
     99  1.23  thorpej  *	Autoconfiguration print routine used by ATA controllers when
    100  1.23  thorpej  *	attaching an atabus instance.
    101  1.23  thorpej  */
    102  1.23  thorpej int
    103  1.23  thorpej atabusprint(void *aux, const char *pnp)
    104  1.23  thorpej {
    105  1.25  thorpej 	struct wdc_channel *chan = aux;
    106  1.23  thorpej 
    107  1.23  thorpej 	if (pnp)
    108  1.23  thorpej 		aprint_normal("atabus at %s", pnp);
    109  1.26  thorpej 	aprint_normal(" channel %d", chan->ch_channel);
    110  1.23  thorpej 
    111  1.23  thorpej 	return (UNCONF);
    112  1.23  thorpej }
    113  1.23  thorpej 
    114  1.23  thorpej /*
    115  1.23  thorpej  * ataprint:
    116  1.23  thorpej  *
    117  1.23  thorpej  *	Autoconfiguration print routine.
    118  1.23  thorpej  */
    119  1.23  thorpej int
    120  1.23  thorpej ataprint(void *aux, const char *pnp)
    121  1.23  thorpej {
    122  1.23  thorpej 	struct ata_device *adev = aux;
    123  1.23  thorpej 
    124  1.23  thorpej 	if (pnp)
    125  1.23  thorpej 		aprint_normal("wd at %s", pnp);
    126  1.23  thorpej 	aprint_normal(" drive %d", adev->adev_drv_data->drive);
    127  1.23  thorpej 
    128  1.23  thorpej 	return (UNCONF);
    129  1.23  thorpej }
    130  1.23  thorpej 
    131  1.23  thorpej /*
    132  1.23  thorpej  * atabus_thread:
    133  1.23  thorpej  *
    134  1.23  thorpej  *	Worker thread for the ATA bus.
    135  1.23  thorpej  */
    136  1.23  thorpej static void
    137  1.23  thorpej atabus_thread(void *arg)
    138  1.23  thorpej {
    139  1.23  thorpej 	struct atabus_softc *sc = arg;
    140  1.25  thorpej 	struct wdc_channel *chp = sc->sc_chan;
    141  1.24  thorpej 	struct ata_xfer *xfer;
    142  1.23  thorpej 	int s;
    143  1.23  thorpej 
    144  1.23  thorpej 	s = splbio();
    145  1.23  thorpej 	chp->ch_flags |= WDCF_TH_RUN;
    146  1.23  thorpej 	splx(s);
    147  1.23  thorpej 
    148  1.23  thorpej 	/* Configure the devices on the bus. */
    149  1.23  thorpej 	atabusconfig(sc);
    150  1.23  thorpej 
    151  1.23  thorpej 	for (;;) {
    152  1.23  thorpej 		s = splbio();
    153  1.23  thorpej 		if ((chp->ch_flags & (WDCF_TH_RESET | WDCF_SHUTDOWN)) == 0 &&
    154  1.33   bouyer 		    (chp->ch_queue->active_xfer == NULL ||
    155  1.23  thorpej 		     chp->ch_queue->queue_freeze == 0)) {
    156  1.23  thorpej 			chp->ch_flags &= ~WDCF_TH_RUN;
    157  1.27  thorpej 			(void) tsleep(&chp->ch_thread, PRIBIO, "atath", 0);
    158  1.23  thorpej 			chp->ch_flags |= WDCF_TH_RUN;
    159  1.23  thorpej 		}
    160  1.23  thorpej 		splx(s);
    161  1.23  thorpej 		if (chp->ch_flags & WDCF_SHUTDOWN)
    162  1.23  thorpej 			break;
    163  1.23  thorpej 		s = splbio();
    164  1.23  thorpej 		if (chp->ch_flags & WDCF_TH_RESET) {
    165  1.31   bouyer 			/*
    166  1.31   bouyer 			 * wdc_reset_channel() will freeze 2 times, so
    167  1.31   bouyer 			 * unfreeze one time. Not a problem as we're at splbio
    168  1.31   bouyer 			 */
    169  1.23  thorpej 			chp->ch_queue->queue_freeze--;
    170  1.31   bouyer 			wdc_reset_channel(chp, AT_WAIT | chp->ch_reset_flags);
    171  1.33   bouyer 		} else if (chp->ch_queue->active_xfer != NULL &&
    172  1.23  thorpej 			   chp->ch_queue->queue_freeze == 1) {
    173  1.23  thorpej 			/*
    174  1.23  thorpej 			 * Caller has bumped queue_freeze, decrease it.
    175  1.23  thorpej 			 */
    176  1.23  thorpej 			chp->ch_queue->queue_freeze--;
    177  1.33   bouyer 			xfer = chp->ch_queue->active_xfer;
    178  1.23  thorpej 			KASSERT(xfer != NULL);
    179  1.23  thorpej 			(*xfer->c_start)(chp, xfer);
    180  1.23  thorpej 		} else if (chp->ch_queue->queue_freeze > 1)
    181  1.23  thorpej 			panic("ata_thread: queue_freeze");
    182  1.23  thorpej 		splx(s);
    183  1.23  thorpej 	}
    184  1.27  thorpej 	chp->ch_thread = NULL;
    185  1.28   bouyer 	wakeup((void *)&chp->ch_flags);
    186  1.23  thorpej 	kthread_exit(0);
    187  1.23  thorpej }
    188  1.23  thorpej 
    189  1.23  thorpej /*
    190  1.23  thorpej  * atabus_create_thread:
    191  1.23  thorpej  *
    192  1.23  thorpej  *	Helper routine to create the ATA bus worker thread.
    193  1.23  thorpej  */
    194  1.23  thorpej static void
    195  1.23  thorpej atabus_create_thread(void *arg)
    196  1.23  thorpej {
    197  1.23  thorpej 	struct atabus_softc *sc = arg;
    198  1.25  thorpej 	struct wdc_channel *chp = sc->sc_chan;
    199  1.23  thorpej 	int error;
    200  1.23  thorpej 
    201  1.27  thorpej 	if ((error = kthread_create1(atabus_thread, sc, &chp->ch_thread,
    202  1.23  thorpej 				     "%s", sc->sc_dev.dv_xname)) != 0)
    203  1.23  thorpej 		aprint_error("%s: unable to create kernel thread: error %d\n",
    204  1.23  thorpej 		    sc->sc_dev.dv_xname, error);
    205  1.23  thorpej }
    206  1.23  thorpej 
    207  1.23  thorpej /*
    208  1.23  thorpej  * atabus_match:
    209  1.23  thorpej  *
    210  1.23  thorpej  *	Autoconfiguration match routine.
    211  1.23  thorpej  */
    212  1.23  thorpej static int
    213  1.23  thorpej atabus_match(struct device *parent, struct cfdata *cf, void *aux)
    214  1.23  thorpej {
    215  1.25  thorpej 	struct wdc_channel *chp = aux;
    216  1.23  thorpej 
    217  1.23  thorpej 	if (chp == NULL)
    218  1.23  thorpej 		return (0);
    219  1.23  thorpej 
    220  1.26  thorpej 	if (cf->cf_loc[ATACF_CHANNEL] != chp->ch_channel &&
    221  1.23  thorpej 	    cf->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT)
    222  1.23  thorpej 	    	return (0);
    223  1.23  thorpej 
    224  1.23  thorpej 	return (1);
    225  1.23  thorpej }
    226  1.23  thorpej 
    227  1.23  thorpej /*
    228  1.23  thorpej  * atabus_attach:
    229  1.23  thorpej  *
    230  1.23  thorpej  *	Autoconfiguration attach routine.
    231  1.23  thorpej  */
    232  1.23  thorpej static void
    233  1.23  thorpej atabus_attach(struct device *parent, struct device *self, void *aux)
    234  1.23  thorpej {
    235  1.23  thorpej 	struct atabus_softc *sc = (void *) self;
    236  1.25  thorpej 	struct wdc_channel *chp = aux;
    237  1.23  thorpej 	struct atabus_initq *initq;
    238  1.23  thorpej 
    239  1.23  thorpej 	sc->sc_chan = chp;
    240  1.23  thorpej 
    241  1.23  thorpej 	aprint_normal("\n");
    242  1.23  thorpej 	aprint_naive("\n");
    243  1.23  thorpej 
    244  1.43  thorpej         if (ata_addref(chp))
    245  1.35  mycroft                 return;
    246  1.35  mycroft 
    247  1.23  thorpej 	initq = malloc(sizeof(*initq), M_DEVBUF, M_WAITOK);
    248  1.23  thorpej 	initq->atabus_sc = sc;
    249  1.23  thorpej 	TAILQ_INSERT_TAIL(&atabus_initq_head, initq, atabus_initq);
    250  1.23  thorpej 	config_pending_incr();
    251  1.23  thorpej 	kthread_create(atabus_create_thread, sc);
    252  1.23  thorpej }
    253  1.23  thorpej 
    254  1.23  thorpej /*
    255  1.23  thorpej  * atabus_activate:
    256  1.23  thorpej  *
    257  1.23  thorpej  *	Autoconfiguration activation routine.
    258  1.23  thorpej  */
    259  1.23  thorpej static int
    260  1.23  thorpej atabus_activate(struct device *self, enum devact act)
    261  1.23  thorpej {
    262  1.23  thorpej 	struct atabus_softc *sc = (void *) self;
    263  1.25  thorpej 	struct wdc_channel *chp = sc->sc_chan;
    264  1.23  thorpej 	struct device *dev = NULL;
    265  1.23  thorpej 	int s, i, error = 0;
    266  1.23  thorpej 
    267  1.23  thorpej 	s = splbio();
    268  1.23  thorpej 	switch (act) {
    269  1.23  thorpej 	case DVACT_ACTIVATE:
    270  1.23  thorpej 		error = EOPNOTSUPP;
    271  1.23  thorpej 		break;
    272  1.23  thorpej 
    273  1.23  thorpej 	case DVACT_DEACTIVATE:
    274  1.23  thorpej 		/*
    275  1.23  thorpej 		 * We might deactivate the children of atapibus twice
    276  1.23  thorpej 		 * (once bia atapibus, once directly), but since the
    277  1.23  thorpej 		 * generic autoconfiguration code maintains the DVF_ACTIVE
    278  1.23  thorpej 		 * flag, it's safe.
    279  1.23  thorpej 		 */
    280  1.23  thorpej 		if ((dev = chp->atapibus) != NULL) {
    281  1.23  thorpej 			error = config_deactivate(dev);
    282  1.23  thorpej 			if (error)
    283  1.23  thorpej 				goto out;
    284  1.23  thorpej 		}
    285  1.23  thorpej 
    286  1.23  thorpej 		for (i = 0; i < 2; i++) {
    287  1.23  thorpej 			if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
    288  1.23  thorpej 				WDCDEBUG_PRINT(("atabus_activate: %s: "
    289  1.23  thorpej 				    "deactivating %s\n", sc->sc_dev.dv_xname,
    290  1.23  thorpej 				    dev->dv_xname),
    291  1.23  thorpej 				    DEBUG_DETACH);
    292  1.23  thorpej 				error = config_deactivate(dev);
    293  1.23  thorpej 				if (error)
    294  1.23  thorpej 					goto out;
    295  1.23  thorpej 			}
    296  1.23  thorpej 		}
    297  1.23  thorpej 		break;
    298  1.23  thorpej 	}
    299  1.23  thorpej  out:
    300  1.23  thorpej 	splx(s);
    301  1.23  thorpej 
    302  1.23  thorpej #ifdef WDCDEBUG
    303  1.23  thorpej 	if (dev != NULL && error != 0)
    304  1.23  thorpej 		WDCDEBUG_PRINT(("atabus_activate: %s: "
    305  1.23  thorpej 		    "error %d deactivating %s\n", sc->sc_dev.dv_xname,
    306  1.23  thorpej 		    error, dev->dv_xname), DEBUG_DETACH);
    307  1.23  thorpej #endif /* WDCDEBUG */
    308  1.23  thorpej 
    309  1.23  thorpej 	return (error);
    310  1.23  thorpej }
    311  1.23  thorpej 
    312  1.23  thorpej /*
    313  1.23  thorpej  * atabus_detach:
    314  1.23  thorpej  *
    315  1.23  thorpej  *	Autoconfiguration detach routine.
    316  1.23  thorpej  */
    317  1.23  thorpej static int
    318  1.23  thorpej atabus_detach(struct device *self, int flags)
    319  1.23  thorpej {
    320  1.23  thorpej 	struct atabus_softc *sc = (void *) self;
    321  1.25  thorpej 	struct wdc_channel *chp = sc->sc_chan;
    322  1.23  thorpej 	struct device *dev = NULL;
    323  1.23  thorpej 	int i, error = 0;
    324  1.23  thorpej 
    325  1.23  thorpej 	/* Shutdown the channel. */
    326  1.23  thorpej 	/* XXX NEED AN INTERLOCK HERE. */
    327  1.23  thorpej 	chp->ch_flags |= WDCF_SHUTDOWN;
    328  1.27  thorpej 	wakeup(&chp->ch_thread);
    329  1.27  thorpej 	while (chp->ch_thread != NULL)
    330  1.28   bouyer 		(void) tsleep((void *)&chp->ch_flags, PRIBIO, "atadown", 0);
    331  1.23  thorpej 
    332  1.23  thorpej 	/*
    333  1.23  thorpej 	 * Detach atapibus and its children.
    334  1.23  thorpej 	 */
    335  1.23  thorpej 	if ((dev = chp->atapibus) != NULL) {
    336  1.23  thorpej 		WDCDEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
    337  1.23  thorpej 		    sc->sc_dev.dv_xname, dev->dv_xname), DEBUG_DETACH);
    338  1.23  thorpej 		error = config_detach(dev, flags);
    339  1.23  thorpej 		if (error)
    340  1.23  thorpej 			goto out;
    341  1.23  thorpej 	}
    342  1.23  thorpej 
    343  1.23  thorpej 	/*
    344  1.23  thorpej 	 * Detach our other children.
    345  1.23  thorpej 	 */
    346  1.23  thorpej 	for (i = 0; i < 2; i++) {
    347  1.23  thorpej 		if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI)
    348  1.23  thorpej 			continue;
    349  1.23  thorpej 		if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
    350  1.23  thorpej 			WDCDEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
    351  1.23  thorpej 			    sc->sc_dev.dv_xname, dev->dv_xname),
    352  1.23  thorpej 			    DEBUG_DETACH);
    353  1.23  thorpej 			error = config_detach(dev, flags);
    354  1.23  thorpej 			if (error)
    355  1.23  thorpej 				goto out;
    356  1.23  thorpej 		}
    357  1.23  thorpej 	}
    358  1.23  thorpej 
    359  1.23  thorpej  out:
    360  1.23  thorpej #ifdef WDCDEBUG
    361  1.23  thorpej 	if (dev != NULL && error != 0)
    362  1.23  thorpej 		WDCDEBUG_PRINT(("atabus_detach: %s: error %d detaching %s\n",
    363  1.23  thorpej 		    sc->sc_dev.dv_xname, error, dev->dv_xname),
    364  1.23  thorpej 		    DEBUG_DETACH);
    365  1.23  thorpej #endif /* WDCDEBUG */
    366  1.23  thorpej 
    367  1.23  thorpej 	return (error);
    368  1.23  thorpej }
    369  1.23  thorpej 
    370  1.23  thorpej CFATTACH_DECL(atabus, sizeof(struct atabus_softc),
    371  1.23  thorpej     atabus_match, atabus_attach, atabus_detach, atabus_activate);
    372  1.23  thorpej 
    373  1.23  thorpej /*****************************************************************************
    374  1.23  thorpej  * Common ATA bus operations.
    375  1.23  thorpej  *****************************************************************************/
    376  1.23  thorpej 
    377   1.2   bouyer /* Get the disk's parameters */
    378   1.2   bouyer int
    379  1.21  thorpej ata_get_params(struct ata_drive_datas *drvp, u_int8_t flags,
    380  1.21  thorpej     struct ataparams *prms)
    381   1.2   bouyer {
    382   1.2   bouyer 	char tb[DEV_BSIZE];
    383  1.36  thorpej 	struct ata_command ata_c;
    384   1.2   bouyer 
    385   1.2   bouyer #if BYTE_ORDER == LITTLE_ENDIAN
    386   1.2   bouyer 	int i;
    387   1.2   bouyer 	u_int16_t *p;
    388   1.2   bouyer #endif
    389   1.2   bouyer 
    390  1.22  thorpej 	WDCDEBUG_PRINT(("ata_get_parms\n"), DEBUG_FUNCS);
    391   1.2   bouyer 
    392   1.2   bouyer 	memset(tb, 0, DEV_BSIZE);
    393   1.2   bouyer 	memset(prms, 0, sizeof(struct ataparams));
    394  1.36  thorpej 	memset(&ata_c, 0, sizeof(struct ata_command));
    395   1.2   bouyer 
    396   1.2   bouyer 	if (drvp->drive_flags & DRIVE_ATA) {
    397  1.36  thorpej 		ata_c.r_command = WDCC_IDENTIFY;
    398  1.36  thorpej 		ata_c.r_st_bmask = WDCS_DRDY;
    399  1.36  thorpej 		ata_c.r_st_pmask = 0;
    400  1.36  thorpej 		ata_c.timeout = 3000; /* 3s */
    401   1.7   bouyer 	} else if (drvp->drive_flags & DRIVE_ATAPI) {
    402  1.36  thorpej 		ata_c.r_command = ATAPI_IDENTIFY_DEVICE;
    403  1.36  thorpej 		ata_c.r_st_bmask = 0;
    404  1.36  thorpej 		ata_c.r_st_pmask = 0;
    405  1.36  thorpej 		ata_c.timeout = 10000; /* 10s */
    406   1.7   bouyer 	} else {
    407  1.22  thorpej 		WDCDEBUG_PRINT(("ata_get_parms: no disks\n"),
    408  1.10   bouyer 		    DEBUG_FUNCS|DEBUG_PROBE);
    409   1.7   bouyer 		return CMD_ERR;
    410   1.2   bouyer 	}
    411  1.36  thorpej 	ata_c.flags = AT_READ | flags;
    412  1.36  thorpej 	ata_c.data = tb;
    413  1.36  thorpej 	ata_c.bcount = DEV_BSIZE;
    414  1.37  thorpej 	if (wdc_exec_command(drvp, &ata_c) != ATACMD_COMPLETE) {
    415  1.22  thorpej 		WDCDEBUG_PRINT(("ata_get_parms: wdc_exec_command failed\n"),
    416  1.10   bouyer 		    DEBUG_FUNCS|DEBUG_PROBE);
    417   1.2   bouyer 		return CMD_AGAIN;
    418  1.10   bouyer 	}
    419  1.36  thorpej 	if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    420  1.36  thorpej 		WDCDEBUG_PRINT(("ata_get_parms: ata_c.flags=0x%x\n",
    421  1.36  thorpej 		    ata_c.flags), DEBUG_FUNCS|DEBUG_PROBE);
    422   1.2   bouyer 		return CMD_ERR;
    423   1.2   bouyer 	} else {
    424  1.17   bouyer 		/* if we didn't read any data something is wrong */
    425  1.36  thorpej 		if ((ata_c.flags & AT_XFDONE) == 0)
    426  1.17   bouyer 			return CMD_ERR;
    427   1.2   bouyer 		/* Read in parameter block. */
    428   1.2   bouyer 		memcpy(prms, tb, sizeof(struct ataparams));
    429   1.2   bouyer #if BYTE_ORDER == LITTLE_ENDIAN
    430   1.2   bouyer 		/*
    431   1.2   bouyer 		 * Shuffle string byte order.
    432   1.2   bouyer 		 * ATAPI Mitsumi and NEC drives don't need this.
    433   1.2   bouyer 		 */
    434   1.2   bouyer 		if ((prms->atap_config & WDC_CFG_ATAPI_MASK) ==
    435   1.2   bouyer 		    WDC_CFG_ATAPI &&
    436   1.2   bouyer 		    ((prms->atap_model[0] == 'N' &&
    437   1.2   bouyer 			prms->atap_model[1] == 'E') ||
    438   1.2   bouyer 		     (prms->atap_model[0] == 'F' &&
    439   1.2   bouyer 			 prms->atap_model[1] == 'X')))
    440   1.2   bouyer 			return 0;
    441   1.2   bouyer 		for (i = 0; i < sizeof(prms->atap_model); i += 2) {
    442   1.2   bouyer 			p = (u_short *)(prms->atap_model + i);
    443   1.2   bouyer 			*p = ntohs(*p);
    444   1.2   bouyer 		}
    445   1.2   bouyer 		for (i = 0; i < sizeof(prms->atap_serial); i += 2) {
    446   1.2   bouyer 			p = (u_short *)(prms->atap_serial + i);
    447   1.2   bouyer 			*p = ntohs(*p);
    448   1.2   bouyer 		}
    449   1.2   bouyer 		for (i = 0; i < sizeof(prms->atap_revision); i += 2) {
    450   1.2   bouyer 			p = (u_short *)(prms->atap_revision + i);
    451   1.2   bouyer 			*p = ntohs(*p);
    452   1.2   bouyer 		}
    453   1.2   bouyer #endif
    454   1.2   bouyer 		return CMD_OK;
    455   1.2   bouyer 	}
    456   1.2   bouyer }
    457   1.2   bouyer 
    458   1.2   bouyer int
    459  1.21  thorpej ata_set_mode(struct ata_drive_datas *drvp, u_int8_t mode, u_int8_t flags)
    460   1.2   bouyer {
    461  1.36  thorpej 	struct ata_command ata_c;
    462   1.2   bouyer 
    463  1.22  thorpej 	WDCDEBUG_PRINT(("ata_set_mode=0x%x\n", mode), DEBUG_FUNCS);
    464  1.36  thorpej 	memset(&ata_c, 0, sizeof(struct ata_command));
    465   1.2   bouyer 
    466  1.36  thorpej 	ata_c.r_command = SET_FEATURES;
    467  1.36  thorpej 	ata_c.r_st_bmask = 0;
    468  1.36  thorpej 	ata_c.r_st_pmask = 0;
    469  1.36  thorpej 	ata_c.r_features = WDSF_SET_MODE;
    470  1.36  thorpej 	ata_c.r_count = mode;
    471  1.36  thorpej 	ata_c.flags = flags;
    472  1.36  thorpej 	ata_c.timeout = 1000; /* 1s */
    473  1.37  thorpej 	if (wdc_exec_command(drvp, &ata_c) != ATACMD_COMPLETE)
    474   1.2   bouyer 		return CMD_AGAIN;
    475  1.36  thorpej 	if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    476   1.2   bouyer 		return CMD_ERR;
    477   1.2   bouyer 	}
    478   1.2   bouyer 	return CMD_OK;
    479  1.11   bouyer }
    480  1.11   bouyer 
    481  1.11   bouyer void
    482  1.21  thorpej ata_dmaerr(struct ata_drive_datas *drvp, int flags)
    483  1.11   bouyer {
    484  1.11   bouyer 	/*
    485  1.11   bouyer 	 * Downgrade decision: if we get NERRS_MAX in NXFER.
    486  1.11   bouyer 	 * We start with n_dmaerrs set to NERRS_MAX-1 so that the
    487  1.11   bouyer 	 * first error within the first NXFER ops will immediatly trigger
    488  1.11   bouyer 	 * a downgrade.
    489  1.11   bouyer 	 * If we got an error and n_xfers is bigger than NXFER reset counters.
    490  1.11   bouyer 	 */
    491  1.11   bouyer 	drvp->n_dmaerrs++;
    492  1.11   bouyer 	if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) {
    493  1.39  thorpej 		ata_downgrade_mode(drvp, flags);
    494  1.11   bouyer 		drvp->n_dmaerrs = NERRS_MAX-1;
    495  1.11   bouyer 		drvp->n_xfers = 0;
    496  1.11   bouyer 		return;
    497  1.11   bouyer 	}
    498  1.11   bouyer 	if (drvp->n_xfers > NXFER) {
    499  1.11   bouyer 		drvp->n_dmaerrs = 1; /* just got an error */
    500  1.11   bouyer 		drvp->n_xfers = 1; /* restart counting from this error */
    501   1.4   bouyer 	}
    502   1.2   bouyer }
    503  1.30   bouyer 
    504  1.44  thorpej /*
    505  1.44  thorpej  * Add a command to the queue and start controller. Must be called at splbio
    506  1.44  thorpej  */
    507  1.44  thorpej void
    508  1.44  thorpej ata_exec_xfer(struct wdc_channel *chp, struct ata_xfer *xfer)
    509  1.44  thorpej {
    510  1.44  thorpej 
    511  1.44  thorpej 	WDCDEBUG_PRINT(("ata_exec_xfer %p channel %d drive %d\n", xfer,
    512  1.44  thorpej 	    chp->ch_channel, xfer->c_drive), DEBUG_XFERS);
    513  1.44  thorpej 
    514  1.44  thorpej 	/* complete xfer setup */
    515  1.44  thorpej 	xfer->c_chp = chp;
    516  1.44  thorpej 
    517  1.44  thorpej 	/* insert at the end of command list */
    518  1.44  thorpej 	TAILQ_INSERT_TAIL(&chp->ch_queue->queue_xfer, xfer, c_xferchain);
    519  1.45  thorpej 	WDCDEBUG_PRINT(("atastart from ata_exec_xfer, flags 0x%x\n",
    520  1.44  thorpej 	    chp->ch_flags), DEBUG_XFERS);
    521  1.45  thorpej 	atastart(chp);
    522  1.45  thorpej }
    523  1.45  thorpej 
    524  1.45  thorpej /*
    525  1.45  thorpej  * Start I/O on a controller, for the given channel.
    526  1.45  thorpej  * The first xfer may be not for our channel if the channel queues
    527  1.45  thorpej  * are shared.
    528  1.45  thorpej  */
    529  1.45  thorpej void
    530  1.45  thorpej atastart(struct wdc_channel *chp)
    531  1.45  thorpej {
    532  1.45  thorpej 	struct wdc_softc *wdc = chp->ch_wdc;
    533  1.45  thorpej 	struct ata_xfer *xfer;
    534  1.45  thorpej 
    535  1.45  thorpej #ifdef WDC_DIAGNOSTIC
    536  1.45  thorpej 	int spl1, spl2;
    537  1.45  thorpej 
    538  1.45  thorpej 	spl1 = splbio();
    539  1.45  thorpej 	spl2 = splbio();
    540  1.45  thorpej 	if (spl2 != spl1) {
    541  1.45  thorpej 		printf("atastart: not at splbio()\n");
    542  1.45  thorpej 		panic("atastart");
    543  1.45  thorpej 	}
    544  1.45  thorpej 	splx(spl2);
    545  1.45  thorpej 	splx(spl1);
    546  1.45  thorpej #endif /* WDC_DIAGNOSTIC */
    547  1.45  thorpej 
    548  1.45  thorpej 	/* is there a xfer ? */
    549  1.45  thorpej 	if ((xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer)) == NULL)
    550  1.45  thorpej 		return;
    551  1.45  thorpej 
    552  1.45  thorpej 	/* adjust chp, in case we have a shared queue */
    553  1.45  thorpej 	chp = xfer->c_chp;
    554  1.45  thorpej 
    555  1.45  thorpej 	if (chp->ch_queue->active_xfer != NULL) {
    556  1.45  thorpej 		return; /* channel aleady active */
    557  1.45  thorpej 	}
    558  1.45  thorpej 	if (__predict_false(chp->ch_queue->queue_freeze > 0)) {
    559  1.45  thorpej 		return; /* queue froozen */
    560  1.45  thorpej 	}
    561  1.45  thorpej #ifdef DIAGNOSTIC
    562  1.45  thorpej 	if ((chp->ch_flags & WDCF_IRQ_WAIT) != 0)
    563  1.45  thorpej 		panic("atastart: channel waiting for irq");
    564  1.45  thorpej #endif
    565  1.46  thorpej 	if (wdc->claim_hw)
    566  1.45  thorpej 		if (!(*wdc->claim_hw)(chp, 0))
    567  1.45  thorpej 			return;
    568  1.45  thorpej 
    569  1.45  thorpej 	WDCDEBUG_PRINT(("atastart: xfer %p channel %d drive %d\n", xfer,
    570  1.45  thorpej 	    chp->ch_channel, xfer->c_drive), DEBUG_XFERS);
    571  1.45  thorpej 	if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_RESET) {
    572  1.45  thorpej 		chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_RESET;
    573  1.45  thorpej 		chp->ch_drive[xfer->c_drive].state = 0;
    574  1.45  thorpej 	}
    575  1.45  thorpej 	chp->ch_queue->active_xfer = xfer;
    576  1.45  thorpej 	TAILQ_REMOVE(&chp->ch_queue->queue_xfer, xfer, c_xferchain);
    577  1.45  thorpej 
    578  1.45  thorpej 	if (wdc->cap & WDC_CAPABILITY_NOIRQ)
    579  1.45  thorpej 		KASSERT(xfer->c_flags & C_POLL);
    580  1.45  thorpej 	xfer->c_start(chp, xfer);
    581  1.44  thorpej }
    582  1.44  thorpej 
    583  1.41  thorpej struct ata_xfer *
    584  1.41  thorpej ata_get_xfer(int flags)
    585  1.41  thorpej {
    586  1.41  thorpej 	struct ata_xfer *xfer;
    587  1.41  thorpej 	int s;
    588  1.41  thorpej 
    589  1.41  thorpej 	s = splbio();
    590  1.41  thorpej 	xfer = pool_get(&ata_xfer_pool,
    591  1.41  thorpej 	    ((flags & ATAXF_NOSLEEP) != 0 ? PR_NOWAIT : PR_WAITOK));
    592  1.41  thorpej 	splx(s);
    593  1.41  thorpej 	if (xfer != NULL) {
    594  1.41  thorpej 		memset(xfer, 0, sizeof(struct ata_xfer));
    595  1.41  thorpej 	}
    596  1.41  thorpej 	return xfer;
    597  1.41  thorpej }
    598  1.41  thorpej 
    599  1.41  thorpej void
    600  1.41  thorpej ata_free_xfer(struct wdc_channel *chp, struct ata_xfer *xfer)
    601  1.41  thorpej {
    602  1.41  thorpej 	struct wdc_softc *wdc = chp->ch_wdc;
    603  1.41  thorpej 	int s;
    604  1.41  thorpej 
    605  1.46  thorpej 	if (wdc->free_hw)
    606  1.41  thorpej 		(*wdc->free_hw)(chp);
    607  1.41  thorpej 	s = splbio();
    608  1.41  thorpej 	pool_put(&ata_xfer_pool, xfer);
    609  1.41  thorpej 	splx(s);
    610  1.41  thorpej }
    611  1.41  thorpej 
    612  1.42  thorpej /*
    613  1.42  thorpej  * Kill off all pending xfers for a wdc_channel.
    614  1.42  thorpej  *
    615  1.42  thorpej  * Must be called at splbio().
    616  1.42  thorpej  */
    617  1.42  thorpej void
    618  1.42  thorpej ata_kill_pending(struct ata_drive_datas *drvp)
    619  1.42  thorpej {
    620  1.42  thorpej 	struct wdc_channel *chp = drvp->chnl_softc;
    621  1.42  thorpej 	struct ata_xfer *xfer, *next_xfer;
    622  1.42  thorpej 	int s = splbio();
    623  1.42  thorpej 
    624  1.42  thorpej 	for (xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer);
    625  1.42  thorpej 	    xfer != NULL; xfer = next_xfer) {
    626  1.42  thorpej 		next_xfer = TAILQ_NEXT(xfer, c_xferchain);
    627  1.42  thorpej 		if (xfer->c_chp != chp || xfer->c_drive != drvp->drive)
    628  1.42  thorpej 			continue;
    629  1.42  thorpej 		TAILQ_REMOVE(&chp->ch_queue->queue_xfer, xfer, c_xferchain);
    630  1.42  thorpej 		(*xfer->c_kill_xfer)(chp, xfer, KILL_GONE);
    631  1.42  thorpej 	}
    632  1.42  thorpej 
    633  1.42  thorpej 	while ((xfer = chp->ch_queue->active_xfer) != NULL) {
    634  1.42  thorpej 		if (xfer->c_chp == chp && xfer->c_drive == drvp->drive) {
    635  1.42  thorpej 			drvp->drive_flags |= DRIVE_WAITDRAIN;
    636  1.42  thorpej 			(void) tsleep(&chp->ch_queue->active_xfer,
    637  1.42  thorpej 			    PRIBIO, "atdrn", 0);
    638  1.42  thorpej 		} else {
    639  1.42  thorpej 			/* no more xfer for us */
    640  1.42  thorpej 			break;
    641  1.42  thorpej 		}
    642  1.42  thorpej 	}
    643  1.42  thorpej 	splx(s);
    644  1.42  thorpej }
    645  1.42  thorpej 
    646  1.43  thorpej int
    647  1.43  thorpej ata_addref(struct wdc_channel *chp)
    648  1.43  thorpej {
    649  1.43  thorpej 	struct wdc_softc *wdc = chp->ch_wdc;
    650  1.43  thorpej 	struct scsipi_adapter *adapt = &wdc->sc_atapi_adapter._generic;
    651  1.43  thorpej 	int s, error = 0;
    652  1.43  thorpej 
    653  1.43  thorpej 	s = splbio();
    654  1.43  thorpej 	if (adapt->adapt_refcnt++ == 0 &&
    655  1.43  thorpej 	    adapt->adapt_enable != NULL) {
    656  1.43  thorpej 		error = (*adapt->adapt_enable)(&wdc->sc_dev, 1);
    657  1.43  thorpej 		if (error)
    658  1.43  thorpej 			adapt->adapt_refcnt--;
    659  1.43  thorpej 	}
    660  1.43  thorpej 	splx(s);
    661  1.43  thorpej 	return (error);
    662  1.43  thorpej }
    663  1.43  thorpej 
    664  1.43  thorpej void
    665  1.43  thorpej ata_delref(struct wdc_channel *chp)
    666  1.43  thorpej {
    667  1.43  thorpej 	struct wdc_softc *wdc = chp->ch_wdc;
    668  1.43  thorpej 	struct scsipi_adapter *adapt = &wdc->sc_atapi_adapter._generic;
    669  1.43  thorpej 	int s;
    670  1.43  thorpej 
    671  1.43  thorpej 	s = splbio();
    672  1.43  thorpej 	if (adapt->adapt_refcnt-- == 1 &&
    673  1.43  thorpej 	    adapt->adapt_enable != NULL)
    674  1.43  thorpej 		(void) (*adapt->adapt_enable)(&wdc->sc_dev, 0);
    675  1.43  thorpej 	splx(s);
    676  1.43  thorpej }
    677  1.43  thorpej 
    678  1.38  thorpej void
    679  1.38  thorpej ata_print_modes(struct wdc_channel *chp)
    680  1.38  thorpej {
    681  1.38  thorpej 	struct wdc_softc *wdc = chp->ch_wdc;
    682  1.38  thorpej 	int drive;
    683  1.38  thorpej 	struct ata_drive_datas *drvp;
    684  1.38  thorpej 
    685  1.38  thorpej 	for (drive = 0; drive < 2; drive++) {
    686  1.38  thorpej 		drvp = &chp->ch_drive[drive];
    687  1.38  thorpej 		if ((drvp->drive_flags & DRIVE) == 0)
    688  1.38  thorpej 			continue;
    689  1.38  thorpej 		aprint_normal("%s(%s:%d:%d): using PIO mode %d",
    690  1.38  thorpej 			drvp->drv_softc->dv_xname,
    691  1.38  thorpej 			wdc->sc_dev.dv_xname,
    692  1.38  thorpej 			chp->ch_channel, drive, drvp->PIO_mode);
    693  1.38  thorpej 		if (drvp->drive_flags & DRIVE_DMA)
    694  1.38  thorpej 			aprint_normal(", DMA mode %d", drvp->DMA_mode);
    695  1.38  thorpej 		if (drvp->drive_flags & DRIVE_UDMA) {
    696  1.38  thorpej 			aprint_normal(", Ultra-DMA mode %d", drvp->UDMA_mode);
    697  1.38  thorpej 			if (drvp->UDMA_mode == 2)
    698  1.38  thorpej 				aprint_normal(" (Ultra/33)");
    699  1.38  thorpej 			else if (drvp->UDMA_mode == 4)
    700  1.38  thorpej 				aprint_normal(" (Ultra/66)");
    701  1.38  thorpej 			else if (drvp->UDMA_mode == 5)
    702  1.38  thorpej 				aprint_normal(" (Ultra/100)");
    703  1.38  thorpej 			else if (drvp->UDMA_mode == 6)
    704  1.38  thorpej 				aprint_normal(" (Ultra/133)");
    705  1.38  thorpej 		}
    706  1.38  thorpej 		if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA))
    707  1.38  thorpej 			aprint_normal(" (using DMA data transfers)");
    708  1.38  thorpej 		aprint_normal("\n");
    709  1.38  thorpej 	}
    710  1.38  thorpej }
    711  1.38  thorpej 
    712  1.39  thorpej /*
    713  1.39  thorpej  * downgrade the transfer mode of a drive after an error. return 1 if
    714  1.39  thorpej  * downgrade was possible, 0 otherwise.
    715  1.39  thorpej  */
    716  1.39  thorpej int
    717  1.39  thorpej ata_downgrade_mode(struct ata_drive_datas *drvp, int flags)
    718  1.39  thorpej {
    719  1.39  thorpej 	struct wdc_channel *chp = drvp->chnl_softc;
    720  1.39  thorpej 	struct wdc_softc *wdc = chp->ch_wdc;
    721  1.39  thorpej 	struct device *drv_dev = drvp->drv_softc;
    722  1.39  thorpej 	int cf_flags = drv_dev->dv_cfdata->cf_flags;
    723  1.39  thorpej 
    724  1.39  thorpej 	/* if drive or controller don't know its mode, we can't do much */
    725  1.39  thorpej 	if ((drvp->drive_flags & DRIVE_MODE) == 0 ||
    726  1.46  thorpej 	    (wdc->set_modes == NULL))
    727  1.39  thorpej 		return 0;
    728  1.39  thorpej 	/* current drive mode was set by a config flag, let it this way */
    729  1.39  thorpej 	if ((cf_flags & ATA_CONFIG_PIO_SET) ||
    730  1.39  thorpej 	    (cf_flags & ATA_CONFIG_DMA_SET) ||
    731  1.39  thorpej 	    (cf_flags & ATA_CONFIG_UDMA_SET))
    732  1.39  thorpej 		return 0;
    733  1.39  thorpej 
    734  1.39  thorpej 	/*
    735  1.39  thorpej 	 * If we were using Ultra-DMA mode, downgrade to the next lower mode.
    736  1.39  thorpej 	 */
    737  1.39  thorpej 	if ((drvp->drive_flags & DRIVE_UDMA) && drvp->UDMA_mode >= 2) {
    738  1.39  thorpej 		drvp->UDMA_mode--;
    739  1.39  thorpej 		printf("%s: transfer error, downgrading to Ultra-DMA mode %d\n",
    740  1.39  thorpej 		    drv_dev->dv_xname, drvp->UDMA_mode);
    741  1.39  thorpej 	}
    742  1.39  thorpej 
    743  1.39  thorpej 	/*
    744  1.39  thorpej 	 * If we were using ultra-DMA, don't downgrade to multiword DMA.
    745  1.39  thorpej 	 */
    746  1.39  thorpej 	else if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) {
    747  1.39  thorpej 		drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
    748  1.39  thorpej 		drvp->PIO_mode = drvp->PIO_cap;
    749  1.39  thorpej 		printf("%s: transfer error, downgrading to PIO mode %d\n",
    750  1.39  thorpej 		    drv_dev->dv_xname, drvp->PIO_mode);
    751  1.39  thorpej 	} else /* already using PIO, can't downgrade */
    752  1.39  thorpej 		return 0;
    753  1.39  thorpej 
    754  1.39  thorpej 	wdc->set_modes(chp);
    755  1.39  thorpej 	ata_print_modes(chp);
    756  1.39  thorpej 	/* reset the channel, which will shedule all drives for setup */
    757  1.39  thorpej 	wdc_reset_channel(chp, flags | AT_RST_NOCMD);
    758  1.39  thorpej 	return 1;
    759  1.39  thorpej }
    760  1.39  thorpej 
    761  1.40  thorpej /*
    762  1.40  thorpej  * Probe drive's capabilities, for use by the controller later
    763  1.40  thorpej  * Assumes drvp points to an existing drive.
    764  1.40  thorpej  */
    765  1.40  thorpej void
    766  1.40  thorpej ata_probe_caps(struct ata_drive_datas *drvp)
    767  1.40  thorpej {
    768  1.40  thorpej 	struct ataparams params, params2;
    769  1.40  thorpej 	struct wdc_channel *chp = drvp->chnl_softc;
    770  1.40  thorpej 	struct wdc_softc *wdc = chp->ch_wdc;
    771  1.40  thorpej 	struct device *drv_dev = drvp->drv_softc;
    772  1.40  thorpej 	int i, printed;
    773  1.40  thorpej 	char *sep = "";
    774  1.40  thorpej 	int cf_flags;
    775  1.40  thorpej 
    776  1.40  thorpej 	if (ata_get_params(drvp, AT_WAIT, &params) != CMD_OK) {
    777  1.40  thorpej 		/* IDENTIFY failed. Can't tell more about the device */
    778  1.40  thorpej 		return;
    779  1.40  thorpej 	}
    780  1.40  thorpej 	if ((wdc->cap & (WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32)) ==
    781  1.40  thorpej 	    (WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32)) {
    782  1.40  thorpej 		/*
    783  1.40  thorpej 		 * Controller claims 16 and 32 bit transfers.
    784  1.40  thorpej 		 * Re-do an IDENTIFY with 32-bit transfers,
    785  1.40  thorpej 		 * and compare results.
    786  1.40  thorpej 		 */
    787  1.40  thorpej 		drvp->drive_flags |= DRIVE_CAP32;
    788  1.40  thorpej 		ata_get_params(drvp, AT_WAIT, &params2);
    789  1.40  thorpej 		if (memcmp(&params, &params2, sizeof(struct ataparams)) != 0) {
    790  1.40  thorpej 			/* Not good. fall back to 16bits */
    791  1.40  thorpej 			drvp->drive_flags &= ~DRIVE_CAP32;
    792  1.40  thorpej 		} else {
    793  1.40  thorpej 			aprint_normal("%s: 32-bit data port\n",
    794  1.40  thorpej 			    drv_dev->dv_xname);
    795  1.40  thorpej 		}
    796  1.40  thorpej 	}
    797  1.40  thorpej #if 0 /* Some ultra-DMA drives claims to only support ATA-3. sigh */
    798  1.40  thorpej 	if (params.atap_ata_major > 0x01 &&
    799  1.40  thorpej 	    params.atap_ata_major != 0xffff) {
    800  1.40  thorpej 		for (i = 14; i > 0; i--) {
    801  1.40  thorpej 			if (params.atap_ata_major & (1 << i)) {
    802  1.40  thorpej 				aprint_normal("%s: ATA version %d\n",
    803  1.40  thorpej 				    drv_dev->dv_xname, i);
    804  1.40  thorpej 				drvp->ata_vers = i;
    805  1.40  thorpej 				break;
    806  1.40  thorpej 			}
    807  1.40  thorpej 		}
    808  1.40  thorpej 	}
    809  1.40  thorpej #endif
    810  1.40  thorpej 
    811  1.40  thorpej 	/* An ATAPI device is at last PIO mode 3 */
    812  1.40  thorpej 	if (drvp->drive_flags & DRIVE_ATAPI)
    813  1.40  thorpej 		drvp->PIO_mode = 3;
    814  1.40  thorpej 
    815  1.40  thorpej 	/*
    816  1.40  thorpej 	 * It's not in the specs, but it seems that some drive
    817  1.40  thorpej 	 * returns 0xffff in atap_extensions when this field is invalid
    818  1.40  thorpej 	 */
    819  1.40  thorpej 	if (params.atap_extensions != 0xffff &&
    820  1.40  thorpej 	    (params.atap_extensions & WDC_EXT_MODES)) {
    821  1.40  thorpej 		printed = 0;
    822  1.40  thorpej 		/*
    823  1.40  thorpej 		 * XXX some drives report something wrong here (they claim to
    824  1.40  thorpej 		 * support PIO mode 8 !). As mode is coded on 3 bits in
    825  1.40  thorpej 		 * SET FEATURE, limit it to 7 (so limit i to 4).
    826  1.40  thorpej 		 * If higher mode than 7 is found, abort.
    827  1.40  thorpej 		 */
    828  1.40  thorpej 		for (i = 7; i >= 0; i--) {
    829  1.40  thorpej 			if ((params.atap_piomode_supp & (1 << i)) == 0)
    830  1.40  thorpej 				continue;
    831  1.40  thorpej 			if (i > 4)
    832  1.40  thorpej 				return;
    833  1.40  thorpej 			/*
    834  1.40  thorpej 			 * See if mode is accepted.
    835  1.40  thorpej 			 * If the controller can't set its PIO mode,
    836  1.40  thorpej 			 * assume the defaults are good, so don't try
    837  1.40  thorpej 			 * to set it
    838  1.40  thorpej 			 */
    839  1.46  thorpej 			if (wdc->set_modes)
    840  1.40  thorpej 				/*
    841  1.40  thorpej 				 * It's OK to pool here, it's fast enouth
    842  1.40  thorpej 				 * to not bother waiting for interrupt
    843  1.40  thorpej 				 */
    844  1.40  thorpej 				if (ata_set_mode(drvp, 0x08 | (i + 3),
    845  1.40  thorpej 				   AT_WAIT) != CMD_OK)
    846  1.40  thorpej 					continue;
    847  1.40  thorpej 			if (!printed) {
    848  1.40  thorpej 				aprint_normal("%s: drive supports PIO mode %d",
    849  1.40  thorpej 				    drv_dev->dv_xname, i + 3);
    850  1.40  thorpej 				sep = ",";
    851  1.40  thorpej 				printed = 1;
    852  1.40  thorpej 			}
    853  1.40  thorpej 			/*
    854  1.40  thorpej 			 * If controller's driver can't set its PIO mode,
    855  1.40  thorpej 			 * get the highter one for the drive.
    856  1.40  thorpej 			 */
    857  1.46  thorpej 			if (wdc->set_modes == NULL || wdc->PIO_cap >= i + 3) {
    858  1.40  thorpej 				drvp->PIO_mode = i + 3;
    859  1.40  thorpej 				drvp->PIO_cap = i + 3;
    860  1.40  thorpej 				break;
    861  1.40  thorpej 			}
    862  1.40  thorpej 		}
    863  1.40  thorpej 		if (!printed) {
    864  1.40  thorpej 			/*
    865  1.40  thorpej 			 * We didn't find a valid PIO mode.
    866  1.40  thorpej 			 * Assume the values returned for DMA are buggy too
    867  1.40  thorpej 			 */
    868  1.40  thorpej 			return;
    869  1.40  thorpej 		}
    870  1.40  thorpej 		drvp->drive_flags |= DRIVE_MODE;
    871  1.40  thorpej 		printed = 0;
    872  1.40  thorpej 		for (i = 7; i >= 0; i--) {
    873  1.40  thorpej 			if ((params.atap_dmamode_supp & (1 << i)) == 0)
    874  1.40  thorpej 				continue;
    875  1.40  thorpej 			if ((wdc->cap & WDC_CAPABILITY_DMA) &&
    876  1.46  thorpej 			    wdc->set_modes != NULL)
    877  1.40  thorpej 				if (ata_set_mode(drvp, 0x20 | i, AT_WAIT)
    878  1.40  thorpej 				    != CMD_OK)
    879  1.40  thorpej 					continue;
    880  1.40  thorpej 			if (!printed) {
    881  1.40  thorpej 				aprint_normal("%s DMA mode %d", sep, i);
    882  1.40  thorpej 				sep = ",";
    883  1.40  thorpej 				printed = 1;
    884  1.40  thorpej 			}
    885  1.40  thorpej 			if (wdc->cap & WDC_CAPABILITY_DMA) {
    886  1.46  thorpej 				if (wdc->set_modes != NULL &&
    887  1.40  thorpej 				    wdc->DMA_cap < i)
    888  1.40  thorpej 					continue;
    889  1.40  thorpej 				drvp->DMA_mode = i;
    890  1.40  thorpej 				drvp->DMA_cap = i;
    891  1.40  thorpej 				drvp->drive_flags |= DRIVE_DMA;
    892  1.40  thorpej 			}
    893  1.40  thorpej 			break;
    894  1.40  thorpej 		}
    895  1.40  thorpej 		if (params.atap_extensions & WDC_EXT_UDMA_MODES) {
    896  1.40  thorpej 			printed = 0;
    897  1.40  thorpej 			for (i = 7; i >= 0; i--) {
    898  1.40  thorpej 				if ((params.atap_udmamode_supp & (1 << i))
    899  1.40  thorpej 				    == 0)
    900  1.40  thorpej 					continue;
    901  1.46  thorpej 				if (wdc->set_modes != NULL &&
    902  1.40  thorpej 				    (wdc->cap & WDC_CAPABILITY_UDMA))
    903  1.40  thorpej 					if (ata_set_mode(drvp, 0x40 | i,
    904  1.40  thorpej 					    AT_WAIT) != CMD_OK)
    905  1.40  thorpej 						continue;
    906  1.40  thorpej 				if (!printed) {
    907  1.40  thorpej 					aprint_normal("%s Ultra-DMA mode %d",
    908  1.40  thorpej 					    sep, i);
    909  1.40  thorpej 					if (i == 2)
    910  1.40  thorpej 						aprint_normal(" (Ultra/33)");
    911  1.40  thorpej 					else if (i == 4)
    912  1.40  thorpej 						aprint_normal(" (Ultra/66)");
    913  1.40  thorpej 					else if (i == 5)
    914  1.40  thorpej 						aprint_normal(" (Ultra/100)");
    915  1.40  thorpej 					else if (i == 6)
    916  1.40  thorpej 						aprint_normal(" (Ultra/133)");
    917  1.40  thorpej 					sep = ",";
    918  1.40  thorpej 					printed = 1;
    919  1.40  thorpej 				}
    920  1.40  thorpej 				if (wdc->cap & WDC_CAPABILITY_UDMA) {
    921  1.46  thorpej 					if (wdc->set_modes != NULL &&
    922  1.40  thorpej 					    wdc->UDMA_cap < i)
    923  1.40  thorpej 						continue;
    924  1.40  thorpej 					drvp->UDMA_mode = i;
    925  1.40  thorpej 					drvp->UDMA_cap = i;
    926  1.40  thorpej 					drvp->drive_flags |= DRIVE_UDMA;
    927  1.40  thorpej 				}
    928  1.40  thorpej 				break;
    929  1.40  thorpej 			}
    930  1.40  thorpej 		}
    931  1.40  thorpej 		aprint_normal("\n");
    932  1.40  thorpej 	}
    933  1.40  thorpej 
    934  1.40  thorpej 	drvp->drive_flags &= ~DRIVE_NOSTREAM;
    935  1.40  thorpej 	if (drvp->drive_flags & DRIVE_ATAPI) {
    936  1.40  thorpej 		if (wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)
    937  1.40  thorpej 			drvp->drive_flags |= DRIVE_NOSTREAM;
    938  1.40  thorpej 	} else {
    939  1.40  thorpej 		if (wdc->cap & WDC_CAPABILITY_ATA_NOSTREAM)
    940  1.40  thorpej 			drvp->drive_flags |= DRIVE_NOSTREAM;
    941  1.40  thorpej 	}
    942  1.40  thorpej 
    943  1.40  thorpej 	/* Try to guess ATA version here, if it didn't get reported */
    944  1.40  thorpej 	if (drvp->ata_vers == 0) {
    945  1.40  thorpej 		if (drvp->drive_flags & DRIVE_UDMA)
    946  1.40  thorpej 			drvp->ata_vers = 4; /* should be at last ATA-4 */
    947  1.40  thorpej 		else if (drvp->PIO_cap > 2)
    948  1.40  thorpej 			drvp->ata_vers = 2; /* should be at last ATA-2 */
    949  1.40  thorpej 	}
    950  1.40  thorpej 	cf_flags = drv_dev->dv_cfdata->cf_flags;
    951  1.40  thorpej 	if (cf_flags & ATA_CONFIG_PIO_SET) {
    952  1.40  thorpej 		drvp->PIO_mode =
    953  1.40  thorpej 		    (cf_flags & ATA_CONFIG_PIO_MODES) >> ATA_CONFIG_PIO_OFF;
    954  1.40  thorpej 		drvp->drive_flags |= DRIVE_MODE;
    955  1.40  thorpej 	}
    956  1.40  thorpej 	if ((wdc->cap & WDC_CAPABILITY_DMA) == 0) {
    957  1.40  thorpej 		/* don't care about DMA modes */
    958  1.40  thorpej 		return;
    959  1.40  thorpej 	}
    960  1.40  thorpej 	if (cf_flags & ATA_CONFIG_DMA_SET) {
    961  1.40  thorpej 		if ((cf_flags & ATA_CONFIG_DMA_MODES) ==
    962  1.40  thorpej 		    ATA_CONFIG_DMA_DISABLE) {
    963  1.40  thorpej 			drvp->drive_flags &= ~DRIVE_DMA;
    964  1.40  thorpej 		} else {
    965  1.40  thorpej 			drvp->DMA_mode = (cf_flags & ATA_CONFIG_DMA_MODES) >>
    966  1.40  thorpej 			    ATA_CONFIG_DMA_OFF;
    967  1.40  thorpej 			drvp->drive_flags |= DRIVE_DMA | DRIVE_MODE;
    968  1.40  thorpej 		}
    969  1.40  thorpej 	}
    970  1.40  thorpej 	if ((wdc->cap & WDC_CAPABILITY_UDMA) == 0) {
    971  1.40  thorpej 		/* don't care about UDMA modes */
    972  1.40  thorpej 		return;
    973  1.40  thorpej 	}
    974  1.40  thorpej 	if (cf_flags & ATA_CONFIG_UDMA_SET) {
    975  1.40  thorpej 		if ((cf_flags & ATA_CONFIG_UDMA_MODES) ==
    976  1.40  thorpej 		    ATA_CONFIG_UDMA_DISABLE) {
    977  1.40  thorpej 			drvp->drive_flags &= ~DRIVE_UDMA;
    978  1.40  thorpej 		} else {
    979  1.40  thorpej 			drvp->UDMA_mode = (cf_flags & ATA_CONFIG_UDMA_MODES) >>
    980  1.40  thorpej 			    ATA_CONFIG_UDMA_OFF;
    981  1.40  thorpej 			drvp->drive_flags |= DRIVE_UDMA | DRIVE_MODE;
    982  1.40  thorpej 		}
    983  1.40  thorpej 	}
    984  1.40  thorpej }
    985  1.40  thorpej 
    986  1.30   bouyer /* management of the /dev/atabus* devices */
    987  1.30   bouyer int atabusopen(dev, flag, fmt, p)
    988  1.30   bouyer 	dev_t dev;
    989  1.30   bouyer 	int flag, fmt;
    990  1.30   bouyer 	struct proc *p;
    991  1.30   bouyer {
    992  1.30   bouyer         struct atabus_softc *sc;
    993  1.30   bouyer         int error, unit = minor(dev);
    994  1.30   bouyer 
    995  1.30   bouyer         if (unit >= atabus_cd.cd_ndevs ||
    996  1.30   bouyer             (sc = atabus_cd.cd_devs[unit]) == NULL)
    997  1.30   bouyer                 return (ENXIO);
    998  1.30   bouyer 
    999  1.30   bouyer         if (sc->sc_flags & ATABUSCF_OPEN)
   1000  1.30   bouyer                 return (EBUSY);
   1001  1.30   bouyer 
   1002  1.43  thorpej         if ((error = ata_addref(sc->sc_chan)) != 0)
   1003  1.30   bouyer                 return (error);
   1004  1.30   bouyer 
   1005  1.30   bouyer         sc->sc_flags |= ATABUSCF_OPEN;
   1006  1.30   bouyer 
   1007  1.30   bouyer         return (0);
   1008  1.30   bouyer }
   1009  1.30   bouyer 
   1010  1.30   bouyer 
   1011  1.30   bouyer int
   1012  1.30   bouyer atabusclose(dev, flag, fmt, p)
   1013  1.30   bouyer         dev_t dev;
   1014  1.30   bouyer         int flag, fmt;
   1015  1.30   bouyer         struct proc *p;
   1016  1.30   bouyer {
   1017  1.30   bouyer         struct atabus_softc *sc = atabus_cd.cd_devs[minor(dev)];
   1018  1.30   bouyer 
   1019  1.43  thorpej         ata_delref(sc->sc_chan);
   1020  1.30   bouyer 
   1021  1.30   bouyer         sc->sc_flags &= ~ATABUSCF_OPEN;
   1022  1.30   bouyer 
   1023  1.30   bouyer         return (0);
   1024  1.30   bouyer }
   1025  1.30   bouyer 
   1026  1.30   bouyer int
   1027  1.30   bouyer atabusioctl(dev, cmd, addr, flag, p)
   1028  1.30   bouyer         dev_t dev;
   1029  1.30   bouyer         u_long cmd;
   1030  1.30   bouyer         caddr_t addr;
   1031  1.30   bouyer         int flag;
   1032  1.30   bouyer         struct proc *p;
   1033  1.30   bouyer {
   1034  1.30   bouyer         struct atabus_softc *sc = atabus_cd.cd_devs[minor(dev)];
   1035  1.32   bouyer 	struct wdc_channel *chp = sc->sc_chan;
   1036  1.32   bouyer 	int min_drive, max_drive, drive;
   1037  1.30   bouyer         int error;
   1038  1.30   bouyer 	int s;
   1039  1.30   bouyer 
   1040  1.30   bouyer         /*
   1041  1.30   bouyer          * Enforce write permission for ioctls that change the
   1042  1.30   bouyer          * state of the bus.  Host adapter specific ioctls must
   1043  1.30   bouyer          * be checked by the adapter driver.
   1044  1.30   bouyer          */
   1045  1.30   bouyer         switch (cmd) {
   1046  1.30   bouyer         case ATABUSIOSCAN:
   1047  1.30   bouyer         case ATABUSIODETACH:
   1048  1.30   bouyer         case ATABUSIORESET:
   1049  1.30   bouyer                 if ((flag & FWRITE) == 0)
   1050  1.30   bouyer                         return (EBADF);
   1051  1.30   bouyer         }
   1052  1.30   bouyer 
   1053  1.30   bouyer         switch (cmd) {
   1054  1.30   bouyer         case ATABUSIORESET:
   1055  1.30   bouyer 		s = splbio();
   1056  1.30   bouyer 		wdc_reset_channel(sc->sc_chan, AT_WAIT | AT_POLL);
   1057  1.30   bouyer 		splx(s);
   1058  1.30   bouyer 		error = 0;
   1059  1.30   bouyer 		break;
   1060  1.32   bouyer 	case ATABUSIOSCAN:
   1061  1.32   bouyer 	{
   1062  1.32   bouyer #if 0
   1063  1.32   bouyer 		struct atabusioscan_args *a=
   1064  1.32   bouyer 		    (struct atabusioscan_args *)addr;
   1065  1.32   bouyer #endif
   1066  1.32   bouyer 		if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) ||
   1067  1.32   bouyer 		    (chp->ch_drive[1].drive_flags & DRIVE_OLD))
   1068  1.32   bouyer 			return (EOPNOTSUPP);
   1069  1.32   bouyer 		return (EOPNOTSUPP);
   1070  1.32   bouyer 	}
   1071  1.32   bouyer 	case ATABUSIODETACH:
   1072  1.32   bouyer 	{
   1073  1.32   bouyer 		struct atabusioscan_args *a=
   1074  1.32   bouyer 		    (struct atabusioscan_args *)addr;
   1075  1.32   bouyer 		if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) ||
   1076  1.32   bouyer 		    (chp->ch_drive[1].drive_flags & DRIVE_OLD))
   1077  1.32   bouyer 			return (EOPNOTSUPP);
   1078  1.32   bouyer 		switch (a->at_dev) {
   1079  1.32   bouyer 		case -1:
   1080  1.32   bouyer 			min_drive = 0;
   1081  1.32   bouyer 			max_drive = 1;
   1082  1.32   bouyer 			break;
   1083  1.32   bouyer 		case 0:
   1084  1.32   bouyer 		case 1:
   1085  1.32   bouyer 			min_drive = max_drive = a->at_dev;
   1086  1.32   bouyer 			break;
   1087  1.32   bouyer 		default:
   1088  1.32   bouyer 			return (EINVAL);
   1089  1.32   bouyer 		}
   1090  1.32   bouyer 		for (drive = min_drive; drive <= max_drive; drive++) {
   1091  1.32   bouyer 			if (chp->ch_drive[drive].drv_softc != NULL) {
   1092  1.32   bouyer 				error = config_detach(
   1093  1.32   bouyer 				    chp->ch_drive[drive].drv_softc, 0);
   1094  1.32   bouyer 				if (error)
   1095  1.32   bouyer 					return (error);
   1096  1.34   bouyer 				chp->ch_drive[drive].drv_softc = NULL;
   1097  1.32   bouyer 			}
   1098  1.32   bouyer 		}
   1099  1.32   bouyer 		error = 0;
   1100  1.32   bouyer 		break;
   1101  1.32   bouyer 	}
   1102  1.30   bouyer 	default:
   1103  1.30   bouyer 		error = ENOTTY;
   1104  1.30   bouyer 	}
   1105  1.30   bouyer 	return (error);
   1106  1.30   bouyer };
   1107