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