Home | History | Annotate | Line # | Download | only in ata
ata.c revision 1.43
      1 /*      $NetBSD: ata.c,v 1.43 2004/08/12 22:39:40 thorpej 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.43 2004/08/12 22:39:40 thorpej Exp $");
     34 
     35 #ifndef WDCDEBUG
     36 #define WDCDEBUG
     37 #endif /* WDCDEBUG */
     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/atareg.h>
     56 #include <dev/ata/atavar.h>
     57 #include <dev/ic/wdcreg.h>
     58 #include <dev/ic/wdcvar.h>
     59 
     60 #include "locators.h"
     61 
     62 #define DEBUG_FUNCS  0x08
     63 #define DEBUG_PROBE  0x10
     64 #define DEBUG_DETACH 0x20
     65 #ifdef WDCDEBUG
     66 extern int wdcdebug_mask; /* init'ed in wdc.c */
     67 #define WDCDEBUG_PRINT(args, level) \
     68         if (wdcdebug_mask & (level)) \
     69 		printf args
     70 #else
     71 #define WDCDEBUG_PRINT(args, level)
     72 #endif
     73 
     74 POOL_INIT(ata_xfer_pool, sizeof(struct ata_xfer), 0, 0, 0, "ataspl", NULL);
     75 
     76 /*****************************************************************************
     77  * ATA bus layer.
     78  *
     79  * ATA controllers attach an atabus instance, which handles probing the bus
     80  * for drives, etc.
     81  *****************************************************************************/
     82 
     83 dev_type_open(atabusopen);
     84 dev_type_close(atabusclose);
     85 dev_type_ioctl(atabusioctl);
     86 
     87 const struct cdevsw atabus_cdevsw = {
     88 	atabusopen, atabusclose, noread, nowrite, atabusioctl,
     89 	nostop, notty, nopoll, nommap, nokqfilter,
     90 };
     91 
     92 extern struct cfdriver atabus_cd;
     93 
     94 
     95 /*
     96  * atabusprint:
     97  *
     98  *	Autoconfiguration print routine used by ATA controllers when
     99  *	attaching an atabus instance.
    100  */
    101 int
    102 atabusprint(void *aux, const char *pnp)
    103 {
    104 	struct wdc_channel *chan = aux;
    105 
    106 	if (pnp)
    107 		aprint_normal("atabus at %s", pnp);
    108 	aprint_normal(" channel %d", chan->ch_channel);
    109 
    110 	return (UNCONF);
    111 }
    112 
    113 /*
    114  * ataprint:
    115  *
    116  *	Autoconfiguration print routine.
    117  */
    118 int
    119 ataprint(void *aux, const char *pnp)
    120 {
    121 	struct ata_device *adev = aux;
    122 
    123 	if (pnp)
    124 		aprint_normal("wd at %s", pnp);
    125 	aprint_normal(" drive %d", adev->adev_drv_data->drive);
    126 
    127 	return (UNCONF);
    128 }
    129 
    130 /*
    131  * atabus_thread:
    132  *
    133  *	Worker thread for the ATA bus.
    134  */
    135 static void
    136 atabus_thread(void *arg)
    137 {
    138 	struct atabus_softc *sc = arg;
    139 	struct wdc_channel *chp = sc->sc_chan;
    140 	struct ata_xfer *xfer;
    141 	int s;
    142 
    143 	s = splbio();
    144 	chp->ch_flags |= WDCF_TH_RUN;
    145 	splx(s);
    146 
    147 	/* Configure the devices on the bus. */
    148 	atabusconfig(sc);
    149 
    150 	for (;;) {
    151 		s = splbio();
    152 		if ((chp->ch_flags & (WDCF_TH_RESET | WDCF_SHUTDOWN)) == 0 &&
    153 		    (chp->ch_queue->active_xfer == NULL ||
    154 		     chp->ch_queue->queue_freeze == 0)) {
    155 			chp->ch_flags &= ~WDCF_TH_RUN;
    156 			(void) tsleep(&chp->ch_thread, PRIBIO, "atath", 0);
    157 			chp->ch_flags |= WDCF_TH_RUN;
    158 		}
    159 		splx(s);
    160 		if (chp->ch_flags & WDCF_SHUTDOWN)
    161 			break;
    162 		s = splbio();
    163 		if (chp->ch_flags & WDCF_TH_RESET) {
    164 			/*
    165 			 * wdc_reset_channel() will freeze 2 times, so
    166 			 * unfreeze one time. Not a problem as we're at splbio
    167 			 */
    168 			chp->ch_queue->queue_freeze--;
    169 			wdc_reset_channel(chp, AT_WAIT | chp->ch_reset_flags);
    170 		} else if (chp->ch_queue->active_xfer != NULL &&
    171 			   chp->ch_queue->queue_freeze == 1) {
    172 			/*
    173 			 * Caller has bumped queue_freeze, decrease it.
    174 			 */
    175 			chp->ch_queue->queue_freeze--;
    176 			xfer = chp->ch_queue->active_xfer;
    177 			KASSERT(xfer != NULL);
    178 			(*xfer->c_start)(chp, xfer);
    179 		} else if (chp->ch_queue->queue_freeze > 1)
    180 			panic("ata_thread: queue_freeze");
    181 		splx(s);
    182 	}
    183 	chp->ch_thread = NULL;
    184 	wakeup((void *)&chp->ch_flags);
    185 	kthread_exit(0);
    186 }
    187 
    188 /*
    189  * atabus_create_thread:
    190  *
    191  *	Helper routine to create the ATA bus worker thread.
    192  */
    193 static void
    194 atabus_create_thread(void *arg)
    195 {
    196 	struct atabus_softc *sc = arg;
    197 	struct wdc_channel *chp = sc->sc_chan;
    198 	int error;
    199 
    200 	if ((error = kthread_create1(atabus_thread, sc, &chp->ch_thread,
    201 				     "%s", sc->sc_dev.dv_xname)) != 0)
    202 		aprint_error("%s: unable to create kernel thread: error %d\n",
    203 		    sc->sc_dev.dv_xname, error);
    204 }
    205 
    206 /*
    207  * atabus_match:
    208  *
    209  *	Autoconfiguration match routine.
    210  */
    211 static int
    212 atabus_match(struct device *parent, struct cfdata *cf, void *aux)
    213 {
    214 	struct wdc_channel *chp = aux;
    215 
    216 	if (chp == NULL)
    217 		return (0);
    218 
    219 	if (cf->cf_loc[ATACF_CHANNEL] != chp->ch_channel &&
    220 	    cf->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT)
    221 	    	return (0);
    222 
    223 	return (1);
    224 }
    225 
    226 /*
    227  * atabus_attach:
    228  *
    229  *	Autoconfiguration attach routine.
    230  */
    231 static void
    232 atabus_attach(struct device *parent, struct device *self, void *aux)
    233 {
    234 	struct atabus_softc *sc = (void *) self;
    235 	struct wdc_channel *chp = aux;
    236 	struct atabus_initq *initq;
    237 
    238 	sc->sc_chan = chp;
    239 
    240 	aprint_normal("\n");
    241 	aprint_naive("\n");
    242 
    243         if (ata_addref(chp))
    244                 return;
    245 
    246 	initq = malloc(sizeof(*initq), M_DEVBUF, M_WAITOK);
    247 	initq->atabus_sc = sc;
    248 	TAILQ_INSERT_TAIL(&atabus_initq_head, initq, atabus_initq);
    249 	config_pending_incr();
    250 	kthread_create(atabus_create_thread, sc);
    251 }
    252 
    253 /*
    254  * atabus_activate:
    255  *
    256  *	Autoconfiguration activation routine.
    257  */
    258 static int
    259 atabus_activate(struct device *self, enum devact act)
    260 {
    261 	struct atabus_softc *sc = (void *) self;
    262 	struct wdc_channel *chp = sc->sc_chan;
    263 	struct device *dev = NULL;
    264 	int s, i, error = 0;
    265 
    266 	s = splbio();
    267 	switch (act) {
    268 	case DVACT_ACTIVATE:
    269 		error = EOPNOTSUPP;
    270 		break;
    271 
    272 	case DVACT_DEACTIVATE:
    273 		/*
    274 		 * We might deactivate the children of atapibus twice
    275 		 * (once bia atapibus, once directly), but since the
    276 		 * generic autoconfiguration code maintains the DVF_ACTIVE
    277 		 * flag, it's safe.
    278 		 */
    279 		if ((dev = chp->atapibus) != NULL) {
    280 			error = config_deactivate(dev);
    281 			if (error)
    282 				goto out;
    283 		}
    284 
    285 		for (i = 0; i < 2; i++) {
    286 			if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
    287 				WDCDEBUG_PRINT(("atabus_activate: %s: "
    288 				    "deactivating %s\n", sc->sc_dev.dv_xname,
    289 				    dev->dv_xname),
    290 				    DEBUG_DETACH);
    291 				error = config_deactivate(dev);
    292 				if (error)
    293 					goto out;
    294 			}
    295 		}
    296 		break;
    297 	}
    298  out:
    299 	splx(s);
    300 
    301 #ifdef WDCDEBUG
    302 	if (dev != NULL && error != 0)
    303 		WDCDEBUG_PRINT(("atabus_activate: %s: "
    304 		    "error %d deactivating %s\n", sc->sc_dev.dv_xname,
    305 		    error, dev->dv_xname), DEBUG_DETACH);
    306 #endif /* WDCDEBUG */
    307 
    308 	return (error);
    309 }
    310 
    311 /*
    312  * atabus_detach:
    313  *
    314  *	Autoconfiguration detach routine.
    315  */
    316 static int
    317 atabus_detach(struct device *self, int flags)
    318 {
    319 	struct atabus_softc *sc = (void *) self;
    320 	struct wdc_channel *chp = sc->sc_chan;
    321 	struct device *dev = NULL;
    322 	int i, error = 0;
    323 
    324 	/* Shutdown the channel. */
    325 	/* XXX NEED AN INTERLOCK HERE. */
    326 	chp->ch_flags |= WDCF_SHUTDOWN;
    327 	wakeup(&chp->ch_thread);
    328 	while (chp->ch_thread != NULL)
    329 		(void) tsleep((void *)&chp->ch_flags, PRIBIO, "atadown", 0);
    330 
    331 	/*
    332 	 * Detach atapibus and its children.
    333 	 */
    334 	if ((dev = chp->atapibus) != NULL) {
    335 		WDCDEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
    336 		    sc->sc_dev.dv_xname, dev->dv_xname), DEBUG_DETACH);
    337 		error = config_detach(dev, flags);
    338 		if (error)
    339 			goto out;
    340 	}
    341 
    342 	/*
    343 	 * Detach our other children.
    344 	 */
    345 	for (i = 0; i < 2; i++) {
    346 		if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI)
    347 			continue;
    348 		if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
    349 			WDCDEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
    350 			    sc->sc_dev.dv_xname, dev->dv_xname),
    351 			    DEBUG_DETACH);
    352 			error = config_detach(dev, flags);
    353 			if (error)
    354 				goto out;
    355 		}
    356 	}
    357 
    358  out:
    359 #ifdef WDCDEBUG
    360 	if (dev != NULL && error != 0)
    361 		WDCDEBUG_PRINT(("atabus_detach: %s: error %d detaching %s\n",
    362 		    sc->sc_dev.dv_xname, error, dev->dv_xname),
    363 		    DEBUG_DETACH);
    364 #endif /* WDCDEBUG */
    365 
    366 	return (error);
    367 }
    368 
    369 CFATTACH_DECL(atabus, sizeof(struct atabus_softc),
    370     atabus_match, atabus_attach, atabus_detach, atabus_activate);
    371 
    372 /*****************************************************************************
    373  * Common ATA bus operations.
    374  *****************************************************************************/
    375 
    376 /* Get the disk's parameters */
    377 int
    378 ata_get_params(struct ata_drive_datas *drvp, u_int8_t flags,
    379     struct ataparams *prms)
    380 {
    381 	char tb[DEV_BSIZE];
    382 	struct ata_command ata_c;
    383 
    384 #if BYTE_ORDER == LITTLE_ENDIAN
    385 	int i;
    386 	u_int16_t *p;
    387 #endif
    388 
    389 	WDCDEBUG_PRINT(("ata_get_parms\n"), DEBUG_FUNCS);
    390 
    391 	memset(tb, 0, DEV_BSIZE);
    392 	memset(prms, 0, sizeof(struct ataparams));
    393 	memset(&ata_c, 0, sizeof(struct ata_command));
    394 
    395 	if (drvp->drive_flags & DRIVE_ATA) {
    396 		ata_c.r_command = WDCC_IDENTIFY;
    397 		ata_c.r_st_bmask = WDCS_DRDY;
    398 		ata_c.r_st_pmask = 0;
    399 		ata_c.timeout = 3000; /* 3s */
    400 	} else if (drvp->drive_flags & DRIVE_ATAPI) {
    401 		ata_c.r_command = ATAPI_IDENTIFY_DEVICE;
    402 		ata_c.r_st_bmask = 0;
    403 		ata_c.r_st_pmask = 0;
    404 		ata_c.timeout = 10000; /* 10s */
    405 	} else {
    406 		WDCDEBUG_PRINT(("ata_get_parms: no disks\n"),
    407 		    DEBUG_FUNCS|DEBUG_PROBE);
    408 		return CMD_ERR;
    409 	}
    410 	ata_c.flags = AT_READ | flags;
    411 	ata_c.data = tb;
    412 	ata_c.bcount = DEV_BSIZE;
    413 	if (wdc_exec_command(drvp, &ata_c) != ATACMD_COMPLETE) {
    414 		WDCDEBUG_PRINT(("ata_get_parms: wdc_exec_command failed\n"),
    415 		    DEBUG_FUNCS|DEBUG_PROBE);
    416 		return CMD_AGAIN;
    417 	}
    418 	if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    419 		WDCDEBUG_PRINT(("ata_get_parms: ata_c.flags=0x%x\n",
    420 		    ata_c.flags), DEBUG_FUNCS|DEBUG_PROBE);
    421 		return CMD_ERR;
    422 	} else {
    423 		/* if we didn't read any data something is wrong */
    424 		if ((ata_c.flags & AT_XFDONE) == 0)
    425 			return CMD_ERR;
    426 		/* Read in parameter block. */
    427 		memcpy(prms, tb, sizeof(struct ataparams));
    428 #if BYTE_ORDER == LITTLE_ENDIAN
    429 		/*
    430 		 * Shuffle string byte order.
    431 		 * ATAPI Mitsumi and NEC drives don't need this.
    432 		 */
    433 		if ((prms->atap_config & WDC_CFG_ATAPI_MASK) ==
    434 		    WDC_CFG_ATAPI &&
    435 		    ((prms->atap_model[0] == 'N' &&
    436 			prms->atap_model[1] == 'E') ||
    437 		     (prms->atap_model[0] == 'F' &&
    438 			 prms->atap_model[1] == 'X')))
    439 			return 0;
    440 		for (i = 0; i < sizeof(prms->atap_model); i += 2) {
    441 			p = (u_short *)(prms->atap_model + i);
    442 			*p = ntohs(*p);
    443 		}
    444 		for (i = 0; i < sizeof(prms->atap_serial); i += 2) {
    445 			p = (u_short *)(prms->atap_serial + i);
    446 			*p = ntohs(*p);
    447 		}
    448 		for (i = 0; i < sizeof(prms->atap_revision); i += 2) {
    449 			p = (u_short *)(prms->atap_revision + i);
    450 			*p = ntohs(*p);
    451 		}
    452 #endif
    453 		return CMD_OK;
    454 	}
    455 }
    456 
    457 int
    458 ata_set_mode(struct ata_drive_datas *drvp, u_int8_t mode, u_int8_t flags)
    459 {
    460 	struct ata_command ata_c;
    461 
    462 	WDCDEBUG_PRINT(("ata_set_mode=0x%x\n", mode), DEBUG_FUNCS);
    463 	memset(&ata_c, 0, sizeof(struct ata_command));
    464 
    465 	ata_c.r_command = SET_FEATURES;
    466 	ata_c.r_st_bmask = 0;
    467 	ata_c.r_st_pmask = 0;
    468 	ata_c.r_features = WDSF_SET_MODE;
    469 	ata_c.r_count = mode;
    470 	ata_c.flags = flags;
    471 	ata_c.timeout = 1000; /* 1s */
    472 	if (wdc_exec_command(drvp, &ata_c) != ATACMD_COMPLETE)
    473 		return CMD_AGAIN;
    474 	if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    475 		return CMD_ERR;
    476 	}
    477 	return CMD_OK;
    478 }
    479 
    480 void
    481 ata_dmaerr(struct ata_drive_datas *drvp, int flags)
    482 {
    483 	/*
    484 	 * Downgrade decision: if we get NERRS_MAX in NXFER.
    485 	 * We start with n_dmaerrs set to NERRS_MAX-1 so that the
    486 	 * first error within the first NXFER ops will immediatly trigger
    487 	 * a downgrade.
    488 	 * If we got an error and n_xfers is bigger than NXFER reset counters.
    489 	 */
    490 	drvp->n_dmaerrs++;
    491 	if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) {
    492 		ata_downgrade_mode(drvp, flags);
    493 		drvp->n_dmaerrs = NERRS_MAX-1;
    494 		drvp->n_xfers = 0;
    495 		return;
    496 	}
    497 	if (drvp->n_xfers > NXFER) {
    498 		drvp->n_dmaerrs = 1; /* just got an error */
    499 		drvp->n_xfers = 1; /* restart counting from this error */
    500 	}
    501 }
    502 
    503 struct ata_xfer *
    504 ata_get_xfer(int flags)
    505 {
    506 	struct ata_xfer *xfer;
    507 	int s;
    508 
    509 	s = splbio();
    510 	xfer = pool_get(&ata_xfer_pool,
    511 	    ((flags & ATAXF_NOSLEEP) != 0 ? PR_NOWAIT : PR_WAITOK));
    512 	splx(s);
    513 	if (xfer != NULL) {
    514 		memset(xfer, 0, sizeof(struct ata_xfer));
    515 	}
    516 	return xfer;
    517 }
    518 
    519 void
    520 ata_free_xfer(struct wdc_channel *chp, struct ata_xfer *xfer)
    521 {
    522 	struct wdc_softc *wdc = chp->ch_wdc;
    523 	int s;
    524 
    525 	if (wdc->cap & WDC_CAPABILITY_HWLOCK)
    526 		(*wdc->free_hw)(chp);
    527 	s = splbio();
    528 	pool_put(&ata_xfer_pool, xfer);
    529 	splx(s);
    530 }
    531 
    532 /*
    533  * Kill off all pending xfers for a wdc_channel.
    534  *
    535  * Must be called at splbio().
    536  */
    537 void
    538 ata_kill_pending(struct ata_drive_datas *drvp)
    539 {
    540 	struct wdc_channel *chp = drvp->chnl_softc;
    541 	struct ata_xfer *xfer, *next_xfer;
    542 	int s = splbio();
    543 
    544 	for (xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer);
    545 	    xfer != NULL; xfer = next_xfer) {
    546 		next_xfer = TAILQ_NEXT(xfer, c_xferchain);
    547 		if (xfer->c_chp != chp || xfer->c_drive != drvp->drive)
    548 			continue;
    549 		TAILQ_REMOVE(&chp->ch_queue->queue_xfer, xfer, c_xferchain);
    550 		(*xfer->c_kill_xfer)(chp, xfer, KILL_GONE);
    551 	}
    552 
    553 	while ((xfer = chp->ch_queue->active_xfer) != NULL) {
    554 		if (xfer->c_chp == chp && xfer->c_drive == drvp->drive) {
    555 			drvp->drive_flags |= DRIVE_WAITDRAIN;
    556 			(void) tsleep(&chp->ch_queue->active_xfer,
    557 			    PRIBIO, "atdrn", 0);
    558 		} else {
    559 			/* no more xfer for us */
    560 			break;
    561 		}
    562 	}
    563 	splx(s);
    564 }
    565 
    566 int
    567 ata_addref(struct wdc_channel *chp)
    568 {
    569 	struct wdc_softc *wdc = chp->ch_wdc;
    570 	struct scsipi_adapter *adapt = &wdc->sc_atapi_adapter._generic;
    571 	int s, error = 0;
    572 
    573 	s = splbio();
    574 	if (adapt->adapt_refcnt++ == 0 &&
    575 	    adapt->adapt_enable != NULL) {
    576 		error = (*adapt->adapt_enable)(&wdc->sc_dev, 1);
    577 		if (error)
    578 			adapt->adapt_refcnt--;
    579 	}
    580 	splx(s);
    581 	return (error);
    582 }
    583 
    584 void
    585 ata_delref(struct wdc_channel *chp)
    586 {
    587 	struct wdc_softc *wdc = chp->ch_wdc;
    588 	struct scsipi_adapter *adapt = &wdc->sc_atapi_adapter._generic;
    589 	int s;
    590 
    591 	s = splbio();
    592 	if (adapt->adapt_refcnt-- == 1 &&
    593 	    adapt->adapt_enable != NULL)
    594 		(void) (*adapt->adapt_enable)(&wdc->sc_dev, 0);
    595 	splx(s);
    596 }
    597 
    598 void
    599 ata_print_modes(struct wdc_channel *chp)
    600 {
    601 	struct wdc_softc *wdc = chp->ch_wdc;
    602 	int drive;
    603 	struct ata_drive_datas *drvp;
    604 
    605 	for (drive = 0; drive < 2; drive++) {
    606 		drvp = &chp->ch_drive[drive];
    607 		if ((drvp->drive_flags & DRIVE) == 0)
    608 			continue;
    609 		aprint_normal("%s(%s:%d:%d): using PIO mode %d",
    610 			drvp->drv_softc->dv_xname,
    611 			wdc->sc_dev.dv_xname,
    612 			chp->ch_channel, drive, drvp->PIO_mode);
    613 		if (drvp->drive_flags & DRIVE_DMA)
    614 			aprint_normal(", DMA mode %d", drvp->DMA_mode);
    615 		if (drvp->drive_flags & DRIVE_UDMA) {
    616 			aprint_normal(", Ultra-DMA mode %d", drvp->UDMA_mode);
    617 			if (drvp->UDMA_mode == 2)
    618 				aprint_normal(" (Ultra/33)");
    619 			else if (drvp->UDMA_mode == 4)
    620 				aprint_normal(" (Ultra/66)");
    621 			else if (drvp->UDMA_mode == 5)
    622 				aprint_normal(" (Ultra/100)");
    623 			else if (drvp->UDMA_mode == 6)
    624 				aprint_normal(" (Ultra/133)");
    625 		}
    626 		if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA))
    627 			aprint_normal(" (using DMA data transfers)");
    628 		aprint_normal("\n");
    629 	}
    630 }
    631 
    632 /*
    633  * downgrade the transfer mode of a drive after an error. return 1 if
    634  * downgrade was possible, 0 otherwise.
    635  */
    636 int
    637 ata_downgrade_mode(struct ata_drive_datas *drvp, int flags)
    638 {
    639 	struct wdc_channel *chp = drvp->chnl_softc;
    640 	struct wdc_softc *wdc = chp->ch_wdc;
    641 	struct device *drv_dev = drvp->drv_softc;
    642 	int cf_flags = drv_dev->dv_cfdata->cf_flags;
    643 
    644 	/* if drive or controller don't know its mode, we can't do much */
    645 	if ((drvp->drive_flags & DRIVE_MODE) == 0 ||
    646 	    (wdc->cap & WDC_CAPABILITY_MODE) == 0)
    647 		return 0;
    648 	/* current drive mode was set by a config flag, let it this way */
    649 	if ((cf_flags & ATA_CONFIG_PIO_SET) ||
    650 	    (cf_flags & ATA_CONFIG_DMA_SET) ||
    651 	    (cf_flags & ATA_CONFIG_UDMA_SET))
    652 		return 0;
    653 
    654 	/*
    655 	 * If we were using Ultra-DMA mode, downgrade to the next lower mode.
    656 	 */
    657 	if ((drvp->drive_flags & DRIVE_UDMA) && drvp->UDMA_mode >= 2) {
    658 		drvp->UDMA_mode--;
    659 		printf("%s: transfer error, downgrading to Ultra-DMA mode %d\n",
    660 		    drv_dev->dv_xname, drvp->UDMA_mode);
    661 	}
    662 
    663 	/*
    664 	 * If we were using ultra-DMA, don't downgrade to multiword DMA.
    665 	 */
    666 	else if (drvp->drive_flags & (DRIVE_DMA | DRIVE_UDMA)) {
    667 		drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
    668 		drvp->PIO_mode = drvp->PIO_cap;
    669 		printf("%s: transfer error, downgrading to PIO mode %d\n",
    670 		    drv_dev->dv_xname, drvp->PIO_mode);
    671 	} else /* already using PIO, can't downgrade */
    672 		return 0;
    673 
    674 	wdc->set_modes(chp);
    675 	ata_print_modes(chp);
    676 	/* reset the channel, which will shedule all drives for setup */
    677 	wdc_reset_channel(chp, flags | AT_RST_NOCMD);
    678 	return 1;
    679 }
    680 
    681 /*
    682  * Probe drive's capabilities, for use by the controller later
    683  * Assumes drvp points to an existing drive.
    684  */
    685 void
    686 ata_probe_caps(struct ata_drive_datas *drvp)
    687 {
    688 	struct ataparams params, params2;
    689 	struct wdc_channel *chp = drvp->chnl_softc;
    690 	struct wdc_softc *wdc = chp->ch_wdc;
    691 	struct device *drv_dev = drvp->drv_softc;
    692 	int i, printed;
    693 	char *sep = "";
    694 	int cf_flags;
    695 
    696 	if (ata_get_params(drvp, AT_WAIT, &params) != CMD_OK) {
    697 		/* IDENTIFY failed. Can't tell more about the device */
    698 		return;
    699 	}
    700 	if ((wdc->cap & (WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32)) ==
    701 	    (WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32)) {
    702 		/*
    703 		 * Controller claims 16 and 32 bit transfers.
    704 		 * Re-do an IDENTIFY with 32-bit transfers,
    705 		 * and compare results.
    706 		 */
    707 		drvp->drive_flags |= DRIVE_CAP32;
    708 		ata_get_params(drvp, AT_WAIT, &params2);
    709 		if (memcmp(&params, &params2, sizeof(struct ataparams)) != 0) {
    710 			/* Not good. fall back to 16bits */
    711 			drvp->drive_flags &= ~DRIVE_CAP32;
    712 		} else {
    713 			aprint_normal("%s: 32-bit data port\n",
    714 			    drv_dev->dv_xname);
    715 		}
    716 	}
    717 #if 0 /* Some ultra-DMA drives claims to only support ATA-3. sigh */
    718 	if (params.atap_ata_major > 0x01 &&
    719 	    params.atap_ata_major != 0xffff) {
    720 		for (i = 14; i > 0; i--) {
    721 			if (params.atap_ata_major & (1 << i)) {
    722 				aprint_normal("%s: ATA version %d\n",
    723 				    drv_dev->dv_xname, i);
    724 				drvp->ata_vers = i;
    725 				break;
    726 			}
    727 		}
    728 	}
    729 #endif
    730 
    731 	/* An ATAPI device is at last PIO mode 3 */
    732 	if (drvp->drive_flags & DRIVE_ATAPI)
    733 		drvp->PIO_mode = 3;
    734 
    735 	/*
    736 	 * It's not in the specs, but it seems that some drive
    737 	 * returns 0xffff in atap_extensions when this field is invalid
    738 	 */
    739 	if (params.atap_extensions != 0xffff &&
    740 	    (params.atap_extensions & WDC_EXT_MODES)) {
    741 		printed = 0;
    742 		/*
    743 		 * XXX some drives report something wrong here (they claim to
    744 		 * support PIO mode 8 !). As mode is coded on 3 bits in
    745 		 * SET FEATURE, limit it to 7 (so limit i to 4).
    746 		 * If higher mode than 7 is found, abort.
    747 		 */
    748 		for (i = 7; i >= 0; i--) {
    749 			if ((params.atap_piomode_supp & (1 << i)) == 0)
    750 				continue;
    751 			if (i > 4)
    752 				return;
    753 			/*
    754 			 * See if mode is accepted.
    755 			 * If the controller can't set its PIO mode,
    756 			 * assume the defaults are good, so don't try
    757 			 * to set it
    758 			 */
    759 			if ((wdc->cap & WDC_CAPABILITY_MODE) != 0)
    760 				/*
    761 				 * It's OK to pool here, it's fast enouth
    762 				 * to not bother waiting for interrupt
    763 				 */
    764 				if (ata_set_mode(drvp, 0x08 | (i + 3),
    765 				   AT_WAIT) != CMD_OK)
    766 					continue;
    767 			if (!printed) {
    768 				aprint_normal("%s: drive supports PIO mode %d",
    769 				    drv_dev->dv_xname, i + 3);
    770 				sep = ",";
    771 				printed = 1;
    772 			}
    773 			/*
    774 			 * If controller's driver can't set its PIO mode,
    775 			 * get the highter one for the drive.
    776 			 */
    777 			if ((wdc->cap & WDC_CAPABILITY_MODE) == 0 ||
    778 			    wdc->PIO_cap >= i + 3) {
    779 				drvp->PIO_mode = i + 3;
    780 				drvp->PIO_cap = i + 3;
    781 				break;
    782 			}
    783 		}
    784 		if (!printed) {
    785 			/*
    786 			 * We didn't find a valid PIO mode.
    787 			 * Assume the values returned for DMA are buggy too
    788 			 */
    789 			return;
    790 		}
    791 		drvp->drive_flags |= DRIVE_MODE;
    792 		printed = 0;
    793 		for (i = 7; i >= 0; i--) {
    794 			if ((params.atap_dmamode_supp & (1 << i)) == 0)
    795 				continue;
    796 			if ((wdc->cap & WDC_CAPABILITY_DMA) &&
    797 			    (wdc->cap & WDC_CAPABILITY_MODE))
    798 				if (ata_set_mode(drvp, 0x20 | i, AT_WAIT)
    799 				    != CMD_OK)
    800 					continue;
    801 			if (!printed) {
    802 				aprint_normal("%s DMA mode %d", sep, i);
    803 				sep = ",";
    804 				printed = 1;
    805 			}
    806 			if (wdc->cap & WDC_CAPABILITY_DMA) {
    807 				if ((wdc->cap & WDC_CAPABILITY_MODE) &&
    808 				    wdc->DMA_cap < i)
    809 					continue;
    810 				drvp->DMA_mode = i;
    811 				drvp->DMA_cap = i;
    812 				drvp->drive_flags |= DRIVE_DMA;
    813 			}
    814 			break;
    815 		}
    816 		if (params.atap_extensions & WDC_EXT_UDMA_MODES) {
    817 			printed = 0;
    818 			for (i = 7; i >= 0; i--) {
    819 				if ((params.atap_udmamode_supp & (1 << i))
    820 				    == 0)
    821 					continue;
    822 				if ((wdc->cap & WDC_CAPABILITY_MODE) &&
    823 				    (wdc->cap & WDC_CAPABILITY_UDMA))
    824 					if (ata_set_mode(drvp, 0x40 | i,
    825 					    AT_WAIT) != CMD_OK)
    826 						continue;
    827 				if (!printed) {
    828 					aprint_normal("%s Ultra-DMA mode %d",
    829 					    sep, i);
    830 					if (i == 2)
    831 						aprint_normal(" (Ultra/33)");
    832 					else if (i == 4)
    833 						aprint_normal(" (Ultra/66)");
    834 					else if (i == 5)
    835 						aprint_normal(" (Ultra/100)");
    836 					else if (i == 6)
    837 						aprint_normal(" (Ultra/133)");
    838 					sep = ",";
    839 					printed = 1;
    840 				}
    841 				if (wdc->cap & WDC_CAPABILITY_UDMA) {
    842 					if ((wdc->cap & WDC_CAPABILITY_MODE) &&
    843 					    wdc->UDMA_cap < i)
    844 						continue;
    845 					drvp->UDMA_mode = i;
    846 					drvp->UDMA_cap = i;
    847 					drvp->drive_flags |= DRIVE_UDMA;
    848 				}
    849 				break;
    850 			}
    851 		}
    852 		aprint_normal("\n");
    853 	}
    854 
    855 	drvp->drive_flags &= ~DRIVE_NOSTREAM;
    856 	if (drvp->drive_flags & DRIVE_ATAPI) {
    857 		if (wdc->cap & WDC_CAPABILITY_ATAPI_NOSTREAM)
    858 			drvp->drive_flags |= DRIVE_NOSTREAM;
    859 	} else {
    860 		if (wdc->cap & WDC_CAPABILITY_ATA_NOSTREAM)
    861 			drvp->drive_flags |= DRIVE_NOSTREAM;
    862 	}
    863 
    864 	/* Try to guess ATA version here, if it didn't get reported */
    865 	if (drvp->ata_vers == 0) {
    866 		if (drvp->drive_flags & DRIVE_UDMA)
    867 			drvp->ata_vers = 4; /* should be at last ATA-4 */
    868 		else if (drvp->PIO_cap > 2)
    869 			drvp->ata_vers = 2; /* should be at last ATA-2 */
    870 	}
    871 	cf_flags = drv_dev->dv_cfdata->cf_flags;
    872 	if (cf_flags & ATA_CONFIG_PIO_SET) {
    873 		drvp->PIO_mode =
    874 		    (cf_flags & ATA_CONFIG_PIO_MODES) >> ATA_CONFIG_PIO_OFF;
    875 		drvp->drive_flags |= DRIVE_MODE;
    876 	}
    877 	if ((wdc->cap & WDC_CAPABILITY_DMA) == 0) {
    878 		/* don't care about DMA modes */
    879 		return;
    880 	}
    881 	if (cf_flags & ATA_CONFIG_DMA_SET) {
    882 		if ((cf_flags & ATA_CONFIG_DMA_MODES) ==
    883 		    ATA_CONFIG_DMA_DISABLE) {
    884 			drvp->drive_flags &= ~DRIVE_DMA;
    885 		} else {
    886 			drvp->DMA_mode = (cf_flags & ATA_CONFIG_DMA_MODES) >>
    887 			    ATA_CONFIG_DMA_OFF;
    888 			drvp->drive_flags |= DRIVE_DMA | DRIVE_MODE;
    889 		}
    890 	}
    891 	if ((wdc->cap & WDC_CAPABILITY_UDMA) == 0) {
    892 		/* don't care about UDMA modes */
    893 		return;
    894 	}
    895 	if (cf_flags & ATA_CONFIG_UDMA_SET) {
    896 		if ((cf_flags & ATA_CONFIG_UDMA_MODES) ==
    897 		    ATA_CONFIG_UDMA_DISABLE) {
    898 			drvp->drive_flags &= ~DRIVE_UDMA;
    899 		} else {
    900 			drvp->UDMA_mode = (cf_flags & ATA_CONFIG_UDMA_MODES) >>
    901 			    ATA_CONFIG_UDMA_OFF;
    902 			drvp->drive_flags |= DRIVE_UDMA | DRIVE_MODE;
    903 		}
    904 	}
    905 }
    906 
    907 /* management of the /dev/atabus* devices */
    908 int atabusopen(dev, flag, fmt, p)
    909 	dev_t dev;
    910 	int flag, fmt;
    911 	struct proc *p;
    912 {
    913         struct atabus_softc *sc;
    914         int error, unit = minor(dev);
    915 
    916         if (unit >= atabus_cd.cd_ndevs ||
    917             (sc = atabus_cd.cd_devs[unit]) == NULL)
    918                 return (ENXIO);
    919 
    920         if (sc->sc_flags & ATABUSCF_OPEN)
    921                 return (EBUSY);
    922 
    923         if ((error = ata_addref(sc->sc_chan)) != 0)
    924                 return (error);
    925 
    926         sc->sc_flags |= ATABUSCF_OPEN;
    927 
    928         return (0);
    929 }
    930 
    931 
    932 int
    933 atabusclose(dev, flag, fmt, p)
    934         dev_t dev;
    935         int flag, fmt;
    936         struct proc *p;
    937 {
    938         struct atabus_softc *sc = atabus_cd.cd_devs[minor(dev)];
    939 
    940         ata_delref(sc->sc_chan);
    941 
    942         sc->sc_flags &= ~ATABUSCF_OPEN;
    943 
    944         return (0);
    945 }
    946 
    947 int
    948 atabusioctl(dev, cmd, addr, flag, p)
    949         dev_t dev;
    950         u_long cmd;
    951         caddr_t addr;
    952         int flag;
    953         struct proc *p;
    954 {
    955         struct atabus_softc *sc = atabus_cd.cd_devs[minor(dev)];
    956 	struct wdc_channel *chp = sc->sc_chan;
    957 	int min_drive, max_drive, drive;
    958         int error;
    959 	int s;
    960 
    961         /*
    962          * Enforce write permission for ioctls that change the
    963          * state of the bus.  Host adapter specific ioctls must
    964          * be checked by the adapter driver.
    965          */
    966         switch (cmd) {
    967         case ATABUSIOSCAN:
    968         case ATABUSIODETACH:
    969         case ATABUSIORESET:
    970                 if ((flag & FWRITE) == 0)
    971                         return (EBADF);
    972         }
    973 
    974         switch (cmd) {
    975         case ATABUSIORESET:
    976 		s = splbio();
    977 		wdc_reset_channel(sc->sc_chan, AT_WAIT | AT_POLL);
    978 		splx(s);
    979 		error = 0;
    980 		break;
    981 	case ATABUSIOSCAN:
    982 	{
    983 #if 0
    984 		struct atabusioscan_args *a=
    985 		    (struct atabusioscan_args *)addr;
    986 #endif
    987 		if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) ||
    988 		    (chp->ch_drive[1].drive_flags & DRIVE_OLD))
    989 			return (EOPNOTSUPP);
    990 		return (EOPNOTSUPP);
    991 	}
    992 	case ATABUSIODETACH:
    993 	{
    994 		struct atabusioscan_args *a=
    995 		    (struct atabusioscan_args *)addr;
    996 		if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) ||
    997 		    (chp->ch_drive[1].drive_flags & DRIVE_OLD))
    998 			return (EOPNOTSUPP);
    999 		switch (a->at_dev) {
   1000 		case -1:
   1001 			min_drive = 0;
   1002 			max_drive = 1;
   1003 			break;
   1004 		case 0:
   1005 		case 1:
   1006 			min_drive = max_drive = a->at_dev;
   1007 			break;
   1008 		default:
   1009 			return (EINVAL);
   1010 		}
   1011 		for (drive = min_drive; drive <= max_drive; drive++) {
   1012 			if (chp->ch_drive[drive].drv_softc != NULL) {
   1013 				error = config_detach(
   1014 				    chp->ch_drive[drive].drv_softc, 0);
   1015 				if (error)
   1016 					return (error);
   1017 				chp->ch_drive[drive].drv_softc = NULL;
   1018 			}
   1019 		}
   1020 		error = 0;
   1021 		break;
   1022 	}
   1023 	default:
   1024 		error = ENOTTY;
   1025 	}
   1026 	return (error);
   1027 };
   1028