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