2004-08-11 03:09:38 +04:00
|
|
|
/* $NetBSD: ata.c,v 1.35 2004/08/10 23:09:38 mycroft Exp $ */
|
2003-12-14 05:45:48 +03:00
|
|
|
|
1998-10-12 20:09:10 +04:00
|
|
|
/*
|
2001-12-03 03:20:22 +03:00
|
|
|
* Copyright (c) 1998, 2001 Manuel Bouyer. All rights reserved.
|
1998-10-12 20:09:10 +04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Manuel Bouyer.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
2000-05-15 12:31:33 +04:00
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
1998-10-12 20:09:10 +04:00
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2001-11-13 15:51:12 +03:00
|
|
|
#include <sys/cdefs.h>
|
2004-08-11 03:09:38 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: ata.c,v 1.35 2004/08/10 23:09:38 mycroft Exp $");
|
2001-11-13 15:51:12 +03:00
|
|
|
|
1999-02-21 03:52:04 +03:00
|
|
|
#ifndef WDCDEBUG
|
1998-10-12 20:09:10 +04:00
|
|
|
#define WDCDEBUG
|
1999-02-21 03:52:04 +03:00
|
|
|
#endif /* WDCDEBUG */
|
1998-10-12 20:09:10 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/device.h>
|
2004-08-02 01:40:41 +04:00
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/fcntl.h>
|
2003-12-30 19:28:37 +03:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/kthread.h>
|
|
|
|
#include <sys/errno.h>
|
2004-08-02 01:40:41 +04:00
|
|
|
#include <sys/ataio.h>
|
1998-10-12 20:09:10 +04:00
|
|
|
|
2001-12-03 03:11:15 +03:00
|
|
|
#include <machine/intr.h>
|
|
|
|
#include <machine/bus.h>
|
|
|
|
|
1998-10-12 20:09:10 +04:00
|
|
|
#include <dev/ata/atareg.h>
|
|
|
|
#include <dev/ata/atavar.h>
|
2001-12-03 03:11:15 +03:00
|
|
|
#include <dev/ic/wdcreg.h>
|
|
|
|
#include <dev/ic/wdcvar.h>
|
1998-10-12 20:09:10 +04:00
|
|
|
|
2003-12-30 19:28:37 +03:00
|
|
|
#include "locators.h"
|
|
|
|
|
1998-10-12 20:09:10 +04:00
|
|
|
#define DEBUG_FUNCS 0x08
|
|
|
|
#define DEBUG_PROBE 0x10
|
2003-12-30 19:28:37 +03:00
|
|
|
#define DEBUG_DETACH 0x20
|
1998-10-12 20:09:10 +04:00
|
|
|
#ifdef WDCDEBUG
|
|
|
|
extern int wdcdebug_mask; /* init'ed in wdc.c */
|
|
|
|
#define WDCDEBUG_PRINT(args, level) \
|
|
|
|
if (wdcdebug_mask & (level)) \
|
|
|
|
printf args
|
|
|
|
#else
|
|
|
|
#define WDCDEBUG_PRINT(args, level)
|
|
|
|
#endif
|
|
|
|
|
2003-12-30 19:28:37 +03:00
|
|
|
/*****************************************************************************
|
|
|
|
* ATA bus layer.
|
|
|
|
*
|
|
|
|
* ATA controllers attach an atabus instance, which handles probing the bus
|
|
|
|
* for drives, etc.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2004-08-02 01:40:41 +04:00
|
|
|
dev_type_open(atabusopen);
|
|
|
|
dev_type_close(atabusclose);
|
|
|
|
dev_type_ioctl(atabusioctl);
|
|
|
|
|
|
|
|
const struct cdevsw atabus_cdevsw = {
|
|
|
|
atabusopen, atabusclose, noread, nowrite, atabusioctl,
|
|
|
|
nostop, notty, nopoll, nommap, nokqfilter,
|
|
|
|
};
|
|
|
|
|
|
|
|
extern struct cfdriver atabus_cd;
|
|
|
|
|
|
|
|
|
2003-12-30 19:28:37 +03:00
|
|
|
/*
|
|
|
|
* atabusprint:
|
|
|
|
*
|
|
|
|
* Autoconfiguration print routine used by ATA controllers when
|
|
|
|
* attaching an atabus instance.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
atabusprint(void *aux, const char *pnp)
|
|
|
|
{
|
2004-01-03 04:50:52 +03:00
|
|
|
struct wdc_channel *chan = aux;
|
2003-12-30 19:28:37 +03:00
|
|
|
|
|
|
|
if (pnp)
|
|
|
|
aprint_normal("atabus at %s", pnp);
|
2004-01-04 01:56:52 +03:00
|
|
|
aprint_normal(" channel %d", chan->ch_channel);
|
2003-12-30 19:28:37 +03:00
|
|
|
|
|
|
|
return (UNCONF);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ataprint:
|
|
|
|
*
|
|
|
|
* Autoconfiguration print routine.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ataprint(void *aux, const char *pnp)
|
|
|
|
{
|
|
|
|
struct ata_device *adev = aux;
|
|
|
|
|
|
|
|
if (pnp)
|
|
|
|
aprint_normal("wd at %s", pnp);
|
|
|
|
aprint_normal(" drive %d", adev->adev_drv_data->drive);
|
|
|
|
|
|
|
|
return (UNCONF);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* atabus_thread:
|
|
|
|
*
|
|
|
|
* Worker thread for the ATA bus.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
atabus_thread(void *arg)
|
|
|
|
{
|
|
|
|
struct atabus_softc *sc = arg;
|
2004-01-03 04:50:52 +03:00
|
|
|
struct wdc_channel *chp = sc->sc_chan;
|
2004-01-01 20:18:53 +03:00
|
|
|
struct ata_xfer *xfer;
|
2003-12-30 19:28:37 +03:00
|
|
|
int s;
|
|
|
|
|
|
|
|
s = splbio();
|
|
|
|
chp->ch_flags |= WDCF_TH_RUN;
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
/* Configure the devices on the bus. */
|
|
|
|
atabusconfig(sc);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
s = splbio();
|
|
|
|
if ((chp->ch_flags & (WDCF_TH_RESET | WDCF_SHUTDOWN)) == 0 &&
|
2004-08-04 22:24:10 +04:00
|
|
|
(chp->ch_queue->active_xfer == NULL ||
|
2003-12-30 19:28:37 +03:00
|
|
|
chp->ch_queue->queue_freeze == 0)) {
|
|
|
|
chp->ch_flags &= ~WDCF_TH_RUN;
|
2004-01-04 02:59:58 +03:00
|
|
|
(void) tsleep(&chp->ch_thread, PRIBIO, "atath", 0);
|
2003-12-30 19:28:37 +03:00
|
|
|
chp->ch_flags |= WDCF_TH_RUN;
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
if (chp->ch_flags & WDCF_SHUTDOWN)
|
|
|
|
break;
|
|
|
|
s = splbio();
|
|
|
|
if (chp->ch_flags & WDCF_TH_RESET) {
|
2004-08-03 02:02:35 +04:00
|
|
|
/*
|
|
|
|
* wdc_reset_channel() will freeze 2 times, so
|
|
|
|
* unfreeze one time. Not a problem as we're at splbio
|
|
|
|
*/
|
2003-12-30 19:28:37 +03:00
|
|
|
chp->ch_queue->queue_freeze--;
|
2004-08-03 02:02:35 +04:00
|
|
|
wdc_reset_channel(chp, AT_WAIT | chp->ch_reset_flags);
|
2004-08-04 22:24:10 +04:00
|
|
|
} else if (chp->ch_queue->active_xfer != NULL &&
|
2003-12-30 19:28:37 +03:00
|
|
|
chp->ch_queue->queue_freeze == 1) {
|
|
|
|
/*
|
|
|
|
* Caller has bumped queue_freeze, decrease it.
|
|
|
|
*/
|
|
|
|
chp->ch_queue->queue_freeze--;
|
2004-08-04 22:24:10 +04:00
|
|
|
xfer = chp->ch_queue->active_xfer;
|
2003-12-30 19:28:37 +03:00
|
|
|
KASSERT(xfer != NULL);
|
|
|
|
(*xfer->c_start)(chp, xfer);
|
|
|
|
} else if (chp->ch_queue->queue_freeze > 1)
|
|
|
|
panic("ata_thread: queue_freeze");
|
|
|
|
splx(s);
|
|
|
|
}
|
2004-01-04 02:59:58 +03:00
|
|
|
chp->ch_thread = NULL;
|
2004-04-13 23:51:06 +04:00
|
|
|
wakeup((void *)&chp->ch_flags);
|
2003-12-30 19:28:37 +03:00
|
|
|
kthread_exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* atabus_create_thread:
|
|
|
|
*
|
|
|
|
* Helper routine to create the ATA bus worker thread.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
atabus_create_thread(void *arg)
|
|
|
|
{
|
|
|
|
struct atabus_softc *sc = arg;
|
2004-01-03 04:50:52 +03:00
|
|
|
struct wdc_channel *chp = sc->sc_chan;
|
2003-12-30 19:28:37 +03:00
|
|
|
int error;
|
|
|
|
|
2004-01-04 02:59:58 +03:00
|
|
|
if ((error = kthread_create1(atabus_thread, sc, &chp->ch_thread,
|
2003-12-30 19:28:37 +03:00
|
|
|
"%s", sc->sc_dev.dv_xname)) != 0)
|
|
|
|
aprint_error("%s: unable to create kernel thread: error %d\n",
|
|
|
|
sc->sc_dev.dv_xname, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* atabus_match:
|
|
|
|
*
|
|
|
|
* Autoconfiguration match routine.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
atabus_match(struct device *parent, struct cfdata *cf, void *aux)
|
|
|
|
{
|
2004-01-03 04:50:52 +03:00
|
|
|
struct wdc_channel *chp = aux;
|
2003-12-30 19:28:37 +03:00
|
|
|
|
|
|
|
if (chp == NULL)
|
|
|
|
return (0);
|
|
|
|
|
2004-01-04 01:56:52 +03:00
|
|
|
if (cf->cf_loc[ATACF_CHANNEL] != chp->ch_channel &&
|
2003-12-30 19:28:37 +03:00
|
|
|
cf->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* atabus_attach:
|
|
|
|
*
|
|
|
|
* Autoconfiguration attach routine.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
atabus_attach(struct device *parent, struct device *self, void *aux)
|
|
|
|
{
|
|
|
|
struct atabus_softc *sc = (void *) self;
|
2004-01-03 04:50:52 +03:00
|
|
|
struct wdc_channel *chp = aux;
|
2003-12-30 19:28:37 +03:00
|
|
|
struct atabus_initq *initq;
|
|
|
|
|
|
|
|
sc->sc_chan = chp;
|
|
|
|
|
|
|
|
aprint_normal("\n");
|
|
|
|
aprint_naive("\n");
|
|
|
|
|
2004-08-11 03:09:38 +04:00
|
|
|
if (wdc_addref(chp))
|
|
|
|
return;
|
|
|
|
|
2003-12-30 19:28:37 +03:00
|
|
|
initq = malloc(sizeof(*initq), M_DEVBUF, M_WAITOK);
|
|
|
|
initq->atabus_sc = sc;
|
|
|
|
TAILQ_INSERT_TAIL(&atabus_initq_head, initq, atabus_initq);
|
|
|
|
config_pending_incr();
|
|
|
|
kthread_create(atabus_create_thread, sc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* atabus_activate:
|
|
|
|
*
|
|
|
|
* Autoconfiguration activation routine.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
atabus_activate(struct device *self, enum devact act)
|
|
|
|
{
|
|
|
|
struct atabus_softc *sc = (void *) self;
|
2004-01-03 04:50:52 +03:00
|
|
|
struct wdc_channel *chp = sc->sc_chan;
|
2003-12-30 19:28:37 +03:00
|
|
|
struct device *dev = NULL;
|
|
|
|
int s, i, error = 0;
|
|
|
|
|
|
|
|
s = splbio();
|
|
|
|
switch (act) {
|
|
|
|
case DVACT_ACTIVATE:
|
|
|
|
error = EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DVACT_DEACTIVATE:
|
|
|
|
/*
|
|
|
|
* We might deactivate the children of atapibus twice
|
|
|
|
* (once bia atapibus, once directly), but since the
|
|
|
|
* generic autoconfiguration code maintains the DVF_ACTIVE
|
|
|
|
* flag, it's safe.
|
|
|
|
*/
|
|
|
|
if ((dev = chp->atapibus) != NULL) {
|
|
|
|
error = config_deactivate(dev);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
|
|
|
|
WDCDEBUG_PRINT(("atabus_activate: %s: "
|
|
|
|
"deactivating %s\n", sc->sc_dev.dv_xname,
|
|
|
|
dev->dv_xname),
|
|
|
|
DEBUG_DETACH);
|
|
|
|
error = config_deactivate(dev);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
#ifdef WDCDEBUG
|
|
|
|
if (dev != NULL && error != 0)
|
|
|
|
WDCDEBUG_PRINT(("atabus_activate: %s: "
|
|
|
|
"error %d deactivating %s\n", sc->sc_dev.dv_xname,
|
|
|
|
error, dev->dv_xname), DEBUG_DETACH);
|
|
|
|
#endif /* WDCDEBUG */
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* atabus_detach:
|
|
|
|
*
|
|
|
|
* Autoconfiguration detach routine.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
atabus_detach(struct device *self, int flags)
|
|
|
|
{
|
|
|
|
struct atabus_softc *sc = (void *) self;
|
2004-01-03 04:50:52 +03:00
|
|
|
struct wdc_channel *chp = sc->sc_chan;
|
2003-12-30 19:28:37 +03:00
|
|
|
struct device *dev = NULL;
|
|
|
|
int i, error = 0;
|
|
|
|
|
|
|
|
/* Shutdown the channel. */
|
|
|
|
/* XXX NEED AN INTERLOCK HERE. */
|
|
|
|
chp->ch_flags |= WDCF_SHUTDOWN;
|
2004-01-04 02:59:58 +03:00
|
|
|
wakeup(&chp->ch_thread);
|
|
|
|
while (chp->ch_thread != NULL)
|
2004-04-13 23:51:06 +04:00
|
|
|
(void) tsleep((void *)&chp->ch_flags, PRIBIO, "atadown", 0);
|
2003-12-30 19:28:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Detach atapibus and its children.
|
|
|
|
*/
|
|
|
|
if ((dev = chp->atapibus) != NULL) {
|
|
|
|
WDCDEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
|
|
|
|
sc->sc_dev.dv_xname, dev->dv_xname), DEBUG_DETACH);
|
|
|
|
error = config_detach(dev, flags);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Detach our other children.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI)
|
|
|
|
continue;
|
|
|
|
if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
|
|
|
|
WDCDEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
|
|
|
|
sc->sc_dev.dv_xname, dev->dv_xname),
|
|
|
|
DEBUG_DETACH);
|
|
|
|
error = config_detach(dev, flags);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
#ifdef WDCDEBUG
|
|
|
|
if (dev != NULL && error != 0)
|
|
|
|
WDCDEBUG_PRINT(("atabus_detach: %s: error %d detaching %s\n",
|
|
|
|
sc->sc_dev.dv_xname, error, dev->dv_xname),
|
|
|
|
DEBUG_DETACH);
|
|
|
|
#endif /* WDCDEBUG */
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
CFATTACH_DECL(atabus, sizeof(struct atabus_softc),
|
|
|
|
atabus_match, atabus_attach, atabus_detach, atabus_activate);
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Common ATA bus operations.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
1998-10-12 20:09:10 +04:00
|
|
|
/* Get the disk's parameters */
|
|
|
|
int
|
2003-12-14 05:45:48 +03:00
|
|
|
ata_get_params(struct ata_drive_datas *drvp, u_int8_t flags,
|
|
|
|
struct ataparams *prms)
|
1998-10-12 20:09:10 +04:00
|
|
|
{
|
|
|
|
char tb[DEV_BSIZE];
|
|
|
|
struct wdc_command wdc_c;
|
|
|
|
|
|
|
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
|
|
|
int i;
|
|
|
|
u_int16_t *p;
|
|
|
|
#endif
|
|
|
|
|
2003-12-30 03:43:31 +03:00
|
|
|
WDCDEBUG_PRINT(("ata_get_parms\n"), DEBUG_FUNCS);
|
1998-10-12 20:09:10 +04:00
|
|
|
|
|
|
|
memset(tb, 0, DEV_BSIZE);
|
|
|
|
memset(prms, 0, sizeof(struct ataparams));
|
|
|
|
memset(&wdc_c, 0, sizeof(struct wdc_command));
|
|
|
|
|
|
|
|
if (drvp->drive_flags & DRIVE_ATA) {
|
|
|
|
wdc_c.r_command = WDCC_IDENTIFY;
|
1999-02-08 18:22:28 +03:00
|
|
|
wdc_c.r_st_bmask = WDCS_DRDY;
|
Fix more probe delay and/or failure problems:
1) Don't wait for DRQ on an IDENTIFY command -- if it's not set when we see
BSY clear, abort the command and ignore the drive. (Do this by testing
for DRQ in the read/write cases in __wdccommand_intr().)
2) Don't wait for DRQ to deassert when we finish an IDENTIFY (or any other
non-block command that reads data) -- we don't do this for block I/O, and
empirically it doesn't clear on my CF cards at all, causing a pointless 1s
delay.
3) Add comments to some of the delay()s, and add missing ones in wdcreset()
and the WDCC_RECAL in the so-called "pre-ATA" probe.
4) Slightly simplify the reset sequence -- we were doing an extra I/O.
5) Modify the register writability test to make sure that registers are not
overlapped -- this can happen in some weird cases with a missing device 1.
6) Check the error register value after the reset -- if it's not 01h or 81h,
as appropriate (see ATA spec), punt.
Tested with a number of ATA-only, ATAPI-only, mixed ATA-ATAPI, CF, and IDE
disk configurations.
Also remove the SINGLE_DRIVE nonsense again.
2003-09-23 13:19:22 +04:00
|
|
|
wdc_c.r_st_pmask = 0;
|
2003-01-28 00:27:52 +03:00
|
|
|
wdc_c.timeout = 3000; /* 3s */
|
1999-03-10 16:11:43 +03:00
|
|
|
} else if (drvp->drive_flags & DRIVE_ATAPI) {
|
1998-10-12 20:09:10 +04:00
|
|
|
wdc_c.r_command = ATAPI_IDENTIFY_DEVICE;
|
|
|
|
wdc_c.r_st_bmask = 0;
|
Fix more probe delay and/or failure problems:
1) Don't wait for DRQ on an IDENTIFY command -- if it's not set when we see
BSY clear, abort the command and ignore the drive. (Do this by testing
for DRQ in the read/write cases in __wdccommand_intr().)
2) Don't wait for DRQ to deassert when we finish an IDENTIFY (or any other
non-block command that reads data) -- we don't do this for block I/O, and
empirically it doesn't clear on my CF cards at all, causing a pointless 1s
delay.
3) Add comments to some of the delay()s, and add missing ones in wdcreset()
and the WDCC_RECAL in the so-called "pre-ATA" probe.
4) Slightly simplify the reset sequence -- we were doing an extra I/O.
5) Modify the register writability test to make sure that registers are not
overlapped -- this can happen in some weird cases with a missing device 1.
6) Check the error register value after the reset -- if it's not 01h or 81h,
as appropriate (see ATA spec), punt.
Tested with a number of ATA-only, ATAPI-only, mixed ATA-ATAPI, CF, and IDE
disk configurations.
Also remove the SINGLE_DRIVE nonsense again.
2003-09-23 13:19:22 +04:00
|
|
|
wdc_c.r_st_pmask = 0;
|
1999-04-15 13:41:09 +04:00
|
|
|
wdc_c.timeout = 10000; /* 10s */
|
1999-03-10 16:11:43 +03:00
|
|
|
} else {
|
2003-12-30 03:43:31 +03:00
|
|
|
WDCDEBUG_PRINT(("ata_get_parms: no disks\n"),
|
1999-11-26 15:39:43 +03:00
|
|
|
DEBUG_FUNCS|DEBUG_PROBE);
|
1999-03-10 16:11:43 +03:00
|
|
|
return CMD_ERR;
|
1998-10-12 20:09:10 +04:00
|
|
|
}
|
|
|
|
wdc_c.flags = AT_READ | flags;
|
|
|
|
wdc_c.data = tb;
|
|
|
|
wdc_c.bcount = DEV_BSIZE;
|
1999-11-26 15:39:43 +03:00
|
|
|
if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE) {
|
2003-12-30 03:43:31 +03:00
|
|
|
WDCDEBUG_PRINT(("ata_get_parms: wdc_exec_command failed\n"),
|
1999-11-26 15:39:43 +03:00
|
|
|
DEBUG_FUNCS|DEBUG_PROBE);
|
1998-10-12 20:09:10 +04:00
|
|
|
return CMD_AGAIN;
|
1999-11-26 15:39:43 +03:00
|
|
|
}
|
1998-10-12 20:09:10 +04:00
|
|
|
if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
|
2003-12-30 03:43:31 +03:00
|
|
|
WDCDEBUG_PRINT(("ata_get_parms: wdc_c.flags=0x%x\n",
|
1999-11-26 15:39:43 +03:00
|
|
|
wdc_c.flags), DEBUG_FUNCS|DEBUG_PROBE);
|
1998-10-12 20:09:10 +04:00
|
|
|
return CMD_ERR;
|
|
|
|
} else {
|
2002-04-10 01:17:53 +04:00
|
|
|
/* if we didn't read any data something is wrong */
|
|
|
|
if ((wdc_c.flags & AT_XFDONE) == 0)
|
|
|
|
return CMD_ERR;
|
1998-10-12 20:09:10 +04:00
|
|
|
/* Read in parameter block. */
|
|
|
|
memcpy(prms, tb, sizeof(struct ataparams));
|
|
|
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
|
|
|
/*
|
|
|
|
* Shuffle string byte order.
|
|
|
|
* ATAPI Mitsumi and NEC drives don't need this.
|
|
|
|
*/
|
|
|
|
if ((prms->atap_config & WDC_CFG_ATAPI_MASK) ==
|
|
|
|
WDC_CFG_ATAPI &&
|
|
|
|
((prms->atap_model[0] == 'N' &&
|
|
|
|
prms->atap_model[1] == 'E') ||
|
|
|
|
(prms->atap_model[0] == 'F' &&
|
|
|
|
prms->atap_model[1] == 'X')))
|
|
|
|
return 0;
|
|
|
|
for (i = 0; i < sizeof(prms->atap_model); i += 2) {
|
|
|
|
p = (u_short *)(prms->atap_model + i);
|
|
|
|
*p = ntohs(*p);
|
|
|
|
}
|
|
|
|
for (i = 0; i < sizeof(prms->atap_serial); i += 2) {
|
|
|
|
p = (u_short *)(prms->atap_serial + i);
|
|
|
|
*p = ntohs(*p);
|
|
|
|
}
|
|
|
|
for (i = 0; i < sizeof(prms->atap_revision); i += 2) {
|
|
|
|
p = (u_short *)(prms->atap_revision + i);
|
|
|
|
*p = ntohs(*p);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return CMD_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-12-14 05:45:48 +03:00
|
|
|
ata_set_mode(struct ata_drive_datas *drvp, u_int8_t mode, u_int8_t flags)
|
1998-10-12 20:09:10 +04:00
|
|
|
{
|
|
|
|
struct wdc_command wdc_c;
|
|
|
|
|
2003-12-30 03:43:31 +03:00
|
|
|
WDCDEBUG_PRINT(("ata_set_mode=0x%x\n", mode), DEBUG_FUNCS);
|
1998-10-12 20:09:10 +04:00
|
|
|
memset(&wdc_c, 0, sizeof(struct wdc_command));
|
|
|
|
|
|
|
|
wdc_c.r_command = SET_FEATURES;
|
|
|
|
wdc_c.r_st_bmask = 0;
|
|
|
|
wdc_c.r_st_pmask = 0;
|
2004-05-27 06:23:12 +04:00
|
|
|
wdc_c.r_features = WDSF_SET_MODE;
|
1998-10-12 20:09:10 +04:00
|
|
|
wdc_c.r_count = mode;
|
Fix more probe delay and/or failure problems:
1) Don't wait for DRQ on an IDENTIFY command -- if it's not set when we see
BSY clear, abort the command and ignore the drive. (Do this by testing
for DRQ in the read/write cases in __wdccommand_intr().)
2) Don't wait for DRQ to deassert when we finish an IDENTIFY (or any other
non-block command that reads data) -- we don't do this for block I/O, and
empirically it doesn't clear on my CF cards at all, causing a pointless 1s
delay.
3) Add comments to some of the delay()s, and add missing ones in wdcreset()
and the WDCC_RECAL in the so-called "pre-ATA" probe.
4) Slightly simplify the reset sequence -- we were doing an extra I/O.
5) Modify the register writability test to make sure that registers are not
overlapped -- this can happen in some weird cases with a missing device 1.
6) Check the error register value after the reset -- if it's not 01h or 81h,
as appropriate (see ATA spec), punt.
Tested with a number of ATA-only, ATAPI-only, mixed ATA-ATAPI, CF, and IDE
disk configurations.
Also remove the SINGLE_DRIVE nonsense again.
2003-09-23 13:19:22 +04:00
|
|
|
wdc_c.flags = flags;
|
1998-10-12 20:09:10 +04:00
|
|
|
wdc_c.timeout = 1000; /* 1s */
|
|
|
|
if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE)
|
|
|
|
return CMD_AGAIN;
|
|
|
|
if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
|
|
|
|
return CMD_ERR;
|
|
|
|
}
|
|
|
|
return CMD_OK;
|
|
|
|
}
|
1999-01-18 23:06:24 +03:00
|
|
|
|
2000-01-17 03:01:00 +03:00
|
|
|
void
|
2003-12-14 05:45:48 +03:00
|
|
|
ata_dmaerr(struct ata_drive_datas *drvp, int flags)
|
2000-01-17 03:01:00 +03:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Downgrade decision: if we get NERRS_MAX in NXFER.
|
|
|
|
* We start with n_dmaerrs set to NERRS_MAX-1 so that the
|
|
|
|
* first error within the first NXFER ops will immediatly trigger
|
|
|
|
* a downgrade.
|
|
|
|
* If we got an error and n_xfers is bigger than NXFER reset counters.
|
|
|
|
*/
|
|
|
|
drvp->n_dmaerrs++;
|
|
|
|
if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) {
|
2003-10-08 14:58:12 +04:00
|
|
|
wdc_downgrade_mode(drvp, flags);
|
2000-01-17 03:01:00 +03:00
|
|
|
drvp->n_dmaerrs = NERRS_MAX-1;
|
|
|
|
drvp->n_xfers = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (drvp->n_xfers > NXFER) {
|
|
|
|
drvp->n_dmaerrs = 1; /* just got an error */
|
|
|
|
drvp->n_xfers = 1; /* restart counting from this error */
|
|
|
|
}
|
|
|
|
}
|
2004-08-02 01:40:41 +04:00
|
|
|
|
|
|
|
/* management of the /dev/atabus* devices */
|
|
|
|
int atabusopen(dev, flag, fmt, p)
|
|
|
|
dev_t dev;
|
|
|
|
int flag, fmt;
|
|
|
|
struct proc *p;
|
|
|
|
{
|
|
|
|
struct atabus_softc *sc;
|
|
|
|
int error, unit = minor(dev);
|
|
|
|
|
|
|
|
if (unit >= atabus_cd.cd_ndevs ||
|
|
|
|
(sc = atabus_cd.cd_devs[unit]) == NULL)
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
if (sc->sc_flags & ATABUSCF_OPEN)
|
|
|
|
return (EBUSY);
|
|
|
|
|
|
|
|
if ((error = wdc_addref(sc->sc_chan)) != 0)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
sc->sc_flags |= ATABUSCF_OPEN;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
atabusclose(dev, flag, fmt, p)
|
|
|
|
dev_t dev;
|
|
|
|
int flag, fmt;
|
|
|
|
struct proc *p;
|
|
|
|
{
|
|
|
|
struct atabus_softc *sc = atabus_cd.cd_devs[minor(dev)];
|
|
|
|
|
|
|
|
wdc_delref(sc->sc_chan);
|
|
|
|
|
|
|
|
sc->sc_flags &= ~ATABUSCF_OPEN;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
atabusioctl(dev, cmd, addr, flag, p)
|
|
|
|
dev_t dev;
|
|
|
|
u_long cmd;
|
|
|
|
caddr_t addr;
|
|
|
|
int flag;
|
|
|
|
struct proc *p;
|
|
|
|
{
|
|
|
|
struct atabus_softc *sc = atabus_cd.cd_devs[minor(dev)];
|
2004-08-04 02:37:19 +04:00
|
|
|
struct wdc_channel *chp = sc->sc_chan;
|
|
|
|
int min_drive, max_drive, drive;
|
2004-08-02 01:40:41 +04:00
|
|
|
int error;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enforce write permission for ioctls that change the
|
|
|
|
* state of the bus. Host adapter specific ioctls must
|
|
|
|
* be checked by the adapter driver.
|
|
|
|
*/
|
|
|
|
switch (cmd) {
|
|
|
|
case ATABUSIOSCAN:
|
|
|
|
case ATABUSIODETACH:
|
|
|
|
case ATABUSIORESET:
|
|
|
|
if ((flag & FWRITE) == 0)
|
|
|
|
return (EBADF);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case ATABUSIORESET:
|
|
|
|
s = splbio();
|
|
|
|
wdc_reset_channel(sc->sc_chan, AT_WAIT | AT_POLL);
|
|
|
|
splx(s);
|
|
|
|
error = 0;
|
|
|
|
break;
|
2004-08-04 02:37:19 +04:00
|
|
|
case ATABUSIOSCAN:
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
struct atabusioscan_args *a=
|
|
|
|
(struct atabusioscan_args *)addr;
|
|
|
|
#endif
|
|
|
|
if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) ||
|
|
|
|
(chp->ch_drive[1].drive_flags & DRIVE_OLD))
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
}
|
|
|
|
case ATABUSIODETACH:
|
|
|
|
{
|
|
|
|
struct atabusioscan_args *a=
|
|
|
|
(struct atabusioscan_args *)addr;
|
|
|
|
if ((chp->ch_drive[0].drive_flags & DRIVE_OLD) ||
|
|
|
|
(chp->ch_drive[1].drive_flags & DRIVE_OLD))
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
switch (a->at_dev) {
|
|
|
|
case -1:
|
|
|
|
min_drive = 0;
|
|
|
|
max_drive = 1;
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
min_drive = max_drive = a->at_dev;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
for (drive = min_drive; drive <= max_drive; drive++) {
|
|
|
|
if (chp->ch_drive[drive].drv_softc != NULL) {
|
|
|
|
error = config_detach(
|
|
|
|
chp->ch_drive[drive].drv_softc, 0);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
2004-08-05 02:44:04 +04:00
|
|
|
chp->ch_drive[drive].drv_softc = NULL;
|
2004-08-04 02:37:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
error = 0;
|
|
|
|
break;
|
|
|
|
}
|
2004-08-02 01:40:41 +04:00
|
|
|
default:
|
|
|
|
error = ENOTTY;
|
|
|
|
}
|
|
|
|
return (error);
|
|
|
|
};
|