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