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