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