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