Home | History | Annotate | Line # | Download | only in ata
ata.c revision 1.107
      1  1.107     pooka /*	$NetBSD: ata.c,v 1.107 2009/09/13 18:45:10 pooka 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.65     perry  * 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.107     pooka __KERNEL_RCSID(0, "$NetBSD: ata.c,v 1.107 2009/09/13 18:45:10 pooka Exp $");
     34    1.2    bouyer 
     35   1.88    dyoung #include "opt_ata.h"
     36    1.2    bouyer 
     37    1.2    bouyer #include <sys/param.h>
     38    1.2    bouyer #include <sys/systm.h>
     39    1.2    bouyer #include <sys/kernel.h>
     40    1.2    bouyer #include <sys/malloc.h>
     41    1.2    bouyer #include <sys/device.h>
     42   1.30    bouyer #include <sys/conf.h>
     43   1.30    bouyer #include <sys/fcntl.h>
     44   1.23   thorpej #include <sys/proc.h>
     45   1.41   thorpej #include <sys/pool.h>
     46   1.23   thorpej #include <sys/kthread.h>
     47   1.23   thorpej #include <sys/errno.h>
     48   1.30    bouyer #include <sys/ataio.h>
     49   1.93     pooka #include <sys/kmem.h>
     50   1.94        ad #include <sys/simplelock.h>
     51   1.91        ad #include <sys/intr.h>
     52   1.91        ad #include <sys/bus.h>
     53  1.107     pooka #include <sys/once.h>
     54   1.15    bouyer 
     55   1.76     itohy #include <dev/ata/ataconf.h>
     56    1.2    bouyer #include <dev/ata/atareg.h>
     57    1.2    bouyer #include <dev/ata/atavar.h>
     58   1.76     itohy #include <dev/ic/wdcvar.h>	/* for PIOBM */
     59    1.2    bouyer 
     60   1.23   thorpej #include "locators.h"
     61   1.23   thorpej 
     62   1.51   thorpej #include "atapibus.h"
     63   1.51   thorpej #include "ataraid.h"
     64   1.51   thorpej 
     65   1.65     perry #if NATARAID > 0
     66   1.65     perry #include <dev/ata/ata_raidvar.h>
     67   1.51   thorpej #endif
     68   1.51   thorpej 
     69    1.2    bouyer #define DEBUG_FUNCS  0x08
     70    1.2    bouyer #define DEBUG_PROBE  0x10
     71   1.23   thorpej #define DEBUG_DETACH 0x20
     72   1.44   thorpej #define	DEBUG_XFERS  0x40
     73   1.47   thorpej #ifdef ATADEBUG
     74   1.47   thorpej int atadebug_mask = 0;
     75   1.47   thorpej #define ATADEBUG_PRINT(args, level) \
     76   1.81     itohy 	if (atadebug_mask & (level)) \
     77    1.2    bouyer 		printf args
     78    1.2    bouyer #else
     79   1.47   thorpej #define ATADEBUG_PRINT(args, level)
     80    1.2    bouyer #endif
     81    1.2    bouyer 
     82  1.107     pooka static struct pool ata_xfer_pool;
     83   1.41   thorpej 
     84   1.51   thorpej /*
     85   1.51   thorpej  * A queue of atabus instances, used to ensure the same bus probe order
     86   1.51   thorpej  * for a given hardware configuration at each boot.
     87   1.51   thorpej  */
     88   1.51   thorpej struct atabus_initq_head atabus_initq_head =
     89   1.51   thorpej     TAILQ_HEAD_INITIALIZER(atabus_initq_head);
     90   1.51   thorpej struct simplelock atabus_interlock = SIMPLELOCK_INITIALIZER;
     91   1.51   thorpej 
     92  1.100    bouyer /* kernel thread probing devices on a atabus. Only one probing at once */
     93  1.100    bouyer struct lwp *atabus_configlwp;
     94  1.100    bouyer 
     95   1.23   thorpej /*****************************************************************************
     96   1.23   thorpej  * ATA bus layer.
     97   1.23   thorpej  *
     98   1.23   thorpej  * ATA controllers attach an atabus instance, which handles probing the bus
     99   1.23   thorpej  * for drives, etc.
    100   1.23   thorpej  *****************************************************************************/
    101   1.23   thorpej 
    102   1.30    bouyer dev_type_open(atabusopen);
    103   1.30    bouyer dev_type_close(atabusclose);
    104   1.30    bouyer dev_type_ioctl(atabusioctl);
    105   1.30    bouyer 
    106   1.30    bouyer const struct cdevsw atabus_cdevsw = {
    107   1.30    bouyer 	atabusopen, atabusclose, noread, nowrite, atabusioctl,
    108   1.75  christos 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER
    109   1.30    bouyer };
    110   1.30    bouyer 
    111   1.30    bouyer extern struct cfdriver atabus_cd;
    112   1.30    bouyer 
    113   1.95    dyoung static void atabus_childdetached(device_t, device_t);
    114   1.97    dyoung static bool atabus_resume(device_t PMF_FN_PROTO);
    115   1.97    dyoung static bool atabus_suspend(device_t PMF_FN_PROTO);
    116  1.100    bouyer static void atabusconfig_thread(void *);
    117   1.30    bouyer 
    118   1.23   thorpej /*
    119   1.23   thorpej  * atabusprint:
    120   1.23   thorpej  *
    121   1.23   thorpej  *	Autoconfiguration print routine used by ATA controllers when
    122   1.23   thorpej  *	attaching an atabus instance.
    123   1.23   thorpej  */
    124   1.23   thorpej int
    125   1.23   thorpej atabusprint(void *aux, const char *pnp)
    126   1.23   thorpej {
    127   1.48   thorpej 	struct ata_channel *chan = aux;
    128   1.65     perry 
    129   1.23   thorpej 	if (pnp)
    130   1.23   thorpej 		aprint_normal("atabus at %s", pnp);
    131   1.26   thorpej 	aprint_normal(" channel %d", chan->ch_channel);
    132   1.23   thorpej 
    133   1.23   thorpej 	return (UNCONF);
    134   1.23   thorpej }
    135   1.23   thorpej 
    136   1.23   thorpej /*
    137   1.23   thorpej  * ataprint:
    138   1.23   thorpej  *
    139   1.23   thorpej  *	Autoconfiguration print routine.
    140   1.23   thorpej  */
    141   1.23   thorpej int
    142   1.23   thorpej ataprint(void *aux, const char *pnp)
    143   1.23   thorpej {
    144   1.23   thorpej 	struct ata_device *adev = aux;
    145   1.23   thorpej 
    146   1.23   thorpej 	if (pnp)
    147   1.23   thorpej 		aprint_normal("wd at %s", pnp);
    148   1.23   thorpej 	aprint_normal(" drive %d", adev->adev_drv_data->drive);
    149   1.23   thorpej 
    150   1.23   thorpej 	return (UNCONF);
    151   1.23   thorpej }
    152   1.23   thorpej 
    153   1.52   thorpej /*
    154   1.52   thorpej  * ata_channel_attach:
    155   1.52   thorpej  *
    156   1.52   thorpej  *	Common parts of attaching an atabus to an ATA controller channel.
    157   1.52   thorpej  */
    158   1.52   thorpej void
    159   1.52   thorpej ata_channel_attach(struct ata_channel *chp)
    160   1.52   thorpej {
    161   1.52   thorpej 
    162   1.52   thorpej 	if (chp->ch_flags & ATACH_DISABLED)
    163   1.52   thorpej 		return;
    164   1.65     perry 
    165  1.105    dyoung 	/* XXX callout_destroy */
    166   1.90        ad 	callout_init(&chp->ch_callout, 0);
    167   1.52   thorpej 
    168   1.52   thorpej 	TAILQ_INIT(&chp->ch_queue->queue_xfer);
    169   1.52   thorpej 	chp->ch_queue->queue_freeze = 0;
    170   1.68    bouyer 	chp->ch_queue->queue_flags = 0;
    171   1.52   thorpej 	chp->ch_queue->active_xfer = NULL;
    172   1.52   thorpej 
    173   1.98      cube 	chp->atabus = config_found_ia(chp->ch_atac->atac_dev, "ata", chp,
    174   1.71  drochner 		atabusprint);
    175   1.52   thorpej }
    176   1.52   thorpej 
    177   1.51   thorpej static void
    178   1.51   thorpej atabusconfig(struct atabus_softc *atabus_sc)
    179   1.51   thorpej {
    180   1.51   thorpej 	struct ata_channel *chp = atabus_sc->sc_chan;
    181   1.51   thorpej 	struct atac_softc *atac = chp->ch_atac;
    182   1.51   thorpej 	struct atabus_initq *atabus_initq = NULL;
    183  1.102    bouyer 	int i, s, error;
    184  1.102    bouyer 
    185  1.102    bouyer 	/* we are in the atabus's thread context */
    186  1.102    bouyer 	s = splbio();
    187  1.102    bouyer 	chp->ch_flags |= ATACH_TH_RUN;
    188  1.102    bouyer 	splx(s);
    189   1.51   thorpej 
    190   1.51   thorpej 	/* Probe for the drives. */
    191   1.82    bouyer 	/* XXX for SATA devices we will power up all drives at once */
    192   1.51   thorpej 	(*atac->atac_probe)(chp);
    193   1.51   thorpej 
    194   1.51   thorpej 	ATADEBUG_PRINT(("atabusattach: ch_drive_flags 0x%x 0x%x\n",
    195   1.51   thorpej 	    chp->ch_drive[0].drive_flags, chp->ch_drive[1].drive_flags),
    196   1.51   thorpej 	    DEBUG_PROBE);
    197   1.51   thorpej 
    198  1.102    bouyer 	/* next operations will occurs in a separate thread */
    199  1.102    bouyer 	s = splbio();
    200  1.102    bouyer 	chp->ch_flags &= ~ATACH_TH_RUN;
    201  1.102    bouyer 	splx(s);
    202  1.102    bouyer 
    203  1.100    bouyer 	/* Make sure the devices probe in atabus order to avoid jitter. */
    204  1.100    bouyer 	simple_lock(&atabus_interlock);
    205  1.100    bouyer 	while(1) {
    206  1.100    bouyer 		atabus_initq = TAILQ_FIRST(&atabus_initq_head);
    207  1.100    bouyer 		if (atabus_initq->atabus_sc == atabus_sc)
    208  1.100    bouyer 			break;
    209  1.100    bouyer 		ltsleep(&atabus_initq_head, PRIBIO, "ata_initq", 0,
    210  1.100    bouyer 		    &atabus_interlock);
    211  1.100    bouyer 	}
    212  1.100    bouyer 	simple_unlock(&atabus_interlock);
    213  1.100    bouyer 
    214   1.51   thorpej 	/* If no drives, abort here */
    215   1.51   thorpej 	for (i = 0; i < chp->ch_ndrive; i++)
    216   1.51   thorpej 		if ((chp->ch_drive[i].drive_flags & DRIVE) != 0)
    217   1.51   thorpej 			break;
    218   1.51   thorpej 	if (i == chp->ch_ndrive)
    219   1.51   thorpej 		goto out;
    220   1.51   thorpej 
    221   1.51   thorpej 	/* Shortcut in case we've been shutdown */
    222   1.51   thorpej 	if (chp->ch_flags & ATACH_SHUTDOWN)
    223   1.51   thorpej 		goto out;
    224   1.51   thorpej 
    225  1.100    bouyer 
    226  1.100    bouyer 	if ((error = kthread_create(PRI_NONE, 0, NULL, atabusconfig_thread,
    227  1.100    bouyer 	    atabus_sc, &atabus_configlwp,
    228  1.100    bouyer 	    "%scnf", device_xname(atac->atac_dev))) != 0)
    229  1.100    bouyer 		aprint_error_dev(atac->atac_dev,
    230  1.100    bouyer 		    "unable to create config thread: error %d\n", error);
    231  1.100    bouyer 	return;
    232  1.100    bouyer 
    233  1.100    bouyer  out:
    234   1.51   thorpej 	simple_lock(&atabus_interlock);
    235  1.100    bouyer 	TAILQ_REMOVE(&atabus_initq_head, atabus_initq, atabus_initq);
    236   1.51   thorpej 	simple_unlock(&atabus_interlock);
    237   1.51   thorpej 
    238  1.100    bouyer 	free(atabus_initq, M_DEVBUF);
    239  1.100    bouyer 	wakeup(&atabus_initq_head);
    240  1.100    bouyer 
    241  1.100    bouyer 	ata_delref(chp);
    242  1.100    bouyer 
    243  1.100    bouyer 	config_pending_decr();
    244  1.100    bouyer }
    245  1.100    bouyer 
    246  1.100    bouyer /*
    247  1.100    bouyer  * atabus_configthread: finish attach of atabus's childrens, in a separate
    248  1.100    bouyer  * kernel thread.
    249  1.100    bouyer  */
    250  1.100    bouyer static void
    251  1.100    bouyer atabusconfig_thread(void *arg)
    252  1.100    bouyer {
    253  1.100    bouyer 	struct atabus_softc *atabus_sc = arg;
    254  1.100    bouyer 	struct ata_channel *chp = atabus_sc->sc_chan;
    255  1.100    bouyer 	struct atac_softc *atac = chp->ch_atac;
    256  1.100    bouyer 	int i, s;
    257  1.100    bouyer 	struct atabus_initq *atabus_initq = NULL;
    258  1.100    bouyer 
    259  1.100    bouyer 	simple_lock(&atabus_interlock);
    260  1.100    bouyer 	atabus_initq = TAILQ_FIRST(&atabus_initq_head);
    261  1.100    bouyer 	simple_unlock(&atabus_interlock);
    262  1.100    bouyer 	KASSERT(atabus_initq->atabus_sc == atabus_sc);
    263   1.51   thorpej 	/*
    264   1.51   thorpej 	 * Attach an ATAPI bus, if needed.
    265   1.51   thorpej 	 */
    266   1.51   thorpej 	for (i = 0; i < chp->ch_ndrive; i++) {
    267   1.51   thorpej 		if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI) {
    268   1.51   thorpej #if NATAPIBUS > 0
    269   1.51   thorpej 			(*atac->atac_atapibus_attach)(atabus_sc);
    270   1.51   thorpej #else
    271   1.51   thorpej 			/*
    272   1.51   thorpej 			 * Fake the autoconfig "not configured" message
    273   1.51   thorpej 			 */
    274   1.51   thorpej 			aprint_normal("atapibus at %s not configured\n",
    275   1.98      cube 			    device_xname(atac->atac_dev));
    276   1.51   thorpej 			chp->atapibus = NULL;
    277   1.58   thorpej 			s = splbio();
    278   1.51   thorpej 			for (i = 0; i < chp->ch_ndrive; i++)
    279   1.51   thorpej 				chp->ch_drive[i].drive_flags &= ~DRIVE_ATAPI;
    280   1.58   thorpej 			splx(s);
    281   1.51   thorpej #endif
    282   1.51   thorpej 			break;
    283   1.51   thorpej 		}
    284   1.51   thorpej 	}
    285   1.51   thorpej 
    286   1.51   thorpej 	for (i = 0; i < chp->ch_ndrive; i++) {
    287   1.51   thorpej 		struct ata_device adev;
    288   1.51   thorpej 		if ((chp->ch_drive[i].drive_flags &
    289   1.51   thorpej 		    (DRIVE_ATA | DRIVE_OLD)) == 0) {
    290   1.51   thorpej 			continue;
    291   1.51   thorpej 		}
    292   1.51   thorpej 		memset(&adev, 0, sizeof(struct ata_device));
    293   1.51   thorpej 		adev.adev_bustype = atac->atac_bustype_ata;
    294   1.51   thorpej 		adev.adev_channel = chp->ch_channel;
    295   1.51   thorpej 		adev.adev_openings = 1;
    296   1.51   thorpej 		adev.adev_drv_data = &chp->ch_drive[i];
    297   1.98      cube 		chp->ata_drives[i] = config_found_ia(atabus_sc->sc_dev,
    298   1.71  drochner 		    "ata_hl", &adev, ataprint);
    299   1.51   thorpej 		if (chp->ata_drives[i] != NULL)
    300   1.51   thorpej 			ata_probe_caps(&chp->ch_drive[i]);
    301   1.58   thorpej 		else {
    302   1.58   thorpej 			s = splbio();
    303   1.51   thorpej 			chp->ch_drive[i].drive_flags &=
    304   1.51   thorpej 			    ~(DRIVE_ATA | DRIVE_OLD);
    305   1.58   thorpej 			splx(s);
    306   1.58   thorpej 		}
    307   1.51   thorpej 	}
    308   1.51   thorpej 
    309   1.51   thorpej 	/* now that we know the drives, the controller can set its modes */
    310   1.51   thorpej 	if (atac->atac_set_modes) {
    311   1.51   thorpej 		(*atac->atac_set_modes)(chp);
    312   1.51   thorpej 		ata_print_modes(chp);
    313   1.51   thorpej 	}
    314   1.51   thorpej #if NATARAID > 0
    315   1.51   thorpej 	if (atac->atac_cap & ATAC_CAP_RAID)
    316   1.51   thorpej 		for (i = 0; i < chp->ch_ndrive; i++)
    317   1.51   thorpej 			if (chp->ata_drives[i] != NULL)
    318   1.51   thorpej 				ata_raid_check_component(chp->ata_drives[i]);
    319   1.51   thorpej #endif /* NATARAID > 0 */
    320   1.51   thorpej 
    321   1.51   thorpej 	/*
    322   1.51   thorpej 	 * reset drive_flags for unattached devices, reset state for attached
    323   1.51   thorpej 	 * ones
    324   1.51   thorpej 	 */
    325   1.58   thorpej 	s = splbio();
    326   1.51   thorpej 	for (i = 0; i < chp->ch_ndrive; i++) {
    327   1.51   thorpej 		if (chp->ch_drive[i].drv_softc == NULL)
    328   1.51   thorpej 			chp->ch_drive[i].drive_flags = 0;
    329   1.51   thorpej 		else
    330   1.51   thorpej 			chp->ch_drive[i].state = 0;
    331   1.51   thorpej 	}
    332   1.58   thorpej 	splx(s);
    333   1.51   thorpej 
    334   1.81     itohy 	simple_lock(&atabus_interlock);
    335   1.81     itohy 	TAILQ_REMOVE(&atabus_initq_head, atabus_initq, atabus_initq);
    336   1.81     itohy 	simple_unlock(&atabus_interlock);
    337   1.51   thorpej 
    338   1.81     itohy 	free(atabus_initq, M_DEVBUF);
    339   1.81     itohy 	wakeup(&atabus_initq_head);
    340   1.51   thorpej 
    341   1.51   thorpej 	ata_delref(chp);
    342   1.51   thorpej 
    343   1.51   thorpej 	config_pending_decr();
    344  1.100    bouyer 	kthread_exit(0);
    345   1.51   thorpej }
    346   1.51   thorpej 
    347   1.23   thorpej /*
    348   1.23   thorpej  * atabus_thread:
    349   1.23   thorpej  *
    350   1.23   thorpej  *	Worker thread for the ATA bus.
    351   1.23   thorpej  */
    352   1.23   thorpej static void
    353   1.23   thorpej atabus_thread(void *arg)
    354   1.23   thorpej {
    355   1.23   thorpej 	struct atabus_softc *sc = arg;
    356   1.48   thorpej 	struct ata_channel *chp = sc->sc_chan;
    357   1.24   thorpej 	struct ata_xfer *xfer;
    358   1.48   thorpej 	int i, s;
    359   1.23   thorpej 
    360   1.23   thorpej 	s = splbio();
    361  1.102    bouyer 	chp->ch_flags |= ATACH_TH_RUN;
    362  1.102    bouyer 
    363   1.48   thorpej 	/*
    364   1.48   thorpej 	 * Probe the drives.  Reset all flags to 0 to indicate to controllers
    365   1.48   thorpej 	 * that can re-probe that all drives must be probed..
    366   1.48   thorpej 	 *
    367   1.48   thorpej 	 * Note: ch_ndrive may be changed during the probe.
    368   1.48   thorpej 	 */
    369   1.48   thorpej 	for (i = 0; i < ATA_MAXDRIVES; i++)
    370   1.48   thorpej 		chp->ch_drive[i].drive_flags = 0;
    371   1.23   thorpej 	splx(s);
    372   1.23   thorpej 
    373   1.68    bouyer 	atabusconfig(sc);
    374   1.23   thorpej 
    375   1.69    bouyer 	s = splbio();
    376   1.23   thorpej 	for (;;) {
    377   1.48   thorpej 		if ((chp->ch_flags & (ATACH_TH_RESET | ATACH_SHUTDOWN)) == 0 &&
    378   1.33    bouyer 		    (chp->ch_queue->active_xfer == NULL ||
    379   1.23   thorpej 		     chp->ch_queue->queue_freeze == 0)) {
    380  1.102    bouyer 			chp->ch_flags &= ~ATACH_TH_RUN;
    381   1.27   thorpej 			(void) tsleep(&chp->ch_thread, PRIBIO, "atath", 0);
    382  1.102    bouyer 			chp->ch_flags |= ATACH_TH_RUN;
    383   1.23   thorpej 		}
    384   1.59   thorpej 		if (chp->ch_flags & ATACH_SHUTDOWN) {
    385   1.23   thorpej 			break;
    386   1.59   thorpej 		}
    387   1.48   thorpej 		if (chp->ch_flags & ATACH_TH_RESET) {
    388   1.31    bouyer 			/*
    389   1.55   thorpej 			 * ata_reset_channel() will freeze 2 times, so
    390   1.31    bouyer 			 * unfreeze one time. Not a problem as we're at splbio
    391   1.31    bouyer 			 */
    392   1.23   thorpej 			chp->ch_queue->queue_freeze--;
    393   1.55   thorpej 			ata_reset_channel(chp, AT_WAIT | chp->ch_reset_flags);
    394   1.33    bouyer 		} else if (chp->ch_queue->active_xfer != NULL &&
    395   1.23   thorpej 			   chp->ch_queue->queue_freeze == 1) {
    396   1.23   thorpej 			/*
    397   1.23   thorpej 			 * Caller has bumped queue_freeze, decrease it.
    398   1.23   thorpej 			 */
    399   1.23   thorpej 			chp->ch_queue->queue_freeze--;
    400   1.33    bouyer 			xfer = chp->ch_queue->active_xfer;
    401   1.23   thorpej 			KASSERT(xfer != NULL);
    402   1.61    bouyer 			(*xfer->c_start)(xfer->c_chp, xfer);
    403   1.23   thorpej 		} else if (chp->ch_queue->queue_freeze > 1)
    404   1.23   thorpej 			panic("ata_thread: queue_freeze");
    405   1.23   thorpej 	}
    406   1.69    bouyer 	splx(s);
    407   1.27   thorpej 	chp->ch_thread = NULL;
    408   1.70  christos 	wakeup(&chp->ch_flags);
    409   1.23   thorpej 	kthread_exit(0);
    410   1.23   thorpej }
    411   1.23   thorpej 
    412   1.23   thorpej /*
    413   1.23   thorpej  * atabus_match:
    414   1.23   thorpej  *
    415   1.23   thorpej  *	Autoconfiguration match routine.
    416   1.23   thorpej  */
    417   1.23   thorpej static int
    418  1.106    cegger atabus_match(device_t parent, cfdata_t cf, void *aux)
    419   1.23   thorpej {
    420   1.48   thorpej 	struct ata_channel *chp = aux;
    421   1.23   thorpej 
    422   1.23   thorpej 	if (chp == NULL)
    423   1.23   thorpej 		return (0);
    424   1.65     perry 
    425   1.26   thorpej 	if (cf->cf_loc[ATACF_CHANNEL] != chp->ch_channel &&
    426   1.23   thorpej 	    cf->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT)
    427   1.81     itohy 		return (0);
    428   1.65     perry 
    429   1.23   thorpej 	return (1);
    430   1.23   thorpej }
    431   1.23   thorpej 
    432  1.107     pooka static int
    433  1.107     pooka atabus_xferpool_init(void)
    434  1.107     pooka {
    435  1.107     pooka 
    436  1.107     pooka 	pool_init(&ata_xfer_pool, sizeof(struct ata_xfer), 0, 0, 0, "ataspl",
    437  1.107     pooka 	    NULL, IPL_BIO);
    438  1.107     pooka 	return 0;
    439  1.107     pooka }
    440  1.107     pooka 
    441   1.23   thorpej /*
    442   1.23   thorpej  * atabus_attach:
    443   1.23   thorpej  *
    444   1.23   thorpej  *	Autoconfiguration attach routine.
    445   1.23   thorpej  */
    446   1.23   thorpej static void
    447   1.95    dyoung atabus_attach(device_t parent, device_t self, void *aux)
    448   1.23   thorpej {
    449   1.95    dyoung 	struct atabus_softc *sc = device_private(self);
    450   1.48   thorpej 	struct ata_channel *chp = aux;
    451   1.23   thorpej 	struct atabus_initq *initq;
    452  1.107     pooka 	static ONCE_DECL(poolinit_ctrl);
    453   1.90        ad 	int error;
    454   1.23   thorpej 
    455   1.23   thorpej 	sc->sc_chan = chp;
    456   1.23   thorpej 
    457   1.23   thorpej 	aprint_normal("\n");
    458   1.23   thorpej 	aprint_naive("\n");
    459   1.23   thorpej 
    460   1.98      cube 	sc->sc_dev = self;
    461   1.98      cube 
    462   1.81     itohy 	if (ata_addref(chp))
    463   1.81     itohy 		return;
    464   1.35   mycroft 
    465  1.107     pooka 	RUN_ONCE(&poolinit_ctrl, atabus_xferpool_init);
    466  1.107     pooka 
    467   1.23   thorpej 	initq = malloc(sizeof(*initq), M_DEVBUF, M_WAITOK);
    468   1.23   thorpej 	initq->atabus_sc = sc;
    469   1.23   thorpej 	TAILQ_INSERT_TAIL(&atabus_initq_head, initq, atabus_initq);
    470   1.23   thorpej 	config_pending_incr();
    471   1.90        ad 
    472   1.90        ad 	if ((error = kthread_create(PRI_NONE, 0, NULL, atabus_thread, sc,
    473   1.95    dyoung 	    &chp->ch_thread, "%s", device_xname(self))) != 0)
    474   1.95    dyoung 		aprint_error_dev(self,
    475   1.95    dyoung 		    "unable to create kernel thread: error %d\n", error);
    476   1.64  jmcneill 
    477   1.92  jmcneill 	if (!pmf_device_register(self, atabus_suspend, atabus_resume))
    478   1.92  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    479   1.23   thorpej }
    480   1.23   thorpej 
    481   1.23   thorpej /*
    482   1.23   thorpej  * atabus_activate:
    483   1.23   thorpej  *
    484   1.23   thorpej  *	Autoconfiguration activation routine.
    485   1.23   thorpej  */
    486   1.23   thorpej static int
    487   1.95    dyoung atabus_activate(device_t self, enum devact act)
    488   1.23   thorpej {
    489   1.95    dyoung 	struct atabus_softc *sc = device_private(self);
    490   1.48   thorpej 	struct ata_channel *chp = sc->sc_chan;
    491   1.95    dyoung 	device_t dev = NULL;
    492   1.23   thorpej 	int s, i, error = 0;
    493   1.23   thorpej 
    494   1.23   thorpej 	s = splbio();
    495   1.23   thorpej 	switch (act) {
    496   1.23   thorpej 	case DVACT_ACTIVATE:
    497   1.23   thorpej 		error = EOPNOTSUPP;
    498   1.23   thorpej 		break;
    499   1.65     perry 
    500   1.23   thorpej 	case DVACT_DEACTIVATE:
    501   1.23   thorpej 		/*
    502   1.23   thorpej 		 * We might deactivate the children of atapibus twice
    503   1.23   thorpej 		 * (once bia atapibus, once directly), but since the
    504   1.23   thorpej 		 * generic autoconfiguration code maintains the DVF_ACTIVE
    505   1.23   thorpej 		 * flag, it's safe.
    506   1.23   thorpej 		 */
    507   1.23   thorpej 		if ((dev = chp->atapibus) != NULL) {
    508   1.23   thorpej 			error = config_deactivate(dev);
    509   1.23   thorpej 			if (error)
    510   1.23   thorpej 				goto out;
    511   1.23   thorpej 		}
    512   1.23   thorpej 
    513   1.48   thorpej 		for (i = 0; i < chp->ch_ndrive; i++) {
    514  1.104    dyoung 			if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI)
    515  1.104    dyoung 				continue;
    516   1.23   thorpej 			if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
    517   1.47   thorpej 				ATADEBUG_PRINT(("atabus_activate: %s: "
    518   1.95    dyoung 				    "deactivating %s\n", device_xname(self),
    519   1.95    dyoung 				    device_xname(dev)),
    520   1.23   thorpej 				    DEBUG_DETACH);
    521   1.23   thorpej 				error = config_deactivate(dev);
    522   1.23   thorpej 				if (error)
    523   1.23   thorpej 					goto out;
    524   1.23   thorpej 			}
    525   1.23   thorpej 		}
    526   1.23   thorpej 		break;
    527   1.23   thorpej 	}
    528   1.23   thorpej  out:
    529   1.23   thorpej 	splx(s);
    530   1.23   thorpej 
    531   1.47   thorpej #ifdef ATADEBUG
    532   1.23   thorpej 	if (dev != NULL && error != 0)
    533   1.47   thorpej 		ATADEBUG_PRINT(("atabus_activate: %s: "
    534   1.95    dyoung 		    "error %d deactivating %s\n", device_xname(self),
    535   1.95    dyoung 		    error, device_xname(dev)), DEBUG_DETACH);
    536   1.47   thorpej #endif /* ATADEBUG */
    537   1.23   thorpej 
    538   1.23   thorpej 	return (error);
    539   1.23   thorpej }
    540   1.23   thorpej 
    541   1.23   thorpej /*
    542   1.23   thorpej  * atabus_detach:
    543   1.23   thorpej  *
    544   1.23   thorpej  *	Autoconfiguration detach routine.
    545   1.23   thorpej  */
    546   1.23   thorpej static int
    547   1.95    dyoung atabus_detach(device_t self, int flags)
    548   1.23   thorpej {
    549   1.95    dyoung 	struct atabus_softc *sc = device_private(self);
    550   1.48   thorpej 	struct ata_channel *chp = sc->sc_chan;
    551   1.95    dyoung 	device_t dev = NULL;
    552   1.57   thorpej 	int s, i, error = 0;
    553   1.23   thorpej 
    554   1.23   thorpej 	/* Shutdown the channel. */
    555   1.57   thorpej 	s = splbio();		/* XXX ALSO NEED AN INTERLOCK HERE. */
    556   1.48   thorpej 	chp->ch_flags |= ATACH_SHUTDOWN;
    557   1.57   thorpej 	splx(s);
    558   1.95    dyoung 
    559   1.27   thorpej 	wakeup(&chp->ch_thread);
    560   1.95    dyoung 
    561   1.27   thorpej 	while (chp->ch_thread != NULL)
    562   1.70  christos 		(void) tsleep(&chp->ch_flags, PRIBIO, "atadown", 0);
    563   1.65     perry 
    564   1.95    dyoung 
    565   1.23   thorpej 	/*
    566   1.23   thorpej 	 * Detach atapibus and its children.
    567   1.23   thorpej 	 */
    568   1.23   thorpej 	if ((dev = chp->atapibus) != NULL) {
    569   1.47   thorpej 		ATADEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
    570   1.95    dyoung 		    device_xname(self), device_xname(dev)), DEBUG_DETACH);
    571   1.95    dyoung 
    572   1.23   thorpej 		error = config_detach(dev, flags);
    573   1.23   thorpej 		if (error)
    574   1.23   thorpej 			goto out;
    575  1.104    dyoung 		KASSERT(chp->atapibus == NULL);
    576   1.23   thorpej 	}
    577   1.23   thorpej 
    578   1.23   thorpej 	/*
    579   1.23   thorpej 	 * Detach our other children.
    580   1.23   thorpej 	 */
    581   1.48   thorpej 	for (i = 0; i < chp->ch_ndrive; i++) {
    582   1.23   thorpej 		if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI)
    583   1.23   thorpej 			continue;
    584  1.104    dyoung 		if ((dev = chp->ata_drives[i]) != NULL) {
    585  1.104    dyoung 			ATADEBUG_PRINT(("%s.%d: %s: detaching %s\n", __func__,
    586  1.104    dyoung 			    __LINE__, device_xname(self), device_xname(dev)),
    587   1.23   thorpej 			    DEBUG_DETACH);
    588  1.104    dyoung 			KASSERT(chp->ch_drive[i].drv_softc ==
    589  1.104    dyoung 			        chp->ata_drives[i]);
    590   1.23   thorpej 			error = config_detach(dev, flags);
    591   1.23   thorpej 			if (error)
    592   1.23   thorpej 				goto out;
    593  1.104    dyoung 			KASSERT(chp->ata_drives[i] == NULL);
    594   1.23   thorpej 		}
    595   1.23   thorpej 	}
    596   1.23   thorpej 
    597   1.23   thorpej  out:
    598   1.47   thorpej #ifdef ATADEBUG
    599   1.23   thorpej 	if (dev != NULL && error != 0)
    600  1.104    dyoung 		ATADEBUG_PRINT(("%s: %s: error %d detaching %s\n", __func__,
    601   1.95    dyoung 		    device_xname(self), error, device_xname(dev)),
    602   1.23   thorpej 		    DEBUG_DETACH);
    603   1.47   thorpej #endif /* ATADEBUG */
    604   1.23   thorpej 
    605   1.23   thorpej 	return (error);
    606   1.23   thorpej }
    607   1.23   thorpej 
    608   1.95    dyoung void
    609   1.95    dyoung atabus_childdetached(device_t self, device_t child)
    610   1.95    dyoung {
    611  1.104    dyoung 	bool found = false;
    612   1.95    dyoung 	struct atabus_softc *sc = device_private(self);
    613   1.95    dyoung 	struct ata_channel *chp = sc->sc_chan;
    614   1.95    dyoung 	int i;
    615   1.95    dyoung 
    616   1.95    dyoung 	/*
    617   1.95    dyoung 	 * atapibus detached.
    618   1.95    dyoung 	 */
    619   1.95    dyoung 	if (child == chp->atapibus) {
    620   1.95    dyoung 		chp->atapibus = NULL;
    621  1.104    dyoung 		found = true;
    622   1.95    dyoung 	}
    623   1.95    dyoung 
    624   1.95    dyoung 	/*
    625   1.95    dyoung 	 * Detach our other children.
    626   1.95    dyoung 	 */
    627   1.95    dyoung 	for (i = 0; i < chp->ch_ndrive; i++) {
    628   1.95    dyoung 		if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI)
    629   1.95    dyoung 			continue;
    630  1.104    dyoung 		if (child == chp->ata_drives[i]) {
    631  1.104    dyoung 			KASSERT(chp->ata_drives[i] ==
    632  1.104    dyoung 			        chp->ch_drive[i].drv_softc);
    633  1.104    dyoung 			chp->ata_drives[i] = NULL;
    634   1.95    dyoung 			chp->ch_drive[i].drv_softc = NULL;
    635   1.95    dyoung 			chp->ch_drive[i].drive_flags = 0;
    636  1.104    dyoung 			found = true;
    637   1.95    dyoung 		}
    638   1.95    dyoung 	}
    639   1.95    dyoung 
    640  1.104    dyoung 	if (!found)
    641  1.104    dyoung 		panic("%s: unknown child %p", device_xname(self),
    642  1.104    dyoung 		    (const void *)child);
    643   1.95    dyoung }
    644   1.95    dyoung 
    645  1.103    dyoung CFATTACH_DECL3_NEW(atabus, sizeof(struct atabus_softc),
    646   1.95    dyoung     atabus_match, atabus_attach, atabus_detach, atabus_activate, NULL,
    647  1.103    dyoung     atabus_childdetached, DVF_DETACH_SHUTDOWN);
    648   1.23   thorpej 
    649   1.23   thorpej /*****************************************************************************
    650   1.23   thorpej  * Common ATA bus operations.
    651   1.23   thorpej  *****************************************************************************/
    652   1.23   thorpej 
    653    1.2    bouyer /* Get the disk's parameters */
    654    1.2    bouyer int
    655   1.21   thorpej ata_get_params(struct ata_drive_datas *drvp, u_int8_t flags,
    656   1.21   thorpej     struct ataparams *prms)
    657    1.2    bouyer {
    658   1.36   thorpej 	struct ata_command ata_c;
    659   1.50   thorpej 	struct ata_channel *chp = drvp->chnl_softc;
    660   1.50   thorpej 	struct atac_softc *atac = chp->ch_atac;
    661   1.93     pooka 	char *tb;
    662   1.93     pooka 	int i, rv;
    663    1.2    bouyer 	u_int16_t *p;
    664    1.2    bouyer 
    665   1.88    dyoung 	ATADEBUG_PRINT(("%s\n", __func__), DEBUG_FUNCS);
    666    1.2    bouyer 
    667   1.93     pooka 	tb = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
    668    1.2    bouyer 	memset(prms, 0, sizeof(struct ataparams));
    669   1.36   thorpej 	memset(&ata_c, 0, sizeof(struct ata_command));
    670    1.2    bouyer 
    671    1.2    bouyer 	if (drvp->drive_flags & DRIVE_ATA) {
    672   1.36   thorpej 		ata_c.r_command = WDCC_IDENTIFY;
    673   1.36   thorpej 		ata_c.r_st_bmask = WDCS_DRDY;
    674   1.73    bouyer 		ata_c.r_st_pmask = WDCS_DRQ;
    675   1.36   thorpej 		ata_c.timeout = 3000; /* 3s */
    676    1.7    bouyer 	} else if (drvp->drive_flags & DRIVE_ATAPI) {
    677   1.36   thorpej 		ata_c.r_command = ATAPI_IDENTIFY_DEVICE;
    678   1.36   thorpej 		ata_c.r_st_bmask = 0;
    679   1.73    bouyer 		ata_c.r_st_pmask = WDCS_DRQ;
    680   1.36   thorpej 		ata_c.timeout = 10000; /* 10s */
    681    1.7    bouyer 	} else {
    682   1.47   thorpej 		ATADEBUG_PRINT(("ata_get_parms: no disks\n"),
    683   1.10    bouyer 		    DEBUG_FUNCS|DEBUG_PROBE);
    684   1.93     pooka 		rv = CMD_ERR;
    685   1.93     pooka 		goto out;
    686    1.2    bouyer 	}
    687   1.36   thorpej 	ata_c.flags = AT_READ | flags;
    688   1.36   thorpej 	ata_c.data = tb;
    689   1.36   thorpej 	ata_c.bcount = DEV_BSIZE;
    690   1.50   thorpej 	if ((*atac->atac_bustype_ata->ata_exec_command)(drvp,
    691   1.50   thorpej 						&ata_c) != ATACMD_COMPLETE) {
    692   1.47   thorpej 		ATADEBUG_PRINT(("ata_get_parms: wdc_exec_command failed\n"),
    693   1.10    bouyer 		    DEBUG_FUNCS|DEBUG_PROBE);
    694   1.93     pooka 		rv = CMD_AGAIN;
    695   1.93     pooka 		goto out;
    696   1.10    bouyer 	}
    697   1.36   thorpej 	if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    698   1.47   thorpej 		ATADEBUG_PRINT(("ata_get_parms: ata_c.flags=0x%x\n",
    699   1.36   thorpej 		    ata_c.flags), DEBUG_FUNCS|DEBUG_PROBE);
    700   1.93     pooka 		rv = CMD_ERR;
    701   1.93     pooka 		goto out;
    702   1.89       dsl 	}
    703   1.89       dsl 	/* if we didn't read any data something is wrong */
    704   1.93     pooka 	if ((ata_c.flags & AT_XFDONE) == 0) {
    705   1.93     pooka 		rv = CMD_ERR;
    706   1.93     pooka 		goto out;
    707   1.93     pooka 	}
    708   1.89       dsl 
    709   1.89       dsl 	/* Read in parameter block. */
    710   1.89       dsl 	memcpy(prms, tb, sizeof(struct ataparams));
    711   1.80     itohy 
    712   1.89       dsl 	/*
    713   1.89       dsl 	 * Shuffle string byte order.
    714   1.89       dsl 	 * ATAPI NEC, Mitsumi and Pioneer drives and
    715   1.89       dsl 	 * old ATA TDK CompactFlash cards
    716   1.89       dsl 	 * have different byte order.
    717   1.89       dsl 	 */
    718   1.80     itohy #if BYTE_ORDER == BIG_ENDIAN
    719   1.80     itohy # define M(n)	prms->atap_model[(n) ^ 1]
    720   1.80     itohy #else
    721   1.80     itohy # define M(n)	prms->atap_model[n]
    722   1.80     itohy #endif
    723   1.89       dsl 	if (
    724   1.80     itohy #if BYTE_ORDER == BIG_ENDIAN
    725   1.89       dsl 	    !
    726   1.80     itohy #endif
    727   1.89       dsl 	    ((drvp->drive_flags & DRIVE_ATAPI) ?
    728   1.89       dsl 	     ((M(0) == 'N' && M(1) == 'E') ||
    729   1.89       dsl 	      (M(0) == 'F' && M(1) == 'X') ||
    730   1.89       dsl 	      (M(0) == 'P' && M(1) == 'i')) :
    731   1.93     pooka 	     ((M(0) == 'T' && M(1) == 'D' && M(2) == 'K')))) {
    732   1.93     pooka 		rv = CMD_OK;
    733   1.93     pooka 		goto out;
    734   1.93     pooka 	     }
    735   1.80     itohy #undef M
    736   1.89       dsl 	for (i = 0; i < sizeof(prms->atap_model); i += 2) {
    737   1.89       dsl 		p = (u_int16_t *)(prms->atap_model + i);
    738   1.89       dsl 		*p = bswap16(*p);
    739   1.89       dsl 	}
    740   1.89       dsl 	for (i = 0; i < sizeof(prms->atap_serial); i += 2) {
    741   1.89       dsl 		p = (u_int16_t *)(prms->atap_serial + i);
    742   1.89       dsl 		*p = bswap16(*p);
    743   1.89       dsl 	}
    744   1.89       dsl 	for (i = 0; i < sizeof(prms->atap_revision); i += 2) {
    745   1.89       dsl 		p = (u_int16_t *)(prms->atap_revision + i);
    746   1.89       dsl 		*p = bswap16(*p);
    747   1.89       dsl 	}
    748   1.80     itohy 
    749   1.93     pooka 	rv = CMD_OK;
    750   1.93     pooka  out:
    751   1.93     pooka 	kmem_free(tb, DEV_BSIZE);
    752   1.93     pooka 	return rv;
    753    1.2    bouyer }
    754    1.2    bouyer 
    755    1.2    bouyer int
    756   1.21   thorpej ata_set_mode(struct ata_drive_datas *drvp, u_int8_t mode, u_int8_t flags)
    757    1.2    bouyer {
    758   1.36   thorpej 	struct ata_command ata_c;
    759   1.50   thorpej 	struct ata_channel *chp = drvp->chnl_softc;
    760   1.50   thorpej 	struct atac_softc *atac = chp->ch_atac;
    761    1.2    bouyer 
    762   1.47   thorpej 	ATADEBUG_PRINT(("ata_set_mode=0x%x\n", mode), DEBUG_FUNCS);
    763   1.36   thorpej 	memset(&ata_c, 0, sizeof(struct ata_command));
    764    1.2    bouyer 
    765   1.36   thorpej 	ata_c.r_command = SET_FEATURES;
    766   1.36   thorpej 	ata_c.r_st_bmask = 0;
    767   1.36   thorpej 	ata_c.r_st_pmask = 0;
    768   1.36   thorpej 	ata_c.r_features = WDSF_SET_MODE;
    769   1.36   thorpej 	ata_c.r_count = mode;
    770   1.36   thorpej 	ata_c.flags = flags;
    771   1.36   thorpej 	ata_c.timeout = 1000; /* 1s */
    772   1.50   thorpej 	if ((*atac->atac_bustype_ata->ata_exec_command)(drvp,
    773   1.50   thorpej 						&ata_c) != ATACMD_COMPLETE)
    774    1.2    bouyer 		return CMD_AGAIN;
    775   1.36   thorpej 	if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    776    1.2    bouyer 		return CMD_ERR;
    777    1.2    bouyer 	}
    778    1.2    bouyer 	return CMD_OK;
    779   1.11    bouyer }
    780   1.11    bouyer 
    781   1.78     itohy #if NATA_DMA
    782   1.11    bouyer void
    783   1.21   thorpej ata_dmaerr(struct ata_drive_datas *drvp, int flags)
    784   1.11    bouyer {
    785   1.11    bouyer 	/*
    786   1.11    bouyer 	 * Downgrade decision: if we get NERRS_MAX in NXFER.
    787   1.11    bouyer 	 * We start with n_dmaerrs set to NERRS_MAX-1 so that the
    788   1.11    bouyer 	 * first error within the first NXFER ops will immediatly trigger
    789   1.11    bouyer 	 * a downgrade.
    790   1.11    bouyer 	 * If we got an error and n_xfers is bigger than NXFER reset counters.
    791   1.11    bouyer 	 */
    792   1.11    bouyer 	drvp->n_dmaerrs++;
    793   1.11    bouyer 	if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) {
    794   1.39   thorpej 		ata_downgrade_mode(drvp, flags);
    795   1.11    bouyer 		drvp->n_dmaerrs = NERRS_MAX-1;
    796   1.11    bouyer 		drvp->n_xfers = 0;
    797   1.11    bouyer 		return;
    798   1.11    bouyer 	}
    799   1.11    bouyer 	if (drvp->n_xfers > NXFER) {
    800   1.11    bouyer 		drvp->n_dmaerrs = 1; /* just got an error */
    801   1.11    bouyer 		drvp->n_xfers = 1; /* restart counting from this error */
    802    1.4    bouyer 	}
    803    1.2    bouyer }
    804   1.78     itohy #endif	/* NATA_DMA */
    805   1.30    bouyer 
    806   1.44   thorpej /*
    807   1.68    bouyer  * freeze the queue and wait for the controller to be idle. Caller has to
    808   1.68    bouyer  * unfreeze/restart the queue
    809   1.68    bouyer  */
    810   1.68    bouyer void
    811   1.68    bouyer ata_queue_idle(struct ata_queue *queue)
    812   1.68    bouyer {
    813   1.68    bouyer 	int s = splbio();
    814   1.68    bouyer 	queue->queue_freeze++;
    815   1.68    bouyer 	while (queue->active_xfer != NULL) {
    816   1.68    bouyer 		queue->queue_flags |= QF_IDLE_WAIT;
    817   1.68    bouyer 		tsleep(&queue->queue_flags, PRIBIO, "qidl", 0);
    818   1.68    bouyer 	}
    819   1.68    bouyer 	splx(s);
    820   1.68    bouyer }
    821   1.68    bouyer 
    822   1.68    bouyer /*
    823   1.57   thorpej  * Add a command to the queue and start controller.
    824   1.57   thorpej  *
    825   1.57   thorpej  * MUST BE CALLED AT splbio()!
    826   1.44   thorpej  */
    827   1.44   thorpej void
    828   1.48   thorpej ata_exec_xfer(struct ata_channel *chp, struct ata_xfer *xfer)
    829   1.44   thorpej {
    830   1.44   thorpej 
    831   1.47   thorpej 	ATADEBUG_PRINT(("ata_exec_xfer %p channel %d drive %d\n", xfer,
    832   1.44   thorpej 	    chp->ch_channel, xfer->c_drive), DEBUG_XFERS);
    833   1.44   thorpej 
    834   1.44   thorpej 	/* complete xfer setup */
    835   1.44   thorpej 	xfer->c_chp = chp;
    836   1.44   thorpej 
    837   1.44   thorpej 	/* insert at the end of command list */
    838   1.44   thorpej 	TAILQ_INSERT_TAIL(&chp->ch_queue->queue_xfer, xfer, c_xferchain);
    839   1.47   thorpej 	ATADEBUG_PRINT(("atastart from ata_exec_xfer, flags 0x%x\n",
    840   1.44   thorpej 	    chp->ch_flags), DEBUG_XFERS);
    841   1.62    bouyer 	/*
    842   1.62    bouyer 	 * if polling and can sleep, wait for the xfer to be at head of queue
    843   1.62    bouyer 	 */
    844   1.62    bouyer 	if ((xfer->c_flags & (C_POLL | C_WAIT)) ==  (C_POLL | C_WAIT)) {
    845   1.62    bouyer 		while (chp->ch_queue->active_xfer != NULL ||
    846   1.62    bouyer 		    TAILQ_FIRST(&chp->ch_queue->queue_xfer) != xfer) {
    847   1.62    bouyer 			xfer->c_flags |= C_WAITACT;
    848   1.62    bouyer 			tsleep(xfer, PRIBIO, "ataact", 0);
    849   1.62    bouyer 			xfer->c_flags &= ~C_WAITACT;
    850   1.62    bouyer 			if (xfer->c_flags & C_FREE) {
    851   1.62    bouyer 				ata_free_xfer(chp, xfer);
    852   1.62    bouyer 				return;
    853   1.62    bouyer 			}
    854   1.62    bouyer 		}
    855   1.62    bouyer 	}
    856   1.45   thorpej 	atastart(chp);
    857   1.45   thorpej }
    858   1.45   thorpej 
    859   1.45   thorpej /*
    860   1.45   thorpej  * Start I/O on a controller, for the given channel.
    861   1.45   thorpej  * The first xfer may be not for our channel if the channel queues
    862   1.45   thorpej  * are shared.
    863   1.57   thorpej  *
    864   1.57   thorpej  * MUST BE CALLED AT splbio()!
    865   1.45   thorpej  */
    866   1.45   thorpej void
    867   1.48   thorpej atastart(struct ata_channel *chp)
    868   1.45   thorpej {
    869   1.49   thorpej 	struct atac_softc *atac = chp->ch_atac;
    870   1.45   thorpej 	struct ata_xfer *xfer;
    871   1.45   thorpej 
    872   1.56   thorpej #ifdef ATA_DEBUG
    873   1.45   thorpej 	int spl1, spl2;
    874   1.45   thorpej 
    875   1.45   thorpej 	spl1 = splbio();
    876   1.45   thorpej 	spl2 = splbio();
    877   1.45   thorpej 	if (spl2 != spl1) {
    878   1.45   thorpej 		printf("atastart: not at splbio()\n");
    879   1.45   thorpej 		panic("atastart");
    880   1.45   thorpej 	}
    881   1.45   thorpej 	splx(spl2);
    882   1.45   thorpej 	splx(spl1);
    883   1.56   thorpej #endif /* ATA_DEBUG */
    884   1.45   thorpej 
    885   1.45   thorpej 	/* is there a xfer ? */
    886   1.45   thorpej 	if ((xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer)) == NULL)
    887   1.45   thorpej 		return;
    888   1.45   thorpej 
    889   1.45   thorpej 	/* adjust chp, in case we have a shared queue */
    890   1.45   thorpej 	chp = xfer->c_chp;
    891   1.45   thorpej 
    892   1.45   thorpej 	if (chp->ch_queue->active_xfer != NULL) {
    893   1.45   thorpej 		return; /* channel aleady active */
    894   1.45   thorpej 	}
    895   1.45   thorpej 	if (__predict_false(chp->ch_queue->queue_freeze > 0)) {
    896   1.68    bouyer 		if (chp->ch_queue->queue_flags & QF_IDLE_WAIT) {
    897   1.68    bouyer 			chp->ch_queue->queue_flags &= ~QF_IDLE_WAIT;
    898   1.68    bouyer 			wakeup(&chp->ch_queue->queue_flags);
    899   1.68    bouyer 		}
    900   1.68    bouyer 		return; /* queue frozen */
    901   1.45   thorpej 	}
    902   1.62    bouyer 	/*
    903   1.62    bouyer 	 * if someone is waiting for the command to be active, wake it up
    904   1.62    bouyer 	 * and let it process the command
    905   1.62    bouyer 	 */
    906   1.62    bouyer 	if (xfer->c_flags & C_WAITACT) {
    907   1.62    bouyer 		ATADEBUG_PRINT(("atastart: xfer %p channel %d drive %d "
    908   1.62    bouyer 		    "wait active\n", xfer, chp->ch_channel, xfer->c_drive),
    909   1.62    bouyer 		    DEBUG_XFERS);
    910   1.62    bouyer 		wakeup(xfer);
    911   1.62    bouyer 		return;
    912   1.62    bouyer 	}
    913   1.45   thorpej #ifdef DIAGNOSTIC
    914   1.48   thorpej 	if ((chp->ch_flags & ATACH_IRQ_WAIT) != 0)
    915   1.45   thorpej 		panic("atastart: channel waiting for irq");
    916   1.45   thorpej #endif
    917   1.49   thorpej 	if (atac->atac_claim_hw)
    918   1.49   thorpej 		if (!(*atac->atac_claim_hw)(chp, 0))
    919   1.45   thorpej 			return;
    920   1.45   thorpej 
    921   1.47   thorpej 	ATADEBUG_PRINT(("atastart: xfer %p channel %d drive %d\n", xfer,
    922   1.45   thorpej 	    chp->ch_channel, xfer->c_drive), DEBUG_XFERS);
    923   1.45   thorpej 	if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_RESET) {
    924   1.45   thorpej 		chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_RESET;
    925   1.45   thorpej 		chp->ch_drive[xfer->c_drive].state = 0;
    926   1.45   thorpej 	}
    927   1.45   thorpej 	chp->ch_queue->active_xfer = xfer;
    928   1.45   thorpej 	TAILQ_REMOVE(&chp->ch_queue->queue_xfer, xfer, c_xferchain);
    929   1.65     perry 
    930   1.49   thorpej 	if (atac->atac_cap & ATAC_CAP_NOIRQ)
    931   1.45   thorpej 		KASSERT(xfer->c_flags & C_POLL);
    932   1.62    bouyer 
    933   1.45   thorpej 	xfer->c_start(chp, xfer);
    934   1.44   thorpej }
    935   1.44   thorpej 
    936   1.41   thorpej struct ata_xfer *
    937   1.41   thorpej ata_get_xfer(int flags)
    938   1.41   thorpej {
    939   1.41   thorpej 	struct ata_xfer *xfer;
    940   1.41   thorpej 	int s;
    941   1.41   thorpej 
    942   1.41   thorpej 	s = splbio();
    943   1.41   thorpej 	xfer = pool_get(&ata_xfer_pool,
    944   1.41   thorpej 	    ((flags & ATAXF_NOSLEEP) != 0 ? PR_NOWAIT : PR_WAITOK));
    945   1.41   thorpej 	splx(s);
    946   1.41   thorpej 	if (xfer != NULL) {
    947   1.41   thorpej 		memset(xfer, 0, sizeof(struct ata_xfer));
    948   1.41   thorpej 	}
    949   1.41   thorpej 	return xfer;
    950   1.41   thorpej }
    951   1.41   thorpej 
    952   1.41   thorpej void
    953   1.48   thorpej ata_free_xfer(struct ata_channel *chp, struct ata_xfer *xfer)
    954   1.41   thorpej {
    955   1.49   thorpej 	struct atac_softc *atac = chp->ch_atac;
    956   1.41   thorpej 	int s;
    957   1.41   thorpej 
    958   1.62    bouyer 	if (xfer->c_flags & C_WAITACT) {
    959   1.62    bouyer 		/* Someone is waiting for this xfer, so we can't free now */
    960   1.62    bouyer 		xfer->c_flags |= C_FREE;
    961   1.62    bouyer 		wakeup(xfer);
    962   1.62    bouyer 		return;
    963   1.62    bouyer 	}
    964   1.62    bouyer 
    965   1.76     itohy #if NATA_PIOBM		/* XXX wdc dependent code */
    966   1.76     itohy 	if (xfer->c_flags & C_PIOBM) {
    967   1.76     itohy 		struct wdc_softc *wdc = CHAN_TO_WDC(chp);
    968   1.76     itohy 
    969   1.76     itohy 		/* finish the busmastering PIO */
    970   1.76     itohy 		(*wdc->piobm_done)(wdc->dma_arg,
    971   1.76     itohy 		    chp->ch_channel, xfer->c_drive);
    972   1.76     itohy 		chp->ch_flags &= ~(ATACH_DMA_WAIT | ATACH_PIOBM_WAIT | ATACH_IRQ_WAIT);
    973   1.76     itohy 	}
    974   1.76     itohy #endif
    975   1.76     itohy 
    976   1.49   thorpej 	if (atac->atac_free_hw)
    977   1.49   thorpej 		(*atac->atac_free_hw)(chp);
    978   1.41   thorpej 	s = splbio();
    979   1.41   thorpej 	pool_put(&ata_xfer_pool, xfer);
    980   1.41   thorpej 	splx(s);
    981   1.41   thorpej }
    982   1.41   thorpej 
    983   1.42   thorpej /*
    984   1.48   thorpej  * Kill off all pending xfers for a ata_channel.
    985   1.42   thorpej  *
    986   1.42   thorpej  * Must be called at splbio().
    987   1.42   thorpej  */
    988   1.42   thorpej void
    989   1.42   thorpej ata_kill_pending(struct ata_drive_datas *drvp)
    990   1.42   thorpej {
    991   1.48   thorpej 	struct ata_channel *chp = drvp->chnl_softc;
    992   1.42   thorpej 	struct ata_xfer *xfer, *next_xfer;
    993   1.42   thorpej 	int s = splbio();
    994   1.42   thorpej 
    995   1.42   thorpej 	for (xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer);
    996   1.42   thorpej 	    xfer != NULL; xfer = next_xfer) {
    997   1.42   thorpej 		next_xfer = TAILQ_NEXT(xfer, c_xferchain);
    998   1.42   thorpej 		if (xfer->c_chp != chp || xfer->c_drive != drvp->drive)
    999   1.42   thorpej 			continue;
   1000   1.42   thorpej 		TAILQ_REMOVE(&chp->ch_queue->queue_xfer, xfer, c_xferchain);
   1001   1.42   thorpej 		(*xfer->c_kill_xfer)(chp, xfer, KILL_GONE);
   1002   1.42   thorpej 	}
   1003   1.42   thorpej 
   1004   1.42   thorpej 	while ((xfer = chp->ch_queue->active_xfer) != NULL) {
   1005   1.42   thorpej 		if (xfer->c_chp == chp && xfer->c_drive == drvp->drive) {
   1006   1.42   thorpej 			drvp->drive_flags |= DRIVE_WAITDRAIN;
   1007   1.42   thorpej 			(void) tsleep(&chp->ch_queue->active_xfer,
   1008   1.42   thorpej 			    PRIBIO, "atdrn", 0);
   1009   1.42   thorpej 		} else {
   1010   1.42   thorpej 			/* no more xfer for us */
   1011   1.42   thorpej 			break;
   1012   1.42   thorpej 		}
   1013   1.42   thorpej 	}
   1014   1.42   thorpej 	splx(s);
   1015   1.42   thorpej }
   1016   1.42   thorpej 
   1017   1.55   thorpej /*
   1018   1.55   thorpej  * ata_reset_channel:
   1019   1.55   thorpej  *
   1020   1.55   thorpej  *	Reset and ATA channel.
   1021   1.57   thorpej  *
   1022   1.57   thorpej  *	MUST BE CALLED AT splbio()!
   1023   1.55   thorpej  */
   1024   1.55   thorpej void
   1025   1.55   thorpej ata_reset_channel(struct ata_channel *chp, int flags)
   1026   1.55   thorpej {
   1027   1.55   thorpej 	struct atac_softc *atac = chp->ch_atac;
   1028   1.55   thorpej 	int drive;
   1029   1.55   thorpej 
   1030   1.57   thorpej #ifdef ATA_DEBUG
   1031   1.57   thorpej 	int spl1, spl2;
   1032   1.57   thorpej 
   1033   1.57   thorpej 	spl1 = splbio();
   1034   1.57   thorpej 	spl2 = splbio();
   1035   1.57   thorpej 	if (spl2 != spl1) {
   1036   1.57   thorpej 		printf("ata_reset_channel: not at splbio()\n");
   1037   1.57   thorpej 		panic("ata_reset_channel");
   1038   1.57   thorpej 	}
   1039   1.57   thorpej 	splx(spl2);
   1040   1.57   thorpej 	splx(spl1);
   1041   1.57   thorpej #endif /* ATA_DEBUG */
   1042   1.57   thorpej 
   1043   1.55   thorpej 	chp->ch_queue->queue_freeze++;
   1044   1.55   thorpej 
   1045   1.55   thorpej 	/*
   1046   1.55   thorpej 	 * If we can poll or wait it's OK, otherwise wake up the
   1047   1.55   thorpej 	 * kernel thread to do it for us.
   1048   1.55   thorpej 	 */
   1049  1.100    bouyer 	ATADEBUG_PRINT(("ata_reset_channel flags 0x%x ch_flags 0x%x\n",
   1050  1.100    bouyer 	    flags, chp->ch_flags), DEBUG_FUNCS | DEBUG_XFERS);
   1051   1.55   thorpej 	if ((flags & (AT_POLL | AT_WAIT)) == 0) {
   1052   1.55   thorpej 		if (chp->ch_flags & ATACH_TH_RESET) {
   1053   1.55   thorpej 			/* No need to schedule a reset more than one time. */
   1054   1.60    bouyer 			chp->ch_queue->queue_freeze--;
   1055   1.55   thorpej 			return;
   1056   1.55   thorpej 		}
   1057   1.55   thorpej 		chp->ch_flags |= ATACH_TH_RESET;
   1058   1.55   thorpej 		chp->ch_reset_flags = flags & (AT_RST_EMERG | AT_RST_NOCMD);
   1059   1.55   thorpej 		wakeup(&chp->ch_thread);
   1060   1.55   thorpej 		return;
   1061   1.55   thorpej 	}
   1062   1.55   thorpej 
   1063   1.55   thorpej 	(*atac->atac_bustype_ata->ata_reset_channel)(chp, flags);
   1064   1.55   thorpej 
   1065   1.55   thorpej 	for (drive = 0; drive < chp->ch_ndrive; drive++)
   1066   1.55   thorpej 		chp->ch_drive[drive].state = 0;
   1067   1.55   thorpej 
   1068   1.55   thorpej 	chp->ch_flags &= ~ATACH_TH_RESET;
   1069   1.55   thorpej 	if ((flags & AT_RST_EMERG) == 0)  {
   1070   1.55   thorpej 		chp->ch_queue->queue_freeze--;
   1071   1.55   thorpej 		atastart(chp);
   1072   1.55   thorpej 	} else {
   1073   1.55   thorpej 		/* make sure that we can use polled commands */
   1074   1.55   thorpej 		TAILQ_INIT(&chp->ch_queue->queue_xfer);
   1075   1.55   thorpej 		chp->ch_queue->queue_freeze = 0;
   1076   1.55   thorpej 		chp->ch_queue->active_xfer = NULL;
   1077   1.55   thorpej 	}
   1078   1.55   thorpej }
   1079   1.55   thorpej 
   1080   1.43   thorpej int
   1081   1.48   thorpej ata_addref(struct ata_channel *chp)
   1082   1.43   thorpej {
   1083   1.65     perry 	struct atac_softc *atac = chp->ch_atac;
   1084   1.49   thorpej 	struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
   1085   1.43   thorpej 	int s, error = 0;
   1086   1.43   thorpej 
   1087   1.43   thorpej 	s = splbio();
   1088   1.43   thorpej 	if (adapt->adapt_refcnt++ == 0 &&
   1089   1.43   thorpej 	    adapt->adapt_enable != NULL) {
   1090   1.98      cube 		error = (*adapt->adapt_enable)(atac->atac_dev, 1);
   1091   1.43   thorpej 		if (error)
   1092   1.43   thorpej 			adapt->adapt_refcnt--;
   1093   1.43   thorpej 	}
   1094   1.43   thorpej 	splx(s);
   1095   1.43   thorpej 	return (error);
   1096   1.43   thorpej }
   1097   1.43   thorpej 
   1098   1.43   thorpej void
   1099   1.48   thorpej ata_delref(struct ata_channel *chp)
   1100   1.43   thorpej {
   1101   1.49   thorpej 	struct atac_softc *atac = chp->ch_atac;
   1102   1.49   thorpej 	struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
   1103   1.43   thorpej 	int s;
   1104   1.43   thorpej 
   1105   1.43   thorpej 	s = splbio();
   1106   1.43   thorpej 	if (adapt->adapt_refcnt-- == 1 &&
   1107   1.43   thorpej 	    adapt->adapt_enable != NULL)
   1108   1.98      cube 		(void) (*adapt->adapt_enable)(atac->atac_dev, 0);
   1109   1.43   thorpej 	splx(s);
   1110   1.43   thorpej }
   1111   1.43   thorpej 
   1112   1.38   thorpej void
   1113   1.48   thorpej ata_print_modes(struct ata_channel *chp)
   1114   1.38   thorpej {
   1115   1.49   thorpej 	struct atac_softc *atac = chp->ch_atac;
   1116   1.38   thorpej 	int drive;
   1117   1.38   thorpej 	struct ata_drive_datas *drvp;
   1118   1.38   thorpej 
   1119   1.67      matt 	for (drive = 0; drive < chp->ch_ndrive; drive++) {
   1120   1.38   thorpej 		drvp = &chp->ch_drive[drive];
   1121   1.67      matt 		if ((drvp->drive_flags & DRIVE) == 0 || drvp->drv_softc == NULL)
   1122   1.38   thorpej 			continue;
   1123   1.85        ad 		aprint_verbose("%s(%s:%d:%d): using PIO mode %d",
   1124   1.95    dyoung 			device_xname(drvp->drv_softc),
   1125   1.98      cube 			device_xname(atac->atac_dev),
   1126   1.67      matt 			chp->ch_channel, drvp->drive, drvp->PIO_mode);
   1127   1.78     itohy #if NATA_DMA
   1128   1.38   thorpej 		if (drvp->drive_flags & DRIVE_DMA)
   1129   1.85        ad 			aprint_verbose(", DMA mode %d", drvp->DMA_mode);
   1130   1.78     itohy #if NATA_UDMA
   1131   1.38   thorpej 		if (drvp->drive_flags & DRIVE_UDMA) {
   1132   1.85        ad 			aprint_verbose(", Ultra-DMA mode %d", drvp->UDMA_mode);
   1133   1.38   thorpej 			if (drvp->UDMA_mode == 2)
   1134   1.85        ad 				aprint_verbose(" (Ultra/33)");
   1135   1.38   thorpej 			else if (drvp->UDMA_mode == 4)
   1136   1.85        ad 				aprint_verbose(" (Ultra/66)");
   1137   1.38   thorpej 			else if (drvp->UDMA_mode == 5)
   1138   1.85        ad 				aprint_verbose(" (Ultra/100)");
   1139   1.38   thorpej 			else if (drvp->UDMA_mode == 6)
   1140   1.85        ad 				aprint_verbose(" (Ultra/133)");
   1141   1.38   thorpej 		}
   1142   1.78     itohy #endif	/* NATA_UDMA */
   1143   1.78     itohy #endif	/* NATA_DMA */
   1144   1.78     itohy #if NATA_DMA || NATA_PIOBM
   1145   1.78     itohy 		if (0
   1146   1.78     itohy #if NATA_DMA
   1147   1.78     itohy 		    || (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA))
   1148   1.78     itohy #endif
   1149   1.76     itohy #if NATA_PIOBM
   1150   1.76     itohy 		    /* PIOBM capable controllers use DMA for PIO commands */
   1151   1.76     itohy 		    || (atac->atac_cap & ATAC_CAP_PIOBM)
   1152   1.76     itohy #endif
   1153   1.76     itohy 		    )
   1154   1.85        ad 			aprint_verbose(" (using DMA)");
   1155   1.78     itohy #endif	/* NATA_DMA || NATA_PIOBM */
   1156   1.85        ad 		aprint_verbose("\n");
   1157   1.38   thorpej 	}
   1158   1.38   thorpej }
   1159   1.38   thorpej 
   1160   1.78     itohy #if NATA_DMA
   1161   1.39   thorpej /*
   1162   1.39   thorpej  * downgrade the transfer mode of a drive after an error. return 1 if
   1163   1.39   thorpej  * downgrade was possible, 0 otherwise.
   1164   1.57   thorpej  *
   1165   1.57   thorpej  * MUST BE CALLED AT splbio()!
   1166   1.39   thorpej  */
   1167   1.39   thorpej int
   1168   1.39   thorpej ata_downgrade_mode(struct ata_drive_datas *drvp, int flags)
   1169   1.39   thorpej {
   1170   1.48   thorpej 	struct ata_channel *chp = drvp->chnl_softc;
   1171   1.49   thorpej 	struct atac_softc *atac = chp->ch_atac;
   1172   1.95    dyoung 	device_t drv_dev = drvp->drv_softc;
   1173   1.74   thorpej 	int cf_flags = device_cfdata(drv_dev)->cf_flags;
   1174   1.39   thorpej 
   1175   1.39   thorpej 	/* if drive or controller don't know its mode, we can't do much */
   1176   1.39   thorpej 	if ((drvp->drive_flags & DRIVE_MODE) == 0 ||
   1177   1.49   thorpej 	    (atac->atac_set_modes == NULL))
   1178   1.39   thorpej 		return 0;
   1179   1.39   thorpej 	/* current drive mode was set by a config flag, let it this way */
   1180   1.39   thorpej 	if ((cf_flags & ATA_CONFIG_PIO_SET) ||
   1181   1.39   thorpej 	    (cf_flags & ATA_CONFIG_DMA_SET) ||
   1182   1.39   thorpej 	    (cf_flags & ATA_CONFIG_UDMA_SET))
   1183   1.39   thorpej 		return 0;
   1184   1.39   thorpej 
   1185   1.78     itohy #if NATA_UDMA
   1186   1.39   thorpej 	/*
   1187   1.39   thorpej 	 * If we were using Ultra-DMA mode, downgrade to the next lower mode.
   1188   1.39   thorpej 	 */
   1189   1.39   thorpej 	if ((drvp->drive_flags & DRIVE_UDMA) && drvp->UDMA_mode >= 2) {
   1190   1.39   thorpej 		drvp->UDMA_mode--;
   1191   1.98      cube 		aprint_error_dev(drv_dev,
   1192   1.98      cube 		    "transfer error, downgrading to Ultra-DMA mode %d\n",
   1193   1.98      cube 		    drvp->UDMA_mode);
   1194   1.39   thorpej 	}
   1195   1.78     itohy #endif
   1196   1.39   thorpej 
   1197   1.39   thorpej 	/*
   1198   1.39   thorpej 	 * If we were using ultra-DMA, don't downgrade to multiword DMA.
   1199   1.39   thorpej 	 */
   1200   1.39   thorpej 	else if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) {
   1201   1.39   thorpej 		drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
   1202   1.39   thorpej 		drvp->PIO_mode = drvp->PIO_cap;
   1203   1.98      cube 		aprint_error_dev(drv_dev,
   1204   1.98      cube 		    "transfer error, downgrading to PIO mode %d\n",
   1205   1.98      cube 		    drvp->PIO_mode);
   1206   1.39   thorpej 	} else /* already using PIO, can't downgrade */
   1207   1.39   thorpej 		return 0;
   1208   1.39   thorpej 
   1209   1.49   thorpej 	(*atac->atac_set_modes)(chp);
   1210   1.39   thorpej 	ata_print_modes(chp);
   1211   1.84       wiz 	/* reset the channel, which will schedule all drives for setup */
   1212   1.55   thorpej 	ata_reset_channel(chp, flags | AT_RST_NOCMD);
   1213   1.39   thorpej 	return 1;
   1214   1.39   thorpej }
   1215   1.78     itohy #endif	/* NATA_DMA */
   1216   1.39   thorpej 
   1217   1.40   thorpej /*
   1218   1.40   thorpej  * Probe drive's capabilities, for use by the controller later
   1219   1.65     perry  * Assumes drvp points to an existing drive.
   1220   1.40   thorpej  */
   1221   1.40   thorpej void
   1222   1.40   thorpej ata_probe_caps(struct ata_drive_datas *drvp)
   1223   1.40   thorpej {
   1224   1.40   thorpej 	struct ataparams params, params2;
   1225   1.48   thorpej 	struct ata_channel *chp = drvp->chnl_softc;
   1226   1.49   thorpej 	struct atac_softc *atac = chp->ch_atac;
   1227   1.95    dyoung 	device_t drv_dev = drvp->drv_softc;
   1228   1.58   thorpej 	int i, printed, s;
   1229   1.70  christos 	const char *sep = "";
   1230   1.40   thorpej 	int cf_flags;
   1231   1.40   thorpej 
   1232   1.40   thorpej 	if (ata_get_params(drvp, AT_WAIT, &params) != CMD_OK) {
   1233   1.40   thorpej 		/* IDENTIFY failed. Can't tell more about the device */
   1234   1.40   thorpej 		return;
   1235   1.40   thorpej 	}
   1236   1.49   thorpej 	if ((atac->atac_cap & (ATAC_CAP_DATA16 | ATAC_CAP_DATA32)) ==
   1237   1.49   thorpej 	    (ATAC_CAP_DATA16 | ATAC_CAP_DATA32)) {
   1238   1.40   thorpej 		/*
   1239   1.40   thorpej 		 * Controller claims 16 and 32 bit transfers.
   1240   1.40   thorpej 		 * Re-do an IDENTIFY with 32-bit transfers,
   1241   1.40   thorpej 		 * and compare results.
   1242   1.40   thorpej 		 */
   1243   1.58   thorpej 		s = splbio();
   1244   1.40   thorpej 		drvp->drive_flags |= DRIVE_CAP32;
   1245   1.58   thorpej 		splx(s);
   1246   1.40   thorpej 		ata_get_params(drvp, AT_WAIT, &params2);
   1247   1.40   thorpej 		if (memcmp(&params, &params2, sizeof(struct ataparams)) != 0) {
   1248   1.40   thorpej 			/* Not good. fall back to 16bits */
   1249   1.58   thorpej 			s = splbio();
   1250   1.40   thorpej 			drvp->drive_flags &= ~DRIVE_CAP32;
   1251   1.58   thorpej 			splx(s);
   1252   1.40   thorpej 		} else {
   1253   1.98      cube 			aprint_verbose_dev(drv_dev, "32-bit data port\n");
   1254   1.40   thorpej 		}
   1255   1.40   thorpej 	}
   1256   1.40   thorpej #if 0 /* Some ultra-DMA drives claims to only support ATA-3. sigh */
   1257   1.65     perry 	if (params.atap_ata_major > 0x01 &&
   1258   1.40   thorpej 	    params.atap_ata_major != 0xffff) {
   1259   1.40   thorpej 		for (i = 14; i > 0; i--) {
   1260   1.40   thorpej 			if (params.atap_ata_major & (1 << i)) {
   1261   1.98      cube 				aprint_verbose_dev(drv_dev,
   1262   1.98      cube 				    "ATA version %d\n", i);
   1263   1.40   thorpej 				drvp->ata_vers = i;
   1264   1.40   thorpej 				break;
   1265   1.40   thorpej 			}
   1266   1.40   thorpej 		}
   1267   1.40   thorpej 	}
   1268   1.40   thorpej #endif
   1269   1.40   thorpej 
   1270   1.40   thorpej 	/* An ATAPI device is at last PIO mode 3 */
   1271   1.40   thorpej 	if (drvp->drive_flags & DRIVE_ATAPI)
   1272   1.40   thorpej 		drvp->PIO_mode = 3;
   1273   1.40   thorpej 
   1274   1.40   thorpej 	/*
   1275   1.65     perry 	 * It's not in the specs, but it seems that some drive
   1276   1.40   thorpej 	 * returns 0xffff in atap_extensions when this field is invalid
   1277   1.40   thorpej 	 */
   1278   1.40   thorpej 	if (params.atap_extensions != 0xffff &&
   1279   1.40   thorpej 	    (params.atap_extensions & WDC_EXT_MODES)) {
   1280   1.40   thorpej 		printed = 0;
   1281   1.40   thorpej 		/*
   1282   1.40   thorpej 		 * XXX some drives report something wrong here (they claim to
   1283   1.40   thorpej 		 * support PIO mode 8 !). As mode is coded on 3 bits in
   1284   1.40   thorpej 		 * SET FEATURE, limit it to 7 (so limit i to 4).
   1285   1.40   thorpej 		 * If higher mode than 7 is found, abort.
   1286   1.40   thorpej 		 */
   1287   1.40   thorpej 		for (i = 7; i >= 0; i--) {
   1288   1.40   thorpej 			if ((params.atap_piomode_supp & (1 << i)) == 0)
   1289   1.40   thorpej 				continue;
   1290   1.40   thorpej 			if (i > 4)
   1291   1.40   thorpej 				return;
   1292   1.40   thorpej 			/*
   1293   1.40   thorpej 			 * See if mode is accepted.
   1294   1.40   thorpej 			 * If the controller can't set its PIO mode,
   1295   1.40   thorpej 			 * assume the defaults are good, so don't try
   1296   1.40   thorpej 			 * to set it
   1297   1.40   thorpej 			 */
   1298   1.49   thorpej 			if (atac->atac_set_modes)
   1299   1.40   thorpej 				/*
   1300   1.40   thorpej 				 * It's OK to pool here, it's fast enouth
   1301   1.40   thorpej 				 * to not bother waiting for interrupt
   1302   1.40   thorpej 				 */
   1303   1.40   thorpej 				if (ata_set_mode(drvp, 0x08 | (i + 3),
   1304   1.40   thorpej 				   AT_WAIT) != CMD_OK)
   1305   1.40   thorpej 					continue;
   1306   1.65     perry 			if (!printed) {
   1307   1.98      cube 				aprint_verbose_dev(drv_dev,
   1308   1.98      cube 				    "drive supports PIO mode %d", i + 3);
   1309   1.40   thorpej 				sep = ",";
   1310   1.40   thorpej 				printed = 1;
   1311   1.40   thorpej 			}
   1312   1.40   thorpej 			/*
   1313   1.40   thorpej 			 * If controller's driver can't set its PIO mode,
   1314   1.40   thorpej 			 * get the highter one for the drive.
   1315   1.40   thorpej 			 */
   1316   1.49   thorpej 			if (atac->atac_set_modes == NULL ||
   1317   1.49   thorpej 			    atac->atac_pio_cap >= i + 3) {
   1318   1.40   thorpej 				drvp->PIO_mode = i + 3;
   1319   1.40   thorpej 				drvp->PIO_cap = i + 3;
   1320   1.40   thorpej 				break;
   1321   1.40   thorpej 			}
   1322   1.40   thorpej 		}
   1323   1.40   thorpej 		if (!printed) {
   1324   1.65     perry 			/*
   1325   1.40   thorpej 			 * We didn't find a valid PIO mode.
   1326   1.40   thorpej 			 * Assume the values returned for DMA are buggy too
   1327   1.40   thorpej 			 */
   1328   1.40   thorpej 			return;
   1329   1.40   thorpej 		}
   1330   1.58   thorpej 		s = splbio();
   1331   1.40   thorpej 		drvp->drive_flags |= DRIVE_MODE;
   1332   1.58   thorpej 		splx(s);
   1333   1.40   thorpej 		printed = 0;
   1334   1.40   thorpej 		for (i = 7; i >= 0; i--) {
   1335   1.40   thorpej 			if ((params.atap_dmamode_supp & (1 << i)) == 0)
   1336   1.40   thorpej 				continue;
   1337   1.78     itohy #if NATA_DMA
   1338   1.49   thorpej 			if ((atac->atac_cap & ATAC_CAP_DMA) &&
   1339   1.49   thorpej 			    atac->atac_set_modes != NULL)
   1340   1.40   thorpej 				if (ata_set_mode(drvp, 0x20 | i, AT_WAIT)
   1341   1.40   thorpej 				    != CMD_OK)
   1342   1.40   thorpej 					continue;
   1343   1.78     itohy #endif
   1344   1.40   thorpej 			if (!printed) {
   1345   1.85        ad 				aprint_verbose("%s DMA mode %d", sep, i);
   1346   1.40   thorpej 				sep = ",";
   1347   1.40   thorpej 				printed = 1;
   1348   1.40   thorpej 			}
   1349   1.78     itohy #if NATA_DMA
   1350   1.49   thorpej 			if (atac->atac_cap & ATAC_CAP_DMA) {
   1351   1.49   thorpej 				if (atac->atac_set_modes != NULL &&
   1352   1.49   thorpej 				    atac->atac_dma_cap < i)
   1353   1.40   thorpej 					continue;
   1354   1.40   thorpej 				drvp->DMA_mode = i;
   1355   1.40   thorpej 				drvp->DMA_cap = i;
   1356   1.58   thorpej 				s = splbio();
   1357   1.40   thorpej 				drvp->drive_flags |= DRIVE_DMA;
   1358   1.58   thorpej 				splx(s);
   1359   1.40   thorpej 			}
   1360   1.78     itohy #endif
   1361   1.40   thorpej 			break;
   1362   1.40   thorpej 		}
   1363   1.40   thorpej 		if (params.atap_extensions & WDC_EXT_UDMA_MODES) {
   1364   1.40   thorpej 			printed = 0;
   1365   1.40   thorpej 			for (i = 7; i >= 0; i--) {
   1366   1.40   thorpej 				if ((params.atap_udmamode_supp & (1 << i))
   1367   1.40   thorpej 				    == 0)
   1368   1.40   thorpej 					continue;
   1369   1.78     itohy #if NATA_UDMA
   1370   1.49   thorpej 				if (atac->atac_set_modes != NULL &&
   1371   1.49   thorpej 				    (atac->atac_cap & ATAC_CAP_UDMA))
   1372   1.40   thorpej 					if (ata_set_mode(drvp, 0x40 | i,
   1373   1.40   thorpej 					    AT_WAIT) != CMD_OK)
   1374   1.40   thorpej 						continue;
   1375   1.78     itohy #endif
   1376   1.40   thorpej 				if (!printed) {
   1377   1.85        ad 					aprint_verbose("%s Ultra-DMA mode %d",
   1378   1.40   thorpej 					    sep, i);
   1379   1.40   thorpej 					if (i == 2)
   1380   1.85        ad 						aprint_verbose(" (Ultra/33)");
   1381   1.40   thorpej 					else if (i == 4)
   1382   1.85        ad 						aprint_verbose(" (Ultra/66)");
   1383   1.40   thorpej 					else if (i == 5)
   1384   1.85        ad 						aprint_verbose(" (Ultra/100)");
   1385   1.40   thorpej 					else if (i == 6)
   1386   1.85        ad 						aprint_verbose(" (Ultra/133)");
   1387   1.40   thorpej 					sep = ",";
   1388   1.40   thorpej 					printed = 1;
   1389   1.40   thorpej 				}
   1390   1.78     itohy #if NATA_UDMA
   1391   1.49   thorpej 				if (atac->atac_cap & ATAC_CAP_UDMA) {
   1392   1.49   thorpej 					if (atac->atac_set_modes != NULL &&
   1393   1.49   thorpej 					    atac->atac_udma_cap < i)
   1394   1.40   thorpej 						continue;
   1395   1.40   thorpej 					drvp->UDMA_mode = i;
   1396   1.40   thorpej 					drvp->UDMA_cap = i;
   1397   1.58   thorpej 					s = splbio();
   1398   1.40   thorpej 					drvp->drive_flags |= DRIVE_UDMA;
   1399   1.58   thorpej 					splx(s);
   1400   1.40   thorpej 				}
   1401   1.78     itohy #endif
   1402   1.40   thorpej 				break;
   1403   1.40   thorpej 			}
   1404   1.40   thorpej 		}
   1405   1.85        ad 		aprint_verbose("\n");
   1406   1.40   thorpej 	}
   1407   1.40   thorpej 
   1408   1.58   thorpej 	s = splbio();
   1409   1.40   thorpej 	drvp->drive_flags &= ~DRIVE_NOSTREAM;
   1410   1.40   thorpej 	if (drvp->drive_flags & DRIVE_ATAPI) {
   1411   1.65     perry 		if (atac->atac_cap & ATAC_CAP_ATAPI_NOSTREAM)
   1412   1.40   thorpej 			drvp->drive_flags |= DRIVE_NOSTREAM;
   1413   1.40   thorpej 	} else {
   1414   1.65     perry 		if (atac->atac_cap & ATAC_CAP_ATA_NOSTREAM)
   1415   1.40   thorpej 			drvp->drive_flags |= DRIVE_NOSTREAM;
   1416   1.40   thorpej 	}
   1417   1.58   thorpej 	splx(s);
   1418   1.40   thorpej 
   1419   1.40   thorpej 	/* Try to guess ATA version here, if it didn't get reported */
   1420   1.40   thorpej 	if (drvp->ata_vers == 0) {
   1421   1.78     itohy #if NATA_UDMA
   1422   1.40   thorpej 		if (drvp->drive_flags & DRIVE_UDMA)
   1423   1.40   thorpej 			drvp->ata_vers = 4; /* should be at last ATA-4 */
   1424   1.78     itohy 		else
   1425   1.78     itohy #endif
   1426   1.78     itohy 		if (drvp->PIO_cap > 2)
   1427   1.40   thorpej 			drvp->ata_vers = 2; /* should be at last ATA-2 */
   1428   1.40   thorpej 	}
   1429   1.74   thorpej 	cf_flags = device_cfdata(drv_dev)->cf_flags;
   1430   1.40   thorpej 	if (cf_flags & ATA_CONFIG_PIO_SET) {
   1431   1.58   thorpej 		s = splbio();
   1432   1.40   thorpej 		drvp->PIO_mode =
   1433   1.40   thorpej 		    (cf_flags & ATA_CONFIG_PIO_MODES) >> ATA_CONFIG_PIO_OFF;
   1434   1.40   thorpej 		drvp->drive_flags |= DRIVE_MODE;
   1435   1.58   thorpej 		splx(s);
   1436   1.40   thorpej 	}
   1437   1.78     itohy #if NATA_DMA
   1438   1.49   thorpej 	if ((atac->atac_cap & ATAC_CAP_DMA) == 0) {
   1439   1.40   thorpej 		/* don't care about DMA modes */
   1440   1.40   thorpej 		return;
   1441   1.40   thorpej 	}
   1442   1.40   thorpej 	if (cf_flags & ATA_CONFIG_DMA_SET) {
   1443   1.58   thorpej 		s = splbio();
   1444   1.40   thorpej 		if ((cf_flags & ATA_CONFIG_DMA_MODES) ==
   1445   1.40   thorpej 		    ATA_CONFIG_DMA_DISABLE) {
   1446   1.40   thorpej 			drvp->drive_flags &= ~DRIVE_DMA;
   1447   1.40   thorpej 		} else {
   1448   1.40   thorpej 			drvp->DMA_mode = (cf_flags & ATA_CONFIG_DMA_MODES) >>
   1449   1.40   thorpej 			    ATA_CONFIG_DMA_OFF;
   1450   1.40   thorpej 			drvp->drive_flags |= DRIVE_DMA | DRIVE_MODE;
   1451   1.40   thorpej 		}
   1452   1.58   thorpej 		splx(s);
   1453   1.40   thorpej 	}
   1454   1.78     itohy #if NATA_UDMA
   1455   1.49   thorpej 	if ((atac->atac_cap & ATAC_CAP_UDMA) == 0) {
   1456   1.40   thorpej 		/* don't care about UDMA modes */
   1457   1.40   thorpej 		return;
   1458   1.40   thorpej 	}
   1459   1.40   thorpej 	if (cf_flags & ATA_CONFIG_UDMA_SET) {
   1460   1.58   thorpej 		s = splbio();
   1461   1.40   thorpej 		if ((cf_flags & ATA_CONFIG_UDMA_MODES) ==
   1462   1.40   thorpej 		    ATA_CONFIG_UDMA_DISABLE) {
   1463   1.40   thorpej 			drvp->drive_flags &= ~DRIVE_UDMA;
   1464   1.40   thorpej 		} else {
   1465   1.40   thorpej 			drvp->UDMA_mode = (cf_flags & ATA_CONFIG_UDMA_MODES) >>
   1466   1.40   thorpej 			    ATA_CONFIG_UDMA_OFF;
   1467   1.40   thorpej 			drvp->drive_flags |= DRIVE_UDMA | DRIVE_MODE;
   1468   1.40   thorpej 		}
   1469   1.58   thorpej 		splx(s);
   1470   1.40   thorpej 	}
   1471   1.78     itohy #endif	/* NATA_UDMA */
   1472   1.78     itohy #endif	/* NATA_DMA */
   1473   1.40   thorpej }
   1474   1.40   thorpej 
   1475   1.30    bouyer /* management of the /dev/atabus* devices */
   1476   1.54   thorpej int
   1477   1.83  christos atabusopen(dev_t dev, int flag, int fmt,
   1478   1.83  christos     struct lwp *l)
   1479   1.30    bouyer {
   1480   1.81     itohy 	struct atabus_softc *sc;
   1481   1.99    cegger 	int error;
   1482   1.65     perry 
   1483   1.99    cegger 	sc = device_lookup_private(&atabus_cd, minor(dev));
   1484   1.99    cegger 	if (sc == NULL)
   1485   1.81     itohy 		return (ENXIO);
   1486   1.65     perry 
   1487   1.81     itohy 	if (sc->sc_flags & ATABUSCF_OPEN)
   1488   1.81     itohy 		return (EBUSY);
   1489   1.30    bouyer 
   1490   1.81     itohy 	if ((error = ata_addref(sc->sc_chan)) != 0)
   1491   1.81     itohy 		return (error);
   1492   1.30    bouyer 
   1493   1.81     itohy 	sc->sc_flags |= ATABUSCF_OPEN;
   1494   1.30    bouyer 
   1495   1.81     itohy 	return (0);
   1496   1.30    bouyer }
   1497   1.30    bouyer 
   1498   1.30    bouyer 
   1499   1.30    bouyer int
   1500   1.83  christos atabusclose(dev_t dev, int flag, int fmt,
   1501   1.83  christos     struct lwp *l)
   1502   1.30    bouyer {
   1503   1.98      cube 	struct atabus_softc *sc =
   1504   1.99    cegger 	    device_lookup_private(&atabus_cd, minor(dev));
   1505   1.30    bouyer 
   1506   1.81     itohy 	ata_delref(sc->sc_chan);
   1507   1.30    bouyer 
   1508   1.81     itohy 	sc->sc_flags &= ~ATABUSCF_OPEN;
   1509   1.30    bouyer 
   1510   1.81     itohy 	return (0);
   1511   1.30    bouyer }
   1512   1.30    bouyer 
   1513   1.30    bouyer int
   1514   1.86  christos atabusioctl(dev_t dev, u_long cmd, void *addr, int flag,
   1515   1.83  christos     struct lwp *l)
   1516   1.30    bouyer {
   1517   1.98      cube 	struct atabus_softc *sc =
   1518   1.99    cegger 	    device_lookup_private(&atabus_cd, minor(dev));
   1519   1.48   thorpej 	struct ata_channel *chp = sc->sc_chan;
   1520   1.32    bouyer 	int min_drive, max_drive, drive;
   1521   1.81     itohy 	int error;
   1522   1.30    bouyer 	int s;
   1523   1.30    bouyer 
   1524   1.81     itohy 	/*
   1525   1.81     itohy 	 * Enforce write permission for ioctls that change the
   1526   1.81     itohy 	 * state of the bus.  Host adapter specific ioctls must
   1527   1.81     itohy 	 * be checked by the adapter driver.
   1528   1.81     itohy 	 */
   1529   1.81     itohy 	switch (cmd) {
   1530   1.81     itohy 	case ATABUSIOSCAN:
   1531   1.81     itohy 	case ATABUSIODETACH:
   1532   1.81     itohy 	case ATABUSIORESET:
   1533   1.81     itohy 		if ((flag & FWRITE) == 0)
   1534   1.81     itohy 			return (EBADF);
   1535   1.81     itohy 	}
   1536   1.30    bouyer 
   1537   1.81     itohy 	switch (cmd) {
   1538   1.81     itohy 	case ATABUSIORESET:
   1539   1.30    bouyer 		s = splbio();
   1540   1.55   thorpej 		ata_reset_channel(sc->sc_chan, AT_WAIT | AT_POLL);
   1541   1.30    bouyer 		splx(s);
   1542   1.30    bouyer 		error = 0;
   1543   1.30    bouyer 		break;
   1544   1.32    bouyer 	case ATABUSIOSCAN:
   1545   1.32    bouyer 	{
   1546   1.32    bouyer #if 0
   1547   1.32    bouyer 		struct atabusioscan_args *a=
   1548   1.32    bouyer 		    (struct atabusioscan_args *)addr;
   1549   1.32    bouyer #endif
   1550   1.32    bouyer 		if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) ||
   1551   1.32    bouyer 		    (chp->ch_drive[1].drive_flags & DRIVE_OLD))
   1552   1.32    bouyer 			return (EOPNOTSUPP);
   1553   1.32    bouyer 		return (EOPNOTSUPP);
   1554   1.32    bouyer 	}
   1555   1.32    bouyer 	case ATABUSIODETACH:
   1556   1.32    bouyer 	{
   1557   1.32    bouyer 		struct atabusioscan_args *a=
   1558   1.32    bouyer 		    (struct atabusioscan_args *)addr;
   1559   1.32    bouyer 		if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) ||
   1560   1.32    bouyer 		    (chp->ch_drive[1].drive_flags & DRIVE_OLD))
   1561   1.32    bouyer 			return (EOPNOTSUPP);
   1562   1.32    bouyer 		switch (a->at_dev) {
   1563   1.32    bouyer 		case -1:
   1564   1.32    bouyer 			min_drive = 0;
   1565   1.32    bouyer 			max_drive = 1;
   1566   1.32    bouyer 			break;
   1567   1.32    bouyer 		case 0:
   1568   1.32    bouyer 		case 1:
   1569   1.32    bouyer 			min_drive = max_drive = a->at_dev;
   1570   1.32    bouyer 			break;
   1571   1.32    bouyer 		default:
   1572   1.32    bouyer 			return (EINVAL);
   1573   1.32    bouyer 		}
   1574   1.32    bouyer 		for (drive = min_drive; drive <= max_drive; drive++) {
   1575   1.32    bouyer 			if (chp->ch_drive[drive].drv_softc != NULL) {
   1576   1.32    bouyer 				error = config_detach(
   1577   1.32    bouyer 				    chp->ch_drive[drive].drv_softc, 0);
   1578   1.32    bouyer 				if (error)
   1579   1.32    bouyer 					return (error);
   1580   1.95    dyoung 				KASSERT(chp->ch_drive[drive].drv_softc == NULL);
   1581   1.32    bouyer 			}
   1582   1.32    bouyer 		}
   1583   1.32    bouyer 		error = 0;
   1584   1.32    bouyer 		break;
   1585   1.32    bouyer 	}
   1586   1.30    bouyer 	default:
   1587   1.30    bouyer 		error = ENOTTY;
   1588   1.30    bouyer 	}
   1589   1.30    bouyer 	return (error);
   1590   1.30    bouyer };
   1591   1.64  jmcneill 
   1592   1.92  jmcneill static bool
   1593   1.97    dyoung atabus_suspend(device_t dv PMF_FN_ARGS)
   1594   1.92  jmcneill {
   1595   1.92  jmcneill 	struct atabus_softc *sc = device_private(dv);
   1596   1.92  jmcneill 	struct ata_channel *chp = sc->sc_chan;
   1597   1.92  jmcneill 
   1598   1.92  jmcneill 	ata_queue_idle(chp->ch_queue);
   1599   1.92  jmcneill 
   1600   1.92  jmcneill 	return true;
   1601   1.92  jmcneill }
   1602   1.92  jmcneill 
   1603   1.92  jmcneill static bool
   1604   1.97    dyoung atabus_resume(device_t dv PMF_FN_ARGS)
   1605   1.64  jmcneill {
   1606   1.92  jmcneill 	struct atabus_softc *sc = device_private(dv);
   1607   1.64  jmcneill 	struct ata_channel *chp = sc->sc_chan;
   1608   1.64  jmcneill 	int s;
   1609   1.64  jmcneill 
   1610   1.92  jmcneill 	/*
   1611   1.92  jmcneill 	 * XXX joerg: with wdc, the first channel unfreezes the controler.
   1612   1.92  jmcneill 	 * Move this the reset and queue idling into wdc.
   1613   1.92  jmcneill 	 */
   1614   1.92  jmcneill 	s = splbio();
   1615   1.92  jmcneill 	if (chp->ch_queue->queue_freeze == 0) {
   1616   1.64  jmcneill 		splx(s);
   1617   1.92  jmcneill 		return true;
   1618   1.64  jmcneill 	}
   1619   1.92  jmcneill 	KASSERT(chp->ch_queue->queue_freeze > 0);
   1620   1.92  jmcneill 	/* unfreeze the queue and reset drives */
   1621   1.92  jmcneill 	chp->ch_queue->queue_freeze--;
   1622   1.92  jmcneill 	ata_reset_channel(chp, AT_WAIT);
   1623   1.92  jmcneill 	splx(s);
   1624   1.64  jmcneill 
   1625   1.92  jmcneill 	return true;
   1626   1.64  jmcneill }
   1627