Home | History | Annotate | Line # | Download | only in scsipi
st_scsi.c revision 1.2.2.5
      1  1.2.2.5  nathanw /*	$NetBSD: st_scsi.c,v 1.2.2.5 2002/01/08 00:31:58 nathanw Exp $ */
      2  1.2.2.2  nathanw 
      3  1.2.2.2  nathanw /*-
      4  1.2.2.2  nathanw  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.2.2.2  nathanw  * All rights reserved.
      6  1.2.2.2  nathanw  *
      7  1.2.2.2  nathanw  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2.2.2  nathanw  * by Charles M. Hannum.
      9  1.2.2.2  nathanw  *
     10  1.2.2.2  nathanw  * Redistribution and use in source and binary forms, with or without
     11  1.2.2.2  nathanw  * modification, are permitted provided that the following conditions
     12  1.2.2.2  nathanw  * are met:
     13  1.2.2.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     14  1.2.2.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     15  1.2.2.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2.2.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     17  1.2.2.2  nathanw  *    documentation and/or other materials provided with the distribution.
     18  1.2.2.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     19  1.2.2.2  nathanw  *    must display the following acknowledgement:
     20  1.2.2.2  nathanw  *        This product includes software developed by the NetBSD
     21  1.2.2.2  nathanw  *        Foundation, Inc. and its contributors.
     22  1.2.2.2  nathanw  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.2.2.2  nathanw  *    contributors may be used to endorse or promote products derived
     24  1.2.2.2  nathanw  *    from this software without specific prior written permission.
     25  1.2.2.2  nathanw  *
     26  1.2.2.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.2.2.2  nathanw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.2.2.2  nathanw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.2.2.2  nathanw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.2.2.2  nathanw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.2.2.2  nathanw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.2.2.2  nathanw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.2.2.2  nathanw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.2.2.2  nathanw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.2.2.2  nathanw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.2.2.2  nathanw  * POSSIBILITY OF SUCH DAMAGE.
     37  1.2.2.2  nathanw  */
     38  1.2.2.2  nathanw 
     39  1.2.2.2  nathanw /*
     40  1.2.2.2  nathanw  * Originally written by Julian Elischer (julian (at) tfs.com)
     41  1.2.2.2  nathanw  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     42  1.2.2.2  nathanw  *
     43  1.2.2.2  nathanw  * TRW Financial Systems, in accordance with their agreement with Carnegie
     44  1.2.2.2  nathanw  * Mellon University, makes this software available to CMU to distribute
     45  1.2.2.2  nathanw  * or use in any manner that they see fit as long as this message is kept with
     46  1.2.2.2  nathanw  * the software. For this reason TFS also grants any other persons or
     47  1.2.2.2  nathanw  * organisations permission to use or modify this software.
     48  1.2.2.2  nathanw  *
     49  1.2.2.2  nathanw  * TFS supplies this software to be publicly redistributed
     50  1.2.2.2  nathanw  * on the understanding that TFS is not responsible for the correct
     51  1.2.2.2  nathanw  * functioning of this software in any circumstances.
     52  1.2.2.2  nathanw  *
     53  1.2.2.2  nathanw  * Ported to run under 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
     54  1.2.2.2  nathanw  * major changes by Julian Elischer (julian (at) jules.dialix.oz.au) May 1993
     55  1.2.2.2  nathanw  *
     56  1.2.2.2  nathanw  * A lot of rewhacking done by mjacob (mjacob (at) nas.nasa.gov).
     57  1.2.2.2  nathanw  */
     58  1.2.2.2  nathanw 
     59  1.2.2.4  nathanw #include <sys/cdefs.h>
     60  1.2.2.5  nathanw __KERNEL_RCSID(0, "$NetBSD: st_scsi.c,v 1.2.2.5 2002/01/08 00:31:58 nathanw Exp $");
     61  1.2.2.4  nathanw 
     62  1.2.2.2  nathanw #include "opt_scsi.h"
     63  1.2.2.2  nathanw #include "rnd.h"
     64  1.2.2.2  nathanw 
     65  1.2.2.2  nathanw #include <sys/param.h>
     66  1.2.2.2  nathanw #include <sys/device.h>
     67  1.2.2.2  nathanw #include <sys/buf.h>
     68  1.2.2.2  nathanw #include <sys/conf.h>
     69  1.2.2.2  nathanw #include <sys/kernel.h>
     70  1.2.2.2  nathanw #include <sys/systm.h>
     71  1.2.2.2  nathanw 
     72  1.2.2.2  nathanw #include <dev/scsipi/stvar.h>
     73  1.2.2.2  nathanw #include <dev/scsipi/scsi_tape.h>
     74  1.2.2.2  nathanw #include <dev/scsipi/scsi_all.h>
     75  1.2.2.2  nathanw 
     76  1.2.2.2  nathanw int	st_scsibus_match __P((struct device *, struct cfdata *, void *));
     77  1.2.2.2  nathanw void	st_scsibus_attach __P((struct device *, struct device *, void *));
     78  1.2.2.2  nathanw int	st_scsibus_ops __P((struct st_softc *, int, int));
     79  1.2.2.2  nathanw int	st_scsibus_read_block_limits __P((struct st_softc *, int));
     80  1.2.2.2  nathanw int	st_scsibus_mode_sense __P((struct st_softc *, int));
     81  1.2.2.2  nathanw int	st_scsibus_mode_select __P((struct st_softc *, int));
     82  1.2.2.2  nathanw int	st_scsibus_cmprss __P((struct st_softc *, int, int));
     83  1.2.2.2  nathanw 
     84  1.2.2.2  nathanw struct cfattach st_scsibus_ca = {
     85  1.2.2.5  nathanw 	sizeof(struct st_softc), st_scsibus_match, st_scsibus_attach,
     86  1.2.2.5  nathanw 	stdetach, stactivate
     87  1.2.2.2  nathanw };
     88  1.2.2.2  nathanw 
     89  1.2.2.2  nathanw const struct scsipi_inquiry_pattern st_scsibus_patterns[] = {
     90  1.2.2.2  nathanw 	{T_SEQUENTIAL, T_REMOV,
     91  1.2.2.2  nathanw 	 "",         "",                 ""},
     92  1.2.2.2  nathanw };
     93  1.2.2.2  nathanw 
     94  1.2.2.2  nathanw int
     95  1.2.2.2  nathanw st_scsibus_match(parent, match, aux)
     96  1.2.2.2  nathanw 	struct device *parent;
     97  1.2.2.2  nathanw 	struct cfdata *match;
     98  1.2.2.2  nathanw 	void *aux;
     99  1.2.2.2  nathanw {
    100  1.2.2.2  nathanw 	struct scsipibus_attach_args *sa = aux;
    101  1.2.2.2  nathanw 	int priority;
    102  1.2.2.2  nathanw 
    103  1.2.2.2  nathanw 	if (scsipi_periph_bustype(sa->sa_periph) != SCSIPI_BUSTYPE_SCSI)
    104  1.2.2.2  nathanw 		return (0);
    105  1.2.2.2  nathanw 
    106  1.2.2.2  nathanw 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
    107  1.2.2.2  nathanw 	    (caddr_t)st_scsibus_patterns,
    108  1.2.2.2  nathanw 	    sizeof(st_scsibus_patterns)/sizeof(st_scsibus_patterns[0]),
    109  1.2.2.2  nathanw 	    sizeof(st_scsibus_patterns[0]), &priority);
    110  1.2.2.2  nathanw 	return (priority);
    111  1.2.2.2  nathanw }
    112  1.2.2.2  nathanw 
    113  1.2.2.2  nathanw void
    114  1.2.2.2  nathanw st_scsibus_attach(parent, self, aux)
    115  1.2.2.2  nathanw 	struct device *parent, *self;
    116  1.2.2.2  nathanw 	void *aux;
    117  1.2.2.2  nathanw {
    118  1.2.2.2  nathanw 	struct st_softc *st = (void *)self;
    119  1.2.2.2  nathanw 
    120  1.2.2.2  nathanw 	st->ops = st_scsibus_ops;
    121  1.2.2.2  nathanw 	stattach(parent, st, aux);
    122  1.2.2.2  nathanw }
    123  1.2.2.2  nathanw 
    124  1.2.2.2  nathanw int
    125  1.2.2.2  nathanw st_scsibus_ops(st, op, flags)
    126  1.2.2.2  nathanw 	struct st_softc *st;
    127  1.2.2.2  nathanw 	int op;
    128  1.2.2.2  nathanw 	int flags;
    129  1.2.2.2  nathanw {
    130  1.2.2.2  nathanw 	switch(op) {
    131  1.2.2.2  nathanw 	case ST_OPS_RBL:
    132  1.2.2.2  nathanw 		return st_scsibus_read_block_limits(st, flags);
    133  1.2.2.2  nathanw 	case ST_OPS_MODESENSE:
    134  1.2.2.2  nathanw 		return st_scsibus_mode_sense(st, flags);
    135  1.2.2.2  nathanw 	case ST_OPS_MODESELECT:
    136  1.2.2.2  nathanw 		return st_scsibus_mode_select(st, flags);
    137  1.2.2.2  nathanw 	case ST_OPS_CMPRSS_ON:
    138  1.2.2.2  nathanw 	case ST_OPS_CMPRSS_OFF:
    139  1.2.2.2  nathanw 		return st_scsibus_cmprss(st, flags,
    140  1.2.2.2  nathanw 		    (op == ST_OPS_CMPRSS_ON) ? 1 : 0);
    141  1.2.2.2  nathanw 	default:
    142  1.2.2.2  nathanw 		panic("st_scsibus_ops: invalid op");
    143  1.2.2.2  nathanw 		return 0; /* XXX to appease gcc */
    144  1.2.2.2  nathanw 	}
    145  1.2.2.2  nathanw 	/* NOTREACHED */
    146  1.2.2.2  nathanw }
    147  1.2.2.2  nathanw 
    148  1.2.2.2  nathanw /*
    149  1.2.2.2  nathanw  * Ask the drive what it's min and max blk sizes are.
    150  1.2.2.2  nathanw  */
    151  1.2.2.2  nathanw int
    152  1.2.2.2  nathanw st_scsibus_read_block_limits(st, flags)
    153  1.2.2.2  nathanw 	struct st_softc *st;
    154  1.2.2.2  nathanw 	int flags;
    155  1.2.2.2  nathanw {
    156  1.2.2.2  nathanw 	struct scsi_block_limits cmd;
    157  1.2.2.2  nathanw 	struct scsi_block_limits_data block_limits;
    158  1.2.2.2  nathanw 	struct scsipi_periph *periph = st->sc_periph;
    159  1.2.2.2  nathanw 	int error;
    160  1.2.2.2  nathanw 
    161  1.2.2.2  nathanw 	/*
    162  1.2.2.2  nathanw 	 * do a 'Read Block Limits'
    163  1.2.2.2  nathanw 	 */
    164  1.2.2.3  nathanw 	memset(&cmd, 0, sizeof(cmd));
    165  1.2.2.2  nathanw 	cmd.opcode = READ_BLOCK_LIMITS;
    166  1.2.2.2  nathanw 
    167  1.2.2.2  nathanw 	/*
    168  1.2.2.2  nathanw 	 * do the command, update the global values
    169  1.2.2.2  nathanw 	 */
    170  1.2.2.2  nathanw 	error = scsipi_command(periph, (struct scsipi_generic *)&cmd,
    171  1.2.2.2  nathanw 	    sizeof(cmd), (u_char *)&block_limits, sizeof(block_limits),
    172  1.2.2.2  nathanw 	    ST_RETRIES, ST_CTL_TIME, NULL,
    173  1.2.2.2  nathanw 	    flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
    174  1.2.2.2  nathanw 	if (error)
    175  1.2.2.2  nathanw 		return (error);
    176  1.2.2.2  nathanw 
    177  1.2.2.2  nathanw 	st->blkmin = _2btol(block_limits.min_length);
    178  1.2.2.2  nathanw 	st->blkmax = _3btol(block_limits.max_length);
    179  1.2.2.2  nathanw 
    180  1.2.2.2  nathanw 	SC_DEBUG(periph, SCSIPI_DB3,
    181  1.2.2.2  nathanw 	    ("(%d <= blksize <= %d)\n", st->blkmin, st->blkmax));
    182  1.2.2.2  nathanw 	return (0);
    183  1.2.2.2  nathanw }
    184  1.2.2.2  nathanw 
    185  1.2.2.2  nathanw /*
    186  1.2.2.2  nathanw  * Get the scsi driver to send a full inquiry to the
    187  1.2.2.2  nathanw  * device and use the results to fill out the global
    188  1.2.2.2  nathanw  * parameter structure.
    189  1.2.2.2  nathanw  *
    190  1.2.2.2  nathanw  * called from:
    191  1.2.2.2  nathanw  * attach
    192  1.2.2.2  nathanw  * open
    193  1.2.2.2  nathanw  * ioctl (to reset original blksize)
    194  1.2.2.2  nathanw  */
    195  1.2.2.2  nathanw int
    196  1.2.2.2  nathanw st_scsibus_mode_sense(st, flags)
    197  1.2.2.2  nathanw 	struct st_softc *st;
    198  1.2.2.2  nathanw 	int flags;
    199  1.2.2.2  nathanw {
    200  1.2.2.2  nathanw 	u_int scsipi_sense_len;
    201  1.2.2.2  nathanw 	int error;
    202  1.2.2.2  nathanw 	struct scsipi_sense {
    203  1.2.2.2  nathanw 		struct scsipi_mode_header header;
    204  1.2.2.2  nathanw 		struct scsi_blk_desc blk_desc;
    205  1.2.2.2  nathanw 		u_char sense_data[MAX_PAGE_0_SIZE];
    206  1.2.2.2  nathanw 	} scsipi_sense;
    207  1.2.2.2  nathanw 	struct scsipi_periph *periph = st->sc_periph;
    208  1.2.2.2  nathanw 
    209  1.2.2.2  nathanw 	scsipi_sense_len = 12 + st->page_0_size;
    210  1.2.2.2  nathanw 
    211  1.2.2.2  nathanw 	/*
    212  1.2.2.2  nathanw 	 * Set up a mode sense
    213  1.2.2.2  nathanw 	 * We don't need the results. Just print them for our interest's sake,
    214  1.2.2.2  nathanw 	 * if asked, or if we need it as a template for the mode select store
    215  1.2.2.2  nathanw 	 * it away.
    216  1.2.2.2  nathanw 	 */
    217  1.2.2.2  nathanw 	error = scsipi_mode_sense(st->sc_periph, 0, SMS_PAGE_CTRL_CURRENT,
    218  1.2.2.2  nathanw 	    &scsipi_sense.header, scsipi_sense_len, flags | XS_CTL_DATA_ONSTACK,
    219  1.2.2.2  nathanw 	    ST_RETRIES, ST_CTL_TIME);
    220  1.2.2.2  nathanw 	if (error)
    221  1.2.2.2  nathanw 		return (error);
    222  1.2.2.2  nathanw 
    223  1.2.2.2  nathanw 	st->numblks = _3btol(scsipi_sense.blk_desc.nblocks);
    224  1.2.2.2  nathanw 	st->media_blksize = _3btol(scsipi_sense.blk_desc.blklen);
    225  1.2.2.2  nathanw 	st->media_density = scsipi_sense.blk_desc.density;
    226  1.2.2.2  nathanw 	if (scsipi_sense.header.dev_spec & SMH_DSP_WRITE_PROT)
    227  1.2.2.2  nathanw 		st->flags |= ST_READONLY;
    228  1.2.2.2  nathanw 	else
    229  1.2.2.2  nathanw 		st->flags &= ~ST_READONLY;
    230  1.2.2.2  nathanw 	SC_DEBUG(periph, SCSIPI_DB3,
    231  1.2.2.2  nathanw 	    ("density code %d, %d-byte blocks, write-%s, ",
    232  1.2.2.2  nathanw 	    st->media_density, st->media_blksize,
    233  1.2.2.2  nathanw 	    st->flags & ST_READONLY ? "protected" : "enabled"));
    234  1.2.2.2  nathanw 	SC_DEBUG(periph, SCSIPI_DB3,
    235  1.2.2.2  nathanw 	    ("%sbuffered\n",
    236  1.2.2.2  nathanw 	    scsipi_sense.header.dev_spec & SMH_DSP_BUFF_MODE ? "" : "un"));
    237  1.2.2.2  nathanw 	if (st->page_0_size)
    238  1.2.2.3  nathanw 		memcpy(st->sense_data, scsipi_sense.sense_data,
    239  1.2.2.2  nathanw 		    st->page_0_size);
    240  1.2.2.2  nathanw 	periph->periph_flags |= PERIPH_MEDIA_LOADED;
    241  1.2.2.2  nathanw 	return (0);
    242  1.2.2.2  nathanw }
    243  1.2.2.2  nathanw 
    244  1.2.2.2  nathanw /*
    245  1.2.2.2  nathanw  * Send a filled out parameter structure to the drive to
    246  1.2.2.2  nathanw  * set it into the desire modes etc.
    247  1.2.2.2  nathanw  */
    248  1.2.2.2  nathanw int
    249  1.2.2.2  nathanw st_scsibus_mode_select(st, flags)
    250  1.2.2.2  nathanw 	struct st_softc *st;
    251  1.2.2.2  nathanw 	int flags;
    252  1.2.2.2  nathanw {
    253  1.2.2.2  nathanw 	u_int scsi_select_len;
    254  1.2.2.2  nathanw 	struct scsi_select {
    255  1.2.2.2  nathanw 		struct scsipi_mode_header header;
    256  1.2.2.2  nathanw 		struct scsi_blk_desc blk_desc;
    257  1.2.2.2  nathanw 		u_char sense_data[MAX_PAGE_0_SIZE];
    258  1.2.2.2  nathanw 	} scsi_select;
    259  1.2.2.2  nathanw 	struct scsipi_periph *periph = st->sc_periph;
    260  1.2.2.2  nathanw 
    261  1.2.2.2  nathanw 	scsi_select_len = 12 + st->page_0_size;
    262  1.2.2.2  nathanw 
    263  1.2.2.2  nathanw 	/*
    264  1.2.2.2  nathanw 	 * This quirk deals with drives that have only one valid mode
    265  1.2.2.2  nathanw 	 * and think this gives them license to reject all mode selects,
    266  1.2.2.2  nathanw 	 * even if the selected mode is the one that is supported.
    267  1.2.2.2  nathanw 	 */
    268  1.2.2.2  nathanw 	if (st->quirks & ST_Q_UNIMODAL) {
    269  1.2.2.2  nathanw 		SC_DEBUG(periph, SCSIPI_DB3,
    270  1.2.2.2  nathanw 		    ("not setting density 0x%x blksize 0x%x\n",
    271  1.2.2.2  nathanw 		    st->density, st->blksize));
    272  1.2.2.2  nathanw 		return (0);
    273  1.2.2.2  nathanw 	}
    274  1.2.2.2  nathanw 
    275  1.2.2.2  nathanw 	/*
    276  1.2.2.2  nathanw 	 * Set up for a mode select
    277  1.2.2.2  nathanw 	 */
    278  1.2.2.3  nathanw 	memset(&scsi_select, 0, scsi_select_len);
    279  1.2.2.2  nathanw 	scsi_select.header.blk_desc_len = sizeof(struct scsi_blk_desc);
    280  1.2.2.2  nathanw 	scsi_select.header.dev_spec &= ~SMH_DSP_BUFF_MODE;
    281  1.2.2.2  nathanw 	scsi_select.blk_desc.density = st->density;
    282  1.2.2.2  nathanw 	if (st->flags & ST_DONTBUFFER)
    283  1.2.2.2  nathanw 		scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_OFF;
    284  1.2.2.2  nathanw 	else
    285  1.2.2.2  nathanw 		scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_ON;
    286  1.2.2.2  nathanw 	if (st->flags & ST_FIXEDBLOCKS)
    287  1.2.2.2  nathanw 		_lto3b(st->blksize, scsi_select.blk_desc.blklen);
    288  1.2.2.2  nathanw 	if (st->page_0_size)
    289  1.2.2.3  nathanw 		memcpy(scsi_select.sense_data, st->sense_data, st->page_0_size);
    290  1.2.2.2  nathanw 
    291  1.2.2.2  nathanw 	/*
    292  1.2.2.2  nathanw 	 * do the command
    293  1.2.2.2  nathanw 	 */
    294  1.2.2.2  nathanw 	return scsipi_mode_select(periph, 0, &scsi_select.header,
    295  1.2.2.2  nathanw 	    scsi_select_len, flags | XS_CTL_DATA_ONSTACK,
    296  1.2.2.2  nathanw 	    ST_RETRIES, ST_CTL_TIME);
    297  1.2.2.2  nathanw }
    298  1.2.2.2  nathanw 
    299  1.2.2.2  nathanw int
    300  1.2.2.2  nathanw st_scsibus_cmprss(st, flags, onoff)
    301  1.2.2.2  nathanw 	struct st_softc *st;
    302  1.2.2.2  nathanw 	int flags;
    303  1.2.2.2  nathanw 	int onoff;
    304  1.2.2.2  nathanw {
    305  1.2.2.2  nathanw 	u_int scsi_dlen;
    306  1.2.2.2  nathanw 	int byte2, page;
    307  1.2.2.2  nathanw 	struct scsi_select {
    308  1.2.2.2  nathanw 		struct scsipi_mode_header header;
    309  1.2.2.2  nathanw 		struct scsi_blk_desc blk_desc;
    310  1.2.2.2  nathanw 		u_char pdata[MAX(sizeof(struct scsi_tape_dev_conf_page),
    311  1.2.2.2  nathanw 		    sizeof(struct scsi_tape_dev_compression_page))];
    312  1.2.2.2  nathanw 	} scsi_pdata;
    313  1.2.2.2  nathanw 	struct scsi_tape_dev_conf_page *ptr;
    314  1.2.2.2  nathanw 	struct scsi_tape_dev_compression_page *cptr;
    315  1.2.2.2  nathanw 	struct scsipi_periph *periph = st->sc_periph;
    316  1.2.2.2  nathanw 	int error, ison;
    317  1.2.2.2  nathanw 
    318  1.2.2.2  nathanw 	scsi_dlen = sizeof(scsi_pdata);
    319  1.2.2.2  nathanw 	/*
    320  1.2.2.2  nathanw 	 * Do DATA COMPRESSION page first.
    321  1.2.2.2  nathanw 	 */
    322  1.2.2.2  nathanw 	page = SMS_PAGE_CTRL_CURRENT | 0xf;
    323  1.2.2.2  nathanw 	byte2 = 0;
    324  1.2.2.2  nathanw 
    325  1.2.2.2  nathanw 	/*
    326  1.2.2.2  nathanw 	 * Do the MODE SENSE command...
    327  1.2.2.2  nathanw 	 */
    328  1.2.2.2  nathanw again:
    329  1.2.2.3  nathanw 	memset(&scsi_pdata, 0, scsi_dlen);
    330  1.2.2.2  nathanw 	error = scsipi_mode_sense(periph, byte2, page,
    331  1.2.2.2  nathanw 	    &scsi_pdata.header, scsi_dlen, flags | XS_CTL_DATA_ONSTACK,
    332  1.2.2.2  nathanw 	    ST_RETRIES, ST_CTL_TIME);
    333  1.2.2.2  nathanw 
    334  1.2.2.2  nathanw 	if (error) {
    335  1.2.2.2  nathanw 		if (byte2 != SMS_DBD) {
    336  1.2.2.2  nathanw 			byte2 = SMS_DBD;
    337  1.2.2.2  nathanw 			goto again;
    338  1.2.2.2  nathanw 		}
    339  1.2.2.2  nathanw 		/*
    340  1.2.2.2  nathanw 		 * Try a different page?
    341  1.2.2.2  nathanw 		 */
    342  1.2.2.2  nathanw 		if (page == (SMS_PAGE_CTRL_CURRENT | 0xf)) {
    343  1.2.2.2  nathanw 			page = SMS_PAGE_CTRL_CURRENT | 0x10;
    344  1.2.2.2  nathanw 			byte2 = 0;
    345  1.2.2.2  nathanw 			goto again;
    346  1.2.2.2  nathanw 		}
    347  1.2.2.2  nathanw 		return (error);
    348  1.2.2.2  nathanw 	}
    349  1.2.2.2  nathanw 
    350  1.2.2.2  nathanw 	if (scsi_pdata.header.blk_desc_len)
    351  1.2.2.2  nathanw 		ptr = (struct scsi_tape_dev_conf_page *) scsi_pdata.pdata;
    352  1.2.2.2  nathanw 	else
    353  1.2.2.2  nathanw 		ptr = (struct scsi_tape_dev_conf_page *) &scsi_pdata.blk_desc;
    354  1.2.2.2  nathanw 
    355  1.2.2.2  nathanw 	if ((page & SMS_PAGE_CODE) == 0xf) {
    356  1.2.2.2  nathanw 		cptr = (struct scsi_tape_dev_compression_page *) ptr;
    357  1.2.2.2  nathanw 		ison = (cptr->dce_dcc & DCP_DCE) != 0;
    358  1.2.2.2  nathanw 		if (onoff)
    359  1.2.2.2  nathanw 			cptr->dce_dcc |= DCP_DCE;
    360  1.2.2.2  nathanw 		else
    361  1.2.2.2  nathanw 			cptr->dce_dcc &= ~DCP_DCE;
    362  1.2.2.2  nathanw 		cptr->pagecode &= ~0x80;
    363  1.2.2.2  nathanw 	} else {
    364  1.2.2.2  nathanw 		ison =  (ptr->sel_comp_alg != 0);
    365  1.2.2.2  nathanw 		if (onoff)
    366  1.2.2.2  nathanw 			ptr->sel_comp_alg = 1;
    367  1.2.2.2  nathanw 		else
    368  1.2.2.2  nathanw 			ptr->sel_comp_alg = 0;
    369  1.2.2.2  nathanw 		ptr->pagecode &= ~0x80;
    370  1.2.2.2  nathanw 		ptr->byte2 = 0;
    371  1.2.2.2  nathanw 		ptr->active_partition = 0;
    372  1.2.2.2  nathanw 		ptr->wb_full_ratio = 0;
    373  1.2.2.2  nathanw 		ptr->rb_empty_ratio = 0;
    374  1.2.2.2  nathanw 		ptr->byte8 &= ~0x30;
    375  1.2.2.2  nathanw 		ptr->gap_size = 0;
    376  1.2.2.2  nathanw 		ptr->byte10 &= ~0xe7;
    377  1.2.2.2  nathanw 		ptr->ew_bufsize[0] = 0;
    378  1.2.2.2  nathanw 		ptr->ew_bufsize[1] = 0;
    379  1.2.2.2  nathanw 		ptr->ew_bufsize[2] = 0;
    380  1.2.2.2  nathanw 		ptr->reserved = 0;
    381  1.2.2.2  nathanw 	}
    382  1.2.2.2  nathanw 	/*
    383  1.2.2.2  nathanw 	 * There might be a virtue in actually doing the MODE SELECTS,
    384  1.2.2.2  nathanw 	 * but let's not clog the bus over it.
    385  1.2.2.2  nathanw 	 */
    386  1.2.2.2  nathanw 	if (onoff == ison)
    387  1.2.2.2  nathanw 		return (0);
    388  1.2.2.2  nathanw 
    389  1.2.2.2  nathanw 	/*
    390  1.2.2.2  nathanw 	 * Set up for a mode select
    391  1.2.2.2  nathanw 	 */
    392  1.2.2.2  nathanw 
    393  1.2.2.2  nathanw 	scsi_pdata.header.data_length = 0;
    394  1.2.2.2  nathanw 	scsi_pdata.header.medium_type = 0;
    395  1.2.2.2  nathanw 	if ((st->flags & ST_DONTBUFFER) == 0)
    396  1.2.2.2  nathanw 		scsi_pdata.header.dev_spec = SMH_DSP_BUFF_MODE_ON;
    397  1.2.2.2  nathanw 	else
    398  1.2.2.2  nathanw 		scsi_pdata.header.dev_spec = 0;
    399  1.2.2.2  nathanw 
    400  1.2.2.2  nathanw 	if (scsi_pdata.header.blk_desc_len) {
    401  1.2.2.2  nathanw 		scsi_pdata.blk_desc.density = 0;
    402  1.2.2.2  nathanw 		scsi_pdata.blk_desc.nblocks[0] = 0;
    403  1.2.2.2  nathanw 		scsi_pdata.blk_desc.nblocks[1] = 0;
    404  1.2.2.2  nathanw 		scsi_pdata.blk_desc.nblocks[2] = 0;
    405  1.2.2.2  nathanw 	}
    406  1.2.2.2  nathanw 
    407  1.2.2.2  nathanw 	/*
    408  1.2.2.2  nathanw 	 * Do the command
    409  1.2.2.2  nathanw 	 */
    410  1.2.2.2  nathanw 	error = scsipi_mode_select(periph, SMS_PF, &scsi_pdata.header,
    411  1.2.2.2  nathanw 	    scsi_dlen, flags | XS_CTL_DATA_ONSTACK, ST_RETRIES, ST_CTL_TIME);
    412  1.2.2.2  nathanw 
    413  1.2.2.2  nathanw 	if (error && (page & SMS_PAGE_CODE) == 0xf) {
    414  1.2.2.2  nathanw 		/*
    415  1.2.2.2  nathanw 		 * Try DEVICE CONFIGURATION page.
    416  1.2.2.2  nathanw 		 */
    417  1.2.2.2  nathanw 		page = SMS_PAGE_CTRL_CURRENT | 0x10;
    418  1.2.2.2  nathanw 		goto again;
    419  1.2.2.2  nathanw 	}
    420  1.2.2.2  nathanw 	return (error);
    421  1.2.2.2  nathanw }
    422