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