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