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