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