Bug Summary

File:scsi/st.c
Warning:line 732, column 2
Value stored to 'link' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name st.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -ffreestanding -mcmodel=kernel -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -target-feature -sse2 -target-feature -sse -target-feature -3dnow -target-feature -mmx -target-feature +save-args -disable-red-zone -no-implicit-float -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -nostdsysteminc -nobuiltininc -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/sys -I /usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -I /usr/src/sys/arch -I /usr/src/sys/dev/pci/drm/include -I /usr/src/sys/dev/pci/drm/include/uapi -I /usr/src/sys/dev/pci/drm/amd/include/asic_reg -I /usr/src/sys/dev/pci/drm/amd/include -I /usr/src/sys/dev/pci/drm/amd/amdgpu -I /usr/src/sys/dev/pci/drm/amd/display -I /usr/src/sys/dev/pci/drm/amd/display/include -I /usr/src/sys/dev/pci/drm/amd/display/dc -I /usr/src/sys/dev/pci/drm/amd/display/amdgpu_dm -I /usr/src/sys/dev/pci/drm/amd/pm/inc -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu11 -I /usr/src/sys/dev/pci/drm/amd/pm/swsmu/smu12 -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/hwmgr -I /usr/src/sys/dev/pci/drm/amd/pm/powerplay/smumgr -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc -I /usr/src/sys/dev/pci/drm/amd/display/dc/inc/hw -I /usr/src/sys/dev/pci/drm/amd/display/dc/clk_mgr -I /usr/src/sys/dev/pci/drm/amd/display/modules/inc -I /usr/src/sys/dev/pci/drm/amd/display/modules/hdcp -I /usr/src/sys/dev/pci/drm/amd/display/dmub/inc -I /usr/src/sys/dev/pci/drm/i915 -D DDB -D DIAGNOSTIC -D KTRACE -D ACCOUNTING -D KMEMSTATS -D PTRACE -D POOL_DEBUG -D CRYPTO -D SYSVMSG -D SYSVSEM -D SYSVSHM -D UVM_SWAP_ENCRYPT -D FFS -D FFS2 -D FFS_SOFTUPDATES -D UFS_DIRHASH -D QUOTA -D EXT2FS -D MFS -D NFSCLIENT -D NFSSERVER -D CD9660 -D UDF -D MSDOSFS -D FIFO -D FUSE -D SOCKET_SPLICE -D TCP_ECN -D TCP_SIGNATURE -D INET6 -D IPSEC -D PPP_BSDCOMP -D PPP_DEFLATE -D PIPEX -D MROUTING -D MPLS -D BOOT_CONFIG -D USER_PCICONF -D APERTURE -D MTRR -D NTFS -D HIBERNATE -D PCIVERBOSE -D USBVERBOSE -D WSDISPLAY_COMPAT_USL -D WSDISPLAY_COMPAT_RAWKBD -D WSDISPLAY_DEFAULTSCREENS=6 -D X86EMU -D ONEWIREVERBOSE -D MULTIPROCESSOR -D MAXUSERS=80 -D _KERNEL -D CONFIG_DRM_AMD_DC_DCN3_0 -O2 -Wno-pointer-sign -Wno-address-of-packed-member -Wno-constant-conversion -Wno-unused-but-set-variable -Wno-gnu-folding-constant -fdebug-compilation-dir=/usr/src/sys/arch/amd64/compile/GENERIC.MP/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -o /usr/obj/sys/arch/amd64/compile/GENERIC.MP/scan-build/2022-01-12-131800-47421-1 -x c /usr/src/sys/scsi/st.c
1/* $OpenBSD: st.c,v 1.188 2022/01/11 23:10:11 jsg Exp $ */
2/* $NetBSD: st.c,v 1.71 1997/02/21 23:03:49 thorpej Exp $ */
3
4/*
5 * Copyright (c) 1994 Charles Hannum. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Charles Hannum.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * Originally written by Julian Elischer (julian@tfs.com)
35 * for TRW Financial Systems for use under the MACH(2.5) operating system.
36 *
37 * TRW Financial Systems, in accordance with their agreement with Carnegie
38 * Mellon University, makes this software available to CMU to distribute
39 * or use in any manner that they see fit as long as this message is kept with
40 * the software. For this reason TFS also grants any other persons or
41 * organisations permission to use or modify this software.
42 *
43 * TFS supplies this software to be publicly redistributed
44 * on the understanding that TFS is not responsible for the correct
45 * functioning of this software in any circumstances.
46 *
47 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
48 * major changes by Julian Elischer (julian@jules.dialix.oz.au) May 1993
49 */
50
51/*
52 * To do:
53 * work out some better way of guessing what a good timeout is going
54 * to be depending on whether we expect to retension or not.
55 */
56
57#include <sys/param.h>
58#include <sys/systm.h>
59#include <sys/timeout.h>
60#include <sys/fcntl.h>
61#include <sys/errno.h>
62#include <sys/ioctl.h>
63#include <sys/stat.h>
64#include <sys/pool.h>
65#include <sys/buf.h>
66#include <sys/mtio.h>
67#include <sys/device.h>
68#include <sys/conf.h>
69#include <sys/vnode.h>
70
71#include <scsi/scsi_all.h>
72#include <scsi/scsi_debug.h>
73#include <scsi/scsi_tape.h>
74#include <scsi/scsiconf.h>
75
76/* Defines for device specific stuff */
77#define DEF_FIXED_BSIZE512 512
78
79#define STMODE(z)( ((unsigned)((z) & 0xff) | (((z) & 0xffff0000) >>
8)) & 0x03)
( minor(z)((unsigned)((z) & 0xff) | (((z) & 0xffff0000) >>
8))
& 0x03)
80#define STUNIT(z)((((unsigned)((z) & 0xff) | (((z) & 0xffff0000) >>
8)) >> 4) )
((minor(z)((unsigned)((z) & 0xff) | (((z) & 0xffff0000) >>
8))
>> 4) )
81
82#define STMINOR(unit, mode)(((unit) << 4) + (mode)) (((unit) << 4) + (mode))
83#define MAXSTMODES16 16 /* Old max retained so minor's don't change. */
84
85#define ST_IO_TIME(3 * 60 * 1000) (3 * 60 * 1000) /* 3 minutes */
86#define ST_CTL_TIME(30 * 1000) (30 * 1000) /* 30 seconds */
87#define ST_SPC_TIME(4 * 60 * 60 * 1000) (4 * 60 * 60 * 1000) /* 4 hours */
88
89/*
90 * Maximum density code allowed in SCSI spec (SSC2R08f, Section 8.3).
91 */
92#define SCSI_MAX_DENSITY_CODE0xff 0xff
93
94/*
95 * Define various devices that we know mis-behave in some way,
96 * and note how they are bad, so we can correct for them
97 */
98struct mode {
99 int blksize;
100 u_int8_t density;
101};
102
103struct quirkdata {
104 u_int quirks;
105#define ST_Q_SENSE_HELP0x0001 0x0001 /* must do READ for good MODE SENSE */
106#define ST_Q_IGNORE_LOADS0x0002 0x0002
107#define ST_Q_UNIMODAL0x0004 0x0004 /* unimode drive rejects mode select */
108 struct mode mode;
109};
110
111struct st_quirk_inquiry_pattern {
112 struct scsi_inquiry_pattern pattern;
113 struct quirkdata quirkdata;
114};
115
116const struct st_quirk_inquiry_pattern st_quirk_patterns[] = {
117 {{T_SEQUENTIAL0x01, T_REMOV1,
118 " ", " ", " "}, {0,
119 {512, 0}}},
120 {{T_SEQUENTIAL0x01, T_REMOV1,
121 "TANDBERG", " TDC 3800 ", ""}, {0,
122 {512, 0}}},
123 {{T_SEQUENTIAL0x01, T_REMOV1,
124 "ARCHIVE ", "VIPER 2525 25462", ""}, {ST_Q_SENSE_HELP0x0001,
125 {0, 0}}},
126 {{T_SEQUENTIAL0x01, T_REMOV1,
127 "SANKYO ", "CP525 ", ""}, {0,
128 {512, 0}}},
129 {{T_SEQUENTIAL0x01, T_REMOV1,
130 "ANRITSU ", "DMT780 ", ""}, {0,
131 {512, 0}}},
132 {{T_SEQUENTIAL0x01, T_REMOV1,
133 "ARCHIVE ", "VIPER 150 21531", ""}, {ST_Q_SENSE_HELP0x0001,
134 {0, 0}}},
135 {{T_SEQUENTIAL0x01, T_REMOV1,
136 "WANGTEK ", "5099ES SCSI", ""}, {0,
137 {512, 0}}},
138 {{T_SEQUENTIAL0x01, T_REMOV1,
139 "WANGTEK ", "5150ES SCSI", ""}, {0,
140 {512, 0}}},
141 {{T_SEQUENTIAL0x01, T_REMOV1,
142 "HP ", "T4000s ", ""}, {ST_Q_UNIMODAL0x0004,
143 {0, QIC_30950x45}}},
144 {{T_SEQUENTIAL0x01, T_REMOV1,
145 "WANGTEK ", "5150ES SCSI FA15", "01 A"}, {ST_Q_IGNORE_LOADS0x0002,
146 {0, 0}}},
147 {{T_SEQUENTIAL0x01, T_REMOV1,
148 "TEAC ", "MT-2ST/N50 ", ""}, {ST_Q_IGNORE_LOADS0x0002,
149 {0, 0}}},
150};
151
152#define NOEJECT0 0
153#define EJECT1 1
154
155#define NOREWIND0 0
156#define DOREWIND1 1
157
158struct st_softc {
159 struct device sc_dev;
160
161 int flags;
162#define ST_INFO_VALID0x00000001 0x00000001
163#define ST_WRITTEN0x00000004 0x00000004
164#define ST_FIXEDBLOCKS0x00000008 0x00000008
165#define ST_AT_FILEMARK0x00000010 0x00000010
166#define ST_EIO_PENDING0x00000020 0x00000020
167#define ST_EOM_PENDING0x00000040 0x00000040
168#define ST_EOD_DETECTED0x00000080 0x00000080
169#define ST_FM_WRITTEN0x00000100 0x00000100
170#define ST_BLANK_READ0x00000200 0x00000200
171#define ST_2FM_AT_EOD0x00000400 0x00000400
172#define ST_MOUNTED0x00000800 0x00000800
173#define ST_DONTBUFFER0x00001000 0x00001000
174#define ST_DYING0x00004000 0x00004000
175#define ST_BOD_DETECTED0x00008000 0x00008000
176#define ST_MODE_DENSITY0x00010000 0x00010000
177#define ST_MODE_BLKSIZE0x00040000 0x00040000
178
179 u_int quirks; /* quirks for the open mode */
180 int blksize; /* blksize we are using */
181 u_int8_t density; /* present density */
182 short mt_resid; /* last (short) resid */
183 short mt_erreg; /* last error (sense key) seen */
184
185 struct scsi_link *sc_link; /* our link to the adapter etc. */
186
187 int blkmin; /* min blk size */
188 int blkmax; /* max blk size */
189
190 u_int32_t media_blksize; /* 0 if not ST_FIXEDBLOCKS */
191 u_int32_t media_density; /* this is what it said when asked */
192 int media_fileno; /* relative to BOT. -1 means unknown. */
193 int media_blkno; /* relative to BOF. -1 means unknown. */
194 int media_eom; /* relative to BOT. -1 means unknown. */
195
196 struct mode mode;
197 struct bufq sc_bufq;
198 struct scsi_xshandler sc_xsh;
199};
200
201
202int stmatch(struct device *, void *, void *);
203void stattach(struct device *, struct device *, void *);
204int stactivate(struct device *, int);
205int stdetach(struct device *, int);
206void stminphys(struct buf *);
207void ststart(struct scsi_xfer *);
208
209int st_mount_tape(struct st_softc *, int);
210void st_unmount(struct st_softc *, int, int);
211int st_decide_mode(struct st_softc *, int);
212void st_buf_done(struct scsi_xfer *);
213int st_read(struct st_softc *, char *, int, int);
214int st_read_block_limits(struct st_softc *, int);
215int st_mode_sense(struct st_softc *, int);
216int st_mode_select(struct st_softc *, int);
217int st_space(struct st_softc *, int, u_int, int);
218int st_write_filemarks(struct st_softc *, int, int);
219int st_check_eod(struct st_softc *, int, int *, int);
220int st_load(struct st_softc *, u_int, int);
221int st_rewind(struct st_softc *, u_int, int);
222int st_interpret_sense(struct scsi_xfer *);
223int st_touch_tape(struct st_softc *);
224int st_erase(struct st_softc *, int, int);
225
226const struct cfattach st_ca = {
227 sizeof(struct st_softc), stmatch, stattach,
228 stdetach, stactivate
229};
230
231struct cfdriver st_cd = {
232 NULL((void *)0), "st", DV_TAPE
233};
234
235#define ST_PER_ACTION(0x00000010 | 0x00000020 | 0x00000040 | 0x00000200) (ST_AT_FILEMARK0x00000010 | ST_EIO_PENDING0x00000020 | ST_EOM_PENDING0x00000040 | \
236 ST_BLANK_READ0x00000200)
237
238#define stlookup(unit)(struct st_softc *)device_lookup(&st_cd, (unit)) (struct st_softc *)device_lookup(&st_cd, (unit))
239
240const struct scsi_inquiry_pattern st_patterns[] = {
241 {T_SEQUENTIAL0x01, T_REMOV1,
242 "", "", ""},
243};
244
245int
246stmatch(struct device *parent, void *match, void *aux)
247{
248 struct scsi_attach_args *sa = aux;
249 struct scsi_inquiry_data *inq = &sa->sa_sc_link->inqdata;
250 int priority;
251
252 (void)scsi_inqmatch(inq, st_patterns, nitems(st_patterns)(sizeof((st_patterns)) / sizeof((st_patterns)[0])),
253 sizeof(st_patterns[0]), &priority);
254 return priority;
255}
256
257/*
258 * The routine called by the low level scsi routine when it discovers
259 * a device suitable for this driver.
260 */
261void
262stattach(struct device *parent, struct device *self, void *aux)
263{
264 const struct st_quirk_inquiry_pattern *finger;
265 struct st_softc *st = (void *)self;
266 struct scsi_attach_args *sa = aux;
267 struct scsi_link *link = sa->sa_sc_link;
268 int priority;
269
270 SC_DEBUG(link, SDEV_DB2, ("stattach:\n"));
271
272 /*
273 * Store information needed to contact our base driver
274 */
275 st->sc_link = link;
276 link->interpret_sense = st_interpret_sense;
277 link->device_softc = st;
278
279 /* Get any quirks and mode information. */
280 finger = (const struct st_quirk_inquiry_pattern *)scsi_inqmatch(
281 &link->inqdata,
282 st_quirk_patterns,
283 nitems(st_quirk_patterns)(sizeof((st_quirk_patterns)) / sizeof((st_quirk_patterns)[0])
)
,
284 sizeof(st_quirk_patterns[0]), &priority);
285 if (finger != NULL((void *)0)) {
286 st->quirks = finger->quirkdata.quirks;
287 st->mode = finger->quirkdata.mode;
288 CLR(st->flags, ST_MODE_BLKSIZE | ST_MODE_DENSITY)((st->flags) &= ~(0x00040000 | 0x00010000));
289 if (st->mode.blksize != 0)
290 SET(st->flags, ST_MODE_BLKSIZE)((st->flags) |= (0x00040000));
291 if (st->mode.density != 0)
292 SET(st->flags, ST_MODE_DENSITY)((st->flags) |= (0x00010000));
293 }
294 printf("\n");
295
296 scsi_xsh_set(&st->sc_xsh, link, ststart);
297
298 /* Set up the buf queue for this device. */
299 bufq_init(&st->sc_bufq, BUFQ_FIFO0);
300
301 /* Start up with media position unknown. */
302 st->media_fileno = -1;
303 st->media_blkno = -1;
304 st->media_eom = -1;
305
306 /*
307 * Reset the media loaded flag, sometimes the data
308 * acquired at boot time is not quite accurate. This
309 * will be checked again at the first open.
310 */
311 CLR(link->flags, SDEV_MEDIA_LOADED)((link->flags) &= ~(0x0002));
312}
313
314int
315stactivate(struct device *self, int act)
316{
317 struct st_softc *st = (struct st_softc *)self;
318
319 switch (act) {
320 case DVACT_DEACTIVATE1:
321 SET(st->flags, ST_DYING)((st->flags) |= (0x00004000));
322 scsi_xsh_del(&st->sc_xsh);
323 break;
324 }
325
326 return 0;
327}
328
329int
330stdetach(struct device *self, int flags)
331{
332 struct st_softc *st = (struct st_softc *)self;
333 int cmaj, mn;
334
335 bufq_drain(&st->sc_bufq);
336
337 /* Locate the lowest minor number to be detached. */
338 mn = STMINOR(self->dv_unit, 0)(((self->dv_unit) << 4) + (0));
339
340 for (cmaj = 0; cmaj < nchrdev; cmaj++)
341 if (cdevsw[cmaj].d_open == stopen)
342 vdevgone(cmaj, mn, mn + MAXSTMODES16 - 1, VCHR);
343
344 bufq_destroy(&st->sc_bufq);
345
346 return 0;
347}
348
349/*
350 * open the device.
351 */
352int
353stopen(dev_t dev, int flags, int fmt, struct proc *p)
354{
355 struct scsi_link *link;
356 struct st_softc *st;
357 int error = 0;
358
359 st = stlookup(STUNIT(dev))(struct st_softc *)device_lookup(&st_cd, (((((unsigned)((
dev) & 0xff) | (((dev) & 0xffff0000) >> 8)) >>
4) )))
;
360 if (st == NULL((void *)0))
361 return ENXIO6;
362 if (ISSET(st->flags, ST_DYING)((st->flags) & (0x00004000))) {
363 error = ENXIO6;
364 goto done;
365 }
366 link = st->sc_link;
367
368 if (ISSET(flags, FWRITE)((flags) & (0x0002)) && ISSET(link->flags, SDEV_READONLY)((link->flags) & (0x0004))) {
369 error = EACCES13;
370 goto done;
371 }
372
373 SC_DEBUG(link, SDEV_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
374 STUNIT(dev), st_cd.cd_ndevs));
375
376 /*
377 * Tape is an exclusive media. Only one open at a time.
378 */
379 if (ISSET(link->flags, SDEV_OPEN)((link->flags) & (0x0008))) {
380 SC_DEBUG(link, SDEV_DB4, ("already open\n"));
381 error = EBUSY16;
382 goto done;
383 }
384
385 /* Use st_interpret_sense() now. */
386 SET(link->flags, SDEV_OPEN)((link->flags) |= (0x0008));
387
388 /*
389 * Check the unit status. This clears any outstanding errors and
390 * will ensure that media is present.
391 */
392 error = scsi_test_unit_ready(link, TEST_READY_RETRIES5,
393 SCSI_SILENT0x00020 | SCSI_IGNORE_MEDIA_CHANGE0x00080 |
394 SCSI_IGNORE_ILLEGAL_REQUEST0x00100);
395
396 /*
397 * Terminate any existing mount session if there is no media.
398 */
399 if (!ISSET(link->flags, SDEV_MEDIA_LOADED)((link->flags) & (0x0002)))
400 st_unmount(st, NOEJECT0, DOREWIND1);
401
402 if (error != 0) {
403 CLR(link->flags, SDEV_OPEN)((link->flags) &= ~(0x0008));
404 goto done;
405 }
406
407 error = st_mount_tape(st, flags);
408 if (error != 0) {
409 CLR(link->flags, SDEV_OPEN)((link->flags) &= ~(0x0008));
410 goto done;
411 }
412
413 /*
414 * Make sure that a tape opened in write-only mode will have
415 * file marks written on it when closed, even if not written to.
416 * This is for SUN compatibility
417 */
418 if ((flags & O_ACCMODE0x0003) == FWRITE0x0002)
419 SET(st->flags, ST_WRITTEN)((st->flags) |= (0x00000004));
420
421done:
422 SC_DEBUG(link, SDEV_DB2, ("open complete\n"));
423 device_unref(&st->sc_dev);
424 return error;
425}
426
427/*
428 * close the device.. only called if we are the LAST
429 * occurrence of an open device
430 */
431int
432stclose(dev_t dev, int flags, int mode, struct proc *p)
433{
434 struct scsi_link *link;
435 struct st_softc *st;
436 int error = 0;
437
438 st = stlookup(STUNIT(dev))(struct st_softc *)device_lookup(&st_cd, (((((unsigned)((
dev) & 0xff) | (((dev) & 0xffff0000) >> 8)) >>
4) )))
;
439 if (st == NULL((void *)0))
440 return ENXIO6;
441 if (ISSET(st->flags, ST_DYING)((st->flags) & (0x00004000))) {
442 error = ENXIO6;
443 goto done;
444 }
445 link = st->sc_link;
446
447 SC_DEBUG(link, SDEV_DB1, ("closing\n"));
448
449 if (ISSET(st->flags, ST_WRITTEN)((st->flags) & (0x00000004)) && !ISSET(st->flags, ST_FM_WRITTEN)((st->flags) & (0x00000100)))
450 st_write_filemarks(st, 1, 0);
451
452 switch (STMODE(dev)( ((unsigned)((dev) & 0xff) | (((dev) & 0xffff0000) >>
8)) & 0x03)
) {
453 case 0: /* /dev/rstN */
454 st_unmount(st, NOEJECT0, DOREWIND1);
455 break;
456 case 1: /* /dev/nrstN */
457 st_unmount(st, NOEJECT0, NOREWIND0);
458 break;
459 case 2: /* /dev/erstN */
460 st_unmount(st, EJECT1, DOREWIND1);
461 break;
462 case 3: /* /dev/enrstN */
463 st_unmount(st, EJECT1, NOREWIND0);
464 break;
465 }
466 CLR(link->flags, SDEV_OPEN)((link->flags) &= ~(0x0008));
467 scsi_xsh_del(&st->sc_xsh);
468
469done:
470 device_unref(&st->sc_dev);
471 return error;
472}
473
474/*
475 * Start a new mount session if needed.
476 */
477int
478st_mount_tape(struct st_softc *st, int flags)
479{
480 struct scsi_link *link = st->sc_link;
481 int error = 0;
482
483 if (ISSET(st->flags, ST_MOUNTED)((st->flags) & (0x00000800)))
484 return 0;
485
486 SC_DEBUG(link, SDEV_DB1, ("mounting\n"));
487
488 /*
489 * Assume the media is new and give it a chance to
490 * to do a 'load' instruction.
491 */
492 if ((error = st_load(st, LD_LOAD0x01, 0)) != 0)
493 goto done;
494
495 /*
496 * Throw another dummy instruction to catch
497 * 'Unit attention' errors. Some drives appear to give
498 * these after doing a Load instruction.
499 * (notably some DAT drives)
500 */
501 /* XXX */
502 scsi_test_unit_ready(link, TEST_READY_RETRIES5, SCSI_SILENT0x00020);
503
504 /*
505 * Some devices can't tell you much until they have been
506 * asked to look at the media. This quirk does this.
507 */
508 if (ISSET(st->quirks, ST_Q_SENSE_HELP)((st->quirks) & (0x0001)))
509 if ((error = st_touch_tape(st)) != 0)
510 return error;
511 /*
512 * Load the physical device parameters
513 * loads: blkmin, blkmax
514 */
515 if (!ISSET(link->flags, SDEV_ATAPI)((link->flags) & (0x0200)) &&
516 (error = st_read_block_limits(st, 0)) != 0)
517 goto done;
518
519 /*
520 * Load the media dependent parameters
521 * includes: media_blksize,media_density
522 * As we have a tape in, it should be reflected here.
523 * If not you may need the "quirk" above.
524 */
525 if ((error = st_mode_sense(st, 0)) != 0)
526 goto done;
527
528 /*
529 * If we have gained a permanent density from somewhere,
530 * then use it in preference to the one supplied by
531 * default by the driver.
532 */
533 if (ISSET(st->flags, ST_MODE_DENSITY)((st->flags) & (0x00010000)))
534 st->density = st->mode.density;
535 else
536 st->density = st->media_density;
537 /*
538 * If we have gained a permanent blocksize
539 * then use it in preference to the one supplied by
540 * default by the driver.
541 */
542 CLR(st->flags, ST_FIXEDBLOCKS)((st->flags) &= ~(0x00000008));
543 if (ISSET(st->flags, ST_MODE_BLKSIZE)((st->flags) & (0x00040000))) {
544 st->blksize = st->mode.blksize;
545 if (st->blksize)
546 SET(st->flags, ST_FIXEDBLOCKS)((st->flags) |= (0x00000008));
547 } else {
548 if ((error = st_decide_mode(st, 0)) != 0)
549 goto done;
550 }
551 if ((error = st_mode_select(st, 0)) != 0) {
552 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
553 goto done;
554 }
555 scsi_prevent(link, PR_PREVENT0x01,
556 SCSI_IGNORE_ILLEGAL_REQUEST0x00100 | SCSI_IGNORE_NOT_READY0x00040);
557 SET(st->flags, ST_MOUNTED)((st->flags) |= (0x00000800));
558 SET(link->flags, SDEV_MEDIA_LOADED)((link->flags) |= (0x0002)); /* move earlier? */
559 st->media_fileno = 0;
560 st->media_blkno = 0;
561 st->media_eom = -1;
562
563done:
564 return error;
565}
566
567/*
568 * End the present mount session.
569 * Rewind, and optionally eject the tape.
570 * Reset various flags to indicate that all new
571 * operations require another mount operation
572 */
573void
574st_unmount(struct st_softc *st, int eject, int rewind)
575{
576 struct scsi_link *link = st->sc_link;
577 int nmarks;
578
579 if (eject == NOEJECT0 && rewind == NOREWIND0) {
580 if (ISSET(link->flags, SDEV_MEDIA_LOADED)((link->flags) & (0x0002)))
581 return;
582 }
583
584 st->media_fileno = -1;
585 st->media_blkno = -1;
586
587 if (!ISSET(st->flags, ST_MOUNTED)((st->flags) & (0x00000800)))
588 return;
589 SC_DEBUG(link, SDEV_DB1, ("unmounting\n"));
590 st_check_eod(st, 0, &nmarks, SCSI_IGNORE_NOT_READY0x00040);
591 if (rewind == DOREWIND1)
592 st_rewind(st, 0, SCSI_IGNORE_NOT_READY0x00040);
593 scsi_prevent(link, PR_ALLOW0x00,
594 SCSI_IGNORE_ILLEGAL_REQUEST0x00100 | SCSI_IGNORE_NOT_READY0x00040);
595 if (eject == EJECT1)
596 st_load(st, LD_UNLOAD0x00, SCSI_IGNORE_NOT_READY0x00040);
597 CLR(st->flags, ST_MOUNTED)((st->flags) &= ~(0x00000800));
598 CLR(link->flags, SDEV_MEDIA_LOADED)((link->flags) &= ~(0x0002));
599}
600
601/*
602 * Given all we know about the device, media, mode, 'quirks' and
603 * initial operation, make a decision as to how we should be set
604 * to run (regarding blocking and EOD marks)
605 */
606int
607st_decide_mode(struct st_softc *st, int first_read)
608{
609 struct scsi_link *link = st->sc_link;
610
611 SC_DEBUG(link, SDEV_DB2, ("starting block mode decision\n"));
612
613 /* ATAPI tapes are always fixed blocksize. */
614 if (ISSET(link->flags, SDEV_ATAPI)((link->flags) & (0x0200))) {
615 SET(st->flags, ST_FIXEDBLOCKS)((st->flags) |= (0x00000008));
616 if (st->media_blksize > 0)
617 st->blksize = st->media_blksize;
618 else
619 st->blksize = DEF_FIXED_BSIZE512;
620 goto done;
621 }
622
623 /*
624 * If the drive can only handle fixed-length blocks and only at
625 * one size, perhaps we should just do that.
626 */
627 if (st->blkmin && (st->blkmin == st->blkmax)) {
628 SET(st->flags, ST_FIXEDBLOCKS)((st->flags) |= (0x00000008));
629 st->blksize = st->blkmin;
630 SC_DEBUG(link, SDEV_DB3,
631 ("blkmin == blkmax of %d\n", st->blkmin));
632 goto done;
633 }
634 /*
635 * If the tape density mandates (or even suggests) use of fixed
636 * or variable-length blocks, comply.
637 */
638 switch (st->density) {
639 case HALFINCH_8000x01:
640 case HALFINCH_16000x02:
641 case HALFINCH_62500x03:
642 case DDS0x13:
643 CLR(st->flags, ST_FIXEDBLOCKS)((st->flags) &= ~(0x00000008));
644 st->blksize = 0;
645 SC_DEBUG(link, SDEV_DB3, ("density specified variable\n"));
646 goto done;
647 case QIC_110x04:
648 case QIC_240x05:
649 case QIC_1200x0f:
650 case QIC_1500x10:
651 case QIC_5250x11:
652 case QIC_13200x12:
653 SET(st->flags, ST_FIXEDBLOCKS)((st->flags) |= (0x00000008));
654 if (st->media_blksize > 0)
655 st->blksize = st->media_blksize;
656 else
657 st->blksize = DEF_FIXED_BSIZE512;
658 SC_DEBUG(link, SDEV_DB3, ("density specified fixed\n"));
659 goto done;
660 }
661 /*
662 * If we're about to read the tape, perhaps we should choose
663 * fixed or variable-length blocks and block size according to
664 * what the drive found on the tape.
665 */
666 if (first_read) {
667 if (st->media_blksize > 0)
668 SET(st->flags, ST_FIXEDBLOCKS)((st->flags) |= (0x00000008));
669 else
670 CLR(st->flags, ST_FIXEDBLOCKS)((st->flags) &= ~(0x00000008));
671 st->blksize = st->media_blksize;
672 SC_DEBUG(link, SDEV_DB3,
673 ("Used media_blksize of %d\n", st->media_blksize));
674 goto done;
675 }
676 /*
677 * We're getting no hints from any direction. Choose variable-
678 * length blocks arbitrarily.
679 */
680 CLR(st->flags, ST_FIXEDBLOCKS)((st->flags) &= ~(0x00000008));
681 st->blksize = 0;
682 SC_DEBUG(link, SDEV_DB3,
683 ("Give up and default to variable mode\n"));
684
685done:
686 /*
687 * Decide whether or not to write two file marks to signify end-
688 * of-data. Make the decision as a function of density. If
689 * the decision is not to use a second file mark, the SCSI BLANK
690 * CHECK condition code will be recognized as end-of-data when
691 * first read.
692 * (I think this should be a by-product of fixed/variable..julian)
693 */
694 switch (st->density) {
695/* case 8 mm: What is the SCSI density code for 8 mm, anyway? */
696 case QIC_110x04:
697 case QIC_240x05:
698 case QIC_1200x0f:
699 case QIC_1500x10:
700 case QIC_5250x11:
701 case QIC_13200x12:
702 CLR(st->flags, ST_2FM_AT_EOD)((st->flags) &= ~(0x00000400));
703 break;
704 default:
705 SET(st->flags, ST_2FM_AT_EOD)((st->flags) |= (0x00000400));
706 }
707 return 0;
708}
709
710/*
711 * Actually translate the requested transfer into
712 * one the physical driver can understand
713 * The transfer is described by a buf and will include
714 * only one physical transfer.
715 */
716void
717ststrategy(struct buf *bp)
718{
719 struct scsi_link *link;
720 struct st_softc *st;
721 int s;
722
723 st = stlookup(STUNIT(bp->b_dev))(struct st_softc *)device_lookup(&st_cd, (((((unsigned)((
bp->b_dev) & 0xff) | (((bp->b_dev) & 0xffff0000
) >> 8)) >> 4) )))
;
724 if (st == NULL((void *)0)) {
725 bp->b_error = ENXIO6;
726 goto bad;
727 }
728 if (ISSET(st->flags, ST_DYING)((st->flags) & (0x00004000))) {
729 bp->b_error = ENXIO6;
730 goto bad;
731 }
732 link = st->sc_link;
Value stored to 'link' is never read
733
734 SC_DEBUG(link, SDEV_DB2, ("ststrategy: %ld bytes @ blk %lld\n",
735 bp->b_bcount, (long long)bp->b_blkno));
736
737 /*
738 * If it's a null transfer, return immediately.
739 */
740 if (bp->b_bcount == 0)
741 goto done;
742 /*
743 * Odd sized request on fixed drives are verboten
744 */
745 if (ISSET(st->flags, ST_FIXEDBLOCKS)((st->flags) & (0x00000008))) {
746 if (bp->b_bcount % st->blksize) {
747 printf("%s: bad request, must be multiple of %d\n",
748 st->sc_dev.dv_xname, st->blksize);
749 bp->b_error = EIO5;
750 goto bad;
751 }
752 }
753 /*
754 * as are out-of-range requests on variable drives.
755 */
756 else if (bp->b_bcount < st->blkmin ||
757 (st->blkmax && bp->b_bcount > st->blkmax)) {
758 printf("%s: bad request, must be between %d and %d\n",
759 st->sc_dev.dv_xname, st->blkmin, st->blkmax);
760 bp->b_error = EIO5;
761 goto bad;
762 }
763
764 /*
765 * Place it in the queue of activities for this tape
766 * at the end (a bit silly because we only have on user..
767 * (but it could fork()))
768 */
769 bufq_queue(&st->sc_bufq, bp);
770
771 /*
772 * Tell the device to get going on the transfer if it's
773 * not doing anything, otherwise just wait for completion
774 * (All a bit silly if we're only allowing 1 open but..)
775 */
776 scsi_xsh_add(&st->sc_xsh);
777
778 device_unref(&st->sc_dev);
779 return;
780bad:
781 SET(bp->b_flags, B_ERROR)((bp->b_flags) |= (0x00000400));
782done:
783 /* Set b_resid to indicate no xfer was done. */
784 bp->b_resid = bp->b_bcount;
785 s = splbio()splraise(0x6);
786 biodone(bp);
787 splx(s)spllower(s);
788 if (st)
789 device_unref(&st->sc_dev);
790}
791
792void
793ststart(struct scsi_xfer *xs)
794{
795 struct scsi_link *link = xs->sc_link;
796 struct st_softc *st = link->device_softc;
797 struct buf *bp;
798 struct scsi_rw_tape *cmd;
799 int s;
800
801 SC_DEBUG(link, SDEV_DB2, ("ststart\n"));
802
803 if (ISSET(st->flags, ST_DYING)((st->flags) & (0x00004000))) {
804 scsi_xs_put(xs);
805 return;
806 }
807
808 /*
809 * if the device has been unmounted by the user
810 * then throw away all requests until done
811 */
812 if (!ISSET(st->flags, ST_MOUNTED)((st->flags) & (0x00000800)) ||
813 !ISSET(link->flags, SDEV_MEDIA_LOADED)((link->flags) & (0x0002))) {
814 /* make sure that one implies the other.. */
815 CLR(link->flags, SDEV_MEDIA_LOADED)((link->flags) &= ~(0x0002));
816 bufq_drain(&st->sc_bufq);
817 scsi_xs_put(xs);
818 return;
819 }
820
821 for (;;) {
822 bp = bufq_dequeue(&st->sc_bufq);
823 if (bp == NULL((void *)0)) {
824 scsi_xs_put(xs);
825 return;
826 }
827
828 /*
829 * Only FIXEDBLOCK devices have pending I/O or space
830 * operations.
831 */
832 if (ISSET(st->flags, ST_FIXEDBLOCKS)((st->flags) & (0x00000008))) {
833 /*
834 * If we are at a filemark but have not reported it yet
835 * then we should report it now
836 */
837 if (ISSET(st->flags, ST_AT_FILEMARK)((st->flags) & (0x00000010))) {
838 if ((bp->b_flags & B_READ0x00008000) == B_WRITE0x00000000) {
839 /*
840 * Handling of ST_AT_FILEMARK in
841 * st_space will fill in the right file
842 * mark count.
843 * Back up over filemark
844 */
845 if (st_space(st, 0, SP_FILEMARKS0x01, 0)) {
846 SET(bp->b_flags, B_ERROR)((bp->b_flags) |= (0x00000400));
847 bp->b_resid = bp->b_bcount;
848 bp->b_error = EIO5;
849 s = splbio()splraise(0x6);
850 biodone(bp);
851 splx(s)spllower(s);
852 continue;
853 }
854 } else {
855 bp->b_resid = bp->b_bcount;
856 bp->b_error = 0;
857 CLR(bp->b_flags, B_ERROR)((bp->b_flags) &= ~(0x00000400));
858 CLR(st->flags, ST_AT_FILEMARK)((st->flags) &= ~(0x00000010));
859 s = splbio()splraise(0x6);
860 biodone(bp);
861 splx(s)spllower(s);
862 continue; /* seek more work */
863 }
864 }
865 }
866
867 /*
868 * If we are at EIO or EOM but have not reported it
869 * yet then we should report it now.
870 */
871 if (ISSET(st->flags, ST_EOM_PENDING | ST_EIO_PENDING)((st->flags) & (0x00000040 | 0x00000020))) {
872 bp->b_resid = bp->b_bcount;
873 if (ISSET(st->flags, ST_EIO_PENDING)((st->flags) & (0x00000020))) {
874 bp->b_error = EIO5;
875 SET(bp->b_flags, B_ERROR)((bp->b_flags) |= (0x00000400));
876 }
877 CLR(st->flags, ST_EOM_PENDING | ST_EIO_PENDING)((st->flags) &= ~(0x00000040 | 0x00000020));
878 s = splbio()splraise(0x6);
879 biodone(bp);
880 splx(s)spllower(s);
881 continue; /* seek more work */
882 }
883 break;
884 }
885
886 /*
887 * Fill out the scsi command
888 */
889 cmd = (struct scsi_rw_tape *)&xs->cmd;
890 bzero(cmd, sizeof(*cmd))__builtin_bzero((cmd), (sizeof(*cmd)));
891 if ((bp->b_flags & B_READ0x00008000) == B_WRITE0x00000000) {
892 cmd->opcode = WRITE0x0a;
893 CLR(st->flags, ST_FM_WRITTEN)((st->flags) &= ~(0x00000100));
894 SET(st->flags, ST_WRITTEN)((st->flags) |= (0x00000004));
895 SET(xs->flags, SCSI_DATA_OUT)((xs->flags) |= (0x01000));
896 } else {
897 cmd->opcode = READ0x08;
898 SET(xs->flags, SCSI_DATA_IN)((xs->flags) |= (0x00800));
899 }
900
901 /*
902 * Handle "fixed-block-mode" tape drives by using the
903 * block count instead of the length.
904 */
905 if (ISSET(st->flags, ST_FIXEDBLOCKS)((st->flags) & (0x00000008))) {
906 SET(cmd->byte2, SRW_FIXED)((cmd->byte2) |= (0x01));
907 _lto3b(bp->b_bcount / st->blksize, cmd->len);
908 } else
909 _lto3b(bp->b_bcount, cmd->len);
910
911 if (st->media_blkno != -1) {
912 /* Update block count now, errors will set it to -1. */
913 if (ISSET(st->flags, ST_FIXEDBLOCKS)((st->flags) & (0x00000008)))
914 st->media_blkno += _3btol(cmd->len);
915 else if (_3btol(cmd->len) != 0)
916 st->media_blkno++;
917 }
918
919 xs->cmdlen = sizeof(*cmd);
920 xs->timeout = ST_IO_TIME(3 * 60 * 1000);
921 xs->data = bp->b_data;
922 xs->datalen = bp->b_bcount;
923 xs->done = st_buf_done;
924 xs->cookie = bp;
925 xs->bp = bp;
926
927 /*
928 * go ask the adapter to do all this for us
929 */
930 scsi_xs_exec(xs);
931
932 /*
933 * should we try do more work now?
934 */
935 if (bufq_peek(&st->sc_bufq))
936 scsi_xsh_add(&st->sc_xsh);
937}
938
939void
940st_buf_done(struct scsi_xfer *xs)
941{
942 struct buf *bp = xs->cookie;
943 int error, s;
944
945 switch (xs->error) {
946 case XS_NOERROR0:
947 bp->b_error = 0;
948 CLR(bp->b_flags, B_ERROR)((bp->b_flags) &= ~(0x00000400));
949 bp->b_resid = xs->resid;
950 break;
951
952 case XS_SENSE1:
953 case XS_SHORTSENSE6:
954 SC_DEBUG_SENSE(xs);
955 error = st_interpret_sense(xs);
956 if (error == 0) {
957 bp->b_error = 0;
958 CLR(bp->b_flags, B_ERROR)((bp->b_flags) &= ~(0x00000400));
959 bp->b_resid = xs->resid;
960 break;
961 }
962 if (error != ERESTART-1)
963 xs->retries = 0;
964 goto retry;
965
966 case XS_BUSY5:
967 if (xs->retries) {
968 if (scsi_delay(xs, 1) != ERESTART-1)
969 xs->retries = 0;
970 }
971 goto retry;
972
973 case XS_TIMEOUT4:
974 retry:
975 if (xs->retries--) {
976 scsi_xs_exec(xs);
977 return;
978 }
979 /* FALLTHROUGH */
980
981 default:
982 bp->b_error = EIO5;
983 SET(bp->b_flags, B_ERROR)((bp->b_flags) |= (0x00000400));
984 bp->b_resid = bp->b_bcount;
985 break;
986 }
987
988 s = splbio()splraise(0x6);
989 biodone(bp);
990 splx(s)spllower(s);
991 scsi_xs_put(xs);
992}
993
994void
995stminphys(struct buf *bp)
996{
997 struct scsi_link *link;
998 struct st_softc *sc;
999
1000 sc = stlookup(STUNIT(bp->b_dev))(struct st_softc *)device_lookup(&st_cd, (((((unsigned)((
bp->b_dev) & 0xff) | (((bp->b_dev) & 0xffff0000
) >> 8)) >> 4) )))
;
1001 if (sc == NULL((void *)0))
1002 return;
1003 link = sc->sc_link;
1004
1005 if (link->bus->sb_adapter->dev_minphys != NULL((void *)0))
1006 (*link->bus->sb_adapter->dev_minphys)(bp, link);
1007 else
1008 minphys(bp);
1009
1010 device_unref(&sc->sc_dev);
1011}
1012
1013int
1014stread(dev_t dev, struct uio *uio, int iomode)
1015{
1016 return physio(ststrategy, dev, B_READ0x00008000, stminphys, uio);
1017}
1018
1019int
1020stwrite(dev_t dev, struct uio *uio, int iomode)
1021{
1022 return physio(ststrategy, dev, B_WRITE0x00000000, stminphys, uio);
1023}
1024
1025/*
1026 * Perform special action on behalf of the user;
1027 * knows about the internals of this device
1028 */
1029int
1030stioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, struct proc *p)
1031{
1032 int error = 0;
1033 int nmarks;
1034 int flags = 0;
1035 struct st_softc *st;
1036 int hold_blksize;
1037 u_int8_t hold_density;
1038 struct mtop *mt = (struct mtop *) arg;
1039 int number;
1040
1041 /*
1042 * Find the device that the user is talking about
1043 */
1044 st = stlookup(STUNIT(dev))(struct st_softc *)device_lookup(&st_cd, (((((unsigned)((
dev) & 0xff) | (((dev) & 0xffff0000) >> 8)) >>
4) )))
;
1045 if (st == NULL((void *)0))
1046 return ENXIO6;
1047
1048 if (ISSET(st->flags, ST_DYING)((st->flags) & (0x00004000))) {
1049 error = ENXIO6;
1050 goto done;
1051 }
1052
1053 hold_blksize = st->blksize;
1054 hold_density = st->density;
1055
1056 switch (cmd) {
1057
1058 case MTIOCGET((unsigned long)0x40000000 | ((sizeof(struct mtget) & 0x1fff
) << 16) | ((('m')) << 8) | ((2)))
: {
1059 struct mtget *g = (struct mtget *) arg;
1060
1061 /*
1062 * (to get the current state of READONLY)
1063 */
1064 error = st_mode_sense(st, SCSI_SILENT0x00020);
1065 if (error != 0)
1066 break;
1067
1068 SC_DEBUG(st->sc_link, SDEV_DB1, ("[ioctl: get status]\n"));
1069 bzero(g, sizeof(struct mtget))__builtin_bzero((g), (sizeof(struct mtget)));
1070 g->mt_type = 0x7; /* Ultrix compat *//*? */
1071 g->mt_blksiz = st->blksize;
1072 g->mt_density = st->density;
1073 g->mt_mblksiz = st->mode.blksize;
1074 g->mt_mdensity = st->mode.density;
1075 if (ISSET(st->sc_link->flags, SDEV_READONLY)((st->sc_link->flags) & (0x0004)))
1076 SET(g->mt_dsreg, MT_DS_RDONLY)((g->mt_dsreg) |= (0x10));
1077 if (ISSET(st->flags, ST_MOUNTED)((st->flags) & (0x00000800)))
1078 SET(g->mt_dsreg, MT_DS_MOUNTED)((g->mt_dsreg) |= (0x03));
1079 g->mt_resid = st->mt_resid;
1080 g->mt_erreg = st->mt_erreg;
1081 g->mt_fileno = st->media_fileno;
1082 g->mt_blkno = st->media_blkno;
1083 /*
1084 * clear latched errors.
1085 */
1086 st->mt_resid = 0;
1087 st->mt_erreg = 0;
1088 break;
1089 }
1090 case MTIOCTOP((unsigned long)0x80000000 | ((sizeof(struct mtop) & 0x1fff
) << 16) | ((('m')) << 8) | ((1)))
: {
1091
1092 SC_DEBUG(st->sc_link, SDEV_DB1,
1093 ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op, mt->mt_count));
1094
1095 number = mt->mt_count;
1096 switch (mt->mt_op) {
1097 case MTWEOF0: /* write an end-of-file record */
1098 error = st_write_filemarks(st, number, flags);
1099 break;
1100 case MTBSF2: /* backward space file */
1101 number = -number;
1102 case MTFSF1: /* forward space file */
1103 error = st_check_eod(st, 0, &nmarks, flags);
1104 if (error == 0)
1105 error = st_space(st, number - nmarks,
1106 SP_FILEMARKS0x01, flags);
1107 break;
1108 case MTBSR4: /* backward space record */
1109 number = -number;
1110 case MTFSR3: /* forward space record */
1111 error = st_check_eod(st, 1, &nmarks, flags);
1112 if (error == 0)
1113 error = st_space(st, number, SP_BLKS0x00, flags);
1114 break;
1115 case MTREW5: /* rewind */
1116 error = st_rewind(st, 0, flags);
1117 break;
1118 case MTOFFL6: /* rewind and put the drive offline */
1119 st_unmount(st, EJECT1, DOREWIND1);
1120 break;
1121 case MTNOP7: /* no operation, sets status only */
1122 break;
1123 case MTRETEN8: /* retension the tape */
1124 error = st_load(st, LD_RETENSION0x02, flags);
1125 if (error == 0)
1126 error = st_load(st, LD_LOAD0x01, flags);
1127 break;
1128 case MTEOM10: /* forward space to end of media */
1129 error = st_check_eod(st, 0, &nmarks, flags);
1130 if (error == 0)
1131 error = st_space(st, 1, SP_EOM0x03, flags);
1132 break;
1133 case MTCACHE12: /* enable controller cache */
1134 CLR(st->flags, ST_DONTBUFFER)((st->flags) &= ~(0x00001000));
1135 goto try_new_value;
1136 case MTNOCACHE13: /* disable controller cache */
1137 SET(st->flags, ST_DONTBUFFER)((st->flags) |= (0x00001000));
1138 goto try_new_value;
1139 case MTERASE9: /* erase volume */
1140 error = st_erase(st, number, flags);
1141 break;
1142 case MTSETBSIZ14: /* Set block size for device and mode. */
1143 if (number == 0) {
1144 CLR(st->flags, ST_FIXEDBLOCKS)((st->flags) &= ~(0x00000008));
1145 } else {
1146 if ((st->blkmin || st->blkmax) &&
1147 (number < st->blkmin ||
1148 number > st->blkmax)) {
1149 error = EINVAL22;
1150 break;
1151 }
1152 SET(st->flags, ST_FIXEDBLOCKS)((st->flags) |= (0x00000008));
1153 }
1154 st->blksize = number;
1155 goto try_new_value;
1156
1157 case MTSETDNSTY15: /* Set density for device and mode. */
1158 if (number < 0 || number > SCSI_MAX_DENSITY_CODE0xff) {
1159 error = EINVAL22;
1160 break;
1161 }
1162 st->density = number;
1163 goto try_new_value;
1164
1165 default:
1166 error = EINVAL22;
1167 }
1168 break;
1169 }
1170 case MTIOCIEOT((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('m')) << 8) | ((3)))
:
1171 case MTIOCEEOT((unsigned long)0x20000000 | ((0 & 0x1fff) << 16) |
((('m')) << 8) | ((4)))
:
1172 break;
1173
1174#if 0
1175 case MTIOCRDSPOS((unsigned long)0x40000000 | ((sizeof(u_int32_t) & 0x1fff
) << 16) | ((('m')) << 8) | ((5)))
:
1176 error = st_rdpos(st, 0, (u_int32_t *) arg);
1177 break;
1178
1179 case MTIOCRDHPOS((unsigned long)0x40000000 | ((sizeof(u_int32_t) & 0x1fff
) << 16) | ((('m')) << 8) | ((6)))
:
1180 error = st_rdpos(st, 1, (u_int32_t *) arg);
1181 break;
1182
1183 case MTIOCSLOCATE((unsigned long)0x80000000 | ((sizeof(u_int32_t) & 0x1fff
) << 16) | ((('m')) << 8) | ((5)))
:
1184 error = st_setpos(st, 0, (u_int32_t *) arg);
1185 break;
1186
1187 case MTIOCHLOCATE((unsigned long)0x80000000 | ((sizeof(u_int32_t) & 0x1fff
) << 16) | ((('m')) << 8) | ((6)))
:
1188 error = st_setpos(st, 1, (u_int32_t *) arg);
1189 break;
1190#endif /* 0 */
1191
1192 default:
1193 error = scsi_do_ioctl(st->sc_link, cmd, arg, flag);
1194 break;
1195 }
1196 goto done;
1197
1198try_new_value:
1199 /*
1200 * Check that the mode being asked for is aggreeable to the
1201 * drive. If not, put it back the way it was.
1202 */
1203 if ((error = st_mode_select(st, 0)) != 0) {/* put it back as it was */
1204 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
1205 st->density = hold_density;
1206 st->blksize = hold_blksize;
1207 if (st->blksize)
1208 SET(st->flags, ST_FIXEDBLOCKS)((st->flags) |= (0x00000008));
1209 else
1210 CLR(st->flags, ST_FIXEDBLOCKS)((st->flags) &= ~(0x00000008));
1211 goto done;
1212 }
1213 /*
1214 * As the drive liked it, if we are setting a new default,
1215 * set it into the structures as such.
1216 */
1217 switch (mt->mt_op) {
1218 case MTSETBSIZ14:
1219 st->mode.blksize = st->blksize;
1220 SET(st->flags, ST_MODE_BLKSIZE)((st->flags) |= (0x00040000));
1221 break;
1222 case MTSETDNSTY15:
1223 st->mode.density = st->density;
1224 SET(st->flags, ST_MODE_DENSITY)((st->flags) |= (0x00010000));
1225 break;
1226 }
1227
1228done:
1229 device_unref(&st->sc_dev);
1230 return error;
1231}
1232
1233/*
1234 * Do a synchronous read.
1235 */
1236int
1237st_read(struct st_softc *st, char *buf, int size, int flags)
1238{
1239 struct scsi_rw_tape *cmd;
1240 struct scsi_xfer *xs;
1241 int error;
1242
1243 if (size == 0)
1244 return 0;
1245
1246 xs = scsi_xs_get(st->sc_link, flags | SCSI_DATA_IN0x00800);
1247 if (xs == NULL((void *)0))
1248 return ENOMEM12;
1249 xs->cmdlen = sizeof(*cmd);
1250 xs->data = buf;
1251 xs->datalen = size;
1252 xs->retries = 0;
1253 xs->timeout = ST_IO_TIME(3 * 60 * 1000);
1254
1255 cmd = (struct scsi_rw_tape *)&xs->cmd;
1256 cmd->opcode = READ0x08;
1257 if (ISSET(st->flags, ST_FIXEDBLOCKS)((st->flags) & (0x00000008))) {
1258 SET(cmd->byte2, SRW_FIXED)((cmd->byte2) |= (0x01));
1259 _lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE512),
1260 cmd->len);
1261 } else
1262 _lto3b(size, cmd->len);
1263
1264 error = scsi_xs_sync(xs);
1265 scsi_xs_put(xs);
1266
1267 return error;
1268}
1269
1270/*
1271 * Ask the drive what its min and max blk sizes are.
1272 */
1273int
1274st_read_block_limits(struct st_softc *st, int flags)
1275{
1276 struct scsi_block_limits_data *block_limits = NULL((void *)0);
1277 struct scsi_block_limits *cmd;
1278 struct scsi_link *link = st->sc_link;
1279 struct scsi_xfer *xs;
1280 int error = 0;
1281
1282 if (ISSET(link->flags, SDEV_MEDIA_LOADED)((link->flags) & (0x0002)))
1283 return 0;
1284
1285 block_limits = dma_alloc(sizeof(*block_limits), PR_NOWAIT0x0002);
1286 if (block_limits == NULL((void *)0))
1287 return ENOMEM12;
1288
1289 xs = scsi_xs_get(link, flags | SCSI_DATA_IN0x00800);
1290 if (xs == NULL((void *)0)) {
1291 error = ENOMEM12;
1292 goto done;
1293 }
1294
1295 xs->cmdlen = sizeof(*cmd);
1296 xs->data = (void *)block_limits;
1297 xs->datalen = sizeof(*block_limits);
1298 xs->timeout = ST_CTL_TIME(30 * 1000);
1299
1300 cmd = (struct scsi_block_limits *)&xs->cmd;
1301 cmd->opcode = READ_BLOCK_LIMITS0x05;
1302
1303 error = scsi_xs_sync(xs);
1304 scsi_xs_put(xs);
1305
1306 if (error == 0) {
1307 st->blkmin = _2btol(block_limits->min_length);
1308 st->blkmax = _3btol(block_limits->max_length);
1309 SC_DEBUG(link, SDEV_DB3,
1310 ("(%d <= blksize <= %d)\n", st->blkmin, st->blkmax));
1311 }
1312
1313done:
1314 if (block_limits)
1315 dma_free(block_limits, sizeof(*block_limits));
1316 return error;
1317}
1318
1319/*
1320 * Get the scsi driver to send a full inquiry to the
1321 * device and use the results to fill out the global
1322 * parameter structure.
1323 *
1324 * called from:
1325 * attach
1326 * open
1327 * ioctl (to reset original blksize)
1328 */
1329int
1330st_mode_sense(struct st_softc *st, int flags)
1331{
1332 union scsi_mode_sense_buf *data = NULL((void *)0);
1333 struct scsi_link *link = st->sc_link;
1334 u_int64_t block_count;
1335 u_int32_t density, block_size;
1336 u_char *page0 = NULL((void *)0);
1337 u_int8_t dev_spec;
1338 int error = 0, big;
1339
1340 data = dma_alloc(sizeof(*data), PR_NOWAIT0x0002);
1341 if (data == NULL((void *)0))
1342 return ENOMEM12;
1343
1344 /*
1345 * Ask for page 0 (vendor specific) mode sense data.
1346 */
1347 density = 0;
1348 block_count = 0;
1349 block_size = 0;
1350 error = scsi_do_mode_sense(link, 0, data, (void **)&page0, 1,
1351 flags | SCSI_SILENT0x00020, &big);
1352 if (error != 0)
1353 goto done;
1354 scsi_parse_blkdesc(link, data, big, &density, &block_count,
1355 &block_size);
1356
1357 /* It is valid for no page0 to be available. */
1358
1359 if (big)
1360 dev_spec = data->hdr_big.dev_spec;
1361 else
1362 dev_spec = data->hdr.dev_spec;
1363
1364 if (ISSET(dev_spec, SMH_DSP_WRITE_PROT)((dev_spec) & (0x80)))
1365 SET(link->flags, SDEV_READONLY)((link->flags) |= (0x0004));
1366 else
1367 CLR(link->flags, SDEV_READONLY)((link->flags) &= ~(0x0004));
1368
1369 st->media_blksize = block_size;
1370 st->media_density = density;
1371
1372 SC_DEBUG(link, SDEV_DB3,
1373 ("density code 0x%x, %d-byte blocks, write-%s, %sbuffered\n",
1374 st->media_density, st->media_blksize,
1375 ISSET(link->flags, SDEV_READONLY) ? "protected" : "enabled",
1376 ISSET(dev_spec, SMH_DSP_BUFF_MODE) ? "" : "un"));
1377
1378 SET(link->flags, SDEV_MEDIA_LOADED)((link->flags) |= (0x0002));
1379
1380 done:
1381 if (data)
1382 dma_free(data, sizeof(*data));
1383 return error;
1384}
1385
1386/*
1387 * Send a filled out parameter structure to the drive to
1388 * set it into the desire mode etc.
1389 */
1390int
1391st_mode_select(struct st_softc *st, int flags)
1392{
1393 union scsi_mode_sense_buf *inbuf = NULL((void *)0), *outbuf = NULL((void *)0);
1394 struct scsi_blk_desc general;
1395 struct scsi_link *link = st->sc_link;
1396 u_int8_t *page0 = NULL((void *)0);
1397 int error = 0, big, page0_size;
1398
1399 inbuf = dma_alloc(sizeof(*inbuf), PR_NOWAIT0x0002);
1400 if (inbuf == NULL((void *)0)) {
1401 error = ENOMEM12;
1402 goto done;
1403 }
1404 outbuf = dma_alloc(sizeof(*outbuf), PR_NOWAIT0x0002 | PR_ZERO0x0008);
1405 if (outbuf == NULL((void *)0)) {
1406 error = ENOMEM12;
1407 goto done;
1408 }
1409
1410 /*
1411 * This quirk deals with drives that have only one valid mode and think
1412 * this gives them license to reject all mode selects, even if the
1413 * selected mode is the one that is supported.
1414 */
1415 if (ISSET(st->quirks, ST_Q_UNIMODAL)((st->quirks) & (0x0004))) {
1416 SC_DEBUG(link, SDEV_DB3,
1417 ("not setting density 0x%x blksize 0x%x\n",
1418 st->density, st->blksize));
1419 error = 0;
1420 goto done;
1421 }
1422
1423 if (ISSET(link->flags, SDEV_ATAPI)((link->flags) & (0x0200))) {
1424 error = 0;
1425 goto done;
1426 }
1427
1428 bzero(&general, sizeof(general))__builtin_bzero((&general), (sizeof(general)));
1429
1430 general.density = st->density;
1431 if (ISSET(st->flags, ST_FIXEDBLOCKS)((st->flags) & (0x00000008)))
1432 _lto3b(st->blksize, general.blklen);
1433
1434 /*
1435 * Ask for page 0 (vendor specific) mode sense data.
1436 *
1437 * page0 == NULL is a valid situation.
1438 */
1439 error = scsi_do_mode_sense(link, 0, inbuf, (void **)&page0, 1,
1440 flags | SCSI_SILENT0x00020, &big);
1441 if (error != 0)
1442 goto done;
1443
1444 if (page0 == NULL((void *)0)) {
1445 page0_size = 0;
1446 } else if (big == 0) {
1447 page0_size = inbuf->hdr.data_length +
1448 sizeof(inbuf->hdr.data_length) - sizeof(inbuf->hdr) -
1449 inbuf->hdr.blk_desc_len;
1450 memcpy(&outbuf->buf[sizeof(outbuf->hdr)+ sizeof(general)],__builtin_memcpy((&outbuf->buf[sizeof(outbuf->hdr)+
sizeof(general)]), (page0), (page0_size))
1451 page0, page0_size)__builtin_memcpy((&outbuf->buf[sizeof(outbuf->hdr)+
sizeof(general)]), (page0), (page0_size))
;
1452 } else {
1453 page0_size = _2btol(inbuf->hdr_big.data_length) +
1454 sizeof(inbuf->hdr_big.data_length) -
1455 sizeof(inbuf->hdr_big) -
1456 _2btol(inbuf->hdr_big.blk_desc_len);
1457 memcpy(&outbuf->buf[sizeof(outbuf->hdr_big) + sizeof(general)],__builtin_memcpy((&outbuf->buf[sizeof(outbuf->hdr_big
) + sizeof(general)]), (page0), (page0_size))
1458 page0, page0_size)__builtin_memcpy((&outbuf->buf[sizeof(outbuf->hdr_big
) + sizeof(general)]), (page0), (page0_size))
;
1459 }
1460
1461 /*
1462 * Set up for a mode select.
1463 */
1464 if (big == 0) {
1465 outbuf->hdr.data_length = sizeof(outbuf->hdr) +
1466 sizeof(general) + page0_size -
1467 sizeof(outbuf->hdr.data_length);
1468 if (!ISSET(st->flags, ST_DONTBUFFER)((st->flags) & (0x00001000)))
1469 outbuf->hdr.dev_spec = SMH_DSP_BUFF_MODE_ON0x10;
1470 outbuf->hdr.blk_desc_len = sizeof(general);
1471 memcpy(&outbuf->buf[sizeof(outbuf->hdr)],__builtin_memcpy((&outbuf->buf[sizeof(outbuf->hdr)]
), (&general), (sizeof(general)))
1472 &general, sizeof(general))__builtin_memcpy((&outbuf->buf[sizeof(outbuf->hdr)]
), (&general), (sizeof(general)))
;
1473 error = scsi_mode_select(st->sc_link, 0, &outbuf->hdr,
1474 flags, ST_CTL_TIME(30 * 1000));
1475 goto done;
1476 }
1477
1478 /* MODE SENSE (10) header was returned, so use MODE SELECT (10). */
1479 _lto2b((sizeof(outbuf->hdr_big) + sizeof(general) + page0_size -
1480 sizeof(outbuf->hdr_big.data_length)), outbuf->hdr_big.data_length);
1481 if (!ISSET(st->flags, ST_DONTBUFFER)((st->flags) & (0x00001000)))
1482 outbuf->hdr_big.dev_spec = SMH_DSP_BUFF_MODE_ON0x10;
1483 _lto2b(sizeof(general), outbuf->hdr_big.blk_desc_len);
1484 memcpy(&outbuf->buf[sizeof(outbuf->hdr_big)], &general,__builtin_memcpy((&outbuf->buf[sizeof(outbuf->hdr_big
)]), (&general), (sizeof(general)))
1485 sizeof(general))__builtin_memcpy((&outbuf->buf[sizeof(outbuf->hdr_big
)]), (&general), (sizeof(general)))
;
1486
1487 error = scsi_mode_select_big(st->sc_link, 0, &outbuf->hdr_big,
1488 flags, ST_CTL_TIME(30 * 1000));
1489done:
1490 if (inbuf)
1491 dma_free(inbuf, sizeof(*inbuf));
1492 if (outbuf)
1493 dma_free(outbuf, sizeof(*outbuf));
1494 return error;
1495}
1496
1497/*
1498 * issue an erase command
1499 */
1500int
1501st_erase(struct st_softc *st, int full, int flags)
1502{
1503 struct scsi_erase *cmd;
1504 struct scsi_xfer *xs;
1505 int error;
1506
1507 xs = scsi_xs_get(st->sc_link, flags);
1508 if (xs == NULL((void *)0))
1509 return ENOMEM12;
1510 xs->cmdlen = sizeof(*cmd);
1511
1512 /*
1513 * Full erase means set LONG bit in erase command, which asks
1514 * the drive to erase the entire unit. Without this bit, we're
1515 * asking the drive to write an erase gap.
1516 */
1517 cmd = (struct scsi_erase *)&xs->cmd;
1518 cmd->opcode = ERASE0x19;
1519 if (full) {
1520 cmd->byte2 = SE_IMMED0x02|SE_LONG0x01;
1521 xs->timeout = ST_SPC_TIME(4 * 60 * 60 * 1000);
1522 } else {
1523 cmd->byte2 = SE_IMMED0x02;
1524 xs->timeout = ST_IO_TIME(3 * 60 * 1000);
1525 }
1526
1527 /*
1528 * XXX We always do this asynchronously, for now. How long should
1529 * we wait if we want to (eventually) to it synchronously?
1530 */
1531 error = scsi_xs_sync(xs);
1532 scsi_xs_put(xs);
1533
1534 return error;
1535}
1536
1537/*
1538 * skip N blocks/filemarks/seq filemarks/eom
1539 */
1540int
1541st_space(struct st_softc *st, int number, u_int what, int flags)
1542{
1543 struct scsi_space *cmd;
1544 struct scsi_xfer *xs;
1545 int error;
1546
1547 switch (what) {
1548 case SP_BLKS0x00:
1549 if (ISSET(st->flags, ST_PER_ACTION)((st->flags) & ((0x00000010 | 0x00000020 | 0x00000040 |
0x00000200)))
) {
1550 if (number > 0) {
1551 CLR(st->flags, ST_PER_ACTION)((st->flags) &= ~((0x00000010 | 0x00000020 | 0x00000040
| 0x00000200)))
;
1552 return EIO5;
1553 } else if (number < 0) {
1554 if (ISSET(st->flags, ST_AT_FILEMARK)((st->flags) & (0x00000010))) {
1555 /*
1556 * Handling of ST_AT_FILEMARK
1557 * in st_space will fill in the
1558 * right file mark count.
1559 */
1560 error = st_space(st, 0, SP_FILEMARKS0x01,
1561 flags);
1562 if (error != 0)
1563 return error;
1564 }
1565 if (ISSET(st->flags, ST_BLANK_READ)((st->flags) & (0x00000200))) {
1566 CLR(st->flags, ST_BLANK_READ)((st->flags) &= ~(0x00000200));
1567 return EIO5;
1568 }
1569 CLR(st->flags, ST_EIO_PENDING | ST_EOM_PENDING)((st->flags) &= ~(0x00000020 | 0x00000040));
1570 }
1571 }
1572 break;
1573 case SP_FILEMARKS0x01:
1574 if (ISSET(st->flags, ST_EIO_PENDING)((st->flags) & (0x00000020))) {
1575 if (number > 0) {
1576 /* pretend we just discovered the error */
1577 CLR(st->flags, ST_EIO_PENDING)((st->flags) &= ~(0x00000020));
1578 return EIO5;
1579 } else if (number < 0) {
1580 /* back away from the error */
1581 CLR(st->flags, ST_EIO_PENDING)((st->flags) &= ~(0x00000020));
1582 }
1583 }
1584 if (ISSET(st->flags, ST_AT_FILEMARK)((st->flags) & (0x00000010))) {
1585 CLR(st->flags, ST_AT_FILEMARK)((st->flags) &= ~(0x00000010));
1586 number--;
1587 }
1588 if (ISSET(st->flags, ST_BLANK_READ)((st->flags) & (0x00000200)) && (number < 0)) {
1589 /* back away from unwritten tape */
1590 CLR(st->flags, ST_BLANK_READ)((st->flags) &= ~(0x00000200));
1591 number++; /* XXX dubious */
1592 }
1593 break;
1594 case SP_EOM0x03:
1595 if (ISSET(st->flags, ST_EOM_PENDING)((st->flags) & (0x00000040))) {
1596 /* We are already there. */
1597 CLR(st->flags, ST_EOM_PENDING)((st->flags) &= ~(0x00000040));
1598 return 0;
1599 }
1600 if (ISSET(st->flags, ST_EIO_PENDING)((st->flags) & (0x00000020))) {
1601 /* pretend we just discovered the error */
1602 CLR(st->flags, ST_EIO_PENDING)((st->flags) &= ~(0x00000020));
1603 return EIO5;
1604 }
1605 if (ISSET(st->flags, ST_AT_FILEMARK)((st->flags) & (0x00000010)))
1606 CLR(st->flags, ST_AT_FILEMARK)((st->flags) &= ~(0x00000010));
1607 break;
1608 }
1609 if (number == 0)
1610 return 0;
1611
1612 xs = scsi_xs_get(st->sc_link, flags);
1613 if (xs == NULL((void *)0))
1614 return ENOMEM12;
1615
1616 cmd = (struct scsi_space *)&xs->cmd;
1617 cmd->opcode = SPACE0x11;
1618 cmd->byte2 = what;
1619 _lto3b(number, cmd->number);
1620 xs->cmdlen = sizeof(*cmd);
1621 xs->timeout = ST_SPC_TIME(4 * 60 * 60 * 1000);
1622
1623 CLR(st->flags, ST_EOD_DETECTED)((st->flags) &= ~(0x00000080));
1624
1625 error = scsi_xs_sync(xs);
1626 scsi_xs_put(xs);
1627
1628 if (error != 0) {
1629 st->media_fileno = -1;
1630 st->media_blkno = -1;
1631 } else {
1632 switch (what) {
1633 case SP_BLKS0x00:
1634 if (st->media_blkno != -1) {
1635 st->media_blkno += number;
1636 if (st->media_blkno < 0)
1637 st->media_blkno = -1;
1638 }
1639 break;
1640 case SP_FILEMARKS0x01:
1641 if (st->media_fileno != -1) {
1642 if (!ISSET(st->flags, ST_EOD_DETECTED)((st->flags) & (0x00000080)))
1643 st->media_fileno += number;
1644 if (st->media_fileno > st->media_eom)
1645 st->media_eom = st->media_fileno;
1646 st->media_blkno = 0;
1647 }
1648 break;
1649 case SP_EOM0x03:
1650 if (st->media_eom != -1) {
1651 st->media_fileno = st->media_eom;
1652 st->media_blkno = 0;
1653 } else {
1654 st->media_fileno = -1;
1655 st->media_blkno = -1;
1656 }
1657 break;
1658 default:
1659 st->media_fileno = -1;
1660 st->media_blkno = -1;
1661 break;
1662 }
1663 }
1664
1665 return error;
1666}
1667
1668/*
1669 * write N filemarks
1670 */
1671int
1672st_write_filemarks(struct st_softc *st, int number, int flags)
1673{
1674 struct scsi_write_filemarks *cmd;
1675 struct scsi_xfer *xs;
1676 int error;
1677
1678 if (number < 0)
1679 return EINVAL22;
1680
1681 xs = scsi_xs_get(st->sc_link, flags);
1682 if (xs == NULL((void *)0))
1683 return ENOMEM12;
1684
1685 xs->cmdlen = sizeof(*cmd);
1686 xs->timeout = ST_IO_TIME(3 * 60 * 1000) * 4;
1687
1688 switch (number) {
1689 case 0: /* really a command to sync the drive's buffers */
1690 break;
1691 case 1:
1692 if (ISSET(st->flags, ST_FM_WRITTEN)((st->flags) & (0x00000100))) /* already have one down */
1693 CLR(st->flags, ST_WRITTEN)((st->flags) &= ~(0x00000004));
1694 else
1695 SET(st->flags, ST_FM_WRITTEN)((st->flags) |= (0x00000100));
1696 CLR(st->flags, ST_PER_ACTION)((st->flags) &= ~((0x00000010 | 0x00000020 | 0x00000040
| 0x00000200)))
;
1697 break;
1698 default:
1699 CLR(st->flags, ST_PER_ACTION | ST_WRITTEN)((st->flags) &= ~((0x00000010 | 0x00000020 | 0x00000040
| 0x00000200) | 0x00000004))
;
1700 break;
1701 }
1702
1703 cmd = (struct scsi_write_filemarks *)&xs->cmd;
1704 cmd->opcode = WRITE_FILEMARKS0x10;
1705 _lto3b(number, cmd->number);
1706
1707 error = scsi_xs_sync(xs);
1708 scsi_xs_put(xs);
1709
1710 if (error != 0) {
1711 st->media_fileno = -1;
1712 st->media_blkno = -1;
1713 st->media_eom = -1;
1714 } else if (st->media_fileno != -1) {
1715 st->media_fileno += number;
1716 st->media_eom = st->media_fileno;
1717 st->media_blkno = 0;
1718 }
1719
1720 return error;
1721}
1722
1723/*
1724 * Make sure the right number of file marks is on tape if the
1725 * tape has been written. If the position argument is true,
1726 * leave the tape positioned where it was originally.
1727 *
1728 * nmarks returns the number of marks to skip (or, if position
1729 * true, which were skipped) to get back original position.
1730 */
1731int
1732st_check_eod(struct st_softc *st, int position, int *nmarks, int flags)
1733{
1734 int error;
1735
1736 switch (st->flags & (ST_WRITTEN0x00000004 | ST_FM_WRITTEN0x00000100 | ST_2FM_AT_EOD0x00000400)) {
1737 default:
1738 *nmarks = 0;
1739 return 0;
1740 case ST_WRITTEN0x00000004:
1741 case ST_WRITTEN0x00000004 | ST_FM_WRITTEN0x00000100 | ST_2FM_AT_EOD0x00000400:
1742 *nmarks = 1;
1743 break;
1744 case ST_WRITTEN0x00000004 | ST_2FM_AT_EOD0x00000400:
1745 *nmarks = 2;
1746 }
1747 error = st_write_filemarks(st, *nmarks, flags);
1748 if (error == 0 && position != 0)
1749 error = st_space(st, -*nmarks, SP_FILEMARKS0x01, flags);
1750 return error;
1751}
1752
1753/*
1754 * load/unload/retension
1755 */
1756int
1757st_load(struct st_softc *st, u_int type, int flags)
1758{
1759 struct scsi_load *cmd;
1760 struct scsi_xfer *xs;
1761 int error, nmarks;
1762
1763 st->media_fileno = -1;
1764 st->media_blkno = -1;
1765 st->media_eom = -1;
1766
1767 if (type != LD_LOAD0x01) {
1768 error = st_check_eod(st, 0, &nmarks, flags);
1769 if (error != 0)
1770 return error;
1771 }
1772
1773 if (ISSET(st->quirks, ST_Q_IGNORE_LOADS)((st->quirks) & (0x0002))) {
1774 if (type == LD_LOAD0x01) {
1775 /*
1776 * If we ignore loads, at least we should try a rewind.
1777 */
1778 return st_rewind(st, 0, flags);
1779 }
1780 return 0;
1781 }
1782
1783
1784 xs = scsi_xs_get(st->sc_link, flags);
1785 if (xs == NULL((void *)0))
1786 return ENOMEM12;
1787 xs->cmdlen = sizeof(*cmd);
1788 xs->timeout = ST_SPC_TIME(4 * 60 * 60 * 1000);
1789
1790 cmd = (struct scsi_load *)&xs->cmd;
1791 cmd->opcode = LOAD0x1b;
1792 cmd->how = type;
1793
1794 error = scsi_xs_sync(xs);
1795 scsi_xs_put(xs);
1796
1797 return error;
1798}
1799
1800/*
1801 * Rewind the device
1802 */
1803int
1804st_rewind(struct st_softc *st, u_int immediate, int flags)
1805{
1806 struct scsi_rewind *cmd;
1807 struct scsi_xfer *xs;
1808 int error, nmarks;
1809
1810 error = st_check_eod(st, 0, &nmarks, flags);
1811 if (error != 0)
1812 return error;
1813 CLR(st->flags, ST_PER_ACTION)((st->flags) &= ~((0x00000010 | 0x00000020 | 0x00000040
| 0x00000200)))
;
1814
1815 xs = scsi_xs_get(st->sc_link, flags);
1816 if (xs == NULL((void *)0))
1817 return ENOMEM12;
1818 xs->cmdlen = sizeof(*cmd);
1819 xs->timeout = immediate ? ST_CTL_TIME(30 * 1000) : ST_SPC_TIME(4 * 60 * 60 * 1000);
1820
1821 cmd = (struct scsi_rewind *)&xs->cmd;
1822 cmd->opcode = REWIND0x01;
1823 cmd->byte2 = immediate;
1824
1825 error = scsi_xs_sync(xs);
1826 scsi_xs_put(xs);
1827
1828 if (error == 0) {
1829 st->media_fileno = 0;
1830 st->media_blkno = 0;
1831 }
1832
1833 return error;
1834}
1835
1836/*
1837 * Look at the returned sense and act on the error to determine
1838 * the unix error number to pass back... (0 = report no error)
1839 * (-1 = continue processing)
1840 */
1841int
1842st_interpret_sense(struct scsi_xfer *xs)
1843{
1844 struct scsi_sense_data *sense = &xs->sense;
1845 struct scsi_link *link = xs->sc_link;
1846 struct scsi_space *space;
1847 struct st_softc *st = link->device_softc;
1848 u_int8_t serr = sense->error_code & SSD_ERRCODE0x7F;
1849 u_int8_t skey = sense->flags & SSD_KEY0x0F;
1850 int32_t resid, info, number;
1851 int datalen;
1852
1853 if (!ISSET(link->flags, SDEV_OPEN)((link->flags) & (0x0008)) ||
1854 (serr != SSD_ERRCODE_CURRENT0x70 && serr != SSD_ERRCODE_DEFERRED0x71))
1855 return scsi_interpret_sense(xs);
1856
1857 info = (int32_t)_4btol(sense->info);
1858
1859 switch (skey) {
1860
1861 /*
1862 * We do custom processing in st for the unit becoming ready case.
1863 * in this case we do not allow xs->retries to be decremented
1864 * only on the "Unit Becoming Ready" case. This is because tape
1865 * drives report "Unit Becoming Ready" when loading media, etc.
1866 * and can take a long time. Rather than having a massive timeout
1867 * for all operations (which would cause other problems) we allow
1868 * operations to wait (but be interruptible with Ctrl-C) forever
1869 * as long as the drive is reporting that it is becoming ready.
1870 * all other cases are handled as per the default.
1871 */
1872
1873 case SKEY_NOT_READY0x02:
1874 if (ISSET(xs->flags, SCSI_IGNORE_NOT_READY)((xs->flags) & (0x00040)))
1875 return 0;
1876 switch (ASC_ASCQ(sense)((sense->add_sense_code << 8) | sense->add_sense_code_qual
)
) {
1877 case SENSE_NOT_READY_BECOMING_READY0x0401:
1878 SC_DEBUG(link, SDEV_DB1, ("not ready: busy (%#x)\n",
1879 sense->add_sense_code_qual));
1880 /* don't count this as a retry */
1881 xs->retries++;
1882 return scsi_delay(xs, 1);
1883 default:
1884 return scsi_interpret_sense(xs);
1885 }
1886 break;
1887 case SKEY_BLANK_CHECK0x08:
1888 if (sense->error_code & SSD_ERRCODE_VALID0x80 &&
1889 xs->cmd.opcode == SPACE0x11) {
1890 switch (ASC_ASCQ(sense)((sense->add_sense_code << 8) | sense->add_sense_code_qual
)
) {
1891 case SENSE_END_OF_DATA_DETECTED0x0005:
1892 SET(st->flags, ST_EOD_DETECTED)((st->flags) |= (0x00000080));
1893 space = (struct scsi_space *)&xs->cmd;
1894 number = _3btol(space->number);
1895 st->media_fileno = number - info;
1896 st->media_eom = st->media_fileno;
1897 return 0;
1898 case SENSE_BEGINNING_OF_MEDIUM_DETECTED0x0004:
1899 /* Standard says: Position is undefined! */
1900 SET(st->flags, ST_BOD_DETECTED)((st->flags) |= (0x00008000));
1901 st->media_fileno = -1;
1902 st->media_blkno = -1;
1903 return 0;
1904 }
1905 }
1906 break;
1907 case SKEY_NO_SENSE0x00:
1908 case SKEY_RECOVERED_ERROR0x01:
1909 case SKEY_MEDIUM_ERROR0x03:
1910 case SKEY_VOLUME_OVERFLOW0x0D:
1911 break;
1912 default:
1913 return scsi_interpret_sense(xs);
1914 }
1915
1916 /*
1917 * 'resid' can be in units of st->blksize or bytes. xs->resid and
1918 * xs->datalen are always in units of bytes. So we need a variable
1919 * to store datalen in the same units as resid and to adjust
1920 * xs->resid to be in bytes.
1921 */
1922 if (ISSET(sense->error_code, SSD_ERRCODE_VALID)((sense->error_code) & (0x80))) {
1923 if (ISSET(st->flags, ST_FIXEDBLOCKS)((st->flags) & (0x00000008)))
1924 resid = info * st->blksize; /* XXXX overflow? */
1925 else
1926 resid = info;
1927 } else {
1928 resid = xs->datalen;
1929 }
1930
1931 if (resid < 0 || resid > xs->datalen)
1932 xs->resid = xs->datalen;
1933 else
1934 xs->resid = resid;
1935
1936 datalen = xs->datalen;
1937 if (ISSET(st->flags, ST_FIXEDBLOCKS)((st->flags) & (0x00000008))) {
1938 resid /= st->blksize;
1939 datalen /= st->blksize;
1940 }
1941
1942 if (ISSET(sense->flags, SSD_FILEMARK)((sense->flags) & (0x80))) {
1943 if (st->media_fileno != -1) {
1944 st->media_fileno++;
1945 if (st->media_fileno > st->media_eom)
1946 st->media_eom = st->media_fileno;
1947 st->media_blkno = 0;
1948 }
1949 if (!ISSET(st->flags, ST_FIXEDBLOCKS)((st->flags) & (0x00000008)))
1950 return 0;
1951 SET(st->flags, ST_AT_FILEMARK)((st->flags) |= (0x00000010));
1952 }
1953
1954 if (ISSET(sense->flags, SSD_EOM)((sense->flags) & (0x40))) {
1955 SET(st->flags, ST_EOM_PENDING)((st->flags) |= (0x00000040));
1956 xs->resid = 0;
1957 if (ISSET(st->flags, ST_FIXEDBLOCKS)((st->flags) & (0x00000008)))
1958 return 0;
1959 }
1960
1961 if (ISSET(sense->flags, SSD_ILI)((sense->flags) & (0x20))) {
1962 if (!ISSET(st->flags, ST_FIXEDBLOCKS)((st->flags) & (0x00000008))) {
1963 if (resid >= 0 && resid <= datalen)
1964 return 0;
1965 if (!ISSET(xs->flags, SCSI_SILENT)((xs->flags) & (0x00020)))
1966 printf( "%s: bad residual %d out of "
1967 "%d\n", st->sc_dev.dv_xname, resid,
1968 datalen);
1969 return EIO5;
1970 }
1971
1972 /* Fixed size blocks. */
1973 if (ISSET(sense->error_code, SSD_ERRCODE_VALID)((sense->error_code) & (0x80)))
1974 if (!ISSET(xs->flags, SCSI_SILENT)((xs->flags) & (0x00020)))
1975 printf("%s: block wrong size, %d blocks "
1976 "residual\n", st->sc_dev.dv_xname, resid);
1977 SET(st->flags, ST_EIO_PENDING)((st->flags) |= (0x00000020));
1978 /*
1979 * This quirk code helps the drive read the first tape block,
1980 * regardless of format. That is required for these drives to
1981 * return proper MODE SENSE information.
1982 */
1983 if (ISSET(st->quirks, ST_Q_SENSE_HELP)((st->quirks) & (0x0001)) &&
1984 !ISSET(link->flags, SDEV_MEDIA_LOADED)((link->flags) & (0x0002)))
1985 st->blksize -= 512;
1986 }
1987
1988 if (ISSET(st->flags, ST_FIXEDBLOCKS)((st->flags) & (0x00000008)) && xs->resid == xs->datalen) {
1989 if (ISSET(st->flags, ST_EIO_PENDING)((st->flags) & (0x00000020)))
1990 return EIO5;
1991 if (ISSET(st->flags, ST_AT_FILEMARK)((st->flags) & (0x00000010)))
1992 return 0;
1993 }
1994
1995 if (skey == SKEY_BLANK_CHECK0x08) {
1996 /*
1997 * This quirk code helps the drive read the first tape block,
1998 * regardless of format. That is required for these drives to
1999 * return proper MODE SENSE information.
2000 */
2001 if (ISSET(st->quirks, ST_Q_SENSE_HELP)((st->quirks) & (0x0001)) &&
2002 !ISSET(link->flags, SDEV_MEDIA_LOADED)((link->flags) & (0x0002))) {
2003 /* still starting */
2004 st->blksize -= 512;
2005 } else if (!ISSET(st->flags, ST_2FM_AT_EOD | ST_BLANK_READ)((st->flags) & (0x00000400 | 0x00000200))) {
2006 SET(st->flags, ST_BLANK_READ)((st->flags) |= (0x00000200));
2007 SET(st->flags, ST_EOM_PENDING)((st->flags) |= (0x00000040));
2008 xs->resid = xs->datalen;
2009 return 0;
2010 }
2011 }
2012
2013 return scsi_interpret_sense(xs);
2014}
2015
2016/*
2017 * The quirk here is that the drive returns some value to st_mode_sense
2018 * incorrectly until the tape has actually passed by the head.
2019 *
2020 * The method is to set the drive to large fixed-block state (user-specified
2021 * density and 1024-byte blocks), then read and rewind to get it to sense the
2022 * tape. If that doesn't work, try 512-byte fixed blocks. If that doesn't
2023 * work, as a last resort, try variable- length blocks. The result will be
2024 * the ability to do an accurate st_mode_sense.
2025 *
2026 * We know we can do a rewind because we just did a load, which implies rewind.
2027 * Rewind seems preferable to space backward if we have a virgin tape.
2028 *
2029 * The rest of the code for this quirk is in ILI processing and BLANK CHECK
2030 * error processing, both part of st_interpret_sense.
2031 */
2032int
2033st_touch_tape(struct st_softc *st)
2034{
2035 char *buf = NULL((void *)0);
2036 int readsize, maxblksize = 1024;
2037 int error = 0;
2038
2039 if ((error = st_mode_sense(st, 0)) != 0)
2040 goto done;
2041 buf = dma_alloc(maxblksize, PR_NOWAIT0x0002);
2042 if (buf == NULL((void *)0)) {
2043 error = ENOMEM12;
2044 goto done;
2045 }
2046
2047 st->blksize = 1024;
2048 do {
2049 switch (st->blksize) {
2050 case 512:
2051 case 1024:
2052 readsize = st->blksize;
2053 SET(st->flags, ST_FIXEDBLOCKS)((st->flags) |= (0x00000008));
2054 break;
2055 default:
2056 readsize = 1;
2057 CLR(st->flags, ST_FIXEDBLOCKS)((st->flags) &= ~(0x00000008));
2058 }
2059 if ((error = st_mode_select(st, 0)) != 0)
2060 goto done;
2061 st_read(st, buf, readsize, SCSI_SILENT0x00020); /* XXX */
2062 if ((error = st_rewind(st, 0, 0)) != 0)
2063 goto done;
2064 } while (readsize != 1 && readsize > st->blksize);
2065done:
2066 dma_free(buf, maxblksize);
2067 return error;
2068}