st.c revision 1.114.2.4 1 /* $NetBSD: st.c,v 1.114.2.4 2000/11/20 09:59:29 bouyer Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Originally written by Julian Elischer (julian (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_periph *sc_periph;/* our link to the adpter 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((struct st_softc *, int, int));
367 void st_unmount __P((struct st_softc *, boolean));
368 int st_decide_mode __P((struct st_softc *, boolean));
369 void ststart __P((struct scsipi_periph *));
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 const struct scsipi_periphsw 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_periph *periph = sa->sa_periph;
464
465 SC_DEBUG(periph, SCSIPI_DB2, ("stattach: "));
466
467 /*
468 * Store information needed to contact our base driver
469 */
470 st->sc_periph = periph;
471 periph->periph_dev = &st->sc_dev;
472 periph->periph_switch = &st_switch;
473
474 /*
475 * Set initial flags
476 */
477
478 st->flags = ST_INIT_FLAGS;
479
480 /*
481 * Check if the drive is a known criminal and take
482 * Any steps needed to bring it into line
483 */
484 st_identify_drive(st, &sa->sa_inqbuf);
485 /*
486 * Use the subdriver to request information regarding
487 * the drive. We cannot use interrupts yet, so the
488 * request must specify this.
489 */
490 printf("\n");
491 printf("%s: %s", st->sc_dev.dv_xname, st->quirkdata ? "rogue, " : "");
492 if (scsipi_test_unit_ready(periph,
493 XS_CTL_DISCOVERY | XS_CTL_SILENT | XS_CTL_IGNORE_MEDIA_CHANGE) ||
494 st_mode_sense(st,
495 XS_CTL_DISCOVERY | XS_CTL_SILENT | XS_CTL_IGNORE_MEDIA_CHANGE))
496 printf("drive empty\n");
497 else {
498 printf("density code %d, ", st->media_density);
499 if (st->media_blksize > 0)
500 printf("%d-byte", st->media_blksize);
501 else
502 printf("variable");
503 printf(" blocks, write-%s\n",
504 (st->flags & ST_READONLY) ? "protected" : "enabled");
505 }
506
507 /*
508 * Set up the buf queue for this device
509 */
510 BUFQ_INIT(&st->buf_queue);
511
512 #if NRND > 0
513 rnd_attach_source(&st->rnd_source, st->sc_dev.dv_xname,
514 RND_TYPE_TAPE, 0);
515 #endif
516 }
517
518 /*
519 * Use the inquiry routine in 'scsi_base' to get drive info so we can
520 * Further tailor our behaviour.
521 */
522 void
523 st_identify_drive(st, inqbuf)
524 struct st_softc *st;
525 struct scsipi_inquiry_pattern *inqbuf;
526 {
527 struct st_quirk_inquiry_pattern *finger;
528 int priority;
529
530 finger = (struct st_quirk_inquiry_pattern *)scsipi_inqmatch(inqbuf,
531 (caddr_t)st_quirk_patterns,
532 sizeof(st_quirk_patterns) / sizeof(st_quirk_patterns[0]),
533 sizeof(st_quirk_patterns[0]), &priority);
534 if (priority != 0) {
535 st->quirkdata = &finger->quirkdata;
536 st->drive_quirks = finger->quirkdata.quirks;
537 st->quirks = finger->quirkdata.quirks; /* start value */
538 st->page_0_size = finger->quirkdata.page_0_size;
539 st_loadquirks(st);
540 }
541 }
542
543 /*
544 * initialise the subdevices to the default (QUIRK) state.
545 * this will remove any setting made by the system operator or previous
546 * operations.
547 */
548 void
549 st_loadquirks(st)
550 struct st_softc *st;
551 {
552 int i;
553 struct modes *mode;
554 struct modes *mode2;
555
556 mode = st->quirkdata->modes;
557 mode2 = st->modes;
558 for (i = 0; i < 4; i++) {
559 bzero(mode2, sizeof(struct modes));
560 st->modeflags[i] &= ~(BLKSIZE_SET_BY_QUIRK |
561 DENSITY_SET_BY_QUIRK | BLKSIZE_SET_BY_USER |
562 DENSITY_SET_BY_USER);
563 if ((mode->quirks | st->drive_quirks) & ST_Q_FORCE_BLKSIZE) {
564 mode2->blksize = mode->blksize;
565 st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK;
566 }
567 if (mode->density) {
568 mode2->density = mode->density;
569 st->modeflags[i] |= DENSITY_SET_BY_QUIRK;
570 }
571 mode++;
572 mode2++;
573 }
574 }
575
576 /*
577 * open the device.
578 */
579 int
580 stopen(dev, flags, mode, p)
581 dev_t dev;
582 int flags;
583 int mode;
584 struct proc *p;
585 {
586 u_int stmode, dsty;
587 int error, sflags, unit, tries, ntries;
588 struct st_softc *st;
589 struct scsipi_periph *periph;
590 struct scsipi_adapter *adapt;
591
592 unit = STUNIT(dev);
593 if (unit >= st_cd.cd_ndevs)
594 return (ENXIO);
595 st = st_cd.cd_devs[unit];
596 if (st == NULL)
597 return (ENXIO);
598
599 stmode = STMODE(dev);
600 dsty = STDSTY(dev);
601
602 periph = st->sc_periph;
603 adapt = periph->periph_channel->chan_adapter;
604
605 SC_DEBUG(periph, SCSIPI_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
606 unit, st_cd.cd_ndevs));
607
608
609 /*
610 * Only allow one at a time
611 */
612 if (periph->periph_flags & PERIPH_OPEN) {
613 printf("%s: already open\n", st->sc_dev.dv_xname);
614 return (EBUSY);
615 }
616
617 if ((error = scsipi_adapter_addref(adapt)) != 0)
618 return (error);
619
620 /*
621 * clear any latched errors.
622 */
623 st->mt_resid = 0;
624 st->mt_erreg = 0;
625 st->asc = 0;
626 st->ascq = 0;
627
628 /*
629 * Catch any unit attention errors. Be silent about this
630 * unless we're already mounted. We ignore media change
631 * if we're in control mode or not mounted yet.
632 */
633 if ((st->flags & ST_MOUNTED) == 0 || stmode == CTRL_MODE) {
634 #ifdef SCSIDEBUG
635 sflags = XS_CTL_IGNORE_MEDIA_CHANGE;
636 #else
637 sflags = XS_CTL_SILENT|XS_CTL_IGNORE_MEDIA_CHANGE;
638 #endif
639 } else
640 sflags = 0;
641
642 /*
643 * If we're already mounted or we aren't configured for
644 * a mount delay, only try a test unit ready once. Otherwise,
645 * try up to ST_MOUNT_DELAY times with a rest interval of
646 * one second between each try.
647 */
648
649 if ((st->flags & ST_MOUNTED) || ST_MOUNT_DELAY == 0) {
650 ntries = 1;
651 } else {
652 ntries = ST_MOUNT_DELAY;
653 }
654
655 for (error = tries = 0; tries < ntries; tries++) {
656 int slpintr, oflags;
657
658 /*
659 * If we had no error, or we're opening the control mode
660 * device, we jump out right away.
661 */
662
663 error = scsipi_test_unit_ready(periph, sflags);
664 if (error == 0 || stmode == CTRL_MODE) {
665 break;
666 }
667
668 /*
669 * We had an error.
670 *
671 * If we're already mounted or we aren't configured for
672 * a mount delay, or the error isn't a NOT READY error,
673 * skip to the error exit now.
674 */
675 if ((st->flags & ST_MOUNTED) || ST_MOUNT_DELAY == 0 ||
676 (st->mt_key != SKEY_NOT_READY)) {
677 goto bad;
678 }
679
680 /*
681 * clear any latched errors.
682 */
683 st->mt_resid = 0;
684 st->mt_erreg = 0;
685 st->asc = 0;
686 st->ascq = 0;
687
688 /*
689 * Fake that we have the device open so
690 * we block other apps from getting in.
691 */
692
693 oflags = periph->periph_flags;
694 periph->periph_flags |= PERIPH_OPEN;
695
696 slpintr = tsleep(&lbolt, PUSER|PCATCH, "stload", 0);
697
698 periph->periph_flags = oflags; /* restore flags */
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 periph->periph_flags |= PERIPH_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 periph->periph_flags |= PERIPH_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 ||
739 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
740 st_unmount(st, NOEJECT);
741
742 /*
743 * If we are not mounted, then we should start a new
744 * mount session.
745 */
746 if ((st->flags & ST_MOUNTED) == 0) {
747 st_mount_tape(st, dsty, flags);
748 st->last_dsty = dsty;
749 }
750
751 SC_DEBUG(periph, SCSIPI_DB2, ("open complete\n"));
752 return (0);
753
754 bad:
755 st_unmount(st, NOEJECT);
756 scsipi_adapter_delref(adapt);
757 periph->periph_flags &= ~PERIPH_OPEN;
758 return (error);
759 }
760
761 /*
762 * close the device.. only called if we are the LAST
763 * occurence of an open device
764 */
765 int
766 stclose(dev, flags, mode, p)
767 dev_t dev;
768 int flags;
769 int mode;
770 struct proc *p;
771 {
772 int stxx, error = 0;
773 struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
774 struct scsipi_periph *periph = st->sc_periph;
775 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
776
777 SC_DEBUG(st->sc_periph, SCSIPI_DB1, ("closing\n"));
778
779 /*
780 * Make sure that a tape opened in write-only mode will have
781 * file marks written on it when closed, even if not written to.
782 *
783 * This is for SUN compatibility. Actually, the Sun way of
784 * things is to:
785 *
786 * only write filemarks if there are fmks to be written and
787 * - open for write (possibly read/write)
788 * - the last operation was a write
789 * or:
790 * - opened for wronly
791 * - no data was written (including filemarks)
792 */
793
794 stxx = st->flags & (ST_WRITTEN | ST_FM_WRITTEN);
795 if (((flags & FWRITE) && stxx == ST_WRITTEN) ||
796 ((flags & O_ACCMODE) == FWRITE && stxx == 0)) {
797 int nm;
798 error = st_check_eod(st, FALSE, &nm, 0);
799 }
800
801 switch (STMODE(dev)) {
802 case NORMAL_MODE:
803 st_unmount(st, NOEJECT);
804 break;
805 case NOREW_MODE:
806 case CTRL_MODE:
807 /*
808 * Leave mounted unless media seems to have been removed.
809 *
810 * Otherwise, if we're to terminate a tape with more than one
811 * filemark [ and because we're not rewinding here ], backspace
812 * one filemark so that later appends will see an unbroken
813 * sequence of:
814 *
815 * file - FMK - file - FMK ... file - FMK FMK (EOM)
816 */
817 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
818 st_unmount(st, NOEJECT);
819 } else if (error == 0) {
820 /*
821 * ST_WRITTEN was preserved from above.
822 *
823 * All we need to know here is:
824 *
825 * Were we writing this tape and was the last
826 * operation a write?
827 *
828 * Are there supposed to be 2FM at EOD?
829 *
830 * If both statements are true, then we backspace
831 * one filemark.
832 */
833 stxx |= (st->flags & ST_2FM_AT_EOD);
834 if ((flags & FWRITE) != 0 &&
835 (stxx == (ST_2FM_AT_EOD|ST_WRITTEN))) {
836 error = st_space(st, -1, SP_FILEMARKS, 0);
837 }
838 }
839 break;
840 case EJECT_MODE:
841 st_unmount(st, EJECT);
842 break;
843 }
844
845 scsipi_wait_drain(periph);
846
847 scsipi_adapter_delref(adapt);
848 periph->periph_flags &= ~PERIPH_OPEN;
849
850 return (error);
851 }
852
853 /*
854 * Start a new mount session.
855 * Copy in all the default parameters from the selected device mode.
856 * and try guess any that seem to be defaulted.
857 */
858 int
859 st_mount_tape(st, dsty, flags)
860 struct st_softc *st;
861 int dsty, flags;
862 {
863 struct scsipi_periph *periph = st->sc_periph;
864 int error = 0;
865
866 if (st->flags & ST_MOUNTED)
867 return (0);
868
869 SC_DEBUG(periph, SCSIPI_DB1, ("mounting\n "));
870 st->flags |= ST_NEW_MOUNT;
871 st->quirks = st->drive_quirks | st->modes[dsty].quirks;
872 /*
873 * If the media is new, then make sure we give it a chance to
874 * to do a 'load' instruction. (We assume it is new.)
875 */
876 if ((error = st_load(st, LD_LOAD, XS_CTL_SILENT)) != 0)
877 return (error);
878 /*
879 * Throw another dummy instruction to catch
880 * 'Unit attention' errors. Many drives give
881 * these after doing a Load instruction (with
882 * the MEDIUM MAY HAVE CHANGED asc/ascq).
883 */
884 scsipi_test_unit_ready(periph, XS_CTL_SILENT); /* XXX */
885
886 /*
887 * Some devices can't tell you much until they have been
888 * asked to look at the media. This quirk does this.
889 */
890 if (st->quirks & ST_Q_SENSE_HELP)
891 if ((error = st_touch_tape(st)) != 0)
892 return (error);
893 /*
894 * Load the physical device parameters
895 * loads: blkmin, blkmax
896 */
897 if ((error = st_read_block_limits(st, 0)) != 0)
898 return (error);
899 /*
900 * Load the media dependent parameters
901 * includes: media_blksize,media_density,numblks
902 * As we have a tape in, it should be reflected here.
903 * If not you may need the "quirk" above.
904 */
905 if ((error = st_mode_sense(st, 0)) != 0)
906 return (error);
907 /*
908 * If we have gained a permanent density from somewhere,
909 * then use it in preference to the one supplied by
910 * default by the driver.
911 */
912 if (st->modeflags[dsty] & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER))
913 st->density = st->modes[dsty].density;
914 else
915 st->density = st->media_density;
916 /*
917 * If we have gained a permanent blocksize
918 * then use it in preference to the one supplied by
919 * default by the driver.
920 */
921 st->flags &= ~ST_FIXEDBLOCKS;
922 if (st->modeflags[dsty] &
923 (BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) {
924 st->blksize = st->modes[dsty].blksize;
925 if (st->blksize)
926 st->flags |= ST_FIXEDBLOCKS;
927 } else {
928 if ((error = st_decide_mode(st, FALSE)) != 0)
929 return (error);
930 }
931 if ((error = st_mode_select(st, 0)) != 0) {
932 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
933 return (error);
934 }
935 if (!(st->quirks & ST_Q_NOPREVENT)) {
936 scsipi_prevent(periph, PR_PREVENT,
937 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_NOT_READY);
938 }
939 st->flags &= ~ST_NEW_MOUNT;
940 st->flags |= ST_MOUNTED;
941 periph->periph_flags |= PERIPH_MEDIA_LOADED; /* move earlier? */
942
943 return (0);
944 }
945
946 /*
947 * End the present mount session.
948 * Rewind, and optionally eject the tape.
949 * Reset various flags to indicate that all new
950 * operations require another mount operation
951 */
952 void
953 st_unmount(st, eject)
954 struct st_softc *st;
955 boolean eject;
956 {
957 struct scsipi_periph *periph = st->sc_periph;
958 int nmarks;
959
960 if ((st->flags & ST_MOUNTED) == 0)
961 return;
962 SC_DEBUG(periph, SCSIPI_DB1, ("unmounting\n"));
963 st_check_eod(st, FALSE, &nmarks, XS_CTL_IGNORE_NOT_READY);
964 st_rewind(st, 0, XS_CTL_IGNORE_NOT_READY);
965 scsipi_prevent(periph, PR_ALLOW,
966 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_NOT_READY);
967 if (eject)
968 st_load(st, LD_UNLOAD, XS_CTL_IGNORE_NOT_READY);
969 st->flags &= ~(ST_MOUNTED | ST_NEW_MOUNT);
970 periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
971 }
972
973 /*
974 * Given all we know about the device, media, mode, 'quirks' and
975 * initial operation, make a decision as to how we should be set
976 * to run (regarding blocking and EOD marks)
977 */
978 int
979 st_decide_mode(st, first_read)
980 struct st_softc *st;
981 boolean first_read;
982 {
983
984 SC_DEBUG(st->sc_periph, SCSIPI_DB2, ("starting block mode decision\n"));
985
986 /*
987 * If the drive can only handle fixed-length blocks and only at
988 * one size, perhaps we should just do that.
989 */
990 if (st->blkmin && (st->blkmin == st->blkmax)) {
991 st->flags |= ST_FIXEDBLOCKS;
992 st->blksize = st->blkmin;
993 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
994 ("blkmin == blkmax of %d\n", st->blkmin));
995 goto done;
996 }
997 /*
998 * If the tape density mandates (or even suggests) use of fixed
999 * or variable-length blocks, comply.
1000 */
1001 switch (st->density) {
1002 case HALFINCH_800:
1003 case HALFINCH_1600:
1004 case HALFINCH_6250:
1005 case DDS:
1006 st->flags &= ~ST_FIXEDBLOCKS;
1007 st->blksize = 0;
1008 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
1009 ("density specified variable\n"));
1010 goto done;
1011 case QIC_11:
1012 case QIC_24:
1013 case QIC_120:
1014 case QIC_150:
1015 case QIC_525:
1016 case QIC_1320:
1017 case QIC_3095:
1018 case QIC_3220:
1019 st->flags |= ST_FIXEDBLOCKS;
1020 if (st->media_blksize > 0)
1021 st->blksize = st->media_blksize;
1022 else
1023 st->blksize = DEF_FIXED_BSIZE;
1024 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
1025 ("density specified fixed\n"));
1026 goto done;
1027 }
1028 /*
1029 * If we're about to read the tape, perhaps we should choose
1030 * fixed or variable-length blocks and block size according to
1031 * what the drive found on the tape.
1032 */
1033 if (first_read &&
1034 (!(st->quirks & ST_Q_BLKSIZE) || (st->media_blksize == 0) ||
1035 (st->media_blksize == DEF_FIXED_BSIZE) ||
1036 (st->media_blksize == 1024))) {
1037 if (st->media_blksize > 0)
1038 st->flags |= ST_FIXEDBLOCKS;
1039 else
1040 st->flags &= ~ST_FIXEDBLOCKS;
1041 st->blksize = st->media_blksize;
1042 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
1043 ("Used media_blksize of %d\n", st->media_blksize));
1044 goto done;
1045 }
1046 /*
1047 * We're getting no hints from any direction. Choose variable-
1048 * length blocks arbitrarily.
1049 */
1050 st->flags &= ~ST_FIXEDBLOCKS;
1051 st->blksize = 0;
1052 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
1053 ("Give up and default to variable mode\n"));
1054
1055 done:
1056 /*
1057 * Decide whether or not to write two file marks to signify end-
1058 * of-data. Make the decision as a function of density. If
1059 * the decision is not to use a second file mark, the SCSI BLANK
1060 * CHECK condition code will be recognized as end-of-data when
1061 * first read.
1062 * (I think this should be a by-product of fixed/variable..julian)
1063 */
1064 switch (st->density) {
1065 /* case 8 mm: What is the SCSI density code for 8 mm, anyway? */
1066 case QIC_11:
1067 case QIC_24:
1068 case QIC_120:
1069 case QIC_150:
1070 case QIC_525:
1071 case QIC_1320:
1072 case QIC_3095:
1073 case QIC_3220:
1074 st->flags &= ~ST_2FM_AT_EOD;
1075 break;
1076 default:
1077 st->flags |= ST_2FM_AT_EOD;
1078 }
1079 return (0);
1080 }
1081
1082 /*
1083 * Actually translate the requested transfer into
1084 * one the physical driver can understand
1085 * The transfer is described by a buf and will include
1086 * only one physical transfer.
1087 */
1088 void
1089 ststrategy(bp)
1090 struct buf *bp;
1091 {
1092 struct st_softc *st = st_cd.cd_devs[STUNIT(bp->b_dev)];
1093 int s;
1094
1095 SC_DEBUG(st->sc_periph, SCSIPI_DB1,
1096 ("ststrategy %ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
1097 /*
1098 * If it's a null transfer, return immediatly
1099 */
1100 if (bp->b_bcount == 0)
1101 goto done;
1102
1103 /* If offset is negative, error */
1104 if (bp->b_blkno < 0) {
1105 bp->b_error = EINVAL;
1106 goto bad;
1107 }
1108
1109 /*
1110 * Odd sized request on fixed drives are verboten
1111 */
1112 if (st->flags & ST_FIXEDBLOCKS) {
1113 if (bp->b_bcount % st->blksize) {
1114 printf("%s: bad request, must be multiple of %d\n",
1115 st->sc_dev.dv_xname, st->blksize);
1116 bp->b_error = EIO;
1117 goto bad;
1118 }
1119 }
1120 /*
1121 * as are out-of-range requests on variable drives.
1122 */
1123 else if (bp->b_bcount < st->blkmin ||
1124 (st->blkmax && bp->b_bcount > st->blkmax)) {
1125 printf("%s: bad request, must be between %d and %d\n",
1126 st->sc_dev.dv_xname, st->blkmin, st->blkmax);
1127 bp->b_error = EIO;
1128 goto bad;
1129 }
1130 s = splbio();
1131
1132 /*
1133 * Place it in the queue of activities for this tape
1134 * at the end (a bit silly because we only have on user..
1135 * (but it could fork()))
1136 */
1137 BUFQ_INSERT_TAIL(&st->buf_queue, bp);
1138
1139 /*
1140 * Tell the device to get going on the transfer if it's
1141 * not doing anything, otherwise just wait for completion
1142 * (All a bit silly if we're only allowing 1 open but..)
1143 */
1144 ststart(st->sc_periph);
1145
1146 splx(s);
1147 return;
1148 bad:
1149 bp->b_flags |= B_ERROR;
1150 done:
1151 /*
1152 * Correctly set the buf to indicate a completed xfer
1153 */
1154 bp->b_resid = bp->b_bcount;
1155 biodone(bp);
1156 return;
1157 }
1158
1159 /*
1160 * ststart looks to see if there is a buf waiting for the device
1161 * and that the device is not already busy. If both are true,
1162 * It dequeues the buf and creates a scsi command to perform the
1163 * transfer required. The transfer request will call scsipi_done
1164 * on completion, which will in turn call this routine again
1165 * so that the next queued transfer is performed.
1166 * The bufs are queued by the strategy routine (ststrategy)
1167 *
1168 * This routine is also called after other non-queued requests
1169 * have been made of the scsi driver, to ensure that the queue
1170 * continues to be drained.
1171 * ststart() is called at splbio
1172 */
1173 void
1174 ststart(periph)
1175 struct scsipi_periph *periph;
1176 {
1177 struct st_softc *st = (void *)periph->periph_dev;
1178 struct buf *bp;
1179 struct scsi_rw_tape cmd;
1180 int flags, error;
1181
1182 SC_DEBUG(periph, SCSIPI_DB2, ("ststart "));
1183 /*
1184 * See if there is a buf to do and we are not already
1185 * doing one
1186 */
1187 while (periph->periph_active < periph->periph_openings) {
1188 /* if a special awaits, let it proceed first */
1189 if (periph->periph_flags & PERIPH_WAITING) {
1190 periph->periph_flags &= ~PERIPH_WAITING;
1191 wakeup((caddr_t)periph);
1192 return;
1193 }
1194
1195 if ((bp = BUFQ_FIRST(&st->buf_queue)) == NULL)
1196 return;
1197 BUFQ_REMOVE(&st->buf_queue, bp);
1198
1199 /*
1200 * If the device has been unmounted by the user
1201 * then throw away all requests until done.
1202 */
1203 if ((st->flags & ST_MOUNTED) == 0 ||
1204 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
1205 /* make sure that one implies the other.. */
1206 periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
1207 bp->b_flags |= B_ERROR;
1208 bp->b_error = EIO;
1209 bp->b_resid = bp->b_bcount;
1210 biodone(bp);
1211 continue;
1212 }
1213 /*
1214 * only FIXEDBLOCK devices have pending I/O or space operations.
1215 */
1216 if (st->flags & ST_FIXEDBLOCKS) {
1217 /*
1218 * If we are at a filemark but have not reported it yet
1219 * then we should report it now
1220 */
1221 if (st->flags & ST_AT_FILEMARK) {
1222 if ((bp->b_flags & B_READ) == B_WRITE) {
1223 /*
1224 * Handling of ST_AT_FILEMARK in
1225 * st_space will fill in the right file
1226 * mark count.
1227 * Back up over filemark
1228 */
1229 if (st_space(st, 0, SP_FILEMARKS, 0)) {
1230 bp->b_flags |= B_ERROR;
1231 bp->b_error = EIO;
1232 biodone(bp);
1233 continue;
1234 }
1235 } else {
1236 bp->b_resid = bp->b_bcount;
1237 bp->b_error = 0;
1238 bp->b_flags &= ~B_ERROR;
1239 st->flags &= ~ST_AT_FILEMARK;
1240 biodone(bp);
1241 continue; /* seek more work */
1242 }
1243 }
1244 }
1245 /*
1246 * If we are at EOM but have not reported it
1247 * yet then we should report it now.
1248 */
1249 if (st->flags & (ST_EOM_PENDING|ST_EIO_PENDING)) {
1250 bp->b_resid = bp->b_bcount;
1251 if (st->flags & ST_EIO_PENDING) {
1252 bp->b_error = EIO;
1253 bp->b_flags |= B_ERROR;
1254 }
1255 st->flags &= ~(ST_EOM_PENDING|ST_EIO_PENDING);
1256 biodone(bp);
1257 continue; /* seek more work */
1258 }
1259
1260 /*
1261 * Fill out the scsi command
1262 */
1263 bzero(&cmd, sizeof(cmd));
1264 flags = XS_CTL_NOSLEEP | XS_CTL_ASYNC;
1265 if ((bp->b_flags & B_READ) == B_WRITE) {
1266 cmd.opcode = WRITE;
1267 st->flags &= ~ST_FM_WRITTEN;
1268 flags |= XS_CTL_DATA_OUT;
1269 } else {
1270 cmd.opcode = READ;
1271 flags |= XS_CTL_DATA_IN;
1272 }
1273
1274 /*
1275 * Handle "fixed-block-mode" tape drives by using the
1276 * block count instead of the length.
1277 */
1278 if (st->flags & ST_FIXEDBLOCKS) {
1279 cmd.byte2 |= SRW_FIXED;
1280 _lto3b(bp->b_bcount / st->blksize, cmd.len);
1281 } else
1282 _lto3b(bp->b_bcount, cmd.len);
1283
1284 /*
1285 * go ask the adapter to do all this for us
1286 */
1287 error = scsipi_command(periph,
1288 (struct scsipi_generic *)&cmd, sizeof(cmd),
1289 (u_char *)bp->b_data, bp->b_bcount,
1290 0, ST_IO_TIME, bp, flags);
1291 if (error) {
1292 printf("%s: not queued, error %d\n",
1293 st->sc_dev.dv_xname, error);
1294 }
1295 } /* go back and see if we can cram more work in.. */
1296 }
1297
1298 void
1299 stdone(xs)
1300 struct scsipi_xfer *xs;
1301 {
1302 struct st_softc *st = (void *)xs->xs_periph->periph_dev;
1303
1304 if (xs->bp != NULL) {
1305 if ((xs->bp->b_flags & B_READ) == B_WRITE) {
1306 st->flags |= ST_WRITTEN;
1307 } else {
1308 st->flags &= ~ST_WRITTEN;
1309 }
1310 #if NRND > 0
1311 rnd_add_uint32(&st->rnd_source, xs->bp->b_blkno);
1312 #endif
1313 }
1314 }
1315
1316 int
1317 stread(dev, uio, iomode)
1318 dev_t dev;
1319 struct uio *uio;
1320 int iomode;
1321 {
1322 struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
1323
1324 return (physio(ststrategy, NULL, dev, B_READ,
1325 st->sc_periph->periph_channel->chan_adapter->adapt_minphys, uio));
1326 }
1327
1328 int
1329 stwrite(dev, uio, iomode)
1330 dev_t dev;
1331 struct uio *uio;
1332 int iomode;
1333 {
1334 struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
1335
1336 return (physio(ststrategy, NULL, dev, B_WRITE,
1337 st->sc_periph->periph_channel->chan_adapter->adapt_minphys, uio));
1338 }
1339
1340 /*
1341 * Perform special action on behalf of the user;
1342 * knows about the internals of this device
1343 */
1344 int
1345 stioctl(dev, cmd, arg, flag, p)
1346 dev_t dev;
1347 u_long cmd;
1348 caddr_t arg;
1349 int flag;
1350 struct proc *p;
1351 {
1352 int error = 0;
1353 int unit;
1354 int number, nmarks, dsty;
1355 int flags;
1356 struct st_softc *st;
1357 int hold_blksize;
1358 u_int8_t hold_density;
1359 struct mtop *mt = (struct mtop *) arg;
1360
1361 /*
1362 * Find the device that the user is talking about
1363 */
1364 flags = 0; /* give error messages, act on errors etc. */
1365 unit = STUNIT(dev);
1366 dsty = STDSTY(dev);
1367 st = st_cd.cd_devs[unit];
1368 hold_blksize = st->blksize;
1369 hold_density = st->density;
1370
1371 switch ((u_int) cmd) {
1372
1373 case MTIOCGET: {
1374 struct mtget *g = (struct mtget *) arg;
1375 /*
1376 * (to get the current state of READONLY)
1377 */
1378 error = st_mode_sense(st, XS_CTL_SILENT);
1379 if (error)
1380 break;
1381 SC_DEBUG(st->sc_periph, SCSIPI_DB1, ("[ioctl: get status]\n"));
1382 bzero(g, sizeof(struct mtget));
1383 g->mt_type = 0x7; /* Ultrix compat *//*? */
1384 g->mt_blksiz = st->blksize;
1385 g->mt_density = st->density;
1386 g->mt_mblksiz[0] = st->modes[0].blksize;
1387 g->mt_mblksiz[1] = st->modes[1].blksize;
1388 g->mt_mblksiz[2] = st->modes[2].blksize;
1389 g->mt_mblksiz[3] = st->modes[3].blksize;
1390 g->mt_mdensity[0] = st->modes[0].density;
1391 g->mt_mdensity[1] = st->modes[1].density;
1392 g->mt_mdensity[2] = st->modes[2].density;
1393 g->mt_mdensity[3] = st->modes[3].density;
1394 if (st->flags & ST_READONLY)
1395 g->mt_dsreg |= MT_DS_RDONLY;
1396 if (st->flags & ST_MOUNTED)
1397 g->mt_dsreg |= MT_DS_MOUNTED;
1398 g->mt_resid = st->mt_resid;
1399 g->mt_erreg = st->mt_erreg;
1400 /*
1401 * clear latched errors.
1402 */
1403 st->mt_resid = 0;
1404 st->mt_erreg = 0;
1405 st->asc = 0;
1406 st->ascq = 0;
1407 break;
1408 }
1409 case MTIOCTOP: {
1410
1411 SC_DEBUG(st->sc_periph, SCSIPI_DB1,
1412 ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op,
1413 mt->mt_count));
1414
1415 /* compat: in U*x it is a short */
1416 number = mt->mt_count;
1417 switch ((short) (mt->mt_op)) {
1418 case MTWEOF: /* write an end-of-file record */
1419 error = st_write_filemarks(st, number, flags);
1420 break;
1421 case MTBSF: /* backward space file */
1422 number = -number;
1423 case MTFSF: /* forward space file */
1424 error = st_check_eod(st, FALSE, &nmarks, flags);
1425 if (!error)
1426 error = st_space(st, number - nmarks,
1427 SP_FILEMARKS, flags);
1428 break;
1429 case MTBSR: /* backward space record */
1430 number = -number;
1431 case MTFSR: /* forward space record */
1432 error = st_check_eod(st, TRUE, &nmarks, flags);
1433 if (!error)
1434 error = st_space(st, number, SP_BLKS, flags);
1435 break;
1436 case MTREW: /* rewind */
1437 error = st_rewind(st, 0, flags);
1438 break;
1439 case MTOFFL: /* rewind and put the drive offline */
1440 st_unmount(st, EJECT);
1441 break;
1442 case MTNOP: /* no operation, sets status only */
1443 break;
1444 case MTRETEN: /* retension the tape */
1445 error = st_load(st, LD_RETENSION, flags);
1446 if (!error)
1447 error = st_load(st, LD_LOAD, flags);
1448 break;
1449 case MTEOM: /* forward space to end of media */
1450 error = st_check_eod(st, FALSE, &nmarks, flags);
1451 if (!error)
1452 error = st_space(st, 1, SP_EOM, flags);
1453 break;
1454 case MTCACHE: /* enable controller cache */
1455 st->flags &= ~ST_DONTBUFFER;
1456 goto try_new_value;
1457 case MTNOCACHE: /* disable controller cache */
1458 st->flags |= ST_DONTBUFFER;
1459 goto try_new_value;
1460 case MTERASE: /* erase volume */
1461 error = st_erase(st, number, flags);
1462 break;
1463 case MTSETBSIZ: /* Set block size for device */
1464 #ifdef NOTYET
1465 if (!(st->flags & ST_NEW_MOUNT)) {
1466 uprintf("re-mount tape before changing blocksize");
1467 error = EINVAL;
1468 break;
1469 }
1470 #endif
1471 if (number == 0)
1472 st->flags &= ~ST_FIXEDBLOCKS;
1473 else {
1474 if ((st->blkmin || st->blkmax) &&
1475 (number < st->blkmin ||
1476 number > st->blkmax)) {
1477 error = EINVAL;
1478 break;
1479 }
1480 st->flags |= ST_FIXEDBLOCKS;
1481 }
1482 st->blksize = number;
1483 st->flags |= ST_BLOCK_SET; /*XXX */
1484 goto try_new_value;
1485
1486 case MTSETDNSTY: /* Set density for device and mode */
1487 /*
1488 * Any number >= 0 and <= 0xff is legal. Numbers
1489 * above 0x80 are 'vendor unique'.
1490 */
1491 if (number < 0 || number > 255) {
1492 error = EINVAL;
1493 break;
1494 } else
1495 st->density = number;
1496 goto try_new_value;
1497
1498 case MTCMPRESS:
1499 error = st_cmprss(st, number);
1500 break;
1501
1502 case MTEWARN:
1503 if (number)
1504 st->flags |= ST_EARLYWARN;
1505 else
1506 st->flags &= ~ST_EARLYWARN;
1507 break;
1508
1509 default:
1510 error = EINVAL;
1511 }
1512 break;
1513 }
1514 case MTIOCIEOT:
1515 case MTIOCEEOT:
1516 break;
1517
1518 case MTIOCRDSPOS:
1519 error = st_rdpos(st, 0, (u_int32_t *) arg);
1520 break;
1521
1522 case MTIOCRDHPOS:
1523 error = st_rdpos(st, 1, (u_int32_t *) arg);
1524 break;
1525
1526 case MTIOCSLOCATE:
1527 error = st_setpos(st, 0, (u_int32_t *) arg);
1528 break;
1529
1530 case MTIOCHLOCATE:
1531 error = st_setpos(st, 1, (u_int32_t *) arg);
1532 break;
1533
1534
1535 default:
1536 error = scsipi_do_ioctl(st->sc_periph, dev, cmd, arg,
1537 flag, p);
1538 break;
1539 }
1540 return (error);
1541 /*-----------------------------*/
1542 try_new_value:
1543 /*
1544 * Check that the mode being asked for is aggreeable to the
1545 * drive. If not, put it back the way it was.
1546 */
1547 if ((error = st_mode_select(st, 0)) != 0) {/* put it back as it was */
1548 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
1549 st->density = hold_density;
1550 st->blksize = hold_blksize;
1551 if (st->blksize)
1552 st->flags |= ST_FIXEDBLOCKS;
1553 else
1554 st->flags &= ~ST_FIXEDBLOCKS;
1555 return (error);
1556 }
1557 /*
1558 * As the drive liked it, if we are setting a new default,
1559 * set it into the structures as such.
1560 *
1561 * The means for deciding this are not finalised yet- but
1562 * if the device was opened in Control Mode, the values
1563 * are persistent now across mounts.
1564 */
1565 if (STMODE(dev) == CTRL_MODE) {
1566 switch ((short) (mt->mt_op)) {
1567 case MTSETBSIZ:
1568 st->modes[dsty].blksize = st->blksize;
1569 st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
1570 break;
1571 case MTSETDNSTY:
1572 st->modes[dsty].density = st->density;
1573 st->modeflags[dsty] |= DENSITY_SET_BY_USER;
1574 break;
1575 }
1576 }
1577 return (0);
1578 }
1579
1580 /*
1581 * Do a synchronous read.
1582 */
1583 int
1584 st_read(st, buf, size, flags)
1585 struct st_softc *st;
1586 int size;
1587 int flags;
1588 char *buf;
1589 {
1590 struct scsi_rw_tape cmd;
1591
1592 /*
1593 * If it's a null transfer, return immediatly
1594 */
1595 if (size == 0)
1596 return (0);
1597 bzero(&cmd, sizeof(cmd));
1598 cmd.opcode = READ;
1599 if (st->flags & ST_FIXEDBLOCKS) {
1600 cmd.byte2 |= SRW_FIXED;
1601 _lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE),
1602 cmd.len);
1603 } else
1604 _lto3b(size, cmd.len);
1605 return (scsipi_command(st->sc_periph,
1606 (struct scsipi_generic *)&cmd, sizeof(cmd),
1607 (u_char *)buf, size, 0, ST_IO_TIME, NULL, flags | XS_CTL_DATA_IN));
1608 }
1609
1610 /*
1611 * Ask the drive what it's min and max blk sizes are.
1612 */
1613 int
1614 st_read_block_limits(st, flags)
1615 struct st_softc *st;
1616 int flags;
1617 {
1618 struct scsi_block_limits cmd;
1619 struct scsi_block_limits_data block_limits;
1620 struct scsipi_periph *periph = st->sc_periph;
1621 int error;
1622
1623 /*
1624 * First check if we have it all loaded
1625 */
1626 if ((periph->periph_flags & PERIPH_MEDIA_LOADED))
1627 return (0);
1628
1629 /*
1630 * do a 'Read Block Limits'
1631 */
1632 bzero(&cmd, sizeof(cmd));
1633 cmd.opcode = READ_BLOCK_LIMITS;
1634
1635 /*
1636 * do the command, update the global values
1637 */
1638 error = scsipi_command(periph, (struct scsipi_generic *)&cmd,
1639 sizeof(cmd), (u_char *)&block_limits, sizeof(block_limits),
1640 ST_RETRIES, ST_CTL_TIME, NULL,
1641 flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
1642 if (error)
1643 return (error);
1644
1645 st->blkmin = _2btol(block_limits.min_length);
1646 st->blkmax = _3btol(block_limits.max_length);
1647
1648 SC_DEBUG(periph, SCSIPI_DB3,
1649 ("(%d <= blksize <= %d)\n", st->blkmin, st->blkmax));
1650 return (0);
1651 }
1652
1653 /*
1654 * Get the scsi driver to send a full inquiry to the
1655 * device and use the results to fill out the global
1656 * parameter structure.
1657 *
1658 * called from:
1659 * attach
1660 * open
1661 * ioctl (to reset original blksize)
1662 */
1663 int
1664 st_mode_sense(st, flags)
1665 struct st_softc *st;
1666 int flags;
1667 {
1668 u_int scsipi_sense_len;
1669 int error;
1670 struct scsi_mode_sense cmd;
1671 struct scsipi_sense {
1672 struct scsi_mode_header header;
1673 struct scsi_blk_desc blk_desc;
1674 u_char sense_data[MAX_PAGE_0_SIZE];
1675 } scsipi_sense;
1676 struct scsipi_periph *periph = st->sc_periph;
1677
1678 scsipi_sense_len = 12 + st->page_0_size;
1679
1680 /*
1681 * Set up a mode sense
1682 */
1683 bzero(&cmd, sizeof(cmd));
1684 cmd.opcode = SCSI_MODE_SENSE;
1685 cmd.length = scsipi_sense_len;
1686
1687 /*
1688 * do the command, but we don't need the results
1689 * just print them for our interest's sake, if asked,
1690 * or if we need it as a template for the mode select
1691 * store it away.
1692 */
1693 error = scsipi_command(periph, (struct scsipi_generic *)&cmd,
1694 sizeof(cmd), (u_char *)&scsipi_sense, scsipi_sense_len,
1695 ST_RETRIES, ST_CTL_TIME, NULL,
1696 flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
1697 if (error)
1698 return (error);
1699
1700 st->numblks = _3btol(scsipi_sense.blk_desc.nblocks);
1701 st->media_blksize = _3btol(scsipi_sense.blk_desc.blklen);
1702 st->media_density = scsipi_sense.blk_desc.density;
1703 if (scsipi_sense.header.dev_spec & SMH_DSP_WRITE_PROT)
1704 st->flags |= ST_READONLY;
1705 else
1706 st->flags &= ~ST_READONLY;
1707 SC_DEBUG(periph, SCSIPI_DB3,
1708 ("density code %d, %d-byte blocks, write-%s, ",
1709 st->media_density, st->media_blksize,
1710 st->flags & ST_READONLY ? "protected" : "enabled"));
1711 SC_DEBUG(periph, SCSIPI_DB3,
1712 ("%sbuffered\n",
1713 scsipi_sense.header.dev_spec & SMH_DSP_BUFF_MODE ? "" : "un"));
1714 if (st->page_0_size)
1715 bcopy(scsipi_sense.sense_data, st->sense_data,
1716 st->page_0_size);
1717 periph->periph_flags |= PERIPH_MEDIA_LOADED;
1718 return (0);
1719 }
1720
1721 /*
1722 * Send a filled out parameter structure to the drive to
1723 * set it into the desire modes etc.
1724 */
1725 int
1726 st_mode_select(st, flags)
1727 struct st_softc *st;
1728 int flags;
1729 {
1730 u_int scsi_select_len;
1731 struct scsi_mode_select cmd;
1732 struct scsi_select {
1733 struct scsi_mode_header header;
1734 struct scsi_blk_desc blk_desc;
1735 u_char sense_data[MAX_PAGE_0_SIZE];
1736 } scsi_select;
1737 struct scsipi_periph *periph = st->sc_periph;
1738
1739 scsi_select_len = 12 + st->page_0_size;
1740
1741 /*
1742 * This quirk deals with drives that have only one valid mode
1743 * and think this gives them license to reject all mode selects,
1744 * even if the selected mode is the one that is supported.
1745 */
1746 if (st->quirks & ST_Q_UNIMODAL) {
1747 SC_DEBUG(periph, SCSIPI_DB3,
1748 ("not setting density 0x%x blksize 0x%x\n",
1749 st->density, st->blksize));
1750 return (0);
1751 }
1752
1753 /*
1754 * Set up for a mode select
1755 */
1756 bzero(&cmd, sizeof(cmd));
1757 cmd.opcode = SCSI_MODE_SELECT;
1758 cmd.length = scsi_select_len;
1759
1760 bzero(&scsi_select, scsi_select_len);
1761 scsi_select.header.blk_desc_len = sizeof(struct scsi_blk_desc);
1762 scsi_select.header.dev_spec &= ~SMH_DSP_BUFF_MODE;
1763 scsi_select.blk_desc.density = st->density;
1764 if (st->flags & ST_DONTBUFFER)
1765 scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_OFF;
1766 else
1767 scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_ON;
1768 if (st->flags & ST_FIXEDBLOCKS)
1769 _lto3b(st->blksize, scsi_select.blk_desc.blklen);
1770 if (st->page_0_size)
1771 bcopy(st->sense_data, scsi_select.sense_data, st->page_0_size);
1772
1773 /*
1774 * do the command
1775 */
1776 return (scsipi_command(periph, (struct scsipi_generic *)&cmd,
1777 sizeof(cmd), (u_char *)&scsi_select, scsi_select_len,
1778 ST_RETRIES, ST_CTL_TIME, NULL,
1779 flags | XS_CTL_DATA_OUT | XS_CTL_DATA_ONSTACK));
1780 }
1781
1782 int
1783 st_cmprss(st, onoff)
1784 struct st_softc *st;
1785 int onoff;
1786 {
1787 u_int scsi_dlen;
1788 struct scsi_mode_select mcmd;
1789 struct scsi_mode_sense scmd;
1790 struct scsi_select {
1791 struct scsi_mode_header header;
1792 struct scsi_blk_desc blk_desc;
1793 u_char pdata[max(sizeof(struct scsi_tape_dev_conf_page),
1794 sizeof(struct scsi_tape_dev_compression_page))];
1795 } scsi_pdata;
1796 struct scsi_tape_dev_conf_page *ptr;
1797 struct scsi_tape_dev_compression_page *cptr;
1798 struct scsipi_periph *periph = st->sc_periph;
1799 int error, ison, flags;
1800
1801 scsi_dlen = sizeof(scsi_pdata);
1802 bzero(&scsi_pdata, scsi_dlen);
1803
1804 /*
1805 * Set up for a mode sense.
1806 * Do DATA COMPRESSION page first.
1807 */
1808 bzero(&scmd, sizeof(scmd));
1809 scmd.opcode = SCSI_MODE_SENSE;
1810 scmd.page = SMS_PAGE_CTRL_CURRENT | 0xf;
1811 scmd.length = scsi_dlen;
1812
1813 flags = XS_CTL_SILENT;
1814
1815 /*
1816 * Do the MODE SENSE command...
1817 */
1818 again:
1819 bzero(&scsi_pdata, scsi_dlen);
1820 error = scsipi_command(periph,
1821 (struct scsipi_generic *)&scmd, sizeof(scmd),
1822 (u_char *)&scsi_pdata, scsi_dlen,
1823 ST_RETRIES, ST_CTL_TIME, NULL,
1824 flags | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
1825
1826 if (error) {
1827 if (scmd.byte2 != SMS_DBD) {
1828 scmd.byte2 = SMS_DBD;
1829 goto again;
1830 }
1831 /*
1832 * Try a different page?
1833 */
1834 if (scmd.page == (SMS_PAGE_CTRL_CURRENT | 0xf)) {
1835 scmd.page = SMS_PAGE_CTRL_CURRENT | 0x10;
1836 scmd.byte2 = 0;
1837 goto again;
1838 }
1839 return (error);
1840 }
1841
1842 if (scsi_pdata.header.blk_desc_len)
1843 ptr = (struct scsi_tape_dev_conf_page *) scsi_pdata.pdata;
1844 else
1845 ptr = (struct scsi_tape_dev_conf_page *) &scsi_pdata.blk_desc;
1846
1847 if ((scmd.page & SMS_PAGE_CODE) == 0xf) {
1848 cptr = (struct scsi_tape_dev_compression_page *) ptr;
1849 ison = (cptr->dce_dcc & DCP_DCE) != 0;
1850 if (onoff)
1851 cptr->dce_dcc |= DCP_DCE;
1852 else
1853 cptr->dce_dcc &= ~DCP_DCE;
1854 cptr->pagecode &= ~0x80;
1855 } else {
1856 ison = (ptr->sel_comp_alg != 0);
1857 if (onoff)
1858 ptr->sel_comp_alg = 1;
1859 else
1860 ptr->sel_comp_alg = 0;
1861 ptr->pagecode &= ~0x80;
1862 ptr->byte2 = 0;
1863 ptr->active_partition = 0;
1864 ptr->wb_full_ratio = 0;
1865 ptr->rb_empty_ratio = 0;
1866 ptr->byte8 &= ~0x30;
1867 ptr->gap_size = 0;
1868 ptr->byte10 &= ~0xe7;
1869 ptr->ew_bufsize[0] = 0;
1870 ptr->ew_bufsize[1] = 0;
1871 ptr->ew_bufsize[2] = 0;
1872 ptr->reserved = 0;
1873 }
1874 onoff = onoff ? 1 : 0;
1875 /*
1876 * There might be a virtue in actually doing the MODE SELECTS,
1877 * but let's not clog the bus over it.
1878 */
1879 if (onoff == ison)
1880 return (0);
1881
1882 /*
1883 * Set up for a mode select
1884 */
1885
1886 scsi_pdata.header.data_length = 0;
1887 scsi_pdata.header.medium_type = 0;
1888 if ((st->flags & ST_DONTBUFFER) == 0)
1889 scsi_pdata.header.dev_spec = SMH_DSP_BUFF_MODE_ON;
1890 else
1891 scsi_pdata.header.dev_spec = 0;
1892
1893 bzero(&mcmd, sizeof(mcmd));
1894 mcmd.opcode = SCSI_MODE_SELECT;
1895 mcmd.byte2 = SMS_PF;
1896 mcmd.length = scsi_dlen;
1897 if (scsi_pdata.header.blk_desc_len) {
1898 scsi_pdata.blk_desc.density = 0;
1899 scsi_pdata.blk_desc.nblocks[0] = 0;
1900 scsi_pdata.blk_desc.nblocks[1] = 0;
1901 scsi_pdata.blk_desc.nblocks[2] = 0;
1902 }
1903
1904 /*
1905 * Do the command
1906 */
1907 error = scsipi_command(periph,
1908 (struct scsipi_generic *)&mcmd, sizeof(mcmd),
1909 (u_char *)&scsi_pdata, scsi_dlen,
1910 ST_RETRIES, ST_CTL_TIME, NULL,
1911 flags | XS_CTL_DATA_OUT | XS_CTL_DATA_ONSTACK);
1912
1913 if (error && (scmd.page & SMS_PAGE_CODE) == 0xf) {
1914 /*
1915 * Try DEVICE CONFIGURATION page.
1916 */
1917 scmd.page = SMS_PAGE_CTRL_CURRENT | 0x10;
1918 goto again;
1919 }
1920 return (error);
1921 }
1922 /*
1923 * issue an erase command
1924 */
1925 int
1926 st_erase(st, full, flags)
1927 struct st_softc *st;
1928 int full, flags;
1929 {
1930 int tmo;
1931 struct scsi_erase cmd;
1932
1933 /*
1934 * Full erase means set LONG bit in erase command, which asks
1935 * the drive to erase the entire unit. Without this bit, we're
1936 * asking the drive to write an erase gap.
1937 */
1938 bzero(&cmd, sizeof(cmd));
1939 cmd.opcode = ERASE;
1940 if (full) {
1941 cmd.byte2 = SE_IMMED|SE_LONG;
1942 tmo = ST_SPC_TIME;
1943 } else {
1944 tmo = ST_IO_TIME;
1945 cmd.byte2 = SE_IMMED;
1946 }
1947
1948 /*
1949 * XXX We always do this asynchronously, for now. How long should
1950 * we wait if we want to (eventually) to it synchronously?
1951 */
1952 return (scsipi_command(st->sc_periph,
1953 (struct scsipi_generic *)&cmd, sizeof(cmd),
1954 0, 0, ST_RETRIES, tmo, NULL, flags));
1955 }
1956
1957 /*
1958 * skip N blocks/filemarks/seq filemarks/eom
1959 */
1960 int
1961 st_space(st, number, what, flags)
1962 struct st_softc *st;
1963 u_int what;
1964 int flags;
1965 int number;
1966 {
1967 struct scsi_space cmd;
1968 int error;
1969
1970 switch (what) {
1971 case SP_BLKS:
1972 if (st->flags & ST_PER_ACTION) {
1973 if (number > 0) {
1974 st->flags &= ~ST_PER_ACTION;
1975 return (EIO);
1976 } else if (number < 0) {
1977 if (st->flags & ST_AT_FILEMARK) {
1978 /*
1979 * Handling of ST_AT_FILEMARK
1980 * in st_space will fill in the
1981 * right file mark count.
1982 */
1983 error = st_space(st, 0, SP_FILEMARKS,
1984 flags);
1985 if (error)
1986 return (error);
1987 }
1988 if (st->flags & ST_BLANK_READ) {
1989 st->flags &= ~ST_BLANK_READ;
1990 return (EIO);
1991 }
1992 st->flags &= ~(ST_EIO_PENDING|ST_EOM_PENDING);
1993 }
1994 }
1995 break;
1996 case SP_FILEMARKS:
1997 if (st->flags & ST_EIO_PENDING) {
1998 if (number > 0) {
1999 /* pretend we just discovered the error */
2000 st->flags &= ~ST_EIO_PENDING;
2001 return (EIO);
2002 } else if (number < 0) {
2003 /* back away from the error */
2004 st->flags &= ~ST_EIO_PENDING;
2005 }
2006 }
2007 if (st->flags & ST_AT_FILEMARK) {
2008 st->flags &= ~ST_AT_FILEMARK;
2009 number--;
2010 }
2011 if ((st->flags & ST_BLANK_READ) && (number < 0)) {
2012 /* back away from unwritten tape */
2013 st->flags &= ~ST_BLANK_READ;
2014 number++; /* XXX dubious */
2015 }
2016 break;
2017 case SP_EOM:
2018 if (st->flags & ST_EOM_PENDING) {
2019 /* we're already there */
2020 st->flags &= ~ST_EOM_PENDING;
2021 return (0);
2022 }
2023 if (st->flags & ST_EIO_PENDING) {
2024 /* pretend we just discovered the error */
2025 st->flags &= ~ST_EIO_PENDING;
2026 return (EIO);
2027 }
2028 if (st->flags & ST_AT_FILEMARK)
2029 st->flags &= ~ST_AT_FILEMARK;
2030 break;
2031 }
2032 if (number == 0)
2033 return (0);
2034
2035 bzero(&cmd, sizeof(cmd));
2036 cmd.opcode = SPACE;
2037 cmd.byte2 = what;
2038 _lto3b(number, cmd.number);
2039
2040 return (scsipi_command(st->sc_periph,
2041 (struct scsipi_generic *)&cmd, sizeof(cmd),
2042 0, 0, 0, ST_SPC_TIME, NULL, flags));
2043 }
2044
2045 /*
2046 * write N filemarks
2047 */
2048 int
2049 st_write_filemarks(st, number, flags)
2050 struct st_softc *st;
2051 int flags;
2052 int number;
2053 {
2054 struct scsi_write_filemarks cmd;
2055
2056 /*
2057 * It's hard to write a negative number of file marks.
2058 * Don't try.
2059 */
2060 if (number < 0)
2061 return (EINVAL);
2062 switch (number) {
2063 case 0: /* really a command to sync the drive's buffers */
2064 break;
2065 case 1:
2066 if (st->flags & ST_FM_WRITTEN) /* already have one down */
2067 st->flags &= ~ST_WRITTEN;
2068 else
2069 st->flags |= ST_FM_WRITTEN;
2070 st->flags &= ~ST_PER_ACTION;
2071 break;
2072 default:
2073 st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
2074 }
2075
2076 bzero(&cmd, sizeof(cmd));
2077 cmd.opcode = WRITE_FILEMARKS;
2078 _lto3b(number, cmd.number);
2079
2080 return (scsipi_command(st->sc_periph,
2081 (struct scsipi_generic *)&cmd, sizeof(cmd),
2082 0, 0, 0, ST_IO_TIME * 4, NULL, flags));
2083 }
2084
2085 /*
2086 * Make sure the right number of file marks is on tape if the
2087 * tape has been written. If the position argument is true,
2088 * leave the tape positioned where it was originally.
2089 *
2090 * nmarks returns the number of marks to skip (or, if position
2091 * true, which were skipped) to get back original position.
2092 */
2093 int
2094 st_check_eod(st, position, nmarks, flags)
2095 struct st_softc *st;
2096 boolean position;
2097 int *nmarks;
2098 int flags;
2099 {
2100 int error;
2101
2102 switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
2103 default:
2104 *nmarks = 0;
2105 return (0);
2106 case ST_WRITTEN:
2107 case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
2108 *nmarks = 1;
2109 break;
2110 case ST_WRITTEN | ST_2FM_AT_EOD:
2111 *nmarks = 2;
2112 }
2113 error = st_write_filemarks(st, *nmarks, flags);
2114 if (position && !error)
2115 error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
2116 return (error);
2117 }
2118
2119 /*
2120 * load/unload/retension
2121 */
2122 int
2123 st_load(st, type, flags)
2124 struct st_softc *st;
2125 u_int type;
2126 int flags;
2127 {
2128 int error;
2129 struct scsi_load cmd;
2130
2131 if (type != LD_LOAD) {
2132 int nmarks;
2133
2134 error = st_check_eod(st, FALSE, &nmarks, flags);
2135 if (error) {
2136 printf("%s: failed to write closing filemarks at "
2137 "unload, errno=%d\n", st->sc_dev.dv_xname, error);
2138 return (error);
2139 }
2140 }
2141 if (st->quirks & ST_Q_IGNORE_LOADS) {
2142 if (type == LD_LOAD) {
2143 /*
2144 * If we ignore loads, at least we should try a rewind.
2145 */
2146 return st_rewind(st, 0, flags);
2147 }
2148 /* otherwise, we should do what's asked of us */
2149 }
2150
2151 bzero(&cmd, sizeof(cmd));
2152 cmd.opcode = LOAD;
2153 cmd.how = type;
2154
2155 error = scsipi_command(st->sc_periph,
2156 (struct scsipi_generic *)&cmd, sizeof(cmd),
2157 0, 0, ST_RETRIES, ST_SPC_TIME, NULL, flags);
2158 if (error) {
2159 printf("%s: error %d in st_load (op %d)\n",
2160 st->sc_dev.dv_xname, error, type);
2161 }
2162 return (error);
2163 }
2164
2165 /*
2166 * Rewind the device
2167 */
2168 int
2169 st_rewind(st, immediate, flags)
2170 struct st_softc *st;
2171 u_int immediate;
2172 int flags;
2173 {
2174 struct scsi_rewind cmd;
2175 int error;
2176 int nmarks;
2177
2178 error = st_check_eod(st, FALSE, &nmarks, flags);
2179 if (error) {
2180 printf("%s: failed to write closing filemarks at "
2181 "rewind, errno=%d\n", st->sc_dev.dv_xname, error);
2182 return (error);
2183 }
2184 st->flags &= ~ST_PER_ACTION;
2185
2186 bzero(&cmd, sizeof(cmd));
2187 cmd.opcode = REWIND;
2188 cmd.byte2 = immediate;
2189
2190 error = scsipi_command(st->sc_periph,
2191 (struct scsipi_generic *)&cmd, sizeof(cmd), 0, 0, ST_RETRIES,
2192 immediate ? ST_CTL_TIME: ST_SPC_TIME, NULL, flags);
2193 if (error) {
2194 printf("%s: error %d trying to rewind\n",
2195 st->sc_dev.dv_xname, error);
2196 }
2197 return (error);
2198 }
2199
2200 int
2201 st_rdpos(st, hard, blkptr)
2202 struct st_softc *st;
2203 int hard;
2204 u_int32_t *blkptr;
2205 {
2206 int error;
2207 u_int8_t posdata[20];
2208 struct scsi_tape_read_position cmd;
2209
2210 /*
2211 * First flush any pending writes...
2212 */
2213 error = st_write_filemarks(st, 0, XS_CTL_SILENT);
2214
2215 /*
2216 * The latter case is for 'write protected' tapes
2217 * which are too stupid to recognize a zero count
2218 * for writing filemarks as a no-op.
2219 */
2220 if (error != 0 && error != EACCES && error != EROFS)
2221 return (error);
2222
2223 bzero(&cmd, sizeof(cmd));
2224 bzero(&posdata, sizeof(posdata));
2225 cmd.opcode = READ_POSITION;
2226 if (hard)
2227 cmd.byte1 = 1;
2228
2229 error = scsipi_command(st->sc_periph,
2230 (struct scsipi_generic *)&cmd, sizeof(cmd), (u_char *)&posdata,
2231 sizeof(posdata), ST_RETRIES, ST_CTL_TIME, NULL,
2232 XS_CTL_SILENT | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
2233
2234 if (error == 0) {
2235 #if 0
2236 printf("posdata:");
2237 for (hard = 0; hard < sizeof(posdata); hard++)
2238 printf("%02x ", posdata[hard] & 0xff);
2239 printf("\n");
2240 #endif
2241 if (posdata[0] & 0x4) /* Block Position Unknown */
2242 error = EINVAL;
2243 else
2244 *blkptr = _4btol(&posdata[4]);
2245 }
2246 return (error);
2247 }
2248
2249 int
2250 st_setpos(st, hard, blkptr)
2251 struct st_softc *st;
2252 int hard;
2253 u_int32_t *blkptr;
2254 {
2255 int error;
2256 struct scsi_tape_locate cmd;
2257
2258 /*
2259 * First flush any pending writes. Strictly speaking,
2260 * we're not supposed to have to worry about this,
2261 * but let's be untrusting.
2262 */
2263 error = st_write_filemarks(st, 0, XS_CTL_SILENT);
2264
2265 /*
2266 * The latter case is for 'write protected' tapes
2267 * which are too stupid to recognize a zero count
2268 * for writing filemarks as a no-op.
2269 */
2270 if (error != 0 && error != EACCES && error != EROFS)
2271 return (error);
2272
2273 bzero(&cmd, sizeof(cmd));
2274 cmd.opcode = LOCATE;
2275 if (hard)
2276 cmd.byte2 = 1 << 2;
2277 _lto4b(*blkptr, cmd.blkaddr);
2278 error = scsipi_command(st->sc_periph,
2279 (struct scsipi_generic *)&cmd, sizeof(cmd),
2280 NULL, 0, ST_RETRIES, ST_SPC_TIME, NULL, 0);
2281 /*
2282 * XXX: Note file && block number position now unknown (if
2283 * XXX: these things ever start being maintained in this driver)
2284 */
2285 return (error);
2286 }
2287
2288
2289 /*
2290 * Look at the returned sense and act on the error and determine
2291 * the unix error number to pass back..., 0 (== report no error),
2292 * -1 = retry the operation, -2 continue error processing.
2293 */
2294 int
2295 st_interpret_sense(xs)
2296 struct scsipi_xfer *xs;
2297 {
2298 struct scsipi_periph *periph = xs->xs_periph;
2299 struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
2300 struct buf *bp = xs->bp;
2301 struct st_softc *st = (void *)periph->periph_dev;
2302 int retval = EJUSTRETURN;
2303 int doprint = ((xs->xs_control & XS_CTL_SILENT) == 0);
2304 u_int8_t key;
2305 int32_t info;
2306
2307 /*
2308 * If it isn't a extended or extended/deferred error, let
2309 * the generic code handle it.
2310 */
2311 if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
2312 (sense->error_code & SSD_ERRCODE) != 0x71) { /* DEFFERRED */
2313 return (retval);
2314 }
2315
2316 if (sense->error_code & SSD_ERRCODE_VALID)
2317 info = _4btol(sense->info);
2318 else
2319 info = xs->datalen; /* bad choice if fixed blocks */
2320 key = sense->flags & SSD_KEY;
2321 st->mt_erreg = key;
2322 st->asc = sense->add_sense_code;
2323 st->ascq = sense->add_sense_code_qual;
2324 st->mt_resid = (short) info;
2325
2326 /*
2327 * If the device is not open yet, let generic handle
2328 */
2329 if ((periph->periph_flags & PERIPH_OPEN) == 0) {
2330 return (retval);
2331 }
2332
2333
2334 if (st->flags & ST_FIXEDBLOCKS) {
2335 xs->resid = info * st->blksize;
2336 if (sense->flags & SSD_EOM) {
2337 if ((st->flags & ST_EARLYWARN) == 0)
2338 st->flags |= ST_EIO_PENDING;
2339 st->flags |= ST_EOM_PENDING;
2340 if (bp)
2341 bp->b_resid = xs->resid;
2342 }
2343 if (sense->flags & SSD_FILEMARK) {
2344 st->flags |= ST_AT_FILEMARK;
2345 if (bp)
2346 bp->b_resid = xs->resid;
2347 }
2348 if (sense->flags & SSD_ILI) {
2349 st->flags |= ST_EIO_PENDING;
2350 if (bp)
2351 bp->b_resid = xs->resid;
2352 if (sense->error_code & SSD_ERRCODE_VALID &&
2353 (xs->xs_control & XS_CTL_SILENT) == 0)
2354 printf("%s: block wrong size, %d blocks "
2355 "residual\n", st->sc_dev.dv_xname, info);
2356
2357 /*
2358 * This quirk code helps the drive read
2359 * the first tape block, regardless of
2360 * format. That is required for these
2361 * drives to return proper MODE SENSE
2362 * information.
2363 */
2364 if ((st->quirks & ST_Q_SENSE_HELP) &&
2365 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
2366 st->blksize -= 512;
2367 }
2368 /*
2369 * If data wanted and no data was tranfered, do it immediatly
2370 */
2371 if (xs->datalen && xs->resid >= xs->datalen) {
2372 if (st->flags & ST_EIO_PENDING)
2373 return (EIO);
2374 if (st->flags & ST_AT_FILEMARK) {
2375 if (bp)
2376 bp->b_resid = xs->resid;
2377 return (0);
2378 }
2379 }
2380 } else { /* must be variable mode */
2381 if (sense->flags & SSD_EOM) {
2382 /*
2383 * The current semantics of this
2384 * driver requires EOM detection
2385 * to return EIO unless early
2386 * warning detection is enabled
2387 * for variable mode (this is always
2388 * on for fixed block mode).
2389 */
2390 if (st->flags & ST_EARLYWARN) {
2391 st->flags |= ST_EOM_PENDING;
2392 retval = 0;
2393 } else {
2394 retval = EIO;
2395 }
2396
2397 /*
2398 * If it's an unadorned EOM detection,
2399 * suppress printing an error.
2400 */
2401 if (key == SKEY_NO_SENSE) {
2402 doprint = 0;
2403 }
2404 } else if (sense->flags & SSD_FILEMARK) {
2405 retval = 0;
2406 } else if (sense->flags & SSD_ILI) {
2407 if (info < 0) {
2408 /*
2409 * The tape record was bigger than the read
2410 * we issued.
2411 */
2412 if ((xs->xs_control & XS_CTL_SILENT) == 0) {
2413 printf("%s: %d-byte tape record too big"
2414 " for %d-byte user buffer\n",
2415 st->sc_dev.dv_xname,
2416 xs->datalen - info, xs->datalen);
2417 }
2418 retval = EIO;
2419 } else {
2420 retval = 0;
2421 }
2422 }
2423 xs->resid = info;
2424 if (bp)
2425 bp->b_resid = info;
2426 }
2427
2428 #ifdef SCSIPI_DEBUG
2429 if (retval == 0 && key == SKEY_NO_SENSE)
2430 doprint = 0;
2431 #endif
2432 if (key == SKEY_BLANK_CHECK) {
2433 /*
2434 * This quirk code helps the drive read the
2435 * first tape block, regardless of format. That
2436 * is required for these drives to return proper
2437 * MODE SENSE information.
2438 */
2439 if ((st->quirks & ST_Q_SENSE_HELP) &&
2440 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
2441 /* still starting */
2442 st->blksize -= 512;
2443 } else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
2444 st->flags |= ST_BLANK_READ;
2445 xs->resid = xs->datalen;
2446 if (bp) {
2447 bp->b_resid = xs->resid;
2448 /* return an EOF */
2449 }
2450 retval = 0;
2451 }
2452 }
2453 #ifdef SCSIVERBOSE
2454 if (doprint) {
2455 scsipi_print_sense(xs, 0);
2456 }
2457 #else
2458 if (doprint) {
2459 scsipi_printaddr(periph);
2460 printf("Sense Key 0x%02x", key);
2461 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
2462 switch (key) {
2463 case SKEY_NOT_READY:
2464 case SKEY_ILLEGAL_REQUEST:
2465 case SKEY_UNIT_ATTENTION:
2466 case SKEY_WRITE_PROTECT:
2467 break;
2468 case SKEY_BLANK_CHECK:
2469 printf(", requested size: %d (decimal)", info);
2470 break;
2471 case SKEY_ABORTED_COMMAND:
2472 if (xs->retries)
2473 printf(", retrying");
2474 printf(", cmd 0x%x, info 0x%x",
2475 xs->cmd->opcode, info);
2476 break;
2477 default:
2478 printf(", info = %d (decimal)", info);
2479 }
2480 }
2481 if (sense->extra_len != 0) {
2482 int n;
2483 printf(", data =");
2484 for (n = 0; n < sense->extra_len; n++)
2485 printf(" %02x", sense->cmd_spec_info[n]);
2486 }
2487 printf("\n");
2488 }
2489 #endif
2490 return (retval);
2491 }
2492
2493 /*
2494 * The quirk here is that the drive returns some value to st_mode_sense
2495 * incorrectly until the tape has actually passed by the head.
2496 *
2497 * The method is to set the drive to large fixed-block state (user-specified
2498 * density and 1024-byte blocks), then read and rewind to get it to sense the
2499 * tape. If that doesn't work, try 512-byte fixed blocks. If that doesn't
2500 * work, as a last resort, try variable- length blocks. The result will be
2501 * the ability to do an accurate st_mode_sense.
2502 *
2503 * We know we can do a rewind because we just did a load, which implies rewind.
2504 * Rewind seems preferable to space backward if we have a virgin tape.
2505 *
2506 * The rest of the code for this quirk is in ILI processing and BLANK CHECK
2507 * error processing, both part of st_interpret_sense.
2508 */
2509 int
2510 st_touch_tape(st)
2511 struct st_softc *st;
2512 {
2513 char *buf;
2514 int readsize;
2515 int error;
2516
2517 buf = malloc(1024, M_TEMP, M_NOWAIT);
2518 if (buf == NULL)
2519 return (ENOMEM);
2520
2521 if ((error = st_mode_sense(st, 0)) != 0)
2522 goto bad;
2523 st->blksize = 1024;
2524 do {
2525 switch (st->blksize) {
2526 case 512:
2527 case 1024:
2528 readsize = st->blksize;
2529 st->flags |= ST_FIXEDBLOCKS;
2530 break;
2531 default:
2532 readsize = 1;
2533 st->flags &= ~ST_FIXEDBLOCKS;
2534 }
2535 if ((error = st_mode_select(st, 0)) != 0)
2536 goto bad;
2537 st_read(st, buf, readsize, XS_CTL_SILENT); /* XXX */
2538 if ((error = st_rewind(st, 0, 0)) != 0) {
2539 bad: free(buf, M_TEMP);
2540 return (error);
2541 }
2542 } while (readsize != 1 && readsize > st->blksize);
2543
2544 free(buf, M_TEMP);
2545 return (0);
2546 }
2547
2548 int
2549 stdump(dev, blkno, va, size)
2550 dev_t dev;
2551 daddr_t blkno;
2552 caddr_t va;
2553 size_t size;
2554 {
2555
2556 /* Not implemented. */
2557 return (ENXIO);
2558 }
2559