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