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