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