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