st.c revision 1.155 1 /* $NetBSD: st.c,v 1.155 2002/05/05 15:16:31 bouyer 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.155 2002/05/05 15:16:31 bouyer 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 if (!(st->quirks & ST_Q_NOPREVENT)) {
943 scsipi_prevent(periph, PR_ALLOW,
944 XS_CTL_IGNORE_ILLEGAL_REQUEST |
945 XS_CTL_IGNORE_NOT_READY);
946 }
947 st_load(st, LD_UNLOAD, XS_CTL_IGNORE_NOT_READY);
948 st->blkno = st->fileno = (daddr_t) -1;
949 } else {
950 st->blkno = st->fileno = (daddr_t) 0;
951 }
952 st->flags &= ~(ST_MOUNTED | ST_NEW_MOUNT);
953 periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
954 }
955
956 /*
957 * Given all we know about the device, media, mode, 'quirks' and
958 * initial operation, make a decision as to how we should be set
959 * to run (regarding blocking and EOD marks)
960 */
961 int
962 st_decide_mode(st, first_read)
963 struct st_softc *st;
964 boolean first_read;
965 {
966
967 SC_DEBUG(st->sc_periph, SCSIPI_DB2, ("starting block mode decision\n"));
968
969 /*
970 * If the drive can only handle fixed-length blocks and only at
971 * one size, perhaps we should just do that.
972 */
973 if (st->blkmin && (st->blkmin == st->blkmax)) {
974 st->flags |= ST_FIXEDBLOCKS;
975 st->blksize = st->blkmin;
976 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
977 ("blkmin == blkmax of %d\n", st->blkmin));
978 goto done;
979 }
980 /*
981 * If the tape density mandates (or even suggests) use of fixed
982 * or variable-length blocks, comply.
983 */
984 switch (st->density) {
985 case HALFINCH_800:
986 case HALFINCH_1600:
987 case HALFINCH_6250:
988 case DDS:
989 st->flags &= ~ST_FIXEDBLOCKS;
990 st->blksize = 0;
991 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
992 ("density specified variable\n"));
993 goto done;
994 case QIC_11:
995 case QIC_24:
996 case QIC_120:
997 case QIC_150:
998 case QIC_525:
999 case QIC_1320:
1000 case QIC_3095:
1001 case QIC_3220:
1002 st->flags |= ST_FIXEDBLOCKS;
1003 if (st->media_blksize > 0)
1004 st->blksize = st->media_blksize;
1005 else
1006 st->blksize = DEF_FIXED_BSIZE;
1007 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
1008 ("density specified fixed\n"));
1009 goto done;
1010 }
1011 /*
1012 * If we're about to read the tape, perhaps we should choose
1013 * fixed or variable-length blocks and block size according to
1014 * what the drive found on the tape.
1015 */
1016 if (first_read &&
1017 (!(st->quirks & ST_Q_BLKSIZE) || (st->media_blksize == 0) ||
1018 (st->media_blksize == DEF_FIXED_BSIZE) ||
1019 (st->media_blksize == 1024))) {
1020 if (st->media_blksize > 0)
1021 st->flags |= ST_FIXEDBLOCKS;
1022 else
1023 st->flags &= ~ST_FIXEDBLOCKS;
1024 st->blksize = st->media_blksize;
1025 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
1026 ("Used media_blksize of %d\n", st->media_blksize));
1027 goto done;
1028 }
1029 /*
1030 * We're getting no hints from any direction. Choose variable-
1031 * length blocks arbitrarily.
1032 */
1033 st->flags &= ~ST_FIXEDBLOCKS;
1034 st->blksize = 0;
1035 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
1036 ("Give up and default to variable mode\n"));
1037
1038 done:
1039 /*
1040 * Decide whether or not to write two file marks to signify end-
1041 * of-data. Make the decision as a function of density. If
1042 * the decision is not to use a second file mark, the SCSI BLANK
1043 * CHECK condition code will be recognized as end-of-data when
1044 * first read.
1045 * (I think this should be a by-product of fixed/variable..julian)
1046 */
1047 switch (st->density) {
1048 /* case 8 mm: What is the SCSI density code for 8 mm, anyway? */
1049 case QIC_11:
1050 case QIC_24:
1051 case QIC_120:
1052 case QIC_150:
1053 case QIC_525:
1054 case QIC_1320:
1055 case QIC_3095:
1056 case QIC_3220:
1057 st->flags &= ~ST_2FM_AT_EOD;
1058 break;
1059 default:
1060 st->flags |= ST_2FM_AT_EOD;
1061 }
1062 return (0);
1063 }
1064
1065 /*
1066 * Actually translate the requested transfer into
1067 * one the physical driver can understand
1068 * The transfer is described by a buf and will include
1069 * only one physical transfer.
1070 */
1071 void
1072 ststrategy(bp)
1073 struct buf *bp;
1074 {
1075 struct st_softc *st = st_cd.cd_devs[STUNIT(bp->b_dev)];
1076 int s;
1077
1078 SC_DEBUG(st->sc_periph, SCSIPI_DB1,
1079 ("ststrategy %ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
1080 /*
1081 * If it's a null transfer, return immediatly
1082 */
1083 if (bp->b_bcount == 0)
1084 goto done;
1085
1086 /* If offset is negative, error */
1087 if (bp->b_blkno < 0) {
1088 bp->b_error = EINVAL;
1089 goto bad;
1090 }
1091
1092 /*
1093 * Odd sized request on fixed drives are verboten
1094 */
1095 if (st->flags & ST_FIXEDBLOCKS) {
1096 if (bp->b_bcount % st->blksize) {
1097 printf("%s: bad request, must be multiple of %d\n",
1098 st->sc_dev.dv_xname, st->blksize);
1099 bp->b_error = EIO;
1100 goto bad;
1101 }
1102 }
1103 /*
1104 * as are out-of-range requests on variable drives.
1105 */
1106 else if (bp->b_bcount < st->blkmin ||
1107 (st->blkmax && bp->b_bcount > st->blkmax)) {
1108 printf("%s: bad request, must be between %d and %d\n",
1109 st->sc_dev.dv_xname, st->blkmin, st->blkmax);
1110 bp->b_error = EIO;
1111 goto bad;
1112 }
1113 s = splbio();
1114
1115 /*
1116 * Place it in the queue of activities for this tape
1117 * at the end (a bit silly because we only have on user..
1118 * (but it could fork()))
1119 */
1120 BUFQ_INSERT_TAIL(&st->buf_queue, bp);
1121
1122 /*
1123 * Tell the device to get going on the transfer if it's
1124 * not doing anything, otherwise just wait for completion
1125 * (All a bit silly if we're only allowing 1 open but..)
1126 */
1127 ststart(st->sc_periph);
1128
1129 splx(s);
1130 return;
1131 bad:
1132 bp->b_flags |= B_ERROR;
1133 done:
1134 /*
1135 * Correctly set the buf to indicate a completed xfer
1136 */
1137 bp->b_resid = bp->b_bcount;
1138 biodone(bp);
1139 return;
1140 }
1141
1142 /*
1143 * ststart looks to see if there is a buf waiting for the device
1144 * and that the device is not already busy. If both are true,
1145 * It dequeues the buf and creates a scsi command to perform the
1146 * transfer required. The transfer request will call scsipi_done
1147 * on completion, which will in turn call this routine again
1148 * so that the next queued transfer is performed.
1149 * The bufs are queued by the strategy routine (ststrategy)
1150 *
1151 * This routine is also called after other non-queued requests
1152 * have been made of the scsi driver, to ensure that the queue
1153 * continues to be drained.
1154 * ststart() is called at splbio
1155 */
1156 void
1157 ststart(periph)
1158 struct scsipi_periph *periph;
1159 {
1160 struct st_softc *st = (void *)periph->periph_dev;
1161 struct buf *bp;
1162 struct scsi_rw_tape cmd;
1163 int flags, error;
1164
1165 SC_DEBUG(periph, SCSIPI_DB2, ("ststart "));
1166 /*
1167 * See if there is a buf to do and we are not already
1168 * doing one
1169 */
1170 while (periph->periph_active < periph->periph_openings) {
1171 /* if a special awaits, let it proceed first */
1172 if (periph->periph_flags & PERIPH_WAITING) {
1173 periph->periph_flags &= ~PERIPH_WAITING;
1174 wakeup((caddr_t)periph);
1175 return;
1176 }
1177
1178 if ((bp = BUFQ_FIRST(&st->buf_queue)) == NULL)
1179 return;
1180 BUFQ_REMOVE(&st->buf_queue, bp);
1181
1182 /*
1183 * If the device has been unmounted by the user
1184 * then throw away all requests until done.
1185 */
1186 if ((st->flags & ST_MOUNTED) == 0 ||
1187 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
1188 /* make sure that one implies the other.. */
1189 periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
1190 bp->b_flags |= B_ERROR;
1191 bp->b_error = EIO;
1192 bp->b_resid = bp->b_bcount;
1193 biodone(bp);
1194 continue;
1195 }
1196 /*
1197 * only FIXEDBLOCK devices have pending I/O or space operations.
1198 */
1199 if (st->flags & ST_FIXEDBLOCKS) {
1200 /*
1201 * If we are at a filemark but have not reported it yet
1202 * then we should report it now
1203 */
1204 if (st->flags & ST_AT_FILEMARK) {
1205 if ((bp->b_flags & B_READ) == B_WRITE) {
1206 /*
1207 * Handling of ST_AT_FILEMARK in
1208 * st_space will fill in the right file
1209 * mark count.
1210 * Back up over filemark
1211 */
1212 if (st_space(st, 0, SP_FILEMARKS, 0)) {
1213 bp->b_flags |= B_ERROR;
1214 bp->b_error = EIO;
1215 biodone(bp);
1216 continue;
1217 }
1218 } else {
1219 bp->b_resid = bp->b_bcount;
1220 bp->b_error = 0;
1221 bp->b_flags &= ~B_ERROR;
1222 st->flags &= ~ST_AT_FILEMARK;
1223 biodone(bp);
1224 continue; /* seek more work */
1225 }
1226 }
1227 }
1228 /*
1229 * If we are at EOM but have not reported it
1230 * yet then we should report it now.
1231 */
1232 if (st->flags & (ST_EOM_PENDING|ST_EIO_PENDING)) {
1233 bp->b_resid = bp->b_bcount;
1234 if (st->flags & ST_EIO_PENDING) {
1235 bp->b_error = EIO;
1236 bp->b_flags |= B_ERROR;
1237 }
1238 st->flags &= ~(ST_EOM_PENDING|ST_EIO_PENDING);
1239 biodone(bp);
1240 continue; /* seek more work */
1241 }
1242
1243 /*
1244 * Fill out the scsi command
1245 */
1246 memset(&cmd, 0, sizeof(cmd));
1247 flags = XS_CTL_NOSLEEP | XS_CTL_ASYNC;
1248 if ((bp->b_flags & B_READ) == B_WRITE) {
1249 cmd.opcode = WRITE;
1250 st->flags &= ~ST_FM_WRITTEN;
1251 flags |= XS_CTL_DATA_OUT;
1252 } else {
1253 cmd.opcode = READ;
1254 flags |= XS_CTL_DATA_IN;
1255 }
1256
1257 /*
1258 * Handle "fixed-block-mode" tape drives by using the
1259 * block count instead of the length.
1260 */
1261 if (st->flags & ST_FIXEDBLOCKS) {
1262 cmd.byte2 |= SRW_FIXED;
1263 _lto3b(bp->b_bcount / st->blksize, cmd.len);
1264 } else
1265 _lto3b(bp->b_bcount, cmd.len);
1266
1267 /*
1268 * Clear 'position updated' indicator
1269 */
1270 st->flags &= ~ST_POSUPDATED;
1271
1272 /*
1273 * go ask the adapter to do all this for us
1274 */
1275 error = scsipi_command(periph,
1276 (struct scsipi_generic *)&cmd, sizeof(cmd),
1277 (u_char *)bp->b_data, bp->b_bcount,
1278 0, ST_IO_TIME, bp, flags);
1279 if (error) {
1280 printf("%s: not queued, error %d\n",
1281 st->sc_dev.dv_xname, error);
1282 }
1283 } /* go back and see if we can cram more work in.. */
1284 }
1285
1286 void
1287 stdone(xs)
1288 struct scsipi_xfer *xs;
1289 {
1290 struct st_softc *st = (void *)xs->xs_periph->periph_dev;
1291
1292 if (xs->bp != NULL) {
1293 if ((xs->bp->b_flags & B_READ) == B_WRITE) {
1294 st->flags |= ST_WRITTEN;
1295 } else {
1296 st->flags &= ~ST_WRITTEN;
1297 }
1298 #if NRND > 0
1299 rnd_add_uint32(&st->rnd_source, xs->bp->b_blkno);
1300 #endif
1301
1302 if ((st->flags & ST_POSUPDATED) == 0) {
1303 if (xs->bp->b_flags & B_ERROR) {
1304 st->fileno = st->blkno = -1;
1305 } else if (st->blkno != -1) {
1306 if (st->flags & ST_FIXEDBLOCKS) {
1307 st->blkno +=
1308 (xs->bp->b_bcount / st->blksize);
1309 } else {
1310 st->blkno++;
1311 }
1312 }
1313 }
1314 }
1315 }
1316
1317 int
1318 stread(dev, uio, iomode)
1319 dev_t dev;
1320 struct uio *uio;
1321 int iomode;
1322 {
1323 struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
1324
1325 return (physio(ststrategy, NULL, dev, B_READ,
1326 st->sc_periph->periph_channel->chan_adapter->adapt_minphys, uio));
1327 }
1328
1329 int
1330 stwrite(dev, uio, iomode)
1331 dev_t dev;
1332 struct uio *uio;
1333 int iomode;
1334 {
1335 struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
1336
1337 return (physio(ststrategy, NULL, dev, B_WRITE,
1338 st->sc_periph->periph_channel->chan_adapter->adapt_minphys, uio));
1339 }
1340
1341 /*
1342 * Perform special action on behalf of the user;
1343 * knows about the internals of this device
1344 */
1345 int
1346 stioctl(dev, cmd, arg, flag, p)
1347 dev_t dev;
1348 u_long cmd;
1349 caddr_t arg;
1350 int flag;
1351 struct proc *p;
1352 {
1353 int error = 0;
1354 int unit;
1355 int number, nmarks, dsty;
1356 int flags;
1357 struct st_softc *st;
1358 int hold_blksize;
1359 u_int8_t hold_density;
1360 struct mtop *mt = (struct mtop *) arg;
1361
1362 /*
1363 * Find the device that the user is talking about
1364 */
1365 flags = 0; /* give error messages, act on errors etc. */
1366 unit = STUNIT(dev);
1367 dsty = STDSTY(dev);
1368 st = st_cd.cd_devs[unit];
1369 hold_blksize = st->blksize;
1370 hold_density = st->density;
1371
1372 switch ((u_int) cmd) {
1373
1374 case MTIOCGET: {
1375 struct mtget *g = (struct mtget *) arg;
1376 /*
1377 * (to get the current state of READONLY)
1378 */
1379 error = st->ops(st, ST_OPS_MODESENSE, XS_CTL_SILENT);
1380 if (error) {
1381 /*
1382 * Ignore the error if in control mode;
1383 * this is mandated by st(4).
1384 */
1385 if (STMODE(dev) != CTRL_MODE)
1386 break;
1387 error = 0;
1388 }
1389 SC_DEBUG(st->sc_periph, SCSIPI_DB1, ("[ioctl: get status]\n"));
1390 memset(g, 0, sizeof(struct mtget));
1391 g->mt_type = 0x7; /* Ultrix compat *//*? */
1392 g->mt_blksiz = st->blksize;
1393 g->mt_density = st->density;
1394 g->mt_mblksiz[0] = st->modes[0].blksize;
1395 g->mt_mblksiz[1] = st->modes[1].blksize;
1396 g->mt_mblksiz[2] = st->modes[2].blksize;
1397 g->mt_mblksiz[3] = st->modes[3].blksize;
1398 g->mt_mdensity[0] = st->modes[0].density;
1399 g->mt_mdensity[1] = st->modes[1].density;
1400 g->mt_mdensity[2] = st->modes[2].density;
1401 g->mt_mdensity[3] = st->modes[3].density;
1402 g->mt_fileno = st->fileno;
1403 g->mt_blkno = st->blkno;
1404 if (st->flags & ST_READONLY)
1405 g->mt_dsreg |= MT_DS_RDONLY;
1406 if (st->flags & ST_MOUNTED)
1407 g->mt_dsreg |= MT_DS_MOUNTED;
1408 g->mt_resid = st->mt_resid;
1409 g->mt_erreg = st->mt_erreg;
1410 /*
1411 * clear latched errors.
1412 */
1413 st->mt_resid = 0;
1414 st->mt_erreg = 0;
1415 st->asc = 0;
1416 st->ascq = 0;
1417 break;
1418 }
1419 case MTIOCTOP: {
1420
1421 SC_DEBUG(st->sc_periph, SCSIPI_DB1,
1422 ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op,
1423 mt->mt_count));
1424
1425 /* compat: in U*x it is a short */
1426 number = mt->mt_count;
1427 switch ((short) (mt->mt_op)) {
1428 case MTWEOF: /* write an end-of-file record */
1429 error = st_write_filemarks(st, number, flags);
1430 break;
1431 case MTBSF: /* backward space file */
1432 number = -number;
1433 case MTFSF: /* forward space file */
1434 error = st_check_eod(st, FALSE, &nmarks, flags);
1435 if (!error)
1436 error = st_space(st, number - nmarks,
1437 SP_FILEMARKS, flags);
1438 break;
1439 case MTBSR: /* backward space record */
1440 number = -number;
1441 case MTFSR: /* forward space record */
1442 error = st_check_eod(st, TRUE, &nmarks, flags);
1443 if (!error)
1444 error = st_space(st, number, SP_BLKS, flags);
1445 break;
1446 case MTREW: /* rewind */
1447 error = st_rewind(st, 0, flags);
1448 break;
1449 case MTOFFL: /* rewind and put the drive offline */
1450 st_unmount(st, EJECT);
1451 break;
1452 case MTNOP: /* no operation, sets status only */
1453 break;
1454 case MTRETEN: /* retension the tape */
1455 error = st_load(st, LD_RETENSION, flags);
1456 if (!error)
1457 error = st_load(st, LD_LOAD, flags);
1458 break;
1459 case MTEOM: /* forward space to end of media */
1460 error = st_check_eod(st, FALSE, &nmarks, flags);
1461 if (!error)
1462 error = st_space(st, 1, SP_EOM, flags);
1463 break;
1464 case MTCACHE: /* enable controller cache */
1465 st->flags &= ~ST_DONTBUFFER;
1466 goto try_new_value;
1467 case MTNOCACHE: /* disable controller cache */
1468 st->flags |= ST_DONTBUFFER;
1469 goto try_new_value;
1470 case MTERASE: /* erase volume */
1471 error = st_erase(st, number, flags);
1472 break;
1473 case MTSETBSIZ: /* Set block size for device */
1474 #ifdef NOTYET
1475 if (!(st->flags & ST_NEW_MOUNT)) {
1476 uprintf("re-mount tape before changing blocksize");
1477 error = EINVAL;
1478 break;
1479 }
1480 #endif
1481 if (number == 0)
1482 st->flags &= ~ST_FIXEDBLOCKS;
1483 else {
1484 if ((st->blkmin || st->blkmax) &&
1485 (number < st->blkmin ||
1486 number > st->blkmax)) {
1487 error = EINVAL;
1488 break;
1489 }
1490 st->flags |= ST_FIXEDBLOCKS;
1491 }
1492 st->blksize = number;
1493 st->flags |= ST_BLOCK_SET; /*XXX */
1494 goto try_new_value;
1495
1496 case MTSETDNSTY: /* Set density for device and mode */
1497 /*
1498 * Any number >= 0 and <= 0xff is legal. Numbers
1499 * above 0x80 are 'vendor unique'.
1500 */
1501 if (number < 0 || number > 255) {
1502 error = EINVAL;
1503 break;
1504 } else
1505 st->density = number;
1506 goto try_new_value;
1507
1508 case MTCMPRESS:
1509 error = st->ops(st, (number == 0) ?
1510 ST_OPS_CMPRSS_OFF : ST_OPS_CMPRSS_ON,
1511 XS_CTL_SILENT);
1512 break;
1513
1514 case MTEWARN:
1515 if (number)
1516 st->flags |= ST_EARLYWARN;
1517 else
1518 st->flags &= ~ST_EARLYWARN;
1519 break;
1520
1521 default:
1522 error = EINVAL;
1523 }
1524 break;
1525 }
1526 case MTIOCIEOT:
1527 case MTIOCEEOT:
1528 break;
1529
1530 case MTIOCRDSPOS:
1531 error = st_rdpos(st, 0, (u_int32_t *) arg);
1532 break;
1533
1534 case MTIOCRDHPOS:
1535 error = st_rdpos(st, 1, (u_int32_t *) arg);
1536 break;
1537
1538 case MTIOCSLOCATE:
1539 error = st_setpos(st, 0, (u_int32_t *) arg);
1540 break;
1541
1542 case MTIOCHLOCATE:
1543 error = st_setpos(st, 1, (u_int32_t *) arg);
1544 break;
1545
1546
1547 default:
1548 error = scsipi_do_ioctl(st->sc_periph, dev, cmd, arg,
1549 flag, p);
1550 break;
1551 }
1552 return (error);
1553 /*-----------------------------*/
1554 try_new_value:
1555 /*
1556 * Check that the mode being asked for is aggreeable to the
1557 * drive. If not, put it back the way it was.
1558 *
1559 * If in control mode, we can make (persistent) mode changes
1560 * even if no medium is loaded (see st(4)).
1561 */
1562 if ((STMODE(dev) != CTRL_MODE || (st->flags & ST_MOUNTED) != 0) &&
1563 (error = st->ops(st, ST_OPS_MODESELECT, 0)) != 0) {
1564 /* put it back as it was */
1565 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
1566 st->density = hold_density;
1567 st->blksize = hold_blksize;
1568 if (st->blksize)
1569 st->flags |= ST_FIXEDBLOCKS;
1570 else
1571 st->flags &= ~ST_FIXEDBLOCKS;
1572 return (error);
1573 }
1574 /*
1575 * As the drive liked it, if we are setting a new default,
1576 * set it into the structures as such.
1577 *
1578 * The means for deciding this are not finalised yet- but
1579 * if the device was opened in Control Mode, the values
1580 * are persistent now across mounts.
1581 */
1582 if (STMODE(dev) == CTRL_MODE) {
1583 switch ((short) (mt->mt_op)) {
1584 case MTSETBSIZ:
1585 st->modes[dsty].blksize = st->blksize;
1586 st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
1587 break;
1588 case MTSETDNSTY:
1589 st->modes[dsty].density = st->density;
1590 st->modeflags[dsty] |= DENSITY_SET_BY_USER;
1591 break;
1592 }
1593 }
1594 return (0);
1595 }
1596
1597 /*
1598 * Do a synchronous read.
1599 */
1600 int
1601 st_read(st, buf, size, flags)
1602 struct st_softc *st;
1603 int size;
1604 int flags;
1605 char *buf;
1606 {
1607 struct scsi_rw_tape cmd;
1608
1609 /*
1610 * If it's a null transfer, return immediatly
1611 */
1612 if (size == 0)
1613 return (0);
1614 memset(&cmd, 0, sizeof(cmd));
1615 cmd.opcode = READ;
1616 if (st->flags & ST_FIXEDBLOCKS) {
1617 cmd.byte2 |= SRW_FIXED;
1618 _lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE),
1619 cmd.len);
1620 } else
1621 _lto3b(size, cmd.len);
1622 return (scsipi_command(st->sc_periph,
1623 (struct scsipi_generic *)&cmd, sizeof(cmd),
1624 (u_char *)buf, size, 0, ST_IO_TIME, NULL, flags | XS_CTL_DATA_IN));
1625 }
1626
1627 /*
1628 * issue an erase command
1629 */
1630 int
1631 st_erase(st, full, flags)
1632 struct st_softc *st;
1633 int full, flags;
1634 {
1635 int tmo;
1636 struct scsi_erase cmd;
1637
1638 /*
1639 * Full erase means set LONG bit in erase command, which asks
1640 * the drive to erase the entire unit. Without this bit, we're
1641 * asking the drive to write an erase gap.
1642 */
1643 memset(&cmd, 0, sizeof(cmd));
1644 cmd.opcode = ERASE;
1645 if (full) {
1646 cmd.byte2 = SE_LONG;
1647 tmo = ST_SPC_TIME;
1648 } else {
1649 tmo = ST_IO_TIME;
1650 }
1651
1652 /*
1653 * XXX We always do this asynchronously, for now, unless the device
1654 * has the ST_Q_ERASE_NOIMM quirk. How long should we wait if we
1655 * want to (eventually) to it synchronously?
1656 */
1657 if ((st->quirks & ST_Q_ERASE_NOIMM) == 0)
1658 cmd.byte2 |= SE_IMMED;
1659
1660 return (scsipi_command(st->sc_periph,
1661 (struct scsipi_generic *)&cmd, sizeof(cmd),
1662 0, 0, ST_RETRIES, tmo, NULL, flags));
1663 }
1664
1665 /*
1666 * skip N blocks/filemarks/seq filemarks/eom
1667 */
1668 int
1669 st_space(st, number, what, flags)
1670 struct st_softc *st;
1671 u_int what;
1672 int flags;
1673 int number;
1674 {
1675 struct scsi_space cmd;
1676 int error;
1677
1678 switch (what) {
1679 case SP_BLKS:
1680 if (st->flags & ST_PER_ACTION) {
1681 if (number > 0) {
1682 st->flags &= ~ST_PER_ACTION;
1683 return (EIO);
1684 } else if (number < 0) {
1685 if (st->flags & ST_AT_FILEMARK) {
1686 /*
1687 * Handling of ST_AT_FILEMARK
1688 * in st_space will fill in the
1689 * right file mark count.
1690 */
1691 error = st_space(st, 0, SP_FILEMARKS,
1692 flags);
1693 if (error)
1694 return (error);
1695 }
1696 if (st->flags & ST_BLANK_READ) {
1697 st->flags &= ~ST_BLANK_READ;
1698 return (EIO);
1699 }
1700 st->flags &= ~(ST_EIO_PENDING|ST_EOM_PENDING);
1701 }
1702 }
1703 break;
1704 case SP_FILEMARKS:
1705 if (st->flags & ST_EIO_PENDING) {
1706 if (number > 0) {
1707 /* pretend we just discovered the error */
1708 st->flags &= ~ST_EIO_PENDING;
1709 return (EIO);
1710 } else if (number < 0) {
1711 /* back away from the error */
1712 st->flags &= ~ST_EIO_PENDING;
1713 }
1714 }
1715 if (st->flags & ST_AT_FILEMARK) {
1716 st->flags &= ~ST_AT_FILEMARK;
1717 number--;
1718 }
1719 if ((st->flags & ST_BLANK_READ) && (number < 0)) {
1720 /* back away from unwritten tape */
1721 st->flags &= ~ST_BLANK_READ;
1722 number++; /* XXX dubious */
1723 }
1724 break;
1725 case SP_EOM:
1726 if (st->flags & ST_EOM_PENDING) {
1727 /* we're already there */
1728 st->flags &= ~ST_EOM_PENDING;
1729 return (0);
1730 }
1731 if (st->flags & ST_EIO_PENDING) {
1732 /* pretend we just discovered the error */
1733 st->flags &= ~ST_EIO_PENDING;
1734 return (EIO);
1735 }
1736 if (st->flags & ST_AT_FILEMARK)
1737 st->flags &= ~ST_AT_FILEMARK;
1738 break;
1739 }
1740 if (number == 0)
1741 return (0);
1742
1743 memset(&cmd, 0, sizeof(cmd));
1744 cmd.opcode = SPACE;
1745 cmd.byte2 = what;
1746 _lto3b(number, cmd.number);
1747
1748 st->flags &= ~ST_POSUPDATED;
1749 st->last_ctl_resid = 0;
1750 error = scsipi_command(st->sc_periph,
1751 (struct scsipi_generic *)&cmd, sizeof(cmd),
1752 0, 0, 0, ST_SPC_TIME, NULL, flags);
1753
1754 if (error == 0 && (st->flags & ST_POSUPDATED) == 0) {
1755 number = number - st->last_ctl_resid;
1756 if (what == SP_BLKS) {
1757 if (st->blkno != -1) {
1758 st->blkno += number;
1759 }
1760 } else if (what == SP_FILEMARKS) {
1761 if (st->fileno != -1) {
1762 st->fileno += number;
1763 if (number > 0) {
1764 st->blkno = 0;
1765 } else if (number < 0) {
1766 st->blkno = -1;
1767 }
1768 }
1769 } else if (what == SP_EOM) {
1770 /*
1771 * This loses us relative position.
1772 */
1773 st->fileno = st->blkno = -1;
1774 }
1775 }
1776 return (error);
1777 }
1778
1779 /*
1780 * write N filemarks
1781 */
1782 int
1783 st_write_filemarks(st, number, flags)
1784 struct st_softc *st;
1785 int flags;
1786 int number;
1787 {
1788 int error;
1789 struct scsi_write_filemarks cmd;
1790
1791 /*
1792 * It's hard to write a negative number of file marks.
1793 * Don't try.
1794 */
1795 if (number < 0)
1796 return (EINVAL);
1797 switch (number) {
1798 case 0: /* really a command to sync the drive's buffers */
1799 break;
1800 case 1:
1801 if (st->flags & ST_FM_WRITTEN) /* already have one down */
1802 st->flags &= ~ST_WRITTEN;
1803 else
1804 st->flags |= ST_FM_WRITTEN;
1805 st->flags &= ~ST_PER_ACTION;
1806 break;
1807 default:
1808 st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
1809 }
1810
1811 memset(&cmd, 0, sizeof(cmd));
1812 cmd.opcode = WRITE_FILEMARKS;
1813 if (scsipi_periph_bustype(st->sc_periph) == SCSIPI_BUSTYPE_ATAPI)
1814 cmd.byte2 = SR_IMMED;
1815 /*
1816 * The ATAPI Onstream DI-30 doesn't support writing filemarks, but
1817 * WRITE_FILEMARKS is still used to flush the buffer
1818 */
1819 if ((st->quirks & ST_Q_NOFILEMARKS) == 0)
1820 _lto3b(number, cmd.number);
1821
1822 /* XXX WE NEED TO BE ABLE TO GET A RESIDIUAL XXX */
1823 error = scsipi_command(st->sc_periph,
1824 (struct scsipi_generic *)&cmd, sizeof(cmd),
1825 0, 0, 0, ST_IO_TIME * 4, NULL, flags);
1826 if (error == 0 && st->fileno != -1) {
1827 st->fileno += number;
1828 }
1829 return (error);
1830 }
1831
1832 /*
1833 * Make sure the right number of file marks is on tape if the
1834 * tape has been written. If the position argument is true,
1835 * leave the tape positioned where it was originally.
1836 *
1837 * nmarks returns the number of marks to skip (or, if position
1838 * true, which were skipped) to get back original position.
1839 */
1840 int
1841 st_check_eod(st, position, nmarks, flags)
1842 struct st_softc *st;
1843 boolean position;
1844 int *nmarks;
1845 int flags;
1846 {
1847 int error;
1848
1849 switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
1850 default:
1851 *nmarks = 0;
1852 return (0);
1853 case ST_WRITTEN:
1854 case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
1855 *nmarks = 1;
1856 break;
1857 case ST_WRITTEN | ST_2FM_AT_EOD:
1858 *nmarks = 2;
1859 }
1860 error = st_write_filemarks(st, *nmarks, flags);
1861 if (position && !error)
1862 error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
1863 return (error);
1864 }
1865
1866 /*
1867 * load/unload/retension
1868 */
1869 int
1870 st_load(st, type, flags)
1871 struct st_softc *st;
1872 u_int type;
1873 int flags;
1874 {
1875 int error;
1876 struct scsi_load cmd;
1877
1878 if (type != LD_LOAD) {
1879 int nmarks;
1880
1881 error = st_check_eod(st, FALSE, &nmarks, flags);
1882 if (error) {
1883 printf("%s: failed to write closing filemarks at "
1884 "unload, errno=%d\n", st->sc_dev.dv_xname, error);
1885 return (error);
1886 }
1887 }
1888 if (st->quirks & ST_Q_IGNORE_LOADS) {
1889 if (type == LD_LOAD) {
1890 /*
1891 * If we ignore loads, at least we should try a rewind.
1892 */
1893 return st_rewind(st, 0, flags);
1894 }
1895 /* otherwise, we should do what's asked of us */
1896 }
1897
1898 memset(&cmd, 0, sizeof(cmd));
1899 cmd.opcode = LOAD;
1900 if (scsipi_periph_bustype(st->sc_periph) == SCSIPI_BUSTYPE_ATAPI)
1901 cmd.byte2 = SR_IMMED;
1902 cmd.how = type;
1903
1904 error = scsipi_command(st->sc_periph,
1905 (struct scsipi_generic *)&cmd, sizeof(cmd),
1906 0, 0, ST_RETRIES, ST_SPC_TIME, NULL, flags);
1907 if (error) {
1908 printf("%s: error %d in st_load (op %d)\n",
1909 st->sc_dev.dv_xname, error, type);
1910 }
1911 return (error);
1912 }
1913
1914 /*
1915 * Rewind the device
1916 */
1917 int
1918 st_rewind(st, immediate, flags)
1919 struct st_softc *st;
1920 u_int immediate;
1921 int flags;
1922 {
1923 struct scsi_rewind cmd;
1924 int error;
1925 int nmarks;
1926
1927 error = st_check_eod(st, FALSE, &nmarks, flags);
1928 if (error) {
1929 printf("%s: failed to write closing filemarks at "
1930 "rewind, errno=%d\n", st->sc_dev.dv_xname, error);
1931 return (error);
1932 }
1933 st->flags &= ~ST_PER_ACTION;
1934
1935 /*
1936 * ATAPI tapes always need immediate to be set
1937 */
1938 if (scsipi_periph_bustype(st->sc_periph) == SCSIPI_BUSTYPE_ATAPI)
1939 immediate = SR_IMMED;
1940
1941 memset(&cmd, 0, sizeof(cmd));
1942 cmd.opcode = REWIND;
1943 cmd.byte2 = immediate;
1944
1945 error = scsipi_command(st->sc_periph,
1946 (struct scsipi_generic *)&cmd, sizeof(cmd), 0, 0, ST_RETRIES,
1947 immediate ? ST_CTL_TIME: ST_SPC_TIME, NULL, flags);
1948 if (error) {
1949 printf("%s: error %d trying to rewind\n",
1950 st->sc_dev.dv_xname, error);
1951 /* lost position */
1952 st->fileno = st->blkno = -1;
1953 } else {
1954 st->fileno = st->blkno = 0;
1955 }
1956 return (error);
1957 }
1958
1959 int
1960 st_rdpos(st, hard, blkptr)
1961 struct st_softc *st;
1962 int hard;
1963 u_int32_t *blkptr;
1964 {
1965 int error;
1966 u_int8_t posdata[20];
1967 struct scsi_tape_read_position cmd;
1968
1969 /*
1970 * We try and flush any buffered writes here if we were writing
1971 * and we're trying to get hardware block position. It eats
1972 * up performance substantially, but I'm wary of drive firmware.
1973 *
1974 * I think that *logical* block position is probably okay-
1975 * but hardware block position might have to wait for data
1976 * to hit media to be valid. Caveat Emptor.
1977 */
1978
1979 if (hard && (st->flags & ST_WRITTEN)) {
1980 /*
1981 * First flush any pending writes...
1982 */
1983 error = st_write_filemarks(st, 0, XS_CTL_SILENT);
1984 /*
1985 * The latter case is for 'write protected' tapes
1986 * which are too stupid to recognize a zero count
1987 * for writing filemarks as a no-op.
1988 */
1989 if (error != 0 && error != EACCES && error != EROFS)
1990 return (error);
1991 }
1992
1993 memset(&cmd, 0, sizeof(cmd));
1994 memset(&posdata, 0, sizeof(posdata));
1995 cmd.opcode = READ_POSITION;
1996 if (hard)
1997 cmd.byte1 = 1;
1998
1999 error = scsipi_command(st->sc_periph,
2000 (struct scsipi_generic *)&cmd, sizeof(cmd), (u_char *)&posdata,
2001 sizeof(posdata), ST_RETRIES, ST_CTL_TIME, NULL,
2002 XS_CTL_SILENT | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
2003
2004 if (error == 0) {
2005 #if 0
2006 printf("posdata:");
2007 for (hard = 0; hard < sizeof(posdata); hard++)
2008 printf("%02x ", posdata[hard] & 0xff);
2009 printf("\n");
2010 #endif
2011 if (posdata[0] & 0x4) /* Block Position Unknown */
2012 error = EINVAL;
2013 else
2014 *blkptr = _4btol(&posdata[4]);
2015 }
2016 return (error);
2017 }
2018
2019 int
2020 st_setpos(st, hard, blkptr)
2021 struct st_softc *st;
2022 int hard;
2023 u_int32_t *blkptr;
2024 {
2025 int error;
2026 struct scsi_tape_locate cmd;
2027
2028 /*
2029 * We used to try and flush any buffered writes here.
2030 * Now we push this onto user applications to either
2031 * flush the pending writes themselves (via a zero count
2032 * WRITE FILEMARKS command) or they can trust their tape
2033 * drive to do this correctly for them.
2034 *
2035 * There are very ugly performance limitations otherwise.
2036 */
2037
2038 memset(&cmd, 0, sizeof(cmd));
2039 cmd.opcode = LOCATE;
2040 if (hard)
2041 cmd.byte2 = 1 << 2;
2042 _lto4b(*blkptr, cmd.blkaddr);
2043 error = scsipi_command(st->sc_periph,
2044 (struct scsipi_generic *)&cmd, sizeof(cmd),
2045 NULL, 0, ST_RETRIES, ST_SPC_TIME, NULL, 0);
2046 /*
2047 * Note file && block number position now unknown (if
2048 * these things ever start being maintained in this driver)
2049 */
2050 st->fileno = st->blkno = -1;
2051 return (error);
2052 }
2053
2054
2055 /*
2056 * Look at the returned sense and act on the error and determine
2057 * the unix error number to pass back..., 0 (== report no error),
2058 * -1 = retry the operation, -2 continue error processing.
2059 */
2060 int
2061 st_interpret_sense(xs)
2062 struct scsipi_xfer *xs;
2063 {
2064 struct scsipi_periph *periph = xs->xs_periph;
2065 struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
2066 struct buf *bp = xs->bp;
2067 struct st_softc *st = (void *)periph->periph_dev;
2068 int retval = EJUSTRETURN;
2069 int doprint = ((xs->xs_control & XS_CTL_SILENT) == 0);
2070 u_int8_t key;
2071 int32_t info;
2072
2073 /*
2074 * If it isn't a extended or extended/deferred error, let
2075 * the generic code handle it.
2076 */
2077 if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
2078 (sense->error_code & SSD_ERRCODE) != 0x71) { /* DEFFERRED */
2079 return (retval);
2080 }
2081
2082 if (sense->error_code & SSD_ERRCODE_VALID)
2083 info = _4btol(sense->info);
2084 else
2085 info = (st->flags & ST_FIXEDBLOCKS) ?
2086 xs->datalen / st->blksize : xs->datalen;
2087 key = sense->flags & SSD_KEY;
2088 st->mt_erreg = key;
2089 st->asc = sense->add_sense_code;
2090 st->ascq = sense->add_sense_code_qual;
2091 st->mt_resid = (short) info;
2092
2093 if (key == SKEY_NOT_READY && st->asc == 0x4 && st->ascq == 0x1) {
2094 /* Not Ready, Logical Unit Is in Process Of Becoming Ready */
2095 if (!callout_active(&periph->periph_callout))
2096 scsipi_periph_freeze(periph, 1);
2097 callout_reset(&periph->periph_callout,
2098 hz, scsipi_periph_timed_thaw, periph);
2099 return (ERESTART);
2100 }
2101
2102 /*
2103 * If the device is not open yet, let generic handle
2104 */
2105 if ((periph->periph_flags & PERIPH_OPEN) == 0) {
2106 return (retval);
2107 }
2108
2109 xs->resid = info;
2110 if (st->flags & ST_FIXEDBLOCKS) {
2111 if (bp) {
2112 xs->resid *= st->blksize;
2113 st->last_io_resid = xs->resid;
2114 } else {
2115 st->last_ctl_resid = xs->resid;
2116 }
2117 if (key == SKEY_VOLUME_OVERFLOW) {
2118 st->flags |= ST_EIO_PENDING;
2119 if (bp)
2120 bp->b_resid = xs->resid;
2121 } else if (sense->flags & SSD_EOM) {
2122 if ((st->flags & ST_EARLYWARN) == 0)
2123 st->flags |= ST_EIO_PENDING;
2124 st->flags |= ST_EOM_PENDING;
2125 if (bp) {
2126 #if 0
2127 bp->b_resid = xs->resid;
2128 #else
2129 /*
2130 * Grotesque as it seems, the few times
2131 * I've actually seen a non-zero resid,
2132 * the tape drive actually lied and had
2133 * written all the data!
2134 */
2135 bp->b_resid = 0;
2136 #endif
2137 }
2138 }
2139 if (sense->flags & SSD_FILEMARK) {
2140 st->flags |= ST_AT_FILEMARK;
2141 if (bp)
2142 bp->b_resid = xs->resid;
2143 if (st->fileno != (daddr_t) -1) {
2144 st->fileno++;
2145 st->blkno = 0;
2146 st->flags |= ST_POSUPDATED;
2147 }
2148 }
2149 if (sense->flags & SSD_ILI) {
2150 st->flags |= ST_EIO_PENDING;
2151 if (bp)
2152 bp->b_resid = xs->resid;
2153 if (sense->error_code & SSD_ERRCODE_VALID &&
2154 (xs->xs_control & XS_CTL_SILENT) == 0)
2155 printf("%s: block wrong size, %d blocks "
2156 "residual\n", st->sc_dev.dv_xname, info);
2157
2158 /*
2159 * This quirk code helps the drive read
2160 * the first tape block, regardless of
2161 * format. That is required for these
2162 * drives to return proper MODE SENSE
2163 * information.
2164 */
2165 if ((st->quirks & ST_Q_SENSE_HELP) &&
2166 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
2167 st->blksize -= 512;
2168 else if ((st->flags & ST_POSUPDATED) == 0) {
2169 if (st->blkno != (daddr_t) -1) {
2170 st->blkno +=
2171 (xs->datalen / st->blksize);
2172 st->flags |= ST_POSUPDATED;
2173 }
2174 }
2175 }
2176 /*
2177 * If data wanted and no data was transferred, do it immediately
2178 */
2179 if (xs->datalen && xs->resid >= xs->datalen) {
2180 if (st->flags & ST_EIO_PENDING)
2181 return (EIO);
2182 if (st->flags & ST_AT_FILEMARK) {
2183 if (bp)
2184 bp->b_resid = xs->resid;
2185 return (0);
2186 }
2187 }
2188 } else { /* must be variable mode */
2189 if (bp) {
2190 st->last_io_resid = xs->resid;
2191 } else {
2192 st->last_ctl_resid = xs->resid;
2193 }
2194 if (sense->flags & SSD_EOM) {
2195 /*
2196 * The current semantics of this
2197 * driver requires EOM detection
2198 * to return EIO unless early
2199 * warning detection is enabled
2200 * for variable mode (this is always
2201 * on for fixed block mode).
2202 */
2203 if (st->flags & ST_EARLYWARN) {
2204 st->flags |= ST_EOM_PENDING;
2205 retval = 0;
2206 } else {
2207 retval = EIO;
2208 }
2209
2210 /*
2211 * If it's an unadorned EOM detection,
2212 * suppress printing an error.
2213 */
2214 if (key == SKEY_NO_SENSE) {
2215 doprint = 0;
2216 }
2217 } else if (sense->flags & SSD_FILEMARK) {
2218 retval = 0;
2219 if (st->fileno != (daddr_t) -1) {
2220 st->fileno++;
2221 st->blkno = 0;
2222 st->flags |= ST_POSUPDATED;
2223 }
2224 } else if (sense->flags & SSD_ILI) {
2225 if (info < 0) {
2226 /*
2227 * The tape record was bigger than the read
2228 * we issued.
2229 */
2230 if ((xs->xs_control & XS_CTL_SILENT) == 0) {
2231 printf("%s: %d-byte tape record too big"
2232 " for %d-byte user buffer\n",
2233 st->sc_dev.dv_xname,
2234 xs->datalen - info, xs->datalen);
2235 }
2236 retval = EIO;
2237 } else {
2238 retval = 0;
2239 if (st->blkno != (daddr_t) -1) {
2240 st->blkno++;
2241 st->flags |= ST_POSUPDATED;
2242 }
2243 }
2244 }
2245 if (bp)
2246 bp->b_resid = info;
2247 }
2248
2249 #ifndef SCSIPI_DEBUG
2250 if (retval == 0 && key == SKEY_NO_SENSE)
2251 doprint = 0;
2252 #endif
2253 if (key == SKEY_BLANK_CHECK) {
2254 /*
2255 * This quirk code helps the drive read the
2256 * first tape block, regardless of format. That
2257 * is required for these drives to return proper
2258 * MODE SENSE information.
2259 */
2260 if ((st->quirks & ST_Q_SENSE_HELP) &&
2261 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
2262 /* still starting */
2263 st->blksize -= 512;
2264 } else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
2265 st->flags |= ST_BLANK_READ;
2266 xs->resid = xs->datalen;
2267 if (bp) {
2268 bp->b_resid = xs->resid;
2269 /* return an EOF */
2270 }
2271 retval = 0;
2272 /* lost position */
2273 st->fileno = st->blkno = -1;
2274 }
2275 }
2276
2277 /*
2278 * If generic sense processing will continue, we should not
2279 * print sense info here.
2280 */
2281 if (retval == EJUSTRETURN)
2282 doprint = 0;
2283
2284 if (doprint) {
2285 #ifdef SCSIVERBOSE
2286 scsipi_print_sense(xs, 0);
2287 #else
2288 scsipi_printaddr(periph);
2289 printf("Sense Key 0x%02x", key);
2290 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
2291 switch (key) {
2292 case SKEY_NOT_READY:
2293 case SKEY_ILLEGAL_REQUEST:
2294 case SKEY_UNIT_ATTENTION:
2295 case SKEY_WRITE_PROTECT:
2296 break;
2297 case SKEY_VOLUME_OVERFLOW:
2298 case SKEY_BLANK_CHECK:
2299 printf(", requested size: %d (decimal)", info);
2300 break;
2301 case SKEY_ABORTED_COMMAND:
2302 if (xs->xs_retries)
2303 printf(", retrying");
2304 printf(", cmd 0x%x, info 0x%x",
2305 xs->cmd->opcode, info);
2306 break;
2307 default:
2308 printf(", info = %d (decimal)", info);
2309 }
2310 }
2311 if (sense->extra_len != 0) {
2312 int n;
2313 printf(", data =");
2314 for (n = 0; n < sense->extra_len; n++)
2315 printf(" %02x", sense->cmd_spec_info[n]);
2316 }
2317 printf("\n");
2318 #endif
2319 }
2320 return (retval);
2321 }
2322
2323 /*
2324 * The quirk here is that the drive returns some value to st_mode_sense
2325 * incorrectly until the tape has actually passed by the head.
2326 *
2327 * The method is to set the drive to large fixed-block state (user-specified
2328 * density and 1024-byte blocks), then read and rewind to get it to sense the
2329 * tape. If that doesn't work, try 512-byte fixed blocks. If that doesn't
2330 * work, as a last resort, try variable- length blocks. The result will be
2331 * the ability to do an accurate st_mode_sense.
2332 *
2333 * We know we can do a rewind because we just did a load, which implies rewind.
2334 * Rewind seems preferable to space backward if we have a virgin tape.
2335 *
2336 * The rest of the code for this quirk is in ILI processing and BLANK CHECK
2337 * error processing, both part of st_interpret_sense.
2338 */
2339 int
2340 st_touch_tape(st)
2341 struct st_softc *st;
2342 {
2343 char *buf;
2344 int readsize;
2345 int error;
2346
2347 buf = malloc(1024, M_TEMP, M_NOWAIT);
2348 if (buf == NULL)
2349 return (ENOMEM);
2350
2351 if ((error = st->ops(st, ST_OPS_MODESENSE, 0)) != 0)
2352 goto bad;
2353
2354 /*
2355 * If the block size is already known from the
2356 * sense data, use it. Else start probing at 1024.
2357 */
2358 if (st->media_blksize > 0)
2359 st->blksize = st->media_blksize;
2360 else
2361 st->blksize = 1024;
2362
2363 do {
2364 switch (st->blksize) {
2365 case 512:
2366 case 1024:
2367 readsize = st->blksize;
2368 st->flags |= ST_FIXEDBLOCKS;
2369 break;
2370 default:
2371 readsize = 1;
2372 st->flags &= ~ST_FIXEDBLOCKS;
2373 }
2374 if ((error = st->ops(st, ST_OPS_MODESELECT, XS_CTL_SILENT))
2375 != 0) {
2376 /*
2377 * The device did not agree with the proposed
2378 * block size. If we exhausted our options,
2379 * return failure, else try another.
2380 */
2381 if (readsize == 1)
2382 goto bad;
2383 st->blksize -= 512;
2384 continue;
2385 }
2386 st_read(st, buf, readsize, XS_CTL_SILENT); /* XXX */
2387 if ((error = st_rewind(st, 0, 0)) != 0) {
2388 bad: free(buf, M_TEMP);
2389 return (error);
2390 }
2391 } while (readsize != 1 && readsize > st->blksize);
2392
2393 free(buf, M_TEMP);
2394 return (0);
2395 }
2396
2397 int
2398 stdump(dev, blkno, va, size)
2399 dev_t dev;
2400 daddr_t blkno;
2401 caddr_t va;
2402 size_t size;
2403 {
2404
2405 /* Not implemented. */
2406 return (ENXIO);
2407 }
2408