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