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