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