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