Home | History | Annotate | Line # | Download | only in ata
ata.c revision 1.30
      1 /*      $NetBSD: ata.c,v 1.30 2004/08/01 21:40:41 bouyer Exp $      */
      2 
      3 /*
      4  * Copyright (c) 1998, 2001 Manuel Bouyer.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 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.30 2004/08/01 21:40:41 bouyer 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/kthread.h>
     48 #include <sys/errno.h>
     49 #include <sys/ataio.h>
     50 
     51 #include <machine/intr.h>
     52 #include <machine/bus.h>
     53 
     54 #include <dev/ata/atareg.h>
     55 #include <dev/ata/atavar.h>
     56 #include <dev/ic/wdcreg.h>
     57 #include <dev/ic/wdcvar.h>
     58 
     59 #include "locators.h"
     60 
     61 #define DEBUG_FUNCS  0x08
     62 #define DEBUG_PROBE  0x10
     63 #define DEBUG_DETACH 0x20
     64 #ifdef WDCDEBUG
     65 extern int wdcdebug_mask; /* init'ed in wdc.c */
     66 #define WDCDEBUG_PRINT(args, level) \
     67         if (wdcdebug_mask & (level)) \
     68 		printf args
     69 #else
     70 #define WDCDEBUG_PRINT(args, level)
     71 #endif
     72 
     73 /*****************************************************************************
     74  * ATA bus layer.
     75  *
     76  * ATA controllers attach an atabus instance, which handles probing the bus
     77  * for drives, etc.
     78  *****************************************************************************/
     79 
     80 dev_type_open(atabusopen);
     81 dev_type_close(atabusclose);
     82 dev_type_ioctl(atabusioctl);
     83 
     84 const struct cdevsw atabus_cdevsw = {
     85 	atabusopen, atabusclose, noread, nowrite, atabusioctl,
     86 	nostop, notty, nopoll, nommap, nokqfilter,
     87 };
     88 
     89 extern struct cfdriver atabus_cd;
     90 
     91 
     92 /*
     93  * atabusprint:
     94  *
     95  *	Autoconfiguration print routine used by ATA controllers when
     96  *	attaching an atabus instance.
     97  */
     98 int
     99 atabusprint(void *aux, const char *pnp)
    100 {
    101 	struct wdc_channel *chan = aux;
    102 
    103 	if (pnp)
    104 		aprint_normal("atabus at %s", pnp);
    105 	aprint_normal(" channel %d", chan->ch_channel);
    106 
    107 	return (UNCONF);
    108 }
    109 
    110 /*
    111  * ataprint:
    112  *
    113  *	Autoconfiguration print routine.
    114  */
    115 int
    116 ataprint(void *aux, const char *pnp)
    117 {
    118 	struct ata_device *adev = aux;
    119 
    120 	if (pnp)
    121 		aprint_normal("wd at %s", pnp);
    122 	aprint_normal(" drive %d", adev->adev_drv_data->drive);
    123 
    124 	return (UNCONF);
    125 }
    126 
    127 /*
    128  * atabus_thread:
    129  *
    130  *	Worker thread for the ATA bus.
    131  */
    132 static void
    133 atabus_thread(void *arg)
    134 {
    135 	struct atabus_softc *sc = arg;
    136 	struct wdc_channel *chp = sc->sc_chan;
    137 	struct ata_xfer *xfer;
    138 	int s;
    139 
    140 	s = splbio();
    141 	chp->ch_flags |= WDCF_TH_RUN;
    142 	splx(s);
    143 
    144 	/* Configure the devices on the bus. */
    145 	atabusconfig(sc);
    146 
    147 	for (;;) {
    148 		s = splbio();
    149 		if ((chp->ch_flags & (WDCF_TH_RESET | WDCF_SHUTDOWN)) == 0 &&
    150 		    ((chp->ch_flags & WDCF_ACTIVE) == 0 ||
    151 		     chp->ch_queue->queue_freeze == 0)) {
    152 			chp->ch_flags &= ~WDCF_TH_RUN;
    153 			(void) tsleep(&chp->ch_thread, PRIBIO, "atath", 0);
    154 			chp->ch_flags |= WDCF_TH_RUN;
    155 		}
    156 		splx(s);
    157 		if (chp->ch_flags & WDCF_SHUTDOWN)
    158 			break;
    159 		s = splbio();
    160 		if (chp->ch_flags & WDCF_TH_RESET) {
    161 			int drive;
    162 
    163 			(void) wdcreset(chp, RESET_SLEEP);
    164 			for (drive = 0; drive < 2; drive++)
    165 				chp->ch_drive[drive].state = 0;
    166 			chp->ch_flags &= ~WDCF_TH_RESET;
    167 			chp->ch_queue->queue_freeze--;
    168 			wdcstart(chp);
    169 		} else if ((chp->ch_flags & WDCF_ACTIVE) != 0 &&
    170 			   chp->ch_queue->queue_freeze == 1) {
    171 			/*
    172 			 * Caller has bumped queue_freeze, decrease it.
    173 			 */
    174 			chp->ch_queue->queue_freeze--;
    175 			xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer);
    176 			KASSERT(xfer != NULL);
    177 			(*xfer->c_start)(chp, xfer);
    178 		} else if (chp->ch_queue->queue_freeze > 1)
    179 			panic("ata_thread: queue_freeze");
    180 		splx(s);
    181 	}
    182 	chp->ch_thread = NULL;
    183 	wakeup((void *)&chp->ch_flags);
    184 	kthread_exit(0);
    185 }
    186 
    187 /*
    188  * atabus_create_thread:
    189  *
    190  *	Helper routine to create the ATA bus worker thread.
    191  */
    192 static void
    193 atabus_create_thread(void *arg)
    194 {
    195 	struct atabus_softc *sc = arg;
    196 	struct wdc_channel *chp = sc->sc_chan;
    197 	int error;
    198 
    199 	if ((error = kthread_create1(atabus_thread, sc, &chp->ch_thread,
    200 				     "%s", sc->sc_dev.dv_xname)) != 0)
    201 		aprint_error("%s: unable to create kernel thread: error %d\n",
    202 		    sc->sc_dev.dv_xname, error);
    203 }
    204 
    205 /*
    206  * atabus_match:
    207  *
    208  *	Autoconfiguration match routine.
    209  */
    210 static int
    211 atabus_match(struct device *parent, struct cfdata *cf, void *aux)
    212 {
    213 	struct wdc_channel *chp = aux;
    214 
    215 	if (chp == NULL)
    216 		return (0);
    217 
    218 	if (cf->cf_loc[ATACF_CHANNEL] != chp->ch_channel &&
    219 	    cf->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT)
    220 	    	return (0);
    221 
    222 	return (1);
    223 }
    224 
    225 /*
    226  * atabus_attach:
    227  *
    228  *	Autoconfiguration attach routine.
    229  */
    230 static void
    231 atabus_attach(struct device *parent, struct device *self, void *aux)
    232 {
    233 	struct atabus_softc *sc = (void *) self;
    234 	struct wdc_channel *chp = aux;
    235 	struct atabus_initq *initq;
    236 
    237 	sc->sc_chan = chp;
    238 
    239 	aprint_normal("\n");
    240 	aprint_naive("\n");
    241 
    242 	initq = malloc(sizeof(*initq), M_DEVBUF, M_WAITOK);
    243 	initq->atabus_sc = sc;
    244 	TAILQ_INSERT_TAIL(&atabus_initq_head, initq, atabus_initq);
    245 	config_pending_incr();
    246 	kthread_create(atabus_create_thread, sc);
    247 }
    248 
    249 /*
    250  * atabus_activate:
    251  *
    252  *	Autoconfiguration activation routine.
    253  */
    254 static int
    255 atabus_activate(struct device *self, enum devact act)
    256 {
    257 	struct atabus_softc *sc = (void *) self;
    258 	struct wdc_channel *chp = sc->sc_chan;
    259 	struct device *dev = NULL;
    260 	int s, i, error = 0;
    261 
    262 	s = splbio();
    263 	switch (act) {
    264 	case DVACT_ACTIVATE:
    265 		error = EOPNOTSUPP;
    266 		break;
    267 
    268 	case DVACT_DEACTIVATE:
    269 		/*
    270 		 * We might deactivate the children of atapibus twice
    271 		 * (once bia atapibus, once directly), but since the
    272 		 * generic autoconfiguration code maintains the DVF_ACTIVE
    273 		 * flag, it's safe.
    274 		 */
    275 		if ((dev = chp->atapibus) != NULL) {
    276 			error = config_deactivate(dev);
    277 			if (error)
    278 				goto out;
    279 		}
    280 
    281 		for (i = 0; i < 2; i++) {
    282 			if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
    283 				WDCDEBUG_PRINT(("atabus_activate: %s: "
    284 				    "deactivating %s\n", sc->sc_dev.dv_xname,
    285 				    dev->dv_xname),
    286 				    DEBUG_DETACH);
    287 				error = config_deactivate(dev);
    288 				if (error)
    289 					goto out;
    290 			}
    291 		}
    292 		break;
    293 	}
    294  out:
    295 	splx(s);
    296 
    297 #ifdef WDCDEBUG
    298 	if (dev != NULL && error != 0)
    299 		WDCDEBUG_PRINT(("atabus_activate: %s: "
    300 		    "error %d deactivating %s\n", sc->sc_dev.dv_xname,
    301 		    error, dev->dv_xname), DEBUG_DETACH);
    302 #endif /* WDCDEBUG */
    303 
    304 	return (error);
    305 }
    306 
    307 /*
    308  * atabus_detach:
    309  *
    310  *	Autoconfiguration detach routine.
    311  */
    312 static int
    313 atabus_detach(struct device *self, int flags)
    314 {
    315 	struct atabus_softc *sc = (void *) self;
    316 	struct wdc_channel *chp = sc->sc_chan;
    317 	struct device *dev = NULL;
    318 	int i, error = 0;
    319 
    320 	/* Shutdown the channel. */
    321 	/* XXX NEED AN INTERLOCK HERE. */
    322 	chp->ch_flags |= WDCF_SHUTDOWN;
    323 	wakeup(&chp->ch_thread);
    324 	while (chp->ch_thread != NULL)
    325 		(void) tsleep((void *)&chp->ch_flags, PRIBIO, "atadown", 0);
    326 
    327 	/*
    328 	 * Detach atapibus and its children.
    329 	 */
    330 	if ((dev = chp->atapibus) != NULL) {
    331 		WDCDEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
    332 		    sc->sc_dev.dv_xname, dev->dv_xname), DEBUG_DETACH);
    333 		error = config_detach(dev, flags);
    334 		if (error)
    335 			goto out;
    336 	}
    337 
    338 	/*
    339 	 * Detach our other children.
    340 	 */
    341 	for (i = 0; i < 2; i++) {
    342 		if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI)
    343 			continue;
    344 		if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
    345 			WDCDEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
    346 			    sc->sc_dev.dv_xname, dev->dv_xname),
    347 			    DEBUG_DETACH);
    348 			error = config_detach(dev, flags);
    349 			if (error)
    350 				goto out;
    351 		}
    352 	}
    353 
    354 	wdc_kill_pending(chp);
    355  out:
    356 #ifdef WDCDEBUG
    357 	if (dev != NULL && error != 0)
    358 		WDCDEBUG_PRINT(("atabus_detach: %s: error %d detaching %s\n",
    359 		    sc->sc_dev.dv_xname, error, dev->dv_xname),
    360 		    DEBUG_DETACH);
    361 #endif /* WDCDEBUG */
    362 
    363 	return (error);
    364 }
    365 
    366 CFATTACH_DECL(atabus, sizeof(struct atabus_softc),
    367     atabus_match, atabus_attach, atabus_detach, atabus_activate);
    368 
    369 /*****************************************************************************
    370  * Common ATA bus operations.
    371  *****************************************************************************/
    372 
    373 /* Get the disk's parameters */
    374 int
    375 ata_get_params(struct ata_drive_datas *drvp, u_int8_t flags,
    376     struct ataparams *prms)
    377 {
    378 	char tb[DEV_BSIZE];
    379 	struct wdc_command wdc_c;
    380 
    381 #if BYTE_ORDER == LITTLE_ENDIAN
    382 	int i;
    383 	u_int16_t *p;
    384 #endif
    385 
    386 	WDCDEBUG_PRINT(("ata_get_parms\n"), DEBUG_FUNCS);
    387 
    388 	memset(tb, 0, DEV_BSIZE);
    389 	memset(prms, 0, sizeof(struct ataparams));
    390 	memset(&wdc_c, 0, sizeof(struct wdc_command));
    391 
    392 	if (drvp->drive_flags & DRIVE_ATA) {
    393 		wdc_c.r_command = WDCC_IDENTIFY;
    394 		wdc_c.r_st_bmask = WDCS_DRDY;
    395 		wdc_c.r_st_pmask = 0;
    396 		wdc_c.timeout = 3000; /* 3s */
    397 	} else if (drvp->drive_flags & DRIVE_ATAPI) {
    398 		wdc_c.r_command = ATAPI_IDENTIFY_DEVICE;
    399 		wdc_c.r_st_bmask = 0;
    400 		wdc_c.r_st_pmask = 0;
    401 		wdc_c.timeout = 10000; /* 10s */
    402 	} else {
    403 		WDCDEBUG_PRINT(("ata_get_parms: no disks\n"),
    404 		    DEBUG_FUNCS|DEBUG_PROBE);
    405 		return CMD_ERR;
    406 	}
    407 	wdc_c.flags = AT_READ | flags;
    408 	wdc_c.data = tb;
    409 	wdc_c.bcount = DEV_BSIZE;
    410 	if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE) {
    411 		WDCDEBUG_PRINT(("ata_get_parms: wdc_exec_command failed\n"),
    412 		    DEBUG_FUNCS|DEBUG_PROBE);
    413 		return CMD_AGAIN;
    414 	}
    415 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    416 		WDCDEBUG_PRINT(("ata_get_parms: wdc_c.flags=0x%x\n",
    417 		    wdc_c.flags), DEBUG_FUNCS|DEBUG_PROBE);
    418 		return CMD_ERR;
    419 	} else {
    420 		/* if we didn't read any data something is wrong */
    421 		if ((wdc_c.flags & AT_XFDONE) == 0)
    422 			return CMD_ERR;
    423 		/* Read in parameter block. */
    424 		memcpy(prms, tb, sizeof(struct ataparams));
    425 #if BYTE_ORDER == LITTLE_ENDIAN
    426 		/*
    427 		 * Shuffle string byte order.
    428 		 * ATAPI Mitsumi and NEC drives don't need this.
    429 		 */
    430 		if ((prms->atap_config & WDC_CFG_ATAPI_MASK) ==
    431 		    WDC_CFG_ATAPI &&
    432 		    ((prms->atap_model[0] == 'N' &&
    433 			prms->atap_model[1] == 'E') ||
    434 		     (prms->atap_model[0] == 'F' &&
    435 			 prms->atap_model[1] == 'X')))
    436 			return 0;
    437 		for (i = 0; i < sizeof(prms->atap_model); i += 2) {
    438 			p = (u_short *)(prms->atap_model + i);
    439 			*p = ntohs(*p);
    440 		}
    441 		for (i = 0; i < sizeof(prms->atap_serial); i += 2) {
    442 			p = (u_short *)(prms->atap_serial + i);
    443 			*p = ntohs(*p);
    444 		}
    445 		for (i = 0; i < sizeof(prms->atap_revision); i += 2) {
    446 			p = (u_short *)(prms->atap_revision + i);
    447 			*p = ntohs(*p);
    448 		}
    449 #endif
    450 		return CMD_OK;
    451 	}
    452 }
    453 
    454 int
    455 ata_set_mode(struct ata_drive_datas *drvp, u_int8_t mode, u_int8_t flags)
    456 {
    457 	struct wdc_command wdc_c;
    458 
    459 	WDCDEBUG_PRINT(("ata_set_mode=0x%x\n", mode), DEBUG_FUNCS);
    460 	memset(&wdc_c, 0, sizeof(struct wdc_command));
    461 
    462 	wdc_c.r_command = SET_FEATURES;
    463 	wdc_c.r_st_bmask = 0;
    464 	wdc_c.r_st_pmask = 0;
    465 	wdc_c.r_features = WDSF_SET_MODE;
    466 	wdc_c.r_count = mode;
    467 	wdc_c.flags = flags;
    468 	wdc_c.timeout = 1000; /* 1s */
    469 	if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE)
    470 		return CMD_AGAIN;
    471 	if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
    472 		return CMD_ERR;
    473 	}
    474 	return CMD_OK;
    475 }
    476 
    477 void
    478 ata_dmaerr(struct ata_drive_datas *drvp, int flags)
    479 {
    480 	/*
    481 	 * Downgrade decision: if we get NERRS_MAX in NXFER.
    482 	 * We start with n_dmaerrs set to NERRS_MAX-1 so that the
    483 	 * first error within the first NXFER ops will immediatly trigger
    484 	 * a downgrade.
    485 	 * If we got an error and n_xfers is bigger than NXFER reset counters.
    486 	 */
    487 	drvp->n_dmaerrs++;
    488 	if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) {
    489 		wdc_downgrade_mode(drvp, flags);
    490 		drvp->n_dmaerrs = NERRS_MAX-1;
    491 		drvp->n_xfers = 0;
    492 		return;
    493 	}
    494 	if (drvp->n_xfers > NXFER) {
    495 		drvp->n_dmaerrs = 1; /* just got an error */
    496 		drvp->n_xfers = 1; /* restart counting from this error */
    497 	}
    498 }
    499 
    500 /* management of the /dev/atabus* devices */
    501 int atabusopen(dev, flag, fmt, p)
    502 	dev_t dev;
    503 	int flag, fmt;
    504 	struct proc *p;
    505 {
    506         struct atabus_softc *sc;
    507         int error, unit = minor(dev);
    508 
    509         if (unit >= atabus_cd.cd_ndevs ||
    510             (sc = atabus_cd.cd_devs[unit]) == NULL)
    511                 return (ENXIO);
    512 
    513         if (sc->sc_flags & ATABUSCF_OPEN)
    514                 return (EBUSY);
    515 
    516         if ((error = wdc_addref(sc->sc_chan)) != 0)
    517                 return (error);
    518 
    519         sc->sc_flags |= ATABUSCF_OPEN;
    520 
    521         return (0);
    522 }
    523 
    524 
    525 int
    526 atabusclose(dev, flag, fmt, p)
    527         dev_t dev;
    528         int flag, fmt;
    529         struct proc *p;
    530 {
    531         struct atabus_softc *sc = atabus_cd.cd_devs[minor(dev)];
    532 
    533         wdc_delref(sc->sc_chan);
    534 
    535         sc->sc_flags &= ~ATABUSCF_OPEN;
    536 
    537         return (0);
    538 }
    539 
    540 int
    541 atabusioctl(dev, cmd, addr, flag, p)
    542         dev_t dev;
    543         u_long cmd;
    544         caddr_t addr;
    545         int flag;
    546         struct proc *p;
    547 {
    548         struct atabus_softc *sc = atabus_cd.cd_devs[minor(dev)];
    549         int error;
    550 	int s;
    551 
    552         /*
    553          * Enforce write permission for ioctls that change the
    554          * state of the bus.  Host adapter specific ioctls must
    555          * be checked by the adapter driver.
    556          */
    557         switch (cmd) {
    558         case ATABUSIOSCAN:
    559         case ATABUSIODETACH:
    560         case ATABUSIORESET:
    561                 if ((flag & FWRITE) == 0)
    562                         return (EBADF);
    563         }
    564 
    565         switch (cmd) {
    566         case ATABUSIORESET:
    567 		s = splbio();
    568 		wdc_reset_channel(sc->sc_chan, AT_WAIT | AT_POLL);
    569 		splx(s);
    570 		error = 0;
    571 		break;
    572 	default:
    573 		error = ENOTTY;
    574 	}
    575 	return (error);
    576 };
    577