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