#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: atapi_base.c,v 1.30 2019/02/03 03:19:28 mrg Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/buf.h>
#include <sys/uio.h>
#include <sys/malloc.h>
#include <sys/errno.h>
#include <sys/device.h>
#include <sys/proc.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsipiconf.h>
#include <dev/scsipi/atapiconf.h>
#include <dev/scsipi/scsipi_base.h>
int
atapi_interpret_sense(struct scsipi_xfer *xs)
{
struct scsipi_periph *periph = xs->xs_periph;
int key, error;
const char *msg = NULL;
if (periph->periph_switch->psw_error != NULL) {
SC_DEBUG(periph, SCSIPI_DB2,
("calling private err_handler()\n"));
error = (*periph->periph_switch->psw_error)(xs);
if (error != EJUSTRETURN)
return (error);
}
if (xs->error == XS_SENSE)
return (scsipi_interpret_sense(xs));
key = (xs->sense.atapi_sense & 0xf0) >> 4;
switch (key) {
case SKEY_RECOVERED_ERROR:
msg = "soft error (corrected)";
case SKEY_NO_SENSE:
if (xs->resid == xs->datalen)
xs->resid = 0;
error = 0;
break;
case SKEY_NOT_READY:
if ((periph->periph_flags & PERIPH_REMOVABLE) != 0)
periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
if ((xs->xs_control & XS_CTL_IGNORE_NOT_READY) != 0)
return (0);
if ((xs->xs_control & XS_CTL_SILENT) != 0)
return (EIO);
msg = "not ready";
error = EIO;
break;
case SKEY_MEDIUM_ERROR:
msg = "medium error";
error = EIO;
break;
case SKEY_HARDWARE_ERROR:
msg = "non-media hardware failure";
error = EIO;
break;
case SKEY_ILLEGAL_REQUEST:
if ((xs->xs_control &
XS_CTL_IGNORE_ILLEGAL_REQUEST) != 0)
return (0);
if ((xs->xs_control & XS_CTL_SILENT) != 0)
return (EIO);
msg = "illegal request";
error = EINVAL;
break;
case SKEY_UNIT_ATTENTION:
if ((periph->periph_flags & PERIPH_REMOVABLE) != 0)
periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
if ((xs->xs_control &
XS_CTL_IGNORE_MEDIA_CHANGE) != 0 ||
(periph->periph_flags & PERIPH_REMOVABLE) == 0)
return (ERESTART);
if ((xs->xs_control & XS_CTL_SILENT) != 0)
return (EIO);
msg = "unit attention";
error = EIO;
break;
case SKEY_DATA_PROTECT:
msg = "readonly device";
error = EROFS;
break;
case SKEY_ABORTED_COMMAND:
msg = "command aborted";
if (xs->xs_retries != 0) {
xs->xs_retries--;
error = ERESTART;
} else
error = EIO;
break;
default:
error = EIO;
break;
}
if (!key) {
if (xs->sense.atapi_sense & 0x01) {
msg = "ATA illegal length indication";
error = EIO;
}
if (xs->sense.atapi_sense & 0x02) {
msg = "ATA volume overflow";
error = ENOSPC;
}
if (xs->sense.atapi_sense & 0x04) {
msg = "ATA command aborted";
if (xs->xs_retries != 0) {
xs->xs_retries--;
error = ERESTART;
} else
error = EIO;
}
}
if (msg) {
scsipi_printaddr(periph);
printf("%s\n", msg);
} else {
if (error) {
scsipi_printaddr(periph);
printf("unknown error code %d\n",
xs->sense.atapi_sense);
}
}
return (error);
}
void
atapi_print_addr(struct scsipi_periph *periph)
{
struct scsipi_channel *chan = periph->periph_channel;
struct scsipi_adapter *adapt = chan->chan_adapter;
printf("%s(%s:%d:%d): ", periph->periph_dev != NULL ?
device_xname(periph->periph_dev) : "probe",
device_xname(adapt->adapt_dev),
chan->chan_channel, periph->periph_target);
}
void
atapi_scsipi_cmd(struct scsipi_xfer *xs)
{
struct scsipi_periph *periph = xs->xs_periph;
SC_DEBUG(periph, SCSIPI_DB2, ("atapi_cmd\n"));
xs->cmdlen = (periph->periph_cap & PERIPH_CAP_CMD16) ? 16 : 12;
}