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