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