Home | History | Annotate | Line # | Download | only in ata
ata.c revision 1.160
      1  1.160  riastrad /*	$NetBSD: ata.c,v 1.160 2020/10/03 22:32:50 riastradh 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  *
     15    1.2    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16    1.2    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17    1.2    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18   1.65     perry  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19    1.2    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20    1.2    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21    1.2    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22    1.2    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23    1.2    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24    1.2    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25    1.2    bouyer  */
     26   1.14     lukem 
     27   1.14     lukem #include <sys/cdefs.h>
     28  1.160  riastrad __KERNEL_RCSID(0, "$NetBSD: ata.c,v 1.160 2020/10/03 22:32:50 riastradh Exp $");
     29    1.2    bouyer 
     30   1.88    dyoung #include "opt_ata.h"
     31    1.2    bouyer 
     32    1.2    bouyer #include <sys/param.h>
     33    1.2    bouyer #include <sys/systm.h>
     34    1.2    bouyer #include <sys/kernel.h>
     35    1.2    bouyer #include <sys/device.h>
     36   1.30    bouyer #include <sys/conf.h>
     37   1.30    bouyer #include <sys/fcntl.h>
     38   1.23   thorpej #include <sys/proc.h>
     39   1.23   thorpej #include <sys/kthread.h>
     40   1.23   thorpej #include <sys/errno.h>
     41   1.30    bouyer #include <sys/ataio.h>
     42   1.93     pooka #include <sys/kmem.h>
     43   1.91        ad #include <sys/intr.h>
     44   1.91        ad #include <sys/bus.h>
     45  1.107     pooka #include <sys/once.h>
     46  1.133  jdolecek #include <sys/bitops.h>
     47  1.154  jdolecek #include <sys/cpu.h>
     48  1.133  jdolecek 
     49  1.133  jdolecek #define ATABUS_PRIVATE
     50   1.15    bouyer 
     51   1.76     itohy #include <dev/ata/ataconf.h>
     52    1.2    bouyer #include <dev/ata/atareg.h>
     53    1.2    bouyer #include <dev/ata/atavar.h>
     54   1.76     itohy #include <dev/ic/wdcvar.h>	/* for PIOBM */
     55    1.2    bouyer 
     56  1.141  riastrad #include "ioconf.h"
     57   1.23   thorpej #include "locators.h"
     58   1.23   thorpej 
     59   1.51   thorpej #include "atapibus.h"
     60   1.51   thorpej #include "ataraid.h"
     61  1.124    bouyer #include "sata_pmp.h"
     62   1.51   thorpej 
     63   1.65     perry #if NATARAID > 0
     64   1.65     perry #include <dev/ata/ata_raidvar.h>
     65   1.51   thorpej #endif
     66  1.124    bouyer #if NSATA_PMP > 0
     67  1.124    bouyer #include <dev/ata/satapmpvar.h>
     68  1.124    bouyer #endif
     69  1.124    bouyer #include <dev/ata/satapmpreg.h>
     70   1.51   thorpej 
     71    1.2    bouyer #define DEBUG_FUNCS  0x08
     72    1.2    bouyer #define DEBUG_PROBE  0x10
     73   1.23   thorpej #define DEBUG_DETACH 0x20
     74   1.44   thorpej #define	DEBUG_XFERS  0x40
     75   1.47   thorpej #ifdef ATADEBUG
     76  1.126       abs #ifndef ATADEBUG_MASK
     77  1.126       abs #define ATADEBUG_MASK 0
     78  1.126       abs #endif
     79  1.126       abs int atadebug_mask = ATADEBUG_MASK;
     80   1.47   thorpej #define ATADEBUG_PRINT(args, level) \
     81   1.81     itohy 	if (atadebug_mask & (level)) \
     82    1.2    bouyer 		printf args
     83    1.2    bouyer #else
     84   1.47   thorpej #define ATADEBUG_PRINT(args, level)
     85    1.2    bouyer #endif
     86    1.2    bouyer 
     87  1.159  jdolecek #if defined(ATA_DOWNGRADE_MODE) && NATA_DMA
     88  1.158  jdolecek static int	ata_downgrade_mode(struct ata_drive_datas *, int);
     89  1.158  jdolecek #endif
     90  1.158  jdolecek 
     91  1.114     rmind static ONCE_DECL(ata_init_ctrl);
     92  1.142  jdolecek static struct pool ata_xfer_pool;
     93   1.41   thorpej 
     94   1.51   thorpej /*
     95   1.51   thorpej  * A queue of atabus instances, used to ensure the same bus probe order
     96  1.114     rmind  * for a given hardware configuration at each boot.  Kthread probing
     97  1.114     rmind  * devices on a atabus.  Only one probing at once.
     98   1.51   thorpej  */
     99  1.114     rmind static TAILQ_HEAD(, atabus_initq)	atabus_initq_head;
    100  1.114     rmind static kmutex_t				atabus_qlock;
    101  1.114     rmind static kcondvar_t			atabus_qcv;
    102  1.114     rmind static lwp_t *				atabus_cfg_lwp;
    103  1.100    bouyer 
    104   1.23   thorpej /*****************************************************************************
    105   1.23   thorpej  * ATA bus layer.
    106   1.23   thorpej  *
    107   1.23   thorpej  * ATA controllers attach an atabus instance, which handles probing the bus
    108   1.23   thorpej  * for drives, etc.
    109   1.23   thorpej  *****************************************************************************/
    110   1.23   thorpej 
    111   1.30    bouyer dev_type_open(atabusopen);
    112   1.30    bouyer dev_type_close(atabusclose);
    113   1.30    bouyer dev_type_ioctl(atabusioctl);
    114   1.30    bouyer 
    115   1.30    bouyer const struct cdevsw atabus_cdevsw = {
    116  1.130  dholland 	.d_open = atabusopen,
    117  1.130  dholland 	.d_close = atabusclose,
    118  1.130  dholland 	.d_read = noread,
    119  1.130  dholland 	.d_write = nowrite,
    120  1.130  dholland 	.d_ioctl = atabusioctl,
    121  1.130  dholland 	.d_stop = nostop,
    122  1.130  dholland 	.d_tty = notty,
    123  1.130  dholland 	.d_poll = nopoll,
    124  1.130  dholland 	.d_mmap = nommap,
    125  1.130  dholland 	.d_kqfilter = nokqfilter,
    126  1.131  dholland 	.d_discard = nodiscard,
    127  1.130  dholland 	.d_flag = D_OTHER
    128   1.30    bouyer };
    129   1.30    bouyer 
    130   1.95    dyoung static void atabus_childdetached(device_t, device_t);
    131  1.115  jakllsch static int atabus_rescan(device_t, const char *, const int *);
    132  1.112    dyoung static bool atabus_resume(device_t, const pmf_qual_t *);
    133  1.112    dyoung static bool atabus_suspend(device_t, const pmf_qual_t *);
    134  1.100    bouyer static void atabusconfig_thread(void *);
    135   1.30    bouyer 
    136  1.133  jdolecek static void ata_channel_idle(struct ata_channel *);
    137  1.133  jdolecek static void ata_activate_xfer_locked(struct ata_channel *, struct ata_xfer *);
    138  1.133  jdolecek static void ata_channel_freeze_locked(struct ata_channel *);
    139  1.133  jdolecek static void ata_thread_wake_locked(struct ata_channel *);
    140  1.133  jdolecek 
    141   1.23   thorpej /*
    142  1.114     rmind  * atabus_init:
    143  1.114     rmind  *
    144  1.114     rmind  *	Initialize ATA subsystem structures.
    145  1.114     rmind  */
    146  1.114     rmind static int
    147  1.114     rmind atabus_init(void)
    148  1.114     rmind {
    149  1.114     rmind 
    150  1.142  jdolecek 	pool_init(&ata_xfer_pool, sizeof(struct ata_xfer), 0, 0, 0,
    151  1.142  jdolecek 	    "ataspl", NULL, IPL_BIO);
    152  1.114     rmind 	TAILQ_INIT(&atabus_initq_head);
    153  1.114     rmind 	mutex_init(&atabus_qlock, MUTEX_DEFAULT, IPL_NONE);
    154  1.114     rmind 	cv_init(&atabus_qcv, "atainitq");
    155  1.114     rmind 	return 0;
    156  1.114     rmind }
    157  1.114     rmind 
    158  1.114     rmind /*
    159   1.23   thorpej  * atabusprint:
    160   1.23   thorpej  *
    161   1.23   thorpej  *	Autoconfiguration print routine used by ATA controllers when
    162   1.23   thorpej  *	attaching an atabus instance.
    163   1.23   thorpej  */
    164   1.23   thorpej int
    165   1.23   thorpej atabusprint(void *aux, const char *pnp)
    166   1.23   thorpej {
    167   1.48   thorpej 	struct ata_channel *chan = aux;
    168   1.65     perry 
    169   1.23   thorpej 	if (pnp)
    170   1.23   thorpej 		aprint_normal("atabus at %s", pnp);
    171   1.26   thorpej 	aprint_normal(" channel %d", chan->ch_channel);
    172   1.23   thorpej 
    173   1.23   thorpej 	return (UNCONF);
    174   1.23   thorpej }
    175   1.23   thorpej 
    176   1.23   thorpej /*
    177   1.23   thorpej  * ataprint:
    178   1.23   thorpej  *
    179   1.23   thorpej  *	Autoconfiguration print routine.
    180   1.23   thorpej  */
    181   1.23   thorpej int
    182   1.23   thorpej ataprint(void *aux, const char *pnp)
    183   1.23   thorpej {
    184   1.23   thorpej 	struct ata_device *adev = aux;
    185   1.23   thorpej 
    186   1.23   thorpej 	if (pnp)
    187   1.23   thorpej 		aprint_normal("wd at %s", pnp);
    188   1.23   thorpej 	aprint_normal(" drive %d", adev->adev_drv_data->drive);
    189   1.23   thorpej 
    190   1.23   thorpej 	return (UNCONF);
    191   1.23   thorpej }
    192   1.23   thorpej 
    193   1.52   thorpej /*
    194   1.52   thorpej  * ata_channel_attach:
    195   1.52   thorpej  *
    196   1.52   thorpej  *	Common parts of attaching an atabus to an ATA controller channel.
    197   1.52   thorpej  */
    198   1.52   thorpej void
    199   1.52   thorpej ata_channel_attach(struct ata_channel *chp)
    200   1.52   thorpej {
    201   1.52   thorpej 	if (chp->ch_flags & ATACH_DISABLED)
    202   1.52   thorpej 		return;
    203   1.65     perry 
    204  1.140  jdolecek 	ata_channel_init(chp);
    205  1.140  jdolecek 
    206  1.133  jdolecek 	KASSERT(chp->ch_queue != NULL);
    207   1.52   thorpej 
    208   1.98      cube 	chp->atabus = config_found_ia(chp->ch_atac->atac_dev, "ata", chp,
    209   1.71  drochner 		atabusprint);
    210   1.52   thorpej }
    211   1.52   thorpej 
    212  1.133  jdolecek /*
    213  1.133  jdolecek  * ata_channel_detach:
    214  1.133  jdolecek  *
    215  1.133  jdolecek  *	Common parts of detaching an atabus to an ATA controller channel.
    216  1.133  jdolecek  */
    217  1.133  jdolecek void
    218  1.133  jdolecek ata_channel_detach(struct ata_channel *chp)
    219  1.133  jdolecek {
    220  1.133  jdolecek 	if (chp->ch_flags & ATACH_DISABLED)
    221  1.133  jdolecek 		return;
    222  1.133  jdolecek 
    223  1.133  jdolecek 	ata_channel_destroy(chp);
    224  1.143  jdolecek 
    225  1.143  jdolecek 	chp->ch_flags |= ATACH_DETACHED;
    226  1.133  jdolecek }
    227  1.133  jdolecek 
    228   1.51   thorpej static void
    229   1.51   thorpej atabusconfig(struct atabus_softc *atabus_sc)
    230   1.51   thorpej {
    231   1.51   thorpej 	struct ata_channel *chp = atabus_sc->sc_chan;
    232   1.51   thorpej 	struct atac_softc *atac = chp->ch_atac;
    233   1.51   thorpej 	struct atabus_initq *atabus_initq = NULL;
    234  1.133  jdolecek 	int i, error;
    235  1.102    bouyer 
    236  1.102    bouyer 	/* we are in the atabus's thread context */
    237   1.51   thorpej 
    238  1.124    bouyer 	/*
    239  1.124    bouyer 	 * Probe for the drives attached to controller, unless a PMP
    240  1.124    bouyer 	 * is already known
    241  1.124    bouyer 	 */
    242   1.82    bouyer 	/* XXX for SATA devices we will power up all drives at once */
    243  1.124    bouyer 	if (chp->ch_satapmp_nports == 0)
    244  1.124    bouyer 		(*atac->atac_probe)(chp);
    245   1.51   thorpej 
    246  1.125    bouyer 	if (chp->ch_ndrives >= 2) {
    247  1.124    bouyer 		ATADEBUG_PRINT(("atabusattach: ch_drive_type 0x%x 0x%x\n",
    248  1.124    bouyer 		    chp->ch_drive[0].drive_type, chp->ch_drive[1].drive_type),
    249  1.124    bouyer 		    DEBUG_PROBE);
    250  1.124    bouyer 	}
    251   1.51   thorpej 
    252  1.100    bouyer 	/* Make sure the devices probe in atabus order to avoid jitter. */
    253  1.114     rmind 	mutex_enter(&atabus_qlock);
    254  1.114     rmind 	for (;;) {
    255  1.100    bouyer 		atabus_initq = TAILQ_FIRST(&atabus_initq_head);
    256  1.100    bouyer 		if (atabus_initq->atabus_sc == atabus_sc)
    257  1.100    bouyer 			break;
    258  1.114     rmind 		cv_wait(&atabus_qcv, &atabus_qlock);
    259  1.100    bouyer 	}
    260  1.114     rmind 	mutex_exit(&atabus_qlock);
    261  1.100    bouyer 
    262  1.133  jdolecek 	ata_channel_lock(chp);
    263  1.133  jdolecek 
    264  1.154  jdolecek 	KASSERT(ata_is_thread_run(chp));
    265  1.154  jdolecek 
    266   1.51   thorpej 	/* If no drives, abort here */
    267  1.124    bouyer 	if (chp->ch_drive == NULL)
    268  1.124    bouyer 		goto out;
    269  1.124    bouyer 	KASSERT(chp->ch_ndrives == 0 || chp->ch_drive != NULL);
    270  1.124    bouyer 	for (i = 0; i < chp->ch_ndrives; i++)
    271  1.124    bouyer 		if (chp->ch_drive[i].drive_type != ATA_DRIVET_NONE)
    272   1.51   thorpej 			break;
    273  1.124    bouyer 	if (i == chp->ch_ndrives)
    274   1.51   thorpej 		goto out;
    275   1.51   thorpej 
    276   1.51   thorpej 	/* Shortcut in case we've been shutdown */
    277   1.51   thorpej 	if (chp->ch_flags & ATACH_SHUTDOWN)
    278   1.51   thorpej 		goto out;
    279   1.51   thorpej 
    280  1.133  jdolecek 	ata_channel_unlock(chp);
    281  1.133  jdolecek 
    282  1.100    bouyer 	if ((error = kthread_create(PRI_NONE, 0, NULL, atabusconfig_thread,
    283  1.114     rmind 	    atabus_sc, &atabus_cfg_lwp,
    284  1.100    bouyer 	    "%scnf", device_xname(atac->atac_dev))) != 0)
    285  1.100    bouyer 		aprint_error_dev(atac->atac_dev,
    286  1.100    bouyer 		    "unable to create config thread: error %d\n", error);
    287  1.100    bouyer 	return;
    288  1.100    bouyer 
    289  1.100    bouyer  out:
    290  1.133  jdolecek 	ata_channel_unlock(chp);
    291  1.133  jdolecek 
    292  1.114     rmind 	mutex_enter(&atabus_qlock);
    293  1.100    bouyer 	TAILQ_REMOVE(&atabus_initq_head, atabus_initq, atabus_initq);
    294  1.114     rmind 	cv_broadcast(&atabus_qcv);
    295  1.114     rmind 	mutex_exit(&atabus_qlock);
    296   1.51   thorpej 
    297  1.142  jdolecek 	kmem_free(atabus_initq, sizeof(*atabus_initq));
    298  1.100    bouyer 
    299  1.100    bouyer 	ata_delref(chp);
    300  1.100    bouyer 
    301  1.160  riastrad 	config_pending_decr(atabus_sc->sc_dev);
    302  1.100    bouyer }
    303  1.100    bouyer 
    304  1.100    bouyer /*
    305  1.100    bouyer  * atabus_configthread: finish attach of atabus's childrens, in a separate
    306  1.100    bouyer  * kernel thread.
    307  1.100    bouyer  */
    308  1.100    bouyer static void
    309  1.100    bouyer atabusconfig_thread(void *arg)
    310  1.100    bouyer {
    311  1.100    bouyer 	struct atabus_softc *atabus_sc = arg;
    312  1.100    bouyer 	struct ata_channel *chp = atabus_sc->sc_chan;
    313  1.100    bouyer 	struct atac_softc *atac = chp->ch_atac;
    314  1.114     rmind 	struct atabus_initq *atabus_initq = NULL;
    315  1.100    bouyer 	int i, s;
    316  1.100    bouyer 
    317  1.114     rmind 	/* XXX seems wrong */
    318  1.114     rmind 	mutex_enter(&atabus_qlock);
    319  1.100    bouyer 	atabus_initq = TAILQ_FIRST(&atabus_initq_head);
    320  1.100    bouyer 	KASSERT(atabus_initq->atabus_sc == atabus_sc);
    321  1.114     rmind 	mutex_exit(&atabus_qlock);
    322  1.114     rmind 
    323   1.51   thorpej 	/*
    324  1.124    bouyer 	 * First look for a port multiplier
    325  1.124    bouyer 	 */
    326  1.124    bouyer 	if (chp->ch_ndrives == PMP_MAX_DRIVES &&
    327  1.124    bouyer 	    chp->ch_drive[PMP_PORT_CTL].drive_type == ATA_DRIVET_PM) {
    328  1.124    bouyer #if NSATA_PMP > 0
    329  1.124    bouyer 		satapmp_attach(chp);
    330  1.124    bouyer #else
    331  1.124    bouyer 		aprint_error_dev(atabus_sc->sc_dev,
    332  1.124    bouyer 		    "SATA port multiplier not supported\n");
    333  1.124    bouyer 		/* no problems going on, all drives are ATA_DRIVET_NONE */
    334  1.124    bouyer #endif
    335  1.124    bouyer 	}
    336  1.124    bouyer 
    337  1.124    bouyer 	/*
    338   1.51   thorpej 	 * Attach an ATAPI bus, if needed.
    339   1.51   thorpej 	 */
    340  1.124    bouyer 	KASSERT(chp->ch_ndrives == 0 || chp->ch_drive != NULL);
    341  1.124    bouyer 	for (i = 0; i < chp->ch_ndrives && chp->atapibus == NULL; i++) {
    342  1.124    bouyer 		if (chp->ch_drive[i].drive_type == ATA_DRIVET_ATAPI) {
    343   1.51   thorpej #if NATAPIBUS > 0
    344   1.51   thorpej 			(*atac->atac_atapibus_attach)(atabus_sc);
    345   1.51   thorpej #else
    346   1.51   thorpej 			/*
    347   1.51   thorpej 			 * Fake the autoconfig "not configured" message
    348   1.51   thorpej 			 */
    349   1.51   thorpej 			aprint_normal("atapibus at %s not configured\n",
    350   1.98      cube 			    device_xname(atac->atac_dev));
    351   1.51   thorpej 			chp->atapibus = NULL;
    352   1.58   thorpej 			s = splbio();
    353  1.124    bouyer 			for (i = 0; i < chp->ch_ndrives; i++) {
    354  1.124    bouyer 				if (chp->ch_drive[i].drive_type == ATA_DRIVET_ATAPI)
    355  1.124    bouyer 					chp->ch_drive[i].drive_type = ATA_DRIVET_NONE;
    356  1.124    bouyer 			}
    357   1.58   thorpej 			splx(s);
    358   1.51   thorpej #endif
    359   1.51   thorpej 			break;
    360   1.51   thorpej 		}
    361   1.51   thorpej 	}
    362   1.51   thorpej 
    363  1.124    bouyer 	for (i = 0; i < chp->ch_ndrives; i++) {
    364   1.51   thorpej 		struct ata_device adev;
    365  1.124    bouyer 		if (chp->ch_drive[i].drive_type != ATA_DRIVET_ATA &&
    366  1.124    bouyer 		    chp->ch_drive[i].drive_type != ATA_DRIVET_OLD) {
    367   1.51   thorpej 			continue;
    368   1.51   thorpej 		}
    369  1.124    bouyer 		if (chp->ch_drive[i].drv_softc != NULL)
    370  1.124    bouyer 			continue;
    371   1.51   thorpej 		memset(&adev, 0, sizeof(struct ata_device));
    372   1.51   thorpej 		adev.adev_bustype = atac->atac_bustype_ata;
    373   1.51   thorpej 		adev.adev_channel = chp->ch_channel;
    374   1.51   thorpej 		adev.adev_drv_data = &chp->ch_drive[i];
    375  1.123  jakllsch 		chp->ch_drive[i].drv_softc = config_found_ia(atabus_sc->sc_dev,
    376   1.71  drochner 		    "ata_hl", &adev, ataprint);
    377  1.124    bouyer 		if (chp->ch_drive[i].drv_softc != NULL) {
    378   1.51   thorpej 			ata_probe_caps(&chp->ch_drive[i]);
    379  1.124    bouyer 		} else {
    380   1.58   thorpej 			s = splbio();
    381  1.124    bouyer 			chp->ch_drive[i].drive_type = ATA_DRIVET_NONE;
    382   1.58   thorpej 			splx(s);
    383   1.58   thorpej 		}
    384   1.51   thorpej 	}
    385   1.51   thorpej 
    386   1.51   thorpej 	/* now that we know the drives, the controller can set its modes */
    387   1.51   thorpej 	if (atac->atac_set_modes) {
    388   1.51   thorpej 		(*atac->atac_set_modes)(chp);
    389   1.51   thorpej 		ata_print_modes(chp);
    390   1.51   thorpej 	}
    391   1.51   thorpej #if NATARAID > 0
    392  1.123  jakllsch 	if (atac->atac_cap & ATAC_CAP_RAID) {
    393  1.124    bouyer 		for (i = 0; i < chp->ch_ndrives; i++) {
    394  1.124    bouyer 			if (chp->ch_drive[i].drive_type == ATA_DRIVET_ATA) {
    395  1.123  jakllsch 				ata_raid_check_component(
    396  1.123  jakllsch 				    chp->ch_drive[i].drv_softc);
    397  1.123  jakllsch 			}
    398  1.123  jakllsch 		}
    399  1.123  jakllsch 	}
    400   1.51   thorpej #endif /* NATARAID > 0 */
    401   1.51   thorpej 
    402   1.51   thorpej 	/*
    403   1.51   thorpej 	 * reset drive_flags for unattached devices, reset state for attached
    404   1.51   thorpej 	 * ones
    405   1.51   thorpej 	 */
    406   1.58   thorpej 	s = splbio();
    407  1.124    bouyer 	for (i = 0; i < chp->ch_ndrives; i++) {
    408  1.124    bouyer 		if (chp->ch_drive[i].drive_type == ATA_DRIVET_PM)
    409  1.124    bouyer 			continue;
    410  1.124    bouyer 		if (chp->ch_drive[i].drv_softc == NULL) {
    411   1.51   thorpej 			chp->ch_drive[i].drive_flags = 0;
    412  1.124    bouyer 			chp->ch_drive[i].drive_type = ATA_DRIVET_NONE;
    413  1.124    bouyer 		} else
    414   1.51   thorpej 			chp->ch_drive[i].state = 0;
    415   1.51   thorpej 	}
    416   1.58   thorpej 	splx(s);
    417   1.51   thorpej 
    418  1.114     rmind 	mutex_enter(&atabus_qlock);
    419   1.81     itohy 	TAILQ_REMOVE(&atabus_initq_head, atabus_initq, atabus_initq);
    420  1.114     rmind 	cv_broadcast(&atabus_qcv);
    421  1.114     rmind 	mutex_exit(&atabus_qlock);
    422   1.51   thorpej 
    423  1.142  jdolecek 	kmem_free(atabus_initq, sizeof(*atabus_initq));
    424   1.51   thorpej 
    425   1.51   thorpej 	ata_delref(chp);
    426   1.51   thorpej 
    427  1.160  riastrad 	config_pending_decr(atabus_sc->sc_dev);
    428  1.100    bouyer 	kthread_exit(0);
    429   1.51   thorpej }
    430   1.51   thorpej 
    431   1.23   thorpej /*
    432  1.157   thorpej  * atabus_thread:
    433   1.23   thorpej  *
    434   1.23   thorpej  *	Worker thread for the ATA bus.
    435   1.23   thorpej  */
    436   1.23   thorpej static void
    437  1.157   thorpej atabus_thread(void *arg)
    438   1.23   thorpej {
    439  1.157   thorpej 	struct atabus_softc *sc = arg;
    440  1.157   thorpej 	struct ata_channel *chp = sc->sc_chan;
    441  1.133  jdolecek 	struct ata_queue *chq = chp->ch_queue;
    442   1.24   thorpej 	struct ata_xfer *xfer;
    443  1.142  jdolecek 	int i, rv;
    444   1.23   thorpej 
    445  1.133  jdolecek 	ata_channel_lock(chp);
    446  1.157   thorpej 	KASSERT(ata_is_thread_run(chp));
    447  1.102    bouyer 
    448  1.157   thorpej 	/*
    449  1.157   thorpej 	 * Probe the drives.  Reset type to indicate to controllers
    450  1.157   thorpej 	 * that can re-probe that all drives must be probed..
    451  1.157   thorpej 	 *
    452  1.157   thorpej 	 * Note: ch_ndrives may be changed during the probe.
    453  1.157   thorpej 	 */
    454  1.157   thorpej 	KASSERT(chp->ch_ndrives == 0 || chp->ch_drive != NULL);
    455  1.157   thorpej 	for (i = 0; i < chp->ch_ndrives; i++) {
    456  1.157   thorpej 		chp->ch_drive[i].drive_flags = 0;
    457  1.157   thorpej 		chp->ch_drive[i].drive_type = ATA_DRIVET_NONE;
    458  1.124    bouyer 	}
    459  1.157   thorpej 	ata_channel_unlock(chp);
    460   1.23   thorpej 
    461  1.157   thorpej 	atabusconfig(sc);
    462   1.23   thorpej 
    463  1.157   thorpej 	ata_channel_lock(chp);
    464   1.23   thorpej 	for (;;) {
    465  1.157   thorpej 		if ((chp->ch_flags & (ATACH_TH_RESET | ATACH_TH_DRIVE_RESET
    466  1.157   thorpej 		    | ATACH_TH_RECOVERY | ATACH_SHUTDOWN)) == 0 &&
    467  1.157   thorpej 		    (chq->queue_active == 0 || chq->queue_freeze == 0)) {
    468  1.157   thorpej 			cv_wait(&chp->ch_thr_idle, &chp->ch_lock);
    469  1.157   thorpej 		}
    470  1.156   thorpej 		if (chp->ch_flags & ATACH_SHUTDOWN) {
    471  1.156   thorpej 			break;
    472  1.156   thorpej 		}
    473  1.115  jakllsch 		if (chp->ch_flags & ATACH_TH_RESCAN) {
    474  1.133  jdolecek 			chp->ch_flags &= ~ATACH_TH_RESCAN;
    475  1.133  jdolecek 			ata_channel_unlock(chp);
    476  1.115  jakllsch 			atabusconfig(sc);
    477  1.133  jdolecek 			ata_channel_lock(chp);
    478  1.115  jakllsch 		}
    479   1.48   thorpej 		if (chp->ch_flags & ATACH_TH_RESET) {
    480  1.142  jdolecek 			/* this will unfreeze the channel */
    481  1.142  jdolecek 			ata_thread_run(chp, AT_WAIT,
    482  1.142  jdolecek 			    ATACH_TH_RESET, ATACH_NODRIVE);
    483  1.142  jdolecek 		} else if (chp->ch_flags & ATACH_TH_DRIVE_RESET) {
    484  1.142  jdolecek 			/* this will unfreeze the channel */
    485  1.142  jdolecek 			for (i = 0; i < chp->ch_ndrives; i++) {
    486  1.142  jdolecek 				struct ata_drive_datas *drvp;
    487  1.142  jdolecek 
    488  1.142  jdolecek 				drvp = &chp->ch_drive[i];
    489  1.142  jdolecek 
    490  1.142  jdolecek 				if (drvp->drive_flags & ATA_DRIVE_TH_RESET) {
    491  1.142  jdolecek 					ata_thread_run(chp,
    492  1.142  jdolecek 					    AT_WAIT, ATACH_TH_DRIVE_RESET, i);
    493  1.142  jdolecek 				}
    494  1.142  jdolecek 			}
    495  1.142  jdolecek 			chp->ch_flags &= ~ATACH_TH_DRIVE_RESET;
    496  1.142  jdolecek 		} else if (chp->ch_flags & ATACH_TH_RECOVERY) {
    497  1.142  jdolecek 			/*
    498  1.142  jdolecek 			 * This will unfreeze the channel; drops locks during
    499  1.142  jdolecek 			 * run, so must wrap in splbio()/splx() to avoid
    500  1.142  jdolecek 			 * spurious interrupts. XXX MPSAFE
    501  1.142  jdolecek 			 */
    502  1.142  jdolecek 			int s = splbio();
    503  1.142  jdolecek 			ata_thread_run(chp, AT_WAIT, ATACH_TH_RECOVERY,
    504  1.142  jdolecek 			    chp->recovery_tfd);
    505  1.133  jdolecek 			splx(s);
    506  1.133  jdolecek 		} else if (chq->queue_active > 0 && chq->queue_freeze == 1) {
    507   1.23   thorpej 			/*
    508  1.133  jdolecek 			 * Caller has bumped queue_freeze, decrease it. This
    509  1.133  jdolecek 			 * flow shalt never be executed for NCQ commands.
    510   1.23   thorpej 			 */
    511  1.133  jdolecek 			KASSERT((chp->ch_flags & ATACH_NCQ) == 0);
    512  1.133  jdolecek 			KASSERT(chq->queue_active == 1);
    513  1.133  jdolecek 
    514  1.133  jdolecek 			ata_channel_thaw_locked(chp);
    515  1.133  jdolecek 			xfer = ata_queue_get_active_xfer_locked(chp);
    516  1.133  jdolecek 
    517   1.23   thorpej 			KASSERT(xfer != NULL);
    518  1.133  jdolecek 			KASSERT((xfer->c_flags & C_POLL) == 0);
    519  1.133  jdolecek 
    520  1.133  jdolecek 			switch ((rv = ata_xfer_start(xfer))) {
    521  1.133  jdolecek 			case ATASTART_STARTED:
    522  1.133  jdolecek 			case ATASTART_POLL:
    523  1.133  jdolecek 			case ATASTART_ABORT:
    524  1.133  jdolecek 				break;
    525  1.133  jdolecek 			case ATASTART_TH:
    526  1.133  jdolecek 			default:
    527  1.133  jdolecek 				panic("%s: ata_xfer_start() unexpected rv %d",
    528  1.133  jdolecek 				    __func__, rv);
    529  1.133  jdolecek 				/* NOTREACHED */
    530  1.133  jdolecek 			}
    531  1.133  jdolecek 		} else if (chq->queue_freeze > 1)
    532  1.133  jdolecek 			panic("%s: queue_freeze", __func__);
    533  1.142  jdolecek 
    534  1.142  jdolecek 		/* Try to run down the queue once channel is unfrozen */
    535  1.142  jdolecek 		if (chq->queue_freeze == 0) {
    536  1.142  jdolecek 			ata_channel_unlock(chp);
    537  1.142  jdolecek 			atastart(chp);
    538  1.142  jdolecek 			ata_channel_lock(chp);
    539  1.142  jdolecek 		}
    540   1.23   thorpej 	}
    541  1.157   thorpej 	chp->ch_thread = NULL;
    542  1.157   thorpej 	cv_signal(&chp->ch_thr_idle);
    543  1.133  jdolecek 	ata_channel_unlock(chp);
    544  1.157   thorpej 	kthread_exit(0);
    545   1.23   thorpej }
    546   1.23   thorpej 
    547  1.154  jdolecek bool
    548  1.154  jdolecek ata_is_thread_run(struct ata_channel *chp)
    549  1.154  jdolecek {
    550  1.154  jdolecek 	KASSERT(mutex_owned(&chp->ch_lock));
    551  1.154  jdolecek 
    552  1.157   thorpej 	return (chp->ch_thread == curlwp && !cpu_intr_p());
    553  1.154  jdolecek }
    554  1.154  jdolecek 
    555  1.133  jdolecek static void
    556  1.133  jdolecek ata_thread_wake_locked(struct ata_channel *chp)
    557  1.133  jdolecek {
    558  1.133  jdolecek 	KASSERT(mutex_owned(&chp->ch_lock));
    559  1.133  jdolecek 	ata_channel_freeze_locked(chp);
    560  1.157   thorpej 	cv_signal(&chp->ch_thr_idle);
    561  1.133  jdolecek }
    562  1.133  jdolecek 
    563   1.23   thorpej /*
    564   1.23   thorpej  * atabus_match:
    565   1.23   thorpej  *
    566   1.23   thorpej  *	Autoconfiguration match routine.
    567   1.23   thorpej  */
    568   1.23   thorpej static int
    569  1.106    cegger atabus_match(device_t parent, cfdata_t cf, void *aux)
    570   1.23   thorpej {
    571   1.48   thorpej 	struct ata_channel *chp = aux;
    572   1.23   thorpej 
    573   1.23   thorpej 	if (chp == NULL)
    574   1.23   thorpej 		return (0);
    575   1.65     perry 
    576   1.26   thorpej 	if (cf->cf_loc[ATACF_CHANNEL] != chp->ch_channel &&
    577   1.23   thorpej 	    cf->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT)
    578   1.81     itohy 		return (0);
    579   1.65     perry 
    580   1.23   thorpej 	return (1);
    581   1.23   thorpej }
    582   1.23   thorpej 
    583   1.23   thorpej /*
    584   1.23   thorpej  * atabus_attach:
    585   1.23   thorpej  *
    586   1.23   thorpej  *	Autoconfiguration attach routine.
    587   1.23   thorpej  */
    588   1.23   thorpej static void
    589   1.95    dyoung atabus_attach(device_t parent, device_t self, void *aux)
    590   1.23   thorpej {
    591   1.95    dyoung 	struct atabus_softc *sc = device_private(self);
    592   1.48   thorpej 	struct ata_channel *chp = aux;
    593   1.23   thorpej 	struct atabus_initq *initq;
    594  1.157   thorpej 	int error;
    595   1.23   thorpej 
    596   1.23   thorpej 	sc->sc_chan = chp;
    597   1.23   thorpej 
    598   1.23   thorpej 	aprint_normal("\n");
    599   1.23   thorpej 	aprint_naive("\n");
    600   1.23   thorpej 
    601   1.98      cube 	sc->sc_dev = self;
    602   1.98      cube 
    603   1.81     itohy 	if (ata_addref(chp))
    604   1.81     itohy 		return;
    605   1.35   mycroft 
    606  1.114     rmind 	RUN_ONCE(&ata_init_ctrl, atabus_init);
    607  1.107     pooka 
    608  1.142  jdolecek 	initq = kmem_zalloc(sizeof(*initq), KM_SLEEP);
    609   1.23   thorpej 	initq->atabus_sc = sc;
    610  1.133  jdolecek 	mutex_enter(&atabus_qlock);
    611   1.23   thorpej 	TAILQ_INSERT_TAIL(&atabus_initq_head, initq, atabus_initq);
    612  1.133  jdolecek 	mutex_exit(&atabus_qlock);
    613  1.129  christos 	config_pending_incr(sc->sc_dev);
    614   1.90        ad 
    615  1.157   thorpej 	/* XXX MPSAFE - no KTHREAD_MPSAFE, so protected by KERNEL_LOCK() */
    616  1.157   thorpej 	if ((error = kthread_create(PRI_NONE, 0, NULL, atabus_thread, sc,
    617  1.157   thorpej 	    &chp->ch_thread, "%s", device_xname(self))) != 0)
    618  1.157   thorpej 		aprint_error_dev(self,
    619  1.157   thorpej 		    "unable to create kernel thread: error %d\n", error);
    620   1.64  jmcneill 
    621   1.92  jmcneill 	if (!pmf_device_register(self, atabus_suspend, atabus_resume))
    622   1.92  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    623   1.23   thorpej }
    624   1.23   thorpej 
    625   1.23   thorpej /*
    626   1.23   thorpej  * atabus_detach:
    627   1.23   thorpej  *
    628   1.23   thorpej  *	Autoconfiguration detach routine.
    629   1.23   thorpej  */
    630   1.23   thorpej static int
    631   1.95    dyoung atabus_detach(device_t self, int flags)
    632   1.23   thorpej {
    633   1.95    dyoung 	struct atabus_softc *sc = device_private(self);
    634   1.48   thorpej 	struct ata_channel *chp = sc->sc_chan;
    635   1.95    dyoung 	device_t dev = NULL;
    636  1.133  jdolecek 	int i, error = 0;
    637   1.23   thorpej 
    638   1.23   thorpej 	/*
    639   1.23   thorpej 	 * Detach atapibus and its children.
    640   1.23   thorpej 	 */
    641   1.23   thorpej 	if ((dev = chp->atapibus) != NULL) {
    642   1.47   thorpej 		ATADEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
    643   1.95    dyoung 		    device_xname(self), device_xname(dev)), DEBUG_DETACH);
    644   1.95    dyoung 
    645   1.23   thorpej 		error = config_detach(dev, flags);
    646   1.23   thorpej 		if (error)
    647   1.23   thorpej 			goto out;
    648  1.104    dyoung 		KASSERT(chp->atapibus == NULL);
    649   1.23   thorpej 	}
    650   1.23   thorpej 
    651  1.124    bouyer 	KASSERT(chp->ch_ndrives == 0 || chp->ch_drive != NULL);
    652  1.124    bouyer 
    653   1.23   thorpej 	/*
    654   1.23   thorpej 	 * Detach our other children.
    655   1.23   thorpej 	 */
    656  1.124    bouyer 	for (i = 0; i < chp->ch_ndrives; i++) {
    657  1.124    bouyer 		if (chp->ch_drive[i].drive_type == ATA_DRIVET_ATAPI)
    658   1.23   thorpej 			continue;
    659  1.124    bouyer 		if (chp->ch_drive[i].drive_type == ATA_DRIVET_PM)
    660  1.124    bouyer 			chp->ch_drive[i].drive_type = ATA_DRIVET_NONE;
    661  1.123  jakllsch 		if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
    662  1.104    dyoung 			ATADEBUG_PRINT(("%s.%d: %s: detaching %s\n", __func__,
    663  1.104    dyoung 			    __LINE__, device_xname(self), device_xname(dev)),
    664   1.23   thorpej 			    DEBUG_DETACH);
    665   1.23   thorpej 			error = config_detach(dev, flags);
    666   1.23   thorpej 			if (error)
    667   1.23   thorpej 				goto out;
    668  1.123  jakllsch 			KASSERT(chp->ch_drive[i].drv_softc == NULL);
    669  1.124    bouyer 			KASSERT(chp->ch_drive[i].drive_type == 0);
    670   1.23   thorpej 		}
    671   1.23   thorpej 	}
    672  1.147  jdolecek 
    673  1.157   thorpej 	/* Shutdown the channel. */
    674  1.147  jdolecek 	ata_channel_lock(chp);
    675  1.147  jdolecek 	chp->ch_flags |= ATACH_SHUTDOWN;
    676  1.157   thorpej 	while (chp->ch_thread != NULL) {
    677  1.157   thorpej 		cv_signal(&chp->ch_thr_idle);
    678  1.157   thorpej 		cv_wait(&chp->ch_thr_idle, &chp->ch_lock);
    679  1.157   thorpej 	}
    680  1.147  jdolecek 	ata_channel_unlock(chp);
    681  1.147  jdolecek 
    682  1.124    bouyer 	atabus_free_drives(chp);
    683   1.23   thorpej 
    684   1.23   thorpej  out:
    685   1.47   thorpej #ifdef ATADEBUG
    686   1.23   thorpej 	if (dev != NULL && error != 0)
    687  1.104    dyoung 		ATADEBUG_PRINT(("%s: %s: error %d detaching %s\n", __func__,
    688   1.95    dyoung 		    device_xname(self), error, device_xname(dev)),
    689   1.23   thorpej 		    DEBUG_DETACH);
    690   1.47   thorpej #endif /* ATADEBUG */
    691   1.23   thorpej 
    692   1.23   thorpej 	return (error);
    693   1.23   thorpej }
    694   1.23   thorpej 
    695   1.95    dyoung void
    696   1.95    dyoung atabus_childdetached(device_t self, device_t child)
    697   1.95    dyoung {
    698  1.104    dyoung 	bool found = false;
    699   1.95    dyoung 	struct atabus_softc *sc = device_private(self);
    700   1.95    dyoung 	struct ata_channel *chp = sc->sc_chan;
    701   1.95    dyoung 	int i;
    702   1.95    dyoung 
    703  1.124    bouyer 	KASSERT(chp->ch_ndrives == 0 || chp->ch_drive != NULL);
    704   1.95    dyoung 	/*
    705   1.95    dyoung 	 * atapibus detached.
    706   1.95    dyoung 	 */
    707   1.95    dyoung 	if (child == chp->atapibus) {
    708   1.95    dyoung 		chp->atapibus = NULL;
    709  1.104    dyoung 		found = true;
    710  1.124    bouyer 		for (i = 0; i < chp->ch_ndrives; i++) {
    711  1.124    bouyer 			if (chp->ch_drive[i].drive_type != ATA_DRIVET_ATAPI)
    712  1.124    bouyer 				continue;
    713  1.124    bouyer 			KASSERT(chp->ch_drive[i].drv_softc != NULL);
    714  1.124    bouyer 			chp->ch_drive[i].drv_softc = NULL;
    715  1.124    bouyer 			chp->ch_drive[i].drive_flags = 0;
    716  1.124    bouyer 			chp->ch_drive[i].drive_type = ATA_DRIVET_NONE;
    717  1.124    bouyer 		}
    718   1.95    dyoung 	}
    719   1.95    dyoung 
    720   1.95    dyoung 	/*
    721   1.95    dyoung 	 * Detach our other children.
    722   1.95    dyoung 	 */
    723  1.124    bouyer 	for (i = 0; i < chp->ch_ndrives; i++) {
    724  1.124    bouyer 		if (chp->ch_drive[i].drive_type == ATA_DRIVET_ATAPI)
    725   1.95    dyoung 			continue;
    726  1.123  jakllsch 		if (child == chp->ch_drive[i].drv_softc) {
    727   1.95    dyoung 			chp->ch_drive[i].drv_softc = NULL;
    728   1.95    dyoung 			chp->ch_drive[i].drive_flags = 0;
    729  1.124    bouyer 			if (chp->ch_drive[i].drive_type == ATA_DRIVET_PM)
    730  1.124    bouyer 				chp->ch_satapmp_nports = 0;
    731  1.124    bouyer 			chp->ch_drive[i].drive_type = ATA_DRIVET_NONE;
    732  1.104    dyoung 			found = true;
    733   1.95    dyoung 		}
    734   1.95    dyoung 	}
    735   1.95    dyoung 
    736  1.104    dyoung 	if (!found)
    737  1.104    dyoung 		panic("%s: unknown child %p", device_xname(self),
    738  1.104    dyoung 		    (const void *)child);
    739   1.95    dyoung }
    740   1.95    dyoung 
    741  1.103    dyoung CFATTACH_DECL3_NEW(atabus, sizeof(struct atabus_softc),
    742  1.115  jakllsch     atabus_match, atabus_attach, atabus_detach, NULL, atabus_rescan,
    743  1.103    dyoung     atabus_childdetached, DVF_DETACH_SHUTDOWN);
    744   1.23   thorpej 
    745   1.23   thorpej /*****************************************************************************
    746   1.23   thorpej  * Common ATA bus operations.
    747   1.23   thorpej  *****************************************************************************/
    748   1.23   thorpej 
    749  1.124    bouyer /* allocate/free the channel's ch_drive[] array */
    750  1.124    bouyer int
    751  1.124    bouyer atabus_alloc_drives(struct ata_channel *chp, int ndrives)
    752  1.124    bouyer {
    753  1.124    bouyer 	int i;
    754  1.124    bouyer 	if (chp->ch_ndrives != ndrives)
    755  1.124    bouyer 		atabus_free_drives(chp);
    756  1.124    bouyer 	if (chp->ch_drive == NULL) {
    757  1.153  christos 		void *drv;
    758  1.153  christos 
    759  1.153  christos 		ata_channel_unlock(chp);
    760  1.153  christos 		drv = kmem_zalloc(sizeof(*chp->ch_drive) * ndrives, KM_SLEEP);
    761  1.153  christos 		ata_channel_lock(chp);
    762  1.153  christos 
    763  1.153  christos 		if (chp->ch_drive != NULL) {
    764  1.153  christos 			/* lost the race */
    765  1.153  christos 			kmem_free(drv, sizeof(*chp->ch_drive) * ndrives);
    766  1.153  christos 			return 0;
    767  1.153  christos 		}
    768  1.153  christos 		chp->ch_drive = drv;
    769  1.124    bouyer 	}
    770  1.124    bouyer 	for (i = 0; i < ndrives; i++) {
    771  1.124    bouyer 		chp->ch_drive[i].chnl_softc = chp;
    772  1.124    bouyer 		chp->ch_drive[i].drive = i;
    773  1.124    bouyer 	}
    774  1.124    bouyer 	chp->ch_ndrives = ndrives;
    775  1.124    bouyer 	return 0;
    776  1.124    bouyer }
    777  1.124    bouyer 
    778  1.124    bouyer void
    779  1.124    bouyer atabus_free_drives(struct ata_channel *chp)
    780  1.124    bouyer {
    781  1.124    bouyer #ifdef DIAGNOSTIC
    782  1.124    bouyer 	int i;
    783  1.124    bouyer 	int dopanic = 0;
    784  1.124    bouyer 	KASSERT(chp->ch_ndrives == 0 || chp->ch_drive != NULL);
    785  1.124    bouyer 	for (i = 0; i < chp->ch_ndrives; i++) {
    786  1.124    bouyer 		if (chp->ch_drive[i].drive_type != ATA_DRIVET_NONE) {
    787  1.124    bouyer 			printf("%s: ch_drive[%d] type %d != ATA_DRIVET_NONE\n",
    788  1.124    bouyer 			    device_xname(chp->atabus), i,
    789  1.124    bouyer 			    chp->ch_drive[i].drive_type);
    790  1.124    bouyer 			dopanic = 1;
    791  1.124    bouyer 		}
    792  1.124    bouyer 		if (chp->ch_drive[i].drv_softc != NULL) {
    793  1.124    bouyer 			printf("%s: ch_drive[%d] attached to %s\n",
    794  1.124    bouyer 			    device_xname(chp->atabus), i,
    795  1.124    bouyer 			    device_xname(chp->ch_drive[i].drv_softc));
    796  1.124    bouyer 			dopanic = 1;
    797  1.124    bouyer 		}
    798  1.124    bouyer 	}
    799  1.124    bouyer 	if (dopanic)
    800  1.124    bouyer 		panic("atabus_free_drives");
    801  1.124    bouyer #endif
    802  1.124    bouyer 
    803  1.124    bouyer 	if (chp->ch_drive == NULL)
    804  1.124    bouyer 		return;
    805  1.142  jdolecek 	kmem_free(chp->ch_drive,
    806  1.142  jdolecek 	    sizeof(struct ata_drive_datas) * chp->ch_ndrives);
    807  1.124    bouyer 	chp->ch_ndrives = 0;
    808  1.124    bouyer 	chp->ch_drive = NULL;
    809  1.124    bouyer }
    810  1.124    bouyer 
    811    1.2    bouyer /* Get the disk's parameters */
    812    1.2    bouyer int
    813  1.132      matt ata_get_params(struct ata_drive_datas *drvp, uint8_t flags,
    814   1.21   thorpej     struct ataparams *prms)
    815    1.2    bouyer {
    816  1.133  jdolecek 	struct ata_xfer *xfer;
    817   1.50   thorpej 	struct ata_channel *chp = drvp->chnl_softc;
    818   1.50   thorpej 	struct atac_softc *atac = chp->ch_atac;
    819   1.93     pooka 	char *tb;
    820   1.93     pooka 	int i, rv;
    821  1.132      matt 	uint16_t *p;
    822    1.2    bouyer 
    823   1.88    dyoung 	ATADEBUG_PRINT(("%s\n", __func__), DEBUG_FUNCS);
    824    1.2    bouyer 
    825  1.142  jdolecek 	xfer = ata_get_xfer(chp, false);
    826  1.133  jdolecek 	if (xfer == NULL) {
    827  1.133  jdolecek 		ATADEBUG_PRINT(("%s: no xfer\n", __func__),
    828  1.133  jdolecek 		    DEBUG_FUNCS|DEBUG_PROBE);
    829  1.133  jdolecek 		return CMD_AGAIN;
    830  1.133  jdolecek 	}
    831  1.133  jdolecek 
    832  1.133  jdolecek 	tb = kmem_zalloc(ATA_BSIZE, KM_SLEEP);
    833    1.2    bouyer 	memset(prms, 0, sizeof(struct ataparams));
    834    1.2    bouyer 
    835  1.124    bouyer 	if (drvp->drive_type == ATA_DRIVET_ATA) {
    836  1.133  jdolecek 		xfer->c_ata_c.r_command = WDCC_IDENTIFY;
    837  1.133  jdolecek 		xfer->c_ata_c.r_st_bmask = WDCS_DRDY;
    838  1.133  jdolecek 		xfer->c_ata_c.r_st_pmask = WDCS_DRQ;
    839  1.133  jdolecek 		xfer->c_ata_c.timeout = 3000; /* 3s */
    840  1.124    bouyer 	} else if (drvp->drive_type == ATA_DRIVET_ATAPI) {
    841  1.133  jdolecek 		xfer->c_ata_c.r_command = ATAPI_IDENTIFY_DEVICE;
    842  1.133  jdolecek 		xfer->c_ata_c.r_st_bmask = 0;
    843  1.133  jdolecek 		xfer->c_ata_c.r_st_pmask = WDCS_DRQ;
    844  1.133  jdolecek 		xfer->c_ata_c.timeout = 10000; /* 10s */
    845    1.7    bouyer 	} else {
    846   1.47   thorpej 		ATADEBUG_PRINT(("ata_get_parms: no disks\n"),
    847   1.10    bouyer 		    DEBUG_FUNCS|DEBUG_PROBE);
    848   1.93     pooka 		rv = CMD_ERR;
    849   1.93     pooka 		goto out;
    850    1.2    bouyer 	}
    851  1.133  jdolecek 	xfer->c_ata_c.flags = AT_READ | flags;
    852  1.133  jdolecek 	xfer->c_ata_c.data = tb;
    853  1.133  jdolecek 	xfer->c_ata_c.bcount = ATA_BSIZE;
    854  1.155  jdolecek 	(*atac->atac_bustype_ata->ata_exec_command)(drvp, xfer);
    855  1.155  jdolecek 	ata_wait_cmd(chp, xfer);
    856  1.133  jdolecek 	if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    857   1.47   thorpej 		ATADEBUG_PRINT(("ata_get_parms: ata_c.flags=0x%x\n",
    858  1.133  jdolecek 		    xfer->c_ata_c.flags), DEBUG_FUNCS|DEBUG_PROBE);
    859   1.93     pooka 		rv = CMD_ERR;
    860   1.93     pooka 		goto out;
    861   1.89       dsl 	}
    862   1.89       dsl 	/* if we didn't read any data something is wrong */
    863  1.133  jdolecek 	if ((xfer->c_ata_c.flags & AT_XFDONE) == 0) {
    864   1.93     pooka 		rv = CMD_ERR;
    865   1.93     pooka 		goto out;
    866   1.93     pooka 	}
    867   1.89       dsl 
    868   1.89       dsl 	/* Read in parameter block. */
    869   1.89       dsl 	memcpy(prms, tb, sizeof(struct ataparams));
    870   1.80     itohy 
    871   1.89       dsl 	/*
    872   1.89       dsl 	 * Shuffle string byte order.
    873   1.89       dsl 	 * ATAPI NEC, Mitsumi and Pioneer drives and
    874   1.89       dsl 	 * old ATA TDK CompactFlash cards
    875   1.89       dsl 	 * have different byte order.
    876   1.89       dsl 	 */
    877   1.80     itohy #if BYTE_ORDER == BIG_ENDIAN
    878   1.80     itohy # define M(n)	prms->atap_model[(n) ^ 1]
    879   1.80     itohy #else
    880   1.80     itohy # define M(n)	prms->atap_model[n]
    881   1.80     itohy #endif
    882   1.89       dsl 	if (
    883   1.80     itohy #if BYTE_ORDER == BIG_ENDIAN
    884   1.89       dsl 	    !
    885   1.80     itohy #endif
    886  1.124    bouyer 	    ((drvp->drive_type == ATA_DRIVET_ATAPI) ?
    887   1.89       dsl 	     ((M(0) == 'N' && M(1) == 'E') ||
    888   1.89       dsl 	      (M(0) == 'F' && M(1) == 'X') ||
    889   1.89       dsl 	      (M(0) == 'P' && M(1) == 'i')) :
    890   1.93     pooka 	     ((M(0) == 'T' && M(1) == 'D' && M(2) == 'K')))) {
    891   1.93     pooka 		rv = CMD_OK;
    892   1.93     pooka 		goto out;
    893   1.93     pooka 	     }
    894   1.80     itohy #undef M
    895   1.89       dsl 	for (i = 0; i < sizeof(prms->atap_model); i += 2) {
    896  1.132      matt 		p = (uint16_t *)(prms->atap_model + i);
    897   1.89       dsl 		*p = bswap16(*p);
    898   1.89       dsl 	}
    899   1.89       dsl 	for (i = 0; i < sizeof(prms->atap_serial); i += 2) {
    900  1.132      matt 		p = (uint16_t *)(prms->atap_serial + i);
    901   1.89       dsl 		*p = bswap16(*p);
    902   1.89       dsl 	}
    903   1.89       dsl 	for (i = 0; i < sizeof(prms->atap_revision); i += 2) {
    904  1.132      matt 		p = (uint16_t *)(prms->atap_revision + i);
    905   1.89       dsl 		*p = bswap16(*p);
    906   1.89       dsl 	}
    907   1.80     itohy 
    908   1.93     pooka 	rv = CMD_OK;
    909   1.93     pooka  out:
    910  1.133  jdolecek 	kmem_free(tb, ATA_BSIZE);
    911  1.133  jdolecek 	ata_free_xfer(chp, xfer);
    912   1.93     pooka 	return rv;
    913    1.2    bouyer }
    914    1.2    bouyer 
    915    1.2    bouyer int
    916  1.132      matt ata_set_mode(struct ata_drive_datas *drvp, uint8_t mode, uint8_t flags)
    917    1.2    bouyer {
    918  1.133  jdolecek 	struct ata_xfer *xfer;
    919  1.133  jdolecek 	int rv;
    920   1.50   thorpej 	struct ata_channel *chp = drvp->chnl_softc;
    921   1.50   thorpej 	struct atac_softc *atac = chp->ch_atac;
    922    1.2    bouyer 
    923   1.47   thorpej 	ATADEBUG_PRINT(("ata_set_mode=0x%x\n", mode), DEBUG_FUNCS);
    924    1.2    bouyer 
    925  1.142  jdolecek 	xfer = ata_get_xfer(chp, false);
    926  1.133  jdolecek 	if (xfer == NULL) {
    927  1.133  jdolecek 		ATADEBUG_PRINT(("%s: no xfer\n", __func__),
    928  1.133  jdolecek 		    DEBUG_FUNCS|DEBUG_PROBE);
    929  1.133  jdolecek 		return CMD_AGAIN;
    930  1.133  jdolecek 	}
    931  1.133  jdolecek 
    932  1.133  jdolecek 	xfer->c_ata_c.r_command = SET_FEATURES;
    933  1.133  jdolecek 	xfer->c_ata_c.r_st_bmask = 0;
    934  1.133  jdolecek 	xfer->c_ata_c.r_st_pmask = 0;
    935  1.133  jdolecek 	xfer->c_ata_c.r_features = WDSF_SET_MODE;
    936  1.133  jdolecek 	xfer->c_ata_c.r_count = mode;
    937  1.133  jdolecek 	xfer->c_ata_c.flags = flags;
    938  1.133  jdolecek 	xfer->c_ata_c.timeout = 1000; /* 1s */
    939  1.155  jdolecek 	(*atac->atac_bustype_ata->ata_exec_command)(drvp, xfer);
    940  1.155  jdolecek 	ata_wait_cmd(chp, xfer);
    941  1.133  jdolecek 	if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    942  1.133  jdolecek 		rv = CMD_ERR;
    943  1.133  jdolecek 		goto out;
    944  1.133  jdolecek 	}
    945  1.133  jdolecek 
    946  1.133  jdolecek 	rv = CMD_OK;
    947  1.133  jdolecek 
    948  1.133  jdolecek out:
    949  1.133  jdolecek 	ata_free_xfer(chp, xfer);
    950  1.133  jdolecek 	return rv;
    951  1.133  jdolecek }
    952  1.133  jdolecek 
    953   1.78     itohy #if NATA_DMA
    954   1.11    bouyer void
    955   1.21   thorpej ata_dmaerr(struct ata_drive_datas *drvp, int flags)
    956   1.11    bouyer {
    957  1.146  jdolecek 	ata_channel_lock_owned(drvp->chnl_softc);
    958  1.146  jdolecek 
    959   1.11    bouyer 	/*
    960   1.11    bouyer 	 * Downgrade decision: if we get NERRS_MAX in NXFER.
    961   1.11    bouyer 	 * We start with n_dmaerrs set to NERRS_MAX-1 so that the
    962   1.11    bouyer 	 * first error within the first NXFER ops will immediatly trigger
    963   1.11    bouyer 	 * a downgrade.
    964   1.11    bouyer 	 * If we got an error and n_xfers is bigger than NXFER reset counters.
    965   1.11    bouyer 	 */
    966   1.11    bouyer 	drvp->n_dmaerrs++;
    967   1.11    bouyer 	if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) {
    968  1.159  jdolecek #ifdef ATA_DOWNGRADE_MODE
    969   1.39   thorpej 		ata_downgrade_mode(drvp, flags);
    970   1.11    bouyer 		drvp->n_dmaerrs = NERRS_MAX-1;
    971  1.159  jdolecek #else
    972  1.159  jdolecek 		static struct timeval last;
    973  1.159  jdolecek 		static const struct timeval serrintvl = { 300, 0 };
    974  1.159  jdolecek 
    975  1.159  jdolecek 		if (ratecheck(&last, &serrintvl)) {
    976  1.159  jdolecek 			aprint_error_dev(drvp->drv_softc,
    977  1.159  jdolecek 			    "excessive DMA errors - %d in last %d transfers\n",
    978  1.159  jdolecek 			    drvp->n_dmaerrs, drvp->n_xfers);
    979  1.159  jdolecek 		}
    980  1.159  jdolecek #endif
    981   1.11    bouyer 		drvp->n_xfers = 0;
    982   1.11    bouyer 		return;
    983   1.11    bouyer 	}
    984   1.11    bouyer 	if (drvp->n_xfers > NXFER) {
    985   1.11    bouyer 		drvp->n_dmaerrs = 1; /* just got an error */
    986   1.11    bouyer 		drvp->n_xfers = 1; /* restart counting from this error */
    987    1.4    bouyer 	}
    988    1.2    bouyer }
    989   1.78     itohy #endif	/* NATA_DMA */
    990   1.30    bouyer 
    991   1.44   thorpej /*
    992   1.68    bouyer  * freeze the queue and wait for the controller to be idle. Caller has to
    993   1.68    bouyer  * unfreeze/restart the queue
    994   1.68    bouyer  */
    995  1.133  jdolecek static void
    996  1.133  jdolecek ata_channel_idle(struct ata_channel *chp)
    997   1.68    bouyer {
    998  1.133  jdolecek 	ata_channel_lock(chp);
    999  1.133  jdolecek 	ata_channel_freeze_locked(chp);
   1000  1.133  jdolecek 	while (chp->ch_queue->queue_active > 0) {
   1001  1.133  jdolecek 		chp->ch_queue->queue_flags |= QF_IDLE_WAIT;
   1002  1.133  jdolecek 		cv_timedwait(&chp->ch_queue->queue_idle, &chp->ch_lock, 1);
   1003   1.68    bouyer 	}
   1004  1.133  jdolecek 	ata_channel_unlock(chp);
   1005   1.68    bouyer }
   1006   1.68    bouyer 
   1007   1.68    bouyer /*
   1008   1.57   thorpej  * Add a command to the queue and start controller.
   1009   1.57   thorpej  *
   1010   1.57   thorpej  * MUST BE CALLED AT splbio()!
   1011   1.44   thorpej  */
   1012   1.44   thorpej void
   1013   1.48   thorpej ata_exec_xfer(struct ata_channel *chp, struct ata_xfer *xfer)
   1014   1.44   thorpej {
   1015   1.44   thorpej 
   1016   1.47   thorpej 	ATADEBUG_PRINT(("ata_exec_xfer %p channel %d drive %d\n", xfer,
   1017   1.44   thorpej 	    chp->ch_channel, xfer->c_drive), DEBUG_XFERS);
   1018   1.44   thorpej 
   1019   1.44   thorpej 	/* complete xfer setup */
   1020   1.44   thorpej 	xfer->c_chp = chp;
   1021   1.44   thorpej 
   1022  1.133  jdolecek 	ata_channel_lock(chp);
   1023  1.133  jdolecek 
   1024  1.133  jdolecek 	/*
   1025  1.133  jdolecek 	 * Standard commands are added to the end of command list, but
   1026  1.133  jdolecek 	 * recovery commands must be run immediatelly.
   1027  1.133  jdolecek 	 */
   1028  1.142  jdolecek 	if ((xfer->c_flags & C_SKIP_QUEUE) == 0)
   1029  1.142  jdolecek 		SIMPLEQ_INSERT_TAIL(&chp->ch_queue->queue_xfer, xfer,
   1030  1.133  jdolecek 		    c_xferchain);
   1031  1.133  jdolecek 	else
   1032  1.142  jdolecek 		SIMPLEQ_INSERT_HEAD(&chp->ch_queue->queue_xfer, xfer,
   1033  1.133  jdolecek 		    c_xferchain);
   1034  1.133  jdolecek 
   1035   1.62    bouyer 	/*
   1036   1.62    bouyer 	 * if polling and can sleep, wait for the xfer to be at head of queue
   1037   1.62    bouyer 	 */
   1038   1.62    bouyer 	if ((xfer->c_flags & (C_POLL | C_WAIT)) ==  (C_POLL | C_WAIT)) {
   1039  1.133  jdolecek 		while (chp->ch_queue->queue_active > 0 ||
   1040  1.142  jdolecek 		    SIMPLEQ_FIRST(&chp->ch_queue->queue_xfer) != xfer) {
   1041   1.62    bouyer 			xfer->c_flags |= C_WAITACT;
   1042  1.142  jdolecek 			cv_wait(&chp->ch_queue->c_active, &chp->ch_lock);
   1043   1.62    bouyer 			xfer->c_flags &= ~C_WAITACT;
   1044  1.142  jdolecek 		}
   1045  1.133  jdolecek 
   1046  1.142  jdolecek 		/*
   1047  1.142  jdolecek 		 * Free xfer now if it there was attempt to free it
   1048  1.142  jdolecek 		 * while we were waiting.
   1049  1.142  jdolecek 		 */
   1050  1.142  jdolecek 		if ((xfer->c_flags & (C_FREE|C_WAITTIMO)) == C_FREE) {
   1051  1.142  jdolecek 			ata_channel_unlock(chp);
   1052  1.133  jdolecek 
   1053  1.142  jdolecek 			ata_free_xfer(chp, xfer);
   1054  1.142  jdolecek 			return;
   1055   1.62    bouyer 		}
   1056   1.62    bouyer 	}
   1057  1.133  jdolecek 
   1058  1.133  jdolecek 	ata_channel_unlock(chp);
   1059  1.133  jdolecek 
   1060  1.133  jdolecek 	ATADEBUG_PRINT(("atastart from ata_exec_xfer, flags 0x%x\n",
   1061  1.133  jdolecek 	    chp->ch_flags), DEBUG_XFERS);
   1062   1.45   thorpej 	atastart(chp);
   1063   1.45   thorpej }
   1064   1.45   thorpej 
   1065   1.45   thorpej /*
   1066   1.45   thorpej  * Start I/O on a controller, for the given channel.
   1067   1.45   thorpej  * The first xfer may be not for our channel if the channel queues
   1068   1.45   thorpej  * are shared.
   1069   1.57   thorpej  *
   1070   1.57   thorpej  * MUST BE CALLED AT splbio()!
   1071  1.144  jdolecek  *
   1072  1.144  jdolecek  * XXX FIS-based switching with PMP
   1073  1.144  jdolecek  * Currently atastart() never schedules concurrent NCQ transfers to more than
   1074  1.144  jdolecek  * one drive, even when channel has several SATA drives attached via PMP.
   1075  1.144  jdolecek  * To support concurrent transfers to different drives with PMP, it would be
   1076  1.144  jdolecek  * necessary to implement FIS-based switching support in controller driver,
   1077  1.144  jdolecek  * and then adjust error handling and recovery to stop assuming at most
   1078  1.144  jdolecek  * one active drive.
   1079   1.45   thorpej  */
   1080   1.45   thorpej void
   1081   1.48   thorpej atastart(struct ata_channel *chp)
   1082   1.45   thorpej {
   1083   1.49   thorpej 	struct atac_softc *atac = chp->ch_atac;
   1084  1.133  jdolecek 	struct ata_queue *chq = chp->ch_queue;
   1085  1.133  jdolecek 	struct ata_xfer *xfer, *axfer;
   1086  1.142  jdolecek 	bool skipq;
   1087   1.45   thorpej 
   1088   1.56   thorpej #ifdef ATA_DEBUG
   1089   1.45   thorpej 	int spl1, spl2;
   1090   1.45   thorpej 
   1091   1.45   thorpej 	spl1 = splbio();
   1092   1.45   thorpej 	spl2 = splbio();
   1093   1.45   thorpej 	if (spl2 != spl1) {
   1094   1.45   thorpej 		printf("atastart: not at splbio()\n");
   1095   1.45   thorpej 		panic("atastart");
   1096   1.45   thorpej 	}
   1097   1.45   thorpej 	splx(spl2);
   1098   1.45   thorpej 	splx(spl1);
   1099   1.56   thorpej #endif /* ATA_DEBUG */
   1100   1.45   thorpej 
   1101  1.133  jdolecek 	ata_channel_lock(chp);
   1102  1.133  jdolecek 
   1103  1.133  jdolecek again:
   1104  1.142  jdolecek 	/* is there a xfer ? */
   1105  1.142  jdolecek 	if ((xfer = SIMPLEQ_FIRST(&chp->ch_queue->queue_xfer)) == NULL) {
   1106  1.142  jdolecek 		ATADEBUG_PRINT(("%s(chp=%p): channel %d queue_xfer is empty\n",
   1107  1.139  jdolecek 		    __func__, chp, chp->ch_channel), DEBUG_XFERS);
   1108  1.137  jdolecek 		goto out;
   1109  1.133  jdolecek 	}
   1110  1.133  jdolecek 
   1111  1.142  jdolecek 	/*
   1112  1.142  jdolecek 	 * if someone is waiting for the command to be active, wake it up
   1113  1.142  jdolecek 	 * and let it process the command
   1114  1.142  jdolecek 	 */
   1115  1.142  jdolecek 	if (__predict_false(xfer->c_flags & C_WAITACT)) {
   1116  1.142  jdolecek 		ATADEBUG_PRINT(("atastart: xfer %p channel %d drive %d "
   1117  1.142  jdolecek 		    "wait active\n", xfer, chp->ch_channel, xfer->c_drive),
   1118  1.142  jdolecek 		    DEBUG_XFERS);
   1119  1.142  jdolecek 		cv_broadcast(&chp->ch_queue->c_active);
   1120  1.133  jdolecek 		goto out;
   1121  1.137  jdolecek 	}
   1122   1.45   thorpej 
   1123  1.142  jdolecek 	skipq = ISSET(xfer->c_flags, C_SKIP_QUEUE);
   1124   1.45   thorpej 
   1125  1.133  jdolecek 	/* is the queue frozen? */
   1126  1.142  jdolecek 	if (__predict_false(!skipq && chq->queue_freeze > 0)) {
   1127  1.133  jdolecek 		if (chq->queue_flags & QF_IDLE_WAIT) {
   1128  1.133  jdolecek 			chq->queue_flags &= ~QF_IDLE_WAIT;
   1129  1.133  jdolecek 			cv_signal(&chp->ch_queue->queue_idle);
   1130  1.133  jdolecek 		}
   1131  1.137  jdolecek 		ATADEBUG_PRINT(("%s(chp=%p): channel %d drive %d "
   1132  1.142  jdolecek 		    "queue frozen: %d\n",
   1133  1.137  jdolecek 		    __func__, chp, chp->ch_channel, xfer->c_drive,
   1134  1.142  jdolecek 		    chq->queue_freeze),
   1135  1.137  jdolecek 		    DEBUG_XFERS);
   1136  1.137  jdolecek 		goto out;
   1137   1.45   thorpej 	}
   1138  1.133  jdolecek 
   1139  1.133  jdolecek 	/* all xfers on same queue must belong to the same channel */
   1140  1.133  jdolecek 	KASSERT(xfer->c_chp == chp);
   1141  1.133  jdolecek 
   1142  1.133  jdolecek 	/*
   1143  1.133  jdolecek 	 * Can only take the command if there are no current active
   1144  1.133  jdolecek 	 * commands, or if the command is NCQ and the active commands are also
   1145  1.133  jdolecek 	 * NCQ. If PM is in use and HBA driver doesn't support/use FIS-based
   1146  1.133  jdolecek 	 * switching, can only send commands to single drive.
   1147  1.133  jdolecek 	 * Need only check first xfer.
   1148  1.133  jdolecek 	 * XXX FIS-based switching - revisit
   1149  1.133  jdolecek 	 */
   1150  1.142  jdolecek 	if (!skipq && (axfer = TAILQ_FIRST(&chp->ch_queue->active_xfers))) {
   1151  1.133  jdolecek 		if (!ISSET(xfer->c_flags, C_NCQ) ||
   1152  1.133  jdolecek 		    !ISSET(axfer->c_flags, C_NCQ) ||
   1153  1.133  jdolecek 		    xfer->c_drive != axfer->c_drive)
   1154  1.133  jdolecek 			goto out;
   1155   1.45   thorpej 	}
   1156  1.133  jdolecek 
   1157  1.133  jdolecek 	struct ata_drive_datas * const drvp = &chp->ch_drive[xfer->c_drive];
   1158  1.133  jdolecek 
   1159   1.62    bouyer 	/*
   1160  1.142  jdolecek 	 * Are we on limit of active xfers ? If the queue has more
   1161  1.142  jdolecek 	 * than 1 openings, we keep one slot reserved for recovery or dump.
   1162   1.62    bouyer 	 */
   1163  1.142  jdolecek 	KASSERT(chq->queue_active <= chq->queue_openings);
   1164  1.142  jdolecek 	const uint8_t chq_openings = (!skipq && chq->queue_openings > 1)
   1165  1.142  jdolecek 	    ? (chq->queue_openings - 1) : chq->queue_openings;
   1166  1.142  jdolecek 	const uint8_t drv_openings = ISSET(xfer->c_flags, C_NCQ)
   1167  1.142  jdolecek 	    ? drvp->drv_openings : ATA_MAX_OPENINGS;
   1168  1.142  jdolecek 	if (chq->queue_active >= MIN(chq_openings, drv_openings)) {
   1169  1.142  jdolecek 		if (skipq) {
   1170  1.142  jdolecek 			panic("%s: channel %d busy, xfer not possible",
   1171  1.142  jdolecek 			    __func__, chp->ch_channel);
   1172  1.142  jdolecek 		}
   1173  1.142  jdolecek 
   1174  1.142  jdolecek 		ATADEBUG_PRINT(("%s(chp=%p): channel %d completely busy\n",
   1175  1.142  jdolecek 		    __func__, chp, chp->ch_channel), DEBUG_XFERS);
   1176  1.133  jdolecek 		goto out;
   1177   1.62    bouyer 	}
   1178  1.133  jdolecek 
   1179  1.142  jdolecek 	/* Slot allocation can fail if drv_openings < ch_openings */
   1180  1.142  jdolecek 	if (!ata_queue_alloc_slot(chp, &xfer->c_slot, drv_openings))
   1181  1.142  jdolecek 		goto out;
   1182  1.142  jdolecek 
   1183  1.142  jdolecek 	if (__predict_false(atac->atac_claim_hw)) {
   1184  1.142  jdolecek 		if (!atac->atac_claim_hw(chp, 0)) {
   1185  1.142  jdolecek 			ata_queue_free_slot(chp, xfer->c_slot);
   1186  1.133  jdolecek 			goto out;
   1187  1.142  jdolecek 		}
   1188  1.142  jdolecek 	}
   1189  1.142  jdolecek 
   1190  1.142  jdolecek 	/* Now committed to start the xfer */
   1191   1.45   thorpej 
   1192  1.137  jdolecek 	ATADEBUG_PRINT(("%s(chp=%p): xfer %p channel %d drive %d\n",
   1193  1.137  jdolecek 	    __func__, chp, xfer, chp->ch_channel, xfer->c_drive), DEBUG_XFERS);
   1194  1.133  jdolecek 	if (drvp->drive_flags & ATA_DRIVE_RESET) {
   1195  1.133  jdolecek 		drvp->drive_flags &= ~ATA_DRIVE_RESET;
   1196  1.133  jdolecek 		drvp->state = 0;
   1197   1.45   thorpej 	}
   1198  1.133  jdolecek 
   1199  1.133  jdolecek 	if (ISSET(xfer->c_flags, C_NCQ))
   1200  1.133  jdolecek 		SET(chp->ch_flags, ATACH_NCQ);
   1201  1.133  jdolecek 	else
   1202  1.133  jdolecek 		CLR(chp->ch_flags, ATACH_NCQ);
   1203  1.133  jdolecek 
   1204  1.142  jdolecek 	SIMPLEQ_REMOVE_HEAD(&chq->queue_xfer, c_xferchain);
   1205  1.142  jdolecek 
   1206  1.133  jdolecek 	ata_activate_xfer_locked(chp, xfer);
   1207   1.65     perry 
   1208   1.49   thorpej 	if (atac->atac_cap & ATAC_CAP_NOIRQ)
   1209   1.45   thorpej 		KASSERT(xfer->c_flags & C_POLL);
   1210   1.62    bouyer 
   1211  1.133  jdolecek 	switch (ata_xfer_start(xfer)) {
   1212  1.133  jdolecek 	case ATASTART_TH:
   1213  1.133  jdolecek 	case ATASTART_ABORT:
   1214  1.133  jdolecek 		/* don't start any further commands in this case */
   1215  1.133  jdolecek 		goto out;
   1216  1.133  jdolecek 	default:
   1217  1.133  jdolecek 		/* nothing to do */
   1218  1.133  jdolecek 		break;
   1219  1.133  jdolecek 	}
   1220  1.133  jdolecek 
   1221  1.142  jdolecek 	/* Queue more commands if possible, but not during recovery or dump */
   1222  1.142  jdolecek 	if (!skipq && chq->queue_active < chq->queue_openings)
   1223  1.133  jdolecek 		goto again;
   1224  1.133  jdolecek 
   1225  1.133  jdolecek out:
   1226  1.133  jdolecek 	ata_channel_unlock(chp);
   1227   1.44   thorpej }
   1228   1.44   thorpej 
   1229  1.133  jdolecek int
   1230  1.133  jdolecek ata_xfer_start(struct ata_xfer *xfer)
   1231  1.133  jdolecek {
   1232  1.133  jdolecek 	struct ata_channel *chp = xfer->c_chp;
   1233  1.133  jdolecek 	int rv;
   1234  1.133  jdolecek 
   1235  1.133  jdolecek 	KASSERT(mutex_owned(&chp->ch_lock));
   1236  1.133  jdolecek 
   1237  1.142  jdolecek 	rv = xfer->ops->c_start(chp, xfer);
   1238  1.133  jdolecek 	switch (rv) {
   1239  1.133  jdolecek 	case ATASTART_STARTED:
   1240  1.133  jdolecek 		/* nothing to do */
   1241  1.133  jdolecek 		break;
   1242  1.133  jdolecek 	case ATASTART_TH:
   1243  1.133  jdolecek 		/* postpone xfer to thread */
   1244  1.133  jdolecek 		ata_thread_wake_locked(chp);
   1245  1.133  jdolecek 		break;
   1246  1.133  jdolecek 	case ATASTART_POLL:
   1247  1.133  jdolecek 		/* can happen even in thread context for some ATAPI devices */
   1248  1.133  jdolecek 		ata_channel_unlock(chp);
   1249  1.142  jdolecek 		KASSERT(xfer->ops != NULL && xfer->ops->c_poll != NULL);
   1250  1.142  jdolecek 		xfer->ops->c_poll(chp, xfer);
   1251  1.133  jdolecek 		ata_channel_lock(chp);
   1252  1.133  jdolecek 		break;
   1253  1.133  jdolecek 	case ATASTART_ABORT:
   1254  1.133  jdolecek 		ata_channel_unlock(chp);
   1255  1.142  jdolecek 		KASSERT(xfer->ops != NULL && xfer->ops->c_abort != NULL);
   1256  1.142  jdolecek 		xfer->ops->c_abort(chp, xfer);
   1257  1.133  jdolecek 		ata_channel_lock(chp);
   1258  1.133  jdolecek 		break;
   1259  1.133  jdolecek 	}
   1260  1.133  jdolecek 
   1261  1.133  jdolecek 	return rv;
   1262  1.133  jdolecek }
   1263  1.133  jdolecek 
   1264  1.133  jdolecek static void
   1265  1.133  jdolecek ata_activate_xfer_locked(struct ata_channel *chp, struct ata_xfer *xfer)
   1266  1.133  jdolecek {
   1267  1.133  jdolecek 	struct ata_queue * const chq = chp->ch_queue;
   1268  1.133  jdolecek 
   1269  1.133  jdolecek 	KASSERT(mutex_owned(&chp->ch_lock));
   1270  1.133  jdolecek 	KASSERT((chq->active_xfers_used & __BIT(xfer->c_slot)) == 0);
   1271  1.133  jdolecek 
   1272  1.142  jdolecek 	if ((xfer->c_flags & C_SKIP_QUEUE) == 0)
   1273  1.133  jdolecek 		TAILQ_INSERT_TAIL(&chq->active_xfers, xfer, c_activechain);
   1274  1.133  jdolecek 	else {
   1275  1.133  jdolecek 		/*
   1276  1.133  jdolecek 		 * Must go to head, so that ata_queue_get_active_xfer()
   1277  1.133  jdolecek 		 * returns the recovery command, and not some other
   1278  1.133  jdolecek 		 * random active transfer.
   1279  1.133  jdolecek 		 */
   1280  1.133  jdolecek 		TAILQ_INSERT_HEAD(&chq->active_xfers, xfer, c_activechain);
   1281  1.133  jdolecek 	}
   1282  1.133  jdolecek 	chq->active_xfers_used |= __BIT(xfer->c_slot);
   1283  1.133  jdolecek 	chq->queue_active++;
   1284  1.133  jdolecek }
   1285  1.133  jdolecek 
   1286  1.142  jdolecek /*
   1287  1.142  jdolecek  * Does it's own locking, does not require splbio().
   1288  1.142  jdolecek  * flags - whether to block waiting for free xfer
   1289  1.142  jdolecek  */
   1290  1.142  jdolecek struct ata_xfer *
   1291  1.142  jdolecek ata_get_xfer(struct ata_channel *chp, bool waitok)
   1292  1.142  jdolecek {
   1293  1.149  christos 	return pool_get(&ata_xfer_pool,
   1294  1.149  christos 	    PR_ZERO | (waitok ? PR_WAITOK : PR_NOWAIT));
   1295  1.142  jdolecek }
   1296  1.142  jdolecek 
   1297  1.142  jdolecek /*
   1298  1.142  jdolecek  * ata_deactivate_xfer() must be always called prior to ata_free_xfer()
   1299  1.142  jdolecek  */
   1300  1.142  jdolecek void
   1301  1.142  jdolecek ata_free_xfer(struct ata_channel *chp, struct ata_xfer *xfer)
   1302  1.142  jdolecek {
   1303  1.142  jdolecek 	struct ata_queue *chq = chp->ch_queue;
   1304  1.142  jdolecek 
   1305  1.142  jdolecek 	ata_channel_lock(chp);
   1306  1.142  jdolecek 
   1307  1.142  jdolecek 	if (__predict_false(xfer->c_flags & (C_WAITACT|C_WAITTIMO))) {
   1308  1.142  jdolecek 		/* Someone is waiting for this xfer, so we can't free now */
   1309  1.142  jdolecek 		xfer->c_flags |= C_FREE;
   1310  1.142  jdolecek 		cv_broadcast(&chq->c_active);
   1311  1.142  jdolecek 		ata_channel_unlock(chp);
   1312  1.142  jdolecek 		return;
   1313  1.142  jdolecek 	}
   1314  1.142  jdolecek 
   1315  1.142  jdolecek 	/* XXX move PIOBM and free_gw to deactivate? */
   1316  1.142  jdolecek #if NATA_PIOBM		/* XXX wdc dependent code */
   1317  1.142  jdolecek 	if (__predict_false(xfer->c_flags & C_PIOBM)) {
   1318  1.142  jdolecek 		struct wdc_softc *wdc = CHAN_TO_WDC(chp);
   1319  1.142  jdolecek 
   1320  1.142  jdolecek 		/* finish the busmastering PIO */
   1321  1.142  jdolecek 		(*wdc->piobm_done)(wdc->dma_arg,
   1322  1.142  jdolecek 		    chp->ch_channel, xfer->c_drive);
   1323  1.142  jdolecek 		chp->ch_flags &= ~(ATACH_DMA_WAIT | ATACH_PIOBM_WAIT | ATACH_IRQ_WAIT);
   1324  1.142  jdolecek 	}
   1325  1.142  jdolecek #endif
   1326  1.142  jdolecek 
   1327  1.142  jdolecek 	if (__predict_false(chp->ch_atac->atac_free_hw))
   1328  1.142  jdolecek 		chp->ch_atac->atac_free_hw(chp);
   1329  1.142  jdolecek 
   1330  1.142  jdolecek 	ata_channel_unlock(chp);
   1331  1.142  jdolecek 
   1332  1.142  jdolecek 	if (__predict_true(!ISSET(xfer->c_flags, C_PRIVATE_ALLOC)))
   1333  1.142  jdolecek 		pool_put(&ata_xfer_pool, xfer);
   1334  1.142  jdolecek }
   1335  1.142  jdolecek 
   1336  1.133  jdolecek void
   1337  1.133  jdolecek ata_deactivate_xfer(struct ata_channel *chp, struct ata_xfer *xfer)
   1338  1.133  jdolecek {
   1339  1.133  jdolecek 	struct ata_queue * const chq = chp->ch_queue;
   1340  1.133  jdolecek 
   1341  1.133  jdolecek 	ata_channel_lock(chp);
   1342  1.133  jdolecek 
   1343  1.133  jdolecek 	KASSERT(chq->queue_active > 0);
   1344  1.133  jdolecek 	KASSERT((chq->active_xfers_used & __BIT(xfer->c_slot)) != 0);
   1345  1.133  jdolecek 
   1346  1.142  jdolecek 	/* Stop only when this is last active xfer */
   1347  1.142  jdolecek 	if (chq->queue_active == 1)
   1348  1.142  jdolecek 		callout_stop(&chp->c_timo_callout);
   1349  1.133  jdolecek 
   1350  1.142  jdolecek 	if (callout_invoking(&chp->c_timo_callout))
   1351  1.133  jdolecek 		xfer->c_flags |= C_WAITTIMO;
   1352  1.133  jdolecek 
   1353  1.133  jdolecek 	TAILQ_REMOVE(&chq->active_xfers, xfer, c_activechain);
   1354  1.133  jdolecek 	chq->active_xfers_used &= ~__BIT(xfer->c_slot);
   1355  1.133  jdolecek 	chq->queue_active--;
   1356  1.133  jdolecek 
   1357  1.142  jdolecek 	ata_queue_free_slot(chp, xfer->c_slot);
   1358  1.142  jdolecek 
   1359  1.142  jdolecek 	if (xfer->c_flags & C_WAIT)
   1360  1.142  jdolecek 		cv_broadcast(&chq->c_cmd_finish);
   1361  1.142  jdolecek 
   1362  1.133  jdolecek 	ata_channel_unlock(chp);
   1363  1.133  jdolecek }
   1364  1.133  jdolecek 
   1365  1.133  jdolecek /*
   1366  1.133  jdolecek  * Called in c_intr hook. Must be called before before any deactivations
   1367  1.133  jdolecek  * are done - if there is drain pending, it calls c_kill_xfer hook which
   1368  1.133  jdolecek  * deactivates the xfer.
   1369  1.133  jdolecek  * Calls c_kill_xfer with channel lock free.
   1370  1.133  jdolecek  * Returns true if caller should just exit without further processing.
   1371  1.133  jdolecek  * Caller must not further access any part of xfer or any related controller
   1372  1.133  jdolecek  * structures in that case, it should just return.
   1373  1.133  jdolecek  */
   1374  1.133  jdolecek bool
   1375  1.133  jdolecek ata_waitdrain_xfer_check(struct ata_channel *chp, struct ata_xfer *xfer)
   1376  1.133  jdolecek {
   1377  1.133  jdolecek 	int drive = xfer->c_drive;
   1378  1.133  jdolecek 	bool draining = false;
   1379  1.133  jdolecek 
   1380  1.133  jdolecek 	ata_channel_lock(chp);
   1381  1.133  jdolecek 
   1382  1.133  jdolecek 	if (chp->ch_drive[drive].drive_flags & ATA_DRIVE_WAITDRAIN) {
   1383  1.133  jdolecek 		ata_channel_unlock(chp);
   1384  1.133  jdolecek 
   1385  1.142  jdolecek 		xfer->ops->c_kill_xfer(chp, xfer, KILL_GONE);
   1386  1.133  jdolecek 
   1387  1.133  jdolecek 		ata_channel_lock(chp);
   1388  1.133  jdolecek 		chp->ch_drive[drive].drive_flags &= ~ATA_DRIVE_WAITDRAIN;
   1389  1.133  jdolecek 		cv_signal(&chp->ch_queue->queue_drain);
   1390  1.133  jdolecek 		draining = true;
   1391  1.133  jdolecek 	}
   1392  1.133  jdolecek 
   1393  1.133  jdolecek 	ata_channel_unlock(chp);
   1394  1.133  jdolecek 
   1395  1.133  jdolecek 	return draining;
   1396  1.133  jdolecek }
   1397  1.133  jdolecek 
   1398  1.133  jdolecek /*
   1399  1.133  jdolecek  * Check for race of normal transfer handling vs. timeout.
   1400  1.133  jdolecek  */
   1401  1.133  jdolecek bool
   1402  1.133  jdolecek ata_timo_xfer_check(struct ata_xfer *xfer)
   1403  1.133  jdolecek {
   1404  1.133  jdolecek 	struct ata_channel *chp = xfer->c_chp;
   1405  1.133  jdolecek 	struct ata_drive_datas *drvp = &chp->ch_drive[xfer->c_drive];
   1406  1.133  jdolecek 
   1407  1.133  jdolecek 	ata_channel_lock(chp);
   1408  1.133  jdolecek 
   1409  1.133  jdolecek 	if (xfer->c_flags & C_WAITTIMO) {
   1410  1.133  jdolecek 		xfer->c_flags &= ~C_WAITTIMO;
   1411  1.133  jdolecek 
   1412  1.133  jdolecek 		/* Handle race vs. ata_free_xfer() */
   1413  1.133  jdolecek 		if (xfer->c_flags & C_FREE) {
   1414  1.133  jdolecek 			xfer->c_flags &= ~C_FREE;
   1415  1.133  jdolecek 			ata_channel_unlock(chp);
   1416  1.133  jdolecek 
   1417  1.142  jdolecek 	    		device_printf(drvp->drv_softc,
   1418  1.142  jdolecek 			    "xfer %"PRIxPTR" freed while invoking timeout\n",
   1419  1.142  jdolecek 			    (intptr_t)xfer & PAGE_MASK);
   1420  1.133  jdolecek 
   1421  1.133  jdolecek 			ata_free_xfer(chp, xfer);
   1422  1.133  jdolecek 			return true;
   1423  1.133  jdolecek 		}
   1424  1.133  jdolecek 
   1425  1.133  jdolecek 		/* Race vs. callout_stop() in ata_deactivate_xfer() */
   1426  1.133  jdolecek 		ata_channel_unlock(chp);
   1427  1.133  jdolecek 
   1428  1.142  jdolecek 	    	device_printf(drvp->drv_softc,
   1429  1.142  jdolecek 		    "xfer %"PRIxPTR" deactivated while invoking timeout\n",
   1430  1.142  jdolecek 		    (intptr_t)xfer & PAGE_MASK);
   1431  1.133  jdolecek 		return true;
   1432  1.133  jdolecek 	}
   1433  1.133  jdolecek 
   1434  1.133  jdolecek 	ata_channel_unlock(chp);
   1435  1.133  jdolecek 
   1436  1.133  jdolecek 	/* No race, proceed with timeout handling */
   1437  1.133  jdolecek 	return false;
   1438  1.133  jdolecek }
   1439  1.133  jdolecek 
   1440   1.42   thorpej /*
   1441  1.133  jdolecek  * Kill off all active xfers for a ata_channel.
   1442   1.42   thorpej  *
   1443  1.133  jdolecek  * Must be called with channel lock held.
   1444  1.133  jdolecek  */
   1445  1.133  jdolecek void
   1446  1.133  jdolecek ata_kill_active(struct ata_channel *chp, int reason, int flags)
   1447  1.133  jdolecek {
   1448  1.133  jdolecek 	struct ata_queue * const chq = chp->ch_queue;
   1449  1.133  jdolecek 	struct ata_xfer *xfer, *xfernext;
   1450  1.133  jdolecek 
   1451  1.133  jdolecek 	KASSERT(mutex_owned(&chp->ch_lock));
   1452  1.133  jdolecek 
   1453  1.133  jdolecek 	TAILQ_FOREACH_SAFE(xfer, &chq->active_xfers, c_activechain, xfernext) {
   1454  1.145  jdolecek 		ata_channel_unlock(chp);
   1455  1.142  jdolecek 		xfer->ops->c_kill_xfer(xfer->c_chp, xfer, reason);
   1456  1.145  jdolecek 		ata_channel_lock(chp);
   1457  1.133  jdolecek 	}
   1458  1.133  jdolecek }
   1459  1.133  jdolecek 
   1460  1.133  jdolecek /*
   1461  1.133  jdolecek  * Kill off all pending xfers for a drive.
   1462   1.42   thorpej  */
   1463   1.42   thorpej void
   1464   1.42   thorpej ata_kill_pending(struct ata_drive_datas *drvp)
   1465   1.42   thorpej {
   1466  1.133  jdolecek 	struct ata_channel * const chp = drvp->chnl_softc;
   1467  1.133  jdolecek 	struct ata_queue * const chq = chp->ch_queue;
   1468  1.142  jdolecek 	struct ata_xfer *xfer;
   1469  1.133  jdolecek 
   1470  1.133  jdolecek 	ata_channel_lock(chp);
   1471  1.133  jdolecek 
   1472  1.133  jdolecek 	/* Kill all pending transfers */
   1473  1.142  jdolecek 	while ((xfer = SIMPLEQ_FIRST(&chq->queue_xfer))) {
   1474  1.133  jdolecek 		KASSERT(xfer->c_chp == chp);
   1475   1.42   thorpej 
   1476  1.133  jdolecek 		if (xfer->c_drive != drvp->drive)
   1477   1.42   thorpej 			continue;
   1478  1.133  jdolecek 
   1479  1.142  jdolecek 		SIMPLEQ_REMOVE_HEAD(&chp->ch_queue->queue_xfer, c_xferchain);
   1480  1.133  jdolecek 
   1481  1.133  jdolecek 		/*
   1482  1.133  jdolecek 		 * Keep the lock, so that we get deadlock (and 'locking against
   1483  1.133  jdolecek 		 * myself' with LOCKDEBUG), instead of silent
   1484  1.133  jdolecek 		 * data corruption, if the hook tries to call back into
   1485  1.133  jdolecek 		 * middle layer for inactive xfer.
   1486  1.133  jdolecek 		 */
   1487  1.142  jdolecek 		xfer->ops->c_kill_xfer(chp, xfer, KILL_GONE_INACTIVE);
   1488   1.42   thorpej 	}
   1489   1.42   thorpej 
   1490  1.133  jdolecek 	/* Wait until all active transfers on the drive finish */
   1491  1.133  jdolecek 	while (chq->queue_active > 0) {
   1492  1.133  jdolecek 		bool drv_active = false;
   1493  1.133  jdolecek 
   1494  1.133  jdolecek 		TAILQ_FOREACH(xfer, &chq->active_xfers, c_activechain) {
   1495  1.133  jdolecek 			KASSERT(xfer->c_chp == chp);
   1496  1.133  jdolecek 
   1497  1.133  jdolecek 			if (xfer->c_drive == drvp->drive) {
   1498  1.133  jdolecek 				drv_active = true;
   1499  1.133  jdolecek 				break;
   1500  1.133  jdolecek 			}
   1501  1.133  jdolecek 		}
   1502  1.133  jdolecek 
   1503  1.133  jdolecek 		if (!drv_active) {
   1504  1.133  jdolecek 			/* all finished */
   1505   1.42   thorpej 			break;
   1506   1.42   thorpej 		}
   1507  1.133  jdolecek 
   1508  1.133  jdolecek 		drvp->drive_flags |= ATA_DRIVE_WAITDRAIN;
   1509  1.133  jdolecek 		cv_wait(&chq->queue_drain, &chp->ch_lock);
   1510   1.42   thorpej 	}
   1511  1.133  jdolecek 
   1512  1.133  jdolecek 	ata_channel_unlock(chp);
   1513  1.133  jdolecek }
   1514  1.133  jdolecek 
   1515  1.133  jdolecek static void
   1516  1.133  jdolecek ata_channel_freeze_locked(struct ata_channel *chp)
   1517  1.133  jdolecek {
   1518  1.133  jdolecek 	chp->ch_queue->queue_freeze++;
   1519  1.137  jdolecek 
   1520  1.137  jdolecek 	ATADEBUG_PRINT(("%s(chp=%p) -> %d\n", __func__, chp,
   1521  1.137  jdolecek 	    chp->ch_queue->queue_freeze), DEBUG_FUNCS | DEBUG_XFERS);
   1522  1.133  jdolecek }
   1523  1.133  jdolecek 
   1524  1.133  jdolecek void
   1525  1.133  jdolecek ata_channel_freeze(struct ata_channel *chp)
   1526  1.133  jdolecek {
   1527  1.133  jdolecek 	ata_channel_lock(chp);
   1528  1.133  jdolecek 	ata_channel_freeze_locked(chp);
   1529  1.133  jdolecek 	ata_channel_unlock(chp);
   1530  1.133  jdolecek }
   1531  1.133  jdolecek 
   1532  1.142  jdolecek void
   1533  1.133  jdolecek ata_channel_thaw_locked(struct ata_channel *chp)
   1534  1.133  jdolecek {
   1535  1.133  jdolecek 	KASSERT(mutex_owned(&chp->ch_lock));
   1536  1.137  jdolecek 	KASSERT(chp->ch_queue->queue_freeze > 0);
   1537  1.133  jdolecek 
   1538  1.133  jdolecek 	chp->ch_queue->queue_freeze--;
   1539  1.137  jdolecek 
   1540  1.137  jdolecek 	ATADEBUG_PRINT(("%s(chp=%p) -> %d\n", __func__, chp,
   1541  1.137  jdolecek 	    chp->ch_queue->queue_freeze), DEBUG_FUNCS | DEBUG_XFERS);
   1542  1.133  jdolecek }
   1543  1.133  jdolecek 
   1544   1.55   thorpej /*
   1545  1.142  jdolecek  * ata_thread_run:
   1546   1.57   thorpej  *
   1547  1.142  jdolecek  *	Reset and ATA channel. Channel lock must be held. arg is type-specific.
   1548   1.55   thorpej  */
   1549   1.55   thorpej void
   1550  1.142  jdolecek ata_thread_run(struct ata_channel *chp, int flags, int type, int arg)
   1551   1.55   thorpej {
   1552   1.55   thorpej 	struct atac_softc *atac = chp->ch_atac;
   1553  1.137  jdolecek 	bool threset = false;
   1554  1.142  jdolecek 	struct ata_drive_datas *drvp;
   1555   1.55   thorpej 
   1556  1.142  jdolecek 	ata_channel_lock_owned(chp);
   1557   1.55   thorpej 
   1558   1.55   thorpej 	/*
   1559   1.55   thorpej 	 * If we can poll or wait it's OK, otherwise wake up the
   1560   1.55   thorpej 	 * kernel thread to do it for us.
   1561   1.55   thorpej 	 */
   1562  1.142  jdolecek 	ATADEBUG_PRINT(("%s flags 0x%x ch_flags 0x%x\n",
   1563  1.142  jdolecek 	    __func__, flags, chp->ch_flags), DEBUG_FUNCS | DEBUG_XFERS);
   1564   1.55   thorpej 	if ((flags & (AT_POLL | AT_WAIT)) == 0) {
   1565  1.142  jdolecek 		switch (type) {
   1566  1.142  jdolecek 		case ATACH_TH_RESET:
   1567  1.142  jdolecek 			if (chp->ch_flags & ATACH_TH_RESET) {
   1568  1.142  jdolecek 				/* No need to schedule another reset */
   1569  1.142  jdolecek 				return;
   1570  1.142  jdolecek 			}
   1571  1.142  jdolecek 			break;
   1572  1.142  jdolecek 		case ATACH_TH_DRIVE_RESET:
   1573  1.142  jdolecek 		    {
   1574  1.142  jdolecek 			int drive = arg;
   1575  1.142  jdolecek 
   1576  1.142  jdolecek 			KASSERT(drive <= chp->ch_ndrives);
   1577  1.142  jdolecek 			drvp = &chp->ch_drive[drive];
   1578  1.142  jdolecek 
   1579  1.142  jdolecek 			if (drvp->drive_flags & ATA_DRIVE_TH_RESET) {
   1580  1.142  jdolecek 				/* No need to schedule another reset */
   1581  1.142  jdolecek 				return;
   1582  1.142  jdolecek 			}
   1583  1.142  jdolecek 			drvp->drive_flags |= ATA_DRIVE_TH_RESET;
   1584  1.142  jdolecek 			break;
   1585  1.142  jdolecek 		    }
   1586  1.142  jdolecek 		case ATACH_TH_RECOVERY:
   1587  1.142  jdolecek 		    {
   1588  1.142  jdolecek 			uint32_t tfd = (uint32_t)arg;
   1589  1.142  jdolecek 
   1590  1.142  jdolecek 			KASSERT((chp->ch_flags & ATACH_RECOVERING) == 0);
   1591  1.142  jdolecek 			chp->recovery_tfd = tfd;
   1592  1.142  jdolecek 			break;
   1593  1.142  jdolecek 		    }
   1594  1.142  jdolecek 		default:
   1595  1.142  jdolecek 			panic("%s: unknown type: %x", __func__, type);
   1596  1.142  jdolecek 			/* NOTREACHED */
   1597   1.55   thorpej 		}
   1598  1.137  jdolecek 
   1599  1.137  jdolecek 		/*
   1600  1.137  jdolecek 		 * Block execution of other commands while reset is scheduled
   1601  1.137  jdolecek 		 * to a thread.
   1602  1.137  jdolecek 		 */
   1603  1.137  jdolecek 		ata_channel_freeze_locked(chp);
   1604  1.142  jdolecek 		chp->ch_flags |= type;
   1605  1.142  jdolecek 
   1606  1.157   thorpej 		cv_signal(&chp->ch_thr_idle);
   1607   1.55   thorpej 		return;
   1608   1.55   thorpej 	}
   1609   1.55   thorpej 
   1610  1.137  jdolecek 	/* Block execution of other commands during reset */
   1611  1.137  jdolecek 	ata_channel_freeze_locked(chp);
   1612  1.137  jdolecek 
   1613  1.137  jdolecek 	/*
   1614  1.137  jdolecek 	 * If reset has been scheduled to a thread, then clear
   1615  1.137  jdolecek 	 * the flag now so that the thread won't try to execute it if
   1616  1.137  jdolecek 	 * we happen to sleep, and thaw one more time after the reset.
   1617  1.137  jdolecek 	 */
   1618  1.142  jdolecek 	if (chp->ch_flags & type) {
   1619  1.142  jdolecek 		chp->ch_flags &= ~type;
   1620  1.137  jdolecek 		threset = true;
   1621  1.137  jdolecek 	}
   1622  1.137  jdolecek 
   1623  1.142  jdolecek 	switch (type) {
   1624  1.142  jdolecek 	case ATACH_TH_RESET:
   1625  1.142  jdolecek 		(*atac->atac_bustype_ata->ata_reset_channel)(chp, flags);
   1626  1.142  jdolecek 
   1627  1.142  jdolecek 		KASSERT(chp->ch_ndrives == 0 || chp->ch_drive != NULL);
   1628  1.142  jdolecek 		for (int drive = 0; drive < chp->ch_ndrives; drive++)
   1629  1.142  jdolecek 			chp->ch_drive[drive].state = 0;
   1630  1.142  jdolecek 		break;
   1631  1.142  jdolecek 
   1632  1.142  jdolecek 	case ATACH_TH_DRIVE_RESET:
   1633  1.142  jdolecek 	    {
   1634  1.142  jdolecek 		int drive = arg;
   1635  1.142  jdolecek 
   1636  1.142  jdolecek 		KASSERT(drive <= chp->ch_ndrives);
   1637  1.142  jdolecek 		drvp = &chp->ch_drive[drive];
   1638  1.142  jdolecek 		(*atac->atac_bustype_ata->ata_reset_drive)(drvp, flags, NULL);
   1639  1.142  jdolecek 		drvp->state = 0;
   1640  1.142  jdolecek 		break;
   1641  1.142  jdolecek 	    }
   1642  1.137  jdolecek 
   1643  1.142  jdolecek 	case ATACH_TH_RECOVERY:
   1644  1.142  jdolecek 	    {
   1645  1.142  jdolecek 		uint32_t tfd = (uint32_t)arg;
   1646  1.142  jdolecek 
   1647  1.142  jdolecek 		KASSERT((chp->ch_flags & ATACH_RECOVERING) == 0);
   1648  1.142  jdolecek 		KASSERT(atac->atac_bustype_ata->ata_recovery != NULL);
   1649  1.142  jdolecek 
   1650  1.142  jdolecek 		SET(chp->ch_flags, ATACH_RECOVERING);
   1651  1.142  jdolecek 		(*atac->atac_bustype_ata->ata_recovery)(chp, flags, tfd);
   1652  1.142  jdolecek 		CLR(chp->ch_flags, ATACH_RECOVERING);
   1653  1.142  jdolecek 		break;
   1654  1.142  jdolecek 	    }
   1655   1.55   thorpej 
   1656  1.142  jdolecek 	default:
   1657  1.142  jdolecek 		panic("%s: unknown type: %x", __func__, type);
   1658  1.142  jdolecek 		/* NOTREACHED */
   1659  1.142  jdolecek 	}
   1660   1.55   thorpej 
   1661  1.137  jdolecek 	/*
   1662  1.137  jdolecek 	 * Thaw one extra time to clear the freeze done when the reset has
   1663  1.137  jdolecek 	 * been scheduled to the thread.
   1664  1.137  jdolecek 	 */
   1665  1.137  jdolecek 	if (threset)
   1666  1.137  jdolecek 		ata_channel_thaw_locked(chp);
   1667  1.137  jdolecek 
   1668  1.137  jdolecek 	/* Allow commands to run again */
   1669  1.137  jdolecek 	ata_channel_thaw_locked(chp);
   1670  1.137  jdolecek 
   1671  1.137  jdolecek 	/* Signal the thread in case there is an xfer to run */
   1672  1.157   thorpej 	cv_signal(&chp->ch_thr_idle);
   1673   1.55   thorpej }
   1674   1.55   thorpej 
   1675   1.43   thorpej int
   1676   1.48   thorpej ata_addref(struct ata_channel *chp)
   1677   1.43   thorpej {
   1678   1.65     perry 	struct atac_softc *atac = chp->ch_atac;
   1679   1.49   thorpej 	struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
   1680   1.43   thorpej 	int s, error = 0;
   1681   1.43   thorpej 
   1682   1.43   thorpej 	s = splbio();
   1683   1.43   thorpej 	if (adapt->adapt_refcnt++ == 0 &&
   1684   1.43   thorpej 	    adapt->adapt_enable != NULL) {
   1685   1.98      cube 		error = (*adapt->adapt_enable)(atac->atac_dev, 1);
   1686   1.43   thorpej 		if (error)
   1687   1.43   thorpej 			adapt->adapt_refcnt--;
   1688   1.43   thorpej 	}
   1689   1.43   thorpej 	splx(s);
   1690   1.43   thorpej 	return (error);
   1691   1.43   thorpej }
   1692   1.43   thorpej 
   1693   1.43   thorpej void
   1694   1.48   thorpej ata_delref(struct ata_channel *chp)
   1695   1.43   thorpej {
   1696   1.49   thorpej 	struct atac_softc *atac = chp->ch_atac;
   1697   1.49   thorpej 	struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
   1698   1.43   thorpej 	int s;
   1699   1.43   thorpej 
   1700   1.43   thorpej 	s = splbio();
   1701   1.43   thorpej 	if (adapt->adapt_refcnt-- == 1 &&
   1702   1.43   thorpej 	    adapt->adapt_enable != NULL)
   1703   1.98      cube 		(void) (*adapt->adapt_enable)(atac->atac_dev, 0);
   1704   1.43   thorpej 	splx(s);
   1705   1.43   thorpej }
   1706   1.43   thorpej 
   1707   1.38   thorpej void
   1708   1.48   thorpej ata_print_modes(struct ata_channel *chp)
   1709   1.38   thorpej {
   1710   1.49   thorpej 	struct atac_softc *atac = chp->ch_atac;
   1711   1.38   thorpej 	int drive;
   1712   1.38   thorpej 	struct ata_drive_datas *drvp;
   1713   1.38   thorpej 
   1714  1.124    bouyer 	KASSERT(chp->ch_ndrives == 0 || chp->ch_drive != NULL);
   1715  1.124    bouyer 	for (drive = 0; drive < chp->ch_ndrives; drive++) {
   1716   1.38   thorpej 		drvp = &chp->ch_drive[drive];
   1717  1.124    bouyer 		if (drvp->drive_type == ATA_DRIVET_NONE ||
   1718  1.124    bouyer 		    drvp->drv_softc == NULL)
   1719   1.38   thorpej 			continue;
   1720   1.85        ad 		aprint_verbose("%s(%s:%d:%d): using PIO mode %d",
   1721   1.95    dyoung 			device_xname(drvp->drv_softc),
   1722   1.98      cube 			device_xname(atac->atac_dev),
   1723   1.67      matt 			chp->ch_channel, drvp->drive, drvp->PIO_mode);
   1724   1.78     itohy #if NATA_DMA
   1725  1.124    bouyer 		if (drvp->drive_flags & ATA_DRIVE_DMA)
   1726   1.85        ad 			aprint_verbose(", DMA mode %d", drvp->DMA_mode);
   1727   1.78     itohy #if NATA_UDMA
   1728  1.124    bouyer 		if (drvp->drive_flags & ATA_DRIVE_UDMA) {
   1729   1.85        ad 			aprint_verbose(", Ultra-DMA mode %d", drvp->UDMA_mode);
   1730   1.38   thorpej 			if (drvp->UDMA_mode == 2)
   1731   1.85        ad 				aprint_verbose(" (Ultra/33)");
   1732   1.38   thorpej 			else if (drvp->UDMA_mode == 4)
   1733   1.85        ad 				aprint_verbose(" (Ultra/66)");
   1734   1.38   thorpej 			else if (drvp->UDMA_mode == 5)
   1735   1.85        ad 				aprint_verbose(" (Ultra/100)");
   1736   1.38   thorpej 			else if (drvp->UDMA_mode == 6)
   1737   1.85        ad 				aprint_verbose(" (Ultra/133)");
   1738   1.38   thorpej 		}
   1739   1.78     itohy #endif	/* NATA_UDMA */
   1740   1.78     itohy #endif	/* NATA_DMA */
   1741   1.78     itohy #if NATA_DMA || NATA_PIOBM
   1742   1.78     itohy 		if (0
   1743   1.78     itohy #if NATA_DMA
   1744  1.124    bouyer 		    || (drvp->drive_flags & (ATA_DRIVE_DMA | ATA_DRIVE_UDMA))
   1745   1.78     itohy #endif
   1746   1.76     itohy #if NATA_PIOBM
   1747   1.76     itohy 		    /* PIOBM capable controllers use DMA for PIO commands */
   1748   1.76     itohy 		    || (atac->atac_cap & ATAC_CAP_PIOBM)
   1749   1.76     itohy #endif
   1750   1.76     itohy 		    )
   1751   1.85        ad 			aprint_verbose(" (using DMA)");
   1752  1.133  jdolecek 
   1753  1.133  jdolecek 		if (drvp->drive_flags & ATA_DRIVE_NCQ) {
   1754  1.133  jdolecek 			aprint_verbose(", NCQ (%d tags)%s",
   1755  1.133  jdolecek 			    ATA_REAL_OPENINGS(chp->ch_queue->queue_openings),
   1756  1.133  jdolecek 			    (drvp->drive_flags & ATA_DRIVE_NCQ_PRIO)
   1757  1.133  jdolecek 			    ? " w/PRIO" : "");
   1758  1.133  jdolecek 		} else if (drvp->drive_flags & ATA_DRIVE_WFUA)
   1759  1.133  jdolecek 			aprint_verbose(", WRITE DMA FUA EXT");
   1760  1.133  jdolecek 
   1761   1.78     itohy #endif	/* NATA_DMA || NATA_PIOBM */
   1762   1.85        ad 		aprint_verbose("\n");
   1763   1.38   thorpej 	}
   1764   1.38   thorpej }
   1765   1.38   thorpej 
   1766  1.159  jdolecek #if defined(ATA_DOWNGRADE_MODE) && NATA_DMA
   1767   1.39   thorpej /*
   1768   1.39   thorpej  * downgrade the transfer mode of a drive after an error. return 1 if
   1769   1.39   thorpej  * downgrade was possible, 0 otherwise.
   1770   1.57   thorpej  *
   1771   1.57   thorpej  * MUST BE CALLED AT splbio()!
   1772   1.39   thorpej  */
   1773  1.158  jdolecek static int
   1774   1.39   thorpej ata_downgrade_mode(struct ata_drive_datas *drvp, int flags)
   1775   1.39   thorpej {
   1776   1.48   thorpej 	struct ata_channel *chp = drvp->chnl_softc;
   1777   1.49   thorpej 	struct atac_softc *atac = chp->ch_atac;
   1778   1.95    dyoung 	device_t drv_dev = drvp->drv_softc;
   1779   1.74   thorpej 	int cf_flags = device_cfdata(drv_dev)->cf_flags;
   1780   1.39   thorpej 
   1781  1.146  jdolecek 	ata_channel_lock_owned(drvp->chnl_softc);
   1782  1.146  jdolecek 
   1783   1.39   thorpej 	/* if drive or controller don't know its mode, we can't do much */
   1784  1.124    bouyer 	if ((drvp->drive_flags & ATA_DRIVE_MODE) == 0 ||
   1785   1.49   thorpej 	    (atac->atac_set_modes == NULL))
   1786   1.39   thorpej 		return 0;
   1787   1.39   thorpej 	/* current drive mode was set by a config flag, let it this way */
   1788   1.39   thorpej 	if ((cf_flags & ATA_CONFIG_PIO_SET) ||
   1789   1.39   thorpej 	    (cf_flags & ATA_CONFIG_DMA_SET) ||
   1790   1.39   thorpej 	    (cf_flags & ATA_CONFIG_UDMA_SET))
   1791   1.39   thorpej 		return 0;
   1792   1.39   thorpej 
   1793   1.78     itohy #if NATA_UDMA
   1794   1.39   thorpej 	/*
   1795   1.39   thorpej 	 * If we were using Ultra-DMA mode, downgrade to the next lower mode.
   1796   1.39   thorpej 	 */
   1797  1.124    bouyer 	if ((drvp->drive_flags & ATA_DRIVE_UDMA) && drvp->UDMA_mode >= 2) {
   1798   1.39   thorpej 		drvp->UDMA_mode--;
   1799   1.98      cube 		aprint_error_dev(drv_dev,
   1800   1.98      cube 		    "transfer error, downgrading to Ultra-DMA mode %d\n",
   1801   1.98      cube 		    drvp->UDMA_mode);
   1802   1.39   thorpej 	}
   1803   1.78     itohy #endif
   1804   1.39   thorpej 
   1805   1.39   thorpej 	/*
   1806   1.39   thorpej 	 * If we were using ultra-DMA, don't downgrade to multiword DMA.
   1807   1.39   thorpej 	 */
   1808  1.124    bouyer 	else if (drvp->drive_flags & (ATA_DRIVE_DMA | ATA_DRIVE_UDMA)) {
   1809  1.124    bouyer 		drvp->drive_flags &= ~(ATA_DRIVE_DMA | ATA_DRIVE_UDMA);
   1810   1.39   thorpej 		drvp->PIO_mode = drvp->PIO_cap;
   1811   1.98      cube 		aprint_error_dev(drv_dev,
   1812   1.98      cube 		    "transfer error, downgrading to PIO mode %d\n",
   1813   1.98      cube 		    drvp->PIO_mode);
   1814   1.39   thorpej 	} else /* already using PIO, can't downgrade */
   1815   1.39   thorpej 		return 0;
   1816   1.39   thorpej 
   1817   1.49   thorpej 	(*atac->atac_set_modes)(chp);
   1818   1.39   thorpej 	ata_print_modes(chp);
   1819   1.84       wiz 	/* reset the channel, which will schedule all drives for setup */
   1820  1.142  jdolecek 	ata_thread_run(chp, flags, ATACH_TH_RESET, ATACH_NODRIVE);
   1821   1.39   thorpej 	return 1;
   1822   1.39   thorpej }
   1823  1.159  jdolecek #endif	/* ATA_DOWNGRADE_MODE && NATA_DMA */
   1824   1.39   thorpej 
   1825   1.40   thorpej /*
   1826   1.40   thorpej  * Probe drive's capabilities, for use by the controller later
   1827   1.65     perry  * Assumes drvp points to an existing drive.
   1828   1.40   thorpej  */
   1829   1.40   thorpej void
   1830   1.40   thorpej ata_probe_caps(struct ata_drive_datas *drvp)
   1831   1.40   thorpej {
   1832   1.40   thorpej 	struct ataparams params, params2;
   1833   1.48   thorpej 	struct ata_channel *chp = drvp->chnl_softc;
   1834   1.49   thorpej 	struct atac_softc *atac = chp->ch_atac;
   1835   1.95    dyoung 	device_t drv_dev = drvp->drv_softc;
   1836  1.133  jdolecek 	int i, printed = 0;
   1837   1.70  christos 	const char *sep = "";
   1838   1.40   thorpej 	int cf_flags;
   1839   1.40   thorpej 
   1840   1.40   thorpej 	if (ata_get_params(drvp, AT_WAIT, &params) != CMD_OK) {
   1841   1.40   thorpej 		/* IDENTIFY failed. Can't tell more about the device */
   1842   1.40   thorpej 		return;
   1843   1.40   thorpej 	}
   1844   1.49   thorpej 	if ((atac->atac_cap & (ATAC_CAP_DATA16 | ATAC_CAP_DATA32)) ==
   1845   1.49   thorpej 	    (ATAC_CAP_DATA16 | ATAC_CAP_DATA32)) {
   1846   1.40   thorpej 		/*
   1847   1.40   thorpej 		 * Controller claims 16 and 32 bit transfers.
   1848   1.40   thorpej 		 * Re-do an IDENTIFY with 32-bit transfers,
   1849   1.40   thorpej 		 * and compare results.
   1850   1.40   thorpej 		 */
   1851  1.133  jdolecek 		ata_channel_lock(chp);
   1852  1.124    bouyer 		drvp->drive_flags |= ATA_DRIVE_CAP32;
   1853  1.133  jdolecek 		ata_channel_unlock(chp);
   1854   1.40   thorpej 		ata_get_params(drvp, AT_WAIT, &params2);
   1855   1.40   thorpej 		if (memcmp(&params, &params2, sizeof(struct ataparams)) != 0) {
   1856   1.40   thorpej 			/* Not good. fall back to 16bits */
   1857  1.133  jdolecek 			ata_channel_lock(chp);
   1858  1.124    bouyer 			drvp->drive_flags &= ~ATA_DRIVE_CAP32;
   1859  1.133  jdolecek 			ata_channel_unlock(chp);
   1860   1.40   thorpej 		} else {
   1861   1.98      cube 			aprint_verbose_dev(drv_dev, "32-bit data port\n");
   1862   1.40   thorpej 		}
   1863   1.40   thorpej 	}
   1864   1.40   thorpej #if 0 /* Some ultra-DMA drives claims to only support ATA-3. sigh */
   1865   1.65     perry 	if (params.atap_ata_major > 0x01 &&
   1866   1.40   thorpej 	    params.atap_ata_major != 0xffff) {
   1867   1.40   thorpej 		for (i = 14; i > 0; i--) {
   1868   1.40   thorpej 			if (params.atap_ata_major & (1 << i)) {
   1869   1.98      cube 				aprint_verbose_dev(drv_dev,
   1870   1.98      cube 				    "ATA version %d\n", i);
   1871   1.40   thorpej 				drvp->ata_vers = i;
   1872   1.40   thorpej 				break;
   1873   1.40   thorpej 			}
   1874   1.40   thorpej 		}
   1875   1.40   thorpej 	}
   1876   1.40   thorpej #endif
   1877   1.40   thorpej 
   1878   1.40   thorpej 	/* An ATAPI device is at last PIO mode 3 */
   1879  1.124    bouyer 	if (drvp->drive_type == ATA_DRIVET_ATAPI)
   1880   1.40   thorpej 		drvp->PIO_mode = 3;
   1881   1.40   thorpej 
   1882   1.40   thorpej 	/*
   1883   1.65     perry 	 * It's not in the specs, but it seems that some drive
   1884   1.40   thorpej 	 * returns 0xffff in atap_extensions when this field is invalid
   1885   1.40   thorpej 	 */
   1886   1.40   thorpej 	if (params.atap_extensions != 0xffff &&
   1887   1.40   thorpej 	    (params.atap_extensions & WDC_EXT_MODES)) {
   1888   1.40   thorpej 		/*
   1889   1.40   thorpej 		 * XXX some drives report something wrong here (they claim to
   1890   1.40   thorpej 		 * support PIO mode 8 !). As mode is coded on 3 bits in
   1891   1.40   thorpej 		 * SET FEATURE, limit it to 7 (so limit i to 4).
   1892   1.40   thorpej 		 * If higher mode than 7 is found, abort.
   1893   1.40   thorpej 		 */
   1894   1.40   thorpej 		for (i = 7; i >= 0; i--) {
   1895   1.40   thorpej 			if ((params.atap_piomode_supp & (1 << i)) == 0)
   1896   1.40   thorpej 				continue;
   1897   1.40   thorpej 			if (i > 4)
   1898   1.40   thorpej 				return;
   1899   1.40   thorpej 			/*
   1900   1.40   thorpej 			 * See if mode is accepted.
   1901   1.40   thorpej 			 * If the controller can't set its PIO mode,
   1902   1.40   thorpej 			 * assume the defaults are good, so don't try
   1903   1.40   thorpej 			 * to set it
   1904   1.40   thorpej 			 */
   1905   1.49   thorpej 			if (atac->atac_set_modes)
   1906   1.40   thorpej 				/*
   1907  1.154  jdolecek 				 * It's OK to poll here, it's fast enough
   1908   1.40   thorpej 				 * to not bother waiting for interrupt
   1909   1.40   thorpej 				 */
   1910   1.40   thorpej 				if (ata_set_mode(drvp, 0x08 | (i + 3),
   1911   1.40   thorpej 				   AT_WAIT) != CMD_OK)
   1912   1.40   thorpej 					continue;
   1913   1.65     perry 			if (!printed) {
   1914   1.98      cube 				aprint_verbose_dev(drv_dev,
   1915   1.98      cube 				    "drive supports PIO mode %d", i + 3);
   1916   1.40   thorpej 				sep = ",";
   1917   1.40   thorpej 				printed = 1;
   1918   1.40   thorpej 			}
   1919   1.40   thorpej 			/*
   1920   1.40   thorpej 			 * If controller's driver can't set its PIO mode,
   1921   1.40   thorpej 			 * get the highter one for the drive.
   1922   1.40   thorpej 			 */
   1923   1.49   thorpej 			if (atac->atac_set_modes == NULL ||
   1924   1.49   thorpej 			    atac->atac_pio_cap >= i + 3) {
   1925   1.40   thorpej 				drvp->PIO_mode = i + 3;
   1926   1.40   thorpej 				drvp->PIO_cap = i + 3;
   1927   1.40   thorpej 				break;
   1928   1.40   thorpej 			}
   1929   1.40   thorpej 		}
   1930   1.40   thorpej 		if (!printed) {
   1931   1.65     perry 			/*
   1932   1.40   thorpej 			 * We didn't find a valid PIO mode.
   1933   1.40   thorpej 			 * Assume the values returned for DMA are buggy too
   1934   1.40   thorpej 			 */
   1935   1.40   thorpej 			return;
   1936   1.40   thorpej 		}
   1937  1.133  jdolecek 		ata_channel_lock(chp);
   1938  1.124    bouyer 		drvp->drive_flags |= ATA_DRIVE_MODE;
   1939  1.133  jdolecek 		ata_channel_unlock(chp);
   1940   1.40   thorpej 		printed = 0;
   1941   1.40   thorpej 		for (i = 7; i >= 0; i--) {
   1942   1.40   thorpej 			if ((params.atap_dmamode_supp & (1 << i)) == 0)
   1943   1.40   thorpej 				continue;
   1944   1.78     itohy #if NATA_DMA
   1945   1.49   thorpej 			if ((atac->atac_cap & ATAC_CAP_DMA) &&
   1946   1.49   thorpej 			    atac->atac_set_modes != NULL)
   1947   1.40   thorpej 				if (ata_set_mode(drvp, 0x20 | i, AT_WAIT)
   1948   1.40   thorpej 				    != CMD_OK)
   1949   1.40   thorpej 					continue;
   1950   1.78     itohy #endif
   1951   1.40   thorpej 			if (!printed) {
   1952   1.85        ad 				aprint_verbose("%s DMA mode %d", sep, i);
   1953   1.40   thorpej 				sep = ",";
   1954   1.40   thorpej 				printed = 1;
   1955   1.40   thorpej 			}
   1956   1.78     itohy #if NATA_DMA
   1957   1.49   thorpej 			if (atac->atac_cap & ATAC_CAP_DMA) {
   1958   1.49   thorpej 				if (atac->atac_set_modes != NULL &&
   1959   1.49   thorpej 				    atac->atac_dma_cap < i)
   1960   1.40   thorpej 					continue;
   1961   1.40   thorpej 				drvp->DMA_mode = i;
   1962   1.40   thorpej 				drvp->DMA_cap = i;
   1963  1.133  jdolecek 				ata_channel_lock(chp);
   1964  1.124    bouyer 				drvp->drive_flags |= ATA_DRIVE_DMA;
   1965  1.133  jdolecek 				ata_channel_unlock(chp);
   1966   1.40   thorpej 			}
   1967   1.78     itohy #endif
   1968   1.40   thorpej 			break;
   1969   1.40   thorpej 		}
   1970   1.40   thorpej 		if (params.atap_extensions & WDC_EXT_UDMA_MODES) {
   1971   1.40   thorpej 			printed = 0;
   1972   1.40   thorpej 			for (i = 7; i >= 0; i--) {
   1973   1.40   thorpej 				if ((params.atap_udmamode_supp & (1 << i))
   1974   1.40   thorpej 				    == 0)
   1975   1.40   thorpej 					continue;
   1976   1.78     itohy #if NATA_UDMA
   1977   1.49   thorpej 				if (atac->atac_set_modes != NULL &&
   1978   1.49   thorpej 				    (atac->atac_cap & ATAC_CAP_UDMA))
   1979   1.40   thorpej 					if (ata_set_mode(drvp, 0x40 | i,
   1980   1.40   thorpej 					    AT_WAIT) != CMD_OK)
   1981   1.40   thorpej 						continue;
   1982   1.78     itohy #endif
   1983   1.40   thorpej 				if (!printed) {
   1984   1.85        ad 					aprint_verbose("%s Ultra-DMA mode %d",
   1985   1.40   thorpej 					    sep, i);
   1986   1.40   thorpej 					if (i == 2)
   1987   1.85        ad 						aprint_verbose(" (Ultra/33)");
   1988   1.40   thorpej 					else if (i == 4)
   1989   1.85        ad 						aprint_verbose(" (Ultra/66)");
   1990   1.40   thorpej 					else if (i == 5)
   1991   1.85        ad 						aprint_verbose(" (Ultra/100)");
   1992   1.40   thorpej 					else if (i == 6)
   1993   1.85        ad 						aprint_verbose(" (Ultra/133)");
   1994   1.40   thorpej 					sep = ",";
   1995   1.40   thorpej 					printed = 1;
   1996   1.40   thorpej 				}
   1997   1.78     itohy #if NATA_UDMA
   1998   1.49   thorpej 				if (atac->atac_cap & ATAC_CAP_UDMA) {
   1999   1.49   thorpej 					if (atac->atac_set_modes != NULL &&
   2000   1.49   thorpej 					    atac->atac_udma_cap < i)
   2001   1.40   thorpej 						continue;
   2002   1.40   thorpej 					drvp->UDMA_mode = i;
   2003   1.40   thorpej 					drvp->UDMA_cap = i;
   2004  1.133  jdolecek 					ata_channel_lock(chp);
   2005  1.124    bouyer 					drvp->drive_flags |= ATA_DRIVE_UDMA;
   2006  1.133  jdolecek 					ata_channel_unlock(chp);
   2007   1.40   thorpej 				}
   2008   1.78     itohy #endif
   2009   1.40   thorpej 				break;
   2010   1.40   thorpej 			}
   2011   1.40   thorpej 		}
   2012   1.40   thorpej 	}
   2013   1.40   thorpej 
   2014  1.133  jdolecek 	ata_channel_lock(chp);
   2015  1.124    bouyer 	drvp->drive_flags &= ~ATA_DRIVE_NOSTREAM;
   2016  1.124    bouyer 	if (drvp->drive_type == ATA_DRIVET_ATAPI) {
   2017   1.65     perry 		if (atac->atac_cap & ATAC_CAP_ATAPI_NOSTREAM)
   2018  1.124    bouyer 			drvp->drive_flags |= ATA_DRIVE_NOSTREAM;
   2019   1.40   thorpej 	} else {
   2020   1.65     perry 		if (atac->atac_cap & ATAC_CAP_ATA_NOSTREAM)
   2021  1.124    bouyer 			drvp->drive_flags |= ATA_DRIVE_NOSTREAM;
   2022   1.40   thorpej 	}
   2023  1.133  jdolecek 	ata_channel_unlock(chp);
   2024   1.40   thorpej 
   2025   1.40   thorpej 	/* Try to guess ATA version here, if it didn't get reported */
   2026   1.40   thorpej 	if (drvp->ata_vers == 0) {
   2027   1.78     itohy #if NATA_UDMA
   2028  1.124    bouyer 		if (drvp->drive_flags & ATA_DRIVE_UDMA)
   2029   1.40   thorpej 			drvp->ata_vers = 4; /* should be at last ATA-4 */
   2030   1.78     itohy 		else
   2031   1.78     itohy #endif
   2032   1.78     itohy 		if (drvp->PIO_cap > 2)
   2033   1.40   thorpej 			drvp->ata_vers = 2; /* should be at last ATA-2 */
   2034   1.40   thorpej 	}
   2035   1.74   thorpej 	cf_flags = device_cfdata(drv_dev)->cf_flags;
   2036   1.40   thorpej 	if (cf_flags & ATA_CONFIG_PIO_SET) {
   2037  1.133  jdolecek 		ata_channel_lock(chp);
   2038   1.40   thorpej 		drvp->PIO_mode =
   2039   1.40   thorpej 		    (cf_flags & ATA_CONFIG_PIO_MODES) >> ATA_CONFIG_PIO_OFF;
   2040  1.124    bouyer 		drvp->drive_flags |= ATA_DRIVE_MODE;
   2041  1.133  jdolecek 		ata_channel_unlock(chp);
   2042   1.40   thorpej 	}
   2043   1.78     itohy #if NATA_DMA
   2044   1.49   thorpej 	if ((atac->atac_cap & ATAC_CAP_DMA) == 0) {
   2045   1.40   thorpej 		/* don't care about DMA modes */
   2046  1.148       uwe 		if (*sep != '\0')
   2047  1.148       uwe 			aprint_verbose("\n");
   2048   1.40   thorpej 		return;
   2049   1.40   thorpej 	}
   2050   1.40   thorpej 	if (cf_flags & ATA_CONFIG_DMA_SET) {
   2051  1.133  jdolecek 		ata_channel_lock(chp);
   2052   1.40   thorpej 		if ((cf_flags & ATA_CONFIG_DMA_MODES) ==
   2053   1.40   thorpej 		    ATA_CONFIG_DMA_DISABLE) {
   2054  1.124    bouyer 			drvp->drive_flags &= ~ATA_DRIVE_DMA;
   2055   1.40   thorpej 		} else {
   2056   1.40   thorpej 			drvp->DMA_mode = (cf_flags & ATA_CONFIG_DMA_MODES) >>
   2057   1.40   thorpej 			    ATA_CONFIG_DMA_OFF;
   2058  1.124    bouyer 			drvp->drive_flags |= ATA_DRIVE_DMA | ATA_DRIVE_MODE;
   2059   1.40   thorpej 		}
   2060  1.133  jdolecek 		ata_channel_unlock(chp);
   2061  1.133  jdolecek 	}
   2062  1.133  jdolecek 
   2063  1.133  jdolecek 	/*
   2064  1.133  jdolecek 	 * Probe WRITE DMA FUA EXT. Support is mandatory for devices
   2065  1.133  jdolecek 	 * supporting LBA48, but nevertheless confirm with the feature flag.
   2066  1.133  jdolecek 	 */
   2067  1.133  jdolecek 	if (drvp->drive_flags & ATA_DRIVE_DMA) {
   2068  1.133  jdolecek 		if ((params.atap_cmd2_en & ATA_CMD2_LBA48) != 0
   2069  1.133  jdolecek 		    && (params.atap_cmd_def & ATA_CMDE_WFE)) {
   2070  1.133  jdolecek 			drvp->drive_flags |= ATA_DRIVE_WFUA;
   2071  1.133  jdolecek 			aprint_verbose("%s WRITE DMA FUA", sep);
   2072  1.133  jdolecek 			sep = ",";
   2073  1.133  jdolecek 		}
   2074  1.133  jdolecek 	}
   2075  1.133  jdolecek 
   2076  1.133  jdolecek 	/* Probe NCQ support - READ/WRITE FPDMA QUEUED command support */
   2077  1.133  jdolecek 	ata_channel_lock(chp);
   2078  1.133  jdolecek 	drvp->drv_openings = 1;
   2079  1.133  jdolecek 	if (params.atap_sata_caps & SATA_NATIVE_CMDQ) {
   2080  1.133  jdolecek 		if (atac->atac_cap & ATAC_CAP_NCQ)
   2081  1.133  jdolecek 			drvp->drive_flags |= ATA_DRIVE_NCQ;
   2082  1.133  jdolecek 		drvp->drv_openings =
   2083  1.133  jdolecek 		    (params.atap_queuedepth & WDC_QUEUE_DEPTH_MASK) + 1;
   2084  1.133  jdolecek 		aprint_verbose("%s NCQ (%d tags)", sep, drvp->drv_openings);
   2085  1.133  jdolecek 		sep = ",";
   2086  1.133  jdolecek 
   2087  1.133  jdolecek 		if (params.atap_sata_caps & SATA_NCQ_PRIO) {
   2088  1.133  jdolecek 			drvp->drive_flags |= ATA_DRIVE_NCQ_PRIO;
   2089  1.133  jdolecek 			aprint_verbose(" w/PRIO");
   2090  1.133  jdolecek 		}
   2091   1.40   thorpej 	}
   2092  1.133  jdolecek 	ata_channel_unlock(chp);
   2093  1.133  jdolecek 
   2094  1.148       uwe 	if (*sep != '\0')
   2095  1.133  jdolecek 		aprint_verbose("\n");
   2096  1.133  jdolecek 
   2097   1.78     itohy #if NATA_UDMA
   2098   1.49   thorpej 	if ((atac->atac_cap & ATAC_CAP_UDMA) == 0) {
   2099   1.40   thorpej 		/* don't care about UDMA modes */
   2100   1.40   thorpej 		return;
   2101   1.40   thorpej 	}
   2102   1.40   thorpej 	if (cf_flags & ATA_CONFIG_UDMA_SET) {
   2103  1.133  jdolecek 		ata_channel_lock(chp);
   2104   1.40   thorpej 		if ((cf_flags & ATA_CONFIG_UDMA_MODES) ==
   2105   1.40   thorpej 		    ATA_CONFIG_UDMA_DISABLE) {
   2106  1.124    bouyer 			drvp->drive_flags &= ~ATA_DRIVE_UDMA;
   2107   1.40   thorpej 		} else {
   2108   1.40   thorpej 			drvp->UDMA_mode = (cf_flags & ATA_CONFIG_UDMA_MODES) >>
   2109   1.40   thorpej 			    ATA_CONFIG_UDMA_OFF;
   2110  1.124    bouyer 			drvp->drive_flags |= ATA_DRIVE_UDMA | ATA_DRIVE_MODE;
   2111   1.40   thorpej 		}
   2112  1.133  jdolecek 		ata_channel_unlock(chp);
   2113   1.40   thorpej 	}
   2114   1.78     itohy #endif	/* NATA_UDMA */
   2115   1.78     itohy #endif	/* NATA_DMA */
   2116   1.40   thorpej }
   2117   1.40   thorpej 
   2118   1.30    bouyer /* management of the /dev/atabus* devices */
   2119   1.54   thorpej int
   2120  1.111    dyoung atabusopen(dev_t dev, int flag, int fmt, struct lwp *l)
   2121   1.30    bouyer {
   2122   1.81     itohy 	struct atabus_softc *sc;
   2123   1.99    cegger 	int error;
   2124   1.65     perry 
   2125   1.99    cegger 	sc = device_lookup_private(&atabus_cd, minor(dev));
   2126   1.99    cegger 	if (sc == NULL)
   2127   1.81     itohy 		return (ENXIO);
   2128   1.65     perry 
   2129   1.81     itohy 	if (sc->sc_flags & ATABUSCF_OPEN)
   2130   1.81     itohy 		return (EBUSY);
   2131   1.30    bouyer 
   2132   1.81     itohy 	if ((error = ata_addref(sc->sc_chan)) != 0)
   2133   1.81     itohy 		return (error);
   2134   1.30    bouyer 
   2135   1.81     itohy 	sc->sc_flags |= ATABUSCF_OPEN;
   2136   1.30    bouyer 
   2137   1.81     itohy 	return (0);
   2138   1.30    bouyer }
   2139   1.30    bouyer 
   2140   1.30    bouyer 
   2141   1.30    bouyer int
   2142  1.111    dyoung atabusclose(dev_t dev, int flag, int fmt, struct lwp *l)
   2143   1.30    bouyer {
   2144   1.98      cube 	struct atabus_softc *sc =
   2145   1.99    cegger 	    device_lookup_private(&atabus_cd, minor(dev));
   2146   1.30    bouyer 
   2147   1.81     itohy 	ata_delref(sc->sc_chan);
   2148   1.30    bouyer 
   2149   1.81     itohy 	sc->sc_flags &= ~ATABUSCF_OPEN;
   2150   1.30    bouyer 
   2151   1.81     itohy 	return (0);
   2152   1.30    bouyer }
   2153   1.30    bouyer 
   2154   1.30    bouyer int
   2155  1.111    dyoung atabusioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   2156   1.30    bouyer {
   2157   1.98      cube 	struct atabus_softc *sc =
   2158   1.99    cegger 	    device_lookup_private(&atabus_cd, minor(dev));
   2159   1.48   thorpej 	struct ata_channel *chp = sc->sc_chan;
   2160   1.32    bouyer 	int min_drive, max_drive, drive;
   2161   1.81     itohy 	int error;
   2162   1.30    bouyer 
   2163   1.81     itohy 	/*
   2164   1.81     itohy 	 * Enforce write permission for ioctls that change the
   2165   1.81     itohy 	 * state of the bus.  Host adapter specific ioctls must
   2166   1.81     itohy 	 * be checked by the adapter driver.
   2167   1.81     itohy 	 */
   2168   1.81     itohy 	switch (cmd) {
   2169   1.81     itohy 	case ATABUSIOSCAN:
   2170   1.81     itohy 	case ATABUSIODETACH:
   2171   1.81     itohy 	case ATABUSIORESET:
   2172   1.81     itohy 		if ((flag & FWRITE) == 0)
   2173   1.81     itohy 			return (EBADF);
   2174   1.81     itohy 	}
   2175   1.30    bouyer 
   2176   1.81     itohy 	switch (cmd) {
   2177   1.81     itohy 	case ATABUSIORESET:
   2178  1.142  jdolecek 		ata_channel_lock(chp);
   2179  1.142  jdolecek 		ata_thread_run(sc->sc_chan, AT_WAIT | AT_POLL,
   2180  1.142  jdolecek 		    ATACH_TH_RESET, ATACH_NODRIVE);
   2181  1.142  jdolecek 		ata_channel_unlock(chp);
   2182  1.111    dyoung 		return 0;
   2183   1.32    bouyer 	case ATABUSIOSCAN:
   2184   1.32    bouyer 	{
   2185   1.32    bouyer #if 0
   2186   1.32    bouyer 		struct atabusioscan_args *a=
   2187   1.32    bouyer 		    (struct atabusioscan_args *)addr;
   2188   1.32    bouyer #endif
   2189  1.124    bouyer 		if ((chp->ch_drive[0].drive_type == ATA_DRIVET_OLD) ||
   2190  1.124    bouyer 		    (chp->ch_drive[1].drive_type == ATA_DRIVET_OLD))
   2191   1.32    bouyer 			return (EOPNOTSUPP);
   2192   1.32    bouyer 		return (EOPNOTSUPP);
   2193   1.32    bouyer 	}
   2194   1.32    bouyer 	case ATABUSIODETACH:
   2195   1.32    bouyer 	{
   2196  1.116     isaki 		struct atabusiodetach_args *a=
   2197  1.116     isaki 		    (struct atabusiodetach_args *)addr;
   2198  1.124    bouyer 		if ((chp->ch_drive[0].drive_type == ATA_DRIVET_OLD) ||
   2199  1.124    bouyer 		    (chp->ch_drive[1].drive_type == ATA_DRIVET_OLD))
   2200   1.32    bouyer 			return (EOPNOTSUPP);
   2201   1.32    bouyer 		switch (a->at_dev) {
   2202   1.32    bouyer 		case -1:
   2203   1.32    bouyer 			min_drive = 0;
   2204   1.32    bouyer 			max_drive = 1;
   2205   1.32    bouyer 			break;
   2206   1.32    bouyer 		case 0:
   2207   1.32    bouyer 		case 1:
   2208   1.32    bouyer 			min_drive = max_drive = a->at_dev;
   2209   1.32    bouyer 			break;
   2210   1.32    bouyer 		default:
   2211   1.32    bouyer 			return (EINVAL);
   2212   1.32    bouyer 		}
   2213   1.32    bouyer 		for (drive = min_drive; drive <= max_drive; drive++) {
   2214   1.32    bouyer 			if (chp->ch_drive[drive].drv_softc != NULL) {
   2215   1.32    bouyer 				error = config_detach(
   2216   1.32    bouyer 				    chp->ch_drive[drive].drv_softc, 0);
   2217   1.32    bouyer 				if (error)
   2218   1.32    bouyer 					return (error);
   2219   1.95    dyoung 				KASSERT(chp->ch_drive[drive].drv_softc == NULL);
   2220   1.32    bouyer 			}
   2221   1.32    bouyer 		}
   2222  1.111    dyoung 		return 0;
   2223   1.32    bouyer 	}
   2224   1.30    bouyer 	default:
   2225  1.111    dyoung 		return ENOTTY;
   2226   1.30    bouyer 	}
   2227  1.111    dyoung }
   2228   1.64  jmcneill 
   2229   1.92  jmcneill static bool
   2230  1.112    dyoung atabus_suspend(device_t dv, const pmf_qual_t *qual)
   2231   1.92  jmcneill {
   2232   1.92  jmcneill 	struct atabus_softc *sc = device_private(dv);
   2233   1.92  jmcneill 	struct ata_channel *chp = sc->sc_chan;
   2234   1.92  jmcneill 
   2235  1.133  jdolecek 	ata_channel_idle(chp);
   2236   1.92  jmcneill 
   2237   1.92  jmcneill 	return true;
   2238   1.92  jmcneill }
   2239   1.92  jmcneill 
   2240   1.92  jmcneill static bool
   2241  1.112    dyoung atabus_resume(device_t dv, const pmf_qual_t *qual)
   2242   1.64  jmcneill {
   2243   1.92  jmcneill 	struct atabus_softc *sc = device_private(dv);
   2244   1.64  jmcneill 	struct ata_channel *chp = sc->sc_chan;
   2245   1.64  jmcneill 
   2246   1.92  jmcneill 	/*
   2247  1.150   msaitoh 	 * XXX joerg: with wdc, the first channel unfreezes the controller.
   2248   1.92  jmcneill 	 * Move this the reset and queue idling into wdc.
   2249   1.92  jmcneill 	 */
   2250  1.133  jdolecek 	ata_channel_lock(chp);
   2251   1.92  jmcneill 	if (chp->ch_queue->queue_freeze == 0) {
   2252  1.133  jdolecek 		ata_channel_unlock(chp);
   2253  1.133  jdolecek 		goto out;
   2254   1.64  jmcneill 	}
   2255  1.133  jdolecek 
   2256   1.92  jmcneill 	/* unfreeze the queue and reset drives */
   2257  1.137  jdolecek 	ata_channel_thaw_locked(chp);
   2258  1.137  jdolecek 
   2259  1.128     blymn 	/* reset channel only if there are drives attached */
   2260  1.128     blymn 	if (chp->ch_ndrives > 0)
   2261  1.142  jdolecek 		ata_thread_run(chp, AT_WAIT, ATACH_TH_RESET, ATACH_NODRIVE);
   2262  1.142  jdolecek 
   2263  1.142  jdolecek 	ata_channel_unlock(chp);
   2264   1.64  jmcneill 
   2265  1.133  jdolecek out:
   2266   1.92  jmcneill 	return true;
   2267   1.64  jmcneill }
   2268  1.115  jakllsch 
   2269  1.115  jakllsch static int
   2270  1.115  jakllsch atabus_rescan(device_t self, const char *ifattr, const int *locators)
   2271  1.115  jakllsch {
   2272  1.115  jakllsch 	struct atabus_softc *sc = device_private(self);
   2273  1.115  jakllsch 	struct ata_channel *chp = sc->sc_chan;
   2274  1.115  jakllsch 	struct atabus_initq *initq;
   2275  1.115  jakllsch 	int i;
   2276  1.115  jakllsch 
   2277  1.124    bouyer 	/*
   2278  1.124    bouyer 	 * we can rescan a port multiplier atabus, even if some devices are
   2279  1.124    bouyer 	 * still attached
   2280  1.124    bouyer 	 */
   2281  1.124    bouyer 	if (chp->ch_satapmp_nports == 0) {
   2282  1.124    bouyer 		if (chp->atapibus != NULL) {
   2283  1.115  jakllsch 			return EBUSY;
   2284  1.115  jakllsch 		}
   2285  1.115  jakllsch 
   2286  1.124    bouyer 		KASSERT(chp->ch_ndrives == 0 || chp->ch_drive != NULL);
   2287  1.124    bouyer 		for (i = 0; i < chp->ch_ndrives; i++) {
   2288  1.124    bouyer 			if (chp->ch_drive[i].drv_softc != NULL) {
   2289  1.124    bouyer 				return EBUSY;
   2290  1.124    bouyer 			}
   2291  1.124    bouyer 		}
   2292  1.115  jakllsch 	}
   2293  1.115  jakllsch 
   2294  1.142  jdolecek 	initq = kmem_zalloc(sizeof(*initq), KM_SLEEP);
   2295  1.115  jakllsch 	initq->atabus_sc = sc;
   2296  1.133  jdolecek 	mutex_enter(&atabus_qlock);
   2297  1.115  jakllsch 	TAILQ_INSERT_TAIL(&atabus_initq_head, initq, atabus_initq);
   2298  1.133  jdolecek 	mutex_exit(&atabus_qlock);
   2299  1.129  christos 	config_pending_incr(sc->sc_dev);
   2300  1.115  jakllsch 
   2301  1.133  jdolecek 	ata_channel_lock(chp);
   2302  1.115  jakllsch 	chp->ch_flags |= ATACH_TH_RESCAN;
   2303  1.157   thorpej 	cv_signal(&chp->ch_thr_idle);
   2304  1.133  jdolecek 	ata_channel_unlock(chp);
   2305  1.115  jakllsch 
   2306  1.115  jakllsch 	return 0;
   2307  1.115  jakllsch }
   2308  1.127    bouyer 
   2309  1.127    bouyer void
   2310  1.133  jdolecek ata_delay(struct ata_channel *chp, int ms, const char *msg, int flags)
   2311  1.127    bouyer {
   2312  1.142  jdolecek 	KASSERT(mutex_owned(&chp->ch_lock));
   2313  1.133  jdolecek 
   2314  1.127    bouyer 	if ((flags & (AT_WAIT | AT_POLL)) == AT_POLL) {
   2315  1.127    bouyer 		/*
   2316  1.133  jdolecek 		 * can't use kpause(), we may be in interrupt context
   2317  1.127    bouyer 		 * or taking a crash dump
   2318  1.127    bouyer 		 */
   2319  1.127    bouyer 		delay(ms * 1000);
   2320  1.127    bouyer 	} else {
   2321  1.133  jdolecek 		int pause = mstohz(ms);
   2322  1.134   mlelstv 
   2323  1.133  jdolecek 		kpause(msg, false, pause > 0 ? pause : 1, &chp->ch_lock);
   2324  1.127    bouyer 	}
   2325  1.127    bouyer }
   2326  1.133  jdolecek 
   2327  1.133  jdolecek void
   2328  1.133  jdolecek atacmd_toncq(struct ata_xfer *xfer, uint8_t *cmd, uint16_t *count,
   2329  1.133  jdolecek     uint16_t *features, uint8_t *device)
   2330  1.133  jdolecek {
   2331  1.133  jdolecek 	if ((xfer->c_flags & C_NCQ) == 0) {
   2332  1.133  jdolecek 		/* FUA handling for non-NCQ drives */
   2333  1.133  jdolecek 		if (xfer->c_bio.flags & ATA_FUA
   2334  1.133  jdolecek 		    && *cmd == WDCC_WRITEDMA_EXT)
   2335  1.133  jdolecek 			*cmd = WDCC_WRITEDMA_FUA_EXT;
   2336  1.133  jdolecek 
   2337  1.133  jdolecek 		return;
   2338  1.133  jdolecek 	}
   2339  1.133  jdolecek 
   2340  1.133  jdolecek 	*cmd = (xfer->c_bio.flags & ATA_READ) ?
   2341  1.133  jdolecek 	    WDCC_READ_FPDMA_QUEUED : WDCC_WRITE_FPDMA_QUEUED;
   2342  1.133  jdolecek 
   2343  1.133  jdolecek 	/* for FPDMA the block count is in features */
   2344  1.133  jdolecek 	*features = *count;
   2345  1.133  jdolecek 
   2346  1.133  jdolecek 	/* NCQ tag */
   2347  1.133  jdolecek 	*count = (xfer->c_slot << 3);
   2348  1.133  jdolecek 
   2349  1.133  jdolecek 	if (xfer->c_bio.flags & ATA_PRIO_HIGH)
   2350  1.133  jdolecek 		*count |= WDSC_PRIO_HIGH;
   2351  1.133  jdolecek 
   2352  1.133  jdolecek 	/* other device flags */
   2353  1.133  jdolecek 	if (xfer->c_bio.flags & ATA_FUA)
   2354  1.133  jdolecek 		*device |= WDSD_FUA;
   2355  1.133  jdolecek }
   2356  1.133  jdolecek 
   2357  1.133  jdolecek void
   2358  1.142  jdolecek ata_wait_cmd(struct ata_channel *chp, struct ata_xfer *xfer)
   2359  1.133  jdolecek {
   2360  1.142  jdolecek 	struct ata_queue *chq = chp->ch_queue;
   2361  1.142  jdolecek 	struct ata_command *ata_c = &xfer->c_ata_c;
   2362  1.142  jdolecek 
   2363  1.142  jdolecek 	ata_channel_lock(chp);
   2364  1.133  jdolecek 
   2365  1.142  jdolecek 	while ((ata_c->flags & AT_DONE) == 0)
   2366  1.142  jdolecek 		cv_wait(&chq->c_cmd_finish, &chp->ch_lock);
   2367  1.133  jdolecek 
   2368  1.142  jdolecek 	ata_channel_unlock(chp);
   2369  1.133  jdolecek 
   2370  1.142  jdolecek 	KASSERT((ata_c->flags & AT_DONE) != 0);
   2371  1.133  jdolecek }
   2372