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