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