Home | History | Annotate | Line # | Download | only in scsipi
ch.c revision 1.34
      1 /*	$NetBSD: ch.c,v 1.34 1998/08/31 22:28:06 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996, 1997 Jason R. Thorpe <thorpej (at) and.com>
      5  * All rights reserved.
      6  *
      7  * Partially based on an autochanger driver written by Stefan Grefen
      8  * and on an autochanger driver written by the Systems Programming Group
      9  * at the University of Utah Computer Science Department.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgements:
     21  *	This product includes software developed by Jason R. Thorpe
     22  *	for And Communications, http://www.and.com/
     23  * 4. The name of the author may not be used to endorse or promote products
     24  *    derived from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     29  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     33  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     34  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/errno.h>
     42 #include <sys/ioctl.h>
     43 #include <sys/buf.h>
     44 #include <sys/proc.h>
     45 #include <sys/user.h>
     46 #include <sys/chio.h>
     47 #include <sys/device.h>
     48 #include <sys/malloc.h>
     49 #include <sys/conf.h>
     50 #include <sys/fcntl.h>
     51 
     52 #include <dev/scsipi/scsipi_all.h>
     53 #include <dev/scsipi/scsi_all.h>
     54 #include <dev/scsipi/scsi_changer.h>
     55 #include <dev/scsipi/scsiconf.h>
     56 
     57 #define CHRETRIES	2
     58 #define CHUNIT(x)	(minor((x)))
     59 
     60 struct ch_softc {
     61 	struct device	sc_dev;		/* generic device info */
     62 	struct scsipi_link *sc_link;	/* link in the SCSI bus */
     63 
     64 	int		sc_picker;	/* current picker */
     65 
     66 	/*
     67 	 * The following information is obtained from the
     68 	 * element address assignment page.
     69 	 */
     70 	int		sc_firsts[4];	/* firsts, indexed by CHET_* */
     71 	int		sc_counts[4];	/* counts, indexed by CHET_* */
     72 
     73 	/*
     74 	 * The following mask defines the legal combinations
     75 	 * of elements for the MOVE MEDIUM command.
     76 	 */
     77 	u_int8_t	sc_movemask[4];
     78 
     79 	/*
     80 	 * As above, but for EXCHANGE MEDIUM.
     81 	 */
     82 	u_int8_t	sc_exchangemask[4];
     83 
     84 	int		flags;		/* misc. info */
     85 
     86 	/*
     87 	 * Quirks; see below.
     88 	 */
     89 	int		sc_settledelay;	/* delay for settle */
     90 
     91 };
     92 
     93 /* sc_flags */
     94 #define CHF_ROTATE	0x01		/* picker can rotate */
     95 
     96 /* Autoconfiguration glue */
     97 int	chmatch __P((struct device *, struct cfdata *, void *));
     98 void	chattach __P((struct device *, struct device *, void *));
     99 
    100 struct cfattach ch_ca = {
    101 	sizeof(struct ch_softc), chmatch, chattach
    102 };
    103 
    104 extern struct cfdriver ch_cd;
    105 
    106 struct scsipi_inquiry_pattern ch_patterns[] = {
    107 	{T_CHANGER, T_REMOV,
    108 	 "",		"",		""},
    109 };
    110 
    111 /* SCSI glue */
    112 struct scsipi_device ch_switch = {
    113 	NULL, NULL, NULL, NULL
    114 };
    115 
    116 int	ch_move __P((struct ch_softc *, struct changer_move *));
    117 int	ch_exchange __P((struct ch_softc *, struct changer_exchange *));
    118 int	ch_position __P((struct ch_softc *, struct changer_position *));
    119 int	ch_ielem __P((struct ch_softc *));
    120 int	ch_usergetelemstatus __P((struct ch_softc *, int, u_int8_t *));
    121 int	ch_getelemstatus __P((struct ch_softc *, int, int, caddr_t, size_t));
    122 int	ch_get_params __P((struct ch_softc *, int));
    123 void	ch_get_quirks __P((struct ch_softc *,
    124 	    struct scsipi_inquiry_pattern *));
    125 
    126 /*
    127  * SCSI changer quirks.
    128  */
    129 struct chquirk {
    130 	struct	scsipi_inquiry_pattern cq_match; /* device id pattern */
    131 	int	cq_settledelay;	/* settle delay, in seconds */
    132 };
    133 
    134 struct chquirk chquirks[] = {
    135 	{{T_CHANGER, T_REMOV,
    136 	  "SPECTRA",	"9000",		"0200"},
    137 	 75},
    138 };
    139 
    140 int
    141 chmatch(parent, match, aux)
    142 	struct device *parent;
    143 	struct cfdata *match;
    144 	void *aux;
    145 {
    146 	struct scsipibus_attach_args *sa = aux;
    147 	int priority;
    148 
    149 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
    150 	    (caddr_t)ch_patterns, sizeof(ch_patterns) / sizeof(ch_patterns[0]),
    151 	    sizeof(ch_patterns[0]), &priority);
    152 
    153 	return (priority);
    154 }
    155 
    156 void
    157 chattach(parent, self, aux)
    158 	struct device *parent, *self;
    159 	void *aux;
    160 {
    161 	struct ch_softc *sc = (struct ch_softc *)self;
    162 	struct scsipibus_attach_args *sa = aux;
    163 	struct scsipi_link *link = sa->sa_sc_link;
    164 
    165 	/* Glue into the SCSI bus */
    166 	sc->sc_link = link;
    167 	link->device = &ch_switch;
    168 	link->device_softc = sc;
    169 	link->openings = 1;
    170 
    171 	printf("\n");
    172 
    173 	/*
    174 	 * Find out our device's quirks.
    175 	 */
    176 	ch_get_quirks(sc, &sa->sa_inqbuf);
    177 
    178 	/*
    179 	 * Some changers require a long time to settle out, to do
    180 	 * tape inventory, for instance.
    181 	 */
    182 	if (sc->sc_settledelay) {
    183 		printf("%s: waiting %d seconds for changer to settle...\n",
    184 		    sc->sc_dev.dv_xname, sc->sc_settledelay);
    185 		delay(1000000 * sc->sc_settledelay);
    186 	}
    187 
    188 	/*
    189 	 * Get information about the device.  Note we can't use
    190 	 * interrupts yet.
    191 	 */
    192 	if (ch_get_params(sc, SCSI_AUTOCONF))
    193 		printf("%s: offline\n", sc->sc_dev.dv_xname);
    194 	else {
    195 #define PLURAL(c)	(c) == 1 ? "" : "s"
    196 		printf("%s: %d slot%s, %d drive%s, %d picker%s, %d portal%s\n",
    197 		    sc->sc_dev.dv_xname,
    198 		    sc->sc_counts[CHET_ST], PLURAL(sc->sc_counts[CHET_ST]),
    199 		    sc->sc_counts[CHET_DT], PLURAL(sc->sc_counts[CHET_DT]),
    200 		    sc->sc_counts[CHET_MT], PLURAL(sc->sc_counts[CHET_MT]),
    201 		    sc->sc_counts[CHET_IE], PLURAL(sc->sc_counts[CHET_IE]));
    202 #undef PLURAL
    203 #ifdef CHANGER_DEBUG
    204 		printf("%s: move mask: 0x%x 0x%x 0x%x 0x%x\n",
    205 		    sc->sc_dev.dv_xname,
    206 		    sc->sc_movemask[CHET_MT], sc->sc_movemask[CHET_ST],
    207 		    sc->sc_movemask[CHET_IE], sc->sc_movemask[CHET_DT]);
    208 		printf("%s: exchange mask: 0x%x 0x%x 0x%x 0x%x\n",
    209 		    sc->sc_dev.dv_xname,
    210 		    sc->sc_exchangemask[CHET_MT], sc->sc_exchangemask[CHET_ST],
    211 		    sc->sc_exchangemask[CHET_IE], sc->sc_exchangemask[CHET_DT]);
    212 #endif /* CHANGER_DEBUG */
    213 	}
    214 
    215 	/* Default the current picker. */
    216 	sc->sc_picker = sc->sc_firsts[CHET_MT];
    217 }
    218 
    219 int
    220 chopen(dev, flags, fmt, p)
    221 	dev_t dev;
    222 	int flags, fmt;
    223 	struct proc *p;
    224 {
    225 	struct ch_softc *sc;
    226 	int unit, error = 0;
    227 
    228 	unit = CHUNIT(dev);
    229 	if ((unit >= ch_cd.cd_ndevs) ||
    230 	    ((sc = ch_cd.cd_devs[unit]) == NULL))
    231 		return (ENXIO);
    232 
    233 	/*
    234 	 * Only allow one open at a time.
    235 	 */
    236 	if (sc->sc_link->flags & SDEV_OPEN)
    237 		return (EBUSY);
    238 
    239 	sc->sc_link->flags |= SDEV_OPEN;
    240 
    241 	/*
    242 	 * Absorb any unit attention errors.  Ignore "not ready"
    243 	 * since this might occur if e.g. a tape isn't actually
    244 	 * loaded in the drive.
    245 	 */
    246 	error = scsipi_test_unit_ready(sc->sc_link,
    247 	    SCSI_IGNORE_NOT_READY|SCSI_IGNORE_MEDIA_CHANGE);
    248 	if (error)
    249 		goto bad;
    250 
    251 	/*
    252 	 * Make sure our parameters are up to date.
    253 	 */
    254 	if ((error = ch_get_params(sc, 0)) != 0)
    255 		goto bad;
    256 
    257 	return (0);
    258 
    259  bad:
    260 	sc->sc_link->flags &= ~SDEV_OPEN;
    261 	return (error);
    262 }
    263 
    264 int
    265 chclose(dev, flags, fmt, p)
    266 	dev_t dev;
    267 	int flags, fmt;
    268 	struct proc *p;
    269 {
    270 	struct ch_softc *sc = ch_cd.cd_devs[CHUNIT(dev)];
    271 
    272 	sc->sc_link->flags &= ~SDEV_OPEN;
    273 	return (0);
    274 }
    275 
    276 int
    277 chioctl(dev, cmd, data, flags, p)
    278 	dev_t dev;
    279 	u_long cmd;
    280 	caddr_t data;
    281 	int flags;
    282 	struct proc *p;
    283 {
    284 	struct ch_softc *sc = ch_cd.cd_devs[CHUNIT(dev)];
    285 	int error = 0;
    286 
    287 	/*
    288 	 * If this command can change the device's state, we must
    289 	 * have the device open for writing.
    290 	 */
    291 	switch (cmd) {
    292 	case CHIOGPICKER:
    293 	case CHIOGPARAMS:
    294 	case CHIOGSTATUS:
    295 		break;
    296 
    297 	default:
    298 		if ((flags & FWRITE) == 0)
    299 			return (EBADF);
    300 	}
    301 
    302 	switch (cmd) {
    303 	case CHIOMOVE:
    304 		error = ch_move(sc, (struct changer_move *)data);
    305 		break;
    306 
    307 	case CHIOEXCHANGE:
    308 		error = ch_exchange(sc, (struct changer_exchange *)data);
    309 		break;
    310 
    311 	case CHIOPOSITION:
    312 		error = ch_position(sc, (struct changer_position *)data);
    313 		break;
    314 
    315 	case CHIOGPICKER:
    316 		*(int *)data = sc->sc_picker - sc->sc_firsts[CHET_MT];
    317 		break;
    318 
    319 	case CHIOSPICKER:	{
    320 		int new_picker = *(int *)data;
    321 
    322 		if (new_picker > (sc->sc_counts[CHET_MT] - 1))
    323 			return (EINVAL);
    324 		sc->sc_picker = sc->sc_firsts[CHET_MT] + new_picker;
    325 		break;		}
    326 
    327 	case CHIOGPARAMS:	{
    328 		struct changer_params *cp = (struct changer_params *)data;
    329 
    330 		cp->cp_curpicker = sc->sc_picker - sc->sc_firsts[CHET_MT];
    331 		cp->cp_npickers = sc->sc_counts[CHET_MT];
    332 		cp->cp_nslots = sc->sc_counts[CHET_ST];
    333 		cp->cp_nportals = sc->sc_counts[CHET_IE];
    334 		cp->cp_ndrives = sc->sc_counts[CHET_DT];
    335 		break;		}
    336 
    337 	case CHIOIELEM:
    338 		error = ch_ielem(sc);
    339 		break;
    340 
    341 	case CHIOGSTATUS:	{
    342 		struct changer_element_status *ces =
    343 		    (struct changer_element_status *)data;
    344 
    345 		error = ch_usergetelemstatus(sc, ces->ces_type, ces->ces_data);
    346 		break;		}
    347 
    348 	/* Implement prevent/allow? */
    349 
    350 	default:
    351 		error = scsipi_do_ioctl(sc->sc_link, dev, cmd, data, flags, p);
    352 		break;
    353 	}
    354 
    355 	return (error);
    356 }
    357 
    358 int
    359 ch_move(sc, cm)
    360 	struct ch_softc *sc;
    361 	struct changer_move *cm;
    362 {
    363 	struct scsi_move_medium cmd;
    364 	u_int16_t fromelem, toelem;
    365 
    366 	/*
    367 	 * Check arguments.
    368 	 */
    369 	if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
    370 		return (EINVAL);
    371 	if ((cm->cm_fromunit > (sc->sc_counts[cm->cm_fromtype] - 1)) ||
    372 	    (cm->cm_tounit > (sc->sc_counts[cm->cm_totype] - 1)))
    373 		return (ENODEV);
    374 
    375 	/*
    376 	 * Check the request against the changer's capabilities.
    377 	 */
    378 	if ((sc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
    379 		return (EINVAL);
    380 
    381 	/*
    382 	 * Calculate the source and destination elements.
    383 	 */
    384 	fromelem = sc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
    385 	toelem = sc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
    386 
    387 	/*
    388 	 * Build the SCSI command.
    389 	 */
    390 	bzero(&cmd, sizeof(cmd));
    391 	cmd.opcode = MOVE_MEDIUM;
    392 	_lto2b(sc->sc_picker, cmd.tea);
    393 	_lto2b(fromelem, cmd.src);
    394 	_lto2b(toelem, cmd.dst);
    395 	if (cm->cm_flags & CM_INVERT)
    396 		cmd.flags |= MOVE_MEDIUM_INVERT;
    397 
    398 	/*
    399 	 * Send command to changer.
    400 	 */
    401 	return (scsipi_command(sc->sc_link,
    402 	    (struct scsipi_generic *)&cmd, sizeof(cmd), NULL, 0, CHRETRIES,
    403 	    100000, NULL, 0));
    404 }
    405 
    406 int
    407 ch_exchange(sc, ce)
    408 	struct ch_softc *sc;
    409 	struct changer_exchange *ce;
    410 {
    411 	struct scsi_exchange_medium cmd;
    412 	u_int16_t src, dst1, dst2;
    413 
    414 	/*
    415 	 * Check arguments.
    416 	 */
    417 	if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
    418 	    (ce->ce_sdsttype > CHET_DT))
    419 		return (EINVAL);
    420 	if ((ce->ce_srcunit > (sc->sc_counts[ce->ce_srctype] - 1)) ||
    421 	    (ce->ce_fdstunit > (sc->sc_counts[ce->ce_fdsttype] - 1)) ||
    422 	    (ce->ce_sdstunit > (sc->sc_counts[ce->ce_sdsttype] - 1)))
    423 		return (ENODEV);
    424 
    425 	/*
    426 	 * Check the request against the changer's capabilities.
    427 	 */
    428 	if (((sc->sc_exchangemask[ce->ce_srctype] &
    429 	     (1 << ce->ce_fdsttype)) == 0) ||
    430 	    ((sc->sc_exchangemask[ce->ce_fdsttype] &
    431 	     (1 << ce->ce_sdsttype)) == 0))
    432 		return (EINVAL);
    433 
    434 	/*
    435 	 * Calculate the source and destination elements.
    436 	 */
    437 	src = sc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
    438 	dst1 = sc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
    439 	dst2 = sc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
    440 
    441 	/*
    442 	 * Build the SCSI command.
    443 	 */
    444 	bzero(&cmd, sizeof(cmd));
    445 	cmd.opcode = EXCHANGE_MEDIUM;
    446 	_lto2b(sc->sc_picker, cmd.tea);
    447 	_lto2b(src, cmd.src);
    448 	_lto2b(dst1, cmd.fdst);
    449 	_lto2b(dst2, cmd.sdst);
    450 	if (ce->ce_flags & CE_INVERT1)
    451 		cmd.flags |= EXCHANGE_MEDIUM_INV1;
    452 	if (ce->ce_flags & CE_INVERT2)
    453 		cmd.flags |= EXCHANGE_MEDIUM_INV2;
    454 
    455 	/*
    456 	 * Send command to changer.
    457 	 */
    458 	return (scsipi_command(sc->sc_link,
    459 	    (struct scsipi_generic *)&cmd, sizeof(cmd), NULL, 0, CHRETRIES,
    460 	    100000, NULL, 0));
    461 }
    462 
    463 int
    464 ch_position(sc, cp)
    465 	struct ch_softc *sc;
    466 	struct changer_position *cp;
    467 {
    468 	struct scsi_position_to_element cmd;
    469 	u_int16_t dst;
    470 
    471 	/*
    472 	 * Check arguments.
    473 	 */
    474 	if (cp->cp_type > CHET_DT)
    475 		return (EINVAL);
    476 	if (cp->cp_unit > (sc->sc_counts[cp->cp_type] - 1))
    477 		return (ENODEV);
    478 
    479 	/*
    480 	 * Calculate the destination element.
    481 	 */
    482 	dst = sc->sc_firsts[cp->cp_type] + cp->cp_unit;
    483 
    484 	/*
    485 	 * Build the SCSI command.
    486 	 */
    487 	bzero(&cmd, sizeof(cmd));
    488 	cmd.opcode = POSITION_TO_ELEMENT;
    489 	_lto2b(sc->sc_picker, cmd.tea);
    490 	_lto2b(dst, cmd.dst);
    491 	if (cp->cp_flags & CP_INVERT)
    492 		cmd.flags |= POSITION_TO_ELEMENT_INVERT;
    493 
    494 	/*
    495 	 * Send command to changer.
    496 	 */
    497 	return (scsipi_command(sc->sc_link,
    498 	    (struct scsipi_generic *)&cmd, sizeof(cmd), NULL, 0, CHRETRIES,
    499 	    100000, NULL, 0));
    500 }
    501 
    502 /*
    503  * Perform a READ ELEMENT STATUS on behalf of the user, and return to
    504  * the user only the data the user is interested in (i.e. an array of
    505  * flags bytes).
    506  */
    507 int
    508 ch_usergetelemstatus(sc, chet, uptr)
    509 	struct ch_softc *sc;
    510 	int chet;
    511 	u_int8_t *uptr;
    512 {
    513 	struct read_element_status_header *st_hdr;
    514 	struct read_element_status_page_header *pg_hdr;
    515 	struct read_element_status_descriptor *desc;
    516 	caddr_t data = NULL;
    517 	size_t size, desclen;
    518 	int avail, i, error = 0;
    519 	u_int8_t *user_data = NULL;
    520 
    521 	/*
    522 	 * If there are no elements of the requested type in the changer,
    523 	 * the request is invalid.
    524 	 */
    525 	if (sc->sc_counts[chet] == 0)
    526 		return (EINVAL);
    527 
    528 	/*
    529 	 * Request one descriptor for the given element type.  This
    530 	 * is used to determine the size of the descriptor so that
    531 	 * we can allocate enough storage for all of them.  We assume
    532 	 * that the first one can fit into 1k.
    533 	 */
    534 	data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK);
    535 	error = ch_getelemstatus(sc, sc->sc_firsts[chet], 1, data, 1024);
    536 	if (error)
    537 		goto done;
    538 
    539 	st_hdr = (struct read_element_status_header *)data;
    540 	pg_hdr = (struct read_element_status_page_header *)((u_long)st_hdr +
    541 	    sizeof(struct read_element_status_header));
    542 	desclen = _2btol(pg_hdr->edl);
    543 
    544 	size = sizeof(struct read_element_status_header) +
    545 	    sizeof(struct read_element_status_page_header) +
    546 	    (desclen * sc->sc_counts[chet]);
    547 
    548 	/*
    549 	 * Reallocate storage for descriptors and get them from the
    550 	 * device.
    551 	 */
    552 	free(data, M_DEVBUF);
    553 	data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK);
    554 	error = ch_getelemstatus(sc, sc->sc_firsts[chet],
    555 	    sc->sc_counts[chet], data, size);
    556 	if (error)
    557 		goto done;
    558 
    559 	/*
    560 	 * Fill in the user status array.
    561 	 */
    562 	st_hdr = (struct read_element_status_header *)data;
    563 	avail = _2btol(st_hdr->count);
    564 
    565 	if (avail != sc->sc_counts[chet])
    566 		printf("%s: warning, READ ELEMENT STATUS avail != count\n",
    567 		    sc->sc_dev.dv_xname);
    568 
    569 	user_data = (u_int8_t *)malloc(avail, M_DEVBUF, M_WAITOK);
    570 
    571 	desc = (struct read_element_status_descriptor *)((u_long)data +
    572 	    sizeof(struct read_element_status_header) +
    573 	    sizeof(struct read_element_status_page_header));
    574 	for (i = 0; i < avail; ++i) {
    575 		user_data[i] = desc->flags1;
    576 		(u_long)desc += desclen;
    577 	}
    578 
    579 	/* Copy flags array out to userspace. */
    580 	error = copyout(user_data, uptr, avail);
    581 
    582  done:
    583 	if (data != NULL)
    584 		free(data, M_DEVBUF);
    585 	if (user_data != NULL)
    586 		free(user_data, M_DEVBUF);
    587 	return (error);
    588 }
    589 
    590 int
    591 ch_getelemstatus(sc, first, count, data, datalen)
    592 	struct ch_softc *sc;
    593 	int first, count;
    594 	caddr_t data;
    595 	size_t datalen;
    596 {
    597 	struct scsi_read_element_status cmd;
    598 
    599 	/*
    600 	 * Build SCSI command.
    601 	 */
    602 	bzero(&cmd, sizeof(cmd));
    603 	cmd.opcode = READ_ELEMENT_STATUS;
    604 	_lto2b(first, cmd.sea);
    605 	_lto2b(count, cmd.count);
    606 	_lto3b(datalen, cmd.len);
    607 
    608 	/*
    609 	 * Send command to changer.
    610 	 */
    611 	return (scsipi_command(sc->sc_link,
    612 	    (struct scsipi_generic *)&cmd, sizeof(cmd),
    613 	    (u_char *)data, datalen, CHRETRIES, 100000, NULL, SCSI_DATA_IN));
    614 }
    615 
    616 
    617 int
    618 ch_ielem(sc)
    619 	struct ch_softc *sc;
    620 {
    621 	int tmo;
    622 	struct scsi_initialize_element_status cmd;
    623 
    624 	/*
    625 	 * Build SCSI command.
    626 	 */
    627 	bzero(&cmd, sizeof(cmd));
    628 	cmd.opcode = INITIALIZE_ELEMENT_STATUS;
    629 
    630 	/*
    631 	 * Send command to changer.
    632 	 *
    633 	 * The problem is, how long to allow for the command?
    634 	 * It can take a *really* long time, and also depends
    635 	 * on unknowable factors such as whether there are
    636 	 * *almost* readable labels on tapes that a barcode
    637 	 * reader is trying to decipher.
    638 	 *
    639 	 * I'm going to make this long enough to allow 5 minutes
    640 	 * per element plus an initial 10 minute wait.
    641 	 */
    642 	tmo =	sc->sc_counts[CHET_MT] +
    643 		sc->sc_counts[CHET_ST] +
    644 		sc->sc_counts[CHET_IE] +
    645 		sc->sc_counts[CHET_DT];
    646 	tmo *= 5 * 1000;
    647 	tmo += (10 * 60 * 1000);
    648 
    649 	return (scsipi_command(sc->sc_link,
    650 	    (struct scsipi_generic *)&cmd, sizeof(cmd),
    651 	    NULL, 0, CHRETRIES, tmo, NULL, 0));
    652 }
    653 
    654 /*
    655  * Ask the device about itself and fill in the parameters in our
    656  * softc.
    657  */
    658 int
    659 ch_get_params(sc, scsiflags)
    660 	struct ch_softc *sc;
    661 	int scsiflags;
    662 {
    663 	struct scsi_mode_sense cmd;
    664 	struct scsi_mode_sense_data {
    665 		struct scsi_mode_header header;
    666 		union {
    667 			struct page_element_address_assignment ea;
    668 			struct page_transport_geometry_parameters tg;
    669 			struct page_device_capabilities cap;
    670 		} pages;
    671 	} sense_data;
    672 	int error, from;
    673 	u_int8_t *moves, *exchanges;
    674 
    675 	/*
    676 	 * Grab info from the element address assignment page.
    677 	 */
    678 	bzero(&cmd, sizeof(cmd));
    679 	bzero(&sense_data, sizeof(sense_data));
    680 	cmd.opcode = SCSI_MODE_SENSE;
    681 	cmd.byte2 |= 0x08;	/* disable block descriptors */
    682 	cmd.page = 0x1d;
    683 	cmd.length = (sizeof(sense_data) & 0xff);
    684 	error = scsipi_command(sc->sc_link,
    685 	    (struct scsipi_generic *)&cmd, sizeof(cmd), (u_char *)&sense_data,
    686 	    sizeof(sense_data), CHRETRIES, 6000, NULL,
    687 	    scsiflags | SCSI_DATA_IN);
    688 	if (error) {
    689 		printf("%s: could not sense element address page\n",
    690 		    sc->sc_dev.dv_xname);
    691 		return (error);
    692 	}
    693 
    694 	sc->sc_firsts[CHET_MT] = _2btol(sense_data.pages.ea.mtea);
    695 	sc->sc_counts[CHET_MT] = _2btol(sense_data.pages.ea.nmte);
    696 	sc->sc_firsts[CHET_ST] = _2btol(sense_data.pages.ea.fsea);
    697 	sc->sc_counts[CHET_ST] = _2btol(sense_data.pages.ea.nse);
    698 	sc->sc_firsts[CHET_IE] = _2btol(sense_data.pages.ea.fieea);
    699 	sc->sc_counts[CHET_IE] = _2btol(sense_data.pages.ea.niee);
    700 	sc->sc_firsts[CHET_DT] = _2btol(sense_data.pages.ea.fdtea);
    701 	sc->sc_counts[CHET_DT] = _2btol(sense_data.pages.ea.ndte);
    702 
    703 	/* XXX ask for page trasport geom */
    704 
    705 	/*
    706 	 * Grab info from the capabilities page.
    707 	 */
    708 	bzero(&cmd, sizeof(cmd));
    709 	bzero(&sense_data, sizeof(sense_data));
    710 	cmd.opcode = SCSI_MODE_SENSE;
    711 	/*
    712 	 * XXX: Note: not all changers can deal with disabled block descriptors
    713 	 */
    714 	cmd.byte2 = 0x08;	/* disable block descriptors */
    715 	cmd.page = 0x1f;
    716 	cmd.length = (sizeof(sense_data) & 0xff);
    717 	error = scsipi_command(sc->sc_link,
    718 	    (struct scsipi_generic *)&cmd, sizeof(cmd), (u_char *)&sense_data,
    719 	    sizeof(sense_data), CHRETRIES, 6000, NULL,
    720 	    scsiflags | SCSI_DATA_IN);
    721 	if (error) {
    722 		printf("%s: could not sense capabilities page\n",
    723 		    sc->sc_dev.dv_xname);
    724 		return (error);
    725 	}
    726 
    727 	bzero(sc->sc_movemask, sizeof(sc->sc_movemask));
    728 	bzero(sc->sc_exchangemask, sizeof(sc->sc_exchangemask));
    729 	moves = &sense_data.pages.cap.move_from_mt;
    730 	exchanges = &sense_data.pages.cap.exchange_with_mt;
    731 	for (from = CHET_MT; from <= CHET_DT; ++from) {
    732 		sc->sc_movemask[from] = moves[from];
    733 		sc->sc_exchangemask[from] = exchanges[from];
    734 	}
    735 
    736 	sc->sc_link->flags |= SDEV_MEDIA_LOADED;
    737 	return (0);
    738 }
    739 
    740 void
    741 ch_get_quirks(sc, inqbuf)
    742 	struct ch_softc *sc;
    743 	struct scsipi_inquiry_pattern *inqbuf;
    744 {
    745 	struct chquirk *match;
    746 	int priority;
    747 
    748 	sc->sc_settledelay = 0;
    749 
    750 	match = (struct chquirk *)scsipi_inqmatch(inqbuf,
    751 	    (caddr_t)chquirks,
    752 	    sizeof(chquirks) / sizeof(chquirks[0]),
    753 	    sizeof(chquirks[0]), &priority);
    754 	if (priority != 0)
    755 		sc->sc_settledelay = match->cq_settledelay;
    756 }
    757