Home | History | Annotate | Line # | Download | only in ata
ata.c revision 1.24
      1  1.24  thorpej /*      $NetBSD: ata.c,v 1.24 2004/01/01 17:18:54 thorpej Exp $      */
      2  1.21  thorpej 
      3   1.2   bouyer /*
      4  1.16   bouyer  * Copyright (c) 1998, 2001 Manuel Bouyer.  All rights reserved.
      5   1.2   bouyer  *
      6   1.2   bouyer  * Redistribution and use in source and binary forms, with or without
      7   1.2   bouyer  * modification, are permitted provided that the following conditions
      8   1.2   bouyer  * are met:
      9   1.2   bouyer  * 1. Redistributions of source code must retain the above copyright
     10   1.2   bouyer  *    notice, this list of conditions and the following disclaimer.
     11   1.2   bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.2   bouyer  *    notice, this list of conditions and the following disclaimer in the
     13   1.2   bouyer  *    documentation and/or other materials provided with the distribution.
     14   1.2   bouyer  * 3. All advertising materials mentioning features or use of this software
     15   1.2   bouyer  *    must display the following acknowledgement:
     16   1.2   bouyer  *  This product includes software developed by Manuel Bouyer.
     17   1.2   bouyer  * 4. The name of the author may not be used to endorse or promote products
     18   1.2   bouyer  *    derived from this software without specific prior written permission.
     19   1.2   bouyer  *
     20   1.2   bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21   1.2   bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22   1.2   bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.13   bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24   1.2   bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25   1.2   bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26   1.2   bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27   1.2   bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28   1.2   bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29   1.2   bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30   1.2   bouyer  */
     31  1.14    lukem 
     32  1.14    lukem #include <sys/cdefs.h>
     33  1.24  thorpej __KERNEL_RCSID(0, "$NetBSD: ata.c,v 1.24 2004/01/01 17:18:54 thorpej Exp $");
     34   1.2   bouyer 
     35   1.6  hubertf #ifndef WDCDEBUG
     36   1.2   bouyer #define WDCDEBUG
     37   1.6  hubertf #endif /* WDCDEBUG */
     38   1.2   bouyer 
     39   1.2   bouyer #include <sys/param.h>
     40   1.2   bouyer #include <sys/systm.h>
     41   1.2   bouyer #include <sys/kernel.h>
     42   1.2   bouyer #include <sys/malloc.h>
     43   1.2   bouyer #include <sys/device.h>
     44  1.23  thorpej #include <sys/proc.h>
     45  1.23  thorpej #include <sys/kthread.h>
     46  1.23  thorpej #include <sys/errno.h>
     47   1.2   bouyer 
     48  1.15   bouyer #include <machine/intr.h>
     49  1.15   bouyer #include <machine/bus.h>
     50  1.15   bouyer 
     51   1.2   bouyer #include <dev/ata/atareg.h>
     52   1.2   bouyer #include <dev/ata/atavar.h>
     53  1.15   bouyer #include <dev/ic/wdcreg.h>
     54  1.15   bouyer #include <dev/ic/wdcvar.h>
     55   1.2   bouyer 
     56  1.23  thorpej #include "locators.h"
     57  1.23  thorpej 
     58   1.2   bouyer #define DEBUG_FUNCS  0x08
     59   1.2   bouyer #define DEBUG_PROBE  0x10
     60  1.23  thorpej #define DEBUG_DETACH 0x20
     61   1.2   bouyer #ifdef WDCDEBUG
     62   1.2   bouyer extern int wdcdebug_mask; /* init'ed in wdc.c */
     63   1.2   bouyer #define WDCDEBUG_PRINT(args, level) \
     64   1.2   bouyer         if (wdcdebug_mask & (level)) \
     65   1.2   bouyer 		printf args
     66   1.2   bouyer #else
     67   1.2   bouyer #define WDCDEBUG_PRINT(args, level)
     68   1.2   bouyer #endif
     69   1.2   bouyer 
     70  1.23  thorpej /*****************************************************************************
     71  1.23  thorpej  * ATA bus layer.
     72  1.23  thorpej  *
     73  1.23  thorpej  * ATA controllers attach an atabus instance, which handles probing the bus
     74  1.23  thorpej  * for drives, etc.
     75  1.23  thorpej  *****************************************************************************/
     76  1.23  thorpej 
     77  1.23  thorpej /*
     78  1.23  thorpej  * atabusprint:
     79  1.23  thorpej  *
     80  1.23  thorpej  *	Autoconfiguration print routine used by ATA controllers when
     81  1.23  thorpej  *	attaching an atabus instance.
     82  1.23  thorpej  */
     83  1.23  thorpej int
     84  1.23  thorpej atabusprint(void *aux, const char *pnp)
     85  1.23  thorpej {
     86  1.23  thorpej 	struct channel_softc *chan = aux;
     87  1.23  thorpej 
     88  1.23  thorpej 	if (pnp)
     89  1.23  thorpej 		aprint_normal("atabus at %s", pnp);
     90  1.23  thorpej 	aprint_normal(" channel %d", chan->channel);
     91  1.23  thorpej 
     92  1.23  thorpej 	return (UNCONF);
     93  1.23  thorpej }
     94  1.23  thorpej 
     95  1.23  thorpej /*
     96  1.23  thorpej  * ataprint:
     97  1.23  thorpej  *
     98  1.23  thorpej  *	Autoconfiguration print routine.
     99  1.23  thorpej  */
    100  1.23  thorpej int
    101  1.23  thorpej ataprint(void *aux, const char *pnp)
    102  1.23  thorpej {
    103  1.23  thorpej 	struct ata_device *adev = aux;
    104  1.23  thorpej 
    105  1.23  thorpej 	if (pnp)
    106  1.23  thorpej 		aprint_normal("wd at %s", pnp);
    107  1.23  thorpej 	aprint_normal(" drive %d", adev->adev_drv_data->drive);
    108  1.23  thorpej 
    109  1.23  thorpej 	return (UNCONF);
    110  1.23  thorpej }
    111  1.23  thorpej 
    112  1.23  thorpej /*
    113  1.23  thorpej  * atabus_thread:
    114  1.23  thorpej  *
    115  1.23  thorpej  *	Worker thread for the ATA bus.
    116  1.23  thorpej  */
    117  1.23  thorpej static void
    118  1.23  thorpej atabus_thread(void *arg)
    119  1.23  thorpej {
    120  1.23  thorpej 	struct atabus_softc *sc = arg;
    121  1.23  thorpej 	struct channel_softc *chp = sc->sc_chan;
    122  1.24  thorpej 	struct ata_xfer *xfer;
    123  1.23  thorpej 	int s;
    124  1.23  thorpej 
    125  1.23  thorpej 	s = splbio();
    126  1.23  thorpej 	chp->ch_flags |= WDCF_TH_RUN;
    127  1.23  thorpej 	splx(s);
    128  1.23  thorpej 
    129  1.23  thorpej 	/* Configure the devices on the bus. */
    130  1.23  thorpej 	atabusconfig(sc);
    131  1.23  thorpej 
    132  1.23  thorpej 	for (;;) {
    133  1.23  thorpej 		s = splbio();
    134  1.23  thorpej 		if ((chp->ch_flags & (WDCF_TH_RESET | WDCF_SHUTDOWN)) == 0 &&
    135  1.23  thorpej 		    ((chp->ch_flags & WDCF_ACTIVE) == 0 ||
    136  1.23  thorpej 		     chp->ch_queue->queue_freeze == 0)) {
    137  1.23  thorpej 			chp->ch_flags &= ~WDCF_TH_RUN;
    138  1.23  thorpej 			(void) tsleep(&chp->thread, PRIBIO, "atath", 0);
    139  1.23  thorpej 			chp->ch_flags |= WDCF_TH_RUN;
    140  1.23  thorpej 		}
    141  1.23  thorpej 		splx(s);
    142  1.23  thorpej 		if (chp->ch_flags & WDCF_SHUTDOWN)
    143  1.23  thorpej 			break;
    144  1.23  thorpej 		s = splbio();
    145  1.23  thorpej 		if (chp->ch_flags & WDCF_TH_RESET) {
    146  1.23  thorpej 			int drive;
    147  1.23  thorpej 
    148  1.23  thorpej 			(void) wdcreset(chp, RESET_SLEEP);
    149  1.23  thorpej 			for (drive = 0; drive < 2; drive++)
    150  1.23  thorpej 				chp->ch_drive[drive].state = 0;
    151  1.23  thorpej 			chp->ch_flags &= ~WDCF_TH_RESET;
    152  1.23  thorpej 			chp->ch_queue->queue_freeze--;
    153  1.23  thorpej 			wdcstart(chp);
    154  1.23  thorpej 		} else if ((chp->ch_flags & WDCF_ACTIVE) != 0 &&
    155  1.23  thorpej 			   chp->ch_queue->queue_freeze == 1) {
    156  1.23  thorpej 			/*
    157  1.23  thorpej 			 * Caller has bumped queue_freeze, decrease it.
    158  1.23  thorpej 			 */
    159  1.23  thorpej 			chp->ch_queue->queue_freeze--;
    160  1.24  thorpej 			xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer);
    161  1.23  thorpej 			KASSERT(xfer != NULL);
    162  1.23  thorpej 			(*xfer->c_start)(chp, xfer);
    163  1.23  thorpej 		} else if (chp->ch_queue->queue_freeze > 1)
    164  1.23  thorpej 			panic("ata_thread: queue_freeze");
    165  1.23  thorpej 		splx(s);
    166  1.23  thorpej 	}
    167  1.23  thorpej 	chp->thread = NULL;
    168  1.23  thorpej 	wakeup(&chp->ch_flags);
    169  1.23  thorpej 	kthread_exit(0);
    170  1.23  thorpej }
    171  1.23  thorpej 
    172  1.23  thorpej /*
    173  1.23  thorpej  * atabus_create_thread:
    174  1.23  thorpej  *
    175  1.23  thorpej  *	Helper routine to create the ATA bus worker thread.
    176  1.23  thorpej  */
    177  1.23  thorpej static void
    178  1.23  thorpej atabus_create_thread(void *arg)
    179  1.23  thorpej {
    180  1.23  thorpej 	struct atabus_softc *sc = arg;
    181  1.23  thorpej 	struct channel_softc *chp = sc->sc_chan;
    182  1.23  thorpej 	int error;
    183  1.23  thorpej 
    184  1.23  thorpej 	if ((error = kthread_create1(atabus_thread, sc, &chp->thread,
    185  1.23  thorpej 				     "%s", sc->sc_dev.dv_xname)) != 0)
    186  1.23  thorpej 		aprint_error("%s: unable to create kernel thread: error %d\n",
    187  1.23  thorpej 		    sc->sc_dev.dv_xname, error);
    188  1.23  thorpej }
    189  1.23  thorpej 
    190  1.23  thorpej /*
    191  1.23  thorpej  * atabus_match:
    192  1.23  thorpej  *
    193  1.23  thorpej  *	Autoconfiguration match routine.
    194  1.23  thorpej  */
    195  1.23  thorpej static int
    196  1.23  thorpej atabus_match(struct device *parent, struct cfdata *cf, void *aux)
    197  1.23  thorpej {
    198  1.23  thorpej 	struct channel_softc *chp = aux;
    199  1.23  thorpej 
    200  1.23  thorpej 	if (chp == NULL)
    201  1.23  thorpej 		return (0);
    202  1.23  thorpej 
    203  1.23  thorpej 	if (cf->cf_loc[ATACF_CHANNEL] != chp->channel &&
    204  1.23  thorpej 	    cf->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT)
    205  1.23  thorpej 	    	return (0);
    206  1.23  thorpej 
    207  1.23  thorpej 	return (1);
    208  1.23  thorpej }
    209  1.23  thorpej 
    210  1.23  thorpej /*
    211  1.23  thorpej  * atabus_attach:
    212  1.23  thorpej  *
    213  1.23  thorpej  *	Autoconfiguration attach routine.
    214  1.23  thorpej  */
    215  1.23  thorpej static void
    216  1.23  thorpej atabus_attach(struct device *parent, struct device *self, void *aux)
    217  1.23  thorpej {
    218  1.23  thorpej 	struct atabus_softc *sc = (void *) self;
    219  1.23  thorpej 	struct channel_softc *chp = aux;
    220  1.23  thorpej 	struct atabus_initq *initq;
    221  1.23  thorpej 
    222  1.23  thorpej 	sc->sc_chan = chp;
    223  1.23  thorpej 
    224  1.23  thorpej 	aprint_normal("\n");
    225  1.23  thorpej 	aprint_naive("\n");
    226  1.23  thorpej 
    227  1.23  thorpej 	initq = malloc(sizeof(*initq), M_DEVBUF, M_WAITOK);
    228  1.23  thorpej 	initq->atabus_sc = sc;
    229  1.23  thorpej 	TAILQ_INSERT_TAIL(&atabus_initq_head, initq, atabus_initq);
    230  1.23  thorpej 	config_pending_incr();
    231  1.23  thorpej 	kthread_create(atabus_create_thread, sc);
    232  1.23  thorpej }
    233  1.23  thorpej 
    234  1.23  thorpej /*
    235  1.23  thorpej  * atabus_activate:
    236  1.23  thorpej  *
    237  1.23  thorpej  *	Autoconfiguration activation routine.
    238  1.23  thorpej  */
    239  1.23  thorpej static int
    240  1.23  thorpej atabus_activate(struct device *self, enum devact act)
    241  1.23  thorpej {
    242  1.23  thorpej 	struct atabus_softc *sc = (void *) self;
    243  1.23  thorpej 	struct channel_softc *chp = sc->sc_chan;
    244  1.23  thorpej 	struct device *dev = NULL;
    245  1.23  thorpej 	int s, i, error = 0;
    246  1.23  thorpej 
    247  1.23  thorpej 	s = splbio();
    248  1.23  thorpej 	switch (act) {
    249  1.23  thorpej 	case DVACT_ACTIVATE:
    250  1.23  thorpej 		error = EOPNOTSUPP;
    251  1.23  thorpej 		break;
    252  1.23  thorpej 
    253  1.23  thorpej 	case DVACT_DEACTIVATE:
    254  1.23  thorpej 		/*
    255  1.23  thorpej 		 * We might deactivate the children of atapibus twice
    256  1.23  thorpej 		 * (once bia atapibus, once directly), but since the
    257  1.23  thorpej 		 * generic autoconfiguration code maintains the DVF_ACTIVE
    258  1.23  thorpej 		 * flag, it's safe.
    259  1.23  thorpej 		 */
    260  1.23  thorpej 		if ((dev = chp->atapibus) != NULL) {
    261  1.23  thorpej 			error = config_deactivate(dev);
    262  1.23  thorpej 			if (error)
    263  1.23  thorpej 				goto out;
    264  1.23  thorpej 		}
    265  1.23  thorpej 
    266  1.23  thorpej 		for (i = 0; i < 2; i++) {
    267  1.23  thorpej 			if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
    268  1.23  thorpej 				WDCDEBUG_PRINT(("atabus_activate: %s: "
    269  1.23  thorpej 				    "deactivating %s\n", sc->sc_dev.dv_xname,
    270  1.23  thorpej 				    dev->dv_xname),
    271  1.23  thorpej 				    DEBUG_DETACH);
    272  1.23  thorpej 				error = config_deactivate(dev);
    273  1.23  thorpej 				if (error)
    274  1.23  thorpej 					goto out;
    275  1.23  thorpej 			}
    276  1.23  thorpej 		}
    277  1.23  thorpej 		break;
    278  1.23  thorpej 	}
    279  1.23  thorpej  out:
    280  1.23  thorpej 	splx(s);
    281  1.23  thorpej 
    282  1.23  thorpej #ifdef WDCDEBUG
    283  1.23  thorpej 	if (dev != NULL && error != 0)
    284  1.23  thorpej 		WDCDEBUG_PRINT(("atabus_activate: %s: "
    285  1.23  thorpej 		    "error %d deactivating %s\n", sc->sc_dev.dv_xname,
    286  1.23  thorpej 		    error, dev->dv_xname), DEBUG_DETACH);
    287  1.23  thorpej #endif /* WDCDEBUG */
    288  1.23  thorpej 
    289  1.23  thorpej 	return (error);
    290  1.23  thorpej }
    291  1.23  thorpej 
    292  1.23  thorpej /*
    293  1.23  thorpej  * atabus_detach:
    294  1.23  thorpej  *
    295  1.23  thorpej  *	Autoconfiguration detach routine.
    296  1.23  thorpej  */
    297  1.23  thorpej static int
    298  1.23  thorpej atabus_detach(struct device *self, int flags)
    299  1.23  thorpej {
    300  1.23  thorpej 	struct atabus_softc *sc = (void *) self;
    301  1.23  thorpej 	struct channel_softc *chp = sc->sc_chan;
    302  1.23  thorpej 	struct device *dev = NULL;
    303  1.23  thorpej 	int i, error = 0;
    304  1.23  thorpej 
    305  1.23  thorpej 	/* Shutdown the channel. */
    306  1.23  thorpej 	/* XXX NEED AN INTERLOCK HERE. */
    307  1.23  thorpej 	chp->ch_flags |= WDCF_SHUTDOWN;
    308  1.23  thorpej 	wakeup(&chp->thread);
    309  1.23  thorpej 	while (chp->thread != NULL)
    310  1.23  thorpej 		(void) tsleep(&chp->ch_flags, PRIBIO, "atadown", 0);
    311  1.23  thorpej 
    312  1.23  thorpej 	/*
    313  1.23  thorpej 	 * Detach atapibus and its children.
    314  1.23  thorpej 	 */
    315  1.23  thorpej 	if ((dev = chp->atapibus) != NULL) {
    316  1.23  thorpej 		WDCDEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
    317  1.23  thorpej 		    sc->sc_dev.dv_xname, dev->dv_xname), DEBUG_DETACH);
    318  1.23  thorpej 		error = config_detach(dev, flags);
    319  1.23  thorpej 		if (error)
    320  1.23  thorpej 			goto out;
    321  1.23  thorpej 	}
    322  1.23  thorpej 
    323  1.23  thorpej 	/*
    324  1.23  thorpej 	 * Detach our other children.
    325  1.23  thorpej 	 */
    326  1.23  thorpej 	for (i = 0; i < 2; i++) {
    327  1.23  thorpej 		if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI)
    328  1.23  thorpej 			continue;
    329  1.23  thorpej 		if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
    330  1.23  thorpej 			WDCDEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
    331  1.23  thorpej 			    sc->sc_dev.dv_xname, dev->dv_xname),
    332  1.23  thorpej 			    DEBUG_DETACH);
    333  1.23  thorpej 			error = config_detach(dev, flags);
    334  1.23  thorpej 			if (error)
    335  1.23  thorpej 				goto out;
    336  1.23  thorpej 		}
    337  1.23  thorpej 	}
    338  1.23  thorpej 
    339  1.23  thorpej 	wdc_kill_pending(chp);
    340  1.23  thorpej  out:
    341  1.23  thorpej #ifdef WDCDEBUG
    342  1.23  thorpej 	if (dev != NULL && error != 0)
    343  1.23  thorpej 		WDCDEBUG_PRINT(("atabus_detach: %s: error %d detaching %s\n",
    344  1.23  thorpej 		    sc->sc_dev.dv_xname, error, dev->dv_xname),
    345  1.23  thorpej 		    DEBUG_DETACH);
    346  1.23  thorpej #endif /* WDCDEBUG */
    347  1.23  thorpej 
    348  1.23  thorpej 	return (error);
    349  1.23  thorpej }
    350  1.23  thorpej 
    351  1.23  thorpej CFATTACH_DECL(atabus, sizeof(struct atabus_softc),
    352  1.23  thorpej     atabus_match, atabus_attach, atabus_detach, atabus_activate);
    353  1.23  thorpej 
    354  1.23  thorpej /*****************************************************************************
    355  1.23  thorpej  * Common ATA bus operations.
    356  1.23  thorpej  *****************************************************************************/
    357  1.23  thorpej 
    358   1.2   bouyer /* Get the disk's parameters */
    359   1.2   bouyer int
    360  1.21  thorpej ata_get_params(struct ata_drive_datas *drvp, u_int8_t flags,
    361  1.21  thorpej     struct ataparams *prms)
    362   1.2   bouyer {
    363   1.2   bouyer 	char tb[DEV_BSIZE];
    364   1.2   bouyer 	struct wdc_command wdc_c;
    365   1.2   bouyer 
    366   1.2   bouyer #if BYTE_ORDER == LITTLE_ENDIAN
    367   1.2   bouyer 	int i;
    368   1.2   bouyer 	u_int16_t *p;
    369   1.2   bouyer #endif
    370   1.2   bouyer 
    371  1.22  thorpej 	WDCDEBUG_PRINT(("ata_get_parms\n"), DEBUG_FUNCS);
    372   1.2   bouyer 
    373   1.2   bouyer 	memset(tb, 0, DEV_BSIZE);
    374   1.2   bouyer 	memset(prms, 0, sizeof(struct ataparams));
    375   1.2   bouyer 	memset(&wdc_c, 0, sizeof(struct wdc_command));
    376   1.2   bouyer 
    377   1.2   bouyer 	if (drvp->drive_flags & DRIVE_ATA) {
    378   1.2   bouyer 		wdc_c.r_command = WDCC_IDENTIFY;
    379   1.5   bouyer 		wdc_c.r_st_bmask = WDCS_DRDY;
    380  1.19  mycroft 		wdc_c.r_st_pmask = 0;
    381  1.18   bouyer 		wdc_c.timeout = 3000; /* 3s */
    382   1.7   bouyer 	} else if (drvp->drive_flags & DRIVE_ATAPI) {
    383   1.2   bouyer 		wdc_c.r_command = ATAPI_IDENTIFY_DEVICE;
    384   1.2   bouyer 		wdc_c.r_st_bmask = 0;
    385  1.19  mycroft 		wdc_c.r_st_pmask = 0;
    386   1.9   bouyer 		wdc_c.timeout = 10000; /* 10s */
    387   1.7   bouyer 	} else {
    388  1.22  thorpej 		WDCDEBUG_PRINT(("ata_get_parms: no disks\n"),
    389  1.10   bouyer 		    DEBUG_FUNCS|DEBUG_PROBE);
    390   1.7   bouyer 		return CMD_ERR;
    391   1.2   bouyer 	}
    392   1.2   bouyer 	wdc_c.flags = AT_READ | flags;
    393   1.2   bouyer 	wdc_c.data = tb;
    394   1.2   bouyer 	wdc_c.bcount = DEV_BSIZE;
    395  1.10   bouyer 	if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE) {
    396  1.22  thorpej 		WDCDEBUG_PRINT(("ata_get_parms: wdc_exec_command failed\n"),
    397  1.10   bouyer 		    DEBUG_FUNCS|DEBUG_PROBE);
    398   1.2   bouyer 		return CMD_AGAIN;
    399  1.10   bouyer 	}
    400   1.2   bouyer 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    401  1.22  thorpej 		WDCDEBUG_PRINT(("ata_get_parms: wdc_c.flags=0x%x\n",
    402  1.10   bouyer 		    wdc_c.flags), DEBUG_FUNCS|DEBUG_PROBE);
    403   1.2   bouyer 		return CMD_ERR;
    404   1.2   bouyer 	} else {
    405  1.17   bouyer 		/* if we didn't read any data something is wrong */
    406  1.17   bouyer 		if ((wdc_c.flags & AT_XFDONE) == 0)
    407  1.17   bouyer 			return CMD_ERR;
    408   1.2   bouyer 		/* Read in parameter block. */
    409   1.2   bouyer 		memcpy(prms, tb, sizeof(struct ataparams));
    410   1.2   bouyer #if BYTE_ORDER == LITTLE_ENDIAN
    411   1.2   bouyer 		/*
    412   1.2   bouyer 		 * Shuffle string byte order.
    413   1.2   bouyer 		 * ATAPI Mitsumi and NEC drives don't need this.
    414   1.2   bouyer 		 */
    415   1.2   bouyer 		if ((prms->atap_config & WDC_CFG_ATAPI_MASK) ==
    416   1.2   bouyer 		    WDC_CFG_ATAPI &&
    417   1.2   bouyer 		    ((prms->atap_model[0] == 'N' &&
    418   1.2   bouyer 			prms->atap_model[1] == 'E') ||
    419   1.2   bouyer 		     (prms->atap_model[0] == 'F' &&
    420   1.2   bouyer 			 prms->atap_model[1] == 'X')))
    421   1.2   bouyer 			return 0;
    422   1.2   bouyer 		for (i = 0; i < sizeof(prms->atap_model); i += 2) {
    423   1.2   bouyer 			p = (u_short *)(prms->atap_model + i);
    424   1.2   bouyer 			*p = ntohs(*p);
    425   1.2   bouyer 		}
    426   1.2   bouyer 		for (i = 0; i < sizeof(prms->atap_serial); i += 2) {
    427   1.2   bouyer 			p = (u_short *)(prms->atap_serial + i);
    428   1.2   bouyer 			*p = ntohs(*p);
    429   1.2   bouyer 		}
    430   1.2   bouyer 		for (i = 0; i < sizeof(prms->atap_revision); i += 2) {
    431   1.2   bouyer 			p = (u_short *)(prms->atap_revision + i);
    432   1.2   bouyer 			*p = ntohs(*p);
    433   1.2   bouyer 		}
    434   1.2   bouyer #endif
    435   1.2   bouyer 		return CMD_OK;
    436   1.2   bouyer 	}
    437   1.2   bouyer }
    438   1.2   bouyer 
    439   1.2   bouyer int
    440  1.21  thorpej ata_set_mode(struct ata_drive_datas *drvp, u_int8_t mode, u_int8_t flags)
    441   1.2   bouyer {
    442   1.2   bouyer 	struct wdc_command wdc_c;
    443   1.2   bouyer 
    444  1.22  thorpej 	WDCDEBUG_PRINT(("ata_set_mode=0x%x\n", mode), DEBUG_FUNCS);
    445   1.2   bouyer 	memset(&wdc_c, 0, sizeof(struct wdc_command));
    446   1.2   bouyer 
    447   1.2   bouyer 	wdc_c.r_command = SET_FEATURES;
    448   1.2   bouyer 	wdc_c.r_st_bmask = 0;
    449   1.2   bouyer 	wdc_c.r_st_pmask = 0;
    450   1.2   bouyer 	wdc_c.r_precomp = WDSF_SET_MODE;
    451   1.2   bouyer 	wdc_c.r_count = mode;
    452  1.19  mycroft 	wdc_c.flags = flags;
    453   1.2   bouyer 	wdc_c.timeout = 1000; /* 1s */
    454   1.2   bouyer 	if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE)
    455   1.2   bouyer 		return CMD_AGAIN;
    456   1.2   bouyer 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    457   1.2   bouyer 		return CMD_ERR;
    458   1.2   bouyer 	}
    459   1.2   bouyer 	return CMD_OK;
    460  1.11   bouyer }
    461  1.11   bouyer 
    462  1.11   bouyer void
    463  1.21  thorpej ata_dmaerr(struct ata_drive_datas *drvp, int flags)
    464  1.11   bouyer {
    465  1.11   bouyer 	/*
    466  1.11   bouyer 	 * Downgrade decision: if we get NERRS_MAX in NXFER.
    467  1.11   bouyer 	 * We start with n_dmaerrs set to NERRS_MAX-1 so that the
    468  1.11   bouyer 	 * first error within the first NXFER ops will immediatly trigger
    469  1.11   bouyer 	 * a downgrade.
    470  1.11   bouyer 	 * If we got an error and n_xfers is bigger than NXFER reset counters.
    471  1.11   bouyer 	 */
    472  1.11   bouyer 	drvp->n_dmaerrs++;
    473  1.11   bouyer 	if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) {
    474  1.20   bouyer 		wdc_downgrade_mode(drvp, flags);
    475  1.11   bouyer 		drvp->n_dmaerrs = NERRS_MAX-1;
    476  1.11   bouyer 		drvp->n_xfers = 0;
    477  1.11   bouyer 		return;
    478  1.11   bouyer 	}
    479  1.11   bouyer 	if (drvp->n_xfers > NXFER) {
    480  1.11   bouyer 		drvp->n_dmaerrs = 1; /* just got an error */
    481  1.11   bouyer 		drvp->n_xfers = 1; /* restart counting from this error */
    482   1.4   bouyer 	}
    483   1.2   bouyer }
    484