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