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