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