Home | History | Annotate | Line # | Download | only in ata
ata.c revision 1.55
      1  1.55  thorpej /*      $NetBSD: ata.c,v 1.55 2004/08/20 23:26:53 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.55  thorpej __KERNEL_RCSID(0, "$NetBSD: ata.c,v 1.55 2004/08/20 23:26:53 thorpej Exp $");
     34   1.2   bouyer 
     35  1.47  thorpej #ifndef ATADEBUG
     36  1.47  thorpej #define ATADEBUG
     37  1.47  thorpej #endif /* ATADEBUG */
     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.2   bouyer 
     58  1.23  thorpej #include "locators.h"
     59  1.23  thorpej 
     60  1.51  thorpej #include "atapibus.h"
     61  1.51  thorpej #include "ataraid.h"
     62  1.51  thorpej 
     63  1.51  thorpej #if NATARAID > 0
     64  1.51  thorpej #include <dev/ata/ata_raidvar.h>
     65  1.51  thorpej #endif
     66  1.51  thorpej 
     67   1.2   bouyer #define DEBUG_FUNCS  0x08
     68   1.2   bouyer #define DEBUG_PROBE  0x10
     69  1.23  thorpej #define DEBUG_DETACH 0x20
     70  1.44  thorpej #define	DEBUG_XFERS  0x40
     71  1.47  thorpej #ifdef ATADEBUG
     72  1.47  thorpej int atadebug_mask = 0;
     73  1.47  thorpej #define ATADEBUG_PRINT(args, level) \
     74  1.47  thorpej         if (atadebug_mask & (level)) \
     75   1.2   bouyer 		printf args
     76   1.2   bouyer #else
     77  1.47  thorpej #define ATADEBUG_PRINT(args, level)
     78   1.2   bouyer #endif
     79   1.2   bouyer 
     80  1.41  thorpej POOL_INIT(ata_xfer_pool, sizeof(struct ata_xfer), 0, 0, 0, "ataspl", NULL);
     81  1.41  thorpej 
     82  1.51  thorpej /*
     83  1.51  thorpej  * A queue of atabus instances, used to ensure the same bus probe order
     84  1.51  thorpej  * for a given hardware configuration at each boot.
     85  1.51  thorpej  */
     86  1.51  thorpej struct atabus_initq_head atabus_initq_head =
     87  1.51  thorpej     TAILQ_HEAD_INITIALIZER(atabus_initq_head);
     88  1.51  thorpej struct simplelock atabus_interlock = SIMPLELOCK_INITIALIZER;
     89  1.51  thorpej 
     90  1.23  thorpej /*****************************************************************************
     91  1.23  thorpej  * ATA bus layer.
     92  1.23  thorpej  *
     93  1.23  thorpej  * ATA controllers attach an atabus instance, which handles probing the bus
     94  1.23  thorpej  * for drives, etc.
     95  1.23  thorpej  *****************************************************************************/
     96  1.23  thorpej 
     97  1.30   bouyer dev_type_open(atabusopen);
     98  1.30   bouyer dev_type_close(atabusclose);
     99  1.30   bouyer dev_type_ioctl(atabusioctl);
    100  1.30   bouyer 
    101  1.30   bouyer const struct cdevsw atabus_cdevsw = {
    102  1.30   bouyer 	atabusopen, atabusclose, noread, nowrite, atabusioctl,
    103  1.30   bouyer 	nostop, notty, nopoll, nommap, nokqfilter,
    104  1.30   bouyer };
    105  1.30   bouyer 
    106  1.30   bouyer extern struct cfdriver atabus_cd;
    107  1.30   bouyer 
    108  1.30   bouyer 
    109  1.23  thorpej /*
    110  1.23  thorpej  * atabusprint:
    111  1.23  thorpej  *
    112  1.23  thorpej  *	Autoconfiguration print routine used by ATA controllers when
    113  1.23  thorpej  *	attaching an atabus instance.
    114  1.23  thorpej  */
    115  1.23  thorpej int
    116  1.23  thorpej atabusprint(void *aux, const char *pnp)
    117  1.23  thorpej {
    118  1.48  thorpej 	struct ata_channel *chan = aux;
    119  1.23  thorpej 
    120  1.23  thorpej 	if (pnp)
    121  1.23  thorpej 		aprint_normal("atabus at %s", pnp);
    122  1.26  thorpej 	aprint_normal(" channel %d", chan->ch_channel);
    123  1.23  thorpej 
    124  1.23  thorpej 	return (UNCONF);
    125  1.23  thorpej }
    126  1.23  thorpej 
    127  1.23  thorpej /*
    128  1.23  thorpej  * ataprint:
    129  1.23  thorpej  *
    130  1.23  thorpej  *	Autoconfiguration print routine.
    131  1.23  thorpej  */
    132  1.23  thorpej int
    133  1.23  thorpej ataprint(void *aux, const char *pnp)
    134  1.23  thorpej {
    135  1.23  thorpej 	struct ata_device *adev = aux;
    136  1.23  thorpej 
    137  1.23  thorpej 	if (pnp)
    138  1.23  thorpej 		aprint_normal("wd at %s", pnp);
    139  1.23  thorpej 	aprint_normal(" drive %d", adev->adev_drv_data->drive);
    140  1.23  thorpej 
    141  1.23  thorpej 	return (UNCONF);
    142  1.23  thorpej }
    143  1.23  thorpej 
    144  1.52  thorpej /*
    145  1.52  thorpej  * ata_channel_attach:
    146  1.52  thorpej  *
    147  1.52  thorpej  *	Common parts of attaching an atabus to an ATA controller channel.
    148  1.52  thorpej  */
    149  1.52  thorpej void
    150  1.52  thorpej ata_channel_attach(struct ata_channel *chp)
    151  1.52  thorpej {
    152  1.52  thorpej 
    153  1.52  thorpej 	if (chp->ch_flags & ATACH_DISABLED)
    154  1.52  thorpej 		return;
    155  1.52  thorpej 
    156  1.52  thorpej 	callout_init(&chp->ch_callout);
    157  1.52  thorpej 
    158  1.52  thorpej 	TAILQ_INIT(&chp->ch_queue->queue_xfer);
    159  1.52  thorpej 	chp->ch_queue->queue_freeze = 0;
    160  1.52  thorpej 	chp->ch_queue->active_xfer = NULL;
    161  1.52  thorpej 
    162  1.52  thorpej 	chp->atabus = config_found(&chp->ch_atac->atac_dev, chp, atabusprint);
    163  1.52  thorpej }
    164  1.52  thorpej 
    165  1.51  thorpej static void
    166  1.51  thorpej atabusconfig(struct atabus_softc *atabus_sc)
    167  1.51  thorpej {
    168  1.51  thorpej 	struct ata_channel *chp = atabus_sc->sc_chan;
    169  1.51  thorpej 	struct atac_softc *atac = chp->ch_atac;
    170  1.51  thorpej 	int i;
    171  1.51  thorpej 	struct atabus_initq *atabus_initq = NULL;
    172  1.51  thorpej 
    173  1.51  thorpej 	/* Probe for the drives. */
    174  1.51  thorpej 	(*atac->atac_probe)(chp);
    175  1.51  thorpej 
    176  1.51  thorpej 	ATADEBUG_PRINT(("atabusattach: ch_drive_flags 0x%x 0x%x\n",
    177  1.51  thorpej 	    chp->ch_drive[0].drive_flags, chp->ch_drive[1].drive_flags),
    178  1.51  thorpej 	    DEBUG_PROBE);
    179  1.51  thorpej 
    180  1.51  thorpej 	/* If no drives, abort here */
    181  1.51  thorpej 	for (i = 0; i < chp->ch_ndrive; i++)
    182  1.51  thorpej 		if ((chp->ch_drive[i].drive_flags & DRIVE) != 0)
    183  1.51  thorpej 			break;
    184  1.51  thorpej 	if (i == chp->ch_ndrive)
    185  1.51  thorpej 		goto out;
    186  1.51  thorpej 
    187  1.51  thorpej 	/* Shortcut in case we've been shutdown */
    188  1.51  thorpej 	if (chp->ch_flags & ATACH_SHUTDOWN)
    189  1.51  thorpej 		goto out;
    190  1.51  thorpej 
    191  1.51  thorpej 	/* Make sure the devices probe in atabus order to avoid jitter. */
    192  1.51  thorpej 	simple_lock(&atabus_interlock);
    193  1.51  thorpej 	while(1) {
    194  1.51  thorpej 		atabus_initq = TAILQ_FIRST(&atabus_initq_head);
    195  1.51  thorpej 		if (atabus_initq->atabus_sc == atabus_sc)
    196  1.51  thorpej 			break;
    197  1.51  thorpej 		ltsleep(&atabus_initq_head, PRIBIO, "ata_initq", 0,
    198  1.51  thorpej 		    &atabus_interlock);
    199  1.51  thorpej 	}
    200  1.51  thorpej 	simple_unlock(&atabus_interlock);
    201  1.51  thorpej 
    202  1.51  thorpej 	/*
    203  1.51  thorpej 	 * Attach an ATAPI bus, if needed.
    204  1.51  thorpej 	 */
    205  1.51  thorpej 	for (i = 0; i < chp->ch_ndrive; i++) {
    206  1.51  thorpej 		if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI) {
    207  1.51  thorpej #if NATAPIBUS > 0
    208  1.51  thorpej 			(*atac->atac_atapibus_attach)(atabus_sc);
    209  1.51  thorpej #else
    210  1.51  thorpej 			/*
    211  1.51  thorpej 			 * Fake the autoconfig "not configured" message
    212  1.51  thorpej 			 */
    213  1.51  thorpej 			aprint_normal("atapibus at %s not configured\n",
    214  1.51  thorpej 			    atac->atac_dev.dv_xname);
    215  1.51  thorpej 			chp->atapibus = NULL;
    216  1.51  thorpej 			for (i = 0; i < chp->ch_ndrive; i++)
    217  1.51  thorpej 				chp->ch_drive[i].drive_flags &= ~DRIVE_ATAPI;
    218  1.51  thorpej #endif
    219  1.51  thorpej 			break;
    220  1.51  thorpej 		}
    221  1.51  thorpej 	}
    222  1.51  thorpej 
    223  1.51  thorpej 	for (i = 0; i < chp->ch_ndrive; i++) {
    224  1.51  thorpej 		struct ata_device adev;
    225  1.51  thorpej 		if ((chp->ch_drive[i].drive_flags &
    226  1.51  thorpej 		    (DRIVE_ATA | DRIVE_OLD)) == 0) {
    227  1.51  thorpej 			continue;
    228  1.51  thorpej 		}
    229  1.51  thorpej 		memset(&adev, 0, sizeof(struct ata_device));
    230  1.51  thorpej 		adev.adev_bustype = atac->atac_bustype_ata;
    231  1.51  thorpej 		adev.adev_channel = chp->ch_channel;
    232  1.51  thorpej 		adev.adev_openings = 1;
    233  1.51  thorpej 		adev.adev_drv_data = &chp->ch_drive[i];
    234  1.51  thorpej 		chp->ata_drives[i] = config_found(&atabus_sc->sc_dev,
    235  1.51  thorpej 		    &adev, ataprint);
    236  1.51  thorpej 		if (chp->ata_drives[i] != NULL)
    237  1.51  thorpej 			ata_probe_caps(&chp->ch_drive[i]);
    238  1.51  thorpej 		else
    239  1.51  thorpej 			chp->ch_drive[i].drive_flags &=
    240  1.51  thorpej 			    ~(DRIVE_ATA | DRIVE_OLD);
    241  1.51  thorpej 	}
    242  1.51  thorpej 
    243  1.51  thorpej 	/* now that we know the drives, the controller can set its modes */
    244  1.51  thorpej 	if (atac->atac_set_modes) {
    245  1.51  thorpej 		(*atac->atac_set_modes)(chp);
    246  1.51  thorpej 		ata_print_modes(chp);
    247  1.51  thorpej 	}
    248  1.51  thorpej #if NATARAID > 0
    249  1.51  thorpej 	if (atac->atac_cap & ATAC_CAP_RAID)
    250  1.51  thorpej 		for (i = 0; i < chp->ch_ndrive; i++)
    251  1.51  thorpej 			if (chp->ata_drives[i] != NULL)
    252  1.51  thorpej 				ata_raid_check_component(chp->ata_drives[i]);
    253  1.51  thorpej #endif /* NATARAID > 0 */
    254  1.51  thorpej 
    255  1.51  thorpej 	/*
    256  1.51  thorpej 	 * reset drive_flags for unattached devices, reset state for attached
    257  1.51  thorpej 	 * ones
    258  1.51  thorpej 	 */
    259  1.51  thorpej 	for (i = 0; i < chp->ch_ndrive; i++) {
    260  1.51  thorpej 		if (chp->ch_drive[i].drv_softc == NULL)
    261  1.51  thorpej 			chp->ch_drive[i].drive_flags = 0;
    262  1.51  thorpej 		else
    263  1.51  thorpej 			chp->ch_drive[i].state = 0;
    264  1.51  thorpej 	}
    265  1.51  thorpej 
    266  1.51  thorpej  out:
    267  1.51  thorpej 	if (atabus_initq == NULL) {
    268  1.51  thorpej 		simple_lock(&atabus_interlock);
    269  1.51  thorpej 		while(1) {
    270  1.51  thorpej 			atabus_initq = TAILQ_FIRST(&atabus_initq_head);
    271  1.51  thorpej 			if (atabus_initq->atabus_sc == atabus_sc)
    272  1.51  thorpej 				break;
    273  1.51  thorpej 			ltsleep(&atabus_initq_head, PRIBIO, "ata_initq", 0,
    274  1.51  thorpej 			    &atabus_interlock);
    275  1.51  thorpej 		}
    276  1.51  thorpej 		simple_unlock(&atabus_interlock);
    277  1.51  thorpej 	}
    278  1.51  thorpej         simple_lock(&atabus_interlock);
    279  1.51  thorpej         TAILQ_REMOVE(&atabus_initq_head, atabus_initq, atabus_initq);
    280  1.51  thorpej         simple_unlock(&atabus_interlock);
    281  1.51  thorpej 
    282  1.51  thorpej         free(atabus_initq, M_DEVBUF);
    283  1.51  thorpej         wakeup(&atabus_initq_head);
    284  1.51  thorpej 
    285  1.51  thorpej 	ata_delref(chp);
    286  1.51  thorpej 
    287  1.51  thorpej 	config_pending_decr();
    288  1.51  thorpej }
    289  1.51  thorpej 
    290  1.23  thorpej /*
    291  1.23  thorpej  * atabus_thread:
    292  1.23  thorpej  *
    293  1.23  thorpej  *	Worker thread for the ATA bus.
    294  1.23  thorpej  */
    295  1.23  thorpej static void
    296  1.23  thorpej atabus_thread(void *arg)
    297  1.23  thorpej {
    298  1.23  thorpej 	struct atabus_softc *sc = arg;
    299  1.48  thorpej 	struct ata_channel *chp = sc->sc_chan;
    300  1.24  thorpej 	struct ata_xfer *xfer;
    301  1.48  thorpej 	int i, s;
    302  1.23  thorpej 
    303  1.23  thorpej 	s = splbio();
    304  1.48  thorpej 	chp->ch_flags |= ATACH_TH_RUN;
    305  1.48  thorpej 
    306  1.48  thorpej 	/*
    307  1.48  thorpej 	 * Probe the drives.  Reset all flags to 0 to indicate to controllers
    308  1.48  thorpej 	 * that can re-probe that all drives must be probed..
    309  1.48  thorpej 	 *
    310  1.48  thorpej 	 * Note: ch_ndrive may be changed during the probe.
    311  1.48  thorpej 	 */
    312  1.48  thorpej 	for (i = 0; i < ATA_MAXDRIVES; i++)
    313  1.48  thorpej 		chp->ch_drive[i].drive_flags = 0;
    314  1.23  thorpej 	splx(s);
    315  1.23  thorpej 
    316  1.23  thorpej 	/* Configure the devices on the bus. */
    317  1.23  thorpej 	atabusconfig(sc);
    318  1.23  thorpej 
    319  1.23  thorpej 	for (;;) {
    320  1.23  thorpej 		s = splbio();
    321  1.48  thorpej 		if ((chp->ch_flags & (ATACH_TH_RESET | ATACH_SHUTDOWN)) == 0 &&
    322  1.33   bouyer 		    (chp->ch_queue->active_xfer == NULL ||
    323  1.23  thorpej 		     chp->ch_queue->queue_freeze == 0)) {
    324  1.48  thorpej 			chp->ch_flags &= ~ATACH_TH_RUN;
    325  1.27  thorpej 			(void) tsleep(&chp->ch_thread, PRIBIO, "atath", 0);
    326  1.48  thorpej 			chp->ch_flags |= ATACH_TH_RUN;
    327  1.23  thorpej 		}
    328  1.23  thorpej 		splx(s);
    329  1.48  thorpej 		if (chp->ch_flags & ATACH_SHUTDOWN)
    330  1.23  thorpej 			break;
    331  1.23  thorpej 		s = splbio();
    332  1.48  thorpej 		if (chp->ch_flags & ATACH_TH_RESET) {
    333  1.31   bouyer 			/*
    334  1.55  thorpej 			 * ata_reset_channel() will freeze 2 times, so
    335  1.31   bouyer 			 * unfreeze one time. Not a problem as we're at splbio
    336  1.31   bouyer 			 */
    337  1.23  thorpej 			chp->ch_queue->queue_freeze--;
    338  1.55  thorpej 			ata_reset_channel(chp, AT_WAIT | chp->ch_reset_flags);
    339  1.33   bouyer 		} else if (chp->ch_queue->active_xfer != NULL &&
    340  1.23  thorpej 			   chp->ch_queue->queue_freeze == 1) {
    341  1.23  thorpej 			/*
    342  1.23  thorpej 			 * Caller has bumped queue_freeze, decrease it.
    343  1.23  thorpej 			 */
    344  1.23  thorpej 			chp->ch_queue->queue_freeze--;
    345  1.33   bouyer 			xfer = chp->ch_queue->active_xfer;
    346  1.23  thorpej 			KASSERT(xfer != NULL);
    347  1.23  thorpej 			(*xfer->c_start)(chp, xfer);
    348  1.23  thorpej 		} else if (chp->ch_queue->queue_freeze > 1)
    349  1.23  thorpej 			panic("ata_thread: queue_freeze");
    350  1.23  thorpej 		splx(s);
    351  1.23  thorpej 	}
    352  1.27  thorpej 	chp->ch_thread = NULL;
    353  1.28   bouyer 	wakeup((void *)&chp->ch_flags);
    354  1.23  thorpej 	kthread_exit(0);
    355  1.23  thorpej }
    356  1.23  thorpej 
    357  1.23  thorpej /*
    358  1.23  thorpej  * atabus_create_thread:
    359  1.23  thorpej  *
    360  1.23  thorpej  *	Helper routine to create the ATA bus worker thread.
    361  1.23  thorpej  */
    362  1.23  thorpej static void
    363  1.23  thorpej atabus_create_thread(void *arg)
    364  1.23  thorpej {
    365  1.23  thorpej 	struct atabus_softc *sc = arg;
    366  1.48  thorpej 	struct ata_channel *chp = sc->sc_chan;
    367  1.23  thorpej 	int error;
    368  1.23  thorpej 
    369  1.27  thorpej 	if ((error = kthread_create1(atabus_thread, sc, &chp->ch_thread,
    370  1.23  thorpej 				     "%s", sc->sc_dev.dv_xname)) != 0)
    371  1.23  thorpej 		aprint_error("%s: unable to create kernel thread: error %d\n",
    372  1.23  thorpej 		    sc->sc_dev.dv_xname, error);
    373  1.23  thorpej }
    374  1.23  thorpej 
    375  1.23  thorpej /*
    376  1.23  thorpej  * atabus_match:
    377  1.23  thorpej  *
    378  1.23  thorpej  *	Autoconfiguration match routine.
    379  1.23  thorpej  */
    380  1.23  thorpej static int
    381  1.23  thorpej atabus_match(struct device *parent, struct cfdata *cf, void *aux)
    382  1.23  thorpej {
    383  1.48  thorpej 	struct ata_channel *chp = aux;
    384  1.23  thorpej 
    385  1.23  thorpej 	if (chp == NULL)
    386  1.23  thorpej 		return (0);
    387  1.23  thorpej 
    388  1.26  thorpej 	if (cf->cf_loc[ATACF_CHANNEL] != chp->ch_channel &&
    389  1.23  thorpej 	    cf->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT)
    390  1.23  thorpej 	    	return (0);
    391  1.23  thorpej 
    392  1.23  thorpej 	return (1);
    393  1.23  thorpej }
    394  1.23  thorpej 
    395  1.23  thorpej /*
    396  1.23  thorpej  * atabus_attach:
    397  1.23  thorpej  *
    398  1.23  thorpej  *	Autoconfiguration attach routine.
    399  1.23  thorpej  */
    400  1.23  thorpej static void
    401  1.23  thorpej atabus_attach(struct device *parent, struct device *self, void *aux)
    402  1.23  thorpej {
    403  1.23  thorpej 	struct atabus_softc *sc = (void *) self;
    404  1.48  thorpej 	struct ata_channel *chp = aux;
    405  1.23  thorpej 	struct atabus_initq *initq;
    406  1.23  thorpej 
    407  1.23  thorpej 	sc->sc_chan = chp;
    408  1.23  thorpej 
    409  1.23  thorpej 	aprint_normal("\n");
    410  1.23  thorpej 	aprint_naive("\n");
    411  1.23  thorpej 
    412  1.43  thorpej         if (ata_addref(chp))
    413  1.35  mycroft                 return;
    414  1.35  mycroft 
    415  1.23  thorpej 	initq = malloc(sizeof(*initq), M_DEVBUF, M_WAITOK);
    416  1.23  thorpej 	initq->atabus_sc = sc;
    417  1.23  thorpej 	TAILQ_INSERT_TAIL(&atabus_initq_head, initq, atabus_initq);
    418  1.23  thorpej 	config_pending_incr();
    419  1.23  thorpej 	kthread_create(atabus_create_thread, sc);
    420  1.23  thorpej }
    421  1.23  thorpej 
    422  1.23  thorpej /*
    423  1.23  thorpej  * atabus_activate:
    424  1.23  thorpej  *
    425  1.23  thorpej  *	Autoconfiguration activation routine.
    426  1.23  thorpej  */
    427  1.23  thorpej static int
    428  1.23  thorpej atabus_activate(struct device *self, enum devact act)
    429  1.23  thorpej {
    430  1.23  thorpej 	struct atabus_softc *sc = (void *) self;
    431  1.48  thorpej 	struct ata_channel *chp = sc->sc_chan;
    432  1.23  thorpej 	struct device *dev = NULL;
    433  1.23  thorpej 	int s, i, error = 0;
    434  1.23  thorpej 
    435  1.23  thorpej 	s = splbio();
    436  1.23  thorpej 	switch (act) {
    437  1.23  thorpej 	case DVACT_ACTIVATE:
    438  1.23  thorpej 		error = EOPNOTSUPP;
    439  1.23  thorpej 		break;
    440  1.23  thorpej 
    441  1.23  thorpej 	case DVACT_DEACTIVATE:
    442  1.23  thorpej 		/*
    443  1.23  thorpej 		 * We might deactivate the children of atapibus twice
    444  1.23  thorpej 		 * (once bia atapibus, once directly), but since the
    445  1.23  thorpej 		 * generic autoconfiguration code maintains the DVF_ACTIVE
    446  1.23  thorpej 		 * flag, it's safe.
    447  1.23  thorpej 		 */
    448  1.23  thorpej 		if ((dev = chp->atapibus) != NULL) {
    449  1.23  thorpej 			error = config_deactivate(dev);
    450  1.23  thorpej 			if (error)
    451  1.23  thorpej 				goto out;
    452  1.23  thorpej 		}
    453  1.23  thorpej 
    454  1.48  thorpej 		for (i = 0; i < chp->ch_ndrive; i++) {
    455  1.23  thorpej 			if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
    456  1.47  thorpej 				ATADEBUG_PRINT(("atabus_activate: %s: "
    457  1.23  thorpej 				    "deactivating %s\n", sc->sc_dev.dv_xname,
    458  1.23  thorpej 				    dev->dv_xname),
    459  1.23  thorpej 				    DEBUG_DETACH);
    460  1.23  thorpej 				error = config_deactivate(dev);
    461  1.23  thorpej 				if (error)
    462  1.23  thorpej 					goto out;
    463  1.23  thorpej 			}
    464  1.23  thorpej 		}
    465  1.23  thorpej 		break;
    466  1.23  thorpej 	}
    467  1.23  thorpej  out:
    468  1.23  thorpej 	splx(s);
    469  1.23  thorpej 
    470  1.47  thorpej #ifdef ATADEBUG
    471  1.23  thorpej 	if (dev != NULL && error != 0)
    472  1.47  thorpej 		ATADEBUG_PRINT(("atabus_activate: %s: "
    473  1.23  thorpej 		    "error %d deactivating %s\n", sc->sc_dev.dv_xname,
    474  1.23  thorpej 		    error, dev->dv_xname), DEBUG_DETACH);
    475  1.47  thorpej #endif /* ATADEBUG */
    476  1.23  thorpej 
    477  1.23  thorpej 	return (error);
    478  1.23  thorpej }
    479  1.23  thorpej 
    480  1.23  thorpej /*
    481  1.23  thorpej  * atabus_detach:
    482  1.23  thorpej  *
    483  1.23  thorpej  *	Autoconfiguration detach routine.
    484  1.23  thorpej  */
    485  1.23  thorpej static int
    486  1.23  thorpej atabus_detach(struct device *self, int flags)
    487  1.23  thorpej {
    488  1.23  thorpej 	struct atabus_softc *sc = (void *) self;
    489  1.48  thorpej 	struct ata_channel *chp = sc->sc_chan;
    490  1.23  thorpej 	struct device *dev = NULL;
    491  1.23  thorpej 	int i, error = 0;
    492  1.23  thorpej 
    493  1.23  thorpej 	/* Shutdown the channel. */
    494  1.23  thorpej 	/* XXX NEED AN INTERLOCK HERE. */
    495  1.48  thorpej 	chp->ch_flags |= ATACH_SHUTDOWN;
    496  1.27  thorpej 	wakeup(&chp->ch_thread);
    497  1.27  thorpej 	while (chp->ch_thread != NULL)
    498  1.28   bouyer 		(void) tsleep((void *)&chp->ch_flags, PRIBIO, "atadown", 0);
    499  1.23  thorpej 
    500  1.23  thorpej 	/*
    501  1.23  thorpej 	 * Detach atapibus and its children.
    502  1.23  thorpej 	 */
    503  1.23  thorpej 	if ((dev = chp->atapibus) != NULL) {
    504  1.47  thorpej 		ATADEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
    505  1.23  thorpej 		    sc->sc_dev.dv_xname, dev->dv_xname), DEBUG_DETACH);
    506  1.23  thorpej 		error = config_detach(dev, flags);
    507  1.23  thorpej 		if (error)
    508  1.23  thorpej 			goto out;
    509  1.23  thorpej 	}
    510  1.23  thorpej 
    511  1.23  thorpej 	/*
    512  1.23  thorpej 	 * Detach our other children.
    513  1.23  thorpej 	 */
    514  1.48  thorpej 	for (i = 0; i < chp->ch_ndrive; i++) {
    515  1.23  thorpej 		if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI)
    516  1.23  thorpej 			continue;
    517  1.23  thorpej 		if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
    518  1.47  thorpej 			ATADEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
    519  1.23  thorpej 			    sc->sc_dev.dv_xname, dev->dv_xname),
    520  1.23  thorpej 			    DEBUG_DETACH);
    521  1.23  thorpej 			error = config_detach(dev, flags);
    522  1.23  thorpej 			if (error)
    523  1.23  thorpej 				goto out;
    524  1.23  thorpej 		}
    525  1.23  thorpej 	}
    526  1.23  thorpej 
    527  1.23  thorpej  out:
    528  1.47  thorpej #ifdef ATADEBUG
    529  1.23  thorpej 	if (dev != NULL && error != 0)
    530  1.47  thorpej 		ATADEBUG_PRINT(("atabus_detach: %s: error %d detaching %s\n",
    531  1.23  thorpej 		    sc->sc_dev.dv_xname, error, dev->dv_xname),
    532  1.23  thorpej 		    DEBUG_DETACH);
    533  1.47  thorpej #endif /* ATADEBUG */
    534  1.23  thorpej 
    535  1.23  thorpej 	return (error);
    536  1.23  thorpej }
    537  1.23  thorpej 
    538  1.23  thorpej CFATTACH_DECL(atabus, sizeof(struct atabus_softc),
    539  1.23  thorpej     atabus_match, atabus_attach, atabus_detach, atabus_activate);
    540  1.23  thorpej 
    541  1.23  thorpej /*****************************************************************************
    542  1.23  thorpej  * Common ATA bus operations.
    543  1.23  thorpej  *****************************************************************************/
    544  1.23  thorpej 
    545   1.2   bouyer /* Get the disk's parameters */
    546   1.2   bouyer int
    547  1.21  thorpej ata_get_params(struct ata_drive_datas *drvp, u_int8_t flags,
    548  1.21  thorpej     struct ataparams *prms)
    549   1.2   bouyer {
    550   1.2   bouyer 	char tb[DEV_BSIZE];
    551  1.36  thorpej 	struct ata_command ata_c;
    552  1.50  thorpej 	struct ata_channel *chp = drvp->chnl_softc;
    553  1.50  thorpej 	struct atac_softc *atac = chp->ch_atac;
    554   1.2   bouyer 
    555   1.2   bouyer #if BYTE_ORDER == LITTLE_ENDIAN
    556   1.2   bouyer 	int i;
    557   1.2   bouyer 	u_int16_t *p;
    558   1.2   bouyer #endif
    559   1.2   bouyer 
    560  1.47  thorpej 	ATADEBUG_PRINT(("ata_get_parms\n"), DEBUG_FUNCS);
    561   1.2   bouyer 
    562   1.2   bouyer 	memset(tb, 0, DEV_BSIZE);
    563   1.2   bouyer 	memset(prms, 0, sizeof(struct ataparams));
    564  1.36  thorpej 	memset(&ata_c, 0, sizeof(struct ata_command));
    565   1.2   bouyer 
    566   1.2   bouyer 	if (drvp->drive_flags & DRIVE_ATA) {
    567  1.36  thorpej 		ata_c.r_command = WDCC_IDENTIFY;
    568  1.36  thorpej 		ata_c.r_st_bmask = WDCS_DRDY;
    569  1.36  thorpej 		ata_c.r_st_pmask = 0;
    570  1.36  thorpej 		ata_c.timeout = 3000; /* 3s */
    571   1.7   bouyer 	} else if (drvp->drive_flags & DRIVE_ATAPI) {
    572  1.36  thorpej 		ata_c.r_command = ATAPI_IDENTIFY_DEVICE;
    573  1.36  thorpej 		ata_c.r_st_bmask = 0;
    574  1.36  thorpej 		ata_c.r_st_pmask = 0;
    575  1.36  thorpej 		ata_c.timeout = 10000; /* 10s */
    576   1.7   bouyer 	} else {
    577  1.47  thorpej 		ATADEBUG_PRINT(("ata_get_parms: no disks\n"),
    578  1.10   bouyer 		    DEBUG_FUNCS|DEBUG_PROBE);
    579   1.7   bouyer 		return CMD_ERR;
    580   1.2   bouyer 	}
    581  1.36  thorpej 	ata_c.flags = AT_READ | flags;
    582  1.36  thorpej 	ata_c.data = tb;
    583  1.36  thorpej 	ata_c.bcount = DEV_BSIZE;
    584  1.50  thorpej 	if ((*atac->atac_bustype_ata->ata_exec_command)(drvp,
    585  1.50  thorpej 						&ata_c) != ATACMD_COMPLETE) {
    586  1.47  thorpej 		ATADEBUG_PRINT(("ata_get_parms: wdc_exec_command failed\n"),
    587  1.10   bouyer 		    DEBUG_FUNCS|DEBUG_PROBE);
    588   1.2   bouyer 		return CMD_AGAIN;
    589  1.10   bouyer 	}
    590  1.36  thorpej 	if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    591  1.47  thorpej 		ATADEBUG_PRINT(("ata_get_parms: ata_c.flags=0x%x\n",
    592  1.36  thorpej 		    ata_c.flags), DEBUG_FUNCS|DEBUG_PROBE);
    593   1.2   bouyer 		return CMD_ERR;
    594   1.2   bouyer 	} else {
    595  1.17   bouyer 		/* if we didn't read any data something is wrong */
    596  1.36  thorpej 		if ((ata_c.flags & AT_XFDONE) == 0)
    597  1.17   bouyer 			return CMD_ERR;
    598   1.2   bouyer 		/* Read in parameter block. */
    599   1.2   bouyer 		memcpy(prms, tb, sizeof(struct ataparams));
    600   1.2   bouyer #if BYTE_ORDER == LITTLE_ENDIAN
    601   1.2   bouyer 		/*
    602   1.2   bouyer 		 * Shuffle string byte order.
    603   1.2   bouyer 		 * ATAPI Mitsumi and NEC drives don't need this.
    604   1.2   bouyer 		 */
    605   1.2   bouyer 		if ((prms->atap_config & WDC_CFG_ATAPI_MASK) ==
    606   1.2   bouyer 		    WDC_CFG_ATAPI &&
    607   1.2   bouyer 		    ((prms->atap_model[0] == 'N' &&
    608   1.2   bouyer 			prms->atap_model[1] == 'E') ||
    609   1.2   bouyer 		     (prms->atap_model[0] == 'F' &&
    610   1.2   bouyer 			 prms->atap_model[1] == 'X')))
    611   1.2   bouyer 			return 0;
    612   1.2   bouyer 		for (i = 0; i < sizeof(prms->atap_model); i += 2) {
    613   1.2   bouyer 			p = (u_short *)(prms->atap_model + i);
    614   1.2   bouyer 			*p = ntohs(*p);
    615   1.2   bouyer 		}
    616   1.2   bouyer 		for (i = 0; i < sizeof(prms->atap_serial); i += 2) {
    617   1.2   bouyer 			p = (u_short *)(prms->atap_serial + i);
    618   1.2   bouyer 			*p = ntohs(*p);
    619   1.2   bouyer 		}
    620   1.2   bouyer 		for (i = 0; i < sizeof(prms->atap_revision); i += 2) {
    621   1.2   bouyer 			p = (u_short *)(prms->atap_revision + i);
    622   1.2   bouyer 			*p = ntohs(*p);
    623   1.2   bouyer 		}
    624   1.2   bouyer #endif
    625   1.2   bouyer 		return CMD_OK;
    626   1.2   bouyer 	}
    627   1.2   bouyer }
    628   1.2   bouyer 
    629   1.2   bouyer int
    630  1.21  thorpej ata_set_mode(struct ata_drive_datas *drvp, u_int8_t mode, u_int8_t flags)
    631   1.2   bouyer {
    632  1.36  thorpej 	struct ata_command ata_c;
    633  1.50  thorpej 	struct ata_channel *chp = drvp->chnl_softc;
    634  1.50  thorpej 	struct atac_softc *atac = chp->ch_atac;
    635   1.2   bouyer 
    636  1.47  thorpej 	ATADEBUG_PRINT(("ata_set_mode=0x%x\n", mode), DEBUG_FUNCS);
    637  1.36  thorpej 	memset(&ata_c, 0, sizeof(struct ata_command));
    638   1.2   bouyer 
    639  1.36  thorpej 	ata_c.r_command = SET_FEATURES;
    640  1.36  thorpej 	ata_c.r_st_bmask = 0;
    641  1.36  thorpej 	ata_c.r_st_pmask = 0;
    642  1.36  thorpej 	ata_c.r_features = WDSF_SET_MODE;
    643  1.36  thorpej 	ata_c.r_count = mode;
    644  1.36  thorpej 	ata_c.flags = flags;
    645  1.36  thorpej 	ata_c.timeout = 1000; /* 1s */
    646  1.50  thorpej 	if ((*atac->atac_bustype_ata->ata_exec_command)(drvp,
    647  1.50  thorpej 						&ata_c) != ATACMD_COMPLETE)
    648   1.2   bouyer 		return CMD_AGAIN;
    649  1.36  thorpej 	if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    650   1.2   bouyer 		return CMD_ERR;
    651   1.2   bouyer 	}
    652   1.2   bouyer 	return CMD_OK;
    653  1.11   bouyer }
    654  1.11   bouyer 
    655  1.11   bouyer void
    656  1.21  thorpej ata_dmaerr(struct ata_drive_datas *drvp, int flags)
    657  1.11   bouyer {
    658  1.11   bouyer 	/*
    659  1.11   bouyer 	 * Downgrade decision: if we get NERRS_MAX in NXFER.
    660  1.11   bouyer 	 * We start with n_dmaerrs set to NERRS_MAX-1 so that the
    661  1.11   bouyer 	 * first error within the first NXFER ops will immediatly trigger
    662  1.11   bouyer 	 * a downgrade.
    663  1.11   bouyer 	 * If we got an error and n_xfers is bigger than NXFER reset counters.
    664  1.11   bouyer 	 */
    665  1.11   bouyer 	drvp->n_dmaerrs++;
    666  1.11   bouyer 	if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) {
    667  1.39  thorpej 		ata_downgrade_mode(drvp, flags);
    668  1.11   bouyer 		drvp->n_dmaerrs = NERRS_MAX-1;
    669  1.11   bouyer 		drvp->n_xfers = 0;
    670  1.11   bouyer 		return;
    671  1.11   bouyer 	}
    672  1.11   bouyer 	if (drvp->n_xfers > NXFER) {
    673  1.11   bouyer 		drvp->n_dmaerrs = 1; /* just got an error */
    674  1.11   bouyer 		drvp->n_xfers = 1; /* restart counting from this error */
    675   1.4   bouyer 	}
    676   1.2   bouyer }
    677  1.30   bouyer 
    678  1.44  thorpej /*
    679  1.44  thorpej  * Add a command to the queue and start controller. Must be called at splbio
    680  1.44  thorpej  */
    681  1.44  thorpej void
    682  1.48  thorpej ata_exec_xfer(struct ata_channel *chp, struct ata_xfer *xfer)
    683  1.44  thorpej {
    684  1.44  thorpej 
    685  1.47  thorpej 	ATADEBUG_PRINT(("ata_exec_xfer %p channel %d drive %d\n", xfer,
    686  1.44  thorpej 	    chp->ch_channel, xfer->c_drive), DEBUG_XFERS);
    687  1.44  thorpej 
    688  1.44  thorpej 	/* complete xfer setup */
    689  1.44  thorpej 	xfer->c_chp = chp;
    690  1.44  thorpej 
    691  1.44  thorpej 	/* insert at the end of command list */
    692  1.44  thorpej 	TAILQ_INSERT_TAIL(&chp->ch_queue->queue_xfer, xfer, c_xferchain);
    693  1.47  thorpej 	ATADEBUG_PRINT(("atastart from ata_exec_xfer, flags 0x%x\n",
    694  1.44  thorpej 	    chp->ch_flags), DEBUG_XFERS);
    695  1.45  thorpej 	atastart(chp);
    696  1.45  thorpej }
    697  1.45  thorpej 
    698  1.45  thorpej /*
    699  1.45  thorpej  * Start I/O on a controller, for the given channel.
    700  1.45  thorpej  * The first xfer may be not for our channel if the channel queues
    701  1.45  thorpej  * are shared.
    702  1.45  thorpej  */
    703  1.45  thorpej void
    704  1.48  thorpej atastart(struct ata_channel *chp)
    705  1.45  thorpej {
    706  1.49  thorpej 	struct atac_softc *atac = chp->ch_atac;
    707  1.45  thorpej 	struct ata_xfer *xfer;
    708  1.45  thorpej 
    709  1.45  thorpej #ifdef WDC_DIAGNOSTIC
    710  1.45  thorpej 	int spl1, spl2;
    711  1.45  thorpej 
    712  1.45  thorpej 	spl1 = splbio();
    713  1.45  thorpej 	spl2 = splbio();
    714  1.45  thorpej 	if (spl2 != spl1) {
    715  1.45  thorpej 		printf("atastart: not at splbio()\n");
    716  1.45  thorpej 		panic("atastart");
    717  1.45  thorpej 	}
    718  1.45  thorpej 	splx(spl2);
    719  1.45  thorpej 	splx(spl1);
    720  1.45  thorpej #endif /* WDC_DIAGNOSTIC */
    721  1.45  thorpej 
    722  1.45  thorpej 	/* is there a xfer ? */
    723  1.45  thorpej 	if ((xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer)) == NULL)
    724  1.45  thorpej 		return;
    725  1.45  thorpej 
    726  1.45  thorpej 	/* adjust chp, in case we have a shared queue */
    727  1.45  thorpej 	chp = xfer->c_chp;
    728  1.45  thorpej 
    729  1.45  thorpej 	if (chp->ch_queue->active_xfer != NULL) {
    730  1.45  thorpej 		return; /* channel aleady active */
    731  1.45  thorpej 	}
    732  1.45  thorpej 	if (__predict_false(chp->ch_queue->queue_freeze > 0)) {
    733  1.45  thorpej 		return; /* queue froozen */
    734  1.45  thorpej 	}
    735  1.45  thorpej #ifdef DIAGNOSTIC
    736  1.48  thorpej 	if ((chp->ch_flags & ATACH_IRQ_WAIT) != 0)
    737  1.45  thorpej 		panic("atastart: channel waiting for irq");
    738  1.45  thorpej #endif
    739  1.49  thorpej 	if (atac->atac_claim_hw)
    740  1.49  thorpej 		if (!(*atac->atac_claim_hw)(chp, 0))
    741  1.45  thorpej 			return;
    742  1.45  thorpej 
    743  1.47  thorpej 	ATADEBUG_PRINT(("atastart: xfer %p channel %d drive %d\n", xfer,
    744  1.45  thorpej 	    chp->ch_channel, xfer->c_drive), DEBUG_XFERS);
    745  1.45  thorpej 	if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_RESET) {
    746  1.45  thorpej 		chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_RESET;
    747  1.45  thorpej 		chp->ch_drive[xfer->c_drive].state = 0;
    748  1.45  thorpej 	}
    749  1.45  thorpej 	chp->ch_queue->active_xfer = xfer;
    750  1.45  thorpej 	TAILQ_REMOVE(&chp->ch_queue->queue_xfer, xfer, c_xferchain);
    751  1.45  thorpej 
    752  1.49  thorpej 	if (atac->atac_cap & ATAC_CAP_NOIRQ)
    753  1.45  thorpej 		KASSERT(xfer->c_flags & C_POLL);
    754  1.45  thorpej 	xfer->c_start(chp, xfer);
    755  1.44  thorpej }
    756  1.44  thorpej 
    757  1.41  thorpej struct ata_xfer *
    758  1.41  thorpej ata_get_xfer(int flags)
    759  1.41  thorpej {
    760  1.41  thorpej 	struct ata_xfer *xfer;
    761  1.41  thorpej 	int s;
    762  1.41  thorpej 
    763  1.41  thorpej 	s = splbio();
    764  1.41  thorpej 	xfer = pool_get(&ata_xfer_pool,
    765  1.41  thorpej 	    ((flags & ATAXF_NOSLEEP) != 0 ? PR_NOWAIT : PR_WAITOK));
    766  1.41  thorpej 	splx(s);
    767  1.41  thorpej 	if (xfer != NULL) {
    768  1.41  thorpej 		memset(xfer, 0, sizeof(struct ata_xfer));
    769  1.41  thorpej 	}
    770  1.41  thorpej 	return xfer;
    771  1.41  thorpej }
    772  1.41  thorpej 
    773  1.41  thorpej void
    774  1.48  thorpej ata_free_xfer(struct ata_channel *chp, struct ata_xfer *xfer)
    775  1.41  thorpej {
    776  1.49  thorpej 	struct atac_softc *atac = chp->ch_atac;
    777  1.41  thorpej 	int s;
    778  1.41  thorpej 
    779  1.49  thorpej 	if (atac->atac_free_hw)
    780  1.49  thorpej 		(*atac->atac_free_hw)(chp);
    781  1.41  thorpej 	s = splbio();
    782  1.41  thorpej 	pool_put(&ata_xfer_pool, xfer);
    783  1.41  thorpej 	splx(s);
    784  1.41  thorpej }
    785  1.41  thorpej 
    786  1.42  thorpej /*
    787  1.48  thorpej  * Kill off all pending xfers for a ata_channel.
    788  1.42  thorpej  *
    789  1.42  thorpej  * Must be called at splbio().
    790  1.42  thorpej  */
    791  1.42  thorpej void
    792  1.42  thorpej ata_kill_pending(struct ata_drive_datas *drvp)
    793  1.42  thorpej {
    794  1.48  thorpej 	struct ata_channel *chp = drvp->chnl_softc;
    795  1.42  thorpej 	struct ata_xfer *xfer, *next_xfer;
    796  1.42  thorpej 	int s = splbio();
    797  1.42  thorpej 
    798  1.42  thorpej 	for (xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer);
    799  1.42  thorpej 	    xfer != NULL; xfer = next_xfer) {
    800  1.42  thorpej 		next_xfer = TAILQ_NEXT(xfer, c_xferchain);
    801  1.42  thorpej 		if (xfer->c_chp != chp || xfer->c_drive != drvp->drive)
    802  1.42  thorpej 			continue;
    803  1.42  thorpej 		TAILQ_REMOVE(&chp->ch_queue->queue_xfer, xfer, c_xferchain);
    804  1.42  thorpej 		(*xfer->c_kill_xfer)(chp, xfer, KILL_GONE);
    805  1.42  thorpej 	}
    806  1.42  thorpej 
    807  1.42  thorpej 	while ((xfer = chp->ch_queue->active_xfer) != NULL) {
    808  1.42  thorpej 		if (xfer->c_chp == chp && xfer->c_drive == drvp->drive) {
    809  1.42  thorpej 			drvp->drive_flags |= DRIVE_WAITDRAIN;
    810  1.42  thorpej 			(void) tsleep(&chp->ch_queue->active_xfer,
    811  1.42  thorpej 			    PRIBIO, "atdrn", 0);
    812  1.42  thorpej 		} else {
    813  1.42  thorpej 			/* no more xfer for us */
    814  1.42  thorpej 			break;
    815  1.42  thorpej 		}
    816  1.42  thorpej 	}
    817  1.42  thorpej 	splx(s);
    818  1.42  thorpej }
    819  1.42  thorpej 
    820  1.55  thorpej /*
    821  1.55  thorpej  * ata_reset_channel:
    822  1.55  thorpej  *
    823  1.55  thorpej  *	Reset and ATA channel.
    824  1.55  thorpej  */
    825  1.55  thorpej void
    826  1.55  thorpej ata_reset_channel(struct ata_channel *chp, int flags)
    827  1.55  thorpej {
    828  1.55  thorpej 	struct atac_softc *atac = chp->ch_atac;
    829  1.55  thorpej 	int drive;
    830  1.55  thorpej 
    831  1.55  thorpej 	chp->ch_queue->queue_freeze++;
    832  1.55  thorpej 
    833  1.55  thorpej 	/*
    834  1.55  thorpej 	 * If we can poll or wait it's OK, otherwise wake up the
    835  1.55  thorpej 	 * kernel thread to do it for us.
    836  1.55  thorpej 	 */
    837  1.55  thorpej 	if ((flags & (AT_POLL | AT_WAIT)) == 0) {
    838  1.55  thorpej 		if (chp->ch_flags & ATACH_TH_RESET) {
    839  1.55  thorpej 			/* No need to schedule a reset more than one time. */
    840  1.55  thorpej 			return;
    841  1.55  thorpej 		}
    842  1.55  thorpej 		chp->ch_flags |= ATACH_TH_RESET;
    843  1.55  thorpej 		chp->ch_reset_flags = flags & (AT_RST_EMERG | AT_RST_NOCMD);
    844  1.55  thorpej 		wakeup(&chp->ch_thread);
    845  1.55  thorpej 		return;
    846  1.55  thorpej 	}
    847  1.55  thorpej 
    848  1.55  thorpej 	(*atac->atac_bustype_ata->ata_reset_channel)(chp, flags);
    849  1.55  thorpej 
    850  1.55  thorpej 	for (drive = 0; drive < chp->ch_ndrive; drive++)
    851  1.55  thorpej 		chp->ch_drive[drive].state = 0;
    852  1.55  thorpej 
    853  1.55  thorpej 	chp->ch_flags &= ~ATACH_TH_RESET;
    854  1.55  thorpej 	if ((flags & AT_RST_EMERG) == 0)  {
    855  1.55  thorpej 		chp->ch_queue->queue_freeze--;
    856  1.55  thorpej 		atastart(chp);
    857  1.55  thorpej 	} else {
    858  1.55  thorpej 		/* make sure that we can use polled commands */
    859  1.55  thorpej 		TAILQ_INIT(&chp->ch_queue->queue_xfer);
    860  1.55  thorpej 		chp->ch_queue->queue_freeze = 0;
    861  1.55  thorpej 		chp->ch_queue->active_xfer = NULL;
    862  1.55  thorpej 	}
    863  1.55  thorpej }
    864  1.55  thorpej 
    865  1.43  thorpej int
    866  1.48  thorpej ata_addref(struct ata_channel *chp)
    867  1.43  thorpej {
    868  1.49  thorpej 	struct atac_softc *atac = chp->ch_atac;
    869  1.49  thorpej 	struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
    870  1.43  thorpej 	int s, error = 0;
    871  1.43  thorpej 
    872  1.43  thorpej 	s = splbio();
    873  1.43  thorpej 	if (adapt->adapt_refcnt++ == 0 &&
    874  1.43  thorpej 	    adapt->adapt_enable != NULL) {
    875  1.49  thorpej 		error = (*adapt->adapt_enable)(&atac->atac_dev, 1);
    876  1.43  thorpej 		if (error)
    877  1.43  thorpej 			adapt->adapt_refcnt--;
    878  1.43  thorpej 	}
    879  1.43  thorpej 	splx(s);
    880  1.43  thorpej 	return (error);
    881  1.43  thorpej }
    882  1.43  thorpej 
    883  1.43  thorpej void
    884  1.48  thorpej ata_delref(struct ata_channel *chp)
    885  1.43  thorpej {
    886  1.49  thorpej 	struct atac_softc *atac = chp->ch_atac;
    887  1.49  thorpej 	struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
    888  1.43  thorpej 	int s;
    889  1.43  thorpej 
    890  1.43  thorpej 	s = splbio();
    891  1.43  thorpej 	if (adapt->adapt_refcnt-- == 1 &&
    892  1.43  thorpej 	    adapt->adapt_enable != NULL)
    893  1.49  thorpej 		(void) (*adapt->adapt_enable)(&atac->atac_dev, 0);
    894  1.43  thorpej 	splx(s);
    895  1.43  thorpej }
    896  1.43  thorpej 
    897  1.38  thorpej void
    898  1.48  thorpej ata_print_modes(struct ata_channel *chp)
    899  1.38  thorpej {
    900  1.49  thorpej 	struct atac_softc *atac = chp->ch_atac;
    901  1.38  thorpej 	int drive;
    902  1.38  thorpej 	struct ata_drive_datas *drvp;
    903  1.38  thorpej 
    904  1.38  thorpej 	for (drive = 0; drive < 2; drive++) {
    905  1.38  thorpej 		drvp = &chp->ch_drive[drive];
    906  1.38  thorpej 		if ((drvp->drive_flags & DRIVE) == 0)
    907  1.38  thorpej 			continue;
    908  1.38  thorpej 		aprint_normal("%s(%s:%d:%d): using PIO mode %d",
    909  1.38  thorpej 			drvp->drv_softc->dv_xname,
    910  1.49  thorpej 			atac->atac_dev.dv_xname,
    911  1.38  thorpej 			chp->ch_channel, drive, drvp->PIO_mode);
    912  1.38  thorpej 		if (drvp->drive_flags & DRIVE_DMA)
    913  1.38  thorpej 			aprint_normal(", DMA mode %d", drvp->DMA_mode);
    914  1.38  thorpej 		if (drvp->drive_flags & DRIVE_UDMA) {
    915  1.38  thorpej 			aprint_normal(", Ultra-DMA mode %d", drvp->UDMA_mode);
    916  1.38  thorpej 			if (drvp->UDMA_mode == 2)
    917  1.38  thorpej 				aprint_normal(" (Ultra/33)");
    918  1.38  thorpej 			else if (drvp->UDMA_mode == 4)
    919  1.38  thorpej 				aprint_normal(" (Ultra/66)");
    920  1.38  thorpej 			else if (drvp->UDMA_mode == 5)
    921  1.38  thorpej 				aprint_normal(" (Ultra/100)");
    922  1.38  thorpej 			else if (drvp->UDMA_mode == 6)
    923  1.38  thorpej 				aprint_normal(" (Ultra/133)");
    924  1.38  thorpej 		}
    925  1.38  thorpej 		if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA))
    926  1.38  thorpej 			aprint_normal(" (using DMA data transfers)");
    927  1.38  thorpej 		aprint_normal("\n");
    928  1.38  thorpej 	}
    929  1.38  thorpej }
    930  1.38  thorpej 
    931  1.39  thorpej /*
    932  1.39  thorpej  * downgrade the transfer mode of a drive after an error. return 1 if
    933  1.39  thorpej  * downgrade was possible, 0 otherwise.
    934  1.39  thorpej  */
    935  1.39  thorpej int
    936  1.39  thorpej ata_downgrade_mode(struct ata_drive_datas *drvp, int flags)
    937  1.39  thorpej {
    938  1.48  thorpej 	struct ata_channel *chp = drvp->chnl_softc;
    939  1.49  thorpej 	struct atac_softc *atac = chp->ch_atac;
    940  1.39  thorpej 	struct device *drv_dev = drvp->drv_softc;
    941  1.39  thorpej 	int cf_flags = drv_dev->dv_cfdata->cf_flags;
    942  1.39  thorpej 
    943  1.39  thorpej 	/* if drive or controller don't know its mode, we can't do much */
    944  1.39  thorpej 	if ((drvp->drive_flags & DRIVE_MODE) == 0 ||
    945  1.49  thorpej 	    (atac->atac_set_modes == NULL))
    946  1.39  thorpej 		return 0;
    947  1.39  thorpej 	/* current drive mode was set by a config flag, let it this way */
    948  1.39  thorpej 	if ((cf_flags & ATA_CONFIG_PIO_SET) ||
    949  1.39  thorpej 	    (cf_flags & ATA_CONFIG_DMA_SET) ||
    950  1.39  thorpej 	    (cf_flags & ATA_CONFIG_UDMA_SET))
    951  1.39  thorpej 		return 0;
    952  1.39  thorpej 
    953  1.39  thorpej 	/*
    954  1.39  thorpej 	 * If we were using Ultra-DMA mode, downgrade to the next lower mode.
    955  1.39  thorpej 	 */
    956  1.39  thorpej 	if ((drvp->drive_flags & DRIVE_UDMA) && drvp->UDMA_mode >= 2) {
    957  1.39  thorpej 		drvp->UDMA_mode--;
    958  1.39  thorpej 		printf("%s: transfer error, downgrading to Ultra-DMA mode %d\n",
    959  1.39  thorpej 		    drv_dev->dv_xname, drvp->UDMA_mode);
    960  1.39  thorpej 	}
    961  1.39  thorpej 
    962  1.39  thorpej 	/*
    963  1.39  thorpej 	 * If we were using ultra-DMA, don't downgrade to multiword DMA.
    964  1.39  thorpej 	 */
    965  1.39  thorpej 	else if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) {
    966  1.39  thorpej 		drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
    967  1.39  thorpej 		drvp->PIO_mode = drvp->PIO_cap;
    968  1.39  thorpej 		printf("%s: transfer error, downgrading to PIO mode %d\n",
    969  1.39  thorpej 		    drv_dev->dv_xname, drvp->PIO_mode);
    970  1.39  thorpej 	} else /* already using PIO, can't downgrade */
    971  1.39  thorpej 		return 0;
    972  1.39  thorpej 
    973  1.49  thorpej 	(*atac->atac_set_modes)(chp);
    974  1.39  thorpej 	ata_print_modes(chp);
    975  1.39  thorpej 	/* reset the channel, which will shedule all drives for setup */
    976  1.55  thorpej 	ata_reset_channel(chp, flags | AT_RST_NOCMD);
    977  1.39  thorpej 	return 1;
    978  1.39  thorpej }
    979  1.39  thorpej 
    980  1.40  thorpej /*
    981  1.40  thorpej  * Probe drive's capabilities, for use by the controller later
    982  1.40  thorpej  * Assumes drvp points to an existing drive.
    983  1.40  thorpej  */
    984  1.40  thorpej void
    985  1.40  thorpej ata_probe_caps(struct ata_drive_datas *drvp)
    986  1.40  thorpej {
    987  1.40  thorpej 	struct ataparams params, params2;
    988  1.48  thorpej 	struct ata_channel *chp = drvp->chnl_softc;
    989  1.49  thorpej 	struct atac_softc *atac = chp->ch_atac;
    990  1.40  thorpej 	struct device *drv_dev = drvp->drv_softc;
    991  1.40  thorpej 	int i, printed;
    992  1.40  thorpej 	char *sep = "";
    993  1.40  thorpej 	int cf_flags;
    994  1.40  thorpej 
    995  1.40  thorpej 	if (ata_get_params(drvp, AT_WAIT, &params) != CMD_OK) {
    996  1.40  thorpej 		/* IDENTIFY failed. Can't tell more about the device */
    997  1.40  thorpej 		return;
    998  1.40  thorpej 	}
    999  1.49  thorpej 	if ((atac->atac_cap & (ATAC_CAP_DATA16 | ATAC_CAP_DATA32)) ==
   1000  1.49  thorpej 	    (ATAC_CAP_DATA16 | ATAC_CAP_DATA32)) {
   1001  1.40  thorpej 		/*
   1002  1.40  thorpej 		 * Controller claims 16 and 32 bit transfers.
   1003  1.40  thorpej 		 * Re-do an IDENTIFY with 32-bit transfers,
   1004  1.40  thorpej 		 * and compare results.
   1005  1.40  thorpej 		 */
   1006  1.40  thorpej 		drvp->drive_flags |= DRIVE_CAP32;
   1007  1.40  thorpej 		ata_get_params(drvp, AT_WAIT, &params2);
   1008  1.40  thorpej 		if (memcmp(&params, &params2, sizeof(struct ataparams)) != 0) {
   1009  1.40  thorpej 			/* Not good. fall back to 16bits */
   1010  1.40  thorpej 			drvp->drive_flags &= ~DRIVE_CAP32;
   1011  1.40  thorpej 		} else {
   1012  1.40  thorpej 			aprint_normal("%s: 32-bit data port\n",
   1013  1.40  thorpej 			    drv_dev->dv_xname);
   1014  1.40  thorpej 		}
   1015  1.40  thorpej 	}
   1016  1.40  thorpej #if 0 /* Some ultra-DMA drives claims to only support ATA-3. sigh */
   1017  1.40  thorpej 	if (params.atap_ata_major > 0x01 &&
   1018  1.40  thorpej 	    params.atap_ata_major != 0xffff) {
   1019  1.40  thorpej 		for (i = 14; i > 0; i--) {
   1020  1.40  thorpej 			if (params.atap_ata_major & (1 << i)) {
   1021  1.40  thorpej 				aprint_normal("%s: ATA version %d\n",
   1022  1.40  thorpej 				    drv_dev->dv_xname, i);
   1023  1.40  thorpej 				drvp->ata_vers = i;
   1024  1.40  thorpej 				break;
   1025  1.40  thorpej 			}
   1026  1.40  thorpej 		}
   1027  1.40  thorpej 	}
   1028  1.40  thorpej #endif
   1029  1.40  thorpej 
   1030  1.40  thorpej 	/* An ATAPI device is at last PIO mode 3 */
   1031  1.40  thorpej 	if (drvp->drive_flags & DRIVE_ATAPI)
   1032  1.40  thorpej 		drvp->PIO_mode = 3;
   1033  1.40  thorpej 
   1034  1.40  thorpej 	/*
   1035  1.40  thorpej 	 * It's not in the specs, but it seems that some drive
   1036  1.40  thorpej 	 * returns 0xffff in atap_extensions when this field is invalid
   1037  1.40  thorpej 	 */
   1038  1.40  thorpej 	if (params.atap_extensions != 0xffff &&
   1039  1.40  thorpej 	    (params.atap_extensions & WDC_EXT_MODES)) {
   1040  1.40  thorpej 		printed = 0;
   1041  1.40  thorpej 		/*
   1042  1.40  thorpej 		 * XXX some drives report something wrong here (they claim to
   1043  1.40  thorpej 		 * support PIO mode 8 !). As mode is coded on 3 bits in
   1044  1.40  thorpej 		 * SET FEATURE, limit it to 7 (so limit i to 4).
   1045  1.40  thorpej 		 * If higher mode than 7 is found, abort.
   1046  1.40  thorpej 		 */
   1047  1.40  thorpej 		for (i = 7; i >= 0; i--) {
   1048  1.40  thorpej 			if ((params.atap_piomode_supp & (1 << i)) == 0)
   1049  1.40  thorpej 				continue;
   1050  1.40  thorpej 			if (i > 4)
   1051  1.40  thorpej 				return;
   1052  1.40  thorpej 			/*
   1053  1.40  thorpej 			 * See if mode is accepted.
   1054  1.40  thorpej 			 * If the controller can't set its PIO mode,
   1055  1.40  thorpej 			 * assume the defaults are good, so don't try
   1056  1.40  thorpej 			 * to set it
   1057  1.40  thorpej 			 */
   1058  1.49  thorpej 			if (atac->atac_set_modes)
   1059  1.40  thorpej 				/*
   1060  1.40  thorpej 				 * It's OK to pool here, it's fast enouth
   1061  1.40  thorpej 				 * to not bother waiting for interrupt
   1062  1.40  thorpej 				 */
   1063  1.40  thorpej 				if (ata_set_mode(drvp, 0x08 | (i + 3),
   1064  1.40  thorpej 				   AT_WAIT) != CMD_OK)
   1065  1.40  thorpej 					continue;
   1066  1.40  thorpej 			if (!printed) {
   1067  1.40  thorpej 				aprint_normal("%s: drive supports PIO mode %d",
   1068  1.40  thorpej 				    drv_dev->dv_xname, i + 3);
   1069  1.40  thorpej 				sep = ",";
   1070  1.40  thorpej 				printed = 1;
   1071  1.40  thorpej 			}
   1072  1.40  thorpej 			/*
   1073  1.40  thorpej 			 * If controller's driver can't set its PIO mode,
   1074  1.40  thorpej 			 * get the highter one for the drive.
   1075  1.40  thorpej 			 */
   1076  1.49  thorpej 			if (atac->atac_set_modes == NULL ||
   1077  1.49  thorpej 			    atac->atac_pio_cap >= i + 3) {
   1078  1.40  thorpej 				drvp->PIO_mode = i + 3;
   1079  1.40  thorpej 				drvp->PIO_cap = i + 3;
   1080  1.40  thorpej 				break;
   1081  1.40  thorpej 			}
   1082  1.40  thorpej 		}
   1083  1.40  thorpej 		if (!printed) {
   1084  1.40  thorpej 			/*
   1085  1.40  thorpej 			 * We didn't find a valid PIO mode.
   1086  1.40  thorpej 			 * Assume the values returned for DMA are buggy too
   1087  1.40  thorpej 			 */
   1088  1.40  thorpej 			return;
   1089  1.40  thorpej 		}
   1090  1.40  thorpej 		drvp->drive_flags |= DRIVE_MODE;
   1091  1.40  thorpej 		printed = 0;
   1092  1.40  thorpej 		for (i = 7; i >= 0; i--) {
   1093  1.40  thorpej 			if ((params.atap_dmamode_supp & (1 << i)) == 0)
   1094  1.40  thorpej 				continue;
   1095  1.49  thorpej 			if ((atac->atac_cap & ATAC_CAP_DMA) &&
   1096  1.49  thorpej 			    atac->atac_set_modes != NULL)
   1097  1.40  thorpej 				if (ata_set_mode(drvp, 0x20 | i, AT_WAIT)
   1098  1.40  thorpej 				    != CMD_OK)
   1099  1.40  thorpej 					continue;
   1100  1.40  thorpej 			if (!printed) {
   1101  1.40  thorpej 				aprint_normal("%s DMA mode %d", sep, i);
   1102  1.40  thorpej 				sep = ",";
   1103  1.40  thorpej 				printed = 1;
   1104  1.40  thorpej 			}
   1105  1.49  thorpej 			if (atac->atac_cap & ATAC_CAP_DMA) {
   1106  1.49  thorpej 				if (atac->atac_set_modes != NULL &&
   1107  1.49  thorpej 				    atac->atac_dma_cap < i)
   1108  1.40  thorpej 					continue;
   1109  1.40  thorpej 				drvp->DMA_mode = i;
   1110  1.40  thorpej 				drvp->DMA_cap = i;
   1111  1.40  thorpej 				drvp->drive_flags |= DRIVE_DMA;
   1112  1.40  thorpej 			}
   1113  1.40  thorpej 			break;
   1114  1.40  thorpej 		}
   1115  1.40  thorpej 		if (params.atap_extensions & WDC_EXT_UDMA_MODES) {
   1116  1.40  thorpej 			printed = 0;
   1117  1.40  thorpej 			for (i = 7; i >= 0; i--) {
   1118  1.40  thorpej 				if ((params.atap_udmamode_supp & (1 << i))
   1119  1.40  thorpej 				    == 0)
   1120  1.40  thorpej 					continue;
   1121  1.49  thorpej 				if (atac->atac_set_modes != NULL &&
   1122  1.49  thorpej 				    (atac->atac_cap & ATAC_CAP_UDMA))
   1123  1.40  thorpej 					if (ata_set_mode(drvp, 0x40 | i,
   1124  1.40  thorpej 					    AT_WAIT) != CMD_OK)
   1125  1.40  thorpej 						continue;
   1126  1.40  thorpej 				if (!printed) {
   1127  1.40  thorpej 					aprint_normal("%s Ultra-DMA mode %d",
   1128  1.40  thorpej 					    sep, i);
   1129  1.40  thorpej 					if (i == 2)
   1130  1.40  thorpej 						aprint_normal(" (Ultra/33)");
   1131  1.40  thorpej 					else if (i == 4)
   1132  1.40  thorpej 						aprint_normal(" (Ultra/66)");
   1133  1.40  thorpej 					else if (i == 5)
   1134  1.40  thorpej 						aprint_normal(" (Ultra/100)");
   1135  1.40  thorpej 					else if (i == 6)
   1136  1.40  thorpej 						aprint_normal(" (Ultra/133)");
   1137  1.40  thorpej 					sep = ",";
   1138  1.40  thorpej 					printed = 1;
   1139  1.40  thorpej 				}
   1140  1.49  thorpej 				if (atac->atac_cap & ATAC_CAP_UDMA) {
   1141  1.49  thorpej 					if (atac->atac_set_modes != NULL &&
   1142  1.49  thorpej 					    atac->atac_udma_cap < i)
   1143  1.40  thorpej 						continue;
   1144  1.40  thorpej 					drvp->UDMA_mode = i;
   1145  1.40  thorpej 					drvp->UDMA_cap = i;
   1146  1.40  thorpej 					drvp->drive_flags |= DRIVE_UDMA;
   1147  1.40  thorpej 				}
   1148  1.40  thorpej 				break;
   1149  1.40  thorpej 			}
   1150  1.40  thorpej 		}
   1151  1.40  thorpej 		aprint_normal("\n");
   1152  1.40  thorpej 	}
   1153  1.40  thorpej 
   1154  1.40  thorpej 	drvp->drive_flags &= ~DRIVE_NOSTREAM;
   1155  1.40  thorpej 	if (drvp->drive_flags & DRIVE_ATAPI) {
   1156  1.49  thorpej 		if (atac->atac_cap & ATAC_CAP_ATAPI_NOSTREAM)
   1157  1.40  thorpej 			drvp->drive_flags |= DRIVE_NOSTREAM;
   1158  1.40  thorpej 	} else {
   1159  1.49  thorpej 		if (atac->atac_cap & ATAC_CAP_ATA_NOSTREAM)
   1160  1.40  thorpej 			drvp->drive_flags |= DRIVE_NOSTREAM;
   1161  1.40  thorpej 	}
   1162  1.40  thorpej 
   1163  1.40  thorpej 	/* Try to guess ATA version here, if it didn't get reported */
   1164  1.40  thorpej 	if (drvp->ata_vers == 0) {
   1165  1.40  thorpej 		if (drvp->drive_flags & DRIVE_UDMA)
   1166  1.40  thorpej 			drvp->ata_vers = 4; /* should be at last ATA-4 */
   1167  1.40  thorpej 		else if (drvp->PIO_cap > 2)
   1168  1.40  thorpej 			drvp->ata_vers = 2; /* should be at last ATA-2 */
   1169  1.40  thorpej 	}
   1170  1.40  thorpej 	cf_flags = drv_dev->dv_cfdata->cf_flags;
   1171  1.40  thorpej 	if (cf_flags & ATA_CONFIG_PIO_SET) {
   1172  1.40  thorpej 		drvp->PIO_mode =
   1173  1.40  thorpej 		    (cf_flags & ATA_CONFIG_PIO_MODES) >> ATA_CONFIG_PIO_OFF;
   1174  1.40  thorpej 		drvp->drive_flags |= DRIVE_MODE;
   1175  1.40  thorpej 	}
   1176  1.49  thorpej 	if ((atac->atac_cap & ATAC_CAP_DMA) == 0) {
   1177  1.40  thorpej 		/* don't care about DMA modes */
   1178  1.40  thorpej 		return;
   1179  1.40  thorpej 	}
   1180  1.40  thorpej 	if (cf_flags & ATA_CONFIG_DMA_SET) {
   1181  1.40  thorpej 		if ((cf_flags & ATA_CONFIG_DMA_MODES) ==
   1182  1.40  thorpej 		    ATA_CONFIG_DMA_DISABLE) {
   1183  1.40  thorpej 			drvp->drive_flags &= ~DRIVE_DMA;
   1184  1.40  thorpej 		} else {
   1185  1.40  thorpej 			drvp->DMA_mode = (cf_flags & ATA_CONFIG_DMA_MODES) >>
   1186  1.40  thorpej 			    ATA_CONFIG_DMA_OFF;
   1187  1.40  thorpej 			drvp->drive_flags |= DRIVE_DMA | DRIVE_MODE;
   1188  1.40  thorpej 		}
   1189  1.40  thorpej 	}
   1190  1.49  thorpej 	if ((atac->atac_cap & ATAC_CAP_UDMA) == 0) {
   1191  1.40  thorpej 		/* don't care about UDMA modes */
   1192  1.40  thorpej 		return;
   1193  1.40  thorpej 	}
   1194  1.40  thorpej 	if (cf_flags & ATA_CONFIG_UDMA_SET) {
   1195  1.40  thorpej 		if ((cf_flags & ATA_CONFIG_UDMA_MODES) ==
   1196  1.40  thorpej 		    ATA_CONFIG_UDMA_DISABLE) {
   1197  1.40  thorpej 			drvp->drive_flags &= ~DRIVE_UDMA;
   1198  1.40  thorpej 		} else {
   1199  1.40  thorpej 			drvp->UDMA_mode = (cf_flags & ATA_CONFIG_UDMA_MODES) >>
   1200  1.40  thorpej 			    ATA_CONFIG_UDMA_OFF;
   1201  1.40  thorpej 			drvp->drive_flags |= DRIVE_UDMA | DRIVE_MODE;
   1202  1.40  thorpej 		}
   1203  1.40  thorpej 	}
   1204  1.40  thorpej }
   1205  1.40  thorpej 
   1206  1.30   bouyer /* management of the /dev/atabus* devices */
   1207  1.54  thorpej int
   1208  1.54  thorpej atabusopen(dev_t dev, int flag, int fmt, struct proc *p)
   1209  1.30   bouyer {
   1210  1.30   bouyer         struct atabus_softc *sc;
   1211  1.30   bouyer         int error, unit = minor(dev);
   1212  1.30   bouyer 
   1213  1.30   bouyer         if (unit >= atabus_cd.cd_ndevs ||
   1214  1.30   bouyer             (sc = atabus_cd.cd_devs[unit]) == NULL)
   1215  1.30   bouyer                 return (ENXIO);
   1216  1.30   bouyer 
   1217  1.30   bouyer         if (sc->sc_flags & ATABUSCF_OPEN)
   1218  1.30   bouyer                 return (EBUSY);
   1219  1.30   bouyer 
   1220  1.43  thorpej         if ((error = ata_addref(sc->sc_chan)) != 0)
   1221  1.30   bouyer                 return (error);
   1222  1.30   bouyer 
   1223  1.30   bouyer         sc->sc_flags |= ATABUSCF_OPEN;
   1224  1.30   bouyer 
   1225  1.30   bouyer         return (0);
   1226  1.30   bouyer }
   1227  1.30   bouyer 
   1228  1.30   bouyer 
   1229  1.30   bouyer int
   1230  1.54  thorpej atabusclose(dev_t dev, int flag, int fmt, struct proc *p)
   1231  1.30   bouyer {
   1232  1.30   bouyer         struct atabus_softc *sc = atabus_cd.cd_devs[minor(dev)];
   1233  1.30   bouyer 
   1234  1.43  thorpej         ata_delref(sc->sc_chan);
   1235  1.30   bouyer 
   1236  1.30   bouyer         sc->sc_flags &= ~ATABUSCF_OPEN;
   1237  1.30   bouyer 
   1238  1.30   bouyer         return (0);
   1239  1.30   bouyer }
   1240  1.30   bouyer 
   1241  1.30   bouyer int
   1242  1.54  thorpej atabusioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
   1243  1.30   bouyer {
   1244  1.30   bouyer         struct atabus_softc *sc = atabus_cd.cd_devs[minor(dev)];
   1245  1.48  thorpej 	struct ata_channel *chp = sc->sc_chan;
   1246  1.32   bouyer 	int min_drive, max_drive, drive;
   1247  1.30   bouyer         int error;
   1248  1.30   bouyer 	int s;
   1249  1.30   bouyer 
   1250  1.30   bouyer         /*
   1251  1.30   bouyer          * Enforce write permission for ioctls that change the
   1252  1.30   bouyer          * state of the bus.  Host adapter specific ioctls must
   1253  1.30   bouyer          * be checked by the adapter driver.
   1254  1.30   bouyer          */
   1255  1.30   bouyer         switch (cmd) {
   1256  1.30   bouyer         case ATABUSIOSCAN:
   1257  1.30   bouyer         case ATABUSIODETACH:
   1258  1.30   bouyer         case ATABUSIORESET:
   1259  1.30   bouyer                 if ((flag & FWRITE) == 0)
   1260  1.30   bouyer                         return (EBADF);
   1261  1.30   bouyer         }
   1262  1.30   bouyer 
   1263  1.30   bouyer         switch (cmd) {
   1264  1.30   bouyer         case ATABUSIORESET:
   1265  1.30   bouyer 		s = splbio();
   1266  1.55  thorpej 		ata_reset_channel(sc->sc_chan, AT_WAIT | AT_POLL);
   1267  1.30   bouyer 		splx(s);
   1268  1.30   bouyer 		error = 0;
   1269  1.30   bouyer 		break;
   1270  1.32   bouyer 	case ATABUSIOSCAN:
   1271  1.32   bouyer 	{
   1272  1.32   bouyer #if 0
   1273  1.32   bouyer 		struct atabusioscan_args *a=
   1274  1.32   bouyer 		    (struct atabusioscan_args *)addr;
   1275  1.32   bouyer #endif
   1276  1.32   bouyer 		if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) ||
   1277  1.32   bouyer 		    (chp->ch_drive[1].drive_flags & DRIVE_OLD))
   1278  1.32   bouyer 			return (EOPNOTSUPP);
   1279  1.32   bouyer 		return (EOPNOTSUPP);
   1280  1.32   bouyer 	}
   1281  1.32   bouyer 	case ATABUSIODETACH:
   1282  1.32   bouyer 	{
   1283  1.32   bouyer 		struct atabusioscan_args *a=
   1284  1.32   bouyer 		    (struct atabusioscan_args *)addr;
   1285  1.32   bouyer 		if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) ||
   1286  1.32   bouyer 		    (chp->ch_drive[1].drive_flags & DRIVE_OLD))
   1287  1.32   bouyer 			return (EOPNOTSUPP);
   1288  1.32   bouyer 		switch (a->at_dev) {
   1289  1.32   bouyer 		case -1:
   1290  1.32   bouyer 			min_drive = 0;
   1291  1.32   bouyer 			max_drive = 1;
   1292  1.32   bouyer 			break;
   1293  1.32   bouyer 		case 0:
   1294  1.32   bouyer 		case 1:
   1295  1.32   bouyer 			min_drive = max_drive = a->at_dev;
   1296  1.32   bouyer 			break;
   1297  1.32   bouyer 		default:
   1298  1.32   bouyer 			return (EINVAL);
   1299  1.32   bouyer 		}
   1300  1.32   bouyer 		for (drive = min_drive; drive <= max_drive; drive++) {
   1301  1.32   bouyer 			if (chp->ch_drive[drive].drv_softc != NULL) {
   1302  1.32   bouyer 				error = config_detach(
   1303  1.32   bouyer 				    chp->ch_drive[drive].drv_softc, 0);
   1304  1.32   bouyer 				if (error)
   1305  1.32   bouyer 					return (error);
   1306  1.34   bouyer 				chp->ch_drive[drive].drv_softc = NULL;
   1307  1.32   bouyer 			}
   1308  1.32   bouyer 		}
   1309  1.32   bouyer 		error = 0;
   1310  1.32   bouyer 		break;
   1311  1.32   bouyer 	}
   1312  1.30   bouyer 	default:
   1313  1.30   bouyer 		error = ENOTTY;
   1314  1.30   bouyer 	}
   1315  1.30   bouyer 	return (error);
   1316  1.30   bouyer };
   1317