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