/*-
* Copyright (c) 1996, 1997, 1998, 1999, 2004 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``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 FOUNDATION OR CONTRIBUTORS
* 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.
*/
/*
* The following information is obtained from the
* element address assignment page.
*/
int sc_firsts[4]; /* firsts, indexed by CHET_* */
int sc_counts[4]; /* counts, indexed by CHET_* */
/*
* The following mask defines the legal combinations
* of elements for the MOVE MEDIUM command.
*/
u_int8_t sc_movemask[4];
/*
* As above, but for EXCHANGE MEDIUM.
*/
u_int8_t sc_exchangemask[4];
/*
* Quirks; see below.
*/
int sc_settledelay; /* delay for settle */
/* SCSI glue */
static int ch_interpret_sense(struct scsipi_xfer *);
static const struct scsipi_periphsw ch_switch = {
ch_interpret_sense, /* check our error handler first */
NULL, /* no queue; our commands are synchronous */
NULL, /* have no async handler */
NULL, /* nothing to be done when xfer is done */
};
static int ch_move(struct ch_softc *, struct changer_move_request *);
static int ch_exchange(struct ch_softc *,
struct changer_exchange_request *);
static int ch_position(struct ch_softc *,
struct changer_position_request *);
static int ch_ielem(struct ch_softc *);
static int ch_ousergetelemstatus(struct ch_softc *, int, u_int8_t *);
static int ch_usergetelemstatus(struct ch_softc *,
struct changer_element_status_request *);
static int ch_getelemstatus(struct ch_softc *, int, int, void *,
size_t, int, int);
static int ch_setvoltag(struct ch_softc *,
struct changer_set_voltag_request *);
static int ch_get_params(struct ch_softc *, int);
static void ch_get_quirks(struct ch_softc *,
struct scsipi_inquiry_pattern *);
static void ch_event(struct ch_softc *, u_int);
static int ch_map_element(struct ch_softc *, u_int16_t, int *, int *);
/* Glue into the SCSI bus */
sc->sc_periph = periph;
periph->periph_dev = sc->sc_dev;
periph->periph_switch = &ch_switch;
printf("\n");
/*
* Find out our device's quirks.
*/
ch_get_quirks(sc, &sa->sa_inqbuf);
/*
* Some changers require a long time to settle out, to do
* tape inventory, for instance.
*/
if (sc->sc_settledelay) {
printf("%s: waiting %d seconds for changer to settle...\n",
device_xname(sc->sc_dev), sc->sc_settledelay);
delay(1000000 * sc->sc_settledelay);
}
/* Default the current picker. */
sc->sc_picker = sc->sc_firsts[CHET_MT];
}
static int
chopen(dev_t dev, int flags, int fmt, struct lwp *l)
{
struct ch_softc *sc;
struct scsipi_periph *periph;
struct scsipi_adapter *adapt;
int unit, error;
unit = CHUNIT(dev);
sc = device_lookup_private(&ch_cd, unit);
if (sc == NULL)
return (ENXIO);
/*
* Only allow one open at a time.
*/
if (periph->periph_flags & PERIPH_OPEN)
return (EBUSY);
if ((error = scsipi_adapter_addref(adapt)) != 0)
return (error);
/*
* Make sure the unit is on-line. If a UNIT ATTENTION
* occurs, we will mark that an Init-Element-Status is
* needed in ch_get_params().
*
* We ignore NOT READY in case e.g a magazine isn't actually
* loaded into the changer or a tape isn't in the drive.
*/
error = scsipi_test_unit_ready(periph, XS_CTL_IGNORE_NOT_READY);
if (error)
goto bad;
periph->periph_flags |= PERIPH_OPEN;
/*
* Make sure our parameters are up to date.
*/
if ((error = ch_get_params(sc, 0)) != 0)
goto bad;
static int
chread(dev_t dev, struct uio *uio, int flags)
{
struct ch_softc *sc = device_lookup_private(&ch_cd, CHUNIT(dev));
int error;
if (uio->uio_resid != CHANGER_EVENT_SIZE)
return (EINVAL);
/*
* Read never blocks; if there are no events pending, we just
* return an all-clear bitmask.
*/
error = uiomove(&sc->sc_events, CHANGER_EVENT_SIZE, uio);
if (error == 0)
sc->sc_events = 0;
return (error);
}
static int
chioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
{
struct ch_softc *sc = device_lookup_private(&ch_cd, CHUNIT(dev));
int error = 0;
/*
* If this command can change the device's state, we must
* have the device open for writing.
*/
switch (cmd) {
case CHIOGPICKER:
case CHIOGPARAMS:
case OCHIOGSTATUS:
break;
default:
if ((flags & FWRITE) == 0)
return (EBADF);
}
/*
* If the periph is already recovering, just do the
* normal error recovering.
*/
if (periph->periph_flags & PERIPH_RECOVERING)
return (EJUSTRETURN);
/*
* If it isn't an extended or extended/deferred error, let
* the generic code handle it.
*/
if (SSD_RCODE(sense->response_code) != SSD_RCODE_CURRENT &&
SSD_RCODE(sense->response_code) != SSD_RCODE_DEFERRED)
return (EJUSTRETURN);
/*
* We're only interested in condtions that
* indicate potential inventory violation.
*
* We use ASC/ASCQ codes for this.
*/
switch (asc_ascq) {
case 0x2800:
/* "Not Ready To Ready Transition (Medium May Have Changed)" */
case 0x2900:
/* "Power On, Reset, or Bus Device Reset Occurred" */
sc->sc_periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
/*
* Enqueue an Element-Status-Changed event, and wake up
* any processes waiting for them.
*/
if ((xs->xs_control & XS_CTL_IGNORE_MEDIA_CHANGE) == 0)
ch_event(sc, CHEV_ELEMENT_STATUS_CHANGED);
break;
default:
break;
}
/*
* Perform a READ ELEMENT STATUS on behalf of the user, and return to
* the user only the data the user is interested in. This returns the
* old data format.
*/
static int
ch_ousergetelemstatus(struct ch_softc *sc, int chet, u_int8_t *uptr)
{
struct read_element_status_header *st_hdrp, st_hdr;
struct read_element_status_page_header *pg_hdrp;
struct read_element_status_descriptor *desc;
size_t size, desclen;
void *data;
int avail, i, error = 0;
u_int8_t user_data;
/*
* If there are no elements of the requested type in the changer,
* the request is invalid.
*/
if (sc->sc_counts[chet] == 0)
return (EINVAL);
/*
* Do the request the user wants, but only read the status header.
* This will tell us the amount of storage we must allocate in
* order to read all data.
*/
error = ch_getelemstatus(sc, sc->sc_firsts[chet],
sc->sc_counts[chet], &st_hdr, sizeof(st_hdr), 0, 0);
if (error)
return (error);
/*
* We must have at least room for the status header and
* one page header (since we only ask for one element type
* at a time).
*/
if (size < (sizeof(struct read_element_status_header) +
sizeof(struct read_element_status_page_header)))
return (EIO);
/*
* Allocate the storage and do the request again.
*/
data = malloc(size, M_DEVBUF, M_WAITOK);
error = ch_getelemstatus(sc, sc->sc_firsts[chet],
sc->sc_counts[chet], data, size, 0, 0);
if (error)
goto done;
/*
* Fill in the user status array.
*/
avail = _2btol(st_hdrp->count);
if (avail != sc->sc_counts[chet])
printf("%s: warning, READ ELEMENT STATUS avail != count\n",
device_xname(sc->sc_dev));
desc = (struct read_element_status_descriptor *)((u_long)data +
sizeof(struct read_element_status_header) +
sizeof(struct read_element_status_page_header));
for (i = 0; i < avail; ++i) {
user_data = desc->flags1;
error = copyout(&user_data, &uptr[i], avail);
if (error)
break;
desc = (struct read_element_status_descriptor *)((u_long)desc
+ desclen);
}
done:
if (data != NULL)
free(data, M_DEVBUF);
return (error);
}
/*
* Perform a READ ELEMENT STATUS on behalf of the user. This returns
* the new (more complete) data format.
*/
static int
ch_usergetelemstatus(struct ch_softc *sc,
struct changer_element_status_request *cesr)
{
struct scsipi_channel *chan = sc->sc_periph->periph_channel;
struct scsipi_periph *dtperiph;
struct read_element_status_header *st_hdrp, st_hdr;
struct read_element_status_page_header *pg_hdrp;
struct read_element_status_descriptor *desc;
struct changer_volume_tag *avol, *pvol;
size_t size, desclen, stddesclen, offset;
int first, avail, i, error = 0;
void *data;
void *uvendptr;
struct changer_element_status ces;
/*
* Check arguments.
*/
if (cesr->cesr_type > CHET_DT)
return (EINVAL);
if (sc->sc_counts[cesr->cesr_type] == 0)
return (ENODEV);
if (cesr->cesr_unit > (sc->sc_counts[cesr->cesr_type] - 1))
return (ENODEV);
if (cesr->cesr_count >
(sc->sc_counts[cesr->cesr_type] + cesr->cesr_unit))
return (EINVAL);
/*
* Do the request the user wants, but only read the status header.
* This will tell us the amount of storage we must allocate
* in order to read all the data.
*/
error = ch_getelemstatus(sc, sc->sc_firsts[cesr->cesr_type] +
cesr->cesr_unit, cesr->cesr_count, &st_hdr, sizeof(st_hdr), 0,
cesr->cesr_flags);
if (error)
return (error);
/*
* We must have at least room for the status header and
* one page header (since we only ask for oen element type
* at a time).
*/
if (size < (sizeof(struct read_element_status_header) +
sizeof(struct read_element_status_page_header)))
return (EIO);
/*
* Allocate the storage and do the request again.
*/
data = malloc(size, M_DEVBUF, M_WAITOK);
error = ch_getelemstatus(sc, sc->sc_firsts[cesr->cesr_type] +
cesr->cesr_unit, cesr->cesr_count, data, size, 0,
cesr->cesr_flags);
if (error)
goto done;
/*
* Fill in the user status array.
*/
first = _2btol(st_hdrp->fear);
if (first < (sc->sc_firsts[cesr->cesr_type] + cesr->cesr_unit) ||
first >= (sc->sc_firsts[cesr->cesr_type] + cesr->cesr_unit +
cesr->cesr_count)) {
error = EIO;
goto done;
}
first -= sc->sc_firsts[cesr->cesr_type] + cesr->cesr_unit;
for (i = 0; i < cesr->cesr_count; i++) {
memset(&ces, 0, sizeof(ces));
if (i < first || i >= (first + avail)) {
error = copyout(&ces, &cesr->cesr_data[i],
sizeof(ces));
if (error)
goto done;
}
/*
* For Data Transport elemenets, get the SCSI ID and LUN,
* and attempt to map them to a device name if they're
* on the same SCSI bus.
*/
if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_IDVALID) {
ces.ces_target = desc->dt_scsi_addr;
ces.ces_flags |= CESTATUS_TARGET_VALID;
}
if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_LUVALID) {
ces.ces_lun = desc->dt_scsi_flags &
READ_ELEMENT_STATUS_DT_LUNMASK;
ces.ces_flags |= CESTATUS_LUN_VALID;
}
if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_NOTBUS)
ces.ces_flags |= CESTATUS_NOTBUS;
else if ((ces.ces_flags &
(CESTATUS_TARGET_VALID|CESTATUS_LUN_VALID)) ==
(CESTATUS_TARGET_VALID|CESTATUS_LUN_VALID)) {
if (ces.ces_target < chan->chan_ntargets &&
ces.ces_lun < chan->chan_nluns &&
(dtperiph = scsipi_lookup_periph(chan,
ces.ces_target, ces.ces_lun)) != NULL &&
dtperiph->periph_dev != NULL) {
strlcpy(ces.ces_xname,
device_xname(dtperiph->periph_dev),
sizeof(ces.ces_xname));
ces.ces_flags |= CESTATUS_XNAME_VALID;
}
}
if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
ces.ces_flags |= CESTATUS_INVERTED;
if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
if (ch_map_element(sc, _2btol(desc->ssea),
&ces.ces_from_type, &ces.ces_from_unit))
ces.ces_flags |= CESTATUS_FROM_VALID;
}
/*
* Compute vendor-specific length. Note the 4 reserved
* bytes between the volume tags and the vendor-specific
* data. Copy it out of the user wants it.
*/
stddesclen += 4;
if (desclen > stddesclen)
ces.ces_vendor_len = desclen - stddesclen;
/*
* Now copy out the status descriptor we've constructed.
*/
error = copyout(&ces, &cesr->cesr_data[i], sizeof(ces));
if (error)
goto done;
}
done:
if (data != NULL)
free(data, M_DEVBUF);
return (error);
}
static int
ch_getelemstatus(struct ch_softc *sc, int first, int count, void *data,
size_t datalen, int scsiflags, int flags)
{
struct scsi_read_element_status cmd;
/*
* Send command to changer.
*
* The problem is, how long to allow for the command?
* It can take a *really* long time, and also depends
* on unknowable factors such as whether there are
* *almost* readable labels on tapes that a barcode
* reader is trying to decipher.
*
* I'm going to make this long enough to allow 5 minutes
* per element plus an initial 10 minute wait.
*/
tmo = sc->sc_counts[CHET_MT] +
sc->sc_counts[CHET_ST] +
sc->sc_counts[CHET_IE] +
sc->sc_counts[CHET_DT];
tmo *= 5 * 60 * 1000;
tmo += (10 * 60 * 1000);
/*
* Ask the device about itself and fill in the parameters in our
* softc.
*/
static int
ch_get_params(struct ch_softc *sc, int scsiflags)
{
struct scsi_mode_sense_data {
struct scsi_mode_parameter_header_6 header;
union {
struct page_element_address_assignment ea;
struct page_transport_geometry_parameters tg;
struct page_device_capabilities cap;
} pages;
} sense_data;
int error, from;
u_int8_t *moves, *exchanges;
/*
* Grab info from the element address assignment page.
*/
memset(&sense_data, 0, sizeof(sense_data));
error = scsipi_mode_sense(sc->sc_periph, SMS_DBD, 0x1d,
&sense_data.header, sizeof(sense_data),
scsiflags, CHRETRIES, 6000);
if (error) {
aprint_error_dev(sc->sc_dev, "could not sense element address page\n");
return (error);
}
/*
* Grab info from the capabilities page.
*/
memset(&sense_data, 0, sizeof(sense_data));
/*
* XXX: Note: not all changers can deal with disabled block descriptors
*/
error = scsipi_mode_sense(sc->sc_periph, SMS_DBD, 0x1f,
&sense_data.header, sizeof(sense_data),
scsiflags, CHRETRIES, 6000);
if (error) {
aprint_error_dev(sc->sc_dev, "could not sense capabilities page\n");
return (error);
}
#ifdef CH_AUTOMATIC_IELEM_POLICY
/*
* If we need to do an Init-Element-Status,
* do that now that we know what's in the changer.
*/
if ((scsiflags & XS_CTL_IGNORE_MEDIA_CHANGE) == 0) {
if ((sc->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
error = ch_ielem(sc);
if (error == 0)
sc->sc_periph->periph_flags |= PERIPH_MEDIA_LOADED;
else
sc->sc_periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
}
#endif
return (error);
}
/*
* Copy the volume tag string from the SCSI representation.
* Per the SCSI-2 spec, we stop at the first blank character.
*/
for (i = 0; i < sizeof(sv->volid); i++) {
if (sv->volid[i] == ' ')
break;
cv->cv_tag[i] = sv->volid[i];
}
cv->cv_tag[i] = '\0';
cv->cv_serial = _2btol(sv->volseq);
}
static int
ch_voltag_convert_out(const struct changer_voltag *cv,
struct changer_volume_tag *sv)
{
int i;
for (i = 0; i < sizeof(sv->volid); i++) {
if (cv->cv_tag[i] == '\0')
break;
/*
* Limit the character set to what is suggested in
* the SCSI-2 spec.
*/
if ((cv->cv_tag[i] < '0' || cv->cv_tag[i] > '9') &&
(cv->cv_tag[i] < 'A' || cv->cv_tag[i] > 'Z') &&
(cv->cv_tag[i] != '_'))
return (EINVAL);
sv->volid[i] = cv->cv_tag[i];
}