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