st.c revision 1.151 1 /* $NetBSD: st.c,v 1.151 2002/03/22 02:03:30 mjacob Exp $ */
2 /*-
3 * Copyright (c) 1998 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Charles M. Hannum.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * Originally written by Julian Elischer (julian (at) tfs.com)
40 * for TRW Financial Systems for use under the MACH(2.5) operating system.
41 *
42 * TRW Financial Systems, in accordance with their agreement with Carnegie
43 * Mellon University, makes this software available to CMU to distribute
44 * or use in any manner that they see fit as long as this message is kept with
45 * the software. For this reason TFS also grants any other persons or
46 * organisations permission to use or modify this software.
47 *
48 * TFS supplies this software to be publicly redistributed
49 * on the understanding that TFS is not responsible for the correct
50 * functioning of this software in any circumstances.
51 *
52 * Ported to run under 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
53 * major changes by Julian Elischer (julian (at) jules.dialix.oz.au) May 1993
54 *
55 * A lot of rewhacking done by mjacob (mjacob (at) nas.nasa.gov).
56 */
57
58 #include <sys/cdefs.h>
59 __KERNEL_RCSID(0, "$NetBSD: st.c,v 1.151 2002/03/22 02:03:30 mjacob Exp $");
60
61 #include "opt_scsi.h"
62
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 #include <sys/vnode.h>
77
78 #include <dev/scsipi/scsi_all.h>
79 #include <dev/scsipi/scsi_tape.h>
80 #include <dev/scsipi/stvar.h>
81
82 /* Defines for device specific stuff */
83 #define DEF_FIXED_BSIZE 512
84
85 #define STMODE(z) ( minor(z) & 0x03)
86 #define STDSTY(z) ((minor(z) >> 2) & 0x03)
87 #define STUNIT(z) ((minor(z) >> 4) )
88 #define STNMINOR 16
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 "OnStream DI-30", "", "1.0"}, {ST_Q_NOFILEMARKS, 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 "NCR H621", "0-STD-03-46F880 ", ""}, {ST_Q_NOPREVENT, 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 int
390 stactivate(self, act)
391 struct device *self;
392 enum devact act;
393 {
394 int rv = 0;
395
396 switch (act) {
397 case DVACT_ACTIVATE:
398 rv = EOPNOTSUPP;
399 break;
400
401 case DVACT_DEACTIVATE:
402 /*
403 * Nothing to do; we key off the device's DVF_ACTIVE.
404 */
405 break;
406 }
407 return (rv);
408 }
409
410 int
411 stdetach(self, flags)
412 struct device *self;
413 int flags;
414 {
415 struct st_softc *st = (struct st_softc *)self;
416 struct buf *bp;
417 int s, bmaj, cmaj, mn;
418
419 /* locate the major number */
420 for (bmaj = 0; bmaj <= nblkdev; bmaj++)
421 if (bdevsw[bmaj].d_open == stopen)
422 break;
423 for (cmaj = 0; cmaj <= nchrdev; cmaj++)
424 if (cdevsw[cmaj].d_open == stopen)
425 break;
426
427 s = splbio();
428
429 /* Kill off any queued buffers. */
430 while ((bp = BUFQ_FIRST(&st->buf_queue)) != NULL) {
431 BUFQ_REMOVE(&st->buf_queue, bp);
432 bp->b_error = EIO;
433 bp->b_flags |= B_ERROR;
434 bp->b_resid = bp->b_bcount;
435 biodone(bp);
436 }
437
438 /* Kill off any pending commands. */
439 scsipi_kill_pending(st->sc_periph);
440
441 splx(s);
442
443 /* Nuke the vnodes for any open instances */
444 mn = STUNIT(self->dv_unit);
445 vdevgone(bmaj, mn, mn+STNMINOR-1, VBLK);
446 vdevgone(cmaj, mn, mn+STNMINOR-1, VCHR);
447
448
449 #if NRND > 0
450 /* Unhook the entropy source. */
451 rnd_detach_source(&st->rnd_source);
452 #endif
453
454 return (0);
455 }
456
457 /*
458 * Use the inquiry routine in 'scsi_base' to get drive info so we can
459 * Further tailor our behaviour.
460 */
461 void
462 st_identify_drive(st, inqbuf)
463 struct st_softc *st;
464 struct scsipi_inquiry_pattern *inqbuf;
465 {
466 struct st_quirk_inquiry_pattern *finger;
467 int priority;
468
469 finger = (struct st_quirk_inquiry_pattern *)scsipi_inqmatch(inqbuf,
470 (caddr_t)st_quirk_patterns,
471 sizeof(st_quirk_patterns) / sizeof(st_quirk_patterns[0]),
472 sizeof(st_quirk_patterns[0]), &priority);
473 if (priority != 0) {
474 st->quirkdata = &finger->quirkdata;
475 st->drive_quirks = finger->quirkdata.quirks;
476 st->quirks = finger->quirkdata.quirks; /* start value */
477 st->page_0_size = finger->quirkdata.page_0_size;
478 st_loadquirks(st);
479 }
480 }
481
482 /*
483 * initialise the subdevices to the default (QUIRK) state.
484 * this will remove any setting made by the system operator or previous
485 * operations.
486 */
487 void
488 st_loadquirks(st)
489 struct st_softc *st;
490 {
491 int i;
492 struct modes *mode;
493 struct modes *mode2;
494
495 mode = st->quirkdata->modes;
496 mode2 = st->modes;
497 for (i = 0; i < 4; i++) {
498 memset(mode2, 0, sizeof(struct modes));
499 st->modeflags[i] &= ~(BLKSIZE_SET_BY_QUIRK |
500 DENSITY_SET_BY_QUIRK | BLKSIZE_SET_BY_USER |
501 DENSITY_SET_BY_USER);
502 if ((mode->quirks | st->drive_quirks) & ST_Q_FORCE_BLKSIZE) {
503 mode2->blksize = mode->blksize;
504 st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK;
505 }
506 if (mode->density) {
507 mode2->density = mode->density;
508 st->modeflags[i] |= DENSITY_SET_BY_QUIRK;
509 }
510 mode2->quirks |= mode->quirks;
511 mode++;
512 mode2++;
513 }
514 }
515
516 /*
517 * open the device.
518 */
519 int
520 stopen(dev, flags, mode, p)
521 dev_t dev;
522 int flags;
523 int mode;
524 struct proc *p;
525 {
526 u_int stmode, dsty;
527 int error, sflags, unit, tries, ntries;
528 struct st_softc *st;
529 struct scsipi_periph *periph;
530 struct scsipi_adapter *adapt;
531
532 unit = STUNIT(dev);
533 if (unit >= st_cd.cd_ndevs)
534 return (ENXIO);
535 st = st_cd.cd_devs[unit];
536 if (st == NULL)
537 return (ENXIO);
538
539 stmode = STMODE(dev);
540 dsty = STDSTY(dev);
541
542 periph = st->sc_periph;
543 adapt = periph->periph_channel->chan_adapter;
544
545 SC_DEBUG(periph, SCSIPI_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
546 unit, st_cd.cd_ndevs));
547
548
549 /*
550 * Only allow one at a time
551 */
552 if (periph->periph_flags & PERIPH_OPEN) {
553 printf("%s: already open\n", st->sc_dev.dv_xname);
554 return (EBUSY);
555 }
556
557 if ((error = scsipi_adapter_addref(adapt)) != 0)
558 return (error);
559
560 /*
561 * clear any latched errors.
562 */
563 st->mt_resid = 0;
564 st->mt_erreg = 0;
565 st->asc = 0;
566 st->ascq = 0;
567
568 /*
569 * Catch any unit attention errors. Be silent about this
570 * unless we're already mounted. We ignore media change
571 * if we're in control mode or not mounted yet.
572 */
573 if ((st->flags & ST_MOUNTED) == 0 || stmode == CTRL_MODE) {
574 #ifdef SCSIDEBUG
575 sflags = XS_CTL_IGNORE_MEDIA_CHANGE;
576 #else
577 sflags = XS_CTL_SILENT|XS_CTL_IGNORE_MEDIA_CHANGE;
578 #endif
579 } else
580 sflags = 0;
581
582 /*
583 * If we're already mounted or we aren't configured for
584 * a mount delay, only try a test unit ready once. Otherwise,
585 * try up to ST_MOUNT_DELAY times with a rest interval of
586 * one second between each try.
587 */
588
589 if ((st->flags & ST_MOUNTED) || ST_MOUNT_DELAY == 0) {
590 ntries = 1;
591 } else {
592 ntries = ST_MOUNT_DELAY;
593 }
594
595 for (error = tries = 0; tries < ntries; tries++) {
596 int slpintr, oflags;
597
598 /*
599 * If we had no error, or we're opening the control mode
600 * device, we jump out right away.
601 */
602
603 error = scsipi_test_unit_ready(periph, sflags);
604 if (error == 0 || stmode == CTRL_MODE) {
605 break;
606 }
607
608 /*
609 * We had an error.
610 *
611 * If we're already mounted or we aren't configured for
612 * a mount delay, or the error isn't a NOT READY error,
613 * skip to the error exit now.
614 */
615 if ((st->flags & ST_MOUNTED) || ST_MOUNT_DELAY == 0 ||
616 (st->mt_key != SKEY_NOT_READY)) {
617 goto bad;
618 }
619
620 /*
621 * clear any latched errors.
622 */
623 st->mt_resid = 0;
624 st->mt_erreg = 0;
625 st->asc = 0;
626 st->ascq = 0;
627
628 /*
629 * Fake that we have the device open so
630 * we block other apps from getting in.
631 */
632
633 oflags = periph->periph_flags;
634 periph->periph_flags |= PERIPH_OPEN;
635
636 slpintr = tsleep(&lbolt, PUSER|PCATCH, "stload", 0);
637
638 periph->periph_flags = oflags; /* restore flags */
639 if (slpintr) {
640 goto bad;
641 }
642 }
643
644
645 /*
646 * If the mode is 3 (e.g. minor = 3,7,11,15) then the device has
647 * been opened to set defaults and perform other, usually non-I/O
648 * related, operations. In this case, do a quick check to see
649 * whether the unit actually had a tape loaded (this will be known
650 * as to whether or not we got a NOT READY for the above
651 * unit attention). If a tape is there, go do a mount sequence.
652 */
653 if (stmode == CTRL_MODE && st->mt_key == SKEY_NOT_READY) {
654 periph->periph_flags |= PERIPH_OPEN;
655 return (0);
656 }
657
658 /*
659 * If we get this far and had an error set, that means we failed
660 * to pass the 'test unit ready' test for the non-controlmode device,
661 * so we bounce the open.
662 */
663
664 if (error)
665 return (error);
666
667 /*
668 * Else, we're now committed to saying we're open.
669 */
670
671 periph->periph_flags |= PERIPH_OPEN; /* unit attn are now errors */
672
673 /*
674 * If it's a different mode, or if the media has been
675 * invalidated, unmount the tape from the previous
676 * session but continue with open processing
677 */
678 if (st->last_dsty != dsty ||
679 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
680 st_unmount(st, NOEJECT);
681
682 /*
683 * If we are not mounted, then we should start a new
684 * mount session.
685 */
686 if (!(st->flags & ST_MOUNTED)) {
687 if ((error = st_mount_tape(dev, flags)) != 0)
688 goto bad;
689 st->last_dsty = dsty;
690 }
691 if (!(st->quirks & ST_Q_NOPREVENT)) {
692 scsipi_prevent(periph, PR_PREVENT,
693 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_NOT_READY);
694 }
695
696 SC_DEBUG(periph, SCSIPI_DB2, ("open complete\n"));
697 return (0);
698
699 bad:
700 st_unmount(st, NOEJECT);
701 scsipi_adapter_delref(adapt);
702 periph->periph_flags &= ~PERIPH_OPEN;
703 return (error);
704 }
705
706 /*
707 * close the device.. only called if we are the LAST
708 * occurence of an open device
709 */
710 int
711 stclose(dev, flags, mode, p)
712 dev_t dev;
713 int flags;
714 int mode;
715 struct proc *p;
716 {
717 int stxx, error = 0;
718 struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
719 struct scsipi_periph *periph = st->sc_periph;
720 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
721
722 SC_DEBUG(st->sc_periph, SCSIPI_DB1, ("closing\n"));
723
724 /*
725 * Make sure that a tape opened in write-only mode will have
726 * file marks written on it when closed, even if not written to.
727 *
728 * This is for SUN compatibility. Actually, the Sun way of
729 * things is to:
730 *
731 * only write filemarks if there are fmks to be written and
732 * - open for write (possibly read/write)
733 * - the last operation was a write
734 * or:
735 * - opened for wronly
736 * - no data was written (including filemarks)
737 */
738
739 stxx = st->flags & (ST_WRITTEN | ST_FM_WRITTEN);
740 if (((flags & FWRITE) && stxx == ST_WRITTEN) ||
741 ((flags & O_ACCMODE) == FWRITE && stxx == 0)) {
742 int nm;
743 error = st_check_eod(st, FALSE, &nm, 0);
744 }
745
746 /*
747 * Allow robots to eject tape if needed.
748 */
749 scsipi_prevent(periph, PR_ALLOW,
750 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_NOT_READY);
751
752 switch (STMODE(dev)) {
753 case NORMAL_MODE:
754 st_unmount(st, NOEJECT);
755 break;
756 case NOREW_MODE:
757 case CTRL_MODE:
758 /*
759 * Leave mounted unless media seems to have been removed.
760 *
761 * Otherwise, if we're to terminate a tape with more than one
762 * filemark [ and because we're not rewinding here ], backspace
763 * one filemark so that later appends will see an unbroken
764 * sequence of:
765 *
766 * file - FMK - file - FMK ... file - FMK FMK (EOM)
767 */
768 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
769 st_unmount(st, NOEJECT);
770 } else if (error == 0) {
771 /*
772 * ST_WRITTEN was preserved from above.
773 *
774 * All we need to know here is:
775 *
776 * Were we writing this tape and was the last
777 * operation a write?
778 *
779 * Are there supposed to be 2FM at EOD?
780 *
781 * If both statements are true, then we backspace
782 * one filemark.
783 */
784 stxx |= (st->flags & ST_2FM_AT_EOD);
785 if ((flags & FWRITE) != 0 &&
786 (stxx == (ST_2FM_AT_EOD|ST_WRITTEN))) {
787 error = st_space(st, -1, SP_FILEMARKS, 0);
788 }
789 }
790 break;
791 case EJECT_MODE:
792 st_unmount(st, EJECT);
793 break;
794 }
795
796 scsipi_wait_drain(periph);
797
798 scsipi_adapter_delref(adapt);
799 periph->periph_flags &= ~PERIPH_OPEN;
800
801 return (error);
802 }
803
804 /*
805 * Start a new mount session.
806 * Copy in all the default parameters from the selected device mode.
807 * and try guess any that seem to be defaulted.
808 */
809 int
810 st_mount_tape(dev, flags)
811 dev_t dev;
812 int flags;
813 {
814 int unit;
815 u_int dsty;
816 struct st_softc *st;
817 struct scsipi_periph *periph;
818 int error = 0;
819
820 unit = STUNIT(dev);
821 dsty = STDSTY(dev);
822 st = st_cd.cd_devs[unit];
823 periph = st->sc_periph;
824
825 if (st->flags & ST_MOUNTED)
826 return (0);
827
828 SC_DEBUG(periph, SCSIPI_DB1, ("mounting\n "));
829 st->flags |= ST_NEW_MOUNT;
830 st->quirks = st->drive_quirks | st->modes[dsty].quirks;
831 /*
832 * If the media is new, then make sure we give it a chance to
833 * to do a 'load' instruction. (We assume it is new.)
834 */
835 if ((error = st_load(st, LD_LOAD, XS_CTL_SILENT)) != 0)
836 return (error);
837 /*
838 * Throw another dummy instruction to catch
839 * 'Unit attention' errors. Many drives give
840 * these after doing a Load instruction (with
841 * the MEDIUM MAY HAVE CHANGED asc/ascq).
842 */
843 scsipi_test_unit_ready(periph, XS_CTL_SILENT); /* XXX */
844
845 /*
846 * Some devices can't tell you much until they have been
847 * asked to look at the media. This quirk does this.
848 */
849 if (st->quirks & ST_Q_SENSE_HELP)
850 if ((error = st_touch_tape(st)) != 0)
851 return (error);
852 /*
853 * Load the physical device parameters
854 * loads: blkmin, blkmax
855 */
856 if ((error = st->ops(st, ST_OPS_RBL, 0)) != 0)
857 return (error);
858 /*
859 * Load the media dependent parameters
860 * includes: media_blksize,media_density,numblks
861 * As we have a tape in, it should be reflected here.
862 * If not you may need the "quirk" above.
863 */
864 if ((error = st->ops(st, ST_OPS_MODESENSE, 0)) != 0)
865 return (error);
866 /*
867 * If we have gained a permanent density from somewhere,
868 * then use it in preference to the one supplied by
869 * default by the driver.
870 */
871 if (st->modeflags[dsty] & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER))
872 st->density = st->modes[dsty].density;
873 else
874 st->density = st->media_density;
875 /*
876 * If we have gained a permanent blocksize
877 * then use it in preference to the one supplied by
878 * default by the driver.
879 */
880 st->flags &= ~ST_FIXEDBLOCKS;
881 if (st->modeflags[dsty] &
882 (BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) {
883 st->blksize = st->modes[dsty].blksize;
884 if (st->blksize)
885 st->flags |= ST_FIXEDBLOCKS;
886 } else {
887 if ((error = st_decide_mode(st, FALSE)) != 0)
888 return (error);
889 }
890 if ((error = st->ops(st, ST_OPS_MODESELECT, 0)) != 0) {
891 /* ATAPI will return ENODEV for this, and this may be OK */
892 if (error != ENODEV) {
893 printf("%s: cannot set selected mode\n",
894 st->sc_dev.dv_xname);
895 return (error);
896 }
897 }
898 st->flags &= ~ST_NEW_MOUNT;
899 st->flags |= ST_MOUNTED;
900 periph->periph_flags |= PERIPH_MEDIA_LOADED; /* move earlier? */
901 st->blkno = st->fileno = (daddr_t) 0;
902 return (0);
903 }
904
905 /*
906 * End the present mount session.
907 * Rewind, and optionally eject the tape.
908 * Reset various flags to indicate that all new
909 * operations require another mount operation
910 */
911 void
912 st_unmount(st, eject)
913 struct st_softc *st;
914 boolean eject;
915 {
916 struct scsipi_periph *periph = st->sc_periph;
917 int nmarks;
918
919 if ((st->flags & ST_MOUNTED) == 0)
920 return;
921 SC_DEBUG(periph, SCSIPI_DB1, ("unmounting\n"));
922 st_check_eod(st, FALSE, &nmarks, XS_CTL_IGNORE_NOT_READY);
923 st_rewind(st, 0, XS_CTL_IGNORE_NOT_READY);
924
925 /*
926 * Section 9.3.3 of the SCSI specs states that a device shall return
927 * the density value specified in the last succesfull MODE SELECT
928 * after an unload operation, in case it is not able to
929 * automatically determine the density of the new medium.
930 *
931 * So we instruct the device to use the default density, which will
932 * prevent the use of stale density values (in particular,
933 * in st_touch_tape().
934 */
935 st->density = 0;
936 if (st->ops(st, ST_OPS_MODESELECT, 0) != 0) {
937 printf("%s: WARNING: cannot revert to default density\n",
938 st->sc_dev.dv_xname);
939 }
940
941 if (eject) {
942 st_load(st, LD_UNLOAD, XS_CTL_IGNORE_NOT_READY);
943 st->blkno = st->fileno = (daddr_t) -1;
944 } else {
945 st->blkno = st->fileno = (daddr_t) 0;
946 }
947 st->flags &= ~(ST_MOUNTED | ST_NEW_MOUNT);
948 periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
949 }
950
951 /*
952 * Given all we know about the device, media, mode, 'quirks' and
953 * initial operation, make a decision as to how we should be set
954 * to run (regarding blocking and EOD marks)
955 */
956 int
957 st_decide_mode(st, first_read)
958 struct st_softc *st;
959 boolean first_read;
960 {
961
962 SC_DEBUG(st->sc_periph, SCSIPI_DB2, ("starting block mode decision\n"));
963
964 /*
965 * If the drive can only handle fixed-length blocks and only at
966 * one size, perhaps we should just do that.
967 */
968 if (st->blkmin && (st->blkmin == st->blkmax)) {
969 st->flags |= ST_FIXEDBLOCKS;
970 st->blksize = st->blkmin;
971 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
972 ("blkmin == blkmax of %d\n", st->blkmin));
973 goto done;
974 }
975 /*
976 * If the tape density mandates (or even suggests) use of fixed
977 * or variable-length blocks, comply.
978 */
979 switch (st->density) {
980 case HALFINCH_800:
981 case HALFINCH_1600:
982 case HALFINCH_6250:
983 case DDS:
984 st->flags &= ~ST_FIXEDBLOCKS;
985 st->blksize = 0;
986 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
987 ("density specified variable\n"));
988 goto done;
989 case QIC_11:
990 case QIC_24:
991 case QIC_120:
992 case QIC_150:
993 case QIC_525:
994 case QIC_1320:
995 case QIC_3095:
996 case QIC_3220:
997 st->flags |= ST_FIXEDBLOCKS;
998 if (st->media_blksize > 0)
999 st->blksize = st->media_blksize;
1000 else
1001 st->blksize = DEF_FIXED_BSIZE;
1002 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
1003 ("density specified fixed\n"));
1004 goto done;
1005 }
1006 /*
1007 * If we're about to read the tape, perhaps we should choose
1008 * fixed or variable-length blocks and block size according to
1009 * what the drive found on the tape.
1010 */
1011 if (first_read &&
1012 (!(st->quirks & ST_Q_BLKSIZE) || (st->media_blksize == 0) ||
1013 (st->media_blksize == DEF_FIXED_BSIZE) ||
1014 (st->media_blksize == 1024))) {
1015 if (st->media_blksize > 0)
1016 st->flags |= ST_FIXEDBLOCKS;
1017 else
1018 st->flags &= ~ST_FIXEDBLOCKS;
1019 st->blksize = st->media_blksize;
1020 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
1021 ("Used media_blksize of %d\n", st->media_blksize));
1022 goto done;
1023 }
1024 /*
1025 * We're getting no hints from any direction. Choose variable-
1026 * length blocks arbitrarily.
1027 */
1028 st->flags &= ~ST_FIXEDBLOCKS;
1029 st->blksize = 0;
1030 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
1031 ("Give up and default to variable mode\n"));
1032
1033 done:
1034 /*
1035 * Decide whether or not to write two file marks to signify end-
1036 * of-data. Make the decision as a function of density. If
1037 * the decision is not to use a second file mark, the SCSI BLANK
1038 * CHECK condition code will be recognized as end-of-data when
1039 * first read.
1040 * (I think this should be a by-product of fixed/variable..julian)
1041 */
1042 switch (st->density) {
1043 /* case 8 mm: What is the SCSI density code for 8 mm, anyway? */
1044 case QIC_11:
1045 case QIC_24:
1046 case QIC_120:
1047 case QIC_150:
1048 case QIC_525:
1049 case QIC_1320:
1050 case QIC_3095:
1051 case QIC_3220:
1052 st->flags &= ~ST_2FM_AT_EOD;
1053 break;
1054 default:
1055 st->flags |= ST_2FM_AT_EOD;
1056 }
1057 return (0);
1058 }
1059
1060 /*
1061 * Actually translate the requested transfer into
1062 * one the physical driver can understand
1063 * The transfer is described by a buf and will include
1064 * only one physical transfer.
1065 */
1066 void
1067 ststrategy(bp)
1068 struct buf *bp;
1069 {
1070 struct st_softc *st = st_cd.cd_devs[STUNIT(bp->b_dev)];
1071 int s;
1072
1073 SC_DEBUG(st->sc_periph, SCSIPI_DB1,
1074 ("ststrategy %ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
1075 /*
1076 * If it's a null transfer, return immediatly
1077 */
1078 if (bp->b_bcount == 0)
1079 goto done;
1080
1081 /* If offset is negative, error */
1082 if (bp->b_blkno < 0) {
1083 bp->b_error = EINVAL;
1084 goto bad;
1085 }
1086
1087 /*
1088 * Odd sized request on fixed drives are verboten
1089 */
1090 if (st->flags & ST_FIXEDBLOCKS) {
1091 if (bp->b_bcount % st->blksize) {
1092 printf("%s: bad request, must be multiple of %d\n",
1093 st->sc_dev.dv_xname, st->blksize);
1094 bp->b_error = EIO;
1095 goto bad;
1096 }
1097 }
1098 /*
1099 * as are out-of-range requests on variable drives.
1100 */
1101 else if (bp->b_bcount < st->blkmin ||
1102 (st->blkmax && bp->b_bcount > st->blkmax)) {
1103 printf("%s: bad request, must be between %d and %d\n",
1104 st->sc_dev.dv_xname, st->blkmin, st->blkmax);
1105 bp->b_error = EIO;
1106 goto bad;
1107 }
1108 s = splbio();
1109
1110 /*
1111 * Place it in the queue of activities for this tape
1112 * at the end (a bit silly because we only have on user..
1113 * (but it could fork()))
1114 */
1115 BUFQ_INSERT_TAIL(&st->buf_queue, bp);
1116
1117 /*
1118 * Tell the device to get going on the transfer if it's
1119 * not doing anything, otherwise just wait for completion
1120 * (All a bit silly if we're only allowing 1 open but..)
1121 */
1122 ststart(st->sc_periph);
1123
1124 splx(s);
1125 return;
1126 bad:
1127 bp->b_flags |= B_ERROR;
1128 done:
1129 /*
1130 * Correctly set the buf to indicate a completed xfer
1131 */
1132 bp->b_resid = bp->b_bcount;
1133 biodone(bp);
1134 return;
1135 }
1136
1137 /*
1138 * ststart looks to see if there is a buf waiting for the device
1139 * and that the device is not already busy. If both are true,
1140 * It dequeues the buf and creates a scsi command to perform the
1141 * transfer required. The transfer request will call scsipi_done
1142 * on completion, which will in turn call this routine again
1143 * so that the next queued transfer is performed.
1144 * The bufs are queued by the strategy routine (ststrategy)
1145 *
1146 * This routine is also called after other non-queued requests
1147 * have been made of the scsi driver, to ensure that the queue
1148 * continues to be drained.
1149 * ststart() is called at splbio
1150 */
1151 void
1152 ststart(periph)
1153 struct scsipi_periph *periph;
1154 {
1155 struct st_softc *st = (void *)periph->periph_dev;
1156 struct buf *bp;
1157 struct scsi_rw_tape cmd;
1158 int flags, error;
1159
1160 SC_DEBUG(periph, SCSIPI_DB2, ("ststart "));
1161 /*
1162 * See if there is a buf to do and we are not already
1163 * doing one
1164 */
1165 while (periph->periph_active < periph->periph_openings) {
1166 /* if a special awaits, let it proceed first */
1167 if (periph->periph_flags & PERIPH_WAITING) {
1168 periph->periph_flags &= ~PERIPH_WAITING;
1169 wakeup((caddr_t)periph);
1170 return;
1171 }
1172
1173 if ((bp = BUFQ_FIRST(&st->buf_queue)) == NULL)
1174 return;
1175 BUFQ_REMOVE(&st->buf_queue, bp);
1176
1177 /*
1178 * If the device has been unmounted by the user
1179 * then throw away all requests until done.
1180 */
1181 if ((st->flags & ST_MOUNTED) == 0 ||
1182 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
1183 /* make sure that one implies the other.. */
1184 periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
1185 bp->b_flags |= B_ERROR;
1186 bp->b_error = EIO;
1187 bp->b_resid = bp->b_bcount;
1188 biodone(bp);
1189 continue;
1190 }
1191 /*
1192 * only FIXEDBLOCK devices have pending I/O or space operations.
1193 */
1194 if (st->flags & ST_FIXEDBLOCKS) {
1195 /*
1196 * If we are at a filemark but have not reported it yet
1197 * then we should report it now
1198 */
1199 if (st->flags & ST_AT_FILEMARK) {
1200 if ((bp->b_flags & B_READ) == B_WRITE) {
1201 /*
1202 * Handling of ST_AT_FILEMARK in
1203 * st_space will fill in the right file
1204 * mark count.
1205 * Back up over filemark
1206 */
1207 if (st_space(st, 0, SP_FILEMARKS, 0)) {
1208 bp->b_flags |= B_ERROR;
1209 bp->b_error = EIO;
1210 biodone(bp);
1211 continue;
1212 }
1213 } else {
1214 bp->b_resid = bp->b_bcount;
1215 bp->b_error = 0;
1216 bp->b_flags &= ~B_ERROR;
1217 st->flags &= ~ST_AT_FILEMARK;
1218 biodone(bp);
1219 continue; /* seek more work */
1220 }
1221 }
1222 }
1223 /*
1224 * If we are at EOM but have not reported it
1225 * yet then we should report it now.
1226 */
1227 if (st->flags & (ST_EOM_PENDING|ST_EIO_PENDING)) {
1228 bp->b_resid = bp->b_bcount;
1229 if (st->flags & ST_EIO_PENDING) {
1230 bp->b_error = EIO;
1231 bp->b_flags |= B_ERROR;
1232 }
1233 st->flags &= ~(ST_EOM_PENDING|ST_EIO_PENDING);
1234 biodone(bp);
1235 continue; /* seek more work */
1236 }
1237
1238 /*
1239 * Fill out the scsi command
1240 */
1241 memset(&cmd, 0, sizeof(cmd));
1242 flags = XS_CTL_NOSLEEP | XS_CTL_ASYNC;
1243 if ((bp->b_flags & B_READ) == B_WRITE) {
1244 cmd.opcode = WRITE;
1245 st->flags &= ~ST_FM_WRITTEN;
1246 flags |= XS_CTL_DATA_OUT;
1247 } else {
1248 cmd.opcode = READ;
1249 flags |= XS_CTL_DATA_IN;
1250 }
1251
1252 /*
1253 * Handle "fixed-block-mode" tape drives by using the
1254 * block count instead of the length.
1255 */
1256 if (st->flags & ST_FIXEDBLOCKS) {
1257 cmd.byte2 |= SRW_FIXED;
1258 _lto3b(bp->b_bcount / st->blksize, cmd.len);
1259 } else
1260 _lto3b(bp->b_bcount, cmd.len);
1261
1262 /*
1263 * Clear 'position updated' indicator
1264 */
1265 st->flags &= ~ST_POSUPDATED;
1266
1267 /*
1268 * go ask the adapter to do all this for us
1269 */
1270 error = scsipi_command(periph,
1271 (struct scsipi_generic *)&cmd, sizeof(cmd),
1272 (u_char *)bp->b_data, bp->b_bcount,
1273 0, ST_IO_TIME, bp, flags);
1274 if (error) {
1275 printf("%s: not queued, error %d\n",
1276 st->sc_dev.dv_xname, error);
1277 }
1278 } /* go back and see if we can cram more work in.. */
1279 }
1280
1281 void
1282 stdone(xs)
1283 struct scsipi_xfer *xs;
1284 {
1285 struct st_softc *st = (void *)xs->xs_periph->periph_dev;
1286
1287 if (xs->bp != NULL) {
1288 if ((xs->bp->b_flags & B_READ) == B_WRITE) {
1289 st->flags |= ST_WRITTEN;
1290 } else {
1291 st->flags &= ~ST_WRITTEN;
1292 }
1293 #if NRND > 0
1294 rnd_add_uint32(&st->rnd_source, xs->bp->b_blkno);
1295 #endif
1296
1297 if ((st->flags & ST_POSUPDATED) == 0) {
1298 if (xs->bp->b_flags & B_ERROR) {
1299 st->fileno = st->blkno = -1;
1300 } else if (st->blkno != -1) {
1301 if (st->flags & ST_FIXEDBLOCKS) {
1302 st->blkno +=
1303 (xs->bp->b_bcount / st->blksize);
1304 } else {
1305 st->blkno++;
1306 }
1307 }
1308 }
1309 }
1310 }
1311
1312 int
1313 stread(dev, uio, iomode)
1314 dev_t dev;
1315 struct uio *uio;
1316 int iomode;
1317 {
1318 struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
1319
1320 return (physio(ststrategy, NULL, dev, B_READ,
1321 st->sc_periph->periph_channel->chan_adapter->adapt_minphys, uio));
1322 }
1323
1324 int
1325 stwrite(dev, uio, iomode)
1326 dev_t dev;
1327 struct uio *uio;
1328 int iomode;
1329 {
1330 struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
1331
1332 return (physio(ststrategy, NULL, dev, B_WRITE,
1333 st->sc_periph->periph_channel->chan_adapter->adapt_minphys, uio));
1334 }
1335
1336 /*
1337 * Perform special action on behalf of the user;
1338 * knows about the internals of this device
1339 */
1340 int
1341 stioctl(dev, cmd, arg, flag, p)
1342 dev_t dev;
1343 u_long cmd;
1344 caddr_t arg;
1345 int flag;
1346 struct proc *p;
1347 {
1348 int error = 0;
1349 int unit;
1350 int number, nmarks, dsty;
1351 int flags;
1352 struct st_softc *st;
1353 int hold_blksize;
1354 u_int8_t hold_density;
1355 struct mtop *mt = (struct mtop *) arg;
1356
1357 /*
1358 * Find the device that the user is talking about
1359 */
1360 flags = 0; /* give error messages, act on errors etc. */
1361 unit = STUNIT(dev);
1362 dsty = STDSTY(dev);
1363 st = st_cd.cd_devs[unit];
1364 hold_blksize = st->blksize;
1365 hold_density = st->density;
1366
1367 switch ((u_int) cmd) {
1368
1369 case MTIOCGET: {
1370 struct mtget *g = (struct mtget *) arg;
1371 /*
1372 * (to get the current state of READONLY)
1373 */
1374 error = st->ops(st, ST_OPS_MODESENSE, XS_CTL_SILENT);
1375 if (error) {
1376 /*
1377 * Ignore the error if in control mode;
1378 * this is mandated by st(4).
1379 */
1380 if (STMODE(dev) != CTRL_MODE)
1381 break;
1382 error = 0;
1383 }
1384 SC_DEBUG(st->sc_periph, SCSIPI_DB1, ("[ioctl: get status]\n"));
1385 memset(g, 0, sizeof(struct mtget));
1386 g->mt_type = 0x7; /* Ultrix compat *//*? */
1387 g->mt_blksiz = st->blksize;
1388 g->mt_density = st->density;
1389 g->mt_mblksiz[0] = st->modes[0].blksize;
1390 g->mt_mblksiz[1] = st->modes[1].blksize;
1391 g->mt_mblksiz[2] = st->modes[2].blksize;
1392 g->mt_mblksiz[3] = st->modes[3].blksize;
1393 g->mt_mdensity[0] = st->modes[0].density;
1394 g->mt_mdensity[1] = st->modes[1].density;
1395 g->mt_mdensity[2] = st->modes[2].density;
1396 g->mt_mdensity[3] = st->modes[3].density;
1397 g->mt_fileno = st->fileno;
1398 g->mt_blkno = st->blkno;
1399 if (st->flags & ST_READONLY)
1400 g->mt_dsreg |= MT_DS_RDONLY;
1401 if (st->flags & ST_MOUNTED)
1402 g->mt_dsreg |= MT_DS_MOUNTED;
1403 g->mt_resid = st->mt_resid;
1404 g->mt_erreg = st->mt_erreg;
1405 /*
1406 * clear latched errors.
1407 */
1408 st->mt_resid = 0;
1409 st->mt_erreg = 0;
1410 st->asc = 0;
1411 st->ascq = 0;
1412 break;
1413 }
1414 case MTIOCTOP: {
1415
1416 SC_DEBUG(st->sc_periph, SCSIPI_DB1,
1417 ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op,
1418 mt->mt_count));
1419
1420 /* compat: in U*x it is a short */
1421 number = mt->mt_count;
1422 switch ((short) (mt->mt_op)) {
1423 case MTWEOF: /* write an end-of-file record */
1424 error = st_write_filemarks(st, number, flags);
1425 break;
1426 case MTBSF: /* backward space file */
1427 number = -number;
1428 case MTFSF: /* forward space file */
1429 error = st_check_eod(st, FALSE, &nmarks, flags);
1430 if (!error)
1431 error = st_space(st, number - nmarks,
1432 SP_FILEMARKS, flags);
1433 break;
1434 case MTBSR: /* backward space record */
1435 number = -number;
1436 case MTFSR: /* forward space record */
1437 error = st_check_eod(st, TRUE, &nmarks, flags);
1438 if (!error)
1439 error = st_space(st, number, SP_BLKS, flags);
1440 break;
1441 case MTREW: /* rewind */
1442 error = st_rewind(st, 0, flags);
1443 break;
1444 case MTOFFL: /* rewind and put the drive offline */
1445 st_unmount(st, EJECT);
1446 break;
1447 case MTNOP: /* no operation, sets status only */
1448 break;
1449 case MTRETEN: /* retension the tape */
1450 error = st_load(st, LD_RETENSION, flags);
1451 if (!error)
1452 error = st_load(st, LD_LOAD, flags);
1453 break;
1454 case MTEOM: /* forward space to end of media */
1455 error = st_check_eod(st, FALSE, &nmarks, flags);
1456 if (!error)
1457 error = st_space(st, 1, SP_EOM, flags);
1458 break;
1459 case MTCACHE: /* enable controller cache */
1460 st->flags &= ~ST_DONTBUFFER;
1461 goto try_new_value;
1462 case MTNOCACHE: /* disable controller cache */
1463 st->flags |= ST_DONTBUFFER;
1464 goto try_new_value;
1465 case MTERASE: /* erase volume */
1466 error = st_erase(st, number, flags);
1467 break;
1468 case MTSETBSIZ: /* Set block size for device */
1469 #ifdef NOTYET
1470 if (!(st->flags & ST_NEW_MOUNT)) {
1471 uprintf("re-mount tape before changing blocksize");
1472 error = EINVAL;
1473 break;
1474 }
1475 #endif
1476 if (number == 0)
1477 st->flags &= ~ST_FIXEDBLOCKS;
1478 else {
1479 if ((st->blkmin || st->blkmax) &&
1480 (number < st->blkmin ||
1481 number > st->blkmax)) {
1482 error = EINVAL;
1483 break;
1484 }
1485 st->flags |= ST_FIXEDBLOCKS;
1486 }
1487 st->blksize = number;
1488 st->flags |= ST_BLOCK_SET; /*XXX */
1489 goto try_new_value;
1490
1491 case MTSETDNSTY: /* Set density for device and mode */
1492 /*
1493 * Any number >= 0 and <= 0xff is legal. Numbers
1494 * above 0x80 are 'vendor unique'.
1495 */
1496 if (number < 0 || number > 255) {
1497 error = EINVAL;
1498 break;
1499 } else
1500 st->density = number;
1501 goto try_new_value;
1502
1503 case MTCMPRESS:
1504 error = st->ops(st, (number == 0) ?
1505 ST_OPS_CMPRSS_OFF : ST_OPS_CMPRSS_ON,
1506 XS_CTL_SILENT);
1507 break;
1508
1509 case MTEWARN:
1510 if (number)
1511 st->flags |= ST_EARLYWARN;
1512 else
1513 st->flags &= ~ST_EARLYWARN;
1514 break;
1515
1516 default:
1517 error = EINVAL;
1518 }
1519 break;
1520 }
1521 case MTIOCIEOT:
1522 case MTIOCEEOT:
1523 break;
1524
1525 case MTIOCRDSPOS:
1526 error = st_rdpos(st, 0, (u_int32_t *) arg);
1527 break;
1528
1529 case MTIOCRDHPOS:
1530 error = st_rdpos(st, 1, (u_int32_t *) arg);
1531 break;
1532
1533 case MTIOCSLOCATE:
1534 error = st_setpos(st, 0, (u_int32_t *) arg);
1535 break;
1536
1537 case MTIOCHLOCATE:
1538 error = st_setpos(st, 1, (u_int32_t *) arg);
1539 break;
1540
1541
1542 default:
1543 error = scsipi_do_ioctl(st->sc_periph, dev, cmd, arg,
1544 flag, p);
1545 break;
1546 }
1547 return (error);
1548 /*-----------------------------*/
1549 try_new_value:
1550 /*
1551 * Check that the mode being asked for is aggreeable to the
1552 * drive. If not, put it back the way it was.
1553 *
1554 * If in control mode, we can make (persistent) mode changes
1555 * even if no medium is loaded (see st(4)).
1556 */
1557 if ((STMODE(dev) != CTRL_MODE || (st->flags & ST_MOUNTED) != 0) &&
1558 (error = st->ops(st, ST_OPS_MODESELECT, 0)) != 0) {
1559 /* put it back as it was */
1560 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
1561 st->density = hold_density;
1562 st->blksize = hold_blksize;
1563 if (st->blksize)
1564 st->flags |= ST_FIXEDBLOCKS;
1565 else
1566 st->flags &= ~ST_FIXEDBLOCKS;
1567 return (error);
1568 }
1569 /*
1570 * As the drive liked it, if we are setting a new default,
1571 * set it into the structures as such.
1572 *
1573 * The means for deciding this are not finalised yet- but
1574 * if the device was opened in Control Mode, the values
1575 * are persistent now across mounts.
1576 */
1577 if (STMODE(dev) == CTRL_MODE) {
1578 switch ((short) (mt->mt_op)) {
1579 case MTSETBSIZ:
1580 st->modes[dsty].blksize = st->blksize;
1581 st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
1582 break;
1583 case MTSETDNSTY:
1584 st->modes[dsty].density = st->density;
1585 st->modeflags[dsty] |= DENSITY_SET_BY_USER;
1586 break;
1587 }
1588 }
1589 return (0);
1590 }
1591
1592 /*
1593 * Do a synchronous read.
1594 */
1595 int
1596 st_read(st, buf, size, flags)
1597 struct st_softc *st;
1598 int size;
1599 int flags;
1600 char *buf;
1601 {
1602 struct scsi_rw_tape cmd;
1603
1604 /*
1605 * If it's a null transfer, return immediatly
1606 */
1607 if (size == 0)
1608 return (0);
1609 memset(&cmd, 0, sizeof(cmd));
1610 cmd.opcode = READ;
1611 if (st->flags & ST_FIXEDBLOCKS) {
1612 cmd.byte2 |= SRW_FIXED;
1613 _lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE),
1614 cmd.len);
1615 } else
1616 _lto3b(size, cmd.len);
1617 return (scsipi_command(st->sc_periph,
1618 (struct scsipi_generic *)&cmd, sizeof(cmd),
1619 (u_char *)buf, size, 0, ST_IO_TIME, NULL, flags | XS_CTL_DATA_IN));
1620 }
1621
1622 /*
1623 * issue an erase command
1624 */
1625 int
1626 st_erase(st, full, flags)
1627 struct st_softc *st;
1628 int full, flags;
1629 {
1630 int tmo;
1631 struct scsi_erase cmd;
1632
1633 /*
1634 * Full erase means set LONG bit in erase command, which asks
1635 * the drive to erase the entire unit. Without this bit, we're
1636 * asking the drive to write an erase gap.
1637 */
1638 memset(&cmd, 0, sizeof(cmd));
1639 cmd.opcode = ERASE;
1640 if (full) {
1641 cmd.byte2 = SE_LONG;
1642 tmo = ST_SPC_TIME;
1643 } else {
1644 tmo = ST_IO_TIME;
1645 }
1646
1647 /*
1648 * XXX We always do this asynchronously, for now, unless the device
1649 * has the ST_Q_ERASE_NOIMM quirk. How long should we wait if we
1650 * want to (eventually) to it synchronously?
1651 */
1652 if ((st->quirks & ST_Q_ERASE_NOIMM) == 0)
1653 cmd.byte2 |= SE_IMMED;
1654
1655 return (scsipi_command(st->sc_periph,
1656 (struct scsipi_generic *)&cmd, sizeof(cmd),
1657 0, 0, ST_RETRIES, tmo, NULL, flags));
1658 }
1659
1660 /*
1661 * skip N blocks/filemarks/seq filemarks/eom
1662 */
1663 int
1664 st_space(st, number, what, flags)
1665 struct st_softc *st;
1666 u_int what;
1667 int flags;
1668 int number;
1669 {
1670 struct scsi_space cmd;
1671 int error;
1672
1673 switch (what) {
1674 case SP_BLKS:
1675 if (st->flags & ST_PER_ACTION) {
1676 if (number > 0) {
1677 st->flags &= ~ST_PER_ACTION;
1678 return (EIO);
1679 } else if (number < 0) {
1680 if (st->flags & ST_AT_FILEMARK) {
1681 /*
1682 * Handling of ST_AT_FILEMARK
1683 * in st_space will fill in the
1684 * right file mark count.
1685 */
1686 error = st_space(st, 0, SP_FILEMARKS,
1687 flags);
1688 if (error)
1689 return (error);
1690 }
1691 if (st->flags & ST_BLANK_READ) {
1692 st->flags &= ~ST_BLANK_READ;
1693 return (EIO);
1694 }
1695 st->flags &= ~(ST_EIO_PENDING|ST_EOM_PENDING);
1696 }
1697 }
1698 break;
1699 case SP_FILEMARKS:
1700 if (st->flags & ST_EIO_PENDING) {
1701 if (number > 0) {
1702 /* pretend we just discovered the error */
1703 st->flags &= ~ST_EIO_PENDING;
1704 return (EIO);
1705 } else if (number < 0) {
1706 /* back away from the error */
1707 st->flags &= ~ST_EIO_PENDING;
1708 }
1709 }
1710 if (st->flags & ST_AT_FILEMARK) {
1711 st->flags &= ~ST_AT_FILEMARK;
1712 number--;
1713 }
1714 if ((st->flags & ST_BLANK_READ) && (number < 0)) {
1715 /* back away from unwritten tape */
1716 st->flags &= ~ST_BLANK_READ;
1717 number++; /* XXX dubious */
1718 }
1719 break;
1720 case SP_EOM:
1721 if (st->flags & ST_EOM_PENDING) {
1722 /* we're already there */
1723 st->flags &= ~ST_EOM_PENDING;
1724 return (0);
1725 }
1726 if (st->flags & ST_EIO_PENDING) {
1727 /* pretend we just discovered the error */
1728 st->flags &= ~ST_EIO_PENDING;
1729 return (EIO);
1730 }
1731 if (st->flags & ST_AT_FILEMARK)
1732 st->flags &= ~ST_AT_FILEMARK;
1733 break;
1734 }
1735 if (number == 0)
1736 return (0);
1737
1738 memset(&cmd, 0, sizeof(cmd));
1739 cmd.opcode = SPACE;
1740 cmd.byte2 = what;
1741 _lto3b(number, cmd.number);
1742
1743 st->flags &= ~ST_POSUPDATED;
1744 st->last_ctl_resid = 0;
1745 error = scsipi_command(st->sc_periph,
1746 (struct scsipi_generic *)&cmd, sizeof(cmd),
1747 0, 0, 0, ST_SPC_TIME, NULL, flags);
1748
1749 if (error == 0 && (st->flags & ST_POSUPDATED) == 0) {
1750 number = number - st->last_ctl_resid;
1751 if (what == SP_BLKS) {
1752 if (st->blkno != -1) {
1753 st->blkno += number;
1754 }
1755 } else if (what == SP_FILEMARKS) {
1756 if (st->fileno != -1) {
1757 st->fileno += number;
1758 if (number > 0) {
1759 st->blkno = 0;
1760 } else if (number < 0) {
1761 st->blkno = -1;
1762 }
1763 }
1764 } else if (what == SP_EOM) {
1765 /*
1766 * This loses us relative position.
1767 */
1768 st->fileno = st->blkno = -1;
1769 }
1770 }
1771 return (error);
1772 }
1773
1774 /*
1775 * write N filemarks
1776 */
1777 int
1778 st_write_filemarks(st, number, flags)
1779 struct st_softc *st;
1780 int flags;
1781 int number;
1782 {
1783 int error;
1784 struct scsi_write_filemarks cmd;
1785
1786 /*
1787 * It's hard to write a negative number of file marks.
1788 * Don't try.
1789 */
1790 if (number < 0)
1791 return (EINVAL);
1792 switch (number) {
1793 case 0: /* really a command to sync the drive's buffers */
1794 break;
1795 case 1:
1796 if (st->flags & ST_FM_WRITTEN) /* already have one down */
1797 st->flags &= ~ST_WRITTEN;
1798 else
1799 st->flags |= ST_FM_WRITTEN;
1800 st->flags &= ~ST_PER_ACTION;
1801 break;
1802 default:
1803 st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
1804 }
1805
1806 memset(&cmd, 0, sizeof(cmd));
1807 cmd.opcode = WRITE_FILEMARKS;
1808 if (scsipi_periph_bustype(st->sc_periph) == SCSIPI_BUSTYPE_ATAPI)
1809 cmd.byte2 = SR_IMMED;
1810 /*
1811 * The ATAPI Onstream DI-30 doesn't support writing filemarks, but
1812 * WRITE_FILEMARKS is still used to flush the buffer
1813 */
1814 if ((st->quirks & ST_Q_NOFILEMARKS) == 0)
1815 _lto3b(number, cmd.number);
1816
1817 /* XXX WE NEED TO BE ABLE TO GET A RESIDIUAL XXX */
1818 error = scsipi_command(st->sc_periph,
1819 (struct scsipi_generic *)&cmd, sizeof(cmd),
1820 0, 0, 0, ST_IO_TIME * 4, NULL, flags);
1821 if (error == 0 && st->fileno != -1) {
1822 st->fileno += number;
1823 }
1824 return (error);
1825 }
1826
1827 /*
1828 * Make sure the right number of file marks is on tape if the
1829 * tape has been written. If the position argument is true,
1830 * leave the tape positioned where it was originally.
1831 *
1832 * nmarks returns the number of marks to skip (or, if position
1833 * true, which were skipped) to get back original position.
1834 */
1835 int
1836 st_check_eod(st, position, nmarks, flags)
1837 struct st_softc *st;
1838 boolean position;
1839 int *nmarks;
1840 int flags;
1841 {
1842 int error;
1843
1844 switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
1845 default:
1846 *nmarks = 0;
1847 return (0);
1848 case ST_WRITTEN:
1849 case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
1850 *nmarks = 1;
1851 break;
1852 case ST_WRITTEN | ST_2FM_AT_EOD:
1853 *nmarks = 2;
1854 }
1855 error = st_write_filemarks(st, *nmarks, flags);
1856 if (position && !error)
1857 error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
1858 return (error);
1859 }
1860
1861 /*
1862 * load/unload/retension
1863 */
1864 int
1865 st_load(st, type, flags)
1866 struct st_softc *st;
1867 u_int type;
1868 int flags;
1869 {
1870 int error;
1871 struct scsi_load cmd;
1872
1873 if (type != LD_LOAD) {
1874 int nmarks;
1875
1876 error = st_check_eod(st, FALSE, &nmarks, flags);
1877 if (error) {
1878 printf("%s: failed to write closing filemarks at "
1879 "unload, errno=%d\n", st->sc_dev.dv_xname, error);
1880 return (error);
1881 }
1882 }
1883 if (st->quirks & ST_Q_IGNORE_LOADS) {
1884 if (type == LD_LOAD) {
1885 /*
1886 * If we ignore loads, at least we should try a rewind.
1887 */
1888 return st_rewind(st, 0, flags);
1889 }
1890 /* otherwise, we should do what's asked of us */
1891 }
1892
1893 memset(&cmd, 0, sizeof(cmd));
1894 cmd.opcode = LOAD;
1895 if (scsipi_periph_bustype(st->sc_periph) == SCSIPI_BUSTYPE_ATAPI)
1896 cmd.byte2 = SR_IMMED;
1897 cmd.how = type;
1898
1899 error = scsipi_command(st->sc_periph,
1900 (struct scsipi_generic *)&cmd, sizeof(cmd),
1901 0, 0, ST_RETRIES, ST_SPC_TIME, NULL, flags);
1902 if (error) {
1903 printf("%s: error %d in st_load (op %d)\n",
1904 st->sc_dev.dv_xname, error, type);
1905 }
1906 return (error);
1907 }
1908
1909 /*
1910 * Rewind the device
1911 */
1912 int
1913 st_rewind(st, immediate, flags)
1914 struct st_softc *st;
1915 u_int immediate;
1916 int flags;
1917 {
1918 struct scsi_rewind cmd;
1919 int error;
1920 int nmarks;
1921
1922 error = st_check_eod(st, FALSE, &nmarks, flags);
1923 if (error) {
1924 printf("%s: failed to write closing filemarks at "
1925 "rewind, errno=%d\n", st->sc_dev.dv_xname, error);
1926 return (error);
1927 }
1928 st->flags &= ~ST_PER_ACTION;
1929
1930 /*
1931 * ATAPI tapes always need immediate to be set
1932 */
1933 if (scsipi_periph_bustype(st->sc_periph) == SCSIPI_BUSTYPE_ATAPI)
1934 immediate = SR_IMMED;
1935
1936 memset(&cmd, 0, sizeof(cmd));
1937 cmd.opcode = REWIND;
1938 cmd.byte2 = immediate;
1939
1940 error = scsipi_command(st->sc_periph,
1941 (struct scsipi_generic *)&cmd, sizeof(cmd), 0, 0, ST_RETRIES,
1942 immediate ? ST_CTL_TIME: ST_SPC_TIME, NULL, flags);
1943 if (error) {
1944 printf("%s: error %d trying to rewind\n",
1945 st->sc_dev.dv_xname, error);
1946 /* lost position */
1947 st->fileno = st->blkno = -1;
1948 } else {
1949 st->fileno = st->blkno = 0;
1950 }
1951 return (error);
1952 }
1953
1954 int
1955 st_rdpos(st, hard, blkptr)
1956 struct st_softc *st;
1957 int hard;
1958 u_int32_t *blkptr;
1959 {
1960 int error;
1961 u_int8_t posdata[20];
1962 struct scsi_tape_read_position cmd;
1963
1964 /*
1965 * We try and flush any buffered writes here if we were writing
1966 * and we're trying to get hardware block position. It eats
1967 * up performance substantially, but I'm wary of drive firmware.
1968 *
1969 * I think that *logical* block position is probably okay-
1970 * but hardware block position might have to wait for data
1971 * to hit media to be valid. Caveat Emptor.
1972 */
1973
1974 if (hard && (st->flags & ST_WRITTEN)) {
1975 /*
1976 * First flush any pending writes...
1977 */
1978 error = st_write_filemarks(st, 0, XS_CTL_SILENT);
1979 /*
1980 * The latter case is for 'write protected' tapes
1981 * which are too stupid to recognize a zero count
1982 * for writing filemarks as a no-op.
1983 */
1984 if (error != 0 && error != EACCES && error != EROFS)
1985 return (error);
1986 }
1987
1988 memset(&cmd, 0, sizeof(cmd));
1989 memset(&posdata, 0, sizeof(posdata));
1990 cmd.opcode = READ_POSITION;
1991 if (hard)
1992 cmd.byte1 = 1;
1993
1994 error = scsipi_command(st->sc_periph,
1995 (struct scsipi_generic *)&cmd, sizeof(cmd), (u_char *)&posdata,
1996 sizeof(posdata), ST_RETRIES, ST_CTL_TIME, NULL,
1997 XS_CTL_SILENT | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
1998
1999 if (error == 0) {
2000 #if 0
2001 printf("posdata:");
2002 for (hard = 0; hard < sizeof(posdata); hard++)
2003 printf("%02x ", posdata[hard] & 0xff);
2004 printf("\n");
2005 #endif
2006 if (posdata[0] & 0x4) /* Block Position Unknown */
2007 error = EINVAL;
2008 else
2009 *blkptr = _4btol(&posdata[4]);
2010 }
2011 return (error);
2012 }
2013
2014 int
2015 st_setpos(st, hard, blkptr)
2016 struct st_softc *st;
2017 int hard;
2018 u_int32_t *blkptr;
2019 {
2020 int error;
2021 struct scsi_tape_locate cmd;
2022
2023 /*
2024 * We used to try and flush any buffered writes here.
2025 * Now we push this onto user applications to either
2026 * flush the pending writes themselves (via a zero count
2027 * WRITE FILEMARKS command) or they can trust their tape
2028 * drive to do this correctly for them.
2029 *
2030 * There are very ugly performance limitations otherwise.
2031 */
2032
2033 memset(&cmd, 0, sizeof(cmd));
2034 cmd.opcode = LOCATE;
2035 if (hard)
2036 cmd.byte2 = 1 << 2;
2037 _lto4b(*blkptr, cmd.blkaddr);
2038 error = scsipi_command(st->sc_periph,
2039 (struct scsipi_generic *)&cmd, sizeof(cmd),
2040 NULL, 0, ST_RETRIES, ST_SPC_TIME, NULL, 0);
2041 /*
2042 * Note file && block number position now unknown (if
2043 * these things ever start being maintained in this driver)
2044 */
2045 st->fileno = st->blkno = -1;
2046 return (error);
2047 }
2048
2049
2050 /*
2051 * Look at the returned sense and act on the error and determine
2052 * the unix error number to pass back..., 0 (== report no error),
2053 * -1 = retry the operation, -2 continue error processing.
2054 */
2055 int
2056 st_interpret_sense(xs)
2057 struct scsipi_xfer *xs;
2058 {
2059 struct scsipi_periph *periph = xs->xs_periph;
2060 struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
2061 struct buf *bp = xs->bp;
2062 struct st_softc *st = (void *)periph->periph_dev;
2063 int retval = EJUSTRETURN;
2064 int doprint = ((xs->xs_control & XS_CTL_SILENT) == 0);
2065 u_int8_t key;
2066 int32_t info;
2067
2068 /*
2069 * If it isn't a extended or extended/deferred error, let
2070 * the generic code handle it.
2071 */
2072 if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
2073 (sense->error_code & SSD_ERRCODE) != 0x71) { /* DEFFERRED */
2074 return (retval);
2075 }
2076
2077 if (sense->error_code & SSD_ERRCODE_VALID)
2078 info = _4btol(sense->info);
2079 else
2080 info = (st->flags & ST_FIXEDBLOCKS) ?
2081 xs->datalen / st->blksize : xs->datalen;
2082 key = sense->flags & SSD_KEY;
2083 st->mt_erreg = key;
2084 st->asc = sense->add_sense_code;
2085 st->ascq = sense->add_sense_code_qual;
2086 st->mt_resid = (short) info;
2087
2088 if (key == SKEY_NOT_READY && st->asc == 0x4 && st->ascq == 0x1) {
2089 /* Not Ready, Logical Unit Is in Process Of Becoming Ready */
2090 scsipi_periph_freeze(periph, 1);
2091 callout_reset(&periph->periph_callout,
2092 hz, scsipi_periph_timed_thaw, periph);
2093 return (ERESTART);
2094 }
2095
2096 /*
2097 * If the device is not open yet, let generic handle
2098 */
2099 if ((periph->periph_flags & PERIPH_OPEN) == 0) {
2100 return (retval);
2101 }
2102
2103 xs->resid = info;
2104 if (st->flags & ST_FIXEDBLOCKS) {
2105 if (bp) {
2106 xs->resid *= st->blksize;
2107 st->last_io_resid = xs->resid;
2108 } else {
2109 st->last_ctl_resid = xs->resid;
2110 }
2111 if (key == SKEY_VOLUME_OVERFLOW) {
2112 st->flags |= ST_EIO_PENDING;
2113 if (bp)
2114 bp->b_resid = xs->resid;
2115 } else if (sense->flags & SSD_EOM) {
2116 if ((st->flags & ST_EARLYWARN) == 0)
2117 st->flags |= ST_EIO_PENDING;
2118 st->flags |= ST_EOM_PENDING;
2119 if (bp) {
2120 #if 0
2121 bp->b_resid = xs->resid;
2122 #else
2123 /*
2124 * Grotesque as it seems, the few times
2125 * I've actually seen a non-zero resid,
2126 * the tape drive actually lied and had
2127 * written all the data!
2128 */
2129 bp->b_resid = 0;
2130 #endif
2131 }
2132 }
2133 if (sense->flags & SSD_FILEMARK) {
2134 st->flags |= ST_AT_FILEMARK;
2135 if (bp)
2136 bp->b_resid = xs->resid;
2137 if (st->fileno != (daddr_t) -1) {
2138 st->fileno++;
2139 st->blkno = 0;
2140 st->flags |= ST_POSUPDATED;
2141 }
2142 }
2143 if (sense->flags & SSD_ILI) {
2144 st->flags |= ST_EIO_PENDING;
2145 if (bp)
2146 bp->b_resid = xs->resid;
2147 if (sense->error_code & SSD_ERRCODE_VALID &&
2148 (xs->xs_control & XS_CTL_SILENT) == 0)
2149 printf("%s: block wrong size, %d blocks "
2150 "residual\n", st->sc_dev.dv_xname, info);
2151
2152 /*
2153 * This quirk code helps the drive read
2154 * the first tape block, regardless of
2155 * format. That is required for these
2156 * drives to return proper MODE SENSE
2157 * information.
2158 */
2159 if ((st->quirks & ST_Q_SENSE_HELP) &&
2160 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
2161 st->blksize -= 512;
2162 else if ((st->flags & ST_POSUPDATED) == 0) {
2163 if (st->blkno != (daddr_t) -1) {
2164 st->blkno +=
2165 (xs->datalen / st->blksize);
2166 st->flags |= ST_POSUPDATED;
2167 }
2168 }
2169 }
2170 /*
2171 * If data wanted and no data was transferred, do it immediately
2172 */
2173 if (xs->datalen && xs->resid >= xs->datalen) {
2174 if (st->flags & ST_EIO_PENDING)
2175 return (EIO);
2176 if (st->flags & ST_AT_FILEMARK) {
2177 if (bp)
2178 bp->b_resid = xs->resid;
2179 return (0);
2180 }
2181 }
2182 } else { /* must be variable mode */
2183 if (bp) {
2184 st->last_io_resid = xs->resid;
2185 } else {
2186 st->last_ctl_resid = xs->resid;
2187 }
2188 if (sense->flags & SSD_EOM) {
2189 /*
2190 * The current semantics of this
2191 * driver requires EOM detection
2192 * to return EIO unless early
2193 * warning detection is enabled
2194 * for variable mode (this is always
2195 * on for fixed block mode).
2196 */
2197 if (st->flags & ST_EARLYWARN) {
2198 st->flags |= ST_EOM_PENDING;
2199 retval = 0;
2200 } else {
2201 retval = EIO;
2202 }
2203
2204 /*
2205 * If it's an unadorned EOM detection,
2206 * suppress printing an error.
2207 */
2208 if (key == SKEY_NO_SENSE) {
2209 doprint = 0;
2210 }
2211 } else if (sense->flags & SSD_FILEMARK) {
2212 retval = 0;
2213 if (st->fileno != (daddr_t) -1) {
2214 st->fileno++;
2215 st->blkno = 0;
2216 st->flags |= ST_POSUPDATED;
2217 }
2218 } else if (sense->flags & SSD_ILI) {
2219 if (info < 0) {
2220 /*
2221 * The tape record was bigger than the read
2222 * we issued.
2223 */
2224 if ((xs->xs_control & XS_CTL_SILENT) == 0) {
2225 printf("%s: %d-byte tape record too big"
2226 " for %d-byte user buffer\n",
2227 st->sc_dev.dv_xname,
2228 xs->datalen - info, xs->datalen);
2229 }
2230 retval = EIO;
2231 } else {
2232 retval = 0;
2233 if (st->blkno != (daddr_t) -1) {
2234 st->blkno++;
2235 st->flags |= ST_POSUPDATED;
2236 }
2237 }
2238 }
2239 if (bp)
2240 bp->b_resid = info;
2241 }
2242
2243 #ifndef SCSIPI_DEBUG
2244 if (retval == 0 && key == SKEY_NO_SENSE)
2245 doprint = 0;
2246 #endif
2247 if (key == SKEY_BLANK_CHECK) {
2248 /*
2249 * This quirk code helps the drive read the
2250 * first tape block, regardless of format. That
2251 * is required for these drives to return proper
2252 * MODE SENSE information.
2253 */
2254 if ((st->quirks & ST_Q_SENSE_HELP) &&
2255 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
2256 /* still starting */
2257 st->blksize -= 512;
2258 } else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
2259 st->flags |= ST_BLANK_READ;
2260 xs->resid = xs->datalen;
2261 if (bp) {
2262 bp->b_resid = xs->resid;
2263 /* return an EOF */
2264 }
2265 retval = 0;
2266 /* lost position */
2267 st->fileno = st->blkno = -1;
2268 }
2269 }
2270
2271 /*
2272 * If generic sense processing will continue, we should not
2273 * print sense info here.
2274 */
2275 if (retval == EJUSTRETURN)
2276 doprint = 0;
2277
2278 if (doprint) {
2279 #ifdef SCSIVERBOSE
2280 scsipi_print_sense(xs, 0);
2281 #else
2282 scsipi_printaddr(periph);
2283 printf("Sense Key 0x%02x", key);
2284 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
2285 switch (key) {
2286 case SKEY_NOT_READY:
2287 case SKEY_ILLEGAL_REQUEST:
2288 case SKEY_UNIT_ATTENTION:
2289 case SKEY_WRITE_PROTECT:
2290 break;
2291 case SKEY_VOLUME_OVERFLOW:
2292 case SKEY_BLANK_CHECK:
2293 printf(", requested size: %d (decimal)", info);
2294 break;
2295 case SKEY_ABORTED_COMMAND:
2296 if (xs->xs_retries)
2297 printf(", retrying");
2298 printf(", cmd 0x%x, info 0x%x",
2299 xs->cmd->opcode, info);
2300 break;
2301 default:
2302 printf(", info = %d (decimal)", info);
2303 }
2304 }
2305 if (sense->extra_len != 0) {
2306 int n;
2307 printf(", data =");
2308 for (n = 0; n < sense->extra_len; n++)
2309 printf(" %02x", sense->cmd_spec_info[n]);
2310 }
2311 printf("\n");
2312 #endif
2313 }
2314 return (retval);
2315 }
2316
2317 /*
2318 * The quirk here is that the drive returns some value to st_mode_sense
2319 * incorrectly until the tape has actually passed by the head.
2320 *
2321 * The method is to set the drive to large fixed-block state (user-specified
2322 * density and 1024-byte blocks), then read and rewind to get it to sense the
2323 * tape. If that doesn't work, try 512-byte fixed blocks. If that doesn't
2324 * work, as a last resort, try variable- length blocks. The result will be
2325 * the ability to do an accurate st_mode_sense.
2326 *
2327 * We know we can do a rewind because we just did a load, which implies rewind.
2328 * Rewind seems preferable to space backward if we have a virgin tape.
2329 *
2330 * The rest of the code for this quirk is in ILI processing and BLANK CHECK
2331 * error processing, both part of st_interpret_sense.
2332 */
2333 int
2334 st_touch_tape(st)
2335 struct st_softc *st;
2336 {
2337 char *buf;
2338 int readsize;
2339 int error;
2340
2341 buf = malloc(1024, M_TEMP, M_NOWAIT);
2342 if (buf == NULL)
2343 return (ENOMEM);
2344
2345 if ((error = st->ops(st, ST_OPS_MODESENSE, 0)) != 0)
2346 goto bad;
2347
2348 /*
2349 * If the block size is already known from the
2350 * sense data, use it. Else start probing at 1024.
2351 */
2352 if (st->media_blksize > 0)
2353 st->blksize = st->media_blksize;
2354 else
2355 st->blksize = 1024;
2356
2357 do {
2358 switch (st->blksize) {
2359 case 512:
2360 case 1024:
2361 readsize = st->blksize;
2362 st->flags |= ST_FIXEDBLOCKS;
2363 break;
2364 default:
2365 readsize = 1;
2366 st->flags &= ~ST_FIXEDBLOCKS;
2367 }
2368 if ((error = st->ops(st, ST_OPS_MODESELECT, XS_CTL_SILENT))
2369 != 0) {
2370 /*
2371 * The device did not agree with the proposed
2372 * block size. If we exhausted our options,
2373 * return failure, else try another.
2374 */
2375 if (readsize == 1)
2376 goto bad;
2377 st->blksize -= 512;
2378 continue;
2379 }
2380 st_read(st, buf, readsize, XS_CTL_SILENT); /* XXX */
2381 if ((error = st_rewind(st, 0, 0)) != 0) {
2382 bad: free(buf, M_TEMP);
2383 return (error);
2384 }
2385 } while (readsize != 1 && readsize > st->blksize);
2386
2387 free(buf, M_TEMP);
2388 return (0);
2389 }
2390
2391 int
2392 stdump(dev, blkno, va, size)
2393 dev_t dev;
2394 daddr_t blkno;
2395 caddr_t va;
2396 size_t size;
2397 {
2398
2399 /* Not implemented. */
2400 return (ENXIO);
2401 }
2402