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