st.c revision 1.51 1 /* $NetBSD: st.c,v 1.51 1995/08/12 20:31:50 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1994 Charles Hannum. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Charles Hannum.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Originally written by Julian Elischer (julian (at) tfs.com)
34 * for TRW Financial Systems for use under the MACH(2.5) operating system.
35 *
36 * TRW Financial Systems, in accordance with their agreement with Carnegie
37 * Mellon University, makes this software available to CMU to distribute
38 * or use in any manner that they see fit as long as this message is kept with
39 * the software. For this reason TFS also grants any other persons or
40 * organisations permission to use or modify this software.
41 *
42 * TFS supplies this software to be publicly redistributed
43 * on the understanding that TFS is not responsible for the correct
44 * functioning of this software in any circumstances.
45 *
46 * Ported to run under 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
47 * major changes by Julian Elischer (julian (at) jules.dialix.oz.au) May 1993
48 */
49
50 /*
51 * To do:
52 * work out some better way of guessing what a good timeout is going
53 * to be depending on whether we expect to retension or not.
54 */
55
56 #include <sys/types.h>
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/fcntl.h>
60 #include <sys/errno.h>
61 #include <sys/ioctl.h>
62 #include <sys/malloc.h>
63 #include <sys/buf.h>
64 #include <sys/proc.h>
65 #include <sys/user.h>
66 #include <sys/mtio.h>
67 #include <sys/device.h>
68
69 #include <scsi/scsi_all.h>
70 #include <scsi/scsi_tape.h>
71 #include <scsi/scsiconf.h>
72
73 /* Defines for device specific stuff */
74 #define DEF_FIXED_BSIZE 512
75 #define ST_RETRIES 4 /* only on non IO commands */
76
77 #define STMODE(z) ( minor(z) & 0x03)
78 #define STDSTY(z) ((minor(z) >> 2) & 0x03)
79 #define STUNIT(z) ((minor(z) >> 4) )
80 #define CTLMODE 3
81
82 #define SCSI_2_MAX_DENSITY_CODE 0x17 /* maximum density code specified
83 * in SCSI II spec. */
84 /*
85 * Define various devices that we know mis-behave in some way,
86 * and note how they are bad, so we can correct for them
87 */
88 struct modes {
89 u_int quirks; /* same definitions as in quirkdata */
90 int blksize;
91 u_int8_t density;
92 };
93
94 struct quirkdata {
95 u_int quirks;
96 #define ST_Q_FORCE_BLKSIZE 0x0001
97 #define ST_Q_SENSE_HELP 0x0002 /* must do READ for good MODE SENSE */
98 #define ST_Q_IGNORE_LOADS 0x0004
99 #define ST_Q_BLKSIZE 0x0008 /* variable-block media_blksize > 0 */
100 u_int page_0_size;
101 #define MAX_PAGE_0_SIZE 64
102 struct modes modes[4];
103 };
104
105 struct st_quirk_inquiry_pattern {
106 struct scsi_inquiry_pattern pattern;
107 struct quirkdata quirkdata;
108 };
109
110 struct st_quirk_inquiry_pattern st_quirk_patterns[] = {
111 {T_SEQUENTIAL, T_REMOV,
112 " ", " ", " ", 0, 0, {
113 ST_Q_FORCE_BLKSIZE, 512, 0, /* minor 0-3 */
114 ST_Q_FORCE_BLKSIZE, 512, QIC_24, /* minor 4-7 */
115 ST_Q_FORCE_BLKSIZE, 0, HALFINCH_1600, /* minor 8-11 */
116 ST_Q_FORCE_BLKSIZE, 0, HALFINCH_6250 /* minor 12-15 */
117 }},
118 {T_SEQUENTIAL, T_REMOV,
119 "TANDBERG", " TDC 3600 ", "", 0, 12, {
120 0, 0, 0, /* minor 0-3 */
121 ST_Q_FORCE_BLKSIZE, 0, QIC_525, /* minor 4-7 */
122 0, 0, QIC_150, /* minor 8-11 */
123 0, 0, QIC_120 /* minor 12-15 */
124 }},
125 /*
126 * At least -005 and -007 need this. I'll assume they all do unless I
127 * hear otherwise. - mycroft, 31MAR1994
128 */
129 {T_SEQUENTIAL, T_REMOV,
130 "ARCHIVE ", "VIPER 2525 25462", "", 0, 0, {
131 ST_Q_SENSE_HELP, 0, 0, /* minor 0-3 */
132 ST_Q_SENSE_HELP, 0, QIC_525, /* minor 4-7 */
133 0, 0, QIC_150, /* minor 8-11 */
134 0, 0, QIC_120 /* minor 12-15 */
135 }},
136 /*
137 * One user reports that this works for his tape drive. It probably
138 * needs more work. - mycroft, 09APR1994
139 */
140 {T_SEQUENTIAL, T_REMOV,
141 "SANKYO ", "CP525 ", "", 0, 0, {
142 ST_Q_FORCE_BLKSIZE, 512, 0, /* minor 0-3 */
143 ST_Q_FORCE_BLKSIZE, 512, QIC_525, /* minor 4-7 */
144 0, 0, QIC_150, /* minor 8-11 */
145 0, 0, QIC_120 /* minor 12-15 */
146 }},
147 {T_SEQUENTIAL, T_REMOV,
148 "ANRITSU ", "DMT780 ", "", 0, 0, {
149 ST_Q_FORCE_BLKSIZE, 512, 0, /* minor 0-3 */
150 ST_Q_FORCE_BLKSIZE, 512, QIC_525, /* minor 4-7 */
151 0, 0, QIC_150, /* minor 8-11 */
152 0, 0, QIC_120 /* minor 12-15 */
153 }},
154 {T_SEQUENTIAL, T_REMOV,
155 "ARCHIVE ", "VIPER 150 21247", "", 0, 12, {
156 0, 0, 0, /* minor 0-3 */
157 0, 0, QIC_150, /* minor 4-7 */
158 0, 0, QIC_120, /* minor 8-11 */
159 0, 0, QIC_24 /* minor 12-15 */
160 }},
161 {T_SEQUENTIAL, T_REMOV,
162 "WANGTEK ", "5525ES SCSI REV7", "", 0, 0, {
163 0, 0, 0, /* minor 0-3 */
164 ST_Q_BLKSIZE, 0, QIC_525, /* minor 4-7 */
165 0, 0, QIC_150, /* minor 8-11 */
166 0, 0, QIC_120 /* minor 12-15 */
167 }},
168 {T_SEQUENTIAL, T_REMOV,
169 "WangDAT ", "Model 1300 ", "", 0, 0, {
170 0, 0, 0, /* minor 0-3 */
171 ST_Q_FORCE_BLKSIZE, 512, DDS, /* minor 4-7 */
172 ST_Q_FORCE_BLKSIZE, 1024, DDS, /* minor 8-11 */
173 ST_Q_FORCE_BLKSIZE, 0, DDS /* minor 12-15 */
174 }},
175 #if 0
176 {T_SEQUENTIAL, T_REMOV,
177 "EXABYTE ", "EXB-8200 ", "", 0, 12, {
178 0, 0, 0, /* minor 0-3 */
179 0, 0, 0, /* minor 4-7 */
180 0, 0, 0, /* minor 8-11 */
181 0, 0, 0 /* minor 12-15 */
182 }},
183 #endif
184 };
185
186 #define NOEJECT 0
187 #define EJECT 1
188
189 struct st_softc {
190 struct device sc_dev;
191 /*--------------------present operating parameters, flags etc.----------------*/
192 int flags; /* see below */
193 u_int quirks; /* quirks for the open mode */
194 int blksize; /* blksize we are using */
195 u_int8_t density; /* present density */
196 u_int page_0_size; /* size of page 0 data */
197 u_int last_dsty; /* last density opened */
198 /*--------------------device/scsi parameters----------------------------------*/
199 struct scsi_link *sc_link; /* our link to the adpter etc. */
200 /*--------------------parameters reported by the device ----------------------*/
201 int blkmin; /* min blk size */
202 int blkmax; /* max blk size */
203 struct quirkdata *quirkdata; /* if we have a rogue entry */
204 /*--------------------parameters reported by the device for this media--------*/
205 u_long numblks; /* nominal blocks capacity */
206 int media_blksize; /* 0 if not ST_FIXEDBLOCKS */
207 u_int8_t media_density; /* this is what it said when asked */
208 /*--------------------quirks for the whole drive------------------------------*/
209 u_int drive_quirks; /* quirks of this drive */
210 /*--------------------How we should set up when opening each minor device----*/
211 struct modes modes[4]; /* plus more for each mode */
212 u_int8_t modeflags[4]; /* flags for the modes */
213 #define DENSITY_SET_BY_USER 0x01
214 #define DENSITY_SET_BY_QUIRK 0x02
215 #define BLKSIZE_SET_BY_USER 0x04
216 #define BLKSIZE_SET_BY_QUIRK 0x08
217 /*--------------------storage for sense data returned by the drive------------*/
218 u_char sense_data[MAX_PAGE_0_SIZE]; /*
219 * additional sense data needed
220 * for mode sense/select.
221 */
222 struct buf buf_queue; /* the queue of pending IO operations */
223 };
224
225 int stmatch __P((struct device *, void *, void *));
226 void stattach __P((struct device *, struct device *, void *));
227
228 struct cfdriver stcd = {
229 NULL, "st", stmatch, stattach, DV_TAPE, sizeof(struct st_softc)
230 };
231
232 int st_space __P((struct st_softc *, int number, u_int what, int flags));
233 int st_rewind __P((struct st_softc *, u_int immediate, int flags));
234 int st_mode_sense __P((struct st_softc *, int flags));
235 int st_decide_mode __P((struct st_softc *, boolean first_read));
236 int st_read_block_limits __P((struct st_softc *, int flags));
237 int st_touch_tape __P((struct st_softc *));
238 int st_write_filemarks __P((struct st_softc *, int number, int flags));
239 int st_load __P((struct st_softc *, u_int type, int flags));
240 int st_mode_select __P((struct st_softc *, int flags));
241 void ststrategy();
242 int st_check_eod();
243 void ststart();
244 void st_unmount();
245 int st_mount_tape();
246 void st_loadquirks();
247 void st_identify_drive();
248 int st_interpret_sense();
249
250 struct scsi_device st_switch = {
251 st_interpret_sense,
252 ststart,
253 NULL,
254 NULL,
255 };
256
257 #define ST_INFO_VALID 0x0001
258 #define ST_BLOCK_SET 0x0002 /* block size, mode set by ioctl */
259 #define ST_WRITTEN 0x0004 /* data have been written, EOD needed */
260 #define ST_FIXEDBLOCKS 0x0008
261 #define ST_AT_FILEMARK 0x0010
262 #define ST_EIO_PENDING 0x0020 /* we couldn't report it then (had data) */
263 #define ST_NEW_MOUNT 0x0040 /* still need to decide mode */
264 #define ST_READONLY 0x0080 /* st_mode_sense says write protected */
265 #define ST_FM_WRITTEN 0x0100 /*
266 * EOF file mark written -- used with
267 * ~ST_WRITTEN to indicate that multiple file
268 * marks have been written
269 */
270 #define ST_BLANK_READ 0x0200 /* BLANK CHECK encountered already */
271 #define ST_2FM_AT_EOD 0x0400 /* write 2 file marks at EOD */
272 #define ST_MOUNTED 0x0800 /* Device is presently mounted */
273
274 #define ST_PER_ACTION (ST_AT_FILEMARK | ST_EIO_PENDING | ST_BLANK_READ)
275 #define ST_PER_MOUNT (ST_INFO_VALID | ST_BLOCK_SET | ST_WRITTEN | \
276 ST_FIXEDBLOCKS | ST_READONLY | ST_FM_WRITTEN | \
277 ST_2FM_AT_EOD | ST_PER_ACTION)
278
279 struct scsi_inquiry_pattern st_patterns[] = {
280 {T_SEQUENTIAL, T_REMOV,
281 "", "", ""},
282 };
283
284 int
285 stmatch(parent, match, aux)
286 struct device *parent;
287 void *match, *aux;
288 {
289 struct cfdata *cf = match;
290 struct scsibus_attach_args *sa = aux;
291 int priority;
292
293 (void)scsi_inqmatch(sa->sa_inqbuf,
294 (caddr_t)st_patterns, sizeof(st_patterns)/sizeof(st_patterns[0]),
295 sizeof(st_patterns[0]), &priority);
296 return (priority);
297 }
298
299 /*
300 * The routine called by the low level scsi routine when it discovers
301 * A device suitable for this driver
302 */
303 void
304 stattach(parent, self, aux)
305 struct device *parent, *self;
306 void *aux;
307 {
308 struct st_softc *st = (void *)self;
309 struct scsibus_attach_args *sa = aux;
310 struct scsi_link *sc_link = sa->sa_sc_link;
311
312 SC_DEBUG(sc_link, SDEV_DB2, ("stattach: "));
313
314 /*
315 * Store information needed to contact our base driver
316 */
317 st->sc_link = sc_link;
318 sc_link->device = &st_switch;
319 sc_link->device_softc = st;
320 sc_link->openings = 1;
321
322 /*
323 * Check if the drive is a known criminal and take
324 * Any steps needed to bring it into line
325 */
326 st_identify_drive(st, sa->sa_inqbuf);
327
328 /*
329 * Use the subdriver to request information regarding
330 * the drive. We cannot use interrupts yet, so the
331 * request must specify this.
332 */
333 printf(": %s", st->quirkdata ? "rogue, " : "");
334 if (scsi_test_unit_ready(sc_link,
335 SCSI_AUTOCONF | SCSI_SILENT | SCSI_IGNORE_MEDIA_CHANGE) ||
336 st_mode_sense(st,
337 SCSI_AUTOCONF | SCSI_SILENT | SCSI_IGNORE_MEDIA_CHANGE))
338 printf("drive empty\n");
339 else {
340 printf("density code 0x%x, ", st->media_density);
341 if (st->media_blksize > 0)
342 printf("%d-byte", st->media_blksize);
343 else
344 printf("variable");
345 printf(" blocks, write-%s\n",
346 (st->flags & ST_READONLY) ? "protected" : "enabled");
347 }
348
349 /*
350 * Set up the buf queue for this device
351 */
352 st->buf_queue.b_active = 0;
353 st->buf_queue.b_actf = 0;
354 st->buf_queue.b_actb = &st->buf_queue.b_actf;
355 }
356
357 /*
358 * Use the inquiry routine in 'scsi_base' to get drive info so we can
359 * Further tailor our behaviour.
360 */
361 void
362 st_identify_drive(st, inqbuf)
363 struct st_softc *st;
364 struct scsi_inquiry_data *inqbuf;
365 {
366 struct st_quirk_inquiry_pattern *finger;
367 int priority;
368
369 finger = (struct st_quirk_inquiry_pattern *)scsi_inqmatch(inqbuf,
370 (caddr_t)st_quirk_patterns,
371 sizeof(st_quirk_patterns)/sizeof(st_quirk_patterns[0]),
372 sizeof(st_quirk_patterns[0]), &priority);
373 if (priority != 0) {
374 st->quirkdata = &finger->quirkdata;
375 st->drive_quirks = finger->quirkdata.quirks;
376 st->quirks = finger->quirkdata.quirks; /* start value */
377 st->page_0_size = finger->quirkdata.page_0_size;
378 st_loadquirks(st);
379 }
380 }
381
382 /*
383 * initialise the subdevices to the default (QUIRK) state.
384 * this will remove any setting made by the system operator or previous
385 * operations.
386 */
387 void
388 st_loadquirks(st)
389 struct st_softc *st;
390 {
391 int i;
392 struct modes *mode;
393 struct modes *mode2;
394
395 mode = st->quirkdata->modes;
396 mode2 = st->modes;
397 for (i = 0; i < 4; i++) {
398 bzero(mode2, sizeof(struct modes));
399 st->modeflags[i] &= ~(BLKSIZE_SET_BY_QUIRK |
400 DENSITY_SET_BY_QUIRK | BLKSIZE_SET_BY_USER |
401 DENSITY_SET_BY_USER);
402 if ((mode->quirks | st->drive_quirks) & ST_Q_FORCE_BLKSIZE) {
403 mode2->blksize = mode->blksize;
404 st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK;
405 }
406 if (mode->density) {
407 mode2->density = mode->density;
408 st->modeflags[i] |= DENSITY_SET_BY_QUIRK;
409 }
410 mode++;
411 mode2++;
412 }
413 }
414
415 /*
416 * open the device.
417 */
418 int
419 stopen(dev, flags)
420 dev_t dev;
421 int flags;
422 {
423 int unit;
424 u_int mode, dsty;
425 int error = 0;
426 struct st_softc *st;
427 struct scsi_link *sc_link;
428
429 unit = STUNIT(dev);
430 if (unit >= stcd.cd_ndevs)
431 return ENXIO;
432 st = stcd.cd_devs[unit];
433 if (!st)
434 return ENXIO;
435
436 mode = STMODE(dev);
437 dsty = STDSTY(dev);
438 sc_link = st->sc_link;
439
440 SC_DEBUG(sc_link, SDEV_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
441 unit, stcd.cd_ndevs));
442
443 /*
444 * Only allow one at a time
445 */
446 if (sc_link->flags & SDEV_OPEN) {
447 printf("%s: already open\n", st->sc_dev.dv_xname);
448 return EBUSY;
449 }
450
451 /*
452 * Catch any unit attention errors.
453 */
454 if (error = scsi_test_unit_ready(sc_link,
455 SCSI_IGNORE_MEDIA_CHANGE | (mode == CTLMODE ? SCSI_IGNORE_NOT_READY : 0)))
456 goto bad;
457
458 sc_link->flags |= SDEV_OPEN; /* unit attn are now errors */
459
460 /*
461 * If the mode is 3 (e.g. minor = 3,7,11,15)
462 * then the device has been opened to set defaults
463 * This mode does NOT ALLOW I/O, only ioctls
464 */
465 if (mode == CTLMODE)
466 return 0;
467
468 /*
469 * if it's a different mode, or if the media has been
470 * invalidated, unmount the tape from the previous
471 * session but continue with open processing
472 */
473 if (st->last_dsty != dsty || !(sc_link->flags & SDEV_MEDIA_LOADED))
474 st_unmount(st, NOEJECT);
475
476 /*
477 * If we are not mounted, then we should start a new
478 * mount session.
479 */
480 if (!(st->flags & ST_MOUNTED)) {
481 st_mount_tape(dev, flags);
482 st->last_dsty = dsty;
483 }
484
485 /*
486 * Make sure that a tape opened in write-only mode will have
487 * file marks written on it when closed, even if not written to.
488 * This is for SUN compatibility
489 */
490 if ((flags & O_ACCMODE) == FWRITE)
491 st->flags |= ST_WRITTEN;
492
493 SC_DEBUG(sc_link, SDEV_DB2, ("open complete\n"));
494 return 0;
495
496 bad:
497 st_unmount(st, NOEJECT);
498 sc_link->flags &= ~SDEV_OPEN;
499 return error;
500 }
501
502 /*
503 * close the device.. only called if we are the LAST
504 * occurence of an open device
505 */
506 int
507 stclose(dev)
508 dev_t dev;
509 {
510 struct st_softc *st = stcd.cd_devs[STUNIT(dev)];
511
512 SC_DEBUG(st->sc_link, SDEV_DB1, ("closing\n"));
513 if ((st->flags & (ST_WRITTEN | ST_FM_WRITTEN)) == ST_WRITTEN)
514 st_write_filemarks(st, 1, 0);
515 switch (STMODE(dev)) {
516 case 0:
517 case 3: /* for now */
518 st_unmount(st, NOEJECT);
519 break;
520 case 1:
521 /* leave mounted unless media seems to have been removed */
522 if (!(st->sc_link->flags & SDEV_MEDIA_LOADED))
523 st_unmount(st, NOEJECT);
524 break;
525 case 2:
526 st_unmount(st, EJECT);
527 break;
528 }
529 st->sc_link->flags &= ~SDEV_OPEN;
530
531 return 0;
532 }
533
534 /*
535 * Start a new mount session.
536 * Copy in all the default parameters from the selected device mode.
537 * and try guess any that seem to be defaulted.
538 */
539 int
540 st_mount_tape(dev, flags)
541 dev_t dev;
542 int flags;
543 {
544 int unit;
545 u_int mode, dsty;
546 struct st_softc *st;
547 struct scsi_link *sc_link;
548 int error = 0;
549
550 unit = STUNIT(dev);
551 mode = STMODE(dev);
552 dsty = STDSTY(dev);
553 st = stcd.cd_devs[unit];
554 sc_link = st->sc_link;
555
556 if (st->flags & ST_MOUNTED)
557 return 0;
558
559 SC_DEBUG(sc_link, SDEV_DB1, ("mounting\n "));
560 st->flags |= ST_NEW_MOUNT;
561 st->quirks = st->drive_quirks | st->modes[dsty].quirks;
562 /*
563 * If the media is new, then make sure we give it a chance to
564 * to do a 'load' instruction. (We assume it is new.)
565 */
566 if (error = st_load(st, LD_LOAD, 0))
567 return error;
568 /*
569 * Throw another dummy instruction to catch
570 * 'Unit attention' errors. Some drives appear to give
571 * these after doing a Load instruction.
572 * (noteably some DAT drives)
573 */
574 scsi_test_unit_ready(sc_link, SCSI_SILENT); /* XXX */
575
576 /*
577 * Some devices can't tell you much until they have been
578 * asked to look at the media. This quirk does this.
579 */
580 if (st->quirks & ST_Q_SENSE_HELP)
581 if (error = st_touch_tape(st))
582 return error;
583 /*
584 * Load the physical device parameters
585 * loads: blkmin, blkmax
586 */
587 if (error = st_read_block_limits(st, 0))
588 return error;
589 /*
590 * Load the media dependent parameters
591 * includes: media_blksize,media_density,numblks
592 * As we have a tape in, it should be reflected here.
593 * If not you may need the "quirk" above.
594 */
595 if (error = st_mode_sense(st, 0))
596 return error;
597 /*
598 * If we have gained a permanent density from somewhere,
599 * then use it in preference to the one supplied by
600 * default by the driver.
601 */
602 if (st->modeflags[dsty] & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER))
603 st->density = st->modes[dsty].density;
604 else
605 st->density = st->media_density;
606 /*
607 * If we have gained a permanent blocksize
608 * then use it in preference to the one supplied by
609 * default by the driver.
610 */
611 st->flags &= ~ST_FIXEDBLOCKS;
612 if (st->modeflags[dsty] & (BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) {
613 st->blksize = st->modes[dsty].blksize;
614 if (st->blksize)
615 st->flags |= ST_FIXEDBLOCKS;
616 } else {
617 if (error = st_decide_mode(st, FALSE))
618 return error;
619 }
620 if (error = st_mode_select(st, 0)) {
621 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
622 return error;
623 }
624 scsi_prevent(sc_link, PR_PREVENT,
625 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY);
626 st->flags &= ~ST_NEW_MOUNT;
627 st->flags |= ST_MOUNTED;
628 sc_link->flags |= SDEV_MEDIA_LOADED; /* move earlier? */
629
630 return 0;
631 }
632
633 /*
634 * End the present mount session.
635 * Rewind, and optionally eject the tape.
636 * Reset various flags to indicate that all new
637 * operations require another mount operation
638 */
639 void
640 st_unmount(st, eject)
641 struct st_softc *st;
642 boolean eject;
643 {
644 struct scsi_link *sc_link = st->sc_link;
645 int nmarks;
646
647 if (!(st->flags & ST_MOUNTED))
648 return;
649 SC_DEBUG(sc_link, SDEV_DB1, ("unmounting\n"));
650 st_check_eod(st, FALSE, &nmarks, SCSI_IGNORE_NOT_READY);
651 st_rewind(st, 0, SCSI_IGNORE_NOT_READY);
652 scsi_prevent(sc_link, PR_ALLOW,
653 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY);
654 if (eject)
655 st_load(st, LD_UNLOAD, SCSI_IGNORE_NOT_READY);
656 st->flags &= ~(ST_MOUNTED | ST_NEW_MOUNT);
657 sc_link->flags &= ~SDEV_MEDIA_LOADED;
658 }
659
660 /*
661 * Given all we know about the device, media, mode, 'quirks' and
662 * initial operation, make a decision as to how we should be set
663 * to run (regarding blocking and EOD marks)
664 */
665 int
666 st_decide_mode(st, first_read)
667 struct st_softc *st;
668 boolean first_read;
669 {
670 #ifdef SCSIDEBUG
671 struct scsi_link *sc_link = st->sc_link;
672 #endif
673
674 SC_DEBUG(sc_link, SDEV_DB2, ("starting block mode decision\n"));
675
676 /*
677 * If the drive can only handle fixed-length blocks and only at
678 * one size, perhaps we should just do that.
679 */
680 if (st->blkmin && (st->blkmin == st->blkmax)) {
681 st->flags |= ST_FIXEDBLOCKS;
682 st->blksize = st->blkmin;
683 SC_DEBUG(sc_link, SDEV_DB3,
684 ("blkmin == blkmax of %d\n", st->blkmin));
685 goto done;
686 }
687 /*
688 * If the tape density mandates (or even suggests) use of fixed
689 * or variable-length blocks, comply.
690 */
691 switch (st->density) {
692 case HALFINCH_800:
693 case HALFINCH_1600:
694 case HALFINCH_6250:
695 case DDS:
696 st->flags &= ~ST_FIXEDBLOCKS;
697 st->blksize = 0;
698 SC_DEBUG(sc_link, SDEV_DB3, ("density specified variable\n"));
699 goto done;
700 case QIC_11:
701 case QIC_24:
702 case QIC_120:
703 case QIC_150:
704 case QIC_525:
705 case QIC_1320:
706 st->flags |= ST_FIXEDBLOCKS;
707 if (st->media_blksize > 0)
708 st->blksize = st->media_blksize;
709 else
710 st->blksize = DEF_FIXED_BSIZE;
711 SC_DEBUG(sc_link, SDEV_DB3, ("density specified fixed\n"));
712 goto done;
713 }
714 /*
715 * If we're about to read the tape, perhaps we should choose
716 * fixed or variable-length blocks and block size according to
717 * what the drive found on the tape.
718 */
719 if (first_read &&
720 (!(st->quirks & ST_Q_BLKSIZE) || (st->media_blksize == 0) ||
721 (st->media_blksize == DEF_FIXED_BSIZE) ||
722 (st->media_blksize == 1024))) {
723 if (st->media_blksize > 0)
724 st->flags |= ST_FIXEDBLOCKS;
725 else
726 st->flags &= ~ST_FIXEDBLOCKS;
727 st->blksize = st->media_blksize;
728 SC_DEBUG(sc_link, SDEV_DB3,
729 ("Used media_blksize of %d\n", st->media_blksize));
730 goto done;
731 }
732 /*
733 * We're getting no hints from any direction. Choose variable-
734 * length blocks arbitrarily.
735 */
736 st->flags &= ~ST_FIXEDBLOCKS;
737 st->blksize = 0;
738 SC_DEBUG(sc_link, SDEV_DB3,
739 ("Give up and default to variable mode\n"));
740
741 done:
742 /*
743 * Decide whether or not to write two file marks to signify end-
744 * of-data. Make the decision as a function of density. If
745 * the decision is not to use a second file mark, the SCSI BLANK
746 * CHECK condition code will be recognized as end-of-data when
747 * first read.
748 * (I think this should be a by-product of fixed/variable..julian)
749 */
750 switch (st->density) {
751 /* case 8 mm: What is the SCSI density code for 8 mm, anyway? */
752 case QIC_11:
753 case QIC_24:
754 case QIC_120:
755 case QIC_150:
756 case QIC_525:
757 case QIC_1320:
758 st->flags &= ~ST_2FM_AT_EOD;
759 break;
760 default:
761 st->flags |= ST_2FM_AT_EOD;
762 }
763 return 0;
764 }
765
766 /*
767 * Actually translate the requested transfer into
768 * one the physical driver can understand
769 * The transfer is described by a buf and will include
770 * only one physical transfer.
771 */
772 void
773 ststrategy(bp)
774 struct buf *bp;
775 {
776 struct st_softc *st = stcd.cd_devs[STUNIT(bp->b_dev)];
777 struct buf *dp;
778 int opri;
779
780 SC_DEBUG(st->sc_link, SDEV_DB1,
781 ("ststrategy %d bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
782 /*
783 * If it's a null transfer, return immediatly
784 */
785 if (bp->b_bcount == 0)
786 goto done;
787 /*
788 * Odd sized request on fixed drives are verboten
789 */
790 if (st->flags & ST_FIXEDBLOCKS) {
791 if (bp->b_bcount % st->blksize) {
792 printf("%s: bad request, must be multiple of %d\n",
793 st->sc_dev.dv_xname, st->blksize);
794 bp->b_error = EIO;
795 goto bad;
796 }
797 }
798 /*
799 * as are out-of-range requests on variable drives.
800 */
801 else if (bp->b_bcount < st->blkmin ||
802 (st->blkmax && bp->b_bcount > st->blkmax)) {
803 printf("%s: bad request, must be between %d and %d\n",
804 st->sc_dev.dv_xname, st->blkmin, st->blkmax);
805 bp->b_error = EIO;
806 goto bad;
807 }
808 opri = splbio();
809
810 /*
811 * Place it in the queue of activities for this tape
812 * at the end (a bit silly because we only have on user..
813 * (but it could fork()))
814 */
815 dp = &st->buf_queue;
816 bp->b_actf = NULL;
817 bp->b_actb = dp->b_actb;
818 *dp->b_actb = bp;
819 dp->b_actb = &bp->b_actf;
820
821 /*
822 * Tell the device to get going on the transfer if it's
823 * not doing anything, otherwise just wait for completion
824 * (All a bit silly if we're only allowing 1 open but..)
825 */
826 ststart(st);
827
828 splx(opri);
829 return;
830 bad:
831 bp->b_flags |= B_ERROR;
832 done:
833 /*
834 * Correctly set the buf to indicate a completed xfer
835 */
836 iodone(bp);
837 return;
838 }
839
840 /*
841 * ststart looks to see if there is a buf waiting for the device
842 * and that the device is not already busy. If both are true,
843 * It dequeues the buf and creates a scsi command to perform the
844 * transfer required. The transfer request will call scsi_done
845 * on completion, which will in turn call this routine again
846 * so that the next queued transfer is performed.
847 * The bufs are queued by the strategy routine (ststrategy)
848 *
849 * This routine is also called after other non-queued requests
850 * have been made of the scsi driver, to ensure that the queue
851 * continues to be drained.
852 * ststart() is called at splbio
853 */
854 void
855 ststart(st)
856 struct st_softc *st;
857 {
858 struct scsi_link *sc_link = st->sc_link;
859 register struct buf *bp, *dp;
860 struct scsi_rw_tape cmd;
861 int flags;
862
863 SC_DEBUG(sc_link, SDEV_DB2, ("ststart "));
864 /*
865 * See if there is a buf to do and we are not already
866 * doing one
867 */
868 while (sc_link->openings > 0) {
869 /* if a special awaits, let it proceed first */
870 if (sc_link->flags & SDEV_WAITING) {
871 sc_link->flags &= ~SDEV_WAITING;
872 wakeup((caddr_t)sc_link);
873 return;
874 }
875
876 bp = st->buf_queue.b_actf;
877 if (!bp)
878 return; /* no work to bother with */
879 if (dp = bp->b_actf)
880 dp->b_actb = bp->b_actb;
881 else
882 st->buf_queue.b_actb = bp->b_actb;
883 *bp->b_actb = dp;
884
885 /*
886 * if the device has been unmounted byt the user
887 * then throw away all requests until done
888 */
889 if (!(st->flags & ST_MOUNTED) ||
890 !(sc_link->flags & SDEV_MEDIA_LOADED)) {
891 /* make sure that one implies the other.. */
892 sc_link->flags &= ~SDEV_MEDIA_LOADED;
893 bp->b_flags |= B_ERROR;
894 bp->b_error = EIO;
895 biodone(bp);
896 continue;
897 }
898 /*
899 * only FIXEDBLOCK devices have pending operations
900 */
901 if (st->flags & ST_FIXEDBLOCKS) {
902 /*
903 * If we are at a filemark but have not reported it yet
904 * then we should report it now
905 */
906 if (st->flags & ST_AT_FILEMARK) {
907 if ((bp->b_flags & B_READ) == B_WRITE) {
908 /*
909 * Handling of ST_AT_FILEMARK in
910 * st_space will fill in the right file
911 * mark count.
912 * Back up over filemark
913 */
914 if (st_space(st, 0, SP_FILEMARKS, 0)) {
915 bp->b_flags |= B_ERROR;
916 bp->b_error = EIO;
917 biodone(bp);
918 continue;
919 }
920 } else {
921 bp->b_resid = bp->b_bcount;
922 bp->b_error = 0;
923 bp->b_flags &= ~B_ERROR;
924 st->flags &= ~ST_AT_FILEMARK;
925 biodone(bp);
926 continue; /* seek more work */
927 }
928 }
929 /*
930 * If we are at EIO (e.g. EOM) but have not reported it
931 * yet then we should report it now
932 */
933 if (st->flags & ST_EIO_PENDING) {
934 bp->b_resid = bp->b_bcount;
935 bp->b_error = EIO;
936 bp->b_flags |= B_ERROR;
937 st->flags &= ~ST_EIO_PENDING;
938 biodone(bp);
939 continue; /* seek more work */
940 }
941 }
942
943 /*
944 * Fill out the scsi command
945 */
946 bzero(&cmd, sizeof(cmd));
947 if ((bp->b_flags & B_READ) == B_WRITE) {
948 cmd.opcode = WRITE;
949 st->flags &= ~ST_FM_WRITTEN;
950 st->flags |= ST_WRITTEN;
951 flags = SCSI_DATA_OUT;
952 } else {
953 cmd.opcode = READ;
954 flags = SCSI_DATA_IN;
955 }
956
957 /*
958 * Handle "fixed-block-mode" tape drives by using the
959 * block count instead of the length.
960 */
961 if (st->flags & ST_FIXEDBLOCKS) {
962 cmd.byte2 |= SRW_FIXED;
963 lto3b(bp->b_bcount / st->blksize, cmd.len);
964 } else
965 lto3b(bp->b_bcount, cmd.len);
966
967 /*
968 * go ask the adapter to do all this for us
969 */
970 if (scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
971 sizeof(cmd), (u_char *) bp->b_data, bp->b_bcount, 0,
972 100000, bp, flags | SCSI_NOSLEEP))
973 printf("%s: not queued\n", st->sc_dev.dv_xname);
974 } /* go back and see if we can cram more work in.. */
975 }
976
977 int
978 stread(dev, uio)
979 dev_t dev;
980 struct uio *uio;
981 {
982
983 return (physio(ststrategy, NULL, dev, B_READ,
984 st->sc_link->adapter->scsi_minphys, uio));
985 }
986
987 int
988 stwrite(dev, uio)
989 dev_t dev;
990 struct uio *uio;
991 {
992
993 return (physio(ststrategy, NULL, dev, B_WRITE,
994 st->sc_link->adapter->scsi_minphys, uio));
995 }
996
997 /*
998 * Perform special action on behalf of the user;
999 * knows about the internals of this device
1000 */
1001 int
1002 stioctl(dev, cmd, arg, flag, p)
1003 dev_t dev;
1004 u_long cmd;
1005 caddr_t arg;
1006 int flag;
1007 struct proc *p;
1008 {
1009 int error = 0;
1010 int unit;
1011 int number, nmarks, dsty;
1012 int flags;
1013 struct st_softc *st;
1014 int hold_blksize;
1015 u_int8_t hold_density;
1016 struct mtop *mt = (struct mtop *) arg;
1017
1018 /*
1019 * Find the device that the user is talking about
1020 */
1021 flags = 0; /* give error messages, act on errors etc. */
1022 unit = STUNIT(dev);
1023 dsty = STDSTY(dev);
1024 st = stcd.cd_devs[unit];
1025 hold_blksize = st->blksize;
1026 hold_density = st->density;
1027
1028 switch (cmd) {
1029
1030 case MTIOCGET: {
1031 struct mtget *g = (struct mtget *) arg;
1032
1033 SC_DEBUG(st->sc_link, SDEV_DB1, ("[ioctl: get status]\n"));
1034 bzero(g, sizeof(struct mtget));
1035 g->mt_type = 0x7; /* Ultrix compat *//*? */
1036 g->mt_blksiz = st->blksize;
1037 g->mt_density = st->density;
1038 g->mt_mblksiz[0] = st->modes[0].blksize;
1039 g->mt_mblksiz[1] = st->modes[1].blksize;
1040 g->mt_mblksiz[2] = st->modes[2].blksize;
1041 g->mt_mblksiz[3] = st->modes[3].blksize;
1042 g->mt_mdensity[0] = st->modes[0].density;
1043 g->mt_mdensity[1] = st->modes[1].density;
1044 g->mt_mdensity[2] = st->modes[2].density;
1045 g->mt_mdensity[3] = st->modes[3].density;
1046 break;
1047 }
1048 case MTIOCTOP: {
1049
1050 SC_DEBUG(st->sc_link, SDEV_DB1,
1051 ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op, mt->mt_count));
1052
1053 /* compat: in U*x it is a short */
1054 number = mt->mt_count;
1055 switch ((short) (mt->mt_op)) {
1056 case MTWEOF: /* write an end-of-file record */
1057 error = st_write_filemarks(st, number, flags);
1058 break;
1059 case MTBSF: /* backward space file */
1060 number = -number;
1061 case MTFSF: /* forward space file */
1062 error = st_check_eod(st, FALSE, &nmarks, flags);
1063 if (!error)
1064 error = st_space(st, number - nmarks,
1065 SP_FILEMARKS, flags);
1066 break;
1067 case MTBSR: /* backward space record */
1068 number = -number;
1069 case MTFSR: /* forward space record */
1070 error = st_check_eod(st, TRUE, &nmarks, flags);
1071 if (!error)
1072 error = st_space(st, number, SP_BLKS, flags);
1073 break;
1074 case MTREW: /* rewind */
1075 error = st_rewind(st, 0, flags);
1076 break;
1077 case MTOFFL: /* rewind and put the drive offline */
1078 st_unmount(st, EJECT);
1079 break;
1080 case MTNOP: /* no operation, sets status only */
1081 break;
1082 case MTRETEN: /* retension the tape */
1083 error = st_load(st, LD_RETENSION, flags);
1084 if (!error)
1085 error = st_load(st, LD_LOAD, flags);
1086 break;
1087 case MTEOM: /* forward space to end of media */
1088 error = st_check_eod(st, FALSE, &nmarks, flags);
1089 if (!error)
1090 error = st_space(st, 1, SP_EOM, flags);
1091 break;
1092 case MTCACHE: /* enable controller cache */
1093 case MTNOCACHE: /* disable controller cache */
1094 break;
1095 case MTSETBSIZ: /* Set block size for device */
1096 #ifdef NOTYET
1097 if (!(st->flags & ST_NEW_MOUNT)) {
1098 uprintf("re-mount tape before changing blocksize");
1099 error = EINVAL;
1100 break;
1101 }
1102 #endif
1103 if (number == 0) {
1104 st->flags &= ~ST_FIXEDBLOCKS;
1105 } else {
1106 if ((st->blkmin || st->blkmax) &&
1107 (number < st->blkmin ||
1108 number > st->blkmax)) {
1109 error = EINVAL;
1110 break;
1111 }
1112 st->flags |= ST_FIXEDBLOCKS;
1113 }
1114 st->blksize = number;
1115 st->flags |= ST_BLOCK_SET; /*XXX */
1116 goto try_new_value;
1117
1118 case MTSETDNSTY: /* Set density for device and mode */
1119 if (number > SCSI_2_MAX_DENSITY_CODE)
1120 error = EINVAL;
1121 else
1122 st->density = number;
1123 goto try_new_value;
1124
1125 default:
1126 error = EINVAL;
1127 }
1128 break;
1129 }
1130 case MTIOCIEOT:
1131 case MTIOCEEOT:
1132 break;
1133 default:
1134 if (STMODE(dev) == CTLMODE)
1135 error = scsi_do_ioctl(st->sc_link, dev, cmd, arg, flag, p);
1136 else
1137 error = ENOTTY;
1138 break;
1139 }
1140 return error;
1141 /*-----------------------------*/
1142 try_new_value:
1143 /*
1144 * Check that the mode being asked for is aggreeable to the
1145 * drive. If not, put it back the way it was.
1146 */
1147 if (error = st_mode_select(st, 0)) { /* put it back as it was */
1148 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
1149 st->density = hold_density;
1150 st->blksize = hold_blksize;
1151 if (st->blksize)
1152 st->flags |= ST_FIXEDBLOCKS;
1153 else
1154 st->flags &= ~ST_FIXEDBLOCKS;
1155 return error;
1156 }
1157 /*
1158 * As the drive liked it, if we are setting a new default,
1159 * set it into the structures as such.
1160 *
1161 * The means for deciding this are not finalised yet
1162 */
1163 if (STMODE(dev) == 0x03) {
1164 /* special mode */
1165 /* XXX */
1166 switch ((short) (mt->mt_op)) {
1167 case MTSETBSIZ:
1168 st->modes[dsty].blksize = st->blksize;
1169 st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
1170 break;
1171 case MTSETDNSTY:
1172 st->modes[dsty].density = st->density;
1173 st->modeflags[dsty] |= DENSITY_SET_BY_USER;
1174 break;
1175 }
1176 }
1177 return 0;
1178 }
1179
1180 /*
1181 * Do a synchronous read.
1182 */
1183 int
1184 st_read(st, buf, size, flags)
1185 struct st_softc *st;
1186 int size;
1187 int flags;
1188 char *buf;
1189 {
1190 struct scsi_rw_tape cmd;
1191
1192 /*
1193 * If it's a null transfer, return immediatly
1194 */
1195 if (size == 0)
1196 return 0;
1197 bzero(&cmd, sizeof(cmd));
1198 cmd.opcode = READ;
1199 if (st->flags & ST_FIXEDBLOCKS) {
1200 cmd.byte2 |= SRW_FIXED;
1201 lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE),
1202 cmd.len);
1203 } else
1204 lto3b(size, cmd.len);
1205 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
1206 sizeof(cmd), (u_char *) buf, size, 0, 100000, NULL,
1207 flags | SCSI_DATA_IN);
1208 }
1209
1210 #ifdef __STDC__
1211 #define b2tol(a) (((unsigned)(a##_1) << 8) + (unsigned)a##_0)
1212 #else
1213 #define b2tol(a) (((unsigned)(a/**/_1) << 8) + (unsigned)a/**/_0)
1214 #endif
1215
1216 /*
1217 * Ask the drive what it's min and max blk sizes are.
1218 */
1219 int
1220 st_read_block_limits(st, flags)
1221 struct st_softc *st;
1222 int flags;
1223 {
1224 struct scsi_block_limits cmd;
1225 struct scsi_block_limits_data block_limits;
1226 struct scsi_link *sc_link = st->sc_link;
1227 int error;
1228
1229 /*
1230 * First check if we have it all loaded
1231 */
1232 if ((sc_link->flags & SDEV_MEDIA_LOADED))
1233 return 0;
1234
1235 /*
1236 * do a 'Read Block Limits'
1237 */
1238 bzero(&cmd, sizeof(cmd));
1239 cmd.opcode = READ_BLOCK_LIMITS;
1240
1241 /*
1242 * do the command, update the global values
1243 */
1244 if (error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
1245 sizeof(cmd), (u_char *) &block_limits, sizeof(block_limits),
1246 ST_RETRIES, 5000, NULL, flags | SCSI_DATA_IN))
1247 return error;
1248
1249 st->blkmin = b2tol(block_limits.min_length);
1250 st->blkmax = _3btol(&block_limits.max_length_2);
1251
1252 SC_DEBUG(sc_link, SDEV_DB3,
1253 ("(%d <= blksize <= %d)\n", st->blkmin, st->blkmax));
1254 return 0;
1255 }
1256
1257 /*
1258 * Get the scsi driver to send a full inquiry to the
1259 * device and use the results to fill out the global
1260 * parameter structure.
1261 *
1262 * called from:
1263 * attach
1264 * open
1265 * ioctl (to reset original blksize)
1266 */
1267 int
1268 st_mode_sense(st, flags)
1269 struct st_softc *st;
1270 int flags;
1271 {
1272 u_int scsi_sense_len;
1273 int error;
1274 struct scsi_mode_sense cmd;
1275 struct scsi_sense {
1276 struct scsi_mode_header header;
1277 struct scsi_blk_desc blk_desc;
1278 u_char sense_data[MAX_PAGE_0_SIZE];
1279 } scsi_sense;
1280 struct scsi_link *sc_link = st->sc_link;
1281
1282 scsi_sense_len = 12 + st->page_0_size;
1283
1284 /*
1285 * Set up a mode sense
1286 */
1287 bzero(&cmd, sizeof(cmd));
1288 cmd.opcode = MODE_SENSE;
1289 cmd.length = scsi_sense_len;
1290
1291 /*
1292 * do the command, but we don't need the results
1293 * just print them for our interest's sake, if asked,
1294 * or if we need it as a template for the mode select
1295 * store it away.
1296 */
1297 if (error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
1298 sizeof(cmd), (u_char *) &scsi_sense, scsi_sense_len,
1299 ST_RETRIES, 5000, NULL, flags | SCSI_DATA_IN))
1300 return error;
1301
1302 st->numblks = _3btol(scsi_sense.blk_desc.nblocks);
1303 st->media_blksize = _3btol(scsi_sense.blk_desc.blklen);
1304 st->media_density = scsi_sense.blk_desc.density;
1305 if (scsi_sense.header.dev_spec & SMH_DSP_WRITE_PROT)
1306 st->flags |= ST_READONLY;
1307 SC_DEBUG(sc_link, SDEV_DB3,
1308 ("density code 0x%x, %d-byte blocks, write-%s, ",
1309 st->media_density, st->media_blksize,
1310 st->flags & ST_READONLY ? "protected" : "enabled"));
1311 SC_DEBUG(sc_link, SDEV_DB3,
1312 ("%sbuffered\n",
1313 scsi_sense.header.dev_spec & SMH_DSP_BUFF_MODE ? "" : "un"));
1314 if (st->page_0_size)
1315 bcopy(scsi_sense.sense_data, st->sense_data, st->page_0_size);
1316 sc_link->flags |= SDEV_MEDIA_LOADED;
1317 return 0;
1318 }
1319
1320 /*
1321 * Send a filled out parameter structure to the drive to
1322 * set it into the desire modes etc.
1323 */
1324 int
1325 st_mode_select(st, flags)
1326 struct st_softc *st;
1327 int flags;
1328 {
1329 u_int scsi_select_len;
1330 int error;
1331 struct scsi_mode_select cmd;
1332 struct scsi_select {
1333 struct scsi_mode_header header;
1334 struct scsi_blk_desc blk_desc;
1335 u_char sense_data[MAX_PAGE_0_SIZE];
1336 } scsi_select;
1337 struct scsi_link *sc_link = st->sc_link;
1338
1339 scsi_select_len = 12 + st->page_0_size;
1340
1341 /*
1342 * Set up for a mode select
1343 */
1344 bzero(&cmd, sizeof(cmd));
1345 cmd.opcode = MODE_SELECT;
1346 cmd.length = scsi_select_len;
1347
1348 bzero(&scsi_select, scsi_select_len);
1349 scsi_select.header.blk_desc_len = sizeof(struct scsi_blk_desc);
1350 scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_ON;
1351 scsi_select.blk_desc.density = st->density;
1352 if (st->flags & ST_FIXEDBLOCKS)
1353 lto3b(st->blksize, scsi_select.blk_desc.blklen);
1354 if (st->page_0_size)
1355 bcopy(st->sense_data, scsi_select.sense_data, st->page_0_size);
1356
1357 /*
1358 * do the command
1359 */
1360 return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
1361 sizeof(cmd), (u_char *) &scsi_select, scsi_select_len,
1362 ST_RETRIES, 5000, NULL, flags | SCSI_DATA_OUT);
1363 }
1364
1365 /*
1366 * skip N blocks/filemarks/seq filemarks/eom
1367 */
1368 int
1369 st_space(st, number, what, flags)
1370 struct st_softc *st;
1371 u_int what;
1372 int flags;
1373 int number;
1374 {
1375 struct scsi_space cmd;
1376 int error;
1377
1378 switch (what) {
1379 case SP_BLKS:
1380 if (st->flags & ST_PER_ACTION) {
1381 if (number > 0) {
1382 st->flags &= ~ST_PER_ACTION;
1383 return EIO;
1384 } else if (number < 0) {
1385 if (st->flags & ST_AT_FILEMARK) {
1386 /*
1387 * Handling of ST_AT_FILEMARK
1388 * in st_space will fill in the
1389 * right file mark count.
1390 */
1391 error = st_space(st, 0, SP_FILEMARKS,
1392 flags);
1393 if (error)
1394 return error;
1395 }
1396 if (st->flags & ST_BLANK_READ) {
1397 st->flags &= ~ST_BLANK_READ;
1398 return EIO;
1399 }
1400 st->flags &= ~ST_EIO_PENDING;
1401 }
1402 }
1403 break;
1404 case SP_FILEMARKS:
1405 if (st->flags & ST_EIO_PENDING) {
1406 if (number > 0) {
1407 /* pretend we just discovered the error */
1408 st->flags &= ~ST_EIO_PENDING;
1409 return EIO;
1410 } else if (number < 0) {
1411 /* back away from the error */
1412 st->flags &= ~ST_EIO_PENDING;
1413 }
1414 }
1415 if (st->flags & ST_AT_FILEMARK) {
1416 st->flags &= ~ST_AT_FILEMARK;
1417 number--;
1418 }
1419 if ((st->flags & ST_BLANK_READ) && (number < 0)) {
1420 /* back away from unwritten tape */
1421 st->flags &= ~ST_BLANK_READ;
1422 number++; /* XXX dubious */
1423 }
1424 break;
1425 case SP_EOM:
1426 if (st->flags & ST_EIO_PENDING) {
1427 /* pretend we just discovered the error */
1428 st->flags &= ~ST_EIO_PENDING;
1429 return EIO;
1430 }
1431 if (st->flags & ST_AT_FILEMARK)
1432 st->flags &= ~ST_AT_FILEMARK;
1433 break;
1434 }
1435 if (number == 0)
1436 return 0;
1437
1438 bzero(&cmd, sizeof(cmd));
1439 cmd.opcode = SPACE;
1440 cmd.byte2 = what;
1441 lto3b(number, cmd.number);
1442
1443 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
1444 sizeof(cmd), 0, 0, 0, 900000, NULL, flags);
1445 }
1446
1447 /*
1448 * write N filemarks
1449 */
1450 int
1451 st_write_filemarks(st, number, flags)
1452 struct st_softc *st;
1453 int flags;
1454 int number;
1455 {
1456 struct scsi_write_filemarks cmd;
1457
1458 /*
1459 * It's hard to write a negative number of file marks.
1460 * Don't try.
1461 */
1462 if (number < 0)
1463 return EINVAL;
1464 switch (number) {
1465 case 0: /* really a command to sync the drive's buffers */
1466 break;
1467 case 1:
1468 if (st->flags & ST_FM_WRITTEN) /* already have one down */
1469 st->flags &= ~ST_WRITTEN;
1470 else
1471 st->flags |= ST_FM_WRITTEN;
1472 st->flags &= ~ST_PER_ACTION;
1473 break;
1474 default:
1475 st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
1476 }
1477
1478 bzero(&cmd, sizeof(cmd));
1479 cmd.opcode = WRITE_FILEMARKS;
1480 lto3b(number, cmd.number);
1481
1482 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
1483 sizeof(cmd), 0, 0, 0, 100000, NULL, flags);
1484 }
1485
1486 /*
1487 * Make sure the right number of file marks is on tape if the
1488 * tape has been written. If the position argument is true,
1489 * leave the tape positioned where it was originally.
1490 *
1491 * nmarks returns the number of marks to skip (or, if position
1492 * true, which were skipped) to get back original position.
1493 */
1494 int
1495 st_check_eod(st, position, nmarks, flags)
1496 struct st_softc *st;
1497 boolean position;
1498 int *nmarks;
1499 int flags;
1500 {
1501 int error;
1502
1503 switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
1504 default:
1505 *nmarks = 0;
1506 return 0;
1507 case ST_WRITTEN:
1508 case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
1509 *nmarks = 1;
1510 break;
1511 case ST_WRITTEN | ST_2FM_AT_EOD:
1512 *nmarks = 2;
1513 }
1514 error = st_write_filemarks(st, *nmarks, flags);
1515 if (position && !error)
1516 error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
1517 return error;
1518 }
1519
1520 /*
1521 * load/unload/retension
1522 */
1523 int
1524 st_load(st, type, flags)
1525 struct st_softc *st;
1526 u_int type;
1527 int flags;
1528 {
1529 struct scsi_load cmd;
1530
1531 if (type != LD_LOAD) {
1532 int error;
1533 int nmarks;
1534
1535 error = st_check_eod(st, FALSE, &nmarks, flags);
1536 if (error)
1537 return error;
1538 }
1539 if (st->quirks & ST_Q_IGNORE_LOADS)
1540 return 0;
1541
1542 bzero(&cmd, sizeof(cmd));
1543 cmd.opcode = LOAD;
1544 cmd.how = type;
1545
1546 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
1547 sizeof(cmd), 0, 0, ST_RETRIES, 300000, NULL, flags);
1548 }
1549
1550 /*
1551 * Rewind the device
1552 */
1553 int
1554 st_rewind(st, immediate, flags)
1555 struct st_softc *st;
1556 u_int immediate;
1557 int flags;
1558 {
1559 struct scsi_rewind cmd;
1560 int error;
1561 int nmarks;
1562
1563 error = st_check_eod(st, FALSE, &nmarks, flags);
1564 if (error)
1565 return error;
1566 st->flags &= ~ST_PER_ACTION;
1567
1568 bzero(&cmd, sizeof(cmd));
1569 cmd.opcode = REWIND;
1570 cmd.byte2 = immediate;
1571
1572 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
1573 sizeof(cmd), 0, 0, ST_RETRIES, immediate ? 5000 : 300000, NULL,
1574 flags);
1575 }
1576
1577 /*
1578 * Look at the returned sense and act on the error and detirmine
1579 * The unix error number to pass back... (0 = report no error)
1580 * (-1 = continue processing)
1581 */
1582 int
1583 st_interpret_sense(xs)
1584 struct scsi_xfer *xs;
1585 {
1586 struct scsi_link *sc_link = xs->sc_link;
1587 struct scsi_sense_data *sense = &xs->sense;
1588 struct buf *bp = xs->bp;
1589 struct st_softc *st = sc_link->device_softc;
1590 u_int8_t key;
1591 u_int32_t info;
1592
1593 /*
1594 * Get the sense fields and work out what code
1595 */
1596 if (sense->error_code & SSD_ERRCODE_VALID) {
1597 bcopy(sense->extended_info, &info, sizeof info);
1598 info = ntohl(info);
1599 } else
1600 info = xs->datalen; /* bad choice if fixed blocks */
1601 if ((sense->error_code & SSD_ERRCODE) != 0x70)
1602 return -1; /* let the generic code handle it */
1603 if (st->flags & ST_FIXEDBLOCKS) {
1604 xs->resid = info * st->blksize;
1605 if (sense->extended_flags & SSD_EOM) {
1606 st->flags |= ST_EIO_PENDING;
1607 if (bp)
1608 bp->b_resid = xs->resid;
1609 }
1610 if (sense->extended_flags & SSD_FILEMARK) {
1611 st->flags |= ST_AT_FILEMARK;
1612 if (bp)
1613 bp->b_resid = xs->resid;
1614 }
1615 if (sense->extended_flags & SSD_ILI) {
1616 st->flags |= ST_EIO_PENDING;
1617 if (bp)
1618 bp->b_resid = xs->resid;
1619 if (sense->error_code & SSD_ERRCODE_VALID &&
1620 (xs->flags & SCSI_SILENT) == 0)
1621 printf("%s: block wrong size, %d blocks residual\n",
1622 st->sc_dev.dv_xname, info);
1623
1624 /*
1625 * This quirk code helps the drive read
1626 * the first tape block, regardless of
1627 * format. That is required for these
1628 * drives to return proper MODE SENSE
1629 * information.
1630 */
1631 if ((st->quirks & ST_Q_SENSE_HELP) &&
1632 !(sc_link->flags & SDEV_MEDIA_LOADED))
1633 st->blksize -= 512;
1634 }
1635 /*
1636 * If no data was tranfered, do it immediatly
1637 */
1638 if (xs->resid >= xs->datalen) {
1639 if (st->flags & ST_EIO_PENDING)
1640 return EIO;
1641 if (st->flags & ST_AT_FILEMARK) {
1642 if (bp)
1643 bp->b_resid = xs->resid;
1644 return 0;
1645 }
1646 }
1647 } else { /* must be variable mode */
1648 xs->resid = xs->datalen; /* to be sure */
1649 if (sense->extended_flags & SSD_EOM)
1650 return EIO;
1651 if (sense->extended_flags & SSD_FILEMARK) {
1652 if (bp)
1653 bp->b_resid = bp->b_bcount;
1654 return 0;
1655 }
1656 if (sense->extended_flags & SSD_ILI) {
1657 if (info < 0) {
1658 /*
1659 * the record was bigger than the read
1660 */
1661 if ((xs->flags & SCSI_SILENT) == 0)
1662 printf("%s: %d-byte record too big\n",
1663 st->sc_dev.dv_xname,
1664 xs->datalen - info);
1665 return EIO;
1666 }
1667 xs->resid = info;
1668 if (bp)
1669 bp->b_resid = info;
1670 }
1671 }
1672 key = sense->extended_flags & SSD_KEY;
1673
1674 if (key == 0x8) {
1675 /*
1676 * This quirk code helps the drive read the
1677 * first tape block, regardless of format. That
1678 * is required for these drives to return proper
1679 * MODE SENSE information.
1680 */
1681 if ((st->quirks & ST_Q_SENSE_HELP) &&
1682 !(sc_link->flags & SDEV_MEDIA_LOADED)) {
1683 /* still starting */
1684 st->blksize -= 512;
1685 } else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
1686 st->flags |= ST_BLANK_READ;
1687 xs->resid = xs->datalen;
1688 if (bp) {
1689 bp->b_resid = xs->resid;
1690 /* return an EOF */
1691 }
1692 return 0;
1693 }
1694 }
1695 return -1; /* let the default/generic handler handle it */
1696 }
1697
1698 /*
1699 * The quirk here is that the drive returns some value to st_mode_sense
1700 * incorrectly until the tape has actually passed by the head.
1701 *
1702 * The method is to set the drive to large fixed-block state (user-specified
1703 * density and 1024-byte blocks), then read and rewind to get it to sense the
1704 * tape. If that doesn't work, try 512-byte fixed blocks. If that doesn't
1705 * work, as a last resort, try variable- length blocks. The result will be
1706 * the ability to do an accurate st_mode_sense.
1707 *
1708 * We know we can do a rewind because we just did a load, which implies rewind.
1709 * Rewind seems preferable to space backward if we have a virgin tape.
1710 *
1711 * The rest of the code for this quirk is in ILI processing and BLANK CHECK
1712 * error processing, both part of st_interpret_sense.
1713 */
1714 int
1715 st_touch_tape(st)
1716 struct st_softc *st;
1717 {
1718 char *buf;
1719 int readsize;
1720 int error;
1721
1722 buf = malloc(1024, M_TEMP, M_NOWAIT);
1723 if (!buf)
1724 return ENOMEM;
1725
1726 if (error = st_mode_sense(st, 0))
1727 goto bad;
1728 st->blksize = 1024;
1729 do {
1730 switch (st->blksize) {
1731 case 512:
1732 case 1024:
1733 readsize = st->blksize;
1734 st->flags |= ST_FIXEDBLOCKS;
1735 break;
1736 default:
1737 readsize = 1;
1738 st->flags &= ~ST_FIXEDBLOCKS;
1739 }
1740 if (error = st_mode_select(st, 0))
1741 goto bad;
1742 st_read(st, buf, readsize, SCSI_SILENT); /* XXX */
1743 if (error = st_rewind(st, 0, 0)) {
1744 bad: free(buf, M_TEMP);
1745 return error;
1746 }
1747 } while (readsize != 1 && readsize > st->blksize);
1748
1749 free(buf, M_TEMP);
1750 return 0;
1751 }
1752
1753 int
1754 stdump(dev, blkno, va, size)
1755 dev_t dev;
1756 daddr_t blkno;
1757 caddr_t va;
1758 size_t size;
1759 {
1760
1761 /* Not implemented. */
1762 return ENXIO;
1763 }
1764