xref: /netbsd-src/sys/dev/scsipi/st_scsi.c (revision de1dfb1250df962f1ff3a011772cf58e605aed11)
1 /*	$NetBSD: st_scsi.c,v 1.12 2004/09/09 19:35:33 bouyer Exp $ */
2 
3 /*-
4  * Copyright (c) 1998 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@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@tfs.com) Sept 1992
54  * major changes by Julian Elischer (julian@jules.dialix.oz.au) May 1993
55  *
56  * A lot of rewhacking done by mjacob (mjacob@nas.nasa.gov).
57  */
58 
59 #include <sys/cdefs.h>
60 __KERNEL_RCSID(0, "$NetBSD: st_scsi.c,v 1.12 2004/09/09 19:35:33 bouyer 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/conf.h>
69 #include <sys/kernel.h>
70 #include <sys/systm.h>
71 
72 #include <dev/scsipi/stvar.h>
73 #include <dev/scsipi/scsi_tape.h>
74 #include <dev/scsipi/scsi_all.h>
75 
76 static int	st_scsibus_match(struct device *, struct cfdata *, void *);
77 static void	st_scsibus_attach(struct device *, struct device *, void *);
78 static int	st_scsibus_ops(struct st_softc *, int, int);
79 static int	st_scsibus_read_block_limits(struct st_softc *, int);
80 static int	st_scsibus_mode_sense(struct st_softc *, int);
81 static int	st_scsibus_mode_select(struct st_softc *, int);
82 static int	st_scsibus_cmprss(struct st_softc *, int, int);
83 
84 CFATTACH_DECL(st_scsibus, sizeof(struct st_softc),
85     st_scsibus_match, st_scsibus_attach, stdetach, stactivate);
86 
87 static const struct scsipi_inquiry_pattern st_scsibus_patterns[] = {
88 	{T_SEQUENTIAL, T_REMOV,
89 	 "",         "",                 ""},
90 };
91 
92 static int
93 st_scsibus_match(struct device *parent, struct cfdata *match, void *aux)
94 {
95 	struct scsipibus_attach_args *sa = aux;
96 	int priority;
97 
98 	if (scsipi_periph_bustype(sa->sa_periph) != SCSIPI_BUSTYPE_SCSI)
99 		return (0);
100 
101 	(void)scsipi_inqmatch(&sa->sa_inqbuf,
102 	    (caddr_t)st_scsibus_patterns,
103 	    sizeof(st_scsibus_patterns)/sizeof(st_scsibus_patterns[0]),
104 	    sizeof(st_scsibus_patterns[0]), &priority);
105 	return (priority);
106 }
107 
108 static void
109 st_scsibus_attach(struct device *parent, struct device *self, void *aux)
110 {
111 	struct st_softc *st = (void *)self;
112 
113 	st->ops = st_scsibus_ops;
114 	stattach(parent, st, aux);
115 }
116 
117 static int
118 st_scsibus_ops(struct st_softc *st, int op, int flags)
119 {
120 	switch(op) {
121 	case ST_OPS_RBL:
122 		return st_scsibus_read_block_limits(st, flags);
123 	case ST_OPS_MODESENSE:
124 		return st_scsibus_mode_sense(st, flags);
125 	case ST_OPS_MODESELECT:
126 		return st_scsibus_mode_select(st, flags);
127 	case ST_OPS_CMPRSS_ON:
128 	case ST_OPS_CMPRSS_OFF:
129 		return st_scsibus_cmprss(st, flags,
130 		    (op == ST_OPS_CMPRSS_ON) ? 1 : 0);
131 	default:
132 		panic("st_scsibus_ops: invalid op");
133 		return 0; /* XXX to appease gcc */
134 	}
135 	/* NOTREACHED */
136 }
137 
138 /*
139  * Ask the drive what it's min and max blk sizes are.
140  */
141 static int
142 st_scsibus_read_block_limits(struct st_softc *st, int flags)
143 {
144 	struct scsi_block_limits cmd;
145 	struct scsi_block_limits_data block_limits;
146 	struct scsipi_periph *periph = st->sc_periph;
147 	int error;
148 
149 	/*
150 	 * do a 'Read Block Limits'
151 	 */
152 	memset(&cmd, 0, sizeof(cmd));
153 	cmd.opcode = READ_BLOCK_LIMITS;
154 
155 	/*
156 	 * do the command, update the global values
157 	 */
158 	error = scsipi_command(periph, NULL, (struct scsipi_generic *)&cmd,
159 	    sizeof(cmd), (u_char *)&block_limits, sizeof(block_limits),
160 	    ST_RETRIES, ST_CTL_TIME, NULL,
161 	    flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
162 	if (error)
163 		return (error);
164 
165 	st->blkmin = _2btol(block_limits.min_length);
166 	st->blkmax = _3btol(block_limits.max_length);
167 
168 	SC_DEBUG(periph, SCSIPI_DB3,
169 	    ("(%d <= blksize <= %d)\n", st->blkmin, st->blkmax));
170 	return (0);
171 }
172 
173 /*
174  * Get the scsi driver to send a full inquiry to the
175  * device and use the results to fill out the global
176  * parameter structure.
177  *
178  * called from:
179  * attach
180  * open
181  * ioctl (to reset original blksize)
182  */
183 static int
184 st_scsibus_mode_sense(struct st_softc *st, int flags)
185 {
186 	u_int scsipi_sense_len;
187 	int error;
188 	struct scsipi_sense {
189 		struct scsipi_mode_header header;
190 		struct scsi_blk_desc blk_desc;
191 		u_char sense_data[MAX_PAGE_0_SIZE];
192 	} scsipi_sense;
193 	struct scsipi_periph *periph = st->sc_periph;
194 
195 	scsipi_sense_len = 12 + st->page_0_size;
196 
197 	/*
198 	 * Set up a mode sense
199 	 * We don't need the results. Just print them for our interest's sake,
200 	 * if asked, or if we need it as a template for the mode select store
201 	 * it away.
202 	 */
203 	error = scsipi_mode_sense(st->sc_periph, 0, SMS_PAGE_CTRL_CURRENT,
204 	    &scsipi_sense.header, scsipi_sense_len, flags | XS_CTL_DATA_ONSTACK,
205 	    ST_RETRIES, ST_CTL_TIME);
206 	if (error)
207 		return (error);
208 
209 	st->numblks = _3btol(scsipi_sense.blk_desc.nblocks);
210 	st->media_blksize = _3btol(scsipi_sense.blk_desc.blklen);
211 	st->media_density = scsipi_sense.blk_desc.density;
212 	if (scsipi_sense.header.dev_spec & SMH_DSP_WRITE_PROT)
213 		st->flags |= ST_READONLY;
214 	else
215 		st->flags &= ~ST_READONLY;
216 	SC_DEBUG(periph, SCSIPI_DB3,
217 	    ("density code %d, %d-byte blocks, write-%s, ",
218 	    st->media_density, st->media_blksize,
219 	    st->flags & ST_READONLY ? "protected" : "enabled"));
220 	SC_DEBUG(periph, SCSIPI_DB3,
221 	    ("%sbuffered\n",
222 	    scsipi_sense.header.dev_spec & SMH_DSP_BUFF_MODE ? "" : "un"));
223 	if (st->page_0_size)
224 		memcpy(st->sense_data, scsipi_sense.sense_data,
225 		    st->page_0_size);
226 	periph->periph_flags |= PERIPH_MEDIA_LOADED;
227 	return (0);
228 }
229 
230 /*
231  * Send a filled out parameter structure to the drive to
232  * set it into the desire modes etc.
233  */
234 static int
235 st_scsibus_mode_select(struct st_softc *st, int flags)
236 {
237 	u_int scsi_select_len;
238 	struct scsi_select {
239 		struct scsipi_mode_header header;
240 		struct scsi_blk_desc blk_desc;
241 		u_char sense_data[MAX_PAGE_0_SIZE];
242 	} scsi_select;
243 	struct scsipi_periph *periph = st->sc_periph;
244 
245 	scsi_select_len = 12 + st->page_0_size;
246 
247 	/*
248 	 * This quirk deals with drives that have only one valid mode
249 	 * and think this gives them license to reject all mode selects,
250 	 * even if the selected mode is the one that is supported.
251 	 */
252 	if (st->quirks & ST_Q_UNIMODAL) {
253 		SC_DEBUG(periph, SCSIPI_DB3,
254 		    ("not setting density 0x%x blksize 0x%x\n",
255 		    st->density, st->blksize));
256 		return (0);
257 	}
258 
259 	/*
260 	 * Set up for a mode select
261 	 */
262 	memset(&scsi_select, 0, scsi_select_len);
263 	scsi_select.header.blk_desc_len = sizeof(struct scsi_blk_desc);
264 	scsi_select.header.dev_spec &= ~SMH_DSP_BUFF_MODE;
265 	scsi_select.blk_desc.density = st->density;
266 	if (st->flags & ST_DONTBUFFER)
267 		scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_OFF;
268 	else
269 		scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_ON;
270 	if (st->flags & ST_FIXEDBLOCKS)
271 		_lto3b(st->blksize, scsi_select.blk_desc.blklen);
272 	if (st->page_0_size)
273 		memcpy(scsi_select.sense_data, st->sense_data, st->page_0_size);
274 
275 	/*
276 	 * do the command
277 	 */
278 	return scsipi_mode_select(periph, 0, &scsi_select.header,
279 	    scsi_select_len, flags | XS_CTL_DATA_ONSTACK,
280 	    ST_RETRIES, ST_CTL_TIME);
281 }
282 
283 static int
284 st_scsibus_cmprss(struct st_softc *st, int flags, int onoff)
285 {
286 	u_int scsi_dlen;
287 	int byte2, page;
288 	struct scsi_select {
289 		struct scsipi_mode_header header;
290 		struct scsi_blk_desc blk_desc;
291 		u_char pdata[MAX(sizeof(struct scsi_tape_dev_conf_page),
292 		    sizeof(struct scsi_tape_dev_compression_page))];
293 	} scsi_pdata;
294 	struct scsi_tape_dev_conf_page *ptr;
295 	struct scsi_tape_dev_compression_page *cptr;
296 	struct scsipi_periph *periph = st->sc_periph;
297 	int error, ison;
298 
299 	scsi_dlen = sizeof(scsi_pdata);
300 	/*
301 	 * Do DATA COMPRESSION page first.
302 	 */
303 	page = SMS_PAGE_CTRL_CURRENT | 0xf;
304 	byte2 = 0;
305 
306 	/*
307 	 * Do the MODE SENSE command...
308 	 */
309 again:
310 	memset(&scsi_pdata, 0, scsi_dlen);
311 	error = scsipi_mode_sense(periph, byte2, page,
312 	    &scsi_pdata.header, scsi_dlen, flags | XS_CTL_DATA_ONSTACK,
313 	    ST_RETRIES, ST_CTL_TIME);
314 
315 	if (error) {
316 		if (byte2 != SMS_DBD) {
317 			byte2 = SMS_DBD;
318 			goto again;
319 		}
320 		/*
321 		 * Try a different page?
322 		 */
323 		if (page == (SMS_PAGE_CTRL_CURRENT | 0xf)) {
324 			page = SMS_PAGE_CTRL_CURRENT | 0x10;
325 			byte2 = 0;
326 			goto again;
327 		}
328 		return (error);
329 	}
330 
331 	if (scsi_pdata.header.blk_desc_len)
332 		ptr = (struct scsi_tape_dev_conf_page *) scsi_pdata.pdata;
333 	else
334 		ptr = (struct scsi_tape_dev_conf_page *) &scsi_pdata.blk_desc;
335 
336 	if ((page & SMS_PAGE_CODE) == 0xf) {
337 		cptr = (struct scsi_tape_dev_compression_page *) ptr;
338 		ison = (cptr->dce_dcc & DCP_DCE) != 0;
339 		if (onoff)
340 			cptr->dce_dcc |= DCP_DCE;
341 		else
342 			cptr->dce_dcc &= ~DCP_DCE;
343 		cptr->pagecode &= ~0x80;
344 	} else {
345 		ison =  (ptr->sel_comp_alg != 0);
346 		if (onoff)
347 			ptr->sel_comp_alg = 1;
348 		else
349 			ptr->sel_comp_alg = 0;
350 		ptr->pagecode &= ~0x80;
351 		ptr->byte2 = 0;
352 		ptr->active_partition = 0;
353 		ptr->wb_full_ratio = 0;
354 		ptr->rb_empty_ratio = 0;
355 		ptr->byte8 &= ~0x30;
356 		ptr->gap_size = 0;
357 		ptr->byte10 &= ~0xe7;
358 		ptr->ew_bufsize[0] = 0;
359 		ptr->ew_bufsize[1] = 0;
360 		ptr->ew_bufsize[2] = 0;
361 		ptr->reserved = 0;
362 	}
363 	/*
364 	 * There might be a virtue in actually doing the MODE SELECTS,
365 	 * but let's not clog the bus over it.
366 	 */
367 	if (onoff == ison)
368 		return (0);
369 
370 	/*
371 	 * Set up for a mode select
372 	 */
373 
374 	scsi_pdata.header.data_length = 0;
375 	scsi_pdata.header.medium_type = 0;
376 	if ((st->flags & ST_DONTBUFFER) == 0)
377 		scsi_pdata.header.dev_spec = SMH_DSP_BUFF_MODE_ON;
378 	else
379 		scsi_pdata.header.dev_spec = 0;
380 
381 	if (scsi_pdata.header.blk_desc_len) {
382 		scsi_pdata.blk_desc.density = 0;
383 		scsi_pdata.blk_desc.nblocks[0] = 0;
384 		scsi_pdata.blk_desc.nblocks[1] = 0;
385 		scsi_pdata.blk_desc.nblocks[2] = 0;
386 	}
387 
388 	/*
389 	 * Do the command
390 	 */
391 	error = scsipi_mode_select(periph, SMS_PF, &scsi_pdata.header,
392 	    scsi_dlen, flags | XS_CTL_DATA_ONSTACK, ST_RETRIES, ST_CTL_TIME);
393 
394 	if (error && (page & SMS_PAGE_CODE) == 0xf) {
395 		/*
396 		 * Try DEVICE CONFIGURATION page.
397 		 */
398 		page = SMS_PAGE_CTRL_CURRENT | 0x10;
399 		goto again;
400 	}
401 	return (error);
402 }
403