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