Home | History | Annotate | Line # | Download | only in scsipi
st_scsi.c revision 1.36
      1 /*	$NetBSD: st_scsi.c,v 1.36 2014/10/18 08:33:28 snj 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Originally written by Julian Elischer (julian (at) tfs.com)
     34  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     35  *
     36  * TRW Financial Systems, in accordance with their agreement with Carnegie
     37  * Mellon University, makes this software available to CMU to distribute
     38  * or use in any manner that they see fit as long as this message is kept with
     39  * the software. For this reason TFS also grants any other persons or
     40  * organisations permission to use or modify this software.
     41  *
     42  * TFS supplies this software to be publicly redistributed
     43  * on the understanding that TFS is not responsible for the correct
     44  * functioning of this software in any circumstances.
     45  *
     46  * Ported to run under 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
     47  * major changes by Julian Elischer (julian (at) jules.dialix.oz.au) May 1993
     48  *
     49  * A lot of rewhacking done by mjacob (mjacob (at) nas.nasa.gov).
     50  */
     51 
     52 #include <sys/cdefs.h>
     53 __KERNEL_RCSID(0, "$NetBSD: st_scsi.c,v 1.36 2014/10/18 08:33:28 snj Exp $");
     54 
     55 #include "opt_scsi.h"
     56 
     57 #include <sys/param.h>
     58 #include <sys/device.h>
     59 #include <sys/buf.h>
     60 #include <sys/bufq.h>
     61 #include <sys/conf.h>
     62 #include <sys/kernel.h>
     63 #include <sys/systm.h>
     64 
     65 #include <dev/scsipi/scsi_all.h>
     66 #include <dev/scsipi/scsi_tape.h>
     67 #include <dev/scsipi/stvar.h>
     68 
     69 static int	st_scsibus_match(device_t, cfdata_t, void *);
     70 static void	st_scsibus_attach(device_t, device_t, void *);
     71 static int	st_scsibus_ops(struct st_softc *, int, int);
     72 static int	st_scsibus_read_block_limits(struct st_softc *, int);
     73 static int	st_scsibus_mode_sense(struct st_softc *, int);
     74 static int	st_scsibus_cmprss(struct st_softc *, int, int);
     75 
     76 CFATTACH_DECL_NEW(
     77 	st_scsibus,
     78 	sizeof(struct st_softc),
     79 	st_scsibus_match,
     80 	st_scsibus_attach,
     81 	stdetach,
     82 	NULL
     83 );
     84 
     85 static const struct scsipi_inquiry_pattern st_scsibus_patterns[] = {
     86 	{T_SEQUENTIAL, T_REMOV,
     87 	 "",         "",                 ""},
     88 };
     89 
     90 static int
     91 st_scsibus_match(device_t parent, cfdata_t match, void *aux)
     92 {
     93 	struct scsipibus_attach_args *sa = aux;
     94 	int priority;
     95 
     96 	if (SCSIPI_BUSTYPE_TYPE(scsipi_periph_bustype(sa->sa_periph)) !=
     97 	    SCSIPI_BUSTYPE_SCSI)
     98 		return 0;
     99 
    100 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
    101 	    st_scsibus_patterns,
    102 	    sizeof(st_scsibus_patterns)/sizeof(st_scsibus_patterns[0]),
    103 	    sizeof(st_scsibus_patterns[0]), &priority);
    104 	return priority;
    105 }
    106 
    107 static void
    108 st_scsibus_attach(device_t parent, device_t self, void *aux)
    109 {
    110 	struct st_softc *st = device_private(self);
    111 
    112 	st->ops = st_scsibus_ops;
    113 	stattach(parent, self, aux);
    114 }
    115 
    116 static int
    117 st_scsibus_ops(struct st_softc *st, int op, int flags)
    118 {
    119 	switch(op) {
    120 	case ST_OPS_RBL:
    121 		return st_scsibus_read_block_limits(st, flags);
    122 	case ST_OPS_MODESENSE:
    123 		return st_scsibus_mode_sense(st, flags);
    124 	case ST_OPS_MODESELECT:
    125 		return st_mode_select(st, flags);
    126 	case ST_OPS_CMPRSS_ON:
    127 	case ST_OPS_CMPRSS_OFF:
    128 		return st_scsibus_cmprss(st, flags,
    129 		    (op == ST_OPS_CMPRSS_ON) ? 1 : 0);
    130 	default:
    131 		panic("st_scsibus_ops: invalid op");
    132 		return 0; /* XXX to appease gcc */
    133 	}
    134 	/* NOTREACHED */
    135 }
    136 
    137 /*
    138  * Ask the drive what its min and max blk sizes are.
    139  */
    140 static int
    141 st_scsibus_read_block_limits(struct st_softc *st, int flags)
    142 {
    143 	struct scsi_block_limits cmd;
    144 	struct scsi_block_limits_data block_limits;
    145 	struct scsipi_periph *periph = st->sc_periph;
    146 	int error;
    147 
    148 	/* do a 'Read Block Limits' */
    149 	memset(&cmd, 0, sizeof(cmd));
    150 	cmd.opcode = READ_BLOCK_LIMITS;
    151 
    152 	/* do the command, update the global values */
    153 	error = scsipi_command(periph, (void *)&cmd, sizeof(cmd),
    154 	    (void *)&block_limits, sizeof(block_limits),
    155 	    ST_RETRIES, ST_CTL_TIME, NULL, flags | XS_CTL_DATA_IN);
    156 	if (error)
    157 		return error;
    158 
    159 	st->blkmin = _2btol(block_limits.min_length);
    160 	st->blkmax = _3btol(block_limits.max_length);
    161 
    162 	SC_DEBUG(periph, SCSIPI_DB3,
    163 	    ("(%d <= blksize <= %d)\n", st->blkmin, st->blkmax));
    164 	return 0;
    165 }
    166 
    167 /*
    168  * Get the scsi driver to send a full inquiry to the
    169  * device and use the results to fill out the global
    170  * parameter structure.
    171  *
    172  * called from:
    173  * attach
    174  * open
    175  * ioctl (to reset original blksize)
    176  */
    177 static int
    178 st_scsibus_mode_sense(struct st_softc *st, int flags)
    179 {
    180 	u_int scsipi_sense_len;
    181 	int error;
    182 	struct scsipi_sense {
    183 		struct scsi_mode_parameter_header_6 header;
    184 		struct scsi_general_block_descriptor blk_desc;
    185 		u_char sense_data[MAX_PAGE_0_SIZE];
    186 	} scsipi_sense;
    187 	struct scsipi_periph *periph = st->sc_periph;
    188 
    189 	scsipi_sense_len = sizeof(scsipi_sense.header) +
    190 			   sizeof(scsipi_sense.blk_desc) +
    191 			   st->page_0_size;
    192 
    193 	/*
    194 	 * Set up a mode sense
    195 	 * We don't need the results. Just print them for our interest's sake,
    196 	 * if asked, or if we need it as a template for the mode select store
    197 	 * it away.
    198 	 */
    199 	error = scsipi_mode_sense(st->sc_periph, 0, SMS_PCTRL_CURRENT,
    200 	    &scsipi_sense.header, scsipi_sense_len, flags,
    201 	    ST_RETRIES, ST_CTL_TIME);
    202 	if (error)
    203 		return error;
    204 
    205 	st->numblks = _3btol(scsipi_sense.blk_desc.nblocks);
    206 	st->media_blksize = _3btol(scsipi_sense.blk_desc.blklen);
    207 	st->media_density = scsipi_sense.blk_desc.density;
    208 	if (scsipi_sense.header.dev_spec & SMH_DSP_WRITE_PROT)
    209 		st->flags |= ST_READONLY;
    210 	else
    211 		st->flags &= ~ST_READONLY;
    212 	SC_DEBUG(periph, SCSIPI_DB3,
    213 	    ("density code %d, %d-byte blocks, write-%s, ",
    214 	    st->media_density, st->media_blksize,
    215 	    st->flags & ST_READONLY ? "protected" : "enabled"));
    216 	SC_DEBUG(periph, SCSIPI_DB3,
    217 	    ("%sbuffered\n",
    218 	    scsipi_sense.header.dev_spec & SMH_DSP_BUFF_MODE ? "" : "un"));
    219 	if (st->page_0_size)
    220 		memcpy(st->sense_data, scsipi_sense.sense_data,
    221 		    st->page_0_size);
    222 	periph->periph_flags |= PERIPH_MEDIA_LOADED;
    223 	return 0;
    224 }
    225 
    226 static int
    227 st_scsibus_cmprss(struct st_softc *st, int flags, int onoff)
    228 {
    229 	u_int scsi_dlen;
    230 	int byte2, page;
    231 	struct scsi_select {
    232 		struct scsi_mode_parameter_header_6 header;
    233 		struct scsi_general_block_descriptor blk_desc;
    234 		u_char pdata[MAX(sizeof(struct scsi_tape_dev_conf_page),
    235 		    sizeof(struct scsi_tape_dev_compression_page))];
    236 	} scsi_pdata;
    237 	struct scsi_tape_dev_conf_page *ptr;
    238 	struct scsi_tape_dev_compression_page *cptr;
    239 	struct scsipi_periph *periph = st->sc_periph;
    240 	int error, ison;
    241 
    242 	scsi_dlen = sizeof(scsi_pdata);
    243 	/* Do DATA COMPRESSION page first. */
    244 	page = SMS_PCTRL_CURRENT | 0xf;
    245 	byte2 = 0;
    246 
    247 	/*  Do the MODE SENSE command... */
    248 again:
    249 	memset(&scsi_pdata, 0, scsi_dlen);
    250 	error = scsipi_mode_sense(periph, byte2, page,
    251 	    &scsi_pdata.header, scsi_dlen, flags, ST_RETRIES, ST_CTL_TIME);
    252 
    253 	if (error) {
    254 		if (byte2 != SMS_DBD) {
    255 			byte2 = SMS_DBD;
    256 			goto again;
    257 		}
    258 		/* Try a different page? */
    259 		if (page == (SMS_PCTRL_CURRENT | 0xf)) {
    260 			page = SMS_PCTRL_CURRENT | 0x10;
    261 			byte2 = 0;
    262 			goto again;
    263 		}
    264 		return error;
    265 	}
    266 
    267 	if (scsi_pdata.header.blk_desc_len)
    268 		ptr = (struct scsi_tape_dev_conf_page *) scsi_pdata.pdata;
    269 	else
    270 		ptr = (struct scsi_tape_dev_conf_page *) &scsi_pdata.blk_desc;
    271 
    272 	if ((page & SMS_PAGE_MASK) == 0xf) {
    273 		cptr = (struct scsi_tape_dev_compression_page *) ptr;
    274 		ison = (cptr->dce_dcc & DCP_DCE) != 0;
    275 		if (onoff)
    276 			cptr->dce_dcc |= DCP_DCE;
    277 		else
    278 			cptr->dce_dcc &= ~DCP_DCE;
    279 		cptr->pagecode &= ~0x80;
    280 	} else {
    281 		ison =  (ptr->sel_comp_alg != 0);
    282 		if (onoff)
    283 			ptr->sel_comp_alg = 1;
    284 		else
    285 			ptr->sel_comp_alg = 0;
    286 		ptr->pagecode &= ~0x80;
    287 		ptr->byte2 = 0;
    288 		ptr->active_partition = 0;
    289 		ptr->wb_full_ratio = 0;
    290 		ptr->rb_empty_ratio = 0;
    291 		ptr->byte8 &= ~0x30;
    292 		ptr->gap_size = 0;
    293 		ptr->byte10 &= ~0xe7;
    294 		ptr->ew_bufsize[0] = 0;
    295 		ptr->ew_bufsize[1] = 0;
    296 		ptr->ew_bufsize[2] = 0;
    297 		ptr->reserved = 0;
    298 	}
    299 	/*
    300 	 * There might be a virtue in actually doing the MODE SELECTS,
    301 	 * but let's not clog the bus over it.
    302 	 */
    303 	if (onoff == ison)
    304 		return 0;
    305 
    306 	/* Set up for a mode select */
    307 	scsi_pdata.header.data_length = 0;
    308 	scsi_pdata.header.medium_type = 0;
    309 	if ((st->flags & ST_DONTBUFFER) == 0)
    310 		scsi_pdata.header.dev_spec = SMH_DSP_BUFF_MODE_ON;
    311 	else
    312 		scsi_pdata.header.dev_spec = 0;
    313 
    314 	if (scsi_pdata.header.blk_desc_len) {
    315 		scsi_pdata.blk_desc.density = 0;
    316 		scsi_pdata.blk_desc.nblocks[0] = 0;
    317 		scsi_pdata.blk_desc.nblocks[1] = 0;
    318 		scsi_pdata.blk_desc.nblocks[2] = 0;
    319 	}
    320 
    321 	/* Do the command */
    322 	error = scsipi_mode_select(periph, SMS_PF, &scsi_pdata.header,
    323 	    scsi_dlen, flags, ST_RETRIES, ST_CTL_TIME);
    324 
    325 	if (error && (page & SMS_PAGE_MASK) == 0xf) {
    326 		/* Try DEVICE CONFIGURATION page. */
    327 		page = SMS_PCTRL_CURRENT | 0x10;
    328 		goto again;
    329 	}
    330 	return error;
    331 }
    332