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