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