790 lines
23 KiB
C
790 lines
23 KiB
C
/*-
|
|
* Copyright (c) 1998,1999,2000 Søren Schmidt
|
|
* All rights reserved.
|
|
*
|
|
* 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,
|
|
* without modification, immediately at the beginning of the file.
|
|
* 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. 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.
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
* 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.
|
|
*
|
|
* $FreeBSD$
|
|
*/
|
|
|
|
#include "apm.h"
|
|
#include "atapicd.h"
|
|
#include "atapist.h"
|
|
#include "atapifd.h"
|
|
#include "opt_global.h"
|
|
#include "opt_ata.h"
|
|
#include <sys/param.h>
|
|
#include <sys/systm.h>
|
|
#include <sys/kernel.h>
|
|
#include <sys/buf.h>
|
|
#include <sys/bus.h>
|
|
#include <sys/malloc.h>
|
|
#include <machine/clock.h>
|
|
#if NAPM > 0
|
|
#include <machine/apm_bios.h>
|
|
#endif
|
|
#include <dev/ata/ata-all.h>
|
|
#include <dev/ata/atapi-all.h>
|
|
|
|
/* prototypes */
|
|
static void atapi_attach(void *);
|
|
static int32_t atapi_getparam(struct atapi_softc *);
|
|
static void atapi_read(struct atapi_request *, int32_t);
|
|
static void atapi_write(struct atapi_request *, int32_t);
|
|
static void atapi_timeout(struct atapi_request *request);
|
|
static int8_t *atapi_type(int32_t);
|
|
static int8_t *atapi_cmd2str(u_int8_t);
|
|
static int8_t *atapi_skey2str(u_int8_t);
|
|
static int32_t atapi_wait(struct atapi_softc *, u_int8_t);
|
|
static void atapi_init(void);
|
|
|
|
/* extern references */
|
|
int32_t acdattach(struct atapi_softc *);
|
|
int32_t afdattach(struct atapi_softc *);
|
|
int32_t astattach(struct atapi_softc *);
|
|
|
|
/* internal vars */
|
|
static struct intr_config_hook *atapi_attach_hook;
|
|
MALLOC_DEFINE(M_ATAPI, "ATAPI generic", "ATAPI driver generic layer");
|
|
|
|
/* defines */
|
|
#define ATAPI_MAX_RETRIES 5
|
|
|
|
static __inline int
|
|
apiomode(struct atapi_params *ap)
|
|
{
|
|
if (ap->atavalid & 2) {
|
|
if (ap->apiomodes & 2) return 4;
|
|
if (ap->apiomodes & 1) return 3;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static __inline int
|
|
wdmamode(struct atapi_params *ap)
|
|
{
|
|
if (ap->atavalid & 2) {
|
|
if (ap->wdmamodes & 4) return 2;
|
|
if (ap->wdmamodes & 2) return 1;
|
|
if (ap->wdmamodes & 1) return 0;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static __inline int
|
|
udmamode(struct atapi_params *ap)
|
|
{
|
|
if (ap->atavalid & 4) {
|
|
if (ap->udmamodes & 4) return 2;
|
|
if (ap->udmamodes & 2) return 1;
|
|
if (ap->udmamodes & 1) return 0;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static void
|
|
atapi_attach(void *notused)
|
|
{
|
|
struct atapi_softc *atp;
|
|
int32_t ctlr, dev;
|
|
int8_t model_buf[40+1];
|
|
int8_t revision_buf[8+1];
|
|
|
|
/* now, run through atadevices and look for ATAPI devices */
|
|
for (ctlr=0; ctlr<MAXATA; ctlr++) {
|
|
if (!atadevices[ctlr]) continue;
|
|
for (dev=0; dev<2; dev++) {
|
|
if (atadevices[ctlr]->devices &
|
|
(dev ? ATA_ATAPI_SLAVE : ATA_ATAPI_MASTER)) {
|
|
if (!(atp = malloc(sizeof(struct atapi_softc),
|
|
M_ATAPI, M_NOWAIT))) {
|
|
printf("atapi: failed to allocate driver storage\n");
|
|
continue;
|
|
}
|
|
bzero(atp, sizeof(struct atapi_softc));
|
|
atp->controller = atadevices[ctlr];
|
|
atp->unit = (dev == 0) ? ATA_MASTER : ATA_SLAVE;
|
|
if (atapi_getparam(atp)) {
|
|
free(atp, M_ATAPI);
|
|
continue;
|
|
}
|
|
if (bootverbose)
|
|
printf("ata%d-%s: piomode=%d dmamode=%d "
|
|
"udmamode=%d dmaflag=%d\n",
|
|
ctlr, (dev == ATA_MASTER) ? "master" : "slave",
|
|
apiomode(atp->atapi_parm),
|
|
wdmamode(atp->atapi_parm),
|
|
udmamode(atp->atapi_parm),
|
|
atp->atapi_parm->dmaflag);
|
|
|
|
#ifdef ATA_ENABLE_ATAPI_DMA
|
|
if (!(atp->atapi_parm->drqtype == ATAPI_DRQT_INTR)) {
|
|
!ata_dmainit(atp->controller, atp->unit,
|
|
(apiomode(atp->atapi_parm) < 0) ?
|
|
(atp->atapi_parm->dmaflag ? 4 : 0) :
|
|
apiomode(atp->atapi_parm),
|
|
(wdmamode(atp->atapi_parm) < 0) ?
|
|
(atp->atapi_parm->dmaflag ? 2 : 0) :
|
|
wdmamode(atp->atapi_parm),
|
|
udmamode(atp->atapi_parm))
|
|
atp->flags |= ATAPI_F_DMA_ENABLED;
|
|
}
|
|
else
|
|
#endif
|
|
/* set PIO mode */
|
|
ata_dmainit(atp->controller, atp->unit,
|
|
(apiomode(atp->atapi_parm) < 0) ?
|
|
0 : apiomode(atp->atapi_parm), -1, -1);
|
|
|
|
switch (atp->atapi_parm->device_type) {
|
|
#if NATAPICD > 0
|
|
case ATAPI_TYPE_CDROM:
|
|
if (acdattach(atp))
|
|
goto notfound;
|
|
break;
|
|
#endif
|
|
#if NATAPIFD > 0
|
|
case ATAPI_TYPE_DIRECT:
|
|
if (afdattach(atp))
|
|
goto notfound;
|
|
break;
|
|
#endif
|
|
#if NATAPIST > 0
|
|
case ATAPI_TYPE_TAPE:
|
|
if (astattach(atp))
|
|
goto notfound;
|
|
break;
|
|
#endif
|
|
notfound:
|
|
default:
|
|
bpack(atp->atapi_parm->model, model_buf, sizeof(model_buf));
|
|
bpack(atp->atapi_parm->revision, revision_buf,
|
|
sizeof(revision_buf));
|
|
printf("ata%d-%s: <%s/%s> %s device - NO DRIVER!\n",
|
|
ctlr, (dev == ATA_MASTER) ? "master" : "slave",
|
|
model_buf, revision_buf,
|
|
atapi_type(atp->atapi_parm->device_type));
|
|
free(atp, M_ATAPI);
|
|
atp = NULL;
|
|
}
|
|
/* store our softc */
|
|
atp->controller->dev_softc[(atp->unit==ATA_MASTER)?0:1] = atp;
|
|
}
|
|
}
|
|
}
|
|
config_intrhook_disestablish(atapi_attach_hook);
|
|
}
|
|
|
|
static int32_t
|
|
atapi_getparam(struct atapi_softc *atp)
|
|
{
|
|
struct atapi_params *atapi_parm;
|
|
int8_t buffer[DEV_BSIZE];
|
|
|
|
/* select drive */
|
|
outb(atp->controller->ioaddr + ATA_DRIVE, ATA_D_IBM | atp->unit);
|
|
DELAY(1);
|
|
if (ata_command(atp->controller, atp->unit, ATA_C_ATAPI_IDENTIFY,
|
|
0, 0, 0, 0, 0, ATA_WAIT_INTR))
|
|
return -1;
|
|
if (atapi_wait(atp, ATA_S_DRQ))
|
|
return -1;
|
|
insw(atp->controller->ioaddr + ATA_DATA, buffer,
|
|
sizeof(buffer)/sizeof(int16_t));
|
|
if (atapi_wait(atp, 0))
|
|
return -1;
|
|
if (!(atapi_parm = malloc(sizeof(struct atapi_params), M_ATAPI, M_NOWAIT)))
|
|
return -1;
|
|
bcopy(buffer, atapi_parm, sizeof(struct atapi_params));
|
|
if (!((atapi_parm->model[0] == 'N' && atapi_parm->model[1] == 'E') ||
|
|
(atapi_parm->model[0] == 'F' && atapi_parm->model[1] == 'X')))
|
|
bswap(atapi_parm->model, sizeof(atapi_parm->model));
|
|
btrim(atapi_parm->model, sizeof(atapi_parm->model));
|
|
bswap(atapi_parm->revision, sizeof(atapi_parm->revision));
|
|
btrim(atapi_parm->revision, sizeof(atapi_parm->revision));
|
|
atp->atapi_parm = atapi_parm;
|
|
return 0;
|
|
}
|
|
|
|
int32_t
|
|
atapi_queue_cmd(struct atapi_softc *atp, int8_t *ccb, void *data,
|
|
int32_t count, int32_t flags, int32_t timeout,
|
|
atapi_callback_t callback, void *driver, struct buf *bp)
|
|
{
|
|
struct atapi_request *request;
|
|
int32_t error, s;
|
|
|
|
if (!(request = malloc(sizeof(struct atapi_request), M_ATAPI, M_NOWAIT)))
|
|
return ENOMEM;
|
|
|
|
bzero(request, sizeof(struct atapi_request));
|
|
request->device = atp;
|
|
request->data = data;
|
|
request->bytecount = count;
|
|
request->flags = flags;
|
|
request->timeout = timeout * hz;
|
|
request->ccbsize = (atp->atapi_parm->cmdsize) ? 16 : 12;
|
|
bcopy(ccb, request->ccb, request->ccbsize);
|
|
if (callback) {
|
|
request->callback = callback;
|
|
request->bp = bp;
|
|
request->driver = driver;
|
|
}
|
|
|
|
/* append onto controller queue and try to start controller */
|
|
s = splbio();
|
|
TAILQ_INSERT_TAIL(&atp->controller->atapi_queue, request, chain);
|
|
if (atp->controller->active == ATA_IDLE)
|
|
ata_start(atp->controller);
|
|
|
|
/* if callback used, then just return, gets called from interrupt context */
|
|
if (callback) {
|
|
splx(s);
|
|
return 0;
|
|
}
|
|
|
|
/* wait for request to complete */
|
|
tsleep((caddr_t)request, PRIBIO, "atprq", 0);
|
|
splx(s);
|
|
error = request->error;
|
|
free(request, M_ATAPI);
|
|
return error;
|
|
}
|
|
|
|
void
|
|
atapi_transfer(struct atapi_request *request)
|
|
{
|
|
struct atapi_softc *atp = request->device;
|
|
int32_t timout;
|
|
int8_t reason;
|
|
|
|
#ifdef ATAPI_DEBUG
|
|
printf("%s: starting %s ",
|
|
request->device->devname, atapi_cmd2str(request->ccb[0]));
|
|
atapi_dump("ccb = ", &request->ccb[0], sizeof(request->ccb));
|
|
#endif
|
|
/* start timeout for this command */
|
|
request->timeout_handle = timeout((timeout_t *)atapi_timeout,
|
|
request, request->timeout);
|
|
if (request->ccb[0] != ATAPI_REQUEST_SENSE)
|
|
atp->cmd = request->ccb[0];
|
|
|
|
/* flag if we can trust the DSC bit */
|
|
if (request->ccb[0] == ATAPI_READ || request->ccb[0] == ATAPI_READ_BIG ||
|
|
request->ccb[0] == ATAPI_WRITE || request->ccb[0] == ATAPI_WRITE_BIG)
|
|
atp->flags |= ATAPI_F_DSC_USED;
|
|
else
|
|
atp->flags &= ~ATAPI_F_DSC_USED;
|
|
|
|
/* if DMA enabled setup DMA hardware */
|
|
if ((atp->flags & ATAPI_F_DMA_ENABLED) &&
|
|
(request->ccb[0] == ATAPI_READ ||
|
|
request->ccb[0] == ATAPI_READ_BIG ||
|
|
((request->ccb[0] == ATAPI_WRITE ||
|
|
request->ccb[0] == ATAPI_WRITE_BIG) &&
|
|
!(atp->controller->flags & ATA_ATAPI_DMA_RO))) &&
|
|
!ata_dmasetup(atp->controller, atp->unit,
|
|
(void *)request->data, request->bytecount,
|
|
request->flags & A_READ)) {
|
|
atp->flags |= ATAPI_F_DMA_USED;
|
|
}
|
|
|
|
/* start ATAPI operation */
|
|
if (ata_command(atp->controller, atp->unit, ATA_C_PACKET_CMD,
|
|
request->bytecount, 0, 0, 0,
|
|
(atp->flags & ATAPI_F_DMA_USED) ? ATA_F_DMA : 0,
|
|
ATA_IMMEDIATE))
|
|
printf("%s: failure to send ATAPI packet command\n", atp->devname);
|
|
|
|
if (atp->flags & ATAPI_F_DMA_USED)
|
|
ata_dmastart(atp->controller);
|
|
|
|
/* command interrupt device ? just return */
|
|
if (atp->atapi_parm->drqtype == ATAPI_DRQT_INTR)
|
|
return;
|
|
|
|
/* ready to write ATAPI command */
|
|
timout = 5000; /* might be less for fast devices */
|
|
while (timout--) {
|
|
reason = inb(atp->controller->ioaddr + ATA_IREASON);
|
|
atp->controller->status = inb(atp->controller->ioaddr + ATA_STATUS);
|
|
if (((reason & (ATA_I_CMD | ATA_I_IN)) |
|
|
(atp->controller->status&(ATA_S_DRQ|ATA_S_BUSY)))==ATAPI_P_CMDOUT)
|
|
break;
|
|
DELAY(20);
|
|
}
|
|
if (timout <= 0) {
|
|
request->result = inb(atp->controller->ioaddr + ATA_ERROR);
|
|
printf("%s: failure to execute ATAPI packet command\n", atp->devname);
|
|
return;
|
|
}
|
|
|
|
/* this seems to be needed for some (slow) devices */
|
|
DELAY(10);
|
|
|
|
/* send actual command */
|
|
outsw(atp->controller->ioaddr + ATA_DATA, request->ccb,
|
|
request->ccbsize / sizeof(int16_t));
|
|
}
|
|
|
|
int32_t
|
|
atapi_interrupt(struct atapi_request *request)
|
|
{
|
|
struct atapi_softc *atp = request->device;
|
|
int8_t **buffer = (int8_t **)&request->data;
|
|
int32_t length, reason, dma_stat = 0;
|
|
|
|
if (request->ccb[0] == ATAPI_REQUEST_SENSE)
|
|
*buffer = (int8_t *)&request->sense;
|
|
|
|
reason = (inb(atp->controller->ioaddr+ATA_IREASON) & (ATA_I_CMD|ATA_I_IN)) |
|
|
(atp->controller->status & ATA_S_DRQ);
|
|
|
|
if (reason == ATAPI_P_CMDOUT) {
|
|
if (!(atp->controller->status & ATA_S_DRQ)) {
|
|
request->result = inb(atp->controller->ioaddr + ATA_ERROR);
|
|
printf("%s: command interrupt, but no DRQ\n", atp->devname);
|
|
goto op_finished;
|
|
}
|
|
outsw(atp->controller->ioaddr + ATA_DATA, request->ccb,
|
|
request->ccbsize / sizeof(int16_t));
|
|
return ATA_OP_CONTINUES;
|
|
}
|
|
|
|
if (atp->flags & ATAPI_F_DMA_USED)
|
|
dma_stat = ata_dmadone(atp->controller);
|
|
|
|
if (atapi_wait(atp, 0) < 0) {
|
|
printf("%s: timeout waiting for status", atp->devname);
|
|
atp->flags &= ~ATAPI_F_DMA_USED;
|
|
request->result = inb(atp->controller->ioaddr + ATA_ERROR) |
|
|
ATAPI_SK_RESERVED;
|
|
goto op_finished;
|
|
}
|
|
|
|
if (atp->flags & ATAPI_F_DMA_USED) {
|
|
atp->flags &= ~ATAPI_F_DMA_USED;
|
|
if ((atp->controller->status & (ATA_S_ERROR | ATA_S_DWF)) ||
|
|
dma_stat != ATA_BMSTAT_INTERRUPT) {
|
|
request->result = inb(atp->controller->ioaddr + ATA_ERROR);
|
|
}
|
|
else {
|
|
request->result = 0;
|
|
request->bytecount = 0;
|
|
}
|
|
goto op_finished;
|
|
}
|
|
|
|
length = inb(atp->controller->ioaddr + ATA_CYL_LSB);
|
|
length |= inb(atp->controller->ioaddr + ATA_CYL_MSB) << 8;
|
|
|
|
switch (reason) {
|
|
case ATAPI_P_WRITE:
|
|
if (request->flags & A_READ) {
|
|
request->result = inb(atp->controller->ioaddr + ATA_ERROR);
|
|
printf("%s: %s trying to write on read buffer\n",
|
|
atp->devname, atapi_cmd2str(atp->cmd));
|
|
goto op_finished;
|
|
}
|
|
atapi_write(request, length);
|
|
return ATA_OP_CONTINUES;
|
|
|
|
case ATAPI_P_READ:
|
|
if (!(request->flags & A_READ)) {
|
|
request->result = inb(atp->controller->ioaddr + ATA_ERROR);
|
|
printf("%s: %s trying to read on write buffer\n",
|
|
atp->devname, atapi_cmd2str(atp->cmd));
|
|
goto op_finished;
|
|
}
|
|
atapi_read(request, length);
|
|
return ATA_OP_CONTINUES;
|
|
|
|
case ATAPI_P_DONEDRQ:
|
|
printf("%s: %s DONEDRQ\n", atp->devname, atapi_cmd2str(atp->cmd));
|
|
if (request->flags & A_READ)
|
|
atapi_read(request, length);
|
|
else
|
|
atapi_write(request, length);
|
|
/* FALLTHROUGH */
|
|
|
|
case ATAPI_P_ABORT:
|
|
case ATAPI_P_DONE:
|
|
if (atp->controller->status & (ATA_S_ERROR | ATA_S_DWF))
|
|
request->result = inb(atp->controller->ioaddr + ATA_ERROR);
|
|
else
|
|
if (request->ccb[0] != ATAPI_REQUEST_SENSE)
|
|
request->result = 0;
|
|
goto op_finished;
|
|
default:
|
|
printf("%s: unknown transfer phase %d\n", atp->devname, reason);
|
|
}
|
|
|
|
op_finished:
|
|
untimeout((timeout_t *)atapi_timeout, request, request->timeout_handle);
|
|
|
|
/* check for error, if valid sense key, queue a request sense cmd */
|
|
if ((request->result & ATAPI_SK_MASK) &&
|
|
request->ccb[0] != ATAPI_REQUEST_SENSE) {
|
|
bzero(request->ccb, request->ccbsize);
|
|
request->ccb[0] = ATAPI_REQUEST_SENSE;
|
|
request->ccb[4] = sizeof(struct atapi_reqsense);
|
|
request->bytecount = sizeof(struct atapi_reqsense);
|
|
request->flags = A_READ;
|
|
TAILQ_INSERT_HEAD(&atp->controller->atapi_queue, request, chain);
|
|
}
|
|
else {
|
|
request->error = 0;
|
|
if (request->result) {
|
|
switch ((request->result & ATAPI_SK_MASK)) {
|
|
case ATAPI_SK_RESERVED:
|
|
printf("%s: %s - timeout error = %02x\n",
|
|
atp->devname,
|
|
atapi_cmd2str(atp->cmd), request->result & ATAPI_E_MASK);
|
|
request->error = EIO;
|
|
break;
|
|
|
|
case ATAPI_SK_NO_SENSE:
|
|
request->error = 0;
|
|
break;
|
|
|
|
case ATAPI_SK_RECOVERED_ERROR:
|
|
printf("%s: %s - recovered error\n",
|
|
atp->devname, atapi_cmd2str(atp->cmd));
|
|
request->error = 0;
|
|
break;
|
|
|
|
case ATAPI_SK_NOT_READY:
|
|
request->error = EBUSY;
|
|
break;
|
|
|
|
case ATAPI_SK_UNIT_ATTENTION:
|
|
atp->flags |= ATAPI_F_MEDIA_CHANGED;
|
|
request->error = EIO;
|
|
break;
|
|
|
|
default:
|
|
printf("%s: %s - %s asc=%02x ascq=%02x error=%02x\n",
|
|
atp->devname, atapi_cmd2str(atp->cmd),
|
|
atapi_skey2str(request->sense.sense_key),
|
|
request->sense.asc, request->sense.ascq,
|
|
request->result & ATAPI_E_MASK);
|
|
request->error = EIO;
|
|
}
|
|
}
|
|
if (request->callback) {
|
|
if (!((request->callback)(request)))
|
|
free(request, M_ATAPI);
|
|
}
|
|
else
|
|
wakeup((caddr_t)request);
|
|
}
|
|
return ATA_OP_FINISHED;
|
|
}
|
|
|
|
void
|
|
atapi_reinit(struct atapi_softc *atp)
|
|
{
|
|
/* reinit device parameters */
|
|
ata_dmainit(atp->controller, atp->unit,
|
|
(apiomode(atp->atapi_parm) < 0) ?
|
|
(atp->atapi_parm->dmaflag ? 4 : 0) : apiomode(atp->atapi_parm),
|
|
(wdmamode(atp->atapi_parm) < 0) ?
|
|
(atp->atapi_parm->dmaflag ? 2 : 0) : wdmamode(atp->atapi_parm),
|
|
udmamode(atp->atapi_parm));
|
|
}
|
|
|
|
int32_t
|
|
atapi_test_ready(struct atapi_softc *atp)
|
|
{
|
|
int8_t ccb[16] = { ATAPI_TEST_UNIT_READY, 0, 0, 0, 0,
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
|
|
return atapi_queue_cmd(atp, ccb, NULL, 0, 0, 30, NULL, NULL, NULL);
|
|
}
|
|
|
|
int32_t
|
|
atapi_wait_ready(struct atapi_softc *atp, int32_t timeout)
|
|
{
|
|
int32_t error = 0, timout = timeout * hz;
|
|
|
|
while (timout > 0) {
|
|
if ((error = atapi_test_ready(atp)) != EBUSY)
|
|
break;
|
|
tsleep((caddr_t)&error, PRIBIO, "atpwt", 50);
|
|
timout -= 50;
|
|
}
|
|
return error;
|
|
}
|
|
|
|
void
|
|
atapi_dump(int8_t *label, void *data, int32_t len)
|
|
{
|
|
u_int8_t *p = data;
|
|
|
|
printf ("%s %02x", label, *p++);
|
|
while (--len > 0)
|
|
printf ("-%02x", *p++);
|
|
printf ("\n");
|
|
}
|
|
|
|
static void
|
|
atapi_read(struct atapi_request *request, int32_t length)
|
|
{
|
|
int8_t **buffer = (int8_t **)&request->data;
|
|
int32_t resid;
|
|
|
|
if (request->ccb[0] == ATAPI_REQUEST_SENSE)
|
|
*buffer = (int8_t *)&request->sense;
|
|
|
|
if (request->bytecount < length) {
|
|
printf("%s: read data overrun %d/%d\n",
|
|
request->device->devname, length, request->bytecount);
|
|
#ifdef ATA_16BIT_ONLY
|
|
insw(request->device->controller->ioaddr + ATA_DATA,
|
|
(void *)((uintptr_t)*buffer), request->bytecount/sizeof(int16_t));
|
|
#else
|
|
insl(request->device->controller->ioaddr + ATA_DATA,
|
|
(void *)((uintptr_t)*buffer), request->bytecount/sizeof(int32_t));
|
|
#endif
|
|
for (resid=request->bytecount; resid<length; resid+=sizeof(int16_t))
|
|
inw(request->device->controller->ioaddr + ATA_DATA);
|
|
*buffer += request->bytecount;
|
|
request->bytecount = 0;
|
|
}
|
|
else {
|
|
insw(request->device->controller->ioaddr + ATA_DATA,
|
|
(void *)((uintptr_t)*buffer), length / sizeof(int16_t));
|
|
*buffer += length;
|
|
request->bytecount -= length;
|
|
}
|
|
}
|
|
|
|
static void
|
|
atapi_write(struct atapi_request *request, int32_t length)
|
|
{
|
|
int8_t **buffer = (int8_t **)&request->data;
|
|
int32_t resid;
|
|
|
|
if (request->ccb[0] == ATAPI_REQUEST_SENSE)
|
|
*buffer = (int8_t *)&request->sense;
|
|
|
|
if (request->bytecount < length) {
|
|
printf("%s: write data underrun %d/%d\n",
|
|
request->device->devname, length, request->bytecount);
|
|
#ifdef ATA_16BIT_ONLY
|
|
outsw(request->device->controller->ioaddr + ATA_DATA,
|
|
(void *)((uintptr_t)*buffer), request->bytecount/sizeof(int16_t));
|
|
#else
|
|
outsl(request->device->controller->ioaddr + ATA_DATA,
|
|
(void *)((uintptr_t)*buffer), request->bytecount/sizeof(int32_t));
|
|
#endif
|
|
for (resid=request->bytecount; resid<length; resid+=sizeof(int16_t))
|
|
outw(request->device->controller->ioaddr + ATA_DATA, 0);
|
|
*buffer += request->bytecount;
|
|
request->bytecount = 0;
|
|
}
|
|
else {
|
|
outsw(request->device->controller->ioaddr + ATA_DATA,
|
|
(void *)((uintptr_t)*buffer), length / sizeof(int16_t));
|
|
*buffer += length;
|
|
request->bytecount -= length;
|
|
}
|
|
}
|
|
|
|
static void
|
|
atapi_timeout(struct atapi_request *request)
|
|
{
|
|
struct atapi_softc *atp = request->device;
|
|
|
|
atp->controller->running = NULL;
|
|
printf("%s: atapi_timeout: cmd=%s - resetting\n",
|
|
atp->devname, atapi_cmd2str(request->ccb[0]));
|
|
|
|
if (request->flags & ATAPI_F_DMA_USED)
|
|
ata_dmadone(atp->controller);
|
|
|
|
/* if retries still permit, reinject this request */
|
|
if (request->retries++ < ATAPI_MAX_RETRIES)
|
|
TAILQ_INSERT_HEAD(&atp->controller->atapi_queue, request, chain);
|
|
else {
|
|
/* retries all used up, return error */
|
|
request->result = ATAPI_SK_RESERVED | ATAPI_E_ABRT;
|
|
wakeup((caddr_t)request);
|
|
}
|
|
ata_reinit(atp->controller);
|
|
}
|
|
|
|
static int8_t *
|
|
atapi_type(int32_t type)
|
|
{
|
|
switch (type) {
|
|
case ATAPI_TYPE_CDROM:
|
|
return "CDROM";
|
|
case ATAPI_TYPE_DIRECT:
|
|
return "floppy";
|
|
case ATAPI_TYPE_TAPE:
|
|
return "tape";
|
|
case ATAPI_TYPE_OPTICAL:
|
|
return "optical";
|
|
default:
|
|
return "Unknown";
|
|
}
|
|
}
|
|
|
|
static int8_t *
|
|
atapi_cmd2str(u_int8_t cmd)
|
|
{
|
|
switch (cmd) {
|
|
case 0x00: return ("TEST_UNIT_READY");
|
|
case 0x01: return ("REWIND");
|
|
case 0x03: return ("REQUEST_SENSE");
|
|
case 0x04: return ("FORMAT_UNIT");
|
|
case 0x08: return ("READ");
|
|
case 0x0a: return ("WRITE");
|
|
case 0x10: return ("WEOF");
|
|
case 0x11: return ("SPACE");
|
|
case 0x15: return ("MODE_SELECT");
|
|
case 0x19: return ("ERASE");
|
|
case 0x1a: return ("MODE_SENSE");
|
|
case 0x1b: return ("START_STOP");
|
|
case 0x1e: return ("PREVENT_ALLOW");
|
|
case 0x25: return ("READ_CAPACITY");
|
|
case 0x28: return ("READ_BIG");
|
|
case 0x2a: return ("WRITE_BIG");
|
|
case 0x34: return ("READ_POSITION");
|
|
case 0x35: return ("SYNCHRONIZE_CACHE");
|
|
case 0x42: return ("READ_SUBCHANNEL");
|
|
case 0x43: return ("READ_TOC");
|
|
case 0x51: return ("READ_DISC_INFO");
|
|
case 0x52: return ("READ_TRACK_INFO");
|
|
case 0x53: return ("RESERVE_TRACK");
|
|
case 0x54: return ("SEND_OPC_INFO");
|
|
case 0x55: return ("MODE_SELECT_BIG");
|
|
case 0x58: return ("REPAIR_TRACK");
|
|
case 0x59: return ("READ_MASTER_CUE");
|
|
case 0x5a: return ("MODE_SENSE_BIG");
|
|
case 0x5b: return ("CLOSE_TRACK/SESSION");
|
|
case 0x5c: return ("READ_BUFFER_CAPACITY");
|
|
case 0x5d: return ("SEND_CUE_SHEET");
|
|
case 0x47: return ("PLAY_MSF");
|
|
case 0x4b: return ("PAUSE");
|
|
case 0x48: return ("PLAY_TRACK");
|
|
case 0xa1: return ("BLANK_CMD");
|
|
case 0xa3: return ("SEND_KEY");
|
|
case 0xa4: return ("REPORT_KEY");
|
|
case 0xa5: return ("PLAY_BIG");
|
|
case 0xad: return ("READ_DVD_STRUCTURE");
|
|
case 0xb4: return ("PLAY_CD");
|
|
case 0xbb: return ("SET_SPEED");
|
|
case 0xbd: return ("MECH_STATUS");
|
|
case 0xbe: return ("READ_CD");
|
|
default: {
|
|
static int8_t buffer[16];
|
|
sprintf(buffer, "Unknown CMD (0x%02x)", cmd);
|
|
return buffer;
|
|
}
|
|
}
|
|
}
|
|
|
|
static int8_t *
|
|
atapi_skey2str(u_int8_t skey)
|
|
{
|
|
switch (skey) {
|
|
case 0x00: return ("NO SENSE");
|
|
case 0x01: return ("RECOVERED ERROR");
|
|
case 0x02: return ("NOT READY");
|
|
case 0x03: return ("MEDIUM ERROR");
|
|
case 0x04: return ("HARDWARE ERROR");
|
|
case 0x05: return ("ILLEGAL REQUEST");
|
|
case 0x06: return ("UNIT ATTENTION");
|
|
case 0x07: return ("DATA PROTECT");
|
|
case 0x08: return ("BLANK CHECK");
|
|
case 0x09: return ("VENDOR SPECIFIC");
|
|
case 0x0a: return ("COPY ABORTED");
|
|
case 0x0b: return ("ABORTED COMMAND");
|
|
case 0x0c: return ("EQUAL");
|
|
case 0x0d: return ("VOLUME OVERFLOW");
|
|
case 0x0e: return ("MISCOMPARE");
|
|
case 0x0f: return ("RESERVED");
|
|
default: return("UNKNOWN");
|
|
}
|
|
}
|
|
|
|
static int32_t
|
|
atapi_wait(struct atapi_softc *atp, u_int8_t mask)
|
|
{
|
|
u_int32_t timeout = 0;
|
|
|
|
DELAY(1);
|
|
while (timeout++ <= 500000) { /* timeout 5 secs */
|
|
atp->controller->status = inb(atp->controller->ioaddr + ATA_STATUS);
|
|
|
|
/* if drive fails status, reselect the drive just to be sure */
|
|
if (atp->controller->status == 0xff) {
|
|
outb(atp->controller->ioaddr + ATA_DRIVE, ATA_D_IBM | atp->unit);
|
|
DELAY(1);
|
|
atp->controller->status = inb(atp->controller->ioaddr + ATA_STATUS);
|
|
}
|
|
if (!(atp->controller->status & ATA_S_BUSY) &&
|
|
(atp->controller->status & ATA_S_READY))
|
|
break;
|
|
DELAY (10);
|
|
}
|
|
if (timeout <= 0)
|
|
return -1;
|
|
if (!mask)
|
|
return (atp->controller->status & ATA_S_ERROR);
|
|
|
|
/* Wait 50 msec for bits wanted. */
|
|
for (timeout=5000; timeout>0; --timeout) {
|
|
atp->controller->status = inb(atp->controller->ioaddr + ATA_STATUS);
|
|
if ((atp->controller->status & mask) == mask)
|
|
return (atp->controller->status & ATA_S_ERROR);
|
|
DELAY (10);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
static void
|
|
atapi_init(void)
|
|
{
|
|
/* register callback for when interrupts are enabled */
|
|
if (!(atapi_attach_hook =
|
|
(struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
|
|
M_TEMP, M_NOWAIT))) {
|
|
printf("atapi: malloc attach_hook failed\n");
|
|
return;
|
|
}
|
|
bzero(atapi_attach_hook, sizeof(struct intr_config_hook));
|
|
|
|
atapi_attach_hook->ich_func = atapi_attach;
|
|
if (config_intrhook_establish(atapi_attach_hook) != 0) {
|
|
printf("atapi: config_intrhook_establish failed\n");
|
|
free(atapi_attach_hook, M_TEMP);
|
|
}
|
|
|
|
}
|
|
|
|
SYSINIT(atconf, SI_SUB_CONFIGURE, SI_ORDER_SECOND, atapi_init, NULL)
|