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