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