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