st.c revision 1.126 1 /* $NetBSD: st.c,v 1.126 2000/11/02 00:52:15 pk 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/systm.h>
65 #include <sys/fcntl.h>
66 #include <sys/errno.h>
67 #include <sys/ioctl.h>
68 #include <sys/malloc.h>
69 #include <sys/buf.h>
70 #include <sys/proc.h>
71 #include <sys/user.h>
72 #include <sys/mtio.h>
73 #include <sys/device.h>
74 #include <sys/conf.h>
75 #include <sys/kernel.h>
76 #if NRND > 0
77 #include <sys/rnd.h>
78 #endif
79
80 #include <dev/scsipi/scsipi_all.h>
81 #include <dev/scsipi/scsi_all.h>
82 #include <dev/scsipi/scsi_tape.h>
83 #include <dev/scsipi/scsiconf.h>
84
85 /* Defines for device specific stuff */
86 #define DEF_FIXED_BSIZE 512
87 #define ST_RETRIES 4 /* only on non IO commands */
88
89 #define STMODE(z) ( minor(z) & 0x03)
90 #define STDSTY(z) ((minor(z) >> 2) & 0x03)
91 #define STUNIT(z) ((minor(z) >> 4) )
92
93 #define NORMAL_MODE 0
94 #define NOREW_MODE 1
95 #define EJECT_MODE 2
96 #define CTRL_MODE 3
97
98 #define FALSE 0
99 #define TRUE 1
100
101 #define ST_IO_TIME (3 * 60 * 1000) /* 3 minutes */
102 #define ST_CTL_TIME (30 * 1000) /* 30 seconds */
103 #define ST_SPC_TIME (4 * 60 * 60 * 1000) /* 4 hours */
104
105 #ifndef ST_MOUNT_DELAY
106 #define ST_MOUNT_DELAY 0
107 #endif
108
109 /*
110 * Define various devices that we know mis-behave in some way,
111 * and note how they are bad, so we can correct for them
112 */
113 struct modes {
114 u_int quirks; /* same definitions as in quirkdata */
115 int blksize;
116 u_int8_t density;
117 };
118
119 struct quirkdata {
120 u_int quirks;
121 #define ST_Q_FORCE_BLKSIZE 0x0001
122 #define ST_Q_SENSE_HELP 0x0002 /* must do READ for good MODE SENSE */
123 #define ST_Q_IGNORE_LOADS 0x0004
124 #define ST_Q_BLKSIZE 0x0008 /* variable-block media_blksize > 0 */
125 #define ST_Q_UNIMODAL 0x0010 /* unimode drive rejects mode select */
126 #define ST_Q_NOPREVENT 0x0020 /* does not support PREVENT */
127 u_int page_0_size;
128 #define MAX_PAGE_0_SIZE 64
129 struct modes modes[4];
130 };
131
132 struct st_quirk_inquiry_pattern {
133 struct scsipi_inquiry_pattern pattern;
134 struct quirkdata quirkdata;
135 };
136
137 struct st_quirk_inquiry_pattern st_quirk_patterns[] = {
138 {{T_SEQUENTIAL, T_REMOV,
139 " ", " ", " "}, {0, 0, {
140 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */
141 {ST_Q_FORCE_BLKSIZE, 512, QIC_24}, /* minor 4-7 */
142 {ST_Q_FORCE_BLKSIZE, 0, HALFINCH_1600}, /* minor 8-11 */
143 {ST_Q_FORCE_BLKSIZE, 0, HALFINCH_6250} /* minor 12-15 */
144 }}},
145 {{T_SEQUENTIAL, T_REMOV,
146 "TANDBERG", " TDC 3600 ", ""}, {0, 12, {
147 {0, 0, 0}, /* minor 0-3 */
148 {ST_Q_FORCE_BLKSIZE, 0, QIC_525}, /* minor 4-7 */
149 {0, 0, QIC_150}, /* minor 8-11 */
150 {0, 0, QIC_120} /* minor 12-15 */
151 }}},
152 {{T_SEQUENTIAL, T_REMOV,
153 "TANDBERG", " TDC 3800 ", ""}, {0, 0, {
154 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */
155 {0, 0, QIC_525}, /* minor 4-7 */
156 {0, 0, QIC_150}, /* minor 8-11 */
157 {0, 0, QIC_120} /* minor 12-15 */
158 }}},
159 /*
160 * lacking a manual for the 4200, it's not clear what the
161 * specific density codes should be- the device is a 2.5GB
162 * capable QIC drive, those density codes aren't readily
163 * availabel. The 'default' will just have to do.
164 */
165 {{T_SEQUENTIAL, T_REMOV,
166 "TANDBERG", " TDC 4200 ", ""}, {0, 0, {
167 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */
168 {0, 0, QIC_525}, /* minor 4-7 */
169 {0, 0, QIC_150}, /* minor 8-11 */
170 {0, 0, QIC_120} /* minor 12-15 */
171 }}},
172 /*
173 * At least -005 and -007 need this. I'll assume they all do unless I
174 * hear otherwise. - mycroft, 31MAR1994
175 */
176 {{T_SEQUENTIAL, T_REMOV,
177 "ARCHIVE ", "VIPER 2525 25462", ""}, {0, 0, {
178 {ST_Q_SENSE_HELP, 0, 0}, /* minor 0-3 */
179 {ST_Q_SENSE_HELP, 0, QIC_525}, /* minor 4-7 */
180 {0, 0, QIC_150}, /* minor 8-11 */
181 {0, 0, QIC_120} /* minor 12-15 */
182 }}},
183 /*
184 * One user reports that this works for his tape drive. It probably
185 * needs more work. - mycroft, 09APR1994
186 */
187 {{T_SEQUENTIAL, T_REMOV,
188 "SANKYO ", "CP525 ", ""}, {0, 0, {
189 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */
190 {ST_Q_FORCE_BLKSIZE, 512, QIC_525}, /* minor 4-7 */
191 {0, 0, QIC_150}, /* minor 8-11 */
192 {0, 0, QIC_120} /* minor 12-15 */
193 }}},
194 {{T_SEQUENTIAL, T_REMOV,
195 "ANRITSU ", "DMT780 ", ""}, {0, 0, {
196 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */
197 {ST_Q_FORCE_BLKSIZE, 512, QIC_525}, /* minor 4-7 */
198 {0, 0, QIC_150}, /* minor 8-11 */
199 {0, 0, QIC_120} /* minor 12-15 */
200 }}},
201 {{T_SEQUENTIAL, T_REMOV,
202 "ARCHIVE ", "VIPER 150 21247", ""}, {0, 12, {
203 {ST_Q_SENSE_HELP, 0, 0}, /* minor 0-3 */
204 {0, 0, QIC_150}, /* minor 4-7 */
205 {0, 0, QIC_120}, /* minor 8-11 */
206 {0, 0, QIC_24} /* minor 12-15 */
207 }}},
208 {{T_SEQUENTIAL, T_REMOV,
209 "ARCHIVE ", "VIPER 150 21531", ""}, {0, 12, {
210 {ST_Q_SENSE_HELP, 0, 0}, /* minor 0-3 */
211 {0, 0, QIC_150}, /* minor 4-7 */
212 {0, 0, QIC_120}, /* minor 8-11 */
213 {0, 0, QIC_24} /* minor 12-15 */
214 }}},
215 {{T_SEQUENTIAL, T_REMOV,
216 "WANGTEK ", "5099ES SCSI", ""}, {0, 0, {
217 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */
218 {0, 0, QIC_11}, /* minor 4-7 */
219 {0, 0, QIC_24}, /* minor 8-11 */
220 {0, 0, QIC_24} /* minor 12-15 */
221 }}},
222 {{T_SEQUENTIAL, T_REMOV,
223 "WANGTEK ", "5150ES SCSI", ""}, {0, 0, {
224 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */
225 {0, 0, QIC_24}, /* minor 4-7 */
226 {0, 0, QIC_120}, /* minor 8-11 */
227 {0, 0, QIC_150} /* minor 12-15 */
228 }}},
229 {{T_SEQUENTIAL, T_REMOV,
230 "WANGTEK ", "5525ES SCSI REV7", ""}, {0, 0, {
231 {0, 0, 0}, /* minor 0-3 */
232 {ST_Q_BLKSIZE, 0, QIC_525}, /* minor 4-7 */
233 {0, 0, QIC_150}, /* minor 8-11 */
234 {0, 0, QIC_120} /* minor 12-15 */
235 }}},
236 {{T_SEQUENTIAL, T_REMOV,
237 "WangDAT ", "Model 1300 ", ""}, {0, 0, {
238 {0, 0, 0}, /* minor 0-3 */
239 {ST_Q_FORCE_BLKSIZE, 512, DDS}, /* minor 4-7 */
240 {ST_Q_FORCE_BLKSIZE, 1024, DDS}, /* minor 8-11 */
241 {ST_Q_FORCE_BLKSIZE, 0, DDS} /* minor 12-15 */
242 }}},
243 {{T_SEQUENTIAL, T_REMOV,
244 "EXABYTE ", "EXB-8200 ", "263H"}, {0, 5, {
245 {0, 0, 0}, /* minor 0-3 */
246 {0, 0, 0}, /* minor 4-7 */
247 {0, 0, 0}, /* minor 8-11 */
248 {0, 0, 0} /* minor 12-15 */
249 }}},
250 {{T_SEQUENTIAL, T_REMOV,
251 "STK", "9490", ""},
252 {ST_Q_FORCE_BLKSIZE, 0, {
253 {0, 0, 0}, /* minor 0-3 */
254 {0, 0, 0}, /* minor 4-7 */
255 {0, 0, 0}, /* minor 8-11 */
256 {0, 0, 0} /* minor 12-15 */
257 }}},
258 {{T_SEQUENTIAL, T_REMOV,
259 "STK", "SD-3", ""},
260 {ST_Q_FORCE_BLKSIZE, 0, {
261 {0, 0, 0}, /* minor 0-3 */
262 {0, 0, 0}, /* minor 4-7 */
263 {0, 0, 0}, /* minor 8-11 */
264 {0, 0, 0} /* minor 12-15 */
265 }}},
266 {{T_SEQUENTIAL, T_REMOV,
267 "IBM", "03590", ""}, {ST_Q_IGNORE_LOADS, 0, {
268 {0, 0, 0}, /* minor 0-3 */
269 {0, 0, 0}, /* minor 4-7 */
270 {0, 0, 0}, /* minor 8-11 */
271 {0, 0, 0} /* minor 12-15 */
272 }}},
273 {{T_SEQUENTIAL, T_REMOV,
274 "HP ", "T4000s ", ""}, {ST_Q_UNIMODAL, 0, {
275 {0, 0, QIC_3095}, /* minor 0-3 */
276 {0, 0, QIC_3095}, /* minor 4-7 */
277 {0, 0, QIC_3095}, /* minor 8-11 */
278 {0, 0, QIC_3095}, /* minor 12-15 */
279 }}},
280 #if 0
281 {{T_SEQUENTIAL, T_REMOV,
282 "EXABYTE ", "EXB-8200 ", ""}, {0, 12, {
283 {0, 0, 0}, /* minor 0-3 */
284 {0, 0, 0}, /* minor 4-7 */
285 {0, 0, 0}, /* minor 8-11 */
286 {0, 0, 0} /* minor 12-15 */
287 }}},
288 #endif
289 {{T_SEQUENTIAL, T_REMOV,
290 "TEAC ", "MT-2ST/N50 ", ""}, {ST_Q_IGNORE_LOADS, 0, {
291 {0, 0, 0}, /* minor 0-3 */
292 {0, 0, 0}, /* minor 4-7 */
293 {0, 0, 0}, /* minor 8-11 */
294 {0, 0, 0} /* minor 12-15 */
295 }}},
296 {{T_SEQUENTIAL, T_REMOV,
297 "OnStream", "ADR50 Drive", ""}, {ST_Q_UNIMODAL, 0, {
298 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */
299 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 4-7 */
300 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 8-11 */
301 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 12-15 */
302 }}},
303 {{T_SEQUENTIAL, T_REMOV,
304 "NCR H621", "0-STD-03-46F880 ", ""}, {ST_Q_NOPREVENT, 0, {
305 {0, 0, 0}, /* minor 0-3 */
306 {0, 0, 0}, /* minor 4-7 */
307 {0, 0, 0}, /* minor 8-11 */
308 {0, 0, 0} /* minor 12-15 */
309 }}},
310 };
311
312 #define NOEJECT 0
313 #define EJECT 1
314
315 struct st_softc {
316 struct device sc_dev;
317 /*--------------------present operating parameters, flags etc.---------------*/
318 int flags; /* see below */
319 u_int quirks; /* quirks for the open mode */
320 int blksize; /* blksize we are using */
321 u_int8_t density; /* present density */
322 u_int page_0_size; /* size of page 0 data */
323 u_int last_dsty; /* last density opened */
324 short mt_resid; /* last (short) resid */
325 short mt_erreg; /* last error (sense key) seen */
326 #define mt_key mt_erreg
327 u_int8_t asc; /* last asc code seen */
328 u_int8_t ascq; /* last asc code seen */
329 /*--------------------device/scsi parameters---------------------------------*/
330 struct scsipi_link *sc_link; /* our link to the adapter etc. */
331 /*--------------------parameters reported by the device ---------------------*/
332 int blkmin; /* min blk size */
333 int blkmax; /* max blk size */
334 struct quirkdata *quirkdata; /* if we have a rogue entry */
335 /*--------------------parameters reported by the device for this media-------*/
336 u_long numblks; /* nominal blocks capacity */
337 int media_blksize; /* 0 if not ST_FIXEDBLOCKS */
338 u_int8_t media_density; /* this is what it said when asked */
339 /*--------------------quirks for the whole drive-----------------------------*/
340 u_int drive_quirks; /* quirks of this drive */
341 /*--------------------How we should set up when opening each minor device----*/
342 struct modes modes[4]; /* plus more for each mode */
343 u_int8_t modeflags[4]; /* flags for the modes */
344 #define DENSITY_SET_BY_USER 0x01
345 #define DENSITY_SET_BY_QUIRK 0x02
346 #define BLKSIZE_SET_BY_USER 0x04
347 #define BLKSIZE_SET_BY_QUIRK 0x08
348 /*--------------------storage for sense data returned by the drive-----------*/
349 u_char sense_data[MAX_PAGE_0_SIZE]; /*
350 * additional sense data needed
351 * for mode sense/select.
352 */
353 struct buf_queue buf_queue; /* the queue of pending IO */
354 /* operations */
355 #if NRND > 0
356 rndsource_element_t rnd_source;
357 #endif
358 };
359
360
361 int stmatch __P((struct device *, struct cfdata *, void *));
362 void stattach __P((struct device *, struct device *, void *));
363 void st_identify_drive __P((struct st_softc *,
364 struct scsipi_inquiry_pattern *));
365 void st_loadquirks __P((struct st_softc *));
366 int st_mount_tape __P((dev_t, int));
367 void st_unmount __P((struct st_softc *, boolean));
368 int st_decide_mode __P((struct st_softc *, boolean));
369 void ststart __P((void *));
370 void stdone __P((struct scsipi_xfer *));
371 int st_read __P((struct st_softc *, char *, int, int));
372 int st_read_block_limits __P((struct st_softc *, int));
373 int st_mode_sense __P((struct st_softc *, int));
374 int st_mode_select __P((struct st_softc *, int));
375 int st_cmprss __P((struct st_softc *, int));
376 int st_space __P((struct st_softc *, int, u_int, int));
377 int st_write_filemarks __P((struct st_softc *, int, int));
378 int st_check_eod __P((struct st_softc *, boolean, int *, int));
379 int st_load __P((struct st_softc *, u_int, int));
380 int st_rewind __P((struct st_softc *, u_int, int));
381 int st_interpret_sense __P((struct scsipi_xfer *));
382 int st_touch_tape __P((struct st_softc *));
383 int st_erase __P((struct st_softc *, int full, int flags));
384 int st_rdpos __P((struct st_softc *, int, u_int32_t *));
385 int st_setpos __P((struct st_softc *, int, u_int32_t *));
386
387 struct cfattach st_ca = {
388 sizeof(struct st_softc), stmatch, stattach
389 };
390
391 extern struct cfdriver st_cd;
392
393 struct scsipi_device st_switch = {
394 st_interpret_sense,
395 ststart,
396 NULL,
397 stdone
398 };
399
400 #define ST_INFO_VALID 0x0001
401 #define ST_BLOCK_SET 0x0002 /* block size, mode set by ioctl */
402 #define ST_WRITTEN 0x0004 /* data has been written, EOD needed */
403 #define ST_FIXEDBLOCKS 0x0008
404 #define ST_AT_FILEMARK 0x0010
405 #define ST_EIO_PENDING 0x0020 /* error reporting deferred until next op */
406 #define ST_NEW_MOUNT 0x0040 /* still need to decide mode */
407 #define ST_READONLY 0x0080 /* st_mode_sense says write protected */
408 #define ST_FM_WRITTEN 0x0100 /*
409 * EOF file mark written -- used with
410 * ~ST_WRITTEN to indicate that multiple file
411 * marks have been written
412 */
413 #define ST_BLANK_READ 0x0200 /* BLANK CHECK encountered already */
414 #define ST_2FM_AT_EOD 0x0400 /* write 2 file marks at EOD */
415 #define ST_MOUNTED 0x0800 /* Device is presently mounted */
416 #define ST_DONTBUFFER 0x1000 /* Disable buffering/caching */
417 #define ST_EARLYWARN 0x2000 /* Do (deferred) EOM for variable mode */
418 #define ST_EOM_PENDING 0x4000 /* EOM reporting deferred until next op */
419
420 #define ST_PER_ACTION (ST_AT_FILEMARK | ST_EIO_PENDING | ST_EOM_PENDING | \
421 ST_BLANK_READ)
422 #define ST_PER_MOUNT (ST_INFO_VALID | ST_BLOCK_SET | ST_WRITTEN | \
423 ST_FIXEDBLOCKS | ST_READONLY | ST_FM_WRITTEN | \
424 ST_2FM_AT_EOD | ST_PER_ACTION)
425
426 #if defined(ST_ENABLE_EARLYWARN)
427 #define ST_INIT_FLAGS ST_EARLYWARN
428 #else
429 #define ST_INIT_FLAGS 0
430 #endif
431
432 struct scsipi_inquiry_pattern st_patterns[] = {
433 {T_SEQUENTIAL, T_REMOV,
434 "", "", ""},
435 };
436
437 int
438 stmatch(parent, match, aux)
439 struct device *parent;
440 struct cfdata *match;
441 void *aux;
442 {
443 struct scsipibus_attach_args *sa = aux;
444 int priority;
445
446 (void)scsipi_inqmatch(&sa->sa_inqbuf,
447 (caddr_t)st_patterns, sizeof(st_patterns)/sizeof(st_patterns[0]),
448 sizeof(st_patterns[0]), &priority);
449 return (priority);
450 }
451
452 /*
453 * The routine called by the low level scsi routine when it discovers
454 * A device suitable for this driver
455 */
456 void
457 stattach(parent, self, aux)
458 struct device *parent, *self;
459 void *aux;
460 {
461 struct st_softc *st = (void *)self;
462 struct scsipibus_attach_args *sa = aux;
463 struct scsipi_link *sc_link = sa->sa_sc_link;
464
465 SC_DEBUG(sc_link, SDEV_DB2, ("stattach: "));
466
467 /*
468 * Store information needed to contact our base driver
469 */
470 st->sc_link = sc_link;
471 sc_link->device = &st_switch;
472 sc_link->device_softc = st;
473 sc_link->openings = 1;
474
475 /*
476 * Set initial flags
477 */
478
479 st->flags = ST_INIT_FLAGS;
480
481 /*
482 * Check if the drive is a known criminal and take
483 * Any steps needed to bring it into line
484 */
485 st_identify_drive(st, &sa->sa_inqbuf);
486 /*
487 * Use the subdriver to request information regarding
488 * the drive. We cannot use interrupts yet, so the
489 * request must specify this.
490 */
491 printf("\n");
492 printf("%s: %s", st->sc_dev.dv_xname, st->quirkdata ? "rogue, " : "");
493 if (scsipi_test_unit_ready(sc_link,
494 XS_CTL_DISCOVERY | XS_CTL_SILENT | XS_CTL_IGNORE_MEDIA_CHANGE) ||
495 st_mode_sense(st,
496 XS_CTL_DISCOVERY | XS_CTL_SILENT | XS_CTL_IGNORE_MEDIA_CHANGE))
497 printf("drive empty\n");
498 else {
499 printf("density code %d, ", st->media_density);
500 if (st->media_blksize > 0)
501 printf("%d-byte", st->media_blksize);
502 else
503 printf("variable");
504 printf(" blocks, write-%s\n",
505 (st->flags & ST_READONLY) ? "protected" : "enabled");
506 }
507
508 /*
509 * Set up the buf queue for this device
510 */
511 BUFQ_INIT(&st->buf_queue);
512
513 #if NRND > 0
514 rnd_attach_source(&st->rnd_source, st->sc_dev.dv_xname,
515 RND_TYPE_TAPE, 0);
516 #endif
517 }
518
519 /*
520 * Use the inquiry routine in 'scsi_base' to get drive info so we can
521 * Further tailor our behaviour.
522 */
523 void
524 st_identify_drive(st, inqbuf)
525 struct st_softc *st;
526 struct scsipi_inquiry_pattern *inqbuf;
527 {
528 struct st_quirk_inquiry_pattern *finger;
529 int priority;
530
531 finger = (struct st_quirk_inquiry_pattern *)scsipi_inqmatch(inqbuf,
532 (caddr_t)st_quirk_patterns,
533 sizeof(st_quirk_patterns) / sizeof(st_quirk_patterns[0]),
534 sizeof(st_quirk_patterns[0]), &priority);
535 if (priority != 0) {
536 st->quirkdata = &finger->quirkdata;
537 st->drive_quirks = finger->quirkdata.quirks;
538 st->quirks = finger->quirkdata.quirks; /* start value */
539 st->page_0_size = finger->quirkdata.page_0_size;
540 st_loadquirks(st);
541 }
542 }
543
544 /*
545 * initialise the subdevices to the default (QUIRK) state.
546 * this will remove any setting made by the system operator or previous
547 * operations.
548 */
549 void
550 st_loadquirks(st)
551 struct st_softc *st;
552 {
553 int i;
554 struct modes *mode;
555 struct modes *mode2;
556
557 mode = st->quirkdata->modes;
558 mode2 = st->modes;
559 for (i = 0; i < 4; i++) {
560 bzero(mode2, sizeof(struct modes));
561 st->modeflags[i] &= ~(BLKSIZE_SET_BY_QUIRK |
562 DENSITY_SET_BY_QUIRK | BLKSIZE_SET_BY_USER |
563 DENSITY_SET_BY_USER);
564 if ((mode->quirks | st->drive_quirks) & ST_Q_FORCE_BLKSIZE) {
565 mode2->blksize = mode->blksize;
566 st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK;
567 }
568 if (mode->density) {
569 mode2->density = mode->density;
570 st->modeflags[i] |= DENSITY_SET_BY_QUIRK;
571 }
572 mode2->quirks |= mode->quirks;
573 mode++;
574 mode2++;
575 }
576 }
577
578 /*
579 * open the device.
580 */
581 int
582 stopen(dev, flags, mode, p)
583 dev_t dev;
584 int flags;
585 int mode;
586 struct proc *p;
587 {
588 u_int stmode, dsty;
589 int error, sflags, unit, tries, ntries;
590 struct st_softc *st;
591 struct scsipi_link *sc_link;
592
593 unit = STUNIT(dev);
594 if (unit >= st_cd.cd_ndevs)
595 return (ENXIO);
596 st = st_cd.cd_devs[unit];
597 if (st == NULL)
598 return (ENXIO);
599
600 stmode = STMODE(dev);
601 dsty = STDSTY(dev);
602 sc_link = st->sc_link;
603
604 SC_DEBUG(sc_link, SDEV_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
605 unit, st_cd.cd_ndevs));
606
607
608 /*
609 * Only allow one at a time
610 */
611 if (sc_link->flags & SDEV_OPEN) {
612 printf("%s: already open\n", st->sc_dev.dv_xname);
613 return (EBUSY);
614 }
615
616 if ((error = scsipi_adapter_addref(sc_link)) != 0)
617 return (error);
618
619 /*
620 * clear any latched errors.
621 */
622 st->mt_resid = 0;
623 st->mt_erreg = 0;
624 st->asc = 0;
625 st->ascq = 0;
626
627 /*
628 * Catch any unit attention errors. Be silent about this
629 * unless we're already mounted. We ignore media change
630 * if we're in control mode or not mounted yet.
631 */
632 if ((st->flags & ST_MOUNTED) == 0 || stmode == CTRL_MODE) {
633 #ifdef SCSIDEBUG
634 sflags = XS_CTL_IGNORE_MEDIA_CHANGE;
635 #else
636 sflags = XS_CTL_SILENT|XS_CTL_IGNORE_MEDIA_CHANGE;
637 #endif
638 } else
639 sflags = 0;
640
641 /*
642 * If we're already mounted or we aren't configured for
643 * a mount delay, only try a test unit ready once. Otherwise,
644 * try up to ST_MOUNT_DELAY times with a rest interval of
645 * one second between each try.
646 */
647
648 if ((st->flags & ST_MOUNTED) || ST_MOUNT_DELAY == 0) {
649 ntries = 1;
650 } else {
651 ntries = ST_MOUNT_DELAY;
652 }
653
654 for (error = tries = 0; tries < ntries; tries++) {
655 int slpintr, oflags;
656
657 /*
658 * If we had no error, or we're opening the control mode
659 * device, we jump out right away.
660 */
661
662 error = scsipi_test_unit_ready(sc_link, sflags);
663 if (error == 0 || stmode == CTRL_MODE) {
664 break;
665 }
666
667 /*
668 * We had an error.
669 *
670 * If we're already mounted or we aren't configured for
671 * a mount delay, or the error isn't a NOT READY error,
672 * skip to the error exit now.
673 */
674 if ((st->flags & ST_MOUNTED) || ST_MOUNT_DELAY == 0 ||
675 (st->mt_key != SKEY_NOT_READY)) {
676 goto bad;
677 }
678
679 /*
680 * clear any latched errors.
681 */
682 st->mt_resid = 0;
683 st->mt_erreg = 0;
684 st->asc = 0;
685 st->ascq = 0;
686
687 /*
688 * Fake that we have the device open so
689 * we block other apps from getting in.
690 */
691
692 oflags = sc_link->flags;
693 sc_link->flags |= SDEV_OPEN;
694
695 slpintr = tsleep(&lbolt, PUSER|PCATCH, "stload", 0);
696
697 sc_link->flags = oflags; /* restore flags */
698
699 if (slpintr) {
700 goto bad;
701 }
702 }
703
704
705 /*
706 * If the mode is 3 (e.g. minor = 3,7,11,15) then the device has
707 * been opened to set defaults and perform other, usually non-I/O
708 * related, operations. In this case, do a quick check to see
709 * whether the unit actually had a tape loaded (this will be known
710 * as to whether or not we got a NOT READY for the above
711 * unit attention). If a tape is there, go do a mount sequence.
712 */
713 if (stmode == CTRL_MODE && st->mt_key == SKEY_NOT_READY) {
714 sc_link->flags |= SDEV_OPEN;
715 return (0);
716 }
717
718 /*
719 * If we get this far and had an error set, that means we failed
720 * to pass the 'test unit ready' test for the non-controlmode device,
721 * so we bounce the open.
722 */
723
724 if (error)
725 return (error);
726
727 /*
728 * Else, we're now committed to saying we're open.
729 */
730
731 sc_link->flags |= SDEV_OPEN; /* unit attn are now errors */
732
733 /*
734 * If it's a different mode, or if the media has been
735 * invalidated, unmount the tape from the previous
736 * session but continue with open processing
737 */
738 if (st->last_dsty != dsty || !(sc_link->flags & SDEV_MEDIA_LOADED))
739 st_unmount(st, NOEJECT);
740
741 /*
742 * If we are not mounted, then we should start a new
743 * mount session.
744 */
745 if (!(st->flags & ST_MOUNTED)) {
746 st_mount_tape(dev, flags);
747 st->last_dsty = dsty;
748 }
749
750 SC_DEBUG(sc_link, SDEV_DB2, ("open complete\n"));
751 return (0);
752
753 bad:
754 st_unmount(st, NOEJECT);
755 scsipi_adapter_delref(sc_link);
756 sc_link->flags &= ~SDEV_OPEN;
757 return (error);
758 }
759
760 /*
761 * close the device.. only called if we are the LAST
762 * occurence of an open device
763 */
764 int
765 stclose(dev, flags, mode, p)
766 dev_t dev;
767 int flags;
768 int mode;
769 struct proc *p;
770 {
771 int stxx, error = 0;
772 struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
773
774 SC_DEBUG(st->sc_link, SDEV_DB1, ("closing\n"));
775
776 /*
777 * Make sure that a tape opened in write-only mode will have
778 * file marks written on it when closed, even if not written to.
779 *
780 * This is for SUN compatibility. Actually, the Sun way of
781 * things is to:
782 *
783 * only write filemarks if there are fmks to be written and
784 * - open for write (possibly read/write)
785 * - the last operation was a write
786 * or:
787 * - opened for wronly
788 * - no data was written (including filemarks)
789 */
790
791 stxx = st->flags & (ST_WRITTEN | ST_FM_WRITTEN);
792 if (((flags & FWRITE) && stxx == ST_WRITTEN) ||
793 ((flags & O_ACCMODE) == FWRITE && stxx == 0)) {
794 int nm;
795 error = st_check_eod(st, FALSE, &nm, 0);
796 }
797
798 switch (STMODE(dev)) {
799 case NORMAL_MODE:
800 st_unmount(st, NOEJECT);
801 break;
802 case NOREW_MODE:
803 case CTRL_MODE:
804 /*
805 * Leave mounted unless media seems to have been removed.
806 *
807 * Otherwise, if we're to terminate a tape with more than one
808 * filemark [ and because we're not rewinding here ], backspace
809 * one filemark so that later appends will see an unbroken
810 * sequence of:
811 *
812 * file - FMK - file - FMK ... file - FMK FMK (EOM)
813 */
814 if (!(st->sc_link->flags & SDEV_MEDIA_LOADED)) {
815 st_unmount(st, NOEJECT);
816 } else if (error == 0) {
817 /*
818 * ST_WRITTEN was preserved from above.
819 *
820 * All we need to know here is:
821 *
822 * Were we writing this tape and was the last
823 * operation a write?
824 *
825 * Are there supposed to be 2FM at EOD?
826 *
827 * If both statements are true, then we backspace
828 * one filemark.
829 */
830 stxx |= (st->flags & ST_2FM_AT_EOD);
831 if ((flags & FWRITE) != 0 &&
832 (stxx == (ST_2FM_AT_EOD|ST_WRITTEN))) {
833 error = st_space(st, -1, SP_FILEMARKS, 0);
834 }
835 }
836 break;
837 case EJECT_MODE:
838 st_unmount(st, EJECT);
839 break;
840 }
841
842 scsipi_wait_drain(st->sc_link);
843
844 scsipi_adapter_delref(st->sc_link);
845 st->sc_link->flags &= ~SDEV_OPEN;
846
847 return (error);
848 }
849
850 /*
851 * Start a new mount session.
852 * Copy in all the default parameters from the selected device mode.
853 * and try guess any that seem to be defaulted.
854 */
855 int
856 st_mount_tape(dev, flags)
857 dev_t dev;
858 int flags;
859 {
860 int unit;
861 u_int dsty;
862 struct st_softc *st;
863 struct scsipi_link *sc_link;
864 int error = 0;
865
866 unit = STUNIT(dev);
867 dsty = STDSTY(dev);
868 st = st_cd.cd_devs[unit];
869 sc_link = st->sc_link;
870
871 if (st->flags & ST_MOUNTED)
872 return (0);
873
874 SC_DEBUG(sc_link, SDEV_DB1, ("mounting\n "));
875 st->flags |= ST_NEW_MOUNT;
876 st->quirks = st->drive_quirks | st->modes[dsty].quirks;
877 /*
878 * If the media is new, then make sure we give it a chance to
879 * to do a 'load' instruction. (We assume it is new.)
880 */
881 if ((error = st_load(st, LD_LOAD, XS_CTL_SILENT)) != 0)
882 return (error);
883 /*
884 * Throw another dummy instruction to catch
885 * 'Unit attention' errors. Many drives give
886 * these after doing a Load instruction (with
887 * the MEDIUM MAY HAVE CHANGED asc/ascq).
888 */
889 scsipi_test_unit_ready(sc_link, XS_CTL_SILENT); /* XXX */
890
891 /*
892 * Some devices can't tell you much until they have been
893 * asked to look at the media. This quirk does this.
894 */
895 if (st->quirks & ST_Q_SENSE_HELP)
896 if ((error = st_touch_tape(st)) != 0)
897 return (error);
898 /*
899 * Load the physical device parameters
900 * loads: blkmin, blkmax
901 */
902 if ((error = st_read_block_limits(st, 0)) != 0)
903 return (error);
904 /*
905 * Load the media dependent parameters
906 * includes: media_blksize,media_density,numblks
907 * As we have a tape in, it should be reflected here.
908 * If not you may need the "quirk" above.
909 */
910 if ((error = st_mode_sense(st, 0)) != 0)
911 return (error);
912 /*
913 * If we have gained a permanent density from somewhere,
914 * then use it in preference to the one supplied by
915 * default by the driver.
916 */
917 if (st->modeflags[dsty] & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER))
918 st->density = st->modes[dsty].density;
919 else
920 st->density = st->media_density;
921 /*
922 * If we have gained a permanent blocksize
923 * then use it in preference to the one supplied by
924 * default by the driver.
925 */
926 st->flags &= ~ST_FIXEDBLOCKS;
927 if (st->modeflags[dsty] &
928 (BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) {
929 st->blksize = st->modes[dsty].blksize;
930 if (st->blksize)
931 st->flags |= ST_FIXEDBLOCKS;
932 } else {
933 if ((error = st_decide_mode(st, FALSE)) != 0)
934 return (error);
935 }
936 if ((error = st_mode_select(st, 0)) != 0) {
937 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
938 return (error);
939 }
940 if (!(st->quirks & ST_Q_NOPREVENT)) {
941 scsipi_prevent(sc_link, PR_PREVENT,
942 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_NOT_READY);
943 }
944 st->flags &= ~ST_NEW_MOUNT;
945 st->flags |= ST_MOUNTED;
946 sc_link->flags |= SDEV_MEDIA_LOADED; /* move earlier? */
947
948 return (0);
949 }
950
951 /*
952 * End the present mount session.
953 * Rewind, and optionally eject the tape.
954 * Reset various flags to indicate that all new
955 * operations require another mount operation
956 */
957 void
958 st_unmount(st, eject)
959 struct st_softc *st;
960 boolean eject;
961 {
962 struct scsipi_link *sc_link = st->sc_link;
963 int nmarks;
964
965 if (!(st->flags & ST_MOUNTED))
966 return;
967 SC_DEBUG(sc_link, SDEV_DB1, ("unmounting\n"));
968 st_check_eod(st, FALSE, &nmarks, XS_CTL_IGNORE_NOT_READY);
969 st_rewind(st, 0, XS_CTL_IGNORE_NOT_READY);
970 scsipi_prevent(sc_link, PR_ALLOW,
971 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_NOT_READY);
972 if (eject)
973 st_load(st, LD_UNLOAD, XS_CTL_IGNORE_NOT_READY);
974 st->flags &= ~(ST_MOUNTED | ST_NEW_MOUNT);
975 sc_link->flags &= ~SDEV_MEDIA_LOADED;
976 }
977
978 /*
979 * Given all we know about the device, media, mode, 'quirks' and
980 * initial operation, make a decision as to how we should be set
981 * to run (regarding blocking and EOD marks)
982 */
983 int
984 st_decide_mode(st, first_read)
985 struct st_softc *st;
986 boolean first_read;
987 {
988 #ifdef SCSIDEBUG
989 struct scsipi_link *sc_link = st->sc_link;
990 #endif
991
992 SC_DEBUG(sc_link, SDEV_DB2, ("starting block mode decision\n"));
993
994 /*
995 * If the drive can only handle fixed-length blocks and only at
996 * one size, perhaps we should just do that.
997 */
998 if (st->blkmin && (st->blkmin == st->blkmax)) {
999 st->flags |= ST_FIXEDBLOCKS;
1000 st->blksize = st->blkmin;
1001 SC_DEBUG(sc_link, SDEV_DB3,
1002 ("blkmin == blkmax of %d\n", st->blkmin));
1003 goto done;
1004 }
1005 /*
1006 * If the tape density mandates (or even suggests) use of fixed
1007 * or variable-length blocks, comply.
1008 */
1009 switch (st->density) {
1010 case HALFINCH_800:
1011 case HALFINCH_1600:
1012 case HALFINCH_6250:
1013 case DDS:
1014 st->flags &= ~ST_FIXEDBLOCKS;
1015 st->blksize = 0;
1016 SC_DEBUG(sc_link, SDEV_DB3, ("density specified variable\n"));
1017 goto done;
1018 case QIC_11:
1019 case QIC_24:
1020 case QIC_120:
1021 case QIC_150:
1022 case QIC_525:
1023 case QIC_1320:
1024 case QIC_3095:
1025 case QIC_3220:
1026 st->flags |= ST_FIXEDBLOCKS;
1027 if (st->media_blksize > 0)
1028 st->blksize = st->media_blksize;
1029 else
1030 st->blksize = DEF_FIXED_BSIZE;
1031 SC_DEBUG(sc_link, SDEV_DB3, ("density specified fixed\n"));
1032 goto done;
1033 }
1034 /*
1035 * If we're about to read the tape, perhaps we should choose
1036 * fixed or variable-length blocks and block size according to
1037 * what the drive found on the tape.
1038 */
1039 if (first_read &&
1040 (!(st->quirks & ST_Q_BLKSIZE) || (st->media_blksize == 0) ||
1041 (st->media_blksize == DEF_FIXED_BSIZE) ||
1042 (st->media_blksize == 1024))) {
1043 if (st->media_blksize > 0)
1044 st->flags |= ST_FIXEDBLOCKS;
1045 else
1046 st->flags &= ~ST_FIXEDBLOCKS;
1047 st->blksize = st->media_blksize;
1048 SC_DEBUG(sc_link, SDEV_DB3,
1049 ("Used media_blksize of %d\n", st->media_blksize));
1050 goto done;
1051 }
1052 /*
1053 * We're getting no hints from any direction. Choose variable-
1054 * length blocks arbitrarily.
1055 */
1056 st->flags &= ~ST_FIXEDBLOCKS;
1057 st->blksize = 0;
1058 SC_DEBUG(sc_link, SDEV_DB3,
1059 ("Give up and default to variable mode\n"));
1060
1061 done:
1062 /*
1063 * Decide whether or not to write two file marks to signify end-
1064 * of-data. Make the decision as a function of density. If
1065 * the decision is not to use a second file mark, the SCSI BLANK
1066 * CHECK condition code will be recognized as end-of-data when
1067 * first read.
1068 * (I think this should be a by-product of fixed/variable..julian)
1069 */
1070 switch (st->density) {
1071 /* case 8 mm: What is the SCSI density code for 8 mm, anyway? */
1072 case QIC_11:
1073 case QIC_24:
1074 case QIC_120:
1075 case QIC_150:
1076 case QIC_525:
1077 case QIC_1320:
1078 case QIC_3095:
1079 case QIC_3220:
1080 st->flags &= ~ST_2FM_AT_EOD;
1081 break;
1082 default:
1083 st->flags |= ST_2FM_AT_EOD;
1084 }
1085 return (0);
1086 }
1087
1088 /*
1089 * Actually translate the requested transfer into
1090 * one the physical driver can understand
1091 * The transfer is described by a buf and will include
1092 * only one physical transfer.
1093 */
1094 void
1095 ststrategy(bp)
1096 struct buf *bp;
1097 {
1098 struct st_softc *st = st_cd.cd_devs[STUNIT(bp->b_dev)];
1099 int s;
1100
1101 SC_DEBUG(st->sc_link, SDEV_DB1,
1102 ("ststrategy %ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
1103 /*
1104 * If it's a null transfer, return immediatly
1105 */
1106 if (bp->b_bcount == 0)
1107 goto done;
1108
1109 /* If offset is negative, error */
1110 if (bp->b_blkno < 0) {
1111 bp->b_error = EINVAL;
1112 goto bad;
1113 }
1114
1115 /*
1116 * Odd sized request on fixed drives are verboten
1117 */
1118 if (st->flags & ST_FIXEDBLOCKS) {
1119 if (bp->b_bcount % st->blksize) {
1120 printf("%s: bad request, must be multiple of %d\n",
1121 st->sc_dev.dv_xname, st->blksize);
1122 bp->b_error = EIO;
1123 goto bad;
1124 }
1125 }
1126 /*
1127 * as are out-of-range requests on variable drives.
1128 */
1129 else if (bp->b_bcount < st->blkmin ||
1130 (st->blkmax && bp->b_bcount > st->blkmax)) {
1131 printf("%s: bad request, must be between %d and %d\n",
1132 st->sc_dev.dv_xname, st->blkmin, st->blkmax);
1133 bp->b_error = EIO;
1134 goto bad;
1135 }
1136 s = splbio();
1137
1138 /*
1139 * Place it in the queue of activities for this tape
1140 * at the end (a bit silly because we only have on user..
1141 * (but it could fork()))
1142 */
1143 BUFQ_INSERT_TAIL(&st->buf_queue, bp);
1144
1145 /*
1146 * Tell the device to get going on the transfer if it's
1147 * not doing anything, otherwise just wait for completion
1148 * (All a bit silly if we're only allowing 1 open but..)
1149 */
1150 ststart(st);
1151
1152 splx(s);
1153 return;
1154 bad:
1155 bp->b_flags |= B_ERROR;
1156 done:
1157 /*
1158 * Correctly set the buf to indicate a completed xfer
1159 */
1160 bp->b_resid = bp->b_bcount;
1161 biodone(bp);
1162 return;
1163 }
1164
1165 /*
1166 * ststart looks to see if there is a buf waiting for the device
1167 * and that the device is not already busy. If both are true,
1168 * It dequeues the buf and creates a scsi command to perform the
1169 * transfer required. The transfer request will call scsipi_done
1170 * on completion, which will in turn call this routine again
1171 * so that the next queued transfer is performed.
1172 * The bufs are queued by the strategy routine (ststrategy)
1173 *
1174 * This routine is also called after other non-queued requests
1175 * have been made of the scsi driver, to ensure that the queue
1176 * continues to be drained.
1177 * ststart() is called at splbio
1178 */
1179 void
1180 ststart(v)
1181 void *v;
1182 {
1183 struct st_softc *st = v;
1184 struct scsipi_link *sc_link = st->sc_link;
1185 struct buf *bp;
1186 struct scsi_rw_tape cmd;
1187 int flags, error;
1188
1189 SC_DEBUG(sc_link, SDEV_DB2, ("ststart "));
1190 /*
1191 * See if there is a buf to do and we are not already
1192 * doing one
1193 */
1194 while (sc_link->active < sc_link->openings) {
1195 /* if a special awaits, let it proceed first */
1196 if (sc_link->flags & SDEV_WAITING) {
1197 sc_link->flags &= ~SDEV_WAITING;
1198 wakeup((caddr_t)sc_link);
1199 return;
1200 }
1201
1202 if ((bp = BUFQ_FIRST(&st->buf_queue)) == NULL)
1203 return;
1204 BUFQ_REMOVE(&st->buf_queue, bp);
1205
1206 /*
1207 * If the device has been unmounted by the user
1208 * then throw away all requests until done.
1209 */
1210 if (!(st->flags & ST_MOUNTED) ||
1211 !(sc_link->flags & SDEV_MEDIA_LOADED)) {
1212 /* make sure that one implies the other.. */
1213 sc_link->flags &= ~SDEV_MEDIA_LOADED;
1214 bp->b_flags |= B_ERROR;
1215 bp->b_error = EIO;
1216 bp->b_resid = bp->b_bcount;
1217 biodone(bp);
1218 continue;
1219 }
1220 /*
1221 * only FIXEDBLOCK devices have pending I/O or space operations.
1222 */
1223 if (st->flags & ST_FIXEDBLOCKS) {
1224 /*
1225 * If we are at a filemark but have not reported it yet
1226 * then we should report it now
1227 */
1228 if (st->flags & ST_AT_FILEMARK) {
1229 if ((bp->b_flags & B_READ) == B_WRITE) {
1230 /*
1231 * Handling of ST_AT_FILEMARK in
1232 * st_space will fill in the right file
1233 * mark count.
1234 * Back up over filemark
1235 */
1236 if (st_space(st, 0, SP_FILEMARKS, 0)) {
1237 bp->b_flags |= B_ERROR;
1238 bp->b_error = EIO;
1239 biodone(bp);
1240 continue;
1241 }
1242 } else {
1243 bp->b_resid = bp->b_bcount;
1244 bp->b_error = 0;
1245 bp->b_flags &= ~B_ERROR;
1246 st->flags &= ~ST_AT_FILEMARK;
1247 biodone(bp);
1248 continue; /* seek more work */
1249 }
1250 }
1251 }
1252 /*
1253 * If we are at EOM but have not reported it
1254 * yet then we should report it now.
1255 */
1256 if (st->flags & (ST_EOM_PENDING|ST_EIO_PENDING)) {
1257 bp->b_resid = bp->b_bcount;
1258 if (st->flags & ST_EIO_PENDING) {
1259 bp->b_error = EIO;
1260 bp->b_flags |= B_ERROR;
1261 }
1262 st->flags &= ~(ST_EOM_PENDING|ST_EIO_PENDING);
1263 biodone(bp);
1264 continue; /* seek more work */
1265 }
1266
1267 /*
1268 * Fill out the scsi command
1269 */
1270 bzero(&cmd, sizeof(cmd));
1271 if ((bp->b_flags & B_READ) == B_WRITE) {
1272 cmd.opcode = WRITE;
1273 st->flags &= ~ST_FM_WRITTEN;
1274 flags = XS_CTL_DATA_OUT;
1275 } else {
1276 cmd.opcode = READ;
1277 flags = XS_CTL_DATA_IN;
1278 }
1279
1280 /*
1281 * Handle "fixed-block-mode" tape drives by using the
1282 * block count instead of the length.
1283 */
1284 if (st->flags & ST_FIXEDBLOCKS) {
1285 cmd.byte2 |= SRW_FIXED;
1286 _lto3b(bp->b_bcount / st->blksize, cmd.len);
1287 } else
1288 _lto3b(bp->b_bcount, cmd.len);
1289
1290 /*
1291 * go ask the adapter to do all this for us
1292 * XXX Really need NOSLEEP?
1293 */
1294 error = scsipi_command(sc_link,
1295 (struct scsipi_generic *)&cmd, sizeof(cmd),
1296 (u_char *)bp->b_data, bp->b_bcount,
1297 0, ST_IO_TIME, bp, flags | XS_CTL_NOSLEEP | XS_CTL_ASYNC);
1298 if (error) {
1299 printf("%s: not queued, error %d\n",
1300 st->sc_dev.dv_xname, error);
1301 }
1302 } /* go back and see if we can cram more work in.. */
1303 }
1304
1305 void
1306 stdone(xs)
1307 struct scsipi_xfer *xs;
1308 {
1309
1310 if (xs->bp != NULL) {
1311 struct st_softc *st = xs->sc_link->device_softc;
1312 if ((xs->bp->b_flags & B_READ) == B_WRITE) {
1313 st->flags |= ST_WRITTEN;
1314 } else {
1315 st->flags &= ~ST_WRITTEN;
1316 }
1317 #if NRND > 0
1318 rnd_add_uint32(&st->rnd_source, xs->bp->b_blkno);
1319 #endif
1320 }
1321 }
1322
1323 int
1324 stread(dev, uio, iomode)
1325 dev_t dev;
1326 struct uio *uio;
1327 int iomode;
1328 {
1329 struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
1330
1331 return (physio(ststrategy, NULL, dev, B_READ,
1332 st->sc_link->adapter->scsipi_minphys, uio));
1333 }
1334
1335 int
1336 stwrite(dev, uio, iomode)
1337 dev_t dev;
1338 struct uio *uio;
1339 int iomode;
1340 {
1341 struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
1342
1343 return (physio(ststrategy, NULL, dev, B_WRITE,
1344 st->sc_link->adapter->scsipi_minphys, uio));
1345 }
1346
1347 /*
1348 * Perform special action on behalf of the user;
1349 * knows about the internals of this device
1350 */
1351 int
1352 stioctl(dev, cmd, arg, flag, p)
1353 dev_t dev;
1354 u_long cmd;
1355 caddr_t arg;
1356 int flag;
1357 struct proc *p;
1358 {
1359 int error = 0;
1360 int unit;
1361 int number, nmarks, dsty;
1362 int flags;
1363 struct st_softc *st;
1364 int hold_blksize;
1365 u_int8_t hold_density;
1366 struct mtop *mt = (struct mtop *) arg;
1367
1368 /*
1369 * Find the device that the user is talking about
1370 */
1371 flags = 0; /* give error messages, act on errors etc. */
1372 unit = STUNIT(dev);
1373 dsty = STDSTY(dev);
1374 st = st_cd.cd_devs[unit];
1375 hold_blksize = st->blksize;
1376 hold_density = st->density;
1377
1378 switch ((u_int) cmd) {
1379
1380 case MTIOCGET: {
1381 struct mtget *g = (struct mtget *) arg;
1382 /*
1383 * (to get the current state of READONLY)
1384 */
1385 error = st_mode_sense(st, XS_CTL_SILENT);
1386 if (error)
1387 break;
1388 SC_DEBUG(st->sc_link, SDEV_DB1, ("[ioctl: get status]\n"));
1389 bzero(g, sizeof(struct mtget));
1390 g->mt_type = 0x7; /* Ultrix compat *//*? */
1391 g->mt_blksiz = st->blksize;
1392 g->mt_density = st->density;
1393 g->mt_mblksiz[0] = st->modes[0].blksize;
1394 g->mt_mblksiz[1] = st->modes[1].blksize;
1395 g->mt_mblksiz[2] = st->modes[2].blksize;
1396 g->mt_mblksiz[3] = st->modes[3].blksize;
1397 g->mt_mdensity[0] = st->modes[0].density;
1398 g->mt_mdensity[1] = st->modes[1].density;
1399 g->mt_mdensity[2] = st->modes[2].density;
1400 g->mt_mdensity[3] = st->modes[3].density;
1401 if (st->flags & ST_READONLY)
1402 g->mt_dsreg |= MT_DS_RDONLY;
1403 if (st->flags & ST_MOUNTED)
1404 g->mt_dsreg |= MT_DS_MOUNTED;
1405 g->mt_resid = st->mt_resid;
1406 g->mt_erreg = st->mt_erreg;
1407 /*
1408 * clear latched errors.
1409 */
1410 st->mt_resid = 0;
1411 st->mt_erreg = 0;
1412 st->asc = 0;
1413 st->ascq = 0;
1414 break;
1415 }
1416 case MTIOCTOP: {
1417
1418 SC_DEBUG(st->sc_link, SDEV_DB1,
1419 ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op,
1420 mt->mt_count));
1421
1422 /* compat: in U*x it is a short */
1423 number = mt->mt_count;
1424 switch ((short) (mt->mt_op)) {
1425 case MTWEOF: /* write an end-of-file record */
1426 error = st_write_filemarks(st, number, flags);
1427 break;
1428 case MTBSF: /* backward space file */
1429 number = -number;
1430 case MTFSF: /* forward space file */
1431 error = st_check_eod(st, FALSE, &nmarks, flags);
1432 if (!error)
1433 error = st_space(st, number - nmarks,
1434 SP_FILEMARKS, flags);
1435 break;
1436 case MTBSR: /* backward space record */
1437 number = -number;
1438 case MTFSR: /* forward space record */
1439 error = st_check_eod(st, TRUE, &nmarks, flags);
1440 if (!error)
1441 error = st_space(st, number, SP_BLKS, flags);
1442 break;
1443 case MTREW: /* rewind */
1444 error = st_rewind(st, 0, flags);
1445 break;
1446 case MTOFFL: /* rewind and put the drive offline */
1447 st_unmount(st, EJECT);
1448 break;
1449 case MTNOP: /* no operation, sets status only */
1450 break;
1451 case MTRETEN: /* retension the tape */
1452 error = st_load(st, LD_RETENSION, flags);
1453 if (!error)
1454 error = st_load(st, LD_LOAD, flags);
1455 break;
1456 case MTEOM: /* forward space to end of media */
1457 error = st_check_eod(st, FALSE, &nmarks, flags);
1458 if (!error)
1459 error = st_space(st, 1, SP_EOM, flags);
1460 break;
1461 case MTCACHE: /* enable controller cache */
1462 st->flags &= ~ST_DONTBUFFER;
1463 goto try_new_value;
1464 case MTNOCACHE: /* disable controller cache */
1465 st->flags |= ST_DONTBUFFER;
1466 goto try_new_value;
1467 case MTERASE: /* erase volume */
1468 error = st_erase(st, number, flags);
1469 break;
1470 case MTSETBSIZ: /* Set block size for device */
1471 #ifdef NOTYET
1472 if (!(st->flags & ST_NEW_MOUNT)) {
1473 uprintf("re-mount tape before changing blocksize");
1474 error = EINVAL;
1475 break;
1476 }
1477 #endif
1478 if (number == 0)
1479 st->flags &= ~ST_FIXEDBLOCKS;
1480 else {
1481 if ((st->blkmin || st->blkmax) &&
1482 (number < st->blkmin ||
1483 number > st->blkmax)) {
1484 error = EINVAL;
1485 break;
1486 }
1487 st->flags |= ST_FIXEDBLOCKS;
1488 }
1489 st->blksize = number;
1490 st->flags |= ST_BLOCK_SET; /*XXX */
1491 goto try_new_value;
1492
1493 case MTSETDNSTY: /* Set density for device and mode */
1494 /*
1495 * Any number >= 0 and <= 0xff is legal. Numbers
1496 * above 0x80 are 'vendor unique'.
1497 */
1498 if (number < 0 || number > 255) {
1499 error = EINVAL;
1500 break;
1501 } else
1502 st->density = number;
1503 goto try_new_value;
1504
1505 case MTCMPRESS:
1506 error = st_cmprss(st, number);
1507 break;
1508
1509 case MTEWARN:
1510 if (number)
1511 st->flags |= ST_EARLYWARN;
1512 else
1513 st->flags &= ~ST_EARLYWARN;
1514 break;
1515
1516 default:
1517 error = EINVAL;
1518 }
1519 break;
1520 }
1521 case MTIOCIEOT:
1522 case MTIOCEEOT:
1523 break;
1524
1525 case MTIOCRDSPOS:
1526 error = st_rdpos(st, 0, (u_int32_t *) arg);
1527 break;
1528
1529 case MTIOCRDHPOS:
1530 error = st_rdpos(st, 1, (u_int32_t *) arg);
1531 break;
1532
1533 case MTIOCSLOCATE:
1534 error = st_setpos(st, 0, (u_int32_t *) arg);
1535 break;
1536
1537 case MTIOCHLOCATE:
1538 error = st_setpos(st, 1, (u_int32_t *) arg);
1539 break;
1540
1541
1542 default:
1543 error = scsipi_do_ioctl(st->sc_link, dev, cmd, arg,
1544 flag, p);
1545 break;
1546 }
1547 return (error);
1548 /*-----------------------------*/
1549 try_new_value:
1550 /*
1551 * Check that the mode being asked for is aggreeable to the
1552 * drive. If not, put it back the way it was.
1553 */
1554 if ((error = st_mode_select(st, 0)) != 0) {/* put it back as it was */
1555 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
1556 st->density = hold_density;
1557 st->blksize = hold_blksize;
1558 if (st->blksize)
1559 st->flags |= ST_FIXEDBLOCKS;
1560 else
1561 st->flags &= ~ST_FIXEDBLOCKS;
1562 return (error);
1563 }
1564 /*
1565 * As the drive liked it, if we are setting a new default,
1566 * set it into the structures as such.
1567 *
1568 * The means for deciding this are not finalised yet- but
1569 * if the device was opened in Control Mode, the values
1570 * are persistent now across mounts.
1571 */
1572 if (STMODE(dev) == CTRL_MODE) {
1573 switch ((short) (mt->mt_op)) {
1574 case MTSETBSIZ:
1575 st->modes[dsty].blksize = st->blksize;
1576 st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
1577 break;
1578 case MTSETDNSTY:
1579 st->modes[dsty].density = st->density;
1580 st->modeflags[dsty] |= DENSITY_SET_BY_USER;
1581 break;
1582 }
1583 }
1584 return (0);
1585 }
1586
1587 /*
1588 * Do a synchronous read.
1589 */
1590 int
1591 st_read(st, buf, size, flags)
1592 struct st_softc *st;
1593 int size;
1594 int flags;
1595 char *buf;
1596 {
1597 struct scsi_rw_tape cmd;
1598
1599 /*
1600 * If it's a null transfer, return immediatly
1601 */
1602 if (size == 0)
1603 return (0);
1604 bzero(&cmd, sizeof(cmd));
1605 cmd.opcode = READ;
1606 if (st->flags & ST_FIXEDBLOCKS) {
1607 cmd.byte2 |= SRW_FIXED;
1608 _lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE),
1609 cmd.len);
1610 } else
1611 _lto3b(size, cmd.len);
1612 return (scsipi_command(st->sc_link,
1613 (struct scsipi_generic *)&cmd, sizeof(cmd),
1614 (u_char *)buf, size, 0, ST_IO_TIME, NULL, flags | XS_CTL_DATA_IN));
1615 }
1616
1617 /*
1618 * Ask the drive what it's min and max blk sizes are.
1619 */
1620 int
1621 st_read_block_limits(st, flags)
1622 struct st_softc *st;
1623 int flags;
1624 {
1625 struct scsi_block_limits cmd;
1626 struct scsi_block_limits_data block_limits;
1627 struct scsipi_link *sc_link = st->sc_link;
1628 int error;
1629
1630 /*
1631 * First check if we have it all loaded
1632 */
1633 if ((sc_link->flags & SDEV_MEDIA_LOADED))
1634 return (0);
1635
1636 /*
1637 * do a 'Read Block Limits'
1638 */
1639 bzero(&cmd, sizeof(cmd));
1640 cmd.opcode = READ_BLOCK_LIMITS;
1641
1642 /*
1643 * do the command, update the global values
1644 */
1645 error = scsipi_command(sc_link, (struct scsipi_generic *)&cmd,
1646 sizeof(cmd), (u_char *)&block_limits, sizeof(block_limits),
1647 ST_RETRIES, ST_CTL_TIME, NULL,
1648 flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
1649 if (error)
1650 return (error);
1651
1652 st->blkmin = _2btol(block_limits.min_length);
1653 st->blkmax = _3btol(block_limits.max_length);
1654
1655 SC_DEBUG(sc_link, SDEV_DB3,
1656 ("(%d <= blksize <= %d)\n", st->blkmin, st->blkmax));
1657 return (0);
1658 }
1659
1660 /*
1661 * Get the scsi driver to send a full inquiry to the
1662 * device and use the results to fill out the global
1663 * parameter structure.
1664 *
1665 * called from:
1666 * attach
1667 * open
1668 * ioctl (to reset original blksize)
1669 */
1670 int
1671 st_mode_sense(st, flags)
1672 struct st_softc *st;
1673 int flags;
1674 {
1675 u_int scsipi_sense_len;
1676 int error;
1677 struct scsi_mode_sense cmd;
1678 struct scsipi_sense {
1679 struct scsi_mode_header header;
1680 struct scsi_blk_desc blk_desc;
1681 u_char sense_data[MAX_PAGE_0_SIZE];
1682 } scsipi_sense;
1683 struct scsipi_link *sc_link = st->sc_link;
1684
1685 scsipi_sense_len = 12 + st->page_0_size;
1686
1687 /*
1688 * Set up a mode sense
1689 */
1690 bzero(&cmd, sizeof(cmd));
1691 cmd.opcode = SCSI_MODE_SENSE;
1692 cmd.length = scsipi_sense_len;
1693
1694 /*
1695 * do the command, but we don't need the results
1696 * just print them for our interest's sake, if asked,
1697 * or if we need it as a template for the mode select
1698 * store it away.
1699 */
1700 error = scsipi_command(sc_link, (struct scsipi_generic *)&cmd,
1701 sizeof(cmd), (u_char *)&scsipi_sense, scsipi_sense_len,
1702 ST_RETRIES, ST_CTL_TIME, NULL,
1703 flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
1704 if (error)
1705 return (error);
1706
1707 st->numblks = _3btol(scsipi_sense.blk_desc.nblocks);
1708 st->media_blksize = _3btol(scsipi_sense.blk_desc.blklen);
1709 st->media_density = scsipi_sense.blk_desc.density;
1710 if (scsipi_sense.header.dev_spec & SMH_DSP_WRITE_PROT)
1711 st->flags |= ST_READONLY;
1712 else
1713 st->flags &= ~ST_READONLY;
1714 SC_DEBUG(sc_link, SDEV_DB3,
1715 ("density code %d, %d-byte blocks, write-%s, ",
1716 st->media_density, st->media_blksize,
1717 st->flags & ST_READONLY ? "protected" : "enabled"));
1718 SC_DEBUG(sc_link, SDEV_DB3,
1719 ("%sbuffered\n",
1720 scsipi_sense.header.dev_spec & SMH_DSP_BUFF_MODE ? "" : "un"));
1721 if (st->page_0_size)
1722 bcopy(scsipi_sense.sense_data, st->sense_data,
1723 st->page_0_size);
1724 sc_link->flags |= SDEV_MEDIA_LOADED;
1725 return (0);
1726 }
1727
1728 /*
1729 * Send a filled out parameter structure to the drive to
1730 * set it into the desire modes etc.
1731 */
1732 int
1733 st_mode_select(st, flags)
1734 struct st_softc *st;
1735 int flags;
1736 {
1737 u_int scsi_select_len;
1738 struct scsi_mode_select cmd;
1739 struct scsi_select {
1740 struct scsi_mode_header header;
1741 struct scsi_blk_desc blk_desc;
1742 u_char sense_data[MAX_PAGE_0_SIZE];
1743 } scsi_select;
1744 struct scsipi_link *sc_link = st->sc_link;
1745
1746 scsi_select_len = 12 + st->page_0_size;
1747
1748 /*
1749 * This quirk deals with drives that have only one valid mode
1750 * and think this gives them license to reject all mode selects,
1751 * even if the selected mode is the one that is supported.
1752 */
1753 if (st->quirks & ST_Q_UNIMODAL) {
1754 SC_DEBUG(sc_link, SDEV_DB3,
1755 ("not setting density 0x%x blksize 0x%x\n",
1756 st->density, st->blksize));
1757 return (0);
1758 }
1759
1760 /*
1761 * Set up for a mode select
1762 */
1763 bzero(&cmd, sizeof(cmd));
1764 cmd.opcode = SCSI_MODE_SELECT;
1765 cmd.length = scsi_select_len;
1766
1767 bzero(&scsi_select, scsi_select_len);
1768 scsi_select.header.blk_desc_len = sizeof(struct scsi_blk_desc);
1769 scsi_select.header.dev_spec &= ~SMH_DSP_BUFF_MODE;
1770 scsi_select.blk_desc.density = st->density;
1771 if (st->flags & ST_DONTBUFFER)
1772 scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_OFF;
1773 else
1774 scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_ON;
1775 if (st->flags & ST_FIXEDBLOCKS)
1776 _lto3b(st->blksize, scsi_select.blk_desc.blklen);
1777 if (st->page_0_size)
1778 bcopy(st->sense_data, scsi_select.sense_data, st->page_0_size);
1779
1780 /*
1781 * do the command
1782 */
1783 return (scsipi_command(sc_link, (struct scsipi_generic *)&cmd,
1784 sizeof(cmd), (u_char *)&scsi_select, scsi_select_len,
1785 ST_RETRIES, ST_CTL_TIME, NULL,
1786 flags | XS_CTL_DATA_OUT | XS_CTL_DATA_ONSTACK));
1787 }
1788
1789 int
1790 st_cmprss(st, onoff)
1791 struct st_softc *st;
1792 int onoff;
1793 {
1794 u_int scsi_dlen;
1795 struct scsi_mode_select mcmd;
1796 struct scsi_mode_sense scmd;
1797 struct scsi_select {
1798 struct scsi_mode_header header;
1799 struct scsi_blk_desc blk_desc;
1800 u_char pdata[max(sizeof(struct scsi_tape_dev_conf_page),
1801 sizeof(struct scsi_tape_dev_compression_page))];
1802 } scsi_pdata;
1803 struct scsi_tape_dev_conf_page *ptr;
1804 struct scsi_tape_dev_compression_page *cptr;
1805 struct scsipi_link *sc_link = st->sc_link;
1806 int error, ison, flags;
1807
1808 scsi_dlen = sizeof(scsi_pdata);
1809 bzero(&scsi_pdata, scsi_dlen);
1810
1811 /*
1812 * Set up for a mode sense.
1813 * Do DATA COMPRESSION page first.
1814 */
1815 bzero(&scmd, sizeof(scmd));
1816 scmd.opcode = SCSI_MODE_SENSE;
1817 scmd.page = SMS_PAGE_CTRL_CURRENT | 0xf;
1818 scmd.length = scsi_dlen;
1819
1820 flags = XS_CTL_SILENT;
1821
1822 /*
1823 * Do the MODE SENSE command...
1824 */
1825 again:
1826 bzero(&scsi_pdata, scsi_dlen);
1827 error = scsipi_command(sc_link,
1828 (struct scsipi_generic *)&scmd, sizeof(scmd),
1829 (u_char *)&scsi_pdata, scsi_dlen,
1830 ST_RETRIES, ST_CTL_TIME, NULL,
1831 flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
1832
1833 if (error) {
1834 if (scmd.byte2 != SMS_DBD) {
1835 scmd.byte2 = SMS_DBD;
1836 goto again;
1837 }
1838 /*
1839 * Try a different page?
1840 */
1841 if (scmd.page == (SMS_PAGE_CTRL_CURRENT | 0xf)) {
1842 scmd.page = SMS_PAGE_CTRL_CURRENT | 0x10;
1843 scmd.byte2 = 0;
1844 goto again;
1845 }
1846 return (error);
1847 }
1848
1849 if (scsi_pdata.header.blk_desc_len)
1850 ptr = (struct scsi_tape_dev_conf_page *) scsi_pdata.pdata;
1851 else
1852 ptr = (struct scsi_tape_dev_conf_page *) &scsi_pdata.blk_desc;
1853
1854 if ((scmd.page & SMS_PAGE_CODE) == 0xf) {
1855 cptr = (struct scsi_tape_dev_compression_page *) ptr;
1856 ison = (cptr->dce_dcc & DCP_DCE) != 0;
1857 if (onoff)
1858 cptr->dce_dcc |= DCP_DCE;
1859 else
1860 cptr->dce_dcc &= ~DCP_DCE;
1861 cptr->pagecode &= ~0x80;
1862 } else {
1863 ison = (ptr->sel_comp_alg != 0);
1864 if (onoff)
1865 ptr->sel_comp_alg = 1;
1866 else
1867 ptr->sel_comp_alg = 0;
1868 ptr->pagecode &= ~0x80;
1869 ptr->byte2 = 0;
1870 ptr->active_partition = 0;
1871 ptr->wb_full_ratio = 0;
1872 ptr->rb_empty_ratio = 0;
1873 ptr->byte8 &= ~0x30;
1874 ptr->gap_size = 0;
1875 ptr->byte10 &= ~0xe7;
1876 ptr->ew_bufsize[0] = 0;
1877 ptr->ew_bufsize[1] = 0;
1878 ptr->ew_bufsize[2] = 0;
1879 ptr->reserved = 0;
1880 }
1881 onoff = onoff ? 1 : 0;
1882 /*
1883 * There might be a virtue in actually doing the MODE SELECTS,
1884 * but let's not clog the bus over it.
1885 */
1886 if (onoff == ison)
1887 return (0);
1888
1889 /*
1890 * Set up for a mode select
1891 */
1892
1893 scsi_pdata.header.data_length = 0;
1894 scsi_pdata.header.medium_type = 0;
1895 if ((st->flags & ST_DONTBUFFER) == 0)
1896 scsi_pdata.header.dev_spec = SMH_DSP_BUFF_MODE_ON;
1897 else
1898 scsi_pdata.header.dev_spec = 0;
1899
1900 bzero(&mcmd, sizeof(mcmd));
1901 mcmd.opcode = SCSI_MODE_SELECT;
1902 mcmd.byte2 = SMS_PF;
1903 mcmd.length = scsi_dlen;
1904 if (scsi_pdata.header.blk_desc_len) {
1905 scsi_pdata.blk_desc.density = 0;
1906 scsi_pdata.blk_desc.nblocks[0] = 0;
1907 scsi_pdata.blk_desc.nblocks[1] = 0;
1908 scsi_pdata.blk_desc.nblocks[2] = 0;
1909 }
1910
1911 /*
1912 * Do the command
1913 */
1914 error = scsipi_command(sc_link,
1915 (struct scsipi_generic *)&mcmd, sizeof(mcmd),
1916 (u_char *)&scsi_pdata, scsi_dlen,
1917 ST_RETRIES, ST_CTL_TIME, NULL,
1918 flags | XS_CTL_DATA_OUT | XS_CTL_DATA_ONSTACK);
1919
1920 if (error && (scmd.page & SMS_PAGE_CODE) == 0xf) {
1921 /*
1922 * Try DEVICE CONFIGURATION page.
1923 */
1924 scmd.page = SMS_PAGE_CTRL_CURRENT | 0x10;
1925 goto again;
1926 }
1927 return (error);
1928 }
1929 /*
1930 * issue an erase command
1931 */
1932 int
1933 st_erase(st, full, flags)
1934 struct st_softc *st;
1935 int full, flags;
1936 {
1937 int tmo;
1938 struct scsi_erase cmd;
1939
1940 /*
1941 * Full erase means set LONG bit in erase command, which asks
1942 * the drive to erase the entire unit. Without this bit, we're
1943 * asking the drive to write an erase gap.
1944 */
1945 bzero(&cmd, sizeof(cmd));
1946 cmd.opcode = ERASE;
1947 if (full) {
1948 cmd.byte2 = SE_IMMED|SE_LONG;
1949 tmo = ST_SPC_TIME;
1950 } else {
1951 tmo = ST_IO_TIME;
1952 cmd.byte2 = SE_IMMED;
1953 }
1954
1955 /*
1956 * XXX We always do this asynchronously, for now. How long should
1957 * we wait if we want to (eventually) to it synchronously?
1958 */
1959 return (scsipi_command(st->sc_link,
1960 (struct scsipi_generic *)&cmd, sizeof(cmd),
1961 0, 0, ST_RETRIES, tmo, NULL, flags));
1962 }
1963
1964 /*
1965 * skip N blocks/filemarks/seq filemarks/eom
1966 */
1967 int
1968 st_space(st, number, what, flags)
1969 struct st_softc *st;
1970 u_int what;
1971 int flags;
1972 int number;
1973 {
1974 struct scsi_space cmd;
1975 int error;
1976
1977 switch (what) {
1978 case SP_BLKS:
1979 if (st->flags & ST_PER_ACTION) {
1980 if (number > 0) {
1981 st->flags &= ~ST_PER_ACTION;
1982 return (EIO);
1983 } else if (number < 0) {
1984 if (st->flags & ST_AT_FILEMARK) {
1985 /*
1986 * Handling of ST_AT_FILEMARK
1987 * in st_space will fill in the
1988 * right file mark count.
1989 */
1990 error = st_space(st, 0, SP_FILEMARKS,
1991 flags);
1992 if (error)
1993 return (error);
1994 }
1995 if (st->flags & ST_BLANK_READ) {
1996 st->flags &= ~ST_BLANK_READ;
1997 return (EIO);
1998 }
1999 st->flags &= ~(ST_EIO_PENDING|ST_EOM_PENDING);
2000 }
2001 }
2002 break;
2003 case SP_FILEMARKS:
2004 if (st->flags & ST_EIO_PENDING) {
2005 if (number > 0) {
2006 /* pretend we just discovered the error */
2007 st->flags &= ~ST_EIO_PENDING;
2008 return (EIO);
2009 } else if (number < 0) {
2010 /* back away from the error */
2011 st->flags &= ~ST_EIO_PENDING;
2012 }
2013 }
2014 if (st->flags & ST_AT_FILEMARK) {
2015 st->flags &= ~ST_AT_FILEMARK;
2016 number--;
2017 }
2018 if ((st->flags & ST_BLANK_READ) && (number < 0)) {
2019 /* back away from unwritten tape */
2020 st->flags &= ~ST_BLANK_READ;
2021 number++; /* XXX dubious */
2022 }
2023 break;
2024 case SP_EOM:
2025 if (st->flags & ST_EOM_PENDING) {
2026 /* we're already there */
2027 st->flags &= ~ST_EOM_PENDING;
2028 return (0);
2029 }
2030 if (st->flags & ST_EIO_PENDING) {
2031 /* pretend we just discovered the error */
2032 st->flags &= ~ST_EIO_PENDING;
2033 return (EIO);
2034 }
2035 if (st->flags & ST_AT_FILEMARK)
2036 st->flags &= ~ST_AT_FILEMARK;
2037 break;
2038 }
2039 if (number == 0)
2040 return (0);
2041
2042 bzero(&cmd, sizeof(cmd));
2043 cmd.opcode = SPACE;
2044 cmd.byte2 = what;
2045 _lto3b(number, cmd.number);
2046
2047 return (scsipi_command(st->sc_link,
2048 (struct scsipi_generic *)&cmd, sizeof(cmd),
2049 0, 0, 0, ST_SPC_TIME, NULL, flags));
2050 }
2051
2052 /*
2053 * write N filemarks
2054 */
2055 int
2056 st_write_filemarks(st, number, flags)
2057 struct st_softc *st;
2058 int flags;
2059 int number;
2060 {
2061 struct scsi_write_filemarks cmd;
2062
2063 /*
2064 * It's hard to write a negative number of file marks.
2065 * Don't try.
2066 */
2067 if (number < 0)
2068 return (EINVAL);
2069 switch (number) {
2070 case 0: /* really a command to sync the drive's buffers */
2071 break;
2072 case 1:
2073 if (st->flags & ST_FM_WRITTEN) /* already have one down */
2074 st->flags &= ~ST_WRITTEN;
2075 else
2076 st->flags |= ST_FM_WRITTEN;
2077 st->flags &= ~ST_PER_ACTION;
2078 break;
2079 default:
2080 st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
2081 }
2082
2083 bzero(&cmd, sizeof(cmd));
2084 cmd.opcode = WRITE_FILEMARKS;
2085 _lto3b(number, cmd.number);
2086
2087 return (scsipi_command(st->sc_link,
2088 (struct scsipi_generic *)&cmd, sizeof(cmd),
2089 0, 0, 0, ST_IO_TIME * 4, NULL, flags));
2090 }
2091
2092 /*
2093 * Make sure the right number of file marks is on tape if the
2094 * tape has been written. If the position argument is true,
2095 * leave the tape positioned where it was originally.
2096 *
2097 * nmarks returns the number of marks to skip (or, if position
2098 * true, which were skipped) to get back original position.
2099 */
2100 int
2101 st_check_eod(st, position, nmarks, flags)
2102 struct st_softc *st;
2103 boolean position;
2104 int *nmarks;
2105 int flags;
2106 {
2107 int error;
2108
2109 switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
2110 default:
2111 *nmarks = 0;
2112 return (0);
2113 case ST_WRITTEN:
2114 case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
2115 *nmarks = 1;
2116 break;
2117 case ST_WRITTEN | ST_2FM_AT_EOD:
2118 *nmarks = 2;
2119 }
2120 error = st_write_filemarks(st, *nmarks, flags);
2121 if (position && !error)
2122 error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
2123 return (error);
2124 }
2125
2126 /*
2127 * load/unload/retension
2128 */
2129 int
2130 st_load(st, type, flags)
2131 struct st_softc *st;
2132 u_int type;
2133 int flags;
2134 {
2135 int error;
2136 struct scsi_load cmd;
2137
2138 if (type != LD_LOAD) {
2139 int nmarks;
2140
2141 error = st_check_eod(st, FALSE, &nmarks, flags);
2142 if (error) {
2143 printf("%s: failed to write closing filemarks at "
2144 "unload, errno=%d\n", st->sc_dev.dv_xname, error);
2145 return (error);
2146 }
2147 }
2148 if (st->quirks & ST_Q_IGNORE_LOADS) {
2149 if (type == LD_LOAD) {
2150 /*
2151 * If we ignore loads, at least we should try a rewind.
2152 */
2153 return st_rewind(st, 0, flags);
2154 }
2155 /* otherwise, we should do what's asked of us */
2156 }
2157
2158 bzero(&cmd, sizeof(cmd));
2159 cmd.opcode = LOAD;
2160 cmd.how = type;
2161
2162 error = scsipi_command(st->sc_link,
2163 (struct scsipi_generic *)&cmd, sizeof(cmd),
2164 0, 0, ST_RETRIES, ST_SPC_TIME, NULL, flags);
2165 if (error) {
2166 printf("%s: error %d in st_load (op %d)\n",
2167 st->sc_dev.dv_xname, error, type);
2168 }
2169 return (error);
2170 }
2171
2172 /*
2173 * Rewind the device
2174 */
2175 int
2176 st_rewind(st, immediate, flags)
2177 struct st_softc *st;
2178 u_int immediate;
2179 int flags;
2180 {
2181 struct scsi_rewind cmd;
2182 int error;
2183 int nmarks;
2184
2185 error = st_check_eod(st, FALSE, &nmarks, flags);
2186 if (error) {
2187 printf("%s: failed to write closing filemarks at "
2188 "rewind, errno=%d\n", st->sc_dev.dv_xname, error);
2189 return (error);
2190 }
2191 st->flags &= ~ST_PER_ACTION;
2192
2193 bzero(&cmd, sizeof(cmd));
2194 cmd.opcode = REWIND;
2195 cmd.byte2 = immediate;
2196
2197 error = scsipi_command(st->sc_link,
2198 (struct scsipi_generic *)&cmd, sizeof(cmd), 0, 0, ST_RETRIES,
2199 immediate ? ST_CTL_TIME: ST_SPC_TIME, NULL, flags);
2200 if (error) {
2201 printf("%s: error %d trying to rewind\n",
2202 st->sc_dev.dv_xname, error);
2203 }
2204 return (error);
2205 }
2206
2207 int
2208 st_rdpos(st, hard, blkptr)
2209 struct st_softc *st;
2210 int hard;
2211 u_int32_t *blkptr;
2212 {
2213 int error;
2214 u_int8_t posdata[20];
2215 struct scsi_tape_read_position cmd;
2216
2217 /*
2218 * First flush any pending writes...
2219 */
2220 error = st_write_filemarks(st, 0, XS_CTL_SILENT);
2221
2222 /*
2223 * The latter case is for 'write protected' tapes
2224 * which are too stupid to recognize a zero count
2225 * for writing filemarks as a no-op.
2226 */
2227 if (error != 0 && error != EACCES && error != EROFS)
2228 return (error);
2229
2230 bzero(&cmd, sizeof(cmd));
2231 bzero(&posdata, sizeof(posdata));
2232 cmd.opcode = READ_POSITION;
2233 if (hard)
2234 cmd.byte1 = 1;
2235
2236 error = scsipi_command(st->sc_link,
2237 (struct scsipi_generic *)&cmd, sizeof(cmd), (u_char *)&posdata,
2238 sizeof(posdata), ST_RETRIES, ST_CTL_TIME, NULL,
2239 XS_CTL_SILENT | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
2240
2241 if (error == 0) {
2242 #if 0
2243 printf("posdata:");
2244 for (hard = 0; hard < sizeof(posdata); hard++)
2245 printf("%02x ", posdata[hard] & 0xff);
2246 printf("\n");
2247 #endif
2248 if (posdata[0] & 0x4) /* Block Position Unknown */
2249 error = EINVAL;
2250 else
2251 *blkptr = _4btol(&posdata[4]);
2252 }
2253 return (error);
2254 }
2255
2256 int
2257 st_setpos(st, hard, blkptr)
2258 struct st_softc *st;
2259 int hard;
2260 u_int32_t *blkptr;
2261 {
2262 int error;
2263 struct scsi_tape_locate cmd;
2264
2265 /*
2266 * First flush any pending writes. Strictly speaking,
2267 * we're not supposed to have to worry about this,
2268 * but let's be untrusting.
2269 */
2270 error = st_write_filemarks(st, 0, XS_CTL_SILENT);
2271
2272 /*
2273 * The latter case is for 'write protected' tapes
2274 * which are too stupid to recognize a zero count
2275 * for writing filemarks as a no-op.
2276 */
2277 if (error != 0 && error != EACCES && error != EROFS)
2278 return (error);
2279
2280 bzero(&cmd, sizeof(cmd));
2281 cmd.opcode = LOCATE;
2282 if (hard)
2283 cmd.byte2 = 1 << 2;
2284 _lto4b(*blkptr, cmd.blkaddr);
2285 error = scsipi_command(st->sc_link,
2286 (struct scsipi_generic *)&cmd, sizeof(cmd),
2287 NULL, 0, ST_RETRIES, ST_SPC_TIME, NULL, 0);
2288 /*
2289 * XXX: Note file && block number position now unknown (if
2290 * XXX: these things ever start being maintained in this driver)
2291 */
2292 return (error);
2293 }
2294
2295
2296 /*
2297 * Look at the returned sense and act on the error and determine
2298 * the unix error number to pass back..., 0 (== report no error),
2299 * -1 = retry the operation, -2 continue error processing.
2300 */
2301 int
2302 st_interpret_sense(xs)
2303 struct scsipi_xfer *xs;
2304 {
2305 struct scsipi_link *sc_link = xs->sc_link;
2306 struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
2307 struct buf *bp = xs->bp;
2308 struct st_softc *st = sc_link->device_softc;
2309 int retval = SCSIRET_CONTINUE;
2310 int doprint = ((xs->xs_control & XS_CTL_SILENT) == 0);
2311 u_int8_t key;
2312 int32_t info;
2313
2314 /*
2315 * If it isn't a extended or extended/deferred error, let
2316 * the generic code handle it.
2317 */
2318 if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
2319 (sense->error_code & SSD_ERRCODE) != 0x71) { /* DEFFERRED */
2320 return (retval);
2321 }
2322
2323 if (sense->error_code & SSD_ERRCODE_VALID)
2324 info = _4btol(sense->info);
2325 else
2326 info = xs->datalen; /* bad choice if fixed blocks */
2327 key = sense->flags & SSD_KEY;
2328 st->mt_erreg = key;
2329 st->asc = sense->add_sense_code;
2330 st->ascq = sense->add_sense_code_qual;
2331 st->mt_resid = (short) info;
2332
2333 /*
2334 * If the device is not open yet, let generic handle
2335 */
2336 if ((sc_link->flags & SDEV_OPEN) == 0) {
2337 return (retval);
2338 }
2339
2340
2341 if (st->flags & ST_FIXEDBLOCKS) {
2342 xs->resid = info * st->blksize;
2343 if (sense->flags & SSD_EOM) {
2344 if ((st->flags & ST_EARLYWARN) == 0)
2345 st->flags |= ST_EIO_PENDING;
2346 st->flags |= ST_EOM_PENDING;
2347 if (bp)
2348 bp->b_resid = xs->resid;
2349 }
2350 if (sense->flags & SSD_FILEMARK) {
2351 st->flags |= ST_AT_FILEMARK;
2352 if (bp)
2353 bp->b_resid = xs->resid;
2354 }
2355 if (sense->flags & SSD_ILI) {
2356 st->flags |= ST_EIO_PENDING;
2357 if (bp)
2358 bp->b_resid = xs->resid;
2359 if (sense->error_code & SSD_ERRCODE_VALID &&
2360 (xs->xs_control & XS_CTL_SILENT) == 0)
2361 printf("%s: block wrong size, %d blocks "
2362 "residual\n", st->sc_dev.dv_xname, info);
2363
2364 /*
2365 * This quirk code helps the drive read
2366 * the first tape block, regardless of
2367 * format. That is required for these
2368 * drives to return proper MODE SENSE
2369 * information.
2370 */
2371 if ((st->quirks & ST_Q_SENSE_HELP) &&
2372 !(sc_link->flags & SDEV_MEDIA_LOADED))
2373 st->blksize -= 512;
2374 }
2375 /*
2376 * If data wanted and no data was tranfered, do it immediatly
2377 */
2378 if (xs->datalen && xs->resid >= xs->datalen) {
2379 if (st->flags & ST_EIO_PENDING)
2380 return (EIO);
2381 if (st->flags & ST_AT_FILEMARK) {
2382 if (bp)
2383 bp->b_resid = xs->resid;
2384 return (SCSIRET_NOERROR);
2385 }
2386 }
2387 } else { /* must be variable mode */
2388 if (sense->flags & SSD_EOM) {
2389 /*
2390 * The current semantics of this
2391 * driver requires EOM detection
2392 * to return EIO unless early
2393 * warning detection is enabled
2394 * for variable mode (this is always
2395 * on for fixed block mode).
2396 */
2397 if (st->flags & ST_EARLYWARN) {
2398 st->flags |= ST_EOM_PENDING;
2399 retval = SCSIRET_NOERROR;
2400 } else {
2401 retval = EIO;
2402 }
2403
2404 /*
2405 * If it's an unadorned EOM detection,
2406 * suppress printing an error.
2407 */
2408 if (key == SKEY_NO_SENSE) {
2409 doprint = 0;
2410 }
2411 } else if (sense->flags & SSD_FILEMARK) {
2412 retval = SCSIRET_NOERROR;
2413 } else if (sense->flags & SSD_ILI) {
2414 if (info < 0) {
2415 /*
2416 * The tape record was bigger than the read
2417 * we issued.
2418 */
2419 if ((xs->xs_control & XS_CTL_SILENT) == 0) {
2420 printf("%s: %d-byte tape record too big"
2421 " for %d-byte user buffer\n",
2422 st->sc_dev.dv_xname,
2423 xs->datalen - info, xs->datalen);
2424 }
2425 retval = EIO;
2426 } else {
2427 retval = SCSIRET_NOERROR;
2428 }
2429 }
2430 xs->resid = info;
2431 if (bp)
2432 bp->b_resid = info;
2433 }
2434
2435 #ifndef SCSIDEBUG
2436 if (retval == SCSIRET_NOERROR && key == SKEY_NO_SENSE) {
2437 doprint = 0;
2438 }
2439 #endif
2440 if (key == SKEY_BLANK_CHECK) {
2441 /*
2442 * This quirk code helps the drive read the
2443 * first tape block, regardless of format. That
2444 * is required for these drives to return proper
2445 * MODE SENSE information.
2446 */
2447 if ((st->quirks & ST_Q_SENSE_HELP) &&
2448 !(sc_link->flags & SDEV_MEDIA_LOADED)) {
2449 /* still starting */
2450 st->blksize -= 512;
2451 } else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
2452 st->flags |= ST_BLANK_READ;
2453 xs->resid = xs->datalen;
2454 if (bp) {
2455 bp->b_resid = xs->resid;
2456 /* return an EOF */
2457 }
2458 retval = SCSIRET_NOERROR;
2459 }
2460 }
2461 #ifdef SCSIVERBOSE
2462 if (doprint) {
2463 scsipi_print_sense(xs, 0);
2464 }
2465 #else
2466 if (doprint) {
2467 xs->sc_link->sc_print_addr(xs->sc_link);
2468 printf("Sense Key 0x%02x", key);
2469 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
2470 switch (key) {
2471 case SKEY_NOT_READY:
2472 case SKEY_ILLEGAL_REQUEST:
2473 case SKEY_UNIT_ATTENTION:
2474 case SKEY_WRITE_PROTECT:
2475 break;
2476 case SKEY_BLANK_CHECK:
2477 printf(", requested size: %d (decimal)", info);
2478 break;
2479 case SKEY_ABORTED_COMMAND:
2480 if (xs->retries)
2481 printf(", retrying");
2482 printf(", cmd 0x%x, info 0x%x",
2483 xs->cmd->opcode, info);
2484 break;
2485 default:
2486 printf(", info = %d (decimal)", info);
2487 }
2488 }
2489 if (sense->extra_len != 0) {
2490 int n;
2491 printf(", data =");
2492 for (n = 0; n < sense->extra_len; n++)
2493 printf(" %02x", sense->cmd_spec_info[n]);
2494 }
2495 printf("\n");
2496 }
2497 #endif
2498 return (retval);
2499 }
2500
2501 /*
2502 * The quirk here is that the drive returns some value to st_mode_sense
2503 * incorrectly until the tape has actually passed by the head.
2504 *
2505 * The method is to set the drive to large fixed-block state (user-specified
2506 * density and 1024-byte blocks), then read and rewind to get it to sense the
2507 * tape. If that doesn't work, try 512-byte fixed blocks. If that doesn't
2508 * work, as a last resort, try variable- length blocks. The result will be
2509 * the ability to do an accurate st_mode_sense.
2510 *
2511 * We know we can do a rewind because we just did a load, which implies rewind.
2512 * Rewind seems preferable to space backward if we have a virgin tape.
2513 *
2514 * The rest of the code for this quirk is in ILI processing and BLANK CHECK
2515 * error processing, both part of st_interpret_sense.
2516 */
2517 int
2518 st_touch_tape(st)
2519 struct st_softc *st;
2520 {
2521 char *buf;
2522 int readsize;
2523 int error;
2524
2525 buf = malloc(1024, M_TEMP, M_NOWAIT);
2526 if (buf == NULL)
2527 return (ENOMEM);
2528
2529 if ((error = st_mode_sense(st, 0)) != 0)
2530 goto bad;
2531 st->blksize = 1024;
2532 do {
2533 switch (st->blksize) {
2534 case 512:
2535 case 1024:
2536 readsize = st->blksize;
2537 st->flags |= ST_FIXEDBLOCKS;
2538 break;
2539 default:
2540 readsize = 1;
2541 st->flags &= ~ST_FIXEDBLOCKS;
2542 }
2543 if ((error = st_mode_select(st, 0)) != 0) {
2544 /*
2545 * The device did not agree with the proposed
2546 * block size. If we exhausted our options,
2547 * return failure, else try another.
2548 */
2549 if (readsize == 1)
2550 goto bad;
2551 st->blksize -= 512;
2552 continue;
2553 }
2554 st_read(st, buf, readsize, XS_CTL_SILENT); /* XXX */
2555 if ((error = st_rewind(st, 0, 0)) != 0) {
2556 bad: free(buf, M_TEMP);
2557 return (error);
2558 }
2559 } while (readsize != 1 && readsize > st->blksize);
2560
2561 free(buf, M_TEMP);
2562 return (0);
2563 }
2564
2565 int
2566 stdump(dev, blkno, va, size)
2567 dev_t dev;
2568 daddr_t blkno;
2569 caddr_t va;
2570 size_t size;
2571 {
2572
2573 /* Not implemented. */
2574 return (ENXIO);
2575 }
2576