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