#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: st.c,v 1.244 2025/12/16 18:24:47 andvar Exp $");
#ifdef _KERNEL_OPT
#include "opt_scsi.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/fcntl.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/malloc.h>
#include <sys/buf.h>
#include <sys/bufq.h>
#include <sys/proc.h>
#include <sys/mtio.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/kernel.h>
#include <sys/vnode.h>
#include <sys/iostat.h>
#include <sys/sysctl.h>
#include <dev/scsipi/scsi_spc.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsi_all.h>
#include <dev/scsipi/scsi_tape.h>
#include <dev/scsipi/scsipiconf.h>
#include <dev/scsipi/scsipi_base.h>
#include <dev/scsipi/stvar.h>
#define DEF_FIXED_BSIZE 512
#define STMODE(z) ( minor(z) & 0x03)
#define STDSTY(z) ((minor(z) >> 2) & 0x03)
#define STUNIT(z) ((minor(z) >> 4) )
#define STNMINOR 16
#define NORMAL_MODE 0
#define NOREW_MODE 1
#define EJECT_MODE 2
#define CTRL_MODE 3
#ifndef ST_MOUNT_DELAY
#define ST_MOUNT_DELAY 0
#endif
static dev_type_open(stopen);
static dev_type_close(stclose);
static dev_type_read(stread);
static dev_type_write(stwrite);
static dev_type_ioctl(stioctl);
static dev_type_strategy(ststrategy);
static dev_type_dump(stdump);
const struct bdevsw st_bdevsw = {
.d_open = stopen,
.d_close = stclose,
.d_strategy = ststrategy,
.d_ioctl = stioctl,
.d_dump = stdump,
.d_psize = nosize,
.d_discard = nodiscard,
.d_flag = D_TAPE | D_MPSAFE
};
const struct cdevsw st_cdevsw = {
.d_open = stopen,
.d_close = stclose,
.d_read = stread,
.d_write = stwrite,
.d_ioctl = stioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = nopoll,
.d_mmap = nommap,
.d_kqfilter = nokqfilter,
.d_discard = nodiscard,
.d_flag = D_TAPE | D_MPSAFE
};
static const struct st_quirk_inquiry_pattern st_quirk_patterns[] = {
{{T_SEQUENTIAL, T_REMOV,
" ", " ", " "}, {0, 0, {
{ST_Q_FORCE_BLKSIZE, 512, 0},
{ST_Q_FORCE_BLKSIZE, 512, QIC_24},
{ST_Q_FORCE_BLKSIZE, 0, HALFINCH_1600},
{ST_Q_FORCE_BLKSIZE, 0, HALFINCH_6250}
}}},
{{T_SEQUENTIAL, T_REMOV,
"TANDBERG", " TDC 3600 ", ""}, {0, 12, {
{0, 0, 0},
{ST_Q_FORCE_BLKSIZE, 0, QIC_525},
{0, 0, QIC_150},
{0, 0, QIC_120}
}}},
{{T_SEQUENTIAL, T_REMOV,
"TANDBERG", " TDC 3800 ", ""}, {0, 0, {
{ST_Q_FORCE_BLKSIZE, 512, 0},
{0, 0, QIC_525},
{0, 0, QIC_150},
{0, 0, QIC_120}
}}},
{{T_SEQUENTIAL, T_REMOV,
"TANDBERG", " SLR5 4/8GB ", ""}, {0, 0, {
{ST_Q_FORCE_BLKSIZE, 1024, 0},
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
}}},
{{T_SEQUENTIAL, T_REMOV,
"TANDBERG", " TDC 4200 ", ""}, {0, 0, {
{ST_Q_FORCE_BLKSIZE, 512, 0},
{0, 0, QIC_525},
{0, 0, QIC_150},
{0, 0, QIC_120}
}}},
{{T_SEQUENTIAL, T_REMOV,
"ARCHIVE ", "VIPER 2525 25462", ""}, {0, 0, {
{ST_Q_SENSE_HELP, 0, 0},
{ST_Q_SENSE_HELP, 0, QIC_525},
{0, 0, QIC_150},
{0, 0, QIC_120}
}}},
{{T_SEQUENTIAL, T_REMOV,
"SANKYO ", "CP525 ", ""}, {0, 0, {
{ST_Q_FORCE_BLKSIZE, 512, 0},
{ST_Q_FORCE_BLKSIZE, 512, QIC_525},
{0, 0, QIC_150},
{0, 0, QIC_120}
}}},
{{T_SEQUENTIAL, T_REMOV,
"ANRITSU ", "DMT780 ", ""}, {0, 0, {
{ST_Q_FORCE_BLKSIZE, 512, 0},
{ST_Q_FORCE_BLKSIZE, 512, QIC_525},
{0, 0, QIC_150},
{0, 0, QIC_120}
}}},
{{T_SEQUENTIAL, T_REMOV,
"ARCHIVE ", "VIPER 150 21247", ""}, {ST_Q_ERASE_NOIMM, 12, {
{ST_Q_SENSE_HELP, 0, 0},
{0, 0, QIC_150},
{0, 0, QIC_120},
{0, 0, QIC_24}
}}},
{{T_SEQUENTIAL, T_REMOV,
"ARCHIVE ", "VIPER 150 21531", ""}, {ST_Q_ERASE_NOIMM, 12, {
{ST_Q_SENSE_HELP, 0, 0},
{0, 0, QIC_150},
{0, 0, QIC_120},
{0, 0, QIC_24}
}}},
{{T_SEQUENTIAL, T_REMOV,
"WANGTEK ", "5099ES SCSI", ""}, {0, 0, {
{ST_Q_FORCE_BLKSIZE, 512, 0},
{0, 0, QIC_11},
{0, 0, QIC_24},
{0, 0, QIC_24}
}}},
{{T_SEQUENTIAL, T_REMOV,
"WANGTEK ", "5150ES SCSI", ""}, {0, 0, {
{ST_Q_FORCE_BLKSIZE, 512, 0},
{0, 0, QIC_24},
{0, 0, QIC_120},
{0, 0, QIC_150}
}}},
{{T_SEQUENTIAL, T_REMOV,
"WANGTEK ", "5525ES SCSI REV7", ""}, {0, 0, {
{0, 0, 0},
{ST_Q_BLKSIZE, 0, QIC_525},
{0, 0, QIC_150},
{0, 0, QIC_120}
}}},
{{T_SEQUENTIAL, T_REMOV,
"WangDAT ", "Model 1300 ", ""}, {0, 0, {
{0, 0, 0},
{ST_Q_FORCE_BLKSIZE, 512, DDS},
{ST_Q_FORCE_BLKSIZE, 1024, DDS},
{ST_Q_FORCE_BLKSIZE, 0, DDS}
}}},
{{T_SEQUENTIAL, T_REMOV,
"EXABYTE ", "EXB-8200 ", "263H"}, {0, 5, {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
}}},
{{T_SEQUENTIAL, T_REMOV,
"STK", "9490", ""},
{ST_Q_FORCE_BLKSIZE, 0, {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
}}},
{{T_SEQUENTIAL, T_REMOV,
"STK", "SD-3", ""},
{ST_Q_FORCE_BLKSIZE, 0, {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
}}},
{{T_SEQUENTIAL, T_REMOV,
"IBM", "03590", ""}, {ST_Q_IGNORE_LOADS, 0, {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
}}},
{{T_SEQUENTIAL, T_REMOV,
"HP ", "T4000s ", ""}, {ST_Q_UNIMODAL, 0, {
{0, 0, QIC_3095},
{0, 0, QIC_3095},
{0, 0, QIC_3095},
{0, 0, QIC_3095},
}}},
#if 0
{{T_SEQUENTIAL, T_REMOV,
"EXABYTE ", "EXB-8200 ", ""}, {0, 12, {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
}}},
#endif
{{T_SEQUENTIAL, T_REMOV,
"TEAC ", "MT-2ST/N50 ", ""}, {ST_Q_IGNORE_LOADS, 0, {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
}}},
{{T_SEQUENTIAL, T_REMOV,
"OnStream", "ADR50 Drive", ""}, {ST_Q_UNIMODAL, 0, {
{ST_Q_FORCE_BLKSIZE, 512, 0},
{ST_Q_FORCE_BLKSIZE, 512, 0},
{ST_Q_FORCE_BLKSIZE, 512, 0},
{ST_Q_FORCE_BLKSIZE, 512, 0},
}}},
{{T_SEQUENTIAL, T_REMOV,
"OnStream DI-30", "", "1.0"}, {ST_Q_NOFILEMARKS, 0, {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
}}},
{{T_SEQUENTIAL, T_REMOV,
"NCR H621", "0-STD-03-46F880 ", ""}, {ST_Q_NOPREVENT, 0, {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
}}},
{{T_SEQUENTIAL, T_REMOV,
"Seagate STT3401A", "hp0atxa", ""}, {0, 0, {
{ST_Q_FORCE_BLKSIZE, 512, 0},
{ST_Q_FORCE_BLKSIZE, 1024, 0},
{ST_Q_FORCE_BLKSIZE, 512, 0},
{ST_Q_FORCE_BLKSIZE, 512, 0}
}}},
};
#define NOEJECT 0
#define EJECT 1
static void st_identify_drive(struct st_softc *,
struct scsipi_inquiry_pattern *);
static void st_loadquirks(struct st_softc *);
static int st_mount_tape(dev_t, int);
static void st_unmount(struct st_softc *, boolean);
static int st_decide_mode(struct st_softc *, boolean);
static void ststart(struct scsipi_periph *);
static int ststart1(struct scsipi_periph *, struct buf *, int *);
static void strestart(void *);
static void stdone(struct scsipi_xfer *, int);
static int st_read(struct st_softc *, char *, int, int);
static int st_space(struct st_softc *, int, u_int, int);
static int st_write_filemarks(struct st_softc *, int, int);
static int st_check_eod(struct st_softc *, boolean, int *, int);
static int st_load(struct st_softc *, u_int, int);
static int st_rewind(struct st_softc *, u_int, int);
static int st_interpret_sense(struct scsipi_xfer *);
static int st_touch_tape(struct st_softc *);
static int st_erase(struct st_softc *, int full, int flags);
static void st_updatefilepos(struct st_softc *);
static int st_rdpos(struct st_softc *, int, uint32_t *);
static int st_setpos(struct st_softc *, int, uint32_t *);
static const struct scsipi_periphsw st_switch = {
st_interpret_sense,
ststart,
NULL,
stdone
};
#if defined(ST_ENABLE_EARLYWARN)
#define ST_INIT_FLAGS ST_EARLYWARN
#else
#define ST_INIT_FLAGS 0
#endif
void
stattach(device_t parent, device_t self, void *aux)
{
struct st_softc *st = device_private(self);
struct scsipibus_attach_args *sa = aux;
struct scsipi_periph *periph = sa->sa_periph;
SC_DEBUG(periph, SCSIPI_DB2, ("stattach: "));
st->sc_dev = self;
st->sc_periph = periph;
periph->periph_dev = st->sc_dev;
periph->periph_switch = &st_switch;
st->flags = ST_INIT_FLAGS;
bufq_alloc(&st->buf_queue, "fcfs", 0);
bufq_alloc(&st->buf_defer, "fcfs", 0);
callout_init(&st->sc_callout, 0);
mutex_init(&st->sc_iolock, MUTEX_DEFAULT, IPL_VM);
st_identify_drive(st, &sa->sa_inqbuf);
aprint_naive("\n");
aprint_normal("\n");
aprint_normal_dev(self, "%s", st->quirkdata ? "quirks apply, " : "");
if (scsipi_test_unit_ready(periph,
XS_CTL_DISCOVERY | XS_CTL_SILENT | XS_CTL_IGNORE_MEDIA_CHANGE) ||
st->ops(st, ST_OPS_MODESENSE,
XS_CTL_DISCOVERY | XS_CTL_SILENT | XS_CTL_IGNORE_MEDIA_CHANGE))
aprint_normal("drive empty\n");
else {
aprint_normal("density code %d, ", st->media_density);
if (st->media_blksize > 0)
aprint_normal("%d-byte", st->media_blksize);
else
aprint_normal("variable");
aprint_normal(" blocks, write-%s\n",
(st->flags & ST_READONLY) ? "protected" : "enabled");
}
st->stats = iostat_alloc(IOSTAT_TAPE, parent,
device_xname(st->sc_dev));
rnd_attach_source(&st->rnd_source, device_xname(st->sc_dev),
RND_TYPE_TAPE, RND_FLAG_DEFAULT);
}
int
stdetach(device_t self, int flags)
{
struct st_softc *st = device_private(self);
struct scsipi_periph *periph = st->sc_periph;
struct scsipi_channel *chan = periph->periph_channel;
int bmaj, cmaj, mn;
bmaj = bdevsw_lookup_major(&st_bdevsw);
cmaj = cdevsw_lookup_major(&st_cdevsw);
callout_halt(&st->sc_callout, NULL);
mutex_enter(chan_mtx(chan));
bufq_drain(st->buf_defer);
bufq_drain(st->buf_queue);
scsipi_kill_pending(st->sc_periph);
mutex_exit(chan_mtx(chan));
bufq_free(st->buf_defer);
bufq_free(st->buf_queue);
mutex_destroy(&st->sc_iolock);
mn = STUNIT(device_unit(self));
vdevgone(bmaj, mn, mn+STNMINOR-1, VBLK);
vdevgone(cmaj, mn, mn+STNMINOR-1, VCHR);
iostat_free(st->stats);
rnd_detach_source(&st->rnd_source);
return 0;
}
static void
st_identify_drive(struct st_softc *st, struct scsipi_inquiry_pattern *inqbuf)
{
const struct st_quirk_inquiry_pattern *finger;
int priority;
finger = scsipi_inqmatch(inqbuf,
st_quirk_patterns,
sizeof(st_quirk_patterns) / sizeof(st_quirk_patterns[0]),
sizeof(st_quirk_patterns[0]), &priority);
if (priority != 0) {
st->quirkdata = &finger->quirkdata;
st->drive_quirks = finger->quirkdata.quirks;
st->quirks = finger->quirkdata.quirks;
st->page_0_size = finger->quirkdata.page_0_size;
KASSERT(st->page_0_size <= MAX_PAGE_0_SIZE);
st_loadquirks(st);
}
}
static void
st_loadquirks(struct st_softc *st)
{
const struct modes *mode;
struct modes *mode2;
int i;
mode = st->quirkdata->modes;
mode2 = st->modes;
for (i = 0; i < 4; i++) {
memset(mode2, 0, sizeof(struct modes));
st->modeflags[i] &= ~(BLKSIZE_SET_BY_QUIRK |
DENSITY_SET_BY_QUIRK | BLKSIZE_SET_BY_USER |
DENSITY_SET_BY_USER);
if ((mode->quirks | st->drive_quirks) & ST_Q_FORCE_BLKSIZE) {
mode2->blksize = mode->blksize;
st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK;
}
if (mode->density) {
mode2->density = mode->density;
st->modeflags[i] |= DENSITY_SET_BY_QUIRK;
}
mode2->quirks |= mode->quirks;
mode++;
mode2++;
}
}
static int
stopen(dev_t dev, int flags, int mode, struct lwp *l)
{
u_int stmode, dsty;
int error, sflags, unit, tries, ntries;
struct st_softc *st;
struct scsipi_periph *periph;
struct scsipi_adapter *adapt;
unit = STUNIT(dev);
st = device_lookup_private(&st_cd, unit);
if (st == NULL)
return ENXIO;
stmode = STMODE(dev);
dsty = STDSTY(dev);
periph = st->sc_periph;
adapt = periph->periph_channel->chan_adapter;
SC_DEBUG(periph, SCSIPI_DB1,
("open: dev=0x%"PRIx64" (unit %d (of %d))\n", dev, unit,
st_cd.cd_ndevs));
if (periph->periph_flags & PERIPH_OPEN) {
aprint_error_dev(st->sc_dev, "already open\n");
return EBUSY;
}
if ((error = scsipi_adapter_addref(adapt)) != 0)
return error;
st->mt_resid = 0;
st->mt_erreg = 0;
st->asc = 0;
st->ascq = 0;
if ((st->flags & ST_MOUNTED) == 0 || stmode == CTRL_MODE) {
#ifdef SCSIDEBUG
sflags = XS_CTL_IGNORE_MEDIA_CHANGE;
#else
sflags = XS_CTL_SILENT|XS_CTL_IGNORE_MEDIA_CHANGE;
#endif
} else
sflags = 0;
if ((st->flags & ST_MOUNTED) || ST_MOUNT_DELAY == 0)
ntries = 1;
else
ntries = ST_MOUNT_DELAY;
for (error = tries = 0; tries < ntries; tries++) {
int slpintr, oflags;
error = scsipi_test_unit_ready(periph, sflags);
if (error == 0 || stmode == CTRL_MODE)
break;
if ((st->flags & ST_MOUNTED) || ST_MOUNT_DELAY == 0 ||
(st->mt_key != SKEY_NOT_READY)) {
device_printf(st->sc_dev,
"mount error (sense key=%d) - "
"terminating mount session\n",
st->mt_key);
if (st->flags & ST_WRITTEN &&
st->mt_key == SKEY_UNIT_ATTENTION) {
st->flags &= ~(ST_WRITTEN|ST_FM_WRITTEN);
device_printf(st->sc_dev,
"CAUTION: file marks/data may be missing"
" - ASC = 0x%02x, ASCQ = 0x%02x\n",
st->asc, st->ascq);
}
goto bad;
}
st->mt_resid = 0;
st->mt_erreg = 0;
st->asc = 0;
st->ascq = 0;
oflags = periph->periph_flags;
periph->periph_flags |= PERIPH_OPEN;
slpintr = kpause("stload", true, hz, NULL);
periph->periph_flags = oflags;
if (slpintr != 0 && slpintr != EWOULDBLOCK) {
device_printf(st->sc_dev, "load interrupted\n");
goto bad;
}
}
if (stmode == CTRL_MODE &&
st->mt_key != SKEY_NO_SENSE &&
st->mt_key != SKEY_UNIT_ATTENTION) {
periph->periph_flags |= PERIPH_OPEN;
return 0;
}
if (error)
return error;
periph->periph_flags |= PERIPH_OPEN;
if (st->last_dsty != dsty ||
(periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
st_unmount(st, NOEJECT);
if (!(st->flags & ST_MOUNTED)) {
if ((error = st_mount_tape(dev, flags)) != 0)
goto bad;
st->last_dsty = dsty;
}
if (!(st->quirks & ST_Q_NOPREVENT)) {
scsipi_prevent(periph, SPAMR_PREVENT_DT,
XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_NOT_READY);
}
SC_DEBUG(periph, SCSIPI_DB2, ("open complete\n"));
return 0;
bad:
st_unmount(st, NOEJECT);
scsipi_adapter_delref(adapt);
periph->periph_flags &= ~PERIPH_OPEN;
return error;
}
static int
stclose(dev_t dev, int flags, int mode, struct lwp *l)
{
int stxx, error = 0;
struct st_softc *st = device_lookup_private(&st_cd, STUNIT(dev));
struct scsipi_periph *periph = st->sc_periph;
struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
SC_DEBUG(st->sc_periph, SCSIPI_DB1, ("closing\n"));
stxx = st->flags & (ST_WRITTEN | ST_FM_WRITTEN);
if ((flags & FWRITE) != 0) {
int nm = 0;
#ifdef ST_SUNCOMPAT
if ((flags & O_ACCMODE) == FWRITE && (stxx == 0)) {
st->flags |= ST_WRITTEN;
SC_DEBUG(st->sc_periph, SCSIPI_DB3,
("SUN compatibility: write FM(s) at close\n"));
}
#endif
error = st_check_eod(st, FALSE, &nm, 0);
SC_DEBUG(st->sc_periph, SCSIPI_DB3,
("wrote %d FM(s) at close error=%d\n", nm, error));
}
if (!(st->quirks & ST_Q_NOPREVENT)) {
scsipi_prevent(periph, SPAMR_ALLOW,
XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_NOT_READY);
}
switch (STMODE(dev)) {
case NORMAL_MODE:
st_unmount(st, NOEJECT);
break;
case NOREW_MODE:
case CTRL_MODE:
if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
st_unmount(st, NOEJECT);
} else if (error == 0) {
stxx &= ~ST_FM_WRITTEN;
stxx |= (st->flags & ST_2FM_AT_EOD);
if ((flags & FWRITE) != 0 &&
(stxx == (ST_2FM_AT_EOD|ST_WRITTEN))) {
error = st_space(st, -1, SP_FILEMARKS, 0);
SC_DEBUG(st->sc_periph, SCSIPI_DB3, ("st_space(-1) error=%d\n", error));
} else {
SC_DEBUG(st->sc_periph, SCSIPI_DB3, ("no backspacing - flags = 0x%x, stxx=0x%x, st->flags=0x%x\n", flags, stxx, st->flags));
}
} else {
SC_DEBUG(st->sc_periph, SCSIPI_DB3, ("error %d from st_check_eod\n", error));
}
break;
case EJECT_MODE:
st_unmount(st, EJECT);
break;
}
KASSERTMSG((st->flags & ST_WRITTEN) == 0,
"pending ST_WRITTEN flag NOT cleared (flags=0x%x)", st->flags);
scsipi_wait_drain(periph);
scsipi_adapter_delref(adapt);
periph->periph_flags &= ~PERIPH_OPEN;
return error;
}
static int
st_mount_tape(dev_t dev, int flags)
{
int unit;
u_int dsty;
struct st_softc *st;
struct scsipi_periph *periph;
int error = 0;
unit = STUNIT(dev);
dsty = STDSTY(dev);
st = device_lookup_private(&st_cd, unit);
periph = st->sc_periph;
if (st->flags & ST_MOUNTED)
return 0;
SC_DEBUG(periph, SCSIPI_DB1, ("mounting\n "));
st->flags |= ST_NEW_MOUNT;
st->quirks = st->drive_quirks | st->modes[dsty].quirks;
if ((error = st_load(st, LD_LOAD, XS_CTL_SILENT)) != 0)
return error;
scsipi_test_unit_ready(periph, XS_CTL_SILENT);
if (st->quirks & ST_Q_SENSE_HELP)
if ((error = st_touch_tape(st)) != 0)
return error;
if ((error = st->ops(st, ST_OPS_RBL, 0)) != 0)
return error;
if ((error = st->ops(st, ST_OPS_MODESENSE, 0)) != 0)
return error;
if (st->modeflags[dsty] & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER))
st->density = st->modes[dsty].density;
else
st->density = st->media_density;
st->flags &= ~ST_FIXEDBLOCKS;
if (st->modeflags[dsty] &
(BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) {
st->blksize = st->modes[dsty].blksize;
if (st->blksize)
st->flags |= ST_FIXEDBLOCKS;
} else {
if ((error = st_decide_mode(st, FALSE)) != 0)
return error;
}
if ((error = st->ops(st, ST_OPS_MODESELECT, 0)) != 0) {
if (error != ENODEV) {
aprint_error_dev(st->sc_dev,
"cannot set selected mode\n");
return error;
}
}
st->flags &= ~ST_NEW_MOUNT;
st->flags |= ST_MOUNTED;
periph->periph_flags |= PERIPH_MEDIA_LOADED;
st->blkno = st->fileno = (daddr_t) 0;
return 0;
}
static void
st_unmount(struct st_softc *st, boolean eject)
{
struct scsipi_periph *periph = st->sc_periph;
int nmarks;
if ((st->flags & ST_MOUNTED) == 0)
return;
SC_DEBUG(periph, SCSIPI_DB1, ("unmounting\n"));
st_check_eod(st, FALSE, &nmarks, XS_CTL_IGNORE_NOT_READY);
st_rewind(st, 0, XS_CTL_IGNORE_NOT_READY);
st->density = 0;
if (st->ops(st, ST_OPS_MODESELECT, 0) != 0) {
aprint_error_dev(st->sc_dev,
"WARNING: cannot revert to default density\n");
}
if (eject) {
if (!(st->quirks & ST_Q_NOPREVENT)) {
scsipi_prevent(periph, SPAMR_ALLOW,
XS_CTL_IGNORE_ILLEGAL_REQUEST |
XS_CTL_IGNORE_NOT_READY);
}
st_load(st, LD_UNLOAD, XS_CTL_IGNORE_NOT_READY);
st->blkno = st->fileno = (daddr_t) -1;
} else {
st->blkno = st->fileno = (daddr_t) 0;
}
st->flags &= ~(ST_MOUNTED | ST_NEW_MOUNT);
periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
}
int
st_decide_mode(struct st_softc *st, boolean first_read)
{
SC_DEBUG(st->sc_periph, SCSIPI_DB2, ("starting block mode decision\n"));
if (st->blkmin && (st->blkmin == st->blkmax)) {
st->flags |= ST_FIXEDBLOCKS;
st->blksize = st->blkmin;
SC_DEBUG(st->sc_periph, SCSIPI_DB3,
("blkmin == blkmax of %d\n", st->blkmin));
goto done;
}
switch (st->density) {
case HALFINCH_800:
case HALFINCH_1600:
case HALFINCH_6250:
case DDS:
st->flags &= ~ST_FIXEDBLOCKS;
st->blksize = 0;
SC_DEBUG(st->sc_periph, SCSIPI_DB3,
("density specified variable\n"));
goto done;
case QIC_11:
case QIC_24:
case QIC_120:
case QIC_150:
case QIC_525:
case QIC_1320:
case QIC_3095:
case QIC_3220:
st->flags |= ST_FIXEDBLOCKS;
if (st->media_blksize > 0)
st->blksize = st->media_blksize;
else
st->blksize = DEF_FIXED_BSIZE;
SC_DEBUG(st->sc_periph, SCSIPI_DB3,
("density specified fixed\n"));
goto done;
}
if (first_read &&
(!(st->quirks & ST_Q_BLKSIZE) || (st->media_blksize == 0) ||
(st->media_blksize == DEF_FIXED_BSIZE) ||
(st->media_blksize == 1024))) {
if (st->media_blksize > 0)
st->flags |= ST_FIXEDBLOCKS;
else
st->flags &= ~ST_FIXEDBLOCKS;
st->blksize = st->media_blksize;
SC_DEBUG(st->sc_periph, SCSIPI_DB3,
("Used media_blksize of %d\n", st->media_blksize));
goto done;
}
st->flags &= ~ST_FIXEDBLOCKS;
st->blksize = 0;
SC_DEBUG(st->sc_periph, SCSIPI_DB3,
("Give up and default to variable mode\n"));
done:
switch (st->density) {
case QIC_11:
case QIC_24:
case QIC_120:
case QIC_150:
case QIC_525:
case QIC_1320:
case QIC_3095:
case QIC_3220:
st->flags &= ~ST_2FM_AT_EOD;
break;
default:
st->flags |= ST_2FM_AT_EOD;
}
return 0;
}
static void
ststrategy(struct buf *bp)
{
struct st_softc *st = device_lookup_private(&st_cd, STUNIT(bp->b_dev));
struct scsipi_periph *periph = st->sc_periph;
struct scsipi_channel *chan = periph->periph_channel;
SC_DEBUG(periph, SCSIPI_DB1,
("ststrategy %d bytes @ blk %" PRId64 "\n", bp->b_bcount,
bp->b_blkno));
if (bp->b_bcount == 0)
goto abort;
if (bp->b_blkno < 0) {
SC_DEBUG(periph, SCSIPI_DB3,
("EINVAL: ststrategy negative blockcount %" PRId64 "\n", bp->b_blkno));
bp->b_error = EINVAL;
goto abort;
}
if (st->flags & ST_FIXEDBLOCKS) {
if (bp->b_bcount % st->blksize) {
aprint_error_dev(st->sc_dev, "bad request, must be multiple of %d\n",
st->blksize);
bp->b_error = EIO;
goto abort;
}
}
else if (bp->b_bcount < st->blkmin ||
(st->blkmax && bp->b_bcount > st->blkmax)) {
aprint_error_dev(st->sc_dev, "bad request, must be between %d and %d\n",
st->blkmin, st->blkmax);
bp->b_error = EIO;
goto abort;
}
mutex_enter(chan_mtx(chan));
bufq_put(st->buf_queue, bp);
ststart(periph);
mutex_exit(chan_mtx(chan));
return;
abort:
bp->b_resid = bp->b_bcount;
biodone(bp);
return;
}
static int
ststart1(struct scsipi_periph *periph, struct buf *bp, int *errnop)
{
struct st_softc *st = device_private(periph->periph_dev);
struct scsipi_channel *chan = periph->periph_channel;
struct scsi_rw_tape cmd;
struct scsipi_xfer *xs;
int flags, error, complete = 1;
SC_DEBUG(periph, SCSIPI_DB2, ("ststart1 "));
mutex_enter(chan_mtx(chan));
if (periph->periph_active >= periph->periph_openings) {
error = EAGAIN;
goto out;
}
if (periph->periph_flags & PERIPH_WAITING) {
periph->periph_flags &= ~PERIPH_WAITING;
cv_broadcast(periph_cv_periph(periph));
error = EAGAIN;
goto out;
}
if (__predict_false((st->flags & ST_MOUNTED) == 0 ||
(periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)) {
error = EIO;
goto out;
}
if (st->flags & ST_FIXEDBLOCKS) {
if (st->flags & ST_AT_FILEMARK) {
if ((bp->b_flags & B_READ) == B_WRITE) {
if (st_space(st, 0, SP_FILEMARKS, 0)) {
error = EIO;
goto out;
}
} else {
error = 0;
st->flags &= ~ST_AT_FILEMARK;
goto out;
}
}
}
if (st->flags & (ST_EOM_PENDING|ST_EIO_PENDING)) {
error = 0;
if (st->flags & ST_EIO_PENDING)
error = EIO;
st->flags &= ~(ST_EOM_PENDING|ST_EIO_PENDING);
goto out;
}
memset(&cmd, 0, sizeof(cmd));
flags = XS_CTL_NOSLEEP | XS_CTL_ASYNC;
if ((bp->b_flags & B_READ) == B_WRITE) {
cmd.opcode = WRITE;
st->flags &= ~ST_FM_WRITTEN;
flags |= XS_CTL_DATA_OUT;
} else {
cmd.opcode = READ;
flags |= XS_CTL_DATA_IN;
}
if (st->flags & ST_FIXEDBLOCKS) {
cmd.byte2 |= SRW_FIXED;
_lto3b(bp->b_bcount / st->blksize, cmd.len);
} else
_lto3b(bp->b_bcount, cmd.len);
st->flags &= ~ST_POSUPDATED;
xs = scsipi_make_xs_locked(periph,
(struct scsipi_generic *)&cmd, sizeof(cmd),
(u_char *)bp->b_data, bp->b_bcount,
0, ST_IO_TIME, bp, flags);
if (__predict_false(xs == NULL)) {
callout_reset(&st->sc_callout, hz / 2, strestart,
periph);
error = EAGAIN;
goto out;
}
error = scsipi_execute_xs(xs);
KASSERT(error == 0);
if (error == 0)
complete = 0;
out:
mutex_exit(chan_mtx(chan));
*errnop = error;
return complete;
}
static void
ststart(struct scsipi_periph *periph)
{
struct st_softc *st = device_private(periph->periph_dev);
struct scsipi_channel *chan = periph->periph_channel;
struct buf *bp;
int error, complete;
SC_DEBUG(periph, SCSIPI_DB2, ("ststart "));
mutex_exit(chan_mtx(chan));
mutex_enter(&st->sc_iolock);
while ((bp = bufq_get(st->buf_defer)) != NULL
|| (bp = bufq_get(st->buf_queue)) != NULL) {
iostat_busy(st->stats);
mutex_exit(&st->sc_iolock);
complete = ststart1(periph, bp, &error);
mutex_enter(&st->sc_iolock);
if (complete) {
iostat_unbusy(st->stats, 0,
((bp->b_flags & B_READ) == B_READ));
if (error == EAGAIN) {
bufq_put(st->buf_defer, bp);
break;
}
}
mutex_exit(&st->sc_iolock);
if (complete) {
bp->b_error = error;
bp->b_resid = bp->b_bcount;
biodone(bp);
}
mutex_enter(&st->sc_iolock);
}
mutex_exit(&st->sc_iolock);
mutex_enter(chan_mtx(chan));
}
static void
strestart(void *v)
{
struct scsipi_periph *periph = (struct scsipi_periph *)v;
struct scsipi_channel *chan = periph->periph_channel;
mutex_enter(chan_mtx(chan));
ststart((struct scsipi_periph *)v);
mutex_exit(chan_mtx(chan));
}
static void
stdone(struct scsipi_xfer *xs, int error)
{
struct st_softc *st = device_private(xs->xs_periph->periph_dev);
struct buf *bp = xs->bp;
if (bp) {
bp->b_error = error;
bp->b_resid = xs->resid;
if (bp->b_resid > bp->b_bcount || bp->b_resid < 0)
bp->b_resid = bp->b_bcount;
mutex_enter(&st->sc_iolock);
if ((bp->b_flags & B_READ) == B_WRITE)
st->flags |= ST_WRITTEN;
else
st->flags &= ~ST_WRITTEN;
iostat_unbusy(st->stats, bp->b_bcount,
((bp->b_flags & B_READ) == B_READ));
if ((st->flags & ST_POSUPDATED) == 0) {
if (error) {
st->fileno = st->blkno = -1;
} else if (st->blkno != -1) {
if (st->flags & ST_FIXEDBLOCKS)
st->blkno +=
(bp->b_bcount / st->blksize);
else
st->blkno++;
}
}
mutex_exit(&st->sc_iolock);
rnd_add_uint32(&st->rnd_source, bp->b_blkno);
biodone(bp);
}
}
static int
stread(dev_t dev, struct uio *uio, int iomode)
{
struct st_softc *st = device_lookup_private(&st_cd, STUNIT(dev));
int r = physio(ststrategy, NULL, dev, B_READ,
st->sc_periph->periph_channel->chan_adapter->adapt_minphys, uio);
SC_DEBUG(st->sc_periph, SCSIPI_DB1, ("[stread: result=%d]\n", r));
return r;
}
static int
stwrite(dev_t dev, struct uio *uio, int iomode)
{
struct st_softc *st = device_lookup_private(&st_cd, STUNIT(dev));
int r = physio(ststrategy, NULL, dev, B_WRITE,
st->sc_periph->periph_channel->chan_adapter->adapt_minphys, uio);
SC_DEBUG(st->sc_periph, SCSIPI_DB1, ("[stwrite: result=%d]\n", r));
return r;
}
static int
stioctl(dev_t dev, u_long cmd, void *arg, int flag, struct lwp *l)
{
int error = 0;
int unit;
int number, nmarks, dsty;
int flags;
struct st_softc *st;
int hold_blksize;
uint8_t hold_density;
struct mtop *mt = (struct mtop *) arg;
flags = 0;
unit = STUNIT(dev);
dsty = STDSTY(dev);
st = device_lookup_private(&st_cd, unit);
hold_blksize = st->blksize;
hold_density = st->density;
switch ((u_int)cmd) {
case MTIOCGET: {
struct mtget *g = (struct mtget *) arg;
error = st->ops(st, ST_OPS_MODESENSE, XS_CTL_SILENT);
if (error) {
if (STMODE(dev) != CTRL_MODE)
break;
error = 0;
}
SC_DEBUG(st->sc_periph, SCSIPI_DB1, ("[ioctl: get status]\n"));
memset(g, 0, sizeof(struct mtget));
g->mt_type = MT_ISAR;
g->mt_blksiz = st->blksize;
g->mt_density = st->density;
g->mt_mblksiz[0] = st->modes[0].blksize;
g->mt_mblksiz[1] = st->modes[1].blksize;
g->mt_mblksiz[2] = st->modes[2].blksize;
g->mt_mblksiz[3] = st->modes[3].blksize;
g->mt_mdensity[0] = st->modes[0].density;
g->mt_mdensity[1] = st->modes[1].density;
g->mt_mdensity[2] = st->modes[2].density;
g->mt_mdensity[3] = st->modes[3].density;
g->mt_fileno = st->fileno;
g->mt_blkno = st->blkno;
if (st->flags & ST_READONLY)
g->mt_dsreg |= MT_DS_RDONLY;
if (st->flags & ST_MOUNTED)
g->mt_dsreg |= MT_DS_MOUNTED;
g->mt_resid = st->mt_resid;
g->mt_erreg = st->mt_erreg;
st->mt_resid = 0;
st->mt_erreg = 0;
st->asc = 0;
st->ascq = 0;
break;
}
case MTIOCTOP: {
SC_DEBUG(st->sc_periph, SCSIPI_DB1,
("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op,
mt->mt_count));
number = mt->mt_count;
switch ((short) (mt->mt_op)) {
case MTWEOF:
error = st_write_filemarks(st, number, flags);
break;
case MTBSF:
number = -number;
case MTFSF:
error = st_check_eod(st, FALSE, &nmarks, flags);
if (!error)
error = st_space(st, number - nmarks,
SP_FILEMARKS, flags);
break;
case MTBSR:
number = -number;
case MTFSR:
error = st_check_eod(st, true, &nmarks, flags);
if (!error)
error = st_space(st, number, SP_BLKS, flags);
break;
case MTREW:
error = st_rewind(st, 0, flags);
break;
case MTOFFL:
st_unmount(st, EJECT);
break;
case MTNOP:
break;
case MTRETEN:
error = st_load(st, LD_RETENSION, flags);
if (!error)
error = st_load(st, LD_LOAD, flags);
break;
case MTEOM:
error = st_check_eod(st, FALSE, &nmarks, flags);
if (!error)
error = st_space(st, 1, SP_EOM, flags);
break;
case MTCACHE:
st->flags &= ~ST_DONTBUFFER;
goto try_new_value;
case MTNOCACHE:
st->flags |= ST_DONTBUFFER;
goto try_new_value;
case MTERASE:
error = st_erase(st, number, flags);
break;
case MTSETBSIZ:
#ifdef NOTYET
if (!(st->flags & ST_NEW_MOUNT)) {
uprintf("re-mount tape before changing "
"blocksize");
error = EINVAL;
break;
}
#endif
if (number == 0)
st->flags &= ~ST_FIXEDBLOCKS;
else {
if ((st->blkmin || st->blkmax) &&
(number < st->blkmin ||
number > st->blkmax)) {
error = EINVAL;
break;
}
st->flags |= ST_FIXEDBLOCKS;
}
st->blksize = number;
st->flags |= ST_BLOCK_SET;
goto try_new_value;
case MTSETDNSTY:
if (number < 0 || number > 255) {
error = EINVAL;
break;
} else
st->density = number;
goto try_new_value;
case MTCMPRESS:
error = st->ops(st, (number == 0) ?
ST_OPS_CMPRSS_OFF : ST_OPS_CMPRSS_ON,
XS_CTL_SILENT);
break;
case MTEWARN:
if (number)
st->flags |= ST_EARLYWARN;
else
st->flags &= ~ST_EARLYWARN;
break;
default:
error = EINVAL;
}
break;
}
case MTIOCIEOT:
case MTIOCEEOT:
break;
case MTIOCRDSPOS:
error = st_rdpos(st, 0, (uint32_t *)arg);
break;
case MTIOCRDHPOS:
error = st_rdpos(st, 1, (uint32_t *)arg);
break;
case MTIOCSLOCATE:
error = st_setpos(st, 0, (uint32_t *)arg);
break;
case MTIOCHLOCATE:
error = st_setpos(st, 1, (uint32_t *)arg);
break;
default:
error = scsipi_do_ioctl(st->sc_periph, dev, cmd, arg, flag, l);
break;
}
return error;
try_new_value:
if ((STMODE(dev) != CTRL_MODE || (st->flags & ST_MOUNTED) != 0) &&
(error = st->ops(st, ST_OPS_MODESELECT, 0)) != 0) {
aprint_error_dev(st->sc_dev, "cannot set selected mode\n");
st->density = hold_density;
st->blksize = hold_blksize;
if (st->blksize)
st->flags |= ST_FIXEDBLOCKS;
else
st->flags &= ~ST_FIXEDBLOCKS;
return error;
}
if (STMODE(dev) == CTRL_MODE) {
switch ((short) (mt->mt_op)) {
case MTSETBSIZ:
st->modes[dsty].blksize = st->blksize;
st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
break;
case MTSETDNSTY:
st->modes[dsty].density = st->density;
st->modeflags[dsty] |= DENSITY_SET_BY_USER;
break;
}
}
return 0;
}
static int
st_read(struct st_softc *st, char *bf, int size, int flags)
{
struct scsi_rw_tape cmd;
if (size == 0)
return 0;
memset(&cmd, 0, sizeof(cmd));
cmd.opcode = READ;
if (st->flags & ST_FIXEDBLOCKS) {
cmd.byte2 |= SRW_FIXED;
_lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE),
cmd.len);
} else
_lto3b(size, cmd.len);
return scsipi_command(st->sc_periph,
(void *)&cmd, sizeof(cmd), (void *)bf, size, 0, ST_IO_TIME, NULL,
flags | XS_CTL_DATA_IN);
}
static int
st_erase(struct st_softc *st, int full, int flags)
{
int tmo;
struct scsi_erase cmd;
memset(&cmd, 0, sizeof(cmd));
cmd.opcode = ERASE;
if (full) {
cmd.byte2 = SE_LONG;
tmo = ST_SPC_TIME;
} else
tmo = ST_IO_TIME;
if ((st->quirks & ST_Q_ERASE_NOIMM) == 0)
cmd.byte2 |= SE_IMMED;
return scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
ST_RETRIES, tmo, NULL, flags);
}
static int
st_space(struct st_softc *st, int number, u_int what, int flags)
{
struct scsi_space cmd;
int error;
switch (what) {
case SP_BLKS:
if (st->flags & ST_PER_ACTION) {
if (number > 0) {
st->flags &= ~ST_PER_ACTION;
return EIO;
} else if (number < 0) {
if (st->flags & ST_AT_FILEMARK) {
error = st_space(st, 0, SP_FILEMARKS,
flags);
if (error)
return error;
}
if (st->flags & ST_BLANK_READ) {
st->flags &= ~ST_BLANK_READ;
return EIO;
}
st->flags &= ~(ST_EIO_PENDING|ST_EOM_PENDING);
}
}
break;
case SP_FILEMARKS:
if (st->flags & ST_EIO_PENDING) {
if (number > 0) {
st->flags &= ~ST_EIO_PENDING;
return EIO;
} else if (number < 0) {
st->flags &= ~ST_EIO_PENDING;
}
}
if (st->flags & ST_AT_FILEMARK) {
st->flags &= ~ST_AT_FILEMARK;
number--;
}
if ((st->flags & ST_BLANK_READ) && (number < 0)) {
st->flags &= ~ST_BLANK_READ;
number++;
}
break;
case SP_EOM:
if (st->flags & ST_EOM_PENDING) {
st->flags &= ~ST_EOM_PENDING;
return 0;
}
if (st->flags & ST_EIO_PENDING) {
st->flags &= ~ST_EIO_PENDING;
return EIO;
}
if (st->flags & ST_AT_FILEMARK)
st->flags &= ~ST_AT_FILEMARK;
break;
}
if (number == 0)
return 0;
memset(&cmd, 0, sizeof(cmd));
cmd.opcode = SPACE;
cmd.byte2 = what;
_lto3b(number, cmd.number);
st->flags &= ~ST_POSUPDATED;
st->last_ctl_resid = 0;
error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
0, ST_SPC_TIME, NULL, flags);
if (error == 0 && (st->flags & ST_POSUPDATED) == 0) {
number = number - st->last_ctl_resid;
if (what == SP_BLKS) {
if (st->blkno != -1)
st->blkno += number;
} else if (what == SP_FILEMARKS) {
if (st->fileno != -1) {
st->fileno += number;
if (number > 0)
st->blkno = 0;
else if (number < 0)
st->blkno = -1;
}
} else if (what == SP_EOM) {
st_updatefilepos(st);
}
}
return error;
}
static int
st_write_filemarks(struct st_softc *st, int number, int flags)
{
int error;
struct scsi_write_filemarks cmd;
if (number < 0) {
SC_DEBUG(st->sc_periph, SCSIPI_DB3,
("EINVAL: st_write_filemarks not writing %d file marks\n", number));
return EINVAL;
}
switch (number) {
case 0:
break;
case 1:
if (st->flags & ST_FM_WRITTEN)
st->flags &= ~ST_WRITTEN;
else
st->flags |= ST_FM_WRITTEN;
st->flags &= ~ST_PER_ACTION;
break;
default:
st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
}
memset(&cmd, 0, sizeof(cmd));
cmd.opcode = WRITE_FILEMARKS;
if (SCSIPI_BUSTYPE_TYPE(scsipi_periph_bustype(st->sc_periph)) ==
SCSIPI_BUSTYPE_ATAPI)
cmd.byte2 = SR_IMMED;
if ((st->quirks & ST_Q_NOFILEMARKS) == 0)
_lto3b(number, cmd.number);
error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
0, ST_IO_TIME * 4, NULL, flags);
if (error == 0 && st->fileno != -1)
st->fileno += number;
return error;
}
static int
st_check_eod(struct st_softc *st, boolean position, int *nmarks, int flags)
{
int error;
switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
default:
*nmarks = 0;
return 0;
case ST_WRITTEN:
case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
*nmarks = 1;
break;
case ST_WRITTEN | ST_2FM_AT_EOD:
*nmarks = 2;
}
error = st_write_filemarks(st, *nmarks, flags);
if (position && !error)
error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
return error;
}
static int
st_load(struct st_softc *st, u_int type, int flags)
{
int error;
struct scsi_load cmd;
if (type != LD_LOAD) {
int nmarks;
error = st_check_eod(st, FALSE, &nmarks, flags);
if (error) {
aprint_error_dev(st->sc_dev,
"failed to write closing filemarks at "
"unload, errno=%d\n", error);
return error;
}
}
if (st->quirks & ST_Q_IGNORE_LOADS) {
if (type == LD_LOAD)
return st_rewind(st, 0, flags);
}
memset(&cmd, 0, sizeof(cmd));
cmd.opcode = LOAD;
if (SCSIPI_BUSTYPE_TYPE(scsipi_periph_bustype(st->sc_periph)) ==
SCSIPI_BUSTYPE_ATAPI)
cmd.byte2 = SR_IMMED;
cmd.how = type;
error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
ST_RETRIES, ST_SPC_TIME, NULL, flags);
if (error) {
aprint_error_dev(st->sc_dev, "error %d in st_load (op %d)\n",
error, type);
}
return error;
}
static int
st_rewind(struct st_softc *st, u_int immediate, int flags)
{
struct scsi_rewind cmd;
int error;
int nmarks;
int timeout;
error = st_check_eod(st, FALSE, &nmarks, flags);
if (error) {
aprint_error_dev(st->sc_dev,
"failed to write closing filemarks at "
"rewind, errno=%d\n", error);
return error;
}
st->flags &= ~ST_PER_ACTION;
timeout = immediate ? ST_CTL_TIME : ST_SPC_TIME;
if (scsipi_periph_bustype(st->sc_periph) == SCSIPI_BUSTYPE_ATAPI)
immediate = SR_IMMED;
memset(&cmd, 0, sizeof(cmd));
cmd.opcode = REWIND;
cmd.byte2 = immediate;
error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
ST_RETRIES, timeout, NULL, flags);
if (error) {
aprint_error_dev(st->sc_dev, "error %d trying to rewind\n",
error);
st->fileno = st->blkno = -1;
} else
st->fileno = st->blkno = 0;
return error;
}
static void
st_updatefilepos(struct st_softc *st)
{
int error;
uint8_t posdata[32];
struct scsi_tape_read_position cmd;
memset(&cmd, 0, sizeof(cmd));
memset(&posdata, 0, sizeof(posdata));
cmd.opcode = READ_POSITION;
cmd.byte1 = 6;
error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd),
(void *)&posdata, sizeof(posdata), ST_RETRIES, ST_CTL_TIME, NULL,
XS_CTL_SILENT | XS_CTL_DATA_IN);
if (error == 0) {
#ifdef SCSIPI_DEBUG
if (st->sc_periph->periph_dbflags & SCSIPI_DB3) {
int hard;
printf("posdata: ");
for (hard = 0; hard < sizeof(posdata); hard++)
printf("%02x ", posdata[hard] & 0xff);
printf("\n");
}
#endif
if (posdata[0] & 0xC) {
SC_DEBUG(st->sc_periph, SCSIPI_DB3,
("st_updatefilepos block/mark position unknown (0x%02x)\n",
posdata[0]));
} else {
st->fileno = _8btol(&posdata[16]);
st->blkno = 0;
SC_DEBUG(st->sc_periph, SCSIPI_DB3,
("st_updatefilepos file position %"PRId64"\n",
st->fileno));
return;
}
} else {
SC_DEBUG(st->sc_periph, SCSIPI_DB3,
("st_updatefilepos READ POSITION(LONG_FORM) failed (error=%d)\n",
error));
}
st->fileno = -1;
st->blkno = -1;
}
static int
st_rdpos(struct st_softc *st, int hard, uint32_t *blkptr)
{
int error;
uint8_t posdata[20];
struct scsi_tape_read_position cmd;
if (hard && (st->flags & ST_WRITTEN)) {
error = st_write_filemarks(st, 0, XS_CTL_SILENT);
if (error != 0 && error != EACCES && error != EROFS)
return error;
}
memset(&cmd, 0, sizeof(cmd));
memset(&posdata, 0, sizeof(posdata));
cmd.opcode = READ_POSITION;
if (hard)
cmd.byte1 = 1;
error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd),
(void *)&posdata, sizeof(posdata), ST_RETRIES, ST_CTL_TIME, NULL,
XS_CTL_SILENT | XS_CTL_DATA_IN);
if (error == 0) {
#if 0
printf("posdata:");
for (hard = 0; hard < sizeof(posdata); hard++)
printf("%02x ", posdata[hard] & 0xff);
printf("\n");
#endif
if (posdata[0] & 0x4) {
SC_DEBUG(st->sc_periph, SCSIPI_DB3,
("EINVAL: strdpos block position unknown\n"));
error = EINVAL;
}
else
*blkptr = _4btol(&posdata[4]);
}
return error;
}
static int
st_setpos(struct st_softc *st, int hard, uint32_t *blkptr)
{
int error;
struct scsi_tape_locate cmd;
memset(&cmd, 0, sizeof(cmd));
cmd.opcode = LOCATE;
if (hard)
cmd.byte2 = 1 << 2;
_lto4b(*blkptr, cmd.blkaddr);
error = scsipi_command(st->sc_periph, (void *)&cmd, sizeof(cmd), 0, 0,
ST_RETRIES, ST_SPC_TIME, NULL, 0);
st->fileno = st->blkno = -1;
return error;
}
static int
st_interpret_sense(struct scsipi_xfer *xs)
{
struct scsipi_periph *periph = xs->xs_periph;
struct scsi_sense_data *sense = &xs->sense.scsi_sense;
struct buf *bp = xs->bp;
struct st_softc *st = device_private(periph->periph_dev);
int retval = EJUSTRETURN;
int doprint = ((xs->xs_control & XS_CTL_SILENT) == 0);
uint8_t key;
int32_t info;
if (SSD_RCODE(sense->response_code) != SSD_RCODE_CURRENT &&
SSD_RCODE(sense->response_code) != SSD_RCODE_DEFERRED)
return retval;
if (sense->response_code & SSD_RCODE_VALID)
info = _4btol(sense->info);
else
info = (st->flags & ST_FIXEDBLOCKS) ?
xs->datalen / st->blksize : xs->datalen;
key = SSD_SENSE_KEY(sense->flags);
st->mt_erreg = key;
st->asc = sense->asc;
st->ascq = sense->ascq;
st->mt_resid = (short) info;
if (key == SKEY_NOT_READY && st->asc == 0x4 && st->ascq == 0x1) {
if (!callout_pending(&periph->periph_callout))
scsipi_periph_freeze(periph, 1);
callout_reset(&periph->periph_callout,
hz, scsipi_periph_timed_thaw, periph);
return ERESTART;
}
if ((periph->periph_flags & PERIPH_OPEN) == 0)
return retval;
xs->resid = info;
if (st->flags & ST_FIXEDBLOCKS) {
if (bp) {
xs->resid *= st->blksize;
st->last_io_resid = xs->resid;
} else
st->last_ctl_resid = xs->resid;
if (key == SKEY_VOLUME_OVERFLOW) {
st->flags |= ST_EIO_PENDING;
if (bp)
bp->b_resid = xs->resid;
} else if (sense->flags & SSD_EOM) {
if ((st->flags & ST_EARLYWARN) == 0)
st->flags |= ST_EIO_PENDING;
st->flags |= ST_EOM_PENDING;
if (bp) {
#if 0
bp->b_resid = xs->resid;
#else
bp->b_resid = 0;
#endif
}
}
if (sense->flags & SSD_FILEMARK) {
st->flags |= ST_AT_FILEMARK;
if (bp)
bp->b_resid = xs->resid;
if (st->fileno != (daddr_t) -1) {
st->fileno++;
st->blkno = 0;
st->flags |= ST_POSUPDATED;
}
}
if (sense->flags & SSD_ILI) {
st->flags |= ST_EIO_PENDING;
if (bp)
bp->b_resid = xs->resid;
if (sense->response_code & SSD_RCODE_VALID &&
(xs->xs_control & XS_CTL_SILENT) == 0)
aprint_error_dev(st->sc_dev,
"block wrong size, %d blocks residual\n",
info);
if ((st->quirks & ST_Q_SENSE_HELP) &&
(periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
st->blksize -= 512;
else if ((st->flags & ST_POSUPDATED) == 0) {
if (st->blkno != (daddr_t) -1) {
st->blkno +=
(xs->datalen / st->blksize);
st->flags |= ST_POSUPDATED;
}
}
}
if (xs->datalen && xs->resid >= xs->datalen) {
if (st->flags & ST_EIO_PENDING)
return EIO;
if (st->flags & ST_AT_FILEMARK) {
if (bp)
bp->b_resid = xs->resid;
return 0;
}
}
} else {
if (bp)
st->last_io_resid = xs->resid;
else
st->last_ctl_resid = xs->resid;
if (sense->flags & SSD_EOM) {
if (st->flags & ST_EARLYWARN) {
st->flags |= ST_EOM_PENDING;
retval = 0;
} else {
retval = EIO;
if (xs->resid == 0)
xs->resid = xs->datalen;
}
if (key == SKEY_NO_SENSE) {
doprint = 0;
}
} else if (sense->flags & SSD_FILEMARK) {
retval = 0;
if (st->fileno != (daddr_t) -1) {
st->fileno++;
st->blkno = 0;
st->flags |= ST_POSUPDATED;
}
} else if (sense->flags & SSD_ILI) {
if (info < 0) {
if ((xs->xs_control & XS_CTL_SILENT) == 0) {
aprint_error_dev(st->sc_dev,
"%d-byte tape record too big"
" for %d-byte user buffer\n",
xs->datalen - info, xs->datalen);
}
retval = EIO;
} else {
retval = 0;
if (st->blkno != (daddr_t) -1) {
st->blkno++;
st->flags |= ST_POSUPDATED;
}
}
}
if (bp)
bp->b_resid = xs->resid;
}
#ifndef SCSIPI_DEBUG
if (retval == 0 && key == SKEY_NO_SENSE)
doprint = 0;
#endif
if (key == SKEY_BLANK_CHECK) {
if ((st->quirks & ST_Q_SENSE_HELP) &&
(periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
st->blksize -= 512;
} else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
st->flags |= ST_BLANK_READ;
xs->resid = xs->datalen;
if (bp) {
bp->b_resid = xs->resid;
}
retval = 0;
st->fileno = st->blkno = -1;
}
}
if (retval == EJUSTRETURN)
doprint = 0;
if (doprint) {
if (scsipi_print_sense(xs, 0) != 0)
return retval;
scsipi_printaddr(periph);
printf("Sense Key 0x%02x", key);
if ((sense->response_code & SSD_RCODE_VALID) != 0) {
switch (key) {
case SKEY_NOT_READY:
case SKEY_ILLEGAL_REQUEST:
case SKEY_UNIT_ATTENTION:
case SKEY_DATA_PROTECT:
break;
case SKEY_VOLUME_OVERFLOW:
case SKEY_BLANK_CHECK:
printf(", requested size: %d (decimal)", info);
break;
case SKEY_ABORTED_COMMAND:
if (xs->xs_retries)
printf(", retrying");
printf(", cmd 0x%x, info 0x%x",
xs->cmd->opcode, info);
break;
default:
printf(", info = %d (decimal)", info);
}
}
if (sense->extra_len != 0) {
int n;
printf(", data =");
for (n = 0; n < sense->extra_len; n++)
printf(" %02x", sense->csi[n]);
}
printf("\n");
}
return retval;
}
static int
st_touch_tape(struct st_softc *st)
{
char *bf;
int readsize;
int error;
bf = malloc(1024, M_TEMP, M_WAITOK);
if ((error = st->ops(st, ST_OPS_MODESENSE, 0)) != 0)
goto bad;
if (st->media_blksize > 0)
st->blksize = st->media_blksize;
else
st->blksize = 1024;
do {
switch (st->blksize) {
case 512:
case 1024:
readsize = st->blksize;
st->flags |= ST_FIXEDBLOCKS;
break;
default:
readsize = 1;
st->flags &= ~ST_FIXEDBLOCKS;
}
if ((error = st->ops(st, ST_OPS_MODESELECT, XS_CTL_SILENT))
!= 0) {
if (readsize == 1)
goto bad;
st->blksize -= 512;
continue;
}
st_read(st, bf, readsize, XS_CTL_SILENT);
if ((error = st_rewind(st, 0, 0)) != 0) {
bad: free(bf, M_TEMP);
return error;
}
} while (readsize != 1 && readsize > st->blksize);
free(bf, M_TEMP);
return 0;
}
static int
stdump(dev_t dev, daddr_t blkno, void *va, size_t size)
{
return ENXIO;
}
int
st_mode_select(struct st_softc *st, int flags)
{
u_int select_len;
struct select {
struct scsi_mode_parameter_header_6 header;
struct scsi_general_block_descriptor blk_desc;
u_char sense_data[MAX_PAGE_0_SIZE];
} select;
struct scsipi_periph *periph = st->sc_periph;
select_len = sizeof(select.header) + sizeof(select.blk_desc) +
st->page_0_size;
if (st->quirks & ST_Q_UNIMODAL) {
SC_DEBUG(periph, SCSIPI_DB3,
("not setting density 0x%x blksize 0x%x\n",
st->density, st->blksize));
return 0;
}
memset(&select, 0, sizeof(select));
select.header.blk_desc_len = sizeof(struct
scsi_general_block_descriptor);
select.header.dev_spec &= ~SMH_DSP_BUFF_MODE;
select.blk_desc.density = st->density;
if (st->flags & ST_DONTBUFFER)
select.header.dev_spec |= SMH_DSP_BUFF_MODE_OFF;
else
select.header.dev_spec |= SMH_DSP_BUFF_MODE_ON;
if (st->flags & ST_FIXEDBLOCKS)
_lto3b(st->blksize, select.blk_desc.blklen);
if (st->page_0_size)
memcpy(select.sense_data, st->sense_data, st->page_0_size);
return scsipi_mode_select(periph, 0, &select.header, select_len,
flags, ST_RETRIES, ST_CTL_TIME);
}