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