st.c revision 1.49 1 /* $NetBSD: st.c,v 1.49 1995/07/04 07:21:07 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 void
978 stminphys(bp)
979 struct buf *bp;
980 {
981 register struct st_softc *st = stcd.cd_devs[STUNIT(bp->b_dev)];
982
983 (st->sc_link->adapter->scsi_minphys)(bp);
984 }
985
986 int
987 stread(dev, uio)
988 dev_t dev;
989 struct uio *uio;
990 {
991
992 return (physio(ststrategy, NULL, dev, B_READ, stminphys, uio));
993 }
994
995 int
996 stwrite(dev, uio)
997 dev_t dev;
998 struct uio *uio;
999 {
1000
1001 return (physio(ststrategy, NULL, dev, B_WRITE, stminphys, uio));
1002 }
1003
1004 /*
1005 * Perform special action on behalf of the user;
1006 * knows about the internals of this device
1007 */
1008 int
1009 stioctl(dev, cmd, arg, flag, p)
1010 dev_t dev;
1011 u_long cmd;
1012 caddr_t arg;
1013 int flag;
1014 struct proc *p;
1015 {
1016 int error = 0;
1017 int unit;
1018 int number, nmarks, dsty;
1019 int flags;
1020 struct st_softc *st;
1021 int hold_blksize;
1022 u_int8_t hold_density;
1023 struct mtop *mt = (struct mtop *) arg;
1024
1025 /*
1026 * Find the device that the user is talking about
1027 */
1028 flags = 0; /* give error messages, act on errors etc. */
1029 unit = STUNIT(dev);
1030 dsty = STDSTY(dev);
1031 st = stcd.cd_devs[unit];
1032 hold_blksize = st->blksize;
1033 hold_density = st->density;
1034
1035 switch (cmd) {
1036
1037 case MTIOCGET: {
1038 struct mtget *g = (struct mtget *) arg;
1039
1040 SC_DEBUG(st->sc_link, SDEV_DB1, ("[ioctl: get status]\n"));
1041 bzero(g, sizeof(struct mtget));
1042 g->mt_type = 0x7; /* Ultrix compat *//*? */
1043 g->mt_blksiz = st->blksize;
1044 g->mt_density = st->density;
1045 g->mt_mblksiz[0] = st->modes[0].blksize;
1046 g->mt_mblksiz[1] = st->modes[1].blksize;
1047 g->mt_mblksiz[2] = st->modes[2].blksize;
1048 g->mt_mblksiz[3] = st->modes[3].blksize;
1049 g->mt_mdensity[0] = st->modes[0].density;
1050 g->mt_mdensity[1] = st->modes[1].density;
1051 g->mt_mdensity[2] = st->modes[2].density;
1052 g->mt_mdensity[3] = st->modes[3].density;
1053 break;
1054 }
1055 case MTIOCTOP: {
1056
1057 SC_DEBUG(st->sc_link, SDEV_DB1,
1058 ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op, mt->mt_count));
1059
1060 /* compat: in U*x it is a short */
1061 number = mt->mt_count;
1062 switch ((short) (mt->mt_op)) {
1063 case MTWEOF: /* write an end-of-file record */
1064 error = st_write_filemarks(st, number, flags);
1065 break;
1066 case MTBSF: /* backward space file */
1067 number = -number;
1068 case MTFSF: /* forward space file */
1069 error = st_check_eod(st, FALSE, &nmarks, flags);
1070 if (!error)
1071 error = st_space(st, number - nmarks,
1072 SP_FILEMARKS, flags);
1073 break;
1074 case MTBSR: /* backward space record */
1075 number = -number;
1076 case MTFSR: /* forward space record */
1077 error = st_check_eod(st, TRUE, &nmarks, flags);
1078 if (!error)
1079 error = st_space(st, number, SP_BLKS, flags);
1080 break;
1081 case MTREW: /* rewind */
1082 error = st_rewind(st, 0, flags);
1083 break;
1084 case MTOFFL: /* rewind and put the drive offline */
1085 st_unmount(st, EJECT);
1086 break;
1087 case MTNOP: /* no operation, sets status only */
1088 break;
1089 case MTRETEN: /* retension the tape */
1090 error = st_load(st, LD_RETENSION, flags);
1091 if (!error)
1092 error = st_load(st, LD_LOAD, flags);
1093 break;
1094 case MTEOM: /* forward space to end of media */
1095 error = st_check_eod(st, FALSE, &nmarks, flags);
1096 if (!error)
1097 error = st_space(st, 1, SP_EOM, flags);
1098 break;
1099 case MTCACHE: /* enable controller cache */
1100 case MTNOCACHE: /* disable controller cache */
1101 break;
1102 case MTSETBSIZ: /* Set block size for device */
1103 #ifdef NOTYET
1104 if (!(st->flags & ST_NEW_MOUNT)) {
1105 uprintf("re-mount tape before changing blocksize");
1106 error = EINVAL;
1107 break;
1108 }
1109 #endif
1110 if (number == 0) {
1111 st->flags &= ~ST_FIXEDBLOCKS;
1112 } else {
1113 if ((st->blkmin || st->blkmax) &&
1114 (number < st->blkmin ||
1115 number > st->blkmax)) {
1116 error = EINVAL;
1117 break;
1118 }
1119 st->flags |= ST_FIXEDBLOCKS;
1120 }
1121 st->blksize = number;
1122 st->flags |= ST_BLOCK_SET; /*XXX */
1123 goto try_new_value;
1124
1125 case MTSETDNSTY: /* Set density for device and mode */
1126 if (number > SCSI_2_MAX_DENSITY_CODE)
1127 error = EINVAL;
1128 else
1129 st->density = number;
1130 goto try_new_value;
1131
1132 default:
1133 error = EINVAL;
1134 }
1135 break;
1136 }
1137 case MTIOCIEOT:
1138 case MTIOCEEOT:
1139 break;
1140 default:
1141 if (STMODE(dev) == CTLMODE)
1142 error = scsi_do_ioctl(st->sc_link, dev, cmd, arg, flag, p);
1143 else
1144 error = ENOTTY;
1145 break;
1146 }
1147 return error;
1148 /*-----------------------------*/
1149 try_new_value:
1150 /*
1151 * Check that the mode being asked for is aggreeable to the
1152 * drive. If not, put it back the way it was.
1153 */
1154 if (error = st_mode_select(st, 0)) { /* put it back as it was */
1155 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
1156 st->density = hold_density;
1157 st->blksize = hold_blksize;
1158 if (st->blksize)
1159 st->flags |= ST_FIXEDBLOCKS;
1160 else
1161 st->flags &= ~ST_FIXEDBLOCKS;
1162 return error;
1163 }
1164 /*
1165 * As the drive liked it, if we are setting a new default,
1166 * set it into the structures as such.
1167 *
1168 * The means for deciding this are not finalised yet
1169 */
1170 if (STMODE(dev) == 0x03) {
1171 /* special mode */
1172 /* XXX */
1173 switch ((short) (mt->mt_op)) {
1174 case MTSETBSIZ:
1175 st->modes[dsty].blksize = st->blksize;
1176 st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
1177 break;
1178 case MTSETDNSTY:
1179 st->modes[dsty].density = st->density;
1180 st->modeflags[dsty] |= DENSITY_SET_BY_USER;
1181 break;
1182 }
1183 }
1184 return 0;
1185 }
1186
1187 /*
1188 * Do a synchronous read.
1189 */
1190 int
1191 st_read(st, buf, size, flags)
1192 struct st_softc *st;
1193 int size;
1194 int flags;
1195 char *buf;
1196 {
1197 struct scsi_rw_tape cmd;
1198
1199 /*
1200 * If it's a null transfer, return immediatly
1201 */
1202 if (size == 0)
1203 return 0;
1204 bzero(&cmd, sizeof(cmd));
1205 cmd.opcode = READ;
1206 if (st->flags & ST_FIXEDBLOCKS) {
1207 cmd.byte2 |= SRW_FIXED;
1208 lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE),
1209 cmd.len);
1210 } else
1211 lto3b(size, cmd.len);
1212 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
1213 sizeof(cmd), (u_char *) buf, size, 0, 100000, NULL,
1214 flags | SCSI_DATA_IN);
1215 }
1216
1217 #ifdef __STDC__
1218 #define b2tol(a) (((unsigned)(a##_1) << 8) + (unsigned)a##_0)
1219 #else
1220 #define b2tol(a) (((unsigned)(a/**/_1) << 8) + (unsigned)a/**/_0)
1221 #endif
1222
1223 /*
1224 * Ask the drive what it's min and max blk sizes are.
1225 */
1226 int
1227 st_read_block_limits(st, flags)
1228 struct st_softc *st;
1229 int flags;
1230 {
1231 struct scsi_block_limits cmd;
1232 struct scsi_block_limits_data block_limits;
1233 struct scsi_link *sc_link = st->sc_link;
1234 int error;
1235
1236 /*
1237 * First check if we have it all loaded
1238 */
1239 if ((sc_link->flags & SDEV_MEDIA_LOADED))
1240 return 0;
1241
1242 /*
1243 * do a 'Read Block Limits'
1244 */
1245 bzero(&cmd, sizeof(cmd));
1246 cmd.opcode = READ_BLOCK_LIMITS;
1247
1248 /*
1249 * do the command, update the global values
1250 */
1251 if (error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
1252 sizeof(cmd), (u_char *) &block_limits, sizeof(block_limits),
1253 ST_RETRIES, 5000, NULL, flags | SCSI_DATA_IN))
1254 return error;
1255
1256 st->blkmin = b2tol(block_limits.min_length);
1257 st->blkmax = _3btol(&block_limits.max_length_2);
1258
1259 SC_DEBUG(sc_link, SDEV_DB3,
1260 ("(%d <= blksize <= %d)\n", st->blkmin, st->blkmax));
1261 return 0;
1262 }
1263
1264 /*
1265 * Get the scsi driver to send a full inquiry to the
1266 * device and use the results to fill out the global
1267 * parameter structure.
1268 *
1269 * called from:
1270 * attach
1271 * open
1272 * ioctl (to reset original blksize)
1273 */
1274 int
1275 st_mode_sense(st, flags)
1276 struct st_softc *st;
1277 int flags;
1278 {
1279 u_int scsi_sense_len;
1280 int error;
1281 struct scsi_mode_sense cmd;
1282 struct scsi_sense {
1283 struct scsi_mode_header header;
1284 struct scsi_blk_desc blk_desc;
1285 u_char sense_data[MAX_PAGE_0_SIZE];
1286 } scsi_sense;
1287 struct scsi_link *sc_link = st->sc_link;
1288
1289 scsi_sense_len = 12 + st->page_0_size;
1290
1291 /*
1292 * Set up a mode sense
1293 */
1294 bzero(&cmd, sizeof(cmd));
1295 cmd.opcode = MODE_SENSE;
1296 cmd.length = scsi_sense_len;
1297
1298 /*
1299 * do the command, but we don't need the results
1300 * just print them for our interest's sake, if asked,
1301 * or if we need it as a template for the mode select
1302 * store it away.
1303 */
1304 if (error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
1305 sizeof(cmd), (u_char *) &scsi_sense, scsi_sense_len,
1306 ST_RETRIES, 5000, NULL, flags | SCSI_DATA_IN))
1307 return error;
1308
1309 st->numblks = _3btol(scsi_sense.blk_desc.nblocks);
1310 st->media_blksize = _3btol(scsi_sense.blk_desc.blklen);
1311 st->media_density = scsi_sense.blk_desc.density;
1312 if (scsi_sense.header.dev_spec & SMH_DSP_WRITE_PROT)
1313 st->flags |= ST_READONLY;
1314 SC_DEBUG(sc_link, SDEV_DB3,
1315 ("density code 0x%x, %d-byte blocks, write-%s, ",
1316 st->media_density, st->media_blksize,
1317 st->flags & ST_READONLY ? "protected" : "enabled"));
1318 SC_DEBUG(sc_link, SDEV_DB3,
1319 ("%sbuffered\n",
1320 scsi_sense.header.dev_spec & SMH_DSP_BUFF_MODE ? "" : "un"));
1321 if (st->page_0_size)
1322 bcopy(scsi_sense.sense_data, st->sense_data, st->page_0_size);
1323 sc_link->flags |= SDEV_MEDIA_LOADED;
1324 return 0;
1325 }
1326
1327 /*
1328 * Send a filled out parameter structure to the drive to
1329 * set it into the desire modes etc.
1330 */
1331 int
1332 st_mode_select(st, flags)
1333 struct st_softc *st;
1334 int flags;
1335 {
1336 u_int scsi_select_len;
1337 int error;
1338 struct scsi_mode_select cmd;
1339 struct scsi_select {
1340 struct scsi_mode_header header;
1341 struct scsi_blk_desc blk_desc;
1342 u_char sense_data[MAX_PAGE_0_SIZE];
1343 } scsi_select;
1344 struct scsi_link *sc_link = st->sc_link;
1345
1346 scsi_select_len = 12 + st->page_0_size;
1347
1348 /*
1349 * Set up for a mode select
1350 */
1351 bzero(&cmd, sizeof(cmd));
1352 cmd.opcode = MODE_SELECT;
1353 cmd.length = scsi_select_len;
1354
1355 bzero(&scsi_select, scsi_select_len);
1356 scsi_select.header.blk_desc_len = sizeof(struct scsi_blk_desc);
1357 scsi_select.header.dev_spec |= SMH_DSP_BUFF_MODE_ON;
1358 scsi_select.blk_desc.density = st->density;
1359 if (st->flags & ST_FIXEDBLOCKS)
1360 lto3b(st->blksize, scsi_select.blk_desc.blklen);
1361 if (st->page_0_size)
1362 bcopy(st->sense_data, scsi_select.sense_data, st->page_0_size);
1363
1364 /*
1365 * do the command
1366 */
1367 return scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
1368 sizeof(cmd), (u_char *) &scsi_select, scsi_select_len,
1369 ST_RETRIES, 5000, NULL, flags | SCSI_DATA_OUT);
1370 }
1371
1372 /*
1373 * skip N blocks/filemarks/seq filemarks/eom
1374 */
1375 int
1376 st_space(st, number, what, flags)
1377 struct st_softc *st;
1378 u_int what;
1379 int flags;
1380 int number;
1381 {
1382 struct scsi_space cmd;
1383 int error;
1384
1385 switch (what) {
1386 case SP_BLKS:
1387 if (st->flags & ST_PER_ACTION) {
1388 if (number > 0) {
1389 st->flags &= ~ST_PER_ACTION;
1390 return EIO;
1391 } else if (number < 0) {
1392 if (st->flags & ST_AT_FILEMARK) {
1393 /*
1394 * Handling of ST_AT_FILEMARK
1395 * in st_space will fill in the
1396 * right file mark count.
1397 */
1398 error = st_space(st, 0, SP_FILEMARKS,
1399 flags);
1400 if (error)
1401 return error;
1402 }
1403 if (st->flags & ST_BLANK_READ) {
1404 st->flags &= ~ST_BLANK_READ;
1405 return EIO;
1406 }
1407 st->flags &= ~ST_EIO_PENDING;
1408 }
1409 }
1410 break;
1411 case SP_FILEMARKS:
1412 if (st->flags & ST_EIO_PENDING) {
1413 if (number > 0) {
1414 /* pretend we just discovered the error */
1415 st->flags &= ~ST_EIO_PENDING;
1416 return EIO;
1417 } else if (number < 0) {
1418 /* back away from the error */
1419 st->flags &= ~ST_EIO_PENDING;
1420 }
1421 }
1422 if (st->flags & ST_AT_FILEMARK) {
1423 st->flags &= ~ST_AT_FILEMARK;
1424 number--;
1425 }
1426 if ((st->flags & ST_BLANK_READ) && (number < 0)) {
1427 /* back away from unwritten tape */
1428 st->flags &= ~ST_BLANK_READ;
1429 number++; /* XXX dubious */
1430 }
1431 break;
1432 case SP_EOM:
1433 if (st->flags & ST_EIO_PENDING) {
1434 /* pretend we just discovered the error */
1435 st->flags &= ~ST_EIO_PENDING;
1436 return EIO;
1437 }
1438 if (st->flags & ST_AT_FILEMARK)
1439 st->flags &= ~ST_AT_FILEMARK;
1440 break;
1441 }
1442 if (number == 0)
1443 return 0;
1444
1445 bzero(&cmd, sizeof(cmd));
1446 cmd.opcode = SPACE;
1447 cmd.byte2 = what;
1448 lto3b(number, cmd.number);
1449
1450 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
1451 sizeof(cmd), 0, 0, 0, 900000, NULL, flags);
1452 }
1453
1454 /*
1455 * write N filemarks
1456 */
1457 int
1458 st_write_filemarks(st, number, flags)
1459 struct st_softc *st;
1460 int flags;
1461 int number;
1462 {
1463 struct scsi_write_filemarks cmd;
1464
1465 /*
1466 * It's hard to write a negative number of file marks.
1467 * Don't try.
1468 */
1469 if (number < 0)
1470 return EINVAL;
1471 switch (number) {
1472 case 0: /* really a command to sync the drive's buffers */
1473 break;
1474 case 1:
1475 if (st->flags & ST_FM_WRITTEN) /* already have one down */
1476 st->flags &= ~ST_WRITTEN;
1477 else
1478 st->flags |= ST_FM_WRITTEN;
1479 st->flags &= ~ST_PER_ACTION;
1480 break;
1481 default:
1482 st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
1483 }
1484
1485 bzero(&cmd, sizeof(cmd));
1486 cmd.opcode = WRITE_FILEMARKS;
1487 lto3b(number, cmd.number);
1488
1489 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
1490 sizeof(cmd), 0, 0, 0, 100000, NULL, flags);
1491 }
1492
1493 /*
1494 * Make sure the right number of file marks is on tape if the
1495 * tape has been written. If the position argument is true,
1496 * leave the tape positioned where it was originally.
1497 *
1498 * nmarks returns the number of marks to skip (or, if position
1499 * true, which were skipped) to get back original position.
1500 */
1501 int
1502 st_check_eod(st, position, nmarks, flags)
1503 struct st_softc *st;
1504 boolean position;
1505 int *nmarks;
1506 int flags;
1507 {
1508 int error;
1509
1510 switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
1511 default:
1512 *nmarks = 0;
1513 return 0;
1514 case ST_WRITTEN:
1515 case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
1516 *nmarks = 1;
1517 break;
1518 case ST_WRITTEN | ST_2FM_AT_EOD:
1519 *nmarks = 2;
1520 }
1521 error = st_write_filemarks(st, *nmarks, flags);
1522 if (position && !error)
1523 error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
1524 return error;
1525 }
1526
1527 /*
1528 * load/unload/retension
1529 */
1530 int
1531 st_load(st, type, flags)
1532 struct st_softc *st;
1533 u_int type;
1534 int flags;
1535 {
1536 struct scsi_load cmd;
1537
1538 if (type != LD_LOAD) {
1539 int error;
1540 int nmarks;
1541
1542 error = st_check_eod(st, FALSE, &nmarks, flags);
1543 if (error)
1544 return error;
1545 }
1546 if (st->quirks & ST_Q_IGNORE_LOADS)
1547 return 0;
1548
1549 bzero(&cmd, sizeof(cmd));
1550 cmd.opcode = LOAD;
1551 cmd.how = type;
1552
1553 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
1554 sizeof(cmd), 0, 0, ST_RETRIES, 300000, NULL, flags);
1555 }
1556
1557 /*
1558 * Rewind the device
1559 */
1560 int
1561 st_rewind(st, immediate, flags)
1562 struct st_softc *st;
1563 u_int immediate;
1564 int flags;
1565 {
1566 struct scsi_rewind cmd;
1567 int error;
1568 int nmarks;
1569
1570 error = st_check_eod(st, FALSE, &nmarks, flags);
1571 if (error)
1572 return error;
1573 st->flags &= ~ST_PER_ACTION;
1574
1575 bzero(&cmd, sizeof(cmd));
1576 cmd.opcode = REWIND;
1577 cmd.byte2 = immediate;
1578
1579 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
1580 sizeof(cmd), 0, 0, ST_RETRIES, immediate ? 5000 : 300000, NULL,
1581 flags);
1582 }
1583
1584 /*
1585 * Look at the returned sense and act on the error and detirmine
1586 * The unix error number to pass back... (0 = report no error)
1587 * (-1 = continue processing)
1588 */
1589 int
1590 st_interpret_sense(xs)
1591 struct scsi_xfer *xs;
1592 {
1593 struct scsi_link *sc_link = xs->sc_link;
1594 struct scsi_sense_data *sense = &xs->sense;
1595 struct buf *bp = xs->bp;
1596 struct st_softc *st = sc_link->device_softc;
1597 u_int8_t key;
1598 u_int32_t info;
1599
1600 /*
1601 * Get the sense fields and work out what code
1602 */
1603 if (sense->error_code & SSD_ERRCODE_VALID) {
1604 bcopy(sense->extended_info, &info, sizeof info);
1605 info = ntohl(info);
1606 } else
1607 info = xs->datalen; /* bad choice if fixed blocks */
1608 if ((sense->error_code & SSD_ERRCODE) != 0x70)
1609 return -1; /* let the generic code handle it */
1610 if (st->flags & ST_FIXEDBLOCKS) {
1611 xs->resid = info * st->blksize;
1612 if (sense->extended_flags & SSD_EOM) {
1613 st->flags |= ST_EIO_PENDING;
1614 if (bp)
1615 bp->b_resid = xs->resid;
1616 }
1617 if (sense->extended_flags & SSD_FILEMARK) {
1618 st->flags |= ST_AT_FILEMARK;
1619 if (bp)
1620 bp->b_resid = xs->resid;
1621 }
1622 if (sense->extended_flags & SSD_ILI) {
1623 st->flags |= ST_EIO_PENDING;
1624 if (bp)
1625 bp->b_resid = xs->resid;
1626 if (sense->error_code & SSD_ERRCODE_VALID &&
1627 (xs->flags & SCSI_SILENT) == 0)
1628 printf("%s: block wrong size, %d blocks residual\n",
1629 st->sc_dev.dv_xname, info);
1630
1631 /*
1632 * This quirk code helps the drive read
1633 * the first tape block, regardless of
1634 * format. That is required for these
1635 * drives to return proper MODE SENSE
1636 * information.
1637 */
1638 if ((st->quirks & ST_Q_SENSE_HELP) &&
1639 !(sc_link->flags & SDEV_MEDIA_LOADED))
1640 st->blksize -= 512;
1641 }
1642 /*
1643 * If no data was tranfered, do it immediatly
1644 */
1645 if (xs->resid >= xs->datalen) {
1646 if (st->flags & ST_EIO_PENDING)
1647 return EIO;
1648 if (st->flags & ST_AT_FILEMARK) {
1649 if (bp)
1650 bp->b_resid = xs->resid;
1651 return 0;
1652 }
1653 }
1654 } else { /* must be variable mode */
1655 xs->resid = xs->datalen; /* to be sure */
1656 if (sense->extended_flags & SSD_EOM)
1657 return EIO;
1658 if (sense->extended_flags & SSD_FILEMARK) {
1659 if (bp)
1660 bp->b_resid = bp->b_bcount;
1661 return 0;
1662 }
1663 if (sense->extended_flags & SSD_ILI) {
1664 if (info < 0) {
1665 /*
1666 * the record was bigger than the read
1667 */
1668 if ((xs->flags & SCSI_SILENT) == 0)
1669 printf("%s: %d-byte record too big\n",
1670 st->sc_dev.dv_xname,
1671 xs->datalen - info);
1672 return EIO;
1673 }
1674 xs->resid = info;
1675 if (bp)
1676 bp->b_resid = info;
1677 }
1678 }
1679 key = sense->extended_flags & SSD_KEY;
1680
1681 if (key == 0x8) {
1682 /*
1683 * This quirk code helps the drive read the
1684 * first tape block, regardless of format. That
1685 * is required for these drives to return proper
1686 * MODE SENSE information.
1687 */
1688 if ((st->quirks & ST_Q_SENSE_HELP) &&
1689 !(sc_link->flags & SDEV_MEDIA_LOADED)) {
1690 /* still starting */
1691 st->blksize -= 512;
1692 } else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
1693 st->flags |= ST_BLANK_READ;
1694 xs->resid = xs->datalen;
1695 if (bp) {
1696 bp->b_resid = xs->resid;
1697 /* return an EOF */
1698 }
1699 return 0;
1700 }
1701 }
1702 return -1; /* let the default/generic handler handle it */
1703 }
1704
1705 /*
1706 * The quirk here is that the drive returns some value to st_mode_sense
1707 * incorrectly until the tape has actually passed by the head.
1708 *
1709 * The method is to set the drive to large fixed-block state (user-specified
1710 * density and 1024-byte blocks), then read and rewind to get it to sense the
1711 * tape. If that doesn't work, try 512-byte fixed blocks. If that doesn't
1712 * work, as a last resort, try variable- length blocks. The result will be
1713 * the ability to do an accurate st_mode_sense.
1714 *
1715 * We know we can do a rewind because we just did a load, which implies rewind.
1716 * Rewind seems preferable to space backward if we have a virgin tape.
1717 *
1718 * The rest of the code for this quirk is in ILI processing and BLANK CHECK
1719 * error processing, both part of st_interpret_sense.
1720 */
1721 int
1722 st_touch_tape(st)
1723 struct st_softc *st;
1724 {
1725 char *buf;
1726 int readsize;
1727 int error;
1728
1729 buf = malloc(1024, M_TEMP, M_NOWAIT);
1730 if (!buf)
1731 return ENOMEM;
1732
1733 if (error = st_mode_sense(st, 0))
1734 goto bad;
1735 st->blksize = 1024;
1736 do {
1737 switch (st->blksize) {
1738 case 512:
1739 case 1024:
1740 readsize = st->blksize;
1741 st->flags |= ST_FIXEDBLOCKS;
1742 break;
1743 default:
1744 readsize = 1;
1745 st->flags &= ~ST_FIXEDBLOCKS;
1746 }
1747 if (error = st_mode_select(st, 0))
1748 goto bad;
1749 st_read(st, buf, readsize, SCSI_SILENT); /* XXX */
1750 if (error = st_rewind(st, 0, 0)) {
1751 bad: free(buf, M_TEMP);
1752 return error;
1753 }
1754 } while (readsize != 1 && readsize > st->blksize);
1755
1756 free(buf, M_TEMP);
1757 return 0;
1758 }
1759
1760 int
1761 stdump(dev, blkno, va, size)
1762 dev_t dev;
1763 daddr_t blkno;
1764 caddr_t va;
1765 size_t size;
1766 {
1767
1768 /* Not implemented. */
1769 return ENXIO;
1770 }
1771